@finalai/daytona-agent-provider 0.8.0 → 0.10.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/HOSTING.md +334 -245
- package/README.md +84 -26
- package/dist/modules/sandboxes/daytona.d.mts +18 -20
- package/dist/modules/sandboxes/daytona.mjs +234 -539
- package/dist/modules/sandboxes/daytona.mjs.map +3 -3
- package/dist/modules/sandboxes/environment.d.mts +5 -36
- package/dist/modules/sandboxes/environment.mjs +11 -59
- package/dist/modules/sandboxes/environment.mjs.map +3 -3
- package/dist/modules/sandboxes/index.d.mts +9 -60
- package/dist/modules/sandboxes/index.mjs +14 -41
- package/dist/modules/sandboxes/index.mjs.map +3 -3
- package/dist/modules/sandboxes/session.d.mts +2 -3
- package/dist/modules/sandboxes/session.mjs +8 -51
- package/dist/modules/sandboxes/session.mjs.map +2 -2
- package/package.json +5 -5
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
import { lookup } from "node:dns/promises";
|
|
2
|
+
import { Capability, FilesAccess, ShellAccess } from "@finalai/agent-protocol";
|
|
2
3
|
import {
|
|
3
|
-
SandboxAccess
|
|
4
|
-
} from "@finalai/agent-provider";
|
|
5
|
-
import {
|
|
6
|
-
defineSandboxProvider,
|
|
7
4
|
attachSandboxNetworking,
|
|
5
|
+
defineSandboxProvider,
|
|
6
|
+
FULL_SANDBOX_GRANT,
|
|
8
7
|
mapSandboxHandle,
|
|
9
8
|
materializeSandboxSeeds,
|
|
10
9
|
NetworkPolicy,
|
|
11
|
-
PLATFORM_ENVIRONMENT_NAMES,
|
|
12
10
|
platformProcessEnvironment,
|
|
13
11
|
reportPlatformNetworkPolicy,
|
|
14
12
|
sandboxSeedArtifact,
|
|
15
13
|
sandboxSeedArtifactSchema,
|
|
16
14
|
sandboxSeedFiles,
|
|
17
|
-
|
|
15
|
+
sandboxSeedFilesForAttach,
|
|
18
16
|
sandboxSeedIdentity,
|
|
19
|
-
sandboxWorkspaceId,
|
|
20
|
-
SandboxWorkspace,
|
|
21
17
|
withPlatformEgressHosts,
|
|
22
18
|
writePlatformFiles
|
|
23
19
|
} from "@finalai/agent-provider/sandbox";
|
|
@@ -32,23 +28,23 @@ import {
|
|
|
32
28
|
import {
|
|
33
29
|
DAYTONA_SANDBOX_NAME,
|
|
34
30
|
daytonaEnvironmentSchema,
|
|
35
|
-
|
|
36
|
-
DaytonaSessionEndPolicy
|
|
31
|
+
DaytonaParkPolicy
|
|
37
32
|
} from "./environment.mjs";
|
|
38
33
|
import {
|
|
39
34
|
createDaytonaSession,
|
|
40
|
-
|
|
35
|
+
DAYTONA_FEATURES,
|
|
41
36
|
daytonaApiError,
|
|
42
37
|
isCidr,
|
|
43
38
|
networkSettingsFor,
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
unwrap,
|
|
40
|
+
withNetworkPolicyValidation
|
|
46
41
|
} from "./session.mjs";
|
|
47
|
-
import { DAYTONA_SANDBOX_SPECIFIER,
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
return
|
|
42
|
+
import { DAYTONA_SANDBOX_SPECIFIER, DaytonaParkPolicy as DaytonaParkPolicy2 } from "./environment.mjs";
|
|
43
|
+
const sha256Hex = async (text) => {
|
|
44
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
45
|
+
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
51
46
|
};
|
|
47
|
+
const daytonaSandboxName = async (args) => `final-${(await sha256Hex(`${args.organizationId}:${args.sandboxId}`)).slice(0, 32)}`;
|
|
52
48
|
var DaytonaSandboxErrorCode = /* @__PURE__ */ ((DaytonaSandboxErrorCode2) => {
|
|
53
49
|
DaytonaSandboxErrorCode2["CredentialMissing"] = "credential_missing";
|
|
54
50
|
DaytonaSandboxErrorCode2["InvalidEnvironment"] = "invalid_environment";
|
|
@@ -65,23 +61,26 @@ const artifactSchema = sandboxSeedArtifactSchema.extend({
|
|
|
65
61
|
// The image the snapshot was built from by prepare; null for a named snapshot.
|
|
66
62
|
image: z.string().nullable()
|
|
67
63
|
});
|
|
64
|
+
const networkPolicySchema = z.object({
|
|
65
|
+
mode: z.nativeEnum(NetworkPolicy),
|
|
66
|
+
allow: z.array(z.string()).optional()
|
|
67
|
+
});
|
|
68
68
|
const stateSchema = z.object({
|
|
69
|
-
version: z.literal(
|
|
70
|
-
sandboxId: z.string(),
|
|
69
|
+
version: z.literal(2),
|
|
71
70
|
// Lifecycle calls address the sandbox by its name, which outlives a re-creation.
|
|
72
71
|
sandboxName: z.string(),
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
72
|
+
// Daytona's id of the sandbox last attached; null before the first attach.
|
|
73
|
+
sandboxId: z.string().nullable(),
|
|
74
|
+
// Null before the first attach reads it.
|
|
75
|
+
workingDirectory: z.string().nullable(),
|
|
76
|
+
// The setup's network policy, enforced from boot; null leaves Daytona's default.
|
|
77
|
+
network: networkPolicySchema.nullable(),
|
|
78
|
+
// Ports the setup's services listen on.
|
|
79
|
+
ports: z.array(z.number().int()),
|
|
80
|
+
// The workspace seeds were deferred by networking and may still be missing.
|
|
81
|
+
seedPending: z.boolean(),
|
|
82
|
+
networkingManaged: z.boolean()
|
|
80
83
|
});
|
|
81
|
-
const sha256Hex = async (text) => {
|
|
82
|
-
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
83
|
-
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
84
|
-
};
|
|
85
84
|
const GONE_STATES = /* @__PURE__ */ new Set([DaytonaSandboxState.Destroyed, DaytonaSandboxState.Destroying]);
|
|
86
85
|
const FAILED_STATES = /* @__PURE__ */ new Set([DaytonaSandboxState.Error, DaytonaSandboxState.BuildFailed]);
|
|
87
86
|
const STOPPED_STATES = /* @__PURE__ */ new Set([
|
|
@@ -109,9 +108,17 @@ const SNAPSHOT_FAILED_STATES = /* @__PURE__ */ new Set([
|
|
|
109
108
|
]);
|
|
110
109
|
const STATE_POLL_MS = 500;
|
|
111
110
|
const SNAPSHOT_POLL_MS = 2e3;
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
111
|
+
const daytonaNetworkPolicyOf = (setup) => {
|
|
112
|
+
const network = setup?.network;
|
|
113
|
+
if (network == null) return null;
|
|
114
|
+
if ((network.deny ?? []).length > 0)
|
|
115
|
+
throw daytonaSandboxError(
|
|
116
|
+
"invalid_environment" /* InvalidEnvironment */,
|
|
117
|
+
"Daytona cannot enforce a network deny list; use an allow list"
|
|
118
|
+
);
|
|
119
|
+
const policy = network.allow == null ? { mode: NetworkPolicy.AllowAll } : { mode: NetworkPolicy.Allowlist, allow: [...network.allow] };
|
|
120
|
+
networkSettingsFor(policy);
|
|
121
|
+
return policy;
|
|
115
122
|
};
|
|
116
123
|
const createDaytonaSandboxProvider = (config) => {
|
|
117
124
|
const apiUrl = config.apiUrl ?? DEFAULT_DAYTONA_API_URL;
|
|
@@ -133,7 +140,7 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
133
140
|
if (credential == null) {
|
|
134
141
|
throw daytonaSandboxError(
|
|
135
142
|
"credential_missing" /* CredentialMissing */,
|
|
136
|
-
'the daytona sandbox has no credential:
|
|
143
|
+
'the daytona sandbox has no credential: give daytona({ credential: { secretRef: "<secret name>" } }), naming the secret that holds your Daytona API key'
|
|
137
144
|
);
|
|
138
145
|
}
|
|
139
146
|
const key = await credentials?.secret({ secretRef: credential.secretRef }) ?? null;
|
|
@@ -181,20 +188,6 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
181
188
|
}
|
|
182
189
|
return current;
|
|
183
190
|
};
|
|
184
|
-
const settleEphemeralStop = async (client, sandbox) => {
|
|
185
|
-
const deadline = now() + startTimeoutMs;
|
|
186
|
-
let current = sandbox;
|
|
187
|
-
while (current?.state === DaytonaSandboxState.Stopping || current?.state === DaytonaSandboxState.Destroying) {
|
|
188
|
-
if (now() > deadline)
|
|
189
|
-
throw daytonaSandboxError(
|
|
190
|
-
"start_failed" /* StartFailed */,
|
|
191
|
-
`Daytona sandbox ${current.name} did not finish stopping`
|
|
192
|
-
);
|
|
193
|
-
await sleep(STATE_POLL_MS);
|
|
194
|
-
current = unwrap(await client.getSandbox(current.id));
|
|
195
|
-
}
|
|
196
|
-
return current;
|
|
197
|
-
};
|
|
198
191
|
const settledSandbox = async (client, sandbox) => {
|
|
199
192
|
const state = SETTLING_STATES.get(sandbox.state);
|
|
200
193
|
return state == null ? sandbox : waitForSandbox(client, sandbox, state);
|
|
@@ -291,8 +284,18 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
291
284
|
throw new Error("Daytona did not confirm its provider network fence");
|
|
292
285
|
};
|
|
293
286
|
const managedWorkspaces = /* @__PURE__ */ new Set();
|
|
294
|
-
const managedWorkspaceIdentities = /* @__PURE__ */ new Map();
|
|
295
287
|
const preparedWorkspaces = /* @__PURE__ */ new Set();
|
|
288
|
+
const workspaceKey = (ctx, name) => `${ctx.organizationId}:${name}`;
|
|
289
|
+
const offerContext = (ctx) => ({
|
|
290
|
+
organizationId: ctx.organizationId,
|
|
291
|
+
projectId: ctx.projectId,
|
|
292
|
+
sandboxId: ctx.sandboxId,
|
|
293
|
+
credentials: ctx.credentials,
|
|
294
|
+
actingUser: ctx.actingUser,
|
|
295
|
+
identity: ctx.identity,
|
|
296
|
+
platformEgressHosts: ctx.platformEgressHosts,
|
|
297
|
+
platformNetworkPolicy: ctx.platformNetworkPolicy
|
|
298
|
+
});
|
|
296
299
|
const environment = (rawOptions) => {
|
|
297
300
|
const parsed = daytonaEnvironmentSchema.safeParse(rawOptions ?? {});
|
|
298
301
|
if (!parsed.success) {
|
|
@@ -302,79 +305,36 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
302
305
|
);
|
|
303
306
|
}
|
|
304
307
|
const options = parsed.data;
|
|
305
|
-
const onSessionEnd = sessionEndPolicyOf(options);
|
|
306
|
-
const specPolicy = options.networkPolicy == null ? null : typeof options.networkPolicy === "string" ? { mode: options.networkPolicy } : { mode: options.networkPolicy.mode, allow: options.networkPolicy.allow };
|
|
307
|
-
const recoveryContext = (ctx, state) => {
|
|
308
|
-
const managed = state.networkingManaged || managedWorkspaces.has(ctx.session.organizationId + ":" + state.sandboxName);
|
|
309
|
-
const retainedIdentity = managedWorkspaceIdentities.get(ctx.session.organizationId + ":" + state.sandboxName) ?? state.workspaceId;
|
|
310
|
-
if (managed && retainedIdentity != null && ctx.session.workspaceId != null && retainedIdentity !== ctx.session.workspaceId)
|
|
311
|
-
throw new Error("Daytona workspace identity does not match retained managed state");
|
|
312
|
-
return {
|
|
313
|
-
...ctx,
|
|
314
|
-
session: {
|
|
315
|
-
...ctx.session,
|
|
316
|
-
workspaceId: ctx.session.workspaceId ?? retainedIdentity ?? sandboxWorkspaceId({ session: ctx.session, workspace: state.workspace })
|
|
317
|
-
}
|
|
318
|
-
};
|
|
319
|
-
};
|
|
320
308
|
const needsWorkspaceNetworking = async (ctx, name, query = {}) => {
|
|
309
|
+
const key = workspaceKey(ctx, name);
|
|
321
310
|
if (config.networking == null) {
|
|
322
|
-
if (
|
|
311
|
+
if (managedWorkspaces.has(key))
|
|
323
312
|
throw new Error(
|
|
324
313
|
"Workspace networking control is required for this managed Daytona workspace"
|
|
325
314
|
);
|
|
326
315
|
return false;
|
|
327
316
|
}
|
|
328
|
-
|
|
329
|
-
ctx:
|
|
330
|
-
|
|
331
|
-
platformNetworkPolicy: ctx.platformNetworkPolicy,
|
|
332
|
-
organizationId: ctx.session.organizationId,
|
|
333
|
-
credentials: ctx.credentials,
|
|
334
|
-
actingUser: ctx.actingUser,
|
|
335
|
-
bindings: ctx.bindings
|
|
336
|
-
},
|
|
337
|
-
workspaceId: sandboxWorkspaceId({ session: ctx.session, workspace: options.workspace }),
|
|
317
|
+
const enabled = (await config.networking.enabled({
|
|
318
|
+
ctx: offerContext(ctx),
|
|
319
|
+
workspaceId: ctx.sandboxId,
|
|
338
320
|
...query
|
|
339
|
-
}) ?? false;
|
|
340
|
-
if (
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
if (enabled) {
|
|
344
|
-
const workspaceId = sandboxWorkspaceId({
|
|
345
|
-
session: ctx.session,
|
|
346
|
-
workspace: options.workspace
|
|
347
|
-
});
|
|
348
|
-
const retained = managedWorkspaceIdentities.get(key);
|
|
349
|
-
if (retained != null && retained !== workspaceId)
|
|
350
|
-
throw new Error("Daytona workspace identity does not match retained managed state");
|
|
351
|
-
managedWorkspaces.add(key);
|
|
352
|
-
managedWorkspaceIdentities.set(key, workspaceId);
|
|
353
|
-
}
|
|
321
|
+
}) ?? false) || managedWorkspaces.has(key);
|
|
322
|
+
if (enabled) {
|
|
323
|
+
managedWorkspaces.add(key);
|
|
324
|
+
config.networking.validateWorkspaceId?.(ctx.sandboxId);
|
|
354
325
|
}
|
|
355
|
-
if (enabled)
|
|
356
|
-
config.networking.validateWorkspaceId?.(
|
|
357
|
-
sandboxWorkspaceId({ session: ctx.session, workspace: options.workspace })
|
|
358
|
-
);
|
|
359
326
|
return enabled;
|
|
360
327
|
};
|
|
361
328
|
const preserveBeforeBoot = async (ctx, name, policy) => {
|
|
362
329
|
networkSettingsFor(policy);
|
|
363
330
|
if (config.networking == null) throw new Error("Missing workspace policy preparation");
|
|
364
331
|
const retained = await config.networking.prepare({
|
|
365
|
-
ctx:
|
|
366
|
-
|
|
367
|
-
platformNetworkPolicy: ctx.platformNetworkPolicy,
|
|
368
|
-
organizationId: ctx.session.organizationId,
|
|
369
|
-
credentials: ctx.credentials,
|
|
370
|
-
actingUser: ctx.actingUser,
|
|
371
|
-
bindings: ctx.bindings
|
|
372
|
-
},
|
|
373
|
-
workspaceId: sandboxWorkspaceId({ session: ctx.session, workspace: options.workspace }),
|
|
332
|
+
ctx: offerContext(ctx),
|
|
333
|
+
workspaceId: ctx.sandboxId,
|
|
374
334
|
policy
|
|
375
335
|
});
|
|
376
336
|
await reportPlatformNetworkPolicy(ctx, retained ?? policy);
|
|
377
|
-
preparedWorkspaces.add(ctx
|
|
337
|
+
preparedWorkspaces.add(workspaceKey(ctx, name));
|
|
378
338
|
};
|
|
379
339
|
const teardown = async (ctx, name, finish) => {
|
|
380
340
|
const failures = [];
|
|
@@ -386,7 +346,7 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
386
346
|
failures.push(cause);
|
|
387
347
|
queryFailed = true;
|
|
388
348
|
}
|
|
389
|
-
let mayFence = managed || managedWorkspaces.has(ctx
|
|
349
|
+
let mayFence = managed || managedWorkspaces.has(workspaceKey(ctx, name));
|
|
390
350
|
if (mayFence) {
|
|
391
351
|
try {
|
|
392
352
|
const client = await clientFor(ctx.credentials, options.credential);
|
|
@@ -394,7 +354,7 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
394
354
|
if (sandbox != null) await preserveBeforeBoot(ctx, name, currentNetworkPolicy(sandbox));
|
|
395
355
|
} catch (cause) {
|
|
396
356
|
failures.push(cause);
|
|
397
|
-
mayFence = preparedWorkspaces.has(ctx
|
|
357
|
+
mayFence = preparedWorkspaces.has(workspaceKey(ctx, name));
|
|
398
358
|
if (!mayFence)
|
|
399
359
|
log("workspace_networking.teardown_unfenced", {
|
|
400
360
|
sandboxId: name,
|
|
@@ -407,16 +367,8 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
407
367
|
(async () => {
|
|
408
368
|
if (managed || queryFailed)
|
|
409
369
|
await config.networking?.suspend({
|
|
410
|
-
ctx:
|
|
411
|
-
|
|
412
|
-
credentials: ctx.credentials,
|
|
413
|
-
actingUser: ctx.actingUser,
|
|
414
|
-
bindings: ctx.bindings
|
|
415
|
-
},
|
|
416
|
-
workspaceId: sandboxWorkspaceId({
|
|
417
|
-
session: ctx.session,
|
|
418
|
-
workspace: options.workspace
|
|
419
|
-
})
|
|
370
|
+
ctx: offerContext(ctx),
|
|
371
|
+
workspaceId: ctx.sandboxId
|
|
420
372
|
});
|
|
421
373
|
})()
|
|
422
374
|
]);
|
|
@@ -425,49 +377,6 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
425
377
|
if (failures.length > 0)
|
|
426
378
|
throw new AggregateError(failures, "Daytona teardown or egress revocation failed");
|
|
427
379
|
};
|
|
428
|
-
const teardownState = async (ctx, state, finish) => {
|
|
429
|
-
let failure;
|
|
430
|
-
try {
|
|
431
|
-
ctx = recoveryContext(ctx, state);
|
|
432
|
-
} catch (cause) {
|
|
433
|
-
failure = cause;
|
|
434
|
-
const workspaceId = managedWorkspaceIdentities.get(ctx.session.organizationId + ":" + state.sandboxName) ?? state.workspaceId;
|
|
435
|
-
ctx = { ...ctx, session: { ...ctx.session, workspaceId } };
|
|
436
|
-
}
|
|
437
|
-
try {
|
|
438
|
-
await teardown(ctx, state.sandboxName, finish);
|
|
439
|
-
} catch (cause) {
|
|
440
|
-
if (failure != null)
|
|
441
|
-
throw new AggregateError(
|
|
442
|
-
[failure, cause],
|
|
443
|
-
"Daytona identity mismatch and teardown failed"
|
|
444
|
-
);
|
|
445
|
-
throw cause;
|
|
446
|
-
}
|
|
447
|
-
if (failure != null) throw failure;
|
|
448
|
-
};
|
|
449
|
-
const loadEnv = async (ctx) => {
|
|
450
|
-
for (const name of [...Object.keys(options.env), ...options.variables]) {
|
|
451
|
-
if (PLATFORM_ENVIRONMENT_NAMES.has(name)) {
|
|
452
|
-
throw daytonaSandboxError(
|
|
453
|
-
"invalid_environment" /* InvalidEnvironment */,
|
|
454
|
-
`${name} is reserved`
|
|
455
|
-
);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
const env = { ...options.env };
|
|
459
|
-
for (const name of options.variables) {
|
|
460
|
-
const value = await ctx.variables.get(name);
|
|
461
|
-
if (value == null) {
|
|
462
|
-
throw daytonaSandboxError(
|
|
463
|
-
"invalid_environment" /* InvalidEnvironment */,
|
|
464
|
-
`variable ${name} is not set`
|
|
465
|
-
);
|
|
466
|
-
}
|
|
467
|
-
env[name] = value;
|
|
468
|
-
}
|
|
469
|
-
return env;
|
|
470
|
-
};
|
|
471
380
|
const egressAddresses = /* @__PURE__ */ new WeakMap();
|
|
472
381
|
const resolveEgressHosts = async (hosts) => {
|
|
473
382
|
const resolved = [];
|
|
@@ -509,29 +418,24 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
509
418
|
}
|
|
510
419
|
return { ...policy, allow: [.../* @__PURE__ */ new Set([...allow, ...await addresses])] };
|
|
511
420
|
};
|
|
512
|
-
const sandboxNameFor = (ctx) => daytonaWorkspaceName({
|
|
513
|
-
...ctx.session,
|
|
514
|
-
conversationWorkspace: options.workspace === SandboxWorkspace.Conversation
|
|
515
|
-
});
|
|
516
421
|
const createBody = async (args) => {
|
|
517
422
|
const body = {
|
|
518
|
-
name: args.
|
|
423
|
+
name: args.state.sandboxName,
|
|
519
424
|
public: false,
|
|
520
425
|
labels: {
|
|
521
426
|
...options.labels,
|
|
522
|
-
"final.organization": args.ctx.
|
|
523
|
-
"final.
|
|
524
|
-
"final.
|
|
427
|
+
"final.organization": args.ctx.organizationId,
|
|
428
|
+
"final.project": args.ctx.projectId,
|
|
429
|
+
"final.sandbox": args.ctx.sandboxId
|
|
525
430
|
},
|
|
526
431
|
autoStopInterval: options.autoStopMinutes
|
|
527
432
|
};
|
|
528
433
|
if (args.artifact.snapshot != null) body.snapshot = args.artifact.snapshot;
|
|
529
|
-
if (options.
|
|
434
|
+
if (options.region != null) body.target = options.region;
|
|
530
435
|
if (options.user != null) body.user = options.user;
|
|
531
436
|
if (options.autoArchiveMinutes != null) body.autoArchiveInterval = options.autoArchiveMinutes;
|
|
532
|
-
if (
|
|
533
|
-
|
|
534
|
-
Object.assign(body, networkSettingsFor(await platformEgress(args.ctx)(specPolicy)));
|
|
437
|
+
if (args.state.network != null)
|
|
438
|
+
Object.assign(body, networkSettingsFor(await platformEgress(args.ctx)(args.state.network)));
|
|
535
439
|
if (args.manageNetworking && config.workspaceNetworkingBootFence !== false && !fenceUnavailable(args.client)) {
|
|
536
440
|
body.networkBlockAll = true;
|
|
537
441
|
body.networkAllowList = void 0;
|
|
@@ -551,8 +455,8 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
551
455
|
await waitForSandbox(client, sandbox, DaytonaSandboxState.Started, timeoutMs);
|
|
552
456
|
};
|
|
553
457
|
const enforceNetworkPolicy = async (args) => {
|
|
554
|
-
if (
|
|
555
|
-
const policy = await platformEgress(args.ctx)(
|
|
458
|
+
if (args.network == null) return;
|
|
459
|
+
const policy = await platformEgress(args.ctx)(args.network);
|
|
556
460
|
if (!args.created)
|
|
557
461
|
unwrap(
|
|
558
462
|
await args.client.updateNetworkSettings(args.sandbox.id, networkSettingsFor(policy))
|
|
@@ -563,7 +467,7 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
563
467
|
const waking = sandbox.state !== DaytonaSandboxState.Started;
|
|
564
468
|
if (managed && config.workspaceNetworkingBootFence === true && BOOTING_STATES.has(sandbox.state))
|
|
565
469
|
throw new Error(
|
|
566
|
-
"Daytona cannot verify network enforcement for a workspace already booting outside this
|
|
470
|
+
"Daytona cannot verify network enforcement for a workspace already booting outside this attach"
|
|
567
471
|
);
|
|
568
472
|
try {
|
|
569
473
|
const ready = await running(client, sandbox);
|
|
@@ -580,10 +484,6 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
580
484
|
throw cause;
|
|
581
485
|
}
|
|
582
486
|
};
|
|
583
|
-
const initialNetworkPolicyForOptions = () => {
|
|
584
|
-
const parsed2 = z.nativeEnum(NetworkPolicy).safeParse(options.networkPolicy ?? NetworkPolicy.AllowAll);
|
|
585
|
-
return parsed2.success ? { mode: parsed2.data } : z.object({ mode: z.nativeEnum(NetworkPolicy), allow: z.array(z.string()).default([]) }).parse(options.networkPolicy);
|
|
586
|
-
};
|
|
587
487
|
const currentNetworkPolicy = (sandbox, requireKnown = true) => {
|
|
588
488
|
if (requireKnown && (sandbox.networkBlockAll == null || sandbox.networkPolicyComplete !== true))
|
|
589
489
|
throw new Error(
|
|
@@ -596,54 +496,50 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
596
496
|
mode: NetworkPolicy.AllowAll
|
|
597
497
|
};
|
|
598
498
|
};
|
|
499
|
+
const wake = async (args) => {
|
|
500
|
+
const { client, existing } = args;
|
|
501
|
+
if (args.manageNetworking) {
|
|
502
|
+
await preserveBeforeBoot(args.ctx, existing.name, currentNetworkPolicy(existing));
|
|
503
|
+
if (existing.state !== DaytonaSandboxState.Started && !BOOTING_STATES.has(existing.state))
|
|
504
|
+
await fenceSandbox(client, existing.id);
|
|
505
|
+
}
|
|
506
|
+
const booted = existing.state !== DaytonaSandboxState.Started;
|
|
507
|
+
const woke = STOPPED_STATES.has(existing.state);
|
|
508
|
+
const sandbox = await runWithFence(client, existing, args.manageNetworking);
|
|
509
|
+
return {
|
|
510
|
+
sandbox,
|
|
511
|
+
created: false,
|
|
512
|
+
booted,
|
|
513
|
+
woke,
|
|
514
|
+
initialNetworkPolicy: currentNetworkPolicy(existing, args.manageNetworking)
|
|
515
|
+
};
|
|
516
|
+
};
|
|
599
517
|
const ensureSandbox = async (args) => {
|
|
600
|
-
const { client
|
|
518
|
+
const { client } = args;
|
|
519
|
+
const name = args.state.sandboxName;
|
|
601
520
|
const found = unwrap(await client.getSandbox(name));
|
|
602
|
-
const
|
|
603
|
-
const existing = candidate == null ? null : await settledSandbox(client, candidate);
|
|
521
|
+
const existing = found == null ? null : await settledSandbox(client, found);
|
|
604
522
|
if (existing?.state === DaytonaSandboxState.Error || existing?.state === DaytonaSandboxState.BuildFailed)
|
|
605
523
|
throw daytonaSandboxError(
|
|
606
524
|
"start_failed" /* StartFailed */,
|
|
607
525
|
`Daytona sandbox ${name} is in ${existing.state}; preserving its workspace for recovery`
|
|
608
526
|
);
|
|
609
|
-
if (existing != null && !GONE_STATES.has(existing.state))
|
|
610
|
-
|
|
611
|
-
if (!BOOTING_STATES.has(existing.state) && existing.state !== DaytonaSandboxState.Started)
|
|
612
|
-
throw new Error(
|
|
613
|
-
"A managed Daytona workspace requires a write binding to resume networking"
|
|
614
|
-
);
|
|
615
|
-
return {
|
|
616
|
-
sandbox: await waitForSandbox(client, existing, DaytonaSandboxState.Started),
|
|
617
|
-
created: false,
|
|
618
|
-
woke: false,
|
|
619
|
-
initialNetworkPolicy: currentNetworkPolicy(existing, true)
|
|
620
|
-
};
|
|
621
|
-
}
|
|
622
|
-
if (args.manageNetworking) {
|
|
623
|
-
await preserveBeforeBoot(args.ctx, name, currentNetworkPolicy(existing));
|
|
624
|
-
if (existing.state !== DaytonaSandboxState.Started && !BOOTING_STATES.has(existing.state))
|
|
625
|
-
await fenceSandbox(client, existing.id);
|
|
626
|
-
}
|
|
627
|
-
const woke = STOPPED_STATES.has(existing.state);
|
|
628
|
-
const runningSandbox = await runWithFence(client, existing, args.manageNetworking);
|
|
629
|
-
return {
|
|
630
|
-
sandbox: runningSandbox,
|
|
631
|
-
created: false,
|
|
632
|
-
woke,
|
|
633
|
-
initialNetworkPolicy: currentNetworkPolicy(existing, args.manageNetworking)
|
|
634
|
-
};
|
|
635
|
-
}
|
|
527
|
+
if (existing != null && !GONE_STATES.has(existing.state))
|
|
528
|
+
return wake({ client, ctx: args.ctx, existing, manageNetworking: args.manageNetworking });
|
|
636
529
|
if (existing != null) unwrap(await client.deleteSandbox(existing.id));
|
|
637
|
-
|
|
530
|
+
if (args.state.sandboxId != null)
|
|
531
|
+
log("daytona_sandbox.recreated", { sandboxId: args.state.sandboxId, name });
|
|
532
|
+
const initialPolicy = args.state.network ?? { mode: NetworkPolicy.AllowAll };
|
|
638
533
|
networkSettingsFor(initialPolicy);
|
|
639
|
-
const
|
|
534
|
+
const bodyArgs = { ...args };
|
|
535
|
+
const body = await createBody(bodyArgs);
|
|
640
536
|
if (args.manageNetworking) await preserveBeforeBoot(args.ctx, name, initialPolicy);
|
|
641
537
|
const create = async () => {
|
|
642
538
|
let result = await client.createSandbox(body);
|
|
643
539
|
if (config.workspaceNetworkingBootFence == null && args.manageNetworking && unsupportedNetworkSettings(result)) {
|
|
644
540
|
unsupportedBootFences.set(client, now() + 15 * 6e4);
|
|
645
541
|
warnUnfenced(name);
|
|
646
|
-
result = await client.createSandbox(await createBody(
|
|
542
|
+
result = await client.createSandbox(await createBody(bodyArgs));
|
|
647
543
|
}
|
|
648
544
|
return result;
|
|
649
545
|
};
|
|
@@ -654,33 +550,13 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
654
550
|
}
|
|
655
551
|
if (!created.ok && created.error === DaytonaErrorCode.Conflict) {
|
|
656
552
|
const raced = unwrap(await client.getSandbox(name));
|
|
657
|
-
if (raced != null)
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
sandbox: await waitForSandbox(client, raced, DaytonaSandboxState.Started),
|
|
665
|
-
created: false,
|
|
666
|
-
woke: false,
|
|
667
|
-
initialNetworkPolicy: currentNetworkPolicy(raced, true)
|
|
668
|
-
};
|
|
669
|
-
}
|
|
670
|
-
if (args.manageNetworking) {
|
|
671
|
-
await preserveBeforeBoot(args.ctx, name, currentNetworkPolicy(raced));
|
|
672
|
-
if (raced.state !== DaytonaSandboxState.Started && !BOOTING_STATES.has(raced.state))
|
|
673
|
-
await fenceSandbox(client, raced.id);
|
|
674
|
-
}
|
|
675
|
-
const woke = STOPPED_STATES.has(raced.state);
|
|
676
|
-
const runningSandbox = await runWithFence(client, raced, args.manageNetworking);
|
|
677
|
-
return {
|
|
678
|
-
sandbox: runningSandbox,
|
|
679
|
-
created: false,
|
|
680
|
-
woke,
|
|
681
|
-
initialNetworkPolicy: currentNetworkPolicy(raced, args.manageNetworking)
|
|
682
|
-
};
|
|
683
|
-
}
|
|
553
|
+
if (raced != null)
|
|
554
|
+
return wake({
|
|
555
|
+
client,
|
|
556
|
+
ctx: args.ctx,
|
|
557
|
+
existing: raced,
|
|
558
|
+
manageNetworking: args.manageNetworking
|
|
559
|
+
});
|
|
684
560
|
}
|
|
685
561
|
const sandbox = unwrap(created);
|
|
686
562
|
log("daytona_sandbox.created", { sandboxId: sandbox.id, name });
|
|
@@ -688,7 +564,7 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
688
564
|
try {
|
|
689
565
|
let ready = await waitForSandbox(client, sandbox, DaytonaSandboxState.Started);
|
|
690
566
|
if (args.manageNetworking) await fenceSandbox(client, ready.id);
|
|
691
|
-
if (args.manageNetworking && config.workspaceNetworkingBootFence == null && fenceUnavailable(client) && ready.networkBlockAll === true && networkSettingsFor(
|
|
567
|
+
if (args.manageNetworking && config.workspaceNetworkingBootFence == null && fenceUnavailable(client) && ready.networkBlockAll === true && networkSettingsFor(initialPolicy).networkBlockAll !== true) {
|
|
692
568
|
unwrap(await client.deleteSandbox(ready.id));
|
|
693
569
|
ownedSandboxId = null;
|
|
694
570
|
const deletedBy = now() + startTimeoutMs;
|
|
@@ -700,7 +576,7 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
700
576
|
await sleep(STATE_POLL_MS);
|
|
701
577
|
}
|
|
702
578
|
const replacement = unwrap(
|
|
703
|
-
await client.createSandbox({ ...await createBody(
|
|
579
|
+
await client.createSandbox({ ...await createBody(bodyArgs), networkBlockAll: false })
|
|
704
580
|
);
|
|
705
581
|
ownedSandboxId = replacement.id;
|
|
706
582
|
ready = await waitForSandbox(client, replacement, DaytonaSandboxState.Started);
|
|
@@ -713,8 +589,9 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
713
589
|
return {
|
|
714
590
|
sandbox: ready,
|
|
715
591
|
created: true,
|
|
592
|
+
booted: true,
|
|
716
593
|
woke: false,
|
|
717
|
-
initialNetworkPolicy:
|
|
594
|
+
initialNetworkPolicy: initialPolicy
|
|
718
595
|
};
|
|
719
596
|
} catch (error) {
|
|
720
597
|
if (ownedSandboxId != null)
|
|
@@ -790,7 +667,7 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
790
667
|
return;
|
|
791
668
|
}
|
|
792
669
|
if (sandbox.state !== DaytonaSandboxState.Stopped) {
|
|
793
|
-
if (sandbox.state
|
|
670
|
+
if (BOOTING_STATES.has(sandbox.state))
|
|
794
671
|
sandbox = await waitForSandbox(client, sandbox, DaytonaSandboxState.Started);
|
|
795
672
|
if (sandbox.state === DaytonaSandboxState.Started) {
|
|
796
673
|
sandbox = unwrap(await client.stopSandbox(sandbox.id));
|
|
@@ -805,75 +682,7 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
805
682
|
}
|
|
806
683
|
if (fenceFailure != null) throw fenceFailure;
|
|
807
684
|
};
|
|
808
|
-
const
|
|
809
|
-
for (const repository of options.repositories) {
|
|
810
|
-
const path = session.resolvePath(repository.alias);
|
|
811
|
-
const readyPath = `${path}/.git/final-clone-ready`;
|
|
812
|
-
if (await session.stat({ path: readyPath }) != null) continue;
|
|
813
|
-
if (await session.stat({ path }) != null) {
|
|
814
|
-
throw daytonaSandboxError(
|
|
815
|
-
"start_failed" /* StartFailed */,
|
|
816
|
-
`repository destination ${path} already exists without a completed clone; choose another alias`
|
|
817
|
-
);
|
|
818
|
-
}
|
|
819
|
-
const stagingPath = session.resolvePath(`.final-clone-${repository.alias}`);
|
|
820
|
-
await session.removePath({ path: stagingPath, recursive: true, force: true });
|
|
821
|
-
const source = await ctx.repositories.cloneSource({
|
|
822
|
-
repository: repository.repository,
|
|
823
|
-
ref: repository.ref
|
|
824
|
-
});
|
|
825
|
-
if (source == null) {
|
|
826
|
-
throw daytonaSandboxError(
|
|
827
|
-
"invalid_environment" /* InvalidEnvironment */,
|
|
828
|
-
`repository ${repository.repository} cannot be resolved for this organization`
|
|
829
|
-
);
|
|
830
|
-
}
|
|
831
|
-
const env = source.authorization == null ? {} : {
|
|
832
|
-
GIT_CONFIG_COUNT: "1",
|
|
833
|
-
GIT_CONFIG_KEY_0: "http.extraHeader",
|
|
834
|
-
GIT_CONFIG_VALUE_0: `Authorization: ${source.authorization}`
|
|
835
|
-
};
|
|
836
|
-
const clone = await session.run({
|
|
837
|
-
command: "git",
|
|
838
|
-
args: ["clone", "--quiet", source.url, stagingPath],
|
|
839
|
-
env: { ...env, GIT_TERMINAL_PROMPT: "0" }
|
|
840
|
-
});
|
|
841
|
-
if (clone.exitCode !== 0) {
|
|
842
|
-
await session.removePath({ path: stagingPath, recursive: true, force: true });
|
|
843
|
-
throw daytonaSandboxError(
|
|
844
|
-
"start_failed" /* StartFailed */,
|
|
845
|
-
`git clone of ${repository.repository} failed: ${clone.stderr.trim().slice(0, 2e3)}`
|
|
846
|
-
);
|
|
847
|
-
}
|
|
848
|
-
const ref = source.ref ?? repository.ref;
|
|
849
|
-
if (ref != null) {
|
|
850
|
-
const checkout = await session.run({
|
|
851
|
-
command: "git",
|
|
852
|
-
args: ["-C", stagingPath, "checkout", "--quiet", ref]
|
|
853
|
-
});
|
|
854
|
-
if (checkout.exitCode !== 0) {
|
|
855
|
-
await session.removePath({ path: stagingPath, recursive: true, force: true });
|
|
856
|
-
throw daytonaSandboxError(
|
|
857
|
-
"start_failed" /* StartFailed */,
|
|
858
|
-
`git checkout ${ref} in ${repository.alias} failed: ${checkout.stderr.trim().slice(0, 2e3)}`
|
|
859
|
-
);
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
await session.writeTextFile({
|
|
863
|
-
path: `${stagingPath}/.git/final-clone-ready`,
|
|
864
|
-
content: "ready"
|
|
865
|
-
});
|
|
866
|
-
const moved = await session.run({ command: "mv", args: ["-T", "--", stagingPath, path] });
|
|
867
|
-
if (moved.exitCode !== 0) {
|
|
868
|
-
throw daytonaSandboxError(
|
|
869
|
-
"start_failed" /* StartFailed */,
|
|
870
|
-
`cannot publish repository ${repository.alias}: ${moved.stderr.trim().slice(0, 2e3)}`
|
|
871
|
-
);
|
|
872
|
-
}
|
|
873
|
-
log("daytona_sandbox.cloned", { alias: repository.alias });
|
|
874
|
-
}
|
|
875
|
-
};
|
|
876
|
-
const attach = async (args) => {
|
|
685
|
+
const openSession = async (args) => {
|
|
877
686
|
const { client, sandbox } = args;
|
|
878
687
|
const toolbox = client.toolbox({
|
|
879
688
|
sandboxId: sandbox.id,
|
|
@@ -883,7 +692,7 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
883
692
|
const stop = () => teardown(args.ctx, sandbox.name, (managed) => stopByName(client, sandbox.name, managed));
|
|
884
693
|
const destroy = () => teardown(args.ctx, sandbox.name, (managed) => deleteByName(client, sandbox.name, managed));
|
|
885
694
|
const admission = async () => {
|
|
886
|
-
if (!args.manageNetworking && config.networking != null && await needsWorkspaceNetworking(args.ctx, sandbox.name
|
|
695
|
+
if (!args.manageNetworking && config.networking != null && await needsWorkspaceNetworking(args.ctx, sandbox.name))
|
|
887
696
|
throw new Error(
|
|
888
697
|
"Workspace networking changed; reconnect this sandbox before running tools"
|
|
889
698
|
);
|
|
@@ -894,8 +703,8 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
894
703
|
sandboxId: sandbox.id,
|
|
895
704
|
description: `Daytona sandbox ${sandbox.name} (${sandbox.target})`,
|
|
896
705
|
workingDirectory,
|
|
897
|
-
ports:
|
|
898
|
-
baseEnv: platformProcessEnvironment(args.ctx,
|
|
706
|
+
ports: args.ports,
|
|
707
|
+
baseEnv: platformProcessEnvironment(args.ctx, {}),
|
|
899
708
|
platformEgress: platformEgress(args.ctx),
|
|
900
709
|
beforeUse: admission,
|
|
901
710
|
stop,
|
|
@@ -962,11 +771,22 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
962
771
|
}
|
|
963
772
|
};
|
|
964
773
|
};
|
|
774
|
+
const teardownState = async (ctx, state, finish) => {
|
|
775
|
+
if (state.networkingManaged) managedWorkspaces.add(workspaceKey(ctx, state.sandboxName));
|
|
776
|
+
await teardown(
|
|
777
|
+
ctx,
|
|
778
|
+
state.sandboxName,
|
|
779
|
+
async (managed) => finish(await clientFor(ctx.credentials, options.credential), managed)
|
|
780
|
+
);
|
|
781
|
+
};
|
|
965
782
|
return {
|
|
966
|
-
|
|
783
|
+
capabilities: async () => ({
|
|
784
|
+
[Capability.Files]: FilesAccess.Write,
|
|
785
|
+
[Capability.Shell]: ShellAccess.Execute
|
|
786
|
+
}),
|
|
967
787
|
// What Daytona's API offers every sandbox; it hosts no MCP servers.
|
|
968
|
-
|
|
969
|
-
.../* @__PURE__ */ new Set([...
|
|
788
|
+
features: async () => [
|
|
789
|
+
.../* @__PURE__ */ new Set([...DAYTONA_FEATURES, ...config.networking?.capabilities ?? []])
|
|
970
790
|
],
|
|
971
791
|
mcpCatalog: async () => ({ computerUse: [], localMcp: {} }),
|
|
972
792
|
artifactKey: async (ctx) => `daytona:v1:${await sha256Hex(
|
|
@@ -980,7 +800,7 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
980
800
|
seedFiles: sandboxSeedIdentity(sandboxSeedFiles(ctx))
|
|
981
801
|
})
|
|
982
802
|
)}`,
|
|
983
|
-
// An image builds
|
|
803
|
+
// An image builds a snapshot once; a named snapshot must exist.
|
|
984
804
|
prepare: async (ctx) => {
|
|
985
805
|
const seedFiles = await sandboxSeedArtifact(ctx, sandboxSeedFiles(ctx));
|
|
986
806
|
if (options.image != null) {
|
|
@@ -1006,67 +826,89 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
1006
826
|
}
|
|
1007
827
|
return { version: 1, snapshot: null, image: null, ...seedFiles };
|
|
1008
828
|
},
|
|
829
|
+
// Creates nothing in Daytona: the first attach does, with the setup's
|
|
830
|
+
// network policy in force from its first boot. What cannot be enforced
|
|
831
|
+
// or reached fails here, before the sandbox exists.
|
|
832
|
+
create: async (ctx, { artifact, setup }) => {
|
|
833
|
+
artifactSchema.parse(artifact);
|
|
834
|
+
const network = daytonaNetworkPolicyOf(setup);
|
|
835
|
+
await clientFor(ctx.credentials, options.credential);
|
|
836
|
+
const ports = [
|
|
837
|
+
...new Set(
|
|
838
|
+
(setup?.services ?? []).flatMap(
|
|
839
|
+
(service) => service.port == null ? [] : [service.port]
|
|
840
|
+
)
|
|
841
|
+
)
|
|
842
|
+
];
|
|
843
|
+
return {
|
|
844
|
+
state: {
|
|
845
|
+
version: 2,
|
|
846
|
+
sandboxName: await daytonaSandboxName(ctx),
|
|
847
|
+
sandboxId: null,
|
|
848
|
+
workingDirectory: null,
|
|
849
|
+
network,
|
|
850
|
+
ports,
|
|
851
|
+
seedPending: false,
|
|
852
|
+
networkingManaged: false
|
|
853
|
+
},
|
|
854
|
+
region: options.region ?? null
|
|
855
|
+
};
|
|
856
|
+
},
|
|
1009
857
|
awaitRunning,
|
|
1010
|
-
|
|
1011
|
-
const access = daytonaOpenOptionsSchema.parse(openOptions ?? {}).access;
|
|
858
|
+
attach: async (ctx, { artifact: rawArtifact, state: rawState }) => {
|
|
1012
859
|
const artifact = artifactSchema.parse(rawArtifact);
|
|
1013
|
-
|
|
860
|
+
const state = stateSchema.parse(rawState);
|
|
861
|
+
const name = state.sandboxName;
|
|
862
|
+
if (state.networkingManaged) managedWorkspaces.add(workspaceKey(ctx, name));
|
|
1014
863
|
const client = await clientFor(ctx.credentials, options.credential);
|
|
1015
|
-
const
|
|
1016
|
-
const
|
|
1017
|
-
const { sandbox, created, woke, initialNetworkPolicy } = await ensureSandbox({
|
|
864
|
+
const manageNetworking = await needsWorkspaceNetworking(ctx, name);
|
|
865
|
+
const { sandbox, created, booted, woke, initialNetworkPolicy } = await ensureSandbox({
|
|
1018
866
|
client,
|
|
1019
867
|
ctx,
|
|
1020
|
-
|
|
868
|
+
state,
|
|
1021
869
|
artifact,
|
|
1022
|
-
manageNetworking
|
|
1023
|
-
access
|
|
870
|
+
manageNetworking
|
|
1024
871
|
});
|
|
1025
872
|
try {
|
|
1026
|
-
await awaitRunning(ctx, { sandboxName:
|
|
1027
|
-
if (!manageNetworking)
|
|
1028
|
-
|
|
873
|
+
await awaitRunning(ctx, { sandboxName: name }, { timeoutMs: startTimeoutMs });
|
|
874
|
+
if (!manageNetworking)
|
|
875
|
+
await enforceNetworkPolicy({ ctx, client, sandbox, created, network: state.network });
|
|
876
|
+
const opened = await openSession({
|
|
1029
877
|
ctx,
|
|
1030
878
|
client,
|
|
1031
879
|
sandbox,
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
manageNetworking
|
|
1035
|
-
initialNetworkPolicy
|
|
880
|
+
workingDirectory: created ? null : state.workingDirectory,
|
|
881
|
+
ports: state.ports,
|
|
882
|
+
manageNetworking
|
|
1036
883
|
});
|
|
1037
|
-
|
|
1038
|
-
|
|
884
|
+
await writePlatformFiles(ctx, opened.session);
|
|
885
|
+
const seedsDue = created || state.seedPending;
|
|
1039
886
|
let preparation = null;
|
|
1040
887
|
const beforeUse = () => preparation ??= (async () => {
|
|
1041
|
-
|
|
888
|
+
if (!seedsDue) return;
|
|
889
|
+
const marker = opened.workingDirectory + "/.final/workspace-prepared/" + await sha256Hex(JSON.stringify(artifact));
|
|
890
|
+
if (!created && await opened.session.stat({ path: marker }) != null) return;
|
|
1042
891
|
await materializeSandboxSeeds({
|
|
1043
|
-
sandbox:
|
|
1044
|
-
root:
|
|
1045
|
-
files: await
|
|
892
|
+
sandbox: opened.session,
|
|
893
|
+
root: opened.workingDirectory,
|
|
894
|
+
files: await sandboxSeedFilesForAttach(ctx, artifact)
|
|
1046
895
|
});
|
|
1047
|
-
|
|
1048
|
-
await attached.session.writeTextFile({
|
|
1049
|
-
path: attached.workingDirectory + "/.final/workspace-prepared/" + await sha256Hex(JSON.stringify(artifact)),
|
|
1050
|
-
content: "complete"
|
|
1051
|
-
});
|
|
896
|
+
await opened.session.writeTextFile({ path: marker, content: "complete" });
|
|
1052
897
|
})().catch((cause) => {
|
|
1053
898
|
preparation = null;
|
|
1054
899
|
throw cause;
|
|
1055
900
|
});
|
|
1056
|
-
let
|
|
901
|
+
let handle = opened.handle;
|
|
1057
902
|
if (!manageNetworking || config.networking == null) await beforeUse();
|
|
1058
|
-
else
|
|
1059
|
-
|
|
903
|
+
else
|
|
904
|
+
handle = await attachSandboxNetworking({
|
|
1060
905
|
extension: config.networking,
|
|
1061
906
|
log: config.log,
|
|
1062
907
|
ctx,
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
handle: attached.handle,
|
|
1068
|
-
access,
|
|
1069
|
-
restoreNetwork: attached.restoreNetwork,
|
|
908
|
+
grant: FULL_SANDBOX_GRANT,
|
|
909
|
+
workspaceId: ctx.sandboxId,
|
|
910
|
+
handle: opened.handle,
|
|
911
|
+
restoreNetwork: opened.restoreNetwork,
|
|
1070
912
|
blockNetwork: () => fenceSandbox(client, sandbox.id, false),
|
|
1071
913
|
initialNetworkPolicy,
|
|
1072
914
|
namespaceExists: async () => {
|
|
@@ -1075,35 +917,28 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
1075
917
|
},
|
|
1076
918
|
beforeUse
|
|
1077
919
|
});
|
|
1078
|
-
const
|
|
920
|
+
const features = manageNetworking ? [
|
|
1079
921
|
.../* @__PURE__ */ new Set([
|
|
1080
|
-
...
|
|
922
|
+
...handle.sandbox.capabilities,
|
|
1081
923
|
...config.networking?.capabilities ?? []
|
|
1082
924
|
])
|
|
1083
|
-
] : [...
|
|
1084
|
-
|
|
1085
|
-
handle: wrapped,
|
|
1086
|
-
mapSandbox: (sandbox2) => sessionForAccess({ ...sandbox2, capabilities }, access)
|
|
1087
|
-
});
|
|
1088
|
-
log("daytona_sandbox.started", { sandboxId: sandbox.id, created });
|
|
925
|
+
] : [...handle.sandbox.capabilities];
|
|
926
|
+
log("daytona_sandbox.attached", { sandboxId: sandbox.id, created, booted });
|
|
1089
927
|
return {
|
|
1090
|
-
handle
|
|
1091
|
-
|
|
928
|
+
handle: mapSandboxHandle({
|
|
929
|
+
handle,
|
|
930
|
+
mapSandbox: (mapped) => withNetworkPolicyValidation({ ...mapped, capabilities: features })
|
|
931
|
+
}),
|
|
1092
932
|
state: {
|
|
1093
|
-
|
|
933
|
+
...state,
|
|
1094
934
|
sandboxId: sandbox.id,
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
}),
|
|
1103
|
-
onSessionEnd,
|
|
1104
|
-
setupPending: preparation == null,
|
|
1105
|
-
networkingManaged: manageNetworking
|
|
1106
|
-
}
|
|
935
|
+
workingDirectory: opened.workingDirectory,
|
|
936
|
+
seedPending: seedsDue && preparation == null,
|
|
937
|
+
networkingManaged: manageNetworking || managedWorkspaces.has(workspaceKey(ctx, name))
|
|
938
|
+
},
|
|
939
|
+
fresh: created,
|
|
940
|
+
booted,
|
|
941
|
+
region: sandbox.target
|
|
1107
942
|
};
|
|
1108
943
|
} catch (error) {
|
|
1109
944
|
if (created || manageNetworking && woke) {
|
|
@@ -1120,185 +955,45 @@ const createDaytonaSandboxProvider = (config) => {
|
|
|
1120
955
|
throw error;
|
|
1121
956
|
}
|
|
1122
957
|
},
|
|
1123
|
-
//
|
|
1124
|
-
|
|
1125
|
-
|
|
958
|
+
// Daytona stops an idle sandbox on its own (autoStopMinutes).
|
|
959
|
+
detach: async (_ctx, { state }) => stateSchema.parse(state),
|
|
960
|
+
// Stopping or archiving keeps the disk.
|
|
961
|
+
park: async (ctx, { state: rawState }) => {
|
|
1126
962
|
const state = stateSchema.parse(rawState);
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
access: state.access
|
|
1133
|
-
});
|
|
1134
|
-
const found = unwrap(await client.getSandbox(state.sandboxName));
|
|
1135
|
-
const candidate = state.workspace === SandboxWorkspace.Ephemeral ? await settleEphemeralStop(client, found) : found;
|
|
1136
|
-
const existing = candidate == null ? null : await settledSandbox(client, candidate);
|
|
1137
|
-
let sandbox;
|
|
1138
|
-
let created = false;
|
|
1139
|
-
let woke = false;
|
|
1140
|
-
let initialNetworkPolicy = initialNetworkPolicyForOptions();
|
|
1141
|
-
if (existing != null && !GONE_STATES.has(existing.state)) {
|
|
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);
|
|
1154
|
-
} else if (state.workspace === SandboxWorkspace.Ephemeral) {
|
|
1155
|
-
const artifact = artifactSchema.parse(rawArtifact);
|
|
1156
|
-
const ensured = await ensureSandbox({
|
|
1157
|
-
client,
|
|
1158
|
-
ctx,
|
|
1159
|
-
name: state.sandboxName,
|
|
1160
|
-
artifact,
|
|
1161
|
-
manageNetworking,
|
|
1162
|
-
access: state.access
|
|
1163
|
-
});
|
|
1164
|
-
sandbox = ensured.sandbox;
|
|
1165
|
-
created = ensured.created;
|
|
1166
|
-
woke = ensured.woke;
|
|
1167
|
-
initialNetworkPolicy = ensured.initialNetworkPolicy;
|
|
1168
|
-
} else {
|
|
1169
|
-
throw daytonaSandboxError(
|
|
1170
|
-
"sandbox_gone" /* SandboxGone */,
|
|
1171
|
-
`Daytona sandbox ${state.sandboxName} is gone`
|
|
1172
|
-
);
|
|
1173
|
-
}
|
|
1174
|
-
try {
|
|
1175
|
-
await awaitRunning(ctx, { sandboxName: sandbox.name }, { timeoutMs: startTimeoutMs });
|
|
1176
|
-
if (!manageNetworking) await enforceNetworkPolicy({ ctx, client, sandbox, created });
|
|
1177
|
-
const attached = await attach({
|
|
1178
|
-
ctx,
|
|
1179
|
-
client,
|
|
1180
|
-
sandbox,
|
|
1181
|
-
access: state.access,
|
|
1182
|
-
workingDirectory: state.workingDirectory,
|
|
1183
|
-
manageNetworking,
|
|
1184
|
-
initialNetworkPolicy
|
|
1185
|
-
});
|
|
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
|
|
1227
|
-
});
|
|
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
|
-
});
|
|
1238
|
-
} catch (error) {
|
|
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
|
-
}
|
|
1250
|
-
throw error;
|
|
1251
|
-
}
|
|
1252
|
-
},
|
|
1253
|
-
// Stopping keeps the disk; Daytona deletes an ephemeral sandbox instead.
|
|
1254
|
-
park: async (ctx, _artifact, rawState) => {
|
|
1255
|
-
const state = stateSchema.parse(rawState);
|
|
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.credentials, options.credential);
|
|
1260
|
-
await stopByName(client, state.sandboxName, managed);
|
|
1261
|
-
});
|
|
963
|
+
await teardownState(
|
|
964
|
+
ctx,
|
|
965
|
+
state,
|
|
966
|
+
(client, managed) => options.park === DaytonaParkPolicy.Archive ? archiveByName(client, state.sandboxName, managed) : stopByName(client, state.sandboxName, managed)
|
|
967
|
+
);
|
|
1262
968
|
return {
|
|
1263
969
|
...state,
|
|
1264
|
-
networkingManaged: state.networkingManaged
|
|
970
|
+
networkingManaged: state.networkingManaged || managedWorkspaces.has(workspaceKey(ctx, state.sandboxName))
|
|
1265
971
|
};
|
|
1266
972
|
},
|
|
1267
|
-
|
|
973
|
+
delete: async (ctx, { state: rawState }) => {
|
|
1268
974
|
const state = stateSchema.parse(rawState);
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
});
|
|
1276
|
-
const finish = async (managed) => {
|
|
1277
|
-
const client = await clientFor(ctx.credentials, options.credential);
|
|
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);
|
|
975
|
+
log("daytona_sandbox.delete", { name: state.sandboxName });
|
|
976
|
+
await teardownState(
|
|
977
|
+
ctx,
|
|
978
|
+
state,
|
|
979
|
+
(client, managed) => deleteByName(client, state.sandboxName, managed)
|
|
980
|
+
);
|
|
1287
981
|
}
|
|
1288
982
|
};
|
|
1289
983
|
};
|
|
1290
984
|
return defineSandboxProvider({
|
|
1291
985
|
name: DAYTONA_SANDBOX_NAME,
|
|
1292
|
-
stateProtocolVersion:
|
|
986
|
+
stateProtocolVersion: 2,
|
|
1293
987
|
environment
|
|
1294
988
|
});
|
|
1295
989
|
};
|
|
1296
990
|
export {
|
|
1297
991
|
DAYTONA_SANDBOX_SPECIFIER,
|
|
992
|
+
DaytonaParkPolicy2 as DaytonaParkPolicy,
|
|
1298
993
|
DaytonaSandboxErrorCode,
|
|
1299
|
-
DaytonaSessionEndPolicy2 as DaytonaSessionEndPolicy,
|
|
1300
994
|
createDaytonaSandboxProvider,
|
|
995
|
+
daytonaNetworkPolicyOf,
|
|
1301
996
|
daytonaSandboxError,
|
|
1302
|
-
|
|
997
|
+
daytonaSandboxName
|
|
1303
998
|
};
|
|
1304
999
|
//# sourceMappingURL=daytona.mjs.map
|