gsd-pi 2.37.0 → 2.37.1-dev.49503be

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.
Files changed (93) hide show
  1. package/README.md +21 -20
  2. package/dist/onboarding.js +1 -0
  3. package/dist/resources/extensions/cmux/package.json +7 -0
  4. package/dist/resources/extensions/gsd/auto-dispatch.js +54 -1
  5. package/dist/resources/extensions/gsd/auto-loop.js +18 -4
  6. package/dist/resources/extensions/gsd/auto-post-unit.js +14 -0
  7. package/dist/resources/extensions/gsd/auto-prompts.js +55 -0
  8. package/dist/resources/extensions/gsd/auto-recovery.js +19 -1
  9. package/dist/resources/extensions/gsd/auto.js +42 -5
  10. package/dist/resources/extensions/gsd/commands.js +80 -33
  11. package/dist/resources/extensions/gsd/files.js +41 -0
  12. package/dist/resources/extensions/gsd/git-service.js +9 -1
  13. package/dist/resources/extensions/gsd/history.js +2 -1
  14. package/dist/resources/extensions/gsd/metrics.js +4 -2
  15. package/dist/resources/extensions/gsd/preferences-types.js +2 -1
  16. package/dist/resources/extensions/gsd/preferences-validation.js +42 -0
  17. package/dist/resources/extensions/gsd/prompts/reactive-execute.md +41 -0
  18. package/dist/resources/extensions/gsd/reactive-graph.js +227 -0
  19. package/dist/resources/extensions/gsd/session-lock.js +26 -6
  20. package/dist/resources/extensions/shared/format-utils.js +5 -41
  21. package/dist/resources/extensions/shared/layout-utils.js +46 -0
  22. package/dist/resources/extensions/shared/mod.js +2 -1
  23. package/package.json +2 -1
  24. package/packages/pi-ai/dist/env-api-keys.js +13 -0
  25. package/packages/pi-ai/dist/env-api-keys.js.map +1 -1
  26. package/packages/pi-ai/dist/models.generated.d.ts +172 -0
  27. package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
  28. package/packages/pi-ai/dist/models.generated.js +172 -0
  29. package/packages/pi-ai/dist/models.generated.js.map +1 -1
  30. package/packages/pi-ai/dist/providers/anthropic-shared.d.ts +64 -0
  31. package/packages/pi-ai/dist/providers/anthropic-shared.d.ts.map +1 -0
  32. package/packages/pi-ai/dist/providers/anthropic-shared.js +668 -0
  33. package/packages/pi-ai/dist/providers/anthropic-shared.js.map +1 -0
  34. package/packages/pi-ai/dist/providers/anthropic-vertex.d.ts +5 -0
  35. package/packages/pi-ai/dist/providers/anthropic-vertex.d.ts.map +1 -0
  36. package/packages/pi-ai/dist/providers/anthropic-vertex.js +85 -0
  37. package/packages/pi-ai/dist/providers/anthropic-vertex.js.map +1 -0
  38. package/packages/pi-ai/dist/providers/anthropic.d.ts +4 -30
  39. package/packages/pi-ai/dist/providers/anthropic.d.ts.map +1 -1
  40. package/packages/pi-ai/dist/providers/anthropic.js +47 -764
  41. package/packages/pi-ai/dist/providers/anthropic.js.map +1 -1
  42. package/packages/pi-ai/dist/providers/register-builtins.d.ts.map +1 -1
  43. package/packages/pi-ai/dist/providers/register-builtins.js +6 -0
  44. package/packages/pi-ai/dist/providers/register-builtins.js.map +1 -1
  45. package/packages/pi-ai/dist/types.d.ts +2 -2
  46. package/packages/pi-ai/dist/types.d.ts.map +1 -1
  47. package/packages/pi-ai/dist/types.js.map +1 -1
  48. package/packages/pi-ai/package.json +1 -0
  49. package/packages/pi-ai/src/env-api-keys.ts +14 -0
  50. package/packages/pi-ai/src/models.generated.ts +172 -0
  51. package/packages/pi-ai/src/providers/anthropic-shared.ts +761 -0
  52. package/packages/pi-ai/src/providers/anthropic-vertex.ts +130 -0
  53. package/packages/pi-ai/src/providers/anthropic.ts +76 -868
  54. package/packages/pi-ai/src/providers/register-builtins.ts +7 -0
  55. package/packages/pi-ai/src/types.ts +2 -0
  56. package/packages/pi-coding-agent/dist/core/extensions/loader.d.ts.map +1 -1
  57. package/packages/pi-coding-agent/dist/core/extensions/loader.js +8 -4
  58. package/packages/pi-coding-agent/dist/core/extensions/loader.js.map +1 -1
  59. package/packages/pi-coding-agent/dist/core/model-resolver.d.ts.map +1 -1
  60. package/packages/pi-coding-agent/dist/core/model-resolver.js +1 -0
  61. package/packages/pi-coding-agent/dist/core/model-resolver.js.map +1 -1
  62. package/packages/pi-coding-agent/package.json +1 -1
  63. package/packages/pi-coding-agent/src/core/extensions/loader.ts +8 -4
  64. package/packages/pi-coding-agent/src/core/model-resolver.ts +1 -0
  65. package/pkg/package.json +1 -1
  66. package/src/resources/extensions/cmux/package.json +7 -0
  67. package/src/resources/extensions/gsd/auto-dispatch.ts +78 -0
  68. package/src/resources/extensions/gsd/auto-loop.ts +24 -6
  69. package/src/resources/extensions/gsd/auto-post-unit.ts +14 -0
  70. package/src/resources/extensions/gsd/auto-prompts.ts +68 -0
  71. package/src/resources/extensions/gsd/auto-recovery.ts +18 -0
  72. package/src/resources/extensions/gsd/auto.ts +56 -5
  73. package/src/resources/extensions/gsd/commands.ts +85 -31
  74. package/src/resources/extensions/gsd/files.ts +45 -0
  75. package/src/resources/extensions/gsd/git-service.ts +12 -1
  76. package/src/resources/extensions/gsd/history.ts +2 -1
  77. package/src/resources/extensions/gsd/metrics.ts +4 -2
  78. package/src/resources/extensions/gsd/preferences-types.ts +5 -1
  79. package/src/resources/extensions/gsd/preferences-validation.ts +41 -0
  80. package/src/resources/extensions/gsd/prompts/reactive-execute.md +41 -0
  81. package/src/resources/extensions/gsd/reactive-graph.ts +289 -0
  82. package/src/resources/extensions/gsd/session-lock.ts +41 -6
  83. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +37 -1
  84. package/src/resources/extensions/gsd/tests/auto-worktree.test.ts +19 -0
  85. package/src/resources/extensions/gsd/tests/cmux.test.ts +25 -1
  86. package/src/resources/extensions/gsd/tests/reactive-executor.test.ts +367 -0
  87. package/src/resources/extensions/gsd/tests/reactive-graph.test.ts +299 -0
  88. package/src/resources/extensions/gsd/tests/session-lock-regression.test.ts +45 -0
  89. package/src/resources/extensions/gsd/types.ts +41 -0
  90. package/src/resources/extensions/shared/format-utils.ts +5 -44
  91. package/src/resources/extensions/shared/layout-utils.ts +49 -0
  92. package/src/resources/extensions/shared/mod.ts +7 -4
  93. package/src/resources/extensions/shared/tests/format-utils.test.ts +5 -3
@@ -0,0 +1,41 @@
1
+ # Reactive Task Execution — Parallel Dispatch
2
+
3
+ **Working directory:** `{{workingDirectory}}`
4
+ **Milestone:** {{milestoneId}} — {{milestoneTitle}}
5
+ **Slice:** {{sliceId}} — {{sliceTitle}}
6
+
7
+ ## Mission
8
+
9
+ You are executing **multiple tasks in parallel** for this slice. The task graph below shows which tasks are ready for simultaneous execution based on their input/output dependencies.
10
+
11
+ **Critical rule:** Use the `subagent` tool in **parallel mode** to dispatch all ready tasks simultaneously. Each subagent gets a self-contained execute-task prompt. After all subagents return, verify each task's outputs and write summaries.
12
+
13
+ ## Task Dependency Graph
14
+
15
+ {{graphContext}}
16
+
17
+ ## Ready Tasks for Parallel Dispatch
18
+
19
+ {{readyTaskCount}} tasks are ready for parallel execution:
20
+
21
+ {{readyTaskList}}
22
+
23
+ ## Execution Protocol
24
+
25
+ 1. **Dispatch all ready tasks** using `subagent` in parallel mode. Each subagent prompt is provided below.
26
+ 2. **Wait for all subagents** to complete.
27
+ 3. **Verify each task's outputs** — check that expected files were created/modified and that verification commands pass.
28
+ 4. **Write task summaries** for each completed task using the task-summary template.
29
+ 5. **Mark completed tasks** as done in the slice plan (checkbox `[x]`).
30
+ 6. **Commit** all changes with a clear message covering the parallel batch.
31
+
32
+ If any subagent fails:
33
+ - Write a summary for the failed task with `blocker_discovered: true`
34
+ - Continue marking the successful tasks as done
35
+ - The orchestrator will handle re-dispatch on the next iteration
36
+
37
+ ## Subagent Prompts
38
+
39
+ {{subagentPrompts}}
40
+
41
+ {{inlinedTemplates}}
@@ -0,0 +1,289 @@
1
+ /**
2
+ * Reactive Task Graph — derives dependency edges from task plan IO signatures.
3
+ *
4
+ * Pure functions that build a DAG from task IO intersections and resolve
5
+ * which tasks are currently ready for parallel dispatch. Used by the
6
+ * reactive-execute dispatch path (ADR-004).
7
+ *
8
+ * Graph derivation and resolution functions are pure (no filesystem access).
9
+ * The `loadSliceTaskIO` loader at the bottom is the only async/IO function.
10
+ */
11
+
12
+ import type { TaskIO, DerivedTaskNode, ReactiveExecutionState } from "./types.js";
13
+ import { loadFile, parsePlan, parseTaskPlanIO } from "./files.js";
14
+ import { resolveTasksDir, resolveTaskFiles } from "./paths.js";
15
+ import { join } from "node:path";
16
+ import { loadJsonFileOrNull, saveJsonFile } from "./json-persistence.js";
17
+ import { existsSync, unlinkSync } from "node:fs";
18
+
19
+ // ─── Graph Construction ───────────────────────────────────────────────────
20
+
21
+ /**
22
+ * Build a dependency graph from task IO signatures.
23
+ *
24
+ * A task T_b depends on T_a when any of T_b's inputFiles appear in T_a's
25
+ * outputFiles. Self-references are excluded.
26
+ *
27
+ * Tasks are returned in the same order as the input array.
28
+ */
29
+ export function deriveTaskGraph(tasks: TaskIO[]): DerivedTaskNode[] {
30
+ // Build output → producer lookup
31
+ const outputToProducer = new Map<string, string[]>();
32
+ for (const task of tasks) {
33
+ for (const outFile of task.outputFiles) {
34
+ const existing = outputToProducer.get(outFile);
35
+ if (existing) {
36
+ existing.push(task.id);
37
+ } else {
38
+ outputToProducer.set(outFile, [task.id]);
39
+ }
40
+ }
41
+ }
42
+
43
+ return tasks.map((task) => {
44
+ const deps = new Set<string>();
45
+ for (const inFile of task.inputFiles) {
46
+ const producers = outputToProducer.get(inFile);
47
+ if (producers) {
48
+ for (const pid of producers) {
49
+ if (pid !== task.id) deps.add(pid);
50
+ }
51
+ }
52
+ }
53
+ return {
54
+ ...task,
55
+ dependsOn: [...deps].sort(),
56
+ };
57
+ });
58
+ }
59
+
60
+ // ─── Ready Set Resolution ─────────────────────────────────────────────────
61
+
62
+ /**
63
+ * Return task IDs whose dependencies are all in `completed`.
64
+ * Excludes tasks that are already done or in-flight.
65
+ */
66
+ export function getReadyTasks(
67
+ graph: DerivedTaskNode[],
68
+ completed: Set<string>,
69
+ inFlight: Set<string>,
70
+ ): string[] {
71
+ return graph
72
+ .filter((node) => {
73
+ if (node.done || completed.has(node.id) || inFlight.has(node.id)) return false;
74
+ return node.dependsOn.every((dep) => completed.has(dep));
75
+ })
76
+ .map((node) => node.id);
77
+ }
78
+
79
+ // ─── Conflict-Free Subset Selection ──────────────────────────────────────
80
+
81
+ /**
82
+ * Greedy selection of non-conflicting tasks up to `maxParallel`.
83
+ *
84
+ * Two tasks conflict if they share any outputFile. We also exclude tasks
85
+ * whose outputs overlap with `inFlightOutputs` (files being written by
86
+ * tasks currently in progress).
87
+ */
88
+ export function chooseNonConflictingSubset(
89
+ readyIds: string[],
90
+ graph: DerivedTaskNode[],
91
+ maxParallel: number,
92
+ inFlightOutputs: Set<string>,
93
+ ): string[] {
94
+ const nodeMap = new Map(graph.map((n) => [n.id, n]));
95
+ const claimed = new Set(inFlightOutputs);
96
+ const selected: string[] = [];
97
+
98
+ for (const id of readyIds) {
99
+ if (selected.length >= maxParallel) break;
100
+ const node = nodeMap.get(id);
101
+ if (!node) continue;
102
+
103
+ // Check for output overlap with already-selected or in-flight
104
+ const conflicts = node.outputFiles.some((f) => claimed.has(f));
105
+ if (conflicts) continue;
106
+
107
+ // Claim this task's outputs
108
+ for (const f of node.outputFiles) claimed.add(f);
109
+ selected.push(id);
110
+ }
111
+
112
+ return selected;
113
+ }
114
+
115
+ // ─── Graph Quality Checks ─────────────────────────────────────────────────
116
+
117
+ /**
118
+ * Returns true if any incomplete task has 0 inputFiles AND 0 outputFiles.
119
+ *
120
+ * An ambiguous graph means IO annotations are too sparse to derive reliable
121
+ * edges — the dispatcher should fall back to sequential execution.
122
+ */
123
+ export function isGraphAmbiguous(graph: DerivedTaskNode[]): boolean {
124
+ return graph.some(
125
+ (node) =>
126
+ !node.done &&
127
+ node.inputFiles.length === 0 &&
128
+ node.outputFiles.length === 0,
129
+ );
130
+ }
131
+
132
+ /**
133
+ * Detect deadlock: no tasks are ready and none are in-flight, yet incomplete
134
+ * tasks remain. This indicates a circular dependency or impossible state.
135
+ */
136
+ export function detectDeadlock(
137
+ graph: DerivedTaskNode[],
138
+ completed: Set<string>,
139
+ inFlight: Set<string>,
140
+ ): boolean {
141
+ const incomplete = graph.filter(
142
+ (n) => !n.done && !completed.has(n.id) && !inFlight.has(n.id),
143
+ );
144
+ if (incomplete.length === 0) return false; // all done
145
+ if (inFlight.size > 0) return false; // something is running, wait for it
146
+
147
+ // Nothing in flight, but incomplete tasks remain — check if any are ready
148
+ const ready = getReadyTasks(graph, completed, inFlight);
149
+ return ready.length === 0;
150
+ }
151
+
152
+ // ─── Graph Metrics ────────────────────────────────────────────────────────
153
+
154
+ /** Compute summary metrics for logging. */
155
+ export function graphMetrics(graph: DerivedTaskNode[]): {
156
+ taskCount: number;
157
+ edgeCount: number;
158
+ readySetSize: number;
159
+ ambiguous: boolean;
160
+ } {
161
+ const completed = new Set(graph.filter((n) => n.done).map((n) => n.id));
162
+ const ready = getReadyTasks(graph, completed, new Set());
163
+ const edgeCount = graph.reduce((sum, n) => sum + n.dependsOn.length, 0);
164
+
165
+ return {
166
+ taskCount: graph.length,
167
+ edgeCount,
168
+ readySetSize: ready.length,
169
+ ambiguous: isGraphAmbiguous(graph),
170
+ };
171
+ }
172
+
173
+ // ─── IO Loader (async, filesystem) ────────────────────────────────────────
174
+
175
+ /**
176
+ * Load TaskIO for all tasks in a slice by reading the slice plan (for done
177
+ * status and task IDs) and individual task plan files (for IO sections).
178
+ *
179
+ * Returns [] when the slice plan or tasks directory doesn't exist.
180
+ */
181
+ export async function loadSliceTaskIO(
182
+ basePath: string,
183
+ mid: string,
184
+ sid: string,
185
+ ): Promise<TaskIO[]> {
186
+ const { resolveSliceFile } = await import("./paths.js");
187
+ const slicePlanPath = resolveSliceFile(basePath, mid, sid, "PLAN");
188
+ const planContent = slicePlanPath ? await loadFile(slicePlanPath) : null;
189
+ if (!planContent) return [];
190
+
191
+ const plan = parsePlan(planContent);
192
+ const tDir = resolveTasksDir(basePath, mid, sid);
193
+ if (!tDir) return [];
194
+
195
+ const results: TaskIO[] = [];
196
+
197
+ for (const taskEntry of plan.tasks) {
198
+ const planFiles = resolveTaskFiles(tDir, "PLAN");
199
+ const taskFileName = planFiles.find((f) =>
200
+ f.toUpperCase().startsWith(taskEntry.id.toUpperCase() + "-"),
201
+ );
202
+ if (!taskFileName) {
203
+ // Task plan file missing — include with empty IO (will trigger ambiguous)
204
+ results.push({
205
+ id: taskEntry.id,
206
+ title: taskEntry.title,
207
+ inputFiles: [],
208
+ outputFiles: [],
209
+ done: taskEntry.done,
210
+ });
211
+ continue;
212
+ }
213
+
214
+ const taskContent = await loadFile(join(tDir, taskFileName));
215
+ if (!taskContent) {
216
+ results.push({
217
+ id: taskEntry.id,
218
+ title: taskEntry.title,
219
+ inputFiles: [],
220
+ outputFiles: [],
221
+ done: taskEntry.done,
222
+ });
223
+ continue;
224
+ }
225
+
226
+ const io = parseTaskPlanIO(taskContent);
227
+ results.push({
228
+ id: taskEntry.id,
229
+ title: taskEntry.title,
230
+ inputFiles: io.inputFiles,
231
+ outputFiles: io.outputFiles,
232
+ done: taskEntry.done,
233
+ });
234
+ }
235
+
236
+ return results;
237
+ }
238
+
239
+ // ─── State Persistence ────────────────────────────────────────────────────
240
+
241
+ function reactiveStatePath(basePath: string, mid: string, sid: string): string {
242
+ return join(basePath, ".gsd", "runtime", `${mid}-${sid}-reactive.json`);
243
+ }
244
+
245
+ function isReactiveState(data: unknown): data is ReactiveExecutionState {
246
+ if (!data || typeof data !== "object") return false;
247
+ const d = data as Record<string, unknown>;
248
+ return typeof d.sliceId === "string" && Array.isArray(d.completed);
249
+ }
250
+
251
+ /**
252
+ * Load persisted reactive execution state for a slice.
253
+ * Returns null when no state file exists or the file is invalid.
254
+ */
255
+ export function loadReactiveState(
256
+ basePath: string,
257
+ mid: string,
258
+ sid: string,
259
+ ): ReactiveExecutionState | null {
260
+ return loadJsonFileOrNull(reactiveStatePath(basePath, mid, sid), isReactiveState);
261
+ }
262
+
263
+ /**
264
+ * Save reactive execution state to disk.
265
+ */
266
+ export function saveReactiveState(
267
+ basePath: string,
268
+ mid: string,
269
+ sid: string,
270
+ state: ReactiveExecutionState,
271
+ ): void {
272
+ saveJsonFile(reactiveStatePath(basePath, mid, sid), state);
273
+ }
274
+
275
+ /**
276
+ * Remove the reactive state file when a slice completes.
277
+ */
278
+ export function clearReactiveState(
279
+ basePath: string,
280
+ mid: string,
281
+ sid: string,
282
+ ): void {
283
+ const path = reactiveStatePath(basePath, mid, sid);
284
+ try {
285
+ if (existsSync(path)) unlinkSync(path);
286
+ } catch {
287
+ // Non-fatal
288
+ }
289
+ }
@@ -40,6 +40,19 @@ export type SessionLockResult =
40
40
  | { acquired: true }
41
41
  | { acquired: false; reason: string; existingPid?: number };
42
42
 
43
+ export type SessionLockFailureReason =
44
+ | "compromised"
45
+ | "missing-metadata"
46
+ | "pid-mismatch";
47
+
48
+ export interface SessionLockStatus {
49
+ valid: boolean;
50
+ failureReason?: SessionLockFailureReason;
51
+ existingPid?: number;
52
+ expectedPid?: number;
53
+ recovered?: boolean;
54
+ }
55
+
43
56
  // ─── Module State ───────────────────────────────────────────────────────────
44
57
 
45
58
  /** Release function from proper-lockfile — calling it releases the OS lock. */
@@ -368,7 +381,7 @@ export function updateSessionLock(
368
381
  *
369
382
  * This is called periodically during the dispatch loop.
370
383
  */
371
- export function validateSessionLock(basePath: string): boolean {
384
+ export function getSessionLockStatus(basePath: string): SessionLockStatus {
372
385
  // Lock was compromised by proper-lockfile (mtime drift from sleep, stall, etc.)
373
386
  if (_lockCompromised) {
374
387
  // Recovery gate (#1512): Before declaring the lock lost, check if the lock
@@ -385,18 +398,23 @@ export function validateSessionLock(basePath: string): boolean {
385
398
  process.stderr.write(
386
399
  `[gsd] Lock recovered after onCompromised — lock file PID matched, re-acquired.\n`,
387
400
  );
388
- return true;
401
+ return { valid: true, recovered: true };
389
402
  }
390
403
  } catch {
391
404
  // Re-acquisition failed — fall through to return false
392
405
  }
393
406
  }
394
- return false;
407
+ return {
408
+ valid: false,
409
+ failureReason: "compromised",
410
+ existingPid: existing?.pid,
411
+ expectedPid: process.pid,
412
+ };
395
413
  }
396
414
 
397
415
  // If we have an OS-level lock, we're still the owner
398
416
  if (_releaseFunction && _lockedPath === basePath) {
399
- return true;
417
+ return { valid: true };
400
418
  }
401
419
 
402
420
  // Fallback: check the lock file PID
@@ -404,10 +422,27 @@ export function validateSessionLock(basePath: string): boolean {
404
422
  const existing = readExistingLockData(lp);
405
423
  if (!existing) {
406
424
  // Lock file was deleted — we lost ownership
407
- return false;
425
+ return {
426
+ valid: false,
427
+ failureReason: "missing-metadata",
428
+ expectedPid: process.pid,
429
+ };
430
+ }
431
+
432
+ if (existing.pid !== process.pid) {
433
+ return {
434
+ valid: false,
435
+ failureReason: "pid-mismatch",
436
+ existingPid: existing.pid,
437
+ expectedPid: process.pid,
438
+ };
408
439
  }
409
440
 
410
- return existing.pid === process.pid;
441
+ return { valid: true };
442
+ }
443
+
444
+ export function validateSessionLock(basePath: string): boolean {
445
+ return getSessionLockStatus(basePath).valid;
411
446
  }
412
447
 
413
448
  /**
@@ -14,6 +14,7 @@ import {
14
14
  type AgentEndEvent,
15
15
  type LoopDeps,
16
16
  } from "../auto-loop.js";
17
+ import type { SessionLockStatus } from "../session-lock.js";
17
18
 
18
19
  // ─── Helpers ─────────────────────────────────────────────────────────────────
19
20
 
@@ -341,7 +342,7 @@ function makeMockDeps(
341
342
  preDispatchHealthGate: async () => ({ proceed: true, fixesApplied: [] }),
342
343
  syncProjectRootToWorktree: () => {},
343
344
  checkResourcesStale: () => null,
344
- validateSessionLock: () => true,
345
+ validateSessionLock: () => ({ valid: true } as SessionLockStatus),
345
346
  updateSessionLock: () => {
346
347
  callLog.push("updateSessionLock");
347
348
  },
@@ -532,6 +533,41 @@ test("autoLoop exits on terminal complete state", async (t) => {
532
533
  );
533
534
  });
534
535
 
536
+ test("autoLoop passes structured session-lock failure details to the handler", async () => {
537
+ _resetPendingResolve();
538
+
539
+ const ctx = makeMockCtx();
540
+ ctx.ui.setStatus = () => {};
541
+ const pi = makeMockPi();
542
+ const s = makeLoopSession();
543
+ let observedLockStatus: SessionLockStatus | undefined;
544
+
545
+ const deps = makeMockDeps({
546
+ validateSessionLock: () =>
547
+ ({
548
+ valid: false,
549
+ failureReason: "compromised",
550
+ expectedPid: process.pid,
551
+ }) as SessionLockStatus,
552
+ handleLostSessionLock: (_ctx, lockStatus) => {
553
+ observedLockStatus = lockStatus;
554
+ deps.callLog.push("handleLostSessionLock");
555
+ },
556
+ });
557
+
558
+ await autoLoop(ctx, pi, s, deps);
559
+
560
+ assert.deepEqual(observedLockStatus, {
561
+ valid: false,
562
+ failureReason: "compromised",
563
+ expectedPid: process.pid,
564
+ });
565
+ assert.ok(
566
+ !deps.callLog.includes("resolveDispatch"),
567
+ "should stop before dispatch after lock validation fails",
568
+ );
569
+ });
570
+
535
571
  test("autoLoop exits on terminal blocked state", async (t) => {
536
572
  _resetPendingResolve();
537
573
 
@@ -153,6 +153,25 @@ async function main(): Promise<void> {
153
153
  // After teardown, originalBase should be null
154
154
  assertEq(getAutoWorktreeOriginalBase(), null, "no split-brain: originalBase cleared");
155
155
 
156
+ // ─── #1526: getMainBranch returns milestone branch in auto-worktree ──
157
+ console.log("\n=== #1526: getMainBranch() returns milestone/<MID> in auto-worktree ===");
158
+ {
159
+ const { GitServiceImpl } = await import("../git-service.ts");
160
+
161
+ // Create worktree
162
+ const wtPath = createAutoWorktree(tempDir, "M005");
163
+ // Don't set main_branch pref so getMainBranch falls through to worktree detection
164
+ const gitService = new GitServiceImpl(wtPath);
165
+ gitService.setMilestoneId("M005");
166
+
167
+ // Verify getMainBranch returns the milestone branch
168
+ const mainBranch = gitService.getMainBranch();
169
+ assertEq(mainBranch, "milestone/M005", "getMainBranch returns milestone/<MID> in auto-worktree");
170
+
171
+ // Cleanup
172
+ teardownAutoWorktree(tempDir, "M005");
173
+ }
174
+
156
175
  // ─── #778: reconcile plan checkboxes on re-attach ─────────────────
157
176
  console.log("\n=== #778: reconcile plan checkboxes on re-attach ===");
158
177
  {
@@ -1,5 +1,8 @@
1
- import test from "node:test";
1
+ import test, { describe } from "node:test";
2
2
  import assert from "node:assert/strict";
3
+ import * as fs from "node:fs";
4
+ import * as path from "node:path";
5
+ import { fileURLToPath } from "node:url";
3
6
  import {
4
7
  buildCmuxProgress,
5
8
  buildCmuxStatusLabel,
@@ -96,3 +99,24 @@ test("buildCmuxStatusLabel and progress prefer deepest active unit", () => {
96
99
  assert.equal(buildCmuxStatusLabel(state), "M001 S02/T03 · executing");
97
100
  assert.deepEqual(buildCmuxProgress(state), { value: 0.4, label: "2/5 tasks" });
98
101
  });
102
+
103
+ describe("cmux extension discovery opt-out", () => {
104
+ test("cmux directory has package.json with pi manifest to prevent auto-discovery as extension", () => {
105
+ const cmuxDir = path.resolve(
106
+ path.dirname(fileURLToPath(import.meta.url)),
107
+ "../../cmux",
108
+ );
109
+ const pkgPath = path.join(cmuxDir, "package.json");
110
+ assert.ok(fs.existsSync(pkgPath), `${pkgPath} must exist`);
111
+
112
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
113
+ assert.ok(
114
+ pkg.pi !== undefined && typeof pkg.pi === "object",
115
+ 'package.json must have a "pi" field to opt out of extension auto-discovery',
116
+ );
117
+ assert.ok(
118
+ !pkg.pi.extensions?.length,
119
+ "pi.extensions must be empty or absent — cmux is a library, not an extension",
120
+ );
121
+ });
122
+ });