@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
@@ -1,12 +1,24 @@
1
1
  import * as os from "node:os";
2
2
  import * as fs from "node:fs";
3
3
  import * as path from "node:path";
4
- import { getPiHomeDir, loadKernelConfig } from "../config";
4
+ import {
5
+ getPiHomeDir,
6
+ loadKernelConfig,
7
+ saveProjectKernelConfig,
8
+ saveGlobalKernelConfig,
9
+ getProjectConfigPath,
10
+ getGlobalConfigPath,
11
+ getGlobalRawConfig,
12
+ } from "../config";
5
13
  import { kernelDebug } from "../safety/kernel_debug";
6
14
  import { writeFileSyncAtomic } from "../safety/atomic_write";
15
+ import { parseToml } from "../config/toml";
7
16
 
8
17
  export type SearchProfile = "lean" | "hybrid" | "full" | "off" | "auto";
9
18
 
19
+ export const EMBEDDING_MODEL_ID = "nomic-ai/nomic-embed-text-v1.5";
20
+ export const EMBEDDING_MODEL_REVISION = "e9b6763023c676ca8431644204f50c2b100d9aab";
21
+
10
22
  export interface SearchConfig {
11
23
  profile: SearchProfile;
12
24
  effectiveProfile: "lean" | "hybrid" | "full" | "off";
@@ -15,6 +27,7 @@ export interface SearchConfig {
15
27
  vectorSimilarityThreshold: number;
16
28
  numThreads: number;
17
29
  modelId: string;
30
+ modelRevision: string;
18
31
  dtype: "q8" | "fp32" | "fp16";
19
32
  batchSize: number;
20
33
  sleepBetweenBatchesMs: number;
@@ -42,7 +55,37 @@ export function detectBestProfile(): "lean" | "hybrid" | "full" {
42
55
  /**
43
56
  * Load persisted search settings or default to config.toml setting.
44
57
  */
45
- export function loadPersistedProfile(): SearchProfile {
58
+ export function loadPersistedProfile(
59
+ cwd?: string,
60
+ projectTrusted = false,
61
+ ): SearchProfile {
62
+ // 1. Explicit trusted project-local config (.pi/config.toml only)
63
+ const projectPath = cwd && projectTrusted ? getProjectConfigPath(cwd) : null;
64
+ if (projectPath && fs.existsSync(projectPath)) {
65
+ try {
66
+ const raw = parseToml(fs.readFileSync(projectPath, "utf-8"));
67
+ const retrieval = raw.retrieval;
68
+ if (retrieval && typeof retrieval === "object" && !Array.isArray(retrieval)) {
69
+ const p = (retrieval as any).default_profile;
70
+ if (p && ["lean", "hybrid", "full", "off", "auto"].includes(p)) {
71
+ return p as SearchProfile;
72
+ }
73
+ }
74
+ } catch (e) {
75
+ kernelDebug(e);
76
+ }
77
+ }
78
+
79
+ // 2. Explicit global configuration (~/.pi/agent/config.toml)
80
+ const globalRaw = getGlobalRawConfig();
81
+ if (
82
+ globalRaw.retrieval?.default_profile &&
83
+ ["lean", "hybrid", "full", "off", "auto"].includes(globalRaw.retrieval.default_profile as string)
84
+ ) {
85
+ return globalRaw.retrieval.default_profile as SearchProfile;
86
+ }
87
+
88
+ // 3. Legacy search_settings.json fallback
46
89
  try {
47
90
  if (fs.existsSync(SETTINGS_FILE)) {
48
91
  const data = JSON.parse(fs.readFileSync(SETTINGS_FILE, "utf-8"));
@@ -56,23 +99,31 @@ export function loadPersistedProfile(): SearchProfile {
56
99
  } catch (e) {
57
100
  kernelDebug(e);
58
101
  }
59
- const kernelCfg = loadKernelConfig();
60
- return (kernelCfg.retrieval.default_profile as SearchProfile) || "lean";
102
+
103
+ // 4. Canonical hardcoded default fallback
104
+ return "lean";
61
105
  }
62
106
 
63
107
  /**
64
- * Persist search settings to disk.
108
+ * Persist search settings to disk and config.toml.
65
109
  */
66
- export function savePersistedProfile(profile: SearchProfile): void {
110
+ export function savePersistedProfile(profile: SearchProfile, cwd?: string): void {
67
111
  try {
68
- const dir = path.dirname(SETTINGS_FILE);
69
- if (!fs.existsSync(dir)) {
70
- fs.mkdirSync(dir, { recursive: true });
112
+ if (cwd) {
113
+ // Workspace-local profile update: writes only to project-local config.toml
114
+ saveProjectKernelConfig(cwd, { retrieval: { default_profile: profile as any } });
115
+ } else {
116
+ // Global profile update: writes to global ~/.pi/agent/config.toml and legacy search_settings.json
117
+ saveGlobalKernelConfig({ retrieval: { default_profile: profile as any } });
118
+ const dir = path.dirname(SETTINGS_FILE);
119
+ if (!fs.existsSync(dir)) {
120
+ fs.mkdirSync(dir, { recursive: true });
121
+ }
122
+ writeFileSyncAtomic(
123
+ SETTINGS_FILE,
124
+ JSON.stringify({ profile, updatedAt: new Date().toISOString() }, null, 2),
125
+ );
71
126
  }
72
- writeFileSyncAtomic(
73
- SETTINGS_FILE,
74
- JSON.stringify({ profile, updatedAt: new Date().toISOString() }, null, 2),
75
- );
76
127
  } catch (e) {
77
128
  kernelDebug(e);
78
129
  }
@@ -84,8 +135,10 @@ export function savePersistedProfile(profile: SearchProfile): void {
84
135
  */
85
136
  export function getSearchConfig(
86
137
  requestedProfile?: SearchProfile,
138
+ cwd?: string,
139
+ projectTrusted = false,
87
140
  ): SearchConfig {
88
- const profile = requestedProfile || loadPersistedProfile();
141
+ const profile = requestedProfile || loadPersistedProfile(cwd, projectTrusted);
89
142
  const effectiveProfile: "lean" | "hybrid" | "full" | "off" =
90
143
  profile === "auto" ? detectBestProfile() : profile || "lean";
91
144
 
@@ -98,7 +151,8 @@ export function getSearchConfig(
98
151
  matryoshkaDim: 0,
99
152
  vectorSimilarityThreshold: 0,
100
153
  numThreads: 1,
101
- modelId: "nomic-ai/nomic-embed-text-v1.5",
154
+ modelId: EMBEDDING_MODEL_ID,
155
+ modelRevision: EMBEDDING_MODEL_REVISION,
102
156
  dtype: "q8",
103
157
  batchSize: 1,
104
158
  sleepBetweenBatchesMs: 0,
@@ -115,7 +169,8 @@ export function getSearchConfig(
115
169
  matryoshkaDim: 0,
116
170
  vectorSimilarityThreshold: 0,
117
171
  numThreads: 1,
118
- modelId: "nomic-ai/nomic-embed-text-v1.5",
172
+ modelId: EMBEDDING_MODEL_ID,
173
+ modelRevision: EMBEDDING_MODEL_REVISION,
119
174
  dtype: "q8",
120
175
  batchSize: 1,
121
176
  sleepBetweenBatchesMs: 0,
@@ -134,7 +189,8 @@ export function getSearchConfig(
134
189
  // while cleanly blocking off-topic noise (unrelated queries score ~0.48-0.52).
135
190
  vectorSimilarityThreshold: 0.55,
136
191
  numThreads: 1,
137
- modelId: "nomic-ai/nomic-embed-text-v1.5",
192
+ modelId: EMBEDDING_MODEL_ID,
193
+ modelRevision: EMBEDDING_MODEL_REVISION,
138
194
  dtype: "q8",
139
195
  batchSize: 1,
140
196
  sleepBetweenBatchesMs: 80, // 80ms sleep between chunk batches to prevent CPU starvation
@@ -151,7 +207,8 @@ export function getSearchConfig(
151
207
  // Empirically calibrated floor for 768-dim embeddings.
152
208
  vectorSimilarityThreshold: 0.55,
153
209
  numThreads: Math.min(2, Math.max(1, Math.floor(os.cpus().length / 2))),
154
- modelId: "nomic-ai/nomic-embed-text-v1.5",
210
+ modelId: EMBEDDING_MODEL_ID,
211
+ modelRevision: EMBEDDING_MODEL_REVISION,
155
212
  dtype: "q8",
156
213
  batchSize: 2,
157
214
  sleepBetweenBatchesMs: 50,
@@ -108,22 +108,13 @@ export class LocalEmbedder {
108
108
  for (const dir of candidateDirs) {
109
109
  if (!fs.existsSync(dir)) continue;
110
110
 
111
- // Check for ONNX files or snapshot folders
112
- const onnxPath = path.join(dir, "onnx");
113
- if (fs.existsSync(onnxPath)) {
114
- const files = fs.readdirSync(onnxPath);
115
- if (files.some((f) => f.endsWith(".onnx"))) return true;
116
- }
117
-
118
- const snapshots = path.join(dir, "snapshots");
119
- if (fs.existsSync(snapshots)) {
120
- const files = fs.readdirSync(snapshots);
121
- if (files.length > 0) return true;
122
- }
123
-
124
- const rootFiles = fs.readdirSync(dir);
125
- if (rootFiles.some((f) => f.endsWith(".onnx") || f === "config.json")) {
126
- return true;
111
+ for (const revisionDir of [
112
+ path.join(dir, this.config.modelRevision),
113
+ path.join(dir, "snapshots", this.config.modelRevision),
114
+ ]) {
115
+ if (fs.existsSync(revisionDir) && fs.readdirSync(revisionDir).length > 0) {
116
+ return true;
117
+ }
127
118
  }
128
119
  }
129
120
  } catch (e) {
@@ -175,6 +166,7 @@ export class LocalEmbedder {
175
166
  this.layerNormFn = layer_norm;
176
167
 
177
168
  this.extractor = await pipeline("feature-extraction", this.config.modelId, {
169
+ revision: this.config.modelRevision,
178
170
  dtype: this.config.dtype,
179
171
  progress_callback: (p: any) => {
180
172
  if (p.status === "progress" && p.total && p.loaded) {