| 1 | package coordinator |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 13 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 14 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 15 | ) |
| 16 | |
| 17 | func contextFixture() runnerproto.CoordinatorContext { |
| 18 | return runnerproto.CoordinatorContext{ConversationID: "conversation", Revision: 1, ConfirmedDecisions: []runnerproto.ContextRef{{Source: team.SourceRef{Kind: "answer", ID: "answer", Revision: 1, Hash: team.ContentDigest([]byte("keep decision"))}, Text: "keep decision"}}} |
| 19 | } |
| 20 | func TestCoordinatorContextBounds(t *testing.T) { |
| 21 | c := contextFixture() |
| 22 | for i := 0; i < 100; i++ { |
| 23 | c.Recent = append(c.Recent, runnerproto.MessageRef{ID: fmt.Sprint(i), AuthorKind: "person", Body: strings.Repeat("x", 16384), Sequence: int64(i + 1), Revision: 1}) |
| 24 | } |
| 25 | bounded, err := runnerproto.BoundCoordinatorContext(c) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if len(bounded.Recent) != 2 || bounded.Recent[0].ID != "98" || bounded.Recent[1].ID != "99" { |
| 30 | t.Fatal("history did not trim oldest first") |
| 31 | } |
| 32 | if bounded.Hash == "" || bounded.Hash != runnerproto.CoordinatorContextHash(bounded) { |
| 33 | t.Fatal("snapshot hash mismatch") |
| 34 | } |
| 35 | prompt, err := runnerproto.CoordinatorPrompt(bounded) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | if !strings.Contains(prompt, "keep decision") { |
| 40 | t.Fatal("required decision trimmed") |
| 41 | } |
| 42 | if _, err = runnerproto.CoordinatorTurnIntentPayload("claude", "model", prompt); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | for _, text := range []string{strings.Repeat("a", 48<<10), strings.Repeat("\x00", 15000)} { |
| 46 | if _, err = runnerproto.CoordinatorTurnIntentPayload("claude", "model", text); err != nil && len(text) == 48<<10 { |
| 47 | t.Fatal("48 KiB boundary rejected", err) |
| 48 | } |
| 49 | } |
| 50 | if _, err = runnerproto.CoordinatorTurnIntentPayload("claude", "model", strings.Repeat("a", (48<<10)+1)); err == nil { |
| 51 | t.Fatal("raw cap bypassed") |
| 52 | } |
| 53 | if _, err = runnerproto.CoordinatorTurnIntentPayload("claude", "model", strings.Repeat("\x00", 48<<10)); err == nil { |
| 54 | t.Fatal("escaped frame cap bypassed") |
| 55 | } |
| 56 | for _, n := range []int{9556, 9557} { |
| 57 | _, err := runnerproto.CoordinatorTurnIntentPayload("claude", "model", strings.Repeat("\x00", n)) |
| 58 | if (err != nil) != (n == 9557) { |
| 59 | t.Fatal("frame boundary incorrect", n, err) |
| 60 | } |
| 61 | } |
| 62 | c = contextFixture() |
| 63 | c.Instructions = []runnerproto.ContextRef{{Text: strings.Repeat("x", 49<<10)}} |
| 64 | if _, err = runnerproto.BoundCoordinatorContext(c); !errors.Is(err, runnerproto.ErrPromptTooLarge) { |
| 65 | t.Fatal("oversized required content accepted", err) |
| 66 | } |
| 67 | c = contextFixture() |
| 68 | c.Recent = []runnerproto.MessageRef{{Body: strings.Repeat("\x00", 16000)}} |
| 69 | bounded, err = runnerproto.BoundCoordinatorContext(c) |
| 70 | if !errors.Is(err, runnerproto.ErrPromptTooLarge) { |
| 71 | t.Fatal("escaped admitted request was trimmed", err) |
| 72 | } |
| 73 | c = contextFixture() |
| 74 | c.Retrieved = []runnerproto.ContextRef{{Text: strings.Repeat("x", 48<<10)}} |
| 75 | bounded, err = runnerproto.BoundCoordinatorContext(c) |
| 76 | if err != nil || len(bounded.Retrieved) != 0 { |
| 77 | t.Fatal("optional retrieval blocked turn", err) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | type runtimeTools struct{} |
| 82 | |
| 83 | func (runtimeTools) Call(context.Context, CoordinatorToolCall) (CoordinatorToolResult, error) { |
| 84 | return CoordinatorToolResult{}, nil |
| 85 | } |
| 86 | |
| 87 | type runtimeLauncher struct { |
| 88 | opens, starts, preflights, closes int |
| 89 | events chan provider.Event |
| 90 | launch provider.Launch |
| 91 | hold bool |
| 92 | attached bool |
| 93 | } |
| 94 | |
| 95 | func (l *runtimeLauncher) Preflight(_ context.Context, b provider.Binding, _ provider.Policy) (provider.Identity, error) { |
| 96 | l.preflights++ |
| 97 | return b.Expected, nil |
| 98 | } |
| 99 | func (l *runtimeLauncher) Open(_ context.Context, v provider.Launch) (provider.Session, error) { |
| 100 | l.opens++ |
| 101 | l.launch = v |
| 102 | l.events = make(chan provider.Event, 4) |
| 103 | return provider.Session{ID: fmt.Sprint(l.opens), AttemptKey: v.AttemptKey, Provider: v.Selection.Provider, Model: v.Selection.Model, AccountID: v.Selection.AccountID, Family: v.Selection.Family}, nil |
| 104 | } |
| 105 | func (l *runtimeLauncher) Events(context.Context, provider.Session) (<-chan provider.Event, error) { |
| 106 | l.attached = true |
| 107 | return l.events, nil |
| 108 | } |
| 109 | func (l *runtimeLauncher) StartTurn(_ context.Context, s provider.Session, v provider.Turn) error { |
| 110 | if !l.attached { |
| 111 | return provider.ErrContract |
| 112 | } |
| 113 | l.starts++ |
| 114 | if !l.hold { |
| 115 | l.events <- provider.Event{SessionID: s.ID, TurnID: v.ID, Kind: "turn.completed", Payload: json.RawMessage(`{}`)} |
| 116 | } |
| 117 | return nil |
| 118 | } |
| 119 | func (l *runtimeLauncher) Respond(context.Context, provider.Session, provider.Answer) error { |
| 120 | return nil |
| 121 | } |
| 122 | func (l *runtimeLauncher) Interrupt(context.Context, provider.Session) error { return nil } |
| 123 | func (l *runtimeLauncher) Close(context.Context, provider.Session) error { l.closes++; return nil } |
| 124 | func runtimeClaim(t *testing.T) runnerproto.CoordinatorClaim { |
| 125 | t.Helper() |
| 126 | c, err := runnerproto.BoundCoordinatorContext(contextFixture()) |
| 127 | if err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | return runnerproto.CoordinatorClaim{Binding: runnerproto.CoordinatorBinding{ID: "binding", Generation: 1}, Turn: runnerproto.CoordinatorTurn{ID: "turn", Generation: 1, ContextHash: c.Hash}, Context: c, Brief: runnerproto.RunnerBrief{Selection: runnerproto.Selection{Provider: "claude", AccountID: "account", Family: "claude", Model: "model"}}, Admission: team.AdmissionTicket{}, AttemptDeadline: time.Now().Add(time.Minute)} |
| 131 | } |
| 132 | func runtimeLaunch(c runnerproto.CoordinatorClaim) provider.Launch { |
| 133 | s := c.Brief.Selection |
| 134 | return provider.Launch{AttemptKey: c.Turn.ID, Selection: provider.Selection{Provider: s.Provider, Family: s.Family, AccountID: s.AccountID, Model: s.Model}, Binding: provider.Binding{Provider: s.Provider, AccountID: s.AccountID, Expected: provider.Identity{AccountID: s.AccountID}}, Policy: provider.Policy{Hash: "policy", Manifest: json.RawMessage(`{"Action":"coordinate"}`), ManifestHash: team.ContentDigest([]byte(`{"Action":"coordinate"}`))}} |
| 135 | } |
| 136 | func TestCoordinatorFreshTurnRevalidates(t *testing.T) { |
| 137 | for _, change := range []string{"resume", "model", "provider", "identity", "policy", "tool-policy", "revoked", "generation", "hash"} { |
| 138 | t.Run(change, func(t *testing.T) { |
| 139 | c := runtimeClaim(t) |
| 140 | l := &runtimeLauncher{} |
| 141 | r, err := New(Config{Launcher: l, Tools: runtimeTools{}, OpenLaunch: func(_ context.Context, c runnerproto.CoordinatorClaim) (provider.Launch, error) { |
| 142 | v := runtimeLaunch(c) |
| 143 | if change == "revoked" { |
| 144 | return v, provider.ErrIdentity |
| 145 | } |
| 146 | switch change { |
| 147 | case "tool-policy": |
| 148 | v.Policy.Manifest = json.RawMessage(`{"Action":"implement"}`) |
| 149 | v.Policy.ManifestHash = team.ContentDigest(v.Policy.Manifest) |
| 150 | case "policy": |
| 151 | v.Policy.Hash = "" |
| 152 | case "resume": |
| 153 | v.ResumeSessionID = "old" |
| 154 | case "model": |
| 155 | v.Selection.Model = "other" |
| 156 | case "provider": |
| 157 | v.Selection.Provider = "codex" |
| 158 | case "identity": |
| 159 | v.Binding.AccountID = "other" |
| 160 | } |
| 161 | return v, nil |
| 162 | }, Record: func(context.Context, provider.Event) error { return nil }}) |
| 163 | if err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | if change == "generation" { |
| 167 | c.Turn.Generation++ |
| 168 | } |
| 169 | if change == "hash" { |
| 170 | c.Context.Hash = "forged" |
| 171 | } |
| 172 | if err = r.RunTurn(context.Background(), c); err == nil || l.starts != 0 || l.opens != 0 { |
| 173 | t.Fatal("changed authority dispatched", err) |
| 174 | } |
| 175 | }) |
| 176 | } |
| 177 | l := &runtimeLauncher{} |
| 178 | r, _ := New(Config{Launcher: l, Tools: runtimeTools{}, OpenLaunch: func(_ context.Context, c runnerproto.CoordinatorClaim) (provider.Launch, error) { |
| 179 | return runtimeLaunch(c), nil |
| 180 | }, Record: func(context.Context, provider.Event) error { return nil }}) |
| 181 | for i := 0; i < 2; i++ { |
| 182 | c := runtimeClaim(t) |
| 183 | c.Turn.ID = fmt.Sprint(i) |
| 184 | if err := r.RunTurn(context.Background(), c); err != nil { |
| 185 | t.Fatal(err) |
| 186 | } |
| 187 | } |
| 188 | if l.preflights != 2 || l.closes != 2 || l.starts != 2 { |
| 189 | t.Fatal("fresh turn reused validation or session", l) |
| 190 | } |
| 191 | } |
| 192 | |