package runner import ( "bytes" "context" "encoding/json" "strings" "sync/atomic" "testing" "time" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" ) func TestToolProxyCapabilityConcurrentBind(t *testing.T) { for _, stage := range []string{"startup", "between-turns"} { t.Run(stage, func(t *testing.T) { b := directToolFixture(t) session := b.Session if stage == "startup" { b.Session = provider.Session{} b.TurnID = "" } _, err := b.Journal.db.Exec("UPDATE direct_turns SET state='completed'") mustJournal(t, err) entered, release := make(chan struct{}), make(chan struct{}) var calls atomic.Int32 b.Current = func(ctx context.Context) error { if calls.Add(1) == 2 { close(entered) select { case <-release: case <-ctx.Done(): return ctx.Err() } } return nil } ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() p, err := OpenToolProxy(ctx, b) mustJournal(t, err) defer p.Close() done := make(chan error, 1) workerDone := make(chan struct{}) go func() { defer close(workerDone) done <- proxyPingR2(ctx, p) }() defer func() { cancel(); <-workerDone }() select { case <-entered: case <-ctx.Done(): t.Fatal("capability authority check did not start") } mustJournal(t, p.BindTurn(session, "next")) close(release) err = <-done if err == nil { t.Fatal("changed binding accepted by in-flight capability check") } p.broker.mu.Lock() invalid := p.broker.invalid p.broker.mu.Unlock() if invalid { t.Fatal("concurrent BindTurn permanently invalidated proxy") } mustJournal(t, proxyPingR2(ctx, p)) }) } } func proxyPingR2(ctx context.Context, p *ToolProxy) error { var out bytes.Buffer if err := ServeToolProxy(ctx, strings.NewReader("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"ping\"}\n"), &out, p.SocketPath); err != nil { return err } var reply mcpResponse if err := json.Unmarshal(out.Bytes(), &reply); err != nil { return err } if reply.Error != nil || reply.Result == nil { return runnerproto.ErrInvalid } return nil } func TestDirectToolStaleTurnKeepsProxy(t *testing.T) { for _, state := range []string{"completed"} { t.Run(state, func(t *testing.T) { ctx := context.Background() b := directToolFixture(t) p, err := OpenToolProxy(ctx, b) mustJournal(t, err) defer p.Close() _, err = b.Journal.db.Exec("UPDATE direct_turns SET state=? WHERE turn=?", state, b.TurnID) mustJournal(t, err) args := json.RawMessage(`{"question":"Which?"}`) if err := p.submit(ctx, "ask_person", "stale", args); err == nil { t.Fatal("stale turn tool call accepted") } rows, err := b.Journal.ToolSubmissions(ctx, 10) mustJournal(t, err) if len(rows) != 0 { t.Fatal("stale call was journaled", rows) } if err := p.BindTurn(b.Session, "next"); err != nil { t.Fatalf("stale call disabled next turn: %v", err) } turn := provider.Turn{ID: "next", AdmissionID: "next", Prompt: "next"} mustJournal(t, b.Journal.BeginTurn(ctx, b.Session, turn)) mustJournal(t, b.Journal.consumeTurn(ctx, b.Session.AttemptKey, b.Session.ID, "next")) mustJournal(t, p.submit(ctx, "ask_person", "fresh", args)) rows, err = b.Journal.ToolSubmissions(ctx, 10) mustJournal(t, err) if len(rows) != 1 || rows[0].RequestID != "fresh" { t.Fatal("new turn call was not recorded", rows) } }) } } func TestToolProxyAuthorityLossPermanent(t *testing.T) { for _, operation := range []string{"capability", "submit"} { t.Run(operation, func(t *testing.T) { ctx := context.Background() b := directToolFixture(t) var revoked atomic.Bool b.Current = func(context.Context) error { if revoked.Load() { return runnerproto.ErrInvalid } return nil } p, err := OpenToolProxy(ctx, b) mustJournal(t, err) defer p.Close() call := func() error { if operation == "capability" { return proxyPingR2(ctx, p) } return p.submit(ctx, "ask_person", "r", json.RawMessage(`{"question":"Which?"}`)) } revoked.Store(true) if call() == nil { t.Fatal("lost authority accepted") } revoked.Store(false) if call() == nil { t.Fatal("lost authority revived after Current recovered") } }) } }