runwarden 0.5.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-BO3OjyhV.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 {
@@ -28,6 +67,13 @@ function mapRow(raw) {
28
67
  input: raw.input,
29
68
  resultText: raw.result_text ?? null,
30
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,
31
77
  idempotencyKey: raw.idempotency_key ?? null,
32
78
  status: raw.status,
33
79
  attemptCount: Number(raw.attempt_count),
@@ -40,6 +86,8 @@ function mapRow(raw) {
40
86
  activeRunId: raw.active_run_id ?? null,
41
87
  activeRunRegisteredAt: raw.active_run_registered_at ?? null,
42
88
  pendingToolCall: raw.pending_tool_call ?? null,
89
+ pendingBudgetRequest: raw.pending_budget_request ?? null,
90
+ budgetGrants: raw.budget_grants ?? null,
43
91
  startedAt: raw.started_at ?? null,
44
92
  completedAt: raw.completed_at ?? null,
45
93
  failedAt: raw.failed_at ?? null,
@@ -61,14 +109,42 @@ function toPendingToolCall(raw) {
61
109
  input: raw.input
62
110
  };
63
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
+ }
64
131
  function toRecord(row) {
132
+ const pendingToolCall = toPendingToolCall(row.pendingToolCall);
133
+ const pendingBudgetRequest = toBudgetRequest(row.pendingBudgetRequest);
65
134
  return {
66
135
  ...row.input,
67
136
  id: String(row.id),
68
137
  status: row.status,
69
138
  attemptCount: row.attemptCount,
70
139
  resultText: row.resultText ?? void 0,
71
- pendingToolCall: toPendingToolCall(row.pendingToolCall),
140
+ ...pendingBudgetRequest ? {
141
+ pendingApprovalKind: "budget",
142
+ pendingBudgetRequest
143
+ } : pendingToolCall ? {
144
+ pendingApprovalKind: "tool",
145
+ pendingToolCall
146
+ } : {},
147
+ budgetGrants: toBudgetGrants(row.budgetGrants),
72
148
  lastErrorCode: row.lastErrorCode ?? void 0,
73
149
  lastErrorMessage: row.lastErrorMessage ?? void 0,
74
150
  createdAt: row.createdAt,
@@ -205,9 +281,17 @@ async function markInvocationSucceeded(db, id, workerId, runId, result) {
205
281
  set status = 'succeeded',
206
282
  result_text = ${result.resultText},
207
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,
208
291
  provider = ${result.provider ?? null},
209
292
  model = ${result.model ?? null},
210
293
  pending_tool_call = null,
294
+ pending_budget_request = null,
211
295
  completed_at = now(),
212
296
  failed_at = null,
213
297
  ${CLEAR_CLAIM},
@@ -217,30 +301,82 @@ async function markInvocationSucceeded(db, id, workerId, runId, result) {
217
301
  returning id
218
302
  `)).rows.length > 0;
219
303
  }
220
- async function markInvocationAwaitingApproval(db, id, workerId, runId, pendingToolCall) {
221
- const raw = (await db.execute(sql`
222
- update agent_runtime.invocations
223
- set status = 'awaiting_approval',
224
- pending_tool_call = ${JSON.stringify(pendingToolCall)}::jsonb,
225
- attempt_count = greatest(attempt_count - 1, 0),
226
- last_error_code = null,
227
- last_error_message = null,
228
- ${CLEAR_CLAIM},
229
- updated_at = now()
230
- where id = ${id} and claimed_by = ${workerId} and status = 'running'
231
- and active_run_id = ${runId}
232
- returning *
233
- `)).rows[0];
234
- 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
+ });
235
362
  }
236
- 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;
237
365
  const raw = (await db.execute(sql`
238
366
  update agent_runtime.invocations
239
- set status = (case when attempt_count >= max_attempts then 'failed' else 'pending' end)::agent_runtime.invocation_status,
240
- next_attempt_at = now() + (${retryDelayMs} * interval '1 millisecond'),
241
- failed_at = case when attempt_count >= max_attempts then now() else failed_at end,
242
- completed_at = case when attempt_count >= max_attempts then now() else completed_at end,
243
- 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,
244
380
  last_error_code = ${error.code},
245
381
  last_error_message = ${error.message},
246
382
  ${CLEAR_CLAIM},
@@ -263,7 +399,11 @@ async function listAwaitingApprovalRows(db, scope) {
263
399
  order by created_at asc, id asc
264
400
  `)).rows.map(mapRow);
265
401
  }
266
- 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 = {}) {
267
407
  return db.transaction(async (tx) => {
268
408
  const raw = (await tx.execute(sql`
269
409
  select * from agent_runtime.invocations where id = ${id} for update
@@ -274,11 +414,71 @@ async function resolveInvocationApproval(db, id, resolution, options = {}) {
274
414
  outcome: "not_awaiting",
275
415
  row
276
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" });
277
478
  const pending = toPendingToolCall(row.pendingToolCall);
278
479
  const detail = {
279
480
  toolInput: pending?.input ?? null,
280
- ...options.resolvedBy ? { resolvedBy: options.resolvedBy } : {},
281
- ...options.note ? { note: options.note } : {}
481
+ ...resolvedByDetail(options)
282
482
  };
283
483
  await tx.execute(sql`
284
484
  insert into agent_runtime.policy_decisions
@@ -320,6 +520,21 @@ async function resolveInvocationApproval(db, id, resolution, options = {}) {
320
520
  };
321
521
  });
322
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
+ }
323
538
  // src/brand.ts
324
539
  const RUNTIME_NAME = "runwarden";
325
540
  const RUN_ID_HEADER = "X-Runwarden-Run-Id";
@@ -397,8 +612,202 @@ function canonicalJson(value) {
397
612
  function deriveToolCallIdempotencyKey(invocationId, toolName, input) {
398
613
  return createHash("sha256").update(`invocation:${invocationId}\n${toolName}\n${canonicalJson(input)}`).digest("hex");
399
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
+ }
400
790
  // src/runtime/policy-engine.ts
401
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
+ }
402
811
  function isSameToolCall(approved, toolName, input) {
403
812
  return approved.toolName === toolName && canonicalJson(approved.input) === canonicalJson(input);
404
813
  }
@@ -408,6 +817,22 @@ var PolicyEngine = class {
408
817
  exactlyOne;
409
818
  correctionBudget;
410
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
+ };
411
836
  pendingCorrection;
412
837
  terminal;
413
838
  approvedToolCalls = 0;
@@ -419,6 +844,9 @@ var PolicyEngine = class {
419
844
  this.exactlyOne = policies.exactlyOneToolCallPerStep ?? true;
420
845
  this.correctionBudget = policies.correctionBudget ?? 1;
421
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);
422
850
  this.approvedToolCall = hooks.approvedToolCall;
423
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`);
424
852
  }
@@ -549,6 +977,147 @@ var PolicyEngine = class {
549
977
  }
550
978
  this.handleViolation(toolCalls, 0);
551
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
+ }
552
1121
  handleViolation(toolCalls, chainCorrections) {
553
1122
  this.recordInvalidCalls(toolCalls);
554
1123
  if (this.exactlyOne && toolCalls.length > 1) {
@@ -819,8 +1388,20 @@ var AiSdkNativeRuntime = class {
819
1388
  invocationId: input.invocationId,
820
1389
  runId: input.runId
821
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
+ },
822
1401
  correctionForInvalidCall: (call) => this.correctionFor(call),
823
- approvedToolCall: input.approvedToolCall
1402
+ approvedToolCall: input.approvedToolCall,
1403
+ budgetGrants: input.budgetGrants,
1404
+ budgetParkable: input.budgetParkable
824
1405
  });
825
1406
  let approvalError;
826
1407
  try {
@@ -830,6 +1411,7 @@ var AiSdkNativeRuntime = class {
830
1411
  const historyLoadedAt = this.now();
831
1412
  const baseInstructions = [systemPrompt, this.options.policies?.exactlyOneToolCallPerStep ?? true ? TOOL_EXECUTION_CONTRACT : ""].filter((part) => part.length > 0).join("\n\n");
832
1413
  const tools = this.createTools(input, startedAt, state, policy);
1414
+ const usageSteps = [];
833
1415
  const agent = new ToolLoopAgent({
834
1416
  model: this.options.model,
835
1417
  tools,
@@ -859,6 +1441,12 @@ var AiSdkNativeRuntime = class {
859
1441
  },
860
1442
  onStepEnd: (step) => {
861
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);
862
1450
  },
863
1451
  stopWhen: () => policy.terminalError != null
864
1452
  });
@@ -876,7 +1464,13 @@ var AiSdkNativeRuntime = class {
876
1464
  });
877
1465
  steps.push(...result.steps);
878
1466
  }
879
- 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
+ }
880
1474
  const text = result.text.trim();
881
1475
  const provider = normalizeProviderName(result.finalStep.model.provider);
882
1476
  const model = result.finalStep.model.modelId;
@@ -901,6 +1495,7 @@ var AiSdkNativeRuntime = class {
901
1495
  role: "user",
902
1496
  content: input.prompt
903
1497
  }, ...steps.flatMap((step) => producedNeutralMessages(step.response.messages))],
1498
+ usage: aggregateUsageSteps(usageSteps),
904
1499
  provider,
905
1500
  model
906
1501
  };
@@ -1067,6 +1662,22 @@ function openAiCompatible(config, fetchImpl = fetch) {
1067
1662
  }
1068
1663
  // src/define-runtime.ts
1069
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
+ }
1070
1681
  function buildMcpClient(servers, policies) {
1071
1682
  if (!servers || servers.length === 0) return void 0;
1072
1683
  const clients = servers.map((server) => new NativeMcpClient({
@@ -1124,11 +1735,13 @@ function defineRuntime(options) {
1124
1735
  loadSystemPrompt: options.loadSystemPrompt,
1125
1736
  loadHistory: options.loadHistory,
1126
1737
  policies: options.policies,
1738
+ pricing: options.pricing,
1127
1739
  onMetrics: options.onMetrics,
1128
1740
  onPolicyDecision: (decision) => {
1129
1741
  for (const listener of decisionListeners) listener(decision);
1130
1742
  options.onPolicyDecision?.(decision);
1131
1743
  },
1744
+ onBudgetWarning: options.onBudgetWarning,
1132
1745
  onToolCallAudit: async (event) => {
1133
1746
  for (const listener of auditListeners) await listener(event);
1134
1747
  try {
@@ -1157,21 +1770,31 @@ function defineRuntime(options) {
1157
1770
  runId: row.activeRunId,
1158
1771
  text: row.resultText ?? "",
1159
1772
  messages: row.resultMessages ?? [],
1773
+ usage: usageFromRow(row),
1160
1774
  provider: row.provider ?? void 0,
1161
1775
  model: row.model ?? void 0
1162
1776
  };
1163
1777
  if (row.status === "awaiting_approval") {
1164
1778
  const pending = toPendingToolCall(row.pendingToolCall);
1779
+ const budgetRequest = toBudgetRequest(row.pendingBudgetRequest);
1165
1780
  throw new AgentRuntimeError("failure", `Invocation ${invocationId} is awaiting approval`, {
1166
1781
  code: "approval_required",
1167
1782
  invocationId,
1168
- ...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
1169
1796
  });
1170
1797
  }
1171
- if (row.status === "failed" || row.status === "cancelled") throw new AgentRuntimeError("failure", row.lastErrorMessage ?? `Invocation ${row.status}`, {
1172
- code: row.lastErrorCode ?? `invocation_${row.status}`,
1173
- invocationId
1174
- });
1175
1798
  if (Date.now() >= deadline) throw new AgentRuntimeError("timeout", `Timed out waiting for invocation ${invocationId}`, { invocationId });
1176
1799
  await sleep$1(pollIntervalMs);
1177
1800
  }
@@ -1183,11 +1806,15 @@ function defineRuntime(options) {
1183
1806
  }
1184
1807
  async function resolveApproval(id, resolution, opts) {
1185
1808
  if (!db) throw dbRequired(`${resolution} resolution`);
1186
- const result = await resolveInvocationApproval(db.db, id, resolution, opts);
1809
+ const result = await resolveInvocationApproval(db.db, id, resolution, opts, options.policies?.budget);
1187
1810
  if (result.outcome === "not_found") throw new AgentRuntimeError("failure", `Invocation ${id} was not found`, {
1188
1811
  code: "invocation_not_found",
1189
1812
  invocationId: id
1190
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
+ });
1191
1818
  if (result.outcome === "not_awaiting") throw new AgentRuntimeError("failure", `Invocation ${id} is not awaiting approval`, {
1192
1819
  code: "invocation_not_awaiting_approval",
1193
1820
  invocationId: id
@@ -1315,6 +1942,25 @@ function pendingFromError(error) {
1315
1942
  input: void 0
1316
1943
  };
1317
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
+ }
1318
1964
  function runWorker(options) {
1319
1965
  const workerId = options.workerId ?? `${hostname()}:${process.pid}:${randomUUID()}`;
1320
1966
  const leaseTimeoutMs = options.leaseTimeoutMs ?? 12e4;
@@ -1380,23 +2026,39 @@ function runWorker(options) {
1380
2026
  });
1381
2027
  try {
1382
2028
  let result;
2029
+ let invocationError;
1383
2030
  try {
1384
2031
  result = await options.runtime.engine.invoke({
1385
2032
  ...row.input,
1386
2033
  invocationId: String(row.id),
1387
2034
  runId: row.activeRunId,
1388
- approvedToolCall: toPendingToolCall(row.pendingToolCall)
2035
+ approvedToolCall: toPendingToolCall(row.pendingToolCall),
2036
+ ...row.budgetGrants ? { budgetGrants: row.budgetGrants } : {},
2037
+ budgetParkable: true
1389
2038
  });
2039
+ } catch (error) {
2040
+ invocationError = error;
1390
2041
  } finally {
1391
2042
  unsubscribe?.();
1392
2043
  unsubscribeAudit?.();
1393
2044
  await Promise.all(auditWrites);
1394
- if (auditError) throw new AgentRuntimeError("failure", "Policy audit persistence failed", { code: "audit_persistence_failed" }, { cause: auditError });
1395
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");
1396
2057
  if (lease.isLost) return;
1397
2058
  if (await markInvocationSucceeded(db, row.id, workerId, row.activeRunId, {
1398
2059
  resultText: result.text,
1399
2060
  resultMessages: result.messages,
2061
+ usage: result.usage,
1400
2062
  provider: result.provider,
1401
2063
  model: result.model
1402
2064
  })) {
@@ -1419,20 +2081,65 @@ function runWorker(options) {
1419
2081
  } catch (error) {
1420
2082
  if (lease.isLost) return;
1421
2083
  const agentError = toAgentError(error);
1422
- if (errorCode(agentError) === "approval_required") {
2084
+ const code = errorCode(agentError);
2085
+ if (code === "approval_required") {
1423
2086
  const pendingToolCall = pendingFromError(agentError);
1424
- const parked = await markInvocationAwaitingApproval(db, row.id, workerId, row.activeRunId, pendingToolCall);
1425
- 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
+ });
1426
2095
  return;
1427
2096
  }
1428
- const updated = await markInvocationFailed(db, row.id, workerId, row.activeRunId, {
1429
- code: errorCode(agentError),
1430
- message: agentError.message
1431
- }, retryDelayMs);
1432
- if (updated?.status === "failed") {
1433
- await internals?.notifyInvocationSettled?.(toRecord(updated));
1434
- 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;
1435
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);
1436
2143
  }
1437
2144
  }
1438
2145
  const loop = (async () => {
@@ -1503,6 +2210,7 @@ var FakeAgentRuntime = class {
1503
2210
  role: "assistant",
1504
2211
  content: text
1505
2212
  }],
2213
+ usage: emptyInvocationUsage(),
1506
2214
  provider: "fake",
1507
2215
  model: "fake-agent-runtime"
1508
2216
  };