@directive-run/cli 1.6.0 → 1.6.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/dist/cli.js +1 -1
- package/dist/cli.js.map +1 -1
- package/package.json +4 -4
package/dist/cli.js
CHANGED
|
@@ -911,7 +911,7 @@ export const system = createSystem({
|
|
|
911
911
|
});
|
|
912
912
|
`}function pt(e){return e.replace(/-([a-z])/g,(t,r)=>r.toUpperCase())}function mt(e,t){let r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true}),writeFileSync(e,t,"utf-8");}function gt(e){return join(e,"src")}async function ht(e,t){let r=dt(t);(!e||!/^[a-z][a-z0-9-]*$/.test(e))&&(console.error(`Invalid module name: ${e||"(none)"}
|
|
913
913
|
Must start with a letter, use lowercase letters, numbers, and hyphens.`),process.exit(1));let s;r.minimal?s=[]:r.with.length>0?s=r.with.filter(l=>ft.includes(l)):s=ft;let n=gt(r.dir),o=join(n,`${e}.ts`);existsSync(o)&&(console.error(`File already exists: ${relative(r.dir,o)}`),process.exit(1));let i=_r(e,s);mt(o,i);let a=relative(r.dir,o);console.log(`${g.green("Created")} ${g.dim(a)}`),s.length===0?console.log(g.dim(" Minimal module (schema + init only)")):console.log(g.dim(` Sections: ${s.join(", ")}`));}async function yt(e,t){let r=dt(t);(!e||!/^[a-z][a-z0-9-]*$/.test(e))&&(console.error(`Invalid orchestrator name: ${e||"(none)"}
|
|
914
|
-
Must start with a letter, use lowercase letters, numbers, and hyphens.`),process.exit(1));let s=gt(r.dir),n=join(s,`${e}.ts`);existsSync(n)&&(console.error(`File already exists: ${relative(r.dir,n)}`),process.exit(1));let o=Ur(e);mt(n,o);let i=relative(r.dir,n);console.log(`${g.green("Created")} ${g.dim(i)}`),console.log(g.dim(" AI orchestrator with memory, guardrails, and streaming"));}Object.freeze(new Set(["__proto__","constructor","prototype"]));var F=new Set(["$eq","$ne","$in","$nin","$exists","$gt","$gte","$lt","$lte","$between","$matches","$startsWith","$endsWith","$contains","$changed"]),bt=new Set(["$all","$any","$not"]),ie=64;function I(e){return typeof e!="object"||e===null||Array.isArray(e)?false:!(e instanceof Date)&&!(e instanceof RegExp)}function ve(e){if(typeof e!="object"||e===null||Array.isArray(e))return false;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function qe(e){if(!I(e))return false;let t=0,r=false;for(let s of Object.keys(e)){if(s.startsWith("$"))r=true,F.has(s)||ae(`predicate: unknown operator "${s}" \u2014 looks like a typo. Known operators: ${[...F].join(", ")}`);else if(r||t===0)return false;t++;}return r?t>0:false}function wt(e){return e===null?false:Array.isArray(e)?e.every(t=>ve(t)&&"fact"in t&&"op"in t):ve(e)}function oe(e,t,r="",s=new WeakSet,n=0){if(n>ie){console.warn(`[Directive] predicate depth limit (${ie}) exceeded \u2014 flatten the predicate or split it into multiple constraints. If this is unexpected, check for a cyclic spec object.`),t.bail?.("depth");return}if(Array.isArray(e)){e.forEach((i,a)=>{if(!I(i))return;let l=i;if(typeof l.fact=="string"&&typeof l.op=="string"){let c=r?`${r}[${a}]`:`[${a}]`;t.operator?.(r?`${r}.${l.fact}`:l.fact,l.op,l.value,`${c}.value`);}});return}if(!I(e))return;if(s.has(e)){console.warn("[Directive] walkPredicate: cyclic predicate spec"),t.bail?.("cycle");return}s.add(e);let o=e;for(let i of ["$all","$any","$not"])if(i in o){if(t.combinator?.(i)===false)return;let a=i==="$not"?[o.$not]:o[i]??[];for(let l of a)oe(l,t,r,s,n+1);return}for(let i of Object.keys(o)){let a=r?`${r}.${i}`:i;if(i.startsWith("$")){t.strayOperatorKey?.(i,a);continue}let l=o[i];if(qe(l)){let c=l;for(let f of Object.keys(c))t.operator?.(a,f,c[f],`${a}.${f}`);continue}if(ve(l)){if(t.nested?.(i)===false)continue;oe(l,t,a,s,n+1);continue}t.literal?.(a,l);}}function $t(e,t=""){function r(n,o,i,a){if(typeof n=="bigint")throw new Error(`[Directive] validatePredicate: bigint operand at "${o}" is not JSON-serializable (JSON.stringify throws on bigint).`);if(n instanceof Set)throw new Error(`[Directive] validatePredicate: Set operand at "${o}" is not JSON-serializable (serializes to {} and loses all members).`);if(n instanceof Map)throw new Error(`[Directive] validatePredicate: Map operand at "${o}" is not JSON-serializable (serializes to {} and loses all entries).`);if(n instanceof RegExp)throw new Error(`[Directive] validatePredicate: RegExp operand at "${o}" is not JSON-serializable (a regex lost to JSON.parse becomes {}). Only a direct $matches operand may be a RegExp.`);if(!(n===null||typeof n!="object")&&!(a>ie)&&!i.has(n)){if(i.add(n),Array.isArray(n)){n.forEach((l,c)=>{r(l,`${o}[${c}]`,i,a+1);});return}for(let l of Object.keys(n))r(n[l],o?`${o}.${l}`:l,i,a+1);}}function s(n,o,i){if(typeof n=="bigint")throw new Error(`[Directive] validatePredicate: bigint operand at "${i}" is not JSON-serializable (JSON.stringify throws on bigint).`);if(n instanceof Set)throw new Error(`[Directive] validatePredicate: Set operand at "${i}" is not JSON-serializable (serializes to {} and loses all members).`);if(n instanceof Map)throw new Error(`[Directive] validatePredicate: Map operand at "${i}" is not JSON-serializable (serializes to {} and loses all entries).`);if(o==="$matches"&&!(n instanceof RegExp))throw new Error(`[Directive] validatePredicate: $matches operand at "${i}" must be a RegExp; got ${n===null?"null":typeof n}. A regex lost to JSON.parse becomes {} \u2014 reify with new RegExp(pattern, flags) before installing.`);if(Array.isArray(n))n.forEach((a,l)=>{r(a,`${i}[${l}]`,new WeakSet,1);});else if(ve(n))for(let a of Object.keys(n))r(n[a],`${i}.${a}`,new WeakSet,1);}if(e instanceof Set)throw new Error(`[Directive] validatePredicate: Set operand${t?` at "${t}"`:""} is not JSON-serializable (serializes to {} and loses all members).`);if(e instanceof Map)throw new Error(`[Directive] validatePredicate: Map operand${t?` at "${t}"`:""} is not JSON-serializable (serializes to {} and loses all entries).`);oe(e,{operator(n,o,i,a){s(i,o,t?`${t}.${a}`:a);},literal(n,o){s(o,"",t?`${t}.${n}`:n);}});}function Br(){return {ids:new WeakMap,next:{v:1},pairs:new Set}}function vt(e,t){let r=e.ids.get(t);return r===void 0&&(r=e.next.v++,e.ids.set(t,r)),r}function q(e,t,r){if(Object.is(e,t))return true;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(typeof e!="object"||typeof t!="object"||e===null||t===null)return false;let s=r??Br(),n=`${vt(s,e)}:${vt(s,t)}`;if(s.pairs.has(n))return true;if(s.pairs.add(n),Array.isArray(e)||Array.isArray(t))return !Array.isArray(e)||!Array.isArray(t)||e.length!==t.length?false:e.every((a,l)=>q(a,t[l],s));if(e instanceof Set||t instanceof Set){if(!(e instanceof Set)||!(t instanceof Set)||e.size!==t.size)return false;let a=[...t];return [...e].every(l=>a.some(c=>q(l,c,s)))}if(e instanceof Map||t instanceof Map){if(!(e instanceof Map)||!(t instanceof Map)||e.size!==t.size)return false;let a=[...t.entries()],l=new Array(a.length).fill(false);for(let[c,f]of e){let u=false;for(let p=0;p<a.length;p++){if(l[p])continue;let[k,j]=a[p];if(q(c,k,s)&&q(f,j,s)){l[p]=true,u=true;break}}if(!u)return false}return true}let o=Object.keys(e),i=Object.keys(t);return o.length!==i.length?false:o.every(a=>Object.hasOwn(t,a)&&q(e[a],t[a],s))}function be(e){if(e instanceof Date)return e.getTime();if(typeof e=="number"||typeof e=="bigint"||typeof e=="string")return e}function Me(e,t,r){let s=be(t),n=be(r);if(s===void 0||n===void 0||typeof s!=typeof n)return false;switch(e){case "$gt":return s>n;case "$gte":return s>=n;case "$lt":return s<n;case "$lte":return s<=n;default:return false}}function we(e,t,r,s){switch(e){case "$eq":return q(t,r);case "$ne":return !q(t,r);case "$in":return Array.isArray(r)&&r.some(n=>q(t,n));case "$nin":return Array.isArray(r)&&!r.some(n=>q(t,n));case "$exists":return r===(t!==void 0);case "$changed":return !q(t,s);case "$gt":case "$gte":case "$lt":case "$lte":return Me(e,t,r);case "$between":{if(!Array.isArray(r)||r.length!==2)return false;let n=be(r[0]),o=be(r[1]);return n!==void 0&&o!==void 0&&typeof n==typeof o&&n>o?(ae("$between: reversed pair \u2014 [min, max] required"),false):Me("$gte",t,r[0])&&Me("$lte",t,r[1])}case "$matches":{if(!(r instanceof RegExp))throw new Error("[Directive] $matches: operand must be a RegExp (string operands are no longer accepted; pass /pattern/flags directly).");return typeof t!="string"?false:r.test(t)}case "$startsWith":return typeof t!="string"?false:t.startsWith(String(r));case "$endsWith":return typeof t!="string"?false:t.endsWith(String(r));case "$contains":return typeof t=="string"?t.includes(String(r)):Array.isArray(t)?t.some(n=>q(n,r)):t instanceof Set?t.has(r):false;default:return false}}function ae(e){console.warn(`[Directive] ${e}`);}function Lr(e,t,r,s){if(qe(e)){let n=Object.keys(e);n.length>1&&ae(`predicate: operator object has ${n.length} operators (${n.join(", ")}) \u2014 write the array form or $all instead. The runtime ANDs them as a best-effort fallback.`);for(let o of n)if(!we(o,t,e[o],r))return false;return true}return I(e)?J(e,I(t)?t:Object.create(null),I(r)?r:void 0,s+1):q(t,e)}function J(e,t,r,s=0){if(s>ie)return ae(`predicate depth limit (${ie}) exceeded \u2014 flatten the predicate or split it into multiple constraints. If this is unexpected, check for a cyclic spec object.`),false;if(Array.isArray(e))return e.every(n=>{if(!I(n))return false;let{fact:o,op:i,value:a}=n;return we(i,t?.[o],a,r?.[o])});if(!I(e))return !!e;if("$all"in e)return e.$all.every(n=>J(n,t,r,s+1));if("$any"in e)return e.$any.some(n=>J(n,t,r,s+1));if("$not"in e)return !J(e.$not,t,r,s+1);for(let n of Object.keys(e)){if(F.has(n))return ae(`predicate: operator "${n}" mixed with fact keys \u2014 wrap operators in a per-fact object`),false;if(!Lr(e[n],t?.[n],r?.[n],s))return false}return true}function le(e,t,r,s=""){let n=[];if(Array.isArray(e)){for(let o of e){if(!I(o))continue;let{fact:i,op:a,value:l}=o,c=t?.[i];n.push({path:s+i,op:a,expected:l,actual:c,pass:we(a,c,l,r?.[i])});}return n}if(!I(e))return n;for(let o of ["$all","$any","$not"])if(o in e){let i=o==="$not"?[e.$not]:e[o],a=[];for(let f of i)a.push(...le(f,t,r,s));let l=a.filter(f=>f.pass).length,c;return o==="$all"?c=a.length===0||l===a.length:o==="$any"?c=a.length>0&&l>0:c=!a.every(f=>f.pass),n.push({path:s||o,op:o,expected:i.length,actual:l,pass:c,children:a}),n}for(let o of Object.keys(e)){if(F.has(o))continue;let i=e[o],a=t?.[o],l=s+o;if(qe(i))for(let c of Object.keys(i))n.push({path:l,op:c,expected:i[c],actual:a,pass:we(c,a,i[c],r?.[o])});else I(i)?n.push(...le(i,I(a)?a:Object.create(null),I(r?.[o])?r?.[o]:void 0,`${l}.`)):n.push({path:l,op:"$eq",expected:i,actual:a,pass:q(a,i)});}return n}var ee=1e6;function St(e,t){try{$t(e);}catch(s){let n=s instanceof Error?s.message:String(s);throw new Error(`[Directive] replayUnder: the ${t} predicate is invalid \u2014 ${n}`)}if(!wt(e)){let s=e===null||typeof e!="object"?`${typeof e} \u2014 ${JSON.stringify(e)}`:JSON.stringify(e).slice(0,80);throw new Error(`[Directive] replayUnder: the ${t} predicate is not a valid FactPredicate (got ${s})`)}let r;if(oe(e,{operator(s,n){r===void 0&&n.startsWith("$")&&!F.has(n)&&(r=n);},strayOperatorKey(s){r===void 0&&!F.has(s)&&!bt.has(s)&&(r=s);}}),r!==void 0)throw new Error(`[Directive] replayUnder: the ${t} predicate uses an unknown operator "${r}" \u2014 known operators: ${[...F].join(", ")}`)}function Ie(e){if(e&&typeof e=="object"&&!Array.isArray(e)&&Array.isArray(e.snapshots))return Kr(e);let t=Array.isArray(e)?e:e&&typeof e=="object"&&Array.isArray(e.frames)?e.frames:null;if(!t)throw new Error("[Directive] toReplayFrames: history must be a JSON array of frames, an object with a `frames` array, or a history export with a `snapshots` array");if(t.length>ee)throw new Error(`[Directive] toReplayFrames: history has ${t.length} frames, exceeds the MAX_REPLAY_FRAMES limit (${ee}) \u2014 split or down-sample the history`);return t.map((r,s)=>{if(r&&typeof r=="object"&&"facts"in r){let n=r,o={id:n.id??`#${s}`,facts:n.facts??{}};return typeof n.timestamp=="number"&&(o.timestamp=n.timestamp),o}return {id:`#${s}`,facts:r??{}}})}function Kr(e){let t=typeof e=="string"?JSON.parse(e):e;if(Array.isArray(t))return kt(t);if(!t||typeof t!="object")throw new Error("[Directive] framesFromHistory: expected a history export object with a `snapshots` array (from system.history.export())");let r=t;if(r.version!==void 0&&r.version!==1)throw new Error(`[Directive] framesFromHistory: unsupported history export version ${JSON.stringify(r.version)} \u2014 expected 1`);if(!Array.isArray(r.snapshots))throw new Error("[Directive] framesFromHistory: expected a history export object with a `snapshots` array (from system.history.export())");return kt(r.snapshots)}function kt(e){if(!Array.isArray(e))throw new Error("[Directive] framesFromSnapshots: expected an array of fact-state snapshots");if(e.length>ee)throw new Error(`[Directive] framesFromSnapshots: history has ${e.length} snapshots, exceeds the MAX_REPLAY_FRAMES limit (${ee}) \u2014 split or down-sample the history`);for(let t=0;t<e.length;t++){let r=e[t];if(!r||typeof r!="object"||!("facts"in r))throw new Error(`[Directive] framesFromSnapshots: snapshot at index ${t} is not a { facts, ... } object`)}return Ie(e)}function Rt(e){let{frames:t,original:r,proposed:s,entityKey:n}=e,o=e.maxSamples??20,i=o>0?o:0;if(t.length>ee)throw new Error(`[Directive] replayUnder: history has ${t.length} frames, exceeds the MAX_REPLAY_FRAMES limit (${ee}) \u2014 split or down-sample the history`);St(r,"original"),St(s,"proposed");let a=0,l=0,c=0,f=0,u=0,p=[],k=[],j=n?new Set:void 0,x=n?new Set:void 0,G;for(let d of t){let $=d.facts,z=J(r,$,G),Se=J(s,$,G);z&&(a++,j?.add($[n])),Se&&(l++,x?.add($[n])),z===Se?u++:!z&&Se?(c++,p.length<i&&p.push(xt(d,r,s,G))):(f++,k.length<i&&k.push(xt(d,r,s,G))),G=$;}let te={framesEvaluated:t.length,original:{matched:a},proposed:{matched:l},delta:l-a,newMatchCount:c,lostMatchCount:f,unchanged:u,newMatches:p,lostMatches:k};return j&&x&&(te.original.matchedEntities=j.size,te.proposed.matchedEntities=x.size),te}function xt(e,t,r,s){let n=e.facts,o={frameId:e.id,facts:n,originalExplain:le(t,n,s),proposedExplain:le(r,n,s)};return e.timestamp!==void 0&&(o.timestamp=e.timestamp),o}function Vr(e){let t={maxSamples:20,json:false};for(let r=0;r<e.length;r++)switch(e[r]){case "--history":{let n=e[++r];n&&(t.historyPath=n);break}case "--proposed":case "-p":{let n=e[++r];n&&(t.proposedPath=n);break}case "--original":case "-o":{let n=e[++r];n&&(t.originalPath=n);break}case "--max-samples":{let n=e[++r],o=n?Number.parseInt(n,10):Number.NaN;Number.isFinite(o)&&o>=0&&(t.maxSamples=o);break}case "--entity-key":{let n=e[++r];n&&(t.entityKey=n);break}case "--json":t.json=true;break}return t}function $e(){console.error(`
|
|
914
|
+
Must start with a letter, use lowercase letters, numbers, and hyphens.`),process.exit(1));let s=gt(r.dir),n=join(s,`${e}.ts`);existsSync(n)&&(console.error(`File already exists: ${relative(r.dir,n)}`),process.exit(1));let o=Ur(e);mt(n,o);let i=relative(r.dir,n);console.log(`${g.green("Created")} ${g.dim(i)}`),console.log(g.dim(" AI orchestrator with memory, guardrails, and streaming"));}var ye=typeof process<"u"&&process.env?.NODE_ENV!=="production";Object.freeze(new Set(["__proto__","constructor","prototype"]));var F=new Set(["$eq","$ne","$in","$nin","$exists","$gt","$gte","$lt","$lte","$between","$matches","$startsWith","$endsWith","$contains","$changed"]),bt=new Set(["$all","$any","$not"]),ie=64;function I(e){return typeof e!="object"||e===null||Array.isArray(e)?false:!(e instanceof Date)&&!(e instanceof RegExp)}function ve(e){if(typeof e!="object"||e===null||Array.isArray(e))return false;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function qe(e){if(!I(e))return false;let t=0,r=false;for(let s of Object.keys(e)){if(s.startsWith("$"))r=true,F.has(s)||ae(`predicate: unknown operator "${s}" \u2014 looks like a typo. Known operators: ${[...F].join(", ")}`);else if(r||t===0)return false;t++;}return r?t>0:false}function wt(e){return e===null?false:Array.isArray(e)?e.every(t=>ve(t)&&"fact"in t&&"op"in t):ve(e)}function oe(e,t,r="",s=new WeakSet,n=0){if(n>ie){ye&&console.warn(`[Directive] predicate depth limit (${ie}) exceeded \u2014 flatten the predicate or split it into multiple constraints. If this is unexpected, check for a cyclic spec object.`),t.bail?.("depth");return}if(Array.isArray(e)){e.forEach((i,a)=>{if(!I(i))return;let l=i;if(typeof l.fact=="string"&&typeof l.op=="string"){let c=r?`${r}[${a}]`:`[${a}]`;t.operator?.(r?`${r}.${l.fact}`:l.fact,l.op,l.value,`${c}.value`);}});return}if(!I(e))return;if(s.has(e)){ye&&console.warn("[Directive] walkPredicate: cyclic predicate spec"),t.bail?.("cycle");return}s.add(e);let o=e;for(let i of ["$all","$any","$not"])if(i in o){if(t.combinator?.(i)===false)return;let a=i==="$not"?[o.$not]:o[i]??[];for(let l of a)oe(l,t,r,s,n+1);return}for(let i of Object.keys(o)){let a=r?`${r}.${i}`:i;if(i.startsWith("$")){t.strayOperatorKey?.(i,a);continue}let l=o[i];if(qe(l)){let c=l;for(let f of Object.keys(c))t.operator?.(a,f,c[f],`${a}.${f}`);continue}if(ve(l)){if(t.nested?.(i)===false)continue;oe(l,t,a,s,n+1);continue}t.literal?.(a,l);}}function $t(e,t=""){function r(n,o,i,a){if(typeof n=="bigint")throw new Error(`[Directive] validatePredicate: bigint operand at "${o}" is not JSON-serializable (JSON.stringify throws on bigint).`);if(n instanceof Set)throw new Error(`[Directive] validatePredicate: Set operand at "${o}" is not JSON-serializable (serializes to {} and loses all members).`);if(n instanceof Map)throw new Error(`[Directive] validatePredicate: Map operand at "${o}" is not JSON-serializable (serializes to {} and loses all entries).`);if(n instanceof RegExp)throw new Error(`[Directive] validatePredicate: RegExp operand at "${o}" is not JSON-serializable (a regex lost to JSON.parse becomes {}). Only a direct $matches operand may be a RegExp.`);if(!(n===null||typeof n!="object")&&!(a>ie)&&!i.has(n)){if(i.add(n),Array.isArray(n)){n.forEach((l,c)=>{r(l,`${o}[${c}]`,i,a+1);});return}for(let l of Object.keys(n))r(n[l],o?`${o}.${l}`:l,i,a+1);}}function s(n,o,i){if(typeof n=="bigint")throw new Error(`[Directive] validatePredicate: bigint operand at "${i}" is not JSON-serializable (JSON.stringify throws on bigint).`);if(n instanceof Set)throw new Error(`[Directive] validatePredicate: Set operand at "${i}" is not JSON-serializable (serializes to {} and loses all members).`);if(n instanceof Map)throw new Error(`[Directive] validatePredicate: Map operand at "${i}" is not JSON-serializable (serializes to {} and loses all entries).`);if(o==="$matches"&&!(n instanceof RegExp))throw new Error(`[Directive] validatePredicate: $matches operand at "${i}" must be a RegExp; got ${n===null?"null":typeof n}. A regex lost to JSON.parse becomes {} \u2014 reify with new RegExp(pattern, flags) before installing.`);if(Array.isArray(n))n.forEach((a,l)=>{r(a,`${i}[${l}]`,new WeakSet,1);});else if(ve(n))for(let a of Object.keys(n))r(n[a],`${i}.${a}`,new WeakSet,1);}if(e instanceof Set)throw new Error(`[Directive] validatePredicate: Set operand${t?` at "${t}"`:""} is not JSON-serializable (serializes to {} and loses all members).`);if(e instanceof Map)throw new Error(`[Directive] validatePredicate: Map operand${t?` at "${t}"`:""} is not JSON-serializable (serializes to {} and loses all entries).`);oe(e,{operator(n,o,i,a){s(i,o,t?`${t}.${a}`:a);},literal(n,o){s(o,"",t?`${t}.${n}`:n);}});}function Br(){return {ids:new WeakMap,next:{v:1},pairs:new Set}}function vt(e,t){let r=e.ids.get(t);return r===void 0&&(r=e.next.v++,e.ids.set(t,r)),r}function q(e,t,r){if(Object.is(e,t))return true;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(typeof e!="object"||typeof t!="object"||e===null||t===null)return false;let s=r??Br(),n=`${vt(s,e)}:${vt(s,t)}`;if(s.pairs.has(n))return true;if(s.pairs.add(n),Array.isArray(e)||Array.isArray(t))return !Array.isArray(e)||!Array.isArray(t)||e.length!==t.length?false:e.every((a,l)=>q(a,t[l],s));if(e instanceof Set||t instanceof Set){if(!(e instanceof Set)||!(t instanceof Set)||e.size!==t.size)return false;let a=[...t];return [...e].every(l=>a.some(c=>q(l,c,s)))}if(e instanceof Map||t instanceof Map){if(!(e instanceof Map)||!(t instanceof Map)||e.size!==t.size)return false;let a=[...t.entries()],l=new Array(a.length).fill(false);for(let[c,f]of e){let u=false;for(let p=0;p<a.length;p++){if(l[p])continue;let[k,j]=a[p];if(q(c,k,s)&&q(f,j,s)){l[p]=true,u=true;break}}if(!u)return false}return true}let o=Object.keys(e),i=Object.keys(t);return o.length!==i.length?false:o.every(a=>Object.hasOwn(t,a)&&q(e[a],t[a],s))}function be(e){if(e instanceof Date)return e.getTime();if(typeof e=="number"||typeof e=="bigint"||typeof e=="string")return e}function Me(e,t,r){let s=be(t),n=be(r);if(s===void 0||n===void 0||typeof s!=typeof n)return false;switch(e){case "$gt":return s>n;case "$gte":return s>=n;case "$lt":return s<n;case "$lte":return s<=n;default:return false}}function we(e,t,r,s){switch(e){case "$eq":return q(t,r);case "$ne":return !q(t,r);case "$in":return Array.isArray(r)&&r.some(n=>q(t,n));case "$nin":return Array.isArray(r)&&!r.some(n=>q(t,n));case "$exists":return r===(t!==void 0);case "$changed":return !q(t,s);case "$gt":case "$gte":case "$lt":case "$lte":return Me(e,t,r);case "$between":{if(!Array.isArray(r)||r.length!==2)return false;let n=be(r[0]),o=be(r[1]);return n!==void 0&&o!==void 0&&typeof n==typeof o&&n>o?(ae("$between: reversed pair \u2014 [min, max] required"),false):Me("$gte",t,r[0])&&Me("$lte",t,r[1])}case "$matches":{if(!(r instanceof RegExp))throw new Error("[Directive] $matches: operand must be a RegExp (string operands are no longer accepted; pass /pattern/flags directly).");return typeof t!="string"?false:r.test(t)}case "$startsWith":return typeof t!="string"?false:t.startsWith(String(r));case "$endsWith":return typeof t!="string"?false:t.endsWith(String(r));case "$contains":return typeof t=="string"?t.includes(String(r)):Array.isArray(t)?t.some(n=>q(n,r)):t instanceof Set?t.has(r):false;default:return false}}function ae(e){ye&&console.warn(`[Directive] ${e}`);}function Lr(e,t,r,s){if(qe(e)){let n=Object.keys(e);n.length>1&&ae(`predicate: operator object has ${n.length} operators (${n.join(", ")}) \u2014 write the array form or $all instead. The runtime ANDs them as a best-effort fallback.`);for(let o of n)if(!we(o,t,e[o],r))return false;return true}return I(e)?J(e,I(t)?t:Object.create(null),I(r)?r:void 0,s+1):q(t,e)}function J(e,t,r,s=0){if(s>ie)return ae(`predicate depth limit (${ie}) exceeded \u2014 flatten the predicate or split it into multiple constraints. If this is unexpected, check for a cyclic spec object.`),false;if(Array.isArray(e))return e.every(n=>{if(!I(n))return false;let{fact:o,op:i,value:a}=n;return we(i,t?.[o],a,r?.[o])});if(!I(e))return !!e;if("$all"in e)return e.$all.every(n=>J(n,t,r,s+1));if("$any"in e)return e.$any.some(n=>J(n,t,r,s+1));if("$not"in e)return !J(e.$not,t,r,s+1);for(let n of Object.keys(e)){if(F.has(n))return ae(`predicate: operator "${n}" mixed with fact keys \u2014 wrap operators in a per-fact object`),false;if(!Lr(e[n],t?.[n],r?.[n],s))return false}return true}function le(e,t,r,s=""){let n=[];if(Array.isArray(e)){for(let o of e){if(!I(o))continue;let{fact:i,op:a,value:l}=o,c=t?.[i];n.push({path:s+i,op:a,expected:l,actual:c,pass:we(a,c,l,r?.[i])});}return n}if(!I(e))return n;for(let o of ["$all","$any","$not"])if(o in e){let i=o==="$not"?[e.$not]:e[o],a=[];for(let f of i)a.push(...le(f,t,r,s));let l=a.filter(f=>f.pass).length,c;return o==="$all"?c=a.length===0||l===a.length:o==="$any"?c=a.length>0&&l>0:c=!a.every(f=>f.pass),n.push({path:s||o,op:o,expected:i.length,actual:l,pass:c,children:a}),n}for(let o of Object.keys(e)){if(F.has(o))continue;let i=e[o],a=t?.[o],l=s+o;if(qe(i))for(let c of Object.keys(i))n.push({path:l,op:c,expected:i[c],actual:a,pass:we(c,a,i[c],r?.[o])});else I(i)?n.push(...le(i,I(a)?a:Object.create(null),I(r?.[o])?r?.[o]:void 0,`${l}.`)):n.push({path:l,op:"$eq",expected:i,actual:a,pass:q(a,i)});}return n}var ee=1e6;function St(e,t){try{$t(e);}catch(s){let n=s instanceof Error?s.message:String(s);throw new Error(`[Directive] replayUnder: the ${t} predicate is invalid \u2014 ${n}`)}if(!wt(e)){let s=e===null||typeof e!="object"?`${typeof e} \u2014 ${JSON.stringify(e)}`:JSON.stringify(e).slice(0,80);throw new Error(`[Directive] replayUnder: the ${t} predicate is not a valid FactPredicate (got ${s})`)}let r;if(oe(e,{operator(s,n){r===void 0&&n.startsWith("$")&&!F.has(n)&&(r=n);},strayOperatorKey(s){r===void 0&&!F.has(s)&&!bt.has(s)&&(r=s);}}),r!==void 0)throw new Error(`[Directive] replayUnder: the ${t} predicate uses an unknown operator "${r}" \u2014 known operators: ${[...F].join(", ")}`)}function Ie(e){if(e&&typeof e=="object"&&!Array.isArray(e)&&Array.isArray(e.snapshots))return Kr(e);let t=Array.isArray(e)?e:e&&typeof e=="object"&&Array.isArray(e.frames)?e.frames:null;if(!t)throw new Error("[Directive] toReplayFrames: history must be a JSON array of frames, an object with a `frames` array, or a history export with a `snapshots` array");if(t.length>ee)throw new Error(`[Directive] toReplayFrames: history has ${t.length} frames, exceeds the MAX_REPLAY_FRAMES limit (${ee}) \u2014 split or down-sample the history`);return t.map((r,s)=>{if(r&&typeof r=="object"&&"facts"in r){let n=r,o={id:n.id??`#${s}`,facts:n.facts??{}};return typeof n.timestamp=="number"&&(o.timestamp=n.timestamp),o}return {id:`#${s}`,facts:r??{}}})}function Kr(e){let t=typeof e=="string"?JSON.parse(e):e;if(Array.isArray(t))return kt(t);if(!t||typeof t!="object")throw new Error("[Directive] framesFromHistory: expected a history export object with a `snapshots` array (from system.history.export())");let r=t;if(r.version!==void 0&&r.version!==1)throw new Error(`[Directive] framesFromHistory: unsupported history export version ${JSON.stringify(r.version)} \u2014 expected 1`);if(!Array.isArray(r.snapshots))throw new Error("[Directive] framesFromHistory: expected a history export object with a `snapshots` array (from system.history.export())");return kt(r.snapshots)}function kt(e){if(!Array.isArray(e))throw new Error("[Directive] framesFromSnapshots: expected an array of fact-state snapshots");if(e.length>ee)throw new Error(`[Directive] framesFromSnapshots: history has ${e.length} snapshots, exceeds the MAX_REPLAY_FRAMES limit (${ee}) \u2014 split or down-sample the history`);for(let t=0;t<e.length;t++){let r=e[t];if(!r||typeof r!="object"||!("facts"in r))throw new Error(`[Directive] framesFromSnapshots: snapshot at index ${t} is not a { facts, ... } object`)}return Ie(e)}function Rt(e){let{frames:t,original:r,proposed:s,entityKey:n}=e,o=e.maxSamples??20,i=o>0?o:0;if(t.length>ee)throw new Error(`[Directive] replayUnder: history has ${t.length} frames, exceeds the MAX_REPLAY_FRAMES limit (${ee}) \u2014 split or down-sample the history`);St(r,"original"),St(s,"proposed");let a=0,l=0,c=0,f=0,u=0,p=[],k=[],j=n?new Set:void 0,x=n?new Set:void 0,G;for(let d of t){let $=d.facts,z=J(r,$,G),Se=J(s,$,G);z&&(a++,j?.add($[n])),Se&&(l++,x?.add($[n])),z===Se?u++:!z&&Se?(c++,p.length<i&&p.push(xt(d,r,s,G))):(f++,k.length<i&&k.push(xt(d,r,s,G))),G=$;}let te={framesEvaluated:t.length,original:{matched:a},proposed:{matched:l},delta:l-a,newMatchCount:c,lostMatchCount:f,unchanged:u,newMatches:p,lostMatches:k};return j&&x&&(te.original.matchedEntities=j.size,te.proposed.matchedEntities=x.size),te}function xt(e,t,r,s){let n=e.facts,o={frameId:e.id,facts:n,originalExplain:le(t,n,s),proposedExplain:le(r,n,s)};return e.timestamp!==void 0&&(o.timestamp=e.timestamp),o}function Vr(e){let t={maxSamples:20,json:false};for(let r=0;r<e.length;r++)switch(e[r]){case "--history":{let n=e[++r];n&&(t.historyPath=n);break}case "--proposed":case "-p":{let n=e[++r];n&&(t.proposedPath=n);break}case "--original":case "-o":{let n=e[++r];n&&(t.originalPath=n);break}case "--max-samples":{let n=e[++r],o=n?Number.parseInt(n,10):Number.NaN;Number.isFinite(o)&&o>=0&&(t.maxSamples=o);break}case "--entity-key":{let n=e[++r];n&&(t.entityKey=n);break}case "--json":t.json=true;break}return t}function $e(){console.error(`
|
|
915
915
|
Usage: directive replay-under --history <frames.json> --proposed <spec.json>
|
|
916
916
|
|
|
917
917
|
Replay a recorded fact-frame history through a proposed constraint
|