Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9215c7e1f5 | |||
| b694f233db | |||
| c9e0301e3e | |||
| 3bbacb8fa4 | |||
| d69a95bd29 | |||
| 90b209081c |
@@ -10,7 +10,7 @@
|
||||
"plugins": [
|
||||
{
|
||||
"name": "claude-mem",
|
||||
"version": "4.3.2",
|
||||
"version": "4.3.4",
|
||||
"source": "./plugin",
|
||||
"description": "Persistent memory system for Claude Code - context compression across sessions"
|
||||
}
|
||||
|
||||
@@ -128,6 +128,18 @@ git tag vX.Y.Z -m "Release vX.Y.Z: [Brief description]"
|
||||
git push && git push --tags
|
||||
```
|
||||
|
||||
### 9. Create GitHub Release
|
||||
```bash
|
||||
# Create GitHub release from the tag
|
||||
# Extract release notes from CLAUDE.md for the current version
|
||||
gh release create vX.Y.Z --title "vX.Y.Z" --notes "[Paste relevant section from CLAUDE.md]"
|
||||
|
||||
# Or generate notes automatically from commits
|
||||
gh release create vX.Y.Z --title "vX.Y.Z" --generate-notes
|
||||
```
|
||||
|
||||
**IMPORTANT**: Always create the GitHub release immediately after pushing the tag. This makes the release discoverable to users and triggers any automated workflows.
|
||||
|
||||
## CLAUDE.md Templates
|
||||
|
||||
### PATCH Version Template
|
||||
@@ -191,7 +203,10 @@ User: "Fixed the memory leak in the search function"
|
||||
You: Determine → PATCH
|
||||
Calculate → 4.2.8 → 4.2.9
|
||||
Update all four files
|
||||
Build and commit
|
||||
Create git tag v4.2.9
|
||||
Push commit and tags
|
||||
Create GitHub release v4.2.9
|
||||
CLAUDE.md: Focus on the fix and impact
|
||||
```
|
||||
|
||||
@@ -201,7 +216,10 @@ User: "Added web search MCP integration"
|
||||
You: Determine → MINOR (new feature)
|
||||
Calculate → 4.2.8 → 4.3.0
|
||||
Update all four files
|
||||
Build and commit
|
||||
Create git tag v4.3.0
|
||||
Push commit and tags
|
||||
Create GitHub release v4.3.0
|
||||
CLAUDE.md: Describe feature and usage
|
||||
```
|
||||
|
||||
@@ -211,7 +229,10 @@ User: "Rewrote storage layer, old data needs migration"
|
||||
You: Determine → MAJOR (breaking change)
|
||||
Calculate → 4.2.8 → 5.0.0
|
||||
Update all four files
|
||||
Build and commit
|
||||
Create git tag v5.0.0
|
||||
Push commit and tags
|
||||
Create GitHub release v5.0.0
|
||||
CLAUDE.md: Include migration steps
|
||||
```
|
||||
|
||||
@@ -220,6 +241,7 @@ You: Determine → MAJOR (breaking change)
|
||||
**ALWAYS verify:**
|
||||
- [ ] All FOUR files have matching version numbers (package.json, marketplace.json, plugin.json, CLAUDE.md)
|
||||
- [ ] Git tag created with format vX.Y.Z
|
||||
- [ ] GitHub release created from the tag
|
||||
- [ ] CLAUDE.md entry matches version type (patch/minor/major)
|
||||
- [ ] Breaking changes are clearly marked with ⚠️
|
||||
- [ ] File references use format: `path/to/file.ts:line_number`
|
||||
@@ -230,6 +252,7 @@ You: Determine → MAJOR (breaking change)
|
||||
- Update only one, two, or three files - ALL FOUR must be updated
|
||||
- Skip the verification step
|
||||
- Forget to create git tag
|
||||
- Forget to create GitHub release
|
||||
- Forget to ask user if version type is unclear
|
||||
- Use vague descriptions in CLAUDE.md
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
Claude-mem is a persistent memory compression system that preserves context across Claude Code sessions. It automatically captures tool usage observations, processes them through the Claude Agent SDK, and makes summaries available to future sessions.
|
||||
|
||||
**Current Version**: 4.3.1
|
||||
**Current Version**: 4.3.4
|
||||
**License**: AGPL-3.0
|
||||
**Author**: Alex Newman (@thedotmack)
|
||||
|
||||
@@ -212,10 +212,40 @@ npm run build && git commit -a -m "Build and update" && git push && cd ~/.claude
|
||||
|
||||
For detailed version history and changelog, see [CHANGELOG.md](CHANGELOG.md).
|
||||
|
||||
**Current Version**: 4.3.2
|
||||
**Current Version**: 4.3.4
|
||||
|
||||
### Recent Highlights
|
||||
|
||||
#### v4.3.4 (2025-11-01)
|
||||
**Breaking Changes**: None (patch version)
|
||||
|
||||
**Fixes**:
|
||||
- Fixed SessionStart hooks running on session resume (plugin/hooks/hooks.json:4)
|
||||
- Added matcher configuration to only run SessionStart hooks on startup, clear, or compact events
|
||||
- Prevents unnecessary hook execution and improves performance on session resume
|
||||
|
||||
**Technical Details**:
|
||||
- Modified: plugin/hooks/hooks.json:4 (added `"matcher": "startup|clear|compact"`)
|
||||
- Impact: Hooks now skip execution when resuming existing sessions
|
||||
|
||||
#### v4.3.3 (2025-10-27)
|
||||
**Breaking Changes**: None (patch version)
|
||||
|
||||
**Improvements**:
|
||||
- Made session display count configurable via constant (DISPLAY_SESSION_COUNT = 8) in src/hooks/context-hook.ts:11
|
||||
- Added first-time setup detection with helpful user messaging in src/hooks/user-message-hook.ts:12-39
|
||||
- Improved user experience: First install message clarifies why it appears under "Plugin Hook Error"
|
||||
|
||||
**Fixes**:
|
||||
- Cleaned up profanity in code comments (src/hooks/context-hook.ts:3)
|
||||
- Fixed first-time setup UX by detecting missing node_modules and showing informative message
|
||||
|
||||
**Technical Details**:
|
||||
- Modified: src/hooks/context-hook.ts:11 (configurable DISPLAY_SESSION_COUNT constant)
|
||||
- Modified: src/hooks/user-message-hook.ts:12-39 (first-time setup detection and messaging)
|
||||
- Modified: plugin/scripts/context-hook.js (rebuilt)
|
||||
- Modified: plugin/scripts/user-message-hook.js (rebuilt)
|
||||
|
||||
#### v4.3.2 (2025-10-27)
|
||||
**Breaking Changes**: None (patch version)
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ module.exports = {
|
||||
max_memory_restart: '500M',
|
||||
min_uptime: '10s',
|
||||
max_restarts: 10,
|
||||
restart_delay: 4000,
|
||||
restart_delay: 0,
|
||||
|
||||
env: {
|
||||
NODE_ENV: 'production',
|
||||
@@ -45,8 +45,8 @@ module.exports = {
|
||||
log_type: 'json',
|
||||
|
||||
// Process management
|
||||
kill_timeout: 5000,
|
||||
listen_timeout: 10000,
|
||||
kill_timeout: 1000,
|
||||
listen_timeout: 3000,
|
||||
shutdown_with_message: true,
|
||||
|
||||
// PM2 Plus (optional monitoring)
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "claude-mem",
|
||||
"version": "4.3.2",
|
||||
"version": "4.3.4",
|
||||
"description": "Memory compression system for Claude Code - persist context across sessions",
|
||||
"keywords": [
|
||||
"claude",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "claude-mem",
|
||||
"version": "4.3.2",
|
||||
"version": "4.3.4",
|
||||
"description": "Persistent memory system for Claude Code - seamlessly preserve context across sessions",
|
||||
"author": {
|
||||
"name": "Alex Newman"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
import W from"path";import{stdin as P}from"process";import de from"better-sqlite3";import{join as T,dirname as re,basename as Se}from"path";import{homedir as j}from"os";import{existsSync as Ie,mkdirSync as ne}from"fs";import{fileURLToPath as ie}from"url";function oe(){return typeof __dirname<"u"?__dirname:re(ie(import.meta.url))}var ae=oe(),I=process.env.CLAUDE_MEM_DATA_DIR||T(j(),".claude-mem"),U=process.env.CLAUDE_CONFIG_DIR||T(j(),".claude"),Le=T(I,"archives"),ve=T(I,"logs"),Ae=T(I,"trash"),ye=T(I,"backups"),Ce=T(I,"settings.json"),Y=T(I,"claude-mem.db"),De=T(U,"settings.json"),ke=T(U,"commands"),xe=T(U,"CLAUDE.md");function K(a){ne(a,{recursive:!0})}function V(){return T(ae,"..","..")}var $=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))($||{}),M=class{level;useColor;constructor(){let e=process.env.CLAUDE_MEM_LOG_LEVEL?.toUpperCase()||"INFO";this.level=$[e]??1,this.useColor=process.stdout.isTTY??!1}correlationId(e,t){return`obs-${e}-${t}`}sessionId(e){return`session-${e}`}formatData(e){if(e==null)return"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return e.toString();if(typeof e=="object"){if(e instanceof Error)return this.level===0?`${e.message}
|
||||
import W from"path";import{stdin as P}from"process";import ce from"better-sqlite3";import{join as T,dirname as ne,basename as be}from"path";import{homedir as j}from"os";import{existsSync as Oe,mkdirSync as ie}from"fs";import{fileURLToPath as oe}from"url";function ae(){return typeof __dirname<"u"?__dirname:ne(oe(import.meta.url))}var de=ae(),I=process.env.CLAUDE_MEM_DATA_DIR||T(j(),".claude-mem"),U=process.env.CLAUDE_CONFIG_DIR||T(j(),".claude"),ve=T(I,"archives"),Ae=T(I,"logs"),ye=T(I,"trash"),Ce=T(I,"backups"),De=T(I,"settings.json"),Y=T(I,"claude-mem.db"),ke=T(U,"settings.json"),xe=T(U,"commands"),we=T(U,"CLAUDE.md");function K(a){ie(a,{recursive:!0})}function V(){return T(de,"..","..")}var $=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))($||{}),M=class{level;useColor;constructor(){let e=process.env.CLAUDE_MEM_LOG_LEVEL?.toUpperCase()||"INFO";this.level=$[e]??1,this.useColor=process.stdout.isTTY??!1}correlationId(e,t){return`obs-${e}-${t}`}sessionId(e){return`session-${e}`}formatData(e){if(e==null)return"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return e.toString();if(typeof e=="object"){if(e instanceof Error)return this.level===0?`${e.message}
|
||||
${e.stack}`:e.message;if(Array.isArray(e))return`[${e.length} items]`;let t=Object.keys(e);return t.length===0?"{}":t.length<=3?JSON.stringify(e):`{${t.length} keys: ${t.slice(0,3).join(", ")}...}`}return String(e)}formatTool(e,t){if(!t)return e;try{let s=typeof t=="string"?JSON.parse(t):t;if(e==="Bash"&&s.command){let r=s.command.length>50?s.command.substring(0,50)+"...":s.command;return`${e}(${r})`}if(e==="Read"&&s.file_path){let r=s.file_path.split("/").pop()||s.file_path;return`${e}(${r})`}if(e==="Edit"&&s.file_path){let r=s.file_path.split("/").pop()||s.file_path;return`${e}(${r})`}if(e==="Write"&&s.file_path){let r=s.file_path.split("/").pop()||s.file_path;return`${e}(${r})`}return e}catch{return e}}log(e,t,s,r,o){if(e<this.level)return;let c=new Date().toISOString().replace("T"," ").substring(0,23),p=$[e].padEnd(5),u=t.padEnd(6),O="";r?.correlationId?O=`[${r.correlationId}] `:r?.sessionId&&(O=`[session-${r.sessionId}] `);let b="";o!=null&&(this.level===0&&typeof o=="object"?b=`
|
||||
`+JSON.stringify(o,null,2):b=" "+this.formatData(o));let n="";if(r){let{sessionId:h,sdkSessionId:k,correlationId:L,...C}=r;Object.keys(C).length>0&&(n=` {${Object.entries(C).map(([d,m])=>`${d}=${m}`).join(", ")}}`)}let N=`[${c}] [${p}] [${u}] ${O}${s}${n}${b}`;e===3?console.error(N):console.log(N)}debug(e,t,s,r){this.log(0,e,t,s,r)}info(e,t,s,r){this.log(1,e,t,s,r)}warn(e,t,s,r){this.log(2,e,t,s,r)}error(e,t,s,r){this.log(3,e,t,s,r)}dataIn(e,t,s,r){this.info(e,`\u2192 ${t}`,s,r)}dataOut(e,t,s,r){this.info(e,`\u2190 ${t}`,s,r)}success(e,t,s,r){this.info(e,`\u2713 ${t}`,s,r)}failure(e,t,s,r){this.error(e,`\u2717 ${t}`,s,r)}timing(e,t,s,r){this.info(e,`\u23F1 ${t}`,r,{duration:`${s}ms`})}},q=new M;var D=class{db;constructor(){K(I),this.db=new de(Y),this.db.pragma("journal_mode = WAL"),this.db.pragma("synchronous = NORMAL"),this.db.pragma("foreign_keys = ON"),this.initializeSchema(),this.ensureWorkerPortColumn(),this.ensurePromptTrackingColumns(),this.removeSessionSummariesUniqueConstraint(),this.addObservationHierarchicalFields(),this.makeObservationsTextNullable(),this.createUserPromptsTable()}initializeSchema(){try{this.db.exec(`
|
||||
`+JSON.stringify(o,null,2):b=" "+this.formatData(o));let n="";if(r){let{sessionId:h,sdkSessionId:k,correlationId:L,...C}=r;Object.keys(C).length>0&&(n=` {${Object.entries(C).map(([d,m])=>`${d}=${m}`).join(", ")}}`)}let N=`[${c}] [${p}] [${u}] ${O}${s}${n}${b}`;e===3?console.error(N):console.log(N)}debug(e,t,s,r){this.log(0,e,t,s,r)}info(e,t,s,r){this.log(1,e,t,s,r)}warn(e,t,s,r){this.log(2,e,t,s,r)}error(e,t,s,r){this.log(3,e,t,s,r)}dataIn(e,t,s,r){this.info(e,`\u2192 ${t}`,s,r)}dataOut(e,t,s,r){this.info(e,`\u2190 ${t}`,s,r)}success(e,t,s,r){this.info(e,`\u2713 ${t}`,s,r)}failure(e,t,s,r){this.error(e,`\u2717 ${t}`,s,r)}timing(e,t,s,r){this.info(e,`\u23F1 ${t}`,r,{duration:`${s}ms`})}},q=new M;var D=class{db;constructor(){K(I),this.db=new ce(Y),this.db.pragma("journal_mode = WAL"),this.db.pragma("synchronous = NORMAL"),this.db.pragma("foreign_keys = ON"),this.initializeSchema(),this.ensureWorkerPortColumn(),this.ensurePromptTrackingColumns(),this.removeSessionSummariesUniqueConstraint(),this.addObservationHierarchicalFields(),this.makeObservationsTextNullable(),this.createUserPromptsTable()}initializeSchema(){try{this.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS schema_versions (
|
||||
id INTEGER PRIMARY KEY,
|
||||
version INTEGER UNIQUE NOT NULL,
|
||||
@@ -306,7 +306,7 @@ ${e.stack}`:e.message;if(Array.isArray(e))return`[${e.length} items]`;let t=Obje
|
||||
UPDATE sdk_sessions
|
||||
SET status = 'failed', completed_at = ?, completed_at_epoch = ?
|
||||
WHERE status = 'active'
|
||||
`).run(e.toISOString(),t).changes}close(){this.db.close()}};import X from"path";import{existsSync as F}from"fs";import{spawn as ce}from"child_process";var pe=parseInt(process.env.CLAUDE_MEM_WORKER_PORT||"37777",10),ue=`http://127.0.0.1:${pe}/health`;async function J(){try{return(await fetch(ue,{signal:AbortSignal.timeout(500)})).ok}catch{return!1}}async function Q(){try{if(await J())return!0;console.error("[claude-mem] Worker not responding, starting...");let a=V(),e=X.join(a,"plugin","scripts","worker-service.cjs");if(!F(e))return console.error(`[claude-mem] Worker service not found at ${e}`),!1;let t=X.join(a,"ecosystem.config.cjs"),s=X.join(a,"node_modules",".bin","pm2");if(!F(s))throw new Error(`PM2 binary not found at ${s}. This is a bundled dependency - try running: npm install`);if(!F(t))throw new Error(`PM2 ecosystem config not found at ${t}. Plugin installation may be corrupted.`);let r=ce(s,["start",t],{detached:!0,stdio:"ignore",cwd:a});r.on("error",o=>{throw new Error(`Failed to spawn PM2: ${o.message}`)}),r.unref(),console.error("[claude-mem] Worker started with PM2");for(let o=0;o<3;o++)if(await new Promise(c=>setTimeout(c,500)),await J())return console.error("[claude-mem] Worker is healthy"),!0;return console.error("[claude-mem] Worker failed to become healthy after startup"),!1}catch(a){return console.error(`[claude-mem] Failed to start worker: ${a.message}`),!1}}var i={reset:"\x1B[0m",bright:"\x1B[1m",dim:"\x1B[2m",cyan:"\x1B[36m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",magenta:"\x1B[35m",gray:"\x1B[90m",red:"\x1B[31m"};function G(a){if(!a)return[];let e=JSON.parse(a);return Array.isArray(e)?e:[]}function le(a){return new Date(a).toLocaleString("en-US",{month:"short",day:"numeric",hour:"numeric",minute:"2-digit",hour12:!0})}function me(a){return new Date(a).toLocaleString("en-US",{hour:"numeric",minute:"2-digit",hour12:!0})}function _e(a){return new Date(a).toLocaleString("en-US",{month:"short",day:"numeric",year:"numeric"})}function Ee(a){return a?Math.ceil(a.length/4):0}function Te(a,e){return W.isAbsolute(a)?W.relative(e,a):a}function he(a,e){if(e.length===0)return[];let t=e.map(()=>"?").join(",");return a.db.prepare(`
|
||||
`).run(e.toISOString(),t).changes}close(){this.db.close()}};import X from"path";import{existsSync as F}from"fs";import{spawn as pe}from"child_process";var ue=parseInt(process.env.CLAUDE_MEM_WORKER_PORT||"37777",10),le=`http://127.0.0.1:${ue}/health`;async function J(){try{return(await fetch(le,{signal:AbortSignal.timeout(500)})).ok}catch{return!1}}async function Q(){try{if(await J())return!0;console.error("[claude-mem] Worker not responding, starting...");let a=V(),e=X.join(a,"plugin","scripts","worker-service.cjs");if(!F(e))return console.error(`[claude-mem] Worker service not found at ${e}`),!1;let t=X.join(a,"ecosystem.config.cjs"),s=X.join(a,"node_modules",".bin","pm2");if(!F(s))throw new Error(`PM2 binary not found at ${s}. This is a bundled dependency - try running: npm install`);if(!F(t))throw new Error(`PM2 ecosystem config not found at ${t}. Plugin installation may be corrupted.`);let r=pe(s,["start",t],{detached:!0,stdio:"ignore",cwd:a});r.on("error",o=>{throw new Error(`Failed to spawn PM2: ${o.message}`)}),r.unref(),console.error("[claude-mem] Worker started with PM2");for(let o=0;o<3;o++)if(await new Promise(c=>setTimeout(c,500)),await J())return console.error("[claude-mem] Worker is healthy"),!0;return console.error("[claude-mem] Worker failed to become healthy after startup"),!1}catch(a){return console.error(`[claude-mem] Failed to start worker: ${a.message}`),!1}}var z=8,i={reset:"\x1B[0m",bright:"\x1B[1m",dim:"\x1B[2m",cyan:"\x1B[36m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",magenta:"\x1B[35m",gray:"\x1B[90m",red:"\x1B[31m"};function G(a){if(!a)return[];let e=JSON.parse(a);return Array.isArray(e)?e:[]}function me(a){return new Date(a).toLocaleString("en-US",{month:"short",day:"numeric",hour:"numeric",minute:"2-digit",hour12:!0})}function _e(a){return new Date(a).toLocaleString("en-US",{hour:"numeric",minute:"2-digit",hour12:!0})}function Ee(a){return new Date(a).toLocaleString("en-US",{month:"short",day:"numeric",year:"numeric"})}function Te(a){return a?Math.ceil(a.length/4):0}function he(a,e){return W.isAbsolute(a)?W.relative(e,a):a}function ge(a,e){if(e.length===0)return[];let t=e.map(()=>"?").join(",");return a.db.prepare(`
|
||||
SELECT
|
||||
id, sdk_session_id, type, title, subtitle, narrative,
|
||||
facts, concepts, files_read, files_modified,
|
||||
@@ -314,18 +314,18 @@ ${e.stack}`:e.message;if(Array.isArray(e))return`[${e.length} items]`;let t=Obje
|
||||
FROM observations
|
||||
WHERE sdk_session_id IN (${t})
|
||||
ORDER BY created_at_epoch DESC
|
||||
`).all(...e)}function z(a,e=!1,t=!1){Q();let s=a?.cwd??process.cwd(),r=s?W.basename(s):"unknown-project",o=new D,c=o.db.prepare(`
|
||||
`).all(...e)}function Z(a,e=!1,t=!1){Q();let s=a?.cwd??process.cwd(),r=s?W.basename(s):"unknown-project",o=new D,c=o.db.prepare(`
|
||||
SELECT id, sdk_session_id, request, completed, next_steps, created_at, created_at_epoch
|
||||
FROM session_summaries
|
||||
WHERE project = ?
|
||||
ORDER BY created_at_epoch DESC
|
||||
LIMIT 4
|
||||
`).all(r);if(c.length===0)return o.close(),e?`
|
||||
LIMIT ?
|
||||
`).all(r,z+1);if(c.length===0)return o.close(),e?`
|
||||
${i.bright}${i.cyan}\u{1F4DD} [${r}] recent context${i.reset}
|
||||
${i.gray}${"\u2500".repeat(60)}${i.reset}
|
||||
|
||||
${i.dim}No previous sessions found for this project yet.${i.reset}
|
||||
`:`# [${r}] recent context
|
||||
|
||||
No previous sessions found for this project yet.`;let p=c.slice(0,3),u=[...new Set(p.map(N=>N.sdk_session_id))],b=he(o,u).filter(N=>{let h=G(N.concepts);return h.includes("what-changed")||h.includes("how-it-works")||h.includes("problem-solution")||h.includes("gotcha")||h.includes("discovery")||h.includes("why-it-exists")||h.includes("decision")||h.includes("trade-off")}),n=[];if(e?(n.push(""),n.push(`${i.bright}${i.cyan}\u{1F4DD} [${r}] recent context${i.reset}`),n.push(`${i.gray}${"\u2500".repeat(60)}${i.reset}`),n.push("")):(n.push(`# [${r}] recent context`),n.push("")),b.length>0){e?(n.push(`${i.dim}Legend: \u{1F3AF} session-request | \u{1F534} gotcha | \u{1F7E1} problem-solution | \u{1F535} how-it-works | \u{1F7E2} what-changed | \u{1F7E3} discovery | \u{1F7E0} why-it-exists | \u{1F7E4} decision | \u2696\uFE0F trade-off${i.reset}`),n.push("")):(n.push("**Legend:** \u{1F3AF} session-request | \u{1F534} gotcha | \u{1F7E1} problem-solution | \u{1F535} how-it-works | \u{1F7E2} what-changed | \u{1F7E3} discovery | \u{1F7E0} why-it-exists | \u{1F7E4} decision | \u2696\uFE0F trade-off"),n.push("")),e?(n.push(`${i.dim}\u{1F4A1} Progressive Disclosure: This index shows WHAT exists (titles) and retrieval COST (token counts).${i.reset}`),n.push(`${i.dim} \u2192 Use MCP search tools to fetch full observation details on-demand (Layer 2)${i.reset}`),n.push(`${i.dim} \u2192 Prefer searching observations over re-reading code for past decisions and learnings${i.reset}`),n.push(`${i.dim} \u2192 Critical types (\u{1F534} gotcha, \u{1F7E4} decision, \u2696\uFE0F trade-off) often worth fetching immediately${i.reset}`),n.push("")):(n.push("\u{1F4A1} **Progressive Disclosure:** This index shows WHAT exists (titles) and retrieval COST (token counts)."),n.push("- Use MCP search tools to fetch full observation details on-demand (Layer 2)"),n.push("- Prefer searching observations over re-reading code for past decisions and learnings"),n.push("- Critical types (\u{1F534} gotcha, \u{1F7E4} decision, \u2696\uFE0F trade-off) often worth fetching immediately"),n.push(""));let N=c[0]?.id,h=p.map((d,m)=>{let l=m===0?null:c[m+1];return{...d,displayEpoch:l?l.created_at_epoch:d.created_at_epoch,displayTime:l?l.created_at:d.created_at,isMostRecent:d.id===N}}),k=[...b.map(d=>({type:"observation",data:d})),...h.map(d=>({type:"summary",data:d}))];k.sort((d,m)=>{let l=d.type==="observation"?d.data.created_at_epoch:d.data.displayEpoch,R=m.type==="observation"?m.data.created_at_epoch:m.data.displayEpoch;return l-R});let L=new Map;for(let d of k){let m=d.type==="observation"?d.data.created_at:d.data.displayTime,l=_e(m);L.has(l)||L.set(l,[]),L.get(l).push(d)}let C=Array.from(L.entries()).sort((d,m)=>{let l=new Date(d[0]).getTime(),R=new Date(m[0]).getTime();return l-R});for(let[d,m]of C){e?(n.push(`${i.bright}${i.cyan}${d}${i.reset}`),n.push("")):(n.push(`### ${d}`),n.push(""));let l=null,R="",v=!1;for(let x of m)if(x.type==="summary"){v&&(n.push(""),v=!1,l=null,R="");let _=x.data,A=`${_.request||"Session started"} (${le(_.displayTime)})`,S=_.isMostRecent?"":`claude-mem://session-summary/${_.id}`;if(e){let E=S?`${i.dim}[${S}]${i.reset}`:"";n.push(`\u{1F3AF} ${i.yellow}#S${_.id}${i.reset} ${A} ${E}`)}else{let E=S?` [\u2192](${S})`:"";n.push(`**\u{1F3AF} #S${_.id}** ${A}${E}`)}n.push("")}else{let _=x.data,A=G(_.files_modified),S=A.length>0?Te(A[0],s):"General";S!==l&&(v&&n.push(""),e?n.push(`${i.dim}${S}${i.reset}`):n.push(`**${S}**`),e||(n.push("| ID | Time | T | Title | Tokens |"),n.push("|----|------|---|-------|--------|")),l=S,v=!0,R="");let E=G(_.concepts),f="\u2022";E.includes("gotcha")?f="\u{1F534}":E.includes("decision")?f="\u{1F7E4}":E.includes("trade-off")?f="\u2696\uFE0F":E.includes("problem-solution")?f="\u{1F7E1}":E.includes("discovery")?f="\u{1F7E3}":E.includes("why-it-exists")?f="\u{1F7E0}":E.includes("how-it-works")?f="\u{1F535}":E.includes("what-changed")&&(f="\u{1F7E2}");let y=me(_.created_at),H=_.title||"Untitled",w=Ee(_.narrative),B=y!==R,ee=B?y:"";if(R=y,e){let se=B?`${i.dim}${y}${i.reset}`:" ".repeat(y.length),te=w>0?`${i.dim}(~${w}t)${i.reset}`:"";n.push(` ${i.dim}#${_.id}${i.reset} ${se} ${f} ${H} ${te}`)}else n.push(`| #${_.id} | ${ee||"\u2033"} | ${f} | ${H} | ~${w} |`)}v&&n.push("")}let g=c[0];g&&(g.completed||g.next_steps)&&(g.completed&&(e?n.push(`${i.green}Completed:${i.reset} ${g.completed}`):n.push(`**Completed**: ${g.completed}`),n.push("")),g.next_steps&&(e?n.push(`${i.magenta}Next Steps:${i.reset} ${g.next_steps}`):n.push(`**Next Steps**: ${g.next_steps}`),n.push(""))),e?n.push(`${i.dim}Use claude-mem MCP search to access records with the given ID${i.reset}`):n.push("*Use claude-mem MCP search to access records with the given ID*")}return o.close(),n.join(`
|
||||
`).trimEnd()}var Z=process.argv.includes("--index"),ge=process.argv.includes("--colors");if(P.isTTY||ge){let a=z(void 0,!0,Z);console.log(a),process.exit(0)}else{let a="";P.on("data",e=>a+=e),P.on("end",()=>{let e=a.trim()?JSON.parse(a):void 0,s={hookSpecificOutput:{hookEventName:"SessionStart",additionalContext:z(e,!1,Z)}};console.log(JSON.stringify(s)),process.exit(0)})}
|
||||
No previous sessions found for this project yet.`;let p=c.slice(0,z),u=[...new Set(p.map(N=>N.sdk_session_id))],b=ge(o,u).filter(N=>{let h=G(N.concepts);return h.includes("what-changed")||h.includes("how-it-works")||h.includes("problem-solution")||h.includes("gotcha")||h.includes("discovery")||h.includes("why-it-exists")||h.includes("decision")||h.includes("trade-off")}),n=[];if(e?(n.push(""),n.push(`${i.bright}${i.cyan}\u{1F4DD} [${r}] recent context${i.reset}`),n.push(`${i.gray}${"\u2500".repeat(60)}${i.reset}`),n.push("")):(n.push(`# [${r}] recent context`),n.push("")),b.length>0){e?(n.push(`${i.dim}Legend: \u{1F3AF} session-request | \u{1F534} gotcha | \u{1F7E1} problem-solution | \u{1F535} how-it-works | \u{1F7E2} what-changed | \u{1F7E3} discovery | \u{1F7E0} why-it-exists | \u{1F7E4} decision | \u2696\uFE0F trade-off${i.reset}`),n.push("")):(n.push("**Legend:** \u{1F3AF} session-request | \u{1F534} gotcha | \u{1F7E1} problem-solution | \u{1F535} how-it-works | \u{1F7E2} what-changed | \u{1F7E3} discovery | \u{1F7E0} why-it-exists | \u{1F7E4} decision | \u2696\uFE0F trade-off"),n.push("")),e?(n.push(`${i.dim}\u{1F4A1} Progressive Disclosure: This index shows WHAT exists (titles) and retrieval COST (token counts).${i.reset}`),n.push(`${i.dim} \u2192 Use MCP search tools to fetch full observation details on-demand (Layer 2)${i.reset}`),n.push(`${i.dim} \u2192 Prefer searching observations over re-reading code for past decisions and learnings${i.reset}`),n.push(`${i.dim} \u2192 Critical types (\u{1F534} gotcha, \u{1F7E4} decision, \u2696\uFE0F trade-off) often worth fetching immediately${i.reset}`),n.push("")):(n.push("\u{1F4A1} **Progressive Disclosure:** This index shows WHAT exists (titles) and retrieval COST (token counts)."),n.push("- Use MCP search tools to fetch full observation details on-demand (Layer 2)"),n.push("- Prefer searching observations over re-reading code for past decisions and learnings"),n.push("- Critical types (\u{1F534} gotcha, \u{1F7E4} decision, \u2696\uFE0F trade-off) often worth fetching immediately"),n.push(""));let N=c[0]?.id,h=p.map((d,m)=>{let l=m===0?null:c[m+1];return{...d,displayEpoch:l?l.created_at_epoch:d.created_at_epoch,displayTime:l?l.created_at:d.created_at,isMostRecent:d.id===N}}),k=[...b.map(d=>({type:"observation",data:d})),...h.map(d=>({type:"summary",data:d}))];k.sort((d,m)=>{let l=d.type==="observation"?d.data.created_at_epoch:d.data.displayEpoch,R=m.type==="observation"?m.data.created_at_epoch:m.data.displayEpoch;return l-R});let L=new Map;for(let d of k){let m=d.type==="observation"?d.data.created_at:d.data.displayTime,l=Ee(m);L.has(l)||L.set(l,[]),L.get(l).push(d)}let C=Array.from(L.entries()).sort((d,m)=>{let l=new Date(d[0]).getTime(),R=new Date(m[0]).getTime();return l-R});for(let[d,m]of C){e?(n.push(`${i.bright}${i.cyan}${d}${i.reset}`),n.push("")):(n.push(`### ${d}`),n.push(""));let l=null,R="",v=!1;for(let x of m)if(x.type==="summary"){v&&(n.push(""),v=!1,l=null,R="");let _=x.data,A=`${_.request||"Session started"} (${me(_.displayTime)})`,S=_.isMostRecent?"":`claude-mem://session-summary/${_.id}`;if(e){let E=S?`${i.dim}[${S}]${i.reset}`:"";n.push(`\u{1F3AF} ${i.yellow}#S${_.id}${i.reset} ${A} ${E}`)}else{let E=S?` [\u2192](${S})`:"";n.push(`**\u{1F3AF} #S${_.id}** ${A}${E}`)}n.push("")}else{let _=x.data,A=G(_.files_modified),S=A.length>0?he(A[0],s):"General";S!==l&&(v&&n.push(""),e?n.push(`${i.dim}${S}${i.reset}`):n.push(`**${S}**`),e||(n.push("| ID | Time | T | Title | Tokens |"),n.push("|----|------|---|-------|--------|")),l=S,v=!0,R="");let E=G(_.concepts),f="\u2022";E.includes("gotcha")?f="\u{1F534}":E.includes("decision")?f="\u{1F7E4}":E.includes("trade-off")?f="\u2696\uFE0F":E.includes("problem-solution")?f="\u{1F7E1}":E.includes("discovery")?f="\u{1F7E3}":E.includes("why-it-exists")?f="\u{1F7E0}":E.includes("how-it-works")?f="\u{1F535}":E.includes("what-changed")&&(f="\u{1F7E2}");let y=_e(_.created_at),H=_.title||"Untitled",w=Te(_.narrative),B=y!==R,se=B?y:"";if(R=y,e){let te=B?`${i.dim}${y}${i.reset}`:" ".repeat(y.length),re=w>0?`${i.dim}(~${w}t)${i.reset}`:"";n.push(` ${i.dim}#${_.id}${i.reset} ${te} ${f} ${H} ${re}`)}else n.push(`| #${_.id} | ${se||"\u2033"} | ${f} | ${H} | ~${w} |`)}v&&n.push("")}let g=c[0];g&&(g.completed||g.next_steps)&&(g.completed&&(e?n.push(`${i.green}Completed:${i.reset} ${g.completed}`):n.push(`**Completed**: ${g.completed}`),n.push("")),g.next_steps&&(e?n.push(`${i.magenta}Next Steps:${i.reset} ${g.next_steps}`):n.push(`**Next Steps**: ${g.next_steps}`),n.push(""))),e?n.push(`${i.dim}Use claude-mem MCP search to access records with the given ID${i.reset}`):n.push("*Use claude-mem MCP search to access records with the given ID*")}return o.close(),n.join(`
|
||||
`).trimEnd()}var ee=process.argv.includes("--index"),fe=process.argv.includes("--colors");if(P.isTTY||fe){let a=Z(void 0,!0,ee);console.log(a),process.exit(0)}else{let a="";P.on("data",e=>a+=e),P.on("end",()=>{let e=a.trim()?JSON.parse(a):void 0,s={hookSpecificOutput:{hookEventName:"SessionStart",additionalContext:Z(e,!1,ee)}};console.log(JSON.stringify(s)),process.exit(0)})}
|
||||
|
||||
@@ -1,7 +1,25 @@
|
||||
#!/usr/bin/env node
|
||||
import{execSync as e}from"child_process";import{join as r}from"path";import{homedir as n}from"os";try{let o=r(n(),".claude","plugins","marketplaces","thedotmack","plugin","scripts","context-hook.js"),t=e(`node "${o}" --colors`,{encoding:"utf8"});console.error(`
|
||||
import{execSync as r}from"child_process";import{join as o}from"path";import{homedir as t}from"os";import{existsSync as s}from"fs";var i=o(t(),".claude","plugins","marketplaces","thedotmack"),a=o(i,"node_modules");s(a)||(console.error(`
|
||||
---
|
||||
\u{1F389} Note: This appears under Plugin Hook Error, but it's not an error. That's the only option for
|
||||
user messages in Claude Code UI until a better method is provided.
|
||||
---
|
||||
|
||||
\u26A0\uFE0F Claude-Mem: First-Time Setup
|
||||
|
||||
Dependencies have been installed in the background. This only happens once.
|
||||
|
||||
\u{1F4A1} TIPS:
|
||||
\u2022 Memories will start generating while you work
|
||||
\u2022 Use /init to write or update your CLAUDE.md for better project context
|
||||
\u2022 Try /clear after one session to see what context looks like
|
||||
|
||||
Thank you for installing Claude-Mem!
|
||||
|
||||
This message was not added to your startup context, so you can continue working as normal.
|
||||
`),process.exit(3));try{let e=o(t(),".claude","plugins","marketplaces","thedotmack","plugin","scripts","context-hook.js"),n=r(`node "${e}" --colors`,{encoding:"utf8"});console.error(`
|
||||
|
||||
\u{1F4DD} Claude-Mem Context Loaded
|
||||
\u2139\uFE0F Note: This appears as stderr but is informational only
|
||||
|
||||
`+t)}catch(o){console.error(`\u274C Failed to load context display: ${o}`)}process.exit(3);
|
||||
`+n)}catch(e){console.error(`\u274C Failed to load context display: ${e}`)}process.exit(3);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Context Hook - SessionStart
|
||||
* Consolidated entry point + logic (no try-catch bullshit)
|
||||
* Consolidated entry point + logic
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
@@ -8,6 +8,9 @@ import { stdin } from 'process';
|
||||
import { SessionStore } from '../services/sqlite/SessionStore.js';
|
||||
import { ensureWorkerRunning } from '../shared/worker-utils.js';
|
||||
|
||||
// Configuration: Number of sessions to display in context
|
||||
const DISPLAY_SESSION_COUNT = 8;
|
||||
|
||||
export interface SessionStartInput {
|
||||
session_id?: string;
|
||||
transcript_path?: string;
|
||||
@@ -128,14 +131,14 @@ function contextHook(input?: SessionStartInput, useColors: boolean = false, useI
|
||||
|
||||
const db = new SessionStore();
|
||||
|
||||
// Get last 4 summaries (use 4th for offset calculation)
|
||||
// Get last N summaries (use N+1 for offset calculation)
|
||||
const recentSummaries = db.db.prepare(`
|
||||
SELECT id, sdk_session_id, request, completed, next_steps, created_at, created_at_epoch
|
||||
FROM session_summaries
|
||||
WHERE project = ?
|
||||
ORDER BY created_at_epoch DESC
|
||||
LIMIT 4
|
||||
`).all(project) as Array<{ id: number; sdk_session_id: string; request: string | null; completed: string | null; next_steps: string | null; created_at: string; created_at_epoch: number }>;
|
||||
LIMIT ?
|
||||
`).all(project, DISPLAY_SESSION_COUNT + 1) as Array<{ id: number; sdk_session_id: string; request: string | null; completed: string | null; next_steps: string | null; created_at: string; created_at_epoch: number }>;
|
||||
|
||||
if (recentSummaries.length === 0) {
|
||||
db.close();
|
||||
@@ -145,8 +148,8 @@ function contextHook(input?: SessionStartInput, useColors: boolean = false, useI
|
||||
return `# [${project}] recent context\n\nNo previous sessions found for this project yet.`;
|
||||
}
|
||||
|
||||
// Extract unique session IDs from first 3 summaries
|
||||
const displaySummaries = recentSummaries.slice(0, 3);
|
||||
// Extract unique session IDs from first N summaries
|
||||
const displaySummaries = recentSummaries.slice(0, DISPLAY_SESSION_COUNT);
|
||||
const sessionIds = [...new Set(displaySummaries.map(s => s.sdk_session_id))];
|
||||
|
||||
// Get all observations from these sessions
|
||||
|
||||
@@ -9,6 +9,35 @@
|
||||
import { execSync } from "child_process";
|
||||
import { join } from "path";
|
||||
import { homedir } from "os";
|
||||
import { existsSync } from "fs";
|
||||
|
||||
// Check if node_modules exists - if not, this is first run
|
||||
const pluginDir = join(homedir(), '.claude', 'plugins', 'marketplaces', 'thedotmack');
|
||||
const nodeModulesPath = join(pluginDir, 'node_modules');
|
||||
|
||||
if (!existsSync(nodeModulesPath)) {
|
||||
// First-time installation - dependencies not yet installed
|
||||
console.error(`
|
||||
---
|
||||
🎉 Note: This appears under Plugin Hook Error, but it's not an error. That's the only option for
|
||||
user messages in Claude Code UI until a better method is provided.
|
||||
---
|
||||
|
||||
⚠️ Claude-Mem: First-Time Setup
|
||||
|
||||
Dependencies have been installed in the background. This only happens once.
|
||||
|
||||
💡 TIPS:
|
||||
• Memories will start generating while you work
|
||||
• Use /init to write or update your CLAUDE.md for better project context
|
||||
• Try /clear after one session to see what context looks like
|
||||
|
||||
Thank you for installing Claude-Mem!
|
||||
|
||||
This message was not added to your startup context, so you can continue working as normal.
|
||||
`);
|
||||
process.exit(3);
|
||||
}
|
||||
|
||||
try {
|
||||
// Cross-platform path to context-hook.js in the installed plugin
|
||||
|
||||
@@ -1,588 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* SDK Worker Process
|
||||
* Background server that processes tool observations via Unix socket
|
||||
*/
|
||||
|
||||
// Bun-specific ImportMeta extension
|
||||
declare global {
|
||||
interface ImportMeta {
|
||||
main: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
import net from 'net';
|
||||
import { unlinkSync, existsSync } from 'fs';
|
||||
import { execSync } from 'child_process';
|
||||
import { query } from '@anthropic-ai/claude-agent-sdk';
|
||||
import type { SDKUserMessage, SDKSystemMessage } from '@anthropic-ai/claude-agent-sdk';
|
||||
import { SessionStore } from '../services/sqlite/SessionStore.js';
|
||||
import { getWorkerSocketPath } from '../shared/paths.js';
|
||||
import { buildInitPrompt, buildObservationPrompt, buildSummaryPrompt } from './prompts.js';
|
||||
import { parseObservations, parseSummary } from './parser.js';
|
||||
import type { SDKSession } from './prompts.js';
|
||||
|
||||
const MODEL = 'claude-sonnet-4-5';
|
||||
const DISALLOWED_TOOLS = ['Glob', 'Grep', 'ListMcpResourcesTool', 'WebSearch'];
|
||||
|
||||
/**
|
||||
* Find Claude Code executable path using which (Unix/Mac) or where (Windows)
|
||||
*/
|
||||
function findClaudePath(): string {
|
||||
try {
|
||||
// Try environment variable first
|
||||
if (process.env.CLAUDE_CODE_PATH) {
|
||||
return process.env.CLAUDE_CODE_PATH;
|
||||
}
|
||||
|
||||
// Use which on Unix/Mac, where on Windows
|
||||
const command = process.platform === 'win32' ? 'where claude' : 'which claude';
|
||||
const result = execSync(command, { encoding: 'utf8' }).trim();
|
||||
|
||||
// On Windows, 'where' returns multiple lines if there are multiple matches, take the first
|
||||
const path = result.split('\n')[0].trim();
|
||||
|
||||
if (!path) {
|
||||
throw new Error('Claude executable not found in PATH');
|
||||
}
|
||||
|
||||
console.error(`[SDK Worker] Found Claude executable: ${path}`);
|
||||
return path;
|
||||
} catch (error: any) {
|
||||
console.error('[SDK Worker] Failed to find Claude executable:', error.message);
|
||||
throw new Error('Claude Code executable not found. Please ensure claude is in your PATH or set CLAUDE_CODE_PATH environment variable.');
|
||||
}
|
||||
}
|
||||
|
||||
interface ObservationMessage {
|
||||
type: 'observation';
|
||||
tool_name: string;
|
||||
tool_input: string;
|
||||
tool_output: string;
|
||||
}
|
||||
|
||||
interface FinalizeMessage {
|
||||
type: 'finalize';
|
||||
}
|
||||
|
||||
type WorkerMessage = ObservationMessage | FinalizeMessage;
|
||||
|
||||
/**
|
||||
* Main worker process entry point
|
||||
*/
|
||||
export async function main() {
|
||||
console.error('[SDK Worker DEBUG] main() called');
|
||||
const sessionDbId = parseInt(process.argv[2], 10);
|
||||
console.error(`[SDK Worker DEBUG] Session DB ID: ${sessionDbId}`);
|
||||
|
||||
if (!sessionDbId) {
|
||||
console.error('[SDK Worker] Missing session ID argument');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const worker = new SDKWorker(sessionDbId);
|
||||
console.error('[SDK Worker DEBUG] SDKWorker instance created');
|
||||
await worker.run();
|
||||
}
|
||||
|
||||
/**
|
||||
* SDK Worker - Unix socket server that processes observations
|
||||
*/
|
||||
class SDKWorker {
|
||||
private sessionDbId: number;
|
||||
private db: SessionStore;
|
||||
private socketPath: string;
|
||||
private server: net.Server | null = null;
|
||||
private sdkSessionId: string | null = null;
|
||||
private project: string = '';
|
||||
private userPrompt: string = '';
|
||||
private abortController: AbortController;
|
||||
private isFinalized = false;
|
||||
private pendingMessages: WorkerMessage[] = [];
|
||||
|
||||
constructor(sessionDbId: number) {
|
||||
this.sessionDbId = sessionDbId;
|
||||
this.db = new SessionStore();
|
||||
this.abortController = new AbortController();
|
||||
this.socketPath = getWorkerSocketPath(sessionDbId);
|
||||
console.error('[claude-mem worker] Worker instance created', {
|
||||
sessionDbId,
|
||||
socketPath: this.socketPath
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Main run loop
|
||||
*/
|
||||
async run(): Promise<void> {
|
||||
console.error('[claude-mem worker] Worker run() started', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
socketPath: this.socketPath
|
||||
});
|
||||
|
||||
try {
|
||||
// Load session info
|
||||
const session = await this.loadSession();
|
||||
if (!session) {
|
||||
console.error('[claude-mem worker] Session not found in database', {
|
||||
sessionDbId: this.sessionDbId
|
||||
});
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.error('[claude-mem worker] Session loaded successfully', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
project: session.project,
|
||||
sdkSessionId: session.sdk_session_id,
|
||||
userPromptLength: session.user_prompt?.length || 0
|
||||
});
|
||||
|
||||
this.project = session.project;
|
||||
this.userPrompt = session.user_prompt;
|
||||
|
||||
// Start Unix socket server
|
||||
await this.startSocketServer();
|
||||
console.error('[claude-mem worker] Socket server started successfully', {
|
||||
socketPath: this.socketPath,
|
||||
sessionDbId: this.sessionDbId
|
||||
});
|
||||
|
||||
// Run SDK agent with streaming input
|
||||
console.error('[claude-mem worker] Starting SDK agent', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
model: MODEL
|
||||
});
|
||||
await this.runSDKAgent();
|
||||
|
||||
// Mark session as completed
|
||||
console.error('[claude-mem worker] SDK agent completed, marking session as completed', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId
|
||||
});
|
||||
this.db.markSessionCompleted(this.sessionDbId);
|
||||
this.db.close();
|
||||
this.cleanup();
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('[claude-mem worker] Fatal error in run()', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
error: error.message,
|
||||
stack: error.stack
|
||||
});
|
||||
this.db.markSessionFailed(this.sessionDbId);
|
||||
this.db.close();
|
||||
this.cleanup();
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start Unix socket server to receive messages from hooks
|
||||
*/
|
||||
private async startSocketServer(): Promise<void> {
|
||||
console.error(`[SDK Worker DEBUG] Starting socket server...`);
|
||||
console.error(`[SDK Worker DEBUG] Socket path: ${this.socketPath}`);
|
||||
|
||||
// Clean up old socket if it exists
|
||||
if (existsSync(this.socketPath)) {
|
||||
console.error(`[SDK Worker DEBUG] Removing existing socket`);
|
||||
unlinkSync(this.socketPath);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
console.error(`[SDK Worker DEBUG] Creating net server...`);
|
||||
this.server = net.createServer((socket) => {
|
||||
console.error('[claude-mem worker] Socket connection received', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
socketPath: this.socketPath
|
||||
});
|
||||
let buffer = '';
|
||||
|
||||
socket.on('data', (chunk) => {
|
||||
console.error('[claude-mem worker] Data received on socket', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
chunkSize: chunk.length
|
||||
});
|
||||
buffer += chunk.toString();
|
||||
|
||||
// Try to parse complete JSON messages (separated by newlines)
|
||||
const lines = buffer.split('\n');
|
||||
buffer = lines.pop() || ''; // Keep incomplete line in buffer
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.trim()) {
|
||||
try {
|
||||
const message: WorkerMessage = JSON.parse(line);
|
||||
console.error('[claude-mem worker] Message received from socket', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
messageType: message.type,
|
||||
rawMessage: line.substring(0, 500) // Truncate to avoid massive logs
|
||||
});
|
||||
this.handleMessage(message);
|
||||
} catch (err) {
|
||||
console.error('[claude-mem worker] Invalid message - failed to parse JSON', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
rawLine: line.substring(0, 200)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('error', (err) => {
|
||||
console.error('[claude-mem worker] Socket connection error', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
error: err.message,
|
||||
stack: err.stack
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
this.server.on('error', (err: any) => {
|
||||
if (err.code === 'EADDRINUSE') {
|
||||
console.error('[claude-mem worker] Socket already in use', {
|
||||
socketPath: this.socketPath,
|
||||
sessionDbId: this.sessionDbId
|
||||
});
|
||||
} else {
|
||||
console.error('[claude-mem worker] Server error', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
error: err.message,
|
||||
code: err.code,
|
||||
stack: err.stack
|
||||
});
|
||||
}
|
||||
reject(err);
|
||||
});
|
||||
|
||||
this.server.listen(this.socketPath, () => {
|
||||
console.error(`[SDK Worker DEBUG] listen() callback fired`);
|
||||
console.error(`[SDK Worker DEBUG] Checking if socket exists: ${existsSync(this.socketPath)}`);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle incoming message from hook
|
||||
*/
|
||||
private handleMessage(message: WorkerMessage): void {
|
||||
console.error('[claude-mem worker] Processing message in handleMessage()', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
messageType: message.type,
|
||||
pendingMessagesCount: this.pendingMessages.length
|
||||
});
|
||||
|
||||
this.pendingMessages.push(message);
|
||||
|
||||
if (message.type === 'finalize') {
|
||||
console.error('[claude-mem worker] FINALIZE message detected - queued for processing', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
pendingMessagesCount: this.pendingMessages.length
|
||||
});
|
||||
// DON'T set isFinalized here - let the generator set it after yielding finalize prompt
|
||||
} else if (message.type === 'observation') {
|
||||
console.error('[claude-mem worker] Observation message queued', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
toolName: message.tool_name,
|
||||
inputLength: message.tool_input?.length || 0,
|
||||
outputLength: message.tool_output?.length || 0
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load session from database
|
||||
*/
|
||||
private async loadSession(): Promise<SDKSession | null> {
|
||||
const db = this.db as any;
|
||||
const query = db.db.query(`
|
||||
SELECT id, sdk_session_id, project, user_prompt
|
||||
FROM sdk_sessions
|
||||
WHERE id = ?
|
||||
LIMIT 1
|
||||
`);
|
||||
|
||||
const session = query.get(this.sessionDbId);
|
||||
return session as SDKSession | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run SDK agent with streaming input mode
|
||||
*/
|
||||
private async runSDKAgent(): Promise<void> {
|
||||
const claudePath = findClaudePath();
|
||||
console.error(`[SDK Worker DEBUG] Using Claude executable: ${claudePath}`);
|
||||
|
||||
const queryResult = query({
|
||||
prompt: this.createMessageGenerator(),
|
||||
options: {
|
||||
model: MODEL,
|
||||
disallowedTools: DISALLOWED_TOOLS,
|
||||
abortController: this.abortController,
|
||||
pathToClaudeCodeExecutable: claudePath
|
||||
}
|
||||
});
|
||||
|
||||
// Iterate over SDK messages
|
||||
for await (const message of queryResult) {
|
||||
// Handle system init message to capture session ID
|
||||
if (message.type === 'system' && message.subtype === 'init') {
|
||||
const systemMsg = message as SDKSystemMessage;
|
||||
if (systemMsg.session_id) {
|
||||
console.error('[claude-mem worker] SDK session initialized', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: systemMsg.session_id
|
||||
});
|
||||
this.sdkSessionId = systemMsg.session_id;
|
||||
this.db.updateSDKSessionId(this.sessionDbId, systemMsg.session_id);
|
||||
}
|
||||
}
|
||||
// Handle assistant messages
|
||||
else if (message.type === 'assistant') {
|
||||
const content = message.message.content;
|
||||
// Extract text content from message
|
||||
const textContent = Array.isArray(content)
|
||||
? content.filter((c: any) => c.type === 'text').map((c: any) => c.text).join('\n')
|
||||
: typeof content === 'string' ? content : '';
|
||||
|
||||
console.error('[claude-mem worker] SDK agent response received', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId,
|
||||
contentLength: textContent.length,
|
||||
contentPreview: textContent.substring(0, 200)
|
||||
});
|
||||
// Parse and store observations from agent response
|
||||
this.handleAgentMessage(textContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create async message generator for SDK streaming input
|
||||
* Now pulls from socket messages instead of polling database
|
||||
*/
|
||||
private async* createMessageGenerator(): AsyncIterable<SDKUserMessage> {
|
||||
// Yield initial prompt
|
||||
const claudeSessionId = `session-${this.sessionDbId}`;
|
||||
const initPrompt = buildInitPrompt(this.project, claudeSessionId, this.userPrompt);
|
||||
console.error('[claude-mem worker] Yielding initial prompt to SDK agent', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
claudeSessionId,
|
||||
project: this.project,
|
||||
promptLength: initPrompt.length
|
||||
});
|
||||
yield {
|
||||
type: 'user',
|
||||
session_id: this.sdkSessionId || claudeSessionId,
|
||||
parent_tool_use_id: null,
|
||||
message: {
|
||||
role: 'user',
|
||||
content: initPrompt
|
||||
}
|
||||
};
|
||||
|
||||
// Process messages as they arrive via socket
|
||||
while (!this.isFinalized) {
|
||||
// Wait for messages to arrive
|
||||
if (this.pendingMessages.length === 0) {
|
||||
await this.sleep(100); // Short sleep, just to yield control
|
||||
continue;
|
||||
}
|
||||
|
||||
// Process all pending messages
|
||||
while (this.pendingMessages.length > 0) {
|
||||
const message = this.pendingMessages.shift()!;
|
||||
|
||||
if (message.type === 'finalize') {
|
||||
console.error('[claude-mem worker] Processing FINALIZE message in generator', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId
|
||||
});
|
||||
this.isFinalized = true;
|
||||
const session = await this.loadSession();
|
||||
if (session) {
|
||||
const finalizePrompt = buildSummaryPrompt(session);
|
||||
console.error('[claude-mem worker] Yielding finalize prompt to SDK agent', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId,
|
||||
promptLength: finalizePrompt.length,
|
||||
promptPreview: finalizePrompt.substring(0, 300)
|
||||
});
|
||||
yield {
|
||||
type: 'user',
|
||||
session_id: this.sdkSessionId || claudeSessionId,
|
||||
parent_tool_use_id: null,
|
||||
message: {
|
||||
role: 'user',
|
||||
content: finalizePrompt
|
||||
}
|
||||
};
|
||||
} else {
|
||||
console.error('[claude-mem worker] Failed to load session for finalize prompt', {
|
||||
sessionDbId: this.sessionDbId
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (message.type === 'observation') {
|
||||
// Build observation prompt
|
||||
const observationPrompt = buildObservationPrompt({
|
||||
id: 0, // Not needed for prompt generation
|
||||
tool_name: message.tool_name,
|
||||
tool_input: message.tool_input,
|
||||
tool_output: message.tool_output,
|
||||
created_at_epoch: Date.now()
|
||||
});
|
||||
console.error('[claude-mem worker] Yielding observation prompt to SDK agent', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
toolName: message.tool_name,
|
||||
promptLength: observationPrompt.length
|
||||
});
|
||||
yield {
|
||||
type: 'user',
|
||||
session_id: this.sdkSessionId || claudeSessionId,
|
||||
parent_tool_use_id: null,
|
||||
message: {
|
||||
role: 'user',
|
||||
content: observationPrompt
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle agent message and parse observations/summaries
|
||||
*/
|
||||
private handleAgentMessage(content: string): void {
|
||||
console.error('[claude-mem worker] Parsing agent message for observations and summary', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId,
|
||||
contentLength: content.length
|
||||
});
|
||||
|
||||
// Parse observations
|
||||
const observations = parseObservations(content);
|
||||
console.error('[claude-mem worker] Observations parsed from response', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId,
|
||||
observationCount: observations.length
|
||||
});
|
||||
|
||||
for (const obs of observations) {
|
||||
if (this.sdkSessionId) {
|
||||
console.error('[claude-mem worker] Storing observation in database', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId,
|
||||
project: this.project,
|
||||
observationType: obs.type,
|
||||
observationTextLength: obs.text?.length || 0
|
||||
});
|
||||
this.db.storeObservation(this.sdkSessionId, this.project, obs.type, obs.text);
|
||||
} else {
|
||||
console.error('[claude-mem worker] Cannot store observation - no SDK session ID', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
observationType: obs.type
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Parse summary (if present)
|
||||
console.error('[claude-mem worker] Attempting to parse summary from response', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId
|
||||
});
|
||||
|
||||
const summary = parseSummary(content);
|
||||
if (summary && this.sdkSessionId) {
|
||||
console.error('[claude-mem worker] Summary parsed successfully', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId,
|
||||
project: this.project,
|
||||
hasRequest: !!summary.request,
|
||||
hasInvestigated: !!summary.investigated,
|
||||
hasLearned: !!summary.learned,
|
||||
hasCompleted: !!summary.completed,
|
||||
filesReadCount: summary.files_read?.length || 0,
|
||||
filesEditedCount: summary.files_edited?.length || 0
|
||||
});
|
||||
|
||||
// Convert file arrays to JSON strings
|
||||
const summaryWithArrays = {
|
||||
request: summary.request,
|
||||
investigated: summary.investigated,
|
||||
learned: summary.learned,
|
||||
completed: summary.completed,
|
||||
next_steps: summary.next_steps,
|
||||
files_read: JSON.stringify(summary.files_read),
|
||||
files_edited: JSON.stringify(summary.files_edited),
|
||||
notes: summary.notes
|
||||
};
|
||||
|
||||
console.error('[claude-mem worker] Storing summary in database', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId,
|
||||
project: this.project
|
||||
});
|
||||
|
||||
this.db.storeSummary(this.sdkSessionId, this.project, summaryWithArrays);
|
||||
|
||||
console.error('[claude-mem worker] Summary stored successfully in database', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId,
|
||||
project: this.project
|
||||
});
|
||||
} else if (summary && !this.sdkSessionId) {
|
||||
console.error('[claude-mem worker] Summary parsed but cannot store - no SDK session ID', {
|
||||
sessionDbId: this.sessionDbId
|
||||
});
|
||||
} else {
|
||||
console.error('[claude-mem worker] No summary found in response', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
sdkSessionId: this.sdkSessionId
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup socket server and socket file
|
||||
*/
|
||||
private cleanup(): void {
|
||||
console.error('[claude-mem worker] Cleaning up worker resources', {
|
||||
sessionDbId: this.sessionDbId,
|
||||
socketPath: this.socketPath,
|
||||
hasServer: !!this.server,
|
||||
socketExists: existsSync(this.socketPath)
|
||||
});
|
||||
|
||||
if (this.server) {
|
||||
this.server.close();
|
||||
}
|
||||
if (existsSync(this.socketPath)) {
|
||||
unlinkSync(this.socketPath);
|
||||
}
|
||||
|
||||
console.error('[claude-mem worker] Cleanup complete', {
|
||||
sessionDbId: this.sessionDbId
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sleep helper
|
||||
*/
|
||||
private sleep(ms: number): Promise<void> {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
}
|
||||
|
||||
// Run if executed directly
|
||||
if (import.meta.main) {
|
||||
main().catch((error) => {
|
||||
console.error('[SDK Worker] Fatal error:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user