@floydous/pi-agent-kernel 0.1.0 → 0.3.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 (35) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +78 -74
  3. package/docs/testing.md +10 -9
  4. package/grammars/tree-sitter-bash.wasm +0 -0
  5. package/grammars/tree-sitter-c.wasm +0 -0
  6. package/grammars/tree-sitter-c_sharp.wasm +0 -0
  7. package/grammars/tree-sitter-cpp.wasm +0 -0
  8. package/grammars/tree-sitter-go.wasm +0 -0
  9. package/grammars/tree-sitter-java.wasm +0 -0
  10. package/grammars/tree-sitter-javascript.wasm +0 -0
  11. package/grammars/tree-sitter-php.wasm +0 -0
  12. package/grammars/tree-sitter-python.wasm +0 -0
  13. package/grammars/tree-sitter-ruby.wasm +0 -0
  14. package/grammars/tree-sitter-rust.wasm +0 -0
  15. package/grammars/tree-sitter-tsx.wasm +0 -0
  16. package/grammars/tree-sitter-typescript.wasm +0 -0
  17. package/grammars/web-tree-sitter.wasm +0 -0
  18. package/package.json +11 -9
  19. package/src/dedup/content_store.ts +365 -365
  20. package/src/index.ts +918 -852
  21. package/src/lsp/lsp_formatter.ts +3 -1
  22. package/src/lsp/lsp_registry.ts +25 -1
  23. package/src/retrieval/ast_search.ts +930 -926
  24. package/src/retrieval/repomap.ts +220 -8
  25. package/src/retrieval/search_bm25.ts +190 -190
  26. package/src/retrieval/search_chunker.ts +254 -251
  27. package/src/retrieval/search_index.ts +22 -3
  28. package/src/retrieval/symbol_reader.ts +198 -200
  29. package/src/retrieval/tree_sitter_engine.ts +799 -0
  30. package/src/safety/epistemic_guard.ts +8 -1
  31. package/src/tools/ast_search_formatter.ts +161 -0
  32. package/src/tools/ast_search_tool.ts +17 -10
  33. package/src/tools/context.ts +20 -20
  34. package/src/tools/lsp_tool.ts +18 -5
  35. package/src/ui/tui_utils.ts +278 -278
package/CHANGELOG.md ADDED
@@ -0,0 +1,57 @@
1
+ # Changelog
2
+
3
+ ## [0.3.0] - 2026-09-07
4
+
5
+ ### Added
6
+
7
+ - Hierarchical grouped formatting for AST code search: search results are now cleanly organized by file and category, eliminating repetitive file path noise and significantly cutting down token usage.
8
+ - Bounded symbol body previews with clear truncation markers when previewing symbol contents.
9
+ - Strict cold-start symbol fallback coverage across 11 programming languages (TypeScript, JavaScript, Python, Rust, Go, Java, C#, C/C++, Ruby, PHP, and Bash), ensuring accurate symbol indexing even before syntax engines are fully loaded.
10
+ - Automated GitHub Actions CI workflow and release publishing workflows.
11
+
12
+ ### Changed
13
+
14
+ - Cleaner, copy-paste-ready symbol reading: reading specific code symbols now outputs clean code directly without line number prefixes, saving ~21% tokens per read and making edits directly applicable.
15
+ - Condensed AST search signatures: cleaned redundant keywords (like `function`, `public`, `def`) while preserving important modifiers (like `async`, `static`, `readonly`) and parameter types.
16
+ - Unified testing workflow: streamlined the entire test suite under a single command (`npm test`) organized by clear functional areas instead of numbered sections.
17
+
18
+ ### Fixed
19
+
20
+ - Fixed missing methods and functions in cold-state project indexing for TypeScript, JavaScript, C#, and C++.
21
+ - Fixed incorrect classification of Go receiver methods and PHP class methods during cold-state scans.
22
+ - Prevented internal local variables from mistakenly leaking into project symbol maps.
23
+ - Fixed misleading alias tags on typed methods and property getters/setters.
24
+ - Fixed temporary loading artifacts showing up in language server hover documentation.
25
+ - Improved code reference snippets when language servers return empty results.
26
+
27
+ ## [0.2.0] - 2026-09-05
28
+
29
+ This release replaces the old line-based symbol extraction path with a Tree-sitter WASM parser where a bundled grammar is available, while keeping the scanner fallback for unsupported or unavailable languages.
30
+
31
+ ### Added
32
+
33
+ - Tree-sitter WASM parsing for TypeScript, JavaScript, Python, Rust, Go, C, C++, Java, C#, Bash, Ruby, and PHP.
34
+ - AST-based symbol lookup and reference extraction that handles class methods and ignores identifiers inside comments and strings.
35
+ - Support for exact AST end lines when reading symbols and building search chunks.
36
+ - Parsing for additional constructs including abstract and ambient TypeScript declarations, Go interfaces, Rust traits, and C++ member methods.
37
+ - Regression coverage for the parser, cache, repository map, LSP lookup, and polyglot extraction paths.
38
+
39
+ ### Changed
40
+
41
+ - Grammars load on demand for languages found in the workspace instead of loading every grammar at startup.
42
+ - Search caches now record the extractor generation and reject caches produced by an older extractor.
43
+ - Repository-map startup waits for Tree-sitter initialization, including the `/repomap` command path.
44
+ - LSP executable lookup checks both the extension-local directory and the legacy user directory.
45
+ - Hover output removes transient `(loading...)` markers returned by language servers.
46
+ - `typebox` is now packaged as a runtime dependency.
47
+
48
+ ### Fixed
49
+
50
+ - Native Tree-sitter parse trees are released after extraction.
51
+ - Local variables no longer appear as top-level repository-map symbols.
52
+ - Braces in strings and comments no longer truncate symbol content.
53
+ - Syntax errors are reported on extracted file tags.
54
+ - LSP reference filtering preserves the intended fallback behavior.
55
+
56
+ [0.3.0]: https://github.com/floydous/pi-agent-kernel/compare/v0.2.0...v0.3.0
57
+ [0.2.0]: https://github.com/floydous/pi-agent-kernel/releases/tag/v0.2.0
package/README.md CHANGED
@@ -1,108 +1,112 @@
1
- # Pi agent kernel
1
+ # pi-agent-kernel
2
2
 
3
- A Pi extension for code retrieval, surgical editing, safety checks, and language-server support.
3
+ ![Comparison Demonstration](https://raw.githubusercontent.com/floydous/pi-agent-kernel/master/static/comparison.gif)
4
4
 
5
- ## Design priorities
5
+ Most coding agents drown in context. They read entire files when looking for a single function, dump thousands of lines of terminal logs into prompts, and rewrite full files just to tweak one line. `pi-agent-kernel` is built around a straightforward rule: **spend the bare minimum tokens necessary to get maximum workflow performance**. It provides surgical retrieval, bounded outputs, and guarded editing tools so your model stays fast, focused, and well within budget.
6
6
 
7
- The extension keeps agent interactions small while improving reliability. It uses focused retrieval, bounded output, deterministic checks, and grounded edits. It avoids loading unnecessary context, doing background work without a reason, or making speculative changes.
7
+ ---
8
8
 
9
- ## Token and performance measurements
9
+ ## Token savings
10
10
 
11
- The table compares this extension with an unconstrained harness that dumps whole files, walks the entire repository, rewrites files for every edit, and streams unbounded terminal output.
11
+ Here is how tool overhead compares to an unconstrained agent harness across everyday coding tasks:
12
12
 
13
- | Capability / Interaction | Unconstrained Harness | `pi-agent-kernel` | Token / Overhead Delta | Latency |
13
+ | Interaction | Traditional agent harness | `pi-agent-kernel` | Tokens saved | Why |
14
14
  |---|---|---|---|---|
15
- | **Context Ingestion** (Workspace map) | Raw recursive tree / full dump (~92.8k tokens) | PageRank AST Repo Map (~1.0k tokens) | **-98.9%** context tokens | ~40 ms |
16
- | **Code Inspection** (Targeted function read) | Whole file read (~1.4k tokens) | AST Symbol Extraction (`read(symbol=...)`) (~428 tokens) | **-68.5%** prompt tokens | <1 ms |
17
- | **Code Mutation** (Surgical function patch) | Full file rewrite (~1.4k tokens in tool payload) | Surgical search/replace (`edit`) (~58 tokens) | **-95.7%** output tokens | <5 ms |
18
- | **Command Execution** (Large test/build output) | Unbounded stream (~27.5k tokens) | Bounded Clamp + Disk Spillover (~1.1k tokens) | **-95.9%** flood tokens prevented | Instant |
19
- | **Codebase Search** (Lexical / BM25 Query) | Linear `grep`/`find` disk scan | Inverted In-Memory BM25 Index | **0 MB** background RAM (Lean) | ~0.03 ms / query |
20
- | **Pre-Commit Safety** (Broken syntax gate) | Allowed to commit broken code | Local Fast AST Delimiter Gate | **Deterministic** failure block | Instant |
15
+ | **Project orientation** | Walks directory tree, reads dozens of files (~92.8k tokens) | PageRank AST repository map (~1.0k tokens) | **~99% fewer tokens** | Only index-relevant code symbols and signatures are loaded. |
16
+ | **Inspecting a function** | Reads the full file (~1.5k–4.0k tokens) | Surgical symbol read: `read(symbol="foo")` (~420 tokens) | **~70–90% fewer tokens** | Pulls just the target AST node; skips line-number prefixes. |
17
+ | **Patching code** | Re-emits entire file content (~2.0k+ tokens) | Surgical search/replace block (`edit`) (~60 tokens) | **~95% fewer tokens** | Only generates the exact lines that change. |
18
+ | **Running tests / builds** | Floods context with raw build logs (~20k+ tokens) | Clamped output with disk spillover (~1.0k tokens) | **~95% context saved** | Full logs are written to disk; agent sees head, tail, and log path. |
19
+ | **AST symbol search** | Repeated absolute paths per match (~1.8k tokens) | Hierarchical grouped layout (~1.2k tokens) | **~34% fewer tokens** | Groups matching symbols under shared files and kinds. |
21
20
 
22
- *These measurements come from `tests/benchmark_metrics.ts`, using this codebase with 45 source files and about 92.8k raw tokens.*
21
+ ---
23
22
 
24
- ## Architecture and directory layout
23
+ ## Quick start
25
24
 
26
- Runtime code lives under `src/`. Tests, documentation, and example configuration remain at the repository root.
25
+ ### 1. Install
27
26
 
28
- ```
29
- pi-agent-kernel/
30
- ├── src/
31
- │ ├── index.ts # Main extension entry point
32
- │ ├── config/ # Hierarchical TOML configuration loader
33
- │ ├── context/ # Context engineering and session repair
34
- │ ├── editing/ # Surgical patching and verification
35
- │ ├── lsp/ # Language Server Protocol integration
36
- │ ├── retrieval/ # AST, BM25, and semantic code search
37
- │ ├── safety/ # Epistemic guards and bounded execution
38
- │ ├── tools/ # Pi tool registrations
39
- │ └── ui/ # Terminal UI components
40
- ├── tests/ # Focused verification sections
41
- ├── docs/ # Project documentation and guides
42
- ├── config.toml # Live configuration
43
- └── package.json # Extension manifest
27
+ Install the extension directly with Pi's package manager:
28
+
29
+ ```bash
30
+ # Global install (recommended)
31
+ pi install npm:@floydous/pi-agent-kernel
32
+
33
+ # Or install locally for the current repository only
34
+ pi install -l npm:@floydous/pi-agent-kernel
44
35
  ```
45
36
 
46
- ## Tool suite
37
+ Restart Pi or start a new session. The extension registers its tools, guards, and status indicators automatically.
47
38
 
48
- | Tool | Purpose | Output Strategy |
49
- |---|---|---|
50
- | `read` | Content and surgical AST symbol inspection | Bounded, zero bloat, line-targeted |
51
- | `edit` | Exact and fuzzy surgical patching with a syntax gate | Empty on clean (`OK!`), diagnostic on `WARN/FAIL` |
52
- | `write` | Complete new-file authoring | Direct write |
53
- | `get_repo_map` | PageRank-ranked repository AST definitions | Pure code symbols without import noise (~1k tokens) |
54
- | `ast_search` | AST structure search across definitions | Concise `file:line [kind] signature` |
55
- | `code_search` | Hybrid BM25 and semantic AST chunk search | Compact `file:start-end (breadcrumb)` snippets |
56
- | `lsp` | Realtime Language Server Protocol queries | Compact `def`, `ref`, and structural symbols |
57
- | `recall` | Restore an exact deduplicated tool result by reference | Bare original output content |
58
- | `search_tools` | Deferred tool discovery | On-demand tool capability matching |
39
+ ---
59
40
 
60
- ## Safety and invariants
41
+ ### 2. Configure the retrieval engine (`/engine`)
61
42
 
62
- - **Epistemic Guard**: Requires inspection before editing an existing file. It rejects ungrounded edits with short, copyable instructions and preserves authorization across sequential mutations.
63
- - **Output Clamping**: Limits stdout and stderr so they do not fill the context window, while saving complete output to disk.
64
- - **Tool Result Deduplication**: Replaces byte-identical repeated output with a small `[=rN,sizeB,tool,paramsKey]` notice that can be recovered with `recall`.
65
- - **Bounded Syntax Verification**: Runs a fast local check on mutations before committing changes to disk.
43
+ `pi-agent-kernel` includes an in-memory retrieval engine for keyword and semantic searches:
66
44
 
67
- ## Installation
45
+ ```text
46
+ /engine status
47
+ ```
68
48
 
69
- Install the extension as a Pi package, either globally or in a project:
49
+ - **`lean`** *(default)*: Fast, AST-aware BM25 search. Consumes 0 MB background model RAM.
50
+ - **`hybrid`**: BM25 keyword search blended with lightweight local 256-dimension embeddings.
51
+ - **`full`**: Dense 768-dimension semantic embeddings for deep conceptual queries across large codebases.
52
+ - **`off`**: Turns off local index building if you only want AST and LSP tools.
70
53
 
71
- ```bash
72
- pi install npm:@floydous/pi-agent-kernel
54
+ Switch profiles at any time:
55
+ ```text
56
+ /engine hybrid
57
+ # or
58
+ /engine full
73
59
  ```
74
60
 
75
- For a project-local installation:
61
+ ---
76
62
 
77
- ```bash
78
- pi install -l npm:@floydous/pi-agent-kernel
63
+ ### 3. Setup language servers (`/lsp`)
64
+
65
+ Get real-time compiler diagnostics, definitions, and references without manual setup:
66
+
67
+ ```text
68
+ /lsp
79
69
  ```
80
70
 
81
- The entry point is declared in `package.json` under `pi.extensions` (`./src/index.ts`). Pi loads it when a session starts.
71
+ Running `/lsp` opens an interactive management screen showing active servers, connection status, and one-click installs for detected workspace languages.
72
+
73
+ Alternatively, install a server directly from the command line:
74
+ ```text
75
+ /lsp install <language> # e.g., python, typescript, rust, go, csharp, etc.
76
+ ```
82
77
 
83
- ## Slash commands
78
+ ---
84
79
 
85
- | Command | Args | Purpose |
86
- |---|---|---|
87
- | `/repomap` | `[budget]` | Render the AST and PageRank-ranked repository map. The default budget is 1024 tokens. |
88
- | `/engine` | `auto\|lean\|hybrid\|full\|off\|status\|reindex` | Change the retrieval profile or inspect engine state. `lean` is the default and uses AST-aware BM25; `hybrid` adds local embeddings. |
89
- | `/lsp` | `[install <lang>]` | Inspect active language servers or install one, for example `/lsp install python`. |
80
+ ### 4. Tip: Turn off Pi documentation when working on your own projects
81
+
82
+ By default, Pi loads system prompt instructions explaining how to extend Pi itself (extensions, themes, skills, and TUI APIs).
83
+
84
+ When you are working on your own software—such as a React app, a Python backend, or a Rust crate—those internal Pi instructions take up room in your prompt that you do not need. You can silence them for the current session with:
85
+
86
+ ```text
87
+ /pi-docs off
88
+ ```
90
89
 
91
- ## Configuration
90
+ Turn it back on with `/pi-docs on` whenever you switch back to hacking on Pi extensions.
92
91
 
93
- The loader resolves configuration per workspace. It looks for `agent-kernel/config.toml` or `config.toml` at the workspace root and then walks upward. The built-in defaults are in [`src/config/kernel_config.ts`](src/config/kernel_config.ts), and the example configuration is [`config.toml`](config.toml). See [`docs/configuration.md`](docs/configuration.md) for the full schema and override rules.
92
+ ---
94
93
 
95
- The top-level sections are `[retrieval]`, `[safety]`, `[lsp]`, and `[ui]`.
94
+ ## Tool reference
96
95
 
97
- ## Documentation
96
+ | Tool | What it does |
97
+ |---|---|
98
+ | `read` | Read specific lines or extract an exact function, class, or type via AST (`symbol="name"`). |
99
+ | `edit` | Apply surgical search/replace patches with built-in syntax checks. |
100
+ | `write` | Create new files or perform complete rewrites when explicitly requested. |
101
+ | `get_repo_map` | Retrieve a concise, PageRank-ranked symbol overview of the codebase (~1k tokens). |
102
+ | `ast_search` | Search declarations across files using Tree-sitter AST queries, grouped cleanly by file. |
103
+ | `code_search` | Hybrid keyword and semantic chunk search with breadcrumb locations. |
104
+ | `lsp` | Query definitions, references, type hover docs, and diagnostics directly from language servers. |
105
+ | `recall` | Retrieve the full text of a deduplicated output previously replaced by a short reference tag. |
106
+ | `search_tools` | Search and activate deferred tools on demand. |
98
107
 
99
- - [`docs/architecture.md`](docs/architecture.md): module map and request flow
100
- - [`docs/retrieval.md`](docs/retrieval.md): search profile semantics
101
- - [`docs/editing-and-verification.md`](docs/editing-and-verification.md): patch and verification pipeline
102
- - [`docs/lsp.md`](docs/lsp.md): LSP integration and registry
103
- - [`docs/configuration.md`](docs/configuration.md): TOML schema and precedence
104
- - [`docs/testing.md`](docs/testing.md): section-based test layout
108
+ ---
105
109
 
106
110
  ## License
107
111
 
108
- ISC. See [`LICENSE`](LICENSE).
112
+ ISC
package/docs/testing.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Testing
2
2
 
3
- The suite is split into focused section files under `tests/`. Each section is
3
+ The suite is split into focused suite directories under `tests/`. Each suite is
4
4
  independently runnable and uses isolated temporary workspaces where needed.
5
5
 
6
6
  ## Commands
@@ -9,24 +9,25 @@ independently runnable and uses isolated temporary workspaces where needed.
9
9
  # Full suite
10
10
  npm test
11
11
 
12
- # Direct section orchestrator
13
- npm run test:focused
14
-
15
12
  # One focused section
16
- npx tsx tests/section-13-epistemic-guard.ts
13
+ npx tsx tests/epistemic-guard/index.ts
17
14
  ```
18
15
 
19
- `npm test` runs the section orchestrator under `tests/`.
16
+ `npm test` runs the suite orchestrator under `tests/`.
20
17
 
21
18
  ## Verification policy
22
19
 
23
20
  Prefer the narrowest useful check first:
24
21
 
25
- 1. Run the focused section related to a change.
22
+ 1. Run the focused suite related to a change.
26
23
  2. Run bounded TypeScript/LSP diagnostics for changed implementation files.
27
24
  3. Run the full suite once the focused checks are understood.
28
25
  4. Review `git diff --check` and the final working-tree status.
29
26
 
30
27
  The suite covers AST extraction, repository maps, patching, syntax checks,
31
- session repair, retrieval, output clamping, LSP support,
32
- configuration, lifecycle integration, and post-edit verification.
28
+ session repair, retrieval, output clamping, LSP support, configuration,
29
+ lifecycle integration, and post-edit verification.
30
+
31
+ The `cold-process-ast` suite launches a fresh process per language and checks
32
+ required symbols, kinds, and local-scope exclusions. This catches warm singleton
33
+ state masking fallback defects.
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@floydous/pi-agent-kernel",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Focused code retrieval, editing, safety checks, and language-server support for Pi.",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -9,20 +9,17 @@
9
9
  "author": "floydous https://github.com/floydous",
10
10
  "scripts": {
11
11
  "test": "npx tsx tests/run-all.ts",
12
- "test:focused": "npx tsx tests/run-all.ts",
13
12
  "typecheck": "tsc --noEmit"
14
13
  },
15
14
  "dependencies": {
16
15
  "@huggingface/transformers": "^4.2.0",
17
- "diff": "^9.0.0"
18
- },
19
- "peerDependencies": {
20
- "typebox": "^1.3.16"
16
+ "diff": "^9.0.0",
17
+ "typebox": "^1.3.16",
18
+ "web-tree-sitter": "^0.26.12"
21
19
  },
22
20
  "devDependencies": {
23
21
  "@earendil-works/pi-coding-agent": "0.84.3",
24
22
  "tsx": "^4.23.13",
25
- "typebox": "^1.3.16",
26
23
  "typescript": "^7.0.2"
27
24
  },
28
25
  "overrides": {
@@ -40,9 +37,14 @@
40
37
  },
41
38
  "files": [
42
39
  "src",
40
+ "grammars",
43
41
  "README.md",
44
42
  "docs",
45
43
  "LICENSE",
46
- "config.toml"
47
- ]
44
+ "config.toml",
45
+ "CHANGELOG.md"
46
+ ],
47
+ "publishConfig": {
48
+ "access": "public"
49
+ }
48
50
  }