pi-odd-worktree 3.12.2 → 3.12.3

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.
@@ -33,6 +33,11 @@ const WT_B = path.resolve('/odd-rail-model/checkout-worktrees/wt-b');
33
33
  // WHICH copy was elected reads as one comparison.
34
34
  const picked = (rows) => rows.map((row) => [row.name, row.checkoutName, row.docPath]);
35
35
 
36
+ // T2 (odd-rail-session-scope S2): the association is the rail's FILTER, so every
37
+ // call names one. `all(...)` is "this session is associated with all of them",
38
+ // which is what the pre-association cases below mean by "draw the list".
39
+ const all = (...names) => new Set(names);
40
+
36
41
  function doc(name, root, mtimeMs) {
37
42
  return { name, docPath: path.join(root, 'odd', 'tasks', `${name}.md`), mtimeMs };
38
43
  }
@@ -57,7 +62,7 @@ describe('one row per ODD name (S12)', () => {
57
62
  mainGroup(['alpha', 'beta'], { alpha: 1000, beta: 2000 }),
58
63
  worktreeGroup('wt-a', WT_DIR, [doc('alpha', WT_DIR, 3000)]),
59
64
  ];
60
- const rows = railRows({ groups, touched: [] });
65
+ const rows = railRows({ groups, touched: [], associated: all(...groups.flatMap((g) => (g.docs || []).map((d) => d.name))) });
61
66
  assert.deepEqual(
62
67
  picked(rows),
63
68
  [
@@ -76,7 +81,7 @@ describe('one row per ODD name (S12)', () => {
76
81
  mainGroup(['alpha'], { alpha: 5000 }),
77
82
  worktreeGroup('wt-a', WT_DIR, [doc('alpha', WT_DIR, 5000)]),
78
83
  ];
79
- assert.deepEqual(picked(railRows({ groups, touched: [] })), [
84
+ assert.deepEqual(picked(railRows({ groups, touched: [], associated: all(...groups.flatMap((g) => (g.docs || []).map((d) => d.name))) })), [
80
85
  ['alpha', MAIN, path.join('/odd-rail-model/checkout/odd/tasks', 'alpha.md')],
81
86
  ]);
82
87
  });
@@ -87,7 +92,7 @@ describe('one row per ODD name (S12)', () => {
87
92
  worktreeGroup('wt-a', WT_DIR, [doc('alpha', WT_DIR, 7000)]),
88
93
  worktreeGroup('wt-b', WT_B, [doc('alpha', WT_B, 7000)]),
89
94
  ];
90
- assert.deepEqual(picked(railRows({ groups, touched: [] })), [
95
+ assert.deepEqual(picked(railRows({ groups, touched: [], associated: all(...groups.flatMap((g) => (g.docs || []).map((d) => d.name))) })), [
91
96
  ['alpha', 'wt-a', path.join(WT_DIR, 'odd', 'tasks', 'alpha.md')],
92
97
  ]);
93
98
  });
@@ -97,7 +102,7 @@ describe('one row per ODD name (S12)', () => {
97
102
  mainGroup(['alpha'], { alpha: 10 }),
98
103
  worktreeGroup('wt-a', WT_DIR, [doc('alpha', WT_DIR, 20)]),
99
104
  ];
100
- assert.deepEqual(picked(railRows({ groups, touched: [] })), [
105
+ assert.deepEqual(picked(railRows({ groups, touched: [], associated: all(...groups.flatMap((g) => (g.docs || []).map((d) => d.name))) })), [
101
106
  ['alpha', 'wt-a', path.join(WT_DIR, 'odd', 'tasks', 'alpha.md')],
102
107
  ]);
103
108
  });
@@ -107,7 +112,7 @@ describe('one row per ODD name (S12)', () => {
107
112
  mainGroup(['alpha', 'beta'], { alpha: 200, beta: 100 }),
108
113
  worktreeGroup('wt-a', WT_DIR, []),
109
114
  ];
110
- const rows = railRows({ groups, touched: [] });
115
+ const rows = railRows({ groups, touched: [], associated: all(...groups.flatMap((g) => (g.docs || []).map((d) => d.name))) });
111
116
  assert.deepEqual(rows.map((row) => [row.name, row.checkoutName]), [['alpha', MAIN], ['beta', MAIN]],
112
117
  'main only, and no row for the empty worktree');
113
118
  });
@@ -120,20 +125,20 @@ describe('order: attribution first, then recency, then name (S9)', () => {
120
125
  { path: path.join(DOC_DIR, 'gamma.md'), name: 'gamma', at: 20 },
121
126
  { path: path.join(DOC_DIR, 'beta.md'), name: 'beta', at: 10 },
122
127
  ];
123
- assert.deepEqual(railRows({ groups, touched }).map((row) => row.name), ['gamma', 'beta', 'alpha'],
128
+ assert.deepEqual(railRows({ groups, touched, associated: all(...groups.flatMap((g) => (g.docs || []).map((d) => d.name))) }).map((row) => row.name), ['gamma', 'beta', 'alpha'],
124
129
  'the MOST recent touch is first, then the older one, then plain recency');
125
130
  });
126
131
 
127
132
  test('a touched name no group lists produces no row, and never a duplicate (S9)', () => {
128
133
  const groups = [mainGroup(['alpha'], { alpha: 1 })];
129
134
  const touched = [{ path: path.join(DOC_DIR, 'ghost.md'), name: 'ghost', at: 99 }];
130
- assert.deepEqual(railRows({ groups, touched }).map((row) => row.name), ['alpha'],
135
+ assert.deepEqual(railRows({ groups, touched, associated: all(...groups.flatMap((g) => (g.docs || []).map((d) => d.name))) }).map((row) => row.name), ['alpha'],
131
136
  'attribution only orders: it adds nothing and removes nothing');
132
137
  });
133
138
 
134
139
  test('equal mtimes order by name in code units, never locale', () => {
135
140
  const groups = [mainGroup(['a', 'B', 'b'], { a: 1, B: 1, b: 1 })];
136
- assert.deepEqual(railRows({ groups, touched: [] }).map((row) => row.name), ['B', 'a', 'b'],
141
+ assert.deepEqual(railRows({ groups, touched: [], associated: all(...groups.flatMap((g) => (g.docs || []).map((d) => d.name))) }).map((row) => row.name), ['B', 'a', 'b'],
137
142
  'code units put B (66) before a (97); localeCompare would not');
138
143
  });
139
144
 
@@ -143,7 +148,7 @@ describe('order: attribution first, then recency, then name (S9)', () => {
143
148
  { path: path.join(DOC_DIR, 'beta.md'), name: 'beta', at: 50 },
144
149
  { path: path.join(DOC_DIR, 'alpha.md'), name: 'alpha', at: 50 },
145
150
  ];
146
- assert.deepEqual(railRows({ groups, touched }).map((row) => row.name), ['beta', 'alpha']);
151
+ assert.deepEqual(railRows({ groups, touched, associated: all(...groups.flatMap((g) => (g.docs || []).map((d) => d.name))) }).map((row) => row.name), ['beta', 'alpha']);
147
152
  });
148
153
  });
149
154
 
@@ -151,14 +156,15 @@ describe('malformed input is skipped, never thrown on', () => {
151
156
  test('garbage groups, documents and touches answer an empty or partial list', () => {
152
157
  assert.deepEqual(railRows({}), []);
153
158
  assert.deepEqual(railRows(), []);
154
- assert.deepEqual(railRows({ groups: 'nope', touched: 'nope' }), []);
155
- assert.deepEqual(railRows({ groups: [null, 7, {}, { docs: 'nope' }], touched: [null, 5] }), []);
159
+ assert.deepEqual(railRows({ groups: 'nope', touched: 'nope', associated: all('alpha') }), []);
160
+ assert.deepEqual(railRows({ groups: [null, 7, {}, { docs: 'nope' }], touched: [null, 5], associated: all('alpha') }), []);
156
161
  const rows = railRows({
157
162
  groups: [
158
163
  { docs: [{ name: 'alpha', docPath: '/x/alpha.md', mtimeMs: 4 }, null, { name: 5 }, { docPath: '/y/z.md' }] },
159
164
  { docs: [{ name: 'beta', docPath: '/x/beta.md', mtimeMs: 'nonsense' }] },
160
165
  ],
161
166
  touched: [{ name: 'alpha' }, null, { at: 3 }, { name: '', at: 1 }],
167
+ associated: all('alpha', 'beta'),
162
168
  });
163
169
  assert.deepEqual(rows.map((row) => [row.name, row.mtimeMs]), [['alpha', 4], ['beta', 0]],
164
170
  'a document with no usable time sorts oldest and reports 0; a touch with no instant is ignored');
@@ -169,7 +175,7 @@ describe('malformed input is skipped, never thrown on', () => {
169
175
  const touched = [{ path: path.join(DOC_DIR, 'beta.md'), name: 'beta', at: 9 }];
170
176
  const groupsBefore = JSON.stringify(groups);
171
177
  const touchedBefore = JSON.stringify(touched);
172
- railRows({ groups, touched });
178
+ railRows({ groups, touched, associated: all(...groups.flatMap((g) => (g.docs || []).map((d) => d.name))) });
173
179
  assert.equal(JSON.stringify(groups), groupsBefore);
174
180
  assert.equal(JSON.stringify(touched), touchedBefore);
175
181
  });
@@ -182,3 +188,61 @@ describe('malformed input is skipped, never thrown on', () => {
182
188
  assert.deepEqual(Object.keys(require('../lib/odd-rail-model.js')), ['railRows']);
183
189
  });
184
190
  });
191
+
192
+ // T2 (odd-rail-session-scope S2): the association is the FILTER. The rail lists
193
+ // only the ODDs the session is associated with — never "everything on disk" — and
194
+ // the live touch record is unioned in, so a document this session just wrote shows
195
+ // even before the history read sees it.
196
+ describe('only the session’s association draws (S2)', () => {
197
+ test('the associated names are drawn, and every other document on disk is not', () => {
198
+ const groups = [mainGroup(['alpha', 'beta', 'gamma'], { alpha: 900, beta: 800, gamma: 700 })];
199
+ assert.deepEqual(
200
+ railRows({ groups, touched: [], associated: all('beta') }).map((row) => row.name),
201
+ ['beta'],
202
+ 'a fresh session associated with one ODD draws one row, not the four the disk holds',
203
+ );
204
+ assert.deepEqual(
205
+ railRows({ groups, touched: [], associated: all('gamma', 'alpha') }).map((row) => row.name),
206
+ ['alpha', 'gamma'],
207
+ 'the election and the recency order are unchanged inside the association',
208
+ );
209
+ });
210
+
211
+ test('an empty association draws nothing at all', () => {
212
+ const groups = [mainGroup(['alpha', 'beta'], { alpha: 2, beta: 1 })];
213
+ assert.deepEqual(railRows({ groups, touched: [], associated: new Set() }), []);
214
+ assert.deepEqual(railRows({ groups, associated: new Set() }), []);
215
+ });
216
+
217
+ test('a live touch is unioned in, and a touched name with no document stays hidden', () => {
218
+ const groups = [mainGroup(['alpha', 'beta', 'gamma'], { alpha: 900, beta: 800, gamma: 700 })];
219
+ const touched = [
220
+ { path: path.join(DOC_DIR, 'gamma.md'), name: 'gamma', at: 30 },
221
+ { path: path.join(DOC_DIR, 'ghost.md'), name: 'ghost', at: 20 },
222
+ ];
223
+ assert.deepEqual(
224
+ railRows({ groups, touched, associated: all('beta') }).map((row) => row.name),
225
+ ['gamma', 'beta'],
226
+ 'the touched document draws (first, by its touch) although history does not name it yet',
227
+ );
228
+ assert.equal(
229
+ railRows({ groups, touched, associated: all('beta') }).some((row) => row.name === 'ghost'),
230
+ false,
231
+ 'a touched name no group lists produces nothing, exactly as before',
232
+ );
233
+ // A touch never ADDS a name outside the election to the association: it is one
234
+ // of the names already elected by the grouped model, not a row of its own.
235
+ assert.equal(railRows({ groups, touched, associated: all() }).length, 1);
236
+ });
237
+
238
+ test('a garbage association draws nothing instead of throwing', () => {
239
+ const groups = [mainGroup(['alpha'], { alpha: 1 })];
240
+ for (const associated of [undefined, null, 'alpha', 42, {}, [null, 5]]) {
241
+ assert.deepEqual(railRows({ groups, touched: [], associated }), [], String(associated));
242
+ }
243
+ // The two accepted shapes, and a malformed member inside one of them.
244
+ assert.deepEqual(railRows({ groups, touched: [], associated: new Set(['alpha']) }).map((row) => row.name), ['alpha']);
245
+ assert.deepEqual(railRows({ groups, touched: [], associated: ['alpha', null, 7] }).map((row) => row.name), ['alpha']);
246
+ assert.deepEqual(railRows({ groups, touched: [], associated: new Set([null, '']) }), []);
247
+ });
248
+ });
@@ -522,3 +522,43 @@ test('the refresh timer is unref\'d, reads once per floor, and stops itself outs
522
522
  disposed.dispose();
523
523
  assert.equal(disposeTimers.active().length, 0, 'dispose clears the floor timer');
524
524
  });
525
+
526
+ // ---------------------------------------------------------------------------
527
+ // S1 (odd-rail-session-scope): an empty body says so.
528
+ // ---------------------------------------------------------------------------
529
+
530
+ test('S1: a ready list with no associated ODD draws ONE dim `no ODD associated` row', async () => {
531
+ const opened = [];
532
+ const sched = manualSchedule();
533
+ const handle = createOddRailSection({
534
+ load: async () => [],
535
+ open: (target) => { opened.push(target); },
536
+ schedule: sched.schedule,
537
+ });
538
+ await handle.refresh();
539
+
540
+ const rows = handle.section.lines({});
541
+ // Exactly the `EMPTY_ROW` of lib/issues-section.js: one dim line, a copy per
542
+ // frame, and no click — there is nothing behind it to open.
543
+ assert.deepEqual(rows, [{ label: 'no ODD associated', tone: 'dim' }]);
544
+ assert.equal('onClick' in rows[0], false, 'the empty line is inert, like the issues one');
545
+ assert.deepEqual(opened, [], 'drawing the empty line opens nothing');
546
+ });
547
+
548
+ test('S1: the empty line is a state, not a failure: another frame inside the TTL keeps it, and a later list replaces it', async () => {
549
+ let items = [];
550
+ const handle = createOddRailSection({
551
+ load: async () => items,
552
+ open: () => {},
553
+ schedule: () => {},
554
+ });
555
+ await handle.refresh();
556
+ assert.deepEqual(handle.section.lines({}), [{ label: 'no ODD associated', tone: 'dim' }]);
557
+ // A second frame reads the same snapshot: no read, the same one line.
558
+ assert.deepEqual(handle.section.lines({}), [{ label: 'no ODD associated', tone: 'dim' }]);
559
+
560
+ items = FOUR;
561
+ await handle.refresh();
562
+ assert.deepEqual(handle.section.lines({}).map((row) => row.label), ['alpha', 'beta', 'gamma', 'delta'],
563
+ 'a list replaces the empty line, and its rows keep their own shape');
564
+ });
@@ -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
+ });