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,181 @@
1
+ 'use strict';
2
+ // S12: the global MCP entry. The package ensures `mcpServers.codegraph` of <agent dir>/mcp.json has the three fields CodeGraph's
3
+ // explore tool needs, adds it when it is missing, fixes only what differs, keeps everything else, and never rewrites a file
4
+ // that is already right. Every case uses a scratch directory: no test reads or writes a real agent directory.
5
+ const { test, after } = require('node:test');
6
+ const assert = require('node:assert/strict');
7
+ const fs = require('node:fs');
8
+ const os = require('node:os');
9
+ const path = require('node:path');
10
+ const { ensureCodegraphServer, CODEGRAPH_SERVER, mcpConfigPath } = require('../lib/codegraph-mcp-config.js');
11
+
12
+ const scratch = [];
13
+ const dir = () => {
14
+ const made = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-mcp-config-'));
15
+ scratch.push(made);
16
+ return made;
17
+ };
18
+ after(() => scratch.forEach((d) => fs.rmSync(d, { recursive: true, force: true })));
19
+
20
+ const write = (agentDir, value, mode) => {
21
+ const file = path.join(agentDir, 'mcp.json');
22
+ fs.writeFileSync(file, typeof value === 'string' ? value : JSON.stringify(value, null, 2), mode === undefined ? {} : { mode });
23
+ return file;
24
+ };
25
+ const read = (agentDir) => JSON.parse(fs.readFileSync(path.join(agentDir, 'mcp.json'), 'utf8'));
26
+ const leftovers = (agentDir) => fs.readdirSync(agentDir).filter((name) => name !== 'mcp.json');
27
+
28
+ test('the entry CodeGraph needs is exactly three fields', () => {
29
+ assert.deepEqual(CODEGRAPH_SERVER, {
30
+ command: 'codegraph',
31
+ args: ['serve', '--mcp'],
32
+ toolExposure: { codegraph_explore: 'direct' },
33
+ });
34
+ assert.equal(mcpConfigPath('/x/agent'), path.join('/x/agent', 'mcp.json'));
35
+ });
36
+
37
+ // S4: direct callability of one MCP tool is the documented per-tool `toolExposure` map, not an entry-level field.
38
+ // Pi docs, `docs/mcp.md` § "Control tool exposure": "To make the model see a tool without searching, give it
39
+ // `direct` exposure with `toolExposure`." and "`toolExposure` overrides the server exposure for individual
40
+ // tools." An entry-level `exposure: "direct"` would declare every CodeGraph tool instead of the one that needs it,
41
+ // so this shape is pinned against a future rewrite to the upstream PR's entry-level spelling.
42
+ test('direct callability is the per-tool toolExposure map, never an entry-level exposure', async () => {
43
+ assert.equal(Object.hasOwn(CODEGRAPH_SERVER, 'exposure'), false, 'the entry carries no server-wide exposure');
44
+ assert.deepEqual(CODEGRAPH_SERVER.toolExposure, { codegraph_explore: 'direct' });
45
+
46
+ const fresh = dir();
47
+ await ensureCodegraphServer({ agentDir: fresh });
48
+ assert.equal(Object.hasOwn(read(fresh).mcpServers.codegraph, 'exposure'), false, 'an added entry writes no exposure');
49
+
50
+ const kept = dir();
51
+ write(kept, { mcpServers: { codegraph: { command: 'codegraph', args: ['serve', '--mcp'], exposure: 'codemode' } } });
52
+ const result = await ensureCodegraphServer({ agentDir: kept });
53
+ assert.equal(result.status, 'fixed');
54
+ assert.deepEqual(result.changes, ['toolExposure.codegraph_explore']);
55
+ assert.deepEqual(read(kept).mcpServers.codegraph, {
56
+ command: 'codegraph',
57
+ args: ['serve', '--mcp'],
58
+ exposure: 'codemode',
59
+ toolExposure: { codegraph_explore: 'direct' },
60
+ });
61
+ });
62
+
63
+ test('a missing entry is added and every other server and key is kept', async () => {
64
+ const agentDir = dir();
65
+ write(agentDir, { settings: { a: 1 }, mcpServers: { other: { command: 'x', env: { TOKEN: 'keep' } } } });
66
+ const result = await ensureCodegraphServer({ agentDir });
67
+ assert.equal(result.status, 'added');
68
+ const config = read(agentDir);
69
+ assert.deepEqual(config.mcpServers.codegraph, CODEGRAPH_SERVER);
70
+ assert.deepEqual(config.mcpServers.other, { command: 'x', env: { TOKEN: 'keep' } });
71
+ assert.deepEqual(config.settings, { a: 1 });
72
+ assert.deepEqual(leftovers(agentDir), [], 'no temp file is left behind');
73
+ });
74
+
75
+ test('a missing file is created when the agent directory exists, and not when it does not', async () => {
76
+ const agentDir = dir();
77
+ assert.equal((await ensureCodegraphServer({ agentDir })).status, 'added');
78
+ assert.deepEqual(read(agentDir), { mcpServers: { codegraph: CODEGRAPH_SERVER } });
79
+ const absent = path.join(dir(), 'nope');
80
+ assert.equal((await ensureCodegraphServer({ agentDir: absent })).status, 'skipped');
81
+ assert.equal(fs.existsSync(absent), false);
82
+ });
83
+
84
+ test('an entry that differs is fixed field by field and the rest of the entry is kept', async () => {
85
+ const agentDir = dir();
86
+ write(agentDir, {
87
+ mcpServers: {
88
+ codegraph: {
89
+ command: '/old/codegraph',
90
+ args: ['serve'],
91
+ env: { A: 'b' },
92
+ toolExposure: { codegraph_explore: 'codemode', other_tool: 'hidden' },
93
+ },
94
+ },
95
+ });
96
+ const result = await ensureCodegraphServer({ agentDir });
97
+ assert.equal(result.status, 'fixed');
98
+ assert.deepEqual(result.changes.sort(), ['args', 'command', 'toolExposure.codegraph_explore']);
99
+ assert.deepEqual(read(agentDir).mcpServers.codegraph, {
100
+ command: 'codegraph',
101
+ args: ['serve', '--mcp'],
102
+ env: { A: 'b' },
103
+ toolExposure: { codegraph_explore: 'direct', other_tool: 'hidden' },
104
+ });
105
+ });
106
+
107
+ test('a missing toolExposure or a non-object entry is repaired too', async () => {
108
+ const a = dir();
109
+ write(a, { mcpServers: { codegraph: { command: 'codegraph', args: ['serve', '--mcp'] } } });
110
+ assert.equal((await ensureCodegraphServer({ agentDir: a })).status, 'fixed');
111
+ assert.deepEqual(read(a).mcpServers.codegraph, CODEGRAPH_SERVER);
112
+ const b = dir();
113
+ write(b, { mcpServers: { codegraph: 'broken' } });
114
+ assert.equal((await ensureCodegraphServer({ agentDir: b })).status, 'fixed');
115
+ assert.deepEqual(read(b).mcpServers.codegraph, CODEGRAPH_SERVER);
116
+ const c = dir();
117
+ write(c, { mcpServers: [] });
118
+ assert.equal((await ensureCodegraphServer({ agentDir: c })).status, 'invalid');
119
+ });
120
+
121
+ test('a file that is already right is not rewritten, whatever its formatting', async () => {
122
+ const agentDir = dir();
123
+ const text = '{"mcpServers":{"codegraph":{"command":"codegraph","args":["serve","--mcp"],"toolExposure":{"codegraph_explore":"direct"}}}}';
124
+ const file = write(agentDir, text);
125
+ const before = fs.statSync(file);
126
+ await new Promise((resolve) => setTimeout(resolve, 20));
127
+ assert.equal((await ensureCodegraphServer({ agentDir })).status, 'unchanged');
128
+ assert.equal(fs.readFileSync(file, 'utf8'), text, 'the bytes are the same');
129
+ assert.equal(fs.statSync(file).mtimeMs, before.mtimeMs);
130
+ });
131
+
132
+ test('invalid JSON is left untouched and reported, never overwritten', async () => {
133
+ const agentDir = dir();
134
+ const file = write(agentDir, '{ "mcpServers": { oops');
135
+ const result = await ensureCodegraphServer({ agentDir });
136
+ assert.equal(result.status, 'invalid');
137
+ assert.equal(fs.readFileSync(file, 'utf8'), '{ "mcpServers": { oops');
138
+ assert.deepEqual(leftovers(agentDir), []);
139
+ });
140
+
141
+ test('the permission bits of the file are kept', { skip: process.platform === 'win32' }, async () => {
142
+ const agentDir = dir();
143
+ const file = write(agentDir, { mcpServers: {} }, 0o600);
144
+ fs.chmodSync(file, 0o600);
145
+ await ensureCodegraphServer({ agentDir });
146
+ assert.equal(fs.statSync(file).mode & 0o777, 0o600);
147
+ });
148
+
149
+ test('a created file is private to its owner', { skip: process.platform === 'win32' }, async () => {
150
+ const agentDir = dir();
151
+ await ensureCodegraphServer({ agentDir });
152
+ assert.equal(fs.statSync(path.join(agentDir, 'mcp.json')).mode & 0o777, 0o600);
153
+ });
154
+
155
+ test('a failing write is reported, leaves the original and no temp file, and never throws', async () => {
156
+ const agentDir = dir();
157
+ const original = JSON.stringify({ mcpServers: {} });
158
+ const file = write(agentDir, original);
159
+ const failing = { ...fs.promises, rename: async () => { throw Object.assign(new Error('EACCES'), { code: 'EACCES' }); } };
160
+ const result = await ensureCodegraphServer({ agentDir, fs: failing });
161
+ assert.equal(result.status, 'failed');
162
+ assert.equal(fs.readFileSync(file, 'utf8'), original);
163
+ assert.deepEqual(leftovers(agentDir), []);
164
+ });
165
+
166
+ test('two sessions in a row change the file once', async () => {
167
+ const agentDir = dir();
168
+ write(agentDir, { mcpServers: {} });
169
+ const [first, second] = [await ensureCodegraphServer({ agentDir }), await ensureCodegraphServer({ agentDir })];
170
+ assert.equal(first.status, 'added');
171
+ assert.equal(second.status, 'unchanged');
172
+ });
173
+
174
+ test('the existing indentation is kept', async () => {
175
+ const agentDir = dir();
176
+ const file = write(agentDir, JSON.stringify({ mcpServers: {} }, null, 4) + '\n');
177
+ await ensureCodegraphServer({ agentDir });
178
+ const text = fs.readFileSync(file, 'utf8');
179
+ assert.match(text, /\n {4}"mcpServers"/);
180
+ assert.ok(text.endsWith('\n'));
181
+ });
@@ -0,0 +1,194 @@
1
+ 'use strict';
2
+ // S5: the MCP configuration is read, never written. A registered entry is not proof a daemon is reachable,
3
+ // and MCP is optional: a session with no server works, and the package only says so.
4
+
5
+ const { test, after } = require('node:test');
6
+ const assert = require('node:assert/strict');
7
+ const fs = require('node:fs');
8
+ const os = require('node:os');
9
+ const path = require('node:path');
10
+
11
+ const mcp = require('../lib/codegraph-mcp.js');
12
+ const { isCodegraphServerMissing, readExposureWarning, readMissingServerWarning } = mcp;
13
+
14
+ const AGENT = '/agent-with-no-files';
15
+ const PROJECT = '/project-with-no-files';
16
+ const GLOBAL_PATH = `${AGENT}/mcp.json`;
17
+ const PROJECT_PATH = `${PROJECT}/.pi/mcp.json`;
18
+
19
+ const base = (codegraph) => `${JSON.stringify({ mcpServers: { codegraph, engram: { command: 'node', lifecycle: 'lazy' } }, other: 1 }, null, 2)}\n`;
20
+ const DIRECT = base({ command: 'codegraph', toolExposure: { codegraph_explore: 'direct' } });
21
+
22
+ /** A read seam over a path→text map: anything else is a missing file, as on disk. */
23
+ function reads(map) {
24
+ return (file) => {
25
+ if (Object.prototype.hasOwnProperty.call(map, file)) return map[file];
26
+ const error = new Error(`ENOENT: no such file or directory, open '${file}'`);
27
+ error.code = 'ENOENT';
28
+ throw error;
29
+ };
30
+ }
31
+
32
+ function files(globalText, projectText) {
33
+ const map = {};
34
+ if (globalText !== undefined) map[GLOBAL_PATH] = globalText;
35
+ if (projectText !== undefined) map[PROJECT_PATH] = projectText;
36
+ return reads(map);
37
+ }
38
+
39
+ const exposure = (globalText, projectText, trusted = true) => readExposureWarning(AGENT, PROJECT, trusted, { readFile: files(globalText, projectText) });
40
+ const missing = (globalText, projectText, trusted = true, registered = false) =>
41
+ readMissingServerWarning(AGENT, PROJECT, trusted, registered, { readFile: files(globalText, projectText) });
42
+
43
+ const tempDirs = [];
44
+ function scratch(prefix) {
45
+ const dir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), prefix)));
46
+ tempDirs.push(dir);
47
+ return dir;
48
+ }
49
+
50
+ after(() => {
51
+ for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true });
52
+ });
53
+
54
+ // --- The surface is three reads, and no writer at all ---
55
+
56
+ test('S5: the module exposes the two warnings and the missing-server read, and no way to edit a configuration', () => {
57
+ assert.deepEqual(Object.keys(mcp).sort(), ['isCodegraphServerMissing', 'readExposureWarning', 'readMissingServerWarning']);
58
+ });
59
+
60
+ // --- Exposure ---
61
+
62
+ test('S5: a direct explore setting is no warning at all', () => {
63
+ assert.equal(exposure(DIRECT), undefined);
64
+ });
65
+
66
+ test('S5: explicit incompatible explore settings are reported, never overridden', () => {
67
+ for (const selected of ['codemode', 'deferred', 'hidden']) {
68
+ assert.equal(exposure(base({ command: 'codegraph', exposure: 'codemode', toolExposure: { codegraph_explore: selected } })), selected);
69
+ }
70
+ });
71
+
72
+ test('S5: hiding a server with a direct explore exception warns that explore remains exposed', () => {
73
+ assert.equal(exposure(base({ command: 'codegraph', exposure: 'hidden', toolExposure: { codegraph_explore: 'direct' } })), 'hidden-with-direct');
74
+ });
75
+
76
+ test('S5: a server-wide exposure other than direct is reported', () => {
77
+ assert.equal(exposure(base({ command: 'codegraph' })), 'codemode');
78
+ assert.equal(exposure(base({ command: 'codegraph', exposure: 'deferred' })), 'deferred');
79
+ assert.equal(exposure(base({ command: 'codegraph', exposure: 'hidden' })), 'hidden');
80
+ });
81
+
82
+ test('S5: a disabled or hidden server is reported', () => {
83
+ assert.equal(exposure(base({ command: 'codegraph', enabled: false })), 'disabled');
84
+ assert.equal(exposure(base({ command: 'codegraph', exposure: 'hidden' })), 'hidden');
85
+ });
86
+
87
+ test('S5: wildcard choices are respected', () => {
88
+ assert.equal(exposure(base({ command: 'codegraph', toolExposure: { 'codegraph_*': 'hidden' } })), 'hidden');
89
+ assert.equal(exposure(base({ command: 'codegraph', toolExposure: { 'codegraph_*': 'direct' } })), undefined);
90
+ assert.equal(exposure(base({ command: 'codegraph', toolExposure: { 'codegraph_*': 'deferred' } })), 'deferred');
91
+ });
92
+
93
+ test('S5: a config without a CodeGraph server has no exposure to warn about', () => {
94
+ assert.equal(exposure(JSON.stringify({ mcpServers: { engram: { command: 'node' } } })), undefined);
95
+ assert.equal(exposure('{}'), undefined);
96
+ });
97
+
98
+ test('S5: invalid or unexpected configuration is not a warning and never throws', () => {
99
+ for (const text of ['', 'not json', '[]', 'null', '{"mcpServers":[]}', '{"mcpServers":{"codegraph":"x"}}']) {
100
+ assert.equal(exposure(text), undefined, JSON.stringify(text));
101
+ }
102
+ });
103
+
104
+ test('S5: an untrusted project configuration is ignored', () => {
105
+ const project = JSON.stringify({ mcpServers: { codegraph: { toolExposure: { codegraph_explore: 'hidden' } } } });
106
+ assert.equal(exposure(DIRECT, project, false), undefined);
107
+ assert.equal(exposure(DIRECT, project, true), 'hidden');
108
+ });
109
+
110
+ test('S5: a trusted project override is inspected on top of the global configuration', () => {
111
+ const project = JSON.stringify({ mcpServers: { codegraph: { exposure: 'hidden' } } });
112
+ assert.equal(exposure(DIRECT, project, true), 'hidden-with-direct');
113
+ assert.equal(exposure(DIRECT, undefined, true), undefined);
114
+ });
115
+
116
+ test('S5: a project server definition replaces the global definition', () => {
117
+ const hidden = base({ command: 'codegraph', toolExposure: { codegraph_explore: 'hidden' } });
118
+ const replacing = JSON.stringify({ mcpServers: { codegraph: { command: 'other', toolExposure: { codegraph_explore: 'direct' } } } });
119
+ assert.equal(exposure(hidden, replacing, true), undefined);
120
+ // A replacement that states nothing about exposure falls back to the default, codemode.
121
+ const plain = JSON.stringify({ mcpServers: { codegraph: { command: 'other' } } });
122
+ assert.equal(exposure(DIRECT, plain, true), 'codemode');
123
+ });
124
+
125
+ test('S5: reading global and trusted project settings changes nothing on disk', () => {
126
+ const agent = scratch('odd-mcp-agent-');
127
+ const project = scratch('odd-mcp-project-');
128
+ fs.mkdirSync(path.join(project, '.pi'));
129
+ fs.writeFileSync(path.join(agent, 'mcp.json'), DIRECT);
130
+ fs.writeFileSync(path.join(project, '.pi', 'mcp.json'), JSON.stringify({ mcpServers: { codegraph: { exposure: 'hidden' } } }));
131
+ const before = fs.readFileSync(path.join(project, '.pi', 'mcp.json'), 'utf8');
132
+ assert.equal(readExposureWarning(agent, project, false), undefined);
133
+ assert.equal(readExposureWarning(agent, project, true), 'hidden-with-direct');
134
+ assert.equal(fs.readFileSync(path.join(project, '.pi', 'mcp.json'), 'utf8'), before);
135
+ });
136
+
137
+ // --- A missing server ---
138
+
139
+ test('S5: a missing CodeGraph server is detected, and the other servers are left alone', () => {
140
+ const input = JSON.stringify({ mcpServers: { other: { command: 'node' } } });
141
+ const before = input;
142
+ assert.equal(isCodegraphServerMissing(input), true);
143
+ assert.equal(input, before, 'the read never rewrites the text it was given');
144
+ assert.equal(isCodegraphServerMissing('{}'), true);
145
+ });
146
+
147
+ test('S5: global, project or extension-registered CodeGraph servers are not missing', () => {
148
+ assert.equal(isCodegraphServerMissing(base({ command: 'codegraph' })), false);
149
+ assert.equal(isCodegraphServerMissing('{}', base({ command: 'codegraph' })), false);
150
+ assert.equal(isCodegraphServerMissing('{}', undefined, true), false);
151
+ });
152
+
153
+ test('S5: a project-only override without a server definition remains missing', () => {
154
+ const project = JSON.stringify({ mcpServers: { codegraph: { exposure: 'direct' } } });
155
+ assert.equal(isCodegraphServerMissing('{}', project), true);
156
+ });
157
+
158
+ test('S5: disabled or malformed settings are not misreported as an absent server', () => {
159
+ assert.equal(isCodegraphServerMissing(base({ command: 'codegraph', enabled: false })), false);
160
+ assert.equal(isCodegraphServerMissing('not json'), false);
161
+ assert.equal(isCodegraphServerMissing('{}', 'not json'), false);
162
+ });
163
+
164
+ test('S5: a missing server is reported without creating a configuration, and untrusted projects are ignored', () => {
165
+ const agent = scratch('odd-mcp-empty-agent-');
166
+ const project = scratch('odd-mcp-trusted-project-');
167
+ fs.mkdirSync(path.join(project, '.pi'));
168
+ const config = JSON.stringify({ mcpServers: { codegraph: { command: 'codegraph' } } });
169
+ fs.writeFileSync(path.join(project, '.pi', 'mcp.json'), config);
170
+ assert.equal(readMissingServerWarning(agent, project, false), true);
171
+ assert.equal(readMissingServerWarning(agent, project, true), false);
172
+ assert.equal(readMissingServerWarning(agent, project, false, true), false);
173
+ assert.equal(fs.existsSync(path.join(agent, 'mcp.json')), false);
174
+ assert.equal(fs.readFileSync(path.join(project, '.pi', 'mcp.json'), 'utf8'), config);
175
+ });
176
+
177
+ test('S5: MCP is optional: no server at all is a warning, never a failure', () => {
178
+ const agent = scratch('odd-mcp-none-agent-');
179
+ const project = scratch('odd-mcp-none-project-');
180
+ assert.equal(readMissingServerWarning(agent, project, true), true);
181
+ assert.equal(readExposureWarning(agent, project, true), undefined);
182
+ assert.deepEqual(fs.readdirSync(agent), []);
183
+ assert.deepEqual(fs.readdirSync(project), []);
184
+ });
185
+
186
+ test('S5: an unreadable configuration is an invalid one, not an absent server', () => {
187
+ const unreadable = () => {
188
+ const error = new Error('EACCES: permission denied');
189
+ error.code = 'EACCES';
190
+ throw error;
191
+ };
192
+ assert.equal(readMissingServerWarning(AGENT, PROJECT, true, false, { readFile: unreadable }), false);
193
+ assert.equal(readExposureWarning(AGENT, PROJECT, true, { readFile: unreadable }), undefined);
194
+ });
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+ // One root identity, and codegraph never spawned with the project as its working directory.
3
+ const { test } = require('node:test');
4
+ const assert = require('node:assert/strict');
5
+ const fs = require('node:fs');
6
+ const os = require('node:os');
7
+ const path = require('node:path');
8
+ const { canonicalRoot, codegraphSpawn, foldRoot, PROJECT_ENV, serveCommand } = require('../lib/codegraph-paths.js');
9
+
10
+ const B = '\\';
11
+ const temp = () => fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'cg-paths-')));
12
+
13
+ test('foldRoot folds case only on Windows; trailing separators go and roots stay roots', () => {
14
+ assert.equal(foldRoot(`C:${B}Users${B}me${B}`, 'win32'), `c:${B}users${B}me`);
15
+ assert.equal(foldRoot(`C:${B}`, 'win32'), `c:${B}`);
16
+ assert.equal(foldRoot('/srv/Repo/', 'linux'), '/srv/Repo');
17
+ assert.equal(foldRoot('/', 'linux'), '/');
18
+ assert.equal(foldRoot('/Users/Me/Repo', 'darwin'), '/Users/Me/Repo');
19
+ });
20
+
21
+ test('canonicalRoot resolves a link and keeps a missing path on its nearest existing parent', (t) => {
22
+ const real = temp();
23
+ t.after(() => fs.rmSync(real, { recursive: true, force: true }));
24
+ assert.equal(canonicalRoot(real + '/'), canonicalRoot(real));
25
+ const link = real + '-link';
26
+ fs.symlinkSync(real, link);
27
+ t.after(() => fs.rmSync(link, { force: true }));
28
+ assert.equal(canonicalRoot(link), canonicalRoot(real));
29
+ assert.equal(canonicalRoot(path.join(link, 'missing', 'x')), path.join(canonicalRoot(real), 'missing', 'x'));
30
+ });
31
+
32
+ test('codegraphSpawn runs from the temp directory with the project as an argument, no shell', () => {
33
+ const spawn = codegraphSpawn(['sync'], '/work/repo', 'linux');
34
+ assert.deepEqual(spawn, { command: 'codegraph', args: ['sync', '/work/repo'], cwd: os.tmpdir(), shell: false, env: {} });
35
+ });
36
+
37
+ test('on Windows the project travels in the environment, never in the command line', () => {
38
+ const spawn = codegraphSpawn(['init'], `C:${B}repo${B}`, 'win32');
39
+ assert.equal(spawn.shell, true);
40
+ assert.ok(!spawn.command.includes('repo'));
41
+ assert.equal(spawn.env[PROJECT_ENV], `C:${B}repo${B}.`);
42
+ const serve = serveCommand('codegraph serve --mcp', `C:${B}repo`, 'win32');
43
+ assert.equal(serve.command, `codegraph serve --mcp -p "%${PROJECT_ENV}%"`);
44
+ });
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+ // The index role of a session: which roles may start an index write at all.
3
+ const { test } = require('node:test');
4
+ const assert = require('node:assert/strict');
5
+ const { indexRole, sessionIndexRole, requestedTools, mayInitiateIndexWrite } = require('../lib/codegraph-role.js');
6
+
7
+ const MCP = 'mcp__codegraph__codegraph_explore';
8
+
9
+ test('the role is read from the declared tools: a file-writing tool wins, then a command tool, then a CodeGraph tool', () => {
10
+ assert.equal(indexRole(['read', 'grep', 'find', 'codegraph']), 'explorer');
11
+ assert.equal(indexRole(['read', 'grep', 'find', MCP]), 'explorer');
12
+ assert.equal(indexRole(['read', 'grep', 'find', 'bash', MCP]), 'read-only');
13
+ assert.equal(indexRole(['read', 'grep', 'find', 'bash', 'edit', MCP]), 'writer');
14
+ assert.equal(indexRole(['read', 'grep', 'find']), 'unknown');
15
+ assert.equal(indexRole([]), 'unknown');
16
+ assert.equal(indexRole(undefined), 'unknown');
17
+ });
18
+
19
+ test('only a read-only role may not start an index write', () => {
20
+ assert.equal(mayInitiateIndexWrite('read-only'), false);
21
+ for (const role of ['explorer', 'writer', 'unknown', undefined]) assert.equal(mayInitiateIndexWrite(role), true, String(role));
22
+ });
23
+
24
+ test('the primary session has no subagent role; a child gets the role of its requested tools', () => {
25
+ assert.equal(sessionIndexRole({}), undefined);
26
+ assert.equal(sessionIndexRole({ GENTLE_PI_AGENTS_CHILD: '0', GENTLE_PI_AGENTS_REQUESTED_TOOLS: 'read' }), undefined);
27
+ assert.equal(sessionIndexRole({ GENTLE_PI_AGENTS_CHILD: '1', GENTLE_PI_AGENTS_REQUESTED_TOOLS: 'read,bash' }), 'read-only');
28
+ assert.equal(sessionIndexRole({ GENTLE_PI_AGENTS_CHILD: '1', GENTLE_PI_AGENTS_REQUESTED_TOOLS: 'read, codegraph ' }), 'explorer');
29
+ assert.deepEqual(requestedTools({ GENTLE_PI_AGENTS_REQUESTED_TOOLS: ' a, ,b ' }), ['a', 'b']);
30
+ });