@opengeni/runtime 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-DYBF7ZEB.js → chunk-RYDCVTMW.js} +1958 -207
- package/dist/chunk-RYDCVTMW.js.map +1 -0
- package/dist/{index-BTo_bZvN.d.ts → index-DIiiwQY2.d.ts} +689 -52
- package/dist/index.d.ts +293 -123
- package/dist/index.js +1819 -790
- package/dist/index.js.map +1 -1
- package/dist/sandbox/index.d.ts +1 -1
- package/dist/sandbox/index.js +79 -3
- package/package.json +21 -20
- package/src/codex-tool-search.ts +185 -31
- package/src/context-compaction.ts +78 -243
- package/src/history-sanitizer.ts +172 -20
- package/src/index.ts +2573 -898
- package/src/metrics.ts +28 -2
- package/src/sandbox/capabilities.ts +12 -3
- package/src/sandbox/channel-a.ts +420 -72
- package/src/sandbox/display-stack.ts +18 -4
- package/src/sandbox/errors.ts +13 -0
- package/src/sandbox/index.ts +398 -112
- package/src/sandbox/op-correlation.ts +70 -0
- package/src/sandbox/providers/index.ts +9 -3
- package/src/sandbox/providers/modal.ts +84 -14
- package/src/sandbox/providers/selfhosted.ts +1 -4
- package/src/sandbox/recording.ts +146 -90
- package/src/sandbox/routing/backend-resolver.ts +144 -10
- package/src/sandbox/routing/routing-session.ts +129 -25
- package/src/sandbox/select.ts +60 -8
- package/src/sandbox/selfhosted/capabilities.ts +59 -25
- package/src/sandbox/selfhosted/control-rpc.ts +160 -28
- package/src/sandbox/selfhosted/fault-rendering.ts +226 -0
- package/src/sandbox/selfhosted/op-observer.ts +54 -0
- package/src/sandbox/selfhosted/op-stream.ts +801 -0
- package/src/sandbox/selfhosted/op-testing.ts +468 -0
- package/src/sandbox/selfhosted/op-transport.ts +151 -0
- package/src/sandbox/selfhosted/retry-policy.ts +177 -0
- package/src/sandbox/selfhosted/session.ts +498 -55
- package/src/sandbox/selfhosted/testing.ts +60 -19
- package/src/sandbox/stream-port.ts +17 -5
- package/src/sandbox/stream-token.ts +8 -2
- package/src/sandbox/terminal-server.ts +18 -4
- package/src/sandbox-computer.ts +332 -70
- package/src/usage-telemetry.ts +73 -0
- package/dist/chunk-DYBF7ZEB.js.map +0 -1
- package/src/image-history.ts +0 -149
- package/src/screenshot-omitted-card.ts +0 -15
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
// API/worker pass a resume-by-id closure for the sibling box's lease).
|
|
20
20
|
|
|
21
21
|
import { buildSelfhostedBackendSession, type SelfhostedRelayConfig } from "../selfhosted/session";
|
|
22
|
+
import type { SelfhostedOpObserver } from "../selfhosted/op-observer";
|
|
22
23
|
import type { ControlRpc } from "../selfhosted/control-rpc";
|
|
23
24
|
import type {
|
|
24
25
|
ActivePointer,
|
|
@@ -60,8 +61,16 @@ export interface ActiveBackendResolverDeps {
|
|
|
60
61
|
* absent, a modal swap target surfaces as unsupported (the caller validated
|
|
61
62
|
* liveness, so this is the "modal swap not wired in this context" guard). */
|
|
62
63
|
establishModalTarget?: (sandbox: RoutableSandbox) => Promise<RoutableBackendSession>;
|
|
63
|
-
/**
|
|
64
|
+
/** The selfhosted CONTROL-op timeout (ping/fs/desktop/pty) for a swap/pin target.
|
|
65
|
+
* Absent ⇒ the session's 30s default. */
|
|
64
66
|
selfhostedTimeoutMs?: number;
|
|
67
|
+
/** The selfhosted EXEC process deadline for a swap/pin target, distinct from the
|
|
68
|
+
* control timeout (a swapped-to machine runs real commands too). Absent ⇒ falls
|
|
69
|
+
* back to the control timeout, as in the session leaf. */
|
|
70
|
+
selfhostedExecTimeoutMs?: number;
|
|
71
|
+
/** The per-op observer threaded into a selfhosted swap/pin target (out-of-band
|
|
72
|
+
* telemetry — metrics + machine.* events). Absent ⇒ no-op. */
|
|
73
|
+
selfhostedOnOp?: SelfhostedOpObserver;
|
|
65
74
|
/**
|
|
66
75
|
* The run's declared sandbox environment — the SAME `Record<string,string>` the
|
|
67
76
|
* worker turn threads into the agent's TARGET manifest (and into the group box at
|
|
@@ -86,15 +95,104 @@ export interface ActiveBackendResolverDeps {
|
|
|
86
95
|
* path (which always builds fresh — it has no pre-established turn session).
|
|
87
96
|
*/
|
|
88
97
|
pinnedSelfhosted?: { sandboxId: string; epoch: number; session: RoutableBackendSession };
|
|
98
|
+
/**
|
|
99
|
+
* Whether `defaultBackend` IS the session's home box — i.e. whether the null pointer
|
|
100
|
+
* (== home) may resolve to it. Defaults to TRUE (omitted): a genuine machine-HOME turn
|
|
101
|
+
* (home IS the machine) and a Modal-home turn established on its group box both pass/omit
|
|
102
|
+
* true, so null resolves to `defaultBackend` as before. Set explicitly FALSE only for a
|
|
103
|
+
* machine-primary turn of a Modal-HOME session (pinned to a machine, group box never
|
|
104
|
+
* established this turn): the null pointer then has nothing to resolve to THIS turn, so
|
|
105
|
+
* rather than silently keep serving the pinned machine as if the detach never happened,
|
|
106
|
+
* the null branch throws a typed `home_unavailable_this_turn` error — the detach's pointer
|
|
107
|
+
* commit stands and takes effect on the NEXT turn, which starts null and establishes the
|
|
108
|
+
* home box normally. Lazily establishing the home box mid-turn on such a clear is a
|
|
109
|
+
* deferred follow-up (issue #341); until then this makes the gap fail typed-and-specific,
|
|
110
|
+
* never silent.
|
|
111
|
+
*/
|
|
112
|
+
defaultIsHome?: boolean;
|
|
89
113
|
}
|
|
90
114
|
|
|
115
|
+
/** Why a persisted pointer / swap target cannot be turned into a live backend. A
|
|
116
|
+
* typed discriminant so callers (turn-start reconcile, the swap tool, future
|
|
117
|
+
* reports) can distinguish a stale pointer from an unaddressable enrollment from a
|
|
118
|
+
* backend the turn context simply cannot establish — instead of string-matching
|
|
119
|
+
* one opaque message (issue #341: "typed diagnostics that distinguish stale
|
|
120
|
+
* pointer, offline enrollment, unsupported backend context, transient failure").
|
|
121
|
+
* - `stale_pointer` — the pointed-at sandbox row is gone (deleted/orphaned).
|
|
122
|
+
* - `offline_enrollment` — a selfhosted target carries no enrollment/agent id to address.
|
|
123
|
+
* - `unsupported_backend_context` — a target no turn routing context can establish
|
|
124
|
+
* (a non-group Modal sibling; an unknown kind).
|
|
125
|
+
* - `transient_establishment` — a momentary establish failure worth a retry (reserved;
|
|
126
|
+
* control-plane timeouts surface as their own typed error).
|
|
127
|
+
* - `home_unavailable_this_turn` — the pointer was cleared to the session default (home)
|
|
128
|
+
* mid-turn, but this turn started pinned to a machine and
|
|
129
|
+
* never established the home box, so null has nothing to
|
|
130
|
+
* resolve to THIS turn (the detach takes effect next turn). */
|
|
131
|
+
export type BackendUnresolvableCode =
|
|
132
|
+
| "stale_pointer"
|
|
133
|
+
| "offline_enrollment"
|
|
134
|
+
| "unsupported_backend_context"
|
|
135
|
+
| "transient_establishment"
|
|
136
|
+
| "home_unavailable_this_turn";
|
|
137
|
+
|
|
91
138
|
/** Thrown when a swap target cannot be resolved (unknown sandbox, or a modal
|
|
92
|
-
* target with no establisher in this context). The caller maps it to a 409.
|
|
139
|
+
* target with no establisher in this context). The caller maps it to a 409.
|
|
140
|
+
* Carries a typed {@link BackendUnresolvableCode} in addition to the message. */
|
|
93
141
|
export class ActiveBackendUnresolvableError extends Error {
|
|
94
142
|
readonly name = "ActiveBackendUnresolvableError";
|
|
95
|
-
|
|
143
|
+
readonly code: BackendUnresolvableCode;
|
|
144
|
+
constructor(code: BackendUnresolvableCode, message: string) {
|
|
96
145
|
super(message);
|
|
146
|
+
this.code = code;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The outcome of {@link swapTargetEstablishability}. */
|
|
151
|
+
export type SwapTargetEstablishability =
|
|
152
|
+
| { ok: true }
|
|
153
|
+
| { ok: false; code: BackendUnresolvableCode; reason: string };
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* THE SINGLE SOURCE OF TRUTH for "can a turn's routing context ESTABLISH this swap
|
|
157
|
+
* target?" — shared by swap/seed ADMISSION (`fleet.resolveTarget`, before the CAS
|
|
158
|
+
* pointer commit) and turn-time ESTABLISHMENT (this resolver + `wrapTurnBoxWithRouting`).
|
|
159
|
+
* Admission MUST reject any target this predicate calls unestablishable, so the
|
|
160
|
+
* pointer is never committed to a backend no turn can resume (issue #341 Shape 1:
|
|
161
|
+
* a Modal-sibling swap was admitted + epoch-bumped, then every following op
|
|
162
|
+
* stranded because no context wires an establisher for it).
|
|
163
|
+
*
|
|
164
|
+
* - the session's OWN group box (`isSessionGroup`) is the null/home target and is
|
|
165
|
+
* always establishable (it IS the default backend);
|
|
166
|
+
* - a `selfhosted` machine target is establishable (`buildSelfhostedBackendSession`);
|
|
167
|
+
* admission's separate liveness probe — not this predicate — gates online-ness;
|
|
168
|
+
* - a NON-group `modal` target is NOT establishable: no turn context wires
|
|
169
|
+
* `establishModalTarget` (cross-group Modal resume-by-id is a future, billing-
|
|
170
|
+
* touching feature — sandbox-routing.ts). Wiring that establisher is the ONE
|
|
171
|
+
* toggle that flips this branch; keep admission and the resolver in lockstep.
|
|
172
|
+
*/
|
|
173
|
+
export function swapTargetEstablishability(target: {
|
|
174
|
+
kind: string;
|
|
175
|
+
isSessionGroup: boolean;
|
|
176
|
+
}): SwapTargetEstablishability {
|
|
177
|
+
if (target.isSessionGroup) {
|
|
178
|
+
return { ok: true };
|
|
179
|
+
}
|
|
180
|
+
if (target.kind === "selfhosted") {
|
|
181
|
+
return { ok: true };
|
|
182
|
+
}
|
|
183
|
+
if (target.kind === "modal") {
|
|
184
|
+
return {
|
|
185
|
+
ok: false,
|
|
186
|
+
code: "unsupported_backend_context",
|
|
187
|
+
reason:
|
|
188
|
+
"a Modal sandbox other than this session's own box cannot be established by a turn yet; swap back to the session default or attach a Connected Machine",
|
|
189
|
+
};
|
|
97
190
|
}
|
|
191
|
+
return {
|
|
192
|
+
ok: false,
|
|
193
|
+
code: "unsupported_backend_context",
|
|
194
|
+
reason: `a sandbox of kind "${target.kind}" cannot be established by a turn`,
|
|
195
|
+
};
|
|
98
196
|
}
|
|
99
197
|
|
|
100
198
|
/**
|
|
@@ -115,9 +213,29 @@ export function makeActiveBackendResolver(
|
|
|
115
213
|
deps: ActiveBackendResolverDeps,
|
|
116
214
|
): (pointer: ActivePointer) => Promise<ResolvedActiveBackend> {
|
|
117
215
|
return async (pointer: ActivePointer): Promise<ResolvedActiveBackend> => {
|
|
118
|
-
//
|
|
119
|
-
//
|
|
216
|
+
// NULL == the session's HOME backend (issue #341 invariant 1). `defaultBackend`
|
|
217
|
+
// is the already-established home the wiring handed us — the Modal group box for a
|
|
218
|
+
// Modal-home turn, or the machine itself for a machine-home turn. It is NEVER a
|
|
219
|
+
// swap target's SelfhostedSession: a clear-to-null must re-land on the home, never
|
|
220
|
+
// keep serving the machine a prior op swapped to. (The proxy caches by the FULL
|
|
221
|
+
// pointer tuple, so a clear-to-null — epoch-bumped OR a same-epoch FK null —
|
|
222
|
+
// invalidates the cached machine and re-enters this branch.) The lease owns the
|
|
223
|
+
// home box lifecycle; the proxy does not re-establish it.
|
|
120
224
|
if (pointer.activeSandboxId === null) {
|
|
225
|
+
// A machine-pinned turn (Modal-home session pinned to a machine) never
|
|
226
|
+
// established its home box, so a mid-turn clear-to-null has no home to resolve
|
|
227
|
+
// THIS turn. Fail typed-and-specific — the detach was accepted and its pointer
|
|
228
|
+
// commit STANDS, taking effect on the next turn (which starts null and
|
|
229
|
+
// establishes the home box) — instead of silently serving the pinned machine as
|
|
230
|
+
// if the clear never happened. Only an EXPLICIT `false` throws; omitted (the
|
|
231
|
+
// common path) resolves to the home default as before. (Lazily establishing the
|
|
232
|
+
// home box mid-turn on such a clear is a deferred follow-up; issue #341.)
|
|
233
|
+
if (deps.defaultIsHome === false) {
|
|
234
|
+
throw new ActiveBackendUnresolvableError(
|
|
235
|
+
"home_unavailable_this_turn",
|
|
236
|
+
"the active sandbox was detached to the session default (home), but this turn started pinned to a machine and established no home box; the detach is accepted and takes effect on the next turn — this turn has no active home box",
|
|
237
|
+
);
|
|
238
|
+
}
|
|
121
239
|
return { session: deps.defaultBackend, sandboxId: null, kind: deps.defaultKind };
|
|
122
240
|
}
|
|
123
241
|
|
|
@@ -129,16 +247,21 @@ export function makeActiveBackendResolver(
|
|
|
129
247
|
// to a fresh build fenced under the CURRENT epoch (the stale pinned instance is
|
|
130
248
|
// fenced at the old epoch and must not be reused).
|
|
131
249
|
if (
|
|
132
|
-
deps.pinnedSelfhosted
|
|
133
|
-
|
|
134
|
-
|
|
250
|
+
deps.pinnedSelfhosted &&
|
|
251
|
+
pointer.activeSandboxId === deps.pinnedSelfhosted.sandboxId &&
|
|
252
|
+
pointer.activeEpoch === deps.pinnedSelfhosted.epoch
|
|
135
253
|
) {
|
|
136
|
-
return {
|
|
254
|
+
return {
|
|
255
|
+
session: deps.pinnedSelfhosted.session,
|
|
256
|
+
sandboxId: pointer.activeSandboxId,
|
|
257
|
+
kind: "selfhosted",
|
|
258
|
+
};
|
|
137
259
|
}
|
|
138
260
|
|
|
139
261
|
const sandbox = await deps.getSandbox(pointer.activeSandboxId);
|
|
140
262
|
if (!sandbox) {
|
|
141
263
|
throw new ActiveBackendUnresolvableError(
|
|
264
|
+
"stale_pointer",
|
|
142
265
|
`active sandbox ${pointer.activeSandboxId} not found in workspace ${deps.workspaceId}`,
|
|
143
266
|
);
|
|
144
267
|
}
|
|
@@ -146,6 +269,7 @@ export function makeActiveBackendResolver(
|
|
|
146
269
|
if (sandbox.kind === "selfhosted") {
|
|
147
270
|
if (!sandbox.enrollmentId) {
|
|
148
271
|
throw new ActiveBackendUnresolvableError(
|
|
272
|
+
"offline_enrollment",
|
|
149
273
|
`selfhosted sandbox ${sandbox.id} has no enrollment (agent id) to address`,
|
|
150
274
|
);
|
|
151
275
|
}
|
|
@@ -161,6 +285,10 @@ export function makeActiveBackendResolver(
|
|
|
161
285
|
agentId: sandbox.enrollmentId,
|
|
162
286
|
epoch: pointer.activeEpoch,
|
|
163
287
|
...(deps.selfhostedTimeoutMs !== undefined ? { timeoutMs: deps.selfhostedTimeoutMs } : {}),
|
|
288
|
+
...(deps.selfhostedExecTimeoutMs !== undefined
|
|
289
|
+
? { execTimeoutMs: deps.selfhostedExecTimeoutMs }
|
|
290
|
+
: {}),
|
|
291
|
+
...(deps.selfhostedOnOp !== undefined ? { onOp: deps.selfhostedOnOp } : {}),
|
|
164
292
|
// The turn's declared environment → the session's manifest.environment, so
|
|
165
293
|
// the SDK's per-turn manifest-env delta is empty (no "cannot change manifest
|
|
166
294
|
// environment variables" throw on a pin-to-vm turn).
|
|
@@ -169,12 +297,17 @@ export function makeActiveBackendResolver(
|
|
|
169
297
|
// for this selfhosted backend. Absent/empty ⇒ the default workspace_root.
|
|
170
298
|
...(pointer.workingDir ? { workingDir: pointer.workingDir } : {}),
|
|
171
299
|
});
|
|
172
|
-
return {
|
|
300
|
+
return {
|
|
301
|
+
session: session as RoutableBackendSession,
|
|
302
|
+
sandboxId: sandbox.id,
|
|
303
|
+
kind: "selfhosted",
|
|
304
|
+
};
|
|
173
305
|
}
|
|
174
306
|
|
|
175
307
|
if (sandbox.kind === "modal") {
|
|
176
308
|
if (!deps.establishModalTarget) {
|
|
177
309
|
throw new ActiveBackendUnresolvableError(
|
|
310
|
+
"unsupported_backend_context",
|
|
178
311
|
`modal swap target ${sandbox.id} cannot be established in this context (no establisher wired)`,
|
|
179
312
|
);
|
|
180
313
|
}
|
|
@@ -183,6 +316,7 @@ export function makeActiveBackendResolver(
|
|
|
183
316
|
}
|
|
184
317
|
|
|
185
318
|
throw new ActiveBackendUnresolvableError(
|
|
319
|
+
"unsupported_backend_context",
|
|
186
320
|
`unsupported swap target kind "${sandbox.kind}" for sandbox ${sandbox.id}`,
|
|
187
321
|
);
|
|
188
322
|
};
|
|
@@ -28,6 +28,8 @@
|
|
|
28
28
|
// `@opengeni/db`.
|
|
29
29
|
|
|
30
30
|
import type { ExposedPortEndpoint } from "../stream-port";
|
|
31
|
+
import { SelfhostedControlError } from "../selfhosted/control-rpc";
|
|
32
|
+
import { renderSelfhostedFault } from "../selfhosted/fault-rendering";
|
|
31
33
|
|
|
32
34
|
/** The per-session active-sandbox pointer the proxy re-reads on every op. Mirror
|
|
33
35
|
* of `@opengeni/db`'s `ActiveSandboxPointer` (structural, so the leaf does not
|
|
@@ -74,7 +76,13 @@ export interface RoutableBackendSession {
|
|
|
74
76
|
// `event` is kept `unknown` (mirroring the interface's structural style + avoiding
|
|
75
77
|
// a proto import into the leaf); the SelfhostedSession takes `DesktopInputRequest["event"]`.
|
|
76
78
|
desktopInput?(event: unknown): Promise<void>;
|
|
77
|
-
screenshot?(): Promise<{
|
|
79
|
+
screenshot?(): Promise<{
|
|
80
|
+
png: Uint8Array;
|
|
81
|
+
width: number;
|
|
82
|
+
height: number;
|
|
83
|
+
nativeWidth: number;
|
|
84
|
+
nativeHeight: number;
|
|
85
|
+
}>;
|
|
78
86
|
}
|
|
79
87
|
|
|
80
88
|
/** The resolved active backend for an epoch: the live session + the sandbox id it
|
|
@@ -148,10 +156,12 @@ function isFenceError(error: unknown): boolean {
|
|
|
148
156
|
if ((error as { fenced?: unknown }).fenced === true) {
|
|
149
157
|
return true;
|
|
150
158
|
}
|
|
151
|
-
const name =
|
|
152
|
-
|
|
159
|
+
const name =
|
|
160
|
+
typeof (error as { name?: unknown }).name === "string" ? (error as { name: string }).name : "";
|
|
161
|
+
const message =
|
|
162
|
+
error instanceof Error ? error.message : String((error as { message?: unknown }).message ?? "");
|
|
153
163
|
const haystack = `${name} ${message}`.toLowerCase();
|
|
154
|
-
return haystack.includes("fenced") || haystack.includes("epoch") && haystack.includes("super");
|
|
164
|
+
return haystack.includes("fenced") || (haystack.includes("epoch") && haystack.includes("super"));
|
|
155
165
|
}
|
|
156
166
|
|
|
157
167
|
/**
|
|
@@ -172,9 +182,17 @@ function isFenceError(error: unknown): boolean {
|
|
|
172
182
|
export class RoutingSandboxSession implements RoutableBackendSession {
|
|
173
183
|
private readonly deps: RoutingSandboxSessionDeps;
|
|
174
184
|
private readonly maxFenceRetries: number;
|
|
175
|
-
// The
|
|
176
|
-
//
|
|
185
|
+
// The resolved-backend cache. Keyed by the FULL pointer tuple
|
|
186
|
+
// `(activeEpoch, activeSandboxId)` — NOT the epoch alone. A swap bumps the epoch,
|
|
187
|
+
// but a pointer can also change its target id WITHOUT an epoch bump: the
|
|
188
|
+
// `sessions.active_sandbox_id` FK is `ON DELETE SET NULL`, so a cascade that
|
|
189
|
+
// deletes the pointed-at sandbox row nulls the id at the SAME epoch. Keying on the
|
|
190
|
+
// epoch alone would then keep serving the deleted/stale backend for that epoch
|
|
191
|
+
// (issue #341 §5.2 — a swap-free route to the Shape-3 symptom). Keying on the tuple
|
|
192
|
+
// makes any target change — epoch-bumped OR not — invalidate the cache so the next
|
|
193
|
+
// op re-resolves (and, for a null id, re-resolves the session HOME).
|
|
177
194
|
private cachedEpoch: number | undefined;
|
|
195
|
+
private cachedSandboxId: string | null | undefined;
|
|
178
196
|
private cached: ResolvedActiveBackend | undefined;
|
|
179
197
|
// The last-resolved backend, exposed via the `state` getter (a method-free read
|
|
180
198
|
// of the active backend's `state`). Updated on every resolve.
|
|
@@ -189,7 +207,13 @@ export class RoutingSandboxSession implements RoutableBackendSession {
|
|
|
189
207
|
// that cannot serve them. So the constructor assigns them ONLY when the
|
|
190
208
|
// construction-time default backend actually implements the native surface (below).
|
|
191
209
|
desktopInput?: (event: unknown) => Promise<void>;
|
|
192
|
-
screenshot?: () => Promise<{
|
|
210
|
+
screenshot?: () => Promise<{
|
|
211
|
+
png: Uint8Array;
|
|
212
|
+
width: number;
|
|
213
|
+
height: number;
|
|
214
|
+
nativeWidth: number;
|
|
215
|
+
nativeHeight: number;
|
|
216
|
+
}>;
|
|
193
217
|
|
|
194
218
|
constructor(deps: RoutingSandboxSessionDeps) {
|
|
195
219
|
this.deps = deps;
|
|
@@ -252,16 +276,34 @@ export class RoutingSandboxSession implements RoutableBackendSession {
|
|
|
252
276
|
*/
|
|
253
277
|
private async resolve(): Promise<ResolvedActiveBackend> {
|
|
254
278
|
const pointer = await this.deps.readPointer();
|
|
255
|
-
if (
|
|
279
|
+
if (
|
|
280
|
+
this.cachedEpoch === pointer.activeEpoch &&
|
|
281
|
+
this.cachedSandboxId === pointer.activeSandboxId &&
|
|
282
|
+
this.cached
|
|
283
|
+
) {
|
|
256
284
|
return this.cached;
|
|
257
285
|
}
|
|
258
286
|
const fromEpoch = this.cachedEpoch ?? pointer.activeEpoch;
|
|
259
287
|
const resolved = await this.deps.resolveActiveBackend(pointer);
|
|
288
|
+
// Re-entrancy guard: a resolver that returns THIS proxy as the active backend
|
|
289
|
+
// makes every op (exec/readFile/…) dispatch back into resolve() -> the same
|
|
290
|
+
// backend -> forever (a silent async infinite recursion that HANGS the turn,
|
|
291
|
+
// not a stack overflow). Fail loud instead — a wiring bug must surface as a
|
|
292
|
+
// legible error, never a hung turn.
|
|
293
|
+
if ((resolved.session as unknown) === this) {
|
|
294
|
+
throw new Error(
|
|
295
|
+
"RoutingSandboxSession.resolveActiveBackend returned the proxy itself as the active backend (re-entrancy) — the resolver must return the underlying box session, not the routing proxy.",
|
|
296
|
+
);
|
|
297
|
+
}
|
|
260
298
|
this.cachedEpoch = pointer.activeEpoch;
|
|
299
|
+
this.cachedSandboxId = pointer.activeSandboxId;
|
|
261
300
|
this.cached = resolved;
|
|
262
301
|
this.lastResolved = resolved;
|
|
263
302
|
this.deps.onTransition?.({
|
|
264
|
-
type:
|
|
303
|
+
type:
|
|
304
|
+
this.cachedEpoch !== undefined && fromEpoch !== pointer.activeEpoch
|
|
305
|
+
? "epoch-changed"
|
|
306
|
+
: "resolved",
|
|
265
307
|
fromEpoch,
|
|
266
308
|
toEpoch: pointer.activeEpoch,
|
|
267
309
|
sandboxId: resolved.sandboxId,
|
|
@@ -281,7 +323,10 @@ export class RoutingSandboxSession implements RoutableBackendSession {
|
|
|
281
323
|
* A non-fence error propagates immediately (it is a real op failure, not a swap
|
|
282
324
|
* race).
|
|
283
325
|
*/
|
|
284
|
-
private async dispatch<T>(
|
|
326
|
+
private async dispatch<T>(
|
|
327
|
+
op: string,
|
|
328
|
+
fn: (session: RoutableBackendSession) => Promise<T>,
|
|
329
|
+
): Promise<T> {
|
|
285
330
|
let attempt = 0;
|
|
286
331
|
let lastError: unknown;
|
|
287
332
|
while (attempt <= this.maxFenceRetries) {
|
|
@@ -297,6 +342,7 @@ export class RoutingSandboxSession implements RoutableBackendSession {
|
|
|
297
342
|
// active sandbox (the fenced-retry role). Bounded by maxFenceRetries.
|
|
298
343
|
lastError = error;
|
|
299
344
|
this.cachedEpoch = undefined;
|
|
345
|
+
this.cachedSandboxId = undefined;
|
|
300
346
|
this.cached = undefined;
|
|
301
347
|
this.deps.onTransition?.({
|
|
302
348
|
type: "fenced-retry",
|
|
@@ -313,6 +359,31 @@ export class RoutingSandboxSession implements RoutableBackendSession {
|
|
|
313
359
|
throw lastError ?? new Error(`routing op "${op}" exhausted fence retries`);
|
|
314
360
|
}
|
|
315
361
|
|
|
362
|
+
/**
|
|
363
|
+
* The failure-visibility boundary for the `exec_command` SDK capability tool — the
|
|
364
|
+
* dominant fault surface, and the one whose thrown `SelfhostedControlError` reaches
|
|
365
|
+
* the model wrapped by the SDK's generic tool-error function as
|
|
366
|
+
* "…Please try again. Error: …" — actively wrong for a machine that is offline, a
|
|
367
|
+
* consent that is not granted, or an oversized reply. Since the SDK closure-captures
|
|
368
|
+
* its `errorFunction` (there is no seam to attach one to its internally-built tools),
|
|
369
|
+
* we render the fault into the doctrine's four fields HERE and return it as the
|
|
370
|
+
* tool's string result — legible, in-band, and free of the misleading wrapper.
|
|
371
|
+
* (`apply_patch` already surfaces `error.message` via the SDK's own catch; the
|
|
372
|
+
* skills / `view_image` tools consume their session methods internally, so they are
|
|
373
|
+
* rendered elsewhere or left to their own SDK renderers — not this string boundary.)
|
|
374
|
+
*
|
|
375
|
+
* A FENCE error is re-thrown, NEVER rendered: `dispatch` already retries it against
|
|
376
|
+
* a re-resolved backend, and a fence that escapes retries is a routing condition the
|
|
377
|
+
* turn handles, not a model-facing fault. Any non-selfhosted error (a Modal fault, a
|
|
378
|
+
* `RoutingUnsupportedError`) is re-thrown unchanged.
|
|
379
|
+
*/
|
|
380
|
+
private renderSelfhostedFaultOrThrow(error: unknown): string {
|
|
381
|
+
if (error instanceof SelfhostedControlError && !error.fenced) {
|
|
382
|
+
return renderSelfhostedFault(error);
|
|
383
|
+
}
|
|
384
|
+
throw error;
|
|
385
|
+
}
|
|
386
|
+
|
|
316
387
|
// ── The forwarded structural surface ──────────────────────────────────────
|
|
317
388
|
// Every method is PRESENT on the proxy (the SDK binds presence once) and
|
|
318
389
|
// dispatches to the active backend at call-time. A missing backend method
|
|
@@ -332,16 +403,22 @@ export class RoutingSandboxSession implements RoutableBackendSession {
|
|
|
332
403
|
}
|
|
333
404
|
|
|
334
405
|
async execCommand(args: unknown): Promise<string> {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
406
|
+
try {
|
|
407
|
+
return await this.dispatch("execCommand", async (s) => {
|
|
408
|
+
if (s.execCommand) {
|
|
409
|
+
return s.execCommand(args);
|
|
410
|
+
}
|
|
411
|
+
if (s.exec) {
|
|
412
|
+
const r = (await s.exec(args)) as { stdout?: string; output?: string };
|
|
413
|
+
return r.stdout ?? r.output ?? "";
|
|
414
|
+
}
|
|
415
|
+
throw new RoutingUnsupportedError("execCommand", this.cached?.kind ?? "unknown");
|
|
416
|
+
});
|
|
417
|
+
} catch (error) {
|
|
418
|
+
// Render a terminal selfhosted fault as the tool's result (four fields, correct
|
|
419
|
+
// verdict) instead of letting the SDK mislabel it "Please try again".
|
|
420
|
+
return this.renderSelfhostedFaultOrThrow(error);
|
|
421
|
+
}
|
|
345
422
|
}
|
|
346
423
|
|
|
347
424
|
async writeStdin(args: unknown): Promise<string> {
|
|
@@ -417,12 +494,39 @@ export class RoutingSandboxSession implements RoutableBackendSession {
|
|
|
417
494
|
return Boolean(s?.supportsPty?.());
|
|
418
495
|
}
|
|
419
496
|
|
|
420
|
-
/** createEditor is a synchronous factory in the SDK surface
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
497
|
+
/** createEditor is a synchronous factory in the SDK surface. The SDK's filesystem
|
|
498
|
+
* capability calls it ONCE at tool-BIND time — `FilesystemCapability.tools()`,
|
|
499
|
+
* every turn, before any tool runs — and throws "Filesystem sandbox sessions must
|
|
500
|
+
* provide createEditor()" if it returns falsy. When a backend is already resolved
|
|
501
|
+
* (eager/selfhosted routing) we bind to its editor directly, byte-for-byte as
|
|
502
|
+
* before. But under LAZY provisioning the backend is not established yet
|
|
503
|
+
* (defaultResolved is the synthetic unprovisioned session with no editor), so a
|
|
504
|
+
* direct delegate returns undefined and every lazy turn would die at bind. Return a
|
|
505
|
+
* LAZY EDITOR PROXY instead: a non-null editor whose async ops resolve the active
|
|
506
|
+
* backend (establishing the box on first use, via `dispatch`) and delegate to its
|
|
507
|
+
* real editor — mirroring how this proxy defers exec/readFile. */
|
|
424
508
|
createEditor(runAs?: string): unknown {
|
|
425
|
-
|
|
509
|
+
const eager = (this.lastResolved ?? this.deps.defaultResolved)?.session.createEditor?.(runAs);
|
|
510
|
+
if (eager) {
|
|
511
|
+
return eager;
|
|
512
|
+
}
|
|
513
|
+
const op =
|
|
514
|
+
(name: "createFile" | "updateFile" | "deleteFile") =>
|
|
515
|
+
(operation: unknown, context?: unknown): Promise<unknown> =>
|
|
516
|
+
this.dispatch(`editor.${name}`, async (s) => {
|
|
517
|
+
const editor = s.createEditor?.(runAs) as
|
|
518
|
+
| Record<string, (operation: unknown, context?: unknown) => Promise<unknown>>
|
|
519
|
+
| undefined;
|
|
520
|
+
if (!editor?.[name]) {
|
|
521
|
+
throw new RoutingUnsupportedError(`editor.${name}`, this.cached?.kind ?? "unknown");
|
|
522
|
+
}
|
|
523
|
+
return editor[name](operation, context);
|
|
524
|
+
});
|
|
525
|
+
return {
|
|
526
|
+
createFile: op("createFile"),
|
|
527
|
+
updateFile: op("updateFile"),
|
|
528
|
+
deleteFile: op("deleteFile"),
|
|
529
|
+
};
|
|
426
530
|
}
|
|
427
531
|
|
|
428
532
|
async resolveExposedPort(port: number): Promise<ExposedPortEndpoint> {
|
package/src/sandbox/select.ts
CHANGED
|
@@ -165,7 +165,14 @@ export function negotiateCapabilities(ctx: NegotiationContext): SessionCapabilit
|
|
|
165
165
|
|
|
166
166
|
const fileSystem = (() => {
|
|
167
167
|
if (osReason) {
|
|
168
|
-
return {
|
|
168
|
+
return {
|
|
169
|
+
available: false,
|
|
170
|
+
readOnly: true,
|
|
171
|
+
root: descriptor.workspaceRoot,
|
|
172
|
+
pathSep: "/" as const,
|
|
173
|
+
treeMode: "lazy" as const,
|
|
174
|
+
reason: osReason,
|
|
175
|
+
};
|
|
169
176
|
}
|
|
170
177
|
const cap = descriptor.capabilities.FileSystem;
|
|
171
178
|
return {
|
|
@@ -181,10 +188,26 @@ export function negotiateCapabilities(ctx: NegotiationContext): SessionCapabilit
|
|
|
181
188
|
const terminal = (() => {
|
|
182
189
|
const cap = descriptor.capabilities.Terminal;
|
|
183
190
|
if (osReason) {
|
|
184
|
-
return {
|
|
191
|
+
return {
|
|
192
|
+
transport: null,
|
|
193
|
+
ptyCapable: false,
|
|
194
|
+
shell: "/bin/bash",
|
|
195
|
+
url: null,
|
|
196
|
+
token: null,
|
|
197
|
+
expiresAt: null,
|
|
198
|
+
reason: osReason,
|
|
199
|
+
};
|
|
185
200
|
}
|
|
186
201
|
if (!cap.available) {
|
|
187
|
-
return {
|
|
202
|
+
return {
|
|
203
|
+
transport: null,
|
|
204
|
+
ptyCapable: false,
|
|
205
|
+
shell: "/bin/bash",
|
|
206
|
+
url: null,
|
|
207
|
+
token: null,
|
|
208
|
+
expiresAt: null,
|
|
209
|
+
reason: "backend_unsupported" as const,
|
|
210
|
+
};
|
|
188
211
|
}
|
|
189
212
|
// The REAL PTY (ttyd pty-ws) rides the SAME tunnel as the desktop, so it is
|
|
190
213
|
// gated identically: a real-PTY backend (cap.pty), the terminal policy toggle
|
|
@@ -231,7 +254,11 @@ export function negotiateCapabilities(ctx: NegotiationContext): SessionCapabilit
|
|
|
231
254
|
if (osReason) {
|
|
232
255
|
return { available: false, repos: [], reason: osReason };
|
|
233
256
|
}
|
|
234
|
-
return {
|
|
257
|
+
return {
|
|
258
|
+
available: cap.available,
|
|
259
|
+
repos: [],
|
|
260
|
+
reason: cap.available ? null : ("backend_unsupported" as const),
|
|
261
|
+
};
|
|
235
262
|
})();
|
|
236
263
|
|
|
237
264
|
const desktop = (() => {
|
|
@@ -317,14 +344,32 @@ export function negotiateCapabilities(ctx: NegotiationContext): SessionCapabilit
|
|
|
317
344
|
const recording = (() => {
|
|
318
345
|
const cap = descriptor.capabilities.Recording;
|
|
319
346
|
if (osReason) {
|
|
320
|
-
return {
|
|
347
|
+
return {
|
|
348
|
+
available: false,
|
|
349
|
+
modes: [] as ("manual" | "on-turn" | "on-verify")[],
|
|
350
|
+
codecs: [] as ("h264-mp4" | "vp9-webm")[],
|
|
351
|
+
reason: osReason,
|
|
352
|
+
};
|
|
321
353
|
}
|
|
322
354
|
if (!cap.available) {
|
|
323
|
-
return {
|
|
355
|
+
return {
|
|
356
|
+
available: false,
|
|
357
|
+
modes: [] as ("manual" | "on-turn" | "on-verify")[],
|
|
358
|
+
codecs: [] as ("h264-mp4" | "vp9-webm")[],
|
|
359
|
+
reason:
|
|
360
|
+
descriptor.tier === "headless"
|
|
361
|
+
? ("tier_headless" as const)
|
|
362
|
+
: ("backend_unsupported" as const),
|
|
363
|
+
};
|
|
324
364
|
}
|
|
325
365
|
// Recording feasibility tracks desktop; policy-gate it the same way.
|
|
326
366
|
if (!ctx.desktopEnabled) {
|
|
327
|
-
return {
|
|
367
|
+
return {
|
|
368
|
+
available: false,
|
|
369
|
+
modes: [] as ("manual" | "on-turn" | "on-verify")[],
|
|
370
|
+
codecs: [] as ("h264-mp4" | "vp9-webm")[],
|
|
371
|
+
reason: "disabled_by_policy" as const,
|
|
372
|
+
};
|
|
328
373
|
}
|
|
329
374
|
return {
|
|
330
375
|
available: true,
|
|
@@ -345,7 +390,14 @@ export function negotiateCapabilities(ctx: NegotiationContext): SessionCapabilit
|
|
|
345
390
|
return { available: false, readOnly, reason: osReason };
|
|
346
391
|
}
|
|
347
392
|
if (!desktopCapable) {
|
|
348
|
-
return {
|
|
393
|
+
return {
|
|
394
|
+
available: false,
|
|
395
|
+
readOnly,
|
|
396
|
+
reason:
|
|
397
|
+
descriptor.tier === "headless"
|
|
398
|
+
? ("tier_headless" as const)
|
|
399
|
+
: ("backend_unsupported" as const),
|
|
400
|
+
};
|
|
349
401
|
}
|
|
350
402
|
if (!ctx.desktopEnabled || ctx.computerUseEnabled === false) {
|
|
351
403
|
return { available: false, readOnly, reason: "disabled_by_policy" as const };
|