@zvada/agent-server 0.3.23 → 0.3.24
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 +34 -0
- package/README.md +14 -5
- package/dist/accounts/chatgpt.js +144 -0
- package/dist/accounts/check.js +71 -0
- package/dist/accounts/claude.js +80 -0
- package/dist/accounts/endpoint.js +104 -0
- package/dist/accounts/http.js +58 -0
- package/dist/accounts/index.js +9 -0
- package/dist/client/client.js +686 -0
- package/dist/client/connection.js +338 -0
- package/dist/client/history.js +49 -0
- package/dist/client/index.js +6 -0
- package/dist/client/listeners.js +19 -0
- package/dist/client/logins.js +92 -0
- package/dist/client/rpc.js +105 -0
- package/dist/client/streams.js +432 -0
- package/dist/client/transports.js +118 -0
- package/dist/client/turns.js +122 -0
- package/dist/core/agents/acp/acp-agent.js +425 -0
- package/dist/core/agents/acp/adapter.js +231 -0
- package/dist/core/agents/acp/client.js +222 -0
- package/dist/core/agents/acp/known-agents.js +57 -0
- package/dist/core/agents/acp/mappings.js +164 -0
- package/dist/core/agents/acp/requests.js +79 -0
- package/dist/core/agents/attachments.js +14 -0
- package/dist/core/agents/base.js +64 -0
- package/dist/core/agents/claude-code/adapter.js +474 -0
- package/dist/core/agents/claude-code/approvals.js +245 -0
- package/dist/core/agents/claude-code/claude-agent.js +319 -0
- package/dist/core/agents/claude-code/credentials.js +142 -0
- package/dist/core/agents/claude-code/events.js +11 -0
- package/dist/core/agents/claude-code/login.js +217 -0
- package/dist/core/agents/claude-code/mcp.js +181 -0
- package/dist/core/agents/claude-code/models.js +74 -0
- package/dist/core/agents/claude-code/options.js +240 -0
- package/dist/core/agents/claude-code/outcome.js +204 -0
- package/dist/core/agents/claude-code/results.js +58 -0
- package/dist/core/agents/claude-code/sdk-pair.js +59 -0
- package/dist/core/agents/claude-code/session.js +593 -0
- package/dist/core/agents/claude-code/spawn.js +125 -0
- package/dist/core/agents/claude-code/spend.js +118 -0
- package/dist/core/agents/claude-code/tools.js +131 -0
- package/dist/core/agents/claude-code/turn-usage.js +121 -0
- package/dist/core/agents/claude-code/turn.js +199 -0
- package/dist/core/agents/codex-app-server/adapter.js +463 -0
- package/dist/core/agents/codex-app-server/client.js +338 -0
- package/dist/core/agents/codex-app-server/codex-agent.js +294 -0
- package/dist/core/agents/codex-app-server/commands.js +184 -0
- package/dist/core/agents/codex-app-server/credentials.js +92 -0
- package/dist/core/agents/codex-app-server/effort.js +13 -0
- package/dist/core/agents/codex-app-server/events.js +18 -0
- package/dist/core/agents/codex-app-server/homes.js +118 -0
- package/dist/core/agents/codex-app-server/input.js +52 -0
- package/dist/core/agents/codex-app-server/items.js +307 -0
- package/dist/core/agents/codex-app-server/login.js +183 -0
- package/dist/core/agents/codex-app-server/mcp.js +323 -0
- package/dist/core/agents/codex-app-server/models.js +160 -0
- package/dist/core/agents/codex-app-server/options.js +155 -0
- package/dist/core/agents/codex-app-server/plan-approval.js +124 -0
- package/dist/core/agents/codex-app-server/protocol.js +38 -0
- package/dist/core/agents/codex-app-server/requests.js +250 -0
- package/dist/core/agents/codex-app-server/session.js +278 -0
- package/dist/core/agents/codex-app-server/subagents.js +260 -0
- package/dist/core/agents/codex-app-server/thread-settings.js +214 -0
- package/dist/core/agents/codex-app-server/turn-items.js +69 -0
- package/dist/core/agents/codex-app-server/turn.js +448 -0
- package/dist/core/agents/codex-app-server/user-login.js +78 -0
- package/dist/core/agents/config-fingerprint.js +10 -0
- package/dist/core/agents/credentials.js +71 -0
- package/dist/core/agents/diffs.js +27 -0
- package/dist/core/agents/elicitation.js +42 -0
- package/dist/core/agents/inherit-config.js +5 -0
- package/dist/core/agents/login.js +56 -0
- package/dist/core/agents/mcp-config.js +109 -0
- package/dist/core/agents/plan.js +6 -0
- package/dist/core/agents/process-pool.js +307 -0
- package/dist/core/agents/registry.js +49 -0
- package/dist/core/agents/scripted/adapter.js +86 -0
- package/dist/core/agents/scripted/mcp-call.js +74 -0
- package/dist/core/agents/scripted/scripted-agent.js +330 -0
- package/dist/core/agents/scripted/scripts.js +154 -0
- package/dist/core/agents/scripted/steps.js +87 -0
- package/dist/core/agents/session-agent.js +209 -0
- package/dist/core/agents/stderr-tail.js +71 -0
- package/dist/core/agents/thinking-levels.js +9 -0
- package/dist/core/agents/types.js +1 -0
- package/dist/core/diagnostics.js +23 -0
- package/dist/core/index.js +31 -0
- package/dist/core/presets.js +114 -0
- package/dist/core/processes/identity.js +91 -0
- package/dist/core/processes/lifeline.js +210 -0
- package/dist/core/processes/native-processes.js +299 -0
- package/dist/core/processes/process-table.js +163 -0
- package/dist/core/provision/cache-trust.js +121 -0
- package/dist/core/provision/cache-usage.js +61 -0
- package/dist/core/provision/checksums.js +22 -0
- package/dist/core/provision/extract.js +28 -0
- package/dist/core/provision/index.js +4 -0
- package/dist/core/provision/install-build.js +99 -0
- package/dist/core/provision/npm.js +316 -0
- package/dist/core/provision/pins.js +35 -0
- package/dist/core/provision/platform.js +65 -0
- package/dist/core/provision/provisioner.js +372 -0
- package/dist/core/provision/releases.js +68 -0
- package/dist/core/provision/tools.js +279 -0
- package/dist/core/proxy/anthropic-proxy.js +163 -0
- package/dist/core/proxy/api-key-store.js +30 -0
- package/dist/core/proxy/index.js +3 -0
- package/dist/core/proxy/loopback.js +86 -0
- package/dist/core/runtime/agent-runtime.js +434 -0
- package/dist/core/runtime/event-processor.js +379 -0
- package/dist/core/runtime/event-sink.js +65 -0
- package/dist/core/runtime/harness-clis.js +83 -0
- package/dist/core/runtime/harness-logins.js +313 -0
- package/dist/core/runtime/login-reads.js +71 -0
- package/dist/core/runtime/model-lists.js +160 -0
- package/dist/core/runtime/permission-policy.js +84 -0
- package/dist/core/runtime/request-broker.js +139 -0
- package/dist/core/runtime/steering.js +79 -0
- package/dist/core/runtime/turn-outcome.js +16 -0
- package/dist/core/runtime/turn.js +329 -0
- package/dist/core/utils/bounded.js +13 -0
- package/dist/core/utils/deadline.js +57 -0
- package/dist/core/utils/errors.js +176 -0
- package/dist/core/utils/is-record.js +4 -0
- package/dist/core/utils/loopback.js +11 -0
- package/dist/index.js +4 -0
- package/dist/operator/index.js +10 -0
- package/dist/operator/mcp.js +139 -0
- package/dist/operator/operator.js +499 -0
- package/dist/operator/render.js +408 -0
- package/dist/operator/view.js +35 -0
- package/dist/protocol/account.js +96 -0
- package/dist/protocol/async-queue.js +65 -0
- package/dist/protocol/auth.js +169 -0
- package/dist/protocol/canonical.js +18 -0
- package/dist/protocol/clis.js +63 -0
- package/dist/protocol/config.js +137 -0
- package/dist/protocol/conversation.js +231 -0
- package/dist/protocol/credentials.js +141 -0
- package/dist/protocol/errors.js +181 -0
- package/dist/protocol/execution.js +28 -0
- package/dist/protocol/factories.js +231 -0
- package/dist/protocol/guards.js +71 -0
- package/dist/protocol/harness.js +93 -0
- package/dist/protocol/history.js +201 -0
- package/dist/protocol/ids.js +37 -0
- package/dist/protocol/index.js +36 -0
- package/dist/protocol/lifecycle.js +562 -0
- package/dist/protocol/meta-keys.js +17 -0
- package/dist/protocol/meta.js +13 -0
- package/dist/protocol/model-catalog.js +223 -0
- package/dist/protocol/models.js +84 -0
- package/dist/protocol/part-input.js +94 -0
- package/dist/protocol/parts.js +166 -0
- package/dist/protocol/plan.js +31 -0
- package/dist/protocol/reduce.js +484 -0
- package/dist/protocol/selectors.js +136 -0
- package/dist/protocol/seq-cursor.js +37 -0
- package/dist/protocol/stop-reasons.js +41 -0
- package/dist/protocol/thinking.js +31 -0
- package/dist/protocol/time.js +21 -0
- package/dist/protocol/tokens.js +57 -0
- package/dist/protocol/tool-closure.js +70 -0
- package/dist/protocol/tool-state.js +153 -0
- package/dist/protocol/verify.js +467 -0
- package/dist/protocol/vocabulary.js +27 -0
- package/dist/protocol/wire-api.js +94 -0
- package/dist/protocol/wire.js +669 -0
- package/dist/server/acp/binding.js +251 -0
- package/dist/server/acp/session-records.js +53 -0
- package/dist/server/acp/translate.js +401 -0
- package/dist/server/agent-server.js +338 -0
- package/dist/server/bin.js +492 -0
- package/dist/server/feedback.js +216 -0
- package/dist/server/history.js +123 -0
- package/dist/server/index.js +8 -0
- package/dist/server/install.js +171 -0
- package/dist/server/open-parts.js +87 -0
- package/dist/server/routes.js +291 -0
- package/dist/server/secrets.js +19 -0
- package/dist/server/session-gate.js +54 -0
- package/dist/server/session-log.js +66 -0
- package/dist/server/session-store.js +243 -0
- package/dist/server/subscriptions.js +189 -0
- package/dist/server/transports.js +214 -0
- package/dist/server/wire-error.js +26 -0
- package/dist/sync/event-log.js +89 -0
- package/dist/sync/fit.js +202 -0
- package/dist/sync/history-reader.js +33 -0
- package/dist/sync/history-sqlite.js +281 -0
- package/dist/sync/history.js +163 -0
- package/dist/sync/index.js +5 -0
- package/dist/sync/journal-sqlite.js +316 -0
- package/dist/sync/journal.js +10 -0
- package/dist/sync/live-tails.js +85 -0
- package/dist/sync/node-sqlite.js +27 -0
- package/dist/sync/projection.js +32 -0
- package/dist/sync/recovery.js +117 -0
- package/dist/sync/retry.js +45 -0
- package/dist/sync/schema.js +73 -0
- package/dist/sync/sql.js +94 -0
- package/dist/sync/sqlite-file.js +31 -0
- package/dist/sync/sqlite.js +18 -0
- package/dist/sync/worker.js +212 -0
- package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts +4 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/options.d.ts +24 -11
- package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/session.d.ts +8 -0
- package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/homes.d.ts +6 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts +12 -0
- package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts +4 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
- package/dist/types/core/agents/process-pool.d.ts +8 -0
- package/dist/types/core/agents/process-pool.d.ts.map +1 -1
- package/dist/types/core/presets.d.ts +2 -2
- package/dist/types/core/presets.d.ts.map +1 -1
- package/dist/types/server/bin.d.ts +3 -0
- package/dist/types/server/bin.d.ts.map +1 -0
- package/dist/types/server/transports.d.ts +2 -1
- package/dist/types/server/transports.d.ts.map +1 -1
- package/dist/types/sync/node-sqlite.d.ts +4 -1
- package/dist/types/sync/node-sqlite.d.ts.map +1 -1
- package/docs/consuming.md +30 -4
- package/docs/deploy.md +28 -0
- package/docs/harnesses.md +13 -1
- package/package.json +43 -18
- package/src/core/agents/claude-code/approvals.ts +5 -1
- package/src/core/agents/claude-code/claude-agent.ts +32 -5
- package/src/core/agents/claude-code/options.ts +24 -11
- package/src/core/agents/claude-code/session.ts +13 -0
- package/src/core/agents/codex-app-server/homes.ts +11 -2
- package/src/core/agents/codex-app-server/login.ts +30 -0
- package/src/core/agents/codex-app-server/requests.ts +7 -6
- package/src/core/agents/codex-app-server/session.ts +11 -2
- package/src/core/agents/process-pool.ts +30 -4
- package/src/core/presets.ts +3 -2
- package/src/server/bin.ts +25 -10
- package/src/server/transports.ts +6 -1
- package/src/sync/node-sqlite.ts +19 -3
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
import { conversationMessages, emptyConversation } from "./conversation.js";
|
|
2
|
+
import { steerMessageId } from "./factories.js";
|
|
3
|
+
import { isLiveOnlyEvent, isUnknownEvent } from "./guards.js";
|
|
4
|
+
import { decodeLifecycleEvent, } from "./lifecycle.js";
|
|
5
|
+
import { reduceConversation } from "./reduce.js";
|
|
6
|
+
// @ref LLP 0002#ordering-and-delivery — the rules this checks, by id
|
|
7
|
+
/**
|
|
8
|
+
* Machine-checkable stream-contract verifier: replay the log, validate the
|
|
9
|
+
* projection, fail on disagreement.
|
|
10
|
+
*
|
|
11
|
+
* `verifyStreamContract` takes one session's ordered event stream and returns
|
|
12
|
+
* every violation of the ordering and delivery contract it can detect,
|
|
13
|
+
* plus a projection check: the reducer's final state must agree with the
|
|
14
|
+
* authoritative part snapshots (snapshots-beat-deltas made testable).
|
|
15
|
+
*
|
|
16
|
+
* Pure and consumer-grade: the CLI's `--verify` flag, the live tests, and any
|
|
17
|
+
* product pipeline (a Durable Object, a persistence shim) can run it over a
|
|
18
|
+
* recorded stream.
|
|
19
|
+
*
|
|
20
|
+
* The ordering bookkeeping below (turns, open messages, part addresses,
|
|
21
|
+
* pending permissions) deliberately does NOT read `ConversationState`, even
|
|
22
|
+
* though the fold computes lookalikes of most of it: a checker derived from
|
|
23
|
+
* the reducer can only ever confirm that the reducer agrees with itself. The
|
|
24
|
+
* duplication is the independence that makes `projection-agrees` and the
|
|
25
|
+
* re-delivery probe able to fail.
|
|
26
|
+
*/
|
|
27
|
+
/** Every rule the verifier checks, by the id LLP 0002 names it with. */
|
|
28
|
+
export const CONTRACT_RULES = [
|
|
29
|
+
// Turns
|
|
30
|
+
"turn-started-once",
|
|
31
|
+
"turn-ended-once",
|
|
32
|
+
"turn-ended-pairs",
|
|
33
|
+
"session-created-once",
|
|
34
|
+
// Requests
|
|
35
|
+
"permission-resolves-once",
|
|
36
|
+
"elicitation-resolves-once",
|
|
37
|
+
// Messages
|
|
38
|
+
"message-in-turn",
|
|
39
|
+
"bracket-pairing",
|
|
40
|
+
"output-index-increases",
|
|
41
|
+
"no-messages-after-turn-end",
|
|
42
|
+
"messages-closed-by-turn-end",
|
|
43
|
+
"model-assistant-only",
|
|
44
|
+
"user-messages-top-level",
|
|
45
|
+
"message-ids-unique",
|
|
46
|
+
// Parts
|
|
47
|
+
"part-self-describing",
|
|
48
|
+
"part-address-stable",
|
|
49
|
+
"late-parts-are-terminal-tools",
|
|
50
|
+
"delta-follows-snapshot",
|
|
51
|
+
// The echo and steers
|
|
52
|
+
"echo-first",
|
|
53
|
+
"echo-parts-done",
|
|
54
|
+
"undelivered-steers-not-echoed",
|
|
55
|
+
// Sequencing
|
|
56
|
+
"seq-monotonic",
|
|
57
|
+
"seq-live",
|
|
58
|
+
// Shape and time
|
|
59
|
+
"schema-valid",
|
|
60
|
+
"timestamp-valid",
|
|
61
|
+
"error-carries-info",
|
|
62
|
+
// Projection
|
|
63
|
+
"reducer-total",
|
|
64
|
+
"reducer-idempotent",
|
|
65
|
+
"projection-agrees",
|
|
66
|
+
];
|
|
67
|
+
export function verifyStreamContract(events, options = {}) {
|
|
68
|
+
const violations = [];
|
|
69
|
+
const report = (rule, index, message) => {
|
|
70
|
+
violations.push({ rule, index, message });
|
|
71
|
+
};
|
|
72
|
+
const malformed = checkShapes(events, report);
|
|
73
|
+
if (options.seqs)
|
|
74
|
+
checkSeqs(events, options.seqs, options.live ?? [], report);
|
|
75
|
+
checkOrdering(events, malformed, options.expectEcho ?? true, report);
|
|
76
|
+
const state = checkProjection(events, malformed, report);
|
|
77
|
+
if (state)
|
|
78
|
+
checkRedelivery(events, state, report);
|
|
79
|
+
return violations;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Every event decodes, and its timestamp is an epoch ms. Via the tolerant
|
|
83
|
+
* decoder, not the closed union: an unknown event type (or an unknown part
|
|
84
|
+
* inside a known event) is forward-compat, not a violation — only a KNOWN type
|
|
85
|
+
* with a malformed body is. Returns the indices that failed to decode: the
|
|
86
|
+
* later passes that re-touch event bodies skip them, since a malformed event's
|
|
87
|
+
* nested fields (message.part's `event.part.id`, `"raw" in part`) cannot be
|
|
88
|
+
* safely dereferenced a second time, and a TypeError there would discard every
|
|
89
|
+
* violation found, the schema-valid report for that index included.
|
|
90
|
+
*/
|
|
91
|
+
function checkShapes(events, report) {
|
|
92
|
+
const malformed = new Set();
|
|
93
|
+
events.forEach((event, i) => {
|
|
94
|
+
try {
|
|
95
|
+
decodeLifecycleEvent(event);
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
malformed.add(i);
|
|
99
|
+
report("schema-valid", i, `event ${i} (${String(event.type)}) fails schema: ${err instanceof Error ? err.message : String(err)}`);
|
|
100
|
+
}
|
|
101
|
+
const ts = event.timestamp;
|
|
102
|
+
if (typeof ts === "number" && (!Number.isSafeInteger(ts) || ts < 0 || Object.is(ts, -0))) {
|
|
103
|
+
report("timestamp-valid", i, `event ${i} timestamp ${ts} is not a non-negative safe integer`);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
return malformed;
|
|
107
|
+
}
|
|
108
|
+
/** Kept events' seqs increase; a live frame, kept by no one, carries the last kept seq. */
|
|
109
|
+
function checkSeqs(events, seqs, live, report) {
|
|
110
|
+
let kept = seqs[0];
|
|
111
|
+
for (let i = 1; i < seqs.length; i++) {
|
|
112
|
+
const next = seqs[i];
|
|
113
|
+
const event = events[i];
|
|
114
|
+
if (live[i] || (event && isLiveOnlyEvent(event))) {
|
|
115
|
+
if (next !== kept)
|
|
116
|
+
report("seq-live", i, `live frame at event ${i} carries seq ${next}, not the last kept ${kept}`);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (next <= kept) {
|
|
120
|
+
report("seq-monotonic", i, `seq ${next} at event ${i} does not increase past ${kept}`);
|
|
121
|
+
}
|
|
122
|
+
kept = next;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** Turns, messages, parts and requests: what may arrive when, and how often. */
|
|
126
|
+
function checkOrdering(events, malformed, expectEcho, report) {
|
|
127
|
+
const turns = new Map();
|
|
128
|
+
/** part.id -> its fixed wire address (message/indices are upsert-stable). */
|
|
129
|
+
const partAddress = new Map();
|
|
130
|
+
const messageRole = new Map();
|
|
131
|
+
/** Every message id started in the session: one id names one message. */
|
|
132
|
+
const startedMessages = new Set();
|
|
133
|
+
const pendingPermissions = new Map();
|
|
134
|
+
const pendingElicitations = new Map();
|
|
135
|
+
const resolvedPermissions = new Set();
|
|
136
|
+
const resolvedElicitations = new Set();
|
|
137
|
+
/**
|
|
138
|
+
* Scoped PER TURN, not per stream: `session.created` comes whenever it says
|
|
139
|
+
* something new (the session's first native id, a new one, a resume that did
|
|
140
|
+
* not continue), so a multi-turn session legally carries several, wherever
|
|
141
|
+
* the harness first reports its id in a turn. Two within a single turn is
|
|
142
|
+
* the violation (LLP 0002, "The session is announced when it changes").
|
|
143
|
+
*/
|
|
144
|
+
let sessionCreatedInTurn = 0;
|
|
145
|
+
events.forEach((event, i) => {
|
|
146
|
+
// Unknown types carry no contract obligations we can check — they are
|
|
147
|
+
// preserved, not interpreted — and a malformed event was reported by
|
|
148
|
+
// `checkShapes` (its nested body is not safe to read again).
|
|
149
|
+
if (isUnknownEvent(event) || malformed.has(i))
|
|
150
|
+
return;
|
|
151
|
+
switch (event.type) {
|
|
152
|
+
case "session.created":
|
|
153
|
+
sessionCreatedInTurn++;
|
|
154
|
+
if (sessionCreatedInTurn > 1) {
|
|
155
|
+
report("session-created-once", i, "session.created emitted twice within one turn");
|
|
156
|
+
}
|
|
157
|
+
return;
|
|
158
|
+
case "turn.started": {
|
|
159
|
+
sessionCreatedInTurn = 0;
|
|
160
|
+
if (turns.has(event.turnId)) {
|
|
161
|
+
report("turn-started-once", i, `turn.started repeated for ${event.turnId}`);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
turns.set(event.turnId, {
|
|
165
|
+
startedIndex: i,
|
|
166
|
+
firstMessageChecked: false,
|
|
167
|
+
openMessages: new Map(),
|
|
168
|
+
endedMessages: new Set(),
|
|
169
|
+
lastOutputIndex: -1,
|
|
170
|
+
});
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
case "message.started": {
|
|
174
|
+
const turn = turns.get(event.turnId);
|
|
175
|
+
if (!turn) {
|
|
176
|
+
report("message-in-turn", i, `message.started for unknown turn ${event.turnId}`);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (turn.endedIndex !== undefined) {
|
|
180
|
+
report("no-messages-after-turn-end", i, `message.started after turn.ended (${event.turnId})`);
|
|
181
|
+
}
|
|
182
|
+
if (!turn.firstMessageChecked) {
|
|
183
|
+
turn.firstMessageChecked = true;
|
|
184
|
+
if (expectEcho && (event.role !== "user" || event.outputIndex !== 0)) {
|
|
185
|
+
report("echo-first", i, `first message of turn ${event.turnId} is role=${event.role} outputIndex=${event.outputIndex}; expected the user echo at outputIndex 0`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (event.outputIndex <= turn.lastOutputIndex) {
|
|
189
|
+
report("output-index-increases", i, `outputIndex ${event.outputIndex} does not increase past ${turn.lastOutputIndex}`);
|
|
190
|
+
}
|
|
191
|
+
turn.lastOutputIndex = Math.max(turn.lastOutputIndex, event.outputIndex);
|
|
192
|
+
if (startedMessages.has(event.messageId)) {
|
|
193
|
+
report("message-ids-unique", i, `message ${event.messageId} started again in turn ${event.turnId}; message ids are unique in a session`);
|
|
194
|
+
}
|
|
195
|
+
startedMessages.add(event.messageId);
|
|
196
|
+
turn.openMessages.set(event.messageId, i);
|
|
197
|
+
messageRole.set(event.messageId, event.role);
|
|
198
|
+
// What the user said — the echo, a steer — is the conversation's own
|
|
199
|
+
// input, never a sub-agent's output.
|
|
200
|
+
if (event.role === "user" && event.parentToolCallId !== undefined) {
|
|
201
|
+
report("user-messages-top-level", i, `user message ${event.messageId} is nested under tool call ${event.parentToolCallId}`);
|
|
202
|
+
}
|
|
203
|
+
// `model` is assistant/model-message metadata; the user echo (and any
|
|
204
|
+
// user-role message) is user-authored, so a `model` field on it
|
|
205
|
+
// misattributes the configured model to the user's prompt. This catches
|
|
206
|
+
// leaks the producer gate prevents but a hand-built or replayed stream
|
|
207
|
+
// could still carry.
|
|
208
|
+
if (event.model !== undefined && event.role !== "assistant") {
|
|
209
|
+
report("model-assistant-only", i, `message.started ${event.messageId} carries model ${JSON.stringify(event.model)} on a ${event.role} message; model is assistant-only metadata`);
|
|
210
|
+
}
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
case "message.part": {
|
|
214
|
+
const known = partAddress.get(event.part.id);
|
|
215
|
+
if (known) {
|
|
216
|
+
if (known.messageId !== event.messageId || known.partIndex !== event.partIndex) {
|
|
217
|
+
report("part-address-stable", i, `part ${event.part.id} moved from ${known.messageId}#${known.partIndex} to ${event.messageId}#${event.partIndex} — upsert addresses are fixed at first emission`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
partAddress.set(event.part.id, {
|
|
222
|
+
messageId: event.messageId,
|
|
223
|
+
partIndex: event.partIndex,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
// Two distinct failures, two distinct messages: an unaddressable part
|
|
227
|
+
// and a part that disagrees with its event are different bugs, and a
|
|
228
|
+
// shared message sends the reader hunting the wrong one.
|
|
229
|
+
if (event.part.id === "") {
|
|
230
|
+
report("part-self-describing", i, "part carries an empty id — nothing to upsert by");
|
|
231
|
+
}
|
|
232
|
+
else if (event.part.messageId !== event.messageId) {
|
|
233
|
+
report("part-self-describing", i, `part ${event.part.id} carries messageId ${event.part.messageId} but the event says ${event.messageId}`);
|
|
234
|
+
}
|
|
235
|
+
// An unknown part type is preserved, never interpreted: the rules
|
|
236
|
+
// below read a part's KNOWN shape, so they simply don't apply to it.
|
|
237
|
+
const part = event.part;
|
|
238
|
+
if ("raw" in part)
|
|
239
|
+
return;
|
|
240
|
+
const turn = turns.get(event.turnId);
|
|
241
|
+
if (turn?.endedIndex !== undefined) {
|
|
242
|
+
const lateOk = part.type === "tool" &&
|
|
243
|
+
(part.state.status === "completed" ||
|
|
244
|
+
part.state.status === "failed" ||
|
|
245
|
+
part.state.status === "cancelled");
|
|
246
|
+
if (!lateOk) {
|
|
247
|
+
report("late-parts-are-terminal-tools", i, `non-terminal ${part.type} snapshot after turn.ended`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (expectEcho &&
|
|
251
|
+
messageRole.get(event.messageId) === "user" &&
|
|
252
|
+
(part.type === "text" || part.type === "reasoning") &&
|
|
253
|
+
part.state !== "done") {
|
|
254
|
+
report("echo-parts-done", i, "user echo part is not state done");
|
|
255
|
+
}
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
case "message.part.delta": {
|
|
259
|
+
if (!partAddress.has(event.partId)) {
|
|
260
|
+
report("delta-follows-snapshot", i, `delta for part ${event.partId} arrived before any message.part snapshot`);
|
|
261
|
+
}
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
case "message.ended": {
|
|
265
|
+
const turn = turns.get(event.turnId);
|
|
266
|
+
if (turn) {
|
|
267
|
+
if (!turn.openMessages.has(event.messageId) && !turn.endedMessages.has(event.messageId)) {
|
|
268
|
+
report("bracket-pairing", i, `message.ended for unknown message ${event.messageId}`);
|
|
269
|
+
}
|
|
270
|
+
turn.openMessages.delete(event.messageId);
|
|
271
|
+
turn.endedMessages.add(event.messageId);
|
|
272
|
+
}
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
case "turn.ended": {
|
|
276
|
+
const turn = turns.get(event.turnId);
|
|
277
|
+
if (!turn) {
|
|
278
|
+
report("turn-ended-pairs", i, `turn.ended for unknown turn ${event.turnId}`);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (turn.endedIndex !== undefined) {
|
|
282
|
+
report("turn-ended-once", i, `turn.ended repeated for ${event.turnId}`);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
turn.endedIndex = i;
|
|
286
|
+
if (turn.openMessages.size > 0) {
|
|
287
|
+
report("messages-closed-by-turn-end", i, `turn.ended with ${turn.openMessages.size} unclosed message(s)`);
|
|
288
|
+
}
|
|
289
|
+
if (event.stopReason === "error" && event.error === undefined) {
|
|
290
|
+
report("error-carries-info", i, "turn.ended stopReason=error without error info");
|
|
291
|
+
}
|
|
292
|
+
for (const steerId of event.undeliveredSteers ?? []) {
|
|
293
|
+
const echo = steerMessageId(steerId);
|
|
294
|
+
if (turn.openMessages.has(echo) || turn.endedMessages.has(echo)) {
|
|
295
|
+
report("undelivered-steers-not-echoed", i, `steer ${steerId} is listed undelivered but the turn echoed it`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
case "permission.requested":
|
|
301
|
+
pendingPermissions.set(event.requestId, i);
|
|
302
|
+
return;
|
|
303
|
+
case "permission.resolved":
|
|
304
|
+
if (resolvedPermissions.has(event.requestId)) {
|
|
305
|
+
report("permission-resolves-once", i, `permission ${event.requestId} resolved twice`);
|
|
306
|
+
}
|
|
307
|
+
resolvedPermissions.add(event.requestId);
|
|
308
|
+
pendingPermissions.delete(event.requestId);
|
|
309
|
+
return;
|
|
310
|
+
case "elicitation.requested":
|
|
311
|
+
pendingElicitations.set(event.requestId, i);
|
|
312
|
+
return;
|
|
313
|
+
case "elicitation.resolved":
|
|
314
|
+
if (resolvedElicitations.has(event.requestId)) {
|
|
315
|
+
report("elicitation-resolves-once", i, `elicitation ${event.requestId} resolved twice`);
|
|
316
|
+
}
|
|
317
|
+
resolvedElicitations.add(event.requestId);
|
|
318
|
+
pendingElicitations.delete(event.requestId);
|
|
319
|
+
return;
|
|
320
|
+
default:
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
// What the stream's end left open.
|
|
325
|
+
for (const [turnId, turn] of turns) {
|
|
326
|
+
if (turn.endedIndex === undefined) {
|
|
327
|
+
report("turn-ended-pairs", turn.startedIndex, `turn ${turnId} never ended`);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
for (const [requestId, index] of pendingPermissions) {
|
|
331
|
+
report("permission-resolves-once", index, `permission ${requestId} never resolved`);
|
|
332
|
+
}
|
|
333
|
+
for (const [requestId, index] of pendingElicitations) {
|
|
334
|
+
report("elicitation-resolves-once", index, `elicitation ${requestId} never resolved`);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* The reducer's final state agrees with the authoritative part snapshots.
|
|
339
|
+
* Returns that state, or undefined when the fold threw.
|
|
340
|
+
*/
|
|
341
|
+
function checkProjection(events, malformed, report) {
|
|
342
|
+
/** part.id -> its last full snapshot and where in the stream it landed. */
|
|
343
|
+
const lastSnapshot = new Map();
|
|
344
|
+
/** part.id -> index of the last text/reasoning delta (tool_input never folds into text). */
|
|
345
|
+
const lastTextDelta = new Map();
|
|
346
|
+
events.forEach((event, i) => {
|
|
347
|
+
if (isUnknownEvent(event) || malformed.has(i))
|
|
348
|
+
return;
|
|
349
|
+
if (event.type === "message.part")
|
|
350
|
+
lastSnapshot.set(event.part.id, { event, index: i });
|
|
351
|
+
else if (event.type === "message.part.delta" && event.delta.type !== "tool_input") {
|
|
352
|
+
lastTextDelta.set(event.partId, i);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
let state = emptyConversation();
|
|
356
|
+
try {
|
|
357
|
+
for (const event of events)
|
|
358
|
+
state = reduceConversation(state, event);
|
|
359
|
+
}
|
|
360
|
+
catch (err) {
|
|
361
|
+
report("reducer-total", -1, `reduceConversation threw: ${err instanceof Error ? err.message : String(err)}`);
|
|
362
|
+
return undefined;
|
|
363
|
+
}
|
|
364
|
+
// The fold closes in-flight tools on EVERY terminal turn.ended (not only
|
|
365
|
+
// cancels): an ended turn's dangling tool legitimately reduces to
|
|
366
|
+
// `cancelled` (the turn was cancelled) or `failed` (any other end) with no
|
|
367
|
+
// snapshot saying so.
|
|
368
|
+
const endedTurns = new Map(state.turns.filter((t) => t.status === "ended").map((t) => [t.turnId, t.stopReason]));
|
|
369
|
+
for (const message of conversationMessages(state)) {
|
|
370
|
+
for (const part of message.parts) {
|
|
371
|
+
if ("raw" in part)
|
|
372
|
+
continue;
|
|
373
|
+
const source = lastSnapshot.get(part.id);
|
|
374
|
+
const snapshot = source?.event.part;
|
|
375
|
+
if (!snapshot) {
|
|
376
|
+
report("projection-agrees", -1, `reduced part ${part.id} has no source snapshot`);
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if ("raw" in snapshot)
|
|
380
|
+
continue; // an unknown part: preserved, not interpreted
|
|
381
|
+
if ((part.type === "text" || part.type === "reasoning") &&
|
|
382
|
+
(snapshot.type === "text" || snapshot.type === "reasoning")) {
|
|
383
|
+
// Deltas that arrive AFTER the last snapshot legitimately extend it —
|
|
384
|
+
// that is the shipped fold ("appends later deltas onto the replacement
|
|
385
|
+
// snapshot"), and it is how EVERY cancelled turn ends: trailing deltas
|
|
386
|
+
// with no closing `content_block_stop` to snapshot them. Authority
|
|
387
|
+
// still bites: the snapshot must be a PREFIX of what the fold holds.
|
|
388
|
+
const extended = (lastTextDelta.get(part.id) ?? -1) > (source?.index ?? -1);
|
|
389
|
+
const agrees = extended ? part.text.startsWith(snapshot.text) : part.text === snapshot.text;
|
|
390
|
+
if (!agrees) {
|
|
391
|
+
report("projection-agrees", -1, extended
|
|
392
|
+
? `part ${part.id} reduced text (${part.text.length} chars) does not extend its last snapshot (${snapshot.text.length} chars) — later deltas may only append`
|
|
393
|
+
: `part ${part.id} reduced text (${part.text.length} chars) != last snapshot (${snapshot.text.length} chars) — snapshots must be authoritative`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (part.type === "tool" && snapshot.type === "tool") {
|
|
397
|
+
const closedByTurnEnd = endedTurns.has(message.turnId) &&
|
|
398
|
+
part.state.status ===
|
|
399
|
+
(endedTurns.get(message.turnId) === "cancelled" ? "cancelled" : "failed") &&
|
|
400
|
+
(snapshot.state.status === "pending" || snapshot.state.status === "in_progress");
|
|
401
|
+
if (part.state.status !== snapshot.state.status && !closedByTurnEnd) {
|
|
402
|
+
report("projection-agrees", -1, `tool ${part.id} reduced status ${part.state.status} != snapshot ${snapshot.state.status}`);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return state;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Re-delivery is idempotent. The wire redelivers: a replay overlap or a
|
|
411
|
+
* reconnect resync hands the same event to the fold twice. Folding a stream in
|
|
412
|
+
* which every event WITH history is duplicated must land on the same state as
|
|
413
|
+
* folding it once — otherwise a reconnect silently doubles a part, a token
|
|
414
|
+
* bill, or an answer. (Refolding the identical sequence cannot fail: the
|
|
415
|
+
* reducer is a pure function of its arguments.)
|
|
416
|
+
*/
|
|
417
|
+
function checkRedelivery(events, state, report) {
|
|
418
|
+
const withDuplicates = [];
|
|
419
|
+
for (const event of events) {
|
|
420
|
+
withDuplicates.push(event);
|
|
421
|
+
if (event.type === "message.part" ||
|
|
422
|
+
event.type === "turn.ended" ||
|
|
423
|
+
event.type === "permission.resolved" ||
|
|
424
|
+
event.type === "elicitation.resolved") {
|
|
425
|
+
withDuplicates.push(event);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
let redelivered = emptyConversation();
|
|
429
|
+
try {
|
|
430
|
+
for (const event of withDuplicates)
|
|
431
|
+
redelivered = reduceConversation(redelivered, event);
|
|
432
|
+
}
|
|
433
|
+
catch (err) {
|
|
434
|
+
report("reducer-total", -1, `reduceConversation threw on re-delivery: ${err instanceof Error ? err.message : String(err)}`);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
const partCounts = (s) => new Map(conversationMessages(s).map((m) => [m.messageId, m.parts.length]));
|
|
438
|
+
const outcomes = (s) => new Map([
|
|
439
|
+
...s.permissions.map((p) => [p.requestId, JSON.stringify(p.outcome)]),
|
|
440
|
+
...s.elicitations.map((e) => [e.requestId, JSON.stringify(e.outcome)]),
|
|
441
|
+
]);
|
|
442
|
+
const idempotent = (message) => report("reducer-idempotent", -1, message);
|
|
443
|
+
if (redelivered.timeline.length !== state.timeline.length) {
|
|
444
|
+
idempotent(`re-delivering upserts changed the timeline (${state.timeline.length} -> ${redelivered.timeline.length} entries)`);
|
|
445
|
+
}
|
|
446
|
+
const once = partCounts(state);
|
|
447
|
+
for (const [messageId, count] of partCounts(redelivered)) {
|
|
448
|
+
if (once.get(messageId) !== count) {
|
|
449
|
+
idempotent(`re-delivering upserts changed message ${messageId}'s part count (${once.get(messageId)} -> ${count})`);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
const resolvedOnce = outcomes(state);
|
|
453
|
+
for (const [requestId, outcome] of outcomes(redelivered)) {
|
|
454
|
+
if (resolvedOnce.get(requestId) !== outcome) {
|
|
455
|
+
idempotent(`re-delivering changed request ${requestId}'s outcome`);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
if (JSON.stringify(redelivered.totals) !== JSON.stringify(state.totals)) {
|
|
459
|
+
idempotent("re-delivering turn.ended double-counted the billing totals");
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
/** Render violations for terminal output; empty string when clean. */
|
|
463
|
+
export function formatViolations(violations) {
|
|
464
|
+
if (violations.length === 0)
|
|
465
|
+
return "";
|
|
466
|
+
return violations.map((v) => ` [${v.rule}] #${v.index}: ${v.message}`).join("\n");
|
|
467
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/** The values each open vocabulary knows, by its schema. */
|
|
3
|
+
const KNOWN = new WeakMap();
|
|
4
|
+
// @ref LLP 0002#open-vocabularies — unknown values pass, blanks never
|
|
5
|
+
/**
|
|
6
|
+
* An OPEN vocabulary. The schema accepts values this build does not
|
|
7
|
+
* know (a newer engine's `stopReason`, an adapter's `_`-prefixed extension),
|
|
8
|
+
* because rejecting them is exactly how a forward-compatible wire turns into a
|
|
9
|
+
* compatibility break — the failure ACP moved to v2 to escape. The cast keeps
|
|
10
|
+
* the TypeScript side as `"a" | "b" | (string & {})`, so the canonical members
|
|
11
|
+
* still autocomplete, while the runtime lets an unknown value through to be
|
|
12
|
+
* preserved. The schema remembers the values it knows (`knownValues`), which
|
|
13
|
+
* is how the casing test checks them.
|
|
14
|
+
*
|
|
15
|
+
* Blank is the one thing every open vocabulary rejects: `""` and `" "` are
|
|
16
|
+
* not values a consumer can switch on, render, or persist — they are a
|
|
17
|
+
* producer bug, and accepting them silently is how it reaches a UI.
|
|
18
|
+
*/
|
|
19
|
+
export function openVocabulary(known) {
|
|
20
|
+
const schema = z.string().min(1).regex(/\S/, "must not be blank");
|
|
21
|
+
KNOWN.set(schema, known);
|
|
22
|
+
return schema;
|
|
23
|
+
}
|
|
24
|
+
/** The values an open vocabulary's schema knows (none for any other schema). */
|
|
25
|
+
export function knownValues(schema) {
|
|
26
|
+
return KNOWN.get(schema) ?? [];
|
|
27
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { AuthCancelParamsSchema, AuthCancelResultSchema, AuthCompleteParamsSchema, AuthLoginParamsSchema, AuthLoginSchema, AuthLogoutParamsSchema, AuthStatusParamsSchema, AuthStatusResultSchema, AuthUpdatedSchema, HarnessAuthSchema, } from "./auth.js";
|
|
3
|
+
import { ClisParamsSchema, ClisResultSchema } from "./clis.js";
|
|
4
|
+
import { HISTORY_METHODS, HistoryCatalogSchema, HistoryCheckpointRequestSchema, HistoryCheckpointSchema, HistoryCommitSchema, HistoryForkSchema, HistoryHeadSchema, HistoryListSchema, HistoryReadSchema, HistorySliceSchema, HistoryVersionSchema, HistoryWaitSchema, SessionRefSchema, } from "./history.js";
|
|
5
|
+
import { ModelListSchema, decodeModelList } from "./models.js";
|
|
6
|
+
import { DurableSessionSchema, ElicitationRespondParamsSchema, ElicitationRespondResultSchema, EventsReplayParamsSchema, EventsReplayResultSchema, InitializeParamsSchema, InitializeResultSchema, ModelsListParamsSchema, PermissionRespondParamsSchema, PermissionRespondResultSchema, RecoveryReasonSchema, SessionCloseParamsSchema, SessionCloseResultSchema, SessionConfirmStoppedParamsSchema, SessionConfirmStoppedResultSchema, SessionGetParamsSchema, SessionReleaseParamsSchema, SessionReleaseResultSchema, SessionSubscribeAllParamsSchema, SessionSubscribeAllResultSchema, SessionSubscribeParamsSchema, SessionSubscribeResultSchema, SessionUnsubscribeAllParamsSchema, SessionUnsubscribeParamsSchema, SessionUnsubscribeResultSchema, ShutdownResultSchema, TurnCancelParamsSchema, TurnCancelResultSchema, TurnGetParamsSchema, TurnReceiptSchema, TurnStartParamsSchema, TurnStartResultSchema, TurnSteerParamsSchema, TurnSteerResultSchema, WIRE_ERROR_CODES, WIRE_METHODS, decodeEventsReplayResult, } from "./wire.js";
|
|
7
|
+
/** `shutdown` takes nothing. */
|
|
8
|
+
const ShutdownParamsSchema = z.object({});
|
|
9
|
+
function method(params, result, decode) {
|
|
10
|
+
return { params, result, decode: decode ?? ((raw) => result.parse(raw)) };
|
|
11
|
+
}
|
|
12
|
+
const API = {
|
|
13
|
+
[WIRE_METHODS.initialize]: method(InitializeParamsSchema, InitializeResultSchema),
|
|
14
|
+
[WIRE_METHODS.turnStart]: method(TurnStartParamsSchema, TurnStartResultSchema),
|
|
15
|
+
[WIRE_METHODS.turnCancel]: method(TurnCancelParamsSchema, TurnCancelResultSchema),
|
|
16
|
+
[WIRE_METHODS.turnSteer]: method(TurnSteerParamsSchema, TurnSteerResultSchema),
|
|
17
|
+
[WIRE_METHODS.sessionClose]: method(SessionCloseParamsSchema, SessionCloseResultSchema),
|
|
18
|
+
[WIRE_METHODS.permissionRespond]: method(PermissionRespondParamsSchema, PermissionRespondResultSchema),
|
|
19
|
+
[WIRE_METHODS.elicitationRespond]: method(ElicitationRespondParamsSchema, ElicitationRespondResultSchema),
|
|
20
|
+
[WIRE_METHODS.eventsReplay]: method(EventsReplayParamsSchema, EventsReplayResultSchema, decodeEventsReplayResult),
|
|
21
|
+
[WIRE_METHODS.modelsList]: method(ModelsListParamsSchema, ModelListSchema, decodeModelList),
|
|
22
|
+
[WIRE_METHODS.clisList]: method(ClisParamsSchema, ClisResultSchema),
|
|
23
|
+
[WIRE_METHODS.clisPrepare]: method(ClisParamsSchema, ClisResultSchema),
|
|
24
|
+
[WIRE_METHODS.authStatus]: method(AuthStatusParamsSchema, AuthStatusResultSchema),
|
|
25
|
+
[WIRE_METHODS.authLogin]: method(AuthLoginParamsSchema, AuthLoginSchema),
|
|
26
|
+
[WIRE_METHODS.authComplete]: method(AuthCompleteParamsSchema, AuthUpdatedSchema),
|
|
27
|
+
[WIRE_METHODS.authCancel]: method(AuthCancelParamsSchema, AuthCancelResultSchema),
|
|
28
|
+
[WIRE_METHODS.authLogout]: method(AuthLogoutParamsSchema, HarnessAuthSchema),
|
|
29
|
+
[WIRE_METHODS.turnGet]: method(TurnGetParamsSchema, TurnReceiptSchema),
|
|
30
|
+
[WIRE_METHODS.sessionGet]: method(SessionGetParamsSchema, DurableSessionSchema),
|
|
31
|
+
[WIRE_METHODS.sessionConfirmStopped]: method(SessionConfirmStoppedParamsSchema, SessionConfirmStoppedResultSchema),
|
|
32
|
+
[WIRE_METHODS.sessionRelease]: method(SessionReleaseParamsSchema, SessionReleaseResultSchema),
|
|
33
|
+
[WIRE_METHODS.sessionSubscribe]: method(SessionSubscribeParamsSchema, SessionSubscribeResultSchema),
|
|
34
|
+
[WIRE_METHODS.sessionUnsubscribe]: method(SessionUnsubscribeParamsSchema, SessionUnsubscribeResultSchema),
|
|
35
|
+
[WIRE_METHODS.sessionSubscribeAll]: method(SessionSubscribeAllParamsSchema, SessionSubscribeAllResultSchema),
|
|
36
|
+
[WIRE_METHODS.sessionUnsubscribeAll]: method(SessionUnsubscribeAllParamsSchema, SessionUnsubscribeResultSchema),
|
|
37
|
+
[WIRE_METHODS.shutdown]: method(ShutdownParamsSchema, ShutdownResultSchema),
|
|
38
|
+
[HISTORY_METHODS.list]: method(HistoryListSchema, HistoryCatalogSchema),
|
|
39
|
+
[HISTORY_METHODS.head]: method(SessionRefSchema, HistoryHeadSchema),
|
|
40
|
+
[HISTORY_METHODS.read]: method(HistoryReadSchema, HistorySliceSchema),
|
|
41
|
+
[HISTORY_METHODS.import]: method(HistorySliceSchema, HistoryVersionSchema),
|
|
42
|
+
[HISTORY_METHODS.fork]: method(HistoryForkSchema, HistoryHeadSchema),
|
|
43
|
+
[HISTORY_METHODS.commit]: method(HistoryCommitSchema, HistoryVersionSchema),
|
|
44
|
+
[HISTORY_METHODS.wait]: method(HistoryWaitSchema, HistoryHeadSchema),
|
|
45
|
+
[HISTORY_METHODS.checkpoint]: method(HistoryCheckpointRequestSchema, HistoryCheckpointSchema.nullable()),
|
|
46
|
+
};
|
|
47
|
+
// @ref LLP 0001#d9--the-wire-is-quick-ack-json-rpc-requests-flow-one-way — one table, read by server and client
|
|
48
|
+
/**
|
|
49
|
+
* The wire's requests, each with its params' schema, its result's, and the
|
|
50
|
+
* decoder a client reads the answer with. The server parses a request's
|
|
51
|
+
* params with it once, and a client's request is typed by its method: a
|
|
52
|
+
* method's params and its answer are paired here and nowhere else.
|
|
53
|
+
*/
|
|
54
|
+
export const WIRE_API = API;
|
|
55
|
+
/** Whether `method` is one of the wire's requests. */
|
|
56
|
+
export function isWireRequestMethod(method) {
|
|
57
|
+
return Object.hasOwn(WIRE_API, method);
|
|
58
|
+
}
|
|
59
|
+
const REFUSAL_DATA = {
|
|
60
|
+
/** The params that failed their schema, each with why. */
|
|
61
|
+
invalidParams: z.array(z.object({ path: z.string(), message: z.string() })),
|
|
62
|
+
harnessNotAvailable: z.object({ available: z.array(z.string()) }),
|
|
63
|
+
protocolVersionMismatch: z.object({ supported: z.array(z.number().int()) }),
|
|
64
|
+
/** The turn the session runs. */
|
|
65
|
+
turnActive: z.object({ turnId: z.string() }),
|
|
66
|
+
/** The turn whose id, or the steer whose id, was reused with other input. */
|
|
67
|
+
turnConflict: z.object({ turnId: z.string(), steerId: z.string().optional() }),
|
|
68
|
+
/** Confirm the agent stopped (`session/confirmStopped`) at this head. */
|
|
69
|
+
recoveryRequired: z.object({
|
|
70
|
+
headSeq: z.number().int().nonnegative(),
|
|
71
|
+
reason: RecoveryReasonSchema,
|
|
72
|
+
}),
|
|
73
|
+
/** The conversation to name as `config.resumeSessionId`. */
|
|
74
|
+
resumeRequired: z.object({ nativeSessionId: z.string() }),
|
|
75
|
+
/** The session's head now, when a confirmation named another. */
|
|
76
|
+
historyConflict: z.object({ headSeq: z.number().int().nonnegative() }),
|
|
77
|
+
/** The sessions a connection may watch. */
|
|
78
|
+
subscriptionLimit: z.object({ limit: z.number().int().nonnegative() }),
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* What a refusal carries in `data`, by its name: what the caller needs to act
|
|
82
|
+
* on it. A refusal not named here carries none.
|
|
83
|
+
*/
|
|
84
|
+
export const WIRE_REFUSAL_DATA = REFUSAL_DATA;
|
|
85
|
+
/**
|
|
86
|
+
* A refusal's data, read by its name: undefined unless `error` is that
|
|
87
|
+
* refusal (`code`, a JSON-RPC error code) and its data is well formed.
|
|
88
|
+
*/
|
|
89
|
+
export function readRefusalData(error, name) {
|
|
90
|
+
if (error.code !== WIRE_ERROR_CODES[name])
|
|
91
|
+
return undefined;
|
|
92
|
+
const parsed = WIRE_REFUSAL_DATA[name].safeParse(error.data);
|
|
93
|
+
return parsed.success ? parsed.data : undefined;
|
|
94
|
+
}
|