| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/event" |
| 11 | "reasonix/internal/provider" |
| 12 | ) |
| 13 | |
| 14 | func coldResumeFixture(t *testing.T, threshold time.Duration) (*agent.Session, string, *Controller) { |
| 15 | t.Helper() |
| 16 | |
| 17 | dir := t.TempDir() |
| 18 | loaded := &agent.Session{Messages: []provider.Message{ |
| 19 | {Role: provider.RoleSystem, Content: "sys"}, |
| 20 | {Role: provider.RoleUser, Content: "task"}, |
| 21 | {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "1", Name: "grep", Arguments: "{}"}}}, |
| 22 | {Role: provider.RoleTool, ToolCallID: "1", Name: "grep", Content: strings.Repeat("y", 5000)}, |
| 23 | {Role: provider.RoleAssistant, Content: "step done"}, |
| 24 | {Role: provider.RoleUser, Content: "next"}, |
| 25 | {Role: provider.RoleAssistant, Content: "ok"}, |
| 26 | }} |
| 27 | path := agent.NewSessionPath(dir, "test") |
| 28 | if err := loaded.Save(path); err != nil { |
| 29 | t.Fatalf("save: %v", err) |
| 30 | } |
| 31 | if _, err := agent.EnsureBranchMeta(path); err != nil { |
| 32 | t.Fatalf("meta: %v", err) |
| 33 | } |
| 34 | |
| 35 | exec := agent.New(nil, nil, agent.NewSession("sys"), agent.Options{ContextWindow: 1000, RecentKeep: 2, ArchiveDir: dir}, event.Discard) |
| 36 | c := newOwnedTestController(t, Options{Executor: exec, SessionDir: dir, Label: "test"}) |
| 37 | if threshold == 0 { |
| 38 | c.testCacheColdAfter = -1 // force cold |
| 39 | } else { |
| 40 | c.testCacheColdAfter = threshold |
| 41 | } |
| 42 | c.Resume(loaded, path) |
| 43 | return loaded, path, c |
| 44 | } |
| 45 | |
| 46 | func TestColdResumeDoesNotRewriteOrNetwork(t *testing.T) { |
| 47 | loaded, path, c := coldResumeFixture(t, 0) |
| 48 | |
| 49 | msgs := loaded.Snapshot() |
| 50 | if !strings.HasPrefix(msgs[3].Content, "yyy") { |
| 51 | t.Fatalf("cold resume rewrote tool result: %.60q", msgs[3].Content) |
| 52 | } |
| 53 | re, err := agent.LoadSession(path) |
| 54 | if err != nil { |
| 55 | t.Fatalf("reload: %v", err) |
| 56 | } |
| 57 | if !strings.HasPrefix(re.Messages[3].Content, "yyy") { |
| 58 | t.Error("cold resume rewrote the saved transcript") |
| 59 | } |
| 60 | if c.executor.CacheState() != agent.CacheStateCold { |
| 61 | t.Fatalf("cache state = %q, want cold", c.executor.CacheState()) |
| 62 | } |
| 63 | // No network: executor has nil provider; if cold resume called Compact it would panic/fail. |
| 64 | } |
| 65 | |
| 66 | func TestColdResumeAfterClonedHistoryStaysInPlace(t *testing.T) { |
| 67 | dir := t.TempDir() |
| 68 | saved := agent.NewSession("old sys") |
| 69 | saved.Add(provider.Message{Role: provider.RoleUser, Content: "task"}) |
| 70 | saved.Add(provider.Message{Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "1", Name: "grep", Arguments: "{}"}}}) |
| 71 | saved.Add(provider.Message{Role: provider.RoleTool, ToolCallID: "1", Name: "grep", Content: strings.Repeat("y", 5000)}) |
| 72 | saved.Add(provider.Message{Role: provider.RoleAssistant, Content: "step done"}) |
| 73 | saved.Add(provider.Message{Role: provider.RoleUser, Content: "next"}) |
| 74 | saved.Add(provider.Message{Role: provider.RoleAssistant, Content: "ok"}) |
| 75 | path := agent.NewSessionPath(dir, "test") |
| 76 | if err := saved.Save(path); err != nil { |
| 77 | t.Fatalf("save: %v", err) |
| 78 | } |
| 79 | if _, err := agent.EnsureBranchMeta(path); err != nil { |
| 80 | t.Fatalf("meta: %v", err) |
| 81 | } |
| 82 | |
| 83 | loaded, err := agent.LoadSession(path) |
| 84 | if err != nil { |
| 85 | t.Fatalf("load: %v", err) |
| 86 | } |
| 87 | msgs := loaded.Snapshot() |
| 88 | msgs[0].Content = "new sys" |
| 89 | resumed := loaded.CloneWithMessages(msgs) |
| 90 | |
| 91 | exec := agent.New(nil, nil, agent.NewSession("new sys"), agent.Options{ContextWindow: 1000, RecentKeep: 2, ArchiveDir: dir}, event.Discard) |
| 92 | c := newOwnedTestController(t, Options{Executor: exec, SessionDir: dir, Label: "test"}) |
| 93 | c.testCacheColdAfter = -1 // force cold |
| 94 | c.Resume(resumed, path) |
| 95 | |
| 96 | if got := c.SessionPath(); got != path { |
| 97 | t.Fatalf("SessionPath after cold resume = %q, want %q", got, path) |
| 98 | } |
| 99 | // Snapshot was not rewritten by cold resume; in-memory clone keeps new sys |
| 100 | // until an explicit save. Disk still has whatever was last saved unless |
| 101 | // SnapshotRewrite ran — cold path must not rewrite. |
| 102 | re, err := agent.LoadSession(path) |
| 103 | if err != nil { |
| 104 | t.Fatalf("reload: %v", err) |
| 105 | } |
| 106 | if got := re.Messages[0].Content; got != "old sys" { |
| 107 | // Cold resume must not SnapshotRewrite the cloned in-memory system prompt. |
| 108 | t.Fatalf("system prompt on disk after cold resume = %q, want old sys (no rewrite)", got) |
| 109 | } |
| 110 | if !strings.HasPrefix(re.Messages[3].Content, "yyy") { |
| 111 | t.Fatalf("tool result rewrote on cold resume: %.60q", re.Messages[3].Content) |
| 112 | } |
| 113 | if matches, err := filepath.Glob(filepath.Join(dir, "*-recovery-*.jsonl")); err != nil || len(matches) != 0 { |
| 114 | t.Fatalf("recovery branches after cloned cold resume = %v err=%v, want none", matches, err) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestWarmResumeLeavesHistoryAlone(t *testing.T) { |
| 119 | loaded, path, c := coldResumeFixture(t, 24*time.Hour) |
| 120 | |
| 121 | if got := loaded.Snapshot()[3].Content; !strings.HasPrefix(got, "yyy") { |
| 122 | t.Fatalf("warm resume rewrote history: %.60q", got) |
| 123 | } |
| 124 | re, err := agent.LoadSession(path) |
| 125 | if err != nil { |
| 126 | t.Fatalf("reload: %v", err) |
| 127 | } |
| 128 | if !strings.HasPrefix(re.Messages[3].Content, "yyy") { |
| 129 | t.Error("warm resume rewrote the saved transcript") |
| 130 | } |
| 131 | if c.executor.CacheState() != agent.CacheStateWarm { |
| 132 | t.Fatalf("cache state = %q, want warm", c.executor.CacheState()) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func TestColdResumeUnderThresholdUsesCanonical(t *testing.T) { |
| 137 | // Small session well under context window: preflight must not compact. |
| 138 | loaded, path, c := coldResumeFixture(t, 0) |
| 139 | _ = path |
| 140 | if c.executor.CacheState() != agent.CacheStateCold { |
| 141 | t.Fatalf("want cold, got %s", c.executor.CacheState()) |
| 142 | } |
| 143 | // Model-visible should still be full transcript (no projection yet). |
| 144 | visible := c.executor.Session().Snapshot() |
| 145 | if len(visible) != len(loaded.Snapshot()) { |
| 146 | t.Fatalf("visible/canonical mismatch without pressure") |
| 147 | } |
| 148 | if len(c.executor.CacheState()) == 0 { |
| 149 | t.Fatal("cache state empty") |
| 150 | } |
| 151 | } |
| 152 |