| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "strings" |
| 8 | "sync/atomic" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 13 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 14 | ) |
| 15 | |
| 16 | func TestToolProxyCapabilityConcurrentBind(t *testing.T) { |
| 17 | for _, stage := range []string{"startup", "between-turns"} { |
| 18 | t.Run(stage, func(t *testing.T) { |
| 19 | b := directToolFixture(t) |
| 20 | session := b.Session |
| 21 | if stage == "startup" { |
| 22 | b.Session = provider.Session{} |
| 23 | b.TurnID = "" |
| 24 | } |
| 25 | _, err := b.Journal.db.Exec("UPDATE direct_turns SET state='completed'") |
| 26 | mustJournal(t, err) |
| 27 | entered, release := make(chan struct{}), make(chan struct{}) |
| 28 | var calls atomic.Int32 |
| 29 | b.Current = func(ctx context.Context) error { |
| 30 | if calls.Add(1) == 2 { |
| 31 | close(entered) |
| 32 | select { |
| 33 | case <-release: |
| 34 | case <-ctx.Done(): |
| 35 | return ctx.Err() |
| 36 | } |
| 37 | } |
| 38 | return nil |
| 39 | } |
| 40 | ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) |
| 41 | defer cancel() |
| 42 | p, err := OpenToolProxy(ctx, b) |
| 43 | mustJournal(t, err) |
| 44 | defer p.Close() |
| 45 | done := make(chan error, 1) |
| 46 | workerDone := make(chan struct{}) |
| 47 | go func() { |
| 48 | defer close(workerDone) |
| 49 | done <- proxyPingR2(ctx, p) |
| 50 | }() |
| 51 | defer func() { cancel(); <-workerDone }() |
| 52 | select { |
| 53 | case <-entered: |
| 54 | case <-ctx.Done(): |
| 55 | t.Fatal("capability authority check did not start") |
| 56 | } |
| 57 | mustJournal(t, p.BindTurn(session, "next")) |
| 58 | close(release) |
| 59 | err = <-done |
| 60 | if err == nil { |
| 61 | t.Fatal("changed binding accepted by in-flight capability check") |
| 62 | } |
| 63 | p.broker.mu.Lock() |
| 64 | invalid := p.broker.invalid |
| 65 | p.broker.mu.Unlock() |
| 66 | if invalid { |
| 67 | t.Fatal("concurrent BindTurn permanently invalidated proxy") |
| 68 | } |
| 69 | mustJournal(t, proxyPingR2(ctx, p)) |
| 70 | }) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func proxyPingR2(ctx context.Context, p *ToolProxy) error { |
| 75 | var out bytes.Buffer |
| 76 | if err := ServeToolProxy(ctx, strings.NewReader("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"ping\"}\n"), &out, p.SocketPath); err != nil { |
| 77 | return err |
| 78 | } |
| 79 | var reply mcpResponse |
| 80 | if err := json.Unmarshal(out.Bytes(), &reply); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | if reply.Error != nil || reply.Result == nil { |
| 84 | return runnerproto.ErrInvalid |
| 85 | } |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | func TestDirectToolStaleTurnKeepsProxy(t *testing.T) { |
| 90 | for _, state := range []string{"completed"} { |
| 91 | t.Run(state, func(t *testing.T) { |
| 92 | ctx := context.Background() |
| 93 | b := directToolFixture(t) |
| 94 | p, err := OpenToolProxy(ctx, b) |
| 95 | mustJournal(t, err) |
| 96 | defer p.Close() |
| 97 | _, err = b.Journal.db.Exec("UPDATE direct_turns SET state=? WHERE turn=?", state, b.TurnID) |
| 98 | mustJournal(t, err) |
| 99 | args := json.RawMessage(`{"question":"Which?"}`) |
| 100 | if err := p.submit(ctx, "ask_person", "stale", args); err == nil { |
| 101 | t.Fatal("stale turn tool call accepted") |
| 102 | } |
| 103 | rows, err := b.Journal.ToolSubmissions(ctx, 10) |
| 104 | mustJournal(t, err) |
| 105 | if len(rows) != 0 { |
| 106 | t.Fatal("stale call was journaled", rows) |
| 107 | } |
| 108 | if err := p.BindTurn(b.Session, "next"); err != nil { |
| 109 | t.Fatalf("stale call disabled next turn: %v", err) |
| 110 | } |
| 111 | turn := provider.Turn{ID: "next", AdmissionID: "next", Prompt: "next"} |
| 112 | mustJournal(t, b.Journal.BeginTurn(ctx, b.Session, turn)) |
| 113 | mustJournal(t, b.Journal.consumeTurn(ctx, b.Session.AttemptKey, b.Session.ID, "next")) |
| 114 | mustJournal(t, p.submit(ctx, "ask_person", "fresh", args)) |
| 115 | rows, err = b.Journal.ToolSubmissions(ctx, 10) |
| 116 | mustJournal(t, err) |
| 117 | if len(rows) != 1 || rows[0].RequestID != "fresh" { |
| 118 | t.Fatal("new turn call was not recorded", rows) |
| 119 | } |
| 120 | }) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func TestToolProxyAuthorityLossPermanent(t *testing.T) { |
| 125 | for _, operation := range []string{"capability", "submit"} { |
| 126 | t.Run(operation, func(t *testing.T) { |
| 127 | ctx := context.Background() |
| 128 | b := directToolFixture(t) |
| 129 | var revoked atomic.Bool |
| 130 | b.Current = func(context.Context) error { |
| 131 | if revoked.Load() { |
| 132 | return runnerproto.ErrInvalid |
| 133 | } |
| 134 | return nil |
| 135 | } |
| 136 | p, err := OpenToolProxy(ctx, b) |
| 137 | mustJournal(t, err) |
| 138 | defer p.Close() |
| 139 | call := func() error { |
| 140 | if operation == "capability" { |
| 141 | return proxyPingR2(ctx, p) |
| 142 | } |
| 143 | return p.submit(ctx, "ask_person", "r", json.RawMessage(`{"question":"Which?"}`)) |
| 144 | } |
| 145 | revoked.Store(true) |
| 146 | if call() == nil { |
| 147 | t.Fatal("lost authority accepted") |
| 148 | } |
| 149 | revoked.Store(false) |
| 150 | if call() == nil { |
| 151 | t.Fatal("lost authority revived after Current recovered") |
| 152 | } |
| 153 | }) |
| 154 | } |
| 155 | } |
| 156 | |