@floydous/pi-agent-kernel 0.3.31 → 0.4.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 (40) hide show
  1. package/AGENT_KERNEL_SYS_PROMPT.md +6 -4
  2. package/CHANGELOG.md +35 -4
  3. package/README.md +39 -74
  4. package/config.example.toml +36 -0
  5. package/docs/configuration.md +11 -3
  6. package/docs/retrieval.md +17 -1
  7. package/docs/testing.md +5 -3
  8. package/package.json +9 -7
  9. package/src/config/agent_kernel_command.ts +91 -91
  10. package/src/config/kernel_config.ts +179 -43
  11. package/src/config/toml.ts +15 -4
  12. package/src/context/session_repair.ts +179 -80
  13. package/src/editing/patch.ts +28 -16
  14. package/src/editing/smart_anchor.ts +29 -36
  15. package/src/editing/syntax-verify.ts +52 -18
  16. package/src/index.ts +257 -219
  17. package/src/lsp/lsp_client.ts +138 -23
  18. package/src/lsp/lsp_formatter.ts +61 -24
  19. package/src/lsp/lsp_installer.ts +277 -561
  20. package/src/lsp/lsp_manager.ts +1 -1
  21. package/src/lsp/lsp_registry.ts +110 -73
  22. package/src/retrieval/ast_search.ts +136 -172
  23. package/src/retrieval/repomap.ts +8 -47
  24. package/src/retrieval/search_bm25.ts +5 -2
  25. package/src/retrieval/search_cache.ts +69 -0
  26. package/src/retrieval/search_chunker.ts +21 -43
  27. package/src/retrieval/search_config.ts +75 -18
  28. package/src/retrieval/search_embedder.ts +8 -16
  29. package/src/retrieval/search_index.ts +923 -123
  30. package/src/retrieval/workspace_walker.ts +262 -0
  31. package/src/safety/atomic_write.ts +59 -20
  32. package/src/safety/epistemic_guard.ts +163 -43
  33. package/src/safety/output_clamper.ts +316 -74
  34. package/src/tools/code_search_output.ts +59 -23
  35. package/src/tools/code_search_tool.ts +42 -46
  36. package/src/tools/context.ts +15 -1
  37. package/src/tools/edit_tool.ts +124 -43
  38. package/src/tools/lsp_tool.ts +17 -4
  39. package/src/tools/read_tool.ts +48 -7
  40. package/config.toml +0 -31
@@ -117,7 +117,7 @@ export class LspManager {
117
117
  if (inFlight) return inFlight;
118
118
 
119
119
  // Resolve installed executable
120
- const resolved = resolveLspServer(langKey);
120
+ const resolved = resolveLspServer(langKey, [], [rootDir]);
121
121
  if (!resolved) return null;
122
122
 
123
123
  const client = new StdioLspClient(clientId, {
@@ -1,34 +1,29 @@
1
1
  import * as fs from "node:fs";
2
2
  import * as path from "node:path";
3
3
  import * as os from "node:os";
4
- import { execSync } from "node:child_process";
4
+ import { execFileSync } from "node:child_process";
5
5
  import type { LspServerConfig } from "./lsp_types";
6
6
  import { getPiHomeDir, loadKernelConfig, saveGlobalKernelConfig } from "../config";
7
7
  import { kernelDebug } from "../safety/kernel_debug";
8
8
  import { writeFileSyncAtomic } from "../safety/atomic_write";
9
9
 
10
- export function getExtensionLspBinDir(): string {
11
- // Confined to extensions/agent-kernel/bin/lsp by default
12
- const candidate1 = path.join(__dirname, "..", "..", "bin", "lsp");
13
- const candidate2 = path.join(process.cwd(), "bin", "lsp");
14
- const candidate = fs.existsSync(candidate1) ? candidate1 : fs.existsSync(candidate2) ? candidate2 : candidate1;
15
-
16
- // Test write permission; if read-only (e.g. system-wide npm global install), fall back to ~/.pi/lsp/bin
10
+ function usableDirectory(candidate: string): string | null {
17
11
  try {
18
- fs.mkdirSync(candidate, { recursive: true });
19
- const probe = path.join(candidate, ".write_test");
20
- fs.writeFileSync(probe, "");
21
- fs.unlinkSync(probe);
22
- return candidate;
12
+ if (fs.lstatSync(candidate).isSymbolicLink()) return null;
13
+ if (!fs.statSync(candidate).isDirectory()) return null;
14
+ fs.accessSync(candidate, fs.constants.W_OK);
15
+ return fs.realpathSync(candidate);
23
16
  } catch {
24
- const userFallback = path.join(getPiHomeDir(), "lsp", "bin");
25
- try {
26
- fs.mkdirSync(userFallback, { recursive: true });
27
- } catch {}
28
- return userFallback;
17
+ return null;
29
18
  }
30
19
  }
31
20
 
21
+ export function getExtensionLspBinDir(): string {
22
+ const packageDir = path.join(__dirname, "..", "..", "bin", "lsp");
23
+ const userDir = path.join(getPiHomeDir(), "lsp", "bin");
24
+ return usableDirectory(packageDir) || usableDirectory(userDir) || path.resolve(userDir);
25
+ }
26
+
32
27
  export const PI_LSP_BIN_DIR = getExtensionLspBinDir();
33
28
 
34
29
  export const PI_LSP_CONFIG_FILE = path.join(
@@ -106,7 +101,7 @@ export const LSP_SERVERS: Record<string, LspServerConfig> = {
106
101
  { bin: "oxc_language_server", args: [] },
107
102
  { bin: "biome", args: ["lsp-proxy"] },
108
103
  ],
109
- installHint: "npm install -g @vtsls/language-server typescript",
104
+ installHint: "Run /lsp install typescript (pins vtsls 0.3.0 and TypeScript 7.0.2)",
110
105
  },
111
106
  python: {
112
107
  languageId: "python",
@@ -127,21 +122,21 @@ export const LSP_SERVERS: Record<string, LspServerConfig> = {
127
122
  { bin: "pylyzer", args: ["--server"] },
128
123
  { bin: "pylsp", args: [] },
129
124
  ],
130
- installHint: "uv tool install ty OR npm i -g pyright",
125
+ installHint: "Run /lsp install python (pins ty 0.0.83 with Pyright 1.1.414 fallback)",
131
126
  },
132
127
  rust: {
133
128
  languageId: "rust",
134
129
  extensions: [".rs"],
135
130
  markers: ["Cargo.toml", "Cargo.lock"],
136
131
  commands: [{ bin: "rust-analyzer", args: [] }],
137
- installHint: "rustup component add rust-analyzer",
132
+ installHint: "Install rust-analyzer through a reviewed rustup toolchain, then run /lsp install rust",
138
133
  },
139
134
  go: {
140
135
  languageId: "go",
141
136
  extensions: [".go"],
142
137
  markers: ["go.mod", "go.work"],
143
138
  commands: [{ bin: "gopls", args: [] }],
144
- installHint: "go install golang.org/x/tools/gopls@latest",
139
+ installHint: "Run /lsp install go (pins gopls v0.23.0)",
145
140
  },
146
141
  cpp: {
147
142
  languageId: "cpp",
@@ -149,14 +144,14 @@ export const LSP_SERVERS: Record<string, LspServerConfig> = {
149
144
  markers: ["compile_commands.json", "CMakeLists.txt", ".clangd"],
150
145
  commands: [{ bin: "clangd", args: ["--background-index", "--clang-tidy"] }],
151
146
  installHint:
152
- "Install LLVM / Clang (brew install llvm / winget install LLVM.LLVM)",
147
+ "Install reviewed LLVM/clangd 23.1.2, then run /lsp install cpp",
153
148
  },
154
149
  zig: {
155
150
  languageId: "zig",
156
151
  extensions: [".zig"],
157
152
  markers: ["build.zig"],
158
153
  commands: [{ bin: "zls", args: [] }],
159
- installHint: "Download ZLS binary or install via zig package manager",
154
+ installHint: "Install reviewed ZLS 0.16.0, then run /lsp install zig",
160
155
  },
161
156
  elixir: {
162
157
  languageId: "elixir",
@@ -178,7 +173,7 @@ export const LSP_SERVERS: Record<string, LspServerConfig> = {
178
173
  { bin: "csharp-ls", args: [] },
179
174
  { bin: "OmniSharp", args: ["-lsp"] },
180
175
  ],
181
- installHint: "dotnet tool install -g csharp-ls",
176
+ installHint: "Run /lsp install csharp (pins csharp-ls 0.28.0)",
182
177
  },
183
178
  ruby: {
184
179
  languageId: "ruby",
@@ -188,14 +183,14 @@ export const LSP_SERVERS: Record<string, LspServerConfig> = {
188
183
  { bin: "ruby-lsp", args: ["--stdio"] },
189
184
  { bin: "solargraph", args: ["stdio"] },
190
185
  ],
191
- installHint: "gem install ruby-lsp",
186
+ installHint: "Run /lsp install ruby (pins ruby-lsp 0.26.11)",
192
187
  },
193
188
  lua: {
194
189
  languageId: "lua",
195
190
  extensions: [".lua"],
196
191
  markers: [".luarc.json", ".luacheckrc"],
197
192
  commands: [{ bin: "lua-language-server", args: [] }],
198
- installHint: "Install lua-language-server (brew / winget / release binary)",
193
+ installHint: "Install reviewed Lua Language Server 3.19.1, then run /lsp install lua",
199
194
  },
200
195
  java: {
201
196
  languageId: "java",
@@ -232,28 +227,28 @@ export const LSP_SERVERS: Record<string, LspServerConfig> = {
232
227
  { bin: "intelephense", args: ["--stdio"] },
233
228
  { bin: "phpactor", args: ["language-server"] },
234
229
  ],
235
- installHint: "npm install -g intelephense",
230
+ installHint: "Run /lsp install php (pins Intelephense 1.18.5)",
236
231
  },
237
232
  shell: {
238
233
  languageId: "shellscript",
239
234
  extensions: [".sh", ".bash", ".zsh"],
240
235
  markers: [".shellcheckrc"],
241
236
  commands: [{ bin: "bash-language-server", args: ["start"] }],
242
- installHint: "npm install -g bash-language-server",
237
+ installHint: "Run /lsp install shell (pins bash-language-server 5.8.1)",
243
238
  },
244
239
  typst: {
245
240
  languageId: "typst",
246
241
  extensions: [".typ"],
247
242
  markers: [],
248
243
  commands: [{ bin: "tinymist", args: [] }],
249
- installHint: "cargo install --locked tinymist",
244
+ installHint: "Run /lsp install typst (pins Tinymist 0.15.2)",
250
245
  },
251
246
  latex: {
252
247
  languageId: "latex",
253
248
  extensions: [".tex", ".latex", ".bib"],
254
249
  markers: [".latexmkrc", "latexmkrc"],
255
250
  commands: [{ bin: "texlab", args: [] }],
256
- installHint: "cargo install texlab",
251
+ installHint: "Run /lsp install latex (pins Texlab 4.3.2)",
257
252
  },
258
253
  markdown: {
259
254
  languageId: "markdown",
@@ -264,7 +259,7 @@ export const LSP_SERVERS: Record<string, LspServerConfig> = {
264
259
  { bin: "markdown-oxide", args: [] },
265
260
  ],
266
261
  installHint:
267
- "winget install Artempyanykh.Marksman OR cargo install markdown-oxide",
262
+ "Install reviewed Marksman 2026-02-08, then run /lsp install markdown",
268
263
  },
269
264
  protobuf: {
270
265
  languageId: "proto",
@@ -274,7 +269,7 @@ export const LSP_SERVERS: Record<string, LspServerConfig> = {
274
269
  { bin: "protols", args: [] },
275
270
  { bin: "buf", args: ["beta", "lsp"] },
276
271
  ],
277
- installHint: "cargo install protols OR install buf",
272
+ installHint: "Run /lsp install protobuf (pins Protols 0.14.1)",
278
273
  },
279
274
  sql: {
280
275
  languageId: "sql",
@@ -286,7 +281,7 @@ export const LSP_SERVERS: Record<string, LspServerConfig> = {
286
281
  { bin: "sql-language-server", args: ["up", "--method", "stdio"] },
287
282
  ],
288
283
  installHint:
289
- "cargo install sqruff OR go install github.com/sqls-server/sqls@latest",
284
+ "Run /lsp install sql (pins sqls v0.2.48)",
290
285
  },
291
286
  nix: {
292
287
  languageId: "nix",
@@ -307,7 +302,7 @@ export const LSP_SERVERS: Record<string, LspServerConfig> = {
307
302
  { bin: "vscode-html-language-server", args: ["--stdio"] },
308
303
  ],
309
304
  installHint:
310
- "Download superhtml (Zig) or npm install -g vscode-langservers-extracted",
305
+ "Run /lsp install html (pins vscode-langservers-extracted 4.10.0)",
311
306
  },
312
307
  json: {
313
308
  languageId: "json",
@@ -318,28 +313,28 @@ export const LSP_SERVERS: Record<string, LspServerConfig> = {
318
313
  { bin: "vscode-json-language-server", args: ["--stdio"] },
319
314
  ],
320
315
  installHint:
321
- "npm install -g @biomejs/biome OR vscode-langservers-extracted",
316
+ "Run /lsp install json (pins Biome 2.5.14 with vscode fallback)",
322
317
  },
323
318
  yaml: {
324
319
  languageId: "yaml",
325
320
  extensions: [".yaml", ".yml"],
326
321
  markers: [],
327
322
  commands: [{ bin: "yaml-language-server", args: ["--stdio"] }],
328
- installHint: "npm install -g yaml-language-server",
323
+ installHint: "Run /lsp install yaml (pins yaml-language-server 1.24.0)",
329
324
  },
330
325
  toml: {
331
326
  languageId: "toml",
332
327
  extensions: [".toml"],
333
328
  markers: ["Cargo.toml", "pyproject.toml"],
334
329
  commands: [{ bin: "taplo", args: ["lsp", "stdio"] }],
335
- installHint: "cargo install taplo-cli --locked --features lsp",
330
+ installHint: "Run /lsp install toml (pins Taplo CLI 0.10.0)",
336
331
  },
337
332
  docker: {
338
333
  languageId: "dockerfile",
339
334
  extensions: ["Dockerfile", ".dockerfile"],
340
335
  markers: ["Dockerfile", "compose.yaml", "docker-compose.yml"],
341
336
  commands: [{ bin: "docker-langserver", args: ["--stdio"] }],
342
- installHint: "npm install -g dockerfile-language-server-nodejs",
337
+ installHint: "Run /lsp install docker (pins dockerfile-language-server-nodejs 0.15.0)",
343
338
  },
344
339
  terraform: {
345
340
  languageId: "terraform",
@@ -363,7 +358,7 @@ function isBinaryUsable(fullPath: string, binName: string): boolean {
363
358
  // but fail to execute if the component was never installed in the active toolchain.
364
359
  if (binName === "rust-analyzer") {
365
360
  try {
366
- execSync(`"${fullPath}" --version`, {
361
+ execFileSync(fullPath, ["--version"], {
367
362
  stdio: ["ignore", "ignore", "ignore"],
368
363
  timeout: 1500,
369
364
  });
@@ -378,12 +373,45 @@ function isBinaryUsable(fullPath: string, binName: string): boolean {
378
373
  /**
379
374
  * Check if an executable exists in system PATH or ~/.pi/lsp/bin
380
375
  */
376
+ function canonicalDirectory(directory: string): string | null {
377
+ try {
378
+ return fs.realpathSync(path.resolve(directory));
379
+ } catch {
380
+ return null;
381
+ }
382
+ }
383
+
384
+ function pathContains(root: string, target: string): boolean {
385
+ const relative = path.relative(root, target);
386
+ return relative === "" || (
387
+ relative !== ".." &&
388
+ !relative.startsWith(`..${path.sep}`) &&
389
+ !path.isAbsolute(relative)
390
+ );
391
+ }
392
+
393
+ function isForbiddenDirectory(directory: string, forbiddenRoots: string[]): boolean {
394
+ const canonical = canonicalDirectory(directory);
395
+ if (!canonical) return true;
396
+ return forbiddenRoots.some((root) => {
397
+ const forbidden = canonicalDirectory(root);
398
+ return forbidden ? pathContains(forbidden, canonical) : false;
399
+ });
400
+ }
401
+
381
402
  export function findExecutable(
382
403
  binName: string,
383
404
  extraDirs: string[] = [],
405
+ forbiddenRoots: string[] = [],
384
406
  ): string | null {
385
- if (executableCache.has(binName)) {
386
- return executableCache.get(binName) || null;
407
+ const cacheKey = JSON.stringify([
408
+ binName,
409
+ extraDirs.map((dir) => path.resolve(dir)),
410
+ forbiddenRoots.map((dir) => path.resolve(dir)),
411
+ process.platform,
412
+ ]);
413
+ if (executableCache.has(cacheKey)) {
414
+ return executableCache.get(cacheKey) || null;
387
415
  }
388
416
 
389
417
  const isWindows = process.platform === "win32";
@@ -436,7 +464,7 @@ export function findExecutable(
436
464
 
437
465
  // 1. Direct path check in extraDirs and ~/.pi/lsp/bin
438
466
  for (const dir of searchDirs) {
439
- if (!dir || !fs.existsSync(dir)) continue;
467
+ if (!dir || !fs.existsSync(dir) || isForbiddenDirectory(dir, forbiddenRoots)) continue;
440
468
  const candidates = isWindows
441
469
  ? [`${binName}.cmd`, `${binName}.exe`, `${binName}.bat`, binName]
442
470
  : [binName];
@@ -448,7 +476,7 @@ export function findExecutable(
448
476
  if (!isBinaryUsable(fullPath, binName)) {
449
477
  continue;
450
478
  }
451
- executableCache.set(binName, fullPath);
479
+ executableCache.set(cacheKey, fullPath);
452
480
  return fullPath;
453
481
  }
454
482
  } catch (e) {
@@ -457,39 +485,47 @@ export function findExecutable(
457
485
  }
458
486
  }
459
487
 
460
- // 2. System PATH lookup via which/where
461
- try {
462
- const cmd = isWindows ? `where "${binName}"` : `which "${binName}"`;
463
- const stdout = execSync(cmd, {
464
- stdio: ["pipe", "pipe", "ignore"],
465
- encoding: "utf8",
466
- timeout: 800,
467
- });
468
- const matches = stdout
469
- .split(/\r?\n/)
470
- .map((l) => l.trim())
471
- .filter((l) => l.length > 0 && fs.existsSync(l));
472
-
473
- if (matches.length > 0) {
474
- let selected = matches[0];
475
- if (isWindows) {
476
- // On Windows, prefer .cmd / .exe / .bat over extensionless bash scripts
477
- const preferred = matches.find((m) =>
478
- [".cmd", ".exe", ".bat"].some((ext) => m.toLowerCase().endsWith(ext)),
479
- );
480
- if (preferred) selected = preferred;
481
- }
488
+ // 2. System PATH lookup without shell execution
489
+ const pathEnv = process.env.PATH || process.env.Path || "";
490
+ const pathDirs = pathEnv.split(path.delimiter).map((d) => d.trim()).filter(Boolean);
482
491
 
483
- if (isBinaryUsable(selected, binName)) {
484
- executableCache.set(binName, selected);
485
- return selected;
492
+ const pathext = (isWindows ? (process.env.PATHEXT || ".COM;.EXE;.BAT;.CMD") : "")
493
+ .split(";")
494
+ .map((e) => e.trim().toLowerCase())
495
+ .filter(Boolean);
496
+
497
+ for (const dir of pathDirs) {
498
+ if (!dir || !fs.existsSync(dir) || isForbiddenDirectory(dir, forbiddenRoots)) continue;
499
+ const candidates = isWindows
500
+ ? [
501
+ ...pathext.map((ext) => `${binName}${ext}`),
502
+ `${binName}.cmd`,
503
+ `${binName}.exe`,
504
+ `${binName}.bat`,
505
+ binName,
506
+ ]
507
+ : [binName];
508
+
509
+ for (const cand of candidates) {
510
+ const fullPath = path.join(dir, cand);
511
+ try {
512
+ if (fs.existsSync(fullPath)) {
513
+ const stat = fs.statSync(fullPath);
514
+ if (stat.isFile()) {
515
+ if (!isBinaryUsable(fullPath, binName)) {
516
+ continue;
517
+ }
518
+ executableCache.set(cacheKey, fullPath);
519
+ return fullPath;
520
+ }
521
+ }
522
+ } catch (e) {
523
+ kernelDebug(e);
486
524
  }
487
525
  }
488
- } catch (e) {
489
- kernelDebug(e);
490
526
  }
491
527
 
492
- executableCache.set(binName, null);
528
+ executableCache.set(cacheKey, null);
493
529
  return null;
494
530
  }
495
531
 
@@ -506,6 +542,7 @@ export function clearExecutableCache(): void {
506
542
  export function resolveLspServer(
507
543
  languageOrExt: string,
508
544
  extraDirs: string[] = [],
545
+ forbiddenRoots: string[] = [],
509
546
  ): {
510
547
  binPath: string;
511
548
  command: string;
@@ -547,7 +584,7 @@ export function resolveLspServer(
547
584
 
548
585
  // Try each configured command in priority order
549
586
  for (const cmd of config.commands) {
550
- const binPath = findExecutable(cmd.bin, extraDirs);
587
+ const binPath = findExecutable(cmd.bin, extraDirs, forbiddenRoots);
551
588
  if (binPath) {
552
589
  return {
553
590
  binPath,