@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,338 @@
|
|
|
1
|
+
import { EventSinkError, callbackSink } from "../core/index.js";
|
|
2
|
+
import { WIRE_API, WIRE_ERROR_CODES, WIRE_METHODS, WireRefusal, decodeWireMessage, encodeErrorResponse, encodeResponse, errorInfo, generateUUIDv7, isLiveOnlyEvent, isWireRequestMethod, } from "../protocol/index.js";
|
|
3
|
+
import { historyRoutes } from "./history.js";
|
|
4
|
+
import { OpenParts } from "./open-parts.js";
|
|
5
|
+
import { durableRoutes, executionRoutes, } from "./routes.js";
|
|
6
|
+
import { SessionGate } from "./session-gate.js";
|
|
7
|
+
import { JournalSessionStore, MemorySessionStore } from "./session-store.js";
|
|
8
|
+
import { DEFAULT_MAX_BACKLOG_BYTES, DEFAULT_MAX_SUBSCRIPTIONS, Subscriptions, } from "./subscriptions.js";
|
|
9
|
+
import { parseParams, wireErrorOf } from "./wire-error.js";
|
|
10
|
+
/**
|
|
11
|
+
* The wire binding: serves the engine over JSON-RPC 2.0 on any
|
|
12
|
+
* number of attached transports. Requests flow client -> server only; every
|
|
13
|
+
* lifecycle event is stamped with a per-session monotonic `seq`, buffered for
|
|
14
|
+
* `events/replay`, and sent as an `event` notification to the connections
|
|
15
|
+
* subscribed to its session (`session/subscribe`; `turn/start` subscribes its
|
|
16
|
+
* caller) — token deltas with the seq of the last kept event, never buffered.
|
|
17
|
+
* `turn/start` is quick-ack: the response only accepts the turn; completion is
|
|
18
|
+
* the `turn.ended` event. One active turn per session.
|
|
19
|
+
*/
|
|
20
|
+
export class AgentServer {
|
|
21
|
+
runtime;
|
|
22
|
+
options;
|
|
23
|
+
/** The attached connections, and the sessions each receives. */
|
|
24
|
+
subscriptions;
|
|
25
|
+
/** The ONE persistence decision: everything else asks the store. */
|
|
26
|
+
store;
|
|
27
|
+
/** Each session's open parts and latest turn start, from the frames this server delivers. */
|
|
28
|
+
openParts = new OpenParts();
|
|
29
|
+
gate = new SessionGate();
|
|
30
|
+
/** Every method this server answers: being durable, or serving history, is having their routes. */
|
|
31
|
+
routes;
|
|
32
|
+
observers = new Set();
|
|
33
|
+
shuttingDown = false;
|
|
34
|
+
shutdownPromise;
|
|
35
|
+
turnsDrained;
|
|
36
|
+
/** Stops writing the runtime's native process records into the journal. */
|
|
37
|
+
unrecord;
|
|
38
|
+
unprogress;
|
|
39
|
+
unauth;
|
|
40
|
+
// @ref LLP 0007#records-and-the-restart-sweep — sweep before claiming, so the claim fences only what is unverified
|
|
41
|
+
/**
|
|
42
|
+
* A server whose restart sweep ran before it took the journal: each native
|
|
43
|
+
* process a previous owner recorded is verified (boot, start time,
|
|
44
|
+
* executable) and, while it is still that process, stopped with its tool
|
|
45
|
+
* sessions. The claim then fences only the sessions whose agent could not
|
|
46
|
+
* be confirmed stopped. Without a journal it is the constructor.
|
|
47
|
+
*/
|
|
48
|
+
static async open(runtime, options = {}) {
|
|
49
|
+
return new AgentServer(runtime, options, await sweep(runtime, options.journal));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* `swept`: what the restart sweep found of the journal's records
|
|
53
|
+
* (`AgentServer.open` passes it). Without it, every session a restart cut
|
|
54
|
+
* off is fenced until the host confirms its agent stopped.
|
|
55
|
+
*/
|
|
56
|
+
constructor(runtime, options = {}, swept) {
|
|
57
|
+
this.runtime = runtime;
|
|
58
|
+
this.options = options;
|
|
59
|
+
const journal = options.journal &&
|
|
60
|
+
new JournalSessionStore(options.journal, (error) => this.failStop(error), swept);
|
|
61
|
+
this.store = journal ?? new MemorySessionStore(options.bufferSize);
|
|
62
|
+
this.subscriptions = new Subscriptions({
|
|
63
|
+
maxSubscriptions: options.maxSubscriptions ?? DEFAULT_MAX_SUBSCRIPTIONS,
|
|
64
|
+
maxBacklogBytes: this.maxBacklogBytes,
|
|
65
|
+
});
|
|
66
|
+
// Each native process a session's turn starts is in the journal before it gets work.
|
|
67
|
+
this.unrecord = journal
|
|
68
|
+
? runtime.onNativeProcess((event) => journal.recordNative(event))
|
|
69
|
+
: () => { };
|
|
70
|
+
// Every connection hears a CLI being installed: whoever caused it,
|
|
71
|
+
// every turn of that harness waits on it.
|
|
72
|
+
this.unprogress = runtime.onCliProgress((progress) => this.subscriptions.notifyAll(WIRE_METHODS.clisProgress, progress));
|
|
73
|
+
// Every connection hears a harness's login change and a login's end:
|
|
74
|
+
// whoever logged in, every turn of that harness runs with it.
|
|
75
|
+
this.unauth = runtime.onAuthUpdated((update) => this.subscriptions.notifyAll(WIRE_METHODS.authUpdated, update));
|
|
76
|
+
const host = {
|
|
77
|
+
runtime,
|
|
78
|
+
store: this.store,
|
|
79
|
+
openParts: this.openParts,
|
|
80
|
+
gate: this.gate,
|
|
81
|
+
subscriptions: this.subscriptions,
|
|
82
|
+
identity: {
|
|
83
|
+
server: options.info ?? { name: "agent-server" },
|
|
84
|
+
// One id per server PROCESS. Within a process, session logs never
|
|
85
|
+
// restart (the store keeps seq monotonic across close/reopen of an
|
|
86
|
+
// id), so a client that sees this id change across a reconnect knows
|
|
87
|
+
// — as a stated fact, not an inference — that every log it tracked
|
|
88
|
+
// belongs to a dead process. Unless `historyId` says they outlive it.
|
|
89
|
+
instanceId: generateUUIDv7(),
|
|
90
|
+
...(options.journal && { historyId: options.journal.sourceId }),
|
|
91
|
+
},
|
|
92
|
+
accepting: () => !this.shuttingDown,
|
|
93
|
+
executeTurn: (request) => void this.executeTurn(request),
|
|
94
|
+
broadcast: (envelope) => this.broadcast(envelope),
|
|
95
|
+
drainTurns: () => this.drainTurns(),
|
|
96
|
+
shutdown: () => this.shutdown(),
|
|
97
|
+
};
|
|
98
|
+
this.routes = {
|
|
99
|
+
...executionRoutes(host),
|
|
100
|
+
...(journal && durableRoutes(journal, host)),
|
|
101
|
+
...(options.history && historyRoutes(options.history)),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* How far behind a connection may fall before it is sent `session/overflow`
|
|
106
|
+
* (`AgentServerOptions.maxBacklogBytes`): `listen` keeps its socket's own
|
|
107
|
+
* limit above it, so the notice is never the frame the socket drops.
|
|
108
|
+
*/
|
|
109
|
+
get maxBacklogBytes() {
|
|
110
|
+
return this.options.maxBacklogBytes ?? DEFAULT_MAX_BACKLOG_BYTES;
|
|
111
|
+
}
|
|
112
|
+
/** Serve the wire on a transport; returns a detach function, which drops its subscriptions. */
|
|
113
|
+
attach(transport) {
|
|
114
|
+
this.subscriptions.attach(transport);
|
|
115
|
+
const offLine = transport.onLine((line) => void this.handleLine(transport, line));
|
|
116
|
+
const detach = () => {
|
|
117
|
+
offLine();
|
|
118
|
+
this.subscriptions.detach(transport);
|
|
119
|
+
};
|
|
120
|
+
transport.onClose(detach);
|
|
121
|
+
return detach;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Observe every broadcast envelope in-process; returns an unsubscribe.
|
|
125
|
+
*
|
|
126
|
+
* Every session's envelopes, subscribed or not — the same sequenced
|
|
127
|
+
* envelopes subscribed transports receive, already appended to the session
|
|
128
|
+
* log (a token delta, which no log keeps, carries the last kept seq) — a
|
|
129
|
+
* host that embeds the server (to keep its own session bookkeeping, mirror
|
|
130
|
+
* events into a DB, trigger follow-up work) gets them as decoded objects
|
|
131
|
+
* instead of attaching a fake transport and parsing its own wire back out
|
|
132
|
+
* of NDJSON.
|
|
133
|
+
*
|
|
134
|
+
* Synchronous and isolated: an observer that throws is swallowed, exactly
|
|
135
|
+
* like a faulty transport or a failing sink — an event is a fact that
|
|
136
|
+
* already happened, and a consumer's bug must not unwind the broadcast for
|
|
137
|
+
* everyone else. Do the slow part yourself (queue it, don't block here).
|
|
138
|
+
*/
|
|
139
|
+
onEvent(observer) {
|
|
140
|
+
this.observers.add(observer);
|
|
141
|
+
return () => this.observers.delete(observer);
|
|
142
|
+
}
|
|
143
|
+
/** Cancel all turns, terminate harnesses, and close every transport. Every
|
|
144
|
+
* caller waits for the same drain. */
|
|
145
|
+
shutdown() {
|
|
146
|
+
this.shutdownPromise ??= this.drain();
|
|
147
|
+
return this.shutdownPromise;
|
|
148
|
+
}
|
|
149
|
+
/** Cancel all turns and terminate harnesses: their turn.ended are broadcast. */
|
|
150
|
+
drainTurns() {
|
|
151
|
+
this.shuttingDown = true;
|
|
152
|
+
this.turnsDrained ??= this.runtime.shutdown();
|
|
153
|
+
return this.turnsDrained;
|
|
154
|
+
}
|
|
155
|
+
async drain() {
|
|
156
|
+
// Drain first so cancelled turns still broadcast their turn.ended.
|
|
157
|
+
// The drain (and each transport's close) is a library extension point
|
|
158
|
+
// that may throw; the finally still closes every transport and runs the
|
|
159
|
+
// host hook even then — every other external call in this file is
|
|
160
|
+
// defended the same way (broadcast/observers/sink/handleLine).
|
|
161
|
+
try {
|
|
162
|
+
await this.drainTurns();
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
for (const transport of this.subscriptions.transports()) {
|
|
166
|
+
try {
|
|
167
|
+
transport.close();
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
// One faulty transport's close must not skip the others or the
|
|
171
|
+
// host's shutdown hook — same isolation broadcast uses for send.
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
this.unrecord();
|
|
175
|
+
this.unprogress();
|
|
176
|
+
this.unauth();
|
|
177
|
+
this.store.relinquish();
|
|
178
|
+
this.options.onShutdown?.();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/** The journal failed to write: stop, delivering nothing it did not commit. */
|
|
182
|
+
failStop(error) {
|
|
183
|
+
try {
|
|
184
|
+
this.options.onJournalError?.(error);
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
// The host's hook must not keep the server running.
|
|
188
|
+
}
|
|
189
|
+
void this.shutdown().catch(() => { });
|
|
190
|
+
}
|
|
191
|
+
async handleLine(transport, line) {
|
|
192
|
+
if (!line.trim())
|
|
193
|
+
return;
|
|
194
|
+
const msg = decodeWireMessage(line);
|
|
195
|
+
if (msg.kind === "invalid") {
|
|
196
|
+
const code = msg.parseError ? WIRE_ERROR_CODES.parseError : WIRE_ERROR_CODES.invalidRequest;
|
|
197
|
+
transport.send(encodeErrorResponse(null, code, msg.error));
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
// Clients only send requests on this wire; stray notifications/responses
|
|
201
|
+
// are tolerated and dropped.
|
|
202
|
+
if (msg.kind !== "request")
|
|
203
|
+
return;
|
|
204
|
+
try {
|
|
205
|
+
await this.handleRequest(transport, msg.id, msg.method, msg.params);
|
|
206
|
+
}
|
|
207
|
+
catch (err) {
|
|
208
|
+
const { code, message, data } = wireErrorOf(err);
|
|
209
|
+
try {
|
|
210
|
+
transport.send(encodeErrorResponse(msg.id, code, message, data));
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
// The requester is gone; nothing else is owed this response.
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
async handleRequest(transport, id, method, params) {
|
|
218
|
+
if (!isWireRequestMethod(method))
|
|
219
|
+
throw new WireRefusal("methodNotFound", `unknown method: ${method}`);
|
|
220
|
+
const after = [];
|
|
221
|
+
const afterResponse = (then) => {
|
|
222
|
+
after.push(then);
|
|
223
|
+
};
|
|
224
|
+
try {
|
|
225
|
+
const answer = this.answer(method, params, { transport, afterResponse });
|
|
226
|
+
// @ref LLP 0008#ordering-joining-without-a-gap-or-a-duplicate — a synchronous route is answered in the same breath
|
|
227
|
+
// No await for a route that answered synchronously: a frame sent
|
|
228
|
+
// between its registration and its answer would precede the point
|
|
229
|
+
// the answer marks.
|
|
230
|
+
transport.send(encodeResponse(id, answer instanceof Promise ? await answer : answer));
|
|
231
|
+
}
|
|
232
|
+
finally {
|
|
233
|
+
for (const then of after)
|
|
234
|
+
then();
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/** A request's answer from its route, its params parsed by `WIRE_API` unless the route reads its own. */
|
|
238
|
+
answer(method, params, context) {
|
|
239
|
+
const route = this.routes[method];
|
|
240
|
+
// Unknown here, or durable-only on a volatile server.
|
|
241
|
+
if (!route)
|
|
242
|
+
throw new WireRefusal("methodNotFound", `unknown method: ${method}`);
|
|
243
|
+
if ("unparsed" in route)
|
|
244
|
+
return route.unparsed(params, context);
|
|
245
|
+
return route(parseParams(WIRE_API[method].params, params ?? {}), context);
|
|
246
|
+
}
|
|
247
|
+
async executeTurn(request) {
|
|
248
|
+
const { sessionId, turnId } = request;
|
|
249
|
+
/** The runtime emitted the turn's `turn.started`. */
|
|
250
|
+
let started = false;
|
|
251
|
+
const sink = callbackSink((event) => {
|
|
252
|
+
const envelope = isLiveOnlyEvent(event)
|
|
253
|
+
? this.store.live(sessionId, event)
|
|
254
|
+
: this.store.append(sessionId, event);
|
|
255
|
+
if (event.type === "turn.started")
|
|
256
|
+
started = true;
|
|
257
|
+
// Once turn.ended is on the wire the session is idle — clear the busy
|
|
258
|
+
// mark here (not when run() settles) so clients acting on the event
|
|
259
|
+
// never race the bookkeeping.
|
|
260
|
+
if (event.type === "turn.ended")
|
|
261
|
+
this.gate.settle(sessionId, turnId);
|
|
262
|
+
this.broadcast(envelope);
|
|
263
|
+
});
|
|
264
|
+
try {
|
|
265
|
+
await this.runtime.run(request, sink);
|
|
266
|
+
}
|
|
267
|
+
catch (err) {
|
|
268
|
+
// The store could not commit: deliver nothing it did not record, not
|
|
269
|
+
// even a turn.ended. Its fault already stops the server.
|
|
270
|
+
if (err instanceof EventSinkError)
|
|
271
|
+
return;
|
|
272
|
+
// @ref LLP 0002#ordering-and-delivery — no turn.ended without its turn.started
|
|
273
|
+
// runtime.run reports errors via events; a throw means it died before
|
|
274
|
+
// emitting turn.ended — synthesize one so clients are never left
|
|
275
|
+
// hanging. One it refused before starting (it was shutting down, the
|
|
276
|
+
// harness could not be set up) emitted nothing: its start goes first.
|
|
277
|
+
const timestamp = Date.now();
|
|
278
|
+
const synthesized = [];
|
|
279
|
+
if (!started)
|
|
280
|
+
synthesized.push({ type: "turn.started", sessionId, turnId, timestamp });
|
|
281
|
+
synthesized.push({
|
|
282
|
+
type: "turn.ended",
|
|
283
|
+
sessionId,
|
|
284
|
+
turnId,
|
|
285
|
+
stopReason: "error",
|
|
286
|
+
error: errorInfo(err),
|
|
287
|
+
timestamp,
|
|
288
|
+
});
|
|
289
|
+
try {
|
|
290
|
+
for (const event of synthesized)
|
|
291
|
+
this.broadcast(this.store.append(sessionId, event));
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
// A store that cannot commit it has failed, and stops the server.
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
finally {
|
|
298
|
+
this.gate.settle(sessionId, turnId);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
broadcast(envelope) {
|
|
302
|
+
// @ref LLP 0008#open-parts-in-memory — kept as delivered, in the breath that delivers it
|
|
303
|
+
// What the store returned, as it stored it, before any connection hears it.
|
|
304
|
+
this.openParts.delivered(envelope);
|
|
305
|
+
// In-process observers first: they see the same envelope, in the same
|
|
306
|
+
// order, whether or not any transport is attached or subscribed.
|
|
307
|
+
for (const observer of [...this.observers]) {
|
|
308
|
+
try {
|
|
309
|
+
observer(envelope);
|
|
310
|
+
}
|
|
311
|
+
catch {
|
|
312
|
+
// An observer's failure is its own; the event is already logged.
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
// A transport that fails a send heals via replay; `session.ended` retires
|
|
316
|
+
// the log in the same breath, so one that missed it learns the session
|
|
317
|
+
// is over from `unknownSession` instead.
|
|
318
|
+
this.subscriptions.deliver(envelope);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Verify and stop what the journal's records name, before anything claims it:
|
|
323
|
+
* record ids by what became of them. Undefined without a journal.
|
|
324
|
+
*/
|
|
325
|
+
async function sweep(runtime, journal) {
|
|
326
|
+
if (!journal)
|
|
327
|
+
return undefined;
|
|
328
|
+
const records = journal.nativeProcesses();
|
|
329
|
+
const stopped = new Set();
|
|
330
|
+
const notStopped = new Set();
|
|
331
|
+
for (const [record, outcome] of await runtime.stopOrphans(records)) {
|
|
332
|
+
if (outcome === "gone" || outcome === "stopped")
|
|
333
|
+
stopped.add(record.id);
|
|
334
|
+
else if (outcome === "not_stopped")
|
|
335
|
+
notStopped.add(record.id);
|
|
336
|
+
}
|
|
337
|
+
return { stopped, notStopped, recorded: runtime.recordedHarnesses() };
|
|
338
|
+
}
|