pacework 1.7.3 → 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.
@@ -35,6 +35,21 @@ import { automationDenyRules, automationLimits } from "../lib/automation-limits.
35
35
  import { truncateSummary } from "../lib/summary-text.mjs"
36
36
  import { activityForStreamEvent, createActivityTracker, createStreamReader, helpSupportsStreamJson, parseClaudeJsonResult } from "../lib/claude-activity.mjs"
37
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"
38
53
 
39
54
  function parseArgs(argv) {
40
55
  const args = {}
@@ -1326,6 +1341,69 @@ async function runVerificationPlan(worktreePath, changedFiles, profile) {
1326
1341
  return { summary, steps }
1327
1342
  }
1328
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
+
1329
1407
  function formatCommitMessage(run, profile) {
1330
1408
  const template = profile.git?.commitMessageTemplate || "{identifier} {title}"
1331
1409
  const identifier = run.task?.identifier || run.task_id
@@ -3182,10 +3260,12 @@ async function runExecutor(run, state) {
3182
3260
  buildPrompt(run, branch, worktreePath, { localCheckoutMode, gitAvailable, workspaceContext, runLimitsText }),
3183
3261
  skillsSection,
3184
3262
  ].filter(Boolean).join("\n\n")
3185
- const execSpec = buildExecutorCommand(run, worktreePath, prompt, outputFile, mcpKey, {
3263
+ const executorOptions = {
3186
3264
  maxCostUsd: costLimitApplies ? runLimits.maxCostUsd : null,
3187
3265
  streamActivity: activeExecutor() === "claude_code" && claudeSupportsStreamJson(),
3188
- })
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")
@@ -3211,7 +3291,8 @@ async function runExecutor(run, state) {
3211
3291
  /** What the agent is doing, from Claude Code's event stream (PWK-317). Null for every other executor. */
3212
3292
  let activity = null
3213
3293
  let streamReader = null
3214
- if (execSpec.summaryMode === "claude_stream") {
3294
+ const startStreamReader = () => {
3295
+ if (execSpec.summaryMode !== "claude_stream") return
3215
3296
  activity = createActivityTracker()
3216
3297
  streamReader = createStreamReader({
3217
3298
  onEvent: (event) => activity.observe(activityForStreamEvent(event)),
@@ -3222,8 +3303,15 @@ async function runExecutor(run, state) {
3222
3303
  },
3223
3304
  })
3224
3305
  }
3306
+ startStreamReader()
3225
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
3226
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
3227
3315
  let childProcess = null
3228
3316
  let lastHeartbeatLogAt = 0
3229
3317
 
@@ -3302,6 +3390,7 @@ async function runExecutor(run, state) {
3302
3390
  let runTimeoutHandle = null
3303
3391
  const runTimeoutPromise = new Promise((_, reject) => {
3304
3392
  runTimeoutHandle = setTimeout(() => {
3393
+ runEnded = true
3305
3394
  if (childProcess && !childProcess.killed) killChildProcessTree(childProcess, "SIGKILL")
3306
3395
  reject(new Error(`Run timed out after ${Math.round(RUN_TIMEOUT_MS / 60000)} minutes`))
3307
3396
  }, RUN_TIMEOUT_MS)
@@ -3383,7 +3472,8 @@ async function runExecutor(run, state) {
3383
3472
  appendLogTail(logState, `> command: ${previewExecutorCommand(execSpec)}`)
3384
3473
  await flushRunnerLog().catch(() => {})
3385
3474
  executorPhase = "running"
3386
- 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 }
3387
3477
  const child = spawn(execSpec.command, execSpec.args, {
3388
3478
  cwd: worktreePath,
3389
3479
  stdio: ["pipe", "pipe", "pipe"],
@@ -3431,7 +3521,9 @@ async function runExecutor(run, state) {
3431
3521
  if (result) {
3432
3522
  // Claude Code prices every run at Anthropic's rates; for
3433
3523
  // another provider that number is wrong, so keep tokens only.
3434
- 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
3435
3527
  // The message, not the JSON around it, is what the
3436
3528
  // summary, the PR description and failure detection read.
3437
3529
  stdioSummary = result.text
@@ -3468,6 +3560,105 @@ async function runExecutor(run, state) {
3468
3560
  reject(new Error(formatChildExitError(activeExecutor(), code, stdioSummary)))
3469
3561
  })
3470
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
+ }
3471
3662
  }
3472
3663
 
3473
3664
  executorPhase = "finished"
@@ -3588,6 +3779,7 @@ async function runExecutor(run, state) {
3588
3779
  const verification = await runVerificationPlan(worktreePath, changedFiles, runnerProfile)
3589
3780
  const verificationSummary = [
3590
3781
  runnerVerificationSummary,
3782
+ localChecksSummary,
3591
3783
  verification.summary,
3592
3784
  ].filter(Boolean).join("\n")
3593
3785
  await postIssueMilestone(
@@ -3703,7 +3895,7 @@ async function runExecutor(run, state) {
3703
3895
  summary,
3704
3896
  changedFiles,
3705
3897
  diffStat: diffStat.stdout,
3706
- verificationSummary: runnerVerificationSummary,
3898
+ verificationSummary: [runnerVerificationSummary, localChecksSummary].filter(Boolean).join("\n\n"),
3707
3899
  }))
3708
3900
  if (pendingPushDir) clearPendingPush(pendingPushDir)
3709
3901
 
@@ -3993,6 +4185,7 @@ async function runExecutor(run, state) {
3993
4185
  }, error)
3994
4186
  throw reportedRunFailure(error)
3995
4187
  } finally {
4188
+ runEnded = true
3996
4189
  clearInterval(logInterval)
3997
4190
  clearInterval(limitInterval)
3998
4191
  if (runTimeoutHandle) clearTimeout(runTimeoutHandle)
@@ -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.3",
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": {