| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "path/filepath" |
| 7 | "sync/atomic" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/agent" |
| 11 | "reasonix/internal/agent/testutil" |
| 12 | "reasonix/internal/event" |
| 13 | "reasonix/internal/provider" |
| 14 | "reasonix/internal/tool" |
| 15 | ) |
| 16 | |
| 17 | type manualProtocolProvider struct { |
| 18 | *testutil.MockProvider |
| 19 | entered, release chan struct{} |
| 20 | calls atomic.Int32 |
| 21 | } |
| 22 | |
| 23 | func (p *manualProtocolProvider) RequiresAssistantReasoning() bool { return true } |
| 24 | func (p *manualProtocolProvider) Stream(ctx context.Context, req provider.Request) (<-chan provider.Chunk, error) { |
| 25 | if p.calls.Add(1) == 2 && p.entered != nil { |
| 26 | close(p.entered) |
| 27 | <-p.release |
| 28 | } |
| 29 | return p.MockProvider.Stream(ctx, req) |
| 30 | } |
| 31 | func TestProtocolRecoveryControllerDurabilityAndConcurrentAdmission(t *testing.T) { |
| 32 | p := &manualProtocolProvider{MockProvider: testutil.NewMock("strict", testutil.ErrorTurn(&provider.APIError{Status: 400, Body: `{"model":"deepseek"}`}), testutil.Turn{Text: "done"}), entered: make(chan struct{}), release: make(chan struct{})} |
| 33 | session := agent.NewSession("system") |
| 34 | session.Add(provider.Message{Role: provider.RoleAssistant, Content: "earlier", ReasoningContent: "proof"}) |
| 35 | a := agent.New(p, tool.NewRegistry(), session, agent.Options{}, event.Discard) |
| 36 | dir := t.TempDir() |
| 37 | path := filepath.Join(dir, "session.jsonl") |
| 38 | c := newOwnedTestController(t, Options{Runner: a, Executor: a, SessionDir: dir, SessionPath: path, Sink: event.Discard}) |
| 39 | defer c.Close() |
| 40 | if err := c.RunTurn(context.Background(), "next"); err == nil { |
| 41 | t.Fatal("expected opaque failure") |
| 42 | } |
| 43 | action := c.PendingProtocolRecovery() |
| 44 | if action == nil { |
| 45 | t.Fatal("missing recovery token") |
| 46 | } |
| 47 | loaded := loadDurableSessionProjection(t, path) |
| 48 | var pending bool |
| 49 | for _, m := range loaded.Messages { |
| 50 | r, ok := provider.DecodeProtocolRecovery(m.ProtocolRecovery) |
| 51 | pending = pending || ok && r.State == "pending" |
| 52 | } |
| 53 | if !pending { |
| 54 | t.Fatal("pending not persisted") |
| 55 | } |
| 56 | done := make(chan error, 1) |
| 57 | go func() { done <- c.RunProtocolRecoveryWithAdmission(context.Background(), action.ID, "", nil) }() |
| 58 | <-p.entered |
| 59 | loaded = loadDurableSessionProjection(t, path) |
| 60 | var consumed bool |
| 61 | for _, m := range loaded.Messages { |
| 62 | r, ok := provider.DecodeProtocolRecovery(m.ProtocolRecovery) |
| 63 | consumed = consumed || ok && r.State == "consumed" |
| 64 | } |
| 65 | if !consumed { |
| 66 | t.Fatal("request started before durable consumption") |
| 67 | } |
| 68 | if err := c.RunProtocolRecoveryWithAdmission(context.Background(), action.ID, "", nil); err == nil { |
| 69 | t.Fatal("concurrent duplicate admitted") |
| 70 | } |
| 71 | close(p.release) |
| 72 | if err := <-done; err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if err := c.RunProtocolRecoveryWithAdmission(context.Background(), action.ID, "", nil); !errors.Is(err, agent.ErrProtocolRecoveryUnavailable) { |
| 76 | t.Fatalf("duplicate=%v", err) |
| 77 | } |
| 78 | if p.calls.Load() != 2 { |
| 79 | t.Fatal("duplicate provider invocation") |
| 80 | } |
| 81 | } |
| 82 | func TestParseProtocolRecoveryCommand(t *testing.T) { |
| 83 | id, guidance, ok := ParseProtocolRecoveryCommand("/recover-context token keep completed work") |
| 84 | if !ok || id != "token" || guidance != "keep completed work" { |
| 85 | t.Fatalf("%q %q %v", id, guidance, ok) |
| 86 | } |
| 87 | if _, _, ok := ParseProtocolRecoveryCommand("/recover-contextual"); ok { |
| 88 | t.Fatal("ambiguous command accepted") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestProtocolRecoveryCancelledBeforeAdmissionKeepsToken(t *testing.T) { |
| 93 | p := &manualProtocolProvider{MockProvider: testutil.NewMock("strict", testutil.ErrorTurn(&provider.APIError{Status: 400, Body: `{"model":"deepseek"}`}))} |
| 94 | session := agent.NewSession("system") |
| 95 | session.Add(provider.Message{Role: provider.RoleAssistant, Content: "earlier", ReasoningContent: "proof"}) |
| 96 | a := agent.New(p, tool.NewRegistry(), session, agent.Options{}, event.Discard) |
| 97 | c := newOwnedTestController(t, Options{Runner: a, Executor: a, Sink: event.Discard}) |
| 98 | defer c.Close() |
| 99 | _ = c.RunTurn(context.Background(), "next") |
| 100 | action := c.PendingProtocolRecovery() |
| 101 | if action == nil { |
| 102 | t.Fatal("no token") |
| 103 | } |
| 104 | ctx, cancel := context.WithCancel(context.Background()) |
| 105 | cancel() |
| 106 | _ = c.RunProtocolRecoveryWithAdmission(ctx, action.ID, "", nil) |
| 107 | if p.calls.Load() != 1 || c.PendingProtocolRecovery() == nil { |
| 108 | t.Fatal("cancelled preparation consumed action or called provider") |
| 109 | } |
| 110 | } |
| 111 |