package pi import ( "crypto/rand" "crypto/sha256" "encoding/hex" "errors" "io" "os" "path/filepath" "runtime" "strings" "amadan.net/oficina/equipo/internal/runner/provider" ) // Install is where a runner's Pi is: the pinned Node program and the // installed node_modules tree, with the digests a launch requires // (Pins for this platform; a test passes its fake's). type Install struct { Node, Tree, NodeSHA256, TreeSHA256 string } // Pinned fills the digests from this platform's pins. func (i Install) Pinned() (Install, bool) { tree, node, ok := Pins(runtime.GOARCH) if !ok { return i, false } if i.NodeSHA256 == "" { i.NodeSHA256 = node.BinarySHA256 } if i.TreeSHA256 == "" { i.TreeSHA256 = tree } return i, i.Node != "" && i.Tree != "" } // Paths are an install root's Node and tree as the installer lays them // out: /node/bin/node and /pi/node_modules. func Paths(root string) (node, tree string) { return filepath.Join(root, "node", "bin", "node"), filepath.Join(root, "pi", "node_modules") } // Run is one Pi launch's inputs, before the policy is built. type Run struct { Install Bwrap, ToolProxy, Scratch string GoRoot, ModuleCache string Workspace, Action string Provider, Model string Equipo []string Resume string EgressHosts []string ExtraCA string } // Prepare makes the attempt's runtime home (under scratchRoot/homes, // 0700: the containment mounts only a home there), writes Pi's config // into its agent directory and Equipo's extension into the policy // scratch, and returns the policy inputs. Cleanup removes the home. func Prepare(scratchRoot string, r Run) (provider.PiPolicyInputs, func(), error) { homes := filepath.Join(scratchRoot, "homes") if err := os.MkdirAll(homes, 0700); err != nil { return provider.PiPolicyInputs{}, nil, err } var b [6]byte rand.Read(b[:]) home := filepath.Join(homes, "pi-"+hex.EncodeToString(b[:])) if err := os.Mkdir(home, 0700); err != nil { return provider.PiPolicyInputs{}, nil, err } goLease := &provider.GoLease{} cleanup := func() { goLease.Close(); os.RemoveAll(home) } for _, d := range []string{"agent", "sessions"} { if err := os.Mkdir(filepath.Join(home, d), 0700); err != nil { cleanup() return provider.PiPolicyInputs{}, nil, err } } ext := filepath.Join(r.Scratch, "equipo.ts") if err := os.WriteFile(ext, Extension(), 0600); err != nil { cleanup() return provider.PiPolicyInputs{}, nil, err } shell := filepath.Join(r.Scratch, "bash") if err := os.WriteFile(shell, provider.PiShellSource(), 0700); err != nil { cleanup() return provider.PiPolicyInputs{}, nil, err } keyEnv := provider.PiKeyEnvs[r.Provider] in := provider.PiPolicyInputs{Node: r.Node, NodeSHA256: r.NodeSHA256, Tree: r.Tree, TreeSHA256: r.TreeSHA256, Bwrap: r.Bwrap, ToolProxy: r.ToolProxy, Extension: ext, Shell: shell, GoRoot: r.GoRoot, ModuleCache: r.ModuleCache, GoLease: goLease, Home: home, Workspace: r.Workspace, Action: r.Action, Provider: r.Provider, Model: r.Model, KeyEnv: keyEnv, Tools: Tools(r.Action, r.Equipo), Resume: r.Resume, EgressHosts: r.EgressHosts, Models: Models(r.Provider, r.Model), Settings: Settings, MCP: MCP, ExtraCA: r.ExtraCA} if err := provider.WritePiConfig(in); err != nil { cleanup() return provider.PiPolicyInputs{}, nil, err } return in, cleanup, nil } // ErrNoSession: the session's transcript is not where it should be. var ErrNoSession = errors.New("pi: no such session") func validID(id string) bool { return len(id) >= 8 && len(id) <= 64 && strings.Trim(id, "0123456789abcdefghijklmnopqrstuvwxyz-") == "" } // sessionFile is the one transcript of session id in dir (Pi names it //