| 1 | //go:build linux |
| 2 | |
| 3 | package containment |
| 4 | |
| 5 | import ( |
| 6 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 7 | "amadan.net/oficina/equipo/internal/runner/provider/claude" |
| 8 | "amadan.net/oficina/equipo/internal/runner/provider/codex" |
| 9 | "encoding/json" |
| 10 | "fmt" |
| 11 | "io" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | // Claude has no offline entry point for its bundled sandbox builder. This |
| 16 | // reproduction consumes the generated filesystem settings and its pinned |
| 17 | // defaults: cwd and CLAUDE_CODE_TMPDIR/claude-${uid}, never all of /tmp. |
| 18 | // Codex exposes `sandbox`, so that branch invokes the real pinned executable. |
| 19 | func engineeringShell(t *testing.T, name, body string) string { |
| 20 | t.Helper() |
| 21 | b := provider.Binding{Provider: name, Home: "/fake-runtime", CredentialHome: "/fake-source", RuntimeRoot: "/fake-homes"} |
| 22 | profile, _ := provider.Profile("implement") |
| 23 | in := provider.PolicyInputs{Action: "implement", Tools: profile.MCPTools()} |
| 24 | var err error |
| 25 | if name == "claude" { |
| 26 | in, err = claude.PolicyInputs(b, in, "Engineering", "claude-opus-5-5") |
| 27 | } else { |
| 28 | in, err = codex.PolicyInputs(b, in, "gpt-6-luna") |
| 29 | } |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | settings, _ := json.Marshal(string(in.Settings)) |
| 34 | script, _ := json.Marshal(body) |
| 35 | return fmt.Sprintf(`import json,os,subprocess |
| 36 | settings=json.loads(%s) |
| 37 | if %q=='claude': |
| 38 | s=settings['sandbox']; fs=s['filesystem'] |
| 39 | assert s['enabled'] and s['failIfUnavailable'] and not s['allowUnsandboxedCommands'] and not s.get('enableWeakerNestedSandbox',False) |
| 40 | temp=os.path.join(os.environ.get('CLAUDE_CODE_TMPDIR','/tmp'),'claude-'+str(os.getuid())) |
| 41 | os.makedirs(temp,exist_ok=True) |
| 42 | prefix=['/opt/bwrap','--new-session','--die-with-parent','--unshare-net','--ro-bind','/','/'] |
| 43 | for path in [os.getcwd(),temp]+fs.get('allowWrite',[]): prefix+=['--bind',path,path] |
| 44 | for path in fs['denyRead']: |
| 45 | if os.path.isdir(path) and path not in ['/proc','/dev/fd']: prefix+=['--tmpfs',path,'--remount-ro',path] |
| 46 | for path in fs['denyWrite']: |
| 47 | if os.path.isdir(path) and path not in fs['denyRead']: prefix+=['--ro-bind',path,path] |
| 48 | for path in ['/workspace/.git/config','/workspace/.git/hooks']: |
| 49 | if os.path.exists(path): prefix+=['--ro-bind',path,path] |
| 50 | prefix+=['--dev','/dev','--unshare-pid','--unshare-user','--proc','/proc','--disable-userns','--'] |
| 51 | else: |
| 52 | p=settings['permissions']['equipo'] |
| 53 | fs=','.join(json.dumps(k)+'='+json.dumps(v) for k,v in sorted(p['filesystem'].items())) |
| 54 | prefix=['/opt/codex','sandbox','-P','equipo','-C','/workspace','-c','permissions.equipo.filesystem={'+fs+'}','-c','permissions.equipo.network.enabled='+str(p['network']['enabled']).lower(),'--'] |
| 55 | child={k:v for k,v in os.environ.items() if k!='CLAUDE_CODE_OAUTH_TOKEN'} |
| 56 | r=subprocess.run(prefix+['/usr/bin/python3','-c',%s],env=child,capture_output=True,text=True) |
| 57 | print(r.stdout,flush=True) |
| 58 | assert r.returncode==0,(r.returncode,r.stderr) |
| 59 | `, settings, name, script) |
| 60 | } |
| 61 | |
| 62 | func engineeringOutput(p provider.Process) ([]byte, []byte) { |
| 63 | stderr := make(chan []byte, 1) |
| 64 | go func() { raw, _ := io.ReadAll(p.Stderr()); stderr <- raw }() |
| 65 | stdout, _ := io.ReadAll(p.Stdout()) |
| 66 | return stdout, <-stderr |
| 67 | } |
| 68 | |