fix: address PR review comments and add file read gate docs

Fix indentation bugs flagged in PR review (SettingsDefaultsManager,
MigrationRunner), add current date/time to file read gate timeline
so the model can judge observation recency, and add documentation
for the file read gate feature.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alex Newman
2026-04-07 13:09:46 -07:00
parent f4570f2a0a
commit c21e49d9fa
7 changed files with 197 additions and 5 deletions
+1 -1
View File
@@ -942,7 +942,7 @@ View Observations Live @ http://localhost:${i}`:void 0;return{hookSpecificOutput
`+String.fromCodePoint(128172)+` Community https://discord.gg/J4wttp9vDu
`+String.fromCodePoint(128250)+` Watch live in browser http://localhost:${r}/
`)}catch{}return{exitCode:it.SUCCESS}}}});function Nxe(t){return t.toLowerCase().replace(" am","a").replace(" pm","p")}function Mxe(t){return new Date(t).toLocaleString("en-US",{hour:"numeric",minute:"2-digit",hour12:!0})}function Dxe(t){return new Date(t).toLocaleString("en-US",{month:"short",day:"numeric",year:"numeric"})}function jxe(t,e,r){let n=new Set,i=[];for(let o of t){let a=o.memory_session_id??`no-session-${o.id}`;n.has(a)||(n.add(a),i.push(o))}let s=i.map(o=>{let a=Ca(o.files_read),c=Ca(o.files_modified),u=a.length+c.length,l=e.replace(/\\/g,"/"),d=c.some(f=>f.replace(/\\/g,"/")===l),p=0;return d&&(p+=2),u<=3?p+=2:u<=8&&(p+=1),{obs:o,specificityScore:p}});return s.sort((o,a)=>a.specificityScore-o.specificityScore),s.slice(0,r).map(o=>o.obs)}function zxe(t,e){let r=e.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\n/g,"\\n"),n=new Map;for(let o of t){let a=Dxe(o.created_at_epoch);n.has(a)||n.set(a,[]),n.get(a).push(o)}let i=Array.from(n.entries()).sort((o,a)=>{let c=Math.min(...o[1].map(l=>l.created_at_epoch)),u=Math.min(...a[1].map(l=>l.created_at_epoch));return c-u}),s=["Read blocked: This file has prior observations. Choose the cheapest path:","- **Already know enough?** The timeline below may be all you need (semantic priming).","- **Need details?** get_observations([IDs]) \u2014 ~300 tokens each.",`- **Need current code?** smart_outline("${r}") for structure (~1-2k tokens), smart_unfold("${r}", "<symbol>") for a specific function (~400-2k tokens).`,"- **Need to edit?** Use smart tools for line numbers, then sed via Bash (Edit requires Read, but you already have the context)."];for(let[o,a]of i){let c=[...a].sort((u,l)=>u.created_at_epoch-l.created_at_epoch);s.push(`### ${o}`);for(let u of c){let l=u.title||"Untitled",d=Axe[u.type]||"\u2753",p=Nxe(Mxe(u.created_at_epoch));s.push(`${u.id} ${p} ${d} ${l}`)}}return s.join(`
`)}catch{}return{exitCode:it.SUCCESS}}}});function Nxe(t){return t.toLowerCase().replace(" am","a").replace(" pm","p")}function Mxe(t){return new Date(t).toLocaleString("en-US",{hour:"numeric",minute:"2-digit",hour12:!0})}function Dxe(t){return new Date(t).toLocaleString("en-US",{month:"short",day:"numeric",year:"numeric"})}function jxe(t,e,r){let n=new Set,i=[];for(let o of t){let a=o.memory_session_id??`no-session-${o.id}`;n.has(a)||(n.add(a),i.push(o))}let s=i.map(o=>{let a=Ca(o.files_read),c=Ca(o.files_modified),u=a.length+c.length,l=e.replace(/\\/g,"/"),d=c.some(f=>f.replace(/\\/g,"/")===l),p=0;return d&&(p+=2),u<=3?p+=2:u<=8&&(p+=1),{obs:o,specificityScore:p}});return s.sort((o,a)=>a.specificityScore-o.specificityScore),s.slice(0,r).map(o=>o.obs)}function zxe(t,e){let r=e.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\n/g,"\\n"),n=new Map;for(let l of t){let d=Dxe(l.created_at_epoch);n.has(d)||n.set(d,[]),n.get(d).push(l)}let i=Array.from(n.entries()).sort((l,d)=>{let p=Math.min(...l[1].map(m=>m.created_at_epoch)),f=Math.min(...d[1].map(m=>m.created_at_epoch));return p-f}),s=new Date,o=s.toLocaleDateString("en-CA"),a=s.toLocaleTimeString("en-US",{hour:"numeric",minute:"2-digit",hour12:!0}).toLowerCase().replace(" ",""),c=s.toLocaleTimeString("en-US",{timeZoneName:"short"}).split(" ").pop(),u=[`Current: ${o} ${a} ${c}`,"Read blocked: This file has prior observations. Choose the cheapest path:","- **Already know enough?** The timeline below may be all you need (semantic priming).","- **Need details?** get_observations([IDs]) \u2014 ~300 tokens each.",`- **Need current code?** smart_outline("${r}") for structure (~1-2k tokens), smart_unfold("${r}", "<symbol>") for a specific function (~400-2k tokens).`,"- **Need to edit?** Use smart tools for line numbers, then sed via Bash (Edit requires Read, but you already have the context)."];for(let[l,d]of i){let p=[...d].sort((f,m)=>f.created_at_epoch-m.created_at_epoch);u.push(`### ${l}`);for(let f of p){let m=f.title||"Untitled",h=Axe[f.type]||"\u2753",g=Nxe(Mxe(f.created_at_epoch));u.push(`${f.id} ${g} ${h} ${m}`)}}return u.join(`
`)}var W5,Va,Oxe,Cxe,Pxe,Axe,LR,UR=Ee(()=>{"use strict";fr();te();Fs();W5=require("fs"),Va=Pe(require("path"),1);A_();tr();It();Fa();Oxe=1500,Cxe=40,Pxe=15,Axe={decision:"\u2696\uFE0F",bugfix:"\u{1F534}",feature:"\u{1F7E3}",refactor:"\u{1F504}",discovery:"\u{1F535}",change:"\u2705"};LR={async execute(t){let r=t.toolInput?.file_path;if(!r)return{continue:!0,suppressOutput:!0};try{let s=Va.default.isAbsolute(r)?r:Va.default.resolve(t.cwd||process.cwd(),r);if((0,W5.statSync)(s).size<Oxe)return{continue:!0,suppressOutput:!0}}catch(s){if(s.code==="ENOENT")return{continue:!0,suppressOutput:!0}}let n=ye.loadFromFile(vt);if(t.cwd&&gl(t.cwd,n.CLAUDE_MEM_EXCLUDED_PROJECTS))return _.debug("HOOK","Project excluded from tracking, skipping file context",{cwd:t.cwd}),{continue:!0,suppressOutput:!0};if(!await sr())return{continue:!0,suppressOutput:!0};try{let s=il(t.cwd),o=t.cwd||process.cwd(),a=Va.default.isAbsolute(r)?r:Va.default.resolve(o,r),c=Va.default.relative(o,a).split(Va.default.sep).join("/"),u=new URLSearchParams({path:c});s.allProjects.length>0&&u.set("projects",s.allProjects.join(",")),u.set("limit",String(Cxe));let l=await Qe(`/api/observations/by-file?${u.toString()}`,{method:"GET"});if(!l.ok)return _.warn("HOOK","File context query failed, skipping",{status:l.status,filePath:r}),{continue:!0,suppressOutput:!0};let d=await l.json();if(!d.observations||d.observations.length===0)return{continue:!0,suppressOutput:!0};let p=jxe(d.observations,c,Pxe);return p.length===0?{continue:!0,suppressOutput:!0}:{hookSpecificOutput:{hookEventName:"PreToolUse",additionalContext:"",permissionDecision:"deny",permissionDecisionReason:zxe(p,r)}}}catch(s){return _.warn("HOOK","File context fetch error, skipping",{error:s instanceof Error?s.message:String(s)}),{continue:!0,suppressOutput:!0}}}}});function V5(t){let e=Lxe[t];return e||(_.warn("HOOK",`Unknown event type: ${t}, returning no-op`),{async execute(){return{continue:!0,suppressOutput:!0,exitCode:it.SUCCESS}}})}var Lxe,K5=Ee(()=>{"use strict";On();te();NR();N_();M_();DR();zR();D_();UR();j_();NR();N_();M_();DR();zR();D_();UR();j_();Lxe={context:AR,"session-init":um,observation:lm,summarize:MR,"session-complete":pm,"user-message":jR,"file-edit":dm,"file-context":LR}});var X5={};Ln(X5,{hookCommand:()=>Uxe,isWorkerUnavailableError:()=>J5});function J5(t){let e=t instanceof Error?t.message:String(t),r=e.toLowerCase();return["econnrefused","econnreset","epipe","etimedout","enotfound","econnaborted","enetunreach","ehostunreach","fetch failed","unable to connect","socket hang up"].some(i=>r.includes(i))||r.includes("timed out")||r.includes("timeout")||/failed:\s*5\d{2}/.test(e)||/status[:\s]+5\d{2}/.test(e)||/failed:\s*429/.test(e)||/status[:\s]+429/.test(e)?!0:(/failed:\s*4\d{2}/.test(e)||/status[:\s]+4\d{2}/.test(e)||t instanceof TypeError||t instanceof ReferenceError||t instanceof SyntaxError,!1)}async function Uxe(t,e,r={}){let n=process.stderr.write.bind(process.stderr);process.stderr.write=(()=>!0);try{let i=q5(t),s=V5(e),o=await C5(),a=i.normalizeInput(o);a.platform=t;let c=await s.execute(a),u=i.formatOutput(c);console.log(JSON.stringify(u));let l=c.exitCode??it.SUCCESS;return r.skipExit||process.exit(l),l}catch(i){return J5(i)?(_.warn("HOOK",`Worker unavailable, skipping hook: ${i instanceof Error?i.message:i}`),r.skipExit||process.exit(it.SUCCESS),it.SUCCESS):(_.error("HOOK",`Hook error: ${i instanceof Error?i.message:i}`,{},i instanceof Error?i:void 0),r.skipExit||process.exit(it.BLOCKING_ERROR),it.BLOCKING_ERROR)}finally{process.stderr.write=n}}var Y5=Ee(()=>{"use strict";P5();H5();K5();On();te()});var qR={};Ln(qR,{cleanClaudeMd:()=>Qxe,generateClaudeMd:()=>Yxe});function Hxe(t){return qxe[t]||"\u{1F4DD}"}function Zxe(t){let e=(t.title?.length||0)+(t.subtitle?.length||0)+(t.narrative?.length||0)+(t.facts?.length||0);return Math.ceil(e/4)}function Bxe(t){let e=new Set;try{let n=(0,t3.execSync)("git ls-files",{cwd:t,encoding:"utf-8",maxBuffer:52428800}).trim().split(`
`).filter(i=>i);for(let i of n){let s=lr.default.join(t,i),o=lr.default.dirname(s);for(;o.length>t.length&&o.startsWith(t);)e.add(o),o=lr.default.dirname(o)}}catch(r){_.warn("CLAUDE_MD","git ls-files failed, falling back to directory walk",{error:String(r)}),r3(t,e)}return e}function r3(t,e,r=0){if(r>10)return;let n=["node_modules",".git",".next","dist","build",".cache","__pycache__",".venv","venv",".idea",".vscode","coverage",".claude-mem",".open-next",".turbo"];try{let i=(0,dr.readdirSync)(t,{withFileTypes:!0});for(let s of i){if(!s.isDirectory()||n.includes(s.name)||s.name.startsWith(".")&&s.name!==".claude")continue;let o=lr.default.join(t,s.name);e.add(o),r3(o,e,r+1)}}catch{}}function Gxe(t,e){let r=n=>{if(!n)return!1;try{let i=JSON.parse(n);if(Array.isArray(i))return i.some(s=>Ra(s,e))}catch{}return!1};return r(t.files_modified)||r(t.files_read)}function Wxe(t,e,r,n){let i=n*3,s=`
SELECT o.*, o.discovery_tokens