toolnet-memory 0.2.20 → 0.3.0
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/.env.example +9 -0
- package/bin/toolnet-memory +76 -0
- package/bundle/agy-hook.js +67 -46
- package/bundle/agy.js +104 -38
- package/bundle/api.js +52 -0
- package/bundle/auto-integrate.js +66 -19
- package/bundle/claude-hook.js +78 -0
- package/bundle/claude.js +3 -0
- package/bundle/codex-context.js +30 -25
- package/bundle/codex.js +80 -61
- package/bundle/context-runtime.js +43 -38
- package/bundle/context.js +15 -14
- package/bundle/continuity-certify.js +49 -0
- package/bundle/doctor.js +4 -4
- package/bundle/full-index.js +18 -8
- package/bundle/graph-index.js +4 -4
- package/bundle/handoff-refresh.js +10 -10
- package/bundle/impact.js +3 -3
- package/bundle/incremental.js +4 -4
- package/bundle/init.js +763 -0
- package/bundle/learner.js +5 -5
- package/bundle/mcp.js +19 -15
- package/bundle/memory-agent.js +10 -11
- package/bundle/memory-query.js +7 -7
- package/bundle/opencode.js +72 -53
- package/bundle/production-certify.js +5 -0
- package/bundle/project-manual.js +6 -6
- package/bundle/recovery-certify.js +55 -0
- package/bundle/runtime.js +11 -11
- package/bundle/semantic.js +6 -6
- package/bundle/setup.js +69 -22
- package/bundle/snapshot.js +4 -4
- package/bundle/update.js +4 -1
- package/bundle/work-continuity.js +6 -6
- package/dist/sdk/public.d.ts +2 -0
- package/dist/sdk/public.js +2 -0
- package/dist/sdk/remote-client.d.ts +41 -0
- package/dist/sdk/remote-client.js +185 -0
- package/dist/sdk/types.d.ts +402 -0
- package/dist/sdk/types.js +1 -0
- package/package.json +14 -5
package/bundle/codex.js
CHANGED
|
@@ -1,26 +1,52 @@
|
|
|
1
|
-
import{existsSync as
|
|
2
|
-
`).replace(/\\r/g,"\r").replace(/\\t/g," ").replace(/\\"/g,'"').replace(/\\\\/g,"\\")):e.length>=2&&e.startsWith("'")&&e.endsWith("'")?e.slice(1,-1):e}function
|
|
3
|
-
`,{encoding:"utf8",mode:384}),_n(s,r)}function Ve(t,e){return{id:t.id,name:t.name,remote:t.remote,rootPath:e,createdAt:t.createdAt,updatedAt:t.updatedAt,graphVersion:t.graphVersion,memoryVersion:t.memoryVersion,metadata:t.metadata}}var _=class{detect(e=process.cwd()){let n=J(e),r=Bn(n),o=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(f=>Pe(Q(r,f))),i=Kn(n,o?r:void 0);if(i){let f=Oe(i),l=zn(f);return l.rootPath!==i&&(l.rootPath=i,l.updatedAt=new Date().toISOString(),He(i,l)),Ve(l,i)}let c=new Date().toISOString(),u=Fn(r),a={version:1,id:Wn(r),name:u,remote:u,rootPath:r,createdAt:c,updatedAt:c,graphVersion:0,memoryVersion:0};return He(r,a),Ve(a,r)}};var Jn=[{type:"openai_key",regex:/\bsk-[A-Za-z0-9_\-]{20,}\b/g},{type:"huggingface_token",regex:/\bhf_[A-Za-z0-9]{20,}\b/g},{type:"hf_s3_access_key",regex:/\bHFAK[A-Za-z0-9]{8,}\b/g},{type:"bearer_token",regex:/\bBearer\s+[A-Za-z0-9._\-]{16,}\b/gi},{type:"jwt",regex:/\beyJ[A-Za-z0-9_\-]{8,}\.[A-Za-z0-9_\-]{8,}\.[A-Za-z0-9_\-]{8,}\b/g},{type:"private_key",regex:/-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |OPENSSH )?PRIVATE KEY-----/g},{type:"password_assignment",regex:/\b(password|passwd|pwd)\s*[:=]\s*["']?[^"'\s]{6,}["']?/gi},{type:"secret_assignment",regex:/\b(secret|api[_-]?key|access[_-]?token|refresh[_-]?token)\s*[:=]\s*["']?[^"'\s]{8,}["']?/gi},{type:"cookie",regex:/\b(cookie|set-cookie)\s*[:=]\s*[^;\n]{8,}/gi}],Z=class{scan(e){let n=[];for(let r of Jn){let s=new RegExp(r.regex.source,r.regex.flags);for(let o of e.matchAll(s))n.push({type:r.type,value:o[0]})}return n}hasSecrets(e){return this.scan(e).length>0}};var q=class{scanner=new Z;sanitize(e){let n=e,r=this.scanner.scan(e),s=new Set;for(let o of r)s.add(o.type),n=n.split(o.value).join(`[REDACTED:${o.type}]`);return{text:n,redacted:r.length,secretTypes:[...s]}}sanitizeValue(e){if(typeof e=="string")return this.sanitize(e).text;if(Array.isArray(e))return e.map(n=>this.sanitizeValue(n));if(e&&typeof e=="object"){let n={};for(let[r,s]of Object.entries(e)){let o=r.toLowerCase();o.includes("password")||o.includes("secret")||o.includes("token")||o.includes("cookie")||o.includes("authorization")?n[r]="[REDACTED]":n[r]=this.sanitizeValue(s)}return n}return e}};import{homedir as fr}from"node:os";import{join as mr}from"node:path";import{DeleteObjectCommand as qn,GetObjectCommand as Hn,HeadObjectCommand as Vn,ListObjectsV2Command as Un,PutObjectCommand as Yn,S3Client as Gn}from"@aws-sdk/client-s3";import{getSignedUrl as Xn}from"@aws-sdk/s3-request-presigner";var ee=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new Gn({region:"us-east-1",endpoint:`https://s3.hf.co/${e.namespace}`,forcePathStyle:!0,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,n,r="application/octet-stream"){let s=typeof n=="string"?Buffer.from(n,"utf8"):n;await this.client.send(new Yn({Bucket:this.bucket,Key:e,Body:s,ContentType:r}))}async get(e){let n=await Xn(this.client,new Hn({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(n,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`HF download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let n=await this.get(e);return n?Buffer.from(n).toString("utf8"):null}async exists(e){try{return await this.client.send(new Vn({Bucket:this.bucket,Key:e})),!0}catch(n){if(typeof n=="object"&&n!==null&&"$metadata"in n&&n.$metadata?.httpStatusCode===404)return!1;throw n}}async delete(e){await this.client.send(new qn({Bucket:this.bucket,Key:e}))}async list(e=""){let n=[],r;do{let s=await this.client.send(new Un({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let o of s.Contents??[])o.Key&&n.push({key:o.Key,size:o.Size,updatedAt:o.LastModified?.toISOString()});r=s.IsTruncated?s.NextContinuationToken:void 0}while(r);return n}};import{access as Ge,mkdir as Qn,readFile as Zn,readdir as er,rm as tr,stat as Xe,writeFile as nr}from"node:fs/promises";import{dirname as rr,join as sr,relative as Qe,resolve as or}from"node:path";var H=class{constructor(e){this.root=e}root;name="local";path(e){let n=e.replace(/^\/+/,"");return or(this.root,n)}async put(e,n){let r=this.path(e);await Qn(rr(r),{recursive:!0}),await nr(r,n)}async get(e){try{return await Zn(this.path(e))}catch(n){if(typeof n=="object"&&n!==null&&"code"in n&&n.code==="ENOENT")return null;throw n}}async getText(e){let n=await this.get(e);return n?Buffer.from(n).toString("utf8"):null}async exists(e){try{return await Ge(this.path(e)),!0}catch{return!1}}async delete(e){await tr(this.path(e),{force:!0})}async list(e=""){let n=this.path(e),r=[];try{await Ge(n)}catch{return r}let s=async i=>{let c=await er(i,{withFileTypes:!0});for(let u of c){let a=sr(i,u.name);if(u.isDirectory()){await s(a);continue}let f=await Xe(a);r.push({key:Qe(this.root,a),size:f.size,updatedAt:f.mtime.toISOString()})}},o=await Xe(n);return o.isDirectory()?await s(n):r.push({key:Qe(this.root,n),size:o.size,updatedAt:o.mtime.toISOString()}),r}};import{DeleteObjectCommand as ir,GetObjectCommand as ar,HeadObjectCommand as cr,ListObjectsV2Command as ur,PutObjectCommand as lr,S3Client as dr}from"@aws-sdk/client-s3";import{getSignedUrl as pr}from"@aws-sdk/s3-request-presigner";var V=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new dr({region:e.region??"us-east-1",endpoint:e.endpoint||void 0,forcePathStyle:e.forcePathStyle??!1,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,n,r="application/octet-stream"){let s=typeof n=="string"?Buffer.from(n,"utf8"):n;await this.client.send(new lr({Bucket:this.bucket,Key:e,Body:s,ContentType:r}))}async get(e){let n=await pr(this.client,new ar({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(n,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`${this.name} download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let n=await this.get(e);return n?Buffer.from(n).toString("utf8"):null}async exists(e){try{return await this.client.send(new cr({Bucket:this.bucket,Key:e})),!0}catch(n){if(typeof n=="object"&&n!==null&&"$metadata"in n&&n.$metadata?.httpStatusCode===404)return!1;throw n}}async delete(e){await this.client.send(new ir({Bucket:this.bucket,Key:e}))}async list(e=""){let n=[],r;do{let s=await this.client.send(new ur({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let o of s.Contents??[])o.Key&&n.push({key:o.Key,size:o.Size,updatedAt:o.LastModified?.toISOString()});r=s.IsTruncated?s.NextContinuationToken:void 0}while(r);return n}};function Ee(t,e){return console.warn(e),new H(t)}function Ze(t){let e=t.localRoot??mr(fr(),".toolnet-memory","storage");if(t.provider==="r2"){let n=t.r2;return n?.accountId&&n.bucket&&n.accessKeyId&&n.secretAccessKey?new V({name:"r2",endpoint:`https://${n.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:n.bucket,forcePathStyle:!0,accessKeyId:n.accessKeyId,secretAccessKey:n.secretAccessKey}):Ee(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(t.provider==="s3"){let n=t.s3;return n?.bucket&&n.accessKeyId&&n.secretAccessKey?new V({name:"s3",endpoint:n.endpoint,region:n.region??"us-east-1",bucket:n.bucket,forcePathStyle:n.forcePathStyle??!1,accessKeyId:n.accessKeyId,secretAccessKey:n.secretAccessKey}):Ee(e,"[storage] S3 credentials missing. Using local fallback.")}if(t.provider==="huggingface"){let n=t.huggingface;return n?.namespace&&n.bucket&&n.accessKeyId&&n.secretAccessKey?new ee({namespace:n.namespace,bucket:n.bucket,accessKeyId:n.accessKeyId,secretAccessKey:n.secretAccessKey}):Ee(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return new H(e)}function gr(t){return new Promise(e=>setTimeout(e,t))}async function et(t,e={}){let n=Math.max(1,e.attempts??3),r=e.baseDelayMs??150,s=e.maxDelayMs??2e3,o;for(let i=1;i<=n;i++)try{return await t()}catch(c){if(o=c,i>=n)break;let u=Math.min(s,r*2**(i-1)),a=Math.floor(Math.random()*Math.max(1,u*.2));await gr(u+a)}throw o}var hr=new Set(["put","get","getText","delete","list"]);function tt(t,e={}){return new Proxy(t,{get(n,r){let s=Reflect.get(n,r,n);return typeof s!="function"?s:hr.has(r)?(...o)=>et(()=>Promise.resolve(s.apply(n,o)),e):s.bind(n)}})}function nt(t){let e=t.trim().replace(/\s+/g,"_").replace(/[^A-Za-z0-9._-]/g,"_").replace(/_+/g,"_").replace(/^\.+|\.+$/g,"").slice(0,100);if(!e||e==="."||e==="..")throw new Error("Invalid project storage folder");return e}function rt(t){let e=t.replace(/^\/+/,"");if(e.startsWith("memories/"))return"memory/records/"+e.slice(9);if(e.startsWith("vectors/"))return"memory/vectors/"+e.slice(8);if(e.startsWith("graph/"))return"code/graph/"+e.slice(6);let n=e.match(/^(snapshots\/[^/]+\/)memories\/(.+)$/);if(n)return`${n[1]}memory/records/${n[2]}`;if(n=e.match(/^(snapshots\/[^/]+\/)vectors\/(.+)$/),n)return`${n[1]}memory/vectors/${n[2]}`;if(n=e.match(/^(snapshots\/[^/]+\/)graph\/(.+)$/),n)return`${n[1]}code/graph/${n[2]}`;let r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)memories\/(.+)$/);if(r)return`${r[1]}memory/records/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)vectors\/(.+)$/),r)return`${r[1]}memory/vectors/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)graph\/(.+)$/),r)return`${r[1]}code/graph/${r[2]}`;let s=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return s?`${s[1]}memory/records/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),s?`${s[1]}memory/vectors/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),s?`${s[1]}code/graph/${s[2]}`:e))}var te=class{constructor(e,n,r,s){this.provider=e;this.name=e.name,this.projectId=n,this.projectName=r,this.folder=nt(s??r),this.sourcePrefix=`projects/${n}`,this.targetPrefix=`projects/${this.folder}`}provider;name;folder;sourcePrefix;targetPrefix;projectId;projectName;registryPromise;async registerProject(){let e=`${this.targetPrefix}/project.json`,n=new Date().toISOString(),r=n,s=await this.provider.getText(e);if(s){let i;try{i=JSON.parse(s)}catch(c){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${c instanceof Error?c.message:String(c)}`)}if(typeof i.id=="string"&&i.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${i.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof i.createdAt=="string"&&(r=i.createdAt)}let o={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:r,updatedAt:n};await this.provider.put(e,JSON.stringify(o,null,2)+`
|
|
4
|
-
`,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=
|
|
5
|
-
`,{encoding:"utf8",mode:384}),
|
|
6
|
-
`,"application/json")}async scan(e){let n=`${e.remotePrefix}/events/`,r=await this.storage.list(n),s=[],o=0;for(let i of r){let
|
|
7
|
-
`,"utf8");r.length>0&&(r.length>=this.maxEventsPerChunk||s+i>this.maxChunkBytes)&&(n.push(r),r=[],s=0),r.push(o),s+=i}return r.length>0&&n.push(r),n}async loadManifest(e){return this.getJson(`${e.remotePrefix}/session.json`)}async loadCursor(e){return this.getJson(`${e.remotePrefix}/cursor.json`)}async recover(e){let n=await this.scan(e);return{maxSequence:n.maxSequence,chunkCount:n.chunks.length}}async append(e,n,r,s={}){let o=await this.loadManifest(e),i=await this.scan(e),
|
|
1
|
+
import{existsSync as au,readFileSync as cu}from"node:fs";import{spawn as uu}from"node:child_process";import{homedir as lu}from"node:os";import{join as du}from"node:path";import{existsSync as Xr,readFileSync as Qr}from"node:fs";import{homedir as Zr}from"node:os";import{join as es}from"node:path";function ts(t){let e=t.trim();return e.length>=2&&e.startsWith('"')&&e.endsWith('"')?(e=e.slice(1,-1),e.replace(/\\n/g,`
|
|
2
|
+
`).replace(/\\r/g,"\r").replace(/\\t/g," ").replace(/\\"/g,'"').replace(/\\\\/g,"\\")):e.length>=2&&e.startsWith("'")&&e.endsWith("'")?e.slice(1,-1):e}function ns(){let t=process.env.TOOLNET_GLOBAL_ENV??es(Zr(),".config","toolnet-memory",".env");if(!Xr(t))return;let e=Qr(t,"utf8");for(let n of e.split(/\r?\n/)){let r=n.trim();if(!r||r.startsWith("#"))continue;r.startsWith("export ")&&(r=r.slice(7));let s=r.indexOf("=");if(s<=0)continue;let o=r.slice(0,s).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(o)&&process.env[o]===void 0&&(process.env[o]=ts(r.slice(s+1)))}}ns();function X(t,e){return t===void 0?e:["1","true","yes","on"].includes(t.toLowerCase())}function Q(t,e){if(!t)return e;let n=Number(t);return Number.isFinite(n)?n:e}function fe(){return{memory:{autoCapture:X(process.env.MEMORY_AUTO_CAPTURE,!0),autoRetrieve:X(process.env.MEMORY_AUTO_RETRIEVE,!0),autoSummarize:X(process.env.MEMORY_AUTO_SUMMARIZE,!0),autoSync:X(process.env.MEMORY_AUTO_SYNC,!0)},retrieval:{maxCandidates:Q(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:Q(process.env.MEMORY_RERANK_TOP,10),finalContext:Q(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:Q(process.env.MEMORY_TOKEN_BUDGET,2e3)},storage:{provider:process.env.MEMORY_STORAGE_PROVIDER??"huggingface",r2:{accountId:process.env.R2_ACCOUNT_ID,bucket:process.env.R2_BUCKET,accessKeyId:process.env.R2_ACCESS_KEY_ID,secretAccessKey:process.env.R2_SECRET_ACCESS_KEY},s3:{endpoint:process.env.S3_ENDPOINT,region:process.env.S3_REGION,bucket:process.env.S3_BUCKET,accessKeyId:process.env.S3_ACCESS_KEY_ID,secretAccessKey:process.env.S3_SECRET_ACCESS_KEY,forcePathStyle:X(process.env.S3_FORCE_PATH_STYLE,!1)},huggingface:{namespace:process.env.HF_NAMESPACE,bucket:process.env.HF_BUCKET,accessKeyId:process.env.HF_S3_ACCESS_KEY_ID,secretAccessKey:process.env.HF_S3_SECRET_ACCESS_KEY},localRoot:process.env.MEMORY_LOCAL_STORAGE_PATH},cache:{maxMb:Q(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as rs}from"node:crypto";import{existsSync as Qe,mkdirSync as ss,readFileSync as os,renameSync as is,writeFileSync as as}from"node:fs";import{basename as cs,dirname as me,join as ge,parse as Et,resolve as Z}from"node:path";var Tt=".toolnet",us="project.json";function ls(t){return rs("sha256").update(t).digest("hex").slice(0,16)}function Ze(t){return ge(t,Tt,us)}function ds(t){return Qe(Ze(t))}function ps(t,e){let n=Z(t),r=Et(n).root;for(;;){if(ds(n))return n;if(n===r||e&&n===Z(e))break;let s=me(n);if(s===n)break;n=s}return null}function fs(t){let e=Z(t),n=Et(e).root,r=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(r.some(o=>Qe(ge(e,o))))return e;if(e===n)break;let s=me(e);if(s===e)break;e=s}return Z(t)}function ms(t){let e;try{e=JSON.parse(os(t,"utf8"))}catch(s){throw new Error(`Invalid ToolNet project manifest: ${t}: ${s instanceof Error?s.message:String(s)}`)}if(!e||typeof e!="object")throw new Error(`Invalid ToolNet project manifest: ${t}`);let n=e;if(typeof n.id!="string"||!n.id.trim())throw new Error(`ToolNet project manifest is missing id: ${t}`);if(typeof n.name!="string"||!n.name.trim())throw new Error(`ToolNet project manifest is missing name: ${t}`);let r=new Date().toISOString();return{version:1,id:n.id,name:n.name,remote:typeof n.remote=="string"&&n.remote.trim()?n.remote:n.name,rootPath:typeof n.rootPath=="string"?n.rootPath:me(me(t)),createdAt:typeof n.createdAt=="string"?n.createdAt:r,updatedAt:typeof n.updatedAt=="string"?n.updatedAt:r,graphVersion:typeof n.graphVersion=="number"?n.graphVersion:0,memoryVersion:typeof n.memoryVersion=="number"?n.memoryVersion:0,metadata:n.metadata&&typeof n.metadata=="object"?n.metadata:void 0}}function At(t,e){let n=ge(t,Tt);ss(n,{recursive:!0});let r=Ze(t),s=`${r}.tmp-${process.pid}`;as(s,JSON.stringify(e,null,2)+`
|
|
3
|
+
`,{encoding:"utf8",mode:384}),is(s,r)}function Mt(t,e){return{id:t.id,name:t.name,remote:t.remote,rootPath:e,createdAt:t.createdAt,updatedAt:t.updatedAt,graphVersion:t.graphVersion,memoryVersion:t.memoryVersion,metadata:t.metadata}}var q=class{detect(e=process.cwd()){let n=Z(e),r=fs(n),o=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(f=>Qe(ge(r,f))),i=ps(n,o?r:void 0);if(i){let f=Ze(i),d=ms(f);return d.rootPath!==i&&(d.rootPath=i,d.updatedAt=new Date().toISOString(),At(i,d)),Mt(d,i)}let a=new Date().toISOString(),c=cs(r),u={version:1,id:ls(r),name:c,remote:c,rootPath:r,createdAt:a,updatedAt:a,graphVersion:0,memoryVersion:0};return At(r,u),Mt(u,r)}};var gs=[{type:"openai_key",regex:/\bsk-[A-Za-z0-9_\-]{20,}\b/g},{type:"huggingface_token",regex:/\bhf_[A-Za-z0-9]{20,}\b/g},{type:"hf_s3_access_key",regex:/\bHFAK[A-Za-z0-9]{8,}\b/g},{type:"bearer_token",regex:/\bBearer\s+[A-Za-z0-9._\-]{16,}\b/gi},{type:"jwt",regex:/\beyJ[A-Za-z0-9_\-]{8,}\.[A-Za-z0-9_\-]{8,}\.[A-Za-z0-9_\-]{8,}\b/g},{type:"private_key",regex:/-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |OPENSSH )?PRIVATE KEY-----/g},{type:"password_assignment",regex:/\b(password|passwd|pwd)\s*[:=]\s*["']?[^"'\s]{6,}["']?/gi},{type:"secret_assignment",regex:/\b(secret|api[_-]?key|access[_-]?token|refresh[_-]?token)\s*[:=]\s*["']?[^"'\s]{8,}["']?/gi},{type:"cookie",regex:/\b(cookie|set-cookie)\s*[:=]\s*[^;\n]{8,}/gi}],ye=class{scan(e){let n=[];for(let r of gs){let s=new RegExp(r.regex.source,r.regex.flags);for(let o of e.matchAll(s))n.push({type:r.type,value:o[0]})}return n}hasSecrets(e){return this.scan(e).length>0}};var ee=class{scanner=new ye;sanitize(e){let n=e,r=this.scanner.scan(e),s=new Set;for(let o of r)s.add(o.type),n=n.split(o.value).join(`[REDACTED:${o.type}]`);return{text:n,redacted:r.length,secretTypes:[...s]}}sanitizeValue(e){if(typeof e=="string")return this.sanitize(e).text;if(Array.isArray(e))return e.map(n=>this.sanitizeValue(n));if(e&&typeof e=="object"){let n={};for(let[r,s]of Object.entries(e)){let o=r.toLowerCase();o.includes("password")||o.includes("secret")||o.includes("token")||o.includes("cookie")||o.includes("authorization")?n[r]="[REDACTED]":n[r]=this.sanitizeValue(s)}return n}return e}};import{homedir as Ws}from"node:os";import{join as Ls}from"node:path";import{DeleteObjectCommand as ys,GetObjectCommand as hs,HeadObjectCommand as Ss,ListObjectsV2Command as ks,PutObjectCommand as ws,S3Client as vs}from"@aws-sdk/client-s3";import{getSignedUrl as bs}from"@aws-sdk/s3-request-presigner";var he=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new vs({region:"us-east-1",endpoint:`https://s3.hf.co/${e.namespace}`,forcePathStyle:!0,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,n,r="application/octet-stream"){let s=typeof n=="string"?Buffer.from(n,"utf8"):n;await this.client.send(new ws({Bucket:this.bucket,Key:e,Body:s,ContentType:r}))}async get(e){let n=await bs(this.client,new hs({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(n,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`HF download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let n=await this.get(e);return n?Buffer.from(n).toString("utf8"):null}async exists(e){try{return await this.client.send(new Ss({Bucket:this.bucket,Key:e})),!0}catch(n){if(typeof n=="object"&&n!==null&&"$metadata"in n&&n.$metadata?.httpStatusCode===404)return!1;throw n}}async delete(e){await this.client.send(new ys({Bucket:this.bucket,Key:e}))}async list(e=""){let n=[],r;do{let s=await this.client.send(new ks({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let o of s.Contents??[])o.Key&&n.push({key:o.Key,size:o.Size,updatedAt:o.LastModified?.toISOString()});r=s.IsTruncated?s.NextContinuationToken:void 0}while(r);return n}};import{access as Ot,mkdir as xs,readFile as js,readdir as Is,rm as Ps,stat as Rt,writeFile as Cs}from"node:fs/promises";import{dirname as As,join as Ms,relative as Nt,resolve as Es}from"node:path";var te=class{constructor(e){this.root=e}root;name="local";path(e){let n=e.replace(/^\/+/,"");return Es(this.root,n)}async put(e,n){let r=this.path(e);await xs(As(r),{recursive:!0}),await Cs(r,n)}async get(e){try{return await js(this.path(e))}catch(n){if(typeof n=="object"&&n!==null&&"code"in n&&n.code==="ENOENT")return null;throw n}}async getText(e){let n=await this.get(e);return n?Buffer.from(n).toString("utf8"):null}async exists(e){try{return await Ot(this.path(e)),!0}catch{return!1}}async delete(e){await Ps(this.path(e),{force:!0})}async list(e=""){let n=this.path(e),r=[];try{await Ot(n)}catch{return r}let s=async i=>{let a=await Is(i,{withFileTypes:!0});for(let c of a){let u=Ms(i,c.name);if(c.isDirectory()){await s(u);continue}let f=await Rt(u);r.push({key:Nt(this.root,u),size:f.size,updatedAt:f.mtime.toISOString()})}},o=await Rt(n);return o.isDirectory()?await s(n):r.push({key:Nt(this.root,n),size:o.size,updatedAt:o.mtime.toISOString()}),r}};import{DeleteObjectCommand as Ts,GetObjectCommand as Os,HeadObjectCommand as Rs,ListObjectsV2Command as Ns,PutObjectCommand as _s,S3Client as $s}from"@aws-sdk/client-s3";import{getSignedUrl as Ks}from"@aws-sdk/s3-request-presigner";var ne=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new $s({region:e.region??"us-east-1",endpoint:e.endpoint||void 0,forcePathStyle:e.forcePathStyle??!1,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,n,r="application/octet-stream"){let s=typeof n=="string"?Buffer.from(n,"utf8"):n;await this.client.send(new _s({Bucket:this.bucket,Key:e,Body:s,ContentType:r}))}async get(e){let n=await Ks(this.client,new Os({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(n,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`${this.name} download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let n=await this.get(e);return n?Buffer.from(n).toString("utf8"):null}async exists(e){try{return await this.client.send(new Rs({Bucket:this.bucket,Key:e})),!0}catch(n){if(typeof n=="object"&&n!==null&&"$metadata"in n&&n.$metadata?.httpStatusCode===404)return!1;throw n}}async delete(e){await this.client.send(new Ts({Bucket:this.bucket,Key:e}))}async list(e=""){let n=[],r;do{let s=await this.client.send(new Ns({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let o of s.Contents??[])o.Key&&n.push({key:o.Key,size:o.Size,updatedAt:o.LastModified?.toISOString()});r=s.IsTruncated?s.NextContinuationToken:void 0}while(r);return n}};function et(t,e){return console.warn(e),new te(t)}function _t(t){let e=t.localRoot??Ls(Ws(),".toolnet-memory","storage");if(t.provider==="r2"){let n=t.r2;return n?.accountId&&n.bucket&&n.accessKeyId&&n.secretAccessKey?new ne({name:"r2",endpoint:`https://${n.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:n.bucket,forcePathStyle:!0,accessKeyId:n.accessKeyId,secretAccessKey:n.secretAccessKey}):et(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(t.provider==="s3"){let n=t.s3;return n?.bucket&&n.accessKeyId&&n.secretAccessKey?new ne({name:"s3",endpoint:n.endpoint,region:n.region??"us-east-1",bucket:n.bucket,forcePathStyle:n.forcePathStyle??!1,accessKeyId:n.accessKeyId,secretAccessKey:n.secretAccessKey}):et(e,"[storage] S3 credentials missing. Using local fallback.")}if(t.provider==="huggingface"){let n=t.huggingface;return n?.namespace&&n.bucket&&n.accessKeyId&&n.secretAccessKey?new he({namespace:n.namespace,bucket:n.bucket,accessKeyId:n.accessKeyId,secretAccessKey:n.secretAccessKey}):et(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return new te(e)}function Fs(t){return new Promise(e=>setTimeout(e,t))}async function $t(t,e={}){let n=Math.max(1,e.attempts??3),r=e.baseDelayMs??150,s=e.maxDelayMs??2e3,o;for(let i=1;i<=n;i++)try{return await t()}catch(a){if(o=a,i>=n)break;let c=Math.min(s,r*2**(i-1)),u=Math.floor(Math.random()*Math.max(1,c*.2));await Fs(c+u)}throw o}var Ds=new Set(["put","get","getText","delete","list"]);function Kt(t,e={}){return new Proxy(t,{get(n,r){let s=Reflect.get(n,r,n);return typeof s!="function"?s:Ds.has(r)?(...o)=>$t(()=>Promise.resolve(s.apply(n,o)),e):s.bind(n)}})}function Wt(t){let e=t.trim().replace(/\s+/g,"_").replace(/[^A-Za-z0-9._-]/g,"_").replace(/_+/g,"_").replace(/^\.+|\.+$/g,"").slice(0,100);if(!e||e==="."||e==="..")throw new Error("Invalid project storage folder");return e}function Lt(t){let e=t.replace(/^\/+/,"");if(e.startsWith("memories/"))return"memory/records/"+e.slice(9);if(e.startsWith("vectors/"))return"memory/vectors/"+e.slice(8);if(e.startsWith("graph/"))return"code/graph/"+e.slice(6);let n=e.match(/^(snapshots\/[^/]+\/)memories\/(.+)$/);if(n)return`${n[1]}memory/records/${n[2]}`;if(n=e.match(/^(snapshots\/[^/]+\/)vectors\/(.+)$/),n)return`${n[1]}memory/vectors/${n[2]}`;if(n=e.match(/^(snapshots\/[^/]+\/)graph\/(.+)$/),n)return`${n[1]}code/graph/${n[2]}`;let r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)memories\/(.+)$/);if(r)return`${r[1]}memory/records/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)vectors\/(.+)$/),r)return`${r[1]}memory/vectors/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)graph\/(.+)$/),r)return`${r[1]}code/graph/${r[2]}`;let s=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return s?`${s[1]}memory/records/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),s?`${s[1]}memory/vectors/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),s?`${s[1]}code/graph/${s[2]}`:e))}var Se=class{constructor(e,n,r,s){this.provider=e;this.name=e.name,this.projectId=n,this.projectName=r,this.folder=Wt(s??r),this.sourcePrefix=`projects/${n}`,this.targetPrefix=`projects/${this.folder}`}provider;name;folder;sourcePrefix;targetPrefix;projectId;projectName;registryPromise;async registerProject(){let e=`${this.targetPrefix}/project.json`,n=new Date().toISOString(),r=n,s=await this.provider.getText(e);if(s){let i;try{i=JSON.parse(s)}catch(a){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${a instanceof Error?a.message:String(a)}`)}if(typeof i.id=="string"&&i.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${i.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof i.createdAt=="string"&&(r=i.createdAt)}let o={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:r,updatedAt:n};await this.provider.put(e,JSON.stringify(o,null,2)+`
|
|
4
|
+
`,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=Lt(e),e===this.sourcePrefix)return this.targetPrefix;if(e.startsWith(`${this.sourcePrefix}/`))return this.targetPrefix+e.slice(this.sourcePrefix.length);if(e===this.targetPrefix||e.startsWith(`${this.targetPrefix}/`))return e;if(e.startsWith("projects/"))throw new Error(["Cross-project storage access denied.",`Current project: ${this.targetPrefix}`,`Requested key: ${e}`].join(" "));return e}async put(e,n,r){return await this.ensureRegistered(),this.provider.put(this.key(e),n,r)}async get(e){return await this.ensureRegistered(),this.provider.get(this.key(e))}async getText(e){return await this.ensureRegistered(),this.provider.getText(this.key(e))}async delete(e){return await this.ensureRegistered(),this.provider.delete(this.key(e))}async exists(e){return await this.ensureRegistered(),this.provider.exists(this.key(e))}async list(e){return await this.ensureRegistered(),this.provider.list(this.key(e))}};import{join as Gs}from"node:path";import{createHash as zs}from"node:crypto";import{dirname as Bs}from"node:path";import{mkdirSync as qs,readFileSync as Vs,renameSync as Hs,writeFileSync as Js}from"node:fs";function w(t){return zs("sha256").update(t).digest("hex")}function tt(t){if(Array.isArray(t))return t.map(tt);if(t&&typeof t=="object"){let e=t,n={};for(let r of Object.keys(e).sort())n[r]=tt(e[r]);return n}return t}function Ft(t){return JSON.stringify(tt(t))}function Dt(t){try{return JSON.parse(Vs(t,"utf8"))}catch{return null}}function M(t,e){qs(Bs(t),{recursive:!0});let n=`${t}.${process.pid}.tmp`;Js(n,JSON.stringify(e,null,2)+`
|
|
5
|
+
`,{encoding:"utf8",mode:384}),Hs(n,t)}function zt(t,e){let n=t.trim(),r=n.replace(/\s+/g,"_").replace(/[^A-Za-z0-9._-]/g,"_").replace(/_+/g,"_").replace(/^\.+|\.+$/g,""),s=w(n).slice(0,12);if(!r||r==="."||r==="..")return`${e}--${s}`;let o=r.slice(0,100);return o===n&&n.length<=100?o:`${o.slice(0,85)}--${s}`}function Bt(t,e,n){let r=e.trim(),s=n.trim();if(!r)throw new Error("Session agent is required");if(!s)throw new Error("Native session ID is required");let o=zt(r.toLowerCase(),"agent"),i=zt(s,"session");return{projectId:t.id,projectName:t.name,projectRoot:t.rootPath,agent:r,nativeSessionId:s,sessionKey:`${r}:${s}`,remotePrefix:["projects",t.id,"sessions",o,i].join("/"),localDirectory:Gs(t.rootPath,".toolnet","sessions",o,i)}}function qt(t){return String(t).padStart(12,"0")}var ke=class{constructor(e,n=100,r=512*1024){this.storage=e;this.maxEventsPerChunk=n;this.maxChunkBytes=r;if(n<1)throw new Error("maxEventsPerChunk must be positive");if(r<1024)throw new Error("maxChunkBytes is too small")}storage;maxEventsPerChunk;maxChunkBytes;async getJson(e){let n=await this.storage.getText(e);return n?JSON.parse(n):null}async putJson(e,n){await this.storage.put(e,JSON.stringify(n,null,2)+`
|
|
6
|
+
`,"application/json")}async scan(e){let n=`${e.remotePrefix}/events/`,r=await this.storage.list(n),s=[],o=0;for(let i of r){let a=i.key.match(/\/events\/(\d+)-(\d+)-[a-f0-9]+\.jsonl$/);if(!a)continue;let c=Number(a[1]),u=Number(a[2]);!Number.isFinite(c)||!Number.isFinite(u)||(s.push({key:i.key,start:c,end:u}),o=Math.max(o,u))}return s.sort((i,a)=>i.start-a.start),{chunks:s,maxSequence:o}}split(e){let n=[],r=[],s=0;for(let o of e){let i=Buffer.byteLength(JSON.stringify(o)+`
|
|
7
|
+
`,"utf8");r.length>0&&(r.length>=this.maxEventsPerChunk||s+i>this.maxChunkBytes)&&(n.push(r),r=[],s=0),r.push(o),s+=i}return r.length>0&&n.push(r),n}async loadManifest(e){return this.getJson(`${e.remotePrefix}/session.json`)}async loadCursor(e){return this.getJson(`${e.remotePrefix}/cursor.json`)}async recover(e){let n=await this.scan(e);return{maxSequence:n.maxSequence,chunkCount:n.chunks.length}}async append(e,n,r,s={}){let o=await this.loadManifest(e),i=await this.scan(e),a=n.filter(h=>h.sequence>i.maxSequence),c=0;for(let h of this.split(a)){let y=h[0],k=h[h.length-1],P=h.map(A=>JSON.stringify(A)).join(`
|
|
8
8
|
`)+`
|
|
9
|
-
`,
|
|
9
|
+
`,C=w(P).slice(0,16),j=[e.remotePrefix,"events",`${qt(y.sequence)}-${qt(k.sequence)}-${C}.jsonl`].join("/");await this.storage.exists(j)||await this.storage.put(j,P,"application/x-ndjson"),c+=h.length}let u=await this.scan(e),f=n[n.length-1],d=o?.status??"active";f?.type==="session_end"||f?.type==="session_idle"?d="idle":f?.type==="error"?d="error":n.length>0&&(d="active");let p=new Date().toISOString(),l=n[0],m={version:1,projectId:e.projectId,projectName:e.projectName,agent:e.agent,nativeSessionId:e.nativeSessionId,sessionKey:e.sessionKey,status:d,createdAt:o?.createdAt??l?.timestamp??p,updatedAt:f?.timestamp??p,firstEventAt:o?.firstEventAt??l?.timestamp,lastEventAt:f?.timestamp??o?.lastEventAt,eventCount:u.maxSequence,chunkCount:u.chunks.length,metadata:{...o?.metadata,...s.metadata}};(s.title??o?.title)&&(m.title=s.title??o?.title);let S={version:1,projectId:e.projectId,agent:e.agent,nativeSessionId:e.nativeSessionId,lastLocalSequence:n.length>0?n[n.length-1].sequence:u.maxSequence,lastRemoteSequence:u.maxSequence,sourceCursors:r,updatedAt:p};return await this.putJson(`${e.remotePrefix}/cursor.json`,S),await this.putJson(`${e.remotePrefix}/session.json`,m),{uploadedEvents:c,lastRemoteSequence:u.maxSequence,eventCount:m.eventCount,chunkCount:m.chunkCount,status:d}}};import{closeSync as nt,existsSync as Us,fsyncSync as Ys,mkdirSync as Xs,openSync as rt,readSync as Qs,rmSync as Vt,statSync as Ht,writeSync as Zs}from"node:fs";import{join as st}from"node:path";var eo=12e4,to=80,no=2e3;function ro(t){Atomics.wait(new Int32Array(new SharedArrayBuffer(4)),0,0,t)}var we=class{constructor(e){this.identity=e;Xs(e.localDirectory,{recursive:!0}),this.eventsFile=st(e.localDirectory,"events.jsonl"),this.stateFile=st(e.localDirectory,"state.json"),this.lockFile=st(e.localDirectory,"journal.lock")}identity;eventsFile;stateFile;lockFile;initialState(){let e=new Date().toISOString();return{version:1,projectId:this.identity.projectId,agent:this.identity.agent,nativeSessionId:this.identity.nativeSessionId,status:"idle",createdAt:e,updatedAt:e,lastSequence:0,lastRemoteSequence:0,remoteByteOffset:0,sourceCursors:{},recentEventIds:[]}}loadStateUnsafe(){return Dt(this.stateFile)??this.initialState()}loadState(){return this.withLock(()=>this.loadStateUnsafe())}saveStateUnsafe(e){M(this.stateFile,e)}acquireLock(){for(let e=0;e<to;e+=1)try{return rt(this.lockFile,"wx",384)}catch(n){if(n.code!=="EEXIST")throw n;try{if(Date.now()-Ht(this.lockFile).mtimeMs>eo){Vt(this.lockFile,{force:!0});continue}}catch{}ro(25)}throw new Error(`Session journal is locked: ${this.lockFile}`)}withLock(e){let n=this.acquireLock();try{return e()}finally{nt(n),Vt(this.lockFile,{force:!0})}}append(e){return e.length===0?[]:this.withLock(()=>{let n=this.loadStateUnsafe(),r=new Set(n.recentEventIds),s=n.lastSequence,o=[];for(let d of e){let p=d.timestamp??new Date().toISOString(),l=d.data??{},m=d.provenance?.rawDigest??w(Ft(l)),S=d.sourceEventId?[this.identity.projectId,this.identity.agent,this.identity.nativeSessionId,d.sourceEventId].join("|"):[this.identity.projectId,this.identity.agent,this.identity.nativeSessionId,s+1,d.type,p,m].join("|"),h=w(S).slice(0,32);if(r.has(h))continue;s+=1;let y={version:1,id:h,sequence:s,projectId:this.identity.projectId,agent:this.identity.agent,nativeSessionId:this.identity.nativeSessionId,type:d.type,timestamp:p,data:l,provenance:{...d.provenance,rawDigest:m}};d.role!==void 0&&(y.role=d.role),d.sourceEventId!==void 0&&(y.sourceEventId=d.sourceEventId),d.sourceSequence!==void 0&&(y.sourceSequence=d.sourceSequence),o.push(y),r.add(h)}if(o.length===0)return[];let i=o.map(d=>JSON.stringify(d)).join(`
|
|
10
10
|
`)+`
|
|
11
|
-
`,
|
|
12
|
-
`).filter(Boolean).map(
|
|
13
|
-
|
|
14
|
-
`);
|
|
15
|
-
`).filter(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
`)
|
|
19
|
-
`)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
`);if(
|
|
23
|
-
`).filter(Boolean).flatMap(a=>{try{return[JSON.parse(a)]}catch{return[]}}),nextOffset:r+Buffer.byteLength(u,"utf8")}}var fe=class{constructor(e){this.options=e;this.journal=new pe(e.storage)}options;journal;async learnNew(){let e=this.options.wal.loadState(),n=Number(e.sourceCursors["work.semantic.offset"]??0),r=Is(this.options.wal.eventsFile,Number.isFinite(n)?n:0);if(r.events.length===0)return{scannedEvents:0,observations:0,journalWritten:!1,reconciled:!1,nextOffset:r.nextOffset};let s=jt(this.options.identity,r.events),o=!1,i=!1;return s.length>0&&(o=!!await this.journal.write(this.options.identity,s),o&&(await Ot(this.options.project,this.options.storage),i=!0)),this.options.wal.setSourceCursor("work.semantic.offset",r.nextOffset),{scannedEvents:r.events.length,observations:s.length,journalWritten:o,reconciled:i,nextOffset:r.nextOffset}}};import{mkdirSync as _s}from"node:fs";import{join as _e}from"node:path";import{existsSync as At,mkdirSync as Ps,readFileSync as Os,statSync as Tt,writeFileSync as Es}from"node:fs";import{dirname as Ts,join as Cs}from"node:path";var Ct=64*1024,As=`# ToolNet Project Operating Manual
|
|
11
|
+
`,a=rt(this.eventsFile,"a",384);try{Zs(a,i,null,"utf8"),Ys(a)}finally{nt(a)}let c=o[o.length-1],u="active";c.type==="session_end"||c.type==="session_idle"?u="idle":c.type==="error"&&(u="error");let f=Array.from(r).slice(-no);return this.saveStateUnsafe({...n,status:u,updatedAt:c.timestamp,lastSequence:c.sequence,recentEventIds:f}),o})}readPending(){return this.withLock(()=>{let e=this.loadStateUnsafe();if(!Us(this.eventsFile))return{events:[],startOffset:e.remoteByteOffset,endOffset:e.remoteByteOffset};let n=Ht(this.eventsFile).size,r=Math.min(e.remoteByteOffset,n);if(n<=r)return{events:[],startOffset:r,endOffset:n};let s=n-r,o=Buffer.alloc(s),i=rt(this.eventsFile,"r");try{Qs(i,o,0,s,r)}finally{nt(i)}return{events:o.toString("utf8").split(`
|
|
12
|
+
`).filter(Boolean).map(u=>JSON.parse(u)),startOffset:r,endOffset:n}})}markRemote(e,n){this.withLock(()=>{let r=this.loadStateUnsafe();this.saveStateUnsafe({...r,lastRemoteSequence:Math.max(r.lastRemoteSequence,e),remoteByteOffset:Math.max(r.remoteByteOffset,n),updatedAt:new Date().toISOString()})})}setSourceCursor(e,n){this.withLock(()=>{let r=this.loadStateUnsafe();this.saveStateUnsafe({...r,sourceCursors:{...r.sourceCursors,[e]:String(n)},updatedAt:new Date().toISOString()})})}};import{closeSync as Gi,existsSync as Ui,openSync as Yi,readSync as Xi,statSync as Qi}from"node:fs";function Jt(t,e){let n=e.toLowerCase();return n.includes("kh\xF4ng \u0111\u01B0\u1EE3c")||n.includes("tuy\u1EC7t \u0111\u1ED1i")||n.includes("must not")||n.includes("never ")?"critical":t==="rule"||t==="decision"?"high":t==="todo"||n.includes("error")||n.includes("failed")||n.includes("exception")?"normal":"temporary"}var Xt=[/không được/iu,/tuyệt đối/iu,/bắt buộc/iu,/đừng\s+/iu,/must not/iu,/do not/iu,/don't/iu,/\bnever\b/iu],so=[/từ giờ/iu,/về sau/iu,/mỗi lần/iu,/luôn luôn/iu,/\bluôn\b/iu,/quy tắc/iu,/workflow/iu,/\balways\b/iu,/\brequired\b/iu,/\bmust\b/iu,/from now on/iu],oo=[/\bchốt\b/iu,/quyết định/iu,/sẽ dùng/iu,/chọn .+ thay/iu,/đổi sang/iu,/chuyển sang/iu,/\bdecided\b/iu,/\bchosen\b/iu,/we will use/iu,/use .+ instead/iu,/switch(?:ed)? to/iu],io=[/\btodo\b/iu,/cần làm/iu,/cần thêm/iu,/cần sửa/iu,/cần kiểm tra/iu,/tiếp theo/iu,/bước tiếp theo/iu,/còn phải/iu,/còn cần/iu,/next step/iu,/\bneed to\b/iu,/\bremaining\b/iu,/follow[- ]?up/iu],ao=[/đã sửa/iu,/đã fix/iu,/đã khắc phục/iu,/đã xử lý/iu,/hoàn tất/iu,/hoàn thành/iu,/\bfixed\b/iu,/\bresolved\b/iu,/\bimplemented\b/iu,/\bcompleted\b/iu,/\bpasses?\b/iu],Gt=[/kiến trúc/iu,/pipeline/iu,/adapter/iu,/schema/iu,/runtime/iu,/namespace/iu,/storage/iu,/lưu trữ/iu,/workflow/iu,/session core/iu,/memory engine/iu,/retrieval/iu],co=[/\bdùng\b/iu,/\btách\b/iu,/\bthay\b/iu,/\bchuyển\b/iu,/\blưu\b/iu,/\bmap\b/iu,/\buse\b/iu,/\bsplit\b/iu,/\bstore\b/iu,/\breplace\b/iu,/\bmove\b/iu],uo=[/đường dẫn/iu,/\bpath\b/iu,/\bport\b/iu,/\bendpoint\b/iu,/\bdomain\b/iu,/\bbucket\b/iu,/\brepository\b/iu,/\brepo\b/iu,/\bbranch\b/iu,/\/[A-Za-z0-9._/-]{4,}/u],lo=[/\blà\b/iu,/\bở\b/iu,/\bdùng\b/iu,/\bnằm\b/iu,/\bis\b/iu,/\buse\b/iu,/located/iu,/runs on/iu],Ut=new Set(["content","text","message","prompt","summary","description","reason","title","last_assistant_message","lastAssistantMessage","input_messages","inputMessages"]),po=new Set(["payload","data","content","message","messages","parts","summary"]);function E(t,e){return e.some(n=>n.test(t))}function Qt(t){return t.normalize("NFKC").replace(/\r/g,"").replace(/^[\s>*#\-•]+/u,"").replace(/\s+/g," ").trim()}function fo(t){return Qt(t).toLowerCase().replace(/[^\p{L}\p{N}:/._-]+/gu," ").replace(/\s+/g," ").trim()}function mo(t){return!(t.length<12||t.length>1e3||(t.match(new RegExp("\\p{L}","gu"))??[]).length<6||/^(?:https?:\/\/\S+|[A-Za-z0-9+/=]{80,})$/u.test(t))}function ot(t,e,n,r=0){if(!(r>6)){if(typeof t=="string"){(!e||Ut.has(e))&&n.push(t);return}if(Array.isArray(t)){for(let s of t.slice(0,50))ot(s,e,n,r+1);return}if(!(!t||typeof t!="object"))for(let[s,o]of Object.entries(t))(Ut.has(s)||po.has(s))&&ot(o,s,n,r+1)}}function go(t){let e=[];ot(t.data,void 0,e);let n=[],r=new Set;for(let s of e)for(let o of s.split(/\n+|(?<=[.!?])\s+/u)){let i=Qt(o);if(mo(i)&&!r.has(i)&&(r.add(i),n.push(i),n.length>=50))return n}return n}function Yt(t){return(t.role??(typeof t.data.role=="string"?t.data.role:"")).toLowerCase()}function yo(t,e,n){if(n.type==="decision")return{kind:"decision",confidence:1};if(n.type==="todo")return{kind:"todo",confidence:1};let r=e==="user"||n.type==="user_prompt",s=e==="assistant"||n.type==="assistant_message";return r&&E(t,Xt)?{kind:"rule",confidence:.98}:r&&E(t,so)?{kind:"rule",confidence:.92}:E(t,oo)?{kind:E(t,Gt)?"architecture":"decision",confidence:r?.93:.86}:r&&E(t,io)?{kind:"todo",confidence:.87}:E(t,Gt)&&E(t,co)?{kind:"architecture",confidence:r?.88:.82}:s&&E(t,ao)?{kind:"fix",confidence:.8}:r&&E(t,uo)&&E(t,lo)?{kind:"context",confidence:.79}:null}function ho(t){switch(t){case"rule":return"rule";case"decision":case"architecture":return"decision";case"todo":return"todo";case"fix":case"context":return"code"}}function So(t,e,n){return t==="rule"&&E(n,Xt)?"critical":t==="architecture"||t==="decision"||t==="rule"?"high":t==="fix"||t==="context"?"normal":Jt(e,n)}function Zt(t,e){let n=[],r=new Set,s=new Map;for(let o of e){let i=typeof o.data.messageId=="string"?o.data.messageId:void 0,a=Yt(o);i&&a&&s.set(i,a)}for(let o of e){let i=Yt(o),a=typeof o.data.messageId=="string"?o.data.messageId:void 0;!i&&a&&(i=s.get(a)??"");for(let c of go(o)){let u=yo(c,i,o);if(!u||u.confidence<.75)continue;let f=ho(u.kind),d=fo(c),p=w([t.projectId,u.kind,d].join("|"));if(r.has(p))continue;r.add(p);let l=o.provenance.sourcePath?[o.provenance.sourcePath]:[],m=o.sourceEventId?[o.sourceEventId]:[];n.push({version:1,fingerprint:p,projectId:t.projectId,agent:t.agent,nativeSessionId:t.nativeSessionId,sessionKey:t.sessionKey,kind:u.kind,type:f,content:c,confidence:u.confidence,importance:So(u.kind,f,c),tags:[f],provenance:{agent:t.agent,nativeSessionId:t.nativeSessionId,sessionKey:t.sessionKey,eventIds:[o.id],sourceEventIds:m,sourcePaths:l,firstSequence:o.sequence,lastSequence:o.sequence},createdAt:o.timestamp})}}return n}import{createHash as ko}from"node:crypto";var wo=["project-knowledge","implementation","continuation","session-context"],vo={"project-knowledge":"Project knowledge",implementation:"Implementation state",continuation:"Work continuation","session-context":"Session context"};function it(t){return ko("sha256").update(t).digest("hex")}function ve(t,e){return`${t}:${it(e).slice(0,24)}`}function bo(t){try{return it(JSON.stringify(t))}catch{return it(String(t))}}function F(t){let e=new Set,n=[];for(let r of t){let s=r?.replace(/\s+/gu," ").trim();if(!s)continue;let o=s.normalize("NFKC").toLowerCase();e.has(o)||(e.add(o),n.push(s))}return n}function tn(t,e=420){let n=t.replace(/\s+/gu," ").trim();return n.length<=e?n:`${n.slice(0,Math.max(0,e-1)).trimEnd()}\u2026`}function xo(t){return t==="rule"||t==="architecture"?"project-knowledge":t==="decision"||t==="fix"?"implementation":t==="todo"?"continuation":"session-context"}function en(t){return t.length===0?0:t.reduce((e,n)=>e+n,0)/t.length}function jo(t){return t.slice().sort((e,n)=>n.importanceScore-e.importanceScore||n.confidence-e.confidence||e.id.localeCompare(n.id)).slice(0,5).map(e=>tn(e.content)).join(" | ")}function Io(t){return t.slice().sort((e,n)=>n.importanceScore-e.importanceScore||n.confidence-e.confidence||e.id.localeCompare(n.id)).slice(0,6).map(e=>tn(e.content)).join(`
|
|
13
|
+
`)}function nn(t,e){let n=t.slice().sort((p,l)=>p.sequence-l.sequence||p.timestamp.localeCompare(l.timestamp)||p.id.localeCompare(l.id)),r=n.map(p=>({id:ve("raw",[p.projectId,p.agent,p.nativeSessionId,p.id,String(p.sequence)].join("|")),level:"raw",eventId:p.id,sourceEventId:p.sourceEventId,sequence:p.sequence,type:p.type,role:p.role,timestamp:p.timestamp,sourcePath:p.provenance.sourcePath,payloadDigest:bo(p.data)})),s=new Map,o=new Map;n.forEach((p,l)=>{let m=r[l];m&&(s.set(p.id,m.id),p.sourceEventId&&o.set(p.sourceEventId,m.id))});let i=e.map(p=>{let l=F([...p.provenance.eventIds.map(m=>s.get(m)),...p.provenance.sourceEventIds.map(m=>o.get(m))]);return{id:ve("fact",p.fingerprint),level:"fact",fingerprint:p.fingerprint,kind:p.kind,type:p.type,content:p.content,knowledgeClass:p.knowledgeClass,importanceScore:p.importanceScore,confidence:p.confidence,tags:F([...p.tags,"level:fact",`class:${p.knowledgeClass}`,`kind:${p.kind}`]),rawIds:l,sourcePaths:F(p.provenance.sourcePaths)}}),a=new Map;for(let p of i){let l=xo(p.kind),m=a.get(l)??[];m.push(p),a.set(l,m)}let c=[];for(let p of wo){let l=a.get(p);if(!l?.length)continue;let m=l.slice().sort((h,y)=>y.importanceScore-h.importanceScore||y.confidence-h.confidence||h.id.localeCompare(y.id)),S=m.map(h=>h.id);c.push({id:ve("scene",`${p}|${S.join("|")}`),level:"scene",kind:p,title:vo[p],summary:jo(m),factIds:S,importanceScore:Math.max(...m.map(h=>h.importanceScore)),confidence:en(m.map(h=>h.confidence)),tags:F(["level:scene",`scene:${p}`,...m.flatMap(h=>h.tags)]),sourcePaths:F(m.flatMap(h=>h.sourcePaths))})}let u=new Map(i.map(p=>[p.id,p])),f=[];for(let p of c){let m=p.factIds.map(y=>u.get(y)).filter(y=>!!y).filter(y=>(y.knowledgeClass==="permanent"||y.knowledgeClass==="task")&&y.importanceScore>=.55);if(m.length===0)continue;let S=m.some(y=>y.knowledgeClass==="permanent")?"permanent":"task",h=Io(m);f.push({id:ve("knowledge",`${p.id}|${S}|${m.map(y=>y.id).join("|")}`),level:"knowledge",knowledgeClass:S,title:p.title,content:h,sceneIds:[p.id],factIds:m.map(y=>y.id),importanceScore:Math.max(...m.map(y=>y.importanceScore)),confidence:en(m.map(y=>y.confidence)),tags:F(["level:knowledge",`class:${S}`,`scene:${p.kind}`,...m.flatMap(y=>y.tags)]),sourcePaths:F(m.flatMap(y=>y.sourcePaths))})}let d=[];for(let p of i)for(let l of p.rawIds)d.push({from:l,to:p.id,type:"supports"});for(let p of c)for(let l of p.factIds)d.push({from:l,to:p.id,type:"belongs_to"});for(let p of f)for(let l of p.sceneIds)d.push({from:l,to:p.id,type:"promotes_to"});return{schema:"toolnet.memory-hierarchy.v1",version:1,raw:r,facts:i,scenes:c,knowledge:f,links:d,stats:{raw:r.length,facts:i.length,scenes:c.length,knowledge:f.length,links:d.length}}}function be(t){return t?Math.ceil(t.length/3.5):0}function xe(t,e){if(!t)return"";if(be(t)<=e)return t;let r=Math.floor(e*3.5),s=t.slice(0,r),o=s.lastIndexOf("."),i=s.lastIndexOf(`
|
|
14
|
+
`),a=Math.max(o,i);return a>r*.7?s.slice(0,a+1):s}function D(){let t=fe(),e=process.env.TOOLNET_SESSION_SAVE||"summary",n=process.env.TOOLNET_RAW_TRANSCRIPT==="on"||e==="archive"||e==="full",r=process.env.TOOLNET_MEMORY_PROMOTION||"conservative",s=parseFloat(process.env.TOOLNET_PROMOTE_MIN_SCORE||"0.65"),o=parseInt(process.env.TOOLNET_SESSION_SUMMARY_MAX_TOKENS||"700",10),i=parseInt(process.env.TOOLNET_DURABLE_MEMORY_MAX_ITEMS_PER_SESSION||"10",10),a=n,c=process.env.TOOLNET_RAW_TRANSCRIPT_REMOTE==="on"||e==="full";return{sessionSave:e,rawTranscript:n,memoryPromotion:r,promoteMinScore:s,sessionSummaryMaxTokens:o,durableMemoryMaxItemsPerSession:i,archiveLocal:a,archiveRemote:c}}function rn(t){return(t||D()).rawTranscript}function sn(t){return(t||D()).durableMemoryMaxItemsPerSession}function on(t){return(t||D()).sessionSummaryMaxTokens}function an(t){return(t||D()).archiveRemote}function Po(t){return t.replace(/\b[A-Za-z0-9_-]{20,}\b/g,"[REDACTED]").replace(/api[_-]?key[:\s=]+[^\s]+/gi,"api_key=[REDACTED]").replace(/token[:\s=]+[^\s]+/gi,"token=[REDACTED]").replace(/secret[:\s=]+[^\s]+/gi,"secret=[REDACTED]").replace(/password[:\s=]+[^\s]+/gi,"password=[REDACTED]")}function Co(t,e){let n=t.toLowerCase(),r=.5,s=["nh\u1EDB","remember","quy t\u1EAFc","rule","t\u1EEB gi\u1EDD","from now","kh\xF4ng \u0111\u01B0\u1EE3c","must not","lu\xF4n lu\xF4n","always","never","critical","important","blocker","deploy","production","architecture"];for(let i of s)n.includes(i)&&(r+=.15);e==="rule"||e==="architecture"||e==="blocker"?r+=.2:e==="decision"||e==="deploy"?r+=.15:(e==="fix"||e==="next_action")&&(r+=.1),t.length<20?r-=.3:t.length>500&&(r-=.1);let o=[/npm (notice|warn|ERR)/i,/\d+ packages? in \d+/i,/found \d+ vulnerabilities/i,/up to date/i,/added \d+ packages/i,/^(ok|done|success|error|warning)$/i];for(let i of o)i.test(t)&&(r-=.4);return Math.max(0,Math.min(1,r))}function Ao(t,e){let n=[],r=new Set;for(let i of t){let a=i.split(`
|
|
15
|
+
`).filter(c=>c.trim());for(let c of a){let u=c.trim();if(u.length<15)continue;let f=u.toLowerCase().replace(/\s+/g," ");if(r.has(f))continue;r.add(f);let d="decision";/\b(rule|quy tắc|policy|standard|convention)\b/i.test(u)||/\b(always|never|must|should|từ giờ|không được)\b/i.test(u)?d="rule":/\b(fix|fixed|bug|issue|error|lỗi)\b/i.test(u)?d="fix":/\b(blocker|blocked|stuck|cannot|không thể)\b/i.test(u)?d="blocker":/\b(next|todo|action|task|cần làm)\b/i.test(u)?d="next_action":/\b(deploy|release|publish|ship)\b/i.test(u)?d="deploy":/\b(architecture|design|structure|pattern)\b/i.test(u)?d="architecture":/\.(ts|js|py|go|rs|java|cpp|c|h)\b/i.test(u)&&(d="file");let p=Co(u,d);if(p<.3)continue;let l=Po(u);n.push({category:d,text:l,importance:p,sourceSessionId:e})}}let s=D(),o=sn(s);return n.sort((i,a)=>a.importance-i.importance).slice(0,o)}function Mo(t){let e=D(),n=on(e),o=t.join(`
|
|
16
|
+
|
|
17
|
+
`).split(`
|
|
18
|
+
`).filter(i=>{let a=i.trim();return a.length>20&&!a.startsWith("npm")&&!a.startsWith("\u2713")&&!a.startsWith("Error:")}).slice(0,20).join(`
|
|
19
|
+
`);return xe(o,n)}function je(t,e){let r=(Array.isArray(t)?t:t.split(`
|
|
20
|
+
|
|
21
|
+
`).filter(l=>l.trim())).map(l=>typeof l=="string"?l:JSON.stringify(l)).filter(l=>l.trim()),s=Ao(r,e),o=s.filter(l=>l.category==="decision").map(l=>l.text),i=s.filter(l=>l.category==="rule").map(l=>l.text),a=s.filter(l=>l.category==="file").map(l=>l.text),c=s.filter(l=>l.category==="fix").map(l=>l.text),u=s.filter(l=>l.category==="blocker").map(l=>l.text),f=s.filter(l=>l.category==="next_action").map(l=>l.text),d=s.filter(l=>l.category==="deploy").map(l=>l.text);return{summary:Mo(r),decisions:o,projectRules:i,filesChanged:a,bugsFixed:c,commands:d,blockers:u,nextActions:f,durableFacts:s}}function $(t){let e=new Set,n=[];for(let r of t){let s=r?.replace(/\s+/g," ").trim();if(!s)continue;let o=s.normalize("NFKC").toLowerCase();e.has(o)||(e.add(o),n.push(s))}return n}function Eo(t){let e=new Map;for(let n of t){if(!n||!n.id||!Number.isFinite(n.sequence))continue;let r=n.sourceEventId?`${n.agent}:${n.sourceEventId}`:n.id,s=e.get(r);(!s||n.sequence>s.sequence)&&e.set(r,n)}return[...e.values()].sort((n,r)=>n.sequence-r.sequence||n.timestamp.localeCompare(r.timestamp))}function To(t){switch(t){case"critical":return 1;case"high":return .85;case"normal":return .6;case"temporary":return .25}}function Oo(t){let e=To(t.importance);return Math.max(0,Math.min(1,e*.75+t.confidence*.25))}function Ro(t){return t.importance==="temporary"||t.confidence<.78?"transient":t.kind==="rule"||t.kind==="architecture"?"permanent":t.kind==="decision"||t.kind==="todo"||t.kind==="fix"?"task":"session"}function No(t){let e=t.normalize("NFKC").toLowerCase().match(/[\p{L}\p{N}_./:-]+/gu)??[],n=new Set;for(let r of e)if(!(r.length<2||/^\d+$/u.test(r))&&(n.add(r),n.size>=40))break;return[...n]}function _o(t){let e=Ro(t),n=Oo(t),r=No(t.content);return{...t,knowledgeClass:e,importanceScore:n,retrievalTerms:r,tags:$([...t.tags,"level:fact",`class:${e}`,`kind:${t.kind}`])}}function $o(t){return t.map(e=>{try{return JSON.stringify({type:e.type,role:e.role,data:e.data,provenance:{sourcePath:e.provenance.sourcePath,files:e.provenance.files}})}catch{return""}}).filter(Boolean)}function Ko(t,e,n){let r=je($o(e),t.nativeSessionId),s=n.filter(u=>u.kind==="todo").map(u=>u.content),o=n.flatMap(u=>u.provenance.sourcePaths),i=n.filter(u=>u.kind==="architecture").map(u=>u.content),a=$([...s,...r.nextActions]),c=$([...r.nextActions,...s]);return{summary:r.summary,state:{task:c[0]??a[0],decisions:$(r.decisions),files:$([...r.filesChanged,...o]),todos:a,completed:$(r.bugsFixed),blockers:$(r.blockers),nextActions:c,architecture:$(i)}}}function cn(t,e){let n=Eo(e),r=Zt(t,n).map(_o),s=r.filter(f=>f.knowledgeClass!=="transient").sort((f,d)=>d.importanceScore-f.importanceScore),{summary:o,state:i}=Ko(t,n,s),a=s.map(f=>({fingerprint:f.fingerprint,kind:f.kind,knowledgeClass:f.knowledgeClass,importanceScore:f.importanceScore,content:f.content,terms:f.retrievalTerms})),c=nn(n,s),u=f=>r.filter(d=>d.knowledgeClass===f).length;return{version:2,normalizedEvents:n,summary:o,state:i,candidates:s,retrievalIndex:a,hierarchy:c,stats:{inputEvents:e.length,normalizedEvents:n.length,extractedCandidates:r.length,persistedCandidates:s.length,permanent:u("permanent"),task:u("task"),session:u("session"),transient:u("transient")}}}import{createHash as Wo}from"node:crypto";import{chmodSync as un,existsSync as Lo,mkdirSync as Fo,readFileSync as Do,renameSync as zo,writeFileSync as ln}from"node:fs";import{dirname as dn,join as Ie}from"node:path";var ut="toolnet.context-offload.v1",Bo="toolnet.context-offload-asset.v1",qo=256,Vo=new Set(["tool_call","tool_result","file_read","file_write","file_edit","command","test","artifact"]);function pn(t){return Ie(t,".toolnet","offload")}function Ho(t){return Ie(pn(t),"assets")}function fn(t){return Ie(pn(t),"graph.json")}function mn(t){Fo(t,{recursive:!0,mode:448});try{un(t,448)}catch{}}function Jo(t,e){mn(dn(t));let n=`${t}.${process.pid}.${Date.now()}.tmp`;ln(n,e,{encoding:"utf8",mode:384}),zo(n,t);try{un(t,384)}catch{}}function ct(t){return Array.isArray(t)?t.map(ct):t&&typeof t=="object"?Object.fromEntries(Object.entries(t).sort(([e],[n])=>e.localeCompare(n)).map(([e,n])=>[e,ct(n)])):t}function Go(t){return Wo("sha256").update(JSON.stringify(ct(t)),"utf8").digest("hex")}function at(){return{schema:ut,version:1,updatedAt:new Date(0).toISOString(),nodes:[]}}function Uo(t){let e=fn(t);if(!Lo(e))return at();try{let n=JSON.parse(Do(e,"utf8"));return n.schema!==ut||n.version!==1||!Array.isArray(n.nodes)?at():n}catch{return at()}}function Yo(t,e){Jo(fn(t),JSON.stringify(e,null,2)+`
|
|
22
|
+
`)}function Xo(t,e=260){if(typeof t!="string")return null;let n=t.replace(/\s+/gu," ").trim();return n?n.slice(0,e):null}function Qo(t){let e=[...t.provenance.files??[],t.provenance.sourcePath],n=[];for(let r of e){let s=Xo(r);if(!(!s||n.includes(s))&&(n.push(s),n.length===3))break}return n}function Zo(t){return`${t.agent}:${t.sourceEventId??t.id}`.replace(/\s+/gu," ").trim().slice(0,120)}function ei(t,e){mn(dn(t));try{return ln(t,e,{encoding:"utf8",flag:"wx",mode:384}),!0}catch(n){if(n.code==="EEXIST")return!1;throw n}}function ti(t,e){let n=t.nodes.find(s=>s.id===e.id),r=n?{...n,kind:e.kind,bytes:e.bytes,sourceRefs:Array.from(new Set([...n.sourceRefs,...e.sourceRefs])).slice(-8),files:Array.from(new Set([...n.files,...e.files])).slice(0,6)}:e;return{schema:ut,version:1,updatedAt:new Date().toISOString(),nodes:[...t.nodes.filter(s=>s.id!==e.id),r].slice(-qo)}}function gn(t,e){let n=Uo(t),r=0,s=0,o=0,i=[];for(let a of e){if(!Vo.has(a.type))continue;r+=1;let c=Go({type:a.type,data:a.data}),u={schema:Bo,version:1,assetId:c,event:{type:a.type,agent:a.agent,nativeSessionId:a.nativeSessionId,timestamp:a.timestamp,sourceEventId:a.sourceEventId,provenance:a.provenance,data:a.data}},f=JSON.stringify(u,null,2)+`
|
|
23
|
+
`;ei(Ie(Ho(t),`${c}.json`),f)?s+=1:o+=1,i.push(c),n=ti(n,{id:c,kind:a.type,bytes:Buffer.byteLength(f,"utf8"),createdAt:a.timestamp,sourceRefs:[Zo(a)],files:Qo(a)})}return r>0&&Yo(t,n),{eligible:r,written:s,deduped:o,graphNodes:n.nodes.length,assetIds:i}}import{createHash as ui}from"node:crypto";import{existsSync as li,readdirSync as di,readFileSync as pi}from"node:fs";import{basename as fi,join as On}from"node:path";import{randomUUID as Sn}from"node:crypto";var x=class extends Error{constructor(n,r){super(n);this.statusCode=r}statusCode};function re(t){let e=new Set,n=[];for(let r of t){let s=r.replace(/\s+/gu," ").trim();if(!s)continue;let o=s.normalize("NFKC").toLowerCase();e.has(o)||(e.add(o),n.push(s))}return n}function B(t){let e=t.normalize("NFKD").replace(/[\u0300-\u036f]/gu,"").toLowerCase().replace(/[^a-z0-9]+/gu,"-").replace(/^-+|-+$/gu,"").slice(0,120);if(!e)throw new x("Invalid Wiki slug",400);return e}function yn(t){let e=[];for(let n of t.matchAll(/\[\[([^\[\]]+)\]\]/gu)){let r=n[1]?.trim();r&&e.push(B(r))}return re(e)}function ni(t){return t.normalize("NFKC").toLowerCase().split(/[^\p{L}\p{N}_-]+/u).map(e=>e.trim()).filter(e=>e.length>=2)}function hn(t){return{id:`revision-${Sn()}`,pageId:t.id,slug:t.slug,revision:t.revision,title:t.title,...t.summary?{summary:t.summary}:{},content:t.content,tags:[...t.tags],links:[...t.links],createdAt:t.updatedAt}}function z(t){return structuredClone(t)}var Pe=class{constructor(e){this.store=e}store;state;queue=Promise.resolve();async initialize(){await this.ensureState()}async ensureState(){return this.state||(this.state=await this.store.load()),this.state}async mutate(e){let n,r=this.queue.then(async()=>{let s=await this.ensureState();n=e(s),s.updatedAt=new Date().toISOString(),await this.store.save(s)});return this.queue=r.then(()=>{},()=>{}),await r,n}async summary(){let e=await this.ensureState(),n=new Set(e.pages.flatMap(r=>r.links));return{schema:"toolnet.wiki-summary.v1",projectId:e.projectId,pages:e.pages.length,revisions:e.revisions.length,tags:re(e.pages.flatMap(r=>r.tags)).sort((r,s)=>r.localeCompare(s)),links:e.pages.reduce((r,s)=>r+s.links.length,0),orphanPages:e.pages.filter(r=>r.links.length===0&&!n.has(r.slug)).length,automatedPages:e.pages.filter(r=>r.tags.some(s=>s.startsWith("toolnet-auto-"))).length,updatedAt:e.updatedAt}}async listPages(){let e=await this.ensureState();return z([...e.pages].sort((n,r)=>n.title.localeCompare(r.title)))}async getPage(e){let n=await this.ensureState(),r=B(e),s=n.pages.find(o=>o.slug===r||o.id===e);if(!s)throw new x(`Wiki page not found: ${e}`,404);return z(s)}async createPage(e){return this.mutate(n=>{let r=e.title.trim(),s=e.content.trim();if(!r)throw new x("Wiki title is required",400);let o=B(e.slug??r);if(n.pages.some(c=>c.slug===o))throw new x(`Wiki page already exists: ${o}`,409);let i=new Date().toISOString(),a={id:`wiki-${Sn()}`,slug:o,title:r,...e.summary?.trim()?{summary:e.summary.trim()}:{},content:s,tags:re(e.tags??[]),links:yn(s),revision:1,createdAt:i,updatedAt:i};return n.pages.push(a),n.revisions.push(hn(a)),z(a)})}async updatePage(e,n){return this.mutate(r=>{let s=B(e),o=r.pages.find(i=>i.slug===s||i.id===e);if(!o)throw new x(`Wiki page not found: ${e}`,404);if(n.title!==void 0){let i=n.title.trim();if(!i)throw new x("Wiki title is required",400);o.title=i}if(n.summary!==void 0){let i=n.summary.trim();i?o.summary=i:delete o.summary}return n.content!==void 0&&(o.content=n.content.trim(),o.links=yn(o.content)),n.tags!==void 0&&(o.tags=re(n.tags)),o.revision+=1,o.updatedAt=new Date().toISOString(),r.revisions.push(hn(o)),z(o)})}async history(e){let n=await this.getPage(e),r=await this.ensureState();return z(r.revisions.filter(s=>s.pageId===n.id).sort((s,o)=>o.revision-s.revision))}async backlinks(e){let n=await this.getPage(e),r=await this.ensureState();return z(r.pages.filter(s=>s.links.includes(n.slug)).sort((s,o)=>s.title.localeCompare(o.title)))}async search(e,n=10){let r=await this.ensureState(),s=re(ni(e));if(s.length===0)return[];let o=Math.max(1,Math.min(20,Math.floor(n))),i=[];for(let a of r.pages){let c=a.title.toLowerCase(),u=a.slug.toLowerCase(),f=a.summary?.toLowerCase()??"",d=a.content.toLowerCase(),p=a.tags.map(m=>m.toLowerCase()),l=0;for(let m of s)u===m&&(l+=12),c===m&&(l+=10),c.includes(m)&&(l+=6),u.includes(m)&&(l+=5),p.some(S=>S===m)?l+=5:p.some(S=>S.includes(m))&&(l+=3),f.includes(m)&&(l+=2),d.includes(m)&&(l+=1);l>0&&i.push({page:z(a),score:l})}return i.sort((a,c)=>c.score-a.score||c.page.updatedAt.localeCompare(a.page.updatedAt)).slice(0,o)}};var kn="wiki/state.v1.json";function ri(t){let e=new Date().toISOString();return{schema:"toolnet.wiki.v1",version:1,projectId:t.id,pages:[],revisions:[],createdAt:e,updatedAt:e}}function si(t,e){let n=JSON.parse(t);if(n.schema!=="toolnet.wiki.v1"||n.version!==1||n.projectId!==e.id||!Array.isArray(n.pages)||!Array.isArray(n.revisions))throw new Error("Invalid ToolNet Wiki state");return n}var Ce=class{constructor(e,n){this.storage=e;this.project=n}storage;project;async load(){let e=await this.storage.getText(kn);if(!e){let n=ri(this.project);return await this.save(n),n}return si(e,this.project)}async save(e){await this.storage.put(kn,JSON.stringify(e,null,2),"application/json")}};import{createHash as oi,randomUUID as wn}from"node:crypto";var vn="wiki/governance.v1.json",In="toolnet.knowledge-governance.v1",bn=500,se={autoApproveThreshold:.86,criticalApproveThreshold:.94,staleAfterDays:90};function ii(t,e=0,n=1){return Math.max(e,Math.min(n,t))}function lt(t){return t.normalize("NFKC").toLowerCase().replace(/[^\p{L}\p{N}]+/gu," ").replace(/\s+/gu," ").trim()}function xn(t){return oi("sha256").update(t.normalize("NFKC").replace(/\s+/gu," ").trim()).digest("hex")}function ai(t){let e=[t.title,t.summary??"",t.content.slice(0,2e3),...t.tags].join(" ").toLowerCase();return/\b(?:architecture|security|authentication|authorization|auth|database|production|deploy|deployment|payment|billing|permission|permissions|acl|credential|secret|migration)\b/u.test(e)}function ci(t){let e=t.sourceType==="skill"?.96:t.sourceType==="memory"?.94:.88,n=t.tags.map(r=>r.toLowerCase());return(n.includes("permanent")||n.includes("task"))&&(e+=.03),t.content.length>=200&&(e+=.02),t.content.length<80&&(e-=.05),t.title.length<4&&(e-=.05),ii(e)}function jn(t){let e=new Date().toISOString();return{schema:In,version:1,projectId:t,policy:{...se},reviews:[],audit:[],createdAt:e,updatedAt:e}}function Pn(t){let e=t.autoApproveThreshold??se.autoApproveThreshold,n=t.criticalApproveThreshold??se.criticalApproveThreshold,r=t.staleAfterDays??se.staleAfterDays;if(!Number.isFinite(e)||e<.5||e>1)throw new x("Invalid autoApproveThreshold",400);if(!Number.isFinite(n)||n<.5||n>1)throw new x("Invalid criticalApproveThreshold",400);if(!Number.isInteger(r)||r<1||r>3650)throw new x("Invalid staleAfterDays",400);return{autoApproveThreshold:e,criticalApproveThreshold:n,staleAfterDays:r}}var Ae=class{constructor(e,n){this.storage=e;this.project=n}storage;project;async load(){let e=await this.storage.getText(vn);if(!e){let n=jn(this.project.id);return await this.save(n),n}try{let n=JSON.parse(e);if(n.schema!==In||n.version!==1||n.projectId!==this.project.id||!Array.isArray(n.reviews)||!Array.isArray(n.audit))throw new Error("invalid");return{...n,policy:Pn(n.policy??se)}}catch{let n=jn(this.project.id);return await this.save(n),n}}async save(e){await this.storage.put(vn,JSON.stringify(e,null,2),"application/json")}},Me=class{constructor(e){this.store=e}store;state;queue=Promise.resolve();async initialize(){await this.ensureState()}async ensureState(){return this.state||(this.state=await this.store.load()),this.state}audit(e,n,r,s={}){e.audit.push({id:wn(),action:n,principal:r,...s.reviewId?{reviewId:s.reviewId}:{},...s.sourceKey?{sourceKey:s.sourceKey}:{},timestamp:new Date().toISOString(),...s.metadata?{metadata:s.metadata}:{}}),e.audit.length>bn&&(e.audit=e.audit.slice(-bn))}async mutate(e){let n,r=this.queue.then(async()=>{let s=await this.ensureState();n=await e(s),s.updatedAt=new Date().toISOString(),await this.store.save(s)});return this.queue=r.then(()=>{},()=>{}),await r,n}async policy(){return{...(await this.ensureState()).policy}}async setPolicy(e,n){return this.mutate(r=>(r.policy=Pn({...r.policy,...e}),this.audit(r,"policy:update",n,{metadata:{...r.policy}}),{...r.policy}))}async summary(){let e=await this.ensureState(),n=r=>e.reviews.filter(s=>s.status===r).length;return{schema:"toolnet.knowledge-governance-summary.v1",projectId:e.projectId,pending:n("pending"),approved:n("approved"),rejected:n("rejected"),superseded:n("superseded"),criticalPending:e.reviews.filter(r=>r.status==="pending"&&r.risk==="critical").length,conflictPending:e.reviews.filter(r=>r.status==="pending"&&r.risk==="conflict").length,auditEvents:e.audit.length,policy:{...e.policy},updatedAt:e.updatedAt}}async listReviews(e){let n=await this.ensureState();return structuredClone(n.reviews.filter(r=>!e||r.status===e).sort((r,s)=>s.updatedAt.localeCompare(r.updatedAt)))}async auditLog(e=100){let n=await this.ensureState(),r=Math.max(1,Math.min(500,Math.floor(e)));return structuredClone(n.audit.slice(-r).reverse())}async assess(e,n){let r=await this.ensureState(),s=ci(e),o=lt(e.title),i=n.filter(f=>f.slug!==e.slug&<(f.title)===o&&xn(f.content)!==xn(e.content)).map(f=>f.slug),a=ai(e),c=[];s<r.policy.autoApproveThreshold&&c.push(`confidence:${s.toFixed(2)}`),a&&s<r.policy.criticalApproveThreshold&&c.push("critical-knowledge"),i.length>0&&c.push("conflicting-knowledge");let u=i.length>0?"conflict":a?"critical":"normal";return{confidence:s,risk:u,requiresReview:i.length>0||s<r.policy.autoApproveThreshold||a&&s<r.policy.criticalApproveThreshold,reasons:c,conflicts:i}}async gate(e,n){let r=await this.assess(e,n);return this.mutate(s=>{let o=s.reviews.find(c=>c.sourceKey===e.sourceKey&&c.digest===e.digest);if(o?.status==="approved")return{allowed:!0,mode:"review-approved",assessment:r,review:structuredClone(o)};if(o?.status==="rejected")return{allowed:!1,mode:"rejected",assessment:r,review:structuredClone(o)};if(!r.requiresReview)return this.audit(s,"knowledge:auto-approved","system",{sourceKey:e.sourceKey,metadata:{confidence:r.confidence,risk:r.risk}}),{allowed:!0,mode:"auto-approved",assessment:r};if(o?.status==="pending")return{allowed:!1,mode:"pending-review",assessment:r,review:structuredClone(o)};let i=new Date().toISOString(),a={id:wn(),sourceKey:e.sourceKey,sourceType:e.sourceType,slug:e.slug,marker:e.marker,digest:e.digest,title:e.title,...e.summary?{summary:e.summary}:{},content:e.content,tags:[...new Set([...e.tags,e.marker])],confidence:r.confidence,risk:r.risk,reasons:[...r.reasons],conflicts:[...r.conflicts],status:"pending",createdAt:i,updatedAt:i};return s.reviews.push(a),this.audit(s,"knowledge:review-requested","system",{reviewId:a.id,sourceKey:a.sourceKey,metadata:{confidence:a.confidence,risk:a.risk}}),{allowed:!1,mode:"pending-review",assessment:r,review:structuredClone(a)}})}async markApplied(e,n){await this.mutate(r=>{let s=r.reviews.find(o=>o.sourceKey===e&&o.digest===n&&o.status==="approved");s&&(s.appliedAt=new Date().toISOString(),s.updatedAt=s.appliedAt,this.audit(r,"knowledge:applied",s.reviewedBy??"system",{reviewId:s.id,sourceKey:e}))})}async decide(e,n,r){return this.mutate(async s=>{let o=s.reviews.find(u=>u.id===e);if(!o)throw new x(`Governance review not found: ${e}`,404);if(o.status!=="pending")throw new x("Governance review is already resolved",409);let i=new Date().toISOString();if(o.reviewedAt=i,o.reviewedBy=n.principal,o.updatedAt=i,n.note?.trim()&&(o.reviewNote=n.note.trim()),n.action==="reject")return o.status="rejected",this.audit(s,"knowledge:rejected",n.principal,{reviewId:e,sourceKey:o.sourceKey}),structuredClone(o);if(n.action==="supersede")return o.status="superseded",n.targetReviewId&&(o.supersededBy=n.targetReviewId),this.audit(s,"knowledge:superseded",n.principal,{reviewId:e,sourceKey:o.sourceKey,metadata:{targetReviewId:n.targetReviewId}}),structuredClone(o);if(n.action==="merge"){if(!n.targetReviewId)throw new x("targetReviewId is required for merge",400);let u=s.reviews.find(f=>f.id===n.targetReviewId);if(!u)throw new x("Merge target review not found",404);return o.status="superseded",o.mergedInto=u.id,this.audit(s,"knowledge:merged",n.principal,{reviewId:e,sourceKey:o.sourceKey,metadata:{targetReviewId:u.id}}),structuredClone(o)}o.status="approved";let c=(await r.listPages()).find(u=>u.slug===o.slug);if(c&&!c.tags.includes(o.marker))throw new x(`Wiki page '${o.slug}' is manually managed`,409);return c?await r.updatePage(o.slug,{title:o.title,summary:o.summary??"",content:o.content,tags:o.tags}):await r.createPage({slug:o.slug,title:o.title,...o.summary?{summary:o.summary}:{},content:o.content,tags:o.tags}),o.appliedAt=i,this.audit(s,"knowledge:approved",n.principal,{reviewId:e,sourceKey:o.sourceKey}),structuredClone(o)})}async quality(e){let n=await this.ensureState(),r=await e.listPages(),s=Date.now(),o=n.policy.staleAfterDays*864e5,i=r.map(f=>{let d=s-Date.parse(f.updatedAt);return{slug:f.slug,title:f.title,updatedAt:f.updatedAt,ageDays:Math.max(0,Math.floor(d/864e5)),stale:Number.isFinite(d)&&d>o}}).filter(f=>f.stale).map(({stale:f,...d})=>d),a=new Map;for(let f of r){let d=lt(f.title),p=a.get(d)??[];p.push(f),a.set(d,p)}let c=[...a.entries()].filter(([,f])=>f.length>1).map(([f,d])=>({title:f,pages:d.map(p=>p.slug)})),u=n.reviews.filter(f=>f.status==="pending");return{schema:"toolnet.knowledge-quality.v1",totalPages:r.length,automatedPages:r.filter(f=>f.tags.some(d=>d.startsWith("toolnet-auto-"))).length,manualPages:r.filter(f=>!f.tags.some(d=>d.startsWith("toolnet-auto-"))).length,stalePages:i,duplicateTitles:c,pendingReviews:u.length,lowConfidenceReviews:u.filter(f=>f.confidence<n.policy.autoApproveThreshold).length,conflicts:u.filter(f=>f.risk==="conflict").length,generatedAt:new Date().toISOString()}}};var Rn="wiki/automation.v1.json",Nn="toolnet.wiki-automation.v1",ft=8e3,Cn=new Set(["raw","rawtext","raw_text","rawtranscript","raw_transcript","transcript","messages","message","payload","prompt","response","assistantmessage","assistant_message","userprompt","user_prompt"]);function ie(t){return ui("sha256").update(JSON.stringify(t)).digest("hex")}function oe(t){if(!(!t||typeof t!="object"||Array.isArray(t)))return t}function An(t){return Array.isArray(t)?t:[]}function _n(t){return typeof t!="string"?void 0:t.replace(/\s+/gu," ").trim()||void 0}function dt(t){return Array.isArray(t)?t.map(_n).filter(e=>!!e):[]}function R(t,e){for(let n of e){let r=_n(t[n]);if(r)return r}}function ae(t){let e=new Set,n=[];for(let r of t){let s=r.replace(/\s+/gu," ").trim();if(!s)continue;let o=s.normalize("NFKC").toLowerCase();e.has(o)||(e.add(o),n.push(s))}return n}function Ee(t,e=0,n=""){if(e>3)return[];let r=n.replace(/[^a-z0-9]/giu,"").toLowerCase();if(Cn.has(r))return[];if(typeof t=="string"){let i=t.replace(/\s+/gu," ").trim();return i.length<8?[]:[i]}if(Array.isArray(t))return t.flatMap(i=>Ee(i,e+1,n));let s=oe(t);if(!s)return[];let o=[];for(let[i,a]of Object.entries(s)){let c=i.replace(/[^a-z0-9]/giu,"").toLowerCase();Cn.has(c)||o.push(...Ee(a,e+1,i))}return o}function Mn(t){let n=ae(["content","summary","text","value","statement","description","decision","task","knowledge","context","outcome","reason","rationale"].flatMap(s=>Ee(t[s],0,s)));return(n.length>0?n:ae(Ee(t))).join(`
|
|
24
|
+
|
|
25
|
+
`).slice(0,ft)}function En(t,e){return R(t,["id","key","fingerprint","knowledgeId","sceneId"])??e}function Tn(t,e){return R(t,["title","name","topic","label","task","kind","type"])??e}function mi(t){return(R(t,["knowledgeClass","class","classification","scope"])??"").toLowerCase()}function gi(t){return(R(t,["kind","sceneKind","type"])??"").toLowerCase()}function yi(t){let e=oe(t);if(!e)return[];let n=[],r=An(e.knowledge);for(let[o,i]of r.entries()){let a=oe(i);if(!a)continue;let c=mi(a);if(c==="session"||c==="transient")continue;let u=Mn(a);if(u.length<20)continue;let f=En(a,ie(a).slice(0,16)),d=Tn(a,`Durable Memory ${o+1}`);n.push({sourceKey:`memory:${f}`,sourceType:"memory",title:d,summary:R(a,["summary","description"]),content:u,tags:ae(["toolnet","auto","memory",...c?[c]:[]])})}let s=An(e.scenes);for(let[o,i]of s.entries()){let a=oe(i);if(!a)continue;let c=gi(a);if(c==="session-context")continue;let u=Mn(a);if(u.length<20)continue;let f=En(a,ie(a).slice(0,16)),d=Tn(a,`Knowledge Scene ${o+1}`);n.push({sourceKey:`scene:${f}`,sourceType:"scene",title:d,summary:R(a,["summary","description"]),content:u,tags:ae(["toolnet","auto","scene",...c?[c]:[]])})}return n}function hi(t){return On(t,".toolnet","memory","skills")}function Si(t){let e=hi(t);if(!li(e))return{candidates:[],failed:0};let n=[],r=0,s=di(e).filter(o=>o.endsWith(".json")).sort();for(let o of s)try{let i=JSON.parse(pi(On(e,o),"utf8")),a=oe(i);if(!a||a.schema!=="toolnet.skill-memory.v1")continue;let c=R(a,["id","fingerprint"])??fi(o,".json"),u=R(a,["task"])??"",f=R(a,["title"])||u||`Reusable Skill ${c.slice(0,8)}`,d=R(a,["summary"])??void 0,p=dt(a.steps),l=dt(a.verification),m=dt(a.files),S=[];u&&S.push(`## Task
|
|
26
|
+
${u}`),d&&S.push(`## Summary
|
|
27
|
+
${d}`),p.length>0&&S.push(`## Procedure
|
|
28
|
+
${p.map((y,k)=>`${k+1}. ${y}`).join(`
|
|
29
|
+
`)}`),l.length>0&&S.push(`## Verification
|
|
30
|
+
${l.map(y=>`- ${y}`).join(`
|
|
31
|
+
`)}`),m.length>0&&S.push(`## Relevant Files
|
|
32
|
+
${m.map(y=>`- \`${y}\``).join(`
|
|
33
|
+
`)}`);let h=S.join(`
|
|
34
|
+
|
|
35
|
+
`).slice(0,ft);if(h.length<20)continue;n.push({sourceKey:`skill:${c}`,sourceType:"skill",title:f,summary:d,content:h,tags:["toolnet","auto","skill","sop"]})}catch{r+=1}return{candidates:n,failed:r}}function pt(t){let e=new Date().toISOString();return{schema:Nn,version:1,projectId:t,entries:[],createdAt:e,updatedAt:e}}async function ki(t,e){let n=await t.getText(Rn);if(!n)return pt(e);try{let r=JSON.parse(n);return r.schema!==Nn||r.version!==1||r.projectId!==e||!Array.isArray(r.entries)?pt(e):r}catch{return pt(e)}}async function wi(t,e){await t.put(Rn,JSON.stringify(e,null,2),"application/json")}function vi(t){return`toolnet-auto-${ie(t).slice(0,12)}`}function bi(t){let e=B(t.title).slice(0,72),n=ie(t.sourceKey).slice(0,10);return B(`auto-${t.sourceType}-${e}-${n}`)}function xi(t){return[`> Auto-generated by ToolNet Knowledge Automation from ${t.sourceType==="skill"?"reusable Skill Memory":t.sourceType==="scene"?"semantic memory scene":"durable memory"}.`,"",t.content].join(`
|
|
36
|
+
`).slice(0,ft)}function ji(t){return ie({sourceType:t.sourceType,title:t.title,summary:t.summary,content:t.content,tags:t.tags})}function Ii(t,e){return t.tags.includes(e)}async function $n(t){let e=yi(t.hierarchy),n=Si(t.project.rootPath),r=new Map;for(let l of[...e,...n.candidates])r.set(l.sourceKey,l);let s=[...r.values()].sort((l,m)=>l.sourceKey.localeCompare(m.sourceKey)),o={schema:"toolnet.wiki-automation-result.v1",scanned:e.length+n.candidates.length,eligible:s.length,created:0,updated:0,unchanged:0,skipped:0,failed:n.failed,reviewPending:0,autoApproved:0,reviewApproved:0,pages:[]},i=new Pe(new Ce(t.storage,t.project));await i.initialize();let a=new Me(new Ae(t.storage,t.project));await a.initialize();let c=await ki(t.storage,t.project.id),u=await i.listPages(),f=new Map(u.map(l=>[l.slug,l])),d=new Map(c.entries.map(l=>[l.sourceKey,l]));for(let l of s)try{let m=vi(l.sourceKey),S=ji(l),h=d.get(l.sourceKey),y=h?.slug??bi(l),k=f.get(y);if(k&&!Ii(k,m)){o.skipped+=1;continue}let P=ae([...l.tags,m]),C=xi(l),j=await a.gate({sourceKey:l.sourceKey,sourceType:l.sourceType,slug:y,marker:m,digest:S,title:l.title,...l.summary?{summary:l.summary}:{},content:C,tags:P},[...f.values()]);if(!j.allowed){j.mode==="pending-review"?o.reviewPending+=1:o.skipped+=1;continue}j.mode==="auto-approved"?o.autoApproved+=1:j.mode==="review-approved"&&(o.reviewApproved+=1),k?h?.digest!==S?(k=await i.updatePage(y,{title:l.title,summary:l.summary??"",content:C,tags:P}),f.set(k.slug,k),o.updated+=1,o.pages.push({sourceKey:l.sourceKey,sourceType:l.sourceType,slug:k.slug,action:"updated"})):(o.unchanged+=1,o.pages.push({sourceKey:l.sourceKey,sourceType:l.sourceType,slug:y,action:"unchanged"})):(k=await i.createPage({slug:y,title:l.title,summary:l.summary,content:C,tags:P}),f.set(k.slug,k),o.created+=1,o.pages.push({sourceKey:l.sourceKey,sourceType:l.sourceType,slug:k.slug,action:"created"}));let A=new Date().toISOString(),g={sourceKey:l.sourceKey,sourceType:l.sourceType,slug:y,digest:S,marker:m,updatedAt:A},v=c.entries.findIndex(L=>L.sourceKey===l.sourceKey);v>=0?c.entries[v]=g:c.entries.push(g),d.set(l.sourceKey,g),await a.markApplied(l.sourceKey,S)}catch(m){if(m instanceof x&&m.statusCode===409){o.skipped+=1;continue}o.failed+=1}let p=new Date().toISOString();return c.updatedAt=p,c.lastRunAt=p,c.entries.sort((l,m)=>l.sourceKey.localeCompare(m.sourceKey)),await wi(t.storage,c),o}import{createHash as Pi}from"node:crypto";import{chmodSync as Wn,existsSync as Ci,mkdirSync as Ai,readFileSync as ap,readdirSync as cp,renameSync as Mi,statSync as up,writeFileSync as Ei}from"node:fs";import{join as Ln}from"node:path";var Ti="toolnet.skill-memory.v1",Kn=5,Oi=16,Ri=24,Ni=32;function _i(t){return Pi("sha256").update(t).digest("hex")}function ue(t,e=Number.MAX_SAFE_INTEGER){let n=new Set,r=[];for(let s of t){let o=s.replace(/\s+/gu," ").trim();if(!o)continue;let i=o.normalize("NFKC").toLowerCase();if(!n.has(i)&&(n.add(i),r.push(o),r.length>=e))break}return r}function mt(t,e=360){let n=t.replace(/\s+/gu," ").trim();return n.length<=e?n:n.slice(0,Math.max(0,e-1)).trimEnd()+"\u2026"}function $i(t){return t.replace(/\bBearer\s+[A-Za-z0-9._~+/=-]+/giu,"Bearer [REDACTED]").replace(/\bsk-[A-Za-z0-9_-]{8,}\b/gu,"[REDACTED]").replace(/\b(api[_-]?key|token|password|passwd|secret)\b(\s*[:=]\s*)(["']?)[^\s"'`]+/giu,"$1$2[REDACTED]")}function _(t,e=360){return t&&mt($i(t),e)||void 0}function le(t,e){for(let n of e){let r=t[n];if(typeof r=="string"&&r.trim())return r}}function Fn(t,e){for(let n of e){let r=t[n];if(typeof r=="number"&&Number.isFinite(r))return r;if(typeof r=="string"&&r.trim()&&Number.isFinite(Number(r)))return Number(r)}}function Dn(t,e){for(let n of e){let r=t[n];if(typeof r=="boolean")return r;if(typeof r=="string"){let s=r.trim().toLowerCase();if(["true","yes","pass","passed","success","succeeded","ok"].includes(s))return!0;if(["false","no","fail","failed","error"].includes(s))return!1}}}function zn(t){let e=t.data??{};if(Dn(e,["passed","pass","success","succeeded","ok"])===!1)return!0;let r=Fn(e,["exitCode","exit_code","code","statusCode"]);if(r!==void 0&&r!==0)return!0;let s=le(e,["status","result","outcome"]);return!!(s&&/\b(fail(?:ed)?|error|broken|cancelled)\b/iu.test(s))}function ce(t){let e=t.data??{};if(zn(t))return!1;if(Dn(e,["passed","pass","success","succeeded","ok"])===!0||Fn(e,["exitCode","exit_code","code","statusCode"])===0)return!0;let s=le(e,["status","result","outcome"]);return s&&/\b(pass(?:ed)?|success(?:ful)?|succeeded|ok|green|complete(?:d)?)\b/iu.test(s)?!0:t.type==="commit"||t.type==="deploy"}function Bn(t){let e=t.data??{},n=le(e,["path","file","filePath","filename","target"]);if(n)return _(n,260);let r=t.provenance?.files;return _(r?.[0],260)}function gt(t){return _(le(t.data??{},["command","cmd","script"]),420)}function V(t){return _(le(t.data??{},["name","test","suite","title","message","text","result","status"]),300)}function Ki(t){let e=[];for(let n of[...t].sort((r,s)=>r.sequence-s.sequence))if(ce(n)){if(n.type==="test"){let r=V(n)??gt(n)??"Tests passed";e.push(`Test passed: ${r}`);continue}if(n.type==="commit"){let r=V(n);e.push(r?`Commit: ${r}`:"Commit completed");continue}if(n.type==="deploy"){let r=V(n);e.push(r?`Deploy: ${r}`:"Deployment completed")}}return ue(e,10)}function Wi(t,e){let n=[];for(let r of[...t].sort((s,o)=>s.sequence-o.sequence))switch(r.type){case"file_write":case"file_edit":{let s=Bn(r);s&&n.push(`Update ${s}`);break}case"command":{if(zn(r))break;let s=gt(r);s&&n.push(`Run: ${s}`);break}case"test":{if(!ce(r))break;let s=V(r)??gt(r)??"project tests";n.push(`Verify: ${s}`);break}case"commit":{if(!ce(r))break;let s=V(r);n.push(s?`Commit: ${s}`:"Commit verified changes");break}case"deploy":{if(!ce(r))break;let s=V(r);n.push(s?`Deploy: ${s}`:"Deploy verified build");break}default:break}if(n.length===0)for(let r of e.files.slice(0,8)){let s=_(r,260);s&&n.push(`Update ${s}`)}return ue(n,Oi)}function Li(t,e){let n=[...e.files];for(let r of t){let s=Bn(r);s&&n.push(s);for(let o of r.provenance?.files??[]){let i=_(o,260);i&&n.push(i)}}return ue(n,Ri)}function Fi(t){return ue(t.filter(e=>["file_write","file_edit","command","test","commit","deploy"].includes(e.type)).map(e=>e.id),Ni)}function Di(t){return t.map(n=>n.timestamp).filter(Boolean).sort().at(-1)??new Date(0).toISOString()}function qn(t,e,n){if(e.length===0)return[];let r=Ki(e),s=ue(n.completed.map(m=>_(m,280)??""),Kn);if(!(s.length>0||e.some(m=>["test","commit","deploy"].includes(m.type)&&ce(m))))return[];let i=_(n.task,280)??_(n.nextActions[0],280),a=s.length>0?s:i?[i]:[];if(a.length===0)return[];let c=Wi(e,n);if(c.length===0)return[];let u=Li(e,n),f=Fi(e),d=Math.min(...e.map(m=>m.sequence)),p=Math.max(...e.map(m=>m.sequence)),l=Di(e);return a.slice(0,Kn).map(m=>{let S=[`Reusable procedure learned from successful task: ${m}.`,u.length>0?`Files: ${u.slice(0,6).join(", ")}.`:"",r.length>0?`Verification: ${r.slice(0,4).join("; ")}.`:""].filter(Boolean),h=JSON.stringify({projectId:t.projectId,task:m,steps:c,verification:r,files:u}),y=_i(h);return{schema:Ti,version:1,id:`skill-${y.slice(0,24)}`,fingerprint:y,projectId:t.projectId,title:mt(`SOP: ${m}`,180),task:m,summary:mt(S.join(" "),900),steps:c,verification:r,files:u,source:{agent:t.agent,nativeSessionId:t.nativeSessionId,sessionKey:t.sessionKey,firstSequence:d,lastSequence:p,eventIds:f},createdAt:l}})}function zi(t){return Ln(t.rootPath,".toolnet","memory","skills")}function Bi(t){let e=zi(t);return Ai(e,{recursive:!0,mode:448}),Wn(e,448),e}function Vn(t,e){if(e.length===0)return{written:0,deduped:0,files:[]};let n=Bi(t),r=0,s=0,o=[];for(let i of e){if(i.projectId!==t.id)throw new Error(`Skill project mismatch: ${i.projectId} != ${t.id}`);let a=Ln(n,`${i.id}.json`);if(o.push(a),Ci(a)){s+=1;continue}let c=`${a}.${process.pid}.${Date.now()}.tmp`;Ei(c,JSON.stringify(i,null,2)+`
|
|
37
|
+
`,{encoding:"utf8",mode:384}),Mi(c,a),Wn(a,384),r+=1}return{written:r,deduped:s,files:o}}function Hn(t){return String(t).padStart(12,"0")}function qi(t){return t.remotePrefix.replace("/sessions/","/memory/learned/")}var Te=class{constructor(e){this.storage=e}storage;async write(e,n,r){if(r.length===0||n.length===0)return null;let s=Math.min(...n.map(f=>f.sequence)),o=Math.max(...n.map(f=>f.sequence)),i={version:1,projectId:e.projectId,agent:e.agent,nativeSessionId:e.nativeSessionId,sessionKey:e.sessionKey,createdAt:new Date().toISOString(),firstSequence:s,lastSequence:o,candidateCount:r.length,candidates:r},a=JSON.stringify(i,null,2)+`
|
|
38
|
+
`,c=w(r.map(f=>f.fingerprint).sort().join("|")).slice(0,16),u=[qi(e),"batches",`${Hn(s)}-${Hn(o)}-${c}.json`].join("/");return await this.storage.exists(u)||await this.storage.put(u,a,"application/json"),u}};import{createHash as Vi}from"node:crypto";function Jn(t){return String(t).padStart(12,"0")}function Hi(t){return Vi("sha256").update(t).digest("hex")}function Ji(t){return t.remotePrefix.replace("/sessions/","/memory/hierarchy/")}var Oe=class{constructor(e){this.storage=e}storage;async write(e,n,r){if(n.length===0||r.facts.length===0)return null;let s=Math.min(...n.map(u=>u.sequence)),o=Math.max(...n.map(u=>u.sequence)),i={schema:"toolnet.memory-hierarchy-batch.v1",version:1,projectId:e.projectId,agent:e.agent,nativeSessionId:e.nativeSessionId,sessionKey:e.sessionKey,createdAt:new Date().toISOString(),firstSequence:s,lastSequence:o,hierarchy:r},a=Hi([...r.facts.map(u=>u.id),...r.knowledge.map(u=>u.id)].sort().join("|")).slice(0,16),c=[Ji(e),"batches",`${Jn(s)}-${Jn(o)}-${a}.json`].join("/");return await this.storage.exists(c)||await this.storage.put(c,`${JSON.stringify(i,null,2)}
|
|
39
|
+
`,"application/json"),c}};function Zi(t,e){if(!Ui(t))return{events:[],nextOffset:e};let n=Qi(t).size,r=Number.isFinite(e)?Math.max(0,e):0;if(r>n&&(r=0),r===n)return{events:[],nextOffset:n};let s=n-r,o=Buffer.alloc(s),i=Yi(t,"r");try{Xi(i,o,0,s,r)}finally{Gi(i)}let a=o.toString("utf8"),c=a.lastIndexOf(`
|
|
40
|
+
`);if(c<0)return{events:[],nextOffset:r};let u=a.slice(0,c+1);return{events:u.split(`
|
|
41
|
+
`).filter(Boolean).flatMap(d=>{try{return[JSON.parse(d)]}catch{return[]}}),nextOffset:r+Buffer.byteLength(u,"utf8")}}var Re=class{constructor(e){this.options=e;this.journal=new Te(e.storage),this.hierarchyJournal=new Oe(e.storage)}options;journal;hierarchyJournal;async learnNew(){let e=this.options.wal.loadState(),n=Number(e.sourceCursors["memory.learner.offset"]??0),r=Number.isFinite(n)?n:0,s=Zi(this.options.wal.eventsFile,r);if(s.events.length===0)return{scannedEvents:0,candidates:0,journalWritten:!1,nextOffset:s.nextOffset};let o=cn(this.options.identity,s.events),i=o.candidates,a=!1;i.length>0&&(a=!!await this.journal.write(this.options.identity,s.events,i));let c=!1;o.hierarchy.facts.length>0&&(c=!!await this.hierarchyJournal.write(this.options.identity,s.events,o.hierarchy));let u=qn(this.options.identity,s.events,o.state),f=Vn(this.options.project,u);this.options.wal.setSourceCursor("memory.pipeline.version",2),this.options.wal.setSourceCursor("memory.pipeline.normalized_events",o.stats.normalizedEvents),this.options.wal.setSourceCursor("memory.pipeline.persisted",o.stats.persistedCandidates),this.options.wal.setSourceCursor("memory.pipeline.permanent",o.stats.permanent),this.options.wal.setSourceCursor("memory.pipeline.task",o.stats.task),this.options.wal.setSourceCursor("memory.pipeline.session",o.stats.session),this.options.wal.setSourceCursor("memory.pipeline.transient",o.stats.transient),this.options.wal.setSourceCursor("memory.pipeline.hierarchy.version",o.hierarchy.version),this.options.wal.setSourceCursor("memory.pipeline.hierarchy.raw",o.hierarchy.stats.raw),this.options.wal.setSourceCursor("memory.pipeline.hierarchy.facts",o.hierarchy.stats.facts),this.options.wal.setSourceCursor("memory.pipeline.hierarchy.scenes",o.hierarchy.stats.scenes),this.options.wal.setSourceCursor("memory.pipeline.hierarchy.knowledge",o.hierarchy.stats.knowledge),this.options.wal.setSourceCursor("memory.pipeline.hierarchy.journal_written",c?1:0),this.options.wal.setSourceCursor("memory.skill.assets",u.length),this.options.wal.setSourceCursor("memory.skill.written",f.written),this.options.wal.setSourceCursor("memory.skill.deduped",f.deduped);try{let d=gn(this.options.project.rootPath,s.events);this.options.wal.setSourceCursor("memory.context_offload.eligible",d.eligible),this.options.wal.setSourceCursor("memory.context_offload.written",d.written),this.options.wal.setSourceCursor("memory.context_offload.deduped",d.deduped),this.options.wal.setSourceCursor("memory.context_offload.graph_nodes",d.graphNodes),this.options.wal.setSourceCursor("memory.context_offload.failed",0)}catch{this.options.wal.setSourceCursor("memory.context_offload.failed",1)}try{let d=await $n({project:this.options.project,storage:this.options.storage,hierarchy:o.hierarchy});this.options.wal.setSourceCursor("memory.wiki_automation.scanned",d.scanned),this.options.wal.setSourceCursor("memory.wiki_automation.eligible",d.eligible),this.options.wal.setSourceCursor("memory.wiki_automation.created",d.created),this.options.wal.setSourceCursor("memory.wiki_automation.updated",d.updated),this.options.wal.setSourceCursor("memory.wiki_automation.unchanged",d.unchanged),this.options.wal.setSourceCursor("memory.wiki_automation.skipped",d.skipped),this.options.wal.setSourceCursor("memory.wiki_automation.failed",d.failed),this.options.wal.setSourceCursor("memory.wiki_automation.review_pending",d.reviewPending),this.options.wal.setSourceCursor("memory.wiki_automation.auto_approved",d.autoApproved),this.options.wal.setSourceCursor("memory.wiki_automation.review_approved",d.reviewApproved)}catch{this.options.wal.setSourceCursor("memory.wiki_automation.failed",1)}return this.options.wal.setSourceCursor("memory.learner.offset",s.nextOffset),{scannedEvents:s.events.length,candidates:i.length,journalWritten:a,nextOffset:s.nextOffset}}};import{closeSync as ia,existsSync as aa,openSync as ca,readSync as ua,statSync as la}from"node:fs";var ea=new Set(["content","text","message","prompt","summary","description","title","reason","last_assistant_message","lastAssistantMessage"]);function J(t){return t.normalize("NFKC").replace(/\s+/g," ").replace(/^[\s>*#•-]+/u,"").trim()}function Un(t){return J(t).toLowerCase().replace(/[^\p{L}\p{N}]+/gu," ").replace(/\s+/g," ").trim()}function H(t,e,n=0){if(!(n>6)){if(typeof t=="string"){e.push(t);return}if(Array.isArray(t)){for(let r of t.slice(0,50))H(r,e,n+1);return}if(!(!t||typeof t!="object"))for(let[r,s]of Object.entries(t))(ea.has(r)||["data","payload","parts","messages"].includes(r))&&H(s,e,n+1)}}function Ne(t){return/\b(cancelled|canceled)\b|đã hủy|bỏ qua/iu.test(t)?"cancelled":/\b(blocked|blocker)\b|đang vướng|bị vướng|đang kẹt|chưa thể/iu.test(t)?"blocked":/\b(completed|complete|done|finished|passed)\b|hoàn thành|hoàn tất|đã xong|đã làm xong|\bxong\b/iu.test(t)?"completed":/\bin[\s_-]*progress\b|working on|đang làm|đang thực hiện|đang xử lý|đang triển khai/iu.test(t)?"in_progress":"pending"}function Gn(t){let e=J(t);return/^(?:đang\s+làm|đang\s+thực\s+hiện|đang\s+xử\s+lý|đang\s+triển\s+khai|hoàn\s+thành|hoàn\s+tất|đã\s+xong|đã\s+làm\s+xong|xong|pending|in[\s_-]*progress|completed|complete|done|finished|blocked|cancelled|canceled)[.!]*$/iu.test(e)}function I(t,e,n,r,s={}){let o=J(r),i=s.key??Un(o);return{version:1,id:w([t.projectId,n,i,e.id,o,s.status??"",s.order??""].join("|")).slice(0,32),projectId:t.projectId,kind:n,key:i,text:o,status:s.status,order:s.order,confidence:s.confidence??.85,occurredAt:e.timestamp,sequence:e.sequence,agent:t.agent,nativeSessionId:t.nativeSessionId,sessionKey:t.sessionKey,eventId:e.id,sourceEventId:e.sourceEventId}}function ta(t,e,n){let r=J(n);if(r.length<5||r.length>1200)return[];let s=[],o=/^(?:next|next action|next step|tiếp theo|bước tiếp theo|cần làm tiếp)\b/iu.test(r),i=r.match(/^(?:mục tiêu|goal|objective)\s*(?::|-)\s*(.+)$/iu);i?.[1]&&s.push(I(t,e,"goal",i[1],{confidence:.97}));let a=r.match(/^(?:kế hoạch|plan)\s*(?::|-)\s*(.+)$/iu);a?.[1]&&s.push(I(t,e,"plan",a[1],{confidence:.95}));let c=/\b(?:phase|giai đoạn|giai doan|stage)\s*(\d+)\s*(?:[:\-–—]\s*)?([^.;\n]{0,220})/giu,u;for(;!o&&(u=c.exec(r));){let p=Number(u[1]),l=J(u[2]??""),m=l&&!Gn(l)?`Phase ${p} - ${l}`:`Phase ${p}`;s.push(I(t,e,"phase",m,{key:`phase:${p}`,order:p,status:Ne(r),confidence:.93}))}let f=n.match(/^\s*[-*]\s*\[([ xX])\]\s*(.+)$/u);f?.[2]&&s.push(I(t,e,"task",f[2],{status:f[1].trim()?"completed":Ne(f[2]),confidence:.96}));let d=r.match(/^(?:todo|task|việc)\s*(\d+)?(?:\s*[:.\-–—]\s*|\s+)(.+)$/iu);if(d?.[2]){let p=d[1]?Number(d[1]):void 0,l=J(d[2]),m=Gn(l);s.push(I(t,e,"task",m&&p!==void 0?`TODO ${p}`:l,{key:p!==void 0?`task:${p}`:Un(l),order:p,status:Ne(r),confidence:.93}))}if(/^(?:next|next action|next step|tiếp theo|bước tiếp theo|cần làm tiếp)\b/iu.test(r)){let p=r.replace(/^(?:next|next action|next step|tiếp theo|bước tiếp theo|cần làm tiếp)\s*(?::|-)?\s*/iu,"");p&&s.push(I(t,e,"next_action",p,{confidence:.9}))}return/\b(blocker|blocked)\b|đang vướng|bị vướng|đang kẹt/iu.test(r)&&s.push(I(t,e,"blocker",r,{confidence:.9})),/\b(chú ý|lưu ý|warning|attention|cẩn thận)\b/iu.test(r)&&s.push(I(t,e,"warning",r,{confidence:.88})),/\b(chốt|quyết định|decided|decision)\b/iu.test(r)&&s.push(I(t,e,"decision",r,{confidence:.9})),s}function _e(t,e){if(e.length===0)return[];let n=[],r=new Set;function s(i){r.has(i.id)||(r.add(i.id),n.push(i))}for(let i of e){if(i.type==="decision"){let c=[];H(i.data,c);for(let u of c)s(I(t,i,"decision",u,{confidence:1}))}if(i.type==="todo"){let c=[];H(i.data,c);for(let u of c)s(I(t,i,"task",u,{status:Ne(u),confidence:1}))}if(["file_write","file_edit"].includes(i.type))for(let c of["filePath","path","file"]){let u=i.data[c];typeof u=="string"&&u&&s(I(t,i,"file",u,{confidence:1}))}if(i.type==="test"){let c=[];H(i.data,c);for(let u of c)s(I(t,i,"test",u,{confidence:1}))}let a=[];H(i.data,a);for(let c of a)for(let u of c.split(/\n+/u))for(let f of ta(t,i,u))s(f)}let o=e[e.length-1];return s(I(t,o,"session",`${t.agent}:${t.nativeSessionId}`,{key:t.sessionKey,confidence:1})),n}function Yn(t){return String(t).padStart(12,"0")}var $e=class{constructor(e){this.storage=e}storage;async write(e,n){if(n.length===0)return null;let r=Math.min(...n.map(u=>u.sequence)),s=Math.max(...n.map(u=>u.sequence)),o={version:1,projectId:e.projectId,agent:e.agent,nativeSessionId:e.nativeSessionId,sessionKey:e.sessionKey,createdAt:new Date().toISOString(),firstSequence:r,lastSequence:s,observations:n},i=JSON.stringify(o,null,2)+`
|
|
42
|
+
`,a=w(n.map(u=>u.id).sort().join("|")).slice(0,16),c=[`projects/${e.projectId}`,"work","observations",e.agent,e.nativeSessionId,`${Yn(r)}-${Yn(s)}-${a}.json`].join("/");return await this.storage.exists(c)||await this.storage.put(c,i,"application/json"),c}};import{join as Xn}from"node:path";import{mkdirSync as na}from"node:fs";function ra(t){return t.normalize("NFKC").toLowerCase().replace(/[^\p{L}\p{N}]+/gu," ").replace(/\s+/g," ").trim()}function G(t,e=20){let n=[],r=new Set;for(let s of t.slice().reverse()){let o=ra(s);if(!(!o||r.has(o))&&(r.add(o),n.push(s),n.length>=e))break}return n.reverse()}function sa(t){return t.kind==="phase"?/^Phase\s+\d+$/iu.test(t.text):t.kind==="task"?/^(?:TODO|Task|Việc)\s+\d+$/iu.test(t.text):!1}function Qn(t,e){let n=e.status??t?.status??"pending",r=n;t&&(t.status==="completed"&&n!=="completed"?r="completed":n==="pending"&&(t.status==="in_progress"||t.status==="blocked")&&(r=t.status));let s=t&&sa(e)?t.title:e.text;return{id:t?.id??w(e.key).slice(0,24),title:s,status:r,order:e.order??t?.order,confidence:Math.max(e.confidence,t?.confidence??0),updatedAt:e.occurredAt,updatedBy:{agent:e.agent,nativeSessionId:e.nativeSessionId,eventId:e.eventId}}}async function oa(t,e){let n=`projects/${t.id}/work/observations/`,r=await e.list(n),s=[];for(let o of r.filter(i=>i.key.endsWith(".json")).sort((i,a)=>i.key.localeCompare(a.key))){let i=await e.getText(o.key);if(i)try{let a=JSON.parse(i);a.version===1&&Array.isArray(a.observations)&&s.push(a)}catch{}}return s}async function Ke(t,e){let r=(await oa(t,e)).flatMap(g=>g.observations).sort((g,v)=>{let L=g.occurredAt.localeCompare(v.occurredAt);if(L!==0)return L;let Ct=g.sequence-v.sequence;return Ct!==0?Ct:g.id.localeCompare(v.id)}),s=new Map,o=new Map,i,a,c,u=[],f=[],d=[],p=[],l=[],m=[];for(let g of r)switch(g.kind){case"goal":i=g.text;break;case"plan":a=g.text;break;case"phase":s.set(g.key,Qn(s.get(g.key),g));break;case"task":o.set(g.key,Qn(o.get(g.key),g));break;case"decision":u.push(g.text);break;case"blocker":f.push(g.text);break;case"warning":d.push(g.text);break;case"next_action":p.push(g.text);break;case"file":l.push(g.text);break;case"test":m.push(g.text);break;case"session":c={agent:g.agent,nativeSessionId:g.nativeSessionId,sessionKey:g.sessionKey,updatedAt:g.occurredAt};break}let S=Array.from(s.values()).sort((g,v)=>(g.order??Number.MAX_SAFE_INTEGER)-(v.order??Number.MAX_SAFE_INTEGER)),h=Array.from(o.values()).sort((g,v)=>(g.order??Number.MAX_SAFE_INTEGER)-(v.order??Number.MAX_SAFE_INTEGER)),y=S.find(g=>g.status==="in_progress")??S.find(g=>g.status==="blocked")??S.find(g=>g.status==="pending"),k=h.find(g=>g.status==="in_progress")??h.find(g=>g.status==="blocked")??h.find(g=>g.status==="pending"),P=G([...p,...k?[k.title]:[],...!k&&y?[y.title]:[],...h.filter(g=>g.status==="pending").slice(0,5).map(g=>g.title)],8),C=G([...f,...S.filter(g=>g.status==="blocked").map(g=>g.title),...h.filter(g=>g.status==="blocked").map(g=>g.title)],20),j={version:1,projectId:t.id,projectName:t.name,goal:i,plan:a,phases:S,tasks:h,decisions:G(u,20),blockers:C,warnings:G(d,20),nextActions:P,filesTouched:G(l,30),tests:G(m,20),currentPhase:y,currentTask:k,progress:{phasesTotal:S.length,phasesCompleted:S.filter(g=>g.status==="completed").length,tasksTotal:h.length,tasksCompleted:h.filter(g=>g.status==="completed").length,blocked:S.filter(g=>g.status==="blocked").length+h.filter(g=>g.status==="blocked").length},lastSession:c,updatedAt:r.length?r[r.length-1].occurredAt:new Date().toISOString()},A=Xn(t.rootPath,".toolnet","work");return na(A,{recursive:!0}),M(Xn(A,"current.json"),j),await e.put(`projects/${t.id}/work/current.json`,JSON.stringify(j,null,2)+`
|
|
43
|
+
`,"application/json"),j}async function We(t,e){let n=await e.getText(`projects/${t.id}/work/current.json`);if(!n)return null;try{return JSON.parse(n)}catch{return null}}function da(t,e){if(!aa(t))return{events:[],nextOffset:e};let n=la(t).size,r=Number.isFinite(e)?Math.max(0,e):0;if(r>n&&(r=0),r===n)return{events:[],nextOffset:n};let s=n-r,o=Buffer.alloc(s),i=ca(t,"r");try{ua(i,o,0,s,r)}finally{ia(i)}let a=o.toString("utf8"),c=a.lastIndexOf(`
|
|
44
|
+
`);if(c<0)return{events:[],nextOffset:r};let u=a.slice(0,c+1);return{events:u.split(`
|
|
45
|
+
`).filter(Boolean).flatMap(d=>{try{return[JSON.parse(d)]}catch{return[]}}),nextOffset:r+Buffer.byteLength(u,"utf8")}}var Le=class{constructor(e){this.options=e;this.journal=new $e(e.storage)}options;journal;async learnNew(){let e=this.options.wal.loadState(),n=Number(e.sourceCursors["work.continuity.offset"]??0),r=da(this.options.wal.eventsFile,Number.isFinite(n)?n:0);if(r.events.length===0)return{scannedEvents:0,observations:0,journalWritten:!1,reconciled:!1,nextOffset:r.nextOffset};let s=_e(this.options.identity,r.events),o=!1,i=!1;return s.length>0&&(o=!!await this.journal.write(this.options.identity,s),o&&(await Ke(this.options.project,this.options.storage),i=!0)),this.options.wal.setSourceCursor("work.continuity.offset",r.nextOffset),{scannedEvents:r.events.length,observations:s.length,journalWritten:o,reconciled:i,nextOffset:r.nextOffset}}};import{closeSync as wa,existsSync as va,openSync as ba,readSync as xa,statSync as ja}from"node:fs";var pa=new Set(["content","text","message","prompt","summary","description","title","reason","last_assistant_message","lastAssistantMessage"]);function U(t){return t.normalize("NFKC").replace(/\s+/g," ").replace(/^[\s>*#•-]+/u,"").trim()}function yt(t,e,n=0){if(!(n>6)){if(typeof t=="string"){e.push(t);return}if(Array.isArray(t)){for(let r of t.slice(0,50))yt(r,e,n+1);return}if(!(!t||typeof t!="object"))for(let[r,s]of Object.entries(t))(pa.has(r)||["data","payload","parts","messages"].includes(r))&&yt(s,e,n+1)}}function T(t,e,n,r,s,o=.95){let i=U(r);return{version:1,id:w([t.projectId,n,s.type,s.key??"",i.toLowerCase(),e.id].join("|")).slice(0,32),projectId:t.projectId,kind:n,value:i,scope:s.type,scopeKey:s.key,scopeOrder:s.order,confidence:o,evidence:{agent:t.agent,nativeSessionId:t.nativeSessionId,sessionKey:t.sessionKey,eventId:e.id,sourceEventId:e.sourceEventId,sequence:e.sequence,occurredAt:e.timestamp}}}function N(t,e){let n=t.toLowerCase();for(let r of e){let s=r.toLowerCase();if(n.startsWith(`${s}:`)||n.startsWith(`${s} -`)||n.startsWith(`${s} \u2014`))return U(t.slice(r.length+1))}return null}function fa(t){let e=t.trimStart();return e.startsWith("- ")||e.startsWith("* ")||/^\d+[.)]\s+/u.test(e)}function ma(t){return U(t.trim().replace(/^[-*]\s+/u,"").replace(/^\d+[.)]\s+/u,""))}function Zn(t,e){let n=[],r=new Set;function s(o){!o.value||o.value.length<3||r.has(o.id)||(r.add(o.id),n.push(o))}for(let o of e){let i=[];yt(o.data,i);for(let a of i){let c={type:"project"},u=null;for(let f of a.split(/\r?\n/u)){let d=U(f);if(!d){u=null;continue}let p=d.match(/^(?:phase|giai đoạn|giai doan|stage)\s*(\d+)\s*(?:[:\-–—]\s*)?(.*)$/iu);if(p){let v=Number(p[1]);c={type:"phase",key:`phase:${v}`,order:v,title:U(p[2]??"")},u=null;continue}let l=d.match(/^(?:todo|task|việc)\s*(\d+)\s*(?:[:\-–—]\s*)?(.*)$/iu);if(l){let v=Number(l[1]);c={type:"task",key:`task:${v}`,order:v,title:U(l[2]??"")},u=null;continue}let m=N(d,["mission","s\u1EE9 m\u1EC7nh","m\u1EE5c ti\xEAu t\u1ED5ng th\u1EC3","m\u1EE5c ti\xEAu project","project goal"]);if(m){s(T(t,o,"mission",m,{type:"project"},.99)),u=null;continue}let S=N(d,["current objective","active objective","m\u1EE5c ti\xEAu hi\u1EC7n t\u1EA1i","objective","m\u1EE5c ti\xEAu","m\u1EE5c \u0111\xEDch"]);if(S){s(T(t,o,c.type==="phase"?"phase_objective":"objective",S,c,.98)),u=null;continue}let h=N(d,["why","why this","why this phase","reason","rationale","v\xEC sao","l\xFD do","t\u1EA1i sao","\xFD ngh\u0129a"]);if(h){s(T(t,o,c.type==="phase"?"phase_why":"why",h,c,.98)),u=null;continue}let y=N(d,["desired outcome","final outcome","k\u1EBFt qu\u1EA3 cu\u1ED1i","k\u1EBFt qu\u1EA3 mong mu\u1ED1n","m\u1EE5c ti\xEAu cu\u1ED1i"]);if(y){s(T(t,o,"desired_outcome",y,{type:"project"},.98)),u=null;continue}let k=N(d,["plan rationale","approach rationale","why this approach","t\u1EA1i sao ch\u1ECDn h\u01B0\u1EDBng n\xE0y","l\xFD do ch\u1ECDn h\u01B0\u1EDBng n\xE0y","l\xFD do ch\u1ECDn ki\u1EBFn tr\xFAc"]);if(k){s(T(t,o,"plan_rationale",k,{type:"project"},.98)),u=null;continue}let P=N(d,["deliverable","output","k\u1EBFt qu\u1EA3 c\u1EA7n \u0111\u1EA1t","ph\u1EA3i t\u1EA1o ra","\u0111\u1EA7u ra"]);if(P){s(T(t,o,"phase_deliverable",P,c,.97)),u=null;continue}let C=N(d,["acceptance criteria","definition of done","done khi","ho\xE0n th\xE0nh khi","ti\xEAu ch\xED ho\xE0n th\xE0nh"]);if(C){s(T(t,o,"acceptance_criterion",C,c,.98)),u="acceptance_criterion";continue}let j=N(d,["depends on","dependency","dependencies","ph\u1EE5 thu\u1ED9c","c\u1EA7n c\xF3 tr\u01B0\u1EDBc"]);if(j){s(T(t,o,"dependency",j,c,.97)),u="dependency";continue}let A=N(d,["open question","open questions","c\xE2u h\u1ECFi m\u1EDF","ch\u01B0a quy\u1EBFt \u0111\u1ECBnh","ch\u01B0a r\xF5"]);if(A){s(T(t,o,"open_question",A,c,.95)),u="open_question";continue}let g=N(d,["constraint","constraints","r\xE0ng bu\u1ED9c","gi\u1EDBi h\u1EA1n"]);if(g){s(T(t,o,"constraint",g,c,.97)),u="constraint";continue}if(/^(?:acceptance criteria|definition of done|done khi|tiêu chí hoàn thành)\s*:?\s*$/iu.test(d)){u="acceptance_criterion";continue}if(/^(?:dependencies|dependency|phụ thuộc)\s*:?\s*$/iu.test(d)){u="dependency";continue}if(/^(?:open questions|open question|câu hỏi mở)\s*:?\s*$/iu.test(d)){u="open_question";continue}if(/^(?:constraints|constraint|ràng buộc)\s*:?\s*$/iu.test(d)){u="constraint";continue}if(u&&fa(f)){s(T(t,o,u,ma(f),c,.96));continue}u=null}}}return n}function er(t){return String(t).padStart(12,"0")}var Fe=class{constructor(e){this.storage=e}storage;async write(e,n){if(n.length===0)return null;let r=Math.min(...n.map(c=>c.evidence.sequence)),s=Math.max(...n.map(c=>c.evidence.sequence)),o={version:1,projectId:e.projectId,agent:e.agent,nativeSessionId:e.nativeSessionId,sessionKey:e.sessionKey,firstSequence:r,lastSequence:s,createdAt:new Date().toISOString(),observations:n},i=w(n.map(c=>c.id).sort().join("|")).slice(0,16),a=[`projects/${e.projectId}`,"work","semantic","observations",e.agent,e.nativeSessionId,`${er(r)}-${er(s)}-${i}.json`].join("/");return await this.storage.exists(a)||await this.storage.put(a,JSON.stringify(o,null,2)+`
|
|
46
|
+
`,"application/json"),a}};import{mkdirSync as ga}from"node:fs";import{join as tr}from"node:path";function ya(t){return{value:t.value,confidence:t.confidence,evidence:t.evidence}}function ha(t,e){if(!e)return!0;let n=t.evidence.occurredAt.localeCompare(e.evidence.occurredAt);return n!==0?n>0:t.evidence.sessionKey===e.evidence.sessionKey?t.evidence.sequence>=e.evidence.sequence:t.confidence>=e.confidence}function K(t,e){return ha(e,t)?e:t}function W(t,e=30){let n=new Set,r=[];for(let s of t){let o=s.value.normalize("NFKC").toLowerCase().replace(/\s+/g," ").trim();!o||n.has(o)||(n.add(o),r.push(s))}return r.slice(-e)}async function Sa(t,e){let n=`projects/${t.id}/work/semantic/observations/`,r=await e.list(n),s=[];for(let o of r.filter(i=>i.key.endsWith(".json")).sort((i,a)=>i.key.localeCompare(a.key))){let i=await e.getText(o.key);if(i)try{let a=JSON.parse(i);a.version===1&&Array.isArray(a.observations)&&s.push(a)}catch{}}return s}function ka(t){return{key:t.scopeKey??`phase:${t.scopeOrder??0}`,order:t.scopeOrder??0,acceptanceCriteria:[],dependencies:[],openQuestions:[],constraints:[],notes:[]}}async function nr(t,e){let r=(await Sa(t,e)).flatMap(S=>S.observations).sort((S,h)=>{let y=S.evidence.occurredAt.localeCompare(h.evidence.occurredAt);return y!==0?y:S.evidence.sessionKey===h.evidence.sessionKey?S.evidence.sequence-h.evidence.sequence:S.id.localeCompare(h.id)}),s,o,i,a,c,u=new Map,f=[],d=[],p=[];for(let S of r){let h=ya(S);if(S.scope==="phase"&&S.scopeKey){let y=u.get(S.scopeKey)??ka(S);switch(S.kind){case"phase_objective":y.objective=K(y.objective,h);break;case"phase_why":y.why=K(y.why,h);break;case"phase_deliverable":y.deliverable=K(y.deliverable,h);break;case"acceptance_criterion":y.acceptanceCriteria.push(h);break;case"dependency":y.dependencies.push(h);break;case"open_question":y.openQuestions.push(h);break;case"constraint":y.constraints.push(h);break;case"note":y.notes.push(h);break}u.set(y.key,y);continue}switch(S.kind){case"mission":s=K(s,h);break;case"objective":o=K(o,h);break;case"why":i=K(i,h);break;case"desired_outcome":a=K(a,h);break;case"plan_rationale":c=K(c,h);break;case"open_question":f.push(h);break;case"constraint":d.push(h);break;case"note":p.push(h);break}}for(let S of u.values())S.acceptanceCriteria=W(S.acceptanceCriteria,20),S.dependencies=W(S.dependencies,15),S.openQuestions=W(S.openQuestions,15),S.constraints=W(S.constraints,15),S.notes=W(S.notes,20);let l={version:1,projectId:t.id,projectName:t.name,mission:s,activeObjective:o,why:i,desiredOutcome:a,planRationale:c,phases:Array.from(u.values()).sort((S,h)=>S.order-h.order),openQuestions:W(f,20),constraints:W(d,20),notes:W(p,20),updatedAt:r.length?r[r.length-1].evidence.occurredAt:new Date().toISOString()},m=tr(t.rootPath,".toolnet","work");return ga(m,{recursive:!0}),M(tr(m,"semantic-current.json"),l),await e.put(`projects/${t.id}/work/semantic/current.json`,JSON.stringify(l,null,2)+`
|
|
47
|
+
`,"application/json"),l}async function rr(t,e){let n=await e.getText(`projects/${t.id}/work/semantic/current.json`);if(!n)return null;try{return JSON.parse(n)}catch{return null}}function Ia(t,e){if(!va(t))return{events:[],nextOffset:e};let n=ja(t).size,r=Number.isFinite(e)?Math.max(0,e):0;if(r>n&&(r=0),r===n)return{events:[],nextOffset:n};let s=Buffer.alloc(n-r),o=ba(t,"r");try{xa(o,s,0,s.length,r)}finally{wa(o)}let i=s.toString("utf8"),a=i.lastIndexOf(`
|
|
48
|
+
`);if(a<0)return{events:[],nextOffset:r};let c=i.slice(0,a+1);return{events:c.split(`
|
|
49
|
+
`).filter(Boolean).flatMap(u=>{try{return[JSON.parse(u)]}catch{return[]}}),nextOffset:r+Buffer.byteLength(c,"utf8")}}var De=class{constructor(e){this.options=e;this.journal=new Fe(e.storage)}options;journal;async learnNew(){let e=this.options.wal.loadState(),n=Number(e.sourceCursors["work.semantic.offset"]??0),r=Ia(this.options.wal.eventsFile,Number.isFinite(n)?n:0);if(r.events.length===0)return{scannedEvents:0,observations:0,journalWritten:!1,reconciled:!1,nextOffset:r.nextOffset};let s=Zn(this.options.identity,r.events),o=!1,i=!1;return s.length>0&&(o=!!await this.journal.write(this.options.identity,s),o&&(await nr(this.options.project,this.options.storage),i=!0)),this.options.wal.setSourceCursor("work.semantic.offset",r.nextOffset),{scannedEvents:r.events.length,observations:s.length,journalWritten:o,reconciled:i,nextOffset:r.nextOffset}}};import{mkdirSync as Ka}from"node:fs";import{join as ht}from"node:path";import{existsSync as ir,mkdirSync as Pa,readFileSync as Ca,statSync as sr,writeFileSync as Aa}from"node:fs";import{dirname as Ma,join as Ea}from"node:path";var or=64*1024,Ta=`# ToolNet Project Operating Manual
|
|
24
50
|
|
|
25
51
|
This file contains persistent instructions for AI agents working on this project.
|
|
26
52
|
|
|
@@ -63,58 +89,51 @@ Things an AI agent should always remember.
|
|
|
63
89
|
<!--
|
|
64
90
|
- [advisory] Prefer small focused files.
|
|
65
91
|
-->
|
|
66
|
-
`;function
|
|
67
|
-
|
|
68
|
-
`,"application/json")
|
|
69
|
-
|
|
70
|
-
`);if(a
|
|
71
|
-
`)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
`)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
`,{encoding:"utf8",mode:384}),
|
|
82
|
-
`)}function
|
|
83
|
-
|
|
84
|
-
`)
|
|
85
|
-
|
|
86
|
-
${s}`:s,ko(n,`${c.trim()}
|
|
87
|
-
`,{encoding:"utf8",mode:384})}import{existsSync as bo,mkdirSync as wo,readFileSync as jo,renameSync as Io,writeFileSync as Po}from"node:fs";import{dirname as Oo,join as Eo}from"node:path";function en(t){return Eo(t.rootPath,".toolnet","context","session-origin.json")}function To(t,e){wo(Oo(t),{recursive:!0});let n=`${t}.tmp-${process.pid}-${Date.now()}`;Po(n,`${JSON.stringify(e,null,2)}
|
|
88
|
-
`,{encoding:"utf8",mode:384}),Io(n,t)}function je(t,e){return[...t].filter(n=>n.kind===e).sort((n,r)=>{let s=n.occurredAt.localeCompare(r.occurredAt);return s!==0?s:n.sequence-r.sequence}).at(-1)}function tn(t,e){let n=je(e.observations,"file"),r=je(e.observations,"next_action"),s=je(e.observations,"blocker"),o=je(e.observations,"decision"),i={version:1,projectId:t.id,agent:e.agent,nativeSessionId:e.nativeSessionId,updatedAt:e.workState.updatedAt,currentTask:e.workState.currentTask?.title,currentPhase:e.workState.currentPhase?.title,lastTouchedFile:n?.text??e.workState.filesTouched.at(-1),latestNextAction:r?.text??e.workState.nextActions.at(-1),latestBlocker:s?.text??e.workState.blockers.at(-1),latestDecision:o?.text??e.workState.decisions.at(-1)};return To(en(t),i),i}function Co(t){let e=en(t);if(!bo(e))return null;try{let n=JSON.parse(jo(e,"utf8"));return n.version!==1||n.projectId!==t.id?null:n}catch{return null}}function nn(t){let e=Co(t);if(!e)return null;let n=["Previous session:",`- Agent: ${e.agent}`,`- Session: ${e.nativeSessionId}`];return e.currentPhase&&n.push(`- Current phase: ${e.currentPhase}`),e.currentTask&&n.push(`- Current task: ${e.currentTask}`),e.lastTouchedFile&&n.push(`- Last touched file: ${e.lastTouchedFile}`),e.latestBlocker&&n.push(`- Blocker: ${e.latestBlocker}`),e.latestNextAction&&n.push(`- Next action: ${e.latestNextAction}`),e.latestDecision&&n.push(`- Important decision: ${e.latestDecision}`),n.join(`
|
|
89
|
-
`)}async function Y(t){let e=t.threadId.trim();if(!e)throw new Error("Codex thread ID is required");let n=U(t.rolloutPath),r=t.cwd??n.cwd;if(r&&!ke(t.project.rootPath,r))throw new Error(`Codex thread does not belong to project: ${r}`);if(n.threadId&&n.threadId!==e)throw new Error(`Codex rollout thread mismatch: expected ${e}, got ${n.threadId}`);let s=new ye({project:t.project,storage:t.storage,agent:"codex",nativeSessionId:e,metadata:{source:"codex-rollout",rolloutPath:t.rolloutPath,cwd:r,client:t.client}}),o=s.status(),i=o.sourceCursors["codex.rollout.path"],c=Number(o.sourceCursors["codex.rollout.offset"]??0);i&&i!==t.rolloutPath&&(c=0),(!Number.isFinite(c)||c<0)&&(c=0);let u=Ft(t.rolloutPath,c),a=[];o.lastSequence===0&&a.push({type:"session_start",sourceEventId:`codex:${e}:start`,data:{threadId:e,cwd:r,rolloutPath:t.rolloutPath,sessionMeta:n.raw},provenance:{source:"codex-rollout",sourcePath:t.rolloutPath}}),i&&i!==t.rolloutPath&&a.push({type:"custom",sourceEventId:`codex:${e}:rollout-rotation:${t.rolloutPath}`,data:{event:"rollout_rotation",previousPath:i,rolloutPath:t.rolloutPath},provenance:{source:"codex-rollout"}}),u.reset&&a.push({type:"custom",sourceEventId:`codex:${e}:rollout-reset:${u.nextOffset}`,data:{event:"rollout_reset"},provenance:{source:"codex-rollout",sourcePath:t.rolloutPath}});let f=u.events.filter(d=>!Lt(d.data)).map(d=>({...d,data:ve(d.data)}));if(a.push(...f),t.idle&&f.length>0)try{let d={projectId:t.project.id,projectName:t.project.name,projectRoot:t.project.rootPath,agent:"codex",nativeSessionId:e,sessionKey:`codex:${e}`,remotePrefix:`projects/${t.project.id}/sessions/codex/${e}`,localDirectory:""},S=f.map((y,b)=>({version:1,id:`codex-work-${e}-${y.sourceSequence??b}`,sequence:b+1,projectId:t.project.id,agent:"codex",nativeSessionId:e,type:y.type,timestamp:y.timestamp??new Date().toISOString(),role:y.role,sourceEventId:y.sourceEventId,sourceSequence:y.sourceSequence,data:y.data??{},provenance:y.provenance??{}})),h=ae(d,S),g=Gt(t.project,h);Zt(t.project,g),tn(t.project,{agent:"codex",nativeSessionId:e,observations:h,workState:g})}catch{}t.idle&&t.turnId&&a.push({type:"session_idle",sourceEventId:`codex:${e}:turn:${t.turnId}:idle`,data:{turnId:t.turnId,client:t.client},provenance:{source:"codex-notify"}});let l=s.recordMany(a);if(s.setSourceCursor("codex.rollout.path",t.rolloutPath),s.setSourceCursor("codex.rollout.offset",u.nextOffset),t.turnId&&s.setSourceCursor("codex.last.turn",t.turnId),t.idle&&f.length>0)try{let d=f.map(h=>JSON.stringify(h.data)),S=zt(d,e);s.setSourceCursor("codex.session.summary",S.summary),s.setSourceCursor("codex.session.facts_count",S.durableFacts.length),Wt()&&!Bt()&&s.setSourceCursor("codex.raw_transcript.archived","local")}catch{}let m=await s.flush();return{threadId:e,imported:l.length,rolloutEvents:u.events.length,eventCount:m.eventCount,chunkCount:m.chunkCount,status:m.status,rolloutOffset:u.nextOffset,reset:u.reset}}import{existsSync as Ao}from"node:fs";import{dirname as Mo,join as Ro,parse as No,resolve as _o}from"node:path";function rn(t){let e=_o(t),n=No(e).root;for(;;){if(Ao(Ro(e,".toolnet","project.json")))return new _().detect(e);if(e===n)break;let r=Mo(e);if(r===e)break;e=r}return null}async function sn(t,e,n=100){let r=[];for(let s of Fe()){let o=U(s);if(!(!o.threadId||!o.cwd)&&ke(t.rootPath,o.cwd)&&(r.push(await Y({project:t,storage:e,threadId:o.threadId,rolloutPath:s,cwd:o.cwd})),r.length>=n))break}return r}import{existsSync as $o,mkdirSync as on,readFileSync as Fo,writeFileSync as an}from"node:fs";import{homedir as cn}from"node:os";import{dirname as un,join as ze}from"node:path";function Lo(t){let e=[],n=/"((?:\\.|[^"\\])*)"|'([^']*)'/g,r;for(;r=n.exec(t);){let s=r[1]??r[2]??"";try{e.push(r[1]!==void 0?JSON.parse(`"${s}"`):s)}catch{e.push(s)}}return e}function ln(t={}){let e=t.configFile??ze(process.env.CODEX_HOME??ze(cn(),".codex"),"config.toml"),n=t.previousFile??ze(cn(),".config","toolnet-memory","codex-notify-previous.json");on(un(e),{recursive:!0}),on(un(n),{recursive:!0});let r=$o(e)?Fo(e,"utf8"):"",s=t.binary??"toolnet-memory",o=`notify = [${JSON.stringify(s)}, "session:codex-notify"]`,i=r.split(`
|
|
90
|
-
`),c=i.findIndex(m=>/^\s*\[/.test(m));c<0&&(c=i.length);let u=-1,a=-1;for(let m=0;m<c;m+=1)if(/^\s*notify\s*=/.test(i[m])){if(u=m,a=m,i[m].includes("[")&&!i[m].includes("]"))for(;a+1<c&&(a+=1,!i[a].includes("]")););break}let f=[];if(u>=0){let m=i.slice(u,a+1).join(`
|
|
91
|
-
`);f=Lo(m),i.splice(u,a-u+1,o)}else c=i.findIndex(m=>/^\s*\[/.test(m)),c<0&&(c=i.length),i.splice(c,0,o);let l=f.length>=2&&f[f.length-1]==="session:codex-notify";return f.length>0&&!l&&an(n,JSON.stringify(f,null,2)+`
|
|
92
|
+
`;function ze(t){return Ea(t.rootPath,".toolnet","PROJECT.md")}function Oa(t){return t.normalize("NFKC").replace(/\s+/g," ").trim()}function Ra(t){let e=[],n=new Set,r=/^\s*[-*]\s+\[(enforce|advisory)\]\s+(.+?)\s*$/gimu,s;for(;s=r.exec(t);){let o=s[1].toLowerCase(),i=Oa(s[2]);if(!i)continue;let a=`${o}:${i.toLowerCase()}`;n.has(a)||(n.add(a),e.push({id:w(a).slice(0,24),mode:o,text:i,source:"manual"}))}return e}function Na(t){let e=ze(t);return ir(e)||(Pa(Ma(e),{recursive:!0}),Aa(e,Ta,{encoding:"utf8",mode:384})),e}function Be(t,e=!1){let n=e?Na(t):ze(t);if(!ir(n))return null;if(sr(n).size>or)throw new Error(`PROJECT.md exceeds ${or} bytes`);let s=Ca(n,"utf8");return{path:n,content:s,digest:w(s),rules:Ra(s),bytes:Buffer.byteLength(s,"utf8"),updatedAt:new Date(sr(n).mtimeMs).toISOString()}}function O(t,e){let n=new Set,r=[];for(let s of t){let o=s.replace(/\s+/g," ").trim();if(!o)continue;let i=o.normalize("NFKC").toLowerCase();if(!n.has(i)&&(n.add(i),r.push(o),r.length>=e))break}return r}function ar(t){if(t)return{id:t.id,title:t.title,status:t.status}}function _a(t){let e=t.slice(-10).join(`
|
|
93
|
+
`).toLowerCase();return/(?:failed|failing|failure|error|✗|❌)/u.test(e)?"failing":/(?:passed|passing|green|success|✓|✅)/u.test(e)?"passing":"unknown"}function $a(t){return w(JSON.stringify(t))}function cr(t){let{project:e,identity:n,state:r}=t,s=r.filesTouched.at(-1),o=r.phases.filter(y=>y.status==="completed").map(y=>y.title),i=r.tasks.filter(y=>y.status==="completed").map(y=>y.title),a=r.phases.filter(y=>y.status!=="completed"&&y.status!=="cancelled").map(y=>y.title),c=r.tasks.filter(y=>y.status!=="completed"&&y.status!=="cancelled").map(y=>y.title),u=O(r.nextActions,10),f=O([...c,...u],15),d=O(r.tests.slice().reverse(),10),p=O(r.filesTouched.slice().reverse(),20),l={schema:"toolnet.handoff.v2",version:2,project:{id:e.id,name:e.name},source:{agent:n.agent,nativeSessionId:n.nativeSessionId,sessionKey:n.sessionKey,sequence:t.sequence,reason:t.reason},capturedAt:t.capturedAt??new Date().toISOString(),goal:r.goal,current:{phase:ar(r.currentPhase),task:ar(r.currentTask),file:s},completed:{phases:O(o,20),tasks:O(i,30)},remaining:{phases:O(a,20),tasks:O(c,30),todos:f},nextAction:u[0],blockers:O(r.blockers.slice().reverse(),10),decisions:O(r.decisions.slice().reverse(),10),files:{current:s,recent:p},tests:{status:_a(r.tests),recent:d},attention:O(t.attention??[],20),progress:r.progress},{capturedAt:m,source:S,...h}=l;return{...l,stateDigest:$a(h)}}function Wa(t){return t.phases.some(e=>e.status==="pending"||e.status==="in_progress"||e.status==="blocked")||t.tasks.some(e=>e.status==="pending"||e.status==="in_progress"||e.status==="blocked")||t.nextActions.length>0||t.blockers.length>0}var qe=class{constructor(e){this.options=e}options;async capture(e,n){let r=await We(this.options.project,this.options.storage);if(r||(r=await Ke(this.options.project,this.options.storage)),!Wa(r))return null;let s=Be(this.options.project,!1),i=[...s?s.rules.filter(m=>m.mode==="enforce").map(m=>m.text):[],...r.warnings].slice(0,20),a=cr({project:this.options.project,identity:this.options.identity,state:r,reason:e,sequence:n,attention:i}),c=a.stateDigest,u=w([this.options.project.id,this.options.identity.sessionKey,c].join("|")).slice(0,24),f=new Date().toISOString(),d={version:1,id:u,projectId:this.options.project.id,projectName:this.options.project.name,createdAt:f,reason:e,sourceSession:{agent:this.options.identity.agent,nativeSessionId:this.options.identity.nativeSessionId,sessionKey:this.options.identity.sessionKey,sequence:n},goal:r.goal,plan:r.plan,progress:r.progress,currentPhase:r.currentPhase,currentTask:r.currentTask,incompletePhases:r.phases.filter(m=>m.status!=="completed"&&m.status!=="cancelled"),incompleteTasks:r.tasks.filter(m=>m.status!=="completed"&&m.status!=="cancelled"),nextActions:r.nextActions.slice(0,10),blockers:r.blockers.slice(0,10),decisions:r.decisions.slice(-10),warnings:r.warnings.slice(-10),attention:i,filesTouched:r.filesTouched.slice(-20),tests:r.tests.slice(-15),stateDigest:c,continuity:a},p=`projects/${this.options.project.id}/work/handoffs/${u}.json`;await this.options.storage.exists(p)||await this.options.storage.put(p,JSON.stringify(d,null,2)+`
|
|
94
|
+
`,"application/json"),await this.options.storage.put(`projects/${this.options.project.id}/work/handoff-latest.json`,JSON.stringify(d,null,2)+`
|
|
95
|
+
`,"application/json");let l=ht(this.options.project.rootPath,".toolnet","work","handoffs");return Ka(l,{recursive:!0}),M(ht(l,`${u}.json`),d),M(ht(this.options.project.rootPath,".toolnet","work","handoff-latest.json"),d),d}};async function ur(t,e){let n=await e.getText(`projects/${t.id}/work/handoff-latest.json`);if(!n)return null;try{return JSON.parse(n)}catch{return null}}var Ve=class{identity;wal;remote;sanitizer=new ee;learner;continuity;semantic;handoff;title;metadata;constructor(e){this.identity=Bt(e.project,e.agent,e.nativeSessionId),this.title=e.title,this.metadata=this.sanitizer.sanitizeValue(e.metadata??{}),this.wal=new we(this.identity),this.remote=new ke(e.storage,e.maxEventsPerChunk??100,e.maxChunkBytes??512*1024),this.learner=new Re({project:e.project,storage:e.storage,identity:this.identity,wal:this.wal}),this.continuity=new Le({project:e.project,storage:e.storage,identity:this.identity,wal:this.wal}),this.semantic=new De({project:e.project,storage:e.storage,identity:this.identity,wal:this.wal}),this.handoff=new qe({project:e.project,storage:e.storage,identity:this.identity})}sanitizeEvent(e){let n=e.provenance?{...e.provenance,metadata:this.sanitizer.sanitizeValue(e.provenance.metadata)}:void 0;return{...e,data:this.sanitizer.sanitizeValue(e.data??{}),provenance:n}}start(e={}){let n=this.wal.loadState();return this.record({type:n.lastSequence===0?"session_start":"session_resume",data:e,provenance:{source:this.identity.agent}})}record(e){return this.wal.append([this.sanitizeEvent(e)])[0]}recordMany(e){return this.wal.append(e.map(n=>this.sanitizeEvent(n)))}setSourceCursor(e,n){this.wal.setSourceCursor(e,n)}async flush(){let e=this.wal.readPending(),n=this.wal.loadState(),r=await this.remote.append(this.identity,e.events,n.sourceCursors,{title:this.title,metadata:this.metadata});if(e.events.length>0){let s=e.events[e.events.length-1];this.wal.markRemote(s.sequence,e.endOffset)}if(process.env.TOOLNET_SESSION_LEARNING!=="0")try{await this.learner.learnNew()}catch{}if(process.env.TOOLNET_WORK_CONTINUITY!=="0")try{await this.continuity.learnNew()}catch{}if(process.env.TOOLNET_SEMANTIC_CONTINUITY!=="0")try{await this.semantic.learnNew()}catch{}if(process.env.TOOLNET_SMART_HANDOFF!=="0"&&e.events.length>0)try{let s=e.events[e.events.length-1],o=["session_idle","session_end","session_compact"].includes(s.type)?s.type:"checkpoint";await this.handoff.capture(o,s.sequence)}catch{}return r}async idle(e={}){return this.record({type:"session_idle",data:e,provenance:{source:this.identity.agent}}),this.flush()}async end(e={}){return this.record({type:"session_end",data:e,provenance:{source:this.identity.agent}}),this.flush()}status(){return this.wal.loadState()}recoverRemote(){return this.remote.recover(this.identity)}};import{existsSync as La,readFileSync as Fa,readdirSync as Da,statSync as lr}from"node:fs";import{homedir as za}from"node:os";import{join as He,resolve as dr}from"node:path";function pr(t){return t&&typeof t=="object"&&!Array.isArray(t)?t:{}}function Ba(){return process.env.CODEX_HOME??He(za(),".codex")}function de(t){let e="";try{e=Fa(t,"utf8").slice(0,128*1024)}catch{return{raw:{}}}for(let n of e.split(`
|
|
96
|
+
`))if(n)try{let r=pr(JSON.parse(n));if(r.type!=="session_meta")continue;let s=pr(r.payload),o=[s.id,s.thread_id,s.threadId,s.session_id,s.sessionId].find(a=>typeof a=="string"),i=typeof s.cwd=="string"?s.cwd:void 0;return{threadId:o,cwd:i,raw:s}}catch{}return{raw:{}}}function St(t,e){if(!La(t))return;let n;try{n=Da(t,{withFileTypes:!0})}catch{return}for(let r of n){let s=He(t,r.name);if(r.isDirectory()){St(s,e);continue}r.isFile()&&r.name.endsWith(".jsonl")&&e.push(s)}}function kt(){let t=Ba(),e=[];return St(He(t,"sessions"),e),St(He(t,"archived_sessions"),e),e.sort((n,r)=>{try{return lr(r).mtimeMs-lr(n).mtimeMs}catch{return 0}})}function wt(t){let e=kt(),n=e.find(r=>r.includes(t));if(n)return n;for(let r of e)if(de(r).threadId===t)return r;return null}function Je(t,e){let n=dr(t),r=dr(e);return r===n||r.startsWith(n+"/")||n.startsWith(r+"/")}import{closeSync as qa,existsSync as Va,openSync as Ha,readSync as Ja,statSync as Ga}from"node:fs";function fr(t){return t&&typeof t=="object"&&!Array.isArray(t)?t:{}}function Ua(t){let e=t.timestamp;if(typeof e=="string"){let n=new Date(e);if(!Number.isNaN(n.getTime()))return n.toISOString()}return new Date().toISOString()}function Ya(t){let e=typeof t.type=="string"?t.type.toLowerCase():"",n=fr(t.payload),r=typeof n.type=="string"?n.type.toLowerCase():"",s=typeof n.role=="string"?n.role.toLowerCase():void 0;return e==="session_meta"?{type:"custom"}:s==="user"||r==="user_message"?{type:"user_prompt",role:"user"}:s==="assistant"||r==="agent_message"||r==="assistant_message"?{type:"assistant_message",role:"assistant"}:r.includes("function_call")||r.includes("tool_call")||r.includes("exec_command_begin")||r.includes("exec_command_start")?{type:"tool_call"}:r.includes("function_call_output")||r.includes("tool_result")||r.includes("exec_command_end")||r.includes("exec_command_complete")?{type:"tool_result"}:r.includes("error")?{type:"error"}:e.includes("compacted")||r.includes("compacted")?{type:"session_compact"}:{type:"custom",role:s}}function mr(t,e=0){if(!Va(t))return{events:[],nextOffset:e,reset:!1};let n=Ga(t).size,r=e>n,s=r?0:e;if(s>=n)return{events:[],nextOffset:n,reset:r};let o=n-s,i=Buffer.alloc(o),a=Ha(t,"r");try{Ja(a,i,0,o,s)}finally{qa(a)}let c=i.toString("utf8"),u=c.lastIndexOf(`
|
|
97
|
+
`);if(u<0)return{events:[],nextOffset:s,reset:r};let f=c.slice(0,u+1),d=0,p=[];for(let l of f.split(`
|
|
98
|
+
`)){if(!l)continue;let m=Buffer.byteLength(l+`
|
|
99
|
+
`,"utf8"),S=s+d;d+=m;let h;try{h=JSON.parse(l)}catch{continue}let y=fr(h),k=Ya(y);p.push({type:k.type,timestamp:Ua(y),role:k.role,sourceEventId:`rollout:${S}`,sourceSequence:S,data:y,provenance:{source:"codex-rollout",sourcePath:t,sourceOffset:S}})}return{events:p,nextOffset:s+Buffer.byteLength(f,"utf8"),reset:r}}var Xa=[/^<SYSTEM MESSAGE>/i,/^<EPHEMERAL MESSAGE>/i,/^<system>/i,/^<ephemeral>/i,/^ManageTask:/i,/^Task \d+ status:/i,/^Task \d+ killed/i,/^Task \d+ loading/i,/^Thought for \d+ tokens/i,/^Prioritizing Tool Usage/i,/^Tool call:/i,/^Tool response:/i,/^npm notice/i,/^npm WARN/i,/^added \d+ packages/i,/^removed \d+ packages/i,/^up to date/i,/^\d+ packages are looking for funding/i,/^run `npm fund` for details/i,/^[\d.]+%/,/^\[={10,}\]/,/^Loading\.\.\./i,/^Processing\.\.\./i,/^bash-\d+\.\d+\$/,/^\$ /,/^\s*$/],Qa=[/api[_-]?key[:\s=]+[^\s]+/gi,/token[:\s=]+[^\s]+/gi,/secret[:\s=]+[^\s]+/gi,/password[:\s=]+[^\s]+/gi,/bearer\s+[^\s]+/gi,/authorization:\s*[^\s]+/gi],Za=["decision","decided","rule","convention","architecture","pattern","fixed","resolved","implemented","created","updated","deleted","deployed","blocker","blocked","issue","bug","error","next","todo","action","requirement","must","should","important"];function ec(t){let e=t.toLowerCase();return Za.some(n=>e.includes(n))}function tc(t){if(!t.trim())return!0;for(let e of Xa)if(e.test(t))return!0;return ec(t),!1}function nc(t){let e=t;for(let n of Qa)e=e.replace(n,r=>{let s=r.split(/[:\s=]+/);return s.length>1?`${s[0]}: [REDACTED]`:"[REDACTED]"});return e}function vt(t){let e=t.trim();return e?tc(e)?{content:"",filtered:!0,reason:"noise"}:{content:nc(e),filtered:!1}:{content:"",filtered:!0,reason:"empty"}}function Ge(t){let e={};for(let[n,r]of Object.entries(t))if(typeof r=="string"){let s=vt(r);s.filtered||(e[n]=s.content)}else r&&typeof r=="object"&&!Array.isArray(r)?e[n]=Ge(r):Array.isArray(r)?e[n]=r.map(s=>{if(typeof s=="string"){let o=vt(s);return o.filtered?null:o.content}return s&&typeof s=="object"?Ge(s):s}).filter(s=>s!==null):e[n]=r;return e}function gr(t){let e=typeof t.type=="string"?t.type.toLowerCase():"";if(e.includes("system")||e.includes("ephemeral")||e==="tool_call"&&!t.result)return!0;if(t.data&&typeof t.data=="object"){let n=t.data,r=typeof n.content=="string"?n.content:"";if(r&&vt(r).filtered)return!0}return!1}import{existsSync as rc,mkdirSync as sc,readFileSync as oc,renameSync as ic,writeFileSync as ac}from"node:fs";import{dirname as cc,join as uc}from"node:path";function lc(t,e){sc(cc(t),{recursive:!0});let n=`${t}.tmp-${process.pid}-${Date.now()}`;ac(n,`${JSON.stringify(e,null,2)}
|
|
100
|
+
`,{encoding:"utf8",mode:384}),ic(n,t)}function vr(t){return uc(t.rootPath,".toolnet","work","current.json")}function dc(t){let e=vr(t);if(!rc(e))return null;try{let n=JSON.parse(oc(e,"utf8"));return n.version!==1||n.projectId!==t.id?null:n}catch{return null}}function bt(t){return t.normalize("NFKC").toLowerCase().replace(/[^\p{L}\p{N}]+/gu," ").replace(/\s+/g," ").trim()}function Y(t,e,n){let r=[],s=new Set;for(let o of[...t,...e].reverse()){let i=bt(o);if(!(!i||s.has(i))&&(s.add(i),r.push(o),r.length>=n))break}return r.reverse()}function pc(t){return t.kind==="phase"?/^Phase\s+\d+$/iu.test(t.text):t.kind==="task"?/^(?:TODO|Task|Việc)\s+\d+$/iu.test(t.text):!1}function yr(t,e){let n=e.status??t?.status??"pending",r=n;t?.status==="completed"&&n!=="completed"&&(r="completed"),t&&n==="pending"&&(t.status==="in_progress"||t.status==="blocked")&&(r=t.status);let s=t&&pc(e)?t.title:e.text;return{id:t?.id??e.id,title:s,status:r,order:e.order??t?.order,confidence:Math.max(t?.confidence??0,e.confidence),updatedAt:e.occurredAt,updatedBy:{agent:e.agent,nativeSessionId:e.nativeSessionId,eventId:e.eventId}}}function hr(t){let e=new Map;for(let n of t){let r=n.order!==void 0?`order:${n.order}`:bt(n.title);e.set(r,n)}return e}function Sr(t){return t.order!==void 0?`order:${t.order}`:bt(t.key||t.text)}function kr(t){return Array.from(t).sort((e,n)=>{let r=e.order??Number.MAX_SAFE_INTEGER,s=n.order??Number.MAX_SAFE_INTEGER;return r!==s?r-s:e.updatedAt.localeCompare(n.updatedAt)})}function wr(t){return t.find(e=>e.status==="in_progress")??t.find(e=>e.status==="blocked")??t.find(e=>e.status==="pending")}function br(t,e){let n=dc(t),r=hr(n?.phases??[]),s=hr(n?.tasks??[]),o=n?.goal,i=n?.plan,a=n?.lastSession,c=[],u=[],f=[],d=[],p=[],l=[],m=[...e].sort((g,v)=>{let L=g.occurredAt.localeCompare(v.occurredAt);return L!==0?L:g.sequence-v.sequence});for(let g of m)switch(g.kind){case"goal":o=g.text;break;case"plan":i=g.text;break;case"phase":{let v=Sr(g);r.set(v,yr(r.get(v),g));break}case"task":{let v=Sr(g);s.set(v,yr(s.get(v),g));break}case"decision":c.push(g.text);break;case"blocker":u.push(g.text);break;case"warning":f.push(g.text);break;case"next_action":d.push(g.text);break;case"file":p.push(g.text);break;case"test":l.push(g.text);break;case"session":a={agent:g.agent,nativeSessionId:g.nativeSessionId,sessionKey:g.sessionKey,updatedAt:g.occurredAt};break}let S=kr(r.values()),h=kr(s.values()),y=wr(S),k=wr(h),P=Y(n?.nextActions??[],[...d,...k?[k.title]:[],...!k&&y?[y.title]:[],...h.filter(g=>g.status==="pending").slice(0,5).map(g=>g.title)],8),C=Y(n?.blockers??[],[...u,...S.filter(g=>g.status==="blocked").map(g=>g.title),...h.filter(g=>g.status==="blocked").map(g=>g.title)],20),j=m.length>0?m[m.length-1].occurredAt:n?.updatedAt??new Date().toISOString(),A={version:1,projectId:t.id,projectName:t.name,goal:o,plan:i,phases:S,tasks:h,decisions:Y(n?.decisions??[],c,20),blockers:C,warnings:Y(n?.warnings??[],f,20),nextActions:P,filesTouched:Y(n?.filesTouched??[],p,30),tests:Y(n?.tests??[],l,20),currentPhase:y,currentTask:k,progress:{phasesTotal:S.length,phasesCompleted:S.filter(g=>g.status==="completed").length,tasksTotal:h.length,tasksCompleted:h.filter(g=>g.status==="completed").length,blocked:S.filter(g=>g.status==="blocked").length+h.filter(g=>g.status==="blocked").length},lastSession:a,updatedAt:j};return lc(vr(t),A),A}import{existsSync as fc,readFileSync as mc,writeFileSync as gc}from"node:fs";import{join as yc}from"node:path";var jr="<!-- TOOLNET:STABLE-WORK:BEGIN -->",xt="<!-- TOOLNET:STABLE-WORK:END -->";function jt(t){switch(t.status){case"completed":return"[x]";case"in_progress":return"[~]";case"blocked":return"[!]";case"cancelled":return"[-]";default:return"[ ]"}}function Ue(t,e){return e.length?["",`${t}:`,...e.map(n=>`- ${n}`)]:[]}function xr(t,e){return e.length?["",`${t}:`,...e.map(n=>`- ${jt(n)} ${n.title}`)]:[]}function hc(t){let e=[jr,"# ToolNet Stable Work State","",`Updated: ${t.updatedAt}`];return t.lastSession&&e.push(`Last agent: ${t.lastSession.agent}`,`Last session: ${t.lastSession.nativeSessionId}`),t.goal&&e.push("","Goal:",t.goal),t.plan&&e.push("","Plan:",t.plan),t.currentPhase&&e.push("","Current phase:",`${jt(t.currentPhase)} ${t.currentPhase.title}`),t.currentTask&&e.push("","Current task:",`${jt(t.currentTask)} ${t.currentTask.title}`),e.push(...xr("Phases",t.phases)),e.push(...xr("TODO / Tasks",t.tasks)),e.push(...Ue("Next actions",t.nextActions)),e.push(...Ue("Blockers",t.blockers)),e.push(...Ue("Important decisions",t.decisions)),e.push(...Ue("Files touched",t.filesTouched)),e.push("","Progress:",`- Phases: ${t.progress.phasesCompleted}/${t.progress.phasesTotal}`,`- Tasks: ${t.progress.tasksCompleted}/${t.progress.tasksTotal}`,`- Blocked: ${t.progress.blocked}`,"","Continuation:","- Resume current unfinished task.","- Never redo completed TODO/Phase unless explicitly requested.","- Ask ToolNet Memory for deeper history only when necessary.",xt),e.join(`
|
|
101
|
+
`)}function Ir(t,e){let n=yc(t.rootPath,".toolnet","current.md"),r="";if(fc(n))try{r=mc(n,"utf8")}catch{r=""}r=r.replace(/<!-- TOOLNET:AUTO-CURRENT:BEGIN -->[\s\S]*?<!-- TOOLNET:AUTO-CURRENT:END -->/gu,"");let s=hc(e),o=r.indexOf(jr),i=r.indexOf(xt),a;o>=0&&i>=o?a=[r.slice(0,o).trimEnd(),s,r.slice(i+xt.length).trimStart()].filter(Boolean).join(`
|
|
102
|
+
|
|
103
|
+
`):a=r.trim()?`${r.trim()}
|
|
104
|
+
|
|
105
|
+
${s}`:s,gc(n,`${a.trim()}
|
|
106
|
+
`,{encoding:"utf8",mode:384})}import{existsSync as Sc,mkdirSync as kc,readFileSync as wc,renameSync as vc,writeFileSync as bc}from"node:fs";import{dirname as xc,join as jc}from"node:path";function Pr(t){return jc(t.rootPath,".toolnet","context","session-origin.json")}function Ic(t,e){kc(xc(t),{recursive:!0});let n=`${t}.tmp-${process.pid}-${Date.now()}`;bc(n,`${JSON.stringify(e,null,2)}
|
|
107
|
+
`,{encoding:"utf8",mode:384}),vc(n,t)}function Ye(t,e){return[...t].filter(n=>n.kind===e).sort((n,r)=>{let s=n.occurredAt.localeCompare(r.occurredAt);return s!==0?s:n.sequence-r.sequence}).at(-1)}function Cr(t,e){let n=Ye(e.observations,"file"),r=Ye(e.observations,"next_action"),s=Ye(e.observations,"blocker"),o=Ye(e.observations,"decision"),i={version:1,projectId:t.id,agent:e.agent,nativeSessionId:e.nativeSessionId,updatedAt:e.workState.updatedAt,currentTask:e.workState.currentTask?.title,currentPhase:e.workState.currentPhase?.title,lastTouchedFile:n?.text??e.workState.filesTouched.at(-1),latestNextAction:r?.text??e.workState.nextActions.at(-1),latestBlocker:s?.text??e.workState.blockers.at(-1),latestDecision:o?.text??e.workState.decisions.at(-1)};return Ic(Pr(t),i),i}function Pc(t){let e=Pr(t);if(!Sc(e))return null;try{let n=JSON.parse(wc(e,"utf8"));return n.version!==1||n.projectId!==t.id?null:n}catch{return null}}function Ar(t){let e=Pc(t);if(!e)return null;let n=["Previous session:",`- Agent: ${e.agent}`,`- Session: ${e.nativeSessionId}`];return e.currentPhase&&n.push(`- Current phase: ${e.currentPhase}`),e.currentTask&&n.push(`- Current task: ${e.currentTask}`),e.lastTouchedFile&&n.push(`- Last touched file: ${e.lastTouchedFile}`),e.latestBlocker&&n.push(`- Blocker: ${e.latestBlocker}`),e.latestNextAction&&n.push(`- Next action: ${e.latestNextAction}`),e.latestDecision&&n.push(`- Important decision: ${e.latestDecision}`),n.join(`
|
|
108
|
+
`)}async function pe(t){let e=t.threadId.trim();if(!e)throw new Error("Codex thread ID is required");let n=de(t.rolloutPath),r=t.cwd??n.cwd;if(r&&!Je(t.project.rootPath,r))throw new Error(`Codex thread does not belong to project: ${r}`);if(n.threadId&&n.threadId!==e)throw new Error(`Codex rollout thread mismatch: expected ${e}, got ${n.threadId}`);let s=new Ve({project:t.project,storage:t.storage,agent:"codex",nativeSessionId:e,metadata:{source:"codex-rollout",rolloutPath:t.rolloutPath,cwd:r,client:t.client}}),o=s.status(),i=o.sourceCursors["codex.rollout.path"],a=Number(o.sourceCursors["codex.rollout.offset"]??0);i&&i!==t.rolloutPath&&(a=0),(!Number.isFinite(a)||a<0)&&(a=0);let c=mr(t.rolloutPath,a),u=[];o.lastSequence===0&&u.push({type:"session_start",sourceEventId:`codex:${e}:start`,data:{threadId:e,cwd:r,rolloutPath:t.rolloutPath,sessionMeta:n.raw},provenance:{source:"codex-rollout",sourcePath:t.rolloutPath}}),i&&i!==t.rolloutPath&&u.push({type:"custom",sourceEventId:`codex:${e}:rollout-rotation:${t.rolloutPath}`,data:{event:"rollout_rotation",previousPath:i,rolloutPath:t.rolloutPath},provenance:{source:"codex-rollout"}}),c.reset&&u.push({type:"custom",sourceEventId:`codex:${e}:rollout-reset:${c.nextOffset}`,data:{event:"rollout_reset"},provenance:{source:"codex-rollout",sourcePath:t.rolloutPath}});let f=c.events.filter(l=>!gr(l.data)).map(l=>({...l,data:Ge(l.data)}));if(u.push(...f),t.idle&&f.length>0)try{let l={projectId:t.project.id,projectName:t.project.name,projectRoot:t.project.rootPath,agent:"codex",nativeSessionId:e,sessionKey:`codex:${e}`,remotePrefix:`projects/${t.project.id}/sessions/codex/${e}`,localDirectory:""},m=f.map((y,k)=>({version:1,id:`codex-work-${e}-${y.sourceSequence??k}`,sequence:k+1,projectId:t.project.id,agent:"codex",nativeSessionId:e,type:y.type,timestamp:y.timestamp??new Date().toISOString(),role:y.role,sourceEventId:y.sourceEventId,sourceSequence:y.sourceSequence,data:y.data??{},provenance:y.provenance??{}})),S=_e(l,m),h=br(t.project,S);Ir(t.project,h),Cr(t.project,{agent:"codex",nativeSessionId:e,observations:S,workState:h})}catch{}t.idle&&t.turnId&&u.push({type:"session_idle",sourceEventId:`codex:${e}:turn:${t.turnId}:idle`,data:{turnId:t.turnId,client:t.client},provenance:{source:"codex-notify"}});let d=s.recordMany(u);if(s.setSourceCursor("codex.rollout.path",t.rolloutPath),s.setSourceCursor("codex.rollout.offset",c.nextOffset),t.turnId&&s.setSourceCursor("codex.last.turn",t.turnId),t.idle&&f.length>0)try{let l=f.map(S=>JSON.stringify(S.data)),m=je(l,e);s.setSourceCursor("codex.session.summary",m.summary),s.setSourceCursor("codex.session.facts_count",m.durableFacts.length),rn()&&!an()&&s.setSourceCursor("codex.raw_transcript.archived","local")}catch{}let p=await s.flush();return{threadId:e,imported:d.length,rolloutEvents:c.events.length,eventCount:p.eventCount,chunkCount:p.chunkCount,status:p.status,rolloutOffset:c.nextOffset,reset:c.reset}}import{existsSync as Cc}from"node:fs";import{dirname as Ac,join as Mc,parse as Ec,resolve as Tc}from"node:path";function Mr(t){let e=Tc(t),n=Ec(e).root;for(;;){if(Cc(Mc(e,".toolnet","project.json")))return new q().detect(e);if(e===n)break;let r=Ac(e);if(r===e)break;e=r}return null}async function Er(t,e,n=100){let r=[];for(let s of kt()){let o=de(s);if(!(!o.threadId||!o.cwd)&&Je(t.rootPath,o.cwd)&&(r.push(await pe({project:t,storage:e,threadId:o.threadId,rolloutPath:s,cwd:o.cwd})),r.length>=n))break}return r}import{existsSync as Oc,mkdirSync as Tr,readFileSync as Rc,writeFileSync as Or}from"node:fs";import{homedir as Rr}from"node:os";import{dirname as Nr,join as It}from"node:path";function Nc(t){let e=[],n=/"((?:\\.|[^"\\])*)"|'([^']*)'/g,r;for(;r=n.exec(t);){let s=r[1]??r[2]??"";try{e.push(r[1]!==void 0?JSON.parse(`"${s}"`):s)}catch{e.push(s)}}return e}function _r(t={}){let e=t.configFile??It(process.env.CODEX_HOME??It(Rr(),".codex"),"config.toml"),n=t.previousFile??It(Rr(),".config","toolnet-memory","codex-notify-previous.json");Tr(Nr(e),{recursive:!0}),Tr(Nr(n),{recursive:!0});let r=Oc(e)?Rc(e,"utf8"):"",s=t.binary??"toolnet-memory",o=`notify = [${JSON.stringify(s)}, "session:codex-notify"]`,i=r.split(`
|
|
109
|
+
`),a=i.findIndex(p=>/^\s*\[/.test(p));a<0&&(a=i.length);let c=-1,u=-1;for(let p=0;p<a;p+=1)if(/^\s*notify\s*=/.test(i[p])){if(c=p,u=p,i[p].includes("[")&&!i[p].includes("]"))for(;u+1<a&&(u+=1,!i[u].includes("]")););break}let f=[];if(c>=0){let p=i.slice(c,u+1).join(`
|
|
110
|
+
`);f=Nc(p),i.splice(c,u-c+1,o)}else a=i.findIndex(p=>/^\s*\[/.test(p)),a<0&&(a=i.length),i.splice(a,0,o);let d=f.length>=2&&f[f.length-1]==="session:codex-notify";return f.length>0&&!d&&Or(n,JSON.stringify(f,null,2)+`
|
|
92
111
|
`,{encoding:"utf8",mode:384}),r=i.join(`
|
|
93
112
|
`),r.endsWith(`
|
|
94
113
|
`)||(r+=`
|
|
95
|
-
`),
|
|
96
|
-
`,{encoding:"utf8",mode:384}),e}import{existsSync as
|
|
97
|
-
`);if(
|
|
98
|
-
`)),i=Math.max(0,e-o);if(i>=16){let
|
|
99
|
-
`).trim()}async function
|
|
114
|
+
`),Or(e,r,{encoding:"utf8",mode:384}),{configFile:e,previousFile:n,preservedPrevious:f.length>0&&!d}}import{existsSync as _c,mkdirSync as $c,readFileSync as Kc,writeFileSync as Wc}from"node:fs";import{homedir as Lc}from"node:os";import{dirname as Fc,join as $r}from"node:path";function Dc(t){return`'${t.replace(/'/g,"'\\''")}'`}function Kr(t={}){let e=t.hooksFile??$r(process.env.CODEX_HOME??$r(Lc(),".codex"),"hooks.json");$c(Fc(e),{recursive:!0});let n={};if(_c(e))try{n=JSON.parse(Kc(e,"utf8"))}catch(a){throw new Error(`Invalid existing Codex hooks.json: ${a instanceof Error?a.message:String(a)}`)}let r=n.hooks&&typeof n.hooks=="object"&&!Array.isArray(n.hooks)?n.hooks:{};n.hooks=r;let o=(Array.isArray(r.SessionStart)?r.SessionStart:[]).filter(a=>{try{return!JSON.stringify(a).includes("session:codex-context")}catch{return!0}}),i=t.binary??"toolnet-memory";return o.push({matcher:"startup|resume|clear|compact",hooks:[{type:"command",command:`${Dc(i)} session:codex-context`,timeout:15,additionalContextLimit:1e3,statusMessage:"Loading ToolNet project continuity"}]}),r.SessionStart=o,Wc(e,JSON.stringify(n,null,2)+`
|
|
115
|
+
`,{encoding:"utf8",mode:384}),e}import{existsSync as Sm,mkdirSync as Vc,readFileSync as km,writeFileSync as Hc}from"node:fs";import{dirname as Jc,join as Lr}from"node:path";function Pt(t){if(!t)return 0;let e=Array.from(t).length,n=t.trim().split(/\s+/u).filter(Boolean).length;return Math.ceil(Math.max(e/3.5,n*1.3))}function b(t,e){let n=t.replace(/\s+/g," ").trim();return n.length<=e?n:n.slice(0,Math.max(0,e-1)).trimEnd()+"\u2026"}function zc(t){let e=[],n=!1;for(let r of t.split(/\r?\n/u)){let s=r.trim();if(s.includes("<!--")&&(n=!0),n){s.includes("-->")&&(n=!1);continue}let o=s.toLowerCase();if(!(!s||s.startsWith("#")||s==="```"||o.startsWith("- [enforce]")||o.startsWith("* [enforce]")||o.startsWith("- [advisory]")||o.startsWith("* [advisory]"))&&(s=s.replace(/^[-*]\s+/u,""),s&&e.push(b(s,280)),e.length>=16))break}return e}function Bc(t){let e=[],n=[];for(let r of t.split(/\\r?\\n/u)){let s=r.trim(),o=s.toLowerCase(),a=["- [enforce]","* [enforce]","- [advisory]","* [advisory]"].find(u=>o.startsWith(u));if(!a)continue;let c=s.slice(a.length).trim();c&&(a.includes("enforce")?e.push(c):n.push(c))}return{enforce:e,advisory:n}}function qc(t,e){let n=[];for(let r of t){let s=[...n,r].join(`
|
|
116
|
+
`);if(Pt(s)<=e){n.push(r);continue}let o=Pt(n.join(`
|
|
117
|
+
`)),i=Math.max(0,e-o);if(i>=16){let a=Math.floor(i*3.2),c=b(r,a);c&&n.push(c)}break}return n.join(`
|
|
118
|
+
`).trim()}async function Wr(t){let e=Math.max(256,Math.min(2e3,t.maxTokens??1e3)),n=Be(t.project,!1),r=n?.content??"";r||(r=await t.storage.getText(`projects/${t.project.id}/project/manual.md`)??"");let s=Bc(r),o=n?n.rules.filter(l=>l.mode==="enforce").map(l=>l.text):s.enforce,i=n?n.rules.filter(l=>l.mode==="advisory").map(l=>l.text):s.advisory,a=r?zc(r):[],c=await We(t.project,t.storage),u=await rr(t.project,t.storage),f=await ur(t.project,t.storage),d=[];if(d.push("[TOOLNET PROJECT CONTEXT]"),d.push(`Project: ${t.project.name}`),d.push("Continue existing project state. Do not restart completed work unless evidence shows it is necessary."),r&&d.push(`Full operating manual: ${ze(t.project)}`),o.length){d.push("","PROJECT RULES \u2014 MUST FOLLOW");for(let l of o.slice(0,24))d.push(`- [ENFORCE] ${b(l,240)}`)}if(i.length){d.push("","PROJECT PREFERENCES");for(let l of i.slice(0,10))d.push(`- ${b(l,220)}`)}if(u&&(u.mission&&d.push("","MISSION",b(u.mission.value,420)),u.activeObjective&&d.push("","CURRENT OBJECTIVE",b(u.activeObjective.value,420)),u.why&&d.push("","WHY THIS WORK MATTERS",b(u.why.value,420)),u.desiredOutcome&&d.push("","DESIRED OUTCOME",b(u.desiredOutcome.value,420)),u.planRationale&&d.push("","WHY THIS APPROACH",b(u.planRationale.value,420))),c){if(d.push("","ACTIVE WORK"),c.goal&&d.push(`Goal: ${b(c.goal,320)}`),c.plan&&d.push(`Plan: ${b(c.plan,320)}`),d.push(`Progress: phases ${c.progress.phasesCompleted}/${c.progress.phasesTotal}; tasks ${c.progress.tasksCompleted}/${c.progress.tasksTotal}; blocked ${c.progress.blocked}`),c.currentPhase&&d.push(`Current phase: ${c.currentPhase.title} [${c.currentPhase.status}]`),c.currentPhase&&u){let l=u.phases.find(m=>m.order===c.currentPhase?.order);l&&(l.objective&&d.push(`Phase objective: ${b(l.objective.value,340)}`),l.why?d.push(`Why this phase: ${b(l.why.value,340)}`):d.push("Why this phase: not explicitly recorded. Inspect existing implementation before assuming intent."),l.deliverable&&d.push(`Deliverable: ${b(l.deliverable.value,340)}`),l.dependencies.length&&d.push(`Depends on: ${l.dependencies.slice(0,4).map(m=>b(m.value,180)).join("; ")}`),l.acceptanceCriteria.length&&(d.push("","DEFINITION OF DONE"),l.acceptanceCriteria.slice(0,6).forEach(m=>{d.push(`- ${b(m.value,260)}`)})),l.openQuestions.length&&(d.push("","OPEN QUESTIONS FOR CURRENT PHASE"),l.openQuestions.slice(0,4).forEach(m=>{d.push(`- ${b(m.value,260)}`)})))}c.currentTask&&d.push(`Current task: ${c.currentTask.title} [${c.currentTask.status}]`),c.nextActions.length&&(d.push("","NEXT ACTIONS"),c.nextActions.slice(0,6).forEach((l,m)=>{d.push(`${m+1}. ${b(l,260)}`)})),c.blockers.length&&(d.push("","BLOCKERS"),c.blockers.slice(0,5).forEach(l=>{d.push(`- ${b(l,260)}`)})),c.warnings.length&&(d.push("","ATTENTION"),c.warnings.slice(-5).forEach(l=>{d.push(`- ${b(l,260)}`)})),c.decisions.length&&(d.push("","RECENT DECISIONS"),c.decisions.slice(-5).forEach(l=>{d.push(`- ${b(l,260)}`)})),c.lastSession&&d.push("",`Last work session: ${c.lastSession.agent} / ${c.lastSession.nativeSessionId}`)}if(u&&u.openQuestions.length&&(d.push("","UNRESOLVED QUESTIONS"),u.openQuestions.slice(0,5).forEach(l=>{d.push(`- ${b(l.value,260)}`)})),f&&d.push(`Latest handoff: ${f.reason} / ${f.sourceSession.agent}`),a.length){d.push("","OPERATING NOTES");for(let l of a)d.push(`- ${l}`)}d.push("","Before changing anything: verify the current repository state and continue from the active phase/task above.");let p=qc(d,e);return{version:1,projectId:t.project.id,projectName:t.project.name,text:p,estimatedTokens:Pt(p),maxTokens:e,hasManual:!!r,hasWorkState:!!c,hasHandoff:!!f,generatedAt:new Date().toISOString()}}function Gc(t){return Lr(t.rootPath,".toolnet","context","startup.md")}function Uc(t){return Lr(t.rootPath,".toolnet","context","startup.json")}function Yc(t,e){let n=Gc(t);Vc(Jc(n),{recursive:!0}),Hc(n,e.text.endsWith(`
|
|
100
119
|
`)?e.text:e.text+`
|
|
101
|
-
`,{encoding:"utf8",mode:384}),
|
|
120
|
+
`,{encoding:"utf8",mode:384}),M(Uc(t),e)}async function Fr(t,e,n=800){let s=(await Wr({project:t,storage:e,maxTokens:n})).text;be(s)>n&&(s=xe(s,n),s+=`
|
|
102
121
|
|
|
103
122
|
[Context trimmed by ToolNet Memory token budget]
|
|
104
|
-
`);let i={version:1,projectId:t.id,projectName:t.name,text:s,digest:
|
|
123
|
+
`);let i={version:1,projectId:t.id,projectName:t.name,text:s,digest:w(s),estimatedTokens:be(s),generatedAt:new Date().toISOString()};return Yc(t,i),await e.put(`projects/${t.id}/context/startup.md`,i.text+`
|
|
105
124
|
`,"text/markdown"),await e.put(`projects/${t.id}/context/startup.json`,JSON.stringify(i,null,2)+`
|
|
106
|
-
`,"application/json"),i}import{existsSync as
|
|
125
|
+
`,"application/json"),i}import{existsSync as nu,readFileSync as ru}from"node:fs";import{join as su}from"node:path";import{existsSync as Dr,mkdirSync as Xc,readFileSync as zr,renameSync as Qc,writeFileSync as Zc}from"node:fs";import{dirname as eu,join as Br}from"node:path";function qr(t){return Br(t.rootPath,".toolnet","context","handoff.md")}function Vr(t){return Br(t.rootPath,".toolnet","context","handoff.json")}function Hr(t,e){Xc(eu(t),{recursive:!0});let n=`${t}.tmp-${process.pid}-${Date.now()}`;Zc(n,e.endsWith(`
|
|
107
126
|
`)?e:`${e}
|
|
108
|
-
`,{encoding:"utf8",mode:384}),
|
|
109
|
-
`)}function
|
|
110
|
-
`).trim();if(!n)return null;let r={version:1,projectId:t.id,projectName:t.name,text:n,digest:
|
|
127
|
+
`,{encoding:"utf8",mode:384}),Qc(n,t)}function tu(t,e){Hr(t,`${JSON.stringify(e,null,2)}
|
|
128
|
+
`)}function Jr(t,e){let n=e.replace(/\r\n/g,`
|
|
129
|
+
`).trim();if(!n)return null;let r={version:1,projectId:t.id,projectName:t.name,text:n,digest:w(n),generatedAt:new Date().toISOString()};return Hr(qr(t),n),tu(Vr(t),r),r}function Gr(t){let e=qr(t);if(!Dr(e))return null;try{let n=zr(e,"utf8").trim();if(!n)return null;let r=new Date(0).toISOString(),s=Vr(t);if(Dr(s))try{let o=JSON.parse(zr(s,"utf8"));typeof o.generatedAt=="string"&&(r=o.generatedAt)}catch{}return{version:1,projectId:t.id,projectName:t.name,text:n,digest:w(n),generatedAt:r}}catch{return null}}function ou(t){let e=su(t.rootPath,".toolnet","current.md");if(!nu(e))return null;try{return ru(e,"utf8").trim()}catch{return null}}function iu(t,e){let n=e.replace(/\r\n/g,`
|
|
111
130
|
`).trim(),r=2400,s=n.length>r?`${n.slice(0,r)}
|
|
112
131
|
|
|
113
|
-
[Current work truncated for fast handoff]`:n,o=
|
|
114
|
-
`)}function
|
|
132
|
+
[Current work truncated for fast handoff]`:n,o=Ar(t);return["# ToolNet Fast Handoff","",`Project: ${t.name}`,"",...o?[o,""]:[],s,"","---","","Continuation rule:","- Continue unfinished work; do not redo completed steps.","- Query ToolNet Memory only when deeper history is needed."].join(`
|
|
133
|
+
`)}function Ur(t){let e=ou(t);if(e===null)return{updated:!1,reason:"missing-current",chars:0};if(!e.trim())return{updated:!1,reason:"empty-current",chars:0};let n=iu(t,e);return Gr(t)?.text.trim()===n.trim()?{updated:!1,reason:"unchanged",chars:n.length}:(Jr(t,n),{updated:!0,reason:"updated",chars:n.length})}function Xe(t,e){let n=t.indexOf(e);return n>=0?t[n+1]:void 0}function Yr(t){let e=fe(),n=Kt(_t({provider:e.storage.provider,huggingface:e.storage.huggingface,localRoot:e.storage.localRoot}),{attempts:3});return new Se(n,t.id,t.name,t.remote??t.name)}async function pu(t){for(let e=0;e<10;e+=1){let n=wt(t);if(n)return n;await new Promise(r=>setTimeout(r,150))}return null}function fu(t){let e=du(lu(),".config","toolnet-memory","codex-notify-previous.json");if(au(e))try{let n=JSON.parse(cu(e,"utf8"));if(!Array.isArray(n)||n.length===0||!n.every(s=>typeof s=="string"))return;uu(n[0],[...n.slice(1),t],{detached:!0,stdio:"ignore"}).unref()}catch{}}async function mu(t){let e;try{e=JSON.parse(t)}catch{return}if(e.type!=="agent-turn-complete")return;let r=typeof e["thread-id"]=="string"?e["thread-id"]:"",s=typeof e["turn-id"]=="string"?e["turn-id"]:"",o=typeof e.cwd=="string"?e.cwd:"",i=typeof e.client=="string"?e.client:void 0;if(!r||!o)return;let a=Mr(o);if(!a)return;let c=await pu(r);if(!c)return;let u=Yr(a);await pe({project:a,storage:u,threadId:r,rolloutPath:c,cwd:o,turnId:s,client:i,idle:!0});try{await Fr(a,u,800)}catch{}try{Ur(a)}catch{}}async function gu(){let[t="help",...e]=process.argv.slice(2);if(t==="install-notify"){let o=Xe(e,"--bin"),i=_r({binary:o}),a=Kr({binary:o});console.log(`\u2705 Codex notify installed: ${i.configFile}`),i.preservedPrevious&&console.log(`\u2705 Previous notify preserved: ${i.previousFile}`),console.log(`\u2705 Codex SessionStart context hook: ${a}`);return}if(t==="notify"){let o=e[e.length-1]??"";try{await mu(o)}finally{fu(o)}return}let n=Xe(e,"--project")??process.cwd(),r=new q().detect(n),s=Yr(r);if(t==="sync"){let o=e[0];if(!o)throw new Error("Usage: sync <thread-id>");let i=Xe(e,"--rollout")??wt(o);if(!i)throw new Error(`Codex rollout not found for ${o}`);let a=await pe({project:r,storage:s,threadId:o,rolloutPath:i,idle:e.includes("--idle")});console.log(JSON.stringify(a,null,2));return}if(t==="recover"){let o=Number(Xe(e,"--limit")??100),i=await Er(r,s,Number.isFinite(o)?o:100);console.log(JSON.stringify({project:r.name,sessions:i.length,imported:i.reduce((a,c)=>a+c.imported,0)},null,2));return}console.log(`Codex Session Adapter
|
|
115
134
|
|
|
116
135
|
Commands:
|
|
117
136
|
install-notify
|
|
118
137
|
sync <thread-id> [--project PATH]
|
|
119
138
|
recover [--project PATH] [--limit N]
|
|
120
|
-
`)}
|
|
139
|
+
`)}gu().catch(t=>{process.argv[2]==="notify"&&process.exit(0),console.error(t instanceof Error?t.message:t),process.exit(1)});
|