stitchkit 0.100.2 → 0.101.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/CHANGELOG.md +153 -0
- package/README.md +2 -1
- package/dist/entrypoints/agent-runtime/browser.js +119 -119
- package/dist/entrypoints/agent-runtime/coding-tools.js +15 -20
- package/dist/entrypoints/agent-runtime/harness.js +55 -63
- package/dist/entrypoints/agent-runtime/openrouter.js +6 -6
- package/dist/entrypoints/agent-runtime/sandbox.js +20 -25
- package/dist/entrypoints/agent-runtime/sqlite/bun.js +11 -13
- package/dist/entrypoints/agent-runtime/sqlite/node.js +11 -13
- package/dist/entrypoints/agent-runtime/testing.js +32 -34
- package/dist/entrypoints/agent-runtime.js +395 -402
- package/dist/entrypoints/application/diagnostic-journal.js +26 -27
- package/dist/entrypoints/application/directory-inbox.js +16 -17
- package/dist/entrypoints/application/grammy.js +13 -13
- package/dist/entrypoints/application/opentelemetry.js +13 -13
- package/dist/entrypoints/application/schemas.js +24 -24
- package/dist/entrypoints/application.js +209 -210
- package/dist/entrypoints/bin/upgrade-cli.js +2 -2
- package/dist/entrypoints/cli.js +81 -84
- package/dist/entrypoints/contract.js +54 -57
- package/dist/entrypoints/declaration.js +22 -22
- package/dist/entrypoints/files.js +18 -21
- package/dist/entrypoints/geo.js +7 -8
- package/dist/entrypoints/google.js +2 -2
- package/dist/entrypoints/index.js +150 -154
- package/dist/entrypoints/live.js +45 -47
- package/dist/entrypoints/node.js +68 -74
- package/dist/entrypoints/oauth.js +6 -6
- package/dist/entrypoints/observability.d.ts +1 -0
- package/dist/entrypoints/observability.d.ts.map +1 -1
- package/dist/entrypoints/observability.js +116 -87
- package/dist/entrypoints/primitives.js +35 -35
- package/dist/entrypoints/react/keyboard.d.ts +4 -0
- package/dist/entrypoints/react/keyboard.d.ts.map +1 -0
- package/dist/entrypoints/react/keyboard.js +397 -0
- package/dist/entrypoints/react.js +10 -10
- package/dist/entrypoints/release.js +7 -7
- package/dist/entrypoints/remote.js +15 -17
- package/dist/entrypoints/server.js +198 -200
- package/dist/entrypoints/telegram/html.d.ts +5 -0
- package/dist/entrypoints/telegram/html.d.ts.map +1 -0
- package/dist/entrypoints/telegram/html.js +26 -0
- package/dist/entrypoints/telegram/screens.d.ts +14 -0
- package/dist/entrypoints/telegram/screens.d.ts.map +1 -0
- package/dist/entrypoints/telegram/screens.js +2028 -0
- package/dist/entrypoints/telegram.d.ts +11 -3
- package/dist/entrypoints/telegram.d.ts.map +1 -1
- package/dist/entrypoints/telegram.js +779 -159
- package/dist/entrypoints/testing.js +75 -79
- package/dist/entrypoints/tools/connections.js +30 -32
- package/dist/entrypoints/tools/contract.js +18 -20
- package/dist/entrypoints/tools/invoker.js +12 -17
- package/dist/entrypoints/tools.js +224 -229
- package/dist/entrypoints/tracking/server.js +6 -6
- package/dist/entrypoints/tracking.js +40 -42
- package/dist/entrypoints/voice.js +3 -3
- package/dist/{index-eaysmdtq.js → index-0ndk4555.js} +56 -56
- package/dist/{index-2bxbyab0.js → index-1bx1xv1k.js} +1 -1
- package/dist/{index-p5r7qpdd.js → index-1eeqpadm.js} +4 -4
- package/dist/{index-hadmp193.js → index-3ct7hhq6.js} +210 -210
- package/dist/{index-e01r054k.js → index-45tdvqa3.js} +26 -26
- package/dist/{index-dce5wxcq.js → index-4s0fep5q.js} +8 -8
- package/dist/index-50ejfz75.js +9 -0
- package/dist/{index-m9zn6cpq.js → index-5107q189.js} +12 -12
- package/dist/{index-sbdmyz75.js → index-5cc6r990.js} +3 -3
- package/dist/{index-c6bxbsnc.js → index-7kxw1awn.js} +2 -2
- package/dist/{index-0r0fbk7z.js → index-7z9ym7t8.js} +28 -28
- package/dist/{index-p7hgdyhb.js → index-814xpr6d.js} +2 -2
- package/dist/{index-gtj3zc53.js → index-8kzct1d6.js} +99 -99
- package/dist/{index-1zbbzgfh.js → index-9pw0a51a.js} +4 -4
- package/dist/{index-79z7m7dt.js → index-a9pd417t.js} +35 -35
- package/dist/{index-ham5sz8m.js → index-aaj97r4y.js} +45 -45
- package/dist/{index-s812ycat.js → index-aan6hqb9.js} +3 -3
- package/dist/{index-10x2hbx0.js → index-adhgd4pe.js} +44 -44
- package/dist/{index-2gxzjvyr.js → index-afjvjyck.js} +23 -23
- package/dist/{index-qmy6zq75.js → index-ahvsksw5.js} +16 -16
- package/dist/{index-hr4tzbwq.js → index-ap3gqe56.js} +3 -3
- package/dist/{index-znfs20ss.js → index-b2bz0kdm.js} +16 -16
- package/dist/{index-r066jx76.js → index-c6aagrbd.js} +148 -120
- package/dist/{index-k30h66yd.js → index-ck71tbcf.js} +9 -9
- package/dist/{index-eqqvgdb3.js → index-ct75c3at.js} +2 -2
- package/dist/{index-gw8bmyaj.js → index-djjhhgjk.js} +41 -41
- package/dist/{index-8qt2m66q.js → index-dvwj1f43.js} +43 -43
- package/dist/index-e05cjgtf.js +144 -0
- package/dist/{index-yw4fzmdz.js → index-e6c2spsd.js} +79 -30
- package/dist/{index-fhwqwxe0.js → index-ej331hah.js} +1 -1
- package/dist/{index-9t3dffv2.js → index-ewbaywk5.js} +5 -5
- package/dist/{index-7p1f0vp2.js → index-fyphpknh.js} +25 -25
- package/dist/{index-83bdg97s.js → index-g149e9pk.js} +1 -1
- package/dist/{index-ngfhvfpd.js → index-g6a3na27.js} +4 -4
- package/dist/{index-qwhynv6x.js → index-h124epcf.js} +20 -20
- package/dist/{index-h6eeys1v.js → index-h97tqqp1.js} +9 -9
- package/dist/{index-zcgf3gqf.js → index-hby4qhax.js} +10 -10
- package/dist/{index-5jrd0yng.js → index-j8aw2c0e.js} +4 -4
- package/dist/{index-d8m8zpd8.js → index-jzczzcsb.js} +14 -14
- package/dist/index-kqnd6a85.js +26 -0
- package/dist/{index-35mh5bhj.js → index-kw1ws0cs.js} +12 -12
- package/dist/{index-4gcnakhx.js → index-m38ej7e5.js} +2 -2
- package/dist/{index-ndddxtyx.js → index-m79yr938.js} +19 -19
- package/dist/{index-5p2m7we1.js → index-m9fndta6.js} +11 -11
- package/dist/index-mnkk1ffz.js +150 -0
- package/dist/index-n15jny5b.js +4 -0
- package/dist/{index-2vtcmrj9.js → index-n53mswt3.js} +20 -20
- package/dist/{index-bt14acb5.js → index-n5yqxd3d.js} +21 -21
- package/dist/{index-bm5rx7n6.js → index-n9tyz8z3.js} +12 -12
- package/dist/index-nmaqxdx7.js +463 -0
- package/dist/{index-mqv3b5rh.js → index-nx6eazt0.js} +30 -30
- package/dist/{index-3z73fh2c.js → index-q0th8q0z.js} +9 -9
- package/dist/{index-v260df89.js → index-qjeggp8w.js} +20 -20
- package/dist/{index-zfrhfn82.js → index-r1z0rd9c.js} +20 -20
- package/dist/{index-wqgjfstn.js → index-r5c4fedv.js} +3 -3
- package/dist/{index-cyktc4bp.js → index-r5hsgkz5.js} +118 -118
- package/dist/{index-0b1ess5e.js → index-r7a7r7ha.js} +1 -1
- package/dist/{index-1j8pm5t0.js → index-rfkh44ss.js} +15 -15
- package/dist/index-rz3z13e9.js +47 -0
- package/dist/{index-k6gpggb9.js → index-s80awwsw.js} +35 -35
- package/dist/{index-vqr7f7v8.js → index-ssmd41kh.js} +124 -124
- package/dist/index-sznqf8fx.js +20 -0
- package/dist/{index-ensd0r74.js → index-v14qqmys.js} +41 -41
- package/dist/{index-paz6sgdp.js → index-v4a2fjtk.js} +36 -36
- package/dist/{index-d9pk3td4.js → index-w0eqdhyd.js} +14 -14
- package/dist/{index-5qn1kx34.js → index-w575evhq.js} +29 -29
- package/dist/{index-3gye9wzb.js → index-w5s3xftz.js} +26 -26
- package/dist/{index-tm88avnj.js → index-wykebpn9.js} +3 -3
- package/dist/{index-sh3s86na.js → index-xkhk5d6g.js} +17 -17
- package/dist/{index-6k1937bx.js → index-xqhede6x.js} +4 -4
- package/dist/{index-f4fx2tza.js → index-zs4ms3th.js} +181 -181
- package/dist/{index-k5hsfc3j.js → index-zsvzhjtk.js} +1 -1
- package/dist/internal/secret-shapes.d.ts +21 -0
- package/dist/internal/secret-shapes.d.ts.map +1 -0
- package/dist/observability/bounded-logger.d.ts +2 -0
- package/dist/observability/bounded-logger.d.ts.map +1 -1
- package/dist/observability/sanitize.d.ts +6 -0
- package/dist/observability/sanitize.d.ts.map +1 -1
- package/dist/observability/secret-values.d.ts +25 -0
- package/dist/observability/secret-values.d.ts.map +1 -0
- package/dist/react/keyboard/hooks.d.ts +71 -0
- package/dist/react/keyboard/hooks.d.ts.map +1 -0
- package/dist/react/keyboard/layers.d.ts +98 -0
- package/dist/react/keyboard/layers.d.ts.map +1 -0
- package/dist/react/keyboard/zone.d.ts +139 -0
- package/dist/react/keyboard/zone.d.ts.map +1 -0
- package/dist/telegram/bot-api.d.ts +7 -4
- package/dist/telegram/bot-api.d.ts.map +1 -1
- package/dist/telegram/crypto.d.ts +16 -0
- package/dist/telegram/crypto.d.ts.map +1 -0
- package/dist/telegram/html/check.d.ts +26 -0
- package/dist/telegram/html/check.d.ts.map +1 -0
- package/dist/telegram/html/nodes.d.ts +70 -0
- package/dist/telegram/html/nodes.d.ts.map +1 -0
- package/dist/telegram/html/parse.d.ts +35 -0
- package/dist/telegram/html/parse.d.ts.map +1 -0
- package/dist/telegram/html/render.d.ts +39 -0
- package/dist/telegram/html/render.d.ts.map +1 -0
- package/dist/telegram/init-data.d.ts +3 -44
- package/dist/telegram/init-data.d.ts.map +1 -1
- package/dist/telegram/operator-channel.d.ts +19 -3
- package/dist/telegram/operator-channel.d.ts.map +1 -1
- package/dist/telegram/operator-dedupe.d.ts +44 -0
- package/dist/telegram/operator-dedupe.d.ts.map +1 -0
- package/dist/telegram/screens/callback-codec.d.ts +34 -0
- package/dist/telegram/screens/callback-codec.d.ts.map +1 -0
- package/dist/telegram/screens/dispatch.d.ts +22 -0
- package/dist/telegram/screens/dispatch.d.ts.map +1 -0
- package/dist/telegram/screens/display.d.ts +70 -0
- package/dist/telegram/screens/display.d.ts.map +1 -0
- package/dist/telegram/screens/execute.d.ts +57 -0
- package/dist/telegram/screens/execute.d.ts.map +1 -0
- package/dist/telegram/screens/html.d.ts +36 -0
- package/dist/telegram/screens/html.d.ts.map +1 -0
- package/dist/telegram/screens/outcome.d.ts +74 -0
- package/dist/telegram/screens/outcome.d.ts.map +1 -0
- package/dist/telegram/screens/path.d.ts +29 -0
- package/dist/telegram/screens/path.d.ts.map +1 -0
- package/dist/telegram/screens/reconcile.d.ts +67 -0
- package/dist/telegram/screens/reconcile.d.ts.map +1 -0
- package/dist/telegram/screens/registry.d.ts +23 -0
- package/dist/telegram/screens/registry.d.ts.map +1 -0
- package/dist/telegram/screens/render.d.ts +70 -0
- package/dist/telegram/screens/render.d.ts.map +1 -0
- package/dist/telegram/screens/runtime.d.ts +133 -0
- package/dist/telegram/screens/runtime.d.ts.map +1 -0
- package/dist/telegram/screens/screen-types.d.ts +130 -0
- package/dist/telegram/screens/screen-types.d.ts.map +1 -0
- package/dist/telegram/screens/screen.d.ts +138 -0
- package/dist/telegram/screens/screen.d.ts.map +1 -0
- package/dist/telegram/screens/state.d.ts +115 -0
- package/dist/telegram/screens/state.d.ts.map +1 -0
- package/dist/telegram/screens/telegram-screens.d.ts +18 -0
- package/dist/telegram/screens/telegram-screens.d.ts.map +1 -0
- package/dist/telegram/screens/test-chat-telegram.d.ts +47 -0
- package/dist/telegram/screens/test-chat-telegram.d.ts.map +1 -0
- package/dist/telegram/screens/test-chat.d.ts +66 -0
- package/dist/telegram/screens/test-chat.d.ts.map +1 -0
- package/dist/telegram/screens/view.d.ts +89 -0
- package/dist/telegram/screens/view.d.ts.map +1 -0
- package/dist/telegram/send-failure.d.ts +25 -0
- package/dist/telegram/send-failure.d.ts.map +1 -1
- package/dist/telegram/update-intake.d.ts +82 -0
- package/dist/telegram/update-intake.d.ts.map +1 -0
- package/dist/telegram/update-store-check.d.ts +22 -0
- package/dist/telegram/update-store-check.d.ts.map +1 -0
- package/dist/telegram/update-store-postgres.d.ts +33 -0
- package/dist/telegram/update-store-postgres.d.ts.map +1 -0
- package/dist/telegram/update-store-sqlite.d.ts +26 -0
- package/dist/telegram/update-store-sqlite.d.ts.map +1 -0
- package/dist/telegram/update-store.d.ts +67 -0
- package/dist/telegram/update-store.d.ts.map +1 -0
- package/dist/telegram/user.d.ts +51 -0
- package/dist/telegram/user.d.ts.map +1 -0
- package/dist/telegram/webhook.d.ts +105 -0
- package/dist/telegram/webhook.d.ts.map +1 -0
- package/dist/tools/cli/args-fields.d.ts +8 -0
- package/dist/tools/cli/args-fields.d.ts.map +1 -1
- package/dist/tools/cli/args-route.d.ts.map +1 -1
- package/dist/tools/cli/args.d.ts.map +1 -1
- package/dist/tools/cli/help.d.ts.map +1 -1
- package/llms/cli.txt +8 -2
- package/llms/observability.txt +15 -2
- package/llms/react-keyboard.txt +193 -0
- package/llms/server.4.txt +16 -0
- package/llms/stitchkit.txt +10 -4
- package/llms/telegram-html.txt +35 -0
- package/llms/telegram-screens.txt +365 -0
- package/llms/telegram.txt +245 -11
- package/llms/{upgrading-0.74.0-0.100.0.txt → upgrading-0.74.0-0.101.0.txt} +39 -3
- package/llms/upgrading.txt +1 -1
- package/llms-full.txt +889 -19
- package/llms.txt +12 -7
- package/package.json +17 -2
- package/dist/index-2k4yrqkc.js +0 -9
- package/dist/index-crykmqkr.js +0 -26
- package/dist/index-y64k3s0h.js +0 -150
- package/dist/index-y91ry9h2.js +0 -4
- /package/dist/{index-hffjpmq9.js → index-9jfdm5tg.js} +0 -0
- /package/dist/{index-zjjyynm3.js → index-d4pa2bgs.js} +0 -0
- /package/dist/{index-rxfy4cq7.js → index-dwfv9a85.js} +0 -0
- /package/dist/{index-5jm36zv1.js → index-gfk7nrht.js} +0 -0
- /package/dist/{index-b9aefrqd.js → index-n1dqr7gf.js} +0 -0
- /package/dist/{index-q3pyb9n0.js → index-sykb5ag7.js} +0 -0
- /package/dist/{index-48ffdxgk.js → index-xhzq2qvj.js} +0 -0
- /package/dist/{index-kzfs85xp.js → index-zsarb5xp.js} +0 -0
|
@@ -1,57 +1,57 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
AgentJsonObjectSchema2,
|
|
3
|
+
AgentProviderResponseSchema2,
|
|
4
|
+
AgentMessagePartSchema2,
|
|
5
|
+
AgentMessageSchema2,
|
|
6
|
+
AgentAssistantPlaceholderSchema2,
|
|
7
|
+
AgentRunOperationSchema2,
|
|
8
|
+
runStateForTerminalReason2,
|
|
9
|
+
AgentProvenanceSchema2,
|
|
10
|
+
AgentUsageSchema2,
|
|
11
|
+
AgentRunSchema2,
|
|
12
|
+
AgentSnapshotSchema2
|
|
13
|
+
} from "./index-8kzct1d6.js";
|
|
8
14
|
import {
|
|
9
15
|
createBoundedSinkManager
|
|
10
|
-
} from "./index-
|
|
16
|
+
} from "./index-h97tqqp1.js";
|
|
17
|
+
import {
|
|
18
|
+
AgentRuntimeEventSchema2,
|
|
19
|
+
agentDurableEventId2
|
|
20
|
+
} from "./index-mnkk1ffz.js";
|
|
11
21
|
import {
|
|
22
|
+
ToolExecutionControlError2,
|
|
23
|
+
isToolExecutionControlError2
|
|
24
|
+
} from "./index-dvwj1f43.js";
|
|
25
|
+
import {
|
|
26
|
+
createLocalStepDurability2,
|
|
27
|
+
isAgentToolError2,
|
|
28
|
+
createToolDurabilityContext
|
|
29
|
+
} from "./index-a9pd417t.js";
|
|
30
|
+
import {
|
|
31
|
+
canonicalAgentJson2,
|
|
32
|
+
createToolChronology,
|
|
12
33
|
advanceToolChronology,
|
|
13
|
-
assistantStatus,
|
|
14
34
|
canProjectToolChronology,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
} from "./index-cyktc4bp.js";
|
|
35
|
+
isAssistantHistoryEvidence2,
|
|
36
|
+
isCompleteAgentHistoryTurn,
|
|
37
|
+
assistantStatus
|
|
38
|
+
} from "./index-r5hsgkz5.js";
|
|
20
39
|
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
AgentMessagePartSchema,
|
|
24
|
-
AgentMessageSchema,
|
|
25
|
-
AgentProvenanceSchema,
|
|
26
|
-
AgentProviderResponseSchema,
|
|
27
|
-
AgentRunOperationSchema,
|
|
28
|
-
AgentRunSchema,
|
|
29
|
-
AgentSnapshotSchema,
|
|
30
|
-
AgentUsageSchema,
|
|
31
|
-
runStateForTerminalReason
|
|
32
|
-
} from "./index-gtj3zc53.js";
|
|
40
|
+
messageOf
|
|
41
|
+
} from "./index-g149e9pk.js";
|
|
33
42
|
import {
|
|
34
43
|
createMutationQueue
|
|
35
|
-
} from "./index-
|
|
36
|
-
import {
|
|
37
|
-
createLocalStepDurability,
|
|
38
|
-
createToolDurabilityContext,
|
|
39
|
-
isAgentToolError
|
|
40
|
-
} from "./index-79z7m7dt.js";
|
|
41
|
-
import {
|
|
42
|
-
ToolExecutionControlError,
|
|
43
|
-
isToolExecutionControlError
|
|
44
|
-
} from "./index-8qt2m66q.js";
|
|
44
|
+
} from "./index-sykb5ag7.js";
|
|
45
45
|
import {
|
|
46
46
|
serializeCanonicalJson
|
|
47
|
-
} from "./index-
|
|
47
|
+
} from "./index-zsvzhjtk.js";
|
|
48
48
|
import {
|
|
49
49
|
isRecord,
|
|
50
50
|
transportResult
|
|
51
|
-
} from "./index-
|
|
51
|
+
} from "./index-9jfdm5tg.js";
|
|
52
52
|
|
|
53
53
|
// src/agent-runtime/terminal-commit.ts
|
|
54
|
-
class
|
|
54
|
+
class AgentRuntimeConflictError2 extends Error {
|
|
55
55
|
constructor(operation) {
|
|
56
56
|
super(`Agent runtime store conflict during ${operation}`);
|
|
57
57
|
this.name = "AgentRuntimeConflictError";
|
|
@@ -60,7 +60,7 @@ class AgentRuntimeConflictError extends Error {
|
|
|
60
60
|
function appliedSnapshot(result, operation) {
|
|
61
61
|
if (result.outcome === "applied" || result.outcome === "duplicate")
|
|
62
62
|
return result.snapshot;
|
|
63
|
-
throw new
|
|
63
|
+
throw new AgentRuntimeConflictError2(operation);
|
|
64
64
|
}
|
|
65
65
|
function viewOfSnapshot(snapshot, runId, retainedAssistant) {
|
|
66
66
|
const run = snapshot.runs.find((candidate) => candidate.id === runId);
|
|
@@ -85,7 +85,7 @@ function canonicalTerminal(view) {
|
|
|
85
85
|
if (!run?.terminalReason)
|
|
86
86
|
return;
|
|
87
87
|
if (view.conversationId !== run.conversationId || !assistant || assistant.id !== run.assistantMessageId || assistant.conversationId !== run.conversationId || assistant.runId !== run.id || assistant.role !== "assistant" || assistant.status !== assistantStatus(run.terminalReason)) {
|
|
88
|
-
throw new
|
|
88
|
+
throw new AgentRuntimeConflictError2("terminal result projection");
|
|
89
89
|
}
|
|
90
90
|
return {
|
|
91
91
|
snapshotVersion: view.snapshotVersion,
|
|
@@ -102,12 +102,12 @@ function interruptedCandidate(candidate, run, now) {
|
|
|
102
102
|
const hasInterruptControl = candidate.assistant.parts.some((part) => part.type === "control" && part.reason === "run-interrupted");
|
|
103
103
|
const parts = hasInterruptControl ? candidate.assistant.parts : [
|
|
104
104
|
...candidate.assistant.parts,
|
|
105
|
-
|
|
105
|
+
AgentMessagePartSchema2.parse({ type: "control", reason: "run-interrupted" })
|
|
106
106
|
];
|
|
107
107
|
return {
|
|
108
108
|
...candidate.usage && { usage: candidate.usage },
|
|
109
109
|
run,
|
|
110
|
-
assistant:
|
|
110
|
+
assistant: AgentMessageSchema2.parse({
|
|
111
111
|
...candidate.assistant,
|
|
112
112
|
status: assistantStatus(reason),
|
|
113
113
|
parts,
|
|
@@ -138,24 +138,24 @@ async function commitAgentRunTerminal(input) {
|
|
|
138
138
|
...candidate.absorb?.length && { absorb: candidate.absorb }
|
|
139
139
|
});
|
|
140
140
|
if (committed.outcome === "applied") {
|
|
141
|
-
const
|
|
142
|
-
if (!
|
|
143
|
-
throw new
|
|
141
|
+
const terminal = canonicalTerminal(viewOfSnapshot(committed.snapshot, candidate.run.id));
|
|
142
|
+
if (!terminal)
|
|
143
|
+
throw new AgentRuntimeConflictError2("terminal result projection");
|
|
144
144
|
const absorbed = committed.snapshot.runs.filter((run) => run.absorbedIntoRunId === candidate.run.id);
|
|
145
145
|
return {
|
|
146
|
-
...
|
|
146
|
+
...terminal,
|
|
147
147
|
committedByCaller: true,
|
|
148
148
|
...absorbed.length > 0 && { absorbed }
|
|
149
149
|
};
|
|
150
150
|
}
|
|
151
151
|
if (committed.outcome === "duplicate") {
|
|
152
|
-
const
|
|
153
|
-
if (!
|
|
154
|
-
throw new
|
|
155
|
-
return
|
|
152
|
+
const terminal = canonicalTerminal(viewOfSnapshot(committed.snapshot, candidate.run.id, committed.assistant));
|
|
153
|
+
if (!terminal)
|
|
154
|
+
throw new AgentRuntimeConflictError2("terminal result projection");
|
|
155
|
+
return terminal;
|
|
156
156
|
}
|
|
157
157
|
if (committed.outcome !== "conflict") {
|
|
158
|
-
throw new
|
|
158
|
+
throw new AgentRuntimeConflictError2("terminal commit");
|
|
159
159
|
}
|
|
160
160
|
const latest = await input.store.loadRun({
|
|
161
161
|
conversationId: candidate.run.conversationId,
|
|
@@ -166,15 +166,15 @@ async function commitAgentRunTerminal(input) {
|
|
|
166
166
|
return terminal;
|
|
167
167
|
const current = latest?.run;
|
|
168
168
|
if (!current || !canRetryTerminal(current, candidate.run, input.runtimeEpoch)) {
|
|
169
|
-
throw new
|
|
169
|
+
throw new AgentRuntimeConflictError2("terminal commit");
|
|
170
170
|
}
|
|
171
171
|
candidate = current.state === "interrupt_requested" ? interruptedCandidate(candidate, current, input.now) : { ...candidate, run: current };
|
|
172
172
|
}
|
|
173
|
-
throw new
|
|
173
|
+
throw new AgentRuntimeConflictError2("terminal commit");
|
|
174
174
|
}
|
|
175
175
|
|
|
176
176
|
// src/agent-runtime/context-refusal.ts
|
|
177
|
-
class
|
|
177
|
+
class AgentContextOverflowError2 extends Error {
|
|
178
178
|
name = "AgentContextOverflowError";
|
|
179
179
|
constructor(message = "Agent context exceeds the configured model budget", options) {
|
|
180
180
|
super(message, options);
|
|
@@ -189,7 +189,7 @@ function assertCloseBudgets(options) {
|
|
|
189
189
|
}
|
|
190
190
|
}
|
|
191
191
|
}
|
|
192
|
-
function
|
|
192
|
+
function createAgentSessionCoordinator2() {
|
|
193
193
|
const lanes = new Map;
|
|
194
194
|
let closed = false;
|
|
195
195
|
const waitWithin = async (settled, timeoutMs) => {
|
|
@@ -325,7 +325,7 @@ function createAgentSessionCoordinator() {
|
|
|
325
325
|
}
|
|
326
326
|
|
|
327
327
|
// src/agent-runtime/events.ts
|
|
328
|
-
function
|
|
328
|
+
function createAgentRuntimeEventSink2(config) {
|
|
329
329
|
const manager = createBoundedSinkManager({
|
|
330
330
|
write: config.write,
|
|
331
331
|
...config.maxPending !== undefined && { maxPending: config.maxPending },
|
|
@@ -334,10 +334,10 @@ function createAgentRuntimeEventSink(config) {
|
|
|
334
334
|
});
|
|
335
335
|
return {
|
|
336
336
|
publish(rawEvent) {
|
|
337
|
-
const event =
|
|
337
|
+
const event = AgentRuntimeEventSchema2.parse(rawEvent);
|
|
338
338
|
const projected = config.project?.(event) ?? (config.project ? undefined : event);
|
|
339
339
|
if (projected)
|
|
340
|
-
manager.submit(() =>
|
|
340
|
+
manager.submit(() => AgentRuntimeEventSchema2.parse(projected));
|
|
341
341
|
},
|
|
342
342
|
flush: (bound) => manager.flush(bound),
|
|
343
343
|
getStatus: () => manager.getStatus(),
|
|
@@ -528,7 +528,7 @@ function interruptedSystemNote(message) {
|
|
|
528
528
|
rendered.add("control");
|
|
529
529
|
return { text: `[interrupted] partial response: ${text}`, rendered };
|
|
530
530
|
}
|
|
531
|
-
async function
|
|
531
|
+
async function projectAgentHistoryDetailed2(messages, options = {}) {
|
|
532
532
|
const projected = [];
|
|
533
533
|
const system = [];
|
|
534
534
|
const decisions = [];
|
|
@@ -542,7 +542,7 @@ async function projectAgentHistoryDetailed(messages, options = {}) {
|
|
|
542
542
|
chronology = createToolChronology();
|
|
543
543
|
observedUser = false;
|
|
544
544
|
}
|
|
545
|
-
if (message.role === "assistant" && !
|
|
545
|
+
if (message.role === "assistant" && !isAssistantHistoryEvidence2(message.status, options.evidencePolicy)) {
|
|
546
546
|
decisions.push(decide(message, "omitted", message.status === "superseded" ? "superseded" : "draft-or-failed"));
|
|
547
547
|
continue;
|
|
548
548
|
}
|
|
@@ -610,15 +610,15 @@ async function projectAgentHistoryDetailed(messages, options = {}) {
|
|
|
610
610
|
}
|
|
611
611
|
return { messages: projected, decisions, system };
|
|
612
612
|
}
|
|
613
|
-
async function
|
|
614
|
-
return [...(await
|
|
613
|
+
async function projectAgentHistory2(messages, options = {}) {
|
|
614
|
+
return [...(await projectAgentHistoryDetailed2(messages, options)).messages];
|
|
615
615
|
}
|
|
616
616
|
|
|
617
617
|
// src/agent-runtime/invocation-schema.ts
|
|
618
618
|
import { z } from "zod";
|
|
619
619
|
var Id = z.string().min(1).max(512);
|
|
620
620
|
var Hash = z.string().regex(/^[a-f0-9]{64}$/);
|
|
621
|
-
var
|
|
621
|
+
var ModelInvocationTraceSchema2 = z.object({
|
|
622
622
|
operationId: Id,
|
|
623
623
|
purpose: Id,
|
|
624
624
|
project: Id,
|
|
@@ -627,10 +627,10 @@ var ModelInvocationTraceSchema = z.object({
|
|
|
627
627
|
case: Id.optional(),
|
|
628
628
|
profile: Id.optional()
|
|
629
629
|
}).strict();
|
|
630
|
-
var
|
|
630
|
+
var CompletionInvocationInputSchema2 = z.object({
|
|
631
631
|
conversationId: Id,
|
|
632
632
|
idempotencyKey: Id,
|
|
633
|
-
trace:
|
|
633
|
+
trace: ModelInvocationTraceSchema2,
|
|
634
634
|
prompt: z.string().min(1),
|
|
635
635
|
models: z.array(Id).min(1).max(8),
|
|
636
636
|
settings: z.object({
|
|
@@ -661,7 +661,7 @@ var Base = z.object({
|
|
|
661
661
|
var InvocationStartedSchema = Base.extend({
|
|
662
662
|
type: z.literal("invocation/started"),
|
|
663
663
|
mode: z.enum(["completion", "agent"]),
|
|
664
|
-
trace:
|
|
664
|
+
trace: ModelInvocationTraceSchema2,
|
|
665
665
|
caller: InvocationCallerSchema,
|
|
666
666
|
executingProcess: InvocationProcessSchema,
|
|
667
667
|
fingerprint: Hash,
|
|
@@ -688,7 +688,7 @@ var InvocationResponseSchema = Attempt.extend({
|
|
|
688
688
|
type: z.literal("provider/response"),
|
|
689
689
|
status: z.enum(["succeeded", "failed", "cancelled"]),
|
|
690
690
|
effective: Identity,
|
|
691
|
-
usage:
|
|
691
|
+
usage: AgentUsageSchema2,
|
|
692
692
|
artifactId: Id,
|
|
693
693
|
payloadSha256: Hash
|
|
694
694
|
}).strict();
|
|
@@ -697,7 +697,7 @@ var InvocationFinishedSchema = Base.extend({
|
|
|
697
697
|
failure: z.object({ artifactId: Id, payloadSha256: Hash }).strict().optional(),
|
|
698
698
|
status: z.enum(["succeeded", "failed", "cancelled"])
|
|
699
699
|
}).strict();
|
|
700
|
-
var
|
|
700
|
+
var ModelInvocationRecordSchema2 = z.discriminatedUnion("type", [
|
|
701
701
|
InvocationStartedSchema,
|
|
702
702
|
InvocationRequestSchema,
|
|
703
703
|
InvocationResponseSchema,
|
|
@@ -859,7 +859,7 @@ import { z as z3 } from "zod";
|
|
|
859
859
|
function findRun(runs, runId) {
|
|
860
860
|
const run = runs.find((candidate) => candidate.id === runId);
|
|
861
861
|
if (!run)
|
|
862
|
-
throw new
|
|
862
|
+
throw new AgentRuntimeConflictError2("run lookup");
|
|
863
863
|
return run;
|
|
864
864
|
}
|
|
865
865
|
function jsonValue(value) {
|
|
@@ -867,13 +867,13 @@ function jsonValue(value) {
|
|
|
867
867
|
return parsed.success ? parsed.data : { message: "Non-JSON tool output omitted" };
|
|
868
868
|
}
|
|
869
869
|
function providerEnvelope(value) {
|
|
870
|
-
const parsed =
|
|
870
|
+
const parsed = AgentJsonObjectSchema2.safeParse(value);
|
|
871
871
|
if (!parsed.success)
|
|
872
872
|
return;
|
|
873
873
|
return { schemaVersion: 1, provider: "ai-sdk", data: parsed.data };
|
|
874
874
|
}
|
|
875
875
|
function providerResponseIdentity(id, provider) {
|
|
876
|
-
return
|
|
876
|
+
return AgentProviderResponseSchema2.parse({
|
|
877
877
|
id,
|
|
878
878
|
...typeof provider === "string" && provider.length > 0 ? { provider } : {}
|
|
879
879
|
});
|
|
@@ -881,11 +881,11 @@ function providerResponseIdentity(id, provider) {
|
|
|
881
881
|
function appendText(parts, text) {
|
|
882
882
|
const previous = parts.at(-1);
|
|
883
883
|
if (previous?.type === "text") {
|
|
884
|
-
const next =
|
|
884
|
+
const next = AgentMessagePartSchema2.parse({ ...previous, text: previous.text + text });
|
|
885
885
|
parts.splice(parts.length - 1, 1, next);
|
|
886
886
|
return;
|
|
887
887
|
}
|
|
888
|
-
parts.push(
|
|
888
|
+
parts.push(AgentMessagePartSchema2.parse({ type: "text", text }));
|
|
889
889
|
}
|
|
890
890
|
function abortTerminalReason(signal) {
|
|
891
891
|
if (signal.reason === "shutdown")
|
|
@@ -1038,7 +1038,7 @@ function createIdleDeadline(parent, timeoutMs) {
|
|
|
1038
1038
|
|
|
1039
1039
|
// src/agent-runtime/invocation-attempt.ts
|
|
1040
1040
|
var currentAttempt = new AsyncLocalStorage;
|
|
1041
|
-
function
|
|
1041
|
+
function currentModelInvocationAttempt2() {
|
|
1042
1042
|
const value = currentAttempt.getStore();
|
|
1043
1043
|
return value && { ...value };
|
|
1044
1044
|
}
|
|
@@ -1274,7 +1274,7 @@ async function readInvocationRecords(store, input) {
|
|
|
1274
1274
|
{
|
|
1275
1275
|
seq: event.seq,
|
|
1276
1276
|
occurredAt: event.occurredAt,
|
|
1277
|
-
record:
|
|
1277
|
+
record: ModelInvocationRecordSchema2.parse(event.payload)
|
|
1278
1278
|
}
|
|
1279
1279
|
];
|
|
1280
1280
|
}),
|
|
@@ -1341,7 +1341,7 @@ async function generateWithFallback(models, writer, input, abortSignal) {
|
|
|
1341
1341
|
}
|
|
1342
1342
|
throw new Error("No completion model attempt available");
|
|
1343
1343
|
}
|
|
1344
|
-
function
|
|
1344
|
+
function createModelInvocationLedger2(config) {
|
|
1345
1345
|
const { store } = config;
|
|
1346
1346
|
const appendOnce = store.appendEventOnce?.bind(store);
|
|
1347
1347
|
if (!appendOnce)
|
|
@@ -1350,7 +1350,7 @@ function createModelInvocationLedger(config) {
|
|
|
1350
1350
|
const payloads = createInvocationPayloads(store, config.payloadKey, maxPayloadBytes);
|
|
1351
1351
|
const authorize = async (input) => InvocationCallerSchema.parse(await config.authorize(input));
|
|
1352
1352
|
const start = async (input) => {
|
|
1353
|
-
const trace =
|
|
1353
|
+
const trace = ModelInvocationTraceSchema2.parse(input.trace);
|
|
1354
1354
|
const caller = await authorize({
|
|
1355
1355
|
context: input.context,
|
|
1356
1356
|
conversationId: input.conversationId,
|
|
@@ -1390,7 +1390,7 @@ function createModelInvocationLedger(config) {
|
|
|
1390
1390
|
onAttempt: config.onAttempt,
|
|
1391
1391
|
receiptErrors: [],
|
|
1392
1392
|
async append(record) {
|
|
1393
|
-
const parsed =
|
|
1393
|
+
const parsed = ModelInvocationRecordSchema2.parse(record);
|
|
1394
1394
|
await store.appendEvent({
|
|
1395
1395
|
conversationId: input.conversationId,
|
|
1396
1396
|
kind: parsed.type,
|
|
@@ -1408,7 +1408,7 @@ function createModelInvocationLedger(config) {
|
|
|
1408
1408
|
});
|
|
1409
1409
|
const ledger = {
|
|
1410
1410
|
async complete(rawInput, context, signal) {
|
|
1411
|
-
const input =
|
|
1411
|
+
const input = CompletionInvocationInputSchema2.parse(rawInput);
|
|
1412
1412
|
const { writer, duplicate } = await start({
|
|
1413
1413
|
conversationId: input.conversationId,
|
|
1414
1414
|
key: `completion:${input.idempotencyKey}`,
|
|
@@ -1436,8 +1436,8 @@ function createModelInvocationLedger(config) {
|
|
|
1436
1436
|
});
|
|
1437
1437
|
} catch (receiptError) {
|
|
1438
1438
|
writer.receiptErrors.push(receiptError);
|
|
1439
|
-
await finish(writer, status).catch((
|
|
1440
|
-
writer.receiptErrors.push(
|
|
1439
|
+
await finish(writer, status).catch((error) => {
|
|
1440
|
+
writer.receiptErrors.push(error);
|
|
1441
1441
|
});
|
|
1442
1442
|
}
|
|
1443
1443
|
return withReceipt({ invocationId, outcome: status }, writer);
|
|
@@ -1494,9 +1494,9 @@ function createModelInvocationLedger(config) {
|
|
|
1494
1494
|
async function assertFence(config, input) {
|
|
1495
1495
|
const rejected = await config.assertCurrent(input);
|
|
1496
1496
|
if (rejected)
|
|
1497
|
-
throw new
|
|
1497
|
+
throw new ToolExecutionControlError2(rejected);
|
|
1498
1498
|
}
|
|
1499
|
-
function
|
|
1499
|
+
function createAgentToolFenceLifecycle2(config) {
|
|
1500
1500
|
const callContext = (ctx, endpoint) => ({
|
|
1501
1501
|
runId: config.runId,
|
|
1502
1502
|
...config.context?.(ctx, endpoint)
|
|
@@ -1516,9 +1516,9 @@ function createAgentToolFenceLifecycle(config) {
|
|
|
1516
1516
|
|
|
1517
1517
|
// src/agent-runtime/prompt.ts
|
|
1518
1518
|
import { z as z6 } from "zod";
|
|
1519
|
-
var
|
|
1519
|
+
var AgentTokenCountSchema2 = z6.object({
|
|
1520
1520
|
value: z6.int().nonnegative().optional(),
|
|
1521
|
-
provenance:
|
|
1521
|
+
provenance: AgentProvenanceSchema2.extract([
|
|
1522
1522
|
"measured",
|
|
1523
1523
|
"computed",
|
|
1524
1524
|
"estimated",
|
|
@@ -1551,7 +1551,7 @@ function budgetTurns(messages, evidencePolicy) {
|
|
|
1551
1551
|
flush();
|
|
1552
1552
|
return turns;
|
|
1553
1553
|
}
|
|
1554
|
-
async function
|
|
1554
|
+
async function selectAgentHistory2(options) {
|
|
1555
1555
|
if (!Number.isSafeInteger(options.availableTokens) || options.availableTokens < 0) {
|
|
1556
1556
|
throw new TypeError("availableTokens must be a non-negative safe integer");
|
|
1557
1557
|
}
|
|
@@ -1559,12 +1559,12 @@ async function selectAgentHistory(options) {
|
|
|
1559
1559
|
if (!Number.isSafeInteger(keepRecentTurns) || keepRecentTurns < 0) {
|
|
1560
1560
|
throw new TypeError("keepRecentTurns must be a non-negative safe integer");
|
|
1561
1561
|
}
|
|
1562
|
-
const spoken = options.messages.filter((message) => message.role !== "assistant" ||
|
|
1562
|
+
const spoken = options.messages.filter((message) => message.role !== "assistant" || isAssistantHistoryEvidence2(message.status, options.evidencePolicy));
|
|
1563
1563
|
const counts = new Map;
|
|
1564
1564
|
let total = 0;
|
|
1565
1565
|
let estimated = false;
|
|
1566
1566
|
for (const message of spoken) {
|
|
1567
|
-
const count =
|
|
1567
|
+
const count = AgentTokenCountSchema2.parse(await options.estimateMessage(message));
|
|
1568
1568
|
counts.set(message.id, count);
|
|
1569
1569
|
const value = knownValue(count);
|
|
1570
1570
|
if (value === undefined) {
|
|
@@ -1572,8 +1572,8 @@ async function selectAgentHistory(options) {
|
|
|
1572
1572
|
messages: [...spoken],
|
|
1573
1573
|
decisions: options.messages.map((candidate) => ({
|
|
1574
1574
|
messageId: candidate.id,
|
|
1575
|
-
action: candidate.role === "assistant" && !
|
|
1576
|
-
reason: candidate.role === "assistant" && !
|
|
1575
|
+
action: candidate.role === "assistant" && !isAssistantHistoryEvidence2(candidate.status, options.evidencePolicy) ? "removed" : "kept",
|
|
1576
|
+
reason: candidate.role === "assistant" && !isAssistantHistoryEvidence2(candidate.status, options.evidencePolicy) ? "unspeakable" : "token-count-unavailable",
|
|
1577
1577
|
tokens: counts.get(candidate.id) ?? { provenance: "unavailable" }
|
|
1578
1578
|
})),
|
|
1579
1579
|
totalTokens: { provenance: "unavailable" },
|
|
@@ -1599,7 +1599,7 @@ async function selectAgentHistory(options) {
|
|
|
1599
1599
|
}
|
|
1600
1600
|
const messages = spoken.filter((message) => !removed.has(message.id));
|
|
1601
1601
|
const decisions = options.messages.map((message) => {
|
|
1602
|
-
if (message.role === "assistant" && !
|
|
1602
|
+
if (message.role === "assistant" && !isAssistantHistoryEvidence2(message.status, options.evidencePolicy)) {
|
|
1603
1603
|
return {
|
|
1604
1604
|
messageId: message.id,
|
|
1605
1605
|
action: "removed",
|
|
@@ -1607,7 +1607,7 @@ async function selectAgentHistory(options) {
|
|
|
1607
1607
|
tokens: { provenance: "unavailable" }
|
|
1608
1608
|
};
|
|
1609
1609
|
}
|
|
1610
|
-
const turnIndex = turns.findIndex((
|
|
1610
|
+
const turnIndex = turns.findIndex((turn) => turn.messages.some((item) => item.id === message.id));
|
|
1611
1611
|
const turn = turns[turnIndex];
|
|
1612
1612
|
let reason = "within-budget";
|
|
1613
1613
|
if (removed.has(message.id))
|
|
@@ -1635,7 +1635,7 @@ async function selectAgentHistory(options) {
|
|
|
1635
1635
|
function knownValue(value) {
|
|
1636
1636
|
return value.provenance === "unavailable" ? undefined : value.value;
|
|
1637
1637
|
}
|
|
1638
|
-
function
|
|
1638
|
+
function composeAgentPrompt2(sections) {
|
|
1639
1639
|
return async (options) => {
|
|
1640
1640
|
const event = z6.enum(["session.started", "turn.started"]).parse(options.event ?? "turn.started");
|
|
1641
1641
|
const rendered = [];
|
|
@@ -1655,9 +1655,9 @@ function composeAgentPrompt(sections) {
|
|
|
1655
1655
|
rendered.push({ name: section.name, stability: section.stability, role, text });
|
|
1656
1656
|
let count;
|
|
1657
1657
|
if (section.estimateTokens) {
|
|
1658
|
-
count =
|
|
1658
|
+
count = AgentTokenCountSchema2.parse(await section.estimateTokens(text));
|
|
1659
1659
|
} else if (options.estimateFallback) {
|
|
1660
|
-
count =
|
|
1660
|
+
count = AgentTokenCountSchema2.parse(await options.estimateFallback(text));
|
|
1661
1661
|
} else
|
|
1662
1662
|
count = { provenance: "unavailable" };
|
|
1663
1663
|
const value = knownValue(count);
|
|
@@ -1687,14 +1687,14 @@ function composeAgentPrompt(sections) {
|
|
|
1687
1687
|
}
|
|
1688
1688
|
const reserveValues = [
|
|
1689
1689
|
options.budget.reservedOutput,
|
|
1690
|
-
knownValue(
|
|
1691
|
-
knownValue(
|
|
1692
|
-
knownValue(
|
|
1690
|
+
knownValue(AgentTokenCountSchema2.parse(options.budget.toolSchemas)),
|
|
1691
|
+
knownValue(AgentTokenCountSchema2.parse(options.budget.attachments)),
|
|
1692
|
+
knownValue(AgentTokenCountSchema2.parse(options.budget.providerOverhead)),
|
|
1693
1693
|
knownValue(instructionTokens)
|
|
1694
1694
|
];
|
|
1695
1695
|
if (reserveValues.every((value) => value !== undefined)) {
|
|
1696
1696
|
availableHistoryTokens = options.budget.contextWindow - reserveValues.reduce((sum, value) => sum + (value ?? 0), 0);
|
|
1697
|
-
const historyTokens = options.historyTokens ? knownValue(
|
|
1697
|
+
const historyTokens = options.historyTokens ? knownValue(AgentTokenCountSchema2.parse(options.historyTokens)) : undefined;
|
|
1698
1698
|
if (historyTokens !== undefined) {
|
|
1699
1699
|
if (availableHistoryTokens < 0)
|
|
1700
1700
|
contextDecision = "oversized";
|
|
@@ -1756,7 +1756,7 @@ var RETRYABLE = new Set([
|
|
|
1756
1756
|
"stream-cut"
|
|
1757
1757
|
]);
|
|
1758
1758
|
|
|
1759
|
-
class
|
|
1759
|
+
class AgentProviderStreamCutError2 extends Error {
|
|
1760
1760
|
reason = "stream-cut";
|
|
1761
1761
|
constructor(message = "Provider stream ended before a terminal part") {
|
|
1762
1762
|
super(message);
|
|
@@ -1774,8 +1774,8 @@ function statusOf(value) {
|
|
|
1774
1774
|
const nested = value.cause ?? value.response ?? value.error;
|
|
1775
1775
|
return nested === value ? undefined : statusOf(nested);
|
|
1776
1776
|
}
|
|
1777
|
-
function
|
|
1778
|
-
if (error instanceof
|
|
1777
|
+
function classifyProviderFailure2(error) {
|
|
1778
|
+
if (error instanceof AgentProviderStreamCutError2) {
|
|
1779
1779
|
return { reason: "stream-cut", retryable: true, evidence: "none" };
|
|
1780
1780
|
}
|
|
1781
1781
|
const status = statusOf(error);
|
|
@@ -1806,7 +1806,7 @@ function classifyProviderFailure(error) {
|
|
|
1806
1806
|
evidence: "none"
|
|
1807
1807
|
};
|
|
1808
1808
|
}
|
|
1809
|
-
function
|
|
1809
|
+
function isToolResultFailure2(output) {
|
|
1810
1810
|
const parsed = typeof output === "string" ? parseJson(output) : output;
|
|
1811
1811
|
if (!isRecord(parsed))
|
|
1812
1812
|
return false;
|
|
@@ -1823,7 +1823,7 @@ function parseJson(value) {
|
|
|
1823
1823
|
}
|
|
1824
1824
|
|
|
1825
1825
|
// src/agent-runtime/retry-policy.ts
|
|
1826
|
-
async function
|
|
1826
|
+
async function recordAgentRetryDecision2(input) {
|
|
1827
1827
|
const retry = input.failure.retryable && input.attempt < input.policy.maxAttempts;
|
|
1828
1828
|
const delayMs = retry ? input.policy.delayMs({ attempt: input.attempt, failure: input.failure }) : 0;
|
|
1829
1829
|
if (!Number.isInteger(delayMs) || delayMs < 0) {
|
|
@@ -1841,7 +1841,7 @@ async function recordAgentRetryDecision(input) {
|
|
|
1841
1841
|
|
|
1842
1842
|
// src/agent-runtime/state-slots.ts
|
|
1843
1843
|
import { z as z7 } from "zod";
|
|
1844
|
-
function
|
|
1844
|
+
function defineStateSlot2(input) {
|
|
1845
1845
|
if (input.name.length === 0)
|
|
1846
1846
|
throw new TypeError("State slot name must not be empty");
|
|
1847
1847
|
const version = input.version ?? 1;
|
|
@@ -1870,7 +1870,7 @@ function slotRegistry(definitions) {
|
|
|
1870
1870
|
}
|
|
1871
1871
|
return result;
|
|
1872
1872
|
}
|
|
1873
|
-
function
|
|
1873
|
+
function createAgentStateSlotStore2(input) {
|
|
1874
1874
|
const definitions = slotRegistry(input.definitions);
|
|
1875
1875
|
const set = async (request) => {
|
|
1876
1876
|
const definition = definitions.get(request.name);
|
|
@@ -1929,7 +1929,7 @@ function createAgentStateSlotStore(input) {
|
|
|
1929
1929
|
const get = async (conversationId, name) => (await list(conversationId)).find((value) => value.name === name);
|
|
1930
1930
|
return { set, get, list };
|
|
1931
1931
|
}
|
|
1932
|
-
function
|
|
1932
|
+
function renderAgentStateSlots2(values) {
|
|
1933
1933
|
if (values.length === 0)
|
|
1934
1934
|
return;
|
|
1935
1935
|
return [
|
|
@@ -1938,7 +1938,7 @@ function renderAgentStateSlots(values) {
|
|
|
1938
1938
|
].join(`
|
|
1939
1939
|
`);
|
|
1940
1940
|
}
|
|
1941
|
-
var
|
|
1941
|
+
var agentGoalStateSlot2 = defineStateSlot2({
|
|
1942
1942
|
name: "goal",
|
|
1943
1943
|
description: "The durable objective and lifecycle of this conversation.",
|
|
1944
1944
|
schema: z7.object({
|
|
@@ -1946,7 +1946,7 @@ var agentGoalStateSlot = defineStateSlot({
|
|
|
1946
1946
|
status: z7.enum(["active", "complete", "blocked"])
|
|
1947
1947
|
}).strict()
|
|
1948
1948
|
});
|
|
1949
|
-
var
|
|
1949
|
+
var agentTodoStateSlot2 = defineStateSlot2({
|
|
1950
1950
|
name: "todo",
|
|
1951
1951
|
description: "The durable ordered work plan for this conversation.",
|
|
1952
1952
|
schema: z7.object({
|
|
@@ -1965,7 +1965,7 @@ async function interruptAgentRun(state, input) {
|
|
|
1965
1965
|
runId: input.runId
|
|
1966
1966
|
});
|
|
1967
1967
|
if (!view)
|
|
1968
|
-
throw new
|
|
1968
|
+
throw new AgentRuntimeConflictError2("run lookup");
|
|
1969
1969
|
const requested = await store.requestRunInterrupt({
|
|
1970
1970
|
conversationId: input.conversationId,
|
|
1971
1971
|
runId: input.runId,
|
|
@@ -1975,7 +1975,7 @@ async function interruptAgentRun(state, input) {
|
|
|
1975
1975
|
const interruptedRun = findRun(requested.snapshot.runs, input.runId);
|
|
1976
1976
|
await state.publish({
|
|
1977
1977
|
type: "run-state",
|
|
1978
|
-
eventId:
|
|
1978
|
+
eventId: agentDurableEventId2("run-state", interruptedRun.id, requested.snapshot.version),
|
|
1979
1979
|
conversationId: interruptedRun.conversationId,
|
|
1980
1980
|
runId: interruptedRun.id,
|
|
1981
1981
|
snapshotVersion: requested.snapshot.version,
|
|
@@ -2002,7 +2002,7 @@ async function abandonAgentRun(state, input) {
|
|
|
2002
2002
|
const abandonedRun = findRun(abandoned.snapshot.runs, input.runId);
|
|
2003
2003
|
await state.publish({
|
|
2004
2004
|
type: "run-state",
|
|
2005
|
-
eventId:
|
|
2005
|
+
eventId: agentDurableEventId2("run-state", abandonedRun.id, abandoned.snapshot.version),
|
|
2006
2006
|
conversationId: abandonedRun.conversationId,
|
|
2007
2007
|
runId: abandonedRun.id,
|
|
2008
2008
|
snapshotVersion: abandoned.snapshot.version,
|
|
@@ -2109,7 +2109,7 @@ function resumeAgentRun(state, rawInput) {
|
|
|
2109
2109
|
runId: rawInput.runId
|
|
2110
2110
|
});
|
|
2111
2111
|
if (!recovered)
|
|
2112
|
-
throw new
|
|
2112
|
+
throw new AgentRuntimeConflictError2("run lookup");
|
|
2113
2113
|
const recoveredRun = recovered.run;
|
|
2114
2114
|
if (recoveredRun.state !== "queued") {
|
|
2115
2115
|
throw new Error("Only a queued recovered agent run can be resumed");
|
|
@@ -2240,7 +2240,7 @@ async function recoverItem(state, options, item, scheduledRuns) {
|
|
|
2240
2240
|
action: "abandon"
|
|
2241
2241
|
});
|
|
2242
2242
|
if (abandoned.outcome !== "applied") {
|
|
2243
|
-
throw new
|
|
2243
|
+
throw new AgentRuntimeConflictError2("recovery abandon");
|
|
2244
2244
|
}
|
|
2245
2245
|
return { conversationId: item.conversationId, runId: item.run.id, outcome: "abandoned" };
|
|
2246
2246
|
}
|
|
@@ -2253,7 +2253,7 @@ async function recoverItem(state, options, item, scheduledRuns) {
|
|
|
2253
2253
|
replaySafe: true
|
|
2254
2254
|
});
|
|
2255
2255
|
if (requeued.outcome !== "applied") {
|
|
2256
|
-
throw new
|
|
2256
|
+
throw new AgentRuntimeConflictError2("recovery requeue");
|
|
2257
2257
|
}
|
|
2258
2258
|
}
|
|
2259
2259
|
const context = await options.resolveContext(item);
|
|
@@ -2388,7 +2388,7 @@ function updateReasoning(state, text, metadata) {
|
|
|
2388
2388
|
const provider = providerEnvelope(metadata);
|
|
2389
2389
|
if (state.reasoningPartIndex === undefined) {
|
|
2390
2390
|
state.reasoningPartIndex = parts.length;
|
|
2391
|
-
parts.push(
|
|
2391
|
+
parts.push(AgentMessagePartSchema2.parse({
|
|
2392
2392
|
type: "reasoning",
|
|
2393
2393
|
text,
|
|
2394
2394
|
...provider && { provider }
|
|
@@ -2397,9 +2397,9 @@ function updateReasoning(state, text, metadata) {
|
|
|
2397
2397
|
}
|
|
2398
2398
|
const current = parts[state.reasoningPartIndex];
|
|
2399
2399
|
if (current?.type !== "reasoning") {
|
|
2400
|
-
throw new
|
|
2400
|
+
throw new AgentRuntimeConflictError2("reasoning accumulator");
|
|
2401
2401
|
}
|
|
2402
|
-
parts.splice(state.reasoningPartIndex, 1,
|
|
2402
|
+
parts.splice(state.reasoningPartIndex, 1, AgentMessagePartSchema2.parse({
|
|
2403
2403
|
...current,
|
|
2404
2404
|
text: current.text + text,
|
|
2405
2405
|
...provider && { provider }
|
|
@@ -2409,7 +2409,7 @@ function checkpoint(execution) {
|
|
|
2409
2409
|
const { config, runtimeEpoch, publish, now } = execution.dependencies;
|
|
2410
2410
|
const { state } = execution;
|
|
2411
2411
|
return execution.serialize(async () => {
|
|
2412
|
-
state.assistant =
|
|
2412
|
+
state.assistant = AgentMessageSchema2.parse({
|
|
2413
2413
|
...state.assistant,
|
|
2414
2414
|
parts: state.parts,
|
|
2415
2415
|
updatedAt: now().toISOString()
|
|
@@ -2434,7 +2434,7 @@ function checkpoint(execution) {
|
|
|
2434
2434
|
};
|
|
2435
2435
|
await publish({
|
|
2436
2436
|
type: "assistant-checkpoint",
|
|
2437
|
-
eventId:
|
|
2437
|
+
eventId: agentDurableEventId2("assistant-checkpoint", state.run.id, state.observedVersion),
|
|
2438
2438
|
conversationId: state.run.conversationId,
|
|
2439
2439
|
runId: state.run.id,
|
|
2440
2440
|
snapshotVersion: state.observedVersion,
|
|
@@ -2467,7 +2467,7 @@ function createAgentRunOperationLifecycle(config) {
|
|
|
2467
2467
|
active = operation;
|
|
2468
2468
|
await config.publish({
|
|
2469
2469
|
type: "run-operation",
|
|
2470
|
-
eventId:
|
|
2470
|
+
eventId: agentDurableEventId2("run-operation", run.id, snapshot.version),
|
|
2471
2471
|
conversationId: run.conversationId,
|
|
2472
2472
|
runId: run.id,
|
|
2473
2473
|
snapshotVersion: snapshot.version,
|
|
@@ -2476,7 +2476,7 @@ function createAgentRunOperationLifecycle(config) {
|
|
|
2476
2476
|
});
|
|
2477
2477
|
});
|
|
2478
2478
|
const startModelRequest = async (callId, step) => {
|
|
2479
|
-
await record(
|
|
2479
|
+
await record(AgentRunOperationSchema2.parse({
|
|
2480
2480
|
operationId: `${callId}:${step}`,
|
|
2481
2481
|
kind: "model-request",
|
|
2482
2482
|
phase: "started",
|
|
@@ -2534,7 +2534,7 @@ function createAgentRunOperationLifecycle(config) {
|
|
|
2534
2534
|
checkpointWaiters.clear();
|
|
2535
2535
|
},
|
|
2536
2536
|
startCompaction(operationId) {
|
|
2537
|
-
return record(
|
|
2537
|
+
return record(AgentRunOperationSchema2.parse({
|
|
2538
2538
|
operationId,
|
|
2539
2539
|
kind: "compaction",
|
|
2540
2540
|
phase: "started",
|
|
@@ -2545,7 +2545,7 @@ function createAgentRunOperationLifecycle(config) {
|
|
|
2545
2545
|
if (active?.kind !== "model-request" || active.phase !== "started") {
|
|
2546
2546
|
return Promise.resolve();
|
|
2547
2547
|
}
|
|
2548
|
-
return record(
|
|
2548
|
+
return record(AgentRunOperationSchema2.parse({
|
|
2549
2549
|
...active,
|
|
2550
2550
|
phase: "first-output",
|
|
2551
2551
|
firstOutputAt: config.now().toISOString()
|
|
@@ -2555,7 +2555,7 @@ function createAgentRunOperationLifecycle(config) {
|
|
|
2555
2555
|
if (!active || ["completed", "failed", "cancelled"].includes(active.phase)) {
|
|
2556
2556
|
return Promise.resolve();
|
|
2557
2557
|
}
|
|
2558
|
-
return record(
|
|
2558
|
+
return record(AgentRunOperationSchema2.parse({
|
|
2559
2559
|
...active,
|
|
2560
2560
|
phase,
|
|
2561
2561
|
finishedAt: config.now().toISOString()
|
|
@@ -2573,14 +2573,14 @@ async function acquireRun(dependencies, input) {
|
|
|
2573
2573
|
runId: input.acceptedRun.id
|
|
2574
2574
|
});
|
|
2575
2575
|
if (!queued)
|
|
2576
|
-
throw new
|
|
2576
|
+
throw new AgentRuntimeConflictError2("run lookup");
|
|
2577
2577
|
if (queued.run.terminalReason === "absorbed") {
|
|
2578
2578
|
const absorbing = queued.run.absorbedIntoRunId ? await config.store.loadRun({
|
|
2579
2579
|
conversationId: queued.run.conversationId,
|
|
2580
2580
|
runId: queued.run.absorbedIntoRunId
|
|
2581
2581
|
}) : undefined;
|
|
2582
2582
|
if (!absorbing?.assistant || !absorbing.run.terminalReason) {
|
|
2583
|
-
throw new
|
|
2583
|
+
throw new AgentRuntimeConflictError2("absorbed run resolution");
|
|
2584
2584
|
}
|
|
2585
2585
|
input.onAcquired?.();
|
|
2586
2586
|
return {
|
|
@@ -2607,7 +2607,7 @@ async function acquireRun(dependencies, input) {
|
|
|
2607
2607
|
input.onAcquired?.();
|
|
2608
2608
|
await publish({
|
|
2609
2609
|
type: "run-state",
|
|
2610
|
-
eventId:
|
|
2610
|
+
eventId: agentDurableEventId2("run-state", run.id, acquired.version),
|
|
2611
2611
|
conversationId: run.conversationId,
|
|
2612
2612
|
runId: run.id,
|
|
2613
2613
|
snapshotVersion: acquired.version,
|
|
@@ -2629,7 +2629,7 @@ async function acquireRun(dependencies, input) {
|
|
|
2629
2629
|
queueWaitMs: Math.max(0, now().getTime() - new Date(run.createdAt).getTime()),
|
|
2630
2630
|
emittedAt: now().toISOString()
|
|
2631
2631
|
});
|
|
2632
|
-
const assistant =
|
|
2632
|
+
const assistant = AgentMessageSchema2.parse({
|
|
2633
2633
|
schemaVersion: 1,
|
|
2634
2634
|
id: run.assistantMessageId,
|
|
2635
2635
|
conversationId: run.conversationId,
|
|
@@ -2704,9 +2704,9 @@ async function acquireRun(dependencies, input) {
|
|
|
2704
2704
|
function failureThisRuntimeOwns(error) {
|
|
2705
2705
|
if (error instanceof InvocationReceiptError)
|
|
2706
2706
|
return "runtime_failure";
|
|
2707
|
-
if (error instanceof
|
|
2707
|
+
if (error instanceof AgentContextOverflowError2)
|
|
2708
2708
|
return "context_overflow";
|
|
2709
|
-
if (error instanceof
|
|
2709
|
+
if (error instanceof AgentRuntimeConflictError2)
|
|
2710
2710
|
return "storage_conflict";
|
|
2711
2711
|
if (isOwnInputRefusal(error))
|
|
2712
2712
|
return "runtime_failure";
|
|
@@ -2728,19 +2728,19 @@ async function classifyRunFailure(execution, error) {
|
|
|
2728
2728
|
state.observedVersion = latest.snapshotVersion;
|
|
2729
2729
|
state.run = latestRun;
|
|
2730
2730
|
}
|
|
2731
|
-
const staleOwner =
|
|
2731
|
+
const staleOwner = isToolExecutionControlError2(error) && error.reason === "stale_run";
|
|
2732
2732
|
if (!staleOwner) {
|
|
2733
2733
|
await operationLifecycle.finish(executionSignal.aborted || durableInterrupt ? "cancelled" : "failed");
|
|
2734
2734
|
}
|
|
2735
|
-
if (
|
|
2735
|
+
if (isToolExecutionControlError2(error) || executionSignal.aborted || durableInterrupt) {
|
|
2736
2736
|
state.terminalReason = executionSignal.aborted ? abortTerminalReason(executionSignal) : "interrupted";
|
|
2737
|
-
state.parts.push(
|
|
2737
|
+
state.parts.push(AgentMessagePartSchema2.parse({
|
|
2738
2738
|
type: "control",
|
|
2739
|
-
reason:
|
|
2739
|
+
reason: isToolExecutionControlError2(error) && error.reason === "stale_run" ? "stale-run" : "run-interrupted"
|
|
2740
2740
|
}));
|
|
2741
|
-
} else if (error instanceof
|
|
2741
|
+
} else if (error instanceof AgentContextOverflowError2) {
|
|
2742
2742
|
state.terminalReason = "context_overflow";
|
|
2743
|
-
} else if (error instanceof
|
|
2743
|
+
} else if (error instanceof AgentRuntimeConflictError2) {
|
|
2744
2744
|
state.terminalReason = "storage_conflict";
|
|
2745
2745
|
} else {
|
|
2746
2746
|
state.terminalReason = failureThisRuntimeOwns(error) ?? (hasProviderOrigin(error) ? "provider_failure" : "runtime_failure");
|
|
@@ -2882,7 +2882,7 @@ async function createRunProviderLedger(input) {
|
|
|
2882
2882
|
async recordRequest(request) {
|
|
2883
2883
|
const messageShas = [];
|
|
2884
2884
|
for (const message of request.messages) {
|
|
2885
|
-
const body =
|
|
2885
|
+
const body = canonicalAgentJson2(JSON.parse(JSON.stringify(withoutBinaryParts(message))));
|
|
2886
2886
|
const sha256 = createHash3("sha256").update(body).digest("hex");
|
|
2887
2887
|
messageShas.push(sha256);
|
|
2888
2888
|
if (knownMessageShas.has(sha256))
|
|
@@ -2894,7 +2894,7 @@ async function createRunProviderLedger(input) {
|
|
|
2894
2894
|
});
|
|
2895
2895
|
knownMessageShas.add(sha256);
|
|
2896
2896
|
}
|
|
2897
|
-
const instructionsSha256 = createHash3("sha256").update(typeof request.instructions === "string" ? request.instructions :
|
|
2897
|
+
const instructionsSha256 = createHash3("sha256").update(typeof request.instructions === "string" ? request.instructions : canonicalAgentJson2(JSON.parse(JSON.stringify(withoutBinaryParts(request.instructions))))).digest("hex");
|
|
2898
2898
|
const bodySha256 = createHash3("sha256").update(instructionsSha256).update(messageShas.join(",")).digest("hex");
|
|
2899
2899
|
await store.appendEvent({
|
|
2900
2900
|
conversationId: state.run.conversationId,
|
|
@@ -2999,7 +2999,7 @@ async function applyToolPart(execution, part) {
|
|
|
2999
2999
|
const { state } = execution;
|
|
3000
3000
|
if (part.type === "tool-call") {
|
|
3001
3001
|
const provider = providerEnvelope(part.providerMetadata);
|
|
3002
|
-
state.parts.push(
|
|
3002
|
+
state.parts.push(AgentMessagePartSchema2.parse({
|
|
3003
3003
|
type: "tool-call",
|
|
3004
3004
|
callId: part.toolCallId,
|
|
3005
3005
|
toolName: part.toolName,
|
|
@@ -3016,7 +3016,7 @@ async function applyToolPart(execution, part) {
|
|
|
3016
3016
|
emittedAt: now().toISOString()
|
|
3017
3017
|
});
|
|
3018
3018
|
} else if (part.type === "tool-result") {
|
|
3019
|
-
state.parts.push(
|
|
3019
|
+
state.parts.push(AgentMessagePartSchema2.parse({
|
|
3020
3020
|
type: "tool-result",
|
|
3021
3021
|
callId: part.toolCallId,
|
|
3022
3022
|
toolName: part.toolName,
|
|
@@ -3034,7 +3034,7 @@ async function applyToolPart(execution, part) {
|
|
|
3034
3034
|
});
|
|
3035
3035
|
} else if (part.type === "tool-error") {
|
|
3036
3036
|
state.internalCause = part.error;
|
|
3037
|
-
if (
|
|
3037
|
+
if (isToolExecutionControlError2(part.error)) {
|
|
3038
3038
|
await publish({
|
|
3039
3039
|
type: "tool-status",
|
|
3040
3040
|
...transientHead(execution),
|
|
@@ -3046,8 +3046,8 @@ async function applyToolPart(execution, part) {
|
|
|
3046
3046
|
await checkpoint(execution);
|
|
3047
3047
|
throw part.error;
|
|
3048
3048
|
}
|
|
3049
|
-
const output =
|
|
3050
|
-
state.parts.push(
|
|
3049
|
+
const output = isAgentToolError2(part.error) ? jsonValue(part.error.output) : { message: "Tool execution failed" };
|
|
3050
|
+
state.parts.push(AgentMessagePartSchema2.parse({
|
|
3051
3051
|
type: "tool-result",
|
|
3052
3052
|
callId: part.toolCallId,
|
|
3053
3053
|
toolName: part.toolName,
|
|
@@ -3064,7 +3064,7 @@ async function applyToolPart(execution, part) {
|
|
|
3064
3064
|
emittedAt: now().toISOString()
|
|
3065
3065
|
});
|
|
3066
3066
|
} else if (part.type === "tool-output-denied") {
|
|
3067
|
-
state.parts.push(
|
|
3067
|
+
state.parts.push(AgentMessagePartSchema2.parse({
|
|
3068
3068
|
type: "tool-result",
|
|
3069
3069
|
callId: part.toolCallId,
|
|
3070
3070
|
toolName: part.toolName,
|
|
@@ -3086,7 +3086,7 @@ async function applyRecordedPart(execution, part) {
|
|
|
3086
3086
|
const { config } = execution.dependencies;
|
|
3087
3087
|
const { parts } = execution.state;
|
|
3088
3088
|
if (part.type === "source") {
|
|
3089
|
-
parts.push(
|
|
3089
|
+
parts.push(AgentMessagePartSchema2.parse({
|
|
3090
3090
|
type: "source",
|
|
3091
3091
|
sourceId: part.id,
|
|
3092
3092
|
...part.sourceType === "url" && { url: part.url },
|
|
@@ -3094,7 +3094,7 @@ async function applyRecordedPart(execution, part) {
|
|
|
3094
3094
|
}));
|
|
3095
3095
|
} else if (part.type === "file" && config.persistGeneratedFile) {
|
|
3096
3096
|
const persisted = await config.persistGeneratedFile(part.file);
|
|
3097
|
-
parts.push(
|
|
3097
|
+
parts.push(AgentMessagePartSchema2.parse({
|
|
3098
3098
|
type: "file",
|
|
3099
3099
|
mediaType: part.file.mediaType,
|
|
3100
3100
|
reference: persisted.reference,
|
|
@@ -3104,7 +3104,7 @@ async function applyRecordedPart(execution, part) {
|
|
|
3104
3104
|
throw new Error("persistGeneratedFile is required for generated file output");
|
|
3105
3105
|
} else if (part.type === "reasoning-file" && config.persistGeneratedFile) {
|
|
3106
3106
|
const persisted = await config.persistGeneratedFile(part.file);
|
|
3107
|
-
parts.push(
|
|
3107
|
+
parts.push(AgentMessagePartSchema2.parse({
|
|
3108
3108
|
type: "file",
|
|
3109
3109
|
mediaType: part.file.mediaType,
|
|
3110
3110
|
reference: persisted.reference,
|
|
@@ -3113,7 +3113,7 @@ async function applyRecordedPart(execution, part) {
|
|
|
3113
3113
|
} else if (part.type === "reasoning-file") {
|
|
3114
3114
|
throw new Error("persistGeneratedFile is required for generated reasoning files");
|
|
3115
3115
|
} else if (part.type === "tool-approval-request") {
|
|
3116
|
-
parts.push(
|
|
3116
|
+
parts.push(AgentMessagePartSchema2.parse({
|
|
3117
3117
|
type: "tool-approval-request",
|
|
3118
3118
|
approvalId: part.approvalId,
|
|
3119
3119
|
callId: part.toolCall.toolCallId,
|
|
@@ -3121,7 +3121,7 @@ async function applyRecordedPart(execution, part) {
|
|
|
3121
3121
|
...part.signature && { signature: part.signature }
|
|
3122
3122
|
}));
|
|
3123
3123
|
} else if (part.type === "tool-approval-response") {
|
|
3124
|
-
parts.push(
|
|
3124
|
+
parts.push(AgentMessagePartSchema2.parse({
|
|
3125
3125
|
type: "tool-approval-response",
|
|
3126
3126
|
approvalId: part.approvalId,
|
|
3127
3127
|
approved: part.approved,
|
|
@@ -3129,7 +3129,7 @@ async function applyRecordedPart(execution, part) {
|
|
|
3129
3129
|
}));
|
|
3130
3130
|
} else if (part.type === "custom") {
|
|
3131
3131
|
const provider = providerEnvelope(part.providerMetadata);
|
|
3132
|
-
parts.push(
|
|
3132
|
+
parts.push(AgentMessagePartSchema2.parse({
|
|
3133
3133
|
type: "provider",
|
|
3134
3134
|
envelope: {
|
|
3135
3135
|
schemaVersion: 1,
|
|
@@ -3138,7 +3138,7 @@ async function applyRecordedPart(execution, part) {
|
|
|
3138
3138
|
}
|
|
3139
3139
|
}));
|
|
3140
3140
|
} else if (part.type === "raw") {
|
|
3141
|
-
parts.push(
|
|
3141
|
+
parts.push(AgentMessagePartSchema2.parse({
|
|
3142
3142
|
type: "provider",
|
|
3143
3143
|
envelope: {
|
|
3144
3144
|
schemaVersion: 1,
|
|
@@ -3334,7 +3334,7 @@ function startAttemptStream(context, attempt, stopConditions) {
|
|
|
3334
3334
|
toolCallId,
|
|
3335
3335
|
signal
|
|
3336
3336
|
});
|
|
3337
|
-
return
|
|
3337
|
+
return createLocalStepDurability2({
|
|
3338
3338
|
store: config.store,
|
|
3339
3339
|
conversationId: state.run.conversationId,
|
|
3340
3340
|
runId: JSON.stringify([state.run.id, toolName, toolCallId]),
|
|
@@ -3392,11 +3392,11 @@ async function retryAfterProviderError(context, attempt, error) {
|
|
|
3392
3392
|
if (!retry || failureThisRuntimeOwns(error) !== undefined)
|
|
3393
3393
|
return false;
|
|
3394
3394
|
const invokedTool = state.parts.slice(attempt.attemptPartStart).some((candidate) => candidate.type === "tool-call");
|
|
3395
|
-
const decision = invokedTool ? { retry: false, delayMs: 0 } : await
|
|
3395
|
+
const decision = invokedTool ? { retry: false, delayMs: 0 } : await recordAgentRetryDecision2({
|
|
3396
3396
|
store: config.store,
|
|
3397
3397
|
conversationId: state.run.conversationId,
|
|
3398
3398
|
attempt: loop.retryAttempt,
|
|
3399
|
-
failure:
|
|
3399
|
+
failure: classifyProviderFailure2(error),
|
|
3400
3400
|
policy: retry
|
|
3401
3401
|
});
|
|
3402
3402
|
if (!decision.retry)
|
|
@@ -3521,7 +3521,7 @@ async function settleStreamOutcome(execution) {
|
|
|
3521
3521
|
state.terminalReason = abortTerminalReason(executionSignal);
|
|
3522
3522
|
const awaitsApproval = state.parts.some((part) => part.type === "tool-approval-request");
|
|
3523
3523
|
if (!awaitsApproval && config.protocol.acceptTerminal && (state.terminalReason === "success" || state.terminalReason === "policy_stop" || state.terminalReason === "provider_stop")) {
|
|
3524
|
-
const candidate =
|
|
3524
|
+
const candidate = AgentMessageSchema2.parse({
|
|
3525
3525
|
...state.assistant,
|
|
3526
3526
|
status: assistantStatus(state.terminalReason),
|
|
3527
3527
|
parts: state.parts,
|
|
@@ -3573,14 +3573,14 @@ async function stopChildrenAfterExplicitStop(execution, committedByCaller) {
|
|
|
3573
3573
|
async function commitRunTerminal(execution) {
|
|
3574
3574
|
const { config, publish, runtimeEpoch, now } = execution.dependencies;
|
|
3575
3575
|
const { state, runStartedAt } = execution;
|
|
3576
|
-
state.assistant =
|
|
3576
|
+
state.assistant = AgentMessageSchema2.parse({
|
|
3577
3577
|
...state.assistant,
|
|
3578
3578
|
status: assistantStatus(state.terminalReason),
|
|
3579
3579
|
parts: state.parts,
|
|
3580
3580
|
updatedAt: now().toISOString()
|
|
3581
3581
|
});
|
|
3582
3582
|
const spent = statedUsage(state.usage);
|
|
3583
|
-
const unsettledEventId = (version) =>
|
|
3583
|
+
const unsettledEventId = (version) => agentDurableEventId2("terminal", `${state.run.id}:${runtimeEpoch}`, version);
|
|
3584
3584
|
let terminal;
|
|
3585
3585
|
try {
|
|
3586
3586
|
terminal = await commitAgentRunTerminal({
|
|
@@ -3592,7 +3592,7 @@ async function commitRunTerminal(execution) {
|
|
|
3592
3592
|
reason: state.terminalReason,
|
|
3593
3593
|
...state.terminalPolicyName && { policyName: state.terminalPolicyName },
|
|
3594
3594
|
usage: spent,
|
|
3595
|
-
...state.absorbed.size > 0 &&
|
|
3595
|
+
...state.absorbed.size > 0 && runStateForTerminalReason2(state.terminalReason) === "completed" && {
|
|
3596
3596
|
absorb: [...state.absorbed].map(([runId, inputMessageIds]) => ({
|
|
3597
3597
|
runId,
|
|
3598
3598
|
inputMessageIds
|
|
@@ -3625,7 +3625,7 @@ async function commitRunTerminal(execution) {
|
|
|
3625
3625
|
} : undefined;
|
|
3626
3626
|
await stopChildrenAfterExplicitStop(execution, terminal.committedByCaller);
|
|
3627
3627
|
emitSpend(execution, {
|
|
3628
|
-
eventId: terminal.committedByCaller ?
|
|
3628
|
+
eventId: terminal.committedByCaller ? agentDurableEventId2("terminal", state.run.id, state.observedVersion) : unsettledEventId(state.observedVersion),
|
|
3629
3629
|
state: state.run.state,
|
|
3630
3630
|
reason: state.terminalReason,
|
|
3631
3631
|
usage: spent
|
|
@@ -3633,7 +3633,7 @@ async function commitRunTerminal(execution) {
|
|
|
3633
3633
|
for (const settled of terminal.absorbed ?? []) {
|
|
3634
3634
|
await publish({
|
|
3635
3635
|
type: "run-state",
|
|
3636
|
-
eventId:
|
|
3636
|
+
eventId: agentDurableEventId2("run-state", settled.id, state.observedVersion),
|
|
3637
3637
|
conversationId: settled.conversationId,
|
|
3638
3638
|
runId: settled.id,
|
|
3639
3639
|
snapshotVersion: state.observedVersion,
|
|
@@ -3644,7 +3644,7 @@ async function commitRunTerminal(execution) {
|
|
|
3644
3644
|
if (terminalMetrics) {
|
|
3645
3645
|
await publish({
|
|
3646
3646
|
type: "terminal",
|
|
3647
|
-
eventId:
|
|
3647
|
+
eventId: agentDurableEventId2("terminal", state.run.id, state.observedVersion),
|
|
3648
3648
|
conversationId: state.run.conversationId,
|
|
3649
3649
|
runId: state.run.id,
|
|
3650
3650
|
snapshotVersion: state.observedVersion,
|
|
@@ -3670,20 +3670,20 @@ import { createHash as createHash4 } from "node:crypto";
|
|
|
3670
3670
|
function snapshotForRunPrompt(snapshot, runId) {
|
|
3671
3671
|
const runIndex = snapshot.runs.findIndex((candidate) => candidate.id === runId);
|
|
3672
3672
|
if (runIndex < 0)
|
|
3673
|
-
throw new
|
|
3673
|
+
throw new AgentRuntimeConflictError2("prompt run lookup");
|
|
3674
3674
|
const ownedIds = new Set(snapshot.runs.flatMap((candidate) => [
|
|
3675
3675
|
...candidate.inputMessageIds,
|
|
3676
3676
|
candidate.assistantMessageId
|
|
3677
3677
|
]));
|
|
3678
3678
|
const eligibleIds = new Set(snapshot.runs.slice(0, runIndex + 1).flatMap((candidate) => [...candidate.inputMessageIds, candidate.assistantMessageId]));
|
|
3679
|
-
return
|
|
3679
|
+
return AgentSnapshotSchema2.parse({
|
|
3680
3680
|
...snapshot,
|
|
3681
3681
|
messages: snapshot.messages.filter((message) => !ownedIds.has(message.id) || eligibleIds.has(message.id))
|
|
3682
3682
|
});
|
|
3683
3683
|
}
|
|
3684
3684
|
var USER_INSTRUCTION_SEED_KEY = "prompt.user-instructions";
|
|
3685
3685
|
function seededInstructionMessage(input) {
|
|
3686
|
-
return
|
|
3686
|
+
return AgentMessageSchema2.parse({
|
|
3687
3687
|
schemaVersion: 1,
|
|
3688
3688
|
id: createHash4("sha256").update(`${input.conversationId}\x00${input.seedKey}\x00${input.index}`).digest("hex"),
|
|
3689
3689
|
conversationId: input.conversationId,
|
|
@@ -3696,18 +3696,18 @@ function seededInstructionMessage(input) {
|
|
|
3696
3696
|
}
|
|
3697
3697
|
async function loadDurableSystem(config, conversationId) {
|
|
3698
3698
|
if (config.stateSlots && config.stateSlots.length > 0) {
|
|
3699
|
-
const slotValues = await
|
|
3699
|
+
const slotValues = await createAgentStateSlotStore2({
|
|
3700
3700
|
store: config.store,
|
|
3701
3701
|
definitions: config.stateSlots
|
|
3702
3702
|
}).list(conversationId);
|
|
3703
|
-
const renderedSlots =
|
|
3703
|
+
const renderedSlots = renderAgentStateSlots2(slotValues);
|
|
3704
3704
|
if (renderedSlots)
|
|
3705
3705
|
return [renderedSlots];
|
|
3706
3706
|
}
|
|
3707
3707
|
return [];
|
|
3708
3708
|
}
|
|
3709
3709
|
function detailedProjection(config, source) {
|
|
3710
|
-
return
|
|
3710
|
+
return projectAgentHistoryDetailed2(source, {
|
|
3711
3711
|
...config.history?.resolveFile && { resolveFile: config.history.resolveFile },
|
|
3712
3712
|
...config.history?.unresolvedFile && {
|
|
3713
3713
|
unresolvedFile: config.history.unresolvedFile
|
|
@@ -3811,7 +3811,7 @@ function createRuntimeContext(execution) {
|
|
|
3811
3811
|
return "stale_run";
|
|
3812
3812
|
return;
|
|
3813
3813
|
};
|
|
3814
|
-
const toolFenceLifecycle =
|
|
3814
|
+
const toolFenceLifecycle = createAgentToolFenceLifecycle2({
|
|
3815
3815
|
runId: state.run.id,
|
|
3816
3816
|
assertCurrent,
|
|
3817
3817
|
context: () => ({
|
|
@@ -3886,10 +3886,10 @@ async function prepareTurn(execution) {
|
|
|
3886
3886
|
if (prompt.finalizeSeed)
|
|
3887
3887
|
prompt = { ...prompt, ...prompt.finalizeSeed(userInstructionsInserted ?? false) };
|
|
3888
3888
|
if (prompt.contextDecision === "oversized") {
|
|
3889
|
-
throw new
|
|
3889
|
+
throw new AgentContextOverflowError2;
|
|
3890
3890
|
}
|
|
3891
3891
|
if (prompt.contextDecision === "requires-compaction") {
|
|
3892
|
-
throw new
|
|
3892
|
+
throw new AgentContextOverflowError2("Agent context still exceeds the model budget after compaction");
|
|
3893
3893
|
}
|
|
3894
3894
|
const projection = createRunHistoryProjection({
|
|
3895
3895
|
config,
|
|
@@ -3960,7 +3960,7 @@ function resolveLoopLimits(config) {
|
|
|
3960
3960
|
};
|
|
3961
3961
|
}
|
|
3962
3962
|
function createAgentRuntimeState(config) {
|
|
3963
|
-
const coordinator =
|
|
3963
|
+
const coordinator = createAgentSessionCoordinator2();
|
|
3964
3964
|
const generateId = config.generateId ?? (() => crypto.randomUUID());
|
|
3965
3965
|
const now = config.now ?? (() => new Date);
|
|
3966
3966
|
const runtimeEpoch = generateId();
|
|
@@ -4048,7 +4048,7 @@ function createSubmission(state, rawInput, input, context) {
|
|
|
4048
4048
|
const inputMessageId = rawInput.recordIds?.inputMessageId ?? generateId();
|
|
4049
4049
|
const runId = rawInput.recordIds?.runId ?? generateId();
|
|
4050
4050
|
const assistantMessageId = rawInput.recordIds?.assistantMessageId ?? generateId();
|
|
4051
|
-
const
|
|
4051
|
+
const userMessage = AgentMessageSchema2.parse({
|
|
4052
4052
|
schemaVersion: 1,
|
|
4053
4053
|
id: inputMessageId,
|
|
4054
4054
|
conversationId: input.conversationId,
|
|
@@ -4059,7 +4059,7 @@ function createSubmission(state, rawInput, input, context) {
|
|
|
4059
4059
|
createdAt: nowIso,
|
|
4060
4060
|
updatedAt: nowIso
|
|
4061
4061
|
});
|
|
4062
|
-
const queuedRun =
|
|
4062
|
+
const queuedRun = AgentRunSchema2.parse({
|
|
4063
4063
|
schemaVersion: 1,
|
|
4064
4064
|
id: runId,
|
|
4065
4065
|
conversationId: input.conversationId,
|
|
@@ -4094,7 +4094,7 @@ function createSubmission(state, rawInput, input, context) {
|
|
|
4094
4094
|
key,
|
|
4095
4095
|
policy,
|
|
4096
4096
|
runId,
|
|
4097
|
-
userMessage
|
|
4097
|
+
userMessage,
|
|
4098
4098
|
queuedRun,
|
|
4099
4099
|
...reservation && { reservation },
|
|
4100
4100
|
previousAcceptance,
|
|
@@ -4187,9 +4187,9 @@ function projectAdmission(submission, acceptance) {
|
|
|
4187
4187
|
const actualInputMessageId = acceptance.outcome === "duplicate" ? acceptance.inputMessageId : submission.userMessage.id;
|
|
4188
4188
|
const acceptedInput = acceptance.outcome === "duplicate" ? acceptance.input : acceptedSnapshot.messages.find((candidate) => candidate.id === actualInputMessageId);
|
|
4189
4189
|
if (!acceptedInput) {
|
|
4190
|
-
throw new
|
|
4190
|
+
throw new AgentRuntimeConflictError2("admission input projection");
|
|
4191
4191
|
}
|
|
4192
|
-
const assistantPlaceholder =
|
|
4192
|
+
const assistantPlaceholder = AgentAssistantPlaceholderSchema2.parse({
|
|
4193
4193
|
schemaVersion: 1,
|
|
4194
4194
|
id: acceptedRun.assistantMessageId,
|
|
4195
4195
|
conversationId: acceptedRun.conversationId,
|
|
@@ -4213,7 +4213,7 @@ async function publishAdmission(state, admission) {
|
|
|
4213
4213
|
const { run, snapshotVersion } = admission;
|
|
4214
4214
|
await state.publish({
|
|
4215
4215
|
type: "admission",
|
|
4216
|
-
eventId:
|
|
4216
|
+
eventId: agentDurableEventId2("admission", run.id, snapshotVersion),
|
|
4217
4217
|
conversationId: run.conversationId,
|
|
4218
4218
|
runId: run.id,
|
|
4219
4219
|
snapshotVersion,
|
|
@@ -4224,7 +4224,7 @@ async function publishAdmission(state, admission) {
|
|
|
4224
4224
|
});
|
|
4225
4225
|
await state.publish({
|
|
4226
4226
|
type: "run-state",
|
|
4227
|
-
eventId:
|
|
4227
|
+
eventId: agentDurableEventId2("run-state", run.id, snapshotVersion),
|
|
4228
4228
|
conversationId: run.conversationId,
|
|
4229
4229
|
runId: run.id,
|
|
4230
4230
|
snapshotVersion,
|
|
@@ -4297,7 +4297,7 @@ function handOffToCoordinator(state, submission, acceptedRun) {
|
|
|
4297
4297
|
}
|
|
4298
4298
|
|
|
4299
4299
|
// src/agent-runtime/runtime.ts
|
|
4300
|
-
function
|
|
4300
|
+
function createAgentRuntime2(config) {
|
|
4301
4301
|
const state = createAgentRuntimeState(config);
|
|
4302
4302
|
return {
|
|
4303
4303
|
submit: (rawInput) => submitAgentInput(state, rawInput),
|
|
@@ -4310,4 +4310,4 @@ function createAgentRuntime(config) {
|
|
|
4310
4310
|
};
|
|
4311
4311
|
}
|
|
4312
4312
|
|
|
4313
|
-
export {
|
|
4313
|
+
export { AgentRuntimeConflictError2, normalizeSdkUsage, AgentContextOverflowError2, createAgentSessionCoordinator2, registerDeferredToolRepair, registerDeferredToolCatalog, deferredToolCatalog, repairedSearchCall, createAgentRuntimeEventSink2, projectAgentHistoryDetailed2, projectAgentHistory2, ModelInvocationTraceSchema2, CompletionInvocationInputSchema2, ModelInvocationRecordSchema2, currentModelInvocationAttempt2, createModelInvocationLedger2, createAgentToolFenceLifecycle2, AgentTokenCountSchema2, selectAgentHistory2, composeAgentPrompt2, AgentProviderStreamCutError2, classifyProviderFailure2, isToolResultFailure2, recordAgentRetryDecision2, defineStateSlot2, createAgentStateSlotStore2, renderAgentStateSlots2, agentGoalStateSlot2, agentTodoStateSlot2, createAgentRuntime2 };
|