@adhd/dispatch-core-optimizer 0.0.6 → 0.1.1

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 CHANGED
@@ -1,3 +1,21 @@
1
+ ## 0.1.1 (2026-08-07)
2
+
3
+
4
+ ### 🚀 Features
5
+
6
+ - **dispatch-core-optimizer:** assemble DispatchUnit.systemPrompt/execution_mode, inline type_spec
7
+
8
+
9
+ ### 🩹 Fixes
10
+
11
+ - **apigen-cli:** restore 2768 files mass-deleted by 0117eb22 (BUG-APIGEN-052)
12
+
13
+
14
+ ### ❤️ Thank You
15
+
16
+ - parity-harness-self-test
17
+ - pseudosky
18
+
1
19
  ## 0.0.6 (2026-07-25)
2
20
 
3
21
 
package/index.js CHANGED
@@ -1,10 +1,10 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const de=new Set(["create","modify-signature","modify-body","add-export","remove-export","rename"]),me=new Set(["pending","pending-surfaced","in_progress","complete","failed","skipped"]),_e=new Set(["pending","in_progress","complete","failed","skipped"]);function S(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function O(e){return typeof e=="string"}function h(e,t,n){return{path:e,message:t,value:n}}function ge(e){const t=[];if(!S(e))return{valid:!1,errors:[h("","root must be an object")]};for(const o of["snapshot_at","description"])O(e[o])||t.push(h(o,"required string field missing",e[o]));O(e.plan)||t.push(h("plan","required string",e.plan));const n=e.milestones;if(!S(n))t.push(h("milestones","must be an object",n));else{const o=Object.keys(n),i=new Set(o);for(const s of o){const r=n[s];if(!S(r)){t.push(h(`milestones.${s}`,"must be an object",r));continue}const c=r.eligible,u=r.pending;c===!0&&u!==null&&u!==void 0&&t.push(h(`milestones.${s}`,"D-07 violation: eligible=true but pending not null")),typeof c!="boolean"&&t.push(h(`milestones.${s}.eligible`,"must be boolean",c));const p=r.status;(!O(p)||!me.has(p))&&t.push(h(`milestones.${s}.status`,"invalid status",p));const _=r.wave;(typeof _!="number"||!Number.isInteger(_)||_<0)&&t.push(h(`milestones.${s}.wave`,"must be non-negative integer",_));const b=r.ki_estimate;b!=null&&typeof b!="number"&&t.push(h(`milestones.${s}.ki_estimate`,"must be number|null",b));for(const v of["tokens_estimated","tokens_actual"]){const y=r[v];y!=null&&typeof y!="number"&&t.push(h(`milestones.${s}.${v}`,"must be number|null",y))}Array.isArray(r.artifacts)||t.push(h(`milestones.${s}.artifacts`,"must be an array",r.artifacts)),typeof r.si_bytes!="number"&&t.push(h(`milestones.${s}.si_bytes`,"must be a number",r.si_bytes))}const l=e.operations;if(!Array.isArray(l))t.push(h("operations","must be an array",l));else for(const s of l){if(!S(s))continue;const r=O(s.id)?s.id:"?",c=s.milestone;O(c)&&!i.has(c)&&t.push(h(`operations[${r}].milestone`,`unknown milestone "${c}"`));const u=s.status;(!O(u)||!_e.has(u))&&t.push(h(`operations[${r}].status`,"invalid operation status",u)),Array.isArray(s.dispatch_ids)||t.push(h(`operations[${r}].dispatch_ids`,"must be an array",s.dispatch_ids)),typeof s.attempt_count!="number"&&t.push(h(`operations[${r}].attempt_count`,"must be a number",s.attempt_count)),Array.isArray(s.blast_radius)||t.push(h(`operations[${r}].blast_radius`,"must be an array",s.blast_radius))}const f=e.open_questions;if(!Array.isArray(f))t.push(h("open_questions","must be an array",f));else for(const s of f){if(!S(s))continue;const r=s.blocking;(!O(r)||!i.has(r))&&t.push(h("open_questions",`unknown blocking milestone "${r}"`)),typeof s.surfaced!="boolean"&&t.push(h(`open_questions[${s.id}]`,"surfaced must be boolean",s.surfaced)),typeof s.answered!="boolean"&&t.push(h(`open_questions[${s.id}]`,"answered must be boolean",s.answered))}const a=e.dispatch_units;if(a!==void 0)if(!Array.isArray(a))t.push(h("dispatch_units","must be an array if present",a));else for(const s of a){if(!S(s))continue;const r=s.fits_context_window;typeof r!="boolean"&&t.push(h(`dispatch_units[${s.id}]`,"fits_context_window must be boolean",r));const c=s.milestones;if(Array.isArray(c))for(const u of c)O(u)&&!i.has(u)&&t.push(h(`dispatch_units[${s.id}].milestones`,`unknown milestone "${u}"`))}S(e.pairwise_overlap)||t.push(h("pairwise_overlap","must be an object",e.pairwise_overlap))}return{valid:t.length===0,errors:t}}function he(e){const t=ge(e);if(!t.valid)throw new Error(`Snapshot validation failed:
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const de=new Set(["create","modify-signature","modify-body","add-export","remove-export","rename"]),me=new Set(["pending","pending-surfaced","in_progress","complete","failed","skipped"]),_e=new Set(["pending","in_progress","complete","failed","skipped"]);function T(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function S(e){return typeof e=="string"}function g(e,t,n){return{path:e,message:t,value:n}}function he(e){const t=[];if(!T(e))return{valid:!1,errors:[g("","root must be an object")]};for(const o of["snapshot_at","description"])S(e[o])||t.push(g(o,"required string field missing",e[o]));S(e.plan)||t.push(g("plan","required string",e.plan));const n=e.milestones;if(!T(n))t.push(g("milestones","must be an object",n));else{const o=Object.keys(n),i=new Set(o);for(const s of o){const r=n[s];if(!T(r)){t.push(g(`milestones.${s}`,"must be an object",r));continue}const a=r.eligible,u=r.pending;a===!0&&u!==null&&u!==void 0&&t.push(g(`milestones.${s}`,"D-07 violation: eligible=true but pending not null")),typeof a!="boolean"&&t.push(g(`milestones.${s}.eligible`,"must be boolean",a));const f=r.status;(!S(f)||!me.has(f))&&t.push(g(`milestones.${s}.status`,"invalid status",f));const _=r.wave;(typeof _!="number"||!Number.isInteger(_)||_<0)&&t.push(g(`milestones.${s}.wave`,"must be non-negative integer",_));const y=r.ki_estimate;y!=null&&typeof y!="number"&&t.push(g(`milestones.${s}.ki_estimate`,"must be number|null",y));for(const v of["tokens_estimated","tokens_actual"]){const b=r[v];b!=null&&typeof b!="number"&&t.push(g(`milestones.${s}.${v}`,"must be number|null",b))}Array.isArray(r.artifacts)||t.push(g(`milestones.${s}.artifacts`,"must be an array",r.artifacts)),typeof r.si_bytes!="number"&&t.push(g(`milestones.${s}.si_bytes`,"must be a number",r.si_bytes))}const l=e.operations;if(!Array.isArray(l))t.push(g("operations","must be an array",l));else for(const s of l){if(!T(s))continue;const r=S(s.id)?s.id:"?",a=s.milestone;S(a)&&!i.has(a)&&t.push(g(`operations[${r}].milestone`,`unknown milestone "${a}"`));const u=s.status;(!S(u)||!_e.has(u))&&t.push(g(`operations[${r}].status`,"invalid operation status",u)),Array.isArray(s.dispatch_ids)||t.push(g(`operations[${r}].dispatch_ids`,"must be an array",s.dispatch_ids)),typeof s.attempt_count!="number"&&t.push(g(`operations[${r}].attempt_count`,"must be a number",s.attempt_count)),Array.isArray(s.blast_radius)||t.push(g(`operations[${r}].blast_radius`,"must be an array",s.blast_radius))}const p=e.open_questions;if(!Array.isArray(p))t.push(g("open_questions","must be an array",p));else for(const s of p){if(!T(s))continue;const r=s.blocking;(!S(r)||!i.has(r))&&t.push(g("open_questions",`unknown blocking milestone "${r}"`)),typeof s.surfaced!="boolean"&&t.push(g(`open_questions[${s.id}]`,"surfaced must be boolean",s.surfaced)),typeof s.answered!="boolean"&&t.push(g(`open_questions[${s.id}]`,"answered must be boolean",s.answered))}const c=e.dispatch_units;if(c!==void 0)if(!Array.isArray(c))t.push(g("dispatch_units","must be an array if present",c));else for(const s of c){if(!T(s))continue;const r=s.fits_context_window;typeof r!="boolean"&&t.push(g(`dispatch_units[${s.id}]`,"fits_context_window must be boolean",r));const a=s.milestones;if(Array.isArray(a))for(const u of a)S(u)&&!i.has(u)&&t.push(g(`dispatch_units[${s.id}].milestones`,`unknown milestone "${u}"`))}T(e.pairwise_overlap)||t.push(g("pairwise_overlap","must be an object",e.pairwise_overlap))}return{valid:t.length===0,errors:t}}function ge(e){const t=he(e);if(!t.valid)throw new Error(`Snapshot validation failed:
2
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 N(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 be={low:600,medium:1e3,high:2e3},ye=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 be[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 a;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=((a=e[s])==null?void 0:a.depends_on)??[];for(const c of r){const u=o.get(c);u!==void 0&&u.push(s),n.set(s,(n.get(s)??0)+1)}}const i=new Map,l=[];let f=t.filter(s=>(n.get(s)??0)===0).sort();for(const s of f)i.set(s,0);for(;f.length>0;){const s=[];for(const r of f){l.push(r);const c=(o.get(r)??[]).slice().sort();for(const u of c){const p=(n.get(u)??0)-1;n.set(u,p);const _=i.get(r)??0,b=i.get(u)??0;i.set(u,Math.max(b,_+1)),p===0&&s.push(u)}}f=s.sort()}if(l.length!==t.length){const s=new Set(l),r=t.filter(c=>!s.has(c));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`,f=[...n,l],a=te(o,f);for(const r of a)for(const c of r.results)if(c.op_id===l&&c.guard_result==="pass")return"complete";for(const r of a)for(const c of r.results)if(f.includes(c.op_id)&&c.status==="failed")return"failed";for(const r of a)for(const c of r.results)if(f.includes(c.op_id)&&c.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 f of t)if(f.operations.includes(n))for(const a of f.results)a.op_id===n&&a.guard_result!==null&&(o=a.guard_result,i=a.guard_output,a.guard_result==="pass"&&(l=a.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):ye.has(n.action)&&n.file&&t.add(n.file);return Array.from(t)}function Me(e,t,n){const o=ne(n,e.id),i=o.map(c=>c.id);let l=null,f=null,a=null;for(const c of o)for(const u of c.results)u.op_id===e.id&&u.guard_result!==null&&(l=u.guard_result,f=u.guard_output,a=u.guard_ran_at);const s=e.ki_estimate!==null&&e.ki_estimate!==void 0?e.ki_estimate:we(e,t),r=je(e.shape);return{...e,ki_estimate:s,shape:r,dispatch_ids:i,attempt_count:0,guard_result:l,guard_output:f,guard_ran_at:a,blast_radius:[],conflict:{detected:!1,competing_op:null,op_key:null,resolution:null},tokens_actual:null}}function je(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 Te(e,t,n,o){const i=`${e}.guard`,l=ne(o,i),f=l.map(c=>c.id);let a=null,s=null,r=null;for(const c of l)for(const u of c.results)u.op_id===i&&u.guard_result!==null&&(a=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:f,attempt_count:0,guard_result:a,guard_output:s,guard_ran_at:r,blast_radius:[],conflict:{detected:!1,competing_op:null,op_key:null,resolution:null},tokens_actual:null}}function Ie(e,t){const n={},{write_multiplier:o,read_multiplier:i,hit_probability:l}=t;for(const[f,a]of Object.entries(e))a===null?n[f]=null:n[f]=Math.round(a*((1-l)*o+l*i));return n}function xe(e,t,n){const o=Array.from(e.keys()),i={};for(let l=0;l<o.length;l++)for(let f=l+1;f<o.length;f++){const a=o[l],s=o[f];if(a===void 0||s===void 0)continue;const[r,c]=a<s?[a,s]:[s,a],u=`${r}:${c}`,p=e.get(a)??[],_=e.get(s)??[],b=new Set(p.map(w=>w.file).filter(w=>w!==null)),v=new Set(_.map(w=>w.file).filter(w=>w!==null)),y=Array.from(b).filter(w=>v.has(w));if(y.length===0)continue;const k=t.get(a)??"pending",$=t.get(s)??"pending",M=k==="complete"&&$==="complete";let P=0;for(const w of y)P+=M?n(w):0;i[u]=P}return i}function ze(e){return(Array.isArray(e)?e:Object.values(e)).map(n=>n.type===void 0?{...n,type:"generative"}:n)}function Pe(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),f=$e(e.optimization.context_window_per_tier,t.contextWindowPerTier),a=Ie(l,i),{waves:s}=ee(e.milestones),r=ze(e.operations),c=new Map;for(const m of r){const d=c.get(m.milestone)??[];d.push(m),c.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 p=Z(Array.from(u),t),_=m=>p.get(m)??0,b=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 j=(c.get(m)??[]).map(I=>I.id),T=d.depends_on.map(I=>b.get(I)??"pending"),x=Ae(m,d,j,o,T);b.set(m,x)}const y={},k=[];for(const m of v){const d=e.milestones[m];if(d===void 0)continue;const j=s.get(m)??0,T=c.get(m)??[],x=T.map(g=>g.id),I=b.get(m)??"pending",D=d.depends_on.map(g=>b.get(g)??"pending"),le=d.depends_on.length===0||D.every(g=>g==="complete"),ue=D.every(g=>g!=="failed"),ae=d.pending===null&&le&&ue,E=te(o,[...x,`${m}.guard`]),ce=E.length>0?E.map(g=>g.started_at).sort()[0]??null:null;let F,q,B;if(d.guard){const g=Oe(m,o);F=g.guard_result,q=g.guard_output,B=g.completed_at}else F=null,q=null,B=null;const R=Se(T),K=R.reduce((g,A)=>g+_(A),0),W=T.map(g=>Me(g,d.effort,o));let z=null,G=0,L=!1;for(const g of W){if(g.ki_estimate===null){L=!0;break}G+=g.ki_estimate}L||(z=G);let V=null;const Y=d.model;if(Y!==null&&z!==null){const g=a[Y]??null;if(g!==null){const A=N(K);V=g+A+z}}let H=null;const J=E.filter(g=>g.completed_at!==null&&g.results.length>0&&g.results.every(A=>A.status==="complete"||A.status==="skipped"));J.length>0&&(H=J.reduce((g,A)=>g+A.turns.reduce((pe,Q)=>pe+Q.input_tokens+Q.output_tokens,0),0));const fe=Te(m,d,x,o);k.push(...W,fe),y[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:j,eligible:ae,status:I,started_at:ce,completed_at:B,guard_result:F,guard_output:q,artifacts:R,si_bytes:K,ki_estimate:z,tokens_estimated:V,tokens_actual:H}}const $=xe(c,b,_),M=Ee(e,y),w={tokens_naive:(()=>{let m=0;for(const d of Object.values(y))if(d.eligible&&d.status==="pending"&&d.model!==null){const j=l[d.model]??t.bPerTier[d.model]??0;m+=j+(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:a,context_window_per_tier:f},C={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:y,operations:k,pairwise_overlap:$,open_questions:M};return he(C),C}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 f=i.status==="pending-surfaced";n.push({id:`q:${o}`,text:l.pending,blocking:o,surfaced:f,raised_at_dispatch:null,raised_at_turn:null,answered:!1,answer:null})}return n}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 Fe(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 qe(e){if(!e)return"";const t=e.indexOf(":");return t>=0?e.slice(t+1):e}function Be(e,t,n){let o=!0;const i=[];for(const l of e){const f=t[l];if(f===void 0)continue;const a=n.filter(r=>r.milestone===l&&r.action!=="guard");a.some(r=>r.type==="generative")&&(o=!1),i.push(`## Milestone: ${l}`),i.push(f.description),f.rationale&&i.push(`
4
- *Rationale:* ${f.rationale}`),i.push(`
5
- ### Operations:`);for(const r of a){const c=r.file?` ${r.file}`:"",u=r.symbol?` (${r.symbol})`:"";if(i.push(`- [${r.id}] ${r.action}${c}${u}`),r.shape&&r.shape.kind!==null){const p=r.shape;if(p.kind==="doc")i.push(` Description: ${p.description}`),i.push(` Objective: ${p.objective}`),p.required_sections&&p.required_sections.length>0&&i.push(` Required sections: ${p.required_sections.join(", ")}`);else if(p.kind==="structured-output")i.push(` Schema:
6
- `+JSON.stringify(p.schema,null,2).split(`
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",M=t.get(s)??"pending",k=A==="complete"&&M==="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 j=(a.get(m)??[]).map(P=>P.id),x=d.depends_on.map(P=>y.get(P)??"pending"),I=Ae(m,d,j,o,x);y.set(m,I)}const b={},A=[];for(const m of v){const d=e.milestones[m];if(d===void 0)continue;const j=s.get(m)??0,x=a.get(m)??[],I=x.map(h=>h.id),P=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,E=te(o,[...I,`${m}.guard`]),ae=E.length>0?E.map(h=>h.started_at).sort()[0]??null:null;let N,B,D;if(d.guard){const h=Oe(m,o);N=h.guard_result,B=h.guard_output,D=h.completed_at}else N=null,B=null,D=null;const R=Se(x),K=R.reduce((h,O)=>h+_(O),0),W=x.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:j,eligible:ce,status:P,started_at:ae,completed_at:D,guard_result:N,guard_output:B,artifacts:R,si_bytes:K,ki_estimate:z,tokens_estimated:H,tokens_actual:V}}const M=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 j=l[d.model]??t.bPerTier[d.model]??0;m+=j+(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:M,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 Ne="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 Be(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 De(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
+ *Rationale:* ${p.rationale}`),i.push(`
5
+ ### Operations:`);for(const r of c){const a=r.file?` ${r.file}`:"",u=r.symbol?` (${r.symbol})`:"";if(i.push(`- [${r.id}] ${r.action}${a}${u}`),r.shape&&r.shape.kind!==null){const f=r.shape;if(f.kind==="doc")i.push(` Description: ${f.description}`),i.push(` Objective: ${f.objective}`),f.required_sections&&f.required_sections.length>0&&i.push(` Required sections: ${f.required_sections.join(", ")}`);else if(f.kind==="structured-output")i.push(` Schema:
6
+ `+JSON.stringify(f.schema,null,2).split(`
7
7
  `).map(_=>` ${_}`).join(`
8
- `));else if("ops"in p&&Array.isArray(p.ops))for(const _ of p.ops){const b=_.to!==null?` → ${_.to}`:"",v=_.target!==null?` "${_.target}"`:"";i.push(` - ${_.op}${v}${b}`)}}}const s=f.guard;i.push(`
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
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),f=N(l),a=e.reduce((s,r)=>{var c;return s+(((c=n.snapshot.milestones[r])==null?void 0:c.ki_estimate)??0)},0);return o+f+a}function Ne(e,t,n){const o=ie(t,n.snapshot,n.deps),i=[...e].sort((a,s)=>{var u,p;const r=((u=n.snapshot.milestones[a])==null?void 0:u.ki_estimate)??0,c=((p=n.snapshot.milestones[s])==null?void 0:p.ki_estimate)??0;return r-c||a.localeCompare(s)}),l=[];let f=[];for(const a of i){const s=[...f,a],r=X(s,t,n);r!==null&&r>o&&f.length>0?(l.push(f),f=[a]):f=s}return f.length>0&&l.push(f),l.filter(a=>{const s=X(a,t,n);return s===null||s<=o})}function Ce(e,t,n,o){const i=e[0]??"",l=o.snapshot.milestones[i],f=(l==null?void 0:l.effort)??null,a=[];for(const k of e)for(const $ of o.opsByMilestone.get(k)??[])a.push($.id);const s=re(e,o),r=s.reduce((k,$)=>k+(o.sizeMap.get($)??0),0),c=se(n,o.snapshot,o.deps),u=e.reduce((k,$)=>{var M;return k+(((M=o.snapshot.milestones[$])==null?void 0:M.ki_estimate)??0)},0),p=c!==null?c+N(r)+u:null,_=ie(n,o.snapshot,o.deps),b=p!==null?p<=_:!0,v=qe((l==null?void 0:l.agent)??null),y=Be(e,o.snapshot.milestones,o.snapshot.operations);return{id:`${i}.dispatch.${t}`,milestones:e,operations:a,model:n,effort:f,two_stage:(l==null?void 0:l.two_stage)??!1,provider:null,agent_name:v,mcp_servers:null,resolved_max_tokens:null,background:!0,prompt:y,context_files:s,si_bytes:r,tokens_estimated:p,fits_context_window:b,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 De(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 p=o.get(u.milestone)??[];p.push(u),o.set(u.milestone,p)}const i=new Map;for(const u of n){const p=e.milestones[u];if(p===void 0)continue;const _=Fe(o.get(u)??[]),b=p.model??"_null",v=`${_}:${b}`,y=i.get(v)??[];y.push(u),i.set(v,y)}const l=new Set;for(const u of n){const p=e.milestones[u];if(p!==void 0){l.add(p.context);for(const _ of p.read_only)l.add(_);for(const _ of o.get(u)??[])_.file&&l.add(_.file)}}const f=Z(Array.from(l),t),a={snapshot:e,deps:t,sizeMap:f,opsByMilestone:o},s=[];let r=0;const c=Array.from(i.keys()).sort();for(const u of c){const p=i.get(u);if(p===void 0)continue;const _=u.slice(u.indexOf(":")+1),b=_!=="_null"?_:null,v=Ne(p,b,a);for(const y of v)s.push(Ce(y,r++,b,a))}return s}function Re(e,t){let n=0;for(const o of oe(e)){const i=e.milestones[o];if(i===void 0)continue;const l=Ke(i.model,e,t)??0;n+=l+(i.ki_estimate??0)}return n}function Ke(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=Re;exports.optimize=De;exports.snapshot=Pe;exports.topoSortMilestones=ee;
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=De((l==null?void 0:l.agent)??null),b=Fe(e,o.snapshot.milestones,o.snapshot.operations),A=b!==null?"model-dispatch":"guard-only",M=b!==null?Ne: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:M,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 _=Be(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;
package/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- const pe = /* @__PURE__ */ new Set([
1
+ const fe = /* @__PURE__ */ new Set([
2
2
  "create",
3
3
  "modify-signature",
4
4
  "modify-body",
@@ -12,62 +12,62 @@ const pe = /* @__PURE__ */ new Set([
12
12
  "complete",
13
13
  "failed",
14
14
  "skipped"
15
- ]), _e = /* @__PURE__ */ new Set([
15
+ ]), me = /* @__PURE__ */ new Set([
16
16
  "pending",
17
17
  "in_progress",
18
18
  "complete",
19
19
  "failed",
20
20
  "skipped"
21
21
  ]);
22
- function S(e) {
22
+ function T(e) {
23
23
  return typeof e == "object" && e !== null && !Array.isArray(e);
24
24
  }
25
- function O(e) {
25
+ function S(e) {
26
26
  return typeof e == "string";
27
27
  }
28
28
  function g(e, t, n) {
29
29
  return { path: e, message: t, value: n };
30
30
  }
31
- function me(e) {
31
+ function _e(e) {
32
32
  const t = [];
33
- if (!S(e))
33
+ if (!T(e))
34
34
  return { valid: !1, errors: [g("", "root must be an object")] };
35
35
  for (const o of ["snapshot_at", "description"])
36
- O(e[o]) || t.push(g(o, "required string field missing", e[o]));
37
- O(e.plan) || t.push(g("plan", "required string", e.plan));
36
+ S(e[o]) || t.push(g(o, "required string field missing", e[o]));
37
+ S(e.plan) || t.push(g("plan", "required string", e.plan));
38
38
  const n = e.milestones;
39
- if (!S(n))
39
+ if (!T(n))
40
40
  t.push(g("milestones", "must be an object", n));
41
41
  else {
42
42
  const o = Object.keys(n), i = new Set(o);
43
43
  for (const s of o) {
44
44
  const r = n[s];
45
- if (!S(r)) {
45
+ if (!T(r)) {
46
46
  t.push(g(`milestones.${s}`, "must be an object", r));
47
47
  continue;
48
48
  }
49
- const c = r.eligible, u = r.pending;
50
- c === !0 && u !== null && u !== void 0 && t.push(
49
+ const a = r.eligible, u = r.pending;
50
+ a === !0 && u !== null && u !== void 0 && t.push(
51
51
  g(
52
52
  `milestones.${s}`,
53
53
  "D-07 violation: eligible=true but pending not null"
54
54
  )
55
- ), typeof c != "boolean" && t.push(
56
- g(`milestones.${s}.eligible`, "must be boolean", c)
55
+ ), typeof a != "boolean" && t.push(
56
+ g(`milestones.${s}.eligible`, "must be boolean", a)
57
57
  );
58
- const p = r.status;
59
- (!O(p) || !de.has(p)) && t.push(g(`milestones.${s}.status`, "invalid status", p));
60
- const m = r.wave;
61
- (typeof m != "number" || !Number.isInteger(m) || m < 0) && t.push(
62
- g(`milestones.${s}.wave`, "must be non-negative integer", m)
58
+ const f = r.status;
59
+ (!S(f) || !de.has(f)) && t.push(g(`milestones.${s}.status`, "invalid status", f));
60
+ const _ = r.wave;
61
+ (typeof _ != "number" || !Number.isInteger(_) || _ < 0) && t.push(
62
+ g(`milestones.${s}.wave`, "must be non-negative integer", _)
63
63
  );
64
- const b = r.ki_estimate;
65
- b != null && typeof b != "number" && t.push(
66
- g(`milestones.${s}.ki_estimate`, "must be number|null", b)
64
+ const y = r.ki_estimate;
65
+ y != null && typeof y != "number" && t.push(
66
+ g(`milestones.${s}.ki_estimate`, "must be number|null", y)
67
67
  );
68
68
  for (const v of ["tokens_estimated", "tokens_actual"]) {
69
- const y = r[v];
70
- y != null && typeof y != "number" && t.push(g(`milestones.${s}.${v}`, "must be number|null", y));
69
+ const b = r[v];
70
+ b != null && typeof b != "number" && t.push(g(`milestones.${s}.${v}`, "must be number|null", b));
71
71
  }
72
72
  Array.isArray(r.artifacts) || t.push(
73
73
  g(
@@ -84,17 +84,17 @@ function me(e) {
84
84
  t.push(g("operations", "must be an array", l));
85
85
  else
86
86
  for (const s of l) {
87
- if (!S(s))
87
+ if (!T(s))
88
88
  continue;
89
- const r = O(s.id) ? s.id : "?", c = s.milestone;
90
- O(c) && !i.has(c) && t.push(
89
+ const r = S(s.id) ? s.id : "?", a = s.milestone;
90
+ S(a) && !i.has(a) && t.push(
91
91
  g(
92
92
  `operations[${r}].milestone`,
93
- `unknown milestone "${c}"`
93
+ `unknown milestone "${a}"`
94
94
  )
95
95
  );
96
96
  const u = s.status;
97
- (!O(u) || !_e.has(u)) && t.push(
97
+ (!S(u) || !me.has(u)) && t.push(
98
98
  g(
99
99
  `operations[${r}].status`,
100
100
  "invalid operation status",
@@ -120,15 +120,15 @@ function me(e) {
120
120
  )
121
121
  );
122
122
  }
123
- const f = e.open_questions;
124
- if (!Array.isArray(f))
125
- t.push(g("open_questions", "must be an array", f));
123
+ const p = e.open_questions;
124
+ if (!Array.isArray(p))
125
+ t.push(g("open_questions", "must be an array", p));
126
126
  else
127
- for (const s of f) {
128
- if (!S(s))
127
+ for (const s of p) {
128
+ if (!T(s))
129
129
  continue;
130
130
  const r = s.blocking;
131
- (!O(r) || !i.has(r)) && t.push(
131
+ (!S(r) || !i.has(r)) && t.push(
132
132
  g("open_questions", `unknown blocking milestone "${r}"`)
133
133
  ), typeof s.surfaced != "boolean" && t.push(
134
134
  g(
@@ -144,15 +144,15 @@ function me(e) {
144
144
  )
145
145
  );
146
146
  }
147
- const a = e.dispatch_units;
148
- if (a !== void 0)
149
- if (!Array.isArray(a))
147
+ const c = e.dispatch_units;
148
+ if (c !== void 0)
149
+ if (!Array.isArray(c))
150
150
  t.push(
151
- g("dispatch_units", "must be an array if present", a)
151
+ g("dispatch_units", "must be an array if present", c)
152
152
  );
153
153
  else
154
- for (const s of a) {
155
- if (!S(s))
154
+ for (const s of c) {
155
+ if (!T(s))
156
156
  continue;
157
157
  const r = s.fits_context_window;
158
158
  typeof r != "boolean" && t.push(
@@ -162,17 +162,17 @@ function me(e) {
162
162
  r
163
163
  )
164
164
  );
165
- const c = s.milestones;
166
- if (Array.isArray(c))
167
- for (const u of c)
168
- O(u) && !i.has(u) && t.push(
165
+ const a = s.milestones;
166
+ if (Array.isArray(a))
167
+ for (const u of a)
168
+ S(u) && !i.has(u) && t.push(
169
169
  g(
170
170
  `dispatch_units[${s.id}].milestones`,
171
171
  `unknown milestone "${u}"`
172
172
  )
173
173
  );
174
174
  }
175
- S(e.pairwise_overlap) || t.push(
175
+ T(e.pairwise_overlap) || t.push(
176
176
  g(
177
177
  "pairwise_overlap",
178
178
  "must be an object",
@@ -183,7 +183,7 @@ function me(e) {
183
183
  return { valid: t.length === 0, errors: t };
184
184
  }
185
185
  function he(e) {
186
- const t = me(e);
186
+ const t = _e(e);
187
187
  if (!t.valid)
188
188
  throw new Error(
189
189
  `Snapshot validation failed:
@@ -199,7 +199,7 @@ const U = {
199
199
  py: 6.3,
200
200
  default: 4
201
201
  };
202
- function C(e, t) {
202
+ function N(e, t) {
203
203
  if (e <= 0)
204
204
  return 0;
205
205
  const o = U[""] ?? U.default ?? 4;
@@ -218,8 +218,8 @@ const ge = {
218
218
  low: 600,
219
219
  medium: 1e3,
220
220
  high: 2e3
221
- }, be = pe;
222
- function ye(e) {
221
+ }, ye = fe;
222
+ function be(e) {
223
223
  const t = e.properties ?? {};
224
224
  return Object.keys(t).length || Object.keys(e).length;
225
225
  }
@@ -238,7 +238,7 @@ function ve(e, t) {
238
238
  return ge[t ?? "medium"] ?? 1e3;
239
239
  if (o === "structured-output") {
240
240
  const i = "schema" in n && n.schema ? n.schema : {};
241
- return ye(i) * 50;
241
+ return be(i) * 50;
242
242
  }
243
243
  return 0;
244
244
  }
@@ -263,37 +263,37 @@ function ke(e, t) {
263
263
  return o;
264
264
  }
265
265
  function $e(e) {
266
- var a;
266
+ var c;
267
267
  const t = Object.keys(e), n = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map();
268
268
  for (const s of t)
269
269
  n.has(s) || n.set(s, 0), o.has(s) || o.set(s, []);
270
270
  for (const s of t) {
271
- const r = ((a = e[s]) == null ? void 0 : a.depends_on) ?? [];
272
- for (const c of r) {
273
- const u = o.get(c);
271
+ const r = ((c = e[s]) == null ? void 0 : c.depends_on) ?? [];
272
+ for (const a of r) {
273
+ const u = o.get(a);
274
274
  u !== void 0 && u.push(s), n.set(s, (n.get(s) ?? 0) + 1);
275
275
  }
276
276
  }
277
277
  const i = /* @__PURE__ */ new Map(), l = [];
278
- let f = t.filter((s) => (n.get(s) ?? 0) === 0).sort();
279
- for (const s of f)
278
+ let p = t.filter((s) => (n.get(s) ?? 0) === 0).sort();
279
+ for (const s of p)
280
280
  i.set(s, 0);
281
- for (; f.length > 0; ) {
281
+ for (; p.length > 0; ) {
282
282
  const s = [];
283
- for (const r of f) {
283
+ for (const r of p) {
284
284
  l.push(r);
285
- const c = (o.get(r) ?? []).slice().sort();
286
- for (const u of c) {
287
- const p = (n.get(u) ?? 0) - 1;
288
- n.set(u, p);
289
- const m = i.get(r) ?? 0, b = i.get(u) ?? 0;
290
- i.set(u, Math.max(b, m + 1)), p === 0 && s.push(u);
285
+ const a = (o.get(r) ?? []).slice().sort();
286
+ for (const u of a) {
287
+ const f = (n.get(u) ?? 0) - 1;
288
+ n.set(u, f);
289
+ const _ = i.get(r) ?? 0, y = i.get(u) ?? 0;
290
+ i.set(u, Math.max(y, _ + 1)), f === 0 && s.push(u);
291
291
  }
292
292
  }
293
- f = s.sort();
293
+ p = s.sort();
294
294
  }
295
295
  if (l.length !== t.length) {
296
- const s = new Set(l), r = t.filter((c) => !s.has(c));
296
+ const s = new Set(l), r = t.filter((a) => !s.has(a));
297
297
  throw new Error(
298
298
  `snapshot(): cycle detected in milestone depends_on graph. Nodes involved: [${r.join(", ")}]`
299
299
  );
@@ -312,20 +312,20 @@ function te(e, t) {
312
312
  return e.filter((n) => n.operations.includes(t));
313
313
  }
314
314
  function Ae(e, t, n, o, i) {
315
- const l = `${e}.guard`, f = [...n, l], a = ee(o, f);
316
- for (const r of a)
317
- for (const c of r.results)
318
- if (c.op_id === l && c.guard_result === "pass")
315
+ const l = `${e}.guard`, p = [...n, l], c = ee(o, p);
316
+ for (const r of c)
317
+ for (const a of r.results)
318
+ if (a.op_id === l && a.guard_result === "pass")
319
319
  return "complete";
320
- for (const r of a)
321
- for (const c of r.results)
322
- if (f.includes(c.op_id) && c.status === "failed")
320
+ for (const r of c)
321
+ for (const a of r.results)
322
+ if (p.includes(a.op_id) && a.status === "failed")
323
323
  return "failed";
324
- for (const r of a)
325
- for (const c of r.results)
326
- if (f.includes(c.op_id) && // "in_progress" is not a dispatch_log result status (only complete/failed/skipped)
324
+ for (const r of c)
325
+ for (const a of r.results)
326
+ if (p.includes(a.op_id) && // "in_progress" is not a dispatch_log result status (only complete/failed/skipped)
327
327
  // but orchestrators may write it; guard defensively
328
- c.status === "in_progress")
328
+ a.status === "in_progress")
329
329
  return "in_progress";
330
330
  const s = i.length === 0 || i.every((r) => r === "complete");
331
331
  return t.pending !== null && s ? "pending-surfaced" : "pending";
@@ -333,10 +333,10 @@ function Ae(e, t, n, o, i) {
333
333
  function Oe(e, t) {
334
334
  const n = `${e}.guard`;
335
335
  let o = null, i = null, l = null;
336
- for (const f of t)
337
- if (f.operations.includes(n))
338
- for (const a of f.results)
339
- a.op_id === n && a.guard_result !== null && (o = a.guard_result, i = a.guard_output, a.guard_result === "pass" && (l = a.guard_ran_at));
336
+ for (const p of t)
337
+ if (p.operations.includes(n))
338
+ for (const c of p.results)
339
+ 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));
340
340
  return o !== null ? {
341
341
  guard_result: o,
342
342
  guard_output: i,
@@ -346,15 +346,15 @@ function Oe(e, t) {
346
346
  function Se(e) {
347
347
  const t = /* @__PURE__ */ new Set();
348
348
  for (const n of e)
349
- n.action === "move" && n.to_file ? t.add(n.to_file) : be.has(n.action) && n.file && t.add(n.file);
349
+ n.action === "move" && n.to_file ? t.add(n.to_file) : ye.has(n.action) && n.file && t.add(n.file);
350
350
  return Array.from(t);
351
351
  }
352
- function je(e, t, n) {
353
- const o = te(n, e.id), i = o.map((c) => c.id);
354
- let l = null, f = null, a = null;
355
- for (const c of o)
356
- for (const u of c.results)
357
- u.op_id === e.id && u.guard_result !== null && (l = u.guard_result, f = u.guard_output, a = u.guard_ran_at);
352
+ function Te(e, t, n) {
353
+ const o = te(n, e.id), i = o.map((a) => a.id);
354
+ let l = null, p = null, c = null;
355
+ for (const a of o)
356
+ for (const u of a.results)
357
+ u.op_id === e.id && u.guard_result !== null && (l = u.guard_result, p = u.guard_output, c = u.guard_ran_at);
358
358
  const s = e.ki_estimate !== null && e.ki_estimate !== void 0 ? e.ki_estimate : ve(e, t), r = Me(e.shape);
359
359
  return {
360
360
  ...e,
@@ -364,8 +364,8 @@ function je(e, t, n) {
364
364
  // TODO: stubbed as 0 — op-level attempt_count requires per-op dispatch log scan
365
365
  attempt_count: 0,
366
366
  guard_result: l,
367
- guard_output: f,
368
- guard_ran_at: a,
367
+ guard_output: p,
368
+ guard_ran_at: c,
369
369
  // TODO: stubbed — requires gitnexus_impact MCP call (future work)
370
370
  blast_radius: [],
371
371
  // TODO: stubbed — requires same-wave op-key collision scan (future work)
@@ -397,12 +397,12 @@ function Me(e) {
397
397
  }
398
398
  return e;
399
399
  }
400
- function Te(e, t, n, o) {
401
- const i = `${e}.guard`, l = te(o, i), f = l.map((c) => c.id);
402
- let a = null, s = null, r = null;
403
- for (const c of l)
404
- for (const u of c.results)
405
- u.op_id === i && u.guard_result !== null && (a = u.guard_result, s = u.guard_output, r = u.guard_ran_at);
400
+ function je(e, t, n, o) {
401
+ const i = `${e}.guard`, l = te(o, i), p = l.map((a) => a.id);
402
+ let c = null, s = null, r = null;
403
+ for (const a of l)
404
+ for (const u of a.results)
405
+ u.op_id === i && u.guard_result !== null && (c = u.guard_result, s = u.guard_output, r = u.guard_ran_at);
406
406
  return {
407
407
  id: i,
408
408
  milestone: e,
@@ -425,9 +425,9 @@ function Te(e, t, n, o) {
425
425
  authored_by: "synthesized",
426
426
  status: "pending",
427
427
  shape: null,
428
- dispatch_ids: f,
428
+ dispatch_ids: p,
429
429
  attempt_count: 0,
430
- guard_result: a,
430
+ guard_result: c,
431
431
  guard_output: s,
432
432
  guard_ran_at: r,
433
433
  blast_radius: [],
@@ -446,117 +446,117 @@ function xe(e, t) {
446
446
  read_multiplier: i,
447
447
  hit_probability: l
448
448
  } = t;
449
- for (const [f, a] of Object.entries(e))
450
- a === null ? n[f] = null : n[f] = Math.round(a * ((1 - l) * o + l * i));
449
+ for (const [p, c] of Object.entries(e))
450
+ c === null ? n[p] = null : n[p] = Math.round(c * ((1 - l) * o + l * i));
451
451
  return n;
452
452
  }
453
453
  function Ie(e, t, n) {
454
454
  const o = Array.from(e.keys()), i = {};
455
455
  for (let l = 0; l < o.length; l++)
456
- for (let f = l + 1; f < o.length; f++) {
457
- const a = o[l], s = o[f];
458
- if (a === void 0 || s === void 0)
456
+ for (let p = l + 1; p < o.length; p++) {
457
+ const c = o[l], s = o[p];
458
+ if (c === void 0 || s === void 0)
459
459
  continue;
460
- const [r, c] = a < s ? [a, s] : [s, a], u = `${r}:${c}`, p = e.get(a) ?? [], m = e.get(s) ?? [], b = new Set(
461
- p.map((w) => w.file).filter((w) => w !== null)
460
+ const [r, a] = c < s ? [c, s] : [s, c], u = `${r}:${a}`, f = e.get(c) ?? [], _ = e.get(s) ?? [], y = new Set(
461
+ f.map((w) => w.file).filter((w) => w !== null)
462
462
  ), v = new Set(
463
- m.map((w) => w.file).filter((w) => w !== null)
464
- ), y = Array.from(b).filter((w) => v.has(w));
465
- if (y.length === 0)
463
+ _.map((w) => w.file).filter((w) => w !== null)
464
+ ), b = Array.from(y).filter((w) => v.has(w));
465
+ if (b.length === 0)
466
466
  continue;
467
- const k = t.get(a) ?? "pending", $ = t.get(s) ?? "pending", j = k === "complete" && $ === "complete";
468
- let P = 0;
469
- for (const w of y)
470
- P += j ? n(w) : 0;
471
- i[u] = P;
467
+ const A = t.get(c) ?? "pending", M = t.get(s) ?? "pending", k = A === "complete" && M === "complete";
468
+ let $ = 0;
469
+ for (const w of b)
470
+ $ += k ? n(w) : 0;
471
+ i[u] = $;
472
472
  }
473
473
  return i;
474
474
  }
475
- function ze(e) {
475
+ function Pe(e) {
476
476
  return (Array.isArray(e) ? e : Object.values(e)).map(
477
477
  (n) => n.type === void 0 ? { ...n, type: "generative" } : n
478
478
  );
479
479
  }
480
- function Ne(e, t) {
481
- const n = (/* @__PURE__ */ new Date()).toISOString(), o = t.dispatchLog ?? e.dispatch_log, i = e.optimization.sentinel_fanout, l = we(e.optimization.b_per_tier, t.bPerTier), f = ke(
480
+ function Re(e, t) {
481
+ const n = (/* @__PURE__ */ new Date()).toISOString(), o = t.dispatchLog ?? e.dispatch_log, i = e.optimization.sentinel_fanout, l = we(e.optimization.b_per_tier, t.bPerTier), p = ke(
482
482
  e.optimization.context_window_per_tier,
483
483
  t.contextWindowPerTier
484
- ), a = xe(l, i), { waves: s } = $e(e.milestones), r = ze(e.operations), c = /* @__PURE__ */ new Map();
485
- for (const _ of r) {
486
- const d = c.get(_.milestone) ?? [];
487
- d.push(_), c.set(_.milestone, d);
484
+ ), c = xe(l, i), { waves: s } = $e(e.milestones), r = Pe(e.operations), a = /* @__PURE__ */ new Map();
485
+ for (const m of r) {
486
+ const d = a.get(m.milestone) ?? [];
487
+ d.push(m), a.set(m.milestone, d);
488
488
  }
489
489
  const u = /* @__PURE__ */ new Set();
490
- for (const _ of r)
491
- _.file && u.add(_.file), _.action === "move" && _.to_file && u.add(_.to_file);
492
- const p = Z(Array.from(u), t), m = (_) => p.get(_) ?? 0, b = /* @__PURE__ */ new Map(), v = Array.from(s.entries()).sort((_, d) => _[1] - d[1] || _[0].localeCompare(d[0])).map(([_]) => _);
493
- for (const _ of v) {
494
- const d = e.milestones[_];
490
+ for (const m of r)
491
+ m.file && u.add(m.file), m.action === "move" && m.to_file && u.add(m.to_file);
492
+ const f = Z(Array.from(u), t), _ = (m) => f.get(m) ?? 0, y = /* @__PURE__ */ new Map(), v = Array.from(s.entries()).sort((m, d) => m[1] - d[1] || m[0].localeCompare(d[0])).map(([m]) => m);
493
+ for (const m of v) {
494
+ const d = e.milestones[m];
495
495
  if (d === void 0)
496
496
  continue;
497
- const M = (c.get(_) ?? []).map((x) => x.id), T = d.depends_on.map(
498
- (x) => b.get(x) ?? "pending"
499
- ), I = Ae(
500
- _,
497
+ const j = (a.get(m) ?? []).map((I) => I.id), x = d.depends_on.map(
498
+ (I) => y.get(I) ?? "pending"
499
+ ), P = Ae(
500
+ m,
501
501
  d,
502
- M,
502
+ j,
503
503
  o,
504
- T
504
+ x
505
505
  );
506
- b.set(_, I);
506
+ y.set(m, P);
507
507
  }
508
- const y = {}, k = [];
509
- for (const _ of v) {
510
- const d = e.milestones[_];
508
+ const b = {}, A = [];
509
+ for (const m of v) {
510
+ const d = e.milestones[m];
511
511
  if (d === void 0)
512
512
  continue;
513
- const M = s.get(_) ?? 0, T = c.get(_) ?? [], I = T.map((h) => h.id), x = b.get(_) ?? "pending", N = d.depends_on.map(
514
- (h) => b.get(h) ?? "pending"
515
- ), re = d.depends_on.length === 0 || N.every((h) => h === "complete"), le = N.every((h) => h !== "failed"), ue = d.pending === null && re && le, E = ee(o, [
516
- ...I,
517
- `${_}.guard`
518
- ]), ae = E.length > 0 ? E.map((h) => h.started_at).sort()[0] ?? null : null;
519
- let F, q, B;
513
+ const j = s.get(m) ?? 0, x = a.get(m) ?? [], P = x.map((h) => h.id), I = y.get(m) ?? "pending", C = d.depends_on.map(
514
+ (h) => y.get(h) ?? "pending"
515
+ ), re = d.depends_on.length === 0 || C.every((h) => h === "complete"), le = C.every((h) => h !== "failed"), ue = d.pending === null && re && le, E = ee(o, [
516
+ ...P,
517
+ `${m}.guard`
518
+ ]), ce = E.length > 0 ? E.map((h) => h.started_at).sort()[0] ?? null : null;
519
+ let B, D, F;
520
520
  if (d.guard) {
521
- const h = Oe(_, o);
522
- F = h.guard_result, q = h.guard_output, B = h.completed_at;
521
+ const h = Oe(m, o);
522
+ B = h.guard_result, D = h.guard_output, F = h.completed_at;
523
523
  } else
524
- F = null, q = null, B = null;
525
- const R = Se(T), K = R.reduce((h, A) => h + m(A), 0), W = T.map(
526
- (h) => je(h, d.effort, o)
524
+ B = null, D = null, F = null;
525
+ const R = Se(x), K = R.reduce((h, O) => h + _(O), 0), W = x.map(
526
+ (h) => Te(h, d.effort, o)
527
527
  );
528
- let z = null, G = 0, L = !1;
528
+ let z = null, G = 0, Y = !1;
529
529
  for (const h of W) {
530
530
  if (h.ki_estimate === null) {
531
- L = !0;
531
+ Y = !0;
532
532
  break;
533
533
  }
534
534
  G += h.ki_estimate;
535
535
  }
536
- L || (z = G);
537
- let V = null;
538
- const Y = d.model;
539
- if (Y !== null && z !== null) {
540
- const h = a[Y] ?? null;
536
+ Y || (z = G);
537
+ let H = null;
538
+ const L = d.model;
539
+ if (L !== null && z !== null) {
540
+ const h = c[L] ?? null;
541
541
  if (h !== null) {
542
- const A = C(K);
543
- V = h + A + z;
542
+ const O = N(K);
543
+ H = h + O + z;
544
544
  }
545
545
  }
546
- let H = null;
546
+ let V = null;
547
547
  const J = E.filter(
548
548
  (h) => h.completed_at !== null && h.results.length > 0 && h.results.every(
549
- (A) => A.status === "complete" || A.status === "skipped"
549
+ (O) => O.status === "complete" || O.status === "skipped"
550
550
  )
551
551
  );
552
- J.length > 0 && (H = J.reduce((h, A) => h + A.turns.reduce((fe, Q) => fe + Q.input_tokens + Q.output_tokens, 0), 0));
553
- const ce = Te(
554
- _,
552
+ J.length > 0 && (V = J.reduce((h, O) => h + O.turns.reduce((pe, Q) => pe + Q.input_tokens + Q.output_tokens, 0), 0));
553
+ const ae = je(
554
+ m,
555
555
  d,
556
- I,
556
+ P,
557
557
  o
558
558
  );
559
- k.push(...W, ce), y[_] = {
559
+ A.push(...W, ae), b[m] = {
560
560
  description: d.description,
561
561
  ...d.rationale !== void 0 ? { rationale: d.rationale } : {},
562
562
  authored_by: d.authored_by,
@@ -570,42 +570,42 @@ function Ne(e, t) {
570
570
  two_stage: d.two_stage,
571
571
  read_only: d.read_only,
572
572
  guard: d.guard,
573
- context: `contexts/${_}.md`,
574
- wave: M,
573
+ context: `contexts/${m}.md`,
574
+ wave: j,
575
575
  eligible: ue,
576
- status: x,
577
- started_at: ae,
578
- completed_at: B,
579
- guard_result: F,
580
- guard_output: q,
576
+ status: I,
577
+ started_at: ce,
578
+ completed_at: F,
579
+ guard_result: B,
580
+ guard_output: D,
581
581
  artifacts: R,
582
582
  si_bytes: K,
583
583
  ki_estimate: z,
584
- tokens_estimated: V,
585
- tokens_actual: H
584
+ tokens_estimated: H,
585
+ tokens_actual: V
586
586
  };
587
587
  }
588
- const $ = Ie(
589
- c,
590
- b,
591
- m
592
- ), j = Pe(e, y), w = {
588
+ const M = Ie(
589
+ a,
590
+ y,
591
+ _
592
+ ), k = ze(e, b), w = {
593
593
  tokens_naive: (() => {
594
- let _ = 0;
595
- for (const d of Object.values(y))
594
+ let m = 0;
595
+ for (const d of Object.values(b))
596
596
  if (d.eligible && d.status === "pending" && d.model !== null) {
597
- const M = l[d.model] ?? t.bPerTier[d.model] ?? 0;
598
- _ += M + (d.ki_estimate ?? 0);
597
+ const j = l[d.model] ?? t.bPerTier[d.model] ?? 0;
598
+ m += j + (d.ki_estimate ?? 0);
599
599
  }
600
- return _;
600
+ return m;
601
601
  })(),
602
602
  sentinel_fanout: i,
603
603
  context_window_override: e.optimization.context_window_override,
604
604
  b_override: e.optimization.b_override,
605
605
  b_per_tier: l,
606
- b_eff_per_tier: a,
607
- context_window_per_tier: f
608
- }, D = {
606
+ b_eff_per_tier: c,
607
+ context_window_per_tier: p
608
+ }, q = {
609
609
  snapshot_at: n,
610
610
  snapshot_version: 1,
611
611
  // callers may increment if persisting
@@ -623,25 +623,25 @@ function Ne(e, t) {
623
623
  terminal: e.terminal,
624
624
  ...e.assumed_baseline !== void 0 ? { assumed_baseline: e.assumed_baseline } : {},
625
625
  optimization: w,
626
- milestones: y,
627
- operations: k,
628
- pairwise_overlap: $,
629
- open_questions: j
626
+ milestones: b,
627
+ operations: A,
628
+ pairwise_overlap: M,
629
+ open_questions: k
630
630
  };
631
- return he(D), D;
631
+ return he(q), q;
632
632
  }
633
- function Pe(e, t) {
633
+ function ze(e, t) {
634
634
  const n = [];
635
635
  for (const [o, i] of Object.entries(t)) {
636
636
  const l = e.milestones[o];
637
637
  if (l === void 0 || l.pending === null)
638
638
  continue;
639
- const f = i.status === "pending-surfaced";
639
+ const p = i.status === "pending-surfaced";
640
640
  n.push({
641
641
  id: `q:${o}`,
642
642
  text: l.pending,
643
643
  blocking: o,
644
- surfaced: f,
644
+ surfaced: p,
645
645
  // TODO: scan dispatch_log notes for the turn/dispatch where question appeared
646
646
  raised_at_dispatch: null,
647
647
  raised_at_turn: null,
@@ -651,13 +651,14 @@ function Pe(e, t) {
651
651
  }
652
652
  return n;
653
653
  }
654
+ const Ee = "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.";
654
655
  function ne(e) {
655
656
  return Object.keys(e.milestones).filter((t) => {
656
657
  const n = e.milestones[t];
657
658
  return n !== void 0 && n.eligible === !0 && n.status === "pending";
658
659
  });
659
660
  }
660
- function Ee(e) {
661
+ function Be(e) {
661
662
  if (e.length === 0 || e.every((t) => t.type === "tool-call"))
662
663
  return "tool-call";
663
664
  for (const t of e) {
@@ -673,48 +674,50 @@ function Ee(e) {
673
674
  }
674
675
  return "code-config";
675
676
  }
676
- function Fe(e) {
677
+ function De(e) {
677
678
  if (!e)
678
679
  return "";
679
680
  const t = e.indexOf(":");
680
681
  return t >= 0 ? e.slice(t + 1) : e;
681
682
  }
682
- function qe(e, t, n) {
683
+ function Fe(e, t, n) {
683
684
  let o = !0;
684
685
  const i = [];
685
686
  for (const l of e) {
686
- const f = t[l];
687
- if (f === void 0)
687
+ const p = t[l];
688
+ if (p === void 0)
688
689
  continue;
689
- const a = n.filter(
690
+ const c = n.filter(
690
691
  (r) => r.milestone === l && r.action !== "guard"
691
692
  );
692
- a.some((r) => r.type === "generative") && (o = !1), i.push(`## Milestone: ${l}`), i.push(f.description), f.rationale && i.push(`
693
- *Rationale:* ${f.rationale}`), i.push(`
693
+ c.some((r) => r.type === "generative") && (o = !1), i.push(`## Milestone: ${l}`), i.push(p.description), p.rationale && i.push(`
694
+ *Rationale:* ${p.rationale}`), i.push(`
694
695
  ### Operations:`);
695
- for (const r of a) {
696
- const c = r.file ? ` ${r.file}` : "", u = r.symbol ? ` (${r.symbol})` : "";
697
- if (i.push(`- [${r.id}] ${r.action}${c}${u}`), r.shape && r.shape.kind !== null) {
698
- const p = r.shape;
699
- if (p.kind === "doc")
700
- i.push(` Description: ${p.description}`), i.push(` Objective: ${p.objective}`), p.required_sections && p.required_sections.length > 0 && i.push(
701
- ` Required sections: ${p.required_sections.join(", ")}`
696
+ for (const r of c) {
697
+ const a = r.file ? ` ${r.file}` : "", u = r.symbol ? ` (${r.symbol})` : "";
698
+ if (i.push(`- [${r.id}] ${r.action}${a}${u}`), r.shape && r.shape.kind !== null) {
699
+ const f = r.shape;
700
+ if (f.kind === "doc")
701
+ i.push(` Description: ${f.description}`), i.push(` Objective: ${f.objective}`), f.required_sections && f.required_sections.length > 0 && i.push(
702
+ ` Required sections: ${f.required_sections.join(", ")}`
702
703
  );
703
- else if (p.kind === "structured-output")
704
+ else if (f.kind === "structured-output")
704
705
  i.push(
705
706
  ` Schema:
706
- ` + JSON.stringify(p.schema, null, 2).split(`
707
- `).map((m) => ` ${m}`).join(`
707
+ ` + JSON.stringify(f.schema, null, 2).split(`
708
+ `).map((_) => ` ${_}`).join(`
708
709
  `)
709
710
  );
710
- else if ("ops" in p && Array.isArray(p.ops))
711
- for (const m of p.ops) {
712
- const b = m.to !== null ? ` → ${m.to}` : "", v = m.target !== null ? ` "${m.target}"` : "";
713
- i.push(` - ${m.op}${v}${b}`);
711
+ else if ("ops" in f && Array.isArray(f.ops))
712
+ for (const _ of f.ops) {
713
+ const y = _.to !== null ? ` → ${_.to}` : "", v = _.target !== null ? ` "${_.target}"` : "";
714
+ if (i.push(` - ${_.op}${v}${y}`), _.type_spec)
715
+ for (const [b, A] of Object.entries(_.type_spec))
716
+ i.push(` - ${b}: ${A}`);
714
717
  }
715
718
  }
716
719
  }
717
- const s = f.guard;
720
+ const s = p.guard;
718
721
  i.push(`
719
722
  ### Guard: ${s ?? "none"}`), i.push("");
720
723
  }
@@ -763,63 +766,65 @@ function X(e, t, n) {
763
766
  const l = ie(e, n).reduce(
764
767
  (s, r) => s + (n.sizeMap.get(r) ?? 0),
765
768
  0
766
- ), f = C(l), a = e.reduce(
769
+ ), p = N(l), c = e.reduce(
767
770
  (s, r) => {
768
- var c;
769
- return s + (((c = n.snapshot.milestones[r]) == null ? void 0 : c.ki_estimate) ?? 0);
771
+ var a;
772
+ return s + (((a = n.snapshot.milestones[r]) == null ? void 0 : a.ki_estimate) ?? 0);
770
773
  },
771
774
  0
772
775
  );
773
- return o + f + a;
776
+ return o + p + c;
774
777
  }
775
- function Be(e, t, n) {
776
- const o = se(t, n.snapshot, n.deps), i = [...e].sort((a, s) => {
777
- var u, p;
778
- const r = ((u = n.snapshot.milestones[a]) == null ? void 0 : u.ki_estimate) ?? 0, c = ((p = n.snapshot.milestones[s]) == null ? void 0 : p.ki_estimate) ?? 0;
779
- return r - c || a.localeCompare(s);
778
+ function Ne(e, t, n) {
779
+ const o = se(t, n.snapshot, n.deps), i = [...e].sort((c, s) => {
780
+ var u, f;
781
+ 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;
782
+ return r - a || c.localeCompare(s);
780
783
  }), l = [];
781
- let f = [];
782
- for (const a of i) {
783
- const s = [...f, a], r = X(s, t, n);
784
- r !== null && r > o && f.length > 0 ? (l.push(f), f = [a]) : f = s;
784
+ let p = [];
785
+ for (const c of i) {
786
+ const s = [...p, c], r = X(s, t, n);
787
+ r !== null && r > o && p.length > 0 ? (l.push(p), p = [c]) : p = s;
785
788
  }
786
- return f.length > 0 && l.push(f), l.filter((a) => {
787
- const s = X(a, t, n);
789
+ return p.length > 0 && l.push(p), l.filter((c) => {
790
+ const s = X(c, t, n);
788
791
  return s === null || s <= o;
789
792
  });
790
793
  }
791
- function Ce(e, t, n, o) {
792
- const i = e[0] ?? "", l = o.snapshot.milestones[i], f = (l == null ? void 0 : l.effort) ?? null, a = [];
794
+ function qe(e, t, n, o) {
795
+ const i = e[0] ?? "", l = o.snapshot.milestones[i], p = (l == null ? void 0 : l.effort) ?? null, c = [];
793
796
  for (const k of e)
794
797
  for (const $ of o.opsByMilestone.get(k) ?? [])
795
- a.push($.id);
798
+ c.push($.id);
796
799
  const s = ie(e, o), r = s.reduce(
797
800
  (k, $) => k + (o.sizeMap.get($) ?? 0),
798
801
  0
799
- ), c = oe(n, o.snapshot, o.deps), u = e.reduce(
802
+ ), a = oe(n, o.snapshot, o.deps), u = e.reduce(
800
803
  (k, $) => {
801
- var j;
802
- return k + (((j = o.snapshot.milestones[$]) == null ? void 0 : j.ki_estimate) ?? 0);
804
+ var w;
805
+ return k + (((w = o.snapshot.milestones[$]) == null ? void 0 : w.ki_estimate) ?? 0);
803
806
  },
804
807
  0
805
- ), p = c !== null ? c + C(r) + u : null, m = se(n, o.snapshot, o.deps), b = p !== null ? p <= m : !0, v = Fe((l == null ? void 0 : l.agent) ?? null), y = qe(e, o.snapshot.milestones, o.snapshot.operations);
808
+ ), f = a !== null ? a + N(r) + u : null, _ = se(n, o.snapshot, o.deps), y = f !== null ? f <= _ : !0, v = De((l == null ? void 0 : l.agent) ?? null), b = Fe(e, o.snapshot.milestones, o.snapshot.operations), A = b !== null ? "model-dispatch" : "guard-only", M = b !== null ? Ee : null;
806
809
  return {
807
810
  id: `${i}.dispatch.${t}`,
808
811
  milestones: e,
809
- operations: a,
812
+ operations: c,
810
813
  model: n,
811
- effort: f,
814
+ effort: p,
812
815
  two_stage: (l == null ? void 0 : l.two_stage) ?? !1,
813
816
  provider: null,
814
817
  agent_name: v,
818
+ execution_mode: A,
815
819
  mcp_servers: null,
816
820
  resolved_max_tokens: null,
817
821
  background: !0,
818
- prompt: y,
822
+ systemPrompt: M,
823
+ prompt: b,
819
824
  context_files: s,
820
825
  si_bytes: r,
821
- tokens_estimated: p,
822
- fits_context_window: b,
826
+ tokens_estimated: f,
827
+ fits_context_window: y,
823
828
  sentinel_role: null,
824
829
  dispatch_log_id: null,
825
830
  remote_task_id: null,
@@ -830,7 +835,7 @@ function Ce(e, t, n, o) {
830
835
  tokens_actual: null
831
836
  };
832
837
  }
833
- function Re(e, t) {
838
+ function Ke(e, t) {
834
839
  const n = ne(e);
835
840
  if (n.length === 0)
836
841
  return [];
@@ -838,53 +843,53 @@ function Re(e, t) {
838
843
  for (const u of e.operations) {
839
844
  if (u.action === "guard")
840
845
  continue;
841
- const p = o.get(u.milestone) ?? [];
842
- p.push(u), o.set(u.milestone, p);
846
+ const f = o.get(u.milestone) ?? [];
847
+ f.push(u), o.set(u.milestone, f);
843
848
  }
844
849
  const i = /* @__PURE__ */ new Map();
845
850
  for (const u of n) {
846
- const p = e.milestones[u];
847
- if (p === void 0)
851
+ const f = e.milestones[u];
852
+ if (f === void 0)
848
853
  continue;
849
- const m = Ee(o.get(u) ?? []), b = p.model ?? "_null", v = `${m}:${b}`, y = i.get(v) ?? [];
850
- y.push(u), i.set(v, y);
854
+ const _ = Be(o.get(u) ?? []), y = f.model ?? "_null", v = `${_}:${y}`, b = i.get(v) ?? [];
855
+ b.push(u), i.set(v, b);
851
856
  }
852
857
  const l = /* @__PURE__ */ new Set();
853
858
  for (const u of n) {
854
- const p = e.milestones[u];
855
- if (p !== void 0) {
856
- l.add(p.context);
857
- for (const m of p.read_only)
858
- l.add(m);
859
- for (const m of o.get(u) ?? [])
860
- m.file && l.add(m.file);
859
+ const f = e.milestones[u];
860
+ if (f !== void 0) {
861
+ l.add(f.context);
862
+ for (const _ of f.read_only)
863
+ l.add(_);
864
+ for (const _ of o.get(u) ?? [])
865
+ _.file && l.add(_.file);
861
866
  }
862
867
  }
863
- const f = Z(Array.from(l), t), a = { snapshot: e, deps: t, sizeMap: f, opsByMilestone: o }, s = [];
868
+ const p = Z(Array.from(l), t), c = { snapshot: e, deps: t, sizeMap: p, opsByMilestone: o }, s = [];
864
869
  let r = 0;
865
- const c = Array.from(i.keys()).sort();
866
- for (const u of c) {
867
- const p = i.get(u);
868
- if (p === void 0)
870
+ const a = Array.from(i.keys()).sort();
871
+ for (const u of a) {
872
+ const f = i.get(u);
873
+ if (f === void 0)
869
874
  continue;
870
- const m = u.slice(u.indexOf(":") + 1), b = m !== "_null" ? m : null, v = Be(p, b, a);
871
- for (const y of v)
872
- s.push(Ce(y, r++, b, a));
875
+ const _ = u.slice(u.indexOf(":") + 1), y = _ !== "_null" ? _ : null, v = Ne(f, y, c);
876
+ for (const b of v)
877
+ s.push(qe(b, r++, y, c));
873
878
  }
874
879
  return s;
875
880
  }
876
- function Ke(e, t) {
881
+ function We(e, t) {
877
882
  let n = 0;
878
883
  for (const o of ne(e)) {
879
884
  const i = e.milestones[o];
880
885
  if (i === void 0)
881
886
  continue;
882
- const l = De(i.model, e, t) ?? 0;
887
+ const l = Ce(i.model, e, t) ?? 0;
883
888
  n += l + (i.ki_estimate ?? 0);
884
889
  }
885
890
  return n;
886
891
  }
887
- function De(e, t, n) {
892
+ function Ce(e, t, n) {
888
893
  if (e === null)
889
894
  return null;
890
895
  const o = t.optimization.b_per_tier[e];
@@ -894,8 +899,8 @@ function De(e, t, n) {
894
899
  return i !== void 0 ? i : null;
895
900
  }
896
901
  export {
897
- Ke as computeTokensNaive,
898
- Re as optimize,
899
- Ne as snapshot,
902
+ We as computeTokensNaive,
903
+ Ke as optimize,
904
+ Re as snapshot,
900
905
  $e as topoSortMilestones
901
906
  };
package/lib/optimize.d.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  import { DagSnapshot, DispatchUnit, IOptimizerDeps } from '@adhd/dispatch-base-spec';
2
2
 
3
+ /**
4
+ * Stable, milestone-independent preamble baked into every model-dispatch
5
+ * DispatchUnit's `systemPrompt` (DEBT-DISPATCH-012). Never varies per
6
+ * milestone — that's precisely what makes it safe to bake once at
7
+ * agent_create and never re-send.
8
+ */
9
+ export declare const DISPATCH_AGENT_SYSTEM_PREAMBLE: string;
3
10
  /**
4
11
  * Compute the greedy dispatch plan for the current scheduling cycle.
5
12
  *
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@adhd/dispatch-core-optimizer",
3
- "version": "0.0.6",
3
+ "version": "0.1.1",
4
4
  "dependencies": {
5
- "@adhd/dispatch-base-spec": "^0.0.6"
5
+ "@adhd/dispatch-base-spec": "^0.1.1"
6
6
  },
7
7
  "main": "./index.js",
8
8
  "module": "./index.mjs",
@@ -1,21 +0,0 @@
1
- import { DagJson, DispatchLogEntry, IOptimizerDeps, MilestoneDag, OperationDag } from '@adhd/dispatch-base-spec';
2
-
3
- /** Recommended cold-start defaults per SCOPE.md Open Decision #2. */
4
- export declare function defaultDeps(overrides?: Partial<IOptimizerDeps>): IOptimizerDeps;
5
- export declare function miniMilestone(overrides?: Partial<MilestoneDag>): MilestoneDag;
6
- export declare function miniOp(overrides?: Partial<OperationDag>): OperationDag;
7
- export declare function miniDag(overrides?: Partial<DagJson>): DagJson;
8
- /**
9
- * Recursively Object.freeze a value and everything reachable from it (own
10
- * enumerable keys only, arrays included). Functions are left unfrozen-and-
11
- * unrecursed — freezing them adds nothing and isn't needed to prove data
12
- * non-mutation.
13
- *
14
- * Used to prove snapshot()/optimize()/computeTokensNaive() honor their "does
15
- * not mutate its input" contract: ES module code runs in strict mode, so any
16
- * in-place write to a frozen object throws a TypeError instead of silently
17
- * no-oping — a mutation bug turns into a failing test, not a passing one.
18
- */
19
- export declare function deepFreeze<T>(value: T, seen?: WeakSet<object>): T;
20
- /** A dispatch_log entry recording a passed guard for `${slug}.guard`. */
21
- export declare function passingGuardEntry(slug: string, overrides?: Partial<DispatchLogEntry>): DispatchLogEntry;