runwarden 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,23 +1,62 @@
1
- import { n as createDb, r as policyDecisions, t as initDb } from "./init-Dm_qLwix.js";
1
+ import { n as createDb, r as policyDecisions, t as initDb } from "./init-RSnueOdR.js";
2
2
  import { createHash, randomUUID } from "node:crypto";
3
3
  import { sql } from "drizzle-orm";
4
4
  import { Client } from "@modelcontextprotocol/sdk/client/index.js";
5
5
  import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
6
6
  import { ToolLoopAgent, tool } from "ai";
7
7
  import { z } from "zod";
8
+ import { calcPrice } from "@pydantic/genai-prices";
8
9
  import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
9
10
  import { hostname } from "node:os";
10
11
  // src/contracts/errors.ts
11
12
  var AgentRuntimeError = class extends Error {
12
13
  kind;
13
14
  diagnostics;
15
+ usage;
14
16
  constructor(kind, message, diagnostics = {}, options = {}) {
15
17
  super(message, options);
16
18
  this.kind = kind;
17
19
  this.diagnostics = diagnostics;
18
20
  this.name = "AgentRuntimeError";
21
+ this.usage = options.usage;
19
22
  }
20
23
  };
24
+ // src/runtime/budget.ts
25
+ function invalid(message) {
26
+ return new AgentRuntimeError("failure", message, { code: "invalid_budget_grant" });
27
+ }
28
+ function validateBudgetGrant(grant, budget) {
29
+ if (!grant) throw invalid("A budget approval requires a grant");
30
+ if (grant.maxTokens === void 0 && grant.maxCostUsd === void 0) throw invalid("A budget grant requires maxTokens or maxCostUsd");
31
+ if (grant.maxTokens !== void 0) {
32
+ if (budget.maxTokens === void 0) throw invalid("A maxTokens grant requires a configured maxTokens limit");
33
+ if (!Number.isSafeInteger(grant.maxTokens) || grant.maxTokens <= 0) throw invalid("A maxTokens grant must be a positive safe integer");
34
+ }
35
+ if (grant.maxCostUsd !== void 0) {
36
+ if (budget.maxCostUsd === void 0) throw invalid("A maxCostUsd grant requires a configured maxCostUsd limit");
37
+ if (!Number.isFinite(grant.maxCostUsd) || grant.maxCostUsd <= 0) throw invalid("A maxCostUsd grant must be a positive finite number");
38
+ }
39
+ return {
40
+ ...grant.maxTokens !== void 0 ? { maxTokens: grant.maxTokens } : {},
41
+ ...grant.maxCostUsd !== void 0 ? { maxCostUsd: grant.maxCostUsd } : {}
42
+ };
43
+ }
44
+ function accumulateBudgetGrants(current, grant) {
45
+ return {
46
+ ...current?.maxTokens !== void 0 || grant.maxTokens !== void 0 ? { maxTokens: (current?.maxTokens ?? 0) + (grant.maxTokens ?? 0) } : {},
47
+ ...current?.maxCostUsd !== void 0 || grant.maxCostUsd !== void 0 ? { maxCostUsd: (current?.maxCostUsd ?? 0) + (grant.maxCostUsd ?? 0) } : {}
48
+ };
49
+ }
50
+ function effectiveBudgetLimits(budget, grants) {
51
+ const maxTokens = budget.maxTokens === void 0 ? void 0 : budget.maxTokens + (grants?.maxTokens ?? 0);
52
+ const maxCostUsd = budget.maxCostUsd === void 0 ? void 0 : budget.maxCostUsd + (grants?.maxCostUsd ?? 0);
53
+ if (maxTokens !== void 0 && !Number.isSafeInteger(maxTokens)) throw invalid("Effective maxTokens must be a safe integer");
54
+ if (maxCostUsd !== void 0 && !Number.isFinite(maxCostUsd)) throw invalid("Effective maxCostUsd must be a finite number");
55
+ return {
56
+ ...maxTokens !== void 0 ? { maxTokens } : {},
57
+ ...maxCostUsd !== void 0 ? { maxCostUsd } : {}
58
+ };
59
+ }
21
60
  // src/db/repositories/invocations.ts
22
61
  function mapRow(raw) {
23
62
  return {
@@ -27,6 +66,14 @@ function mapRow(raw) {
27
66
  orderingKey: raw.ordering_key ?? null,
28
67
  input: raw.input,
29
68
  resultText: raw.result_text ?? null,
69
+ resultMessages: raw.result_messages ?? null,
70
+ inputTokens: raw.input_tokens == null ? null : Number(raw.input_tokens),
71
+ outputTokens: raw.output_tokens == null ? null : Number(raw.output_tokens),
72
+ cacheReadTokens: raw.cache_read_tokens == null ? null : Number(raw.cache_read_tokens),
73
+ cacheWriteTokens: raw.cache_write_tokens == null ? null : Number(raw.cache_write_tokens),
74
+ reasoningTokens: raw.reasoning_tokens == null ? null : Number(raw.reasoning_tokens),
75
+ costUsd: raw.cost_usd == null ? null : Number(raw.cost_usd),
76
+ usageSteps: raw.usage_steps ?? null,
30
77
  idempotencyKey: raw.idempotency_key ?? null,
31
78
  status: raw.status,
32
79
  attemptCount: Number(raw.attempt_count),
@@ -39,6 +86,8 @@ function mapRow(raw) {
39
86
  activeRunId: raw.active_run_id ?? null,
40
87
  activeRunRegisteredAt: raw.active_run_registered_at ?? null,
41
88
  pendingToolCall: raw.pending_tool_call ?? null,
89
+ pendingBudgetRequest: raw.pending_budget_request ?? null,
90
+ budgetGrants: raw.budget_grants ?? null,
42
91
  startedAt: raw.started_at ?? null,
43
92
  completedAt: raw.completed_at ?? null,
44
93
  failedAt: raw.failed_at ?? null,
@@ -60,14 +109,42 @@ function toPendingToolCall(raw) {
60
109
  input: raw.input
61
110
  };
62
111
  }
112
+ function toBudgetRequest(raw) {
113
+ if (typeof raw !== "object" || raw === null) return void 0;
114
+ const request = raw;
115
+ if (typeof request.requestId !== "string" || !Array.isArray(request.limits) || typeof request.usage !== "object" || request.usage === null) return;
116
+ return {
117
+ requestId: request.requestId,
118
+ limits: request.limits,
119
+ usage: request.usage
120
+ };
121
+ }
122
+ function toBudgetGrants(raw) {
123
+ if (typeof raw !== "object" || raw === null) return void 0;
124
+ const grants = raw;
125
+ if (grants.maxTokens === void 0 && grants.maxCostUsd === void 0) return;
126
+ return {
127
+ ...grants.maxTokens !== void 0 ? { maxTokens: grants.maxTokens } : {},
128
+ ...grants.maxCostUsd !== void 0 ? { maxCostUsd: grants.maxCostUsd } : {}
129
+ };
130
+ }
63
131
  function toRecord(row) {
132
+ const pendingToolCall = toPendingToolCall(row.pendingToolCall);
133
+ const pendingBudgetRequest = toBudgetRequest(row.pendingBudgetRequest);
64
134
  return {
65
135
  ...row.input,
66
136
  id: String(row.id),
67
137
  status: row.status,
68
138
  attemptCount: row.attemptCount,
69
139
  resultText: row.resultText ?? void 0,
70
- pendingToolCall: toPendingToolCall(row.pendingToolCall),
140
+ ...pendingBudgetRequest ? {
141
+ pendingApprovalKind: "budget",
142
+ pendingBudgetRequest
143
+ } : pendingToolCall ? {
144
+ pendingApprovalKind: "tool",
145
+ pendingToolCall
146
+ } : {},
147
+ budgetGrants: toBudgetGrants(row.budgetGrants),
71
148
  lastErrorCode: row.lastErrorCode ?? void 0,
72
149
  lastErrorMessage: row.lastErrorMessage ?? void 0,
73
150
  createdAt: row.createdAt,
@@ -203,9 +280,18 @@ async function markInvocationSucceeded(db, id, workerId, runId, result) {
203
280
  update agent_runtime.invocations
204
281
  set status = 'succeeded',
205
282
  result_text = ${result.resultText},
283
+ result_messages = ${result.resultMessages ? JSON.stringify(result.resultMessages) : null}::jsonb,
284
+ input_tokens = ${result.usage?.inputTokens ?? null},
285
+ output_tokens = ${result.usage?.outputTokens ?? null},
286
+ cache_read_tokens = ${result.usage?.cacheReadTokens ?? null},
287
+ cache_write_tokens = ${result.usage?.cacheWriteTokens ?? null},
288
+ reasoning_tokens = ${result.usage?.reasoningTokens ?? null},
289
+ cost_usd = ${result.usage?.costUsd ?? null},
290
+ usage_steps = ${result.usage ? JSON.stringify(result.usage.steps) : null}::jsonb,
206
291
  provider = ${result.provider ?? null},
207
292
  model = ${result.model ?? null},
208
293
  pending_tool_call = null,
294
+ pending_budget_request = null,
209
295
  completed_at = now(),
210
296
  failed_at = null,
211
297
  ${CLEAR_CLAIM},
@@ -215,30 +301,82 @@ async function markInvocationSucceeded(db, id, workerId, runId, result) {
215
301
  returning id
216
302
  `)).rows.length > 0;
217
303
  }
218
- async function markInvocationAwaitingApproval(db, id, workerId, runId, pendingToolCall) {
219
- const raw = (await db.execute(sql`
220
- update agent_runtime.invocations
221
- set status = 'awaiting_approval',
222
- pending_tool_call = ${JSON.stringify(pendingToolCall)}::jsonb,
223
- attempt_count = greatest(attempt_count - 1, 0),
224
- last_error_code = null,
225
- last_error_message = null,
226
- ${CLEAR_CLAIM},
227
- updated_at = now()
228
- where id = ${id} and claimed_by = ${workerId} and status = 'running'
229
- and active_run_id = ${runId}
230
- returning *
231
- `)).rows[0];
232
- return raw ? mapRow(raw) : void 0;
304
+ async function markInvocationAwaitingApproval(db, id, workerId, runId, park) {
305
+ return db.transaction(async (tx) => {
306
+ const raw = (park.kind === "tool" ? await tx.execute(sql`
307
+ update agent_runtime.invocations
308
+ set status = 'awaiting_approval',
309
+ pending_tool_call = ${JSON.stringify(park.toolCall)}::jsonb,
310
+ pending_budget_request = null,
311
+ attempt_count = greatest(attempt_count - 1, 0),
312
+ last_error_code = null,
313
+ last_error_message = null,
314
+ ${CLEAR_CLAIM},
315
+ updated_at = now()
316
+ where id = ${id} and claimed_by = ${workerId} and status = 'running'
317
+ and active_run_id = ${runId}
318
+ returning *
319
+ `) : await tx.execute(sql`
320
+ update agent_runtime.invocations
321
+ set status = 'awaiting_approval',
322
+ pending_budget_request = ${JSON.stringify(park.request)}::jsonb,
323
+ pending_tool_call = null,
324
+ input_tokens = ${park.request.usage.inputTokens},
325
+ output_tokens = ${park.request.usage.outputTokens},
326
+ cache_read_tokens = ${park.request.usage.cacheReadTokens},
327
+ cache_write_tokens = ${park.request.usage.cacheWriteTokens},
328
+ reasoning_tokens = ${park.request.usage.reasoningTokens},
329
+ cost_usd = ${park.request.usage.costUsd ?? null},
330
+ usage_steps = ${JSON.stringify(park.request.usage.steps)}::jsonb,
331
+ attempt_count = greatest(attempt_count - 1, 0),
332
+ last_error_code = null,
333
+ last_error_message = null,
334
+ ${CLEAR_CLAIM},
335
+ updated_at = now()
336
+ where id = ${id} and claimed_by = ${workerId} and status = 'running'
337
+ and active_run_id = ${runId}
338
+ returning *
339
+ `)).rows[0];
340
+ if (!raw) return void 0;
341
+ if (park.kind === "budget") await tx.execute(sql`
342
+ insert into agent_runtime.policy_decisions
343
+ (scope, invocation_id, run_id, tool_name, rule, decision, detail)
344
+ values (
345
+ ${raw.scope},
346
+ ${id},
347
+ ${park.request.requestId},
348
+ null,
349
+ 'budget',
350
+ 'approval_requested',
351
+ ${JSON.stringify({
352
+ event: "approval_requested",
353
+ requestId: park.request.requestId,
354
+ limits: park.request.limits,
355
+ usage: park.request.usage,
356
+ ...park.detail
357
+ })}::jsonb
358
+ )
359
+ `);
360
+ return mapRow(raw);
361
+ });
233
362
  }
234
- async function markInvocationFailed(db, id, workerId, runId, error, retryDelayMs) {
363
+ async function markInvocationFailed(db, id, workerId, runId, error, retryDelayMs, options = {}) {
364
+ const terminal = options.retryable === false;
235
365
  const raw = (await db.execute(sql`
236
366
  update agent_runtime.invocations
237
- set status = (case when attempt_count >= max_attempts then 'failed' else 'pending' end)::agent_runtime.invocation_status,
238
- next_attempt_at = now() + (${retryDelayMs} * interval '1 millisecond'),
239
- failed_at = case when attempt_count >= max_attempts then now() else failed_at end,
240
- completed_at = case when attempt_count >= max_attempts then now() else completed_at end,
241
- pending_tool_call = case when attempt_count >= max_attempts then null else pending_tool_call end,
367
+ set status = (case when ${terminal} or attempt_count >= max_attempts then 'failed' else 'pending' end)::agent_runtime.invocation_status,
368
+ next_attempt_at = case when ${terminal} or attempt_count >= max_attempts then now() else now() + (${retryDelayMs} * interval '1 millisecond') end,
369
+ failed_at = case when ${terminal} or attempt_count >= max_attempts then now() else failed_at end,
370
+ completed_at = case when ${terminal} or attempt_count >= max_attempts then now() else completed_at end,
371
+ pending_tool_call = case when ${terminal} or attempt_count >= max_attempts then null else pending_tool_call end,
372
+ pending_budget_request = case when ${terminal} or attempt_count >= max_attempts then null else pending_budget_request end,
373
+ input_tokens = ${options.usage?.inputTokens ?? null},
374
+ output_tokens = ${options.usage?.outputTokens ?? null},
375
+ cache_read_tokens = ${options.usage?.cacheReadTokens ?? null},
376
+ cache_write_tokens = ${options.usage?.cacheWriteTokens ?? null},
377
+ reasoning_tokens = ${options.usage?.reasoningTokens ?? null},
378
+ cost_usd = ${options.usage?.costUsd ?? null},
379
+ usage_steps = ${options.usage ? JSON.stringify(options.usage.steps) : null}::jsonb,
242
380
  last_error_code = ${error.code},
243
381
  last_error_message = ${error.message},
244
382
  ${CLEAR_CLAIM},
@@ -261,7 +399,11 @@ async function listAwaitingApprovalRows(db, scope) {
261
399
  order by created_at asc, id asc
262
400
  `)).rows.map(mapRow);
263
401
  }
264
- async function resolveInvocationApproval(db, id, resolution, options = {}) {
402
+ const resolvedByDetail = (options) => ({
403
+ ...options.resolvedBy ? { resolvedBy: options.resolvedBy } : {},
404
+ ...options.note ? { note: options.note } : {}
405
+ });
406
+ async function resolveInvocationApproval(db, id, resolution, options = {}, configuredBudget = {}) {
265
407
  return db.transaction(async (tx) => {
266
408
  const raw = (await tx.execute(sql`
267
409
  select * from agent_runtime.invocations where id = ${id} for update
@@ -272,11 +414,71 @@ async function resolveInvocationApproval(db, id, resolution, options = {}) {
272
414
  outcome: "not_awaiting",
273
415
  row
274
416
  };
417
+ const budgetRequest = toBudgetRequest(row.pendingBudgetRequest);
418
+ if (budgetRequest) {
419
+ if (!options.requestId || options.requestId !== budgetRequest.requestId) return {
420
+ outcome: "stale_request",
421
+ row
422
+ };
423
+ const grants = toBudgetGrants(row.budgetGrants);
424
+ const detail = {
425
+ event: resolution,
426
+ requestId: budgetRequest.requestId,
427
+ limits: budgetRequest.limits,
428
+ grants: grants ?? null,
429
+ ...resolvedByDetail(options)
430
+ };
431
+ if (resolution === "approved") {
432
+ const grant = validateBudgetGrant(options.grant, configuredBudget);
433
+ const total = accumulateBudgetGrants(grants, grant);
434
+ const effectiveLimits = effectiveBudgetLimits(configuredBudget, total);
435
+ detail["grant"] = grant;
436
+ detail["grants"] = total;
437
+ detail["effectiveLimits"] = effectiveLimits;
438
+ await insertResolutionAudit(tx, row, budgetRequest.requestId, resolution, detail);
439
+ return {
440
+ outcome: "resolved",
441
+ row: mapRow((await tx.execute(sql`
442
+ update agent_runtime.invocations
443
+ set status = 'pending',
444
+ next_attempt_at = now(),
445
+ budget_grants = ${JSON.stringify(total)}::jsonb,
446
+ pending_budget_request = null,
447
+ last_error_code = null,
448
+ last_error_message = null,
449
+ updated_at = now()
450
+ where id = ${row.id}
451
+ returning *
452
+ `)).rows[0])
453
+ };
454
+ }
455
+ detail["effectiveLimits"] = effectiveBudgetLimits(configuredBudget, grants);
456
+ await insertResolutionAudit(tx, row, budgetRequest.requestId, resolution, detail);
457
+ return {
458
+ outcome: "resolved",
459
+ row: mapRow((await tx.execute(sql`
460
+ update agent_runtime.invocations
461
+ set status = 'failed',
462
+ pending_budget_request = null,
463
+ last_error_code = 'budget_exceeded',
464
+ last_error_message = ${options.note ?? "Budget approval rejected"},
465
+ failed_at = now(),
466
+ completed_at = now(),
467
+ updated_at = now()
468
+ where id = ${row.id}
469
+ returning *
470
+ `)).rows[0])
471
+ };
472
+ }
473
+ if (options.requestId !== void 0) return {
474
+ outcome: "stale_request",
475
+ row
476
+ };
477
+ if (options.grant !== void 0) throw new AgentRuntimeError("failure", "A grant is only valid when approving a budget request", { code: "invalid_budget_grant" });
275
478
  const pending = toPendingToolCall(row.pendingToolCall);
276
479
  const detail = {
277
480
  toolInput: pending?.input ?? null,
278
- ...options.resolvedBy ? { resolvedBy: options.resolvedBy } : {},
279
- ...options.note ? { note: options.note } : {}
481
+ ...resolvedByDetail(options)
280
482
  };
281
483
  await tx.execute(sql`
282
484
  insert into agent_runtime.policy_decisions
@@ -318,6 +520,21 @@ async function resolveInvocationApproval(db, id, resolution, options = {}) {
318
520
  };
319
521
  });
320
522
  }
523
+ async function insertResolutionAudit(tx, row, requestId, decision, detail) {
524
+ await tx.execute(sql`
525
+ insert into agent_runtime.policy_decisions
526
+ (scope, invocation_id, run_id, tool_name, rule, decision, detail)
527
+ values (
528
+ ${row.scope},
529
+ ${row.id},
530
+ ${requestId},
531
+ null,
532
+ 'budget',
533
+ ${decision},
534
+ ${JSON.stringify(detail)}::jsonb
535
+ )
536
+ `);
537
+ }
321
538
  // src/brand.ts
322
539
  const RUNTIME_NAME = "runwarden";
323
540
  const RUN_ID_HEADER = "X-Runwarden-Run-Id";
@@ -395,8 +612,202 @@ function canonicalJson(value) {
395
612
  function deriveToolCallIdempotencyKey(invocationId, toolName, input) {
396
613
  return createHash("sha256").update(`invocation:${invocationId}\n${toolName}\n${canonicalJson(input)}`).digest("hex");
397
614
  }
615
+ // src/runtime/usage.ts
616
+ const EMPTY_TOKENS = {
617
+ inputTokens: 0,
618
+ outputTokens: 0,
619
+ cacheReadTokens: 0,
620
+ cacheWriteTokens: 0,
621
+ reasoningTokens: 0
622
+ };
623
+ const TOKEN_KEYS = Object.keys(EMPTY_TOKENS);
624
+ function sumTokens(steps) {
625
+ const tokens = { ...EMPTY_TOKENS };
626
+ let overflow = false;
627
+ for (const key of TOKEN_KEYS) {
628
+ let total = 0;
629
+ for (const step of steps) {
630
+ const value = step[key];
631
+ if (!Number.isSafeInteger(value) || value < 0) {
632
+ overflow = true;
633
+ continue;
634
+ }
635
+ total += value;
636
+ if (total > Number.MAX_SAFE_INTEGER) {
637
+ overflow = true;
638
+ total = Number.MAX_SAFE_INTEGER;
639
+ break;
640
+ }
641
+ }
642
+ tokens[key] = total;
643
+ }
644
+ return {
645
+ tokens,
646
+ overflow
647
+ };
648
+ }
649
+ function usageStepsOverflow(steps) {
650
+ return sumTokens(steps).overflow;
651
+ }
652
+ function emptyInvocationUsage() {
653
+ return {
654
+ ...EMPTY_TOKENS,
655
+ costIncomplete: false,
656
+ steps: []
657
+ };
658
+ }
659
+ function validMetric(value) {
660
+ return value !== void 0 && Number.isSafeInteger(value) && value >= 0 ? value : void 0;
661
+ }
662
+ function metric(value) {
663
+ return validMetric(value) ?? 0;
664
+ }
665
+ function usageValues(usage) {
666
+ return [
667
+ usage.inputTokens,
668
+ usage.outputTokens,
669
+ usage.inputTokenDetails.noCacheTokens,
670
+ usage.inputTokenDetails.cacheReadTokens,
671
+ usage.inputTokenDetails.cacheWriteTokens,
672
+ usage.outputTokenDetails.textTokens,
673
+ usage.outputTokenDetails.reasoningTokens
674
+ ];
675
+ }
676
+ function hasUsage(usage) {
677
+ return usage !== void 0 && usageValues(usage).some((value) => value !== void 0);
678
+ }
679
+ function hasInvalidUsage(usage) {
680
+ return usage !== void 0 && usageValues(usage).some((value) => value !== void 0 && validMetric(value) === void 0);
681
+ }
682
+ function tokenCounts(usage) {
683
+ if (!usage) return { ...EMPTY_TOKENS };
684
+ const cacheReadTokens = validMetric(usage.inputTokenDetails.cacheReadTokens) ?? 0;
685
+ const cacheWriteTokens = validMetric(usage.inputTokenDetails.cacheWriteTokens) ?? 0;
686
+ const reasoningTokens = validMetric(usage.outputTokenDetails.reasoningTokens) ?? 0;
687
+ const inputTotalValue = validMetric(usage.inputTokens);
688
+ const outputTotalValue = validMetric(usage.outputTokens);
689
+ return {
690
+ inputTokens: validMetric(usage.inputTokenDetails.noCacheTokens) ?? (inputTotalValue === void 0 ? 0 : Math.max(inputTotalValue - cacheReadTokens - cacheWriteTokens, 0)),
691
+ outputTokens: validMetric(usage.outputTokenDetails.textTokens) ?? (outputTotalValue === void 0 ? 0 : Math.max(outputTotalValue - reasoningTokens, 0)),
692
+ cacheReadTokens,
693
+ cacheWriteTokens,
694
+ reasoningTokens
695
+ };
696
+ }
697
+ function inputTotal(usage) {
698
+ if (!usage || hasInvalidUsage(usage)) return void 0;
699
+ const input = validMetric(usage.inputTokens);
700
+ if (input !== void 0) return input;
701
+ const details = usage.inputTokenDetails;
702
+ const noCache = validMetric(details.noCacheTokens);
703
+ const cacheRead = validMetric(details.cacheReadTokens);
704
+ const cacheWrite = validMetric(details.cacheWriteTokens);
705
+ if (noCache === void 0 || cacheRead === void 0 || cacheWrite === void 0) return;
706
+ return noCache + cacheRead + cacheWrite;
707
+ }
708
+ function outputTotal(usage) {
709
+ if (!usage || hasInvalidUsage(usage)) return void 0;
710
+ const output = validMetric(usage.outputTokens);
711
+ if (output !== void 0) return output;
712
+ const details = usage.outputTokenDetails;
713
+ const text = validMetric(details.textTokens);
714
+ const reasoning = validMetric(details.reasoningTokens);
715
+ if (text === void 0 || reasoning === void 0) return void 0;
716
+ return text + reasoning;
717
+ }
718
+ const PROVIDER_ALIASES = {
719
+ "amazon-bedrock": "aws",
720
+ "google-vertex": "google",
721
+ togetherai: "together",
722
+ xai: "x-ai",
723
+ moonshot: "moonshotai",
724
+ zhipu: "zhipuai"
725
+ };
726
+ function catalogProviderId(provider) {
727
+ const base = provider.toLowerCase().split(".")[0] ?? provider.toLowerCase();
728
+ return PROVIDER_ALIASES[base] ?? base;
729
+ }
730
+ function overrideProvider(override) {
731
+ return {
732
+ api_pattern: "",
733
+ id: "runwarden-override",
734
+ name: "Runwarden price override",
735
+ models: [{
736
+ id: override.model,
737
+ match: { equals: override.model },
738
+ prices: {
739
+ input_mtok: override.inputPerMillionTokens,
740
+ output_mtok: override.outputPerMillionTokens,
741
+ ...override.cacheReadPerMillionTokens !== void 0 ? { cache_read_mtok: override.cacheReadPerMillionTokens } : {},
742
+ ...override.cacheWritePerMillionTokens !== void 0 ? { cache_write_mtok: override.cacheWritePerMillionTokens } : {}
743
+ }
744
+ }]
745
+ };
746
+ }
747
+ function priceStep(source, pricing) {
748
+ const input = inputTotal(source.usage);
749
+ const output = outputTotal(source.usage);
750
+ if (input === void 0 || output === void 0) return void 0;
751
+ const override = pricing?.overrides?.find((candidate) => candidate.model.toLowerCase() === source.model.toLowerCase() && (!candidate.provider || candidate.provider.toLowerCase() === source.provider?.toLowerCase()));
752
+ const providerId = pricing?.providerId ?? (source.provider ? catalogProviderId(source.provider) : void 0);
753
+ try {
754
+ const total = calcPrice({
755
+ input_tokens: input,
756
+ output_tokens: output,
757
+ cache_read_tokens: metric(source.usage?.inputTokenDetails.cacheReadTokens),
758
+ cache_write_tokens: metric(source.usage?.inputTokenDetails.cacheWriteTokens)
759
+ }, source.model, override ? { provider: overrideProvider(override) } : providerId ? { providerId } : void 0)?.total_price;
760
+ return total !== void 0 && Number.isFinite(total) && total >= 0 && total < 0xde0b6b3a7640000 ? total : void 0;
761
+ } catch {
762
+ return;
763
+ }
764
+ }
765
+ function buildUsageStep(source, pricing) {
766
+ const usageKnown = hasUsage(source.usage) && !hasInvalidUsage(source.usage) && inputTotal(source.usage) !== void 0 && outputTotal(source.usage) !== void 0;
767
+ const tokens = tokenCounts(source.usage);
768
+ const costUsd = usageKnown ? priceStep(source, pricing) : void 0;
769
+ return {
770
+ ...tokens,
771
+ ...source.provider ? { provider: source.provider } : {},
772
+ model: source.model,
773
+ usageKnown,
774
+ ...costUsd !== void 0 ? { costUsd } : {},
775
+ costKnown: costUsd !== void 0
776
+ };
777
+ }
778
+ function aggregateUsageSteps(steps) {
779
+ const knownCosts = steps.flatMap((step) => step.costUsd === void 0 ? [] : [step.costUsd]);
780
+ const knownCostTotal = knownCosts.length > 0 ? knownCosts.reduce((total, cost) => total + cost, 0) : void 0;
781
+ const costUsd = knownCostTotal !== void 0 && Number.isFinite(knownCostTotal) && knownCostTotal < 0xde0b6b3a7640000 ? knownCostTotal : void 0;
782
+ const { tokens: totals, overflow: tokensIncomplete } = sumTokens(steps);
783
+ return {
784
+ ...totals,
785
+ ...costUsd !== void 0 ? { costUsd } : {},
786
+ costIncomplete: tokensIncomplete || steps.some((step) => !step.costKnown) || knownCosts.length > 0 && costUsd === void 0,
787
+ steps: [...steps]
788
+ };
789
+ }
398
790
  // src/runtime/policy-engine.ts
399
791
  const TOOL_PROPOSAL_CORRECTION = "Return exactly one tool call for the next required step. Wait for its result before proposing another tool.";
792
+ const BUDGET_TOKEN_FIELDS = [
793
+ "inputTokens",
794
+ "outputTokens",
795
+ "cacheReadTokens",
796
+ "cacheWriteTokens",
797
+ "reasoningTokens"
798
+ ];
799
+ function validateBudget(budget, grants) {
800
+ if (!budget) return void 0;
801
+ const onExceeded = budget.onExceeded ?? "stop";
802
+ if (onExceeded !== "stop" && onExceeded !== "log" && onExceeded !== "approve") throw new Error("budget.onExceeded must be 'stop', 'log', or 'approve'");
803
+ if (budget.maxCostUsd === void 0 && budget.maxTokens === void 0) throw new Error("budget requires maxCostUsd or maxTokens");
804
+ if (budget.maxCostUsd !== void 0 && (!Number.isFinite(budget.maxCostUsd) || budget.maxCostUsd <= 0)) throw new Error("budget.maxCostUsd must be a positive finite number");
805
+ if (budget.maxTokens !== void 0 && (!Number.isSafeInteger(budget.maxTokens) || budget.maxTokens <= 0)) throw new Error("budget.maxTokens must be a positive integer");
806
+ return {
807
+ ...effectiveBudgetLimits(budget, grants === void 0 ? void 0 : validateBudgetGrant(grants, budget)),
808
+ onExceeded
809
+ };
810
+ }
400
811
  function isSameToolCall(approved, toolName, input) {
401
812
  return approved.toolName === toolName && canonicalJson(approved.input) === canonicalJson(input);
402
813
  }
@@ -406,6 +817,22 @@ var PolicyEngine = class {
406
817
  exactlyOne;
407
818
  correctionBudget;
408
819
  maxToolCallsPerInvocation;
820
+ budget;
821
+ budgetParkable;
822
+ budgetGrants;
823
+ budgetWarned = {
824
+ tokens: false,
825
+ costUsd: false
826
+ };
827
+ budgetExceeded = {
828
+ tokens: false,
829
+ costUsd: false
830
+ };
831
+ budgetExceededDetails = {};
832
+ budgetIncomplete = {
833
+ tokens: false,
834
+ costUsd: false
835
+ };
409
836
  pendingCorrection;
410
837
  terminal;
411
838
  approvedToolCalls = 0;
@@ -417,6 +844,9 @@ var PolicyEngine = class {
417
844
  this.exactlyOne = policies.exactlyOneToolCallPerStep ?? true;
418
845
  this.correctionBudget = policies.correctionBudget ?? 1;
419
846
  this.maxToolCallsPerInvocation = policies.maxToolCallsPerInvocation ?? 8;
847
+ this.budgetGrants = hooks.budgetGrants;
848
+ this.budgetParkable = hooks.budgetParkable ?? false;
849
+ this.budget = validateBudget(policies.budget, hooks.budgetGrants);
420
850
  this.approvedToolCall = hooks.approvedToolCall;
421
851
  for (const [name, value] of [["correctionBudget", this.correctionBudget], ["maxToolCallsPerInvocation", this.maxToolCallsPerInvocation]]) if (!Number.isSafeInteger(value) || value < (name === "correctionBudget" ? 0 : 1)) throw new Error(`${name} must be a ${name === "correctionBudget" ? "non-negative" : "positive"} integer`);
422
852
  }
@@ -547,6 +977,147 @@ var PolicyEngine = class {
547
977
  }
548
978
  this.handleViolation(toolCalls, 0);
549
979
  }
980
+ evaluateBudget(usage, stepNumber, canContinue) {
981
+ const budget = this.budget;
982
+ if (!budget) return;
983
+ const existingTerminal = this.terminal && this.terminal.diagnostics.code !== "audit_persistence_failed" ? this.terminal : void 0;
984
+ const terminalOnThisStep = existingTerminal?.diagnostics.code ?? (canContinue ? void 0 : "final_step");
985
+ const tokenTotal = BUDGET_TOKEN_FIELDS.reduce((total, key) => total + BigInt(usage[key]), 0n);
986
+ const accumulatedTokens = tokenTotal > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(tokenTotal);
987
+ const tokenCompleteness = usage.steps.every((step) => step.usageKnown) && !usageStepsOverflow(usage.steps) ? "complete" : "lower_bound";
988
+ const warn = (limitKind, accumulated, limit, completeness) => {
989
+ if (this.budgetWarned[limitKind]) return;
990
+ this.budgetWarned[limitKind] = true;
991
+ const event = {
992
+ limitKind,
993
+ accumulated,
994
+ limit,
995
+ threshold: .8,
996
+ stepNumber,
997
+ completeness
998
+ };
999
+ this.record({
1000
+ rule: "budget",
1001
+ decision: "warning",
1002
+ detail: {
1003
+ event: "warning",
1004
+ ...event,
1005
+ onExceeded: budget.onExceeded
1006
+ }
1007
+ });
1008
+ try {
1009
+ this.hooks.onBudgetWarning?.(event);
1010
+ } catch {}
1011
+ };
1012
+ if (budget.maxTokens !== void 0) {
1013
+ const limit = BigInt(budget.maxTokens);
1014
+ if (tokenTotal * 5n >= limit * 4n) warn("tokens", accumulatedTokens, budget.maxTokens, tokenCompleteness);
1015
+ if (tokenTotal >= limit) {
1016
+ if (!this.budgetExceeded.tokens) {
1017
+ this.budgetExceeded.tokens = true;
1018
+ const detail = {
1019
+ limitKind: "tokens",
1020
+ accumulated: accumulatedTokens,
1021
+ limit: budget.maxTokens,
1022
+ stepNumber,
1023
+ completeness: tokenCompleteness
1024
+ };
1025
+ this.budgetExceededDetails.tokens = detail;
1026
+ this.record({
1027
+ rule: "budget",
1028
+ decision: "exceeded",
1029
+ detail: {
1030
+ event: "exceeded",
1031
+ ...detail,
1032
+ ...terminalOnThisStep ? { terminal: terminalOnThisStep } : {}
1033
+ }
1034
+ });
1035
+ }
1036
+ } else if (tokenCompleteness === "lower_bound" && !this.budgetIncomplete.tokens) {
1037
+ this.budgetIncomplete.tokens = true;
1038
+ this.record({
1039
+ rule: "budget",
1040
+ decision: "incomplete",
1041
+ detail: {
1042
+ event: "incomplete",
1043
+ limitKind: "tokens",
1044
+ accumulated: accumulatedTokens,
1045
+ limit: budget.maxTokens,
1046
+ stepNumber,
1047
+ onExceeded: budget.onExceeded,
1048
+ completeness: tokenCompleteness,
1049
+ reason: "usage_incomplete"
1050
+ }
1051
+ });
1052
+ }
1053
+ }
1054
+ if (budget.maxCostUsd !== void 0) {
1055
+ const cost = usage.costUsd;
1056
+ if (cost !== void 0 && Number.isFinite(cost) && !usage.costIncomplete) {
1057
+ if (cost >= budget.maxCostUsd * .8) warn("costUsd", cost, budget.maxCostUsd, "complete");
1058
+ if (cost >= budget.maxCostUsd) {
1059
+ if (!this.budgetExceeded.costUsd) {
1060
+ this.budgetExceeded.costUsd = true;
1061
+ const detail = {
1062
+ limitKind: "costUsd",
1063
+ accumulated: cost,
1064
+ limit: budget.maxCostUsd,
1065
+ stepNumber,
1066
+ completeness: "complete"
1067
+ };
1068
+ this.budgetExceededDetails.costUsd = detail;
1069
+ this.record({
1070
+ rule: "budget",
1071
+ decision: "exceeded",
1072
+ detail: {
1073
+ event: "exceeded",
1074
+ ...detail,
1075
+ ...terminalOnThisStep ? { terminal: terminalOnThisStep } : {}
1076
+ }
1077
+ });
1078
+ }
1079
+ }
1080
+ } else if (!this.budgetIncomplete.costUsd) {
1081
+ this.budgetIncomplete.costUsd = true;
1082
+ this.record({
1083
+ rule: "budget",
1084
+ decision: "incomplete",
1085
+ detail: {
1086
+ event: "incomplete",
1087
+ limitKind: "costUsd",
1088
+ accumulated: null,
1089
+ knownCostUsd: cost ?? null,
1090
+ limit: budget.maxCostUsd,
1091
+ stepNumber,
1092
+ onExceeded: budget.onExceeded,
1093
+ completeness: "lower_bound",
1094
+ reason: "cost_unknown"
1095
+ }
1096
+ });
1097
+ }
1098
+ }
1099
+ const exceeded = Object.values(this.budgetExceededDetails).filter((detail) => detail !== void 0);
1100
+ if (exceeded.length > 0 && budget.onExceeded !== "log" && canContinue && !existingTerminal) {
1101
+ const auditFailure = this.terminal?.diagnostics.code === "audit_persistence_failed" ? this.terminal.message : void 0;
1102
+ const detail = {
1103
+ limits: exceeded,
1104
+ effectiveLimits: {
1105
+ ...budget.maxTokens !== void 0 ? { maxTokens: budget.maxTokens } : {},
1106
+ ...budget.maxCostUsd !== void 0 ? { maxCostUsd: budget.maxCostUsd } : {}
1107
+ },
1108
+ ...this.budgetGrants ? { grants: this.budgetGrants } : {},
1109
+ ...auditFailure ? { auditFailure } : {}
1110
+ };
1111
+ if (budget.onExceeded === "approve" && this.budgetParkable) this.terminal = new AgentRuntimeError("budget", "Budget approval required", {
1112
+ code: "budget_approval_required",
1113
+ ...detail
1114
+ }, { usage });
1115
+ else this.terminal = new AgentRuntimeError("budget", "Invocation budget exceeded", {
1116
+ code: "budget_exceeded",
1117
+ ...detail
1118
+ }, { usage });
1119
+ }
1120
+ }
550
1121
  handleViolation(toolCalls, chainCorrections) {
551
1122
  this.recordInvalidCalls(toolCalls);
552
1123
  if (this.exactlyOne && toolCalls.length > 1) {
@@ -677,15 +1248,120 @@ function dropBlankOptionalStringArguments(rawArguments, inputShape) {
677
1248
  }
678
1249
  return normalized;
679
1250
  }
680
- function modelMessages(history, input) {
681
- return [...history.map((message) => ({
682
- role: message.role,
683
- content: message.content
684
- })), {
1251
+ const DEFAULT_MAX_TOOL_RESULT_BYTES = 32768;
1252
+ function toolResultOutputBytes(output) {
1253
+ switch (output.type) {
1254
+ case "text":
1255
+ case "error-text": return Buffer.byteLength(output.value, "utf8");
1256
+ case "json":
1257
+ case "error-json": return Buffer.byteLength(JSON.stringify(output.value) ?? "", "utf8");
1258
+ case "execution-denied": return Buffer.byteLength(output.reason ?? "", "utf8");
1259
+ }
1260
+ }
1261
+ function toModelToolResultPart(part, maxToolResultBytes) {
1262
+ if (toolResultOutputBytes(part.output) > maxToolResultBytes) throw new AgentRuntimeError("failure", "MCP tool result exceeded limit");
1263
+ return {
1264
+ type: "tool-result",
1265
+ toolCallId: part.toolCallId,
1266
+ toolName: part.toolName,
1267
+ output: part.output
1268
+ };
1269
+ }
1270
+ function toModelContentPart(part) {
1271
+ switch (part.type) {
1272
+ case "text": return {
1273
+ type: "text",
1274
+ text: part.text
1275
+ };
1276
+ case "tool-call": return {
1277
+ type: "tool-call",
1278
+ toolCallId: part.toolCallId,
1279
+ toolName: part.toolName,
1280
+ input: part.input
1281
+ };
1282
+ }
1283
+ }
1284
+ function toModelMessage(message, maxToolResultBytes) {
1285
+ switch (message.role) {
1286
+ case "user":
1287
+ case "system": return {
1288
+ role: message.role,
1289
+ content: message.content
1290
+ };
1291
+ case "assistant": return {
1292
+ role: "assistant",
1293
+ content: typeof message.content === "string" ? message.content : message.content.map((part) => toModelContentPart(part))
1294
+ };
1295
+ case "tool": return {
1296
+ role: "tool",
1297
+ content: message.content.map((part) => toModelToolResultPart(part, maxToolResultBytes))
1298
+ };
1299
+ }
1300
+ }
1301
+ function modelMessages(history, input, maxToolResultBytes = DEFAULT_MAX_TOOL_RESULT_BYTES) {
1302
+ return [...history.map((message) => toModelMessage(message, maxToolResultBytes)), {
685
1303
  role: "user",
686
1304
  content: input.prompt
687
1305
  }];
688
1306
  }
1307
+ function toNeutralToolResultOutput(output) {
1308
+ if (output.type === "content") return {
1309
+ type: "text",
1310
+ value: output.value.filter((part) => part.type === "text").map((part) => part.text).join("")
1311
+ };
1312
+ return output;
1313
+ }
1314
+ function toNeutralToolResultPart(part) {
1315
+ return {
1316
+ type: "tool-result",
1317
+ toolCallId: part.toolCallId,
1318
+ toolName: part.toolName,
1319
+ output: toNeutralToolResultOutput(part.output)
1320
+ };
1321
+ }
1322
+ function toNeutralContentPart(part) {
1323
+ switch (part.type) {
1324
+ case "text": return {
1325
+ type: "text",
1326
+ text: part.text
1327
+ };
1328
+ case "tool-call": return {
1329
+ type: "tool-call",
1330
+ toolCallId: part.toolCallId,
1331
+ toolName: part.toolName,
1332
+ input: part.input
1333
+ };
1334
+ default: return;
1335
+ }
1336
+ }
1337
+ function toNeutralToolContentPart(part) {
1338
+ return part.type === "tool-result" ? toNeutralToolResultPart(part) : void 0;
1339
+ }
1340
+ function toNeutralMessage(message) {
1341
+ switch (message.role) {
1342
+ case "assistant": {
1343
+ if (typeof message.content === "string") return message.content.length > 0 ? {
1344
+ role: "assistant",
1345
+ content: message.content
1346
+ } : void 0;
1347
+ const parts = message.content.map(toNeutralContentPart).filter((part) => part !== void 0);
1348
+ return parts.length > 0 ? {
1349
+ role: "assistant",
1350
+ content: parts
1351
+ } : void 0;
1352
+ }
1353
+ case "tool": {
1354
+ const parts = message.content.map(toNeutralToolContentPart).filter((part) => part !== void 0);
1355
+ return parts.length > 0 ? {
1356
+ role: "tool",
1357
+ content: parts
1358
+ } : void 0;
1359
+ }
1360
+ }
1361
+ }
1362
+ function producedNeutralMessages(messages) {
1363
+ return messages.map(toNeutralMessage).filter((message) => message !== void 0);
1364
+ }
689
1365
  var AiSdkNativeRuntime = class {
690
1366
  options;
691
1367
  now;
@@ -712,8 +1388,20 @@ var AiSdkNativeRuntime = class {
712
1388
  invocationId: input.invocationId,
713
1389
  runId: input.runId
714
1390
  }),
1391
+ onBudgetWarning: (warning) => {
1392
+ try {
1393
+ this.options.onBudgetWarning?.({
1394
+ ...warning,
1395
+ scope: input.scope,
1396
+ invocationId: input.invocationId,
1397
+ runId: input.runId
1398
+ });
1399
+ } catch {}
1400
+ },
715
1401
  correctionForInvalidCall: (call) => this.correctionFor(call),
716
- approvedToolCall: input.approvedToolCall
1402
+ approvedToolCall: input.approvedToolCall,
1403
+ budgetGrants: input.budgetGrants,
1404
+ budgetParkable: input.budgetParkable
717
1405
  });
718
1406
  let approvalError;
719
1407
  try {
@@ -723,6 +1411,7 @@ var AiSdkNativeRuntime = class {
723
1411
  const historyLoadedAt = this.now();
724
1412
  const baseInstructions = [systemPrompt, this.options.policies?.exactlyOneToolCallPerStep ?? true ? TOOL_EXECUTION_CONTRACT : ""].filter((part) => part.length > 0).join("\n\n");
725
1413
  const tools = this.createTools(input, startedAt, state, policy);
1414
+ const usageSteps = [];
726
1415
  const agent = new ToolLoopAgent({
727
1416
  model: this.options.model,
728
1417
  tools,
@@ -752,10 +1441,16 @@ var AiSdkNativeRuntime = class {
752
1441
  },
753
1442
  onStepEnd: (step) => {
754
1443
  policy.afterStep(step.toolCalls);
1444
+ usageSteps.push(buildUsageStep({
1445
+ usage: step.usage,
1446
+ provider: normalizeProviderName(step.model.provider),
1447
+ model: step.model.modelId
1448
+ }, this.options.pricing));
1449
+ policy.evaluateBudget(aggregateUsageSteps(usageSteps), usageSteps.length, step.toolCalls.length > 0 || policy.correctionInstruction() !== void 0);
755
1450
  },
756
1451
  stopWhen: () => policy.terminalError != null
757
1452
  });
758
- const messages = modelMessages(history, input);
1453
+ const messages = modelMessages(history, input, this.options.policies?.maxToolResultBytes);
759
1454
  let result = await agent.generate({
760
1455
  messages,
761
1456
  timeout: this.remaining(startedAt)
@@ -769,7 +1464,13 @@ var AiSdkNativeRuntime = class {
769
1464
  });
770
1465
  steps.push(...result.steps);
771
1466
  }
772
- if (policy.terminalError) throw policy.terminalError;
1467
+ if (policy.terminalError) {
1468
+ if (policy.terminalError.kind === "budget") {
1469
+ policy.terminalError.diagnostics.invocationId = input.invocationId;
1470
+ policy.terminalError.diagnostics.runId = input.runId;
1471
+ }
1472
+ throw policy.terminalError;
1473
+ }
773
1474
  const text = result.text.trim();
774
1475
  const provider = normalizeProviderName(result.finalStep.model.provider);
775
1476
  const model = result.finalStep.model.modelId;
@@ -790,6 +1491,11 @@ var AiSdkNativeRuntime = class {
790
1491
  invocationId: input.invocationId,
791
1492
  runId: input.runId,
792
1493
  text,
1494
+ messages: [{
1495
+ role: "user",
1496
+ content: input.prompt
1497
+ }, ...steps.flatMap((step) => producedNeutralMessages(step.response.messages))],
1498
+ usage: aggregateUsageSteps(usageSteps),
793
1499
  provider,
794
1500
  model
795
1501
  };
@@ -956,6 +1662,22 @@ function openAiCompatible(config, fetchImpl = fetch) {
956
1662
  }
957
1663
  // src/define-runtime.ts
958
1664
  const RUNTIME_INTERNALS = Symbol("runwarden/internals");
1665
+ function usageFromRow(row) {
1666
+ if (!row.usageSteps) return {
1667
+ ...emptyInvocationUsage(),
1668
+ costIncomplete: true
1669
+ };
1670
+ return {
1671
+ inputTokens: row.inputTokens ?? 0,
1672
+ outputTokens: row.outputTokens ?? 0,
1673
+ cacheReadTokens: row.cacheReadTokens ?? 0,
1674
+ cacheWriteTokens: row.cacheWriteTokens ?? 0,
1675
+ reasoningTokens: row.reasoningTokens ?? 0,
1676
+ ...row.costUsd === null ? {} : { costUsd: row.costUsd },
1677
+ costIncomplete: row.usageSteps.some((step) => !step.costKnown) || row.usageSteps.some((step) => step.costKnown) && row.costUsd === null || usageStepsOverflow(row.usageSteps),
1678
+ steps: row.usageSteps
1679
+ };
1680
+ }
959
1681
  function buildMcpClient(servers, policies) {
960
1682
  if (!servers || servers.length === 0) return void 0;
961
1683
  const clients = servers.map((server) => new NativeMcpClient({
@@ -1013,11 +1735,13 @@ function defineRuntime(options) {
1013
1735
  loadSystemPrompt: options.loadSystemPrompt,
1014
1736
  loadHistory: options.loadHistory,
1015
1737
  policies: options.policies,
1738
+ pricing: options.pricing,
1016
1739
  onMetrics: options.onMetrics,
1017
1740
  onPolicyDecision: (decision) => {
1018
1741
  for (const listener of decisionListeners) listener(decision);
1019
1742
  options.onPolicyDecision?.(decision);
1020
1743
  },
1744
+ onBudgetWarning: options.onBudgetWarning,
1021
1745
  onToolCallAudit: async (event) => {
1022
1746
  for (const listener of auditListeners) await listener(event);
1023
1747
  try {
@@ -1045,21 +1769,32 @@ function defineRuntime(options) {
1045
1769
  invocationId,
1046
1770
  runId: row.activeRunId,
1047
1771
  text: row.resultText ?? "",
1772
+ messages: row.resultMessages ?? [],
1773
+ usage: usageFromRow(row),
1048
1774
  provider: row.provider ?? void 0,
1049
1775
  model: row.model ?? void 0
1050
1776
  };
1051
1777
  if (row.status === "awaiting_approval") {
1052
1778
  const pending = toPendingToolCall(row.pendingToolCall);
1779
+ const budgetRequest = toBudgetRequest(row.pendingBudgetRequest);
1053
1780
  throw new AgentRuntimeError("failure", `Invocation ${invocationId} is awaiting approval`, {
1054
1781
  code: "approval_required",
1055
1782
  invocationId,
1056
- ...pending ? { toolName: pending.toolName } : {}
1783
+ ...pending ? { toolName: pending.toolName } : {},
1784
+ ...budgetRequest ? { budgetRequest } : {}
1785
+ });
1786
+ }
1787
+ if (row.status === "failed" || row.status === "cancelled") {
1788
+ if (row.lastErrorCode === "budget_exceeded") throw new AgentRuntimeError("budget", row.lastErrorMessage ?? "Invocation budget exceeded", {
1789
+ code: "budget_exceeded",
1790
+ invocationId,
1791
+ runId: row.activeRunId
1792
+ }, { usage: usageFromRow(row) });
1793
+ throw new AgentRuntimeError("failure", row.lastErrorMessage ?? `Invocation ${row.status}`, {
1794
+ code: row.lastErrorCode ?? `invocation_${row.status}`,
1795
+ invocationId
1057
1796
  });
1058
1797
  }
1059
- if (row.status === "failed" || row.status === "cancelled") throw new AgentRuntimeError("failure", row.lastErrorMessage ?? `Invocation ${row.status}`, {
1060
- code: row.lastErrorCode ?? `invocation_${row.status}`,
1061
- invocationId
1062
- });
1063
1798
  if (Date.now() >= deadline) throw new AgentRuntimeError("timeout", `Timed out waiting for invocation ${invocationId}`, { invocationId });
1064
1799
  await sleep$1(pollIntervalMs);
1065
1800
  }
@@ -1071,11 +1806,15 @@ function defineRuntime(options) {
1071
1806
  }
1072
1807
  async function resolveApproval(id, resolution, opts) {
1073
1808
  if (!db) throw dbRequired(`${resolution} resolution`);
1074
- const result = await resolveInvocationApproval(db.db, id, resolution, opts);
1809
+ const result = await resolveInvocationApproval(db.db, id, resolution, opts, options.policies?.budget);
1075
1810
  if (result.outcome === "not_found") throw new AgentRuntimeError("failure", `Invocation ${id} was not found`, {
1076
1811
  code: "invocation_not_found",
1077
1812
  invocationId: id
1078
1813
  });
1814
+ if (result.outcome === "stale_request") throw new AgentRuntimeError("failure", `Invocation ${id} has a newer pending approval request`, {
1815
+ code: "approval_request_stale",
1816
+ invocationId: id
1817
+ });
1079
1818
  if (result.outcome === "not_awaiting") throw new AgentRuntimeError("failure", `Invocation ${id} is not awaiting approval`, {
1080
1819
  code: "invocation_not_awaiting_approval",
1081
1820
  invocationId: id
@@ -1203,6 +1942,25 @@ function pendingFromError(error) {
1203
1942
  input: void 0
1204
1943
  };
1205
1944
  }
1945
+ function budgetRequestFromError(error, runId) {
1946
+ const limits = error.diagnostics.limits;
1947
+ if (!Array.isArray(limits) || !error.usage) return void 0;
1948
+ return {
1949
+ requestId: runId,
1950
+ limits,
1951
+ usage: error.usage
1952
+ };
1953
+ }
1954
+ function auditFailureError(cause, usage, nonRetryable = false) {
1955
+ return new AgentRuntimeError("failure", "Policy audit persistence failed", {
1956
+ code: "audit_persistence_failed",
1957
+ auditFailure: "Policy audit persistence failed",
1958
+ ...nonRetryable ? { retryable: false } : {}
1959
+ }, {
1960
+ cause,
1961
+ ...usage ? { usage } : {}
1962
+ });
1963
+ }
1206
1964
  function runWorker(options) {
1207
1965
  const workerId = options.workerId ?? `${hostname()}:${process.pid}:${randomUUID()}`;
1208
1966
  const leaseTimeoutMs = options.leaseTimeoutMs ?? 12e4;
@@ -1268,22 +2026,39 @@ function runWorker(options) {
1268
2026
  });
1269
2027
  try {
1270
2028
  let result;
2029
+ let invocationError;
1271
2030
  try {
1272
2031
  result = await options.runtime.engine.invoke({
1273
2032
  ...row.input,
1274
2033
  invocationId: String(row.id),
1275
2034
  runId: row.activeRunId,
1276
- approvedToolCall: toPendingToolCall(row.pendingToolCall)
2035
+ approvedToolCall: toPendingToolCall(row.pendingToolCall),
2036
+ ...row.budgetGrants ? { budgetGrants: row.budgetGrants } : {},
2037
+ budgetParkable: true
1277
2038
  });
2039
+ } catch (error) {
2040
+ invocationError = error;
1278
2041
  } finally {
1279
2042
  unsubscribe?.();
1280
2043
  unsubscribeAudit?.();
1281
2044
  await Promise.all(auditWrites);
1282
- if (auditError) throw new AgentRuntimeError("failure", "Policy audit persistence failed", { code: "audit_persistence_failed" }, { cause: auditError });
1283
2045
  }
2046
+ if (auditError) {
2047
+ const budgetError = invocationError === void 0 ? void 0 : toAgentError(invocationError);
2048
+ const code = budgetError ? errorCode(budgetError) : void 0;
2049
+ if (budgetError && code === "budget_exceeded") {
2050
+ budgetError.diagnostics.auditFailure = "Policy audit persistence failed";
2051
+ throw budgetError;
2052
+ }
2053
+ throw auditFailureError(auditError, budgetError?.usage, code === "budget_approval_required");
2054
+ }
2055
+ if (invocationError !== void 0) throw invocationError;
2056
+ if (!result) throw new AgentRuntimeError("failure", "Native runtime returned no invocation result");
1284
2057
  if (lease.isLost) return;
1285
2058
  if (await markInvocationSucceeded(db, row.id, workerId, row.activeRunId, {
1286
2059
  resultText: result.text,
2060
+ resultMessages: result.messages,
2061
+ usage: result.usage,
1287
2062
  provider: result.provider,
1288
2063
  model: result.model
1289
2064
  })) {
@@ -1306,20 +2081,65 @@ function runWorker(options) {
1306
2081
  } catch (error) {
1307
2082
  if (lease.isLost) return;
1308
2083
  const agentError = toAgentError(error);
1309
- if (errorCode(agentError) === "approval_required") {
2084
+ const code = errorCode(agentError);
2085
+ if (code === "approval_required") {
1310
2086
  const pendingToolCall = pendingFromError(agentError);
1311
- const parked = await markInvocationAwaitingApproval(db, row.id, workerId, row.activeRunId, pendingToolCall);
1312
- if (parked) await options.onAwaitingApproval?.(toRecord(parked), pendingToolCall);
2087
+ const parked = await park(row, lease, {
2088
+ kind: "tool",
2089
+ toolCall: pendingToolCall
2090
+ });
2091
+ if (parked) await options.onAwaitingApproval?.(parked, {
2092
+ kind: "tool",
2093
+ toolCall: pendingToolCall
2094
+ });
1313
2095
  return;
1314
2096
  }
1315
- const updated = await markInvocationFailed(db, row.id, workerId, row.activeRunId, {
1316
- code: errorCode(agentError),
1317
- message: agentError.message
1318
- }, retryDelayMs);
1319
- if (updated?.status === "failed") {
1320
- await internals?.notifyInvocationSettled?.(toRecord(updated));
1321
- await options.onFailed?.(toRecord(updated), agentError);
2097
+ if (code === "budget_approval_required") {
2098
+ const budgetRequest = budgetRequestFromError(agentError, row.activeRunId);
2099
+ if (!budgetRequest) {
2100
+ await settleFailure(row, agentError);
2101
+ return;
2102
+ }
2103
+ const detail = {
2104
+ ...agentError.diagnostics.effectiveLimits ? { effectiveLimits: agentError.diagnostics.effectiveLimits } : {},
2105
+ ...agentError.diagnostics.grants ? { grants: agentError.diagnostics.grants } : {}
2106
+ };
2107
+ let parked;
2108
+ try {
2109
+ parked = await park(row, lease, {
2110
+ kind: "budget",
2111
+ request: budgetRequest,
2112
+ detail
2113
+ });
2114
+ } catch (parkError) {
2115
+ await settleFailure(row, auditFailureError(parkError, agentError.usage), true);
2116
+ return;
2117
+ }
2118
+ if (parked) await options.onAwaitingApproval?.(parked, {
2119
+ kind: "budget",
2120
+ budgetRequest
2121
+ });
2122
+ return;
1322
2123
  }
2124
+ await settleFailure(row, agentError);
2125
+ }
2126
+ }
2127
+ async function park(row, lease, request) {
2128
+ if (lease.isLost) return void 0;
2129
+ const parked = await markInvocationAwaitingApproval(db, row.id, workerId, row.activeRunId, request);
2130
+ return parked ? toRecord(parked) : void 0;
2131
+ }
2132
+ async function settleFailure(row, agentError, nonRetryable = false) {
2133
+ const updated = await markInvocationFailed(db, row.id, workerId, row.activeRunId, {
2134
+ code: errorCode(agentError),
2135
+ message: agentError.message
2136
+ }, retryDelayMs, {
2137
+ ...agentError.usage ? { usage: agentError.usage } : {},
2138
+ ...nonRetryable || agentError.diagnostics.retryable === false || errorCode(agentError) === "budget_exceeded" ? { retryable: false } : {}
2139
+ });
2140
+ if (updated?.status === "failed") {
2141
+ await internals?.notifyInvocationSettled?.(toRecord(updated));
2142
+ await options.onFailed?.(toRecord(updated), agentError);
1323
2143
  }
1324
2144
  }
1325
2145
  const loop = (async () => {
@@ -1377,10 +2197,20 @@ var FakeAgentRuntime = class {
1377
2197
  async invoke(input) {
1378
2198
  const invocationIndex = this.invocations.push(input);
1379
2199
  if (typeof this.result === "function") return this.result(input, invocationIndex);
1380
- return this.result ?? {
2200
+ if (this.result) return this.result;
2201
+ const text = `Fake runtime response to: ${input.prompt}`;
2202
+ return {
1381
2203
  invocationId: input.invocationId,
1382
2204
  runId: input.runId,
1383
- text: `Fake runtime response to: ${input.prompt}`,
2205
+ text,
2206
+ messages: [{
2207
+ role: "user",
2208
+ content: input.prompt
2209
+ }, {
2210
+ role: "assistant",
2211
+ content: text
2212
+ }],
2213
+ usage: emptyInvocationUsage(),
1384
2214
  provider: "fake",
1385
2215
  model: "fake-agent-runtime"
1386
2216
  };