@os-eco/overstory-cli 0.9.4 → 0.11.0

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 (124) hide show
  1. package/README.md +50 -19
  2. package/agents/builder.md +19 -9
  3. package/agents/coordinator.md +6 -6
  4. package/agents/lead.md +204 -87
  5. package/agents/merger.md +25 -14
  6. package/agents/reviewer.md +22 -16
  7. package/agents/scout.md +17 -12
  8. package/package.json +6 -3
  9. package/src/agents/capabilities.test.ts +85 -0
  10. package/src/agents/capabilities.ts +125 -0
  11. package/src/agents/headless-mail-injector.test.ts +448 -0
  12. package/src/agents/headless-mail-injector.ts +219 -0
  13. package/src/agents/headless-prompt.test.ts +102 -0
  14. package/src/agents/headless-prompt.ts +68 -0
  15. package/src/agents/hooks-deployer.test.ts +514 -14
  16. package/src/agents/hooks-deployer.ts +141 -0
  17. package/src/agents/mail-poll-detect.test.ts +153 -0
  18. package/src/agents/mail-poll-detect.ts +73 -0
  19. package/src/agents/overlay.test.ts +60 -4
  20. package/src/agents/overlay.ts +63 -8
  21. package/src/agents/scope-detect.test.ts +190 -0
  22. package/src/agents/scope-detect.ts +146 -0
  23. package/src/agents/turn-lock.test.ts +181 -0
  24. package/src/agents/turn-lock.ts +235 -0
  25. package/src/agents/turn-runner-dispatch.test.ts +182 -0
  26. package/src/agents/turn-runner-dispatch.ts +105 -0
  27. package/src/agents/turn-runner.test.ts +2312 -0
  28. package/src/agents/turn-runner.ts +1383 -0
  29. package/src/commands/agents.ts +9 -0
  30. package/src/commands/clean.ts +54 -0
  31. package/src/commands/coordinator.test.ts +254 -0
  32. package/src/commands/coordinator.ts +273 -8
  33. package/src/commands/dashboard.test.ts +188 -0
  34. package/src/commands/dashboard.ts +14 -4
  35. package/src/commands/doctor.ts +3 -1
  36. package/src/commands/group.test.ts +94 -0
  37. package/src/commands/group.ts +49 -20
  38. package/src/commands/init.test.ts +8 -0
  39. package/src/commands/init.ts +8 -1
  40. package/src/commands/log.test.ts +187 -11
  41. package/src/commands/log.ts +171 -71
  42. package/src/commands/mail.test.ts +162 -0
  43. package/src/commands/mail.ts +64 -9
  44. package/src/commands/merge.test.ts +230 -1
  45. package/src/commands/merge.ts +68 -12
  46. package/src/commands/nudge.test.ts +351 -4
  47. package/src/commands/nudge.ts +356 -34
  48. package/src/commands/run.test.ts +43 -7
  49. package/src/commands/serve/build.test.ts +202 -0
  50. package/src/commands/serve/build.ts +206 -0
  51. package/src/commands/serve/coordinator-actions.test.ts +339 -0
  52. package/src/commands/serve/coordinator-actions.ts +408 -0
  53. package/src/commands/serve/dev.test.ts +168 -0
  54. package/src/commands/serve/dev.ts +117 -0
  55. package/src/commands/serve/mail-actions.test.ts +312 -0
  56. package/src/commands/serve/mail-actions.ts +167 -0
  57. package/src/commands/serve/rest.test.ts +1323 -0
  58. package/src/commands/serve/rest.ts +708 -0
  59. package/src/commands/serve/static.ts +51 -0
  60. package/src/commands/serve/ws.test.ts +361 -0
  61. package/src/commands/serve/ws.ts +332 -0
  62. package/src/commands/serve.test.ts +459 -0
  63. package/src/commands/serve.ts +565 -0
  64. package/src/commands/sling.test.ts +177 -1
  65. package/src/commands/sling.ts +243 -71
  66. package/src/commands/status.test.ts +9 -0
  67. package/src/commands/status.ts +12 -4
  68. package/src/commands/stop.test.ts +255 -1
  69. package/src/commands/stop.ts +107 -8
  70. package/src/commands/watch.test.ts +43 -0
  71. package/src/commands/watch.ts +153 -28
  72. package/src/config.ts +23 -0
  73. package/src/doctor/consistency.test.ts +106 -0
  74. package/src/doctor/consistency.ts +48 -1
  75. package/src/doctor/serve.test.ts +95 -0
  76. package/src/doctor/serve.ts +86 -0
  77. package/src/doctor/types.ts +2 -1
  78. package/src/doctor/watchdog.ts +57 -1
  79. package/src/events/tailer.test.ts +234 -1
  80. package/src/events/tailer.ts +90 -0
  81. package/src/index.ts +57 -6
  82. package/src/insights/quality-gates.test.ts +141 -0
  83. package/src/insights/quality-gates.ts +156 -0
  84. package/src/json.ts +29 -0
  85. package/src/logging/theme.ts +4 -0
  86. package/src/mail/client.ts +15 -2
  87. package/src/mail/store.test.ts +82 -0
  88. package/src/mail/store.ts +41 -4
  89. package/src/merge/lock.test.ts +149 -0
  90. package/src/merge/lock.ts +140 -0
  91. package/src/merge/predict.test.ts +387 -0
  92. package/src/merge/predict.ts +249 -0
  93. package/src/merge/resolver.ts +1 -1
  94. package/src/mulch/client.ts +3 -3
  95. package/src/runtimes/__fixtures__/claude-stream-fixture.ts +22 -0
  96. package/src/runtimes/claude.test.ts +791 -1
  97. package/src/runtimes/claude.ts +323 -1
  98. package/src/runtimes/connections.test.ts +141 -1
  99. package/src/runtimes/connections.ts +73 -4
  100. package/src/runtimes/headless-connection.test.ts +264 -0
  101. package/src/runtimes/headless-connection.ts +158 -0
  102. package/src/runtimes/types.ts +10 -0
  103. package/src/schema-consistency.test.ts +1 -0
  104. package/src/sessions/store.test.ts +657 -29
  105. package/src/sessions/store.ts +286 -23
  106. package/src/test-setup.test.ts +31 -0
  107. package/src/test-setup.ts +28 -0
  108. package/src/types.ts +107 -2
  109. package/src/utils/pid.test.ts +85 -1
  110. package/src/utils/pid.ts +86 -1
  111. package/src/utils/process-scan.test.ts +53 -0
  112. package/src/utils/process-scan.ts +76 -0
  113. package/src/watchdog/daemon.test.ts +1607 -376
  114. package/src/watchdog/daemon.ts +462 -88
  115. package/src/watchdog/health.test.ts +282 -0
  116. package/src/watchdog/health.ts +126 -27
  117. package/src/worktree/manager.test.ts +218 -1
  118. package/src/worktree/manager.ts +55 -0
  119. package/src/worktree/process.test.ts +71 -0
  120. package/src/worktree/process.ts +25 -5
  121. package/src/worktree/tmux.test.ts +28 -0
  122. package/src/worktree/tmux.ts +27 -3
  123. package/templates/CLAUDE.md.tmpl +19 -8
  124. package/templates/overlay.md.tmpl +5 -2
@@ -20,6 +20,7 @@ import {
20
20
  loadGroups,
21
21
  printGroupProgress,
22
22
  removeFromGroup,
23
+ resolveGroup,
23
24
  } from "./group.ts";
24
25
 
25
26
  let tempDir: string;
@@ -379,3 +380,96 @@ describe("printGroupProgress", () => {
379
380
  expect(output).toContain("2026-01-15T10:00:00.000Z");
380
381
  });
381
382
  });
383
+
384
+ // -- resolveGroup --
385
+
386
+ describe("resolveGroup", () => {
387
+ test("resolves by exact UUID", () => {
388
+ const a = makeGroup({ id: "group-aaaaaaaa", name: "alpha" });
389
+ const b = makeGroup({ id: "group-bbbbbbbb", name: "beta" });
390
+ expect(resolveGroup([a, b], "group-aaaaaaaa")).toBe(a);
391
+ });
392
+
393
+ test("resolves by unique name", () => {
394
+ const a = makeGroup({ id: "group-aaaaaaaa", name: "alpha" });
395
+ const b = makeGroup({ id: "group-bbbbbbbb", name: "beta" });
396
+ expect(resolveGroup([a, b], "beta")).toBe(b);
397
+ });
398
+
399
+ test("ID match wins when name === some-other-group's-id", () => {
400
+ const a = makeGroup({ id: "group-aaaaaaaa", name: "group-bbbbbbbb" });
401
+ const b = makeGroup({ id: "group-bbbbbbbb", name: "beta" });
402
+ expect(resolveGroup([a, b], "group-bbbbbbbb")).toBe(b);
403
+ });
404
+
405
+ test("name match prefers the active group when others are completed", () => {
406
+ const old = makeGroup({ id: "group-aaaaaaaa", name: "dup", status: "completed" });
407
+ const live = makeGroup({ id: "group-bbbbbbbb", name: "dup", status: "active" });
408
+ expect(resolveGroup([old, live], "dup")).toBe(live);
409
+ });
410
+
411
+ test("ambiguous when multiple active groups share a name", () => {
412
+ const x = makeGroup({ id: "group-aaaaaaaa", name: "dup", status: "active" });
413
+ const y = makeGroup({ id: "group-bbbbbbbb", name: "dup", status: "active" });
414
+ expect(() => resolveGroup([x, y], "dup")).toThrow(GroupError);
415
+ try {
416
+ resolveGroup([x, y], "dup");
417
+ } catch (err) {
418
+ expect(err).toBeInstanceOf(GroupError);
419
+ const message = (err as GroupError).message;
420
+ expect(message).toContain("ambiguous");
421
+ expect(message).toContain("group-aaaaaaaa");
422
+ expect(message).toContain("group-bbbbbbbb");
423
+ }
424
+ });
425
+
426
+ test("ambiguous when zero active groups share the name", () => {
427
+ const x = makeGroup({ id: "group-aaaaaaaa", name: "dup", status: "completed" });
428
+ const y = makeGroup({ id: "group-bbbbbbbb", name: "dup", status: "completed" });
429
+ expect(() => resolveGroup([x, y], "dup")).toThrow(GroupError);
430
+ });
431
+
432
+ test("throws not-found for unknown identifier", () => {
433
+ const a = makeGroup({ id: "group-aaaaaaaa", name: "alpha" });
434
+ expect(() => resolveGroup([a], "nope")).toThrow(/not found/);
435
+ });
436
+ });
437
+
438
+ // -- name-or-id lookup in addToGroup / removeFromGroup --
439
+
440
+ describe("name-or-id lookup", () => {
441
+ test("addToGroup resolves by name", async () => {
442
+ const group = makeGroup({ id: "group-aaaaaaaa", name: "alpha", memberIssueIds: ["i1"] });
443
+ await writeGroups([group]);
444
+ const tracker = stubTrackerOk();
445
+ const updated = await addToGroup(tempDir, "alpha", ["i2"], false, tracker);
446
+ expect(updated.id).toBe("group-aaaaaaaa");
447
+ expect(updated.memberIssueIds).toEqual(["i1", "i2"]);
448
+ });
449
+
450
+ test("removeFromGroup resolves by name", async () => {
451
+ const group = makeGroup({ id: "group-aaaaaaaa", name: "alpha", memberIssueIds: ["i1", "i2"] });
452
+ await writeGroups([group]);
453
+ const updated = await removeFromGroup(tempDir, "alpha", ["i2"]);
454
+ expect(updated.id).toBe("group-aaaaaaaa");
455
+ expect(updated.memberIssueIds).toEqual(["i1"]);
456
+ });
457
+ });
458
+
459
+ function stubTrackerOk(): import("../tracker/types.ts").TrackerClient {
460
+ return {
461
+ ready: async () => [],
462
+ show: async (id: string): Promise<TrackerIssue> => ({
463
+ id,
464
+ title: id,
465
+ status: "open",
466
+ priority: 2,
467
+ type: "task",
468
+ }),
469
+ create: async () => "stub-id",
470
+ claim: async () => undefined,
471
+ close: async () => undefined,
472
+ list: async () => [],
473
+ sync: async () => undefined,
474
+ };
475
+ }
@@ -79,6 +79,44 @@ function generateGroupId(): string {
79
79
  return `group-${crypto.randomUUID().slice(0, 8)}`;
80
80
  }
81
81
 
82
+ /**
83
+ * Resolve a group by ID or name.
84
+ *
85
+ * Names are not enforced unique by `createGroup`, so live `groups.json` files
86
+ * contain duplicate names — a naive name lookup would silently pick the wrong
87
+ * group. Resolution precedence:
88
+ * 1. Exact ID match wins (UUIDs are unambiguous).
89
+ * 2. Otherwise filter by name. If exactly one match, return it.
90
+ * 3. If multiple name matches, prefer a single `active` one. If still
91
+ * ambiguous, throw with the matching IDs so the caller can disambiguate
92
+ * by passing the UUID.
93
+ *
94
+ * @internal Exported for testing.
95
+ */
96
+ export function resolveGroup(groups: TaskGroup[], identifier: string): TaskGroup {
97
+ const byId = groups.find((g) => g.id === identifier);
98
+ if (byId) return byId;
99
+
100
+ const byName = groups.filter((g) => g.name === identifier);
101
+ if (byName.length === 1) {
102
+ const only = byName[0];
103
+ if (only) return only;
104
+ }
105
+ if (byName.length > 1) {
106
+ const active = byName.filter((g) => g.status === "active");
107
+ if (active.length === 1) {
108
+ const only = active[0];
109
+ if (only) return only;
110
+ }
111
+ const ids = byName.map((g) => g.id).join(", ");
112
+ throw new GroupError(
113
+ `Group name "${identifier}" is ambiguous (matches: ${ids}). Use the group ID.`,
114
+ { groupId: identifier },
115
+ );
116
+ }
117
+ throw new GroupError(`Group "${identifier}" not found`, { groupId: identifier });
118
+ }
119
+
82
120
  /**
83
121
  * Create a new task group.
84
122
  * @internal Exported for testing.
@@ -140,16 +178,13 @@ export async function addToGroup(
140
178
  }
141
179
 
142
180
  const groups = await loadGroups(projectRoot);
143
- const group = groups.find((g) => g.id === groupId);
144
- if (!group) {
145
- throw new GroupError(`Group "${groupId}" not found`, { groupId });
146
- }
181
+ const group = resolveGroup(groups, groupId);
147
182
 
148
183
  // Check for duplicates against existing members
149
184
  for (const id of issueIds) {
150
185
  if (group.memberIssueIds.includes(id)) {
151
- throw new GroupError(`Issue "${id}" is already a member of group "${groupId}"`, {
152
- groupId,
186
+ throw new GroupError(`Issue "${id}" is already a member of group "${group.id}"`, {
187
+ groupId: group.id,
153
188
  });
154
189
  }
155
190
  }
@@ -187,16 +222,13 @@ export async function removeFromGroup(
187
222
  }
188
223
 
189
224
  const groups = await loadGroups(projectRoot);
190
- const group = groups.find((g) => g.id === groupId);
191
- if (!group) {
192
- throw new GroupError(`Group "${groupId}" not found`, { groupId });
193
- }
225
+ const group = resolveGroup(groups, groupId);
194
226
 
195
227
  // Validate all issues are members
196
228
  for (const id of issueIds) {
197
229
  if (!group.memberIssueIds.includes(id)) {
198
- throw new GroupError(`Issue "${id}" is not a member of group "${groupId}"`, {
199
- groupId,
230
+ throw new GroupError(`Issue "${id}" is not a member of group "${group.id}"`, {
231
+ groupId: group.id,
200
232
  });
201
233
  }
202
234
  }
@@ -204,7 +236,7 @@ export async function removeFromGroup(
204
236
  // Check that removal won't empty the group
205
237
  const remaining = group.memberIssueIds.filter((id) => !issueIds.includes(id));
206
238
  if (remaining.length === 0) {
207
- throw new GroupError("Cannot remove all issues from a group", { groupId });
239
+ throw new GroupError("Cannot remove all issues from a group", { groupId: group.id });
208
240
  }
209
241
 
210
242
  group.memberIssueIds = remaining;
@@ -347,7 +379,7 @@ export function createGroupCommand(): Command {
347
379
  cmd
348
380
  .command("status")
349
381
  .description("Show progress for one or all groups")
350
- .argument("[group-id]", "Group ID (optional, shows all if omitted)")
382
+ .argument("[group-id-or-name]", "Group ID or name (optional, shows all if omitted)")
351
383
  .option("--json", "Output as JSON")
352
384
  .option("--skip-validation", "Skip task validation (for offline use)")
353
385
  .action(
@@ -361,10 +393,7 @@ export function createGroupCommand(): Command {
361
393
  const groups = await loadGroups(projectRoot);
362
394
 
363
395
  if (groupId) {
364
- const group = groups.find((g) => g.id === groupId);
365
- if (!group) {
366
- throw new GroupError(`Group "${groupId}" not found`, { groupId });
367
- }
396
+ const group = resolveGroup(groups, groupId);
368
397
  const progress = await getGroupProgress(projectRoot, group, groups, tracker);
369
398
  if (json) {
370
399
  jsonOutput("group status", { ...progress });
@@ -401,7 +430,7 @@ export function createGroupCommand(): Command {
401
430
  cmd
402
431
  .command("add")
403
432
  .description("Add issues to a group")
404
- .argument("<group-id>", "Group ID")
433
+ .argument("<group-id-or-name>", "Group ID or name")
405
434
  .argument("<ids...>", "Issue IDs to add")
406
435
  .option("--json", "Output as JSON")
407
436
  .option("--skip-validation", "Skip task validation (for offline use)")
@@ -437,7 +466,7 @@ export function createGroupCommand(): Command {
437
466
  cmd
438
467
  .command("remove")
439
468
  .description("Remove issues from a group")
440
- .argument("<group-id>", "Group ID")
469
+ .argument("<group-id-or-name>", "Group ID or name")
441
470
  .argument("<ids...>", "Issue IDs to remove")
442
471
  .option("--json", "Output as JSON")
443
472
  .action(async (groupId: string, ids: string[], opts: { json?: boolean }) => {
@@ -353,6 +353,14 @@ describe("initCommand: canonical branch detection", () => {
353
353
  const content = await Bun.file(configPath).text();
354
354
  expect(content).toContain("canonicalBranch: main");
355
355
  });
356
+
357
+ test("generated config opts into headless Claude by default (overstory-caec)", async () => {
358
+ await initCommand({ _spawner: noopSpawner });
359
+
360
+ const configPath = join(tempDir, ".overstory", "config.yaml");
361
+ const content = await Bun.file(configPath).text();
362
+ expect(content).toContain("claudeHeadlessByDefault: true");
363
+ });
356
364
  });
357
365
 
358
366
  describe("initCommand: --yes flag", () => {
@@ -816,6 +816,10 @@ export async function initCommand(opts: InitOptions): Promise<void> {
816
816
  config.project.canonicalBranch = canonicalBranch;
817
817
  if (config.runtime) {
818
818
  config.runtime.default = defaultRuntime;
819
+ // New projects default to headless Claude spawns; the UI (`ov serve`) is the
820
+ // primary operator surface and tmux is opt-in via `--no-headless`. Existing
821
+ // projects keep tmux until they edit their config (overstory-caec).
822
+ config.runtime.claudeHeadlessByDefault = true;
819
823
  }
820
824
 
821
825
  const configYaml = serializeConfigToYaml(config);
@@ -956,5 +960,8 @@ export async function initCommand(opts: InitOptions): Promise<void> {
956
960
 
957
961
  printSuccess("Initialized");
958
962
  printHint("Next: run `ov hooks install` to enable Claude Code hooks.");
959
- printHint("Then: run `ov status` to see the current state.");
963
+ printHint("Then: `ov coordinator start` and `ov serve` open http://localhost:7321");
964
+ printHint(
965
+ " (UI is the primary operator surface; pass `--no-headless` to ov sling for tmux attach)",
966
+ );
960
967
  }
@@ -633,8 +633,55 @@ describe("logCommand", () => {
633
633
  });
634
634
  });
635
635
 
636
- test("session-end writes pending-nudge marker for coordinator when lead completes", async () => {
637
- // Create sessions.db with a lead agent
636
+ test("session-end does NOT transition lead to completed (persistent agent)", async () => {
637
+ // Regression test for overstory-49a7:
638
+ // The lead's Stop hook fires every turn (interactive Claude Code), not just at
639
+ // true session end. session-end must NOT mark leads completed, or they vanish
640
+ // from getActive() after their first turn while their tmux is still alive.
641
+ const dbPath = join(tempDir, ".overstory", "sessions.db");
642
+ const session: AgentSession = {
643
+ id: "session-lead",
644
+ agentName: "lead-alpha",
645
+ capability: "lead",
646
+ worktreePath: tempDir,
647
+ branchName: "lead-alpha-branch",
648
+ taskId: "bead-lead-001",
649
+ tmuxSession: "overstory-lead-alpha",
650
+ state: "working",
651
+ pid: 33333,
652
+ parentAgent: null,
653
+ depth: 0,
654
+ runId: null,
655
+ startedAt: new Date().toISOString(),
656
+ lastActivity: new Date(Date.now() - 60_000).toISOString(),
657
+ escalationLevel: 0,
658
+ stalledSince: null,
659
+ transcriptPath: null,
660
+ };
661
+ const store = createSessionStore(dbPath);
662
+ store.upsert(session);
663
+ store.close();
664
+
665
+ await logCommand(["session-end", "--agent", "lead-alpha"]);
666
+
667
+ // Lead should remain 'working', not transition to 'completed'
668
+ const readStore = createSessionStore(dbPath);
669
+ const updatedSession = readStore.getByName("lead-alpha");
670
+ readStore.close();
671
+
672
+ expect(updatedSession).toBeDefined();
673
+ expect(updatedSession?.state).toBe("working");
674
+ // But lastActivity should be updated
675
+ expect(new Date(updatedSession?.lastActivity ?? "").getTime()).toBeGreaterThan(
676
+ new Date(session.lastActivity).getTime(),
677
+ );
678
+ });
679
+
680
+ test("session-end does NOT write pending-nudge marker for leads (moved to ov stop)", async () => {
681
+ // Regression test for overstory-49a7:
682
+ // The lead_completed nudge used to fire from the per-turn Stop hook, spamming
683
+ // the coordinator with false completion signals every turn. It is now emitted
684
+ // only by `ov stop <lead>` (the real completion signal).
638
685
  const dbPath = join(tempDir, ".overstory", "sessions.db");
639
686
  const session: AgentSession = {
640
687
  id: "session-lead",
@@ -661,17 +708,10 @@ describe("logCommand", () => {
661
708
 
662
709
  await logCommand(["session-end", "--agent", "lead-alpha"]);
663
710
 
664
- // Verify the pending-nudge marker was written for the coordinator
711
+ // No pending-nudge marker should be written from session-end
665
712
  const markerPath = join(tempDir, ".overstory", "pending-nudges", "coordinator.json");
666
713
  const markerFile = Bun.file(markerPath);
667
- expect(await markerFile.exists()).toBe(true);
668
-
669
- const marker = JSON.parse(await markerFile.text());
670
- expect(marker.from).toBe("lead-alpha");
671
- expect(marker.reason).toBe("lead_completed");
672
- expect(marker.subject).toContain("lead-alpha");
673
- expect(marker.messageId).toContain("auto-nudge-lead-alpha-");
674
- expect(marker.createdAt).toBeDefined();
714
+ expect(await markerFile.exists()).toBe(false);
675
715
  });
676
716
 
677
717
  test("session-end does NOT write pending-nudge marker for non-lead agents", async () => {
@@ -1259,6 +1299,81 @@ describe("logCommand", () => {
1259
1299
  expect(mail?.body).toContain("10 tool calls");
1260
1300
  expect(mail?.body).toContain("pattern"); // At least 1 pattern insight
1261
1301
  });
1302
+
1303
+ test("threads outcomeStatus into per-domain reference and per-insight records", async () => {
1304
+ const learnResult: MulchLearnResult = {
1305
+ success: true,
1306
+ command: "mulch learn",
1307
+ changedFiles: ["src/foo.ts"],
1308
+ suggestedDomains: ["typescript"],
1309
+ unmatchedFiles: [],
1310
+ };
1311
+ const { client, recordCalls } = createFakeMulchClient(learnResult);
1312
+ const mailDbPath = join(tempDir, ".overstory", "auto-record-outcome.db");
1313
+
1314
+ // Seed events so analyzer emits at least one insight (10+ tool calls).
1315
+ const eventsDbPath = join(tempDir, ".overstory", "events.db");
1316
+ const eventStore = createEventStore(eventsDbPath);
1317
+ const sessionStartedAt = new Date(Date.now() - 60_000).toISOString();
1318
+ for (let i = 0; i < 10; i++) {
1319
+ eventStore.insert({
1320
+ runId: null,
1321
+ agentName: "outcome-agent",
1322
+ sessionId: "sess-outcome",
1323
+ eventType: "tool_start",
1324
+ toolName: "Read",
1325
+ toolArgs: JSON.stringify({ file_path: `/src/file${i}.ts` }),
1326
+ toolDurationMs: null,
1327
+ level: "info",
1328
+ data: JSON.stringify({ summary: `read: /src/file${i}.ts` }),
1329
+ });
1330
+ }
1331
+ eventStore.close();
1332
+
1333
+ await autoRecordExpertise({
1334
+ mulchClient: client,
1335
+ agentName: "outcome-agent",
1336
+ capability: "builder",
1337
+ taskId: "bead-outcome",
1338
+ mailDbPath,
1339
+ parentAgent: "parent-agent",
1340
+ projectRoot: tempDir,
1341
+ sessionStartedAt,
1342
+ outcomeStatus: "partial",
1343
+ });
1344
+
1345
+ expect(recordCalls.length).toBeGreaterThanOrEqual(2);
1346
+ for (const call of recordCalls) {
1347
+ expect(call.options.outcomeStatus).toBe("partial");
1348
+ expect(call.options.outcomeAgent).toBe("outcome-agent");
1349
+ }
1350
+ });
1351
+
1352
+ test("omits outcomeStatus when caller does not supply one", async () => {
1353
+ const learnResult: MulchLearnResult = {
1354
+ success: true,
1355
+ command: "mulch learn",
1356
+ changedFiles: ["src/foo.ts"],
1357
+ suggestedDomains: ["typescript"],
1358
+ unmatchedFiles: [],
1359
+ };
1360
+ const { client, recordCalls } = createFakeMulchClient(learnResult);
1361
+ const mailDbPath = join(tempDir, ".overstory", "auto-record-no-outcome.db");
1362
+
1363
+ await autoRecordExpertise({
1364
+ mulchClient: client,
1365
+ agentName: "no-outcome-agent",
1366
+ capability: "builder",
1367
+ taskId: null,
1368
+ mailDbPath,
1369
+ parentAgent: null,
1370
+ projectRoot: tempDir,
1371
+ sessionStartedAt: new Date().toISOString(),
1372
+ });
1373
+
1374
+ expect(recordCalls).toHaveLength(1);
1375
+ expect(recordCalls[0]?.options.outcomeStatus).toBeUndefined();
1376
+ });
1262
1377
  });
1263
1378
 
1264
1379
  /**
@@ -1312,6 +1427,10 @@ try {
1312
1427
  stdin: "pipe",
1313
1428
  stdout: "pipe",
1314
1429
  stderr: "pipe",
1430
+ // Pin project root to tempDir. Without this, a subprocess started from
1431
+ // inside an `ov sling`-spawned worktree inherits OVERSTORY_PROJECT_ROOT
1432
+ // pointing at the parent project, and writes events to prod's events.db.
1433
+ env: { ...process.env, OVERSTORY_PROJECT_ROOT: tempDir },
1315
1434
  });
1316
1435
 
1317
1436
  // Write the JSON payload to stdin and close
@@ -1501,6 +1620,7 @@ try {
1501
1620
  stdin: "pipe",
1502
1621
  stdout: "pipe",
1503
1622
  stderr: "pipe",
1623
+ env: { ...process.env, OVERSTORY_PROJECT_ROOT: tempDir },
1504
1624
  });
1505
1625
 
1506
1626
  // Write empty string and close immediately
@@ -1734,4 +1854,60 @@ describe("appendOutcomeToAppliedRecords", () => {
1734
1854
  });
1735
1855
  expect(count).toBe(0);
1736
1856
  });
1857
+
1858
+ test("uses supplied outcomeStatus when provided", async () => {
1859
+ const agentDir = join(tempDir, ".overstory", "agents", "test-agent");
1860
+ await mkdir(agentDir, { recursive: true });
1861
+ await Bun.write(
1862
+ join(agentDir, "applied-records.json"),
1863
+ JSON.stringify({
1864
+ taskId: "bead-outcome",
1865
+ agentName: "test-agent",
1866
+ capability: "builder",
1867
+ records: [{ id: "mx-aaa111", domain: "agents" }],
1868
+ }),
1869
+ );
1870
+
1871
+ const { client, appendOutcomeCalls } = makeOutcomeClient();
1872
+ await appendOutcomeToAppliedRecords({
1873
+ mulchClient: client,
1874
+ agentName: "test-agent",
1875
+ capability: "builder",
1876
+ taskId: "bead-outcome",
1877
+ projectRoot: tempDir,
1878
+ outcomeStatus: "failure",
1879
+ });
1880
+
1881
+ expect(appendOutcomeCalls).toHaveLength(1);
1882
+ expect(appendOutcomeCalls[0]?.outcome).toMatchObject({ status: "failure" });
1883
+ expect(appendOutcomeCalls[0]?.outcome.notes).toContain("Quality gates: failure");
1884
+ });
1885
+
1886
+ test("falls back to 'success' when outcomeStatus is undefined (backward compat)", async () => {
1887
+ const agentDir = join(tempDir, ".overstory", "agents", "test-agent");
1888
+ await mkdir(agentDir, { recursive: true });
1889
+ await Bun.write(
1890
+ join(agentDir, "applied-records.json"),
1891
+ JSON.stringify({
1892
+ taskId: "bead-default",
1893
+ agentName: "test-agent",
1894
+ capability: "builder",
1895
+ records: [{ id: "mx-bbb222", domain: "agents" }],
1896
+ }),
1897
+ );
1898
+
1899
+ const { client, appendOutcomeCalls } = makeOutcomeClient();
1900
+ await appendOutcomeToAppliedRecords({
1901
+ mulchClient: client,
1902
+ agentName: "test-agent",
1903
+ capability: "builder",
1904
+ taskId: "bead-default",
1905
+ projectRoot: tempDir,
1906
+ });
1907
+
1908
+ expect(appendOutcomeCalls).toHaveLength(1);
1909
+ expect(appendOutcomeCalls[0]?.outcome.status).toBe("success");
1910
+ // No "Quality gates:" annotation when caller didn't provide outcomeStatus
1911
+ expect(appendOutcomeCalls[0]?.outcome.notes).not.toContain("Quality gates:");
1912
+ });
1737
1913
  });