-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmanager.ts
More file actions
708 lines (638 loc) · 30.7 KB
/
Copy pathmanager.ts
File metadata and controls
708 lines (638 loc) · 30.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
import type { SandboxRuntime, SandboxWorkspace } from './msb'
import { buildNetworkAllow, egressRestrictionRequested, describeMsbUnavailable, type MsbAvailability } from './msb'
import type { Logger, SandboxResources, SandboxMountConfig, SandboxSecretConfig } from '../types'
import { resolve, join, isAbsolute, posix as posixPath } from 'path'
import { mkdirSync, existsSync } from 'fs'
import { defaultGitService, type GitService } from '../utils/git-service'
import { canonicalizePath, isSameOrDescendantPath, type SandboxMount } from './path'
import { formatTemplateBuildCommands } from './template'
export interface SandboxManagerConfig {
image: string
resources?: SandboxResources
sourceProjectDir?: string
mountProjectReadonly?: boolean
customMounts?: SandboxMountConfig[]
buildContextDir?: string
browserControl?: boolean
/**
* Host path of opencode's tool-output (truncation) directory. When set and present, it is
* bind-mounted read-only at the identical container path so the agent's in-container tools
* (`sh`, `glob`, `grep`) can read overflow files that opencode references by absolute host path.
*/
toolOutputDir?: string
/**
* Host path of the shared loop scratch/temp directory. When set, it is created if missing and
* bind-mounted read-WRITE at the identical container path, so absolute temp paths resolve
* unchanged inside the container and match the host (worktree-only) view.
*/
tmpDir?: string
/**
* Network policy for the msb sandbox. `env` lists host environment variable names to inject
* into the guest at create time (bare `-e <NAME>`, so values stay off the command line).
* `secrets` lists host-held credentials that never enter the guest (msb substitutes them only
* for the listed hosts at the network boundary). `allow` opts into egress restriction: when it
* is empty msb's own allow-public default applies, and configuring any host switches the
* sandbox to deny-by-default with one allow rule per validated host.
*/
network?: { env?: string[]; allow?: string[]; secrets?: SandboxSecretConfig[] }
}
const DEFAULT_RESOURCES: Required<Pick<SandboxResources, 'memory' | 'cpus'>> = {
memory: '8g',
cpus: '4',
}
function normalizeContainerPath(path: string): string {
const normalized = posixPath.normalize(path)
return normalized.length > 1 && normalized.endsWith('/') ? normalized.slice(0, -1) : normalized
}
function containerPathsOverlap(a: string, b: string): boolean {
const left = normalizeContainerPath(canonicalizePath(a))
const right = normalizeContainerPath(canonicalizePath(b))
return isSameOrDescendantPath(left, right) || isSameOrDescendantPath(right, left)
}
function findContainerPathCollision(container: string, used: ReadonlySet<string>): string | undefined {
for (const existing of used) {
if (containerPathsOverlap(container, existing)) return existing
}
return undefined
}
function isStrictDescendantPath(path: string, prefix: string): boolean {
const resolved = normalizeContainerPath(canonicalizePath(path))
const base = normalizeContainerPath(canonicalizePath(prefix))
return resolved !== base && resolved.startsWith(base + '/')
}
/**
* Whether a new workspace cannot coexist with an already-accepted one on an overlapping path.
* msb accepts nested workspace mounts, but a read-only mount applies to its whole subtree, so
* flags cannot differ across a nesting boundary: a read-only ancestor silently makes a
* read-write descendant read-only, and a read-write descendant of a read-only mount never takes
* effect. The one safe flag mismatch is a read-only mount strictly inside a read-write mount,
* which merely restricts that subtree. Matching flags always coexist.
*/
function mountConflictsWith(next: SandboxMount, accepted: SandboxMount): boolean {
if (!containerPathsOverlap(next.hostDir, accepted.hostDir)) return false
const nextReadOnly = next.readOnly === true
const acceptedReadOnly = accepted.readOnly === true
if (nextReadOnly === acceptedReadOnly) return false
if (nextReadOnly) {
return !isStrictDescendantPath(next.hostDir, accepted.hostDir)
}
return true
}
/** Whether an accepted mount with the same access already covers `next`, making it redundant. */
function mountAlreadyCovered(next: SandboxMount, accepted: SandboxMount): boolean {
if ((next.readOnly === true) !== (accepted.readOnly === true)) return false
return isSameOrDescendantPath(
normalizeContainerPath(canonicalizePath(next.hostDir)),
normalizeContainerPath(canonicalizePath(accepted.hostDir)),
)
}
export function resolveCustomMounts(
raw: SandboxMountConfig[] | undefined,
reservedHostPaths: ReadonlySet<string>,
logger: Logger,
): SandboxMount[] {
if (!raw || raw.length === 0) return []
const resolved: SandboxMount[] = []
const used = new Set<string>(reservedHostPaths)
for (const entry of raw) {
const host = entry?.host?.trim()
if (!host) {
logger.log(`Sandbox: skipping custom mount with missing host path: ${JSON.stringify(entry)}`)
continue
}
if (!isAbsolute(host)) {
logger.log(`Sandbox: skipping custom mount; host path must be absolute: ${host}`)
continue
}
const hostDir = resolve(host)
if (!existsSync(hostDir)) {
logger.log(`Sandbox: skipping custom mount; host path does not exist: ${hostDir}`)
continue
}
const collision = findContainerPathCollision(hostDir, used)
if (collision) {
logger.log(`Sandbox: skipping custom mount; host path already in use: ${hostDir} conflicts with ${collision}`)
continue
}
used.add(hostDir)
resolved.push({ hostDir, containerDir: hostDir, readOnly: entry.readonly !== false })
}
return resolved
}
const DOCKER_AVAILABLE_TTL = 30_000
const LIVENESS_CHECK_TTL = 2_000
export interface ActiveSandbox {
containerName: string
projectDir: string
startedAt: string
mounts: SandboxMount[]
}
export interface SandboxManager {
runtime: SandboxRuntime
start(worktreeName: string, projectDir: string, startedAt?: string): Promise<{ containerName: string }>
stop(worktreeName: string): Promise<void>
getActive(worktreeName: string): ActiveSandbox | null
isActive(worktreeName: string): boolean
isLive(worktreeName: string): Promise<boolean>
cleanupOrphans(preserveWorktrees?: string[]): Promise<number>
restore(worktreeName: string, projectDir: string, startedAt: string): Promise<void>
ensureRunning(worktreeName: string, projectDir: string, startedAt?: string): Promise<string>
}
function dropConflictingMounts(mounts: SandboxMount[], logger: Logger): SandboxMount[] {
const accepted: SandboxMount[] = []
const kept: SandboxMount[] = []
for (const mount of mounts) {
if (accepted.some((existing) => mountConflictsWith(mount, existing))) {
logger.log(`Sandbox: dropping workspace ${mount.hostDir} because it overlaps an already-mounted host dir with conflicting permissions`)
continue
}
if (accepted.some((existing) => mountAlreadyCovered(mount, existing))) continue
accepted.push(mount)
kept.push(mount)
}
return kept
}
/**
* Maps the resolved mount plan to `msb create` workspaces. msb accepts nested workspace
* mounts, so overlapping read-write mounts (worktree + git dirs) coexist; a mount is dropped
* only when its read-only flag conflicts with an accepted mount's (see `mountConflictsWith`).
* Callers must still pass mounts in priority order (worktree → git dirs → read-only project →
* tool-output → temp → custom) so the first-accepted flag wins when a conflict is unavoidable.
*
* Host paths are canonicalized here because msb refuses to mount a host path that traverses a
* symlink, failing the entire sandbox with `ENOTDIR`. On macOS that breaks every `os.tmpdir()`
* mount, since `/var` is a symlink to `private/var`. The container path is deliberately left
* uncanonicalized so absolute paths handed to the agent resolve identically inside the sandbox.
*/
export function buildSandboxWorkspaces(mounts: SandboxMount[], logger: Logger): SandboxWorkspace[] {
return dropConflictingMounts(mounts, logger).map((mount) => ({
hostDir: canonicalizePath(mount.hostDir),
containerDir: mount.containerDir,
readOnly: mount.readOnly,
}))
}
export function createSandboxManager(
runtime: SandboxRuntime,
config: SandboxManagerConfig,
logger: Logger,
git: GitService = defaultGitService,
): SandboxManager {
const activeSandboxes = new Map<string, ActiveSandbox>()
const lastLivenessCheck = new Map<string, number>()
const ensureRunningInFlight = new Map<string, Promise<string>>()
const gitMountCache = new Map<string, SandboxMount[]>()
const convergedSecrets = new Set<string>()
const handledSecretEnvs = new Map<string, Set<string>>()
const warnedUnsetSecretEnv = new Set<string>()
let runtimeAvailableCache: { value: MsbAvailability; at: number } | null = null
let imageReady = false
async function ensureRuntimeAvailable(): Promise<void> {
const now = Date.now()
if (runtimeAvailableCache && (now - runtimeAvailableCache.at) < DOCKER_AVAILABLE_TTL) {
if (!runtimeAvailableCache.value.available) {
throw new Error(describeMsbUnavailable(runtimeAvailableCache.value))
}
return
}
const result = await runtime.checkAvailable()
// An inconclusive probe says nothing about availability: the probe can exhaust its query
// bound under startup load (every worktree probes independently), so a timeout or throw is
// not evidence the runtime is unusable. Failing on it would block loop launches under
// exactly the concurrency forge exists to provide, and caching it would extend one slow probe
// into a window of refusals. Proceed instead and let the real operation report authoritatively.
if (!result.available && result.reason === 'unknown') {
logger.log(`Sandbox: could not determine daemon availability (${result.detail ?? 'no detail'}); continuing`)
return
}
runtimeAvailableCache = { value: result, at: now }
if (!result.available) {
throw new Error(describeMsbUnavailable(result))
}
}
async function ensureTemplate(): Promise<void> {
if (imageReady) return
const exists = await runtime.templateExists(config.image)
if (!exists) {
// A runtime that cannot answer `msb images` is indistinguishable from a missing template,
// so confirm it is really reachable before telling the user to rebuild the image.
const availability = await runtime.checkAvailable()
if (!availability.available) {
if (availability.reason === 'unknown') return
throw new Error(describeMsbUnavailable(availability))
}
const buildHint = ` ${formatTemplateBuildCommands(
config.buildContextDir ?? '<build-context-dir>',
config.image,
{ browserControl: config.browserControl },
)}`
throw new Error(
`Sandbox template "${config.image}" not found. Build and load it first:\n${buildHint}\n\n` +
`To disable the sandbox, set "sandbox": { "enabled": false } in your forge config.`
)
}
imageReady = true
}
function buildMountPlan(projectDir: string): { mounts: SandboxMount[] } {
const absolute = resolve(projectDir)
// `msb` mounts every workspace at its identical host path (there is no separate
// `/workspace` container path), so the primary worktree mount is hostDir == containerDir.
const worktreeMount: SandboxMount = { hostDir: absolute, containerDir: absolute }
const gitMounts = detectGitMount(absolute)
// Outer/common dir first so it survives the overlap drop and keeps the whole git metadata
// region writable (the nested worktree git dir is swallowed by its common dir).
const orderedGitMounts = [...gitMounts].sort((a, b) => a.hostDir.length - b.hostDir.length)
const sourceProjectDir = config.sourceProjectDir
const resolvedSourceProjectDir = sourceProjectDir ? resolve(sourceProjectDir) : undefined
const hasProjectMount = config.mountProjectReadonly !== false
&& !!resolvedSourceProjectDir
&& resolvedSourceProjectDir !== absolute
&& existsSync(resolvedSourceProjectDir)
const projectMount: SandboxMount | undefined = hasProjectMount
? { hostDir: resolvedSourceProjectDir, containerDir: resolvedSourceProjectDir, readOnly: true }
: undefined
const toolOutputMount = resolveToolOutputMount(absolute)
const tmpMount = resolveTempMount(absolute)
const reserved = new Set<string>([worktreeMount.containerDir, ...orderedGitMounts.map((m) => m.containerDir)])
if (projectMount) reserved.add(projectMount.containerDir)
if (toolOutputMount) reserved.add(toolOutputMount.containerDir)
if (tmpMount) reserved.add(tmpMount.containerDir)
const customMounts = resolveCustomMounts(config.customMounts, reserved, logger)
// Priority order is load-bearing: worktree first, then git dirs (outer/common dir first so
// it survives any conflict and keeps the whole git metadata region writable), then the
// read-only project mount, then tool-output/temp/custom. msb accepts nested workspaces, so
// read-write mounts (worktree + git dirs) all mount even when one nests inside another; the
// read-only project workspace is dropped because it is an ancestor of the writable git
// workspaces and a read-only ancestor silently makes them read-only inside the sandbox.
// Never resolve that conflict by making the project workspace read-write — that would let a
// sandboxed agent modify the user's main checkout.
const candidates: SandboxMount[] = [
worktreeMount,
...orderedGitMounts,
...(projectMount ? [projectMount] : []),
...(toolOutputMount ? [toolOutputMount] : []),
...(tmpMount ? [tmpMount] : []),
...customMounts,
]
const mounts = dropConflictingMounts(candidates, logger)
return { mounts }
}
function resolveToolOutputMount(workspaceDir: string): SandboxMount | undefined {
const dir = config.toolOutputDir
if (!dir) return undefined
const resolved = resolve(dir)
if (!existsSync(resolved)) {
logger.log(`Sandbox: skipping tool-output mount; directory does not exist: ${resolved}`)
return undefined
}
if (resolved === workspaceDir || resolved.startsWith(workspaceDir + '/')) return undefined
return { hostDir: resolved, containerDir: resolved, readOnly: true }
}
function resolveTempMount(workspaceDir: string): SandboxMount | undefined {
const dir = config.tmpDir
if (!dir) return undefined
const resolved = resolve(dir)
try {
mkdirSync(resolved, { recursive: true })
} catch (err) {
logger.log(`Sandbox: skipping temp mount; could not create ${resolved}: ${err instanceof Error ? err.message : String(err)}`)
return undefined
}
if (resolved === workspaceDir || resolved.startsWith(workspaceDir + '/')) return undefined
return { hostDir: resolved, containerDir: resolved, readOnly: false }
}
function detectGitMount(projectDir: string): SandboxMount[] {
const cached = gitMountCache.get(projectDir)
if (cached) return cached
const gitDirResult = git.revParseGitDir(projectDir)
const commonDirResult = git.revParseGitCommonDir(projectDir)
if (!gitDirResult.ok || !commonDirResult.ok || !gitDirResult.stdout || !commonDirResult.stdout) {
gitMountCache.set(projectDir, [])
return []
}
const paths = new Set<string>()
const resolvedGitDir = resolve(projectDir, gitDirResult.stdout.trim())
const resolvedCommonDir = resolve(projectDir, commonDirResult.stdout.trim())
if (!isSameOrDescendantPath(canonicalizePath(resolvedGitDir), canonicalizePath(projectDir))) {
paths.add(resolvedGitDir)
}
if (!isSameOrDescendantPath(canonicalizePath(resolvedCommonDir), canonicalizePath(projectDir))) {
paths.add(resolvedCommonDir)
}
const result: SandboxMount[] = [...paths].map((hostDir) => ({ hostDir, containerDir: hostDir, readOnly: false }))
// The git metadata region is mounted read-write so in-sandbox git works, which would also let
// the sandbox plant a hook that runs on the host under the user's account. Forge's own git
// disables hooksPath (see `git-service`), but the user's git in the same repository does not,
// so the hooks directory is re-mounted read-only inside the writable region. `msb` workspaces
// are directories only, so the repo-local config file cannot be protected the same way.
const hooksDir = join(resolvedCommonDir, 'hooks')
if (existsSync(hooksDir)) {
result.push({ hostDir: hooksDir, containerDir: hooksDir, readOnly: true })
}
gitMountCache.set(projectDir, result)
return result
}
/**
* Single point that records a usable sandbox in the active map, shared by the create and
* adopt paths in `start` and by `resolveUsableSandbox`. An existing entry's `startedAt` wins
* so adopting a sandbox never resets the time it actually came up. A precomputed mount plan
* is reused when provided so the create path does not run it twice.
*/
function registerActiveSandbox(worktreeName: string, containerName: string, projectDir: string, startedAt?: string, mounts?: SandboxMount[]): void {
const active = activeSandboxes.get(worktreeName)
activeSandboxes.set(worktreeName, {
containerName,
projectDir: resolve(projectDir),
startedAt: active?.startedAt ?? startedAt ?? new Date().toISOString(),
mounts: mounts ?? buildMountPlan(projectDir).mounts,
})
}
/**
* Resolves the configured env passthrough against the live host environment. msb fails the
* create when a bare `-e NAME` references an unset host variable, so only defined values are
* forwarded and the omission is logged.
*/
function resolvePassthroughEnv(): string[] {
return (config.network?.env ?? []).filter((name) => {
if (process.env[name] === undefined) {
logger.log(`Sandbox: skipping env passthrough ${name}: host variable is not set`)
return false
}
return true
})
}
/**
* Resolves the configured secrets against the live host environment. Entries without an env
* name or allowed hosts are misconfigurations, and msb refuses a secret whose host variable is
* unset at create, so each skipped entry is logged with its reason.
*/
function resolveSandboxSecrets(): SandboxSecretConfig[] {
const resolved: SandboxSecretConfig[] = []
for (const raw of config.network?.secrets ?? []) {
const env = raw.env.trim()
if (!env) {
logger.log('Sandbox: skipping secret: missing env name')
continue
}
const hosts = (raw.hosts ?? []).map((h) => h.trim()).filter(Boolean)
if (hosts.length === 0) {
logger.log(`Sandbox: skipping secret ${env}: no allowed hosts`)
continue
}
if (process.env[env] === undefined) {
if (!warnedUnsetSecretEnv.has(env)) {
warnedUnsetSecretEnv.add(env)
logger.log(`Sandbox: skipping secret ${env}: host variable is not set; sandboxed shell commands will fail until the variable is exported in the environment that launches opencode (configured under sandbox.network.secrets)`)
}
continue
}
resolved.push({ env, hosts })
}
return resolved
}
/**
* Rotates the configured host-held secrets on a sandbox forge is adopting. Create-time
* bindings are captured once, but adoption reuses a long-lived container across plugin
* restarts, so a rotated host token would otherwise stay stale for the life of the sandbox.
* Runs only on adopt paths: the fresh-create path already bound the same filtered list.
* Returns false (without marking the container converged) when the rotation fails, so the
* caller fails the adoption and a later attempt can retry.
*/
async function refreshSecrets(containerName: string): Promise<boolean> {
if (convergedSecrets.has(containerName)) return true
const secrets = resolveSandboxSecrets()
if (secrets.length === 0) {
convergedSecrets.add(containerName)
return true
}
warnUncoveredSecretHosts(containerName, secrets)
if (!(await runtime.refreshSandboxSecrets(containerName, secrets))) {
logger.log(`Sandbox: failed to refresh secrets for ${containerName}`)
return false
}
convergedSecrets.add(containerName)
recordHandledSecretEnvs(containerName, secrets)
return true
}
function recordHandledSecretEnvs(containerName: string, secrets: SandboxSecretConfig[]): void {
const known = handledSecretEnvs.get(containerName) ?? new Set<string>()
for (const secret of secrets) known.add(secret.env)
handledSecretEnvs.set(containerName, known)
}
function warnUncoveredSecretHosts(containerName: string, secrets: SandboxSecretConfig[]): void {
const known = handledSecretEnvs.get(containerName)
const introduced = known ? secrets.filter((s) => !known.has(s.env)) : secrets
if (introduced.length === 0) return
logger.log(`Sandbox: egress for secret host(s) of ${introduced.map((s) => s.env).join(', ')} may be unreachable in ${containerName}: msb cannot change egress rules on an existing sandbox, so recreate the sandbox for the new host(s) to be allowed`)
}
async function start(worktreeName: string, projectDir: string, startedAt?: string): Promise<{ containerName: string }> {
await ensureRuntimeAvailable()
await ensureTemplate()
const containerName = runtime.sandboxContainerName(worktreeName)
const absoluteProjectDir = resolve(projectDir)
const state = await runtime.getSandboxState(containerName)
// `unknown` means the state query failed and says nothing about the sandbox: adopting
// could register a non-existent container as usable, and creating could collide with a
// live one. Fail closed and let the caller surface the indeterminate state.
if (state === 'unknown') {
throw new Error(
`Could not determine whether sandbox ${containerName} exists (state query failed); refusing to start`,
)
}
if (state !== 'missing') {
logger.log(`Sandbox ${containerName} already exists (${state}), adopting`)
if (!(await refreshSecrets(containerName))) {
throw new Error(`Failed to refresh secrets for sandbox ${containerName}; refusing to adopt`)
}
registerActiveSandbox(worktreeName, containerName, projectDir, startedAt)
return { containerName }
}
const { mounts } = buildMountPlan(absoluteProjectDir)
const workspaces = buildSandboxWorkspaces(mounts, logger)
const resources: SandboxResources = {
memory: config.resources?.memory ?? DEFAULT_RESOURCES.memory,
maxMemory: config.resources?.maxMemory,
cpus: config.resources?.cpus ?? DEFAULT_RESOURCES.cpus,
maxCpus: config.resources?.maxCpus,
dockerDisk: config.resources?.dockerDisk,
}
// Secret destinations are unioned into the egress allow-list: msb's proxy is deny-by-default
// at the sandbox level, so a secrets-only configuration would otherwise never reach its hosts.
const secrets = resolveSandboxSecrets()
const memoryLabel = resources.maxMemory ? `${resources.memory}/max ${resources.maxMemory}` : resources.memory
const cpusLabel = resources.maxCpus ? `${resources.cpus}/max ${resources.maxCpus}` : resources.cpus
logger.log(`Creating sandbox ${containerName} for ${absoluteProjectDir} (memory=${memoryLabel} cpus=${cpusLabel} workspaces=${workspaces.length})`)
await runtime.createSandbox(containerName, workspaces, {
image: config.image,
resources,
networkAllow: buildNetworkAllow(config.network?.allow, secrets, logger),
restrictEgress: egressRestrictionRequested(config.network?.allow, secrets),
env: resolvePassthroughEnv(),
secrets,
})
convergedSecrets.add(containerName)
recordHandledSecretEnvs(containerName, secrets)
registerActiveSandbox(worktreeName, containerName, projectDir, startedAt, mounts)
logger.log(`Sandbox ${containerName} started`)
return { containerName }
}
async function stop(worktreeName: string): Promise<void> {
const active = activeSandboxes.get(worktreeName)
const containerName = active?.containerName || runtime.sandboxContainerName(worktreeName)
// Fail-closed on the five-state contract: `unknown` means the state query failed and says
// nothing about the sandbox, so removal could destroy a live container that a concurrent or
// indeterminate query cannot see. Preserve the active-map entry (if any) so callers can
// observe the indeterminate state, and refuse to touch the sandbox. `missing` is a confirmed
// absence: clear stale local bookkeeping without invoking msb.
const state = await runtime.getSandboxState(containerName)
if (state === 'unknown') {
const err = new Error(
`Could not determine whether sandbox ${containerName} exists (state query failed); refusing to remove`,
)
logger.log(`Sandbox ${containerName} stop: ${err.message}`)
throw err
}
if (state === 'missing') {
activeSandboxes.delete(worktreeName)
convergedSecrets.delete(containerName)
handledSecretEnvs.delete(containerName)
logger.log(`Sandbox ${containerName} already gone`)
return
}
// Cleanup (in-memory map entry) always runs; the removal failure is rethrown so
// callers that own the container lifecycle (e.g. the session-sandbox controller) can observe
// that the container may still be live instead of recording a successful stop.
let removalError: unknown = null
try {
await runtime.removeSandbox(containerName)
logger.log(`Sandbox ${containerName} removed`)
} catch (err) {
removalError = err
const errMsg = err instanceof Error ? err.message : String(err)
logger.log(`Sandbox ${containerName} removal: ${errMsg}`)
} finally {
// Cleanup of the in-memory map entry must never be skipped: a stale entry would leave the
// manager believing a removed container is live, blocking recreation.
activeSandboxes.delete(worktreeName)
convergedSecrets.delete(containerName)
handledSecretEnvs.delete(containerName)
}
if (removalError) throw removalError
}
function getActive(worktreeName: string): ActiveSandbox | null {
return activeSandboxes.get(worktreeName) || null
}
function isActive(worktreeName: string): boolean {
return activeSandboxes.has(worktreeName)
}
/**
* A `stopped` sandbox is live: msb suspends idle microVMs and `msb exec` resumes them in
* place. Only a confirmed-`missing` sandbox invalidates the map entry — `unknown` means the
* status query failed and is not evidence the sandbox is gone.
*/
async function isLive(worktreeName: string): Promise<boolean> {
const active = activeSandboxes.get(worktreeName)
if (!active) {
return false
}
const containerName = active.containerName
const state = await runtime.getSandboxState(containerName)
if (state === 'missing') {
logger.log(`Sandbox: sandbox ${containerName} no longer exists, removing stale map entry for ${worktreeName}`)
activeSandboxes.delete(worktreeName)
return false
}
return true
}
async function cleanupOrphans(preserveWorktrees?: string[]): Promise<number> {
const sandboxes = await runtime.listSandboxesByPrefix('forge-')
let removed = 0
const preserveSet = preserveWorktrees
? new Set(preserveWorktrees.map((wt) => runtime.sandboxContainerName(wt)))
: new Set<string>()
for (const name of sandboxes) {
if (preserveSet.has(name)) {
continue
}
try {
await runtime.removeSandbox(name)
removed++
convergedSecrets.delete(name)
handledSecretEnvs.delete(name)
logger.log(`Removed orphaned sandbox: ${name}`)
} catch (err) {
const errMsg = err instanceof Error ? err.message : String(err)
logger.error(`Failed to remove orphaned sandbox ${name}: ${errMsg}`)
}
}
if (!preserveWorktrees) {
activeSandboxes.clear()
convergedSecrets.clear()
handledSecretEnvs.clear()
} else {
for (const key of activeSandboxes.keys()) {
if (!preserveWorktrees.includes(key)) {
activeSandboxes.delete(key)
}
}
}
return removed
}
async function restore(worktreeName: string, projectDir: string, startedAt: string): Promise<void> {
await ensureRunning(worktreeName, projectDir, startedAt)
}
/**
* Single decision point for "is this worktree's sandbox usable?", shared by the mapped and
* unmapped paths. `running` and `stopped` are both usable — msb suspends idle microVMs to
* `stopped` and `msb exec` resumes them in place, so recreating one would needlessly destroy
* container-local state. `unknown` means the status query failed and says nothing about the
* sandbox, so an existing entry is kept as-is (without refreshing the liveness timestamp, so
* the next call re-checks) and only a confirmed-`missing` sandbox is created.
*/
async function resolveUsableSandbox(worktreeName: string, projectDir: string, startedAt?: string): Promise<string> {
const active = activeSandboxes.get(worktreeName)
const containerName = active?.containerName ?? runtime.sandboxContainerName(worktreeName)
const state = await runtime.getSandboxState(containerName)
if (state === 'running' || state === 'stopped') {
if (!(await refreshSecrets(containerName))) {
throw new Error(`Failed to refresh secrets for sandbox ${containerName}; refusing to adopt`)
}
registerActiveSandbox(worktreeName, containerName, projectDir, startedAt)
lastLivenessCheck.set(worktreeName, Date.now())
return containerName
}
if (state === 'unknown' && active) {
logger.log(`Sandbox: state of ${containerName} is unknown, keeping ${worktreeName} unchanged`)
return active.containerName
}
const result = await start(worktreeName, projectDir, startedAt)
lastLivenessCheck.set(worktreeName, Date.now())
return result.containerName
}
async function ensureRunning(worktreeName: string, projectDir: string, startedAt?: string): Promise<string> {
const active = activeSandboxes.get(worktreeName)
const lastCheck = lastLivenessCheck.get(worktreeName)
const now = Date.now()
if (active && lastCheck !== undefined && (now - lastCheck) < LIVENESS_CHECK_TTL) {
return active.containerName
}
// Single-flight per worktree: concurrent callers would otherwise each run the create path and
// race one another, with the loser's create rejected while the winner's is still in flight.
const inFlight = ensureRunningInFlight.get(worktreeName)
if (inFlight) return inFlight
const pending = resolveUsableSandbox(worktreeName, projectDir, startedAt)
.finally(() => ensureRunningInFlight.delete(worktreeName))
ensureRunningInFlight.set(worktreeName, pending)
return pending
}
return {
runtime,
start,
stop,
getActive,
isActive,
isLive,
cleanupOrphans,
restore,
ensureRunning,
}
}