@neta-art/cohub-cli 10.24.0 → 10.25.1
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/bin/cohub.js +4 -1
- package/dist/board-gpu/page.js +1566 -71230
- package/dist/bootstrap.js +67 -27
- package/dist/build.json +1 -1
- package/dist/{board-kernel/FilterPipe.js → chunks/Geometry.js} +10447 -10469
- package/dist/chunks/about.js +102 -0
- package/dist/chunks/app-realtime.js +2216 -0
- package/dist/chunks/app-realtime2.js +2 -0
- package/dist/chunks/app-ref.js +88 -0
- package/dist/chunks/apps.js +1423 -0
- package/dist/chunks/auth.js +175 -0
- package/dist/chunks/avatar.js +146 -0
- package/dist/chunks/binary-pins.js +5 -0
- package/dist/chunks/board-export-resources.js +232 -0
- package/dist/chunks/board-export.js +874 -0
- package/dist/chunks/board-export2.js +2 -0
- package/dist/{board-kernel.js → chunks/board-kernel.js} +18032 -28512
- package/dist/chunks/board-kernel2.js +4 -0
- package/dist/chunks/board-profile.js +69 -0
- package/dist/chunks/board-simulate.js +315 -0
- package/dist/chunks/board-time.js +34 -0
- package/dist/chunks/boards.js +1943 -0
- package/dist/{board-kernel → chunks}/browserAll.js +9 -8
- package/dist/chunks/channels.js +64 -0
- package/dist/chunks/client.js +12606 -0
- package/dist/chunks/command-input.js +17 -0
- package/dist/chunks/commander.js +2962 -0
- package/dist/chunks/cron-jobs.js +163 -0
- package/dist/chunks/daemon.js +545 -0
- package/dist/chunks/daemon2.js +2 -0
- package/dist/chunks/desktop.js +172 -0
- package/dist/chunks/endpoint-options.js +26 -0
- package/dist/chunks/env-proxy.js +35 -0
- package/dist/chunks/environment.js +154 -0
- package/dist/chunks/feedback.js +120 -0
- package/dist/chunks/generations.js +379 -0
- package/dist/chunks/host.js +9 -0
- package/dist/chunks/http-put.js +131 -0
- package/dist/chunks/id.js +2416 -0
- package/dist/{board-kernel → chunks}/init.js +1 -1
- package/dist/chunks/install.js +11959 -0
- package/dist/chunks/launch.js +374 -0
- package/dist/chunks/local-media.js +47 -0
- package/dist/chunks/me.js +134 -0
- package/dist/chunks/media.js +315 -0
- package/dist/chunks/models.js +242 -0
- package/dist/chunks/operands.js +46 -0
- package/dist/chunks/package-paths.js +13 -0
- package/dist/chunks/policy.js +221 -0
- package/dist/chunks/preferences.js +29 -0
- package/dist/chunks/profile.js +64 -0
- package/dist/chunks/prompts.js +41 -0
- package/dist/chunks/public.js +2 -0
- package/dist/chunks/public2.js +244 -0
- package/dist/chunks/references.js +197 -0
- package/dist/chunks/referrals.js +90 -0
- package/dist/chunks/rolldown-runtime.js +26 -0
- package/dist/chunks/rooms-connect.js +2 -0
- package/dist/chunks/rooms-connect2.js +151 -0
- package/dist/chunks/rooms-rtc.js +311 -0
- package/dist/chunks/rooms.js +211 -0
- package/dist/chunks/run.js +220 -0
- package/dist/chunks/runtime.js +638 -0
- package/dist/chunks/runtime2.js +286 -0
- package/dist/chunks/runtimes.js +48 -0
- package/dist/chunks/safe-remote-image.js +198 -0
- package/dist/chunks/schemas.js +6017 -0
- package/dist/chunks/search.js +113 -0
- package/dist/chunks/self-update.js +238 -0
- package/dist/chunks/serve.js +1246 -0
- package/dist/chunks/session-status.js +24 -0
- package/dist/chunks/skills.js +35 -0
- package/dist/chunks/spaces.js +5080 -0
- package/dist/chunks/src.js +405 -0
- package/dist/chunks/supervisor.js +2513 -0
- package/dist/chunks/supervisor2.js +2 -0
- package/dist/chunks/tasks.js +82 -0
- package/dist/chunks/themes.js +2898 -0
- package/dist/chunks/version.js +2 -0
- package/dist/chunks/version2.js +22 -0
- package/dist/chunks/version3.js +18 -0
- package/dist/chunks/web.js +7 -0
- package/dist/{board-kernel → chunks}/webworkerAll.js +1 -1
- package/dist/runtime/native/pi-extension.js +174 -166
- package/dist/runtime/worker.js +27 -16
- package/package.json +8 -9
- package/dist/about.d.ts +0 -2
- package/dist/about.js +0 -71
- package/dist/app-download.d.ts +0 -11
- package/dist/app-download.js +0 -252
- package/dist/app-ref.d.ts +0 -5
- package/dist/app-ref.js +0 -8
- package/dist/app-target.d.ts +0 -26
- package/dist/app-target.js +0 -55
- package/dist/auth.d.ts +0 -45
- package/dist/auth.js +0 -307
- package/dist/avatar.d.ts +0 -55
- package/dist/avatar.js +0 -104
- package/dist/board-command-support.d.ts +0 -6
- package/dist/board-command-support.js +0 -88
- package/dist/board-export-resources.d.ts +0 -12
- package/dist/board-export-resources.js +0 -51
- package/dist/board-export.d.ts +0 -110
- package/dist/board-export.js +0 -592
- package/dist/board-fonts.d.ts +0 -11
- package/dist/board-fonts.js +0 -10
- package/dist/board-gpu/daemon.d.ts +0 -11
- package/dist/board-gpu/daemon.js +0 -295
- package/dist/board-gpu/index.d.ts +0 -33
- package/dist/board-gpu/index.js +0 -189
- package/dist/board-gpu/protocol.d.ts +0 -77
- package/dist/board-gpu/protocol.js +0 -1
- package/dist/board-kernel.d.ts +0 -5
- package/dist/board-media-export.d.ts +0 -17
- package/dist/board-media-export.js +0 -164
- package/dist/board-media.d.ts +0 -5
- package/dist/board-media.js +0 -66
- package/dist/board-profile.d.ts +0 -21
- package/dist/board-profile.js +0 -67
- package/dist/board-simulate.d.ts +0 -67
- package/dist/board-simulate.js +0 -259
- package/dist/board-time.d.ts +0 -3
- package/dist/board-time.js +0 -34
- package/dist/bootstrap.d.ts +0 -2
- package/dist/client.d.ts +0 -5
- package/dist/client.js +0 -24
- package/dist/command-input.d.ts +0 -4
- package/dist/command-input.js +0 -16
- package/dist/commands/app-commerce.d.ts +0 -2
- package/dist/commands/app-commerce.js +0 -228
- package/dist/commands/app-domains.d.ts +0 -2
- package/dist/commands/app-domains.js +0 -133
- package/dist/commands/apps.d.ts +0 -4
- package/dist/commands/apps.js +0 -827
- package/dist/commands/auth.d.ts +0 -2
- package/dist/commands/auth.js +0 -186
- package/dist/commands/boards/authoring.d.ts +0 -2
- package/dist/commands/boards/authoring.js +0 -166
- package/dist/commands/boards/examples.d.ts +0 -10
- package/dist/commands/boards/examples.js +0 -118
- package/dist/commands/boards.d.ts +0 -13
- package/dist/commands/boards.js +0 -836
- package/dist/commands/channels.d.ts +0 -2
- package/dist/commands/channels.js +0 -74
- package/dist/commands/cron-jobs.d.ts +0 -2
- package/dist/commands/cron-jobs.js +0 -155
- package/dist/commands/decision.d.ts +0 -4
- package/dist/commands/decision.js +0 -113
- package/dist/commands/desktop.d.ts +0 -10
- package/dist/commands/desktop.js +0 -233
- package/dist/commands/feedback.d.ts +0 -2
- package/dist/commands/feedback.js +0 -131
- package/dist/commands/generations.d.ts +0 -2
- package/dist/commands/generations.js +0 -427
- package/dist/commands/host.d.ts +0 -2
- package/dist/commands/host.js +0 -11
- package/dist/commands/me.d.ts +0 -2
- package/dist/commands/me.js +0 -121
- package/dist/commands/models.d.ts +0 -28
- package/dist/commands/models.js +0 -271
- package/dist/commands/profile.d.ts +0 -2
- package/dist/commands/profile.js +0 -81
- package/dist/commands/prompts.d.ts +0 -2
- package/dist/commands/prompts.js +0 -37
- package/dist/commands/public.d.ts +0 -20
- package/dist/commands/public.js +0 -291
- package/dist/commands/references.d.ts +0 -2
- package/dist/commands/references.js +0 -193
- package/dist/commands/referrals.d.ts +0 -2
- package/dist/commands/referrals.js +0 -88
- package/dist/commands/rooms-connect.d.ts +0 -17
- package/dist/commands/rooms-connect.js +0 -140
- package/dist/commands/rooms-rtc-binary.d.ts +0 -2
- package/dist/commands/rooms-rtc-binary.js +0 -107
- package/dist/commands/rooms-rtc.d.ts +0 -21
- package/dist/commands/rooms-rtc.js +0 -171
- package/dist/commands/rooms.d.ts +0 -3
- package/dist/commands/rooms.js +0 -277
- package/dist/commands/run.d.ts +0 -13
- package/dist/commands/run.js +0 -246
- package/dist/commands/runtime.d.ts +0 -3
- package/dist/commands/runtime.js +0 -320
- package/dist/commands/sandboxd-binary.d.ts +0 -11
- package/dist/commands/sandboxd-binary.js +0 -238
- package/dist/commands/search.d.ts +0 -2
- package/dist/commands/search.js +0 -114
- package/dist/commands/session-access.d.ts +0 -2
- package/dist/commands/session-access.js +0 -70
- package/dist/commands/skills.d.ts +0 -2
- package/dist/commands/skills.js +0 -34
- package/dist/commands/space-activity.d.ts +0 -26
- package/dist/commands/space-activity.js +0 -147
- package/dist/commands/space-commerce.d.ts +0 -2
- package/dist/commands/space-commerce.js +0 -514
- package/dist/commands/space-displays.d.ts +0 -2
- package/dist/commands/space-displays.js +0 -620
- package/dist/commands/space-files.d.ts +0 -5
- package/dist/commands/space-files.js +0 -215
- package/dist/commands/space-invitations.d.ts +0 -29
- package/dist/commands/space-invitations.js +0 -189
- package/dist/commands/space-turns.d.ts +0 -49
- package/dist/commands/space-turns.js +0 -205
- package/dist/commands/space-wallet.d.ts +0 -2
- package/dist/commands/space-wallet.js +0 -141
- package/dist/commands/space-webhooks.d.ts +0 -7
- package/dist/commands/space-webhooks.js +0 -73
- package/dist/commands/spaces.d.ts +0 -25
- package/dist/commands/spaces.js +0 -2170
- package/dist/commands/tasks.d.ts +0 -8
- package/dist/commands/tasks.js +0 -80
- package/dist/commands/version.d.ts +0 -2
- package/dist/commands/version.js +0 -21
- package/dist/endpoint-options.d.ts +0 -4
- package/dist/endpoint-options.js +0 -26
- package/dist/env-proxy.d.ts +0 -5
- package/dist/env-proxy.js +0 -20
- package/dist/help-path.d.ts +0 -18
- package/dist/help-path.js +0 -223
- package/dist/host/auth.d.ts +0 -20
- package/dist/host/auth.js +0 -119
- package/dist/host/login-env.d.ts +0 -1
- package/dist/host/login-env.js +0 -31
- package/dist/host/runtime.d.ts +0 -35
- package/dist/host/runtime.js +0 -395
- package/dist/host/runtimes.d.ts +0 -9
- package/dist/host/runtimes.js +0 -42
- package/dist/host/serve.d.ts +0 -2
- package/dist/host/serve.js +0 -117
- package/dist/host/workspaces.d.ts +0 -2
- package/dist/host/workspaces.js +0 -18
- package/dist/http-put.d.ts +0 -25
- package/dist/http-put.js +0 -132
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -129
- package/dist/launcher.d.ts +0 -4
- package/dist/launcher.js +0 -38
- package/dist/local-media.d.ts +0 -2
- package/dist/local-media.js +0 -30
- package/dist/network-error.d.ts +0 -3
- package/dist/network-error.js +0 -22
- package/dist/output.d.ts +0 -46
- package/dist/output.js +0 -244
- package/dist/runtime/archive-store.d.ts +0 -54
- package/dist/runtime/archive-store.js +0 -379
- package/dist/runtime/cli-path.d.ts +0 -5
- package/dist/runtime/cli-path.js +0 -27
- package/dist/runtime/codex-usage.d.ts +0 -7
- package/dist/runtime/codex-usage.js +0 -11
- package/dist/runtime/connection.d.ts +0 -42
- package/dist/runtime/connection.js +0 -789
- package/dist/runtime/diagnostics.d.ts +0 -107
- package/dist/runtime/diagnostics.js +0 -392
- package/dist/runtime/harnesses/chained-projection.d.ts +0 -40
- package/dist/runtime/harnesses/chained-projection.js +0 -95
- package/dist/runtime/harnesses/chained-transcript.d.ts +0 -24
- package/dist/runtime/harnesses/chained-transcript.js +0 -62
- package/dist/runtime/harnesses/claude-approval.d.ts +0 -34
- package/dist/runtime/harnesses/claude-approval.js +0 -124
- package/dist/runtime/harnesses/claude-transcript.d.ts +0 -8
- package/dist/runtime/harnesses/claude-transcript.js +0 -138
- package/dist/runtime/harnesses/claude.d.ts +0 -8
- package/dist/runtime/harnesses/claude.js +0 -225
- package/dist/runtime/harnesses/codex-approval.d.ts +0 -25
- package/dist/runtime/harnesses/codex-approval.js +0 -69
- package/dist/runtime/harnesses/codex.d.ts +0 -4
- package/dist/runtime/harnesses/codex.js +0 -269
- package/dist/runtime/harnesses/cursor-transcript.d.ts +0 -37
- package/dist/runtime/harnesses/cursor-transcript.js +0 -183
- package/dist/runtime/harnesses/cursor.d.ts +0 -24
- package/dist/runtime/harnesses/cursor.js +0 -620
- package/dist/runtime/harnesses/index.d.ts +0 -16
- package/dist/runtime/harnesses/index.js +0 -73
- package/dist/runtime/harnesses/pi.d.ts +0 -4
- package/dist/runtime/harnesses/pi.js +0 -247
- package/dist/runtime/harnesses/process.d.ts +0 -31
- package/dist/runtime/harnesses/process.js +0 -75
- package/dist/runtime/harnesses/stream-json.d.ts +0 -48
- package/dist/runtime/harnesses/stream-json.js +0 -336
- package/dist/runtime/harnesses/types.d.ts +0 -117
- package/dist/runtime/harnesses/types.js +0 -1
- package/dist/runtime/harnesses/workbuddy-transcript.d.ts +0 -8
- package/dist/runtime/harnesses/workbuddy-transcript.js +0 -127
- package/dist/runtime/harnesses/workbuddy.d.ts +0 -21
- package/dist/runtime/harnesses/workbuddy.js +0 -123
- package/dist/runtime/instance.d.ts +0 -20
- package/dist/runtime/instance.js +0 -247
- package/dist/runtime/json-rpc.d.ts +0 -53
- package/dist/runtime/json-rpc.js +0 -212
- package/dist/runtime/launch.d.ts +0 -25
- package/dist/runtime/launch.js +0 -247
- package/dist/runtime/local-workspace.d.ts +0 -15
- package/dist/runtime/local-workspace.js +0 -52
- package/dist/runtime/model-catalog.d.ts +0 -12
- package/dist/runtime/model-catalog.js +0 -84
- package/dist/runtime/native/adapters.d.ts +0 -15
- package/dist/runtime/native/adapters.js +0 -11
- package/dist/runtime/native/approvals.d.ts +0 -18
- package/dist/runtime/native/approvals.js +0 -54
- package/dist/runtime/native/archive.d.ts +0 -19
- package/dist/runtime/native/archive.js +0 -142
- package/dist/runtime/native/attach.d.ts +0 -36
- package/dist/runtime/native/attach.js +0 -128
- package/dist/runtime/native/codex-link.d.ts +0 -60
- package/dist/runtime/native/codex-link.js +0 -322
- package/dist/runtime/native/config.d.ts +0 -16
- package/dist/runtime/native/config.js +0 -24
- package/dist/runtime/native/daemon.d.ts +0 -92
- package/dist/runtime/native/daemon.js +0 -380
- package/dist/runtime/native/execution.d.ts +0 -26
- package/dist/runtime/native/execution.js +0 -34
- package/dist/runtime/native/identity.d.ts +0 -9
- package/dist/runtime/native/identity.js +0 -15
- package/dist/runtime/native/ingest.d.ts +0 -263
- package/dist/runtime/native/ingest.js +0 -884
- package/dist/runtime/native/install.d.ts +0 -9
- package/dist/runtime/native/install.js +0 -113
- package/dist/runtime/native/paths.d.ts +0 -2
- package/dist/runtime/native/paths.js +0 -7
- package/dist/runtime/native/pi-extension.d.ts +0 -36
- package/dist/runtime/native/pi-link.d.ts +0 -69
- package/dist/runtime/native/pi-link.js +0 -269
- package/dist/runtime/native/results.d.ts +0 -41
- package/dist/runtime/native/results.js +0 -128
- package/dist/runtime/native/sessions.d.ts +0 -45
- package/dist/runtime/native/sessions.js +0 -87
- package/dist/runtime/native/transcript.d.ts +0 -89
- package/dist/runtime/native/transcript.js +0 -656
- package/dist/runtime/native/translate.d.ts +0 -46
- package/dist/runtime/native/translate.js +0 -299
- package/dist/runtime/native/transport.d.ts +0 -7
- package/dist/runtime/native/transport.js +0 -16
- package/dist/runtime/native/version.d.ts +0 -5
- package/dist/runtime/native/version.js +0 -21
- package/dist/runtime/preferences.d.ts +0 -7
- package/dist/runtime/preferences.js +0 -22
- package/dist/runtime/presentation.d.ts +0 -32
- package/dist/runtime/presentation.js +0 -124
- package/dist/runtime/process-group.d.ts +0 -5
- package/dist/runtime/process-group.js +0 -161
- package/dist/runtime/projection-store.d.ts +0 -27
- package/dist/runtime/projection-store.js +0 -30
- package/dist/runtime/space-binding.d.ts +0 -48
- package/dist/runtime/space-binding.js +0 -342
- package/dist/runtime/supervisor.d.ts +0 -19
- package/dist/runtime/supervisor.js +0 -441
- package/dist/runtime/turn-projection.d.ts +0 -41
- package/dist/runtime/turn-projection.js +0 -120
- package/dist/runtime/worker.d.ts +0 -1
- package/dist/safe-remote-image.d.ts +0 -36
- package/dist/safe-remote-image.js +0 -191
- package/dist/self-update.d.ts +0 -7
- package/dist/self-update.js +0 -202
- package/dist/session-status.d.ts +0 -10
- package/dist/session-status.js +0 -25
- package/dist/space-files/limit.d.ts +0 -2
- package/dist/space-files/limit.js +0 -16
- package/dist/space-files/operands.d.ts +0 -17
- package/dist/space-files/operands.js +0 -33
- package/dist/space-files/transfer.d.ts +0 -53
- package/dist/space-files/transfer.js +0 -297
- package/dist/space-files/upload.d.ts +0 -17
- package/dist/space-files/upload.js +0 -76
- package/dist/space.d.ts +0 -44
- package/dist/space.js +0 -192
- package/dist/version.d.ts +0 -2
- package/dist/version.js +0 -17
- package/dist/web.d.ts +0 -1
- package/dist/web.js +0 -4
- /package/dist/{board-kernel → chunks}/lexer.js +0 -0
|
@@ -1,789 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { setTimeout as delay } from "node:timers/promises";
|
|
3
|
-
import { isDeepStrictEqual } from "node:util";
|
|
4
|
-
import { RUNTIME_MAX_FRAME_BYTES, RUNTIME_PROTOCOL_VERSION, runtimeCapabilitiesSchema, runtimeCommandSchema, runtimeHelloCapabilities, runtimeReadySchema, runtimeNativeStopSchema, runtimeApprovalDecisionSchema, } from "@neta-art/cohub";
|
|
5
|
-
import { describeError } from "../network-error.js";
|
|
6
|
-
import { CLI_VERSION } from "../version.js";
|
|
7
|
-
import { executeTurn } from "./native/execution.js";
|
|
8
|
-
import { ContextRequiredError } from "./native/results.js";
|
|
9
|
-
import { SizeLimitError } from "./native/transcript.js";
|
|
10
|
-
import { ProcessCleanupUncertainError } from "./process-group.js";
|
|
11
|
-
import { serializeDiagnosticError, } from "./diagnostics.js";
|
|
12
|
-
export async function serveRuntime(options) {
|
|
13
|
-
const runtimeId = options.runtimeId ?? options.diagnostics?.runtimeId ?? randomUUID();
|
|
14
|
-
let backoff = 500;
|
|
15
|
-
let attempt = 0;
|
|
16
|
-
let conflictSince = null;
|
|
17
|
-
// Executions outlive a single transport so a dropped socket reconnects immediately
|
|
18
|
-
// instead of waiting for in-flight turns to finish.
|
|
19
|
-
const executions = options.executions ?? new Map();
|
|
20
|
-
const seen = new Set();
|
|
21
|
-
const announce = { current: null };
|
|
22
|
-
const catalog = { current: options.capabilities, publish: null };
|
|
23
|
-
const uploads = new AbortController();
|
|
24
|
-
const uploadSignal = AbortSignal.any([options.signal, uploads.signal]);
|
|
25
|
-
const log = (level, event, data, context) => options.diagnostics?.log(level, event, data, context);
|
|
26
|
-
const flush = () => options.executor.archives.flush(uploadSignal).catch((error) => {
|
|
27
|
-
if (!uploadSignal.aborted) {
|
|
28
|
-
log("warn", "archive.flush_failed", { error: serializeDiagnosticError(error) });
|
|
29
|
-
// The diagnostic sink handles terminal presentation and throttling.
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
const timer = setInterval(() => {
|
|
33
|
-
void flush();
|
|
34
|
-
}, 10_000);
|
|
35
|
-
let refreshing = false;
|
|
36
|
-
const refreshCatalog = async () => {
|
|
37
|
-
if (!options.refreshCapabilities || refreshing || options.signal.aborted)
|
|
38
|
-
return;
|
|
39
|
-
refreshing = true;
|
|
40
|
-
try {
|
|
41
|
-
const next = await options.refreshCapabilities(catalog.current);
|
|
42
|
-
// The Gateway closes a Runtime that sends an invalid frame; an invalid catalog stays local.
|
|
43
|
-
runtimeCapabilitiesSchema.parse(next);
|
|
44
|
-
if (options.signal.aborted || isDeepStrictEqual(next, catalog.current))
|
|
45
|
-
return;
|
|
46
|
-
catalog.current = next;
|
|
47
|
-
log("info", "runtime.catalog_changed", { harnesses: next.harnesses, modelCount: next.models.length });
|
|
48
|
-
catalog.publish?.();
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
log("warn", "runtime.catalog_refresh_failed", { error: serializeDiagnosticError(error) });
|
|
52
|
-
}
|
|
53
|
-
finally {
|
|
54
|
-
refreshing = false;
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
const catalogTimer = options.refreshCapabilities ? setInterval(() => { void refreshCatalog(); }, options.capabilitiesRefreshMs ?? 60 * 60_000) : undefined;
|
|
58
|
-
void flush();
|
|
59
|
-
log("info", "runtime.started", {
|
|
60
|
-
runtimeId,
|
|
61
|
-
workspace: options.cwd,
|
|
62
|
-
harnesses: options.capabilities.harnesses,
|
|
63
|
-
});
|
|
64
|
-
try {
|
|
65
|
-
while (!options.signal.aborted) {
|
|
66
|
-
attempt += 1;
|
|
67
|
-
let readyAt = 0;
|
|
68
|
-
const connectStartedAt = Date.now();
|
|
69
|
-
let outcome;
|
|
70
|
-
try {
|
|
71
|
-
outcome = await connect({
|
|
72
|
-
...options,
|
|
73
|
-
runtimeId,
|
|
74
|
-
attempt,
|
|
75
|
-
executions,
|
|
76
|
-
seen,
|
|
77
|
-
announce,
|
|
78
|
-
catalog,
|
|
79
|
-
onReady: () => {
|
|
80
|
-
readyAt = Date.now();
|
|
81
|
-
conflictSince = null;
|
|
82
|
-
options.onReady();
|
|
83
|
-
void flush();
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
if (options.signal.aborted)
|
|
89
|
-
return;
|
|
90
|
-
log("warn", "runtime.connection_failed", { error: serializeDiagnosticError(error) });
|
|
91
|
-
outcome = { kind: "retry" };
|
|
92
|
-
}
|
|
93
|
-
options.onDisconnected?.();
|
|
94
|
-
if (options.signal.aborted)
|
|
95
|
-
return;
|
|
96
|
-
if (outcome.kind === "fatal")
|
|
97
|
-
throw new Error(`Runtime connection rejected (${[outcome.code, outcome.reason].filter(Boolean).join(" ")}); ${outcome.code === 4403 ? "check Space permissions" : "upgrade the CLI"}`);
|
|
98
|
-
if (readyAt && Date.now() - readyAt >= 60_000) {
|
|
99
|
-
backoff = 500;
|
|
100
|
-
attempt = 0;
|
|
101
|
-
}
|
|
102
|
-
if (outcome.kind === "conflict") {
|
|
103
|
-
conflictSince ??= Date.now();
|
|
104
|
-
if (Date.now() - conflictSince >= (options.leaseConflictTimeoutMs ?? 90_000)) {
|
|
105
|
-
throw new Error("Space is already connected to another Runtime");
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
log("debug", "runtime.reconnect_scheduled", {
|
|
109
|
-
attempt: attempt || 1,
|
|
110
|
-
delayMs: backoff,
|
|
111
|
-
outcome: outcome.kind,
|
|
112
|
-
connectedMs: Date.now() - connectStartedAt,
|
|
113
|
-
readyMs: readyAt ? Date.now() - readyAt : null,
|
|
114
|
-
activeExecutions: executions.size,
|
|
115
|
-
});
|
|
116
|
-
await delay(backoff / 2 + Math.random() * backoff / 2, undefined, { signal: options.signal }).catch(() => undefined);
|
|
117
|
-
backoff = Math.min(30_000, backoff * 2);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
finally {
|
|
121
|
-
clearInterval(catalogTimer);
|
|
122
|
-
for (const execution of executions.values())
|
|
123
|
-
execution.controller.abort();
|
|
124
|
-
await Promise.allSettled([...executions.values()].map((entry) => entry.promise));
|
|
125
|
-
clearInterval(timer);
|
|
126
|
-
uploads.abort();
|
|
127
|
-
await flush();
|
|
128
|
-
log("info", "runtime.stopped", { runtimeId });
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
function frameIdentity(frame) {
|
|
132
|
-
const value = frame;
|
|
133
|
-
const event = value?.event;
|
|
134
|
-
const pick = (input) => typeof input === "string" ? input : undefined;
|
|
135
|
-
return {
|
|
136
|
-
frameType: pick(value?.type),
|
|
137
|
-
requestId: pick(value?.requestId),
|
|
138
|
-
eventType: pick(event?.type),
|
|
139
|
-
sessionId: pick(event?.sessionId) ?? pick(event?.input?.sessionId),
|
|
140
|
-
turnId: pick(event?.turnId) ?? pick(event?.input?.turnId),
|
|
141
|
-
harness: pick(event?.input?.harness),
|
|
142
|
-
nativeSessionId: pick(event?.input?.nativeSessionId),
|
|
143
|
-
turnCount: Array.isArray(event?.input?.turns) ? event.input.turns.length : undefined,
|
|
144
|
-
turnIds: Array.isArray(event?.input?.turns) ? event.input.turns.slice(0, 10).map((turn) => pick(turn?.turnId)).filter(Boolean) : undefined,
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
function executionContext(connectionId, input) {
|
|
148
|
-
return {
|
|
149
|
-
connectionId,
|
|
150
|
-
sessionId: input.sessionId,
|
|
151
|
-
turnId: input.turnId,
|
|
152
|
-
harness: input.harness,
|
|
153
|
-
requestId: input.traceContext?.requestId ?? input.requestId ?? null,
|
|
154
|
-
traceContext: input.traceContext,
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
async function connect(options) {
|
|
158
|
-
const log = (level, event, data, context) => options.diagnostics?.log(level, event, data, context);
|
|
159
|
-
const connectedAt = Date.now();
|
|
160
|
-
log("debug", "runtime.websocket.connecting", {
|
|
161
|
-
attempt: options.attempt,
|
|
162
|
-
url: options.url,
|
|
163
|
-
});
|
|
164
|
-
let currentToken;
|
|
165
|
-
try {
|
|
166
|
-
currentToken = await options.token();
|
|
167
|
-
}
|
|
168
|
-
catch (error) {
|
|
169
|
-
log("warn", error instanceof Error && error.name === "AuthRequiredError" ? "runtime.auth_required" : "runtime.auth_token_failed", { error: serializeDiagnosticError(error) });
|
|
170
|
-
throw error;
|
|
171
|
-
}
|
|
172
|
-
const runtimeUrl = new URL(options.url);
|
|
173
|
-
runtimeUrl.searchParams.set("runtimeId", options.runtimeId);
|
|
174
|
-
const socket = new WebSocket(runtimeUrl.toString());
|
|
175
|
-
const active = options.executions;
|
|
176
|
-
const seen = options.seen;
|
|
177
|
-
const disconnected = new AbortController();
|
|
178
|
-
const contexts = new Map();
|
|
179
|
-
let lastHeartbeat = Date.now();
|
|
180
|
-
let openedAt = 0;
|
|
181
|
-
let readyAt = 0;
|
|
182
|
-
let connectionId = null;
|
|
183
|
-
let fatal = false;
|
|
184
|
-
let fatalCode = 0;
|
|
185
|
-
let fatalReason = "";
|
|
186
|
-
let conflict = false;
|
|
187
|
-
let unauthorized = false;
|
|
188
|
-
let readyTimer;
|
|
189
|
-
const nativePending = new Map();
|
|
190
|
-
let nativeChannelReady = false;
|
|
191
|
-
const send = (frame) => {
|
|
192
|
-
if (socket.readyState !== WebSocket.OPEN || socket.bufferedAmount > RUNTIME_MAX_FRAME_BYTES) {
|
|
193
|
-
log("warn", "runtime.frame_send_unavailable", {
|
|
194
|
-
readyState: socket.readyState,
|
|
195
|
-
bufferedBytes: socket.bufferedAmount,
|
|
196
|
-
}, { connectionId });
|
|
197
|
-
throw new Error("Runtime connection unavailable");
|
|
198
|
-
}
|
|
199
|
-
const data = JSON.stringify(frame);
|
|
200
|
-
const bytes = Buffer.byteLength(data);
|
|
201
|
-
if (bytes > RUNTIME_MAX_FRAME_BYTES) {
|
|
202
|
-
const identity = frameIdentity(frame);
|
|
203
|
-
const execution = identity.requestId ? active.get(identity.requestId) : undefined;
|
|
204
|
-
log("error", "runtime.frame_too_large", { bytes, maxBytes: RUNTIME_MAX_FRAME_BYTES, ...identity }, execution ? executionContext(connectionId, execution) : { connectionId });
|
|
205
|
-
throw new SizeLimitError("Runtime frame exceeds transfer limit");
|
|
206
|
-
}
|
|
207
|
-
socket.send(data);
|
|
208
|
-
};
|
|
209
|
-
const closeTransport = () => socket.close();
|
|
210
|
-
/** What the Gateway holds for this connection. */
|
|
211
|
-
let registered = null;
|
|
212
|
-
let capabilitiesFrame = false;
|
|
213
|
-
// A Gateway without the update frame learns a changed catalog from the next hello.
|
|
214
|
-
const publishCatalog = () => {
|
|
215
|
-
const current = options.catalog.current;
|
|
216
|
-
if (!connectionId || isDeepStrictEqual(registered, capabilitiesFrame ? current : runtimeHelloCapabilities(current)))
|
|
217
|
-
return;
|
|
218
|
-
if (!capabilitiesFrame) {
|
|
219
|
-
log("info", "runtime.catalog_reconnect", undefined, { connectionId });
|
|
220
|
-
closeTransport();
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
try {
|
|
224
|
-
send({ type: "runtime.capabilities", capabilities: current });
|
|
225
|
-
registered = current;
|
|
226
|
-
log("info", "runtime.catalog_published", { modelCount: current.models.length }, { connectionId });
|
|
227
|
-
}
|
|
228
|
-
catch (error) {
|
|
229
|
-
log("warn", "runtime.catalog_publish_failed", { error: serializeDiagnosticError(error) }, { connectionId });
|
|
230
|
-
closeTransport();
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
const shutdown = () => {
|
|
234
|
-
for (const execution of active.values())
|
|
235
|
-
execution.controller.abort();
|
|
236
|
-
closeTransport();
|
|
237
|
-
};
|
|
238
|
-
const sendNative = (event) => new Promise((resolve, reject) => {
|
|
239
|
-
if (!nativeChannelReady || !connectionId) {
|
|
240
|
-
reject(new Error("Runtime native channel is unavailable"));
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
const requestId = randomUUID();
|
|
244
|
-
// An ingest batch writes each of its Turns durably; everything else answers from memory or one row.
|
|
245
|
-
const timer = setTimeout(() => { nativePending.delete(requestId); reject(new Error("Native Runtime event timed out")); }, event.type === "ingest" ? 120_000 : 30_000);
|
|
246
|
-
nativePending.set(requestId, { resolve, reject, timer });
|
|
247
|
-
try {
|
|
248
|
-
send({ type: "runtime.native", requestId, event });
|
|
249
|
-
}
|
|
250
|
-
catch (error) {
|
|
251
|
-
clearTimeout(timer);
|
|
252
|
-
nativePending.delete(requestId);
|
|
253
|
-
reject(error instanceof Error ? error : new Error(String(error)));
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
options.signal.addEventListener("abort", shutdown, { once: true });
|
|
257
|
-
const heartbeat = setInterval(() => {
|
|
258
|
-
const heartbeatAgeMs = Date.now() - lastHeartbeat;
|
|
259
|
-
if (heartbeatAgeMs > 30_000) {
|
|
260
|
-
log("error", "runtime.heartbeat_timeout", {
|
|
261
|
-
ageMs: heartbeatAgeMs,
|
|
262
|
-
activeExecutions: active.size,
|
|
263
|
-
readyState: socket.readyState,
|
|
264
|
-
bufferedBytes: socket.bufferedAmount,
|
|
265
|
-
}, { connectionId });
|
|
266
|
-
closeTransport();
|
|
267
|
-
// A half-open TCP path may never deliver the close event; stop waiting so reconnect can start.
|
|
268
|
-
if (heartbeatAgeMs > 45_000)
|
|
269
|
-
handleClose({ code: 1006, reason: "Runtime heartbeat timed out", wasClean: false }, true);
|
|
270
|
-
}
|
|
271
|
-
else if (connectionId) {
|
|
272
|
-
try {
|
|
273
|
-
send({ type: "runtime.heartbeat" });
|
|
274
|
-
}
|
|
275
|
-
catch (error) {
|
|
276
|
-
log("warn", "runtime.heartbeat_send_failed", {
|
|
277
|
-
error: serializeDiagnosticError(error),
|
|
278
|
-
}, { connectionId });
|
|
279
|
-
closeTransport();
|
|
280
|
-
}
|
|
281
|
-
void options.token().then((next) => {
|
|
282
|
-
if (next !== currentToken) {
|
|
283
|
-
try {
|
|
284
|
-
send({ type: "runtime.auth", token: next });
|
|
285
|
-
currentToken = next;
|
|
286
|
-
log("debug", "runtime.auth_refreshed", undefined, { connectionId });
|
|
287
|
-
}
|
|
288
|
-
catch (error) {
|
|
289
|
-
log("warn", "runtime.auth_refresh_send_failed", {
|
|
290
|
-
error: serializeDiagnosticError(error),
|
|
291
|
-
}, { connectionId });
|
|
292
|
-
closeTransport();
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}).catch((error) => {
|
|
296
|
-
log("warn", "runtime.auth_refresh_failed", { error: serializeDiagnosticError(error) }, { connectionId });
|
|
297
|
-
closeTransport();
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
}, 10_000);
|
|
301
|
-
const announceHere = (execution) => {
|
|
302
|
-
try {
|
|
303
|
-
send({ type: "runtime.recovery", executions: [execution] });
|
|
304
|
-
log("info", "runtime.detached_result_announced", undefined, executionContext(connectionId, execution));
|
|
305
|
-
}
|
|
306
|
-
catch {
|
|
307
|
-
// The next ready connection reports every pending receipt.
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
/** Replays a saved result, or answers explicitly that none is available; never leaves the server waiting. */
|
|
311
|
-
const replaySaved = async (requestId, input, context, executionRequestId) => {
|
|
312
|
-
let saved = null;
|
|
313
|
-
try {
|
|
314
|
-
saved = await options.executor.results.recover(input, executionRequestId);
|
|
315
|
-
}
|
|
316
|
-
catch (error) {
|
|
317
|
-
log("error", "runtime.recovery_failed", { error: serializeDiagnosticError(error) }, context);
|
|
318
|
-
}
|
|
319
|
-
if (saved)
|
|
320
|
-
for (const event of saved.events)
|
|
321
|
-
send({ type: "runtime.event", requestId, event });
|
|
322
|
-
else
|
|
323
|
-
send({ type: "runtime.event", requestId, event: { type: "turn.error", uncertain: true, message: "Result unavailable; files retained" } });
|
|
324
|
-
};
|
|
325
|
-
let closeHandled = false;
|
|
326
|
-
let resolveClosed = () => { };
|
|
327
|
-
const closed = new Promise((resolve) => { resolveClosed = resolve; });
|
|
328
|
-
const handleClose = (event, forced = false) => {
|
|
329
|
-
if (closeHandled)
|
|
330
|
-
return;
|
|
331
|
-
closeHandled = true;
|
|
332
|
-
unauthorized = event.code === 4401;
|
|
333
|
-
fatal = [4400, 4403].includes(event.code);
|
|
334
|
-
if (fatal) {
|
|
335
|
-
fatalCode = event.code;
|
|
336
|
-
fatalReason = event.reason;
|
|
337
|
-
}
|
|
338
|
-
conflict = event.code === 4409;
|
|
339
|
-
disconnected.abort();
|
|
340
|
-
const now = Date.now();
|
|
341
|
-
log(options.signal.aborted ? "debug" : fatal || conflict ? "error" : "warn", fatal ? "runtime.websocket.rejected" : "runtime.websocket.closed", {
|
|
342
|
-
code: event.code,
|
|
343
|
-
reason: event.reason,
|
|
344
|
-
wasClean: event.wasClean,
|
|
345
|
-
forced,
|
|
346
|
-
durationMs: now - connectedAt,
|
|
347
|
-
openMs: openedAt ? now - openedAt : null,
|
|
348
|
-
readyMs: readyAt ? now - readyAt : null,
|
|
349
|
-
lastInboundAgeMs: now - lastHeartbeat,
|
|
350
|
-
bufferedBytes: socket.bufferedAmount,
|
|
351
|
-
activeExecutions: active.size,
|
|
352
|
-
fatal,
|
|
353
|
-
conflict,
|
|
354
|
-
}, { connectionId });
|
|
355
|
-
nativeChannelReady = false;
|
|
356
|
-
for (const pending of nativePending.values()) {
|
|
357
|
-
clearTimeout(pending.timer);
|
|
358
|
-
pending.reject(new Error("Runtime native channel disconnected"));
|
|
359
|
-
}
|
|
360
|
-
nativePending.clear();
|
|
361
|
-
options.onDisconnected?.();
|
|
362
|
-
const executionMustStop = options.signal.aborted || fatal || conflict;
|
|
363
|
-
if (options.announce.current === announceHere)
|
|
364
|
-
options.announce.current = null;
|
|
365
|
-
if (options.catalog.publish === publishCatalog)
|
|
366
|
-
options.catalog.publish = null;
|
|
367
|
-
for (const [requestId, execution] of active) {
|
|
368
|
-
if (executionMustStop) {
|
|
369
|
-
log("error", "runtime.execution_transport_invalidated", { code: event.code, reason: event.reason }, executionContext(connectionId, execution));
|
|
370
|
-
execution.controller.abort();
|
|
371
|
-
}
|
|
372
|
-
else if (execution.recovery) {
|
|
373
|
-
// The server re-asks on the next connection; a stale waiter must not hold a slot or its request id.
|
|
374
|
-
execution.controller.abort();
|
|
375
|
-
}
|
|
376
|
-
else if (!execution.detached) {
|
|
377
|
-
execution.detached = true;
|
|
378
|
-
log("warn", "runtime.execution_transport_detached", { code: event.code, reason: event.reason }, executionContext(connectionId, execution));
|
|
379
|
-
// The server may have stopped asking by the time this finishes; a saved result is announced again.
|
|
380
|
-
void execution.promise.finally(() => {
|
|
381
|
-
if (!execution.result || active.get(requestId) !== execution)
|
|
382
|
-
return;
|
|
383
|
-
options.announce.current?.({ sessionId: execution.sessionId, turnId: execution.turnId, harness: execution.harness });
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
resolveClosed();
|
|
388
|
-
};
|
|
389
|
-
socket.addEventListener("close", (event) => handleClose(event), { once: true });
|
|
390
|
-
socket.addEventListener("error", (event) => {
|
|
391
|
-
const detail = event;
|
|
392
|
-
log("warn", "runtime.websocket.error", {
|
|
393
|
-
type: event.type,
|
|
394
|
-
message: typeof detail.message === "string" && detail.message ? detail.message : undefined,
|
|
395
|
-
readyState: socket.readyState,
|
|
396
|
-
opened: openedAt > 0,
|
|
397
|
-
ready: readyAt > 0,
|
|
398
|
-
lastInboundAgeMs: Date.now() - lastHeartbeat,
|
|
399
|
-
error: serializeDiagnosticError(detail.error ?? new Error("WebSocket error")),
|
|
400
|
-
}, { connectionId });
|
|
401
|
-
socket.close();
|
|
402
|
-
});
|
|
403
|
-
socket.addEventListener("open", () => {
|
|
404
|
-
openedAt = Date.now();
|
|
405
|
-
try {
|
|
406
|
-
log("info", "runtime.websocket.open", {
|
|
407
|
-
attempt: options.attempt,
|
|
408
|
-
durationMs: Date.now() - connectedAt,
|
|
409
|
-
});
|
|
410
|
-
registered = runtimeHelloCapabilities(options.catalog.current);
|
|
411
|
-
send({
|
|
412
|
-
type: "runtime.hello",
|
|
413
|
-
version: RUNTIME_PROTOCOL_VERSION,
|
|
414
|
-
spaceId: options.spaceId,
|
|
415
|
-
token: currentToken,
|
|
416
|
-
capabilities: registered,
|
|
417
|
-
client: { name: "cli", version: CLI_VERSION },
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
catch (error) {
|
|
421
|
-
log("error", "runtime.hello_failed", { error: serializeDiagnosticError(error) });
|
|
422
|
-
closeTransport();
|
|
423
|
-
}
|
|
424
|
-
});
|
|
425
|
-
socket.addEventListener("message", (event) => {
|
|
426
|
-
void (async () => {
|
|
427
|
-
let raw;
|
|
428
|
-
try {
|
|
429
|
-
raw = JSON.parse(String(event.data));
|
|
430
|
-
}
|
|
431
|
-
catch (error) {
|
|
432
|
-
log("error", "runtime.protocol.invalid_json", { error: serializeDiagnosticError(error) }, { connectionId });
|
|
433
|
-
throw error;
|
|
434
|
-
}
|
|
435
|
-
// Any inbound frame proves the transport is live; heartbeat frames are one case.
|
|
436
|
-
lastHeartbeat = Date.now();
|
|
437
|
-
if (raw.type === "runtime.ready") {
|
|
438
|
-
const frame = runtimeReadySchema.parse(raw);
|
|
439
|
-
if (connectionId === frame.connectionId)
|
|
440
|
-
return;
|
|
441
|
-
if (connectionId)
|
|
442
|
-
throw new Error("Runtime connection identity changed");
|
|
443
|
-
connectionId = frame.connectionId;
|
|
444
|
-
readyAt = Date.now();
|
|
445
|
-
clearTimeout(readyTimer);
|
|
446
|
-
log("info", "runtime.ready", {
|
|
447
|
-
connectionId,
|
|
448
|
-
durationMs: readyAt - connectedAt,
|
|
449
|
-
detachedExecutions: [...active.values()].filter((entry) => entry.detached).length,
|
|
450
|
-
}, { connectionId });
|
|
451
|
-
nativeChannelReady = true;
|
|
452
|
-
capabilitiesFrame = frame.features?.includes("capabilities") === true;
|
|
453
|
-
options.onNativeChannel?.(sendNative, { approvals: frame.features?.includes("approvals") === true });
|
|
454
|
-
options.onReady();
|
|
455
|
-
options.announce.current = announceHere;
|
|
456
|
-
options.catalog.publish = publishCatalog;
|
|
457
|
-
publishCatalog();
|
|
458
|
-
let recoveryBatch = 0;
|
|
459
|
-
for await (const executions of options.executor.results.pendingBatches()) {
|
|
460
|
-
recoveryBatch += 1;
|
|
461
|
-
log("info", "runtime.recovery_batch_sent", {
|
|
462
|
-
batch: recoveryBatch,
|
|
463
|
-
count: executions.length,
|
|
464
|
-
}, { connectionId });
|
|
465
|
-
send({ type: "runtime.recovery", executions });
|
|
466
|
-
}
|
|
467
|
-
return;
|
|
468
|
-
}
|
|
469
|
-
if (!connectionId)
|
|
470
|
-
throw new Error("Runtime handshake is incomplete");
|
|
471
|
-
if (raw.type === "runtime.heartbeat") {
|
|
472
|
-
return;
|
|
473
|
-
}
|
|
474
|
-
if (raw.type === "runtime.native.stop") {
|
|
475
|
-
const stop = runtimeNativeStopSchema.safeParse(raw);
|
|
476
|
-
if (!stop.success) {
|
|
477
|
-
log("warn", "runtime.protocol.invalid_native_stop", { error: stop.error.message }, { connectionId });
|
|
478
|
-
return;
|
|
479
|
-
}
|
|
480
|
-
if (stop.data.spaceId === options.spaceId)
|
|
481
|
-
options.onNativeStop?.(stop.data);
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
if (raw.type === "runtime.approval.decision") {
|
|
485
|
-
const decision = runtimeApprovalDecisionSchema.safeParse(raw);
|
|
486
|
-
if (!decision.success) {
|
|
487
|
-
log("warn", "runtime.protocol.invalid_approval_decision", { error: decision.error.message }, { connectionId });
|
|
488
|
-
return;
|
|
489
|
-
}
|
|
490
|
-
if (decision.data.spaceId === options.spaceId)
|
|
491
|
-
options.onApprovalDecision?.(decision.data);
|
|
492
|
-
return;
|
|
493
|
-
}
|
|
494
|
-
if (raw.type === "runtime.native.result") {
|
|
495
|
-
const result = raw;
|
|
496
|
-
const pending = result.requestId ? nativePending.get(result.requestId) : null;
|
|
497
|
-
if (!pending)
|
|
498
|
-
return;
|
|
499
|
-
clearTimeout(pending.timer);
|
|
500
|
-
nativePending.delete(result.requestId ?? "");
|
|
501
|
-
if (result.error)
|
|
502
|
-
pending.reject(new Error(result.error));
|
|
503
|
-
else
|
|
504
|
-
pending.resolve(result.result);
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
const frame = runtimeCommandSchema.parse(raw);
|
|
508
|
-
if (frame.type === "session.context") {
|
|
509
|
-
log("debug", "runtime.context_received", {
|
|
510
|
-
messageCount: frame.context.messages.length,
|
|
511
|
-
}, { connectionId });
|
|
512
|
-
contexts.get(frame.requestId)?.(frame.context);
|
|
513
|
-
contexts.delete(frame.requestId);
|
|
514
|
-
return;
|
|
515
|
-
}
|
|
516
|
-
if (frame.type === "turn.abort") {
|
|
517
|
-
log("info", "runtime.turn_abort_received", undefined, { connectionId, requestId: frame.requestId });
|
|
518
|
-
active.get(frame.requestId)?.controller.abort();
|
|
519
|
-
return;
|
|
520
|
-
}
|
|
521
|
-
if (frame.type === "turn.ack") {
|
|
522
|
-
const execution = active.get(frame.requestId);
|
|
523
|
-
if (!execution)
|
|
524
|
-
return;
|
|
525
|
-
const context = executionContext(connectionId, execution);
|
|
526
|
-
try {
|
|
527
|
-
await execution.promise;
|
|
528
|
-
if (!execution.result)
|
|
529
|
-
throw new Error("Runtime result is not available");
|
|
530
|
-
await options.executor.results.acknowledge(execution.sessionId, frame.turnId);
|
|
531
|
-
}
|
|
532
|
-
catch (error) {
|
|
533
|
-
log("error", "runtime.turn_ack_failed", {
|
|
534
|
-
revision: frame.revision,
|
|
535
|
-
error: serializeDiagnosticError(error),
|
|
536
|
-
}, context);
|
|
537
|
-
// Keep the receipt; the next reconciliation can retry acknowledgement.
|
|
538
|
-
active.delete(frame.requestId);
|
|
539
|
-
send({ type: "runtime.event", requestId: frame.requestId, event: { type: "turn.error", message: "Local acknowledgement failed; result retained" } });
|
|
540
|
-
return;
|
|
541
|
-
}
|
|
542
|
-
log("info", "runtime.turn_acknowledged", { revision: frame.revision }, context);
|
|
543
|
-
active.delete(frame.requestId);
|
|
544
|
-
send({ type: "runtime.event", requestId: frame.requestId, event: { type: "turn.acknowledged" } });
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
if (frame.type === "turn.recover") {
|
|
548
|
-
const identity = frame.execution;
|
|
549
|
-
if (identity.spaceId !== options.spaceId)
|
|
550
|
-
throw new Error("Invalid Runtime target");
|
|
551
|
-
const context = executionContext(connectionId, {
|
|
552
|
-
...identity,
|
|
553
|
-
requestId: frame.traceContext?.requestId,
|
|
554
|
-
traceContext: frame.traceContext,
|
|
555
|
-
});
|
|
556
|
-
log("info", "runtime.recovery_requested", undefined, context);
|
|
557
|
-
const previous = active.get(frame.requestId);
|
|
558
|
-
if (previous) {
|
|
559
|
-
if (previous.sessionId !== identity.sessionId || previous.turnId !== identity.turnId || previous.harness !== identity.harness)
|
|
560
|
-
throw new Error("Runtime recovery identity changed");
|
|
561
|
-
await previous.promise;
|
|
562
|
-
await replaySaved(frame.requestId, identity, context);
|
|
563
|
-
return;
|
|
564
|
-
}
|
|
565
|
-
const running = [...active].find(([, entry]) => entry.turnId === identity.turnId && entry.sessionId === identity.sessionId && entry.harness === identity.harness);
|
|
566
|
-
const recovery = {
|
|
567
|
-
...identity,
|
|
568
|
-
requestId: frame.traceContext?.requestId,
|
|
569
|
-
traceContext: frame.traceContext,
|
|
570
|
-
controller: new AbortController(),
|
|
571
|
-
promise: Promise.resolve(),
|
|
572
|
-
recovery: true,
|
|
573
|
-
};
|
|
574
|
-
active.set(frame.requestId, recovery);
|
|
575
|
-
recovery.promise = (async () => {
|
|
576
|
-
try {
|
|
577
|
-
if (running) {
|
|
578
|
-
// A detached execution was not superseded by this host; wait for its result instead of discarding it.
|
|
579
|
-
if (running[1].detached) {
|
|
580
|
-
log("info", "runtime.recovery_waiting_detached", { runningRequestId: running[0] }, context);
|
|
581
|
-
// A recovery the server gave up on must not hold an execution slot until the Turn ends.
|
|
582
|
-
const stopped = recovery.controller.signal;
|
|
583
|
-
await Promise.race([running[1].promise, new Promise((resolve) => stopped.addEventListener("abort", () => resolve(), { once: true }))]);
|
|
584
|
-
stopped.throwIfAborted();
|
|
585
|
-
}
|
|
586
|
-
else {
|
|
587
|
-
running[1].controller.abort();
|
|
588
|
-
await running[1].promise;
|
|
589
|
-
}
|
|
590
|
-
if (active.get(running[0]) === running[1])
|
|
591
|
-
active.delete(running[0]);
|
|
592
|
-
}
|
|
593
|
-
const saved = await options.executor.results.recover(identity);
|
|
594
|
-
recovery.controller.signal.throwIfAborted();
|
|
595
|
-
const last = saved?.events.at(-1);
|
|
596
|
-
if (!saved || last?.type !== "turn.end")
|
|
597
|
-
throw new Error("No confirmed result");
|
|
598
|
-
recovery.result = { session: saved.session, event: last };
|
|
599
|
-
for (const event of saved.events)
|
|
600
|
-
send({ type: "runtime.event", requestId: frame.requestId, event });
|
|
601
|
-
}
|
|
602
|
-
catch (error) {
|
|
603
|
-
if (!recovery.controller.signal.aborted) {
|
|
604
|
-
log("error", "runtime.recovery_failed", { error: serializeDiagnosticError(error) }, context);
|
|
605
|
-
// Original files and receipts remain available for reconciliation.
|
|
606
|
-
try {
|
|
607
|
-
send({ type: "runtime.event", requestId: frame.requestId, event: { type: "turn.error", uncertain: true, message: "Result unavailable; files retained" } });
|
|
608
|
-
}
|
|
609
|
-
catch {
|
|
610
|
-
// The next connection can read the same result.
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
if (active.get(frame.requestId) === recovery)
|
|
614
|
-
active.delete(frame.requestId);
|
|
615
|
-
}
|
|
616
|
-
})();
|
|
617
|
-
return;
|
|
618
|
-
}
|
|
619
|
-
if (frame.input.spaceId !== options.spaceId || !options.catalog.current.harnesses.includes(frame.input.harness))
|
|
620
|
-
throw new Error("Invalid Runtime target");
|
|
621
|
-
const context = executionContext(connectionId, frame.input);
|
|
622
|
-
log("info", "runtime.turn_received", {
|
|
623
|
-
requestId: frame.requestId,
|
|
624
|
-
resumeOnly: frame.resumeOnly === true,
|
|
625
|
-
}, context);
|
|
626
|
-
const previous = active.get(frame.requestId);
|
|
627
|
-
if (previous) {
|
|
628
|
-
if (previous.sessionId !== frame.input.sessionId || previous.turnId !== frame.input.turnId || previous.harness !== frame.input.harness)
|
|
629
|
-
throw new Error("Runtime execution identity changed");
|
|
630
|
-
log("info", "runtime.execution_resumed", { detached: previous.detached === true, settled: Boolean(previous.result) }, executionContext(connectionId, frame.input));
|
|
631
|
-
await previous.promise;
|
|
632
|
-
await replaySaved(frame.requestId, frame.input, context, frame.requestId);
|
|
633
|
-
return;
|
|
634
|
-
}
|
|
635
|
-
const requestContext = async (executionSignal) => {
|
|
636
|
-
const signal = AbortSignal.any([executionSignal, disconnected.signal]);
|
|
637
|
-
signal.throwIfAborted();
|
|
638
|
-
const pendingTurnIds = await options.executor.results.pendingTurnIds(frame.input.sessionId);
|
|
639
|
-
log("debug", "runtime.context_requested", { pendingTurnCount: pendingTurnIds.length }, context);
|
|
640
|
-
return new Promise((resolve, reject) => {
|
|
641
|
-
const abort = () => {
|
|
642
|
-
clearTimeout(timeout);
|
|
643
|
-
contexts.delete(frame.requestId);
|
|
644
|
-
signal.removeEventListener("abort", abort);
|
|
645
|
-
reject(new Error("Runtime context request aborted"));
|
|
646
|
-
};
|
|
647
|
-
const timeout = setTimeout(abort, 60_000);
|
|
648
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
649
|
-
contexts.set(frame.requestId, (nextContext) => {
|
|
650
|
-
clearTimeout(timeout);
|
|
651
|
-
signal.removeEventListener("abort", abort);
|
|
652
|
-
resolve(nextContext);
|
|
653
|
-
});
|
|
654
|
-
try {
|
|
655
|
-
send({ type: "runtime.event", requestId: frame.requestId, event: { type: "context.required", pendingTurnIds } });
|
|
656
|
-
}
|
|
657
|
-
catch {
|
|
658
|
-
abort();
|
|
659
|
-
}
|
|
660
|
-
if (signal.aborted)
|
|
661
|
-
abort();
|
|
662
|
-
});
|
|
663
|
-
};
|
|
664
|
-
if ([...active.values()].some((entry) => entry.sessionId === frame.input.sessionId) && !frame.input.context.complete)
|
|
665
|
-
frame.input.context = await requestContext(options.signal);
|
|
666
|
-
const repeated = seen.has(frame.requestId);
|
|
667
|
-
for (const [requestId, entry] of active) {
|
|
668
|
-
if (entry.sessionId !== frame.input.sessionId)
|
|
669
|
-
continue;
|
|
670
|
-
const resolved = frame.input.context.resolvedTurnIds?.includes(entry.turnId);
|
|
671
|
-
const settled = entry.result && frame.input.context.settledTurnIds?.includes(entry.turnId);
|
|
672
|
-
if (!resolved && !settled)
|
|
673
|
-
continue;
|
|
674
|
-
entry.controller.abort();
|
|
675
|
-
await entry.promise;
|
|
676
|
-
active.delete(requestId);
|
|
677
|
-
}
|
|
678
|
-
if ([...active.values()].some((entry) => entry.sessionId === frame.input.sessionId) || active.size >= 8) {
|
|
679
|
-
log("warn", "runtime.turn_rejected_busy", { activeExecutions: active.size }, context);
|
|
680
|
-
send({ type: "runtime.event", requestId: frame.requestId, event: { type: "turn.error", message: "Local Runtime is busy" } });
|
|
681
|
-
return;
|
|
682
|
-
}
|
|
683
|
-
seen.add(frame.requestId);
|
|
684
|
-
if (seen.size > 4096) {
|
|
685
|
-
const oldest = seen.values().next().value;
|
|
686
|
-
if (oldest)
|
|
687
|
-
seen.delete(oldest);
|
|
688
|
-
}
|
|
689
|
-
const controller = new AbortController();
|
|
690
|
-
const execution = {
|
|
691
|
-
controller,
|
|
692
|
-
sessionId: frame.input.sessionId,
|
|
693
|
-
turnId: frame.input.turnId,
|
|
694
|
-
harness: frame.input.harness,
|
|
695
|
-
requestId: frame.input.requestId,
|
|
696
|
-
traceContext: frame.input.traceContext,
|
|
697
|
-
promise: Promise.resolve(),
|
|
698
|
-
};
|
|
699
|
-
active.set(frame.requestId, execution);
|
|
700
|
-
const durableEvents = [];
|
|
701
|
-
const emit = (value) => {
|
|
702
|
-
if (value.type === "message.commit")
|
|
703
|
-
durableEvents.push(value);
|
|
704
|
-
// Streaming is best-effort. A detached transport must not cancel local model or tool work;
|
|
705
|
-
// durable commits and the final result are replayed after reconnect.
|
|
706
|
-
if (disconnected.signal.aborted || socket.readyState !== WebSocket.OPEN || socket.bufferedAmount > RUNTIME_MAX_FRAME_BYTES)
|
|
707
|
-
return;
|
|
708
|
-
try {
|
|
709
|
-
send({ type: "runtime.event", requestId: frame.requestId, event: value });
|
|
710
|
-
}
|
|
711
|
-
catch (error) {
|
|
712
|
-
if (!disconnected.signal.aborted && socket.readyState === WebSocket.OPEN)
|
|
713
|
-
throw error;
|
|
714
|
-
}
|
|
715
|
-
};
|
|
716
|
-
execution.promise = (async () => {
|
|
717
|
-
try {
|
|
718
|
-
const saved = await options.executor.results.recover(frame.input, frame.requestId);
|
|
719
|
-
if (saved) {
|
|
720
|
-
const last = saved.events.at(-1);
|
|
721
|
-
if (last?.type !== "turn.end")
|
|
722
|
-
throw new Error("Incomplete saved Runtime result");
|
|
723
|
-
execution.result = { session: saved.session, event: last };
|
|
724
|
-
for (const event of saved.events)
|
|
725
|
-
emit(event);
|
|
726
|
-
return;
|
|
727
|
-
}
|
|
728
|
-
if (frame.resumeOnly || repeated) {
|
|
729
|
-
emit({ type: "turn.error", message: "Execution outcome is unknown; native files retained, no replay", uncertain: true });
|
|
730
|
-
active.delete(frame.requestId);
|
|
731
|
-
return;
|
|
732
|
-
}
|
|
733
|
-
const run = () => executeTurn(options.executor, frame.input, emit, controller.signal, frame.requestId, context);
|
|
734
|
-
// Preparation can request context, then fall back once from native archive to DB.
|
|
735
|
-
// These retries precede started(), so they never replay model or tool work.
|
|
736
|
-
for (let retry = 0;; retry += 1) {
|
|
737
|
-
try {
|
|
738
|
-
execution.result = await run();
|
|
739
|
-
break;
|
|
740
|
-
}
|
|
741
|
-
catch (error) {
|
|
742
|
-
if (!(error instanceof ContextRequiredError) || retry >= 2)
|
|
743
|
-
throw error;
|
|
744
|
-
frame.input.context = await requestContext(controller.signal);
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
await options.executor.results.record(execution.result.session, frame.input, frame.requestId, [...durableEvents, execution.result.event], execution.result.uncertainCleanup);
|
|
748
|
-
if (execution.result.uncertainCleanup) {
|
|
749
|
-
log("warn", "runtime.turn_cleanup_pending", { cleanup: execution.result.uncertainCleanup.error }, context);
|
|
750
|
-
}
|
|
751
|
-
emit(execution.result.event);
|
|
752
|
-
}
|
|
753
|
-
catch (error) {
|
|
754
|
-
log("error", "runtime.turn_failed", {
|
|
755
|
-
uncertain: Boolean(execution.result) || frame.resumeOnly === true || error instanceof ProcessCleanupUncertainError,
|
|
756
|
-
error: serializeDiagnosticError(error),
|
|
757
|
-
}, context);
|
|
758
|
-
try {
|
|
759
|
-
emit({ type: "turn.error", message: describeError(error), uncertain: !!execution.result || frame.resumeOnly === true || error instanceof ProcessCleanupUncertainError });
|
|
760
|
-
}
|
|
761
|
-
catch {
|
|
762
|
-
// Native files remain for recovery.
|
|
763
|
-
}
|
|
764
|
-
active.delete(frame.requestId);
|
|
765
|
-
}
|
|
766
|
-
})();
|
|
767
|
-
})().catch((error) => {
|
|
768
|
-
log("error", "runtime.protocol_error", { error: serializeDiagnosticError(error) }, { connectionId });
|
|
769
|
-
closeTransport();
|
|
770
|
-
});
|
|
771
|
-
});
|
|
772
|
-
readyTimer = setTimeout(() => {
|
|
773
|
-
log("error", "runtime.handshake_timeout", { timeoutMs: 15_000 }, { connectionId });
|
|
774
|
-
socket.close(4408, "Runtime handshake timed out");
|
|
775
|
-
}, 15_000);
|
|
776
|
-
if (options.signal.aborted)
|
|
777
|
-
shutdown();
|
|
778
|
-
try {
|
|
779
|
-
await closed;
|
|
780
|
-
}
|
|
781
|
-
finally {
|
|
782
|
-
clearTimeout(readyTimer);
|
|
783
|
-
clearInterval(heartbeat);
|
|
784
|
-
options.signal.removeEventListener("abort", shutdown);
|
|
785
|
-
}
|
|
786
|
-
if (unauthorized && !options.signal.aborted)
|
|
787
|
-
await options.token(true);
|
|
788
|
-
return fatal ? { kind: "fatal", code: fatalCode, reason: fatalReason } : conflict ? { kind: "conflict" } : { kind: "retry" };
|
|
789
|
-
}
|