@exero1/claudecontext 0.1.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 (78) hide show
  1. package/README.md +286 -0
  2. package/dist/installer/install.d.ts +12 -0
  3. package/dist/installer/install.d.ts.map +1 -0
  4. package/dist/installer/install.js +714 -0
  5. package/dist/installer/install.js.map +1 -0
  6. package/dist/src/cache/budget.d.ts +48 -0
  7. package/dist/src/cache/budget.d.ts.map +1 -0
  8. package/dist/src/cache/budget.js +55 -0
  9. package/dist/src/cache/budget.js.map +1 -0
  10. package/dist/src/cache/compressor.d.ts +21 -0
  11. package/dist/src/cache/compressor.d.ts.map +1 -0
  12. package/dist/src/cache/compressor.js +89 -0
  13. package/dist/src/cache/compressor.js.map +1 -0
  14. package/dist/src/cache/levels.d.ts +16 -0
  15. package/dist/src/cache/levels.d.ts.map +1 -0
  16. package/dist/src/cache/levels.js +41 -0
  17. package/dist/src/cache/levels.js.map +1 -0
  18. package/dist/src/cache/manager.d.ts +38 -0
  19. package/dist/src/cache/manager.d.ts.map +1 -0
  20. package/dist/src/cache/manager.js +196 -0
  21. package/dist/src/cache/manager.js.map +1 -0
  22. package/dist/src/cli.d.ts +3 -0
  23. package/dist/src/cli.d.ts.map +1 -0
  24. package/dist/src/cli.js +279 -0
  25. package/dist/src/cli.js.map +1 -0
  26. package/dist/src/detection/areas.d.ts +13 -0
  27. package/dist/src/detection/areas.d.ts.map +1 -0
  28. package/dist/src/detection/areas.js +96 -0
  29. package/dist/src/detection/areas.js.map +1 -0
  30. package/dist/src/detection/task.d.ts +28 -0
  31. package/dist/src/detection/task.d.ts.map +1 -0
  32. package/dist/src/detection/task.js +77 -0
  33. package/dist/src/detection/task.js.map +1 -0
  34. package/dist/src/gating/gate.d.ts +38 -0
  35. package/dist/src/gating/gate.d.ts.map +1 -0
  36. package/dist/src/gating/gate.js +74 -0
  37. package/dist/src/gating/gate.js.map +1 -0
  38. package/dist/src/graph/edges.d.ts +41 -0
  39. package/dist/src/graph/edges.d.ts.map +1 -0
  40. package/dist/src/graph/edges.js +115 -0
  41. package/dist/src/graph/edges.js.map +1 -0
  42. package/dist/src/graph/indexer.d.ts +38 -0
  43. package/dist/src/graph/indexer.d.ts.map +1 -0
  44. package/dist/src/graph/indexer.js +228 -0
  45. package/dist/src/graph/indexer.js.map +1 -0
  46. package/dist/src/graph/traversal.d.ts +25 -0
  47. package/dist/src/graph/traversal.d.ts.map +1 -0
  48. package/dist/src/graph/traversal.js +173 -0
  49. package/dist/src/graph/traversal.js.map +1 -0
  50. package/dist/src/index.d.ts +3 -0
  51. package/dist/src/index.d.ts.map +1 -0
  52. package/dist/src/index.js +82 -0
  53. package/dist/src/index.js.map +1 -0
  54. package/dist/src/indexing/codebase.d.ts +30 -0
  55. package/dist/src/indexing/codebase.d.ts.map +1 -0
  56. package/dist/src/indexing/codebase.js +127 -0
  57. package/dist/src/indexing/codebase.js.map +1 -0
  58. package/dist/src/markdown/writer.d.ts +34 -0
  59. package/dist/src/markdown/writer.d.ts.map +1 -0
  60. package/dist/src/markdown/writer.js +96 -0
  61. package/dist/src/markdown/writer.js.map +1 -0
  62. package/dist/src/server.d.ts +15 -0
  63. package/dist/src/server.d.ts.map +1 -0
  64. package/dist/src/server.js +520 -0
  65. package/dist/src/server.js.map +1 -0
  66. package/dist/src/storage/db.d.ts +123 -0
  67. package/dist/src/storage/db.d.ts.map +1 -0
  68. package/dist/src/storage/db.js +318 -0
  69. package/dist/src/storage/db.js.map +1 -0
  70. package/dist/src/utils/glob.d.ts +11 -0
  71. package/dist/src/utils/glob.d.ts.map +1 -0
  72. package/dist/src/utils/glob.js +20 -0
  73. package/dist/src/utils/glob.js.map +1 -0
  74. package/hooks/post-write.mjs +57 -0
  75. package/hooks/pre-compact.mjs +44 -0
  76. package/hooks/pre-tool-use.mjs +87 -0
  77. package/hooks/session-start.mjs +54 -0
  78. package/package.json +51 -0
package/README.md ADDED
@@ -0,0 +1,286 @@
1
+ # ClaudeContext
2
+
3
+ Persistent memory for Claude Code. When the context window compacts, your session state survives.
4
+
5
+ ClaudeContext gives Claude a two-part memory system: a **hierarchical level cache** that guarantees deterministic context from day one, and an **association graph** that discovers related context you never knew to ask for. Together, they solve the compaction problem without requiring a larger context window.
6
+
7
+ ---
8
+
9
+ ## The Problem
10
+
11
+ Claude Code sessions are bounded. When the context window fills up, Claude Code compacts — summarizing recent history and starting fresh. That compaction event destroys:
12
+
13
+ - Which files you were editing and why
14
+ - What decisions were made two hours ago
15
+ - Which architectural patterns you established earlier
16
+ - The thread of reasoning connecting your current branch to past work
17
+
18
+ Every compaction forces you to manually re-explain your intent. On long-running features, multi-branch projects, or codebases with deep architectural context, that overhead compounds into hours of lost productivity per week.
19
+
20
+ ClaudeContext intercepts that compaction, stores the important state, and rebuilds it automatically at the start of every session.
21
+
22
+ ---
23
+
24
+ ## Quick Install
25
+
26
+ **Prerequisites:** Node.js 22 or later, Claude Code installed.
27
+
28
+ ```bash
29
+ npm install -g claudecontext
30
+ ```
31
+
32
+ **Per-project setup** (run inside your project directory):
33
+
34
+ ```bash
35
+ claudecontext init
36
+ ```
37
+
38
+ This runs an interactive wizard that:
39
+ - Detects your project structure and identifies main subsystems (auth, api, db, etc.)
40
+ - Generates a populated `CLAUDE.md` with your project description and conventions
41
+ - Creates `.claudecontext/state.db` (SQLite, WAL mode)
42
+ - Installs four hook scripts into `.claude/hooks/`
43
+ - Writes `.mcp.json` pointing to the MCP server via `npx`
44
+ - Creates a `tasks/` directory for per-branch task files
45
+
46
+ **Global install for all projects:**
47
+
48
+ ```bash
49
+ claudecontext global-install
50
+ ```
51
+
52
+ Registers ClaudeContext as a global MCP server across all Claude Code projects.
53
+
54
+ ---
55
+
56
+ ## How It Works
57
+
58
+ ClaudeContext combines two systems that work together to give Claude the right context at the right time.
59
+
60
+ ### System 1: Hierarchical Cache Levels
61
+
62
+ Five levels of storage, each with a defined budget and purpose:
63
+
64
+ | Level | Name | Storage | Token Budget | Purpose |
65
+ |-------|------|---------|-------------|---------|
66
+ | L0 | Working set | `.claudecontext/l0.log` | 3,000 | Recent tool calls, errors, file writes — append-only, recovered on restart |
67
+ | L1 | Task cache | `tasks/<branch-slug>.md` | 8,000 | Current task: goal, decisions, files touched, open questions |
68
+ | L2 | Area docs | `docs/context/<area>.md` | 10,000 | Subsystem architecture, API contracts — matched by file path |
69
+ | L3 | Project cache | `CLAUDE.md` | — | Rules, conventions, invariants — NOT in bundle (auto-loaded by Claude Code) |
70
+ | L4 | Decision archive | `.claudecontext/archive/` | 2,000 | Historical decisions, session summaries |
71
+
72
+ At session start, `context_get_bundle` assembles these levels into a single markdown document injected as `additionalContext`. Unused budget from any level flows to the graph discovery budget.
73
+
74
+ ### System 2: Association Graph
75
+
76
+ A SQLite-backed directed graph that learns which files and concepts belong together. Graph edges are created and reinforced automatically during normal coding:
77
+
78
+ | Edge Type | Direction | How Created | V1/V2 |
79
+ |-----------|-----------|-------------|-------|
80
+ | `imports` | file → file | AST analysis (acorn) on every file write | V1 |
81
+ | `subsystem_of` | file → area | File path matches area glob pattern | V1 |
82
+ | `co_modified` | file ↔ file | Two files written in the same session | V1 |
83
+ | `always_with` | file ↔ file | Statistical co-occurrence across 3+ sessions | V2 |
84
+ | `informed_by` | task → archive | Keyword match to archived decisions | V2 |
85
+ | `caused_by` | task → archive | Error in session matches archived decision | V2 |
86
+ | `breaks_with` | file → file | File change causes test failure for another file | V2 |
87
+
88
+ During bundle assembly, the graph is traversed starting from the files listed in your active task (L1). In V1, direct neighbors (1 hop) are loaded in weight-descending order until the token budget is exhausted. In V2, a priority-queue BFS expands up to 3 hops.
89
+
90
+ Edge weights start at 1.0 and decay exponentially (half-life ~70 days). Reinforcement adds 0.1, capped at 1.0. Edges below weight 0.1 and unreinforced for 90 days are pruned.
91
+
92
+ **Day 1:** graph is empty — bundle is levels only. Fully functional immediately.
93
+ **Day 30:** graph has hundreds of weighted edges — bundle discovers context you never knew to ask for.
94
+
95
+ ---
96
+
97
+ ## MCP Tools Reference
98
+
99
+ Claude Code can call these six tools directly. The `CLAUDE.md` generated by `claudecontext init` instructs Claude to use them automatically.
100
+
101
+ | Tool | Read-Only | Purpose |
102
+ |------|-----------|---------|
103
+ | `context_detect_task` | Yes | Infer active task from git branch and database |
104
+ | `context_get_bundle` | Yes | Build unified context bundle: levels + graph-discovered files |
105
+ | `context_update_task` | No | Patch the active task file (L1) with new decisions or files |
106
+ | `context_update_project` | No | Patch `CLAUDE.md` (L3) with project-wide conventions |
107
+ | `context_search_codebase` | Yes | Search indexed files by query, boosted by graph neighbor relationships |
108
+ | `context_status` | Yes | Show token breakdown per level, edge counts, active task, graph age |
109
+
110
+ Full parameter reference: [docs/API.md](docs/API.md)
111
+
112
+ ---
113
+
114
+ ## CLI Reference
115
+
116
+ The `claudecontext-cli` binary is used internally by hooks and is also useful for inspection:
117
+
118
+ | Command | Purpose |
119
+ |---------|---------|
120
+ | `claudecontext init` | Interactive project setup wizard |
121
+ | `claudecontext global-install` | Register as global MCP server |
122
+ | `claudecontext remove` | Remove hooks and `.mcp.json` (preserves state DB) |
123
+ | `claudecontext migrate` | Apply schema migrations for version upgrades |
124
+ | `claudecontext doctor` | Check Node version, DB integrity, hook permissions, `.mcp.json` |
125
+ | `claudecontext-cli status` | Print current task, edge counts, file index size |
126
+ | `claudecontext-cli graph-inspect <file>` | Show all incoming and outgoing edges for a file |
127
+ | `claudecontext-cli gate-check <input>` | Test gate rules against a command string |
128
+ | `claudecontext-cli ingest-diff <file...>` | Record file touches and create/reinforce edges |
129
+ | `claudecontext-cli hydrate` | Build bundle and output `additionalContext` JSON |
130
+
131
+ ---
132
+
133
+ ## Bundle Format
134
+
135
+ When `context_get_bundle` runs, it returns a markdown document with this structure:
136
+
137
+ ```markdown
138
+ <!-- CLAUDECONTEXT BUNDLE (L0=847 L1=3.2k L2=6.1k L4=1.8k Graph=4.3k | Total=16.2k) -->
139
+ <!-- Task: feature-add-auth | Branch: feature/add-auth | Graph depth: 1 hop(s) -->
140
+
141
+ ---
142
+
143
+ ## Task Context (L1 — tasks/feature-add-auth.md)
144
+ <!-- CONTENT START -->
145
+ # Task: feature-add-auth
146
+ **Branch:** feature/add-auth
147
+ **Created:** 2026-02-20T09:00:00.000Z
148
+ **Status:** active
149
+
150
+ ## Goal
151
+ Add JWT-based authentication to the API layer.
152
+
153
+ ## Files touched
154
+ - src/auth.ts
155
+ - src/db/users.ts
156
+
157
+ ## Decisions
158
+ - Use RS256 over HS256 for cross-service token validation
159
+ <!-- CONTENT END -->
160
+
161
+ ---
162
+
163
+ ## Working Set (L0 — recent activity)
164
+ <!-- CONTENT START -->
165
+ [2026-02-20T10:12:00Z] WRITE src/auth.ts
166
+ [2026-02-20T10:13:45Z] WRITE src/db/users.ts
167
+ [2026-02-20T10:14:01Z] Tool error in context_get_bundle: ...
168
+ <!-- CONTENT END -->
169
+
170
+ ---
171
+
172
+ ## Area Documentation (L2)
173
+ <!-- CONTENT START -->
174
+ ### auth
175
+ [Contents of docs/context/auth.md — subsystem architecture, API contracts]
176
+ <!-- CONTENT END -->
177
+
178
+ ---
179
+
180
+ ## Decision Archive (L4)
181
+ <!-- CONTENT START -->
182
+ **[2026-02-15] feature-jwt-refresh:** Decided to store refresh tokens in HttpOnly cookies, not localStorage, due to XSS risk discovered in security review.
183
+ <!-- CONTENT END -->
184
+
185
+ ---
186
+
187
+ ## Graph-Discovered Context
188
+ ### `src/auth/jwt_handler.ts` [via imports from src/auth.ts (weight=0.91)]
189
+ <!-- FILE CONTENT START -->
190
+ [contents of jwt_handler.ts]
191
+ <!-- FILE CONTENT END -->
192
+
193
+ ---
194
+
195
+ <!-- END CLAUDECONTEXT BUNDLE -->
196
+ ```
197
+
198
+ All file content is wrapped in `<!-- FILE CONTENT START -->` delimiters to prevent prompt injection from file contents.
199
+
200
+ ---
201
+
202
+ ## Configuration
203
+
204
+ ### CLAUDECONTEXT_PROJECT_ROOT
205
+
206
+ By default, ClaudeContext uses the current working directory as the project root. Override it with:
207
+
208
+ ```bash
209
+ export CLAUDECONTEXT_PROJECT_ROOT=/path/to/your/project
210
+ ```
211
+
212
+ This is useful when running Claude Code from a directory other than your project root, or when managing multiple projects.
213
+
214
+ ### areas.json
215
+
216
+ The file `.claudecontext/areas.json` defines your project's subsystems. Generated by `claudecontext init`, but editable manually:
217
+
218
+ ```json
219
+ [
220
+ {
221
+ "name": "auth",
222
+ "globs": ["src/auth/**", "src/middleware/auth*"],
223
+ "docPath": "docs/context/auth.md"
224
+ },
225
+ {
226
+ "name": "database",
227
+ "globs": ["src/db/**", "src/models/**"],
228
+ "docPath": "docs/context/database.md"
229
+ }
230
+ ]
231
+ ```
232
+
233
+ When a file matches a glob, a `subsystem_of` edge is created linking the file to its area. The area's `docPath` is loaded as L2 area documentation whenever that area is relevant to the active task.
234
+
235
+ ### .mcp.json
236
+
237
+ Created by `claudecontext init`. Uses `npx` — never absolute paths, so it works across machines and npm updates:
238
+
239
+ ```json
240
+ {
241
+ "mcpServers": {
242
+ "claudecontext": {
243
+ "command": "npx",
244
+ "args": ["--node-options=--no-warnings", "claudecontext-mcp"],
245
+ "env": {
246
+ "CLAUDECONTEXT_PROJECT_ROOT": "${workspaceFolder}"
247
+ }
248
+ }
249
+ }
250
+ }
251
+ ```
252
+
253
+ ---
254
+
255
+ ## Prerequisites
256
+
257
+ - **Node.js 22 or later** — required for `node:sqlite` (built-in SQLite, no native addon)
258
+ - **Claude Code** — the MCP server connects via stdio transport; requires Claude Code as the host
259
+ - **Git** (optional) — branch detection uses `simple-git`; falls back to `default` task if not a git repo
260
+
261
+ ---
262
+
263
+ ## Contributing
264
+
265
+ ClaudeContext is built with TypeScript + Node.js ESM. Every import must use `.js` extensions (NodeNext module resolution).
266
+
267
+ ```bash
268
+ git clone https://github.com/your-org/claudecontext
269
+ cd claudecontext
270
+ npm install
271
+ npm run build
272
+ npm run test
273
+ ```
274
+
275
+ Before submitting a PR:
276
+ - Run `npm run build && npm run typecheck && npm run test`
277
+ - Add unit tests for any new functions
278
+ - Never `console.log()` — use `process.stderr.write()` in the MCP server, `process.stdout.write()` in the CLI
279
+ - See [.cursorrules](.cursorrules) for full coding guidelines
280
+ - See [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) for system design
281
+
282
+ Phase tracker: [docs/PHASES.md](docs/PHASES.md)
283
+ API reference: [docs/API.md](docs/API.md)
284
+ Product requirements: [docs/PRD.md](docs/PRD.md)
285
+
286
+ **License:** MIT
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * ClaudeContext installer.
4
+ *
5
+ * Commands:
6
+ * claudecontext init [--project-root <path>] Install in a project
7
+ * claudecontext remove Remove hooks/config (preserves state.db)
8
+ * claudecontext migrate Migrate schema for upgrades
9
+ * claudecontext doctor Check installation health
10
+ */
11
+ export {};
12
+ //# sourceMappingURL=install.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"install.d.ts","sourceRoot":"","sources":["../../installer/install.ts"],"names":[],"mappings":";AACA;;;;;;;;GAQG"}