runwarden 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -196,6 +196,20 @@ a tool behind human approval, add
196
196
  [full quickstart](examples/quickstart/README.md) walks through that flow
197
197
  end to end.
198
198
 
199
+ For input-dependent approval, provide a synchronous predicate instead:
200
+
201
+ ```ts
202
+ policies: {
203
+ requireApproval: ({ toolName, input }) =>
204
+ toolName === 'billing__refund' &&
205
+ typeof input === 'object' && input !== null &&
206
+ 'amount' in input && typeof input.amount === 'number' && input.amount > 50,
207
+ }
208
+ ```
209
+
210
+ The predicate also receives `scope`. Return `true` to require approval; a
211
+ thrown error fails the invocation.
212
+
199
213
  ## The full quickstart (from the repo)
200
214
 
201
215
  ### Requirements
@@ -243,7 +257,7 @@ real model provider), see [examples/durable](examples/durable/README.md).
243
257
  5. Every ruling lands in `policy_decisions`, a call's latest dispatch state
244
258
  lands in `tool_call_audits`, and invocation state lands in `invocations`.
245
259
  Tool audits are latest-state records, not a history of every attempt.
246
- 6. If a proposed call is listed in `requireApproval`, the invocation
260
+ 6. If a proposed call requires approval under `requireApproval`, the invocation
247
261
  parks in `awaiting_approval` instead of failing. Your app resolves it
248
262
  with `approveInvocation` or `rejectInvocation`. Approval reruns the
249
263
  whole invocation; rejection cancels it.
package/dist/index.d.ts CHANGED
@@ -36,7 +36,11 @@ interface RuntimePolicies {
36
36
  maxToolCallsPerInvocation?: number;
37
37
  maxDurationMs?: number;
38
38
  toolAllowlist?: string[] | ((scope: string) => string[] | undefined);
39
- requireApproval?: string[];
39
+ requireApproval?: string[] | ((call: {
40
+ scope: string;
41
+ toolName: string;
42
+ input: unknown;
43
+ }) => boolean);
40
44
  maxToolResultBytes?: number;
41
45
  }
42
46
  type AgentRuntimeErrorKind = 'timeout' | 'failure';
package/dist/index.js CHANGED
@@ -468,7 +468,7 @@ var PolicyEngine = class {
468
468
  reason: "The correction must call the same tool with corrected arguments."
469
469
  };
470
470
  }
471
- const requiresApproval = this.policies.requireApproval?.includes(input.toolName);
471
+ const requiresApproval = this.isApprovalRequired(input.scope, input.toolName, input.toolInput);
472
472
  const matchesApproval = this.approvedToolCall !== void 0 && isSameToolCall(this.approvedToolCall, input.toolName, input.toolInput);
473
473
  if (requiresApproval && !matchesApproval) {
474
474
  this.pendingApproval ??= {
@@ -519,6 +519,17 @@ var PolicyEngine = class {
519
519
  afterStep(toolCalls) {
520
520
  if (this.terminal) return;
521
521
  if (this.pendingCorrection) {
522
+ if (toolCalls.length === 0) {
523
+ if (this.pendingCorrection.correctionsUsed >= this.correctionBudget) this.exhaustCorrection(toolCalls);
524
+ else {
525
+ this.pendingCorrection.correctionsUsed += 1;
526
+ this.record({
527
+ rule: this.pendingCorrection.kind === "multi-call" ? "exactly_one" : "input_validation",
528
+ decision: "correction_issued"
529
+ });
530
+ }
531
+ return;
532
+ }
522
533
  const only = toolCalls.length === 1 ? toolCalls[0] : void 0;
523
534
  if (only && !only.invalid) {
524
535
  this.pendingCorrection = void 0;
@@ -592,6 +603,14 @@ var PolicyEngine = class {
592
603
  });
593
604
  this.terminal = new AgentRuntimeError("failure", toolCalls.length === 0 ? "Provider did not return a corrected tool call" : toolCalls.length > 1 ? "Provider returned multiple tool calls" : "Provider returned invalid tool input", toolCalls.length > 1 ? { code: "multiple_tool_calls" } : void 0);
594
605
  }
606
+ isApprovalRequired(scope, toolName, input) {
607
+ const policy = this.policies.requireApproval;
608
+ return typeof policy === "function" ? policy({
609
+ scope,
610
+ toolName,
611
+ input
612
+ }) : policy?.includes(toolName) ?? false;
613
+ }
595
614
  record(decision) {
596
615
  try {
597
616
  this.hooks.onDecision?.(decision);
@@ -696,6 +715,7 @@ var AiSdkNativeRuntime = class {
696
715
  correctionForInvalidCall: (call) => this.correctionFor(call),
697
716
  approvedToolCall: input.approvedToolCall
698
717
  });
718
+ let approvalError;
699
719
  try {
700
720
  const systemPrompt = this.options.loadSystemPrompt ? await this.options.loadSystemPrompt(input) : "";
701
721
  const contextLoadedAt = this.now();
@@ -703,7 +723,7 @@ var AiSdkNativeRuntime = class {
703
723
  const historyLoadedAt = this.now();
704
724
  const baseInstructions = [systemPrompt, this.options.policies?.exactlyOneToolCallPerStep ?? true ? TOOL_EXECUTION_CONTRACT : ""].filter((part) => part.length > 0).join("\n\n");
705
725
  const tools = this.createTools(input, startedAt, state, policy);
706
- const result = await new ToolLoopAgent({
726
+ const agent = new ToolLoopAgent({
707
727
  model: this.options.model,
708
728
  tools,
709
729
  instructions: baseInstructions,
@@ -716,21 +736,39 @@ var AiSdkNativeRuntime = class {
716
736
  onLanguageModelCallEnd: (event) => {
717
737
  state.currentProposalCount = event.content.filter((part) => part.type === "tool-call").length;
718
738
  },
719
- toolApproval: ({ toolCall }) => policy.approvalFor({
720
- toolName: toolCall.toolName,
721
- toolCallId: toolCall.toolCallId,
722
- toolInput: toolCall.input,
723
- proposalCount: state.currentProposalCount,
724
- scope: input.scope
725
- }),
739
+ toolApproval: ({ toolCall }) => {
740
+ try {
741
+ return policy.approvalFor({
742
+ toolName: toolCall.toolName,
743
+ toolCallId: toolCall.toolCallId,
744
+ toolInput: toolCall.input,
745
+ proposalCount: state.currentProposalCount,
746
+ scope: input.scope
747
+ });
748
+ } catch (error) {
749
+ approvalError = { error };
750
+ throw error;
751
+ }
752
+ },
726
753
  onStepEnd: (step) => {
727
754
  policy.afterStep(step.toolCalls);
728
755
  },
729
756
  stopWhen: () => policy.terminalError != null
730
- }).generate({
731
- messages: modelMessages(history, input),
757
+ });
758
+ const messages = modelMessages(history, input);
759
+ let result = await agent.generate({
760
+ messages,
732
761
  timeout: this.remaining(startedAt)
733
762
  });
763
+ const steps = [...result.steps];
764
+ while (!policy.terminalError && policy.correctionInstruction()) {
765
+ messages.push(...result.response.messages);
766
+ result = await agent.generate({
767
+ messages,
768
+ timeout: this.remaining(startedAt)
769
+ });
770
+ steps.push(...result.steps);
771
+ }
734
772
  if (policy.terminalError) throw policy.terminalError;
735
773
  const text = result.text.trim();
736
774
  const provider = normalizeProviderName(result.finalStep.model.provider);
@@ -745,7 +783,7 @@ var AiSdkNativeRuntime = class {
745
783
  contextLoadMs: contextLoadedAt - startedAt,
746
784
  historyLoadMs: historyLoadedAt - contextLoadedAt,
747
785
  toolCallCount: state.toolCallCount,
748
- ...aggregateStepTimings(result.steps),
786
+ ...aggregateStepTimings(steps),
749
787
  totalMs: completedAt - startedAt
750
788
  });
751
789
  return {
@@ -756,6 +794,7 @@ var AiSdkNativeRuntime = class {
756
794
  model
757
795
  };
758
796
  } catch (error) {
797
+ if (approvalError) throw approvalError.error;
759
798
  const runtimeError = findAgentRuntimeError(error);
760
799
  if (runtimeError) throw runtimeError;
761
800
  if (isTimeoutFailure(error)) throw new AgentRuntimeError("timeout", "Native runtime timed out");
package/package.json CHANGED
@@ -57,18 +57,6 @@
57
57
  "main": "./dist/index.js",
58
58
  "name": "runwarden",
59
59
  "packageManager": "pnpm@10.34.9",
60
- "pnpm": {
61
- "onlyBuiltDependencies": [
62
- "@embedded-postgres/darwin-arm64",
63
- "@embedded-postgres/darwin-x64",
64
- "@embedded-postgres/linux-arm",
65
- "@embedded-postgres/linux-arm64",
66
- "@embedded-postgres/linux-ia32",
67
- "@embedded-postgres/linux-ppc64",
68
- "@embedded-postgres/linux-x64",
69
- "@embedded-postgres/windows-x64"
70
- ]
71
- },
72
60
  "prettier": {
73
61
  "importOrder": [
74
62
  "<BUILTIN_MODULES>",
@@ -103,5 +91,5 @@
103
91
  "sideEffects": false,
104
92
  "type": "module",
105
93
  "types": "./dist/index.d.ts",
106
- "version": "0.3.0"
94
+ "version": "0.4.0"
107
95
  }