Refactor silent debugging to happy path error handling

- Replaced instances of silentDebug with happy_path_error__with_fallback across multiple files to improve error logging and handling.
- Updated the utility function to provide clearer semantics for error handling when expected values are missing.
- Introduced a script to find potential silent failures in the codebase that may need to be addressed with the new error handling approach.
This commit is contained in:
Alex Newman
2025-12-09 15:09:44 -05:00
parent d957bff495
commit c3761a2204
25 changed files with 293 additions and 204 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
"use strict";var Be=Object.create;var V=Object.defineProperty;var He=Object.getOwnPropertyDescriptor;var je=Object.getOwnPropertyNames;var Ge=Object.getPrototypeOf,Ye=Object.prototype.hasOwnProperty;var Ve=(a,e)=>{for(var s in e)V(a,s,{get:e[s],enumerable:!0})},Te=(a,e,s,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of je(e))!Ye.call(a,n)&&n!==s&&V(a,n,{get:()=>e[n],enumerable:!(t=He(e,n))||t.enumerable});return a};var ge=(a,e,s)=>(s=a!=null?Be(Ge(a)):{},Te(e||!a||!a.__esModule?V(s,"default",{value:a,enumerable:!0}):s,a)),Ke=a=>Te(V({},"__esModule",{value:!0}),a);var cs={};Ve(cs,{generateContext:()=>as});module.exports=Ke(cs);var U=ge(require("path"),1),ee=require("os"),P=require("fs");var Ce=ge(require("better-sqlite3"),1);var S=require("path"),oe=require("os"),Ne=require("fs");var Ae=require("url");var Oe=require("path"),Re=require("os"),K=require("fs");var Se=require("fs"),he=require("os"),be=require("path"),qe=(0,be.join)((0,he.homedir)(),".claude-mem","silent.log");function fe(a,e,s=""){let t=new Date().toISOString(),d=((new Error().stack||"").split(`
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),_=d?`${d[1].split("/").pop()}:${d[2]}`:"unknown",m=`[${t}] [${_}] ${a}`;if(e!==void 0)try{m+=` ${JSON.stringify(e)}`}catch(l){m+=` [stringify error: ${l}]`}m+=`
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),_=d?`${d[1].split("/").pop()}:${d[2]}`:"unknown",m=`[${t}] [HAPPY-PATH-ERROR] [${_}] ${a}`;if(e!==void 0)try{m+=` ${JSON.stringify(e)}`}catch(l){m+=` [stringify error: ${l}]`}m+=`
`;try{(0,Se.appendFileSync)(qe,m)}catch(l){console.error("[silent-debug] Failed to write to log:",l)}return s}var ne=(0,Oe.join)((0,Re.homedir)(),".claude-mem","settings.json");function q(a,e){try{if((0,K.existsSync)(ne)){let t=JSON.parse((0,K.readFileSync)(ne,"utf-8")).env?.[a];if(t!==void 0)return t}}catch(s){fe("Failed to load settings file",{error:s,settingsPath:ne,key:a})}return process.env[a]||e}var Qe={};function Je(){return typeof __dirname<"u"?__dirname:(0,S.dirname)((0,Ae.fileURLToPath)(Qe.url))}var Ss=Je(),C=q("CLAUDE_MEM_DATA_DIR",(0,S.join)((0,oe.homedir)(),".claude-mem")),ie=process.env.CLAUDE_CONFIG_DIR||(0,S.join)((0,oe.homedir)(),".claude"),hs=(0,S.join)(C,"archives"),bs=(0,S.join)(C,"logs"),fs=(0,S.join)(C,"trash"),Os=(0,S.join)(C,"backups"),Rs=(0,S.join)(C,"settings.json"),Ie=(0,S.join)(C,"claude-mem.db"),Ns=(0,S.join)(C,"vector-db"),As=(0,S.join)(ie,"settings.json"),Is=(0,S.join)(ie,"commands"),Ls=(0,S.join)(ie,"CLAUDE.md");function Le(a){(0,Ne.mkdirSync)(a,{recursive:!0})}var ae=(i=>(i[i.DEBUG=0]="DEBUG",i[i.INFO=1]="INFO",i[i.WARN=2]="WARN",i[i.ERROR=3]="ERROR",i[i.SILENT=4]="SILENT",i))(ae||{}),ce=class{level;useColor;constructor(){let e=q("CLAUDE_MEM_LOG_LEVEL","INFO").toUpperCase();this.level=ae[e]??1,this.useColor=process.stdout.isTTY??!1}correlationId(e,s){return`obs-${e}-${s}`}sessionId(e){return`session-${e}`}formatData(e){if(e==null)return"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return e.toString();if(typeof e=="object"){if(e instanceof Error)return this.level===0?`${e.message}
${e.stack}`:e.message;if(Array.isArray(e))return`[${e.length} items]`;let s=Object.keys(e);return s.length===0?"{}":s.length<=3?JSON.stringify(e):`{${s.length} keys: ${s.slice(0,3).join(", ")}...}`}return String(e)}formatTool(e,s){if(!s)return e;try{let t=typeof s=="string"?JSON.parse(s):s;if(e==="Bash"&&t.command){let n=t.command.length>50?t.command.substring(0,50)+"...":t.command;return`${e}(${n})`}if(e==="Read"&&t.file_path){let n=t.file_path.split("/").pop()||t.file_path;return`${e}(${n})`}if(e==="Edit"&&t.file_path){let n=t.file_path.split("/").pop()||t.file_path;return`${e}(${n})`}if(e==="Write"&&t.file_path){let n=t.file_path.split("/").pop()||t.file_path;return`${e}(${n})`}return e}catch{return e}}log(e,s,t,n,i){if(e<this.level)return;let c=new Date().toISOString().replace("T"," ").substring(0,23),d=ae[e].padEnd(5),_=s.padEnd(6),m="";n?.correlationId?m=`[${n.correlationId}] `:n?.sessionId&&(m=`[session-${n.sessionId}] `);let l="";i!=null&&(this.level===0&&typeof i=="object"?l=`
`+JSON.stringify(i,null,2):l=" "+this.formatData(i));let b="";if(n){let{sessionId:f,sdkSessionId:y,correlationId:E,...r}=n;Object.keys(r).length>0&&(b=` {${Object.entries(r).map(([O,h])=>`${O}=${h}`).join(", ")}}`)}let v=`[${c}] [${d}] [${_}] ${m}${t}${b}${l}`;e===3?console.error(v):console.log(v)}debug(e,s,t,n){this.log(0,e,s,t,n)}info(e,s,t,n){this.log(1,e,s,t,n)}warn(e,s,t,n){this.log(2,e,s,t,n)}error(e,s,t,n){this.log(3,e,s,t,n)}dataIn(e,s,t,n){this.info(e,`\u2192 ${s}`,t,n)}dataOut(e,s,t,n){this.info(e,`\u2190 ${s}`,t,n)}success(e,s,t,n){this.info(e,`\u2713 ${s}`,t,n)}failure(e,s,t,n){this.error(e,`\u2717 ${s}`,t,n)}timing(e,s,t,n){this.info(e,`\u23F1 ${s}`,n,{duration:`${t}ms`})}},B=new ce;var J=class{db;constructor(){Le(C),this.db=new Ce.default(Ie),this.db.pragma("journal_mode = WAL"),this.db.pragma("synchronous = NORMAL"),this.db.pragma("foreign_keys = ON"),this.initializeSchema(),this.ensureWorkerPortColumn(),this.ensurePromptTrackingColumns(),this.removeSessionSummariesUniqueConstraint(),this.addObservationHierarchicalFields(),this.makeObservationsTextNullable(),this.createUserPromptsTable(),this.ensureDiscoveryTokensColumn()}initializeSchema(){try{this.db.exec(`