pi-odd-worktree 2.1.0 → 3.0.1

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.
Files changed (160) hide show
  1. package/README.md +54 -319
  2. package/extensions/odd-worktree-tools.ts +4182 -2539
  3. package/lib/bash-activity.js +1166 -0
  4. package/lib/bash-design.js +684 -0
  5. package/lib/bash-destructive.js +559 -0
  6. package/lib/bash-frame.js +205 -0
  7. package/lib/card-content.js +23 -0
  8. package/lib/card-style.js +175 -0
  9. package/lib/checkout-facts.js +30 -70
  10. package/lib/codegraph-card-preview.js +628 -0
  11. package/lib/codegraph-card.js +515 -0
  12. package/lib/codegraph-compaction.js +76 -0
  13. package/lib/codegraph-daemon.js +246 -0
  14. package/lib/codegraph-edit-sync.js +182 -0
  15. package/lib/codegraph-guard-target.js +337 -0
  16. package/lib/codegraph-guard.js +211 -0
  17. package/lib/codegraph-index.js +1020 -0
  18. package/lib/codegraph-lock.js +607 -0
  19. package/lib/codegraph-mcp-config.js +134 -0
  20. package/lib/codegraph-mcp.js +123 -0
  21. package/lib/codegraph-paths.js +69 -0
  22. package/lib/codegraph-prompt/explore.md +11 -0
  23. package/lib/codegraph-prompt/fix.md +6 -0
  24. package/lib/codegraph-prompt/generic.md +8 -0
  25. package/lib/codegraph-prompt/judge.md +9 -0
  26. package/lib/codegraph-prompt/orchestrator.md +17 -0
  27. package/lib/codegraph-prompt/verify.md +10 -0
  28. package/lib/codegraph-prompt/worker.md +7 -0
  29. package/lib/codegraph-role.js +55 -0
  30. package/lib/codegraph-rules.js +146 -0
  31. package/lib/codegraph-session.js +303 -383
  32. package/lib/codegraph-warmup.js +139 -0
  33. package/lib/file-card-markdown.js +141 -0
  34. package/lib/file-card-resolver.js +245 -0
  35. package/lib/file-card.js +659 -0
  36. package/lib/files-index.js +387 -0
  37. package/lib/files-insert.js +81 -0
  38. package/lib/files-orders.js +326 -0
  39. package/lib/files-picker-view.js +1421 -0
  40. package/lib/files-preview.js +338 -0
  41. package/lib/files-scope-detail.js +171 -0
  42. package/lib/files-scope-view.js +507 -0
  43. package/lib/gh-issues.js +323 -15
  44. package/lib/issue-preview.js +26 -7
  45. package/lib/issues-bash-mutation.js +206 -0
  46. package/lib/issues-cache-store.js +261 -0
  47. package/lib/issues-section.js +710 -0
  48. package/lib/issues-view.js +649 -52
  49. package/lib/odd-prompt/odd-worktrees.md +21 -0
  50. package/lib/odd-worktrees-prompt.js +10 -28
  51. package/lib/related-issues-cache.js +210 -0
  52. package/lib/shell-card.js +825 -0
  53. package/lib/skill-odd-card.js +183 -0
  54. package/lib/tui-paint.js +4 -5
  55. package/lib/warp-create-panel.js +538 -250
  56. package/lib/warp-create-picker.js +56 -9
  57. package/lib/warp-delete-confirm.js +19 -28
  58. package/lib/warp-selector-view.js +160 -82
  59. package/lib/which-command.js +90 -0
  60. package/lib/worktree-card.js +267 -897
  61. package/lib/worktree-cards.js +383 -0
  62. package/lib/worktree-create.js +287 -0
  63. package/lib/worktree-git.js +3 -3
  64. package/lib/worktree-guard.js +548 -0
  65. package/lib/worktree-hooks.js +421 -712
  66. package/lib/worktree-list.js +120 -0
  67. package/lib/worktree-registry.js +162 -879
  68. package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
  69. package/lib/worktree-setup-runner.js +376 -0
  70. package/lib/worktree-setup.js +140 -0
  71. package/lib/worktree-warp-busy.js +462 -85
  72. package/lib/worktree-warp-sessions.js +1 -2
  73. package/package.json +2 -2
  74. package/tools/bash-activity.test.js +1420 -0
  75. package/tools/bash-design.test.js +1093 -0
  76. package/tools/bash-destructive.test.js +139 -0
  77. package/tools/card-content.test.js +44 -0
  78. package/tools/card-style.test.js +298 -0
  79. package/tools/codegraph-card-preview.test.js +254 -0
  80. package/tools/codegraph-card.test.js +1080 -0
  81. package/tools/codegraph-compaction.test.js +378 -0
  82. package/tools/codegraph-daemon.test.js +248 -0
  83. package/tools/codegraph-edit-sync.test.js +295 -0
  84. package/tools/codegraph-guard-target.test.js +174 -0
  85. package/tools/codegraph-guard.test.js +238 -0
  86. package/tools/codegraph-index.test.js +1948 -0
  87. package/tools/codegraph-lock.test.js +932 -0
  88. package/tools/codegraph-mcp-config.test.js +181 -0
  89. package/tools/codegraph-mcp.test.js +194 -0
  90. package/tools/codegraph-paths.test.js +44 -0
  91. package/tools/codegraph-role.test.js +30 -0
  92. package/tools/codegraph-rules.test.js +250 -0
  93. package/tools/codegraph-session.test.js +660 -391
  94. package/tools/codegraph-warmup.test.js +316 -0
  95. package/tools/commands.test.js +510 -31
  96. package/tools/file-card-markdown.test.js +188 -0
  97. package/tools/file-card.test.js +1289 -0
  98. package/tools/files-bridge.test.js +575 -0
  99. package/tools/files-command.test.js +613 -0
  100. package/tools/files-index.test.js +473 -0
  101. package/tools/files-insert.test.js +139 -0
  102. package/tools/files-orders.test.js +597 -0
  103. package/tools/files-picker-view.test.js +1889 -0
  104. package/tools/files-preview.test.js +452 -0
  105. package/tools/files-scope-detail.test.js +264 -0
  106. package/tools/files-scope-view.test.js +614 -0
  107. package/tools/fixtures/card-codegraph-outputs.js +110 -0
  108. package/tools/fixtures/codegraph-cli-stub.js +16 -0
  109. package/tools/fixtures/codegraph-init-process.js +28 -0
  110. package/tools/fixtures/codegraph-lock-worker.js +117 -0
  111. package/tools/fixtures/daemon-fake.js +98 -0
  112. package/tools/fixtures/daemon-mcp-server.js +39 -0
  113. package/tools/fixtures/daemon-warmup-worker.js +12 -0
  114. package/tools/fixtures/load-extension.js +136 -3
  115. package/tools/gh-issues-feedback.test.js +31 -7
  116. package/tools/gh-issues.test.js +417 -1
  117. package/tools/issue-preview.test.js +44 -7
  118. package/tools/issues-bash-mutation.test.js +101 -0
  119. package/tools/issues-cache-store.test.js +222 -0
  120. package/tools/issues-command.test.js +1517 -67
  121. package/tools/issues-open-guard.test.js +1096 -0
  122. package/tools/issues-section.test.js +4235 -0
  123. package/tools/issues-seed-query-regression.test.js +67 -0
  124. package/tools/issues-view.test.js +1489 -60
  125. package/tools/odd-worktrees-prompt.test.js +98 -65
  126. package/tools/package-surface.test.js +431 -36
  127. package/tools/related-issues-cache.test.js +324 -0
  128. package/tools/shell-card.test.js +834 -0
  129. package/tools/skill-odd-card.test.js +200 -0
  130. package/tools/t5-session-identity.test.js +44 -22
  131. package/tools/warp-busy-guard.test.js +2565 -309
  132. package/tools/warp-create-panel.test.js +499 -74
  133. package/tools/warp-create-picker.test.js +101 -5
  134. package/tools/warp-delete-confirm.test.js +49 -15
  135. package/tools/warp-empty-session.test.js +0 -1
  136. package/tools/warp-fallback-regression.test.js +533 -0
  137. package/tools/warp-selector-view.test.js +460 -196
  138. package/tools/which-command.test.js +126 -0
  139. package/tools/worktree-card.test.js +551 -0
  140. package/tools/worktree-cards.test.js +845 -0
  141. package/tools/worktree-changes.test.js +4 -5
  142. package/tools/worktree-create.test.js +489 -0
  143. package/tools/worktree-guard.test.js +823 -0
  144. package/tools/worktree-hooks.test.js +905 -0
  145. package/tools/worktree-list.test.js +170 -0
  146. package/tools/worktree-registry.test.js +89 -230
  147. package/tools/worktree-remove-async.test.js +318 -0
  148. package/tools/worktree-setup-runner.test.js +276 -0
  149. package/tools/worktree-setup.test.js +140 -0
  150. package/tools/worktree-tools.test.js +419 -199
  151. package/lib/codegraph-commands.js +0 -165
  152. package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
  153. package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
  154. package/lib/worktree-model-result.js +0 -291
  155. package/tools/codegraph-commands.test.js +0 -371
  156. package/tools/herdr-free-create.test.js +0 -161
  157. package/tools/worktree-close-herdr-async.test.js +0 -204
  158. package/tools/worktree-odd-migration.test.js +0 -224
  159. package/tools/worktree-registry-cache.test.js +0 -163
  160. package/tools/worktree-registry-odd.test.js +0 -231
@@ -0,0 +1,250 @@
1
+ 'use strict';
2
+ // The `## CodeGraph` section: one approved text per role, selected from the
3
+ // session's own facts and appended to the prompt options. The package runs,
4
+ // probes and writes no CodeGraph; the section only advises a session that
5
+ // already holds a CodeGraph tool. The texts are read from disk, never hardcoded.
6
+ const { test, describe } = require('node:test');
7
+ const assert = require('node:assert/strict');
8
+ const fs = require('node:fs');
9
+ const os = require('node:os');
10
+ const path = require('node:path');
11
+
12
+ const rules = require('../lib/codegraph-rules.js');
13
+
14
+ const DIR = rules.PROMPT_DIR;
15
+ const file = (variant) => fs.readFileSync(path.join(DIR, `${variant}.md`), 'utf8').trim();
16
+ const MCP = 'mcp__codegraph__codegraph_explore';
17
+
18
+ function run(options, env, argv) {
19
+ const event = { systemPromptOptions: options };
20
+ const returned = rules.appendCodegraphSection(event, { env, argv });
21
+ assert.equal(returned, undefined, 'the mutation is the whole contract');
22
+ return options.appendSystemPrompt;
23
+ }
24
+
25
+ const child = (agent, tools = [MCP]) => ({
26
+ env: { GENTLE_PI_AGENTS_CHILD: '1', GENTLE_PI_AGENTS_REQUESTED_TOOLS: tools.join(',') },
27
+ argv: ['node', `/tmp/gentle-pi-subagent-${agent}-ab12cd/instructions.md`],
28
+ });
29
+
30
+ describe('the seven texts on disk', () => {
31
+ test('each opens with the shared heading and names the same six search tools', () => {
32
+ for (const variant of rules.VARIANTS) {
33
+ const text = file(variant).replace(/\s+/g, ' ');
34
+ assert.ok(text.startsWith('## CodeGraph'), variant);
35
+ assert.match(text, /before you search or read code/, `${variant} gates searching and reading code`);
36
+ for (const binary of ['fd', 'find', 'rg', 'grep', 'read', 'cat']) {
37
+ assert.ok(text.includes(`\`${binary}\``), `${variant} names ${binary}`);
38
+ }
39
+ assert.match(text, /other tools stay|may skip it/, `${variant} leaves the other tools available`);
40
+ }
41
+ });
42
+
43
+ test('no text tells an agent to create, refresh or locate an index', () => {
44
+ for (const variant of rules.VARIANTS) {
45
+ assert.doesNotMatch(file(variant), /\binit\b|\bsync\b|create it|no index|fall back to/i, variant);
46
+ }
47
+ });
48
+
49
+ test('only the roles holding the native tool name its operations; the MCP tool is named in full', () => {
50
+ for (const variant of ['orchestrator', 'explore']) {
51
+ assert.match(file(variant), /`codegraph` `query`/, variant);
52
+ }
53
+ for (const variant of ['verify', 'worker', 'fix', 'judge']) {
54
+ const text = file(variant);
55
+ assert.ok(text.includes(`\`${MCP}\``), variant);
56
+ assert.doesNotMatch(text, /`query`|`explore`|`codegraph`/, variant);
57
+ }
58
+ assert.doesNotMatch(file('generic'), /`query`|`explore`|`mcp__/, 'generic names no operation');
59
+ assert.doesNotMatch(
60
+ rules.VARIANTS.map(file).join('\n'),
61
+ /`codegraph_explore`/,
62
+ 'the MCP tool is never named without its prefix',
63
+ );
64
+ });
65
+
66
+ test('only the orchestrator points the MCP tool at another checkout, through projectPath, and still delegates', () => {
67
+ const text = file('orchestrator').replace(/\s+/g, ' ');
68
+ assert.ok(text.includes(`\`${MCP}\``), 'it names the MCP tool in full');
69
+ assert.match(text, /`projectPath`/, 'it names the argument that selects the checkout');
70
+ assert.match(text, /sibling worktree/, 'it covers a worktree of the same project');
71
+ assert.match(text, /another git project/, 'it covers another project');
72
+ assert.match(text, /delegate/, 'it keeps delegation for what a look does not settle');
73
+ assert.match(text, /never present/, 'it keeps one workspace\'s answer from standing for another');
74
+ for (const variant of rules.VARIANTS.filter((name) => name !== 'orchestrator')) {
75
+ assert.doesNotMatch(file(variant), /projectPath/, `${variant} does not name projectPath`);
76
+ }
77
+ });
78
+ });
79
+
80
+ describe('the orchestrator and explore texts stay inside their role', () => {
81
+ const prose = (variant) => file(variant).replace(/\s+/g, ' ');
82
+
83
+ test('the orchestrator sizes with the graph only when one read batch does not already explain the change', () => {
84
+ const text = prose('orchestrator');
85
+ assert.match(text, /before you plan or size a change that one read batch does not already explain/, text);
86
+ assert.match(text, /a file or location you already know/, 'a known location needs no call, not only a known file');
87
+ assert.match(text, /which is what scope, size and any handoff depend on/, 'the small path has no handoff');
88
+ });
89
+
90
+ test('the orchestrator looks at the ODD\'s own worktree through projectPath when the session runs elsewhere', () => {
91
+ const text = prose('orchestrator');
92
+ assert.match(text, /An ODD works in its own worktree: when the session runs elsewhere, look at that worktree, at a sibling worktree or at another git project/, text);
93
+ assert.match(text, /`projectPath` set to that checkout/, text);
94
+ });
95
+
96
+ test('the orchestrator repeats no permission rule: that one is stated, and guarded, elsewhere', () => {
97
+ assert.doesNotMatch(prose('orchestrator'), /authori[sz]ation|ask first|permission/i);
98
+ });
99
+
100
+ test('the explorer\'s second look has a stop', () => {
101
+ assert.match(prose('explore'), /when the next layer is what the question needs, then stop/);
102
+ });
103
+ });
104
+
105
+ describe('selection', () => {
106
+ test('a primary session holding the native tool gets the orchestrator text', () => {
107
+ assert.equal(run({ selectedTools: ['codegraph', 'read'] }, {}, ['node']), file('orchestrator'));
108
+ });
109
+
110
+ test('each gentle-shell agent gets its own text', () => {
111
+ const cases = {
112
+ 'gentle-ai-explore': 'explore',
113
+ 'gentle-ai-verify': 'verify',
114
+ 'gentle-ai-worker': 'worker',
115
+ 'jd-fix-agent': 'fix',
116
+ 'jd-judge-a': 'judge',
117
+ 'jd-judge-b': 'judge',
118
+ };
119
+ for (const [agent, variant] of Object.entries(cases)) {
120
+ const { env, argv } = child(agent);
121
+ assert.equal(run({ selectedTools: [] }, env, argv), file(variant), agent);
122
+ }
123
+ });
124
+
125
+ test('a child whose name is unknown or unreadable gets the generic text', () => {
126
+ const unknown = child('some-other-agent');
127
+ assert.equal(run({ selectedTools: [] }, unknown.env, unknown.argv), file('generic'));
128
+ assert.equal(run({ selectedTools: [] }, unknown.env, ['node', 'no-directory-here']), file('generic'));
129
+ });
130
+
131
+ test('the agent name is cut against the known names, so the random suffix is not part of it', () => {
132
+ assert.equal(rules.agentNameFromArgv(['x', '/t/gentle-pi-subagent-jd-judge-a-9f/i.md']), 'jd-judge-a');
133
+ // An unrecognised name keeps the directory's spelling; the `review-` prefix is what recognises a reviewer.
134
+ assert.equal(rules.agentNameFromArgv(['x', '/t/gentle-pi-subagent-review-risk-9f/i.md']), 'review-risk-9f');
135
+ assert.equal(rules.agentNameFromArgv(['x', 7, null]), undefined);
136
+ assert.equal(rules.agentNameFromArgv(undefined), undefined);
137
+ });
138
+
139
+ test('both judges share one text and no variant is missing', () => {
140
+ assert.equal(rules.VARIANT_BY_AGENT['jd-judge-a'], rules.VARIANT_BY_AGENT['jd-judge-b']);
141
+ for (const variant of Object.values(rules.VARIANT_BY_AGENT)) assert.ok(rules.VARIANTS.includes(variant));
142
+ });
143
+ });
144
+
145
+ describe('gates', () => {
146
+ test('a session holding no CodeGraph tool gets nothing', () => {
147
+ assert.equal(run({ selectedTools: ['read', 'bash'] }, {}, ['node']), undefined);
148
+ const worker = child('gentle-ai-worker', ['read', 'edit']);
149
+ assert.equal(run({ selectedTools: [] }, worker.env, worker.argv), undefined);
150
+ });
151
+
152
+ test('the MCP wildcard counts as the explore tool', () => {
153
+ const worker = child('gentle-ai-worker', ['mcp__codegraph__*']);
154
+ assert.equal(run({ selectedTools: [] }, worker.env, worker.argv), file('worker'));
155
+ });
156
+
157
+ test('a review-* agent gets nothing even when it holds a CodeGraph tool', () => {
158
+ const reviewer = child('review-risk', [MCP, 'codegraph']);
159
+ assert.equal(run({ selectedTools: [] }, reviewer.env, reviewer.argv), undefined);
160
+ });
161
+
162
+ test('ODD_WORKTREE_PROMPT=0 suppresses it with the worktrees block', () => {
163
+ assert.equal(run({ selectedTools: ['codegraph'] }, { ODD_WORKTREE_PROMPT: '0' }, ['node']), undefined);
164
+ });
165
+
166
+ test('a child never gets the orchestrator text', () => {
167
+ const { env } = child('gentle-ai-worker');
168
+ assert.notEqual(run({ selectedTools: [] }, env, ['node']), file('orchestrator'));
169
+ });
170
+ });
171
+
172
+ describe('append-only injection', () => {
173
+ test('existing harness text stays the prefix and the section follows it', () => {
174
+ const harness = 'You are Pi.\n\n## ODD worktrees\n\nText.';
175
+ const out = run({ selectedTools: ['codegraph'], appendSystemPrompt: harness }, {}, ['node']);
176
+ assert.equal(out, `${harness}\n\n${file('orchestrator')}`);
177
+ });
178
+
179
+ test('a trailing newline keeps the separator minimal', () => {
180
+ assert.equal(
181
+ run({ selectedTools: ['codegraph'], appendSystemPrompt: 'Harness.\n' }, {}, ['node']),
182
+ `Harness.\n\n${file('orchestrator')}`,
183
+ );
184
+ });
185
+
186
+ test('idempotent by heading, and never assigns or returns systemPrompt', () => {
187
+ const event = { systemPromptOptions: { selectedTools: ['codegraph'] }, systemPrompt: 'harness-owned' };
188
+ rules.appendCodegraphSection(event, { env: {}, argv: ['node'] });
189
+ const once = event.systemPromptOptions.appendSystemPrompt;
190
+ rules.appendCodegraphSection(event, { env: {}, argv: ['node'] });
191
+ assert.equal(event.systemPromptOptions.appendSystemPrompt, once);
192
+ assert.equal(event.systemPrompt, 'harness-owned');
193
+ assert.ok(!('systemPrompt' in event.systemPromptOptions));
194
+ });
195
+
196
+ test('never throws on a hostile event', () => {
197
+ for (const event of [undefined, null, {}, { systemPromptOptions: null }, { systemPromptOptions: { selectedTools: 5 } }]) {
198
+ assert.doesNotThrow(() => rules.appendCodegraphSection(event, { env: {}, argv: ['node'] }));
199
+ }
200
+ const frozen = Object.freeze({ selectedTools: ['codegraph'] });
201
+ assert.doesNotThrow(() => rules.appendCodegraphSection({ systemPromptOptions: frozen }, { env: {}, argv: ['node'] }));
202
+ });
203
+ });
204
+
205
+ describe('a file that cannot be used', () => {
206
+ function scratch(files) {
207
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-rules-'));
208
+ for (const [name, content] of Object.entries(files)) fs.writeFileSync(path.join(dir, name), content);
209
+ return dir;
210
+ }
211
+
212
+ test('a missing or heading-less text falls back to the generic one; no generic is no section', () => {
213
+ const dir = scratch({ 'generic.md': '## CodeGraph\n\nGeneric.', 'worker.md': 'no heading here' });
214
+ try {
215
+ assert.equal(rules.sectionText('worker', dir), '## CodeGraph\n\nGeneric.');
216
+ assert.equal(rules.sectionText('explore', dir), '## CodeGraph\n\nGeneric.');
217
+ assert.equal(rules.sectionText('generic', dir), '## CodeGraph\n\nGeneric.');
218
+ } finally {
219
+ fs.rmSync(dir, { recursive: true, force: true });
220
+ }
221
+ const empty = scratch({});
222
+ try {
223
+ assert.equal(rules.sectionText('worker', empty), null);
224
+ } finally {
225
+ fs.rmSync(empty, { recursive: true, force: true });
226
+ }
227
+ });
228
+ });
229
+
230
+ describe('extension wiring', () => {
231
+ test('before_agent_start injects the section for a session that holds the tool', async () => {
232
+ const saved = { ...process.env };
233
+ delete process.env.GENTLE_PI_AGENTS_CHILD;
234
+ delete process.env.ODD_WORKTREE_PROMPT;
235
+ try {
236
+ const { loadExtension } = require('./fixtures/load-extension.js');
237
+ const { hooks } = await loadExtension({});
238
+ const handlers = hooks.filter(([name]) => name === 'before_agent_start').map(([, handler]) => handler);
239
+ assert.ok(handlers.length >= 1, 'the section has a before_agent_start handler');
240
+ const options = { selectedTools: ['codegraph'] };
241
+ for (const handler of handlers) await handler({ systemPromptOptions: options }, { cwd: os.tmpdir() });
242
+ assert.equal(options.appendSystemPrompt.split('## CodeGraph').length - 1, 1, 'the section is appended once');
243
+ assert.ok(options.appendSystemPrompt.includes(file('orchestrator')));
244
+ assert.ok(options.appendSystemPrompt.includes('## ODD worktrees'), 'the worktrees block is still there');
245
+ } finally {
246
+ for (const key of Object.keys(process.env)) if (!(key in saved)) delete process.env[key];
247
+ Object.assign(process.env, saved);
248
+ }
249
+ });
250
+ });