pacework 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/pacework-desktop-bridge.mjs +260 -0
- package/bin/pacework-runner-ctl.mjs +3475 -0
- package/bin/pacework-runner.mjs +3847 -0
- package/lib/control-state.mjs +152 -0
- package/lib/environment.mjs +202 -0
- package/lib/legacy-env.mjs +13 -0
- package/lib/logger.mjs +233 -0
- package/lib/service.mjs +270 -0
- package/lib/usage-limit.mjs +79 -0
- package/package.json +19 -4
- package/README.md +0 -3
|
@@ -0,0 +1,3847 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Must run before anything reads the environment (see the file).
|
|
3
|
+
import "../lib/legacy-env.mjs"
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs"
|
|
6
|
+
import os from "node:os"
|
|
7
|
+
import path from "node:path"
|
|
8
|
+
import process from "node:process"
|
|
9
|
+
import { spawn, execFileSync } from "node:child_process"
|
|
10
|
+
import { AsyncLocalStorage } from "node:async_hooks"
|
|
11
|
+
import { createLogger } from "../lib/logger.mjs"
|
|
12
|
+
import { detectUsageLimit } from "../lib/usage-limit.mjs"
|
|
13
|
+
import { prepareRunEnvironment } from "../lib/environment.mjs"
|
|
14
|
+
|
|
15
|
+
function parseArgs(argv) {
|
|
16
|
+
const args = {}
|
|
17
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
18
|
+
const token = argv[i]
|
|
19
|
+
if (!token.startsWith("--")) continue
|
|
20
|
+
const key = token.slice(2)
|
|
21
|
+
const next = argv[i + 1]
|
|
22
|
+
if (!next || next.startsWith("--")) {
|
|
23
|
+
args[key] = "true"
|
|
24
|
+
continue
|
|
25
|
+
}
|
|
26
|
+
args[key] = next
|
|
27
|
+
i += 1
|
|
28
|
+
}
|
|
29
|
+
return args
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const args = parseArgs(process.argv.slice(2))
|
|
33
|
+
|
|
34
|
+
function normalizeLocalAppUrl(value) {
|
|
35
|
+
const raw = String(value || "").trim()
|
|
36
|
+
if (!raw) return "http://127.0.0.1:3000"
|
|
37
|
+
return raw.replace("://localhost:", "://127.0.0.1:")
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const appUrl = normalizeLocalAppUrl(args["app-url"] || process.env.APP_URL || process.env.NEXT_PUBLIC_APP_URL || "http://127.0.0.1:3000")
|
|
41
|
+
const workspaceId = args["workspace-id"] || process.env.WORKSPACE_ID
|
|
42
|
+
const workspaceApiKey = args["workspace-api-key"]
|
|
43
|
+
|| args["launch-token"]
|
|
44
|
+
|| process.env.WORKSPACE_API_KEY
|
|
45
|
+
|| process.env.RUNNER_LAUNCH_TOKEN
|
|
46
|
+
|| null
|
|
47
|
+
const repoRoot = path.resolve(args["repo-root"] || process.env.RUNNER_REPO_ROOT || process.cwd())
|
|
48
|
+
/** The coding tool this runner was started with. */
|
|
49
|
+
const defaultExecutor = args.executor || process.env.RUNNER_EXECUTOR || "codex"
|
|
50
|
+
/**
|
|
51
|
+
* The coding tool for the run in progress. A runner can serve several tools —
|
|
52
|
+
* an agent with an OpenAI key runs on Codex, one with an Anthropic key on Claude
|
|
53
|
+
* Code — so each run sets its own, and everything inside it reads this.
|
|
54
|
+
*/
|
|
55
|
+
const runExecutorScope = new AsyncLocalStorage()
|
|
56
|
+
function activeExecutor() {
|
|
57
|
+
return runExecutorScope.getStore() ?? defaultExecutor
|
|
58
|
+
}
|
|
59
|
+
const pollIntervalMs = Number(args["poll-interval-ms"] || process.env.RUNNER_POLL_INTERVAL_MS || 10000)
|
|
60
|
+
const requestedMaxParallelRuns = Math.max(1, Number(args["max-parallel-runs"] || process.env.RUNNER_MAX_PARALLEL_RUNS || 1))
|
|
61
|
+
// Gemini and Cursor read MCP config from one file in the user's home directory,
|
|
62
|
+
// so two parallel runs would overwrite each other's key and act as the wrong agent.
|
|
63
|
+
const SINGLE_RUN_EXECUTORS = new Set(["gemini", "cursor_agent"])
|
|
64
|
+
const maxParallelRuns = SINGLE_RUN_EXECUTORS.has(defaultExecutor) ? 1 : requestedMaxParallelRuns
|
|
65
|
+
/**
|
|
66
|
+
* Run agents inside their tool's OS sandbox (Seatbelt on macOS, bubblewrap on
|
|
67
|
+
* Linux): writes only to the ticket's checkout and the repo's git data, and for
|
|
68
|
+
* Claude Code, network only to the domains below. Workspaces turn it on for
|
|
69
|
+
* every run (Settings → Automation, on by default); this flag turns it on for
|
|
70
|
+
* this machine whatever the workspace says.
|
|
71
|
+
*/
|
|
72
|
+
const sandboxed = ["true", "1", "yes"].includes(String(args.sandbox ?? process.env.RUNNER_SANDBOX ?? "").toLowerCase())
|
|
73
|
+
const SANDBOX_ALLOWED_DOMAINS = (args["sandbox-allow-domains"] || process.env.RUNNER_SANDBOX_ALLOW_DOMAINS
|
|
74
|
+
|| "registry.npmjs.org,registry.yarnpkg.com,pypi.org,files.pythonhosted.org,github.com,api.github.com,codeload.github.com,objects.githubusercontent.com")
|
|
75
|
+
.split(",").map((domain) => domain.trim()).filter(Boolean)
|
|
76
|
+
const registerOnly = (args["register-only"] || "").toLowerCase() === "true"
|
|
77
|
+
/**
|
|
78
|
+
* Set by `pacework run-cloud` in a GitHub Actions job: a registration bound
|
|
79
|
+
* to the one run the job was started for. The runner uses it as is — no state
|
|
80
|
+
* file, no registration — and stops after that run.
|
|
81
|
+
*/
|
|
82
|
+
const cloudState = (() => {
|
|
83
|
+
try {
|
|
84
|
+
return process.env.PACEWORK_CLOUD_STATE ? JSON.parse(process.env.PACEWORK_CLOUD_STATE) : null
|
|
85
|
+
} catch {
|
|
86
|
+
return null
|
|
87
|
+
}
|
|
88
|
+
})()
|
|
89
|
+
const once = (args.once || "").toLowerCase() === "true" || Boolean(cloudState)
|
|
90
|
+
/**
|
|
91
|
+
* In a cloud job: the repository's PACEWORK_ENV_* secrets, already stripped of the
|
|
92
|
+
* prefix by run-cloud. Taken out of this process's environment so only the
|
|
93
|
+
* agent's run gets them, not every child process.
|
|
94
|
+
*/
|
|
95
|
+
const cloudAgentSecrets = (() => {
|
|
96
|
+
const raw = process.env.PACEWORK_AGENT_ENV
|
|
97
|
+
delete process.env.PACEWORK_AGENT_ENV
|
|
98
|
+
try {
|
|
99
|
+
return raw ? JSON.parse(raw) : {}
|
|
100
|
+
} catch {
|
|
101
|
+
return {}
|
|
102
|
+
}
|
|
103
|
+
})()
|
|
104
|
+
const maxRuns = Number(args["max-runs"] || process.env.RUNNER_MAX_RUNS || (once ? 1 : Number.POSITIVE_INFINITY))
|
|
105
|
+
|
|
106
|
+
const runnerName = args.name || process.env.RUNNER_NAME || `${os.hostname()} ${defaultExecutor}`
|
|
107
|
+
const machineName = args["machine-name"] || process.env.RUNNER_MACHINE_NAME || os.hostname()
|
|
108
|
+
const labels = (args.labels || process.env.RUNNER_LABELS || "")
|
|
109
|
+
.split(",")
|
|
110
|
+
.map(value => value.trim())
|
|
111
|
+
.filter(Boolean)
|
|
112
|
+
const runnerBaseLogger = createLogger({
|
|
113
|
+
service: "runner",
|
|
114
|
+
component: "local_agent_runner",
|
|
115
|
+
workspace_id: workspaceId || undefined,
|
|
116
|
+
meta: {
|
|
117
|
+
local_executor: defaultExecutor,
|
|
118
|
+
machine_name: machineName,
|
|
119
|
+
repo_root: repoRoot,
|
|
120
|
+
labels,
|
|
121
|
+
},
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
if (!workspaceId) {
|
|
125
|
+
runnerBaseLogger.fatal("runner.configuration.invalid", "Missing workspace id. Set --workspace-id or WORKSPACE_ID.")
|
|
126
|
+
process.exit(1)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const stateDir = path.resolve(args["state-dir"] || process.env.RUNNER_STATE_DIR || path.join(repoRoot, ".agent", "runners"))
|
|
130
|
+
const stateFile = path.resolve(
|
|
131
|
+
args["state-file"]
|
|
132
|
+
|| process.env.RUNNER_STATE_FILE
|
|
133
|
+
|| path.join(stateDir, `${workspaceId}-${defaultExecutor}.json`),
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
const codexBin = args["codex-bin"] || process.env.CODEX_BIN || "codex"
|
|
137
|
+
const claudeCodeBin = args["claude-code-bin"] || process.env.CLAUDE_CODE_BIN || "claude"
|
|
138
|
+
const geminiBin = args["gemini-bin"] || process.env.GEMINI_BIN || "gemini"
|
|
139
|
+
const cursorAgentBin = args["cursor-agent-bin"] || process.env.CURSOR_AGENT_BIN || "cursor-agent"
|
|
140
|
+
const githubCopilotBin = args["github-copilot-bin"] || process.env.GITHUB_COPILOT_BIN || "gh"
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The coding tools installed on this machine. The runner claims runs for any of
|
|
144
|
+
* them — an agent on an OpenAI key needs Codex even when this runner was set up
|
|
145
|
+
* for Claude Code — and starts each run with its own.
|
|
146
|
+
*/
|
|
147
|
+
const installedExecutors = (() => {
|
|
148
|
+
// Not Gemini or Cursor: they keep their settings in one file per machine, so
|
|
149
|
+
// a run on them cannot share the machine with another (SINGLE_RUN_EXECUTORS).
|
|
150
|
+
const candidates = { claude_code: claudeCodeBin, codex: codexBin }
|
|
151
|
+
const found = new Set([defaultExecutor])
|
|
152
|
+
for (const [executor, bin] of Object.entries(candidates)) {
|
|
153
|
+
try {
|
|
154
|
+
execFileSync(process.platform === "win32" ? "where" : "which", [bin], { stdio: "ignore" })
|
|
155
|
+
found.add(executor)
|
|
156
|
+
} catch {
|
|
157
|
+
// not installed
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return [...found]
|
|
161
|
+
})()
|
|
162
|
+
|
|
163
|
+
/** The tool a claimed run asks for, when this machine has it. */
|
|
164
|
+
function executorForRun(run) {
|
|
165
|
+
return installedExecutors.includes(run.local_executor) ? run.local_executor : defaultExecutor
|
|
166
|
+
}
|
|
167
|
+
const codexModel = (process.env.CODEX_MODEL || "gpt-5.4").trim()
|
|
168
|
+
const codexReasoningEffortRaw = (args["codex-reasoning-effort"] || process.env.CODEX_REASONING_EFFORT || "medium").trim().toLowerCase()
|
|
169
|
+
const codexReasoningEffort = ["low", "medium", "high", "xhigh"].includes(codexReasoningEffortRaw)
|
|
170
|
+
? codexReasoningEffortRaw
|
|
171
|
+
: "medium"
|
|
172
|
+
const runnerModelPreferences = normalizeModelPreferenceList(args["model-preferences"] || process.env.RUNNER_MODEL_PREFERENCES || "")
|
|
173
|
+
const autoCommitOnSuccessFlag = (args["auto-commit-on-success"] || process.env.RUNNER_AUTO_COMMIT_ON_SUCCESS || "true").toLowerCase() === "true"
|
|
174
|
+
const autoPushOnSuccessFlag = (args["auto-push-on-success"] || process.env.RUNNER_AUTO_PUSH_ON_SUCCESS || "true").toLowerCase() === "true"
|
|
175
|
+
const autoMergeToMainFlag = (args["auto-merge-to-main"] || process.env.RUNNER_AUTO_MERGE_TO_MAIN || "").toLowerCase() === "true"
|
|
176
|
+
const automationSmokeTestMode = (args["smoke-test"] || process.env.AUTOMATION_SMOKE_TEST_MODE || "").toLowerCase() === "true" || process.env.AUTOMATION_SMOKE_TEST_MODE === "1"
|
|
177
|
+
const automationSmokeTestMarker = "[AUTOMATION_SMOKE_TEST]"
|
|
178
|
+
|
|
179
|
+
function getRunnerLogger(state) {
|
|
180
|
+
return runnerBaseLogger.child({
|
|
181
|
+
workspace_id: state?.workspace_id || workspaceId || undefined,
|
|
182
|
+
runner_id: state?.runner_id || undefined,
|
|
183
|
+
meta: {
|
|
184
|
+
local_executor: state?.local_executor || defaultExecutor,
|
|
185
|
+
machine_name: state?.machine_name || machineName,
|
|
186
|
+
repo_root: state?.repo_root || repoRoot,
|
|
187
|
+
},
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function getRunLogger(state, run) {
|
|
192
|
+
return getRunnerLogger(state).child({
|
|
193
|
+
run_id: run?.id || undefined,
|
|
194
|
+
task_id: run?.task_id || undefined,
|
|
195
|
+
agent_id: run?.agent_id || run?.agent?.id || undefined,
|
|
196
|
+
})
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function executorDisplayName(executor = activeExecutor()) {
|
|
200
|
+
switch (executor) {
|
|
201
|
+
case "claude_code":
|
|
202
|
+
return "Claude Code"
|
|
203
|
+
case "gemini":
|
|
204
|
+
return "Gemini CLI"
|
|
205
|
+
case "cursor_agent":
|
|
206
|
+
return "Cursor Agent"
|
|
207
|
+
case "github_copilot":
|
|
208
|
+
return "GitHub Copilot"
|
|
209
|
+
case "ollama":
|
|
210
|
+
return "Ollama"
|
|
211
|
+
case "codex":
|
|
212
|
+
default:
|
|
213
|
+
return "Codex"
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function normalizeModelPreferenceList(value) {
|
|
218
|
+
const seen = new Set()
|
|
219
|
+
return String(value || "")
|
|
220
|
+
.split(/[\n,]/g)
|
|
221
|
+
.map(entry => entry.trim())
|
|
222
|
+
.filter((entry) => {
|
|
223
|
+
if (!entry || seen.has(entry)) return false
|
|
224
|
+
seen.add(entry)
|
|
225
|
+
return true
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function getAgentDefaultModel(executor = activeExecutor()) {
|
|
230
|
+
switch (executor) {
|
|
231
|
+
case "claude_code":
|
|
232
|
+
return "claude-sonnet-4-6"
|
|
233
|
+
case "gemini":
|
|
234
|
+
return "gemini-2.5-pro"
|
|
235
|
+
case "cursor_agent":
|
|
236
|
+
return "gpt-5"
|
|
237
|
+
case "github_copilot":
|
|
238
|
+
return "gpt-5.4"
|
|
239
|
+
case "codex":
|
|
240
|
+
default:
|
|
241
|
+
return "gpt-5.4"
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Kept for callers that still want a concrete name. Model resolution no longer
|
|
247
|
+
* falls back to these — an unspecified model means "let the executor decide".
|
|
248
|
+
*/
|
|
249
|
+
function getRunnerDefaultModel(executor = activeExecutor()) {
|
|
250
|
+
if (executor === "codex") {
|
|
251
|
+
return codexModel
|
|
252
|
+
}
|
|
253
|
+
return getAgentDefaultModel(executor)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function executorSupportsModel(executor = activeExecutor(), model) {
|
|
257
|
+
const normalized = String(model || "").trim().toLowerCase()
|
|
258
|
+
if (!normalized) return false
|
|
259
|
+
|
|
260
|
+
switch (executor) {
|
|
261
|
+
case "claude_code":
|
|
262
|
+
// Claude Code's aliases always mean the latest of each family.
|
|
263
|
+
return normalized.startsWith("claude") || ["opus", "sonnet", "haiku", "opusplan"].includes(normalized)
|
|
264
|
+
case "gemini":
|
|
265
|
+
return normalized.startsWith("gemini")
|
|
266
|
+
case "codex":
|
|
267
|
+
return !normalized.startsWith("gemini") && !normalized.startsWith("claude")
|
|
268
|
+
case "cursor_agent":
|
|
269
|
+
case "github_copilot":
|
|
270
|
+
default:
|
|
271
|
+
return true
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function isExecutorDefaultAgentModel(executor = activeExecutor(), model) {
|
|
276
|
+
return String(model || "").trim() === getAgentDefaultModel(executor)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* API providers an agent can run on, and how each reaches its coding tool.
|
|
281
|
+
* Claude Code speaks Anthropic's API, which DeepSeek and other providers also
|
|
282
|
+
* offer at their own base URL; Codex speaks OpenAI's.
|
|
283
|
+
*/
|
|
284
|
+
const API_PROVIDERS = {
|
|
285
|
+
anthropic: { executor: "claude_code" },
|
|
286
|
+
deepseek: { executor: "claude_code", baseUrl: "https://api.deepseek.com/anthropic", defaultModel: "deepseek-chat" },
|
|
287
|
+
anthropic_compatible: { executor: "claude_code" },
|
|
288
|
+
openai: { executor: "codex" },
|
|
289
|
+
// A Claude Pro/Max token from `claude setup-token`: the subscription, usable
|
|
290
|
+
// where nobody can sign in — a GitHub Actions job.
|
|
291
|
+
claude_subscription: { executor: "claude_code" },
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** The agent's API key for this run, if it has one (see resolveRunCredentials on the server). */
|
|
295
|
+
function runCredentials(run) {
|
|
296
|
+
const credentials = run?.api_credentials
|
|
297
|
+
return credentials && typeof credentials.key === "string" && API_PROVIDERS[credentials.provider] ? credentials : null
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** A provider other than Anthropic or OpenAI: its model names are its own. */
|
|
301
|
+
function usesThirdPartyModels(run) {
|
|
302
|
+
const credentials = runCredentials(run)
|
|
303
|
+
return Boolean(credentials && !["anthropic", "openai", "claude_subscription"].includes(credentials.provider))
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function resolveRequestedModel(run) {
|
|
307
|
+
const explicitModel = typeof run.agent?.model === "string" ? run.agent.model.trim() : ""
|
|
308
|
+
// DeepSeek and other providers name their own models; the Claude/OpenAI
|
|
309
|
+
// checks and this machine's model preferences do not apply to them.
|
|
310
|
+
if (usesThirdPartyModels(run)) {
|
|
311
|
+
return explicitModel || API_PROVIDERS[runCredentials(run).provider].defaultModel || ""
|
|
312
|
+
}
|
|
313
|
+
const preferredModel = runnerModelPreferences.find((model) => executorSupportsModel(activeExecutor(), model)) || ""
|
|
314
|
+
|
|
315
|
+
if (preferredModel && (!explicitModel || isExecutorDefaultAgentModel(activeExecutor(), explicitModel) || !executorSupportsModel(activeExecutor(), explicitModel))) {
|
|
316
|
+
return preferredModel
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (executorSupportsModel(activeExecutor(), explicitModel)) {
|
|
320
|
+
return explicitModel
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (preferredModel) {
|
|
324
|
+
return preferredModel
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Nothing was asked for, so pass no --model and let the executor use its own
|
|
328
|
+
// current default. Returning a hardcoded name here pinned every run to whatever
|
|
329
|
+
// pacework's table said at the time, which silently goes stale as executors ship
|
|
330
|
+
// newer models.
|
|
331
|
+
return ""
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function ensureDir(dir) {
|
|
335
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function sleep(ms) {
|
|
339
|
+
return new Promise(resolve => setTimeout(resolve, ms))
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Until when this runner claims nothing because the executor's plan limit was
|
|
344
|
+
* hit. Every run would fail the same way until the limit resets.
|
|
345
|
+
*/
|
|
346
|
+
let usageLimitPausedUntil = 0
|
|
347
|
+
|
|
348
|
+
function formatLocalTime(date) {
|
|
349
|
+
return date.toLocaleString(undefined, { weekday: "short", hour: "2-digit", minute: "2-digit", timeZoneName: "short" })
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function truncate(value, max = 6000) {
|
|
353
|
+
if (!value) return ""
|
|
354
|
+
return value.length <= max ? value : `${value.slice(-max)}`
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function truncateHead(value, max = 40000) {
|
|
358
|
+
if (!value) return ""
|
|
359
|
+
if (value.length <= max) return value
|
|
360
|
+
const omitted = value.length - max
|
|
361
|
+
return `${value.slice(0, max)}\n\n... diff excerpt truncated (${omitted} more chars omitted)`
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Claude Code's --output-format json result: the final message and what the run
|
|
366
|
+
* cost. Null when the output is not that JSON (an older CLI, a crash).
|
|
367
|
+
*/
|
|
368
|
+
function parseClaudeJsonResult(stdout) {
|
|
369
|
+
const lines = String(stdout || "").trim().split(/\r?\n/).reverse()
|
|
370
|
+
for (const line of lines) {
|
|
371
|
+
if (!line.trim().startsWith("{")) continue
|
|
372
|
+
try {
|
|
373
|
+
const parsed = JSON.parse(line)
|
|
374
|
+
if (parsed?.type !== "result") continue
|
|
375
|
+
const usage = parsed.usage ?? {}
|
|
376
|
+
const inputTokens = [usage.input_tokens, usage.cache_creation_input_tokens, usage.cache_read_input_tokens]
|
|
377
|
+
.filter((value) => typeof value === "number")
|
|
378
|
+
return {
|
|
379
|
+
text: typeof parsed.result === "string" ? parsed.result : "",
|
|
380
|
+
isError: parsed.is_error === true,
|
|
381
|
+
usage: {
|
|
382
|
+
cost_usd: typeof parsed.total_cost_usd === "number" ? parsed.total_cost_usd : null,
|
|
383
|
+
input_tokens: inputTokens.length ? inputTokens.reduce((a, b) => a + b, 0) : null,
|
|
384
|
+
output_tokens: typeof usage.output_tokens === "number" ? usage.output_tokens : null,
|
|
385
|
+
num_turns: typeof parsed.num_turns === "number" ? parsed.num_turns : null,
|
|
386
|
+
},
|
|
387
|
+
}
|
|
388
|
+
} catch {
|
|
389
|
+
// not the result line
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return null
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function formatChildExitError(executor, code, output) {
|
|
396
|
+
const tail = truncate(String(output || "").trim(), 4000)
|
|
397
|
+
return `${executor} exited with code ${code}${tail ? `\n\nLast output:\n${tail}` : ""}`
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function appendLogTail(logState, value) {
|
|
401
|
+
const text = String(value || "").trimEnd()
|
|
402
|
+
if (!text) return
|
|
403
|
+
logState.tail = truncate(`${logState.tail ? `${logState.tail}\n` : ""}${text}`, 12000)
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function formatElapsed(ms) {
|
|
407
|
+
const totalSeconds = Math.max(0, Math.floor(ms / 1000))
|
|
408
|
+
const minutes = Math.floor(totalSeconds / 60)
|
|
409
|
+
const seconds = totalSeconds % 60
|
|
410
|
+
if (minutes <= 0) return `${seconds}s`
|
|
411
|
+
return `${minutes}m ${String(seconds).padStart(2, "0")}s`
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function previewExecutorCommand(execSpec) {
|
|
415
|
+
const redactedArgs = []
|
|
416
|
+
for (let index = 0; index < execSpec.args.length; index += 1) {
|
|
417
|
+
const arg = String(execSpec.args[index] ?? "")
|
|
418
|
+
if (arg === "-p" || arg === "--prompt") {
|
|
419
|
+
redactedArgs.push(arg)
|
|
420
|
+
if (index + 1 < execSpec.args.length) {
|
|
421
|
+
redactedArgs.push("<prompt>")
|
|
422
|
+
index += 1
|
|
423
|
+
}
|
|
424
|
+
continue
|
|
425
|
+
}
|
|
426
|
+
if (arg === "--mcp-config") {
|
|
427
|
+
redactedArgs.push(arg)
|
|
428
|
+
if (index + 1 < execSpec.args.length) {
|
|
429
|
+
redactedArgs.push("<mcp-config>")
|
|
430
|
+
index += 1
|
|
431
|
+
}
|
|
432
|
+
continue
|
|
433
|
+
}
|
|
434
|
+
if (arg.length > 240) {
|
|
435
|
+
redactedArgs.push(`${arg.slice(0, 120)}…${arg.slice(-40)}`)
|
|
436
|
+
continue
|
|
437
|
+
}
|
|
438
|
+
redactedArgs.push(arg)
|
|
439
|
+
}
|
|
440
|
+
return [execSpec.command, ...redactedArgs].map(shellQuote).join(" ")
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function summarizeDiffStat(value) {
|
|
444
|
+
const lines = String(value || "")
|
|
445
|
+
.split(/\r?\n/)
|
|
446
|
+
.map(line => line.trim())
|
|
447
|
+
.filter(Boolean)
|
|
448
|
+
return lines.at(-1) || null
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function shellQuote(value) {
|
|
452
|
+
return `'${String(value).replace(/'/g, `'\\''`)}'`
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function extractSection(summary, heading) {
|
|
456
|
+
if (!summary) return ""
|
|
457
|
+
const lines = summary.split(/\r?\n/)
|
|
458
|
+
const normalizedHeading = heading.toLowerCase()
|
|
459
|
+
let collecting = false
|
|
460
|
+
const collected = []
|
|
461
|
+
|
|
462
|
+
for (const line of lines) {
|
|
463
|
+
const trimmed = line.trim()
|
|
464
|
+
const headingMatch = /^#{1,6}\s*(.+)$/.exec(trimmed)
|
|
465
|
+
if (headingMatch) {
|
|
466
|
+
const currentHeading = headingMatch[1].trim().toLowerCase()
|
|
467
|
+
if (collecting && currentHeading !== normalizedHeading) break
|
|
468
|
+
collecting = currentHeading === normalizedHeading
|
|
469
|
+
continue
|
|
470
|
+
}
|
|
471
|
+
if (collecting) {
|
|
472
|
+
collected.push(line)
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return collected.join("\n").trim()
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function buildExecutionReportMarkdown(input) {
|
|
480
|
+
const changedFileLines = input.changedFiles.length
|
|
481
|
+
? input.changedFiles.map(file => `- \`${file}\``)
|
|
482
|
+
: ["- No file changes were produced."]
|
|
483
|
+
|
|
484
|
+
const locationLine = input.localCheckoutMode === "repo_root"
|
|
485
|
+
? `Repo root: \`${input.worktreePath}\`${input.branch ? ` on branch \`${input.branch}\`` : ""}`
|
|
486
|
+
: `Ticket branch: \`${input.branch}\``
|
|
487
|
+
|
|
488
|
+
return [
|
|
489
|
+
"# Execution Report",
|
|
490
|
+
"",
|
|
491
|
+
`Generated at: ${new Date().toISOString()}`,
|
|
492
|
+
`Executor: ${executorDisplayName()}`,
|
|
493
|
+
locationLine,
|
|
494
|
+
`Artifacts: \`${input.artifactsFile}\``,
|
|
495
|
+
`Report: \`${input.reportFile}\``,
|
|
496
|
+
input.commitResult ? `Commit: \`${input.commitResult.sha}\` - ${input.commitResult.subject}` : "Commit: not created automatically.",
|
|
497
|
+
"",
|
|
498
|
+
"## Summary",
|
|
499
|
+
input.summary.trim() || "Run finished without a final summary.",
|
|
500
|
+
"",
|
|
501
|
+
"## Verification",
|
|
502
|
+
input.verificationSummary?.trim() || "No verification summary was reported by the runner.",
|
|
503
|
+
"",
|
|
504
|
+
"## Diff Stat",
|
|
505
|
+
input.diffStat?.trim() || "No diff stat was reported.",
|
|
506
|
+
"",
|
|
507
|
+
"## Changed Files",
|
|
508
|
+
...changedFileLines,
|
|
509
|
+
].join("\n")
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function buildRunnerCompletionComment(input) {
|
|
513
|
+
const changeSummary = input.hasChanges
|
|
514
|
+
? `${input.changedFiles.length} file${input.changedFiles.length === 1 ? "" : "s"} changed${input.diffSummary ? ` (${input.diffSummary})` : ""}.`
|
|
515
|
+
: "No file changes."
|
|
516
|
+
|
|
517
|
+
const verificationState = input.verificationSummary?.trim()
|
|
518
|
+
? "Verification recorded."
|
|
519
|
+
: "No verification summary."
|
|
520
|
+
|
|
521
|
+
return [
|
|
522
|
+
`Run complete${input.branch ? ` on \`${input.branch}\`` : ""}.`,
|
|
523
|
+
changeSummary,
|
|
524
|
+
verificationState,
|
|
525
|
+
`Full report: \`${input.reportFile}\`.`,
|
|
526
|
+
].join(" ")
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Whether a run is a review. The server says so in `purpose`, from the issue's
|
|
531
|
+
* workflow stage or the agent's role; the skill slug is only a fallback for
|
|
532
|
+
* servers that predate it. Deciding by slug made a renamed reviewer build code.
|
|
533
|
+
*/
|
|
534
|
+
function isReviewRun(run) {
|
|
535
|
+
if (run?.purpose) return run.purpose === "review"
|
|
536
|
+
return getAgentSkillProfileSlug(run?.agent) === "reviewer"
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function getAgentSkillProfileSlug(agent) {
|
|
540
|
+
if (!agent || typeof agent !== "object") return null
|
|
541
|
+
const direct = agent.skill_profile
|
|
542
|
+
if (direct && typeof direct === "object" && typeof direct.slug === "string") return direct.slug
|
|
543
|
+
const nested = agent.skill_profiles
|
|
544
|
+
if (Array.isArray(nested)) {
|
|
545
|
+
const first = nested.find((entry) => entry && typeof entry.slug === "string")
|
|
546
|
+
return first?.slug || null
|
|
547
|
+
}
|
|
548
|
+
if (nested && typeof nested === "object" && typeof nested.slug === "string") return nested.slug
|
|
549
|
+
return null
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function parseReviewerDecisionSummary(summary) {
|
|
553
|
+
const text = String(summary || "").trim()
|
|
554
|
+
if (!text) return null
|
|
555
|
+
|
|
556
|
+
const decisionPattern = /^\s*REVIEW_DECISION\s*:?\s*(?:\n\s*:\s*)?(approve|request_changes|block)\s*$/im
|
|
557
|
+
const summaryPattern = /^\s*REVIEW_SUMMARY\s*:?\s*(?:\n\s*:\s*)?(.+)$/im
|
|
558
|
+
const decisionMatch = text.match(decisionPattern)
|
|
559
|
+
if (!decisionMatch) return null
|
|
560
|
+
|
|
561
|
+
const decision = decisionMatch[1]
|
|
562
|
+
// The review is everything after the REVIEW_SUMMARY line: a structured,
|
|
563
|
+
// multi-line write-up, not the single line this used to keep.
|
|
564
|
+
const summaryMatch = summaryPattern.exec(text)
|
|
565
|
+
const review = summaryMatch
|
|
566
|
+
? text.slice(summaryMatch.index).replace(summaryPattern, "$1").replace(decisionPattern, "").trim()
|
|
567
|
+
: text.replace(decisionPattern, "").trim()
|
|
568
|
+
|
|
569
|
+
return {
|
|
570
|
+
decision,
|
|
571
|
+
summary: truncate(review || "Reviewer decision submitted.", 6000),
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
async function maybeSubmitReviewerDecision(run, mcpKey, summary) {
|
|
576
|
+
if (!isReviewRun(run)) return null
|
|
577
|
+
|
|
578
|
+
const issue = await callMcpTool(
|
|
579
|
+
mcpKey,
|
|
580
|
+
"get_issue",
|
|
581
|
+
{
|
|
582
|
+
issue_id: run.task_id,
|
|
583
|
+
workspace_id: run.workspace_id,
|
|
584
|
+
},
|
|
585
|
+
run.agent_id,
|
|
586
|
+
)
|
|
587
|
+
|
|
588
|
+
if (!issue || issue.agent_id !== run.agent_id || issue.status !== "in_review") {
|
|
589
|
+
return null
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
const parsed = parseReviewerDecisionSummary(summary)
|
|
593
|
+
if (!parsed) {
|
|
594
|
+
// The reviewer may have recorded its decision with submit_issue_review or
|
|
595
|
+
// review_pr during the run; the server reads it from the run. If it did
|
|
596
|
+
// neither, the server hands the review to a person — failing the run here
|
|
597
|
+
// only triggered a retry of the same review.
|
|
598
|
+
return null
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// review_pr posts a real GitHub review AND records the pacework decision in one
|
|
602
|
+
// call. submit_issue_review only did the latter, so the reviewer's findings
|
|
603
|
+
// never reached the pull request. Fall back to it when there is no PR yet.
|
|
604
|
+
const hasPullRequest = Boolean(issue?.pr_url)
|
|
605
|
+
const toolName = hasPullRequest && parsed.decision !== "block"
|
|
606
|
+
? "review_pr"
|
|
607
|
+
: "submit_issue_review"
|
|
608
|
+
|
|
609
|
+
try {
|
|
610
|
+
await callMcpTool(
|
|
611
|
+
mcpKey,
|
|
612
|
+
toolName,
|
|
613
|
+
{
|
|
614
|
+
issue_id: run.task_id,
|
|
615
|
+
workspace_id: run.workspace_id,
|
|
616
|
+
decision: parsed.decision,
|
|
617
|
+
summary: parsed.summary,
|
|
618
|
+
},
|
|
619
|
+
run.agent_id,
|
|
620
|
+
)
|
|
621
|
+
} catch (error) {
|
|
622
|
+
if (toolName === "review_pr") {
|
|
623
|
+
// A GitHub failure must not lose the decision itself.
|
|
624
|
+
console.error(
|
|
625
|
+
"runner.review.github_post_failed: could not post the review to GitHub; "
|
|
626
|
+
+ "recording it in pacework only —",
|
|
627
|
+
error instanceof Error ? error.message : error,
|
|
628
|
+
)
|
|
629
|
+
await callMcpTool(
|
|
630
|
+
mcpKey,
|
|
631
|
+
"submit_issue_review",
|
|
632
|
+
{
|
|
633
|
+
issue_id: run.task_id,
|
|
634
|
+
workspace_id: run.workspace_id,
|
|
635
|
+
decision: parsed.decision,
|
|
636
|
+
summary: parsed.summary,
|
|
637
|
+
},
|
|
638
|
+
run.agent_id,
|
|
639
|
+
)
|
|
640
|
+
} else {
|
|
641
|
+
throw error
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
return parsed
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function sanitizeBranchPart(value) {
|
|
649
|
+
return String(value || "issue")
|
|
650
|
+
.toLowerCase()
|
|
651
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
652
|
+
.replace(/^-+|-+$/g, "")
|
|
653
|
+
.slice(0, 32) || "issue"
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function shortBranchToken(value) {
|
|
657
|
+
return sanitizeBranchPart(value).replace(/-/g, "").slice(0, 8) || "run"
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function unwrapOne(value) {
|
|
661
|
+
return Array.isArray(value) ? (value[0] ?? null) : (value ?? null)
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
function resolveLocalCheckoutMode(run) {
|
|
665
|
+
switch (run?.agent?.local_checkout_mode) {
|
|
666
|
+
case "per_task":
|
|
667
|
+
case "repo_root":
|
|
668
|
+
return run.agent.local_checkout_mode
|
|
669
|
+
case "shared_family":
|
|
670
|
+
default:
|
|
671
|
+
return "shared_family"
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function buildIssueToken(issue, fallbackValue) {
|
|
676
|
+
const identifierPart = sanitizeBranchPart(issue?.identifier)
|
|
677
|
+
const fallbackTaskToken = shortBranchToken(issue?.id || fallbackValue)
|
|
678
|
+
return identifierPart && identifierPart !== "issue"
|
|
679
|
+
? identifierPart
|
|
680
|
+
: `issue-${fallbackTaskToken}`
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function resolveBranchSource(run) {
|
|
684
|
+
const issue = run?.task || null
|
|
685
|
+
const parent = unwrapOne(issue?.parent)
|
|
686
|
+
return { issue, parent }
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
function getBranchPrefix() {
|
|
690
|
+
switch (activeExecutor()) {
|
|
691
|
+
case "claude_code": return "claude"
|
|
692
|
+
case "gemini": return "gemini"
|
|
693
|
+
case "cursor_agent": return "cursor"
|
|
694
|
+
case "github_copilot": return "copilot"
|
|
695
|
+
case "codex":
|
|
696
|
+
default: return "codex"
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function buildRunBranchName(run) {
|
|
701
|
+
// start_work already created this branch on GitHub and recorded it on the run,
|
|
702
|
+
// so merge_pr can verify the PR head against it.
|
|
703
|
+
if (run?.__serverBranch) return run.__serverBranch
|
|
704
|
+
|
|
705
|
+
const localCheckoutMode = resolveLocalCheckoutMode(run)
|
|
706
|
+
const { issue, parent } = resolveBranchSource(run)
|
|
707
|
+
const parentBranch = parent?.branch_name?.trim()
|
|
708
|
+
const prefix = getBranchPrefix()
|
|
709
|
+
|
|
710
|
+
if (localCheckoutMode === "repo_root") {
|
|
711
|
+
return null
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
const ownBranch = issue?.branch_name?.trim()
|
|
715
|
+
if (localCheckoutMode === "per_task") {
|
|
716
|
+
if (ownBranch && (!issue?.parent_id || ownBranch !== parentBranch)) return ownBranch
|
|
717
|
+
return `${prefix}/${buildIssueToken(issue, run?.id)}`
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
if (ownBranch) return ownBranch
|
|
721
|
+
|
|
722
|
+
if (parentBranch) return parentBranch
|
|
723
|
+
|
|
724
|
+
if (issue?.parent_id) {
|
|
725
|
+
return `${prefix}/${buildIssueToken(parent, issue.parent_id)}`
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
return `${prefix}/${buildIssueToken(issue, run?.id)}`
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
function buildIssueWorktreePath(run, runRepoRoot = repoRoot) {
|
|
732
|
+
const localCheckoutMode = resolveLocalCheckoutMode(run)
|
|
733
|
+
if (localCheckoutMode === "repo_root") {
|
|
734
|
+
return runRepoRoot
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
const { issue, parent } = resolveBranchSource(run)
|
|
738
|
+
const issueToken = localCheckoutMode === "per_task"
|
|
739
|
+
? buildIssueToken(issue, run?.id)
|
|
740
|
+
: issue?.parent_id
|
|
741
|
+
? buildIssueToken(parent, issue.parent_id)
|
|
742
|
+
: buildIssueToken(issue, run?.id)
|
|
743
|
+
return path.join(runRepoRoot, ".agent", "issues", issueToken, "worktree")
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
async function currentBranch(worktreePath) {
|
|
747
|
+
const result = await execCommandSafe("git", ["-C", worktreePath, "rev-parse", "--abbrev-ref", "HEAD"], worktreePath)
|
|
748
|
+
return result?.stdout?.trim() || null
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
function isAutomationSmokeTestIssue(run) {
|
|
752
|
+
const title = String(run?.task?.title || "")
|
|
753
|
+
const description = String(run?.task?.description || "")
|
|
754
|
+
return title.includes(automationSmokeTestMarker) || description.includes(automationSmokeTestMarker)
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
async function requestJson(url, init = {}) {
|
|
758
|
+
const response = await fetch(url, init)
|
|
759
|
+
let payload = null
|
|
760
|
+
try {
|
|
761
|
+
payload = await response.json()
|
|
762
|
+
} catch {
|
|
763
|
+
payload = null
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
if (!response.ok) {
|
|
767
|
+
const errorDetail = Array.isArray(payload?.message)
|
|
768
|
+
? payload.message.join("; ")
|
|
769
|
+
: typeof payload?.message === "string"
|
|
770
|
+
? payload.message
|
|
771
|
+
: ""
|
|
772
|
+
const errorLabel = typeof payload?.error === "string" ? payload.error : ""
|
|
773
|
+
const errorMessage = errorDetail || errorLabel || `Request failed: ${response.status}`
|
|
774
|
+
throw new Error(errorMessage)
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
return payload
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
async function postIssueMilestone(rawApiKey, taskId, message, kind = "info") {
|
|
781
|
+
if (!rawApiKey || !taskId || !message) return null
|
|
782
|
+
|
|
783
|
+
return await requestJson(`${appUrl}/api/mcp/issues/${taskId}/milestone`, {
|
|
784
|
+
method: "POST",
|
|
785
|
+
headers: {
|
|
786
|
+
"Content-Type": "application/json",
|
|
787
|
+
Authorization: `Bearer ${rawApiKey}`,
|
|
788
|
+
},
|
|
789
|
+
body: JSON.stringify({
|
|
790
|
+
message,
|
|
791
|
+
kind,
|
|
792
|
+
}),
|
|
793
|
+
}).catch(() => null)
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
async function callMcpTool(rawApiKey, name, argumentsPayload, agentId) {
|
|
797
|
+
const payload = await requestJson(`${appUrl}/api/mcp`, {
|
|
798
|
+
method: "POST",
|
|
799
|
+
headers: {
|
|
800
|
+
"Content-Type": "application/json",
|
|
801
|
+
Authorization: `Bearer ${rawApiKey}`,
|
|
802
|
+
},
|
|
803
|
+
body: JSON.stringify({
|
|
804
|
+
jsonrpc: "2.0",
|
|
805
|
+
id: 1,
|
|
806
|
+
method: "tools/call",
|
|
807
|
+
params: {
|
|
808
|
+
name,
|
|
809
|
+
arguments: argumentsPayload,
|
|
810
|
+
_meta: agentId ? { agent_id: agentId } : undefined,
|
|
811
|
+
},
|
|
812
|
+
}),
|
|
813
|
+
})
|
|
814
|
+
|
|
815
|
+
const text = payload?.result?.content?.[0]?.text
|
|
816
|
+
if (!text) return payload?.result ?? null
|
|
817
|
+
try {
|
|
818
|
+
return JSON.parse(text)
|
|
819
|
+
} catch {
|
|
820
|
+
return text
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
function loadState() {
|
|
825
|
+
try {
|
|
826
|
+
return JSON.parse(fs.readFileSync(stateFile, "utf8"))
|
|
827
|
+
} catch {
|
|
828
|
+
return null
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
function saveState(state) {
|
|
833
|
+
ensureDir(path.dirname(stateFile))
|
|
834
|
+
fs.writeFileSync(stateFile, JSON.stringify(state, null, 2))
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
function clearState() {
|
|
838
|
+
try {
|
|
839
|
+
fs.rmSync(stateFile, { force: true })
|
|
840
|
+
} catch {}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
function isInvalidRunnerCredentialsError(error) {
|
|
844
|
+
return error instanceof Error && error.message.includes("Invalid runner credentials")
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
async function registerRunner() {
|
|
848
|
+
if (!workspaceApiKey) {
|
|
849
|
+
throw new Error("Missing runner launch token or workspace API key. Set --launch-token, RUNNER_LAUNCH_TOKEN, --workspace-api-key, or WORKSPACE_API_KEY to register a runner.")
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
const payload = await requestJson(`${appUrl}/api/agent-runners/register`, {
|
|
853
|
+
method: "POST",
|
|
854
|
+
headers: {
|
|
855
|
+
"Content-Type": "application/json",
|
|
856
|
+
Authorization: `Bearer ${workspaceApiKey}`,
|
|
857
|
+
},
|
|
858
|
+
body: JSON.stringify({
|
|
859
|
+
workspace_id: workspaceId,
|
|
860
|
+
name: runnerName,
|
|
861
|
+
machine_name: machineName,
|
|
862
|
+
repo_root: repoRoot,
|
|
863
|
+
local_executor: defaultExecutor,
|
|
864
|
+
labels,
|
|
865
|
+
max_parallel_runs: maxParallelRuns,
|
|
866
|
+
}),
|
|
867
|
+
})
|
|
868
|
+
|
|
869
|
+
const state = {
|
|
870
|
+
workspace_id: workspaceId,
|
|
871
|
+
runner_id: payload.runner.id,
|
|
872
|
+
runner_secret: payload.runner_secret,
|
|
873
|
+
runner_mcp_key: payload.runner_mcp_key ?? null,
|
|
874
|
+
name: payload.runner.name,
|
|
875
|
+
machine_name: payload.runner.machine_name,
|
|
876
|
+
repo_root: payload.runner.repo_root,
|
|
877
|
+
local_executor: payload.runner.local_executor,
|
|
878
|
+
max_parallel_runs: payload.runner.max_parallel_runs ?? maxParallelRuns,
|
|
879
|
+
labels: payload.runner.labels ?? [],
|
|
880
|
+
registered_at: new Date().toISOString(),
|
|
881
|
+
}
|
|
882
|
+
saveState(state)
|
|
883
|
+
return state
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
async function ensureRegisteredRunner() {
|
|
887
|
+
const state = loadState()
|
|
888
|
+
if (state?.runner_id && state?.runner_secret && state?.runner_mcp_key) return state
|
|
889
|
+
return registerRunner()
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
async function heartbeat(state, status = "online") {
|
|
893
|
+
return requestJson(`${appUrl}/api/agent-runners/heartbeat`, {
|
|
894
|
+
method: "POST",
|
|
895
|
+
headers: {
|
|
896
|
+
"Content-Type": "application/json",
|
|
897
|
+
Authorization: `Bearer ${state.runner_secret}`,
|
|
898
|
+
},
|
|
899
|
+
body: JSON.stringify({
|
|
900
|
+
workspace_id: workspaceId,
|
|
901
|
+
runner_id: state.runner_id,
|
|
902
|
+
status,
|
|
903
|
+
local_executors: installedExecutors,
|
|
904
|
+
}),
|
|
905
|
+
})
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
async function deregisterRunner(state) {
|
|
909
|
+
return requestJson(`${appUrl}/api/agent-runners/deregister`, {
|
|
910
|
+
method: "POST",
|
|
911
|
+
headers: {
|
|
912
|
+
"Content-Type": "application/json",
|
|
913
|
+
Authorization: `Bearer ${state.runner_secret}`,
|
|
914
|
+
},
|
|
915
|
+
body: JSON.stringify({
|
|
916
|
+
workspace_id: state.workspace_id,
|
|
917
|
+
runner_id: state.runner_id,
|
|
918
|
+
}),
|
|
919
|
+
})
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
async function claimNext(state, currentActiveRuns = 0) {
|
|
923
|
+
return requestJson(`${appUrl}/api/agent-runners/claim-next`, {
|
|
924
|
+
method: "POST",
|
|
925
|
+
headers: {
|
|
926
|
+
"Content-Type": "application/json",
|
|
927
|
+
Authorization: `Bearer ${state.runner_secret}`,
|
|
928
|
+
},
|
|
929
|
+
body: JSON.stringify({
|
|
930
|
+
workspace_id: workspaceId,
|
|
931
|
+
runner_id: state.runner_id,
|
|
932
|
+
current_active_runs: currentActiveRuns,
|
|
933
|
+
}),
|
|
934
|
+
})
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* The MCP key a run acts with. Prefer the key minted for this run: the server binds
|
|
939
|
+
* it to the run's agent, so the executor's calls are attributed to that agent and
|
|
940
|
+
* limited by its role. The runner's shared key is a fallback for older servers.
|
|
941
|
+
*/
|
|
942
|
+
function runMcpKey(run, state) {
|
|
943
|
+
return run.mcp_api_key || run.planner_api_key || state.runner_mcp_key
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
async function postRunLog(state, runId, logTail) {
|
|
947
|
+
return requestJson(`${appUrl}/api/agent-runners/runs/${runId}/log`, {
|
|
948
|
+
method: "POST",
|
|
949
|
+
headers: {
|
|
950
|
+
"Content-Type": "application/json",
|
|
951
|
+
Authorization: `Bearer ${state.runner_secret}`,
|
|
952
|
+
},
|
|
953
|
+
body: JSON.stringify({
|
|
954
|
+
workspace_id: workspaceId,
|
|
955
|
+
runner_id: state.runner_id,
|
|
956
|
+
run_id: runId,
|
|
957
|
+
log_tail: truncate(logTail, 12000),
|
|
958
|
+
heartbeat_at: new Date().toISOString(),
|
|
959
|
+
}),
|
|
960
|
+
})
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
function omitNullishEntries(record) {
|
|
964
|
+
return Object.fromEntries(
|
|
965
|
+
Object.entries(record).filter(([, value]) => value !== null && value !== undefined),
|
|
966
|
+
)
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
async function completeRun(state, runId, payload) {
|
|
970
|
+
return requestJson(`${appUrl}/api/agent-runners/runs/${runId}/complete`, {
|
|
971
|
+
method: "POST",
|
|
972
|
+
headers: {
|
|
973
|
+
"Content-Type": "application/json",
|
|
974
|
+
Authorization: `Bearer ${state.runner_secret}`,
|
|
975
|
+
},
|
|
976
|
+
body: JSON.stringify(omitNullishEntries({
|
|
977
|
+
workspace_id: workspaceId,
|
|
978
|
+
runner_id: state.runner_id,
|
|
979
|
+
run_id: runId,
|
|
980
|
+
...payload,
|
|
981
|
+
})),
|
|
982
|
+
})
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
async function preparePlannerRun(run, workspaceContext = null) {
|
|
986
|
+
return requestJson(`${appUrl}/api/agent-planner/runs/${run.id}/prepare`, {
|
|
987
|
+
method: "POST",
|
|
988
|
+
headers: {
|
|
989
|
+
"Content-Type": "application/json",
|
|
990
|
+
Authorization: `Bearer ${run.planner_api_key}`,
|
|
991
|
+
},
|
|
992
|
+
body: JSON.stringify({
|
|
993
|
+
workspace_id: workspaceId,
|
|
994
|
+
task_id: run.task_id,
|
|
995
|
+
agent_id: run.agent_id || null,
|
|
996
|
+
workspace_context: workspaceContext,
|
|
997
|
+
}),
|
|
998
|
+
})
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
async function postPlannerLog(run, logTail) {
|
|
1002
|
+
return requestJson(`${appUrl}/api/agent-planner/runs/${run.id}/log`, {
|
|
1003
|
+
method: "POST",
|
|
1004
|
+
headers: {
|
|
1005
|
+
"Content-Type": "application/json",
|
|
1006
|
+
Authorization: `Bearer ${run.planner_api_key}`,
|
|
1007
|
+
},
|
|
1008
|
+
body: JSON.stringify({
|
|
1009
|
+
workspace_id: workspaceId,
|
|
1010
|
+
log_tail: truncate(logTail, 12000),
|
|
1011
|
+
}),
|
|
1012
|
+
})
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
async function completePlannerRun(run, payload, workspaceContext = null) {
|
|
1016
|
+
return requestJson(`${appUrl}/api/agent-planner/runs/${run.id}/complete`, {
|
|
1017
|
+
method: "POST",
|
|
1018
|
+
headers: {
|
|
1019
|
+
"Content-Type": "application/json",
|
|
1020
|
+
Authorization: `Bearer ${run.planner_api_key}`,
|
|
1021
|
+
},
|
|
1022
|
+
body: JSON.stringify(omitNullishEntries({
|
|
1023
|
+
workspace_id: workspaceId,
|
|
1024
|
+
task_id: run.task_id,
|
|
1025
|
+
agent_id: run.agent_id || null,
|
|
1026
|
+
workspace_context: workspaceContext,
|
|
1027
|
+
...payload,
|
|
1028
|
+
})),
|
|
1029
|
+
})
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
async function execCommand(command, argsList, cwd) {
|
|
1033
|
+
return new Promise((resolve, reject) => {
|
|
1034
|
+
const child = spawn(command, argsList, { cwd, stdio: ["ignore", "pipe", "pipe"] })
|
|
1035
|
+
let stdout = ""
|
|
1036
|
+
let stderr = ""
|
|
1037
|
+
child.stdout.on("data", chunk => { stdout += chunk.toString() })
|
|
1038
|
+
child.stderr.on("data", chunk => { stderr += chunk.toString() })
|
|
1039
|
+
child.on("error", reject)
|
|
1040
|
+
child.on("close", code => {
|
|
1041
|
+
if (code === 0) {
|
|
1042
|
+
resolve({ stdout, stderr })
|
|
1043
|
+
return
|
|
1044
|
+
}
|
|
1045
|
+
reject(new Error(`${command} ${argsList.join(" ")} failed with code ${code}\n${stderr || stdout}`))
|
|
1046
|
+
})
|
|
1047
|
+
})
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
async function execShellCommand(command, cwd) {
|
|
1051
|
+
return new Promise((resolve, reject) => {
|
|
1052
|
+
const shell = process.env.SHELL || "zsh"
|
|
1053
|
+
const child = spawn(shell, ["-lc", command], { cwd, stdio: ["ignore", "pipe", "pipe"] })
|
|
1054
|
+
let stdout = ""
|
|
1055
|
+
let stderr = ""
|
|
1056
|
+
child.stdout.on("data", chunk => { stdout += chunk.toString() })
|
|
1057
|
+
child.stderr.on("data", chunk => { stderr += chunk.toString() })
|
|
1058
|
+
child.on("error", reject)
|
|
1059
|
+
child.on("close", code => {
|
|
1060
|
+
if (code === 0) {
|
|
1061
|
+
resolve({ stdout, stderr })
|
|
1062
|
+
return
|
|
1063
|
+
}
|
|
1064
|
+
reject(new Error(`${command} failed with code ${code}\n${stderr || stdout}`))
|
|
1065
|
+
})
|
|
1066
|
+
})
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
function defaultRunnerProfile() {
|
|
1070
|
+
return {
|
|
1071
|
+
verification: {
|
|
1072
|
+
always: ["git diff --check"],
|
|
1073
|
+
rules: [],
|
|
1074
|
+
},
|
|
1075
|
+
git: {
|
|
1076
|
+
autoCommitOnSuccess: autoCommitOnSuccessFlag,
|
|
1077
|
+
commitMessageTemplate: "{identifier} {title}",
|
|
1078
|
+
},
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
function loadRunnerProfile() {
|
|
1083
|
+
const candidates = [
|
|
1084
|
+
path.join(repoRoot, "pacework.runner.json"),
|
|
1085
|
+
path.join(repoRoot, ".pacework", "runner-profile.json"),
|
|
1086
|
+
// Repositories configured before the Kentral → pacework rename.
|
|
1087
|
+
path.join(repoRoot, "kentral.runner.json"),
|
|
1088
|
+
path.join(repoRoot, ".kentral", "runner-profile.json"),
|
|
1089
|
+
]
|
|
1090
|
+
|
|
1091
|
+
const defaults = defaultRunnerProfile()
|
|
1092
|
+
for (const candidate of candidates) {
|
|
1093
|
+
if (!fs.existsSync(candidate)) continue
|
|
1094
|
+
try {
|
|
1095
|
+
const raw = JSON.parse(fs.readFileSync(candidate, "utf8"))
|
|
1096
|
+
return {
|
|
1097
|
+
...defaults,
|
|
1098
|
+
...raw,
|
|
1099
|
+
verification: {
|
|
1100
|
+
...defaults.verification,
|
|
1101
|
+
...(raw.verification || {}),
|
|
1102
|
+
},
|
|
1103
|
+
git: {
|
|
1104
|
+
...defaults.git,
|
|
1105
|
+
...(raw.git || {}),
|
|
1106
|
+
},
|
|
1107
|
+
_path: candidate,
|
|
1108
|
+
}
|
|
1109
|
+
} catch (error) {
|
|
1110
|
+
runnerBaseLogger.warn("runner.profile.invalid", "Failed to parse runner profile", {
|
|
1111
|
+
meta: {
|
|
1112
|
+
path: candidate,
|
|
1113
|
+
},
|
|
1114
|
+
}, error)
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
return { ...defaults, _path: null }
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
function matchesVerificationRule(file, matchers) {
|
|
1121
|
+
return (matchers || []).some(matcher => (
|
|
1122
|
+
file === matcher
|
|
1123
|
+
|| file.startsWith(matcher)
|
|
1124
|
+
|| file.endsWith(matcher)
|
|
1125
|
+
|| file.includes(matcher)
|
|
1126
|
+
))
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
function buildVerificationPlan(changedFiles, profile, gitAvailable = true) {
|
|
1130
|
+
const commands = []
|
|
1131
|
+
for (const command of profile.verification?.always || []) {
|
|
1132
|
+
if (!gitAvailable && /^\s*git\b/i.test(command)) continue
|
|
1133
|
+
commands.push({ label: "always", command })
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
for (const rule of profile.verification?.rules || []) {
|
|
1137
|
+
const matchedFiles = changedFiles.filter(file => matchesVerificationRule(file, rule.match || []))
|
|
1138
|
+
if (!matchedFiles.length) continue
|
|
1139
|
+
|
|
1140
|
+
for (const command of rule.commands || []) {
|
|
1141
|
+
if (!gitAvailable && /^\s*git\b/i.test(command)) continue
|
|
1142
|
+
commands.push({
|
|
1143
|
+
label: rule.name || "rule",
|
|
1144
|
+
command: command
|
|
1145
|
+
.replace(/\{files\}/g, matchedFiles.map(shellQuote).join(" "))
|
|
1146
|
+
.replace(/\{all_files\}/g, changedFiles.map(shellQuote).join(" ")),
|
|
1147
|
+
})
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
return commands
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
async function runVerificationPlan(worktreePath, changedFiles, profile) {
|
|
1155
|
+
const gitAvailable = await isGitRepository(worktreePath)
|
|
1156
|
+
const plan = buildVerificationPlan(changedFiles, profile, gitAvailable)
|
|
1157
|
+
if (!plan.length) {
|
|
1158
|
+
return {
|
|
1159
|
+
summary: gitAvailable
|
|
1160
|
+
? "No verification commands were configured for this run."
|
|
1161
|
+
: "Skipped git-based verification because the workspace root is not a git repository.",
|
|
1162
|
+
steps: [],
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
const steps = []
|
|
1167
|
+
for (const step of plan) {
|
|
1168
|
+
try {
|
|
1169
|
+
const result = await execShellCommand(step.command, worktreePath)
|
|
1170
|
+
steps.push({
|
|
1171
|
+
label: step.label,
|
|
1172
|
+
command: step.command,
|
|
1173
|
+
status: "passed",
|
|
1174
|
+
output: truncate(`${result.stdout}\n${result.stderr}`.trim(), 4000),
|
|
1175
|
+
})
|
|
1176
|
+
} catch (error) {
|
|
1177
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
1178
|
+
steps.push({
|
|
1179
|
+
label: step.label,
|
|
1180
|
+
command: step.command,
|
|
1181
|
+
status: "failed",
|
|
1182
|
+
output: truncate(message, 4000),
|
|
1183
|
+
})
|
|
1184
|
+
const summary = steps.map(entry => (
|
|
1185
|
+
`- ${entry.status === "passed" ? "PASS" : "FAIL"} [${entry.label}] ${entry.command}`
|
|
1186
|
+
)).join("\n")
|
|
1187
|
+
throw new Error(`Verification failed.\n${summary}\n\n${message}`)
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
const summary = steps.map(entry => (
|
|
1192
|
+
`- PASS [${entry.label}] ${entry.command}`
|
|
1193
|
+
)).join("\n")
|
|
1194
|
+
|
|
1195
|
+
return { summary, steps }
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
function formatCommitMessage(run, profile) {
|
|
1199
|
+
const template = profile.git?.commitMessageTemplate || "{identifier} {title}"
|
|
1200
|
+
const identifier = run.task?.identifier || run.task_id
|
|
1201
|
+
const title = String(run.task?.title || "Automated update").replace(/\s+/g, " ").trim()
|
|
1202
|
+
const subject = template
|
|
1203
|
+
.replace(/\{identifier\}/g, identifier)
|
|
1204
|
+
.replace(/\{title\}/g, title)
|
|
1205
|
+
.slice(0, 72)
|
|
1206
|
+
.trim()
|
|
1207
|
+
|
|
1208
|
+
return {
|
|
1209
|
+
subject: subject || `${identifier} automated update`,
|
|
1210
|
+
body: [
|
|
1211
|
+
`Automated local ${executorDisplayName()} run.`,
|
|
1212
|
+
`Run: ${run.id}`,
|
|
1213
|
+
`Agent: ${run.agent?.name || "Agent"}`,
|
|
1214
|
+
// Everything reaches GitHub as the App, so the person who set this work
|
|
1215
|
+
// going is otherwise invisible. A co-author trailer attributes it to
|
|
1216
|
+
// them in GitHub's UI without needing a second identity.
|
|
1217
|
+
...(run.task?.requested_by_email
|
|
1218
|
+
? [``, `Co-authored-by: ${run.task.requested_by_name || run.task.requested_by_email} <${run.task.requested_by_email}>`]
|
|
1219
|
+
: []),
|
|
1220
|
+
].join("\n"),
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
async function createStandardizedCommit(worktreePath, run, profile) {
|
|
1225
|
+
const gitStatus = await execCommand("git", ["-C", worktreePath, "status", "--short"], worktreePath)
|
|
1226
|
+
if (!gitStatus.stdout.trim()) {
|
|
1227
|
+
return null
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
const commitMessage = formatCommitMessage(run, profile)
|
|
1231
|
+
await execCommand("git", ["-C", worktreePath, "add", "-A"], worktreePath)
|
|
1232
|
+
await execCommand("git", ["-C", worktreePath, "commit", "-m", commitMessage.subject, "-m", commitMessage.body], worktreePath)
|
|
1233
|
+
const sha = (await execCommand("git", ["-C", worktreePath, "rev-parse", "HEAD"], worktreePath)).stdout.trim()
|
|
1234
|
+
|
|
1235
|
+
return {
|
|
1236
|
+
sha,
|
|
1237
|
+
subject: commitMessage.subject,
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
async function fetchGithubToken(state, workspaceId, projectId, teamId = null, taskId = null) {
|
|
1242
|
+
const payload = await requestJson(`${appUrl}/api/agent-runners/github-token`, {
|
|
1243
|
+
method: "POST",
|
|
1244
|
+
headers: {
|
|
1245
|
+
"Content-Type": "application/json",
|
|
1246
|
+
Authorization: `Bearer ${state.runner_secret}`,
|
|
1247
|
+
},
|
|
1248
|
+
body: JSON.stringify({
|
|
1249
|
+
workspace_id: workspaceId,
|
|
1250
|
+
project_id: projectId ?? null,
|
|
1251
|
+
team_id: teamId ?? null,
|
|
1252
|
+
// An issue can name its own repository (work split across repos).
|
|
1253
|
+
task_id: taskId ?? null,
|
|
1254
|
+
}),
|
|
1255
|
+
})
|
|
1256
|
+
return { token: payload.token, repo: payload.repo ?? null, baseBranch: payload.base_branch ?? null }
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
/**
|
|
1260
|
+
* Git options that drop an Authorization header stored in the repo's config.
|
|
1261
|
+
* GitHub Actions' checkout stores the job's read-only token that way, and git
|
|
1262
|
+
* sends it ahead of the token in the URL — every push came back 403.
|
|
1263
|
+
*/
|
|
1264
|
+
const GIT_IGNORE_STORED_AUTH = ["-c", "http.https://github.com/.extraheader="]
|
|
1265
|
+
|
|
1266
|
+
async function pushBranchToGitHub(worktreePath, branch, githubToken, repo) {
|
|
1267
|
+
// Set remote URL with embedded token for auth, then push
|
|
1268
|
+
const remoteUrl = `https://x-access-token:${githubToken}@github.com/${repo.owner}/${repo.repo}.git`
|
|
1269
|
+
await execCommand(
|
|
1270
|
+
"git",
|
|
1271
|
+
["-C", worktreePath, "remote", "set-url", "origin", remoteUrl],
|
|
1272
|
+
worktreePath,
|
|
1273
|
+
)
|
|
1274
|
+
// start_work creates the branch on GitHub before the runner has a local clone
|
|
1275
|
+
// of it, so --force-with-lease sees a remote it has no ref for and rejects the
|
|
1276
|
+
// push. Fetch first so the lease has something to compare against.
|
|
1277
|
+
await execCommandSafe(
|
|
1278
|
+
"git",
|
|
1279
|
+
["-C", worktreePath, ...GIT_IGNORE_STORED_AUTH, "fetch", "origin", branch],
|
|
1280
|
+
worktreePath,
|
|
1281
|
+
)
|
|
1282
|
+
await execCommand(
|
|
1283
|
+
"git",
|
|
1284
|
+
["-C", worktreePath, ...GIT_IGNORE_STORED_AUTH, "push", "--set-upstream", "origin", branch, "--force-with-lease"],
|
|
1285
|
+
worktreePath,
|
|
1286
|
+
)
|
|
1287
|
+
// Restore remote URL without token for safety
|
|
1288
|
+
await execCommandSafe(
|
|
1289
|
+
"git",
|
|
1290
|
+
["-C", worktreePath, "remote", "set-url", "origin", `https://github.com/${repo.owner}/${repo.repo}.git`],
|
|
1291
|
+
worktreePath,
|
|
1292
|
+
)
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
/** A section the agent left empty or answered with "none" says nothing to a reader. */
|
|
1296
|
+
function isEmptySection(text) {
|
|
1297
|
+
const flat = String(text || "").replace(/[\s.*_-]+/g, " ").trim().toLowerCase()
|
|
1298
|
+
return !flat || /^(none|n\/a|no remaining gaps|no known gaps|nothing)\b/.test(flat)
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
/**
|
|
1302
|
+
* The pull request description, written for a reviewer who has not seen the
|
|
1303
|
+
* ticket: what changed and why, which files, and how it was verified. Built from
|
|
1304
|
+
* the agent's own report so it says what this change does, not that a bot made it.
|
|
1305
|
+
*/
|
|
1306
|
+
function buildPullRequestBody(run, details = {}) {
|
|
1307
|
+
const identifier = run.task?.identifier || run.task_id
|
|
1308
|
+
const report = String(details.summary || "").trim()
|
|
1309
|
+
const summary = extractSection(report, "Summary")
|
|
1310
|
+
const verification = details.verificationSummary?.trim() || extractSection(report, "Verification")
|
|
1311
|
+
const gaps = extractSection(report, "Remaining gaps")
|
|
1312
|
+
const description = String(run.task?.description || "").trim()
|
|
1313
|
+
const changedFiles = Array.isArray(details.changedFiles) ? details.changedFiles : []
|
|
1314
|
+
const diffSummary = summarizeDiffStat(details.diffStat)
|
|
1315
|
+
const model = resolveRequestedModel(run)
|
|
1316
|
+
|
|
1317
|
+
const sections = [
|
|
1318
|
+
"## Summary",
|
|
1319
|
+
// An agent that ignored the headings still wrote a report; show it whole.
|
|
1320
|
+
summary || truncate(report, 4000) || `Implements ${identifier}: ${run.task?.title || "see the linked issue"}.`,
|
|
1321
|
+
"",
|
|
1322
|
+
"## Why",
|
|
1323
|
+
`pacework issue **${identifier}** — ${run.task?.title || "Untitled"}`,
|
|
1324
|
+
...(description ? ["", truncate(description, 1500).split(/\r?\n/).map((line) => `> ${line}`).join("\n")] : []),
|
|
1325
|
+
"",
|
|
1326
|
+
"## Changes",
|
|
1327
|
+
...(changedFiles.length
|
|
1328
|
+
? changedFiles.slice(0, 30).map((file) => `- \`${file}\``)
|
|
1329
|
+
: ["- No file list was reported."]),
|
|
1330
|
+
...(changedFiles.length > 30 ? [`- …and ${changedFiles.length - 30} more`] : []),
|
|
1331
|
+
...(diffSummary ? ["", diffSummary] : []),
|
|
1332
|
+
"",
|
|
1333
|
+
"## Verification",
|
|
1334
|
+
verification || "The agent did not report how it verified this change.",
|
|
1335
|
+
...(isEmptySection(gaps) ? [] : ["", "## Known gaps", gaps]),
|
|
1336
|
+
"",
|
|
1337
|
+
"---",
|
|
1338
|
+
`<sub>Opened by pacework for ${identifier}`
|
|
1339
|
+
+ `${run.task?.requested_by_name || run.task?.requested_by_email ? ` · requested by ${run.task.requested_by_name || run.task.requested_by_email}` : ""}`
|
|
1340
|
+
+ ` · agent **${run.agent?.name || "Agent"}** · `
|
|
1341
|
+
+ `${executorDisplayName()}${model ? ` (${model})` : ""} on ${runnerName} · run \`${run.id}\`</sub>`,
|
|
1342
|
+
]
|
|
1343
|
+
return sections.join("\n")
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
async function createGithubPr(githubToken, repo, branch, defaultBranch, run, details) {
|
|
1347
|
+
const identifier = run.task?.identifier || run.task_id
|
|
1348
|
+
const title = `${identifier}: ${String(run.task?.title || "Automated update").replace(/\s+/g, " ").trim()}`.slice(0, 120)
|
|
1349
|
+
const body = buildPullRequestBody(run, details)
|
|
1350
|
+
|
|
1351
|
+
const res = await fetch(`https://api.github.com/repos/${repo.owner}/${repo.repo}/pulls`, {
|
|
1352
|
+
method: "POST",
|
|
1353
|
+
headers: {
|
|
1354
|
+
Authorization: `Bearer ${githubToken}`,
|
|
1355
|
+
Accept: "application/vnd.github+json",
|
|
1356
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
1357
|
+
"Content-Type": "application/json",
|
|
1358
|
+
},
|
|
1359
|
+
body: JSON.stringify({
|
|
1360
|
+
title,
|
|
1361
|
+
body,
|
|
1362
|
+
head: branch,
|
|
1363
|
+
base: defaultBranch,
|
|
1364
|
+
draft: false,
|
|
1365
|
+
}),
|
|
1366
|
+
})
|
|
1367
|
+
|
|
1368
|
+
if (!res.ok) {
|
|
1369
|
+
// 422 = PR already exists — fetch the existing one
|
|
1370
|
+
if (res.status === 422) {
|
|
1371
|
+
const existingRes = await fetch(
|
|
1372
|
+
`https://api.github.com/repos/${repo.owner}/${repo.repo}/pulls?head=${repo.owner}:${branch}&state=open`,
|
|
1373
|
+
{
|
|
1374
|
+
headers: {
|
|
1375
|
+
Authorization: `Bearer ${githubToken}`,
|
|
1376
|
+
Accept: "application/vnd.github+json",
|
|
1377
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
1378
|
+
},
|
|
1379
|
+
},
|
|
1380
|
+
)
|
|
1381
|
+
if (existingRes.ok) {
|
|
1382
|
+
const existing = await existingRes.json()
|
|
1383
|
+
if (existing?.[0]?.html_url) {
|
|
1384
|
+
// A rework round pushes to the same PR; describe what it is now.
|
|
1385
|
+
await fetch(`https://api.github.com/repos/${repo.owner}/${repo.repo}/pulls/${existing[0].number}`, {
|
|
1386
|
+
method: "PATCH",
|
|
1387
|
+
headers: {
|
|
1388
|
+
Authorization: `Bearer ${githubToken}`,
|
|
1389
|
+
Accept: "application/vnd.github+json",
|
|
1390
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
1391
|
+
"Content-Type": "application/json",
|
|
1392
|
+
},
|
|
1393
|
+
body: JSON.stringify({ body }),
|
|
1394
|
+
}).catch(() => {})
|
|
1395
|
+
return { html_url: existing[0].html_url, number: existing[0].number }
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
const text = await res.text().catch(() => "")
|
|
1400
|
+
throw new Error(`GitHub PR creation failed (${res.status}): ${text}`)
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
const pr = await res.json()
|
|
1404
|
+
return { html_url: pr.html_url, number: pr.number }
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
async function detectDefaultBranch(repoRoot) {
|
|
1408
|
+
// Use the current checked-out branch of the main repo (not any worktree)
|
|
1409
|
+
// This ensures agent branches merge back into whatever branch the developer is on.
|
|
1410
|
+
try {
|
|
1411
|
+
const result = await execCommand("git", ["-C", repoRoot, "rev-parse", "--abbrev-ref", "HEAD"], repoRoot)
|
|
1412
|
+
const current = result.stdout.trim()
|
|
1413
|
+
if (current && current !== "HEAD") return current
|
|
1414
|
+
} catch {
|
|
1415
|
+
// fall through
|
|
1416
|
+
}
|
|
1417
|
+
// Fall back to common default branch names
|
|
1418
|
+
for (const name of ["main", "master"]) {
|
|
1419
|
+
try {
|
|
1420
|
+
await execCommand("git", ["-C", repoRoot, "rev-parse", "--verify", name], repoRoot)
|
|
1421
|
+
return name
|
|
1422
|
+
} catch {
|
|
1423
|
+
// Branch doesn't exist locally, try next
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
return "main"
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
async function mergeFeatureBranchToMain(repoRoot, worktreePath, branch, taskTitle, runLogger) {
|
|
1430
|
+
const defaultBranch = await detectDefaultBranch(repoRoot)
|
|
1431
|
+
|
|
1432
|
+
if (branch === defaultBranch) {
|
|
1433
|
+
runLogger.info("runner.merge.skipped", "Feature branch is already the default branch, skipping merge", {
|
|
1434
|
+
meta: { branch, default_branch: defaultBranch },
|
|
1435
|
+
})
|
|
1436
|
+
return { merged: false, skipped: true, reason: "same_branch" }
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
runLogger.info("runner.merge.start", `Merging ${branch} → ${defaultBranch}`, {
|
|
1440
|
+
meta: { branch, default_branch: defaultBranch, repo_root: repoRoot },
|
|
1441
|
+
})
|
|
1442
|
+
|
|
1443
|
+
try {
|
|
1444
|
+
await execCommand("git", [
|
|
1445
|
+
"-C", repoRoot,
|
|
1446
|
+
"merge", "--no-ff", branch,
|
|
1447
|
+
"-m", `Merge ${branch}: ${taskTitle || branch}`,
|
|
1448
|
+
], repoRoot)
|
|
1449
|
+
|
|
1450
|
+
runLogger.info("runner.merge.success", `Merged ${branch} into ${defaultBranch}`, {
|
|
1451
|
+
meta: { branch, default_branch: defaultBranch },
|
|
1452
|
+
})
|
|
1453
|
+
|
|
1454
|
+
// Clean up worktree and feature branch (best-effort)
|
|
1455
|
+
await execCommandSafe("git", ["-C", repoRoot, "worktree", "remove", worktreePath, "--force"], repoRoot)
|
|
1456
|
+
await execCommandSafe("git", ["-C", repoRoot, "branch", "-d", branch], repoRoot)
|
|
1457
|
+
|
|
1458
|
+
return { merged: true, defaultBranch }
|
|
1459
|
+
} catch (error) {
|
|
1460
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
1461
|
+
runLogger.error("runner.merge.failed", `Failed to merge ${branch} into ${defaultBranch}`, {
|
|
1462
|
+
meta: { branch, default_branch: defaultBranch },
|
|
1463
|
+
}, error)
|
|
1464
|
+
// Abort any in-progress merge to leave the repo clean
|
|
1465
|
+
await execCommandSafe("git", ["-C", repoRoot, "merge", "--abort"], repoRoot)
|
|
1466
|
+
return { merged: false, error: message }
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
function resolveMergeApprovalTarget(run, branch) {
|
|
1471
|
+
const parent = run?.task?.parent
|
|
1472
|
+
if (parent?.id && (!parent.branch_name || parent.branch_name === branch)) {
|
|
1473
|
+
return {
|
|
1474
|
+
taskId: parent.id,
|
|
1475
|
+
label: parent.identifier || parent.id,
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
return {
|
|
1480
|
+
taskId: run?.task_id,
|
|
1481
|
+
label: run?.task?.identifier || run?.task_id,
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
async function execCommandSafe(command, argsList, cwd) {
|
|
1486
|
+
try {
|
|
1487
|
+
return await execCommand(command, argsList, cwd)
|
|
1488
|
+
} catch {
|
|
1489
|
+
return null
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
async function isGitRepository(worktreePath) {
|
|
1494
|
+
const result = await execCommandSafe("git", ["-C", worktreePath, "rev-parse", "--is-inside-work-tree"], worktreePath)
|
|
1495
|
+
return result?.stdout?.trim() === "true"
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
function killChildProcessTree(childProcess, signal = "SIGTERM") {
|
|
1499
|
+
if (!childProcess || childProcess.killed !== false) return
|
|
1500
|
+
|
|
1501
|
+
const pid = typeof childProcess.pid === "number" ? childProcess.pid : null
|
|
1502
|
+
if (!pid) return
|
|
1503
|
+
|
|
1504
|
+
try {
|
|
1505
|
+
if (process.platform !== "win32") {
|
|
1506
|
+
process.kill(-pid, signal)
|
|
1507
|
+
return
|
|
1508
|
+
}
|
|
1509
|
+
} catch {
|
|
1510
|
+
// Fall back to direct child termination below.
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
try {
|
|
1514
|
+
childProcess.kill(signal)
|
|
1515
|
+
} catch {
|
|
1516
|
+
// Ignore kill errors; the process may already be gone.
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1520
|
+
function requestChildProcessShutdown(childProcess, graceMs = 5000) {
|
|
1521
|
+
if (!childProcess || childProcess.killed) return
|
|
1522
|
+
killChildProcessTree(childProcess, "SIGTERM")
|
|
1523
|
+
setTimeout(() => {
|
|
1524
|
+
if (childProcess && !childProcess.killed) {
|
|
1525
|
+
killChildProcessTree(childProcess, "SIGKILL")
|
|
1526
|
+
}
|
|
1527
|
+
}, graceMs).unref?.()
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
function listWorkspaceRootEntries(rootPath) {
|
|
1531
|
+
if (!fs.existsSync(rootPath)) {
|
|
1532
|
+
return { visibleEntries: [], notablePaths: [] }
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
const ignoredNames = new Set([".git", ".agent", "node_modules", ".DS_Store"])
|
|
1536
|
+
const entries = fs.readdirSync(rootPath, { withFileTypes: true })
|
|
1537
|
+
const visibleEntries = entries
|
|
1538
|
+
.filter((entry) => !ignoredNames.has(entry.name))
|
|
1539
|
+
.map((entry) => ({
|
|
1540
|
+
name: entry.name,
|
|
1541
|
+
isDirectory: entry.isDirectory(),
|
|
1542
|
+
}))
|
|
1543
|
+
|
|
1544
|
+
const notableEntries = visibleEntries.length > 0
|
|
1545
|
+
? visibleEntries
|
|
1546
|
+
: entries
|
|
1547
|
+
.filter((entry) => entry.name === ".agent" || entry.name === ".git")
|
|
1548
|
+
.map((entry) => ({
|
|
1549
|
+
name: entry.name,
|
|
1550
|
+
isDirectory: entry.isDirectory(),
|
|
1551
|
+
}))
|
|
1552
|
+
|
|
1553
|
+
return {
|
|
1554
|
+
visibleEntries,
|
|
1555
|
+
notablePaths: notableEntries
|
|
1556
|
+
.slice(0, 12)
|
|
1557
|
+
.map((entry) => entry.isDirectory ? `${entry.name}/` : entry.name),
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
async function buildWorkspaceContext(rootPath) {
|
|
1562
|
+
const gitAvailable = await isGitRepository(rootPath)
|
|
1563
|
+
const { visibleEntries, notablePaths } = listWorkspaceRootEntries(rootPath)
|
|
1564
|
+
return {
|
|
1565
|
+
repo_root: rootPath,
|
|
1566
|
+
git_available: gitAvailable,
|
|
1567
|
+
visible_entry_count: visibleEntries.length,
|
|
1568
|
+
is_empty_workspace: visibleEntries.length === 0,
|
|
1569
|
+
notable_paths: notablePaths,
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
async function branchExists(branch, dir = repoRoot) {
|
|
1574
|
+
const result = await execCommandSafe("git", ["-C", dir, "branch", "--list", branch], dir)
|
|
1575
|
+
return result !== null && result.stdout.trim().length > 0
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
async function worktreeRegistered(worktreePath, dir = repoRoot) {
|
|
1579
|
+
const result = await execCommandSafe("git", ["-C", dir, "worktree", "list", "--porcelain"], dir)
|
|
1580
|
+
if (!result) return false
|
|
1581
|
+
return result.stdout.includes(worktreePath)
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
/**
|
|
1585
|
+
* Root of the local checkout for a run's repository.
|
|
1586
|
+
*
|
|
1587
|
+
* The runner used to work only in its configured repo-root, so a job belonging to
|
|
1588
|
+
* a team with its own repo produced code from one repo and a PR against another.
|
|
1589
|
+
* Each repo now gets its own checkout under <repoRoot>/.agent/repos/<owner>/<repo>,
|
|
1590
|
+
* cloned on first use. A job whose repo matches the configured repo-root keeps
|
|
1591
|
+
* using that directly, so existing single-repo setups are unaffected.
|
|
1592
|
+
*/
|
|
1593
|
+
async function resolveRunRepoRoot(run, state) {
|
|
1594
|
+
const repo = run?.__repo ?? null
|
|
1595
|
+
if (!repo?.owner || !repo?.repo) return repoRoot
|
|
1596
|
+
|
|
1597
|
+
// Already working in this repo? Use it as-is.
|
|
1598
|
+
const configuredRemote = await gitRemoteSlug(repoRoot)
|
|
1599
|
+
if (configuredRemote
|
|
1600
|
+
&& configuredRemote.owner.toLowerCase() === repo.owner.toLowerCase()
|
|
1601
|
+
&& configuredRemote.repo.toLowerCase() === repo.repo.toLowerCase()) {
|
|
1602
|
+
return repoRoot
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
const checkoutRoot = path.join(repoRoot, ".agent", "repos", repo.owner, repo.repo)
|
|
1606
|
+
|
|
1607
|
+
if (await isGitRepository(checkoutRoot)) {
|
|
1608
|
+
return checkoutRoot
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
if (!run.__githubToken) {
|
|
1612
|
+
runLogger.warn(
|
|
1613
|
+
"runner.repo.clone_skipped",
|
|
1614
|
+
`No GitHub token available to clone ${repo.owner}/${repo.repo}; falling back to the configured repo root`,
|
|
1615
|
+
)
|
|
1616
|
+
return repoRoot
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
ensureDir(path.dirname(checkoutRoot))
|
|
1620
|
+
const cloneUrl = `https://x-access-token:${run.__githubToken}@github.com/${repo.owner}/${repo.repo}.git`
|
|
1621
|
+
await execCommand("git", ["clone", cloneUrl, checkoutRoot], path.dirname(checkoutRoot))
|
|
1622
|
+
// Never leave the token sitting in .git/config.
|
|
1623
|
+
await execCommand(
|
|
1624
|
+
"git",
|
|
1625
|
+
["-C", checkoutRoot, "remote", "set-url", "origin",
|
|
1626
|
+
`https://github.com/${repo.owner}/${repo.repo}.git`],
|
|
1627
|
+
checkoutRoot,
|
|
1628
|
+
)
|
|
1629
|
+
return checkoutRoot
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
/**
|
|
1633
|
+
* Update origin/* from GitHub. The token goes in the fetch URL for this one
|
|
1634
|
+
* command, never into .git/config; a failure (offline, no token) keeps the
|
|
1635
|
+
* refs as they were.
|
|
1636
|
+
*/
|
|
1637
|
+
async function refreshRemoteRefs(dir, run) {
|
|
1638
|
+
const repo = run.__repo
|
|
1639
|
+
const source = run.__githubToken && repo?.owner && repo?.repo
|
|
1640
|
+
? `https://x-access-token:${run.__githubToken}@github.com/${repo.owner}/${repo.repo}.git`
|
|
1641
|
+
: "origin"
|
|
1642
|
+
await execCommandSafe("git", ["-C", dir, ...GIT_IGNORE_STORED_AUTH, "fetch", "--quiet", source, "+refs/heads/*:refs/remotes/origin/*"], dir)
|
|
1643
|
+
|
|
1644
|
+
// In a clone the runner made for itself, keep the checked-out default branch
|
|
1645
|
+
// at GitHub's too, so `git diff main` means what an agent expects. Never in a
|
|
1646
|
+
// person's own checkout: their branch is theirs to move.
|
|
1647
|
+
if (dir.includes(`${path.sep}.agent${path.sep}repos${path.sep}`)) {
|
|
1648
|
+
await execCommandSafe("git", ["-C", dir, "merge", "--ff-only", "--quiet", "origin/HEAD"], dir)
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
/** owner/repo of a checkout's origin remote, or null. */
|
|
1653
|
+
async function gitRemoteSlug(dir) {
|
|
1654
|
+
try {
|
|
1655
|
+
const out = await execCommand("git", ["-C", dir, "remote", "get-url", "origin"], dir)
|
|
1656
|
+
const text = typeof out === "string" ? out : (out?.stdout ?? "")
|
|
1657
|
+
const match = text.trim().match(/github\.com[/:]([^/]+)\/(.+?)(?:\.git)?$/)
|
|
1658
|
+
return match ? { owner: match[1], repo: match[2] } : null
|
|
1659
|
+
} catch {
|
|
1660
|
+
return null
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
async function prepareWorktree(run) {
|
|
1665
|
+
const runRoot = path.join(repoRoot, ".agent", "runs", run.id)
|
|
1666
|
+
ensureDir(runRoot)
|
|
1667
|
+
|
|
1668
|
+
// The repo this run targets, which may differ from the configured repo-root.
|
|
1669
|
+
const runRepoRoot = run.__repoRoot || repoRoot
|
|
1670
|
+
|
|
1671
|
+
const localCheckoutMode = resolveLocalCheckoutMode(run)
|
|
1672
|
+
const branch = buildRunBranchName(run)
|
|
1673
|
+
const worktreePath = buildIssueWorktreePath(run, runRepoRoot)
|
|
1674
|
+
const gitAvailable = await isGitRepository(runRepoRoot)
|
|
1675
|
+
|
|
1676
|
+
if (!gitAvailable) {
|
|
1677
|
+
return {
|
|
1678
|
+
runRoot,
|
|
1679
|
+
worktreePath: runRepoRoot,
|
|
1680
|
+
branch: null,
|
|
1681
|
+
localCheckoutMode: "repo_root",
|
|
1682
|
+
gitAvailable: false,
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
if (localCheckoutMode === "repo_root") {
|
|
1687
|
+
const currentRepoBranch = await currentBranch(runRepoRoot)
|
|
1688
|
+
return {
|
|
1689
|
+
runRoot,
|
|
1690
|
+
worktreePath: runRepoRoot,
|
|
1691
|
+
branch: currentRepoBranch,
|
|
1692
|
+
localCheckoutMode,
|
|
1693
|
+
gitAvailable: true,
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
ensureDir(path.dirname(worktreePath))
|
|
1698
|
+
|
|
1699
|
+
// Pull requests merge on GitHub, so origin/* in this checkout falls behind.
|
|
1700
|
+
// A stale base gave builders an old main to work on, and gave reviewers a
|
|
1701
|
+
// `git diff origin/HEAD..HEAD` full of work that had already merged.
|
|
1702
|
+
await refreshRemoteRefs(runRepoRoot, run)
|
|
1703
|
+
const upToDateBase = (await execCommandSafe(
|
|
1704
|
+
"git", ["-C", runRepoRoot, "rev-parse", "--verify", "--quiet", "origin/HEAD"], runRepoRoot,
|
|
1705
|
+
)) ? "origin/HEAD" : "HEAD"
|
|
1706
|
+
|
|
1707
|
+
// Which ticket this checkout belongs to, by id: identifiers can repeat, and
|
|
1708
|
+
// cleanup must never remove another ticket's checkout.
|
|
1709
|
+
fs.writeFileSync(path.join(path.dirname(worktreePath), "pacework-task.json"), JSON.stringify({ task_id: run.task_id, branch }))
|
|
1710
|
+
|
|
1711
|
+
const worktreeExists = fs.existsSync(worktreePath)
|
|
1712
|
+
const worktreeKnown = await worktreeRegistered(worktreePath, runRepoRoot)
|
|
1713
|
+
|
|
1714
|
+
if (worktreeExists && !worktreeKnown) {
|
|
1715
|
+
// Stale directory from a previous failed worktree add — clean it up
|
|
1716
|
+
fs.rmSync(worktreePath, { recursive: true, force: true })
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
if (!fs.existsSync(worktreePath)) {
|
|
1720
|
+
const branchAlreadyExists = await branchExists(branch, runRepoRoot)
|
|
1721
|
+
if (branchAlreadyExists) {
|
|
1722
|
+
// Branch exists from a prior run — reuse it instead of -b
|
|
1723
|
+
await execCommand("git", ["-C", runRepoRoot, "worktree", "add", worktreePath, branch], runRepoRoot)
|
|
1724
|
+
} else {
|
|
1725
|
+
await execCommand("git", ["-C", runRepoRoot, "worktree", "add", "-b", branch, worktreePath, upToDateBase], runRepoRoot)
|
|
1726
|
+
}
|
|
1727
|
+
} else if (worktreeKnown) {
|
|
1728
|
+
const checkedOutBranch = await currentBranch(worktreePath)
|
|
1729
|
+
if (checkedOutBranch && checkedOutBranch !== branch) {
|
|
1730
|
+
// The worktree may be left on a branch from a previous executor (the
|
|
1731
|
+
// branch prefix is per-executor), so the target branch can be absent.
|
|
1732
|
+
// Create it when missing instead of failing the whole run.
|
|
1733
|
+
const targetExists = await branchExists(branch, runRepoRoot)
|
|
1734
|
+
await execCommand(
|
|
1735
|
+
"git",
|
|
1736
|
+
targetExists
|
|
1737
|
+
? ["-C", worktreePath, "checkout", branch]
|
|
1738
|
+
: ["-C", worktreePath, "checkout", "-b", branch],
|
|
1739
|
+
worktreePath,
|
|
1740
|
+
)
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
|
|
1744
|
+
return { runRoot, worktreePath, branch, localCheckoutMode, gitAvailable: true }
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1747
|
+
function buildPrompt(run, branch, worktreePath, options = {}) {
|
|
1748
|
+
const issue = run.task
|
|
1749
|
+
const agent = run.agent
|
|
1750
|
+
const isReviewer = isReviewRun(run)
|
|
1751
|
+
const localCheckoutMode = options.localCheckoutMode || resolveLocalCheckoutMode(run)
|
|
1752
|
+
const gitAvailable = options.gitAvailable !== false
|
|
1753
|
+
const workspaceContext = options.workspaceContext || null
|
|
1754
|
+
const greenfieldWorkspace = workspaceContext?.is_empty_workspace === true
|
|
1755
|
+
const skillPrompt = agent?.skill_profile?.system_prompt?.trim()
|
|
1756
|
+
const base = skillPrompt ? `${skillPrompt}\n\n---\n\n` : ""
|
|
1757
|
+
const codingRules = Array.isArray(run?.coding_rules) ? run.coding_rules : []
|
|
1758
|
+
const reviewerContext = run?.reviewer_context || null
|
|
1759
|
+
const description = issue?.description || "No description provided."
|
|
1760
|
+
const lowerDescription = description.toLowerCase()
|
|
1761
|
+
const explicitlyNoEdit = [
|
|
1762
|
+
"do not modify files",
|
|
1763
|
+
"leave the repository unchanged",
|
|
1764
|
+
"leave the repo unchanged",
|
|
1765
|
+
"no-edit smoke test",
|
|
1766
|
+
"read-only smoke test",
|
|
1767
|
+
"read only smoke test",
|
|
1768
|
+
].some(pattern => lowerDescription.includes(pattern))
|
|
1769
|
+
const noEditMode = explicitlyNoEdit || (automationSmokeTestMode && isAutomationSmokeTestIssue(run))
|
|
1770
|
+
const executionModeGuidance = isReviewer
|
|
1771
|
+
? `Reviewer mode.
|
|
1772
|
+
- Do not modify product code while reviewing.
|
|
1773
|
+
- Inspect the reviewer context, changed files, verification notes, and any relevant issue comments.
|
|
1774
|
+
- Read the pull request's diff with the \`get_pr_diff\` tool: it is exactly what will merge.
|
|
1775
|
+
Do not judge scope from a local \`git diff\` — the local base branch can be behind, and
|
|
1776
|
+
work already merged would look like part of this change. Use \`git diff\` only when the
|
|
1777
|
+
issue has no pull request yet.
|
|
1778
|
+
- Do not call \`review_pr\` or \`submit_issue_review\` yourself. The runner posts your review
|
|
1779
|
+
to the pull request and the issue from the lines below.
|
|
1780
|
+
- End your final response with exactly this, filling in every part:
|
|
1781
|
+
|
|
1782
|
+
REVIEW_DECISION: approve|request_changes|block
|
|
1783
|
+
REVIEW_SUMMARY:
|
|
1784
|
+
**Verdict:** one sentence on whether this change does what the issue asks.
|
|
1785
|
+
|
|
1786
|
+
**What I checked**
|
|
1787
|
+
- each acceptance point or behaviour you verified, and how
|
|
1788
|
+
|
|
1789
|
+
**Findings**
|
|
1790
|
+
- \`path/to/file:line\` — a concrete problem and why it matters (or "None")
|
|
1791
|
+
|
|
1792
|
+
**Suggestions** (optional, non-blocking)
|
|
1793
|
+
- smaller improvements that should not hold up the merge
|
|
1794
|
+
|
|
1795
|
+
Use request_changes only for findings that must be fixed before merging.
|
|
1796
|
+
Any removed or changed line the issue did not ask for is a must-fix finding — removing
|
|
1797
|
+
work that is already on the base branch is the worst thing a pull request can do.`
|
|
1798
|
+
: noEditMode
|
|
1799
|
+
? `This is a no-edit smoke test. Do not modify files. Keep command usage minimal.
|
|
1800
|
+
- Use at most 3 read-only shell commands.
|
|
1801
|
+
- Do not inspect more of the repo than necessary.
|
|
1802
|
+
- Finish as soon as you can confirm the local runner path is functioning.`
|
|
1803
|
+
: `Normal execution mode.
|
|
1804
|
+
- Make pragmatic code changes directly in the repository when needed.
|
|
1805
|
+
- Run relevant verification commands for the files you touch.
|
|
1806
|
+
- Never rebase, reset, or force-push away commits — least of all ones already on the base
|
|
1807
|
+
branch, even if a reviewer suggests it. To bring in the latest base branch, merge
|
|
1808
|
+
\`origin/HEAD\` into this branch and resolve any conflicts.
|
|
1809
|
+
- When resolving a merge conflict, keep BOTH sides: the base branch's lines are other
|
|
1810
|
+
people's merged work. Combine them with this issue's change; never pick one side over
|
|
1811
|
+
the other unless the issue says to replace that content.
|
|
1812
|
+
- If feedback asks you to remove changes "unrelated" to this issue, first run
|
|
1813
|
+
\`git diff origin/HEAD...HEAD\`. Anything not in that diff is already on the base
|
|
1814
|
+
branch — do not revert it; say so in your summary instead.`
|
|
1815
|
+
|
|
1816
|
+
const checkoutModeGuidance = !gitAvailable
|
|
1817
|
+
? `- Work directly in the assigned workspace folder: ${worktreePath}
|
|
1818
|
+
- This folder is not a git repository.
|
|
1819
|
+
- Do not rely on branches, worktrees, or git diff output for this run.`
|
|
1820
|
+
: localCheckoutMode === "repo_root"
|
|
1821
|
+
? `- Work directly in the repository root: ${worktreePath}
|
|
1822
|
+
- The runner did not create or switch branches for you.
|
|
1823
|
+
- Start from the current repo branch: ${branch || "unknown"}
|
|
1824
|
+
- Avoid branch/worktree setup unless the issue explicitly requires it.`
|
|
1825
|
+
: `- Work inside this git worktree only: ${worktreePath}
|
|
1826
|
+
- The ticket branch is already created for this execution mode: ${branch}`
|
|
1827
|
+
const existingCheckoutGuidance = !gitAvailable
|
|
1828
|
+
? `- The workspace folder may already contain files from an earlier attempt. Inspect the existing files before you edit and continue from the current state instead of assuming a clean repository.`
|
|
1829
|
+
: localCheckoutMode === "repo_root"
|
|
1830
|
+
? `- The repository root may already contain changes from an earlier attempt or other local work. Inspect the current diff before you edit, continue from prior issue work when appropriate, and do not revert unrelated changes.`
|
|
1831
|
+
: `- This ticket checkout may already contain changes from an earlier attempt. Inspect the current diff before you edit and continue from the existing state instead of starting over.`
|
|
1832
|
+
const workspaceSnapshotGuidance = workspaceContext
|
|
1833
|
+
? `- Workspace snapshot: ${greenfieldWorkspace ? "no user project files exist yet" : `${workspaceContext.visible_entry_count} visible root entries detected`}${workspaceContext.notable_paths?.length ? ` (${workspaceContext.notable_paths.join(", ")})` : ""}.`
|
|
1834
|
+
: ""
|
|
1835
|
+
const greenfieldGuidance = greenfieldWorkspace
|
|
1836
|
+
? `- This is a greenfield workspace. If the issue asks for a new app, game, or project, scaffold it directly in this folder instead of waiting for existing structure.
|
|
1837
|
+
- Respect any explicit framework request from the issue description. If it says Next.js, use Next.js.
|
|
1838
|
+
- If no stack is specified, choose a sensible best-practice stack for the request and proceed without blocking on a planning question.`
|
|
1839
|
+
: ""
|
|
1840
|
+
const optionalGuidance = [
|
|
1841
|
+
workspaceSnapshotGuidance,
|
|
1842
|
+
greenfieldGuidance,
|
|
1843
|
+
].filter(Boolean).join("\n")
|
|
1844
|
+
const codingRulesGuidance = codingRules.length > 0
|
|
1845
|
+
? `- Workspace coding rules (must follow):\n${codingRules.map((rule) => ` - ${rule.title}: ${rule.rule_text}`).join("\n")}`
|
|
1846
|
+
: ""
|
|
1847
|
+
// Feedback posted after the original description — reviewer notes and PR
|
|
1848
|
+
// comments. Without this the agent re-reads only the description, sees the
|
|
1849
|
+
// work already done, and reports "no changes needed".
|
|
1850
|
+
const recentComments = Array.isArray(run?.recent_comments) ? run.recent_comments : []
|
|
1851
|
+
const feedbackGuidance = recentComments.length > 0
|
|
1852
|
+
? `- Recent activity on this issue (newest first). Treat any feedback here as\n`
|
|
1853
|
+
+ ` work still to do, even when the original description already looks satisfied:\n`
|
|
1854
|
+
+ recentComments
|
|
1855
|
+
.slice(0, 6)
|
|
1856
|
+
.map((c) => ` - ${c.author || "Someone"}: ${String(c.content || "").replace(/\s+/g, " ").slice(0, 400)}`)
|
|
1857
|
+
.join("\n")
|
|
1858
|
+
: ""
|
|
1859
|
+
const answersGuidance = clarificationAnswers(run)
|
|
1860
|
+
const environmentGuidance = run.__environmentNote
|
|
1861
|
+
? `- Environment setup did not fully work. Say so in your summary rather than treating it as a bug in the code:\n ${run.__environmentNote.replace(/\n/g, "\n ")}`
|
|
1862
|
+
: ""
|
|
1863
|
+
const environmentServices = Object.keys(run.__extraEnv || {}).filter((key) => /_URL$|_URI$/.test(key))
|
|
1864
|
+
const reviewerGuidance = reviewerContext
|
|
1865
|
+
? `- Reviewer context:
|
|
1866
|
+
- Latest implementation branch: ${reviewerContext.branch_name || "not reported"}
|
|
1867
|
+
- Implementation summary: ${reviewerContext.result_summary || "not reported"}
|
|
1868
|
+
- Verification summary: ${reviewerContext.verification_summary || "not reported"}
|
|
1869
|
+
${reviewerContext.pr_diff
|
|
1870
|
+
? ` - The pull request diff, from GitHub — exactly what will merge, and the only change to judge${reviewerContext.pr_diff_truncated ? " (truncated; use get_pr_diff or git for the rest)" : ""}:
|
|
1871
|
+
${reviewerContext.pr_diff}`
|
|
1872
|
+
: ` - Diff excerpt:
|
|
1873
|
+
${reviewerContext.diff_excerpt || "(not reported)"}`}`
|
|
1874
|
+
: ""
|
|
1875
|
+
|
|
1876
|
+
return `${base}You are ${agent?.name || "Dev Bot"}, working locally inside the assigned workspace folder.
|
|
1877
|
+
|
|
1878
|
+
Assigned issue:
|
|
1879
|
+
- Identifier: ${issue?.identifier || run.task_id}
|
|
1880
|
+
- Title: ${issue?.title || "Untitled issue"}
|
|
1881
|
+
- Description: ${description}
|
|
1882
|
+
- Current status: ${issue?.status || "unknown"}
|
|
1883
|
+
${answersGuidance ? `\n${answersGuidance}\n` : ""}
|
|
1884
|
+
Execution rules:
|
|
1885
|
+
- Local checkout mode: ${localCheckoutMode}
|
|
1886
|
+
${checkoutModeGuidance}
|
|
1887
|
+
${existingCheckoutGuidance}
|
|
1888
|
+
${optionalGuidance ? `${optionalGuidance}\n` : ""}${environmentServices.length > 0 ? `- Services are running for this checkout; their addresses are in ${environmentServices.join(", ")}.\n` : ""}${environmentGuidance ? `${environmentGuidance}\n` : ""}${feedbackGuidance ? `${feedbackGuidance}\n` : ""}${codingRulesGuidance ? `${codingRulesGuidance}\n` : ""}${reviewerGuidance ? `${reviewerGuidance}\n` : ""}- Do not open a PR yet.
|
|
1889
|
+
- Treat explicit issue-description constraints as higher priority than generic instructions.
|
|
1890
|
+
- ${executionModeGuidance}
|
|
1891
|
+
${isReviewer || answersGuidance ? "" : `- ${ASK_WHEN_UNCLEAR.replace(/\n/g, "\n ")}\n`}- End with a concise summary of what changed, what was verified, and any remaining gaps.
|
|
1892
|
+
|
|
1893
|
+
${isReviewer ? "" : `Your final response becomes the pull request description, so write it for a reviewer
|
|
1894
|
+
who has not seen this issue. Use exactly these markdown headings:
|
|
1895
|
+
|
|
1896
|
+
## Summary
|
|
1897
|
+
What changed and why, in a few sentences. Name the behaviour, not just the files.
|
|
1898
|
+
|
|
1899
|
+
## Verification
|
|
1900
|
+
What you ran or checked, and the result.
|
|
1901
|
+
|
|
1902
|
+
## Remaining gaps
|
|
1903
|
+
Anything left undone or worth a follow-up, or "None".
|
|
1904
|
+
`}`
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
/**
|
|
1908
|
+
* Whether this run is sandboxed: this machine asked for it (`--sandbox`), or the
|
|
1909
|
+
* workspace requires it (on by default). A cloud job is a throwaway machine of
|
|
1910
|
+
* its own, so it runs without.
|
|
1911
|
+
*/
|
|
1912
|
+
function runSandboxed(run) {
|
|
1913
|
+
if (cloudState) return false
|
|
1914
|
+
return sandboxed || run?.sandbox === true
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1917
|
+
/** Domains a sandboxed run may reach: registries and GitHub, plus the repository's own. */
|
|
1918
|
+
function runAllowedDomains(run) {
|
|
1919
|
+
const extra = Array.isArray(run?.environment?.allowed_domains) ? run.environment.allowed_domains : []
|
|
1920
|
+
return [...new Set([...SANDBOX_ALLOWED_DOMAINS, ...extra])]
|
|
1921
|
+
}
|
|
1922
|
+
|
|
1923
|
+
/**
|
|
1924
|
+
* Claude Code --settings for a run, or null when it needs none: the sandbox
|
|
1925
|
+
* (see runSandboxed) and the agent's own Anthropic key.
|
|
1926
|
+
*/
|
|
1927
|
+
function claudeRunSettings(run) {
|
|
1928
|
+
const settings = {}
|
|
1929
|
+
if (runSandboxed(run)) {
|
|
1930
|
+
// A worktree's git data lives in the main repository's .git, outside the
|
|
1931
|
+
// checkout; without it the agent cannot commit.
|
|
1932
|
+
const gitDir = path.join(run.__repoRoot || repoRoot, ".git")
|
|
1933
|
+
settings.sandbox = { enabled: true, autoAllowBashIfSandboxed: true, allowUnsandboxedCommands: false }
|
|
1934
|
+
settings.permissions = {
|
|
1935
|
+
allow: [
|
|
1936
|
+
`Edit(${gitDir}/**)`,
|
|
1937
|
+
...runAllowedDomains(run).map((domain) => `WebFetch(domain:${domain})`),
|
|
1938
|
+
],
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
if (runCredentials(run)?.provider === "anthropic") {
|
|
1942
|
+
// ANTHROPIC_API_KEY alone loses to a signed-in Claude subscription, so the
|
|
1943
|
+
// run would be billed to the machine's plan. A key helper always wins. It
|
|
1944
|
+
// reads the key from the run's environment, keeping it out of argv.
|
|
1945
|
+
settings.apiKeyHelper = 'printf %s "$PACEWORK_RUN_API_KEY"'
|
|
1946
|
+
}
|
|
1947
|
+
return Object.keys(settings).length ? JSON.stringify(settings) : null
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
function buildExecutorCommand(run, worktreePath, prompt, outputFile, mcpKey) {
|
|
1951
|
+
const requestedModel = resolveRequestedModel(run)
|
|
1952
|
+
|
|
1953
|
+
if (activeExecutor() === "codex") {
|
|
1954
|
+
const argsList = [
|
|
1955
|
+
"exec",
|
|
1956
|
+
// Only pin a model when one was actually requested; otherwise let codex
|
|
1957
|
+
// use its own current default rather than a name hardcoded here.
|
|
1958
|
+
...(requestedModel ? ["-m", requestedModel] : []),
|
|
1959
|
+
"--ephemeral",
|
|
1960
|
+
// Codex's sandbox cannot allow single domains, so network stays on.
|
|
1961
|
+
...(runSandboxed(run)
|
|
1962
|
+
? [
|
|
1963
|
+
"-s", "workspace-write",
|
|
1964
|
+
"-c", "approval_policy=\"never\"",
|
|
1965
|
+
"-c", "sandbox_workspace_write.network_access=true",
|
|
1966
|
+
"-c", `sandbox_workspace_write.writable_roots=${JSON.stringify([path.join(run.__repoRoot || repoRoot, ".git")])}`,
|
|
1967
|
+
]
|
|
1968
|
+
: ["--dangerously-bypass-approvals-and-sandbox"]),
|
|
1969
|
+
"--color",
|
|
1970
|
+
"never",
|
|
1971
|
+
"-C",
|
|
1972
|
+
worktreePath,
|
|
1973
|
+
"--output-last-message",
|
|
1974
|
+
outputFile,
|
|
1975
|
+
"-",
|
|
1976
|
+
]
|
|
1977
|
+
return {
|
|
1978
|
+
command: codexBin,
|
|
1979
|
+
args: argsList,
|
|
1980
|
+
stdin: prompt,
|
|
1981
|
+
summaryMode: "file",
|
|
1982
|
+
}
|
|
1983
|
+
}
|
|
1984
|
+
|
|
1985
|
+
if (activeExecutor() === "claude_code") {
|
|
1986
|
+
const argsList = [
|
|
1987
|
+
"-p",
|
|
1988
|
+
prompt,
|
|
1989
|
+
// json: the same final message, plus what the run cost.
|
|
1990
|
+
"--output-format",
|
|
1991
|
+
"json",
|
|
1992
|
+
"--permission-mode",
|
|
1993
|
+
"acceptEdits",
|
|
1994
|
+
"--dangerously-skip-permissions",
|
|
1995
|
+
...(claudeRunSettings(run) ? ["--settings", claudeRunSettings(run)] : []),
|
|
1996
|
+
]
|
|
1997
|
+
if (mcpKey) {
|
|
1998
|
+
const inlineMcpConfig = JSON.stringify({
|
|
1999
|
+
mcpServers: {
|
|
2000
|
+
kentral_local: {
|
|
2001
|
+
type: "http",
|
|
2002
|
+
url: `${appUrl}/api/mcp`,
|
|
2003
|
+
headers: { Authorization: `Bearer ${mcpKey}` },
|
|
2004
|
+
},
|
|
2005
|
+
},
|
|
2006
|
+
})
|
|
2007
|
+
argsList.push("--mcp-config", inlineMcpConfig)
|
|
2008
|
+
}
|
|
2009
|
+
if (requestedModel) {
|
|
2010
|
+
argsList.push("--model", requestedModel)
|
|
2011
|
+
}
|
|
2012
|
+
return {
|
|
2013
|
+
command: claudeCodeBin,
|
|
2014
|
+
args: argsList,
|
|
2015
|
+
stdin: null,
|
|
2016
|
+
summaryMode: "claude_json",
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
if (activeExecutor() === "gemini") {
|
|
2021
|
+
const argsList = [
|
|
2022
|
+
"-p",
|
|
2023
|
+
prompt,
|
|
2024
|
+
"--output-format",
|
|
2025
|
+
"text",
|
|
2026
|
+
"--approval-mode",
|
|
2027
|
+
"yolo",
|
|
2028
|
+
]
|
|
2029
|
+
if (requestedModel) {
|
|
2030
|
+
argsList.push("--model", requestedModel)
|
|
2031
|
+
}
|
|
2032
|
+
return {
|
|
2033
|
+
command: geminiBin,
|
|
2034
|
+
args: argsList,
|
|
2035
|
+
stdin: null,
|
|
2036
|
+
summaryMode: "stdout",
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
|
|
2040
|
+
if (activeExecutor() === "cursor_agent") {
|
|
2041
|
+
const argsList = [
|
|
2042
|
+
"--print",
|
|
2043
|
+
"--output-format",
|
|
2044
|
+
"text",
|
|
2045
|
+
"--trust",
|
|
2046
|
+
"--force",
|
|
2047
|
+
"--workspace",
|
|
2048
|
+
worktreePath,
|
|
2049
|
+
]
|
|
2050
|
+
if (requestedModel) {
|
|
2051
|
+
argsList.push("--model", requestedModel)
|
|
2052
|
+
}
|
|
2053
|
+
argsList.push(prompt)
|
|
2054
|
+
return {
|
|
2055
|
+
command: cursorAgentBin,
|
|
2056
|
+
args: argsList,
|
|
2057
|
+
stdin: null,
|
|
2058
|
+
summaryMode: "stdout",
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
|
|
2062
|
+
if (activeExecutor() === "github_copilot") {
|
|
2063
|
+
const useGhWrapper = path.basename(githubCopilotBin).toLowerCase() === "gh"
|
|
2064
|
+
const argsList = useGhWrapper ? ["copilot", "--"] : []
|
|
2065
|
+
argsList.push(
|
|
2066
|
+
"-p",
|
|
2067
|
+
prompt,
|
|
2068
|
+
"--allow-all",
|
|
2069
|
+
"--no-ask-user",
|
|
2070
|
+
"--output-format",
|
|
2071
|
+
"text",
|
|
2072
|
+
"--add-dir",
|
|
2073
|
+
worktreePath,
|
|
2074
|
+
"-s",
|
|
2075
|
+
)
|
|
2076
|
+
if (requestedModel) {
|
|
2077
|
+
argsList.push("--model", requestedModel)
|
|
2078
|
+
}
|
|
2079
|
+
return {
|
|
2080
|
+
command: githubCopilotBin,
|
|
2081
|
+
args: argsList,
|
|
2082
|
+
stdin: null,
|
|
2083
|
+
summaryMode: "stdout",
|
|
2084
|
+
}
|
|
2085
|
+
}
|
|
2086
|
+
|
|
2087
|
+
throw new Error(`Unsupported local executor: ${activeExecutor()}`)
|
|
2088
|
+
}
|
|
2089
|
+
|
|
2090
|
+
function escapeTomlBasicString(value) {
|
|
2091
|
+
return String(value || "")
|
|
2092
|
+
.replace(/\\/g, "\\\\")
|
|
2093
|
+
.replace(/"/g, '\\"')
|
|
2094
|
+
}
|
|
2095
|
+
|
|
2096
|
+
// ── MCP server fetching ────────────────────────────────────────────────────
|
|
2097
|
+
|
|
2098
|
+
async function fetchMcpServers(state) {
|
|
2099
|
+
try {
|
|
2100
|
+
const result = await requestJson(
|
|
2101
|
+
`${appUrl}/api/agent-runners/mcp-servers?workspace_id=${encodeURIComponent(state.workspace_id)}&runner_id=${encodeURIComponent(state.runner_id)}`,
|
|
2102
|
+
{
|
|
2103
|
+
method: "GET",
|
|
2104
|
+
headers: {
|
|
2105
|
+
Authorization: `Bearer ${state.runner_secret}`,
|
|
2106
|
+
},
|
|
2107
|
+
},
|
|
2108
|
+
)
|
|
2109
|
+
return Array.isArray(result?.mcp_servers) ? result.mcp_servers : []
|
|
2110
|
+
} catch {
|
|
2111
|
+
return []
|
|
2112
|
+
}
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
// ── MCP config injection per executor ─────────────────────────────────────
|
|
2116
|
+
|
|
2117
|
+
function buildTomlMcpBlock(servers) {
|
|
2118
|
+
const lines = []
|
|
2119
|
+
for (const s of servers) {
|
|
2120
|
+
const key = s.name.replace(/[^a-zA-Z0-9_]/g, "_")
|
|
2121
|
+
if (s.transport === "http" && s.url) {
|
|
2122
|
+
lines.push(`[mcp_servers.${key}]`)
|
|
2123
|
+
lines.push(`enabled = true`)
|
|
2124
|
+
lines.push(`url = "${escapeTomlBasicString(s.url)}"`)
|
|
2125
|
+
if (s.headers && typeof s.headers === "object") {
|
|
2126
|
+
lines.push(`[mcp_servers.${key}.http_headers]`)
|
|
2127
|
+
for (const [hk, hv] of Object.entries(s.headers)) {
|
|
2128
|
+
lines.push(`${hk} = "${escapeTomlBasicString(String(hv))}"`)
|
|
2129
|
+
}
|
|
2130
|
+
}
|
|
2131
|
+
if (s.env && typeof s.env === "object") {
|
|
2132
|
+
lines.push(`[mcp_servers.${key}.env]`)
|
|
2133
|
+
for (const [ek, ev] of Object.entries(s.env)) {
|
|
2134
|
+
lines.push(`${ek} = "${escapeTomlBasicString(String(ev))}"`)
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
lines.push("")
|
|
2138
|
+
} else if (s.transport === "stdio" && s.command) {
|
|
2139
|
+
// codex only supports http MCP servers — skip stdio entries
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
return lines.join("\n")
|
|
2143
|
+
}
|
|
2144
|
+
|
|
2145
|
+
const CODEX_HOME_RETENTION_MS = 24 * 60 * 60_000
|
|
2146
|
+
|
|
2147
|
+
/** Run homes outlive their run briefly; clear old ones so auth copies don't pile up. */
|
|
2148
|
+
function pruneStaleCodexHomes(runsDir) {
|
|
2149
|
+
let entries = []
|
|
2150
|
+
try {
|
|
2151
|
+
entries = fs.readdirSync(runsDir, { withFileTypes: true })
|
|
2152
|
+
} catch {
|
|
2153
|
+
return
|
|
2154
|
+
}
|
|
2155
|
+
const cutoff = Date.now() - CODEX_HOME_RETENTION_MS
|
|
2156
|
+
for (const entry of entries) {
|
|
2157
|
+
if (!entry.isDirectory()) continue
|
|
2158
|
+
const dir = path.join(runsDir, entry.name)
|
|
2159
|
+
try {
|
|
2160
|
+
if (fs.statSync(dir).mtimeMs < cutoff) fs.rmSync(dir, { recursive: true, force: true })
|
|
2161
|
+
} catch {}
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
|
|
2165
|
+
function ensureIsolatedCodexHome(runId, worktreePath, mcpKey, requestedModel, userMcpServers, openAiKey = null) {
|
|
2166
|
+
// One home per run. A shared home meant parallel runs overwrote each other's
|
|
2167
|
+
// config.toml — including the MCP key that says which agent is calling.
|
|
2168
|
+
const runsDir = path.join(repoRoot, ".agent", "codex-home", activeExecutor(), "runs")
|
|
2169
|
+
pruneStaleCodexHomes(runsDir)
|
|
2170
|
+
const codexHome = path.join(runsDir, String(runId))
|
|
2171
|
+
ensureDir(codexHome)
|
|
2172
|
+
ensureDir(path.join(codexHome, "log"))
|
|
2173
|
+
ensureDir(path.join(codexHome, "tmp"))
|
|
2174
|
+
fs.rmSync(path.join(codexHome, ".tmp", "plugins"), { recursive: true, force: true })
|
|
2175
|
+
|
|
2176
|
+
const sourceCodexHome = process.env.CODEX_HOME || path.join(os.homedir(), ".codex")
|
|
2177
|
+
const sourceAuthFile = path.join(sourceCodexHome, "auth.json")
|
|
2178
|
+
const targetAuthFile = path.join(codexHome, "auth.json")
|
|
2179
|
+
if (openAiKey) {
|
|
2180
|
+
// The agent's own key — what `codex login --api-key` would store — so the
|
|
2181
|
+
// run is billed to it, not to the machine's ChatGPT login.
|
|
2182
|
+
fs.writeFileSync(targetAuthFile, JSON.stringify({ OPENAI_API_KEY: openAiKey }), { mode: 0o600 })
|
|
2183
|
+
} else if (fs.existsSync(sourceAuthFile) && !fs.existsSync(targetAuthFile)) {
|
|
2184
|
+
fs.copyFileSync(sourceAuthFile, targetAuthFile)
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
const userMcpBlock = buildTomlMcpBlock(userMcpServers || [])
|
|
2188
|
+
|
|
2189
|
+
const configToml = [
|
|
2190
|
+
// Omit the model line entirely when nothing was requested, so codex falls
|
|
2191
|
+
// back to its own configured default instead of a name pinned here.
|
|
2192
|
+
...(requestedModel ? [`model = "${escapeTomlBasicString(requestedModel)}"`] : []),
|
|
2193
|
+
`model_reasoning_effort = "${escapeTomlBasicString(codexReasoningEffort)}"`,
|
|
2194
|
+
"",
|
|
2195
|
+
`[projects."${escapeTomlBasicString(worktreePath)}"]`,
|
|
2196
|
+
`trust_level = "trusted"`,
|
|
2197
|
+
"",
|
|
2198
|
+
`[features]`,
|
|
2199
|
+
`multi_agent = false`,
|
|
2200
|
+
"",
|
|
2201
|
+
`[mcp_servers.kentral_local]`,
|
|
2202
|
+
`enabled = true`,
|
|
2203
|
+
`url = "${escapeTomlBasicString(`${appUrl}/api/mcp`)}"`,
|
|
2204
|
+
"",
|
|
2205
|
+
`[mcp_servers.kentral_local.http_headers]`,
|
|
2206
|
+
`Authorization = "Bearer ${escapeTomlBasicString(mcpKey)}"`,
|
|
2207
|
+
"",
|
|
2208
|
+
`[mcp_servers.kentral_local.tools.update_issue_status]`,
|
|
2209
|
+
`approval_mode = "approve"`,
|
|
2210
|
+
"",
|
|
2211
|
+
userMcpBlock,
|
|
2212
|
+
].join("\n")
|
|
2213
|
+
|
|
2214
|
+
fs.writeFileSync(path.join(codexHome, "config.toml"), configToml)
|
|
2215
|
+
|
|
2216
|
+
return codexHome
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
function buildClaudeMcpJson(mcpKey, userMcpServers) {
|
|
2220
|
+
const mcpServers = {
|
|
2221
|
+
kentral_local: {
|
|
2222
|
+
type: "http",
|
|
2223
|
+
url: `${appUrl}/api/mcp`,
|
|
2224
|
+
headers: { Authorization: `Bearer ${mcpKey}` },
|
|
2225
|
+
},
|
|
2226
|
+
}
|
|
2227
|
+
|
|
2228
|
+
for (const s of (userMcpServers || [])) {
|
|
2229
|
+
const key = s.name.replace(/[^a-zA-Z0-9_]/g, "_")
|
|
2230
|
+
if (s.transport === "http" && s.url) {
|
|
2231
|
+
mcpServers[key] = {
|
|
2232
|
+
type: "http",
|
|
2233
|
+
url: s.url,
|
|
2234
|
+
...(s.headers && Object.keys(s.headers).length ? { headers: s.headers } : {}),
|
|
2235
|
+
}
|
|
2236
|
+
} else if (s.transport === "stdio" && s.command) {
|
|
2237
|
+
mcpServers[key] = {
|
|
2238
|
+
type: "stdio",
|
|
2239
|
+
command: s.command,
|
|
2240
|
+
...(Array.isArray(s.args) && s.args.length ? { args: s.args } : {}),
|
|
2241
|
+
...(s.env && Object.keys(s.env).length ? { env: s.env } : {}),
|
|
2242
|
+
}
|
|
2243
|
+
}
|
|
2244
|
+
}
|
|
2245
|
+
|
|
2246
|
+
return { mcpServers }
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
function buildJsonMcpServers(mcpKey, userMcpServers) {
|
|
2250
|
+
const servers = {
|
|
2251
|
+
kentral_local: {
|
|
2252
|
+
type: "http",
|
|
2253
|
+
url: `${appUrl}/api/mcp`,
|
|
2254
|
+
headers: { Authorization: `Bearer ${mcpKey}` },
|
|
2255
|
+
},
|
|
2256
|
+
}
|
|
2257
|
+
|
|
2258
|
+
for (const s of (userMcpServers || [])) {
|
|
2259
|
+
const key = s.name.replace(/[^a-zA-Z0-9_]/g, "_")
|
|
2260
|
+
if (s.transport === "http" && s.url) {
|
|
2261
|
+
servers[key] = {
|
|
2262
|
+
type: "http",
|
|
2263
|
+
url: s.url,
|
|
2264
|
+
...(s.headers && Object.keys(s.headers).length ? { headers: s.headers } : {}),
|
|
2265
|
+
}
|
|
2266
|
+
} else if (s.transport === "stdio" && s.command) {
|
|
2267
|
+
servers[key] = {
|
|
2268
|
+
command: s.command,
|
|
2269
|
+
...(Array.isArray(s.args) && s.args.length ? { args: s.args } : {}),
|
|
2270
|
+
...(s.env && Object.keys(s.env).length ? { env: s.env } : {}),
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
|
|
2275
|
+
return servers
|
|
2276
|
+
}
|
|
2277
|
+
|
|
2278
|
+
function injectClaudeCodeMcp(mcpKey, userMcpServers) {
|
|
2279
|
+
const claudeConfigDir = path.join(repoRoot, ".agent", "claude-home")
|
|
2280
|
+
ensureDir(claudeConfigDir)
|
|
2281
|
+
|
|
2282
|
+
const claudeJsonPath = path.join(claudeConfigDir, ".claude.json")
|
|
2283
|
+
let existing = {}
|
|
2284
|
+
try {
|
|
2285
|
+
existing = JSON.parse(fs.readFileSync(claudeJsonPath, "utf8"))
|
|
2286
|
+
} catch {}
|
|
2287
|
+
|
|
2288
|
+
const updated = { ...existing, ...buildClaudeMcpJson(mcpKey, userMcpServers) }
|
|
2289
|
+
fs.writeFileSync(claudeJsonPath, JSON.stringify(updated, null, 2))
|
|
2290
|
+
|
|
2291
|
+
return claudeConfigDir
|
|
2292
|
+
}
|
|
2293
|
+
|
|
2294
|
+
function injectGeminiMcp(mcpKey, userMcpServers) {
|
|
2295
|
+
const geminiConfigDir = path.join(os.homedir(), ".gemini")
|
|
2296
|
+
ensureDir(geminiConfigDir)
|
|
2297
|
+
const settingsPath = path.join(geminiConfigDir, "settings.json")
|
|
2298
|
+
|
|
2299
|
+
let existing = {}
|
|
2300
|
+
try {
|
|
2301
|
+
existing = JSON.parse(fs.readFileSync(settingsPath, "utf8"))
|
|
2302
|
+
} catch {}
|
|
2303
|
+
|
|
2304
|
+
const backup = fs.existsSync(settingsPath) ? fs.readFileSync(settingsPath, "utf8") : null
|
|
2305
|
+
|
|
2306
|
+
const mcpServers = buildJsonMcpServers(mcpKey, userMcpServers)
|
|
2307
|
+
const geminiServers = {}
|
|
2308
|
+
for (const [k, v] of Object.entries(mcpServers)) {
|
|
2309
|
+
if (v.type === "http" && v.url) {
|
|
2310
|
+
geminiServers[k] = { httpUrl: v.url, ...(v.headers ? { headers: v.headers } : {}) }
|
|
2311
|
+
} else if (v.command) {
|
|
2312
|
+
geminiServers[k] = { command: v.command, ...(v.args ? { args: v.args } : {}), ...(v.env ? { env: v.env } : {}) }
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
|
|
2316
|
+
fs.writeFileSync(settingsPath, JSON.stringify({ ...existing, mcpServers: geminiServers }, null, 2))
|
|
2317
|
+
|
|
2318
|
+
return backup
|
|
2319
|
+
}
|
|
2320
|
+
|
|
2321
|
+
function injectCursorMcp(mcpKey, userMcpServers) {
|
|
2322
|
+
const cursorConfigDir = path.join(os.homedir(), ".cursor")
|
|
2323
|
+
ensureDir(cursorConfigDir)
|
|
2324
|
+
const mcpPath = path.join(cursorConfigDir, "mcp.json")
|
|
2325
|
+
|
|
2326
|
+
const backup = fs.existsSync(mcpPath) ? fs.readFileSync(mcpPath, "utf8") : null
|
|
2327
|
+
|
|
2328
|
+
const mcpServers = buildJsonMcpServers(mcpKey, userMcpServers)
|
|
2329
|
+
const cursorServers = {}
|
|
2330
|
+
for (const [k, v] of Object.entries(mcpServers)) {
|
|
2331
|
+
if (v.type === "http" && v.url) {
|
|
2332
|
+
cursorServers[k] = { url: v.url, ...(v.headers ? { headers: v.headers } : {}) }
|
|
2333
|
+
} else if (v.command) {
|
|
2334
|
+
cursorServers[k] = { command: v.command, ...(v.args ? { args: v.args } : {}), ...(v.env ? { env: v.env } : {}) }
|
|
2335
|
+
}
|
|
2336
|
+
}
|
|
2337
|
+
|
|
2338
|
+
fs.writeFileSync(mcpPath, JSON.stringify({ mcpServers: cursorServers }, null, 2))
|
|
2339
|
+
|
|
2340
|
+
return backup
|
|
2341
|
+
}
|
|
2342
|
+
|
|
2343
|
+
function buildExecutorEnv(run, worktreePath, mcpKey, userMcpServers) {
|
|
2344
|
+
// The repository's services and secrets first, so a run's own credentials
|
|
2345
|
+
// below always win over a secret with the same name.
|
|
2346
|
+
const env = { ...process.env, ...(run.__extraEnv || {}) }
|
|
2347
|
+
|
|
2348
|
+
const credentials = runCredentials(run)
|
|
2349
|
+
if (activeExecutor() === "codex" && mcpKey) {
|
|
2350
|
+
const requestedModel = resolveRequestedModel(run)
|
|
2351
|
+
const openAiKey = credentials?.provider === "openai" ? credentials.key : null
|
|
2352
|
+
env.CODEX_HOME = ensureIsolatedCodexHome(run.id, worktreePath, mcpKey, requestedModel, userMcpServers, openAiKey)
|
|
2353
|
+
if (openAiKey) env.OPENAI_API_KEY = openAiKey
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
if (activeExecutor() === "claude_code" && credentials) {
|
|
2357
|
+
// Claude Code uses these over the machine's Claude login. Clear all three
|
|
2358
|
+
// first so a key set in the runner's own environment cannot leak in.
|
|
2359
|
+
delete env.ANTHROPIC_API_KEY
|
|
2360
|
+
delete env.ANTHROPIC_AUTH_TOKEN
|
|
2361
|
+
delete env.ANTHROPIC_BASE_URL
|
|
2362
|
+
delete env.CLAUDE_CODE_OAUTH_TOKEN
|
|
2363
|
+
if (credentials.provider === "anthropic") {
|
|
2364
|
+
// Read by the apiKeyHelper in claudeRunSettings.
|
|
2365
|
+
env.PACEWORK_RUN_API_KEY = credentials.key
|
|
2366
|
+
} else if (credentials.provider === "claude_subscription") {
|
|
2367
|
+
env.CLAUDE_CODE_OAUTH_TOKEN = credentials.key
|
|
2368
|
+
// A Claude login on the machine beats the token, which would bill the
|
|
2369
|
+
// machine's own plan instead. An empty config dir has no login to find.
|
|
2370
|
+
// (In GitHub Actions there is none anyway.)
|
|
2371
|
+
env.CLAUDE_CONFIG_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "pacework-claude-"))
|
|
2372
|
+
} else {
|
|
2373
|
+
env.ANTHROPIC_BASE_URL = credentials.base_url || API_PROVIDERS[credentials.provider].baseUrl
|
|
2374
|
+
env.ANTHROPIC_AUTH_TOKEN = credentials.key
|
|
2375
|
+
// Claude Code also calls a small model for background work; it must be
|
|
2376
|
+
// one this provider serves.
|
|
2377
|
+
const model = resolveRequestedModel(run)
|
|
2378
|
+
if (model) {
|
|
2379
|
+
env.ANTHROPIC_MODEL = model
|
|
2380
|
+
env.ANTHROPIC_SMALL_FAST_MODEL = model
|
|
2381
|
+
env.ANTHROPIC_DEFAULT_HAIKU_MODEL = model
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
}
|
|
2385
|
+
|
|
2386
|
+
if (activeExecutor() === "claude_code" && mcpKey) {
|
|
2387
|
+
const claudeHome = injectClaudeCodeMcp(mcpKey, userMcpServers)
|
|
2388
|
+
env.CLAUDE_HOME = claudeHome
|
|
2389
|
+
}
|
|
2390
|
+
|
|
2391
|
+
return env
|
|
2392
|
+
}
|
|
2393
|
+
|
|
2394
|
+
function cleanupExecutorMcp(backups) {
|
|
2395
|
+
if (!backups) return
|
|
2396
|
+
if (backups.geminiSettingsPath && backups.geminiBackup !== undefined) {
|
|
2397
|
+
try {
|
|
2398
|
+
if (backups.geminiBackup === null) {
|
|
2399
|
+
fs.rmSync(backups.geminiSettingsPath, { force: true })
|
|
2400
|
+
} else {
|
|
2401
|
+
fs.writeFileSync(backups.geminiSettingsPath, backups.geminiBackup)
|
|
2402
|
+
}
|
|
2403
|
+
} catch {}
|
|
2404
|
+
}
|
|
2405
|
+
if (backups.cursorMcpPath && backups.cursorBackup !== undefined) {
|
|
2406
|
+
try {
|
|
2407
|
+
if (backups.cursorBackup === null) {
|
|
2408
|
+
fs.rmSync(backups.cursorMcpPath, { force: true })
|
|
2409
|
+
} else {
|
|
2410
|
+
fs.writeFileSync(backups.cursorMcpPath, backups.cursorBackup)
|
|
2411
|
+
}
|
|
2412
|
+
} catch {}
|
|
2413
|
+
}
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
function setupExecutorMcpSideEffects(mcpKey, userMcpServers) {
|
|
2417
|
+
const backups = {}
|
|
2418
|
+
|
|
2419
|
+
if (activeExecutor() === "gemini" && mcpKey) {
|
|
2420
|
+
const geminiSettingsPath = path.join(os.homedir(), ".gemini", "settings.json")
|
|
2421
|
+
backups.geminiSettingsPath = geminiSettingsPath
|
|
2422
|
+
backups.geminiBackup = injectGeminiMcp(mcpKey, userMcpServers)
|
|
2423
|
+
}
|
|
2424
|
+
|
|
2425
|
+
if (activeExecutor() === "cursor_agent" && mcpKey) {
|
|
2426
|
+
const cursorMcpPath = path.join(os.homedir(), ".cursor", "mcp.json")
|
|
2427
|
+
backups.cursorMcpPath = cursorMcpPath
|
|
2428
|
+
backups.cursorBackup = injectCursorMcp(mcpKey, userMcpServers)
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
return backups
|
|
2432
|
+
}
|
|
2433
|
+
|
|
2434
|
+
async function runSmokeTestExecution(run, state, context) {
|
|
2435
|
+
if (isReviewRun(run)) {
|
|
2436
|
+
const reviewBody = [
|
|
2437
|
+
"**Summary**: Smoke-test review completed locally. The implementation run posted a structured summary and verification output.",
|
|
2438
|
+
"**Approved**",
|
|
2439
|
+
"**Findings**: none",
|
|
2440
|
+
"**Suggestions**: none",
|
|
2441
|
+
].join("\n")
|
|
2442
|
+
|
|
2443
|
+
await callMcpTool(
|
|
2444
|
+
runMcpKey(run, state),
|
|
2445
|
+
"add_comment",
|
|
2446
|
+
{
|
|
2447
|
+
issue_id: run.task_id,
|
|
2448
|
+
content: reviewBody,
|
|
2449
|
+
...(context.activityThreadCommentId ? { parent_id: context.activityThreadCommentId } : {}),
|
|
2450
|
+
},
|
|
2451
|
+
run.agent_id,
|
|
2452
|
+
)
|
|
2453
|
+
|
|
2454
|
+
await callMcpTool(
|
|
2455
|
+
runMcpKey(run, state),
|
|
2456
|
+
"submit_issue_review",
|
|
2457
|
+
{
|
|
2458
|
+
issue_id: run.task_id,
|
|
2459
|
+
decision: "approve",
|
|
2460
|
+
summary: "Smoke-test reviewer approved.",
|
|
2461
|
+
},
|
|
2462
|
+
run.agent_id,
|
|
2463
|
+
)
|
|
2464
|
+
|
|
2465
|
+
return {
|
|
2466
|
+
summary: [
|
|
2467
|
+
"# Summary",
|
|
2468
|
+
"Completed the deterministic reviewer smoke test and recorded an approval.",
|
|
2469
|
+
"",
|
|
2470
|
+
"# Verification",
|
|
2471
|
+
"- Submitted a structured review comment.",
|
|
2472
|
+
"- Recorded an `approve` decision via `submit_issue_review`.",
|
|
2473
|
+
"",
|
|
2474
|
+
"# Remaining gaps",
|
|
2475
|
+
"- None for the deterministic reviewer smoke path.",
|
|
2476
|
+
].join("\n"),
|
|
2477
|
+
verificationSummary: "- PASS [smoke] Reviewer approval submitted through MCP.",
|
|
2478
|
+
}
|
|
2479
|
+
}
|
|
2480
|
+
|
|
2481
|
+
return {
|
|
2482
|
+
summary: [
|
|
2483
|
+
"# Summary",
|
|
2484
|
+
"Completed the deterministic engineering smoke test without modifying repository files.",
|
|
2485
|
+
"",
|
|
2486
|
+
"# Verification",
|
|
2487
|
+
"- Claimed the issue through MCP.",
|
|
2488
|
+
"- Prepared a local worktree and branch.",
|
|
2489
|
+
"- Produced the structured summary required by the runner completion path.",
|
|
2490
|
+
"",
|
|
2491
|
+
"# Remaining gaps",
|
|
2492
|
+
"- Review and closure continue through the configured automation policy.",
|
|
2493
|
+
].join("\n"),
|
|
2494
|
+
verificationSummary: "- PASS [smoke] Local runner smoke path completed without file edits.",
|
|
2495
|
+
}
|
|
2496
|
+
}
|
|
2497
|
+
|
|
2498
|
+
async function runPlannerPromptWithOllama(prompt, config) {
|
|
2499
|
+
const baseUrl = (config.baseUrl || "http://127.0.0.1:11434").replace(/\/+$/, "")
|
|
2500
|
+
const response = await fetch(`${baseUrl}/api/chat`, {
|
|
2501
|
+
method: "POST",
|
|
2502
|
+
headers: { "Content-Type": "application/json" },
|
|
2503
|
+
body: JSON.stringify({
|
|
2504
|
+
model: config.model,
|
|
2505
|
+
stream: false,
|
|
2506
|
+
messages: [{ role: "user", content: prompt }],
|
|
2507
|
+
}),
|
|
2508
|
+
})
|
|
2509
|
+
if (!response.ok) {
|
|
2510
|
+
const text = await response.text().catch(() => "")
|
|
2511
|
+
throw new Error(`Ollama returned ${response.status}${text ? `: ${text}` : ""}`)
|
|
2512
|
+
}
|
|
2513
|
+
const payload = await response.json().catch(() => null)
|
|
2514
|
+
const content = payload?.message?.content?.trim() ?? ""
|
|
2515
|
+
if (!content) {
|
|
2516
|
+
throw new Error(payload?.error?.trim() || "Ollama returned an empty planner response.")
|
|
2517
|
+
}
|
|
2518
|
+
return content
|
|
2519
|
+
}
|
|
2520
|
+
|
|
2521
|
+
async function runPlannerExecutor(run, state) {
|
|
2522
|
+
const runLogger = getRunLogger(state, run)
|
|
2523
|
+
const runRoot = path.join(repoRoot, ".agent", "runs", run.id)
|
|
2524
|
+
ensureDir(runRoot)
|
|
2525
|
+
|
|
2526
|
+
const outputFile = path.join(runRoot, "planner-last-message.txt")
|
|
2527
|
+
const logState = { tail: "" }
|
|
2528
|
+
const startedAt = Date.now()
|
|
2529
|
+
let stdioSummary = ""
|
|
2530
|
+
let cancelRequested = false
|
|
2531
|
+
let childProcess = null
|
|
2532
|
+
let lastHeartbeatLogAt = 0
|
|
2533
|
+
let plannerMcpBackups = null
|
|
2534
|
+
let plannerUsage = null
|
|
2535
|
+
|
|
2536
|
+
function appendHeartbeat(force = false) {
|
|
2537
|
+
if (!childProcess || childProcess.killed) return
|
|
2538
|
+
const now = Date.now()
|
|
2539
|
+
if (!force && now - lastHeartbeatLogAt < 30000) return
|
|
2540
|
+
lastHeartbeatLogAt = now
|
|
2541
|
+
appendLogTail(
|
|
2542
|
+
logState,
|
|
2543
|
+
`> still running (${formatElapsed(now - startedAt)} elapsed); pid ${childProcess.pid ?? "unknown"}; waiting for ${activeExecutor()} output...`,
|
|
2544
|
+
)
|
|
2545
|
+
}
|
|
2546
|
+
|
|
2547
|
+
async function flushPlannerLog() {
|
|
2548
|
+
appendHeartbeat()
|
|
2549
|
+
const response = await postPlannerLog(run, logState.tail)
|
|
2550
|
+
if (response?.cancel_requested) {
|
|
2551
|
+
cancelRequested = true
|
|
2552
|
+
if (childProcess && !childProcess.killed) {
|
|
2553
|
+
appendLogTail(logState, "> cancel requested; terminating executor process...")
|
|
2554
|
+
requestChildProcessShutdown(childProcess)
|
|
2555
|
+
}
|
|
2556
|
+
}
|
|
2557
|
+
}
|
|
2558
|
+
|
|
2559
|
+
const logInterval = setInterval(() => {
|
|
2560
|
+
flushPlannerLog().catch(() => {})
|
|
2561
|
+
}, 5000)
|
|
2562
|
+
|
|
2563
|
+
let runTimeoutHandle = null
|
|
2564
|
+
const runTimeoutPromise = new Promise((_, reject) => {
|
|
2565
|
+
runTimeoutHandle = setTimeout(() => {
|
|
2566
|
+
if (childProcess && !childProcess.killed) killChildProcessTree(childProcess, "SIGKILL")
|
|
2567
|
+
reject(new Error(`Planner run timed out after ${Math.round(RUN_TIMEOUT_MS / 60000)} minutes`))
|
|
2568
|
+
}, RUN_TIMEOUT_MS)
|
|
2569
|
+
})
|
|
2570
|
+
|
|
2571
|
+
try {
|
|
2572
|
+
const problem = credentialProblem(run)
|
|
2573
|
+
if (problem) throw new Error(`Could not start: ${problem}`)
|
|
2574
|
+
runLogger.info("runner.planner.started", "Local runner planner execution started", {
|
|
2575
|
+
meta: {
|
|
2576
|
+
issue_identifier: run.task?.identifier || run.task_id,
|
|
2577
|
+
},
|
|
2578
|
+
})
|
|
2579
|
+
appendLogTail(logState, `> preparing ${run.agent?.name || "planner"} run in ${repoRoot}`)
|
|
2580
|
+
await flushPlannerLog().catch(() => {})
|
|
2581
|
+
await postIssueMilestone(
|
|
2582
|
+
runMcpKey(run, state),
|
|
2583
|
+
run.task_id,
|
|
2584
|
+
"Planning started on the local runner.",
|
|
2585
|
+
"info",
|
|
2586
|
+
)
|
|
2587
|
+
|
|
2588
|
+
appendLogTail(logState, "> loading workspace context and planner prompt...")
|
|
2589
|
+
await flushPlannerLog().catch(() => {})
|
|
2590
|
+
const workspaceContext = await buildWorkspaceContext(repoRoot)
|
|
2591
|
+
const plannerPayload = await preparePlannerRun(run, workspaceContext)
|
|
2592
|
+
const staticResponse = typeof plannerPayload?.staticResponse === "string" ? plannerPayload.staticResponse.trim() : ""
|
|
2593
|
+
const answers = clarificationAnswers(run)
|
|
2594
|
+
const prompt = typeof plannerPayload?.prompt === "string" && plannerPayload.prompt
|
|
2595
|
+
? `${plannerPayload.prompt}\n\n${answers || ASK_WHEN_UNCLEAR}`
|
|
2596
|
+
: ""
|
|
2597
|
+
|
|
2598
|
+
if (!staticResponse && !prompt) {
|
|
2599
|
+
throw new Error("Planner prepare response did not include prompt or static response")
|
|
2600
|
+
}
|
|
2601
|
+
|
|
2602
|
+
const ollamaConfig = plannerPayload?.ollama_config || null
|
|
2603
|
+
const useOllama = ollamaConfig && typeof ollamaConfig.model === "string" && ollamaConfig.model.trim()
|
|
2604
|
+
|
|
2605
|
+
if (staticResponse) {
|
|
2606
|
+
stdioSummary = staticResponse
|
|
2607
|
+
appendLogTail(logState, "> using static planner response.")
|
|
2608
|
+
appendLogTail(logState, staticResponse)
|
|
2609
|
+
} else if (useOllama) {
|
|
2610
|
+
runLogger.info("runner.planner.ollama", "Using Ollama for planner run", {
|
|
2611
|
+
meta: {
|
|
2612
|
+
model: ollamaConfig.model,
|
|
2613
|
+
base_url: ollamaConfig.base_url || "http://127.0.0.1:11434",
|
|
2614
|
+
},
|
|
2615
|
+
})
|
|
2616
|
+
appendLogTail(logState, `> running planner with Ollama model ${ollamaConfig.model}`)
|
|
2617
|
+
await flushPlannerLog().catch(() => {})
|
|
2618
|
+
const ollamaResponse = await runPlannerPromptWithOllama(prompt, {
|
|
2619
|
+
model: ollamaConfig.model,
|
|
2620
|
+
baseUrl: ollamaConfig.base_url || "http://127.0.0.1:11434",
|
|
2621
|
+
})
|
|
2622
|
+
stdioSummary = ollamaResponse
|
|
2623
|
+
appendLogTail(logState, ollamaResponse)
|
|
2624
|
+
} else {
|
|
2625
|
+
const plannerMcpKey = runMcpKey(run, state)
|
|
2626
|
+
const execSpec = buildExecutorCommand(run, repoRoot, prompt, outputFile, plannerMcpKey)
|
|
2627
|
+
appendLogTail(logState, `> spawning ${activeExecutor()} planner executor`)
|
|
2628
|
+
appendLogTail(logState, `> cwd: ${repoRoot}`)
|
|
2629
|
+
appendLogTail(logState, `> command: ${previewExecutorCommand(execSpec)}`)
|
|
2630
|
+
await flushPlannerLog().catch(() => {})
|
|
2631
|
+
const plannerMcpServers = await fetchMcpServers(state)
|
|
2632
|
+
const execEnv = buildExecutorEnv(run, repoRoot, plannerMcpKey, plannerMcpServers)
|
|
2633
|
+
plannerMcpBackups = setupExecutorMcpSideEffects(plannerMcpKey, plannerMcpServers)
|
|
2634
|
+
|
|
2635
|
+
let plannerStdout = ""
|
|
2636
|
+
await Promise.race([runTimeoutPromise, new Promise((resolve, reject) => {
|
|
2637
|
+
const child = spawn(execSpec.command, execSpec.args, {
|
|
2638
|
+
cwd: repoRoot,
|
|
2639
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
2640
|
+
detached: process.platform !== "win32",
|
|
2641
|
+
env: execEnv,
|
|
2642
|
+
})
|
|
2643
|
+
childProcess = child
|
|
2644
|
+
appendLogTail(logState, `> child pid: ${child.pid ?? "unknown"}`)
|
|
2645
|
+
appendHeartbeat(true)
|
|
2646
|
+
flushPlannerLog().catch(() => {})
|
|
2647
|
+
|
|
2648
|
+
if (execSpec.stdin) {
|
|
2649
|
+
child.stdin.write(execSpec.stdin)
|
|
2650
|
+
}
|
|
2651
|
+
child.stdin.end()
|
|
2652
|
+
|
|
2653
|
+
child.stdout.on("data", chunk => {
|
|
2654
|
+
const text = chunk.toString()
|
|
2655
|
+
if (execSpec.summaryMode === "claude_json") plannerStdout = truncate(`${plannerStdout}${text}`, 2_000_000)
|
|
2656
|
+
appendLogTail(logState, text)
|
|
2657
|
+
stdioSummary = truncate(`${stdioSummary}\n${text}`, 16000)
|
|
2658
|
+
})
|
|
2659
|
+
child.stderr.on("data", chunk => {
|
|
2660
|
+
const text = chunk.toString()
|
|
2661
|
+
appendLogTail(logState, text)
|
|
2662
|
+
stdioSummary = truncate(`${stdioSummary}\n${text}`, 16000)
|
|
2663
|
+
})
|
|
2664
|
+
child.on("error", error => {
|
|
2665
|
+
appendLogTail(logState, `> executor spawn error: ${error instanceof Error ? error.message : String(error)}`)
|
|
2666
|
+
reject(error)
|
|
2667
|
+
})
|
|
2668
|
+
child.on("close", code => {
|
|
2669
|
+
appendLogTail(logState, `> executor exited with code ${code}`)
|
|
2670
|
+
// Same JSON result as a build run; the planner reads its message.
|
|
2671
|
+
const parsed = execSpec.summaryMode === "claude_json" ? parseClaudeJsonResult(plannerStdout) : null
|
|
2672
|
+
if (parsed) {
|
|
2673
|
+
stdioSummary = parsed.text
|
|
2674
|
+
// Planning is paid work too; budgets count it.
|
|
2675
|
+
plannerUsage = usesThirdPartyModels(run) ? { ...parsed.usage, cost_usd: null } : parsed.usage
|
|
2676
|
+
}
|
|
2677
|
+
if (code === 0) {
|
|
2678
|
+
resolve()
|
|
2679
|
+
return
|
|
2680
|
+
}
|
|
2681
|
+
if (cancelRequested) {
|
|
2682
|
+
reject(new Error("Canceled by admin."))
|
|
2683
|
+
return
|
|
2684
|
+
}
|
|
2685
|
+
reject(new Error(formatChildExitError(activeExecutor(), code, stdioSummary)))
|
|
2686
|
+
})
|
|
2687
|
+
})])
|
|
2688
|
+
|
|
2689
|
+
cleanupExecutorMcp(plannerMcpBackups)
|
|
2690
|
+
plannerMcpBackups = null
|
|
2691
|
+
}
|
|
2692
|
+
|
|
2693
|
+
await flushPlannerLog().catch(() => {})
|
|
2694
|
+
|
|
2695
|
+
const fileContent = fs.existsSync(outputFile) ? fs.readFileSync(outputFile, "utf8").trim() : ""
|
|
2696
|
+
const plannerResponse = staticResponse || fileContent || stdioSummary.trim()
|
|
2697
|
+
|
|
2698
|
+
if (!plannerResponse) {
|
|
2699
|
+
throw new Error("Planner run finished without a final response")
|
|
2700
|
+
}
|
|
2701
|
+
|
|
2702
|
+
await completePlannerRun(run, {
|
|
2703
|
+
status: "completed",
|
|
2704
|
+
planner_response: plannerResponse,
|
|
2705
|
+
usage: plannerUsage,
|
|
2706
|
+
}, workspaceContext)
|
|
2707
|
+
await postIssueMilestone(
|
|
2708
|
+
runMcpKey(run, state),
|
|
2709
|
+
run.task_id,
|
|
2710
|
+
"Planning finished and results were posted back to the issue.",
|
|
2711
|
+
"success",
|
|
2712
|
+
)
|
|
2713
|
+
|
|
2714
|
+
runLogger.info("runner.planner.completed", "Local runner planner execution completed")
|
|
2715
|
+
return { summary: plannerResponse }
|
|
2716
|
+
} catch (error) {
|
|
2717
|
+
await flushPlannerLog().catch(() => {})
|
|
2718
|
+
const message = cancelRequested ? "Canceled by admin." : (error instanceof Error ? error.message : String(error))
|
|
2719
|
+
|
|
2720
|
+
await completePlannerRun(run, {
|
|
2721
|
+
status: cancelRequested ? "canceled" : "error",
|
|
2722
|
+
error_message: truncate(message, 4000),
|
|
2723
|
+
}).catch(() => {})
|
|
2724
|
+
await postIssueMilestone(
|
|
2725
|
+
runMcpKey(run, state),
|
|
2726
|
+
run.task_id,
|
|
2727
|
+
`Planning ${cancelRequested ? "was canceled" : "failed"}: ${truncate(message.replace(/\s+/g, " "), 280)}`,
|
|
2728
|
+
"warning",
|
|
2729
|
+
)
|
|
2730
|
+
|
|
2731
|
+
runLogger.error("runner.planner.failed", "Local runner planner execution failed", {
|
|
2732
|
+
meta: {
|
|
2733
|
+
canceled: cancelRequested,
|
|
2734
|
+
},
|
|
2735
|
+
}, error)
|
|
2736
|
+
throw error
|
|
2737
|
+
} finally {
|
|
2738
|
+
clearInterval(logInterval)
|
|
2739
|
+
if (runTimeoutHandle) clearTimeout(runTimeoutHandle)
|
|
2740
|
+
cleanupExecutorMcp(plannerMcpBackups)
|
|
2741
|
+
}
|
|
2742
|
+
}
|
|
2743
|
+
|
|
2744
|
+
const LOCAL_MERGE_POLL_INTERVAL_MS = 30_000
|
|
2745
|
+
const LOCAL_MERGE_WAIT_MS = 24 * 60 * 60_000
|
|
2746
|
+
|
|
2747
|
+
/**
|
|
2748
|
+
* Merge a run's branch into the local default branch once its issue is done —
|
|
2749
|
+
* i.e. after review and any human sign-off. Runs outside the run's slot.
|
|
2750
|
+
*/
|
|
2751
|
+
async function mergeToMainWhenApproved({ run, state, branch, worktreePath, runLogger }) {
|
|
2752
|
+
const target = resolveMergeApprovalTarget(run, branch)
|
|
2753
|
+
runLogger.info("runner.run.awaiting_approval", "Waiting for issue approval before merging to main", {
|
|
2754
|
+
meta: { approval_task_id: target.taskId, approval_issue: target.label },
|
|
2755
|
+
})
|
|
2756
|
+
|
|
2757
|
+
const startedAt = Date.now()
|
|
2758
|
+
while (Date.now() - startedAt < LOCAL_MERGE_WAIT_MS) {
|
|
2759
|
+
await new Promise((resolve) => setTimeout(resolve, LOCAL_MERGE_POLL_INTERVAL_MS))
|
|
2760
|
+
let status = null
|
|
2761
|
+
try {
|
|
2762
|
+
const response = await requestJson(
|
|
2763
|
+
`${appUrl}/api/agent-runners/tasks/${target.taskId}/status?workspace_id=${encodeURIComponent(workspaceId)}`,
|
|
2764
|
+
{ method: "GET", headers: { Authorization: `Bearer ${state.runner_secret}` } },
|
|
2765
|
+
)
|
|
2766
|
+
status = response?.status ?? null
|
|
2767
|
+
} catch (error) {
|
|
2768
|
+
runLogger.warn("runner.run.poll_status.failed", "Task status poll failed", { meta: { error: String(error) } })
|
|
2769
|
+
continue
|
|
2770
|
+
}
|
|
2771
|
+
|
|
2772
|
+
if (status === "canceled") {
|
|
2773
|
+
runLogger.info("runner.run.merge_skipped", "Task canceled — skipping merge")
|
|
2774
|
+
return
|
|
2775
|
+
}
|
|
2776
|
+
if (status !== "done") continue
|
|
2777
|
+
|
|
2778
|
+
if (!(await branchExists(branch))) {
|
|
2779
|
+
runLogger.info("runner.run.merge_skipped", "Feature branch no longer exists locally, assuming it was already merged or cleaned up", {
|
|
2780
|
+
meta: { branch },
|
|
2781
|
+
})
|
|
2782
|
+
return
|
|
2783
|
+
}
|
|
2784
|
+
const result = await mergeFeatureBranchToMain(repoRoot, worktreePath, branch, run.task?.title, runLogger)
|
|
2785
|
+
runLogger.info("runner.run.merged_to_main", "Merged the approved branch into main", {
|
|
2786
|
+
meta: { branch, merged: Boolean(result?.merged), default_branch: result?.defaultBranch ?? null },
|
|
2787
|
+
})
|
|
2788
|
+
return
|
|
2789
|
+
}
|
|
2790
|
+
|
|
2791
|
+
runLogger.warn("runner.run.merge_timeout", "Issue was not approved within a day — merge skipped")
|
|
2792
|
+
}
|
|
2793
|
+
|
|
2794
|
+
/** Why this run's key or token cannot be used here, or null when it can (or it has none). */
|
|
2795
|
+
function credentialProblem(run) {
|
|
2796
|
+
const providerExecutor = API_PROVIDERS[run.api_credentials?.provider]?.executor
|
|
2797
|
+
const mismatched = runCredentials(run) && providerExecutor !== activeExecutor()
|
|
2798
|
+
if (!run.api_credentials?.error && !(run.api_credentials && !runCredentials(run)) && !mismatched) return null
|
|
2799
|
+
return run.api_credentials.error
|
|
2800
|
+
|| (mismatched
|
|
2801
|
+
? `This agent's ${run.api_credentials.provider} key needs ${executorDisplayName(providerExecutor)}, but the run is set up for ${executorDisplayName()}. Re-save the agent, or install ${executorDisplayName(providerExecutor)} on this machine.`
|
|
2802
|
+
: `This agent's API provider (${run.api_credentials.provider}) is not supported by this runner. Update the runner.`)
|
|
2803
|
+
}
|
|
2804
|
+
|
|
2805
|
+
/** Answers the issue's owner gave to an agent's questions, for any later run's prompt. */
|
|
2806
|
+
function clarificationAnswers(run) {
|
|
2807
|
+
const answered = Array.isArray(run?.clarifications) ? run.clarifications : []
|
|
2808
|
+
if (answered.length === 0) return ""
|
|
2809
|
+
return `Answers from the issue owner (they decide; follow them over your own assumptions):
|
|
2810
|
+
${answered.map((c) => `- Q: ${String(c.question || "").replace(/\s+/g, " ")}\n A: ${String(c.answer || "").replace(/\s+/g, " ")}`).join("\n")}`
|
|
2811
|
+
}
|
|
2812
|
+
|
|
2813
|
+
/** When to ask instead of guessing. Builders and planners get it; reviewers judge what exists. */
|
|
2814
|
+
const ASK_WHEN_UNCLEAR = `If the issue is unclear in a way that changes what you would build — two reasonable
|
|
2815
|
+
readings that lead to different results — and neither the code nor the issue settles it,
|
|
2816
|
+
do not guess. Call \`request_clarification\` with one concrete question per call (1–3 in
|
|
2817
|
+
total, each with options when there are clear choices), then stop without making changes
|
|
2818
|
+
and say you are waiting for answers. pacework pauses the issue, asks its owner, and runs
|
|
2819
|
+
this step again with the answers. Do not ask about things you can decide or check yourself.`
|
|
2820
|
+
|
|
2821
|
+
async function runExecutor(run, state) {
|
|
2822
|
+
const runLogger = getRunLogger(state, run)
|
|
2823
|
+
|
|
2824
|
+
const problem = credentialProblem(run)
|
|
2825
|
+
if (problem) {
|
|
2826
|
+
const message = problem
|
|
2827
|
+
await postIssueMilestone(runMcpKey(run, state), run.task_id, `Could not start: ${message}`, "warning").catch(() => {})
|
|
2828
|
+
await completeRun(state, run.id, { status: "error", error_message: message }).catch(() => {})
|
|
2829
|
+
return
|
|
2830
|
+
}
|
|
2831
|
+
|
|
2832
|
+
// Resolve the repo this ticket belongs to before preparing a checkout, so the
|
|
2833
|
+
// code is written in the same repo the PR will be opened against.
|
|
2834
|
+
try {
|
|
2835
|
+
const { token, repo } = await fetchGithubToken(
|
|
2836
|
+
state,
|
|
2837
|
+
run.workspace_id,
|
|
2838
|
+
run.task?.project_id ?? null,
|
|
2839
|
+
run.task?.team_id ?? null,
|
|
2840
|
+
run.task_id ?? null,
|
|
2841
|
+
)
|
|
2842
|
+
run.__githubToken = token ?? null
|
|
2843
|
+
run.__repo = repo ?? null
|
|
2844
|
+
run.__repoRoot = await resolveRunRepoRoot(run, state)
|
|
2845
|
+
if (run.__repoRoot !== repoRoot) {
|
|
2846
|
+
runLogger.info("runner.repo.resolved", "Working in the ticket's repository", {
|
|
2847
|
+
meta: { repo: run.__repo ? `${run.__repo.owner}/${run.__repo.repo}` : null, path: run.__repoRoot },
|
|
2848
|
+
})
|
|
2849
|
+
}
|
|
2850
|
+
} catch (error) {
|
|
2851
|
+
runLogger.warn(
|
|
2852
|
+
"runner.repo.resolve_failed",
|
|
2853
|
+
"Could not resolve the ticket's repository; using the configured repo root",
|
|
2854
|
+
undefined,
|
|
2855
|
+
error,
|
|
2856
|
+
)
|
|
2857
|
+
run.__repoRoot = repoRoot
|
|
2858
|
+
}
|
|
2859
|
+
|
|
2860
|
+
// Ask the server to claim the issue and name the branch. The runner used to
|
|
2861
|
+
// invent its own name (codex/plt-9) while the server recorded a different one
|
|
2862
|
+
// (agent/PLT-9-...), so merge_pr could never verify the PR head and agent
|
|
2863
|
+
// merging was impossible. The server's name is authoritative.
|
|
2864
|
+
if (!isReviewRun(run)) {
|
|
2865
|
+
const startMcpKey = runMcpKey(run, state)
|
|
2866
|
+
try {
|
|
2867
|
+
const started = await callMcpTool(
|
|
2868
|
+
startMcpKey,
|
|
2869
|
+
"start_work",
|
|
2870
|
+
{ issue_id: run.task_id, workspace_id: run.workspace_id },
|
|
2871
|
+
run.agent_id,
|
|
2872
|
+
)
|
|
2873
|
+
if (started?.branch) {
|
|
2874
|
+
run.__serverBranch = started.branch
|
|
2875
|
+
runLogger.info("runner.run.start_work", "Server assigned the working branch", {
|
|
2876
|
+
meta: { branch: started.branch, base: started.base, repo: started.repo },
|
|
2877
|
+
})
|
|
2878
|
+
}
|
|
2879
|
+
} catch (error) {
|
|
2880
|
+
// A missing GitHub integration or permission should not block the run;
|
|
2881
|
+
// fall back to locally derived naming.
|
|
2882
|
+
runLogger.warn(
|
|
2883
|
+
"runner.run.start_work_failed",
|
|
2884
|
+
"start_work failed; falling back to a locally derived branch name",
|
|
2885
|
+
undefined,
|
|
2886
|
+
error,
|
|
2887
|
+
)
|
|
2888
|
+
}
|
|
2889
|
+
}
|
|
2890
|
+
|
|
2891
|
+
runLogger.info("runner.run.prepare", "Preparing worktree")
|
|
2892
|
+
const { runRoot, worktreePath, branch, localCheckoutMode, gitAvailable } = await prepareWorktree(run)
|
|
2893
|
+
const prepared = await prepareRunEnvironment({
|
|
2894
|
+
environment: run.environment,
|
|
2895
|
+
checkoutPath: worktreePath,
|
|
2896
|
+
isCloud: Boolean(cloudState),
|
|
2897
|
+
secrets: cloudAgentSecrets,
|
|
2898
|
+
log: (line) => runLogger.info("runner.run.environment", line),
|
|
2899
|
+
})
|
|
2900
|
+
run.__extraEnv = prepared.env
|
|
2901
|
+
run.__environmentNote = prepared.note
|
|
2902
|
+
runLogger.info("runner.run.prepare", "Scanning workspace", { meta: { worktree_path: worktreePath } })
|
|
2903
|
+
const workspaceContext = await buildWorkspaceContext(worktreePath)
|
|
2904
|
+
runLogger.info("runner.run.prepare", "Building prompt")
|
|
2905
|
+
const outputFile = path.join(runRoot, "last-message.txt")
|
|
2906
|
+
const artifactsFile = path.join(runRoot, "artifacts.json")
|
|
2907
|
+
const reportFile = path.join(runRoot, "report.md")
|
|
2908
|
+
const runnerProfile = loadRunnerProfile()
|
|
2909
|
+
// Use persistent runner MCP key if available (avoids per-run key creation overhead)
|
|
2910
|
+
const mcpKey = runMcpKey(run, state)
|
|
2911
|
+
if (isReviewRun(run)) {
|
|
2912
|
+
// The pull request as GitHub has it is what gets merged. Hand it to the
|
|
2913
|
+
// reviewer up front: left to choose, reviewers diffed against a stale
|
|
2914
|
+
// local main and flagged work that had already merged.
|
|
2915
|
+
const prDiff = await callMcpTool(mcpKey, "get_pr_diff", { issue_id: run.task_id, workspace_id: run.workspace_id }, run.agent_id)
|
|
2916
|
+
.catch(() => null)
|
|
2917
|
+
if (typeof prDiff?.diff === "string" && prDiff.diff.trim()) {
|
|
2918
|
+
run.reviewer_context = { ...(run.reviewer_context || {}), pr_diff: prDiff.diff, pr_diff_truncated: prDiff.truncated === true }
|
|
2919
|
+
}
|
|
2920
|
+
}
|
|
2921
|
+
const prompt = buildPrompt(run, branch, worktreePath, { localCheckoutMode, gitAvailable, workspaceContext })
|
|
2922
|
+
const execSpec = buildExecutorCommand(run, worktreePath, prompt, outputFile, mcpKey)
|
|
2923
|
+
// These steps run before the executor spawns; when one stalls the run sits in
|
|
2924
|
+
// "running" with no output, so each is traced.
|
|
2925
|
+
runLogger.info("runner.run.prepare", "Fetching MCP servers")
|
|
2926
|
+
const userMcpServers = await fetchMcpServers(state)
|
|
2927
|
+
runLogger.info("runner.run.prepare", "Building executor environment", {
|
|
2928
|
+
meta: { mcp_server_count: userMcpServers.length },
|
|
2929
|
+
})
|
|
2930
|
+
const execEnv = buildExecutorEnv(run, worktreePath, mcpKey, userMcpServers)
|
|
2931
|
+
runLogger.info("runner.run.prepare", "Applying MCP side effects")
|
|
2932
|
+
const mcpSideEffectBackups = setupExecutorMcpSideEffects(mcpKey, userMcpServers)
|
|
2933
|
+
runLogger.info("runner.run.prepare", "Spawning executor", {
|
|
2934
|
+
meta: { command: execSpec.command, arg_count: execSpec.args.length },
|
|
2935
|
+
})
|
|
2936
|
+
|
|
2937
|
+
const logState = { tail: "" }
|
|
2938
|
+
const startedAt = Date.now()
|
|
2939
|
+
let stdioSummary = ""
|
|
2940
|
+
// Claude Code's JSON result arrives on stdout only; stderr would break parsing.
|
|
2941
|
+
let stdoutRaw = ""
|
|
2942
|
+
let runUsage = null
|
|
2943
|
+
let cancelRequested = false
|
|
2944
|
+
let childProcess = null
|
|
2945
|
+
let lastHeartbeatLogAt = 0
|
|
2946
|
+
let activityThreadCommentId = run.activity_thread_comment_id || null
|
|
2947
|
+
|
|
2948
|
+
function appendHeartbeat(force = false) {
|
|
2949
|
+
if (!childProcess || childProcess.killed) return
|
|
2950
|
+
const now = Date.now()
|
|
2951
|
+
if (!force && now - lastHeartbeatLogAt < 30000) return
|
|
2952
|
+
lastHeartbeatLogAt = now
|
|
2953
|
+
appendLogTail(
|
|
2954
|
+
logState,
|
|
2955
|
+
`> still running (${formatElapsed(now - startedAt)} elapsed); pid ${childProcess.pid ?? "unknown"}; waiting for ${activeExecutor()} output...`,
|
|
2956
|
+
)
|
|
2957
|
+
}
|
|
2958
|
+
|
|
2959
|
+
async function flushRunnerLog() {
|
|
2960
|
+
appendHeartbeat()
|
|
2961
|
+
const response = await postRunLog(state, run.id, logState.tail)
|
|
2962
|
+
if (response?.cancel_requested) {
|
|
2963
|
+
cancelRequested = true
|
|
2964
|
+
if (childProcess && !childProcess.killed) {
|
|
2965
|
+
appendLogTail(logState, "> cancel requested; terminating executor process...")
|
|
2966
|
+
requestChildProcessShutdown(childProcess)
|
|
2967
|
+
}
|
|
2968
|
+
}
|
|
2969
|
+
}
|
|
2970
|
+
|
|
2971
|
+
const logInterval = setInterval(() => {
|
|
2972
|
+
flushRunnerLog().catch(() => {})
|
|
2973
|
+
}, 5000)
|
|
2974
|
+
|
|
2975
|
+
// Hard timeout — kill the run if it hangs
|
|
2976
|
+
let runTimeoutHandle = null
|
|
2977
|
+
const runTimeoutPromise = new Promise((_, reject) => {
|
|
2978
|
+
runTimeoutHandle = setTimeout(() => {
|
|
2979
|
+
if (childProcess && !childProcess.killed) killChildProcessTree(childProcess, "SIGKILL")
|
|
2980
|
+
reject(new Error(`Run timed out after ${Math.round(RUN_TIMEOUT_MS / 60000)} minutes`))
|
|
2981
|
+
}, RUN_TIMEOUT_MS)
|
|
2982
|
+
})
|
|
2983
|
+
|
|
2984
|
+
try {
|
|
2985
|
+
runLogger.info("runner.run.started", "Local runner execution started", {
|
|
2986
|
+
meta: {
|
|
2987
|
+
branch,
|
|
2988
|
+
worktree_path: worktreePath,
|
|
2989
|
+
local_checkout_mode: localCheckoutMode,
|
|
2990
|
+
git_available: gitAvailable,
|
|
2991
|
+
},
|
|
2992
|
+
})
|
|
2993
|
+
appendLogTail(logState, `> preparing ${run.agent?.name || "agent"} run`)
|
|
2994
|
+
appendLogTail(logState, `> worktree: ${worktreePath}`)
|
|
2995
|
+
appendLogTail(logState, `> branch: ${branch || "(none)"}`)
|
|
2996
|
+
appendLogTail(logState, `> executor: ${activeExecutor()}`)
|
|
2997
|
+
await flushRunnerLog().catch(() => {})
|
|
2998
|
+
await postIssueMilestone(
|
|
2999
|
+
mcpKey,
|
|
3000
|
+
run.task_id,
|
|
3001
|
+
isReviewRun(run)
|
|
3002
|
+
? `AI review started in ${gitAvailable ? (branch || worktreePath) : worktreePath}.`
|
|
3003
|
+
: `Implementation started in ${gitAvailable ? (branch || worktreePath) : worktreePath}.`,
|
|
3004
|
+
"info",
|
|
3005
|
+
)
|
|
3006
|
+
if (!isReviewRun(run)) {
|
|
3007
|
+
appendLogTail(logState, "> claiming issue through local MCP...")
|
|
3008
|
+
await flushRunnerLog().catch(() => {})
|
|
3009
|
+
await callMcpTool(mcpKey, "claim_issue", { issue_id: run.task_id }, run.agent_id)
|
|
3010
|
+
}
|
|
3011
|
+
const locationSummary = !gitAvailable
|
|
3012
|
+
? `Starting local ${executorDisplayName()} run in workspace folder \`${worktreePath}\`. This folder is not a git repository, so branch setup, worktrees, and git-based verification are disabled for this run.`
|
|
3013
|
+
: localCheckoutMode === "repo_root"
|
|
3014
|
+
? `Starting local ${executorDisplayName()} run in repo root \`${worktreePath}\`${branch ? ` on current branch \`${branch}\`` : ""}. If this issue was attempted before, existing repo-root changes are still present and should be reviewed before new edits.`
|
|
3015
|
+
: `Using local ${executorDisplayName()} worktree on working branch \`${branch}\`. Worktree: \`${worktreePath}\`. If this issue was attempted before, existing worktree changes are preserved and should be reviewed before new edits.`
|
|
3016
|
+
if (!activityThreadCommentId) {
|
|
3017
|
+
activityThreadCommentId = null
|
|
3018
|
+
}
|
|
3019
|
+
await flushRunnerLog().catch(() => {})
|
|
3020
|
+
|
|
3021
|
+
if (automationSmokeTestMode && isAutomationSmokeTestIssue(run)) {
|
|
3022
|
+
const smokeResult = await runSmokeTestExecution(run, state, { activityThreadCommentId })
|
|
3023
|
+
stdioSummary = smokeResult.summary
|
|
3024
|
+
appendLogTail(logState, smokeResult.summary)
|
|
3025
|
+
appendLogTail(logState, smokeResult.verificationSummary)
|
|
3026
|
+
if (execSpec.summaryMode === "file") {
|
|
3027
|
+
fs.writeFileSync(outputFile, smokeResult.summary)
|
|
3028
|
+
}
|
|
3029
|
+
} else {
|
|
3030
|
+
appendLogTail(logState, `> spawning ${activeExecutor()} executor`)
|
|
3031
|
+
appendLogTail(logState, `> cwd: ${worktreePath}`)
|
|
3032
|
+
appendLogTail(logState, `> command: ${previewExecutorCommand(execSpec)}`)
|
|
3033
|
+
await flushRunnerLog().catch(() => {})
|
|
3034
|
+
await Promise.race([runTimeoutPromise, new Promise((resolve, reject) => {
|
|
3035
|
+
const child = spawn(execSpec.command, execSpec.args, {
|
|
3036
|
+
cwd: worktreePath,
|
|
3037
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
3038
|
+
detached: process.platform !== "win32",
|
|
3039
|
+
env: execEnv,
|
|
3040
|
+
})
|
|
3041
|
+
childProcess = child
|
|
3042
|
+
appendLogTail(logState, `> child pid: ${child.pid ?? "unknown"}`)
|
|
3043
|
+
appendHeartbeat(true)
|
|
3044
|
+
flushRunnerLog().catch(() => {})
|
|
3045
|
+
|
|
3046
|
+
if (execSpec.stdin) {
|
|
3047
|
+
child.stdin.write(execSpec.stdin)
|
|
3048
|
+
}
|
|
3049
|
+
child.stdin.end()
|
|
3050
|
+
|
|
3051
|
+
child.stdout.on("data", chunk => {
|
|
3052
|
+
const text = chunk.toString()
|
|
3053
|
+
if (execSpec.summaryMode === "claude_json") stdoutRaw = truncate(`${stdoutRaw}${text}`, 2_000_000)
|
|
3054
|
+
appendLogTail(logState, text)
|
|
3055
|
+
stdioSummary = truncate(`${stdioSummary}\n${text}`, 16000)
|
|
3056
|
+
})
|
|
3057
|
+
child.stderr.on("data", chunk => {
|
|
3058
|
+
const text = chunk.toString()
|
|
3059
|
+
appendLogTail(logState, text)
|
|
3060
|
+
stdioSummary = truncate(`${stdioSummary}\n${text}`, 16000)
|
|
3061
|
+
})
|
|
3062
|
+
child.on("error", error => {
|
|
3063
|
+
appendLogTail(logState, `> executor spawn error: ${error instanceof Error ? error.message : String(error)}`)
|
|
3064
|
+
reject(error)
|
|
3065
|
+
})
|
|
3066
|
+
child.on("close", code => {
|
|
3067
|
+
appendLogTail(logState, `> executor exited with code ${code}`)
|
|
3068
|
+
if (execSpec.summaryMode === "claude_json") {
|
|
3069
|
+
const result = parseClaudeJsonResult(stdoutRaw)
|
|
3070
|
+
if (result) {
|
|
3071
|
+
// Claude Code prices every run at Anthropic's rates; for
|
|
3072
|
+
// another provider that number is wrong, so keep tokens only.
|
|
3073
|
+
runUsage = usesThirdPartyModels(run) ? { ...result.usage, cost_usd: null } : result.usage
|
|
3074
|
+
// The message, not the JSON around it, is what the
|
|
3075
|
+
// summary, the PR description and failure detection read.
|
|
3076
|
+
stdioSummary = result.text
|
|
3077
|
+
appendLogTail(logState, `> ${result.isError ? "error" : "result"}: ${truncate(result.text, 2000)}`)
|
|
3078
|
+
if (result.usage.cost_usd !== null) {
|
|
3079
|
+
appendLogTail(logState, `> cost: $${result.usage.cost_usd.toFixed(4)}, ${result.usage.num_turns ?? "?"} turns`)
|
|
3080
|
+
}
|
|
3081
|
+
if (code === 0 && result.isError) {
|
|
3082
|
+
reject(new Error(formatChildExitError(activeExecutor(), "0 (reported an error)", result.text)))
|
|
3083
|
+
return
|
|
3084
|
+
}
|
|
3085
|
+
}
|
|
3086
|
+
}
|
|
3087
|
+
if (code === 0) {
|
|
3088
|
+
resolve()
|
|
3089
|
+
return
|
|
3090
|
+
}
|
|
3091
|
+
if (cancelRequested) {
|
|
3092
|
+
reject(new Error("Canceled by admin."))
|
|
3093
|
+
return
|
|
3094
|
+
}
|
|
3095
|
+
reject(new Error(formatChildExitError(activeExecutor(), code, stdioSummary)))
|
|
3096
|
+
})
|
|
3097
|
+
})])
|
|
3098
|
+
}
|
|
3099
|
+
|
|
3100
|
+
await flushRunnerLog().catch(() => {})
|
|
3101
|
+
|
|
3102
|
+
const summary = execSpec.summaryMode === "file"
|
|
3103
|
+
? (fs.existsSync(outputFile) ? fs.readFileSync(outputFile, "utf8").trim() : "Run finished without a final summary.")
|
|
3104
|
+
: (stdioSummary.trim() || "Run finished without a final summary.")
|
|
3105
|
+
|
|
3106
|
+
const reviewerDecision = await maybeSubmitReviewerDecision(run, mcpKey, summary)
|
|
3107
|
+
|
|
3108
|
+
const gitStatus = gitAvailable
|
|
3109
|
+
? await execCommand("git", ["-C", worktreePath, "status", "--short"], worktreePath)
|
|
3110
|
+
: { stdout: "", stderr: "" }
|
|
3111
|
+
// A plain `git diff` shows only uncommitted work. Agents usually commit their
|
|
3112
|
+
// own changes, which left the diff empty even though the run produced real
|
|
3113
|
+
// work — so Execution details had nothing to show.
|
|
3114
|
+
// Always compare against the default branch, never the branch's own upstream.
|
|
3115
|
+
// A worktree reused across runs already tracks its remote branch, so
|
|
3116
|
+
// @{upstream}..HEAD is empty and the diff came back blank even though the
|
|
3117
|
+
// run had produced real work.
|
|
3118
|
+
const diffRange = gitAvailable ? "origin/HEAD..HEAD" : null
|
|
3119
|
+
|
|
3120
|
+
const gitDiff = async (extraArgs) => {
|
|
3121
|
+
if (!gitAvailable) return { stdout: "", stderr: "" }
|
|
3122
|
+
// Uncommitted changes first; fall back to what this branch adds.
|
|
3123
|
+
const working = await execCommandSafe(
|
|
3124
|
+
"git", ["-C", worktreePath, "diff", ...extraArgs], worktreePath,
|
|
3125
|
+
)
|
|
3126
|
+
if (working?.stdout?.trim()) return working
|
|
3127
|
+
const committed = await execCommandSafe(
|
|
3128
|
+
"git", ["-C", worktreePath, "diff", diffRange, ...extraArgs], worktreePath,
|
|
3129
|
+
)
|
|
3130
|
+
return committed ?? { stdout: "", stderr: "" }
|
|
3131
|
+
}
|
|
3132
|
+
|
|
3133
|
+
// Captured after the commit below, not here: when the runner commits the
|
|
3134
|
+
// agent's work, a diff taken at this point predates that commit and both
|
|
3135
|
+
// the working tree and origin/HEAD..HEAD come back empty.
|
|
3136
|
+
let diffStat = { stdout: "", stderr: "" }
|
|
3137
|
+
let diffPatch = { stdout: "", stderr: "" }
|
|
3138
|
+
let diffSummary = ""
|
|
3139
|
+
const hasChanges = gitAvailable && gitStatus.stdout.trim().length > 0
|
|
3140
|
+
|
|
3141
|
+
// Agents often commit their own work, which leaves the working tree clean.
|
|
3142
|
+
// Checking only for uncommitted changes therefore concluded "nothing
|
|
3143
|
+
// happened" and skipped the push, stranding real commits on the runner.
|
|
3144
|
+
// Compare against the remote branch when it exists, otherwise against the
|
|
3145
|
+
// repository's default branch. A brand-new agent branch has no upstream yet.
|
|
3146
|
+
const unpushedCommits = gitAvailable
|
|
3147
|
+
? (
|
|
3148
|
+
(await execCommandSafe(
|
|
3149
|
+
"git",
|
|
3150
|
+
["-C", worktreePath, "log", "--oneline", "@{upstream}..HEAD"],
|
|
3151
|
+
worktreePath,
|
|
3152
|
+
))?.stdout
|
|
3153
|
+
?? (await execCommandSafe(
|
|
3154
|
+
"git",
|
|
3155
|
+
["-C", worktreePath, "log", "--oneline", "origin/HEAD..HEAD"],
|
|
3156
|
+
worktreePath,
|
|
3157
|
+
))?.stdout
|
|
3158
|
+
?? ""
|
|
3159
|
+
).trim()
|
|
3160
|
+
: ""
|
|
3161
|
+
// Agents can also push their own branch (Claude Code has a shell and the
|
|
3162
|
+
// machine's git credentials). Then nothing is "unpushed", yet the branch is
|
|
3163
|
+
// ahead of the default branch and still needs its pull request. Ahead of
|
|
3164
|
+
// the default branch is what counts; pushing again is a no-op.
|
|
3165
|
+
const aheadOfBase = gitAvailable
|
|
3166
|
+
? ((await execCommandSafe(
|
|
3167
|
+
"git", ["-C", worktreePath, "log", "--oneline", "origin/HEAD..HEAD"], worktreePath,
|
|
3168
|
+
))?.stdout ?? "").trim()
|
|
3169
|
+
: ""
|
|
3170
|
+
const hasUnpushedCommits = unpushedCommits.length > 0 || aheadOfBase.length > 0
|
|
3171
|
+
// Same reason as the diff: after the agent commits, `git status` is empty, so
|
|
3172
|
+
// the changed-file list came back empty for runs that had real work.
|
|
3173
|
+
const committedFileList = gitAvailable && !gitStatus.stdout.trim()
|
|
3174
|
+
? ((await execCommandSafe(
|
|
3175
|
+
"git", ["-C", worktreePath, "diff", "--name-only", diffRange], worktreePath,
|
|
3176
|
+
))?.stdout ?? "")
|
|
3177
|
+
: ""
|
|
3178
|
+
|
|
3179
|
+
const changedFiles = gitAvailable
|
|
3180
|
+
? (gitStatus.stdout.trim()
|
|
3181
|
+
? gitStatus.stdout
|
|
3182
|
+
.split(/\r?\n/)
|
|
3183
|
+
.map(line => line.trim())
|
|
3184
|
+
.filter(Boolean)
|
|
3185
|
+
.map(line => line.replace(/^[A-Z?]+\s+/, ""))
|
|
3186
|
+
: committedFileList
|
|
3187
|
+
.split(/\r?\n/)
|
|
3188
|
+
.map(line => line.trim())
|
|
3189
|
+
.filter(Boolean))
|
|
3190
|
+
: []
|
|
3191
|
+
|
|
3192
|
+
// TRIPWIRE PROTOCOL
|
|
3193
|
+
const isScout = getAgentSkillProfileSlug(run.agent) === "scout"
|
|
3194
|
+
if (gitAvailable && changedFiles.length > 20 && !isScout) {
|
|
3195
|
+
runLogger.error("runner.tripwire.tripped", "Agent tripwire activated: Too many file changes.", {
|
|
3196
|
+
meta: { changed_files_count: changedFiles.length, files: changedFiles }
|
|
3197
|
+
})
|
|
3198
|
+
// Reset to prevent dirty state from corrupting the workspace
|
|
3199
|
+
await execCommand("git", ["-C", worktreePath, "reset", "--hard"], worktreePath).catch(() => {})
|
|
3200
|
+
await execCommand("git", ["-C", worktreePath, "clean", "-fd"], worktreePath).catch(() => {})
|
|
3201
|
+
|
|
3202
|
+
throw new Error(`TRIPWIRE ACTIVATED: Agent attempted to modify ${changedFiles.length} files, exceeding the limit. An Impact Report has been generated.`)
|
|
3203
|
+
}
|
|
3204
|
+
|
|
3205
|
+
|
|
3206
|
+
const runnerVerificationSummary = extractSection(summary, "Verification")
|
|
3207
|
+
const verification = await runVerificationPlan(worktreePath, changedFiles, runnerProfile)
|
|
3208
|
+
const verificationSummary = [
|
|
3209
|
+
runnerVerificationSummary,
|
|
3210
|
+
verification.summary,
|
|
3211
|
+
].filter(Boolean).join("\n")
|
|
3212
|
+
await postIssueMilestone(
|
|
3213
|
+
mcpKey,
|
|
3214
|
+
run.task_id,
|
|
3215
|
+
verificationSummary
|
|
3216
|
+
? `Verification finished. ${truncate(verificationSummary.replace(/\s+/g, " "), 280)}`
|
|
3217
|
+
: "Verification finished with no summary reported.",
|
|
3218
|
+
"success",
|
|
3219
|
+
)
|
|
3220
|
+
// No commit means no push and no pull request, and each reason below used to
|
|
3221
|
+
// pass silently — leaving a branch on GitHub with nothing on it and a
|
|
3222
|
+
// "create pull request" button that fails with GitHub's 422.
|
|
3223
|
+
const canAutoCommit = runnerProfile.git?.autoCommitOnSuccess !== false
|
|
3224
|
+
const commitResult = gitAvailable && hasChanges && canAutoCommit
|
|
3225
|
+
? await createStandardizedCommit(worktreePath, run, runnerProfile)
|
|
3226
|
+
: null
|
|
3227
|
+
|
|
3228
|
+
diffStat = await gitDiff(["--stat"])
|
|
3229
|
+
diffPatch = await gitDiff(["--no-ext-diff", "--minimal", "--unified=3"])
|
|
3230
|
+
diffSummary = gitAvailable ? summarizeDiffStat(diffStat.stdout) : ""
|
|
3231
|
+
const diffExcerpt = gitAvailable ? (truncateHead(diffPatch.stdout.trim(), 40000) || null) : null
|
|
3232
|
+
// Kept: an empty diff on a run that produced work is a real failure mode and
|
|
3233
|
+
// was invisible for a long time.
|
|
3234
|
+
runLogger.info("runner.run.diff_captured", "Diff captured", {
|
|
3235
|
+
meta: {
|
|
3236
|
+
range: diffRange,
|
|
3237
|
+
stat_len: (diffStat.stdout || "").trim().length,
|
|
3238
|
+
patch_len: (diffPatch.stdout || "").trim().length,
|
|
3239
|
+
},
|
|
3240
|
+
})
|
|
3241
|
+
|
|
3242
|
+
// Push when there is anything to push, whether this runner committed it or
|
|
3243
|
+
// the agent did.
|
|
3244
|
+
const somethingToPush = Boolean(commitResult) || hasUnpushedCommits
|
|
3245
|
+
|
|
3246
|
+
if (!commitResult && !hasUnpushedCommits) {
|
|
3247
|
+
const reason = !gitAvailable
|
|
3248
|
+
? "the workspace is not a git repository"
|
|
3249
|
+
: !hasChanges
|
|
3250
|
+
? "the agent did not change any files"
|
|
3251
|
+
: `auto-commit is disabled in ${runnerProfile._path || "the runner profile"}`
|
|
3252
|
+
runLogger.warn(
|
|
3253
|
+
"runner.run.commit_skipped",
|
|
3254
|
+
`No commit was made: ${reason}`,
|
|
3255
|
+
{ meta: { branch, has_changes: hasChanges, auto_commit: canAutoCommit } },
|
|
3256
|
+
)
|
|
3257
|
+
await postIssueMilestone(
|
|
3258
|
+
mcpKey,
|
|
3259
|
+
run.task_id,
|
|
3260
|
+
`No commit was made, so no pull request was opened: ${reason}.`,
|
|
3261
|
+
"warning",
|
|
3262
|
+
).catch(() => {})
|
|
3263
|
+
}
|
|
3264
|
+
|
|
3265
|
+
// Push branch to GitHub and open a PR when a commit was made and
|
|
3266
|
+
// RUNNER_AUTO_PUSH_ON_SUCCESS=true (or --auto-push-on-success).
|
|
3267
|
+
// Requires the workspace to have a GitHub App installation connected
|
|
3268
|
+
// and the task's project (or workspace) to have a github_repo configured.
|
|
3269
|
+
let pushResult = null
|
|
3270
|
+
const isReviewerRun2 = isReviewRun(run)
|
|
3271
|
+
|
|
3272
|
+
// Same problem as the commit gate: a skipped push left the work stranded on
|
|
3273
|
+
// the runner machine with no indication why.
|
|
3274
|
+
if (somethingToPush && !isReviewerRun2 && !autoPushOnSuccessFlag) {
|
|
3275
|
+
runLogger.warn(
|
|
3276
|
+
"runner.run.push_skipped",
|
|
3277
|
+
"A commit was made but auto-push is disabled, so no pull request was opened",
|
|
3278
|
+
{ meta: { branch } },
|
|
3279
|
+
)
|
|
3280
|
+
await postIssueMilestone(
|
|
3281
|
+
mcpKey,
|
|
3282
|
+
run.task_id,
|
|
3283
|
+
"Changes were committed locally but auto-push is disabled, so no pull request was opened.",
|
|
3284
|
+
"warning",
|
|
3285
|
+
).catch(() => {})
|
|
3286
|
+
}
|
|
3287
|
+
|
|
3288
|
+
if (autoPushOnSuccessFlag && somethingToPush && branch && gitAvailable && !isReviewerRun2) {
|
|
3289
|
+
try {
|
|
3290
|
+
const githubToken = run.__githubToken
|
|
3291
|
+
const repo = run.__repo
|
|
3292
|
+
|
|
3293
|
+
if (githubToken && repo?.owner && repo?.repo) {
|
|
3294
|
+
await pushBranchToGitHub(worktreePath, branch, githubToken, repo)
|
|
3295
|
+
|
|
3296
|
+
// Base the PR on the target repo's default branch, not the runner's.
|
|
3297
|
+
const defaultBranch = await detectDefaultBranch(run.__repoRoot || repoRoot)
|
|
3298
|
+
const pr = await createGithubPr(githubToken, repo, branch, defaultBranch, run, {
|
|
3299
|
+
summary,
|
|
3300
|
+
changedFiles,
|
|
3301
|
+
diffStat: diffStat.stdout,
|
|
3302
|
+
verificationSummary: runnerVerificationSummary,
|
|
3303
|
+
})
|
|
3304
|
+
|
|
3305
|
+
pushResult = { pushed: true, repo: `${repo.owner}/${repo.repo}`, pr_url: pr.html_url, pr_number: pr.number }
|
|
3306
|
+
|
|
3307
|
+
// Post PR URL back to the issue via MCP. This is what moves the
|
|
3308
|
+
// issue to in_review and starts the reviewer handoff, so a
|
|
3309
|
+
// silent failure here strands a finished pull request with no
|
|
3310
|
+
// review — which is exactly what it did before it was surfaced.
|
|
3311
|
+
try {
|
|
3312
|
+
await callMcpTool(
|
|
3313
|
+
mcpKey,
|
|
3314
|
+
"set_pr_url",
|
|
3315
|
+
{ issue_id: run.task_id, pr_url: pr.html_url },
|
|
3316
|
+
run.agent_id,
|
|
3317
|
+
)
|
|
3318
|
+
} catch (setPrError) {
|
|
3319
|
+
runLogger.error(
|
|
3320
|
+
"runner.run.set_pr_url_failed",
|
|
3321
|
+
"Pull request was opened but could not be linked to the issue",
|
|
3322
|
+
{ meta: { pr_url: pr.html_url } },
|
|
3323
|
+
setPrError,
|
|
3324
|
+
)
|
|
3325
|
+
await postIssueMilestone(
|
|
3326
|
+
mcpKey,
|
|
3327
|
+
run.task_id,
|
|
3328
|
+
`Pull request ${pr.html_url} was opened but could not be linked to this issue, `
|
|
3329
|
+
+ `so review was not requested automatically.`,
|
|
3330
|
+
"warning",
|
|
3331
|
+
).catch(() => {})
|
|
3332
|
+
}
|
|
3333
|
+
|
|
3334
|
+
await postIssueMilestone(
|
|
3335
|
+
mcpKey,
|
|
3336
|
+
run.task_id,
|
|
3337
|
+
`Branch pushed and PR opened: ${pr.html_url}`,
|
|
3338
|
+
"success",
|
|
3339
|
+
)
|
|
3340
|
+
|
|
3341
|
+
runLogger.info("runner.run.pushed", "Branch pushed and PR opened", {
|
|
3342
|
+
meta: { branch, repo: `${repo.owner}/${repo.repo}`, pr_url: pr.html_url },
|
|
3343
|
+
})
|
|
3344
|
+
} else {
|
|
3345
|
+
runLogger.warn("runner.run.push_skipped", "No GitHub repo configured for this task — skipping push", {
|
|
3346
|
+
meta: { project_id: projectId },
|
|
3347
|
+
})
|
|
3348
|
+
}
|
|
3349
|
+
} catch (pushError) {
|
|
3350
|
+
// Never let a token in a remote URL reach a log or the issue.
|
|
3351
|
+
const pushMessage = (pushError instanceof Error ? pushError.message : String(pushError))
|
|
3352
|
+
.replace(/x-access-token:[^@\s]+@/g, "x-access-token:***@")
|
|
3353
|
+
runLogger.error("runner.run.push_failed", "Failed to push branch to GitHub", { meta: { branch } })
|
|
3354
|
+
// Work that never reached GitHub is not finished: completing the run
|
|
3355
|
+
// sent an empty branch to review, which asked for changes until the
|
|
3356
|
+
// revision limit ran out. Fail it with the reason instead.
|
|
3357
|
+
throw new Error(`The work was committed but could not be pushed to GitHub: ${truncate(pushMessage.replace(/\s+/g, " "), 400)}`)
|
|
3358
|
+
}
|
|
3359
|
+
}
|
|
3360
|
+
|
|
3361
|
+
const artifacts = {
|
|
3362
|
+
run_id: run.id,
|
|
3363
|
+
task_id: run.task_id,
|
|
3364
|
+
issue_identifier: run.task?.identifier || null,
|
|
3365
|
+
branch_name: gitAvailable ? branch : null,
|
|
3366
|
+
worktree_path: worktreePath,
|
|
3367
|
+
repo_root: repoRoot,
|
|
3368
|
+
git_available: gitAvailable,
|
|
3369
|
+
changed_files: changedFiles,
|
|
3370
|
+
diff_stat: diffStat.stdout.trim() || null,
|
|
3371
|
+
diff_excerpt: diffExcerpt,
|
|
3372
|
+
verification_summary: verificationSummary || null,
|
|
3373
|
+
commit: commitResult,
|
|
3374
|
+
result_summary: summary,
|
|
3375
|
+
local_executor: activeExecutor(),
|
|
3376
|
+
local_checkout_mode: localCheckoutMode,
|
|
3377
|
+
runner_profile_path: runnerProfile._path || null,
|
|
3378
|
+
report_path: reportFile,
|
|
3379
|
+
generated_at: new Date().toISOString(),
|
|
3380
|
+
}
|
|
3381
|
+
fs.writeFileSync(artifactsFile, JSON.stringify(artifacts, null, 2))
|
|
3382
|
+
|
|
3383
|
+
const reportMarkdown = buildExecutionReportMarkdown({
|
|
3384
|
+
artifactsFile,
|
|
3385
|
+
branch,
|
|
3386
|
+
changedFiles,
|
|
3387
|
+
commitResult,
|
|
3388
|
+
diffStat: diffStat.stdout.trim(),
|
|
3389
|
+
localCheckoutMode,
|
|
3390
|
+
reportFile,
|
|
3391
|
+
summary,
|
|
3392
|
+
verificationSummary,
|
|
3393
|
+
worktreePath,
|
|
3394
|
+
})
|
|
3395
|
+
fs.writeFileSync(reportFile, reportMarkdown)
|
|
3396
|
+
|
|
3397
|
+
// Post before completing: completion revokes this run's key, and a milestone
|
|
3398
|
+
// sent after it was rejected without a trace.
|
|
3399
|
+
await postIssueMilestone(
|
|
3400
|
+
mcpKey,
|
|
3401
|
+
run.task_id,
|
|
3402
|
+
isReviewRun(run)
|
|
3403
|
+
? "AI review finished and the review outcome was posted back to the issue."
|
|
3404
|
+
: commitResult?.sha
|
|
3405
|
+
? pushResult?.pr_url
|
|
3406
|
+
? `Implementation complete. PR opened: ${pushResult.pr_url}`
|
|
3407
|
+
: `Implementation complete. Created commit ${commitResult.sha.slice(0, 7)}.`
|
|
3408
|
+
: "Implementation complete and results were posted back to the issue.",
|
|
3409
|
+
"success",
|
|
3410
|
+
)
|
|
3411
|
+
|
|
3412
|
+
await completeRun(state, run.id, {
|
|
3413
|
+
status: "completed",
|
|
3414
|
+
result_summary: truncate(summary, 4000),
|
|
3415
|
+
usage: runUsage,
|
|
3416
|
+
branch_name: branch,
|
|
3417
|
+
changed_files: changedFiles,
|
|
3418
|
+
diff_stat: truncate(diffStat.stdout.trim(), 4000),
|
|
3419
|
+
// This completion payload omitted the excerpt entirely, so the diff was
|
|
3420
|
+
// captured and then dropped on the way to the server.
|
|
3421
|
+
diff_excerpt: diffExcerpt,
|
|
3422
|
+
verification_summary: truncate(verificationSummary, 4000),
|
|
3423
|
+
worktree_path: worktreePath,
|
|
3424
|
+
commit_sha: commitResult?.sha,
|
|
3425
|
+
commit_message: commitResult?.subject,
|
|
3426
|
+
pr_url: pushResult?.pr_url ?? null,
|
|
3427
|
+
artifact_path: artifactsFile,
|
|
3428
|
+
review_outcome: reviewerDecision?.decision === "approve"
|
|
3429
|
+
? "approved"
|
|
3430
|
+
: reviewerDecision?.decision === "request_changes"
|
|
3431
|
+
? "changes_requested"
|
|
3432
|
+
: undefined,
|
|
3433
|
+
})
|
|
3434
|
+
|
|
3435
|
+
// Optional local merge (RUNNER_AUTO_MERGE_TO_MAIN=true) once the issue is
|
|
3436
|
+
// approved and closed. It waits in the background: waiting inside the run
|
|
3437
|
+
// held a parallel slot for up to 30 minutes doing nothing.
|
|
3438
|
+
if (
|
|
3439
|
+
autoMergeToMainFlag
|
|
3440
|
+
&& !isReviewRun(run)
|
|
3441
|
+
&& commitResult
|
|
3442
|
+
&& branch
|
|
3443
|
+
&& gitAvailable
|
|
3444
|
+
&& repoRoot
|
|
3445
|
+
&& run.task_id
|
|
3446
|
+
) {
|
|
3447
|
+
void mergeToMainWhenApproved({ run, state, branch, worktreePath, runLogger }).catch((error) => {
|
|
3448
|
+
runLogger.warn("runner.run.merge_failed", "Background merge to main failed", { meta: { error: String(error) } })
|
|
3449
|
+
})
|
|
3450
|
+
}
|
|
3451
|
+
|
|
3452
|
+
|
|
3453
|
+
runLogger.info("runner.run.completed", "Local runner execution completed", {
|
|
3454
|
+
meta: {
|
|
3455
|
+
branch,
|
|
3456
|
+
worktree_path: worktreePath,
|
|
3457
|
+
artifact_path: artifactsFile,
|
|
3458
|
+
changed_file_count: changedFiles.length,
|
|
3459
|
+
committed: Boolean(commitResult),
|
|
3460
|
+
pushed_to_github: Boolean(pushResult?.pushed),
|
|
3461
|
+
pr_url: pushResult?.pr_url ?? null,
|
|
3462
|
+
},
|
|
3463
|
+
})
|
|
3464
|
+
|
|
3465
|
+
return { summary, branch, worktreePath, artifactsFile, reportFile, verificationSummary, commitResult, pushResult }
|
|
3466
|
+
} catch (error) {
|
|
3467
|
+
await flushRunnerLog().catch(() => {})
|
|
3468
|
+
const rawMessage = cancelRequested ? "Canceled by admin." : (error instanceof Error ? error.message : String(error))
|
|
3469
|
+
const usageLimit = cancelRequested ? null : detectUsageLimit(`${rawMessage}\n${logState.tail}`)
|
|
3470
|
+
// A key's own limit is that key's business: pausing the runner would also
|
|
3471
|
+
// stop agents on the machine's subscription and on other keys. The server
|
|
3472
|
+
// switches the agent to its fallback key, or retries at the reset.
|
|
3473
|
+
const paysWithKey = Boolean(runCredentials(run))
|
|
3474
|
+
// The server reads "Resets at <ISO>" to schedule the retry for that moment.
|
|
3475
|
+
const message = usageLimit
|
|
3476
|
+
? `Usage limit reached on ${paysWithKey ? `this agent's ${run.api_credentials.provider} key` : executorDisplayName()}. Resets at ${usageLimit.resetAt.toISOString()}.\n\n${rawMessage}`
|
|
3477
|
+
: rawMessage
|
|
3478
|
+
if (usageLimit && !paysWithKey) {
|
|
3479
|
+
usageLimitPausedUntil = Math.max(usageLimitPausedUntil, usageLimit.resetAt.getTime())
|
|
3480
|
+
runLogger.warn("runner.usage_limit", "Executor hit its usage limit; pausing claims", {
|
|
3481
|
+
meta: { reset_at: usageLimit.resetAt.toISOString(), stated: usageLimit.stated },
|
|
3482
|
+
})
|
|
3483
|
+
}
|
|
3484
|
+
await postIssueMilestone(
|
|
3485
|
+
mcpKey,
|
|
3486
|
+
run.task_id,
|
|
3487
|
+
usageLimit
|
|
3488
|
+
? `${paysWithKey ? "The agent's key" : executorDisplayName()} hit its usage limit. This picks up again automatically `
|
|
3489
|
+
+ `${usageLimit.stated ? "when it resets" : "after a short wait"}, at ${formatLocalTime(usageLimit.resetAt)}, or sooner on a fallback key.`
|
|
3490
|
+
: `Execution ${cancelRequested ? "was canceled" : "failed"}: ${truncate(message.replace(/\s+/g, " "), 280)}`,
|
|
3491
|
+
"warning",
|
|
3492
|
+
)
|
|
3493
|
+
// No add_comment here: the failure is already on the run record (shown in
|
|
3494
|
+
// Execution details) and in the milestone above. A third copy dumped the raw
|
|
3495
|
+
// stack trace into the issue feed, which buried the actual conversation.
|
|
3496
|
+
|
|
3497
|
+
await completeRun(state, run.id, {
|
|
3498
|
+
status: "error",
|
|
3499
|
+
error_message: truncate(message, 4000),
|
|
3500
|
+
usage: runUsage,
|
|
3501
|
+
}).catch(() => {})
|
|
3502
|
+
|
|
3503
|
+
runLogger.error("runner.run.failed", "Local runner execution failed", {
|
|
3504
|
+
meta: {
|
|
3505
|
+
branch,
|
|
3506
|
+
worktree_path: worktreePath,
|
|
3507
|
+
canceled: cancelRequested,
|
|
3508
|
+
},
|
|
3509
|
+
}, error)
|
|
3510
|
+
throw error
|
|
3511
|
+
} finally {
|
|
3512
|
+
clearInterval(logInterval)
|
|
3513
|
+
if (runTimeoutHandle) clearTimeout(runTimeoutHandle)
|
|
3514
|
+
cleanupExecutorMcp(mcpSideEffectBackups)
|
|
3515
|
+
}
|
|
3516
|
+
}
|
|
3517
|
+
|
|
3518
|
+
// How long (ms) before we consider a run stuck and mark it error
|
|
3519
|
+
const RUN_TIMEOUT_MS = Number(args["run-timeout-ms"] || process.env.RUNNER_RUN_TIMEOUT_MS || 4 * 60 * 60 * 1000) // 4 hour default
|
|
3520
|
+
const CLAIM_ERROR_BACKOFF_MS = Number(args["claim-error-backoff-ms"] || process.env.RUNNER_CLAIM_ERROR_BACKOFF_MS || 5000)
|
|
3521
|
+
|
|
3522
|
+
const CHECKOUT_CLEANUP_INTERVAL_MS = 30 * 60_000
|
|
3523
|
+
|
|
3524
|
+
/**
|
|
3525
|
+
* Remove the checkouts and local branches of tickets that are finished.
|
|
3526
|
+
*
|
|
3527
|
+
* Every ticket gets its own git worktree, and nothing removed them, so a busy
|
|
3528
|
+
* runner kept one full checkout per ticket forever. A checkout goes once its
|
|
3529
|
+
* issue is Done or Canceled — and only if it has no uncommitted changes and its
|
|
3530
|
+
* branch is the one the issue recorded (identifiers can repeat; the branch
|
|
3531
|
+
* proves it is the same ticket). Open and failed tickets keep theirs.
|
|
3532
|
+
*/
|
|
3533
|
+
async function cleanupFinishedCheckouts(state, logger) {
|
|
3534
|
+
const mcpKey = state.runner_mcp_key
|
|
3535
|
+
if (!mcpKey) return 0
|
|
3536
|
+
const roots = [repoRoot]
|
|
3537
|
+
const reposDir = path.join(repoRoot, ".agent", "repos")
|
|
3538
|
+
for (const owner of fs.existsSync(reposDir) ? fs.readdirSync(reposDir) : []) {
|
|
3539
|
+
for (const repo of fs.readdirSync(path.join(reposDir, owner))) roots.push(path.join(reposDir, owner, repo))
|
|
3540
|
+
}
|
|
3541
|
+
|
|
3542
|
+
let removed = 0
|
|
3543
|
+
for (const root of roots) {
|
|
3544
|
+
const issuesDir = path.join(root, ".agent", "issues")
|
|
3545
|
+
if (!fs.existsSync(issuesDir)) continue
|
|
3546
|
+
for (const slug of fs.readdirSync(issuesDir)) {
|
|
3547
|
+
const worktree = path.join(issuesDir, slug, "worktree")
|
|
3548
|
+
if (!fs.existsSync(worktree)) continue
|
|
3549
|
+
const branch = (await execCommandSafe("git", ["-C", worktree, "branch", "--show-current"], worktree))?.stdout?.trim()
|
|
3550
|
+
if (!branch?.startsWith("agent/")) continue
|
|
3551
|
+
const marker = (() => {
|
|
3552
|
+
try { return JSON.parse(fs.readFileSync(path.join(issuesDir, slug, "pacework-task.json"), "utf8")) } catch { return null }
|
|
3553
|
+
})()
|
|
3554
|
+
const issue = await callMcpTool(mcpKey, "get_issue", { issue_id: marker?.task_id || slug.toUpperCase(), workspace_id: workspaceId }).catch(() => null)
|
|
3555
|
+
if (!issue || !["done", "canceled"].includes(issue.status) || issue.branch_name !== branch) continue
|
|
3556
|
+
const dirty = (await execCommandSafe("git", ["-C", worktree, "status", "--porcelain"], worktree))?.stdout?.trim()
|
|
3557
|
+
if (dirty) continue
|
|
3558
|
+
const gone = await execCommandSafe("git", ["-C", root, "worktree", "remove", worktree], root)
|
|
3559
|
+
if (!gone) continue
|
|
3560
|
+
// Squash merges leave the branch "unmerged" to git, hence -D.
|
|
3561
|
+
await execCommandSafe("git", ["-C", root, "branch", "-D", branch], root)
|
|
3562
|
+
fs.rmSync(path.join(issuesDir, slug), { recursive: true, force: true })
|
|
3563
|
+
removed += 1
|
|
3564
|
+
}
|
|
3565
|
+
await execCommandSafe("git", ["-C", root, "worktree", "prune"], root)
|
|
3566
|
+
}
|
|
3567
|
+
if (removed > 0) logger.info("runner.cleanup.checkouts", `Removed ${removed} checkout(s) of finished tickets`, { meta: { removed } })
|
|
3568
|
+
return removed
|
|
3569
|
+
}
|
|
3570
|
+
|
|
3571
|
+
async function resetStaleRuns(state) {
|
|
3572
|
+
// Tell the app to reset any runs this runner left in "running" state
|
|
3573
|
+
return await requestJson(`${appUrl}/api/agent-runners/reset-stale`, {
|
|
3574
|
+
method: "POST",
|
|
3575
|
+
headers: {
|
|
3576
|
+
"Content-Type": "application/json",
|
|
3577
|
+
Authorization: `Bearer ${state.runner_secret}`,
|
|
3578
|
+
},
|
|
3579
|
+
body: JSON.stringify({
|
|
3580
|
+
workspace_id: state.workspace_id,
|
|
3581
|
+
runner_id: state.runner_id,
|
|
3582
|
+
}),
|
|
3583
|
+
})
|
|
3584
|
+
}
|
|
3585
|
+
|
|
3586
|
+
async function verifyEnvironmentHealth() {
|
|
3587
|
+
const nodeVer = process.version
|
|
3588
|
+
if (!nodeVer.startsWith("v2") && !nodeVer.startsWith("v18") && !nodeVer.startsWith("v19")) {
|
|
3589
|
+
runnerBaseLogger.warn("runner.env.check", `Warning: Unexpected Node version ${nodeVer}. Proceeding, but execution may fail.`)
|
|
3590
|
+
}
|
|
3591
|
+
|
|
3592
|
+
const envExists = fs.existsSync(path.join(repoRoot, ".env")) || fs.existsSync(path.join(repoRoot, ".env.local"))
|
|
3593
|
+
if (!envExists) {
|
|
3594
|
+
runnerBaseLogger.warn("runner.env.check", "No .env or .env.local found in repo root. The AI agent will not have access to local environment variables.", {
|
|
3595
|
+
meta: { repo_root: repoRoot },
|
|
3596
|
+
})
|
|
3597
|
+
}
|
|
3598
|
+
}
|
|
3599
|
+
|
|
3600
|
+
async function main() {
|
|
3601
|
+
await verifyEnvironmentHealth()
|
|
3602
|
+
let state = cloudState ?? await ensureRegisteredRunner()
|
|
3603
|
+
getRunnerLogger(state).info("runner.ready", "Runner ready", {
|
|
3604
|
+
meta: {
|
|
3605
|
+
local_executor: state.local_executor,
|
|
3606
|
+
},
|
|
3607
|
+
})
|
|
3608
|
+
|
|
3609
|
+
if (registerOnly) return
|
|
3610
|
+
|
|
3611
|
+
// On startup, reset any runs this runner left in "running" state from a previous crash
|
|
3612
|
+
const resetResult = await withRunnerAuth((runnerState) => resetStaleRuns(runnerState)).catch((error) => {
|
|
3613
|
+
getRunnerLogger(state).warn("runner.reset_stale.failed", "Failed to reset stale runs on startup", undefined, error)
|
|
3614
|
+
return null
|
|
3615
|
+
})
|
|
3616
|
+
if (resetResult?.reset > 0) {
|
|
3617
|
+
getRunnerLogger(state).warn("runner.reset_stale.requeued", "Re-queued stale runs left behind by a previous runner crash", {
|
|
3618
|
+
meta: {
|
|
3619
|
+
reset_run_count: resetResult.reset,
|
|
3620
|
+
run_ids: Array.isArray(resetResult.run_ids) ? resetResult.run_ids : [],
|
|
3621
|
+
},
|
|
3622
|
+
})
|
|
3623
|
+
}
|
|
3624
|
+
|
|
3625
|
+
const startedAtMs = Date.now()
|
|
3626
|
+
let launchedRuns = 0
|
|
3627
|
+
let shuttingDown = false
|
|
3628
|
+
const activeRuns = new Map()
|
|
3629
|
+
let lastCheckoutCleanup = 0
|
|
3630
|
+
let currentControlState = "active"
|
|
3631
|
+
|
|
3632
|
+
async function withRunnerAuth(operation) {
|
|
3633
|
+
try {
|
|
3634
|
+
return await operation(state)
|
|
3635
|
+
} catch (error) {
|
|
3636
|
+
if (!isInvalidRunnerCredentialsError(error) || !workspaceApiKey) {
|
|
3637
|
+
throw error
|
|
3638
|
+
}
|
|
3639
|
+
|
|
3640
|
+
getRunnerLogger(state).warn("runner.credentials.invalid", "Runner credentials were rejected; re-registering", undefined, error)
|
|
3641
|
+
clearState()
|
|
3642
|
+
state = await registerRunner()
|
|
3643
|
+
getRunnerLogger(state).info("runner.reregistered", "Runner re-registered", {
|
|
3644
|
+
meta: {
|
|
3645
|
+
local_executor: state.local_executor,
|
|
3646
|
+
},
|
|
3647
|
+
})
|
|
3648
|
+
return operation(state)
|
|
3649
|
+
}
|
|
3650
|
+
}
|
|
3651
|
+
|
|
3652
|
+
async function heartbeatAndSync(status) {
|
|
3653
|
+
try {
|
|
3654
|
+
const payload = await withRunnerAuth((runnerState) => heartbeat(runnerState, status))
|
|
3655
|
+
currentControlState = payload?.control_state || currentControlState
|
|
3656
|
+
return payload
|
|
3657
|
+
} catch {
|
|
3658
|
+
return null
|
|
3659
|
+
}
|
|
3660
|
+
}
|
|
3661
|
+
|
|
3662
|
+
async function retireRunner() {
|
|
3663
|
+
if (activeRuns.size > 0) return false
|
|
3664
|
+
try {
|
|
3665
|
+
await withRunnerAuth((runnerState) => deregisterRunner(runnerState))
|
|
3666
|
+
const retiredRunnerId = state.runner_id
|
|
3667
|
+
clearState()
|
|
3668
|
+
getRunnerLogger({
|
|
3669
|
+
...state,
|
|
3670
|
+
runner_id: retiredRunnerId,
|
|
3671
|
+
}).info("runner.retired", "Runner retired")
|
|
3672
|
+
return true
|
|
3673
|
+
} catch (error) {
|
|
3674
|
+
getRunnerLogger(state).warn("runner.deregister.failed", "Runner deregistration failed", undefined, error)
|
|
3675
|
+
return false
|
|
3676
|
+
}
|
|
3677
|
+
}
|
|
3678
|
+
|
|
3679
|
+
const stop = async () => {
|
|
3680
|
+
if (shuttingDown) return
|
|
3681
|
+
shuttingDown = true
|
|
3682
|
+
try {
|
|
3683
|
+
await Promise.allSettled([...activeRuns.values()])
|
|
3684
|
+
const retired = await retireRunner()
|
|
3685
|
+
if (!retired) {
|
|
3686
|
+
await heartbeat(state, "offline").catch(() => {})
|
|
3687
|
+
}
|
|
3688
|
+
} catch {}
|
|
3689
|
+
process.exit(0)
|
|
3690
|
+
}
|
|
3691
|
+
|
|
3692
|
+
process.on("SIGINT", stop)
|
|
3693
|
+
process.on("SIGTERM", stop)
|
|
3694
|
+
|
|
3695
|
+
while (!shuttingDown) {
|
|
3696
|
+
const runnerStatus = activeRuns.size > 0 ? "busy" : "online"
|
|
3697
|
+
await heartbeatAndSync(runnerStatus)
|
|
3698
|
+
let claimFailed = false
|
|
3699
|
+
|
|
3700
|
+
if (currentControlState === "draining" && activeRuns.size === 0) {
|
|
3701
|
+
getRunnerLogger(state).info("runner.drain.ready", "Retire requested and no active work remains")
|
|
3702
|
+
break
|
|
3703
|
+
}
|
|
3704
|
+
|
|
3705
|
+
// Tidy finished tickets' checkouts while nothing is running, so a cleanup
|
|
3706
|
+
// can never race a run that is using one.
|
|
3707
|
+
if (activeRuns.size === 0 && Date.now() - lastCheckoutCleanup > CHECKOUT_CLEANUP_INTERVAL_MS) {
|
|
3708
|
+
lastCheckoutCleanup = Date.now()
|
|
3709
|
+
await cleanupFinishedCheckouts(state, getRunnerLogger(state)).catch((error) => {
|
|
3710
|
+
getRunnerLogger(state).warn("runner.cleanup.failed", "Cleaning up finished checkouts failed", undefined, error)
|
|
3711
|
+
})
|
|
3712
|
+
}
|
|
3713
|
+
|
|
3714
|
+
// Paused for this machine's usage limit: its subscription takes nothing
|
|
3715
|
+
// until the reset. Work paid with a key or token is not limited by it, so
|
|
3716
|
+
// ask once a minute — the server offers only that kind while paused.
|
|
3717
|
+
const pausedFor = usageLimitPausedUntil - Date.now()
|
|
3718
|
+
if (pausedFor > 0) {
|
|
3719
|
+
await sleep(Math.min(pausedFor, 60_000))
|
|
3720
|
+
if (shuttingDown) continue
|
|
3721
|
+
}
|
|
3722
|
+
|
|
3723
|
+
while (!shuttingDown && activeRuns.size < maxParallelRuns && launchedRuns < maxRuns) {
|
|
3724
|
+
let claim = null
|
|
3725
|
+
try {
|
|
3726
|
+
claim = await withRunnerAuth((runnerState) => claimNext({ ...runnerState }, activeRuns.size))
|
|
3727
|
+
} catch (error) {
|
|
3728
|
+
claimFailed = true
|
|
3729
|
+
getRunnerLogger(state).warn("runner.claim.failed", "Failed to claim next run; will retry", {
|
|
3730
|
+
meta: {
|
|
3731
|
+
active_run_count: activeRuns.size,
|
|
3732
|
+
},
|
|
3733
|
+
}, error)
|
|
3734
|
+
await sleep(Math.min(pollIntervalMs, CLAIM_ERROR_BACKOFF_MS))
|
|
3735
|
+
break
|
|
3736
|
+
}
|
|
3737
|
+
currentControlState = claim.control_state || currentControlState
|
|
3738
|
+
if (claim.backpressure === "backoff" && claim.available_after) {
|
|
3739
|
+
// The server holds this runner back (a usage limit reported earlier,
|
|
3740
|
+
// possibly before a restart). Wait it out instead of asking again.
|
|
3741
|
+
const until = new Date(claim.available_after).getTime()
|
|
3742
|
+
if (until > Date.now() && until > usageLimitPausedUntil) {
|
|
3743
|
+
usageLimitPausedUntil = until
|
|
3744
|
+
getRunnerLogger(state).info("runner.paused", "Paused until the usage limit resets", {
|
|
3745
|
+
meta: { until: new Date(until).toISOString() },
|
|
3746
|
+
})
|
|
3747
|
+
}
|
|
3748
|
+
}
|
|
3749
|
+
if (!claim.run) break
|
|
3750
|
+
|
|
3751
|
+
const run = claim.run
|
|
3752
|
+
|
|
3753
|
+
getRunLogger(state, run).info("runner.claimed", "Claimed run", {
|
|
3754
|
+
meta: {
|
|
3755
|
+
issue_identifier: run.task?.identifier || run.task_id,
|
|
3756
|
+
},
|
|
3757
|
+
})
|
|
3758
|
+
await postIssueMilestone(
|
|
3759
|
+
runMcpKey(run, state),
|
|
3760
|
+
run.task_id,
|
|
3761
|
+
run.run_kind === "planner"
|
|
3762
|
+
? `Planner run claimed by ${runnerName}.`
|
|
3763
|
+
: `Run claimed by ${runnerName}.`,
|
|
3764
|
+
"info",
|
|
3765
|
+
)
|
|
3766
|
+
if (run.activity_thread_comment_id) {
|
|
3767
|
+
await callMcpTool(
|
|
3768
|
+
runMcpKey(run, state),
|
|
3769
|
+
"add_comment",
|
|
3770
|
+
{
|
|
3771
|
+
issue_id: run.task_id,
|
|
3772
|
+
content: `${run.run_kind === "planner" ? "Planner run" : "Run"} claimed by **${runnerName}**.`,
|
|
3773
|
+
parent_id: run.activity_thread_comment_id,
|
|
3774
|
+
},
|
|
3775
|
+
run.agent_id,
|
|
3776
|
+
).catch(() => {})
|
|
3777
|
+
}
|
|
3778
|
+
launchedRuns += 1
|
|
3779
|
+
|
|
3780
|
+
const runPromise = runExecutorScope.run(
|
|
3781
|
+
executorForRun(run),
|
|
3782
|
+
() => run.run_kind === "planner" ? runPlannerExecutor(run, state) : runExecutor(run, state),
|
|
3783
|
+
)
|
|
3784
|
+
.catch(async (error) => {
|
|
3785
|
+
// A failure before the executor spawns (worktree setup, repo
|
|
3786
|
+
// resolution) never reaches runExecutor's own handler, so the run
|
|
3787
|
+
// would sit in "running" until the 4-hour timeout. Mark it failed.
|
|
3788
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
3789
|
+
getRunnerLogger(state).error(
|
|
3790
|
+
"runner.run.setup_failed",
|
|
3791
|
+
"Run failed before the executor started",
|
|
3792
|
+
{ meta: { run_id: run.id } },
|
|
3793
|
+
error,
|
|
3794
|
+
)
|
|
3795
|
+
await completeRun(state, run.id, {
|
|
3796
|
+
status: "error",
|
|
3797
|
+
error_message: truncate(message, 4000),
|
|
3798
|
+
}).catch(() => {})
|
|
3799
|
+
})
|
|
3800
|
+
.finally(() => {
|
|
3801
|
+
activeRuns.delete(run.id)
|
|
3802
|
+
})
|
|
3803
|
+
|
|
3804
|
+
activeRuns.set(run.id, runPromise)
|
|
3805
|
+
}
|
|
3806
|
+
|
|
3807
|
+
if (claimFailed) {
|
|
3808
|
+
continue
|
|
3809
|
+
}
|
|
3810
|
+
|
|
3811
|
+
if (currentControlState === "draining" && activeRuns.size === 0) {
|
|
3812
|
+
getRunnerLogger(state).info("runner.drain.idle", "Retire requested and runner is now idle")
|
|
3813
|
+
break
|
|
3814
|
+
}
|
|
3815
|
+
|
|
3816
|
+
if (launchedRuns >= maxRuns && activeRuns.size === 0) break
|
|
3817
|
+
if (once && activeRuns.size === 0 && launchedRuns > 0) break
|
|
3818
|
+
// A cloud job exists for one run. If that run cannot be claimed — it was
|
|
3819
|
+
// cancelled, or already taken — stop instead of spending Actions minutes.
|
|
3820
|
+
if (cloudState && launchedRuns === 0 && Date.now() - startedAtMs > 60_000) {
|
|
3821
|
+
getRunnerLogger(state).error("runner.cloud.no_run", "The run this cloud job was started for could not be claimed")
|
|
3822
|
+
process.exitCode = 1
|
|
3823
|
+
break
|
|
3824
|
+
}
|
|
3825
|
+
|
|
3826
|
+
if (activeRuns.size === 0) {
|
|
3827
|
+
await sleep(pollIntervalMs)
|
|
3828
|
+
continue
|
|
3829
|
+
}
|
|
3830
|
+
|
|
3831
|
+
await Promise.race([
|
|
3832
|
+
sleep(pollIntervalMs),
|
|
3833
|
+
...activeRuns.values(),
|
|
3834
|
+
])
|
|
3835
|
+
}
|
|
3836
|
+
|
|
3837
|
+
await Promise.allSettled([...activeRuns.values()])
|
|
3838
|
+
const retired = await retireRunner()
|
|
3839
|
+
if (!retired) {
|
|
3840
|
+
await heartbeat(state, "online").catch(() => {})
|
|
3841
|
+
}
|
|
3842
|
+
}
|
|
3843
|
+
|
|
3844
|
+
main().catch(async error => {
|
|
3845
|
+
runnerBaseLogger.fatal("runner.crashed", "Local runner crashed", undefined, error)
|
|
3846
|
+
process.exit(1)
|
|
3847
|
+
})
|