oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package runner
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "os"
8
9 "amadan.net/oficina/equipo/internal/release"
10 "amadan.net/oficina/equipo/internal/runner/provider"
11 "amadan.net/oficina/equipo/internal/runner/workspace"
12 "amadan.net/oficina/equipo/internal/runnerproto"
13)
14
15func (r *Runtime) constructDirect(ctx context.Context, s provider.Selection, work, step string) (provider.Launch, func() error, error) {
16 b, _, err := r.binding(s)
17 if err != nil {
18 return provider.Launch{}, nil, err
19 }
20 binding, err := r.cfg.DirectBinding(b)
21 if err != nil {
22 return provider.Launch{}, nil, err
23 }
24 if err = r.verifyRelease(); err != nil {
25 return provider.Launch{}, nil, err
26 }
27 lease, err := provider.OpenAccountWithCLI(ctx, binding, provider.LoginOptions{ClaudePath: r.cfg.ClaudePath, CodexPath: r.cfg.CodexPath})
28 if err != nil {
29 return provider.Launch{}, nil, err
30 }
31 return r.constructDirectLease(ctx, s, work, step, lease)
32}
33func (r *Runtime) constructDirectLease(ctx context.Context, s provider.Selection, work, step string, lease *provider.AccountLease) (provider.Launch, func() error, error) {
34 policyScratch, err := os.MkdirTemp(r.cfg.ScratchRoot, "policy-")
35 if err != nil {
36 return provider.Launch{}, lease.Close, err
37 }
38 cleanup := func() error { return errors.Join(lease.Close(), os.RemoveAll(policyScratch)) }
39 binding := lease.Binding()
40 in, err := PolicyInputs(ctx, r.cfg, s, binding, work, step, policyScratch)
41 if err != nil {
42 return provider.Launch{}, cleanup, err
43 }
44 // PolicyInputs names the provider's own hosts and no auth host.
45 policy, err := provider.BuildPolicy(binding, in)
46 if err != nil {
47 return provider.Launch{}, cleanup, err
48 }
49 return provider.Launch{Workspace: work, Selection: provider.Selection{ConnectionID: s.ConnectionID, Provider: s.Provider, Family: s.Family, AccountID: s.AccountID, Model: s.Model}, Binding: binding, Policy: policy}, cleanup, nil
50}
51
52func (r *Runtime) directLaunch(ctx context.Context, c RunClaim, ws workspace.Workspace, proxy *ToolProxy) (provider.Launch, func() error, error) {
53 action := c.Brief.Step.Action
54 if action == "" {
55 action = c.Assignment.StepKey
56 }
57 if action == "scope" && c.Brief.Target.Kind == "project_read" {
58 action = "recommend"
59 }
60 // Only the owner's own confined route runs: no release certifies another.
61 owner := r.ownerSelection(c.Assignment.Selection)
62 if c.Assignment.Selection.Isolation.Mode != "" && !owner {
63 return provider.Launch{}, nil, provider.ErrIdentity
64 }
65 if _, err := provider.Profile(action); err != nil {
66 return provider.Launch{}, nil, err
67 }
68 if !owner {
69 return provider.Launch{}, nil, fmt.Errorf("%w: %s/%s", provider.ErrUncertified, c.Assignment.Selection.Provider, action)
70 }
71 l, cleanup, err := r.constructDirect(ctx, selection(c.Assignment), ws.Path, action)
72 if err != nil {
73 return l, cleanup, err
74 }
75 if r.verifyRelease() != nil || c.Assignment.Selection.Isolation.Status != "confined" {
76 return l, cleanup, provider.ErrPolicy
77 }
78 l.Mode = c.Assignment.Selection.Isolation.Mode
79 l.AttemptKey = attemptKey(c.Scope, c.Handle) + "/model"
80 l.ToolSocket = proxy.SocketPath
81 return l, cleanup, nil
82}
83
84func (r *Runtime) coordinatorLaunch(ctx context.Context, c runnerproto.CoordinatorClaim, proxy *ToolProxy) (provider.Launch, func() error, error) {
85 owner := r.ownerSelection(c.Brief.Selection) && c.Binding.Person == c.Brief.Selection.Person
86 if c.Brief.Selection.Isolation.Mode != "" && !owner {
87 return provider.Launch{}, nil, provider.ErrIdentity
88 }
89 if !owner {
90 return provider.Launch{}, nil, fmt.Errorf("%w: %s/coordinate", provider.ErrUncertified, c.Brief.Selection.Provider)
91 }
92 work, err := os.MkdirTemp(r.cfg.WorkspaceRoot, "coordinator-")
93 if err != nil {
94 return provider.Launch{}, nil, err
95 }
96 sel := c.Brief.Selection
97 l, release, err := r.constructDirect(ctx, provider.Selection{ConnectionID: sel.ConnectionID, Provider: sel.Provider, Family: sel.Family, Model: sel.Model, AccountID: sel.AccountID}, work, "coordinate")
98 cleanup := func() error {
99 if release != nil {
100 if err := release(); err != nil {
101 return err
102 }
103 }
104 return os.RemoveAll(work)
105 }
106 if err != nil {
107 return l, cleanup, err
108 }
109 if r.verifyRelease() != nil || sel.Isolation.Status != "confined" {
110 return l, cleanup, provider.ErrPolicy
111 }
112 l.Mode = sel.Isolation.Mode
113 l.AttemptKey = attemptKey(c.Scope, c.Handle) + "/coordinator"
114 l.ToolSocket = proxy.SocketPath
115 return l, cleanup, nil
116}
117
118func (r *Runtime) discoverNamespaces(ctx context.Context) ([]runnerproto.ConnectionReport, error) {
119 bs, err := loadDirectBindings(r.cfg.AccountBindingsPath)
120 if err != nil {
121 return nil, err
122 }
123 reports := make([]runnerproto.ConnectionReport, 0, len(bs))
124 manifest := release.BuildManifest()
125 installed := r.verifyRelease() == nil
126 for _, b := range bs {
127 report, err := ownerConnectionReport(ctx, b, manifest, installed, func(action string) (provider.Launch, func() error, error) {
128 work, err := os.MkdirTemp(r.cfg.WorkspaceRoot, "preflight-")
129 if err != nil {
130 return provider.Launch{}, nil, err
131 }
132 l, cleanup, err := r.constructDirect(ctx, provider.Selection{ConnectionID: b.LocalID, Provider: b.Provider, Family: b.Family, Model: b.Model, AccountID: b.AccountID}, work, action)
133 return l, func() error {
134 var e error
135 if cleanup != nil {
136 e = cleanup()
137 }
138 return errors.Join(e, os.RemoveAll(work))
139 }, err
140 }, func(l provider.Launch) error {
141 lease, err := l.Binding.AccountLease()
142 if err != nil {
143 return err
144 }
145 fingerprint, err := lease.PreflightFingerprint(ctx)
146 if err != nil {
147 return err
148 }
149 return r.cachedPreflight(ctx, b.ID, runnerproto.EventHash([]string{fingerprint, l.Policy.Hash}), func() error { _, e := r.launchers[b.Provider].Preflight(ctx, l.Binding, l.Policy); return e })
150 })
151 if err != nil {
152 return nil, err
153 }
154 if err = r.journal.prunePreflights(ctx, r.namespaces.Forget); err != nil {
155 return nil, err
156 }
157 if report.UnavailableReason == "account.sign_in_again" {
158 report.SignInCommand = provider.SignInCommand(b.Provider, b.ProviderHome, provider.LoginOptions{ClaudePath: r.cfg.ClaudePath, CodexPath: r.cfg.CodexPath})
159 }
160 reports = append(reports, report)
161 }
162 return reports, nil
163}
164
165func (r *Runtime) verifyRelease() error {
166 return release.BuildManifest().VerifyInstalled(map[string]string{"claude": r.cfg.ClaudePath, "codex": r.cfg.CodexPath, "codex-code-mode-host": r.cfg.CodexCodeModeHostPath, "bwrap": r.cfg.bwrap(), "equipo-tool-proxy": r.cfg.toolProxy()})
167}
168
169func (r *Runtime) ownerSelection(s runnerproto.Selection) bool {
170 if r.api == nil {
171 return false
172 }
173 r.api.mu.Lock()
174 defer r.api.mu.Unlock()
175 return ownerRoute(r.api.bearer.Person, r.api.bearer.RunnerID, s)
176}
177