@floydous/pi-agent-kernel 0.5.0 → 0.6.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 (37) hide show
  1. package/AGENT_KERNEL_SYS_PROMPT.md +20 -6
  2. package/CHANGELOG.md +69 -0
  3. package/README.md +23 -20
  4. package/config.example.toml +1 -0
  5. package/docs/configuration.md +1 -1
  6. package/docs/editing-and-verification.md +20 -5
  7. package/docs/podman-linux-checks.md +24 -0
  8. package/docs/release-discipline.md +10 -0
  9. package/docs/retrieval.md +14 -11
  10. package/grammars/tree-sitter-html.wasm +0 -0
  11. package/package.json +2 -2
  12. package/src/config/agent_kernel_command.ts +60 -593
  13. package/src/config/kernel_config.ts +46 -2
  14. package/src/editing/patch.ts +48 -21
  15. package/src/editing/post_edit_verification.ts +8 -4
  16. package/src/editing/smart_anchor.ts +2 -1
  17. package/src/editing/syntax-verify.ts +73 -220
  18. package/src/index.ts +105 -203
  19. package/src/retrieval/html_chunker.ts +526 -0
  20. package/src/retrieval/search_bm25.ts +36 -27
  21. package/src/retrieval/search_chunker.ts +68 -6
  22. package/src/retrieval/search_config.ts +17 -34
  23. package/src/retrieval/search_embedder.ts +11 -29
  24. package/src/retrieval/search_index.ts +307 -145
  25. package/src/retrieval/search_modal.ts +1247 -222
  26. package/src/retrieval/tag_extractor.ts +1 -1
  27. package/src/retrieval/tree_sitter_engine.ts +103 -4
  28. package/src/retrieval/workspace_walker.ts +72 -0
  29. package/src/safety/host_environment.ts +190 -0
  30. package/src/safety/output_clamper.ts +83 -19
  31. package/src/tools/code_search_output.ts +9 -1
  32. package/src/tools/code_search_tool.ts +51 -14
  33. package/src/tools/context.ts +15 -0
  34. package/src/tools/edit_tool.ts +306 -79
  35. package/src/tools/read_tool.ts +248 -39
  36. package/src/tools/stream_reader.ts +206 -0
  37. package/src/ui/tui_utils.ts +70 -12
@@ -11,7 +11,9 @@ export const AUTO_OUTPUT_BUDGET = 8_000;
11
11
  const PREVIEW_LINE_LIMIT = 12;
12
12
  const PREVIEW_CHAR_LIMIT = 1_400;
13
13
  const AUTO_FULL_CHAR_LIMIT = 6_000;
14
- const AUTO_PREVIEW_HIT_LIMIT = 3;
14
+ const AUTO_PREVIEW_HIT_LIMIT = 5;
15
+ /** A previewed chunk this small is shown whole: a 12-line window often misses the relevant lines of a mid-size body. */
16
+ const PREVIEW_WHOLE_CHUNK_CHAR_LIMIT = 2_000;
15
17
 
16
18
  export interface VisibleCodeRange {
17
19
  chunkId: string;
@@ -88,6 +90,12 @@ function queryPreview(
88
90
  hit: SearchHit,
89
91
  query: string,
90
92
  ): { text: string; visibleRange?: VisibleCodeRange } {
93
+ if (hit.chunk.content.length <= PREVIEW_WHOLE_CHUNK_CHAR_LIMIT) {
94
+ return {
95
+ text: `${hitHeader(hit)}\n\`\`\`${languageFor(hit.chunk.filePath)}\n${hit.chunk.content}\n\`\`\``,
96
+ visibleRange: { chunkId: hit.chunk.id, startLine: hit.chunk.startLine, endLine: hit.chunk.endLine },
97
+ };
98
+ }
91
99
  const lines = hit.chunk.content.split("\n");
92
100
  const terms = Array.from(new Set(tokenizeCode(query)));
93
101
  const bestLine = bestMatchingLine(lines, terms);
@@ -3,20 +3,20 @@ import { Type } from "typebox";
3
3
  import { Text, makeOutputText } from "../ui/tui_utils";
4
4
  import { globalEpistemicGuard } from "../safety/epistemic_guard";
5
5
  import { formatCodeSearchResults, CODE_SEARCH_MODES, type CodeSearchMode } from "./code_search_output";
6
- import type { SearchDeps } from "./context";
6
+ import { markReturnedErrors, type SearchDeps } from "./context";
7
7
 
8
8
  /** Extracted from index.ts — registers the `code_search` tool. */
9
9
  export function registerCodeSearchTool(
10
10
  pi: ExtensionAPI,
11
11
  deps: SearchDeps,
12
12
  ): void {
13
- // 5.5 Tool: `code_search` (Local Hybrid BM25 + Semantic AST Code Search)
14
- pi.registerTool({
13
+ // 5.5 Tool: `code_search` (Local AST-Chunk BM25 + Profile-Dependent Semantic Code Search)
14
+ pi.registerTool(markReturnedErrors({
15
15
  name: "code_search",
16
16
  label: "Codebase Search",
17
17
  description:
18
- "Fast BM25 & AST semantic search across code chunks. Best for conceptual questions, finding unfamiliar features, and searching when exact symbol names or file paths are unknown (prefer rg for exact text literals).",
19
- promptSnippet: "Search codebase conceptually or by keywords via hybrid AST index",
18
+ "AST-chunk BM25 keyword search across source code (with neural semantic fusion when the active profile enables embeddings). Use when locating features, workflows, UI behaviors, or architecture without known symbol names or file paths. Do NOT use for exact string literals, call-sites, or non-code files; use bash (rg -n) instead.",
19
+ promptSnippet: "Search code chunks by mechanism keywords or concepts (BM25, plus vectors in full/auto profile)",
20
20
  renderShell: "default",
21
21
  parameters: Type.Object({
22
22
  query: Type.String({
@@ -24,12 +24,28 @@ export function registerCodeSearchTool(
24
24
  }),
25
25
  file_pattern: Type.Optional(
26
26
  Type.String({
27
- description: "Optional relative path substring filter",
27
+ description: "Optional relative path substring filter (e.g., 'src/auth', 'tests'). Omit for whole-workspace search. Do NOT guess paths.",
28
28
  }),
29
29
  ),
30
30
  limit: Type.Optional(
31
31
  Type.Number({
32
- description: "Max results (default: 5, max: 15)",
32
+ minimum: 1,
33
+ maximum: 15,
34
+ description: "Max results (default: 5, range: 1-15)",
35
+ }),
36
+ ),
37
+ mode: Type.Optional(
38
+ Type.Unsafe<"auto" | "full" | "preview" | "summary">({
39
+ type: "string",
40
+ enum: ["auto", "full", "preview", "summary"],
41
+ description: "Output detail level: 'auto' (adaptive), 'full' (complete chunk bodies), 'preview' (compact snippets), or 'summary' (headers only)",
42
+ }),
43
+ ),
44
+ rrf_k: Type.Optional(
45
+ Type.Number({
46
+ minimum: 1,
47
+ maximum: 200,
48
+ description: "Optional Reciprocal Rank Fusion smoothing constant (1-200, default: 60)",
33
49
  }),
34
50
  ),
35
51
  }),
@@ -82,28 +98,48 @@ export function registerCodeSearchTool(
82
98
  limit,
83
99
  filePattern: params.file_pattern,
84
100
  rrfK: params.rrf_k,
101
+ separateTests: true,
85
102
  });
86
103
 
87
104
  const indexStatus = typeof index.getStatus === "function" ? index.getStatus() : undefined;
88
- const wantsVectors =
89
- indexStatus?.effectiveProfile === "hybrid" ||
90
- indexStatus?.effectiveProfile === "full";
105
+ const wantsVectors = indexStatus?.effectiveProfile === "full";
91
106
  const vectorNotice =
92
107
  wantsVectors &&
93
108
  indexStatus &&
94
109
  indexStatus.vectorCount < indexStatus.chunkCount
95
110
  ? `[Notice: Vector embeddings are incomplete (${indexStatus.vectorCount}/${indexStatus.chunkCount} chunks); searched via BM25]\n\n`
96
111
  : "";
112
+ const grammarIssues = indexStatus?.grammarIssues ?? [];
113
+ const grammarNotice = grammarIssues.length > 0
114
+ ? `[Notice: tree-sitter grammar unavailable for ${grammarIssues.map((issue) => `${issue.extensions.join(", ")} (${issue.reason})`).join("; ")}; code structure for those files is approximate]\n\n`
115
+ : "";
116
+ const notIndexed = indexStatus?.notIndexed;
117
+ const missingFiles = notIndexed ? notIndexed.source + notIndexed.test + notIndexed.vendored : 0;
118
+ const count = (n: number) => n.toLocaleString("en-US");
119
+ const coverageNotice = indexStatus && notIndexed && missingFiles > 0
120
+ ? `[Notice: index covers ${count(indexStatus.indexableFiles - missingFiles)} of ${indexStatus.walkTruncated ? "≥ " : ""}${count(indexStatus.indexableFiles)} indexable files (index limits); not indexed: ${(["source", "test", "vendored"] as const).filter((tier) => notIndexed[tier] > 0).map((tier) => `${count(notIndexed[tier])} ${tier}`).join(", ")} (${indexStatus.notIndexedDirs.join(", ")}); use rg for those paths]\n\n`
121
+ : "";
97
122
 
98
123
  if (hits.length === 0) {
124
+ // Blame the filter, not the query, when it excludes every indexed file (e.g. "README": docs are not indexed).
125
+ const filterExcludesIndex = params.file_pattern && indexStatus && indexStatus.chunkCount > 0 &&
126
+ typeof index.hasIndexedFile === "function" && !index.hasIndexedFile(params.file_pattern);
127
+ let message: string;
128
+ if (filterExcludesIndex) {
129
+ message = `No indexed file path contains "${params.file_pattern}". code_search indexes source code only (not Markdown, JSON, YAML, TOML or other text files); use rg to search other files.`;
130
+ } else if (indexStatus?.effectiveProfile === "full") {
131
+ message = `No code chunks found matching "${query}". (Engine: Full [Neural RRF]. Tips: Try broader conceptual intent phrases if exact keywords failed; use 'rg' for exact text literals.)`;
132
+ } else {
133
+ message = `No code chunks found matching "${query}". (Engine: Lean [BM25 lexical]. Tips: Query with mechanism keywords or identifier patterns likely in source code; use 'rg' for exact text literals.)`;
134
+ }
99
135
  return {
100
136
  content: [
101
137
  {
102
138
  type: "text",
103
- text: `${vectorNotice}No code chunks found matching "${query}". (Tips: Try broader or different terms, or 'rg' for exact text literals.)`,
139
+ text: `${coverageNotice}${grammarNotice}${vectorNotice}${message}`,
104
140
  },
105
141
  ],
106
- details: { count: 0 },
142
+ details: { count: 0, effectiveProfile: indexStatus?.effectiveProfile || "lean" },
107
143
  };
108
144
  }
109
145
 
@@ -148,11 +184,12 @@ export function registerCodeSearchTool(
148
184
  content: [
149
185
  {
150
186
  type: "text",
151
- text: `${vectorNotice}${formatted.text}`,
187
+ text: `${coverageNotice}${grammarNotice}${vectorNotice}${formatted.text}`,
152
188
  },
153
189
  ],
154
190
  details: {
155
191
  count: hits.length,
192
+ effectiveProfile: indexStatus?.effectiveProfile || "lean",
156
193
  mode: formatted.mode,
157
194
  requestedMode,
158
195
  truncated: formatted.truncated,
@@ -184,5 +221,5 @@ export function registerCodeSearchTool(
184
221
  result.content?.find((c: any) => c.type === "text")?.text || "";
185
222
  return makeOutputText(`\n${theme.fg("toolOutput", output)}`);
186
223
  },
187
- });
224
+ }));
188
225
  }
@@ -12,6 +12,21 @@ export interface SessionDeps {
12
12
  invalidateSearchFile?: (cwd: string, filePath: string) => void | Promise<void>;
13
13
  }
14
14
 
15
+ /**
16
+ * pi flags a tool result as an error only when `execute` throws; a returned `isError` is dropped. Kernel tools return
17
+ * their failures, so copy the flag into `details.isError`, where the kernel's tool_result hook restores it.
18
+ */
19
+ export function markReturnedErrors<T extends { execute: (...args: any[]) => Promise<any> }>(tool: T): T {
20
+ const execute = tool.execute;
21
+ return {
22
+ ...tool,
23
+ async execute(...args: any[]) {
24
+ const result = await execute.apply(tool, args);
25
+ return result?.isError === true ? { ...result, details: { ...(result.details ?? {}), isError: true } } : result;
26
+ },
27
+ };
28
+ }
29
+
15
30
  export interface SearchDeps extends SessionDeps {
16
31
  /** Lazily create/access the workspace hybrid search index singleton. */
17
32
  getSearchIndex: (