oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "strings"
6
7 "amadan.net/oficina/equipo/internal/runner"
8 "amadan.net/oficina/equipo/internal/runner/provider"
9 "amadan.net/oficina/equipo/internal/runner/provider/opencode"
10 "amadan.net/oficina/equipo/internal/runner/provider/pi"
11 "amadan.net/oficina/equipo/internal/runner/workspace"
12 "amadan.net/oficina/equipo/internal/wire"
13)
14
15// OpenCodeOptions is where this runner's OpenCode is
16// (EQUIPO_OPENCODE_ROOT, laid out as opencode.Paths says) and the pins a
17// launch requires: this platform's committed ones in production, a
18// test's fake's in tests (connectors spec §4.2, slice S5).
19type OpenCodeOptions struct {
20 Install opencode.Install
21}
22
23// harnessOf is the harness that runs a metered binding: its provider's
24// (wire.APIKeyProviders), Pi for TensorX and OpenCode for OpenCode Go.
25func harnessOf(b runner.AccountBinding) string { return wire.APIKeyProviders[b.Provider].Harness }
26
27// meteredHarness reports whether a metered connection is run by a
28// harness this runner has code for, the one its provider's table names.
29func meteredHarness(provider, harness string) bool {
30 kp, ok := wire.APIKeyProviders[provider]
31 return ok && kp.Harness == harness && (harness == wire.HarnessPi || harness == wire.HarnessOpenCode)
32}
33
34// meteredFamily is the model family of a metered connection's model, by
35// its harness's rule (spec §3, "Families").
36func meteredFamily(provider, model string) string {
37 if wire.APIKeyProviders[provider].Harness == wire.HarnessOpenCode {
38 return opencode.Family(model)
39 }
40 return pi.Family(model)
41}
42
43// hasKeptSession reports whether dir keeps a metered session: Pi's
44// transcript or OpenCode's database (their ids never look alike).
45func hasKeptSession(dir, id string) bool {
46 return pi.HasSession(dir, id) || opencode.HasSession(dir, id)
47}
48
49// meteredLauncher is a metered harness's launcher, Pi's or OpenCode's.
50type meteredLauncher interface {
51 provider.Launcher
52 Unknown(provider.Session) map[string]int
53 // OpenWithin starts the harness for the attempt's lifetime and waits
54 // for its session only within startup.
55 OpenWithin(lifetime, startup context.Context, launch provider.Launch) (provider.Session, error)
56}
57
58// meteredPrep is one metered launch prepared: its runtime home made and
59// its policy certified, with what differs between the harnesses.
60type meteredPrep struct {
61 harness string
62 policy provider.Policy
63 home string
64 binding provider.Binding
65 // resume is the session the launch names to the containment
66 // (OpenCode's, loaded over ACP); session is the one known before the
67 // launch (Pi's, which Equipo picks). Pi's resume is in its argv.
68 resume, session string
69 cleanup func()
70 launcher func(provider.ProcessHost) meteredLauncher
71 // keep keeps the run's session in dir, with its reported cost so far
72 // (OpenCode's baseline for the next launch; Pi has none).
73 keep func(home, id, dir string, cost float64) (int64, error)
74 family func(model string) string
75}
76
77// prepareMetered prepares the step's metered launch on its binding's
78// harness, or says why it cannot run: the harness not installed here
79// (account_unavailable), its configuration or policy refused
80// (policy_rejected), or the kept session gone (session_gone).
81func (e *Engineer) prepareMetered(tree workspace.TaskTree, spec specialistRun, scratch string, hosts []string) (meteredPrep, string) {
82 o, b := e.O, spec.binding
83 kept := firstNonEmpty(spec.from, spec.keep)
84 switch harnessOf(b) {
85 case wire.HarnessPi:
86 inst, ok := e.Pi.Install.Pinned()
87 if !ok {
88 return meteredPrep{}, "account_unavailable"
89 }
90 session := spec.resume
91 if session == "" {
92 session = pi.NewSessionID()
93 }
94 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()})
95 if err != nil {
96 e.log("step pi", err)
97 return meteredPrep{}, "policy_rejected"
98 }
99 if spec.resume != "" {
100 if err := pi.RestoreSession(kept, session, in.Home); err != nil {
101 cleanup()
102 return meteredPrep{}, "session_gone"
103 }
104 }
105 policy, err := provider.BuildPiPolicy(in)
106 if err != nil {
107 e.log("step policy", err)
108 cleanup()
109 return meteredPrep{}, "policy_rejected"
110 }
111 return meteredPrep{harness: wire.HarnessPi, policy: policy, home: in.Home, binding: provider.PiBinding(b.LocalID, in.Home, b.CredentialHome), session: session, cleanup: cleanup,
112 launcher: func(h provider.ProcessHost) meteredLauncher { l, _ := pi.New(h); return l },
113 keep: func(home, id, dir string, _ float64) (int64, error) { return pi.KeepSession(home, id, dir) }, family: pi.Family}, ""
114 case wire.HarnessOpenCode:
115 inst, ok := e.OpenCode.Install.Pinned()
116 if !ok {
117 return meteredPrep{}, "account_unavailable"
118 }
119 if spec.resume != "" && !provider.OpenCodeSessionID(spec.resume) {
120 return meteredPrep{}, "session_gone"
121 }
122 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()})
123 if err != nil {
124 e.log("step opencode", err)
125 return meteredPrep{}, "policy_rejected"
126 }
127 base := 0.0
128 if spec.resume != "" {
129 if base, err = opencode.RestoreSession(kept, spec.resume, in.Home); err != nil {
130 cleanup()
131 return meteredPrep{}, "session_gone"
132 }
133 }
134 policy, err := provider.BuildOpenCodePolicy(in)
135 if err != nil {
136 e.log("step policy", err)
137 cleanup()
138 return meteredPrep{}, "policy_rejected"
139 }
140 return meteredPrep{harness: wire.HarnessOpenCode, policy: policy, home: in.Home, binding: provider.OpenCodeBinding(b.LocalID, in.Home, b.CredentialHome), resume: spec.resume, cleanup: cleanup,
141 launcher: func(h provider.ProcessHost) meteredLauncher { l, _ := opencode.New(h); l.CostBase = base; return l }, keep: opencode.KeepSession, family: opencode.Family}, ""
142 }
143 return meteredPrep{}, "account_unavailable"
144}
145
146// meteredInstalled reports whether this runner has the pinned harness a
147// metered binding runs on.
148func (e *Engineer) meteredInstalled(b runner.AccountBinding) bool {
149 switch harnessOf(b) {
150 case wire.HarnessPi:
151 _, ok := e.Pi.Install.Pinned()
152 return ok
153 case wire.HarnessOpenCode:
154 _, ok := e.OpenCode.Install.Pinned()
155 return ok
156 }
157 return false
158}
159
160// equipoTool is the step tool a metered harness's tool name stands for:
161// Pi's mcp__equipo__<tool>, OpenCode's equipo_<tool> ("" if none).
162func equipoTool(name string) string {
163 if t := pi.EquipoTool(name); t != "" {
164 return t
165 }
166 if t, ok := strings.CutPrefix(name, "equipo_"); ok {
167 return t
168 }
169 return ""
170}
171
172// opencodeOptions is the runner's OpenCode: the install at
173// EQUIPO_OPENCODE_ROOT, held to this platform's committed pins unless a
174// test pins its fake's.
175func opencodeOptions(c runner.Config, test OpenCodeOptions) OpenCodeOptions {
176 if test.Install.Binary != "" {
177 return test
178 }
179 if c.OpenCodeRoot == "" {
180 return OpenCodeOptions{}
181 }
182 inst, ok := opencode.At(c.OpenCodeRoot)
183 if !ok {
184 return OpenCodeOptions{}
185 }
186 return OpenCodeOptions{Install: inst}
187}
188