| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/event" |
| 11 | "reasonix/internal/provider" |
| 12 | "reasonix/internal/session" |
| 13 | "reasonix/internal/store" |
| 14 | "reasonix/internal/tool" |
| 15 | ) |
| 16 | |
| 17 | // TestConcurrentControllersShareOneLogWithoutRecoveryCopies covers the short |
| 18 | // in-process overlap used by runtime replacement. The durable v3 history stays |
| 19 | // linear; it no longer manufactures same-log heads for each controller. |
| 20 | func TestConcurrentControllersShareOneLogWithoutRecoveryCopies(t *testing.T) { |
| 21 | dir := t.TempDir() |
| 22 | path := filepath.Join(dir, "shared.jsonl") |
| 23 | const systemPrompt = "SYS" |
| 24 | reply := [][]provider.Chunk{{{Type: provider.ChunkText, Text: "ok"}, {Type: provider.ChunkDone}}} |
| 25 | |
| 26 | provA := &recordingProvider{streams: reply} |
| 27 | execA := agent.New(provA, tool.NewRegistry(), agent.NewSession(systemPrompt), agent.Options{}, event.Discard) |
| 28 | ctrlA := newOwnedTestController(t, Options{Runner: execA, Executor: execA, SystemPrompt: systemPrompt, SessionDir: dir, SessionPath: path, Label: "a", Sink: event.Discard}) |
| 29 | if err := ctrlA.RunTurn(context.Background(), "first from A"); err != nil { |
| 30 | t.Fatalf("A first turn: %v", err) |
| 31 | } |
| 32 | if err := ctrlA.Snapshot(); err != nil { |
| 33 | t.Fatalf("A snapshot: %v", err) |
| 34 | } |
| 35 | |
| 36 | loaded, err := agent.LoadSession(path) |
| 37 | if err != nil { |
| 38 | t.Fatalf("LoadSession: %v", err) |
| 39 | } |
| 40 | provB := &recordingProvider{streams: reply} |
| 41 | execB := agent.New(provB, tool.NewRegistry(), agent.NewSession(systemPrompt), agent.Options{}, event.Discard) |
| 42 | ctrlB := newOwnedTestController(t, Options{Runner: execB, Executor: execB, SystemPrompt: systemPrompt, SessionDir: dir, SessionPath: path, Label: "b", Sink: event.Discard}) |
| 43 | ctrlB.Resume(loaded, path) |
| 44 | |
| 45 | if err := ctrlA.RunTurn(context.Background(), "second from A"); err != nil { |
| 46 | t.Fatalf("A second turn: %v", err) |
| 47 | } |
| 48 | if err := ctrlB.RunTurn(context.Background(), "second from B"); err != nil { |
| 49 | t.Fatalf("B turn: %v", err) |
| 50 | } |
| 51 | if err := ctrlA.Snapshot(); err != nil { |
| 52 | t.Fatalf("A snapshot: %v", err) |
| 53 | } |
| 54 | if err := ctrlB.Snapshot(); err != nil { |
| 55 | t.Fatalf("B snapshot: %v", err) |
| 56 | } |
| 57 | |
| 58 | entries, err := os.ReadDir(dir) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | for _, entry := range entries { |
| 63 | if store.IsSessionTranscriptName(entry.Name()) && entry.Name() != "shared.jsonl" { |
| 64 | t.Fatalf("concurrent controllers created a transcript copy: %s", entry.Name()) |
| 65 | } |
| 66 | } |
| 67 | commits, err := session.Replay(sessionDirectory(path), nil) |
| 68 | if err != nil { |
| 69 | t.Fatalf("Replay v3: %v", err) |
| 70 | } |
| 71 | turnEnds := 0 |
| 72 | var kinds []string |
| 73 | for _, commit := range commits { |
| 74 | for _, event := range commit.Events { |
| 75 | kinds = append(kinds, event.Kind) |
| 76 | if event.Kind == "turn/end" { |
| 77 | turnEnds++ |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | if turnEnds != 3 { |
| 82 | t.Fatalf("linear v3 turn endings = %d, want 3; kinds=%v", turnEnds, kinds) |
| 83 | } |
| 84 | if ctrlA.SessionPath() != path || ctrlB.SessionPath() != path { |
| 85 | t.Fatalf("controllers moved off the shared path: %q %q", ctrlA.SessionPath(), ctrlB.SessionPath()) |
| 86 | } |
| 87 | // system + three complete user/assistant turns come from the shared typed |
| 88 | // event projection for both short-lived controller generations. |
| 89 | if len(ctrlB.History()) != 7 || len(ctrlA.History()) != 7 { |
| 90 | t.Fatalf("histories A=%d B=%d", len(ctrlA.History()), len(ctrlB.History())) |
| 91 | } |
| 92 | } |
| 93 |