@directive-run/ai 1.20.2 → 1.21.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.
@@ -720,7 +720,7 @@ interface OrchestratorOptions<F extends Record<string, unknown>> {
720
720
  * @example
721
721
  * ```typescript
722
722
  * import { t } from '@directive-run/core';
723
- * const orchestrator = createOrchestrator({
723
+ * const orchestrator = createAgentOrchestrator({
724
724
  * factsSchema: { confidence: t.number(), category: t.string() },
725
725
  * // ...
726
726
  * });
@@ -775,6 +775,52 @@ interface OrchestratorOptions<F extends Record<string, unknown>> {
775
775
  maxBudget: number;
776
776
  percentage: number;
777
777
  }) => void;
778
+ /**
779
+ * Optional reservation hint for the budget pre-flight check. When set
780
+ * (number or per-call callback), the orchestrator reserves that many
781
+ * tokens for each in-flight call so concurrent runs cannot collectively
782
+ * overshoot `maxTokenBudget`. Without this, the pre-flight only catches
783
+ * the "already over" case — N concurrent runs can each individually
784
+ * read `tokenUsage = 0` and overshoot together (TOCTOU).
785
+ *
786
+ * - `undefined` (default): pre-flight only checks the already-over
787
+ * condition. Concurrent runs may still collectively overshoot.
788
+ * - `number`: reserve this many tokens per call (typical small-agent
789
+ * ceiling is 2000–8000).
790
+ * - `(input, agent) => number`: per-call estimate.
791
+ *
792
+ * @example
793
+ * ```ts
794
+ * createAgentOrchestrator({
795
+ * maxTokenBudget: 100_000,
796
+ * budgetEstimateTokens: 5_000, // conservative reservation per call
797
+ * });
798
+ * ```
799
+ */
800
+ budgetEstimateTokens?: number | ((input: string, agent: AgentLike) => number);
801
+ /**
802
+ * Controls how `circuitBreaker` composes with `agentRetry`.
803
+ *
804
+ * Prior to v1.21, the orchestrator wrapped the entire retry loop in
805
+ * one `circuitBreaker.execute(...)` call — `attempts: 3` +
806
+ * `failureThreshold: 5` required 15 real upstream failures before the
807
+ * breaker opened. That's the retry-storm anti-pattern: a transient
808
+ * provider outage cost roughly `attempts × failureThreshold` calls
809
+ * before any protection kicked in.
810
+ *
811
+ * The new default (`false`) routes each retry attempt through the
812
+ * breaker individually. Each failed attempt counts toward
813
+ * `failureThreshold`; if the breaker opens partway through retries,
814
+ * the remaining attempts short-circuit and the orchestrator surfaces
815
+ * the breaker's open-circuit error to the caller. This is the
816
+ * correct semantic under sustained upstream failure.
817
+ *
818
+ * Set to `true` for the legacy behavior (one breaker call per
819
+ * top-level run, retries hidden from the breaker).
820
+ *
821
+ * @default false
822
+ */
823
+ retryInsideCircuit?: boolean;
778
824
  /** Plugins */
779
825
  plugins?: Plugin[];
780
826
  /**
@@ -720,7 +720,7 @@ interface OrchestratorOptions<F extends Record<string, unknown>> {
720
720
  * @example
721
721
  * ```typescript
722
722
  * import { t } from '@directive-run/core';
723
- * const orchestrator = createOrchestrator({
723
+ * const orchestrator = createAgentOrchestrator({
724
724
  * factsSchema: { confidence: t.number(), category: t.string() },
725
725
  * // ...
726
726
  * });
@@ -775,6 +775,52 @@ interface OrchestratorOptions<F extends Record<string, unknown>> {
775
775
  maxBudget: number;
776
776
  percentage: number;
777
777
  }) => void;
778
+ /**
779
+ * Optional reservation hint for the budget pre-flight check. When set
780
+ * (number or per-call callback), the orchestrator reserves that many
781
+ * tokens for each in-flight call so concurrent runs cannot collectively
782
+ * overshoot `maxTokenBudget`. Without this, the pre-flight only catches
783
+ * the "already over" case — N concurrent runs can each individually
784
+ * read `tokenUsage = 0` and overshoot together (TOCTOU).
785
+ *
786
+ * - `undefined` (default): pre-flight only checks the already-over
787
+ * condition. Concurrent runs may still collectively overshoot.
788
+ * - `number`: reserve this many tokens per call (typical small-agent
789
+ * ceiling is 2000–8000).
790
+ * - `(input, agent) => number`: per-call estimate.
791
+ *
792
+ * @example
793
+ * ```ts
794
+ * createAgentOrchestrator({
795
+ * maxTokenBudget: 100_000,
796
+ * budgetEstimateTokens: 5_000, // conservative reservation per call
797
+ * });
798
+ * ```
799
+ */
800
+ budgetEstimateTokens?: number | ((input: string, agent: AgentLike) => number);
801
+ /**
802
+ * Controls how `circuitBreaker` composes with `agentRetry`.
803
+ *
804
+ * Prior to v1.21, the orchestrator wrapped the entire retry loop in
805
+ * one `circuitBreaker.execute(...)` call — `attempts: 3` +
806
+ * `failureThreshold: 5` required 15 real upstream failures before the
807
+ * breaker opened. That's the retry-storm anti-pattern: a transient
808
+ * provider outage cost roughly `attempts × failureThreshold` calls
809
+ * before any protection kicked in.
810
+ *
811
+ * The new default (`false`) routes each retry attempt through the
812
+ * breaker individually. Each failed attempt counts toward
813
+ * `failureThreshold`; if the breaker opens partway through retries,
814
+ * the remaining attempts short-circuit and the orchestrator surfaces
815
+ * the breaker's open-circuit error to the caller. This is the
816
+ * correct semantic under sustained upstream failure.
817
+ *
818
+ * Set to `true` for the legacy behavior (one breaker call per
819
+ * top-level run, retries hidden from the breaker).
820
+ *
821
+ * @default false
822
+ */
823
+ retryInsideCircuit?: boolean;
778
824
  /** Plugins */
779
825
  plugins?: Plugin[];
780
826
  /**
package/dist/testing.cjs CHANGED
@@ -1,2 +1,2 @@
1
- 'use strict';var chunk2C3R77ZW_cjs=require('./chunk-2C3R77ZW.cjs'),chunk7EJN6TYX_cjs=require('./chunk-7EJN6TYX.cjs');require('./chunk-K64WKZ22.cjs');var chunkWOFIBIPW_cjs=require('./chunk-WOFIBIPW.cjs');require('./chunk-WCQK3JOR.cjs');function b(r={}){let{defaultResponse:e={output:"mock response",totalTokens:10},responses:t={},recordCalls:n=true,onRun:o}=r,s=[],a=new Map(Object.entries(t)),i=e;return {run:async(l,c,g)=>{o?.(l,c),n&&s.push({agent:l,input:c,options:g,timestamp:Date.now()});let d=a.get(l.name)??i;if(d.generate){let p=d.generate(c,l);d={...d,...p};}if(d.error)throw d.error;d.delay&&d.delay>0&&await new Promise(p=>setTimeout(p,d.delay));let f=d.messages??[];for(let p of f)g?.onMessage?.(p);let m=d.toolCalls??[];for(let p of m)g?.onToolCall?.(p);return {output:d.output,messages:f,toolCalls:m,totalTokens:d.totalTokens??10}},getCalls:()=>[...s],getCallsFor:l=>s.filter(c=>c.agent.name===l),clearCalls:()=>s.length=0,setResponse:(l,c)=>a.set(l,c),setDefaultResponse:l=>i=l}}async function T(r,e,t){let n={agentName:"test-agent",input:"",...e},o={agentName:e.agentName??"test-agent",input:e.input??"",facts:t?.facts??{},...t},s=Date.now(),a=await r(n,o),i=Date.now()-s;return {...a,duration:i,testedData:n,assertPassed(){if(!a.passed)throw new Error(`Expected guardrail to pass, but it failed: ${a.reason}`)},assertFailed(u){if(a.passed)throw new Error("Expected guardrail to fail, but it passed");if(u!==void 0){if(typeof u=="string"&&!a.reason?.includes(u))throw new Error(`Expected failure reason to include "${u}", got: ${a.reason}`);if(u instanceof RegExp&&!u.test(a.reason??""))throw new Error(`Expected failure reason to match ${u}, got: ${a.reason}`)}},assertTransformed(u){if(a.transformed===void 0)throw new Error("Expected guardrail to transform input, but no transformation occurred");if(u!==void 0&&a.transformed!==u)throw new Error(`Expected transformation to be ${JSON.stringify(u)}, got: ${JSON.stringify(a.transformed)}`)}}}async function x(r,e){let t=[],n=[];for(let o=0;o<e.length;o++){let s=e[o],a=await T(r,s.input,s.context);t.push(a),(a.passed?a.transformed!==void 0?"transform":"pass":"fail")!==s.expect&&n.push({index:o,expected:s.expect,actual:a});}return {results:t,allPassed:()=>n.length===0,failures:()=>n}}function R(r={}){let{autoApprove:e,autoReject:t,delay:n=0,recordRequests:o=true}=r,s=[],a=new Map,i=[];return {async handle(u){o&&s.push(u);for(let l=i.length-1;l>=0;l--){let c=i[l];c.predicate(u)&&(i.splice(l,1),c.resolve(u));}return n>0&&await new Promise(l=>setTimeout(l,n)),t&&t(u)?"rejected":e?.(u)?"approved":new Promise(l=>{a.set(u.id,{request:u,resolve:l});})},getRequests:()=>[...s],clearRequests:()=>s.length=0,approve(u){let l=a.get(u);l&&(l.resolve("approved"),a.delete(u));},reject(u,l){let c=a.get(u);if(c){c.resolve("rejected"),a.delete(u);let g=s.find(d=>d.id===u);g&&(g.rejectionReason=l);}},waitForRequest(u,l=5e3){let c=s.find(u);return c?Promise.resolve(c):new Promise((g,d)=>{let f={predicate:u,resolve:p=>{clearTimeout(m),g(p);}},m=setTimeout(()=>{let p=i.indexOf(f);p>=0&&i.splice(p,1),d(new Error("Timeout waiting for approval request"));},l);i.push(f);})}}}function C(r={}){let{mockResponses:e,defaultMockResponse:t,...n}=r,o=b({responses:e,defaultResponse:t}),s=R(),a=chunk2C3R77ZW_cjs.a({...n,runner:o.run,onApprovalRequest:u=>{s.handle(u),n.onApprovalRequest?.(u);}}),i=Object.create(a);return i.mockRunner=o,i.approvalSimulator=s,i.getCalls=()=>o.getCalls(),i.getApprovalRequests=()=>s.getRequests(),i.resetAll=()=>{a.reset(),o.clearCalls(),s.clearRequests();},i}function O(){let r=[];return {plugin:{name:"constraint-recorder",onRequirementCreated(e){r.push({constraintId:e.constraintId,triggered:true,requirement:e.requirement,facts:e.facts,timestamp:Date.now()});}},getSnapshots:()=>[...r],clearSnapshots:()=>r.length=0}}function M(r,e){let t=r.facts;if(e.agentStatus!==void 0&&t.agent.status!==e.agentStatus)throw new Error(`Expected agent status to be "${e.agentStatus}", got "${t.agent.status}"`);if(e.tokenUsage!==void 0){let{min:n,max:o,exact:s}=e.tokenUsage;if(s!==void 0&&t.agent.tokenUsage!==s)throw new Error(`Expected token usage to be exactly ${s}, got ${t.agent.tokenUsage}`);if(n!==void 0&&t.agent.tokenUsage<n)throw new Error(`Expected token usage to be at least ${n}, got ${t.agent.tokenUsage}`);if(o!==void 0&&t.agent.tokenUsage>o)throw new Error(`Expected token usage to be at most ${o}, got ${t.agent.tokenUsage}`)}if(e.pendingApprovals!==void 0&&t.approval.pending.length!==e.pendingApprovals)throw new Error(`Expected ${e.pendingApprovals} pending approvals, got ${t.approval.pending.length}`);if(e.conversationLength!==void 0){let{min:n,max:o,exact:s}=e.conversationLength,a=t.conversation.length;if(s!==void 0&&a!==s)throw new Error(`Expected conversation length to be exactly ${s}, got ${a}`);if(n!==void 0&&a<n)throw new Error(`Expected conversation length to be at least ${n}, got ${a}`);if(o!==void 0&&a>o)throw new Error(`Expected conversation length to be at most ${o}, got ${a}`)}}function q(r=Date.now()){let e=r,t=r;return {now:()=>e,advance:n=>{e+=n;},set:n=>{e=n;},reset:()=>{e=t;}}}function D(r){let{mockResponses:e={},defaultMockResponse:t,mockTasks:n,...o}=r;if(n){let c=o.tasks??{};for(let[g,d]of Object.entries(n))c[g]||(c[g]=y(d.output,{delay:d.delay,shouldError:d.shouldError}));o.tasks=c;}let s={};for(let[c,g]of Object.entries(e)){let d=o.agents[c];d&&(s[d.agent.name]=g);}let a=b({responses:s,defaultResponse:t}),i=R(),u=chunk7EJN6TYX_cjs.ra({...o,runner:a.run,onApprovalRequest:c=>{i.handle(c),o.onApprovalRequest?.(c);}}),l=Object.create(u);return l.mockRunner=a,l.approvalSimulator=i,l.getCalls=()=>a.getCalls(),l.getApprovalRequests=()=>i.getRequests(),l.resetAll=()=>{u.reset(),a.clearCalls(),i.clearRequests();},l}function N(r,e){if(e.taskStatus)for(let[t,n]of Object.entries(e.taskStatus)){let o=r.getTaskState(t);if(!o)throw new Error(`Expected task "${t}" to exist, but it was not found`);if(o.status!==n)throw new Error(`Expected task "${t}" status to be "${n}", got "${o.status}"`)}if(e.agentStatus)for(let[t,n]of Object.entries(e.agentStatus)){let o=r.getAgentState(t);if(!o)throw new Error(`Expected agent "${t}" to exist, but it was not found`);if(o.status!==n)throw new Error(`Expected agent "${t}" status to be "${n}", got "${o.status}"`)}if(e.totalTokens){let{agentId:t,min:n,max:o}=e.totalTokens;if(t){let s=r.getAgentState(t);if(!s)throw new Error(`Expected agent "${t}" to exist, but it was not found`);if(n!==void 0&&s.totalTokens<n)throw new Error(`Expected agent "${t}" tokens to be at least ${n}, got ${s.totalTokens}`);if(o!==void 0&&s.totalTokens>o)throw new Error(`Expected agent "${t}" tokens to be at most ${o}, got ${s.totalTokens}`)}else {let s=r.getAllAgentStates(),a=Object.values(s).reduce((i,u)=>i+u.totalTokens,0);if(n!==void 0&&a<n)throw new Error(`Expected total tokens to be at least ${n}, got ${a}`);if(o!==void 0&&a>o)throw new Error(`Expected total tokens to be at most ${o}, got ${a}`)}}if(e.globalTokens){let{min:t,max:n}=e.globalTokens,o=r.totalTokens;if(t!==void 0&&o<t)throw new Error(`Expected global tokens to be at least ${t}, got ${o}`);if(n!==void 0&&o>n)throw new Error(`Expected global tokens to be at most ${n}, got ${o}`)}if(e.pendingHandoffs!==void 0){let t=r.getPendingHandoffs().length;if(t!==e.pendingHandoffs)throw new Error(`Expected ${e.pendingHandoffs} pending handoffs, got ${t}`)}}function y(r,e){return {run:async(t,n)=>{if(n.aborted)throw new Error("Task aborted");if(e?.delay&&e.delay>0&&await new Promise((o,s)=>{let a=setTimeout(o,e.delay);n.addEventListener("abort",()=>{clearTimeout(a),s(new Error("Task aborted"));},{once:true});}),e?.shouldError)throw new Error("Mock task error");return r},label:e?.label??"Mock Task",description:e?.description}}function P(r,e,t){return chunk7EJN6TYX_cjs.W(r,e??(o=>o.outputs),t)}function I(r,e){if(e.nodeStatuses)for(let[t,n]of Object.entries(e.nodeStatuses)){let o=r.statuses[t];if(o!==n)throw new Error(`Expected node "${t}" status to be "${n}", got "${o}"`)}if(e.completedNodes){for(let t of e.completedNodes)if(r.statuses[t]!=="completed")throw new Error(`Expected node "${t}" to be completed, got "${r.statuses[t]}"`)}if(e.skippedNodes){for(let t of e.skippedNodes)if(r.statuses[t]!=="skipped")throw new Error(`Expected node "${t}" to be skipped, got "${r.statuses[t]}"`)}if(e.errorNodes){for(let t of e.errorNodes)if(r.statuses[t]!=="error")throw new Error(`Expected node "${t}" to be error, got "${r.statuses[t]}"`)}if(e.outputContains)for(let[t,n]of Object.entries(e.outputContains)){let o=r.outputs[t];if(o!==n)throw new Error(`Expected node "${t}" output to be ${JSON.stringify(n)}, got ${JSON.stringify(o)}`)}}function B(r,e){let t=chunkWOFIBIPW_cjs.a({maxEvents:e?.maxEvents??500});if(r)for(let n of r)t.record({timestamp:Date.now(),snapshotId:null,agentId:"",...n});return t}function F(r,e){let t=r.getEvents();if(e.totalEvents!==void 0&&t.length!==e.totalEvents)throw new Error(`Expected ${e.totalEvents} timeline events, got ${t.length}`);if(e.minEvents!==void 0&&t.length<e.minEvents)throw new Error(`Expected at least ${e.minEvents} timeline events, got ${t.length}`);if(e.maxEvents!==void 0&&t.length>e.maxEvents)throw new Error(`Expected at most ${e.maxEvents} timeline events, got ${t.length}`);if(e.eventTypes){for(let n of e.eventTypes)if(!t.some(s=>s.type===n))throw new Error(`Expected timeline to contain event of type "${n}", but none found`)}if(e.agentEvents)for(let[n,o]of Object.entries(e.agentEvents)){let s=r.getEventsForAgent(n).length;if(s!==o)throw new Error(`Expected ${o} events for agent "${n}", got ${s}`)}if(e.hasType&&!t.some(o=>o.type===e.hasType))throw new Error(`Expected timeline to contain event of type "${e.hasType}"`);if(e.doesNotHaveType&&t.some(o=>o.type===e.doesNotHaveType))throw new Error(`Expected timeline NOT to contain event of type "${e.doesNotHaveType}"`)}function H(r,e){let t=0,n=e?.failAfter??0;return async(o,s,a)=>{if(t++,n>0&&t<=n)return {output:"success",messages:[],toolCalls:[],totalTokens:10};throw e?.delay&&e.delay>0&&await new Promise(i=>setTimeout(i,e.delay)),r??new Error("Runner failed")}}function L(r,e){if(r.length===0)throw new Error("Expected at least one reroute event, but none occurred");if(e.minReroutes!==void 0&&r.length<e.minReroutes)throw new Error(`Expected at least ${e.minReroutes} reroute events, got ${r.length}`);if(e.fromAgent&&!r.some(n=>n.originalAgent===e.fromAgent))throw new Error(`Expected reroute from agent "${e.fromAgent}", but no matching event found`);if(e.toAgent&&!r.some(n=>n.reroutedTo===e.toAgent))throw new Error(`Expected reroute to agent "${e.toAgent}", but no matching event found`);if(e.reason&&!r.some(n=>typeof e.reason=="string"?n.reason.includes(e.reason):e.reason.test(n.reason)))throw new Error(`Expected reroute reason matching ${e.reason}, but no matching event found`)}function J(r,e,t){let n=r.getMetrics(e);if(t.minScore!==void 0&&n.healthScore<t.minScore)throw new Error(`Expected agent "${e}" health score to be at least ${t.minScore}, got ${n.healthScore}`);if(t.maxScore!==void 0&&n.healthScore>t.maxScore)throw new Error(`Expected agent "${e}" health score to be at most ${t.maxScore}, got ${n.healthScore}`);if(t.circuitState!==void 0&&n.circuitState!==t.circuitState)throw new Error(`Expected agent "${e}" circuit state to be "${t.circuitState}", got "${n.circuitState}"`);if(t.minSuccessRate!==void 0&&n.successRate<t.minSuccessRate)throw new Error(`Expected agent "${e}" success rate to be at least ${t.minSuccessRate}, got ${n.successRate}`)}function U(r=100){let e=new chunk7EJN6TYX_cjs.y({maxCheckpoints:r}),t=[];return {inner:e,saved:t,getLatest:()=>t[t.length-1],async save(n){return t.push(n),e.save(n)},load:n=>e.load(n),list:()=>e.list(),delete:n=>e.delete(n),clear:()=>e.clear(),prune:()=>e.prune()}}function _(r,e){if(e.orchestratorType!==void 0&&r.orchestratorType!==e.orchestratorType)throw new Error(`Expected checkpoint orchestratorType "${e.orchestratorType}", got "${r.orchestratorType}"`);if(e.hasSystemExport===true&&(!r.systemExport||r.systemExport===""))throw new Error("Expected checkpoint to have non-empty systemExport, but it was empty");if(e.hasTimeline===true&&r.timelineExport===null)throw new Error("Expected checkpoint to have timeline export, but it was null");if(e.hasTimeline===false&&r.timelineExport!==null)throw new Error("Expected checkpoint to have no timeline export, but it was present");if(e.hasMemory===true&&r.memoryExport===null)throw new Error("Expected checkpoint to have memory export, but it was null");if(e.hasMemory===false&&r.memoryExport!==null)throw new Error("Expected checkpoint to have no memory export, but it was present");if(e.label!==void 0&&r.label!==e.label)throw new Error(`Expected checkpoint label "${e.label}", got "${r.label}"`)}function V(r={}){let e=[],t=null,n=false;return {handler:s=>{if(e.push(s),!n)throw new Error("[Directive] BreakpointSimulator: handler called but attachTo() was never called. Breakpoints will not be resolved. Call simulator.attachTo(orchestrator) after creation.");let a=r.autoResumeDelay??0,i=()=>{t&&(r.cancel?t.cancelBreakpoint(s.id,r.cancelReason):t.resumeBreakpoint(s.id,r.modifications));};a>0?setTimeout(i,a):queueMicrotask(i);},hits:e,attachTo:s=>{t=s,n=true;}}}function W(r,e){if(e.count!==void 0&&r.length!==e.count)throw new Error(`Expected ${e.count} breakpoint hits, got ${r.length}`);if(e.type&&!r.some(n=>n.type===e.type)){let n=r.map(o=>o.type).join(", ");throw new Error(`Expected breakpoint of type "${e.type}", found types: [${n}]`)}if(e.agentId&&!r.some(n=>n.agentId===e.agentId)){let n=r.map(o=>o.agentId).join(", ");throw new Error(`Expected breakpoint for agent "${e.agentId}", found agents: [${n}]`)}}function z(r,e){return {safeParse:t=>r(t)?{success:true,data:t}:{success:false,error:{message:"Validation failed"}},description:e}}async function K(r){let e=[];for await(let t of r)e.push(t);return e}function Q(r,e){if(e.minChunks!==void 0&&r.length<e.minChunks)throw new Error(`Expected at least ${e.minChunks} chunks, got ${r.length}`);if(e.agentIds){let t=new Set(r.map(n=>n.agentId));for(let n of e.agentIds)if(!t.has(n))throw new Error(`Expected chunks from agent "${n}", found agents: [${[...t].join(", ")}]`)}if(e.hasDone===true&&!r.some(n=>n.chunk.type==="done"))throw new Error("Expected at least one 'done' chunk, found none");if(e.hasErrors===true&&!r.some(n=>n.chunk.type==="error"))throw new Error("Expected at least one 'error' chunk, found none");if(e.hasErrors===false&&r.some(n=>n.chunk.type==="error"))throw new Error("Expected no 'error' chunks, but found some")}function X(r){let e=r?.passAfter??1,t=r?.feedback??"Needs improvement",n=r?.score,o=0;return (s,a)=>{o++;let i=o>=e;return {passed:i,feedback:i?void 0:t,score:n??(i?1:.5)}}}function Y(r,e){let t=r.getAll();for(let[n,o]of Object.entries(e)){let s=t[n];if(s!==o)throw new Error(`Expected scratchpad key "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}}function Z(r,e){let t=r.derived;for(let[n,o]of Object.entries(e)){let s=t[n];if(typeof o=="object"&&o!==null){let a=i=>JSON.stringify(i,Object.keys(i).sort());if(a(s)!==a(o))throw new Error(`Expected derived value "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}else if(s!==o)throw new Error(`Expected derived value "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}}exports.assertAgentHealth=J;exports.assertBreakpointHit=W;exports.assertCheckpoint=_;exports.assertDagExecution=I;exports.assertDerivedValues=Z;exports.assertMultiAgentState=N;exports.assertMultiplexedStream=Q;exports.assertOrchestratorState=M;exports.assertRerouted=L;exports.assertScratchpadState=Y;exports.assertTimelineEvents=F;exports.collectMultiplexedStream=K;exports.createApprovalSimulator=R;exports.createBreakpointSimulator=V;exports.createConstraintRecorder=O;exports.createFailingRunner=H;exports.createMockAgentRunner=b;exports.createMockSchema=z;exports.createMockTask=y;exports.createTestCheckpointStore=U;exports.createTestDag=P;exports.createTestMultiAgentOrchestrator=D;exports.createTestOrchestrator=C;exports.createTestReflectionEvaluator=X;exports.createTestTimeline=B;exports.createTimeController=q;exports.testGuardrail=T;exports.testGuardrailBatch=x;//# sourceMappingURL=testing.cjs.map
1
+ 'use strict';var chunkTH2D22JP_cjs=require('./chunk-TH2D22JP.cjs'),chunk7EJN6TYX_cjs=require('./chunk-7EJN6TYX.cjs');require('./chunk-K64WKZ22.cjs');var chunkWOFIBIPW_cjs=require('./chunk-WOFIBIPW.cjs');require('./chunk-WCQK3JOR.cjs');function b(r={}){let{defaultResponse:e={output:"mock response",totalTokens:10},responses:t={},recordCalls:n=true,onRun:o}=r,s=[],a=new Map(Object.entries(t)),i=e;return {run:async(l,c,g)=>{o?.(l,c),n&&s.push({agent:l,input:c,options:g,timestamp:Date.now()});let d=a.get(l.name)??i;if(d.generate){let p=d.generate(c,l);d={...d,...p};}if(d.error)throw d.error;d.delay&&d.delay>0&&await new Promise(p=>setTimeout(p,d.delay));let f=d.messages??[];for(let p of f)g?.onMessage?.(p);let m=d.toolCalls??[];for(let p of m)g?.onToolCall?.(p);return {output:d.output,messages:f,toolCalls:m,totalTokens:d.totalTokens??10}},getCalls:()=>[...s],getCallsFor:l=>s.filter(c=>c.agent.name===l),clearCalls:()=>s.length=0,setResponse:(l,c)=>a.set(l,c),setDefaultResponse:l=>i=l}}async function T(r,e,t){let n={agentName:"test-agent",input:"",...e},o={agentName:e.agentName??"test-agent",input:e.input??"",facts:t?.facts??{},...t},s=Date.now(),a=await r(n,o),i=Date.now()-s;return {...a,duration:i,testedData:n,assertPassed(){if(!a.passed)throw new Error(`Expected guardrail to pass, but it failed: ${a.reason}`)},assertFailed(u){if(a.passed)throw new Error("Expected guardrail to fail, but it passed");if(u!==void 0){if(typeof u=="string"&&!a.reason?.includes(u))throw new Error(`Expected failure reason to include "${u}", got: ${a.reason}`);if(u instanceof RegExp&&!u.test(a.reason??""))throw new Error(`Expected failure reason to match ${u}, got: ${a.reason}`)}},assertTransformed(u){if(a.transformed===void 0)throw new Error("Expected guardrail to transform input, but no transformation occurred");if(u!==void 0&&a.transformed!==u)throw new Error(`Expected transformation to be ${JSON.stringify(u)}, got: ${JSON.stringify(a.transformed)}`)}}}async function x(r,e){let t=[],n=[];for(let o=0;o<e.length;o++){let s=e[o],a=await T(r,s.input,s.context);t.push(a),(a.passed?a.transformed!==void 0?"transform":"pass":"fail")!==s.expect&&n.push({index:o,expected:s.expect,actual:a});}return {results:t,allPassed:()=>n.length===0,failures:()=>n}}function R(r={}){let{autoApprove:e,autoReject:t,delay:n=0,recordRequests:o=true}=r,s=[],a=new Map,i=[];return {async handle(u){o&&s.push(u);for(let l=i.length-1;l>=0;l--){let c=i[l];c.predicate(u)&&(i.splice(l,1),c.resolve(u));}return n>0&&await new Promise(l=>setTimeout(l,n)),t&&t(u)?"rejected":e?.(u)?"approved":new Promise(l=>{a.set(u.id,{request:u,resolve:l});})},getRequests:()=>[...s],clearRequests:()=>s.length=0,approve(u){let l=a.get(u);l&&(l.resolve("approved"),a.delete(u));},reject(u,l){let c=a.get(u);if(c){c.resolve("rejected"),a.delete(u);let g=s.find(d=>d.id===u);g&&(g.rejectionReason=l);}},waitForRequest(u,l=5e3){let c=s.find(u);return c?Promise.resolve(c):new Promise((g,d)=>{let f={predicate:u,resolve:p=>{clearTimeout(m),g(p);}},m=setTimeout(()=>{let p=i.indexOf(f);p>=0&&i.splice(p,1),d(new Error("Timeout waiting for approval request"));},l);i.push(f);})}}}function C(r={}){let{mockResponses:e,defaultMockResponse:t,...n}=r,o=b({responses:e,defaultResponse:t}),s=R(),a=chunkTH2D22JP_cjs.a({...n,runner:o.run,onApprovalRequest:u=>{s.handle(u),n.onApprovalRequest?.(u);}}),i=Object.create(a);return i.mockRunner=o,i.approvalSimulator=s,i.getCalls=()=>o.getCalls(),i.getApprovalRequests=()=>s.getRequests(),i.resetAll=()=>{a.reset(),o.clearCalls(),s.clearRequests();},i}function O(){let r=[];return {plugin:{name:"constraint-recorder",onRequirementCreated(e){r.push({constraintId:e.constraintId,triggered:true,requirement:e.requirement,facts:e.facts,timestamp:Date.now()});}},getSnapshots:()=>[...r],clearSnapshots:()=>r.length=0}}function M(r,e){let t=r.facts;if(e.agentStatus!==void 0&&t.agent.status!==e.agentStatus)throw new Error(`Expected agent status to be "${e.agentStatus}", got "${t.agent.status}"`);if(e.tokenUsage!==void 0){let{min:n,max:o,exact:s}=e.tokenUsage;if(s!==void 0&&t.agent.tokenUsage!==s)throw new Error(`Expected token usage to be exactly ${s}, got ${t.agent.tokenUsage}`);if(n!==void 0&&t.agent.tokenUsage<n)throw new Error(`Expected token usage to be at least ${n}, got ${t.agent.tokenUsage}`);if(o!==void 0&&t.agent.tokenUsage>o)throw new Error(`Expected token usage to be at most ${o}, got ${t.agent.tokenUsage}`)}if(e.pendingApprovals!==void 0&&t.approval.pending.length!==e.pendingApprovals)throw new Error(`Expected ${e.pendingApprovals} pending approvals, got ${t.approval.pending.length}`);if(e.conversationLength!==void 0){let{min:n,max:o,exact:s}=e.conversationLength,a=t.conversation.length;if(s!==void 0&&a!==s)throw new Error(`Expected conversation length to be exactly ${s}, got ${a}`);if(n!==void 0&&a<n)throw new Error(`Expected conversation length to be at least ${n}, got ${a}`);if(o!==void 0&&a>o)throw new Error(`Expected conversation length to be at most ${o}, got ${a}`)}}function q(r=Date.now()){let e=r,t=r;return {now:()=>e,advance:n=>{e+=n;},set:n=>{e=n;},reset:()=>{e=t;}}}function D(r){let{mockResponses:e={},defaultMockResponse:t,mockTasks:n,...o}=r;if(n){let c=o.tasks??{};for(let[g,d]of Object.entries(n))c[g]||(c[g]=y(d.output,{delay:d.delay,shouldError:d.shouldError}));o.tasks=c;}let s={};for(let[c,g]of Object.entries(e)){let d=o.agents[c];d&&(s[d.agent.name]=g);}let a=b({responses:s,defaultResponse:t}),i=R(),u=chunk7EJN6TYX_cjs.ra({...o,runner:a.run,onApprovalRequest:c=>{i.handle(c),o.onApprovalRequest?.(c);}}),l=Object.create(u);return l.mockRunner=a,l.approvalSimulator=i,l.getCalls=()=>a.getCalls(),l.getApprovalRequests=()=>i.getRequests(),l.resetAll=()=>{u.reset(),a.clearCalls(),i.clearRequests();},l}function N(r,e){if(e.taskStatus)for(let[t,n]of Object.entries(e.taskStatus)){let o=r.getTaskState(t);if(!o)throw new Error(`Expected task "${t}" to exist, but it was not found`);if(o.status!==n)throw new Error(`Expected task "${t}" status to be "${n}", got "${o.status}"`)}if(e.agentStatus)for(let[t,n]of Object.entries(e.agentStatus)){let o=r.getAgentState(t);if(!o)throw new Error(`Expected agent "${t}" to exist, but it was not found`);if(o.status!==n)throw new Error(`Expected agent "${t}" status to be "${n}", got "${o.status}"`)}if(e.totalTokens){let{agentId:t,min:n,max:o}=e.totalTokens;if(t){let s=r.getAgentState(t);if(!s)throw new Error(`Expected agent "${t}" to exist, but it was not found`);if(n!==void 0&&s.totalTokens<n)throw new Error(`Expected agent "${t}" tokens to be at least ${n}, got ${s.totalTokens}`);if(o!==void 0&&s.totalTokens>o)throw new Error(`Expected agent "${t}" tokens to be at most ${o}, got ${s.totalTokens}`)}else {let s=r.getAllAgentStates(),a=Object.values(s).reduce((i,u)=>i+u.totalTokens,0);if(n!==void 0&&a<n)throw new Error(`Expected total tokens to be at least ${n}, got ${a}`);if(o!==void 0&&a>o)throw new Error(`Expected total tokens to be at most ${o}, got ${a}`)}}if(e.globalTokens){let{min:t,max:n}=e.globalTokens,o=r.totalTokens;if(t!==void 0&&o<t)throw new Error(`Expected global tokens to be at least ${t}, got ${o}`);if(n!==void 0&&o>n)throw new Error(`Expected global tokens to be at most ${n}, got ${o}`)}if(e.pendingHandoffs!==void 0){let t=r.getPendingHandoffs().length;if(t!==e.pendingHandoffs)throw new Error(`Expected ${e.pendingHandoffs} pending handoffs, got ${t}`)}}function y(r,e){return {run:async(t,n)=>{if(n.aborted)throw new Error("Task aborted");if(e?.delay&&e.delay>0&&await new Promise((o,s)=>{let a=setTimeout(o,e.delay);n.addEventListener("abort",()=>{clearTimeout(a),s(new Error("Task aborted"));},{once:true});}),e?.shouldError)throw new Error("Mock task error");return r},label:e?.label??"Mock Task",description:e?.description}}function P(r,e,t){return chunk7EJN6TYX_cjs.W(r,e??(o=>o.outputs),t)}function I(r,e){if(e.nodeStatuses)for(let[t,n]of Object.entries(e.nodeStatuses)){let o=r.statuses[t];if(o!==n)throw new Error(`Expected node "${t}" status to be "${n}", got "${o}"`)}if(e.completedNodes){for(let t of e.completedNodes)if(r.statuses[t]!=="completed")throw new Error(`Expected node "${t}" to be completed, got "${r.statuses[t]}"`)}if(e.skippedNodes){for(let t of e.skippedNodes)if(r.statuses[t]!=="skipped")throw new Error(`Expected node "${t}" to be skipped, got "${r.statuses[t]}"`)}if(e.errorNodes){for(let t of e.errorNodes)if(r.statuses[t]!=="error")throw new Error(`Expected node "${t}" to be error, got "${r.statuses[t]}"`)}if(e.outputContains)for(let[t,n]of Object.entries(e.outputContains)){let o=r.outputs[t];if(o!==n)throw new Error(`Expected node "${t}" output to be ${JSON.stringify(n)}, got ${JSON.stringify(o)}`)}}function B(r,e){let t=chunkWOFIBIPW_cjs.a({maxEvents:e?.maxEvents??500});if(r)for(let n of r)t.record({timestamp:Date.now(),snapshotId:null,agentId:"",...n});return t}function F(r,e){let t=r.getEvents();if(e.totalEvents!==void 0&&t.length!==e.totalEvents)throw new Error(`Expected ${e.totalEvents} timeline events, got ${t.length}`);if(e.minEvents!==void 0&&t.length<e.minEvents)throw new Error(`Expected at least ${e.minEvents} timeline events, got ${t.length}`);if(e.maxEvents!==void 0&&t.length>e.maxEvents)throw new Error(`Expected at most ${e.maxEvents} timeline events, got ${t.length}`);if(e.eventTypes){for(let n of e.eventTypes)if(!t.some(s=>s.type===n))throw new Error(`Expected timeline to contain event of type "${n}", but none found`)}if(e.agentEvents)for(let[n,o]of Object.entries(e.agentEvents)){let s=r.getEventsForAgent(n).length;if(s!==o)throw new Error(`Expected ${o} events for agent "${n}", got ${s}`)}if(e.hasType&&!t.some(o=>o.type===e.hasType))throw new Error(`Expected timeline to contain event of type "${e.hasType}"`);if(e.doesNotHaveType&&t.some(o=>o.type===e.doesNotHaveType))throw new Error(`Expected timeline NOT to contain event of type "${e.doesNotHaveType}"`)}function H(r,e){let t=0,n=e?.failAfter??0;return async(o,s,a)=>{if(t++,n>0&&t<=n)return {output:"success",messages:[],toolCalls:[],totalTokens:10};throw e?.delay&&e.delay>0&&await new Promise(i=>setTimeout(i,e.delay)),r??new Error("Runner failed")}}function L(r,e){if(r.length===0)throw new Error("Expected at least one reroute event, but none occurred");if(e.minReroutes!==void 0&&r.length<e.minReroutes)throw new Error(`Expected at least ${e.minReroutes} reroute events, got ${r.length}`);if(e.fromAgent&&!r.some(n=>n.originalAgent===e.fromAgent))throw new Error(`Expected reroute from agent "${e.fromAgent}", but no matching event found`);if(e.toAgent&&!r.some(n=>n.reroutedTo===e.toAgent))throw new Error(`Expected reroute to agent "${e.toAgent}", but no matching event found`);if(e.reason&&!r.some(n=>typeof e.reason=="string"?n.reason.includes(e.reason):e.reason.test(n.reason)))throw new Error(`Expected reroute reason matching ${e.reason}, but no matching event found`)}function J(r,e,t){let n=r.getMetrics(e);if(t.minScore!==void 0&&n.healthScore<t.minScore)throw new Error(`Expected agent "${e}" health score to be at least ${t.minScore}, got ${n.healthScore}`);if(t.maxScore!==void 0&&n.healthScore>t.maxScore)throw new Error(`Expected agent "${e}" health score to be at most ${t.maxScore}, got ${n.healthScore}`);if(t.circuitState!==void 0&&n.circuitState!==t.circuitState)throw new Error(`Expected agent "${e}" circuit state to be "${t.circuitState}", got "${n.circuitState}"`);if(t.minSuccessRate!==void 0&&n.successRate<t.minSuccessRate)throw new Error(`Expected agent "${e}" success rate to be at least ${t.minSuccessRate}, got ${n.successRate}`)}function U(r=100){let e=new chunk7EJN6TYX_cjs.y({maxCheckpoints:r}),t=[];return {inner:e,saved:t,getLatest:()=>t[t.length-1],async save(n){return t.push(n),e.save(n)},load:n=>e.load(n),list:()=>e.list(),delete:n=>e.delete(n),clear:()=>e.clear(),prune:()=>e.prune()}}function _(r,e){if(e.orchestratorType!==void 0&&r.orchestratorType!==e.orchestratorType)throw new Error(`Expected checkpoint orchestratorType "${e.orchestratorType}", got "${r.orchestratorType}"`);if(e.hasSystemExport===true&&(!r.systemExport||r.systemExport===""))throw new Error("Expected checkpoint to have non-empty systemExport, but it was empty");if(e.hasTimeline===true&&r.timelineExport===null)throw new Error("Expected checkpoint to have timeline export, but it was null");if(e.hasTimeline===false&&r.timelineExport!==null)throw new Error("Expected checkpoint to have no timeline export, but it was present");if(e.hasMemory===true&&r.memoryExport===null)throw new Error("Expected checkpoint to have memory export, but it was null");if(e.hasMemory===false&&r.memoryExport!==null)throw new Error("Expected checkpoint to have no memory export, but it was present");if(e.label!==void 0&&r.label!==e.label)throw new Error(`Expected checkpoint label "${e.label}", got "${r.label}"`)}function V(r={}){let e=[],t=null,n=false;return {handler:s=>{if(e.push(s),!n)throw new Error("[Directive] BreakpointSimulator: handler called but attachTo() was never called. Breakpoints will not be resolved. Call simulator.attachTo(orchestrator) after creation.");let a=r.autoResumeDelay??0,i=()=>{t&&(r.cancel?t.cancelBreakpoint(s.id,r.cancelReason):t.resumeBreakpoint(s.id,r.modifications));};a>0?setTimeout(i,a):queueMicrotask(i);},hits:e,attachTo:s=>{t=s,n=true;}}}function W(r,e){if(e.count!==void 0&&r.length!==e.count)throw new Error(`Expected ${e.count} breakpoint hits, got ${r.length}`);if(e.type&&!r.some(n=>n.type===e.type)){let n=r.map(o=>o.type).join(", ");throw new Error(`Expected breakpoint of type "${e.type}", found types: [${n}]`)}if(e.agentId&&!r.some(n=>n.agentId===e.agentId)){let n=r.map(o=>o.agentId).join(", ");throw new Error(`Expected breakpoint for agent "${e.agentId}", found agents: [${n}]`)}}function z(r,e){return {safeParse:t=>r(t)?{success:true,data:t}:{success:false,error:{message:"Validation failed"}},description:e}}async function K(r){let e=[];for await(let t of r)e.push(t);return e}function Q(r,e){if(e.minChunks!==void 0&&r.length<e.minChunks)throw new Error(`Expected at least ${e.minChunks} chunks, got ${r.length}`);if(e.agentIds){let t=new Set(r.map(n=>n.agentId));for(let n of e.agentIds)if(!t.has(n))throw new Error(`Expected chunks from agent "${n}", found agents: [${[...t].join(", ")}]`)}if(e.hasDone===true&&!r.some(n=>n.chunk.type==="done"))throw new Error("Expected at least one 'done' chunk, found none");if(e.hasErrors===true&&!r.some(n=>n.chunk.type==="error"))throw new Error("Expected at least one 'error' chunk, found none");if(e.hasErrors===false&&r.some(n=>n.chunk.type==="error"))throw new Error("Expected no 'error' chunks, but found some")}function X(r){let e=r?.passAfter??1,t=r?.feedback??"Needs improvement",n=r?.score,o=0;return (s,a)=>{o++;let i=o>=e;return {passed:i,feedback:i?void 0:t,score:n??(i?1:.5)}}}function Y(r,e){let t=r.getAll();for(let[n,o]of Object.entries(e)){let s=t[n];if(s!==o)throw new Error(`Expected scratchpad key "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}}function Z(r,e){let t=r.derived;for(let[n,o]of Object.entries(e)){let s=t[n];if(typeof o=="object"&&o!==null){let a=i=>JSON.stringify(i,Object.keys(i).sort());if(a(s)!==a(o))throw new Error(`Expected derived value "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}else if(s!==o)throw new Error(`Expected derived value "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}}exports.assertAgentHealth=J;exports.assertBreakpointHit=W;exports.assertCheckpoint=_;exports.assertDagExecution=I;exports.assertDerivedValues=Z;exports.assertMultiAgentState=N;exports.assertMultiplexedStream=Q;exports.assertOrchestratorState=M;exports.assertRerouted=L;exports.assertScratchpadState=Y;exports.assertTimelineEvents=F;exports.collectMultiplexedStream=K;exports.createApprovalSimulator=R;exports.createBreakpointSimulator=V;exports.createConstraintRecorder=O;exports.createFailingRunner=H;exports.createMockAgentRunner=b;exports.createMockSchema=z;exports.createMockTask=y;exports.createTestCheckpointStore=U;exports.createTestDag=P;exports.createTestMultiAgentOrchestrator=D;exports.createTestOrchestrator=C;exports.createTestReflectionEvaluator=X;exports.createTestTimeline=B;exports.createTimeController=q;exports.testGuardrail=T;exports.testGuardrailBatch=x;//# sourceMappingURL=testing.cjs.map
2
2
  //# sourceMappingURL=testing.cjs.map
@@ -1,4 +1,4 @@
1
- import { M as MultiAgentOrchestrator, a as MultiAgentOrchestratorOptions, A as AgentOrchestrator, O as OrchestratorOptions, b as MultiplexedStreamChunk, T as TaskRegistration, R as ReflectionEvaluator } from './orchestrator-types-C-sgbBQX.cjs';
1
+ import { M as MultiAgentOrchestrator, a as MultiAgentOrchestratorOptions, A as AgentOrchestrator, O as OrchestratorOptions, b as MultiplexedStreamChunk, T as TaskRegistration, R as ReflectionEvaluator } from './orchestrator-types-DxWn77qo.cjs';
2
2
  import { A as ApprovalRequest, B as BreakpointModifications, I as InputGuardrailData, O as OutputGuardrailData, M as Message, T as ToolCallGuardrailData, a as ToolCall, G as GuardrailResult, b as AgentLike, c as AgentRunner, R as RunOptions, d as BreakpointRequest, C as Checkpoint, D as DagExecutionContext, e as DagNodeStatus, f as RerouteEvent, S as Scratchpad, g as DebugEventType, h as CheckpointStore, i as InMemoryCheckpointStore, j as DagNode, k as DagPattern, l as DebugEvent, m as GuardrailFn, n as GuardrailContext } from './types-DJ09LjZX.cjs';
3
3
  import { D as DebugTimeline } from './debug-timeline-DpnRMnLU.cjs';
4
4
  import { H as HealthMonitor } from './health-monitor-C6xoXrQz.cjs';
package/dist/testing.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { M as MultiAgentOrchestrator, a as MultiAgentOrchestratorOptions, A as AgentOrchestrator, O as OrchestratorOptions, b as MultiplexedStreamChunk, T as TaskRegistration, R as ReflectionEvaluator } from './orchestrator-types-CX1PqpKR.js';
1
+ import { M as MultiAgentOrchestrator, a as MultiAgentOrchestratorOptions, A as AgentOrchestrator, O as OrchestratorOptions, b as MultiplexedStreamChunk, T as TaskRegistration, R as ReflectionEvaluator } from './orchestrator-types-BxOJllSa.js';
2
2
  import { A as ApprovalRequest, B as BreakpointModifications, I as InputGuardrailData, O as OutputGuardrailData, M as Message, T as ToolCallGuardrailData, a as ToolCall, G as GuardrailResult, b as AgentLike, c as AgentRunner, R as RunOptions, d as BreakpointRequest, C as Checkpoint, D as DagExecutionContext, e as DagNodeStatus, f as RerouteEvent, S as Scratchpad, g as DebugEventType, h as CheckpointStore, i as InMemoryCheckpointStore, j as DagNode, k as DagPattern, l as DebugEvent, m as GuardrailFn, n as GuardrailContext } from './types-DJ09LjZX.js';
3
3
  import { D as DebugTimeline } from './debug-timeline-L13P-U2I.js';
4
4
  import { H as HealthMonitor } from './health-monitor-qL9RNMH3.js';
package/dist/testing.js CHANGED
@@ -1,2 +1,2 @@
1
- import {a}from'./chunk-ARYO7BLK.js';import {ra,W as W$1,y as y$1}from'./chunk-G7CRWJT7.js';import'./chunk-UR5BMWEN.js';import {a as a$1}from'./chunk-FBT73WFY.js';import'./chunk-HRR45JZX.js';function b(r={}){let{defaultResponse:e={output:"mock response",totalTokens:10},responses:t={},recordCalls:n=true,onRun:o}=r,s=[],a=new Map(Object.entries(t)),i=e;return {run:async(l,c,g)=>{o?.(l,c),n&&s.push({agent:l,input:c,options:g,timestamp:Date.now()});let d=a.get(l.name)??i;if(d.generate){let p=d.generate(c,l);d={...d,...p};}if(d.error)throw d.error;d.delay&&d.delay>0&&await new Promise(p=>setTimeout(p,d.delay));let f=d.messages??[];for(let p of f)g?.onMessage?.(p);let m=d.toolCalls??[];for(let p of m)g?.onToolCall?.(p);return {output:d.output,messages:f,toolCalls:m,totalTokens:d.totalTokens??10}},getCalls:()=>[...s],getCallsFor:l=>s.filter(c=>c.agent.name===l),clearCalls:()=>s.length=0,setResponse:(l,c)=>a.set(l,c),setDefaultResponse:l=>i=l}}async function T(r,e,t){let n={agentName:"test-agent",input:"",...e},o={agentName:e.agentName??"test-agent",input:e.input??"",facts:t?.facts??{},...t},s=Date.now(),a=await r(n,o),i=Date.now()-s;return {...a,duration:i,testedData:n,assertPassed(){if(!a.passed)throw new Error(`Expected guardrail to pass, but it failed: ${a.reason}`)},assertFailed(u){if(a.passed)throw new Error("Expected guardrail to fail, but it passed");if(u!==void 0){if(typeof u=="string"&&!a.reason?.includes(u))throw new Error(`Expected failure reason to include "${u}", got: ${a.reason}`);if(u instanceof RegExp&&!u.test(a.reason??""))throw new Error(`Expected failure reason to match ${u}, got: ${a.reason}`)}},assertTransformed(u){if(a.transformed===void 0)throw new Error("Expected guardrail to transform input, but no transformation occurred");if(u!==void 0&&a.transformed!==u)throw new Error(`Expected transformation to be ${JSON.stringify(u)}, got: ${JSON.stringify(a.transformed)}`)}}}async function x(r,e){let t=[],n=[];for(let o=0;o<e.length;o++){let s=e[o],a=await T(r,s.input,s.context);t.push(a),(a.passed?a.transformed!==void 0?"transform":"pass":"fail")!==s.expect&&n.push({index:o,expected:s.expect,actual:a});}return {results:t,allPassed:()=>n.length===0,failures:()=>n}}function R(r={}){let{autoApprove:e,autoReject:t,delay:n=0,recordRequests:o=true}=r,s=[],a=new Map,i=[];return {async handle(u){o&&s.push(u);for(let l=i.length-1;l>=0;l--){let c=i[l];c.predicate(u)&&(i.splice(l,1),c.resolve(u));}return n>0&&await new Promise(l=>setTimeout(l,n)),t&&t(u)?"rejected":e?.(u)?"approved":new Promise(l=>{a.set(u.id,{request:u,resolve:l});})},getRequests:()=>[...s],clearRequests:()=>s.length=0,approve(u){let l=a.get(u);l&&(l.resolve("approved"),a.delete(u));},reject(u,l){let c=a.get(u);if(c){c.resolve("rejected"),a.delete(u);let g=s.find(d=>d.id===u);g&&(g.rejectionReason=l);}},waitForRequest(u,l=5e3){let c=s.find(u);return c?Promise.resolve(c):new Promise((g,d)=>{let f={predicate:u,resolve:p=>{clearTimeout(m),g(p);}},m=setTimeout(()=>{let p=i.indexOf(f);p>=0&&i.splice(p,1),d(new Error("Timeout waiting for approval request"));},l);i.push(f);})}}}function C(r={}){let{mockResponses:e,defaultMockResponse:t,...n}=r,o=b({responses:e,defaultResponse:t}),s=R(),a$1=a({...n,runner:o.run,onApprovalRequest:u=>{s.handle(u),n.onApprovalRequest?.(u);}}),i=Object.create(a$1);return i.mockRunner=o,i.approvalSimulator=s,i.getCalls=()=>o.getCalls(),i.getApprovalRequests=()=>s.getRequests(),i.resetAll=()=>{a$1.reset(),o.clearCalls(),s.clearRequests();},i}function O(){let r=[];return {plugin:{name:"constraint-recorder",onRequirementCreated(e){r.push({constraintId:e.constraintId,triggered:true,requirement:e.requirement,facts:e.facts,timestamp:Date.now()});}},getSnapshots:()=>[...r],clearSnapshots:()=>r.length=0}}function M(r,e){let t=r.facts;if(e.agentStatus!==void 0&&t.agent.status!==e.agentStatus)throw new Error(`Expected agent status to be "${e.agentStatus}", got "${t.agent.status}"`);if(e.tokenUsage!==void 0){let{min:n,max:o,exact:s}=e.tokenUsage;if(s!==void 0&&t.agent.tokenUsage!==s)throw new Error(`Expected token usage to be exactly ${s}, got ${t.agent.tokenUsage}`);if(n!==void 0&&t.agent.tokenUsage<n)throw new Error(`Expected token usage to be at least ${n}, got ${t.agent.tokenUsage}`);if(o!==void 0&&t.agent.tokenUsage>o)throw new Error(`Expected token usage to be at most ${o}, got ${t.agent.tokenUsage}`)}if(e.pendingApprovals!==void 0&&t.approval.pending.length!==e.pendingApprovals)throw new Error(`Expected ${e.pendingApprovals} pending approvals, got ${t.approval.pending.length}`);if(e.conversationLength!==void 0){let{min:n,max:o,exact:s}=e.conversationLength,a=t.conversation.length;if(s!==void 0&&a!==s)throw new Error(`Expected conversation length to be exactly ${s}, got ${a}`);if(n!==void 0&&a<n)throw new Error(`Expected conversation length to be at least ${n}, got ${a}`);if(o!==void 0&&a>o)throw new Error(`Expected conversation length to be at most ${o}, got ${a}`)}}function q(r=Date.now()){let e=r,t=r;return {now:()=>e,advance:n=>{e+=n;},set:n=>{e=n;},reset:()=>{e=t;}}}function D(r){let{mockResponses:e={},defaultMockResponse:t,mockTasks:n,...o}=r;if(n){let c=o.tasks??{};for(let[g,d]of Object.entries(n))c[g]||(c[g]=y(d.output,{delay:d.delay,shouldError:d.shouldError}));o.tasks=c;}let s={};for(let[c,g]of Object.entries(e)){let d=o.agents[c];d&&(s[d.agent.name]=g);}let a=b({responses:s,defaultResponse:t}),i=R(),u=ra({...o,runner:a.run,onApprovalRequest:c=>{i.handle(c),o.onApprovalRequest?.(c);}}),l=Object.create(u);return l.mockRunner=a,l.approvalSimulator=i,l.getCalls=()=>a.getCalls(),l.getApprovalRequests=()=>i.getRequests(),l.resetAll=()=>{u.reset(),a.clearCalls(),i.clearRequests();},l}function N(r,e){if(e.taskStatus)for(let[t,n]of Object.entries(e.taskStatus)){let o=r.getTaskState(t);if(!o)throw new Error(`Expected task "${t}" to exist, but it was not found`);if(o.status!==n)throw new Error(`Expected task "${t}" status to be "${n}", got "${o.status}"`)}if(e.agentStatus)for(let[t,n]of Object.entries(e.agentStatus)){let o=r.getAgentState(t);if(!o)throw new Error(`Expected agent "${t}" to exist, but it was not found`);if(o.status!==n)throw new Error(`Expected agent "${t}" status to be "${n}", got "${o.status}"`)}if(e.totalTokens){let{agentId:t,min:n,max:o}=e.totalTokens;if(t){let s=r.getAgentState(t);if(!s)throw new Error(`Expected agent "${t}" to exist, but it was not found`);if(n!==void 0&&s.totalTokens<n)throw new Error(`Expected agent "${t}" tokens to be at least ${n}, got ${s.totalTokens}`);if(o!==void 0&&s.totalTokens>o)throw new Error(`Expected agent "${t}" tokens to be at most ${o}, got ${s.totalTokens}`)}else {let s=r.getAllAgentStates(),a=Object.values(s).reduce((i,u)=>i+u.totalTokens,0);if(n!==void 0&&a<n)throw new Error(`Expected total tokens to be at least ${n}, got ${a}`);if(o!==void 0&&a>o)throw new Error(`Expected total tokens to be at most ${o}, got ${a}`)}}if(e.globalTokens){let{min:t,max:n}=e.globalTokens,o=r.totalTokens;if(t!==void 0&&o<t)throw new Error(`Expected global tokens to be at least ${t}, got ${o}`);if(n!==void 0&&o>n)throw new Error(`Expected global tokens to be at most ${n}, got ${o}`)}if(e.pendingHandoffs!==void 0){let t=r.getPendingHandoffs().length;if(t!==e.pendingHandoffs)throw new Error(`Expected ${e.pendingHandoffs} pending handoffs, got ${t}`)}}function y(r,e){return {run:async(t,n)=>{if(n.aborted)throw new Error("Task aborted");if(e?.delay&&e.delay>0&&await new Promise((o,s)=>{let a=setTimeout(o,e.delay);n.addEventListener("abort",()=>{clearTimeout(a),s(new Error("Task aborted"));},{once:true});}),e?.shouldError)throw new Error("Mock task error");return r},label:e?.label??"Mock Task",description:e?.description}}function P(r,e,t){return W$1(r,e??(o=>o.outputs),t)}function I(r,e){if(e.nodeStatuses)for(let[t,n]of Object.entries(e.nodeStatuses)){let o=r.statuses[t];if(o!==n)throw new Error(`Expected node "${t}" status to be "${n}", got "${o}"`)}if(e.completedNodes){for(let t of e.completedNodes)if(r.statuses[t]!=="completed")throw new Error(`Expected node "${t}" to be completed, got "${r.statuses[t]}"`)}if(e.skippedNodes){for(let t of e.skippedNodes)if(r.statuses[t]!=="skipped")throw new Error(`Expected node "${t}" to be skipped, got "${r.statuses[t]}"`)}if(e.errorNodes){for(let t of e.errorNodes)if(r.statuses[t]!=="error")throw new Error(`Expected node "${t}" to be error, got "${r.statuses[t]}"`)}if(e.outputContains)for(let[t,n]of Object.entries(e.outputContains)){let o=r.outputs[t];if(o!==n)throw new Error(`Expected node "${t}" output to be ${JSON.stringify(n)}, got ${JSON.stringify(o)}`)}}function B(r,e){let t=a$1({maxEvents:e?.maxEvents??500});if(r)for(let n of r)t.record({timestamp:Date.now(),snapshotId:null,agentId:"",...n});return t}function F(r,e){let t=r.getEvents();if(e.totalEvents!==void 0&&t.length!==e.totalEvents)throw new Error(`Expected ${e.totalEvents} timeline events, got ${t.length}`);if(e.minEvents!==void 0&&t.length<e.minEvents)throw new Error(`Expected at least ${e.minEvents} timeline events, got ${t.length}`);if(e.maxEvents!==void 0&&t.length>e.maxEvents)throw new Error(`Expected at most ${e.maxEvents} timeline events, got ${t.length}`);if(e.eventTypes){for(let n of e.eventTypes)if(!t.some(s=>s.type===n))throw new Error(`Expected timeline to contain event of type "${n}", but none found`)}if(e.agentEvents)for(let[n,o]of Object.entries(e.agentEvents)){let s=r.getEventsForAgent(n).length;if(s!==o)throw new Error(`Expected ${o} events for agent "${n}", got ${s}`)}if(e.hasType&&!t.some(o=>o.type===e.hasType))throw new Error(`Expected timeline to contain event of type "${e.hasType}"`);if(e.doesNotHaveType&&t.some(o=>o.type===e.doesNotHaveType))throw new Error(`Expected timeline NOT to contain event of type "${e.doesNotHaveType}"`)}function H(r,e){let t=0,n=e?.failAfter??0;return async(o,s,a)=>{if(t++,n>0&&t<=n)return {output:"success",messages:[],toolCalls:[],totalTokens:10};throw e?.delay&&e.delay>0&&await new Promise(i=>setTimeout(i,e.delay)),r??new Error("Runner failed")}}function L(r,e){if(r.length===0)throw new Error("Expected at least one reroute event, but none occurred");if(e.minReroutes!==void 0&&r.length<e.minReroutes)throw new Error(`Expected at least ${e.minReroutes} reroute events, got ${r.length}`);if(e.fromAgent&&!r.some(n=>n.originalAgent===e.fromAgent))throw new Error(`Expected reroute from agent "${e.fromAgent}", but no matching event found`);if(e.toAgent&&!r.some(n=>n.reroutedTo===e.toAgent))throw new Error(`Expected reroute to agent "${e.toAgent}", but no matching event found`);if(e.reason&&!r.some(n=>typeof e.reason=="string"?n.reason.includes(e.reason):e.reason.test(n.reason)))throw new Error(`Expected reroute reason matching ${e.reason}, but no matching event found`)}function J(r,e,t){let n=r.getMetrics(e);if(t.minScore!==void 0&&n.healthScore<t.minScore)throw new Error(`Expected agent "${e}" health score to be at least ${t.minScore}, got ${n.healthScore}`);if(t.maxScore!==void 0&&n.healthScore>t.maxScore)throw new Error(`Expected agent "${e}" health score to be at most ${t.maxScore}, got ${n.healthScore}`);if(t.circuitState!==void 0&&n.circuitState!==t.circuitState)throw new Error(`Expected agent "${e}" circuit state to be "${t.circuitState}", got "${n.circuitState}"`);if(t.minSuccessRate!==void 0&&n.successRate<t.minSuccessRate)throw new Error(`Expected agent "${e}" success rate to be at least ${t.minSuccessRate}, got ${n.successRate}`)}function U(r=100){let e=new y$1({maxCheckpoints:r}),t=[];return {inner:e,saved:t,getLatest:()=>t[t.length-1],async save(n){return t.push(n),e.save(n)},load:n=>e.load(n),list:()=>e.list(),delete:n=>e.delete(n),clear:()=>e.clear(),prune:()=>e.prune()}}function _(r,e){if(e.orchestratorType!==void 0&&r.orchestratorType!==e.orchestratorType)throw new Error(`Expected checkpoint orchestratorType "${e.orchestratorType}", got "${r.orchestratorType}"`);if(e.hasSystemExport===true&&(!r.systemExport||r.systemExport===""))throw new Error("Expected checkpoint to have non-empty systemExport, but it was empty");if(e.hasTimeline===true&&r.timelineExport===null)throw new Error("Expected checkpoint to have timeline export, but it was null");if(e.hasTimeline===false&&r.timelineExport!==null)throw new Error("Expected checkpoint to have no timeline export, but it was present");if(e.hasMemory===true&&r.memoryExport===null)throw new Error("Expected checkpoint to have memory export, but it was null");if(e.hasMemory===false&&r.memoryExport!==null)throw new Error("Expected checkpoint to have no memory export, but it was present");if(e.label!==void 0&&r.label!==e.label)throw new Error(`Expected checkpoint label "${e.label}", got "${r.label}"`)}function V(r={}){let e=[],t=null,n=false;return {handler:s=>{if(e.push(s),!n)throw new Error("[Directive] BreakpointSimulator: handler called but attachTo() was never called. Breakpoints will not be resolved. Call simulator.attachTo(orchestrator) after creation.");let a=r.autoResumeDelay??0,i=()=>{t&&(r.cancel?t.cancelBreakpoint(s.id,r.cancelReason):t.resumeBreakpoint(s.id,r.modifications));};a>0?setTimeout(i,a):queueMicrotask(i);},hits:e,attachTo:s=>{t=s,n=true;}}}function W(r,e){if(e.count!==void 0&&r.length!==e.count)throw new Error(`Expected ${e.count} breakpoint hits, got ${r.length}`);if(e.type&&!r.some(n=>n.type===e.type)){let n=r.map(o=>o.type).join(", ");throw new Error(`Expected breakpoint of type "${e.type}", found types: [${n}]`)}if(e.agentId&&!r.some(n=>n.agentId===e.agentId)){let n=r.map(o=>o.agentId).join(", ");throw new Error(`Expected breakpoint for agent "${e.agentId}", found agents: [${n}]`)}}function z(r,e){return {safeParse:t=>r(t)?{success:true,data:t}:{success:false,error:{message:"Validation failed"}},description:e}}async function K(r){let e=[];for await(let t of r)e.push(t);return e}function Q(r,e){if(e.minChunks!==void 0&&r.length<e.minChunks)throw new Error(`Expected at least ${e.minChunks} chunks, got ${r.length}`);if(e.agentIds){let t=new Set(r.map(n=>n.agentId));for(let n of e.agentIds)if(!t.has(n))throw new Error(`Expected chunks from agent "${n}", found agents: [${[...t].join(", ")}]`)}if(e.hasDone===true&&!r.some(n=>n.chunk.type==="done"))throw new Error("Expected at least one 'done' chunk, found none");if(e.hasErrors===true&&!r.some(n=>n.chunk.type==="error"))throw new Error("Expected at least one 'error' chunk, found none");if(e.hasErrors===false&&r.some(n=>n.chunk.type==="error"))throw new Error("Expected no 'error' chunks, but found some")}function X(r){let e=r?.passAfter??1,t=r?.feedback??"Needs improvement",n=r?.score,o=0;return (s,a)=>{o++;let i=o>=e;return {passed:i,feedback:i?void 0:t,score:n??(i?1:.5)}}}function Y(r,e){let t=r.getAll();for(let[n,o]of Object.entries(e)){let s=t[n];if(s!==o)throw new Error(`Expected scratchpad key "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}}function Z(r,e){let t=r.derived;for(let[n,o]of Object.entries(e)){let s=t[n];if(typeof o=="object"&&o!==null){let a=i=>JSON.stringify(i,Object.keys(i).sort());if(a(s)!==a(o))throw new Error(`Expected derived value "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}else if(s!==o)throw new Error(`Expected derived value "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}}export{J as assertAgentHealth,W as assertBreakpointHit,_ as assertCheckpoint,I as assertDagExecution,Z as assertDerivedValues,N as assertMultiAgentState,Q as assertMultiplexedStream,M as assertOrchestratorState,L as assertRerouted,Y as assertScratchpadState,F as assertTimelineEvents,K as collectMultiplexedStream,R as createApprovalSimulator,V as createBreakpointSimulator,O as createConstraintRecorder,H as createFailingRunner,b as createMockAgentRunner,z as createMockSchema,y as createMockTask,U as createTestCheckpointStore,P as createTestDag,D as createTestMultiAgentOrchestrator,C as createTestOrchestrator,X as createTestReflectionEvaluator,B as createTestTimeline,q as createTimeController,T as testGuardrail,x as testGuardrailBatch};//# sourceMappingURL=testing.js.map
1
+ import {a}from'./chunk-QRDBUGPO.js';import {ra,W as W$1,y as y$1}from'./chunk-G7CRWJT7.js';import'./chunk-UR5BMWEN.js';import {a as a$1}from'./chunk-FBT73WFY.js';import'./chunk-HRR45JZX.js';function b(r={}){let{defaultResponse:e={output:"mock response",totalTokens:10},responses:t={},recordCalls:n=true,onRun:o}=r,s=[],a=new Map(Object.entries(t)),i=e;return {run:async(l,c,g)=>{o?.(l,c),n&&s.push({agent:l,input:c,options:g,timestamp:Date.now()});let d=a.get(l.name)??i;if(d.generate){let p=d.generate(c,l);d={...d,...p};}if(d.error)throw d.error;d.delay&&d.delay>0&&await new Promise(p=>setTimeout(p,d.delay));let f=d.messages??[];for(let p of f)g?.onMessage?.(p);let m=d.toolCalls??[];for(let p of m)g?.onToolCall?.(p);return {output:d.output,messages:f,toolCalls:m,totalTokens:d.totalTokens??10}},getCalls:()=>[...s],getCallsFor:l=>s.filter(c=>c.agent.name===l),clearCalls:()=>s.length=0,setResponse:(l,c)=>a.set(l,c),setDefaultResponse:l=>i=l}}async function T(r,e,t){let n={agentName:"test-agent",input:"",...e},o={agentName:e.agentName??"test-agent",input:e.input??"",facts:t?.facts??{},...t},s=Date.now(),a=await r(n,o),i=Date.now()-s;return {...a,duration:i,testedData:n,assertPassed(){if(!a.passed)throw new Error(`Expected guardrail to pass, but it failed: ${a.reason}`)},assertFailed(u){if(a.passed)throw new Error("Expected guardrail to fail, but it passed");if(u!==void 0){if(typeof u=="string"&&!a.reason?.includes(u))throw new Error(`Expected failure reason to include "${u}", got: ${a.reason}`);if(u instanceof RegExp&&!u.test(a.reason??""))throw new Error(`Expected failure reason to match ${u}, got: ${a.reason}`)}},assertTransformed(u){if(a.transformed===void 0)throw new Error("Expected guardrail to transform input, but no transformation occurred");if(u!==void 0&&a.transformed!==u)throw new Error(`Expected transformation to be ${JSON.stringify(u)}, got: ${JSON.stringify(a.transformed)}`)}}}async function x(r,e){let t=[],n=[];for(let o=0;o<e.length;o++){let s=e[o],a=await T(r,s.input,s.context);t.push(a),(a.passed?a.transformed!==void 0?"transform":"pass":"fail")!==s.expect&&n.push({index:o,expected:s.expect,actual:a});}return {results:t,allPassed:()=>n.length===0,failures:()=>n}}function R(r={}){let{autoApprove:e,autoReject:t,delay:n=0,recordRequests:o=true}=r,s=[],a=new Map,i=[];return {async handle(u){o&&s.push(u);for(let l=i.length-1;l>=0;l--){let c=i[l];c.predicate(u)&&(i.splice(l,1),c.resolve(u));}return n>0&&await new Promise(l=>setTimeout(l,n)),t&&t(u)?"rejected":e?.(u)?"approved":new Promise(l=>{a.set(u.id,{request:u,resolve:l});})},getRequests:()=>[...s],clearRequests:()=>s.length=0,approve(u){let l=a.get(u);l&&(l.resolve("approved"),a.delete(u));},reject(u,l){let c=a.get(u);if(c){c.resolve("rejected"),a.delete(u);let g=s.find(d=>d.id===u);g&&(g.rejectionReason=l);}},waitForRequest(u,l=5e3){let c=s.find(u);return c?Promise.resolve(c):new Promise((g,d)=>{let f={predicate:u,resolve:p=>{clearTimeout(m),g(p);}},m=setTimeout(()=>{let p=i.indexOf(f);p>=0&&i.splice(p,1),d(new Error("Timeout waiting for approval request"));},l);i.push(f);})}}}function C(r={}){let{mockResponses:e,defaultMockResponse:t,...n}=r,o=b({responses:e,defaultResponse:t}),s=R(),a$1=a({...n,runner:o.run,onApprovalRequest:u=>{s.handle(u),n.onApprovalRequest?.(u);}}),i=Object.create(a$1);return i.mockRunner=o,i.approvalSimulator=s,i.getCalls=()=>o.getCalls(),i.getApprovalRequests=()=>s.getRequests(),i.resetAll=()=>{a$1.reset(),o.clearCalls(),s.clearRequests();},i}function O(){let r=[];return {plugin:{name:"constraint-recorder",onRequirementCreated(e){r.push({constraintId:e.constraintId,triggered:true,requirement:e.requirement,facts:e.facts,timestamp:Date.now()});}},getSnapshots:()=>[...r],clearSnapshots:()=>r.length=0}}function M(r,e){let t=r.facts;if(e.agentStatus!==void 0&&t.agent.status!==e.agentStatus)throw new Error(`Expected agent status to be "${e.agentStatus}", got "${t.agent.status}"`);if(e.tokenUsage!==void 0){let{min:n,max:o,exact:s}=e.tokenUsage;if(s!==void 0&&t.agent.tokenUsage!==s)throw new Error(`Expected token usage to be exactly ${s}, got ${t.agent.tokenUsage}`);if(n!==void 0&&t.agent.tokenUsage<n)throw new Error(`Expected token usage to be at least ${n}, got ${t.agent.tokenUsage}`);if(o!==void 0&&t.agent.tokenUsage>o)throw new Error(`Expected token usage to be at most ${o}, got ${t.agent.tokenUsage}`)}if(e.pendingApprovals!==void 0&&t.approval.pending.length!==e.pendingApprovals)throw new Error(`Expected ${e.pendingApprovals} pending approvals, got ${t.approval.pending.length}`);if(e.conversationLength!==void 0){let{min:n,max:o,exact:s}=e.conversationLength,a=t.conversation.length;if(s!==void 0&&a!==s)throw new Error(`Expected conversation length to be exactly ${s}, got ${a}`);if(n!==void 0&&a<n)throw new Error(`Expected conversation length to be at least ${n}, got ${a}`);if(o!==void 0&&a>o)throw new Error(`Expected conversation length to be at most ${o}, got ${a}`)}}function q(r=Date.now()){let e=r,t=r;return {now:()=>e,advance:n=>{e+=n;},set:n=>{e=n;},reset:()=>{e=t;}}}function D(r){let{mockResponses:e={},defaultMockResponse:t,mockTasks:n,...o}=r;if(n){let c=o.tasks??{};for(let[g,d]of Object.entries(n))c[g]||(c[g]=y(d.output,{delay:d.delay,shouldError:d.shouldError}));o.tasks=c;}let s={};for(let[c,g]of Object.entries(e)){let d=o.agents[c];d&&(s[d.agent.name]=g);}let a=b({responses:s,defaultResponse:t}),i=R(),u=ra({...o,runner:a.run,onApprovalRequest:c=>{i.handle(c),o.onApprovalRequest?.(c);}}),l=Object.create(u);return l.mockRunner=a,l.approvalSimulator=i,l.getCalls=()=>a.getCalls(),l.getApprovalRequests=()=>i.getRequests(),l.resetAll=()=>{u.reset(),a.clearCalls(),i.clearRequests();},l}function N(r,e){if(e.taskStatus)for(let[t,n]of Object.entries(e.taskStatus)){let o=r.getTaskState(t);if(!o)throw new Error(`Expected task "${t}" to exist, but it was not found`);if(o.status!==n)throw new Error(`Expected task "${t}" status to be "${n}", got "${o.status}"`)}if(e.agentStatus)for(let[t,n]of Object.entries(e.agentStatus)){let o=r.getAgentState(t);if(!o)throw new Error(`Expected agent "${t}" to exist, but it was not found`);if(o.status!==n)throw new Error(`Expected agent "${t}" status to be "${n}", got "${o.status}"`)}if(e.totalTokens){let{agentId:t,min:n,max:o}=e.totalTokens;if(t){let s=r.getAgentState(t);if(!s)throw new Error(`Expected agent "${t}" to exist, but it was not found`);if(n!==void 0&&s.totalTokens<n)throw new Error(`Expected agent "${t}" tokens to be at least ${n}, got ${s.totalTokens}`);if(o!==void 0&&s.totalTokens>o)throw new Error(`Expected agent "${t}" tokens to be at most ${o}, got ${s.totalTokens}`)}else {let s=r.getAllAgentStates(),a=Object.values(s).reduce((i,u)=>i+u.totalTokens,0);if(n!==void 0&&a<n)throw new Error(`Expected total tokens to be at least ${n}, got ${a}`);if(o!==void 0&&a>o)throw new Error(`Expected total tokens to be at most ${o}, got ${a}`)}}if(e.globalTokens){let{min:t,max:n}=e.globalTokens,o=r.totalTokens;if(t!==void 0&&o<t)throw new Error(`Expected global tokens to be at least ${t}, got ${o}`);if(n!==void 0&&o>n)throw new Error(`Expected global tokens to be at most ${n}, got ${o}`)}if(e.pendingHandoffs!==void 0){let t=r.getPendingHandoffs().length;if(t!==e.pendingHandoffs)throw new Error(`Expected ${e.pendingHandoffs} pending handoffs, got ${t}`)}}function y(r,e){return {run:async(t,n)=>{if(n.aborted)throw new Error("Task aborted");if(e?.delay&&e.delay>0&&await new Promise((o,s)=>{let a=setTimeout(o,e.delay);n.addEventListener("abort",()=>{clearTimeout(a),s(new Error("Task aborted"));},{once:true});}),e?.shouldError)throw new Error("Mock task error");return r},label:e?.label??"Mock Task",description:e?.description}}function P(r,e,t){return W$1(r,e??(o=>o.outputs),t)}function I(r,e){if(e.nodeStatuses)for(let[t,n]of Object.entries(e.nodeStatuses)){let o=r.statuses[t];if(o!==n)throw new Error(`Expected node "${t}" status to be "${n}", got "${o}"`)}if(e.completedNodes){for(let t of e.completedNodes)if(r.statuses[t]!=="completed")throw new Error(`Expected node "${t}" to be completed, got "${r.statuses[t]}"`)}if(e.skippedNodes){for(let t of e.skippedNodes)if(r.statuses[t]!=="skipped")throw new Error(`Expected node "${t}" to be skipped, got "${r.statuses[t]}"`)}if(e.errorNodes){for(let t of e.errorNodes)if(r.statuses[t]!=="error")throw new Error(`Expected node "${t}" to be error, got "${r.statuses[t]}"`)}if(e.outputContains)for(let[t,n]of Object.entries(e.outputContains)){let o=r.outputs[t];if(o!==n)throw new Error(`Expected node "${t}" output to be ${JSON.stringify(n)}, got ${JSON.stringify(o)}`)}}function B(r,e){let t=a$1({maxEvents:e?.maxEvents??500});if(r)for(let n of r)t.record({timestamp:Date.now(),snapshotId:null,agentId:"",...n});return t}function F(r,e){let t=r.getEvents();if(e.totalEvents!==void 0&&t.length!==e.totalEvents)throw new Error(`Expected ${e.totalEvents} timeline events, got ${t.length}`);if(e.minEvents!==void 0&&t.length<e.minEvents)throw new Error(`Expected at least ${e.minEvents} timeline events, got ${t.length}`);if(e.maxEvents!==void 0&&t.length>e.maxEvents)throw new Error(`Expected at most ${e.maxEvents} timeline events, got ${t.length}`);if(e.eventTypes){for(let n of e.eventTypes)if(!t.some(s=>s.type===n))throw new Error(`Expected timeline to contain event of type "${n}", but none found`)}if(e.agentEvents)for(let[n,o]of Object.entries(e.agentEvents)){let s=r.getEventsForAgent(n).length;if(s!==o)throw new Error(`Expected ${o} events for agent "${n}", got ${s}`)}if(e.hasType&&!t.some(o=>o.type===e.hasType))throw new Error(`Expected timeline to contain event of type "${e.hasType}"`);if(e.doesNotHaveType&&t.some(o=>o.type===e.doesNotHaveType))throw new Error(`Expected timeline NOT to contain event of type "${e.doesNotHaveType}"`)}function H(r,e){let t=0,n=e?.failAfter??0;return async(o,s,a)=>{if(t++,n>0&&t<=n)return {output:"success",messages:[],toolCalls:[],totalTokens:10};throw e?.delay&&e.delay>0&&await new Promise(i=>setTimeout(i,e.delay)),r??new Error("Runner failed")}}function L(r,e){if(r.length===0)throw new Error("Expected at least one reroute event, but none occurred");if(e.minReroutes!==void 0&&r.length<e.minReroutes)throw new Error(`Expected at least ${e.minReroutes} reroute events, got ${r.length}`);if(e.fromAgent&&!r.some(n=>n.originalAgent===e.fromAgent))throw new Error(`Expected reroute from agent "${e.fromAgent}", but no matching event found`);if(e.toAgent&&!r.some(n=>n.reroutedTo===e.toAgent))throw new Error(`Expected reroute to agent "${e.toAgent}", but no matching event found`);if(e.reason&&!r.some(n=>typeof e.reason=="string"?n.reason.includes(e.reason):e.reason.test(n.reason)))throw new Error(`Expected reroute reason matching ${e.reason}, but no matching event found`)}function J(r,e,t){let n=r.getMetrics(e);if(t.minScore!==void 0&&n.healthScore<t.minScore)throw new Error(`Expected agent "${e}" health score to be at least ${t.minScore}, got ${n.healthScore}`);if(t.maxScore!==void 0&&n.healthScore>t.maxScore)throw new Error(`Expected agent "${e}" health score to be at most ${t.maxScore}, got ${n.healthScore}`);if(t.circuitState!==void 0&&n.circuitState!==t.circuitState)throw new Error(`Expected agent "${e}" circuit state to be "${t.circuitState}", got "${n.circuitState}"`);if(t.minSuccessRate!==void 0&&n.successRate<t.minSuccessRate)throw new Error(`Expected agent "${e}" success rate to be at least ${t.minSuccessRate}, got ${n.successRate}`)}function U(r=100){let e=new y$1({maxCheckpoints:r}),t=[];return {inner:e,saved:t,getLatest:()=>t[t.length-1],async save(n){return t.push(n),e.save(n)},load:n=>e.load(n),list:()=>e.list(),delete:n=>e.delete(n),clear:()=>e.clear(),prune:()=>e.prune()}}function _(r,e){if(e.orchestratorType!==void 0&&r.orchestratorType!==e.orchestratorType)throw new Error(`Expected checkpoint orchestratorType "${e.orchestratorType}", got "${r.orchestratorType}"`);if(e.hasSystemExport===true&&(!r.systemExport||r.systemExport===""))throw new Error("Expected checkpoint to have non-empty systemExport, but it was empty");if(e.hasTimeline===true&&r.timelineExport===null)throw new Error("Expected checkpoint to have timeline export, but it was null");if(e.hasTimeline===false&&r.timelineExport!==null)throw new Error("Expected checkpoint to have no timeline export, but it was present");if(e.hasMemory===true&&r.memoryExport===null)throw new Error("Expected checkpoint to have memory export, but it was null");if(e.hasMemory===false&&r.memoryExport!==null)throw new Error("Expected checkpoint to have no memory export, but it was present");if(e.label!==void 0&&r.label!==e.label)throw new Error(`Expected checkpoint label "${e.label}", got "${r.label}"`)}function V(r={}){let e=[],t=null,n=false;return {handler:s=>{if(e.push(s),!n)throw new Error("[Directive] BreakpointSimulator: handler called but attachTo() was never called. Breakpoints will not be resolved. Call simulator.attachTo(orchestrator) after creation.");let a=r.autoResumeDelay??0,i=()=>{t&&(r.cancel?t.cancelBreakpoint(s.id,r.cancelReason):t.resumeBreakpoint(s.id,r.modifications));};a>0?setTimeout(i,a):queueMicrotask(i);},hits:e,attachTo:s=>{t=s,n=true;}}}function W(r,e){if(e.count!==void 0&&r.length!==e.count)throw new Error(`Expected ${e.count} breakpoint hits, got ${r.length}`);if(e.type&&!r.some(n=>n.type===e.type)){let n=r.map(o=>o.type).join(", ");throw new Error(`Expected breakpoint of type "${e.type}", found types: [${n}]`)}if(e.agentId&&!r.some(n=>n.agentId===e.agentId)){let n=r.map(o=>o.agentId).join(", ");throw new Error(`Expected breakpoint for agent "${e.agentId}", found agents: [${n}]`)}}function z(r,e){return {safeParse:t=>r(t)?{success:true,data:t}:{success:false,error:{message:"Validation failed"}},description:e}}async function K(r){let e=[];for await(let t of r)e.push(t);return e}function Q(r,e){if(e.minChunks!==void 0&&r.length<e.minChunks)throw new Error(`Expected at least ${e.minChunks} chunks, got ${r.length}`);if(e.agentIds){let t=new Set(r.map(n=>n.agentId));for(let n of e.agentIds)if(!t.has(n))throw new Error(`Expected chunks from agent "${n}", found agents: [${[...t].join(", ")}]`)}if(e.hasDone===true&&!r.some(n=>n.chunk.type==="done"))throw new Error("Expected at least one 'done' chunk, found none");if(e.hasErrors===true&&!r.some(n=>n.chunk.type==="error"))throw new Error("Expected at least one 'error' chunk, found none");if(e.hasErrors===false&&r.some(n=>n.chunk.type==="error"))throw new Error("Expected no 'error' chunks, but found some")}function X(r){let e=r?.passAfter??1,t=r?.feedback??"Needs improvement",n=r?.score,o=0;return (s,a)=>{o++;let i=o>=e;return {passed:i,feedback:i?void 0:t,score:n??(i?1:.5)}}}function Y(r,e){let t=r.getAll();for(let[n,o]of Object.entries(e)){let s=t[n];if(s!==o)throw new Error(`Expected scratchpad key "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}}function Z(r,e){let t=r.derived;for(let[n,o]of Object.entries(e)){let s=t[n];if(typeof o=="object"&&o!==null){let a=i=>JSON.stringify(i,Object.keys(i).sort());if(a(s)!==a(o))throw new Error(`Expected derived value "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}else if(s!==o)throw new Error(`Expected derived value "${n}" to be ${JSON.stringify(o)}, got ${JSON.stringify(s)}`)}}export{J as assertAgentHealth,W as assertBreakpointHit,_ as assertCheckpoint,I as assertDagExecution,Z as assertDerivedValues,N as assertMultiAgentState,Q as assertMultiplexedStream,M as assertOrchestratorState,L as assertRerouted,Y as assertScratchpadState,F as assertTimelineEvents,K as collectMultiplexedStream,R as createApprovalSimulator,V as createBreakpointSimulator,O as createConstraintRecorder,H as createFailingRunner,b as createMockAgentRunner,z as createMockSchema,y as createMockTask,U as createTestCheckpointStore,P as createTestDag,D as createTestMultiAgentOrchestrator,C as createTestOrchestrator,X as createTestReflectionEvaluator,B as createTestTimeline,q as createTimeController,T as testGuardrail,x as testGuardrailBatch};//# sourceMappingURL=testing.js.map
2
2
  //# sourceMappingURL=testing.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@directive-run/ai",
3
- "version": "1.20.2",
3
+ "version": "1.21.0",
4
4
  "description": "AI guardrails and orchestration for Directive. Prompt injection, PII detection, cost tracking, multi-agent patterns.",
5
5
  "license": "(MIT OR Apache-2.0)",
6
6
  "author": "Jason Comes",
@@ -115,7 +115,7 @@
115
115
  "@types/node": "^25.2.0",
116
116
  "tsup": "^8.3.5",
117
117
  "typescript": "^5.7.2",
118
- "@directive-run/core": "1.20.2"
118
+ "@directive-run/core": "1.21.0"
119
119
  },
120
120
  "scripts": {
121
121
  "build": "tsup",
@@ -1,16 +0,0 @@
1
- 'use strict';var chunk7EJN6TYX_cjs=require('./chunk-7EJN6TYX.cjs'),chunkK64WKZ22_cjs=require('./chunk-K64WKZ22.cjs'),chunkWOFIBIPW_cjs=require('./chunk-WOFIBIPW.cjs'),chunkWCQK3JOR_cjs=require('./chunk-WCQK3JOR.cjs'),core=require('@directive-run/core'),adapterUtils=require('@directive-run/core/adapter-utils');var Ke=500,Xe=200,Ye=false;function At(ke){let{runner:ee,factsSchema:ye={},init:ve,constraints:Ve={},resolvers:Je={},guardrails:G={},onApprovalRequest:ie,autoApproveToolCalls:ue=true,maxTokenBudget:V,budgetWarningThreshold:te=.8,onBudgetWarning:be,plugins:ze=[],debug:we=false,approvalTimeoutMs:ce=3e5,agentRetry:re,hooks:Te={},memory:B,circuitBreaker:le,selfHealing:R,outputSchema:Qe,maxSchemaRetries:Ze,checkpointStore:Re,breakpoints:_,onBreakpoint:et,breakpointTimeoutMs:tt}=ke,D=typeof we=="object"?true:!!we,de=5e3;if(D&&R&&!le&&console.warn("[Directive] selfHealing config has no effect without a circuitBreaker \u2014 fallback behavior requires the circuit breaker to detect failures."),te<0||te>1)throw new Error(`[Directive Orchestrator] budgetWarningThreshold must be between 0 and 1, got ${te}`);if(!ue&&!ie)throw new Error(`[Directive] Invalid approval configuration: autoApproveToolCalls is false but no onApprovalRequest callback provided. Tool calls would wait for approval indefinitely. Either:
2
- - Set autoApproveToolCalls: true to auto-approve all tool calls
3
- - Provide an onApprovalRequest callback to handle approvals programmatically`);function q(e,r){try{Te[e]?.(r);}catch(o){D&&console.debug(`[Directive] hooks.${e} threw:`,o);}}let Ae=["agent","approval","conversation","toolCalls"];for(let e of Object.keys(ye))if(Ae.includes(e))throw new Error(`[Directive] Facts schema key "${e}" conflicts with orchestrator state. Reserved keys: ${Ae.join(", ")}. Rename your fact to avoid the collision.`);let rt={facts:{...chunkK64WKZ22_cjs.i.facts,...ye,__budgetWarningFired:core.t.boolean()},derivations:{},events:{},requirements:{}},Se,t,Ce=chunk7EJN6TYX_cjs.q(Ve);V&&(Ce.__budgetLimit={priority:100,when:e=>chunk7EJN6TYX_cjs.f(e).tokenUsage>V,require:{type:"__PAUSE_BUDGET_EXCEEDED"}});let Oe=chunk7EJN6TYX_cjs.r(Je,(e,r,o,a)=>Se(e,r,o,a),()=>t.facts);Oe.__pause={requirement:adapterUtils.requirementGuard("__PAUSE_BUDGET_EXCEEDED"),resolve:async(e,r)=>{let o=chunk7EJN6TYX_cjs.f(r.facts);chunk7EJN6TYX_cjs.g(r.facts,{...o,status:"paused"});}};let y=null;D&&(y=chunkWOFIBIPW_cjs.a({getSnapshotId:()=>{try{return t.history?.currentIndex??null}catch{return null}},goToSnapshot:e=>{try{t.history?.goTo?.(e);}catch{}}}));let nt=adapterUtils.createCallbackPlugin("directive-ai-callbacks",{}),at=core.createModule("directive-ai-orchestrator",{schema:rt,init:e=>{if(chunk7EJN6TYX_cjs.g(e,{status:"idle",currentAgent:null,input:null,output:null,error:null,tokenUsage:0,turnCount:0,startedAt:null,completedAt:null}),chunk7EJN6TYX_cjs.i(e,{pending:[],approved:[],rejected:[]}),chunk7EJN6TYX_cjs.k(e,[]),chunk7EJN6TYX_cjs.m(e,[]),chunk7EJN6TYX_cjs.o(e,{pending:[],resolved:[],cancelled:[]}),adapterUtils.setBridgeFact(e,"__budgetWarningFired",false),ve){let r=chunk7EJN6TYX_cjs.p(e),o={...e,...r};ve(o);}},constraints:Ce,resolvers:Oe}),_e=[...ze,nt];D&&y&&_e.push(chunkWOFIBIPW_cjs.b(y,()=>{try{return t.history?.currentIndex??null}catch{return null}})),t=core.createSystem({module:at,plugins:_e,history:D?true:void 0}),t.start();async function De(e,r,o,a,i){if(le)try{return await le.execute(()=>Ee(e,r,o,a,i))}catch(c){if(R){if(R.fallbackRunners)for(let p of R.fallbackRunners)try{let b={originalAgent:e.name,reroutedTo:"fallback-runner",reason:c instanceof Error?c.message:String(c),timestamp:Date.now()};try{R.onReroute?.(b);}catch{}return y&&y.record({type:"reroute",timestamp:Date.now(),agentId:e.name,snapshotId:null,from:e.name,to:"fallback-runner",reason:c instanceof Error?c.message:String(c)}),await p(e,r,a)}catch{}if(R.fallbackAgent)try{let p={originalAgent:e.name,reroutedTo:R.fallbackAgent.name,reason:c instanceof Error?c.message:String(c),timestamp:Date.now()};try{R.onReroute?.(p);}catch{}return y&&y.record({type:"reroute",timestamp:Date.now(),agentId:e.name,snapshotId:null,from:e.name,to:R.fallbackAgent.name,reason:c instanceof Error?c.message:String(c)}),await ee(R.fallbackAgent,r,a)}catch{}if(R.degradation==="fallback-response"&&R.fallbackResponse!==void 0)return {output:R.fallbackResponse,messages:[],toolCalls:[],totalTokens:0}}throw c}return Ee(e,r,o,a,i)}async function Ee(e,r,o,a,i){let c=Date.now();if(B){let n=B.getContextMessages();if(n.length>0){let u=n.map(g=>`${g.role}: ${g.content}`).join(`
4
- `);e={...e,instructions:`${e.instructions??""}
5
-
6
- Conversation context:
7
- ${u}`};}}if(ke.metaContext){let n=chunk7EJN6TYX_cjs.a(t.inspect());n&&(e={...e,instructions:`${e.instructions??""}
8
-
9
- ${n}`});}if(_&&_.length>0){let n={agentId:e.name,agentName:e.name,input:r,state:t.facts.$store.toObject(),breakpointType:"pre_input_guardrails"},u=await ne("pre_input_guardrails",n,i?.signal??a?.signal);if(u?.skip)return {output:void 0,messages:[],toolCalls:[],totalTokens:0};u?.input&&(r=u.input);}let p=i?.inputGuardrails!==void 0?i.inputGuardrails:G.input??[],b=i?.outputGuardrails!==void 0?i.outputGuardrails:G.output??[],$=p.map((n,u)=>chunk7EJN6TYX_cjs.c(n,u,"input"));for(let n of $){let{name:u}=n,g={agentName:e.name,input:r,facts:t.facts.$store.toObject()},E=Date.now(),m=await chunk7EJN6TYX_cjs.d(n,{input:r,agentName:e.name},g);if(q("onGuardrailCheck",{agentId:e.name,guardrailName:u,guardrailType:"input",passed:m.passed,reason:m.reason,durationMs:Date.now()-E,timestamp:Date.now()}),!m.passed)throw new chunkK64WKZ22_cjs.a({code:"INPUT_GUARDRAIL_FAILED",message:`Input guardrail "${u}" failed: ${m.reason}`,guardrailName:u,guardrailType:"input",userMessage:m.reason??"Input validation failed",agentName:e.name,input:r});m.transformed!==void 0&&(r=m.transformed);}if(q("onAgentStart",{agentName:e.name,input:r,timestamp:c}),y&&y.record({type:"agent_start",timestamp:Date.now(),agentId:e.name,snapshotId:null,inputLength:r.length,modelId:e.model??void 0,...e.instructions?{instructions:e.instructions.slice(0,de)}:{},input:r.slice(0,de)}),t.batch(()=>{let n=chunk7EJN6TYX_cjs.f(t.facts);chunk7EJN6TYX_cjs.g(t.facts,{...n,status:"running",currentAgent:e.name,input:r,startedAt:Date.now()});}),_&&_.length>0){let n={agentId:e.name,agentName:e.name,input:r,state:t.facts.$store.toObject(),breakpointType:"pre_agent_run"},u=await ne("pre_agent_run",n,i?.signal??a?.signal);if(u?.skip)return {output:void 0,messages:[],toolCalls:[],totalTokens:0};u?.input&&(r=u.input);}let A=i?.outputSchema!==void 0?i.outputSchema:Qe,C=ee;A&&(C=chunkWCQK3JOR_cjs.b(ee,{schema:A,maxRetries:i?.maxSchemaRetries??Ze??2}));let l=await chunk7EJN6TYX_cjs.e(C,e,r,{...a,signal:a?.signal,onMessage:n=>{let g=[...chunk7EJN6TYX_cjs.j(t.facts),n];chunk7EJN6TYX_cjs.k(t.facts,g.length>Ke?g.slice(-Ke):g),a?.onMessage?.(n);},onToolCall:async n=>{let u=(G.toolCall??[]).map((m,x)=>chunk7EJN6TYX_cjs.c(m,x,"toolCall"));for(let m of u){let{name:x}=m,M={agentName:e.name,input:r,facts:t.facts.$store.toObject()},d=Date.now(),w=await chunk7EJN6TYX_cjs.d(m,{toolCall:n,agentName:e.name,input:r},M);if(q("onGuardrailCheck",{agentId:e.name,guardrailName:x,guardrailType:"toolCall",passed:w.passed,reason:w.reason,durationMs:Date.now()-d,timestamp:Date.now()}),!w.passed)throw new chunkK64WKZ22_cjs.a({code:"TOOL_CALL_GUARDRAIL_FAILED",message:`Tool call guardrail "${x}" failed: ${w.reason}`,guardrailName:x,guardrailType:"toolCall",userMessage:w.reason??"Tool call blocked",data:{toolCall:n},agentName:e.name,input:r})}if(!ue){let m=`tool-${n.id}`,x={id:m,type:"tool_call",agentName:e.name,description:`Tool call: ${n.name}`,data:n,requestedAt:Date.now()};t.batch(()=>{let M=chunk7EJN6TYX_cjs.h(t.facts);chunk7EJN6TYX_cjs.i(t.facts,{...M,pending:[...M.pending,x]});}),ie?.(x),await xe(m,i?.signal??a?.signal);}let E=[...chunk7EJN6TYX_cjs.l(t.facts),n];chunk7EJN6TYX_cjs.m(t.facts,E.length>Xe?E.slice(-Xe):E),a?.onToolCall?.(n);}},re?{...re,onRetry:(n,u,g)=>{re.onRetry?.(n,u,g),q("onAgentRetry",{agentName:e.name,input:r,attempt:n,error:u,delayMs:g,timestamp:Date.now()});}}:void 0);if(_&&_.length>0){let n={agentId:e.name,agentName:e.name,input:r,state:t.facts.$store.toObject(),breakpointType:"pre_output_guardrails"},u=await ne("pre_output_guardrails",n,i?.signal??a?.signal);if(u?.skip)return {output:void 0,messages:[],toolCalls:[],totalTokens:0};u?.input&&(r=u.input);}let j=b.map((n,u)=>chunk7EJN6TYX_cjs.c(n,u,"output"));for(let n of j){let{name:u}=n,g={agentName:e.name,input:r,facts:t.facts.$store.toObject()},E=Date.now(),m=await chunk7EJN6TYX_cjs.d(n,{output:l.output,agentName:e.name,input:r,messages:l.messages},g);if(q("onGuardrailCheck",{agentId:e.name,guardrailName:u,guardrailType:"output",passed:m.passed,reason:m.reason,durationMs:Date.now()-E,timestamp:Date.now()}),!m.passed)throw new chunkK64WKZ22_cjs.a({code:"OUTPUT_GUARDRAIL_FAILED",message:`Output guardrail "${u}" failed: ${m.reason}`,guardrailName:u,guardrailType:"output",userMessage:m.reason??"Output validation failed",agentName:e.name,input:r});m.transformed!==void 0&&(l.output=m.transformed);}let ae=false,h=0;if(t.batch(()=>{let n=chunk7EJN6TYX_cjs.f(t.facts),u=n.tokenUsage+l.totalTokens;if(chunk7EJN6TYX_cjs.g(t.facts,{...n,status:"completed",output:l.output,tokenUsage:u,turnCount:n.turnCount+l.messages.length,completedAt:Date.now()}),V&&be){h=u/V;let g=adapterUtils.getBridgeFact(t.facts,"__budgetWarningFired");h>=te&&!g&&(adapterUtils.setBridgeFact(t.facts,"__budgetWarningFired",true),ae=true);}}),ae)try{be({currentTokens:chunk7EJN6TYX_cjs.f(t.facts).tokenUsage,maxBudget:V,percentage:h});}catch(n){D&&console.debug("[Directive Orchestrator] onBudgetWarning threw:",n);}if(B&&l.messages.length>0)try{B.addMessages(l.messages);}catch(n){D&&console.debug("[Directive] Memory addMessages failed:",n);}if(q("onAgentComplete",{agentName:e.name,input:r,output:l.output,tokenUsage:l.totalTokens,durationMs:Date.now()-c,timestamp:Date.now()}),y){let n=typeof l.output=="string"?l.output:JSON.stringify(l.output);y.record({type:"agent_complete",timestamp:Date.now(),agentId:e.name,snapshotId:null,outputLength:n?.length??0,totalTokens:l.totalTokens,inputTokens:l.tokenUsage?.inputTokens??0,outputTokens:l.tokenUsage?.outputTokens??0,durationMs:Date.now()-c,modelId:e.model??void 0,output:n.slice(0,de)});}if(_&&_.length>0){let n={agentId:e.name,agentName:e.name,input:r,state:t.facts.$store.toObject(),breakpointType:"post_run"},u=await ne("post_run",n,i?.signal??a?.signal);if(u?.skip)return {output:void 0,messages:[],toolCalls:[],totalTokens:0};u?.input&&(r=u.input);}return l}Se=De;let U=new Map,J=new Map;function ot(e,r){return r?.aborted?Promise.reject(r.reason??new Error("Aborted while waiting for breakpoint")):new Promise((o,a)=>{let i=false,c=null,p=()=>{i||(i=true,c&&(clearTimeout(c),c=null),r&&r.removeEventListener("abort",b),$());},b=()=>{p(),a(r.reason??new Error(`Breakpoint wait for ${e} aborted`));};r&&r.addEventListener("abort",b,{once:true});let $=t.facts.$store.subscribe([chunkK64WKZ22_cjs.g],()=>{if(i)return;let C=chunk7EJN6TYX_cjs.n(t.facts);if(C.resolved.includes(e)){p();let l=U.get(e)??null;U.delete(e),o(l);}else if(C.cancelled.includes(e)){p(),U.delete(e);let l=J.get(e);J.delete(e),a(new Error(l?`Breakpoint ${e} was cancelled: ${l}`:`Breakpoint ${e} was cancelled`));}}),A=tt??3e5;c=setTimeout(()=>{i||(p(),U.delete(e),J.delete(e),a(new Error(`[Directive] Breakpoint timeout: ${e} not resolved within ${Math.round(A/1e3)}s`)));},A);})}async function ne(e,r,o){if(!_||_.length===0)return null;let a=chunk7EJN6TYX_cjs.u(_,e,r);if(!a)return null;let i=chunk7EJN6TYX_cjs.t(),c={id:i,type:e,agentId:r.agentId,input:r.input,label:a.label,requestedAt:Date.now()};t.batch(()=>{let b=chunk7EJN6TYX_cjs.n(t.facts);chunk7EJN6TYX_cjs.o(t.facts,{...b,pending:[...b.pending,c]});});try{et?.(c);}catch{}try{Te.onBreakpoint?.(c);}catch{}y&&y.record({type:"breakpoint_hit",timestamp:Date.now(),snapshotId:null,agentId:r.agentId,breakpointId:i,breakpointType:e,label:a.label});let p=await ot(i,o);return y&&y.record({type:"breakpoint_resumed",timestamp:Date.now(),snapshotId:null,agentId:r.agentId,breakpointId:i,modified:!!p?.input,skipped:!!p?.skip}),p}function xe(e,r){return r?.aborted?Promise.reject(r.reason??new Error("Aborted while waiting for approval")):new Promise((o,a)=>{let i=false,c=null,p=()=>{i||(i=true,c&&(clearTimeout(c),c=null),r&&r.removeEventListener("abort",b),$());},b=()=>{p(),a(r.reason??new Error(`Approval wait for ${e} aborted`));};r&&r.addEventListener("abort",b,{once:true});let $=t.facts.$store.subscribe([chunkK64WKZ22_cjs.d],()=>{if(i)return;let A=chunk7EJN6TYX_cjs.h(t.facts);if(A.approved.includes(e))p(),o();else {let C=A.rejected.find(l=>l.id===e);if(C){p();let l=C.reason?`Request ${e} rejected: ${C.reason}`:`Request ${e} rejected`;a(new Error(l));}}});c=setTimeout(()=>{if(i)return;p();let A=Math.round(ce/1e3);a(new Error(`[Directive] Approval timeout: Request ${e} not resolved within ${A}s.
10
- Solutions:
11
- 1. Handle via onApprovalRequest callback and call orchestrator.approve()/reject()
12
- 2. Set autoApproveToolCalls: true to auto-approve
13
- 3. Increase approvalTimeoutMs (current: ${ce}ms)
14
- See: https://directive.run/docs/ai/running-agents`));},ce);})}function Ie(){return {...chunk7EJN6TYX_cjs.p(t.facts)}}return {system:t,get facts(){return Ie()},get totalTokens(){return chunk7EJN6TYX_cjs.f(t.facts).tokenUsage},get timeline(){return y},async run(e,r,o){return De(e,r,Ie(),void 0,o)},runStream(e,r,o={}){let a=new AbortController,i=1e4,c=[],p=[],b=false,$=Date.now(),A=0,C=1e5,l="",j;o.signal&&(j=()=>a.abort(),o.signal.addEventListener("abort",j,{once:true}));let ae=()=>{j&&o.signal&&o.signal.removeEventListener("abort",j);},h=d=>{if(b)return;let w=p.shift();w?w(d):(c.push(d),c.length>i&&c.shift());},n=()=>{b=true,ae();for(let d of p)d(null);p.length=0;},u=null,g=o.liveContext;if(g){if(g.mode!==void 0&&!Ye&&(Ye=true,console.warn("[Directive] liveContext.mode is deprecated and has no runtime behavior \u2014 the 1.x landing ships abort-and-emit only. Remove the field; automatic re-prompt semantics will return in a follow-up RFC.")),D&&g.system===t){let H=["agent","conversation","approvalState"],s=g.keys.filter(k=>H.some(T=>k.startsWith(T)));s.length>0&&console.warn(`[Directive] liveContext watches bridge-state keys (${s.join(", ")}) on the orchestrator's own system. The orchestrator's own setAgentState / setConversation / approvalState writes will trigger interruptWhen, self-looping the run. Use a separate system for liveContext, or scope \`keys\` to user-fact namespaces.`);}let d=g.notifyOn??"interrupt-only",w=g.interruptWhen??(()=>true),O=new Set(g.keys),W=g.system.facts.$store;if(typeof W?.subscribe=="function"){let H=W.toObject?.()??{};u=W.subscribe(g.keys,()=>{if(b)return;let s=W.toObject?.()??{},k=[];for(let v of g.keys)s[v]!==H[v]&&k.push(v);if(H=s,k.length===0)return;let T=k.filter(v=>O.has(v));if(T.length===0)return;let f=false;try{f=w(s,T);}catch(v){console.error("[Directive] liveContext.interruptWhen threw \u2014 treating as non-interrupt",v),f=false;}try{g.onContextUpdate?.(T);}catch(v){console.error("[Directive] liveContext.onContextUpdate threw",v);}f?(h({type:"interrupted",reason:"liveContext.interruptWhen",partialOutput:l,changedKeys:T}),a.abort()):d==="all-changes"&&h({type:"context_updated",changedKeys:T});});}}let E=()=>{u&&(u(),u=null);},m=(async()=>{h({type:"progress",phase:"starting",message:"Running input guardrails"});try{let d=r,w=(G.input??[]).map((s,k)=>chunk7EJN6TYX_cjs.c(s,k,"input"));for(let s of w){let{name:k}=s,T={agentName:e.name,input:d,facts:t.facts.$store.toObject()},f=await chunk7EJN6TYX_cjs.d(s,{input:d,agentName:e.name},T);if(!f.passed)throw h({type:"guardrail_triggered",guardrailName:k,reason:f.reason??"Input validation failed",partialOutput:l,stopped:!0}),new chunkK64WKZ22_cjs.a({code:"INPUT_GUARDRAIL_FAILED",message:`Input guardrail "${k}" failed: ${f.reason}`,guardrailName:k,guardrailType:"input",userMessage:f.reason??"Input validation failed",agentName:e.name,input:d});f.transformed!==void 0&&(d=f.transformed);}h({type:"progress",phase:"generating",message:"Starting agent"}),t.batch(()=>{let s=chunk7EJN6TYX_cjs.f(t.facts);chunk7EJN6TYX_cjs.g(t.facts,{...s,status:"running",currentAgent:e.name,input:d,startedAt:Date.now()});});let O=await chunk7EJN6TYX_cjs.e(ee,e,d,{signal:a.signal,onMessage:s=>{let k=chunk7EJN6TYX_cjs.j(t.facts);if(chunk7EJN6TYX_cjs.k(t.facts,[...k,s]),h({type:"message",message:s}),s.role==="assistant"&&s.content){let T=Math.ceil(s.content.length/4);A+=T,l+=s.content,l.length>C&&(l=l.slice(-C)),h({type:"token",data:s.content,tokenCount:A});}},onToolCall:async s=>{h({type:"tool_start",tool:s.name,toolCallId:s.id,arguments:s.arguments});let k=(G.toolCall??[]).map((f,v)=>chunk7EJN6TYX_cjs.c(f,v,"toolCall"));for(let f of k){let{name:v}=f,oe={agentName:e.name,input:d,facts:t.facts.$store.toObject()},se=await chunk7EJN6TYX_cjs.d(f,{toolCall:s,agentName:e.name,input:d},oe);if(!se.passed)throw h({type:"guardrail_triggered",guardrailName:v,reason:se.reason??"Tool call blocked",partialOutput:l,stopped:!0}),new chunkK64WKZ22_cjs.a({code:"TOOL_CALL_GUARDRAIL_FAILED",message:`Tool call guardrail "${v}" failed: ${se.reason}`,guardrailName:v,guardrailType:"toolCall",userMessage:se.reason??"Tool call blocked",data:{toolCall:s},agentName:e.name,input:d})}if(!ue){let f=`tool-${s.id}`;h({type:"approval_required",requestId:f,toolName:s.name});let v={id:f,type:"tool_call",agentName:e.name,description:`Tool call: ${s.name}`,data:s,requestedAt:Date.now()};t.batch(()=>{let oe=chunk7EJN6TYX_cjs.h(t.facts);chunk7EJN6TYX_cjs.i(t.facts,{...oe,pending:[...oe.pending,v]});}),ie?.(v),await xe(f,a.signal),h({type:"approval_resolved",requestId:f,approved:!0});}let T=chunk7EJN6TYX_cjs.l(t.facts);chunk7EJN6TYX_cjs.m(t.facts,[...T,s]),s.result&&h({type:"tool_end",tool:s.name,toolCallId:s.id,result:s.result});}},re);h({type:"progress",phase:"finishing",message:"Running output guardrails"});let W=(G.output??[]).map((s,k)=>chunk7EJN6TYX_cjs.c(s,k,"output"));for(let s of W){let{name:k}=s,T={agentName:e.name,input:d,facts:t.facts.$store.toObject()},f=await chunk7EJN6TYX_cjs.d(s,{output:O.output,agentName:e.name,input:d,messages:O.messages},T);if(!f.passed)throw h({type:"guardrail_triggered",guardrailName:k,reason:f.reason??"Output validation failed",partialOutput:typeof O.output=="string"?O.output:"",stopped:!0}),new chunkK64WKZ22_cjs.a({code:"OUTPUT_GUARDRAIL_FAILED",message:`Output guardrail "${k}" failed: ${f.reason}`,guardrailName:k,guardrailType:"output",userMessage:f.reason??"Output validation failed",agentName:e.name,input:d});f.transformed!==void 0&&(O.output=f.transformed);}t.batch(()=>{let s=chunk7EJN6TYX_cjs.f(t.facts);chunk7EJN6TYX_cjs.g(t.facts,{...s,status:"completed",output:O.output,tokenUsage:s.tokenUsage+O.totalTokens,turnCount:s.turnCount+O.messages.length,completedAt:Date.now()});});let H=Date.now()-$;return h({type:"done",totalTokens:O.totalTokens,duration:H,droppedTokens:0}),n(),O}catch(d){throw h({type:"error",error:d instanceof Error?d:new Error(String(d))}),n(),d}})();m.catch(()=>{});let x={[Symbol.asyncIterator](){return {async next(){return c.length>0?{done:false,value:c.shift()}:b?{done:true,value:void 0}:new Promise(d=>{p.push(w=>{d(w===null?{done:true,value:void 0}:{done:false,value:w});});})}}}},M=false;return m.finally(()=>{M||E();}).catch(()=>{}),{stream:x,result:m,abort:()=>{a.abort(),E(),n();},interrupt:d=>{M=true,h({type:"interrupted",reason:d??"caller.interrupt",partialOutput:l,changedKeys:[]}),a.abort();}}},async waitForIdle(e){let r=()=>chunk7EJN6TYX_cjs.f(t.facts).status!=="running";if(r())return;let o=Date.now();for(;!r();){if(e!==void 0&&Date.now()-o>e)throw new Error("[Directive Orchestrator] waitForIdle timed out");await new Promise(a=>setTimeout(a,50));}},approve(e){t.batch(()=>{let r=chunk7EJN6TYX_cjs.h(t.facts);if(!r.pending.some(i=>i.id===e)){D&&console.debug(`[Directive] approve() ignored: no pending request "${e}"`);return}let o=200,a=[...r.approved,e];chunk7EJN6TYX_cjs.i(t.facts,{...r,pending:r.pending.filter(i=>i.id!==e),approved:a.length>o?a.slice(-o):a});});},reject(e,r){t.batch(()=>{let o=chunk7EJN6TYX_cjs.h(t.facts);if(!o.pending.some(p=>p.id===e)){D&&console.debug(`[Directive] reject() ignored: no pending request "${e}"`);return}r&&D&&console.debug(`[Directive] Request ${e} rejected: ${r}`);let a={id:e,reason:r,rejectedAt:Date.now()},i=200,c=[...o.rejected,a];chunk7EJN6TYX_cjs.i(t.facts,{...o,pending:o.pending.filter(p=>p.id!==e),rejected:c.length>i?c.slice(-i):c});});},pause(){let e=chunk7EJN6TYX_cjs.f(t.facts);chunk7EJN6TYX_cjs.g(t.facts,{...e,status:"paused"});},resume(){let e=chunk7EJN6TYX_cjs.f(t.facts);e.status==="paused"&&chunk7EJN6TYX_cjs.g(t.facts,{...e,status:e.currentAgent?"running":"idle"});},reset(){t.batch(()=>{chunk7EJN6TYX_cjs.g(t.facts,{status:"idle",currentAgent:null,input:null,output:null,error:null,tokenUsage:0,turnCount:0,startedAt:null,completedAt:null}),chunk7EJN6TYX_cjs.i(t.facts,{pending:[],approved:[],rejected:[]}),chunk7EJN6TYX_cjs.k(t.facts,[]),chunk7EJN6TYX_cjs.m(t.facts,[]),chunk7EJN6TYX_cjs.o(t.facts,{pending:[],resolved:[],cancelled:[]}),adapterUtils.setBridgeFact(t.facts,"__budgetWarningFired",false);}),U.clear(),J.clear();},async checkpoint(e){if(chunk7EJN6TYX_cjs.f(t.facts).status==="running")throw new Error("[Directive] Cannot checkpoint while agent is running");if(!t.history?.export)throw new Error("[Directive] Checkpointing requires history. Set `debug: true` in orchestrator options.");let o={version:1,id:chunk7EJN6TYX_cjs.w(),createdAt:new Date().toISOString(),label:e?.label,systemExport:t.history.export(),timelineExport:y?.export()??null,localState:{type:"single"},memoryExport:B?B.export?.()??null:null,orchestratorType:"single"};return Re&&await Re.save(o),o},restore(e,r){if(!chunk7EJN6TYX_cjs.x(e))throw new Error("[Directive] Invalid checkpoint data");if(e.orchestratorType!=="single")throw new Error("[Directive] Cannot restore multi-agent checkpoint in single-agent orchestrator");if(!t.history?.import)throw new Error("[Directive] Restoring a checkpoint requires history. Set `debug: true` in orchestrator options.");t.history.import(e.systemExport),r?.restoreTimeline!==false&&e.timelineExport&&y&&y.import(e.timelineExport),e.memoryExport!==null&&B&&B.import&&B.import(e.memoryExport);},resumeBreakpoint(e,r){r&&U.set(e,r),t.batch(()=>{let o=chunk7EJN6TYX_cjs.n(t.facts),a=[...o.resolved,e];chunk7EJN6TYX_cjs.o(t.facts,{...o,pending:o.pending.filter(i=>i.id!==e),resolved:a.length>chunk7EJN6TYX_cjs.s?a.slice(-chunk7EJN6TYX_cjs.s):a});});},cancelBreakpoint(e,r){r&&J.set(e,r),t.batch(()=>{let o=chunk7EJN6TYX_cjs.n(t.facts),a=[...o.cancelled,e];chunk7EJN6TYX_cjs.o(t.facts,{...o,pending:o.pending.filter(i=>i.id!==e),cancelled:a.length>chunk7EJN6TYX_cjs.s?a.slice(-chunk7EJN6TYX_cjs.s):a});});},getPendingBreakpoints(){return [...chunk7EJN6TYX_cjs.n(t.facts).pending]},destroy(){t.destroy();}}}
15
- exports.a=At;//# sourceMappingURL=chunk-2C3R77ZW.cjs.map
16
- //# sourceMappingURL=chunk-2C3R77ZW.cjs.map