gentle-pi 5.0.0 → 5.1.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.
Files changed (234) hide show
  1. package/README.md +1 -1
  2. package/assets/agents/gentle-ai-explore.md +3 -6
  3. package/assets/agents/gentle-ai-verify.md +1 -1
  4. package/assets/agents/gentle-ai-worker.md +9 -9
  5. package/assets/agents/jd-fix-agent.md +1 -1
  6. package/assets/agents/jd-judge-a.md +0 -1
  7. package/assets/agents/jd-judge-b.md +0 -1
  8. package/assets/migrations/agent-body-history.json +6 -0
  9. package/assets/orchestrator-full-delegation.md +1 -1
  10. package/assets/orchestrator-maintenance.md +11 -0
  11. package/assets/orchestrator-quota-recovery.md +1 -1
  12. package/assets/orchestrator-skills.md +10 -0
  13. package/assets/orchestrator-verification.md +9 -7
  14. package/assets/orchestrator.md +1 -1
  15. package/bin/gentle-shell.mjs +67 -15
  16. package/contracts/review-provider-contract-mirror/provider-contract.lock.json +10 -10
  17. package/contracts/review-provider-contract-mirror/{v1.6.0 → v1.7.0}/bundle/manifest.json +5 -5
  18. package/contracts/review-provider-contract-mirror/{v1.6.0 → v1.7.0}/bundle/orchestration/pi.md +5 -3
  19. package/contracts/review-provider-contract-mirror/{v1.6.0 → v1.7.0}/bundle/routing/pi.md +1 -1
  20. package/contracts/review-provider-contract-mirror/{v1.6.0 → v1.7.0}/bundle/schemas/lens.schema.json +3 -3
  21. package/contracts/review-provider-contract-mirror/v1.7.0/bundle/vectors/lens.json +1 -0
  22. package/contracts/review-provider-contract-mirror/{v1.6.0 → v1.7.0}/generated/provider-capabilities.baseline.json +3 -3
  23. package/contracts/review-provider-contract-mirror/{v1.6.0 → v1.7.0}/generated/provider-roles.baseline.json +3 -3
  24. package/docs/bundled-install.md +2 -2
  25. package/docs/delegated-verification.md +8 -4
  26. package/docs/desktop-feed.md +15 -6
  27. package/docs/desktop-rpc.md +44 -20
  28. package/docs/gentle-shell-desktop.md +10 -3
  29. package/docs/gentle-shell.md +17 -13
  30. package/docs/install-wizard.md +2 -2
  31. package/docs/odd-preparation-continuity.md +2 -2
  32. package/docs/permission-modes.md +10 -2
  33. package/docs/readme-reference.md +46 -11
  34. package/docs/session-profile-format.md +33 -7
  35. package/docs/work-continuity.md +7 -7
  36. package/docs/yolo-mode.md +1 -1
  37. package/extensions/child-context.ts +2 -11
  38. package/extensions/gentle-agents.ts +47 -28
  39. package/extensions/gentle-ai.ts +573 -353
  40. package/extensions/gentle-desktop-feed.ts +13 -3
  41. package/extensions/gentle-fast.ts +9 -2
  42. package/extensions/gentle-shell.ts +142 -55
  43. package/extensions/gentle-tps.ts +5 -0
  44. package/extensions/quiet-tools.ts +13 -22
  45. package/extensions/skill-registry.ts +9 -1
  46. package/herdr/mods/gentle-agents/snapshot.js +30 -23
  47. package/herdr/mods/gentle-chat/feed.js +6 -0
  48. package/herdr/mods/gentle-chat/main.js +76 -16
  49. package/herdr/mods/gentle-chat/session.js +13 -5
  50. package/herdr/mods/gentle-chat/snapshot.js +30 -23
  51. package/herdr/mods/gentle-chat/ui.js +7 -1
  52. package/herdr/mods/gentle-inspector/main.js +2 -0
  53. package/herdr/mods/gentle-inspector/snapshot.js +30 -23
  54. package/herdr/mods/gentle-palette/main.js +1 -1
  55. package/herdr/mods/gentle-palette/snapshot.js +30 -23
  56. package/herdr/mods/gentle-pane/snapshot.js +30 -23
  57. package/herdr/mods/gentle-sidebar/snapshot.js +30 -23
  58. package/herdr/mods/gentle-spaces/snapshot.js +30 -23
  59. package/herdr/mods/gentle-status/snapshot.js +30 -23
  60. package/herdr/mods/gentle-tabs/main.js +3 -2
  61. package/herdr/mods/gentle-tabs/new-tab.js +9 -0
  62. package/herdr/mods/gentle-tabs/snapshot.js +30 -23
  63. package/herdr/mods/gentle-tokens/snapshot.js +30 -23
  64. package/lib/agent-profiles.ts +50 -10
  65. package/lib/agents-config.ts +59 -4
  66. package/lib/agents-runner.ts +34 -1
  67. package/lib/auto-permission-runtime.ts +2 -1
  68. package/lib/background-jobs-tools.ts +24 -16
  69. package/lib/claude-bridge-config.ts +41 -0
  70. package/lib/desktop-agent-definitions-service.ts +10 -6
  71. package/lib/desktop-command-rpc.ts +71 -0
  72. package/lib/desktop-feed-sources.ts +44 -1
  73. package/lib/desktop-feed.ts +41 -29
  74. package/lib/desktop-rpc-agent-definitions.ts +7 -3
  75. package/lib/desktop-rpc-changes.ts +13 -4
  76. package/lib/desktop-rpc-config.ts +106 -3
  77. package/lib/exploration-budget.ts +3 -7
  78. package/lib/gentle-design.ts +23 -4
  79. package/lib/gentle-shell-desktop.ts +85 -0
  80. package/lib/gentle-shell-launcher.ts +106 -3
  81. package/lib/mcp-config.ts +214 -0
  82. package/lib/native-review-cli.ts +78 -34
  83. package/lib/odd-phase-inference.ts +1 -1
  84. package/lib/orchestrator-mode-policy.ts +25 -12
  85. package/lib/path-consent-fence.ts +113 -0
  86. package/lib/pi-settings.ts +157 -0
  87. package/lib/review-continuation-registry.ts +7 -20
  88. package/lib/review-coordinator.ts +5 -0
  89. package/lib/review-host-relay.ts +2 -35
  90. package/lib/review-integration-v2.ts +31 -12
  91. package/lib/sensitive-paths.ts +22 -2
  92. package/lib/session-profile-freeze.ts +11 -4
  93. package/lib/session-worktree-registry.ts +11 -5
  94. package/lib/shell-card.ts +27 -1
  95. package/lib/shell-changes-view.ts +6 -2
  96. package/lib/shell-changes.ts +94 -37
  97. package/lib/shell-git-worker.ts +158 -0
  98. package/lib/shell-idle-changes.ts +88 -0
  99. package/lib/shell-untracked-cache.ts +142 -0
  100. package/lib/shell-untracked-lines.ts +178 -0
  101. package/lib/tps-policy.ts +85 -0
  102. package/lib/vim-editor-adapter.ts +3 -2
  103. package/lib/voice-artifacts.ts +8 -1
  104. package/lib/voice-install.ts +7 -3
  105. package/lib/yolo-session-policy.ts +2 -2
  106. package/package.json +1 -1
  107. package/runtime/gentle-shell-desktop.mjs +85 -0
  108. package/runtime/gentle-shell-launcher.mjs +106 -3
  109. package/runtime/native-review-cli.mjs +77 -33
  110. package/runtime/review-integration-v2.mjs +31 -12
  111. package/runtime/sensitive-paths.mjs +22 -2
  112. package/scripts/gentle-ai-installer.mjs +10 -10
  113. package/scripts/main-channel.mjs +27 -7
  114. package/scripts/verify-package-files.mjs +13 -12
  115. package/skills/rdd-defect-workflow/SKILL.md +2 -2
  116. package/tests/agent-profiles-workflow.test.ts +102 -0
  117. package/tests/agents-config.test.ts +1 -2
  118. package/tests/agents-protocol.test.ts +1 -1
  119. package/tests/agents-runner.test.ts +94 -2
  120. package/tests/append-system-prompt-route.test.ts +16 -11
  121. package/tests/auto-permission-runtime.test.ts +4 -4
  122. package/tests/bounded-stop-contract.test.ts +50 -62
  123. package/tests/child-context-files.test.ts +6 -6
  124. package/tests/child-web-package.test.ts +1 -1
  125. package/tests/controlled-engineering-editor.test.ts +192 -0
  126. package/tests/controlled-engineering-launch.test.ts +136 -0
  127. package/tests/controlled-engineering.test.ts +244 -0
  128. package/tests/delivery-b-rendered-surfaces.test.ts +1 -1
  129. package/tests/desktop-feed-tool-summaries.test.ts +1 -1
  130. package/tests/desktop-feed-transcript.test.ts +17 -0
  131. package/tests/desktop-feed.test.ts +141 -7
  132. package/tests/desktop-liveness.test.ts +125 -0
  133. package/tests/desktop-persona.test.ts +17 -0
  134. package/tests/desktop-rpc-agent-definitions.test.ts +23 -2
  135. package/tests/desktop-rpc-changes.test.ts +80 -3
  136. package/tests/desktop-rpc-config-mcp-fidelity.test.ts +106 -0
  137. package/tests/desktop-rpc-config-readonly-stores.test.ts +458 -0
  138. package/tests/desktop-rpc-config.test.ts +5 -2
  139. package/tests/desktop-rpc-design.test.ts +21 -0
  140. package/tests/desktop-rpc-session-snapshot-commands.test.ts +50 -0
  141. package/tests/desktop-session-switch.test.ts +237 -0
  142. package/tests/desktop-target-session.test.ts +104 -0
  143. package/tests/destructive-command-guard.test.ts +187 -0
  144. package/tests/devbinary/pi-host-relay.devtest.ts +73 -9
  145. package/tests/exploration-budget.test.ts +2 -26
  146. package/tests/fixtures/primary-session-prompt/gentleman.golden.md +14 -10
  147. package/tests/full-delegation-closure.test.ts +19 -9
  148. package/tests/generic-agent-tools.test.ts +3 -3
  149. package/tests/gentle-agents.test.ts +214 -9
  150. package/tests/gentle-ai-exploration-budget.test.ts +3 -2
  151. package/tests/gentle-ai-installer.test.ts +52 -51
  152. package/tests/gentle-ai.test.ts +29 -5
  153. package/tests/gentle-chat-read-failure.test.ts +74 -0
  154. package/tests/gentle-chat-session-read.test.ts +127 -0
  155. package/tests/gentle-design.test.ts +39 -0
  156. package/tests/gentle-shell-desktop.test.ts +108 -0
  157. package/tests/gentle-shell-launcher-auto-provision.test.ts +152 -0
  158. package/tests/gentle-shell-launcher-machine-output.test.ts +356 -0
  159. package/tests/gentle-shell.test.ts +93 -12
  160. package/tests/gentle-tps.test.ts +57 -0
  161. package/tests/independent-recovery-delivery.test.ts +15 -13
  162. package/tests/installer-preflight.test.ts +3 -2
  163. package/tests/job-turn-end-texts.test.ts +57 -0
  164. package/tests/lazy-module-contradictions-contract.test.ts +1 -2
  165. package/tests/main-channel.test.ts +12 -1
  166. package/tests/maintainer/desktop-rpc.maintest.ts +2 -2
  167. package/tests/native-review-capability-contract.test.ts +7 -1
  168. package/tests/native-review-cli.test.ts +113 -1
  169. package/tests/no-self-pause-contract.test.ts +8 -7
  170. package/tests/odd-phase-inference.test.ts +2 -2
  171. package/tests/odd-routing-canonical-ratchet.test.ts +1 -1
  172. package/tests/odd-routing-contract.test.ts +1 -1
  173. package/tests/orchestrator-budget.test.ts +7 -2
  174. package/tests/orchestrator-prompt-diet-contract.test.ts +5 -5
  175. package/tests/orchestrator-skills-inline-contract.test.ts +26 -0
  176. package/tests/package-manifest.test.ts +11 -16
  177. package/tests/packaged-agent-codemode.test.ts +79 -0
  178. package/tests/path-consent-fence.test.ts +326 -0
  179. package/tests/pi-update-launcher.test.ts +1 -1
  180. package/tests/primary-session-prompt.test.ts +9 -7
  181. package/tests/profile-fast-tier.test.ts +79 -4
  182. package/tests/progress-rule-contract.test.ts +54 -0
  183. package/tests/rdd-delivery-warning.test.ts +42 -4
  184. package/tests/review-actor-tool-deny.test.ts +1 -2
  185. package/tests/review-continuation-registry.test.ts +17 -17
  186. package/tests/review-controller-native-recovery.test.ts +100 -0
  187. package/tests/review-controller-native-routing.test.ts +129 -24
  188. package/tests/review-coordinator.test.ts +14 -0
  189. package/tests/review-host-relay-restart-parity.test.ts +2 -52
  190. package/tests/review-host-relay-routing.test.ts +57 -297
  191. package/tests/review-host-relay.test.ts +0 -46
  192. package/tests/review-integration-v2-forward.test.ts +39 -2
  193. package/tests/review-json-arguments.test.ts +30 -9
  194. package/tests/review-one-call-findings.test.ts +1 -0
  195. package/tests/review-untracked-inventory-replay.test.ts +181 -0
  196. package/tests/runtime-harness.mjs +27 -1
  197. package/tests/sensitive-path-tools.test.ts +155 -4
  198. package/tests/session-profile-extension.test.ts +90 -11
  199. package/tests/session-profile-freeze.test.ts +49 -2
  200. package/tests/session-profile-v1-compat.test.ts +11 -3
  201. package/tests/session-worktree-registry.test.ts +23 -0
  202. package/tests/shell-changes-idle-start.test.ts +510 -0
  203. package/tests/shell-changes-untracked-bound.test.ts +230 -0
  204. package/tests/shell-changes-view.test.ts +8 -2
  205. package/tests/shell-changes.test.ts +134 -0
  206. package/tests/shell-git-runner-stall.test.ts +74 -0
  207. package/tests/shell-git-worker-fallback.test.ts +140 -0
  208. package/tests/shell-git-worker-lifetime.test.ts +91 -0
  209. package/tests/shell-render-cache-runtime.test.ts +256 -3
  210. package/tests/shell-untracked-cache.test.ts +213 -0
  211. package/tests/short-check-routing-contract.test.ts +11 -7
  212. package/tests/skill-registry.test.ts +3 -3
  213. package/tests/support/chat-ui.ts +39 -0
  214. package/tests/t7-recovery-preservation.test.ts +68 -0
  215. package/tests/tps-policy.test.ts +96 -0
  216. package/tests/turn-end-rule-contract.test.ts +155 -0
  217. package/tests/turn-end-scope.test.ts +39 -0
  218. package/tests/verification-scoped-stop-contract.test.ts +1 -1
  219. package/tests/verification-tiers-contract.test.ts +4 -3
  220. package/tests/verify-first-launch-contract.test.ts +19 -7
  221. package/tests/vim-editor-adapter.test.ts +4 -4
  222. package/tests/voice-install.test.ts +97 -11
  223. package/tests/windows-hidden-processes.test.ts +11 -44
  224. package/tests/work-continuity-contract.test.ts +48 -55
  225. package/tests/yolo-session-policy.test.ts +2 -0
  226. package/contracts/review-provider-contract-mirror/v1.6.0/bundle/vectors/lens.json +0 -1
  227. package/extensions/codegraph-tools.ts +0 -437
  228. package/tests/child-codegraph-bootstrap.test.ts +0 -74
  229. package/tests/codegraph-tools.test.ts +0 -554
  230. /package/contracts/review-provider-contract-mirror/{v1.6.0 → v1.7.0}/bundle/README.md +0 -0
  231. /package/contracts/review-provider-contract-mirror/{v1.6.0 → v1.7.0}/bundle/schemas/refuter.schema.json +0 -0
  232. /package/contracts/review-provider-contract-mirror/{v1.6.0 → v1.7.0}/bundle/schemas/targeted-validator.schema.json +0 -0
  233. /package/contracts/review-provider-contract-mirror/{v1.6.0 → v1.7.0}/bundle/vectors/refuter.json +0 -0
  234. /package/contracts/review-provider-contract-mirror/{v1.6.0 → v1.7.0}/bundle/vectors/targeted-validator.json +0 -0
@@ -1,554 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { execFileSync } from "node:child_process";
3
- import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
4
- import { homedir, tmpdir } from "node:os";
5
- import { join, resolve } from "node:path";
6
- import test from "node:test";
7
- import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
8
- import codeGraphTools, {
9
- createCodeGraphTool,
10
- codeGraphNodeScript,
11
- findCodeGraphNodeScriptOnPath,
12
- isSameDirectory,
13
- type CodeGraphRunner,
14
- } from "../extensions/codegraph-tools.ts";
15
-
16
- function workspace(t: test.TestContext): string {
17
- const cwd = realpathSync(mkdtempSync(join(tmpdir(), "gentle-pi-codegraph-")));
18
- execFileSync("git", ["init", "-b", "main"], { cwd, stdio: "ignore" });
19
- t.after(() => rmSync(cwd, { recursive: true, force: true }));
20
- return cwd;
21
- }
22
-
23
- test("CodeGraph tool rejects non-project, nested-project, HOME, and temporary workspaces before init", async (t) => {
24
- const nonProject = realpathSync(mkdtempSync(join(tmpdir(), "gentle-pi-codegraph-non-project-")));
25
- t.after(() => rmSync(nonProject, { recursive: true, force: true }));
26
- const root = workspace(t);
27
- const nested = join(root, "nested");
28
- mkdirSync(nested);
29
- let calls = 0;
30
- const tool = createCodeGraphTool(async () => {
31
- calls += 1;
32
- return { stdout: "unexpected", stderr: "" };
33
- });
34
- for (const cwd of [nonProject, nested, homedir(), tmpdir()]) {
35
- await assert.rejects(
36
- () => tool.execute("test", { operation: "init" }, undefined, undefined, { cwd } as ExtensionContext),
37
- /real Git project root equal to the current workspace/i,
38
- );
39
- }
40
- assert.equal(calls, 0);
41
- });
42
-
43
- test("CodeGraph workspace guard compares directories by identity, not path spelling (#2558)", (t) => {
44
- // A mapped drive and its UNC form name one directory; an alias reproduces that without a network share.
45
- const root = workspace(t);
46
- const holder = realpathSync(mkdtempSync(join(tmpdir(), "gentle-pi-codegraph-alias-")));
47
- t.after(() => rmSync(holder, { recursive: true, force: true }));
48
- const alias = join(holder, "root");
49
- symlinkSync(root, alias, "junction");
50
- t.after(() => rmSync(alias, { force: true }));
51
- const nested = join(root, "nested");
52
- mkdirSync(nested);
53
- assert.notEqual(alias, root);
54
- assert.equal(isSameDirectory(alias, root), true);
55
- assert.equal(isSameDirectory(root, nested), false);
56
- assert.equal(isSameDirectory(root, join(root, "missing")), false);
57
- });
58
-
59
- test("CodeGraph tool accepts the Git root reached through a different spelling of the same directory (#2558)", async (t) => {
60
- // A mapped drive is one such spelling; on a case-insensitive filesystem a different letter case is another.
61
- const root = workspace(t);
62
- const respelled = [...root].map((char) => (char === char.toLowerCase() ? char.toUpperCase() : char.toLowerCase())).join("");
63
- if (respelled === root || !existsSync(respelled)) {
64
- t.skip("case-sensitive filesystem: no second spelling of the same directory");
65
- return;
66
- }
67
- const cwds: string[] = [];
68
- const tool = createCodeGraphTool(async (_args, options) => {
69
- cwds.push(options.cwd);
70
- return { stdout: "ok", stderr: "" };
71
- });
72
- await tool.execute("test", { operation: "query", query: "symbol" }, undefined, undefined, { cwd: respelled } as ExtensionContext);
73
- assert.deepEqual(cwds, [realpathSync(respelled)]);
74
- });
75
-
76
- test("CodeGraph workspace rejection names the compared paths and Git's own error (#2558)", async (t) => {
77
- const nonProject = realpathSync(mkdtempSync(join(tmpdir(), "gentle-pi-codegraph-non-project-")));
78
- t.after(() => rmSync(nonProject, { recursive: true, force: true }));
79
- const root = workspace(t);
80
- const nested = join(root, "nested");
81
- mkdirSync(nested);
82
- let calls = 0;
83
- const tool = createCodeGraphTool(async () => {
84
- calls += 1;
85
- return { stdout: "unexpected", stderr: "" };
86
- });
87
- const rejection = async (cwd: string): Promise<string> => {
88
- try {
89
- await tool.execute("test", { operation: "init" }, undefined, undefined, { cwd } as ExtensionContext);
90
- } catch (error) {
91
- return (error as Error).message;
92
- }
93
- assert.fail(`expected ${cwd} to be rejected`);
94
- };
95
-
96
- assert.equal(
97
- await rejection(nested),
98
- `CodeGraph requires a real Git project root equal to the current workspace.\n workspace: ${nested}\n git toplevel: ${root}`,
99
- );
100
-
101
- const [prefix, workspaceLine, gitLine, ...rest] = (await rejection(nonProject)).split("\n");
102
- assert.equal(prefix, "CodeGraph requires a real Git project root equal to the current workspace.");
103
- assert.equal(workspaceLine, ` workspace: ${nonProject}`);
104
- // Git's stderr, not Node's "Command failed" wrapper; Git may localize the text itself.
105
- assert.match(gitLine, /^ git: \S/);
106
- assert.doesNotMatch(gitLine, /Command failed/);
107
- for (const line of rest) assert.match(line, /^ /);
108
- assert.equal(calls, 0);
109
- });
110
-
111
- test("CodeGraph query timeout is reported distinctly from user abort and never bounds init (#1901)", async (t) => {
112
- const cwd = workspace(t);
113
- const ctx = { cwd } as ExtensionContext;
114
- // Behaves like execFile: settles only when its signal aborts, then rejects with AbortError.
115
- const hanging: CodeGraphRunner = (_args, options) => new Promise((_resolve, reject) => {
116
- options.signal?.addEventListener("abort", () => reject(Object.assign(new Error("The operation was aborted"), { name: "AbortError", code: "ABORT_ERR" })), { once: true });
117
- });
118
-
119
- const bounded = createCodeGraphTool(hanging, { queryTimeoutMs: 20 });
120
- // AbortSignal.timeout is unref'd; on Node 22 nothing else keeps the loop alive while the hanging runner awaits it.
121
- const keepalive = setTimeout(() => {}, 2000);
122
- try {
123
- for (const parameters of [{ operation: "query", query: "x" }, { operation: "explore", query: "x" }] as const) {
124
- const result = await bounded.execute("test", parameters, undefined, undefined, ctx);
125
- assert.match(result.content[0].text, /CodeGraph timed out after 20 ms/);
126
- assert.match(result.content[0].text, /Use read, grep, and find/);
127
- assert.equal((result.details as { status: string }).status, "failed");
128
- }
129
- } finally {
130
- clearTimeout(keepalive);
131
- }
132
-
133
- // A user cancellation is not a timeout.
134
- const user = new AbortController();
135
- setTimeout(() => user.abort(), 5);
136
- const cancelled = await createCodeGraphTool(hanging, { queryTimeoutMs: 60_000 }).execute("test", { operation: "query", query: "x" }, user.signal, undefined, ctx);
137
- assert.doesNotMatch(cancelled.content[0].text, /timed out/);
138
- assert.match(cancelled.content[0].text, /CodeGraph failed to run/);
139
-
140
- // init is the legitimately slow first index: only the user's own signal bounds it.
141
- let initSignal: AbortSignal | undefined = new AbortController().signal;
142
- const slowInit = createCodeGraphTool(async (_args, options) => {
143
- initSignal = options.signal;
144
- await new Promise((resolve) => setTimeout(resolve, 60));
145
- return { stdout: "indexed", stderr: "" };
146
- }, { queryTimeoutMs: 20 });
147
- const init = await slowInit.execute("test", { operation: "init" }, undefined, undefined, ctx);
148
- assert.deepEqual(init.content, [{ type: "text", text: "indexed" }]);
149
- assert.equal(initSignal, undefined);
150
- });
151
-
152
- test("CodeGraph tool runs only fixed cwd-scoped init, query, and explore commands", async (t) => {
153
- const cwd = workspace(t);
154
- const calls: Array<{ args: readonly string[]; cwd: string }> = [];
155
- const runner: CodeGraphRunner = async (args, options) => {
156
- calls.push({ args, cwd: options.cwd });
157
- return { stdout: "indexed", stderr: "" };
158
- };
159
- const tool = createCodeGraphTool(runner);
160
- const ctx = { cwd } as ExtensionContext;
161
-
162
- for (const parameters of [
163
- { operation: "init" },
164
- { operation: "query", query: "buildGentlePrompt", limit: 4 },
165
- { operation: "explore", query: "review gate", limit: 3 },
166
- ] as const) {
167
- const result = await tool.execute("test", parameters, undefined, undefined, ctx);
168
- assert.deepEqual(result.content, [{ type: "text", text: "indexed" }]);
169
- }
170
-
171
- assert.deepEqual(calls, [
172
- { args: ["init", cwd], cwd },
173
- {
174
- args: ["query", "--path", cwd, "--limit", "4", "--", "buildGentlePrompt"],
175
- cwd,
176
- },
177
- {
178
- args: ["explore", "--path", cwd, "--max-files", "3", "--", "review gate"],
179
- cwd,
180
- },
181
- ]);
182
- });
183
-
184
- test("CodeGraph tool rejects pre-existing .codegraph symlinks and non-directories", async (t) => {
185
- for (const kind of ["symlink", "file"] as const) {
186
- const cwd = workspace(t);
187
- const indexPath = join(cwd, ".codegraph");
188
- if (kind === "symlink") {
189
- const outside = join(cwd, "outside");
190
- mkdirSync(outside);
191
- symlinkSync(outside, indexPath);
192
- } else {
193
- writeFileSync(indexPath, "not an index directory");
194
- }
195
- let calls = 0;
196
- const tool = createCodeGraphTool(async () => {
197
- calls += 1;
198
- return { stdout: "unexpected", stderr: "" };
199
- });
200
-
201
- await assert.rejects(
202
- () => tool.execute("test", { operation: "init" }, undefined, undefined, { cwd } as ExtensionContext),
203
- /must be a real directory/i,
204
- );
205
- assert.equal(calls, 0, `${kind} index must not execute CodeGraph`);
206
- }
207
- });
208
-
209
- test("CodeGraph tool passes hyphen-leading queries after the option terminator", async (t) => {
210
- const cwd = workspace(t);
211
- const calls: Array<readonly string[]> = [];
212
- const tool = createCodeGraphTool(async (args) => {
213
- calls.push(args);
214
- return { stdout: "safe", stderr: "" };
215
- });
216
- const ctx = { cwd } as ExtensionContext;
217
-
218
- await tool.execute("test", { operation: "query", query: "--help" }, undefined, undefined, ctx);
219
-
220
- assert.deepEqual(calls, [
221
- ["query", "--path", cwd, "--limit", "10", "--", "--help"],
222
- ]);
223
- });
224
-
225
- test("CodeGraph tool returns structured fallback instructions when the binary is unavailable", async (t) => {
226
- const cwd = workspace(t);
227
- const unavailable = Object.assign(new Error("spawn codegraph ENOENT"), { code: "ENOENT" });
228
- const tool = createCodeGraphTool(async () => {
229
- throw unavailable;
230
- });
231
- const ctx = { cwd } as ExtensionContext;
232
-
233
- const result = await tool.execute("test", { operation: "query", query: "symbol" }, undefined, undefined, ctx);
234
-
235
- assert.deepEqual(result.content, [{
236
- type: "text",
237
- text: "CodeGraph is unavailable because the codegraph binary was not found. Use read, grep, and find for this exploration.",
238
- }]);
239
- assert.deepEqual(result.details, {
240
- status: "unavailable",
241
- operation: "query",
242
- cwd,
243
- fallback: "Use read, grep, and find for this exploration.",
244
- });
245
- });
246
-
247
- test("CodeGraph tool returns fallback instructions when CodeGraph fails", async (t) => {
248
- const cwd = workspace(t);
249
- const tool = createCodeGraphTool(async () => {
250
- throw new Error("CodeGraph exited with status 1");
251
- });
252
- const ctx = { cwd } as ExtensionContext;
253
-
254
- const result = await tool.execute("test", { operation: "explore", query: "call path" }, undefined, undefined, ctx);
255
-
256
- assert.deepEqual(result.content, [{
257
- type: "text",
258
- text: "CodeGraph failed to run. Use read, grep, and find for this exploration.",
259
- }]);
260
- assert.deepEqual(result.details, {
261
- status: "failed",
262
- operation: "explore",
263
- cwd,
264
- fallback: "Use read, grep, and find for this exploration.",
265
- });
266
- });
267
-
268
- test("CodeGraph tool configures a process buffer above the returned-output truncation threshold", async (t) => {
269
- const cwd = workspace(t);
270
- let maxBuffer = 0;
271
- const tool = createCodeGraphTool(async (_args, options) => {
272
- maxBuffer = options.maxBuffer;
273
- return { stdout: "x".repeat(100_001), stderr: "" };
274
- });
275
- const ctx = { cwd } as ExtensionContext;
276
-
277
- const result = await tool.execute("test", { operation: "explore", query: "large result" }, undefined, undefined, ctx);
278
-
279
- assert.ok(maxBuffer > 100_000);
280
- assert.match(result.content[0]?.text ?? "", /\[CodeGraph output truncated\]$/);
281
- });
282
-
283
- test("CodeGraph tool rejects incomplete or oversized query requests before running a process", async (t) => {
284
- const cwd = workspace(t);
285
- let calls = 0;
286
- const tool = createCodeGraphTool(async () => {
287
- calls += 1;
288
- return { stdout: "unexpected", stderr: "" };
289
- });
290
- const ctx = { cwd } as ExtensionContext;
291
-
292
- await assert.rejects(
293
- () => tool.execute("test", { operation: "query" }, undefined, undefined, ctx),
294
- /query is required/i,
295
- );
296
- await assert.rejects(
297
- () => tool.execute("test", { operation: "explore", query: "x", limit: 21 }, undefined, undefined, ctx),
298
- /between 1 and 20/i,
299
- );
300
- assert.equal(calls, 0);
301
- });
302
-
303
- test("CodeGraph tool registration exposes a single constrained custom tool", () => {
304
- const tools: Array<{ name: string; renderShell?: string; parameters: Record<string, unknown> }> = [];
305
- const pi = {
306
- registerTool(tool: { name: string; parameters: Record<string, unknown> }) {
307
- tools.push(tool);
308
- },
309
- } as unknown as ExtensionAPI;
310
-
311
- codeGraphTools(pi);
312
-
313
- assert.equal(tools.length, 1);
314
- assert.equal(tools[0]?.name, "codegraph");
315
- assert.equal(tools[0]?.renderShell, "self", "CodeGraph must not inherit Pi's painted Box");
316
- assert.deepEqual(tools[0]?.parameters, {
317
- type: "object",
318
- additionalProperties: false,
319
- required: ["operation"],
320
- properties: {
321
- operation: { type: "string", enum: ["init", "query", "explore"] },
322
- query: { type: "string", minLength: 1, maxLength: 2_000 },
323
- limit: { type: "integer", minimum: 1, maximum: 20 },
324
- },
325
- });
326
- });
327
-
328
- function writeCodeGraphPackage(binDir: string, scriptSource = "#!/usr/bin/env node\n"): string {
329
- const pkgRoot = join(binDir, "node_modules", "@colbymchenry", "codegraph");
330
- mkdirSync(pkgRoot, { recursive: true });
331
- const scriptTarget = join(pkgRoot, "npm-shim.js");
332
- writeFileSync(scriptTarget, scriptSource);
333
- writeFileSync(
334
- join(pkgRoot, "package.json"),
335
- JSON.stringify({
336
- name: "@colbymchenry/codegraph",
337
- version: "1.0.0",
338
- bin: { codegraph: "npm-shim.js" },
339
- }),
340
- );
341
- writeFileSync(
342
- join(binDir, "codegraph.cmd"),
343
- "@ECHO off\nnode \"%~dp0\\node_modules\\@colbymchenry\\codegraph\\npm-shim.js\" %*\n",
344
- );
345
- return scriptTarget;
346
- }
347
-
348
- function withWindowsPath(t: test.TestContext, path: string): void {
349
- const previousPath = process.env.PATH;
350
- const previousWindowsPath = process.env.Path;
351
- const windowsPath = process.platform === "win32"
352
- ? `${path};${previousWindowsPath ?? previousPath ?? ""}`
353
- : path;
354
- process.env.Path = windowsPath;
355
- process.env.PATH = windowsPath;
356
- t.after(() => {
357
- if (previousPath === undefined) delete process.env.PATH;
358
- else process.env.PATH = previousPath;
359
- if (previousWindowsPath === undefined) delete process.env.Path;
360
- else process.env.Path = previousWindowsPath;
361
- });
362
- }
363
-
364
- test("Windows shim helpers skip stale PATH shims before resolving the npm script", (t) => {
365
- const staleBinDir = realpathSync(mkdtempSync(join(tmpdir(), "gentle-pi-codegraph-stale-bin-")));
366
- const validBinDir = realpathSync(mkdtempSync(join(tmpdir(), "gentle-pi-codegraph-valid-bin-")));
367
- t.after(() => rmSync(staleBinDir, { recursive: true, force: true }));
368
- t.after(() => rmSync(validBinDir, { recursive: true, force: true }));
369
- writeFileSync(join(staleBinDir, "codegraph.cmd"), "@ECHO off\n");
370
- const stalePkgRoot = join(staleBinDir, "node_modules", "@colbymchenry", "codegraph");
371
- mkdirSync(stalePkgRoot, { recursive: true });
372
- writeFileSync(join(stalePkgRoot, "package.json"), JSON.stringify({ bin: { codegraph: "." } }));
373
- const scriptTarget = writeCodeGraphPackage(validBinDir);
374
-
375
- withWindowsPath(t, `${staleBinDir};${validBinDir}`);
376
-
377
- assert.equal(codeGraphNodeScript(join(staleBinDir, "codegraph.cmd")), undefined);
378
- assert.equal(findCodeGraphNodeScriptOnPath(), scriptTarget);
379
- });
380
-
381
- test("Windows shim helpers resolve quoted PATH entries in order without stripping unmatched quotes", (t) => {
382
- const root = realpathSync(mkdtempSync(join(tmpdir(), "gentle-pi-codegraph-quoted-bin-")));
383
- t.after(() => rmSync(root, { recursive: true, force: true }));
384
- const firstBin = join(root, "Program Files", "nodejs");
385
- const secondBin = join(root, "other-bin");
386
- const firstScript = writeCodeGraphPackage(firstBin);
387
- const secondScript = writeCodeGraphPackage(secondBin);
388
- withWindowsPath(t, `"${firstBin}";${secondBin}`);
389
-
390
- assert.equal(findCodeGraphNodeScriptOnPath(), firstScript);
391
- for (const [path, expected] of [
392
- [`${firstBin};"${secondBin}"`, firstScript],
393
- [`"${firstBin};${secondBin}`, secondScript],
394
- [`${firstBin}";${secondBin}`, secondScript],
395
- [`${secondBin};"${firstBin}"`, secondScript],
396
- ]) {
397
- process.env.Path = path;
398
- process.env.PATH = path;
399
- assert.equal(findCodeGraphNodeScriptOnPath(), expected);
400
- }
401
- });
402
-
403
- test("Windows launcher passes command metacharacters as one literal argv value", async (t) => {
404
- const binDir = realpathSync(mkdtempSync(join(tmpdir(), "gentle-pi-codegraph-bin-")));
405
- t.after(() => rmSync(binDir, { recursive: true, force: true }));
406
- writeCodeGraphPackage(binDir, "process.stdout.write(JSON.stringify(process.argv.slice(2)));\n");
407
- const cwd = workspace(t);
408
- if (process.platform !== "win32") {
409
- const gitShim = join(binDir, "git");
410
- writeFileSync(gitShim, "#!/bin/sh\nprintf '%s\\n' \"$PWD\"\n");
411
- chmodSync(gitShim, 0o755);
412
- }
413
- withWindowsPath(t, binDir);
414
-
415
- const previousPlatform = process.platform;
416
- Object.defineProperty(process, "platform", { configurable: true, value: "win32" });
417
- t.after(() => Object.defineProperty(process, "platform", { configurable: true, value: previousPlatform }));
418
-
419
- const query = "literal & echo injected > shell-injection.txt";
420
- const result = await createCodeGraphTool().execute(
421
- "test",
422
- { operation: "query", query },
423
- undefined,
424
- undefined,
425
- { cwd } as ExtensionContext,
426
- );
427
-
428
- assert.deepEqual(result.content, [{
429
- type: "text",
430
- text: JSON.stringify(["query", "--path", cwd, "--limit", "10", "--", query]),
431
- }]);
432
- assert.equal(existsSync(join(cwd, "shell-injection.txt")), false);
433
- });
434
-
435
- // Real CodeGraph writes .codegraph/.gitignore with `*` and `!.gitignore`, so the
436
- // .gitignore itself stays untracked and would otherwise enter every diff.
437
- const writeIndex: CodeGraphRunner = async (_args, options) => {
438
- mkdirSync(join(options.cwd, ".codegraph"), { recursive: true });
439
- writeFileSync(join(options.cwd, ".codegraph", ".gitignore"), "*\n!.gitignore\n");
440
- writeFileSync(join(options.cwd, ".codegraph", "codegraph.db"), "index");
441
- return { stdout: "indexed", stderr: "" };
442
- };
443
-
444
- function gitOutput(cwd: string, ...args: string[]): string {
445
- return execFileSync("git", args, { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim();
446
- }
447
-
448
- function excludeFile(cwd: string): string {
449
- return realpathSync(resolve(cwd, gitOutput(cwd, "rev-parse", "--git-path", "info/exclude")));
450
- }
451
-
452
- function excludeLines(cwd: string): string[] {
453
- return readFileSync(excludeFile(cwd), "utf8").split("\n").filter((line) => line.trim() === ".codegraph/");
454
- }
455
-
456
- test("CodeGraph init keeps the whole .codegraph index out of git through the local exclude file, idempotently", async (t) => {
457
- const cwd = workspace(t);
458
- writeFileSync(join(cwd, "tracked.txt"), "tracked\n");
459
- execFileSync("git", ["add", "tracked.txt"], { cwd });
460
- const tool = createCodeGraphTool(writeIndex);
461
- const ctx = { cwd } as ExtensionContext;
462
-
463
- const first = await tool.execute("test", { operation: "init" }, undefined, undefined, ctx);
464
- const second = await tool.execute("test", { operation: "init" }, undefined, undefined, ctx);
465
-
466
- assert.deepEqual(first.content, [{ type: "text", text: "indexed" }]);
467
- assert.equal(gitOutput(cwd, "status", "--porcelain", "--untracked-files=all"), "A tracked.txt");
468
- assert.deepEqual(excludeLines(cwd), [".codegraph/"], "exactly one exclude line after repeated init");
469
- assert.equal(existsSync(join(cwd, ".gitignore")), false, "never creates or edits a tracked .gitignore");
470
- assert.deepEqual((first.details as { localExclude?: unknown }).localExclude, { status: "added", path: excludeFile(cwd) });
471
- assert.deepEqual((second.details as { localExclude?: unknown }).localExclude, { status: "present", path: excludeFile(cwd) });
472
- });
473
-
474
- test("CodeGraph exclusion appends on a new line and recognizes an equivalent existing pattern", async (t) => {
475
- const appended = workspace(t);
476
- writeFileSync(excludeFile(appended), "# local\n*.log");
477
- await createCodeGraphTool(writeIndex).execute("test", { operation: "init" }, undefined, undefined, { cwd: appended } as ExtensionContext);
478
- assert.equal(readFileSync(excludeFile(appended), "utf8"), "# local\n*.log\n.codegraph/\n");
479
-
480
- const equivalent = workspace(t);
481
- writeFileSync(excludeFile(equivalent), "/.codegraph\n");
482
- const result = await createCodeGraphTool(writeIndex).execute("test", { operation: "init" }, undefined, undefined, { cwd: equivalent } as ExtensionContext);
483
- assert.equal(readFileSync(excludeFile(equivalent), "utf8"), "/.codegraph\n");
484
- assert.equal((result.details as { localExclude?: { status: string } }).localExclude?.status, "present");
485
- assert.equal(gitOutput(equivalent, "status", "--porcelain", "--untracked-files=all"), "");
486
- });
487
-
488
- test("CodeGraph exclusion uses the exclude file git resolves for a linked worktree", async (t) => {
489
- const base = realpathSync(mkdtempSync(join(tmpdir(), "gentle-pi-codegraph-worktree-")));
490
- t.after(() => rmSync(base, { recursive: true, force: true }));
491
- const primary = join(base, "primary");
492
- const linked = join(base, "linked");
493
- mkdirSync(primary);
494
- execFileSync("git", ["init", "-b", "main"], { cwd: primary, stdio: "ignore" });
495
- execFileSync("git", ["-c", "user.name=Fixture", "-c", "user.email=fixture@example.invalid", "-c", "commit.gpgsign=false", "commit", "--allow-empty", "-m", "fixture"], { cwd: primary, stdio: "ignore" });
496
- execFileSync("git", ["worktree", "add", "-b", "feature", linked], { cwd: primary, stdio: "ignore" });
497
-
498
- const result = await createCodeGraphTool(writeIndex).execute("test", { operation: "init" }, undefined, undefined, { cwd: linked } as ExtensionContext);
499
-
500
- assert.deepEqual(result.content, [{ type: "text", text: "indexed" }]);
501
- assert.equal(gitOutput(linked, "status", "--porcelain", "--untracked-files=all"), "");
502
- assert.deepEqual(excludeLines(linked), [".codegraph/"]);
503
- assert.equal((result.details as { localExclude?: { path: string } }).localExclude?.path, excludeFile(linked));
504
- assert.equal(existsSync(join(linked, ".git", "info")), false, "the linked worktree's .git file is never treated as a directory");
505
- });
506
-
507
- test("CodeGraph query excludes an index that already exists, and leaves the exclude file alone without one", async (t) => {
508
- const indexed = workspace(t);
509
- await writeIndex(["init", indexed], { cwd: indexed, maxBuffer: 1 });
510
- const answer: CodeGraphRunner = async () => ({ stdout: "found", stderr: "" });
511
- await createCodeGraphTool(answer).execute("test", { operation: "query", query: "symbol" }, undefined, undefined, { cwd: indexed } as ExtensionContext);
512
- assert.deepEqual(excludeLines(indexed), [".codegraph/"]);
513
- assert.equal(gitOutput(indexed, "status", "--porcelain", "--untracked-files=all"), "");
514
-
515
- const bare = workspace(t);
516
- const result = await createCodeGraphTool(answer).execute("test", { operation: "query", query: "symbol" }, undefined, undefined, { cwd: bare } as ExtensionContext);
517
- assert.deepEqual(excludeLines(bare), []);
518
- assert.equal((result.details as { localExclude?: unknown }).localExclude, undefined);
519
- });
520
-
521
- test("CodeGraph init still succeeds and reports when git is unavailable for the local exclusion", async (t) => {
522
- const cwd = workspace(t);
523
- const empty = realpathSync(mkdtempSync(join(tmpdir(), "gentle-pi-codegraph-nogit-")));
524
- const originalPath = process.env.PATH;
525
- t.after(() => {
526
- process.env.PATH = originalPath;
527
- rmSync(empty, { recursive: true, force: true });
528
- });
529
- const result = await createCodeGraphTool(async (args, options) => {
530
- const output = await writeIndex(args, options);
531
- process.env.PATH = empty; // git disappears after the workspace check
532
- return output;
533
- }).execute("test", { operation: "init" }, undefined, undefined, { cwd } as ExtensionContext);
534
- process.env.PATH = originalPath;
535
-
536
- assert.match(result.content[0].text, /^indexed\n\n/);
537
- assert.match(result.content[0].text, /\.codegraph\/ is not excluded from git locally/);
538
- assert.match(result.content[0].text, /keep \.codegraph\/ out of the change/i);
539
- assert.equal((result.details as { localExclude?: { status: string } }).localExclude?.status, "failed");
540
- assert.deepEqual(excludeLines(cwd), []);
541
- });
542
-
543
- test("CodeGraph excludes a partial index left behind by a failed init without changing the fallback text", async (t) => {
544
- const cwd = workspace(t);
545
- const result = await createCodeGraphTool(async (args, options) => {
546
- await writeIndex(args, options);
547
- throw new Error("CodeGraph exited with status 1");
548
- }).execute("test", { operation: "init" }, undefined, undefined, { cwd } as ExtensionContext);
549
-
550
- assert.deepEqual(result.content, [{ type: "text", text: "CodeGraph failed to run. Use read, grep, and find for this exploration." }]);
551
- assert.equal((result.details as { status: string }).status, "failed");
552
- assert.equal((result.details as { localExclude?: { status: string } }).localExclude?.status, "added");
553
- assert.equal(gitOutput(cwd, "status", "--porcelain", "--untracked-files=all"), "");
554
- });