package thin import ( "context" "strings" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runner/provider/opencode" "amadan.net/oficina/equipo/internal/runner/provider/pi" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/wire" ) // OpenCodeOptions is where this runner's OpenCode is // (EQUIPO_OPENCODE_ROOT, laid out as opencode.Paths says) and the pins a // launch requires: this platform's committed ones in production, a // test's fake's in tests (connectors spec §4.2, slice S5). type OpenCodeOptions struct { Install opencode.Install } // harnessOf is the harness that runs a metered binding: its provider's // (wire.APIKeyProviders), Pi for TensorX and OpenCode for OpenCode Go. func harnessOf(b runner.AccountBinding) string { return wire.APIKeyProviders[b.Provider].Harness } // meteredHarness reports whether a metered connection is run by a // harness this runner has code for, the one its provider's table names. func meteredHarness(provider, harness string) bool { kp, ok := wire.APIKeyProviders[provider] return ok && kp.Harness == harness && (harness == wire.HarnessPi || harness == wire.HarnessOpenCode) } // meteredFamily is the model family of a metered connection's model, by // its harness's rule (spec §3, "Families"). func meteredFamily(provider, model string) string { if wire.APIKeyProviders[provider].Harness == wire.HarnessOpenCode { return opencode.Family(model) } return pi.Family(model) } // hasKeptSession reports whether dir keeps a metered session: Pi's // transcript or OpenCode's database (their ids never look alike). func hasKeptSession(dir, id string) bool { return pi.HasSession(dir, id) || opencode.HasSession(dir, id) } // meteredLauncher is a metered harness's launcher, Pi's or OpenCode's. type meteredLauncher interface { provider.Launcher Unknown(provider.Session) map[string]int // OpenWithin starts the harness for the attempt's lifetime and waits // for its session only within startup. OpenWithin(lifetime, startup context.Context, launch provider.Launch) (provider.Session, error) } // meteredPrep is one metered launch prepared: its runtime home made and // its policy certified, with what differs between the harnesses. type meteredPrep struct { harness string policy provider.Policy home string binding provider.Binding // resume is the session the launch names to the containment // (OpenCode's, loaded over ACP); session is the one known before the // launch (Pi's, which Equipo picks). Pi's resume is in its argv. resume, session string cleanup func() launcher func(provider.ProcessHost) meteredLauncher // keep keeps the run's session in dir, with its reported cost so far // (OpenCode's baseline for the next launch; Pi has none). keep func(home, id, dir string, cost float64) (int64, error) family func(model string) string } // prepareMetered prepares the step's metered launch on its binding's // harness, or says why it cannot run: the harness not installed here // (account_unavailable), its configuration or policy refused // (policy_rejected), or the kept session gone (session_gone). func (e *Engineer) prepareMetered(tree workspace.TaskTree, spec specialistRun, scratch string, hosts []string) (meteredPrep, string) { o, b := e.O, spec.binding kept := firstNonEmpty(spec.from, spec.keep) switch harnessOf(b) { case wire.HarnessPi: inst, ok := e.Pi.Install.Pinned() if !ok { return meteredPrep{}, "account_unavailable" } session := spec.resume if session == "" { session = pi.NewSessionID() } in, cleanup, err := pi.Prepare(o.Runner.ScratchRoot, pi.Run{GoRoot: o.Runner.GoRoot, ModuleCache: firstNonEmpty(o.Runner.ModuleCache, homeModuleCache()), Install: inst, Bwrap: o.Runner.Bwrap(), ToolProxy: o.Runner.ToolProxy(), Scratch: scratch, Workspace: tree.Dir, Action: spec.action, Provider: b.Provider, Model: b.Model, Equipo: spec.names, Resume: session, EgressHosts: hosts, ExtraCA: piTestCA()}) if err != nil { e.log("step pi", err) return meteredPrep{}, "policy_rejected" } if spec.resume != "" { if err := pi.RestoreSession(kept, session, in.Home); err != nil { cleanup() return meteredPrep{}, "session_gone" } } policy, err := provider.BuildPiPolicy(in) if err != nil { e.log("step policy", err) cleanup() return meteredPrep{}, "policy_rejected" } return meteredPrep{harness: wire.HarnessPi, policy: policy, home: in.Home, binding: provider.PiBinding(b.LocalID, in.Home, b.CredentialHome), session: session, cleanup: cleanup, launcher: func(h provider.ProcessHost) meteredLauncher { l, _ := pi.New(h); return l }, keep: func(home, id, dir string, _ float64) (int64, error) { return pi.KeepSession(home, id, dir) }, family: pi.Family}, "" case wire.HarnessOpenCode: inst, ok := e.OpenCode.Install.Pinned() if !ok { return meteredPrep{}, "account_unavailable" } if spec.resume != "" && !provider.OpenCodeSessionID(spec.resume) { return meteredPrep{}, "session_gone" } in, cleanup, err := opencode.Prepare(o.Runner.ScratchRoot, opencode.Run{Install: inst, Bwrap: o.Runner.Bwrap(), ToolProxy: o.Runner.ToolProxy(), Workspace: tree.Dir, Action: spec.action, Provider: b.Provider, Model: b.Model, Equipo: spec.names, Resume: spec.resume, EgressHosts: hosts, ExtraCA: piTestCA()}) if err != nil { e.log("step opencode", err) return meteredPrep{}, "policy_rejected" } base := 0.0 if spec.resume != "" { if base, err = opencode.RestoreSession(kept, spec.resume, in.Home); err != nil { cleanup() return meteredPrep{}, "session_gone" } } policy, err := provider.BuildOpenCodePolicy(in) if err != nil { e.log("step policy", err) cleanup() return meteredPrep{}, "policy_rejected" } return meteredPrep{harness: wire.HarnessOpenCode, policy: policy, home: in.Home, binding: provider.OpenCodeBinding(b.LocalID, in.Home, b.CredentialHome), resume: spec.resume, cleanup: cleanup, launcher: func(h provider.ProcessHost) meteredLauncher { l, _ := opencode.New(h); l.CostBase = base; return l }, keep: opencode.KeepSession, family: opencode.Family}, "" } return meteredPrep{}, "account_unavailable" } // meteredInstalled reports whether this runner has the pinned harness a // metered binding runs on. func (e *Engineer) meteredInstalled(b runner.AccountBinding) bool { switch harnessOf(b) { case wire.HarnessPi: _, ok := e.Pi.Install.Pinned() return ok case wire.HarnessOpenCode: _, ok := e.OpenCode.Install.Pinned() return ok } return false } // equipoTool is the step tool a metered harness's tool name stands for: // Pi's mcp__equipo__, OpenCode's equipo_ ("" if none). func equipoTool(name string) string { if t := pi.EquipoTool(name); t != "" { return t } if t, ok := strings.CutPrefix(name, "equipo_"); ok { return t } return "" } // opencodeOptions is the runner's OpenCode: the install at // EQUIPO_OPENCODE_ROOT, held to this platform's committed pins unless a // test pins its fake's. func opencodeOptions(c runner.Config, test OpenCodeOptions) OpenCodeOptions { if test.Install.Binary != "" { return test } if c.OpenCodeRoot == "" { return OpenCodeOptions{} } inst, ok := opencode.At(c.OpenCodeRoot) if !ok { return OpenCodeOptions{} } return OpenCodeOptions{Install: inst} }