pi-odd-worktree 3.1.0 → 3.2.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.
@@ -334,8 +334,8 @@ test('the work a synchronous handler starts still runs after it has answered the
334
334
  // session's tools come from `before_agent_start` (the only source), the checkouts from one cached fake
335
335
  // git toplevel, and the texts are the approved ones, written out here.
336
336
 
337
- const NATIVE_REMINDER = 'Follow the CodeGraph rule: run `codegraph` `query` or `explore` first, if it applies.';
338
- const MCP_REMINDER = 'Follow the CodeGraph rule: call `mcp__codegraph__codegraph_explore` first, if it applies.';
337
+ const NATIVE_REMINDER = 'Follow the CodeGraph rule: run `codegraph` `query` or `explore` first.';
338
+ const MCP_REMINDER = 'Follow the CodeGraph rule: call `mcp__codegraph__codegraph_explore` first.';
339
339
  const anotherReminder = (root) => `This call went to ${root}, another checkout. Follow the CodeGraph rule: call \`mcp__codegraph__codegraph_explore\` with \`projectPath\` set to ${root} first.`;
340
340
 
341
341
  const SESSION_TOOLS = { systemPromptOptions: { selectedTools: ['read', 'grep', 'bash', 'codegraph'] } };
@@ -417,6 +417,21 @@ test('a new session start re-arms it, and a /reload is not a new session', async
417
417
  assert.ok(await run('tool_result', readEvent('src/a.ts')), 'a new session forgets it');
418
418
  });
419
419
 
420
+ test('a new request re-arms the one reminder of the checkout (S3, S7)', async () => {
421
+ const { run } = setup({ toplevel: checkoutMap() });
422
+ await run('before_agent_start', SESSION_TOOLS);
423
+ const texts = [];
424
+ for (let call = 0; call < 17; call += 1) {
425
+ const patch = await run('tool_result', readEvent('src/a.ts'));
426
+ texts.push(patch === undefined ? undefined : patch.content[1].text);
427
+ }
428
+ assert.deepEqual(texts.filter((text) => text !== undefined), [NATIVE_REMINDER], '17 searches of one request hear it once');
429
+ assert.equal(texts[0], NATIVE_REMINDER, 'and on the first of them, not at 8, 9 or 17');
430
+ await run('before_agent_start', SESSION_TOOLS);
431
+ const next = await run('tool_result', readEvent('src/a.ts'));
432
+ assert.equal(next.content[1].text, NATIVE_REMINDER, 'the first search of the next request hears it again');
433
+ });
434
+
420
435
  test('a worker child is told to call the MCP tool, and a review agent is told nothing', async () => {
421
436
  const worker = setup({
422
437
  env: { GENTLE_PI_AGENTS_CHILD: '1', GENTLE_PI_AGENTS_REQUESTED_TOOLS: 'read,bash,mcp__codegraph__codegraph_explore' },
@@ -447,7 +462,7 @@ test('a git read that throws never fails the tool result', async () => {
447
462
  });
448
463
 
449
464
  test('the guard is one injected collaborator, driven by the host events', async () => {
450
- const seen = { tools: [], results: [], compacted: 0, sessions: 0 };
465
+ const seen = { tools: [], results: [], compacted: 0, sessions: 0, requests: 0 };
451
466
  const { run } = setup({
452
467
  impl: {
453
468
  createCodegraphGuard: (options) => {
@@ -464,6 +479,9 @@ test('the guard is one injected collaborator, driven by the host events', async
464
479
  onNewSession: () => {
465
480
  seen.sessions += 1;
466
481
  },
482
+ onNewRequest: () => {
483
+ seen.requests += 1;
484
+ },
467
485
  };
468
486
  },
469
487
  },
@@ -476,10 +494,34 @@ test('the guard is one injected collaborator, driven by the host events', async
476
494
  assert.deepEqual(seen.tools, [SESSION_TOOLS.systemPromptOptions.selectedTools]);
477
495
  assert.equal(seen.compacted, 1);
478
496
  assert.equal(seen.sessions, 2, 'the startup and the new session both reset');
497
+ assert.equal(seen.requests, 1, 'one new request, no more');
479
498
  assert.deepEqual(seen.results, ['grep']);
480
499
  assert.deepEqual(patch.content, [{ type: 'text', text: 'REMINDER' }], 'the reminder rides the result as one more part');
481
500
  });
482
501
 
502
+ test('S7: every new request re-arms guard 2, and a finished call does not', async () => {
503
+ let requests = 0;
504
+ const { run } = setup({
505
+ impl: {
506
+ createCodegraphGuard: () => ({
507
+ observeTools: () => {},
508
+ onToolResult: async () => undefined,
509
+ onCompacted: () => {},
510
+ onNewSession: () => {},
511
+ onNewRequest: () => {
512
+ requests += 1;
513
+ },
514
+ }),
515
+ },
516
+ });
517
+ await run('tool_result', { toolName: 'grep', input: { pattern: 'x' }, isError: false, content: [] });
518
+ assert.equal(requests, 0, 'a finished tool call is no new request');
519
+ await run('before_agent_start', SESSION_TOOLS);
520
+ assert.equal(requests, 1, 'the prompt of a new request re-arms it');
521
+ await run('before_agent_start', SESSION_TOOLS);
522
+ assert.equal(requests, 2, 'each new request, not each session');
523
+ });
524
+
483
525
  // ---- S2: the bounded join on the pre-execution hook (T6) ----
484
526
  //
485
527
  // Before a CodeGraph search runs, the index work this process already has in flight for that checkout is
@@ -443,7 +443,7 @@ describe('a host that draws no frame gets the whole pane (S1)', () => {
443
443
  });
444
444
 
445
445
  describe('the edit diff, recoloured (S1)', () => {
446
- test('additions, deletions, hunk headers and context wear their roles, text untouched', () => {
446
+ test('a deletion and an addition wear their whole role, a hunk header and context their own', () => {
447
447
  const { seam } = seamFor();
448
448
  const rows = diffColorDesign(seam).apply(editCard(), {
449
449
  tool: 'edit',
@@ -455,13 +455,14 @@ describe('the edit diff, recoloured (S1)', () => {
455
455
  assert.equal(rows.length, 6, 'every row of the host card is still there');
456
456
  assert.equal(unpaint(rows[1]).includes('@@ -12,2 +12,2 @@'), true);
457
457
  assert.match(rows[1], /<muted>@@ -12,2 \+12,2 @@<\/muted>/u, 'a hunk header is muted');
458
- assert.match(rows[2], /<toolDiffRemoved>-12 <\/toolDiffRemoved>/u, 'a deletion is toolDiffRemoved');
459
- assert.match(rows[3], /<toolDiffAdded>\+12 <\/toolDiffAdded>/u, 'an addition is toolDiffAdded');
460
- // The code of a row is the host's highlighter, with the language the path names.
461
- assert.ok(rows[2].includes(HIGHLIGHT('const total = 1')), rows[2]);
462
- assert.ok(rows[3].includes(HIGHLIGHT('const total = 2')), rows[3]);
463
- // Context is the theme's plain text.
464
- assert.match(rows[4], /<muted>13 <\/muted>/u);
458
+ // S13: the whole row — marker, number, divider and code — is the ONE role, as `git diff` paints it.
459
+ assert.match(rows[2], /<toolDiffRemoved>-12 │ const total = 1<\/toolDiffRemoved>/u, 'a deletion is toolDiffRemoved whole');
460
+ assert.match(rows[3], /<toolDiffAdded>\+12 │ const total = 2<\/toolDiffAdded>/u, 'an addition is toolDiffAdded whole');
461
+ // A changed row carries no syntax colour of its own: the role's colour is the whole point.
462
+ assert.equal(rows[2].includes(HIGHLIGHT('const total = 1')), false, rows[2]);
463
+ assert.equal(rows[3].includes(HIGHLIGHT('const total = 2')), false, rows[3]);
464
+ // Context keeps today's drawing: the gutter, the divider and the host's own highlighting.
465
+ assert.match(rows[4], /<muted>13<\/muted> <dim>│<\/dim> /u);
465
466
  assert.ok(rows[4].includes(HIGHLIGHT('return total')), rows[4]);
466
467
  // The frame and the row widths are the host's, byte for byte.
467
468
  assert.equal(rows[0], editCard()[0]);
@@ -479,7 +480,9 @@ describe('the edit diff, recoloured (S1)', () => {
479
480
  const out = diffColorDesign(seam).apply(rows, { tool: 'edit', part: 'call', width: WIDTH, theme, render: { args: { path: 'lib/app.js' } } });
480
481
  assert.deepEqual(heard, ['lib/app.js']);
481
482
  assert.equal(out.length, rows.length);
482
- assert.ok(out[2].includes(HIGHLIGHT('const total = 1')));
483
+ // The highlighter is what a CONTEXT row's code is drawn with; a changed row is its role alone.
484
+ assert.ok(out[4].includes(HIGHLIGHT('return total')), out[4]);
485
+ assert.equal(out[2].includes(HIGHLIGHT('const total = 1')), false, out[2]);
483
486
  });
484
487
 
485
488
  test('a row already wearing this paint comes back identical: never painted twice', () => {
@@ -508,53 +511,173 @@ describe('the edit diff, recoloured (S1)', () => {
508
511
  });
509
512
  });
510
513
 
514
+ describe('the edit row carries the write pane\'s gutter divider (S12)', () => {
515
+ const ctx = () => ({ tool: 'edit', part: 'call', width: WIDTH, theme, render: { args: { path: 'lib/app.js' } } });
516
+
517
+ test('the divider is the write pane\'s own, in the theme role that pane paints it', () => {
518
+ const write = diffColorDesign(seamFor().seam).apply(writeHalves().result, resultCtx({ render: { args: WRITE_ARGS } }));
519
+ assert.ok(write[0].includes('<muted> 1</muted> <dim>│</dim> '), `the write pane divides its gutter with the dim role: ${write[0]}`);
520
+ const rows = diffColorDesign(seamFor().seam).apply(editCard(), ctx());
521
+ assert.ok(rows[4].includes('<muted>13</muted> <dim>│</dim> '), `the edit draws the same one: ${rows[4]}`);
522
+ assert.match(unpaint(rows[4]), /^│ +13 │ return total +│$/u, unpaint(rows[4]));
523
+ // A changed row is divided too, and its divider is part of the row's ONE role, not its own paint.
524
+ assert.ok(rows[2].includes('<toolDiffRemoved>-12 │ const total = 1</toolDiffRemoved>'), rows[2]);
525
+ assert.equal(rows[2].includes('<dim>'), false, 'a changed row has no colour of its own but its role');
526
+ });
527
+
528
+ test('the divider is measured as PLAIN text, so the row keeps the width the host gave it', () => {
529
+ // A row whose code already fills it: the two columns the divider needs come out of the CODE.
530
+ const code = 'for (const item of items) sum += item.price;';
531
+ const tight = `│ -12 ${code} │`;
532
+ const rows = diffColorDesign(seamFor().seam).apply([tight], ctx());
533
+ assert.equal(cols(rows[0]), cols(tight), 'the row keeps the width the host gave it');
534
+ const plain = unpaint(rows[0]);
535
+ assert.equal((plain.match(/│/gu) ?? []).length, 3, `two frame cells and ONE divider: ${plain}`);
536
+ assert.ok(plain.startsWith('│ -12 │ for (const item of items) sum += item.pric'), plain);
537
+ assert.ok(plain.includes('…'), 'the code was clipped to what the divider left');
538
+ assert.equal(plain.includes('item.price;'), false, 'what did not fit is clipped, never wrapped or pushed out');
539
+ });
540
+
541
+ test('a clipped context row is drawn plain, never cut out of a painted row', () => {
542
+ const { seam, asked } = seamFor();
543
+ const row = `│ 12 const value = ${'x'.repeat(44)} │`;
544
+ const rows = diffColorDesign(seam).apply([row], ctx());
545
+ assert.equal(cols(rows[0]), cols(row), 'the row keeps the width the host gave it');
546
+ assert.ok(unpaint(rows[0]).includes('…'), 'the row ends in the ellipsis the clip wrote');
547
+ assert.equal(rows[0].includes('\u001b[36m'), false, 'a clipped line is drawn plain, never cut out of a paint');
548
+ assert.equal(rows[0].includes('<text>'), true, rows[0]);
549
+ assert.equal(asked.length, 0, 'no painted line was measured or clipped');
550
+ });
551
+
552
+ test('a row too narrow to hold the gutter keeps the host\'s drawing whole', () => {
553
+ const row = '│ -4 x │';
554
+ const out = diffColorDesign(seamFor().seam).apply([row], ctx());
555
+ assert.deepEqual(out, [row], 'the divider is never added by overflowing the row');
556
+ });
557
+
558
+ test('a hunk header and a skip marker state no line, so neither is divided', () => {
559
+ const hunk = '│ @@ -1,4 +1,4 @@ │';
560
+ const skip = '│ ... │';
561
+ const [outHunk, outSkip] = diffColorDesign(seamFor().seam).apply([hunk, skip], ctx());
562
+ assert.equal(outSkip, skip, 'a skip marker is not a diff row and is left as drawn');
563
+ assert.equal(unpaint(outHunk), unpaint(hunk), 'a hunk header states the same text');
564
+ assert.match(outHunk, /<muted>@@ -1,4 \+1,4 @@<\/muted>/u);
565
+ });
566
+ });
567
+
568
+ describe('a changed row is painted whole, as `git diff` paints it (S13)', () => {
569
+ const ctx = () => ({ tool: 'edit', part: 'call', width: WIDTH, theme, render: { args: { path: 'lib/app.js' } } });
570
+ const card = [
571
+ '│ -12 const total = 1 │',
572
+ '│ +12 const total = 2 │',
573
+ ];
574
+
575
+ test('a removed row is ENTIRELY the removed role: marker, number, divider and code', () => {
576
+ const [removed] = diffColorDesign(seamFor().seam).apply(card, ctx());
577
+ assert.equal(removed, `│ <toolDiffRemoved>-12 │ const total = 1</toolDiffRemoved>${' '.repeat(17)}│`);
578
+ });
579
+
580
+ test('an added row is ENTIRELY the added role', () => {
581
+ const added = diffColorDesign(seamFor().seam).apply(card, ctx())[1];
582
+ assert.equal(added, `│ <toolDiffAdded>+12 │ const total = 2</toolDiffAdded>${' '.repeat(17)}│`);
583
+ });
584
+
585
+ test('the highlighter is never asked for a changed row\'s code', () => {
586
+ const { seam, asked } = seamFor();
587
+ diffColorDesign(seam).apply([...card, '│ 13 return total │'], ctx());
588
+ assert.deepEqual(asked.map(([code]) => code), ['return total'], 'only the context row is the highlighter\'s');
589
+ });
590
+
591
+ test('the host\'s intra-line emphasis is replaced by the role, so the row reads as one colour', () => {
592
+ // `renderDiff` puts `theme.inverse(...)` runs around the words that changed, INSIDE the role's
593
+ // paint. A whole-row role has no span to emphasise, and keeping them would make a second pass
594
+ // (which rebuilds the row from its plain text) differ from the first.
595
+ const emphasised = '│ -12 const \u001b[7mtotal\u001b[27m = 1 │';
596
+ const [row] = diffColorDesign(undefined).apply([emphasised], { ...ctx(), theme: { fg: (role, text) => `<${role}>${text}</${role}>` } });
597
+ assert.equal(row.includes('\u001b[7m'), false, 'no inverse run survives: the role is the whole row');
598
+ assert.equal(unpaint(row).includes('const total = 1'), true, 'the text itself is kept');
599
+ });
600
+
601
+ test('a context row keeps today\'s drawing, so the change is the changed rows alone', () => {
602
+ const { seam, asked } = seamFor();
603
+ const [row] = diffColorDesign(seam).apply(['│ 13 return total │'], ctx());
604
+ assert.deepEqual(asked.map(([code]) => code), ['return total']);
605
+ assert.ok(row.includes(`<muted>13</muted> <dim>│</dim> ${HIGHLIGHT('return total')}`), row);
606
+ });
607
+ });
608
+
511
609
  // S8b: a row already wearing this paint comes back BYTE-IDENTICAL. The markup theme above states
512
610
  // the role's NAME (`<toolDiffAdded>`), which the parser reads as the row's own text, so it can
513
611
  // never show an escape added twice; only a real theme, whose `fg` answers SGR codes, can.
514
- describe('repainting a row it already painted is a byte-identical no-op (S8b)', () => {
515
- const SGR_ROLE = { toolDiffAdded: '92', toolDiffRemoved: '91', muted: '90', text: '39' };
612
+ describe('repainting a row it already painted is a byte-identical no-op (S8b/S14)', () => {
613
+ const SGR_ROLE = { toolDiffAdded: '92', toolDiffRemoved: '91', muted: '90', dim: '2', text: '39' };
516
614
  const sgr = (role, text) => `\u001b[${SGR_ROLE[role] ?? '39'}m${text}\u001b[39m`;
517
615
  const sgrTheme = { fg: (role, text) => sgr(role, text) };
518
- // The host's own edit body row: the frame cells painted, the marker and the code its own colour.
616
+ // The host's own edit body rows, at the layout its `renderDiff` draws them
617
+ // (`dist/core/tools/edit-diff.js:356,362,377`: `${marker}${padded number} ${code}`), between the
618
+ // card's own painted frame cells and padded out to the card's width.
519
619
  const FRAME = '\u001b[32m│\u001b[39m';
520
- const hostRow = (text) => `${FRAME} ${text} ${FRAME}`;
620
+ const hostRow = (text, pad = 8) => `${FRAME} ${text}${' '.repeat(pad)} ${FRAME}`;
521
621
  const hostPainted = [
522
622
  hostRow('@@ -1,4 +1,4 @@'),
523
- hostRow('\u001b[91m-4 const stale = 3;\u001b[39m'),
524
- hostRow('\u001b[92m+4 const fresh = 4;\u001b[39m'),
525
- hostRow(' 4 const kept = 2;'),
623
+ hostRow('\u001b[91m- 4 const stale = 3;\u001b[39m'),
624
+ hostRow('\u001b[92m+ 4 const fresh = 4;\u001b[39m'),
625
+ hostRow(' 4 const kept = 2;'),
526
626
  ];
627
+ // A row's own text with its frame and padding removed: the divider is then the only difference a
628
+ // pass may make to what a row says.
629
+ const interior = (row) => unpaint(row).replace(/^│ +/u, '').replace(/ *│$/u, '');
630
+ const DIVIDED = ['@@ -1,4 +1,4 @@', '- 4 │ const stale = 3;', '+ 4 │ const fresh = 4;', '4 │ const kept = 2;'];
527
631
  const escapes = (row) => row.match(/\u001b\[[0-9;]*m/gu) ?? [];
528
632
 
529
633
  for (const [name, seam] of [['no highlighter', undefined], ['a real highlighter', seamFor().seam]]) {
530
- test(`${name}: a second pass over its own rows adds no escape and no reset`, () => {
634
+ test(`${name}: four passes over its own rows add no escape, no reset and no second divider`, () => {
531
635
  const design = diffColorDesign(seam);
532
636
  const ctx = { tool: 'edit', part: 'call', width: WIDTH, theme: sgrTheme, render: { args: { path: 'lib/app.js' } } };
533
- const once = design.apply(hostPainted, ctx);
534
- const twice = design.apply(once, ctx);
535
- assert.deepEqual(twice, once, 'the second application is a byte-identical no-op');
536
- for (const [index, row] of twice.entries()) {
537
- assert.equal(unpaint(row), unpaint(hostPainted[index]), `row ${index} states the same text`);
538
- assert.equal(cols(row), cols(hostPainted[index]), `row ${index} keeps its width`);
539
- assert.deepEqual(escapes(row), escapes(once[index]), `row ${index} gains no escape`);
637
+ const passes = [];
638
+ let rows = hostPainted;
639
+ for (let pass = 0; pass < 4; pass += 1) {
640
+ rows = design.apply(rows, ctx);
641
+ passes.push(rows);
642
+ }
643
+ for (const [pass, seen] of passes.entries()) {
644
+ assert.deepEqual(seen, passes[0], `pass ${pass + 1} is a byte-identical no-op`);
645
+ for (const [index, row] of seen.entries()) {
646
+ assert.equal(cols(row), cols(hostPainted[index]), `row ${index} keeps its width`);
647
+ assert.deepEqual(escapes(row), escapes(passes[0][index]), `row ${index} gains no escape`);
648
+ }
540
649
  }
650
+ assert.deepEqual(passes[0].map(interior), DIVIDED, 'one divider per row, never two');
541
651
  // The defect this pins: the added row's opening code used to survive in the prefix and be
542
- // issued again, so the second pass read `\u001b[92m\u001b[92m`. No escape is ever issued twice
543
- // in a row here — the marker's paint is rebuilt, never stacked on the paint already there.
544
- assert.doesNotMatch(once[2], /\u001b\[([0-9;]*)m\u001b\[\1m/u, once[2]);
545
- assert.doesNotMatch(once[1], /\u001b\[([0-9;]*)m\u001b\[\1m/u, once[1]);
546
- // The host's own frame cells keep their paint, on both passes.
547
- assert.ok(once[2].startsWith(`${FRAME} `), once[2]);
548
- assert.ok(once[2].endsWith(` ${FRAME}`), once[2]);
652
+ // issued again, so a second pass read `\u001b[92m\u001b[92m`. No escape is ever issued twice
653
+ // in a row here — the row is rebuilt from its plain text, never stacked on the paint already
654
+ // there.
655
+ assert.doesNotMatch(passes[0][2], /\u001b\[([0-9;]*)m\u001b\[\1m/u, passes[0][2]);
656
+ assert.doesNotMatch(passes[0][1], /\u001b\[([0-9;]*)m\u001b\[\1m/u, passes[0][1]);
657
+ // The host's own frame cells keep their paint, on every pass.
658
+ assert.ok(passes[0][2].startsWith(`${FRAME} `), passes[0][2]);
659
+ assert.ok(passes[0][2].endsWith(` ${FRAME}`), passes[0][2]);
549
660
  });
550
661
  }
551
662
 
663
+ test('a row with no padding gives up code, never the frame, and only once', () => {
664
+ const design = diffColorDesign(undefined);
665
+ const ctx = { tool: 'edit', part: 'call', width: WIDTH, theme: sgrTheme, render: { args: { path: 'lib/app.js' } } };
666
+ // The card's width is already spent: the two columns the divider needs come out of the code.
667
+ const full = `${FRAME} -12 for (const item of items) sum += item.price; ${FRAME}`;
668
+ const once = design.apply([full], ctx);
669
+ assert.equal(cols(once[0]), cols(full), 'the frame is not pushed out');
670
+ assert.match(interior(once[0]), /^-12 │ for \(const item of items\) sum \+= item\.pric…$/u, interior(once[0]));
671
+ assert.ok(interior(once[0]).endsWith('…'), 'what did not fit is clipped');
672
+ assert.deepEqual(design.apply(once, ctx), once, 'the clip fills the row, so the next pass is a no-op');
673
+ });
674
+
552
675
  test('the plainest host row is a no-op too: a real theme never doubles the marker paint', () => {
553
676
  const design = diffColorDesign(undefined);
554
677
  const ctx = { tool: 'edit', part: 'call', width: WIDTH, theme: sgrTheme, render: { args: { path: 'lib/app.js' } } };
555
- const rows = ['│ +4 const fresh = 4; │'];
678
+ const rows = ['│ +4 const fresh = 4; │'];
556
679
  const once = design.apply(rows, ctx);
557
- assert.equal(once[0], `│ ${sgr('toolDiffAdded', '+4 ')}${sgr('toolDiffAdded', 'const fresh = 4;')} │`);
680
+ assert.equal(once[0], `│ ${sgr('toolDiffAdded', '+4 │ const fresh = 4;')} │`);
558
681
  assert.deepEqual(design.apply(once, ctx), once);
559
682
  });
560
683
  });
@@ -595,7 +718,7 @@ describe('a missing or broken highlighter degrades to plain rows', () => {
595
718
  assert.doesNotThrow(() => { rows = diffColorDesign(seam).apply(result(), resultCtx({ render: { args: { path: 'lib/app.js', content } } })); });
596
719
  assert.ok(rows[0].includes('<text>const x = 1</text>'), rows[0]);
597
720
  const edit = diffColorDesign(seam).apply(editCard(), { tool: 'edit', part: 'call', width: WIDTH, theme, render: { args: { path: 'a.js' } } });
598
- assert.match(edit[2], /<toolDiffRemoved>const total = 1<\/toolDiffRemoved>/u, 'the diff role carries the code');
721
+ assert.match(edit[2], /<toolDiffRemoved>-12 │ const total = 1<\/toolDiffRemoved>/u, 'the diff role carries the code');
599
722
  });
600
723
 
601
724
  test('a highlighter whose paint changes the text is not a paint', () => {
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
- // S7: the `## ODD worktrees` section and its append-only injection. The text is
3
- // the contract, so it is asserted verbatim. There is exactly ONE section: the
4
- // package promises no CodeGraph work, so no gate and no variant selection.
2
+ // S7 and (T3) S5: the `## ODD worktrees` section and its append-only injection. The
3
+ // text is the contract, so it is asserted verbatim. There is exactly ONE primary
4
+ // section, plus the one short block a child that WRITES gets (S5): the same heading,
5
+ // its own file, and only for the three kinds of writing child.
5
6
  const { test, describe, after } = require('node:test');
6
7
  const assert = require('node:assert/strict');
7
8
  const os = require('node:os');
@@ -15,6 +16,19 @@ const PROMPT_DIR = path.resolve(__dirname, '..', 'lib', 'odd-prompt');
15
16
  const SECTION_FILE = path.join(PROMPT_DIR, 'odd-worktrees.md');
16
17
  const SECTION = fs.readFileSync(SECTION_FILE, 'utf8').trim();
17
18
 
19
+ // S5: the one-line block a CHILD that writes gets. It lives in its own file, so it is
20
+ // read here the way it is read on disk; a file that is gone fails the cases below.
21
+ const CHILD_FILE = path.join(PROMPT_DIR, 'odd-worktrees-child.md');
22
+ const childSection = () => fs.readFileSync(CHILD_FILE, 'utf8').trim();
23
+
24
+ // The argv a subagent child really runs with: one directory named after the agent.
25
+ function childArgv(agent) {
26
+ if (typeof agent !== 'string') return ['node', path.join(os.tmpdir(), 'plain-agent', 'instructions.md')];
27
+ return ['node', path.join(os.tmpdir(), `gentle-pi-subagent-${agent}-ab12cd`, 'instructions.md')];
28
+ }
29
+
30
+ const CHILD_ENV = { GENTLE_PI_AGENTS_CHILD: '1' };
31
+
18
32
  const savedEnv = { ...process.env };
19
33
  after(() => {
20
34
  for (const key of Object.keys(process.env)) {
@@ -90,12 +104,13 @@ describe('session gating', () => {
90
104
  }
91
105
  });
92
106
 
93
- test('a GENTLE_PI_AGENTS_CHILD worker gets nothing', () => {
107
+ test('a GENTLE_PI_AGENTS_CHILD session gets the child block, never the primary one', () => {
94
108
  clearEnv();
95
109
  process.env.GENTLE_PI_AGENTS_CHILD = '1';
96
110
  const options = {};
97
111
  run(primaryEvent(options), { env: process.env });
98
- assert.equal(options.appendSystemPrompt, undefined);
112
+ assert.equal(options.appendSystemPrompt, childSection(), 'a child whose name cannot be read is the generic child');
113
+ assert.notEqual(options.appendSystemPrompt, SECTION);
99
114
  });
100
115
 
101
116
  test('ODD_WORKTREE_PROMPT=0 suppresses the block entirely', () => {
@@ -127,6 +142,77 @@ describe('extension wiring (T3)', () => {
127
142
  });
128
143
  });
129
144
 
145
+ // ---------------------------------------------------------------------------
146
+ // S5: the child block — one short line, and only for a child that writes.
147
+ // ---------------------------------------------------------------------------
148
+ describe('S5: a child that writes gets one short block', () => {
149
+ test('the block lives in its own file, opens with the idempotency heading and states the rule in two sentences at most', () => {
150
+ const text = childSection();
151
+ assert.ok(text.startsWith('## ODD worktrees'), text.split('\n')[0]);
152
+ assert.match(text.replace(/\s+/g, ' '), /work only under the worktree of your workspace root/i);
153
+ assert.match(text.replace(/\s+/g, ' '), /primary checkout on the default branch/i);
154
+ assert.match(text.replace(/\s+/g, ' '), /stop and report/i);
155
+ assert.match(text.replace(/\s+/g, ' '), /instead of writing/i);
156
+ const sentences = (text.replace(/\s+/g, ' ').match(/[.!?](\s|$)/g) || []).length;
157
+ assert.ok(sentences <= 2, `two sentences at most, got ${sentences}`);
158
+ assert.notEqual(text, SECTION, 'the child block is not the primary block');
159
+ });
160
+
161
+ test('gentle-ai-worker, jd-fix-agent and a child whose agent name cannot be read get it', () => {
162
+ for (const agent of ['gentle-ai-worker', 'jd-fix-agent', undefined]) {
163
+ const options = {};
164
+ run(primaryEvent(options), { env: CHILD_ENV, argv: childArgv(agent) });
165
+ assert.equal(options.appendSystemPrompt, childSection(), String(agent));
166
+ }
167
+ });
168
+
169
+ test('a child whose name is outside the table is the generic child, and gets it too', () => {
170
+ const options = {};
171
+ run(primaryEvent(options), { env: CHILD_ENV, argv: childArgv('some-unknown-agent') });
172
+ assert.equal(options.appendSystemPrompt, childSection());
173
+ });
174
+
175
+ test('explore, verify, the two judges and every review-* child get nothing', () => {
176
+ for (const agent of ['gentle-ai-explore', 'gentle-ai-verify', 'jd-judge-a', 'jd-judge-b', 'review-a', 'review-verify']) {
177
+ const options = {};
178
+ run(primaryEvent(options), { env: CHILD_ENV, argv: childArgv(agent) });
179
+ assert.equal(options.appendSystemPrompt, undefined, agent);
180
+ }
181
+ });
182
+
183
+ test('a child block is appended after the harness text and is idempotent', () => {
184
+ const harness = 'You are Pi.\n\nUse the tools.';
185
+ const options = { appendSystemPrompt: harness };
186
+ run(primaryEvent(options), { env: CHILD_ENV, argv: childArgv('gentle-ai-worker') });
187
+ assert.ok(options.appendSystemPrompt.startsWith(harness), 'existing text is the prefix');
188
+ assert.equal(options.appendSystemPrompt, `${harness}\n\n${childSection()}`);
189
+ const once = options.appendSystemPrompt;
190
+ run(primaryEvent(options), { env: CHILD_ENV, argv: childArgv('gentle-ai-worker') });
191
+ assert.equal(options.appendSystemPrompt, once, 'a second call adds nothing');
192
+ });
193
+
194
+ test('ODD_WORKTREE_PROMPT=0 suppresses the child block too', () => {
195
+ const options = {};
196
+ run(primaryEvent(options), { env: { ...CHILD_ENV, ODD_WORKTREE_PROMPT: '0' }, argv: childArgv('gentle-ai-worker') });
197
+ assert.equal(options.appendSystemPrompt, undefined);
198
+ });
199
+
200
+ test('the primary session keeps its own block, unchanged', () => {
201
+ const options = {};
202
+ run(primaryEvent(options), { env: {}, argv: childArgv('gentle-ai-worker') });
203
+ assert.equal(options.appendSystemPrompt, SECTION, 'a primary session gets the primary block');
204
+ assert.notEqual(SECTION, childSection());
205
+ });
206
+
207
+ test('a missing child file falls back to the heading alone, never a crash', () => {
208
+ const missing = path.join(os.tmpdir(), `odd-prompt-missing-${process.pid}`, 'odd-worktrees-child.md');
209
+ assert.equal(prompt.readPromptFile(missing), null);
210
+ assert.equal(prompt.sectionFromFile(missing, prompt.CHILD_FALLBACK), prompt.CHILD_FALLBACK);
211
+ assert.equal(prompt.CHILD_FALLBACK.trim(), '## ODD worktrees');
212
+ assert.equal(prompt.CHILD_SECTION, childSection());
213
+ });
214
+ });
215
+
130
216
  describe('the section is prose on disk', () => {
131
217
  test('the section lives in its own Markdown file and is what gets injected', () => {
132
218
  assert.ok(fs.existsSync(SECTION_FILE), 'the section file exists');
@@ -166,20 +252,21 @@ describe('the section states the worktree policy', () => {
166
252
 
167
253
  test('the work happens in the ODD\'s worktree, never in the primary checkout or on the default branch', () => {
168
254
  const text = prose();
169
- assert.match(text, /An ODD writes only in its own worktree, never in the primary checkout or on the default branch; the feature branch lives there/, text);
255
+ assert.match(text, /Every write goes in a worktree, whatever the size of the task, a one-line fix included\. Never write in the primary checkout or on the default branch/, text);
256
+ assert.doesNotMatch(text, /An ODD writes/, 'no task size decides whether the rule applies');
170
257
  assert.match(text, /Reading anywhere is fine, and a request that stays read-only needs no worktree/, text);
171
258
  // The rule is the premise: it opens the section, before the worktree is described.
172
- assert.ok(text.indexOf('never in the primary checkout') < text.indexOf('A worktree lives at'), text);
259
+ assert.ok(text.indexOf('Never write in the primary checkout') < text.indexOf('A worktree lives at'), text);
173
260
  });
174
261
 
175
262
  test('a worktree is the ODD: find the assignment, resume it, create it, carry pending work', () => {
176
263
  const text = prose();
177
264
  assert.match(text, /A worktree lives at `/, text);
178
265
  assert.match(text, /directory IS the ODD/, text);
179
- assert.match(text, /Before the ODD's first write, call `worktree_list`/, text);
180
- assert.match(text, /if it already has a worktree, work there/, text);
181
- assert.match(text, /if not, create it with `worktree_create`/, text);
182
- assert.match(text, /use `copy_changes: true` if the pending changes of the current checkout belong to that work/, text);
266
+ assert.match(text, /Before your first `edit` or `write`: 1\. Run `worktree_list`\./, text);
267
+ assert.match(text, /2\. If the ODD has a worktree, work there\./, text);
268
+ assert.match(text, /3\. If not, run `worktree_create`/, text);
269
+ assert.match(text, /`copy_changes: true` if the pending changes of the current checkout belong to that work/, text);
183
270
  });
184
271
 
185
272
  test('one ODD, one worktree', () => {