@adhd/dispatch-core-optimizer 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/index.js +7 -10
- package/index.mjs +643 -908
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
## 0.1.4 (2026-09-26)
|
|
2
|
+
|
|
3
|
+
### 🚀 Features
|
|
4
|
+
|
|
5
|
+
- **vite-plugins:** absorb perf/test-resolve-fix — test-time @adhd/* source resolution ([3e344506](https://github.com/PseudoSky/adhd/commit/3e344506))
|
|
6
|
+
|
|
7
|
+
### 🩹 Fixes
|
|
8
|
+
|
|
9
|
+
- **vite:** restore import.meta.url in CJS output under vite 8 ([7916e639](https://github.com/PseudoSky/adhd/commit/7916e639))
|
|
10
|
+
- **nx:** finish the ESLint v9 flat-config migration and unblock the gate ([53f4ff3e](https://github.com/PseudoSky/adhd/commit/53f4ff3e))
|
|
11
|
+
|
|
12
|
+
### ❤️ Thank You
|
|
13
|
+
|
|
14
|
+
- pseudosky
|
|
15
|
+
|
|
1
16
|
## 0.1.3 (2026-09-24)
|
|
2
17
|
|
|
3
18
|
This was a version bump only for dispatch-core-optimizer to align it with other projects, there were no code changes.
|
package/index.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
${t.errors.map(n=>` ${n.path}: ${n.message}`).join(`
|
|
3
|
-
`)}`)}const U={prose:5.5,md:5.5,ts:6.3,tsx:6.3,py:6.3,default:4};function F(e,t){if(e<=0)return 0;const o=U[""]??U.default??4;return Math.ceil(e/o)}function Z(e,t){if(e.length===0||t.fileSizes===void 0)return new Map;try{return t.fileSizes(e)}catch{return new Map}}const ye={low:600,medium:1e3,high:2e3},be=de;function ve(e){const t=e.properties??{};return Object.keys(t).length||Object.keys(e).length}function we(e,t){if(e.type==="tool-call")return 0;const n=e.shape;if(!n||n.kind===null)return 0;const o=n.kind;if(o==="function"||o==="interface"||o==="type"||o==="class"||o==="enum"||o==="const"||o==="script")return("ops"in n&&Array.isArray(n.ops)?n.ops:[]).length*200;if(o==="config"||o==="env"||o==="schema"||o==="manifest")return("ops"in n&&Array.isArray(n.ops)?n.ops:[]).length*100;if(o==="doc")return ye[t??"medium"]??1e3;if(o==="structured-output"){const i="schema"in n&&n.schema?n.schema:{};return ve(i)*50}return 0}function ke(e,t){const n=new Set([...Object.keys(e),...Object.keys(t)]),o={};for(const i of n){const l=e[i];o[i]=l??t[i]??null}return o}function $e(e,t){const n=new Set([...Object.keys(e),...Object.keys(t)]),o={};for(const i of n){const l=e[i]??t[i];if(l===void 0)throw new TypeError(`Model tier "${i}" not found in dag or deps context_window_per_tier`);o[i]=l}return o}function ee(e){var c;const t=Object.keys(e),n=new Map,o=new Map;for(const s of t)n.has(s)||n.set(s,0),o.has(s)||o.set(s,[]);for(const s of t){const r=((c=e[s])==null?void 0:c.depends_on)??[];for(const a of r){const u=o.get(a);u!==void 0&&u.push(s),n.set(s,(n.get(s)??0)+1)}}const i=new Map,l=[];let p=t.filter(s=>(n.get(s)??0)===0).sort();for(const s of p)i.set(s,0);for(;p.length>0;){const s=[];for(const r of p){l.push(r);const a=(o.get(r)??[]).slice().sort();for(const u of a){const f=(n.get(u)??0)-1;n.set(u,f);const _=i.get(r)??0,y=i.get(u)??0;i.set(u,Math.max(y,_+1)),f===0&&s.push(u)}}p=s.sort()}if(l.length!==t.length){const s=new Set(l),r=t.filter(a=>!s.has(a));throw new Error(`snapshot(): cycle detected in milestone depends_on graph. Nodes involved: [${r.join(", ")}]`)}return{order:l,waves:i}}function te(e,t){if(t.length===0)return[];const n=new Set(t);return e.filter(o=>o.operations.some(i=>n.has(i)))}function ne(e,t){return e.filter(n=>n.operations.includes(t))}function Ae(e,t,n,o,i){const l=`${e}.guard`,p=[...n,l],c=te(o,p);for(const r of c)for(const a of r.results)if(a.op_id===l&&a.guard_result==="pass")return"complete";for(const r of c)for(const a of r.results)if(p.includes(a.op_id)&&a.status==="failed")return"failed";for(const r of c)for(const a of r.results)if(p.includes(a.op_id)&&a.status==="in_progress")return"in_progress";const s=i.length===0||i.every(r=>r==="complete");return t.pending!==null&&s?"pending-surfaced":"pending"}function Oe(e,t){const n=`${e}.guard`;let o=null,i=null,l=null;for(const p of t)if(p.operations.includes(n))for(const c of p.results)c.op_id===n&&c.guard_result!==null&&(o=c.guard_result,i=c.guard_output,c.guard_result==="pass"&&(l=c.guard_ran_at));return o!==null?{guard_result:o,guard_output:i,completed_at:l}:{guard_result:"pending",guard_output:null,completed_at:null}}function Se(e){const t=new Set;for(const n of e)n.action==="move"&&n.to_file?t.add(n.to_file):be.has(n.action)&&n.file&&t.add(n.file);return Array.from(t)}function Te(e,t,n){const o=ne(n,e.id),i=o.map(a=>a.id);let l=null,p=null,c=null;for(const a of o)for(const u of a.results)u.op_id===e.id&&u.guard_result!==null&&(l=u.guard_result,p=u.guard_output,c=u.guard_ran_at);const s=e.ki_estimate!==null&&e.ki_estimate!==void 0?e.ki_estimate:we(e,t),r=Me(e.shape);return{...e,ki_estimate:s,shape:r,dispatch_ids:i,attempt_count:0,guard_result:l,guard_output:p,guard_ran_at:c,blast_radius:[],conflict:{detected:!1,competing_op:null,op_key:null,resolution:null},tokens_actual:null}}function Me(e){if(!e)return null;if(e.kind===null)return e;const t=e.kind;if(t==="function"||t==="interface"||t==="type"||t==="class"||t==="enum"||t==="const"||t==="script"||t==="config"||t==="env"||t==="schema"||t==="manifest"){const n=("ops"in e&&Array.isArray(e.ops)?e.ops:[]).map(o=>({...o,from:null,breaking:null,severity:null}));return{...e,ops:n}}return e}function je(e,t,n,o){const i=`${e}.guard`,l=ne(o,i),p=l.map(a=>a.id);let c=null,s=null,r=null;for(const a of l)for(const u of a.results)u.op_id===i&&u.guard_result!==null&&(c=u.guard_result,s=u.guard_output,r=u.guard_ran_at);return{id:i,milestone:e,depends_on:n,type:"tool-call",action:"guard",file:null,symbol:null,provenance:null,confidence:null,audit_check:null,criteria:[],tool:null,args:null,guard:t.guard??null,to_file:null,to_symbol:null,ki_estimate:0,ki_source:null,authored_by:"synthesized",status:"pending",shape:null,dispatch_ids:p,attempt_count:0,guard_result:c,guard_output:s,guard_ran_at:r,blast_radius:[],conflict:{detected:!1,competing_op:null,op_key:null,resolution:null},tokens_actual:null}}function xe(e,t){const n={},{write_multiplier:o,read_multiplier:i,hit_probability:l}=t;for(const[p,c]of Object.entries(e))c===null?n[p]=null:n[p]=Math.round(c*((1-l)*o+l*i));return n}function Pe(e,t,n){const o=Array.from(e.keys()),i={};for(let l=0;l<o.length;l++)for(let p=l+1;p<o.length;p++){const c=o[l],s=o[p];if(c===void 0||s===void 0)continue;const[r,a]=c<s?[c,s]:[s,c],u=`${r}:${a}`,f=e.get(c)??[],_=e.get(s)??[],y=new Set(f.map(w=>w.file).filter(w=>w!==null)),v=new Set(_.map(w=>w.file).filter(w=>w!==null)),b=Array.from(y).filter(w=>v.has(w));if(b.length===0)continue;const A=t.get(c)??"pending",j=t.get(s)??"pending",k=A==="complete"&&j==="complete";let $=0;for(const w of b)$+=k?n(w):0;i[u]=$}return i}function Ie(e){return(Array.isArray(e)?e:Object.values(e)).map(n=>n.type===void 0?{...n,type:"generative"}:n)}function ze(e,t){const n=new Date().toISOString(),o=t.dispatchLog??e.dispatch_log,i=e.optimization.sentinel_fanout,l=ke(e.optimization.b_per_tier,t.bPerTier),p=$e(e.optimization.context_window_per_tier,t.contextWindowPerTier),c=xe(l,i),{waves:s}=ee(e.milestones),r=Ie(e.operations),a=new Map;for(const m of r){const d=a.get(m.milestone)??[];d.push(m),a.set(m.milestone,d)}const u=new Set;for(const m of r)m.file&&u.add(m.file),m.action==="move"&&m.to_file&&u.add(m.to_file);const f=Z(Array.from(u),t),_=m=>f.get(m)??0,y=new Map,v=Array.from(s.entries()).sort((m,d)=>m[1]-d[1]||m[0].localeCompare(d[0])).map(([m])=>m);for(const m of v){const d=e.milestones[m];if(d===void 0)continue;const x=(a.get(m)??[]).map(M=>M.id),P=d.depends_on.map(M=>y.get(M)??"pending"),I=Ae(m,d,x,o,P);y.set(m,I)}const b={},A=[];for(const m of v){const d=e.milestones[m];if(d===void 0)continue;const x=s.get(m)??0,P=a.get(m)??[],I=P.map(h=>h.id),M=y.get(m)??"pending",C=d.depends_on.map(h=>y.get(h)??"pending"),le=d.depends_on.length===0||C.every(h=>h==="complete"),ue=C.every(h=>h!=="failed"),ce=d.pending===null&&le&&ue&&M!=="complete",E=te(o,[...I,`${m}.guard`]),ae=E.length>0?E.map(h=>h.started_at).sort()[0]??null:null;let D,N,B;if(d.guard){const h=Oe(m,o);D=h.guard_result,N=h.guard_output,B=h.completed_at}else D=null,N=null,B=null;const R=Se(P),K=R.reduce((h,O)=>h+_(O),0),W=P.map(h=>Te(h,d.effort,o));let z=null,G=0,Y=!1;for(const h of W){if(h.ki_estimate===null){Y=!0;break}G+=h.ki_estimate}Y||(z=G);let H=null;const L=d.model;if(L!==null&&z!==null){const h=c[L]??null;if(h!==null){const O=F(K);H=h+O+z}}let V=null;const J=E.filter(h=>h.completed_at!==null&&h.results.length>0&&h.results.every(O=>O.status==="complete"||O.status==="skipped"));J.length>0&&(V=J.reduce((h,O)=>h+O.turns.reduce((fe,Q)=>fe+Q.input_tokens+Q.output_tokens,0),0));const pe=je(m,d,I,o);A.push(...W,pe),b[m]={description:d.description,...d.rationale!==void 0?{rationale:d.rationale}:{},authored_by:d.authored_by,pending:d.pending,triggered_by:d.triggered_by,phase:d.phase,depends_on:d.depends_on,agent:d.agent,model:d.model,effort:d.effort,two_stage:d.two_stage,read_only:d.read_only,guard:d.guard,context:`contexts/${m}.md`,wave:x,eligible:ce,status:M,started_at:ae,completed_at:B,guard_result:D,guard_output:N,artifacts:R,si_bytes:K,ki_estimate:z,tokens_estimated:H,tokens_actual:V}}const j=Pe(a,y,_),k=Ee(e,b),w={tokens_naive:(()=>{let m=0;for(const d of Object.values(b))if(d.eligible&&d.status==="pending"&&d.model!==null){const x=l[d.model]??t.bPerTier[d.model]??0;m+=x+(d.ki_estimate??0)}return m})(),sentinel_fanout:i,context_window_override:e.optimization.context_window_override,b_override:e.optimization.b_override,b_per_tier:l,b_eff_per_tier:c,context_window_per_tier:p},q={snapshot_at:n,snapshot_version:1,plan:"",schema_version:e.schema_version,plan_kind:e.plan_kind,description:e.description,problem:e.problem,approach:e.approach,executor:e.executor,...e.executor_model!==void 0?{executor_model:e.executor_model}:{},...e.executor_effort!==void 0?{executor_effort:e.executor_effort}:{},phases:[...e.phases],terminal:e.terminal,...e.assumed_baseline!==void 0?{assumed_baseline:e.assumed_baseline}:{},optimization:w,milestones:b,operations:A,pairwise_overlap:j,open_questions:k};return ge(q),q}function Ee(e,t){const n=[];for(const[o,i]of Object.entries(t)){const l=e.milestones[o];if(l===void 0||l.pending===null)continue;const p=i.status==="pending-surfaced";n.push({id:`q:${o}`,text:l.pending,blocking:o,surfaced:p,raised_at_dispatch:null,raised_at_turn:null,answered:!1,answer:null})}return n}const De="You are a dispatch-orchestrator worker agent executing one or more milestones from a dependency-DAG build plan. Each task turn gives you the full compiled instructions for this dispatch: milestone description, the operations to perform, and the guard command that defines done. Execute the operations precisely and produce real output (code, docs, or config) — never a plan or a summary in place of the work.";function oe(e){return Object.keys(e.milestones).filter(t=>{const n=e.milestones[t];return n!==void 0&&n.eligible===!0&&n.status==="pending"})}function Ne(e){if(e.length===0||e.every(t=>t.type==="tool-call"))return"tool-call";for(const t of e){if(t.type!=="generative"||!t.shape||t.shape.kind===null)continue;const n=t.shape.kind;if(n==="function"||n==="interface"||n==="type"||n==="class"||n==="enum"||n==="const"||n==="script"||n==="config"||n==="env"||n==="schema"||n==="manifest")return"code-config";if(n==="doc")return"doc";if(n==="structured-output")return"structured"}return"code-config"}function Be(e){if(!e)return"";const t=e.indexOf(":");return t>=0?e.slice(t+1):e}function Fe(e,t,n){let o=!0;const i=[];for(const l of e){const p=t[l];if(p===void 0)continue;const c=n.filter(r=>r.milestone===l&&r.action!=="guard");c.some(r=>r.type==="generative")&&(o=!1),i.push(`## Milestone: ${l}`),i.push(p.description),p.rationale&&i.push(`
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
`).
|
|
8
|
-
`));else if("ops"in f&&Array.isArray(f.ops))for(const _ of f.ops){const y=_.to!==null?` → ${_.to}`:"",v=_.target!==null?` "${_.target}"`:"";if(i.push(` - ${_.op}${v}${y}`),_.type_spec)for(const[b,A]of Object.entries(_.type_spec))i.push(` - ${b}: ${A}`)}}}const s=p.guard;i.push(`
|
|
9
|
-
### Guard: ${s??"none"}`),i.push("")}return o?null:i.join(`
|
|
10
|
-
`)}function se(e,t,n){if(e===null)return null;const o=t.optimization.b_eff_per_tier[e];if(o!=null)return o;const i=t.optimization.b_per_tier[e];if(i!=null)return i;const l=n.bPerTier[e];return l!==void 0?l:null}function ie(e,t,n){if(e===null)return Number.POSITIVE_INFINITY;const o=t.optimization.context_window_per_tier[e];if(o!==void 0)return o;const i=n.contextWindowPerTier[e];return i!==void 0?i:Number.POSITIVE_INFINITY}function re(e,t){const n=new Set;for(const o of e){const i=t.snapshot.milestones[o];if(i!==void 0){n.add(i.context);for(const l of i.read_only)n.add(l);for(const l of t.opsByMilestone.get(o)??[])l.file&&n.add(l.file)}}return Array.from(n)}function X(e,t,n){const o=se(t,n.snapshot,n.deps);if(o===null)return null;const l=re(e,n).reduce((s,r)=>s+(n.sizeMap.get(r)??0),0),p=F(l),c=e.reduce((s,r)=>{var a;return s+(((a=n.snapshot.milestones[r])==null?void 0:a.ki_estimate)??0)},0);return o+p+c}function qe(e,t,n){const o=ie(t,n.snapshot,n.deps),i=[...e].sort((c,s)=>{var u,f;const r=((u=n.snapshot.milestones[c])==null?void 0:u.ki_estimate)??0,a=((f=n.snapshot.milestones[s])==null?void 0:f.ki_estimate)??0;return r-a||c.localeCompare(s)}),l=[];let p=[];for(const c of i){const s=[...p,c],r=X(s,t,n);r!==null&&r>o&&p.length>0?(l.push(p),p=[c]):p=s}return p.length>0&&l.push(p),l.filter(c=>{const s=X(c,t,n);return s===null||s<=o})}function Ce(e,t,n,o){const i=e[0]??"",l=o.snapshot.milestones[i],p=(l==null?void 0:l.effort)??null,c=[];for(const k of e)for(const $ of o.opsByMilestone.get(k)??[])c.push($.id);const s=re(e,o),r=s.reduce((k,$)=>k+(o.sizeMap.get($)??0),0),a=se(n,o.snapshot,o.deps),u=e.reduce((k,$)=>{var w;return k+(((w=o.snapshot.milestones[$])==null?void 0:w.ki_estimate)??0)},0),f=a!==null?a+F(r)+u:null,_=ie(n,o.snapshot,o.deps),y=f!==null?f<=_:!0,v=Be((l==null?void 0:l.agent)??null),b=Fe(e,o.snapshot.milestones,o.snapshot.operations),A=b!==null?"model-dispatch":"guard-only",j=b!==null?De:null;return{id:`${i}.dispatch.${t}`,milestones:e,operations:c,model:n,effort:p,two_stage:(l==null?void 0:l.two_stage)??!1,provider:null,agent_name:v,execution_mode:A,mcp_servers:null,resolved_max_tokens:null,background:!0,systemPrompt:j,prompt:b,context_files:s,si_bytes:r,tokens_estimated:f,fits_context_window:y,sentinel_role:null,dispatch_log_id:null,remote_task_id:null,result:null,status:"pending",started_at:null,completed_at:null,tokens_actual:null}}function Re(e,t){const n=oe(e);if(n.length===0)return[];const o=new Map;for(const u of e.operations){if(u.action==="guard")continue;const f=o.get(u.milestone)??[];f.push(u),o.set(u.milestone,f)}const i=new Map;for(const u of n){const f=e.milestones[u];if(f===void 0)continue;const _=Ne(o.get(u)??[]),y=f.model??"_null",v=`${_}:${y}`,b=i.get(v)??[];b.push(u),i.set(v,b)}const l=new Set;for(const u of n){const f=e.milestones[u];if(f!==void 0){l.add(f.context);for(const _ of f.read_only)l.add(_);for(const _ of o.get(u)??[])_.file&&l.add(_.file)}}const p=Z(Array.from(l),t),c={snapshot:e,deps:t,sizeMap:p,opsByMilestone:o},s=[];let r=0;const a=Array.from(i.keys()).sort();for(const u of a){const f=i.get(u);if(f===void 0)continue;const _=u.slice(u.indexOf(":")+1),y=_!=="_null"?_:null,v=qe(f,y,c);for(const b of v)s.push(Ce(b,r++,y,c))}return s}function Ke(e,t){let n=0;for(const o of oe(e)){const i=e.milestones[o];if(i===void 0)continue;const l=We(i.model,e,t)??0;n+=l+(i.ki_estimate??0)}return n}function We(e,t,n){if(e===null)return null;const o=t.optimization.b_per_tier[e];if(o!=null)return o;const i=n.bPerTier[e];return i!==void 0?i:null}exports.computeTokensNaive=Ke;exports.optimize=Re;exports.snapshot=ze;exports.topoSortMilestones=ee;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=new Set([`create`,`modify-signature`,`modify-body`,`add-export`,`remove-export`,`rename`]),t=new Set([`pending`,`pending-surfaced`,`in_progress`,`complete`,`failed`,`skipped`]),n=new Set([`pending`,`in_progress`,`complete`,`failed`,`skipped`]);function r(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function i(e){return typeof e==`string`}function a(e,t,n){return{path:e,message:t,value:n}}function o(e){let o=[];if(!r(e))return{valid:!1,errors:[a(``,`root must be an object`)]};for(let t of[`snapshot_at`,`description`])i(e[t])||o.push(a(t,`required string field missing`,e[t]));i(e.plan)||o.push(a(`plan`,`required string`,e.plan));let s=e.milestones;if(!r(s))o.push(a(`milestones`,`must be an object`,s));else{let c=Object.keys(s),l=new Set(c);for(let e of c){let n=s[e];if(!r(n)){o.push(a(`milestones.${e}`,`must be an object`,n));continue}let c=n.eligible,l=n.pending;c===!0&&l!=null&&o.push(a(`milestones.${e}`,`D-07 violation: eligible=true but pending not null`)),typeof c!=`boolean`&&o.push(a(`milestones.${e}.eligible`,`must be boolean`,c));let u=n.status;(!i(u)||!t.has(u))&&o.push(a(`milestones.${e}.status`,`invalid status`,u)),c===!0&&u===`complete`&&o.push(a(`milestones.${e}`,`D-07 violation: eligible=true but status is complete`));let d=n.wave;(typeof d!=`number`||!Number.isInteger(d)||d<0)&&o.push(a(`milestones.${e}.wave`,`must be non-negative integer`,d));let f=n.ki_estimate;f!=null&&typeof f!=`number`&&o.push(a(`milestones.${e}.ki_estimate`,`must be number|null`,f));for(let t of[`tokens_estimated`,`tokens_actual`]){let r=n[t];r!=null&&typeof r!=`number`&&o.push(a(`milestones.${e}.${t}`,`must be number|null`,r))}Array.isArray(n.artifacts)||o.push(a(`milestones.${e}.artifacts`,`must be an array`,n.artifacts)),typeof n.si_bytes!=`number`&&o.push(a(`milestones.${e}.si_bytes`,`must be a number`,n.si_bytes))}let u=e.operations;if(!Array.isArray(u))o.push(a(`operations`,`must be an array`,u));else for(let e of u){if(!r(e))continue;let t=i(e.id)?e.id:`?`,s=e.milestone;i(s)&&!l.has(s)&&o.push(a(`operations[${t}].milestone`,`unknown milestone "${s}"`));let c=e.status;(!i(c)||!n.has(c))&&o.push(a(`operations[${t}].status`,`invalid operation status`,c)),Array.isArray(e.dispatch_ids)||o.push(a(`operations[${t}].dispatch_ids`,`must be an array`,e.dispatch_ids)),typeof e.attempt_count!=`number`&&o.push(a(`operations[${t}].attempt_count`,`must be a number`,e.attempt_count)),Array.isArray(e.blast_radius)||o.push(a(`operations[${t}].blast_radius`,`must be an array`,e.blast_radius))}let d=e.open_questions;if(!Array.isArray(d))o.push(a(`open_questions`,`must be an array`,d));else for(let e of d){if(!r(e))continue;let t=e.blocking;(!i(t)||!l.has(t))&&o.push(a(`open_questions`,`unknown blocking milestone "${t}"`)),typeof e.surfaced!=`boolean`&&o.push(a(`open_questions[${e.id}]`,`surfaced must be boolean`,e.surfaced)),typeof e.answered!=`boolean`&&o.push(a(`open_questions[${e.id}]`,`answered must be boolean`,e.answered))}let f=e.dispatch_units;if(f!==void 0){if(!Array.isArray(f))o.push(a(`dispatch_units`,`must be an array if present`,f));else for(let e of f){if(!r(e))continue;let t=e.fits_context_window;typeof t!=`boolean`&&o.push(a(`dispatch_units[${e.id}]`,`fits_context_window must be boolean`,t));let n=e.milestones;if(Array.isArray(n))for(let t of n)i(t)&&!l.has(t)&&o.push(a(`dispatch_units[${e.id}].milestones`,`unknown milestone "${t}"`))}}r(e.pairwise_overlap)||o.push(a(`pairwise_overlap`,`must be an object`,e.pairwise_overlap))}return{valid:o.length===0,errors:o}}function s(e){let t=o(e);if(!t.valid)throw Error(`Snapshot validation failed:\n${t.errors.map(e=>` ${e.path}: ${e.message}`).join(`
|
|
2
|
+
`)}`)}var c={prose:5.5,md:5.5,ts:6.3,tsx:6.3,py:6.3,default:4};function l(e,t){if(e<=0)return 0;let n=c[t?t.split(`.`).pop()?.toLowerCase()??``:``]??c.default??4;return Math.ceil(e/n)}function u(e,t){if(e.length===0||t.fileSizes===void 0)return new Map;try{return t.fileSizes(e)}catch{return new Map}}var d={low:600,medium:1e3,high:2e3},f=e;function p(e){let t=e.properties??{};return Object.keys(t).length||Object.keys(e).length}function m(e,t){if(e.type===`tool-call`)return 0;let n=e.shape;if(n?.kind==null)return 0;let r=n.kind;return r===`function`||r===`interface`||r===`type`||r===`class`||r===`enum`||r===`const`||r===`script`?(`ops`in n&&Array.isArray(n.ops)?n.ops:[]).length*200:r===`config`||r===`env`||r===`schema`||r===`manifest`?(`ops`in n&&Array.isArray(n.ops)?n.ops:[]).length*100:r===`doc`?d[t??`medium`]??1e3:r===`structured-output`?p(`schema`in n&&n.schema?n.schema:{})*50:0}function h(e,t){let n=new Set([...Object.keys(e),...Object.keys(t)]),r={};for(let i of n)r[i]=e[i]??t[i]??null;return r}function g(e,t){let n=new Set([...Object.keys(e),...Object.keys(t)]),r={};for(let i of n){let n=e[i]??t[i];if(n===void 0)throw TypeError(`Model tier "${i}" not found in dag or deps context_window_per_tier`);r[i]=n}return r}function _(e){let t=Object.keys(e),n=new Map,r=new Map;for(let e of t)n.has(e)||n.set(e,0),r.has(e)||r.set(e,[]);for(let i of t){let t=e[i]?.depends_on??[];for(let e of t){let t=r.get(e);t!==void 0&&t.push(i),n.set(i,(n.get(i)??0)+1)}}let i=new Map,a=[],o=t.filter(e=>(n.get(e)??0)===0).sort();for(let e of o)i.set(e,0);for(;o.length>0;){let e=[];for(let t of o){a.push(t);let o=(r.get(t)??[]).slice().sort();for(let r of o){let a=(n.get(r)??0)-1;n.set(r,a);let o=i.get(t)??0,s=i.get(r)??0;i.set(r,Math.max(s,o+1)),a===0&&e.push(r)}}o=e.sort()}if(a.length!==t.length){let e=new Set(a),n=t.filter(t=>!e.has(t));throw Error(`snapshot(): cycle detected in milestone depends_on graph. Nodes involved: [${n.join(`, `)}]`)}return{order:a,waves:i}}function v(e,t){if(t.length===0)return[];let n=new Set(t);return e.filter(e=>e.operations.some(e=>n.has(e)))}function y(e,t){return e.filter(e=>e.operations.includes(t))}function b(e,t,n,r,i){let a=`${e}.guard`,o=[...n,a],s=v(r,o);for(let e of s)for(let t of e.results)if(t.op_id===a&&t.guard_result===`pass`)return`complete`;for(let e of s)for(let t of e.results)if(o.includes(t.op_id)&&t.status===`failed`)return`failed`;for(let e of s)for(let t of e.results)if(o.includes(t.op_id)&&t.status===`in_progress`)return`in_progress`;let c=i.length===0||i.every(e=>e===`complete`);return t.pending!==null&&c?`pending-surfaced`:`pending`}function x(e,t){let n=`${e}.guard`,r=null,i=null,a=null;for(let e of t)if(e.operations.includes(n))for(let t of e.results)t.op_id===n&&t.guard_result!==null&&(r=t.guard_result,i=t.guard_output,t.guard_result===`pass`&&(a=t.guard_ran_at));return r===null?{guard_result:`pending`,guard_output:null,completed_at:null}:{guard_result:r,guard_output:i,completed_at:a}}function S(e){let t=new Set;for(let n of e)n.action===`move`&&n.to_file?t.add(n.to_file):f.has(n.action)&&n.file&&t.add(n.file);return Array.from(t)}function C(e,t,n){let r=y(n,e.id),i=r.map(e=>e.id),a=null,o=null,s=null;for(let t of r)for(let n of t.results)n.op_id===e.id&&n.guard_result!==null&&(a=n.guard_result,o=n.guard_output,s=n.guard_ran_at);let c=e.ki_estimate!==null&&e.ki_estimate!==void 0?e.ki_estimate:m(e,t),l=w(e.shape);return{...e,ki_estimate:c,shape:l,dispatch_ids:i,attempt_count:0,guard_result:a,guard_output:o,guard_ran_at:s,blast_radius:[],conflict:{detected:!1,competing_op:null,op_key:null,resolution:null},tokens_actual:null}}function w(e){if(!e)return null;if(e.kind===null)return e;let t=e.kind;if(t===`function`||t===`interface`||t===`type`||t===`class`||t===`enum`||t===`const`||t===`script`||t===`config`||t===`env`||t===`schema`||t===`manifest`){let t=(`ops`in e&&Array.isArray(e.ops)?e.ops:[]).map(e=>({...e,from:null,breaking:null,severity:null}));return{...e,ops:t}}return e}function T(e,t,n,r){let i=`${e}.guard`,a=y(r,i),o=a.map(e=>e.id),s=null,c=null,l=null;for(let e of a)for(let t of e.results)t.op_id===i&&t.guard_result!==null&&(s=t.guard_result,c=t.guard_output,l=t.guard_ran_at);return{id:i,milestone:e,depends_on:n,type:`tool-call`,action:`guard`,file:null,symbol:null,provenance:null,confidence:null,audit_check:null,criteria:[],tool:null,args:null,guard:t.guard??null,to_file:null,to_symbol:null,ki_estimate:0,ki_source:null,authored_by:`synthesized`,status:`pending`,shape:null,dispatch_ids:o,attempt_count:0,guard_result:s,guard_output:c,guard_ran_at:l,blast_radius:[],conflict:{detected:!1,competing_op:null,op_key:null,resolution:null},tokens_actual:null}}function E(e,t){let n={},{write_multiplier:r,read_multiplier:i,hit_probability:a}=t;for(let[t,o]of Object.entries(e))n[t]=o===null?null:Math.round(o*((1-a)*r+a*i));return n}function D(e,t,n){let r=Array.from(e.keys()),i={};for(let a=0;a<r.length;a++)for(let o=a+1;o<r.length;o++){let s=r[a],c=r[o];if(s===void 0||c===void 0)continue;let[l,u]=s<c?[s,c]:[c,s],d=`${l}:${u}`,f=e.get(s)??[],p=e.get(c)??[],m=new Set(f.map(e=>e.file).filter(e=>e!==null)),h=new Set(p.map(e=>e.file).filter(e=>e!==null)),g=Array.from(m).filter(e=>h.has(e));if(g.length===0)continue;let _=t.get(s)??`pending`,v=t.get(c)??`pending`,y=_===`complete`&&v===`complete`,b=0;for(let e of g)b+=y?n(e):0;i[d]=b}return i}function O(e){return(Array.isArray(e)?e:Object.values(e)).map(e=>e.type===void 0?{...e,type:`generative`}:e)}function k(e,t){let n=new Date().toISOString(),r=t.dispatchLog??e.dispatch_log,i=e.optimization.sentinel_fanout,a=h(e.optimization.b_per_tier,t.bPerTier),o=g(e.optimization.context_window_per_tier,t.contextWindowPerTier),c=E(a,i),{waves:d}=_(e.milestones),f=O(e.operations),p=new Map;for(let e of f){let t=p.get(e.milestone)??[];t.push(e),p.set(e.milestone,t)}let m=new Set;for(let e of f)e.file&&m.add(e.file),e.action===`move`&&e.to_file&&m.add(e.to_file);let y=u(Array.from(m),t),w=e=>y.get(e)??0,k=new Map,j=Array.from(d.entries()).sort((e,t)=>e[1]-t[1]||e[0].localeCompare(t[0])).map(([e])=>e);for(let t of j){let n=e.milestones[t];if(n===void 0)continue;let i=b(t,n,(p.get(t)??[]).map(e=>e.id),r,n.depends_on.map(e=>k.get(e)??`pending`));k.set(t,i)}let M={},N=[];for(let t of j){let n=e.milestones[t];if(n===void 0)continue;let i=d.get(t)??0,a=p.get(t)??[],o=a.map(e=>e.id),s=k.get(t)??`pending`,u=n.depends_on.map(e=>k.get(e)??`pending`),f=n.depends_on.length===0||u.every(e=>e===`complete`),m=u.every(e=>e!==`failed`),h=n.pending===null&&f&&m&&s!==`complete`,g=v(r,[...o,`${t}.guard`]),_=g.length>0?g.map(e=>e.started_at).sort()[0]??null:null,y,b,E;if(n.guard){let e=x(t,r);y=e.guard_result,b=e.guard_output,E=e.completed_at}else y=null,b=null,E=null;let D=S(a),O=D.reduce((e,t)=>e+w(t),0),A=a.map(e=>C(e,n.effort,r)),j=null,P=0,F=!1;for(let e of A){if(e.ki_estimate===null){F=!0;break}P+=e.ki_estimate}F||(j=P);let I=null,L=n.model;if(L!==null&&j!==null){let e=c[L]??null;e!==null&&(I=e+l(O)+j)}let R=null,z=g.filter(e=>e.completed_at!==null&&e.results.length>0&&e.results.every(e=>e.status===`complete`||e.status===`skipped`));z.length>0&&(R=z.reduce((e,t)=>e+t.turns.reduce((e,t)=>e+t.input_tokens+t.output_tokens,0),0));let B=T(t,n,o,r);N.push(...A,B),M[t]={description:n.description,...n.rationale===void 0?{}:{rationale:n.rationale},authored_by:n.authored_by,pending:n.pending,triggered_by:n.triggered_by,phase:n.phase,depends_on:n.depends_on,agent:n.agent,model:n.model,effort:n.effort,two_stage:n.two_stage,read_only:n.read_only,guard:n.guard,context:`contexts/${t}.md`,wave:i,eligible:h,status:s,started_at:_,completed_at:E,guard_result:y,guard_output:b,artifacts:D,si_bytes:O,ki_estimate:j,tokens_estimated:I,tokens_actual:R}}let P=D(p,k,w),F=A(e,M),I={tokens_naive:(()=>{let e=0;for(let n of Object.values(M))if(n.eligible&&n.status===`pending`&&n.model!==null){let r=a[n.model]??t.bPerTier[n.model]??0;e+=r+(n.ki_estimate??0)}return e})(),sentinel_fanout:i,context_window_override:e.optimization.context_window_override,b_override:e.optimization.b_override,b_per_tier:a,b_eff_per_tier:c,context_window_per_tier:o},L={snapshot_at:n,snapshot_version:1,plan:``,schema_version:e.schema_version,plan_kind:e.plan_kind,description:e.description,problem:e.problem,approach:e.approach,executor:e.executor,...e.executor_model===void 0?{}:{executor_model:e.executor_model},...e.executor_effort===void 0?{}:{executor_effort:e.executor_effort},phases:[...e.phases],terminal:e.terminal,...e.assumed_baseline===void 0?{}:{assumed_baseline:e.assumed_baseline},optimization:I,milestones:M,operations:N,pairwise_overlap:P,open_questions:F};return s(L),L}function A(e,t){let n=[];for(let[r,i]of Object.entries(t)){let t=e.milestones[r];if(t?.pending==null)continue;let a=i.status===`pending-surfaced`;n.push({id:`q:${r}`,text:t.pending,blocking:r,surfaced:a,raised_at_dispatch:null,raised_at_turn:null,answered:!1,answer:null})}return n}var j=`You are a dispatch-orchestrator worker agent executing one or more milestones from a dependency-DAG build plan. Each task turn gives you the full compiled instructions for this dispatch: milestone description, the operations to perform, and the guard command that defines done. Execute the operations precisely and produce real output (code, docs, or config) — never a plan or a summary in place of the work.`;function M(e){return Object.keys(e.milestones).filter(t=>{let n=e.milestones[t];return n?.eligible===!0&&n.status===`pending`})}function N(e){if(e.length===0||e.every(e=>e.type===`tool-call`))return`tool-call`;for(let t of e){if(t.type!==`generative`||t.shape?.kind==null)continue;let e=t.shape.kind;if(e===`function`||e===`interface`||e===`type`||e===`class`||e===`enum`||e===`const`||e===`script`||e===`config`||e===`env`||e===`schema`||e===`manifest`)return`code-config`;if(e===`doc`)return`doc`;if(e===`structured-output`)return`structured`}return`code-config`}function P(e){if(!e)return``;let t=e.indexOf(`:`);return t>=0?e.slice(t+1):e}function F(e,t,n){let r=!0,i=[];for(let a of e){let e=t[a];if(e===void 0)continue;let o=n.filter(e=>e.milestone===a&&e.action!==`guard`);o.some(e=>e.type===`generative`)&&(r=!1),i.push(`## Milestone: ${a}`),i.push(e.description),e.rationale&&i.push(`\n*Rationale:* ${e.rationale}`),i.push(`
|
|
3
|
+
### Operations:`);for(let e of o){let t=e.file?` ${e.file}`:``,n=e.symbol?` (${e.symbol})`:``;if(i.push(`- [${e.id}] ${e.action}${t}${n}`),e.shape&&e.shape.kind!==null){let t=e.shape;if(t.kind===`doc`)i.push(` Description: ${t.description}`),i.push(` Objective: ${t.objective}`),t.required_sections&&t.required_sections.length>0&&i.push(` Required sections: ${t.required_sections.join(`, `)}`);else if(t.kind===`structured-output`)i.push(` Schema:
|
|
4
|
+
`+JSON.stringify(t.schema,null,2).split(`
|
|
5
|
+
`).map(e=>` ${e}`).join(`
|
|
6
|
+
`));else if(`ops`in t&&Array.isArray(t.ops))for(let e of t.ops){let t=e.to===null?``:` → ${e.to}`,n=e.target===null?``:` "${e.target}"`;if(i.push(` - ${e.op}${n}${t}`),e.type_spec)for(let[t,n]of Object.entries(e.type_spec))i.push(` - ${t}: ${n}`)}}}let s=e.guard;i.push(`\n### Guard: ${s??`none`}`),i.push(``)}return r?null:i.join(`
|
|
7
|
+
`)}function I(e,t,n){if(e===null)return null;let r=t.optimization.b_eff_per_tier[e];if(r!=null)return r;let i=t.optimization.b_per_tier[e];if(i!=null)return i;let a=n.bPerTier[e];return a===void 0?null:a}function L(e,t,n){if(e===null)return 1/0;let r=t.optimization.context_window_per_tier[e];if(r!==void 0)return r;let i=n.contextWindowPerTier[e];return i===void 0?1/0:i}function R(e,t){let n=new Set;for(let r of e){let e=t.snapshot.milestones[r];if(e!==void 0){n.add(e.context);for(let t of e.read_only)n.add(t);for(let e of t.opsByMilestone.get(r)??[])e.file&&n.add(e.file)}}return Array.from(n)}function z(e,t,n){let r=I(t,n.snapshot,n.deps);if(r===null)return null;let i=l(R(e,n).reduce((e,t)=>e+(n.sizeMap.get(t)??0),0)),a=e.reduce((e,t)=>e+(n.snapshot.milestones[t]?.ki_estimate??0),0);return r+i+a}function B(e,t,n){let r=L(t,n.snapshot,n.deps),i=[...e].sort((e,t)=>(n.snapshot.milestones[e]?.ki_estimate??0)-(n.snapshot.milestones[t]?.ki_estimate??0)||e.localeCompare(t)),a=[],o=[];for(let e of i){let i=[...o,e],s=z(i,t,n);s!==null&&s>r&&o.length>0?(a.push(o),o=[e]):o=i}return o.length>0&&a.push(o),a.filter(e=>{let i=z(e,t,n);return i===null||i<=r})}function V(e,t,n,r){let i=e[0]??``,a=r.snapshot.milestones[i],o=a?.effort??null,s=[];for(let t of e)for(let e of r.opsByMilestone.get(t)??[])s.push(e.id);let c=R(e,r),u=c.reduce((e,t)=>e+(r.sizeMap.get(t)??0),0),d=I(n,r.snapshot,r.deps),f=e.reduce((e,t)=>e+(r.snapshot.milestones[t]?.ki_estimate??0),0),p=d===null?null:d+l(u)+f,m=L(n,r.snapshot,r.deps),h=p===null||p<=m,g=P(a?.agent??null),_=F(e,r.snapshot.milestones,r.snapshot.operations),v=_===null?`guard-only`:`model-dispatch`,y=_===null?null:j;return{id:`${i}.dispatch.${t}`,milestones:e,operations:s,model:n,effort:o,two_stage:a?.two_stage??!1,provider:null,agent_name:g,execution_mode:v,mcp_servers:null,resolved_max_tokens:null,background:!0,systemPrompt:y,prompt:_,context_files:c,si_bytes:u,tokens_estimated:p,fits_context_window:h,sentinel_role:null,dispatch_log_id:null,remote_task_id:null,result:null,status:`pending`,started_at:null,completed_at:null,tokens_actual:null}}function H(e,t){let n=M(e);if(n.length===0)return[];let r=new Map;for(let t of e.operations){if(t.action===`guard`)continue;let e=r.get(t.milestone)??[];e.push(t),r.set(t.milestone,e)}let i=new Map;for(let t of n){let n=e.milestones[t];if(n===void 0)continue;let a=`${N(r.get(t)??[])}:${n.model??`_null`}`,o=i.get(a)??[];o.push(t),i.set(a,o)}let a=new Set;for(let t of n){let n=e.milestones[t];if(n!==void 0){a.add(n.context);for(let e of n.read_only)a.add(e);for(let e of r.get(t)??[])e.file&&a.add(e.file)}}let o={snapshot:e,deps:t,sizeMap:u(Array.from(a),t),opsByMilestone:r},s=[],c=0,l=Array.from(i.keys()).sort();for(let e of l){let t=i.get(e);if(t===void 0)continue;let n=e.slice(e.indexOf(`:`)+1),r=n===`_null`?null:n,a=B(t,r,o);for(let e of a)s.push(V(e,c++,r,o))}return s}function U(e,t){let n=0;for(let r of M(e)){let i=e.milestones[r];if(i===void 0)continue;let a=W(i.model,e,t)??0;n+=a+(i.ki_estimate??0)}return n}function W(e,t,n){if(e===null)return null;let r=t.optimization.b_per_tier[e];if(r!=null)return r;let i=n.bPerTier[e];return i===void 0?null:i}exports.computeTokensNaive=U,exports.optimize=H,exports.snapshot=k,exports.topoSortMilestones=_;
|