| 1 | package coordinator |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "sync" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 10 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 11 | ) |
| 12 | |
| 13 | type Config struct { |
| 14 | Launcher provider.Launcher |
| 15 | Tools ToolClient |
| 16 | OpenLaunch func(context.Context, runnerproto.CoordinatorClaim) (provider.Launch, error) |
| 17 | Record func(context.Context, provider.Event) error |
| 18 | } |
| 19 | type Runtime struct { |
| 20 | cfg Config |
| 21 | mu sync.Mutex |
| 22 | cancel context.CancelFunc |
| 23 | done chan struct{} |
| 24 | stopped bool |
| 25 | closeErr error |
| 26 | } |
| 27 | |
| 28 | func New(c Config) (*Runtime, error) { |
| 29 | if c.Launcher == nil || c.Tools == nil || c.OpenLaunch == nil || c.Record == nil { |
| 30 | return nil, provider.ErrContract |
| 31 | } |
| 32 | return &Runtime{cfg: c}, nil |
| 33 | } |
| 34 | func (r *Runtime) RunTurn(ctx context.Context, c runnerproto.CoordinatorClaim) (err error) { |
| 35 | if c.Binding.Generation < 1 || c.Turn.Generation != c.Binding.Generation || c.Turn.ID == "" || c.Turn.ContextHash != c.Context.Hash || !time.Now().Before(c.AttemptDeadline) { |
| 36 | return provider.ErrIdentity |
| 37 | } |
| 38 | prompt, err := runnerproto.CoordinatorPrompt(c.Context) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | r.mu.Lock() |
| 43 | if r.stopped || r.cancel != nil { |
| 44 | r.mu.Unlock() |
| 45 | return provider.ErrUncertain |
| 46 | } |
| 47 | ctx, cancel := context.WithDeadline(ctx, c.AttemptDeadline) |
| 48 | done := make(chan struct{}) |
| 49 | r.cancel = cancel |
| 50 | r.done = done |
| 51 | r.closeErr = nil |
| 52 | r.mu.Unlock() |
| 53 | defer func() { cancel(); r.mu.Lock(); r.cancel = nil; close(done); r.mu.Unlock() }() |
| 54 | l, err := r.cfg.OpenLaunch(ctx, c) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | sel := c.Brief.Selection |
| 59 | if l.ResumeSessionID != "" || l.AttemptKey == "" || l.Policy.Hash == "" || !l.Policy.Coordinator() || l.Selection.Provider != sel.Provider || l.Selection.Model != sel.Model || l.Selection.AccountID != sel.AccountID || l.Selection.Family != sel.Family || l.Binding.AccountID != sel.AccountID || l.Binding.Provider != sel.Provider { |
| 60 | return provider.ErrIdentity |
| 61 | } |
| 62 | identity, err := r.cfg.Launcher.Preflight(ctx, l.Binding, l.Policy) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | if identity.AccountID != l.Binding.Expected.AccountID || identity.ProviderVersion != l.Binding.Expected.ProviderVersion { |
| 67 | return provider.ErrIdentity |
| 68 | } |
| 69 | s, err := r.cfg.Launcher.Open(ctx, l) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | defer func() { |
| 74 | cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) |
| 75 | defer cancel() |
| 76 | closeErr := r.cfg.Launcher.Close(cleanup, s) |
| 77 | r.mu.Lock() |
| 78 | r.closeErr = closeErr |
| 79 | r.mu.Unlock() |
| 80 | err = errors.Join(err, closeErr) |
| 81 | }() |
| 82 | if err = s.Validate(l); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | events, err := r.cfg.Launcher.Events(ctx, s) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | if err = r.cfg.Record(ctx, provider.Event{SessionID: s.ID, Kind: "session.bound"}); err != nil { |
| 90 | return err |
| 91 | } |
| 92 | if _, err = runnerproto.CoordinatorTurnIntentPayload(sel.Provider, sel.Model, prompt); err != nil { |
| 93 | return err |
| 94 | } |
| 95 | turn := provider.Turn{ID: c.Turn.ID, AdmissionID: c.Turn.ID, Prompt: prompt} |
| 96 | if err = r.cfg.Launcher.StartTurn(ctx, s, turn); err != nil { |
| 97 | return err |
| 98 | } |
| 99 | for { |
| 100 | select { |
| 101 | case <-ctx.Done(): |
| 102 | return ctx.Err() |
| 103 | case e, ok := <-events: |
| 104 | if !ok { |
| 105 | return provider.ErrUncertain |
| 106 | } |
| 107 | if e.SessionID != s.ID || e.TurnID != "" && e.TurnID != turn.ID { |
| 108 | return provider.ErrIdentity |
| 109 | } |
| 110 | if err = r.cfg.Record(ctx, e); err != nil { |
| 111 | return err |
| 112 | } |
| 113 | switch e.Kind { |
| 114 | case "turn.completed": |
| 115 | return nil |
| 116 | case "turn.failed": |
| 117 | return provider.ErrUncertain |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |