@directive-run/core 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -526,6 +526,128 @@ declare const MAX_SWEEP_POINTS = 10000;
526
526
  */
527
527
  declare function sweepUnder<F = Record<string, unknown>>(options: SweepUnderOptions<F>): SweepReport;
528
528
 
529
+ /**
530
+ * Structural diff between two snapshots of a system's constraint `whenSpec`
531
+ * map. The "git diff for business rules" — operates on the predicate AST
532
+ * instead of source-text lines.
533
+ *
534
+ * A predicate is a tree of leaf clauses (`{ fact: operator(value) }`) and
535
+ * combinators (`$all` / `$any` / `$not`). `diffRules` flattens both trees
536
+ * into path-keyed leaf lists and compares: missing path → added/removed,
537
+ * same path with different operand → changed (with relaxed/tightened
538
+ * classification for numeric thresholds).
539
+ *
540
+ * Pure module — imports only utils. No engine / store / predicate-runtime
541
+ * dependency: predicates are walked as plain JSON.
542
+ */
543
+ /** A leaf clause extracted from a predicate tree, keyed by its dotted path. */
544
+ interface LeafClause {
545
+ /** Dotted path through the predicate. E.g. `phase`, `$all[0].elapsed`, `$not.paused`. */
546
+ path: string;
547
+ /** Operator name. `$eq` is implied for bare-value equality. */
548
+ op: string;
549
+ /** Operand. */
550
+ value: unknown;
551
+ }
552
+ /** Kind of change observed for a single clause. */
553
+ type ChangeKind = "added" | "removed" | "changed" | "relaxed" | "tightened";
554
+ /** A single change between two predicates at a specific path. */
555
+ interface Change {
556
+ path: string;
557
+ kind: ChangeKind;
558
+ before?: {
559
+ op: string;
560
+ value: unknown;
561
+ };
562
+ after?: {
563
+ op: string;
564
+ value: unknown;
565
+ };
566
+ }
567
+ /** Status of a single constraint across the two snapshots. */
568
+ type ConstraintStatus = "added" | "removed" | "changed" | "unchanged";
569
+ interface ConstraintDiff {
570
+ id: string;
571
+ status: ConstraintStatus;
572
+ /** Empty for `unchanged`. For `added` / `removed`, every leaf is listed once. */
573
+ changes: Change[];
574
+ }
575
+ interface RulesDiffReport {
576
+ constraints: ConstraintDiff[];
577
+ summary: {
578
+ added: number;
579
+ removed: number;
580
+ changed: number;
581
+ unchanged: number;
582
+ totalClauseChanges: number;
583
+ };
584
+ }
585
+ interface DiffRulesOptions {
586
+ before: RulesMapInput;
587
+ after: RulesMapInput;
588
+ }
589
+ /**
590
+ * Accepted shapes for the `before` / `after` predicate maps:
591
+ * - A plain map `{ [constraintId]: whenSpec }`
592
+ * - The `system.inspect().constraints` array form `Array<{ id, whenSpec? }>`
593
+ * - Either wrapped as `{ constraints: <one-of-the-above> }`
594
+ *
595
+ * Anything else throws at `toRulesMap` so a bad input fails loud.
596
+ */
597
+ type RulesMapInput = unknown;
598
+ /**
599
+ * Coerce supported input shapes into a flat `Record<constraintId, whenSpec>`.
600
+ * Constraints without a `whenSpec` (function-form `when`) are dropped with
601
+ * an undefined entry so callers can distinguish them from absent constraints
602
+ * if they care; the diff treats `undefined` as "no data" and skips clause
603
+ * walking.
604
+ */
605
+ declare function toRulesMap(raw: RulesMapInput): Record<string, unknown>;
606
+ /**
607
+ * Walk a predicate tree and emit every leaf clause with its dotted path.
608
+ * Combinators (`$all` / `$any` / `$not`) become indexed path segments.
609
+ * Bare-value equality (`{ phase: "red" }`) emits as `op: "$eq"`.
610
+ *
611
+ * Array-form predicates (`[{ fact, op, value }, ...]`) are also accepted —
612
+ * each clause's `fact` becomes the path, the operator is `op`, and the
613
+ * value is `value`.
614
+ *
615
+ * **What gets dropped:** non-object combinator children (e.g. `$not: "red"`
616
+ * with a string operand instead of a predicate object), function-form
617
+ * predicates (the engine accepts a function for `when`; this walker only
618
+ * sees data), and bare primitive predicates (a top-level `true` or `"x"`).
619
+ * The walker is a tree visitor — anything not shaped like a predicate node
620
+ * is silently skipped, never thrown on.
621
+ */
622
+ declare function flattenPredicate(spec: unknown, pathPrefix?: string, out?: LeafClause[]): LeafClause[];
623
+ /**
624
+ * Diff two snapshots of a system's constraint whenSpec map.
625
+ *
626
+ * @example
627
+ * ```ts
628
+ * const report = diffRules({
629
+ * before: { blockCheckout: { cartTotal: { $gte: 100 } } },
630
+ * after: { blockCheckout: { cartTotal: { $gte: 50 } } },
631
+ * });
632
+ *
633
+ * report.constraints[0].status; // "changed"
634
+ * report.constraints[0].changes[0].kind; // "relaxed"
635
+ * report.summary.totalClauseChanges; // 1
636
+ * ```
637
+ *
638
+ * The input shape is forgiving — either a flat `{ id: whenSpec }` map, the
639
+ * `system.inspect().constraints` array form, or either wrapped as
640
+ * `{ constraints: ... }`. Constraints whose `when` is a function (no
641
+ * `whenSpec`) are tracked as added/removed but cannot be clause-diffed
642
+ * (the function form is opaque).
643
+ */
644
+ declare function diffRules(options: DiffRulesOptions): RulesDiffReport;
645
+ /**
646
+ * Diff two predicate trees and return the list of leaf-level changes.
647
+ * Exported for reuse beyond the full per-constraint report.
648
+ */
649
+ declare function diffClauses(before: unknown, after: unknown): Change[];
650
+
529
651
  /** Brand symbol for branded types */
530
652
  declare const Brand: unique symbol;
531
653
  /** Branded type - adds a unique brand to a base type */
@@ -1786,4 +1908,4 @@ declare const Backoff: {
1786
1908
  readonly Exponential: "exponential";
1787
1909
  };
1788
1910
 
1789
- export { Backoff, type Branded, type ChainableSchemaType, ClauseResult, CreateSystemOptionsNamed, CreateSystemOptionsSingle, CrossModuleDeps, DefinitionMeta, ErrorBoundaryConfig, type ExtendedSchemaType, FactPredicate, FactTemplate, Facts, MAX_REPLAY_FRAMES, MAX_SWEEP_POINTS, type ModuleConfig, type ModuleConfigWithDeps, ModuleDef, ModuleHooks, ModuleSchema, ModulesMap, NamespacedSystem, PatchSpec, Plugin, type PredicateBacktestReport, type ReplayDiffSample, type ReplayFrame, type ReplayUnderOptions, Requirement, RequirementSet, type RequirementTypeStatus, RequirementWithId, SchemaType, type SignalClock, SingleModuleSystem, type SweepHole, type SweepPoint, type SweepReport, type SweepUnderOptions, type TimerFactOpts, type TimerFactState, TraceOption, applyPatch, completeTimer, createModule, createModuleFactory, createRequirementStatusPlugin, createStatusHook, createSystem, createSystemWithStatus, defaultClock, elapsedMs, evaluateKeySelector, evaluatePredicate, evaluatePredicateExplained, evaluateTemplate, extractDeps, extractTemplateKeys, forType, framesFromHistory, framesFromSnapshots, generateRequirementId, initialTimerState, isPredicate, isRequirementType, isTemplate, memoizePredicate, pauseTimer, realClock, registerRepeat, remainingMs, replayUnder, req, resetTimer, resumeTimer, startTimer, sweepUnder, t, tickTimer, timerOps, toReplayFrames, validatePredicate, virtualClock };
1911
+ export { Backoff, type Branded, type ChainableSchemaType, type Change, type ChangeKind, ClauseResult, type ConstraintDiff, type ConstraintStatus, CreateSystemOptionsNamed, CreateSystemOptionsSingle, CrossModuleDeps, DefinitionMeta, type DiffRulesOptions, ErrorBoundaryConfig, type ExtendedSchemaType, FactPredicate, FactTemplate, Facts, type LeafClause, MAX_REPLAY_FRAMES, MAX_SWEEP_POINTS, type ModuleConfig, type ModuleConfigWithDeps, ModuleDef, ModuleHooks, ModuleSchema, ModulesMap, NamespacedSystem, PatchSpec, Plugin, type PredicateBacktestReport, type ReplayDiffSample, type ReplayFrame, type ReplayUnderOptions, Requirement, RequirementSet, type RequirementTypeStatus, RequirementWithId, type RulesDiffReport, type RulesMapInput, SchemaType, type SignalClock, SingleModuleSystem, type SweepHole, type SweepPoint, type SweepReport, type SweepUnderOptions, type TimerFactOpts, type TimerFactState, TraceOption, applyPatch, completeTimer, createModule, createModuleFactory, createRequirementStatusPlugin, createStatusHook, createSystem, createSystemWithStatus, defaultClock, diffClauses, diffRules, elapsedMs, evaluateKeySelector, evaluatePredicate, evaluatePredicateExplained, evaluateTemplate, extractDeps, extractTemplateKeys, flattenPredicate, forType, framesFromHistory, framesFromSnapshots, generateRequirementId, initialTimerState, isPredicate, isRequirementType, isTemplate, memoizePredicate, pauseTimer, realClock, registerRepeat, remainingMs, replayUnder, req, resetTimer, resumeTimer, startTimer, sweepUnder, t, tickTimer, timerOps, toReplayFrames, toRulesMap, validatePredicate, virtualClock };
package/dist/index.d.ts CHANGED
@@ -526,6 +526,128 @@ declare const MAX_SWEEP_POINTS = 10000;
526
526
  */
527
527
  declare function sweepUnder<F = Record<string, unknown>>(options: SweepUnderOptions<F>): SweepReport;
528
528
 
529
+ /**
530
+ * Structural diff between two snapshots of a system's constraint `whenSpec`
531
+ * map. The "git diff for business rules" — operates on the predicate AST
532
+ * instead of source-text lines.
533
+ *
534
+ * A predicate is a tree of leaf clauses (`{ fact: operator(value) }`) and
535
+ * combinators (`$all` / `$any` / `$not`). `diffRules` flattens both trees
536
+ * into path-keyed leaf lists and compares: missing path → added/removed,
537
+ * same path with different operand → changed (with relaxed/tightened
538
+ * classification for numeric thresholds).
539
+ *
540
+ * Pure module — imports only utils. No engine / store / predicate-runtime
541
+ * dependency: predicates are walked as plain JSON.
542
+ */
543
+ /** A leaf clause extracted from a predicate tree, keyed by its dotted path. */
544
+ interface LeafClause {
545
+ /** Dotted path through the predicate. E.g. `phase`, `$all[0].elapsed`, `$not.paused`. */
546
+ path: string;
547
+ /** Operator name. `$eq` is implied for bare-value equality. */
548
+ op: string;
549
+ /** Operand. */
550
+ value: unknown;
551
+ }
552
+ /** Kind of change observed for a single clause. */
553
+ type ChangeKind = "added" | "removed" | "changed" | "relaxed" | "tightened";
554
+ /** A single change between two predicates at a specific path. */
555
+ interface Change {
556
+ path: string;
557
+ kind: ChangeKind;
558
+ before?: {
559
+ op: string;
560
+ value: unknown;
561
+ };
562
+ after?: {
563
+ op: string;
564
+ value: unknown;
565
+ };
566
+ }
567
+ /** Status of a single constraint across the two snapshots. */
568
+ type ConstraintStatus = "added" | "removed" | "changed" | "unchanged";
569
+ interface ConstraintDiff {
570
+ id: string;
571
+ status: ConstraintStatus;
572
+ /** Empty for `unchanged`. For `added` / `removed`, every leaf is listed once. */
573
+ changes: Change[];
574
+ }
575
+ interface RulesDiffReport {
576
+ constraints: ConstraintDiff[];
577
+ summary: {
578
+ added: number;
579
+ removed: number;
580
+ changed: number;
581
+ unchanged: number;
582
+ totalClauseChanges: number;
583
+ };
584
+ }
585
+ interface DiffRulesOptions {
586
+ before: RulesMapInput;
587
+ after: RulesMapInput;
588
+ }
589
+ /**
590
+ * Accepted shapes for the `before` / `after` predicate maps:
591
+ * - A plain map `{ [constraintId]: whenSpec }`
592
+ * - The `system.inspect().constraints` array form `Array<{ id, whenSpec? }>`
593
+ * - Either wrapped as `{ constraints: <one-of-the-above> }`
594
+ *
595
+ * Anything else throws at `toRulesMap` so a bad input fails loud.
596
+ */
597
+ type RulesMapInput = unknown;
598
+ /**
599
+ * Coerce supported input shapes into a flat `Record<constraintId, whenSpec>`.
600
+ * Constraints without a `whenSpec` (function-form `when`) are dropped with
601
+ * an undefined entry so callers can distinguish them from absent constraints
602
+ * if they care; the diff treats `undefined` as "no data" and skips clause
603
+ * walking.
604
+ */
605
+ declare function toRulesMap(raw: RulesMapInput): Record<string, unknown>;
606
+ /**
607
+ * Walk a predicate tree and emit every leaf clause with its dotted path.
608
+ * Combinators (`$all` / `$any` / `$not`) become indexed path segments.
609
+ * Bare-value equality (`{ phase: "red" }`) emits as `op: "$eq"`.
610
+ *
611
+ * Array-form predicates (`[{ fact, op, value }, ...]`) are also accepted —
612
+ * each clause's `fact` becomes the path, the operator is `op`, and the
613
+ * value is `value`.
614
+ *
615
+ * **What gets dropped:** non-object combinator children (e.g. `$not: "red"`
616
+ * with a string operand instead of a predicate object), function-form
617
+ * predicates (the engine accepts a function for `when`; this walker only
618
+ * sees data), and bare primitive predicates (a top-level `true` or `"x"`).
619
+ * The walker is a tree visitor — anything not shaped like a predicate node
620
+ * is silently skipped, never thrown on.
621
+ */
622
+ declare function flattenPredicate(spec: unknown, pathPrefix?: string, out?: LeafClause[]): LeafClause[];
623
+ /**
624
+ * Diff two snapshots of a system's constraint whenSpec map.
625
+ *
626
+ * @example
627
+ * ```ts
628
+ * const report = diffRules({
629
+ * before: { blockCheckout: { cartTotal: { $gte: 100 } } },
630
+ * after: { blockCheckout: { cartTotal: { $gte: 50 } } },
631
+ * });
632
+ *
633
+ * report.constraints[0].status; // "changed"
634
+ * report.constraints[0].changes[0].kind; // "relaxed"
635
+ * report.summary.totalClauseChanges; // 1
636
+ * ```
637
+ *
638
+ * The input shape is forgiving — either a flat `{ id: whenSpec }` map, the
639
+ * `system.inspect().constraints` array form, or either wrapped as
640
+ * `{ constraints: ... }`. Constraints whose `when` is a function (no
641
+ * `whenSpec`) are tracked as added/removed but cannot be clause-diffed
642
+ * (the function form is opaque).
643
+ */
644
+ declare function diffRules(options: DiffRulesOptions): RulesDiffReport;
645
+ /**
646
+ * Diff two predicate trees and return the list of leaf-level changes.
647
+ * Exported for reuse beyond the full per-constraint report.
648
+ */
649
+ declare function diffClauses(before: unknown, after: unknown): Change[];
650
+
529
651
  /** Brand symbol for branded types */
530
652
  declare const Brand: unique symbol;
531
653
  /** Branded type - adds a unique brand to a base type */
@@ -1786,4 +1908,4 @@ declare const Backoff: {
1786
1908
  readonly Exponential: "exponential";
1787
1909
  };
1788
1910
 
1789
- export { Backoff, type Branded, type ChainableSchemaType, ClauseResult, CreateSystemOptionsNamed, CreateSystemOptionsSingle, CrossModuleDeps, DefinitionMeta, ErrorBoundaryConfig, type ExtendedSchemaType, FactPredicate, FactTemplate, Facts, MAX_REPLAY_FRAMES, MAX_SWEEP_POINTS, type ModuleConfig, type ModuleConfigWithDeps, ModuleDef, ModuleHooks, ModuleSchema, ModulesMap, NamespacedSystem, PatchSpec, Plugin, type PredicateBacktestReport, type ReplayDiffSample, type ReplayFrame, type ReplayUnderOptions, Requirement, RequirementSet, type RequirementTypeStatus, RequirementWithId, SchemaType, type SignalClock, SingleModuleSystem, type SweepHole, type SweepPoint, type SweepReport, type SweepUnderOptions, type TimerFactOpts, type TimerFactState, TraceOption, applyPatch, completeTimer, createModule, createModuleFactory, createRequirementStatusPlugin, createStatusHook, createSystem, createSystemWithStatus, defaultClock, elapsedMs, evaluateKeySelector, evaluatePredicate, evaluatePredicateExplained, evaluateTemplate, extractDeps, extractTemplateKeys, forType, framesFromHistory, framesFromSnapshots, generateRequirementId, initialTimerState, isPredicate, isRequirementType, isTemplate, memoizePredicate, pauseTimer, realClock, registerRepeat, remainingMs, replayUnder, req, resetTimer, resumeTimer, startTimer, sweepUnder, t, tickTimer, timerOps, toReplayFrames, validatePredicate, virtualClock };
1911
+ export { Backoff, type Branded, type ChainableSchemaType, type Change, type ChangeKind, ClauseResult, type ConstraintDiff, type ConstraintStatus, CreateSystemOptionsNamed, CreateSystemOptionsSingle, CrossModuleDeps, DefinitionMeta, type DiffRulesOptions, ErrorBoundaryConfig, type ExtendedSchemaType, FactPredicate, FactTemplate, Facts, type LeafClause, MAX_REPLAY_FRAMES, MAX_SWEEP_POINTS, type ModuleConfig, type ModuleConfigWithDeps, ModuleDef, ModuleHooks, ModuleSchema, ModulesMap, NamespacedSystem, PatchSpec, Plugin, type PredicateBacktestReport, type ReplayDiffSample, type ReplayFrame, type ReplayUnderOptions, Requirement, RequirementSet, type RequirementTypeStatus, RequirementWithId, type RulesDiffReport, type RulesMapInput, SchemaType, type SignalClock, SingleModuleSystem, type SweepHole, type SweepPoint, type SweepReport, type SweepUnderOptions, type TimerFactOpts, type TimerFactState, TraceOption, applyPatch, completeTimer, createModule, createModuleFactory, createRequirementStatusPlugin, createStatusHook, createSystem, createSystemWithStatus, defaultClock, diffClauses, diffRules, elapsedMs, evaluateKeySelector, evaluatePredicate, evaluatePredicateExplained, evaluateTemplate, extractDeps, extractTemplateKeys, flattenPredicate, forType, framesFromHistory, framesFromSnapshots, generateRequirementId, initialTimerState, isPredicate, isRequirementType, isTemplate, memoizePredicate, pauseTimer, realClock, registerRepeat, remainingMs, replayUnder, req, resetTimer, resumeTimer, startTimer, sweepUnder, t, tickTimer, timerOps, toReplayFrames, toRulesMap, validatePredicate, virtualClock };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import {a as a$1}from'./chunk-FCOZCTLY.js';export{a as createSystem}from'./chunk-FCOZCTLY.js';import {h,i,g,d,e,a as a$2,b as b$1,c}from'./chunk-ZE2RY5KP.js';export{q as DirectiveError,D as RequirementSet,o as applyPatch,n as evaluateKeySelector,h as evaluatePredicate,i as evaluatePredicateExplained,l as evaluateTemplate,k as extractDeps,m as extractTemplateKeys,C as forType,z as generateRequirementId,w as isNamespacedSystem,d as isPredicate,B as isRequirementType,v as isSingleModuleSystem,f as isTemplate,j as memoizePredicate,A as req,t as typedConstraint,u as typedResolver,g as validatePredicate}from'./chunk-ZE2RY5KP.js';import {a,l}from'./chunk-TZHC4E6S.js';export{j as diffSnapshots,k as isSignedSnapshot,h as isSnapshotExpired,f as shallowEqual,l as signSnapshot,i as validateSnapshot,m as verifySnapshotSignature}from'./chunk-T6IJUWYR.js';var b=1e6;function K(e$1,t){try{g(e$1);}catch(r){let i=r instanceof Error?r.message:String(r);throw new Error(`[Directive] replayUnder: the ${t} predicate is invalid \u2014 ${i}`)}if(!d(e$1)){let r=e$1===null||typeof e$1!="object"?`${typeof e$1} \u2014 ${JSON.stringify(e$1)}`:JSON.stringify(e$1).slice(0,80);throw new Error(`[Directive] replayUnder: the ${t} predicate is not a valid FactPredicate (got ${r})`)}let n;if(e(e$1,{operator(r,i){n===void 0&&i.startsWith("$")&&!a$2.has(i)&&(n=i);},strayOperatorKey(r){n===void 0&&!a$2.has(r)&&!b$1.has(r)&&(n=r);}}),n!==void 0)throw new Error(`[Directive] replayUnder: the ${t} predicate uses an unknown operator "${n}" \u2014 known operators: ${[...a$2].join(", ")}`)}function z(e){if(e&&typeof e=="object"&&!Array.isArray(e)&&Array.isArray(e.snapshots))return X(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>b)throw new Error(`[Directive] toReplayFrames: history has ${t.length} frames, exceeds the MAX_REPLAY_FRAMES limit (${b}) \u2014 split or down-sample the history`);return t.map((n,r)=>{if(n&&typeof n=="object"&&"facts"in n){let i=n,a={id:i.id??`#${r}`,facts:i.facts??{}};return typeof i.timestamp=="number"&&(a.timestamp=i.timestamp),a}return {id:`#${r}`,facts:n??{}}})}function X(e){let t=typeof e=="string"?JSON.parse(e):e;if(Array.isArray(t))return $(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 n=t;if(n.version!==void 0&&n.version!==1)throw new Error(`[Directive] framesFromHistory: unsupported history export version ${JSON.stringify(n.version)} \u2014 expected 1`);if(!Array.isArray(n.snapshots))throw new Error("[Directive] framesFromHistory: expected a history export object with a `snapshots` array (from system.history.export())");return $(n.snapshots)}function $(e){if(!Array.isArray(e))throw new Error("[Directive] framesFromSnapshots: expected an array of fact-state snapshots");if(e.length>b)throw new Error(`[Directive] framesFromSnapshots: history has ${e.length} snapshots, exceeds the MAX_REPLAY_FRAMES limit (${b}) \u2014 split or down-sample the history`);for(let t=0;t<e.length;t++){let n=e[t];if(!n||typeof n!="object"||!("facts"in n))throw new Error(`[Directive] framesFromSnapshots: snapshot at index ${t} is not a { facts, ... } object`)}return z(e)}function C(e){let{frames:t,original:n,proposed:r,entityKey:i}=e,a=e.maxSamples??20,o=a>0?a:0;if(t.length>b)throw new Error(`[Directive] replayUnder: history has ${t.length} frames, exceeds the MAX_REPLAY_FRAMES limit (${b}) \u2014 split or down-sample the history`);K(n,"original"),K(r,"proposed");let d=0,s=0,u=0,l=0,c=0,m=[],y=[],M=i?new Set:void 0,T=i?new Set:void 0,g;for(let v of t){let f=v.facts,h$1=h(n,f,g),S=h(r,f,g);h$1&&(d++,M?.add(f[i])),S&&(s++,T?.add(f[i])),h$1===S?c++:!h$1&&S?(u++,m.length<o&&m.push(L(v,n,r,g))):(l++,y.length<o&&y.push(L(v,n,r,g))),g=f;}let w={framesEvaluated:t.length,original:{matched:d},proposed:{matched:s},delta:s-d,newMatchCount:u,lostMatchCount:l,unchanged:c,newMatches:m,lostMatches:y};return M&&T&&(w.original.matchedEntities=M.size,w.proposed.matchedEntities=T.size),w}function L(e,t,n,r){let i$1=e.facts,a={frameId:e.id,facts:i$1,originalExplain:i(t,i$1,r),proposedExplain:i(n,i$1,r)};return e.timestamp!==void 0&&(a.timestamp=e.timestamp),a}var I=1e4,Y=5e7;function Fe(e){return e!==null&&typeof e=="object"&&!Array.isArray(e)&&Object.keys(e).length===1&&typeof e.$hole=="string"}function B(e,t,n=new Set,r=0){if(r>c)throw new Error(`[Directive] sweepUnder: template exceeds MAX_PREDICATE_DEPTH (${c}) \u2014 flatten the template or split the sweep`);if(Fe(e)){let i=e.$hole;if(!(i in t))throw new Error(`[Directive] sweepUnder: template references hole "${i}" but sweep has no values for it`);return t[i]}if(e===null||typeof e!="object")return e;if(n.has(e))throw new Error("[Directive] sweepUnder: template contains a cycle \u2014 predicate templates must be a tree");n.add(e);try{if(Array.isArray(e))return e.map(a=>B(a,t,n,r+1));let i={};for(let[a,o]of Object.entries(e))i[a]=B(o,t,n,r+1);return i}finally{n.delete(e);}}function*J(e,t){if(e.length===0){yield {};return}let n=e[0],r=e.slice(1),i=t[n]??[];for(let a of i)for(let o of J(r,t))yield {[n]:a,...o};}function Pe(e){let t=1;for(let n of Object.values(e))t*=n.length;return t}function je(e){let{frames:t,original:n,template:r,sweep:i,objective:a=f=>f.proposed.matched,entityKey:o,maxSamples:d=0}=e,s=Object.keys(i);if(s.length===0)throw new Error("[Directive] sweepUnder: `sweep` must contain at least one hole name");let u=Pe(i);if(u>I)throw new Error(`[Directive] sweepUnder: grid has ${u} points, exceeds the MAX_SWEEP_POINTS limit (${I}) \u2014 narrow the sweep ranges or split the run`);if(u===0)throw new Error("[Directive] sweepUnder: at least one hole has zero candidate values");let l=u*t.length;if(l>Y)throw new Error(`[Directive] sweepUnder: ${u} points \xD7 ${t.length} frames = ${l} per-frame evaluations, exceeds the MAX_SWEEP_EVALUATIONS limit (${Y}) \u2014 narrow the sweep, down-sample the history, or split the run`);let c=false,m=f=>{let h;try{h=a(f);}catch(S){return c||(c=true,console.warn(`[Directive] sweepUnder: objective threw \u2014 point will not rank (${S.message})`)),Number.NEGATIVE_INFINITY}return typeof h!="number"||!Number.isFinite(h)?(c||(c=true,console.warn(`[Directive] sweepUnder: objective returned a non-finite number (${String(h)}) \u2014 point will not rank`)),Number.NEGATIVE_INFINITY):h},y=C({frames:t,original:n,proposed:n,entityKey:o,maxSamples:d}),M={values:{},report:y,score:m(y)},T=[],g=0,w=Number.NEGATIVE_INFINITY;for(let f of J(s,i)){let h=B(r,f),S=C({frames:t,original:n,proposed:h,entityKey:o,maxSamples:d}),F=m(S);F>w&&(w=F,g=T.length),T.push({values:f,report:S,score:F});}let v=T[g];return {points:T,bestIndex:g,best:v,baseline:M}}function E(e=[],t,n,r,i,a,o){return {_type:void 0,_validators:e,_typeName:t,_default:n,_transform:r,_description:i,_refinements:a,_meta:o,validate(d){return E([...e,d],t,n,r,i,a,o)}}}function p(e,t,n,r,i,a,o){return {...E(e,t,n,r,i,a,o),default(s){return p(e,t,s,r,i,a,o)},transform(s){return p([],t,void 0,l=>{let c=r?r(l):l;return s(c)},i,void 0,o)},brand(){return p(e,`Branded<${t}>`,n,r,i,a,o)},describe(s){return p(e,t,n,r,s,a,o)},refine(s,u){let l=[...a??[],{predicate:s,message:u}];return p([...e,s],t,n,r,i,l,o)},nullable(){return p([s=>s===null||e.every(u=>u(s))],`${t} | null`,n,r,i,void 0,o)},optional(){return p([s=>s===void 0||e.every(u=>u(s))],`${t} | undefined`,n,r,i,void 0,o)},meta(s){return p(e,t,n,r,i,a,s)}}}var Oe=((...e)=>{if(e.length===0)return p([],"union");let t=e.map(n=>n._typeName??"unknown");return p([n=>e.some(r=>r._validators.every(i=>i(n)))],t.join(" | "))}),_e={string(){let e=(t,n,r,i,a,o)=>({...p(t,"string",n,r,i,a,o),minLength(s){return e([...t,u=>u.length>=s],n,r,i,a,o)},maxLength(s){return e([...t,u=>u.length<=s],n,r,i,a,o)},pattern(s){return e([...t,u=>s.test(u)],n,r,i,a,o)},default(s){return e(t,s,r,i,a,o)},describe(s){return e(t,n,r,s,a,o)},refine(s,u){let l=[...a??[],{predicate:s,message:u}];return e([...t,s],n,r,i,l,o)},meta(s){return e(t,n,r,i,a,s)}});return e([t=>typeof t=="string"])},number(){let e=(t,n,r,i,a,o)=>({...p(t,"number",n,r,i,a,o),min(s){return e([...t,u=>u>=s],n,r,i,a,o)},max(s){return e([...t,u=>u<=s],n,r,i,a,o)},default(s){return e(t,s,r,i,a,o)},describe(s){return e(t,n,r,s,a,o)},refine(s,u){let l=[...a??[],{predicate:s,message:u}];return e([...t,s],n,r,i,l,o)},meta(s){return e(t,n,r,i,a,s)}});return e([t=>typeof t=="number"])},boolean(){return p([e=>typeof e=="boolean"],"boolean")},array(){let e=(t,n,r,i,a,o)=>{let d=p(t,"array",r,void 0,i,void 0,o),s=a??{value:-1};return {...d,get _lastFailedIndex(){return s.value},set _lastFailedIndex(l){s.value=l;},of(l){let c={value:-1};return e([...t,m=>{for(let y=0;y<m.length;y++)if(!l._validators.every(M=>M(m[y])))return c.value=y,false;return true}],l,r,i,c,o)},nonEmpty(){return e([...t,l=>l.length>0],n,r,i,s,o)},maxLength(l){return e([...t,c=>c.length<=l],n,r,i,s,o)},minLength(l){return e([...t,c=>c.length>=l],n,r,i,s,o)},default(l){return e(t,n,l,i,s,o)},describe(l){return e(t,n,r,l,s,o)},meta(l){return e(t,n,r,i,s,l)}}};return e([t=>Array.isArray(t)])},object(){let e=(t,n,r,i)=>({...p(t,"object",n,void 0,r,void 0,i),shape(o){return e([...t,d=>{for(let[s,u]of Object.entries(o)){let l=d[s],c=u;if(c&&!c._validators.every(m=>m(l)))return false}return true}],n,r,i)},nonNull(){return e([...t,o=>o!=null],n,r,i)},hasKeys(...o){return e([...t,d=>o.every(s=>s in d)],n,r,i)},default(o){return e(t,o,r,i)},describe(o){return e(t,n,o,i)},meta(o){return e(t,n,r,o)}});return e([t=>typeof t=="object"&&t!==null&&!Array.isArray(t)])},enum(...e){a&&e.length===0&&console.warn("[Directive] t.enum() called with no values - this will reject all strings");let t=new Set(e);return p([n=>typeof n=="string"&&t.has(n)],`enum(${e.join("|")})`)},literal(e){return p([t=>t===e],`literal(${String(e)})`)},nullable(e){let t=e._typeName??"unknown";return E([n=>n===null?true:e._validators.every(r=>r(n))],`${t} | null`)},optional(e){let t=e._typeName??"unknown";return E([n=>n===void 0?true:e._validators.every(r=>r(n))],`${t} | undefined`)},union:Oe,record(e){let t=e._typeName??"unknown";return p([n=>typeof n!="object"||n===null||Array.isArray(n)?false:Object.values(n).every(r=>e._validators.every(i=>i(r)))],`Record<string, ${t}>`)},tuple(...e){a&&e.length===0&&console.warn("[Directive] t.tuple() called with no types - this will only accept empty arrays");let t=e.map(n=>n._typeName??"unknown");return p([n=>!Array.isArray(n)||n.length!==e.length?false:e.every((r,i)=>r._validators.every(a=>a(n[i])))],`[${t.join(", ")}]`)},date(){return p([e=>e instanceof Date&&!Number.isNaN(e.getTime())],"Date")},uuid(){let e=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;return p([t=>typeof t=="string"&&e.test(t)],"uuid")},email(){let e=/^[^\s@]+@[^\s@]+\.[^\s@]+$/;return p([t=>typeof t=="string"&&e.test(t)],"email")},url(){return p([e=>{if(typeof e!="string")return false;try{return new URL(e),!0}catch{return false}}],"url")},bigint(){return p([e=>typeof e=="bigint"],"bigint")},any(){return p([],"any")},unknown(){return p([],"unknown")}};function $e(e){if(!e||typeof e!="string"){console.warn("[Directive] Module ID must be a non-empty string");return}/^(__[a-z][a-z0-9_-]*|[a-z][a-z0-9-]*)$/i.test(e)||console.warn(`[Directive] Module ID "${e}" should follow kebab-case convention (e.g., "my-module")`);}function G(e,t,n,r,i){for(let a of e)t.has(a)||console.warn(`[Directive] ${n} "${a}" not declared in ${r}`);for(let a of t)e.has(a)||console.warn(`[Directive] ${r}["${a}"] ${i}`);}function Ie(e,t){e.length===0&&console.warn("[Directive] history.snapshotEvents is an empty array \u2014 no events will create history snapshots. Omit history.snapshotEvents entirely to snapshot all events, or list specific events.");let n=new Set(Object.keys(t));for(let r of e)n.has(r)||console.warn(`[Directive] history.snapshotEvents entry "${r}" not declared in schema.events. Available events: ${[...n].join(", ")||"(none)"}`);}function Be(e,t){let n=new Set(Object.keys(t));for(let[r,i]of Object.entries(e)){let a=i;typeof a.requirement=="string"&&!n.has(a.requirement)&&console.warn(`[Directive] Resolver "${r}" references unknown requirement type "${a.requirement}". Available types: ${[...n].join(", ")||"(none)"}`);}}function Ne(e,t){let n=t.schema?.facts??{},r=Object.keys(n);if(r.length===0)return;let i=new Set(["self","prev","current"]),a="crossModuleDeps"in t&&t.crossModuleDeps?Object.keys(t.crossModuleDeps):[];for(let o of a)i.add(o);for(let o of r)if(i.has(o))throw new Error(`[Directive] module '${e}': fact key '${o}' conflicts with a reserved namespace pivot or evaluation alias (self / prev / current / a crossModuleDep namespace). Three fixes:
2
- 1. Rename the fact (e.g. ${o}_)
3
- 2. Remove '${o}' from this module's crossModuleDeps if it's not actually needed
4
- 3. Move the fact under a wrapping namespace (t.object({ inner: ... }))`)}function Ue(e,t){let n=t.constraints;if(n)for(let[r,i]of Object.entries(n)){let a=i?.owns;if(a){for(let o of a)if(l.has(o)||o.startsWith("$"))throw new Error(`[Directive] module '${e}' constraint '${r}': owns key '${o}' is reserved (BLOCKED_PROPS or $-prefixed)`)}}}function qe(e,t){$e(e),t.schema?t.schema.facts||console.warn("[Directive] Module schema.facts is required"):console.warn("[Directive] Module schema is required"),G(new Set(Object.keys(t.derive??{})),new Set(Object.keys(t.schema?.derivations??{})),"Derivation","schema.derivations","has no matching implementation in derive"),G(new Set(Object.keys(t.events??{})),new Set(Object.keys(t.schema?.events??{})),"Event","schema.events","has no matching handler in events"),t.history?.snapshotEvents&&Ie(t.history.snapshotEvents,t.schema?.events??{}),t.resolvers&&t.schema?.requirements&&Be(t.resolvers,t.schema.requirements);}function Q(e,t){Ne(e,t),Ue(e,t),a&&qe(e,t);let n="crossModuleDeps"in t?t.crossModuleDeps:void 0;return {id:e,schema:t.schema,init:t.init,derive:t.derive??{},events:t.events??{},effects:t.effects,constraints:t.constraints,resolvers:t.resolvers,hooks:t.hooks,meta:t.meta,history:t.history,crossModuleDeps:n}}function We(e){return t=>Q(t,e)}function N(){let e={pending:new Map,inflight:new Map,failed:new Map,errors:new Map,listeners:new Set};function t(){for(let s of e.listeners)s();}function n(s,u){let l=s.get(u);return l||(l=new Set,s.set(u,l)),l}function r(s){let u=e.pending.get(s)??new Set,l=e.inflight.get(s)??new Set,c=e.failed.get(s)??new Set,m=e.errors.get(s)??null;return {pending:u.size,inflight:l.size,failed:c.size,isLoading:u.size>0||l.size>0,hasError:c.size>0,lastError:m}}function i(){let s=new Set([...e.pending.keys(),...e.inflight.keys(),...e.failed.keys()]),u=new Map;for(let l of s)u.set(l,r(l));return u}function a(s){return e.listeners.add(s),()=>e.listeners.delete(s)}function o(){e.pending.clear(),e.inflight.clear(),e.failed.clear(),e.errors.clear(),t();}return {plugin:{name:"requirement-status",onRequirementCreated(s){let u=s.requirement.type;n(e.pending,u).add(s.id),e.failed.get(u)?.delete(s.id),t();},onResolverStart(s,u){let l=u.requirement.type;e.pending.get(l)?.delete(u.id),n(e.inflight,l).add(u.id),t();},onResolverComplete(s,u){let l=u.requirement.type;e.inflight.get(l)?.delete(u.id),e.pending.get(l)?.delete(u.id),t();},onResolverError(s,u,l){let c=u.requirement.type;e.inflight.get(c)?.delete(u.id),n(e.failed,c).add(u.id),e.errors.set(c,l instanceof Error?l:new Error(String(l))),t();},onResolverCancel(s,u){let l=u.requirement.type;e.pending.get(l)?.delete(u.id),e.inflight.get(l)?.delete(u.id),t();},onRequirementMet(s){let u=s.requirement.type;e.pending.get(u)?.delete(s.id),e.inflight.get(u)?.delete(s.id),t();}},getStatus:r,getAllStatus:i,subscribe:a,reset:o}}function He(e){return t=>e.getStatus(t)}function Ke(e){let t=N(),r=[...e.plugins??[],t.plugin];return {system:a$1({module:e.module,plugins:r,trace:e.trace,errorBoundary:e.errorBoundary,tickMs:e.tickMs,zeroConfig:e.zeroConfig,initialFacts:e.initialFacts}),statusPlugin:t}}function Z(){return {now:()=>Date.now(),setTimeout:(e,t)=>{let n=globalThis.setTimeout(e,t);return ()=>globalThis.clearTimeout(n)}}}function Le(e=0){let t=e,n=0,r=[];return {now:()=>t,setTimeout:(i,a)=>{let o={id:n++,deadlineMs:t+a,cb:i,canceled:false};return r.push(o),()=>{o.canceled=true;}},advanceBy:i=>{let a=t+i;for(;;){let o=r.filter(s=>!s.canceled&&s.deadlineMs<=a).sort((s,u)=>s.deadlineMs!==u.deadlineMs?s.deadlineMs-u.deadlineMs:s.id-u.id);if(o.length===0)break;let d=o[0];t=Math.max(t,d.deadlineMs),d.canceled=true,d.cb();}t=Math.max(t,a);}}}function ze(){return Z()}function U(){return {startedAtMs:null,pausedDurationMs:0,pausedAtMs:null,status:"idle",repeats:0}}function A(e,t){return e.startedAtMs===null?0:e.status==="paused"&&e.pausedAtMs!==null?Math.max(0,e.pausedAtMs-e.startedAtMs-e.pausedDurationMs):Math.max(0,t-e.startedAtMs-e.pausedDurationMs)}function V(e,t,n){return Math.max(0,n-A(e,t))}function ee(e,t){return e.status==="running"||e.status==="paused"?e:{...e,startedAtMs:t,pausedDurationMs:0,pausedAtMs:null,status:"running",repeats:0}}function te(e,t){return e.status!=="running"?e:{...e,pausedAtMs:t,status:"paused"}}function ne(e,t){if(e.status!=="paused"||e.pausedAtMs===null)return e;let n=Math.max(0,t-e.pausedAtMs);return {...e,pausedDurationMs:e.pausedDurationMs+n,pausedAtMs:null,status:"running"}}function re(){return U()}function se(e){return {...e,status:"completed"}}function ie(e,t){return e.startedAtMs===null||e.status==="paused"?e:{...e,startedAtMs:e.startedAtMs+t,pausedDurationMs:0,pausedAtMs:null,repeats:e.repeats+1}}function oe(e,t,n){if(e.status!=="running")return {kind:"no-op"};let r=A(e,t);return n.mode==="up"?{kind:"no-op"}:n.mode==="repeat"?r>=n.ms?{kind:"repeat"}:{kind:"no-op"}:r>=n.ms?{kind:"complete"}:{kind:"no-op"}}function Xe(e){return {initial:U,start:ee,pause:te,resume:ne,reset:re,complete:se,registerRepeat:t=>ie(t,e.ms),tick:(t,n)=>oe(t,n,e),elapsedMs:A,remainingMs:(t,n)=>V(t,n,e.ms)}}var pt={None:"none",Linear:"linear",Exponential:"exponential"};export{pt as Backoff,b as MAX_REPLAY_FRAMES,I as MAX_SWEEP_POINTS,se as completeTimer,Q as createModule,We as createModuleFactory,N as createRequirementStatusPlugin,He as createStatusHook,Ke as createSystemWithStatus,ze as defaultClock,A as elapsedMs,X as framesFromHistory,$ as framesFromSnapshots,U as initialTimerState,te as pauseTimer,Z as realClock,ie as registerRepeat,V as remainingMs,C as replayUnder,re as resetTimer,ne as resumeTimer,ee as startTimer,je as sweepUnder,_e as t,oe as tickTimer,Xe as timerOps,z as toReplayFrames,Le as virtualClock};//# sourceMappingURL=index.js.map
1
+ import {a as a$1}from'./chunk-FCOZCTLY.js';export{a as createSystem}from'./chunk-FCOZCTLY.js';import {h,i,g,d as d$1,e,a as a$2,b,c}from'./chunk-ZE2RY5KP.js';export{q as DirectiveError,D as RequirementSet,o as applyPatch,n as evaluateKeySelector,h as evaluatePredicate,i as evaluatePredicateExplained,l as evaluateTemplate,k as extractDeps,m as extractTemplateKeys,C as forType,z as generateRequirementId,w as isNamespacedSystem,d as isPredicate,B as isRequirementType,v as isSingleModuleSystem,f as isTemplate,j as memoizePredicate,A as req,t as typedConstraint,u as typedResolver,g as validatePredicate}from'./chunk-ZE2RY5KP.js';import {a,l}from'./chunk-TZHC4E6S.js';import {d}from'./chunk-T6IJUWYR.js';export{j as diffSnapshots,k as isSignedSnapshot,h as isSnapshotExpired,f as shallowEqual,l as signSnapshot,i as validateSnapshot,m as verifySnapshotSignature}from'./chunk-T6IJUWYR.js';var v=1e6;function J(e$1,n){try{g(e$1);}catch(r){let s=r instanceof Error?r.message:String(r);throw new Error(`[Directive] replayUnder: the ${n} predicate is invalid \u2014 ${s}`)}if(!d$1(e$1)){let r=e$1===null||typeof e$1!="object"?`${typeof e$1} \u2014 ${JSON.stringify(e$1)}`:JSON.stringify(e$1).slice(0,80);throw new Error(`[Directive] replayUnder: the ${n} predicate is not a valid FactPredicate (got ${r})`)}let t;if(e(e$1,{operator(r,s){t===void 0&&s.startsWith("$")&&!a$2.has(s)&&(t=s);},strayOperatorKey(r){t===void 0&&!a$2.has(r)&&!b.has(r)&&(t=r);}}),t!==void 0)throw new Error(`[Directive] replayUnder: the ${n} predicate uses an unknown operator "${t}" \u2014 known operators: ${[...a$2].join(", ")}`)}function Q(e){if(e&&typeof e=="object"&&!Array.isArray(e)&&Array.isArray(e.snapshots))return Z(e);let n=Array.isArray(e)?e:e&&typeof e=="object"&&Array.isArray(e.frames)?e.frames:null;if(!n)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(n.length>v)throw new Error(`[Directive] toReplayFrames: history has ${n.length} frames, exceeds the MAX_REPLAY_FRAMES limit (${v}) \u2014 split or down-sample the history`);return n.map((t,r)=>{if(t&&typeof t=="object"&&"facts"in t){let s=t,o={id:s.id??`#${r}`,facts:s.facts??{}};return typeof s.timestamp=="number"&&(o.timestamp=s.timestamp),o}return {id:`#${r}`,facts:t??{}}})}function Z(e){let n=typeof e=="string"?JSON.parse(e):e;if(Array.isArray(n))return N(n);if(!n||typeof n!="object")throw new Error("[Directive] framesFromHistory: expected a history export object with a `snapshots` array (from system.history.export())");let t=n;if(t.version!==void 0&&t.version!==1)throw new Error(`[Directive] framesFromHistory: unsupported history export version ${JSON.stringify(t.version)} \u2014 expected 1`);if(!Array.isArray(t.snapshots))throw new Error("[Directive] framesFromHistory: expected a history export object with a `snapshots` array (from system.history.export())");return N(t.snapshots)}function N(e){if(!Array.isArray(e))throw new Error("[Directive] framesFromSnapshots: expected an array of fact-state snapshots");if(e.length>v)throw new Error(`[Directive] framesFromSnapshots: history has ${e.length} snapshots, exceeds the MAX_REPLAY_FRAMES limit (${v}) \u2014 split or down-sample the history`);for(let n=0;n<e.length;n++){let t=e[n];if(!t||typeof t!="object"||!("facts"in t))throw new Error(`[Directive] framesFromSnapshots: snapshot at index ${n} is not a { facts, ... } object`)}return Q(e)}function $(e){let{frames:n,original:t,proposed:r,entityKey:s}=e,o=e.maxSamples??20,a=o>0?o:0;if(n.length>v)throw new Error(`[Directive] replayUnder: history has ${n.length} frames, exceeds the MAX_REPLAY_FRAMES limit (${v}) \u2014 split or down-sample the history`);J(t,"original"),J(r,"proposed");let d=0,i=0,u=0,l=0,c=0,m=[],f=[],y=s?new Set:void 0,h$1=s?new Set:void 0,b;for(let D of n){let g=D.facts,S=h(t,g,b),T=h(r,g,b);S&&(d++,y?.add(g[s])),T&&(i++,h$1?.add(g[s])),S===T?c++:!S&&T?(u++,m.length<a&&m.push(G(D,t,r,b))):(l++,f.length<a&&f.push(G(D,t,r,b))),b=g;}let k={framesEvaluated:n.length,original:{matched:d},proposed:{matched:i},delta:i-d,newMatchCount:u,lostMatchCount:l,unchanged:c,newMatches:m,lostMatches:f};return y&&h$1&&(k.original.matchedEntities=y.size,k.proposed.matchedEntities=h$1.size),k}function G(e,n,t,r){let s=e.facts,o={frameId:e.id,facts:s,originalExplain:i(n,s,r),proposedExplain:i(t,s,r)};return e.timestamp!==void 0&&(o.timestamp=e.timestamp),o}var U=1e4,V=5e7;function Be(e){return e!==null&&typeof e=="object"&&!Array.isArray(e)&&Object.keys(e).length===1&&typeof e.$hole=="string"}function q(e,n,t=new Set,r=0){if(r>c)throw new Error(`[Directive] sweepUnder: template exceeds MAX_PREDICATE_DEPTH (${c}) \u2014 flatten the template or split the sweep`);if(Be(e)){let s=e.$hole;if(!(s in n))throw new Error(`[Directive] sweepUnder: template references hole "${s}" but sweep has no values for it`);return n[s]}if(e===null||typeof e!="object")return e;if(t.has(e))throw new Error("[Directive] sweepUnder: template contains a cycle \u2014 predicate templates must be a tree");t.add(e);try{if(Array.isArray(e))return e.map(o=>q(o,n,t,r+1));let s={};for(let[o,a]of Object.entries(e))s[o]=q(a,n,t,r+1);return s}finally{t.delete(e);}}function*ee(e,n){if(e.length===0){yield {};return}let t=e[0],r=e.slice(1),s=n[t]??[];for(let o of s)for(let a of ee(r,n))yield {[t]:o,...a};}function Ne(e){let n=1;for(let t of Object.values(e))n*=t.length;return n}function Ue(e){let{frames:n,original:t,template:r,sweep:s,objective:o=g=>g.proposed.matched,entityKey:a,maxSamples:d=0}=e,i=Object.keys(s);if(i.length===0)throw new Error("[Directive] sweepUnder: `sweep` must contain at least one hole name");let u=Ne(s);if(u>U)throw new Error(`[Directive] sweepUnder: grid has ${u} points, exceeds the MAX_SWEEP_POINTS limit (${U}) \u2014 narrow the sweep ranges or split the run`);if(u===0)throw new Error("[Directive] sweepUnder: at least one hole has zero candidate values");let l=u*n.length;if(l>V)throw new Error(`[Directive] sweepUnder: ${u} points \xD7 ${n.length} frames = ${l} per-frame evaluations, exceeds the MAX_SWEEP_EVALUATIONS limit (${V}) \u2014 narrow the sweep, down-sample the history, or split the run`);let c=false,m=g=>{let S;try{S=o(g);}catch(T){return c||(c=true,console.warn(`[Directive] sweepUnder: objective threw \u2014 point will not rank (${T.message})`)),Number.NEGATIVE_INFINITY}return typeof S!="number"||!Number.isFinite(S)?(c||(c=true,console.warn(`[Directive] sweepUnder: objective returned a non-finite number (${String(S)}) \u2014 point will not rank`)),Number.NEGATIVE_INFINITY):S},f=$({frames:n,original:t,proposed:t,entityKey:a,maxSamples:d}),y={values:{},report:f,score:m(f)},h=[],b=0,k=Number.NEGATIVE_INFINITY;for(let g of ee(i,s)){let S=q(r,g),T=$({frames:n,original:t,proposed:S,entityKey:a,maxSamples:d}),O=m(T);O>k&&(k=O,b=h.length),h.push({values:g,report:T,score:O});}let D=h[b];return {points:h,bestIndex:b,best:D,baseline:y}}function W(e){let n=e&&typeof e=="object"&&!Array.isArray(e)&&"constraints"in e?e.constraints:e;if(Array.isArray(n)){let t={};for(let r of n){if(!r||typeof r!="object"||!("id"in r))throw new Error("[Directive] diffRules: array entries must be `{ id, whenSpec }` objects");let s=r;if(typeof s.id!="string")throw new Error("[Directive] diffRules: constraint `id` must be a string");t[s.id]=s.whenSpec;}return t}if(n&&typeof n=="object")return n;throw new Error("[Directive] diffRules: expected a `{ id: whenSpec }` map, an array of `{ id, whenSpec }`, or `{ constraints: ... }`")}var qe=new Set(["$eq","$ne","$in","$nin","$exists","$gt","$gte","$lt","$lte","$between","$matches","$startsWith","$endsWith","$contains","$changed"]),We=new Set(["$all","$any","$not"]);function ne(e){return e!==null&&typeof e=="object"&&!Array.isArray(e)}function Le(e){if(!ne(e))return false;let n=Object.keys(e);if(n.length===0)return false;let t=false;for(let r of n){if(!r.startsWith("$"))return false;qe.has(r)&&(t=true);}return t}function M(e,n="",t=[]){if(e===null||typeof e!="object")return t;if(Array.isArray(e)){for(let r of e)if(r&&typeof r=="object"&&"fact"in r&&"op"in r){let s=r;t.push({path:n?`${n}.${String(s.fact)}`:String(s.fact),op:String(s.op),value:s.value});}return t}if("$all"in e&&Array.isArray(e.$all))return e.$all.forEach((s,o)=>{M(s,`${n}$all[${o}]`,t);}),t;if("$any"in e&&Array.isArray(e.$any))return e.$any.forEach((s,o)=>{M(s,`${n}$any[${o}]`,t);}),t;if("$not"in e)return M(e.$not,`${n}$not`,t),t;for(let[r,s]of Object.entries(e)){let o=n?`${n}.${r}`:r;if(Le(s))for(let[a,d]of Object.entries(s))t.push({path:o,op:a,value:d});else ne(s)&&!We.has(r)?M(s,o,t):t.push({path:o,op:"$eq",value:s});}return t}function w(e){return typeof e=="number"&&Number.isFinite(e)}function Ke(e,n,t){switch(e){case "$gte":case "$gt":if(w(n)&&w(t)){if(t<n)return "relaxed";if(t>n)return "tightened"}return null;case "$lte":case "$lt":if(w(n)&&w(t)){if(t>n)return "relaxed";if(t<n)return "tightened"}return null;case "$between":{if(Array.isArray(n)&&Array.isArray(t)&&n.length===2&&t.length===2&&w(n[0])&&w(n[1])&&w(t[0])&&w(t[1])){let r=n[1]-n[0],s=t[1]-t[0];if(s>r)return "relaxed";if(s<r)return "tightened"}return null}case "$in":case "$nin":{if(Array.isArray(n)&&Array.isArray(t))if(e==="$in"){if(t.length>n.length)return "relaxed";if(t.length<n.length)return "tightened"}else {if(t.length>n.length)return "tightened";if(t.length<n.length)return "relaxed"}return null}case "$contains":{if(Array.isArray(n)&&Array.isArray(t)){if(t.length>n.length)return "relaxed";if(t.length<n.length)return "tightened"}return null}default:return null}}function He(e){let n=W(e.before),t=W(e.after),s=[...new Set([...Object.keys(n),...Object.keys(t)])].sort(),o=[],a={added:0,removed:0,changed:0,unchanged:0,totalClauseChanges:0};for(let d of s){let i=n[d],u=t[d],l=d in n,c=d in t;if(l&&!c){let f=M(i),y=f.length===0?[{path:"(function-form predicate)",kind:"removed"}]:f.map(h=>({path:h.path,kind:"removed",before:{op:h.op,value:h.value}}));L(y),o.push({id:d,status:"removed",changes:y}),a.removed++,a.totalClauseChanges+=y.length;continue}if(!l&&c){let f=M(u),y=f.length===0?[{path:"(function-form predicate)",kind:"added"}]:f.map(h=>({path:h.path,kind:"added",after:{op:h.op,value:h.value}}));L(y),o.push({id:d,status:"added",changes:y}),a.added++,a.totalClauseChanges+=y.length;continue}let m=te(i,u);m.length===0?(o.push({id:d,status:"unchanged",changes:[]}),a.unchanged++):(o.push({id:d,status:"changed",changes:m}),a.changed++,a.totalClauseChanges+=m.length);}return {constraints:o,summary:a}}function te(e,n){if(e!==void 0&&n!==void 0&&(e===null||n===null||typeof e!="object"||typeof n!="object"))return d(e)===d(n)?[]:[{path:"",kind:"changed",before:{op:"$eq",value:e},after:{op:"$eq",value:n}}];let t=e===void 0?[]:M(e),r=n===void 0?[]:M(n),s=u=>`${u.path}::${u.op}`,o=new Map(t.map(u=>[s(u),u])),a=new Map(r.map(u=>[s(u),u])),d$1=new Set([...o.keys(),...a.keys()]),i=[];for(let u of d$1){let l=o.get(u),c=a.get(u);if(l&&!c){i.push({path:l.path,kind:"removed",before:{op:l.op,value:l.value}});continue}if(!l&&c){i.push({path:c.path,kind:"added",after:{op:c.op,value:c.value}});continue}if(l&&c){if(d(l.value)===d(c.value))continue;let m=Ke(l.op,l.value,c.value);i.push({path:l.path,kind:m??"changed",before:{op:l.op,value:l.value},after:{op:c.op,value:c.value}});}}return L(i),i}function L(e){e.sort((n,t)=>{let r=n.path.localeCompare(t.path);if(r!==0)return r;let s=n.before?.op??n.after?.op??"",o=t.before?.op??t.after?.op??"";return s.localeCompare(o)});}function F(e=[],n,t,r,s,o,a){return {_type:void 0,_validators:e,_typeName:n,_default:t,_transform:r,_description:s,_refinements:o,_meta:a,validate(d){return F([...e,d],n,t,r,s,o,a)}}}function p(e,n,t,r,s,o,a){return {...F(e,n,t,r,s,o,a),default(i){return p(e,n,i,r,s,o,a)},transform(i){return p([],n,void 0,l=>{let c=r?r(l):l;return i(c)},s,void 0,a)},brand(){return p(e,`Branded<${n}>`,t,r,s,o,a)},describe(i){return p(e,n,t,r,i,o,a)},refine(i,u){let l=[...o??[],{predicate:i,message:u}];return p([...e,i],n,t,r,s,l,a)},nullable(){return p([i=>i===null||e.every(u=>u(i))],`${n} | null`,t,r,s,void 0,a)},optional(){return p([i=>i===void 0||e.every(u=>u(i))],`${n} | undefined`,t,r,s,void 0,a)},meta(i){return p(e,n,t,r,s,o,i)}}}var ze=((...e)=>{if(e.length===0)return p([],"union");let n=e.map(t=>t._typeName??"unknown");return p([t=>e.some(r=>r._validators.every(s=>s(t)))],n.join(" | "))}),Xe={string(){let e=(n,t,r,s,o,a)=>({...p(n,"string",t,r,s,o,a),minLength(i){return e([...n,u=>u.length>=i],t,r,s,o,a)},maxLength(i){return e([...n,u=>u.length<=i],t,r,s,o,a)},pattern(i){return e([...n,u=>i.test(u)],t,r,s,o,a)},default(i){return e(n,i,r,s,o,a)},describe(i){return e(n,t,r,i,o,a)},refine(i,u){let l=[...o??[],{predicate:i,message:u}];return e([...n,i],t,r,s,l,a)},meta(i){return e(n,t,r,s,o,i)}});return e([n=>typeof n=="string"])},number(){let e=(n,t,r,s,o,a)=>({...p(n,"number",t,r,s,o,a),min(i){return e([...n,u=>u>=i],t,r,s,o,a)},max(i){return e([...n,u=>u<=i],t,r,s,o,a)},default(i){return e(n,i,r,s,o,a)},describe(i){return e(n,t,r,i,o,a)},refine(i,u){let l=[...o??[],{predicate:i,message:u}];return e([...n,i],t,r,s,l,a)},meta(i){return e(n,t,r,s,o,i)}});return e([n=>typeof n=="number"])},boolean(){return p([e=>typeof e=="boolean"],"boolean")},array(){let e=(n,t,r,s,o,a)=>{let d=p(n,"array",r,void 0,s,void 0,a),i=o??{value:-1};return {...d,get _lastFailedIndex(){return i.value},set _lastFailedIndex(l){i.value=l;},of(l){let c={value:-1};return e([...n,m=>{for(let f=0;f<m.length;f++)if(!l._validators.every(y=>y(m[f])))return c.value=f,false;return true}],l,r,s,c,a)},nonEmpty(){return e([...n,l=>l.length>0],t,r,s,i,a)},maxLength(l){return e([...n,c=>c.length<=l],t,r,s,i,a)},minLength(l){return e([...n,c=>c.length>=l],t,r,s,i,a)},default(l){return e(n,t,l,s,i,a)},describe(l){return e(n,t,r,l,i,a)},meta(l){return e(n,t,r,s,i,l)}}};return e([n=>Array.isArray(n)])},object(){let e=(n,t,r,s)=>({...p(n,"object",t,void 0,r,void 0,s),shape(a){return e([...n,d=>{for(let[i,u]of Object.entries(a)){let l=d[i],c=u;if(c&&!c._validators.every(m=>m(l)))return false}return true}],t,r,s)},nonNull(){return e([...n,a=>a!=null],t,r,s)},hasKeys(...a){return e([...n,d=>a.every(i=>i in d)],t,r,s)},default(a){return e(n,a,r,s)},describe(a){return e(n,t,a,s)},meta(a){return e(n,t,r,a)}});return e([n=>typeof n=="object"&&n!==null&&!Array.isArray(n)])},enum(...e){a&&e.length===0&&console.warn("[Directive] t.enum() called with no values - this will reject all strings");let n=new Set(e);return p([t=>typeof t=="string"&&n.has(t)],`enum(${e.join("|")})`)},literal(e){return p([n=>n===e],`literal(${String(e)})`)},nullable(e){let n=e._typeName??"unknown";return F([t=>t===null?true:e._validators.every(r=>r(t))],`${n} | null`)},optional(e){let n=e._typeName??"unknown";return F([t=>t===void 0?true:e._validators.every(r=>r(t))],`${n} | undefined`)},union:ze,record(e){let n=e._typeName??"unknown";return p([t=>typeof t!="object"||t===null||Array.isArray(t)?false:Object.values(t).every(r=>e._validators.every(s=>s(r)))],`Record<string, ${n}>`)},tuple(...e){a&&e.length===0&&console.warn("[Directive] t.tuple() called with no types - this will only accept empty arrays");let n=e.map(t=>t._typeName??"unknown");return p([t=>!Array.isArray(t)||t.length!==e.length?false:e.every((r,s)=>r._validators.every(o=>o(t[s])))],`[${n.join(", ")}]`)},date(){return p([e=>e instanceof Date&&!Number.isNaN(e.getTime())],"Date")},uuid(){let e=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;return p([n=>typeof n=="string"&&e.test(n)],"uuid")},email(){let e=/^[^\s@]+@[^\s@]+\.[^\s@]+$/;return p([n=>typeof n=="string"&&e.test(n)],"email")},url(){return p([e=>{if(typeof e!="string")return false;try{return new URL(e),!0}catch{return false}}],"url")},bigint(){return p([e=>typeof e=="bigint"],"bigint")},any(){return p([],"any")},unknown(){return p([],"unknown")}};function Ye(e){if(!e||typeof e!="string"){console.warn("[Directive] Module ID must be a non-empty string");return}/^(__[a-z][a-z0-9_-]*|[a-z][a-z0-9-]*)$/i.test(e)||console.warn(`[Directive] Module ID "${e}" should follow kebab-case convention (e.g., "my-module")`);}function re(e,n,t,r,s){for(let o of e)n.has(o)||console.warn(`[Directive] ${t} "${o}" not declared in ${r}`);for(let o of n)e.has(o)||console.warn(`[Directive] ${r}["${o}"] ${s}`);}function Je(e,n){e.length===0&&console.warn("[Directive] history.snapshotEvents is an empty array \u2014 no events will create history snapshots. Omit history.snapshotEvents entirely to snapshot all events, or list specific events.");let t=new Set(Object.keys(n));for(let r of e)t.has(r)||console.warn(`[Directive] history.snapshotEvents entry "${r}" not declared in schema.events. Available events: ${[...t].join(", ")||"(none)"}`);}function Ge(e,n){let t=new Set(Object.keys(n));for(let[r,s]of Object.entries(e)){let o=s;typeof o.requirement=="string"&&!t.has(o.requirement)&&console.warn(`[Directive] Resolver "${r}" references unknown requirement type "${o.requirement}". Available types: ${[...t].join(", ")||"(none)"}`);}}function Qe(e,n){let t=n.schema?.facts??{},r=Object.keys(t);if(r.length===0)return;let s=new Set(["self","prev","current"]),o="crossModuleDeps"in n&&n.crossModuleDeps?Object.keys(n.crossModuleDeps):[];for(let a of o)s.add(a);for(let a of r)if(s.has(a))throw new Error(`[Directive] module '${e}': fact key '${a}' conflicts with a reserved namespace pivot or evaluation alias (self / prev / current / a crossModuleDep namespace). Three fixes:
2
+ 1. Rename the fact (e.g. ${a}_)
3
+ 2. Remove '${a}' from this module's crossModuleDeps if it's not actually needed
4
+ 3. Move the fact under a wrapping namespace (t.object({ inner: ... }))`)}function Ze(e,n){let t=n.constraints;if(t)for(let[r,s]of Object.entries(t)){let o=s?.owns;if(o){for(let a of o)if(l.has(a)||a.startsWith("$"))throw new Error(`[Directive] module '${e}' constraint '${r}': owns key '${a}' is reserved (BLOCKED_PROPS or $-prefixed)`)}}}function Ve(e,n){Ye(e),n.schema?n.schema.facts||console.warn("[Directive] Module schema.facts is required"):console.warn("[Directive] Module schema is required"),re(new Set(Object.keys(n.derive??{})),new Set(Object.keys(n.schema?.derivations??{})),"Derivation","schema.derivations","has no matching implementation in derive"),re(new Set(Object.keys(n.events??{})),new Set(Object.keys(n.schema?.events??{})),"Event","schema.events","has no matching handler in events"),n.history?.snapshotEvents&&Je(n.history.snapshotEvents,n.schema?.events??{}),n.resolvers&&n.schema?.requirements&&Ge(n.resolvers,n.schema.requirements);}function se(e,n){Qe(e,n),Ze(e,n),a&&Ve(e,n);let t="crossModuleDeps"in n?n.crossModuleDeps:void 0;return {id:e,schema:n.schema,init:n.init,derive:n.derive??{},events:n.events??{},effects:n.effects,constraints:n.constraints,resolvers:n.resolvers,hooks:n.hooks,meta:n.meta,history:n.history,crossModuleDeps:t}}function en(e){return n=>se(n,e)}function K(){let e={pending:new Map,inflight:new Map,failed:new Map,errors:new Map,listeners:new Set};function n(){for(let i of e.listeners)i();}function t(i,u){let l=i.get(u);return l||(l=new Set,i.set(u,l)),l}function r(i){let u=e.pending.get(i)??new Set,l=e.inflight.get(i)??new Set,c=e.failed.get(i)??new Set,m=e.errors.get(i)??null;return {pending:u.size,inflight:l.size,failed:c.size,isLoading:u.size>0||l.size>0,hasError:c.size>0,lastError:m}}function s(){let i=new Set([...e.pending.keys(),...e.inflight.keys(),...e.failed.keys()]),u=new Map;for(let l of i)u.set(l,r(l));return u}function o(i){return e.listeners.add(i),()=>e.listeners.delete(i)}function a(){e.pending.clear(),e.inflight.clear(),e.failed.clear(),e.errors.clear(),n();}return {plugin:{name:"requirement-status",onRequirementCreated(i){let u=i.requirement.type;t(e.pending,u).add(i.id),e.failed.get(u)?.delete(i.id),n();},onResolverStart(i,u){let l=u.requirement.type;e.pending.get(l)?.delete(u.id),t(e.inflight,l).add(u.id),n();},onResolverComplete(i,u){let l=u.requirement.type;e.inflight.get(l)?.delete(u.id),e.pending.get(l)?.delete(u.id),n();},onResolverError(i,u,l){let c=u.requirement.type;e.inflight.get(c)?.delete(u.id),t(e.failed,c).add(u.id),e.errors.set(c,l instanceof Error?l:new Error(String(l))),n();},onResolverCancel(i,u){let l=u.requirement.type;e.pending.get(l)?.delete(u.id),e.inflight.get(l)?.delete(u.id),n();},onRequirementMet(i){let u=i.requirement.type;e.pending.get(u)?.delete(i.id),e.inflight.get(u)?.delete(i.id),n();}},getStatus:r,getAllStatus:s,subscribe:o,reset:a}}function nn(e){return n=>e.getStatus(n)}function tn(e){let n=K(),r=[...e.plugins??[],n.plugin];return {system:a$1({module:e.module,plugins:r,trace:e.trace,errorBoundary:e.errorBoundary,tickMs:e.tickMs,zeroConfig:e.zeroConfig,initialFacts:e.initialFacts}),statusPlugin:n}}function ie(){return {now:()=>Date.now(),setTimeout:(e,n)=>{let t=globalThis.setTimeout(e,n);return ()=>globalThis.clearTimeout(t)}}}function rn(e=0){let n=e,t=0,r=[];return {now:()=>n,setTimeout:(s,o)=>{let a={id:t++,deadlineMs:n+o,cb:s,canceled:false};return r.push(a),()=>{a.canceled=true;}},advanceBy:s=>{let o=n+s;for(;;){let a=r.filter(i=>!i.canceled&&i.deadlineMs<=o).sort((i,u)=>i.deadlineMs!==u.deadlineMs?i.deadlineMs-u.deadlineMs:i.id-u.id);if(a.length===0)break;let d=a[0];n=Math.max(n,d.deadlineMs),d.canceled=true,d.cb();}n=Math.max(n,o);}}}function sn(){return ie()}function H(){return {startedAtMs:null,pausedDurationMs:0,pausedAtMs:null,status:"idle",repeats:0}}function j(e,n){return e.startedAtMs===null?0:e.status==="paused"&&e.pausedAtMs!==null?Math.max(0,e.pausedAtMs-e.startedAtMs-e.pausedDurationMs):Math.max(0,n-e.startedAtMs-e.pausedDurationMs)}function ae(e,n,t){return Math.max(0,t-j(e,n))}function oe(e,n){return e.status==="running"||e.status==="paused"?e:{...e,startedAtMs:n,pausedDurationMs:0,pausedAtMs:null,status:"running",repeats:0}}function ue(e,n){return e.status!=="running"?e:{...e,pausedAtMs:n,status:"paused"}}function le(e,n){if(e.status!=="paused"||e.pausedAtMs===null)return e;let t=Math.max(0,n-e.pausedAtMs);return {...e,pausedDurationMs:e.pausedDurationMs+t,pausedAtMs:null,status:"running"}}function ce(){return H()}function de(e){return {...e,status:"completed"}}function pe(e,n){return e.startedAtMs===null||e.status==="paused"?e:{...e,startedAtMs:e.startedAtMs+n,pausedDurationMs:0,pausedAtMs:null,repeats:e.repeats+1}}function me(e,n,t){if(e.status!=="running")return {kind:"no-op"};let r=j(e,n);return t.mode==="up"?{kind:"no-op"}:t.mode==="repeat"?r>=t.ms?{kind:"repeat"}:{kind:"no-op"}:r>=t.ms?{kind:"complete"}:{kind:"no-op"}}function an(e){return {initial:H,start:oe,pause:ue,resume:le,reset:ce,complete:de,registerRepeat:n=>pe(n,e.ms),tick:(n,t)=>me(n,t,e),elapsedMs:j,remainingMs:(n,t)=>ae(n,t,e.ms)}}var xn={None:"none",Linear:"linear",Exponential:"exponential"};export{xn as Backoff,v as MAX_REPLAY_FRAMES,U as MAX_SWEEP_POINTS,de as completeTimer,se as createModule,en as createModuleFactory,K as createRequirementStatusPlugin,nn as createStatusHook,tn as createSystemWithStatus,sn as defaultClock,te as diffClauses,He as diffRules,j as elapsedMs,M as flattenPredicate,Z as framesFromHistory,N as framesFromSnapshots,H as initialTimerState,ue as pauseTimer,ie as realClock,pe as registerRepeat,ae as remainingMs,$ as replayUnder,ce as resetTimer,le as resumeTimer,oe as startTimer,Ue as sweepUnder,Xe as t,me as tickTimer,an as timerOps,Q as toReplayFrames,W as toRulesMap,rn as virtualClock};//# sourceMappingURL=index.js.map
5
5
  //# sourceMappingURL=index.js.map