-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tasks.ps1
More file actions
237 lines (199 loc) · 6.75 KB
/
run_tasks.ps1
File metadata and controls
237 lines (199 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
param(
[Parameter(Mandatory = $true)]
[string]$RunId,
[string]$Project,
[Parameter(Mandatory = $true)]
[ValidateSet("task-file", "task-json", "prompt-file", "goal")]
[string]$InputMode,
[Parameter(Mandatory = $true)]
[string]$TasksFile,
[switch]$ContinueOnFailure,
[string]$TitlePrefix = "Task",
[string]$Adapter,
[int]$Timeout,
[string]$Policy,
[string[]]$Validation = @(),
[string[]]$Acceptance = @(),
[string]$OrchestratorCtl = $(if ($env:ORCHESTRATORCTL) { $env:ORCHESTRATORCTL } else { "./bin/orchestratorctl" })
)
$ErrorActionPreference = "Stop"
function Write-Stderr {
param([string]$Message)
[Console]::Error.WriteLine($Message)
}
function Fail {
param(
[string]$Message,
[int]$Code = 1
)
Write-Stderr $Message
exit $Code
}
function Invoke-OrchestratorJson {
param([string[]]$Args)
$output = & $OrchestratorCtl @Args
$exitCode = $LASTEXITCODE
$stdoutText = ($output -join "`n")
$payload = $null
if (-not [string]::IsNullOrWhiteSpace($stdoutText)) {
try {
$payload = $stdoutText | ConvertFrom-Json -Depth 20
} catch {
$payload = $null
}
}
return [PSCustomObject]@{
ExitCode = $exitCode
Stdout = $stdoutText
Payload = $payload
}
}
function Get-LatestStepId {
$steps = Invoke-OrchestratorJson @("step", "list", $RunId, "--json")
if ($steps.ExitCode -ne 0) {
return ""
}
if ($steps.Payload -is [System.Array] -and $steps.Payload.Count -gt 0) {
return [string]$steps.Payload[-1].id
}
return ""
}
function Ensure-Run {
$state = Invoke-OrchestratorJson @("run", "state", $RunId, "--json")
if ($state.ExitCode -eq 0) {
if (-not $Project -and $state.Payload) {
$script:Project = [string]$state.Payload.project_id
}
Write-Stderr "Reusing run $RunId"
return
}
if ($state.ExitCode -eq 1 -and $state.Payload -and [string]$state.Payload.status -eq "404") {
if (-not $Project) {
Fail "Run $RunId does not exist. Re-run with --Project <project> so the wrapper can create it."
}
Write-Stderr "Creating run $RunId for project $Project"
$create = Invoke-OrchestratorJson @("run", "start", $RunId, "--project", $Project, "--json")
if ($create.ExitCode -ne 0) {
Fail "Failed to create run $RunId. $($create.Stdout)" $create.ExitCode
}
return
}
Fail "Failed to query run $RunId. $($state.Stdout)" $state.ExitCode
}
if (-not (Test-Path -LiteralPath $TasksFile)) {
Fail "Tasks file not found: $TasksFile"
}
$directArgsUsed = [bool]($Adapter -or $Policy -or $Validation.Count -gt 0 -or $Acceptance.Count -gt 0 -or $PSBoundParameters.ContainsKey("Timeout"))
if (($InputMode -eq "task-file" -or $InputMode -eq "task-json") -and $directArgsUsed) {
Fail "Direct-mode flags (--Adapter, --Timeout, --Policy, --Validation, --Acceptance) are only supported with prompt-file and goal modes."
}
if ($InputMode -ne "goal" -and $PSBoundParameters.ContainsKey("TitlePrefix")) {
if ($TitlePrefix -ne "Task") {
Fail "--TitlePrefix is only supported with goal mode."
}
}
$continueMode = $ContinueOnFailure.IsPresent -or $env:CODENCER_CONTINUE_ON_FAILURE -eq "1"
$taskItems = @()
Get-Content -LiteralPath $TasksFile -Encoding UTF8 | ForEach-Object {
$line = $_.TrimEnd("`r")
if ([string]::IsNullOrWhiteSpace($line)) {
return
}
if ($line.TrimStart().StartsWith("#")) {
return
}
$taskItems += $line
}
if ($taskItems.Count -eq 0) {
Fail "No tasks found in $TasksFile after filtering blank lines and comments."
}
Ensure-Run
$results = @()
$tasksSucceeded = 0
$tasksFailed = 0
$firstNonZeroExit = 0
for ($i = 0; $i -lt $taskItems.Count; $i++) {
$index = $i + 1
$source = $taskItems[$i]
Write-Stderr "[$index/$($taskItems.Count)] submitting $source"
$submitArgs = @("submit", $RunId)
switch ($InputMode) {
"task-file" { $submitArgs += $source }
"task-json" { $submitArgs += @("--task-json", $source) }
"prompt-file" { $submitArgs += @("--prompt-file", $source) }
"goal" {
$submitArgs += @("--goal", $source, "--title", ("{0} {1:d2}" -f $TitlePrefix, $index))
}
}
if ($InputMode -eq "prompt-file" -or $InputMode -eq "goal") {
if ($Adapter) { $submitArgs += @("--adapter", $Adapter) }
if ($PSBoundParameters.ContainsKey("Timeout")) { $submitArgs += @("--timeout", [string]$Timeout) }
if ($Policy) { $submitArgs += @("--policy", $Policy) }
foreach ($item in $Validation) {
$submitArgs += @("--validation", $item)
}
foreach ($item in $Acceptance) {
$submitArgs += @("--acceptance", $item)
}
}
$submitArgs += @("--wait", "--json")
$submit = Invoke-OrchestratorJson $submitArgs
$state = if ($submit.Payload) {
if ($submit.Payload.state) { [string]$submit.Payload.state } elseif ($submit.Payload.error) { [string]$submit.Payload.error } else { "unknown" }
} else {
"unknown"
}
$stepId = ""
if ($submit.Payload) {
if ($submit.Payload.id) {
$stepId = [string]$submit.Payload.id
} elseif ($submit.Payload.step_id) {
$stepId = [string]$submit.Payload.step_id
}
}
if (-not $stepId) {
$stepId = Get-LatestStepId
}
$results += [PSCustomObject]@{
index = $index
source = $source
step_id = $stepId
state = $state
exit_code = $submit.ExitCode
}
if ($submit.ExitCode -eq 0) {
$tasksSucceeded++
} else {
$tasksFailed++
if ($firstNonZeroExit -eq 0) {
$firstNonZeroExit = $submit.ExitCode
}
Write-Stderr "[$index/$($taskItems.Count)] task failed with exit code $($submit.ExitCode) and state $state"
if (-not $continueMode) {
[PSCustomObject]@{
run_id = $RunId
project = $Project
input_mode = $InputMode
continue_on_failure = $false
tasks_total = $taskItems.Count
tasks_succeeded = $tasksSucceeded
tasks_failed = $tasksFailed
results = $results
final_exit_code = $submit.ExitCode
} | ConvertTo-Json -Depth 6
exit $submit.ExitCode
}
}
}
[PSCustomObject]@{
run_id = $RunId
project = $Project
input_mode = $InputMode
continue_on_failure = $continueMode
tasks_total = $taskItems.Count
tasks_succeeded = $tasksSucceeded
tasks_failed = $tasksFailed
results = $results
final_exit_code = $firstNonZeroExit
} | ConvertTo-Json -Depth 6
exit $firstNonZeroExit