@@ -198,6 +198,15 @@ if ($Prompt)
198198 $Prompt | Set-Content - Path $promptFile - Encoding UTF8 - NoNewline
199199 Write-Host " Running Claude with prompt from file: $promptFile " - ForegroundColor Cyan
200200
201+ # Tag TeamCity build with the prompt
202+ if ($env: IS_TEAMCITY_AGENT -eq " true" -or $env: IS_TEAMCITY_AGENT -eq " 1" ) {
203+ # Escape special characters for TeamCity service message format
204+ $tagValue = $Prompt -replace ' \|' , ' ||' -replace " '" , " |'" -replace ' \[' , ' |[' -replace ' \]' , ' |]' -replace " `n " , ' |n' -replace " `r " , ' |r'
205+ # Truncate to avoid excessively long tags
206+ if ($tagValue.Length -gt 200 ) { $tagValue = $tagValue.Substring (0 , 200 ) + " ..." }
207+ Write-Host " ##teamcity[addBuildTag tag='$tagValue ']"
208+ }
209+
201210 # Stream JSON output for human-readable real-time monitoring
202211 $processArgs = " -p --output-format stream-json --verbose --model $Model --dangerously-skip-permissions $mcpConfigArg "
203212
@@ -217,8 +226,37 @@ if ($Prompt)
217226 $process.StandardInput.Write ($promptContent )
218227 $process.StandardInput.Close ()
219228
229+ # Set up log file for raw JSON output
230+ $logDir = Join-Path (Resolve-Path " $PSScriptRoot \.." ).Path " artifacts\logs"
231+ New-Item - ItemType Directory - Path $logDir - Force | Out-Null
232+ $timestamp = (Get-Date ).ToString(" yyyy-MM-dd-HHmmss" )
233+ $logFile = Join-Path $logDir " claude-$timestamp .log.json"
234+ $logWriter = [System.IO.StreamWriter ]::new($logFile , $false , [System.Text.Encoding ]::UTF8)
235+ $logWriter.WriteLine (" [" )
236+ $isFirstJsonLine = $true
237+
220238 # Read and parse stdout line by line (real-time streaming)
221239 while ($null -ne ($line = $process.StandardOutput.ReadLine ())) {
240+ # Write to log file in real-time
241+ if (-not [string ]::IsNullOrWhiteSpace($line )) {
242+ try {
243+ $obj = $line | ConvertFrom-Json
244+ $indented = $obj | ConvertTo-Json - Depth 100
245+ if (-not $isFirstJsonLine ) {
246+ $logWriter.WriteLine (" ," )
247+ }
248+ $logWriter.Write ($indented )
249+ $isFirstJsonLine = $false
250+ } catch {
251+ # Non-JSON line - write as raw string
252+ if (-not $isFirstJsonLine ) {
253+ $logWriter.WriteLine (" ," )
254+ }
255+ $logWriter.Write (" `" $ ( $line -replace ' \\' , ' \\\\' -replace ' "' , ' \"' ) `" " )
256+ $isFirstJsonLine = $false
257+ }
258+ $logWriter.Flush ()
259+ }
222260 ConvertFrom-ClaudeJsonLine - Line $line
223261 }
224262
@@ -229,6 +267,12 @@ if ($Prompt)
229267 $process.WaitForExit ()
230268 $exitCode = $process.ExitCode
231269
270+ # Close JSON log file
271+ $logWriter.WriteLine ()
272+ $logWriter.WriteLine (" ]" )
273+ $logWriter.Close ()
274+ Write-Host " Claude output log: $logFile " - ForegroundColor Green
275+
232276 # Clean up prompt file
233277 Remove-Item $promptFile - ErrorAction SilentlyContinue
234278
0 commit comments