dorfl 0.11.1 → 0.11.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/src/pi-harness.ts CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  } from './harness.js';
14
14
  import {generateSessionPath} from './session-path.js';
15
15
  import {lastAssistantText} from './watch-session.js';
16
+ import {reapProcessGroup} from './reap-agent-tree.js';
16
17
  import type {HarnessAdapter} from './config.js';
17
18
 
18
19
  /**
@@ -237,8 +238,46 @@ export class PiHarness implements Harness {
237
238
  cwd: input.dir,
238
239
  env: input.env ?? process.env,
239
240
  stdio: ['pipe', 'pipe', 'pipe'],
241
+ // PROCESS-GROUP LEADER (observation
242
+ // `checkpoint-releases-lock-while-predecessor-agent-still-writes`): pi's
243
+ // pgid becomes its own pid, so the deadline stop can signal the WHOLE
244
+ // agent tree with `kill(-pgid)` and VERIFY it is gone. Without this,
245
+ // `child.kill()` reached exactly one pid: subagents / MCP servers / tool
246
+ // subshells survived, were re-parented to init (so no ppid walk could even
247
+ // find them), and kept writing into the worktree while a SUCCESSOR agent
248
+ // was already editing it. A pgid is inherited by every descendant and is
249
+ // unaffected by re-parenting, which is why it is the only usable handle.
250
+ // We deliberately do NOT `unref()` here: the parent keeps supervising the
251
+ // child (and forwards its own termination to the group, below).
252
+ detached: true,
240
253
  });
241
254
  record.pid = child.pid; // the liveness anchor, recorded like spawnSync.
255
+ // The group id equals the leader's pid because we spawned `detached`.
256
+ const pgid = child.pid;
257
+ // `detached: true` takes pi OUT of our terminal's foreground process group,
258
+ // so a Ctrl-C / `kill` aimed at the runner would no longer reach it — which
259
+ // would WIDEN the very "aborting `do` does not kill the spawned agent tree"
260
+ // gap this change is closing. Forward our own termination to the group for
261
+ // as long as the child is live, so detaching strictly improves reachability
262
+ // instead of trading one orphan class for another.
263
+ const forwardSignal = (signal: NodeJS.Signals) => (): void => {
264
+ if (pgid === undefined) {
265
+ return;
266
+ }
267
+ try {
268
+ process.kill(-pgid, signal);
269
+ } catch {
270
+ // Already gone; nothing to forward to.
271
+ }
272
+ };
273
+ const onSigint = forwardSignal('SIGINT');
274
+ const onSigterm = forwardSignal('SIGTERM');
275
+ process.on('SIGINT', onSigint);
276
+ process.on('SIGTERM', onSigterm);
277
+ const stopForwarding = (): void => {
278
+ process.off('SIGINT', onSigint);
279
+ process.off('SIGTERM', onSigterm);
280
+ };
242
281
  let stderr = '';
243
282
  child.stderr?.on('data', (chunk: Buffer) => {
244
283
  stderr += chunk.toString('utf8');
@@ -276,18 +315,29 @@ export class PiHarness implements Harness {
276
315
  return;
277
316
  }
278
317
  timedOut = true;
318
+ // Signal the whole GROUP, not just pi: the descendants are exactly the
319
+ // processes that outlive it and keep writing to the worktree.
279
320
  try {
280
- child.kill('SIGTERM');
321
+ if (pgid !== undefined) {
322
+ process.kill(-pgid, 'SIGTERM');
323
+ } else {
324
+ child.kill('SIGTERM');
325
+ }
281
326
  } catch {
282
- // Best-effort: a already-exited child throws ESRCH; the `exit`
283
- // handler will still settle the promise.
327
+ // Best-effort: an already-exited group throws ESRCH; the `exit`
328
+ // handler will still settle the promise, and the post-exit reap below
329
+ // is what actually VERIFIES the tree is gone.
284
330
  }
285
331
  hardTimer = setTimeout(() => {
286
332
  if (settled) {
287
333
  return;
288
334
  }
289
335
  try {
290
- child.kill('SIGKILL');
336
+ if (pgid !== undefined) {
337
+ process.kill(-pgid, 'SIGKILL');
338
+ } else {
339
+ child.kill('SIGKILL');
340
+ }
291
341
  } catch {
292
342
  // Best-effort: see above.
293
343
  }
@@ -302,6 +352,7 @@ export class PiHarness implements Harness {
302
352
  }
303
353
  settled = true;
304
354
  clearDeadlineTimers();
355
+ stopForwarding();
305
356
  reject(new Error(`failed to spawn pi (${this.piBin}): ${err.message}`));
306
357
  });
307
358
  // Resolve on `exit` (pi itself terminated), NOT `close`: `close` waits for
@@ -314,6 +365,7 @@ export class PiHarness implements Harness {
314
365
  }
315
366
  settled = true;
316
367
  clearDeadlineTimers();
368
+ stopForwarding();
317
369
  // Release our end of the stdio pipes so a leaked grandchild's inherited
318
370
  // FDs stop keeping our streams referenced; `unref` the child handle too.
319
371
  child.stdout?.destroy();
@@ -321,17 +373,59 @@ export class PiHarness implements Harness {
321
373
  child.stdin?.destroy();
322
374
  child.unref?.();
323
375
  const status = code ?? -1;
324
- resolve({
325
- ok: status === 0 && !timedOut,
326
- record,
327
- detail:
328
- status === 0 && !timedOut ? undefined : stderr.trim() || undefined,
329
- timedOut: timedOut ? true : undefined,
330
- // Read the agent's ANSWER from the `.jsonl` at `exit` — the same
331
- // last-assistant-text read `launch` does at return (task
332
- // `harness-agent-output`); the process has exited so the log is final.
333
- output: readLastAssistantText(sessionFile),
334
- });
376
+ const settleWith = (reap?: LaunchResult['reap']): void => {
377
+ resolve({
378
+ ok: status === 0 && !timedOut,
379
+ record,
380
+ detail:
381
+ status === 0 && !timedOut
382
+ ? undefined
383
+ : stderr.trim() || undefined,
384
+ timedOut: timedOut ? true : undefined,
385
+ ...(reap ? {reap} : {}),
386
+ // Read the agent's ANSWER from the `.jsonl` at `exit` — the same
387
+ // last-assistant-text read `launch` does at return (task
388
+ // `harness-agent-output`); the process has exited so the log is final.
389
+ output: readLastAssistantText(sessionFile),
390
+ });
391
+ };
392
+ if (!timedOut || pgid === undefined) {
393
+ // Normal exit: we signalled nothing, so there is nothing to prove and
394
+ // nothing to kill. Byte-for-byte the pre-existing behaviour — in
395
+ // particular we do NOT reap a group the agent may have deliberately left
396
+ // running behind a successful run.
397
+ settleWith();
398
+ return;
399
+ }
400
+ // DEADLINE STOP: pi's own exit says NOTHING about its descendants — that
401
+ // assumption is the defect. Before this promise resolves (which is the
402
+ // runner's cue to save WIP, release the lock and dispatch a SUCCESSOR into
403
+ // this same worktree), reap the group and VERIFY it is gone. Bounded by
404
+ // construction, so this cannot reintroduce the resolve-on-`exit` hang the
405
+ // doc-comment above guards against: a tree that will not die resolves with
406
+ // `reaped: false` and the caller refuses to release the lock.
407
+ void reapProcessGroup({pgid})
408
+ .then((result) => {
409
+ settleWith({
410
+ reaped: result.reaped,
411
+ pgid,
412
+ escalatedToSigkill: result.escalatedToSigkill,
413
+ detail: result.detail,
414
+ });
415
+ })
416
+ .catch((err: unknown) => {
417
+ // A throw here means we could not even RUN the verification, which is
418
+ // indistinguishable from "might still be alive" — report it as an
419
+ // unproven reap rather than silently claiming success.
420
+ settleWith({
421
+ reaped: false,
422
+ pgid,
423
+ detail:
424
+ `could not verify that the agent process group ${pgid} exited ` +
425
+ `(${err instanceof Error ? err.message : String(err)}); treating ` +
426
+ 'the predecessor as possibly still writing to the worktree.',
427
+ });
428
+ });
335
429
  });
336
430
  // Feed the same prepared prompt on stdin, then close it (pi reads to EOF).
337
431
  if (input.prompt !== undefined) {
@@ -0,0 +1,221 @@
1
+ /**
2
+ * **Reap a stopped agent's whole PROCESS TREE, and VERIFY it is gone** (spec
3
+ * `graceful-pre-timeout-wip-checkpoint`, observation
4
+ * `checkpoint-releases-lock-while-predecessor-agent-still-writes`).
5
+ *
6
+ * ## The gap this closes
7
+ *
8
+ * The deadline checkpoint used to `child.kill('SIGTERM')` the agent process and
9
+ * treat the signal as if it were the outcome: the moment pi's own `exit` fired,
10
+ * the runner saved the WIP, RELEASED the item lock, and let the next tick
11
+ * dispatch a CONTINUATION agent into the SAME worktree. But `child.kill` signals
12
+ * exactly ONE pid, and a modern agent is a TREE (subagent processes, MCP servers,
13
+ * model proxies, tool subshells). Those descendants survive the parent's SIGTERM,
14
+ * and once pi exits they are re-parented to init — so they can no longer even be
15
+ * FOUND by walking `ppid`, while they keep writing into the worktree.
16
+ *
17
+ * Observed on a real run: the checkpoint saved WIP at 02:13, a continuation agent
18
+ * onboarded into the same worktree at ~02:15, and the predecessor's session log
19
+ * kept being written until 02:19:29 — four minutes INTO the successor's run,
20
+ * whose opening `git status` had already read the tree as clean. Nothing was lost
21
+ * only because the two happened to touch different files. The shape is the
22
+ * defect: one lock, one working tree, two live writers. A write landing after the
23
+ * successor's `git status` is invisible to it; a write landing during its edits
24
+ * can be clobbered either way; and the successor can commit the predecessor's
25
+ * half-finished edits as its own, under a message describing something else.
26
+ *
27
+ * ## Why the PROCESS GROUP is the handle
28
+ *
29
+ * A pid-tree walk cannot work here: the descendants we must reap are precisely
30
+ * the ones that OUTLIVE the parent, and an orphan's `ppid` is gone. A process
31
+ * GROUP id, by contrast, is inherited by every descendant and is NOT changed by
32
+ * re-parenting. So if the agent is spawned as a group LEADER (`detached: true`,
33
+ * making its pgid equal its pid), `kill(-pgid, …)` reaches the entire tree,
34
+ * orphans included — which is why {@link reapProcessGroup} takes a pgid and why
35
+ * `pi-harness.ts` spawns the deadline-capable async launch detached.
36
+ *
37
+ * ## Signal, then VERIFY — never assume
38
+ *
39
+ * The point of the whole module is that sending a signal is not evidence that
40
+ * anything died. So: SIGTERM the group, POLL until it is actually gone, escalate
41
+ * to SIGKILL after a grace, keep polling, and if it STILL will not die, say so
42
+ * LOUDLY and let the caller refuse to release the lock. A checkpoint that cannot
43
+ * prove the predecessor is dead must not hand the worktree to a successor.
44
+ */
45
+
46
+ /** Poll interval while waiting for a signalled group to actually exit. */
47
+ const REAP_POLL_MS = 50;
48
+
49
+ /**
50
+ * How long to wait after SIGTERM before escalating to SIGKILL. Matches
51
+ * `pi-harness.ts`'s `DEADLINE_SIGKILL_GRACE_MS` intent: enough for an agent to
52
+ * flush its session log and exit cleanly, short enough not to stall a CI leg.
53
+ */
54
+ export const REAP_SIGTERM_GRACE_MS = 10_000;
55
+
56
+ /** How long to keep waiting after SIGKILL before declaring the reap FAILED. */
57
+ export const REAP_SIGKILL_TIMEOUT_MS = 5_000;
58
+
59
+ /** The outcome of a {@link reapProcessGroup} attempt. */
60
+ export interface ReapResult {
61
+ /**
62
+ * True iff the group is VERIFIED gone (observed non-existent, not merely
63
+ * signalled). Only a `true` here licenses releasing the item lock and
64
+ * dispatching a successor into the same worktree.
65
+ */
66
+ reaped: boolean;
67
+ /** True iff SIGKILL was needed (the tree ignored SIGTERM) — worth reporting. */
68
+ escalatedToSigkill: boolean;
69
+ /** Total wall-clock ms spent waiting for the tree to die. */
70
+ waitedMs: number;
71
+ /** A human-readable account, always populated (the LOUD failure text). */
72
+ detail: string;
73
+ }
74
+
75
+ /** Sleep helper (injectable clock is not needed: callers inject `wait` in tests). */
76
+ function sleep(ms: number): Promise<void> {
77
+ return new Promise((resolve) => {
78
+ const timer = setTimeout(resolve, ms);
79
+ timer.unref?.();
80
+ });
81
+ }
82
+
83
+ /**
84
+ * Is any process still alive in process group `pgid`?
85
+ *
86
+ * `process.kill(-pgid, 0)` is the liveness probe: signal 0 performs the
87
+ * permission/existence check WITHOUT delivering a signal (the same technique
88
+ * `harness.ts`'s {@link pidAlive} uses for a single pid, widened to the group).
89
+ *
90
+ * - It THROWS `ESRCH` when no process in the group exists ⇒ the group is gone.
91
+ * - It THROWS `EPERM` when the group exists but we may not signal it. That is
92
+ * still "alive", and reporting it as dead would be the very assumption this
93
+ * module exists to remove — so `EPERM` reads as ALIVE.
94
+ * - It succeeds ⇒ alive.
95
+ */
96
+ export function processGroupAlive(pgid: number): boolean {
97
+ if (!Number.isInteger(pgid) || pgid <= 1) {
98
+ // pgid 0/1 (or a bogus value) would mean "our own group" / init — signalling
99
+ // those would be catastrophic, so never claim they are ours to reap.
100
+ return false;
101
+ }
102
+ try {
103
+ process.kill(-pgid, 0);
104
+ return true;
105
+ } catch (err) {
106
+ const code = (err as NodeJS.ErrnoException).code;
107
+ if (code === 'EPERM') {
108
+ return true; // exists, just not signallable by us.
109
+ }
110
+ return false; // ESRCH (or anything else): treat as gone.
111
+ }
112
+ }
113
+
114
+ /** Signal a whole process group, tolerating an already-dead group. */
115
+ function signalGroup(pgid: number, signal: NodeJS.Signals): void {
116
+ try {
117
+ process.kill(-pgid, signal);
118
+ } catch {
119
+ // ESRCH: already gone — the wait loop below observes that and succeeds.
120
+ }
121
+ }
122
+
123
+ /**
124
+ * SIGTERM process group `pgid`, wait for it to ACTUALLY exit, escalate to
125
+ * SIGKILL after {@link REAP_SIGTERM_GRACE_MS}, and report whether the tree is
126
+ * VERIFIED gone.
127
+ *
128
+ * Bounded by construction: at worst `sigtermGraceMs + sigkillTimeoutMs` before it
129
+ * returns `reaped: false` with a loud `detail`. It never waits indefinitely, so
130
+ * it cannot reintroduce the runner-hang the async launch's resolve-on-`exit`
131
+ * discipline exists to avoid.
132
+ */
133
+ export async function reapProcessGroup(params: {
134
+ /** The process GROUP id to reap (the group leader's pid). */
135
+ pgid: number;
136
+ sigtermGraceMs?: number;
137
+ sigkillTimeoutMs?: number;
138
+ /** Injectable sleep so tests need not burn real seconds. */
139
+ wait?: (ms: number) => Promise<void>;
140
+ /**
141
+ * Injectable liveness probe (default {@link processGroupAlive}). Exists so the
142
+ * REFUSAL path — a tree that survives SIGTERM *and* SIGKILL — can be tested
143
+ * deterministically. There is no portable way to create a genuinely unkillable
144
+ * process, and the alternative (a group we truly cannot signal) would risk the
145
+ * test process itself, so the probe is the seam.
146
+ */
147
+ alive?: (pgid: number) => boolean;
148
+ }): Promise<ReapResult> {
149
+ const {
150
+ pgid,
151
+ sigtermGraceMs = REAP_SIGTERM_GRACE_MS,
152
+ sigkillTimeoutMs = REAP_SIGKILL_TIMEOUT_MS,
153
+ wait = sleep,
154
+ alive = processGroupAlive,
155
+ } = params;
156
+ const started = Date.now();
157
+
158
+ if (!alive(pgid)) {
159
+ return {
160
+ reaped: true,
161
+ escalatedToSigkill: false,
162
+ waitedMs: 0,
163
+ detail: `agent process group ${pgid} was already gone (nothing to reap).`,
164
+ };
165
+ }
166
+
167
+ // 1. SOFT: ask the whole tree to stop, then WAIT for it to be observably gone.
168
+ signalGroup(pgid, 'SIGTERM');
169
+ while (Date.now() - started < sigtermGraceMs) {
170
+ if (!alive(pgid)) {
171
+ const waitedMs = Date.now() - started;
172
+ return {
173
+ reaped: true,
174
+ escalatedToSigkill: false,
175
+ waitedMs,
176
+ detail:
177
+ `agent process group ${pgid} exited on SIGTERM after ${waitedMs}ms ` +
178
+ '(verified gone).',
179
+ };
180
+ }
181
+ await wait(REAP_POLL_MS);
182
+ }
183
+
184
+ // 2. HARD: the tree ignored SIGTERM through the grace. SIGKILL it and keep
185
+ // verifying — a wedged agent still holding the worktree must not survive
186
+ // into the successor's run.
187
+ signalGroup(pgid, 'SIGKILL');
188
+ const killDeadline = Date.now() + sigkillTimeoutMs;
189
+ while (Date.now() < killDeadline) {
190
+ if (!alive(pgid)) {
191
+ const waitedMs = Date.now() - started;
192
+ return {
193
+ reaped: true,
194
+ escalatedToSigkill: true,
195
+ waitedMs,
196
+ detail:
197
+ `agent process group ${pgid} ignored SIGTERM and was SIGKILLed; ` +
198
+ `exited after ${waitedMs}ms (verified gone).`,
199
+ };
200
+ }
201
+ await wait(REAP_POLL_MS);
202
+ }
203
+
204
+ // 3. LOUD FAILURE: we cannot prove the predecessor is dead. Say exactly that,
205
+ // and exactly what the caller must not do as a result.
206
+ const waitedMs = Date.now() - started;
207
+ return {
208
+ reaped: false,
209
+ escalatedToSigkill: true,
210
+ waitedMs,
211
+ detail:
212
+ `REFUSING TO PROCEED: agent process group ${pgid} is STILL ALIVE ${waitedMs}ms ` +
213
+ 'after SIGTERM and SIGKILL (an unkillable/uninterruptible descendant — e.g. a ' +
214
+ 'process wedged in a kernel call, or one we lack permission to signal). It may ' +
215
+ 'still be WRITING to the worktree, so the item lock must NOT be released and no ' +
216
+ 'successor agent may onboard here: two live writers in one working tree can ' +
217
+ 'silently clobber each other and let a successor commit the predecessor’s ' +
218
+ `half-finished edits. Inspect and kill it by hand (\`ps -g ${pgid}\`, ` +
219
+ `\`kill -9 -${pgid}\`) before re-running this item.`,
220
+ };
221
+ }
@@ -0,0 +1,217 @@
1
+ import {
2
+ existsSync,
3
+ mkdirSync,
4
+ readFileSync,
5
+ rmSync,
6
+ writeFileSync,
7
+ } from 'node:fs';
8
+ import {dirname, join} from 'node:path';
9
+ import {run} from './git.js';
10
+ import {pidAlive} from './harness.js';
11
+ import {processGroupAlive} from './reap-agent-tree.js';
12
+
13
+ /**
14
+ * **The per-WORKING-TREE writer sentinel** (observation
15
+ * `checkpoint-releases-lock-while-predecessor-agent-still-writes`).
16
+ *
17
+ * The per-item lock (`item-lock.ts`) guards the ITEM: it answers "who owns this
18
+ * task?" and it is what claim / requeue / bounce move around. Nothing guarded the
19
+ * WORKING TREE. Those are different resources, and the deadline checkpoint is
20
+ * exactly where they come apart: the checkpoint releases the item lock so the
21
+ * next tick can continue the task, but the tree the previous agent was editing is
22
+ * reused by the successor. If the predecessor is still alive, two agents write to
23
+ * one tree.
24
+ *
25
+ * The primary fix is to reap the predecessor and VERIFY it is gone before the
26
+ * lock moves (`reap-agent-tree.ts`). This sentinel is the INDEPENDENT backstop:
27
+ * even if a live writer survives by some route the reap did not cover (a
28
+ * deliberately `setsid`-ed grandchild, a stale run from a crashed runner, an
29
+ * operator manually re-driving an item), a second agent physically cannot onboard
30
+ * into a tree that already has a LIVE holder. It is deliberately keyed on the
31
+ * TREE, not on the item: two different items sharing one worktree is just as
32
+ * unsafe as two attempts at the same item.
33
+ *
34
+ * ## Where the sentinel lives, and why not in the tree
35
+ *
36
+ * It is written to the worktree's PRIVATE git directory (`git rev-parse
37
+ * --absolute-git-dir`, which for a linked worktree is
38
+ * `.../.git/worktrees/<name>/`), NOT to a file inside the working tree. That
39
+ * placement is load-bearing:
40
+ *
41
+ * - it is per-worktree (linked worktrees each get their own git dir), which is
42
+ * precisely the granularity we are guarding;
43
+ * - it can never appear in `git status`, so it cannot be mistaken for agent work,
44
+ * cannot be swept into a commit by a `git add -A`, and needs no new exclusion
45
+ * in the empty-diff backstop / `gc`'s cleanliness predicate (unlike
46
+ * `.dorfl-job.json`, which each of those has to filter out by name);
47
+ * - it is removed with the worktree, so it cannot outlive what it guards.
48
+ *
49
+ * ## Liveness, not presence
50
+ *
51
+ * A pid file that only records presence becomes a permanent blocker the first
52
+ * time a runner is `kill -9`ed. So the holder is checked for LIVENESS (its
53
+ * process group first, falling back to its pid) and a dead holder's sentinel is
54
+ * treated as stale and taken over. Only a genuinely live foreign writer refuses.
55
+ */
56
+
57
+ /** The sentinel filename inside the worktree's private git directory. */
58
+ export const WRITER_SENTINEL_FILENAME = 'dorfl-writer.json';
59
+
60
+ /** The recorded holder of a worktree's writer sentinel. */
61
+ export interface WorktreeWriter {
62
+ /** The runner process that owns the agent writing in this tree. */
63
+ pid: number;
64
+ /** The agent's process GROUP, when the harness spawned a killable one. */
65
+ pgid?: number;
66
+ /** The item being built in this tree (diagnostics: names the other writer). */
67
+ slug: string;
68
+ /** ISO timestamp of acquisition (diagnostics: how long it has been held). */
69
+ startedAt: string;
70
+ }
71
+
72
+ /** The outcome of trying to become a worktree's sole writer. */
73
+ export type WorktreeWriterLock =
74
+ | {
75
+ acquired: true;
76
+ /** Release the sentinel. Idempotent, and safe if it was already stolen. */
77
+ release(): void;
78
+ }
79
+ | {
80
+ acquired: false;
81
+ /** The LIVE holder that refused us (when it could be parsed). */
82
+ holder?: WorktreeWriter;
83
+ /** Human-readable refusal, naming the other writer. */
84
+ reason: string;
85
+ };
86
+
87
+ /**
88
+ * The worktree's PRIVATE git directory, or `undefined` when `dir` is not a git
89
+ * worktree (in which case there is no sentinel location and the caller proceeds
90
+ * unguarded rather than failing — this is a backstop, not a gate).
91
+ */
92
+ function worktreeGitDir(
93
+ dir: string,
94
+ env: NodeJS.ProcessEnv | undefined,
95
+ ): string | undefined {
96
+ const result = run('git', ['rev-parse', '--absolute-git-dir'], dir, {env});
97
+ if (result.status !== 0) {
98
+ return undefined;
99
+ }
100
+ const path = result.stdout.trim();
101
+ return path === '' ? undefined : path;
102
+ }
103
+
104
+ /** The sentinel path for `dir`, or `undefined` when `dir` is not a worktree. */
105
+ export function writerSentinelPath(
106
+ dir: string,
107
+ env?: NodeJS.ProcessEnv,
108
+ ): string | undefined {
109
+ const gitDir = worktreeGitDir(dir, env);
110
+ return gitDir === undefined
111
+ ? undefined
112
+ : join(gitDir, WRITER_SENTINEL_FILENAME);
113
+ }
114
+
115
+ /** Read + parse the sentinel, or `undefined` when absent/corrupt. */
116
+ export function readWorktreeWriter(
117
+ dir: string,
118
+ env?: NodeJS.ProcessEnv,
119
+ ): WorktreeWriter | undefined {
120
+ const path = writerSentinelPath(dir, env);
121
+ if (path === undefined || !existsSync(path)) {
122
+ return undefined;
123
+ }
124
+ try {
125
+ const parsed = JSON.parse(readFileSync(path, 'utf8')) as WorktreeWriter;
126
+ return typeof parsed?.pid === 'number' ? parsed : undefined;
127
+ } catch {
128
+ // A corrupt sentinel records nothing we can trust; treat it as absent so it
129
+ // self-heals on the next acquire rather than wedging the worktree forever.
130
+ return undefined;
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Is the recorded holder still running? Prefers the agent's process GROUP (which
136
+ * survives the group leader's death and so catches exactly the orphaned-writer
137
+ * case this exists for), and falls back to the runner pid.
138
+ */
139
+ export function writerAlive(holder: WorktreeWriter): boolean {
140
+ if (holder.pgid !== undefined && processGroupAlive(holder.pgid)) {
141
+ return true;
142
+ }
143
+ return pidAlive(holder.pid);
144
+ }
145
+
146
+ /**
147
+ * Claim `dir` as the SOLE agent-writable working tree for `slug`.
148
+ *
149
+ * Refuses when a DIFFERENT, still-LIVE writer holds it — the second-agent case
150
+ * the observation describes. A dead holder's sentinel is stale and is taken over
151
+ * silently (a `kill -9`ed runner must not poison the worktree forever), and our
152
+ * OWN pid re-acquiring is a no-op re-entry rather than a refusal.
153
+ *
154
+ * When `dir` is not a git worktree there is nowhere private to record the
155
+ * sentinel; that is reported as acquired with a no-op release, because this is a
156
+ * defence-in-depth backstop and must never become a new way for a legitimate run
157
+ * to fail.
158
+ */
159
+ export function acquireWorktreeWriterLock(params: {
160
+ dir: string;
161
+ slug: string;
162
+ /** The agent's process group, when known (the strongest liveness anchor). */
163
+ pgid?: number;
164
+ env?: NodeJS.ProcessEnv;
165
+ }): WorktreeWriterLock {
166
+ const {dir, slug, pgid, env} = params;
167
+ const path = writerSentinelPath(dir, env);
168
+ if (path === undefined) {
169
+ return {acquired: true, release: () => {}};
170
+ }
171
+
172
+ const existing = readWorktreeWriter(dir, env);
173
+ if (
174
+ existing !== undefined &&
175
+ existing.pid !== process.pid &&
176
+ writerAlive(existing)
177
+ ) {
178
+ return {
179
+ acquired: false,
180
+ holder: existing,
181
+ reason:
182
+ `worktree ${dir} already has a LIVE agent writer: pid ${existing.pid}` +
183
+ (existing.pgid !== undefined ? ` (group ${existing.pgid})` : '') +
184
+ ` building '${existing.slug}' since ${existing.startedAt}. Refusing to ` +
185
+ `onboard '${slug}' into the same working tree: two live agents in one ` +
186
+ 'tree can clobber each other’s edits, and the second can commit the ' +
187
+ 'first’s half-finished work under a message describing something else. ' +
188
+ 'Wait for it to exit, or kill it, then retry.',
189
+ };
190
+ }
191
+
192
+ const record: WorktreeWriter = {
193
+ pid: process.pid,
194
+ ...(pgid !== undefined ? {pgid} : {}),
195
+ slug,
196
+ startedAt: new Date().toISOString(),
197
+ };
198
+ mkdirSync(dirname(path), {recursive: true});
199
+ writeFileSync(path, `${JSON.stringify(record, null, 2)}\n`, 'utf8');
200
+
201
+ let released = false;
202
+ return {
203
+ acquired: true,
204
+ release: (): void => {
205
+ if (released) {
206
+ return;
207
+ }
208
+ released = true;
209
+ // Only remove a sentinel that is still OURS: if it was stolen as stale by
210
+ // another runner, deleting it would silently un-guard that runner's tree.
211
+ const current = readWorktreeWriter(dir, env);
212
+ if (current === undefined || current.pid === process.pid) {
213
+ rmSync(path, {force: true});
214
+ }
215
+ },
216
+ };
217
+ }