Compare commits
182 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ebefae864e | |||
| 0cd931bb06 | |||
| 4c792f026d | |||
| aa7cdb6d9f | |||
| 5db90f2ea0 | |||
| 4ddf57610a | |||
| d0fc68c630 | |||
| 1d7500604f | |||
| 05232ff091 | |||
| b411d91885 | |||
| 4538e686ad | |||
| f97c50bfb9 | |||
| 983be42998 | |||
| 544e9d39f5 | |||
| 16a0737dfc | |||
| 3d92684e04 | |||
| 471e1f62f9 | |||
| f44605658d | |||
| eeb6841033 | |||
| 2a2008bac2 | |||
| d64c252f4d | |||
| 59ce0fc553 | |||
| 31ee1024c5 | |||
| 7d5d4c5036 | |||
| 06b997e3d0 | |||
| a390a537c9 | |||
| 2357835942 | |||
| 77a22d30b2 | |||
| 40a25e0225 | |||
| 4c2ab98d90 | |||
| 7bcfd73985 | |||
| 7dd321f869 | |||
| 153ddb814b | |||
| 216d17879d | |||
| fa73dd483c | |||
| 9dd0ae10a3 | |||
| 9a91a1be2b | |||
| a5b2c26592 | |||
| fc9331fc39 | |||
| ff17609a81 | |||
| fe8737420d | |||
| 8275b3da3b | |||
| b7c23ca232 | |||
| edc8535ac1 | |||
| ad127bec40 | |||
| 2f19eab9c2 | |||
| e7bf2ac65a | |||
| 5ac54239d8 | |||
| 08cf2ba3bd | |||
| c7c4fd54d6 | |||
| f61eb2d162 | |||
| e9a234308a | |||
| e398212983 | |||
| 36a03f75b2 | |||
| 5676cab83f | |||
| 126129fbac | |||
| cde4faae2f | |||
| b701bf29e6 | |||
| c648d5d8d2 | |||
| 07be61cf91 | |||
| f7fd2221c8 | |||
| 6461d718f2 | |||
| 29f2d0bc02 | |||
| abd55977ca | |||
| 1fc66add67 | |||
| 25ccf46ac0 | |||
| 1a6a68cac8 | |||
| a0e895b53b | |||
| 753a993647 | |||
| d0676aa049 | |||
| 7996dfd5cd | |||
| 95889c7b4e | |||
| 25bb93a995 | |||
| c21e49d9fa | |||
| f4570f2a0a | |||
| cbb68ad9e1 | |||
| b8999c1181 | |||
| 052da384b2 | |||
| d8947473b8 | |||
| 53f98fad67 | |||
| 64062ac761 | |||
| 8cdabe6315 | |||
| e3475180cd | |||
| ef1b427a2a | |||
| 455aeaf654 | |||
| 31910fb265 | |||
| 6250a194dd | |||
| 3b935294bf | |||
| 58fcd85724 | |||
| 2d5480b5e4 | |||
| c1a3fc27ec | |||
| d570909bf1 | |||
| 5dd2a6f758 | |||
| c3cb8f81ed | |||
| 8d02271321 | |||
| 54289b34e6 | |||
| 5a52121216 | |||
| 5cffff7d40 | |||
| d63d73acc2 | |||
| 9a4afab4c2 | |||
| 832bd755ed | |||
| 995f69e4e9 | |||
| 842d614adb | |||
| b1da4c7e2c | |||
| 4d2bb1f13e | |||
| a9de029c02 | |||
| a4115d055a | |||
| 53c1fc9a70 | |||
| 79d3ca6aaa | |||
| 85f57e6440 | |||
| 36de44d661 | |||
| f32fda8b35 | |||
| 4509da1409 | |||
| b0f70b8302 | |||
| 1f808c0be7 | |||
| a28eddb925 | |||
| a60f79c44d | |||
| 5e696888d6 | |||
| 17fa383450 | |||
| 9f01228a2b | |||
| 18aa5dc4e7 | |||
| 6cb74c6183 | |||
| 0f9745535a | |||
| f81684c61c | |||
| 7def736f0a | |||
| d3262ae1f4 | |||
| 2b8fbcf50e | |||
| 0099a196c5 | |||
| 41010c527d | |||
| 753837bff3 | |||
| 76a27296f0 | |||
| e2d4babae8 | |||
| 00ab61b46e | |||
| c3e5f3a79e | |||
| 6c0dcd9a4a | |||
| 8c03704246 | |||
| 91f73a83bc | |||
| c74101b7f7 | |||
| 1b5d1a1234 | |||
| c4146cca67 | |||
| eea9c100ba | |||
| 16f79d6f71 | |||
| a74ff0034f | |||
| a66b98bcdd | |||
| bd47a919a8 | |||
| 4d4b0a2f24 | |||
| 472d302133 | |||
| 303aafa64b | |||
| 67645041fa | |||
| d8eb2fa9f9 | |||
| 93a30c5c8f | |||
| 2a304d59eb | |||
| 12501412b9 | |||
| fb8c9dbdbe | |||
| b81281fd6c | |||
| 247d287bdc | |||
| 2a6c9ea2b7 | |||
| 4589b34eab | |||
| 7fce21c145 | |||
| b0f1a458cf | |||
| 83f61177c7 | |||
| 88b47f9e9c | |||
| f86be1ef2b | |||
| a48bf89963 | |||
| 368daddd88 | |||
| ed444dfec7 | |||
| 4aa7119d7d | |||
| 9cfa57d498 | |||
| fe8c65a8cd | |||
| 4f6fb9e614 | |||
| 2b60dd2932 | |||
| b6f9950bb3 | |||
| 4324f6bbc1 | |||
| df1fb8bb89 | |||
| 5b041d6b49 | |||
| abb5940788 | |||
| d88ea71590 | |||
| c80763390b | |||
| 47d6d51030 | |||
| e07b13f7de | |||
| 1d48f63b99 | |||
| fb9d917f8a |
@@ -10,7 +10,7 @@
|
||||
"plugins": [
|
||||
{
|
||||
"name": "claude-mem",
|
||||
"version": "11.0.0",
|
||||
"version": "12.1.4",
|
||||
"source": "./plugin",
|
||||
"description": "Persistent memory system for Claude Code - context compression across sessions"
|
||||
}
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
{
|
||||
"name": "claude-mem",
|
||||
"version": "10.4.1",
|
||||
"description": "Persistent memory system for Claude Code - seamlessly preserve context across sessions",
|
||||
"version": "12.1.4",
|
||||
"description": "Memory compression system for Claude Code - persist context across sessions",
|
||||
"author": {
|
||||
"name": "Alex Newman"
|
||||
},
|
||||
"repository": "https://github.com/thedotmack/claude-mem",
|
||||
"license": "AGPL-3.0",
|
||||
"keywords": [
|
||||
"claude",
|
||||
"claude-code",
|
||||
"claude-agent-sdk",
|
||||
"mcp",
|
||||
"plugin",
|
||||
"memory",
|
||||
"context",
|
||||
"persistence",
|
||||
"hooks",
|
||||
"mcp"
|
||||
]
|
||||
"compression",
|
||||
"knowledge-graph",
|
||||
"transcript",
|
||||
"typescript",
|
||||
"nodejs"
|
||||
],
|
||||
"homepage": "https://github.com/thedotmack/claude-mem#readme"
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"sessionId":"6a00de6e-282e-4cd8-98ec-b5afb73c468d","pid":50072,"acquiredAt":1775678989779}
|
||||
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "claude-mem",
|
||||
"version": "12.1.4",
|
||||
"description": "Memory compression system for Claude Code - persist context across sessions",
|
||||
"author": {
|
||||
"name": "Alex Newman",
|
||||
"url": "https://github.com/thedotmack"
|
||||
},
|
||||
"homepage": "https://github.com/thedotmack/claude-mem#readme",
|
||||
"repository": "https://github.com/thedotmack/claude-mem",
|
||||
"license": "AGPL-3.0",
|
||||
"keywords": [
|
||||
"claude",
|
||||
"claude-code",
|
||||
"claude-agent-sdk",
|
||||
"mcp",
|
||||
"plugin",
|
||||
"memory",
|
||||
"compression",
|
||||
"knowledge-graph",
|
||||
"transcript",
|
||||
"typescript",
|
||||
"nodejs"
|
||||
],
|
||||
"interface": {
|
||||
"displayName": "claude-mem",
|
||||
"shortDescription": "Persistent memory and context compression across coding sessions.",
|
||||
"longDescription": "claude-mem captures coding-session activity, compresses it into reusable observations, and injects relevant context back into future Claude Code and Codex-compatible sessions.",
|
||||
"developerName": "Alex Newman",
|
||||
"category": "Productivity",
|
||||
"capabilities": [
|
||||
"Interactive",
|
||||
"Write"
|
||||
],
|
||||
"websiteURL": "https://github.com/thedotmack/claude-mem",
|
||||
"defaultPrompt": [
|
||||
"Find what I already learned about this codebase before I start a new task.",
|
||||
"Show recent observations related to the files I am editing right now.",
|
||||
"Summarize the last session and inject the most relevant context into this one."
|
||||
],
|
||||
"brandColor": "#1F6FEB"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
# Normalize all text files to LF on commit and checkout.
|
||||
# This prevents CRLF shebang lines in bundled scripts from breaking
|
||||
# the MCP server on macOS/Linux when built on Windows. Fixes #1342.
|
||||
* text=auto eol=lf
|
||||
|
||||
# Compiled plugin scripts must always be LF — CRLF in the shebang
|
||||
# causes "env: node\r: No such file or directory" on non-Windows hosts.
|
||||
plugin/scripts/*.cjs eol=lf
|
||||
plugin/scripts/*.js eol=lf
|
||||
|
||||
# Explicitly mark binary assets so git never modifies them.
|
||||
*.png binary
|
||||
*.jpg binary
|
||||
*.jpeg binary
|
||||
*.ico binary
|
||||
*.gif binary
|
||||
*.woff binary
|
||||
*.woff2 binary
|
||||
*.ttf binary
|
||||
*.eot binary
|
||||
*.otf binary
|
||||
@@ -27,7 +27,7 @@ jobs:
|
||||
|
||||
- name: Comment with AI summary
|
||||
run: |
|
||||
gh issue comment $ISSUE_NUMBER --body '${{ steps.inference.outputs.response }}'
|
||||
gh issue comment "$ISSUE_NUMBER" --body "$RESPONSE"
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
ISSUE_NUMBER: ${{ github.event.issue.number }}
|
||||
|
||||
+319
-3
@@ -4,10 +4,326 @@ All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||
|
||||
## [10.7.2] - 2026-
|
||||
## [12.1.3] - 2026-
|
||||
✅ CHANGELOG.md generated successfully!
|
||||
220 releases processed
|
||||
m error to debug (#1606) — reduces noise in logs by treating routine concept-type cleanup as debug-level rather than error-level logging.
|
||||
228 releases processed
|
||||
mary salvage logic** (#1850) — Reverts PR #1718 which fabricated synthetic summaries from observation data when the AI returned `<observation>` instead of `<summary>` tags. Missing a summary is preferable to creating a fake one with poorly-mapped fields.
|
||||
|
||||
**Full Changelog**: https://github.com/thedotmack/claude-mem/compare/v12.1.2...v12.1.3
|
||||
|
||||
## [12.1.2] - 2026-04-15
|
||||
|
||||
## Community PRs merged (15)
|
||||
|
||||
**Runtime & reliability**
|
||||
- #1698 Reap stuck generators in reapStaleSessions (@ousamabenyounes)
|
||||
- #1697 Circuit breaker on OpenClaw worker client (@ousamabenyounes)
|
||||
- #1696 Resolve Setup hook reference, warn on macOS-only binary (@ousamabenyounes)
|
||||
- #1693 Session lifecycle guards to prevent runaway API spend (@ousamabenyounes)
|
||||
- #1692 Resolve Gemini CLI 0.37.0 session capture failures (@ousamabenyounes)
|
||||
|
||||
**Cross-platform & hooks**
|
||||
- #1833 Replace hardcoded nvm/homebrew PATH with login-shell resolution (@masak1yu)
|
||||
- #1781 Filter empty-string args before Bun spawn() (@biswanath-cmd)
|
||||
- #1780 Fix npx search, default Codex context to workspace-local AGENTS (@enma998)
|
||||
|
||||
**Data integrity**
|
||||
- #1820 Use parent project name for worktree observation writes (@0xLeathery)
|
||||
- #1771 Exclude primary-key index from unique-constraint check in migration 7 (@derjochenmeyer)
|
||||
- #1770 Restrict ~/.claude-mem/.env permissions to 0600 (@derjochenmeyer)
|
||||
- #1729 Preserve targeted file reads and invalidate on mtime (@quangtran88)
|
||||
- #1776 Coerce corpus route filters (@suyua9)
|
||||
|
||||
**Docs**
|
||||
- #1777 Document CLAUDE_MEM_MODE (@AviArora02-commits)
|
||||
- #1765 Update opencode install instructions (@s-uryansh)
|
||||
|
||||
## Held for rebase
|
||||
- #1748, #1694, #1695 — developed conflicts during batch merge
|
||||
|
||||
## Test baseline
|
||||
1429 pass / 11 fail (improved from 18 fail at v12.1.1)
|
||||
|
||||
## [12.1.1] - 2026-04-15
|
||||
|
||||
14 community PRs merged + 1 post-merge bug fix. This patch addresses the most impactful bugs across summary persistence, MCP compliance, cross-platform compatibility, and data integrity.
|
||||
|
||||
### Highlights
|
||||
|
||||
**Summary pipeline fix** — When the LLM returns `<observation>` tags instead of `<summary>` tags (~72% of the time on v12.0.x), data is now salvaged into a synthetic summary instead of being silently discarded. (#1718)
|
||||
|
||||
**MCP compliance** — `list_corpora` now returns proper `CallToolResult` objects instead of bare arrays that crashed MCP clients. Search and timeline tools now declare `inputSchema.properties`. (#1701, #1555)
|
||||
|
||||
**Data integrity** — Ghost observations with no content fields are now filtered before storage. Search queries are now scoped to the current project via `WHERE project = ?`. (#1676, #1688... wait, #1688 wasn't in this batch)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- **fix(ResponseProcessor):** salvage synthetic summary when AI returns `<observation>` instead of `<summary>` (#1718)
|
||||
- **fix(ResponseProcessor):** broadcast uses `summaryForStore` to support salvaged summaries (post-merge fix for #1718)
|
||||
- **fix(hooks):** soft-fail SessionStart health check on cold start (#1725)
|
||||
- **fix(deps):** upgrade glob ^11.0.3 → ^13.0.0 for CVE fix (#1724, #1717)
|
||||
- **fix(MCP):** wrap `list_corpora` response in CallToolResult shape (#1701, #1700)
|
||||
- **fix(MCP):** declare inputSchema properties for search and timeline tools (#1555, #1384, #1413)
|
||||
- **fix(config):** use bun to run mcp-server.cjs instead of node shebang (#1658, #1648)
|
||||
- **fix(parser):** filter ghost observations with no content fields (#1676, #1625)
|
||||
- **fix(chroma):** set cwd to homedir when spawning chroma-mcp to prevent .env.local crash (#1679, #1297)
|
||||
- **fix(Windows):** avoid DEP0190 deprecation by using single-string spawnSync (#1677, #1503)
|
||||
- **fix(worker):** suppress false ERROR when duplicate daemon loses port bind race (#1680, #1447)
|
||||
- **fix(session):** expose `summaryStored` in session status for silent summary loss detection (#1686, #1633)
|
||||
- **fix(cross-platform):** add .gitattributes to enforce LF endings on plugin scripts (#1678, #1342)
|
||||
- **fix(tests):** remove leaky mock.module() that polluted parallel workers (#1666, #1299)
|
||||
|
||||
### Docs
|
||||
|
||||
- Add Language Support section to smart-explore/SKILL.md (#1670, #1651)
|
||||
- Remove misplaced tree-sitter docs from mem-search/SKILL.md
|
||||
|
||||
### Contributors
|
||||
|
||||
@ousamabenyounes (10 PRs), @aaronwong1989, @kbroughton, @joao-oliveira-softtor, @octo-patch, @ck0park
|
||||
|
||||
## [12.1.0] - 2026-04-09
|
||||
|
||||
## Knowledge Agents
|
||||
|
||||
Build queryable AI "brains" from your claude-mem observation history. Compile a filtered slice of your past work into a corpus, prime it into a Claude session, and ask questions conversationally — getting synthesized, grounded answers instead of raw search results.
|
||||
|
||||
### New Features
|
||||
|
||||
- **Knowledge Agent system** — full lifecycle: build, prime, query, reprime, rebuild, delete
|
||||
- **6 new MCP tools**: `build_corpus`, `list_corpora`, `prime_corpus`, `query_corpus`, `rebuild_corpus`, `reprime_corpus`
|
||||
- **8 new HTTP API endpoints** on the worker service (`/api/corpus/*`)
|
||||
- **CorpusBuilder** — searches observations, hydrates full records, calculates stats, persists to `~/.claude-mem/corpora/`
|
||||
- **CorpusRenderer** — renders observations into full-detail prompt text for the 1M token context window
|
||||
- **KnowledgeAgent** — manages Agent SDK sessions with session resume for multi-turn Q&A
|
||||
- **Auto-reprime** — expired sessions are automatically reprimed and retried (only for session errors, not all failures)
|
||||
- **Knowledge agent skill** (`/knowledge-agent`) for guided corpus creation
|
||||
|
||||
### Security & Robustness
|
||||
|
||||
- Path traversal prevention in CorpusStore (alphanumeric name validation + resolved path check)
|
||||
- System prompt hardened against instruction injection from untrusted corpus content
|
||||
- Runtime name validation on all MCP corpus tool handlers
|
||||
- Question field validated as non-empty string
|
||||
- Session state only persisted after successful prime (not null on failure)
|
||||
- Refreshed session_id persisted after query execution
|
||||
- E2e curl wrappers hardened with connect-timeout and transport failure fallback
|
||||
|
||||
### Documentation
|
||||
|
||||
- New docs page: Knowledge Agents usage guide with Quick Start, architecture diagram, filter reference, and API reference
|
||||
- Knowledge agent skill page with workflow examples
|
||||
- Added to docs navigation
|
||||
|
||||
### Testing
|
||||
|
||||
- Comprehensive e2e test suite (31 tests) covering full corpus lifecycle
|
||||
|
||||
**Full Changelog**: https://github.com/thedotmack/claude-mem/compare/v12.0.1...v12.1.0
|
||||
|
||||
## [12.0.1] - 2026-04-08
|
||||
|
||||
## 🔴 Hotfix: MCP server crashed with `Cannot find module 'bun:sqlite'` under Node
|
||||
|
||||
v12.0.0 shipped a broken MCP server bundle that crashed on the very first `require()` call because a transitive import pulled `bun:sqlite` (a Bun-only module) into a bundle that runs under Node. Every MCP-only client (Codex and any flow that boots the MCP tool surface) was completely broken on v12.0.0.
|
||||
|
||||
### Root cause
|
||||
|
||||
`src/servers/mcp-server.ts` imported `ensureWorkerStarted` from `worker-service.ts`, which transitively pulled in `DatabaseManager` → `bun:sqlite`. The bundle ballooned from ~358KB (v11.0.1) to ~1.96MB (v12.0.0) and `node mcp-server.cjs` immediately threw `Error: Cannot find module 'bun:sqlite'`.
|
||||
|
||||
### Fix
|
||||
|
||||
- **Extracted** `ensureWorkerStarted` and Windows spawn-cooldown helpers into a new lightweight `src/services/worker-spawner.ts` module that has zero database/SQLite/ChromaDB imports
|
||||
- **Wired** `mcp-server.ts` and `worker-service.ts` through the new module via a thin back-compat wrapper
|
||||
- **Fixed** `resolveWorkerRuntimePath()` to find Bun on every platform (not just Windows) so the MCP server running under Node can correctly spawn the worker daemon under Bun
|
||||
- **Added** two build-time guardrails in `scripts/build-hooks.js`:
|
||||
- Regex check: fails the build if `mcp-server.cjs` ever contains a `require("bun:*")` call
|
||||
- Bundle size budget: fails the build if `mcp-server.cjs` exceeds 600KB
|
||||
- **Improved** error messages when Bun cannot be located (now names the install URL and explains *why* Bun is required)
|
||||
- **Validated** `workerScriptPath` at the spawner entry point with empty-string and existsSync guards
|
||||
- **Memoized** `resolveWorkerRuntimePath()` to skip repeated PATH lookups during crash loops, while never caching the not-found result so a long-running MCP server can recover if Bun is installed mid-session
|
||||
|
||||
### Verification
|
||||
|
||||
- `node mcp-server.cjs` exits cleanly under Node
|
||||
- JSON-RPC `initialize` + `tools/list` + `tools/call search` all succeed end-to-end
|
||||
- Bundle is back to ~384KB with zero `require("bun:sqlite")` calls
|
||||
- 47 unit tests pass (44 ProcessManager + 3 worker-spawner)
|
||||
- Both build guardrails verified to trip on simulated regressions
|
||||
- Smoke test: MCP server serves the full 7-tool surface
|
||||
|
||||
### What this means for users
|
||||
|
||||
- **MCP-only clients (Codex, etc.):** v12.0.0 was broken; v12.0.1 restores full functionality
|
||||
- **Claude Code users:** worker startup via the SessionStart hook continued working under Bun on v12.0.0, but the MCP tool surface (`mem-search`, `timeline`, `get_observations`, `smart_*`) was unreliable. v12.0.1 fixes that completely.
|
||||
- **Plugin developers:** new build-time guardrails prevent this regression class from shipping again
|
||||
|
||||
PR: #1645
|
||||
Merge commit: `abd55977`
|
||||
|
||||
## [12.0.0] - 2026-04-07
|
||||
|
||||
# claude-mem v12.0.0
|
||||
|
||||
A major release delivering intelligent file-read gating, expanded language support for smart-explore, platform source isolation, and 40+ bug fixes across Windows, Linux, and macOS.
|
||||
|
||||
## Highlights
|
||||
|
||||
### File-Read Decision Gate
|
||||
Claude Code now intelligently gates redundant file reads. When a file has prior observations in the timeline, the PreToolUse hook injects the observation history and blocks the read — saving tokens and keeping context focused. The gate supports both `Read` and `Edit` tools, uses `permissionDecision` deny with a rich timeline payload, and includes file-size thresholds and observation deduplication.
|
||||
|
||||
### Smart-Explore: 24 Language Support
|
||||
The `smart-explore` skill now supports **24 programming languages** via tree-sitter AST parsing: TypeScript, JavaScript, Python, Rust, Go, Java, C, C++, C#, Ruby, PHP, Swift, Kotlin, Scala, Bash, CSS, SCSS, HTML, Lua, Haskell, Elixir, Zig, TOML, and YAML. User-installable grammars with `--legacy-peer-deps` support for tree-sitter version conflicts.
|
||||
|
||||
### Platform Source Isolation
|
||||
Claude and Codex sessions are now fully isolated with `platform_source` column on `sdk_sessions`. Each platform gets its own session namespace, preventing cross-contamination between different AI coding tools. Normalized at route boundaries for consistent behavior.
|
||||
|
||||
### Codex & OpenClaw Support
|
||||
- Codex plugin manifest added for marketplace discoverability
|
||||
- OpenClaw: `workerHost` config for Docker deployments
|
||||
- OpenClaw: handle stale `plugins.allow` and non-interactive TTY in installer
|
||||
|
||||
## New Features
|
||||
|
||||
- **File-read decision gate** — blocks redundant file reads with observation timeline injection (#1564, #1629, #1641)
|
||||
- **24-language smart-explore** — AST-based code exploration across all major languages
|
||||
- **Platform source isolation** — Claude/Codex session namespacing with DB migration
|
||||
- **CLAUDE.local.md support** — `CLAUDE_MEM_FOLDER_USE_LOCAL_MD` setting for writing to local-only config
|
||||
- **OpenClaw workerHost** — Docker deployment support for OpenClaw plugin
|
||||
- **Codex plugin manifest** — discoverability in Codex marketplace
|
||||
- **File-size threshold** — skip file-read gating for small files
|
||||
- **Observation deduplication** — prevent duplicate observations in timeline gate
|
||||
|
||||
## Bug Fixes
|
||||
|
||||
### Worker & Startup
|
||||
- Fix worker startup crash with missing observation columns (#1641)
|
||||
- Fix SessionStart hooks failing on cold start due to worker race condition
|
||||
- Fix worker daemon being killed by its own hooks (#1490)
|
||||
- Fail worker-start hook if worker never becomes healthy
|
||||
- Fix readiness timeout logging on reused-worker path (#1491)
|
||||
- Remove dead `USER_MESSAGE_ONLY` exit code that caused SessionStart hook errors
|
||||
- Decouple MCP health from loopback self-check
|
||||
|
||||
### Data Integrity
|
||||
- Fix migration version conflict: `addSessionPlatformSourceColumn` now correctly uses v25
|
||||
- Add migration for `generated_by_model` and `relevance_count` columns
|
||||
- Wire `generated_by_model` into observation write path
|
||||
- Use null-byte delimiter in observation content hash to prevent collisions
|
||||
- Persist session completion to database in `completeByDbId` (#1532)
|
||||
- Handle bare path strings in `files_modified`/`files_read` columns (#1359)
|
||||
- Guard `json_each()` calls against legacy bare-path rows
|
||||
- Deduplicate session init to prevent multiple prompt records
|
||||
|
||||
### Security
|
||||
- Prevent shell injection in summary workflow (#1285)
|
||||
- Sanitize observation titles in file-context deny reason (strip newlines, collapse whitespace)
|
||||
- Normalize `platformSource` at route boundary to prevent filter inconsistencies
|
||||
- Escape `filePath` in recovery hints to prevent malformed output
|
||||
- Address path safety, SQL injection, and gate scoping in file-read hook
|
||||
|
||||
### Windows
|
||||
- Fix `isMainModule` CJS branch failure on Bun — add `CLAUDE_MEM_MANAGED` fallback
|
||||
- Use `cmd /c` to execute `bun.cmd` on Windows
|
||||
- Prefer `bun.cmd` over bun shell script on Windows
|
||||
- Add `shell: true` on Windows to spawn bun from npm
|
||||
|
||||
### Cross-Platform
|
||||
- Replace GNU `sort -V` with POSIX-portable version sort
|
||||
- Resolve `node not found` on nvm/homebrew installations
|
||||
- Resolve hook failures when `CLAUDE_PLUGIN_ROOT` is not injected (#1533)
|
||||
- Fix bun-runner signal exit handling — scope to `start` subcommand only
|
||||
- Guard `/stream` SSE endpoint with 503 before DB initialization
|
||||
- Provide empty JSON fallback when stdin is not piped (#1560)
|
||||
|
||||
### Parser & Content
|
||||
- Strip `<persisted-output>` tags from memory
|
||||
- Strip `<system-reminder>` tags from persisted memory and DRY up regex
|
||||
- Skip `parseSummary` false positives with no sub-tags (#1360)
|
||||
- Handle bare filenames in `regenerate-claude-md.ts` (#1514)
|
||||
- Handle bare filenames in `path-utils.ts isDirectChild`
|
||||
- Handle single-quoted paths and dangling var edge case
|
||||
- Strip hardcoded `__dirname`/`__filename` from bundled CJS output
|
||||
- Add PHP grammar support to smart-file-read parser (#1617)
|
||||
|
||||
### Installer & Config
|
||||
- Make post-install allowlist write guaranteed
|
||||
- Harden plugin manifest sync script
|
||||
- Fix `expand ~` to home directory before project resolution
|
||||
- Update default model from `claude-sonnet-4-5` to `claude-sonnet-4-6` (#1390)
|
||||
- Fix Gemini conversation history truncation to prevent O(N²) token cost growth
|
||||
|
||||
## Refactoring
|
||||
|
||||
- Rename formatters to `AgentFormatter`/`HumanFormatter` for semantic clarity
|
||||
|
||||
---
|
||||
|
||||
**Full Changelog**: https://github.com/thedotmack/claude-mem/compare/v11.0.1...v12.0.0
|
||||
|
||||
## [11.0.1] - 2026-04-06
|
||||
|
||||
**Patch release** — Changes `CLAUDE_MEM_SEMANTIC_INJECT` default from `true` to `false`.
|
||||
|
||||
### What changed
|
||||
- Per-prompt Chroma vector search on `UserPromptSubmit` is now **opt-in** rather than opt-out
|
||||
- Reduces latency and context noise for users who haven't explicitly enabled it
|
||||
- Users can re-enable via `CLAUDE_MEM_SEMANTIC_INJECT=true` in `~/.claude-mem/settings.json`
|
||||
|
||||
### Why
|
||||
The semantic inject fires on every prompt and often surfaces tangentially related observations. A more precise file-context approach (PreToolUse timeline gate) is in development as a replacement.
|
||||
|
||||
## [11.0.0] - 2026-04-05
|
||||
|
||||
## claude-mem v11.0.0
|
||||
|
||||
**4 releases today** · 21 commits · 6,051 insertions · 34 files changed
|
||||
|
||||
### Features
|
||||
|
||||
#### Semantic Context Injection (#1568)
|
||||
Every `UserPromptSubmit` now queries ChromaDB for the top-N most relevant past observations and injects them as context. Replaces recency-based "last N observations" with relevance-based semantic search. Survives `/clear`, skips trivial prompts (<20 chars), and degrades gracefully when Chroma is unavailable.
|
||||
|
||||
#### Tier Routing by Queue Complexity
|
||||
The SDK agent now inspects pending queue complexity before selecting a model. Simple tool-only queues (Read, Glob, Grep) route to Haiku; mixed/complex queues use the default model. Production result: **~52% cost reduction** on SDK agent usage with quality indistinguishable from Sonnet. Includes a new `observation_feedback` table for future Thompson Sampling optimization.
|
||||
|
||||
#### Multi-Machine Observation Sync (#1570)
|
||||
New `claude-mem-sync` CLI with `push`, `pull`, `sync`, and `status` commands. Bidirectional sync of observations and session summaries between machines via SSH/SCP with deduplication by `(created_at, title)`. Tested syncing 3,400+ observations between two physical servers — a session on the remote machine used transferred memory to deliver a real feature PR.
|
||||
|
||||
#### Orphaned Message Drain (#1567)
|
||||
When `deleteSession()` aborts the SDK agent via SIGTERM, pending messages are now marked abandoned instead of remaining in `pending` status forever. Production evidence: 15 orphaned messages found before fix → 0 orphaned messages over 23 days after fix.
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
#### Installer Regression Fixed (v10.7.0 → v10.7.1)
|
||||
The install simplification in v10.7.0 over-applied scope — it replaced the entire `runInstallCommand` with just two `claude` CLI commands, gutting the interactive IDE multi-select, `--ide` flag, and all 13 IDE-specific setup dispatchers. v10.7.1 restores the full installer for all non-Claude-Code IDEs while keeping the native plugin delegation for Claude Code.
|
||||
|
||||
#### 3 Upstream Production Bugs (#1566)
|
||||
Found via analysis of 543K log lines over 17 days across two servers:
|
||||
- **summarize.ts**: Skip summary when transcript has no assistant message (was causing ~30 errors/day)
|
||||
- **ChromaSync.ts**: Fallback to `chroma_update_documents` when add fails with "IDs already exist"
|
||||
- **HealthMonitor.ts**: Replace HTTP-based port check with atomic socket bind (eliminates TOCTOU race on simultaneous session starts)
|
||||
|
||||
#### Other Fixes
|
||||
- Concept-type cleanup log downgraded from error to debug (reduces log noise)
|
||||
|
||||
### Breaking Change
|
||||
|
||||
**Strict Observer Response Contract** — The memory agent can no longer return prose-style skip responses like "Skipping — no substantive tool executions." `buildObservationPrompt` now requires `<observation>` XML blocks or an empty response. `ResponseProcessor` warns when non-XML content is received. This prevents silent data loss from the observer deciding on its own that tool output isn't worth recording.
|
||||
|
||||
### Community
|
||||
|
||||
Features in this release were contributed by **Alessandro Costa** ([@alessandropcostabr](https://github.com/alessandropcostabr)) — semantic injection, tier routing, multi-machine sync, orphan drain, and the 3-bug production fix. All PRs include production data from real multi-server deployments.
|
||||
|
||||
### Release History
|
||||
|
||||
This release consolidates v10.7.0 through v11.0.0, all shipped on April 4, 2026. For the full v10.x era (267 commits, 39 releases), see [v10.7.0](https://github.com/thedotmack/claude-mem/releases/tag/v10.7.0) and earlier.
|
||||
|
||||
## [10.7.2] - 2026-04-05
|
||||
|
||||
## Bug Fix
|
||||
|
||||
- **fix**: Downgrade concept-type cleanup log from error to debug (#1606) — reduces noise in logs by treating routine concept-type cleanup as debug-level rather than error-level logging.
|
||||
|
||||
## [10.7.1] - 2026-04-05
|
||||
|
||||
|
||||
@@ -138,6 +138,11 @@ Or install for Gemini CLI (auto-detects `~/.gemini`):
|
||||
```bash
|
||||
npx claude-mem install --ide gemini-cli
|
||||
```
|
||||
Or install for OpenCode:
|
||||
|
||||
```bash
|
||||
npx claude-mem install --ide opencode
|
||||
```
|
||||
|
||||
Or install from the plugin marketplace inside Claude Code:
|
||||
|
||||
@@ -300,6 +305,45 @@ Settings are managed in `~/.claude-mem/settings.json` (auto-created with default
|
||||
|
||||
See the **[Configuration Guide](https://docs.claude-mem.ai/configuration)** for all available settings and examples.
|
||||
|
||||
### Mode & Language Configuration
|
||||
|
||||
Claude-Mem supports multiple workflow modes and languages via the `CLAUDE_MEM_MODE` setting.
|
||||
|
||||
This option controls both:
|
||||
- The workflow behavior (e.g. code, chill, investigation)
|
||||
- The language used in generated observations
|
||||
|
||||
#### How to Configure
|
||||
|
||||
Edit your settings file at `~/.claude-mem/settings.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"CLAUDE_MEM_MODE": "code--zh"
|
||||
}
|
||||
```
|
||||
|
||||
Modes are defined in `plugin/modes/`. To see all available modes locally:
|
||||
|
||||
```bash
|
||||
ls ~/.claude/plugins/marketplaces/thedotmack/plugin/modes/
|
||||
```
|
||||
|
||||
#### Available Modes
|
||||
|
||||
| Mode | Description |
|
||||
|------------|-------------------------|
|
||||
| `code` | Default English mode |
|
||||
| `code--zh` | Simplified Chinese mode |
|
||||
| `code--ja` | Japanese mode |
|
||||
|
||||
Language-specific modes follow the pattern `code--[lang]` where `[lang]` is the ISO 639-1 language code (e.g., `zh` for Chinese, `ja` for Japanese, `es` for Spanish).
|
||||
|
||||
> Note: `code--zh` (Simplified Chinese) is already built-in — no additional installation or plugin update is required.
|
||||
|
||||
#### After Changing Mode
|
||||
|
||||
Restart Claude Code to apply the new mode configuration.
|
||||
---
|
||||
|
||||
## Development
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
[test]
|
||||
# Force each test file into its own worker process.
|
||||
# Prevents mock.module() calls (which are permanent within a worker)
|
||||
# from leaking across test files in parallel runs.
|
||||
# Note: smol=true increases test startup time by spawning one Bun process per file.
|
||||
# See: https://github.com/thedotmack/claude-mem/issues/1299
|
||||
smol = true
|
||||
@@ -23,14 +23,14 @@ Claude-mem uses **two distinct session IDs** to track conversations and memory:
|
||||
↓
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ 2. SDKAgent starts, checks hasRealMemorySessionId │
|
||||
│ const hasReal = memorySessionId !== null │
|
||||
│ const hasReal = !!memorySessionId │
|
||||
│ → FALSE (it's NULL) │
|
||||
│ → Resume NOT used (fresh SDK session) │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
↓
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ 3. First SDK message arrives with session_id │
|
||||
│ updateMemorySessionId(sessionDbId, "sdk-gen-abc123") │
|
||||
│ ensureMemorySessionIdRegistered(sessionDbId, "sdk-gen-abc123") │
|
||||
│ │
|
||||
│ Database state: │
|
||||
│ ├─ content_session_id: "user-session-123" │
|
||||
@@ -38,45 +38,43 @@ Claude-mem uses **two distinct session IDs** to track conversations and memory:
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
↓
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ 4. Subsequent prompts use resume │
|
||||
│ const hasReal = memorySessionId !== null │
|
||||
│ → TRUE (it's not NULL) │
|
||||
│ 4. Subsequent prompts may use resume │
|
||||
│ const shouldResume = │
|
||||
│ !!memorySessionId && lastPromptNumber > 1 && !forceInit│
|
||||
│ → TRUE only for continuation prompts in the same runtime │
|
||||
│ → Resume parameter: { resume: "sdk-gen-abc123" } │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
### Observation Storage
|
||||
|
||||
**CRITICAL**: Observations are stored with `contentSessionId`, NOT the captured SDK `memorySessionId`.
|
||||
**CRITICAL**: Observations are stored with the real `memorySessionId`, NOT `contentSessionId`.
|
||||
|
||||
```typescript
|
||||
// SDKAgent.ts line 332-333
|
||||
this.dbManager.getSessionStore().storeObservation(
|
||||
session.contentSessionId, // ← contentSessionId, not memorySessionId!
|
||||
session.project,
|
||||
obs,
|
||||
// ...
|
||||
);
|
||||
// SessionStore.ts
|
||||
storeObservation(memorySessionId, project, observation, ...);
|
||||
```
|
||||
|
||||
Even though the parameter is named `memorySessionId`, it receives `contentSessionId`. This means:
|
||||
This means:
|
||||
|
||||
- Database column: `observations.memory_session_id`
|
||||
- Stored value: `contentSessionId` (the user's session ID)
|
||||
- Stored value: the captured or synthesized `memorySessionId`
|
||||
- Foreign key: References `sdk_sessions.memory_session_id`
|
||||
|
||||
The observations are linked to the session via `contentSessionId`, which remains constant throughout the session lifecycle.
|
||||
Observation storage is blocked until a real `memorySessionId` is registered in `sdk_sessions`.
|
||||
This is why `SDKAgent` persists the SDK-returned `session_id` immediately through
|
||||
`ensureMemorySessionIdRegistered(...)` before any observation insert can succeed.
|
||||
|
||||
## Key Invariants
|
||||
|
||||
### 1. NULL-Based Detection
|
||||
|
||||
```typescript
|
||||
const hasRealMemorySessionId = session.memorySessionId !== null;
|
||||
const hasRealMemorySessionId = !!session.memorySessionId;
|
||||
```
|
||||
|
||||
- When `memorySessionId === null` → Not yet captured
|
||||
- When `memorySessionId !== null` → Real SDK session captured
|
||||
- When `memorySessionId` is falsy → Not yet captured
|
||||
- When `memorySessionId` is truthy → Real SDK session captured
|
||||
|
||||
### 2. Resume Safety
|
||||
|
||||
@@ -86,12 +84,20 @@ const hasRealMemorySessionId = session.memorySessionId !== null;
|
||||
// ❌ FORBIDDEN - Would resume user's session instead of memory session!
|
||||
query({ resume: contentSessionId })
|
||||
|
||||
// ✅ CORRECT - Only resume when we have real memory session ID
|
||||
// ✅ CORRECT - Only resume for a continuation prompt in a valid runtime
|
||||
query({
|
||||
...(hasRealMemorySessionId && { resume: memorySessionId })
|
||||
...(
|
||||
!!memorySessionId &&
|
||||
lastPromptNumber > 1 &&
|
||||
!forceInit &&
|
||||
{ resume: memorySessionId }
|
||||
)
|
||||
})
|
||||
```
|
||||
|
||||
`memorySessionId` is necessary but not sufficient.
|
||||
Worker restart and crash-recovery paths may still carry a persisted ID while forcing a fresh INIT run.
|
||||
|
||||
### 3. Session Isolation
|
||||
|
||||
- Each `contentSessionId` maps to exactly one database session
|
||||
@@ -103,7 +109,8 @@ query({
|
||||
- Observations reference `sdk_sessions.memory_session_id`
|
||||
- Initially, `sdk_sessions.memory_session_id` is NULL (no observations can be stored yet)
|
||||
- When SDK session ID is captured, `sdk_sessions.memory_session_id` is set to the real value
|
||||
- Observations are stored using `contentSessionId` and remain retrievable via `contentSessionId`
|
||||
- Observations are stored using that real `memory_session_id`
|
||||
- Queries can still find the session from `content_session_id`, but observation rows themselves stay keyed by `memory_session_id`
|
||||
|
||||
## Testing Strategy
|
||||
|
||||
@@ -116,8 +123,8 @@ The test suite validates all critical invariants:
|
||||
### Test Categories
|
||||
|
||||
1. **NULL-Based Detection** - Validates `hasRealMemorySessionId` logic
|
||||
2. **Observation Storage** - Confirms observations use `contentSessionId`
|
||||
3. **Resume Safety** - Prevents `contentSessionId` from being used for resume
|
||||
2. **Observation Storage** - Confirms observations use real `memorySessionId` values after registration
|
||||
3. **Resume Safety** - Prevents `contentSessionId` and stale INIT sessions from being used for resume
|
||||
4. **Cross-Contamination Prevention** - Ensures session isolation
|
||||
5. **Foreign Key Integrity** - Validates cascade behavior
|
||||
6. **Session Lifecycle** - Tests create → capture → resume flow
|
||||
@@ -141,14 +148,14 @@ bun test --verbose
|
||||
### ❌ Using memorySessionId for observations
|
||||
|
||||
```typescript
|
||||
// WRONG - Don't use the captured SDK session ID
|
||||
storeObservation(session.memorySessionId, ...)
|
||||
// WRONG - Don't store observations before memorySessionId is available
|
||||
storeObservation(session.contentSessionId, ...)
|
||||
```
|
||||
|
||||
### ❌ Resuming without checking for NULL
|
||||
|
||||
```typescript
|
||||
// WRONG - memorySessionId could be NULL!
|
||||
// WRONG - memorySessionId alone is not enough
|
||||
if (session.memorySessionId) {
|
||||
query({ resume: session.memorySessionId })
|
||||
}
|
||||
@@ -166,14 +173,14 @@ const resumeId = session.memorySessionId
|
||||
### ✅ Storing observations
|
||||
|
||||
```typescript
|
||||
// Always use contentSessionId
|
||||
storeObservation(session.contentSessionId, project, obs, ...)
|
||||
// Only store after a real memorySessionId has been captured or synthesized
|
||||
storeObservation(session.memorySessionId, project, obs, ...)
|
||||
```
|
||||
|
||||
### ✅ Checking for real memory session ID
|
||||
|
||||
```typescript
|
||||
const hasRealMemorySessionId = session.memorySessionId !== null;
|
||||
const hasRealMemorySessionId = !!session.memorySessionId;
|
||||
```
|
||||
|
||||
### ✅ Using resume parameter
|
||||
@@ -182,7 +189,12 @@ const hasRealMemorySessionId = session.memorySessionId !== null;
|
||||
query({
|
||||
prompt: messageGenerator,
|
||||
options: {
|
||||
...(hasRealMemorySessionId && { resume: session.memorySessionId }),
|
||||
...(
|
||||
hasRealMemorySessionId &&
|
||||
session.lastPromptNumber > 1 &&
|
||||
!session.forceInit &&
|
||||
{ resume: session.memorySessionId }
|
||||
),
|
||||
// ... other options
|
||||
}
|
||||
})
|
||||
@@ -234,6 +246,6 @@ WHERE s.content_session_id = 'your-session-id';
|
||||
## References
|
||||
|
||||
- **Implementation**: `src/services/worker/SDKAgent.ts` (lines 72-94)
|
||||
- **Database Schema**: `src/services/sqlite/SessionStore.ts` (line 95-104)
|
||||
- **Session Store**: `src/services/sqlite/SessionStore.ts`
|
||||
- **Tests**: `tests/session_id_usage_validation.test.ts`
|
||||
- **Related Tests**: `tests/session_id_refactor.test.ts`
|
||||
|
||||
@@ -32,7 +32,7 @@ For simple single-turn queries where you don't need to maintain a session, use `
|
||||
import { unstable_v2_prompt } from '@anthropic-ai/claude-agent-sdk'
|
||||
|
||||
const result = await unstable_v2_prompt('What is 2 + 2?', {
|
||||
model: 'claude-sonnet-4-5-20250929'
|
||||
model: 'claude-sonnet-4-6-20250929'
|
||||
})
|
||||
console.log(result.result)
|
||||
```
|
||||
@@ -45,7 +45,7 @@ import { query } from '@anthropic-ai/claude-agent-sdk'
|
||||
|
||||
const q = query({
|
||||
prompt: 'What is 2 + 2?',
|
||||
options: { model: 'claude-sonnet-4-5-20250929' }
|
||||
options: { model: 'claude-sonnet-4-6-20250929' }
|
||||
})
|
||||
|
||||
for await (const msg of q) {
|
||||
@@ -71,7 +71,7 @@ The example below creates a session, sends "Hello!" to Claude, and prints the te
|
||||
import { unstable_v2_createSession } from '@anthropic-ai/claude-agent-sdk'
|
||||
|
||||
await using session = unstable_v2_createSession({
|
||||
model: 'claude-sonnet-4-5-20250929'
|
||||
model: 'claude-sonnet-4-6-20250929'
|
||||
})
|
||||
|
||||
await session.send('Hello!')
|
||||
@@ -97,7 +97,7 @@ import { query } from '@anthropic-ai/claude-agent-sdk'
|
||||
|
||||
const q = query({
|
||||
prompt: 'Hello!',
|
||||
options: { model: 'claude-sonnet-4-5-20250929' }
|
||||
options: { model: 'claude-sonnet-4-6-20250929' }
|
||||
})
|
||||
|
||||
for await (const msg of q) {
|
||||
@@ -123,7 +123,7 @@ This example asks a math question, then asks a follow-up that references the pre
|
||||
import { unstable_v2_createSession } from '@anthropic-ai/claude-agent-sdk'
|
||||
|
||||
await using session = unstable_v2_createSession({
|
||||
model: 'claude-sonnet-4-5-20250929'
|
||||
model: 'claude-sonnet-4-6-20250929'
|
||||
})
|
||||
|
||||
// Turn 1
|
||||
@@ -177,7 +177,7 @@ async function* createInputStream() {
|
||||
|
||||
const q = query({
|
||||
prompt: createInputStream(),
|
||||
options: { model: 'claude-sonnet-4-5-20250929' }
|
||||
options: { model: 'claude-sonnet-4-6-20250929' }
|
||||
})
|
||||
|
||||
for await (const msg of q) {
|
||||
@@ -217,7 +217,7 @@ function getAssistantText(msg: SDKMessage): string | null {
|
||||
|
||||
// Create initial session and have a conversation
|
||||
const session = unstable_v2_createSession({
|
||||
model: 'claude-sonnet-4-5-20250929'
|
||||
model: 'claude-sonnet-4-6-20250929'
|
||||
})
|
||||
|
||||
await session.send('Remember this number: 42')
|
||||
@@ -235,7 +235,7 @@ session.close()
|
||||
|
||||
// Later: resume the session using the stored ID
|
||||
await using resumedSession = unstable_v2_resumeSession(sessionId!, {
|
||||
model: 'claude-sonnet-4-5-20250929'
|
||||
model: 'claude-sonnet-4-6-20250929'
|
||||
})
|
||||
|
||||
await resumedSession.send('What number did I ask you to remember?')
|
||||
@@ -254,7 +254,7 @@ import { query } from '@anthropic-ai/claude-agent-sdk'
|
||||
// Create initial session
|
||||
const initialQuery = query({
|
||||
prompt: 'Remember this number: 42',
|
||||
options: { model: 'claude-sonnet-4-5-20250929' }
|
||||
options: { model: 'claude-sonnet-4-6-20250929' }
|
||||
})
|
||||
|
||||
// Get session ID from any message
|
||||
@@ -276,7 +276,7 @@ console.log('Session ID:', sessionId)
|
||||
const resumedQuery = query({
|
||||
prompt: 'What number did I ask you to remember?',
|
||||
options: {
|
||||
model: 'claude-sonnet-4-5-20250929',
|
||||
model: 'claude-sonnet-4-6-20250929',
|
||||
resume: sessionId
|
||||
}
|
||||
})
|
||||
@@ -304,7 +304,7 @@ Sessions can be closed manually or automatically using [`await using`](https://w
|
||||
import { unstable_v2_createSession } from '@anthropic-ai/claude-agent-sdk'
|
||||
|
||||
await using session = unstable_v2_createSession({
|
||||
model: 'claude-sonnet-4-5-20250929'
|
||||
model: 'claude-sonnet-4-6-20250929'
|
||||
})
|
||||
// Session closes automatically when the block exits
|
||||
```
|
||||
@@ -315,7 +315,7 @@ await using session = unstable_v2_createSession({
|
||||
import { unstable_v2_createSession } from '@anthropic-ai/claude-agent-sdk'
|
||||
|
||||
const session = unstable_v2_createSession({
|
||||
model: 'claude-sonnet-4-5-20250929'
|
||||
model: 'claude-sonnet-4-6-20250929'
|
||||
})
|
||||
// ... use the session ...
|
||||
session.close()
|
||||
|
||||
@@ -860,7 +860,7 @@ async startSession(session: ActiveSession, worker?: any) {
|
||||
const queryResult = query({
|
||||
prompt: messageGenerator,
|
||||
options: {
|
||||
model: 'claude-sonnet-4-5',
|
||||
model: 'claude-sonnet-4-6',
|
||||
disallowedTools: ['Bash', 'Read', 'Write', ...], // Observer-only
|
||||
abortController: session.abortController
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
"usage/openrouter-provider",
|
||||
"usage/gemini-provider",
|
||||
"usage/search-tools",
|
||||
"usage/knowledge-agents",
|
||||
"usage/claude-desktop",
|
||||
"usage/private-tags",
|
||||
"usage/export-import",
|
||||
@@ -70,6 +71,7 @@
|
||||
"pages": [
|
||||
"context-engineering",
|
||||
"progressive-disclosure",
|
||||
"file-read-gate",
|
||||
"smart-explore-benchmark"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
---
|
||||
title: "File Read Gate"
|
||||
description: "How claude-mem intercepts file reads to save tokens using observation history"
|
||||
---
|
||||
|
||||
# File Read Gate
|
||||
|
||||
## What It Is
|
||||
|
||||
The File Read Gate is a **PreToolUse hook** that intercepts Claude's `Read` tool calls. When Claude tries to read a file that has prior observations in the database, the gate blocks the read and instead shows a compact timeline of past work on that file. Claude then decides the cheapest path to get the context it needs.
|
||||
|
||||
This is a concrete implementation of [progressive disclosure](/progressive-disclosure) -- show what exists first, let the agent decide what to fetch.
|
||||
|
||||
---
|
||||
|
||||
## How It Works
|
||||
|
||||
```
|
||||
Claude calls Read("src/services/worker-service.ts")
|
||||
↓
|
||||
PreToolUse hook fires
|
||||
↓
|
||||
File size < 1,500 bytes? ──→ Allow read (timeline costs more than file)
|
||||
↓ No
|
||||
Project excluded? ──→ Allow read
|
||||
↓ No
|
||||
Query worker: GET /api/observations/by-file
|
||||
↓
|
||||
No observations found? ──→ Allow read
|
||||
↓ Has observations
|
||||
Deduplicate (1 per session)
|
||||
Rank by specificity
|
||||
Limit to 15
|
||||
↓
|
||||
DENY read with timeline
|
||||
```
|
||||
|
||||
When the gate fires, Claude sees a message like this:
|
||||
|
||||
```
|
||||
Current: 2026-04-07 3:25pm PDT
|
||||
Read blocked: This file has prior observations. Choose the cheapest path:
|
||||
- Already know enough? The timeline below may be all you need (semantic priming).
|
||||
- Need details? get_observations([IDs]) -- ~300 tokens each.
|
||||
- Need current code? smart_outline("path") for structure (~1-2k tokens),
|
||||
smart_unfold("path", "<symbol>") for a specific function (~400-2k tokens).
|
||||
- Need to edit? Use smart tools for line numbers, then sed via Bash.
|
||||
|
||||
### Apr 5, 2026
|
||||
42301 2:15pm Fixed database connection pooling
|
||||
42298 1:50pm Refactored worker startup sequence
|
||||
|
||||
### Mar 28, 2026
|
||||
41890 4:30pm Added health check endpoint
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## The Decision Tree
|
||||
|
||||
Claude has four options after seeing the timeline, ordered from cheapest to most expensive:
|
||||
|
||||
| Option | Token Cost | When to Use |
|
||||
|--------|-----------|-------------|
|
||||
| **Semantic priming** | 0 extra | Timeline titles tell Claude enough to proceed |
|
||||
| **get_observations([IDs])** | ~300 each | Need specific details from past work |
|
||||
| **smart_outline / smart_unfold** | ~1-2k | Need current code structure or a specific function |
|
||||
| **Full file read** | 5k-50k | File has changed significantly since observations |
|
||||
|
||||
In practice, most file reads resolve at the semantic priming or get_observations level, saving thousands of tokens per interaction.
|
||||
|
||||
---
|
||||
|
||||
## Current Date/Time for Temporal Reasoning
|
||||
|
||||
The timeline includes the current date and time as its first line:
|
||||
|
||||
```
|
||||
Current: 2026-04-07 3:25pm PDT
|
||||
```
|
||||
|
||||
This lets Claude reason about how recent the observations are relative to now. For example:
|
||||
|
||||
- **Observations from today** -- likely still accurate, semantic priming is safe
|
||||
- **Observations from last week** -- probably accurate, get_observations for details
|
||||
- **Observations from months ago** -- file may have changed, consider smart_outline or full read
|
||||
|
||||
The timestamp format matches the session start context header (`YYYY-MM-DD time timezone`), so Claude sees consistent temporal markers throughout its session.
|
||||
|
||||
---
|
||||
|
||||
## Token Economics
|
||||
|
||||
A typical source file costs **5,000-50,000 tokens** to read in full. The File Read Gate replaces that with:
|
||||
|
||||
| Component | Tokens |
|
||||
|-----------|--------|
|
||||
| Timeline header + instructions | ~120 |
|
||||
| 15 observation entries | ~250 |
|
||||
| **Total timeline** | **~370** |
|
||||
|
||||
If Claude needs more detail, it fetches individual observations at ~300 tokens each. Even fetching 3 observations totals ~1,270 tokens -- still a **75-97% savings** over reading the full file.
|
||||
|
||||
### Real-World Example
|
||||
|
||||
Without the gate (reading `worker-service.ts`):
|
||||
```
|
||||
Read: 18,000 tokens
|
||||
```
|
||||
|
||||
With the gate:
|
||||
```
|
||||
Timeline: 370 tokens
|
||||
+ 2 observations: 600 tokens
|
||||
Total: 970 tokens (95% savings)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Specificity Ranking
|
||||
|
||||
Not all observations about a file are equally relevant. The gate scores each observation by how specifically it relates to the target file:
|
||||
|
||||
| Signal | Score Bonus |
|
||||
|--------|------------|
|
||||
| File was **modified** (not just read) | +2 |
|
||||
| Observation covers **3 or fewer** total files | +2 |
|
||||
| Observation covers **4-8** total files | +1 |
|
||||
| Observation covers **9+** files (survey-like) | +0 |
|
||||
|
||||
Higher-scoring observations appear first in the timeline. An observation where the file was the primary modification target ranks above one where the file was incidentally read alongside 20 others.
|
||||
|
||||
---
|
||||
|
||||
## Configuration
|
||||
|
||||
### Small File Bypass
|
||||
|
||||
Files smaller than **1,500 bytes** always pass through the gate without interception. At that size, the timeline (~370 tokens) would cost more than reading the file directly. This threshold is hardcoded in `src/cli/handlers/file-context.ts`.
|
||||
|
||||
### Project Exclusions
|
||||
|
||||
Projects matching patterns in `CLAUDE_MEM_EXCLUDED_PROJECTS` skip the gate entirely. Configure this in `~/.claude-mem/settings.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"CLAUDE_MEM_EXCLUDED_PROJECTS": "/tmp/*,/scratch/*"
|
||||
}
|
||||
```
|
||||
|
||||
### How to Disable the Gate
|
||||
|
||||
The File Read Gate is implemented as a PreToolUse hook on the `Read` tool matcher. To disable it, remove the `Read` matcher entry from the hooks configuration:
|
||||
|
||||
1. Open your Claude Code settings:
|
||||
```
|
||||
~/.claude/settings.json
|
||||
```
|
||||
|
||||
2. Find the claude-mem hooks section under `hooks.PreToolUse` and remove the entry with the `Read` matcher.
|
||||
|
||||
Alternatively, if you want to keep the gate installed but bypass it for a specific read, Claude can ask you to allow the read -- the gate's deny decision is presented to the user, who can override it.
|
||||
|
||||
<Note>
|
||||
Disabling the gate means Claude will read full files every time, which increases token usage but ensures it always sees the latest code. This is a reasonable choice for small projects or when observations are sparse.
|
||||
</Note>
|
||||
|
||||
---
|
||||
|
||||
## How It Fits Together
|
||||
|
||||
The File Read Gate is one piece of claude-mem's layered context strategy:
|
||||
|
||||
1. **Session Start**: Inject timeline of recent observations (layer 1 -- metadata)
|
||||
2. **File Read Gate**: Intercept reads with observation history (layer 1 -- metadata)
|
||||
3. **get_observations**: Fetch specific observation details on demand (layer 2 -- details)
|
||||
4. **smart_outline / smart_unfold**: Read current code structure efficiently (layer 3 -- source)
|
||||
5. **Full file read**: Last resort when everything else is insufficient
|
||||
|
||||
Each layer is progressively more expensive. The gate ensures Claude starts at the cheapest layer and escalates only when needed.
|
||||
@@ -33,6 +33,7 @@ Restart Claude Code. Context from previous sessions will automatically appear in
|
||||
- 🌐 **Multilingual Modes** - Supports 28 languages (Spanish, Chinese, French, Japanese, etc.)
|
||||
- 🎭 **Mode System** - Switch between workflows (Code, Email Investigation, Chill)
|
||||
- 🔍 **MCP Search Tools** - Query your project history with natural language
|
||||
- 🧠 **Knowledge Agents** - Build queryable "brains" from your observation history
|
||||
- 🌐 **Web Viewer UI** - Real-time memory stream visualization at http://localhost:37777
|
||||
- 🔒 **Privacy Control** - Use `<private>` tags to exclude sensitive content from storage
|
||||
- ⚙️ **Context Configuration** - Fine-grained control over what context gets injected
|
||||
@@ -115,4 +116,7 @@ See [Architecture Overview](architecture/overview) for details.
|
||||
<Card title="Search Tools" icon="magnifying-glass" href="/usage/search-tools">
|
||||
Query your project history
|
||||
</Card>
|
||||
<Card title="Knowledge Agents" icon="brain" href="/usage/knowledge-agents">
|
||||
Build queryable corpora from your history
|
||||
</Card>
|
||||
</CardGroup>
|
||||
|
||||
@@ -46,7 +46,7 @@ GET /api/context/recent?project=my-project&limit=3
|
||||
### Environment Variables
|
||||
|
||||
```bash
|
||||
CLAUDE_MEM_MODEL=claude-sonnet-4-5 # Model for observations/summaries
|
||||
CLAUDE_MEM_MODEL=claude-sonnet-4-6 # Model for observations/summaries
|
||||
CLAUDE_MEM_CONTEXT_OBSERVATIONS=50 # Observations injected at SessionStart
|
||||
CLAUDE_MEM_WORKER_PORT=37777 # Worker service port
|
||||
CLAUDE_MEM_PYTHON_VERSION=3.13 # Python version for chroma-mcp
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
---
|
||||
title: "Knowledge Agents"
|
||||
description: "Build queryable AI brains from your observation history"
|
||||
---
|
||||
|
||||
# Knowledge Agents
|
||||
|
||||
Knowledge agents let you compile a slice of your claude-mem observation history into a **queryable "brain"** that answers questions conversationally. Instead of getting raw search results back, you get synthesized, grounded answers drawn from your actual project history -- decisions, discoveries, bugfixes, and features.
|
||||
|
||||
## Quick Start
|
||||
|
||||
Three ways to use knowledge agents, from simplest to most powerful.
|
||||
|
||||
### 1. Create a Knowledge Agent
|
||||
|
||||
Use the `/knowledge-agent` skill or the MCP tools directly:
|
||||
|
||||
```
|
||||
build_corpus name="hooks-expertise" query="hooks architecture" project="claude-mem" limit=200
|
||||
```
|
||||
|
||||
This searches your observation history, collects matching records, and saves them as a corpus file. Then prime it — this loads the corpus into a Claude session's context window:
|
||||
|
||||
```
|
||||
prime_corpus name="hooks-expertise"
|
||||
```
|
||||
|
||||
Your knowledge agent is ready. The returned `session_id` **is** the agent — a Claude session with your history baked in.
|
||||
|
||||
### 2. Ask a Single Question
|
||||
|
||||
Once primed, ask any question and get a grounded answer:
|
||||
|
||||
```
|
||||
query_corpus name="hooks-expertise" question="What are the 5 lifecycle hooks and when does each fire?"
|
||||
```
|
||||
|
||||
The agent answers grounded in its corpus — responses are drawn from your actual project history, reducing hallucination and guessing. Each follow-up question builds on the prior conversation:
|
||||
|
||||
```
|
||||
query_corpus name="hooks-expertise" question="Which hook handles context injection?"
|
||||
```
|
||||
|
||||
### 3. Start a Fresh Conversation
|
||||
|
||||
If the conversation drifts, or you want to ask an unrelated question against the same corpus, reprime to start clean:
|
||||
|
||||
```
|
||||
reprime_corpus name="hooks-expertise"
|
||||
```
|
||||
|
||||
This creates a **new session** with the full corpus reloaded — like opening a fresh chat with the same "brain." All prior Q&A context is cleared, but the corpus knowledge remains. Use this when:
|
||||
|
||||
- The conversation went off-track and you want a clean slate
|
||||
- You're switching topics within the same corpus
|
||||
- You want to ask a question without prior answers biasing the response
|
||||
|
||||
### Keeping It Current
|
||||
|
||||
When new observations are added to your project, rebuild the corpus to pull in the latest, then reprime:
|
||||
|
||||
```
|
||||
rebuild_corpus name="hooks-expertise"
|
||||
reprime_corpus name="hooks-expertise"
|
||||
```
|
||||
|
||||
Rebuild re-runs the original search filters. Reprime loads the refreshed data into a new session.
|
||||
|
||||
---
|
||||
|
||||
## The Workflow: Build, Prime, Query
|
||||
|
||||
```
|
||||
BUILD ──> PRIME ──> QUERY
|
||||
```
|
||||
|
||||
### 1. Build a Corpus
|
||||
|
||||
A corpus is a filtered collection of observations saved as a JSON file. Use search filters to select exactly the slice of history you want.
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:37777/api/corpus \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"name": "hooks-expertise",
|
||||
"query": "hooks architecture",
|
||||
"project": "claude-mem",
|
||||
"types": ["decision", "discovery"],
|
||||
"limit": 200
|
||||
}'
|
||||
```
|
||||
|
||||
Under the hood, `CorpusBuilder` searches your observations, hydrates full records, parses structured fields (facts, concepts, files), calculates stats, and writes everything to `~/.claude-mem/corpora/hooks-expertise.corpus.json`.
|
||||
|
||||
### 2. Prime the Knowledge Agent
|
||||
|
||||
Priming loads the entire corpus into a Claude session's context window.
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:37777/api/corpus/hooks-expertise/prime
|
||||
```
|
||||
|
||||
The agent renders all observations into full-detail text and feeds them to the Claude Agent SDK. Claude reads the corpus and acknowledges the themes. The returned `session_id` **is** the knowledge agent -- a Claude session with your history baked in.
|
||||
|
||||
### 3. Query
|
||||
|
||||
Resume the primed session and ask questions.
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:37777/api/corpus/hooks-expertise/query \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{ "question": "What are the 5 lifecycle hooks?" }'
|
||||
```
|
||||
|
||||
Each follow-up question adds to the conversation naturally. If the session expires, the agent auto-reprimes from the corpus file and retries.
|
||||
|
||||
---
|
||||
|
||||
## Filter Options
|
||||
|
||||
Use these parameters when building a corpus to control which observations are included:
|
||||
|
||||
| Parameter | Type | Description |
|
||||
|-----------|------|-------------|
|
||||
| `name` | string | Name for the corpus (used in all subsequent API calls) |
|
||||
| `project` | string | Filter by project name |
|
||||
| `types` | string[] | Filter by observation type (bugfix, feature, decision, discovery, refactor, change) |
|
||||
| `concepts` | string[] | Filter by tagged concepts |
|
||||
| `files` | string[] | Filter by files read or modified |
|
||||
| `query` | string | Full-text search query |
|
||||
| `dateStart` | string | Start date filter (YYYY-MM-DD) |
|
||||
| `dateEnd` | string | End date filter (YYYY-MM-DD) |
|
||||
| `limit` | number | Maximum observations to include |
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
MCP Tools HTTP API
|
||||
(mcp-server.ts) (worker on :37777)
|
||||
| |
|
||||
build_corpus ──┤ |
|
||||
list_corpora ──┤ |
|
||||
prime_corpus ──┤── callWorkerAPIPost() ──>|
|
||||
query_corpus ──┤ |
|
||||
rebuild_corpus ──┤ |
|
||||
reprime_corpus ──┘ |
|
||||
v
|
||||
CorpusRoutes
|
||||
(8 endpoints)
|
||||
/ | \
|
||||
CorpusBuilder | KnowledgeAgent
|
||||
| | |
|
||||
SearchOrchestrator | Agent SDK V1
|
||||
SessionStore | query() + resume
|
||||
|
|
||||
CorpusStore
|
||||
(~/.claude-mem/corpora/)
|
||||
```
|
||||
|
||||
**Key insight:** The Agent SDK's `resume` option lets you prime a session once (upload the corpus), save the `session_id`, and resume it for every future question. The corpus stays in context permanently -- no re-uploading, no prompt caching tricks. The 1M token context window makes this viable: 2,000 observations at ~300 tokens each fits comfortably.
|
||||
|
||||
---
|
||||
|
||||
## When to Use `/knowledge-agent` vs `/mem-search`
|
||||
|
||||
| | `/mem-search` | `/knowledge-agent` |
|
||||
|---|---|---|
|
||||
| **Returns** | Raw observation records | Synthesized conversational answers |
|
||||
| **Best for** | Finding specific observations, IDs, timelines | Asking questions about patterns, decisions, architecture |
|
||||
| **Token model** | Pay-per-query (3-layer progressive disclosure) | Pay-once at prime time, then cheap follow-ups |
|
||||
| **Interaction** | Search, filter, fetch | Ask questions in natural language |
|
||||
| **Data freshness** | Always current (queries database live) | Snapshot at build time (rebuild to refresh) |
|
||||
| **Setup** | None -- works immediately | Build + prime required before first query |
|
||||
|
||||
**Rule of thumb:** Use `/mem-search` when you need to find something specific. Use `/knowledge-agent` when you want to understand something broadly.
|
||||
|
||||
---
|
||||
|
||||
## API Reference
|
||||
|
||||
| Method | Path | Description |
|
||||
|--------|------|-------------|
|
||||
| POST | `/api/corpus` | Build a new corpus from filters |
|
||||
| GET | `/api/corpus` | List all corpora with stats |
|
||||
| GET | `/api/corpus/:name` | Get corpus metadata |
|
||||
| DELETE | `/api/corpus/:name` | Delete a corpus |
|
||||
| POST | `/api/corpus/:name/rebuild` | Rebuild from stored filters |
|
||||
| POST | `/api/corpus/:name/prime` | Create AI session with corpus loaded |
|
||||
| POST | `/api/corpus/:name/query` | Ask the knowledge agent a question |
|
||||
| POST | `/api/corpus/:name/reprime` | Fresh session (wipe prior Q&A) |
|
||||
|
||||
---
|
||||
|
||||
## Edge Cases
|
||||
|
||||
- **Session expiry**: If `resume` fails, the agent auto-reprimes from the corpus file and retries
|
||||
- **SDK process exit**: If the Claude process exits after yielding all messages, the agent treats it as success when the session_id or answer was already captured
|
||||
- **Empty corpus**: A corpus with 0 observations is valid (just empty)
|
||||
- **Model from settings**: Reads `CLAUDE_MEM_MODEL` from user settings -- no hardcoded model IDs
|
||||
|
||||
## Next Steps
|
||||
|
||||
- [Memory Search](/usage/search-tools) - The 3-layer search workflow for finding specific observations
|
||||
- [Progressive Disclosure](/progressive-disclosure) - Philosophy behind token-efficient retrieval
|
||||
- [Architecture Overview](/architecture/overview) - System components
|
||||
+58
-9
@@ -80,17 +80,18 @@ setup_tty() {
|
||||
if [[ -t 0 ]]; then
|
||||
# stdin IS a terminal — use it directly
|
||||
TTY_FD=0
|
||||
elif [[ -e /dev/tty ]]; then
|
||||
# stdin is piped (curl | bash) but /dev/tty is available
|
||||
elif [[ "$NON_INTERACTIVE" == "true" ]]; then
|
||||
# In non-interactive mode, do not require /dev/tty
|
||||
TTY_FD=0
|
||||
elif [[ -r /dev/tty ]]; then
|
||||
# stdin is piped (curl | bash) but /dev/tty is available and readable
|
||||
exec 3</dev/tty
|
||||
TTY_FD=3
|
||||
else
|
||||
# No terminal available at all
|
||||
if [[ "$NON_INTERACTIVE" != "true" ]]; then
|
||||
echo "Error: No terminal available for interactive prompts." >&2
|
||||
echo "Use --non-interactive or run directly: bash install.sh" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "Error: No terminal available for interactive prompts." >&2
|
||||
echo "Use --non-interactive or run directly: bash install.sh" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -787,11 +788,16 @@ install_plugin() {
|
||||
const configPath = process.env.INSTALLER_CONFIG_FILE;
|
||||
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
||||
const entry = config?.plugins?.entries?.['claude-mem'];
|
||||
if (entry || config?.plugins?.slots?.memory === 'claude-mem') {
|
||||
const allowHasClaudeMem = Array.isArray(config?.plugins?.allow) && config.plugins.allow.includes('claude-mem');
|
||||
if (entry || config?.plugins?.slots?.memory === 'claude-mem' || allowHasClaudeMem) {
|
||||
// Save the config block so we can restore it after install
|
||||
process.stdout.write(JSON.stringify(entry?.config || {}));
|
||||
// Remove the stale entry so OpenClaw CLI can run
|
||||
if (entry) delete config.plugins.entries['claude-mem'];
|
||||
// Also remove stale allowlist reference — this alone can block ALL CLI commands
|
||||
if (Array.isArray(config?.plugins?.allow)) {
|
||||
config.plugins.allow = config.plugins.allow.filter((x) => x !== 'claude-mem');
|
||||
}
|
||||
// Also remove the slot reference — if the slot points to a plugin
|
||||
// that isn't in entries, OpenClaw's config validator rejects ALL commands
|
||||
if (config?.plugins?.slots?.memory === 'claude-mem') {
|
||||
@@ -818,6 +824,49 @@ install_plugin() {
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ensure claude-mem is present in plugins.allow after successful install+enable.
|
||||
# Some OpenClaw environments require explicit allowlisting for local plugins.
|
||||
# This write is guaranteed: if config doesn't exist, configure_memory_slot() will create it.
|
||||
if [[ -f "$oc_config" ]]; then
|
||||
if ! INSTALLER_CONFIG_FILE="$oc_config" node -e "
|
||||
const fs = require('fs');
|
||||
const configPath = process.env.INSTALLER_CONFIG_FILE;
|
||||
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
||||
if (!config.plugins) config.plugins = {};
|
||||
if (!Array.isArray(config.plugins.allow)) config.plugins.allow = [];
|
||||
if (!config.plugins.allow.includes('claude-mem')) {
|
||||
config.plugins.allow.push('claude-mem');
|
||||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
||||
console.log('Added claude-mem to plugins.allow');
|
||||
} else {
|
||||
console.log('claude-mem already in plugins.allow');
|
||||
}
|
||||
" 2>&1; then
|
||||
warn "Failed to write plugins.allow — claude-mem may need manual allowlisting"
|
||||
fi
|
||||
else
|
||||
# Config doesn't exist yet; configure_memory_slot() will create it with plugins.allow
|
||||
# We'll add claude-mem to the allowlist in a follow-up step after config is materialized
|
||||
info "OpenClaw config not yet materialized; will ensure allowlist in post-install"
|
||||
# Force config materialization by running a harmless OpenClaw command
|
||||
if run_openclaw status --json >/dev/null 2>&1 && [[ -f "$oc_config" ]]; then
|
||||
if ! INSTALLER_CONFIG_FILE="$oc_config" node -e "
|
||||
const fs = require('fs');
|
||||
const configPath = process.env.INSTALLER_CONFIG_FILE;
|
||||
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
||||
if (!config.plugins) config.plugins = {};
|
||||
if (!Array.isArray(config.plugins.allow)) config.plugins.allow = [];
|
||||
if (!config.plugins.allow.includes('claude-mem')) {
|
||||
config.plugins.allow.push('claude-mem');
|
||||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
||||
console.log('Added claude-mem to plugins.allow (post-materialization)');
|
||||
}
|
||||
" 2>&1; then
|
||||
warn "Failed to write plugins.allow after materialization — configure manually"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# Restore saved plugin config (workerPort, syncMemoryFile, observationFeed, etc.)
|
||||
# from any pre-existing installation that was temporarily removed above.
|
||||
if [[ -n "$saved_plugin_config" && "$saved_plugin_config" != "{}" ]]; then
|
||||
@@ -1101,7 +1150,7 @@ write_settings() {
|
||||
|
||||
// All defaults from SettingsDefaultsManager.ts
|
||||
const defaults = {
|
||||
CLAUDE_MEM_MODEL: 'claude-sonnet-4-5',
|
||||
CLAUDE_MEM_MODEL: 'claude-sonnet-4-6',
|
||||
CLAUDE_MEM_CONTEXT_OBSERVATIONS: '50',
|
||||
CLAUDE_MEM_WORKER_PORT: '37777',
|
||||
CLAUDE_MEM_WORKER_HOST: '127.0.0.1',
|
||||
|
||||
@@ -27,6 +27,11 @@
|
||||
"default": 37777,
|
||||
"description": "Port for Claude-Mem worker service"
|
||||
},
|
||||
"workerHost": {
|
||||
"type": "string",
|
||||
"default": "127.0.0.1",
|
||||
"description": "Hostname for Claude-Mem worker service. Set to host.docker.internal when the gateway runs in Docker and the worker runs on the host."
|
||||
},
|
||||
"project": {
|
||||
"type": "string",
|
||||
"default": "openclaw",
|
||||
|
||||
@@ -979,3 +979,207 @@ describe("SSE stream integration", () => {
|
||||
await getService().stop({});
|
||||
});
|
||||
});
|
||||
|
||||
describe("circuit breaker", () => {
|
||||
// Reset circuit breaker state before each test by firing gateway_start.
|
||||
// The circuit is module-level state, so tests would otherwise bleed into each other.
|
||||
beforeEach(async () => {
|
||||
const { api, fireEvent } = createMockApi({ workerPort: 59999 });
|
||||
claudeMemPlugin(api);
|
||||
await fireEvent("gateway_start", {}, {});
|
||||
});
|
||||
|
||||
it("opens after threshold failures and stops further requests", async () => {
|
||||
const { api, logs, fireEvent } = createMockApi({ workerPort: 59999 });
|
||||
claudeMemPlugin(api);
|
||||
// Reset circuit inside the test body to guard against timers from preceding
|
||||
// tests (e.g. completionDelayMs timers) that may fire between beforeEach and here.
|
||||
await fireEvent("gateway_start", {}, {});
|
||||
|
||||
// Fire threshold+1 calls so the circuit is open by the end of the loop
|
||||
// regardless of whether a concurrent timer fires at the exact boundary.
|
||||
for (let i = 0; i < 4; i++) {
|
||||
await fireEvent("before_agent_start", { prompt: "hello" }, { sessionKey: `cb-open-${i}` });
|
||||
}
|
||||
|
||||
// Circuit is now OPEN. Subsequent calls must be silently dropped.
|
||||
const logCountBeforeDrop = logs.length;
|
||||
await fireEvent("before_agent_start", { prompt: "hello" }, { sessionKey: "cb-drop" });
|
||||
const noisyDropLogs = logs.slice(logCountBeforeDrop).filter(
|
||||
(l) => l.includes("failed") || l.includes("disabling")
|
||||
);
|
||||
assert.equal(noisyDropLogs.length, 0, "calls when circuit is open should be silently dropped");
|
||||
});
|
||||
|
||||
it("logs individual failures while circuit is closed, then disabling when it opens", async () => {
|
||||
const { api, logs, fireEvent } = createMockApi({ workerPort: 59999 });
|
||||
claudeMemPlugin(api);
|
||||
await fireEvent("gateway_start", {}, {});
|
||||
const logsAfterReset = logs.length;
|
||||
|
||||
// Fire exactly threshold (3) calls
|
||||
for (let i = 0; i < 3; i++) {
|
||||
await fireEvent("before_agent_start", { prompt: "hello" }, { sessionKey: `cb-log-${i}` });
|
||||
}
|
||||
|
||||
const newLogs = logs.slice(logsAfterReset);
|
||||
// At least some failures should have been logged (circuit was active)
|
||||
assert.ok(newLogs.length > 0, "threshold calls should produce log output");
|
||||
// Exactly one disabling warning should appear
|
||||
const disablingLogs = newLogs.filter((l) => l.includes("disabling requests"));
|
||||
assert.equal(disablingLogs.length, 1, "should emit exactly one disabling warning when circuit opens");
|
||||
// The last call (the threshold-crossing one) should NOT log an individual failure
|
||||
const failureLogs = newLogs.filter((l) => l.includes("failed:"));
|
||||
assert.ok(failureLogs.length < 3, "threshold-crossing call should not log an individual failure");
|
||||
});
|
||||
|
||||
it("resets on gateway_start, allowing connections again", async () => {
|
||||
const { api, logs, fireEvent } = createMockApi({ workerPort: 59999 });
|
||||
claudeMemPlugin(api);
|
||||
await fireEvent("gateway_start", {}, {});
|
||||
|
||||
// Open the circuit by firing threshold+1 calls
|
||||
for (let i = 0; i < 4; i++) {
|
||||
await fireEvent("before_agent_start", { prompt: "hello" }, { sessionKey: `cb-reset-${i}` });
|
||||
}
|
||||
|
||||
// Confirm circuit is open (call is silently dropped)
|
||||
const logCountWhileOpen = logs.length;
|
||||
await fireEvent("before_agent_start", { prompt: "hello" }, { sessionKey: "cb-while-open" });
|
||||
assert.equal(
|
||||
logs.slice(logCountWhileOpen).filter((l) => l.includes("failed") || l.includes("disabling")).length,
|
||||
0,
|
||||
"call while circuit is open should be silently dropped"
|
||||
);
|
||||
|
||||
// gateway_start resets the circuit
|
||||
await fireEvent("gateway_start", {}, {});
|
||||
|
||||
// Next call should attempt to connect again (not silently drop)
|
||||
const logCountAfterReset = logs.length;
|
||||
await fireEvent("before_agent_start", { prompt: "hello" }, { sessionKey: "cb-after-reset" });
|
||||
const newLogs = logs.slice(logCountAfterReset);
|
||||
assert.ok(
|
||||
newLogs.some((l) => l.includes("failed:") || l.includes("disabling")),
|
||||
"should attempt worker connection after gateway_start reset"
|
||||
);
|
||||
});
|
||||
|
||||
it("HALF_OPEN allows only a single probe — non-2xx keeps circuit open, 2xx closes it", async () => {
|
||||
// ---- Phase 1: open the circuit via network failures (unreachable port) ----
|
||||
// Reset circuit state first
|
||||
const resetMock = createMockApi({ workerPort: 59999 });
|
||||
claudeMemPlugin(resetMock.api);
|
||||
await resetMock.fireEvent("gateway_start", {}, {});
|
||||
|
||||
// Drive 4 failures to ensure circuit is OPEN
|
||||
for (let i = 0; i < 4; i++) {
|
||||
await resetMock.fireEvent("before_agent_start", { prompt: "probe-test" }, { sessionKey: `probe-phase1-${i}` });
|
||||
}
|
||||
|
||||
// ---- Phase 2: advance clock so cooldown has elapsed ----
|
||||
// _circuitOpenedAt was set during Phase 1 using the real Date.now().
|
||||
// Advancing Date.now by 31s means the next circuitAllow call sees the cooldown elapsed.
|
||||
const realDateNow = Date.now.bind(Date);
|
||||
Date.now = () => realDateNow() + 31_000;
|
||||
|
||||
try {
|
||||
// ---- Phase 3: non-2xx probe — circuit should stay OPEN ----
|
||||
// Start a server that returns 500 for all requests
|
||||
let serverA: Server | null = null;
|
||||
const portA: number = await new Promise((resolve) => {
|
||||
serverA = createServer((_req: IncomingMessage, res: ServerResponse) => {
|
||||
res.writeHead(500);
|
||||
res.end();
|
||||
});
|
||||
serverA!.listen(0, () => {
|
||||
const addr = serverA!.address();
|
||||
resolve((addr as any).port);
|
||||
});
|
||||
});
|
||||
|
||||
// Reuse the same module-level circuit state — just change the worker port.
|
||||
// Create a new mock api instance pointed at server A (500 responder).
|
||||
const mockA = createMockApi({ workerPort: portA });
|
||||
claudeMemPlugin(mockA.api);
|
||||
// Do NOT fire gateway_start here — we want the OPEN circuit state from Phase 1.
|
||||
|
||||
// The circuit is OPEN but the mocked clock says cooldown elapsed.
|
||||
// The next call should: transition to HALF_OPEN, set _halfOpenProbeInFlight=true,
|
||||
// send the probe to server A (which returns 500), then call circuitOnFailure
|
||||
// and re-open the circuit.
|
||||
const logCountAtProbe = mockA.logs.length;
|
||||
await mockA.fireEvent("before_agent_start", { prompt: "probe" }, { sessionKey: "probe-call-non2xx" });
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
|
||||
const probeALogs = mockA.logs.slice(logCountAtProbe);
|
||||
// After a 500 response, circuitOnFailure is called which logs "disabling requests"
|
||||
// (because state was HALF_OPEN) and logger.warn logs the 500 status.
|
||||
assert.ok(
|
||||
probeALogs.some((l) => l.includes("disabling") || l.includes("returned 500") || l.includes("Worker POST")),
|
||||
"non-2xx probe should keep circuit open (expected disabling or 500 status log)"
|
||||
);
|
||||
|
||||
// Verify probe flag resets: a second call with cooldown elapsed should be allowed as a new probe
|
||||
// (i.e., _halfOpenProbeInFlight was cleared by circuitOnFailure).
|
||||
// But without advancing time further the circuit is OPEN again — so calls are dropped.
|
||||
const logCountAfterFailedProbe = mockA.logs.length;
|
||||
await mockA.fireEvent("before_agent_start", { prompt: "probe" }, { sessionKey: "probe-concurrent" });
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
const droppedLogs = mockA.logs.slice(logCountAfterFailedProbe).filter(
|
||||
(l) => l.includes("failed") || l.includes("disabling")
|
||||
);
|
||||
assert.equal(droppedLogs.length, 0, "call should be silently dropped while circuit is OPEN again after failed probe");
|
||||
|
||||
serverA!.close();
|
||||
|
||||
// ---- Phase 4: 2xx probe — circuit should close ----
|
||||
// Re-open the circuit with fresh failures, then probe with a 200-returning server.
|
||||
// Reset circuit state first.
|
||||
const resetMock2 = createMockApi({ workerPort: 59999 });
|
||||
claudeMemPlugin(resetMock2.api);
|
||||
await resetMock2.fireEvent("gateway_start", {}, {});
|
||||
|
||||
// Drive failures (still using mocked Date.now, but _circuitOpenedAt will be set to
|
||||
// the mocked time, so cooldown is NOT elapsed yet from the mocked perspective).
|
||||
// We need to temporarily restore real Date.now while opening the circuit, then
|
||||
// re-mock it for the probe.
|
||||
Date.now = realDateNow;
|
||||
for (let i = 0; i < 4; i++) {
|
||||
await resetMock2.fireEvent("before_agent_start", { prompt: "probe-test" }, { sessionKey: `probe-phase4-${i}` });
|
||||
}
|
||||
// Re-advance the clock past cooldown
|
||||
Date.now = () => realDateNow() + 31_000;
|
||||
|
||||
let serverB: Server | null = null;
|
||||
const portB: number = await new Promise((resolve) => {
|
||||
serverB = createServer((_req: IncomingMessage, res: ServerResponse) => {
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
res.end(JSON.stringify({ sessionDbId: 1, promptNumber: 1, skipped: false }));
|
||||
});
|
||||
serverB!.listen(0, () => {
|
||||
const addr = serverB!.address();
|
||||
resolve((addr as any).port);
|
||||
});
|
||||
});
|
||||
|
||||
const mockB = createMockApi({ workerPort: portB });
|
||||
claudeMemPlugin(mockB.api);
|
||||
// Do NOT fire gateway_start — reuse OPEN circuit state from resetMock2.
|
||||
|
||||
const logCountBeforeSuccessProbe = mockB.logs.length;
|
||||
await mockB.fireEvent("before_agent_start", { prompt: "probe" }, { sessionKey: "probe-call-2xx" });
|
||||
await new Promise((resolve) => setTimeout(resolve, 150));
|
||||
|
||||
const successProbeLogs = mockB.logs.slice(logCountBeforeSuccessProbe);
|
||||
assert.ok(
|
||||
successProbeLogs.some((l) => l.includes("restored") || l.includes("circuit closed")),
|
||||
"2xx probe should close the circuit — expected 'restored' or 'circuit closed' log"
|
||||
);
|
||||
|
||||
serverB!.close();
|
||||
} finally {
|
||||
Date.now = realDateNow;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
+236
-46
@@ -183,6 +183,7 @@ interface ClaudeMemPluginConfig {
|
||||
syncMemoryFileExclude?: string[];
|
||||
project?: string;
|
||||
workerPort?: number;
|
||||
workerHost?: string;
|
||||
observationFeed?: {
|
||||
enabled?: boolean;
|
||||
channel?: string;
|
||||
@@ -198,6 +199,7 @@ interface ClaudeMemPluginConfig {
|
||||
|
||||
const MAX_SSE_BUFFER_SIZE = 1024 * 1024; // 1MB
|
||||
const DEFAULT_WORKER_PORT = 37777;
|
||||
const DEFAULT_WORKER_HOST = "127.0.0.1";
|
||||
|
||||
// Emoji pool for deterministic auto-assignment to unknown agents.
|
||||
// Uses a hash of the agentId to pick a consistent emoji — no persistent state needed.
|
||||
@@ -256,8 +258,77 @@ function buildGetSourceLabel(
|
||||
// Worker HTTP Client
|
||||
// ============================================================================
|
||||
|
||||
let _workerHost = DEFAULT_WORKER_HOST;
|
||||
|
||||
function workerBaseUrl(port: number): string {
|
||||
return `http://127.0.0.1:${port}`;
|
||||
return `http://${_workerHost}:${port}`;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Worker Circuit Breaker
|
||||
// ============================================================================
|
||||
// Prevents CPU-spinning retry loops when the worker is unreachable.
|
||||
// After CIRCUIT_BREAKER_THRESHOLD consecutive network errors, the circuit
|
||||
// opens and all worker calls are silently dropped for CIRCUIT_BREAKER_COOLDOWN_MS.
|
||||
// After the cooldown, one probe attempt is allowed to check if the worker recovered.
|
||||
|
||||
const CIRCUIT_BREAKER_THRESHOLD = 3;
|
||||
const CIRCUIT_BREAKER_COOLDOWN_MS = 30_000;
|
||||
|
||||
type CircuitState = "CLOSED" | "OPEN" | "HALF_OPEN";
|
||||
|
||||
let _circuitState: CircuitState = "CLOSED";
|
||||
let _circuitFailures = 0;
|
||||
let _circuitOpenedAt = 0;
|
||||
let _halfOpenProbeInFlight = false;
|
||||
|
||||
function circuitAllow(logger: PluginLogger): boolean {
|
||||
if (_circuitState === "CLOSED") return true;
|
||||
if (_circuitState === "OPEN") {
|
||||
if (Date.now() - _circuitOpenedAt >= CIRCUIT_BREAKER_COOLDOWN_MS) {
|
||||
_circuitState = "HALF_OPEN";
|
||||
logger.info("[claude-mem] Circuit breaker: probing worker connection");
|
||||
if (_halfOpenProbeInFlight) return false;
|
||||
_halfOpenProbeInFlight = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// HALF_OPEN: allow one probe through
|
||||
if (_halfOpenProbeInFlight) return false;
|
||||
_halfOpenProbeInFlight = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
function circuitOnSuccess(logger: PluginLogger): void {
|
||||
if (_circuitState !== "CLOSED") {
|
||||
logger.info("[claude-mem] Worker connection restored — circuit closed");
|
||||
}
|
||||
_circuitState = "CLOSED";
|
||||
_circuitFailures = 0;
|
||||
_halfOpenProbeInFlight = false;
|
||||
}
|
||||
|
||||
function circuitOnFailure(logger: PluginLogger): void {
|
||||
_halfOpenProbeInFlight = false;
|
||||
_circuitFailures++;
|
||||
if (
|
||||
_circuitState === "HALF_OPEN" ||
|
||||
(_circuitState === "CLOSED" && _circuitFailures >= CIRCUIT_BREAKER_THRESHOLD)
|
||||
) {
|
||||
_circuitState = "OPEN";
|
||||
_circuitOpenedAt = Date.now();
|
||||
logger.warn(
|
||||
`[claude-mem] Worker unreachable — disabling requests for ${CIRCUIT_BREAKER_COOLDOWN_MS / 1000}s`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function circuitReset(): void {
|
||||
_circuitState = "CLOSED";
|
||||
_circuitFailures = 0;
|
||||
_circuitOpenedAt = 0;
|
||||
_halfOpenProbeInFlight = false;
|
||||
}
|
||||
|
||||
async function workerPost(
|
||||
@@ -266,6 +337,7 @@ async function workerPost(
|
||||
body: Record<string, unknown>,
|
||||
logger: PluginLogger
|
||||
): Promise<Record<string, unknown> | null> {
|
||||
if (!circuitAllow(logger)) return null;
|
||||
try {
|
||||
const response = await fetch(`${workerBaseUrl(port)}${path}`, {
|
||||
method: "POST",
|
||||
@@ -273,13 +345,18 @@ async function workerPost(
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (!response.ok) {
|
||||
circuitOnFailure(logger);
|
||||
logger.warn(`[claude-mem] Worker POST ${path} returned ${response.status}`);
|
||||
return null;
|
||||
}
|
||||
circuitOnSuccess(logger);
|
||||
return (await response.json()) as Record<string, unknown>;
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
logger.warn(`[claude-mem] Worker POST ${path} failed: ${message}`);
|
||||
circuitOnFailure(logger);
|
||||
if (_circuitState !== "OPEN") {
|
||||
logger.warn(`[claude-mem] Worker POST ${path} failed: ${message}`);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -290,13 +367,24 @@ function workerPostFireAndForget(
|
||||
body: Record<string, unknown>,
|
||||
logger: PluginLogger
|
||||
): void {
|
||||
if (!circuitAllow(logger)) return;
|
||||
fetch(`${workerBaseUrl(port)}${path}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
}).then((response) => {
|
||||
if (!response.ok) {
|
||||
circuitOnFailure(logger);
|
||||
logger.warn(`[claude-mem] Worker POST ${path} returned ${response.status}`);
|
||||
return;
|
||||
}
|
||||
circuitOnSuccess(logger);
|
||||
}).catch((error: unknown) => {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
logger.warn(`[claude-mem] Worker POST ${path} failed: ${message}`);
|
||||
circuitOnFailure(logger);
|
||||
if (_circuitState !== "OPEN") {
|
||||
logger.warn(`[claude-mem] Worker POST ${path} failed: ${message}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -305,16 +393,22 @@ async function workerGetText(
|
||||
path: string,
|
||||
logger: PluginLogger
|
||||
): Promise<string | null> {
|
||||
if (!circuitAllow(logger)) return null;
|
||||
try {
|
||||
const response = await fetch(`${workerBaseUrl(port)}${path}`);
|
||||
if (!response.ok) {
|
||||
circuitOnFailure(logger);
|
||||
logger.warn(`[claude-mem] Worker GET ${path} returned ${response.status}`);
|
||||
return null;
|
||||
}
|
||||
circuitOnSuccess(logger);
|
||||
return await response.text();
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
logger.warn(`[claude-mem] Worker GET ${path} failed: ${message}`);
|
||||
circuitOnFailure(logger);
|
||||
if (_circuitState !== "OPEN") {
|
||||
logger.warn(`[claude-mem] Worker GET ${path} failed: ${message}`);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -533,6 +627,7 @@ async function connectToSSEStream(
|
||||
export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
||||
const userConfig = (api.pluginConfig || {}) as ClaudeMemPluginConfig;
|
||||
const workerPort = userConfig.workerPort || DEFAULT_WORKER_PORT;
|
||||
_workerHost = userConfig.workerHost || DEFAULT_WORKER_HOST;
|
||||
const baseProjectName = userConfig.project || "openclaw";
|
||||
const getSourceLabel = buildGetSourceLabel(userConfig.observationFeed?.emojis);
|
||||
|
||||
@@ -547,6 +642,14 @@ export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
||||
// Session tracking for observation I/O
|
||||
// ------------------------------------------------------------------
|
||||
const sessionIds = new Map<string, string>();
|
||||
const canonicalSessionKeys = new Map<string, string>();
|
||||
const sessionAliasesByCanonicalKey = new Map<string, Set<string>>();
|
||||
const pendingCompletionTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
||||
const recentPromptInits = new Map<string, number>();
|
||||
const completionDelayMs = (() => {
|
||||
const val = Number((userConfig as Record<string, unknown>).completionDelayMs);
|
||||
return Number.isFinite(val) ? Math.max(0, val) : 5000;
|
||||
})();
|
||||
const syncMemoryFile = userConfig.syncMemoryFile !== false; // default true
|
||||
const syncMemoryFileExclude = new Set(userConfig.syncMemoryFileExclude || []);
|
||||
|
||||
@@ -565,6 +668,83 @@ export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
||||
return true;
|
||||
}
|
||||
|
||||
type SessionTrackingContext = {
|
||||
sessionKey?: string;
|
||||
workspaceDir?: string;
|
||||
channelId?: string;
|
||||
conversationId?: string;
|
||||
};
|
||||
|
||||
function getSessionAliases(ctx: SessionTrackingContext): string[] {
|
||||
const aliases = new Set<string>();
|
||||
for (const rawKey of [ctx.sessionKey, ctx.conversationId, ctx.channelId]) {
|
||||
const key = typeof rawKey === "string" ? rawKey.trim() : "";
|
||||
if (key) aliases.add(key);
|
||||
}
|
||||
if (aliases.size === 0) aliases.add("default");
|
||||
return Array.from(aliases);
|
||||
}
|
||||
|
||||
function rememberSessionContext(ctx: SessionTrackingContext): { canonicalKey: string; contentSessionId: string } {
|
||||
const aliases = getSessionAliases(ctx);
|
||||
let canonicalKey = aliases.find((alias) => canonicalSessionKeys.has(alias));
|
||||
canonicalKey = canonicalKey ? canonicalSessionKeys.get(canonicalKey)! : aliases[0];
|
||||
let aliasSet = sessionAliasesByCanonicalKey.get(canonicalKey);
|
||||
if (!aliasSet) {
|
||||
aliasSet = new Set([canonicalKey]);
|
||||
sessionAliasesByCanonicalKey.set(canonicalKey, aliasSet);
|
||||
}
|
||||
for (const alias of aliases) {
|
||||
aliasSet.add(alias);
|
||||
canonicalSessionKeys.set(alias, canonicalKey);
|
||||
}
|
||||
const contentSessionId = getContentSessionId(canonicalKey);
|
||||
for (const alias of aliasSet) {
|
||||
sessionIds.set(alias, contentSessionId);
|
||||
}
|
||||
return { canonicalKey, contentSessionId };
|
||||
}
|
||||
|
||||
function shouldSkipDuplicatePromptInit(contentSessionId: string, project: string, prompt: string): boolean {
|
||||
const now = Date.now();
|
||||
for (const [key, timestamp] of recentPromptInits) {
|
||||
if (now - timestamp > 2000) recentPromptInits.delete(key);
|
||||
}
|
||||
const cacheKey = `${contentSessionId}::${project}::${prompt}`;
|
||||
const lastSeenAt = recentPromptInits.get(cacheKey);
|
||||
// Note: cache is set unconditionally before return. If workerPost fails
|
||||
// after this check, a retry within 2s would be incorrectly skipped.
|
||||
// Acceptable because before_agent_start is not retried by the runtime.
|
||||
recentPromptInits.set(cacheKey, now);
|
||||
return typeof lastSeenAt === "number" && now - lastSeenAt <= 2000;
|
||||
}
|
||||
|
||||
function clearSessionContext(ctx: SessionTrackingContext): void {
|
||||
const aliases = getSessionAliases(ctx);
|
||||
const canonicalKey = aliases
|
||||
.map((alias) => canonicalSessionKeys.get(alias))
|
||||
.find(Boolean) || aliases[0];
|
||||
const knownAliases = sessionAliasesByCanonicalKey.get(canonicalKey) || new Set([canonicalKey, ...aliases]);
|
||||
for (const alias of knownAliases) {
|
||||
canonicalSessionKeys.delete(alias);
|
||||
sessionIds.delete(alias);
|
||||
}
|
||||
sessionAliasesByCanonicalKey.delete(canonicalKey);
|
||||
sessionIds.delete(canonicalKey);
|
||||
}
|
||||
|
||||
function scheduleSessionComplete(contentSessionId: string): void {
|
||||
const existingTimer = pendingCompletionTimers.get(contentSessionId);
|
||||
if (existingTimer) clearTimeout(existingTimer);
|
||||
const timer = setTimeout(() => {
|
||||
pendingCompletionTimers.delete(contentSessionId);
|
||||
workerPostFireAndForget(workerPort, "/api/sessions/complete", {
|
||||
contentSessionId,
|
||||
}, api.logger);
|
||||
}, completionDelayMs);
|
||||
pendingCompletionTimers.set(contentSessionId, timer);
|
||||
}
|
||||
|
||||
// TTL cache for context injection to avoid re-fetching on every LLM turn.
|
||||
// before_prompt_build fires on every turn; caching for 60s keeps the worker
|
||||
// load manageable while still picking up new observations reasonably quickly.
|
||||
@@ -600,61 +780,54 @@ export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Event: session_start — init claude-mem session (fires on /new, /reset)
|
||||
// Event: session_start — track session (fires on /new, /reset)
|
||||
// Init is deferred to before_agent_start to avoid duplicate prompt records.
|
||||
// ------------------------------------------------------------------
|
||||
api.on("session_start", async (_event, ctx) => {
|
||||
const contentSessionId = getContentSessionId(ctx.sessionKey);
|
||||
|
||||
await workerPost(workerPort, "/api/sessions/init", {
|
||||
contentSessionId,
|
||||
project: getProjectName(ctx),
|
||||
prompt: "",
|
||||
}, api.logger);
|
||||
|
||||
api.logger.info(`[claude-mem] Session initialized: ${contentSessionId}`);
|
||||
const { contentSessionId } = rememberSessionContext(ctx);
|
||||
api.logger.info(`[claude-mem] Session tracking initialized: ${contentSessionId}`);
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Event: message_received — capture inbound user prompts from channels
|
||||
// Event: message_received — alias tracking only; init deferred to before_agent_start
|
||||
// ------------------------------------------------------------------
|
||||
api.on("message_received", async (event, ctx) => {
|
||||
const sessionKey = ctx.conversationId || ctx.channelId || "default";
|
||||
const contentSessionId = getContentSessionId(sessionKey);
|
||||
|
||||
await workerPost(workerPort, "/api/sessions/init", {
|
||||
contentSessionId,
|
||||
project: baseProjectName,
|
||||
prompt: event.content || "[media prompt]",
|
||||
}, api.logger);
|
||||
const { canonicalKey, contentSessionId } = rememberSessionContext(ctx);
|
||||
api.logger.info(`[claude-mem] Message received — prompt capture deferred to before_agent_start: session=${canonicalKey} contentSessionId=${contentSessionId} hasContent=${Boolean(event.content)}`);
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Event: after_compaction — re-init session after context compaction
|
||||
// Event: after_compaction — preserve session tracking after context compaction.
|
||||
// Re-init is intentionally NOT called here; the worker retains session state
|
||||
// independently and re-initializing would create duplicate prompt records.
|
||||
// ------------------------------------------------------------------
|
||||
api.on("after_compaction", async (_event, ctx) => {
|
||||
const contentSessionId = getContentSessionId(ctx.sessionKey);
|
||||
|
||||
await workerPost(workerPort, "/api/sessions/init", {
|
||||
contentSessionId,
|
||||
project: getProjectName(ctx),
|
||||
prompt: "",
|
||||
}, api.logger);
|
||||
|
||||
api.logger.info(`[claude-mem] Session re-initialized after compaction: ${contentSessionId}`);
|
||||
const { contentSessionId } = rememberSessionContext(ctx);
|
||||
api.logger.info(`[claude-mem] Session preserved after compaction: ${contentSessionId}`);
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Event: before_agent_start — init session
|
||||
// Event: before_agent_start — single init point with dedup guard
|
||||
// ------------------------------------------------------------------
|
||||
api.on("before_agent_start", async (event, ctx) => {
|
||||
const { contentSessionId } = rememberSessionContext(ctx);
|
||||
const projectName = getProjectName(ctx);
|
||||
const promptText = event.prompt || "agent run";
|
||||
|
||||
if (shouldSkipDuplicatePromptInit(contentSessionId, projectName, promptText)) {
|
||||
api.logger.info(`[claude-mem] Skipping duplicate prompt init: contentSessionId=${contentSessionId} project=${projectName}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize session in the worker so observations are not skipped
|
||||
// (the privacy check requires a stored user prompt to exist)
|
||||
const contentSessionId = getContentSessionId(ctx.sessionKey);
|
||||
await workerPost(workerPort, "/api/sessions/init", {
|
||||
contentSessionId,
|
||||
project: getProjectName(ctx),
|
||||
prompt: event.prompt || "agent run",
|
||||
project: projectName,
|
||||
prompt: promptText,
|
||||
}, api.logger);
|
||||
|
||||
api.logger.info(`[claude-mem] Session initialized via before_agent_start: contentSessionId=${contentSessionId} project=${projectName}`);
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
@@ -686,7 +859,7 @@ export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
||||
// Skip memory_ tools to prevent recursive observation loops
|
||||
if (toolName.startsWith("memory_")) return;
|
||||
|
||||
const contentSessionId = getContentSessionId(ctx.sessionKey);
|
||||
const { canonicalKey, contentSessionId } = rememberSessionContext(ctx);
|
||||
|
||||
// Extract result text from all content blocks
|
||||
let toolResponseText = "";
|
||||
@@ -704,13 +877,23 @@ export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
||||
toolResponseText = toolResponseText.slice(0, MAX_TOOL_RESPONSE_LENGTH);
|
||||
}
|
||||
|
||||
// Resolve workspaceDir with fallback chain.
|
||||
// Empty cwd causes worker-side observation queueing failures,
|
||||
// so we drop the observation rather than sending cwd: "".
|
||||
const workspaceDir = ctx.workspaceDir;
|
||||
|
||||
if (!workspaceDir) {
|
||||
api.logger.warn(`[claude-mem] Skipping observation persist because workspaceDir is unavailable: session=${canonicalKey} tool=${toolName}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fire-and-forget: send observation to worker
|
||||
workerPostFireAndForget(workerPort, "/api/sessions/observations", {
|
||||
contentSessionId,
|
||||
tool_name: toolName,
|
||||
tool_input: event.params || {},
|
||||
tool_response: toolResponseText,
|
||||
cwd: "",
|
||||
cwd: workspaceDir,
|
||||
}, api.logger);
|
||||
});
|
||||
|
||||
@@ -718,7 +901,7 @@ export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
||||
// Event: agent_end — summarize and complete session
|
||||
// ------------------------------------------------------------------
|
||||
api.on("agent_end", async (event, ctx) => {
|
||||
const contentSessionId = getContentSessionId(ctx.sessionKey);
|
||||
const { contentSessionId } = rememberSessionContext(ctx);
|
||||
|
||||
// Extract last assistant message for summarization
|
||||
let lastAssistantMessage = "";
|
||||
@@ -747,25 +930,32 @@ export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
||||
last_assistant_message: lastAssistantMessage,
|
||||
}, api.logger);
|
||||
|
||||
workerPostFireAndForget(workerPort, "/api/sessions/complete", {
|
||||
contentSessionId,
|
||||
}, api.logger);
|
||||
api.logger.info(`[claude-mem] Scheduling session complete in ${completionDelayMs}ms: ${contentSessionId}`);
|
||||
scheduleSessionComplete(contentSessionId);
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Event: session_end — clean up session tracking to prevent unbounded growth
|
||||
// ------------------------------------------------------------------
|
||||
api.on("session_end", async (_event, ctx) => {
|
||||
const key = ctx.sessionKey || "default";
|
||||
sessionIds.delete(key);
|
||||
clearSessionContext(ctx);
|
||||
api.logger.info(`[claude-mem] Session tracking cleaned up`);
|
||||
});
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Event: gateway_start — clear session tracking for fresh start
|
||||
// ------------------------------------------------------------------
|
||||
api.on("gateway_start", async () => {
|
||||
circuitReset();
|
||||
sessionIds.clear();
|
||||
contextCache.clear();
|
||||
recentPromptInits.clear();
|
||||
canonicalSessionKeys.clear();
|
||||
sessionAliasesByCanonicalKey.clear();
|
||||
for (const timer of pendingCompletionTimers.values()) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
pendingCompletionTimers.clear();
|
||||
api.logger.info("[claude-mem] Gateway started — session tracking reset");
|
||||
});
|
||||
|
||||
@@ -1047,5 +1237,5 @@ export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
||||
},
|
||||
});
|
||||
|
||||
api.logger.info(`[claude-mem] OpenClaw plugin loaded — v1.0.0 (worker: 127.0.0.1:${workerPort})`);
|
||||
api.logger.info(`[claude-mem] OpenClaw plugin loaded — v1.0.0 (worker: ${_workerHost}:${workerPort})`);
|
||||
}
|
||||
|
||||
@@ -643,7 +643,7 @@ test_write_settings_new_file() {
|
||||
|
||||
local model
|
||||
model="$(node -e "const s = JSON.parse(require('fs').readFileSync('${settings_file}','utf8')); console.log(s.CLAUDE_MEM_MODEL);")"
|
||||
assert_eq "claude-sonnet-4-5" "$model" "CLAUDE_MEM_MODEL defaults to claude-sonnet-4-5"
|
||||
assert_eq "claude-sonnet-4-6" "$model" "CLAUDE_MEM_MODEL defaults to claude-sonnet-4-6"
|
||||
|
||||
HOME="$ORIGINAL_HOME"
|
||||
rm -rf "$fake_home"
|
||||
|
||||
+32
-4
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "claude-mem",
|
||||
"version": "11.0.0",
|
||||
"version": "12.1.4",
|
||||
"description": "Memory compression system for Claude Code - persist context across sessions",
|
||||
"keywords": [
|
||||
"claude",
|
||||
@@ -60,7 +60,7 @@
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "npm run build-and-sync",
|
||||
"build": "node scripts/build-hooks.js",
|
||||
"build": "node scripts/sync-plugin-manifests.js && node scripts/build-hooks.js",
|
||||
"build-and-sync": "npm run build && npm run sync-marketplace && sleep 1 && cd ~/.claude/plugins/marketplaces/thedotmack && npm run worker:restart",
|
||||
"sync-marketplace": "node scripts/sync-marketplace.cjs",
|
||||
"sync-marketplace:force": "node scripts/sync-marketplace.cjs --force",
|
||||
@@ -115,7 +115,7 @@
|
||||
"ansi-to-html": "^0.7.2",
|
||||
"dompurify": "^3.3.1",
|
||||
"express": "^4.18.2",
|
||||
"glob": "^11.0.3",
|
||||
"glob": "^13.0.0",
|
||||
"handlebars": "^4.7.8",
|
||||
"picocolors": "^1.1.1",
|
||||
"react": "^18.3.1",
|
||||
@@ -124,6 +124,12 @@
|
||||
"zod-to-json-schema": "^3.24.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@derekstride/tree-sitter-sql": "^0.3.11",
|
||||
"@tree-sitter-grammars/tree-sitter-lua": "^0.4.1",
|
||||
"@tree-sitter-grammars/tree-sitter-markdown": "^0.3.2",
|
||||
"@tree-sitter-grammars/tree-sitter-toml": "^0.7.0",
|
||||
"@tree-sitter-grammars/tree-sitter-yaml": "^0.7.1",
|
||||
"@tree-sitter-grammars/tree-sitter-zig": "^1.1.2",
|
||||
"@types/cors": "^2.8.19",
|
||||
"@types/dompurify": "^3.0.5",
|
||||
"@types/express": "^4.17.21",
|
||||
@@ -132,20 +138,42 @@
|
||||
"@types/react-dom": "^18.3.0",
|
||||
"esbuild": "^0.27.2",
|
||||
"np": "^11.0.2",
|
||||
"tree-sitter-bash": "^0.25.1",
|
||||
"tree-sitter-c": "^0.24.1",
|
||||
"tree-sitter-cli": "^0.26.5",
|
||||
"tree-sitter-cpp": "^0.23.4",
|
||||
"tree-sitter-css": "^0.25.0",
|
||||
"tree-sitter-elixir": "^0.3.5",
|
||||
"tree-sitter-go": "^0.25.0",
|
||||
"tree-sitter-haskell": "^0.23.1",
|
||||
"tree-sitter-java": "^0.23.5",
|
||||
"tree-sitter-javascript": "^0.25.0",
|
||||
"tree-sitter-kotlin": "^0.3.8",
|
||||
"tree-sitter-php": "^0.24.2",
|
||||
"tree-sitter-python": "^0.25.0",
|
||||
"tree-sitter-ruby": "^0.23.1",
|
||||
"tree-sitter-rust": "^0.24.0",
|
||||
"tree-sitter-scala": "^0.24.0",
|
||||
"tree-sitter-scss": "^1.0.0",
|
||||
"tree-sitter-swift": "^0.7.1",
|
||||
"tree-sitter-typescript": "^0.23.2",
|
||||
"tsx": "^4.20.6",
|
||||
"typescript": "^5.3.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"tree-kill": "^1.2.2"
|
||||
}
|
||||
},
|
||||
"trustedDependencies": [
|
||||
"esbuild",
|
||||
"tree-sitter-c",
|
||||
"tree-sitter-cli",
|
||||
"tree-sitter-cpp",
|
||||
"tree-sitter-go",
|
||||
"tree-sitter-java",
|
||||
"tree-sitter-javascript",
|
||||
"tree-sitter-python",
|
||||
"tree-sitter-ruby",
|
||||
"tree-sitter-rust",
|
||||
"tree-sitter-typescript"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "claude-mem",
|
||||
"version": "11.0.0",
|
||||
"version": "12.1.4",
|
||||
"description": "Persistent memory system for Claude Code - seamlessly preserve context across sessions",
|
||||
"author": {
|
||||
"name": "Alex Newman"
|
||||
|
||||
+2
-1
@@ -2,7 +2,8 @@
|
||||
"mcpServers": {
|
||||
"mcp-search": {
|
||||
"type": "stdio",
|
||||
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/mcp-server.cjs"
|
||||
"command": "bun",
|
||||
"args": ["${CLAUDE_PLUGIN_ROOT}/scripts/mcp-server.cjs"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+21
-9
@@ -7,7 +7,7 @@
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "_R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; \"$_R/scripts/setup.sh\"",
|
||||
"command": "export PATH=\"$HOME/.nvm/versions/node/v$(ls \\\"$HOME/.nvm/versions/node\\\" 2>/dev/null | sed 's/^v//' | sort -t. -k1,1n -k2,2n -k3,3n | tail -1)/bin:$HOME/.local/bin:/usr/local/bin:/opt/homebrew/bin:$PATH\"; _R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=$(ls -dt $HOME/.claude/plugins/cache/thedotmack/claude-mem/[0-9]*/ 2>/dev/null | head -1); _R=\"${_R%/}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/smart-install.js\"",
|
||||
"timeout": 300
|
||||
}
|
||||
]
|
||||
@@ -19,17 +19,17 @@
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "_R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/smart-install.js\"",
|
||||
"command": "export PATH=\"$($SHELL -lc 'echo $PATH' 2>/dev/null):$PATH\"; _R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=$(ls -dt $HOME/.claude/plugins/cache/thedotmack/claude-mem/[0-9]*/ 2>/dev/null | head -1); _R=\"${_R%/}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/smart-install.js\"",
|
||||
"timeout": 300
|
||||
},
|
||||
{
|
||||
"type": "command",
|
||||
"command": "_R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" start",
|
||||
"command": "export PATH=\"$($SHELL -lc 'echo $PATH' 2>/dev/null):$PATH\"; _R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=$(ls -dt $HOME/.claude/plugins/cache/thedotmack/claude-mem/[0-9]*/ 2>/dev/null | head -1); _R=\"${_R%/}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" start; for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do curl -sf http://localhost:37777/health >/dev/null 2>&1 && break; sleep 1; done; curl -sf http://localhost:37777/health >/dev/null 2>&1 || true; echo '{\"continue\":true,\"suppressOutput\":true}'",
|
||||
"timeout": 60
|
||||
},
|
||||
{
|
||||
"type": "command",
|
||||
"command": "_R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" hook claude-code context",
|
||||
"command": "export PATH=\"$($SHELL -lc 'echo $PATH' 2>/dev/null):$PATH\"; _R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=$(ls -dt $HOME/.claude/plugins/cache/thedotmack/claude-mem/[0-9]*/ 2>/dev/null | head -1); _R=\"${_R%/}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do curl -sf http://localhost:37777/health >/dev/null 2>&1 && break; sleep 1; done; if curl -sf http://localhost:37777/health >/dev/null 2>&1; then node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" hook claude-code context || true; fi",
|
||||
"timeout": 60
|
||||
}
|
||||
]
|
||||
@@ -40,7 +40,7 @@
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "_R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" hook claude-code session-init",
|
||||
"command": "export PATH=\"$($SHELL -lc 'echo $PATH' 2>/dev/null):$PATH\"; _R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=$(ls -dt $HOME/.claude/plugins/cache/thedotmack/claude-mem/[0-9]*/ 2>/dev/null | head -1); _R=\"${_R%/}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" hook claude-code session-init",
|
||||
"timeout": 60
|
||||
}
|
||||
]
|
||||
@@ -52,18 +52,30 @@
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "_R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" hook claude-code observation",
|
||||
"command": "export PATH=\"$($SHELL -lc 'echo $PATH' 2>/dev/null):$PATH\"; _R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=$(ls -dt $HOME/.claude/plugins/cache/thedotmack/claude-mem/[0-9]*/ 2>/dev/null | head -1); _R=\"${_R%/}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" hook claude-code observation",
|
||||
"timeout": 120
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"PreToolUse": [
|
||||
{
|
||||
"matcher": "Read",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "export PATH=\"$($SHELL -lc 'echo $PATH' 2>/dev/null):$PATH\"; _R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=$(ls -dt $HOME/.claude/plugins/cache/thedotmack/claude-mem/[0-9]*/ 2>/dev/null | head -1); _R=\"${_R%/}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" hook claude-code file-context",
|
||||
"timeout": 2000
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"Stop": [
|
||||
{
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "_R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" hook claude-code summarize",
|
||||
"command": "export PATH=\"$($SHELL -lc 'echo $PATH' 2>/dev/null):$PATH\"; _R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=$(ls -dt $HOME/.claude/plugins/cache/thedotmack/claude-mem/[0-9]*/ 2>/dev/null | head -1); _R=\"${_R%/}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" hook claude-code summarize",
|
||||
"timeout": 120
|
||||
}
|
||||
]
|
||||
@@ -74,8 +86,8 @@
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node -e \"let d='';process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>{try{const{sessionId:s}=JSON.parse(d);if(!s){process.exit(0)}const r=require('http').request({hostname:'127.0.0.1',port:37777,path:'/api/sessions/complete',method:'POST',headers:{'Content-Type':'application/json'}});r.on('error',()=>{});r.end(JSON.stringify({contentSessionId:s}));process.exit(0)}catch{process.exit(0)}})\"",
|
||||
"timeout": 2
|
||||
"command": "export PATH=\"$($SHELL -lc 'echo $PATH' 2>/dev/null):$PATH\"; _R=\"${CLAUDE_PLUGIN_ROOT}\"; [ -z \"$_R\" ] && _R=$(ls -dt $HOME/.claude/plugins/cache/thedotmack/claude-mem/[0-9]*/ 2>/dev/null | head -1); _R=\"${_R%/}\"; [ -z \"$_R\" ] && _R=\"$HOME/.claude/plugins/marketplaces/thedotmack/plugin\"; node \"$_R/scripts/bun-runner.js\" \"$_R/scripts/worker-service.cjs\" hook claude-code session-complete",
|
||||
"timeout": 30
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
{
|
||||
"name": "Meme Token Trading",
|
||||
"description": "Solana memecoin activity monitoring, pump detection, and trading signal analysis",
|
||||
"version": "1.0.0",
|
||||
"observation_types": [
|
||||
{
|
||||
"id": "pump-detected",
|
||||
"label": "Pump Detected",
|
||||
"description": "Token showing rapid price increase with high trading activity (U/m surge, multi-timeframe gains)",
|
||||
"emoji": "🚀",
|
||||
"work_emoji": "📈"
|
||||
},
|
||||
{
|
||||
"id": "dump-detected",
|
||||
"label": "Dump Detected",
|
||||
"description": "Token showing rapid price decline, sell pressure, or activity collapse after a pump",
|
||||
"emoji": "💀",
|
||||
"work_emoji": "📉"
|
||||
},
|
||||
{
|
||||
"id": "signal-change",
|
||||
"label": "Signal Change",
|
||||
"description": "Token transitioning between signal tiers (FLAT/WATCH/RISING/STRONG) indicating momentum shift",
|
||||
"emoji": "🔄",
|
||||
"work_emoji": "📊"
|
||||
},
|
||||
{
|
||||
"id": "token-profile",
|
||||
"label": "Token Profile",
|
||||
"description": "Notable token characteristics: pool size, age, buy pressure pattern, liquidity ratio, repeat behavior",
|
||||
"emoji": "🪙",
|
||||
"work_emoji": "🔍"
|
||||
},
|
||||
{
|
||||
"id": "market-condition",
|
||||
"label": "Market Condition",
|
||||
"description": "Broad market state observation: lull, heating up, multiple pumps, activity distribution across tokens",
|
||||
"emoji": "🌡️",
|
||||
"work_emoji": "📊"
|
||||
},
|
||||
{
|
||||
"id": "algorithm-insight",
|
||||
"label": "Algorithm Insight",
|
||||
"description": "Observation about sorting behavior, signal accuracy, false positives, filter gaps, or ranking quality",
|
||||
"emoji": "⚙️",
|
||||
"work_emoji": "🔧"
|
||||
}
|
||||
],
|
||||
"observation_concepts": [
|
||||
{
|
||||
"id": "early-detection",
|
||||
"label": "Early Detection",
|
||||
"description": "Token caught before or during the initial pump phase"
|
||||
},
|
||||
{
|
||||
"id": "lifecycle",
|
||||
"label": "Lifecycle",
|
||||
"description": "Full pump-hold-dump cycle or multi-wave pattern observed"
|
||||
},
|
||||
{
|
||||
"id": "false-signal",
|
||||
"label": "False Signal",
|
||||
"description": "Token ranked high but not actually pumping, or filter/ranking issue"
|
||||
},
|
||||
{
|
||||
"id": "whale-activity",
|
||||
"label": "Whale Activity",
|
||||
"description": "Large buy pressure relative to pool size suggesting whale involvement"
|
||||
},
|
||||
{
|
||||
"id": "repeat-pumper",
|
||||
"label": "Repeat Pumper",
|
||||
"description": "Token that cycles through multiple pump-dump waves"
|
||||
},
|
||||
{
|
||||
"id": "dead-cat-bounce",
|
||||
"label": "Dead Cat Bounce",
|
||||
"description": "Brief recovery in a dumping token that tricks the ranking into surfacing it"
|
||||
},
|
||||
{
|
||||
"id": "sustained-momentum",
|
||||
"label": "Sustained Momentum",
|
||||
"description": "Token maintaining high activity and gains over extended period (5+ minutes)"
|
||||
}
|
||||
],
|
||||
"prompts": {
|
||||
"system_identity": "You are Claude-Mem, a specialized observer for Solana memecoin trading activity.\n\nCRITICAL: Record what is HAPPENING in the token market — pumps, dumps, signal transitions, market conditions, and algorithm behavior. Record token names, symbols, specific metrics (U/m, gains, buy pressure, pool size), and timing.\n\nYou do not have access to tools. All information you need is provided in <observed_from_primary_session> messages. Create observations from what you observe.",
|
||||
"spatial_awareness": "SPATIAL AWARENESS: You are observing a live token activity monitor connected to Jupiter DEX on Solana.\n- Tokens are ranked by updatesPerMinute (U/m) as the primary metric\n- Signal tiers: STRONG (45+ U/m), RISING (30+), WATCH (15+), FLAT (<15)\n- Key metrics: U/m, 1-5 minute price gains, buyPressure5m, liquidity pool size, token age\n- The sorting algorithm prioritizes activity (U/m) over price gains\n- Staleness decay: tokens with no updates for 5+ seconds get linearly decayed to 0 U/m over 10 seconds",
|
||||
"observer_role": "Your job is to monitor meme token trading activity happening RIGHT NOW, creating observations about pumps, dumps, market conditions, and algorithm behavior. You are tracking the HOT POTATO GAME — which tokens have the most trading activity and whether that activity leads to real price movement.",
|
||||
"recording_focus": "WHAT TO RECORD\n--------------\nFocus on trading signals and market behavior:\n- Pump detection: token symbol, U/m, signal tier, price gains across timeframes, buy pressure, pool size\n- Dump detection: activity collapse, negative gains, sell pressure\n- Signal transitions: FLAT→WATCH→RISING→STRONG or reverse\n- Multi-wave pumps: tokens that pump, die, then pump again\n- Market conditions: how many STRONG/RISING tokens, overall activity level\n- Algorithm quality: false positives, tokens that shouldn't be ranked high, filter gaps\n- Buy pressure ratios: buyPressure5m relative to pool liquidity (high ratio = potential whale)\n\nALWAYS INCLUDE SPECIFIC NUMBERS:\n- U/m value and signal tier\n- Price gains (1m%, 2m%, 3m%, 4m%, 5m%)\n- Buy pressure dollar amount\n- Pool liquidity\n- Token age and discovery time\n\n✅ GOOD EXAMPLES:\n- \"MEMEMAN hit 58 U/m STRONG with +82.3% 3m gain, $2.5K buy pressure on $7K pool, discovered 5 minutes ago\"\n- \"Market in deep lull: no STRONG/RISING tokens, all FLAT at 1-9 U/m, only noise-level shuffling\"\n- \"思念熊 appeared for 8th time — repeat pumper cycling FLAT→WATCH→RISING then collapsing within 3 checks\"\n\n❌ BAD EXAMPLES:\n- \"Observed token activity and recorded findings\"\n- \"Monitored market conditions and logged results\"",
|
||||
"skip_guidance": "WHEN TO SKIP\n------------\nSkip these:\n- Routine checks with no notable changes from previous observation\n- Tokens at 1-2 U/m with 0% gains (background noise)\n- Repeat observations of the same token at the same signal tier with no meaningful metric change\n- Code file reads or edits (these are algorithm changes, not token observations)\n- **No output necessary if skipping.**",
|
||||
"type_guidance": "**type**: MUST be EXACTLY one of these 6 options (no other values allowed):\n - pump-detected: rapid price increase with high trading activity\n - dump-detected: rapid price decline, sell pressure, or activity collapse\n - signal-change: token transitioning between signal tiers (FLAT/WATCH/RISING/STRONG)\n - token-profile: notable token characteristics, patterns, or repeat behavior\n - market-condition: broad market state (lull, heating up, multiple pumps)\n - algorithm-insight: observation about sorting behavior, ranking quality, or filter gaps",
|
||||
"concept_guidance": "**concepts**: 2-5 knowledge-type categories. MUST use ONLY these exact keywords:\n - early-detection: token caught before or during initial pump\n - lifecycle: full pump-hold-dump cycle or multi-wave pattern\n - false-signal: token ranked high but not actually pumping\n - whale-activity: large buy pressure relative to pool size\n - repeat-pumper: token cycling through multiple pump-dump waves\n - dead-cat-bounce: brief recovery tricking the ranking\n - sustained-momentum: high activity and gains over 5+ minutes\n\n IMPORTANT: Do NOT include the observation type as a concept.\n Types and concepts are separate dimensions.",
|
||||
"field_guidance": "**facts**: Concise, self-contained statements about token activity\nEach fact is ONE piece of information\n No pronouns - each fact must stand alone\n ALWAYS include: token symbol, U/m, signal tier, specific gain percentages, buy pressure, pool size\n Include timing: when discovered, how long at current tier, which check number\n\n**files**: Leave empty for token observations (no files involved)",
|
||||
"output_format_header": "OUTPUT FORMAT\n-------------\nOutput observations using this XML structure:",
|
||||
"format_examples": "**Token Observation Examples:**\n\n<observation>\n <type>pump-detected</type>\n <title>SIMULAT Reaches RISING at 36 U/m With +45.5% 3m Gain</title>\n <subtitle>6-day-old token building sustained momentum over 5 consecutive checks since discovery at 6 U/m</subtitle>\n <facts>\n <fact>SIMULAT reached 36 U/m RISING signal tier at 10:33 PM</fact>\n <fact>SIMULAT price gains: +15.3% 1m, +33.9% 2m, +45.5% 3m</fact>\n <fact>SIMULAT buy pressure $4.8K on $4K pool (1.2:1 pressure-to-pool ratio)</fact>\n <fact>SIMULAT first detected at 6 U/m FLAT, promoted through WATCH to RISING over 4 minutes</fact>\n </facts>\n <narrative>SIMULAT demonstrated the ideal early-detection pattern for the activity-first algorithm. First appearing at 6 U/m with +15% 1m gain, it steadily built activity through WATCH to RISING over 4 minutes. The 1.2:1 buy-pressure-to-pool ratio suggests concentrated buying interest. This token was surfaced 4 minutes before its biggest price move.</narrative>\n <concepts><concept>early-detection</concept><concept>sustained-momentum</concept></concepts>\n <files></files>\n</observation>",
|
||||
"footer": "IMPORTANT! DO NOT do any work right now other than generating OBSERVATIONS from the token monitoring data.\n\nNever reference yourself or your own actions. Focus on what is happening in the market. Include specific numbers — U/m, gains, buy pressure, pool size — in every observation. Token observations without specific metrics are useless.\n\nThese observations help us understand which tokens pump, how the algorithm detects them, and what patterns emerge over time. Thank you!",
|
||||
|
||||
"xml_title_placeholder": "[Token Symbol + Key Metric Change, e.g. 'MEMEMAN Hits 58 U/m STRONG With +82% 3m Gain']",
|
||||
"xml_subtitle_placeholder": "[One sentence with timing and context (max 24 words)]",
|
||||
"xml_fact_placeholder": "[Token symbol + specific metric: U/m value, signal tier, gain %, buy pressure $, pool size $]",
|
||||
"xml_narrative_placeholder": "[**narrative**: What happened, how fast, what the metrics say about the move, and what it means for the algorithm's detection quality]",
|
||||
"xml_concept_placeholder": "[early-detection | lifecycle | false-signal | whale-activity | repeat-pumper | dead-cat-bounce | sustained-momentum]",
|
||||
"xml_file_placeholder": "",
|
||||
|
||||
"xml_summary_request_placeholder": "[Short title: time range + key market events, e.g. '10:18-10:48 PM — MEMEMAN triple pump, SIMULAT +85% slow build']",
|
||||
"xml_summary_investigated_placeholder": "[What tokens were tracked? How many checks performed? Total updates processed?]",
|
||||
"xml_summary_learned_placeholder": "[What patterns emerged? Which token archetypes appeared? How did the algorithm perform?]",
|
||||
"xml_summary_completed_placeholder": "[How long monitored? Key pumps detected? Algorithm changes deployed?]",
|
||||
"xml_summary_next_steps_placeholder": "[What to watch for next? Any algorithm improvements identified?]",
|
||||
"xml_summary_notes_placeholder": "[Market conditions, unusual patterns, algorithm edge cases observed]",
|
||||
|
||||
"header_memory_start": "TOKEN MONITORING START\n=======================",
|
||||
"header_memory_continued": "TOKEN MONITORING CONTINUED\n===========================",
|
||||
"header_summary_checkpoint": "MARKET SUMMARY CHECKPOINT\n===========================",
|
||||
|
||||
"continuation_greeting": "Hello memory agent, you are continuing to observe live meme token trading activity.",
|
||||
"continuation_instruction": "IMPORTANT: Continue generating observations from token monitoring data using the XML structure below. Focus on NEW pumps, dumps, signal changes, and market shifts since your last observation.",
|
||||
|
||||
"summary_instruction": "Write a market summary covering: tokens that pumped, tokens that dumped, market conditions (hot vs lull periods), algorithm performance, and any patterns observed. Include specific metrics for the most notable tokens. This is a checkpoint — the monitoring session is ongoing.",
|
||||
"summary_context_label": "Token Monitoring Data:",
|
||||
"summary_format_instruction": "Respond in this XML format:",
|
||||
"summary_footer": "IMPORTANT! DO NOT do any work right now other than generating this MARKET SUMMARY.\n\nNever reference yourself or your own actions. Focus on what happened in the token market. Include specific numbers. Thank you!"
|
||||
}
|
||||
}
|
||||
+17
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "claude-mem-plugin",
|
||||
"version": "11.0.0",
|
||||
"version": "12.1.4",
|
||||
"private": true,
|
||||
"description": "Runtime dependencies for claude-mem bundled hooks",
|
||||
"type": "module",
|
||||
@@ -14,7 +14,22 @@
|
||||
"tree-sitter-python": "^0.25.0",
|
||||
"tree-sitter-ruby": "^0.23.1",
|
||||
"tree-sitter-rust": "^0.24.0",
|
||||
"tree-sitter-typescript": "^0.23.2"
|
||||
"tree-sitter-typescript": "^0.23.2",
|
||||
"tree-sitter-kotlin": "^0.3.8",
|
||||
"tree-sitter-swift": "^0.7.1",
|
||||
"tree-sitter-php": "^0.24.2",
|
||||
"tree-sitter-elixir": "^0.3.5",
|
||||
"@tree-sitter-grammars/tree-sitter-lua": "^0.4.1",
|
||||
"tree-sitter-scala": "^0.24.0",
|
||||
"tree-sitter-bash": "^0.25.1",
|
||||
"tree-sitter-haskell": "^0.23.1",
|
||||
"@tree-sitter-grammars/tree-sitter-zig": "^1.1.2",
|
||||
"tree-sitter-css": "^0.25.0",
|
||||
"tree-sitter-scss": "^1.0.0",
|
||||
"@tree-sitter-grammars/tree-sitter-toml": "^0.7.0",
|
||||
"@tree-sitter-grammars/tree-sitter-yaml": "^0.7.1",
|
||||
"@derekstride/tree-sitter-sql": "^0.3.11",
|
||||
"@tree-sitter-grammars/tree-sitter-markdown": "^0.3.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.0.0",
|
||||
|
||||
@@ -47,14 +47,29 @@ function fixBrokenScriptPath(argPath) {
|
||||
* Find Bun executable - checks PATH first, then common install locations
|
||||
*/
|
||||
function findBun() {
|
||||
// Try PATH first
|
||||
const pathCheck = spawnSync(IS_WINDOWS ? 'where' : 'which', ['bun'], {
|
||||
encoding: 'utf-8',
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
shell: IS_WINDOWS
|
||||
});
|
||||
// Try PATH first.
|
||||
// On Windows, pass a single command string to avoid Node 22+ DEP0190 deprecation warning
|
||||
// (triggered when an args array is combined with shell:true, as the args are only
|
||||
// concatenated, not escaped). Fixes #1503.
|
||||
const pathCheck = IS_WINDOWS
|
||||
? spawnSync('where bun', {
|
||||
encoding: 'utf-8',
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
shell: true
|
||||
})
|
||||
: spawnSync('which', ['bun'], {
|
||||
encoding: 'utf-8',
|
||||
stdio: ['pipe', 'pipe', 'pipe']
|
||||
});
|
||||
|
||||
if (pathCheck.status === 0 && pathCheck.stdout.trim()) {
|
||||
// On Windows, prefer bun.cmd over bun (bun is a shell script, bun.cmd is the Windows batch file)
|
||||
if (IS_WINDOWS) {
|
||||
const bunCmdPath = pathCheck.stdout.split('\n').find(line => line.trim().endsWith('bun.cmd'));
|
||||
if (bunCmdPath) {
|
||||
return bunCmdPath.trim();
|
||||
}
|
||||
}
|
||||
return 'bun'; // Found in PATH
|
||||
}
|
||||
|
||||
@@ -152,17 +167,31 @@ const stdinData = await collectStdin();
|
||||
|
||||
// Spawn Bun with the provided script and args
|
||||
// Use spawn (not spawnSync) to properly handle stdio
|
||||
// Note: Don't use shell mode on Windows - it breaks paths with spaces in usernames
|
||||
// On Windows, use cmd.exe to execute bun.cmd since npm-installed bun is a batch file
|
||||
// Use windowsHide to prevent a visible console window from spawning on Windows
|
||||
const child = spawn(bunPath, args, {
|
||||
stdio: [stdinData ? 'pipe' : 'ignore', 'inherit', 'inherit'],
|
||||
const spawnOptions = {
|
||||
stdio: ['pipe', 'inherit', 'inherit'],
|
||||
windowsHide: true,
|
||||
env: process.env
|
||||
});
|
||||
};
|
||||
|
||||
// Write buffered stdin to child's pipe, then close it so the child sees EOF
|
||||
if (stdinData && child.stdin) {
|
||||
child.stdin.write(stdinData);
|
||||
let spawnCmd = bunPath;
|
||||
let spawnArgs = args;
|
||||
|
||||
if (IS_WINDOWS) {
|
||||
// On Windows, bun.cmd must be executed via cmd /c
|
||||
spawnCmd = 'cmd';
|
||||
spawnArgs = ['/c', bunPath, ...args];
|
||||
}
|
||||
|
||||
const child = spawn(spawnCmd, spawnArgs, spawnOptions);
|
||||
|
||||
// Write buffered stdin to child's pipe, then close it so the child sees EOF.
|
||||
// Fall back to '{}' when no stdin data is available so worker-service.cjs
|
||||
// always receives valid JSON input even when Claude Code doesn't pipe stdin
|
||||
// (e.g. during SessionStart on some platforms). Fixes #1560.
|
||||
if (child.stdin) {
|
||||
child.stdin.write(stdinData || '{}');
|
||||
child.stdin.end();
|
||||
}
|
||||
|
||||
@@ -171,6 +200,12 @@ child.on('error', (err) => {
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
child.on('close', (code) => {
|
||||
child.on('close', (code, signal) => {
|
||||
// Fix #1505: When the "start" subcommand forks a daemon, the parent bun
|
||||
// process may be killed by signal (e.g. SIGKILL, exit code 137). The daemon
|
||||
// is running fine — treat signal-based exits for "start" as success.
|
||||
if ((signal || code > 128) && args.includes('start')) {
|
||||
process.exit(0);
|
||||
}
|
||||
process.exit(code || 0);
|
||||
});
|
||||
|
||||
File diff suppressed because one or more lines are too long
+128
-48
File diff suppressed because one or more lines are too long
@@ -9,7 +9,7 @@
|
||||
* for both cache and marketplace installs), falling back to script location
|
||||
* and legacy paths.
|
||||
*/
|
||||
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
||||
import { existsSync, readFileSync, writeFileSync, openSync, readSync, closeSync } from 'fs';
|
||||
import { execSync, spawnSync } from 'child_process';
|
||||
import { join, dirname } from 'path';
|
||||
import { homedir } from 'os';
|
||||
@@ -449,7 +449,7 @@ function installDeps() {
|
||||
console.error('⚠️ Bun install failed, falling back to npm...');
|
||||
console.error(' (This can happen with npm alias packages like *-cjs)');
|
||||
try {
|
||||
execSync('npm install', { cwd: ROOT, stdio: installStdio, shell: IS_WINDOWS });
|
||||
execSync('npm install --legacy-peer-deps', { cwd: ROOT, stdio: installStdio, shell: IS_WINDOWS });
|
||||
} catch (npmError) {
|
||||
throw new Error('Both bun and npm install failed: ' + npmError.message);
|
||||
}
|
||||
@@ -490,6 +490,56 @@ function verifyCriticalModules() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Mach-O 64-bit magic values as seen when reading the first 4 file bytes with readUInt32LE.
|
||||
// Native arm64/x86_64 Mach-O files start with bytes [CF FA ED FE]; readUInt32LE gives 0xFEEDFACF.
|
||||
// Byte-swapped (big-endian) Mach-O files start with bytes [FE ED FA CF]; readUInt32LE gives 0xCFFAEDFE.
|
||||
const MACHO_MAGIC_NATIVE = 0xFEEDFACF; // native 64-bit (arm64/x86_64) — file bytes CF FA ED FE
|
||||
const MACHO_MAGIC_SWAPPED = 0xCFFAEDFE; // byte-swapped 64-bit — file bytes FE ED FA CF
|
||||
|
||||
/**
|
||||
* Warn when the bundled claude-mem binary cannot run on the current platform.
|
||||
*
|
||||
* The committed binary (plugin/scripts/claude-mem) is compiled for macOS arm64.
|
||||
* On Linux or Windows it produces "Exec format error" and silently fails.
|
||||
* This check surfaces the incompatibility at install time so users know why
|
||||
* the binary path doesn't work, and confirms the JS fallback (bun-runner.js →
|
||||
* worker-service.cjs) is active and covers all functionality.
|
||||
*
|
||||
* Fixes #1547 — Plugin silently fails on Linux ARM64.
|
||||
*/
|
||||
export function checkBinaryPlatformCompatibility(binaryPath = join(ROOT, 'scripts', 'claude-mem')) {
|
||||
|
||||
if (!existsSync(binaryPath)) {
|
||||
return; // Binary absent — nothing to check (e.g. after npm install which excludes it)
|
||||
}
|
||||
|
||||
// The binary only matters on non-macOS platforms; on macOS it works correctly.
|
||||
if (process.platform === 'darwin') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Read the first 4 bytes to identify the binary format.
|
||||
let fd;
|
||||
try {
|
||||
const buf = Buffer.alloc(4);
|
||||
fd = openSync(binaryPath, 'r');
|
||||
readSync(fd, buf, 0, 4, 0);
|
||||
|
||||
const magic = buf.readUInt32LE(0);
|
||||
if (magic === MACHO_MAGIC_NATIVE || magic === MACHO_MAGIC_SWAPPED) {
|
||||
console.error('⚠️ Platform notice: The bundled claude-mem binary is macOS-only.');
|
||||
console.error(` Current platform: ${process.platform} ${process.arch}`);
|
||||
console.error(' The binary will not execute on this platform.');
|
||||
console.error(' Plugin functionality is provided by the JS fallback');
|
||||
console.error(' (bun-runner.js → worker-service.cjs) which works on all platforms.');
|
||||
}
|
||||
} catch {
|
||||
// Unreadable binary — not critical, skip silently
|
||||
} finally {
|
||||
if (fd !== undefined) closeSync(fd);
|
||||
}
|
||||
}
|
||||
|
||||
// Main execution
|
||||
try {
|
||||
// Step 1: Ensure Bun is installed and meets minimum version (REQUIRED)
|
||||
@@ -546,7 +596,7 @@ try {
|
||||
if (!verifyCriticalModules()) {
|
||||
console.error('⚠️ Retrying install with npm...');
|
||||
try {
|
||||
execSync('npm install --production', { cwd: ROOT, stdio: ['pipe', 'pipe', 'inherit'], shell: IS_WINDOWS });
|
||||
execSync('npm install --production --legacy-peer-deps', { cwd: ROOT, stdio: ['pipe', 'pipe', 'inherit'], shell: IS_WINDOWS });
|
||||
} catch {
|
||||
// npm also failed
|
||||
}
|
||||
@@ -582,6 +632,9 @@ try {
|
||||
// Step 4: Install CLI to PATH
|
||||
installCLI();
|
||||
|
||||
// Step 5: Warn if the bundled native binary is incompatible with this platform
|
||||
checkBinaryPlatformCompatibility();
|
||||
|
||||
// Output valid JSON for Claude Code hook contract
|
||||
console.log(JSON.stringify({ continue: true, suppressOutput: true }));
|
||||
} catch (e) {
|
||||
|
||||
+424
-275
File diff suppressed because one or more lines are too long
@@ -0,0 +1,80 @@
|
||||
---
|
||||
name: knowledge-agent
|
||||
description: Build and query AI-powered knowledge bases from claude-mem observations. Use when users want to create focused "brains" from their observation history, ask questions about past work patterns, or compile expertise on specific topics.
|
||||
---
|
||||
|
||||
# Knowledge Agent
|
||||
|
||||
Build and query AI-powered knowledge bases from claude-mem observations.
|
||||
|
||||
## What Are Knowledge Agents?
|
||||
|
||||
Knowledge agents are filtered corpora of observations compiled into a conversational AI session. Build a corpus from your observation history, prime it (loads the knowledge into an AI session), then ask it questions conversationally.
|
||||
|
||||
Think of them as custom "brains": "everything about hooks", "all decisions from the last month", "all bugfixes for the worker service".
|
||||
|
||||
## Workflow
|
||||
|
||||
### Step 1: Build a corpus
|
||||
|
||||
```text
|
||||
build_corpus name="hooks-expertise" description="Everything about the hooks lifecycle" project="claude-mem" concepts="hooks" limit=500
|
||||
```
|
||||
|
||||
Filter options:
|
||||
- `project` — filter by project name
|
||||
- `types` — comma-separated: decision, bugfix, feature, refactor, discovery, change
|
||||
- `concepts` — comma-separated concept tags
|
||||
- `files` — comma-separated file paths (prefix match)
|
||||
- `query` — semantic search query
|
||||
- `dateStart` / `dateEnd` — ISO date range
|
||||
- `limit` — max observations (default 500)
|
||||
|
||||
### Step 2: Prime the corpus
|
||||
|
||||
```text
|
||||
prime_corpus name="hooks-expertise"
|
||||
```
|
||||
|
||||
This creates an AI session loaded with all the corpus knowledge. Takes a moment for large corpora.
|
||||
|
||||
### Step 3: Query
|
||||
|
||||
```text
|
||||
query_corpus name="hooks-expertise" question="What are the 5 lifecycle hooks and when does each fire?"
|
||||
```
|
||||
|
||||
The knowledge agent answers from its corpus. Follow-up questions maintain context.
|
||||
|
||||
### Step 4: List corpora
|
||||
|
||||
```text
|
||||
list_corpora
|
||||
```
|
||||
|
||||
Shows all corpora with stats and priming status.
|
||||
|
||||
## Tips
|
||||
|
||||
- **Focused corpora work best** — "hooks architecture" beats "everything ever"
|
||||
- **Prime once, query many times** — the session persists across queries
|
||||
- **Reprime for fresh context** — if the conversation drifts, reprime to reset
|
||||
- **Rebuild to update** — when new observations are added, rebuild then reprime
|
||||
|
||||
## Maintenance
|
||||
|
||||
### Rebuild a corpus (refresh with new observations)
|
||||
|
||||
```text
|
||||
rebuild_corpus name="hooks-expertise"
|
||||
```
|
||||
|
||||
After rebuilding, reprime to load the updated knowledge:
|
||||
|
||||
### Reprime (fresh session)
|
||||
|
||||
```text
|
||||
reprime_corpus name="hooks-expertise"
|
||||
```
|
||||
|
||||
Clears prior Q&A context and reloads the corpus into a new session.
|
||||
@@ -125,3 +125,7 @@ get_observations(ids=[11131, 10942, 10855], orderBy="date_desc")
|
||||
- **Full observation:** ~500-1000 tokens each
|
||||
- **Batch fetch:** 1 HTTP request vs N individual requests
|
||||
- **10x token savings** by filtering before fetching
|
||||
|
||||
## Knowledge Agents
|
||||
|
||||
Want synthesized answers instead of raw records? Use `/knowledge-agent` to build a queryable corpus from your observation history. The knowledge agent reads all matching observations and answers questions conversationally.
|
||||
|
||||
@@ -143,3 +143,48 @@ Use smart_* tools for code exploration, Read for non-code files. Mix freely.
|
||||
| Explore agent | ~39,000-59,000 | Cross-file synthesis with narrative |
|
||||
|
||||
**4-8x savings** on file understanding (outline + unfold vs Read). **11-18x savings** on codebase exploration vs Explore agent. The narrower the query, the wider the gap — a 27-line function costs 55x less to read via unfold than via an Explore agent, because the agent still reads the entire file.
|
||||
|
||||
## Language Support
|
||||
|
||||
Smart-explore uses **tree-sitter AST parsing** for structural analysis. Unsupported file types fall back to text-based search.
|
||||
|
||||
### Bundled Languages
|
||||
|
||||
| Language | Extensions |
|
||||
|----------|-----------|
|
||||
| JavaScript | `.js`, `.mjs`, `.cjs` |
|
||||
| TypeScript | `.ts` |
|
||||
| TSX / JSX | `.tsx`, `.jsx` |
|
||||
| Python | `.py`, `.pyw` |
|
||||
| Go | `.go` |
|
||||
| Rust | `.rs` |
|
||||
| Ruby | `.rb` |
|
||||
| Java | `.java` |
|
||||
| C | `.c`, `.h` |
|
||||
| C++ | `.cpp`, `.cc`, `.cxx`, `.hpp`, `.hh` |
|
||||
|
||||
Files with unrecognized extensions are parsed as plain text — `smart_search` still works (grep-style), but `smart_outline` and `smart_unfold` will not extract structured symbols.
|
||||
|
||||
### Custom Grammars (`.claude-mem.json`)
|
||||
|
||||
You can register additional tree-sitter grammars for file types not in the bundled list. Create or update `.claude-mem.json` in your project root:
|
||||
|
||||
```json
|
||||
{
|
||||
"grammars": {
|
||||
".sol": "tree-sitter-solidity",
|
||||
".graphql": "tree-sitter-graphql"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Each key is a file extension; each value is the npm package name of the tree-sitter grammar. The grammar must be installed locally (`npm install tree-sitter-solidity`). Once registered, `smart_outline` and `smart_unfold` will parse those extensions structurally instead of falling back to plain text.
|
||||
|
||||
### Markdown Special Support
|
||||
|
||||
Markdown files (`.md`, `.mdx`) receive special handling beyond the generic plain-text fallback:
|
||||
|
||||
- **`smart_outline`** — extracts headings (`#`, `##`, `###`) as the symbol tree. Use it to navigate long documents without reading the full file.
|
||||
- **`smart_search`** — searches within code fences as well as prose, so queries for function names inside ` ```ts ``` ` blocks work as expected.
|
||||
- **`smart_unfold`** — expands heading sections rather than function bodies; each section up to the next same-level heading is returned as a chunk.
|
||||
- **Frontmatter** — YAML frontmatter (lines between leading `---` delimiters) is included in `smart_outline` output under a synthetic `frontmatter` symbol so metadata like `title:` and `description:` is visible without reading the whole file.
|
||||
|
||||
File diff suppressed because one or more lines are too long
+124
-1
@@ -355,6 +355,14 @@
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.03);
|
||||
}
|
||||
|
||||
.header-main {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 18px;
|
||||
min-width: 0;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.sidebar-header {
|
||||
padding: 14px 18px;
|
||||
border-bottom: 1px solid var(--color-border-primary);
|
||||
@@ -549,6 +557,42 @@
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.source-tabs {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.source-tab {
|
||||
background: transparent;
|
||||
border: 1px solid var(--color-border-primary);
|
||||
color: var(--color-text-secondary);
|
||||
border-radius: 999px;
|
||||
padding: 6px 12px;
|
||||
font-size: 12px;
|
||||
line-height: 1;
|
||||
font-weight: 600;
|
||||
letter-spacing: 0.01em;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.source-tab:hover {
|
||||
background: var(--color-bg-card-hover);
|
||||
border-color: var(--color-border-focus);
|
||||
color: var(--color-text-primary);
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
.source-tab.active {
|
||||
background: linear-gradient(135deg, var(--color-bg-button) 0%, var(--color-accent-primary) 100%);
|
||||
border-color: var(--color-bg-button);
|
||||
color: var(--color-text-button);
|
||||
box-shadow: 0 2px 8px rgba(9, 105, 218, 0.18);
|
||||
}
|
||||
|
||||
.settings-btn,
|
||||
.theme-toggle-btn {
|
||||
background: var(--color-bg-card);
|
||||
@@ -887,6 +931,49 @@
|
||||
letter-spacing: 0.5px;
|
||||
}
|
||||
|
||||
.card-source {
|
||||
padding: 2px 8px;
|
||||
border-radius: 999px;
|
||||
font-weight: 600;
|
||||
font-size: 10px;
|
||||
letter-spacing: 0.04em;
|
||||
text-transform: uppercase;
|
||||
border: 1px solid transparent;
|
||||
}
|
||||
|
||||
.source-claude {
|
||||
background: rgba(255, 138, 61, 0.12);
|
||||
color: #c25a00;
|
||||
border-color: rgba(255, 138, 61, 0.22);
|
||||
}
|
||||
|
||||
.source-codex {
|
||||
background: rgba(33, 150, 243, 0.12);
|
||||
color: #0f5ba7;
|
||||
border-color: rgba(33, 150, 243, 0.24);
|
||||
}
|
||||
|
||||
.source-cursor {
|
||||
background: rgba(124, 58, 237, 0.12);
|
||||
color: #6d28d9;
|
||||
border-color: rgba(124, 58, 237, 0.24);
|
||||
}
|
||||
|
||||
[data-theme="dark"] .source-claude {
|
||||
color: #ffb067;
|
||||
border-color: rgba(255, 176, 103, 0.2);
|
||||
}
|
||||
|
||||
[data-theme="dark"] .source-codex {
|
||||
color: #8fc7ff;
|
||||
border-color: rgba(143, 199, 255, 0.2);
|
||||
}
|
||||
|
||||
[data-theme="dark"] .source-cursor {
|
||||
color: #c4b5fd;
|
||||
border-color: rgba(196, 181, 253, 0.2);
|
||||
}
|
||||
|
||||
.card-title {
|
||||
font-size: 17px;
|
||||
margin-bottom: 14px;
|
||||
@@ -1483,6 +1570,10 @@
|
||||
padding: 14px 20px;
|
||||
}
|
||||
|
||||
.header-main {
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.status {
|
||||
gap: 6px;
|
||||
}
|
||||
@@ -1491,6 +1582,11 @@
|
||||
max-width: 160px;
|
||||
}
|
||||
|
||||
.source-tab {
|
||||
padding: 6px 10px;
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
/* Hide icon links (docs, github, twitter) on tablet */
|
||||
.icon-link {
|
||||
display: none;
|
||||
@@ -1544,6 +1640,28 @@
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.header-main {
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.source-tabs {
|
||||
width: 100%;
|
||||
flex-wrap: nowrap;
|
||||
overflow-x: auto;
|
||||
padding-bottom: 2px;
|
||||
scrollbar-width: none;
|
||||
}
|
||||
|
||||
.source-tabs::-webkit-scrollbar {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.source-tab {
|
||||
flex-shrink: 0;
|
||||
padding: 5px 10px;
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.logomark {
|
||||
height: 28px;
|
||||
}
|
||||
@@ -1732,6 +1850,11 @@
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.preview-selector select:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.preview-selector select {
|
||||
background: var(--color-bg-card);
|
||||
border: 1px solid var(--color-border-primary);
|
||||
@@ -2873,4 +2996,4 @@
|
||||
<script src="viewer-bundle.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
|
||||
@@ -27,6 +27,48 @@ const CONTEXT_GENERATOR = {
|
||||
source: 'src/services/context-generator.ts'
|
||||
};
|
||||
|
||||
/**
|
||||
* Strip hardcoded __dirname/__filename from bundled CJS output.
|
||||
*
|
||||
* When esbuild converts ESM TypeScript source to CJS format, it inlines
|
||||
* __dirname and __filename as static strings based on the SOURCE file paths
|
||||
* at build time. These `var __dirname = "/build/machine/path/..."` declarations
|
||||
* shadow the runtime's native __dirname (provided by Bun/Node's CJS module
|
||||
* wrapper), causing path resolution to fail on end-user machines.
|
||||
*
|
||||
* This post-build step removes those hardcoded assignments so the runtime
|
||||
* globals are used instead.
|
||||
*
|
||||
* See: https://github.com/thedotmack/claude-mem/issues/1410
|
||||
*/
|
||||
function stripHardcodedDirname(filePath) {
|
||||
let content = fs.readFileSync(filePath, 'utf-8');
|
||||
const before = content.length;
|
||||
|
||||
// Match both double-quoted and single-quoted string literals.
|
||||
// esbuild currently emits double quotes, but single quotes are handled
|
||||
// defensively in case future versions change quoting style.
|
||||
const str = `(?:"[^"]*"|'[^']*')`;
|
||||
|
||||
for (const id of ['__dirname', '__filename']) {
|
||||
// Remove `var <id> = "...", rest` → `var rest`
|
||||
content = content.replace(new RegExp(`\\bvar ${id}\\s*=\\s*${str},\\s*`, 'g'), 'var ');
|
||||
// Remove standalone `var <id> = "...";`
|
||||
content = content.replace(new RegExp(`\\bvar ${id}\\s*=\\s*${str};\\s*`, 'g'), '');
|
||||
// Remove `, <id> = "..."` from mid/end of var declarations
|
||||
content = content.replace(new RegExp(`,\\s*${id}\\s*=\\s*${str}`, 'g'), '');
|
||||
}
|
||||
|
||||
// Clean up dangling `var ;` left when __dirname was the sole declarator
|
||||
content = content.replace(/\bvar\s*;/g, '');
|
||||
|
||||
const removed = before - content.length;
|
||||
if (removed > 0) {
|
||||
fs.writeFileSync(filePath, content);
|
||||
console.log(` ✓ Stripped hardcoded __dirname/__filename paths (${removed} bytes)`);
|
||||
}
|
||||
}
|
||||
|
||||
async function buildHooks() {
|
||||
console.log('🔨 Building claude-mem hooks and worker service...\n');
|
||||
|
||||
@@ -69,6 +111,21 @@ async function buildHooks() {
|
||||
'tree-sitter-ruby': '^0.23.1',
|
||||
'tree-sitter-rust': '^0.24.0',
|
||||
'tree-sitter-typescript': '^0.23.2',
|
||||
'tree-sitter-kotlin': '^0.3.8',
|
||||
'tree-sitter-swift': '^0.7.1',
|
||||
'tree-sitter-php': '^0.24.2',
|
||||
'tree-sitter-elixir': '^0.3.5',
|
||||
'@tree-sitter-grammars/tree-sitter-lua': '^0.4.1',
|
||||
'tree-sitter-scala': '^0.24.0',
|
||||
'tree-sitter-bash': '^0.25.1',
|
||||
'tree-sitter-haskell': '^0.23.1',
|
||||
'@tree-sitter-grammars/tree-sitter-zig': '^1.1.2',
|
||||
'tree-sitter-css': '^0.25.0',
|
||||
'tree-sitter-scss': '^1.0.0',
|
||||
'@tree-sitter-grammars/tree-sitter-toml': '^0.7.0',
|
||||
'@tree-sitter-grammars/tree-sitter-yaml': '^0.7.1',
|
||||
'@derekstride/tree-sitter-sql': '^0.3.11',
|
||||
'@tree-sitter-grammars/tree-sitter-markdown': '^0.3.2',
|
||||
},
|
||||
engines: {
|
||||
node: '>=18.0.0',
|
||||
@@ -124,6 +181,9 @@ async function buildHooks() {
|
||||
}
|
||||
});
|
||||
|
||||
// Fix hardcoded __dirname/__filename in bundled output (#1410)
|
||||
stripHardcodedDirname(`${hooksDir}/${WORKER_SERVICE.name}.cjs`);
|
||||
|
||||
// Make worker service executable
|
||||
fs.chmodSync(`${hooksDir}/${WORKER_SERVICE.name}.cjs`, 0o755);
|
||||
const workerStats = fs.statSync(`${hooksDir}/${WORKER_SERVICE.name}.cjs`);
|
||||
@@ -152,6 +212,21 @@ async function buildHooks() {
|
||||
'tree-sitter-java',
|
||||
'tree-sitter-c',
|
||||
'tree-sitter-cpp',
|
||||
'tree-sitter-kotlin',
|
||||
'tree-sitter-swift',
|
||||
'tree-sitter-php',
|
||||
'tree-sitter-elixir',
|
||||
'@tree-sitter-grammars/tree-sitter-lua',
|
||||
'tree-sitter-scala',
|
||||
'tree-sitter-bash',
|
||||
'tree-sitter-haskell',
|
||||
'@tree-sitter-grammars/tree-sitter-zig',
|
||||
'tree-sitter-css',
|
||||
'tree-sitter-scss',
|
||||
'@tree-sitter-grammars/tree-sitter-toml',
|
||||
'@tree-sitter-grammars/tree-sitter-yaml',
|
||||
'@derekstride/tree-sitter-sql',
|
||||
'@tree-sitter-grammars/tree-sitter-markdown',
|
||||
],
|
||||
define: {
|
||||
'__DEFAULT_PACKAGE_VERSION__': `"${version}"`
|
||||
@@ -161,11 +236,50 @@ async function buildHooks() {
|
||||
}
|
||||
});
|
||||
|
||||
// Fix hardcoded __dirname/__filename in bundled output (#1410)
|
||||
stripHardcodedDirname(`${hooksDir}/${MCP_SERVER.name}.cjs`);
|
||||
|
||||
// Make MCP server executable
|
||||
fs.chmodSync(`${hooksDir}/${MCP_SERVER.name}.cjs`, 0o755);
|
||||
const mcpServerStats = fs.statSync(`${hooksDir}/${MCP_SERVER.name}.cjs`);
|
||||
console.log(`✓ mcp-server built (${(mcpServerStats.size / 1024).toFixed(2)} KB)`);
|
||||
|
||||
// GUARDRAIL (#1645): The MCP server runs under Node, but the entire `bun:`
|
||||
// module namespace (bun:sqlite, bun:ffi, bun:test, etc.) is Bun-only. If
|
||||
// any transitive import in mcp-server.ts ever pulls one in, the bundle
|
||||
// will crash on first require under Node — which is exactly the regression
|
||||
// PR #1645 fixed for `bun:sqlite`. Fail the build instead of shipping a
|
||||
// broken bundle so future contributors get an immediate signal.
|
||||
//
|
||||
// Only flag actual `require("bun:...")` / `require('bun:...')` calls, not
|
||||
// the bare string — error messages and inline comments may legitimately
|
||||
// mention `bun:sqlite` by name without re-introducing the import.
|
||||
const mcpBundleContent = fs.readFileSync(`${hooksDir}/${MCP_SERVER.name}.cjs`, 'utf-8');
|
||||
const bunRequireRegex = /require\(\s*["']bun:[a-z][a-z0-9_-]*["']\s*\)/;
|
||||
const bunRequireMatch = mcpBundleContent.match(bunRequireRegex);
|
||||
if (bunRequireMatch) {
|
||||
throw new Error(
|
||||
`mcp-server.cjs contains a Bun-only ${bunRequireMatch[0]} call. This means a transitive import in src/servers/mcp-server.ts pulled in code from worker-service.ts (or another module that touches DatabaseManager/ChromaSync). The MCP server runs under Node and cannot load bun:* modules. Audit recent imports in src/servers/mcp-server.ts and src/services/worker-spawner.ts — the spawner module is intentionally lightweight and MUST NOT import anything that touches SQLite or other Bun-only modules. See PR #1645 for context.`
|
||||
);
|
||||
}
|
||||
|
||||
// SECONDARY GUARDRAIL (#1645 round 11): bundle size budget. The bun:sqlite
|
||||
// regex above catches the specific regression class we already know about,
|
||||
// but esbuild could in theory change how it emits external module specifiers
|
||||
// and silently slip past the regex. A bundle-size budget catches the
|
||||
// structural symptom (worker-service.ts dragged into the bundle blew the
|
||||
// size from ~358KB to ~1.96MB) regardless of how the imports look.
|
||||
//
|
||||
// 600KB is a generous ceiling — current size is ~384KB, the broken v12.0.0
|
||||
// bundle was ~1920KB, and there's plenty of headroom for legitimate growth
|
||||
// before we'd want to revisit this number.
|
||||
const MCP_SERVER_MAX_BYTES = 600 * 1024;
|
||||
if (mcpServerStats.size > MCP_SERVER_MAX_BYTES) {
|
||||
throw new Error(
|
||||
`mcp-server.cjs is ${(mcpServerStats.size / 1024).toFixed(2)} KB, exceeding the ${(MCP_SERVER_MAX_BYTES / 1024).toFixed(0)} KB budget. This usually means a transitive import pulled worker-service.ts (or another heavy module) into the MCP bundle. The MCP server is supposed to be a thin HTTP wrapper — audit recent imports in src/servers/mcp-server.ts and src/services/worker-spawner.ts. See PR #1645 for context on why this guardrail exists.`
|
||||
);
|
||||
}
|
||||
|
||||
// Build context generator
|
||||
console.log(`\n🔧 Building context generator...`);
|
||||
await build({
|
||||
@@ -184,6 +298,9 @@ async function buildHooks() {
|
||||
// No banner needed: CJS files under Node.js have __dirname/__filename natively
|
||||
});
|
||||
|
||||
// Fix hardcoded __dirname/__filename in bundled output (#1410)
|
||||
stripHardcodedDirname(`${hooksDir}/${CONTEXT_GENERATOR.name}.cjs`);
|
||||
|
||||
const contextGenStats = fs.statSync(`${hooksDir}/${CONTEXT_GENERATOR.name}.cjs`);
|
||||
console.log(`✓ context-generator built (${(contextGenStats.size / 1024).toFixed(2)} KB)`);
|
||||
|
||||
|
||||
Executable
+337
@@ -0,0 +1,337 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# E2E Test: Knowledge Agents
|
||||
# Fully hands-off test of the complete knowledge agent lifecycle.
|
||||
# Designed to be orchestrated via tmux-cli from Claude Code.
|
||||
#
|
||||
# Flow: health check → build corpus → list → get → prime → query → reprime → query → rebuild → delete → verify
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
WORKER_URL="http://localhost:37777"
|
||||
CORPUS_NAME="e2e-test-knowledge-agent"
|
||||
PASS_COUNT=0
|
||||
FAIL_COUNT=0
|
||||
LOG_FILE="${HOME}/.claude-mem/logs/e2e-knowledge-agents-$(date +%Y%m%d-%H%M%S).log"
|
||||
|
||||
# -- Helpers ------------------------------------------------------------------
|
||||
|
||||
log() { echo "[$(date +%H:%M:%S)] $*" | tee -a "$LOG_FILE"; }
|
||||
pass() { PASS_COUNT=$((PASS_COUNT + 1)); log "PASS: $1"; }
|
||||
fail() { FAIL_COUNT=$((FAIL_COUNT + 1)); log "FAIL: $1 — $2"; }
|
||||
|
||||
assert_http_status() {
|
||||
local description="$1" expected_status="$2" actual_status="$3"
|
||||
if [[ "$actual_status" == "$expected_status" ]]; then
|
||||
pass "$description (HTTP $actual_status)"
|
||||
else
|
||||
fail "$description" "expected HTTP $expected_status, got $actual_status"
|
||||
fi
|
||||
}
|
||||
|
||||
assert_json_field() {
|
||||
local description="$1" json="$2" field="$3" expected="$4"
|
||||
local actual
|
||||
actual=$(echo "$json" | jq -r "$field" 2>/dev/null || echo "PARSE_ERROR")
|
||||
if [[ "$actual" == "$expected" ]]; then
|
||||
pass "$description ($field=$actual)"
|
||||
else
|
||||
fail "$description" "expected $field=$expected, got $actual"
|
||||
fi
|
||||
}
|
||||
|
||||
assert_json_field_not_empty() {
|
||||
local description="$1" json="$2" field="$3"
|
||||
local actual
|
||||
actual=$(echo "$json" | jq -r "$field" 2>/dev/null || echo "")
|
||||
if [[ -n "$actual" && "$actual" != "null" && "$actual" != "" ]]; then
|
||||
pass "$description ($field is present)"
|
||||
else
|
||||
fail "$description" "$field is empty or null"
|
||||
fi
|
||||
}
|
||||
|
||||
assert_json_field_numeric_gt() {
|
||||
local description="$1" json="$2" field="$3" min_value="$4"
|
||||
local actual
|
||||
actual=$(echo "$json" | jq -r "$field" 2>/dev/null || echo "0")
|
||||
if [[ "$actual" -gt "$min_value" ]] 2>/dev/null; then
|
||||
pass "$description ($field=$actual > $min_value)"
|
||||
else
|
||||
fail "$description" "expected $field > $min_value, got $actual"
|
||||
fi
|
||||
}
|
||||
|
||||
curl_get() {
|
||||
curl -sS --connect-timeout 5 --max-time 30 -w '\n%{http_code}' "$WORKER_URL$1" 2>/dev/null || printf '\n000'
|
||||
}
|
||||
|
||||
curl_post() {
|
||||
local path="$1" body="$2" max_time="${3:-30}"
|
||||
curl -sS --connect-timeout 5 --max-time "$max_time" -w '\n%{http_code}' -X POST "$WORKER_URL$path" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "$body" 2>/dev/null || printf '\n000'
|
||||
}
|
||||
|
||||
curl_delete() {
|
||||
curl -sS --connect-timeout 5 --max-time 30 -w '\n%{http_code}' -X DELETE "$WORKER_URL$1" 2>/dev/null || printf '\n000'
|
||||
}
|
||||
|
||||
extract_body_and_status() {
|
||||
local response="$1"
|
||||
RESPONSE_BODY=$(echo "$response" | sed '$d')
|
||||
RESPONSE_STATUS=$(echo "$response" | tail -1)
|
||||
}
|
||||
|
||||
# -- Cleanup ------------------------------------------------------------------
|
||||
|
||||
cleanup_test_corpus() {
|
||||
log "Cleaning up test corpus '$CORPUS_NAME'..."
|
||||
curl -s -X DELETE "$WORKER_URL/api/corpus/$CORPUS_NAME" > /dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
# -- Tests --------------------------------------------------------------------
|
||||
|
||||
test_worker_health() {
|
||||
log "=== Test: Worker Health ==="
|
||||
local response
|
||||
response=$(curl_get "/api/health")
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Worker health check" "200" "$RESPONSE_STATUS"
|
||||
}
|
||||
|
||||
test_worker_readiness() {
|
||||
log "=== Test: Worker Readiness ==="
|
||||
local response
|
||||
response=$(curl_get "/api/readiness")
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Worker readiness check" "200" "$RESPONSE_STATUS"
|
||||
}
|
||||
|
||||
test_build_corpus() {
|
||||
log "=== Test: Build Corpus ==="
|
||||
local response
|
||||
response=$(curl_post "/api/corpus" "{
|
||||
\"name\": \"$CORPUS_NAME\",
|
||||
\"description\": \"E2E test corpus for knowledge agents\",
|
||||
\"query\": \"architecture\",
|
||||
\"limit\": 20
|
||||
}")
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Build corpus" "200" "$RESPONSE_STATUS"
|
||||
assert_json_field "Build corpus name" "$RESPONSE_BODY" ".name" "$CORPUS_NAME"
|
||||
assert_json_field_not_empty "Build corpus description" "$RESPONSE_BODY" ".description"
|
||||
assert_json_field_not_empty "Build corpus stats" "$RESPONSE_BODY" ".stats.observation_count"
|
||||
log "Build response: $(echo "$RESPONSE_BODY" | jq -c '{name, stats: .stats}' 2>/dev/null)"
|
||||
}
|
||||
|
||||
test_list_corpora() {
|
||||
log "=== Test: List Corpora ==="
|
||||
local response
|
||||
response=$(curl_get "/api/corpus")
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "List corpora" "200" "$RESPONSE_STATUS"
|
||||
|
||||
# Verify our test corpus is in the list
|
||||
local found
|
||||
found=$(echo "$RESPONSE_BODY" | jq -r ".[] | select(.name == \"$CORPUS_NAME\") | .name" 2>/dev/null)
|
||||
if [[ "$found" == "$CORPUS_NAME" ]]; then
|
||||
pass "Test corpus found in list"
|
||||
else
|
||||
fail "Test corpus in list" "corpus '$CORPUS_NAME' not found"
|
||||
fi
|
||||
}
|
||||
|
||||
test_get_corpus() {
|
||||
log "=== Test: Get Corpus ==="
|
||||
local response
|
||||
response=$(curl_get "/api/corpus/$CORPUS_NAME")
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Get corpus" "200" "$RESPONSE_STATUS"
|
||||
assert_json_field "Get corpus name" "$RESPONSE_BODY" ".name" "$CORPUS_NAME"
|
||||
assert_json_field "Get corpus session_id (pre-prime)" "$RESPONSE_BODY" ".session_id" "null"
|
||||
}
|
||||
|
||||
test_get_corpus_404() {
|
||||
log "=== Test: Get Nonexistent Corpus ==="
|
||||
local response
|
||||
response=$(curl_get "/api/corpus/nonexistent-corpus-that-does-not-exist")
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Get nonexistent corpus returns 404" "404" "$RESPONSE_STATUS"
|
||||
}
|
||||
|
||||
test_prime_corpus() {
|
||||
log "=== Test: Prime Corpus ==="
|
||||
log " (This may take 30-120 seconds — Agent SDK session is being created...)"
|
||||
local response
|
||||
response=$(curl_post "/api/corpus/$CORPUS_NAME/prime" '{}' 300)
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Prime corpus" "200" "$RESPONSE_STATUS"
|
||||
assert_json_field_not_empty "Prime returns session_id" "$RESPONSE_BODY" ".session_id"
|
||||
assert_json_field "Prime returns corpus name" "$RESPONSE_BODY" ".name" "$CORPUS_NAME"
|
||||
log "Prime response: $(echo "$RESPONSE_BODY" | jq -c '{name, session_id: (.session_id | .[0:20] + "...")}' 2>/dev/null)"
|
||||
}
|
||||
|
||||
test_query_corpus() {
|
||||
log "=== Test: Query Corpus ==="
|
||||
local response
|
||||
response=$(curl_post "/api/corpus/$CORPUS_NAME/query" '{"question": "What are the main topics and themes in this knowledge base? Give a brief summary."}' 300)
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Query corpus" "200" "$RESPONSE_STATUS"
|
||||
assert_json_field_not_empty "Query returns answer" "$RESPONSE_BODY" ".answer"
|
||||
assert_json_field_not_empty "Query returns session_id" "$RESPONSE_BODY" ".session_id"
|
||||
|
||||
local answer_length
|
||||
answer_length=$(echo "$RESPONSE_BODY" | jq -r '.answer | length' 2>/dev/null || echo "0")
|
||||
if [[ "$answer_length" -gt 50 ]]; then
|
||||
pass "Query answer is substantive (${answer_length} chars)"
|
||||
else
|
||||
fail "Query answer length" "expected > 50 chars, got $answer_length"
|
||||
fi
|
||||
log "Query answer preview: $(echo "$RESPONSE_BODY" | jq -r '.answer' 2>/dev/null | head -3)"
|
||||
}
|
||||
|
||||
test_query_without_prime() {
|
||||
log "=== Test: Query Unprimed Corpus ==="
|
||||
# Build a second corpus but don't prime it
|
||||
curl_post "/api/corpus" "{\"name\": \"e2e-unprimed-test\", \"limit\": 5}" > /dev/null 2>&1
|
||||
local response
|
||||
response=$(curl_post "/api/corpus/e2e-unprimed-test/query" '{"question": "test"}' 30)
|
||||
extract_body_and_status "$response"
|
||||
# Should fail because corpus isn't primed
|
||||
if [[ "$RESPONSE_STATUS" != "200" ]] || echo "$RESPONSE_BODY" | jq -r '.error' 2>/dev/null | grep -qi "prime\|session"; then
|
||||
pass "Query unprimed corpus correctly rejected"
|
||||
else
|
||||
fail "Query unprimed corpus" "expected error about priming, got HTTP $RESPONSE_STATUS"
|
||||
fi
|
||||
# Cleanup
|
||||
curl -s -X DELETE "$WORKER_URL/api/corpus/e2e-unprimed-test" > /dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
test_reprime_corpus() {
|
||||
log "=== Test: Reprime Corpus ==="
|
||||
log " (Creating fresh session...)"
|
||||
|
||||
# Capture old session_id
|
||||
local old_response old_session_id
|
||||
old_response=$(curl_get "/api/corpus/$CORPUS_NAME")
|
||||
extract_body_and_status "$old_response"
|
||||
old_session_id=$(echo "$RESPONSE_BODY" | jq -r '.session_id' 2>/dev/null)
|
||||
|
||||
local response
|
||||
response=$(curl_post "/api/corpus/$CORPUS_NAME/reprime" '{}' 300)
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Reprime corpus" "200" "$RESPONSE_STATUS"
|
||||
assert_json_field_not_empty "Reprime returns session_id" "$RESPONSE_BODY" ".session_id"
|
||||
|
||||
local new_session_id
|
||||
new_session_id=$(echo "$RESPONSE_BODY" | jq -r '.session_id' 2>/dev/null)
|
||||
if [[ "$new_session_id" != "$old_session_id" ]]; then
|
||||
pass "Reprime created new session (different session_id)"
|
||||
else
|
||||
fail "Reprime session_id" "expected new session_id, got same as before"
|
||||
fi
|
||||
}
|
||||
|
||||
test_query_after_reprime() {
|
||||
log "=== Test: Query After Reprime ==="
|
||||
local response
|
||||
response=$(curl_post "/api/corpus/$CORPUS_NAME/query" '{"question": "List the types of observations in this knowledge base."}' 300)
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Query after reprime" "200" "$RESPONSE_STATUS"
|
||||
assert_json_field_not_empty "Answer after reprime" "$RESPONSE_BODY" ".answer"
|
||||
log "Post-reprime answer preview: $(echo "$RESPONSE_BODY" | jq -r '.answer' 2>/dev/null | head -3)"
|
||||
}
|
||||
|
||||
test_rebuild_corpus() {
|
||||
log "=== Test: Rebuild Corpus ==="
|
||||
local response
|
||||
response=$(curl_post "/api/corpus/$CORPUS_NAME/rebuild" '{}' 60)
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Rebuild corpus" "200" "$RESPONSE_STATUS"
|
||||
assert_json_field "Rebuild returns name" "$RESPONSE_BODY" ".name" "$CORPUS_NAME"
|
||||
assert_json_field_not_empty "Rebuild returns stats" "$RESPONSE_BODY" ".stats.observation_count"
|
||||
}
|
||||
|
||||
test_delete_corpus() {
|
||||
log "=== Test: Delete Corpus ==="
|
||||
local response
|
||||
response=$(curl_delete "/api/corpus/$CORPUS_NAME")
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Delete corpus" "200" "$RESPONSE_STATUS"
|
||||
|
||||
# Verify it's gone
|
||||
local verify_response
|
||||
verify_response=$(curl_get "/api/corpus/$CORPUS_NAME")
|
||||
extract_body_and_status "$verify_response"
|
||||
assert_http_status "Deleted corpus returns 404" "404" "$RESPONSE_STATUS"
|
||||
}
|
||||
|
||||
test_delete_nonexistent() {
|
||||
log "=== Test: Delete Nonexistent Corpus ==="
|
||||
local response
|
||||
response=$(curl_delete "/api/corpus/nonexistent-corpus-that-does-not-exist")
|
||||
extract_body_and_status "$response"
|
||||
assert_http_status "Delete nonexistent returns 404" "404" "$RESPONSE_STATUS"
|
||||
}
|
||||
|
||||
# -- Main ---------------------------------------------------------------------
|
||||
|
||||
main() {
|
||||
mkdir -p "$(dirname "$LOG_FILE")"
|
||||
log "======================================================"
|
||||
log " Knowledge Agents E2E Test"
|
||||
log " $(date)"
|
||||
log "======================================================"
|
||||
log ""
|
||||
|
||||
# Cleanup any leftover test data
|
||||
cleanup_test_corpus
|
||||
|
||||
# Phase 1: Health checks
|
||||
test_worker_health
|
||||
test_worker_readiness
|
||||
log ""
|
||||
|
||||
# Phase 2: CRUD operations
|
||||
test_build_corpus
|
||||
test_list_corpora
|
||||
test_get_corpus
|
||||
test_get_corpus_404
|
||||
log ""
|
||||
|
||||
# Phase 3: Agent SDK operations (prime + query)
|
||||
test_prime_corpus
|
||||
test_query_corpus
|
||||
test_query_without_prime
|
||||
log ""
|
||||
|
||||
# Phase 4: Reprime + query again
|
||||
test_reprime_corpus
|
||||
test_query_after_reprime
|
||||
log ""
|
||||
|
||||
# Phase 5: Rebuild + cleanup
|
||||
test_rebuild_corpus
|
||||
test_delete_corpus
|
||||
test_delete_nonexistent
|
||||
log ""
|
||||
|
||||
# Summary
|
||||
local total=$((PASS_COUNT + FAIL_COUNT))
|
||||
log "======================================================"
|
||||
log " RESULTS: $PASS_COUNT/$total passed, $FAIL_COUNT failed"
|
||||
log "======================================================"
|
||||
|
||||
if [[ "$FAIL_COUNT" -gt 0 ]]; then
|
||||
log " STATUS: FAILED"
|
||||
log " Log: $LOG_FILE"
|
||||
exit 1
|
||||
else
|
||||
log " STATUS: ALL PASSED"
|
||||
log " Log: $LOG_FILE"
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
|
||||
main "$@"
|
||||
@@ -94,9 +94,12 @@ function getTrackedFolders(workingDir: string): Set<string> {
|
||||
const absPath = path.join(workingDir, file);
|
||||
let dir = path.dirname(absPath);
|
||||
|
||||
// Add all parent directories up to (but not including) the working dir
|
||||
while (dir.length > workingDir.length && dir.startsWith(workingDir)) {
|
||||
// Add all parent directories up to and including the working dir itself.
|
||||
// The working dir is included so that root-level files (stored in the DB
|
||||
// as bare filenames with no directory component) can be matched. Fixes #1514.
|
||||
while (dir.length >= workingDir.length && dir.startsWith(workingDir)) {
|
||||
folders.add(dir);
|
||||
if (dir === workingDir) break;
|
||||
dir = path.dirname(dir);
|
||||
}
|
||||
}
|
||||
@@ -164,19 +167,37 @@ function findObservationsByFolder(db: Database, relativeFolderPath: string, proj
|
||||
// Query more results than needed since we'll filter some out
|
||||
const queryLimit = limit * 3;
|
||||
|
||||
const sql = `
|
||||
SELECT o.*, o.discovery_tokens
|
||||
FROM observations o
|
||||
WHERE o.project = ?
|
||||
AND (o.files_modified LIKE ? OR o.files_read LIKE ?)
|
||||
ORDER BY o.created_at_epoch DESC
|
||||
LIMIT ?
|
||||
`;
|
||||
// For the root folder (empty relativeFolderPath), observations may have bare
|
||||
// filenames stored without any directory component (e.g. ["dashboard.html"]).
|
||||
// In that case the LIKE pattern below would never match, so we fetch all
|
||||
// observations for the project and let isDirectChild filter to root-level files.
|
||||
// Fixes #1514.
|
||||
let allMatches: ObservationRow[];
|
||||
|
||||
// Files in DB are stored as relative paths like "src/services/foo.ts"
|
||||
// Match any file that starts with this folder path (we'll filter to direct children below)
|
||||
const likePattern = `%"${relativeFolderPath}/%`;
|
||||
const allMatches = db.prepare(sql).all(project, likePattern, likePattern, queryLimit) as ObservationRow[];
|
||||
if (relativeFolderPath === '' || relativeFolderPath === '.') {
|
||||
const sql = `
|
||||
SELECT o.*, o.discovery_tokens
|
||||
FROM observations o
|
||||
WHERE o.project = ?
|
||||
AND (o.files_modified IS NOT NULL OR o.files_read IS NOT NULL)
|
||||
ORDER BY o.created_at_epoch DESC
|
||||
LIMIT ?
|
||||
`;
|
||||
allMatches = db.prepare(sql).all(project, queryLimit) as ObservationRow[];
|
||||
} else {
|
||||
const sql = `
|
||||
SELECT o.*, o.discovery_tokens
|
||||
FROM observations o
|
||||
WHERE o.project = ?
|
||||
AND (o.files_modified LIKE ? OR o.files_read LIKE ?)
|
||||
ORDER BY o.created_at_epoch DESC
|
||||
LIMIT ?
|
||||
`;
|
||||
// Files in DB are stored as relative paths like "src/services/foo.ts"
|
||||
// Match any file that starts with this folder path (we'll filter to direct children below)
|
||||
const likePattern = `%"${relativeFolderPath}/%`;
|
||||
allMatches = db.prepare(sql).all(project, likePattern, likePattern, queryLimit) as ObservationRow[];
|
||||
}
|
||||
|
||||
// Filter to only observations with direct child files (not in subfolders)
|
||||
return allMatches.filter(obs => hasDirectChildFile(obs, relativeFolderPath)).slice(0, limit);
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const rootDir = path.resolve(__dirname, '..');
|
||||
|
||||
const packageJsonPath = path.join(rootDir, 'package.json');
|
||||
const codexPluginPath = path.join(rootDir, '.codex-plugin', 'plugin.json');
|
||||
const claudePluginPath = path.join(rootDir, '.claude-plugin', 'plugin.json');
|
||||
|
||||
function readJson(filePath) {
|
||||
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
}
|
||||
|
||||
function writeJson(filePath, value) {
|
||||
fs.writeFileSync(filePath, JSON.stringify(value, null, 2) + '\n');
|
||||
}
|
||||
|
||||
function syncCodexPlugin(plugin, pkg) {
|
||||
const author =
|
||||
typeof plugin.author === 'object' && plugin.author ? plugin.author : {};
|
||||
|
||||
return {
|
||||
...plugin,
|
||||
name: pkg.name,
|
||||
version: pkg.version,
|
||||
description: pkg.description,
|
||||
homepage: pkg.homepage,
|
||||
repository: normalizeRepositoryUrl(pkg.repository),
|
||||
license: pkg.license,
|
||||
keywords: pkg.keywords,
|
||||
author: {
|
||||
...author,
|
||||
name: normalizeAuthorName(pkg.author),
|
||||
},
|
||||
interface: {
|
||||
...plugin.interface,
|
||||
developerName: normalizeAuthorName(pkg.author),
|
||||
websiteURL: normalizeRepositoryUrl(pkg.repository),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function syncClaudePlugin(plugin, pkg) {
|
||||
return {
|
||||
...plugin,
|
||||
name: pkg.name,
|
||||
version: pkg.version,
|
||||
description: pkg.description,
|
||||
homepage: pkg.homepage,
|
||||
repository: normalizeRepositoryUrl(pkg.repository),
|
||||
license: pkg.license,
|
||||
keywords: pkg.keywords,
|
||||
author: {
|
||||
...(typeof plugin.author === 'object' && plugin.author ? plugin.author : {}),
|
||||
name: normalizeAuthorName(pkg.author),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeAuthorName(author) {
|
||||
if (typeof author === 'string') return author;
|
||||
if (author && typeof author === 'object' && typeof author.name === 'string') return author.name;
|
||||
return '';
|
||||
}
|
||||
|
||||
function normalizeRepositoryUrl(repository) {
|
||||
if (typeof repository === 'string') return repository.replace(/\.git$/, '');
|
||||
if (repository && typeof repository === 'object' && typeof repository.url === 'string')
|
||||
return repository.url.replace(/\.git$/, '');
|
||||
return '';
|
||||
}
|
||||
|
||||
function main() {
|
||||
for (const filePath of [packageJsonPath, codexPluginPath, claudePluginPath]) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`Missing required file: ${filePath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
const pkg = readJson(packageJsonPath);
|
||||
const codexPlugin = readJson(codexPluginPath);
|
||||
const claudePlugin = readJson(claudePluginPath);
|
||||
|
||||
writeJson(codexPluginPath, syncCodexPlugin(codexPlugin, pkg));
|
||||
writeJson(claudePluginPath, syncClaudePlugin(claudePlugin, pkg));
|
||||
|
||||
console.log('✓ Synced plugin manifests from package.json');
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -13,7 +13,7 @@ import type { PlatformAdapter } from '../types.js';
|
||||
* Notification → observation (system events like ToolPermission)
|
||||
*
|
||||
* Agent:
|
||||
* BeforeAgent → user-message (captures user prompt)
|
||||
* BeforeAgent → session-init (initializes session, captures user prompt)
|
||||
* AfterAgent → observation (full agent response)
|
||||
*
|
||||
* Tool:
|
||||
|
||||
@@ -12,6 +12,7 @@ import { HOOK_EXIT_CODES } from '../../shared/hook-constants.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { SettingsDefaultsManager } from '../../shared/SettingsDefaultsManager.js';
|
||||
import { USER_SETTINGS_PATH } from '../../shared/paths.js';
|
||||
import { normalizePlatformSource } from '../../shared/platform-source.js';
|
||||
|
||||
export const contextHandler: EventHandler = {
|
||||
async execute(input: NormalizedHookInput): Promise<HookResult> {
|
||||
@@ -31,6 +32,7 @@ export const contextHandler: EventHandler = {
|
||||
const cwd = input.cwd ?? process.cwd();
|
||||
const context = getProjectContext(cwd);
|
||||
const port = getWorkerPort();
|
||||
const platformSource = normalizePlatformSource(input.platform);
|
||||
|
||||
// Check if terminal output should be shown (load settings early)
|
||||
const settings = SettingsDefaultsManager.loadFromFile(USER_SETTINGS_PATH);
|
||||
@@ -38,7 +40,7 @@ export const contextHandler: EventHandler = {
|
||||
|
||||
// Pass all projects (parent + worktree if applicable) for unified timeline
|
||||
const projectsParam = context.allProjects.join(',');
|
||||
const apiPath = `/api/context/inject?projects=${encodeURIComponent(projectsParam)}`;
|
||||
const apiPath = `/api/context/inject?projects=${encodeURIComponent(projectsParam)}&platformSource=${encodeURIComponent(platformSource)}`;
|
||||
const colorApiPath = input.platform === 'claude-code' ? `${apiPath}&colors=true` : apiPath;
|
||||
|
||||
// Note: Removed AbortSignal.timeout due to Windows Bun cleanup issue (libuv assertion)
|
||||
|
||||
@@ -0,0 +1,295 @@
|
||||
/**
|
||||
* File Context Handler - PreToolUse
|
||||
*
|
||||
* Injects relevant observation history when Claude reads/edits a file,
|
||||
* so it can avoid duplicating past work.
|
||||
*/
|
||||
|
||||
import type { EventHandler, NormalizedHookInput, HookResult } from '../types.js';
|
||||
import { ensureWorkerRunning, workerHttpRequest } from '../../shared/worker-utils.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { parseJsonArray } from '../../shared/timeline-formatting.js';
|
||||
import { statSync } from 'fs';
|
||||
import path from 'path';
|
||||
import { isProjectExcluded } from '../../utils/project-filter.js';
|
||||
import { SettingsDefaultsManager } from '../../shared/SettingsDefaultsManager.js';
|
||||
import { USER_SETTINGS_PATH } from '../../shared/paths.js';
|
||||
import { getProjectContext } from '../../utils/project-name.js';
|
||||
|
||||
/** Skip the gate for files smaller than this — timeline overhead exceeds file read cost. */
|
||||
const FILE_READ_GATE_MIN_BYTES = 1_500;
|
||||
|
||||
/** Fetch more candidates than the display limit so dedup still fills 15 slots. */
|
||||
const FETCH_LOOKAHEAD_LIMIT = 40;
|
||||
|
||||
/** Maximum observations to show in the timeline. */
|
||||
const DISPLAY_LIMIT = 15;
|
||||
|
||||
const TYPE_ICONS: Record<string, string> = {
|
||||
decision: '\u2696\uFE0F',
|
||||
bugfix: '\uD83D\uDD34',
|
||||
feature: '\uD83D\uDFE3',
|
||||
refactor: '\uD83D\uDD04',
|
||||
discovery: '\uD83D\uDD35',
|
||||
change: '\u2705',
|
||||
};
|
||||
|
||||
function compactTime(timeStr: string): string {
|
||||
return timeStr.toLowerCase().replace(' am', 'a').replace(' pm', 'p');
|
||||
}
|
||||
|
||||
function formatTime(epoch: number): string {
|
||||
const date = new Date(epoch);
|
||||
return date.toLocaleString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true });
|
||||
}
|
||||
|
||||
function formatDate(epoch: number): string {
|
||||
const date = new Date(epoch);
|
||||
return date.toLocaleString('en-US', { month: 'short', day: 'numeric', year: 'numeric' });
|
||||
}
|
||||
|
||||
interface ObservationRow {
|
||||
id: number;
|
||||
memory_session_id: string;
|
||||
title: string | null;
|
||||
type: string;
|
||||
created_at_epoch: number;
|
||||
files_read: string | null;
|
||||
files_modified: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deduplicate and rank observations for the timeline display.
|
||||
*
|
||||
* 1. Same-session dedup: keep only the most recent observation per session
|
||||
* (input is already sorted newest-first by SQL).
|
||||
* 2. Specificity scoring: rank by how specifically the observation is about
|
||||
* the target file (modified > read-only, fewer total files > many).
|
||||
* 3. Truncate to displayLimit.
|
||||
*/
|
||||
function deduplicateObservations(
|
||||
observations: ObservationRow[],
|
||||
targetPath: string,
|
||||
displayLimit: number
|
||||
): ObservationRow[] {
|
||||
// Phase 1: Keep only the most recent observation per session
|
||||
const seenSessions = new Set<string>();
|
||||
const dedupedBySession: ObservationRow[] = [];
|
||||
for (const obs of observations) {
|
||||
const sessionKey = obs.memory_session_id ?? `no-session-${obs.id}`;
|
||||
if (!seenSessions.has(sessionKey)) {
|
||||
seenSessions.add(sessionKey);
|
||||
dedupedBySession.push(obs);
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: Score by specificity to the target file
|
||||
const scored = dedupedBySession.map(obs => {
|
||||
const filesRead = parseJsonArray(obs.files_read);
|
||||
const filesModified = parseJsonArray(obs.files_modified);
|
||||
const totalFiles = filesRead.length + filesModified.length;
|
||||
const normalizedTarget = targetPath.replace(/\\/g, '/');
|
||||
const inModified = filesModified.some(f => f.replace(/\\/g, '/') === normalizedTarget);
|
||||
|
||||
let specificityScore = 0;
|
||||
if (inModified) specificityScore += 2;
|
||||
if (totalFiles <= 3) specificityScore += 2;
|
||||
else if (totalFiles <= 8) specificityScore += 1;
|
||||
// totalFiles > 8: no bonus (survey-like observation)
|
||||
|
||||
return { obs, specificityScore };
|
||||
});
|
||||
|
||||
// Stable sort: higher specificity first, preserve chronological order within same score
|
||||
scored.sort((a, b) => b.specificityScore - a.specificityScore);
|
||||
|
||||
return scored.slice(0, displayLimit).map(s => s.obs);
|
||||
}
|
||||
|
||||
function formatFileTimeline(
|
||||
observations: ObservationRow[],
|
||||
filePath: string,
|
||||
truncated: boolean
|
||||
): string {
|
||||
// Escape filePath for safe interpolation into recovery hints (quotes, backslashes, newlines)
|
||||
const safePath = filePath.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n');
|
||||
// Group observations by day
|
||||
const byDay = new Map<string, ObservationRow[]>();
|
||||
for (const obs of observations) {
|
||||
const day = formatDate(obs.created_at_epoch);
|
||||
if (!byDay.has(day)) {
|
||||
byDay.set(day, []);
|
||||
}
|
||||
byDay.get(day)!.push(obs);
|
||||
}
|
||||
|
||||
// Sort days chronologically (use earliest observation in each group, not first — which is specificity-sorted)
|
||||
const sortedDays = Array.from(byDay.entries()).sort((a, b) => {
|
||||
const aEpoch = Math.min(...a[1].map(o => o.created_at_epoch));
|
||||
const bEpoch = Math.min(...b[1].map(o => o.created_at_epoch));
|
||||
return aEpoch - bEpoch;
|
||||
});
|
||||
|
||||
// Include current date/time so the model can judge recency of observations
|
||||
const now = new Date();
|
||||
const currentDate = now.toLocaleDateString('en-CA'); // YYYY-MM-DD
|
||||
const currentTime = now.toLocaleTimeString('en-US', {
|
||||
hour: 'numeric',
|
||||
minute: '2-digit',
|
||||
hour12: true
|
||||
}).toLowerCase().replace(' ', '');
|
||||
const currentTimezone = now.toLocaleTimeString('en-US', { timeZoneName: 'short' }).split(' ').pop();
|
||||
|
||||
const headerLine = truncated
|
||||
? `This file has prior observations. Only line 1 was read to save tokens.`
|
||||
: `This file has prior observations. The requested section was read normally.`;
|
||||
|
||||
const lines: string[] = [
|
||||
`Current: ${currentDate} ${currentTime} ${currentTimezone}`,
|
||||
headerLine,
|
||||
`- **Already know enough?** The timeline below may be all you need (semantic priming).`,
|
||||
`- **Need details?** get_observations([IDs]) — ~300 tokens each.`,
|
||||
`- **Need full file?** Read again with offset/limit for the section you need.`,
|
||||
`- **Need to edit?** Edit works — the file is registered as read. Use smart_outline("${safePath}") for line numbers.`,
|
||||
];
|
||||
|
||||
for (const [day, dayObservations] of sortedDays) {
|
||||
// Sort within each day chronologically (deduplicateObservations reorders by specificity)
|
||||
const chronological = [...dayObservations].sort((a, b) => a.created_at_epoch - b.created_at_epoch);
|
||||
lines.push(`### ${day}`);
|
||||
for (const obs of chronological) {
|
||||
const title = (obs.title || 'Untitled').replace(/[\r\n\t]+/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 160);
|
||||
const icon = TYPE_ICONS[obs.type] || '\u2753';
|
||||
const time = compactTime(formatTime(obs.created_at_epoch));
|
||||
lines.push(`${obs.id} ${time} ${icon} ${title}`);
|
||||
}
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
export const fileContextHandler: EventHandler = {
|
||||
async execute(input: NormalizedHookInput): Promise<HookResult> {
|
||||
// Extract file_path from toolInput
|
||||
const toolInput = input.toolInput as Record<string, unknown> | undefined;
|
||||
const filePath = toolInput?.file_path as string | undefined;
|
||||
|
||||
if (!filePath) {
|
||||
return { continue: true, suppressOutput: true };
|
||||
}
|
||||
|
||||
// Preserve user-supplied offset/limit to avoid read-dedup collisions (fixes #1719)
|
||||
const userOffset = typeof toolInput?.offset === 'number' && Number.isFinite(toolInput.offset) && toolInput.offset >= 0
|
||||
? Math.floor(toolInput.offset) : undefined;
|
||||
const userLimit = typeof toolInput?.limit === 'number' && Number.isFinite(toolInput.limit) && toolInput.limit > 0
|
||||
? Math.floor(toolInput.limit) : undefined;
|
||||
const isTargetedRead = userOffset !== undefined || userLimit !== undefined;
|
||||
|
||||
// Stat the file once: size (gate) + mtime (cache invalidation).
|
||||
// 0 = stat failed non-fatally (e.g. EPERM) — skip mtime check, fall through to truncation.
|
||||
let fileMtimeMs = 0;
|
||||
try {
|
||||
const statPath = path.isAbsolute(filePath)
|
||||
? filePath
|
||||
: path.resolve(input.cwd || process.cwd(), filePath);
|
||||
const stat = statSync(statPath);
|
||||
// Skip gate for files below the token-economics threshold — timeline (~370 tokens)
|
||||
// costs more than reading small files directly.
|
||||
if (stat.size < FILE_READ_GATE_MIN_BYTES) {
|
||||
return { continue: true, suppressOutput: true };
|
||||
}
|
||||
fileMtimeMs = stat.mtimeMs;
|
||||
} catch (err: any) {
|
||||
if (err.code === 'ENOENT') return { continue: true, suppressOutput: true };
|
||||
// Other errors (symlink, permission denied) — fall through and let gate proceed
|
||||
}
|
||||
|
||||
// Check if project is excluded from tracking
|
||||
const settings = SettingsDefaultsManager.loadFromFile(USER_SETTINGS_PATH);
|
||||
if (input.cwd && isProjectExcluded(input.cwd, settings.CLAUDE_MEM_EXCLUDED_PROJECTS)) {
|
||||
logger.debug('HOOK', 'Project excluded from tracking, skipping file context', { cwd: input.cwd });
|
||||
return { continue: true, suppressOutput: true };
|
||||
}
|
||||
|
||||
// Ensure worker is running
|
||||
const workerReady = await ensureWorkerRunning();
|
||||
if (!workerReady) {
|
||||
return { continue: true, suppressOutput: true };
|
||||
}
|
||||
|
||||
// Query worker for observations related to this file
|
||||
try {
|
||||
const context = getProjectContext(input.cwd);
|
||||
// Observations store relative paths — convert absolute to relative using cwd
|
||||
const cwd = input.cwd || process.cwd();
|
||||
const absolutePath = path.isAbsolute(filePath) ? filePath : path.resolve(cwd, filePath);
|
||||
const relativePath = path.relative(cwd, absolutePath).split(path.sep).join("/");
|
||||
const queryParams = new URLSearchParams({ path: relativePath });
|
||||
// Pass all project names (parent + worktree) for unified lookup
|
||||
if (context.allProjects.length > 0) {
|
||||
queryParams.set('projects', context.allProjects.join(','));
|
||||
}
|
||||
queryParams.set('limit', String(FETCH_LOOKAHEAD_LIMIT));
|
||||
|
||||
const response = await workerHttpRequest(`/api/observations/by-file?${queryParams.toString()}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
logger.warn('HOOK', 'File context query failed, skipping', { status: response.status, filePath });
|
||||
return { continue: true, suppressOutput: true };
|
||||
}
|
||||
|
||||
const data = await response.json() as { observations: ObservationRow[]; count: number };
|
||||
|
||||
if (!data.observations || data.observations.length === 0) {
|
||||
return { continue: true, suppressOutput: true };
|
||||
}
|
||||
|
||||
// mtime invalidation: bypass truncation when the file is newer than the latest observation.
|
||||
// Uses >= to handle same-millisecond edits (cost: one extra full read vs risk of stuck truncation).
|
||||
if (fileMtimeMs > 0) {
|
||||
const newestObservationMs = Math.max(...data.observations.map(o => o.created_at_epoch));
|
||||
if (fileMtimeMs >= newestObservationMs) {
|
||||
logger.debug('HOOK', 'File modified since last observation, skipping truncation', {
|
||||
filePath: relativePath,
|
||||
fileMtimeMs,
|
||||
newestObservationMs,
|
||||
});
|
||||
return { continue: true, suppressOutput: true };
|
||||
}
|
||||
}
|
||||
|
||||
// Deduplicate: one per session, ranked by specificity to this file
|
||||
const dedupedObservations = deduplicateObservations(data.observations, relativePath, DISPLAY_LIMIT);
|
||||
if (dedupedObservations.length === 0) {
|
||||
return { continue: true, suppressOutput: true };
|
||||
}
|
||||
|
||||
// Unconstrained → truncate to 1 line; targeted → preserve offset/limit.
|
||||
const truncated = !isTargetedRead;
|
||||
const timeline = formatFileTimeline(dedupedObservations, filePath, truncated);
|
||||
const updatedInput: Record<string, unknown> = { file_path: filePath };
|
||||
if (isTargetedRead) {
|
||||
if (userOffset !== undefined) updatedInput.offset = userOffset;
|
||||
if (userLimit !== undefined) updatedInput.limit = userLimit;
|
||||
} else {
|
||||
updatedInput.limit = 1;
|
||||
}
|
||||
|
||||
return {
|
||||
hookSpecificOutput: {
|
||||
hookEventName: 'PreToolUse',
|
||||
additionalContext: timeline,
|
||||
permissionDecision: 'allow',
|
||||
updatedInput,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
logger.warn('HOOK', 'File context fetch error, skipping', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
return { continue: true, suppressOutput: true };
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -9,6 +9,7 @@ import type { EventHandler, NormalizedHookInput, HookResult } from '../types.js'
|
||||
import { ensureWorkerRunning, workerHttpRequest } from '../../shared/worker-utils.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { HOOK_EXIT_CODES } from '../../shared/hook-constants.js';
|
||||
import { normalizePlatformSource } from '../../shared/platform-source.js';
|
||||
|
||||
export const fileEditHandler: EventHandler = {
|
||||
async execute(input: NormalizedHookInput): Promise<HookResult> {
|
||||
@@ -20,6 +21,7 @@ export const fileEditHandler: EventHandler = {
|
||||
}
|
||||
|
||||
const { sessionId, cwd, filePath, edits } = input;
|
||||
const platformSource = normalizePlatformSource(input.platform);
|
||||
|
||||
if (!filePath) {
|
||||
throw new Error('fileEditHandler requires filePath');
|
||||
@@ -42,6 +44,7 @@ export const fileEditHandler: EventHandler = {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
contentSessionId: sessionId,
|
||||
platformSource,
|
||||
tool_name: 'write_file',
|
||||
tool_input: { filePath, edits },
|
||||
tool_response: { success: true },
|
||||
|
||||
@@ -13,6 +13,7 @@ import { observationHandler } from './observation.js';
|
||||
import { summarizeHandler } from './summarize.js';
|
||||
import { userMessageHandler } from './user-message.js';
|
||||
import { fileEditHandler } from './file-edit.js';
|
||||
import { fileContextHandler } from './file-context.js';
|
||||
import { sessionCompleteHandler } from './session-complete.js';
|
||||
|
||||
export type EventType =
|
||||
@@ -22,7 +23,8 @@ export type EventType =
|
||||
| 'summarize' // Stop - generate summary (phase 1)
|
||||
| 'session-complete' // Stop - complete session (phase 2) - fixes #842
|
||||
| 'user-message' // SessionStart (parallel) - display to user
|
||||
| 'file-edit'; // Cursor afterFileEdit
|
||||
| 'file-edit' // Cursor afterFileEdit
|
||||
| 'file-context'; // PreToolUse - inject file observation history
|
||||
|
||||
const handlers: Record<EventType, EventHandler> = {
|
||||
'context': contextHandler,
|
||||
@@ -31,7 +33,8 @@ const handlers: Record<EventType, EventHandler> = {
|
||||
'summarize': summarizeHandler,
|
||||
'session-complete': sessionCompleteHandler,
|
||||
'user-message': userMessageHandler,
|
||||
'file-edit': fileEditHandler
|
||||
'file-edit': fileEditHandler,
|
||||
'file-context': fileContextHandler
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -64,4 +67,5 @@ export { observationHandler } from './observation.js';
|
||||
export { summarizeHandler } from './summarize.js';
|
||||
export { userMessageHandler } from './user-message.js';
|
||||
export { fileEditHandler } from './file-edit.js';
|
||||
export { fileContextHandler } from './file-context.js';
|
||||
export { sessionCompleteHandler } from './session-complete.js';
|
||||
|
||||
@@ -11,6 +11,7 @@ import { HOOK_EXIT_CODES } from '../../shared/hook-constants.js';
|
||||
import { isProjectExcluded } from '../../utils/project-filter.js';
|
||||
import { SettingsDefaultsManager } from '../../shared/SettingsDefaultsManager.js';
|
||||
import { USER_SETTINGS_PATH } from '../../shared/paths.js';
|
||||
import { normalizePlatformSource } from '../../shared/platform-source.js';
|
||||
|
||||
export const observationHandler: EventHandler = {
|
||||
async execute(input: NormalizedHookInput): Promise<HookResult> {
|
||||
@@ -22,6 +23,7 @@ export const observationHandler: EventHandler = {
|
||||
}
|
||||
|
||||
const { sessionId, cwd, toolName, toolInput, toolResponse } = input;
|
||||
const platformSource = normalizePlatformSource(input.platform);
|
||||
|
||||
if (!toolName) {
|
||||
// No tool name provided - skip observation gracefully
|
||||
@@ -51,6 +53,7 @@ export const observationHandler: EventHandler = {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
contentSessionId: sessionId,
|
||||
platformSource,
|
||||
tool_name: toolName,
|
||||
tool_input: toolInput,
|
||||
tool_response: toolResponse,
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
import type { EventHandler, NormalizedHookInput, HookResult } from '../types.js';
|
||||
import { ensureWorkerRunning, workerHttpRequest } from '../../shared/worker-utils.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { normalizePlatformSource } from '../../shared/platform-source.js';
|
||||
|
||||
export const sessionCompleteHandler: EventHandler = {
|
||||
async execute(input: NormalizedHookInput): Promise<HookResult> {
|
||||
@@ -23,6 +24,7 @@ export const sessionCompleteHandler: EventHandler = {
|
||||
}
|
||||
|
||||
const { sessionId } = input;
|
||||
const platformSource = normalizePlatformSource(input.platform);
|
||||
|
||||
if (!sessionId) {
|
||||
logger.warn('HOOK', 'session-complete: Missing sessionId, skipping');
|
||||
@@ -39,7 +41,8 @@ export const sessionCompleteHandler: EventHandler = {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
contentSessionId: sessionId
|
||||
contentSessionId: sessionId,
|
||||
platformSource
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
@@ -6,12 +6,13 @@
|
||||
|
||||
import type { EventHandler, NormalizedHookInput, HookResult } from '../types.js';
|
||||
import { ensureWorkerRunning, workerHttpRequest } from '../../shared/worker-utils.js';
|
||||
import { getProjectName } from '../../utils/project-name.js';
|
||||
import { getProjectContext } from '../../utils/project-name.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { HOOK_EXIT_CODES } from '../../shared/hook-constants.js';
|
||||
import { isProjectExcluded } from '../../utils/project-filter.js';
|
||||
import { SettingsDefaultsManager } from '../../shared/SettingsDefaultsManager.js';
|
||||
import { USER_SETTINGS_PATH } from '../../shared/paths.js';
|
||||
import { normalizePlatformSource } from '../../shared/platform-source.js';
|
||||
|
||||
export const sessionInitHandler: EventHandler = {
|
||||
async execute(input: NormalizedHookInput): Promise<HookResult> {
|
||||
@@ -41,7 +42,8 @@ export const sessionInitHandler: EventHandler = {
|
||||
// Use placeholder so sessions still get created and tracked for memory
|
||||
const prompt = (!rawPrompt || !rawPrompt.trim()) ? '[media prompt]' : rawPrompt;
|
||||
|
||||
const project = getProjectName(cwd);
|
||||
const project = getProjectContext(cwd).primary;
|
||||
const platformSource = normalizePlatformSource(input.platform);
|
||||
|
||||
logger.debug('HOOK', 'session-init: Calling /api/sessions/init', { contentSessionId: sessionId, project });
|
||||
|
||||
@@ -52,7 +54,8 @@ export const sessionInitHandler: EventHandler = {
|
||||
body: JSON.stringify({
|
||||
contentSessionId: sessionId,
|
||||
project,
|
||||
prompt
|
||||
prompt,
|
||||
platformSource
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import { ensureWorkerRunning, workerHttpRequest } from '../../shared/worker-util
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { extractLastMessage } from '../../shared/transcript-parser.js';
|
||||
import { HOOK_EXIT_CODES, HOOK_TIMEOUTS, getTimeout } from '../../shared/hook-constants.js';
|
||||
import { normalizePlatformSource } from '../../shared/platform-source.js';
|
||||
|
||||
const SUMMARIZE_TIMEOUT_MS = getTimeout(HOOK_TIMEOUTS.DEFAULT);
|
||||
const POLL_INTERVAL_MS = 500;
|
||||
@@ -66,13 +67,16 @@ export const summarizeHandler: EventHandler = {
|
||||
hasLastAssistantMessage: !!lastAssistantMessage
|
||||
});
|
||||
|
||||
const platformSource = normalizePlatformSource(input.platform);
|
||||
|
||||
// 1. Queue summarize request — worker returns immediately with { status: 'queued' }
|
||||
const response = await workerHttpRequest('/api/sessions/summarize', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
contentSessionId: sessionId,
|
||||
last_assistant_message: lastAssistantMessage
|
||||
last_assistant_message: lastAssistantMessage,
|
||||
platformSource
|
||||
}),
|
||||
timeoutMs: SUMMARIZE_TIMEOUT_MS
|
||||
});
|
||||
@@ -87,20 +91,32 @@ export const summarizeHandler: EventHandler = {
|
||||
// This keeps the Stop hook alive (120s timeout) so the SDK agent
|
||||
// can finish processing the summary before SessionEnd kills the session.
|
||||
const waitStart = Date.now();
|
||||
let summaryStored: boolean | null = null;
|
||||
while ((Date.now() - waitStart) < MAX_WAIT_FOR_SUMMARY_MS) {
|
||||
await new Promise(resolve => setTimeout(resolve, POLL_INTERVAL_MS));
|
||||
try {
|
||||
const statusResponse = await workerHttpRequest(`/api/sessions/status?contentSessionId=${encodeURIComponent(sessionId)}`, {
|
||||
timeoutMs: 5000
|
||||
});
|
||||
if (statusResponse.ok) {
|
||||
const status = await statusResponse.json() as { queueLength?: number };
|
||||
if ((status.queueLength ?? 0) === 0) {
|
||||
logger.info('HOOK', 'Summary processing complete', {
|
||||
const status = await statusResponse.json() as { queueLength?: number; summaryStored?: boolean | null };
|
||||
const queueLength = status.queueLength ?? 0;
|
||||
// Only treat an empty queue as completion when the session exists (non-404).
|
||||
// A 404 means the session was not found — not that processing finished.
|
||||
if (queueLength === 0 && statusResponse.status !== 404) {
|
||||
summaryStored = status.summaryStored ?? null;
|
||||
logger.info('HOOK', 'Summary processing complete', {
|
||||
waitedMs: Date.now() - waitStart,
|
||||
summaryStored
|
||||
});
|
||||
// Warn when the agent processed a summarize request but produced no storable summary.
|
||||
// This is the silent-failure path described in #1633: queue empties but no summary record exists.
|
||||
if (summaryStored === false) {
|
||||
logger.warn('HOOK', 'Summary was not stored: LLM response likely lacked valid <summary> tags (#1633)', {
|
||||
sessionId,
|
||||
waitedMs: Date.now() - waitStart
|
||||
});
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
} catch {
|
||||
// Worker may be busy — keep polling
|
||||
|
||||
+7
-1
@@ -17,7 +17,13 @@ export interface NormalizedHookInput {
|
||||
export interface HookResult {
|
||||
continue?: boolean;
|
||||
suppressOutput?: boolean;
|
||||
hookSpecificOutput?: { hookEventName: string; additionalContext: string };
|
||||
hookSpecificOutput?: {
|
||||
hookEventName: string;
|
||||
additionalContext: string;
|
||||
permissionDecision?: 'allow' | 'deny';
|
||||
permissionDecisionReason?: string;
|
||||
updatedInput?: Record<string, unknown>;
|
||||
};
|
||||
systemMessage?: string;
|
||||
exitCode?: number;
|
||||
}
|
||||
|
||||
@@ -116,7 +116,8 @@ export function detectInstalledIDEs(): IDEInfo[] {
|
||||
id: 'cursor',
|
||||
label: 'Cursor',
|
||||
detected: existsSync(join(home, '.cursor')),
|
||||
supported: false,
|
||||
supported: true,
|
||||
hint: 'hooks + MCP integration',
|
||||
},
|
||||
{
|
||||
id: 'copilot-cli',
|
||||
|
||||
@@ -135,9 +135,22 @@ async function setupIDEs(selectedIDEs: string[]): Promise<string[]> {
|
||||
break;
|
||||
}
|
||||
|
||||
case 'cursor':
|
||||
log.warn('Cursor: integration not yet implemented. Skipping.');
|
||||
case 'cursor': {
|
||||
const { installCursorHooks, configureCursorMcp } = await import('../../services/integrations/CursorHooksInstaller.js');
|
||||
const cursorResult = await installCursorHooks('user');
|
||||
if (cursorResult === 0) {
|
||||
const mcpResult = configureCursorMcp('user');
|
||||
if (mcpResult === 0) {
|
||||
log.success('Cursor: hooks + MCP installed.');
|
||||
} else {
|
||||
log.success('Cursor: hooks installed (MCP setup failed — run `npx claude-mem cursor mcp` to retry).');
|
||||
}
|
||||
} else {
|
||||
log.error('Cursor: hook installation failed.');
|
||||
failedIDEs.push(ideId);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'gemini-cli': {
|
||||
const { installGeminiCliHooks } = await import('../../services/integrations/GeminiCliHooksInstaller.js');
|
||||
|
||||
@@ -102,7 +102,7 @@ export function runStatusCommand(): void {
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the worker API at `GET /api/search?q=<query>`.
|
||||
* Search the worker API at `GET /api/search?query=<query>`.
|
||||
*/
|
||||
export async function runSearchCommand(queryParts: string[]): Promise<void> {
|
||||
ensureInstalledOrExit();
|
||||
@@ -114,7 +114,7 @@ export async function runSearchCommand(queryParts: string[]): Promise<void> {
|
||||
}
|
||||
|
||||
const workerPort = process.env.CLAUDE_MEM_WORKER_PORT || '37777';
|
||||
const searchUrl = `http://127.0.0.1:${workerPort}/api/search?q=${encodeURIComponent(query)}`;
|
||||
const searchUrl = `http://127.0.0.1:${workerPort}/api/search?query=${encodeURIComponent(query)}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(searchUrl);
|
||||
|
||||
+25
-4
@@ -50,9 +50,8 @@ export function parseObservations(text: string, correlationId?: string): ParsedO
|
||||
const files_read = extractArrayElements(obsContent, 'files_read', 'file');
|
||||
const files_modified = extractArrayElements(obsContent, 'files_modified', 'file');
|
||||
|
||||
// NOTE FROM THEDOTMACK: ALWAYS save observations - never skip. 10/24/2025
|
||||
// All fields except type are nullable in schema
|
||||
// If type is missing or invalid, use first type from mode as fallback
|
||||
// All fields except type are nullable in schema.
|
||||
// If type is missing or invalid, use first type from mode as fallback.
|
||||
|
||||
// Determine final type using active mode's valid types
|
||||
const mode = ModeManager.getInstance().getActiveMode();
|
||||
@@ -83,6 +82,19 @@ export function parseObservations(text: string, correlationId?: string): ParsedO
|
||||
});
|
||||
}
|
||||
|
||||
// Skip ghost observations — records where every content field is null/empty.
|
||||
// These accumulate when the LLM emits a bare <observation/> (or one with only <type>)
|
||||
// due to context overflow. They carry no information and pollute the context window.
|
||||
// (subtitle and file lists are intentionally excluded from this guard: an observation
|
||||
// with only a subtitle is still too thin to be useful on its own.)
|
||||
if (!title && !narrative && facts.length === 0 && cleanedConcepts.length === 0) {
|
||||
logger.warn('PARSER', 'Skipping empty observation (all content fields null)', {
|
||||
correlationId,
|
||||
type: finalType
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
observations.push({
|
||||
type: finalType,
|
||||
title,
|
||||
@@ -138,7 +150,7 @@ export function parseSummary(text: string, sessionId?: number): ParsedSummary |
|
||||
const next_steps = extractField(summaryContent, 'next_steps');
|
||||
const notes = extractField(summaryContent, 'notes'); // Optional
|
||||
|
||||
// NOTE FROM THEDOTMACK: 100% of the time we must SAVE the summary, even if fields are missing. 10/24/2025
|
||||
// NOTE FROM THEDOTMACK: 100% of the time we must SAVE the summary, even if fields are missing. 10/24/2025
|
||||
// NEVER DO THIS NONSENSE AGAIN.
|
||||
|
||||
// Validate required fields are present (notes is optional)
|
||||
@@ -154,6 +166,15 @@ export function parseSummary(text: string, sessionId?: number): ParsedSummary |
|
||||
// return null;
|
||||
// }
|
||||
|
||||
// Guard: if NO sub-tags matched at all, this is a false positive —
|
||||
// <summary> accidentally appeared inside an <observation> response with no structured content.
|
||||
// This is NOT the same as missing some fields (which we intentionally allow above).
|
||||
// Fix for #1360.
|
||||
if (!request && !investigated && !learned && !completed && !next_steps) {
|
||||
logger.warn('PARSER', 'Summary match has no sub-tags — skipping false positive', { sessionId });
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
request,
|
||||
investigated,
|
||||
|
||||
+254
-8
@@ -16,7 +16,6 @@ import { logger } from '../utils/logger.js';
|
||||
// CRITICAL: Redirect console to stderr BEFORE other imports
|
||||
// MCP uses stdio transport where stdout is reserved for JSON-RPC protocol messages.
|
||||
// Any logs to stdout break the protocol (Claude Desktop parses "[2025..." as JSON array).
|
||||
const _originalLog = console['log'];
|
||||
console['log'] = (...args: any[]) => {
|
||||
logger.error('CONSOLE', 'Intercepted console output (MCP protocol protection)', undefined, { args });
|
||||
};
|
||||
@@ -27,11 +26,70 @@ import {
|
||||
CallToolRequestSchema,
|
||||
ListToolsRequestSchema,
|
||||
} from '@modelcontextprotocol/sdk/types.js';
|
||||
import { workerHttpRequest } from '../shared/worker-utils.js';
|
||||
import { getWorkerPort, workerHttpRequest } from '../shared/worker-utils.js';
|
||||
import { ensureWorkerStarted } from '../services/worker-spawner.js';
|
||||
import { searchCodebase, formatSearchResults } from '../services/smart-file-read/search.js';
|
||||
import { parseFile, formatFoldedView, unfoldSymbol } from '../services/smart-file-read/parser.js';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { resolve } from 'node:path';
|
||||
import { existsSync } from 'node:fs';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
// Resolve the path to worker-service.cjs, which lives alongside mcp-server.cjs
|
||||
// in the plugin's scripts directory. We need an explicit path because the MCP
|
||||
// server runs under Node while the worker must run under Bun, so we can't rely
|
||||
// on `__filename` pointing to a self-spawnable script.
|
||||
//
|
||||
// In the deployed CJS bundle, `__dirname` is always defined — the import.meta
|
||||
// fallback only exists to keep the source future-proof against an eventual
|
||||
// ESM port. Both fallback branches should be functionally unreachable today.
|
||||
let mcpServerDirResolutionFailed = false;
|
||||
const mcpServerDir = (() => {
|
||||
if (typeof __dirname !== 'undefined') return __dirname;
|
||||
try {
|
||||
return dirname(fileURLToPath(import.meta.url));
|
||||
} catch {
|
||||
// Last-ditch fallback: cwd is almost certainly wrong, but throwing here
|
||||
// would crash the MCP server before it can serve a single request. Mark
|
||||
// the failure so the existence check below can produce a single, loud,
|
||||
// root-cause-attributing log line instead of a confusing "missing worker
|
||||
// bundle" warning that hides the dirname resolution failure.
|
||||
mcpServerDirResolutionFailed = true;
|
||||
return process.cwd();
|
||||
}
|
||||
})();
|
||||
const WORKER_SCRIPT_PATH = resolve(mcpServerDir, 'worker-service.cjs');
|
||||
|
||||
/**
|
||||
* Surface a clear, actionable error if the worker bundle isn't where we
|
||||
* expect. Without this check, a missing or partial install only fails later
|
||||
* inside spawnDaemon as a generic "failed to spawn" message.
|
||||
*
|
||||
* If dirname resolution itself failed (extremely unlikely in CJS), attribute
|
||||
* the missing-bundle warning to the root cause so the user doesn't waste time
|
||||
* looking for an install bug that doesn't exist.
|
||||
*
|
||||
* Called lazily from `ensureWorkerConnection` (not at module load) so that
|
||||
* tests or tools that import this module without booting the MCP server
|
||||
* don't see noisy ERROR-level log lines for a worker they never intended
|
||||
* to start. The check is cheap and idempotent, so calling it on every
|
||||
* auto-start attempt is fine.
|
||||
*/
|
||||
function errorIfWorkerScriptMissing(): void {
|
||||
// Only log here when the dirname resolution itself failed — that's the
|
||||
// mcp-server-specific root cause attribution that the spawner cannot
|
||||
// provide. The plain "missing bundle" case is already covered by the
|
||||
// existsSync guard inside ensureWorkerStarted, and logging from both
|
||||
// sites would produce a confusing double-log on the same code path.
|
||||
if (!mcpServerDirResolutionFailed) return;
|
||||
if (existsSync(WORKER_SCRIPT_PATH)) return;
|
||||
|
||||
logger.error(
|
||||
'SYSTEM',
|
||||
'mcp-server: dirname resolution failed (both __dirname and import.meta.url are unavailable). Fell back to process.cwd() and the resolved WORKER_SCRIPT_PATH does not exist. This is the actual problem — the worker bundle is fine, but mcp-server cannot locate it. Worker auto-start will fail until the dirname-resolution path is fixed.',
|
||||
{ workerScriptPath: WORKER_SCRIPT_PATH, mcpServerDir }
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map tool names to Worker HTTP endpoints
|
||||
@@ -144,6 +202,44 @@ async function verifyWorkerConnection(): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure Worker is available for Codex and other MCP-only clients.
|
||||
* Claude hooks already start the worker; this path makes Codex turnkey.
|
||||
*/
|
||||
async function ensureWorkerConnection(): Promise<boolean> {
|
||||
if (await verifyWorkerConnection()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
logger.warn('SYSTEM', 'Worker not available, attempting auto-start for MCP client');
|
||||
|
||||
// Validate the worker bundle path lazily here (rather than at module load)
|
||||
// so that tests/tools that import this module without booting the MCP
|
||||
// server don't see noisy ERROR-level log lines for a worker they never
|
||||
// intended to start.
|
||||
errorIfWorkerScriptMissing();
|
||||
|
||||
try {
|
||||
const port = getWorkerPort();
|
||||
const started = await ensureWorkerStarted(port, WORKER_SCRIPT_PATH);
|
||||
if (!started) {
|
||||
logger.error(
|
||||
'SYSTEM',
|
||||
'Worker auto-start returned false — MCP tools that require the worker (search, timeline, get_observations) will fail until the worker is running. Check earlier log lines for the specific failure reason (Bun not found, missing worker bundle, port conflict, etc.).'
|
||||
);
|
||||
}
|
||||
return started;
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
'SYSTEM',
|
||||
'Worker auto-start threw — MCP tools that require the worker (search, timeline, get_observations) will fail until the worker is running.',
|
||||
undefined,
|
||||
error as Error
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tool definitions with HTTP-based handlers
|
||||
* Minimal descriptions - use help() tool with operation parameter for detailed docs
|
||||
@@ -188,7 +284,17 @@ NEVER fetch full details without filtering first. 10x token savings.`,
|
||||
description: 'Step 1: Search memory. Returns index with IDs. Params: query, limit, project, type, obs_type, dateStart, dateEnd, offset, orderBy',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {},
|
||||
properties: {
|
||||
query: { type: 'string', description: 'Search query' },
|
||||
limit: { type: 'number', description: 'Max results (default 20)' },
|
||||
project: { type: 'string', description: 'Filter by project name' },
|
||||
type: { type: 'string', description: 'Filter by observation type' },
|
||||
obs_type: { type: 'string', description: 'Filter by obs_type field' },
|
||||
dateStart: { type: 'string', description: 'Start date filter (ISO)' },
|
||||
dateEnd: { type: 'string', description: 'End date filter (ISO)' },
|
||||
offset: { type: 'number', description: 'Pagination offset' },
|
||||
orderBy: { type: 'string', description: 'Sort order: date_desc or date_asc' }
|
||||
},
|
||||
additionalProperties: true
|
||||
},
|
||||
handler: async (args: any) => {
|
||||
@@ -201,7 +307,13 @@ NEVER fetch full details without filtering first. 10x token savings.`,
|
||||
description: 'Step 2: Get context around results. Params: anchor (observation ID) OR query (finds anchor automatically), depth_before, depth_after, project',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {},
|
||||
properties: {
|
||||
anchor: { type: 'number', description: 'Observation ID to center the timeline around' },
|
||||
query: { type: 'string', description: 'Query to find anchor automatically' },
|
||||
depth_before: { type: 'number', description: 'Items before anchor (default 3)' },
|
||||
depth_after: { type: 'number', description: 'Items after anchor (default 3)' },
|
||||
project: { type: 'string', description: 'Filter by project name' }
|
||||
},
|
||||
additionalProperties: true
|
||||
},
|
||||
handler: async (args: any) => {
|
||||
@@ -339,6 +451,111 @@ NEVER fetch full details without filtering first. 10x token savings.`,
|
||||
}]
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'build_corpus',
|
||||
description: 'Build a knowledge corpus from filtered observations. Creates a queryable knowledge agent. Params: name (required), description, project, types (comma-separated), concepts (comma-separated), files (comma-separated), query, dateStart, dateEnd, limit',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Corpus name (used as filename)' },
|
||||
description: { type: 'string', description: 'What this corpus is about' },
|
||||
project: { type: 'string', description: 'Filter by project' },
|
||||
types: { type: 'string', description: 'Comma-separated observation types: decision,bugfix,feature,refactor,discovery,change' },
|
||||
concepts: { type: 'string', description: 'Comma-separated concepts to filter by' },
|
||||
files: { type: 'string', description: 'Comma-separated file paths to filter by' },
|
||||
query: { type: 'string', description: 'Semantic search query' },
|
||||
dateStart: { type: 'string', description: 'Start date (ISO format)' },
|
||||
dateEnd: { type: 'string', description: 'End date (ISO format)' },
|
||||
limit: { type: 'number', description: 'Maximum observations (default 500)' }
|
||||
},
|
||||
required: ['name'],
|
||||
additionalProperties: true
|
||||
},
|
||||
handler: async (args: any) => {
|
||||
return await callWorkerAPIPost('/api/corpus', args);
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'list_corpora',
|
||||
description: 'List all knowledge corpora with their stats and priming status',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {},
|
||||
additionalProperties: true
|
||||
},
|
||||
handler: async (args: any) => {
|
||||
return await callWorkerAPI('/api/corpus', args);
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'prime_corpus',
|
||||
description: 'Prime a knowledge corpus — creates an AI session loaded with the corpus knowledge. Must be called before query_corpus.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Name of the corpus to prime' }
|
||||
},
|
||||
required: ['name'],
|
||||
additionalProperties: true
|
||||
},
|
||||
handler: async (args: any) => {
|
||||
const { name, ...rest } = args;
|
||||
if (typeof name !== 'string' || name.trim() === '') throw new Error('Missing required argument: name');
|
||||
return await callWorkerAPIPost(`/api/corpus/${encodeURIComponent(name)}/prime`, rest);
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'query_corpus',
|
||||
description: 'Ask a question to a primed knowledge corpus. The corpus must be primed first with prime_corpus.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Name of the corpus to query' },
|
||||
question: { type: 'string', description: 'The question to ask' }
|
||||
},
|
||||
required: ['name', 'question'],
|
||||
additionalProperties: true
|
||||
},
|
||||
handler: async (args: any) => {
|
||||
const { name, ...rest } = args;
|
||||
if (typeof name !== 'string' || name.trim() === '') throw new Error('Missing required argument: name');
|
||||
return await callWorkerAPIPost(`/api/corpus/${encodeURIComponent(name)}/query`, rest);
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'rebuild_corpus',
|
||||
description: 'Rebuild a knowledge corpus from its stored filter — re-runs the search to refresh with new observations. Does not re-prime the session.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Name of the corpus to rebuild' }
|
||||
},
|
||||
required: ['name'],
|
||||
additionalProperties: true
|
||||
},
|
||||
handler: async (args: any) => {
|
||||
const { name, ...rest } = args;
|
||||
if (typeof name !== 'string' || name.trim() === '') throw new Error('Missing required argument: name');
|
||||
return await callWorkerAPIPost(`/api/corpus/${encodeURIComponent(name)}/rebuild`, rest);
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'reprime_corpus',
|
||||
description: 'Create a fresh knowledge agent session for a corpus, clearing prior Q&A context. Use when conversation has drifted or after rebuilding.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Name of the corpus to reprime' }
|
||||
},
|
||||
required: ['name'],
|
||||
additionalProperties: true
|
||||
},
|
||||
handler: async (args: any) => {
|
||||
const { name, ...rest } = args;
|
||||
if (typeof name !== 'string' || name.trim() === '') throw new Error('Missing required argument: name');
|
||||
return await callWorkerAPIPost(`/api/corpus/${encodeURIComponent(name)}/reprime`, rest);
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
@@ -392,6 +609,30 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||
// Prevents orphaned MCP server processes when Claude Code exits unexpectedly
|
||||
const HEARTBEAT_INTERVAL_MS = 30_000;
|
||||
let heartbeatTimer: ReturnType<typeof setInterval> | null = null;
|
||||
let isCleaningUp = false;
|
||||
|
||||
function handleStdioClosed() {
|
||||
cleanup('stdio-closed');
|
||||
}
|
||||
|
||||
function handleStdioError(error: Error) {
|
||||
logger.warn('SYSTEM', 'MCP stdio stream errored, shutting down', {
|
||||
message: error.message
|
||||
});
|
||||
cleanup('stdio-error');
|
||||
}
|
||||
|
||||
function attachStdioLifecycle() {
|
||||
process.stdin.on('end', handleStdioClosed);
|
||||
process.stdin.on('close', handleStdioClosed);
|
||||
process.stdin.on('error', handleStdioError);
|
||||
}
|
||||
|
||||
function detachStdioLifecycle() {
|
||||
process.stdin.off('end', handleStdioClosed);
|
||||
process.stdin.off('close', handleStdioClosed);
|
||||
process.stdin.off('error', handleStdioError);
|
||||
}
|
||||
|
||||
function startParentHeartbeat() {
|
||||
// ppid-based orphan detection only works on Unix
|
||||
@@ -414,9 +655,13 @@ function startParentHeartbeat() {
|
||||
|
||||
// Cleanup function — synchronous to ensure consistent behavior whether called
|
||||
// from signal handlers, heartbeat interval, or awaited in async context
|
||||
function cleanup() {
|
||||
function cleanup(reason: string = 'shutdown') {
|
||||
if (isCleaningUp) return;
|
||||
isCleaningUp = true;
|
||||
|
||||
if (heartbeatTimer) clearInterval(heartbeatTimer);
|
||||
logger.info('SYSTEM', 'MCP server shutting down');
|
||||
detachStdioLifecycle();
|
||||
logger.info('SYSTEM', 'MCP server shutting down', { reason });
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
@@ -428,6 +673,7 @@ process.on('SIGINT', cleanup);
|
||||
async function main() {
|
||||
// Start the MCP server
|
||||
const transport = new StdioServerTransport();
|
||||
attachStdioLifecycle();
|
||||
await server.connect(transport);
|
||||
logger.info('SYSTEM', 'Claude-mem search server started');
|
||||
|
||||
@@ -436,7 +682,7 @@ async function main() {
|
||||
|
||||
// Check Worker availability in background
|
||||
setTimeout(async () => {
|
||||
const workerAvailable = await verifyWorkerConnection();
|
||||
const workerAvailable = await ensureWorkerConnection();
|
||||
if (!workerAvailable) {
|
||||
logger.error('SYSTEM', 'Worker not available', undefined, {});
|
||||
logger.error('SYSTEM', 'Tools will fail until Worker is started');
|
||||
|
||||
@@ -10,7 +10,7 @@ import { homedir } from 'os';
|
||||
import { unlinkSync } from 'fs';
|
||||
import { SessionStore } from '../sqlite/SessionStore.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { getProjectName } from '../../utils/project-name.js';
|
||||
import { getProjectContext } from '../../utils/project-name.js';
|
||||
|
||||
import type { ContextInput, ContextConfig, Observation, SessionSummary } from './types.js';
|
||||
import { loadContextConfig } from './ContextConfigLoader.js';
|
||||
@@ -29,8 +29,8 @@ import { renderHeader } from './sections/HeaderRenderer.js';
|
||||
import { renderTimeline } from './sections/TimelineRenderer.js';
|
||||
import { shouldShowSummary, renderSummaryFields } from './sections/SummaryRenderer.js';
|
||||
import { renderPreviouslySection, renderFooter } from './sections/FooterRenderer.js';
|
||||
import { renderMarkdownEmptyState } from './formatters/MarkdownFormatter.js';
|
||||
import { renderColorEmptyState } from './formatters/ColorFormatter.js';
|
||||
import { renderAgentEmptyState } from './formatters/AgentFormatter.js';
|
||||
import { renderHumanEmptyState } from './formatters/HumanFormatter.js';
|
||||
|
||||
// Version marker path for native module error handling
|
||||
const VERSION_MARKER_PATH = path.join(
|
||||
@@ -66,8 +66,8 @@ function initializeDatabase(): SessionStore | null {
|
||||
/**
|
||||
* Render empty state when no data exists
|
||||
*/
|
||||
function renderEmptyState(project: string, useColors: boolean): string {
|
||||
return useColors ? renderColorEmptyState(project) : renderMarkdownEmptyState(project);
|
||||
function renderEmptyState(project: string, forHuman: boolean): string {
|
||||
return forHuman ? renderHumanEmptyState(project) : renderAgentEmptyState(project);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +80,7 @@ function buildContextOutput(
|
||||
config: ContextConfig,
|
||||
cwd: string,
|
||||
sessionId: string | undefined,
|
||||
useColors: boolean
|
||||
forHuman: boolean
|
||||
): string {
|
||||
const output: string[] = [];
|
||||
|
||||
@@ -88,7 +88,7 @@ function buildContextOutput(
|
||||
const economics = calculateTokenEconomics(observations);
|
||||
|
||||
// Render header section
|
||||
output.push(...renderHeader(project, economics, config, useColors));
|
||||
output.push(...renderHeader(project, economics, config, forHuman));
|
||||
|
||||
// Prepare timeline data
|
||||
const displaySummaries = summaries.slice(0, config.sessionCount);
|
||||
@@ -97,22 +97,22 @@ function buildContextOutput(
|
||||
const fullObservationIds = getFullObservationIds(observations, config.fullObservationCount);
|
||||
|
||||
// Render timeline
|
||||
output.push(...renderTimeline(timeline, fullObservationIds, config, cwd, useColors));
|
||||
output.push(...renderTimeline(timeline, fullObservationIds, config, cwd, forHuman));
|
||||
|
||||
// Render most recent summary if applicable
|
||||
const mostRecentSummary = summaries[0];
|
||||
const mostRecentObservation = observations[0];
|
||||
|
||||
if (shouldShowSummary(config, mostRecentSummary, mostRecentObservation)) {
|
||||
output.push(...renderSummaryFields(mostRecentSummary, useColors));
|
||||
output.push(...renderSummaryFields(mostRecentSummary, forHuman));
|
||||
}
|
||||
|
||||
// Render previously section (prior assistant message)
|
||||
const priorMessages = getPriorSessionMessages(observations, config, sessionId, cwd);
|
||||
output.push(...renderPreviouslySection(priorMessages, useColors));
|
||||
output.push(...renderPreviouslySection(priorMessages, forHuman));
|
||||
|
||||
// Render footer
|
||||
output.push(...renderFooter(economics, config, useColors));
|
||||
output.push(...renderFooter(economics, config, forHuman));
|
||||
|
||||
return output.join('\n').trimEnd();
|
||||
}
|
||||
@@ -125,14 +125,16 @@ function buildContextOutput(
|
||||
*/
|
||||
export async function generateContext(
|
||||
input?: ContextInput,
|
||||
useColors: boolean = false
|
||||
forHuman: boolean = false
|
||||
): Promise<string> {
|
||||
const config = loadContextConfig();
|
||||
const cwd = input?.cwd ?? process.cwd();
|
||||
const project = getProjectName(cwd);
|
||||
const context = getProjectContext(cwd);
|
||||
const project = context.primary;
|
||||
const platformSource = input?.platform_source;
|
||||
|
||||
// Use provided projects array (for worktree support) or fall back to single project
|
||||
const projects = input?.projects || [project];
|
||||
// Use provided projects array (for worktree support) or fall back to all known projects
|
||||
const projects = input?.projects ?? context.allProjects;
|
||||
|
||||
// Full mode: fetch all observations but keep normal rendering (level 1 summaries)
|
||||
if (input?.full) {
|
||||
@@ -149,15 +151,15 @@ export async function generateContext(
|
||||
try {
|
||||
// Query data for all projects (supports worktree: parent + worktree combined)
|
||||
const observations = projects.length > 1
|
||||
? queryObservationsMulti(db, projects, config)
|
||||
: queryObservations(db, project, config);
|
||||
? queryObservationsMulti(db, projects, config, platformSource)
|
||||
: queryObservations(db, project, config, platformSource);
|
||||
const summaries = projects.length > 1
|
||||
? querySummariesMulti(db, projects, config)
|
||||
: querySummaries(db, project, config);
|
||||
? querySummariesMulti(db, projects, config, platformSource)
|
||||
: querySummaries(db, project, config, platformSource);
|
||||
|
||||
// Handle empty state
|
||||
if (observations.length === 0 && summaries.length === 0) {
|
||||
return renderEmptyState(project, useColors);
|
||||
return renderEmptyState(project, forHuman);
|
||||
}
|
||||
|
||||
// Build and return context
|
||||
@@ -168,7 +170,7 @@ export async function generateContext(
|
||||
config,
|
||||
cwd,
|
||||
input?.session_id,
|
||||
useColors
|
||||
forHuman
|
||||
);
|
||||
|
||||
return output;
|
||||
|
||||
@@ -8,6 +8,7 @@ import path from 'path';
|
||||
import { existsSync, readFileSync } from 'fs';
|
||||
import { SessionStore } from '../sqlite/SessionStore.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { SYSTEM_REMINDER_REGEX } from '../../utils/tag-stripping.js';
|
||||
import { CLAUDE_CONFIG_DIR } from '../../shared/paths.js';
|
||||
import type {
|
||||
ContextConfig,
|
||||
@@ -25,7 +26,8 @@ import { SUMMARY_LOOKAHEAD } from './types.js';
|
||||
export function queryObservations(
|
||||
db: SessionStore,
|
||||
project: string,
|
||||
config: ContextConfig
|
||||
config: ContextConfig,
|
||||
platformSource?: string
|
||||
): Observation[] {
|
||||
const typeArray = Array.from(config.observationTypes);
|
||||
const typePlaceholders = typeArray.map(() => '?').join(',');
|
||||
@@ -34,19 +36,38 @@ export function queryObservations(
|
||||
|
||||
return db.db.prepare(`
|
||||
SELECT
|
||||
id, memory_session_id, type, title, subtitle, narrative,
|
||||
facts, concepts, files_read, files_modified, discovery_tokens,
|
||||
created_at, created_at_epoch
|
||||
FROM observations
|
||||
WHERE project = ?
|
||||
o.id,
|
||||
o.memory_session_id,
|
||||
COALESCE(s.platform_source, 'claude') as platform_source,
|
||||
o.type,
|
||||
o.title,
|
||||
o.subtitle,
|
||||
o.narrative,
|
||||
o.facts,
|
||||
o.concepts,
|
||||
o.files_read,
|
||||
o.files_modified,
|
||||
o.discovery_tokens,
|
||||
o.created_at,
|
||||
o.created_at_epoch
|
||||
FROM observations o
|
||||
LEFT JOIN sdk_sessions s ON o.memory_session_id = s.memory_session_id
|
||||
WHERE o.project = ?
|
||||
AND type IN (${typePlaceholders})
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM json_each(concepts)
|
||||
SELECT 1 FROM json_each(o.concepts)
|
||||
WHERE value IN (${conceptPlaceholders})
|
||||
)
|
||||
ORDER BY created_at_epoch DESC
|
||||
${platformSource ? "AND COALESCE(s.platform_source, 'claude') = ?" : ''}
|
||||
ORDER BY o.created_at_epoch DESC
|
||||
LIMIT ?
|
||||
`).all(project, ...typeArray, ...conceptArray, config.totalObservationCount) as Observation[];
|
||||
`).all(
|
||||
project,
|
||||
...typeArray,
|
||||
...conceptArray,
|
||||
...(platformSource ? [platformSource] : []),
|
||||
config.totalObservationCount
|
||||
) as Observation[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,15 +76,30 @@ export function queryObservations(
|
||||
export function querySummaries(
|
||||
db: SessionStore,
|
||||
project: string,
|
||||
config: ContextConfig
|
||||
config: ContextConfig,
|
||||
platformSource?: string
|
||||
): SessionSummary[] {
|
||||
return db.db.prepare(`
|
||||
SELECT id, memory_session_id, request, investigated, learned, completed, next_steps, created_at, created_at_epoch
|
||||
FROM session_summaries
|
||||
WHERE project = ?
|
||||
ORDER BY created_at_epoch DESC
|
||||
SELECT
|
||||
ss.id,
|
||||
ss.memory_session_id,
|
||||
COALESCE(s.platform_source, 'claude') as platform_source,
|
||||
ss.request,
|
||||
ss.investigated,
|
||||
ss.learned,
|
||||
ss.completed,
|
||||
ss.next_steps,
|
||||
ss.created_at,
|
||||
ss.created_at_epoch
|
||||
FROM session_summaries ss
|
||||
LEFT JOIN sdk_sessions s ON ss.memory_session_id = s.memory_session_id
|
||||
WHERE ss.project = ?
|
||||
${platformSource ? "AND COALESCE(s.platform_source, 'claude') = ?" : ''}
|
||||
ORDER BY ss.created_at_epoch DESC
|
||||
LIMIT ?
|
||||
`).all(project, config.sessionCount + SUMMARY_LOOKAHEAD) as SessionSummary[];
|
||||
`).all(
|
||||
...[project, ...(platformSource ? [platformSource] : []), config.sessionCount + SUMMARY_LOOKAHEAD]
|
||||
) as SessionSummary[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,7 +111,8 @@ export function querySummaries(
|
||||
export function queryObservationsMulti(
|
||||
db: SessionStore,
|
||||
projects: string[],
|
||||
config: ContextConfig
|
||||
config: ContextConfig,
|
||||
platformSource?: string
|
||||
): Observation[] {
|
||||
const typeArray = Array.from(config.observationTypes);
|
||||
const typePlaceholders = typeArray.map(() => '?').join(',');
|
||||
@@ -87,19 +124,39 @@ export function queryObservationsMulti(
|
||||
|
||||
return db.db.prepare(`
|
||||
SELECT
|
||||
id, memory_session_id, type, title, subtitle, narrative,
|
||||
facts, concepts, files_read, files_modified, discovery_tokens,
|
||||
created_at, created_at_epoch, project
|
||||
FROM observations
|
||||
WHERE project IN (${projectPlaceholders})
|
||||
o.id,
|
||||
o.memory_session_id,
|
||||
COALESCE(s.platform_source, 'claude') as platform_source,
|
||||
o.type,
|
||||
o.title,
|
||||
o.subtitle,
|
||||
o.narrative,
|
||||
o.facts,
|
||||
o.concepts,
|
||||
o.files_read,
|
||||
o.files_modified,
|
||||
o.discovery_tokens,
|
||||
o.created_at,
|
||||
o.created_at_epoch,
|
||||
o.project
|
||||
FROM observations o
|
||||
LEFT JOIN sdk_sessions s ON o.memory_session_id = s.memory_session_id
|
||||
WHERE o.project IN (${projectPlaceholders})
|
||||
AND type IN (${typePlaceholders})
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM json_each(concepts)
|
||||
SELECT 1 FROM json_each(o.concepts)
|
||||
WHERE value IN (${conceptPlaceholders})
|
||||
)
|
||||
ORDER BY created_at_epoch DESC
|
||||
${platformSource ? "AND COALESCE(s.platform_source, 'claude') = ?" : ''}
|
||||
ORDER BY o.created_at_epoch DESC
|
||||
LIMIT ?
|
||||
`).all(...projects, ...typeArray, ...conceptArray, config.totalObservationCount) as Observation[];
|
||||
`).all(
|
||||
...projects,
|
||||
...typeArray,
|
||||
...conceptArray,
|
||||
...(platformSource ? [platformSource] : []),
|
||||
config.totalObservationCount
|
||||
) as Observation[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,18 +168,32 @@ export function queryObservationsMulti(
|
||||
export function querySummariesMulti(
|
||||
db: SessionStore,
|
||||
projects: string[],
|
||||
config: ContextConfig
|
||||
config: ContextConfig,
|
||||
platformSource?: string
|
||||
): SessionSummary[] {
|
||||
// Build IN clause for projects
|
||||
const projectPlaceholders = projects.map(() => '?').join(',');
|
||||
|
||||
return db.db.prepare(`
|
||||
SELECT id, memory_session_id, request, investigated, learned, completed, next_steps, created_at, created_at_epoch, project
|
||||
FROM session_summaries
|
||||
WHERE project IN (${projectPlaceholders})
|
||||
ORDER BY created_at_epoch DESC
|
||||
SELECT
|
||||
ss.id,
|
||||
ss.memory_session_id,
|
||||
COALESCE(s.platform_source, 'claude') as platform_source,
|
||||
ss.request,
|
||||
ss.investigated,
|
||||
ss.learned,
|
||||
ss.completed,
|
||||
ss.next_steps,
|
||||
ss.created_at,
|
||||
ss.created_at_epoch,
|
||||
ss.project
|
||||
FROM session_summaries ss
|
||||
LEFT JOIN sdk_sessions s ON ss.memory_session_id = s.memory_session_id
|
||||
WHERE ss.project IN (${projectPlaceholders})
|
||||
${platformSource ? "AND COALESCE(s.platform_source, 'claude') = ?" : ''}
|
||||
ORDER BY ss.created_at_epoch DESC
|
||||
LIMIT ?
|
||||
`).all(...projects, config.sessionCount + SUMMARY_LOOKAHEAD) as SessionSummary[];
|
||||
`).all(...projects, ...(platformSource ? [platformSource] : []), config.sessionCount + SUMMARY_LOOKAHEAD) as SessionSummary[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,7 +235,7 @@ export function extractPriorMessages(transcriptPath: string): PriorMessages {
|
||||
text += block.text;
|
||||
}
|
||||
}
|
||||
text = text.replace(/<system-reminder>[\s\S]*?<\/system-reminder>/g, '').trim();
|
||||
text = text.replace(SYSTEM_REMINDER_REGEX, '').trim();
|
||||
if (text) {
|
||||
lastAssistantMessage = text;
|
||||
break;
|
||||
|
||||
+31
-31
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* MarkdownFormatter - Formats context output as compact markdown for LLM injection
|
||||
* AgentFormatter - Formats context output as compact markdown for LLM injection
|
||||
*
|
||||
* Optimized for token efficiency: flat lines instead of tables, no repeated headers.
|
||||
* The colored terminal formatter (ColorFormatter.ts) handles human-readable display separately.
|
||||
* The human-readable terminal formatter (HumanFormatter.ts) handles human-readable display separately.
|
||||
*/
|
||||
|
||||
import type {
|
||||
@@ -31,19 +31,19 @@ function formatHeaderDateTime(): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown header
|
||||
* Render agent header
|
||||
*/
|
||||
export function renderMarkdownHeader(project: string): string[] {
|
||||
export function renderAgentHeader(project: string): string[] {
|
||||
return [
|
||||
`# $CMEM ${project} ${formatHeaderDateTime()}`,
|
||||
`# [${project}] recent context, ${formatHeaderDateTime()}`,
|
||||
''
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown legend
|
||||
* Render agent legend
|
||||
*/
|
||||
export function renderMarkdownLegend(): string[] {
|
||||
export function renderAgentLegend(): string[] {
|
||||
const mode = ModeManager.getInstance().getActiveMode();
|
||||
const typeLegendItems = mode.observation_types.map(t => `${t.emoji}${t.id}`).join(' ');
|
||||
|
||||
@@ -56,23 +56,23 @@ export function renderMarkdownLegend(): string[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown column key - no longer needed in compact format
|
||||
* Render agent column key - no longer needed in compact format
|
||||
*/
|
||||
export function renderMarkdownColumnKey(): string[] {
|
||||
export function renderAgentColumnKey(): string[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown context index instructions - folded into legend
|
||||
* Render agent context index instructions - folded into legend
|
||||
*/
|
||||
export function renderMarkdownContextIndex(): string[] {
|
||||
export function renderAgentContextIndex(): string[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown context economics
|
||||
* Render agent context economics
|
||||
*/
|
||||
export function renderMarkdownContextEconomics(
|
||||
export function renderAgentContextEconomics(
|
||||
economics: TokenEconomics,
|
||||
config: ContextConfig
|
||||
): string[] {
|
||||
@@ -98,18 +98,18 @@ export function renderMarkdownContextEconomics(
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown day header
|
||||
* Render agent day header
|
||||
*/
|
||||
export function renderMarkdownDayHeader(day: string): string[] {
|
||||
export function renderAgentDayHeader(day: string): string[] {
|
||||
return [
|
||||
`### ${day}`,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown file header - no longer renders table headers in compact format
|
||||
* Render agent file header - no longer renders table headers in compact format
|
||||
*/
|
||||
export function renderMarkdownFileHeader(_file: string): string[] {
|
||||
export function renderAgentFileHeader(_file: string): string[] {
|
||||
// File grouping eliminated in compact format - file context is in observation titles
|
||||
return [];
|
||||
}
|
||||
@@ -124,7 +124,7 @@ function compactTime(time: string): string {
|
||||
/**
|
||||
* Render compact flat line for observation (replaces table row)
|
||||
*/
|
||||
export function renderMarkdownTableRow(
|
||||
export function renderAgentTableRow(
|
||||
obs: Observation,
|
||||
timeDisplay: string,
|
||||
_config: ContextConfig
|
||||
@@ -137,9 +137,9 @@ export function renderMarkdownTableRow(
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown full observation
|
||||
* Render agent full observation
|
||||
*/
|
||||
export function renderMarkdownFullObservation(
|
||||
export function renderAgentFullObservation(
|
||||
obs: Observation,
|
||||
timeDisplay: string,
|
||||
detailField: string | null,
|
||||
@@ -172,9 +172,9 @@ export function renderMarkdownFullObservation(
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown summary item in timeline
|
||||
* Render agent summary item in timeline
|
||||
*/
|
||||
export function renderMarkdownSummaryItem(
|
||||
export function renderAgentSummaryItem(
|
||||
summary: { id: number; request: string | null },
|
||||
formattedTime: string
|
||||
): string[] {
|
||||
@@ -184,17 +184,17 @@ export function renderMarkdownSummaryItem(
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown summary field
|
||||
* Render agent summary field
|
||||
*/
|
||||
export function renderMarkdownSummaryField(label: string, value: string | null): string[] {
|
||||
export function renderAgentSummaryField(label: string, value: string | null): string[] {
|
||||
if (!value) return [];
|
||||
return [`**${label}**: ${value}`, ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown previously section
|
||||
* Render agent previously section
|
||||
*/
|
||||
export function renderMarkdownPreviouslySection(priorMessages: PriorMessages): string[] {
|
||||
export function renderAgentPreviouslySection(priorMessages: PriorMessages): string[] {
|
||||
if (!priorMessages.assistantMessage) return [];
|
||||
|
||||
return [
|
||||
@@ -209,9 +209,9 @@ export function renderMarkdownPreviouslySection(priorMessages: PriorMessages): s
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown footer
|
||||
* Render agent footer
|
||||
*/
|
||||
export function renderMarkdownFooter(totalDiscoveryTokens: number, totalReadTokens: number): string[] {
|
||||
export function renderAgentFooter(totalDiscoveryTokens: number, totalReadTokens: number): string[] {
|
||||
const workTokensK = Math.round(totalDiscoveryTokens / 1000);
|
||||
return [
|
||||
'',
|
||||
@@ -220,8 +220,8 @@ export function renderMarkdownFooter(totalDiscoveryTokens: number, totalReadToke
|
||||
}
|
||||
|
||||
/**
|
||||
* Render markdown empty state
|
||||
* Render agent empty state
|
||||
*/
|
||||
export function renderMarkdownEmptyState(project: string): string {
|
||||
return `# $CMEM ${project} ${formatHeaderDateTime()}\n\nNo previous sessions found.`;
|
||||
export function renderAgentEmptyState(project: string): string {
|
||||
return `# [${project}] recent context, ${formatHeaderDateTime()}\n\nNo previous sessions found.`;
|
||||
}
|
||||
+29
-29
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* ColorFormatter - Formats context output with ANSI colors for terminal
|
||||
* HumanFormatter - Formats context output with ANSI colors for terminal
|
||||
*
|
||||
* Handles all colored formatting for context injection (terminal display).
|
||||
*/
|
||||
@@ -30,9 +30,9 @@ function formatHeaderDateTime(): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored header
|
||||
* Render human-readable header
|
||||
*/
|
||||
export function renderColorHeader(project: string): string[] {
|
||||
export function renderHumanHeader(project: string): string[] {
|
||||
return [
|
||||
'',
|
||||
`${colors.bright}${colors.cyan}[${project}] recent context, ${formatHeaderDateTime()}${colors.reset}`,
|
||||
@@ -42,9 +42,9 @@ export function renderColorHeader(project: string): string[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored legend
|
||||
* Render human-readable legend
|
||||
*/
|
||||
export function renderColorLegend(): string[] {
|
||||
export function renderHumanLegend(): string[] {
|
||||
const mode = ModeManager.getInstance().getActiveMode();
|
||||
const typeLegendItems = mode.observation_types.map(t => `${t.emoji} ${t.id}`).join(' | ');
|
||||
|
||||
@@ -55,9 +55,9 @@ export function renderColorLegend(): string[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored column key
|
||||
* Render human-readable column key
|
||||
*/
|
||||
export function renderColorColumnKey(): string[] {
|
||||
export function renderHumanColumnKey(): string[] {
|
||||
return [
|
||||
`${colors.bright}Column Key${colors.reset}`,
|
||||
`${colors.dim} Read: Tokens to read this observation (cost to learn it now)${colors.reset}`,
|
||||
@@ -67,9 +67,9 @@ export function renderColorColumnKey(): string[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored context index instructions
|
||||
* Render human-readable context index instructions
|
||||
*/
|
||||
export function renderColorContextIndex(): string[] {
|
||||
export function renderHumanContextIndex(): string[] {
|
||||
return [
|
||||
`${colors.dim}Context Index: This semantic index (titles, types, files, tokens) is usually sufficient to understand past work.${colors.reset}`,
|
||||
'',
|
||||
@@ -82,9 +82,9 @@ export function renderColorContextIndex(): string[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored context economics
|
||||
* Render human-readable context economics
|
||||
*/
|
||||
export function renderColorContextEconomics(
|
||||
export function renderHumanContextEconomics(
|
||||
economics: TokenEconomics,
|
||||
config: ContextConfig
|
||||
): string[] {
|
||||
@@ -111,9 +111,9 @@ export function renderColorContextEconomics(
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored day header
|
||||
* Render human-readable day header
|
||||
*/
|
||||
export function renderColorDayHeader(day: string): string[] {
|
||||
export function renderHumanDayHeader(day: string): string[] {
|
||||
return [
|
||||
`${colors.bright}${colors.cyan}${day}${colors.reset}`,
|
||||
''
|
||||
@@ -121,18 +121,18 @@ export function renderColorDayHeader(day: string): string[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored file header
|
||||
* Render human-readable file header
|
||||
*/
|
||||
export function renderColorFileHeader(file: string): string[] {
|
||||
export function renderHumanFileHeader(file: string): string[] {
|
||||
return [
|
||||
`${colors.dim}${file}${colors.reset}`
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored table row for observation
|
||||
* Render human-readable table row for observation
|
||||
*/
|
||||
export function renderColorTableRow(
|
||||
export function renderHumanTableRow(
|
||||
obs: Observation,
|
||||
time: string,
|
||||
showTime: boolean,
|
||||
@@ -150,9 +150,9 @@ export function renderColorTableRow(
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored full observation
|
||||
* Render human-readable full observation
|
||||
*/
|
||||
export function renderColorFullObservation(
|
||||
export function renderHumanFullObservation(
|
||||
obs: Observation,
|
||||
time: string,
|
||||
showTime: boolean,
|
||||
@@ -181,9 +181,9 @@ export function renderColorFullObservation(
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored summary item in timeline
|
||||
* Render human-readable summary item in timeline
|
||||
*/
|
||||
export function renderColorSummaryItem(
|
||||
export function renderHumanSummaryItem(
|
||||
summary: { id: number; request: string | null },
|
||||
formattedTime: string
|
||||
): string[] {
|
||||
@@ -195,17 +195,17 @@ export function renderColorSummaryItem(
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored summary field
|
||||
* Render human-readable summary field
|
||||
*/
|
||||
export function renderColorSummaryField(label: string, value: string | null, color: string): string[] {
|
||||
export function renderHumanSummaryField(label: string, value: string | null, color: string): string[] {
|
||||
if (!value) return [];
|
||||
return [`${color}${label}:${colors.reset} ${value}`, ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored previously section
|
||||
* Render human-readable previously section
|
||||
*/
|
||||
export function renderColorPreviouslySection(priorMessages: PriorMessages): string[] {
|
||||
export function renderHumanPreviouslySection(priorMessages: PriorMessages): string[] {
|
||||
if (!priorMessages.assistantMessage) return [];
|
||||
|
||||
return [
|
||||
@@ -220,9 +220,9 @@ export function renderColorPreviouslySection(priorMessages: PriorMessages): stri
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored footer
|
||||
* Render human-readable footer
|
||||
*/
|
||||
export function renderColorFooter(totalDiscoveryTokens: number, totalReadTokens: number): string[] {
|
||||
export function renderHumanFooter(totalDiscoveryTokens: number, totalReadTokens: number): string[] {
|
||||
const workTokensK = Math.round(totalDiscoveryTokens / 1000);
|
||||
return [
|
||||
'',
|
||||
@@ -231,8 +231,8 @@ export function renderColorFooter(totalDiscoveryTokens: number, totalReadTokens:
|
||||
}
|
||||
|
||||
/**
|
||||
* Render colored empty state
|
||||
* Render human-readable empty state
|
||||
*/
|
||||
export function renderColorEmptyState(project: string): string {
|
||||
export function renderHumanEmptyState(project: string): string {
|
||||
return `\n${colors.bright}${colors.cyan}[${project}] recent context, ${formatHeaderDateTime()}${colors.reset}\n${colors.gray}${'─'.repeat(60)}${colors.reset}\n\n${colors.dim}No previous sessions found for this project yet.${colors.reset}\n`;
|
||||
}
|
||||
@@ -6,20 +6,20 @@
|
||||
|
||||
import type { ContextConfig, TokenEconomics, PriorMessages } from '../types.js';
|
||||
import { shouldShowContextEconomics } from '../TokenCalculator.js';
|
||||
import * as Markdown from '../formatters/MarkdownFormatter.js';
|
||||
import * as Color from '../formatters/ColorFormatter.js';
|
||||
import * as Agent from '../formatters/AgentFormatter.js';
|
||||
import * as Human from '../formatters/HumanFormatter.js';
|
||||
|
||||
/**
|
||||
* Render the previously section (prior assistant message)
|
||||
*/
|
||||
export function renderPreviouslySection(
|
||||
priorMessages: PriorMessages,
|
||||
useColors: boolean
|
||||
forHuman: boolean
|
||||
): string[] {
|
||||
if (useColors) {
|
||||
return Color.renderColorPreviouslySection(priorMessages);
|
||||
if (forHuman) {
|
||||
return Human.renderHumanPreviouslySection(priorMessages);
|
||||
}
|
||||
return Markdown.renderMarkdownPreviouslySection(priorMessages);
|
||||
return Agent.renderAgentPreviouslySection(priorMessages);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -28,15 +28,15 @@ export function renderPreviouslySection(
|
||||
export function renderFooter(
|
||||
economics: TokenEconomics,
|
||||
config: ContextConfig,
|
||||
useColors: boolean
|
||||
forHuman: boolean
|
||||
): string[] {
|
||||
// Only show footer if we have savings to display
|
||||
if (!shouldShowContextEconomics(config) || economics.totalDiscoveryTokens <= 0 || economics.savings <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (useColors) {
|
||||
return Color.renderColorFooter(economics.totalDiscoveryTokens, economics.totalReadTokens);
|
||||
if (forHuman) {
|
||||
return Human.renderHumanFooter(economics.totalDiscoveryTokens, economics.totalReadTokens);
|
||||
}
|
||||
return Markdown.renderMarkdownFooter(economics.totalDiscoveryTokens, economics.totalReadTokens);
|
||||
return Agent.renderAgentFooter(economics.totalDiscoveryTokens, economics.totalReadTokens);
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
|
||||
import type { ContextConfig, TokenEconomics } from '../types.js';
|
||||
import { shouldShowContextEconomics } from '../TokenCalculator.js';
|
||||
import * as Markdown from '../formatters/MarkdownFormatter.js';
|
||||
import * as Color from '../formatters/ColorFormatter.js';
|
||||
import * as Agent from '../formatters/AgentFormatter.js';
|
||||
import * as Human from '../formatters/HumanFormatter.js';
|
||||
|
||||
/**
|
||||
* Render the complete header section
|
||||
@@ -16,44 +16,44 @@ export function renderHeader(
|
||||
project: string,
|
||||
economics: TokenEconomics,
|
||||
config: ContextConfig,
|
||||
useColors: boolean
|
||||
forHuman: boolean
|
||||
): string[] {
|
||||
const output: string[] = [];
|
||||
|
||||
// Main header
|
||||
if (useColors) {
|
||||
output.push(...Color.renderColorHeader(project));
|
||||
if (forHuman) {
|
||||
output.push(...Human.renderHumanHeader(project));
|
||||
} else {
|
||||
output.push(...Markdown.renderMarkdownHeader(project));
|
||||
output.push(...Agent.renderAgentHeader(project));
|
||||
}
|
||||
|
||||
// Legend
|
||||
if (useColors) {
|
||||
output.push(...Color.renderColorLegend());
|
||||
if (forHuman) {
|
||||
output.push(...Human.renderHumanLegend());
|
||||
} else {
|
||||
output.push(...Markdown.renderMarkdownLegend());
|
||||
output.push(...Agent.renderAgentLegend());
|
||||
}
|
||||
|
||||
// Column key
|
||||
if (useColors) {
|
||||
output.push(...Color.renderColorColumnKey());
|
||||
if (forHuman) {
|
||||
output.push(...Human.renderHumanColumnKey());
|
||||
} else {
|
||||
output.push(...Markdown.renderMarkdownColumnKey());
|
||||
output.push(...Agent.renderAgentColumnKey());
|
||||
}
|
||||
|
||||
// Context index instructions
|
||||
if (useColors) {
|
||||
output.push(...Color.renderColorContextIndex());
|
||||
if (forHuman) {
|
||||
output.push(...Human.renderHumanContextIndex());
|
||||
} else {
|
||||
output.push(...Markdown.renderMarkdownContextIndex());
|
||||
output.push(...Agent.renderAgentContextIndex());
|
||||
}
|
||||
|
||||
// Context economics
|
||||
if (shouldShowContextEconomics(config)) {
|
||||
if (useColors) {
|
||||
output.push(...Color.renderColorContextEconomics(economics, config));
|
||||
if (forHuman) {
|
||||
output.push(...Human.renderHumanContextEconomics(economics, config));
|
||||
} else {
|
||||
output.push(...Markdown.renderMarkdownContextEconomics(economics, config));
|
||||
output.push(...Agent.renderAgentContextEconomics(economics, config));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
|
||||
import type { ContextConfig, Observation, SessionSummary } from '../types.js';
|
||||
import { colors } from '../types.js';
|
||||
import * as Markdown from '../formatters/MarkdownFormatter.js';
|
||||
import * as Color from '../formatters/ColorFormatter.js';
|
||||
import * as Agent from '../formatters/AgentFormatter.js';
|
||||
import * as Human from '../formatters/HumanFormatter.js';
|
||||
|
||||
/**
|
||||
* Check if summary should be displayed
|
||||
@@ -45,20 +45,20 @@ export function shouldShowSummary(
|
||||
*/
|
||||
export function renderSummaryFields(
|
||||
summary: SessionSummary,
|
||||
useColors: boolean
|
||||
forHuman: boolean
|
||||
): string[] {
|
||||
const output: string[] = [];
|
||||
|
||||
if (useColors) {
|
||||
output.push(...Color.renderColorSummaryField('Investigated', summary.investigated, colors.blue));
|
||||
output.push(...Color.renderColorSummaryField('Learned', summary.learned, colors.yellow));
|
||||
output.push(...Color.renderColorSummaryField('Completed', summary.completed, colors.green));
|
||||
output.push(...Color.renderColorSummaryField('Next Steps', summary.next_steps, colors.magenta));
|
||||
if (forHuman) {
|
||||
output.push(...Human.renderHumanSummaryField('Investigated', summary.investigated, colors.blue));
|
||||
output.push(...Human.renderHumanSummaryField('Learned', summary.learned, colors.yellow));
|
||||
output.push(...Human.renderHumanSummaryField('Completed', summary.completed, colors.green));
|
||||
output.push(...Human.renderHumanSummaryField('Next Steps', summary.next_steps, colors.magenta));
|
||||
} else {
|
||||
output.push(...Markdown.renderMarkdownSummaryField('Investigated', summary.investigated));
|
||||
output.push(...Markdown.renderMarkdownSummaryField('Learned', summary.learned));
|
||||
output.push(...Markdown.renderMarkdownSummaryField('Completed', summary.completed));
|
||||
output.push(...Markdown.renderMarkdownSummaryField('Next Steps', summary.next_steps));
|
||||
output.push(...Agent.renderAgentSummaryField('Investigated', summary.investigated));
|
||||
output.push(...Agent.renderAgentSummaryField('Learned', summary.learned));
|
||||
output.push(...Agent.renderAgentSummaryField('Completed', summary.completed));
|
||||
output.push(...Agent.renderAgentSummaryField('Next Steps', summary.next_steps));
|
||||
}
|
||||
|
||||
return output;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* TimelineRenderer - Renders the chronological timeline of observations and summaries
|
||||
*
|
||||
* Handles day grouping and rendering. In markdown (LLM) mode, uses flat compact lines.
|
||||
* In color (terminal) mode, uses file grouping with visual formatting.
|
||||
* Handles day grouping and rendering. In agent (LLM) mode, uses flat compact lines.
|
||||
* In human (terminal) mode, uses file grouping with visual formatting.
|
||||
*/
|
||||
|
||||
import type {
|
||||
@@ -12,8 +12,8 @@ import type {
|
||||
SummaryTimelineItem,
|
||||
} from '../types.js';
|
||||
import { formatTime, formatDate, formatDateTime, extractFirstFile, parseJsonArray } from '../../../shared/timeline-formatting.js';
|
||||
import * as Markdown from '../formatters/MarkdownFormatter.js';
|
||||
import * as Color from '../formatters/ColorFormatter.js';
|
||||
import * as Agent from '../formatters/AgentFormatter.js';
|
||||
import * as Human from '../formatters/HumanFormatter.js';
|
||||
|
||||
/**
|
||||
* Group timeline items by day
|
||||
@@ -51,9 +51,9 @@ function getDetailField(obs: Observation, config: ContextConfig): string | null
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a single day's timeline items (markdown/LLM mode - flat compact lines)
|
||||
* Render a single day's timeline items (agent/LLM mode - flat compact lines)
|
||||
*/
|
||||
function renderDayTimelineMarkdown(
|
||||
function renderDayTimelineAgent(
|
||||
day: string,
|
||||
dayItems: TimelineItem[],
|
||||
fullObservationIds: Set<number>,
|
||||
@@ -61,17 +61,15 @@ function renderDayTimelineMarkdown(
|
||||
): string[] {
|
||||
const output: string[] = [];
|
||||
|
||||
output.push(...Markdown.renderMarkdownDayHeader(day));
|
||||
output.push(...Agent.renderAgentDayHeader(day));
|
||||
|
||||
let lastTime = '';
|
||||
|
||||
for (const item of dayItems) {
|
||||
if (item.type === 'summary') {
|
||||
lastTime = '';
|
||||
|
||||
const summary = item.data as SummaryTimelineItem;
|
||||
const formattedTime = formatDateTime(summary.displayTime);
|
||||
output.push(...Markdown.renderMarkdownSummaryItem(summary, formattedTime));
|
||||
output.push(...Agent.renderAgentSummaryItem(summary, formattedTime));
|
||||
} else {
|
||||
const obs = item.data as Observation;
|
||||
const time = formatTime(obs.created_at);
|
||||
@@ -83,9 +81,9 @@ function renderDayTimelineMarkdown(
|
||||
|
||||
if (shouldShowFull) {
|
||||
const detailField = getDetailField(obs, config);
|
||||
output.push(...Markdown.renderMarkdownFullObservation(obs, timeDisplay, detailField, config));
|
||||
output.push(...Agent.renderAgentFullObservation(obs, timeDisplay, detailField, config));
|
||||
} else {
|
||||
output.push(Markdown.renderMarkdownTableRow(obs, timeDisplay, config));
|
||||
output.push(Agent.renderAgentTableRow(obs, timeDisplay, config));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,9 +92,9 @@ function renderDayTimelineMarkdown(
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a single day's timeline items (color/terminal mode - file grouped with tables)
|
||||
* Render a single day's timeline items (human/terminal mode - file grouped with tables)
|
||||
*/
|
||||
function renderDayTimelineColor(
|
||||
function renderDayTimelineHuman(
|
||||
day: string,
|
||||
dayItems: TimelineItem[],
|
||||
fullObservationIds: Set<number>,
|
||||
@@ -105,7 +103,7 @@ function renderDayTimelineColor(
|
||||
): string[] {
|
||||
const output: string[] = [];
|
||||
|
||||
output.push(...Color.renderColorDayHeader(day));
|
||||
output.push(...Human.renderHumanDayHeader(day));
|
||||
|
||||
let currentFile: string | null = null;
|
||||
let lastTime = '';
|
||||
@@ -117,7 +115,7 @@ function renderDayTimelineColor(
|
||||
|
||||
const summary = item.data as SummaryTimelineItem;
|
||||
const formattedTime = formatDateTime(summary.displayTime);
|
||||
output.push(...Color.renderColorSummaryItem(summary, formattedTime));
|
||||
output.push(...Human.renderHumanSummaryItem(summary, formattedTime));
|
||||
} else {
|
||||
const obs = item.data as Observation;
|
||||
const file = extractFirstFile(obs.files_modified, cwd, obs.files_read);
|
||||
@@ -129,15 +127,15 @@ function renderDayTimelineColor(
|
||||
|
||||
// Check if we need a new file section
|
||||
if (file !== currentFile) {
|
||||
output.push(...Color.renderColorFileHeader(file));
|
||||
output.push(...Human.renderHumanFileHeader(file));
|
||||
currentFile = file;
|
||||
}
|
||||
|
||||
if (shouldShowFull) {
|
||||
const detailField = getDetailField(obs, config);
|
||||
output.push(...Color.renderColorFullObservation(obs, time, showTime, detailField, config));
|
||||
output.push(...Human.renderHumanFullObservation(obs, time, showTime, detailField, config));
|
||||
} else {
|
||||
output.push(Color.renderColorTableRow(obs, time, showTime, config));
|
||||
output.push(Human.renderHumanTableRow(obs, time, showTime, config));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,12 +154,12 @@ export function renderDayTimeline(
|
||||
fullObservationIds: Set<number>,
|
||||
config: ContextConfig,
|
||||
cwd: string,
|
||||
useColors: boolean
|
||||
forHuman: boolean
|
||||
): string[] {
|
||||
if (useColors) {
|
||||
return renderDayTimelineColor(day, dayItems, fullObservationIds, config, cwd);
|
||||
if (forHuman) {
|
||||
return renderDayTimelineHuman(day, dayItems, fullObservationIds, config, cwd);
|
||||
}
|
||||
return renderDayTimelineMarkdown(day, dayItems, fullObservationIds, config);
|
||||
return renderDayTimelineAgent(day, dayItems, fullObservationIds, config);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -172,13 +170,13 @@ export function renderTimeline(
|
||||
fullObservationIds: Set<number>,
|
||||
config: ContextConfig,
|
||||
cwd: string,
|
||||
useColors: boolean
|
||||
forHuman: boolean
|
||||
): string[] {
|
||||
const output: string[] = [];
|
||||
const itemsByDay = groupTimelineByDay(timeline);
|
||||
|
||||
for (const [day, dayItems] of itemsByDay) {
|
||||
output.push(...renderDayTimeline(day, dayItems, fullObservationIds, config, cwd, useColors));
|
||||
output.push(...renderDayTimeline(day, dayItems, fullObservationIds, config, cwd, forHuman));
|
||||
}
|
||||
|
||||
return output;
|
||||
|
||||
@@ -15,6 +15,7 @@ export interface ContextInput {
|
||||
projects?: string[];
|
||||
/** When true, return ALL observations with no limit */
|
||||
full?: boolean;
|
||||
platform_source?: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
@@ -49,6 +50,7 @@ export interface ContextConfig {
|
||||
export interface Observation {
|
||||
id: number;
|
||||
memory_session_id: string;
|
||||
platform_source?: string;
|
||||
type: string;
|
||||
title: string | null;
|
||||
subtitle: string | null;
|
||||
@@ -70,6 +72,7 @@ export interface Observation {
|
||||
export interface SessionSummary {
|
||||
id: number;
|
||||
memory_session_id: string;
|
||||
platform_source?: string;
|
||||
request: string | null;
|
||||
investigated: string | null;
|
||||
learned: string | null;
|
||||
|
||||
@@ -71,21 +71,62 @@ function lookupBinaryInPath(binaryName: string, platform: NodeJS.Platform): stri
|
||||
}
|
||||
}
|
||||
|
||||
// Memoize the resolved runtime path for the no-options call site (which is
|
||||
// what spawnDaemon uses). Caches successful resolutions so repeated spawn
|
||||
// attempts (crash loops, health thrashing) don't repeatedly hit `statSync`
|
||||
// on the candidate paths.
|
||||
//
|
||||
// IMPORTANT: only success is cached. A `null` result (Bun not found) is
|
||||
// never cached so that a long-running MCP server can recover if the user
|
||||
// installs Bun in another terminal between the first failed lookup and a
|
||||
// subsequent retry. Caching `null` would permanently break the process
|
||||
// until restart. Per PR #1645 round-10 review.
|
||||
//
|
||||
// `undefined` means "not yet resolved"; tests that pass options bypass the
|
||||
// cache entirely.
|
||||
let cachedWorkerRuntimePath: string | undefined = undefined;
|
||||
|
||||
/**
|
||||
* Reset the memoized runtime path. Exported for test isolation only —
|
||||
* production code never needs to call this.
|
||||
*/
|
||||
export function resetWorkerRuntimePathCache(): void {
|
||||
cachedWorkerRuntimePath = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the runtime executable for spawning the worker daemon.
|
||||
*
|
||||
* Windows must prefer Bun because worker-service.cjs imports bun:sqlite,
|
||||
* which is unavailable in Node.js.
|
||||
* worker-service.cjs imports `bun:sqlite`, so it MUST run under Bun on every
|
||||
* platform — not just Windows. When the caller is already running under Bun
|
||||
* (e.g. the worker self-spawning from a hook), we reuse process.execPath to
|
||||
* avoid an extra PATH lookup. Otherwise (notably when the MCP server running
|
||||
* under Node spawns the worker for the first time) we locate the Bun binary
|
||||
* via env vars, well-known install locations, and finally the system PATH.
|
||||
*/
|
||||
export function resolveWorkerRuntimePath(options: RuntimeResolverOptions = {}): string | null {
|
||||
// Memoization fast path — only when called with no injected options. Tests
|
||||
// that pass options always run the full resolution (and never populate or
|
||||
// read the cache) to keep the existing test cases deterministic.
|
||||
const isMemoizable = Object.keys(options).length === 0;
|
||||
if (isMemoizable && cachedWorkerRuntimePath !== undefined) {
|
||||
return cachedWorkerRuntimePath;
|
||||
}
|
||||
|
||||
const result = resolveWorkerRuntimePathUncached(options);
|
||||
|
||||
// Only cache successful resolutions. See the comment on
|
||||
// `cachedWorkerRuntimePath` above for the rationale.
|
||||
if (isMemoizable && result !== null) {
|
||||
cachedWorkerRuntimePath = result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function resolveWorkerRuntimePathUncached(options: RuntimeResolverOptions): string | null {
|
||||
const platform = options.platform ?? process.platform;
|
||||
const execPath = options.execPath ?? process.execPath;
|
||||
|
||||
// Non-Windows currently relies on the runtime that launched worker-service.
|
||||
if (platform !== 'win32') {
|
||||
return execPath;
|
||||
}
|
||||
|
||||
// If already running under Bun, reuse it directly.
|
||||
if (isBunExecutablePath(execPath)) {
|
||||
return execPath;
|
||||
@@ -96,15 +137,26 @@ export function resolveWorkerRuntimePath(options: RuntimeResolverOptions = {}):
|
||||
const pathExists = options.pathExists ?? existsSync;
|
||||
const lookupInPath = options.lookupInPath ?? lookupBinaryInPath;
|
||||
|
||||
const candidatePaths = [
|
||||
env.BUN,
|
||||
env.BUN_PATH,
|
||||
path.join(homeDirectory, '.bun', 'bin', 'bun.exe'),
|
||||
path.join(homeDirectory, '.bun', 'bin', 'bun'),
|
||||
env.USERPROFILE ? path.join(env.USERPROFILE, '.bun', 'bin', 'bun.exe') : undefined,
|
||||
env.LOCALAPPDATA ? path.join(env.LOCALAPPDATA, 'bun', 'bun.exe') : undefined,
|
||||
env.LOCALAPPDATA ? path.join(env.LOCALAPPDATA, 'bun', 'bin', 'bun.exe') : undefined,
|
||||
];
|
||||
const candidatePaths: (string | undefined)[] = platform === 'win32'
|
||||
? [
|
||||
env.BUN,
|
||||
env.BUN_PATH,
|
||||
path.join(homeDirectory, '.bun', 'bin', 'bun.exe'),
|
||||
path.join(homeDirectory, '.bun', 'bin', 'bun'),
|
||||
env.USERPROFILE ? path.join(env.USERPROFILE, '.bun', 'bin', 'bun.exe') : undefined,
|
||||
env.LOCALAPPDATA ? path.join(env.LOCALAPPDATA, 'bun', 'bun.exe') : undefined,
|
||||
env.LOCALAPPDATA ? path.join(env.LOCALAPPDATA, 'bun', 'bin', 'bun.exe') : undefined,
|
||||
]
|
||||
: [
|
||||
env.BUN,
|
||||
env.BUN_PATH,
|
||||
path.join(homeDirectory, '.bun', 'bin', 'bun'),
|
||||
'/usr/local/bin/bun',
|
||||
'/opt/homebrew/bin/bun',
|
||||
'/home/linuxbrew/.linuxbrew/bin/bun',
|
||||
'/usr/bin/bun', // Debian/Ubuntu apt install path
|
||||
'/snap/bin/bun', // Ubuntu Snap install path
|
||||
];
|
||||
|
||||
for (const candidate of candidatePaths) {
|
||||
const normalized = candidate?.trim();
|
||||
@@ -114,7 +166,11 @@ export function resolveWorkerRuntimePath(options: RuntimeResolverOptions = {}):
|
||||
return normalized;
|
||||
}
|
||||
|
||||
// Allow command-style values from env (e.g. BUN=bun)
|
||||
// Allow command-style values from env (e.g. BUN=bun). The previous branch
|
||||
// would also match this candidate via isBunExecutablePath('bun') === true,
|
||||
// but pathExists('bun') is false because it's a relative name — so this
|
||||
// branch is what actually fires for the bare-command case. We return the
|
||||
// bare name unchanged so child_process.spawn() resolves it via PATH.
|
||||
if (normalized.toLowerCase() === 'bun') {
|
||||
return normalized;
|
||||
}
|
||||
@@ -453,6 +509,19 @@ export async function aggressiveStartupCleanup(): Promise<void> {
|
||||
const pidsToKill: number[] = [];
|
||||
const allPatterns = [...AGGRESSIVE_CLEANUP_PATTERNS, ...AGE_GATED_CLEANUP_PATTERNS];
|
||||
|
||||
// Protect parent process (the hook that spawned us) from being killed.
|
||||
// Without this, a new daemon kills its own parent hook process (#1426).
|
||||
//
|
||||
// Note: readPidFile() is not used here because start() writes the new PID
|
||||
// before initializeBackground() calls this function, so readPidFile() would
|
||||
// just return process.pid (already protected). If a pre-existing worker needs
|
||||
// protection, ensureWorkerStarted() handles that by returning early when a
|
||||
// healthy worker is detected — we never reach this code in that case.
|
||||
const protectedPids = new Set<number>([currentPid]);
|
||||
if (process.ppid && process.ppid > 0) {
|
||||
protectedPids.add(process.ppid);
|
||||
}
|
||||
|
||||
try {
|
||||
if (isWindows) {
|
||||
// Use WQL -Filter for server-side filtering (no $_ pipeline syntax).
|
||||
@@ -475,7 +544,7 @@ export async function aggressiveStartupCleanup(): Promise<void> {
|
||||
|
||||
for (const proc of processList) {
|
||||
const pid = proc.ProcessId;
|
||||
if (!Number.isInteger(pid) || pid <= 0 || pid === currentPid) continue;
|
||||
if (!Number.isInteger(pid) || pid <= 0 || protectedPids.has(pid)) continue;
|
||||
|
||||
const commandLine = proc.CommandLine || '';
|
||||
const isAggressive = AGGRESSIVE_CLEANUP_PATTERNS.some(p => commandLine.includes(p));
|
||||
@@ -518,7 +587,7 @@ export async function aggressiveStartupCleanup(): Promise<void> {
|
||||
const etime = match[2];
|
||||
const command = match[3];
|
||||
|
||||
if (!Number.isInteger(pid) || pid <= 0 || pid === currentPid) continue;
|
||||
if (!Number.isInteger(pid) || pid <= 0 || protectedPids.has(pid)) continue;
|
||||
|
||||
const isAggressive = AGGRESSIVE_CLEANUP_PATTERNS.some(p => command.includes(p));
|
||||
|
||||
@@ -635,16 +704,24 @@ export function spawnDaemon(
|
||||
...extraEnv
|
||||
});
|
||||
|
||||
// worker-service.cjs imports `bun:sqlite`, so the spawned runtime MUST be
|
||||
// Bun on every platform — never the current process.execPath, which may be
|
||||
// Node when the caller is the MCP server. Resolve once before the OS branch
|
||||
// split so we don't pay for a duplicate PATH lookup if Bun isn't found at a
|
||||
// well-known path. See resolveWorkerRuntimePath() for the candidate list.
|
||||
const runtimePath = resolveWorkerRuntimePath();
|
||||
if (!runtimePath) {
|
||||
logger.error(
|
||||
'SYSTEM',
|
||||
'Bun runtime not found — install from https://bun.sh and ensure it is on PATH or set BUN env var. The worker daemon requires Bun because it uses bun:sqlite.'
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (isWindows) {
|
||||
// Use PowerShell Start-Process to spawn a hidden, independent process
|
||||
// Unlike WMIC, PowerShell inherits environment variables from parent
|
||||
// -WindowStyle Hidden prevents console popup
|
||||
const runtimePath = resolveWorkerRuntimePath();
|
||||
|
||||
if (!runtimePath) {
|
||||
logger.error('SYSTEM', 'Failed to locate Bun runtime for Windows worker spawn');
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Use -EncodedCommand to avoid all shell quoting issues with spaces in paths
|
||||
const psScript = `Start-Process -FilePath '${runtimePath.replace(/'/g, "''")}' -ArgumentList @('${scriptPath.replace(/'/g, "''")}','--daemon') -WindowStyle Hidden`;
|
||||
@@ -656,6 +733,13 @@ export function spawnDaemon(
|
||||
windowsHide: true,
|
||||
env
|
||||
});
|
||||
// Windows success sentinel: PowerShell `Start-Process` does not return
|
||||
// the spawned PID, and we don't want to pay for an extra `Get-Process`
|
||||
// round-trip just to discover it. Return 0 (a conventionally invalid
|
||||
// Unix PID) so callers can distinguish "spawn dispatched" from "spawn
|
||||
// failed". Callers MUST use `pid === undefined` to detect failure —
|
||||
// never falsy checks like `if (!pid)`, which would silently treat
|
||||
// success as failure here.
|
||||
return 0;
|
||||
} catch (error) {
|
||||
// APPROVED OVERRIDE: Windows daemon spawn is best-effort; log and let callers fall back to health checks/retry flow.
|
||||
@@ -668,9 +752,10 @@ export function spawnDaemon(
|
||||
// controlling terminal. This prevents SIGHUP from reaching the daemon
|
||||
// even if the in-process SIGHUP handler somehow fails (belt-and-suspenders).
|
||||
// Fall back to standard detached spawn if setsid is not available.
|
||||
// `runtimePath` was resolved at the top of this function (see comment there).
|
||||
const setsidPath = '/usr/bin/setsid';
|
||||
if (existsSync(setsidPath)) {
|
||||
const child = spawn(setsidPath, [process.execPath, scriptPath, '--daemon'], {
|
||||
const child = spawn(setsidPath, [runtimePath, scriptPath, '--daemon'], {
|
||||
detached: true,
|
||||
stdio: 'ignore',
|
||||
env
|
||||
@@ -685,7 +770,7 @@ export function spawnDaemon(
|
||||
}
|
||||
|
||||
// Fallback: standard detached spawn (macOS, systems without setsid)
|
||||
const child = spawn(process.execPath, [scriptPath, '--daemon'], {
|
||||
const child = spawn(runtimePath, [scriptPath, '--daemon'], {
|
||||
detached: true,
|
||||
stdio: 'ignore',
|
||||
env
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*
|
||||
* 1. Writes/merges transcript-watch config to ~/.claude-mem/transcript-watch.json
|
||||
* 2. Sets up watch for ~/.codex/sessions/**\/*.jsonl using existing watcher
|
||||
* 3. Injects context via ~/.codex/AGENTS.md (Codex reads this natively)
|
||||
* 3. Injects context via workspace-local AGENTS.md files (Codex reads these natively)
|
||||
*
|
||||
* Anti-patterns:
|
||||
* - Does NOT add notify hooks -- transcript watching is sufficient
|
||||
@@ -67,7 +67,7 @@ function loadExistingTranscriptWatchConfig(): TranscriptWatchConfig {
|
||||
|
||||
return parsed;
|
||||
} catch (parseError) {
|
||||
logger.error('CODEX', 'Corrupt transcript-watch.json, creating backup', { path: configPath }, parseError as Error);
|
||||
logger.error('SYSTEM', 'Corrupt transcript-watch.json, creating backup', { path: configPath }, parseError as Error);
|
||||
|
||||
// Back up corrupt file
|
||||
const backupPath = `${configPath}.backup.${Date.now()}`;
|
||||
@@ -130,42 +130,10 @@ function writeTranscriptWatchConfig(config: TranscriptWatchConfig): void {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Inject claude-mem context section into ~/.codex/AGENTS.md.
|
||||
* Uses the same <claude-mem-context> tag pattern as CLAUDE.md and GEMINI.md.
|
||||
* Remove legacy claude-mem context from ~/.codex/AGENTS.md.
|
||||
* Codex now uses workspace-local AGENTS.md files to avoid cross-project bleed.
|
||||
* Preserves any existing user content outside the tags.
|
||||
*/
|
||||
function injectCodexAgentsMdContext(): void {
|
||||
try {
|
||||
mkdirSync(CODEX_DIR, { recursive: true });
|
||||
|
||||
let existingContent = '';
|
||||
if (existsSync(CODEX_AGENTS_MD_PATH)) {
|
||||
existingContent = readFileSync(CODEX_AGENTS_MD_PATH, 'utf-8');
|
||||
}
|
||||
|
||||
// Initial placeholder content -- will be populated after first session
|
||||
const contextContent = [
|
||||
'# Recent Activity',
|
||||
'',
|
||||
'<!-- This section is auto-generated by claude-mem. Edit content outside the tags. -->',
|
||||
'',
|
||||
'*No context yet. Complete your first session and context will appear here.*',
|
||||
].join('\n');
|
||||
|
||||
const finalContent = replaceTaggedContent(existingContent, contextContent);
|
||||
writeFileSync(CODEX_AGENTS_MD_PATH, finalContent);
|
||||
console.log(` Injected context placeholder into ${CODEX_AGENTS_MD_PATH}`);
|
||||
} catch (error) {
|
||||
// Non-fatal -- transcript watching still works without context injection
|
||||
logger.warn('CODEX', 'Failed to inject AGENTS.md context', { error: (error as Error).message });
|
||||
console.warn(` Warning: Could not inject context into AGENTS.md: ${(error as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove claude-mem context section from AGENTS.md.
|
||||
* Preserves user content outside the <claude-mem-context> tags.
|
||||
*/
|
||||
function removeCodexAgentsMdContext(): void {
|
||||
try {
|
||||
if (!existsSync(CODEX_AGENTS_MD_PATH)) return;
|
||||
@@ -179,7 +147,6 @@ function removeCodexAgentsMdContext(): void {
|
||||
|
||||
if (startIdx === -1 || endIdx === -1) return;
|
||||
|
||||
// Remove the tagged section and any surrounding blank lines
|
||||
const before = content.substring(0, startIdx).replace(/\n+$/, '');
|
||||
const after = content.substring(endIdx + endTag.length).replace(/^\n+/, '');
|
||||
const finalContent = (before + (after ? '\n\n' + after : '')).trim();
|
||||
@@ -187,17 +154,21 @@ function removeCodexAgentsMdContext(): void {
|
||||
if (finalContent) {
|
||||
writeFileSync(CODEX_AGENTS_MD_PATH, finalContent + '\n');
|
||||
} else {
|
||||
// File would be empty -- leave it empty rather than deleting
|
||||
// (user may have other tooling that expects it to exist)
|
||||
writeFileSync(CODEX_AGENTS_MD_PATH, '');
|
||||
}
|
||||
|
||||
console.log(` Removed context section from ${CODEX_AGENTS_MD_PATH}`);
|
||||
console.log(` Removed legacy global context from ${CODEX_AGENTS_MD_PATH}`);
|
||||
} catch (error) {
|
||||
logger.warn('CODEX', 'Failed to clean AGENTS.md context', { error: (error as Error).message });
|
||||
logger.warn('SYSTEM', 'Failed to clean AGENTS.md context', { error: (error as Error).message });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Codex now uses workspace-local AGENTS.md via transcript processor fallback.
|
||||
* Preserves user content outside the <claude-mem-context> tags.
|
||||
*/
|
||||
const cleanupLegacyCodexAgentsMdContext = removeCodexAgentsMdContext;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public API: Install
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -206,7 +177,7 @@ function removeCodexAgentsMdContext(): void {
|
||||
* Install Codex CLI integration for claude-mem.
|
||||
*
|
||||
* 1. Merges Codex transcript-watch config into ~/.claude-mem/transcript-watch.json
|
||||
* 2. Injects context placeholder into ~/.codex/AGENTS.md
|
||||
* 2. Cleans up any legacy global context block in ~/.codex/AGENTS.md
|
||||
*
|
||||
* @returns 0 on success, 1 on failure
|
||||
*/
|
||||
@@ -222,19 +193,19 @@ export async function installCodexCli(): Promise<number> {
|
||||
console.log(` Watch path: ~/.codex/sessions/**/*.jsonl`);
|
||||
console.log(` Schema: codex (v${SAMPLE_CONFIG.schemas?.codex?.version ?? '?'})`);
|
||||
|
||||
// Step 2: Inject context into AGENTS.md
|
||||
injectCodexAgentsMdContext();
|
||||
// Step 2: Clean up legacy global AGENTS.md context
|
||||
cleanupLegacyCodexAgentsMdContext();
|
||||
|
||||
console.log(`
|
||||
Installation complete!
|
||||
|
||||
Transcript watch config: ${DEFAULT_CONFIG_PATH}
|
||||
Context file: ${CODEX_AGENTS_MD_PATH}
|
||||
Context files: <workspace>/AGENTS.md
|
||||
|
||||
How it works:
|
||||
- claude-mem watches Codex session JSONL files for new activity
|
||||
- No hooks needed -- transcript watching is fully automatic
|
||||
- Context from past sessions is injected via ${CODEX_AGENTS_MD_PATH}
|
||||
- Context from past sessions is injected via AGENTS.md in the active Codex workspace
|
||||
|
||||
Next steps:
|
||||
1. Start claude-mem worker: npx claude-mem start
|
||||
@@ -284,8 +255,8 @@ export function uninstallCodexCli(): number {
|
||||
console.log(' No transcript-watch.json found -- nothing to remove.');
|
||||
}
|
||||
|
||||
// Step 2: Remove context section from AGENTS.md
|
||||
removeCodexAgentsMdContext();
|
||||
// Step 2: Remove legacy global context section from AGENTS.md
|
||||
cleanupLegacyCodexAgentsMdContext();
|
||||
|
||||
console.log('\nUninstallation complete!');
|
||||
console.log('Restart claude-mem worker to apply changes.\n');
|
||||
@@ -340,20 +311,20 @@ export function checkCodexCliStatus(): number {
|
||||
// Check context config
|
||||
if (codexWatch.context) {
|
||||
console.log(` Context mode: ${codexWatch.context.mode}`);
|
||||
console.log(` Context path: ${codexWatch.context.path ?? 'default'}`);
|
||||
console.log(` Context path: ${codexWatch.context.path ?? '<workspace>/AGENTS.md (default)'}`);
|
||||
console.log(` Context updates on: ${codexWatch.context.updateOn?.join(', ') ?? 'none'}`);
|
||||
}
|
||||
|
||||
// Check AGENTS.md
|
||||
// Check legacy global AGENTS.md usage
|
||||
if (existsSync(CODEX_AGENTS_MD_PATH)) {
|
||||
const mdContent = readFileSync(CODEX_AGENTS_MD_PATH, 'utf-8');
|
||||
if (mdContent.includes('<claude-mem-context>')) {
|
||||
console.log(` Context: Active (${CODEX_AGENTS_MD_PATH})`);
|
||||
console.log(` Legacy global context: Present (${CODEX_AGENTS_MD_PATH})`);
|
||||
} else {
|
||||
console.log(` Context: AGENTS.md exists but no context tags`);
|
||||
console.log(` Legacy global context: Not active`);
|
||||
}
|
||||
} else {
|
||||
console.log(` Context: No AGENTS.md file`);
|
||||
console.log(` Legacy global context: None`);
|
||||
}
|
||||
|
||||
// Check if ~/.codex/sessions exists (indicates Codex has been used)
|
||||
|
||||
@@ -80,7 +80,7 @@ const HOOK_TIMEOUT_MS = 10000;
|
||||
*/
|
||||
const GEMINI_EVENT_TO_INTERNAL_EVENT: Record<string, string> = {
|
||||
'SessionStart': 'context',
|
||||
'BeforeAgent': 'user-message',
|
||||
'BeforeAgent': 'session-init',
|
||||
'AfterAgent': 'observation',
|
||||
'BeforeTool': 'observation',
|
||||
'AfterTool': 'observation',
|
||||
|
||||
@@ -3,13 +3,15 @@
|
||||
*
|
||||
* No native bindings. No WASM. Just the CLI binary + query patterns.
|
||||
*
|
||||
* Supported: JS, TS, Python, Go, Rust, Ruby, Java, C, C++
|
||||
* Supported: JS, TS, Python, Go, Rust, Ruby, Java, C, C++,
|
||||
* Kotlin, Swift, PHP, Elixir, Lua, Scala, Bash, Haskell, Zig,
|
||||
* CSS, SCSS, TOML, YAML, SQL, Markdown
|
||||
*
|
||||
* by Copter Labs
|
||||
*/
|
||||
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { writeFileSync, mkdtempSync, rmSync, existsSync } from "node:fs";
|
||||
import { writeFileSync, readFileSync, mkdtempSync, rmSync, existsSync } from "node:fs";
|
||||
import { join, dirname } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { createRequire } from "node:module";
|
||||
@@ -25,7 +27,7 @@ const _require = typeof __filename !== 'undefined'
|
||||
|
||||
export interface CodeSymbol {
|
||||
name: string;
|
||||
kind: "function" | "class" | "method" | "interface" | "type" | "const" | "variable" | "export" | "struct" | "enum" | "trait" | "impl" | "property" | "getter" | "setter";
|
||||
kind: "function" | "class" | "method" | "interface" | "type" | "const" | "variable" | "export" | "struct" | "enum" | "trait" | "impl" | "property" | "getter" | "setter" | "mixin" | "section" | "code" | "metadata" | "reference";
|
||||
signature: string;
|
||||
jsdoc?: string;
|
||||
lineStart: number;
|
||||
@@ -66,6 +68,28 @@ const LANG_MAP: Record<string, string> = {
|
||||
".cxx": "cpp",
|
||||
".hpp": "cpp",
|
||||
".hh": "cpp",
|
||||
".kt": "kotlin",
|
||||
".kts": "kotlin",
|
||||
".swift": "swift",
|
||||
".php": "php",
|
||||
".ex": "elixir",
|
||||
".exs": "elixir",
|
||||
".lua": "lua",
|
||||
".scala": "scala",
|
||||
".sc": "scala",
|
||||
".sh": "bash",
|
||||
".bash": "bash",
|
||||
".zsh": "bash",
|
||||
".hs": "haskell",
|
||||
".zig": "zig",
|
||||
".css": "css",
|
||||
".scss": "scss",
|
||||
".toml": "toml",
|
||||
".yml": "yaml",
|
||||
".yaml": "yaml",
|
||||
".sql": "sql",
|
||||
".md": "markdown",
|
||||
".mdx": "markdown",
|
||||
};
|
||||
|
||||
export function detectLanguage(filePath: string): string {
|
||||
@@ -73,6 +97,135 @@ export function detectLanguage(filePath: string): string {
|
||||
return LANG_MAP[ext] || "unknown";
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect language with fallback to user-configured grammar extensions.
|
||||
* Bundled LANG_MAP takes priority.
|
||||
*/
|
||||
function detectLanguageWithUserGrammars(filePath: string, userConfig: UserGrammarConfig): string {
|
||||
const ext = filePath.slice(filePath.lastIndexOf("."));
|
||||
if (LANG_MAP[ext]) return LANG_MAP[ext];
|
||||
if (userConfig.extensionToLanguage[ext]) return userConfig.extensionToLanguage[ext];
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the query key for a language, checking user config for custom queries.
|
||||
*/
|
||||
function getUserAwareQueryKey(language: string, userConfig: UserGrammarConfig): string {
|
||||
// If user config has a specific query key for this language, use it
|
||||
if (userConfig.languageToQueryKey[language]) {
|
||||
return userConfig.languageToQueryKey[language];
|
||||
}
|
||||
// Otherwise fall back to the bundled query key mapping
|
||||
return getQueryKey(language);
|
||||
}
|
||||
|
||||
// --- User-installable grammars via .claude-mem.json ---
|
||||
|
||||
export interface UserGrammarEntry {
|
||||
package: string;
|
||||
extensions: string[];
|
||||
query?: string;
|
||||
}
|
||||
|
||||
export interface UserGrammarConfig {
|
||||
/** language name → grammar entry */
|
||||
grammars: Record<string, UserGrammarEntry>;
|
||||
/** file extension → language name (for user-defined extensions only) */
|
||||
extensionToLanguage: Record<string, string>;
|
||||
/** language name → query content (custom .scm file content or "generic") */
|
||||
languageToQueryKey: Record<string, string>;
|
||||
}
|
||||
|
||||
const userGrammarCache = new Map<string, UserGrammarConfig>();
|
||||
|
||||
const EMPTY_USER_GRAMMAR_CONFIG: UserGrammarConfig = {
|
||||
grammars: {},
|
||||
extensionToLanguage: {},
|
||||
languageToQueryKey: {},
|
||||
};
|
||||
|
||||
/**
|
||||
* Load user grammar configuration from .claude-mem.json in a project root.
|
||||
* Cached per project root. Returns empty config if file doesn't exist or is invalid.
|
||||
* User entries do NOT override bundled grammars.
|
||||
*/
|
||||
export function loadUserGrammars(projectRoot: string): UserGrammarConfig {
|
||||
if (userGrammarCache.has(projectRoot)) return userGrammarCache.get(projectRoot)!;
|
||||
|
||||
const configPath = join(projectRoot, ".claude-mem.json");
|
||||
let rawConfig: Record<string, unknown>;
|
||||
|
||||
try {
|
||||
const content = readFileSync(configPath, "utf-8");
|
||||
rawConfig = JSON.parse(content);
|
||||
} catch {
|
||||
userGrammarCache.set(projectRoot, EMPTY_USER_GRAMMAR_CONFIG);
|
||||
return EMPTY_USER_GRAMMAR_CONFIG;
|
||||
}
|
||||
|
||||
const grammarsRaw = rawConfig.grammars;
|
||||
if (!grammarsRaw || typeof grammarsRaw !== "object" || Array.isArray(grammarsRaw)) {
|
||||
userGrammarCache.set(projectRoot, EMPTY_USER_GRAMMAR_CONFIG);
|
||||
return EMPTY_USER_GRAMMAR_CONFIG;
|
||||
}
|
||||
|
||||
const config: UserGrammarConfig = {
|
||||
grammars: {},
|
||||
extensionToLanguage: {},
|
||||
languageToQueryKey: {},
|
||||
};
|
||||
|
||||
for (const [language, entry] of Object.entries(grammarsRaw as Record<string, unknown>)) {
|
||||
// Skip if this language is already bundled
|
||||
if (GRAMMAR_PACKAGES[language]) continue;
|
||||
|
||||
if (!entry || typeof entry !== "object" || Array.isArray(entry)) continue;
|
||||
const typedEntry = entry as Record<string, unknown>;
|
||||
|
||||
const pkg = typedEntry.package;
|
||||
const extensions = typedEntry.extensions;
|
||||
const queryPath = typedEntry.query;
|
||||
|
||||
// Validate required fields
|
||||
if (typeof pkg !== "string" || !Array.isArray(extensions)) continue;
|
||||
if (!extensions.every((e: unknown) => typeof e === "string")) continue;
|
||||
|
||||
config.grammars[language] = {
|
||||
package: pkg,
|
||||
extensions: extensions as string[],
|
||||
query: typeof queryPath === "string" ? queryPath : undefined,
|
||||
};
|
||||
|
||||
// Map extensions to language (skip extensions already handled by bundled LANG_MAP)
|
||||
for (const ext of extensions as string[]) {
|
||||
if (!LANG_MAP[ext]) {
|
||||
config.extensionToLanguage[ext] = language;
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve query content
|
||||
if (typeof queryPath === "string") {
|
||||
const fullQueryPath = join(projectRoot, queryPath);
|
||||
try {
|
||||
const queryContent = readFileSync(fullQueryPath, "utf-8");
|
||||
// Store with a unique key to avoid collisions with built-in queries
|
||||
const queryKey = `user_${language}`;
|
||||
QUERIES[queryKey] = queryContent;
|
||||
config.languageToQueryKey[language] = queryKey;
|
||||
} catch {
|
||||
console.error(`[smart-file-read] Custom query file not found: ${fullQueryPath}, falling back to generic`);
|
||||
config.languageToQueryKey[language] = "generic";
|
||||
}
|
||||
} else {
|
||||
config.languageToQueryKey[language] = "generic";
|
||||
}
|
||||
}
|
||||
|
||||
userGrammarCache.set(projectRoot, config);
|
||||
return config;
|
||||
}
|
||||
|
||||
// --- Grammar path resolution ---
|
||||
|
||||
const GRAMMAR_PACKAGES: Record<string, string> = {
|
||||
@@ -86,11 +239,45 @@ const GRAMMAR_PACKAGES: Record<string, string> = {
|
||||
java: "tree-sitter-java",
|
||||
c: "tree-sitter-c",
|
||||
cpp: "tree-sitter-cpp",
|
||||
kotlin: "tree-sitter-kotlin",
|
||||
swift: "tree-sitter-swift",
|
||||
php: "tree-sitter-php/php",
|
||||
elixir: "tree-sitter-elixir",
|
||||
lua: "@tree-sitter-grammars/tree-sitter-lua",
|
||||
scala: "tree-sitter-scala",
|
||||
bash: "tree-sitter-bash",
|
||||
haskell: "tree-sitter-haskell",
|
||||
zig: "@tree-sitter-grammars/tree-sitter-zig",
|
||||
css: "tree-sitter-css",
|
||||
scss: "tree-sitter-scss",
|
||||
toml: "@tree-sitter-grammars/tree-sitter-toml",
|
||||
yaml: "@tree-sitter-grammars/tree-sitter-yaml",
|
||||
sql: "@derekstride/tree-sitter-sql",
|
||||
markdown: "@tree-sitter-grammars/tree-sitter-markdown",
|
||||
};
|
||||
|
||||
// Grammars where the parser source lives in a subdirectory of the npm package root,
|
||||
// AND that subdirectory lacks its own package.json (so require.resolve won't find it).
|
||||
// Maps language → subdirectory name under the package root.
|
||||
const GRAMMAR_SUBDIR: Record<string, string> = {
|
||||
markdown: "tree-sitter-markdown",
|
||||
};
|
||||
|
||||
function resolveGrammarPath(language: string): string | null {
|
||||
const pkg = GRAMMAR_PACKAGES[language];
|
||||
if (!pkg) return null;
|
||||
|
||||
const subdir = GRAMMAR_SUBDIR[language];
|
||||
if (subdir) {
|
||||
// Package root has no sub-package.json — resolve root then append subdir
|
||||
try {
|
||||
const rootPkgPath = _require.resolve(pkg + "/package.json");
|
||||
const resolved = join(dirname(rootPkgPath), subdir);
|
||||
if (existsSync(join(resolved, "src"))) return resolved;
|
||||
} catch { /* fall through */ }
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const packageJsonPath = _require.resolve(pkg + "/package.json");
|
||||
return dirname(packageJsonPath);
|
||||
@@ -99,6 +286,37 @@ function resolveGrammarPath(language: string): string | null {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve grammar path with fallback to user-installed grammars.
|
||||
* First tries bundled grammars, then falls back to the project's node_modules.
|
||||
*/
|
||||
export function resolveGrammarPathWithFallback(language: string, projectRoot?: string): string | null {
|
||||
// Try bundled grammar first
|
||||
const bundled = resolveGrammarPath(language);
|
||||
if (bundled) return bundled;
|
||||
|
||||
// Fall back to user-installed grammar in project's node_modules
|
||||
if (!projectRoot) return null;
|
||||
|
||||
const userConfig = loadUserGrammars(projectRoot);
|
||||
const entry = userConfig.grammars[language];
|
||||
if (!entry) return null;
|
||||
|
||||
try {
|
||||
const packageJsonPath = join(projectRoot, "node_modules", entry.package, "package.json");
|
||||
if (existsSync(packageJsonPath)) {
|
||||
const grammarDir = dirname(packageJsonPath);
|
||||
// Verify it has a src/ directory (required by tree-sitter CLI)
|
||||
if (existsSync(join(grammarDir, "src"))) return grammarDir;
|
||||
}
|
||||
} catch {
|
||||
// Grammar package not installed
|
||||
}
|
||||
|
||||
console.error(`[smart-file-read] Grammar package not found for "${language}": ${entry.package} (install it in your project's node_modules)`);
|
||||
return null;
|
||||
}
|
||||
|
||||
// --- Query patterns (declarative symbol extraction) ---
|
||||
|
||||
const QUERIES: Record<string, string> = {
|
||||
@@ -150,6 +368,104 @@ const QUERIES: Record<string, string> = {
|
||||
(interface_declaration name: (identifier) @name) @iface
|
||||
(enum_declaration name: (identifier) @name) @enm
|
||||
(import_declaration) @imp
|
||||
`,
|
||||
|
||||
kotlin: `
|
||||
(function_declaration (simple_identifier) @name) @func
|
||||
(class_declaration (type_identifier) @name) @cls
|
||||
(object_declaration (type_identifier) @name) @cls
|
||||
(import_header) @imp
|
||||
`,
|
||||
|
||||
swift: `
|
||||
(function_declaration name: (simple_identifier) @name) @func
|
||||
(class_declaration name: (type_identifier) @name) @cls
|
||||
(protocol_declaration name: (type_identifier) @name) @iface
|
||||
(import_declaration) @imp
|
||||
`,
|
||||
|
||||
php: `
|
||||
(function_definition name: (name) @name) @func
|
||||
(class_declaration name: (name) @name) @cls
|
||||
(interface_declaration name: (name) @name) @iface
|
||||
(trait_declaration name: (name) @name) @trait_def
|
||||
(method_declaration name: (name) @name) @method
|
||||
(namespace_use_declaration) @imp
|
||||
`,
|
||||
|
||||
lua: `
|
||||
(function_declaration name: (identifier) @name) @func
|
||||
(function_declaration name: (dot_index_expression) @name) @func
|
||||
(function_declaration name: (method_index_expression) @name) @func
|
||||
`,
|
||||
|
||||
scala: `
|
||||
(function_definition name: (identifier) @name) @func
|
||||
(class_definition name: (identifier) @name) @cls
|
||||
(object_definition name: (identifier) @name) @cls
|
||||
(trait_definition name: (identifier) @name) @trait_def
|
||||
(import_declaration) @imp
|
||||
`,
|
||||
|
||||
bash: `
|
||||
(function_definition name: (word) @name) @func
|
||||
`,
|
||||
|
||||
haskell: `
|
||||
(function name: (variable) @name) @func
|
||||
(type_synomym name: (name) @name) @tdef
|
||||
(newtype name: (name) @name) @tdef
|
||||
(data_type name: (name) @name) @tdef
|
||||
(class name: (name) @name) @cls
|
||||
(import) @imp
|
||||
`,
|
||||
|
||||
zig: `
|
||||
(function_declaration name: (identifier) @name) @func
|
||||
(test_declaration) @func
|
||||
`,
|
||||
|
||||
css: `
|
||||
(rule_set (selectors) @name) @func
|
||||
(media_statement) @cls
|
||||
(keyframes_statement (keyframes_name) @name) @cls
|
||||
(import_statement) @imp
|
||||
`,
|
||||
|
||||
scss: `
|
||||
(rule_set (selectors) @name) @func
|
||||
(media_statement) @cls
|
||||
(keyframes_statement (keyframes_name) @name) @cls
|
||||
(import_statement) @imp
|
||||
(mixin_statement name: (identifier) @name) @mixin_def
|
||||
(function_statement name: (identifier) @name) @func
|
||||
(include_statement) @imp
|
||||
`,
|
||||
|
||||
toml: `
|
||||
(table (bare_key) @name) @cls
|
||||
(table (dotted_key) @name) @cls
|
||||
(table_array_element (bare_key) @name) @cls
|
||||
(table_array_element (dotted_key) @name) @cls
|
||||
`,
|
||||
|
||||
yaml: `
|
||||
(block_mapping_pair key: (flow_node) @name) @func
|
||||
`,
|
||||
|
||||
sql: `
|
||||
(create_table (object_reference) @name) @cls
|
||||
(create_function (object_reference) @name) @func
|
||||
(create_view (object_reference) @name) @cls
|
||||
`,
|
||||
|
||||
markdown: `
|
||||
(atx_heading heading_content: (inline) @name) @heading
|
||||
(setext_heading heading_content: (paragraph) @name) @heading
|
||||
(fenced_code_block (info_string (language) @name)) @code_block
|
||||
(fenced_code_block) @code_block
|
||||
(minus_metadata) @frontmatter
|
||||
(link_reference_definition (link_label) @name) @ref
|
||||
`,
|
||||
|
||||
generic: `
|
||||
@@ -159,6 +475,15 @@ const QUERIES: Record<string, string> = {
|
||||
(class_definition name: (identifier) @name) @cls
|
||||
(import_statement) @imp
|
||||
(import_declaration) @imp
|
||||
`,
|
||||
|
||||
php: `
|
||||
(function_definition name: (name) @name) @func
|
||||
(method_declaration name: (name) @name) @method
|
||||
(class_declaration name: (name) @name) @cls
|
||||
(interface_declaration name: (name) @name) @iface
|
||||
(trait_declaration name: (name) @name) @trait_def
|
||||
(namespace_use_declaration) @imp
|
||||
`,
|
||||
};
|
||||
|
||||
@@ -173,6 +498,21 @@ function getQueryKey(language: string): string {
|
||||
case "rust": return "rust";
|
||||
case "ruby": return "ruby";
|
||||
case "java": return "java";
|
||||
case "kotlin": return "kotlin";
|
||||
case "swift": return "swift";
|
||||
case "php": return "php";
|
||||
case "elixir": return "generic";
|
||||
case "lua": return "lua";
|
||||
case "scala": return "scala";
|
||||
case "bash": return "bash";
|
||||
case "haskell": return "haskell";
|
||||
case "zig": return "zig";
|
||||
case "css": return "css";
|
||||
case "scss": return "scss";
|
||||
case "toml": return "toml";
|
||||
case "yaml": return "yaml";
|
||||
case "sql": return "sql";
|
||||
case "markdown": return "markdown";
|
||||
default: return "generic";
|
||||
}
|
||||
}
|
||||
@@ -308,6 +648,11 @@ const KIND_MAP: Record<string, CodeSymbol["kind"]> = {
|
||||
struct_def: "struct",
|
||||
trait_def: "trait",
|
||||
impl_def: "impl",
|
||||
mixin_def: "mixin",
|
||||
heading: "section",
|
||||
code_block: "code",
|
||||
frontmatter: "metadata",
|
||||
ref: "reference",
|
||||
};
|
||||
|
||||
const CONTAINER_KINDS = new Set(["class", "struct", "impl", "trait"]);
|
||||
@@ -407,18 +752,36 @@ function buildSymbols(matches: RawMatch[], lines: string[], language: string): {
|
||||
const nameCapture = match.captures.find(c => c.tag === "name");
|
||||
if (!kindCapture) continue;
|
||||
|
||||
const name = nameCapture?.text || "anonymous";
|
||||
const startRow = kindCapture.startRow;
|
||||
const endRow = kindCapture.endRow;
|
||||
const kind = KIND_MAP[kindCapture.tag];
|
||||
const name = nameCapture?.text || "anonymous";
|
||||
|
||||
const comment = findCommentAbove(lines, startRow);
|
||||
// Markdown-specific: extract heading level and build signature
|
||||
let signature: string;
|
||||
if (language === "markdown" && kind === "section") {
|
||||
const headingLine = lines[startRow] || "";
|
||||
const hashMatch = headingLine.match(/^(#{1,6})\s/);
|
||||
const level = hashMatch ? hashMatch[1].length : 1;
|
||||
signature = `${"#".repeat(level)} ${name}`;
|
||||
} else if (language === "markdown" && kind === "code") {
|
||||
const langTag = name !== "anonymous" ? name : "";
|
||||
signature = langTag ? "```" + langTag : "```";
|
||||
} else if (language === "markdown" && kind === "metadata") {
|
||||
signature = "---frontmatter---";
|
||||
} else if (language === "markdown" && kind === "reference") {
|
||||
signature = lines[startRow]?.trim() || name;
|
||||
} else {
|
||||
signature = extractSignatureFromLines(lines, startRow, endRow);
|
||||
}
|
||||
|
||||
const comment = language === "markdown" ? undefined : findCommentAbove(lines, startRow);
|
||||
const docstring = language === "python" ? findPythonDocstringFromLines(lines, startRow, endRow) : undefined;
|
||||
|
||||
const sym: CodeSymbol = {
|
||||
name,
|
||||
kind,
|
||||
signature: extractSignatureFromLines(lines, startRow, endRow),
|
||||
signature,
|
||||
jsdoc: comment || docstring,
|
||||
lineStart: startRow,
|
||||
lineEnd: endRow,
|
||||
@@ -433,6 +796,34 @@ function buildSymbols(matches: RawMatch[], lines: string[], language: string): {
|
||||
symbols.push(sym);
|
||||
}
|
||||
|
||||
// Markdown: deduplicate code_block matches. The catch-all `(fenced_code_block) @code_block`
|
||||
// pattern and the language-specific pattern both match the same block. Keep the named one.
|
||||
if (language === "markdown") {
|
||||
const codeBlocksByRange = new Map<string, CodeSymbol>();
|
||||
const duplicateCodeBlocks = new Set<CodeSymbol>();
|
||||
for (const sym of symbols) {
|
||||
if (sym.kind !== "code") continue;
|
||||
const rangeKey = `${sym.lineStart}:${sym.lineEnd}`;
|
||||
const existing = codeBlocksByRange.get(rangeKey);
|
||||
if (existing) {
|
||||
// Prefer the named version (has actual language tag vs "anonymous")
|
||||
if (sym.name !== "anonymous") {
|
||||
duplicateCodeBlocks.add(existing);
|
||||
codeBlocksByRange.set(rangeKey, sym);
|
||||
} else {
|
||||
duplicateCodeBlocks.add(sym);
|
||||
}
|
||||
} else {
|
||||
codeBlocksByRange.set(rangeKey, sym);
|
||||
}
|
||||
}
|
||||
if (duplicateCodeBlocks.size > 0) {
|
||||
const filtered = symbols.filter(s => !duplicateCodeBlocks.has(s));
|
||||
symbols.length = 0;
|
||||
symbols.push(...filtered);
|
||||
}
|
||||
}
|
||||
|
||||
// Nest methods inside containers
|
||||
const nested = new Set<CodeSymbol>();
|
||||
for (const container of containers) {
|
||||
@@ -451,11 +842,12 @@ function buildSymbols(matches: RawMatch[], lines: string[], language: string): {
|
||||
|
||||
// --- Main parse functions ---
|
||||
|
||||
export function parseFile(content: string, filePath: string): FoldedFile {
|
||||
const language = detectLanguage(filePath);
|
||||
export function parseFile(content: string, filePath: string, projectRoot?: string): FoldedFile {
|
||||
const userConfig = projectRoot ? loadUserGrammars(projectRoot) : EMPTY_USER_GRAMMAR_CONFIG;
|
||||
const language = detectLanguageWithUserGrammars(filePath, userConfig);
|
||||
const lines = content.split("\n");
|
||||
|
||||
const grammarPath = resolveGrammarPath(language);
|
||||
const grammarPath = resolveGrammarPathWithFallback(language, projectRoot);
|
||||
if (!grammarPath) {
|
||||
return {
|
||||
filePath, language, symbols: [], imports: [],
|
||||
@@ -463,7 +855,7 @@ export function parseFile(content: string, filePath: string): FoldedFile {
|
||||
};
|
||||
}
|
||||
|
||||
const queryKey = getQueryKey(language);
|
||||
const queryKey = getUserAwareQueryKey(language, userConfig);
|
||||
const queryFile = getQueryFile(queryKey);
|
||||
|
||||
// Write content to temp file with correct extension for language detection
|
||||
@@ -498,20 +890,22 @@ export function parseFile(content: string, filePath: string): FoldedFile {
|
||||
* Much faster than calling parseFile() per file (one process spawn per language vs per file).
|
||||
*/
|
||||
export function parseFilesBatch(
|
||||
files: Array<{ absolutePath: string; relativePath: string; content: string }>
|
||||
files: Array<{ absolutePath: string; relativePath: string; content: string }>,
|
||||
projectRoot?: string
|
||||
): Map<string, FoldedFile> {
|
||||
const results = new Map<string, FoldedFile>();
|
||||
const userConfig = projectRoot ? loadUserGrammars(projectRoot) : EMPTY_USER_GRAMMAR_CONFIG;
|
||||
|
||||
// Group files by language (and thus by query + grammar)
|
||||
const languageGroups = new Map<string, typeof files>();
|
||||
for (const file of files) {
|
||||
const language = detectLanguage(file.relativePath);
|
||||
const language = detectLanguageWithUserGrammars(file.relativePath, userConfig);
|
||||
if (!languageGroups.has(language)) languageGroups.set(language, []);
|
||||
languageGroups.get(language)!.push(file);
|
||||
}
|
||||
|
||||
for (const [language, groupFiles] of languageGroups) {
|
||||
const grammarPath = resolveGrammarPath(language);
|
||||
const grammarPath = resolveGrammarPathWithFallback(language, projectRoot);
|
||||
if (!grammarPath) {
|
||||
// No grammar — return empty results for these files
|
||||
for (const file of groupFiles) {
|
||||
@@ -524,7 +918,7 @@ export function parseFilesBatch(
|
||||
continue;
|
||||
}
|
||||
|
||||
const queryKey = getQueryKey(language);
|
||||
const queryKey = getUserAwareQueryKey(language, userConfig);
|
||||
const queryFile = getQueryFile(queryKey);
|
||||
|
||||
// Run one batch query for all files of this language
|
||||
@@ -558,6 +952,10 @@ export function parseFilesBatch(
|
||||
// --- Formatting ---
|
||||
|
||||
export function formatFoldedView(file: FoldedFile): string {
|
||||
if (file.language === "markdown") {
|
||||
return formatMarkdownFoldedView(file);
|
||||
}
|
||||
|
||||
const parts: string[] = [];
|
||||
|
||||
parts.push(`📁 ${file.filePath} (${file.language}, ${file.totalLines} lines)`);
|
||||
@@ -581,6 +979,64 @@ export function formatFoldedView(file: FoldedFile): string {
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
function formatMarkdownFoldedView(file: FoldedFile): string {
|
||||
const parts: string[] = [];
|
||||
// Total width for the content column (before the line range)
|
||||
const COL_WIDTH = 56;
|
||||
|
||||
parts.push(`📄 ${file.filePath} (${file.language}, ${file.totalLines} lines)`);
|
||||
|
||||
for (const sym of file.symbols) {
|
||||
if (sym.kind === "section") {
|
||||
// Extract heading level from the signature (count leading # characters)
|
||||
const hashMatch = sym.signature.match(/^(#{1,6})\s/);
|
||||
const level = hashMatch ? hashMatch[1].length : 1;
|
||||
const indent = " ".repeat(level);
|
||||
const lineRange = `L${sym.lineStart + 1}`;
|
||||
const content = `${indent}${sym.signature}`;
|
||||
parts.push(`${content.padEnd(COL_WIDTH)}${lineRange}`);
|
||||
} else if (sym.kind === "code") {
|
||||
// Find containing heading level for indentation
|
||||
const containingLevel = findContainingHeadingLevel(file.symbols, sym.lineStart);
|
||||
const indent = " ".repeat(containingLevel + 1);
|
||||
const lineRange = sym.lineStart === sym.lineEnd
|
||||
? `L${sym.lineStart + 1}`
|
||||
: `L${sym.lineStart + 1}-${sym.lineEnd + 1}`;
|
||||
const content = `${indent}${sym.signature}`;
|
||||
parts.push(`${content.padEnd(COL_WIDTH)}${lineRange}`);
|
||||
} else if (sym.kind === "metadata") {
|
||||
const lineRange = sym.lineStart === sym.lineEnd
|
||||
? `L${sym.lineStart + 1}`
|
||||
: `L${sym.lineStart + 1}-${sym.lineEnd + 1}`;
|
||||
const content = ` ${sym.signature}`;
|
||||
parts.push(`${content.padEnd(COL_WIDTH)}${lineRange}`);
|
||||
} else if (sym.kind === "reference") {
|
||||
const containingLevel = findContainingHeadingLevel(file.symbols, sym.lineStart);
|
||||
const indent = " ".repeat(containingLevel + 1);
|
||||
const lineRange = `L${sym.lineStart + 1}`;
|
||||
const content = `${indent}↗ ${sym.name}`;
|
||||
parts.push(`${content.padEnd(COL_WIDTH)}${lineRange}`);
|
||||
}
|
||||
}
|
||||
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the heading level of the most recent section heading before the given line.
|
||||
* Returns 0 if no heading precedes the line.
|
||||
*/
|
||||
function findContainingHeadingLevel(symbols: CodeSymbol[], lineStart: number): number {
|
||||
let bestLevel = 0;
|
||||
for (const sym of symbols) {
|
||||
if (sym.kind === "section" && sym.lineStart < lineStart) {
|
||||
const hashMatch = sym.signature.match(/^(#{1,6})\s/);
|
||||
bestLevel = hashMatch ? hashMatch[1].length : 1;
|
||||
}
|
||||
}
|
||||
return bestLevel;
|
||||
}
|
||||
|
||||
function formatSymbol(sym: CodeSymbol, indent: string): string {
|
||||
const parts: string[] = [];
|
||||
|
||||
@@ -621,7 +1077,8 @@ function getSymbolIcon(kind: CodeSymbol["kind"]): string {
|
||||
function: "ƒ", method: "ƒ", class: "◆", interface: "◇",
|
||||
type: "◇", const: "●", variable: "○", export: "→",
|
||||
struct: "◆", enum: "▣", trait: "◇", impl: "◈",
|
||||
property: "○", getter: "⇢", setter: "⇠",
|
||||
property: "○", getter: "⇢", setter: "⇠", mixin: "◈",
|
||||
section: "§", code: "⌘", metadata: "◊", reference: "↗",
|
||||
};
|
||||
return icons[kind] || "·";
|
||||
}
|
||||
@@ -647,6 +1104,31 @@ export function unfoldSymbol(content: string, filePath: string, symbolName: stri
|
||||
|
||||
const lines = content.split("\n");
|
||||
|
||||
// Markdown section unfold: return from heading to next heading of same or higher level
|
||||
if (file.language === "markdown" && symbol.kind === "section") {
|
||||
const hashMatch = symbol.signature.match(/^(#{1,6})\s/);
|
||||
const level = hashMatch ? hashMatch[1].length : 1;
|
||||
const start = symbol.lineStart;
|
||||
|
||||
// Find the next heading at same or higher (lower number) level
|
||||
let end = lines.length - 1;
|
||||
for (const sym of file.symbols) {
|
||||
if (sym.kind === "section" && sym.lineStart > start) {
|
||||
const otherHashMatch = sym.signature.match(/^(#{1,6})\s/);
|
||||
const otherLevel = otherHashMatch ? otherHashMatch[1].length : 1;
|
||||
if (otherLevel <= level) {
|
||||
end = sym.lineStart - 1;
|
||||
// Trim trailing blank lines
|
||||
while (end > start && lines[end].trim() === "") end--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const extracted = lines.slice(start, end + 1).join("\n");
|
||||
return `<!-- 📍 ${filePath} L${start + 1}-${end + 1} -->\n${extracted}`;
|
||||
}
|
||||
|
||||
// Include preceding comments/decorators
|
||||
let start = symbol.lineStart;
|
||||
for (let i = symbol.lineStart - 1; i >= 0; i--) {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
import { readFile, readdir, stat } from "node:fs/promises";
|
||||
import { join, relative } from "node:path";
|
||||
import { parseFilesBatch, formatFoldedView, type FoldedFile } from "./parser.js";
|
||||
import { parseFilesBatch, formatFoldedView, loadUserGrammars, type FoldedFile } from "./parser.js";
|
||||
|
||||
const CODE_EXTENSIONS = new Set([
|
||||
".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs",
|
||||
@@ -22,11 +22,22 @@ const CODE_EXTENSIONS = new Set([
|
||||
".rb",
|
||||
".java",
|
||||
".cs",
|
||||
".cpp", ".c", ".h", ".hpp",
|
||||
".cpp", ".cc", ".cxx", ".c", ".h", ".hpp", ".hh",
|
||||
".swift",
|
||||
".kt",
|
||||
".kt", ".kts",
|
||||
".php",
|
||||
".vue", ".svelte",
|
||||
".ex", ".exs",
|
||||
".lua",
|
||||
".scala", ".sc",
|
||||
".sh", ".bash", ".zsh",
|
||||
".hs",
|
||||
".zig",
|
||||
".css", ".scss",
|
||||
".toml",
|
||||
".yml", ".yaml",
|
||||
".sql",
|
||||
".md", ".mdx",
|
||||
]);
|
||||
|
||||
const IGNORE_DIRS = new Set([
|
||||
@@ -59,8 +70,9 @@ export interface SymbolMatch {
|
||||
|
||||
/**
|
||||
* Walk a directory recursively, yielding file paths.
|
||||
* extraExtensions: additional file extensions to include (from user grammar config).
|
||||
*/
|
||||
async function* walkDir(dir: string, rootDir: string, maxDepth: number = 20): AsyncGenerator<string> {
|
||||
async function* walkDir(dir: string, rootDir: string, maxDepth: number = 20, extraExtensions?: Set<string>): AsyncGenerator<string> {
|
||||
if (maxDepth <= 0) return;
|
||||
|
||||
let entries;
|
||||
@@ -77,10 +89,10 @@ async function* walkDir(dir: string, rootDir: string, maxDepth: number = 20): As
|
||||
const fullPath = join(dir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
yield* walkDir(fullPath, rootDir, maxDepth - 1);
|
||||
yield* walkDir(fullPath, rootDir, maxDepth - 1, extraExtensions);
|
||||
} else if (entry.isFile()) {
|
||||
const ext = entry.name.slice(entry.name.lastIndexOf("."));
|
||||
if (CODE_EXTENSIONS.has(ext)) {
|
||||
if (CODE_EXTENSIONS.has(ext) || (extraExtensions && extraExtensions.has(ext))) {
|
||||
yield fullPath;
|
||||
}
|
||||
}
|
||||
@@ -121,16 +133,29 @@ export async function searchCodebase(
|
||||
maxResults?: number;
|
||||
includeImports?: boolean;
|
||||
filePattern?: string;
|
||||
projectRoot?: string;
|
||||
} = {}
|
||||
): Promise<SearchResult> {
|
||||
const maxResults = options.maxResults || 20;
|
||||
const queryLower = query.toLowerCase();
|
||||
const queryParts = queryLower.split(/[\s_\-./]+/).filter(p => p.length > 0);
|
||||
|
||||
// Load user grammar config for extra file extensions
|
||||
const projectRoot = options.projectRoot || rootDir;
|
||||
const userConfig = loadUserGrammars(projectRoot);
|
||||
const extraExtensions = new Set<string>();
|
||||
for (const entry of Object.values(userConfig.grammars)) {
|
||||
for (const ext of entry.extensions) {
|
||||
if (!CODE_EXTENSIONS.has(ext)) {
|
||||
extraExtensions.add(ext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 1: Collect files
|
||||
const filesToParse: Array<{ absolutePath: string; relativePath: string; content: string }> = [];
|
||||
|
||||
for await (const filePath of walkDir(rootDir, rootDir)) {
|
||||
for await (const filePath of walkDir(rootDir, rootDir, 20, extraExtensions.size > 0 ? extraExtensions : undefined)) {
|
||||
if (options.filePattern) {
|
||||
const relPath = relative(rootDir, filePath);
|
||||
if (!relPath.toLowerCase().includes(options.filePattern.toLowerCase())) continue;
|
||||
@@ -147,7 +172,7 @@ export async function searchCodebase(
|
||||
}
|
||||
|
||||
// Phase 2: Batch parse (one CLI call per language)
|
||||
const parsedFiles = parseFilesBatch(filesToParse);
|
||||
const parsedFiles = parseFilesBatch(filesToParse, projectRoot);
|
||||
|
||||
// Phase 3: Match query against symbols
|
||||
const foldedFiles: FoldedFile[] = [];
|
||||
|
||||
@@ -3,6 +3,7 @@ import { TableNameRow } from '../../types/database.js';
|
||||
import { DATA_DIR, DB_PATH, ensureDir } from '../../shared/paths.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { isDirectChild } from '../../shared/path-utils.js';
|
||||
import { AppError } from '../server/ErrorHandler.js';
|
||||
import {
|
||||
ObservationSearchResult,
|
||||
SessionSummarySearchResult,
|
||||
@@ -22,6 +23,8 @@ import {
|
||||
export class SessionSearch {
|
||||
private db: Database;
|
||||
|
||||
private static readonly MISSING_SEARCH_INPUT_MESSAGE = 'Either query or filters required for search';
|
||||
|
||||
constructor(dbPath?: string) {
|
||||
if (!dbPath) {
|
||||
ensureDir(DATA_DIR);
|
||||
@@ -280,7 +283,7 @@ export class SessionSearch {
|
||||
if (!query) {
|
||||
const filterClause = this.buildFilterClause(filters, params, 'o');
|
||||
if (!filterClause) {
|
||||
throw new Error('Either query or filters required for search');
|
||||
throw new AppError(SessionSearch.MISSING_SEARCH_INPUT_MESSAGE, 400, 'INVALID_SEARCH_REQUEST');
|
||||
}
|
||||
|
||||
const orderClause = this.buildOrderClause(orderBy, false);
|
||||
@@ -317,7 +320,7 @@ export class SessionSearch {
|
||||
delete filterOptions.type;
|
||||
const filterClause = this.buildFilterClause(filterOptions, params, 's');
|
||||
if (!filterClause) {
|
||||
throw new Error('Either query or filters required for search');
|
||||
throw new AppError(SessionSearch.MISSING_SEARCH_INPUT_MESSAGE, 400, 'INVALID_SEARCH_REQUEST');
|
||||
}
|
||||
|
||||
const orderClause = orderBy === 'date_asc'
|
||||
@@ -551,7 +554,7 @@ export class SessionSearch {
|
||||
// FILTER-ONLY PATH: When no query text, query user_prompts table directly
|
||||
if (!query) {
|
||||
if (baseConditions.length === 0) {
|
||||
throw new Error('Either query or filters required for search');
|
||||
throw new AppError(SessionSearch.MISSING_SEARCH_INPUT_MESSAGE, 400, 'INVALID_SEARCH_REQUEST');
|
||||
}
|
||||
|
||||
const whereClause = `WHERE ${baseConditions.join(' AND ')}`;
|
||||
|
||||
@@ -14,6 +14,18 @@ import {
|
||||
} from '../../types/database.js';
|
||||
import type { PendingMessageStore } from './PendingMessageStore.js';
|
||||
import { computeObservationContentHash, findDuplicateObservation } from './observations/store.js';
|
||||
import { parseFileList } from './observations/files.js';
|
||||
import { DEFAULT_PLATFORM_SOURCE, normalizePlatformSource, sortPlatformSources } from '../../shared/platform-source.js';
|
||||
|
||||
function resolveCreateSessionArgs(
|
||||
customTitle?: string,
|
||||
platformSource?: string
|
||||
): { customTitle?: string; platformSource?: string } {
|
||||
return {
|
||||
customTitle,
|
||||
platformSource: platformSource ? normalizePlatformSource(platformSource) : undefined
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Session data store for SDK sessions, observations, and summaries
|
||||
@@ -51,6 +63,8 @@ export class SessionStore {
|
||||
this.addOnUpdateCascadeToForeignKeys();
|
||||
this.addObservationContentHashColumn();
|
||||
this.addSessionCustomTitleColumn();
|
||||
this.addSessionPlatformSourceColumn();
|
||||
this.addObservationModelColumns();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,6 +92,7 @@ export class SessionStore {
|
||||
content_session_id TEXT UNIQUE NOT NULL,
|
||||
memory_session_id TEXT UNIQUE,
|
||||
project TEXT NOT NULL,
|
||||
platform_source TEXT NOT NULL DEFAULT 'claude',
|
||||
user_prompt TEXT,
|
||||
started_at TEXT NOT NULL,
|
||||
started_at_epoch INTEGER NOT NULL,
|
||||
@@ -202,7 +217,7 @@ export class SessionStore {
|
||||
private removeSessionSummariesUniqueConstraint(): void {
|
||||
// Check actual constraint state — don't rely on version tracking alone (issue #979)
|
||||
const summariesIndexes = this.db.query('PRAGMA index_list(session_summaries)').all() as IndexInfo[];
|
||||
const hasUniqueConstraint = summariesIndexes.some(idx => idx.unique === 1);
|
||||
const hasUniqueConstraint = summariesIndexes.some(idx => idx.unique === 1 && idx.origin !== 'pk');
|
||||
|
||||
if (!hasUniqueConstraint) {
|
||||
// Already migrated (no constraint exists)
|
||||
@@ -875,6 +890,60 @@ export class SessionStore {
|
||||
this.db.prepare('INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)').run(23, new Date().toISOString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add platform_source column to sdk_sessions for Claude/Codex isolation (migration 24)
|
||||
*/
|
||||
private addSessionPlatformSourceColumn(): void {
|
||||
const tableInfo = this.db.query('PRAGMA table_info(sdk_sessions)').all() as TableColumnInfo[];
|
||||
const hasColumn = tableInfo.some(col => col.name === 'platform_source');
|
||||
const indexInfo = this.db.query('PRAGMA index_list(sdk_sessions)').all() as IndexInfo[];
|
||||
const hasIndex = indexInfo.some(index => index.name === 'idx_sdk_sessions_platform_source');
|
||||
const applied = this.db.prepare('SELECT version FROM schema_versions WHERE version = ?').get(24) as SchemaVersion | undefined;
|
||||
|
||||
if (applied && hasColumn && hasIndex) return;
|
||||
|
||||
if (!hasColumn) {
|
||||
this.db.run(`ALTER TABLE sdk_sessions ADD COLUMN platform_source TEXT NOT NULL DEFAULT '${DEFAULT_PLATFORM_SOURCE}'`);
|
||||
logger.debug('DB', 'Added platform_source column to sdk_sessions table');
|
||||
}
|
||||
|
||||
this.db.run(`
|
||||
UPDATE sdk_sessions
|
||||
SET platform_source = '${DEFAULT_PLATFORM_SOURCE}'
|
||||
WHERE platform_source IS NULL OR platform_source = ''
|
||||
`);
|
||||
|
||||
if (!hasIndex) {
|
||||
this.db.run('CREATE INDEX IF NOT EXISTS idx_sdk_sessions_platform_source ON sdk_sessions(platform_source)');
|
||||
}
|
||||
|
||||
this.db.prepare('INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)').run(24, new Date().toISOString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add generated_by_model and relevance_count columns to observations (migration 26)
|
||||
*
|
||||
* Note: Cannot trust schema_versions alone — the old MigrationRunner may have
|
||||
* recorded version 26 without the ALTER TABLE actually succeeding. Always
|
||||
* check column existence directly.
|
||||
*/
|
||||
private addObservationModelColumns(): void {
|
||||
const columns = this.db.query('PRAGMA table_info(observations)').all() as TableColumnInfo[];
|
||||
const hasGeneratedByModel = columns.some(col => col.name === 'generated_by_model');
|
||||
const hasRelevanceCount = columns.some(col => col.name === 'relevance_count');
|
||||
|
||||
if (hasGeneratedByModel && hasRelevanceCount) return;
|
||||
|
||||
if (!hasGeneratedByModel) {
|
||||
this.db.run('ALTER TABLE observations ADD COLUMN generated_by_model TEXT');
|
||||
}
|
||||
if (!hasRelevanceCount) {
|
||||
this.db.run('ALTER TABLE observations ADD COLUMN relevance_count INTEGER DEFAULT 0');
|
||||
}
|
||||
|
||||
this.db.prepare('INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)').run(26, new Date().toISOString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the memory session ID for a session
|
||||
* Called by SDKAgent when it captures the session ID from the first SDK message
|
||||
@@ -888,6 +957,16 @@ export class SessionStore {
|
||||
`).run(memorySessionId, sessionDbId);
|
||||
}
|
||||
|
||||
markSessionCompleted(sessionDbId: number): void {
|
||||
const nowEpoch = Date.now();
|
||||
const nowIso = new Date(nowEpoch).toISOString();
|
||||
this.db.prepare(`
|
||||
UPDATE sdk_sessions
|
||||
SET status = 'completed', completed_at = ?, completed_at_epoch = ?
|
||||
WHERE id = ?
|
||||
`).run(nowIso, nowEpoch, sessionDbId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures memory_session_id is registered in sdk_sessions before FK-constrained INSERT.
|
||||
* This fixes Issue #846 where observations fail after worker restart because the
|
||||
@@ -1002,14 +1081,26 @@ export class SessionStore {
|
||||
subtitle: string | null;
|
||||
text: string;
|
||||
project: string;
|
||||
platform_source: string;
|
||||
prompt_number: number | null;
|
||||
created_at: string;
|
||||
created_at_epoch: number;
|
||||
}> {
|
||||
const stmt = this.db.prepare(`
|
||||
SELECT id, type, title, subtitle, text, project, prompt_number, created_at, created_at_epoch
|
||||
FROM observations
|
||||
ORDER BY created_at_epoch DESC
|
||||
SELECT
|
||||
o.id,
|
||||
o.type,
|
||||
o.title,
|
||||
o.subtitle,
|
||||
o.text,
|
||||
o.project,
|
||||
COALESCE(s.platform_source, '${DEFAULT_PLATFORM_SOURCE}') as platform_source,
|
||||
o.prompt_number,
|
||||
o.created_at,
|
||||
o.created_at_epoch
|
||||
FROM observations o
|
||||
LEFT JOIN sdk_sessions s ON o.memory_session_id = s.memory_session_id
|
||||
ORDER BY o.created_at_epoch DESC
|
||||
LIMIT ?
|
||||
`);
|
||||
|
||||
@@ -1030,16 +1121,30 @@ export class SessionStore {
|
||||
files_edited: string | null;
|
||||
notes: string | null;
|
||||
project: string;
|
||||
platform_source: string;
|
||||
prompt_number: number | null;
|
||||
created_at: string;
|
||||
created_at_epoch: number;
|
||||
}> {
|
||||
const stmt = this.db.prepare(`
|
||||
SELECT id, request, investigated, learned, completed, next_steps,
|
||||
files_read, files_edited, notes, project, prompt_number,
|
||||
created_at, created_at_epoch
|
||||
FROM session_summaries
|
||||
ORDER BY created_at_epoch DESC
|
||||
SELECT
|
||||
ss.id,
|
||||
ss.request,
|
||||
ss.investigated,
|
||||
ss.learned,
|
||||
ss.completed,
|
||||
ss.next_steps,
|
||||
ss.files_read,
|
||||
ss.files_edited,
|
||||
ss.notes,
|
||||
ss.project,
|
||||
COALESCE(s.platform_source, '${DEFAULT_PLATFORM_SOURCE}') as platform_source,
|
||||
ss.prompt_number,
|
||||
ss.created_at,
|
||||
ss.created_at_epoch
|
||||
FROM session_summaries ss
|
||||
LEFT JOIN sdk_sessions s ON ss.memory_session_id = s.memory_session_id
|
||||
ORDER BY ss.created_at_epoch DESC
|
||||
LIMIT ?
|
||||
`);
|
||||
|
||||
@@ -1053,6 +1158,7 @@ export class SessionStore {
|
||||
id: number;
|
||||
content_session_id: string;
|
||||
project: string;
|
||||
platform_source: string;
|
||||
prompt_number: number;
|
||||
prompt_text: string;
|
||||
created_at: string;
|
||||
@@ -1063,6 +1169,7 @@ export class SessionStore {
|
||||
up.id,
|
||||
up.content_session_id,
|
||||
s.project,
|
||||
COALESCE(s.platform_source, '${DEFAULT_PLATFORM_SOURCE}') as platform_source,
|
||||
up.prompt_number,
|
||||
up.prompt_text,
|
||||
up.created_at,
|
||||
@@ -1079,18 +1186,74 @@ export class SessionStore {
|
||||
/**
|
||||
* Get all unique projects from the database (for web UI project filter)
|
||||
*/
|
||||
getAllProjects(): string[] {
|
||||
const stmt = this.db.prepare(`
|
||||
getAllProjects(platformSource?: string): string[] {
|
||||
const normalizedPlatformSource = platformSource ? normalizePlatformSource(platformSource) : undefined;
|
||||
let query = `
|
||||
SELECT DISTINCT project
|
||||
FROM sdk_sessions
|
||||
WHERE project IS NOT NULL AND project != ''
|
||||
ORDER BY project ASC
|
||||
`);
|
||||
`;
|
||||
const params: unknown[] = [];
|
||||
|
||||
const rows = stmt.all() as Array<{ project: string }>;
|
||||
if (normalizedPlatformSource) {
|
||||
query += ' AND COALESCE(platform_source, ?) = ?';
|
||||
params.push(DEFAULT_PLATFORM_SOURCE, normalizedPlatformSource);
|
||||
}
|
||||
|
||||
query += ' ORDER BY project ASC';
|
||||
|
||||
const rows = this.db.prepare(query).all(...params) as Array<{ project: string }>;
|
||||
return rows.map(row => row.project);
|
||||
}
|
||||
|
||||
getProjectCatalog(): {
|
||||
projects: string[];
|
||||
sources: string[];
|
||||
projectsBySource: Record<string, string[]>;
|
||||
} {
|
||||
const rows = this.db.prepare(`
|
||||
SELECT
|
||||
COALESCE(platform_source, '${DEFAULT_PLATFORM_SOURCE}') as platform_source,
|
||||
project,
|
||||
MAX(started_at_epoch) as latest_epoch
|
||||
FROM sdk_sessions
|
||||
WHERE project IS NOT NULL AND project != ''
|
||||
GROUP BY COALESCE(platform_source, '${DEFAULT_PLATFORM_SOURCE}'), project
|
||||
ORDER BY latest_epoch DESC
|
||||
`).all() as Array<{ platform_source: string; project: string; latest_epoch: number }>;
|
||||
|
||||
const projects: string[] = [];
|
||||
const seenProjects = new Set<string>();
|
||||
const projectsBySource: Record<string, string[]> = {};
|
||||
|
||||
for (const row of rows) {
|
||||
const source = normalizePlatformSource(row.platform_source);
|
||||
|
||||
if (!projectsBySource[source]) {
|
||||
projectsBySource[source] = [];
|
||||
}
|
||||
|
||||
if (!projectsBySource[source].includes(row.project)) {
|
||||
projectsBySource[source].push(row.project);
|
||||
}
|
||||
|
||||
if (!seenProjects.has(row.project)) {
|
||||
seenProjects.add(row.project);
|
||||
projects.push(row.project);
|
||||
}
|
||||
}
|
||||
|
||||
const sources = sortPlatformSources(Object.keys(projectsBySource));
|
||||
|
||||
return {
|
||||
projects,
|
||||
sources,
|
||||
projectsBySource: Object.fromEntries(
|
||||
sources.map(source => [source, projectsBySource[source] || []])
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get latest user prompt with session info for a Claude session
|
||||
* Used for syncing prompts to Chroma during session initialization
|
||||
@@ -1100,6 +1263,7 @@ export class SessionStore {
|
||||
content_session_id: string;
|
||||
memory_session_id: string;
|
||||
project: string;
|
||||
platform_source: string;
|
||||
prompt_number: number;
|
||||
prompt_text: string;
|
||||
created_at_epoch: number;
|
||||
@@ -1108,7 +1272,8 @@ export class SessionStore {
|
||||
SELECT
|
||||
up.*,
|
||||
s.memory_session_id,
|
||||
s.project
|
||||
s.project,
|
||||
COALESCE(s.platform_source, '${DEFAULT_PLATFORM_SOURCE}') as platform_source
|
||||
FROM user_prompts up
|
||||
JOIN sdk_sessions s ON up.content_session_id = s.content_session_id
|
||||
WHERE up.content_session_id = ?
|
||||
@@ -1309,20 +1474,10 @@ export class SessionStore {
|
||||
|
||||
for (const row of rows) {
|
||||
// Parse files_read
|
||||
if (row.files_read) {
|
||||
const files = JSON.parse(row.files_read);
|
||||
if (Array.isArray(files)) {
|
||||
files.forEach(f => filesReadSet.add(f));
|
||||
}
|
||||
}
|
||||
parseFileList(row.files_read).forEach(f => filesReadSet.add(f));
|
||||
|
||||
// Parse files_modified
|
||||
if (row.files_modified) {
|
||||
const files = JSON.parse(row.files_modified);
|
||||
if (Array.isArray(files)) {
|
||||
files.forEach(f => filesModifiedSet.add(f));
|
||||
}
|
||||
}
|
||||
parseFileList(row.files_modified).forEach(f => filesModifiedSet.add(f));
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -1339,11 +1494,14 @@ export class SessionStore {
|
||||
content_session_id: string;
|
||||
memory_session_id: string | null;
|
||||
project: string;
|
||||
platform_source: string;
|
||||
user_prompt: string;
|
||||
custom_title: string | null;
|
||||
} | null {
|
||||
const stmt = this.db.prepare(`
|
||||
SELECT id, content_session_id, memory_session_id, project, user_prompt, custom_title
|
||||
SELECT id, content_session_id, memory_session_id, project,
|
||||
COALESCE(platform_source, '${DEFAULT_PLATFORM_SOURCE}') as platform_source,
|
||||
user_prompt, custom_title
|
||||
FROM sdk_sessions
|
||||
WHERE id = ?
|
||||
LIMIT 1
|
||||
@@ -1361,6 +1519,7 @@ export class SessionStore {
|
||||
content_session_id: string;
|
||||
memory_session_id: string;
|
||||
project: string;
|
||||
platform_source: string;
|
||||
user_prompt: string;
|
||||
custom_title: string | null;
|
||||
started_at: string;
|
||||
@@ -1373,7 +1532,9 @@ export class SessionStore {
|
||||
|
||||
const placeholders = memorySessionIds.map(() => '?').join(',');
|
||||
const stmt = this.db.prepare(`
|
||||
SELECT id, content_session_id, memory_session_id, project, user_prompt, custom_title,
|
||||
SELECT id, content_session_id, memory_session_id, project,
|
||||
COALESCE(platform_source, '${DEFAULT_PLATFORM_SOURCE}') as platform_source,
|
||||
user_prompt, custom_title,
|
||||
started_at, started_at_epoch, completed_at, completed_at_epoch, status
|
||||
FROM sdk_sessions
|
||||
WHERE memory_session_id IN (${placeholders})
|
||||
@@ -1418,14 +1579,22 @@ export class SessionStore {
|
||||
* Pure get-or-create: never modifies memory_session_id.
|
||||
* Multi-terminal isolation is handled by ON UPDATE CASCADE at the schema level.
|
||||
*/
|
||||
createSDKSession(contentSessionId: string, project: string, userPrompt: string, customTitle?: string): number {
|
||||
createSDKSession(
|
||||
contentSessionId: string,
|
||||
project: string,
|
||||
userPrompt: string,
|
||||
customTitle?: string,
|
||||
platformSource?: string
|
||||
): number {
|
||||
const now = new Date();
|
||||
const nowEpoch = now.getTime();
|
||||
const resolved = resolveCreateSessionArgs(customTitle, platformSource);
|
||||
const normalizedPlatformSource = resolved.platformSource ?? DEFAULT_PLATFORM_SOURCE;
|
||||
|
||||
// Session reuse: Return existing session ID if already created for this contentSessionId.
|
||||
const existing = this.db.prepare(`
|
||||
SELECT id FROM sdk_sessions WHERE content_session_id = ?
|
||||
`).get(contentSessionId) as { id: number } | undefined;
|
||||
SELECT id, platform_source FROM sdk_sessions WHERE content_session_id = ?
|
||||
`).get(contentSessionId) as { id: number; platform_source: string | null } | undefined;
|
||||
|
||||
if (existing) {
|
||||
// Backfill project if session was created by another hook with empty project
|
||||
@@ -1436,11 +1605,29 @@ export class SessionStore {
|
||||
`).run(project, contentSessionId);
|
||||
}
|
||||
// Backfill custom_title if provided and not yet set
|
||||
if (customTitle) {
|
||||
if (resolved.customTitle) {
|
||||
this.db.prepare(`
|
||||
UPDATE sdk_sessions SET custom_title = ?
|
||||
WHERE content_session_id = ? AND custom_title IS NULL
|
||||
`).run(customTitle, contentSessionId);
|
||||
`).run(resolved.customTitle, contentSessionId);
|
||||
}
|
||||
|
||||
if (resolved.platformSource) {
|
||||
const storedPlatformSource = existing.platform_source?.trim()
|
||||
? normalizePlatformSource(existing.platform_source)
|
||||
: undefined;
|
||||
|
||||
if (!storedPlatformSource) {
|
||||
this.db.prepare(`
|
||||
UPDATE sdk_sessions SET platform_source = ?
|
||||
WHERE content_session_id = ?
|
||||
AND COALESCE(platform_source, '') = ''
|
||||
`).run(resolved.platformSource, contentSessionId);
|
||||
} else if (storedPlatformSource !== resolved.platformSource) {
|
||||
throw new Error(
|
||||
`Platform source conflict for session ${contentSessionId}: existing=${storedPlatformSource}, received=${resolved.platformSource}`
|
||||
);
|
||||
}
|
||||
}
|
||||
return existing.id;
|
||||
}
|
||||
@@ -1451,9 +1638,9 @@ export class SessionStore {
|
||||
// must NEVER equal contentSessionId - that would inject memory messages into the user's transcript!
|
||||
this.db.prepare(`
|
||||
INSERT INTO sdk_sessions
|
||||
(content_session_id, memory_session_id, project, user_prompt, custom_title, started_at, started_at_epoch, status)
|
||||
VALUES (?, NULL, ?, ?, ?, ?, ?, 'active')
|
||||
`).run(contentSessionId, project, userPrompt, customTitle || null, now.toISOString(), nowEpoch);
|
||||
(content_session_id, memory_session_id, project, platform_source, user_prompt, custom_title, started_at, started_at_epoch, status)
|
||||
VALUES (?, NULL, ?, ?, ?, ?, ?, ?, 'active')
|
||||
`).run(contentSessionId, project, normalizedPlatformSource, userPrompt, resolved.customTitle || null, now.toISOString(), nowEpoch);
|
||||
|
||||
// Return new ID
|
||||
const row = this.db.prepare('SELECT id FROM sdk_sessions WHERE content_session_id = ?')
|
||||
@@ -1517,7 +1704,8 @@ export class SessionStore {
|
||||
},
|
||||
promptNumber?: number,
|
||||
discoveryTokens: number = 0,
|
||||
overrideTimestampEpoch?: number
|
||||
overrideTimestampEpoch?: number,
|
||||
generatedByModel?: string
|
||||
): { id: number; createdAtEpoch: number } {
|
||||
// Use override timestamp if provided (for processing backlog messages with original timestamps)
|
||||
const timestampEpoch = overrideTimestampEpoch ?? Date.now();
|
||||
@@ -1533,8 +1721,9 @@ export class SessionStore {
|
||||
const stmt = this.db.prepare(`
|
||||
INSERT INTO observations
|
||||
(memory_session_id, project, type, title, subtitle, facts, narrative, concepts,
|
||||
files_read, files_modified, prompt_number, discovery_tokens, content_hash, created_at, created_at_epoch)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
files_read, files_modified, prompt_number, discovery_tokens, content_hash, created_at, created_at_epoch,
|
||||
generated_by_model)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
|
||||
const result = stmt.run(
|
||||
@@ -1552,7 +1741,8 @@ export class SessionStore {
|
||||
discoveryTokens,
|
||||
contentHash,
|
||||
timestampIso,
|
||||
timestampEpoch
|
||||
timestampEpoch,
|
||||
generatedByModel || null
|
||||
);
|
||||
|
||||
return {
|
||||
@@ -1651,7 +1841,8 @@ export class SessionStore {
|
||||
} | null,
|
||||
promptNumber?: number,
|
||||
discoveryTokens: number = 0,
|
||||
overrideTimestampEpoch?: number
|
||||
overrideTimestampEpoch?: number,
|
||||
generatedByModel?: string
|
||||
): { observationIds: number[]; summaryId: number | null; createdAtEpoch: number } {
|
||||
// Use override timestamp if provided
|
||||
const timestampEpoch = overrideTimestampEpoch ?? Date.now();
|
||||
@@ -1665,8 +1856,9 @@ export class SessionStore {
|
||||
const obsStmt = this.db.prepare(`
|
||||
INSERT INTO observations
|
||||
(memory_session_id, project, type, title, subtitle, facts, narrative, concepts,
|
||||
files_read, files_modified, prompt_number, discovery_tokens, content_hash, created_at, created_at_epoch)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
files_read, files_modified, prompt_number, discovery_tokens, content_hash, created_at, created_at_epoch,
|
||||
generated_by_model)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
|
||||
for (const observation of observations) {
|
||||
@@ -1693,7 +1885,8 @@ export class SessionStore {
|
||||
discoveryTokens,
|
||||
contentHash,
|
||||
timestampIso,
|
||||
timestampEpoch
|
||||
timestampEpoch,
|
||||
generatedByModel || null
|
||||
);
|
||||
observationIds.push(Number(result.lastInsertRowid));
|
||||
}
|
||||
@@ -1780,7 +1973,8 @@ export class SessionStore {
|
||||
_pendingStore: PendingMessageStore,
|
||||
promptNumber?: number,
|
||||
discoveryTokens: number = 0,
|
||||
overrideTimestampEpoch?: number
|
||||
overrideTimestampEpoch?: number,
|
||||
generatedByModel?: string
|
||||
): { observationIds: number[]; summaryId?: number; createdAtEpoch: number } {
|
||||
// Use override timestamp if provided
|
||||
const timestampEpoch = overrideTimestampEpoch ?? Date.now();
|
||||
@@ -1794,8 +1988,9 @@ export class SessionStore {
|
||||
const obsStmt = this.db.prepare(`
|
||||
INSERT INTO observations
|
||||
(memory_session_id, project, type, title, subtitle, facts, narrative, concepts,
|
||||
files_read, files_modified, prompt_number, discovery_tokens, content_hash, created_at, created_at_epoch)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
files_read, files_modified, prompt_number, discovery_tokens, content_hash, created_at, created_at_epoch,
|
||||
generated_by_model)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
|
||||
for (const observation of observations) {
|
||||
@@ -1822,7 +2017,8 @@ export class SessionStore {
|
||||
discoveryTokens,
|
||||
contentHash,
|
||||
timestampIso,
|
||||
timestampEpoch
|
||||
timestampEpoch,
|
||||
generatedByModel || null
|
||||
);
|
||||
observationIds.push(Number(result.lastInsertRowid));
|
||||
}
|
||||
@@ -2233,9 +2429,9 @@ export class SessionStore {
|
||||
// Create new manual session
|
||||
const now = new Date();
|
||||
this.db.prepare(`
|
||||
INSERT INTO sdk_sessions (memory_session_id, content_session_id, project, started_at, started_at_epoch, status)
|
||||
VALUES (?, ?, ?, ?, ?, 'active')
|
||||
`).run(memorySessionId, contentSessionId, project, now.toISOString(), now.getTime());
|
||||
INSERT INTO sdk_sessions (memory_session_id, content_session_id, project, platform_source, started_at, started_at_epoch, status)
|
||||
VALUES (?, ?, ?, ?, ?, ?, 'active')
|
||||
`).run(memorySessionId, contentSessionId, project, DEFAULT_PLATFORM_SOURCE, now.toISOString(), now.getTime());
|
||||
|
||||
logger.info('SESSION', 'Created manual session', { memorySessionId, project });
|
||||
|
||||
@@ -2261,6 +2457,7 @@ export class SessionStore {
|
||||
content_session_id: string;
|
||||
memory_session_id: string;
|
||||
project: string;
|
||||
platform_source?: string;
|
||||
user_prompt: string;
|
||||
started_at: string;
|
||||
started_at_epoch: number;
|
||||
@@ -2279,15 +2476,16 @@ export class SessionStore {
|
||||
|
||||
const stmt = this.db.prepare(`
|
||||
INSERT INTO sdk_sessions (
|
||||
content_session_id, memory_session_id, project, user_prompt,
|
||||
content_session_id, memory_session_id, project, platform_source, user_prompt,
|
||||
started_at, started_at_epoch, completed_at, completed_at_epoch, status
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
|
||||
const result = stmt.run(
|
||||
session.content_session_id,
|
||||
session.memory_session_id,
|
||||
session.project,
|
||||
normalizePlatformSource(session.platform_source),
|
||||
session.user_prompt,
|
||||
session.started_at,
|
||||
session.started_at_epoch,
|
||||
@@ -2417,6 +2615,23 @@ export class SessionStore {
|
||||
return { imported: true, id: result.lastInsertRowid as number };
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuild the FTS5 index for observations.
|
||||
* Should be called after bulk imports to ensure imported rows are searchable.
|
||||
* No-op if observations_fts table does not exist.
|
||||
*/
|
||||
rebuildObservationsFTSIndex(): void {
|
||||
const hasFTS = (this.db.prepare(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name='observations_fts'"
|
||||
).all() as { name: string }[]).length > 0;
|
||||
|
||||
if (!hasFTS) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.db.run("INSERT INTO observations_fts(observations_fts) VALUES('rebuild')");
|
||||
}
|
||||
|
||||
/**
|
||||
* Import user prompt with duplicate checking
|
||||
* Duplicates are identified by content_session_id + prompt_number
|
||||
|
||||
@@ -541,6 +541,37 @@ export const migration008: Migration = {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Migration 009: Add missing columns to observations table
|
||||
*
|
||||
* The generated_by_model column tracks which model generated each observation
|
||||
* (required for model selection optimization via Thompson Sampling).
|
||||
* The relevance_count column tracks how many times an observation was reused
|
||||
* (incremented by the feedback recording pipeline).
|
||||
*
|
||||
* Both columns may already exist in databases created by the compiled binary
|
||||
* (v10.6.3) but are missing from the migration source. This migration
|
||||
* conditionally adds them.
|
||||
*/
|
||||
export const migration009: Migration = {
|
||||
version: 26,
|
||||
up: (db: Database) => {
|
||||
const columns = db.prepare('PRAGMA table_info(observations)').all() as any[];
|
||||
const hasGeneratedByModel = columns.some((c: any) => c.name === 'generated_by_model');
|
||||
const hasRelevanceCount = columns.some((c: any) => c.name === 'relevance_count');
|
||||
|
||||
if (!hasGeneratedByModel) {
|
||||
db.run('ALTER TABLE observations ADD COLUMN generated_by_model TEXT');
|
||||
}
|
||||
if (!hasRelevanceCount) {
|
||||
db.run('ALTER TABLE observations ADD COLUMN relevance_count INTEGER DEFAULT 0');
|
||||
}
|
||||
},
|
||||
down: (_db: Database) => {
|
||||
// SQLite does not support DROP COLUMN in older versions; no-op
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* All migrations in order
|
||||
*/
|
||||
@@ -552,5 +583,6 @@ export const migrations: Migration[] = [
|
||||
migration005,
|
||||
migration006,
|
||||
migration007,
|
||||
migration008
|
||||
migration008,
|
||||
migration009
|
||||
];
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
TableNameRow,
|
||||
SchemaVersion
|
||||
} from '../../../types/database.js';
|
||||
import { DEFAULT_PLATFORM_SOURCE } from '../../../shared/platform-source.js';
|
||||
|
||||
/**
|
||||
* MigrationRunner handles all database schema migrations
|
||||
@@ -35,6 +36,7 @@ export class MigrationRunner {
|
||||
this.addObservationContentHashColumn();
|
||||
this.addSessionCustomTitleColumn();
|
||||
this.createObservationFeedbackTable();
|
||||
this.addSessionPlatformSourceColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,6 +64,7 @@ export class MigrationRunner {
|
||||
content_session_id TEXT UNIQUE NOT NULL,
|
||||
memory_session_id TEXT UNIQUE,
|
||||
project TEXT NOT NULL,
|
||||
platform_source TEXT NOT NULL DEFAULT 'claude',
|
||||
user_prompt TEXT,
|
||||
started_at TEXT NOT NULL,
|
||||
started_at_epoch INTEGER NOT NULL,
|
||||
@@ -186,7 +189,7 @@ export class MigrationRunner {
|
||||
private removeSessionSummariesUniqueConstraint(): void {
|
||||
// Check actual constraint state — don't rely on version tracking alone (issue #979)
|
||||
const summariesIndexes = this.db.query('PRAGMA index_list(session_summaries)').all() as IndexInfo[];
|
||||
const hasUniqueConstraint = summariesIndexes.some(idx => idx.unique === 1);
|
||||
const hasUniqueConstraint = summariesIndexes.some(idx => idx.unique === 1 && idx.origin !== 'pk');
|
||||
|
||||
if (!hasUniqueConstraint) {
|
||||
// Already migrated (no constraint exists)
|
||||
@@ -654,10 +657,9 @@ export class MigrationRunner {
|
||||
this.db.run('BEGIN TRANSACTION');
|
||||
|
||||
try {
|
||||
// ==========================================
|
||||
// ===================================
|
||||
// 1. Recreate observations table
|
||||
// ==========================================
|
||||
|
||||
// ===================================
|
||||
// Drop FTS triggers first (they reference the observations table)
|
||||
this.db.run('DROP TRIGGER IF EXISTS observations_ai');
|
||||
this.db.run('DROP TRIGGER IF EXISTS observations_ad');
|
||||
@@ -730,10 +732,9 @@ export class MigrationRunner {
|
||||
`);
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// ===================================
|
||||
// 2. Recreate session_summaries table
|
||||
// ==========================================
|
||||
|
||||
// ===================================
|
||||
// Clean up leftover temp table from a previously-crashed run
|
||||
this.db.run('DROP TABLE IF EXISTS session_summaries_new');
|
||||
|
||||
@@ -891,4 +892,34 @@ export class MigrationRunner {
|
||||
this.db.prepare('INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)').run(24, new Date().toISOString());
|
||||
logger.debug('DB', 'Created observation_feedback table for usage tracking');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add platform_source column to sdk_sessions for Claude/Codex isolation (migration 25)
|
||||
*/
|
||||
private addSessionPlatformSourceColumn(): void {
|
||||
const tableInfo = this.db.query('PRAGMA table_info(sdk_sessions)').all() as TableColumnInfo[];
|
||||
const hasColumn = tableInfo.some(col => col.name === 'platform_source');
|
||||
const indexInfo = this.db.query('PRAGMA index_list(sdk_sessions)').all() as IndexInfo[];
|
||||
const hasIndex = indexInfo.some(index => index.name === 'idx_sdk_sessions_platform_source');
|
||||
const applied = this.db.prepare('SELECT version FROM schema_versions WHERE version = ?').get(25) as SchemaVersion | undefined;
|
||||
|
||||
if (applied && hasColumn && hasIndex) return;
|
||||
|
||||
if (!hasColumn) {
|
||||
this.db.run(`ALTER TABLE sdk_sessions ADD COLUMN platform_source TEXT NOT NULL DEFAULT '${DEFAULT_PLATFORM_SOURCE}'`);
|
||||
logger.debug('DB', 'Added platform_source column to sdk_sessions table');
|
||||
}
|
||||
|
||||
this.db.run(`
|
||||
UPDATE sdk_sessions
|
||||
SET platform_source = '${DEFAULT_PLATFORM_SOURCE}'
|
||||
WHERE platform_source IS NULL OR platform_source = ''
|
||||
`);
|
||||
|
||||
if (!hasIndex) {
|
||||
this.db.run('CREATE INDEX IF NOT EXISTS idx_sdk_sessions_platform_source ON sdk_sessions(platform_source)');
|
||||
}
|
||||
|
||||
this.db.prepare('INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)').run(25, new Date().toISOString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,21 @@ import { Database } from 'bun:sqlite';
|
||||
import { logger } from '../../../utils/logger.js';
|
||||
import type { SessionFilesResult } from './types.js';
|
||||
|
||||
/**
|
||||
* Safely parse a JSON array string from the DB.
|
||||
* Handles legacy bare-path strings (e.g. "/foo/bar.ts") by wrapping them
|
||||
* in an array instead of crashing with a SyntaxError (fix for #1359).
|
||||
*/
|
||||
export function parseFileList(value: string | null | undefined): string[] {
|
||||
if (!value) return [];
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
return Array.isArray(parsed) ? parsed : [String(parsed)];
|
||||
} catch {
|
||||
return [value];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get aggregated files from all observations for a session
|
||||
*/
|
||||
@@ -30,20 +45,10 @@ export function getFilesForSession(
|
||||
|
||||
for (const row of rows) {
|
||||
// Parse files_read
|
||||
if (row.files_read) {
|
||||
const files = JSON.parse(row.files_read);
|
||||
if (Array.isArray(files)) {
|
||||
files.forEach(f => filesReadSet.add(f));
|
||||
}
|
||||
}
|
||||
parseFileList(row.files_read).forEach(f => filesReadSet.add(f));
|
||||
|
||||
// Parse files_modified
|
||||
if (row.files_modified) {
|
||||
const files = JSON.parse(row.files_modified);
|
||||
if (Array.isArray(files)) {
|
||||
files.forEach(f => filesModifiedSet.add(f));
|
||||
}
|
||||
}
|
||||
parseFileList(row.files_modified).forEach(f => filesModifiedSet.add(f));
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -111,3 +111,42 @@ export function getObservationsForSession(
|
||||
|
||||
return stmt.all(memorySessionId) as ObservationSessionRow[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get observations associated with a given file path, scoped to specific projects.
|
||||
* Matches on the full file path (not just basename) to avoid cross-project collisions.
|
||||
*/
|
||||
export function getObservationsByFilePath(
|
||||
db: Database,
|
||||
filePath: string,
|
||||
options?: { projects?: string[]; limit?: number }
|
||||
): ObservationRecord[] {
|
||||
const rawLimit = options?.limit;
|
||||
const limit = Number.isInteger(rawLimit) && (rawLimit as number) > 0
|
||||
? Math.min(rawLimit as number, 100)
|
||||
: 15;
|
||||
const params: (string | number)[] = [filePath, filePath];
|
||||
|
||||
let projectClause = '';
|
||||
if (options?.projects?.length) {
|
||||
const placeholders = options.projects.map(() => '?').join(',');
|
||||
projectClause = `AND project IN (${placeholders})`;
|
||||
params.push(...options.projects);
|
||||
}
|
||||
|
||||
params.push(limit);
|
||||
|
||||
const stmt = db.prepare(`
|
||||
SELECT *
|
||||
FROM observations
|
||||
WHERE (
|
||||
(files_read LIKE '[%' AND EXISTS (SELECT 1 FROM json_each(files_read) WHERE value = ?))
|
||||
OR (files_modified LIKE '[%' AND EXISTS (SELECT 1 FROM json_each(files_modified) WHERE value = ?))
|
||||
)
|
||||
${projectClause}
|
||||
ORDER BY created_at_epoch DESC
|
||||
LIMIT ?
|
||||
`);
|
||||
|
||||
return stmt.all(...params) as ObservationRecord[];
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ export function computeObservationContentHash(
|
||||
narrative: string | null
|
||||
): string {
|
||||
return createHash('sha256')
|
||||
.update((memorySessionId || '') + (title || '') + (narrative || ''))
|
||||
.update([memorySessionId || '', title || '', narrative || ''].join('\x00'))
|
||||
.digest('hex')
|
||||
.slice(0, 16);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,17 @@
|
||||
|
||||
import type { Database } from 'bun:sqlite';
|
||||
import { logger } from '../../../utils/logger.js';
|
||||
import { DEFAULT_PLATFORM_SOURCE, normalizePlatformSource } from '../../../shared/platform-source.js';
|
||||
|
||||
function resolveCreateSessionArgs(
|
||||
customTitle?: string,
|
||||
platformSource?: string
|
||||
): { customTitle?: string; platformSource?: string } {
|
||||
return {
|
||||
customTitle,
|
||||
platformSource: platformSource ? normalizePlatformSource(platformSource) : undefined
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new SDK session (idempotent - returns existing session ID if already exists)
|
||||
@@ -22,15 +33,18 @@ export function createSDKSession(
|
||||
contentSessionId: string,
|
||||
project: string,
|
||||
userPrompt: string,
|
||||
customTitle?: string
|
||||
customTitle?: string,
|
||||
platformSource?: string
|
||||
): number {
|
||||
const now = new Date();
|
||||
const nowEpoch = now.getTime();
|
||||
const resolved = resolveCreateSessionArgs(customTitle, platformSource);
|
||||
const normalizedPlatformSource = resolved.platformSource ?? DEFAULT_PLATFORM_SOURCE;
|
||||
|
||||
// Check for existing session
|
||||
const existing = db.prepare(`
|
||||
SELECT id FROM sdk_sessions WHERE content_session_id = ?
|
||||
`).get(contentSessionId) as { id: number } | undefined;
|
||||
SELECT id, platform_source FROM sdk_sessions WHERE content_session_id = ?
|
||||
`).get(contentSessionId) as { id: number; platform_source: string | null } | undefined;
|
||||
|
||||
if (existing) {
|
||||
// Backfill project if session was created by another hook with empty project
|
||||
@@ -41,11 +55,29 @@ export function createSDKSession(
|
||||
`).run(project, contentSessionId);
|
||||
}
|
||||
// Backfill custom_title if provided and not yet set
|
||||
if (customTitle) {
|
||||
if (resolved.customTitle) {
|
||||
db.prepare(`
|
||||
UPDATE sdk_sessions SET custom_title = ?
|
||||
WHERE content_session_id = ? AND custom_title IS NULL
|
||||
`).run(customTitle, contentSessionId);
|
||||
`).run(resolved.customTitle, contentSessionId);
|
||||
}
|
||||
|
||||
if (resolved.platformSource) {
|
||||
const storedPlatformSource = existing.platform_source?.trim()
|
||||
? normalizePlatformSource(existing.platform_source)
|
||||
: undefined;
|
||||
|
||||
if (!storedPlatformSource) {
|
||||
db.prepare(`
|
||||
UPDATE sdk_sessions SET platform_source = ?
|
||||
WHERE content_session_id = ?
|
||||
AND COALESCE(platform_source, '') = ''
|
||||
`).run(resolved.platformSource, contentSessionId);
|
||||
} else if (storedPlatformSource !== resolved.platformSource) {
|
||||
throw new Error(
|
||||
`Platform source conflict for session ${contentSessionId}: existing=${storedPlatformSource}, received=${resolved.platformSource}`
|
||||
);
|
||||
}
|
||||
}
|
||||
return existing.id;
|
||||
}
|
||||
@@ -56,9 +88,9 @@ export function createSDKSession(
|
||||
// must NEVER equal contentSessionId - that would inject memory messages into the user's transcript!
|
||||
db.prepare(`
|
||||
INSERT INTO sdk_sessions
|
||||
(content_session_id, memory_session_id, project, user_prompt, custom_title, started_at, started_at_epoch, status)
|
||||
VALUES (?, NULL, ?, ?, ?, ?, ?, 'active')
|
||||
`).run(contentSessionId, project, userPrompt, customTitle || null, now.toISOString(), nowEpoch);
|
||||
(content_session_id, memory_session_id, project, platform_source, user_prompt, custom_title, started_at, started_at_epoch, status)
|
||||
VALUES (?, NULL, ?, ?, ?, ?, ?, ?, 'active')
|
||||
`).run(contentSessionId, project, normalizedPlatformSource, userPrompt, resolved.customTitle || null, now.toISOString(), nowEpoch);
|
||||
|
||||
// Return new ID
|
||||
const row = db.prepare('SELECT id FROM sdk_sessions WHERE content_session_id = ?')
|
||||
|
||||
@@ -17,7 +17,9 @@ import type {
|
||||
*/
|
||||
export function getSessionById(db: Database, id: number): SessionBasic | null {
|
||||
const stmt = db.prepare(`
|
||||
SELECT id, content_session_id, memory_session_id, project, user_prompt, custom_title
|
||||
SELECT id, content_session_id, memory_session_id, project,
|
||||
COALESCE(platform_source, 'claude') as platform_source,
|
||||
user_prompt, custom_title
|
||||
FROM sdk_sessions
|
||||
WHERE id = ?
|
||||
LIMIT 1
|
||||
@@ -38,7 +40,9 @@ export function getSdkSessionsBySessionIds(
|
||||
|
||||
const placeholders = memorySessionIds.map(() => '?').join(',');
|
||||
const stmt = db.prepare(`
|
||||
SELECT id, content_session_id, memory_session_id, project, user_prompt, custom_title,
|
||||
SELECT id, content_session_id, memory_session_id, project,
|
||||
COALESCE(platform_source, 'claude') as platform_source,
|
||||
user_prompt, custom_title,
|
||||
started_at, started_at_epoch, completed_at, completed_at_epoch, status
|
||||
FROM sdk_sessions
|
||||
WHERE memory_session_id IN (${placeholders})
|
||||
|
||||
@@ -12,6 +12,7 @@ export interface SessionBasic {
|
||||
content_session_id: string;
|
||||
memory_session_id: string | null;
|
||||
project: string;
|
||||
platform_source: string;
|
||||
user_prompt: string;
|
||||
custom_title: string | null;
|
||||
}
|
||||
@@ -24,6 +25,7 @@ export interface SessionFull {
|
||||
content_session_id: string;
|
||||
memory_session_id: string;
|
||||
project: string;
|
||||
platform_source: string;
|
||||
user_prompt: string;
|
||||
custom_title: string | null;
|
||||
started_at: string;
|
||||
|
||||
@@ -120,10 +120,16 @@ export class ChromaMcpManager {
|
||||
args: uvxSpawnArgs.join(' ')
|
||||
});
|
||||
|
||||
// Run chroma-mcp from the home directory so that pydantic-settings (used
|
||||
// by chroma-mcp internally) does not pick up .env / .env.local files from
|
||||
// the project directory. Those files often contain project-specific vars
|
||||
// that pydantic rejects with "Extra inputs are not permitted", crashing the
|
||||
// subprocess immediately. Fixes #1297.
|
||||
this.transport = new StdioClientTransport({
|
||||
command: uvxSpawnCommand,
|
||||
args: uvxSpawnArgs,
|
||||
env: spawnEnvironment,
|
||||
cwd: os.homedir(),
|
||||
stderr: 'pipe'
|
||||
});
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import { ChromaMcpManager } from './ChromaMcpManager.js';
|
||||
import { ParsedObservation, ParsedSummary } from '../../sdk/parser.js';
|
||||
import { SessionStore } from '../sqlite/SessionStore.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { parseFileList } from '../sqlite/observations/files.js';
|
||||
|
||||
interface ChromaDocument {
|
||||
id: string;
|
||||
@@ -125,8 +126,8 @@ export class ChromaSync {
|
||||
// Parse JSON fields
|
||||
const facts = obs.facts ? JSON.parse(obs.facts) : [];
|
||||
const concepts = obs.concepts ? JSON.parse(obs.concepts) : [];
|
||||
const files_read = obs.files_read ? JSON.parse(obs.files_read) : [];
|
||||
const files_modified = obs.files_modified ? JSON.parse(obs.files_modified) : [];
|
||||
const files_read = parseFileList(obs.files_read);
|
||||
const files_modified = parseFileList(obs.files_modified);
|
||||
|
||||
const baseMetadata: Record<string, string | number> = {
|
||||
sqlite_id: obs.id,
|
||||
|
||||
@@ -97,7 +97,6 @@ export const SAMPLE_CONFIG: TranscriptWatchConfig = {
|
||||
startAtEnd: true,
|
||||
context: {
|
||||
mode: 'agents',
|
||||
path: '~/.codex/AGENTS.md',
|
||||
updateOn: ['session_start', 'session_end']
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,16 @@ import { fileEditHandler } from '../../cli/handlers/file-edit.js';
|
||||
import { sessionCompleteHandler } from '../../cli/handlers/session-complete.js';
|
||||
import { ensureWorkerRunning, workerHttpRequest } from '../../shared/worker-utils.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { getProjectContext, getProjectName } from '../../utils/project-name.js';
|
||||
import { getProjectContext } from '../../utils/project-name.js';
|
||||
import { writeAgentsMd } from '../../utils/agents-md-utils.js';
|
||||
import { resolveFieldSpec, resolveFields, matchesRule } from './field-utils.js';
|
||||
import { expandHomePath } from './config.js';
|
||||
import type { TranscriptSchema, WatchTarget, SchemaEvent } from './types.js';
|
||||
import { normalizePlatformSource } from '../../shared/platform-source.js';
|
||||
|
||||
interface SessionState {
|
||||
sessionId: string;
|
||||
platformSource: string;
|
||||
cwd?: string;
|
||||
project?: string;
|
||||
lastUserMessage?: string;
|
||||
@@ -51,6 +53,7 @@ export class TranscriptEventProcessor {
|
||||
if (!session) {
|
||||
session = {
|
||||
sessionId,
|
||||
platformSource: normalizePlatformSource(watch.name),
|
||||
pendingTools: new Map()
|
||||
};
|
||||
this.sessions.set(key, session);
|
||||
@@ -101,7 +104,7 @@ export class TranscriptEventProcessor {
|
||||
const resolved = resolveFieldSpec(fieldSpec, entry, ctx);
|
||||
if (typeof resolved === 'string' && resolved.trim()) return resolved;
|
||||
if (watch.project) return watch.project;
|
||||
if (session.cwd) return getProjectName(session.cwd);
|
||||
if (session.cwd) return getProjectContext(session.cwd).primary;
|
||||
return session.project;
|
||||
}
|
||||
|
||||
@@ -181,7 +184,7 @@ export class TranscriptEventProcessor {
|
||||
sessionId: session.sessionId,
|
||||
cwd,
|
||||
prompt,
|
||||
platform: 'transcript'
|
||||
platform: session.platformSource
|
||||
});
|
||||
}
|
||||
|
||||
@@ -250,7 +253,7 @@ export class TranscriptEventProcessor {
|
||||
toolName,
|
||||
toolInput: this.maybeParseJson(fields.toolInput),
|
||||
toolResponse: this.maybeParseJson(fields.toolResponse),
|
||||
platform: 'transcript'
|
||||
platform: session.platformSource
|
||||
});
|
||||
}
|
||||
|
||||
@@ -263,7 +266,7 @@ export class TranscriptEventProcessor {
|
||||
cwd: session.cwd ?? process.cwd(),
|
||||
filePath,
|
||||
edits: Array.isArray(fields.edits) ? fields.edits : undefined,
|
||||
platform: 'transcript'
|
||||
platform: session.platformSource
|
||||
});
|
||||
}
|
||||
|
||||
@@ -305,7 +308,7 @@ export class TranscriptEventProcessor {
|
||||
await sessionCompleteHandler.execute({
|
||||
sessionId: session.sessionId,
|
||||
cwd: session.cwd ?? process.cwd(),
|
||||
platform: 'transcript'
|
||||
platform: session.platformSource
|
||||
});
|
||||
await this.updateContext(session, watch);
|
||||
session.pendingTools.clear();
|
||||
@@ -325,7 +328,8 @@ export class TranscriptEventProcessor {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
contentSessionId: session.sessionId,
|
||||
last_assistant_message: lastAssistantMessage
|
||||
last_assistant_message: lastAssistantMessage,
|
||||
platformSource: session.platformSource
|
||||
})
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -350,7 +354,7 @@ export class TranscriptEventProcessor {
|
||||
|
||||
try {
|
||||
const response = await workerHttpRequest(
|
||||
`/api/context/inject?projects=${encodeURIComponent(projectsParam)}`
|
||||
`/api/context/inject?projects=${encodeURIComponent(projectsParam)}&platformSource=${encodeURIComponent(session.platformSource)}`
|
||||
);
|
||||
if (!response.ok) return;
|
||||
|
||||
|
||||
@@ -117,15 +117,15 @@ export class TranscriptWatcher {
|
||||
const files = this.resolveWatchFiles(resolvedPath);
|
||||
|
||||
for (const filePath of files) {
|
||||
await this.addTailer(filePath, watch, schema);
|
||||
await this.addTailer(filePath, watch, schema, true);
|
||||
}
|
||||
|
||||
const rescanIntervalMs = watch.rescanIntervalMs ?? 5000;
|
||||
const timer = setInterval(async () => {
|
||||
const timer = setInterval(async () => {
|
||||
const newFiles = this.resolveWatchFiles(resolvedPath);
|
||||
for (const filePath of newFiles) {
|
||||
if (!this.tailers.has(filePath)) {
|
||||
await this.addTailer(filePath, watch, schema);
|
||||
await this.addTailer(filePath, watch, schema, false);
|
||||
}
|
||||
}
|
||||
}, rescanIntervalMs);
|
||||
@@ -164,13 +164,20 @@ export class TranscriptWatcher {
|
||||
return /[*?[\]{}()]/.test(inputPath);
|
||||
}
|
||||
|
||||
private async addTailer(filePath: string, watch: WatchTarget, schema: TranscriptSchema): Promise<void> {
|
||||
private async addTailer(
|
||||
filePath: string,
|
||||
watch: WatchTarget,
|
||||
schema: TranscriptSchema,
|
||||
initialDiscovery: boolean
|
||||
): Promise<void> {
|
||||
if (this.tailers.has(filePath)) return;
|
||||
|
||||
const sessionIdOverride = this.extractSessionIdFromPath(filePath);
|
||||
|
||||
let offset = this.state.offsets[filePath] ?? 0;
|
||||
if (offset === 0 && watch.startAtEnd) {
|
||||
// `startAtEnd` is useful on worker startup to avoid replaying the full backlog,
|
||||
// but new transcript files must be read from byte 0 or we lose session_meta/user_message.
|
||||
if (offset === 0 && watch.startAtEnd && initialDiscovery) {
|
||||
try {
|
||||
offset = statSync(filePath).size;
|
||||
} catch {
|
||||
|
||||
+124
-182
@@ -10,7 +10,7 @@
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import { existsSync, writeFileSync, unlinkSync, statSync } from 'fs';
|
||||
import { existsSync } from 'fs';
|
||||
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
||||
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
||||
import { getWorkerPort, getWorkerHost } from '../shared/worker-utils.js';
|
||||
@@ -23,43 +23,11 @@ import { ChromaSync } from './sync/ChromaSync.js';
|
||||
import { configureSupervisorSignalHandlers, getSupervisor, startSupervisor } from '../supervisor/index.js';
|
||||
import { sanitizeEnv } from '../supervisor/env-sanitizer.js';
|
||||
|
||||
// Windows: avoid repeated spawn popups when startup fails (issue #921)
|
||||
const WINDOWS_SPAWN_COOLDOWN_MS = 2 * 60 * 1000;
|
||||
|
||||
function getWorkerSpawnLockPath(): string {
|
||||
return path.join(SettingsDefaultsManager.get('CLAUDE_MEM_DATA_DIR'), '.worker-start-attempted');
|
||||
}
|
||||
|
||||
function shouldSkipSpawnOnWindows(): boolean {
|
||||
if (process.platform !== 'win32') return false;
|
||||
const lockPath = getWorkerSpawnLockPath();
|
||||
if (!existsSync(lockPath)) return false;
|
||||
try {
|
||||
const modifiedTimeMs = statSync(lockPath).mtimeMs;
|
||||
return Date.now() - modifiedTimeMs < WINDOWS_SPAWN_COOLDOWN_MS;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function markWorkerSpawnAttempted(): void {
|
||||
if (process.platform !== 'win32') return;
|
||||
try {
|
||||
writeFileSync(getWorkerSpawnLockPath(), '', 'utf-8');
|
||||
} catch {
|
||||
// Best-effort lock file — failure to write shouldn't block startup
|
||||
}
|
||||
}
|
||||
|
||||
function clearWorkerSpawnAttempted(): void {
|
||||
if (process.platform !== 'win32') return;
|
||||
try {
|
||||
const lockPath = getWorkerSpawnLockPath();
|
||||
if (existsSync(lockPath)) unlinkSync(lockPath);
|
||||
} catch {
|
||||
// Best-effort cleanup
|
||||
}
|
||||
}
|
||||
// Worker spawn / Windows-cooldown helpers are defined in ./worker-spawner.ts
|
||||
// so that lightweight consumers (e.g. the MCP server running under Node) can
|
||||
// ensure the worker daemon is up without importing this entire module — which
|
||||
// transitively pulls in the SQLite database layer via ChromaSync/DatabaseManager.
|
||||
import { ensureWorkerStarted as ensureWorkerStartedShared } from './worker-spawner.js';
|
||||
|
||||
// Re-export for backward compatibility — canonical implementation in shared/plugin-state.ts
|
||||
export { isPluginDisabledInClaudeSettings } from '../shared/plugin-state.js';
|
||||
@@ -80,7 +48,6 @@ import {
|
||||
cleanStalePidFile,
|
||||
isProcessAlive,
|
||||
spawnDaemon,
|
||||
isPidFileRecent,
|
||||
touchPidFile
|
||||
} from './infrastructure/ProcessManager.js';
|
||||
import {
|
||||
@@ -88,8 +55,7 @@ import {
|
||||
waitForHealth,
|
||||
waitForReadiness,
|
||||
waitForPortFree,
|
||||
httpShutdown,
|
||||
checkVersionMatch
|
||||
httpShutdown
|
||||
} from './infrastructure/HealthMonitor.js';
|
||||
import { performGracefulShutdown } from './infrastructure/GracefulShutdown.js';
|
||||
|
||||
@@ -118,6 +84,8 @@ import { SearchManager } from './worker/SearchManager.js';
|
||||
import { FormattingService } from './worker/FormattingService.js';
|
||||
import { TimelineService } from './worker/TimelineService.js';
|
||||
import { SessionEventBroadcaster } from './worker/events/SessionEventBroadcaster.js';
|
||||
import { DEFAULT_CONFIG_PATH, DEFAULT_STATE_PATH, expandHomePath, loadTranscriptWatchConfig, writeSampleConfig } from './transcripts/config.js';
|
||||
import { TranscriptWatcher } from './transcripts/watcher.js';
|
||||
|
||||
// HTTP route handlers
|
||||
import { ViewerRoutes } from './worker/http/routes/ViewerRoutes.js';
|
||||
@@ -127,14 +95,16 @@ import { SearchRoutes } from './worker/http/routes/SearchRoutes.js';
|
||||
import { SettingsRoutes } from './worker/http/routes/SettingsRoutes.js';
|
||||
import { LogsRoutes } from './worker/http/routes/LogsRoutes.js';
|
||||
import { MemoryRoutes } from './worker/http/routes/MemoryRoutes.js';
|
||||
import { CorpusRoutes } from './worker/http/routes/CorpusRoutes.js';
|
||||
|
||||
// Knowledge agent services
|
||||
import { CorpusStore } from './worker/knowledge/CorpusStore.js';
|
||||
import { CorpusBuilder } from './worker/knowledge/CorpusBuilder.js';
|
||||
import { KnowledgeAgent } from './worker/knowledge/KnowledgeAgent.js';
|
||||
|
||||
// Process management for zombie cleanup (Issue #737)
|
||||
import { startOrphanReaper, reapOrphanedProcesses, getProcessBySession, ensureProcessExit } from './worker/ProcessRegistry.js';
|
||||
|
||||
// Transcript watcher for external CLI session monitoring
|
||||
import { TranscriptWatcher } from './transcripts/watcher.js';
|
||||
import { loadTranscriptWatchConfig, expandHomePath, DEFAULT_CONFIG_PATH as TRANSCRIPT_CONFIG_PATH } from './transcripts/config.js';
|
||||
|
||||
/**
|
||||
* Build JSON status output for hook framework communication.
|
||||
* This is a pure function extracted for testability.
|
||||
@@ -179,6 +149,7 @@ export class WorkerService {
|
||||
private paginationHelper: PaginationHelper;
|
||||
private settingsManager: SettingsManager;
|
||||
private sessionEventBroadcaster: SessionEventBroadcaster;
|
||||
private corpusStore: CorpusStore;
|
||||
|
||||
// Route handlers
|
||||
private searchRoutes: SearchRoutes | null = null;
|
||||
@@ -186,6 +157,9 @@ export class WorkerService {
|
||||
// Chroma MCP manager (lazy - connects on first use)
|
||||
private chromaMcpManager: ChromaMcpManager | null = null;
|
||||
|
||||
// Transcript watcher for Codex and other transcript-based clients
|
||||
private transcriptWatcher: TranscriptWatcher | null = null;
|
||||
|
||||
// Initialization tracking
|
||||
private initializationComplete: Promise<void>;
|
||||
private resolveInitialization!: () => void;
|
||||
@@ -196,9 +170,6 @@ export class WorkerService {
|
||||
// Stale session reaper interval (Issue #1168)
|
||||
private staleSessionReaperInterval: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
// Transcript watcher for external CLI sessions (e.g. Codex, Gemini)
|
||||
private transcriptWatcher: TranscriptWatcher | null = null;
|
||||
|
||||
// AI interaction tracking for health endpoint
|
||||
private lastAiInteraction: {
|
||||
timestamp: number;
|
||||
@@ -224,6 +195,7 @@ export class WorkerService {
|
||||
this.paginationHelper = new PaginationHelper(this.dbManager);
|
||||
this.settingsManager = new SettingsManager(this.dbManager);
|
||||
this.sessionEventBroadcaster = new SessionEventBroadcaster(this.sseBroadcaster, this);
|
||||
this.corpusStore = new CorpusStore();
|
||||
|
||||
// Set callback for when sessions are deleted
|
||||
this.sessionManager.setOnSessionDeleted(() => {
|
||||
@@ -424,6 +396,22 @@ export class WorkerService {
|
||||
this.server.registerRoutes(this.searchRoutes);
|
||||
logger.info('WORKER', 'SearchManager initialized and search routes registered');
|
||||
|
||||
// Register corpus routes (knowledge agents) — needs SearchOrchestrator from search module
|
||||
const { SearchOrchestrator } = await import('./worker/search/SearchOrchestrator.js');
|
||||
const corpusSearchOrchestrator = new SearchOrchestrator(
|
||||
this.dbManager.getSessionSearch(),
|
||||
this.dbManager.getSessionStore(),
|
||||
this.dbManager.getChromaSync()
|
||||
);
|
||||
const corpusBuilder = new CorpusBuilder(
|
||||
this.dbManager.getSessionStore(),
|
||||
corpusSearchOrchestrator,
|
||||
this.corpusStore
|
||||
);
|
||||
const knowledgeAgent = new KnowledgeAgent(this.corpusStore);
|
||||
this.server.registerRoutes(new CorpusRoutes(this.corpusStore, corpusBuilder, knowledgeAgent));
|
||||
logger.info('WORKER', 'CorpusRoutes registered');
|
||||
|
||||
// DB and search are ready — mark initialization complete so hooks can proceed.
|
||||
// MCP connection is tracked separately via mcpReady and is NOT required for
|
||||
// the worker to serve context/search requests.
|
||||
@@ -431,21 +419,7 @@ export class WorkerService {
|
||||
this.resolveInitialization();
|
||||
logger.info('SYSTEM', 'Core initialization complete (DB + search ready)');
|
||||
|
||||
// Auto-start transcript watchers if configured
|
||||
if (existsSync(TRANSCRIPT_CONFIG_PATH)) {
|
||||
try {
|
||||
const transcriptConfig = loadTranscriptWatchConfig(TRANSCRIPT_CONFIG_PATH);
|
||||
if (transcriptConfig.watches.length > 0) {
|
||||
const transcriptStatePath = expandHomePath(transcriptConfig.stateFile ?? '~/.claude-mem/transcript-watch-state.json');
|
||||
this.transcriptWatcher = new TranscriptWatcher(transcriptConfig, transcriptStatePath);
|
||||
await this.transcriptWatcher.start();
|
||||
logger.info('SYSTEM', `Transcript watcher started with ${transcriptConfig.watches.length} watch target(s)`);
|
||||
}
|
||||
} catch (transcriptError) {
|
||||
logger.warn('SYSTEM', 'Failed to start transcript watcher (non-fatal)', {}, transcriptError as Error);
|
||||
// Non-fatal — worker continues without transcript watching
|
||||
}
|
||||
}
|
||||
await this.startTranscriptWatcher(settings);
|
||||
|
||||
// Auto-backfill Chroma for all projects if out of sync with SQLite (fire-and-forget)
|
||||
if (this.chromaMcpManager) {
|
||||
@@ -456,8 +430,13 @@ export class WorkerService {
|
||||
});
|
||||
}
|
||||
|
||||
// Connect to MCP server
|
||||
// Mark MCP as externally ready once the bundled stdio server binary exists.
|
||||
// Codex/Claude Desktop connect to this binary directly; the loopback client
|
||||
// below is only a best-effort self-check and should not mark health false.
|
||||
const mcpServerPath = path.join(__dirname, 'mcp-server.cjs');
|
||||
this.mcpReady = existsSync(mcpServerPath);
|
||||
|
||||
// Best-effort loopback MCP self-check
|
||||
getSupervisor().assertCanSpawn('mcp server');
|
||||
const transport = new StdioClientTransport({
|
||||
command: 'node',
|
||||
@@ -479,7 +458,7 @@ export class WorkerService {
|
||||
await Promise.race([mcpConnectionPromise, timeoutPromise]);
|
||||
} catch (connectionError) {
|
||||
clearTimeout(timeoutId!);
|
||||
logger.warn('WORKER', 'MCP server connection failed, cleaning up subprocess', {
|
||||
logger.warn('WORKER', 'MCP loopback self-check failed, cleaning up subprocess', {
|
||||
error: connectionError instanceof Error ? connectionError.message : String(connectionError)
|
||||
});
|
||||
try {
|
||||
@@ -487,7 +466,10 @@ export class WorkerService {
|
||||
} catch {
|
||||
// Best effort: the supervisor handles later process cleanup for survivors.
|
||||
}
|
||||
throw connectionError;
|
||||
logger.info('WORKER', 'Bundled MCP server remains available for external stdio clients', {
|
||||
path: mcpServerPath
|
||||
});
|
||||
return;
|
||||
}
|
||||
clearTimeout(timeoutId!);
|
||||
|
||||
@@ -502,8 +484,7 @@ export class WorkerService {
|
||||
getSupervisor().unregisterProcess('mcp-server');
|
||||
});
|
||||
}
|
||||
this.mcpReady = true;
|
||||
logger.success('WORKER', 'MCP server connected');
|
||||
logger.success('WORKER', 'MCP loopback self-check connected');
|
||||
|
||||
// Start orphan reaper to clean up zombie processes (Issue #737)
|
||||
this.stopOrphanReaper = startOrphanReaper(() => {
|
||||
@@ -545,6 +526,48 @@ export class WorkerService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start transcript watcher for Codex and other transcript-based clients.
|
||||
* This is intentionally non-fatal so Claude hooks remain usable even if
|
||||
* transcript ingestion is misconfigured.
|
||||
*/
|
||||
private async startTranscriptWatcher(settings: ReturnType<typeof SettingsDefaultsManager.loadFromFile>): Promise<void> {
|
||||
const transcriptsEnabled = settings.CLAUDE_MEM_TRANSCRIPTS_ENABLED !== 'false';
|
||||
if (!transcriptsEnabled) {
|
||||
logger.info('TRANSCRIPT', 'Transcript watcher disabled via CLAUDE_MEM_TRANSCRIPTS_ENABLED=false');
|
||||
return;
|
||||
}
|
||||
|
||||
const configPath = settings.CLAUDE_MEM_TRANSCRIPTS_CONFIG_PATH || DEFAULT_CONFIG_PATH;
|
||||
const resolvedConfigPath = expandHomePath(configPath);
|
||||
|
||||
try {
|
||||
if (!existsSync(resolvedConfigPath)) {
|
||||
writeSampleConfig(configPath);
|
||||
logger.info('TRANSCRIPT', 'Created default transcript watch config', {
|
||||
configPath: resolvedConfigPath
|
||||
});
|
||||
}
|
||||
|
||||
const transcriptConfig = loadTranscriptWatchConfig(configPath);
|
||||
const statePath = expandHomePath(transcriptConfig.stateFile ?? DEFAULT_STATE_PATH);
|
||||
|
||||
this.transcriptWatcher = new TranscriptWatcher(transcriptConfig, statePath);
|
||||
await this.transcriptWatcher.start();
|
||||
logger.info('TRANSCRIPT', 'Transcript watcher started', {
|
||||
configPath: resolvedConfigPath,
|
||||
statePath,
|
||||
watches: transcriptConfig.watches.length
|
||||
});
|
||||
} catch (error) {
|
||||
this.transcriptWatcher?.stop();
|
||||
this.transcriptWatcher = null;
|
||||
logger.error('TRANSCRIPT', 'Failed to start transcript watcher (continuing without Codex ingestion)', {
|
||||
configPath: resolvedConfigPath
|
||||
}, error as Error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the appropriate agent based on provider settings.
|
||||
* Same logic as SessionRoutes.getActiveAgent() for consistency.
|
||||
@@ -936,6 +959,12 @@ export class WorkerService {
|
||||
* Shutdown the worker service
|
||||
*/
|
||||
async shutdown(): Promise<void> {
|
||||
if (this.transcriptWatcher) {
|
||||
this.transcriptWatcher.stop();
|
||||
this.transcriptWatcher = null;
|
||||
logger.info('TRANSCRIPT', 'Transcript watcher stopped');
|
||||
}
|
||||
|
||||
// Stop orphan reaper before shutdown (Issue #737)
|
||||
if (this.stopOrphanReaper) {
|
||||
this.stopOrphanReaper();
|
||||
@@ -948,13 +977,6 @@ export class WorkerService {
|
||||
this.staleSessionReaperInterval = null;
|
||||
}
|
||||
|
||||
// Stop transcript watcher
|
||||
if (this.transcriptWatcher) {
|
||||
this.transcriptWatcher.stop();
|
||||
this.transcriptWatcher = null;
|
||||
logger.info('SYSTEM', 'Transcript watcher stopped');
|
||||
}
|
||||
|
||||
await performGracefulShutdown({
|
||||
server: this.server.getHttpServer(),
|
||||
sessionManager: this.sessionManager,
|
||||
@@ -992,115 +1014,22 @@ export class WorkerService {
|
||||
|
||||
/**
|
||||
* Ensures the worker is started and healthy.
|
||||
* This function can be called by both 'start' and 'hook' commands.
|
||||
*
|
||||
* Thin wrapper around the canonical implementation in ./worker-spawner.ts.
|
||||
*
|
||||
* `__filename` is forwarded as the worker script path because, in the CJS
|
||||
* bundle that ships to users, `__filename` always resolves to the compiled
|
||||
* `worker-service.cjs` itself — which is exactly the script the spawner
|
||||
* needs to relaunch as a detached daemon. The MCP server (a separate Node
|
||||
* bundle) cannot rely on its own `__filename` because that would point at
|
||||
* `mcp-server.cjs`, so it computes the worker path explicitly via
|
||||
* `dirname(__filename) + 'worker-service.cjs'` instead.
|
||||
*
|
||||
* @param port - The TCP port (used for port-in-use checks and daemon spawn)
|
||||
* @returns true if worker is healthy (existing or newly started), false on failure
|
||||
*/
|
||||
async function ensureWorkerStarted(port: number): Promise<boolean> {
|
||||
// Clean stale PID file first (cheap: 1 fs read + 1 signal-0 check)
|
||||
const pidFileStatus = cleanStalePidFile();
|
||||
if (pidFileStatus === 'alive') {
|
||||
logger.info('SYSTEM', 'Worker PID file points to a live process, skipping duplicate spawn');
|
||||
const healthy = await waitForHealth(port, getPlatformTimeout(HOOK_TIMEOUTS.PORT_IN_USE_WAIT));
|
||||
if (healthy) {
|
||||
logger.info('SYSTEM', 'Worker became healthy while waiting on live PID');
|
||||
return true;
|
||||
}
|
||||
logger.warn('SYSTEM', 'Live PID detected but worker did not become healthy before timeout');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if worker is already running and healthy
|
||||
if (await waitForHealth(port, 1000)) {
|
||||
const versionCheck = await checkVersionMatch(port);
|
||||
if (!versionCheck.matches) {
|
||||
// Guard: If PID file was written recently, another session is likely already
|
||||
// restarting the worker. Poll health instead of starting a concurrent restart.
|
||||
// This prevents the "100 sessions all restart simultaneously" storm (#1145).
|
||||
const RESTART_COORDINATION_THRESHOLD_MS = 15000;
|
||||
if (isPidFileRecent(RESTART_COORDINATION_THRESHOLD_MS)) {
|
||||
logger.info('SYSTEM', 'Version mismatch detected but PID file is recent — another restart likely in progress, polling health', {
|
||||
pluginVersion: versionCheck.pluginVersion,
|
||||
workerVersion: versionCheck.workerVersion
|
||||
});
|
||||
const healthy = await waitForHealth(port, RESTART_COORDINATION_THRESHOLD_MS);
|
||||
if (healthy) {
|
||||
logger.info('SYSTEM', 'Worker became healthy after waiting for concurrent restart');
|
||||
return true;
|
||||
}
|
||||
logger.warn('SYSTEM', 'Worker did not become healthy after waiting — proceeding with own restart');
|
||||
}
|
||||
|
||||
logger.info('SYSTEM', 'Worker version mismatch detected - auto-restarting', {
|
||||
pluginVersion: versionCheck.pluginVersion,
|
||||
workerVersion: versionCheck.workerVersion
|
||||
});
|
||||
|
||||
await httpShutdown(port);
|
||||
const freed = await waitForPortFree(port, getPlatformTimeout(HOOK_TIMEOUTS.PORT_IN_USE_WAIT));
|
||||
if (!freed) {
|
||||
logger.error('SYSTEM', 'Port did not free up after shutdown for version mismatch restart', { port });
|
||||
return false;
|
||||
}
|
||||
removePidFile();
|
||||
} else {
|
||||
logger.info('SYSTEM', 'Worker already running and healthy');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if port is in use by something else
|
||||
const portInUse = await isPortInUse(port);
|
||||
if (portInUse) {
|
||||
logger.info('SYSTEM', 'Port in use, waiting for worker to become healthy');
|
||||
const healthy = await waitForHealth(port, getPlatformTimeout(HOOK_TIMEOUTS.PORT_IN_USE_WAIT));
|
||||
if (healthy) {
|
||||
logger.info('SYSTEM', 'Worker is now healthy');
|
||||
return true;
|
||||
}
|
||||
logger.error('SYSTEM', 'Port in use but worker not responding to health checks');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Windows: skip spawn if a recent attempt already failed (prevents repeated bun.exe popups, issue #921)
|
||||
if (shouldSkipSpawnOnWindows()) {
|
||||
logger.warn('SYSTEM', 'Worker unavailable on Windows — skipping spawn (recent attempt failed within cooldown)');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Spawn new worker daemon
|
||||
logger.info('SYSTEM', 'Starting worker daemon');
|
||||
markWorkerSpawnAttempted();
|
||||
const pid = spawnDaemon(__filename, port);
|
||||
if (pid === undefined) {
|
||||
logger.error('SYSTEM', 'Failed to spawn worker daemon');
|
||||
return false;
|
||||
}
|
||||
|
||||
// PID file is written by the worker itself after listen() succeeds
|
||||
// This is race-free and works correctly on Windows where cmd.exe PID is useless
|
||||
|
||||
const healthy = await waitForHealth(port, getPlatformTimeout(HOOK_TIMEOUTS.POST_SPAWN_WAIT));
|
||||
if (!healthy) {
|
||||
removePidFile();
|
||||
logger.error('SYSTEM', 'Worker failed to start (health check timeout)');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Health passed (HTTP listening). Now wait for DB + search initialization
|
||||
// so hooks that run immediately after can actually use the worker.
|
||||
const ready = await waitForReadiness(port, getPlatformTimeout(HOOK_TIMEOUTS.READINESS_WAIT));
|
||||
if (!ready) {
|
||||
logger.warn('SYSTEM', 'Worker is alive but readiness timed out — proceeding anyway');
|
||||
}
|
||||
|
||||
clearWorkerSpawnAttempted();
|
||||
// Touch PID file to signal other sessions that a restart just completed.
|
||||
// Other sessions checking isPidFileRecent() will see this and skip their own restart.
|
||||
touchPidFile();
|
||||
logger.info('SYSTEM', 'Worker started successfully');
|
||||
return true;
|
||||
export async function ensureWorkerStarted(port: number): Promise<boolean> {
|
||||
return ensureWorkerStartedShared(port, __filename);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -1295,7 +1224,18 @@ async function main() {
|
||||
});
|
||||
|
||||
const worker = new WorkerService();
|
||||
worker.start().catch((error) => {
|
||||
worker.start().catch(async (error) => {
|
||||
// Port race: when the MCP server and SessionStart hook both spawn a daemon
|
||||
// concurrently, one will lose the bind race with EADDRINUSE or Bun's equivalent
|
||||
// "port in use" error. If the winner is already healthy, exit cleanly (#1447).
|
||||
const isPortConflict = error instanceof Error && (
|
||||
(error as NodeJS.ErrnoException).code === 'EADDRINUSE' ||
|
||||
/port.*in use|address.*in use/i.test(error.message)
|
||||
);
|
||||
if (isPortConflict && await waitForHealth(port, 3000)) {
|
||||
logger.info('SYSTEM', 'Duplicate daemon exiting — another worker already claimed port', { port });
|
||||
process.exit(0);
|
||||
}
|
||||
logger.failure('SYSTEM', 'Worker failed to start', {}, error as Error);
|
||||
removePidFile();
|
||||
// Exit gracefully: Windows Terminal won't keep tab open on exit 0
|
||||
@@ -1307,8 +1247,10 @@ async function main() {
|
||||
}
|
||||
|
||||
// Check if running as main module in both ESM and CommonJS
|
||||
// The CLAUDE_MEM_MANAGED check handles Bun on Windows where require.main !== module
|
||||
// in CJS mode despite being the entry point (see #1450)
|
||||
const isMainModule = typeof require !== 'undefined' && typeof module !== 'undefined'
|
||||
? require.main === module || !module.parent
|
||||
? require.main === module || !module.parent || process.env.CLAUDE_MEM_MANAGED === 'true'
|
||||
: import.meta.url === `file://${process.argv[1]}`
|
||||
|| process.argv[1]?.endsWith('worker-service')
|
||||
|| process.argv[1]?.endsWith('worker-service.cjs')
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
/**
|
||||
* Worker Spawner - Lightweight worker daemon lifecycle helper
|
||||
*
|
||||
* Extracted from worker-service.ts so that lightweight consumers (like the
|
||||
* MCP server running under Node) can ensure the worker daemon is running
|
||||
* without importing the full worker-service bundle, which transitively pulls
|
||||
* in `bun:sqlite` and the entire database layer.
|
||||
*
|
||||
* This module MUST NOT import anything that touches SQLite, ChromaDB, or the
|
||||
* worker business logic modules. Keep it lean on purpose.
|
||||
*
|
||||
* Dependency boundary note: this file imports from `SettingsDefaultsManager`,
|
||||
* `ProcessManager`, and `HealthMonitor`. None of those currently touch
|
||||
* `bun:sqlite` or any other Bun-only module. If any of them ever does, this
|
||||
* module's SQLite-free contract silently breaks and the build guardrail in
|
||||
* `scripts/build-hooks.js` is the only thing that catches it. Audit transitive
|
||||
* imports here when adding new helpers from the shared/infrastructure layers.
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import { existsSync, mkdirSync, writeFileSync, unlinkSync, statSync } from 'fs';
|
||||
import { logger } from '../utils/logger.js';
|
||||
import { HOOK_TIMEOUTS } from '../shared/hook-constants.js';
|
||||
import { SettingsDefaultsManager } from '../shared/SettingsDefaultsManager.js';
|
||||
import {
|
||||
cleanStalePidFile,
|
||||
getPlatformTimeout,
|
||||
removePidFile,
|
||||
spawnDaemon,
|
||||
touchPidFile,
|
||||
} from './infrastructure/ProcessManager.js';
|
||||
import {
|
||||
isPortInUse,
|
||||
waitForHealth,
|
||||
waitForReadiness,
|
||||
} from './infrastructure/HealthMonitor.js';
|
||||
|
||||
// Windows: avoid repeated spawn popups when startup fails (issue #921)
|
||||
const WINDOWS_SPAWN_COOLDOWN_MS = 2 * 60 * 1000;
|
||||
|
||||
function getWorkerSpawnLockPath(): string {
|
||||
return path.join(SettingsDefaultsManager.get('CLAUDE_MEM_DATA_DIR'), '.worker-start-attempted');
|
||||
}
|
||||
|
||||
// Internal helpers — NOT exported. Only ensureWorkerStarted should be on the
|
||||
// public surface; callers must not bypass the lifecycle by calling these
|
||||
// directly. See PR #1645 review feedback for context.
|
||||
|
||||
function shouldSkipSpawnOnWindows(): boolean {
|
||||
if (process.platform !== 'win32') return false;
|
||||
const lockPath = getWorkerSpawnLockPath();
|
||||
if (!existsSync(lockPath)) return false;
|
||||
try {
|
||||
const modifiedTimeMs = statSync(lockPath).mtimeMs;
|
||||
return Date.now() - modifiedTimeMs < WINDOWS_SPAWN_COOLDOWN_MS;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function markWorkerSpawnAttempted(): void {
|
||||
if (process.platform !== 'win32') return;
|
||||
try {
|
||||
const lockPath = getWorkerSpawnLockPath();
|
||||
// Ensure CLAUDE_MEM_DATA_DIR exists before writing the marker. On a fresh
|
||||
// user profile the directory may not exist yet, in which case writeFileSync
|
||||
// would throw ENOENT, the catch would swallow it, and the cooldown marker
|
||||
// would never be created — defeating the popup-loop protection that this
|
||||
// helper exists to provide. recursive: true is a no-op when the dir already
|
||||
// exists, so this is safe to call on every spawn attempt.
|
||||
mkdirSync(path.dirname(lockPath), { recursive: true });
|
||||
writeFileSync(lockPath, '', 'utf-8');
|
||||
} catch {
|
||||
// APPROVED OVERRIDE: best-effort cooldown marker. If we can't even create
|
||||
// the data dir or write the marker, the worker spawn itself is almost
|
||||
// certainly going to fail too — surfacing that downstream gives the user
|
||||
// a far more useful error than a noisy log line about a lock file.
|
||||
}
|
||||
}
|
||||
|
||||
function clearWorkerSpawnAttempted(): void {
|
||||
if (process.platform !== 'win32') return;
|
||||
try {
|
||||
const lockPath = getWorkerSpawnLockPath();
|
||||
if (existsSync(lockPath)) unlinkSync(lockPath);
|
||||
} catch {
|
||||
// APPROVED OVERRIDE: best-effort cleanup of the cooldown marker after a
|
||||
// successful spawn. A stale marker on disk is harmless — the worst case
|
||||
// is one suppressed retry within the cooldown window, then it self-heals.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures the worker is started and healthy.
|
||||
*
|
||||
* @param port - The TCP port (used for port-in-use checks and daemon spawn)
|
||||
* @param workerScriptPath - Absolute path to the worker-service script to spawn.
|
||||
* Callers running inside worker-service pass `__filename`.
|
||||
* Callers outside (e.g., mcp-server) must resolve the
|
||||
* path to worker-service.cjs in the plugin's scripts dir.
|
||||
* @returns true if worker is healthy (existing or newly started), false on failure
|
||||
*/
|
||||
export async function ensureWorkerStarted(
|
||||
port: number,
|
||||
workerScriptPath: string
|
||||
): Promise<boolean> {
|
||||
// Defensive guard: validate the worker script path before any health check
|
||||
// or spawn attempt. Without this, an empty string or missing file just
|
||||
// surfaces as a low-signal child_process error from spawnDaemon. Callers
|
||||
// should always pass a valid path, but a partial install or a regression
|
||||
// in path resolution upstream is much easier to debug with an explicit
|
||||
// log line at the entry point. See PR #1645 review feedback for context.
|
||||
if (!workerScriptPath) {
|
||||
logger.error('SYSTEM', 'ensureWorkerStarted called with empty workerScriptPath — caller bug');
|
||||
return false;
|
||||
}
|
||||
if (!existsSync(workerScriptPath)) {
|
||||
logger.error(
|
||||
'SYSTEM',
|
||||
'ensureWorkerStarted: worker script not found at expected path — likely a partial install or build artifact missing',
|
||||
{ workerScriptPath }
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clean stale PID file first (cheap: 1 fs read + 1 signal-0 check)
|
||||
const pidFileStatus = cleanStalePidFile();
|
||||
if (pidFileStatus === 'alive') {
|
||||
logger.info('SYSTEM', 'Worker PID file points to a live process, skipping duplicate spawn');
|
||||
const healthy = await waitForHealth(port, getPlatformTimeout(HOOK_TIMEOUTS.PORT_IN_USE_WAIT));
|
||||
if (healthy) {
|
||||
// A previous failed spawn may have left a stale Windows cooldown marker
|
||||
// on disk. Now that the worker is confirmed healthy via this alternate
|
||||
// path, clear it so a future genuine outage isn't suppressed for the
|
||||
// remainder of the 2-minute window. Per CodeRabbit on PR #1645.
|
||||
// No-op on non-Windows.
|
||||
clearWorkerSpawnAttempted();
|
||||
logger.info('SYSTEM', 'Worker became healthy while waiting on live PID');
|
||||
return true;
|
||||
}
|
||||
logger.warn('SYSTEM', 'Live PID detected but worker did not become healthy before timeout');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if worker is already running and healthy.
|
||||
// NOTE: Version mismatch auto-restart intentionally removed (#1435).
|
||||
if (await waitForHealth(port, 1000)) {
|
||||
// Same rationale as above: clear any stale cooldown marker now that we
|
||||
// know the worker is healthy via the fast-path health check.
|
||||
clearWorkerSpawnAttempted();
|
||||
const ready = await waitForReadiness(port, getPlatformTimeout(HOOK_TIMEOUTS.READINESS_WAIT));
|
||||
if (!ready) {
|
||||
logger.warn('SYSTEM', 'Worker is alive but readiness timed out — proceeding anyway');
|
||||
}
|
||||
logger.info('SYSTEM', 'Worker already running and healthy');
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if port is in use by something else
|
||||
const portInUse = await isPortInUse(port);
|
||||
if (portInUse) {
|
||||
logger.info('SYSTEM', 'Port in use, waiting for worker to become healthy');
|
||||
const healthy = await waitForHealth(port, getPlatformTimeout(HOOK_TIMEOUTS.PORT_IN_USE_WAIT));
|
||||
if (healthy) {
|
||||
// Same rationale as above.
|
||||
clearWorkerSpawnAttempted();
|
||||
logger.info('SYSTEM', 'Worker is now healthy');
|
||||
return true;
|
||||
}
|
||||
logger.error('SYSTEM', 'Port in use but worker not responding to health checks');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Windows: skip spawn if a recent attempt already failed (issue #921)
|
||||
if (shouldSkipSpawnOnWindows()) {
|
||||
logger.warn('SYSTEM', 'Worker unavailable on Windows — skipping spawn (recent attempt failed within cooldown)');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Spawn new worker daemon
|
||||
logger.info('SYSTEM', 'Starting worker daemon', { workerScriptPath });
|
||||
markWorkerSpawnAttempted();
|
||||
const pid = spawnDaemon(workerScriptPath, port);
|
||||
if (pid === undefined) {
|
||||
logger.error('SYSTEM', 'Failed to spawn worker daemon');
|
||||
return false;
|
||||
}
|
||||
|
||||
// PID file is written by the worker itself after listen() succeeds
|
||||
const healthy = await waitForHealth(port, getPlatformTimeout(HOOK_TIMEOUTS.POST_SPAWN_WAIT));
|
||||
if (!healthy) {
|
||||
removePidFile();
|
||||
logger.error('SYSTEM', 'Worker failed to start (health check timeout)');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Health passed (HTTP listening). Now wait for DB + search initialization
|
||||
const ready = await waitForReadiness(port, getPlatformTimeout(HOOK_TIMEOUTS.READINESS_WAIT));
|
||||
if (!ready) {
|
||||
logger.warn('SYSTEM', 'Worker is alive but readiness timed out — proceeding anyway');
|
||||
}
|
||||
|
||||
clearWorkerSpawnAttempted();
|
||||
touchPidFile();
|
||||
logger.info('SYSTEM', 'Worker started successfully');
|
||||
return true;
|
||||
}
|
||||
@@ -22,6 +22,7 @@ export interface ActiveSession {
|
||||
contentSessionId: string; // User's Claude Code session being observed
|
||||
memorySessionId: string | null; // Memory agent's session ID for resume
|
||||
project: string;
|
||||
platformSource: string;
|
||||
userPrompt: string;
|
||||
pendingMessages: PendingMessage[]; // Deprecated: now using persistent store, kept for compatibility
|
||||
abortController: AbortController;
|
||||
@@ -42,6 +43,9 @@ export interface ActiveSession {
|
||||
processingMessageIds: number[];
|
||||
// Tier routing: model override per session based on queue complexity
|
||||
modelOverride?: string;
|
||||
// Track whether the most recent storage operation persisted a summary record.
|
||||
// Used by the status endpoint so the Stop hook can detect silent summary loss (#1633).
|
||||
lastSummaryStored?: boolean;
|
||||
}
|
||||
|
||||
export interface PendingMessage {
|
||||
@@ -99,6 +103,7 @@ export interface PaginationParams {
|
||||
offset: number;
|
||||
limit: number;
|
||||
project?: string;
|
||||
platformSource?: string;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -119,6 +124,7 @@ export interface Observation {
|
||||
id: number;
|
||||
memory_session_id: string; // Renamed from sdk_session_id
|
||||
project: string;
|
||||
platform_source: string;
|
||||
type: string;
|
||||
title: string;
|
||||
subtitle: string | null;
|
||||
@@ -137,6 +143,7 @@ export interface Summary {
|
||||
id: number;
|
||||
session_id: string; // content_session_id (from JOIN)
|
||||
project: string;
|
||||
platform_source: string;
|
||||
request: string | null;
|
||||
investigated: string | null;
|
||||
learned: string | null;
|
||||
@@ -151,6 +158,7 @@ export interface UserPrompt {
|
||||
id: number;
|
||||
content_session_id: string; // Renamed from claude_session_id
|
||||
project: string; // From JOIN with sdk_sessions
|
||||
platform_source: string;
|
||||
prompt_number: number;
|
||||
prompt_text: string;
|
||||
created_at: string;
|
||||
@@ -161,6 +169,7 @@ export interface DBSession {
|
||||
id: number;
|
||||
content_session_id: string; // Renamed from claude_session_id
|
||||
project: string;
|
||||
platform_source: string;
|
||||
user_prompt: string;
|
||||
memory_session_id: string | null; // Renamed from sdk_session_id
|
||||
status: 'active' | 'completed' | 'failed';
|
||||
|
||||
@@ -18,6 +18,8 @@ import { logger } from '../../utils/logger.js';
|
||||
import { buildInitPrompt, buildObservationPrompt, buildSummaryPrompt, buildContinuationPrompt } from '../../sdk/prompts.js';
|
||||
import { SettingsDefaultsManager } from '../../shared/SettingsDefaultsManager.js';
|
||||
import { getCredential } from '../../shared/EnvManager.js';
|
||||
import { USER_SETTINGS_PATH } from '../../shared/paths.js';
|
||||
import { estimateTokens } from '../../shared/timeline-formatting.js';
|
||||
import type { ActiveSession, ConversationMessage } from '../worker-types.js';
|
||||
import { ModeManager } from '../domain/ModeManager.js';
|
||||
import {
|
||||
@@ -56,6 +58,10 @@ const GEMINI_RPM_LIMITS: Record<GeminiModel, number> = {
|
||||
// Track last request time for rate limiting
|
||||
let lastRequestTime = 0;
|
||||
|
||||
// Context window limits (prevents O(N²) token cost growth)
|
||||
const DEFAULT_MAX_CONTEXT_MESSAGES = 20; // Maximum messages to keep in conversation history
|
||||
const DEFAULT_MAX_ESTIMATED_TOKENS = 100000; // ~100k tokens max context (safety limit)
|
||||
|
||||
/**
|
||||
* Enforce RPM rate limit for Gemini free tier.
|
||||
* Waits the required time between requests based on model's RPM limit + 100ms safety buffer.
|
||||
@@ -175,7 +181,9 @@ export class GeminiAgent {
|
||||
worker,
|
||||
tokensUsed,
|
||||
null,
|
||||
'Gemini'
|
||||
'Gemini',
|
||||
undefined,
|
||||
model
|
||||
);
|
||||
} else {
|
||||
logger.error('SDK', 'Empty Gemini init response - session may lack context', {
|
||||
@@ -248,7 +256,8 @@ export class GeminiAgent {
|
||||
tokensUsed,
|
||||
originalTimestamp,
|
||||
'Gemini',
|
||||
lastCwd
|
||||
lastCwd,
|
||||
model
|
||||
);
|
||||
} else {
|
||||
logger.warn('SDK', 'Empty Gemini observation response, skipping processing to preserve message', {
|
||||
@@ -298,7 +307,8 @@ export class GeminiAgent {
|
||||
tokensUsed,
|
||||
originalTimestamp,
|
||||
'Gemini',
|
||||
lastCwd
|
||||
lastCwd,
|
||||
model
|
||||
);
|
||||
} else {
|
||||
logger.warn('SDK', 'Empty Gemini summary response, skipping processing to preserve message', {
|
||||
@@ -342,6 +352,54 @@ export class GeminiAgent {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate conversation history to prevent runaway context costs.
|
||||
* Keeps most recent messages within both message count and token budget.
|
||||
* Returns a new array — never mutates the original history.
|
||||
*/
|
||||
private truncateHistory(history: ConversationMessage[]): ConversationMessage[] {
|
||||
const settings = SettingsDefaultsManager.loadFromFile(USER_SETTINGS_PATH);
|
||||
|
||||
const MAX_CONTEXT_MESSAGES = parseInt(settings.CLAUDE_MEM_GEMINI_MAX_CONTEXT_MESSAGES) || DEFAULT_MAX_CONTEXT_MESSAGES;
|
||||
const MAX_ESTIMATED_TOKENS = parseInt(settings.CLAUDE_MEM_GEMINI_MAX_TOKENS) || DEFAULT_MAX_ESTIMATED_TOKENS;
|
||||
|
||||
if (history.length <= MAX_CONTEXT_MESSAGES) {
|
||||
// Check token count even if message count is ok
|
||||
const totalTokens = history.reduce((sum, m) => sum + estimateTokens(m.content), 0);
|
||||
if (totalTokens <= MAX_ESTIMATED_TOKENS) {
|
||||
return history;
|
||||
}
|
||||
}
|
||||
|
||||
// Sliding window: keep most recent messages within limits
|
||||
const truncated: ConversationMessage[] = [];
|
||||
let tokenCount = 0;
|
||||
|
||||
// Process messages in reverse (most recent first)
|
||||
for (let i = history.length - 1; i >= 0; i--) {
|
||||
const msg = history[i];
|
||||
const msgTokens = estimateTokens(msg.content);
|
||||
|
||||
// Always include at least the newest message — an empty contents array
|
||||
// would cause a hard Gemini API error, which is worse than an oversized request.
|
||||
if (truncated.length > 0 && (truncated.length >= MAX_CONTEXT_MESSAGES || tokenCount + msgTokens > MAX_ESTIMATED_TOKENS)) {
|
||||
logger.warn('SDK', 'Context window truncated to prevent runaway costs', {
|
||||
originalMessages: history.length,
|
||||
keptMessages: truncated.length,
|
||||
droppedMessages: i + 1,
|
||||
estimatedTokens: tokenCount,
|
||||
tokenLimit: MAX_ESTIMATED_TOKENS
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
truncated.unshift(msg); // Add to beginning
|
||||
tokenCount += msgTokens;
|
||||
}
|
||||
|
||||
return truncated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert shared ConversationMessage array to Gemini's contents format
|
||||
* Maps 'assistant' role to 'model' for Gemini API compatibility
|
||||
@@ -354,8 +412,8 @@ export class GeminiAgent {
|
||||
}
|
||||
|
||||
/**
|
||||
* Query Gemini via REST API with full conversation history (multi-turn)
|
||||
* Sends the entire conversation context for coherent responses
|
||||
* Query Gemini via REST API with truncated conversation history (multi-turn)
|
||||
* Truncates history to prevent O(N²) token cost growth, then sends for coherent responses
|
||||
*/
|
||||
private async queryGeminiMultiTurn(
|
||||
history: ConversationMessage[],
|
||||
@@ -363,11 +421,13 @@ export class GeminiAgent {
|
||||
model: GeminiModel,
|
||||
rateLimitingEnabled: boolean
|
||||
): Promise<{ content: string; tokensUsed?: number }> {
|
||||
const contents = this.conversationToGeminiContents(history);
|
||||
const totalChars = history.reduce((sum, m) => sum + m.content.length, 0);
|
||||
const truncatedHistory = this.truncateHistory(history);
|
||||
const contents = this.conversationToGeminiContents(truncatedHistory);
|
||||
const totalChars = truncatedHistory.reduce((sum, m) => sum + m.content.length, 0);
|
||||
|
||||
logger.debug('SDK', `Querying Gemini multi-turn (${model})`, {
|
||||
turns: history.length,
|
||||
turns: truncatedHistory.length,
|
||||
totalTurns: history.length,
|
||||
totalChars
|
||||
});
|
||||
|
||||
|
||||
@@ -131,7 +131,8 @@ export class OpenRouterAgent {
|
||||
tokensUsed,
|
||||
null,
|
||||
'OpenRouter',
|
||||
undefined // No lastCwd yet - before message processing
|
||||
undefined, // No lastCwd yet - before message processing
|
||||
model
|
||||
);
|
||||
} else {
|
||||
logger.error('SDK', 'Empty OpenRouter init response - session may lack context', {
|
||||
@@ -202,7 +203,8 @@ export class OpenRouterAgent {
|
||||
tokensUsed,
|
||||
originalTimestamp,
|
||||
'OpenRouter',
|
||||
lastCwd
|
||||
lastCwd,
|
||||
model
|
||||
);
|
||||
|
||||
} else if (message.type === 'summarize') {
|
||||
@@ -244,7 +246,8 @@ export class OpenRouterAgent {
|
||||
tokensUsed,
|
||||
originalTimestamp,
|
||||
'OpenRouter',
|
||||
lastCwd
|
||||
lastCwd,
|
||||
model
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,14 +71,54 @@ export class PaginationHelper {
|
||||
/**
|
||||
* Get paginated observations
|
||||
*/
|
||||
getObservations(offset: number, limit: number, project?: string): PaginatedResult<Observation> {
|
||||
const result = this.paginate<Observation>(
|
||||
'observations',
|
||||
'id, memory_session_id, project, type, title, subtitle, narrative, text, facts, concepts, files_read, files_modified, prompt_number, created_at, created_at_epoch',
|
||||
getObservations(offset: number, limit: number, project?: string, platformSource?: string): PaginatedResult<Observation> {
|
||||
const db = this.dbManager.getSessionStore().db;
|
||||
let query = `
|
||||
SELECT
|
||||
o.id,
|
||||
o.memory_session_id,
|
||||
o.project,
|
||||
COALESCE(s.platform_source, 'claude') as platform_source,
|
||||
o.type,
|
||||
o.title,
|
||||
o.subtitle,
|
||||
o.narrative,
|
||||
o.text,
|
||||
o.facts,
|
||||
o.concepts,
|
||||
o.files_read,
|
||||
o.files_modified,
|
||||
o.prompt_number,
|
||||
o.created_at,
|
||||
o.created_at_epoch
|
||||
FROM observations o
|
||||
LEFT JOIN sdk_sessions s ON o.memory_session_id = s.memory_session_id
|
||||
`;
|
||||
const params: unknown[] = [];
|
||||
const conditions: string[] = [];
|
||||
|
||||
if (project) {
|
||||
conditions.push('o.project = ?');
|
||||
params.push(project);
|
||||
}
|
||||
if (platformSource) {
|
||||
conditions.push(`COALESCE(s.platform_source, 'claude') = ?`);
|
||||
params.push(platformSource);
|
||||
}
|
||||
if (conditions.length > 0) {
|
||||
query += ` WHERE ${conditions.join(' AND ')}`;
|
||||
}
|
||||
|
||||
query += ' ORDER BY o.created_at_epoch DESC LIMIT ? OFFSET ?';
|
||||
params.push(limit + 1, offset);
|
||||
|
||||
const results = db.prepare(query).all(...params) as Observation[];
|
||||
const result: PaginatedResult<Observation> = {
|
||||
items: results.slice(0, limit),
|
||||
hasMore: results.length > limit,
|
||||
offset,
|
||||
limit,
|
||||
project
|
||||
);
|
||||
limit
|
||||
};
|
||||
|
||||
// Strip project paths from file paths before returning
|
||||
return {
|
||||
@@ -90,13 +130,14 @@ export class PaginationHelper {
|
||||
/**
|
||||
* Get paginated summaries
|
||||
*/
|
||||
getSummaries(offset: number, limit: number, project?: string): PaginatedResult<Summary> {
|
||||
getSummaries(offset: number, limit: number, project?: string, platformSource?: string): PaginatedResult<Summary> {
|
||||
const db = this.dbManager.getSessionStore().db;
|
||||
|
||||
let query = `
|
||||
SELECT
|
||||
ss.id,
|
||||
s.content_session_id as session_id,
|
||||
COALESCE(s.platform_source, 'claude') as platform_source,
|
||||
ss.request,
|
||||
ss.investigated,
|
||||
ss.learned,
|
||||
@@ -110,11 +151,22 @@ export class PaginationHelper {
|
||||
`;
|
||||
const params: any[] = [];
|
||||
|
||||
const conditions: string[] = [];
|
||||
|
||||
if (project) {
|
||||
query += ' WHERE ss.project = ?';
|
||||
conditions.push('ss.project = ?');
|
||||
params.push(project);
|
||||
}
|
||||
|
||||
if (platformSource) {
|
||||
conditions.push(`COALESCE(s.platform_source, 'claude') = ?`);
|
||||
params.push(platformSource);
|
||||
}
|
||||
|
||||
if (conditions.length > 0) {
|
||||
query += ` WHERE ${conditions.join(' AND ')}`;
|
||||
}
|
||||
|
||||
query += ' ORDER BY ss.created_at_epoch DESC LIMIT ? OFFSET ?';
|
||||
params.push(limit + 1, offset);
|
||||
|
||||
@@ -132,21 +184,40 @@ export class PaginationHelper {
|
||||
/**
|
||||
* Get paginated user prompts
|
||||
*/
|
||||
getPrompts(offset: number, limit: number, project?: string): PaginatedResult<UserPrompt> {
|
||||
getPrompts(offset: number, limit: number, project?: string, platformSource?: string): PaginatedResult<UserPrompt> {
|
||||
const db = this.dbManager.getSessionStore().db;
|
||||
|
||||
let query = `
|
||||
SELECT up.id, up.content_session_id, s.project, up.prompt_number, up.prompt_text, up.created_at, up.created_at_epoch
|
||||
SELECT
|
||||
up.id,
|
||||
up.content_session_id,
|
||||
s.project,
|
||||
COALESCE(s.platform_source, 'claude') as platform_source,
|
||||
up.prompt_number,
|
||||
up.prompt_text,
|
||||
up.created_at,
|
||||
up.created_at_epoch
|
||||
FROM user_prompts up
|
||||
JOIN sdk_sessions s ON up.content_session_id = s.content_session_id
|
||||
`;
|
||||
const params: any[] = [];
|
||||
|
||||
const conditions: string[] = [];
|
||||
|
||||
if (project) {
|
||||
query += ' WHERE s.project = ?';
|
||||
conditions.push('s.project = ?');
|
||||
params.push(project);
|
||||
}
|
||||
|
||||
if (platformSource) {
|
||||
conditions.push(`COALESCE(s.platform_source, 'claude') = ?`);
|
||||
params.push(platformSource);
|
||||
}
|
||||
|
||||
if (conditions.length > 0) {
|
||||
query += ` WHERE ${conditions.join(' AND ')}`;
|
||||
}
|
||||
|
||||
query += ' ORDER BY up.created_at_epoch DESC LIMIT ? OFFSET ?';
|
||||
params.push(limit + 1, offset);
|
||||
|
||||
|
||||
@@ -382,21 +382,51 @@ export function createPidCapturingSpawn(sessionDbId: number) {
|
||||
env?: NodeJS.ProcessEnv;
|
||||
signal?: AbortSignal;
|
||||
}) => {
|
||||
// Kill any existing process for this session before spawning a new one.
|
||||
// Multiple processes sharing the same --resume UUID waste API credits and
|
||||
// can conflict with each other (Issue #1590).
|
||||
const existing = getProcessBySession(sessionDbId);
|
||||
if (existing && existing.process.exitCode === null) {
|
||||
logger.warn('PROCESS', `Killing duplicate process PID ${existing.pid} before spawning new one for session ${sessionDbId}`, {
|
||||
existingPid: existing.pid,
|
||||
sessionDbId
|
||||
});
|
||||
let exited = false;
|
||||
try {
|
||||
existing.process.kill('SIGTERM');
|
||||
exited = existing.process.exitCode !== null;
|
||||
} catch {
|
||||
// Already dead — safe to unregister immediately
|
||||
exited = true;
|
||||
}
|
||||
|
||||
if (exited) {
|
||||
unregisterProcess(existing.pid);
|
||||
}
|
||||
// If still alive, the 'exit' handler (line ~440) will unregister it.
|
||||
}
|
||||
|
||||
getSupervisor().assertCanSpawn('claude sdk');
|
||||
|
||||
// On Windows, use cmd.exe wrapper for .cmd files to properly handle paths with spaces
|
||||
const useCmdWrapper = process.platform === 'win32' && spawnOptions.command.endsWith('.cmd');
|
||||
const env = sanitizeEnv(spawnOptions.env ?? process.env);
|
||||
|
||||
// Filter empty string args: Bun's spawn() silently drops empty strings from argv,
|
||||
// causing subsequent flags to be consumed as values for the preceding flag.
|
||||
// The Agent SDK may produce empty-string args (e.g., settingSources defaults to []
|
||||
// which joins to ""). Node preserves these, but Bun drops them, breaking CLI parsing.
|
||||
const args = spawnOptions.args.filter(arg => arg !== '');
|
||||
|
||||
const child = useCmdWrapper
|
||||
? spawn('cmd.exe', ['/d', '/c', spawnOptions.command, ...spawnOptions.args], {
|
||||
? spawn('cmd.exe', ['/d', '/c', spawnOptions.command, ...args], {
|
||||
cwd: spawnOptions.cwd,
|
||||
env,
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
signal: spawnOptions.signal,
|
||||
windowsHide: true
|
||||
})
|
||||
: spawn(spawnOptions.command, spawnOptions.args, {
|
||||
: spawn(spawnOptions.command, args, {
|
||||
cwd: spawnOptions.cwd,
|
||||
env,
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
|
||||
@@ -270,7 +270,8 @@ export class SDKAgent {
|
||||
discoveryTokens,
|
||||
originalTimestamp,
|
||||
'SDK',
|
||||
cwdTracker.lastCwd
|
||||
cwdTracker.lastCwd,
|
||||
modelId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -395,7 +395,9 @@ export class SearchManager {
|
||||
* Tool handler: timeline
|
||||
*/
|
||||
async timeline(args: any): Promise<any> {
|
||||
const { anchor, query, depth_before = 10, depth_after = 10, project } = args;
|
||||
const { anchor, query, depth_before, depth_after, project } = args;
|
||||
const depthBefore = depth_before != null ? Number(depth_before) : 10;
|
||||
const depthAfter = depth_after != null ? Number(depth_after) : 10;
|
||||
const cwd = process.cwd();
|
||||
|
||||
// Validate: must provide either anchor or query, not both
|
||||
@@ -464,7 +466,7 @@ export class SearchManager {
|
||||
anchorId = topResult.id;
|
||||
anchorEpoch = topResult.created_at_epoch;
|
||||
logger.debug('SEARCH', 'Query mode: Using observation as timeline anchor', { observationId: topResult.id });
|
||||
timelineData = this.sessionStore.getTimelineAroundObservation(topResult.id, topResult.created_at_epoch, depth_before, depth_after, project);
|
||||
timelineData = this.sessionStore.getTimelineAroundObservation(topResult.id, topResult.created_at_epoch, depthBefore, depthAfter, project);
|
||||
}
|
||||
// MODE 2: Anchor-based timeline
|
||||
else if (typeof anchor === 'number') {
|
||||
@@ -481,7 +483,7 @@ export class SearchManager {
|
||||
}
|
||||
anchorId = anchor;
|
||||
anchorEpoch = obs.created_at_epoch;
|
||||
timelineData = this.sessionStore.getTimelineAroundObservation(anchor, anchorEpoch, depth_before, depth_after, project);
|
||||
timelineData = this.sessionStore.getTimelineAroundObservation(anchor, anchorEpoch, depthBefore, depthAfter, project);
|
||||
} else if (typeof anchor === 'string') {
|
||||
// Session ID or ISO timestamp
|
||||
if (anchor.startsWith('S') || anchor.startsWith('#S')) {
|
||||
@@ -499,7 +501,7 @@ export class SearchManager {
|
||||
}
|
||||
anchorEpoch = sessions[0].created_at_epoch;
|
||||
anchorId = `S${sessionNum}`;
|
||||
timelineData = this.sessionStore.getTimelineAroundTimestamp(anchorEpoch, depth_before, depth_after, project);
|
||||
timelineData = this.sessionStore.getTimelineAroundTimestamp(anchorEpoch, depthBefore, depthAfter, project);
|
||||
} else {
|
||||
// ISO timestamp
|
||||
const date = new Date(anchor);
|
||||
@@ -514,7 +516,7 @@ export class SearchManager {
|
||||
}
|
||||
anchorEpoch = date.getTime();
|
||||
anchorId = anchor;
|
||||
timelineData = this.sessionStore.getTimelineAroundTimestamp(anchorEpoch, depth_before, depth_after, project);
|
||||
timelineData = this.sessionStore.getTimelineAroundTimestamp(anchorEpoch, depthBefore, depthAfter, project);
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
@@ -533,15 +535,15 @@ export class SearchManager {
|
||||
...(timelineData.prompts || []).map((prompt: any) => ({ type: 'prompt' as const, data: prompt, epoch: prompt.created_at_epoch }))
|
||||
];
|
||||
items.sort((a, b) => a.epoch - b.epoch);
|
||||
const filteredItems = this.timelineService.filterByDepth(items, anchorId, anchorEpoch, depth_before, depth_after);
|
||||
const filteredItems = this.timelineService.filterByDepth(items, anchorId, anchorEpoch, depthBefore, depthAfter);
|
||||
|
||||
if (!filteredItems || filteredItems.length === 0) {
|
||||
return {
|
||||
content: [{
|
||||
type: 'text' as const,
|
||||
text: query
|
||||
? `Found observation matching "${query}", but no timeline context available (${depth_before} records before, ${depth_after} records after).`
|
||||
: `No context found around anchor (${depth_before} records before, ${depth_after} records after)`
|
||||
? `Found observation matching "${query}", but no timeline context available (${depthBefore} records before, ${depthAfter} records after).`
|
||||
: `No context found around anchor (${depthBefore} records before, ${depthAfter} records after)`
|
||||
}]
|
||||
};
|
||||
}
|
||||
@@ -559,7 +561,7 @@ export class SearchManager {
|
||||
lines.push(`# Timeline around anchor: ${anchorId}`);
|
||||
}
|
||||
|
||||
lines.push(`**Window:** ${depth_before} records before -> ${depth_after} records after | **Items:** ${filteredItems?.length ?? 0}`);
|
||||
lines.push(`**Window:** ${depthBefore} records before -> ${depthAfter} records after | **Items:** ${filteredItems?.length ?? 0}`);
|
||||
lines.push('');
|
||||
|
||||
|
||||
@@ -1443,7 +1445,9 @@ export class SearchManager {
|
||||
* Tool handler: get_context_timeline
|
||||
*/
|
||||
async getContextTimeline(args: any): Promise<any> {
|
||||
const { anchor, depth_before = 10, depth_after = 10, project } = args;
|
||||
const { anchor, depth_before, depth_after, project } = args;
|
||||
const depthBefore = depth_before != null ? Number(depth_before) : 10;
|
||||
const depthAfter = depth_after != null ? Number(depth_after) : 10;
|
||||
const cwd = process.cwd();
|
||||
let anchorEpoch: number;
|
||||
let anchorId: string | number = anchor;
|
||||
@@ -1463,7 +1467,7 @@ export class SearchManager {
|
||||
};
|
||||
}
|
||||
anchorEpoch = obs.created_at_epoch;
|
||||
timelineData = this.sessionStore.getTimelineAroundObservation(anchor, anchorEpoch, depth_before, depth_after, project);
|
||||
timelineData = this.sessionStore.getTimelineAroundObservation(anchor, anchorEpoch, depthBefore, depthAfter, project);
|
||||
} else if (typeof anchor === 'string') {
|
||||
// Session ID or ISO timestamp
|
||||
if (anchor.startsWith('S') || anchor.startsWith('#S')) {
|
||||
@@ -1481,7 +1485,7 @@ export class SearchManager {
|
||||
}
|
||||
anchorEpoch = sessions[0].created_at_epoch;
|
||||
anchorId = `S${sessionNum}`;
|
||||
timelineData = this.sessionStore.getTimelineAroundTimestamp(anchorEpoch, depth_before, depth_after, project);
|
||||
timelineData = this.sessionStore.getTimelineAroundTimestamp(anchorEpoch, depthBefore, depthAfter, project);
|
||||
} else {
|
||||
// ISO timestamp
|
||||
const date = new Date(anchor);
|
||||
@@ -1495,7 +1499,7 @@ export class SearchManager {
|
||||
};
|
||||
}
|
||||
anchorEpoch = date.getTime(); // Keep as milliseconds
|
||||
timelineData = this.sessionStore.getTimelineAroundTimestamp(anchorEpoch, depth_before, depth_after, project);
|
||||
timelineData = this.sessionStore.getTimelineAroundTimestamp(anchorEpoch, depthBefore, depthAfter, project);
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
@@ -1514,14 +1518,14 @@ export class SearchManager {
|
||||
...timelineData.prompts.map(prompt => ({ type: 'prompt' as const, data: prompt, epoch: prompt.created_at_epoch }))
|
||||
];
|
||||
items.sort((a, b) => a.epoch - b.epoch);
|
||||
const filteredItems = this.timelineService.filterByDepth(items, anchorId, anchorEpoch, depth_before, depth_after);
|
||||
const filteredItems = this.timelineService.filterByDepth(items, anchorId, anchorEpoch, depthBefore, depthAfter);
|
||||
|
||||
if (!filteredItems || filteredItems.length === 0) {
|
||||
const anchorDate = new Date(anchorEpoch).toLocaleString();
|
||||
return {
|
||||
content: [{
|
||||
type: 'text' as const,
|
||||
text: `No context found around ${anchorDate} (${depth_before} records before, ${depth_after} records after)`
|
||||
text: `No context found around ${anchorDate} (${depthBefore} records before, ${depthAfter} records after)`
|
||||
}]
|
||||
};
|
||||
}
|
||||
@@ -1531,7 +1535,7 @@ export class SearchManager {
|
||||
|
||||
// Header
|
||||
lines.push(`# Timeline around anchor: ${anchorId}`);
|
||||
lines.push(`**Window:** ${depth_before} records before -> ${depth_after} records after | **Items:** ${filteredItems?.length ?? 0}`);
|
||||
lines.push(`**Window:** ${depthBefore} records before -> ${depthAfter} records after | **Items:** ${filteredItems?.length ?? 0}`);
|
||||
lines.push('');
|
||||
|
||||
|
||||
@@ -1655,7 +1659,9 @@ export class SearchManager {
|
||||
* Tool handler: get_timeline_by_query
|
||||
*/
|
||||
async getTimelineByQuery(args: any): Promise<any> {
|
||||
const { query, mode = 'auto', depth_before = 10, depth_after = 10, limit = 5, project } = args;
|
||||
const { query, mode = 'auto', depth_before, depth_after, limit = 5, project } = args;
|
||||
const depthBefore = depth_before != null ? Number(depth_before) : 10;
|
||||
const depthAfter = depth_after != null ? Number(depth_after) : 10;
|
||||
const cwd = process.cwd();
|
||||
|
||||
// Step 1: Search for observations
|
||||
@@ -1736,8 +1742,8 @@ export class SearchManager {
|
||||
const timelineData = this.sessionStore.getTimelineAroundObservation(
|
||||
topResult.id,
|
||||
topResult.created_at_epoch,
|
||||
depth_before,
|
||||
depth_after,
|
||||
depthBefore,
|
||||
depthAfter,
|
||||
project
|
||||
);
|
||||
|
||||
@@ -1748,13 +1754,13 @@ export class SearchManager {
|
||||
...(timelineData.prompts || []).map(prompt => ({ type: 'prompt' as const, data: prompt, epoch: prompt.created_at_epoch }))
|
||||
];
|
||||
items.sort((a, b) => a.epoch - b.epoch);
|
||||
const filteredItems = this.timelineService.filterByDepth(items, topResult.id, 0, depth_before, depth_after);
|
||||
const filteredItems = this.timelineService.filterByDepth(items, topResult.id, 0, depthBefore, depthAfter);
|
||||
|
||||
if (!filteredItems || filteredItems.length === 0) {
|
||||
return {
|
||||
content: [{
|
||||
type: 'text' as const,
|
||||
text: `Found observation #${topResult.id} matching "${query}", but no timeline context available (${depth_before} records before, ${depth_after} records after).`
|
||||
text: `Found observation #${topResult.id} matching "${query}", but no timeline context available (${depthBefore} records before, ${depthAfter} records after).`
|
||||
}]
|
||||
};
|
||||
}
|
||||
@@ -1765,7 +1771,7 @@ export class SearchManager {
|
||||
// Header
|
||||
lines.push(`# Timeline for query: "${query}"`);
|
||||
lines.push(`**Anchor:** Observation #${topResult.id} - ${topResult.title || 'Untitled'}`);
|
||||
lines.push(`**Window:** ${depth_before} records before -> ${depth_after} records after | **Items:** ${filteredItems?.length ?? 0}`);
|
||||
lines.push(`**Window:** ${depthBefore} records before -> ${depthAfter} records after | **Items:** ${filteredItems?.length ?? 0}`);
|
||||
lines.push('');
|
||||
|
||||
|
||||
|
||||
@@ -17,6 +17,64 @@ import { SessionQueueProcessor } from '../queue/SessionQueueProcessor.js';
|
||||
import { getProcessBySession, ensureProcessExit } from './ProcessRegistry.js';
|
||||
import { getSupervisor } from '../../supervisor/index.js';
|
||||
|
||||
/** Idle threshold before a stuck generator (zombie subprocess) is force-killed. */
|
||||
export const MAX_GENERATOR_IDLE_MS = 5 * 60 * 1000; // 5 minutes
|
||||
|
||||
/** Idle threshold before a no-generator session with no pending work is reaped. */
|
||||
export const MAX_SESSION_IDLE_MS = 15 * 60 * 1000; // 15 minutes
|
||||
|
||||
/**
|
||||
* Minimal process interface used by detectStaleGenerator — compatible with
|
||||
* both the real Bun.Subprocess / ChildProcess shapes and test mocks.
|
||||
*/
|
||||
export interface StaleGeneratorProcess {
|
||||
exitCode: number | null;
|
||||
kill(signal?: string): boolean | void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Minimal session fields required to evaluate stale-generator status.
|
||||
* This is a subset of ActiveSession, allowing unit tests to pass plain objects.
|
||||
*/
|
||||
export interface StaleGeneratorCandidate {
|
||||
generatorPromise: Promise<void> | null;
|
||||
lastGeneratorActivity: number;
|
||||
abortController: AbortController;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect whether a session's generator is stuck (zombie subprocess) and, if so,
|
||||
* SIGKILL the subprocess and abort the controller.
|
||||
*
|
||||
* Extracted from reapStaleSessions() so tests can import and exercise the exact
|
||||
* same logic rather than duplicating it locally. (Issue #1652)
|
||||
*
|
||||
* @param session - session to inspect
|
||||
* @param proc - tracked subprocess (may be undefined if not in ProcessRegistry)
|
||||
* @param now - current timestamp (defaults to Date.now(); pass explicit value in tests)
|
||||
* @returns true if the session was marked stale, false otherwise
|
||||
*/
|
||||
export function detectStaleGenerator(
|
||||
session: StaleGeneratorCandidate,
|
||||
proc: StaleGeneratorProcess | undefined,
|
||||
now = Date.now()
|
||||
): boolean {
|
||||
if (!session.generatorPromise) return false;
|
||||
|
||||
const generatorIdleMs = now - session.lastGeneratorActivity;
|
||||
if (generatorIdleMs <= MAX_GENERATOR_IDLE_MS) return false;
|
||||
|
||||
// Kill subprocess to unblock stuck for-await
|
||||
if (proc && proc.exitCode === null) {
|
||||
try {
|
||||
proc.kill('SIGKILL');
|
||||
} catch {}
|
||||
}
|
||||
// Signal the SDK agent loop to exit
|
||||
session.abortController.abort();
|
||||
return true;
|
||||
}
|
||||
|
||||
export class SessionManager {
|
||||
private dbManager: DatabaseManager;
|
||||
private sessions: Map<number, ActiveSession> = new Map();
|
||||
@@ -77,6 +135,9 @@ export class SessionManager {
|
||||
});
|
||||
session.project = dbSession.project;
|
||||
}
|
||||
if (dbSession.platform_source && dbSession.platform_source !== session.platformSource) {
|
||||
session.platformSource = dbSession.platform_source;
|
||||
}
|
||||
|
||||
// Update userPrompt for continuation prompts
|
||||
if (currentUserPrompt) {
|
||||
@@ -144,6 +205,7 @@ export class SessionManager {
|
||||
contentSessionId: dbSession.content_session_id,
|
||||
memorySessionId: null, // Always start fresh - SDK will capture new ID
|
||||
project: dbSession.project,
|
||||
platformSource: dbSession.platform_source,
|
||||
userPrompt,
|
||||
pendingMessages: [],
|
||||
abortController: new AbortController(),
|
||||
@@ -360,10 +422,12 @@ export class SessionManager {
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly MAX_SESSION_IDLE_MS = 15 * 60 * 1000; // 15 minutes
|
||||
|
||||
/**
|
||||
* Reap sessions with no active generator and no pending work that have been idle too long.
|
||||
* Also reaps sessions whose generator has been stuck (no lastGeneratorActivity update) for
|
||||
* longer than MAX_GENERATOR_IDLE_MS — these are zombie subprocesses that will never exit
|
||||
* on their own because the orphan reaper skips sessions in the active sessions map. (Issue #1652)
|
||||
*
|
||||
* This unblocks the orphan reaper which skips processes for "active" sessions. (Issue #1168)
|
||||
*/
|
||||
async reapStaleSessions(): Promise<number> {
|
||||
@@ -371,8 +435,31 @@ export class SessionManager {
|
||||
const staleSessionIds: number[] = [];
|
||||
|
||||
for (const [sessionDbId, session] of this.sessions) {
|
||||
// Skip sessions with active generators
|
||||
if (session.generatorPromise) continue;
|
||||
// Sessions with active generators — check for stuck/zombie generators (Issue #1652)
|
||||
if (session.generatorPromise) {
|
||||
const generatorIdleMs = now - session.lastGeneratorActivity;
|
||||
if (generatorIdleMs > MAX_GENERATOR_IDLE_MS) {
|
||||
logger.warn('SESSION', `Stale generator detected for session ${sessionDbId} (no activity for ${Math.round(generatorIdleMs / 60000)}m) — force-killing subprocess`, {
|
||||
sessionDbId,
|
||||
generatorIdleMs
|
||||
});
|
||||
// Force-kill the subprocess to unblock the stuck for-await in SDKAgent.
|
||||
// Without this the generator is blocked on `for await (const msg of queryResult)`
|
||||
// and will never exit even after abort() is called.
|
||||
const trackedProcess = getProcessBySession(sessionDbId);
|
||||
if (trackedProcess && trackedProcess.process.exitCode === null) {
|
||||
try {
|
||||
trackedProcess.process.kill('SIGKILL');
|
||||
} catch (err) {
|
||||
logger.warn('SESSION', 'Failed to SIGKILL subprocess for stale generator', { sessionDbId }, err as Error);
|
||||
}
|
||||
}
|
||||
// Signal the SDK agent loop to exit after the subprocess dies
|
||||
session.abortController.abort();
|
||||
staleSessionIds.push(sessionDbId);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip sessions with pending work
|
||||
const pendingCount = this.getPendingStore().getPendingCount(sessionDbId);
|
||||
@@ -380,13 +467,13 @@ export class SessionManager {
|
||||
|
||||
// No generator + no pending work + old enough = stale
|
||||
const sessionAge = now - session.startTime;
|
||||
if (sessionAge > SessionManager.MAX_SESSION_IDLE_MS) {
|
||||
if (sessionAge > MAX_SESSION_IDLE_MS) {
|
||||
logger.warn('SESSION', `Reaping idle session ${sessionDbId} (no activity for >${Math.round(MAX_SESSION_IDLE_MS / 60000)}m)`, { sessionDbId });
|
||||
staleSessionIds.push(sessionDbId);
|
||||
}
|
||||
}
|
||||
|
||||
for (const sessionDbId of staleSessionIds) {
|
||||
logger.warn('SESSION', `Reaping stale session ${sessionDbId} (no activity for >${Math.round(SessionManager.MAX_SESSION_IDLE_MS / 60000)}m)`, { sessionDbId });
|
||||
await this.deleteSession(sessionDbId);
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,8 @@ export async function processAgentResponse(
|
||||
discoveryTokens: number,
|
||||
originalTimestamp: number | null,
|
||||
agentName: string,
|
||||
projectRoot?: string
|
||||
projectRoot?: string,
|
||||
modelId?: string
|
||||
): Promise<void> {
|
||||
// Track generator activity for stale detection (Issue #1099)
|
||||
session.lastGeneratorActivity = Date.now();
|
||||
@@ -115,7 +116,8 @@ export async function processAgentResponse(
|
||||
summaryForStore,
|
||||
session.lastPromptNumber,
|
||||
discoveryTokens,
|
||||
originalTimestamp ?? undefined
|
||||
originalTimestamp ?? undefined,
|
||||
modelId
|
||||
);
|
||||
|
||||
// Log storage result with IDs for end-to-end traceability
|
||||
@@ -124,6 +126,10 @@ export async function processAgentResponse(
|
||||
memorySessionId: session.memorySessionId
|
||||
});
|
||||
|
||||
// Track whether a summary record was stored so the status endpoint can expose this
|
||||
// to the Stop hook for silent-summary-loss detection (#1633)
|
||||
session.lastSummaryStored = result.summaryId !== null;
|
||||
|
||||
// CLAIM-CONFIRM: Now that storage succeeded, confirm all processing messages (delete from queue)
|
||||
// This is the critical step that prevents message loss on generator crash
|
||||
const pendingStore = sessionManager.getPendingMessageStore();
|
||||
@@ -236,6 +242,7 @@ async function syncAndBroadcastObservations(
|
||||
id: obsId,
|
||||
memory_session_id: session.memorySessionId,
|
||||
session_id: session.contentSessionId,
|
||||
platform_source: session.platformSource,
|
||||
type: obs.type,
|
||||
title: obs.title,
|
||||
subtitle: obs.subtitle,
|
||||
@@ -325,12 +332,13 @@ async function syncAndBroadcastSummary(
|
||||
broadcastSummary(worker, {
|
||||
id: result.summaryId,
|
||||
session_id: session.contentSessionId,
|
||||
request: summary!.request,
|
||||
investigated: summary!.investigated,
|
||||
learned: summary!.learned,
|
||||
completed: summary!.completed,
|
||||
next_steps: summary!.next_steps,
|
||||
notes: summary!.notes,
|
||||
platform_source: session.platformSource,
|
||||
request: summaryForStore!.request,
|
||||
investigated: summaryForStore!.investigated,
|
||||
learned: summaryForStore!.learned,
|
||||
completed: summaryForStore!.completed,
|
||||
next_steps: summaryForStore!.next_steps,
|
||||
notes: summaryForStore!.notes,
|
||||
project: session.project,
|
||||
prompt_number: session.lastPromptNumber,
|
||||
created_at_epoch: result.createdAtEpoch
|
||||
|
||||
@@ -33,6 +33,7 @@ export interface ObservationSSEPayload {
|
||||
id: number;
|
||||
memory_session_id: string | null;
|
||||
session_id: string;
|
||||
platform_source: string;
|
||||
type: string;
|
||||
title: string | null;
|
||||
subtitle: string | null;
|
||||
@@ -50,6 +51,7 @@ export interface ObservationSSEPayload {
|
||||
export interface SummarySSEPayload {
|
||||
id: number;
|
||||
session_id: string;
|
||||
platform_source: string;
|
||||
request: string | null;
|
||||
investigated: string | null;
|
||||
learned: string | null;
|
||||
|
||||
@@ -23,6 +23,7 @@ export class SessionEventBroadcaster {
|
||||
id: number;
|
||||
content_session_id: string;
|
||||
project: string;
|
||||
platform_source: string;
|
||||
prompt_number: number;
|
||||
prompt_text: string;
|
||||
created_at_epoch: number;
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user