pacework 1.7.2 → 1.7.4

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.
@@ -33,7 +33,23 @@ import { AVAILABILITY_MODES, describeRegistration, isRevokedError, registrationC
33
33
  import { describeTriageJob, runTriageLoop, TRIAGE_EXECUTORS } from "../lib/triage-worker.mjs"
34
34
  import { automationDenyRules, automationLimits } from "../lib/automation-limits.mjs"
35
35
  import { truncateSummary } from "../lib/summary-text.mjs"
36
+ import { activityForStreamEvent, createActivityTracker, createStreamReader, helpSupportsStreamJson, parseClaudeJsonResult } from "../lib/claude-activity.mjs"
36
37
  import { RUNNER_VERSION } from "../lib/version.mjs"
38
+ import {
39
+ buildFixInstructions,
40
+ checkEnvironment,
41
+ DATABASE_URL_ENV,
42
+ DEFAULT_MAX_FIX_ATTEMPTS,
43
+ DEFAULT_WORKFLOW_PATH,
44
+ execLocalCheck,
45
+ failedChecks,
46
+ formatLocalChecks,
47
+ parseCiWorkflow,
48
+ planLocalChecks,
49
+ runChecksUntilGreen,
50
+ runLocalChecks,
51
+ skipWholePlan,
52
+ } from "../lib/local-checks.mjs"
37
53
 
38
54
  function parseArgs(argv) {
39
55
  const args = {}
@@ -454,39 +470,6 @@ function truncateHead(value, max = 40000) {
454
470
  return `${value.slice(0, max)}\n\n... diff excerpt truncated (${omitted} more chars omitted)`
455
471
  }
456
472
 
457
- /**
458
- * Claude Code's --output-format json result: the final message and what the run
459
- * cost. Null when the output is not that JSON (an older CLI, a crash).
460
- */
461
- function parseClaudeJsonResult(stdout) {
462
- const lines = String(stdout || "").trim().split(/\r?\n/).reverse()
463
- for (const line of lines) {
464
- if (!line.trim().startsWith("{")) continue
465
- try {
466
- const parsed = JSON.parse(line)
467
- if (parsed?.type !== "result") continue
468
- const usage = parsed.usage ?? {}
469
- const inputTokens = [usage.input_tokens, usage.cache_creation_input_tokens, usage.cache_read_input_tokens]
470
- .filter((value) => typeof value === "number")
471
- return {
472
- text: typeof parsed.result === "string" ? parsed.result : "",
473
- isError: parsed.is_error === true,
474
- // "error_max_budget_usd" when --max-budget-usd ended the run.
475
- subtype: typeof parsed.subtype === "string" ? parsed.subtype : null,
476
- usage: {
477
- cost_usd: typeof parsed.total_cost_usd === "number" ? parsed.total_cost_usd : null,
478
- input_tokens: inputTokens.length ? inputTokens.reduce((a, b) => a + b, 0) : null,
479
- output_tokens: typeof usage.output_tokens === "number" ? usage.output_tokens : null,
480
- num_turns: typeof parsed.num_turns === "number" ? parsed.num_turns : null,
481
- },
482
- }
483
- } catch {
484
- // not the result line
485
- }
486
- }
487
- return null
488
- }
489
-
490
473
  function formatChildExitError(executor, code, output) {
491
474
  const tail = truncate(String(output || "").trim(), 4000)
492
475
  return `${executor} exited with code ${code}${tail ? `\n\nLast output:\n${tail}` : ""}`
@@ -1089,8 +1072,15 @@ function runMcpKey(run, state) {
1089
1072
  return run.mcp_api_key || run.planner_api_key || state.runner_mcp_key
1090
1073
  }
1091
1074
 
1092
- async function postRunLog(state, runId, logTail) {
1093
- return requestJson(`${appUrl}/api/agent-runners/runs/${runId}/log`, {
1075
+ /** False once the server refused an activity report: an older server rejects unknown fields. */
1076
+ let serverTakesActivity = true
1077
+
1078
+ /**
1079
+ * @param {{ activity: string, activity_at: string } | null} [activity] What the agent
1080
+ * is doing now (PWK-317). Only the value and when it changed, never content.
1081
+ */
1082
+ async function postRunLog(state, runId, logTail, activity = null) {
1083
+ const send = (withActivity) => requestJson(`${appUrl}/api/agent-runners/runs/${runId}/log`, {
1094
1084
  method: "POST",
1095
1085
  headers: {
1096
1086
  "Content-Type": "application/json",
@@ -1102,8 +1092,18 @@ async function postRunLog(state, runId, logTail) {
1102
1092
  run_id: runId,
1103
1093
  log_tail: truncate(logTail, 12000),
1104
1094
  heartbeat_at: new Date().toISOString(),
1095
+ ...(withActivity ? activity : {}),
1105
1096
  }),
1106
1097
  })
1098
+ if (!activity || !serverTakesActivity) return send(false)
1099
+ try {
1100
+ return await send(true)
1101
+ } catch (error) {
1102
+ if (error?.status !== 400) throw error
1103
+ // A server without activity support: stop sending it, and still deliver the log.
1104
+ serverTakesActivity = false
1105
+ return send(false)
1106
+ }
1107
1107
  }
1108
1108
 
1109
1109
  function omitNullishEntries(record) {
@@ -1341,6 +1341,69 @@ async function runVerificationPlan(worktreePath, changedFiles, profile) {
1341
1341
  return { summary, steps }
1342
1342
  }
1343
1343
 
1344
+ /**
1345
+ * The checks the repository's CI workflow lists, or null when local checks are
1346
+ * off (runner profile `localChecks: false`, or PACEWORK_LOCAL_CHECKS=off) or the
1347
+ * repository has no workflow they can be read from.
1348
+ */
1349
+ function loadLocalChecks(worktreePath, profile) {
1350
+ const setting = profile.localChecks
1351
+ if (setting === false || setting?.enabled === false) return null
1352
+ if (/^(off|false|0|no)$/i.test(process.env.PACEWORK_LOCAL_CHECKS || "")) return null
1353
+ const source = (typeof setting?.workflow === "string" && setting.workflow) || DEFAULT_WORKFLOW_PATH
1354
+ const file = path.resolve(worktreePath, source)
1355
+ // The workflow is read from the checkout, never from outside it.
1356
+ if (path.relative(worktreePath, file).startsWith("..")) return null
1357
+ try {
1358
+ const workflow = parseCiWorkflow(fs.readFileSync(file, "utf8"))
1359
+ if (!workflow) return null
1360
+ const maxFixAttempts = Number.isInteger(setting?.maxFixAttempts) && setting.maxFixAttempts >= 0
1361
+ ? setting.maxFixAttempts
1362
+ : DEFAULT_MAX_FIX_ATTEMPTS
1363
+ return { workflow, source, maxFixAttempts }
1364
+ } catch {
1365
+ return null
1366
+ }
1367
+ }
1368
+
1369
+ /**
1370
+ * Every file this branch changes against the base branch, committed or not: what
1371
+ * CI's change detection sees. `baseFound` is false when no base ref exists in the
1372
+ * checkout; then only uncommitted work is listed.
1373
+ */
1374
+ async function listBranchChangedFiles(worktreePath, baseBranch = null) {
1375
+ const git = async (argsList) => (await execCommandSafe("git", ["-C", worktreePath, ...argsList], worktreePath))?.stdout ?? ""
1376
+ let base = ""
1377
+ for (const ref of ["origin/HEAD", baseBranch ? `origin/${baseBranch}` : null].filter(Boolean)) {
1378
+ base = (await git(["merge-base", ref, "HEAD"])).trim()
1379
+ if (base) break
1380
+ }
1381
+ const lists = await Promise.all([
1382
+ git(["diff", "--name-only", base || "HEAD"]),
1383
+ git(["ls-files", "--others", "--exclude-standard"]),
1384
+ ])
1385
+ const files = [...new Set(lists.join("\n").split(/\r?\n/).map(line => line.trim()).filter(Boolean))]
1386
+ return { files, baseFound: Boolean(base) }
1387
+ }
1388
+
1389
+ /**
1390
+ * Why a sandboxed run runs none of its local checks. Their commands come from
1391
+ * the branch, which the agent can edit, and the runner cannot put them inside
1392
+ * the sandbox the agent's tool gives the agent. Running them outside it would
1393
+ * hand the agent a way out, so they are left to CI.
1394
+ */
1395
+ const SANDBOXED_RUN_SKIP = "this run is sandboxed and the runner cannot run checks inside its sandbox"
1396
+
1397
+ /** Sum two usage records from the same run; a value missing on both sides stays null. */
1398
+ function addUsage(a, b) {
1399
+ const sum = { ...a, ...b }
1400
+ for (const key of Object.keys(sum)) {
1401
+ if (typeof a[key] === "number" && typeof b[key] === "number") sum[key] = a[key] + b[key]
1402
+ else if (typeof a[key] === "number" && b[key] == null) sum[key] = a[key]
1403
+ }
1404
+ return sum
1405
+ }
1406
+
1344
1407
  function formatCommitMessage(run, profile) {
1345
1408
  const template = profile.git?.commitMessageTemplate || "{identifier} {title}"
1346
1409
  const identifier = run.task?.identifier || run.task_id
@@ -2133,23 +2196,37 @@ function claudeRunSettings(run) {
2133
2196
  return Object.keys(settings).length ? JSON.stringify(settings) : null
2134
2197
  }
2135
2198
 
2136
- /** Whether this machine's Claude Code takes --max-budget-usd; an older one would refuse to start. */
2137
- let claudeMaxBudgetSupport = null
2138
- function claudeSupportsMaxBudget() {
2139
- if (claudeMaxBudgetSupport === null) {
2199
+ /** This machine's `claude --help`, read once; empty when it cannot be read. */
2200
+ let claudeHelp = null
2201
+ function claudeHelpText() {
2202
+ if (claudeHelp === null) {
2140
2203
  try {
2141
- const help = execFileSync(claudeCodeBin, ["--help"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 15_000 })
2142
- claudeMaxBudgetSupport = help.includes("--max-budget-usd")
2204
+ claudeHelp = execFileSync(claudeCodeBin, ["--help"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 15_000 })
2143
2205
  } catch {
2144
- claudeMaxBudgetSupport = false
2206
+ claudeHelp = ""
2145
2207
  }
2146
2208
  }
2147
- return claudeMaxBudgetSupport
2209
+ return claudeHelp
2210
+ }
2211
+
2212
+ /** Whether this machine's Claude Code takes --max-budget-usd; an older one would refuse to start. */
2213
+ function claudeSupportsMaxBudget() {
2214
+ return claudeHelpText().includes("--max-budget-usd")
2215
+ }
2216
+
2217
+ /**
2218
+ * Whether this machine's Claude Code streams events (`--output-format stream-json`
2219
+ * with `--verbose`), which is how a run reports what it is doing. An older one
2220
+ * runs with the single JSON result and reports no activity.
2221
+ */
2222
+ function claudeSupportsStreamJson() {
2223
+ return helpSupportsStreamJson(claudeHelpText())
2148
2224
  }
2149
2225
 
2150
2226
  /**
2151
- * @param {{ maxCostUsd?: number | null }} [options] The run's cost limit, for
2152
- * Claude Code: it ends the run itself once the limit is spent.
2227
+ * @param {{ maxCostUsd?: number | null, streamActivity?: boolean }} [options] The run's
2228
+ * cost limit, for Claude Code: it ends the run itself once the limit is spent.
2229
+ * streamActivity: Claude Code streams events, so the run can report what it is doing.
2153
2230
  */
2154
2231
  function buildExecutorCommand(run, worktreePath, prompt, outputFile, mcpKey, options = {}) {
2155
2232
  const requestedModel = resolveRequestedModel(run)
@@ -2204,9 +2281,9 @@ function buildExecutorCommand(run, worktreePath, prompt, outputFile, mcpKey, opt
2204
2281
  const argsList = [
2205
2282
  "-p",
2206
2283
  prompt,
2207
- // json: the same final message, plus what the run cost.
2208
- "--output-format",
2209
- "json",
2284
+ // json: the same final message, plus what the run cost. stream-json ends with
2285
+ // that same result as its last event, and shows what the agent does on the way.
2286
+ ...(options.streamActivity ? ["--output-format", "stream-json", "--verbose"] : ["--output-format", "json"]),
2210
2287
  "--permission-mode",
2211
2288
  "acceptEdits",
2212
2289
  "--dangerously-skip-permissions",
@@ -2234,7 +2311,7 @@ function buildExecutorCommand(run, worktreePath, prompt, outputFile, mcpKey, opt
2234
2311
  command: claudeCodeBin,
2235
2312
  args: argsList,
2236
2313
  stdin: null,
2237
- summaryMode: "claude_json",
2314
+ summaryMode: options.streamActivity ? "claude_stream" : "claude_json",
2238
2315
  }
2239
2316
  }
2240
2317
 
@@ -3183,9 +3260,12 @@ async function runExecutor(run, state) {
3183
3260
  buildPrompt(run, branch, worktreePath, { localCheckoutMode, gitAvailable, workspaceContext, runLimitsText }),
3184
3261
  skillsSection,
3185
3262
  ].filter(Boolean).join("\n\n")
3186
- const execSpec = buildExecutorCommand(run, worktreePath, prompt, outputFile, mcpKey, {
3263
+ const executorOptions = {
3187
3264
  maxCostUsd: costLimitApplies ? runLimits.maxCostUsd : null,
3188
- })
3265
+ streamActivity: activeExecutor() === "claude_code" && claudeSupportsStreamJson(),
3266
+ }
3267
+ // Replaced when the builder is sent back to fix failing local checks.
3268
+ let execSpec = buildExecutorCommand(run, worktreePath, prompt, outputFile, mcpKey, executorOptions)
3189
3269
  // These steps run before the executor spawns; when one stalls the run sits in
3190
3270
  // "running" with no output, so each is traced.
3191
3271
  runLogger.info("runner.run.prepare", "Fetching MCP servers")
@@ -3207,8 +3287,31 @@ async function runExecutor(run, state) {
3207
3287
  let stdioSummary = ""
3208
3288
  // Claude Code's JSON result arrives on stdout only; stderr would break parsing.
3209
3289
  let stdoutRaw = ""
3290
+ const claudeResultMode = execSpec.summaryMode === "claude_json" || execSpec.summaryMode === "claude_stream"
3291
+ /** What the agent is doing, from Claude Code's event stream (PWK-317). Null for every other executor. */
3292
+ let activity = null
3293
+ let streamReader = null
3294
+ const startStreamReader = () => {
3295
+ if (execSpec.summaryMode !== "claude_stream") return
3296
+ activity = createActivityTracker()
3297
+ streamReader = createStreamReader({
3298
+ onEvent: (event) => activity.observe(activityForStreamEvent(event)),
3299
+ // Not an event: something the CLI printed itself, kept for the log as before.
3300
+ onOtherLine: (line) => {
3301
+ appendLogTail(logState, line)
3302
+ stdioSummary = truncate(`${stdioSummary}\n${line}`, 16000)
3303
+ },
3304
+ })
3305
+ }
3306
+ startStreamReader()
3210
3307
  let runUsage = null
3308
+ /** What the checks from the CI workflow found on this machine, for the run summary. */
3309
+ let localChecksSummary = ""
3310
+ /** Usage of the agent processes that already finished in this run (fix rounds start another). */
3311
+ let earlierUsage = null
3211
3312
  let cancelRequested = false
3313
+ /** Set when the run timed out or runExecutor is leaving: nothing may start a check or an agent after it. */
3314
+ let runEnded = false
3212
3315
  let childProcess = null
3213
3316
  let lastHeartbeatLogAt = 0
3214
3317
 
@@ -3237,7 +3340,9 @@ async function runExecutor(run, state) {
3237
3340
 
3238
3341
  async function flushRunnerLog() {
3239
3342
  appendHeartbeat()
3240
- const response = await postRunLog(state, run.id, logState.tail)
3343
+ const report = activity && childProcess && !childProcess.killed ? activity.due() : null
3344
+ const response = await postRunLog(state, run.id, logState.tail, report)
3345
+ if (report) activity.markSent(report)
3241
3346
  if (runEndedOnServer(response)) {
3242
3347
  cancelRequested = true
3243
3348
  if (childProcess && !childProcess.killed) {
@@ -3285,6 +3390,7 @@ async function runExecutor(run, state) {
3285
3390
  let runTimeoutHandle = null
3286
3391
  const runTimeoutPromise = new Promise((_, reject) => {
3287
3392
  runTimeoutHandle = setTimeout(() => {
3393
+ runEnded = true
3288
3394
  if (childProcess && !childProcess.killed) killChildProcessTree(childProcess, "SIGKILL")
3289
3395
  reject(new Error(`Run timed out after ${Math.round(RUN_TIMEOUT_MS / 60000)} minutes`))
3290
3396
  }, RUN_TIMEOUT_MS)
@@ -3366,7 +3472,8 @@ async function runExecutor(run, state) {
3366
3472
  appendLogTail(logState, `> command: ${previewExecutorCommand(execSpec)}`)
3367
3473
  await flushRunnerLog().catch(() => {})
3368
3474
  executorPhase = "running"
3369
- await Promise.race([runTimeoutPromise, new Promise((resolve, reject) => {
3475
+ const runAgent = () => Promise.race([runTimeoutPromise, new Promise((resolve, reject) => {
3476
+ if (runEnded) { reject(new Error("The run already ended.")); return }
3370
3477
  const child = spawn(execSpec.command, execSpec.args, {
3371
3478
  cwd: worktreePath,
3372
3479
  stdio: ["pipe", "pipe", "pipe"],
@@ -3385,6 +3492,9 @@ async function runExecutor(run, state) {
3385
3492
 
3386
3493
  child.stdout.on("data", chunk => {
3387
3494
  const text = chunk.toString()
3495
+ // The event stream stays on this machine: only the final result is kept,
3496
+ // and only lines that are not events reach the log.
3497
+ if (streamReader) { streamReader.push(text); return }
3388
3498
  if (execSpec.summaryMode === "claude_json") stdoutRaw = truncate(`${stdoutRaw}${text}`, 2_000_000)
3389
3499
  appendLogTail(logState, text)
3390
3500
  stdioSummary = truncate(`${stdioSummary}\n${text}`, 16000)
@@ -3400,12 +3510,20 @@ async function runExecutor(run, state) {
3400
3510
  })
3401
3511
  child.on("close", code => {
3402
3512
  appendLogTail(logState, `> executor exited with code ${code}`)
3403
- if (execSpec.summaryMode === "claude_json") {
3513
+ // The agent is no longer doing anything; the server clears the value when the run ends.
3514
+ activity = null
3515
+ if (streamReader) {
3516
+ streamReader.end()
3517
+ stdoutRaw = streamReader.resultText()
3518
+ }
3519
+ if (claudeResultMode) {
3404
3520
  const result = parseClaudeJsonResult(stdoutRaw)
3405
3521
  if (result) {
3406
3522
  // Claude Code prices every run at Anthropic's rates; for
3407
3523
  // another provider that number is wrong, so keep tokens only.
3408
- runUsage = usesThirdPartyModels(run) ? { ...result.usage, cost_usd: null } : result.usage
3524
+ const usage = usesThirdPartyModels(run) ? { ...result.usage, cost_usd: null } : result.usage
3525
+ // A fix round is a second agent process; the run's usage is the sum.
3526
+ runUsage = earlierUsage ? addUsage(earlierUsage, usage) : usage
3409
3527
  // The message, not the JSON around it, is what the
3410
3528
  // summary, the PR description and failure detection read.
3411
3529
  stdioSummary = result.text
@@ -3442,6 +3560,105 @@ async function runExecutor(run, state) {
3442
3560
  reject(new Error(formatChildExitError(activeExecutor(), code, stdioSummary)))
3443
3561
  })
3444
3562
  })])
3563
+ await runAgent()
3564
+
3565
+ // Before anything is committed or pushed, run the checks CI would run for
3566
+ // the changed areas on this machine. A failing check goes back to the
3567
+ // builder; a check this machine cannot run is noted and left to CI.
3568
+ const localChecks = !isReviewRun(run) && gitAvailable ? loadLocalChecks(worktreePath, runnerProfile) : null
3569
+ if (localChecks) {
3570
+ const stopRequested = () => runEnded || cancelRequested || Boolean(limitStop)
3571
+ // The checks run scripts from the branch, which the agent can edit: a
3572
+ // sandboxed run does not run them, and no run hands them its
3573
+ // credentials or the repository's secrets.
3574
+ const skipAll = runSandboxed(run) ? SANDBOXED_RUN_SKIP : null
3575
+ const checkEnv = checkEnvironment(process.env)
3576
+ let baseNote = ""
3577
+ const outcome = await Promise.race([runTimeoutPromise, runChecksUntilGreen({
3578
+ maxFixAttempts: localChecks.maxFixAttempts,
3579
+ shouldStop: stopRequested,
3580
+ check: async () => {
3581
+ const changed = await listBranchChangedFiles(worktreePath, run.__baseBranch)
3582
+ baseNote = changed.baseFound ? "" : "The base branch was not found in this checkout, so only uncommitted changes decided which checks ran."
3583
+ const planned = planLocalChecks(localChecks.workflow, changed.files, { databaseUrl: process.env[DATABASE_URL_ENV] || null })
3584
+ const plan = skipAll ? skipWholePlan(planned, skipAll) : planned
3585
+ if (!plan.length) return []
3586
+ // Not the AI tool: a failure here is never a usage limit.
3587
+ executorPhase = "finished"
3588
+ appendLogTail(logState, `> running local checks from ${localChecks.source}`)
3589
+ await flushRunnerLog().catch(() => {})
3590
+ const results = await runLocalChecks(plan, {
3591
+ cwd: worktreePath,
3592
+ resolveCwd: (root, dir) => path.resolve(root, dir),
3593
+ shouldStop: stopRequested,
3594
+ // The cancel, limit and timeout paths stop whatever childProcess points at.
3595
+ exec: (command, options) => {
3596
+ if (stopRequested()) return Promise.resolve({ code: null, output: "the run was stopped", timedOut: false })
3597
+ return execLocalCheck(command, {
3598
+ ...options,
3599
+ baseEnv: checkEnv,
3600
+ onChild: (child) => { childProcess = child },
3601
+ })
3602
+ },
3603
+ })
3604
+ for (const result of results) {
3605
+ appendLogTail(logState, `> local check ${result.status}: ${result.label}${result.note ? ` (${result.note})` : ""}`)
3606
+ }
3607
+ return results
3608
+ },
3609
+ fix: async (failed, attempt) => {
3610
+ if (runEnded) throw new Error("The run already ended.")
3611
+ if (cancelRequested) throw new Error("Canceled by admin.")
3612
+ if (limitStop) throw new Error(tripwireHeadline(limitStop))
3613
+ const names = failed.map(result => result.label).join("; ")
3614
+ runLogger.warn("runner.local_checks.failed", `Local checks failed, sending them back to the builder (round ${attempt})`, {
3615
+ meta: { failed: failed.map(result => result.label), attempt },
3616
+ })
3617
+ await postIssueMilestone(
3618
+ mcpKey,
3619
+ run.task_id,
3620
+ `Local checks failed before the push: ${truncate(names, 400)}. Nothing was pushed; the builder is fixing them (round ${attempt} of ${localChecks.maxFixAttempts}).`,
3621
+ "warning",
3622
+ ).catch(() => {})
3623
+ const spent = runUsage?.cost_usd ?? 0
3624
+ earlierUsage = runUsage
3625
+ stdioSummary = ""
3626
+ stdoutRaw = ""
3627
+ if (execSpec.summaryMode === "file") fs.rmSync(outputFile, { force: true })
3628
+ execSpec = buildExecutorCommand(
3629
+ run,
3630
+ worktreePath,
3631
+ `${prompt}\n\n${buildFixInstructions(failed, { attempt, maxFixAttempts: localChecks.maxFixAttempts, source: localChecks.source })}`,
3632
+ outputFile,
3633
+ mcpKey,
3634
+ // The cost limit is for the whole run, so a fix round gets what is left of it.
3635
+ { ...executorOptions, maxCostUsd: executorOptions.maxCostUsd === null ? null : Math.max(0.01, executorOptions.maxCostUsd - spent) },
3636
+ )
3637
+ startStreamReader()
3638
+ appendLogTail(logState, `> local checks failed (${names}); starting the builder again to fix them`)
3639
+ executorPhase = "running"
3640
+ await runAgent()
3641
+ },
3642
+ })])
3643
+ if (cancelRequested) throw new Error("Canceled by admin.")
3644
+ if (limitStop) throw new Error(tripwireHeadline(limitStop))
3645
+ if (outcome.stopped) throw new Error("The run ended while its local checks were running.")
3646
+ localChecksSummary = [
3647
+ formatLocalChecks(outcome.results, { fixAttempts: outcome.fixAttempts, source: localChecks.source }),
3648
+ outcome.results.length ? baseNote : "",
3649
+ ].filter(Boolean).join("\n")
3650
+ if (!outcome.passed) {
3651
+ const failed = failedChecks(outcome.results)
3652
+ throw new Error(
3653
+ `Local checks still fail after ${outcome.fixAttempts} fix round${outcome.fixAttempts === 1 ? "" : "s"}, so nothing was pushed and no CI run was started.\n\n`
3654
+ + `${localChecksSummary}\n\n${failed[0].label}:\n${truncate(failed[0].output, 3000)}\n\n`
3655
+ + "If the check fails on this machine for a reason that is not the change (it also fails on the base branch, or this machine is set up differently from CI), start the runner with PACEWORK_LOCAL_CHECKS=off to leave the checks to CI.",
3656
+ )
3657
+ }
3658
+ if (localChecksSummary) {
3659
+ await postIssueMilestone(mcpKey, run.task_id, localChecksSummary, "success").catch(() => {})
3660
+ }
3661
+ }
3445
3662
  }
3446
3663
 
3447
3664
  executorPhase = "finished"
@@ -3562,6 +3779,7 @@ async function runExecutor(run, state) {
3562
3779
  const verification = await runVerificationPlan(worktreePath, changedFiles, runnerProfile)
3563
3780
  const verificationSummary = [
3564
3781
  runnerVerificationSummary,
3782
+ localChecksSummary,
3565
3783
  verification.summary,
3566
3784
  ].filter(Boolean).join("\n")
3567
3785
  await postIssueMilestone(
@@ -3677,7 +3895,7 @@ async function runExecutor(run, state) {
3677
3895
  summary,
3678
3896
  changedFiles,
3679
3897
  diffStat: diffStat.stdout,
3680
- verificationSummary: runnerVerificationSummary,
3898
+ verificationSummary: [runnerVerificationSummary, localChecksSummary].filter(Boolean).join("\n\n"),
3681
3899
  }))
3682
3900
  if (pendingPushDir) clearPendingPush(pendingPushDir)
3683
3901
 
@@ -3967,6 +4185,7 @@ async function runExecutor(run, state) {
3967
4185
  }, error)
3968
4186
  throw reportedRunFailure(error)
3969
4187
  } finally {
4188
+ runEnded = true
3970
4189
  clearInterval(logInterval)
3971
4190
  clearInterval(limitInterval)
3972
4191
  if (runTimeoutHandle) clearTimeout(runTimeoutHandle)
@@ -0,0 +1,202 @@
1
+ /**
2
+ * What a Claude Code run is doing right now (PWK-317), for the Workers office.
3
+ *
4
+ * With `--output-format stream-json --verbose`, Claude Code prints one JSON event
5
+ * per line while it works and ends with a `result` event that carries the same
6
+ * fields as the single `--output-format json` result. This module turns those
7
+ * events into one of five activity values and decides when to send a change.
8
+ *
9
+ * Only the value and when it changed ever leave the machine: no file names,
10
+ * commands, prompts or text.
11
+ */
12
+
13
+ export const RUN_ACTIVITIES = ["writing", "reading", "running", "thinking", "waiting"]
14
+
15
+ /** At most one activity report per run in this window. */
16
+ export const ACTIVITY_MIN_INTERVAL_MS = 5_000
17
+ /**
18
+ * The runner sends its log every 5 seconds, and timers drift by a few milliseconds:
19
+ * two ticks can be 4,999 ms apart. Without this slack every other tick would be
20
+ * skipped and a change would take 10 seconds to show.
21
+ */
22
+ export const ACTIVITY_TICK_SLACK_MS = 250
23
+ /** Silence from the agent this long, while its process is alive, reads as waiting. */
24
+ export const ACTIVITY_WAITING_AFTER_MS = 60_000
25
+ /** A single event line longer than this is not kept whole (a huge tool result, for example). */
26
+ const MAX_LINE_CHARS = 1_000_000
27
+
28
+ const WRITING_TOOLS = new Set(["Edit", "MultiEdit", "Write", "NotebookEdit"])
29
+ const READING_TOOLS = new Set(["Read", "Grep", "Glob", "LS", "WebFetch", "WebSearch", "NotebookRead", "ToolSearch", "ListMcpResourcesTool", "ReadMcpResourceTool"])
30
+ const RUNNING_TOOLS = new Set(["Bash", "BashOutput", "KillBash", "KillShell", "Monitor"])
31
+
32
+ /** The activity for one tool name. MCP tools look things up, so they read; anything else is the agent working things out. */
33
+ export function activityForTool(name) {
34
+ const tool = String(name || "")
35
+ if (WRITING_TOOLS.has(tool)) return "writing"
36
+ if (READING_TOOLS.has(tool)) return "reading"
37
+ if (RUNNING_TOOLS.has(tool)) return "running"
38
+ if (tool.startsWith("mcp__")) return "reading"
39
+ return "thinking"
40
+ }
41
+
42
+ /**
43
+ * The activity one stream event shows, or null when it shows nothing new (system
44
+ * events, rate limit notices, the final result).
45
+ *
46
+ * - An assistant message that uses a tool: that tool's activity (the last tool when it uses several).
47
+ * - An assistant message with text or reasoning only: thinking.
48
+ * - A tool result coming back: the agent is reading it and deciding what to do next, so thinking.
49
+ */
50
+ export function activityForStreamEvent(event) {
51
+ if (!event || typeof event !== "object") return null
52
+ const content = Array.isArray(event.message?.content) ? event.message.content : []
53
+ if (event.type === "assistant") {
54
+ const tools = content.filter((block) => block?.type === "tool_use")
55
+ if (tools.length) return activityForTool(tools[tools.length - 1].name)
56
+ if (content.some((block) => block?.type === "text" || block?.type === "thinking" || block?.type === "redacted_thinking")) return "thinking"
57
+ return null
58
+ }
59
+ if (event.type === "user" && content.some((block) => block?.type === "tool_result")) return "thinking"
60
+ return null
61
+ }
62
+
63
+ /**
64
+ * Reads Claude Code's stream line by line as chunks arrive. Holds at most one
65
+ * partial line and the final result line, never the whole stream.
66
+ *
67
+ * @param {{ onEvent?: (event: object) => void, onOtherLine?: (line: string) => void }} handlers
68
+ * onEvent gets each parsed JSON event; onOtherLine gets lines that are not JSON
69
+ * (an error printed by the CLI, for example), so they still reach the log.
70
+ */
71
+ export function createStreamReader({ onEvent, onOtherLine } = {}) {
72
+ let partial = ""
73
+ let resultLine = null
74
+ let overflow = false
75
+
76
+ function line(raw) {
77
+ const text = raw.trim()
78
+ if (!text) return
79
+ if (!text.startsWith("{")) {
80
+ onOtherLine?.(text)
81
+ return
82
+ }
83
+ let event
84
+ try {
85
+ event = JSON.parse(text)
86
+ } catch {
87
+ onOtherLine?.(text)
88
+ return
89
+ }
90
+ if (event?.type === "result") resultLine = text
91
+ onEvent?.(event)
92
+ }
93
+
94
+ return {
95
+ push(chunk) {
96
+ const text = partial + String(chunk ?? "")
97
+ const lines = text.split(/\r?\n/)
98
+ partial = lines.pop() ?? ""
99
+ if (partial.length > MAX_LINE_CHARS) {
100
+ // Not an event we need whole; drop it rather than grow without limit.
101
+ partial = ""
102
+ overflow = true
103
+ }
104
+ for (const raw of lines) {
105
+ if (overflow) { overflow = false; continue }
106
+ line(raw)
107
+ }
108
+ },
109
+ end() {
110
+ if (partial && !overflow) line(partial)
111
+ partial = ""
112
+ overflow = false
113
+ },
114
+ /** The final result event's line, in the form parseClaudeJsonResult reads. Empty until it arrives. */
115
+ resultText() {
116
+ return resultLine ?? ""
117
+ },
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Tracks a run's activity and decides when to send it: only when it changed,
123
+ * at most once per `minIntervalMs`, and as `waiting` once the agent has been
124
+ * silent for `waitingAfterMs`.
125
+ */
126
+ export function createActivityTracker({ startedAt = Date.now(), minIntervalMs = ACTIVITY_MIN_INTERVAL_MS, waitingAfterMs = ACTIVITY_WAITING_AFTER_MS } = {}) {
127
+ let current = null
128
+ let currentAt = null
129
+ let lastEventAt = startedAt
130
+ let sent = null
131
+ let sentAt = -Infinity
132
+
133
+ function set(activity, at) {
134
+ if (activity === current) return
135
+ current = activity
136
+ currentAt = at
137
+ }
138
+
139
+ return {
140
+ /** An event arrived from the agent. `activity` is null when the event shows no activity of its own. */
141
+ observe(activity, at = Date.now()) {
142
+ lastEventAt = at
143
+ if (activity) set(activity, at)
144
+ // Any sign of life ends a wait; the agent was last seen doing what it does now.
145
+ else if (current === "waiting") set("thinking", at)
146
+ },
147
+ /** The value to send now, or null when nothing should be sent. Call markSent once it was sent. */
148
+ due(now = Date.now()) {
149
+ if (now - lastEventAt >= waitingAfterMs) set("waiting", lastEventAt + waitingAfterMs)
150
+ if (!current || current === sent) return null
151
+ if (now - sentAt < minIntervalMs - ACTIVITY_TICK_SLACK_MS) return null
152
+ return { activity: current, activity_at: new Date(currentAt).toISOString() }
153
+ },
154
+ markSent(report, now = Date.now()) {
155
+ sent = report.activity
156
+ sentAt = now
157
+ },
158
+ current() {
159
+ return current
160
+ },
161
+ }
162
+ }
163
+
164
+ /** Whether this Claude Code's help text offers streaming output. */
165
+ export function helpSupportsStreamJson(help) {
166
+ return /stream-json/.test(String(help || "")) && /--verbose/.test(String(help || ""))
167
+ }
168
+
169
+ /**
170
+ * Claude Code's result (the --output-format json line, or the last event of
171
+ * stream-json): the final message and what the run
172
+ * cost. Null when the output is not that JSON (an older CLI, a crash).
173
+ */
174
+ export function parseClaudeJsonResult(stdout) {
175
+ const lines = String(stdout || "").trim().split(/\r?\n/).reverse()
176
+ for (const line of lines) {
177
+ if (!line.trim().startsWith("{")) continue
178
+ try {
179
+ const parsed = JSON.parse(line)
180
+ if (parsed?.type !== "result") continue
181
+ const usage = parsed.usage ?? {}
182
+ const inputTokens = [usage.input_tokens, usage.cache_creation_input_tokens, usage.cache_read_input_tokens]
183
+ .filter((value) => typeof value === "number")
184
+ return {
185
+ text: typeof parsed.result === "string" ? parsed.result : "",
186
+ isError: parsed.is_error === true,
187
+ // "error_max_budget_usd" when --max-budget-usd ended the run.
188
+ subtype: typeof parsed.subtype === "string" ? parsed.subtype : null,
189
+ usage: {
190
+ cost_usd: typeof parsed.total_cost_usd === "number" ? parsed.total_cost_usd : null,
191
+ input_tokens: inputTokens.length ? inputTokens.reduce((a, b) => a + b, 0) : null,
192
+ output_tokens: typeof usage.output_tokens === "number" ? usage.output_tokens : null,
193
+ num_turns: typeof parsed.num_turns === "number" ? parsed.num_turns : null,
194
+ },
195
+ }
196
+ } catch {
197
+ // not the result line
198
+ }
199
+ }
200
+ return null
201
+ }
202
+
@@ -0,0 +1,440 @@
1
+ // Local checks: before a build is pushed, the checks CI would run for the
2
+ // changed areas are run on this machine, so the CI server is not spent on a
3
+ // change that cannot pass.
4
+ //
5
+ // The list comes from the repository's CI workflow (.github/workflows/ci.yml),
6
+ // read as text, so it cannot drift from CI. A workflow is usable when it follows
7
+ // this shape:
8
+ // - one job's script names the changed areas with lines like
9
+ // backend=$(checked '^backend/')
10
+ // and writes them out with echo "backend=$backend";
11
+ // - each check job is gated with if: needs.<job>.outputs.<area> == 'true'
12
+ // and lists its commands as steps with `run:` (and `working-directory:`).
13
+ // A repository without such a workflow gets no local checks.
14
+ //
15
+ // What does not run here:
16
+ // - a job marked with the comment `# local-checks: skip` (optionally
17
+ // `# local-checks: skip - reason`);
18
+ // - a job that runs in a container image;
19
+ // - in a job that needs a service (a database), every step except the
20
+ // static ones (typecheck, lint), unless a throwaway database is given in
21
+ // PACEWORK_LOCAL_CHECKS_DATABASE_URL;
22
+ // - a step that needs Docker when Docker is not running.
23
+ //
24
+ // The commands come from the branch being built, so they run without the run's
25
+ // credentials (checkEnvironment). The runner has no sandbox of its own to put
26
+ // them in, so a sandboxed run does not run them at all (skipWholePlan).
27
+ // All of these are reported as skipped with the reason. Only a check that ran
28
+ // and failed blocks the push.
29
+
30
+ import { spawn } from "node:child_process"
31
+ import fs from "node:fs"
32
+
33
+ export const DEFAULT_WORKFLOW_PATH = ".github/workflows/ci.yml"
34
+ export const DATABASE_URL_ENV = "PACEWORK_LOCAL_CHECKS_DATABASE_URL"
35
+ export const DEFAULT_MAX_FIX_ATTEMPTS = 2
36
+
37
+ const INSTALL_COMMAND = /\b(pnpm|npm|yarn|bun)\s+(install|ci)\b/
38
+ const STATIC_CHECK_COMMAND = /\b(typecheck|type-check|tsc|lint|eslint)\b/
39
+ const DOCKER_COMMAND = /(^|[\s;&|(])docker\s/
40
+ // Steps that only wire up the CI job itself.
41
+ const CI_ONLY_COMMAND = /GITHUB_(ENV|OUTPUT|PATH|STEP_SUMMARY)|\$\{\{\s*(job|secrets|steps|github)\./
42
+
43
+ function indentOf(line) {
44
+ return line.length - line.trimStart().length
45
+ }
46
+
47
+ function unquote(value) {
48
+ const text = value.trim()
49
+ const quoted = /^(['"])(.*)\1$/.exec(text)
50
+ return quoted ? quoted[2] : text
51
+ }
52
+
53
+ /** A `key: value` whose value may be a block scalar (`|`, `>-`) on the lines below. */
54
+ function readValue(lines, index, keyIndent, inline) {
55
+ const first = inline.trim()
56
+ if (!/^[|>][+-]?$/.test(first)) return { value: unquote(first), next: index + 1 }
57
+ const body = []
58
+ let next = index + 1
59
+ while (next < lines.length && (lines[next].trim() === "" || indentOf(lines[next]) > keyIndent)) {
60
+ body.push(lines[next])
61
+ next += 1
62
+ }
63
+ const indent = Math.min(...body.filter(line => line.trim()).map(indentOf))
64
+ const text = body.map(line => line.slice(indent)).join(first.startsWith(">") ? " " : "\n").trim()
65
+ return { value: text, next }
66
+ }
67
+
68
+ function parseSteps(lines) {
69
+ const steps = []
70
+ let step = null
71
+ let index = 0
72
+ while (index < lines.length) {
73
+ const line = lines[index]
74
+ if (!line.trim() || line.trim().startsWith("#")) { index += 1; continue }
75
+ const start = /^(\s*)- (.*)$/.exec(line)
76
+ let keyIndent = indentOf(line)
77
+ let body = line.trim()
78
+ if (start && (!step || start[1].length <= step.indent)) {
79
+ step = { indent: start[1].length, env: {} }
80
+ steps.push(step)
81
+ keyIndent = start[1].length + 2
82
+ body = start[2]
83
+ }
84
+ const pair = /^([\w-]+):\s*(.*)$/.exec(body)
85
+ if (!step || !pair || keyIndent !== step.indent + 2) { index += 1; continue }
86
+ const [, key, inline] = pair
87
+ if (key === "env" && !inline.trim()) {
88
+ index += 1
89
+ while (index < lines.length && (lines[index].trim() === "" || indentOf(lines[index]) > keyIndent)) {
90
+ const entry = /^\s*([\w-]+):\s*(.*)$/.exec(lines[index])
91
+ if (entry && !lines[index].trim().startsWith("#")) step.env[entry[1]] = unquote(entry[2])
92
+ index += 1
93
+ }
94
+ continue
95
+ }
96
+ const { value, next } = readValue(lines, index, keyIndent, inline)
97
+ step[key] = value
98
+ index = next
99
+ }
100
+ return steps
101
+ }
102
+
103
+ function parseJob(id, lines) {
104
+ const stepsAt = lines.findIndex(line => /^ {4}steps:\s*$/.test(line))
105
+ const header = stepsAt < 0 ? lines : lines.slice(0, stepsAt)
106
+ const job = { id, name: id, condition: "", container: false, services: false, skip: null, timeoutMinutes: null, steps: [] }
107
+ for (let index = 0; index < header.length; index += 1) {
108
+ const line = header[index]
109
+ const marker = /^\s*#\s*local-checks:\s*skip\b(?:\s*[-:,]\s*(.+))?/.exec(line)
110
+ if (marker) { job.skip = (marker[1] || "").trim() || "marked in the workflow as not run locally"; continue }
111
+ const pair = /^ {4}([\w-]+):\s*(.*)$/.exec(line)
112
+ if (!pair) continue
113
+ const [, key, inline] = pair
114
+ if (key === "name") job.name = unquote(inline)
115
+ else if (key === "if") job.condition = readValue(header, index, 4, inline).value
116
+ else if (key === "container") job.container = true
117
+ else if (key === "services") job.services = true
118
+ else if (key === "timeout-minutes" && /^\d+$/.test(inline.trim())) job.timeoutMinutes = Number(inline.trim())
119
+ }
120
+ if (stepsAt >= 0) job.steps = parseSteps(lines.slice(stepsAt + 1))
121
+ return job
122
+ }
123
+
124
+ function compile(pattern) {
125
+ try { return new RegExp(pattern) } catch { return null }
126
+ }
127
+
128
+ /** The changed-area rules, from the job script that computes them. */
129
+ function parseAreas(script) {
130
+ const variables = new Map()
131
+ for (const match of script.matchAll(/^\s*(\w+)=\$\(checked '([^']+)'\)/gm)) {
132
+ const pattern = compile(match[2])
133
+ if (!pattern) return null
134
+ variables.set(match[1], pattern)
135
+ }
136
+ const areas = []
137
+ for (const match of script.matchAll(/echo "([\w-]+)=\$\{?(\w+)\}?"/g)) {
138
+ if (variables.has(match[2])) areas.push({ name: match[1], pattern: variables.get(match[2]) })
139
+ }
140
+ if (!areas.length) return null
141
+ // Files that never need a check (docs), and files that need every check.
142
+ const ignore = /grep -Ev '([^']+)'/.exec(script)
143
+ const everything = /grep -Eq '([^']+)' <<<"\$code"/.exec(script)
144
+ return {
145
+ areas,
146
+ ignore: ignore ? compile(ignore[1]) : null,
147
+ everything: everything ? compile(everything[1]) : null,
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Read a CI workflow into its changed-area rules and check jobs.
153
+ * Returns null when the text is not a workflow of the shape described above.
154
+ */
155
+ export function parseCiWorkflow(text) {
156
+ const lines = String(text || "").split(/\r?\n/)
157
+ const jobsAt = lines.findIndex(line => /^jobs:\s*$/.test(line))
158
+ if (jobsAt < 0) return null
159
+ const raw = []
160
+ for (let index = jobsAt + 1; index < lines.length; index += 1) {
161
+ const line = lines[index]
162
+ if (/^[^\s#]/.test(line)) break
163
+ const key = /^ {2}([\w-]+):\s*$/.exec(line)
164
+ if (key) raw.push({ id: key[1], lines: [] })
165
+ else if (raw.length) raw[raw.length - 1].lines.push(line)
166
+ }
167
+ const all = raw.map(job => parseJob(job.id, job.lines))
168
+
169
+ let rules = null
170
+ let rulesJob = null
171
+ for (const job of all) {
172
+ const script = job.steps.map(step => step.run || "").find(run => /\$\(checked '/.test(run))
173
+ if (!script) continue
174
+ rules = parseAreas(script)
175
+ rulesJob = job.id
176
+ break
177
+ }
178
+ if (!rules) return null
179
+
180
+ const jobs = []
181
+ for (const job of all) {
182
+ // Jobs that only run on the main branch (publish, deploy) are not checks.
183
+ if (/refs\/heads\/|event_name\s*==\s*'push'/.test(job.condition)) continue
184
+ const outputs = [...job.condition.matchAll(new RegExp(`needs\\.${rulesJob}\\.outputs\\.([\\w-]+)\\s*==\\s*'true'`, "g"))]
185
+ .map(match => match[1])
186
+ .filter(name => rules.areas.some(area => area.name === name))
187
+ if (!outputs.length) continue
188
+ jobs.push({ ...job, areas: outputs, steps: job.steps.filter(step => step.run && !step.uses) })
189
+ }
190
+ return jobs.length ? { ...rules, jobs } : null
191
+ }
192
+
193
+ /** GitHub expressions mean nothing here: `env.NAME` reads this machine's value, the rest is dropped. */
194
+ function resolveExpressions(text, env) {
195
+ return String(text).replace(/\$\{\{([^}]*)\}\}/g, (_, expression) => {
196
+ const name = /^\s*env\.(\w+)\s*$/.exec(expression)
197
+ return name ? (env[name[1]] ?? "") : ""
198
+ }).trim()
199
+ }
200
+
201
+ /**
202
+ * The steps to run for a set of changed files, in order, each either runnable
203
+ * or carrying the reason it is skipped.
204
+ */
205
+ export function planLocalChecks(workflow, changedFiles, { databaseUrl = null } = {}) {
206
+ const files = changedFiles.filter(file => !(workflow.ignore && workflow.ignore.test(file)))
207
+ if (!files.length) return []
208
+ const everything = Boolean(workflow.everything) && files.some(file => workflow.everything.test(file))
209
+ const changed = new Set(workflow.areas
210
+ .filter(area => everything || files.some(file => area.pattern.test(file)))
211
+ .map(area => area.name))
212
+ const baseEnv = databaseUrl ? { DATABASE_URL: databaseUrl } : {}
213
+
214
+ const plan = []
215
+ for (const job of workflow.jobs) {
216
+ if (!job.areas.some(area => changed.has(area))) continue
217
+ const jobSkip = job.skip
218
+ || (job.container ? "it runs in a container image on CI" : null)
219
+ if (jobSkip) {
220
+ plan.push({ job: job.name, name: null, kind: "job", skip: jobSkip })
221
+ continue
222
+ }
223
+ const steps = []
224
+ for (const step of job.steps) {
225
+ if (step.if || CI_ONLY_COMMAND.test(step.run)) continue
226
+ const command = resolveExpressions(step.run, baseEnv)
227
+ const kind = INSTALL_COMMAND.test(command) ? "install" : "check"
228
+ const needsService = job.services && !databaseUrl && kind === "check" && !STATIC_CHECK_COMMAND.test(command)
229
+ const env = { ...baseEnv }
230
+ for (const [key, value] of Object.entries(step.env)) env[key] = resolveExpressions(value, baseEnv)
231
+ steps.push({
232
+ job: job.name,
233
+ name: step.name || command.split("\n")[0],
234
+ kind,
235
+ command,
236
+ cwd: step["working-directory"] || ".",
237
+ env,
238
+ needsDocker: DOCKER_COMMAND.test(command),
239
+ timeoutMs: (job.timeoutMinutes || 20) * 60_000,
240
+ skip: needsService
241
+ ? `it needs the database CI starts for this job; set ${DATABASE_URL_ENV} to a throwaway database to run it here`
242
+ : null,
243
+ })
244
+ }
245
+ // Installing dependencies for nothing but skipped checks is wasted time.
246
+ if (steps.some(step => step.kind === "check" && !step.skip)) plan.push(...steps)
247
+ else plan.push(...steps.filter(step => step.kind === "check"))
248
+ }
249
+ return plan
250
+ }
251
+
252
+ // Names that hold a credential. The checks run scripts from the branch being
253
+ // built, so they never get the run's model key, the repository's secrets or
254
+ // anything else in the runner's environment that looks like one.
255
+ const SECRET_ENV_NAME = /(KEY|TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIAL|AUTH|COOKIE|SESSION)/i
256
+ const SECRET_ENV_PREFIX = /^(PACEWORK_|ANTHROPIC_|CLAUDE_|OPENAI_|CODEX_|GH_|GITHUB_|AWS_|NPM_CONFIG_|npm_config_)/
257
+
258
+ /** The environment a check runs with: this machine's, without anything that names a credential. */
259
+ export function checkEnvironment(env = process.env) {
260
+ const clean = {}
261
+ for (const [key, value] of Object.entries(env)) {
262
+ if (value === undefined || SECRET_ENV_NAME.test(key) || SECRET_ENV_PREFIX.test(key)) continue
263
+ clean[key] = value
264
+ }
265
+ return clean
266
+ }
267
+
268
+ /** CI runs its steps in bash; the user's own shell is the fallback where bash is missing. */
269
+ function checkShell() {
270
+ for (const shell of ["/bin/bash", "/usr/bin/bash"]) {
271
+ if (fs.existsSync(shell)) return shell
272
+ }
273
+ return process.env.SHELL || "sh"
274
+ }
275
+
276
+ /** Run a command in a login shell, where the user's node and package managers are on PATH. */
277
+ export function execLocalCheck(command, { cwd, env = {}, baseEnv = checkEnvironment(), timeoutMs = 20 * 60_000, onChild = () => {} } = {}) {
278
+ return new Promise((resolve) => {
279
+ let output = ""
280
+ let timedOut = false
281
+ let child
282
+ try {
283
+ child = spawn(checkShell(), ["-lc", command], {
284
+ cwd,
285
+ env: { ...baseEnv, CI: "true", ...env },
286
+ stdio: ["ignore", "pipe", "pipe"],
287
+ detached: process.platform !== "win32",
288
+ })
289
+ } catch (error) {
290
+ resolve({ code: null, output: String(error?.message || error), timedOut: false })
291
+ return
292
+ }
293
+ onChild(child)
294
+ const kill = () => {
295
+ try {
296
+ if (process.platform !== "win32" && child.pid) process.kill(-child.pid, "SIGKILL")
297
+ else child.kill("SIGKILL")
298
+ } catch { /* already gone */ }
299
+ }
300
+ const timer = setTimeout(() => { timedOut = true; kill() }, timeoutMs)
301
+ const keep = (chunk) => { output = `${output}${chunk}`.slice(-200_000) }
302
+ child.stdout.on("data", keep)
303
+ child.stderr.on("data", keep)
304
+ child.on("error", (error) => {
305
+ clearTimeout(timer)
306
+ resolve({ code: null, output: String(error?.message || error), timedOut: false })
307
+ })
308
+ child.on("close", (code, signal) => {
309
+ clearTimeout(timer)
310
+ resolve({ code, output, timedOut, signal })
311
+ })
312
+ })
313
+ }
314
+
315
+ /** The same plan with every step marked as skipped for one reason, for a run that may not run any of them. */
316
+ export function skipWholePlan(plan, reason) {
317
+ return plan.map(step => ({ ...step, skip: reason }))
318
+ }
319
+
320
+ function tail(text, max = 6000) {
321
+ const value = String(text || "").trim()
322
+ return value.length > max ? `...${value.slice(-max)}` : value
323
+ }
324
+
325
+ /**
326
+ * Run a plan. Each result is passed, failed (it ran and reported a problem) or
327
+ * skipped (it could not run on this machine; `note` says why).
328
+ *
329
+ * @param {ReturnType<typeof planLocalChecks>} plan
330
+ * @param {{ cwd: string, exec?: typeof execLocalCheck, resolveCwd?: (root: string, dir: string) => string, shouldStop?: () => boolean }} options
331
+ */
332
+ export async function runLocalChecks(plan, { cwd, exec = execLocalCheck, resolveCwd = (root, dir) => (dir === "." ? root : `${root}/${dir}`), shouldStop = () => false }) {
333
+ const results = []
334
+ const blockedJobs = new Map()
335
+ let dockerAvailable = null
336
+ for (const step of plan) {
337
+ const label = step.name ? `${step.job}: ${step.name}` : step.job
338
+ const result = { label, job: step.job, command: step.command || null, status: "skipped", note: null, output: "" }
339
+ const skip = (note) => { result.note = note; if (step.kind !== "install") results.push(result) }
340
+ if (shouldStop()) { skip("the run was stopped"); continue }
341
+ if (step.skip) { skip(step.skip); continue }
342
+ if (blockedJobs.has(step.job)) { skip(blockedJobs.get(step.job)); continue }
343
+ if (step.needsDocker) {
344
+ if (dockerAvailable === null) dockerAvailable = (await exec("docker info", { cwd, timeoutMs: 30_000 })).code === 0
345
+ if (!dockerAvailable) { skip("Docker is not running on this machine"); continue }
346
+ }
347
+ const run = await exec(step.command, { cwd: resolveCwd(cwd, step.cwd), env: step.env, timeoutMs: step.timeoutMs })
348
+ if (run.code === 0 && !run.timedOut) {
349
+ result.status = "passed"
350
+ if (step.kind !== "install") results.push(result)
351
+ continue
352
+ }
353
+ const cannotRun = run.timedOut
354
+ ? `it did not finish within ${Math.round(step.timeoutMs / 60_000)} minutes on this machine`
355
+ : run.signal
356
+ ? `it was killed on this machine before it finished (${run.signal})`
357
+ : run.code === null || run.code === 127 || run.code === 126
358
+ // A tool that is missing because of the change itself also lands here; CI reports that one.
359
+ ? `its command could not be started on this machine (${tail(run.output, 200).split("\n").pop() || "command not found"})`
360
+ : null
361
+ if (step.kind === "install") {
362
+ // Without dependencies the job's checks would fail for a reason that is not the change.
363
+ blockedJobs.set(step.job, cannotRun
364
+ ? `dependencies could not be installed: ${cannotRun}`
365
+ : `dependencies could not be installed on this machine (\`${step.command}\` exited with code ${run.code})`)
366
+ continue
367
+ }
368
+ if (cannotRun) { skip(cannotRun); continue }
369
+ result.status = "failed"
370
+ result.note = `exited with code ${run.code}`
371
+ result.output = tail(run.output)
372
+ results.push(result)
373
+ }
374
+ return results
375
+ }
376
+
377
+ export function failedChecks(results) {
378
+ return results.filter(result => result.status === "failed")
379
+ }
380
+
381
+ /**
382
+ * Run the checks; while any fail, hand the failures to the builder and run them
383
+ * again. Stops after `maxFixAttempts` rounds of fixing, or as soon as
384
+ * `shouldStop` says the run is over (canceled, timed out, over a limit): then
385
+ * no further check runs and the builder is not started again.
386
+ *
387
+ * @param {{ check: () => Promise<Array>, fix: (failed: Array, attempt: number) => Promise<void>, maxFixAttempts?: number, shouldStop?: () => boolean }} input
388
+ */
389
+ export async function runChecksUntilGreen({ check, fix, maxFixAttempts = DEFAULT_MAX_FIX_ATTEMPTS, shouldStop = () => false }) {
390
+ let results = []
391
+ let fixAttempts = 0
392
+ const stopped = () => ({ results, fixAttempts, passed: false, stopped: true })
393
+ if (shouldStop()) return stopped()
394
+ results = await check()
395
+ while (failedChecks(results).length > 0 && fixAttempts < maxFixAttempts) {
396
+ if (shouldStop()) return stopped()
397
+ fixAttempts += 1
398
+ await fix(failedChecks(results), fixAttempts)
399
+ if (shouldStop()) return stopped()
400
+ results = await check()
401
+ }
402
+ if (shouldStop()) return stopped()
403
+ return { results, fixAttempts, passed: failedChecks(results).length === 0, stopped: false }
404
+ }
405
+
406
+ /** The lines for the run summary: which checks ran, passed, failed or were skipped and why. */
407
+ export function formatLocalChecks(results, { fixAttempts = 0, source = DEFAULT_WORKFLOW_PATH } = {}) {
408
+ if (!results.length) return ""
409
+ const lines = results.map((result) => {
410
+ if (result.status === "passed") return `- PASS ${result.label}`
411
+ if (result.status === "failed") return `- FAIL ${result.label} (${result.note})`
412
+ return `- SKIPPED ${result.label}: ${result.note}. CI still runs it.`
413
+ })
414
+ const fixed = fixAttempts > 0
415
+ ? ` The builder was sent back ${fixAttempts} time${fixAttempts === 1 ? "" : "s"} to fix failing checks.`
416
+ : ""
417
+ return [`Local checks before the push (from \`${source}\`).${fixed}`, ...lines].join("\n")
418
+ }
419
+
420
+ /** What the builder is told when checks fail: which ones, the command, and the end of the output. */
421
+ export function buildFixInstructions(failed, { attempt, maxFixAttempts = DEFAULT_MAX_FIX_ATTEMPTS, source = DEFAULT_WORKFLOW_PATH }) {
422
+ const sections = failed.map(result => [
423
+ `### ${result.label}`,
424
+ `Command: \`${result.command}\``,
425
+ "Output (end):",
426
+ "```",
427
+ result.output || "(no output)",
428
+ "```",
429
+ ].join("\n"))
430
+ return [
431
+ "## Local checks failed: fix them before this work can be pushed",
432
+ `Your earlier work on this issue is in the checkout. The runner then ran the checks CI runs for the changed areas (from \`${source}\`) and ${failed.length === 1 ? "this one" : "these"} failed. This is fix round ${attempt} of ${maxFixAttempts}; nothing is pushed while a check fails.`,
433
+ "- Fix the cause in the code or the tests. Do not delete, skip or weaken a check to make it pass.",
434
+ "- Run the failing command yourself until it passes, then finish.",
435
+ "- If a failure is not caused by your change and you cannot fix it, say so plainly in your summary.",
436
+ "- Your final response still becomes the pull request description for the whole change, not only for this fix.",
437
+ "",
438
+ ...sections,
439
+ ].join("\n")
440
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacework",
3
- "version": "1.7.2",
3
+ "version": "1.7.4",
4
4
  "description": "pacework CLI: connect this machine as a runner for pacework AI agents",
5
5
  "type": "module",
6
6
  "bin": {