@orquentra/codebuddy 2.4.0 → 2.5.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 (61) hide show
  1. package/README.md +42 -1090
  2. package/dist/chunk-3XYQGFPB.js +91 -0
  3. package/dist/chunk-3XYQGFPB.js.map +1 -0
  4. package/dist/chunk-4BYGTEJT.js +103 -0
  5. package/dist/chunk-4BYGTEJT.js.map +1 -0
  6. package/dist/{chunk-ITGRANYW.js → chunk-6MKEIDX5.js} +2 -2
  7. package/dist/{chunk-K63EOA4B.js → chunk-776EQXDV.js} +25 -6
  8. package/dist/chunk-776EQXDV.js.map +1 -0
  9. package/dist/{chunk-5OWZVZSO.js → chunk-CQPCHMBS.js} +8 -165
  10. package/dist/chunk-CQPCHMBS.js.map +1 -0
  11. package/dist/chunk-JDZ3VK26.js +4006 -0
  12. package/dist/chunk-JDZ3VK26.js.map +1 -0
  13. package/dist/chunk-JMWJ7V6Y.js +99 -0
  14. package/dist/chunk-JMWJ7V6Y.js.map +1 -0
  15. package/dist/chunk-KYK6RPAJ.js +99 -0
  16. package/dist/chunk-KYK6RPAJ.js.map +1 -0
  17. package/dist/{chunk-IBMJYIEP.js → chunk-L5AYZC3F.js} +7 -7
  18. package/dist/{chunk-3ZELDCR3.js → chunk-PCE4STAY.js} +7 -7
  19. package/dist/{chunk-TI5TQD6D.js → chunk-QDBDYCM2.js} +2 -2
  20. package/dist/{chunk-DSN5CEHO.js → chunk-ROQTT5ZO.js} +3 -3
  21. package/dist/{chunk-IE2OUO54.js → chunk-UFZD3JAA.js} +3 -3
  22. package/dist/chunk-Z5QB367W.js +5028 -0
  23. package/dist/chunk-Z5QB367W.js.map +1 -0
  24. package/dist/cli/index.js +2 -2
  25. package/dist/cloud-GP6G3EAK.js +17 -0
  26. package/dist/{codebuddy-D2xJ6hFk.d.ts → codebuddy-DM-i0Fu5.d.ts} +49 -18
  27. package/dist/{config-S2TULOMK.js → config-KFEHLV6W.js} +3 -3
  28. package/dist/index.d.ts +23 -3
  29. package/dist/index.js +34 -25
  30. package/dist/langgraph/index.d.ts +1 -1
  31. package/dist/lean-command-DA4ZNKG3.js +15 -0
  32. package/dist/local-3NGNESNB.js +13 -0
  33. package/dist/{main-ZZ3EQLYA.js → main-BDVQAGAS.js} +1261 -242
  34. package/dist/main-BDVQAGAS.js.map +1 -0
  35. package/dist/{memory-NIBFAMY4.js → memory-4RGVLPKG.js} +6 -6
  36. package/dist/operations-EQGOFCBD.js +24 -0
  37. package/dist/operations-EQGOFCBD.js.map +1 -0
  38. package/dist/{rerank-Q7MIFNKA.js → rerank-3P3C2UHN.js} +4 -4
  39. package/dist/{summaries-TSE44KUQ.js → summaries-ZBV5IN7Y.js} +6 -6
  40. package/dist/summaries-ZBV5IN7Y.js.map +1 -0
  41. package/dist/{zvec-F27AR357.js → zvec-MAPSJQSR.js} +3 -3
  42. package/dist/zvec-MAPSJQSR.js.map +1 -0
  43. package/package.json +7 -3
  44. package/dist/chunk-5OWZVZSO.js.map +0 -1
  45. package/dist/chunk-K63EOA4B.js.map +0 -1
  46. package/dist/chunk-SYHB3AMM.js +0 -8580
  47. package/dist/chunk-SYHB3AMM.js.map +0 -1
  48. package/dist/lean-command-D6TZJEFX.js +0 -13
  49. package/dist/main-ZZ3EQLYA.js.map +0 -1
  50. /package/dist/{chunk-ITGRANYW.js.map → chunk-6MKEIDX5.js.map} +0 -0
  51. /package/dist/{chunk-IBMJYIEP.js.map → chunk-L5AYZC3F.js.map} +0 -0
  52. /package/dist/{chunk-3ZELDCR3.js.map → chunk-PCE4STAY.js.map} +0 -0
  53. /package/dist/{chunk-TI5TQD6D.js.map → chunk-QDBDYCM2.js.map} +0 -0
  54. /package/dist/{chunk-DSN5CEHO.js.map → chunk-ROQTT5ZO.js.map} +0 -0
  55. /package/dist/{chunk-IE2OUO54.js.map → chunk-UFZD3JAA.js.map} +0 -0
  56. /package/dist/{config-S2TULOMK.js.map → cloud-GP6G3EAK.js.map} +0 -0
  57. /package/dist/{lean-command-D6TZJEFX.js.map → config-KFEHLV6W.js.map} +0 -0
  58. /package/dist/{summaries-TSE44KUQ.js.map → lean-command-DA4ZNKG3.js.map} +0 -0
  59. /package/dist/{zvec-F27AR357.js.map → local-3NGNESNB.js.map} +0 -0
  60. /package/dist/{memory-NIBFAMY4.js.map → memory-4RGVLPKG.js.map} +0 -0
  61. /package/dist/{rerank-Q7MIFNKA.js.map → rerank-3P3C2UHN.js.map} +0 -0
package/README.md CHANGED
@@ -1,1119 +1,71 @@
1
1
  # CodeBuddy
2
2
 
3
- **A local-first project brain for AI coding agents.** CodeBuddy gives Claude
4
- Code and Codex durable memory *and* prepares the smallest useful slice of
5
- project context before every edit — so the agent works like a careful senior
6
- engineer instead of re-reading your whole repo on every turn.
3
+ Local-first project context and memory for AI coding agents.
7
4
 
8
- It runs on your machine. Knowledge lives as readable Markdown under
9
- `.codebuddy/`. PostgreSQL + pgvector is only a search index, never the source of
10
- truth. Nothing is sent to a hosted CodeBuddy service — there isn't one.
5
+ CodeBuddy helps agents find relevant code, understand project constraints, and
6
+ remember decisions across sessions. It works through MCP with Claude Code,
7
+ Codex, OpenCode, Hermes, and other compatible clients.
11
8
 
12
- - **npm:** `@orquentra/codebuddy` (v2.4.0)
13
- - **Surfaces:** CLI · MCP stdio server · TypeScript SDK · LangGraph helpers
9
+ ## Install
14
10
 
15
- `@orquentra/codebuddy` continues the project previously published as
16
- [`@ayushkumar320/codebuddy`](https://www.npmjs.com/package/@ayushkumar320/codebuddy).
17
- The original package remains available as the historical release archive, with
18
- its existing versions and download records. Future releases use the Orquentra
19
- package; npm tracks the two package names separately.
20
-
21
- ---
22
-
23
- ## Table of contents
24
-
25
- - [The problem it solves](#the-problem-it-solves)
26
- - [How it works (mental model)](#how-it-works-mental-model)
27
- - [Install & setup](#install--setup)
28
- - [Feature tour (with examples)](#feature-tour-with-examples)
29
- - [1. Durable Markdown memory](#1-durable-markdown-memory)
30
- - [2. Plans](#2-plans)
31
- - [3. Risk assessment](#3-risk-assessment)
32
- - [4. Architecture map](#4-architecture-map)
33
- - [5. Background indexing](#5-background-indexing)
34
- - [6. Automatic context (bootstrap & before-edit)](#6-automatic-context-bootstrap--before-edit)
35
- - [7. Automatic memory capture + review queue](#7-automatic-memory-capture--review-queue)
36
- - [8. Measured token use](#8-measured-token-use)
37
- - [9. Read-only suggestions](#9-read-only-suggestions)
38
- - [10. Change readiness](#10-change-readiness)
39
- - [11. Client workflow templates](#11-client-workflow-templates)
40
- - [12. Fully automatic mode (hooks)](#12-fully-automatic-mode-hooks)
41
- - [How CodeBuddy saves tokens vs. a big knowledge graph](#how-codebuddy-saves-tokens-vs-a-big-knowledge-graph)
42
- - [MCP tools reference](#mcp-tools-reference)
43
- - [SDK usage](#sdk-usage)
44
- - [LangGraph](#langgraph)
45
- - [Working as a team](#working-as-a-team)
46
- - [Repo layout (for contributors)](#repo-layout-for-contributors)
47
- - [Troubleshooting](#troubleshooting)
48
- - [Privacy](#privacy)
49
-
50
- ---
51
-
52
- ## The problem it solves
53
-
54
- When you code with an AI agent, two things quietly cost you money and quality:
55
-
56
- 1. **The agent forgets.** Every session starts cold. You re-explain the same
57
- decisions, the same "don't touch that file," the same past bug.
58
- 2. **The agent over-reads.** To answer "fix the OAuth callback," it opens and
59
- reads dozens of files — burning tokens on raw source it mostly doesn't need.
60
-
61
- CodeBuddy fixes both:
62
-
63
- - It **remembers** durable facts, decisions, and incidents as Markdown you can
64
- read and commit.
65
- - It **prepares context** — a compact, bounded summary of the plan, risks, past
66
- incidents, and only the relevant files — so the agent gets what it needs
67
- without a repo dump.
68
-
69
- ---
70
-
71
- ## How it works (mental model)
72
-
73
- ```text
74
- .codebuddy/ (Markdown = source of truth, committable)
75
- ├── memory/facts, summaries, review (what we know)
76
- ├── plans/ (what we're doing)
77
- ├── policies.yaml (what needs care)
78
- └── cache/index.json (rebuildable index)
79
- │
80
- ┌──────────────────────┼───────────────────────────────┐
81
- │ │ │
82
- CLI (codebuddy …) MCP server (codebuddy serve) SDK / LangGraph
83
- │ │ │
84
- └── file-based tools ──┴── Postgres-backed tools ───────┘
85
- (no DB needed) (recall / embeddings)
86
- ```
87
-
88
- A turn with an agent looks like this:
89
-
90
- ```text
91
- turn starts
92
- → context_bootstrap # identity, active plan, policies, incidents, architecture
93
- → context_before_edit # for the files about to change: plan + risk + incidents + neighbours
94
- agent edits with compact context (not a repo dump)
95
- turn ends
96
- → context_after_turn # capture durable facts/decisions/incidents (confidence-gated)
97
- → code_suggestions # optional read-only quality findings
98
- ```
99
-
100
- Two design rules run through everything:
101
-
102
- - **Bounded, deterministic, explainable.** Every tool output is capped, produced
103
- the same way each run, and carries `explain[]` reasons. No black-box ranking.
104
- - **Local-first & no mandatory LLM.** The core context/index/suggestion
105
- paths use zero LLM calls. Embeddings (Hugging Face) power semantic recall only.
106
-
107
- ---
108
-
109
- ## Install & setup
110
-
111
- ### Requirements
112
-
113
- - **Node 20+** (required)
114
- - **PostgreSQL 16 + pgvector** — only for semantic `recall`/embeddings. Every
115
- new v2.0 feature works without it.
116
- - **Hugging Face token** — only for embeddings. Optional otherwise.
117
- - Optional: Docker (for the bundled Postgres), `@langchain/langgraph` peer dep.
118
-
119
- ### Fastest path
120
-
121
- ```bash
122
- npm install -g @orquentra/codebuddy
123
- cd ~/Projects/your-project
124
- codebuddy # first-run setup; offers to start Docker when needed
125
- codebuddy use your-project # repeat setup for a named namespace
126
- codebuddy rules install --client claude # teach the agent when to call the tools
127
- ```
128
-
129
- For a zero-database setup that also enables PR reports:
130
-
131
- ```bash
132
- npx @orquentra/codebuddy init --non-interactive --github-action
133
- ```
134
-
135
- This creates the local `.codebuddy/` scaffold and an idempotent
136
- `.github/workflows/codebuddy.yml` workflow. Commit the workflow to start
137
- receiving a CodeBuddy report on every pull request.
138
-
139
- On the first run, `codebuddy` also asks whether to enable Graphify:
140
-
141
- - **Yes:** it installs/detects Graphify, builds `graphify-out/graph.json`,
142
- registers its local MCP server in the project `.mcp.json`, and enables
143
- Graphify architecture input for CodeBuddy.
144
- - **No:** it registers only CodeBuddy. The MCP context workflow is unchanged;
145
- CodeBuddy uses its own local architecture index.
146
-
147
- In both cases it writes the CodeBuddy MCP entry without putting database
148
- credentials in `.mcp.json`, and adds managed instructions to `CLAUDE.md` and
149
- `AGENTS.md` so the agent knows to use `context_pack`. The Graphify MCP entry is
150
- added only when Graphify is enabled.
151
-
152
- Graphify runs locally. Its current installer is `uv tool install graphifyy`
153
- (with pipx/pip alternatives). Setup generates `graphify-out/graph.json` using
154
- Graphify's code-only extractor before registering the MCP server. Once the MCP
155
- server starts, CodeBuddy automatically indexes the existing repository, imports
156
- `.codebuddy/memory/` into the database, refreshes Graphify after code changes,
157
- and queues embeddings in the background. No manual refresh command is needed.
158
- The explicit command remains available for diagnostics:
159
-
160
- ```bash
161
- codebuddy graphify index
162
- ```
163
-
164
- `codebuddy serve` also checks an enabled Graphify configuration and prepares a
165
- missing or invalid graph automatically. Graph output is staged and validated
166
- before replacement, so a valid existing graph is preserved if extraction fails.
167
-
168
- ### How CodeBuddy helps, in plain English
169
-
170
- CodeBuddy is a small context and memory layer between your repository and your
171
- coding agent. You describe a task once, and CodeBuddy prepares the useful
172
- project information before Claude or Codex starts editing:
173
-
174
- 1. It finds the files, functions, tests, rules, previous incidents, and related
175
- architecture that matter to the task.
176
- 2. It sends a compact, token-budgeted `context_pack` instead of making the
177
- agent repeatedly read the whole repository.
178
- 3. The agent makes the change with more relevant evidence and less guessing.
179
- 4. CodeBuddy can record durable decisions and change outcomes for future tasks.
180
- 5. Pull requests can receive a readiness report covering risk, tests, plan drift,
181
- and architecture impact.
182
-
183
- The practical result is less repeated reading, lower context cost, fewer
184
- irrelevant edits, and a project that becomes easier for new agents to
185
- understand over time. Graphify is optional: it adds a richer local map of how
186
- the codebase connects, while CodeBuddy's own MCP context workflow works either
187
- way.
188
-
189
- ### No-database path (try the context engine in 30 seconds)
190
-
191
- Everything except semantic recall is file-based, so you can skip Postgres:
11
+ Requires Node.js 20 or later.
192
12
 
193
13
  ```bash
194
14
  npm install -g @orquentra/codebuddy
195
- cd ~/Projects/your-project
196
- codebuddy init --non-interactive # just creates the .codebuddy/ scaffold
197
- codebuddy index
198
- codebuddy suggest --git
199
- ```
200
-
201
- ### Bring-your-own vs. bundled Postgres
202
-
203
- The normal setup flow checks the default local Postgres URL first. If it is not
204
- reachable, CodeBuddy offers to start Docker Desktop and its bundled pgvector
205
- database automatically. Choose `No` if you want to configure Postgres yourself.
206
- Use `codebuddy use --no-docker` to skip that offer in scripts or controlled
207
- environments.
208
-
209
- ```bash
210
- # Manual control is still available:
211
- codebuddy postgres up
212
- codebuddy doctor # DB, pgvector, scaffold, model health
213
-
214
- # Or your own database — the URL must include real user:password
215
- export DATABASE_URL="postgres://user:pass@host:5432/db"
216
- codebuddy migrate
15
+ cd your-project
16
+ codebuddy
217
17
  ```
218
18
 
219
- > If `codebuddy doctor` says `password authentication failed for user "<you>"`,
220
- > your `DATABASE_URL` has no credentials and Postgres fell back to your OS
221
- > username. Set the URL as above. This does **not** affect the file-based tools.
19
+ The setup wizard configures the project and agent integration. Project context
20
+ works without a database; semantic memory recall optionally uses PostgreSQL
21
+ with pgvector and a local Hugging Face embedding model (no token needed).
222
22
 
223
- ---
23
+ ## What it does
224
24
 
225
- ## Feature tour (with examples)
25
+ - **Task context:** relevant files, symbols, code excerpts, nearby dependencies,
26
+ and covering tests within a token budget.
27
+ - **Project memory:** decisions, rules, and handoffs stored as readable Markdown,
28
+ recalled on every prompt by hybrid lexical + vector search
29
+ ([how it works](docs/semantic-memory.md)).
30
+ - **Change guidance:** plans, policies, incident history, and risk signals.
31
+ - **Optional retrieval:** zvec semantic search, Graphify architecture maps, and
32
+ a bring-your-own LLM layer.
226
33
 
227
- ### 1. Durable Markdown memory
228
-
229
- Facts and summaries are written as Markdown under `.codebuddy/memory/`. They
230
- survive database loss and are diffable in git.
34
+ ## See what it does
231
35
 
232
36
  ```bash
233
- codebuddy list-facts
234
- codebuddy inspect your-project
235
- codebuddy export your-project > backup.json
236
- codebuddy stats
37
+ codebuddy dashboard
237
38
  ```
238
39
 
239
- Through MCP an agent calls `remember` / `recall` / `share` / `forget`. Facts can
240
- be tagged as **incidents** (with `paths` and `severity`) so CodeBuddy can warn
241
- when a future change touches a file that broke before.
242
-
243
- ### 2. Plans
244
-
245
- A plan is a durable Markdown work-order: title, brief, files to touch, tests to
246
- add, risks, out-of-scope. At most one plan is "active" per namespace.
247
-
248
- ```bash
249
- codebuddy plan new "Add OAuth login flow" # opens $EDITOR
250
- codebuddy plan list
251
- codebuddy plan approve pln_abc123
252
- codebuddy plan start pln_abc123
253
- codebuddy plan complete pln_abc123 --commit <sha>
254
- ```
40
+ This opens a live local page showing:
41
+ - every prompt and the memories recalled for it;
42
+ - MCP tool calls with their latency;
43
+ - the tokens delivered to your agent.
255
44
 
256
- ### 3. Risk assessment
45
+ It updates as you work, and the traces never leave your machine.
257
46
 
258
- Deterministic, evidence-backed risk scoring from incident memory, project
259
- policy, and recent git churn — no LLM.
47
+ ## Try it without a database
260
48
 
261
49
  ```bash
262
- codebuddy risk assess --git
263
- codebuddy risk assess --paths src/auth/oauth.ts,src/api/login.ts
264
- codebuddy risk assess --plan pln_abc123 --explain
265
- ```
266
-
267
- Policy lives in `.codebuddy/policies.yaml`:
268
-
269
- ```yaml
270
- rules:
271
- - id: auth-sensitive
272
- pattern: "src/auth/**"
273
- message: "Auth changes need careful review."
274
- weight: 70
50
+ codebuddy init --non-interactive
51
+ codebuddy context preview "fix the login flow"
275
52
  ```
276
53
 
277
- ### 4. Architecture map
278
-
279
- A lightweight import graph, scanned on demand.
54
+ Agents use `context_pack` to request task evidence and `context_after_turn` to
55
+ capture useful knowledge. Request `evidence: "source"` when a short excerpt
56
+ needs more detail. Inspect missing or truncated evidence before editing.
280
57
 
281
- ```bash
282
- codebuddy map build
283
- codebuddy map deps src/cli/index.ts # what this file imports
284
- codebuddy map dependents src/core/codebuddy.ts # what imports this file (blast radius)
285
- ```
58
+ ## Documentation
286
59
 
287
- ### 5. Background indexing
60
+ - [Full guide and examples](https://github.com/Orquentra-Inc/CodeBuddy/blob/348bb7c/README.md)
61
+ - [Token budgets](https://github.com/Orquentra-Inc/CodeBuddy/blob/main/docs/token-budgets.md)
62
+ - [Optional LLM setup](https://github.com/Orquentra-Inc/CodeBuddy/blob/main/docs/llm-byok.md)
63
+ - [Source and issues](https://github.com/Orquentra-Inc/CodeBuddy)
288
64
 
289
- Builds a content-hash + one-line summary for every file, cached at
290
- `.codebuddy/cache/index.json` (gitignored, rebuildable). Incremental: unchanged
291
- files are skipped. Respects `.gitignore`.
292
-
293
- ```bash
294
- codebuddy index # e.g. "128 indexed (+128 ~0 =0 -0) in 40ms"
295
- codebuddy index # again → "=128" (all unchanged)
296
- codebuddy watch # optional foreground watcher; MCP serve watches automatically
297
- ```
298
-
299
- The index also records a per-file **symbol table** (functions, classes, class
300
- methods as `Class.method`, types), so context can send a file's public API
301
- instead of its whole body. Before-edit context ranks the whole table by the task
302
- before keeping 12 signatures, and returns the best match per top file as a
303
- `spans[]` entry (line range, `source`, zvec `rank`, a short excerpt) read from
304
- current source:
305
-
306
- ```bash
307
- codebuddy symbols src/service.ts
308
- # export function getUser L3-5
309
- # ...
310
- # 5 symbol(s)
311
- ```
312
-
313
- ### 6. Automatic context (bootstrap & before-edit)
314
-
315
- The heart of v2.0. Preview exactly what the agent would receive:
316
-
317
- ```bash
318
- codebuddy context bootstrap
319
- # project + active plan + policy + incident hotspots + architecture summary + token stats
320
-
321
- codebuddy context preview "fix the OAuth callback" --paths src/auth/oauth.ts
322
- # target files + relevant plan + matching policy + incidents + risk + import neighbours
323
-
324
- codebuddy context explain --git
325
- # the same, plus WHY each piece was included and per-stage token accounting
326
- ```
327
-
328
- Over MCP these are `context_bootstrap` and `context_before_edit`.
329
-
330
- For Claude and Codex, the simplest integration is the single-call
331
- `context_pack` tool:
332
-
333
- ```text
334
- context_pack({
335
- "task": "fix the OAuth callback",
336
- "tokenBudget": 4000
337
- })
338
- ```
339
-
340
- It automatically ranks files from the local index when paths are omitted, then
341
- returns bounded symbols, policies, risks, incidents, architecture neighbours,
342
- and relevant team facts. The agent does not need to manually chain recall,
343
- risk, map, and context tools. The response includes token accounting and whether
344
- safety evidence was omitted when the budget is tight. See the
345
- [token budget contract](docs/token-budgets.md) for the exact counted surfaces,
346
- final response accounting, and overflow behavior.
347
-
348
- **Optional semantic retrieval (zvec-grep).** When paths are omitted, files are
349
- found by keyword ranking over the local index. For conceptually phrased tasks
350
- you can add [zvec-grep](https://github.com/zvec-ai/zvec-grep) (`zg`, 0.2.x) as a
351
- local semantic candidate source. It is off by default and needs no network or
352
- hosted service:
353
-
354
- ```bash
355
- npm install -g @zvec/zvec-grep
356
- codebuddy zvec index # explicit; local/potion-code-16m-v2, seconds on a mid-size repo
357
- codebuddy zvec enable # or CODEBUDDY_ZVEC=1, or "zvec": { "enabled": true } in config
358
- codebuddy zvec status # enabled / version / index ready / zg server / reason
359
- echo ".zvec-grep/" >> .gitignore
360
- zg server on # optional: halves query latency; CodeBuddy never starts it
361
- ```
362
-
363
- CodeBuddy then runs `zg query --vector --mode auto` (oversampling chunks so one
364
- file's many chunks can't crowd out the rest), keeps only hits that still exist in
365
- the working tree (tests only if the task is about tests; never
366
- `.codebuddy/cache/`), drops vector hits whose current source shares fewer than two
367
- task words (unless they pass the keyword floor below), and fuses the rest with the
368
- keyword ranking (equal-weight RRF; weighting vectors 0.5–0.75 or 1.5 scored worse
369
- on the benchmark). Hit line ranges are kept
370
- and re-checked against current source to pick symbol spans. A task naming a code
371
- identifier (`withSessionLock`, `HookStagingStore.claim`) skips vectors and routes
372
- straight to the declaring files (`retrieval: exact-symbol routing`). It never builds
373
- a zvec index for you: a missing binary, unsupported version, unready index,
374
- timeout or failure falls back to keyword ranking, and `explain` says which
375
- happened (`retrieval: …`). The version/readiness check is cached for 10 minutes
376
- in `.codebuddy/cache/zvec-ready.json`; a failed check is cached for 30 seconds
377
- (dropped as soon as `.zvec-grep/` changes), and the explain line then says
378
- `cached check from Ns ago`. Hits only choose candidate files and
379
- spans; risks, policies and incidents still come from CodeBuddy's own evidence,
380
- and facts are ranked lexically (no semantic fact ranker is wired by default).
381
-
382
- **Optional small model (bring your own key).** With a free Groq or OpenRouter key,
383
- CodeBuddy can write one-line summaries of your files (so a reworded task still finds
384
- the right file) and rerank the candidates once per prompt with a short deadline. It's
385
- off by default, the key stays in your environment, and any failure falls back to the
386
- deterministic ranking. See [docs/llm-byok.md](docs/llm-byok.md) for setup and exactly
387
- what is sent.
388
-
389
- ```bash
390
- export GROQ_API_KEY=gsk_...
391
- codebuddy llm enable && codebuddy llm summarize
392
- ```
393
-
394
- **Evidence floor and abstention.** A task-discovered file needs a keyword score
395
- of 1.5 (two distinct task words in its path, summary or symbols, or one in its
396
- file name), or, for a vector hit, two task words in its current source. Weaker
397
- candidates are dropped (`abstention: N weak keyword candidate(s) … dropped`), and
398
- when no *source* file reaches the floor the task gets no targets at all rather
399
- than unrelated code (`abstention: no source file reached the evidence floor …`);
400
- docs and config alone never justify sending code. The thresholds were calibrated
401
- on the `tune` split of `evals/task-savings.json` only. Explicit `paths`, plans and
402
- exact identifiers are unaffected.
403
-
404
- **Multi-file expansion.** When the task asks for a path through several files
405
- (`trace`, `follow`, `through`, `across`, `between`, `from … to`), up to two import
406
- neighbours of the top three targets that share a task word are ranked with the
407
- import edge credited as one more word, so a workflow's partner files survive the
408
- floor and the target cap without displacing stronger evidence (`expansion: …`).
409
-
410
- Benchmark on this repo (`npm run eval:retrieval`, 12 tasks, `zg` index built with
411
- `codebuddy zvec index`):
412
-
413
- | | top-5 hits | MRR | context | cold | warm |
414
- |---|---|---|---|---|---|
415
- | keyword only | 6/12 | 0.39 | ~1.7k tokens | ~130ms | ~20ms |
416
- | + zvec-grep (direct) | 9/12 | 0.51 | ~2.1k tokens | ~810ms | ~425ms |
417
-
418
- The evidence floor cut keyword context from ~2.5k and fused from ~2.3k tokens per
419
- task at unchanged hits; a running `zg server` roughly halves warm latency.
420
-
421
- ### 7. Automatic memory capture + review queue
422
-
423
- After a turn, `context_after_turn` (MCP) turns a summary into classified
424
- candidates and **gates** them:
425
-
426
- - **Auto-saved** as durable Markdown: confident facts, decisions, incidents.
427
- - **Queued for review:** temporary notes, low-confidence items, anything
428
- sensitive (secrets/keys/tokens are **never** auto-saved).
429
- - **Dropped:** chatter.
430
-
431
- Curate the queue from the CLI:
432
-
433
- ```bash
434
- codebuddy memory review # list pending candidates (⚠ marks sensitive)
435
- codebuddy memory review show <id>
436
- codebuddy memory review approve <id> # promote to a durable fact
437
- codebuddy memory review reject <id>
438
- ```
439
-
440
- Bias by design: **prefer a missed memory over a false durable fact.**
441
-
442
- ### 8. Measured token use
443
-
444
- CodeBuddy does not print estimated savings. The token numbers it reports are the
445
- size of its own payload (`tokens.returnedEstimate` against `tokens.budget`) and
446
- what agent CLIs bill in A/B studies.
447
-
448
- The three-arm study (`npm run eval:agent-ab`, results in
449
- [`evals/agent-ab-repos.jsonl`](evals/agent-ab-repos.jsonl)) ran 14 bug-fix tasks
450
- across semver, pygments, click, chi and commander, 3 runs per arm, with
451
- `claude -p` on Claude Opus 5.5 and CodeBuddy 2.3.0:
452
-
453
- | Arm | Cost | Tokens | Agent turns | Tasks passed |
454
- |---|---|---|---|---|
455
- | no CodeBuddy | $13.77 | 16.56M | 532 | 42/42 |
456
- | `lean` | $12.79 (−7.1%) | 14.45M (−12.7%) | 480 | 42/42 |
457
-
458
- Lean was cheaper on 11 of the 14 tasks; per task, tokens ranged from −43% to
459
- +16%. The saving comes from fewer agent steps, each of which re-sends the
460
- conversation, more than from reading fewer bytes.
461
-
462
- For development, `npm run eval:savings` measures `context_pack` itself on
463
- [`evals/task-savings.json`](evals/task-savings.json) (27 cases: identifiers,
464
- paraphrases, multi-file, negative queries; TypeScript + Python; `tune` vs
465
- `holdout` splits) by calling the real MCP tool:
466
-
467
- - **compression** — a recount that tokenizes (cl100k) only files whose
468
- signatures actually reached the payload, against those files' raw source;
469
- - **transmission** — observed MCP result tokens on the first turn and on an
470
- acknowledged repeat turn. Reported next to it, each labelled separately:
471
- the **framing** count tokenizes the whole SDK tool result (`context_pack`
472
- returns the payload both as `content[].text` and as `structuredContent`, so a
473
- client that forwards both pays roughly twice); the **JSON-RPC envelope** is
474
- the serialized request and response message captured from the transport; the
475
- **tool list** is the `tools/list` schema (and its serialized response) a
476
- client loads once per session. System-prompt framing is client-specific and
477
- not observable from the server, so it is reported as not measured;
478
- - **net task tokens** — first turn + raw reads of expected files the payload
479
- only names + **function-body reads** (the lines of each declaration the task
480
- edits, per the dataset's `symbols`, that neither a listed signature nor a span
481
- excerpt shows), against an oracle that reads exactly the expected files. Not
482
- clamped: losses show as negative.
483
-
484
- Quality is deterministic (no LLM): evidence sufficiency (every expected file's
485
- signatures present and every expected identifier in the payload), abstention
486
- on negative queries, and the false durable memory rate of `context_after_turn`.
487
- Each case reports its retrieval status (`disabled`/`vector`/`fallback`); fused
488
- quality only counts `vector` cases, so a zvec fallback never passes as semantic
489
- evidence. Fused mode never touches your repository's zg index: with `zg` 0.2.x
490
- installed, each dataset repository is copied to a temp directory and indexed
491
- there first (a few seconds).
492
-
493
- Scenarios run alongside (dataset `scenarios`, no Postgres or `zg` needed):
494
-
495
- - **stale index** — index a copy, then edit a signature, delete a file and
496
- rename a file without reindexing: each case must report only current
497
- symbols/paths and flag the refresh in its `index:` explain line;
498
- - **provider outage** — fused mode against a stub `zg` that is missing, crashes
499
- or hangs: every case must report `fallback` (or exact-symbol routing), keep the
500
- keyword-only targets, and never yield vector-quality evidence;
501
- - **long session** — twelve acknowledged `context_pack` turns in one session
502
- that switch between tasks and come back (A, B, A …), with `full: true` at turn
503
- 6 (a compacted client), against the same tasks as independent first turns.
504
- The `full` turn must match a fresh session section for section, and every
505
- omitted section must have been delivered earlier since the last `full`;
506
- - **agent harness** — four fixture tasks
507
- ([`evals/fixtures/agent-tasks`](evals/fixtures/agent-tasks)), each with a real
508
- check that fails until the task is done. The default, deterministic
509
- `simulated` agent picks its target only from the payload (files whose listed
510
- signatures declare the symbol to change, then the other targets in rank
511
- order), reads what the payload does not show, applies the task's scripted
512
- edit, and runs the check; a failed edit is reverted and the next candidate is
513
- a retry. Retries, incorrect edits (files outside the expected set), passes and
514
- tokens are recorded, so a worse payload shows up as a worse outcome.
515
-
516
- ```bash
517
- npm run eval:savings # per-case table + summary
518
- npm run eval:savings -- --json # machine-readable report (incl. scenarios)
519
- npm run eval:savings -- --check # exit 1 on regression vs evals/task-savings.baseline.json
520
- npm run eval:savings -- --check --mode keyword # what CI runs
521
- npm run eval:savings -- --check --mode fused # fused gate on zg-indexed temp copies
522
- npm run eval:savings -- --check --gate-latency # also gate p95 latency (same machine only)
523
- npm run eval:savings -- --write-baseline
524
- npm run eval:savings -- --mode keyword --agent-cmd "claude -p --output-format json"
525
- npm run eval:savings -- --mode keyword --agent-cmd "codex exec" --agent-context none
526
- ```
527
-
528
- With the #45 evidence floor (keyword mode) abstention is 1.0 and first turns are
529
- ~1,401 tokens per task; tool schema is 1,261 tokens with `CODEBUDDY_TOOL_PROFILE=core`
530
- (see [docs/audits](docs/audits/README.md#experiments)). The detailed figures below
531
- predate that change; `evals/task-savings.baseline.json` holds the current baseline.
532
-
533
- `--agent-cmd` switches the harness to `agent` mode: it runs a real agent CLI in
534
- a temp copy per task (the prompt is passed as the last argument, with the
535
- `context_pack` payload unless `--agent-context none`), re-prompts with the check
536
- output on failure, and records files edited, retries, the check result and the
537
- tokens the CLI reports (`usage` JSON or `tokens used: N`; `null` when it reports
538
- none, never estimated). It spends model tokens, so it is opt-in and never runs
539
- in CI; `--check` never compares it with the simulated baseline.
540
-
541
- The committed baseline was measured on this repo. Keyword mode: evidence
542
- sufficiency 0.667, abstention 0.667, ~1,898 first-turn and ~467 repeat-turn
543
- tokens per task, latency p50 ~25 ms / p95 ~29 ms. Fused mode (zg 0.2.2,
544
- `local/potion-code-16m-v2`, temp copies): 23 `vector` + 4 exact-symbol cases,
545
- 0 fallback, vector-case sufficiency 0.75 (0.792 over all cases), ~1,788
546
- first-turn tokens, p50 ~433 ms / p95 ~604 ms. Net modeled task tokens across
547
- the 27 cases are **−15,128** keyword / −9,307 fused, of which function-body
548
- reads are 5,156 / 6,124 and whole-file reads 7,432 / 3,604: for small
549
- single-file tasks an agent reading the right file directly is cheaper. Framing,
550
- separately: the SDK result is ~3,973 tokens per first turn, the serialized
551
- JSON-RPC response ~3,987 and the request ~56 (net −71,158 and −73,042 if a
552
- client keeps them); the tool list is 3,419 tokens of schema (3,437 serialized,
553
- 290 for `context_pack` alone) once per session. Scenarios: stale index 3/3
554
- correct, outage 15/15 safe (12 `fallback`, 3 exact-symbol, 0 `vector`), long
555
- session 21,418 vs 32,638 tokens for the same twelve tasks fetched
556
- independently, agent harness 4/4 passed with 0 retries and 0 incorrect edits at
557
- 1,965 tokens (an oracle that reads only the edited files: 434). Without a ready
558
- index, fused mode re-probed `zg` on every call (~206 ms); the failed probe is now
559
- cached for 30 s (10 calls: 2,071 ms → 212 ms).
560
-
561
- `--check` fails if sufficiency or abstention drops, false memory rises, tokens
562
- (payload, framing, envelope, function-body reads, tool list or session) grow
563
- more than 10%, a stale-index case claims stale evidence or fewer are correct, an
564
- outage case reports `vector` or loses keyword context, the session's acks,
565
- `full` resend or omissions break, or the simulated harness passes fewer tasks or
566
- needs more retries or incorrect edits. The fused baseline is only enforced when
567
- fused mode ran with `zg` 0.2.x; without it the check reports `skip` instead of
568
- failing, so CI stays green without `zg`. Token counts are deterministic up to a
569
- random `deliveryId` and the absolute repo root echoed in the payload (a few
570
- tokens), so the gate is stable across machines; latency is not, so p95 only
571
- warns unless `--gate-latency` is passed. CI runs `--check --mode keyword`.
572
-
573
- ### 9. Read-only suggestions
574
-
575
- Evidence-backed, severity-ranked findings — a careful reviewer, not a noisy
576
- linter. It **never edits code.**
577
-
578
- ```bash
579
- codebuddy suggest --git
580
- codebuddy suggest --paths src/auth/oauth.ts
581
- # [med ] Review high-risk change to src/auth/oauth.ts
582
- # ↳ risk_score: score 70 (policy)
583
- # [low ] No test found for src/auth/oauth.ts
584
- # [low ] Stale policy rule "gone" → legacy/** does not exist
585
- ```
586
-
587
- Categories: risk · plan divergence · architecture blast radius · incident
588
- history · missing tests · stale policies. Over MCP: `code_suggestions`.
589
-
590
- ### 10. Change readiness
591
-
592
- Turn the existing signals into one evidence-backed change report. It combines
593
- the current diff, active plan, risk, architecture dependents, incident history,
594
- and test coverage into a single readiness decision.
595
-
596
- ```bash
597
- codebuddy change
598
- codebuddy change --paths src/auth/oauth.ts --no-git
599
- codebuddy change --json # stable output for CI or another agent
600
- # [review ] change report
601
- # highest risk: 70
602
- # verification: 1 test file(s) changed; 0 code file(s) without a discovered test
603
-
604
- codebuddy change verify # detects and runs npm/pnpm/yarn/bun test
605
- codebuddy change verify --command "npm run test:unit" --timeout 300000
606
-
607
- codebuddy eval # run the committed readiness evaluation cases
608
- ```
609
-
610
- The status is `ready`, `review`, `blocked`, or `no_changes`. The command is
611
- read-only and is designed to run before a commit or as an agent checkpoint.
612
- `change verify` returns a non-zero exit code when tests fail, time out, or no
613
- test command can be found, and supports `--json` for CI integration.
614
-
615
- The evaluation dataset lives under `evals/` and is intentionally small,
616
- readable, and versioned. Extend it when changing risk thresholds or adding a
617
- new signal so product claims remain reproducible.
618
-
619
- Record the outcome of a real change so future reports learn from the project:
620
-
621
- ```bash
622
- codebuddy learn outcome --outcome safe \
623
- --paths src/auth/oauth.ts \
624
- --summary "OAuth callback change passed review and regression tests."
625
-
626
- codebuddy learn outcome --outcome regression \
627
- --paths src/auth/oauth.ts \
628
- --severity high \
629
- --summary "Dropped callback state caused a production login failure."
630
- ```
631
-
632
- Outcomes are stored as reviewable Markdown under `.codebuddy/memory/`. A
633
- regression becomes path-specific incident memory and raises the next change's
634
- risk score for that area.
635
-
636
- If a repository already uses Graphify, CodeBuddy can consume its local graph
637
- without taking a Graphify dependency:
638
-
639
- ```bash
640
- codebuddy change --graphify graphify-out/graph.json
641
- codebuddy change verify --graphify graphify-out/graph.json
642
- ```
643
-
644
- The imported relationships enrich architecture dependents while CodeBuddy
645
- keeps its own policy, plan, verification, and team-memory signals layered on
646
- top.
647
-
648
- ### 11. Client workflow templates
649
-
650
- Teach Claude/Codex *when* to call each tool. Install writes an idempotent
651
- managed block into `CLAUDE.md` / `AGENTS.md` between clear markers — re-running
652
- updates it in place and never touches your other content.
653
-
654
- ```bash
655
- codebuddy rules show --client claude # preview the exact text
656
- codebuddy rules install --client claude # → managed block in CLAUDE.md
657
- codebuddy rules install --client codex # → managed block in AGENTS.md
658
- ```
659
-
660
- See [docs/degraded-mode.md](docs/degraded-mode.md) for how the tools fail soft
661
- (a tool missing, Postgres down, empty retrieval).
662
-
663
- #### Client adapters and automation modes
664
-
665
- Each client gets one explicit mode — who drives retrieval and capture:
666
-
667
- | Mode | Rules block | Hooks | Who calls `context_pack` / `context_after_turn` |
668
- | --- | --- | --- | --- |
669
- | `agent` | agent-driven | removed | the model, as the rules instruct |
670
- | `hooks` | hook-driven | installed | the hooks; the rules tell the model **not** to repeat them |
671
- | `mcp-only` | removed | removed | nobody automatically — tools are just available |
672
- | `lean` | removed | lean hook | nobody — no MCP server; the hook injects up to 3 ranked file pointers (claude, opencode, hermes) |
673
-
674
- ```bash
675
- codebuddy clients list # surfaces, lifecycle coverage, mode in this repo
676
- codebuddy clients install claude --mode hooks # CLAUDE.md + .claude/settings.json + .mcp.json
677
- codebuddy clients install opencode --mode hooks # AGENTS.md + .opencode/plugins/codebuddy.js + opencode.json
678
- codebuddy clients install hermes --mode hooks --global # + .hermes/plugins/codebuddy/ + ~/.hermes/config.yaml
679
- codebuddy clients install claude --mode lean # lean hook only + index build; no rules block, no MCP
680
- codebuddy clients uninstall opencode # removes only CodeBuddy's entries
681
- codebuddy clients verify [client] # installed vs verified host version, no-model conformance checks
682
- codebuddy clients cost # measured per-turn token cost per client and mode
683
- ```
684
-
685
- | Client | Rules | MCP config | Lifecycle hooks | Verified host |
686
- | --- | --- | --- | --- | --- |
687
- | Claude Code | `CLAUDE.md` | `.mcp.json` | prompt, edit, turn-end, compaction | 2.1.287 |
688
- | Codex | `AGENTS.md` | global `~/.codex/config.toml` (`--global`) | missing | 0.163.0 |
689
- | OpenCode | `AGENTS.md` | `opencode.json` or `opencode.jsonc` (`mcp`, local) | prompt, edit, turn-end, compaction (plugin) | 1.18.32 |
690
- | Freebuff | `AGENTS.md` | `.agents/mcp.json` | missing: no hook config, SDK `run_file_change_hooks` is a no-op | source `f9ae64dd` |
691
- | Hermes Agent | `AGENTS.md` | global `~/.hermes/config.yaml` (`--global`) | prompt, edit, turn-end, compaction (plugin) | 0.17.0 |
692
-
693
- Global MCP configs are only written with `--global` (install and uninstall),
694
- and only CodeBuddy's `codebuddy-<namespace>` entry is touched. Codex reuses the
695
- `codebuddy codex install` writer. Hermes gets a line-scoped edit of
696
- `mcp_servers` (`HERMES_HOME` is honoured): every other line and comment stays
697
- byte-for-byte, and a file whose shape can't be edited safely (flow map,
698
- invalid YAML) is left alone with a manual step.
699
-
700
- `clients verify` runs `<host> --version` and, where the host is installed,
701
- `claude mcp get`, `opencode debug config`, `codex mcp list --json` and
702
- `hermes mcp list`, plus the Hermes plugin gates. It never calls a model and
703
- reports a missing host or a failing command instead of failing.
704
-
705
- `hook-driven` is only offered where CodeBuddy wires every lifecycle event, so a
706
- turn is never half-automated. Clients sharing `AGENTS.md` share one managed
707
- block. `codebuddy rules install` without `--mode` and `codebuddy use` follow the
708
- installed hooks, and `hooks install|uninstall` switch an existing block, so the
709
- two paths never both retrieve and capture. `clients list` warns if they do.
710
- While OpenCode hooks are installed the shared `AGENTS.md` block stays
711
- hook-driven; it tells agents without hooks to use the agent-driven flow.
712
-
713
- The OpenCode plugin forwards `chat.message` (context appended to the prompt as a
714
- synthetic text part), `tool.execute.after` for `edit`/`write`/`apply_patch`/`patch`,
715
- `session.idle` and `session.compacted` to the same `codebuddy hooks` runtime.
716
- It never overwrites a `codebuddy.js` it did not write. A commented
717
- `opencode.json(c)` is never rewritten: install prints the entry to add by hand.
718
-
719
- The Hermes plugin (`.hermes/plugins/codebuddy/`, Python) forwards `pre_llm_call`
720
- (the block is returned as `{"context": ...}`), `post_tool_call` for
721
- `write_file`/`patch` (paths and V4A headers only, never file content) and
722
- `terminal` exit codes, `post_llm_call` (capture summary) and `on_session_end`
723
- (capture) to the same runtime. Hermes adds plugin context to the current turn
724
- only and never stores it, so the bridge asks for the full block every turn and
725
- resets the session ledger with it. That reset also covers compaction, which
726
- has no plugin hook. Hermes loads project plugins only with
727
- `HERMES_ENABLE_PROJECT_PLUGINS=1` and `codebuddy` listed under `plugins.enabled`
728
- in `~/.hermes/config.yaml`. `hermes plugins enable` rejects project plugins,
729
- so add that entry by hand. `clients verify hermes` checks both.
730
-
731
- Per-turn CodeBuddy cost, measured by `codebuddy clients cost` on this repo
732
- (cl100k tokens). "1st" is the first prompt of a session; "later" is a later
733
- prompt with nothing changed:
734
-
735
- | Client | Mode | Rules | MCP `tools/list` | Hook block 1st / later | Turn 1st / later |
736
- | --- | --- | --- | --- | --- | --- |
737
- | Claude Code | agent | 696 | 3419 | 0 / 0 | 4115 / 4115 |
738
- | Claude Code | hooks | 660 | 3419 | 162 / 141 | 4241 / 4220 |
739
- | Codex, OpenCode, Freebuff, Hermes | agent | 703 | 3419 | 0 / 0 | 4122 / 4122 |
740
- | OpenCode | hooks | 667 | 3419 | 162 / 141 | 4248 / 4227 |
741
- | Hermes Agent | hooks | 667 | 3419 | 162 / 162 | 4248 / 4248 |
742
-
743
- The rules block, the `tools/list` payload and the hook block are measured.
744
- The hook block depends on the repo's memory and is capped at 1,800 characters.
745
- In agent mode, the cost of the tool results depends on which tools the model
746
- calls (see `npm run eval:savings`).
747
-
748
- How each host frames these costs:
749
-
750
- - **Claude Code.** Adds the hook output to the prompt with its own wrapper,
751
- and may defer MCP schemas behind tool search. Neither is measured here.
752
- - **OpenCode.** Appends the block unchanged as a synthetic text part that
753
- stays in history.
754
- - **Hermes.** Appends the block to the user message with `\n\n` (checked in
755
- the source), and resends it every turn. `hermes prompt-size --json`,
756
- measured on 0.17.0, shows the `AGENTS.md` context section at 3,070 bytes for
757
- a 2,960-byte file: 110 bytes of framing on every turn. It doesn't count MCP
758
- tools.
759
- - **Freebuff.** Sends `AGENTS.md` as a fenced block under a fixed header,
760
- according to the source.
761
-
762
- ### 12. Fully automatic mode (hooks)
763
-
764
- Templates *ask* the agent to call the tools. **Hooks guarantee it** — CodeBuddy
765
- wires into Claude Code's lifecycle so recall and capture fire deterministically,
766
- whether or not the model remembers to.
767
-
768
- ```bash
769
- codebuddy hooks install --client claude # → .claude/settings.json (idempotent)
770
- codebuddy hooks install --client opencode # → .opencode/plugins/codebuddy.js
771
- codebuddy hooks status --client claude
772
- codebuddy hooks uninstall --client claude # removes only CodeBuddy's hooks
773
- ```
774
-
775
- - `UserPromptSubmit` → inject compact project context into every prompt (**recall**)
776
- - `PostToolUse(Edit|Write|MultiEdit)` → record the changed files
777
- - `Stop` → capture durable memory from the real change set + transcript (**capture**),
778
- and write a worklog handoff including the test/lint/typecheck/build commands run
779
- that turn and whether they passed
780
- - `SessionStart(compact|clear)` → forget which context was already sent, so the
781
- next prompt resends it in full (re-run `codebuddy hooks install` to add this to
782
- an existing install)
783
- - `PreToolUse(Read|Grep)` → **tool guard**: a Read of a file over 1,000 lines or 80 KB
784
- with no `offset`/`limit` is answered with an outline (symbols or headings with line
785
- ranges) and a hint to read a range or Grep; a content Grep without `head_limit`
786
- gets `head_limit: 100`. It never grants permission, and any ranged Read passes.
787
- Lean mode installs it too. `CODEBUDDY_TOOL_GUARD=0` turns it off.
788
-
789
- Installing hooks switches an existing managed rules block to hook-driven mode
790
- (and uninstalling switches it back), so the model is not also told to call
791
- `context_pack` / `context_after_turn`. The injected block names its `session`;
792
- MCP calls that pass it as `sessionId` share the hook ledger, so a compaction
793
- reset makes both paths resend in full.
794
-
795
- Set this up once and you never call `remember`/`recall` — it happens on its own.
796
- Hooks are fail-soft (a hook never breaks a turn) and capture still runs through
797
- the same confidence + sensitivity gating. Restart Claude Code after installing.
798
-
799
- ---
800
-
801
- ## How CodeBuddy saves tokens vs. a big knowledge graph
802
-
803
- A popular alternative is to build a **whole-repo knowledge graph** (GraphRAG
804
- style): parse every file into nodes and edges, cluster them, and retrieve
805
- subgraphs to answer questions. That's great for *exploration* ("how does X
806
- connect to Y across the codebase?"). It is expensive for *day-to-day editing*.
807
-
808
- The difference in one line: **a graph models the entire repo; CodeBuddy models
809
- just the change in front of you.**
810
-
811
- | | Whole-repo knowledge graph | CodeBuddy context engine |
812
- |---|---|---|
813
- | Unit | thousands of nodes + edges | a few file "capsules" + the active plan/risk |
814
- | What the LLM sees | a retrieved subgraph (size varies, can be large) | a **bounded, budgeted** payload with `explain[]` |
815
- | Scope | the whole project | target files + their direct import neighbours |
816
- | Build cost | LLM extraction over every file (tokens $$) | deterministic hashes + summaries (no LLM) |
817
- | Freshness | re-extract on change (often another LLM pass) | incremental hash diff, milliseconds |
818
- | Measurability | hard to say what a query "cost" | every payload reports its size against its budget; savings are measured in billed A/B runs |
819
-
820
- Concretely, for a change touching `src/auth/oauth.ts`:
821
-
822
- - **Graph approach:** to be safe, retrieval pulls the auth community — many
823
- nodes, their neighbours, and descriptions — and drops a large subgraph into
824
- context. The token cost scales with how big and connected that region is.
825
- - **CodeBuddy:** sends the file's one-line summary + exported symbols, its direct
826
- import neighbours, the matching policy rule, any past incident, and the active
827
- plan — then **packs it into a token budget, keeping the highest-value evidence
828
- first.** If it doesn't fit, low-value context is dropped (and reported), never
829
- the risk finding.
830
-
831
- Why it saves tokens: a file capsule such as
832
- `"ts • 42 lines • exports login, verify"` points the agent at the right file, so
833
- it spends fewer steps searching. Every step re-sends the conversation, so fewer
834
- steps is where the saving comes from; see
835
- [measured token use](#8-measured-token-use).
836
-
837
- The honest caveats:
838
-
839
- - These are **complementary tools.** Use a knowledge graph to *understand* an
840
- unfamiliar codebase; use CodeBuddy to *edit* one efficiently and safely.
841
- - Savings depend on the task: in the A/B study they ranged from −43% to +16%
842
- tokens per task.
843
- - CodeBuddy does **not** try to answer arbitrary cross-repo questions; that's
844
- what the graph is for.
845
-
846
- ---
847
-
848
- ## MCP tools reference
849
-
850
- CodeBuddy ships tools over stdio (`codebuddy serve`). HTTP transport is deferred.
851
-
852
- | Tool | Input | Output |
853
- |---|---|---|
854
- | `remember` | `{ sessionId?, content, type?, idempotencyKey?, agentId? }` | `{ id, sessionId, deduplicated }` |
855
- | `remember_batch` | `{ items: [...] }` | `{ items: [...] }` |
856
- | `recall` | `{ sessionId, query, budget?, callerModel?, conflictMode? }` | `{ system, messages, stats }` |
857
- | `list_facts` | `{ subject?, limit?, cursor? }` | `{ facts[], nextCursor? }` |
858
- | `list_namespaces` | `{}` | `{ namespaces[] }` |
859
- | `forget` | `{ id }` | `{ ok, entityType }` |
860
- | `share` | `{ to_namespace, factIds, mode?, to_project_root?, agentId? }` | `{ shared }` |
861
- | `unshare` | `{ to_namespace, factIds, agentId? }` | `{ revoked }` |
862
- | `plan_current` | `{}` | `{ plan, policy }` |
863
- | `plan_create` / `plan_amend` / `plan_status` | plan fields | `{ plan }` |
864
- | `risk_assess` | `{ planId?, paths?, useGit?, limit? }` | `{ items, stats }` |
865
- | `map_query` / `map_neighbours` | graph query | edges / `{ modules, edges }` |
866
- | `context_bootstrap` | `{ sessionId?, ack?, full? }` | `{ project, plan, policy, policyRules, incidents, facts, handoffs, architecture, tokens, delivery? }` |
867
- | `context_before_edit` | `{ task?, paths?, planId?, useGit?, tokenBudget?, sessionId?, ack?, full? }` | `{ targetPaths, plan, policyRules, incidents, facts, handoffs, risks, neighbours, symbols, diffs, tokens, delivery? }` |
868
- | `context_pack` | `{ task, paths?, planId?, useGit?, tokenBudget?, sessionId?, ack?, full? }` | same as `context_before_edit` |
869
- | `context_after_turn` | `{ summary, changedFiles?, planId?, taskType?, agentId?, sessionId?, verification?, commitSha? }` | `{ saved, queuedForReview, ignored, reasons, extractor, worklogId }` |
870
- | `code_suggestions` | `{ paths?, planId?, useGit?, limit? }` | `{ suggestions, stats }` |
871
- | `health` | `{}` | `{ database: { available, state, error?, retryInMs? }, tools: { profile, file, database } }` |
872
-
873
- **Tool profiles.** Every tool definition costs schema tokens in each session
874
- (3,419 tokens for all 21). Set `CODEBUDDY_TOOL_PROFILE=core` in the MCP server's
875
- environment to list only the automatic context workflow — `context_pack`,
876
- `context_bootstrap`, `context_before_edit`, `context_after_turn`,
877
- `code_suggestions` and `health` — for 1,261 tokens. `full` (the default) lists
878
- everything; any other value fails at startup. In a client's MCP config:
879
-
880
- ```json
881
- { "mcpServers": { "codebuddy": { "command": "codebuddy", "args": ["serve"],
882
- "env": { "CODEBUDDY_TOOL_PROFILE": "core" } } } }
883
- ```
884
-
885
- (Codex: `CODEBUDDY_TOOL_PROFILE = "core"` under `[mcp_servers."<name>".env]`.)
886
- Use `full` when the agent should call `recall`, `remember`, plans or map tools
887
- directly; hooks and the CLI are unaffected.
888
-
889
- If PostgreSQL is unreachable the server still starts: file-based tools work and
890
- the database tools return `{ degraded: true, tool, database, fallback }` with
891
- `isError` until a later call reconnects (see [docs/degraded-mode.md](docs/degraded-mode.md)).
892
-
893
- **Session-aware delivery (contract v1).** The three context tools accept an
894
- optional `sessionId`. The response then carries `delivery: { version, sessionId,
895
- deliveryId, ack, unchanged, avoidedTokens, note }`. Pass that `deliveryId` as
896
- `ack` on your next call; sections you acknowledged and that have not changed
897
- (`plan`, `policyRules`, `incidents`, `facts`, `handoffs`, `architecture`,
898
- `symbols`, `neighbours`) are then omitted and named in `delivery.unchanged`.
899
- Target paths, risks and diffs are always sent. The last 8 acknowledged
900
- versions of each section are remembered, so switching back to an earlier task
901
- in the same session does not resend what you already hold. Nothing counts as
902
- seen until it is acknowledged, acknowledged sections are resent after 30
903
- minutes, and `full: true` resends everything and forgets every earlier
904
- acknowledgement. Without `sessionId` the payload is complete and
905
- unchanged from earlier versions.
906
-
907
- **Cross-chat handoffs.** Turns captured via `context_after_turn` or the Stop
908
- hook that changed files, saved memory or report verification write a bounded
909
- Markdown entry to `.codebuddy/worklog/`. Context tools return relevant entries
910
- as `handoffs` with freshness (`changedSince`, `missing`). Inspect them with
911
- `codebuddy worklog list|show|latest`.
912
-
913
- Wire it into a client with `codebuddy use`, or a project `.mcp.json` (see
914
- [CLAUDE_CODEX.md](CLAUDE_CODEX.md)).
915
-
916
- ---
917
-
918
- ## SDK usage
919
-
920
- `createRuntime` wires the Postgres repository, initializes `CodeBuddy`, and
921
- returns a `close()` hook that drains the worker and closes the client.
922
-
923
- ```ts
924
- import { createRuntime } from "codebuddy";
925
-
926
- const runtime = await createRuntime({
927
- postgresUrl: process.env.DATABASE_URL!,
928
- provider: { type: "huggingface", apiKey: process.env.HF_TOKEN },
929
- namespace: "research-agent",
930
- tokenBudget: 8000,
931
- });
932
-
933
- await runtime.memory.remember({
934
- sessionId: "sess_123",
935
- content: "Use layer caching for the Docker build.",
936
- type: "fact",
937
- });
938
-
939
- const context = await runtime.memory.recall({
940
- sessionId: "sess_123",
941
- query: "What do we know about deployment?",
942
- budget: 6000,
943
- });
944
-
945
- await runtime.close();
946
- ```
947
-
948
- Notes: `remember` defaults to `type: "interaction"`; repeated writes with the
949
- same idempotency key or content return `deduplicated: true`; embeddings are
950
- prepared asynchronously and recall falls back to recency plus lexical matches
951
- over durable facts while vectors are pending or the provider is unavailable.
952
-
953
- ---
954
-
955
- ## LangGraph
956
-
957
- ```ts
958
- import { CodeBuddyNode, CodeBuddyCheckpointer } from "codebuddy/langgraph";
959
-
960
- const graph = new StateGraph(State)
961
- .addNode("recall", new CodeBuddyNode({ memory, mode: "recall", cacheTtlSeconds: 30 }))
962
- .addNode("agent", agentNode)
963
- .addNode("remember", new CodeBuddyNode({ memory, mode: "remember" }))
964
- .compile({ checkpointer: new CodeBuddyCheckpointer({ namespace: "research-agent" }) });
965
- ```
966
-
967
- Recall caching defaults to 30s, keyed by `(namespace, sessionId, query)`.
968
- `CodeBuddyCheckpointer` keeps checkpoints and pending writes in process memory.
969
- It preserves parent history and separates LangGraph checkpoint namespaces, but
970
- state is lost when the process exits. Use a durable LangGraph checkpointer when
971
- resume across restarts is required.
972
-
973
- ---
974
-
975
- ## Working as a team
976
-
977
- CodeBuddy is built so teammates share project knowledge without sharing secrets.
978
-
979
- `codebuddy use` / `codebuddy init` create this scaffold:
980
-
981
- ```text
982
- .codebuddy/
983
- ├── .gitignore # keeps config, cache, locks, and review queue out of git
984
- ├── config.json # LOCAL ONLY — never commit (your DB URL + token)
985
- ├── policies.yaml # commit: shared "handle with care" rules
986
- ├── memory/
987
- │ ├── facts/ # commit: reviewable Markdown facts
988
- │ ├── summaries/ # commit: reviewable summaries
989
- │ └── review/ # local: un-approved candidates
990
- ├── plans/ # commit: shared work plans
991
- └── cache/index.json # local: rebuildable index
992
- ```
993
-
994
- - **Commit** `memory/facts`, `memory/summaries`, `plans/*.md`, and
995
- `policies.yaml` to share memory, plans, and policy with the team.
996
- - **Never commit** `config.json` — each teammate runs `codebuddy use` locally
997
- with their own token and database.
998
- - One namespace per project (`codebuddy use my-project`); use the `share` tool to
999
- explicitly hand facts between namespaces.
1000
- - If project memory is private, add `.codebuddy/memory/` to the repo `.gitignore`.
1001
-
1002
- New collaborator quickstart:
1003
-
1004
- ```bash
1005
- git clone <repo> && cd <repo>
1006
- npm install -g @orquentra/codebuddy
1007
- codebuddy use <project> # local DB + migrations + client wiring
1008
- codebuddy index
1009
- codebuddy rules install --client claude
1010
- codebuddy context bootstrap # sanity-check what the agent will see
1011
- ```
1012
-
1013
- ---
1014
-
1015
- ## Repo layout (for contributors)
1016
-
1017
- ```text
1018
- src/
1019
- ├── core/ config, Markdown stores, plan lifecycle, runtime
1020
- ├── db/ Drizzle schema, migrations, client
1021
- ├── mcp/ stdio server + tool registration (Zod schemas)
1022
- ├── risk/ evidence-backed risk assessor + signals (no LLM)
1023
- ├── map/ import-graph architecture map
1024
- ├── context/ [04.2] context bootstrap + before-edit engine
1025
- ├── indexer/ [04.3] scan, summaries, incremental index, watch
1026
- ├── memory-extract/ [04.4] auto-capture, sensitivity scan, review queue
1027
- ├── savings/ [04.5] token estimator and budget-aware packer
1028
- ├── suggest/ [04.6] read-only suggestion engine
1029
- ├── templates/ [04.7] client workflow templates + safe install
1030
- ├── planner/ providers/ langgraph/
1031
- └── cli/ commander CLI (commands/*)
1032
- ```
1033
-
1034
- Dev workflow (see [CLAUDE.md](CLAUDE.md) for agent-oriented notes):
1035
-
1036
- ```bash
1037
- npm run typecheck # tsc --noEmit (strict)
1038
- npm run lint # biome check . (npx biome check --write . to fix)
1039
- npm test # vitest (one integration test skips without Postgres)
1040
- npm run build # tsup → dist/
1041
- ```
1042
-
1043
- ---
1044
-
1045
- ## Troubleshooting
1046
-
1047
- | Symptom | Fix |
1048
- |---|---|
1049
- | `password authentication failed` | `DATABASE_URL` needs `user:password`; use the bundled DB URL or `codebuddy use`. File tools are unaffected. |
1050
- | `namespace ... differs from folder default` | Run `codebuddy use <name>` or set `CODEBUDDY_NAMESPACE`. |
1051
- | `model ... unknown` in doctor | No HF token / model check skipped — informational, not a failure. |
1052
- | Facts/vectors are empty after setup | Restart the MCP server once; startup imports existing Markdown memory and the embedding worker then processes pending vectors. Check `codebuddy doctor` for provider/database errors. |
1053
- | Client doesn't show CodeBuddy tools | Confirm MCP registration (`codebuddy claude list` / `codebuddy codex list`) and restart the client. |
1054
- | `plan list` errors on this repo | Its `.codebuddy/plans/` holds hand-written docs, not generated plans — test plan/context features in a clean scaffold. |
1055
-
1056
- `codebuddy doctor` reports DB connectivity, pgvector, vector/index health, model
1057
- reachability, recent usage, and config permissions.
1058
-
1059
- ---
1060
-
1061
- ## Privacy
1062
-
1063
- - Facts, summaries, and plans are plaintext Markdown — inspect before committing.
1064
- - Conversations, telemetry, and indexes live in PostgreSQL. Reference shares are also
1065
- recorded in the source fact's `sharedWith` front matter; snapshots are fact files in
1066
- the target project.
1067
- - `context_after_turn` never auto-saves sensitive content (keys, tokens,
1068
- credentials, emails); such items are flagged and queued for review.
1069
- - For private memory, add `.codebuddy/memory/` to `.gitignore`.
1070
- - The optional BYOK small model (`codebuddy llm enable`) sends file paths, signatures,
1071
- short summaries and your task text to the provider you choose; see
1072
- [docs/llm-byok.md](docs/llm-byok.md). It's off unless you enable it with your own key.
1073
- - Encryption at rest is the deployment owner's responsibility.
65
+ Previously published as
66
+ [`@ayushkumar320/codebuddy`](https://www.npmjs.com/package/@ayushkumar320/codebuddy),
67
+ which retains the historical releases and download records.
1074
68
 
1075
69
  ## License
1076
70
 
1077
- MIT © Agentic Office — see [LICENSE](LICENSE).
1078
-
1079
- **Project docs:** start at the [docs index](docs/README.md).
1080
- [current-version.md](docs/current-version.md) (what ships today + how automatic
1081
- it really is) · [roadmap.md](docs/roadmap.md) (what's left to build) ·
1082
- [improvements.md](docs/improvements.md) (quality/robustness backlog) ·
1083
- [live-test-checklist.md](docs/live-test-checklist.md) (pre-release verification) ·
1084
- [migration-2.0.md](docs/migration-2.0.md) · [degraded-mode.md](docs/degraded-mode.md) ·
1085
- [CLAUDE_CODEX.md](CLAUDE_CODEX.md).
1086
-
1087
- ### Context quality and delivery cost
1088
-
1089
- Workflow installs now select `CODEBUDDY_TOOL_PROFILE=core`; set it to `full` in
1090
- that client's server environment when direct memory, plan or map tools are needed.
1091
- Manually configured servers are unaffected until reinstalled; reinstalling a workflow selects core again.
1092
-
1093
- For clients that forward both MCP text and structured output to the model,
1094
- `CODEBUDDY_RESPONSE_FORMAT=text` sends the same complete JSON once as text.
1095
- The default `both` preserves existing structured consumers. Verify your host's
1096
- rendering before selecting text mode. This changes delivery representation, not
1097
- which evidence is selected or the compact-JSON budget contract.
1098
-
1099
- Prefer sufficient task evidence over the smallest possible payload. Reuse context
1100
- already delivered; read missing bodies before editing, and narrow the request or
1101
- increase its budget when safety evidence is omitted. Lean remains optional.
1102
-
1103
- ### Requesting deeper task evidence
1104
-
1105
- `context_pack` and `context_before_edit` accept `evidence: "source"` when the
1106
- initial short excerpt omits behavior you need to inspect. Pass the known paths
1107
- and task. Source mode returns up to 40 lines / 1,800 characters per selected span
1108
- with a global 5,400-character excerpt ceiling; the existing final token budget
1109
- still applies. Truncated excerpts remain labelled. The default is `overview`.
1110
-
1111
- Task packs use compatible semantic fact scores when the MCP server's database is
1112
- already ready, with a 350 ms deadline and lexical fallback. File-only operation
1113
- and hook processes remain available without a database. Facts are filtered through
1114
- the current Markdown eligibility rules before any semantic scores are applied.
1115
-
1116
- Graphs generated with `codebuddy graphify index` now include a source-hash
1117
- snapshot beside the graph. Source hashes are captured before extraction; changed
1118
- endpoints are excluded even if timestamps are restored. Regenerate older graphs
1119
- to enable this check; unstamped legacy graphs retain the timestamp fallback.
71
+ MIT. Copyright © 2026 Orquentra Inc.