package containment import ( "context" "crypto/sha256" "encoding/hex" "encoding/json" "net" "os" "path/filepath" "testing" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/testci" ) // piLaunch is a Pi launch on m: a sealed package tree, the pinned files // (a copy of true stands in for Node), the runtime home under the // scratch root's homes, a key, a tool socket and a workspace. func piLaunch(t *testing.T, m *Manager) (provider.Launch, []string, string) { t.Helper() root := t.TempDir() put := func(name, content string, mode os.FileMode) string { p := filepath.Join(root, name) if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(p, []byte(content), mode); err != nil { t.Fatal(err) } return p } trueBin, err := os.ReadFile("/usr/bin/true") if err != nil { t.Fatal(err) } node := put("node", string(trueBin), 0755) tree := filepath.Join(root, "pi", "node_modules") put("pi/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", "cli", 0644) put("pi/node_modules/dep/index.js", "dep", 0644) if err := provider.SealTree(tree); err != nil { t.Fatal(err) } t.Cleanup(func() { unsealTree(filepath.Dir(tree)) }) sum, err := provider.TreeSHA256(tree) if err != nil { t.Fatal(err) } home := filepath.Join(m.cfg.ScratchRoot, "homes", "pi-home") if err := os.MkdirAll(filepath.Join(home, "agent"), 0700); err != nil { t.Fatal(err) } os.Chmod(filepath.Join(m.cfg.ScratchRoot, "homes"), 0700) os.Chmod(home, 0700) work := filepath.Join(root, "work") os.Mkdir(work, 0700) keyHome := filepath.Join(root, "key") os.Mkdir(keyHome, 0700) put("key/"+provider.TokenFile, "tx-test-key", 0600) sockDir := filepath.Join(root, "tool") os.Mkdir(sockDir, 0700) ln, err := net.Listen("unix", filepath.Join(sockDir, "equipo.sock")) if err != nil { t.Fatal(err) } t.Cleanup(func() { ln.Close() }) os.Chmod(filepath.Join(sockDir, "equipo.sock"), 0600) nodeSum := sha256Hex(trueBin) in := provider.PiPolicyInputs{Node: node, NodeSHA256: nodeSum, Tree: tree, TreeSHA256: sum, Bwrap: "/usr/bin/bwrap", ToolProxy: put("proxy", "proxy", 0755), Extension: put("equipo.ts", string(provider.PiExtensionSource()), 0600), Shell: put("bash", string(provider.PiShellSource()), 0700), Home: home, Workspace: work, Action: "inspect", Provider: "tensorx", Model: "z-ai/glm-5.3", KeyEnv: "TENSORX_API_KEY", Tools: []string{"read", "grep", "find", "ls", "mcp__equipo__submit_review"}, EgressHosts: []string{"api.tensorx.ai"}, Models: json.RawMessage(`{"providers":{"tensorx":{"api":"openai-completions","apiKey":"$TENSORX_API_KEY","baseUrl":"https://api.tensorx.ai/v1","models":[{"id":"z-ai/glm-5.3"}]}}}`), Settings: provider.PiSettings, MCP: json.RawMessage(`{"autoEnableCodemode":false,"mcpServers":{"equipo":{"args":["--socket","/tool/equipo.sock"],"command":"/opt/equipo-tool-proxy","exposure":"direct"}}}`)} if err := provider.WritePiConfig(in); err != nil { t.Fatal(err) } p, err := provider.BuildPiPolicy(in) if err != nil { t.Fatal(err) } l := provider.Launch{ToolSocket: filepath.Join(sockDir, "equipo.sock"), AttemptKey: "pi-swap", Workspace: work, Selection: provider.Selection{Provider: "tensorx", Model: "z-ai/glm-5.3"}, Binding: provider.PiBinding("tensorx-a", home, keyHome), Policy: p} return l, p.PiArgv(), tree } func unsealTree(dir string) { filepath.Walk(dir, func(p string, info os.FileInfo, err error) error { if err == nil && info.IsDir() { os.Chmod(p, 0755) } return nil }) } // A Pi launch mounts the very tree it hashed: replaced after the // attestation and before the mount (a file rewritten, or the whole // directory swapped at its path), the launch is refused; untouched, it // starts. func TestNamespacePiTreeSwapAfterAttestation(t *testing.T) { testci.Tools(t, "bwrap") for _, swap := range []string{"none", "file", "directory"} { t.Run(swap, func(t *testing.T) { m, _ := fixture(t) l, argv, tree := piLaunch(t, m) called := false m.beforePin = func() { called = true switch swap { case "file": f := filepath.Join(tree, "dep", "index.js") os.Chmod(filepath.Dir(f), 0755) os.Chmod(f, 0644) if err := os.WriteFile(f, []byte("changed"), 0644); err != nil { t.Fatal(err) } provider.SealTree(tree) case "directory": other := tree + "-other" if err := os.MkdirAll(filepath.Join(other, "dep"), 0755); err != nil { t.Fatal(err) } os.WriteFile(filepath.Join(other, "dep", "index.js"), []byte("other"), 0644) provider.SealTree(other) os.Chmod(filepath.Dir(tree), 0755) os.Chmod(tree, 0755) if err := os.Rename(tree, tree+"-old"); err != nil { t.Fatal(err) } os.Chmod(other, 0755) if err := os.Rename(other, tree); err != nil { t.Fatal(err) } os.Chmod(tree, 0555) } } p, err := m.Start(context.Background(), l, argv) if p != nil { p.(*process).kill() p.Wait(context.Background()) m.Stop(context.Background(), p.ID()) p.Stdin().Close() p.Stdout().Close() p.Stderr().Close() } if !called { t.Fatal("did not reach the attestation-to-mount boundary") } if swap == "none" && err != nil { t.Fatalf("the untouched tree was refused: %v", err) } if swap != "none" && err == nil { t.Fatal("a tree changed after its attestation was mounted") } }) } } func sha256Hex(raw []byte) string { sum := sha256.Sum256(raw) return hex.EncodeToString(sum[:]) }