pacework 1.7.3 → 1.7.5

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,23 @@ 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 { catchUpWithRemote, isPushRefused, pushKeepingRemoteCommits, remoteBranchExists, switchToBranch } from "../lib/branch-sync.mjs"
39
+ import { closeSupersededPullRequest } from "../lib/superseded-pr.mjs"
40
+ import {
41
+ buildFixInstructions,
42
+ checkEnvironment,
43
+ DATABASE_URL_ENV,
44
+ DEFAULT_MAX_FIX_ATTEMPTS,
45
+ DEFAULT_WORKFLOW_PATH,
46
+ execLocalCheck,
47
+ failedChecks,
48
+ formatLocalChecks,
49
+ parseCiWorkflow,
50
+ planLocalChecks,
51
+ runChecksUntilGreen,
52
+ runLocalChecks,
53
+ skipWholePlan,
54
+ } from "../lib/local-checks.mjs"
38
55
 
39
56
  function parseArgs(argv) {
40
57
  const args = {}
@@ -1326,6 +1343,69 @@ async function runVerificationPlan(worktreePath, changedFiles, profile) {
1326
1343
  return { summary, steps }
1327
1344
  }
1328
1345
 
1346
+ /**
1347
+ * The checks the repository's CI workflow lists, or null when local checks are
1348
+ * off (runner profile `localChecks: false`, or PACEWORK_LOCAL_CHECKS=off) or the
1349
+ * repository has no workflow they can be read from.
1350
+ */
1351
+ function loadLocalChecks(worktreePath, profile) {
1352
+ const setting = profile.localChecks
1353
+ if (setting === false || setting?.enabled === false) return null
1354
+ if (/^(off|false|0|no)$/i.test(process.env.PACEWORK_LOCAL_CHECKS || "")) return null
1355
+ const source = (typeof setting?.workflow === "string" && setting.workflow) || DEFAULT_WORKFLOW_PATH
1356
+ const file = path.resolve(worktreePath, source)
1357
+ // The workflow is read from the checkout, never from outside it.
1358
+ if (path.relative(worktreePath, file).startsWith("..")) return null
1359
+ try {
1360
+ const workflow = parseCiWorkflow(fs.readFileSync(file, "utf8"))
1361
+ if (!workflow) return null
1362
+ const maxFixAttempts = Number.isInteger(setting?.maxFixAttempts) && setting.maxFixAttempts >= 0
1363
+ ? setting.maxFixAttempts
1364
+ : DEFAULT_MAX_FIX_ATTEMPTS
1365
+ return { workflow, source, maxFixAttempts }
1366
+ } catch {
1367
+ return null
1368
+ }
1369
+ }
1370
+
1371
+ /**
1372
+ * Every file this branch changes against the base branch, committed or not: what
1373
+ * CI's change detection sees. `baseFound` is false when no base ref exists in the
1374
+ * checkout; then only uncommitted work is listed.
1375
+ */
1376
+ async function listBranchChangedFiles(worktreePath, baseBranch = null) {
1377
+ const git = async (argsList) => (await execCommandSafe("git", ["-C", worktreePath, ...argsList], worktreePath))?.stdout ?? ""
1378
+ let base = ""
1379
+ for (const ref of ["origin/HEAD", baseBranch ? `origin/${baseBranch}` : null].filter(Boolean)) {
1380
+ base = (await git(["merge-base", ref, "HEAD"])).trim()
1381
+ if (base) break
1382
+ }
1383
+ const lists = await Promise.all([
1384
+ git(["diff", "--name-only", base || "HEAD"]),
1385
+ git(["ls-files", "--others", "--exclude-standard"]),
1386
+ ])
1387
+ const files = [...new Set(lists.join("\n").split(/\r?\n/).map(line => line.trim()).filter(Boolean))]
1388
+ return { files, baseFound: Boolean(base) }
1389
+ }
1390
+
1391
+ /**
1392
+ * Why a sandboxed run runs none of its local checks. Their commands come from
1393
+ * the branch, which the agent can edit, and the runner cannot put them inside
1394
+ * the sandbox the agent's tool gives the agent. Running them outside it would
1395
+ * hand the agent a way out, so they are left to CI.
1396
+ */
1397
+ const SANDBOXED_RUN_SKIP = "this run is sandboxed and the runner cannot run checks inside its sandbox"
1398
+
1399
+ /** Sum two usage records from the same run; a value missing on both sides stays null. */
1400
+ function addUsage(a, b) {
1401
+ const sum = { ...a, ...b }
1402
+ for (const key of Object.keys(sum)) {
1403
+ if (typeof a[key] === "number" && typeof b[key] === "number") sum[key] = a[key] + b[key]
1404
+ else if (typeof a[key] === "number" && b[key] == null) sum[key] = a[key]
1405
+ }
1406
+ return sum
1407
+ }
1408
+
1329
1409
  function formatCommitMessage(run, profile) {
1330
1410
  const template = profile.git?.commitMessageTemplate || "{identifier} {title}"
1331
1411
  const identifier = run.task?.identifier || run.task_id
@@ -1394,6 +1474,21 @@ async function fetchGithubToken(state, workspaceId, projectId, teamId = null, ta
1394
1474
  */
1395
1475
  const GIT_IGNORE_STORED_AUTH = ["-c", "http.https://github.com/.extraheader="]
1396
1476
 
1477
+ /**
1478
+ * `git <args>` in a directory, resolving { code, stdout, stderr } instead of
1479
+ * throwing. On failure `stderr` is the full error message, command included.
1480
+ */
1481
+ function gitIn(dir, options = []) {
1482
+ return async (args) => {
1483
+ try {
1484
+ const result = await execCommand("git", ["-C", dir, ...options, ...args], dir)
1485
+ return { code: 0, stdout: result.stdout ?? "", stderr: result.stderr ?? "" }
1486
+ } catch (error) {
1487
+ return { code: 1, stdout: "", stderr: error instanceof Error ? error.message : String(error) }
1488
+ }
1489
+ }
1490
+ }
1491
+
1397
1492
  async function pushBranchToGitHub(worktreePath, branch, githubToken, repo, onRetry = () => {}) {
1398
1493
  // Worktrees of one repository share its config, remote URL included: a run
1399
1494
  // pushing while another swaps the URL would push without the token, or leave
@@ -1413,19 +1508,16 @@ async function pushBranchWithToken(worktreePath, branch, githubToken, repo, onRe
1413
1508
  // A GitHub 500 or a dropped connection is retried a few times before the
1414
1509
  // run gives up; one hiccup used to throw a finished run away.
1415
1510
  await retryTransient(async () => {
1416
- // start_work creates the branch on GitHub before the runner has a local clone
1417
- // of it, so --force-with-lease sees a remote it has no ref for and rejects the
1418
- // push. Fetch first so the lease has something to compare against.
1419
- await execCommandSafe(
1420
- "git",
1421
- ["-C", worktreePath, ...GIT_IGNORE_STORED_AUTH, "fetch", "origin", branch],
1422
- worktreePath,
1423
- )
1424
- await execCommand(
1425
- "git",
1426
- ["-C", worktreePath, ...GIT_IGNORE_STORED_AUTH, "push", "--set-upstream", "origin", branch, "--force-with-lease"],
1427
- worktreePath,
1428
- )
1511
+ // Never forced: the branch may be a person's pull request, and a forced
1512
+ // push removed the commits they pushed while the run worked (PWK-370).
1513
+ // Commits GitHub has and this checkout lacks are merged in first.
1514
+ const authed = gitIn(worktreePath, GIT_IGNORE_STORED_AUTH)
1515
+ await pushKeepingRemoteCommits({
1516
+ exec: gitIn(worktreePath),
1517
+ fetch: () => authed(["fetch", "origin", branch]),
1518
+ push: () => authed(["push", "--set-upstream", "origin", branch]),
1519
+ branch,
1520
+ })
1429
1521
  }, {
1430
1522
  onRetry,
1431
1523
  // `--set-upstream` writes the shared config: a lock held by another git
@@ -1882,26 +1974,34 @@ async function prepareWorktreeCheckout(run, runRepoRoot, worktreePath, branch) {
1882
1974
  // Branch exists from a prior run — reuse it instead of -b
1883
1975
  await execCommand("git", ["-C", runRepoRoot, "worktree", "add", worktreePath, branch], runRepoRoot)
1884
1976
  } else {
1977
+ // A branch GitHub already has (an open pull request, rework begun on
1978
+ // another machine) starts from GitHub's copy, not from main: starting
1979
+ // from main dropped its commits and the push was rejected (PWK-370).
1885
1980
  // --no-track: the run's branch must not follow the base branch, and
1886
1981
  // recording an upstream writes .git/config, the lock runs collided on.
1887
- await execCommand("git", ["-C", runRepoRoot, "worktree", "add", "--no-track", "-b", branch, worktreePath, upToDateBase], runRepoRoot)
1982
+ const startPoint = await remoteBranchExists(gitIn(runRepoRoot), branch) ? `origin/${branch}` : upToDateBase
1983
+ await execCommand("git", ["-C", runRepoRoot, "worktree", "add", "--no-track", "-b", branch, worktreePath, startPoint], runRepoRoot)
1888
1984
  }
1889
1985
  } else if (worktreeKnown) {
1890
1986
  const checkedOutBranch = await currentBranch(worktreePath)
1891
1987
  if (checkedOutBranch && checkedOutBranch !== branch) {
1892
1988
  // The worktree may be left on a branch from a previous executor (the
1893
1989
  // branch prefix is per-executor), so the target branch can be absent.
1894
- // Create it when missing instead of failing the whole run.
1895
- const targetExists = await branchExists(branch, runRepoRoot)
1896
- await execCommand(
1897
- "git",
1898
- targetExists
1899
- ? ["-C", worktreePath, "checkout", branch]
1900
- : ["-C", worktreePath, "checkout", "-b", branch],
1901
- worktreePath,
1902
- )
1990
+ // Create it when missing instead of failing the whole run: from GitHub's
1991
+ // copy when GitHub has it (a person's pull request), so its commits are kept.
1992
+ const switched = await switchToBranch(gitIn(worktreePath), branch)
1993
+ if (switched.code !== 0) throw new Error(switched.stderr || `Could not check out ${branch}`)
1903
1994
  }
1904
1995
  }
1996
+
1997
+ // GitHub's copy may have moved since this machine last saw it: the merger
1998
+ // brought it up to date with main, or another runner pushed. Catch up now so
1999
+ // the agent works on the latest; a branch that diverged is left as it is, and
2000
+ // the push merges GitHub's commits in instead of replacing them.
2001
+ const synced = await catchUpWithRemote(gitIn(worktreePath), branch)
2002
+ if (synced === "fast_forwarded" || synced === "diverged" || synced === "failed") {
2003
+ runnerBaseLogger.child({ run_id: run.id, task_id: run.task_id }).info("runner.branch.sync", `Branch ${branch} vs GitHub: ${synced}`, { meta: { branch, result: synced } })
2004
+ }
1905
2005
  }
1906
2006
 
1907
2007
  function buildPrompt(run, branch, worktreePath, options = {}) {
@@ -3105,6 +3205,10 @@ async function runExecutor(run, state) {
3105
3205
  )
3106
3206
  if (started?.branch) {
3107
3207
  run.__serverBranch = started.branch
3208
+ // The open pull request this run continues on, or the one a new
3209
+ // branch replaces (closed by the runner once the new one is open).
3210
+ run.__openPullRequest = started.pull_request || null
3211
+ run.__supersededPullRequest = started.superseded_pull_request || null
3108
3212
  runLogger.info("runner.run.start_work", "Server assigned the working branch", {
3109
3213
  meta: { branch: started.branch, base: started.base, repo: started.repo },
3110
3214
  })
@@ -3122,7 +3226,11 @@ async function runExecutor(run, state) {
3122
3226
  }
3123
3227
 
3124
3228
  runLogger.info("runner.run.prepare", "Preparing worktree")
3125
- const { runRoot, worktreePath, branch, localCheckoutMode, gitAvailable } = await prepareWorktree(run)
3229
+ const preparedWorktree = await prepareWorktree(run)
3230
+ const { runRoot, worktreePath, localCheckoutMode, gitAvailable } = preparedWorktree
3231
+ // Reassigned only when the pull request's branch refuses the push and the
3232
+ // work moves to a new branch.
3233
+ let branch = preparedWorktree.branch
3126
3234
  // Where a commit that could not reach GitHub is noted; only this issue's own
3127
3235
  // checkout has a folder of its own to hold the note.
3128
3236
  const pendingPushDir = gitAvailable && branch && localCheckoutMode !== "repo_root" ? path.dirname(worktreePath) : null
@@ -3182,10 +3290,12 @@ async function runExecutor(run, state) {
3182
3290
  buildPrompt(run, branch, worktreePath, { localCheckoutMode, gitAvailable, workspaceContext, runLimitsText }),
3183
3291
  skillsSection,
3184
3292
  ].filter(Boolean).join("\n\n")
3185
- const execSpec = buildExecutorCommand(run, worktreePath, prompt, outputFile, mcpKey, {
3293
+ const executorOptions = {
3186
3294
  maxCostUsd: costLimitApplies ? runLimits.maxCostUsd : null,
3187
3295
  streamActivity: activeExecutor() === "claude_code" && claudeSupportsStreamJson(),
3188
- })
3296
+ }
3297
+ // Replaced when the builder is sent back to fix failing local checks.
3298
+ let execSpec = buildExecutorCommand(run, worktreePath, prompt, outputFile, mcpKey, executorOptions)
3189
3299
  // These steps run before the executor spawns; when one stalls the run sits in
3190
3300
  // "running" with no output, so each is traced.
3191
3301
  runLogger.info("runner.run.prepare", "Fetching MCP servers")
@@ -3211,7 +3321,8 @@ async function runExecutor(run, state) {
3211
3321
  /** What the agent is doing, from Claude Code's event stream (PWK-317). Null for every other executor. */
3212
3322
  let activity = null
3213
3323
  let streamReader = null
3214
- if (execSpec.summaryMode === "claude_stream") {
3324
+ const startStreamReader = () => {
3325
+ if (execSpec.summaryMode !== "claude_stream") return
3215
3326
  activity = createActivityTracker()
3216
3327
  streamReader = createStreamReader({
3217
3328
  onEvent: (event) => activity.observe(activityForStreamEvent(event)),
@@ -3222,8 +3333,15 @@ async function runExecutor(run, state) {
3222
3333
  },
3223
3334
  })
3224
3335
  }
3336
+ startStreamReader()
3225
3337
  let runUsage = null
3338
+ /** What the checks from the CI workflow found on this machine, for the run summary. */
3339
+ let localChecksSummary = ""
3340
+ /** Usage of the agent processes that already finished in this run (fix rounds start another). */
3341
+ let earlierUsage = null
3226
3342
  let cancelRequested = false
3343
+ /** Set when the run timed out or runExecutor is leaving: nothing may start a check or an agent after it. */
3344
+ let runEnded = false
3227
3345
  let childProcess = null
3228
3346
  let lastHeartbeatLogAt = 0
3229
3347
 
@@ -3302,6 +3420,7 @@ async function runExecutor(run, state) {
3302
3420
  let runTimeoutHandle = null
3303
3421
  const runTimeoutPromise = new Promise((_, reject) => {
3304
3422
  runTimeoutHandle = setTimeout(() => {
3423
+ runEnded = true
3305
3424
  if (childProcess && !childProcess.killed) killChildProcessTree(childProcess, "SIGKILL")
3306
3425
  reject(new Error(`Run timed out after ${Math.round(RUN_TIMEOUT_MS / 60000)} minutes`))
3307
3426
  }, RUN_TIMEOUT_MS)
@@ -3383,7 +3502,8 @@ async function runExecutor(run, state) {
3383
3502
  appendLogTail(logState, `> command: ${previewExecutorCommand(execSpec)}`)
3384
3503
  await flushRunnerLog().catch(() => {})
3385
3504
  executorPhase = "running"
3386
- await Promise.race([runTimeoutPromise, new Promise((resolve, reject) => {
3505
+ const runAgent = () => Promise.race([runTimeoutPromise, new Promise((resolve, reject) => {
3506
+ if (runEnded) { reject(new Error("The run already ended.")); return }
3387
3507
  const child = spawn(execSpec.command, execSpec.args, {
3388
3508
  cwd: worktreePath,
3389
3509
  stdio: ["pipe", "pipe", "pipe"],
@@ -3431,7 +3551,9 @@ async function runExecutor(run, state) {
3431
3551
  if (result) {
3432
3552
  // Claude Code prices every run at Anthropic's rates; for
3433
3553
  // another provider that number is wrong, so keep tokens only.
3434
- runUsage = usesThirdPartyModels(run) ? { ...result.usage, cost_usd: null } : result.usage
3554
+ const usage = usesThirdPartyModels(run) ? { ...result.usage, cost_usd: null } : result.usage
3555
+ // A fix round is a second agent process; the run's usage is the sum.
3556
+ runUsage = earlierUsage ? addUsage(earlierUsage, usage) : usage
3435
3557
  // The message, not the JSON around it, is what the
3436
3558
  // summary, the PR description and failure detection read.
3437
3559
  stdioSummary = result.text
@@ -3468,6 +3590,105 @@ async function runExecutor(run, state) {
3468
3590
  reject(new Error(formatChildExitError(activeExecutor(), code, stdioSummary)))
3469
3591
  })
3470
3592
  })])
3593
+ await runAgent()
3594
+
3595
+ // Before anything is committed or pushed, run the checks CI would run for
3596
+ // the changed areas on this machine. A failing check goes back to the
3597
+ // builder; a check this machine cannot run is noted and left to CI.
3598
+ const localChecks = !isReviewRun(run) && gitAvailable ? loadLocalChecks(worktreePath, runnerProfile) : null
3599
+ if (localChecks) {
3600
+ const stopRequested = () => runEnded || cancelRequested || Boolean(limitStop)
3601
+ // The checks run scripts from the branch, which the agent can edit: a
3602
+ // sandboxed run does not run them, and no run hands them its
3603
+ // credentials or the repository's secrets.
3604
+ const skipAll = runSandboxed(run) ? SANDBOXED_RUN_SKIP : null
3605
+ const checkEnv = checkEnvironment(process.env)
3606
+ let baseNote = ""
3607
+ const outcome = await Promise.race([runTimeoutPromise, runChecksUntilGreen({
3608
+ maxFixAttempts: localChecks.maxFixAttempts,
3609
+ shouldStop: stopRequested,
3610
+ check: async () => {
3611
+ const changed = await listBranchChangedFiles(worktreePath, run.__baseBranch)
3612
+ baseNote = changed.baseFound ? "" : "The base branch was not found in this checkout, so only uncommitted changes decided which checks ran."
3613
+ const planned = planLocalChecks(localChecks.workflow, changed.files, { databaseUrl: process.env[DATABASE_URL_ENV] || null })
3614
+ const plan = skipAll ? skipWholePlan(planned, skipAll) : planned
3615
+ if (!plan.length) return []
3616
+ // Not the AI tool: a failure here is never a usage limit.
3617
+ executorPhase = "finished"
3618
+ appendLogTail(logState, `> running local checks from ${localChecks.source}`)
3619
+ await flushRunnerLog().catch(() => {})
3620
+ const results = await runLocalChecks(plan, {
3621
+ cwd: worktreePath,
3622
+ resolveCwd: (root, dir) => path.resolve(root, dir),
3623
+ shouldStop: stopRequested,
3624
+ // The cancel, limit and timeout paths stop whatever childProcess points at.
3625
+ exec: (command, options) => {
3626
+ if (stopRequested()) return Promise.resolve({ code: null, output: "the run was stopped", timedOut: false })
3627
+ return execLocalCheck(command, {
3628
+ ...options,
3629
+ baseEnv: checkEnv,
3630
+ onChild: (child) => { childProcess = child },
3631
+ })
3632
+ },
3633
+ })
3634
+ for (const result of results) {
3635
+ appendLogTail(logState, `> local check ${result.status}: ${result.label}${result.note ? ` (${result.note})` : ""}`)
3636
+ }
3637
+ return results
3638
+ },
3639
+ fix: async (failed, attempt) => {
3640
+ if (runEnded) throw new Error("The run already ended.")
3641
+ if (cancelRequested) throw new Error("Canceled by admin.")
3642
+ if (limitStop) throw new Error(tripwireHeadline(limitStop))
3643
+ const names = failed.map(result => result.label).join("; ")
3644
+ runLogger.warn("runner.local_checks.failed", `Local checks failed, sending them back to the builder (round ${attempt})`, {
3645
+ meta: { failed: failed.map(result => result.label), attempt },
3646
+ })
3647
+ await postIssueMilestone(
3648
+ mcpKey,
3649
+ run.task_id,
3650
+ `Local checks failed before the push: ${truncate(names, 400)}. Nothing was pushed; the builder is fixing them (round ${attempt} of ${localChecks.maxFixAttempts}).`,
3651
+ "warning",
3652
+ ).catch(() => {})
3653
+ const spent = runUsage?.cost_usd ?? 0
3654
+ earlierUsage = runUsage
3655
+ stdioSummary = ""
3656
+ stdoutRaw = ""
3657
+ if (execSpec.summaryMode === "file") fs.rmSync(outputFile, { force: true })
3658
+ execSpec = buildExecutorCommand(
3659
+ run,
3660
+ worktreePath,
3661
+ `${prompt}\n\n${buildFixInstructions(failed, { attempt, maxFixAttempts: localChecks.maxFixAttempts, source: localChecks.source })}`,
3662
+ outputFile,
3663
+ mcpKey,
3664
+ // The cost limit is for the whole run, so a fix round gets what is left of it.
3665
+ { ...executorOptions, maxCostUsd: executorOptions.maxCostUsd === null ? null : Math.max(0.01, executorOptions.maxCostUsd - spent) },
3666
+ )
3667
+ startStreamReader()
3668
+ appendLogTail(logState, `> local checks failed (${names}); starting the builder again to fix them`)
3669
+ executorPhase = "running"
3670
+ await runAgent()
3671
+ },
3672
+ })])
3673
+ if (cancelRequested) throw new Error("Canceled by admin.")
3674
+ if (limitStop) throw new Error(tripwireHeadline(limitStop))
3675
+ if (outcome.stopped) throw new Error("The run ended while its local checks were running.")
3676
+ localChecksSummary = [
3677
+ formatLocalChecks(outcome.results, { fixAttempts: outcome.fixAttempts, source: localChecks.source }),
3678
+ outcome.results.length ? baseNote : "",
3679
+ ].filter(Boolean).join("\n")
3680
+ if (!outcome.passed) {
3681
+ const failed = failedChecks(outcome.results)
3682
+ throw new Error(
3683
+ `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`
3684
+ + `${localChecksSummary}\n\n${failed[0].label}:\n${truncate(failed[0].output, 3000)}\n\n`
3685
+ + "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.",
3686
+ )
3687
+ }
3688
+ if (localChecksSummary) {
3689
+ await postIssueMilestone(mcpKey, run.task_id, localChecksSummary, "success").catch(() => {})
3690
+ }
3691
+ }
3471
3692
  }
3472
3693
 
3473
3694
  executorPhase = "finished"
@@ -3588,6 +3809,7 @@ async function runExecutor(run, state) {
3588
3809
  const verification = await runVerificationPlan(worktreePath, changedFiles, runnerProfile)
3589
3810
  const verificationSummary = [
3590
3811
  runnerVerificationSummary,
3812
+ localChecksSummary,
3591
3813
  verification.summary,
3592
3814
  ].filter(Boolean).join("\n")
3593
3815
  await postIssueMilestone(
@@ -3682,11 +3904,32 @@ async function runExecutor(run, state) {
3682
3904
 
3683
3905
  if (githubToken && repo?.owner && repo?.repo) {
3684
3906
  publishStage = "push"
3685
- await pushBranchToGitHub(worktreePath, branch, githubToken, repo, ({ attempt, delayMs, error }) => {
3907
+ const onPushRetry = ({ attempt, delayMs, error }) => {
3686
3908
  runLogger.warn("runner.run.push_retry", `GitHub push failed (attempt ${attempt}), retrying in ${Math.round(delayMs / 1000)}s`, {
3687
3909
  meta: { branch, reason: describeGitFailure(error instanceof Error ? error.message : String(error), 300) },
3688
3910
  })
3689
- })
3911
+ }
3912
+ /** Why the old pull request is being replaced, for the comment left on it. */
3913
+ let supersededReason = "It could not be continued on its own branch (for example, it comes from a fork)."
3914
+ try {
3915
+ await pushBranchToGitHub(worktreePath, branch, githubToken, repo, onPushRetry)
3916
+ } catch (pushRefused) {
3917
+ // The open pull request's branch does not accept this runner's push
3918
+ // (protected, or no permission). The work goes to a new branch and a
3919
+ // new pull request, and the old one is closed below with a link to it.
3920
+ const refusal = pushRefused instanceof Error ? pushRefused.message : String(pushRefused)
3921
+ if (!run.__openPullRequest || !isPushRefused(refusal)) throw pushRefused
3922
+ const fallbackBranch = `agent/${buildIssueToken(run.task, run.id)}-${shortBranchToken(run.id)}`
3923
+ const cut = await gitIn(worktreePath)(["checkout", "-b", fallbackBranch])
3924
+ if (cut.code !== 0) throw pushRefused
3925
+ runLogger.warn("runner.run.push_refused", `GitHub refused the push to ${branch}; continuing on ${fallbackBranch}`, {
3926
+ meta: { branch, fallback_branch: fallbackBranch, reason: describeGitFailure(refusal, 300) },
3927
+ })
3928
+ supersededReason = `The runner is not allowed to push to \`${branch}\`, so the work moved to \`${fallbackBranch}\`.`
3929
+ branch = fallbackBranch
3930
+ run.__supersededPullRequest = run.__openPullRequest
3931
+ await pushBranchToGitHub(worktreePath, branch, githubToken, repo, onPushRetry)
3932
+ }
3690
3933
  publishStage = "pull_request"
3691
3934
 
3692
3935
  // Base the PR on the target repo's base branch, never on the branch
@@ -3703,12 +3946,27 @@ async function runExecutor(run, state) {
3703
3946
  summary,
3704
3947
  changedFiles,
3705
3948
  diffStat: diffStat.stdout,
3706
- verificationSummary: runnerVerificationSummary,
3949
+ verificationSummary: [runnerVerificationSummary, localChecksSummary].filter(Boolean).join("\n\n"),
3707
3950
  }))
3708
3951
  if (pendingPushDir) clearPendingPush(pendingPushDir)
3709
3952
 
3710
3953
  pushResult = { pushed: true, repo: `${repo.owner}/${repo.repo}`, pr_url: pr.html_url, pr_number: pr.number }
3711
3954
 
3955
+ // One issue, one open pull request: the one this replaces is closed
3956
+ // with a comment linking the new one.
3957
+ if (run.__supersededPullRequest) {
3958
+ const superseded = await closeSupersededPullRequest({
3959
+ token: githubToken, repo, oldUrl: run.__supersededPullRequest, newUrl: pr.html_url, reason: supersededReason,
3960
+ })
3961
+ if (superseded.result === "closed") {
3962
+ runLogger.info("runner.pr.superseded_closed", `Closed ${run.__supersededPullRequest} in favour of ${pr.html_url}`)
3963
+ await postIssueMilestone(mcpKey, run.task_id, `Closed the earlier pull request ${run.__supersededPullRequest}: ${pr.html_url} continues the work. ${supersededReason}`, "info").catch(() => {})
3964
+ } else if (superseded.result === "failed") {
3965
+ runLogger.warn("runner.pr.superseded_close_failed", `Could not close ${run.__supersededPullRequest}`, { meta: { reason: superseded.reason } })
3966
+ await postIssueMilestone(mcpKey, run.task_id, `The earlier pull request ${run.__supersededPullRequest} is still open and could not be closed automatically (${superseded.reason}). Close it by hand: ${pr.html_url} continues the work.`, "warning").catch(() => {})
3967
+ }
3968
+ }
3969
+
3712
3970
  // Post PR URL back to the issue via MCP. This is what moves the
3713
3971
  // issue to in_review and starts the reviewer handoff, so a
3714
3972
  // silent failure here strands a finished pull request with no
@@ -3993,6 +4251,7 @@ async function runExecutor(run, state) {
3993
4251
  }, error)
3994
4252
  throw reportedRunFailure(error)
3995
4253
  } finally {
4254
+ runEnded = true
3996
4255
  clearInterval(logInterval)
3997
4256
  clearInterval(limitInterval)
3998
4257
  if (runTimeoutHandle) clearTimeout(runTimeoutHandle)
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Keep a run's branch in step with GitHub's copy of it (PWK-370).
3
+ *
4
+ * The branch a run works on often already has commits on GitHub that this
5
+ * machine has not seen: an open pull request a person or another runner
6
+ * opened, rework on a different Mac than the first build, or the merger
7
+ * bringing the branch up to date with main (PWK-365). Creating the branch
8
+ * from main dropped those commits, and a local branch left behind GitHub's
9
+ * was rejected on push.
10
+ *
11
+ * `exec(args)` runs `git <args>` and resolves `{ code, stdout }` without
12
+ * throwing, so the caller decides where commands run.
13
+ */
14
+
15
+ /** Whether `origin/<branch>` exists in this checkout's remote-tracking refs. */
16
+ export async function remoteBranchExists(exec, branch) {
17
+ const result = await exec(["rev-parse", "--verify", "--quiet", `refs/remotes/origin/${branch}`])
18
+ return result.code === 0 && result.stdout.trim().length > 0
19
+ }
20
+
21
+ /**
22
+ * Bring a checked-out branch up to GitHub's copy when GitHub is ahead.
23
+ * - "current": already at or ahead of GitHub's copy
24
+ * - "fast_forwarded": moved to GitHub's copy
25
+ * - "diverged": both have commits the other lacks; left alone, and
26
+ * pushKeepingRemoteCommits merges the two at push time
27
+ * - "no_remote": GitHub has no copy yet
28
+ * - "failed": the fast-forward did not apply (for example, uncommitted changes)
29
+ */
30
+ export async function catchUpWithRemote(exec, branch) {
31
+ if (!await remoteBranchExists(exec, branch)) return "no_remote"
32
+ const remote = `origin/${branch}`
33
+ const [head, theirs] = await Promise.all([exec(["rev-parse", "HEAD"]), exec(["rev-parse", remote])])
34
+ if (head.code !== 0 || theirs.code !== 0) return "failed"
35
+ if (head.stdout.trim() === theirs.stdout.trim()) return "current"
36
+ // GitHub's copy already contained in ours: we are ahead (an unpushed commit).
37
+ if ((await exec(["merge-base", "--is-ancestor", remote, "HEAD"])).code === 0) return "current"
38
+ if ((await exec(["merge-base", "--is-ancestor", "HEAD", remote])).code !== 0) return "diverged"
39
+ return (await exec(["merge", "--ff-only", "--quiet", remote])).code === 0 ? "fast_forwarded" : "failed"
40
+ }
41
+
42
+ /**
43
+ * Switch an existing checkout to the run's branch. A branch only GitHub has
44
+ * (a person's pull request) is created from GitHub's copy: cutting it from
45
+ * whatever the checkout was on left the pull request's commits out, and the
46
+ * push then replaced them.
47
+ * `exec` must throw or return a non-zero code on failure; the result of the
48
+ * checkout is returned.
49
+ */
50
+ export async function switchToBranch(exec, branch) {
51
+ if ((await exec(["rev-parse", "--verify", "--quiet", `refs/heads/${branch}`])).code === 0) {
52
+ return exec(["checkout", branch])
53
+ }
54
+ if (await remoteBranchExists(exec, branch)) {
55
+ return exec(["checkout", "--no-track", "-b", branch, `origin/${branch}`])
56
+ }
57
+ return exec(["checkout", "-b", branch])
58
+ }
59
+
60
+ const NON_FAST_FORWARD = /non-fast-forward|fetch first|stale info|\[rejected\]/i
61
+
62
+ /** GitHub refused the push because of who is pushing or where, not because of what. */
63
+ export function isPushRefused(message) {
64
+ return /protected branch|permission to \S+ denied|permission .* denied|returned error: 403|pre-receive hook declined|GH006|GH013/i.test(String(message ?? ""))
65
+ }
66
+
67
+ /**
68
+ * Push a branch without ever replacing commits GitHub has and this checkout
69
+ * lacks. The runner used to push with --force-with-lease right after a fetch,
70
+ * so the lease always matched: fine on an agent/ branch only it writes to, but
71
+ * on a person's pull request branch it removed whatever they had pushed while
72
+ * the run worked.
73
+ *
74
+ * GitHub's commits are merged into the branch first, then the branch is pushed
75
+ * plainly. A merge that conflicts is undone and reported; nothing is pushed.
76
+ *
77
+ * `fetch()` and `push()` resolve `{ code, stderr }` (they carry the caller's
78
+ * auth options); `exec(args)` runs local git the same way.
79
+ * Returns "pushed" or "merged_and_pushed"; throws with a readable message.
80
+ */
81
+ export async function pushKeepingRemoteCommits({ exec, fetch, push, branch, attempts = 3 }) {
82
+ let merged = false
83
+ let last = null
84
+ for (let attempt = 0; attempt < attempts; attempt += 1) {
85
+ // A failed fetch (GitHub has no such branch yet, or is unreachable) leaves
86
+ // nothing trustworthy to merge: a remote-tracking ref may be left over from
87
+ // a branch deleted after its merge. The plain push below is still safe.
88
+ const fetched = (await fetch()).code === 0
89
+ const remote = `origin/${branch}`
90
+ if (fetched && await remoteBranchExists(exec, branch) && (await exec(["merge-base", "--is-ancestor", remote, "HEAD"])).code !== 0) {
91
+ const merge = await exec(["merge", "--no-edit", remote])
92
+ if (merge.code !== 0) {
93
+ await exec(["merge", "--abort"])
94
+ const error = new Error(
95
+ `${branch} has commits on GitHub that this run did not have, and they conflict with the run's changes. `
96
+ + "Nothing was pushed and nothing on GitHub was changed. "
97
+ + `Merge origin/${branch} into the branch and resolve the conflicts, or run the agent again so it starts from GitHub's copy.`,
98
+ )
99
+ error.code = "REMOTE_CONFLICT"
100
+ throw error
101
+ }
102
+ merged = true
103
+ }
104
+ last = await push()
105
+ if (last.code === 0) return merged ? "merged_and_pushed" : "pushed"
106
+ // Someone pushed between the fetch and the push: take their commits and try again.
107
+ if (!NON_FAST_FORWARD.test(last.stderr || "") || isPushRefused(last.stderr)) break
108
+ }
109
+ throw new Error(last?.stderr || `git push origin ${branch} failed`)
110
+ }
@@ -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
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * One issue, one open pull request (PWK-370).
3
+ *
4
+ * Work on an issue normally continues on its open pull request. When it cannot
5
+ * (the pull request comes from a fork, or its branch refuses the runner's
6
+ * push) the runner opens a new one, and the old one is closed here with a
7
+ * comment that links the new one, so nobody has to notice and close it.
8
+ */
9
+
10
+ /** { owner, repo, number } from a GitHub pull request URL, or null. */
11
+ export function parsePullRequestUrl(url) {
12
+ const match = /github\.com\/([^/\s]+)\/([^/\s]+)\/pull\/(\d+)/i.exec(String(url ?? ""))
13
+ return match ? { owner: match[1], repo: match[2], number: Number(match[3]) } : null
14
+ }
15
+
16
+ /**
17
+ * Close `oldUrl` in favour of `newUrl` when it is another, still open pull
18
+ * request in the same repository. Returns what happened:
19
+ * "closed", "same", "other_repository", "not_open", or "failed" (with `reason`).
20
+ * Never throws: the new pull request is already open and must still be linked.
21
+ */
22
+ export async function closeSupersededPullRequest({ token, repo, oldUrl, newUrl, reason = "", fetchImpl = fetch }) {
23
+ const old = parsePullRequestUrl(oldUrl)
24
+ const next = parsePullRequestUrl(newUrl)
25
+ if (!old || !next) return { result: "same" }
26
+ const same = (a, b) => String(a).toLowerCase() === String(b).toLowerCase()
27
+ if (!same(old.owner, repo.owner) || !same(old.repo, repo.repo)) return { result: "other_repository" }
28
+ if (old.number === next.number) return { result: "same" }
29
+
30
+ const api = `https://api.github.com/repos/${repo.owner}/${repo.repo}`
31
+ const headers = {
32
+ Authorization: `Bearer ${token}`,
33
+ Accept: "application/vnd.github+json",
34
+ "X-GitHub-Api-Version": "2022-11-28",
35
+ "Content-Type": "application/json",
36
+ }
37
+ try {
38
+ const current = await fetchImpl(`${api}/pulls/${old.number}`, { headers })
39
+ if (!current.ok) return { result: "failed", reason: `GitHub answered ${current.status} for pull request #${old.number}` }
40
+ if ((await current.json())?.state !== "open") return { result: "not_open" }
41
+
42
+ const body = `Closed in favour of #${next.number} (${newUrl}), which continues this work.`
43
+ + (reason ? ` ${reason}` : "")
44
+ // Comment first: a pull request closed without a word is the worse outcome.
45
+ const comment = await fetchImpl(`${api}/issues/${old.number}/comments`, { method: "POST", headers, body: JSON.stringify({ body }) })
46
+ if (!comment.ok) return { result: "failed", reason: `GitHub answered ${comment.status} to the comment on #${old.number}` }
47
+ const closed = await fetchImpl(`${api}/pulls/${old.number}`, { method: "PATCH", headers, body: JSON.stringify({ state: "closed" }) })
48
+ if (!closed.ok) return { result: "failed", reason: `GitHub answered ${closed.status} when closing #${old.number}` }
49
+ return { result: "closed", number: old.number }
50
+ } catch (error) {
51
+ return { result: "failed", reason: error instanceof Error ? error.message : String(error) }
52
+ }
53
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pacework",
3
- "version": "1.7.3",
3
+ "version": "1.7.5",
4
4
  "description": "pacework CLI: connect this machine as a runner for pacework AI agents",
5
5
  "type": "module",
6
6
  "bin": {