44 * Creates Docker bind specs for writable workspaces and read-only skill source mounts.
55 */
66import fs from "node:fs" ;
7+ import fsp from "node:fs/promises" ;
78import path from "node:path" ;
89import { isPathInside } from "../../infra/path-guards.js" ;
910import { SANDBOX_AGENT_WORKSPACE_MOUNT } from "./constants.js" ;
@@ -19,6 +20,10 @@ export type ReadOnlyWorkspaceSkillMount = {
1920 containerPath : string ;
2021} ;
2122
23+ type ReadOnlyWorkspaceSkillMountCandidate = ReadOnlyWorkspaceSkillMount & {
24+ rootDir : string ;
25+ } ;
26+
2227function formatManagedWorkspaceBind ( params : {
2328 hostPath : string ;
2429 containerPath : string ;
@@ -41,6 +46,15 @@ export function resolveMaterializedSandboxSkillsWorkspaceDir(rootDir: string): s
4146 return path . join ( rootDir , ...MATERIALIZED_SANDBOX_SKILLS_WORKSPACE_PARTS ) ;
4247}
4348
49+ function isCanonicalWorkspaceSkillMountSource ( params : {
50+ rootDir : string ;
51+ hostPath : string ;
52+ } ) : boolean {
53+ const agentRoot = resolveSandboxHostPathViaExistingAncestor ( path . resolve ( params . rootDir ) ) ;
54+ const canonicalSource = resolveSandboxHostPathViaExistingAncestor ( path . resolve ( params . hostPath ) ) ;
55+ return isPathInside ( agentRoot , canonicalSource ) ;
56+ }
57+
4458/** Returns true when a skill mount source exists inside the canonical mount root. */
4559export function isExistingWorkspaceSkillMountSource ( params : {
4660 rootDir : string ;
@@ -54,27 +68,25 @@ export function isExistingWorkspaceSkillMountSource(params: {
5468 return false ;
5569 }
5670
57- const agentRoot = resolveSandboxHostPathViaExistingAncestor ( path . resolve ( params . rootDir ) ) ;
58- const canonicalSource = resolveSandboxHostPathViaExistingAncestor ( path . resolve ( params . hostPath ) ) ;
59- return isPathInside ( agentRoot , canonicalSource ) ;
71+ return isCanonicalWorkspaceSkillMountSource ( params ) ;
6072}
6173
62- /** Finds agent-workspace skill directories that should be mounted read-only in rw workspaces. */
63- export function resolveReadOnlyWorkspaceSkillMounts ( params : {
74+ function resolveReadOnlyWorkspaceSkillMountCandidates ( params : {
6475 workspaceDir : string ;
6576 agentWorkspaceDir : string ;
6677 skillsWorkspaceDir ?: string ;
6778 workdir : string ;
6879 workspaceAccess : SandboxWorkspaceAccess ;
69- } ) : ReadOnlyWorkspaceSkillMount [ ] {
80+ } ) : ReadOnlyWorkspaceSkillMountCandidate [ ] {
7081 if ( params . workspaceAccess !== "rw" ) {
7182 return [ ] ;
7283 }
7384
7485 // RW workspaces mount the project as writable, but skill sources remain read-only so agent
7586 // instructions are visible without letting sandbox commands mutate them.
7687 const materializedSkillsWorkspaceDir =
77- params . skillsWorkspaceDir ?? resolveMaterializedSandboxSkillsWorkspaceDir ( params . agentWorkspaceDir ) ;
88+ params . skillsWorkspaceDir ??
89+ resolveMaterializedSandboxSkillsWorkspaceDir ( params . agentWorkspaceDir ) ;
7890 const mounts = [
7991 {
8092 hostPath : path . join ( params . agentWorkspaceDir , "skills" ) ,
@@ -96,8 +108,56 @@ export function resolveReadOnlyWorkspaceSkillMounts(params: {
96108 rootDir : materializedSkillsWorkspaceDir ,
97109 } ,
98110 ] ;
111+ return mounts ;
112+ }
113+
114+ /** Materializes protected skill mount roots before a first rw sandbox launch. */
115+ export async function ensureReadOnlyWorkspaceSkillMountSources ( params : {
116+ workspaceDir : string ;
117+ agentWorkspaceDir : string ;
118+ skillsWorkspaceDir ?: string ;
119+ workdir : string ;
120+ workspaceAccess : SandboxWorkspaceAccess ;
121+ } ) : Promise < void > {
122+ for ( const mount of resolveReadOnlyWorkspaceSkillMountCandidates ( params ) ) {
123+ if (
124+ ! isCanonicalWorkspaceSkillMountSource ( { rootDir : mount . rootDir , hostPath : mount . hostPath } )
125+ ) {
126+ continue ;
127+ }
128+ try {
129+ const stat = await fsp . lstat ( mount . hostPath ) ;
130+ if ( stat . isDirectory ( ) ) {
131+ continue ;
132+ }
133+ // Existing non-directory or symlink leaves stay rejected by the mount resolver.
134+ continue ;
135+ } catch ( error ) {
136+ if ( ( error as NodeJS . ErrnoException ) . code !== "ENOENT" ) {
137+ throw error ;
138+ }
139+ }
99140
100- return mounts
141+ await fsp . mkdir ( mount . hostPath , { recursive : true } ) ;
142+ if (
143+ ! isExistingWorkspaceSkillMountSource ( { rootDir : mount . rootDir , hostPath : mount . hostPath } )
144+ ) {
145+ throw new Error (
146+ `Created sandbox skill mount source outside workspace root: ${ mount . hostPath } ` ,
147+ ) ;
148+ }
149+ }
150+ }
151+
152+ /** Finds agent-workspace skill directories that should be mounted read-only in rw workspaces. */
153+ export function resolveReadOnlyWorkspaceSkillMounts ( params : {
154+ workspaceDir : string ;
155+ agentWorkspaceDir : string ;
156+ skillsWorkspaceDir ?: string ;
157+ workdir : string ;
158+ workspaceAccess : SandboxWorkspaceAccess ;
159+ } ) : ReadOnlyWorkspaceSkillMount [ ] {
160+ return resolveReadOnlyWorkspaceSkillMountCandidates ( params )
101161 . filter ( ( mount ) =>
102162 isExistingWorkspaceSkillMountSource ( {
103163 rootDir : mount . rootDir ,
0 commit comments