claude-mem 12.4.4 → 12.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-mem",
3
- "version": "12.4.4",
3
+ "version": "12.4.5",
4
4
  "description": "Memory compression system for Claude Code - persist context across sessions",
5
5
  "keywords": [
6
6
  "claude",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-mem",
3
- "version": "12.4.4",
3
+ "version": "12.4.5",
4
4
  "description": "Persistent memory system for Claude Code - seamlessly preserve context across sessions",
5
5
  "author": {
6
6
  "name": "Alex Newman"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-mem-plugin",
3
- "version": "12.4.4",
3
+ "version": "12.4.5",
4
4
  "private": true,
5
5
  "description": "Runtime dependencies for claude-mem bundled hooks",
6
6
  "type": "module",
@@ -6,7 +6,7 @@ ${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?u=`
6
6
  `,"utf8")}catch(g){process.stderr.write(`[LOGGER] Failed to write to log file: ${g instanceof Error?g.message:String(g)}
7
7
  `)}else process.stderr.write(p+`
8
8
  `)}debug(e,t,s,n){this.log(0,e,t,s,n)}info(e,t,s,n){this.log(1,e,t,s,n)}warn(e,t,s,n){this.log(2,e,t,s,n)}error(e,t,s,n){this.log(3,e,t,s,n)}dataIn(e,t,s,n){this.info(e,`\u2192 ${t}`,s,n)}dataOut(e,t,s,n){this.info(e,`\u2190 ${t}`,s,n)}success(e,t,s,n){this.info(e,`\u2713 ${t}`,s,n)}failure(e,t,s,n){this.error(e,`\u2717 ${t}`,s,n)}timing(e,t,s,n){this.info(e,`\u23F1 ${t}`,n,{duration:`${s}ms`})}happyPathError(e,t,s,n,o=""){let m=((new Error().stack||"").split(`
9
- `)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),u=m?`${m[1].split("/").pop()}:${m[2]}`:"unknown",E={...s,location:u};return this.warn(e,`[HAPPY-PATH] ${t}`,E,n),o}},c=new Z;var Ht={};function wt(){return typeof __dirname<"u"?__dirname:(0,S.dirname)((0,Ae.fileURLToPath)(Ht.url))}var Ft=wt();function $t(){if(process.env.CLAUDE_MEM_DATA_DIR)return process.env.CLAUDE_MEM_DATA_DIR;let r=(0,S.join)((0,ee.homedir)(),".claude-mem"),e=(0,S.join)(r,"settings.json");try{if((0,H.existsSync)(e)){let{readFileSync:t}=require("fs"),s=JSON.parse(t(e,"utf-8")),n=s.env??s;if(n.CLAUDE_MEM_DATA_DIR)return n.CLAUDE_MEM_DATA_DIR}}catch{}return r}var R=$t(),y=process.env.CLAUDE_CONFIG_DIR||(0,S.join)((0,ee.homedir)(),".claude"),us=(0,S.join)(y,"plugins","marketplaces","thedotmack"),ms=(0,S.join)(R,"archives"),cs=(0,S.join)(R,"logs"),ls=(0,S.join)(R,"trash"),ps=(0,S.join)(R,"backups"),Es=(0,S.join)(R,"modes"),gs=(0,S.join)(R,"settings.json"),Re=(0,S.join)(R,"claude-mem.db"),Ts=(0,S.join)(R,"vector-db"),Pt=(0,S.join)(R,"observer-sessions"),te=(0,S.basename)(Pt),fs=(0,S.join)(y,"settings.json"),Ss=(0,S.join)(y,"commands"),bs=(0,S.join)(y,"CLAUDE.md");function Ne(r){(0,H.mkdirSync)(r,{recursive:!0})}function Ce(){return(0,S.join)(Ft,"..")}var De=require("crypto");var Le=require("os"),ye=L(require("path"),1);var j=require("fs"),G=L(require("path"),1),U={isWorktree:!1,worktreeName:null,parentRepoPath:null,parentProjectName:null};function Ie(r){let e=G.default.join(r,".git"),t;try{t=(0,j.statSync)(e)}catch(u){return u instanceof Error&&u.code!=="ENOENT"&&console.warn("[worktree] Unexpected error checking .git:",u),U}if(!t.isFile())return U;let s;try{s=(0,j.readFileSync)(e,"utf-8").trim()}catch(u){return console.warn("[worktree] Failed to read .git file:",u instanceof Error?u.message:String(u)),U}let n=s.match(/^gitdir:\s*(.+)$/);if(!n)return U;let i=n[1].match(/^(.+)[/\\]\.git[/\\]worktrees[/\\]([^/\\]+)$/);if(!i)return U;let a=i[1],d=G.default.basename(r),m=G.default.basename(a);return{isWorktree:!0,worktreeName:d,parentRepoPath:a,parentProjectName:m}}function Me(r){return r==="~"||r.startsWith("~/")?r.replace(/^~/,(0,Le.homedir)()):r}function Gt(r){if(!r||r.trim()==="")return c.warn("PROJECT_NAME","Empty cwd provided, using fallback",{cwd:r}),"unknown-project";let e=Me(r),t=ye.default.basename(e);if(t===""){if(process.platform==="win32"){let n=r.match(/^([A-Z]):\\/i);if(n){let i=`drive-${n[1].toUpperCase()}`;return c.info("PROJECT_NAME","Drive root detected",{cwd:r,projectName:i}),i}}return c.warn("PROJECT_NAME","Root directory detected, using fallback",{cwd:r}),"unknown-project"}return t}function se(r){let e=Gt(r);if(!r)return{primary:e,parent:null,isWorktree:!1,allProjects:[e]};let t=Me(r),s=Ie(t);if(s.isWorktree&&s.parentProjectName){let n=`${s.parentProjectName}/${e}`;return{primary:n,parent:s.parentProjectName,isWorktree:!0,allProjects:[s.parentProjectName,n]}}return{primary:e,parent:null,isWorktree:!1,allProjects:[e]}}function X(r,e,t){return(0,De.createHash)("sha256").update([r||"",e||"",t||""].join("\0")).digest("hex").slice(0,16)}function re(r){if(!r)return[];try{let e=JSON.parse(r);return Array.isArray(e)?e:[String(e)]}catch{return[r]}}var h="claude";function jt(r){return r.trim().toLowerCase().replace(/\s+/g,"-")}function M(r){if(!r)return h;let e=jt(r);return e?e==="transcript"||e.includes("codex")?"codex":e.includes("cursor")?"cursor":e.includes("claude")?"claude":e:h}function ve(r){let e=["claude","codex","cursor"];return[...r].sort((t,s)=>{let n=e.indexOf(t),o=e.indexOf(s);return n!==-1||o!==-1?n===-1?1:o===-1?-1:n-o:t.localeCompare(s)})}function Xt(r,e){return{customTitle:r,platformSource:e?M(e):void 0}}var B=class{db;constructor(e=Re){e instanceof ne.Database?this.db=e:(e!==":memory:"&&Ne(R),this.db=new ne.Database(e),this.db.run("PRAGMA journal_mode = WAL"),this.db.run("PRAGMA synchronous = NORMAL"),this.db.run("PRAGMA foreign_keys = ON"),this.db.run("PRAGMA journal_size_limit = 4194304")),this.initializeSchema(),this.ensureWorkerPortColumn(),this.ensurePromptTrackingColumns(),this.removeSessionSummariesUniqueConstraint(),this.addObservationHierarchicalFields(),this.makeObservationsTextNullable(),this.createUserPromptsTable(),this.ensureDiscoveryTokensColumn(),this.createPendingMessagesTable(),this.renameSessionIdColumns(),this.repairSessionIdColumnRename(),this.addFailedAtEpochColumn(),this.addOnUpdateCascadeToForeignKeys(),this.addObservationContentHashColumn(),this.addSessionCustomTitleColumn(),this.addSessionPlatformSourceColumn(),this.addObservationModelColumns(),this.ensureMergedIntoProjectColumns(),this.addObservationSubagentColumns(),this.addObservationsUniqueContentHashIndex()}initializeSchema(){this.db.run(`
9
+ `)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),u=m?`${m[1].split("/").pop()}:${m[2]}`:"unknown",E={...s,location:u};return this.warn(e,`[HAPPY-PATH] ${t}`,E,n),o}},c=new Z;var Ht={};function wt(){return typeof __dirname<"u"?__dirname:(0,S.dirname)((0,Ae.fileURLToPath)(Ht.url))}var Ft=wt();function $t(){if(process.env.CLAUDE_MEM_DATA_DIR)return process.env.CLAUDE_MEM_DATA_DIR;let r=(0,S.join)((0,ee.homedir)(),".claude-mem"),e=(0,S.join)(r,"settings.json");try{if((0,H.existsSync)(e)){let{readFileSync:t}=require("fs"),s=JSON.parse(t(e,"utf-8")),n=s.env??s;if(n.CLAUDE_MEM_DATA_DIR)return n.CLAUDE_MEM_DATA_DIR}}catch{}return r}var R=$t(),y=process.env.CLAUDE_CONFIG_DIR||(0,S.join)((0,ee.homedir)(),".claude"),us=(0,S.join)(y,"plugins","marketplaces","thedotmack"),ms=(0,S.join)(R,"archives"),cs=(0,S.join)(R,"logs"),ls=(0,S.join)(R,"trash"),ps=(0,S.join)(R,"backups"),Es=(0,S.join)(R,"modes"),gs=(0,S.join)(R,"settings.json"),Re=(0,S.join)(R,"claude-mem.db"),Ts=(0,S.join)(R,"vector-db"),Pt=(0,S.join)(R,"observer-sessions"),te=(0,S.basename)(Pt),fs=(0,S.join)(y,"settings.json"),Ss=(0,S.join)(y,"commands"),bs=(0,S.join)(y,"CLAUDE.md");function Ne(r){(0,H.mkdirSync)(r,{recursive:!0})}function Ce(){return(0,S.join)(Ft,"..")}var De=require("crypto");var Le=require("os"),ye=L(require("path"),1);var j=require("fs"),G=L(require("path"),1),U={isWorktree:!1,worktreeName:null,parentRepoPath:null,parentProjectName:null};function Ie(r){let e=G.default.join(r,".git"),t;try{t=(0,j.statSync)(e)}catch(u){return u instanceof Error&&u.code!=="ENOENT"&&console.warn("[worktree] Unexpected error checking .git:",u),U}if(!t.isFile())return U;let s;try{s=(0,j.readFileSync)(e,"utf-8").trim()}catch(u){return console.warn("[worktree] Failed to read .git file:",u instanceof Error?u.message:String(u)),U}let n=s.match(/^gitdir:\s*(.+)$/);if(!n)return U;let i=n[1].match(/^(.+)[/\\]\.git[/\\]worktrees[/\\]([^/\\]+)$/);if(!i)return U;let a=i[1],d=G.default.basename(r),m=G.default.basename(a);return{isWorktree:!0,worktreeName:d,parentRepoPath:a,parentProjectName:m}}function Me(r){return r==="~"||r.startsWith("~/")?r.replace(/^~/,(0,Le.homedir)()):r}function Gt(r){if(!r||r.trim()==="")return c.warn("PROJECT_NAME","Empty cwd provided, using fallback",{cwd:r}),"unknown-project";let e=Me(r),t=ye.default.basename(e);if(t===""){if(process.platform==="win32"){let n=r.match(/^([A-Z]):\\/i);if(n){let i=`drive-${n[1].toUpperCase()}`;return c.info("PROJECT_NAME","Drive root detected",{cwd:r,projectName:i}),i}}return c.warn("PROJECT_NAME","Root directory detected, using fallback",{cwd:r}),"unknown-project"}return t}function se(r){let e=Gt(r);if(!r)return{primary:e,parent:null,isWorktree:!1,allProjects:[e]};let t=Me(r),s=Ie(t);if(s.isWorktree&&s.parentProjectName){let n=`${s.parentProjectName}/${e}`;return{primary:n,parent:s.parentProjectName,isWorktree:!0,allProjects:[s.parentProjectName,n]}}return{primary:e,parent:null,isWorktree:!1,allProjects:[e]}}function X(r,e,t){return(0,De.createHash)("sha256").update([r||"",e||"",t||""].join("\0")).digest("hex").slice(0,16)}function re(r){if(!r)return[];try{let e=JSON.parse(r);return Array.isArray(e)?e:[String(e)]}catch{return[r]}}var h="claude";function jt(r){return r.trim().toLowerCase().replace(/\s+/g,"-")}function M(r){if(!r)return h;let e=jt(r);return e?e==="transcript"||e.includes("codex")?"codex":e.includes("cursor")?"cursor":e.includes("claude")?"claude":e:h}function ve(r){let e=["claude","codex","cursor"];return[...r].sort((t,s)=>{let n=e.indexOf(t),o=e.indexOf(s);return n!==-1||o!==-1?n===-1?1:o===-1?-1:n-o:t.localeCompare(s)})}function Xt(r,e){return{customTitle:r,platformSource:e?M(e):void 0}}var B=class{db;constructor(e=Re){e instanceof ne.Database?this.db=e:(e!==":memory:"&&Ne(R),this.db=new ne.Database(e),this.db.run("PRAGMA journal_mode = WAL"),this.db.run("PRAGMA synchronous = NORMAL"),this.db.run("PRAGMA foreign_keys = ON"),this.db.run("PRAGMA journal_size_limit = 4194304")),this.initializeSchema(),this.ensureWorkerPortColumn(),this.ensurePromptTrackingColumns(),this.removeSessionSummariesUniqueConstraint(),this.addObservationHierarchicalFields(),this.makeObservationsTextNullable(),this.createUserPromptsTable(),this.ensureDiscoveryTokensColumn(),this.createPendingMessagesTable(),this.renameSessionIdColumns(),this.repairSessionIdColumnRename(),this.addFailedAtEpochColumn(),this.addOnUpdateCascadeToForeignKeys(),this.addObservationContentHashColumn(),this.addSessionCustomTitleColumn(),this.addSessionPlatformSourceColumn(),this.addObservationModelColumns(),this.ensureMergedIntoProjectColumns(),this.addObservationSubagentColumns(),this.addPendingMessagesToolUseIdAndWorkerPidColumns(),this.addObservationsUniqueContentHashIndex()}initializeSchema(){this.db.run(`
10
10
  CREATE TABLE IF NOT EXISTS schema_versions (
11
11
  id INTEGER PRIMARY KEY,
12
12
  version INTEGER UNIQUE NOT NULL,
@@ -290,7 +290,19 @@ ${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?u=`
290
290
  UPDATE sdk_sessions
291
291
  SET platform_source = '${h}'
292
292
  WHERE platform_source IS NULL OR platform_source = ''
293
- `),n||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()))}addObservationModelColumns(){let e=this.db.query("PRAGMA table_info(observations)").all(),t=e.some(n=>n.name==="generated_by_model"),s=e.some(n=>n.name==="relevance_count");t&&s||(t||this.db.run("ALTER TABLE observations ADD COLUMN generated_by_model TEXT"),s||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()))}ensureMergedIntoProjectColumns(){this.db.query("PRAGMA table_info(observations)").all().some(s=>s.name==="merged_into_project")||this.db.run("ALTER TABLE observations ADD COLUMN merged_into_project TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_merged_into ON observations(merged_into_project)"),this.db.query("PRAGMA table_info(session_summaries)").all().some(s=>s.name==="merged_into_project")||this.db.run("ALTER TABLE session_summaries ADD COLUMN merged_into_project TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_summaries_merged_into ON session_summaries(merged_into_project)")}addObservationSubagentColumns(){let e=this.db.prepare("SELECT version FROM schema_versions WHERE version = ?").get(27),t=this.db.query("PRAGMA table_info(observations)").all(),s=t.some(i=>i.name==="agent_type"),n=t.some(i=>i.name==="agent_id");s||this.db.run("ALTER TABLE observations ADD COLUMN agent_type TEXT"),n||this.db.run("ALTER TABLE observations ADD COLUMN agent_id TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_agent_type ON observations(agent_type)"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_agent_id ON observations(agent_id)");let o=this.db.query("PRAGMA table_info(pending_messages)").all();if(o.length>0){let i=o.some(d=>d.name==="agent_type"),a=o.some(d=>d.name==="agent_id");i||this.db.run("ALTER TABLE pending_messages ADD COLUMN agent_type TEXT"),a||this.db.run("ALTER TABLE pending_messages ADD COLUMN agent_id TEXT")}e||this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(27,new Date().toISOString())}addObservationsUniqueContentHashIndex(){if(this.db.prepare("SELECT version FROM schema_versions WHERE version = ?").get(29))return;let t=this.db.query("PRAGMA table_info(observations)").all(),s=t.some(o=>o.name==="memory_session_id"),n=t.some(o=>o.name==="content_hash");if(!s||!n){this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(29,new Date().toISOString());return}this.db.run("BEGIN TRANSACTION");try{this.db.run(`
293
+ `),n||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()))}addObservationModelColumns(){let e=this.db.query("PRAGMA table_info(observations)").all(),t=e.some(n=>n.name==="generated_by_model"),s=e.some(n=>n.name==="relevance_count");t&&s||(t||this.db.run("ALTER TABLE observations ADD COLUMN generated_by_model TEXT"),s||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()))}ensureMergedIntoProjectColumns(){this.db.query("PRAGMA table_info(observations)").all().some(s=>s.name==="merged_into_project")||this.db.run("ALTER TABLE observations ADD COLUMN merged_into_project TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_merged_into ON observations(merged_into_project)"),this.db.query("PRAGMA table_info(session_summaries)").all().some(s=>s.name==="merged_into_project")||this.db.run("ALTER TABLE session_summaries ADD COLUMN merged_into_project TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_summaries_merged_into ON session_summaries(merged_into_project)")}addObservationSubagentColumns(){let e=this.db.prepare("SELECT version FROM schema_versions WHERE version = ?").get(27),t=this.db.query("PRAGMA table_info(observations)").all(),s=t.some(i=>i.name==="agent_type"),n=t.some(i=>i.name==="agent_id");s||this.db.run("ALTER TABLE observations ADD COLUMN agent_type TEXT"),n||this.db.run("ALTER TABLE observations ADD COLUMN agent_id TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_agent_type ON observations(agent_type)"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_agent_id ON observations(agent_id)");let o=this.db.query("PRAGMA table_info(pending_messages)").all();if(o.length>0){let i=o.some(d=>d.name==="agent_type"),a=o.some(d=>d.name==="agent_id");i||this.db.run("ALTER TABLE pending_messages ADD COLUMN agent_type TEXT"),a||this.db.run("ALTER TABLE pending_messages ADD COLUMN agent_id TEXT")}e||this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(27,new Date().toISOString())}addPendingMessagesToolUseIdAndWorkerPidColumns(){if(this.db.query("SELECT name FROM sqlite_master WHERE type='table' AND name='pending_messages'").all().length===0){this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(28,new Date().toISOString());return}let t=this.db.query("PRAGMA table_info(pending_messages)").all(),s=t.some(o=>o.name==="tool_use_id"),n=t.some(o=>o.name==="worker_pid");s||this.db.run("ALTER TABLE pending_messages ADD COLUMN tool_use_id TEXT"),n||this.db.run("ALTER TABLE pending_messages ADD COLUMN worker_pid INTEGER"),this.db.run("BEGIN TRANSACTION");try{this.db.run("CREATE INDEX IF NOT EXISTS idx_pending_messages_worker_pid ON pending_messages(worker_pid)"),this.db.run(`
294
+ DELETE FROM pending_messages
295
+ WHERE tool_use_id IS NOT NULL
296
+ AND id NOT IN (
297
+ SELECT MIN(id) FROM pending_messages
298
+ WHERE tool_use_id IS NOT NULL
299
+ GROUP BY content_session_id, tool_use_id
300
+ )
301
+ `),this.db.run(`
302
+ CREATE UNIQUE INDEX IF NOT EXISTS ux_pending_session_tool
303
+ ON pending_messages(content_session_id, tool_use_id)
304
+ WHERE tool_use_id IS NOT NULL
305
+ `),this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(28,new Date().toISOString()),this.db.run("COMMIT")}catch(o){throw this.db.run("ROLLBACK"),o}}addObservationsUniqueContentHashIndex(){if(this.db.prepare("SELECT version FROM schema_versions WHERE version = ?").get(29))return;let t=this.db.query("PRAGMA table_info(observations)").all(),s=t.some(o=>o.name==="memory_session_id"),n=t.some(o=>o.name==="content_hash");if(!s||!n){this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(29,new Date().toISOString());return}this.db.run("BEGIN TRANSACTION");try{this.db.run(`
294
306
  DELETE FROM observations
295
307
  WHERE id NOT IN (
296
308
  SELECT MIN(id) FROM observations
@@ -187,7 +187,7 @@ ${f}`}let s=i.lineStart;for(let u=i.lineStart-1;u>=0;u--){let l=a[u].trim();if(l
187
187
  ${c}`}var $b=new Set([".js",".jsx",".ts",".tsx",".mjs",".cjs",".py",".pyw",".go",".rs",".rb",".java",".cs",".cpp",".cc",".cxx",".c",".h",".hpp",".hh",".swift",".kt",".kts",".php",".vue",".svelte",".ex",".exs",".lua",".scala",".sc",".sh",".bash",".zsh",".hs",".zig",".css",".scss",".toml",".yml",".yaml",".sql",".md",".mdx"]),uD=new Set(["node_modules",".git","dist","build",".next","__pycache__",".venv","venv","env",".env","target","vendor",".cache",".turbo","coverage",".nyc_output",".claude",".smart-file-read"]),lD=512*1024;async function*bb(t,e,r=20,n){if(r<=0)return;let o;try{o=await(0,Zn.readdir)(t,{withFileTypes:!0})}catch(i){_.debug("WORKER",`walkDir: failed to read directory ${t}`,void 0,i instanceof Error?i:void 0);return}for(let i of o){if(i.name.startsWith(".")&&i.name!=="."||uD.has(i.name))continue;let a=(0,Ai.join)(t,i.name);if(i.isDirectory())yield*bb(a,e,r-1,n);else if(i.isFile()){let s=i.name.slice(i.name.lastIndexOf("."));($b.has(s)||n&&n.has(s))&&(yield a)}}}async function dD(t){try{let e=await(0,Zn.stat)(t);if(e.size>lD||e.size===0)return null;let r=await(0,Zn.readFile)(t,"utf-8");return r.slice(0,1e3).includes("\0")?null:r}catch(e){return _.debug("WORKER",`safeReadFile: failed to read ${t}`,void 0,e instanceof Error?e:void 0),null}}async function Sb(t,e,r={}){let n=r.maxResults||20,o=e.toLowerCase(),i=o.split(/[\s_\-./]+/).filter(x=>x.length>0),a=r.projectRoot||t,s=Ni(a),c=new Set;for(let x of Object.values(s.grammars))for(let S of x.extensions)$b.has(S)||c.add(S);let u=[];for await(let x of bb(t,t,20,c.size>0?c:void 0)){if(r.filePattern&&!(0,Ai.relative)(t,x).toLowerCase().includes(r.filePattern.toLowerCase()))continue;let S=await dD(x);S&&u.push({absolutePath:x,relativePath:(0,Ai.relative)(t,x),content:S})}let l=vb(u,a),d=[],p=[],f=0;for(let[x,S]of l){f+=pD(S);let z=nc(x.toLowerCase(),i)>0,Ve=[],We=(qn,qr)=>{for(let ye of qn){let rr=0,mt="",Vn=nc(ye.name.toLowerCase(),i);Vn>0&&(rr+=Vn*3,mt="name match"),ye.signature.toLowerCase().includes(o)&&(rr+=2,mt=mt?`${mt} + signature`:"signature match"),ye.jsdoc&&ye.jsdoc.toLowerCase().includes(o)&&(rr+=1,mt=mt?`${mt} + jsdoc`:"jsdoc match"),rr>0&&(z=!0,Ve.push({filePath:x,symbolName:qr?`${qr}.${ye.name}`:ye.name,kind:ye.kind,signature:ye.signature,jsdoc:ye.jsdoc,lineStart:ye.lineStart,lineEnd:ye.lineEnd,matchReason:mt})),ye.children&&We(ye.children,ye.name)}};We(S.symbols),z&&(d.push(S),p.push(...Ve))}p.sort((x,S)=>{let j=nc(x.symbolName.toLowerCase(),i);return nc(S.symbolName.toLowerCase(),i)-j});let h=p.slice(0,n),g=new Set(h.map(x=>x.filePath)),$=d.filter(x=>g.has(x.filePath)).slice(0,n),k=$.reduce((x,S)=>x+S.foldedTokenEstimate,0);return{foldedFiles:$,matchingSymbols:h,totalFilesScanned:u.length,totalSymbolsFound:f,tokenEstimate:k}}function nc(t,e){let r=0;for(let n of e)if(t===n)r+=10;else if(t.includes(n))r+=5;else{let o=0,i=0;for(let a of n){let s=t.indexOf(a,o);s!==-1&&(i++,o=s+1)}i===n.length&&(r+=1)}return r}function pD(t){let e=t.symbols.length;for(let r of t.symbols)r.children&&(e+=r.children.length);return e}function xb(t,e){let r=[];if(r.push(`\u{1F50D} Smart Search: "${e}"`),r.push(` Scanned ${t.totalFilesScanned} files, found ${t.totalSymbolsFound} symbols`),r.push(` ${t.matchingSymbols.length} matches across ${t.foldedFiles.length} files (~${t.tokenEstimate} tokens for folded view)`),r.push(""),t.matchingSymbols.length===0)return r.push(" No matching symbols found."),r.join(`
188
188
  `);r.push("\u2500\u2500 Matching Symbols \u2500\u2500"),r.push("");for(let n of t.matchingSymbols){if(r.push(` ${n.kind} ${n.symbolName} (${n.filePath}:${n.lineStart+1})`),r.push(` ${n.signature}`),n.jsdoc){let o=n.jsdoc.split(`
189
189
  `).find(i=>i.replace(/^[\s*/]+/,"").trim().length>0);o&&r.push(` \u{1F4AC} ${o.replace(/^[\s*/]+/,"").trim()}`)}r.push("")}r.push("\u2500\u2500 Folded File Views \u2500\u2500"),r.push("");for(let n of t.foldedFiles)r.push(Ln(n)),r.push("");return r.push("\u2500\u2500 Actions \u2500\u2500"),r.push(" To see full implementation: use smart_unfold with file path and symbol name"),r.join(`
190
- `)}var ym=require("node:fs/promises"),Eb=require("node:fs"),Fr=require("node:path"),Ib=require("node:url"),xD={},fD="12.4.4";console.log=(...t)=>{_.error("CONSOLE","Intercepted console output (MCP protocol protection)",void 0,{args:t})};var Pb=!1,Tb=(()=>{if(typeof __dirname<"u")return __dirname;try{return(0,Fr.dirname)((0,Ib.fileURLToPath)(xD.url))}catch{return Pb=!0,process.cwd()}})(),$m=(0,Fr.resolve)(Tb,"worker-service.cjs");function mD(){Pb&&((0,Eb.existsSync)($m)||_.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 \u2014 the worker bundle is fine, but mcp-server cannot locate it. Worker auto-start will fail until the dirname-resolution path is fixed.",{workerScriptPath:$m,mcpServerDir:Tb}))}var kb={search:"/api/search",timeline:"/api/timeline"};async function _m(t,e){_.debug("SYSTEM","\u2192 Worker API",void 0,{endpoint:t,params:e});let r=new URLSearchParams;for(let[o,i]of Object.entries(e))i!=null&&r.append(o,String(i));let n=`${t}?${r}`;try{let o=await ec(n);if(!o.ok){let a=await o.text();throw new Error(`Worker API error (${o.status}): ${a}`)}let i=await o.json();return _.debug("SYSTEM","\u2190 Worker API success",void 0,{endpoint:t}),i}catch(o){return _.error("SYSTEM","\u2190 Worker API error",{endpoint:t},o instanceof Error?o:new Error(String(o))),{content:[{type:"text",text:`Error calling Worker API: ${o instanceof Error?o.message:String(o)}`}],isError:!0}}}async function hD(t,e){let r=await ec(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!r.ok){let o=await r.text();throw new Error(`Worker API error (${r.status}): ${o}`)}let n=await r.json();return _.debug("HTTP","Worker API success (POST)",void 0,{endpoint:t}),{content:[{type:"text",text:JSON.stringify(n,null,2)}]}}async function Fn(t,e){_.debug("HTTP","Worker API request (POST)",void 0,{endpoint:t});try{return await hD(t,e)}catch(r){return _.error("HTTP","Worker API error (POST)",{endpoint:t},r instanceof Error?r:new Error(String(r))),{content:[{type:"text",text:`Error calling Worker API: ${r instanceof Error?r.message:String(r)}`}],isError:!0}}}async function gD(){try{return(await ec("/api/health")).ok}catch(t){return _.debug("SYSTEM","Worker health check failed",{},t instanceof Error?t:new Error(String(t))),!1}}async function vD(){if(await gD())return!0;_.warn("SYSTEM","Worker not available, attempting auto-start for MCP client"),mD();try{let t=am(),e=await ib(t,$m);return e||_.error("SYSTEM","Worker auto-start returned false \u2014 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.)."),e}catch(t){return _.error("SYSTEM","Worker auto-start threw \u2014 MCP tools that require the worker (search, timeline, get_observations) will fail until the worker is running.",void 0,t instanceof Error?t:new Error(String(t))),!1}}var zb=[{name:"__IMPORTANT",description:`3-LAYER WORKFLOW (ALWAYS FOLLOW):
190
+ `)}var ym=require("node:fs/promises"),Eb=require("node:fs"),Fr=require("node:path"),Ib=require("node:url"),xD={},fD="12.4.5";console.log=(...t)=>{_.error("CONSOLE","Intercepted console output (MCP protocol protection)",void 0,{args:t})};var Pb=!1,Tb=(()=>{if(typeof __dirname<"u")return __dirname;try{return(0,Fr.dirname)((0,Ib.fileURLToPath)(xD.url))}catch{return Pb=!0,process.cwd()}})(),$m=(0,Fr.resolve)(Tb,"worker-service.cjs");function mD(){Pb&&((0,Eb.existsSync)($m)||_.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 \u2014 the worker bundle is fine, but mcp-server cannot locate it. Worker auto-start will fail until the dirname-resolution path is fixed.",{workerScriptPath:$m,mcpServerDir:Tb}))}var kb={search:"/api/search",timeline:"/api/timeline"};async function _m(t,e){_.debug("SYSTEM","\u2192 Worker API",void 0,{endpoint:t,params:e});let r=new URLSearchParams;for(let[o,i]of Object.entries(e))i!=null&&r.append(o,String(i));let n=`${t}?${r}`;try{let o=await ec(n);if(!o.ok){let a=await o.text();throw new Error(`Worker API error (${o.status}): ${a}`)}let i=await o.json();return _.debug("SYSTEM","\u2190 Worker API success",void 0,{endpoint:t}),i}catch(o){return _.error("SYSTEM","\u2190 Worker API error",{endpoint:t},o instanceof Error?o:new Error(String(o))),{content:[{type:"text",text:`Error calling Worker API: ${o instanceof Error?o.message:String(o)}`}],isError:!0}}}async function hD(t,e){let r=await ec(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});if(!r.ok){let o=await r.text();throw new Error(`Worker API error (${r.status}): ${o}`)}let n=await r.json();return _.debug("HTTP","Worker API success (POST)",void 0,{endpoint:t}),{content:[{type:"text",text:JSON.stringify(n,null,2)}]}}async function Fn(t,e){_.debug("HTTP","Worker API request (POST)",void 0,{endpoint:t});try{return await hD(t,e)}catch(r){return _.error("HTTP","Worker API error (POST)",{endpoint:t},r instanceof Error?r:new Error(String(r))),{content:[{type:"text",text:`Error calling Worker API: ${r instanceof Error?r.message:String(r)}`}],isError:!0}}}async function gD(){try{return(await ec("/api/health")).ok}catch(t){return _.debug("SYSTEM","Worker health check failed",{},t instanceof Error?t:new Error(String(t))),!1}}async function vD(){if(await gD())return!0;_.warn("SYSTEM","Worker not available, attempting auto-start for MCP client"),mD();try{let t=am(),e=await ib(t,$m);return e||_.error("SYSTEM","Worker auto-start returned false \u2014 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.)."),e}catch(t){return _.error("SYSTEM","Worker auto-start threw \u2014 MCP tools that require the worker (search, timeline, get_observations) will fail until the worker is running.",void 0,t instanceof Error?t:new Error(String(t))),!1}}var zb=[{name:"__IMPORTANT",description:`3-LAYER WORKFLOW (ALWAYS FOLLOW):
191
191
  1. search(query) \u2192 Get index with IDs (~50-100 tokens/result)
192
192
  2. timeline(anchor=ID) \u2192 Get context around interesting results
193
193
  3. get_observations([IDs]) \u2192 Fetch full details ONLY for filtered IDs
@@ -17,7 +17,7 @@ ${s.stack}`:` ${s.message}`:this.getLevel()===0&&typeof s=="object"?l=`
17
17
  `)}else process.stderr.write(p+`
18
18
  `)}debug(e,r,i,n){this.log(0,e,r,i,n)}info(e,r,i,n){this.log(1,e,r,i,n)}warn(e,r,i,n){this.log(2,e,r,i,n)}error(e,r,i,n){this.log(3,e,r,i,n)}dataIn(e,r,i,n){this.info(e,`\u2192 ${r}`,i,n)}dataOut(e,r,i,n){this.info(e,`\u2190 ${r}`,i,n)}success(e,r,i,n){this.info(e,`\u2713 ${r}`,i,n)}failure(e,r,i,n){this.error(e,`\u2717 ${r}`,i,n)}timing(e,r,i,n){this.info(e,`\u23F1 ${r}`,n,{duration:`${i}ms`})}happyPathError(e,r,i,n,s=""){let u=((new Error().stack||"").split(`
19
19
  `)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),l=u?`${u[1].split("/").pop()}:${u[2]}`:"unknown",d={...i,location:l};return this.warn(e,`[HAPPY-PATH] ${r}`,d,n),s}},g=new NR});function IF(t){return process.platform==="win32"?Math.round(t*fn.WINDOWS_MULTIPLIER):t}var fn,dt,Xn=ye(()=>{"use strict";fn={DEFAULT:3e5,HEALTH_CHECK:3e3,POST_SPAWN_WAIT:15e3,READINESS_WAIT:3e4,PORT_IN_USE_WAIT:3e3,WORKER_STARTUP_WAIT:1e3,PRE_RESTART_SETTLE_DELAY:2e3,POWERSHELL_COMMAND:1e4,WINDOWS_MULTIPLIER:1.5},dt={SUCCESS:0,FAILURE:1,BLOCKING_ERROR:2,USER_MESSAGE_ONLY:3}});var OF={};Sr(OF,{SettingsDefaultsManager:()=>ke});var Js,jf,MR,ke,sr=ye(()=>{"use strict";Js=require("fs"),jf=require("path"),MR=require("os"),ke=class{static DEFAULTS={CLAUDE_MEM_MODEL:"claude-sonnet-4-6",CLAUDE_MEM_CONTEXT_OBSERVATIONS:"50",CLAUDE_MEM_WORKER_PORT:String(37700+(process.getuid?.()??77)%100),CLAUDE_MEM_WORKER_HOST:"127.0.0.1",CLAUDE_MEM_SKIP_TOOLS:"ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion",CLAUDE_MEM_PROVIDER:"claude",CLAUDE_MEM_CLAUDE_AUTH_METHOD:"cli",CLAUDE_MEM_GEMINI_API_KEY:"",CLAUDE_MEM_GEMINI_MODEL:"gemini-2.5-flash-lite",CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED:"true",CLAUDE_MEM_GEMINI_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_GEMINI_MAX_TOKENS:"100000",CLAUDE_MEM_OPENROUTER_API_KEY:"",CLAUDE_MEM_OPENROUTER_MODEL:"xiaomi/mimo-v2-flash:free",CLAUDE_MEM_OPENROUTER_SITE_URL:"",CLAUDE_MEM_OPENROUTER_APP_NAME:"claude-mem",CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_OPENROUTER_MAX_TOKENS:"100000",CLAUDE_MEM_DATA_DIR:(0,jf.join)((0,MR.homedir)(),".claude-mem"),CLAUDE_MEM_LOG_LEVEL:"INFO",CLAUDE_MEM_PYTHON_VERSION:"3.13",CLAUDE_CODE_PATH:"",CLAUDE_MEM_MODE:"code",CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS:"false",CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS:"false",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT:"false",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_PERCENT:"true",CLAUDE_MEM_CONTEXT_FULL_COUNT:"0",CLAUDE_MEM_CONTEXT_FULL_FIELD:"narrative",CLAUDE_MEM_CONTEXT_SESSION_COUNT:"10",CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY:"true",CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE:"false",CLAUDE_MEM_CONTEXT_SHOW_TERMINAL_OUTPUT:"true",CLAUDE_MEM_FOLDER_CLAUDEMD_ENABLED:"false",CLAUDE_MEM_FOLDER_USE_LOCAL_MD:"false",CLAUDE_MEM_TRANSCRIPTS_ENABLED:"true",CLAUDE_MEM_TRANSCRIPTS_CONFIG_PATH:(0,jf.join)((0,MR.homedir)(),".claude-mem","transcript-watch.json"),CLAUDE_MEM_MAX_CONCURRENT_AGENTS:"2",CLAUDE_MEM_HOOK_FAIL_LOUD_THRESHOLD:"3",CLAUDE_MEM_EXCLUDED_PROJECTS:"",CLAUDE_MEM_FOLDER_MD_EXCLUDE:"[]",CLAUDE_MEM_SEMANTIC_INJECT:"false",CLAUDE_MEM_SEMANTIC_INJECT_LIMIT:"5",CLAUDE_MEM_TIER_ROUTING_ENABLED:"true",CLAUDE_MEM_TIER_SIMPLE_MODEL:"haiku",CLAUDE_MEM_TIER_SUMMARY_MODEL:"",CLAUDE_MEM_CHROMA_ENABLED:"true",CLAUDE_MEM_CHROMA_MODE:"local",CLAUDE_MEM_CHROMA_HOST:"127.0.0.1",CLAUDE_MEM_CHROMA_PORT:"8000",CLAUDE_MEM_CHROMA_SSL:"false",CLAUDE_MEM_CHROMA_API_KEY:"",CLAUDE_MEM_CHROMA_TENANT:"default_tenant",CLAUDE_MEM_CHROMA_DATABASE:"default_database",CLAUDE_MEM_TELEGRAM_ENABLED:"true",CLAUDE_MEM_TELEGRAM_BOT_TOKEN:"",CLAUDE_MEM_TELEGRAM_CHAT_ID:"",CLAUDE_MEM_TELEGRAM_TRIGGER_TYPES:"security_alert",CLAUDE_MEM_TELEGRAM_TRIGGER_CONCEPTS:""};static getAllDefaults(){return{...this.DEFAULTS}}static get(e){return process.env[e]??this.DEFAULTS[e]}static getInt(e){let r=this.get(e);return parseInt(r,10)}static getBool(e){let r=this.get(e);return r==="true"||r===!0}static applyEnvOverrides(e){let r={...e};for(let i of Object.keys(this.DEFAULTS))process.env[i]!==void 0&&(r[i]=process.env[i]);return r}static loadFromFile(e){try{if(!(0,Js.existsSync)(e)){let o=this.getAllDefaults();try{let a=(0,jf.dirname)(e);(0,Js.existsSync)(a)||(0,Js.mkdirSync)(a,{recursive:!0}),(0,Js.writeFileSync)(e,JSON.stringify(o,null,2),"utf-8"),console.log("[SETTINGS] Created settings file with defaults:",e)}catch(a){console.warn("[SETTINGS] Failed to create settings file, using in-memory defaults:",e,a instanceof Error?a.message:String(a))}return this.applyEnvOverrides(o)}let r=(0,Js.readFileSync)(e,"utf-8"),i=JSON.parse(r),n=i;if(i.env&&typeof i.env=="object"){n=i.env;try{(0,Js.writeFileSync)(e,JSON.stringify(n,null,2),"utf-8"),console.log("[SETTINGS] Migrated settings file from nested to flat schema:",e)}catch(o){console.warn("[SETTINGS] Failed to auto-migrate settings file:",e,o instanceof Error?o.message:String(o))}}let s={...this.DEFAULTS};for(let o of Object.keys(this.DEFAULTS))n[o]!==void 0&&(s[o]=n[o]);return this.applyEnvOverrides(s)}catch(r){return console.warn("[SETTINGS] Failed to load settings, using defaults:",e,r instanceof Error?r.message:String(r)),this.applyEnvOverrides(this.getAllDefaults())}}}});var $F={};Sr($F,{ARCHIVES_DIR:()=>DR,BACKUPS_DIR:()=>NF,CLAUDE_COMMANDS_DIR:()=>MF,CLAUDE_CONFIG_DIR:()=>No,CLAUDE_MD_PATH:()=>Wpe,CLAUDE_SETTINGS_PATH:()=>qpe,DATA_DIR:()=>Kt,DB_PATH:()=>Wc,LOGS_DIR:()=>PF,MARKETPLACE_ROOT:()=>Mo,MODES_DIR:()=>jR,OBSERVER_SESSIONS_DIR:()=>zi,OBSERVER_SESSIONS_PROJECT:()=>Ui,TRASH_DIR:()=>AF,USER_SETTINGS_PATH:()=>kt,VECTOR_DB_DIR:()=>Fpe,createBackupFilename:()=>Jpe,ensureAllClaudeDirs:()=>Vpe,ensureAllDataDirs:()=>Zpe,ensureDir:()=>xr,ensureModesDir:()=>Gpe,getCurrentProjectName:()=>Kpe,getPackageCommandsDir:()=>Ype,getPackageRoot:()=>Qn,getProjectArchiveDir:()=>Hpe,getWorkerSocketPath:()=>Bpe});function zpe(){return typeof __dirname<"u"?__dirname:(0,wt.dirname)((0,CF.fileURLToPath)(Xpe.url))}function Lpe(){if(process.env.CLAUDE_MEM_DATA_DIR)return process.env.CLAUDE_MEM_DATA_DIR;let t=(0,wt.join)((0,$R.homedir)(),".claude-mem"),e=(0,wt.join)(t,"settings.json");try{if((0,pS.existsSync)(e)){let{readFileSync:r}=require("fs"),i=JSON.parse(r(e,"utf-8")),n=i.env??i;if(n.CLAUDE_MEM_DATA_DIR)return n.CLAUDE_MEM_DATA_DIR}}catch{}return t}function Hpe(t){return(0,wt.join)(DR,t)}function Bpe(t){return(0,wt.join)(Kt,`worker-${t}.sock`)}function xr(t){(0,pS.mkdirSync)(t,{recursive:!0})}function Zpe(){xr(Kt),xr(DR),xr(PF),xr(AF),xr(NF),xr(jR)}function Gpe(){xr(jR)}function Vpe(){xr(No),xr(MF)}function Kpe(){try{let t=(0,RF.execSync)("git rev-parse --show-toplevel",{cwd:process.cwd(),encoding:"utf8",stdio:["pipe","pipe","ignore"],windowsHide:!0}).trim();return(0,wt.basename)((0,wt.dirname)(t))+"/"+(0,wt.basename)(t)}catch(t){g.debug("SYSTEM","Git root detection failed, using cwd basename",{cwd:process.cwd()},t instanceof Error?t:new Error(String(t)));let e=process.cwd();return(0,wt.basename)((0,wt.dirname)(e))+"/"+(0,wt.basename)(e)}}function Qn(){return(0,wt.join)(Upe,"..")}function Ype(){let t=Qn();return(0,wt.join)(t,"commands")}function Jpe(t){let e=new Date().toISOString().replace(/[:.]/g,"-").replace("T","_").slice(0,19);return`${t}.backup.${e}`}var wt,$R,pS,RF,CF,Xpe,Upe,Kt,No,Mo,DR,PF,AF,NF,jR,kt,Wc,Fpe,zi,Ui,qpe,MF,Wpe,Tt=ye(()=>{"use strict";wt=require("path"),$R=require("os"),pS=require("fs"),RF=require("child_process"),CF=require("url");oe();Xpe={};Upe=zpe();Kt=Lpe(),No=process.env.CLAUDE_CONFIG_DIR||(0,wt.join)((0,$R.homedir)(),".claude"),Mo=(0,wt.join)(No,"plugins","marketplaces","thedotmack"),DR=(0,wt.join)(Kt,"archives"),PF=(0,wt.join)(Kt,"logs"),AF=(0,wt.join)(Kt,"trash"),NF=(0,wt.join)(Kt,"backups"),jR=(0,wt.join)(Kt,"modes"),kt=(0,wt.join)(Kt,"settings.json"),Wc=(0,wt.join)(Kt,"claude-mem.db"),Fpe=(0,wt.join)(Kt,"vector-db"),zi=(0,wt.join)(Kt,"observer-sessions"),Ui=(0,wt.basename)(zi),qpe=(0,wt.join)(No,"settings.json"),MF=(0,wt.join)(No,"commands"),Wpe=(0,wt.join)(No,"CLAUDE.md")});function Ma(){return mS!==null||(mS=ke.loadFromFile(kt)),mS}var mS,zf=ye(()=>{"use strict";sr();Tt();mS=null});function pi(t=process.env){let e={};for(let[r,i]of Object.entries(t))if(i!==void 0){if(Qpe.has(r)){e[r]=i;continue}UR.has(r)||zR.some(n=>r.startsWith(n))||(e[r]=i)}return e}var zR,UR,Qpe,$a=ye(()=>{"use strict";zR=["CLAUDECODE_","CLAUDE_CODE_"],UR=new Set(["CLAUDECODE","CLAUDE_CODE_SESSION","CLAUDE_CODE_ENTRYPOINT","MCP_SESSION_ID"]),Qpe=new Set(["CLAUDE_CODE_OAUTH_TOKEN","CLAUDE_CODE_GIT_BASH_PATH"])});function mi(t){if(!Number.isInteger(t)||t<0||t===0)return!1;try{return process.kill(t,0),!0}catch(e){if(e instanceof Error){let r=e.code;return r==="EPERM"?!0:(g.debug("SYSTEM","PID check failed",{pid:t,code:r}),!1)}return g.warn("SYSTEM","PID check threw non-Error",{pid:t,error:String(e)}),!1}}function qR(t){if(!Number.isInteger(t)||t<=0)return null;if(process.platform==="linux")try{let e=(0,Xs.readFileSync)(`/proc/${t}/stat`,"utf-8"),r=e.lastIndexOf(") ");if(r<0)return null;let n=e.slice(r+2).split(" ")[19];return n&&/^\d+$/.test(n)?n:null}catch(e){return g.debug("SYSTEM","captureProcessStartToken: /proc read failed",{pid:t,error:e instanceof Error?e.message:String(e)}),null}if(process.platform==="win32")return null;try{let e=(0,Lf.spawnSync)("ps",["-p",String(t),"-o","lstart="],{encoding:"utf-8",timeout:2e3,env:{...process.env,LC_ALL:"C",LANG:"C"}});if(e.status!==0)return null;let r=e.stdout.trim();return r.length>0?r:null}catch(e){return g.debug("SYSTEM","captureProcessStartToken: ps exec failed",{pid:t,error:e instanceof Error?e.message:String(e)}),null}}function qf(t){if(!t||!mi(t.pid))return!1;if(!t.startToken)return!0;let e=qR(t.pid);if(e===null)return!0;let r=e===t.startToken;return r||g.debug("SYSTEM","verifyPidFileOwnership: start-token mismatch (PID reused)",{pid:t.pid,stored:t.startToken,current:e}),r}function Da(){return LR||(LR=new FR),LR}function ja(t){let e=Da(),r=e.getBySession(t).filter(s=>s.type==="sdk");r.length>1&&g.warn("PROCESS",`Multiple SDK processes found for session ${t}`,{count:r.length,pids:r.map(s=>s.pid)});let i=r[0];if(!i)return;let n=e.getRuntimeProcess(i.id);if(n)return{pid:i.pid,pgid:i.pgid,sessionDbId:t,process:n}}async function za(t,e=5e3){let{pid:r,pgid:i,process:n}=t;if(n.exitCode!==null)return;let s=new Promise(u=>{n.once("exit",()=>u())}),o=new Promise(u=>{setTimeout(u,e)});if(await Promise.race([s,o]),n.exitCode!==null)return;g.warn("PROCESS",`PID ${r} did not exit after ${e}ms, sending SIGKILL to process group`,{pid:r,pgid:i,timeoutMs:e});try{typeof i=="number"&&process.platform!=="win32"?process.kill(-i,"SIGKILL"):n.kill("SIGKILL")}catch{}let a=new Promise(u=>{n.once("exit",()=>u())}),c=new Promise(u=>{setTimeout(u,1e3)});await Promise.race([a,c])}function jF(){return Da().getAll().filter(t=>t.type==="sdk").length}function ime(){let t=Uf.shift();t&&t()}async function UF(t,e=6e4){let r=jF();if(r>=DF)throw new Error(`Hard cap exceeded: ${r} processes in registry (cap=${DF}). Refusing to spawn more.`);if(!(r<t))return g.info("PROCESS",`Pool limit reached (${r}/${t}), waiting for slot...`),new Promise((i,n)=>{let s=setTimeout(()=>{let a=Uf.indexOf(o);a>=0&&Uf.splice(a,1),n(new Error(`Timed out waiting for agent pool slot after ${e}ms`))},e),o=()=>{clearTimeout(s),jF()<t?i():Uf.push(o)};Uf.push(o)})}function sme(t,e){let r=Da(),i=process.platform==="win32"&&e.command.endsWith(".cmd"),n=pi(e.env??process.env),s=[];for(let d of e.args){if(d===""){s.length>0&&s[s.length-1].startsWith("--")&&s.pop();continue}s.push(d)}let o=i?(0,Lf.spawn)("cmd.exe",["/d","/c",e.command,...s],{cwd:e.cwd,env:n,detached:!0,stdio:["pipe","pipe","pipe"],signal:e.signal,windowsHide:!0}):(0,Lf.spawn)(e.command,s,{cwd:e.cwd,env:n,detached:!0,stdio:["pipe","pipe","pipe"],signal:e.signal,windowsHide:!0});if(o.on("error",d=>{g.warn("SDK_SPAWN",`[session-${t}] child emitted error event`,{sessionDbId:t,pid:o.pid,errorName:d.name,errorCode:d.code},d)}),!o.pid)return g.error("PROCESS","Spawn succeeded but produced no PID",{sessionDbId:t}),null;let a=o.pid,c=a;o.stderr&&o.stderr.on("data",d=>{g.debug("SDK_SPAWN",`[session-${t}] stderr: ${d.toString().trim()}`)});let u=`sdk:${t}:${a}`;if(r.register(u,{pid:a,type:"sdk",sessionId:t,startedAt:new Date().toISOString(),pgid:c},o),o.on("exit",(d,p)=>{d!==0&&g.warn("SDK_SPAWN",`[session-${t}] Claude process exited`,{code:d,signal:p,pid:a}),r.unregister(u),ime()}),!o.stdin||!o.stdout||!o.stderr){g.error("PROCESS","Spawned SDK child missing required stdio streams",{sessionDbId:t,pid:a,hasStdin:!!o.stdin,hasStdout:!!o.stdout,hasStderr:!!o.stderr});try{o.kill("SIGKILL")}catch{}return null}return{process:{stdin:o.stdin,stdout:o.stdout,stderr:o.stderr,get killed(){return o.killed},get exitCode(){return o.exitCode},kill:o.kill.bind(o),on:o.on.bind(o),once:o.once.bind(o),off:o.off.bind(o)},pid:a,pgid:c}}function LF(t){return e=>{let i=Da().getBySession(t).filter(s=>s.type==="sdk");for(let s of i)if(mi(s.pid))try{typeof s.pgid=="number"&&process.platform!=="win32"?process.kill(-s.pgid,"SIGTERM"):process.kill(s.pid,"SIGTERM"),g.warn("PROCESS",`Killing duplicate SDK process PID ${s.pid} before spawning new one for session ${t}`,{existingPid:s.pid,sessionDbId:t})}catch(o){(o instanceof Error?o.code:void 0)!=="ESRCH"&&(o instanceof Error?g.warn("PROCESS",`Failed to SIGTERM duplicate SDK process PID ${s.pid}`,{sessionDbId:t},o):g.warn("PROCESS",`Failed to SIGTERM duplicate SDK process PID ${s.pid} (non-Error)`,{sessionDbId:t,error:String(o)}))}let n=sme(t,e);if(!n)throw new Error(`Failed to spawn SDK subprocess for session ${t}`);return n.process}}var Lf,Xs,zF,Ff,eme,tme,rme,nme,FR,LR,DF,Uf,Qs=ye(()=>{"use strict";Lf=require("child_process"),Xs=require("fs"),zF=require("os"),Ff=Oe(require("path"),1);oe();$a();eme=5e3,tme=1e3,rme=Ff.default.join((0,zF.homedir)(),".claude-mem"),nme=Ff.default.join(rme,"supervisor.json");FR=class{registryPath;entries=new Map;runtimeProcesses=new Map;initialized=!1;constructor(e=nme){this.registryPath=e}initialize(){if(this.initialized)return;if(this.initialized=!0,(0,Xs.mkdirSync)(Ff.default.dirname(this.registryPath),{recursive:!0}),!(0,Xs.existsSync)(this.registryPath)){this.persist();return}try{let i=JSON.parse((0,Xs.readFileSync)(this.registryPath,"utf-8")).processes??{};for(let[n,s]of Object.entries(i))this.entries.set(n,s)}catch(r){r instanceof Error?g.warn("SYSTEM","Failed to parse supervisor registry, rebuilding",{path:this.registryPath},r):g.warn("SYSTEM","Failed to parse supervisor registry, rebuilding",{path:this.registryPath,error:String(r)}),this.entries.clear()}let e=this.pruneDeadEntries();e>0&&g.info("SYSTEM","Removed dead processes from supervisor registry",{removed:e}),this.persist()}register(e,r,i){this.initialize(),this.entries.set(e,r),i&&this.runtimeProcesses.set(e,i),this.persist()}unregister(e){this.initialize(),this.entries.delete(e),this.runtimeProcesses.delete(e),this.persist()}clear(){this.entries.clear(),this.runtimeProcesses.clear(),this.persist()}getAll(){return this.initialize(),Array.from(this.entries.entries()).map(([e,r])=>({id:e,...r})).sort((e,r)=>{let i=Date.parse(e.startedAt),n=Date.parse(r.startedAt);return(Number.isNaN(i)?0:i)-(Number.isNaN(n)?0:n)})}getBySession(e){let r=String(e);return this.getAll().filter(i=>i.sessionId!==void 0&&String(i.sessionId)===r)}getRuntimeProcess(e){return this.runtimeProcesses.get(e)}getByPid(e){return this.getAll().filter(r=>r.pid===e)}pruneDeadEntries(){this.initialize();let e=0;for(let[r,i]of this.entries)mi(i.pid)||(this.entries.delete(r),this.runtimeProcesses.delete(r),e+=1);return e>0&&this.persist(),e}async reapSession(e){this.initialize();let r=this.getBySession(e);if(r.length===0)return 0;let i=typeof e=="number"?e:Number(e)||void 0;g.info("SYSTEM",`Reaping ${r.length} process(es) for session ${e}`,{sessionId:i,pids:r.map(a=>a.pid)});let n=r.filter(a=>mi(a.pid));for(let a of n)try{typeof a.pgid=="number"&&process.platform!=="win32"?process.kill(-a.pgid,"SIGTERM"):process.kill(a.pid,"SIGTERM")}catch(c){c instanceof Error?c.code!=="ESRCH"&&g.debug("SYSTEM",`Failed to SIGTERM session process PID ${a.pid}`,{pid:a.pid,pgid:a.pgid},c):g.warn("SYSTEM",`Failed to SIGTERM session process PID ${a.pid} (non-Error)`,{pid:a.pid,pgid:a.pgid,error:String(c)})}let s=Date.now()+eme;for(;Date.now()<s&&n.filter(c=>mi(c.pid)).length!==0;)await new Promise(c=>setTimeout(c,100));let o=n.filter(a=>mi(a.pid));for(let a of o){g.warn("SYSTEM",`Session process PID ${a.pid} did not exit after SIGTERM, sending SIGKILL`,{pid:a.pid,pgid:a.pgid,sessionId:i});try{typeof a.pgid=="number"&&process.platform!=="win32"?process.kill(-a.pgid,"SIGKILL"):process.kill(a.pid,"SIGKILL")}catch(c){c instanceof Error?c.code!=="ESRCH"&&g.debug("SYSTEM",`Failed to SIGKILL session process PID ${a.pid}`,{pid:a.pid,pgid:a.pgid},c):g.warn("SYSTEM",`Failed to SIGKILL session process PID ${a.pid} (non-Error)`,{pid:a.pid,pgid:a.pgid,error:String(c)})}}if(o.length>0){let a=Date.now()+tme;for(;Date.now()<a&&o.filter(u=>mi(u.pid)).length!==0;)await new Promise(u=>setTimeout(u,100))}for(let a of r)this.entries.delete(a.id),this.runtimeProcesses.delete(a.id);return this.persist(),g.info("SYSTEM",`Reaped ${r.length} process(es) for session ${e}`,{sessionId:i,reaped:r.length}),r.length}persist(){let e={processes:Object.fromEntries(this.entries.entries())};(0,Xs.mkdirSync)(Ff.default.dirname(this.registryPath),{recursive:!0}),(0,Xs.writeFileSync)(this.registryPath,JSON.stringify(e,null,2))}},LR=null;DF=10,Uf=[]});async function GF(t){let e=t.currentPid??process.pid,r=t.pidFilePath??cme,i=t.registry.getAll(),n=[...i].filter(o=>o.pid!==e).sort((o,a)=>Date.parse(a.startedAt)-Date.parse(o.startedAt));for(let o of n){if(!mi(o.pid)){t.registry.unregister(o.id);continue}try{await qF(o,"SIGTERM")}catch(a){a instanceof Error?g.debug("SYSTEM","Failed to send SIGTERM to child process",{pid:o.pid,pgid:o.pgid,type:o.type},a):g.warn("SYSTEM","Failed to send SIGTERM to child process (non-Error)",{pid:o.pid,pgid:o.pgid,type:o.type,error:String(a)})}}await FF(n,5e3);let s=n.filter(o=>mi(o.pid));for(let o of s)try{await qF(o,"SIGKILL")}catch(a){a instanceof Error?g.debug("SYSTEM","Failed to force kill child process",{pid:o.pid,pgid:o.pgid,type:o.type},a):g.warn("SYSTEM","Failed to force kill child process (non-Error)",{pid:o.pid,pgid:o.pgid,type:o.type,error:String(a)})}await FF(s,1e3);for(let o of n)t.registry.unregister(o.id);for(let o of i.filter(a=>a.pid===e))t.registry.unregister(o.id);try{(0,HF.rmSync)(r,{force:!0})}catch(o){o instanceof Error?g.debug("SYSTEM","Failed to remove PID file during shutdown",{pidFilePath:r},o):g.warn("SYSTEM","Failed to remove PID file during shutdown (non-Error)",{pidFilePath:r,error:String(o)})}t.registry.pruneDeadEntries()}async function FF(t,e){let r=Date.now()+e;for(;Date.now()<r;){if(t.filter(n=>mi(n.pid)).length===0)return;await new Promise(n=>setTimeout(n,100))}}async function qF(t,e){let{pid:r,pgid:i}=t;if(process.platform!=="win32"){try{typeof i=="number"?process.kill(-i,e):process.kill(r,e)}catch(o){if(o instanceof Error&&o.code==="ESRCH")return;throw o}return}if(e==="SIGTERM"){try{process.kill(r,e)}catch(o){if(o instanceof Error&&o.code==="ESRCH")return;throw o}return}let n=await ume();if(n){await new Promise((o,a)=>{n(r,e,c=>{if(!c){o();return}if(c.code==="ESRCH"){o();return}a(c)})});return}let s=["/PID",String(r),"/T"];e==="SIGKILL"&&s.push("/F"),await ome("taskkill",s,{timeout:fn.POWERSHELL_COMMAND,windowsHide:!0})}async function ume(){let t="tree-kill";try{let e=await import(t);return e.default??e}catch(e){return g.debug("SYSTEM","tree-kill module not available, using fallback",{},e instanceof Error?e:void 0),null}}var WF,HF,BF,WR,ZF,ome,ame,cme,VF=ye(()=>{"use strict";WF=require("child_process"),HF=require("fs"),BF=require("os"),WR=Oe(require("path"),1),ZF=require("util");oe();Xn();Qs();ome=(0,ZF.promisify)(WF.execFile),ame=WR.default.join((0,BF.homedir)(),".claude-mem"),cme=WR.default.join(ame,"worker.pid")});function lme(){let e=Da().pruneDeadEntries();e>0&&g.info("SYSTEM",`Health check: pruned ${e} dead process(es) from registry`)}function YF(){Ql===null&&(Ql=setInterval(lme,KF),Ql.unref(),g.debug("SYSTEM","Health checker started",{intervalMs:KF}))}function JF(){Ql!==null&&(clearInterval(Ql),Ql=null,g.debug("SYSTEM","Health checker stopped"))}var KF,Ql,XF=ye(()=>{"use strict";oe();Qs();KF=3e4,Ql=null});async function eq(){await ZR.start()}function Xr(){return ZR}function tq(t){ZR.configureSignalHandlers(t)}function Wf(t={}){let e=t.pidFilePath??pme;if(!(0,Hc.existsSync)(e))return"missing";let r=null;try{r=JSON.parse((0,Hc.readFileSync)(e,"utf-8"))}catch(n){return n instanceof Error?g.warn("SYSTEM","Failed to parse worker PID file, removing it",{path:e},n):g.warn("SYSTEM","Failed to parse worker PID file, removing it",{path:e,error:String(n)}),(0,Hc.rmSync)(e,{force:!0}),"invalid"}return qf(r)&&r?((t.logAlive??!0)&&g.info("SYSTEM","Worker already running (PID alive)",{existingPid:r.pid,existingPort:r.port,startedAt:r.startedAt}),"alive"):(g.info("SYSTEM","Removing stale PID file (worker process is dead or PID has been reused)",{pid:r?.pid,port:r?.port,startedAt:r?.startedAt}),(0,Hc.rmSync)(e,{force:!0}),"stale")}var Hc,QF,BR,dme,pme,HR,ZR,Ua=ye(()=>{"use strict";Hc=require("fs"),QF=require("os"),BR=Oe(require("path"),1);oe();Qs();VF();XF();dme=BR.default.join((0,QF.homedir)(),".claude-mem"),pme=BR.default.join(dme,"worker.pid"),HR=class{registry;started=!1;stopPromise=null;signalHandlersRegistered=!1;shutdownInitiated=!1;shutdownHandler=null;constructor(e){this.registry=e}async start(){if(this.started)return;if(this.registry.initialize(),Wf({logAlive:!1})==="alive")throw new Error("Worker already running");this.started=!0,YF()}configureSignalHandlers(e){if(this.shutdownHandler=e,this.signalHandlersRegistered)return;this.signalHandlersRegistered=!0;let r=async i=>{if(this.shutdownInitiated){g.warn("SYSTEM",`Received ${i} but shutdown already in progress`);return}this.shutdownInitiated=!0,g.info("SYSTEM",`Received ${i}, shutting down...`);try{this.shutdownHandler?await this.shutdownHandler():await this.stop()}catch(n){n instanceof Error?g.error("SYSTEM","Error during shutdown",{},n):g.error("SYSTEM","Error during shutdown (non-Error)",{error:String(n)});try{await this.stop()}catch(s){s instanceof Error?g.debug("SYSTEM","Supervisor shutdown fallback failed",{},s):g.debug("SYSTEM","Supervisor shutdown fallback failed",{error:String(s)})}}process.exit(0)};process.on("SIGTERM",()=>{r("SIGTERM")}),process.on("SIGINT",()=>{r("SIGINT")}),process.platform!=="win32"&&(process.argv.includes("--daemon")?process.on("SIGHUP",()=>{g.debug("SYSTEM","Ignoring SIGHUP in daemon mode")}):process.on("SIGHUP",()=>{r("SIGHUP")}))}async stop(){if(this.stopPromise){await this.stopPromise;return}JF(),this.stopPromise=GF({registry:this.registry,currentPid:process.pid}).finally(()=>{this.started=!1,this.stopPromise=null}),await this.stopPromise}assertCanSpawn(e){if(this.stopPromise!==null)throw new Error(`Supervisor is shutting down, refusing to spawn ${e}`)}registerProcess(e,r,i){this.registry.register(e,r,i)}unregisterProcess(e){this.registry.unregister(e)}getRegistry(){return this.registry}},ZR=new HR(Da())});function mme(t,e={},r){return new Promise((i,n)=>{let s=setTimeout(()=>n(new Error(`Request timed out after ${r}ms`)),r);fetch(t,e).then(o=>{clearTimeout(s),i(o)},o=>{clearTimeout(s),n(o)})})}function On(){if(Hf!==null)return Hf;let t=La.default.join(ke.get("CLAUDE_MEM_DATA_DIR"),"settings.json"),e=ke.loadFromFile(t);return Hf=parseInt(e.CLAUDE_MEM_WORKER_PORT,10),Hf}function VR(){if(Bf!==null)return Bf;let t=La.default.join(ke.get("CLAUDE_MEM_DATA_DIR"),"settings.json");return Bf=ke.loadFromFile(t).CLAUDE_MEM_WORKER_HOST,Bf}function nq(){Hf=null,Bf=null}function fme(t){return`http://${VR()}:${On()}${t}`}function Li(t,e={}){let r=e.method??"GET",i=e.timeoutMs??GR,n=fme(t),s={method:r};return e.headers&&(s.headers=e.headers),e.body&&(s.body=e.body),i>0?mme(n,s,i):fetch(n,s)}async function hme(){return(await Li("/api/health",{timeoutMs:GR})).ok}function gme(){try{let t=La.default.join(Mo,"package.json");return JSON.parse((0,fi.readFileSync)(t,"utf-8")).version}catch(t){let e=t instanceof Error?t.code:void 0;if(e==="ENOENT"||e==="EBUSY")return g.debug("SYSTEM","Could not read plugin version (shutdown race)",{code:e}),"unknown";throw t}}async function vme(){let t=await Li("/api/version",{timeoutMs:GR});if(!t.ok)throw new Error(`Failed to get worker version: ${t.status}`);return(await t.json()).version}async function yme(){let t;try{t=gme()}catch(r){g.debug("SYSTEM","Version check failed reading plugin version",{error:r instanceof Error?r.message:String(r)});return}if(t==="unknown")return;let e;try{e=await vme()}catch(r){g.debug("SYSTEM","Version check failed reading worker version",{error:r instanceof Error?r.message:String(r)});return}e!=="unknown"&&t!==e&&g.debug("SYSTEM","Version check",{pluginVersion:t,workerVersion:e,note:"Mismatch will be auto-restarted by worker-service start command"})}function _me(){let t=[La.default.join(Mo,"plugin","scripts","worker-service.cjs"),La.default.join(process.cwd(),"plugin","scripts","worker-service.cjs")];for(let e of t)if((0,fi.existsSync)(e))return e;return null}function bme(){if(process.env.BUN&&(0,fi.existsSync)(process.env.BUN))return process.env.BUN;try{let t=process.platform==="win32"?"where bun":"which bun";return(0,hS.execSync)(t,{stdio:["ignore","pipe","ignore"],encoding:"utf-8",windowsHide:!0}).split(/\r?\n/).map(i=>i.trim()).find(i=>i.length>0)||null}catch{return null}}async function Sme(t){let e=t.backoffMs;for(let r=1;r<=t.attempts;r++){if(await iq())return!0;r<t.attempts&&(await new Promise(i=>setTimeout(i,e)),e*=2)}return!1}async function iq(){let t;try{t=await hme()}catch(r){return g.debug("SYSTEM","Worker health check threw",{error:r instanceof Error?r.message:String(r)}),!1}if(!t)return!1;let e=Wf({logAlive:!1});return e==="missing"||e==="alive"}async function gS(){if(await iq())return await yme(),!0;let t=bme(),e=_me();if(!t)return g.warn("SYSTEM","Cannot lazy-spawn worker: Bun runtime not found on PATH"),!1;if(!e)return g.warn("SYSTEM","Cannot lazy-spawn worker: worker-service.cjs not found in plugin/scripts"),!1;g.info("SYSTEM","Worker not running \u2014 lazy-spawning",{runtimePath:t,scriptPath:e});try{(0,hS.spawn)(t,[e,"--daemon"],{detached:!0,stdio:["ignore","ignore","ignore"]}).unref()}catch(i){return i instanceof Error?g.error("SYSTEM","Lazy-spawn of worker failed",{runtimePath:t,scriptPath:e},i):g.error("SYSTEM","Lazy-spawn of worker failed (non-Error)",{runtimePath:t,scriptPath:e,error:String(i)}),!1}return await Sme({attempts:3,backoffMs:250})?!0:(g.warn("SYSTEM","Worker port did not open after lazy-spawn within 3 attempts"),!1)}async function wme(){return fS!==null||(fS=await gS()),fS}function sq(){return La.default.join(Kt,"state")}function oq(){return La.default.join(sq(),"hook-failures.json")}function aq(){try{let t=(0,fi.readFileSync)(oq(),"utf-8"),e=JSON.parse(t);return{consecutiveFailures:typeof e.consecutiveFailures=="number"&&Number.isFinite(e.consecutiveFailures)?Math.max(0,Math.floor(e.consecutiveFailures)):0,lastFailureAt:typeof e.lastFailureAt=="number"&&Number.isFinite(e.lastFailureAt)?e.lastFailureAt:0}}catch{return{consecutiveFailures:0,lastFailureAt:0}}}function cq(t){let e=sq(),r=oq(),i=`${r}.tmp`;try{(0,fi.existsSync)(e)||(0,fi.mkdirSync)(e,{recursive:!0}),(0,fi.writeFileSync)(i,JSON.stringify(t),"utf-8"),(0,fi.renameSync)(i,r)}catch(n){g.debug("SYSTEM","Failed to persist hook-failure counter",{error:n instanceof Error?n.message:String(n)})}}function Eme(){try{let e=Ma().CLAUDE_MEM_HOOK_FAIL_LOUD_THRESHOLD,r=parseInt(e,10);if(Number.isFinite(r)&&r>=1)return r}catch{}return xme}function kme(){let e={consecutiveFailures:aq().consecutiveFailures+1,lastFailureAt:Date.now()};cq(e);let r=Eme();return e.consecutiveFailures>=r&&(process.stderr.write(`claude-mem worker unreachable for ${e.consecutiveFailures} consecutive hooks.
20
- `),process.exit(dt.BLOCKING_ERROR)),e.consecutiveFailures}function rq(){aq().consecutiveFailures!==0&&cq({consecutiveFailures:0,lastFailureAt:0})}function Qr(t){return typeof t=="object"&&t!==null&&t[uq]===!0}async function en(t,e,r,i={}){if(!await wme())return kme(),{continue:!0,reason:"worker_unreachable",[uq]:!0};let s={method:e};r!==void 0&&(s.headers={"Content-Type":"application/json"},s.body=JSON.stringify(r)),i.timeoutMs!==void 0&&(s.timeoutMs=i.timeoutMs);let o=await Li(t,s);if(!o.ok){rq();let c=await o.text().catch(()=>""),u=c;try{u=JSON.parse(c)}catch{}return u}rq();let a=await o.text();if(a.length!==0)try{return JSON.parse(a)}catch{return a}}var La,fi,hS,GR,Hf,Bf,fS,xme,uq,hn=ye(()=>{"use strict";La=Oe(require("path"),1),fi=require("fs"),hS=require("child_process");oe();Xn();sr();Tt();zf();Ua();GR=(()=>{let t=process.env.CLAUDE_MEM_HEALTH_TIMEOUT_MS;if(t){let e=parseInt(t,10);if(Number.isFinite(e)&&e>=500&&e<=3e5)return e;g.warn("SYSTEM","Invalid CLAUDE_MEM_HEALTH_TIMEOUT_MS, using default",{value:t,min:500,max:3e5})}return IF(fn.HEALTH_CHECK)})();Hf=null,Bf=null;fS=null;xme=3;uq=Symbol.for("claude-mem/worker-fallback")});function hq(t){let e=bS.default.join(t,".git"),r;try{r=(0,SS.statSync)(e)}catch(l){return l instanceof Error&&l.code!=="ENOENT"&&console.warn("[worktree] Unexpected error checking .git:",l),Vf}if(!r.isFile())return Vf;let i;try{i=(0,SS.readFileSync)(e,"utf-8").trim()}catch(l){return console.warn("[worktree] Failed to read .git file:",l instanceof Error?l.message:String(l)),Vf}let n=i.match(/^gitdir:\s*(.+)$/);if(!n)return Vf;let o=n[1].match(/^(.+)[/\\]\.git[/\\]worktrees[/\\]([^/\\]+)$/);if(!o)return Vf;let a=o[1],c=bS.default.basename(t),u=bS.default.basename(a);return{isWorktree:!0,worktreeName:c,parentRepoPath:a,parentProjectName:u}}var SS,bS,Vf,gq=ye(()=>{"use strict";SS=require("fs"),bS=Oe(require("path"),1),Vf={isWorktree:!1,worktreeName:null,parentRepoPath:null,parentProjectName:null}});function _q(t){return t==="~"||t.startsWith("~/")?t.replace(/^~/,(0,vq.homedir)()):t}function Nme(t){if(!t||t.trim()==="")return g.warn("PROJECT_NAME","Empty cwd provided, using fallback",{cwd:t}),"unknown-project";let e=_q(t),r=yq.default.basename(e);if(r===""){if(process.platform==="win32"){let n=t.match(/^([A-Z]):\\/i);if(n){let o=`drive-${n[1].toUpperCase()}`;return g.info("PROJECT_NAME","Drive root detected",{cwd:t,projectName:o}),o}}return g.warn("PROJECT_NAME","Root directory detected, using fallback",{cwd:t}),"unknown-project"}return r}function vr(t){let e=Nme(t);if(!t)return{primary:e,parent:null,isWorktree:!1,allProjects:[e]};let r=_q(t),i=hq(r);if(i.isWorktree&&i.parentProjectName){let n=`${i.parentProjectName}/${e}`;return{primary:n,parent:i.parentProjectName,isWorktree:!0,allProjects:[i.parentProjectName,n]}}return{primary:e,parent:null,isWorktree:!1,allProjects:[e]}}var vq,yq,eo=ye(()=>{"use strict";vq=require("os"),yq=Oe(require("path"),1);oe();gq()});function wS(t,e,r){return(0,bq.createHash)("sha256").update([t||"",e||"",r||""].join("\0")).digest("hex").slice(0,16)}var bq,Sq=ye(()=>{"use strict";bq=require("crypto");oe();eo()});function td(t){if(!t)return[];try{let e=JSON.parse(t);return Array.isArray(e)?e:[String(e)]}catch{return[t]}}var nC=ye(()=>{"use strict"});function Mme(t){return t.trim().toLowerCase().replace(/\s+/g,"-")}function Zt(t){if(!t)return gn;let e=Mme(t);return e?e==="transcript"||e.includes("codex")?"codex":e.includes("cursor")?"cursor":e.includes("claude")?"claude":e:gn}function wq(t){let e=["claude","codex","cursor"];return[...t].sort((r,i)=>{let n=e.indexOf(r),s=e.indexOf(i);return n!==-1||s!==-1?n===-1?1:s===-1?-1:n-s:r.localeCompare(i)})}var gn,to=ye(()=>{"use strict";gn="claude"});function $me(t,e){return{customTitle:t,platformSource:e?Zt(e):void 0}}var iC,Do,xS=ye(()=>{"use strict";iC=require("bun:sqlite");Tt();oe();Sq();nC();to();Do=class{db;constructor(e=Wc){e instanceof iC.Database?this.db=e:(e!==":memory:"&&xr(Kt),this.db=new iC.Database(e),this.db.run("PRAGMA journal_mode = WAL"),this.db.run("PRAGMA synchronous = NORMAL"),this.db.run("PRAGMA foreign_keys = ON"),this.db.run("PRAGMA journal_size_limit = 4194304")),this.initializeSchema(),this.ensureWorkerPortColumn(),this.ensurePromptTrackingColumns(),this.removeSessionSummariesUniqueConstraint(),this.addObservationHierarchicalFields(),this.makeObservationsTextNullable(),this.createUserPromptsTable(),this.ensureDiscoveryTokensColumn(),this.createPendingMessagesTable(),this.renameSessionIdColumns(),this.repairSessionIdColumnRename(),this.addFailedAtEpochColumn(),this.addOnUpdateCascadeToForeignKeys(),this.addObservationContentHashColumn(),this.addSessionCustomTitleColumn(),this.addSessionPlatformSourceColumn(),this.addObservationModelColumns(),this.ensureMergedIntoProjectColumns(),this.addObservationSubagentColumns(),this.addObservationsUniqueContentHashIndex()}initializeSchema(){this.db.run(`
20
+ `),process.exit(dt.BLOCKING_ERROR)),e.consecutiveFailures}function rq(){aq().consecutiveFailures!==0&&cq({consecutiveFailures:0,lastFailureAt:0})}function Qr(t){return typeof t=="object"&&t!==null&&t[uq]===!0}async function en(t,e,r,i={}){if(!await wme())return kme(),{continue:!0,reason:"worker_unreachable",[uq]:!0};let s={method:e};r!==void 0&&(s.headers={"Content-Type":"application/json"},s.body=JSON.stringify(r)),i.timeoutMs!==void 0&&(s.timeoutMs=i.timeoutMs);let o=await Li(t,s);if(!o.ok){rq();let c=await o.text().catch(()=>""),u=c;try{u=JSON.parse(c)}catch{}return u}rq();let a=await o.text();if(a.length!==0)try{return JSON.parse(a)}catch{return a}}var La,fi,hS,GR,Hf,Bf,fS,xme,uq,hn=ye(()=>{"use strict";La=Oe(require("path"),1),fi=require("fs"),hS=require("child_process");oe();Xn();sr();Tt();zf();Ua();GR=(()=>{let t=process.env.CLAUDE_MEM_HEALTH_TIMEOUT_MS;if(t){let e=parseInt(t,10);if(Number.isFinite(e)&&e>=500&&e<=3e5)return e;g.warn("SYSTEM","Invalid CLAUDE_MEM_HEALTH_TIMEOUT_MS, using default",{value:t,min:500,max:3e5})}return IF(fn.HEALTH_CHECK)})();Hf=null,Bf=null;fS=null;xme=3;uq=Symbol.for("claude-mem/worker-fallback")});function hq(t){let e=bS.default.join(t,".git"),r;try{r=(0,SS.statSync)(e)}catch(l){return l instanceof Error&&l.code!=="ENOENT"&&console.warn("[worktree] Unexpected error checking .git:",l),Vf}if(!r.isFile())return Vf;let i;try{i=(0,SS.readFileSync)(e,"utf-8").trim()}catch(l){return console.warn("[worktree] Failed to read .git file:",l instanceof Error?l.message:String(l)),Vf}let n=i.match(/^gitdir:\s*(.+)$/);if(!n)return Vf;let o=n[1].match(/^(.+)[/\\]\.git[/\\]worktrees[/\\]([^/\\]+)$/);if(!o)return Vf;let a=o[1],c=bS.default.basename(t),u=bS.default.basename(a);return{isWorktree:!0,worktreeName:c,parentRepoPath:a,parentProjectName:u}}var SS,bS,Vf,gq=ye(()=>{"use strict";SS=require("fs"),bS=Oe(require("path"),1),Vf={isWorktree:!1,worktreeName:null,parentRepoPath:null,parentProjectName:null}});function _q(t){return t==="~"||t.startsWith("~/")?t.replace(/^~/,(0,vq.homedir)()):t}function Nme(t){if(!t||t.trim()==="")return g.warn("PROJECT_NAME","Empty cwd provided, using fallback",{cwd:t}),"unknown-project";let e=_q(t),r=yq.default.basename(e);if(r===""){if(process.platform==="win32"){let n=t.match(/^([A-Z]):\\/i);if(n){let o=`drive-${n[1].toUpperCase()}`;return g.info("PROJECT_NAME","Drive root detected",{cwd:t,projectName:o}),o}}return g.warn("PROJECT_NAME","Root directory detected, using fallback",{cwd:t}),"unknown-project"}return r}function vr(t){let e=Nme(t);if(!t)return{primary:e,parent:null,isWorktree:!1,allProjects:[e]};let r=_q(t),i=hq(r);if(i.isWorktree&&i.parentProjectName){let n=`${i.parentProjectName}/${e}`;return{primary:n,parent:i.parentProjectName,isWorktree:!0,allProjects:[i.parentProjectName,n]}}return{primary:e,parent:null,isWorktree:!1,allProjects:[e]}}var vq,yq,eo=ye(()=>{"use strict";vq=require("os"),yq=Oe(require("path"),1);oe();gq()});function wS(t,e,r){return(0,bq.createHash)("sha256").update([t||"",e||"",r||""].join("\0")).digest("hex").slice(0,16)}var bq,Sq=ye(()=>{"use strict";bq=require("crypto");oe();eo()});function td(t){if(!t)return[];try{let e=JSON.parse(t);return Array.isArray(e)?e:[String(e)]}catch{return[t]}}var nC=ye(()=>{"use strict"});function Mme(t){return t.trim().toLowerCase().replace(/\s+/g,"-")}function Zt(t){if(!t)return gn;let e=Mme(t);return e?e==="transcript"||e.includes("codex")?"codex":e.includes("cursor")?"cursor":e.includes("claude")?"claude":e:gn}function wq(t){let e=["claude","codex","cursor"];return[...t].sort((r,i)=>{let n=e.indexOf(r),s=e.indexOf(i);return n!==-1||s!==-1?n===-1?1:s===-1?-1:n-s:r.localeCompare(i)})}var gn,to=ye(()=>{"use strict";gn="claude"});function $me(t,e){return{customTitle:t,platformSource:e?Zt(e):void 0}}var iC,Do,xS=ye(()=>{"use strict";iC=require("bun:sqlite");Tt();oe();Sq();nC();to();Do=class{db;constructor(e=Wc){e instanceof iC.Database?this.db=e:(e!==":memory:"&&xr(Kt),this.db=new iC.Database(e),this.db.run("PRAGMA journal_mode = WAL"),this.db.run("PRAGMA synchronous = NORMAL"),this.db.run("PRAGMA foreign_keys = ON"),this.db.run("PRAGMA journal_size_limit = 4194304")),this.initializeSchema(),this.ensureWorkerPortColumn(),this.ensurePromptTrackingColumns(),this.removeSessionSummariesUniqueConstraint(),this.addObservationHierarchicalFields(),this.makeObservationsTextNullable(),this.createUserPromptsTable(),this.ensureDiscoveryTokensColumn(),this.createPendingMessagesTable(),this.renameSessionIdColumns(),this.repairSessionIdColumnRename(),this.addFailedAtEpochColumn(),this.addOnUpdateCascadeToForeignKeys(),this.addObservationContentHashColumn(),this.addSessionCustomTitleColumn(),this.addSessionPlatformSourceColumn(),this.addObservationModelColumns(),this.ensureMergedIntoProjectColumns(),this.addObservationSubagentColumns(),this.addPendingMessagesToolUseIdAndWorkerPidColumns(),this.addObservationsUniqueContentHashIndex()}initializeSchema(){this.db.run(`
21
21
  CREATE TABLE IF NOT EXISTS schema_versions (
22
22
  id INTEGER PRIMARY KEY,
23
23
  version INTEGER UNIQUE NOT NULL,
@@ -301,7 +301,19 @@ ${s.stack}`:` ${s.message}`:this.getLevel()===0&&typeof s=="object"?l=`
301
301
  UPDATE sdk_sessions
302
302
  SET platform_source = '${gn}'
303
303
  WHERE platform_source IS NULL OR platform_source = ''
304
- `),n||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()))}addObservationModelColumns(){let e=this.db.query("PRAGMA table_info(observations)").all(),r=e.some(n=>n.name==="generated_by_model"),i=e.some(n=>n.name==="relevance_count");r&&i||(r||this.db.run("ALTER TABLE observations ADD COLUMN generated_by_model TEXT"),i||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()))}ensureMergedIntoProjectColumns(){this.db.query("PRAGMA table_info(observations)").all().some(i=>i.name==="merged_into_project")||this.db.run("ALTER TABLE observations ADD COLUMN merged_into_project TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_merged_into ON observations(merged_into_project)"),this.db.query("PRAGMA table_info(session_summaries)").all().some(i=>i.name==="merged_into_project")||this.db.run("ALTER TABLE session_summaries ADD COLUMN merged_into_project TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_summaries_merged_into ON session_summaries(merged_into_project)")}addObservationSubagentColumns(){let e=this.db.prepare("SELECT version FROM schema_versions WHERE version = ?").get(27),r=this.db.query("PRAGMA table_info(observations)").all(),i=r.some(o=>o.name==="agent_type"),n=r.some(o=>o.name==="agent_id");i||this.db.run("ALTER TABLE observations ADD COLUMN agent_type TEXT"),n||this.db.run("ALTER TABLE observations ADD COLUMN agent_id TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_agent_type ON observations(agent_type)"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_agent_id ON observations(agent_id)");let s=this.db.query("PRAGMA table_info(pending_messages)").all();if(s.length>0){let o=s.some(c=>c.name==="agent_type"),a=s.some(c=>c.name==="agent_id");o||this.db.run("ALTER TABLE pending_messages ADD COLUMN agent_type TEXT"),a||this.db.run("ALTER TABLE pending_messages ADD COLUMN agent_id TEXT")}e||this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(27,new Date().toISOString())}addObservationsUniqueContentHashIndex(){if(this.db.prepare("SELECT version FROM schema_versions WHERE version = ?").get(29))return;let r=this.db.query("PRAGMA table_info(observations)").all(),i=r.some(s=>s.name==="memory_session_id"),n=r.some(s=>s.name==="content_hash");if(!i||!n){this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(29,new Date().toISOString());return}this.db.run("BEGIN TRANSACTION");try{this.db.run(`
304
+ `),n||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()))}addObservationModelColumns(){let e=this.db.query("PRAGMA table_info(observations)").all(),r=e.some(n=>n.name==="generated_by_model"),i=e.some(n=>n.name==="relevance_count");r&&i||(r||this.db.run("ALTER TABLE observations ADD COLUMN generated_by_model TEXT"),i||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()))}ensureMergedIntoProjectColumns(){this.db.query("PRAGMA table_info(observations)").all().some(i=>i.name==="merged_into_project")||this.db.run("ALTER TABLE observations ADD COLUMN merged_into_project TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_merged_into ON observations(merged_into_project)"),this.db.query("PRAGMA table_info(session_summaries)").all().some(i=>i.name==="merged_into_project")||this.db.run("ALTER TABLE session_summaries ADD COLUMN merged_into_project TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_summaries_merged_into ON session_summaries(merged_into_project)")}addObservationSubagentColumns(){let e=this.db.prepare("SELECT version FROM schema_versions WHERE version = ?").get(27),r=this.db.query("PRAGMA table_info(observations)").all(),i=r.some(o=>o.name==="agent_type"),n=r.some(o=>o.name==="agent_id");i||this.db.run("ALTER TABLE observations ADD COLUMN agent_type TEXT"),n||this.db.run("ALTER TABLE observations ADD COLUMN agent_id TEXT"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_agent_type ON observations(agent_type)"),this.db.run("CREATE INDEX IF NOT EXISTS idx_observations_agent_id ON observations(agent_id)");let s=this.db.query("PRAGMA table_info(pending_messages)").all();if(s.length>0){let o=s.some(c=>c.name==="agent_type"),a=s.some(c=>c.name==="agent_id");o||this.db.run("ALTER TABLE pending_messages ADD COLUMN agent_type TEXT"),a||this.db.run("ALTER TABLE pending_messages ADD COLUMN agent_id TEXT")}e||this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(27,new Date().toISOString())}addPendingMessagesToolUseIdAndWorkerPidColumns(){if(this.db.query("SELECT name FROM sqlite_master WHERE type='table' AND name='pending_messages'").all().length===0){this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(28,new Date().toISOString());return}let r=this.db.query("PRAGMA table_info(pending_messages)").all(),i=r.some(s=>s.name==="tool_use_id"),n=r.some(s=>s.name==="worker_pid");i||this.db.run("ALTER TABLE pending_messages ADD COLUMN tool_use_id TEXT"),n||this.db.run("ALTER TABLE pending_messages ADD COLUMN worker_pid INTEGER"),this.db.run("BEGIN TRANSACTION");try{this.db.run("CREATE INDEX IF NOT EXISTS idx_pending_messages_worker_pid ON pending_messages(worker_pid)"),this.db.run(`
305
+ DELETE FROM pending_messages
306
+ WHERE tool_use_id IS NOT NULL
307
+ AND id NOT IN (
308
+ SELECT MIN(id) FROM pending_messages
309
+ WHERE tool_use_id IS NOT NULL
310
+ GROUP BY content_session_id, tool_use_id
311
+ )
312
+ `),this.db.run(`
313
+ CREATE UNIQUE INDEX IF NOT EXISTS ux_pending_session_tool
314
+ ON pending_messages(content_session_id, tool_use_id)
315
+ WHERE tool_use_id IS NOT NULL
316
+ `),this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(28,new Date().toISOString()),this.db.run("COMMIT")}catch(s){throw this.db.run("ROLLBACK"),s}}addObservationsUniqueContentHashIndex(){if(this.db.prepare("SELECT version FROM schema_versions WHERE version = ?").get(29))return;let r=this.db.query("PRAGMA table_info(observations)").all(),i=r.some(s=>s.name==="memory_session_id"),n=r.some(s=>s.name==="content_hash");if(!i||!n){this.db.prepare("INSERT OR IGNORE INTO schema_versions (version, applied_at) VALUES (?, ?)").run(29,new Date().toISOString());return}this.db.run("BEGIN TRANSACTION");try{this.db.run(`
305
317
  DELETE FROM observations
306
318
  WHERE id NOT IN (
307
319
  SELECT MIN(id) FROM observations
@@ -1047,7 +1059,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
1047
1059
  SELECT cwd FROM pending_messages
1048
1060
  WHERE cwd IS NOT NULL AND cwd != ''
1049
1061
  GROUP BY cwd
1050
- `).all();for(let{cwd:u}of c){let l=Zq(u);l&&n.add(l)}}finally{s?.close()}if(n.size===0)return g.debug("SYSTEM","Worktree adoption found no known parent repos"),i;for(let o of n)try{let a=await yC({repoPath:o,dataDirectory:e,dryRun:t.dryRun});i.push(a)}catch(a){g.warn("SYSTEM","Worktree adoption failed for parent repo (continuing)",{repoPath:o,error:a instanceof Error?a.message:String(a)})}return i}var WB=Oe(j0(),1),HB=Oe(require("http"),1),UA=Oe(require("fs"),1),xh=Oe(require("path"),1);var MA=["search","context","summarize","import","export"],CB=["workflow","search_params","examples","all"];oe();var $A=Oe(j0(),1),DB=Oe($B(),1),jB=Oe(require("path"),1);Tt();oe();function DA(t){let e=[];e.push($A.default.json({limit:"5mb"})),e.push((0,DB.default)({origin:(n,s)=>{!n||n.startsWith("http://localhost:")||n.startsWith("http://127.0.0.1:")?s(null,!0):s(new Error("CORS not allowed"))},methods:["GET","HEAD","POST","PUT","PATCH","DELETE"],allowedHeaders:["Content-Type","X-Requested-With"],credentials:!1})),e.push((n,s,o)=>{let c=[".html",".js",".css",".svg",".png",".jpg",".jpeg",".webp",".woff",".woff2",".ttf",".eot"].some(f=>n.path.endsWith(f)),u=n.path==="/api/logs";if(n.path.startsWith("/health")||n.path==="/"||c||u)return o();let l=Date.now(),d=`${n.method}-${Date.now()}`,p=t(n.method,n.path,n.body);g.debug("HTTP",`\u2192 ${n.method} ${n.path}`,{requestId:d},p);let m=s.send.bind(s);s.send=function(f){let h=Date.now()-l;return g.debug("HTTP",`\u2190 ${s.statusCode} ${n.path}`,{requestId:d,duration:`${h}ms`}),m(f)},o()});let r=Qn(),i=jB.default.join(r,"plugin","ui");return e.push($A.default.static(i)),e}function Sh(t,e,r){let i=t.ip||t.connection.remoteAddress||"";if(!(i==="127.0.0.1"||i==="::1"||i==="::ffff:127.0.0.1"||i==="localhost")){g.warn("SECURITY","Admin endpoint access denied - not localhost",{endpoint:t.path,clientIp:i,method:t.method}),e.status(403).json({error:"Forbidden",message:"Admin endpoints are only accessible from localhost"});return}r()}function jA(t,e,r){if(!r||Object.keys(r).length===0||e.includes("/init"))return"";if(e.includes("/observations")){let i=r.tool_name||"?",n=r.tool_input;return`tool=${g.formatTool(i,n)}`}return e.includes("/summarize")?"requesting summary":""}wh();Ua();Qs();$a();var BB=xh.default.resolve(__dirname,"../skills/mem-search"),Mwe=xh.default.join(BB,"operations"),zA=xh.default.join(BB,"SKILL.md"),FB=(()=>{try{let t=UA.readFileSync(zA,"utf-8");return g.info("SYSTEM","Cached SKILL.md at boot",{path:zA,bytes:Buffer.byteLength(t,"utf-8")}),t}catch(t){return g.debug("SYSTEM","SKILL.md not present at boot, /api/instructions will 404 for topic queries",{path:zA,message:t instanceof Error?t.message:String(t)}),null}})(),$we=(()=>{let t=new Map;for(let e of MA){let r=xh.default.join(Mwe,`${e}.md`);try{t.set(e,UA.readFileSync(r,"utf-8"))}catch(i){g.debug("SYSTEM","Operation instruction file not present at boot",{path:r,message:i instanceof Error?i.message:String(i)})}}return t.size>0&&g.info("SYSTEM","Cached operation instruction files at boot",{count:t.size,operations:Array.from(t.keys())}),t})(),qB="12.4.4",z0=class{app;server=null;options;startTime=Date.now();constructor(e){this.options=e,this.app=(0,WB.default)(),this.setupMiddleware(),this.setupCoreRoutes()}getHttpServer(){return this.server}async listen(e,r){return new Promise((i,n)=>{let s=HB.default.createServer(this.app);this.server=s;let o=c=>{s.off("listening",a),n(c)},a=()=>{s.off("error",o),g.info("SYSTEM","HTTP server started",{host:r,port:e,pid:process.pid}),i()};s.once("error",o),s.once("listening",a),s.listen(e,r)})}async close(){this.server&&(this.server.closeAllConnections(),process.platform==="win32"&&await new Promise(e=>setTimeout(e,500)),await new Promise((e,r)=>{this.server.close(i=>i?r(i):e())}),process.platform==="win32"&&await new Promise(e=>setTimeout(e,500)),this.server=null,g.info("SYSTEM","HTTP server closed"))}registerRoutes(e){e.setupRoutes(this.app)}finalizeRoutes(){this.app.use(LB),this.app.use(UB)}setupMiddleware(){DA(jA).forEach(r=>this.app.use(r))}setupCoreRoutes(){this.app.get("/api/health",(e,r)=>{r.status(200).json({status:"ok",version:qB,workerPath:this.options.workerPath,uptime:Date.now()-this.startTime,managed:process.env.CLAUDE_MEM_MANAGED==="true",hasIpc:typeof process.send=="function",platform:process.platform,pid:process.pid,initialized:this.options.getInitializationComplete(),mcpReady:this.options.getMcpReady(),ai:this.options.getAiStatus()})}),this.app.get("/api/readiness",(e,r)=>{this.options.getInitializationComplete()?r.status(200).json({status:"ready",mcpReady:this.options.getMcpReady()}):r.status(503).json({status:"initializing",message:"Worker is still initializing, please retry"})}),this.app.get("/api/version",(e,r)=>{r.status(200).json({version:qB})}),this.app.get("/api/instructions",(e,r)=>{let i=e.query.topic||"all",n=e.query.operation;if(i&&!CB.includes(i))return r.status(400).json({error:"Invalid topic"});if(n&&!MA.includes(n))return r.status(400).json({error:"Invalid operation"});if(n){let o=$we.get(n);return o===void 0?(g.debug("HTTP","Instruction file not cached at boot",{operation:n}),r.status(404).json({error:"Instruction not found"})):r.json({content:[{type:"text",text:o}]})}if(FB===null)return g.debug("HTTP","SKILL.md not cached at boot",{topic:i}),r.status(404).json({error:"Instruction not found"});let s=this.extractInstructionSection(FB,i);r.json({content:[{type:"text",text:s}]})}),this.app.post("/api/admin/restart",Sh,async(e,r)=>{r.json({status:"restarting"}),process.platform==="win32"&&process.env.CLAUDE_MEM_MANAGED==="true"&&process.send?(g.info("SYSTEM","Sending restart request to wrapper"),process.send({type:"restart"})):setTimeout(async()=>{try{await this.options.onRestart()}finally{process.exit(0)}},100)}),this.app.post("/api/admin/shutdown",Sh,async(e,r)=>{r.json({status:"shutting_down"}),process.platform==="win32"&&process.env.CLAUDE_MEM_MANAGED==="true"&&process.send?(g.info("SYSTEM","Sending shutdown request to wrapper"),process.send({type:"shutdown"})):setTimeout(async()=>{try{await this.options.onShutdown()}finally{process.exit(0)}},100)}),this.app.get("/api/admin/doctor",Sh,(e,r)=>{let o=Xr().getRegistry().getAll().map(f=>({id:f.id,pid:f.pid,type:f.type,status:mi(f.pid)?"alive":"dead",startedAt:f.startedAt})),a=o.filter(f=>f.status==="dead").map(f=>f.pid),c=!Object.keys(process.env).some(f=>UR.has(f)||zR.some(h=>f.startsWith(h))),u=Date.now()-this.startTime,l=Math.floor(u/1e3),d=Math.floor(l/3600),p=Math.floor(l%3600/60),m=d>0?`${d}h ${p}m`:`${p}m`;r.json({supervisor:{running:!0,pid:process.pid,uptime:m},processes:o,health:{deadProcessPids:a,envClean:c}})})}extractInstructionSection(e,r){let i={workflow:this.extractBetween(e,"## The Workflow","## Search Parameters"),search_params:this.extractBetween(e,"## Search Parameters","## Examples"),examples:this.extractBetween(e,"## Examples","## Why This Workflow"),all:e};return i[r]||i.all}extractBetween(e,r,i){let n=e.indexOf(r),s=e.indexOf(i);return n===-1?e:s===-1?e.substring(n):e.substring(n,s).trim()}};var qt=Oe(require("path"),1),kh=require("os"),or=require("fs"),VB=require("child_process"),KB=require("util");oe();hn();Tt();var Xi=require("fs"),Eh=require("path");oe();function ZB(t){try{return(0,Xi.existsSync)(t)?JSON.parse((0,Xi.readFileSync)(t,"utf-8")):{}}catch(e){return g.error("CONFIG","Failed to read Cursor registry, using empty registry",{file:t,error:e instanceof Error?e.message:String(e)}),{}}}function GB(t,e){let r=(0,Eh.join)(t,"..");(0,Xi.mkdirSync)(r,{recursive:!0}),(0,Xi.writeFileSync)(t,JSON.stringify(e,null,2))}function LA(t,e){let r=(0,Eh.join)(t,".cursor","rules"),i=(0,Eh.join)(r,"claude-mem-context.mdc"),n=`${i}.tmp`;(0,Xi.mkdirSync)(r,{recursive:!0});let s=`---
1062
+ `).all();for(let{cwd:u}of c){let l=Zq(u);l&&n.add(l)}}finally{s?.close()}if(n.size===0)return g.debug("SYSTEM","Worktree adoption found no known parent repos"),i;for(let o of n)try{let a=await yC({repoPath:o,dataDirectory:e,dryRun:t.dryRun});i.push(a)}catch(a){g.warn("SYSTEM","Worktree adoption failed for parent repo (continuing)",{repoPath:o,error:a instanceof Error?a.message:String(a)})}return i}var WB=Oe(j0(),1),HB=Oe(require("http"),1),UA=Oe(require("fs"),1),xh=Oe(require("path"),1);var MA=["search","context","summarize","import","export"],CB=["workflow","search_params","examples","all"];oe();var $A=Oe(j0(),1),DB=Oe($B(),1),jB=Oe(require("path"),1);Tt();oe();function DA(t){let e=[];e.push($A.default.json({limit:"5mb"})),e.push((0,DB.default)({origin:(n,s)=>{!n||n.startsWith("http://localhost:")||n.startsWith("http://127.0.0.1:")?s(null,!0):s(new Error("CORS not allowed"))},methods:["GET","HEAD","POST","PUT","PATCH","DELETE"],allowedHeaders:["Content-Type","X-Requested-With"],credentials:!1})),e.push((n,s,o)=>{let c=[".html",".js",".css",".svg",".png",".jpg",".jpeg",".webp",".woff",".woff2",".ttf",".eot"].some(f=>n.path.endsWith(f)),u=n.path==="/api/logs";if(n.path.startsWith("/health")||n.path==="/"||c||u)return o();let l=Date.now(),d=`${n.method}-${Date.now()}`,p=t(n.method,n.path,n.body);g.debug("HTTP",`\u2192 ${n.method} ${n.path}`,{requestId:d},p);let m=s.send.bind(s);s.send=function(f){let h=Date.now()-l;return g.debug("HTTP",`\u2190 ${s.statusCode} ${n.path}`,{requestId:d,duration:`${h}ms`}),m(f)},o()});let r=Qn(),i=jB.default.join(r,"plugin","ui");return e.push($A.default.static(i)),e}function Sh(t,e,r){let i=t.ip||t.connection.remoteAddress||"";if(!(i==="127.0.0.1"||i==="::1"||i==="::ffff:127.0.0.1"||i==="localhost")){g.warn("SECURITY","Admin endpoint access denied - not localhost",{endpoint:t.path,clientIp:i,method:t.method}),e.status(403).json({error:"Forbidden",message:"Admin endpoints are only accessible from localhost"});return}r()}function jA(t,e,r){if(!r||Object.keys(r).length===0||e.includes("/init"))return"";if(e.includes("/observations")){let i=r.tool_name||"?",n=r.tool_input;return`tool=${g.formatTool(i,n)}`}return e.includes("/summarize")?"requesting summary":""}wh();Ua();Qs();$a();var BB=xh.default.resolve(__dirname,"../skills/mem-search"),Mwe=xh.default.join(BB,"operations"),zA=xh.default.join(BB,"SKILL.md"),FB=(()=>{try{let t=UA.readFileSync(zA,"utf-8");return g.info("SYSTEM","Cached SKILL.md at boot",{path:zA,bytes:Buffer.byteLength(t,"utf-8")}),t}catch(t){return g.debug("SYSTEM","SKILL.md not present at boot, /api/instructions will 404 for topic queries",{path:zA,message:t instanceof Error?t.message:String(t)}),null}})(),$we=(()=>{let t=new Map;for(let e of MA){let r=xh.default.join(Mwe,`${e}.md`);try{t.set(e,UA.readFileSync(r,"utf-8"))}catch(i){g.debug("SYSTEM","Operation instruction file not present at boot",{path:r,message:i instanceof Error?i.message:String(i)})}}return t.size>0&&g.info("SYSTEM","Cached operation instruction files at boot",{count:t.size,operations:Array.from(t.keys())}),t})(),qB="12.4.5",z0=class{app;server=null;options;startTime=Date.now();constructor(e){this.options=e,this.app=(0,WB.default)(),this.setupMiddleware(),this.setupCoreRoutes()}getHttpServer(){return this.server}async listen(e,r){return new Promise((i,n)=>{let s=HB.default.createServer(this.app);this.server=s;let o=c=>{s.off("listening",a),n(c)},a=()=>{s.off("error",o),g.info("SYSTEM","HTTP server started",{host:r,port:e,pid:process.pid}),i()};s.once("error",o),s.once("listening",a),s.listen(e,r)})}async close(){this.server&&(this.server.closeAllConnections(),process.platform==="win32"&&await new Promise(e=>setTimeout(e,500)),await new Promise((e,r)=>{this.server.close(i=>i?r(i):e())}),process.platform==="win32"&&await new Promise(e=>setTimeout(e,500)),this.server=null,g.info("SYSTEM","HTTP server closed"))}registerRoutes(e){e.setupRoutes(this.app)}finalizeRoutes(){this.app.use(LB),this.app.use(UB)}setupMiddleware(){DA(jA).forEach(r=>this.app.use(r))}setupCoreRoutes(){this.app.get("/api/health",(e,r)=>{r.status(200).json({status:"ok",version:qB,workerPath:this.options.workerPath,uptime:Date.now()-this.startTime,managed:process.env.CLAUDE_MEM_MANAGED==="true",hasIpc:typeof process.send=="function",platform:process.platform,pid:process.pid,initialized:this.options.getInitializationComplete(),mcpReady:this.options.getMcpReady(),ai:this.options.getAiStatus()})}),this.app.get("/api/readiness",(e,r)=>{this.options.getInitializationComplete()?r.status(200).json({status:"ready",mcpReady:this.options.getMcpReady()}):r.status(503).json({status:"initializing",message:"Worker is still initializing, please retry"})}),this.app.get("/api/version",(e,r)=>{r.status(200).json({version:qB})}),this.app.get("/api/instructions",(e,r)=>{let i=e.query.topic||"all",n=e.query.operation;if(i&&!CB.includes(i))return r.status(400).json({error:"Invalid topic"});if(n&&!MA.includes(n))return r.status(400).json({error:"Invalid operation"});if(n){let o=$we.get(n);return o===void 0?(g.debug("HTTP","Instruction file not cached at boot",{operation:n}),r.status(404).json({error:"Instruction not found"})):r.json({content:[{type:"text",text:o}]})}if(FB===null)return g.debug("HTTP","SKILL.md not cached at boot",{topic:i}),r.status(404).json({error:"Instruction not found"});let s=this.extractInstructionSection(FB,i);r.json({content:[{type:"text",text:s}]})}),this.app.post("/api/admin/restart",Sh,async(e,r)=>{r.json({status:"restarting"}),process.platform==="win32"&&process.env.CLAUDE_MEM_MANAGED==="true"&&process.send?(g.info("SYSTEM","Sending restart request to wrapper"),process.send({type:"restart"})):setTimeout(async()=>{try{await this.options.onRestart()}finally{process.exit(0)}},100)}),this.app.post("/api/admin/shutdown",Sh,async(e,r)=>{r.json({status:"shutting_down"}),process.platform==="win32"&&process.env.CLAUDE_MEM_MANAGED==="true"&&process.send?(g.info("SYSTEM","Sending shutdown request to wrapper"),process.send({type:"shutdown"})):setTimeout(async()=>{try{await this.options.onShutdown()}finally{process.exit(0)}},100)}),this.app.get("/api/admin/doctor",Sh,(e,r)=>{let o=Xr().getRegistry().getAll().map(f=>({id:f.id,pid:f.pid,type:f.type,status:mi(f.pid)?"alive":"dead",startedAt:f.startedAt})),a=o.filter(f=>f.status==="dead").map(f=>f.pid),c=!Object.keys(process.env).some(f=>UR.has(f)||zR.some(h=>f.startsWith(h))),u=Date.now()-this.startTime,l=Math.floor(u/1e3),d=Math.floor(l/3600),p=Math.floor(l%3600/60),m=d>0?`${d}h ${p}m`:`${p}m`;r.json({supervisor:{running:!0,pid:process.pid,uptime:m},processes:o,health:{deadProcessPids:a,envClean:c}})})}extractInstructionSection(e,r){let i={workflow:this.extractBetween(e,"## The Workflow","## Search Parameters"),search_params:this.extractBetween(e,"## Search Parameters","## Examples"),examples:this.extractBetween(e,"## Examples","## Why This Workflow"),all:e};return i[r]||i.all}extractBetween(e,r,i){let n=e.indexOf(r),s=e.indexOf(i);return n===-1?e:s===-1?e.substring(n):e.substring(n,s).trim()}};var qt=Oe(require("path"),1),kh=require("os"),or=require("fs"),VB=require("child_process"),KB=require("util");oe();hn();Tt();var Xi=require("fs"),Eh=require("path");oe();function ZB(t){try{return(0,Xi.existsSync)(t)?JSON.parse((0,Xi.readFileSync)(t,"utf-8")):{}}catch(e){return g.error("CONFIG","Failed to read Cursor registry, using empty registry",{file:t,error:e instanceof Error?e.message:String(e)}),{}}}function GB(t,e){let r=(0,Eh.join)(t,"..");(0,Xi.mkdirSync)(r,{recursive:!0}),(0,Xi.writeFileSync)(t,JSON.stringify(e,null,2))}function LA(t,e){let r=(0,Eh.join)(t,".cursor","rules"),i=(0,Eh.join)(r,"claude-mem-context.mdc"),n=`${i}.tmp`;(0,Xi.mkdirSync)(r,{recursive:!0});let s=`---
1051
1063
  alwaysApply: true
1052
1064
  description: "Claude-mem context from past sessions (auto-updated)"
1053
1065
  ---
@@ -1709,7 +1721,7 @@ ${s.formatTableHeader()}`,m=d.map((f,h)=>s.formatObservationIndex(f,h));i.json({
1709
1721
  `);xr(zi);let n=this.findClaudeExecutable(),s=pi(Zf()),o=Ng({prompt:i,options:{model:this.getModelId(),cwd:zi,disallowedTools:Qee,pathToClaudeCodeExecutable:n,env:s,mcpServers:{}}}),a;try{for await(let c of o)c.session_id&&(a=c.session_id),c.type==="result"&&g.info("WORKER",`Knowledge agent primed for corpus "${e.name}"`)}catch(c){if(a)c instanceof Error?g.debug("WORKER",`SDK process exited after priming corpus "${e.name}" \u2014 session captured, continuing`,{},c):g.debug("WORKER",`SDK process exited after priming corpus "${e.name}" \u2014 session captured, continuing (non-Error thrown)`,{thrownValue:String(c)});else throw c}if(!a)throw new Error(`Failed to capture session_id while priming corpus "${e.name}"`);return e.session_id=a,this.corpusStore.write(e),a}async query(e,r){if(!e.session_id)throw new Error(`Corpus "${e.name}" has no session \u2014 call prime first`);try{let i=await this.executeQuery(e,r);return i.session_id!==e.session_id&&(e.session_id=i.session_id,this.corpusStore.write(e)),i}catch(i){if(!this.isSessionResumeError(i))throw i instanceof Error?g.error("WORKER",`Query failed for corpus "${e.name}"`,{},i):g.error("WORKER",`Query failed for corpus "${e.name}" (non-Error thrown)`,{thrownValue:String(i)}),i;g.info("WORKER",`Session expired for corpus "${e.name}", auto-repriming...`),await this.prime(e);let n=this.corpusStore.read(e.name);if(!n||!n.session_id)throw new Error(`Auto-reprime failed for corpus "${e.name}"`);let s=await this.executeQuery(n,r);return s.session_id!==n.session_id&&(n.session_id=s.session_id,this.corpusStore.write(n)),s}}async reprime(e){return e.session_id=null,this.prime(e)}isSessionResumeError(e){let r=e instanceof Error?e.message:String(e);return/session|resume|expired|invalid.*session|not found/i.test(r)}async executeQuery(e,r){xr(zi);let i=this.findClaudeExecutable(),n=pi(Zf()),s=Ng({prompt:r,options:{model:this.getModelId(),resume:e.session_id,cwd:zi,disallowedTools:Qee,pathToClaudeCodeExecutable:i,env:n,mcpServers:{}}}),o="",a=e.session_id;try{for await(let c of s)c.session_id&&(a=c.session_id),c.type==="assistant"&&(o=c.message.content.filter(l=>l.type==="text").map(l=>l.text).join(""))}catch(c){if(o)c instanceof Error?g.debug("WORKER","SDK process exited after query \u2014 answer captured, continuing",{},c):g.debug("WORKER","SDK process exited after query \u2014 answer captured, continuing (non-Error thrown)",{thrownValue:String(c)});else throw c}return{answer:o,session_id:a}}getModelId(){return ke.loadFromFile(kt).CLAUDE_MEM_MODEL}findClaudeExecutable(){let e=ke.loadFromFile(kt);if(e.CLAUDE_CODE_PATH){let{existsSync:r}=require("fs");if(!r(e.CLAUDE_CODE_PATH))throw new Error(`CLAUDE_CODE_PATH is set to "${e.CLAUDE_CODE_PATH}" but the file does not exist.`);return e.CLAUDE_CODE_PATH}if(process.platform==="win32")try{return(0,tj.execSync)("where claude.cmd",{encoding:"utf8",windowsHide:!0,stdio:["ignore","pipe","ignore"]}),"claude.cmd"}catch{}try{let r=(0,tj.execSync)(process.platform==="win32"?"where claude":"which claude",{encoding:"utf8",windowsHide:!0,stdio:["ignore","pipe","ignore"]}).trim().split(`
1710
1722
  `)[0].trim();if(r)return r}catch(r){r instanceof Error?g.debug("WORKER","Claude executable auto-detection failed",{},r):g.debug("WORKER","Claude executable auto-detection failed (non-Error thrown)",{thrownValue:String(r)})}throw new Error(`Claude executable not found. Please either:
1711
1723
  1. Add "claude" to your system PATH, or
1712
- 2. Set CLAUDE_CODE_PATH in ~/.claude-mem/settings.json`)}};Qs();var tje={},JDe="12.4.4";function Rte(t,e){return{continue:!0,suppressOutput:!0,status:t,...e&&{message:e}}}var JE=class t{server;startTime=Date.now();mcpClient;mcpReady=!1;initializationCompleteFlag=!1;isShuttingDown=!1;dbManager;sessionManager;sseBroadcaster;sdkAgent;geminiAgent;openRouterAgent;paginationHelper;settingsManager;sessionEventBroadcaster;completionHandler;corpusStore;searchRoutes=null;chromaMcpManager=null;transcriptWatcher=null;initializationComplete;resolveInitialization;lastAiInteraction=null;constructor(){this.initializationComplete=new Promise(e=>{this.resolveInitialization=e}),this.dbManager=new q0,this.sessionManager=new Z0(this.dbManager),this.sseBroadcaster=new G0,this.sdkAgent=new qx(this.dbManager,this.sessionManager),this.geminiAgent=new Wx(this.dbManager,this.sessionManager),this.openRouterAgent=new Zx(this.dbManager,this.sessionManager),this.paginationHelper=new Gx(this.dbManager),this.settingsManager=new Vx(this.dbManager),this.sessionEventBroadcaster=new Qx(this.sseBroadcaster,this),this.completionHandler=new eE(this.sessionManager,this.sessionEventBroadcaster,this.dbManager),this.corpusStore=new ZE,vZ({sessionManager:this.sessionManager,dbManager:this.dbManager,eventBroadcaster:this.sessionEventBroadcaster}),this.sessionManager.setOnSessionDeleted(()=>{this.broadcastProcessingStatus()}),this.mcpClient=new Kl({name:"worker-search-proxy",version:JDe},{capabilities:{}}),this.server=new z0({getInitializationComplete:()=>this.initializationCompleteFlag,getMcpReady:()=>this.mcpReady,onShutdown:()=>this.shutdown(),onRestart:()=>this.shutdown(),workerPath:__filename,getAiStatus:()=>{let e="claude";return yp()&&Tu()?e="openrouter":vp()&&ku()&&(e="gemini"),{provider:e,authMethod:vS(),lastInteraction:this.lastAiInteraction?{timestamp:this.lastAiInteraction.timestamp,success:this.lastAiInteraction.success,...this.lastAiInteraction.error&&{error:this.lastAiInteraction.error}}:null}}}),this.registerRoutes(),this.registerSignalHandlers()}registerSignalHandlers(){tq(async()=>{this.isShuttingDown=!0,await this.shutdown()})}registerRoutes(){this.server.registerRoutes(new BE),this.server.app.get("/api/context/inject",async(r,i,n)=>{if(!this.initializationCompleteFlag||!this.searchRoutes){g.warn("SYSTEM","Context requested before initialization complete, returning empty"),i.status(200).json({content:[{type:"text",text:""}]});return}n()}),this.server.app.use("/api",async(r,i,n)=>{if(r.path==="/chroma/status"||r.path==="/health"||r.path==="/readiness"||r.path==="/version"){n();return}if(this.initializationCompleteFlag){n();return}let s=12e4,o=new Promise((a,c)=>setTimeout(()=>c(new Error("Database initialization timeout")),s));try{await Promise.race([this.initializationComplete,o]),n()}catch(a){a instanceof Error?g.error("WORKER",`Request to ${r.method} ${r.path} rejected \u2014 DB not initialized`,{},a):g.error("WORKER",`Request to ${r.method} ${r.path} rejected \u2014 DB not initialized with non-Error`,{},new Error(String(a))),i.status(503).json({error:"Service initializing",message:"Database is still initializing, please retry"});return}}),this.server.registerRoutes(new RE(this.sseBroadcaster,this.dbManager,this.sessionManager));let e=new PE(this.sessionManager,this.dbManager,this.sdkAgent,this.geminiAgent,this.openRouterAgent,this.sessionEventBroadcaster,this,this.completionHandler);this.server.registerRoutes(e),yZ((r,i)=>e.ensureGeneratorRunning(r,i)),this.server.registerRoutes(new AE(this.paginationHelper,this.dbManager,this.sessionManager,this.sseBroadcaster,this,this.startTime)),this.server.registerRoutes(new LE(this.settingsManager)),this.server.registerRoutes(new qE),this.server.registerRoutes(new WE(this.dbManager,"claude-mem"))}async start(){let e=On(),r=VR();await eq(),await this.server.listen(e,r),Iq({pid:process.pid,port:e,startedAt:new Date().toISOString()}),Xr().registerProcess("worker",{pid:process.pid,type:"worker",startedAt:new Date().toISOString()}),g.info("SYSTEM","Worker started",{host:r,port:e,pid:process.pid}),this.initializeBackground().catch(i=>{g.error("SYSTEM","Background initialization failed",{},i)})}async initializeBackground(){try{g.info("WORKER","Background initialization starting..."),await Oq();let{ModeManager:e}=await Promise.resolve().then(()=>(Pn(),aZ)),{SettingsDefaultsManager:r}=await Promise.resolve().then(()=>(sr(),OF)),{USER_SETTINGS_PATH:i}=await Promise.resolve().then(()=>(Tt(),$F)),n=r.loadFromFile(i),s=n.CLAUDE_MEM_MODE;e.getInstance().loadMode(s),g.info("SYSTEM",`Mode loaded: ${s}`),(n.CLAUDE_MEM_MODE==="local"||!n.CLAUDE_MEM_MODE)&&(g.info("WORKER","Checking for one-time Chroma migration..."),Rq()),g.info("WORKER","Checking for one-time CWD remap..."),Cq(),g.info("WORKER","Adopting merged worktrees (background)..."),Gq({}).then(h=>{if(h)for(let v of h)(v.adoptedObservations>0||v.adoptedSummaries>0||v.chromaUpdates>0)&&g.info("SYSTEM","Merged worktrees adopted in background",v),v.errors.length>0&&g.warn("SYSTEM","Worktree adoption had per-branch errors",{repoPath:v.repoPath,errors:v.errors})}).catch(h=>{g.error("WORKER","Worktree adoption failed (background)",{},h instanceof Error?h:new Error(String(h)))}),n.CLAUDE_MEM_CHROMA_ENABLED!=="false"?(this.chromaMcpManager=Fi.getInstance(),g.info("SYSTEM","ChromaMcpManager initialized (lazy - connects on first use)")):g.info("SYSTEM","Chroma disabled via CLAUDE_MEM_CHROMA_ENABLED=false, skipping ChromaMcpManager"),g.info("WORKER","Initializing database manager..."),await this.dbManager.initialize();try{g.info("WORKER","Running startup GC for pending messages...");let{PendingMessageStore:h}=await Promise.resolve().then(()=>(W0(),GA)),y=new h(this.dbManager.getSessionStore().db,3).clearFailedOlderThan(10080*60*1e3);y>0&&g.info("QUEUE","Startup GC cleared old failed pending_messages rows",{cleared:y})}catch(h){g.warn("QUEUE","Startup GC for failed pending_messages rows failed",{},h instanceof Error?h:void 0)}Uq(),g.info("WORKER","Initializing search services...");let a=new Jx,c=new Xx,u=new Yx(this.dbManager.getSessionSearch(),this.dbManager.getSessionStore(),this.dbManager.getChromaSync(),a,c);this.searchRoutes=new jE(u),this.server.registerRoutes(this.searchRoutes),g.info("WORKER","SearchManager initialized and search routes registered");let{SearchOrchestrator:l}=await Promise.resolve().then(()=>(gD(),kX)),d=new l(this.dbManager.getSessionSearch(),this.dbManager.getSessionStore(),this.dbManager.getChromaSync()),p=new VE(this.dbManager.getSessionStore(),d,this.corpusStore),m=new KE(this.corpusStore);this.server.registerRoutes(new HE(this.corpusStore,p,m)),g.info("WORKER","CorpusRoutes registered"),this.initializationCompleteFlag=!0,this.resolveInitialization(),g.info("SYSTEM","Core initialization complete (DB + search ready)"),await this.startTranscriptWatcher(n),this.chromaMcpManager&&Fa.backfillAllProjects(this.dbManager.getSessionStore()).then(()=>{g.info("CHROMA_SYNC","Backfill check complete for all projects")}).catch(h=>{g.error("CHROMA_SYNC","Backfill failed (non-blocking)",{},h)});let f=Ote.default.join(__dirname,"mcp-server.cjs");this.mcpReady=(0,gj.existsSync)(f),this.runMcpSelfCheck(f).catch(h=>{g.debug("WORKER","MCP self-check failed (non-fatal)",{error:h.message})});return}catch(e){g.error("SYSTEM","Background initialization failed",{},e instanceof Error?e:void 0)}}async runMcpSelfCheck(e){try{Xr().assertCanSpawn("mcp server");let r=new Xl({command:process.execPath,args:[e],env:Object.fromEntries(Object.entries(pi(process.env)).filter(([,o])=>o!==void 0))}),i=6e4,n=this.mcpClient.connect(r),s=new Promise((o,a)=>{setTimeout(()=>a(new Error("MCP connection timeout")),6e4)});await Promise.race([n,s]),g.info("WORKER","MCP loopback self-check connected successfully"),await r.close()}catch(r){g.warn("WORKER","MCP loopback self-check failed",{error:r instanceof Error?r.message:String(r)})}}async startTranscriptWatcher(e){if(!(e.CLAUDE_MEM_TRANSCRIPTS_ENABLED!=="false")){g.info("TRANSCRIPT","Transcript watcher disabled via CLAUDE_MEM_TRANSCRIPTS_ENABLED=false");return}let i=e.CLAUDE_MEM_TRANSCRIPTS_CONFIG_PATH||rE,n=ra(i);(0,gj.existsSync)(n)||(IX(i),g.info("TRANSCRIPT","Created default transcript watch config",{configPath:n}));let s=TX(i),o=ra(s.stateFile??nE);try{this.transcriptWatcher=new TE(s,o),await this.transcriptWatcher.start()}catch(a){this.transcriptWatcher?.stop(),this.transcriptWatcher=null,a instanceof Error?g.error("WORKER","Failed to start transcript watcher (continuing without Codex ingestion)",{configPath:n},a):g.error("WORKER","Failed to start transcript watcher with non-Error (continuing without Codex ingestion)",{configPath:n},new Error(String(a)));return}g.info("TRANSCRIPT","Transcript watcher started",{configPath:n,statePath:o,watches:s.watches.length})}getActiveAgent(){return yp()&&Tu()?this.openRouterAgent:vp()&&ku()?this.geminiAgent:this.sdkAgent}startSessionProcessor(e,r){if(!e)return;let i=e.sessionDbId,n=this.getActiveAgent(),s=n.constructor.name;e.abortController.signal.aborted&&(g.debug("SYSTEM","Replacing aborted AbortController before starting generator",{sessionId:e.sessionDbId}),e.abortController=new AbortController);let o=!1,a=!1;g.info("SYSTEM",`Starting generator (${r}) using ${s}`,{sessionId:i}),e.lastGeneratorActivity=Date.now(),e.generatorPromise=n.startSession(e,this).catch(async c=>{let u=c?.message||"";if(["Claude executable not found","CLAUDE_CODE_PATH","ENOENT","spawn","Invalid API key","API_KEY_INVALID","API key expired","API key not valid","PERMISSION_DENIED","Gemini API error: 400","Gemini API error: 401","Gemini API error: 403","FOREIGN KEY constraint failed"].some(p=>u.includes(p))){o=!0,this.lastAiInteraction={timestamp:Date.now(),success:!1,provider:s,error:u},g.error("SDK","Unrecoverable generator error - will NOT restart",{sessionId:e.sessionDbId,project:e.project,errorMessage:u});return}if(this.isSessionTerminatedError(c))return g.warn("SDK","SDK resume failed, falling back to standalone processing",{sessionId:e.sessionDbId,project:e.project,reason:c instanceof Error?c.message:String(c)}),this.runFallbackForTerminatedSession(e,c);throw["aborted by user","No conversation found"].some(p=>u.includes(p))&&e.memorySessionId&&(g.warn("SDK","Detected stale resume failure, clearing memorySessionId for fresh start",{sessionId:e.sessionDbId,memorySessionId:e.memorySessionId,errorMessage:u}),this.dbManager.getSessionStore().updateMemorySessionId(e.sessionDbId,null),e.memorySessionId=null,e.forceInit=!0),g.error("SDK","Session generator failed",{sessionId:e.sessionDbId,project:e.project,provider:s},c),a=!0,this.lastAiInteraction={timestamp:Date.now(),success:!1,provider:s,error:u},c}).finally(async()=>{let c=ja(e.sessionDbId);if(c&&c.process.exitCode===null&&await za(c,5e3),e.generatorPromise=null,!a&&!o&&(this.lastAiInteraction={timestamp:Date.now(),success:!0,provider:s}),o){this.terminateSession(e.sessionDbId,"unrecoverable_error");return}let l=this.sessionManager.getPendingMessageStore().getPendingCount(e.sessionDbId);if(e.idleTimedOut&&(e.idleTimedOut=!1,l===0)){this.terminateSession(e.sessionDbId,"idle_timeout");return}if(l>0){e.restartGuard||(e.restartGuard=new Wa);let d=e.restartGuard.recordRestart();if(e.consecutiveRestarts=(e.consecutiveRestarts||0)+1,!d){g.error("SYSTEM","Restart guard tripped: session is dead, terminating",{sessionId:e.sessionDbId,pendingCount:l,restartsInWindow:e.restartGuard.restartsInWindow,windowMs:e.restartGuard.windowMs,maxRestarts:e.restartGuard.maxRestarts,consecutiveFailures:e.restartGuard.consecutiveFailuresSinceSuccess,maxConsecutiveFailures:e.restartGuard.maxConsecutiveFailures}),e.consecutiveRestarts=0,this.terminateSession(e.sessionDbId,"max_restarts_exceeded");return}g.info("SYSTEM","Pending work remains after generator exit, restarting with fresh AbortController",{sessionId:e.sessionDbId,pendingCount:l,attempt:e.consecutiveRestarts}),e.abortController=new AbortController,this.startSessionProcessor(e,"pending-work-restart"),this.broadcastProcessingStatus()}else e.restartGuard?.recordSuccess(),e.consecutiveRestarts=0,this.completionHandler.finalizeSession(e.sessionDbId),this.sessionManager.removeSessionImmediate(e.sessionDbId)})}static SESSION_TERMINATED_PATTERNS=["process aborted by user","processtransport","not ready for writing","session generator failed","claude code process"];isSessionTerminatedError(e){let i=(e instanceof Error?e.message:String(e)).toLowerCase();return t.SESSION_TERMINATED_PATTERNS.some(n=>i.includes(n))}async runFallbackForTerminatedSession(e,r){if(!e)return;let i=e.sessionDbId;if(!e.memorySessionId){let n=`fallback-${i}-${Date.now()}`;e.memorySessionId=n,this.dbManager.getSessionStore().updateMemorySessionId(i,n)}if(ku())try{await this.geminiAgent.startSession(e,this);return}catch(n){n instanceof Error?(g.warn("WORKER","Fallback Gemini failed, trying OpenRouter",{sessionId:i}),g.error("WORKER","Gemini fallback error detail",{sessionId:i},n)):g.error("WORKER","Gemini fallback failed with non-Error",{sessionId:i},new Error(String(n)))}if(Tu())try{await this.openRouterAgent.startSession(e,this);return}catch(n){n instanceof Error?g.error("WORKER","Fallback OpenRouter failed, will abandon messages",{sessionId:i},n):g.error("WORKER","Fallback OpenRouter failed with non-Error, will abandon messages",{sessionId:i},new Error(String(n)))}this.completionHandler.finalizeSession(i),this.sessionManager.removeSessionImmediate(i)}terminateSession(e,r){g.info("SYSTEM","Session terminated",{sessionId:e,reason:r}),this.completionHandler.finalizeSession(e),this.sessionManager.removeSessionImmediate(e)}async processPendingQueues(e=10){let{PendingMessageStore:r}=await Promise.resolve().then(()=>(W0(),GA)),i=new r(this.dbManager.getSessionStore().db,3),n=this.dbManager.getSessionStore(),s=360*60*1e3,o=Date.now()-s,a=n.db.prepare(`
1724
+ 2. Set CLAUDE_CODE_PATH in ~/.claude-mem/settings.json`)}};Qs();var tje={},JDe="12.4.5";function Rte(t,e){return{continue:!0,suppressOutput:!0,status:t,...e&&{message:e}}}var JE=class t{server;startTime=Date.now();mcpClient;mcpReady=!1;initializationCompleteFlag=!1;isShuttingDown=!1;dbManager;sessionManager;sseBroadcaster;sdkAgent;geminiAgent;openRouterAgent;paginationHelper;settingsManager;sessionEventBroadcaster;completionHandler;corpusStore;searchRoutes=null;chromaMcpManager=null;transcriptWatcher=null;initializationComplete;resolveInitialization;lastAiInteraction=null;constructor(){this.initializationComplete=new Promise(e=>{this.resolveInitialization=e}),this.dbManager=new q0,this.sessionManager=new Z0(this.dbManager),this.sseBroadcaster=new G0,this.sdkAgent=new qx(this.dbManager,this.sessionManager),this.geminiAgent=new Wx(this.dbManager,this.sessionManager),this.openRouterAgent=new Zx(this.dbManager,this.sessionManager),this.paginationHelper=new Gx(this.dbManager),this.settingsManager=new Vx(this.dbManager),this.sessionEventBroadcaster=new Qx(this.sseBroadcaster,this),this.completionHandler=new eE(this.sessionManager,this.sessionEventBroadcaster,this.dbManager),this.corpusStore=new ZE,vZ({sessionManager:this.sessionManager,dbManager:this.dbManager,eventBroadcaster:this.sessionEventBroadcaster}),this.sessionManager.setOnSessionDeleted(()=>{this.broadcastProcessingStatus()}),this.mcpClient=new Kl({name:"worker-search-proxy",version:JDe},{capabilities:{}}),this.server=new z0({getInitializationComplete:()=>this.initializationCompleteFlag,getMcpReady:()=>this.mcpReady,onShutdown:()=>this.shutdown(),onRestart:()=>this.shutdown(),workerPath:__filename,getAiStatus:()=>{let e="claude";return yp()&&Tu()?e="openrouter":vp()&&ku()&&(e="gemini"),{provider:e,authMethod:vS(),lastInteraction:this.lastAiInteraction?{timestamp:this.lastAiInteraction.timestamp,success:this.lastAiInteraction.success,...this.lastAiInteraction.error&&{error:this.lastAiInteraction.error}}:null}}}),this.registerRoutes(),this.registerSignalHandlers()}registerSignalHandlers(){tq(async()=>{this.isShuttingDown=!0,await this.shutdown()})}registerRoutes(){this.server.registerRoutes(new BE),this.server.app.get("/api/context/inject",async(r,i,n)=>{if(!this.initializationCompleteFlag||!this.searchRoutes){g.warn("SYSTEM","Context requested before initialization complete, returning empty"),i.status(200).json({content:[{type:"text",text:""}]});return}n()}),this.server.app.use("/api",async(r,i,n)=>{if(r.path==="/chroma/status"||r.path==="/health"||r.path==="/readiness"||r.path==="/version"){n();return}if(this.initializationCompleteFlag){n();return}let s=12e4,o=new Promise((a,c)=>setTimeout(()=>c(new Error("Database initialization timeout")),s));try{await Promise.race([this.initializationComplete,o]),n()}catch(a){a instanceof Error?g.error("WORKER",`Request to ${r.method} ${r.path} rejected \u2014 DB not initialized`,{},a):g.error("WORKER",`Request to ${r.method} ${r.path} rejected \u2014 DB not initialized with non-Error`,{},new Error(String(a))),i.status(503).json({error:"Service initializing",message:"Database is still initializing, please retry"});return}}),this.server.registerRoutes(new RE(this.sseBroadcaster,this.dbManager,this.sessionManager));let e=new PE(this.sessionManager,this.dbManager,this.sdkAgent,this.geminiAgent,this.openRouterAgent,this.sessionEventBroadcaster,this,this.completionHandler);this.server.registerRoutes(e),yZ((r,i)=>e.ensureGeneratorRunning(r,i)),this.server.registerRoutes(new AE(this.paginationHelper,this.dbManager,this.sessionManager,this.sseBroadcaster,this,this.startTime)),this.server.registerRoutes(new LE(this.settingsManager)),this.server.registerRoutes(new qE),this.server.registerRoutes(new WE(this.dbManager,"claude-mem"))}async start(){let e=On(),r=VR();await eq(),await this.server.listen(e,r),Iq({pid:process.pid,port:e,startedAt:new Date().toISOString()}),Xr().registerProcess("worker",{pid:process.pid,type:"worker",startedAt:new Date().toISOString()}),g.info("SYSTEM","Worker started",{host:r,port:e,pid:process.pid}),this.initializeBackground().catch(i=>{g.error("SYSTEM","Background initialization failed",{},i)})}async initializeBackground(){try{g.info("WORKER","Background initialization starting..."),await Oq();let{ModeManager:e}=await Promise.resolve().then(()=>(Pn(),aZ)),{SettingsDefaultsManager:r}=await Promise.resolve().then(()=>(sr(),OF)),{USER_SETTINGS_PATH:i}=await Promise.resolve().then(()=>(Tt(),$F)),n=r.loadFromFile(i),s=n.CLAUDE_MEM_MODE;e.getInstance().loadMode(s),g.info("SYSTEM",`Mode loaded: ${s}`),(n.CLAUDE_MEM_MODE==="local"||!n.CLAUDE_MEM_MODE)&&(g.info("WORKER","Checking for one-time Chroma migration..."),Rq()),g.info("WORKER","Checking for one-time CWD remap..."),Cq(),g.info("WORKER","Adopting merged worktrees (background)..."),Gq({}).then(h=>{if(h)for(let v of h)(v.adoptedObservations>0||v.adoptedSummaries>0||v.chromaUpdates>0)&&g.info("SYSTEM","Merged worktrees adopted in background",v),v.errors.length>0&&g.warn("SYSTEM","Worktree adoption had per-branch errors",{repoPath:v.repoPath,errors:v.errors})}).catch(h=>{g.error("WORKER","Worktree adoption failed (background)",{},h instanceof Error?h:new Error(String(h)))}),n.CLAUDE_MEM_CHROMA_ENABLED!=="false"?(this.chromaMcpManager=Fi.getInstance(),g.info("SYSTEM","ChromaMcpManager initialized (lazy - connects on first use)")):g.info("SYSTEM","Chroma disabled via CLAUDE_MEM_CHROMA_ENABLED=false, skipping ChromaMcpManager"),g.info("WORKER","Initializing database manager..."),await this.dbManager.initialize();try{g.info("WORKER","Running startup GC for pending messages...");let{PendingMessageStore:h}=await Promise.resolve().then(()=>(W0(),GA)),y=new h(this.dbManager.getSessionStore().db,3).clearFailedOlderThan(10080*60*1e3);y>0&&g.info("QUEUE","Startup GC cleared old failed pending_messages rows",{cleared:y})}catch(h){g.warn("QUEUE","Startup GC for failed pending_messages rows failed",{},h instanceof Error?h:void 0)}Uq(),g.info("WORKER","Initializing search services...");let a=new Jx,c=new Xx,u=new Yx(this.dbManager.getSessionSearch(),this.dbManager.getSessionStore(),this.dbManager.getChromaSync(),a,c);this.searchRoutes=new jE(u),this.server.registerRoutes(this.searchRoutes),g.info("WORKER","SearchManager initialized and search routes registered");let{SearchOrchestrator:l}=await Promise.resolve().then(()=>(gD(),kX)),d=new l(this.dbManager.getSessionSearch(),this.dbManager.getSessionStore(),this.dbManager.getChromaSync()),p=new VE(this.dbManager.getSessionStore(),d,this.corpusStore),m=new KE(this.corpusStore);this.server.registerRoutes(new HE(this.corpusStore,p,m)),g.info("WORKER","CorpusRoutes registered"),this.initializationCompleteFlag=!0,this.resolveInitialization(),g.info("SYSTEM","Core initialization complete (DB + search ready)"),await this.startTranscriptWatcher(n),this.chromaMcpManager&&Fa.backfillAllProjects(this.dbManager.getSessionStore()).then(()=>{g.info("CHROMA_SYNC","Backfill check complete for all projects")}).catch(h=>{g.error("CHROMA_SYNC","Backfill failed (non-blocking)",{},h)});let f=Ote.default.join(__dirname,"mcp-server.cjs");this.mcpReady=(0,gj.existsSync)(f),this.runMcpSelfCheck(f).catch(h=>{g.debug("WORKER","MCP self-check failed (non-fatal)",{error:h.message})});return}catch(e){g.error("SYSTEM","Background initialization failed",{},e instanceof Error?e:void 0)}}async runMcpSelfCheck(e){try{Xr().assertCanSpawn("mcp server");let r=new Xl({command:process.execPath,args:[e],env:Object.fromEntries(Object.entries(pi(process.env)).filter(([,o])=>o!==void 0))}),i=6e4,n=this.mcpClient.connect(r),s=new Promise((o,a)=>{setTimeout(()=>a(new Error("MCP connection timeout")),6e4)});await Promise.race([n,s]),g.info("WORKER","MCP loopback self-check connected successfully"),await r.close()}catch(r){g.warn("WORKER","MCP loopback self-check failed",{error:r instanceof Error?r.message:String(r)})}}async startTranscriptWatcher(e){if(!(e.CLAUDE_MEM_TRANSCRIPTS_ENABLED!=="false")){g.info("TRANSCRIPT","Transcript watcher disabled via CLAUDE_MEM_TRANSCRIPTS_ENABLED=false");return}let i=e.CLAUDE_MEM_TRANSCRIPTS_CONFIG_PATH||rE,n=ra(i);(0,gj.existsSync)(n)||(IX(i),g.info("TRANSCRIPT","Created default transcript watch config",{configPath:n}));let s=TX(i),o=ra(s.stateFile??nE);try{this.transcriptWatcher=new TE(s,o),await this.transcriptWatcher.start()}catch(a){this.transcriptWatcher?.stop(),this.transcriptWatcher=null,a instanceof Error?g.error("WORKER","Failed to start transcript watcher (continuing without Codex ingestion)",{configPath:n},a):g.error("WORKER","Failed to start transcript watcher with non-Error (continuing without Codex ingestion)",{configPath:n},new Error(String(a)));return}g.info("TRANSCRIPT","Transcript watcher started",{configPath:n,statePath:o,watches:s.watches.length})}getActiveAgent(){return yp()&&Tu()?this.openRouterAgent:vp()&&ku()?this.geminiAgent:this.sdkAgent}startSessionProcessor(e,r){if(!e)return;let i=e.sessionDbId,n=this.getActiveAgent(),s=n.constructor.name;e.abortController.signal.aborted&&(g.debug("SYSTEM","Replacing aborted AbortController before starting generator",{sessionId:e.sessionDbId}),e.abortController=new AbortController);let o=!1,a=!1;g.info("SYSTEM",`Starting generator (${r}) using ${s}`,{sessionId:i}),e.lastGeneratorActivity=Date.now(),e.generatorPromise=n.startSession(e,this).catch(async c=>{let u=c?.message||"";if(["Claude executable not found","CLAUDE_CODE_PATH","ENOENT","spawn","Invalid API key","API_KEY_INVALID","API key expired","API key not valid","PERMISSION_DENIED","Gemini API error: 400","Gemini API error: 401","Gemini API error: 403","FOREIGN KEY constraint failed"].some(p=>u.includes(p))){o=!0,this.lastAiInteraction={timestamp:Date.now(),success:!1,provider:s,error:u},g.error("SDK","Unrecoverable generator error - will NOT restart",{sessionId:e.sessionDbId,project:e.project,errorMessage:u});return}if(this.isSessionTerminatedError(c))return g.warn("SDK","SDK resume failed, falling back to standalone processing",{sessionId:e.sessionDbId,project:e.project,reason:c instanceof Error?c.message:String(c)}),this.runFallbackForTerminatedSession(e,c);throw["aborted by user","No conversation found"].some(p=>u.includes(p))&&e.memorySessionId&&(g.warn("SDK","Detected stale resume failure, clearing memorySessionId for fresh start",{sessionId:e.sessionDbId,memorySessionId:e.memorySessionId,errorMessage:u}),this.dbManager.getSessionStore().updateMemorySessionId(e.sessionDbId,null),e.memorySessionId=null,e.forceInit=!0),g.error("SDK","Session generator failed",{sessionId:e.sessionDbId,project:e.project,provider:s},c),a=!0,this.lastAiInteraction={timestamp:Date.now(),success:!1,provider:s,error:u},c}).finally(async()=>{let c=ja(e.sessionDbId);if(c&&c.process.exitCode===null&&await za(c,5e3),e.generatorPromise=null,!a&&!o&&(this.lastAiInteraction={timestamp:Date.now(),success:!0,provider:s}),o){this.terminateSession(e.sessionDbId,"unrecoverable_error");return}let l=this.sessionManager.getPendingMessageStore().getPendingCount(e.sessionDbId);if(e.idleTimedOut&&(e.idleTimedOut=!1,l===0)){this.terminateSession(e.sessionDbId,"idle_timeout");return}if(l>0){e.restartGuard||(e.restartGuard=new Wa);let d=e.restartGuard.recordRestart();if(e.consecutiveRestarts=(e.consecutiveRestarts||0)+1,!d){g.error("SYSTEM","Restart guard tripped: session is dead, terminating",{sessionId:e.sessionDbId,pendingCount:l,restartsInWindow:e.restartGuard.restartsInWindow,windowMs:e.restartGuard.windowMs,maxRestarts:e.restartGuard.maxRestarts,consecutiveFailures:e.restartGuard.consecutiveFailuresSinceSuccess,maxConsecutiveFailures:e.restartGuard.maxConsecutiveFailures}),e.consecutiveRestarts=0,this.terminateSession(e.sessionDbId,"max_restarts_exceeded");return}g.info("SYSTEM","Pending work remains after generator exit, restarting with fresh AbortController",{sessionId:e.sessionDbId,pendingCount:l,attempt:e.consecutiveRestarts}),e.abortController=new AbortController,this.startSessionProcessor(e,"pending-work-restart"),this.broadcastProcessingStatus()}else e.restartGuard?.recordSuccess(),e.consecutiveRestarts=0,this.completionHandler.finalizeSession(e.sessionDbId),this.sessionManager.removeSessionImmediate(e.sessionDbId)})}static SESSION_TERMINATED_PATTERNS=["process aborted by user","processtransport","not ready for writing","session generator failed","claude code process"];isSessionTerminatedError(e){let i=(e instanceof Error?e.message:String(e)).toLowerCase();return t.SESSION_TERMINATED_PATTERNS.some(n=>i.includes(n))}async runFallbackForTerminatedSession(e,r){if(!e)return;let i=e.sessionDbId;if(!e.memorySessionId){let n=`fallback-${i}-${Date.now()}`;e.memorySessionId=n,this.dbManager.getSessionStore().updateMemorySessionId(i,n)}if(ku())try{await this.geminiAgent.startSession(e,this);return}catch(n){n instanceof Error?(g.warn("WORKER","Fallback Gemini failed, trying OpenRouter",{sessionId:i}),g.error("WORKER","Gemini fallback error detail",{sessionId:i},n)):g.error("WORKER","Gemini fallback failed with non-Error",{sessionId:i},new Error(String(n)))}if(Tu())try{await this.openRouterAgent.startSession(e,this);return}catch(n){n instanceof Error?g.error("WORKER","Fallback OpenRouter failed, will abandon messages",{sessionId:i},n):g.error("WORKER","Fallback OpenRouter failed with non-Error, will abandon messages",{sessionId:i},new Error(String(n)))}this.completionHandler.finalizeSession(i),this.sessionManager.removeSessionImmediate(i)}terminateSession(e,r){g.info("SYSTEM","Session terminated",{sessionId:e,reason:r}),this.completionHandler.finalizeSession(e),this.sessionManager.removeSessionImmediate(e)}async processPendingQueues(e=10){let{PendingMessageStore:r}=await Promise.resolve().then(()=>(W0(),GA)),i=new r(this.dbManager.getSessionStore().db,3),n=this.dbManager.getSessionStore(),s=360*60*1e3,o=Date.now()-s,a=n.db.prepare(`
1713
1725
  SELECT id FROM sdk_sessions
1714
1726
  WHERE status = 'active' AND started_at_epoch < ?
1715
1727
  `).all(o);if(a.length>0){let l=a.map(m=>m.id),d=l.map(()=>"?").join(","),p=Date.now();try{n.db.prepare(`