ticket-runner 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.ko.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # ticket-runner
2
2
 
3
+ [![npm](https://img.shields.io/npm/v/ticket-runner)](https://www.npmjs.com/package/ticket-runner)
4
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue)](https://github.com/jjongs2/ticket-runner/blob/main/LICENSE)
5
+
3
6
  [English](https://github.com/jjongs2/ticket-runner/blob/main/README.md)
4
7
 
5
8
  `ticket-runner`는 [`mattpocock-skills`](https://github.com/mattpocock/skills) 체인에서 사람이 지켜보지
@@ -21,8 +24,8 @@ Acceptance Criteria 체크리스트가 달리고, blocker가 연결됩니다. **
21
24
 
22
25
  한 번 실패하면 고치는 세션 한 번과 두 번째 검사를 받습니다. 그래도 끝내지 못한 일은 사람에게 돌아옵니다.
23
26
  `ready-for-human` 라벨과 draft pull request, 무엇이 실패했는지 적은 코멘트가 함께 남습니다. 구독의
24
- rate limit에 걸려 멈춘 세션은 아무것도 잃지 않습니다. 이슈는 보드로 돌아가고, 다음 Run이 멈춘 곳부터
25
- 이어 합니다.
27
+ rate limit이나 일시적인 API 장애로 멈춘 세션은 아무것도 잃지 않습니다. 이슈는 보드로 돌아가고, Run은 한도가
28
+ 풀리는 시각을 알려 주며, 다음 Run이 멈춘 곳부터 이어 합니다.
26
29
 
27
30
  ## 나에게 맞을까?
28
31
 
@@ -32,7 +35,7 @@ rate limit에 걸려 멈춘 세션은 아무것도 잃지 않습니다. 이슈
32
35
  설정해 두었고, Ticket 하나는 세션 하나로 끝낼 만큼 작습니다.
33
36
  - pull request에서 도는 CI 워크플로가 있고, 파이프라인이 직접 돌릴 테스트나 typecheck 명령이 있습니다.
34
37
  - 돌리는 머신에 Node 22 이상, `git`, 인증된 [`gh`](https://cli.github.com/), 그리고
35
- [`mattpocock-skills`](https://github.com/mattpocock/skills) 플러그인 1.2.3 버전을 설치한 `claude`가
38
+ [`mattpocock-skills`](https://github.com/mattpocock/skills) 플러그인 1.2.3 이상 버전을 설치한 `claude`가
36
39
  있습니다.
37
40
 
38
41
  파이프라인은 묻지 않고 base branch에 merge합니다. 그걸 원하는 저장소에 쓰세요.
@@ -91,7 +94,7 @@ npm uninstall -g ticket-runner # 전역 설치
91
94
  - [멈추고 이어 하기](https://jjongs2.github.io/ticket-runner/ko/guide/stopping-and-resuming)
92
95
  - [내부 구조](https://jjongs2.github.io/ticket-runner/ko/guide/internals)
93
96
 
94
- 용어는 [`CONTEXT.md`](https://github.com/jjongs2/ticket-runner/blob/main/CONTEXT.md)에, 설계 결정은
97
+ 용어는 [`GLOSSARY.md`](https://github.com/jjongs2/ticket-runner/blob/main/GLOSSARY.md)에, 설계 결정은
95
98
  [`docs/adr/`](https://github.com/jjongs2/ticket-runner/tree/main/docs/adr)에, 파이프라인 자체를 고칠
96
99
  때의 규칙은 [`CONTRIBUTING.md`](https://github.com/jjongs2/ticket-runner/blob/main/CONTRIBUTING.md)에
97
100
  있습니다. 모두 영어입니다.
package/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # ticket-runner
2
2
 
3
+ [![npm](https://img.shields.io/npm/v/ticket-runner)](https://www.npmjs.com/package/ticket-runner)
4
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue)](https://github.com/jjongs2/ticket-runner/blob/main/LICENSE)
5
+
3
6
  [한국어](https://github.com/jjongs2/ticket-runner/blob/main/README.ko.md)
4
7
 
5
8
  `ticket-runner` runs the unattended half of the
@@ -21,8 +24,9 @@ every such issue whose blockers have closed, it:
21
24
 
22
25
  One failure buys one fix session and a second pass. Anything it still cannot finish goes back
23
26
  to you, labelled `ready-for-human`, with a draft pull request and a comment saying what failed.
24
- A session stopped by your subscription's rate limit costs nothing: the issue goes back on the
25
- board, and the next Run carries on from where it stopped.
27
+ A session stopped by your subscription's rate limit, or by the API failing on its side, costs
28
+ nothing: the issue goes back on the board, the Run says when the limit resets, and the next Run
29
+ carries on from where it stopped.
26
30
 
27
31
  ## Is it for you?
28
32
 
@@ -34,7 +38,7 @@ It fits a repository where:
34
38
  run itself
35
39
  - the machine running it has Node 22 or newer, `git`, an authenticated
36
40
  [`gh`](https://cli.github.com/), and `claude` with the
37
- [`mattpocock-skills`](https://github.com/mattpocock/skills) plugin, version 1.2.3
41
+ [`mattpocock-skills`](https://github.com/mattpocock/skills) plugin, version 1.2.3 or later
38
42
 
39
43
  It merges to your base branch without asking. It is for repositories where that is what you want.
40
44
 
@@ -93,6 +97,6 @@ The [project wiki](https://jjongs2.github.io/ticket-runner/) explains the pipeli
93
97
  - [Stopping and resuming](https://jjongs2.github.io/ticket-runner/guide/stopping-and-resuming)
94
98
  - [Internals](https://jjongs2.github.io/ticket-runner/guide/internals)
95
99
 
96
- The vocabulary is defined in [`CONTEXT.md`](https://github.com/jjongs2/ticket-runner/blob/main/CONTEXT.md), the decisions in
100
+ The vocabulary is defined in [`GLOSSARY.md`](https://github.com/jjongs2/ticket-runner/blob/main/GLOSSARY.md), the decisions in
97
101
  [`docs/adr/`](https://github.com/jjongs2/ticket-runner/tree/main/docs/adr), and the conventions for working on the pipeline itself in
98
102
  [`CONTRIBUTING.md`](https://github.com/jjongs2/ticket-runner/blob/main/CONTRIBUTING.md).
@@ -16,7 +16,7 @@ Install only what this Host does not already have. What the cloud environment's
16
16
  1. The pipeline. If `ticket-runner --version` answers, use that copy. Otherwise read the Version on the first line of `docs/agents/pipeline-conventions.md`, the `<number>` in `<!-- ticket-runner:version <number> -->`, and install that Version:
17
17
 
18
18
  ```bash
19
- npm install -g "github:jjongs2/ticket-runner#v<number>"
19
+ npm install -g ticket-runner@<number>
20
20
  ```
21
21
 
22
22
  If the first line carries no Version, install nothing and tell the human to run `ticket-runner init` on this repository from a workstation.
package/package.json CHANGED
@@ -1,7 +1,14 @@
1
1
  {
2
2
  "name": "ticket-runner",
3
- "version": "0.6.0",
4
- "description": "Drives the mattpocock-skills chain unattended: humans plan, the pipeline executes.",
3
+ "version": "0.6.2",
4
+ "description": "Drives the mattpocock-skills chain: humans plan, the pipeline executes.",
5
+ "keywords": [
6
+ "claude-code",
7
+ "cli",
8
+ "github-issues",
9
+ "mattpocock-skills"
10
+ ],
11
+ "homepage": "https://jjongs2.github.io/ticket-runner/",
5
12
  "license": "MIT",
6
13
  "repository": {
7
14
  "type": "git",
@@ -33,8 +40,8 @@
33
40
  },
34
41
  "devDependencies": {
35
42
  "@types/node": "^22.18.9",
36
- "typescript": "^5.9.3",
37
- "vitest": "^3.2.4"
43
+ "typescript": "^7.0.2",
44
+ "vitest": "^5.0.4"
38
45
  },
39
46
  "allowScripts": {
40
47
  "esbuild@0.28.2": true
@@ -1,4 +1,5 @@
1
- import { appendFileSync, mkdirSync, writeFileSync } from "node:fs";
1
+ import { randomUUID } from "node:crypto";
2
+ import { appendFileSync, existsSync, mkdirSync, writeFileSync } from "node:fs";
2
3
  import { join } from "node:path";
3
4
  import {
4
5
  type AgentPreflight,
@@ -10,12 +11,44 @@ import {
10
11
  } from "../ports/agent-runner.js";
11
12
  import { STAGE_ENV_VAR } from "../stage-guard.js";
12
13
  import { type Execution, type RunProcess, exec } from "./exec.js";
14
+ import type { Mark } from "./process-tree.js";
13
15
 
14
16
  export interface ClaudeAgentRunnerOptions {
15
17
  binary?: string;
16
18
  run?: RunProcess;
19
+ /** Whether a file is on the Host, asked of the Stage's shell. */
20
+ exists?: (path: string) => boolean;
17
21
  }
18
22
 
23
+ /**
24
+ * The shell a Stage's commands run in. The Host user's own may be zsh, which
25
+ * aborts the whole command line on a glob that matches nothing or a word that
26
+ * starts with `=`, so the session would spend turns finding out why.
27
+ */
28
+ const STAGE_SHELL = "/bin/bash";
29
+
30
+ /**
31
+ * The Session mark: a variable every Stage carries at a value no other Stage
32
+ * session shares, and every process it starts inherits. A Stage killed at its
33
+ * time limit is killed with every process still carrying its value, whoever
34
+ * their parent is by then.
35
+ */
36
+ const SESSION_ENV_VAR = "TICKET_RUNNER_SESSION";
37
+
38
+ /**
39
+ * The tools that act beyond a Stage's worktree and its session: a cloud
40
+ * routine, a scheduled prompt, a notification, a wakeup after the Stage has
41
+ * ended, a workflow of further agents. Nothing a Stage is asked to do needs one.
42
+ */
43
+ const DISALLOWED_TOOLS = [
44
+ "CronCreate",
45
+ "CronDelete",
46
+ "RemoteTrigger",
47
+ "PushNotification",
48
+ "ScheduleWakeup",
49
+ "Workflow",
50
+ ];
51
+
19
52
  /**
20
53
  * How long one preflight question may take. Generous for a CLI printing its
21
54
  * own version, and short enough that `init` cannot hang on a binary that never
@@ -31,8 +64,16 @@ interface ResultEvent {
31
64
  num_turns?: number;
32
65
  result?: unknown;
33
66
  structured_output?: unknown;
34
- /** The HTTP status of the API error that ended the session, when one did. */
35
- api_error_status?: number;
67
+ /**
68
+ * Why the session's loop stopped. `api_error` is a request the API answered
69
+ * with an error, or never answered, after the CLI's own retries ran out.
70
+ */
71
+ terminal_reason?: string;
72
+ /**
73
+ * The HTTP status of the API error that ended the session, when one did. The
74
+ * CLI prints `null` for a request that never got an answer.
75
+ */
76
+ api_error_status?: number | null;
36
77
  }
37
78
 
38
79
  /**
@@ -41,7 +82,8 @@ interface ResultEvent {
41
82
  */
42
83
  interface RateLimitEvent {
43
84
  type: "rate_limit_event";
44
- rate_limit_info?: { status?: string };
85
+ /** `resetsAt` is when the limit resets, in seconds since the epoch. */
86
+ rate_limit_info?: { status?: string; resetsAt?: number };
45
87
  }
46
88
 
47
89
  /**
@@ -53,10 +95,12 @@ interface RateLimitEvent {
53
95
  export class ClaudeAgentRunner implements AgentRunner {
54
96
  private readonly binary: string;
55
97
  private readonly runProcess: RunProcess;
98
+ private readonly exists: (path: string) => boolean;
56
99
 
57
100
  constructor(options: ClaudeAgentRunnerOptions = {}) {
58
101
  this.binary = options.binary ?? "claude";
59
102
  this.runProcess = options.run ?? exec;
103
+ this.exists = options.exists ?? existsSync;
60
104
  }
61
105
 
62
106
  /**
@@ -88,14 +132,16 @@ export class ClaudeAgentRunner implements AgentRunner {
88
132
  private async spawn(
89
133
  request: StageRequest,
90
134
  args: string[],
135
+ env: Record<string, string>,
136
+ session: Mark,
91
137
  log: StageLog,
92
138
  ): Promise<Execution> {
93
139
  try {
94
140
  return await this.runProcess(this.binary, args, {
95
141
  cwd: request.cwd,
96
142
  timeoutMs: request.maxMinutes * 60_000,
97
- // The Stage mark the CLI refuses on, so the session cannot nest a Run.
98
- extraEnv: stageEnv(request),
143
+ extraEnv: env,
144
+ mark: session,
99
145
  onStdout: log.appendStdout,
100
146
  onStderr: log.appendStderr,
101
147
  });
@@ -106,13 +152,15 @@ export class ClaudeAgentRunner implements AgentRunner {
106
152
 
107
153
  async run(request: StageRequest): Promise<StageResult> {
108
154
  const args = buildArgs(request);
109
- const commandLine = quoteCommand(stageEnv(request), this.binary, args);
155
+ const session = { name: SESSION_ENV_VAR, value: randomUUID() };
156
+ const env = this.stageEnv(request, session);
157
+ const commandLine = quoteCommand(env, this.binary, args);
110
158
  const startedAt = Date.now();
111
159
 
112
160
  // Started before the child, so a Run killed mid-Stage still leaves the
113
161
  // command line and everything printed so far behind.
114
162
  const log = startStageLog(request, commandLine);
115
- const execution = await this.spawn(request, args, log);
163
+ const execution = await this.spawn(request, args, env, session, log);
116
164
 
117
165
  const events = parseEvents(execution.stdout);
118
166
  log.close(execution, events);
@@ -121,6 +169,7 @@ export class ClaudeAgentRunner implements AgentRunner {
121
169
  const failure = classify(request, execution, events);
122
170
  const structured = request.jsonSchema === undefined ? undefined : structuredOutput(results);
123
171
  const turns = countTurns(results);
172
+ const resetsAt = failure === "rate-limited" ? limitResetsAt(events) : undefined;
124
173
 
125
174
  return {
126
175
  ok: failure === undefined,
@@ -129,12 +178,27 @@ export class ClaudeAgentRunner implements AgentRunner {
129
178
  commandLine,
130
179
  transcriptPath: log.transcriptPath,
131
180
  ...(turns === undefined ? {} : { turns }),
181
+ ...(resetsAt === undefined ? {} : { limitResetsAt: resetsAt }),
132
182
  // The clock is the runner's own. A session that woke for a background
133
183
  // agent reports one `duration_ms` per waking, and the time it spent
134
184
  // waiting for that agent belongs to none of them.
135
185
  durationMs: Date.now() - startedAt,
136
186
  };
137
187
  }
188
+
189
+ /** What a Stage's environment carries beyond the one it inherits. */
190
+ private stageEnv(request: StageRequest, session: Mark): Record<string, string> {
191
+ return {
192
+ // The Stage mark the CLI refuses on, so the session cannot nest a Run.
193
+ [STAGE_ENV_VAR]: request.stage,
194
+ // Two Lanes in the same Stage share the Stage mark, never this one.
195
+ [session.name]: session.value,
196
+ // The Host user's memory is about their own projects, not the Target.
197
+ CLAUDE_CODE_DISABLE_AUTO_MEMORY: "1",
198
+ // Where the Host has no bash, its own shell is still better than none.
199
+ ...(this.exists(STAGE_SHELL) ? { CLAUDE_CODE_SHELL: STAGE_SHELL } : {}),
200
+ };
201
+ }
138
202
  }
139
203
 
140
204
  /**
@@ -155,11 +219,6 @@ async function spawnFailure(error: unknown): Promise<Execution> {
155
219
  return { exitCode: 1, stdout: "", stderr: message, output: message, timedOut: false };
156
220
  }
157
221
 
158
- /** What a Stage's shell carries beyond the environment it inherits. */
159
- function stageEnv(request: StageRequest): Record<string, string> {
160
- return { [STAGE_ENV_VAR]: request.stage };
161
- }
162
-
163
222
  function buildArgs(request: StageRequest): string[] {
164
223
  const args = [
165
224
  "--print",
@@ -172,6 +231,11 @@ function buildArgs(request: StageRequest): string[] {
172
231
  "none",
173
232
  "--permission-mode",
174
233
  request.permissionMode,
234
+ // Without `--mcp-config`, this leaves the session no MCP server at all,
235
+ // the claude.ai connectors of the Host user's account included.
236
+ "--strict-mcp-config",
237
+ "--disallowedTools",
238
+ ...DISALLOWED_TOOLS,
175
239
  "--model",
176
240
  request.model,
177
241
  // Always passed, like the model: a Stage's logged command line is what a
@@ -294,6 +358,7 @@ function classify(
294
358
  const failed = execution.exitCode !== 0 || result?.is_error === true || result === undefined;
295
359
  if (failed) {
296
360
  if (rateLimited(execution, events, result)) return "rate-limited";
361
+ if (transientApiFailure(result)) return "api-error";
297
362
  return "nonzero-exit";
298
363
  }
299
364
  // A schema the Stage answered with nothing is only a failure where the
@@ -326,13 +391,41 @@ function rateLimited(
326
391
  result: ResultEvent | undefined,
327
392
  ): boolean {
328
393
  if (result?.api_error_status === 429) return true;
329
- const rejected = (event: unknown) =>
330
- isRateLimitEvent(event) && event.rate_limit_info?.status === "rejected";
331
394
  if (events.some(rejected)) return true;
332
395
  const text = [result?.subtype ?? "", String(result?.result ?? ""), execution.stderr].join(" ");
333
396
  return /usage limit|session limit|rate limit|rate_limit/i.test(text);
334
397
  }
335
398
 
399
+ /** A `rate_limit_event` that says the request was refused, not merely warned about. */
400
+ function rejected(event: unknown): event is RateLimitEvent {
401
+ return isRateLimitEvent(event) && event.rate_limit_info?.status === "rejected";
402
+ }
403
+
404
+ /**
405
+ * When the limit that stopped a session resets, ISO-8601: the last rejecting
406
+ * event's word on it, or nothing when no rejecting event carried one.
407
+ */
408
+ function limitResetsAt(events: unknown[]): string | undefined {
409
+ const resetsAt = events.filter(rejected).at(-1)?.rate_limit_info?.resetsAt;
410
+ if (typeof resetsAt !== "number" || !Number.isFinite(resetsAt)) return undefined;
411
+ return new Date(resetsAt * 1000).toISOString();
412
+ }
413
+
414
+ /**
415
+ * Whether a session that failed, and was not rate-limited, was stopped by the
416
+ * API failing on its own side: an overloaded or broken server, a 5xx, or a
417
+ * request that never got an answer at all, which the CLI reports without a
418
+ * status. Asked only after {@link rateLimited}, because a 429 ends the session
419
+ * with the same `terminal_reason`. The CLI has already retried each of these by
420
+ * the time it gives up, so the session is not worth retrying in place either.
421
+ * Any other status — a prompt the API refused, say — is the session's own.
422
+ */
423
+ function transientApiFailure(result: ResultEvent | undefined): boolean {
424
+ if (result?.terminal_reason !== "api_error") return false;
425
+ const status = result.api_error_status;
426
+ return status === undefined || status === null || (status >= 500 && status <= 599);
427
+ }
428
+
336
429
  /**
337
430
  * The Stage's structured output, from the last `result` event that carries one.
338
431
  *
@@ -1,4 +1,5 @@
1
1
  import { spawn } from "node:child_process";
2
+ import { type Mark, killTree } from "./process-tree.js";
2
3
 
3
4
  export interface Execution {
4
5
  exitCode: number;
@@ -19,8 +20,19 @@ export interface ExecOptions {
19
20
  /** Run through the shell, for user-supplied Check commands. */
20
21
  shell?: boolean;
21
22
  timeoutMs?: number;
23
+ /**
24
+ * How long a command killed at `timeoutMs` has between SIGTERM and SIGKILL.
25
+ * `KILL_GRACE_MS` unless a test needs it shorter.
26
+ */
27
+ killGraceMs?: number;
22
28
  /** Variables merged over the environment the child inherits, not replacing it. */
23
29
  extraEnv?: Record<string, string>;
30
+ /**
31
+ * A variable set over `extraEnv`, whose value nothing else on the Host
32
+ * carries. A kill at `timeoutMs` also takes every process still carrying it,
33
+ * which reaches what the child started after its parent has exited.
34
+ */
35
+ mark?: Mark;
24
36
  /**
25
37
  * Called with each chunk the child prints on that stream, as it arrives, so
26
38
  * a caller that saves the output has it before the child exits. A sink runs
@@ -45,6 +57,13 @@ export interface ExecOptions {
45
57
  */
46
58
  const DRAIN_MS = 100;
47
59
 
60
+ /**
61
+ * How long a command killed at its time limit, and everything it started, has
62
+ * to end on SIGTERM before SIGKILL. Long enough for a session to write out what
63
+ * it holds, short enough that the Ticket is not held up waiting.
64
+ */
65
+ const KILL_GRACE_MS = 5_000;
66
+
48
67
  /**
49
68
  * How a child process is run. Adapters take one so tests can hand them
50
69
  * recorded output instead of spawning anything.
@@ -62,11 +81,12 @@ export function exec(
62
81
  options: ExecOptions = {},
63
82
  ): Promise<Execution> {
64
83
  return new Promise((resolve, reject) => {
84
+ const { mark } = options;
85
+ const extraEnv =
86
+ mark === undefined ? options.extraEnv : { ...options.extraEnv, [mark.name]: mark.value };
65
87
  const child = spawn(command, args, {
66
88
  ...(options.cwd === undefined ? {} : { cwd: options.cwd }),
67
- ...(options.extraEnv === undefined
68
- ? {}
69
- : { env: { ...process.env, ...options.extraEnv } }),
89
+ ...(extraEnv === undefined ? {} : { env: { ...process.env, ...extraEnv } }),
70
90
  shell: options.shell ?? false,
71
91
  stdio: ["ignore", "pipe", "pipe"],
72
92
  });
@@ -78,6 +98,7 @@ export function exec(
78
98
  let settled = false;
79
99
  let limit: NodeJS.Timeout | undefined;
80
100
  let drain: NodeJS.Timeout | undefined;
101
+ let killing: Promise<void> | undefined;
81
102
 
82
103
  /** The child itself is gone. Only a descendant can still hold its pipes. */
83
104
  const hasExited = () =>
@@ -95,13 +116,17 @@ export function exec(
95
116
  const finish = () => {
96
117
  if (settled) return;
97
118
  stopWaiting();
98
- resolve({
119
+ const execution = {
99
120
  exitCode: killed ? 124 : (child.exitCode ?? 1),
100
121
  stdout,
101
122
  stderr,
102
123
  output,
103
124
  timedOut: killed,
104
- });
125
+ };
126
+ // A killed command is reported once what it started is gone too, so the
127
+ // caller never works beside what is left of it.
128
+ if (killing === undefined) resolve(execution);
129
+ else void killing.then(() => resolve(execution));
105
130
  };
106
131
 
107
132
  /** Keep reading for a moment, then stop waiting on the pipes for good. */
@@ -115,7 +140,11 @@ export function exec(
115
140
  // the drain, and its own exit code is the honest one to report.
116
141
  if (hasExited()) return;
117
142
  killed = true;
118
- child.kill("SIGKILL");
143
+ if (child.pid === undefined) child.kill("SIGKILL");
144
+ else {
145
+ const graceMs = options.killGraceMs ?? KILL_GRACE_MS;
146
+ killing = killTree(child.pid, graceMs, mark === undefined ? {} : { mark });
147
+ }
119
148
  }, options.timeoutMs);
120
149
  }
121
150
 
@@ -16,6 +16,7 @@ import type {
16
16
  } from "../ports/tracker.js";
17
17
  import { DEFAULT_CI_GRACE_MINUTES } from "../config.js";
18
18
  import { type HostKind, hostKind } from "../host.js";
19
+ import { MAX_TAIL_LINES, logTail } from "../log-tail.js";
19
20
  import { highestVersion } from "../version-number.js";
20
21
  import { type Execution, type RunProcess, exec, throwOnFailure } from "./exec.js";
21
22
 
@@ -587,31 +588,30 @@ export class GhTracker implements Tracker {
587
588
  const logs: string[] = [];
588
589
  for (const { name, job, link } of jobs.slice(0, MAX_LOG_JOBS)) {
589
590
  const log = await this.jobLog(job);
591
+ const tail = log === undefined ? "" : logTail(log);
590
592
  logs.push(
591
- log === undefined
593
+ tail === ""
592
594
  ? `${name}\n(the log was unavailable; the job is at ${link})`
593
- : `${name}\n${tail(log)}`,
595
+ : `${name}\n${tail}`,
594
596
  );
595
597
  }
596
598
  return logs.join("\n\n");
597
599
  }
598
600
 
599
601
  /**
600
- * One Actions job's log through REST, up to the last line the runner marked
601
- * as an error, or nothing when there is no log to be had.
602
+ * One Actions job's whole log through REST, or nothing when there is no log
603
+ * to be had.
602
604
  *
603
605
  * REST gives the whole job where `gh run view --log-failed` gave the failed
604
- * steps, so what the post-job cleanup printed after the failure is cut off:
605
- * otherwise it would be the tail that is kept.
606
+ * steps, so {@link logTail} cuts what the post-job cleanup printed after the
607
+ * failure.
606
608
  */
607
609
  private async jobLog(job: string): Promise<string | undefined> {
608
610
  try {
609
611
  const result = await this.gh(["api", `repos/{owner}/{repo}/actions/jobs/${job}/logs`], {
610
612
  allowFailure: true,
611
613
  });
612
- if (result.exitCode !== 0) return undefined;
613
- const log = throughLastError(result.stdout).trim();
614
- return log === "" ? undefined : log;
614
+ return result.exitCode === 0 ? result.stdout : undefined;
615
615
  } catch {
616
616
  // `gh` itself could not be run. The failure still stands; the log does not.
617
617
  return undefined;
@@ -806,16 +806,10 @@ const ACTIONS_JOB_URL = /\/actions\/runs\/\d+\/job\/(\d+)(?:[?#]|$)/;
806
806
  *
807
807
  * A matrix build fails a leg at a time with the same log each time, and the
808
808
  * prompt and the hand-off comment both have to stay readable, so the excerpt
809
- * is bounded by this times {@link MAX_LOG_LINES} rather than by the build.
809
+ * is bounded by this times {@link MAX_TAIL_LINES} rather than by the build.
810
810
  */
811
811
  const MAX_LOG_JOBS = 3;
812
812
 
813
- /** Lines kept from the tail of one job's log. */
814
- const MAX_LOG_LINES = 40;
815
-
816
- /** Characters kept from the tail of one job's log, for lines long enough to need it. */
817
- const MAX_LOG_CHARS = 4_000;
818
-
819
813
  /**
820
814
  * A comment's id, as the string {@link Tracker.updateComment} takes, or nothing
821
815
  * when REST reported none.
@@ -914,13 +908,6 @@ function linked(url: string | null | undefined): { link?: string } {
914
908
  return url ? { link: url } : {};
915
909
  }
916
910
 
917
- /** Everything up to the last line an Actions runner marked `##[error]`, or all of it. */
918
- function throughLastError(log: string): string {
919
- const lines = log.split("\n");
920
- const last = lines.findLastIndex((line) => line.includes("##[error]"));
921
- return last === -1 ? log : lines.slice(0, last + 1).join("\n");
922
- }
923
-
924
911
  /**
925
912
  * The Actions job id behind a check's link, or nothing when the check does not
926
913
  * report from one. An external app's check has a link of its own shape and no
@@ -930,26 +917,3 @@ function actionsJobId(link: string | undefined): string | undefined {
930
917
  const match = link === undefined ? null : ACTIONS_JOB_URL.exec(link);
931
918
  return match ? (match[1] as string) : undefined;
932
919
  }
933
-
934
- /**
935
- * The end of a log, under a fixed cap, saying what it left out.
936
- *
937
- * The tail is the part worth carrying: a runner prints the assertion or the
938
- * stack that ended the job last, under however much setup noise came first.
939
- */
940
- function tail(log: string): string {
941
- const lines = log.split("\n");
942
- let kept = lines.slice(-MAX_LOG_LINES);
943
- while (kept.length > 1 && kept.join("\n").length > MAX_LOG_CHARS) kept = kept.slice(1);
944
-
945
- const text = kept.join("\n");
946
- const omitted = lines.length - kept.length;
947
-
948
- // A single line longer than the whole cap keeps its own tail: the error is at
949
- // the end of it. Its own start is cut too, which the count alone would not say.
950
- if (text.length > MAX_LOG_CHARS) {
951
- const earlier = omitted > 0 ? `${omitted} earlier lines and the start of this one` : "the start of this line";
952
- return `… (${earlier} omitted)\n${text.slice(-MAX_LOG_CHARS)}`;
953
- }
954
- return omitted > 0 ? `… (${omitted} earlier lines omitted)\n${text}` : text;
955
- }
@@ -54,6 +54,9 @@ export { STATE_BRANCH };
54
54
  /** The name every Ticket's worktree has under `.worktrees/`. */
55
55
  const TICKET_WORKTREE = /^ticket-\d+$/;
56
56
 
57
+ /** The name a worktree at a merge-base has there, which a Check runs in. */
58
+ const BASE_WORKTREE = /^base-[0-9a-f]+$/;
59
+
57
60
  /**
58
61
  * How many times a change to the state branch is tried, read again from the
59
62
  * remote each time, before its push failing is an error. Meant for a remote
@@ -160,6 +163,12 @@ export class GitWorkspace implements Workspace {
160
163
  await this.git(["worktree", "add", "--no-track", "-b", branch, path, from]);
161
164
  }
162
165
 
166
+ async createDetachedWorktree(path: string, commit: string): Promise<void> {
167
+ if (existsSync(path)) await this.tryGit(["worktree", "remove", "--force", path]);
168
+ rmSync(path, { recursive: true, force: true });
169
+ await this.addWorktree(["--detach", path, commit]);
170
+ }
171
+
163
172
  async removeWorktree({ path, branch }: { path: string; branch?: string }): Promise<void> {
164
173
  await this.git(["worktree", "remove", "--force", path]);
165
174
  // A worktree directory left behind would block the next Run on this Ticket.
@@ -182,8 +191,14 @@ export class GitWorkspace implements Workspace {
182
191
  const listed = lines.find((line) => line.startsWith("worktree "))?.slice(9);
183
192
  if (listed === undefined) continue;
184
193
  const path = resolve(listed);
185
- if (dirname(path) !== worktrees || !TICKET_WORKTREE.test(basename(path))) continue;
186
- if (!existsSync(path)) continue;
194
+ if (dirname(path) !== worktrees || !existsSync(path)) continue;
195
+ // A checkout of a commit the remote already has, and whatever a Check
196
+ // printed into it: nothing there is anybody's work.
197
+ if (BASE_WORKTREE.test(basename(path))) {
198
+ found.push({ path, uncommitted: false, unpushed: false });
199
+ continue;
200
+ }
201
+ if (!TICKET_WORKTREE.test(basename(path))) continue;
187
202
 
188
203
  const branch = lines
189
204
  .find((line) => line.startsWith("branch refs/heads/"))
@@ -374,6 +389,12 @@ export class GitWorkspace implements Workspace {
374
389
  return result.exitCode === 0;
375
390
  }
376
391
 
392
+ async mergeBase(cwd: string, base: string): Promise<string> {
393
+ const from = (await this.fetchBranch(base)) ?? base;
394
+ const { stdout } = await execOrThrow("git", ["merge-base", "HEAD", from], { cwd });
395
+ return stdout.trim();
396
+ }
397
+
377
398
  async commitSubjects(branch: string, base: string): Promise<string[]> {
378
399
  // --reverse turns git's newest-first log into the order they were written.
379
400
  const { stdout } = await this.git([