| 1 | package codex |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "io/fs" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "path/filepath" |
| 13 | "runtime" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 19 | "amadan.net/oficina/equipo/internal/testci" |
| 20 | ) |
| 21 | |
| 22 | // The transport runs the real CLI without an egress relay. Open, policy |
| 23 | // attestation, account staging and RPC handling are the production code. |
| 24 | type offlineCodexHost struct{ t *testing.T } |
| 25 | type offlineCodexProcess struct { |
| 26 | in io.WriteCloser |
| 27 | out io.ReadCloser |
| 28 | done chan struct{} |
| 29 | exit provider.Exit |
| 30 | } |
| 31 | |
| 32 | func (p *offlineCodexProcess) ID() string { return "offline-replay" } |
| 33 | func (p *offlineCodexProcess) Stdin() io.WriteCloser { return p.in } |
| 34 | func (p *offlineCodexProcess) Stdout() io.ReadCloser { return p.out } |
| 35 | func (p *offlineCodexProcess) Stderr() io.ReadCloser { return io.NopCloser(strings.NewReader("")) } |
| 36 | func (p *offlineCodexProcess) Wait(ctx context.Context) (provider.Exit, error) { |
| 37 | select { |
| 38 | case <-p.done: |
| 39 | return p.exit, nil |
| 40 | case <-ctx.Done(): |
| 41 | return provider.Exit{}, ctx.Err() |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func (h offlineCodexHost) Start(ctx context.Context, l provider.Launch, argv []string) (provider.Process, error) { |
| 46 | if _, err := provider.AttestLaunch(l, argv); err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | args := []string{"--unshare-all", "--die-with-parent", "--new-session", "--ro-bind", "/usr", "/usr", "--symlink", "usr/bin", "/bin", "--symlink", "usr/lib", "/lib", "--symlink", "usr/lib64", "/lib64", "--proc", "/proc", "--dev", "/dev", "--tmpfs", "/tmp", "--bind", l.Binding.Home, "/home/runner", "--bind", l.Workspace, "/workspace"} |
| 50 | for _, m := range l.Policy.Mounts { |
| 51 | args = append(args, "--ro-bind", m.Source, m.Target) |
| 52 | } |
| 53 | args = append(args, "--chdir", "/workspace", "--clearenv", "--setenv", "PATH", "/usr/bin:/bin:/opt", "--setenv", "HOME", "/home/runner", "--setenv", "CODEX_HOME", "/home/runner", "--setenv", "TMPDIR", "/tmp", "--setenv", "LANG", "C.UTF-8") |
| 54 | args = append(args, argv...) |
| 55 | cmd := exec.CommandContext(ctx, "bwrap", args...) |
| 56 | p := &offlineCodexProcess{done: make(chan struct{})} |
| 57 | var err error |
| 58 | if p.in, err = cmd.StdinPipe(); err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | if p.out, err = cmd.StdoutPipe(); err != nil { |
| 62 | p.in.Close() |
| 63 | return nil, err |
| 64 | } |
| 65 | if err = cmd.Start(); err != nil { |
| 66 | p.in.Close() |
| 67 | p.out.Close() |
| 68 | return nil, err |
| 69 | } |
| 70 | go func() { _ = cmd.Wait(); p.exit.Code = cmd.ProcessState.ExitCode(); close(p.done) }() |
| 71 | h.t.Cleanup(func() { _ = cmd.Process.Kill(); <-p.done; p.in.Close(); p.out.Close() }) |
| 72 | return p, nil |
| 73 | } |
| 74 | |
| 75 | func TestCodexEmptyHomeReplay(t *testing.T) { |
| 76 | testci.Tools(t, "try-bin", "bwrap", "python3") |
| 77 | home, err := os.UserHomeDir() |
| 78 | if err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | bin := filepath.Join(home, ".local/share/equipo/try/bin") |
| 82 | // The official build for this machine: the certified pins on amd64. |
| 83 | executable, codeModeHost, ok := ReleasePins(runtime.GOARCH) |
| 84 | if !ok { |
| 85 | t.Fatalf("no pinned Codex 0.165.0 build for %s", runtime.GOARCH) |
| 86 | } |
| 87 | for name, want := range map[string]string{"codex": executable, "codex-code-mode-host": codeModeHost} { |
| 88 | f, err := os.Open(filepath.Join(bin, name)) |
| 89 | if os.IsNotExist(err) { |
| 90 | testci.Absent(t, "pinned Codex 0.165.0 binaries absent; empty-home replay requires the local pinned installation") |
| 91 | } |
| 92 | if err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | sum := sha256.New() |
| 96 | _, err = io.Copy(sum, f) |
| 97 | f.Close() |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | if hex.EncodeToString(sum.Sum(nil)) != want { |
| 102 | t.Fatalf("pinned %s digest differs", name) |
| 103 | } |
| 104 | } |
| 105 | for _, action := range []string{"implement", "coordinate"} { |
| 106 | t.Run(action, func(t *testing.T) { |
| 107 | f := fixtureFor(t) |
| 108 | // This source is a newly-created test fixture, containing no login. |
| 109 | source := filepath.Join(filepath.Dir(f.interactive), "dedicated", "auth.json") |
| 110 | if err := os.WriteFile(source, []byte(`{"auth_mode":"apikey"}`), 0600); err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | in := f.inputs |
| 114 | in.Action = action |
| 115 | profile, _ := provider.Profile(action) |
| 116 | in.Tools = profile.MCPTools() |
| 117 | for i := range in.RuntimeFiles { |
| 118 | if in.RuntimeFiles[i].Target != "/opt/equipo-tool-proxy" { |
| 119 | in.RuntimeFiles[i].Source = filepath.Join(bin, filepath.Base(in.RuntimeFiles[i].Target)) |
| 120 | } |
| 121 | } |
| 122 | // No thread starts, so the tool proxy is never invoked. |
| 123 | in, err = PolicyInputs(f.lease.Binding(), in, f.launch.Selection.Model) |
| 124 | if err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | f.launch.Policy, err = provider.BuildPolicy(f.lease.Binding(), in) |
| 128 | if err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | f.l, err = New(offlineCodexHost{t}, fakeCapture{}) |
| 132 | if err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | f.l.digest, f.l.hostDigest = executable, codeModeHost |
| 136 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 137 | defer cancel() |
| 138 | _, err = f.l.Open(ctx, f.launch) |
| 139 | if !errors.Is(err, provider.ErrIdentity) || errors.Is(err, provider.ErrContainment) { |
| 140 | t.Fatalf("expected missing account from real Open, got %v", err) |
| 141 | } |
| 142 | t.Logf("real %s Open: %v", action, err) |
| 143 | // Missing account returns before config/read in Open. Check the same |
| 144 | // production policy on a second unauthenticated process, without a turn. |
| 145 | err = f.lease.Preflight(ctx, func(ctx context.Context, b provider.Binding) (provider.Identity, error) { |
| 146 | probeCtx, stop := context.WithCancel(ctx) |
| 147 | defer stop() |
| 148 | launch := f.launch |
| 149 | launch.Binding = b |
| 150 | proc, _, rpc, e := f.l.start(probeCtx, launch, func(provider.Frame) error { return nil }) |
| 151 | defer func() { |
| 152 | if rpc != nil { |
| 153 | rpc.close() |
| 154 | } |
| 155 | stop() |
| 156 | waitProcess(proc) |
| 157 | if rpc != nil { |
| 158 | <-rpc.done |
| 159 | } |
| 160 | }() |
| 161 | if e != nil { |
| 162 | return provider.Identity{}, e |
| 163 | } |
| 164 | if e = initialize(probeCtx, rpc); e != nil { |
| 165 | return provider.Identity{}, e |
| 166 | } |
| 167 | if e = readConfig(probeCtx, rpc, launch.Policy); e != nil { |
| 168 | return provider.Identity{}, e |
| 169 | } |
| 170 | return readIdentity(probeCtx, rpc) |
| 171 | }) |
| 172 | if !errors.Is(err, provider.ErrIdentity) { |
| 173 | t.Fatalf("readback probe: %v", err) |
| 174 | } |
| 175 | for _, name := range []string{"skills", ".tmp/plugins.sha", ".tmp/plugins"} { |
| 176 | if _, err = os.Lstat(filepath.Join(f.lease.Binding().Home, name)); !os.IsNotExist(err) { |
| 177 | t.Fatalf("disabled startup artifact %s: %v", name, err) |
| 178 | } |
| 179 | } |
| 180 | // Only make the fixture ready after all real processes have exited, so |
| 181 | // ValidatePolicy can check their actual startup files. No CLI uses this |
| 182 | // synthetic identity and no credentials or successful Open are fabricated. |
| 183 | if err = f.lease.Preflight(ctx, func(context.Context, provider.Binding) (provider.Identity, error) { return f.lease.Identity(), nil }); err != nil { |
| 184 | t.Fatal(err) |
| 185 | } |
| 186 | filepath.WalkDir(f.lease.Binding().Home, func(path string, d fs.DirEntry, e error) error { |
| 187 | if e == nil && strings.Contains(path, "arg0") { |
| 188 | r, _ := filepath.Rel(f.lease.Binding().Home, path) |
| 189 | t.Logf("runtime: %s %s", r, d.Type()) |
| 190 | } |
| 191 | return e |
| 192 | }) |
| 193 | if err = f.lease.ValidatePolicy(ctx); err != nil { |
| 194 | t.Fatalf("real runtime tree rejected: %v", err) |
| 195 | } |
| 196 | }) |
| 197 | } |
| 198 | } |
| 199 | |