runwarden 0.0.0-stage → 0.2.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 ADDED
@@ -0,0 +1,1336 @@
1
+ import { n as createDb, r as policyDecisions, t as initDb } from "./init-Dm_qLwix.js";
2
+ import { createHash, randomUUID } from "node:crypto";
3
+ import { sql } from "drizzle-orm";
4
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
5
+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
6
+ import { ToolLoopAgent, tool } from "ai";
7
+ import { z } from "zod";
8
+ import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
9
+ import { hostname } from "node:os";
10
+ // src/contracts/errors.ts
11
+ var AgentRuntimeError = class extends Error {
12
+ kind;
13
+ diagnostics;
14
+ constructor(kind, message, diagnostics = {}, options = {}) {
15
+ super(message, options);
16
+ this.kind = kind;
17
+ this.diagnostics = diagnostics;
18
+ this.name = "AgentRuntimeError";
19
+ }
20
+ };
21
+ // src/db/repositories/invocations.ts
22
+ function mapRow(raw) {
23
+ return {
24
+ id: Number(raw.id),
25
+ scope: raw.scope,
26
+ conversationId: raw.conversation_id ?? null,
27
+ orderingKey: raw.ordering_key ?? null,
28
+ input: raw.input,
29
+ resultText: raw.result_text ?? null,
30
+ idempotencyKey: raw.idempotency_key ?? null,
31
+ status: raw.status,
32
+ attemptCount: Number(raw.attempt_count),
33
+ maxAttempts: Number(raw.max_attempts),
34
+ nextAttemptAt: raw.next_attempt_at,
35
+ claimedAt: raw.claimed_at ?? null,
36
+ claimedBy: raw.claimed_by ?? null,
37
+ leaseHeartbeatAt: raw.lease_heartbeat_at ?? null,
38
+ leaseExpiresAt: raw.lease_expires_at ?? null,
39
+ activeRunId: raw.active_run_id ?? null,
40
+ activeRunRegisteredAt: raw.active_run_registered_at ?? null,
41
+ pendingToolCall: raw.pending_tool_call ?? null,
42
+ startedAt: raw.started_at ?? null,
43
+ completedAt: raw.completed_at ?? null,
44
+ failedAt: raw.failed_at ?? null,
45
+ provider: raw.provider ?? null,
46
+ model: raw.model ?? null,
47
+ lastErrorCode: raw.last_error_code ?? null,
48
+ lastErrorMessage: raw.last_error_message ?? null,
49
+ metadata: raw.metadata ?? null,
50
+ createdAt: raw.created_at,
51
+ updatedAt: raw.updated_at
52
+ };
53
+ }
54
+ function toPendingToolCall(raw) {
55
+ if (typeof raw !== "object" || raw === null) return void 0;
56
+ const toolName = raw.toolName;
57
+ if (typeof toolName !== "string") return void 0;
58
+ return {
59
+ toolName,
60
+ input: raw.input
61
+ };
62
+ }
63
+ function toRecord(row) {
64
+ return {
65
+ ...row.input,
66
+ id: String(row.id),
67
+ status: row.status,
68
+ attemptCount: row.attemptCount,
69
+ resultText: row.resultText ?? void 0,
70
+ pendingToolCall: toPendingToolCall(row.pendingToolCall),
71
+ lastErrorCode: row.lastErrorCode ?? void 0,
72
+ lastErrorMessage: row.lastErrorMessage ?? void 0,
73
+ createdAt: row.createdAt,
74
+ completedAt: row.completedAt ?? void 0
75
+ };
76
+ }
77
+ async function enqueueInvocation(db, input) {
78
+ const first = (await db.execute(sql`
79
+ insert into agent_runtime.invocations
80
+ (scope, conversation_id, ordering_key, input, idempotency_key, max_attempts, metadata)
81
+ values (
82
+ ${input.scope},
83
+ ${input.conversationId ?? null},
84
+ ${input.orderingKey ?? null},
85
+ ${JSON.stringify(input)}::jsonb,
86
+ ${input.idempotencyKey ?? null},
87
+ ${input.maxAttempts ?? 3},
88
+ ${input.metadata ? JSON.stringify(input.metadata) : null}::jsonb
89
+ )
90
+ on conflict (scope, idempotency_key) where idempotency_key is not null
91
+ do nothing
92
+ returning id
93
+ `)).rows[0];
94
+ if (first) return {
95
+ invocationId: String(first.id),
96
+ created: true
97
+ };
98
+ if (!input.idempotencyKey) throw new Error("enqueue inserted no row without an idempotency key");
99
+ const found = (await db.execute(sql`
100
+ select id from agent_runtime.invocations
101
+ where scope = ${input.scope} and idempotency_key = ${input.idempotencyKey}
102
+ limit 1
103
+ `)).rows[0];
104
+ if (!found) throw new Error("enqueue conflicted but no existing row found");
105
+ return {
106
+ invocationId: String(found.id),
107
+ created: false
108
+ };
109
+ }
110
+ async function getInvocationRowById(db, id) {
111
+ const raw = (await db.execute(sql`select * from agent_runtime.invocations where id = ${id} limit 1`)).rows[0];
112
+ return raw ? mapRow(raw) : void 0;
113
+ }
114
+ async function claimInvocations(db, workerId, options) {
115
+ const result = await db.execute(sql`
116
+ with exhausted as (
117
+ update agent_runtime.invocations
118
+ set status = 'failed',
119
+ failed_at = now(), completed_at = now(), updated_at = now(),
120
+ last_error_code = 'lease_expired',
121
+ last_error_message = 'Lease expired on the final attempt',
122
+ ${CLEAR_CLAIM}
123
+ where status = 'running' and lease_expires_at < now()
124
+ and attempt_count >= max_attempts
125
+ returning *
126
+ ), candidate as (
127
+ select inv.id
128
+ from agent_runtime.invocations inv
129
+ where inv.attempt_count < inv.max_attempts
130
+ and (
131
+ (inv.status = 'pending' and inv.next_attempt_at <= now())
132
+ or (inv.status = 'running' and inv.lease_expires_at < now())
133
+ )
134
+ and (
135
+ inv.ordering_key is null
136
+ or not exists (
137
+ select 1
138
+ from agent_runtime.invocations pred
139
+ where pred.scope = inv.scope
140
+ and pred.ordering_key = inv.ordering_key
141
+ and pred.id <> inv.id
142
+ and pred.status in ('pending', 'running', 'awaiting_approval')
143
+ and pred.id not in (select id from exhausted)
144
+ and (
145
+ pred.status = 'running'
146
+ or pred.created_at < inv.created_at
147
+ or (pred.created_at = inv.created_at and pred.id < inv.id)
148
+ )
149
+ )
150
+ )
151
+ order by inv.created_at asc, inv.id asc
152
+ for update skip locked
153
+ limit ${options.maxBatchSize}
154
+ ), claimed as (
155
+ update agent_runtime.invocations inv
156
+ set status = 'running',
157
+ attempt_count = inv.attempt_count + 1,
158
+ claimed_at = now(),
159
+ claimed_by = ${workerId},
160
+ lease_heartbeat_at = now(),
161
+ lease_expires_at = now() + (${options.leaseTimeoutMs} * interval '1 millisecond'),
162
+ started_at = coalesce(inv.started_at, now()),
163
+ active_run_id = gen_random_uuid(),
164
+ active_run_registered_at = now(),
165
+ updated_at = now()
166
+ from candidate
167
+ where inv.id = candidate.id
168
+ returning inv.*
169
+ )
170
+ select 'claimed' as transition, claimed.* from claimed
171
+ union all
172
+ select 'exhausted' as transition, exhausted.* from exhausted
173
+ `);
174
+ const claimed = [];
175
+ const exhausted = [];
176
+ for (const raw of result.rows) if (raw.transition === "exhausted") exhausted.push(mapRow(raw));
177
+ else claimed.push(mapRow(raw));
178
+ return {
179
+ claimed,
180
+ exhausted
181
+ };
182
+ }
183
+ async function heartbeatInvocation(db, id, workerId, runId, leaseTimeoutMs) {
184
+ return (await db.execute(sql`
185
+ update agent_runtime.invocations
186
+ set lease_heartbeat_at = now(),
187
+ lease_expires_at = now() + (${leaseTimeoutMs} * interval '1 millisecond'),
188
+ updated_at = now()
189
+ where id = ${id} and claimed_by = ${workerId} and status = 'running'
190
+ and active_run_id = ${runId}
191
+ returning id
192
+ `)).rows.length > 0;
193
+ }
194
+ const CLEAR_CLAIM = sql`
195
+ claimed_at = null,
196
+ claimed_by = null,
197
+ lease_heartbeat_at = null,
198
+ lease_expires_at = null
199
+ `;
200
+ async function markInvocationSucceeded(db, id, workerId, runId, result) {
201
+ return (await db.execute(sql`
202
+ update agent_runtime.invocations
203
+ set status = 'succeeded',
204
+ result_text = ${result.resultText},
205
+ provider = ${result.provider ?? null},
206
+ model = ${result.model ?? null},
207
+ pending_tool_call = null,
208
+ completed_at = now(),
209
+ failed_at = null,
210
+ ${CLEAR_CLAIM},
211
+ updated_at = now()
212
+ where id = ${id} and claimed_by = ${workerId} and status = 'running'
213
+ and active_run_id = ${runId}
214
+ returning id
215
+ `)).rows.length > 0;
216
+ }
217
+ async function markInvocationAwaitingApproval(db, id, workerId, runId, pendingToolCall) {
218
+ const raw = (await db.execute(sql`
219
+ update agent_runtime.invocations
220
+ set status = 'awaiting_approval',
221
+ pending_tool_call = ${JSON.stringify(pendingToolCall)}::jsonb,
222
+ attempt_count = greatest(attempt_count - 1, 0),
223
+ last_error_code = null,
224
+ last_error_message = null,
225
+ ${CLEAR_CLAIM},
226
+ updated_at = now()
227
+ where id = ${id} and claimed_by = ${workerId} and status = 'running'
228
+ and active_run_id = ${runId}
229
+ returning *
230
+ `)).rows[0];
231
+ return raw ? mapRow(raw) : void 0;
232
+ }
233
+ async function markInvocationFailed(db, id, workerId, runId, error, retryDelayMs) {
234
+ const raw = (await db.execute(sql`
235
+ update agent_runtime.invocations
236
+ set status = (case when attempt_count >= max_attempts then 'failed' else 'pending' end)::agent_runtime.invocation_status,
237
+ next_attempt_at = now() + (${retryDelayMs} * interval '1 millisecond'),
238
+ failed_at = case when attempt_count >= max_attempts then now() else failed_at end,
239
+ completed_at = case when attempt_count >= max_attempts then now() else completed_at end,
240
+ pending_tool_call = case when attempt_count >= max_attempts then null else pending_tool_call end,
241
+ last_error_code = ${error.code},
242
+ last_error_message = ${error.message},
243
+ ${CLEAR_CLAIM},
244
+ updated_at = now()
245
+ where id = ${id} and claimed_by = ${workerId} and status = 'running'
246
+ and active_run_id = ${runId}
247
+ returning *
248
+ `)).rows[0];
249
+ return raw ? mapRow(raw) : void 0;
250
+ }
251
+ // src/db/repositories/approvals.ts
252
+ async function listAwaitingApprovalRows(db, scope) {
253
+ return (scope !== void 0 ? await db.execute(sql`
254
+ select * from agent_runtime.invocations
255
+ where status = 'awaiting_approval' and scope = ${scope}
256
+ order by created_at asc, id asc
257
+ `) : await db.execute(sql`
258
+ select * from agent_runtime.invocations
259
+ where status = 'awaiting_approval'
260
+ order by created_at asc, id asc
261
+ `)).rows.map(mapRow);
262
+ }
263
+ async function resolveInvocationApproval(db, id, resolution, options = {}) {
264
+ return db.transaction(async (tx) => {
265
+ const raw = (await tx.execute(sql`
266
+ select * from agent_runtime.invocations where id = ${id} for update
267
+ `)).rows[0];
268
+ if (!raw) return { outcome: "not_found" };
269
+ const row = mapRow(raw);
270
+ if (row.status !== "awaiting_approval") return {
271
+ outcome: "not_awaiting",
272
+ row
273
+ };
274
+ const pending = toPendingToolCall(row.pendingToolCall);
275
+ const detail = {
276
+ toolInput: pending?.input ?? null,
277
+ ...options.resolvedBy ? { resolvedBy: options.resolvedBy } : {},
278
+ ...options.note ? { note: options.note } : {}
279
+ };
280
+ await tx.execute(sql`
281
+ insert into agent_runtime.policy_decisions
282
+ (scope, invocation_id, run_id, tool_name, rule, decision, detail)
283
+ values (
284
+ ${row.scope},
285
+ ${row.id},
286
+ ${row.activeRunId ?? randomUUID()},
287
+ ${pending?.toolName ?? null},
288
+ 'require_approval',
289
+ ${resolution},
290
+ ${JSON.stringify(detail)}::jsonb
291
+ )
292
+ `);
293
+ const updatedRaw = (resolution === "approved" ? await tx.execute(sql`
294
+ update agent_runtime.invocations
295
+ set status = 'pending',
296
+ next_attempt_at = now(),
297
+ last_error_code = null,
298
+ last_error_message = null,
299
+ updated_at = now()
300
+ where id = ${row.id}
301
+ returning *
302
+ `) : await tx.execute(sql`
303
+ update agent_runtime.invocations
304
+ set status = 'cancelled',
305
+ pending_tool_call = null,
306
+ last_error_code = 'approval_rejected',
307
+ last_error_message = ${options.note ?? "Approval rejected"},
308
+ failed_at = now(),
309
+ completed_at = now(),
310
+ updated_at = now()
311
+ where id = ${row.id}
312
+ returning *
313
+ `)).rows[0];
314
+ return {
315
+ outcome: "resolved",
316
+ row: mapRow(updatedRaw)
317
+ };
318
+ });
319
+ }
320
+ // src/brand.ts
321
+ const RUNTIME_NAME = "runwarden";
322
+ const RUN_ID_HEADER = "X-Runwarden-Run-Id";
323
+ const ERROR_CATEGORY_META_KEY = "runwarden/error-category";
324
+ const IDEMPOTENCY_KEY_META_KEY = "runwarden/idempotency-key";
325
+ // src/mcp/native-mcp-client.ts
326
+ function isTimeoutFailure$1(error) {
327
+ if (!(error instanceof Error)) return false;
328
+ return ["AbortError", "TimeoutError"].includes(error.name) || error.code === -32001;
329
+ }
330
+ function required$1(value, name) {
331
+ if (!value.trim()) throw new Error(`${name} is required`);
332
+ return value;
333
+ }
334
+ function isCorrectable(meta) {
335
+ return meta?.[ERROR_CATEGORY_META_KEY] === "correctable";
336
+ }
337
+ var NativeMcpClient = class {
338
+ endpoint;
339
+ headers;
340
+ timeoutMs;
341
+ maxResultBytes;
342
+ constructor(config) {
343
+ this.endpoint = new URL(required$1(config.endpoint, "endpoint"));
344
+ if (!["http:", "https:"].includes(this.endpoint.protocol)) throw new Error("endpoint must use http or https");
345
+ this.headers = config.headers ?? {};
346
+ this.timeoutMs = config.timeoutMs ?? 3e4;
347
+ this.maxResultBytes = config.maxResultBytes ?? 32768;
348
+ for (const [name, value] of [["timeoutMs", this.timeoutMs], ["maxResultBytes", this.maxResultBytes]]) if (!Number.isSafeInteger(value) || value <= 0) throw new Error(`${name} must be a positive integer`);
349
+ }
350
+ async open(input) {
351
+ const client = new Client({
352
+ name: `${RUNTIME_NAME}-runtime`,
353
+ version: "1"
354
+ });
355
+ const transport = new StreamableHTTPClientTransport(this.endpoint, { requestInit: { headers: {
356
+ ...this.headers,
357
+ [RUN_ID_HEADER]: input.runId
358
+ } } });
359
+ try {
360
+ await client.connect(transport, { timeout: Math.min(this.timeoutMs, input.timeoutMs) });
361
+ } catch (error) {
362
+ await client.close().catch(() => void 0);
363
+ if (isTimeoutFailure$1(error)) throw new AgentRuntimeError("timeout", "MCP connection timed out");
364
+ throw new AgentRuntimeError("failure", "MCP connection failed");
365
+ }
366
+ return {
367
+ callTool: async (call) => {
368
+ try {
369
+ const result = await client.callTool({
370
+ name: call.name,
371
+ arguments: call.arguments,
372
+ ...call.idempotencyKey ? { _meta: { [IDEMPOTENCY_KEY_META_KEY]: call.idempotencyKey } } : {}
373
+ }, void 0, { timeout: Math.min(this.timeoutMs, call.timeoutMs) });
374
+ if (!Array.isArray(result.content) || result.isError && !isCorrectable(result._meta)) throw new AgentRuntimeError("failure", "MCP tool call failed");
375
+ const serialized = JSON.stringify(result.content);
376
+ if (Buffer.byteLength(serialized, "utf8") > this.maxResultBytes) throw new AgentRuntimeError("failure", "MCP tool result exceeded limit");
377
+ return serialized;
378
+ } catch (error) {
379
+ if (error instanceof AgentRuntimeError) throw error;
380
+ if (isTimeoutFailure$1(error)) throw new AgentRuntimeError("timeout", "MCP tool call timed out");
381
+ throw new AgentRuntimeError("failure", "MCP tool call failed");
382
+ }
383
+ },
384
+ close: () => client.close()
385
+ };
386
+ }
387
+ };
388
+ // src/runtime/canonical-json.ts
389
+ function canonicalJson(value) {
390
+ if (Array.isArray(value)) return `[${value.map((entry) => canonicalJson(entry)).join(",")}]`;
391
+ if (typeof value === "object" && value !== null) return `{${Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, entry]) => `${JSON.stringify(key)}:${canonicalJson(entry)}`).join(",")}}`;
392
+ return JSON.stringify(value) ?? "undefined";
393
+ }
394
+ function deriveToolCallIdempotencyKey(invocationId, toolName, input) {
395
+ return createHash("sha256").update(`invocation:${invocationId}\n${toolName}\n${canonicalJson(input)}`).digest("hex");
396
+ }
397
+ // src/runtime/policy-engine.ts
398
+ const TOOL_PROPOSAL_CORRECTION = "Return exactly one tool call for the next required step. Wait for its result before proposing another tool.";
399
+ function isSameToolCall(approved, toolName, input) {
400
+ return approved.toolName === toolName && canonicalJson(approved.input) === canonicalJson(input);
401
+ }
402
+ var PolicyEngine = class {
403
+ policies;
404
+ hooks;
405
+ exactlyOne;
406
+ correctionBudget;
407
+ maxToolCallsPerInvocation;
408
+ pendingCorrection;
409
+ terminal;
410
+ approvedToolCalls = 0;
411
+ approvedToolCall;
412
+ pendingApproval;
413
+ constructor(policies, hooks = {}) {
414
+ this.policies = policies;
415
+ this.hooks = hooks;
416
+ this.exactlyOne = policies.exactlyOneToolCallPerStep ?? true;
417
+ this.correctionBudget = policies.correctionBudget ?? 1;
418
+ this.maxToolCallsPerInvocation = policies.maxToolCallsPerInvocation ?? 8;
419
+ this.approvedToolCall = hooks.approvedToolCall;
420
+ 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`);
421
+ }
422
+ get terminalError() {
423
+ return this.terminal;
424
+ }
425
+ get pendingApprovalCall() {
426
+ return this.pendingApproval;
427
+ }
428
+ fail(error) {
429
+ if (!this.terminal || error.diagnostics.code === "audit_persistence_failed") this.terminal = error;
430
+ }
431
+ correctionInstruction() {
432
+ return this.pendingCorrection?.instruction;
433
+ }
434
+ approvalFor(input) {
435
+ if (this.exactlyOne && input.proposalCount > 1) {
436
+ this.record({
437
+ rule: "exactly_one",
438
+ decision: "deny",
439
+ toolName: input.toolName
440
+ });
441
+ return {
442
+ type: "denied",
443
+ reason: TOOL_PROPOSAL_CORRECTION
444
+ };
445
+ }
446
+ const allowlist = typeof this.policies.toolAllowlist === "function" ? this.policies.toolAllowlist(input.scope) : this.policies.toolAllowlist;
447
+ if (allowlist && !allowlist.includes(input.toolName)) {
448
+ this.record({
449
+ rule: "allowlist",
450
+ decision: "deny",
451
+ toolName: input.toolName
452
+ });
453
+ return {
454
+ type: "denied",
455
+ reason: `Tool ${input.toolName} is not available under the current policy.`
456
+ };
457
+ }
458
+ if (this.pendingCorrection?.kind === "input-validation" && input.toolName !== this.pendingCorrection.toolName) {
459
+ this.record({
460
+ rule: "input_validation",
461
+ decision: "deny",
462
+ toolName: input.toolName
463
+ });
464
+ this.terminal ??= new AgentRuntimeError("failure", `Provider did not return a corrected ${this.pendingCorrection.toolName} tool call`);
465
+ return {
466
+ type: "denied",
467
+ reason: "The correction must call the same tool with corrected arguments."
468
+ };
469
+ }
470
+ const requiresApproval = this.policies.requireApproval?.includes(input.toolName);
471
+ const matchesApproval = this.approvedToolCall !== void 0 && isSameToolCall(this.approvedToolCall, input.toolName, input.toolInput);
472
+ if (requiresApproval && !matchesApproval) {
473
+ this.pendingApproval ??= {
474
+ toolName: input.toolName,
475
+ input: input.toolInput
476
+ };
477
+ this.record({
478
+ rule: "require_approval",
479
+ decision: "approval_requested",
480
+ toolName: input.toolName,
481
+ detail: { toolCallId: input.toolCallId }
482
+ });
483
+ this.terminal ??= new AgentRuntimeError("failure", `Tool ${input.toolName} requires approval before execution`, {
484
+ code: "approval_required",
485
+ toolName: input.toolName,
486
+ pendingToolCall: this.pendingApproval
487
+ });
488
+ return {
489
+ type: "denied",
490
+ reason: "This tool call requires approval before it can run."
491
+ };
492
+ }
493
+ if (this.approvedToolCalls >= this.maxToolCallsPerInvocation) {
494
+ this.record({
495
+ rule: "max_tool_calls",
496
+ decision: "deny",
497
+ toolName: input.toolName,
498
+ detail: { maxToolCallsPerInvocation: this.maxToolCallsPerInvocation }
499
+ });
500
+ this.terminal = new AgentRuntimeError("failure", "Tool-call budget exceeded");
501
+ return {
502
+ type: "denied",
503
+ reason: "Tool-call budget exhausted."
504
+ };
505
+ }
506
+ if (requiresApproval && matchesApproval) {
507
+ this.approvedToolCall = void 0;
508
+ this.record({
509
+ rule: "require_approval",
510
+ decision: "allow",
511
+ toolName: input.toolName,
512
+ detail: { viaApproval: true }
513
+ });
514
+ }
515
+ this.approvedToolCalls += 1;
516
+ return "not-applicable";
517
+ }
518
+ afterStep(toolCalls) {
519
+ if (this.terminal) return;
520
+ if (this.pendingCorrection) {
521
+ const only = toolCalls.length === 1 ? toolCalls[0] : void 0;
522
+ if (only && !only.invalid) {
523
+ this.pendingCorrection = void 0;
524
+ return;
525
+ }
526
+ if (this.pendingCorrection.correctionsUsed < this.correctionBudget) {
527
+ const chain = this.pendingCorrection.correctionsUsed;
528
+ this.pendingCorrection = void 0;
529
+ this.handleViolation(toolCalls, chain);
530
+ return;
531
+ }
532
+ this.recordInvalidCalls(toolCalls);
533
+ this.exhaustCorrection(toolCalls);
534
+ return;
535
+ }
536
+ this.handleViolation(toolCalls, 0);
537
+ }
538
+ handleViolation(toolCalls, chainCorrections) {
539
+ this.recordInvalidCalls(toolCalls);
540
+ if (this.exactlyOne && toolCalls.length > 1) {
541
+ if (chainCorrections >= this.correctionBudget) {
542
+ this.exhaustCorrection(toolCalls);
543
+ return;
544
+ }
545
+ this.pendingCorrection = {
546
+ kind: "multi-call",
547
+ instruction: TOOL_PROPOSAL_CORRECTION,
548
+ correctionsUsed: chainCorrections + 1
549
+ };
550
+ this.record({
551
+ rule: "exactly_one",
552
+ decision: "correction_issued"
553
+ });
554
+ return;
555
+ }
556
+ const invalid = toolCalls.find((toolCall) => toolCall.invalid);
557
+ if (!invalid) return;
558
+ const correction = this.hooks.correctionForInvalidCall?.(invalid);
559
+ if (correction) {
560
+ if (chainCorrections >= this.correctionBudget) {
561
+ this.exhaustCorrection(toolCalls);
562
+ return;
563
+ }
564
+ this.pendingCorrection = {
565
+ kind: "input-validation",
566
+ instruction: correction,
567
+ toolName: invalid.toolName,
568
+ correctionsUsed: chainCorrections + 1
569
+ };
570
+ this.record({
571
+ rule: "input_validation",
572
+ decision: "correction_issued",
573
+ toolName: invalid.toolName
574
+ });
575
+ return;
576
+ }
577
+ this.terminal = new AgentRuntimeError("failure", "Provider returned invalid tool input");
578
+ }
579
+ recordInvalidCalls(toolCalls) {
580
+ for (const call of toolCalls) if (call.invalid) this.record({
581
+ rule: "input_validation",
582
+ decision: "deny",
583
+ toolName: call.toolName
584
+ });
585
+ }
586
+ exhaustCorrection(toolCalls) {
587
+ this.record({
588
+ rule: "correction_budget",
589
+ decision: "deny",
590
+ detail: { correctionBudget: this.correctionBudget }
591
+ });
592
+ this.terminal = new AgentRuntimeError("failure", toolCalls.length === 0 ? "Provider did not return a corrected tool call" : toolCalls.length > 1 ? "Provider returned multiple tool calls" : "Provider returned invalid tool input", toolCalls.length > 1 ? { code: "multiple_tool_calls" } : void 0);
593
+ }
594
+ record(decision) {
595
+ try {
596
+ this.hooks.onDecision?.(decision);
597
+ } catch {}
598
+ }
599
+ };
600
+ // src/runtime/ai-sdk-runtime.ts
601
+ const TOOL_EXECUTION_CONTRACT = [
602
+ "Tool execution contract:",
603
+ "- Propose exactly one tool call in each assistant response when tools are needed.",
604
+ "- Wait for that tool result before deciding on the next tool call.",
605
+ "- Omit optional arguments when the user or context did not provide a meaningful value.",
606
+ "- Never use empty strings as placeholders for omitted optional arguments."
607
+ ].join("\n");
608
+ function sum(values) {
609
+ return values.reduce((total, value) => total + value, 0);
610
+ }
611
+ function aggregateStepTimings(steps) {
612
+ return {
613
+ providerCallCount: steps.length,
614
+ totalProviderMs: sum(steps.map((step) => step.performance.responseTimeMs)),
615
+ totalToolMs: sum(steps.flatMap((step) => Object.values(step.performance.toolExecutionMs)))
616
+ };
617
+ }
618
+ function auditErrorCode(error) {
619
+ const code = error.diagnostics.code;
620
+ if (typeof code === "string") return code;
621
+ return error.kind === "timeout" ? "tool_call_timeout" : "tool_call_failed";
622
+ }
623
+ function normalizeProviderName(provider) {
624
+ return provider.endsWith(".chat") ? provider.slice(0, -5) : provider;
625
+ }
626
+ function findAgentRuntimeError(error) {
627
+ let current = error;
628
+ for (let depth = 0; depth < 8 && current != null; depth += 1) {
629
+ if (current instanceof AgentRuntimeError) return current;
630
+ if (!(current instanceof Error)) return void 0;
631
+ current = current.cause;
632
+ }
633
+ }
634
+ function isTimeoutFailure(error) {
635
+ let current = error;
636
+ for (let depth = 0; depth < 8 && current != null; depth += 1) {
637
+ if (current instanceof Error && ["AbortError", "TimeoutError"].includes(current.name)) return true;
638
+ if (!(current instanceof Error)) return false;
639
+ current = current.cause;
640
+ }
641
+ return false;
642
+ }
643
+ function zodObjectShape(schema) {
644
+ let current = schema;
645
+ for (let depth = 0; depth < 6 && current != null; depth += 1) {
646
+ if (current instanceof z.ZodObject) return current.shape;
647
+ const def = current._def;
648
+ current = def?.["schema"] ?? def?.["innerType"];
649
+ }
650
+ }
651
+ function dropBlankOptionalStringArguments(rawArguments, inputShape) {
652
+ if (typeof rawArguments !== "object" || rawArguments === null || Array.isArray(rawArguments)) return rawArguments;
653
+ const normalized = { ...rawArguments };
654
+ for (const [key, fieldSchema] of Object.entries(inputShape)) {
655
+ const value = normalized[key];
656
+ if (typeof value === "string" && value.trim() === "" && fieldSchema.safeParse(void 0).success && !fieldSchema.safeParse(value).success) delete normalized[key];
657
+ }
658
+ return normalized;
659
+ }
660
+ function modelMessages(history, input) {
661
+ return [...history.map((message) => ({
662
+ role: message.role,
663
+ content: message.content
664
+ })), {
665
+ role: "user",
666
+ content: input.prompt
667
+ }];
668
+ }
669
+ var AiSdkNativeRuntime = class {
670
+ options;
671
+ now;
672
+ maxDurationMs;
673
+ toolsByName;
674
+ constructor(options) {
675
+ this.options = options;
676
+ this.now = options.now ?? performance.now.bind(performance);
677
+ this.maxDurationMs = options.policies?.maxDurationMs ?? 12e4;
678
+ if (!Number.isSafeInteger(this.maxDurationMs) || this.maxDurationMs <= 0) throw new Error("maxDurationMs must be a positive integer");
679
+ this.toolsByName = new Map((options.tools ?? []).map((definition) => [definition.name, definition]));
680
+ if (this.toolsByName.size > 0 && !options.mcpClient) throw new Error("mcpClient is required when tools are registered (tools execute through MCP)");
681
+ }
682
+ async invoke(input) {
683
+ const startedAt = this.now();
684
+ const state = {
685
+ currentProposalCount: 0,
686
+ toolCallCount: 0
687
+ };
688
+ const policy = new PolicyEngine(this.options.policies ?? {}, {
689
+ onDecision: (decision) => this.options.onPolicyDecision?.({
690
+ ...decision,
691
+ scope: input.scope,
692
+ invocationId: input.invocationId,
693
+ runId: input.runId
694
+ }),
695
+ correctionForInvalidCall: (call) => this.correctionFor(call),
696
+ approvedToolCall: input.approvedToolCall
697
+ });
698
+ try {
699
+ const systemPrompt = this.options.loadSystemPrompt ? await this.options.loadSystemPrompt(input) : "";
700
+ const contextLoadedAt = this.now();
701
+ const history = this.options.loadHistory ? await this.options.loadHistory(input) : [];
702
+ const historyLoadedAt = this.now();
703
+ const baseInstructions = [systemPrompt, this.options.policies?.exactlyOneToolCallPerStep ?? true ? TOOL_EXECUTION_CONTRACT : ""].filter((part) => part.length > 0).join("\n\n");
704
+ const tools = this.createTools(input, startedAt, state, policy);
705
+ const result = await new ToolLoopAgent({
706
+ model: this.options.model,
707
+ tools,
708
+ instructions: baseInstructions,
709
+ repairToolCall: async ({ toolCall }) => this.repairBlankOptionalStrings(toolCall),
710
+ prepareStep: () => {
711
+ this.remaining(startedAt);
712
+ const correction = policy.correctionInstruction();
713
+ return { instructions: correction ? `${baseInstructions}\n\n${correction}` : baseInstructions };
714
+ },
715
+ onLanguageModelCallEnd: (event) => {
716
+ state.currentProposalCount = event.content.filter((part) => part.type === "tool-call").length;
717
+ },
718
+ toolApproval: ({ toolCall }) => policy.approvalFor({
719
+ toolName: toolCall.toolName,
720
+ toolCallId: toolCall.toolCallId,
721
+ toolInput: toolCall.input,
722
+ proposalCount: state.currentProposalCount,
723
+ scope: input.scope
724
+ }),
725
+ onStepEnd: (step) => {
726
+ policy.afterStep(step.toolCalls);
727
+ },
728
+ stopWhen: () => policy.terminalError != null
729
+ }).generate({
730
+ messages: modelMessages(history, input),
731
+ timeout: this.remaining(startedAt)
732
+ });
733
+ if (policy.terminalError) throw policy.terminalError;
734
+ const text = result.text.trim();
735
+ const provider = normalizeProviderName(result.finalStep.model.provider);
736
+ const model = result.finalStep.model.modelId;
737
+ if (!text) throw new AgentRuntimeError("failure", "Provider response contained no assistant text", {
738
+ provider,
739
+ model
740
+ });
741
+ const completedAt = this.now();
742
+ this.emitMetrics({
743
+ runId: input.runId,
744
+ contextLoadMs: contextLoadedAt - startedAt,
745
+ historyLoadMs: historyLoadedAt - contextLoadedAt,
746
+ toolCallCount: state.toolCallCount,
747
+ ...aggregateStepTimings(result.steps),
748
+ totalMs: completedAt - startedAt
749
+ });
750
+ return {
751
+ invocationId: input.invocationId,
752
+ runId: input.runId,
753
+ text,
754
+ provider,
755
+ model
756
+ };
757
+ } catch (error) {
758
+ const runtimeError = findAgentRuntimeError(error);
759
+ if (runtimeError) throw runtimeError;
760
+ if (isTimeoutFailure(error)) throw new AgentRuntimeError("timeout", "Native runtime timed out");
761
+ throw new AgentRuntimeError("failure", "Native runtime failed");
762
+ } finally {
763
+ await state.session?.then((session) => session.close()).catch(() => void 0);
764
+ }
765
+ }
766
+ createTools(input, startedAt, state, policy) {
767
+ return Object.fromEntries([...this.toolsByName.values()].map((definition) => {
768
+ const name = definition.name;
769
+ return [name, tool({
770
+ description: definition.description,
771
+ inputSchema: definition.inputSchema,
772
+ execute: async (arguments_, options) => {
773
+ const audit = {
774
+ dispatchId: randomUUID(),
775
+ scope: input.scope,
776
+ invocationId: input.invocationId,
777
+ runId: input.runId,
778
+ toolName: name,
779
+ toolCallId: options.toolCallId,
780
+ idempotencyKey: deriveToolCallIdempotencyKey(input.invocationId, name, arguments_),
781
+ input: arguments_
782
+ };
783
+ try {
784
+ await this.emitToolCallAudit({
785
+ ...audit,
786
+ phase: "started"
787
+ });
788
+ } catch (error) {
789
+ policy.fail(findAgentRuntimeError(error) ?? new AgentRuntimeError("failure", "Tool call audit failed"));
790
+ throw error;
791
+ }
792
+ let result;
793
+ try {
794
+ state.session ??= this.options.mcpClient.open({
795
+ runId: input.runId,
796
+ timeoutMs: this.remaining(startedAt)
797
+ });
798
+ const session = await state.session;
799
+ state.toolCallCount += 1;
800
+ result = await session.callTool({
801
+ name,
802
+ arguments: arguments_,
803
+ timeoutMs: this.remaining(startedAt),
804
+ idempotencyKey: audit.idempotencyKey
805
+ });
806
+ } catch (error) {
807
+ const agentError = findAgentRuntimeError(error) ?? new AgentRuntimeError("failure", "MCP tool call failed");
808
+ policy.fail(agentError);
809
+ try {
810
+ await this.emitToolCallAudit({
811
+ ...audit,
812
+ phase: "failed",
813
+ errorCode: auditErrorCode(agentError)
814
+ });
815
+ } catch (auditError) {
816
+ policy.fail(findAgentRuntimeError(auditError) ?? agentError);
817
+ throw auditError;
818
+ }
819
+ throw error;
820
+ }
821
+ try {
822
+ await this.emitToolCallAudit({
823
+ ...audit,
824
+ phase: "succeeded"
825
+ });
826
+ } catch (error) {
827
+ policy.fail(findAgentRuntimeError(error) ?? new AgentRuntimeError("failure", "Tool call audit failed"));
828
+ throw error;
829
+ }
830
+ return result;
831
+ }
832
+ })];
833
+ }));
834
+ }
835
+ async emitToolCallAudit(event) {
836
+ if (!this.options.onToolCallAudit) return;
837
+ try {
838
+ await this.options.onToolCallAudit(event);
839
+ } catch (error) {
840
+ throw new AgentRuntimeError("failure", "Tool call audit persistence failed", { code: "audit_persistence_failed" }, { cause: error });
841
+ }
842
+ }
843
+ correctionFor(call) {
844
+ const definition = this.toolsByName.get(call.toolName);
845
+ if (!definition?.validateInput) return void 0;
846
+ const shape = zodObjectShape(definition.inputSchema);
847
+ const normalized = shape ? dropBlankOptionalStringArguments(call.input, shape) : call.input;
848
+ const verdict = definition.validateInput(normalized);
849
+ return verdict.ok ? void 0 : verdict.correction;
850
+ }
851
+ repairBlankOptionalStrings(toolCall) {
852
+ const definition = this.toolsByName.get(toolCall.toolName);
853
+ if (!definition) return null;
854
+ const shape = zodObjectShape(definition.inputSchema);
855
+ if (!shape) return null;
856
+ let parsed;
857
+ try {
858
+ parsed = JSON.parse(toolCall.input);
859
+ } catch {
860
+ return null;
861
+ }
862
+ const normalized = dropBlankOptionalStringArguments(parsed, shape);
863
+ if (JSON.stringify(normalized) === JSON.stringify(parsed)) return null;
864
+ return {
865
+ type: "tool-call",
866
+ toolCallType: "function",
867
+ toolCallId: toolCall.toolCallId,
868
+ toolName: toolCall.toolName,
869
+ input: JSON.stringify(normalized)
870
+ };
871
+ }
872
+ remaining(startedAt) {
873
+ const remaining = this.maxDurationMs - (this.now() - startedAt);
874
+ if (remaining <= 0) throw new AgentRuntimeError("timeout", "Native runtime timed out");
875
+ return Math.max(1, Math.floor(remaining));
876
+ }
877
+ emitMetrics(metrics) {
878
+ try {
879
+ this.options.onMetrics?.(metrics);
880
+ } catch {}
881
+ }
882
+ };
883
+ // src/runtime/model.ts
884
+ function required(value, name) {
885
+ if (!value.trim()) throw new Error(`${name} is required`);
886
+ return value;
887
+ }
888
+ function withProviderTimeout(fetchImpl, timeoutMs) {
889
+ return async (input, init) => {
890
+ const timeoutSignal = AbortSignal.timeout(timeoutMs);
891
+ const signal = init?.signal ? AbortSignal.any([init.signal, timeoutSignal]) : timeoutSignal;
892
+ return fetchImpl(input, {
893
+ ...init,
894
+ signal
895
+ });
896
+ };
897
+ }
898
+ function openAiCompatible(config, fetchImpl = fetch) {
899
+ const baseUrl = required(config.baseUrl, "baseUrl");
900
+ const apiKey = required(config.apiKey, "apiKey");
901
+ const model = required(config.model, "model");
902
+ const timeoutMs = config.timeoutMs ?? 3e4;
903
+ if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0) throw new Error("timeoutMs must be a positive integer");
904
+ const endpoint = new URL(baseUrl);
905
+ if (!["http:", "https:"].includes(endpoint.protocol)) throw new Error("baseUrl must use http or https");
906
+ return createOpenAICompatible({
907
+ name: "openai-compatible",
908
+ baseURL: baseUrl,
909
+ apiKey,
910
+ fetch: withProviderTimeout(fetchImpl, timeoutMs),
911
+ transformRequestBody: (body) => Array.isArray(body.tools) ? {
912
+ ...body,
913
+ parallel_tool_calls: false
914
+ } : body
915
+ }).chatModel(model);
916
+ }
917
+ // src/define-runtime.ts
918
+ const RUNTIME_INTERNALS = Symbol("runwarden/internals");
919
+ function buildMcpClient(servers, policies) {
920
+ if (!servers || servers.length === 0) return void 0;
921
+ const clients = servers.map((server) => new NativeMcpClient({
922
+ endpoint: server.url,
923
+ headers: server.headers,
924
+ maxResultBytes: policies?.maxToolResultBytes
925
+ }));
926
+ if (clients.length === 1) return clients[0];
927
+ return new RoutingMcpClient(clients, servers);
928
+ }
929
+ var RoutingMcpClient = class {
930
+ clients;
931
+ servers;
932
+ constructor(clients, servers) {
933
+ this.clients = clients;
934
+ this.servers = servers;
935
+ }
936
+ async open(input) {
937
+ const opened = await Promise.allSettled(this.clients.map((client) => client.open(input)));
938
+ const sessions = opened.flatMap((result) => result.status === "fulfilled" ? [result.value] : []);
939
+ const failure = opened.find((result) => result.status === "rejected");
940
+ if (failure) {
941
+ await Promise.allSettled(sessions.map((session) => session.close()));
942
+ throw failure.reason;
943
+ }
944
+ const route = (toolName) => {
945
+ const index = this.servers.findIndex((server) => server.tools?.includes(toolName));
946
+ return sessions[index >= 0 ? index : 0];
947
+ };
948
+ return {
949
+ callTool: (call) => route(call.name).callTool(call),
950
+ close: async () => {
951
+ await Promise.allSettled(sessions.map((session) => session.close()));
952
+ }
953
+ };
954
+ }
955
+ };
956
+ const sleep$1 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
957
+ function dbRequired(operation) {
958
+ return new AgentRuntimeError("failure", `${operation} requires a database: pass createDb() as defineRuntime({ db })`, { code: "db_required" });
959
+ }
960
+ function defineRuntime(options) {
961
+ const model = options.model ?? (options.provider ? openAiCompatible(options.provider) : void 0);
962
+ if (!model) throw new Error("defineRuntime requires either `model` or `provider`");
963
+ const mcpClient = options.mcpClient ?? buildMcpClient(options.mcpServers, options.policies);
964
+ const decisionListeners = /* @__PURE__ */ new Set();
965
+ const auditListeners = /* @__PURE__ */ new Set();
966
+ const engine = new AiSdkNativeRuntime({
967
+ model,
968
+ tools: options.tools,
969
+ mcpClient,
970
+ loadSystemPrompt: options.loadSystemPrompt,
971
+ loadHistory: options.loadHistory,
972
+ policies: options.policies,
973
+ onMetrics: options.onMetrics,
974
+ onPolicyDecision: (decision) => {
975
+ for (const listener of decisionListeners) listener(decision);
976
+ options.onPolicyDecision?.(decision);
977
+ },
978
+ onToolCallAudit: async (event) => {
979
+ for (const listener of auditListeners) await listener(event);
980
+ try {
981
+ await options.onToolCallAudit?.(event);
982
+ } catch {}
983
+ }
984
+ });
985
+ const db = options.db;
986
+ async function notifyInvocationSettled(invocation) {
987
+ try {
988
+ await options.onInvocationSettled?.(invocation);
989
+ } catch {}
990
+ }
991
+ async function waitForInvocation(invocationId, opts) {
992
+ if (!db) throw dbRequired("waitForInvocation");
993
+ const deadline = Date.now() + (opts?.waitTimeoutMs ?? 3e5);
994
+ const pollIntervalMs = opts?.pollIntervalMs ?? 250;
995
+ for (;;) {
996
+ const row = await getInvocationRowById(db.db, invocationId);
997
+ if (!row) throw new AgentRuntimeError("failure", `Invocation ${invocationId} was not found`, {
998
+ code: "invocation_not_found",
999
+ invocationId
1000
+ });
1001
+ if (row.status === "succeeded") return {
1002
+ invocationId,
1003
+ runId: row.activeRunId,
1004
+ text: row.resultText ?? "",
1005
+ provider: row.provider ?? void 0,
1006
+ model: row.model ?? void 0
1007
+ };
1008
+ if (row.status === "awaiting_approval") {
1009
+ const pending = toPendingToolCall(row.pendingToolCall);
1010
+ throw new AgentRuntimeError("failure", `Invocation ${invocationId} is awaiting approval`, {
1011
+ code: "approval_required",
1012
+ invocationId,
1013
+ ...pending ? { toolName: pending.toolName } : {}
1014
+ });
1015
+ }
1016
+ if (row.status === "failed" || row.status === "cancelled") throw new AgentRuntimeError("failure", row.lastErrorMessage ?? `Invocation ${row.status}`, {
1017
+ code: row.lastErrorCode ?? `invocation_${row.status}`,
1018
+ invocationId
1019
+ });
1020
+ if (Date.now() >= deadline) throw new AgentRuntimeError("timeout", `Timed out waiting for invocation ${invocationId}`, { invocationId });
1021
+ await sleep$1(pollIntervalMs);
1022
+ }
1023
+ }
1024
+ async function durableInvoke(input, opts) {
1025
+ const { invocationId } = await enqueueInvocation(db.db, input);
1026
+ return waitForInvocation(invocationId, opts);
1027
+ }
1028
+ async function resolveApproval(id, resolution, opts) {
1029
+ if (!db) throw dbRequired(`${resolution} resolution`);
1030
+ const result = await resolveInvocationApproval(db.db, id, resolution, opts);
1031
+ if (result.outcome === "not_found") throw new AgentRuntimeError("failure", `Invocation ${id} was not found`, {
1032
+ code: "invocation_not_found",
1033
+ invocationId: id
1034
+ });
1035
+ if (result.outcome === "not_awaiting") throw new AgentRuntimeError("failure", `Invocation ${id} is not awaiting approval`, {
1036
+ code: "invocation_not_awaiting_approval",
1037
+ invocationId: id
1038
+ });
1039
+ const record = toRecord(result.row);
1040
+ if (resolution === "rejected") await notifyInvocationSettled(record);
1041
+ return record;
1042
+ }
1043
+ return {
1044
+ engine,
1045
+ invoke: db ? durableInvoke : (input) => engine.invoke({
1046
+ ...input,
1047
+ invocationId: randomUUID(),
1048
+ runId: randomUUID()
1049
+ }),
1050
+ enqueue: async (input) => {
1051
+ if (!db) throw dbRequired("enqueue");
1052
+ return enqueueInvocation(db.db, input);
1053
+ },
1054
+ getInvocation: async (id) => {
1055
+ if (!db) throw dbRequired("getInvocation");
1056
+ const row = await getInvocationRowById(db.db, id);
1057
+ return row ? toRecord(row) : void 0;
1058
+ },
1059
+ waitForInvocation,
1060
+ listPendingApprovals: async (scope) => {
1061
+ if (!db) throw dbRequired("listPendingApprovals");
1062
+ return (await listAwaitingApprovalRows(db.db, scope)).map(toRecord);
1063
+ },
1064
+ approveInvocation: (id, opts) => resolveApproval(id, "approved", opts),
1065
+ rejectInvocation: (id, opts) => resolveApproval(id, "rejected", opts),
1066
+ [RUNTIME_INTERNALS]: {
1067
+ onPolicyDecision: (listener) => {
1068
+ decisionListeners.add(listener);
1069
+ return () => {
1070
+ decisionListeners.delete(listener);
1071
+ };
1072
+ },
1073
+ onToolCallAudit: (listener) => {
1074
+ auditListeners.add(listener);
1075
+ return () => {
1076
+ auditListeners.delete(listener);
1077
+ };
1078
+ },
1079
+ notifyInvocationSettled
1080
+ }
1081
+ };
1082
+ }
1083
+ // src/db/repositories/decisions.ts
1084
+ async function insertPolicyDecision(db, decision) {
1085
+ await db.insert(policyDecisions).values({
1086
+ scope: decision.scope,
1087
+ invocationId: Number(decision.invocationId),
1088
+ runId: decision.runId,
1089
+ toolName: decision.toolName ?? null,
1090
+ rule: decision.rule,
1091
+ decision: decision.decision,
1092
+ detail: decision.detail ?? null
1093
+ });
1094
+ }
1095
+ // src/db/repositories/tool-call-audits.ts
1096
+ async function claimToolCallAudit(db, claim) {
1097
+ const first = (await db.execute(sql`
1098
+ insert into agent_runtime.tool_call_audits
1099
+ (invocation_id, scope, run_id, tool_name, idempotency_key, status,
1100
+ requested_at, request_snapshot, metadata)
1101
+ values (
1102
+ ${claim.invocationId},
1103
+ ${claim.scope},
1104
+ ${claim.runId},
1105
+ ${claim.toolName},
1106
+ ${claim.idempotencyKey},
1107
+ 'started',
1108
+ now(),
1109
+ ${JSON.stringify(claim.requestSnapshot)}::jsonb,
1110
+ ${JSON.stringify({
1111
+ ...claim.metadata,
1112
+ dispatchId: claim.dispatchId
1113
+ })}::jsonb
1114
+ )
1115
+ on conflict (scope, tool_name, idempotency_key)
1116
+ where idempotency_key is not null
1117
+ do update set status = 'started',
1118
+ requested_at = now(),
1119
+ completed_at = null,
1120
+ error_code = null,
1121
+ run_id = excluded.run_id,
1122
+ request_snapshot = excluded.request_snapshot,
1123
+ metadata = excluded.metadata
1124
+ returning id
1125
+ `)).rows[0];
1126
+ if (!first) throw new Error("tool call audit claim inserted no row");
1127
+ return Number(first.id);
1128
+ }
1129
+ async function completeToolCallAudit(db, id, status, errorCode, runId, dispatchId) {
1130
+ return (await db.execute(sql`
1131
+ update agent_runtime.tool_call_audits
1132
+ set status = ${status},
1133
+ completed_at = now(),
1134
+ error_code = ${errorCode ?? null}
1135
+ where id = ${id} and status = 'started'
1136
+ and run_id = ${runId}
1137
+ and metadata->>'dispatchId' = ${dispatchId}
1138
+ returning id
1139
+ `)).rows.length > 0;
1140
+ }
1141
+ // src/worker/run-worker.ts
1142
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
1143
+ function toAgentError(error) {
1144
+ if (error instanceof AgentRuntimeError) return error;
1145
+ return new AgentRuntimeError("failure", error instanceof Error ? error.message : String(error), { code: "execution_failed" }, error instanceof Error ? { cause: error } : {});
1146
+ }
1147
+ function errorCode(error) {
1148
+ const code = error.diagnostics.code;
1149
+ return typeof code === "string" ? code : "execution_failed";
1150
+ }
1151
+ function pendingFromError(error) {
1152
+ const pending = toPendingToolCall(error.diagnostics.pendingToolCall);
1153
+ if (pending) return pending;
1154
+ const toolName = error.diagnostics.toolName;
1155
+ return {
1156
+ toolName: typeof toolName === "string" ? toolName : "unknown",
1157
+ input: void 0
1158
+ };
1159
+ }
1160
+ function runWorker(options) {
1161
+ const workerId = options.workerId ?? `${hostname()}:${process.pid}:${randomUUID()}`;
1162
+ const leaseTimeoutMs = options.leaseTimeoutMs ?? 12e4;
1163
+ const heartbeatIntervalMs = options.heartbeatIntervalMs ?? 3e4;
1164
+ const retryDelayMs = options.retryDelayMs ?? 6e4;
1165
+ const pollIdleMs = options.pollIdleMs ?? 500;
1166
+ const pollActiveMs = options.pollActiveMs ?? 250;
1167
+ const db = options.db.db;
1168
+ const internals = options.runtime[RUNTIME_INTERNALS];
1169
+ let stopped = false;
1170
+ function guardLease(row) {
1171
+ let lost = false;
1172
+ async function beat() {
1173
+ try {
1174
+ const owned = await heartbeatInvocation(db, row.id, workerId, row.activeRunId, leaseTimeoutMs);
1175
+ if (!owned) lost = true;
1176
+ return owned && !lost;
1177
+ } catch {
1178
+ return false;
1179
+ }
1180
+ }
1181
+ const interval = setInterval(() => void beat(), heartbeatIntervalMs);
1182
+ interval.unref?.();
1183
+ return {
1184
+ beat,
1185
+ get isLost() {
1186
+ return lost;
1187
+ },
1188
+ stop: () => clearInterval(interval)
1189
+ };
1190
+ }
1191
+ async function executeOne(row, lease) {
1192
+ const record = toRecord(row);
1193
+ const auditWrites = [];
1194
+ let auditError;
1195
+ const unsubscribe = internals?.onPolicyDecision((decision) => {
1196
+ if (decision.invocationId !== String(row.id) || decision.runId !== row.activeRunId) return;
1197
+ auditWrites.push(insertPolicyDecision(db, decision).catch((error) => {
1198
+ auditError = error;
1199
+ }));
1200
+ });
1201
+ const auditIds = /* @__PURE__ */ new Map();
1202
+ const unsubscribeAudit = internals?.onToolCallAudit?.(async (event) => {
1203
+ if (event.invocationId !== String(row.id) || event.runId !== row.activeRunId) return;
1204
+ if (event.phase === "started") {
1205
+ const auditId = await claimToolCallAudit(db, {
1206
+ invocationId: event.invocationId,
1207
+ scope: event.scope,
1208
+ runId: event.runId,
1209
+ dispatchId: event.dispatchId,
1210
+ toolName: event.toolName,
1211
+ idempotencyKey: event.idempotencyKey,
1212
+ requestSnapshot: event.input ?? null,
1213
+ metadata: event.toolCallId ? { toolCallId: event.toolCallId } : void 0
1214
+ });
1215
+ auditIds.set(event.dispatchId, auditId);
1216
+ return;
1217
+ }
1218
+ const auditId = auditIds.get(event.dispatchId);
1219
+ if (auditId === void 0) throw new Error("Tool audit completion has no started event");
1220
+ if (!await completeToolCallAudit(db, auditId, event.phase, event.errorCode, event.runId, event.dispatchId)) throw new Error("Tool audit completion lost its dispatch claim");
1221
+ auditIds.delete(event.dispatchId);
1222
+ });
1223
+ try {
1224
+ let result;
1225
+ try {
1226
+ result = await options.runtime.engine.invoke({
1227
+ ...row.input,
1228
+ invocationId: String(row.id),
1229
+ runId: row.activeRunId,
1230
+ approvedToolCall: toPendingToolCall(row.pendingToolCall)
1231
+ });
1232
+ } finally {
1233
+ unsubscribe?.();
1234
+ unsubscribeAudit?.();
1235
+ await Promise.all(auditWrites);
1236
+ if (auditError) throw new AgentRuntimeError("failure", "Policy audit persistence failed", { code: "audit_persistence_failed" }, { cause: auditError });
1237
+ }
1238
+ if (lease.isLost) return;
1239
+ if (await markInvocationSucceeded(db, row.id, workerId, row.activeRunId, {
1240
+ resultText: result.text,
1241
+ provider: result.provider,
1242
+ model: result.model
1243
+ })) {
1244
+ const settledRecord = await getInvocationRowById(db, String(row.id)).then((settledRow) => settledRow ? toRecord(settledRow) : {
1245
+ ...record,
1246
+ status: "succeeded",
1247
+ resultText: result.text
1248
+ }, () => ({
1249
+ ...record,
1250
+ status: "succeeded",
1251
+ resultText: result.text
1252
+ }));
1253
+ await internals?.notifyInvocationSettled?.(settledRecord);
1254
+ await options.onSucceeded?.({
1255
+ ...record,
1256
+ status: "succeeded",
1257
+ resultText: result.text
1258
+ }, result);
1259
+ }
1260
+ } catch (error) {
1261
+ if (lease.isLost) return;
1262
+ const agentError = toAgentError(error);
1263
+ if (errorCode(agentError) === "approval_required") {
1264
+ const pendingToolCall = pendingFromError(agentError);
1265
+ const parked = await markInvocationAwaitingApproval(db, row.id, workerId, row.activeRunId, pendingToolCall);
1266
+ if (parked) await options.onAwaitingApproval?.(toRecord(parked), pendingToolCall);
1267
+ return;
1268
+ }
1269
+ const updated = await markInvocationFailed(db, row.id, workerId, row.activeRunId, {
1270
+ code: errorCode(agentError),
1271
+ message: agentError.message
1272
+ }, retryDelayMs);
1273
+ if (updated?.status === "failed") {
1274
+ await internals?.notifyInvocationSettled?.(toRecord(updated));
1275
+ await options.onFailed?.(toRecord(updated), agentError);
1276
+ }
1277
+ }
1278
+ }
1279
+ const loop = (async () => {
1280
+ while (!stopped) {
1281
+ let claimed = [];
1282
+ try {
1283
+ try {
1284
+ const result = await claimInvocations(db, workerId, {
1285
+ leaseTimeoutMs,
1286
+ maxBatchSize: 1
1287
+ });
1288
+ claimed = result.claimed.map((row) => ({
1289
+ row,
1290
+ lease: guardLease(row)
1291
+ }));
1292
+ for (const exhaustedRow of result.exhausted) await internals?.notifyInvocationSettled?.(toRecord(exhaustedRow));
1293
+ } catch {
1294
+ await sleep(pollIdleMs);
1295
+ continue;
1296
+ }
1297
+ for (const { row, lease } of claimed) {
1298
+ if (stopped) break;
1299
+ if (!await lease.beat()) continue;
1300
+ if (stopped) break;
1301
+ await executeOne(row, lease);
1302
+ }
1303
+ } finally {
1304
+ for (const { lease } of claimed) lease.stop();
1305
+ }
1306
+ await sleep(claimed.length === 0 ? pollIdleMs : pollActiveMs);
1307
+ }
1308
+ })();
1309
+ return { async stop() {
1310
+ stopped = true;
1311
+ await loop;
1312
+ } };
1313
+ }
1314
+ // src/fake-runtime.ts
1315
+ var FakeAgentRuntime = class {
1316
+ result;
1317
+ invocations = [];
1318
+ constructor(result) {
1319
+ this.result = result;
1320
+ }
1321
+ getInvocationHistory() {
1322
+ return [...this.invocations];
1323
+ }
1324
+ async invoke(input) {
1325
+ const invocationIndex = this.invocations.push(input);
1326
+ if (typeof this.result === "function") return this.result(input, invocationIndex);
1327
+ return this.result ?? {
1328
+ invocationId: input.invocationId,
1329
+ runId: input.runId,
1330
+ text: `Fake runtime response to: ${input.prompt}`,
1331
+ provider: "fake",
1332
+ model: "fake-agent-runtime"
1333
+ };
1334
+ }
1335
+ };
1336
+ export { AgentRuntimeError, ERROR_CATEGORY_META_KEY, FakeAgentRuntime, NativeMcpClient, RUNTIME_NAME, RUN_ID_HEADER, createDb, defineRuntime, initDb, openAiCompatible, runWorker };