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.
@@ -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
+ });
@@ -697,6 +697,26 @@ describe('the tool_call handler the extension registers', () => {
697
697
  }
698
698
  });
699
699
 
700
+ test('a subagent child is judged by neither worktree guard: they are the orchestrator\'s actions', async () => {
701
+ const before = process.env.GENTLE_PI_AGENTS_CHILD;
702
+ try {
703
+ process.env.GENTLE_PI_AGENTS_CHILD = '1';
704
+ assert.equal(await callTool(loaded.hooks, bash('git worktree add ../odd-b'), principal), undefined, 'worktree add');
705
+ assert.equal(await callTool(loaded.hooks, bash('git checkout -b feat/odd-b'), principal), undefined, 'branch switch');
706
+ const file = path.join(principal, 'src', 'a.js');
707
+ assert.equal(await callTool(loaded.hooks, { toolName: 'write', input: { path: file, content: 'x' } }, principal), undefined, 'write');
708
+ assert.equal(await callTool(loaded.hooks, { toolName: 'edit', input: { path: file, oldText: 'a', newText: 'b' } }, principal), undefined, 'edit');
709
+ } finally {
710
+ if (before === undefined) delete process.env.GENTLE_PI_AGENTS_CHILD;
711
+ else process.env.GENTLE_PI_AGENTS_CHILD = before;
712
+ }
713
+ assert.deepEqual(
714
+ await callTool(loaded.hooks, bash('git worktree add ../odd-b'), principal),
715
+ { block: true, reason: WORKTREE_ADD_REFUSAL },
716
+ 'the primary session is still judged',
717
+ );
718
+ });
719
+
700
720
  test('refuses the switches that leave the principal checkout\'s default branch', async () => {
701
721
  const expected = [
702
722
  ['git checkout -b feat/odd-b', BRANCH_SWITCH_REFUSAL],