pi-odd-worktree 3.12.2 → 3.12.4

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.
@@ -178,18 +178,69 @@ function sectionOf(loaded) {
178
178
  return loaded.sections.find((section) => section && section.id === RAIL_ID);
179
179
  }
180
180
 
181
- function ctxFor(repo, registeredRoots = []) {
181
+ // The session the section reads. T4 (odd-rail-session-scope S2-S5): the rail draws
182
+ // only the ODDs the session is ASSOCIATED with, so the context carries an
183
+ // association; `associated` names are handed over as the persisted touch entries a
184
+ // real session has, which is the same evidence `associatedFromHistory` reads.
185
+ //
186
+ // `getEntries()` is the append-only list and `getBranch()` is the path the session is
187
+ // on, so a case can put a registration or a write on an abandoned branch. Both
188
+ // answer the same rows unless a case says otherwise.
189
+ function ctxFor(repo, registeredRoots = [], options = {}) {
190
+ const opts = options !== null && typeof options === 'object' ? options : {};
191
+ const registrations = opts.registeredInBranch === false
192
+ ? []
193
+ : registeredRoots.map((root) => ({
194
+ type: 'custom',
195
+ customType: SESSION_WORKTREE_CUSTOM_TYPE,
196
+ data: { sessionId: 'thread-1', root, evidence: 'session:tool' },
197
+ }));
198
+ // The association as a real session HAS it: an `edit` the session made on the
199
+ // document, with the successful result that vouches for it. It is history, not a
200
+ // touch, so it decides WHICH rows exist and never their order (S9's ordering is
201
+ // the live record's alone).
202
+ const association = (Array.isArray(opts.associated) ? opts.associated : []).flatMap((name) => {
203
+ const id = `call-${name}`;
204
+ const target = path.join(repo, 'odd', 'tasks', `${name}.md`);
205
+ return [
206
+ {
207
+ type: 'message',
208
+ id: `assistant-${name}`,
209
+ parentId: null,
210
+ message: { role: 'assistant', content: [{ type: 'toolCall', id, name: 'edit', arguments: { path: target } }] },
211
+ },
212
+ {
213
+ type: 'message',
214
+ id: `result-${name}`,
215
+ parentId: `assistant-${name}`,
216
+ message: { role: 'toolResult', toolCallId: id, toolName: 'edit', isError: false, content: [] },
217
+ },
218
+ ];
219
+ });
220
+ const branch = [
221
+ ...registrations,
222
+ ...association,
223
+ ...(Array.isArray(opts.branchEntries) ? opts.branchEntries : []),
224
+ ];
225
+ const entries = opts.registeredInBranch === false
226
+ ? [
227
+ ...registeredRoots.map((root) => ({
228
+ type: 'custom',
229
+ customType: SESSION_WORKTREE_CUSTOM_TYPE,
230
+ data: { sessionId: 'thread-1', root, evidence: 'session:tool' },
231
+ })),
232
+ ...association,
233
+ ...(Array.isArray(opts.entries) ? opts.entries : []),
234
+ ]
235
+ : [...branch, ...(Array.isArray(opts.entries) ? opts.entries : [])];
182
236
  return {
183
237
  cwd: repo,
184
238
  hasUI: true,
185
239
  mode: 'tui',
186
240
  sessionManager: {
187
241
  getSessionId: () => 'thread-1',
188
- getEntries: () => registeredRoots.map((root) => ({
189
- type: 'custom',
190
- customType: SESSION_WORKTREE_CUSTOM_TYPE,
191
- data: { sessionId: 'thread-1', root, evidence: 'session:tool' },
192
- })),
242
+ getEntries: () => entries,
243
+ getBranch: () => branch,
193
244
  },
194
245
  ui: { notify: () => {}, custom: undefined, select: undefined },
195
246
  };
@@ -290,7 +341,7 @@ describe('the rows the rail draws (S5, S12, S13)', () => {
290
341
  test('main only when no worktree is registered, and one row per name with `main` as meta', async () => {
291
342
  const loaded = await load();
292
343
  const repo = makeRepo();
293
- const ctx = ctxFor(repo, []);
344
+ const ctx = ctxFor(repo, [], { associated: ['first', 'second'] });
294
345
  startSession(loaded, ctx);
295
346
  const section = sectionOf(loaded);
296
347
  const rows = await waitForRows(section, ctx, 2);
@@ -309,7 +360,7 @@ describe('the rows the rail draws (S5, S12, S13)', () => {
309
360
  for (const name of ['first', 'second']) {
310
361
  writeTask(worktree, name, fs.readFileSync(path.join(repo, 'odd', 'tasks', `${name}.md`), 'utf8'), 300);
311
362
  }
312
- const ctx = ctxFor(repo, [worktree]);
363
+ const ctx = ctxFor(repo, [worktree], { associated: ['first', 'second'] });
313
364
  startSession(loaded, ctx);
314
365
  const section = sectionOf(loaded);
315
366
  const mainOnly = await waitForRows(section, ctx, 2);
@@ -323,7 +374,7 @@ describe('the rows the rail draws (S5, S12, S13)', () => {
323
374
  shutdown(loaded, ctx);
324
375
  // A fresh load for a fresh projection: the section is a new binding.
325
376
  const again = await load();
326
- const ctxAgain = ctxFor(repo, [worktree]);
377
+ const ctxAgain = ctxFor(repo, [worktree], { associated: ['first', 'second', 'wt-only'] });
327
378
  startSession(again, ctxAgain);
328
379
  const rows = await waitForRows(sectionOf(again), ctxAgain, 3);
329
380
  const byName = new Map(rows.map((row) => [row.label, row]));
@@ -345,7 +396,7 @@ describe('the rows the rail draws (S5, S12, S13)', () => {
345
396
  }
346
397
  git(repo, ['add', '-A']);
347
398
  git(repo, ['commit', '-m', 'more']);
348
- const ctx = ctxFor(repo, []);
399
+ const ctx = ctxFor(repo, [], { associated: ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'] });
349
400
  startSession(loaded, ctx);
350
401
  const section = sectionOf(loaded);
351
402
  const deadline = Date.now() + 20_000;
@@ -366,7 +417,7 @@ describe('attribution and the click (S6, S9)', () => {
366
417
  test('a write recorded by T8 sends its ODD first and forces the refresh', async () => {
367
418
  const loaded = await load();
368
419
  const repo = makeRepo();
369
- const ctx = ctxFor(repo, []);
420
+ const ctx = ctxFor(repo, [], { associated: ['first', 'second'] });
370
421
  startSession(loaded, ctx);
371
422
  const section = sectionOf(loaded);
372
423
  const before = await waitForRows(section, ctx, 2);
@@ -391,7 +442,7 @@ describe('attribution and the click (S6, S9)', () => {
391
442
  test('a click dispatches ONE /odd message, a second click inside the guard dispatches nothing', async () => {
392
443
  const loaded = await load();
393
444
  const repo = makeRepo();
394
- const ctx = ctxFor(repo, []);
445
+ const ctx = ctxFor(repo, [], { associated: ['first', 'second'] });
395
446
  startSession(loaded, ctx);
396
447
  const section = sectionOf(loaded);
397
448
  const rows = await waitForRows(section, ctx, 2);
@@ -419,7 +470,7 @@ describe('attribution and the click (S6, S9)', () => {
419
470
  const loaded = await load();
420
471
  const repo = makeRepo();
421
472
  const spaced = writeTask(repo, 'with space', '# body of with space\n');
422
- const ctx = ctxFor(repo, []);
473
+ const ctx = ctxFor(repo, [], { associated: ['first', 'second', 'with space'] });
423
474
  startSession(loaded, ctx);
424
475
  const section = sectionOf(loaded);
425
476
  const deadline = Date.now() + 20_000;
@@ -462,7 +513,7 @@ describe('the rail follows its session (S13)', () => {
462
513
  test('a session_start drops the previous rows, so the next frame reloads', async () => {
463
514
  const loaded = await load();
464
515
  const repo = makeRepo();
465
- const ctx = ctxFor(repo, []);
516
+ const ctx = ctxFor(repo, [], { associated: ['first', 'second', 'third'] });
466
517
  startSession(loaded, ctx);
467
518
  const section = sectionOf(loaded);
468
519
  const first = await waitForRows(section, ctx, 2);
@@ -480,7 +531,7 @@ describe('the rail follows its session (S13)', () => {
480
531
  test('a fired worktree-registration event forces a refresh', async () => {
481
532
  const loaded = await load();
482
533
  const repo = makeRepo();
483
- const ctx = ctxFor(repo, []);
534
+ const ctx = ctxFor(repo, [], { associated: ['first', 'second', 'third'] });
484
535
  startSession(loaded, ctx);
485
536
  const section = sectionOf(loaded);
486
537
  await waitForRows(section, ctx, 2);
@@ -496,7 +547,7 @@ describe('the rail follows its session (S13)', () => {
496
547
  test('a host without pi.events still registers the rail and draws its rows', async () => {
497
548
  const loaded = await load({ noEvents: true });
498
549
  const repo = makeRepo();
499
- const ctx = ctxFor(repo, []);
550
+ const ctx = ctxFor(repo, [], { associated: ['first', 'second'] });
500
551
  startSession(loaded, ctx);
501
552
  const section = sectionOf(loaded);
502
553
  assert.ok(section, 'the rail registers on a host with no event seam');
@@ -508,7 +559,7 @@ describe('the rail follows its session (S13)', () => {
508
559
  test('a host whose event seam throws still registers the rail', async () => {
509
560
  const loaded = await load({ eventsOnThrows: true });
510
561
  const repo = makeRepo();
511
- const ctx = ctxFor(repo, []);
562
+ const ctx = ctxFor(repo, [], { associated: ['first', 'second'] });
512
563
  startSession(loaded, ctx);
513
564
  const section = sectionOf(loaded);
514
565
  assert.ok(section, 'a throwing listener registration costs only the listener');
@@ -523,7 +574,7 @@ describe('teardown (S6, S9)', () => {
523
574
  const observed = observeGit(loaded.mod);
524
575
  try {
525
576
  const repo = makeRepo();
526
- const ctx = ctxFor(repo, []);
577
+ const ctx = ctxFor(repo, [], { associated: ['first', 'second'] });
527
578
  startSession(loaded, ctx);
528
579
  const section = sectionOf(loaded);
529
580
  const rows = await waitForRows(section, ctx, 2);
@@ -548,7 +599,7 @@ test('T6 round 4: a failed worktree-list refresh never labels an unregistered ch
548
599
  const repo = makeRepo();
549
600
  const linked = addWorktree(repo, 'linked');
550
601
  writeTask(linked, 'linked-private', '# Never in session scope\n');
551
- const ctx = ctxFor(linked, []);
602
+ const ctx = ctxFor(linked, [], { associated: ['first', 'second'] });
552
603
  startSession(e, ctx);
553
604
  const section = sectionOf(e);
554
605
  const gitLib = e.mod.libModule('worktree-git.js');
@@ -570,3 +621,171 @@ test('T6 round 4: a failed worktree-list refresh never labels an unregistered ch
570
621
  shutdown(e, ctx);
571
622
  }
572
623
  });
624
+
625
+ // T4 (odd-rail-session-scope S1-S5): the rail is the SESSION's, drawn from the
626
+ // association rebuilt off the active branch. Everything below speaks only the host's
627
+ // language — the entries a session file holds and the rows the section paints.
628
+ describe('the rail is scoped to the session’s association (S1-S5)', () => {
629
+ // One assistant `edit` toolCall and the toolResult that says whether it landed.
630
+ // This is what an OLD session has instead of the persisted touch entry.
631
+ function editHistory(repo, name, isError = false) {
632
+ const id = `call-${name}`;
633
+ return [
634
+ {
635
+ type: 'message',
636
+ id: `assistant-${name}`,
637
+ parentId: null,
638
+ message: {
639
+ role: 'assistant',
640
+ content: [{ type: 'toolCall', id, name: 'edit', arguments: { path: path.join(repo, 'odd', 'tasks', `${name}.md`) } }],
641
+ },
642
+ },
643
+ {
644
+ type: 'message',
645
+ id: `result-${name}`,
646
+ parentId: `assistant-${name}`,
647
+ message: { role: 'toolResult', toolCallId: id, toolName: 'edit', isError, content: [] },
648
+ },
649
+ ];
650
+ }
651
+ const report = (text) => ({ type: 'custom_message', id: 'report', parentId: null, customType: 'gentle-agents.result', content: text, display: true });
652
+
653
+ test('a NEW session draws the one dim line, not the four ODDs on disk (S1, S2)', async () => {
654
+ const loaded = await load();
655
+ const repo = makeRepo();
656
+ // The disk holds two documents; the session is associated with none of them.
657
+ const ctx = ctxFor(repo, []);
658
+ startSession(loaded, ctx);
659
+ const rows = await waitForRows(sectionOf(loaded), ctx, 1);
660
+ assert.deepEqual(rows, [{ label: 'no ODD associated', tone: 'dim' }],
661
+ 'the same dim line the GITHUB section draws when it has nothing');
662
+ assert.equal('onClick' in rows[0], false, 'and it is inert');
663
+ });
664
+
665
+ test('an OLD session’s successful edit draws its ODD, with no touch entry anywhere (S3)', async () => {
666
+ const loaded = await load();
667
+ const repo = makeRepo();
668
+ const branch = editHistory(repo, 'second');
669
+ assert.equal(branch.some((entry) => entry.customType === 'odd-worktree.odd-touch'), false,
670
+ 'the fixture really carries no persisted touch: the history is the only evidence');
671
+ const ctx = ctxFor(repo, [], { branchEntries: branch });
672
+ startSession(loaded, ctx);
673
+ const rows = await waitForRows(sectionOf(loaded), ctx, 1);
674
+ assert.deepEqual(rows.map((row) => [row.label, row.meta]), [['second', 'main']],
675
+ 'only the ODD that session worked on, not the one it never touched');
676
+ });
677
+
678
+ test('a FAILED write is not associated, so only the successful one draws (S3)', async () => {
679
+ const loaded = await load();
680
+ const repo = makeRepo();
681
+ const ctx = ctxFor(repo, [], {
682
+ branchEntries: [...editHistory(repo, 'first'), ...editHistory(repo, 'second', true)],
683
+ });
684
+ startSession(loaded, ctx);
685
+ const rows = await waitForRows(sectionOf(loaded), ctx, 1);
686
+ assert.deepEqual(rows.map((row) => row.label), ['first'], 'a write that never landed is no association');
687
+ });
688
+
689
+ test('an abandoned branch’s write is not associated: only the active branch counts (S3)', async () => {
690
+ const loaded = await load();
691
+ const repo = makeRepo();
692
+ // The abandoned path still sits in the append-only list; the ACTIVE branch does
693
+ // not contain it — that is the whole difference `getBranch()` makes.
694
+ const ctx = ctxFor(repo, [], { branchEntries: [], entries: editHistory(repo, 'second') });
695
+ startSession(loaded, ctx);
696
+ const rows = await waitForRows(sectionOf(loaded), ctx, 1);
697
+ assert.deepEqual(rows, [{ label: 'no ODD associated', tone: 'dim' }],
698
+ 'the branch the session is not on never claims an ODD');
699
+ });
700
+
701
+ test('a subagent’s report and its `Spec:` line associate their ODDs (S4)', async () => {
702
+ const loaded = await load();
703
+ const repo = makeRepo();
704
+ writeTask(repo, 'third', '# body of third\n', 300);
705
+ writeTask(repo, 'fourth', '# body of fourth\n', 400);
706
+ const task = `Do the work.\n\nSpec: odd/tasks/fourth.md, T1, T2\n\n## Allowed edit surfaces\n- lib/x.js`;
707
+ const branch = [
708
+ report('status: completed\nfiles_changed:\n - odd/tasks/third.md: the change\n'),
709
+ {
710
+ type: 'message',
711
+ id: 'assistant-subagent',
712
+ parentId: null,
713
+ message: {
714
+ role: 'assistant',
715
+ content: [{ type: 'toolCall', id: 'call-subagent', name: 'subagent_run', arguments: { task } }],
716
+ },
717
+ },
718
+ {
719
+ type: 'message',
720
+ id: 'result-subagent',
721
+ parentId: 'assistant-subagent',
722
+ message: { role: 'toolResult', toolCallId: 'call-subagent', toolName: 'subagent_run', isError: false, content: [] },
723
+ },
724
+ ];
725
+ const ctx = ctxFor(repo, [], { branchEntries: branch });
726
+ startSession(loaded, ctx);
727
+ const rows = await waitForRows(sectionOf(loaded), ctx, 2);
728
+ assert.deepEqual(rows.map((row) => row.label).sort(), ['fourth', 'third'],
729
+ 'the parent reads its own history: both the report and the task’s Spec line');
730
+ });
731
+
732
+ test('a RESUMED session restores its association from the branch (S3)', async () => {
733
+ const loaded = await load();
734
+ const repo = makeRepo();
735
+ const touch = {
736
+ type: 'custom',
737
+ id: 'touch',
738
+ parentId: null,
739
+ customType: 'odd-worktree.odd-touch',
740
+ data: { path: path.join(repo, 'odd', 'tasks', 'second.md'), at: 1000 },
741
+ };
742
+ const ctx = ctxFor(repo, [], { branchEntries: [touch] });
743
+ startSession(loaded, { ...ctx, sessionManager: { ...ctx.sessionManager, getEntries: () => [] } });
744
+ const rows = await waitForRows(sectionOf(loaded), ctx, 1);
745
+ assert.deepEqual(rows.map((row) => row.label), ['second'],
746
+ 'resuming reads the branch, not the append-only list');
747
+ });
748
+
749
+ test('a persisted subagent result refreshes the rail without any draw or TTL wait (S4)', async () => {
750
+ const loaded = await load();
751
+ const repo = makeRepo();
752
+ const branch = editHistory(repo, 'first');
753
+ const ctx = ctxFor(repo, [], { branchEntries: branch });
754
+ startSession(loaded, ctx);
755
+ const section = sectionOf(loaded);
756
+ const before = await waitForRows(section, ctx, 1);
757
+ assert.deepEqual(before.map((row) => row.label), ['first']);
758
+
759
+ // The subagent's ODD did not exist when the rail read: only a forced refresh
760
+ // can show it, so the rows below prove the message_end hook asked for one.
761
+ writeTask(repo, 'third', '# body of third\n');
762
+ const later = [
763
+ ...branch,
764
+ report('files_changed:\n - odd/tasks/third.md: done\n'),
765
+ ];
766
+ const laterCtx = ctxFor(repo, [], { branchEntries: later });
767
+ for (const [event, handler] of loaded.hooks) {
768
+ if (event !== 'message_end') continue;
769
+ await handler({ message: { role: 'custom', customType: 'gentle-agents.result', content: 'x' } }, laterCtx);
770
+ }
771
+ const rows = await waitForRows(section, ctx, 2, 3000);
772
+ assert.deepEqual(rows.map((row) => row.label), ['third', 'first'],
773
+ 'the report’s ODD appeared, which only a forced read can do');
774
+ });
775
+
776
+ test('a worktree registered only on an abandoned branch is out of scope (S5)', async () => {
777
+ const loaded = await load();
778
+ const repo = makeRepo();
779
+ const worktree = addWorktree(repo, 'wt-abandoned');
780
+ writeTask(worktree, 'abandoned-only', '# Never in this session’s scope\n');
781
+ const ctx = ctxFor(repo, [worktree], {
782
+ registeredInBranch: false,
783
+ branchEntries: editHistory(repo, 'first'),
784
+ });
785
+ startSession(loaded, ctx);
786
+ const rows = await waitForRows(sectionOf(loaded), ctx, 1);
787
+ assert.deepEqual(rows.map((row) => row.label), ['first']);
788
+ assert.equal(rows.some((row) => row.label === 'abandoned-only'), false,
789
+ 'a registration the session abandoned never keeps its documents in scope');
790
+ });
791
+ });
@@ -122,7 +122,11 @@ const presentGh = (run) => withGhPath('present', run);
122
122
 
123
123
  const handlersOf = (loaded, event) => loaded.hooks.filter(([name]) => name === event).map(([, handler]) => handler);
124
124
 
125
- function ctxOf(entries = [], calls = []) {
125
+ // T4 (odd-rail-session-scope S3): the association is read from the ACTIVE branch
126
+ // (`getBranch()`), so the double answers both: `entries` is the append-only list
127
+ // (`getEntries()`) and `branch` is the path the session is actually on. The default
128
+ // is the same list, which is what an unbranched session holds.
129
+ function ctxOf(entries = [], calls = [], branch = entries) {
126
130
  return {
127
131
  cwd: CWD,
128
132
  hasUI: false,
@@ -132,6 +136,10 @@ function ctxOf(entries = [], calls = []) {
132
136
  calls.push('getEntries');
133
137
  return entries;
134
138
  },
139
+ getBranch: () => {
140
+ calls.push('getBranch');
141
+ return branch;
142
+ },
135
143
  getSessionFile: () => undefined,
136
144
  getSessionId: () => 'session-1',
137
145
  getLeafId: () => null,
@@ -139,6 +147,47 @@ function ctxOf(entries = [], calls = []) {
139
147
  };
140
148
  }
141
149
 
150
+ // A manager that cannot answer the branch at all: the restore falls back to the
151
+ // append-only list rather than losing the session's own touches.
152
+ function ctxWithoutBranch(entries = []) {
153
+ const ctx = ctxOf(entries);
154
+ delete ctx.sessionManager.getBranch;
155
+ return ctx;
156
+ }
157
+
158
+ // The transcript entries a real session file holds (docs/session-format.md): an
159
+ // assistant message with a `write`/`edit` toolCall, and the toolResult that says
160
+ // whether it landed. A branch's write is associated by its NAME.
161
+ function callEntry(id, name, target) {
162
+ return {
163
+ type: 'message',
164
+ id: `call-${id}`,
165
+ parentId: null,
166
+ timestamp: new Date(1000).toISOString(),
167
+ message: { role: 'assistant', content: [{ type: 'toolCall', id, name, arguments: { path: target } }], timestamp: 1000 },
168
+ };
169
+ }
170
+ function resultEntry(id, isError = false, toolName = 'write') {
171
+ return {
172
+ type: 'message',
173
+ id: `result-${id}`,
174
+ parentId: `call-${id}`,
175
+ timestamp: new Date(1000).toISOString(),
176
+ message: { role: 'toolResult', toolCallId: id, toolName, isError, content: [], timestamp: 1000 },
177
+ };
178
+ }
179
+ const reportEntry = (content) => ({
180
+ type: 'custom_message',
181
+ id: 'report',
182
+ parentId: null,
183
+ timestamp: new Date(1000).toISOString(),
184
+ customType: 'gentle-agents.result',
185
+ content,
186
+ display: true,
187
+ });
188
+
189
+ const associated = (loaded) => [...loaded.mod.oddAssociated].sort();
190
+
142
191
  // The host's own delivery: every handler of that event, in registration order, one at
143
192
  // a time, awaited the way the host awaits them.
144
193
  async function fire(loaded, event, payload, ctx) {
@@ -417,3 +466,141 @@ describe('the module switch and the neighbouring observations', () => {
417
466
  });
418
467
  });
419
468
  });
469
+
470
+ // T4 (odd-rail-session-scope S2-S4): the association the rail filters by. It is
471
+ // rebuilt from the session's OWN ACTIVE branch — so an old session, written before
472
+ // the touch entry existed, and a resumed one both show their ODDs — and grown by the
473
+ // live touch record. A subagent's work reaches the parent as a persisted
474
+ // `gentle-agents.result`, which is the moment the parent re-reads its branch.
475
+ describe('the association rebuilt from the active branch (S3, S4)', () => {
476
+ test('session_start associates what an old session wrote or edited, with no touch entry at all', async () => {
477
+ await presentGh(async () => {
478
+ publishBridge(undefined);
479
+ const loaded = await loadModule();
480
+ assert.deepEqual(associated(loaded), [], 'nothing yet');
481
+ const history = [
482
+ callEntry('call-1', 'edit', path.join(TASKS, 'alpha.md')),
483
+ resultEntry('call-1', false, 'edit'),
484
+ callEntry('call-2', 'write', 'odd/tasks/beta.md'),
485
+ resultEntry('call-2', false, 'write'),
486
+ ];
487
+ await fire(loaded, 'session_start', { reason: 'resume' }, ctxOf(history));
488
+ assert.deepEqual(associated(loaded), ['alpha', 'beta'],
489
+ 'the branch history names them, which is what an old session has instead of an entry');
490
+ });
491
+ });
492
+
493
+ test('a FAILED call is not associated, and the association is not a restore of the record', async () => {
494
+ await presentGh(async () => {
495
+ publishBridge(undefined);
496
+ const loaded = await loadModule();
497
+ const history = [
498
+ callEntry('call-1', 'write', path.join(TASKS, 'alpha.md')),
499
+ resultEntry('call-1', true),
500
+ callEntry('call-2', 'write', path.join(TASKS, 'beta.md')),
501
+ ];
502
+ await fire(loaded, 'session_start', { reason: 'new' }, ctxOf(history));
503
+ assert.deepEqual(associated(loaded), [], 'a failed or unanswered write names nothing');
504
+ assert.deepEqual(touches(loaded), [], 'and the touch record still holds only persisted touches');
505
+ });
506
+ });
507
+
508
+ test('the abandoned branch is not read: only the active branch associates', async () => {
509
+ await presentGh(async () => {
510
+ publishBridge(undefined);
511
+ const loaded = await loadModule();
512
+ const abandoned = [
513
+ callEntry('call-1', 'write', path.join(TASKS, 'alpha.md')),
514
+ resultEntry('call-1'),
515
+ ];
516
+ // `getEntries()` still holds the abandoned path — that is the whole defect
517
+ // `getBranch()` fixes — and the active branch is a different one.
518
+ await fire(loaded, 'session_start', { reason: 'resume' }, ctxOf(abandoned, [], []));
519
+ assert.deepEqual(associated(loaded), [], 'an abandoned branch’s write is not this session’s work');
520
+ });
521
+ });
522
+
523
+ test('a live touch joins the association immediately', async () => {
524
+ await presentGh(async () => {
525
+ publishBridge(undefined);
526
+ const loaded = await loadModule();
527
+ await fire(loaded, 'session_start', { reason: 'new' }, ctxOf([]));
528
+ assert.deepEqual(associated(loaded), []);
529
+ await fire(loaded, 'tool_result', writeEvent('odd/tasks/gamma.md'), ctxOf([]));
530
+ assert.deepEqual(associated(loaded), ['gamma'], 'the row can show before anything is resumed');
531
+ await fire(loaded, 'session_start', { reason: 'resume' }, ctxOf([]));
532
+ assert.deepEqual(associated(loaded), [], 'and a session boundary replaces it, never merges it');
533
+ });
534
+ });
535
+
536
+ test('a persisted subagent report associates its files_changed paths', async () => {
537
+ await presentGh(async () => {
538
+ publishBridge(undefined);
539
+ const loaded = await loadModule();
540
+ await fire(loaded, 'session_start', { reason: 'new' }, ctxOf([]));
541
+
542
+ const report = [
543
+ 'status: completed',
544
+ 'files_changed:',
545
+ ` - ${path.join(TASKS, 'alpha.md')}: the change`,
546
+ ' - odd/tasks/beta.md: another change',
547
+ ].join('\n');
548
+ const branch = [reportEntry(report)];
549
+ await fire(
550
+ loaded,
551
+ 'message_end',
552
+ { message: { role: 'custom', customType: 'gentle-agents.result', content: report, display: true } },
553
+ ctxOf(branch),
554
+ );
555
+ assert.deepEqual(associated(loaded), ['alpha', 'beta'], 'the parent reads its own history, not the child');
556
+
557
+ // Somebody else's custom message is not a subagent report.
558
+ await fire(
559
+ loaded,
560
+ 'message_end',
561
+ { message: { role: 'custom', customType: 'some.other', content: 'files_changed:\n - odd/tasks/gamma.md: x\n', display: true } },
562
+ ctxOf([reportEntry(report), reportEntry('files_changed:\n - odd/tasks/gamma.md: x\n')]),
563
+ );
564
+ assert.deepEqual(associated(loaded), ['alpha', 'beta'], 'no association from a message that is not one');
565
+ });
566
+ });
567
+
568
+ test('a resumed session restores touches, reports and Spec lines alike', async () => {
569
+ await presentGh(async () => {
570
+ publishBridge(undefined);
571
+ const loaded = await loadModule();
572
+ const task = 'Do the work.\n\nSpec: odd/tasks/delta.md, T1\n';
573
+ const history = [
574
+ {
575
+ type: 'custom',
576
+ id: 'touch',
577
+ parentId: null,
578
+ timestamp: new Date(1000).toISOString(),
579
+ customType: ENTRY_TYPE,
580
+ data: { path: path.join(TASKS, 'alpha.md'), at: 1000 },
581
+ },
582
+ reportEntry('files_changed:\n - odd/tasks/beta.md: x\n'),
583
+ callEntry('call-1', 'subagent_run', 'odd/tasks/ignored.md'),
584
+ resultEntry('call-1', false, 'subagent_run'),
585
+ ];
586
+ // The Spec line travels in the task ARGUMENT, which callEntry does not carry.
587
+ history[2].message.content[0].arguments = { task };
588
+ await fire(loaded, 'session_start', { reason: 'resume' }, ctxOf(history));
589
+ assert.deepEqual(associated(loaded), ['alpha', 'beta', 'delta']);
590
+ });
591
+ });
592
+
593
+ test('a manager that cannot answer the branch keeps the append-only fallback', async () => {
594
+ await presentGh(async () => {
595
+ publishBridge(undefined);
596
+ const loaded = await loadModule();
597
+ const history = [
598
+ callEntry('call-1', 'edit', path.join(TASKS, 'alpha.md')),
599
+ resultEntry('call-1', false, 'edit'),
600
+ ];
601
+ await fire(loaded, 'session_start', { reason: 'resume' }, ctxWithoutBranch(history));
602
+ assert.deepEqual(associated(loaded), ['alpha'],
603
+ 'a host without `getBranch` restores what it can rather than nothing');
604
+ });
605
+ });
606
+ });