@finalai/daytona-agent-provider 0.1.10 → 0.4.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/README.md +19 -0
- package/dist/modules/client/sandboxes.mjs +10 -2
- package/dist/modules/client/sandboxes.mjs.map +2 -2
- package/dist/modules/client/types.d.mts +4 -0
- package/dist/modules/client/types.mjs.map +2 -2
- package/dist/modules/sandboxes/daytona.d.mts +13 -1
- package/dist/modules/sandboxes/daytona.mjs +705 -83
- package/dist/modules/sandboxes/daytona.mjs.map +3 -3
- package/dist/modules/sandboxes/session.d.mts +1 -0
- package/dist/modules/sandboxes/session.mjs +52 -10
- package/dist/modules/sandboxes/session.mjs.map +2 -2
- package/dist/modules/testing/fake-daytona.mjs +10 -1
- package/dist/modules/testing/fake-daytona.mjs.map +2 -2
- package/package.json +2 -2
|
@@ -2,6 +2,8 @@ import { lookup } from "node:dns/promises";
|
|
|
2
2
|
import { SandboxAccess } from "@finalai/agent-provider";
|
|
3
3
|
import {
|
|
4
4
|
defineSandboxProvider,
|
|
5
|
+
attachSandboxNetworking,
|
|
6
|
+
mapSandboxHandle,
|
|
5
7
|
materializeSandboxSeeds,
|
|
6
8
|
NetworkPolicy,
|
|
7
9
|
PLATFORM_ENVIRONMENT_NAMES,
|
|
@@ -12,6 +14,7 @@ import {
|
|
|
12
14
|
sandboxSeedFiles,
|
|
13
15
|
sandboxSeedFilesForStart,
|
|
14
16
|
sandboxSeedIdentity,
|
|
17
|
+
sandboxWorkspaceId,
|
|
15
18
|
SandboxWorkspace,
|
|
16
19
|
withPlatformEgressHosts,
|
|
17
20
|
writePlatformFiles
|
|
@@ -45,6 +48,10 @@ import {
|
|
|
45
48
|
DAYTONA_SANDBOX_SPECIFIER,
|
|
46
49
|
DaytonaSessionEndPolicy as DaytonaSessionEndPolicy2
|
|
47
50
|
} from "./environment.mjs";
|
|
51
|
+
const daytonaWorkspaceName = async (options) => {
|
|
52
|
+
const scope = options.conversationWorkspace && options.conversationId != null ? `conversation:${options.conversationId}:${options.target}` : `binding:${options.bindingId}`;
|
|
53
|
+
return `final-${(await sha256Hex(`${options.organizationId}:${scope}`)).slice(0, 32)}`;
|
|
54
|
+
};
|
|
48
55
|
var DaytonaSandboxErrorCode = /* @__PURE__ */ ((DaytonaSandboxErrorCode2) => {
|
|
49
56
|
DaytonaSandboxErrorCode2["CredentialMissing"] = "credential_missing";
|
|
50
57
|
DaytonaSandboxErrorCode2["InvalidEnvironment"] = "invalid_environment";
|
|
@@ -69,7 +76,10 @@ const stateSchema = z.object({
|
|
|
69
76
|
workingDirectory: z.string(),
|
|
70
77
|
access: z.nativeEnum(SandboxAccess),
|
|
71
78
|
workspace: z.nativeEnum(SandboxWorkspace),
|
|
72
|
-
onSessionEnd: z.nativeEnum(DaytonaSessionEndPolicy)
|
|
79
|
+
onSessionEnd: z.nativeEnum(DaytonaSessionEndPolicy),
|
|
80
|
+
setupPending: z.boolean().default(false),
|
|
81
|
+
networkingManaged: z.boolean().default(false),
|
|
82
|
+
workspaceId: z.string().optional()
|
|
73
83
|
});
|
|
74
84
|
const sha256Hex = async (text) => {
|
|
75
85
|
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
@@ -82,6 +92,15 @@ const STOPPED_STATES = /* @__PURE__ */ new Set([
|
|
|
82
92
|
DaytonaSandboxState.Archived,
|
|
83
93
|
DaytonaSandboxState.Paused
|
|
84
94
|
]);
|
|
95
|
+
const BOOTING_STATES = /* @__PURE__ */ new Set([
|
|
96
|
+
DaytonaSandboxState.Creating,
|
|
97
|
+
DaytonaSandboxState.Starting,
|
|
98
|
+
DaytonaSandboxState.Restoring,
|
|
99
|
+
DaytonaSandboxState.Resuming,
|
|
100
|
+
DaytonaSandboxState.PendingBuild,
|
|
101
|
+
DaytonaSandboxState.BuildingSnapshot,
|
|
102
|
+
DaytonaSandboxState.PullingSnapshot
|
|
103
|
+
]);
|
|
85
104
|
const SETTLING_STATES = /* @__PURE__ */ new Map([
|
|
86
105
|
[DaytonaSandboxState.Stopping, DaytonaSandboxState.Stopped],
|
|
87
106
|
[DaytonaSandboxState.Archiving, DaytonaSandboxState.Archived],
|
|
@@ -181,6 +200,10 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
181
200
|
}
|
|
182
201
|
return current;
|
|
183
202
|
};
|
|
203
|
+
const settledSandbox = async (client, sandbox) => {
|
|
204
|
+
const state = SETTLING_STATES.get(sandbox.state);
|
|
205
|
+
return state == null ? sandbox : waitForSandbox(client, sandbox, state);
|
|
206
|
+
};
|
|
184
207
|
const running = async (client, sandbox) => {
|
|
185
208
|
let current = sandbox;
|
|
186
209
|
const settled = SETTLING_STATES.get(current.state);
|
|
@@ -230,6 +253,51 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
230
253
|
args.log?.(`Daytona snapshot ${name} is ready`);
|
|
231
254
|
return name;
|
|
232
255
|
};
|
|
256
|
+
const unsupportedBootFences = /* @__PURE__ */ new WeakMap();
|
|
257
|
+
const fenceUnavailable = (client) => (unsupportedBootFences.get(client) ?? 0) > now();
|
|
258
|
+
const unsupportedNetworkSettings = (result) => !result.ok && result.error === DaytonaErrorCode.BadRequest && result.statusCode === 400 && result.details?.some(
|
|
259
|
+
({ message }) => message?.includes("Network access is restricted") === true
|
|
260
|
+
) === true;
|
|
261
|
+
const warnUnfenced = (id, reason = "provider-network-settings-unsupported") => log("workspace_egress.boot_unfenced", {
|
|
262
|
+
provider: "daytona",
|
|
263
|
+
sandboxId: id,
|
|
264
|
+
reason,
|
|
265
|
+
guarantee: "agent-commands-after-initialization"
|
|
266
|
+
});
|
|
267
|
+
const fenceSandbox = async (client, id, warnWindow = true) => {
|
|
268
|
+
if (config.workspaceNetworkingBootFence === false && warnWindow) {
|
|
269
|
+
warnUnfenced(id);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (config.workspaceNetworkingBootFence !== true && fenceUnavailable(client)) {
|
|
273
|
+
if (warnWindow) warnUnfenced(id);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const current = unwrap(await client.getSandbox(id));
|
|
277
|
+
if (current != null && current.state !== DaytonaSandboxState.Started) {
|
|
278
|
+
if (config.workspaceNetworkingBootFence === true && warnWindow)
|
|
279
|
+
throw new Error("Daytona cannot verify network enforcement before a stopped sandbox boots");
|
|
280
|
+
if (warnWindow) warnUnfenced(id, "provider-stopped-boot-enforcement-unverified");
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const result = await client.updateNetworkSettings(id, {
|
|
284
|
+
networkBlockAll: true,
|
|
285
|
+
domainAllowList: "",
|
|
286
|
+
networkAllowList: ""
|
|
287
|
+
});
|
|
288
|
+
if (config.workspaceNetworkingBootFence !== true && unsupportedNetworkSettings(result)) {
|
|
289
|
+
unsupportedBootFences.set(client, now() + 15 * 6e4);
|
|
290
|
+
if (warnWindow) warnUnfenced(id);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
unwrap(result);
|
|
294
|
+
unsupportedBootFences.delete(client);
|
|
295
|
+
if (unwrap(await client.getSandbox(id))?.networkBlockAll !== true)
|
|
296
|
+
throw new Error("Daytona did not confirm its provider network fence");
|
|
297
|
+
};
|
|
298
|
+
const managedWorkspaces = /* @__PURE__ */ new Set();
|
|
299
|
+
const managedWorkspaceIdentities = /* @__PURE__ */ new Map();
|
|
300
|
+
const preparedWorkspaces = /* @__PURE__ */ new Set();
|
|
233
301
|
const environment = (rawOptions) => {
|
|
234
302
|
const parsed = daytonaEnvironmentSchema.safeParse(rawOptions ?? {});
|
|
235
303
|
if (!parsed.success) {
|
|
@@ -241,6 +309,145 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
241
309
|
const options = parsed.data;
|
|
242
310
|
const onSessionEnd = sessionEndPolicyOf(options);
|
|
243
311
|
const specPolicy = options.networkPolicy == null ? null : typeof options.networkPolicy === "string" ? { mode: options.networkPolicy } : { mode: options.networkPolicy.mode, allow: options.networkPolicy.allow };
|
|
312
|
+
const recoveryContext = (ctx, state) => {
|
|
313
|
+
const managed = state.networkingManaged || managedWorkspaces.has(ctx.session.organizationId + ":" + state.sandboxName);
|
|
314
|
+
const retainedIdentity = managedWorkspaceIdentities.get(ctx.session.organizationId + ":" + state.sandboxName) ?? state.workspaceId;
|
|
315
|
+
if (managed && retainedIdentity != null && ctx.session.workspaceId != null && retainedIdentity !== ctx.session.workspaceId)
|
|
316
|
+
throw new Error("Daytona workspace identity does not match retained managed state");
|
|
317
|
+
return {
|
|
318
|
+
...ctx,
|
|
319
|
+
session: {
|
|
320
|
+
...ctx.session,
|
|
321
|
+
workspaceId: ctx.session.workspaceId ?? retainedIdentity ?? sandboxWorkspaceId({ session: ctx.session, workspace: state.workspace })
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
};
|
|
325
|
+
const needsWorkspaceNetworking = async (ctx, name, query = {}) => {
|
|
326
|
+
if (config.networking == null) {
|
|
327
|
+
if (name != null && managedWorkspaces.has(ctx.session.organizationId + ":" + name))
|
|
328
|
+
throw new Error(
|
|
329
|
+
"Workspace networking control is required for this managed Daytona workspace"
|
|
330
|
+
);
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
let enabled = await config.networking.enabled({
|
|
334
|
+
ctx: {
|
|
335
|
+
platformEgressHosts: ctx.platformEgressHosts,
|
|
336
|
+
platformNetworkPolicy: ctx.platformNetworkPolicy,
|
|
337
|
+
organizationId: ctx.session.organizationId,
|
|
338
|
+
actingUser: ctx.actingUser,
|
|
339
|
+
bindings: ctx.bindings
|
|
340
|
+
},
|
|
341
|
+
workspaceId: sandboxWorkspaceId({ session: ctx.session, workspace: options.workspace }),
|
|
342
|
+
...query
|
|
343
|
+
}) ?? false;
|
|
344
|
+
if (name != null) {
|
|
345
|
+
const key = ctx.session.organizationId + ":" + name;
|
|
346
|
+
enabled ||= managedWorkspaces.has(key);
|
|
347
|
+
if (enabled) {
|
|
348
|
+
const workspaceId = sandboxWorkspaceId({
|
|
349
|
+
session: ctx.session,
|
|
350
|
+
workspace: options.workspace
|
|
351
|
+
});
|
|
352
|
+
const retained = managedWorkspaceIdentities.get(key);
|
|
353
|
+
if (retained != null && retained !== workspaceId)
|
|
354
|
+
throw new Error("Daytona workspace identity does not match retained managed state");
|
|
355
|
+
managedWorkspaces.add(key);
|
|
356
|
+
managedWorkspaceIdentities.set(key, workspaceId);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (enabled)
|
|
360
|
+
config.networking.validateWorkspaceId?.(
|
|
361
|
+
sandboxWorkspaceId({ session: ctx.session, workspace: options.workspace })
|
|
362
|
+
);
|
|
363
|
+
return enabled;
|
|
364
|
+
};
|
|
365
|
+
const preserveBeforeBoot = async (ctx, name, policy) => {
|
|
366
|
+
networkSettingsFor(policy);
|
|
367
|
+
if (config.networking == null) throw new Error("Missing workspace policy preparation");
|
|
368
|
+
const retained = await config.networking.prepare({
|
|
369
|
+
ctx: {
|
|
370
|
+
platformEgressHosts: ctx.platformEgressHosts,
|
|
371
|
+
platformNetworkPolicy: ctx.platformNetworkPolicy,
|
|
372
|
+
organizationId: ctx.session.organizationId,
|
|
373
|
+
actingUser: ctx.actingUser,
|
|
374
|
+
bindings: ctx.bindings
|
|
375
|
+
},
|
|
376
|
+
workspaceId: sandboxWorkspaceId({ session: ctx.session, workspace: options.workspace }),
|
|
377
|
+
policy
|
|
378
|
+
});
|
|
379
|
+
await reportPlatformNetworkPolicy(ctx, retained ?? policy);
|
|
380
|
+
preparedWorkspaces.add(ctx.session.organizationId + ":" + name);
|
|
381
|
+
};
|
|
382
|
+
const teardown = async (ctx, name, finish) => {
|
|
383
|
+
const failures = [];
|
|
384
|
+
let managed = false;
|
|
385
|
+
let queryFailed = false;
|
|
386
|
+
try {
|
|
387
|
+
managed = await needsWorkspaceNetworking(ctx, name, { teardown: true });
|
|
388
|
+
} catch (cause) {
|
|
389
|
+
failures.push(cause);
|
|
390
|
+
queryFailed = true;
|
|
391
|
+
}
|
|
392
|
+
let mayFence = managed || managedWorkspaces.has(ctx.session.organizationId + ":" + name);
|
|
393
|
+
if (mayFence) {
|
|
394
|
+
try {
|
|
395
|
+
const client = await clientFor(ctx.session.organizationId);
|
|
396
|
+
const sandbox = unwrap(await client.getSandbox(name));
|
|
397
|
+
if (sandbox != null) await preserveBeforeBoot(ctx, name, currentNetworkPolicy(sandbox));
|
|
398
|
+
} catch (cause) {
|
|
399
|
+
failures.push(cause);
|
|
400
|
+
mayFence = preparedWorkspaces.has(ctx.session.organizationId + ":" + name);
|
|
401
|
+
if (!mayFence)
|
|
402
|
+
log("workspace_networking.teardown_unfenced", {
|
|
403
|
+
sandboxId: name,
|
|
404
|
+
reason: "policy-preservation-failed"
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
const results = await Promise.allSettled([
|
|
409
|
+
finish(mayFence),
|
|
410
|
+
(async () => {
|
|
411
|
+
if (managed || queryFailed)
|
|
412
|
+
await config.networking?.suspend({
|
|
413
|
+
ctx: {
|
|
414
|
+
organizationId: ctx.session.organizationId,
|
|
415
|
+
actingUser: ctx.actingUser,
|
|
416
|
+
bindings: ctx.bindings
|
|
417
|
+
},
|
|
418
|
+
workspaceId: sandboxWorkspaceId({
|
|
419
|
+
session: ctx.session,
|
|
420
|
+
workspace: options.workspace
|
|
421
|
+
})
|
|
422
|
+
});
|
|
423
|
+
})()
|
|
424
|
+
]);
|
|
425
|
+
for (const result of results) if (result.status === "rejected") failures.push(result.reason);
|
|
426
|
+
if (failures.length === 1) throw failures[0];
|
|
427
|
+
if (failures.length > 0)
|
|
428
|
+
throw new AggregateError(failures, "Daytona teardown or egress revocation failed");
|
|
429
|
+
};
|
|
430
|
+
const teardownState = async (ctx, state, finish) => {
|
|
431
|
+
let failure;
|
|
432
|
+
try {
|
|
433
|
+
ctx = recoveryContext(ctx, state);
|
|
434
|
+
} catch (cause) {
|
|
435
|
+
failure = cause;
|
|
436
|
+
const workspaceId = managedWorkspaceIdentities.get(ctx.session.organizationId + ":" + state.sandboxName) ?? state.workspaceId;
|
|
437
|
+
ctx = { ...ctx, session: { ...ctx.session, workspaceId } };
|
|
438
|
+
}
|
|
439
|
+
try {
|
|
440
|
+
await teardown(ctx, state.sandboxName, finish);
|
|
441
|
+
} catch (cause) {
|
|
442
|
+
if (failure != null)
|
|
443
|
+
throw new AggregateError(
|
|
444
|
+
[failure, cause],
|
|
445
|
+
"Daytona identity mismatch and teardown failed"
|
|
446
|
+
);
|
|
447
|
+
throw cause;
|
|
448
|
+
}
|
|
449
|
+
if (failure != null) throw failure;
|
|
450
|
+
};
|
|
244
451
|
const loadEnv = async (ctx) => {
|
|
245
452
|
for (const name of [...Object.keys(options.env), ...options.variables]) {
|
|
246
453
|
if (PLATFORM_ENVIRONMENT_NAMES.has(name)) {
|
|
@@ -304,10 +511,10 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
304
511
|
}
|
|
305
512
|
return { ...policy, allow: [.../* @__PURE__ */ new Set([...allow, ...await addresses])] };
|
|
306
513
|
};
|
|
307
|
-
const sandboxNameFor =
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
};
|
|
514
|
+
const sandboxNameFor = (ctx) => daytonaWorkspaceName({
|
|
515
|
+
...ctx.session,
|
|
516
|
+
conversationWorkspace: options.workspace === SandboxWorkspace.Conversation
|
|
517
|
+
});
|
|
311
518
|
const createBody = async (args) => {
|
|
312
519
|
const body = {
|
|
313
520
|
name: args.name,
|
|
@@ -327,6 +534,11 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
327
534
|
if (options.workspace === SandboxWorkspace.Ephemeral) body.ephemeral = true;
|
|
328
535
|
if (specPolicy != null)
|
|
329
536
|
Object.assign(body, networkSettingsFor(await platformEgress(args.ctx)(specPolicy)));
|
|
537
|
+
if (args.manageNetworking && config.workspaceNetworkingBootFence !== false && !fenceUnavailable(args.client)) {
|
|
538
|
+
body.networkBlockAll = true;
|
|
539
|
+
body.networkAllowList = void 0;
|
|
540
|
+
body.domainAllowList = void 0;
|
|
541
|
+
}
|
|
330
542
|
return body;
|
|
331
543
|
};
|
|
332
544
|
const awaitRunning = async (ctx, rawState, { timeoutMs }) => {
|
|
@@ -349,68 +561,251 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
349
561
|
);
|
|
350
562
|
await reportPlatformNetworkPolicy(args.ctx, policy);
|
|
351
563
|
};
|
|
564
|
+
const runWithFence = async (client, sandbox, managed) => {
|
|
565
|
+
const waking = sandbox.state !== DaytonaSandboxState.Started;
|
|
566
|
+
if (managed && config.workspaceNetworkingBootFence === true && BOOTING_STATES.has(sandbox.state))
|
|
567
|
+
throw new Error(
|
|
568
|
+
"Daytona cannot verify network enforcement for a workspace already booting outside this binding"
|
|
569
|
+
);
|
|
570
|
+
try {
|
|
571
|
+
const ready = await running(client, sandbox);
|
|
572
|
+
if (managed && waking) await fenceSandbox(client, ready.id, false);
|
|
573
|
+
return ready;
|
|
574
|
+
} catch (cause) {
|
|
575
|
+
if (managed && waking) {
|
|
576
|
+
try {
|
|
577
|
+
await stopByName(client, sandbox.name, true);
|
|
578
|
+
} catch (cleanup) {
|
|
579
|
+
throw new AggregateError([cause, cleanup], "Daytona wake and cleanup failed");
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
throw cause;
|
|
583
|
+
}
|
|
584
|
+
};
|
|
585
|
+
const initialNetworkPolicyForOptions = () => {
|
|
586
|
+
const parsed2 = z.nativeEnum(NetworkPolicy).safeParse(options.networkPolicy ?? NetworkPolicy.AllowAll);
|
|
587
|
+
return parsed2.success ? { mode: parsed2.data } : z.object({ mode: z.nativeEnum(NetworkPolicy), allow: z.array(z.string()).default([]) }).parse(options.networkPolicy);
|
|
588
|
+
};
|
|
589
|
+
const currentNetworkPolicy = (sandbox, requireKnown = true) => {
|
|
590
|
+
if (requireKnown && (sandbox.networkBlockAll == null || sandbox.networkPolicyComplete !== true))
|
|
591
|
+
throw new Error(
|
|
592
|
+
"Daytona did not return a supported network policy; cannot initialize managed networking"
|
|
593
|
+
);
|
|
594
|
+
if (sandbox.networkBlockAll === true) return { mode: NetworkPolicy.DenyAll };
|
|
595
|
+
const allow = [sandbox.networkAllowList, sandbox.domainAllowList].flatMap((value) => value?.split(",") ?? []).map((value) => value.trim()).filter((value) => value.length > 0);
|
|
596
|
+
if (allow.length > 0) return { mode: NetworkPolicy.Allowlist, allow };
|
|
597
|
+
return {
|
|
598
|
+
mode: NetworkPolicy.AllowAll
|
|
599
|
+
};
|
|
600
|
+
};
|
|
352
601
|
const ensureSandbox = async (args) => {
|
|
353
602
|
const { client, name } = args;
|
|
354
603
|
const found = unwrap(await client.getSandbox(name));
|
|
355
|
-
const
|
|
604
|
+
const candidate = options.workspace === SandboxWorkspace.Ephemeral ? await settleEphemeralStop(client, found) : found;
|
|
605
|
+
const existing = candidate == null ? null : await settledSandbox(client, candidate);
|
|
356
606
|
if (existing?.state === DaytonaSandboxState.Error || existing?.state === DaytonaSandboxState.BuildFailed)
|
|
357
607
|
throw daytonaSandboxError(
|
|
358
608
|
"start_failed" /* StartFailed */,
|
|
359
609
|
`Daytona sandbox ${name} is in ${existing.state}; preserving its workspace for recovery`
|
|
360
610
|
);
|
|
361
611
|
if (existing != null && !GONE_STATES.has(existing.state)) {
|
|
362
|
-
|
|
612
|
+
if (args.manageNetworking && args.access === SandboxAccess.Read) {
|
|
613
|
+
if (!BOOTING_STATES.has(existing.state) && existing.state !== DaytonaSandboxState.Started)
|
|
614
|
+
throw new Error(
|
|
615
|
+
"A managed Daytona workspace requires a write binding to resume networking"
|
|
616
|
+
);
|
|
617
|
+
return {
|
|
618
|
+
sandbox: await waitForSandbox(client, existing, DaytonaSandboxState.Started),
|
|
619
|
+
created: false,
|
|
620
|
+
woke: false,
|
|
621
|
+
initialNetworkPolicy: currentNetworkPolicy(existing, true)
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
if (args.manageNetworking) {
|
|
625
|
+
await preserveBeforeBoot(args.ctx, name, currentNetworkPolicy(existing));
|
|
626
|
+
if (existing.state !== DaytonaSandboxState.Started && !BOOTING_STATES.has(existing.state))
|
|
627
|
+
await fenceSandbox(client, existing.id);
|
|
628
|
+
}
|
|
629
|
+
const woke = STOPPED_STATES.has(existing.state);
|
|
630
|
+
const runningSandbox = await runWithFence(client, existing, args.manageNetworking);
|
|
631
|
+
return {
|
|
632
|
+
sandbox: runningSandbox,
|
|
633
|
+
created: false,
|
|
634
|
+
woke,
|
|
635
|
+
initialNetworkPolicy: currentNetworkPolicy(existing, args.manageNetworking)
|
|
636
|
+
};
|
|
363
637
|
}
|
|
364
638
|
if (existing != null) unwrap(await client.deleteSandbox(existing.id));
|
|
639
|
+
const initialPolicy = initialNetworkPolicyForOptions();
|
|
640
|
+
networkSettingsFor(initialPolicy);
|
|
365
641
|
const body = await createBody(args);
|
|
366
|
-
|
|
642
|
+
if (args.manageNetworking) await preserveBeforeBoot(args.ctx, name, initialPolicy);
|
|
643
|
+
const create = async () => {
|
|
644
|
+
let result = await client.createSandbox(body);
|
|
645
|
+
if (config.workspaceNetworkingBootFence == null && args.manageNetworking && unsupportedNetworkSettings(result)) {
|
|
646
|
+
unsupportedBootFences.set(client, now() + 15 * 6e4);
|
|
647
|
+
warnUnfenced(name);
|
|
648
|
+
result = await client.createSandbox(await createBody(args));
|
|
649
|
+
}
|
|
650
|
+
return result;
|
|
651
|
+
};
|
|
652
|
+
let created = await create();
|
|
367
653
|
if (!created.ok && args.artifact.image != null && (created.error === DaytonaErrorCode.NotFound || created.error === DaytonaErrorCode.BadRequest) && /snapshot/i.test(created.details?.[0]?.message ?? "")) {
|
|
368
654
|
await ensureSnapshot({ client, options, image: args.artifact.image });
|
|
369
|
-
created = await
|
|
655
|
+
created = await create();
|
|
370
656
|
}
|
|
371
657
|
if (!created.ok && created.error === DaytonaErrorCode.Conflict) {
|
|
372
658
|
const raced = unwrap(await client.getSandbox(name));
|
|
373
|
-
if (raced != null)
|
|
659
|
+
if (raced != null) {
|
|
660
|
+
if (args.manageNetworking && args.access === SandboxAccess.Read) {
|
|
661
|
+
if (!BOOTING_STATES.has(raced.state) && raced.state !== DaytonaSandboxState.Started)
|
|
662
|
+
throw new Error(
|
|
663
|
+
"A managed Daytona workspace requires a write binding to resume networking"
|
|
664
|
+
);
|
|
665
|
+
return {
|
|
666
|
+
sandbox: await waitForSandbox(client, raced, DaytonaSandboxState.Started),
|
|
667
|
+
created: false,
|
|
668
|
+
woke: false,
|
|
669
|
+
initialNetworkPolicy: currentNetworkPolicy(raced, true)
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
if (args.manageNetworking) {
|
|
673
|
+
await preserveBeforeBoot(args.ctx, name, currentNetworkPolicy(raced));
|
|
674
|
+
if (raced.state !== DaytonaSandboxState.Started && !BOOTING_STATES.has(raced.state))
|
|
675
|
+
await fenceSandbox(client, raced.id);
|
|
676
|
+
}
|
|
677
|
+
const woke = STOPPED_STATES.has(raced.state);
|
|
678
|
+
const runningSandbox = await runWithFence(client, raced, args.manageNetworking);
|
|
679
|
+
return {
|
|
680
|
+
sandbox: runningSandbox,
|
|
681
|
+
created: false,
|
|
682
|
+
woke,
|
|
683
|
+
initialNetworkPolicy: currentNetworkPolicy(raced, args.manageNetworking)
|
|
684
|
+
};
|
|
685
|
+
}
|
|
374
686
|
}
|
|
375
687
|
const sandbox = unwrap(created);
|
|
376
688
|
log("daytona_sandbox.created", { sandboxId: sandbox.id, name });
|
|
689
|
+
let ownedSandboxId = sandbox.id;
|
|
377
690
|
try {
|
|
691
|
+
let ready = await waitForSandbox(client, sandbox, DaytonaSandboxState.Started);
|
|
692
|
+
if (args.manageNetworking) await fenceSandbox(client, ready.id);
|
|
693
|
+
if (args.manageNetworking && config.workspaceNetworkingBootFence == null && fenceUnavailable(client) && ready.networkBlockAll === true && networkSettingsFor(initialNetworkPolicyForOptions()).networkBlockAll !== true) {
|
|
694
|
+
unwrap(await client.deleteSandbox(ready.id));
|
|
695
|
+
ownedSandboxId = null;
|
|
696
|
+
const deletedBy = now() + startTimeoutMs;
|
|
697
|
+
for (; ; ) {
|
|
698
|
+
const deleted = unwrap(await client.getSandbox(name));
|
|
699
|
+
if (deleted == null || deleted.state === DaytonaSandboxState.Destroyed) break;
|
|
700
|
+
if (now() >= deletedBy)
|
|
701
|
+
throw new Error("Daytona did not release the deleted sandbox name before recreation");
|
|
702
|
+
await sleep(STATE_POLL_MS);
|
|
703
|
+
}
|
|
704
|
+
const replacement = unwrap(
|
|
705
|
+
await client.createSandbox({ ...await createBody(args), networkBlockAll: false })
|
|
706
|
+
);
|
|
707
|
+
ownedSandboxId = replacement.id;
|
|
708
|
+
ready = await waitForSandbox(client, replacement, DaytonaSandboxState.Started);
|
|
709
|
+
if (ready.networkBlockAll !== false)
|
|
710
|
+
throw new Error(
|
|
711
|
+
"Daytona did not confirm unfenced creation; networking remains blocked"
|
|
712
|
+
);
|
|
713
|
+
warnUnfenced(ready.id);
|
|
714
|
+
}
|
|
378
715
|
return {
|
|
379
|
-
sandbox:
|
|
380
|
-
created: true
|
|
716
|
+
sandbox: ready,
|
|
717
|
+
created: true,
|
|
718
|
+
woke: false,
|
|
719
|
+
initialNetworkPolicy: initialNetworkPolicyForOptions()
|
|
381
720
|
};
|
|
382
721
|
} catch (error) {
|
|
383
|
-
|
|
384
|
-
() => log("daytona_sandbox.cleanup_failed", { sandboxId:
|
|
385
|
-
);
|
|
722
|
+
if (ownedSandboxId != null)
|
|
723
|
+
await client.deleteSandbox(ownedSandboxId).then(unwrap).catch(() => log("daytona_sandbox.cleanup_failed", { sandboxId: ownedSandboxId }));
|
|
386
724
|
throw error;
|
|
387
725
|
}
|
|
388
726
|
};
|
|
389
|
-
const stopByName = async (client, name) => {
|
|
727
|
+
const stopByName = async (client, name, manageNetworking) => {
|
|
390
728
|
let sandbox = unwrap(await client.getSandbox(name));
|
|
391
|
-
if (sandbox
|
|
729
|
+
if (sandbox != null && BOOTING_STATES.has(sandbox.state))
|
|
392
730
|
sandbox = await waitForSandbox(client, sandbox, DaytonaSandboxState.Started);
|
|
393
|
-
if (sandbox == null || sandbox.state
|
|
394
|
-
const
|
|
395
|
-
if (
|
|
396
|
-
|
|
731
|
+
if (sandbox == null || GONE_STATES.has(sandbox.state)) return;
|
|
732
|
+
const failures = [];
|
|
733
|
+
if (manageNetworking) {
|
|
734
|
+
try {
|
|
735
|
+
await fenceSandbox(client, sandbox.id, false);
|
|
736
|
+
} catch (cause) {
|
|
737
|
+
failures.push(cause);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
if (sandbox.state !== DaytonaSandboxState.Started) {
|
|
741
|
+
if (failures.length === 1) throw failures[0];
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
try {
|
|
745
|
+
const stopped = await client.stopSandbox(sandbox.id);
|
|
746
|
+
if (!stopped.ok && stopped.error !== DaytonaErrorCode.NotFound)
|
|
747
|
+
throw daytonaApiError(stopped);
|
|
748
|
+
} catch (cause) {
|
|
749
|
+
failures.push(cause);
|
|
750
|
+
}
|
|
751
|
+
if (failures.length === 1) throw failures[0];
|
|
752
|
+
if (failures.length > 0)
|
|
753
|
+
throw new AggregateError(failures, "Daytona stop or network fence failed");
|
|
397
754
|
};
|
|
398
|
-
const deleteByName = async (client, name) => {
|
|
399
|
-
|
|
755
|
+
const deleteByName = async (client, name, managed = false) => {
|
|
756
|
+
const failures = [];
|
|
757
|
+
if (managed) {
|
|
758
|
+
try {
|
|
759
|
+
const current = unwrap(await client.getSandbox(name));
|
|
760
|
+
if (current != null && !GONE_STATES.has(current.state))
|
|
761
|
+
await fenceSandbox(client, current.id, false);
|
|
762
|
+
} catch (cause) {
|
|
763
|
+
failures.push(cause);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
try {
|
|
767
|
+
unwrap(await client.deleteSandbox(name));
|
|
768
|
+
} catch (cause) {
|
|
769
|
+
failures.push(cause);
|
|
770
|
+
}
|
|
771
|
+
if (failures.length === 1) throw failures[0];
|
|
772
|
+
if (failures.length > 0)
|
|
773
|
+
throw new AggregateError(failures, "Daytona delete or network fence failed");
|
|
400
774
|
};
|
|
401
|
-
const archiveByName = async (client, name) => {
|
|
402
|
-
let
|
|
403
|
-
if (
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
if (sandbox.state === DaytonaSandboxState.Started) {
|
|
409
|
-
sandbox = unwrap(await client.stopSandbox(sandbox.id));
|
|
775
|
+
const archiveByName = async (client, name, managed) => {
|
|
776
|
+
let fenceFailure = null;
|
|
777
|
+
if (managed) {
|
|
778
|
+
try {
|
|
779
|
+
await stopByName(client, name, true);
|
|
780
|
+
} catch (cause) {
|
|
781
|
+
fenceFailure = cause;
|
|
410
782
|
}
|
|
411
|
-
sandbox = await waitForSandbox(client, sandbox, DaytonaSandboxState.Stopped);
|
|
412
783
|
}
|
|
413
|
-
|
|
784
|
+
try {
|
|
785
|
+
let sandbox = unwrap(await client.getSandbox(name));
|
|
786
|
+
if (sandbox == null || GONE_STATES.has(sandbox.state)) {
|
|
787
|
+
if (fenceFailure != null) throw fenceFailure;
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
if (sandbox.state === DaytonaSandboxState.Archived) {
|
|
791
|
+
if (fenceFailure != null) throw fenceFailure;
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
if (sandbox.state !== DaytonaSandboxState.Stopped) {
|
|
795
|
+
if (sandbox.state === DaytonaSandboxState.Starting)
|
|
796
|
+
sandbox = await waitForSandbox(client, sandbox, DaytonaSandboxState.Started);
|
|
797
|
+
if (sandbox.state === DaytonaSandboxState.Started) {
|
|
798
|
+
sandbox = unwrap(await client.stopSandbox(sandbox.id));
|
|
799
|
+
}
|
|
800
|
+
sandbox = await waitForSandbox(client, sandbox, DaytonaSandboxState.Stopped);
|
|
801
|
+
}
|
|
802
|
+
unwrap(await client.archiveSandbox(sandbox.id));
|
|
803
|
+
} catch (cause) {
|
|
804
|
+
if (fenceFailure != null && cause !== fenceFailure)
|
|
805
|
+
throw new AggregateError([fenceFailure, cause], "Daytona fencing and archive failed");
|
|
806
|
+
throw cause;
|
|
807
|
+
}
|
|
808
|
+
if (fenceFailure != null) throw fenceFailure;
|
|
414
809
|
};
|
|
415
810
|
const cloneRepositories = async (ctx, session) => {
|
|
416
811
|
for (const repository of options.repositories) {
|
|
@@ -487,9 +882,15 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
487
882
|
toolboxProxyUrl: sandbox.toolboxProxyUrl
|
|
488
883
|
});
|
|
489
884
|
const workingDirectory = args.workingDirectory ?? options.workingDirectory ?? unwrap(await toolbox.workDir());
|
|
490
|
-
const stop = () => stopByName(client, sandbox.name);
|
|
491
|
-
const destroy = () => deleteByName(client, sandbox.name);
|
|
492
|
-
const
|
|
885
|
+
const stop = () => teardown(args.ctx, sandbox.name, (managed) => stopByName(client, sandbox.name, managed));
|
|
886
|
+
const destroy = () => teardown(args.ctx, sandbox.name, (managed) => deleteByName(client, sandbox.name, managed));
|
|
887
|
+
const admission = async () => {
|
|
888
|
+
if (!args.manageNetworking && config.networking != null && await needsWorkspaceNetworking(args.ctx, sandbox.name, { access: args.access }))
|
|
889
|
+
throw new Error(
|
|
890
|
+
"Workspace networking changed; reconnect this sandbox before running tools"
|
|
891
|
+
);
|
|
892
|
+
};
|
|
893
|
+
const baseSession = createDaytonaSession({
|
|
493
894
|
client,
|
|
494
895
|
toolbox,
|
|
495
896
|
sandboxId: sandbox.id,
|
|
@@ -498,14 +899,65 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
498
899
|
ports: options.ports,
|
|
499
900
|
baseEnv: platformProcessEnvironment(args.ctx, await loadEnv(args.ctx)),
|
|
500
901
|
platformEgress: platformEgress(args.ctx),
|
|
902
|
+
beforeUse: admission,
|
|
501
903
|
stop,
|
|
502
904
|
destroy
|
|
503
905
|
});
|
|
906
|
+
let network = args.manageNetworking ? networkSettingsFor(currentNetworkPolicy(sandbox)) : {
|
|
907
|
+
networkBlockAll: sandbox.networkBlockAll,
|
|
908
|
+
networkAllowList: sandbox.networkAllowList ?? void 0,
|
|
909
|
+
domainAllowList: sandbox.domainAllowList ?? void 0
|
|
910
|
+
};
|
|
911
|
+
const applyNetwork = async (policy) => {
|
|
912
|
+
await admission();
|
|
913
|
+
const target = policy == null ? network : networkSettingsFor(await platformEgress(args.ctx)(policy));
|
|
914
|
+
const normalizeList = (value) => [
|
|
915
|
+
...new Set(
|
|
916
|
+
(value ?? "").split(",").map((entry) => entry.trim().toLowerCase()).filter(Boolean)
|
|
917
|
+
)
|
|
918
|
+
].sort().join(",");
|
|
919
|
+
const matches = (actual) => (!args.manageNetworking || actual.networkPolicyComplete === true) && (args.manageNetworking ? actual.networkBlockAll : actual.networkBlockAll ?? false) === (target.networkBlockAll ?? false) && normalizeList(actual.networkAllowList) === normalizeList(target.networkAllowList) && normalizeList(actual.domainAllowList) === normalizeList(target.domainAllowList);
|
|
920
|
+
try {
|
|
921
|
+
const actual = args.manageNetworking ? unwrap(await client.getSandbox(sandbox.id)) : null;
|
|
922
|
+
if (args.manageNetworking && actual == null)
|
|
923
|
+
throw new Error("Daytona sandbox no longer exists");
|
|
924
|
+
if (!args.manageNetworking || actual == null || !matches(actual)) {
|
|
925
|
+
const update = await client.updateNetworkSettings(sandbox.id, target);
|
|
926
|
+
if (args.manageNetworking && unsupportedNetworkSettings(update))
|
|
927
|
+
throw new Error(
|
|
928
|
+
"Daytona cannot restore this network policy without custom network controls"
|
|
929
|
+
);
|
|
930
|
+
unwrap(update);
|
|
931
|
+
const confirmed = args.manageNetworking ? unwrap(await client.getSandbox(sandbox.id)) : null;
|
|
932
|
+
if (args.manageNetworking && (confirmed == null || !matches(confirmed)))
|
|
933
|
+
throw new Error("Daytona did not apply the requested network policy");
|
|
934
|
+
}
|
|
935
|
+
network = target;
|
|
936
|
+
} catch (cause) {
|
|
937
|
+
if (args.manageNetworking) {
|
|
938
|
+
try {
|
|
939
|
+
await fenceSandbox(client, sandbox.id, false);
|
|
940
|
+
} catch (fence) {
|
|
941
|
+
throw new AggregateError(
|
|
942
|
+
[cause, fence],
|
|
943
|
+
"Daytona policy restoration and denial failed"
|
|
944
|
+
);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
throw cause;
|
|
948
|
+
}
|
|
949
|
+
};
|
|
950
|
+
const session = {
|
|
951
|
+
...baseSession,
|
|
952
|
+
capabilities: baseSession.capabilities,
|
|
953
|
+
setNetworkPolicy: applyNetwork
|
|
954
|
+
};
|
|
504
955
|
return {
|
|
505
956
|
session,
|
|
506
957
|
workingDirectory,
|
|
958
|
+
restoreNetwork: applyNetwork,
|
|
507
959
|
handle: {
|
|
508
|
-
sandbox:
|
|
960
|
+
sandbox: session,
|
|
509
961
|
onSandboxStop: stop,
|
|
510
962
|
onSandboxDelete: destroy,
|
|
511
963
|
onRuntimeShutdown: async () => void 0
|
|
@@ -515,7 +967,9 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
515
967
|
return {
|
|
516
968
|
workspace: async () => options.workspace,
|
|
517
969
|
// What Daytona's API offers every sandbox; it hosts no MCP servers.
|
|
518
|
-
capabilities: async () => [
|
|
970
|
+
capabilities: async () => [
|
|
971
|
+
.../* @__PURE__ */ new Set([...DAYTONA_CAPABILITIES, ...config.networking?.capabilities ?? []])
|
|
972
|
+
],
|
|
519
973
|
mcpCatalog: async () => ({ computerUse: [], localMcp: {} }),
|
|
520
974
|
artifactKey: async (ctx) => `daytona:v1:${await sha256Hex(
|
|
521
975
|
JSON.stringify({
|
|
@@ -559,21 +1013,81 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
559
1013
|
await loadEnv(ctx);
|
|
560
1014
|
const client = await clientFor(ctx.session.organizationId);
|
|
561
1015
|
const name = await sandboxNameFor(ctx);
|
|
562
|
-
const
|
|
1016
|
+
const manageNetworking = await needsWorkspaceNetworking(ctx, name, { access });
|
|
1017
|
+
const { sandbox, created, woke, initialNetworkPolicy } = await ensureSandbox({
|
|
1018
|
+
client,
|
|
1019
|
+
ctx,
|
|
1020
|
+
name,
|
|
1021
|
+
artifact,
|
|
1022
|
+
manageNetworking,
|
|
1023
|
+
access
|
|
1024
|
+
});
|
|
563
1025
|
try {
|
|
564
1026
|
await awaitRunning(ctx, { sandboxName: sandbox.name }, { timeoutMs: startTimeoutMs });
|
|
565
|
-
await enforceNetworkPolicy({ ctx, client, sandbox, created });
|
|
566
|
-
const attached = await attach({
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
1027
|
+
if (!manageNetworking) await enforceNetworkPolicy({ ctx, client, sandbox, created });
|
|
1028
|
+
const attached = await attach({
|
|
1029
|
+
ctx,
|
|
1030
|
+
client,
|
|
1031
|
+
sandbox,
|
|
1032
|
+
access,
|
|
1033
|
+
workingDirectory: null,
|
|
1034
|
+
manageNetworking,
|
|
1035
|
+
initialNetworkPolicy
|
|
1036
|
+
});
|
|
1037
|
+
if (!manageNetworking || access !== SandboxAccess.Read)
|
|
1038
|
+
await writePlatformFiles(ctx, attached.session);
|
|
1039
|
+
let preparation = null;
|
|
1040
|
+
const beforeUse = () => preparation ??= (async () => {
|
|
1041
|
+
await cloneRepositories(ctx, attached.session);
|
|
1042
|
+
await materializeSandboxSeeds({
|
|
1043
|
+
sandbox: attached.session,
|
|
1044
|
+
root: attached.workingDirectory,
|
|
1045
|
+
files: await sandboxSeedFilesForStart(ctx, artifact)
|
|
1046
|
+
});
|
|
1047
|
+
if (manageNetworking)
|
|
1048
|
+
await attached.session.writeTextFile({
|
|
1049
|
+
path: attached.workingDirectory + "/.final/workspace-prepared/" + await sha256Hex(JSON.stringify(artifact)),
|
|
1050
|
+
content: "complete"
|
|
1051
|
+
});
|
|
1052
|
+
})().catch((cause) => {
|
|
1053
|
+
preparation = null;
|
|
1054
|
+
throw cause;
|
|
1055
|
+
});
|
|
1056
|
+
let wrapped = attached.handle;
|
|
1057
|
+
if (!manageNetworking || config.networking == null) await beforeUse();
|
|
1058
|
+
else if (access !== SandboxAccess.Read && config.networking != null)
|
|
1059
|
+
wrapped = await attachSandboxNetworking({
|
|
1060
|
+
extension: config.networking,
|
|
1061
|
+
log: config.log,
|
|
1062
|
+
ctx,
|
|
1063
|
+
workspaceId: sandboxWorkspaceId({
|
|
1064
|
+
session: ctx.session,
|
|
1065
|
+
workspace: options.workspace
|
|
1066
|
+
}),
|
|
1067
|
+
handle: attached.handle,
|
|
1068
|
+
access,
|
|
1069
|
+
restoreNetwork: attached.restoreNetwork,
|
|
1070
|
+
blockNetwork: () => fenceSandbox(client, sandbox.id, false),
|
|
1071
|
+
initialNetworkPolicy,
|
|
1072
|
+
namespaceExists: async () => {
|
|
1073
|
+
const current = unwrap(await client.getSandbox(sandbox.id));
|
|
1074
|
+
return current?.state === DaytonaSandboxState.Started;
|
|
1075
|
+
},
|
|
1076
|
+
beforeUse
|
|
1077
|
+
});
|
|
1078
|
+
const capabilities = manageNetworking && access === SandboxAccess.Write ? [
|
|
1079
|
+
.../* @__PURE__ */ new Set([
|
|
1080
|
+
...wrapped.sandbox.capabilities,
|
|
1081
|
+
...config.networking?.capabilities ?? []
|
|
1082
|
+
])
|
|
1083
|
+
] : [...wrapped.sandbox.capabilities];
|
|
1084
|
+
const handle = mapSandboxHandle({
|
|
1085
|
+
handle: wrapped,
|
|
1086
|
+
mapSandbox: (sandbox2) => sessionForAccess({ ...sandbox2, capabilities }, access)
|
|
573
1087
|
});
|
|
574
1088
|
log("daytona_sandbox.started", { sandboxId: sandbox.id, created });
|
|
575
1089
|
return {
|
|
576
|
-
handle
|
|
1090
|
+
handle,
|
|
577
1091
|
region: sandbox.target,
|
|
578
1092
|
state: {
|
|
579
1093
|
version: 1,
|
|
@@ -582,14 +1096,27 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
582
1096
|
workingDirectory: attached.workingDirectory,
|
|
583
1097
|
access,
|
|
584
1098
|
workspace: options.workspace,
|
|
585
|
-
|
|
1099
|
+
workspaceId: sandboxWorkspaceId({
|
|
1100
|
+
session: ctx.session,
|
|
1101
|
+
workspace: options.workspace
|
|
1102
|
+
}),
|
|
1103
|
+
onSessionEnd,
|
|
1104
|
+
setupPending: preparation == null,
|
|
1105
|
+
networkingManaged: manageNetworking
|
|
586
1106
|
}
|
|
587
1107
|
};
|
|
588
1108
|
} catch (error) {
|
|
589
|
-
if (created)
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
1109
|
+
if (created || manageNetworking && woke) {
|
|
1110
|
+
try {
|
|
1111
|
+
await teardown(
|
|
1112
|
+
ctx,
|
|
1113
|
+
name,
|
|
1114
|
+
(managed) => created ? deleteByName(client, name, managed) : stopByName(client, name, managed)
|
|
1115
|
+
);
|
|
1116
|
+
} catch (cleanup) {
|
|
1117
|
+
throw new AggregateError([error, cleanup], "Daytona attachment and cleanup failed");
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
593
1120
|
throw error;
|
|
594
1121
|
}
|
|
595
1122
|
},
|
|
@@ -597,18 +1124,47 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
597
1124
|
// one Daytona deleted on stop is created again under the same name.
|
|
598
1125
|
resume: async (ctx, rawArtifact, rawState) => {
|
|
599
1126
|
const state = stateSchema.parse(rawState);
|
|
1127
|
+
if (state.networkingManaged)
|
|
1128
|
+
managedWorkspaces.add(ctx.session.organizationId + ":" + state.sandboxName);
|
|
1129
|
+
ctx = recoveryContext(ctx, state);
|
|
600
1130
|
const client = await clientFor(ctx.session.organizationId);
|
|
1131
|
+
const manageNetworking = await needsWorkspaceNetworking(ctx, state.sandboxName, {
|
|
1132
|
+
access: state.access
|
|
1133
|
+
});
|
|
601
1134
|
const found = unwrap(await client.getSandbox(state.sandboxName));
|
|
602
|
-
const
|
|
1135
|
+
const candidate = state.workspace === SandboxWorkspace.Ephemeral ? await settleEphemeralStop(client, found) : found;
|
|
1136
|
+
const existing = candidate == null ? null : await settledSandbox(client, candidate);
|
|
603
1137
|
let sandbox;
|
|
604
1138
|
let created = false;
|
|
1139
|
+
let woke = false;
|
|
1140
|
+
let initialNetworkPolicy = initialNetworkPolicyForOptions();
|
|
605
1141
|
if (existing != null && !GONE_STATES.has(existing.state)) {
|
|
606
|
-
|
|
1142
|
+
if (manageNetworking && state.access === SandboxAccess.Read && existing.state !== DaytonaSandboxState.Started)
|
|
1143
|
+
throw new Error(
|
|
1144
|
+
"A managed Daytona workspace requires a write binding to resume networking"
|
|
1145
|
+
);
|
|
1146
|
+
initialNetworkPolicy = currentNetworkPolicy(existing, manageNetworking);
|
|
1147
|
+
if (manageNetworking && state.access !== SandboxAccess.Read) {
|
|
1148
|
+
await preserveBeforeBoot(ctx, state.sandboxName, initialNetworkPolicy);
|
|
1149
|
+
if (existing.state !== DaytonaSandboxState.Started && !BOOTING_STATES.has(existing.state))
|
|
1150
|
+
await fenceSandbox(client, existing.id);
|
|
1151
|
+
}
|
|
1152
|
+
woke = STOPPED_STATES.has(existing.state);
|
|
1153
|
+
sandbox = await runWithFence(client, existing, manageNetworking);
|
|
607
1154
|
} else if (state.workspace === SandboxWorkspace.Ephemeral) {
|
|
608
1155
|
const artifact = artifactSchema.parse(rawArtifact);
|
|
609
|
-
const ensured = await ensureSandbox({
|
|
1156
|
+
const ensured = await ensureSandbox({
|
|
1157
|
+
client,
|
|
1158
|
+
ctx,
|
|
1159
|
+
name: state.sandboxName,
|
|
1160
|
+
artifact,
|
|
1161
|
+
manageNetworking,
|
|
1162
|
+
access: state.access
|
|
1163
|
+
});
|
|
610
1164
|
sandbox = ensured.sandbox;
|
|
611
1165
|
created = ensured.created;
|
|
1166
|
+
woke = ensured.woke;
|
|
1167
|
+
initialNetworkPolicy = ensured.initialNetworkPolicy;
|
|
612
1168
|
} else {
|
|
613
1169
|
throw daytonaSandboxError(
|
|
614
1170
|
"sandbox_gone" /* SandboxGone */,
|
|
@@ -617,52 +1173,117 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
617
1173
|
}
|
|
618
1174
|
try {
|
|
619
1175
|
await awaitRunning(ctx, { sandboxName: sandbox.name }, { timeoutMs: startTimeoutMs });
|
|
620
|
-
await enforceNetworkPolicy({ ctx, client, sandbox, created });
|
|
621
|
-
const
|
|
1176
|
+
if (!manageNetworking) await enforceNetworkPolicy({ ctx, client, sandbox, created });
|
|
1177
|
+
const attached = await attach({
|
|
622
1178
|
ctx,
|
|
623
1179
|
client,
|
|
624
1180
|
sandbox,
|
|
625
1181
|
access: state.access,
|
|
626
|
-
workingDirectory: state.workingDirectory
|
|
1182
|
+
workingDirectory: state.workingDirectory,
|
|
1183
|
+
manageNetworking,
|
|
1184
|
+
initialNetworkPolicy
|
|
627
1185
|
});
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
1186
|
+
if (!manageNetworking || state.access !== SandboxAccess.Read)
|
|
1187
|
+
await writePlatformFiles(ctx, attached.session);
|
|
1188
|
+
let preparation = null;
|
|
1189
|
+
const beforeUse = () => preparation ??= (async () => {
|
|
1190
|
+
await cloneRepositories(ctx, attached.session);
|
|
1191
|
+
const artifact = created || sandbox.id !== state.sandboxId || state.setupPending ? artifactSchema.parse(rawArtifact) : null;
|
|
1192
|
+
const marker = artifact == null ? null : state.workingDirectory + "/.final/workspace-prepared/" + await sha256Hex(JSON.stringify(artifact));
|
|
1193
|
+
if (artifact != null && marker != null && (created || await attached.session.stat({ path: marker }) == null)) {
|
|
1194
|
+
await materializeSandboxSeeds({
|
|
1195
|
+
sandbox: attached.session,
|
|
1196
|
+
root: state.workingDirectory,
|
|
1197
|
+
files: await sandboxSeedFilesForStart(ctx, artifact)
|
|
1198
|
+
});
|
|
1199
|
+
if (manageNetworking || state.setupPending)
|
|
1200
|
+
await attached.session.writeTextFile({ path: marker, content: "complete" });
|
|
1201
|
+
}
|
|
1202
|
+
})().catch((cause) => {
|
|
1203
|
+
preparation = null;
|
|
1204
|
+
throw cause;
|
|
1205
|
+
});
|
|
1206
|
+
let handle = attached.handle;
|
|
1207
|
+
if (!manageNetworking || config.networking == null) await beforeUse();
|
|
1208
|
+
else if (state.access !== SandboxAccess.Read && config.networking != null)
|
|
1209
|
+
handle = await attachSandboxNetworking({
|
|
1210
|
+
extension: config.networking,
|
|
1211
|
+
log: config.log,
|
|
1212
|
+
ctx,
|
|
1213
|
+
workspaceId: sandboxWorkspaceId({
|
|
1214
|
+
session: ctx.session,
|
|
1215
|
+
workspace: options.workspace
|
|
1216
|
+
}),
|
|
1217
|
+
handle: attached.handle,
|
|
1218
|
+
access: state.access,
|
|
1219
|
+
restoreNetwork: attached.restoreNetwork,
|
|
1220
|
+
blockNetwork: () => fenceSandbox(client, sandbox.id, false),
|
|
1221
|
+
initialNetworkPolicy,
|
|
1222
|
+
namespaceExists: async () => {
|
|
1223
|
+
const current = unwrap(await client.getSandbox(sandbox.id));
|
|
1224
|
+
return current?.state === DaytonaSandboxState.Started;
|
|
1225
|
+
},
|
|
1226
|
+
beforeUse
|
|
635
1227
|
});
|
|
636
|
-
|
|
1228
|
+
const capabilities = manageNetworking && state.access === SandboxAccess.Write ? [
|
|
1229
|
+
.../* @__PURE__ */ new Set([
|
|
1230
|
+
...handle.sandbox.capabilities,
|
|
1231
|
+
...config.networking?.capabilities ?? []
|
|
1232
|
+
])
|
|
1233
|
+
] : [...handle.sandbox.capabilities];
|
|
1234
|
+
return mapSandboxHandle({
|
|
1235
|
+
handle,
|
|
1236
|
+
mapSandbox: (sandbox2) => sessionForAccess({ ...sandbox2, capabilities }, state.access)
|
|
1237
|
+
});
|
|
637
1238
|
} catch (error) {
|
|
638
|
-
if (created)
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
1239
|
+
if (created || manageNetworking && woke) {
|
|
1240
|
+
try {
|
|
1241
|
+
await teardown(
|
|
1242
|
+
ctx,
|
|
1243
|
+
state.sandboxName,
|
|
1244
|
+
(managed) => created ? deleteByName(client, state.sandboxName, managed) : stopByName(client, state.sandboxName, managed)
|
|
1245
|
+
);
|
|
1246
|
+
} catch (cleanup) {
|
|
1247
|
+
throw new AggregateError([error, cleanup], "Daytona attachment and cleanup failed");
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
642
1250
|
throw error;
|
|
643
1251
|
}
|
|
644
1252
|
},
|
|
645
1253
|
// Stopping keeps the disk; Daytona deletes an ephemeral sandbox instead.
|
|
646
1254
|
park: async (ctx, _artifact, rawState) => {
|
|
647
1255
|
const state = stateSchema.parse(rawState);
|
|
648
|
-
|
|
649
|
-
|
|
1256
|
+
if (state.networkingManaged)
|
|
1257
|
+
managedWorkspaces.add(ctx.session.organizationId + ":" + state.sandboxName);
|
|
1258
|
+
await teardownState(ctx, state, async (managed) => {
|
|
1259
|
+
const client = await clientFor(ctx.session.organizationId);
|
|
1260
|
+
await stopByName(client, state.sandboxName, managed);
|
|
1261
|
+
});
|
|
1262
|
+
return {
|
|
1263
|
+
...state,
|
|
1264
|
+
networkingManaged: state.networkingManaged === true || managedWorkspaces.has(ctx.session.organizationId + ":" + state.sandboxName)
|
|
1265
|
+
};
|
|
650
1266
|
},
|
|
651
1267
|
onSessionEnd: async (ctx, _artifact, rawState, { reason }) => {
|
|
652
1268
|
const state = stateSchema.parse(rawState);
|
|
653
|
-
|
|
1269
|
+
if (state.networkingManaged)
|
|
1270
|
+
managedWorkspaces.add(ctx.session.organizationId + ":" + state.sandboxName);
|
|
654
1271
|
log("daytona_sandbox.session_end", {
|
|
655
1272
|
sandboxId: state.sandboxId,
|
|
656
1273
|
reason,
|
|
657
1274
|
action: state.onSessionEnd
|
|
658
1275
|
});
|
|
659
|
-
|
|
660
|
-
await
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
1276
|
+
const finish = async (managed) => {
|
|
1277
|
+
const client = await clientFor(ctx.session.organizationId);
|
|
1278
|
+
if (state.onSessionEnd === DaytonaSessionEndPolicy.Delete) {
|
|
1279
|
+
await deleteByName(client, state.sandboxName, managed);
|
|
1280
|
+
} else if (state.onSessionEnd === DaytonaSessionEndPolicy.Archive) {
|
|
1281
|
+
await archiveByName(client, state.sandboxName, managed);
|
|
1282
|
+
} else {
|
|
1283
|
+
await stopByName(client, state.sandboxName, managed);
|
|
1284
|
+
}
|
|
1285
|
+
};
|
|
1286
|
+
await teardownState(ctx, state, finish);
|
|
666
1287
|
}
|
|
667
1288
|
};
|
|
668
1289
|
};
|
|
@@ -678,6 +1299,7 @@ export {
|
|
|
678
1299
|
DaytonaSandboxErrorCode,
|
|
679
1300
|
DaytonaSessionEndPolicy2 as DaytonaSessionEndPolicy,
|
|
680
1301
|
createDaytonaSandboxProvider,
|
|
681
|
-
daytonaSandboxError
|
|
1302
|
+
daytonaSandboxError,
|
|
1303
|
+
daytonaWorkspaceName
|
|
682
1304
|
};
|
|
683
1305
|
//# sourceMappingURL=daytona.mjs.map
|