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.
@@ -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
+ });
@@ -280,3 +280,194 @@ describe('restore (S8/S9)', () => {
280
280
  ]);
281
281
  });
282
282
  });
283
+
284
+ // T1 (odd-rail-session-scope S3, S4): the association rebuilt from the session's
285
+ // OWN history, the source that covers OLD sessions (written before the touch entry
286
+ // existed) and RESUMED ones. The caller hands over the ACTIVE branch only
287
+ // (`ctx.sessionManager.getBranch()`), so an abandoned branch's write is simply not
288
+ // an entry this function ever sees.
289
+ //
290
+ // What the history names, all by ODD NAME (`odd/tasks/<name>.md`):
291
+ // - an assistant `write`/`edit` toolCall whose matching `toolResult` (`toolCallId`)
292
+ // is not an error;
293
+ // - the persisted `odd-worktree.odd-touch` entries;
294
+ // - a `gentle-agents.result` custom_message whose report lists the path in its
295
+ // `files_changed:` block, and the `Spec: odd/tasks/<name>.md` line of a
296
+ // successful `subagent_run`/`subagent_continue` task;
297
+ // - nothing else, and every malformed entry is SKIPPED instead of thrown on.
298
+ const { associatedFromHistory } = require('../lib/odd-session.js');
299
+
300
+ const assistant = (id, blocks) => ({
301
+ type: 'message',
302
+ id,
303
+ parentId: null,
304
+ message: { role: 'assistant', content: blocks, timestamp: 1 },
305
+ });
306
+ const toolCall = (id, name, args) => ({ type: 'toolCall', id, name, arguments: args });
307
+ const result = (id, toolCallId, isError = false, toolName = 'write') => ({
308
+ type: 'message',
309
+ id,
310
+ parentId: null,
311
+ message: { role: 'toolResult', toolCallId, toolName, isError, content: [], timestamp: 1 },
312
+ });
313
+ const touchEntry = (id, file, at = 1000) => ({
314
+ type: 'custom',
315
+ id,
316
+ parentId: null,
317
+ customType: ENTRY_TYPE,
318
+ data: { path: abs(`odd/tasks/${file}.md`), at },
319
+ });
320
+ const resultMessage = (content, customType = 'gentle-agents.result') => ({
321
+ type: 'custom_message',
322
+ id: 'r',
323
+ parentId: null,
324
+ customType,
325
+ content,
326
+ display: true,
327
+ });
328
+ const names = (set) => [...set].sort();
329
+
330
+ describe('the association rebuilt from history (S3, S4)', () => {
331
+ test('a successful write or edit on an odd/tasks document is associated by name', () => {
332
+ const associated = associatedFromHistory([
333
+ assistant('a1', [toolCall('call-1', 'write', { path: 'odd/tasks/alpha.md', content: '# alpha' })]),
334
+ result('r1', 'call-1', false, 'write'),
335
+ assistant('a2', [toolCall('call-2', 'edit', { path: abs('odd/tasks/beta.md') })]),
336
+ result('r2', 'call-2', false, 'edit'),
337
+ ]);
338
+ assert.deepEqual(names(associated), ['alpha', 'beta']);
339
+ });
340
+
341
+ test('a failed or unanswered call is not associated, and the result is matched by toolCallId', () => {
342
+ const associated = associatedFromHistory([
343
+ assistant('a1', [toolCall('call-1', 'write', { path: 'odd/tasks/alpha.md' })]),
344
+ result('r1', 'call-1', true),
345
+ // A successful result for ANOTHER call never vouches for this one.
346
+ assistant('a2', [toolCall('call-2', 'write', { path: 'odd/tasks/beta.md' })]),
347
+ result('r2', 'call-9', false),
348
+ // ... and a call whose result never landed (an in-flight or aborted turn).
349
+ assistant('a3', [toolCall('call-3', 'edit', { path: 'odd/tasks/gamma.md' })]),
350
+ assistant('a4', [toolCall('call-4', 'read', { path: 'odd/tasks/delta.md' })]),
351
+ result('r4', 'call-4', false, 'read'),
352
+ ]);
353
+ assert.deepEqual(names(associated), []);
354
+ });
355
+
356
+ test('a path with `..` or one that is not exactly odd/tasks/<name>.md is ignored', () => {
357
+ const paths = [
358
+ 'docs/alpha.md',
359
+ 'odd/alpha.md',
360
+ 'odd/tasks/nested/alpha.md',
361
+ 'odd/tasks/.alpha.md',
362
+ 'odd/tasks/alpha.txt',
363
+ 'odd/tasks/alpha.md.bak',
364
+ 'odd/tasks/../tasks/alpha.md',
365
+ '/repo/odd/../odd/tasks/alpha.md',
366
+ 'odd/tasks',
367
+ '',
368
+ undefined,
369
+ 42,
370
+ ];
371
+ const entries = [];
372
+ paths.forEach((target, index) => {
373
+ entries.push(assistant(`a${index}`, [toolCall(`call-${index}`, 'write', { path: target })]));
374
+ entries.push(result(`r${index}`, `call-${index}`));
375
+ });
376
+ assert.deepEqual(names(associatedFromHistory(entries)), []);
377
+ });
378
+
379
+ test('the persisted odd-touch entries are associated, whether or not a tool call is still in history', () => {
380
+ const associated = associatedFromHistory([
381
+ { type: 'custom', id: 'x', customType: 'odd-worktree.other', data: { path: abs('odd/tasks/nope.md'), at: 1 } },
382
+ touchEntry('a', 'alpha'),
383
+ touchEntry('b', 'beta'),
384
+ // The same document by its other spelling is the same NAME.
385
+ touchEntry('c', 'alpha', 2000),
386
+ ]);
387
+ assert.deepEqual(names(associated), ['alpha', 'beta']);
388
+ });
389
+
390
+ test('a gentle-agents.result report associates every odd/tasks document under `files_changed:`', () => {
391
+ const content = [
392
+ 'status: completed',
393
+ 'summary: did the work',
394
+ 'files_changed:',
395
+ ` - ${abs('odd/tasks/alpha.md')}: the change`,
396
+ ' - odd/tasks/beta.md: another change',
397
+ ' - lib/odd-session.js: not a document',
398
+ '',
399
+ 'validation:',
400
+ ' - npm test: pass',
401
+ ].join('\n');
402
+ assert.deepEqual(names(associatedFromHistory([resultMessage(content)])), ['alpha', 'beta']);
403
+ // The content may arrive as text blocks, exactly as Pi stores a custom message.
404
+ assert.deepEqual(
405
+ names(associatedFromHistory([resultMessage([{ type: 'text', text: content }])])),
406
+ ['alpha', 'beta'],
407
+ );
408
+ // A custom_message under another customType is somebody else's report.
409
+ assert.deepEqual(names(associatedFromHistory([resultMessage(content, 'someone.else')])), []);
410
+ });
411
+
412
+ test('a successful subagent task naming a Spec document is associated', () => {
413
+ const task = 'Do the work.\n\nSpec: odd/tasks/gamma.md, T1, T2\n\n## Allowed edit surfaces\n- lib/odd-session.js';
414
+ const associated = associatedFromHistory([
415
+ assistant('a1', [toolCall('call-1', 'subagent_run', { task })]),
416
+ result('r1', 'call-1', false, 'subagent_run'),
417
+ assistant('a2', [toolCall('call-2', 'subagent_continue', { task: 'Spec: odd/tasks/delta.md, T3' })]),
418
+ result('r2', 'call-2', false, 'subagent_continue'),
419
+ // A failed subagent did no work on its Spec.
420
+ assistant('a3', [toolCall('call-3', 'subagent_run', { task: 'Spec: odd/tasks/epsilon.md, T4' })]),
421
+ result('r3', 'call-3', true, 'subagent_run'),
422
+ // A Spec line that names no document is no association.
423
+ assistant('a4', [toolCall('call-4', 'subagent_run', { task: 'Spec: odd/tasks/../docs/zeta.md, T5' })]),
424
+ result('r4', 'call-4', false, 'subagent_run'),
425
+ ]);
426
+ assert.deepEqual(names(associated), ['delta', 'gamma']);
427
+ });
428
+
429
+ test('a quoted example document is a placeholder, never an ODD', () => {
430
+ // Prose that shows the rule reaches a transcript as an ordinary path: the
431
+ // harness quotes `Spec: odd/tasks/<existing>.md, T<n>`. Electing it would name
432
+ // an ODD that cannot exist.
433
+ const associated = associatedFromHistory([
434
+ assistant('a1', [
435
+ toolCall('c1', 'subagent_run', { task: 'Do it.\n\nSpec: odd/tasks/<existing>.md, T2, S1' }),
436
+ toolCall('c2', 'subagent_run', { task: 'Do it.\n\nSpec: odd/tasks/real-one.md, T2, S1' }),
437
+ ]),
438
+ result('r1', 'c1', false, 'subagent_run'),
439
+ result('r2', 'c2', false, 'subagent_run'),
440
+ resultMessage('files_changed:\n - odd/tasks/<name>.md: what changed'),
441
+ ]);
442
+ assert.deepEqual(names(associated), ['real-one']);
443
+ // A space is a real name, not a placeholder: the rail must still list it.
444
+ assert.deepEqual(
445
+ names(associatedFromHistory([assistant('a2', [toolCall('c3', 'write', { path: 'odd/tasks/with space.md' })]), result('r3', 'c3')])),
446
+ ['with space'],
447
+ );
448
+ });
449
+
450
+ test('garbage entries, garbage blocks and garbage content are skipped instead of thrown on', () => {
451
+ const associated = associatedFromHistory([
452
+ null,
453
+ undefined,
454
+ 'odd/tasks/alpha.md',
455
+ 42,
456
+ {},
457
+ { type: 'message' },
458
+ { type: 'message', message: null },
459
+ { type: 'message', message: { role: 'assistant', content: 'nope' } },
460
+ { type: 'message', message: { role: 'assistant', content: [null, 7, {}, { type: 'text' }, toolCall('c', 'write')] } },
461
+ { type: 'message', message: { role: 'toolResult' } },
462
+ { type: 'custom', customType: ENTRY_TYPE, data: null },
463
+ { type: 'custom', customType: ENTRY_TYPE, data: { path: 42, at: 1 } },
464
+ { type: 'custom_message', customType: 'gentle-agents.result', content: 42 },
465
+ { type: 'custom_message', customType: 'gentle-agents.result', content: [null, { type: 'text', text: null }] },
466
+ touchEntry('a', 'alpha'),
467
+ ]);
468
+ assert.deepEqual(names(associated), ['alpha']);
469
+ for (const answer of [undefined, null, 'entries', 42, {}]) {
470
+ assert.deepEqual(names(associatedFromHistory(answer)), []);
471
+ }
472
+ });
473
+ });