@gencode/agents 0.7.6 → 0.7.7
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/CHANGELOG.md +6 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -38,7 +38,7 @@ Preview:
|
|
|
38
38
|
`)}const rl=[`If you need content beyond this preview, do not load the full output into the parent context.`,`Use subagent_spawn to inspect the Full output file and return only a concise, query-focused summary.`,`Pass the Full output path, note that it is relative to dataDir when not absolute, and include the current question or search target in the subagent task.`];async function il(e){let t=h(e.dataDir,e.sessionId,e),n=new Map,r=new Map,i=[],o=[],s=[],c,l,u=0,d,f,p=await ll(t);for(let e of p.readStates)n.set(al(e.path,e.offset,e.limit),e);for(let e of p.toolResults)r.set(e.toolCallId,e);i.push(...p.compaction.budgets),o.push(...p.compaction.snips),s.push(...p.compaction.collapseSpans),c=p.compaction.sessionMemory,l=p.compaction.modelUsage,u=p.compaction.consecutiveAutocompactFailures,d=p.compaction.lastCompactionAt,f=p.compaction.lastCompactionLayer;let m=async()=>{let p={version:2,updatedAt:new Date().toISOString(),readStates:Array.from(n.values()),toolResults:Array.from(r.values()).slice(-100),compaction:{sessionMemory:c,modelUsage:l,budgets:i.slice(-200),snips:o.slice(-200),collapseSpans:s.slice(-100),consecutiveAutocompactFailures:u,lastCompactionAt:d,lastCompactionLayer:f}};await G.mkdir(K.dirname(t),{recursive:!0});let m=e.encryptSessions??!1;await a(t,JSON.stringify(p,null,2),m)};return{async findReusableRead(e,t,r){let i=al(e,t,r),a=n.get(i);if(!a)return null;let o=await sl(e);return o===null||o!==a.lastModifiedMs?(n.delete(i),await m(),null):a},async recordRead(e){let t=new Date().toISOString(),r={path:e.path,offset:e.offset,limit:e.limit,contentHash:cl(e.content),lineCount:e.lineCount,capturedAt:t,lastModifiedMs:await sl(e.path)??Date.now()};return n.set(al(e.path,e.offset,e.limit),r),await m(),r},async invalidatePath(e){let t=!1;for(let[r,i]of n.entries())ol(i.path)===ol(e)&&(n.delete(r),t=!0);t&&await m()},async persistToolResult(t){let n=t.thresholdChars??12e3;if(t.content.length<=n)return{content:t.content};let o=t.previewChars??2e3,s=S(e.dataDir,e.sessionId,e),c=`${t.toolCallId}.txt`,l=K.join(s,c);await G.mkdir(s,{recursive:!0});let u=e.encryptSessions??!1;await a(l,t.content,u);let p=K.relative(e.dataDir,l).split(K.sep).join(`/`),h=pl(t.content,o),g={sessionId:e.sessionId,toolCallId:t.toolCallId,toolName:t.toolName,storagePath:p,preview:h,originalChars:t.content.length,truncated:!0,createdAt:new Date().toISOString()};return r.set(g.toolCallId,g),i.push({toolCallId:g.toolCallId,toolName:g.toolName,originalChars:t.content.length,storedChars:t.content.length,previewChars:h.length,strategy:`persist`,createdAt:g.createdAt}),d=g.createdAt,f=`L2`,await m(),{content:fl(g),reference:g}},async recordSnip(e){o.push(e),d=e.createdAt,f=e.reason===`context_dedup`?`L1`:e.reason===`microcompact`?`L4`:`L3`,await m()},async recordCollapse(e){s.push(e),d=e.createdAt,f=e.layer,await m()},async setSessionMemory(t){c=t,await ml(e.dataDir,e.sessionId,t,e),await m()},async recordModelUsage(e){l=e,await m()},async recordAutocompactResult(e){u=e.failed?u+1:0,d=e.timestamp,f=`L6`,await m()},getSnapshot(){return{version:2,updatedAt:new Date().toISOString(),readStates:Array.from(n.values()),toolResults:Array.from(r.values()).slice(-100),compaction:{sessionMemory:c,modelUsage:l,budgets:i.slice(-200),snips:o.slice(-200),collapseSpans:s.slice(-100),consecutiveAutocompactFailures:u,lastCompactionAt:d,lastCompactionLayer:f}}}}}function al(e,t,n){return`${ol(e)}::${t??``}::${n??``}`}function ol(e){return K.normalize(e)}async function sl(e){try{return(await G.stat(e)).mtimeMs}catch{return null}}function cl(e){return Ue(`sha1`).update(e).digest(`hex`)}async function ll(e){try{let t=await l(e);if(t===null)return ul();let n=JSON.parse(t),r=typeof n.version==`number`?n.version:void 0;return r!==1&&r!==2?ul():{version:2,updatedAt:typeof n.updatedAt==`string`?n.updatedAt:new Date(0).toISOString(),readStates:Array.isArray(n.readStates)?n.readStates:[],toolResults:Array.isArray(n.toolResults)?n.toolResults:[],compaction:{sessionMemory:n.compaction?.sessionMemory,modelUsage:dl(n.compaction?.modelUsage),budgets:Array.isArray(n.compaction?.budgets)?n.compaction.budgets:[],snips:Array.isArray(n.compaction?.snips)?n.compaction.snips:[],collapseSpans:Array.isArray(n.compaction?.collapseSpans)?n.compaction.collapseSpans:[],consecutiveAutocompactFailures:typeof n.compaction?.consecutiveAutocompactFailures==`number`?n.compaction.consecutiveAutocompactFailures:0,lastCompactionAt:typeof n.compaction?.lastCompactionAt==`string`?n.compaction.lastCompactionAt:void 0,lastCompactionLayer:n.compaction?.lastCompactionLayer===`L1`||n.compaction?.lastCompactionLayer===`L2`||n.compaction?.lastCompactionLayer===`L3`||n.compaction?.lastCompactionLayer===`L4`||n.compaction?.lastCompactionLayer===`L5`||n.compaction?.lastCompactionLayer===`L6`?n.compaction.lastCompactionLayer:void 0}}}catch{return ul()}}function ul(){return{version:2,updatedAt:new Date(0).toISOString(),readStates:[],toolResults:[],compaction:{budgets:[],snips:[],collapseSpans:[],consecutiveAutocompactFailures:0}}}function dl(e){if(!e||typeof e!=`object`)return;let t=e;if(!(t.version!==1||typeof t.model!=`string`||typeof t.inputTokens!=`number`||typeof t.outputTokens!=`number`||typeof t.totalTokens!=`number`||typeof t.coveredTranscriptEntryCount!=`number`||typeof t.recordedAt!=`string`))return{version:1,model:t.model,inputTokens:Math.max(0,Math.floor(t.inputTokens)),outputTokens:Math.max(0,Math.floor(t.outputTokens)),totalTokens:Math.max(0,Math.floor(t.totalTokens)),coveredTranscriptEntryCount:Math.max(0,Math.floor(t.coveredTranscriptEntryCount)),recordedAt:t.recordedAt}}function fl(e){let t=e.preview.length<e.originalChars?`
|
|
39
39
|
...`:``;return[`[tool result persisted] ${e.toolName} output was too large (${e.originalChars} chars).`,`Full output: ${e.storagePath}`,`Instruction:`,...rl,`Preview:`,e.preview+t].join(`
|
|
40
40
|
`)}function pl(e,t){if(e.length<=t)return e;let n=e.slice(0,t).lastIndexOf(`
|
|
41
|
-
`),r=n>t*.5?n:t;return e.slice(0,r)}async function ml(e,t,r,i){let o=n(e,t,i);await G.mkdir(K.dirname(o),{recursive:!0});let s=i?.encryptSessions??!1;await a(o,JSON.stringify(r,null,2),s)}const hl=new Set([`read_file`,`list_dir`,`glob`,`grep`,`bash`,`exec`,`web_fetch`,`web_search`]);async function gl(e){let{entries:t,modelInfo:n,contextWindowTokens:r,llm:i,historyLimit:a,compactionEnabled:o=!0,pendingUserMessage:s,signal:c,hooks:l,hookCtx:u,contextStore:d,dataDir:f,sessionId:p,sessionStoreName:m,sessionPathScope:h}=e,g,_=Ol(t),v=_>=0?t.slice(_+1):t;_>=0&&(g=t[_].content);let y=t.length,b=Mc(Yc(v,a)),x=[];d&&(g=await _l({entries:b,existingSummary:g,llm:i,contextStore:d,signal:c,hooks:l,hookCtx:u}));let S=b;if(S=await vl({entries:S,maxInlineResults:
|
|
41
|
+
`),r=n>t*.5?n:t;return e.slice(0,r)}async function ml(e,t,r,i){let o=n(e,t,i);await G.mkdir(K.dirname(o),{recursive:!0});let s=i?.encryptSessions??!1;await a(o,JSON.stringify(r,null,2),s)}const hl=new Set([`read_file`,`list_dir`,`glob`,`grep`,`bash`,`exec`,`web_fetch`,`web_search`]);async function gl(e){let{entries:t,modelInfo:n,contextWindowTokens:r,llm:i,historyLimit:a,compactionEnabled:o=!0,pendingUserMessage:s,signal:c,hooks:l,hookCtx:u,contextStore:d,dataDir:f,sessionId:p,sessionStoreName:m,sessionPathScope:h}=e,g,_=Ol(t),v=_>=0?t.slice(_+1):t;_>=0&&(g=t[_].content);let y=t.length,b=Mc(Yc(v,a)),x=[];d&&(g=await _l({entries:b,existingSummary:g,llm:i,contextStore:d,signal:c,hooks:l,hookCtx:u}));let S=b;if(S=await vl({entries:S,maxInlineResults:16,maxChars:1e3,layer:`L3`,strategy:`snip`,contextStore:d,compactionEvents:x}),S=await yl({entries:S,contextStore:d,compactionEvents:x}),S=await bl({entries:S,contextStore:d,compactionEvents:x,dataDir:f,sessionId:p,sessionStoreName:m,sessionPathScope:h}),!o)return{messages:Qc(S,n),priorSummary:g,compactionEntry:void 0,stats:{originalCount:y,keptCount:S.length,estimatedTokens:$(S),compacted:!1},compactionEvents:x};let C=d?.getSnapshot(),w=Math.max(1e3,Math.floor(r*.7)),T=kl({entries:t,workingEntries:S,pendingUserMessage:s,checkpoint:C?.compaction.modelUsage}).projectedInputTokens>=w,E=C?.compaction.consecutiveAutocompactFailures??0;if(!T||E>=3)return{messages:Qc(S,n),priorSummary:g,compactionEntry:void 0,stats:{originalCount:y,keptCount:S.length,estimatedTokens:$(S),compacted:!1},compactionEvents:x};let{toKeep:D,toSummarise:O}=jc(S,r);if(O.length===0)return{messages:Qc(S,n),priorSummary:g,compactionEntry:void 0,stats:{originalCount:y,keptCount:S.length,estimatedTokens:$(S),compacted:!1},compactionEvents:x};let k=new Date().toISOString();try{let e=C?.compaction.sessionMemory,t=e?.summary?.trim()?e.summary.trim():await Tc({entries:O,llm:i,previousSummary:g,signal:c,hooks:l,hookCtx:u}),r={role:`compaction`,content:t,keptCount:D.length,droppedCount:O.length,timestamp:k};return await d?.recordAutocompactResult({layer:`L6`,failed:!1,timestamp:k}),x.push({type:`compaction`,reason:e?.summary?.trim()?`Autocompact replaced ${O.length} entries with session memory`:`Autocompact summarised ${O.length} entries`,layer:`L6`,strategy:e?.summary?.trim()?`session_memory`:`full_summary`,estimatedTokensFreed:Math.max(0,$(O)),affectedEntries:O.length,snapshotVersion:2}),{messages:Qc(Mc(D),n),priorSummary:t,compactionEntry:r,stats:{originalCount:y,keptCount:D.length,estimatedTokens:$(D),compacted:!0},compactionEvents:x}}catch{return await d?.recordAutocompactResult({layer:`L6`,failed:!0,timestamp:k}),{messages:Qc(S,n),priorSummary:g,compactionEntry:void 0,stats:{originalCount:y,keptCount:S.length,estimatedTokens:$(S),compacted:!1},compactionEvents:[...x,{type:`compaction`,reason:`Autocompact failed and was skipped for this turn`,layer:`L6`,strategy:`circuit_breaker_retry`,snapshotVersion:2}]}}}async function _l(e){let{entries:t,existingSummary:n,llm:r,contextStore:i,signal:a,hooks:o,hookCtx:s}=e,c=$(t),l=i.getSnapshot().compaction.sessionMemory,u=!l&&c>=8e3,d=!!l&&c>=(l?.tokenEstimate??0)+15e3&&Dl(t)>=3;if(!u&&!d)return n??l?.summary;let f=await Tc({entries:t,llm:r,previousSummary:n??l?.summary,instructions:[`Produce a durable session memory for future compaction and resume.`,`If skill_load results are present, do not restate or reconstruct SKILL.md instructions.`,`Record only that the skill was loaded and that it must be reloaded with skill_load if its full content is not visible.`].join(` `),signal:a,hooks:o,hookCtx:s}),p={version:1,summary:f,generatedAt:new Date().toISOString(),sourceEntryCount:t.length,tokenEstimate:c,trigger:u?`threshold`:`autocompact`};return await i.setSessionMemory(p),f}async function vl(e){let{entries:t,maxInlineResults:n,maxChars:r,layer:i,strategy:a,contextStore:o,compactionEvents:s}=e;if(t.filter(Sl).length<=n)return t;let c=n,l=[],u=0,d=0;for(let e=t.length-1;e>=0;--e){let n=t[e];if(!Sl(n)){l.push(n);continue}if(c>0&&n.content.length<=r){--c,l.push(n);continue}let i=n.toolResultRef?.preview?.trim()||n.content.slice(0,r).trim(),s=Cl(n)?wl(n):[`[${a}] Older tool output condensed for context control.`,`Tool: ${n.toolName}`,i?`Preview:\n${i}`:``].filter(Boolean).join(`
|
|
42
42
|
`);if(s===n.content){l.push(n);continue}u+=1,d+=Math.max(0,Math.floor((n.content.length-s.length)/4));let f=new Date().toISOString();await o?.recordSnip({toolCallId:n.toolCallId,toolName:n.toolName,originalChars:n.content.length,retainedChars:s.length,reason:a===`microcompact`?`microcompact`:`history_snip`,createdAt:f}),l.push({...n,content:s})}return u>0&&s.push({type:`compaction`,reason:`${a===`snip`?`Snip compacted`:`Microcompacted`} ${u} tool results`,layer:i,strategy:a,estimatedTokensFreed:d,affectedEntries:u,snapshotVersion:2}),Mc(l.reverse())}async function yl(e){let{entries:t,contextStore:n,compactionEvents:r}=e,i=Date.now()-1728e5,a=t.reduce((e,t,n)=>(t.role===`user`&&e.push(n),e),[]),o=a.length>=3?a[a.length-3]??t.length:t.length,s=0,c=0,l=[];for(let[e,r]of t.entries()){if(!Sl(r)){l.push(r);continue}if(e>=o){l.push(r);continue}let t=Date.parse(r.timestamp);if(!(t>0&&t<i)){l.push(r);continue}let a=Cl(r)?wl(r):[`[microcompact] Tool output aged out of the hot context window.`,`Tool: ${r.toolName}`,`Preview:\n${r.toolResultRef?.preview?.trim()||r.content.slice(0,400).trim()}`].join(`
|
|
43
43
|
`);a!==r.content&&(s+=1,c+=Math.max(0,Math.floor((r.content.length-a.length)/4)),await n?.recordSnip({toolCallId:r.toolCallId,toolName:r.toolName,originalChars:r.content.length,retainedChars:a.length,reason:`microcompact`,createdAt:new Date().toISOString()})),l.push({...r,content:a})}return s===0?t:(r.push({type:`compaction`,reason:`Microcompacted ${s} stale tool results`,layer:`L4`,strategy:`microcompact_age`,estimatedTokensFreed:c,affectedEntries:s,snapshotVersion:2}),vl({entries:l,maxInlineResults:4,maxChars:400,layer:`L4`,strategy:`microcompact`,contextStore:n,compactionEvents:r}))}async function bl(e){let{entries:t,contextStore:n,compactionEvents:r,dataDir:i,sessionId:a,sessionStoreName:o,sessionPathScope:s}=e,c={storeName:o,...s},l=t.reduce((e,t,n)=>(t.role===`user`&&e.push(n),e),[]);if(l.length<=2)return t;let u=l[l.length-2]??t.length,d=[],f=0,p=0,m=0;for(;f<t.length;){if(f>=u||t[f]?.role===`user`){d.push(t[f]),f+=1;continue}let e=f;for(;f<u&&t[f]?.role!==`user`;)f+=1;let r=t.slice(e,f),o=r.filter(Sl).filter(e=>hl.has(e.toolName)),s=o.reduce((e,t)=>e+t.content.length,0);if(o.length<3||s<1500){d.push(...r);continue}let l=xl(r,o),h=new Date().toISOString(),g={role:`assistant`,content:l,timestamp:h};d.push(g),p+=r.length,m+=Math.max(0,Math.floor($(r)-$([g])));let _={id:We(),layer:`L5`,startEntryIndex:e,endEntryIndex:f-1,toolNames:[...new Set(o.map(e=>e.toolName))],summary:l,sourceCount:r.length,estimatedTokensFreed:m,createdAt:h};await n?.recordCollapse(_),i&&a&&await Al(i,a,_,c)}return p>0&&r.push({type:`compaction`,reason:`Collapsed ${p} historical tool activity entries`,layer:`L5`,strategy:`context_collapse`,estimatedTokensFreed:m,affectedEntries:p,snapshotVersion:2}),d}function xl(e,t){let n=[...new Set(t.map(e=>e.toolName))],r=t.slice(-3).map(e=>`- ${e.toolName}: ${(e.toolResultRef?.preview||e.content).slice(0,180).trim()}`).join(`
|
|
44
44
|
`),i=e.filter(e=>e.role===`assistant`).map(e=>e.content.trim()).filter(Boolean).slice(-2).join(`
|