| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "maps" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "reflect" |
| 12 | "strings" |
| 13 | "sync/atomic" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "reasonix/internal/agent" |
| 18 | "reasonix/internal/checkpoint" |
| 19 | "reasonix/internal/event" |
| 20 | "reasonix/internal/provider" |
| 21 | "reasonix/internal/tool" |
| 22 | ) |
| 23 | |
| 24 | func TestCompatibilityRewindRequiresConfirmationForPartialCoverage(t *testing.T) { |
| 25 | dir := t.TempDir() |
| 26 | root := t.TempDir() |
| 27 | path := filepath.Join(root, "partial.txt") |
| 28 | if err := os.WriteFile(path, []byte("before"), 0o644); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | sess := agent.NewSession("sys") |
| 32 | ag := agent.New(nil, tool.NewRegistry(), sess, agent.Options{}, event.Discard) |
| 33 | c := newOwnedTestController(t, Options{ |
| 34 | Runner: ag, |
| 35 | Executor: ag, |
| 36 | SessionDir: dir, |
| 37 | SessionPath: filepath.Join(dir, "partial.jsonl"), |
| 38 | WorkspaceRoot: root, |
| 39 | Sink: event.Discard, |
| 40 | }) |
| 41 | c.beginCheckpoint(context.Background(), "edit partial.txt") |
| 42 | c.mutationObserver.BeforeMutation("partial.txt", "write_file", checkpoint.CaptureBeforeMutation) |
| 43 | if err := os.WriteFile(path, []byte("after"), 0o644); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | c.mutationObserver.AfterMutation("partial.txt", "write_file") |
| 47 | c.mutationObserver.RecordGap(checkpoint.CoverageGap{Reason: checkpoint.GapBashSideEffect, Tool: "bash"}) |
| 48 | |
| 49 | plan, err := c.PrepareRewind(0, RewindCode) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if !plan.CanFiles || !RewindPlanRequiresConfirmation(plan) { |
| 54 | t.Fatalf("partial plan = %+v, want restorable files with explicit confirmation", plan) |
| 55 | } |
| 56 | if err := c.Rewind(0, RewindCode); !errors.Is(err, ErrRewindCoverageConfirmationRequired) { |
| 57 | t.Fatalf("compatibility Rewind error = %v, want confirmation-required", err) |
| 58 | } |
| 59 | if got := string(mustReadFile(t, path)); got != "after" { |
| 60 | t.Fatalf("unconfirmed rewind changed file to %q", got) |
| 61 | } |
| 62 | |
| 63 | result, err := c.CommitRewind(plan.PlanID) |
| 64 | if err != nil || !result.OK { |
| 65 | t.Fatalf("confirmed CommitRewind result=%+v err=%v", result, err) |
| 66 | } |
| 67 | if got := string(mustReadFile(t, path)); got != "before" { |
| 68 | t.Fatalf("confirmed rewind left file at %q, want before", got) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestResumeRecoversCommittingCombinedRewind(t *testing.T) { |
| 73 | dir := t.TempDir() |
| 74 | root := t.TempDir() |
| 75 | sessionPath := filepath.Join(dir, "session.jsonl") |
| 76 | filePath := filepath.Join(root, "a.txt") |
| 77 | if err := os.WriteFile(filePath, []byte("before"), 0o644); err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | fileInfo, err := os.Stat(filePath) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | diskMode := uint32(fileInfo.Mode().Perm()) |
| 85 | fullMessages := []provider.Message{ |
| 86 | {Role: provider.RoleSystem, Content: "sys"}, |
| 87 | {Role: provider.RoleUser, Content: "first"}, |
| 88 | {Role: provider.RoleAssistant, Content: "answer"}, |
| 89 | {Role: provider.RoleUser, Content: "second"}, |
| 90 | {Role: provider.RoleAssistant, Content: "later"}, |
| 91 | } |
| 92 | saved := agent.NewSession("") |
| 93 | saved.Replace(fullMessages[:3]) |
| 94 | if err := saved.Save(sessionPath); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | forward, err := json.Marshal(fullMessages) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | checkpointBackup, err := json.Marshal([]*checkpoint.Checkpoint{{ |
| 102 | SchemaVersion: checkpoint.SchemaV2, |
| 103 | Turn: 1, |
| 104 | Prompt: "second", |
| 105 | MsgIndex: 3, |
| 106 | }}) |
| 107 | if err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | checkpointDir := ckptDir(sessionPath) |
| 111 | if err := os.MkdirAll(filepath.Join(checkpointDir, "transactions"), 0o755); err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | tx := checkpoint.TransactionManifest{ |
| 115 | SchemaVersion: checkpoint.SchemaV2, |
| 116 | ID: "tx-resume-recovery", |
| 117 | WorkspaceRoot: root, |
| 118 | State: checkpoint.TxCommitting, |
| 119 | Kind: "rewind", |
| 120 | Turn: 1, |
| 121 | Scope: checkpoint.RewindBoth, |
| 122 | HasBoundary: true, |
| 123 | BoundaryIndex: 3, |
| 124 | TruncateFrom: 1, |
| 125 | ConversationForward: forward, |
| 126 | CheckpointBackup: checkpointBackup, |
| 127 | Targets: []checkpoint.TransactionTarget{{ |
| 128 | Path: "a.txt", AbsPath: filePath, Action: "write", Published: true, |
| 129 | RestoreExisted: true, RestoreSHA: checkpoint.Digest([]byte("before")), RestoreMode: diskMode, |
| 130 | ForwardExisted: true, ForwardSHA: checkpoint.Digest([]byte("after")), ForwardMode: diskMode, |
| 131 | ForwardInline: []byte("after"), BackupPath: filepath.Join(root, ".a.txt.reasonix-recovery.bak"), |
| 132 | }}, |
| 133 | } |
| 134 | raw, err := json.Marshal(tx) |
| 135 | if err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | manifestPath := filepath.Join(checkpointDir, "transactions", tx.ID+".json") |
| 139 | if err := os.WriteFile(manifestPath, raw, 0o644); err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | |
| 143 | loaded, err := agent.LoadSession(sessionPath) |
| 144 | if err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | ag := agent.New(nil, tool.NewRegistry(), agent.NewSession("sys"), agent.Options{}, event.Discard) |
| 148 | c := newOwnedTestController(t, Options{Executor: ag, Runner: ag, SessionDir: dir, WorkspaceRoot: root}) |
| 149 | c.Resume(loaded, sessionPath) |
| 150 | if got := ag.Session().Snapshot(); len(got) != len(fullMessages) || got[len(got)-1].Content != "later" { |
| 151 | t.Fatalf("recovered conversation = %#v, want full forward transcript", got) |
| 152 | } |
| 153 | data, err := os.ReadFile(filePath) |
| 154 | if err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | if string(data) != "after" { |
| 158 | t.Fatalf("recovered file = %q, want after", data) |
| 159 | } |
| 160 | if got := c.Checkpoints(); len(got) != 1 || got[0].Turn != 1 { |
| 161 | t.Fatalf("recovered checkpoints = %+v, want turn 1", got) |
| 162 | } |
| 163 | if err := json.Unmarshal(mustReadFile(t, manifestPath), &tx); err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | if tx.State != checkpoint.TxAborted { |
| 167 | t.Fatalf("transaction state = %s, want aborted", tx.State) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func mustReadFile(t *testing.T, path string) []byte { |
| 172 | t.Helper() |
| 173 | b, err := os.ReadFile(path) |
| 174 | if err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | return b |
| 178 | } |
| 179 | |
| 180 | func runTwoTurns(t *testing.T) (*Controller, *agent.Agent, *[]event.Event) { |
| 181 | t.Helper() |
| 182 | dir := t.TempDir() |
| 183 | prov := &scriptedTurns{turns: [][]provider.Chunk{ |
| 184 | textTurn("first answer"), |
| 185 | textTurn("second answer"), |
| 186 | textTurn("edited answer"), |
| 187 | }} |
| 188 | ag := agent.New(prov, tool.NewRegistry(), agent.NewSession("sys"), agent.Options{}, event.Discard) |
| 189 | var events []event.Event |
| 190 | c := newOwnedTestController(t, Options{ |
| 191 | Runner: ag, |
| 192 | Executor: ag, |
| 193 | SessionDir: dir, |
| 194 | Label: "test", |
| 195 | Sink: event.FuncSink(func(e event.Event) { events = append(events, e) }), |
| 196 | }) |
| 197 | c.SetSessionPath(agent.NewSessionPath(dir, "test")) |
| 198 | if err := c.runTurnWithRaw(context.Background(), "first prompt", "first prompt"); err != nil { |
| 199 | t.Fatalf("turn 1: %v", err) |
| 200 | } |
| 201 | if err := c.runTurnWithRaw(context.Background(), "second prompt", "second prompt"); err != nil { |
| 202 | t.Fatalf("turn 2: %v", err) |
| 203 | } |
| 204 | return c, ag, &events |
| 205 | } |
| 206 | |
| 207 | // TestRewindConversationFailsLoudlyAfterCompaction reproduces #3598: once |
| 208 | // compaction shrinks the message log below a turn's recorded boundary, a |
| 209 | // conversation/both rewind to that turn skipped the truncation but still emitted |
| 210 | // a success notice — code rolled back, conversation silently did not. |
| 211 | func TestRewindConversationFailsLoudlyAfterCompaction(t *testing.T) { |
| 212 | c, ag, events := runTwoTurns(t) |
| 213 | |
| 214 | c.checkpoints.mu.Lock() |
| 215 | lastTurn := c.checkpoints.turn - 1 |
| 216 | boundary := c.checkpoints.bound[lastTurn] |
| 217 | c.checkpoints.mu.Unlock() |
| 218 | if boundary <= 1 { |
| 219 | t.Fatalf("expected the latest turn's boundary above 1, got bound=%v", c.checkpoints.bound) |
| 220 | } |
| 221 | |
| 222 | // Auto-compaction replaces the prefix with a summary, shrinking the log below |
| 223 | // the recorded boundary; compaction does not rewrite checkpoint boundaries. |
| 224 | sess := ag.Session() |
| 225 | sess.Messages = []provider.Message{{Role: provider.RoleUser, Content: "summary"}} |
| 226 | |
| 227 | *events = nil |
| 228 | err := c.Rewind(lastTurn, RewindBoth) |
| 229 | if err == nil || !strings.Contains(err.Error(), "compacted") { |
| 230 | t.Fatalf("Rewind after compaction error = %v, want a 'compacted past' failure", err) |
| 231 | } |
| 232 | for _, e := range *events { |
| 233 | if e.Kind == event.Notice && strings.Contains(e.Text, "rewound conversation") { |
| 234 | t.Fatalf("emitted a false conversation-rewind success after skipping truncation: %q", e.Text) |
| 235 | } |
| 236 | } |
| 237 | if got := len(ag.Session().Messages); got != 1 { |
| 238 | t.Fatalf("session messages = %d, want the compacted log left intact at 1", got) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // TestRewindConversationSucceedsWithLiveBoundary is the companion happy path: a |
| 243 | // boundary still within the log truncates the conversation and reports success. |
| 244 | func TestRewindConversationSucceedsWithLiveBoundary(t *testing.T) { |
| 245 | c, ag, events := runTwoTurns(t) |
| 246 | |
| 247 | c.checkpoints.mu.Lock() |
| 248 | lastTurn := c.checkpoints.turn - 1 |
| 249 | boundary := c.checkpoints.bound[lastTurn] |
| 250 | c.checkpoints.mu.Unlock() |
| 251 | |
| 252 | *events = nil |
| 253 | if err := c.Rewind(lastTurn, RewindConversation); err != nil { |
| 254 | t.Fatalf("Rewind with a live boundary: %v", err) |
| 255 | } |
| 256 | if got := len(ag.Session().Messages); got != boundary { |
| 257 | t.Fatalf("switched session = %d messages, want boundary %d", got, boundary) |
| 258 | } |
| 259 | ok := false |
| 260 | for _, e := range *events { |
| 261 | if e.Kind == event.Notice && strings.Contains(e.Text, "forked conversation") { |
| 262 | ok = true |
| 263 | } |
| 264 | } |
| 265 | if !ok { |
| 266 | t.Fatal("expected a conversation-fork success notice") |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | func TestCompatibilityRewindTransfersLeaseBeforeForkSwitch(t *testing.T) { |
| 271 | t.Setenv(agent.SessionLogSchemaEnv, "v1") |
| 272 | c, ag, _ := runTwoTurns(t) |
| 273 | originalPath := c.SessionPath() |
| 274 | keeper := NewSessionLeaseKeeper() |
| 275 | defer keeper.Release() |
| 276 | if err := keeper.Rebind(originalPath); err != nil { |
| 277 | t.Fatal(err) |
| 278 | } |
| 279 | if err := keeper.BindControllerAuthority(c); err != nil { |
| 280 | t.Fatal(err) |
| 281 | } |
| 282 | |
| 283 | if err := c.Rewind(1, RewindConversation); err != nil { |
| 284 | t.Fatalf("Rewind: %v", err) |
| 285 | } |
| 286 | targetPath := c.SessionPath() |
| 287 | if targetPath == originalPath { |
| 288 | t.Fatal("conversation rewind did not switch to its fork") |
| 289 | } |
| 290 | if got := keeper.HeldPath(); got != agent.CanonicalSessionPath(targetPath) { |
| 291 | t.Fatalf("keeper path = %q, want %q", got, agent.CanonicalSessionPath(targetPath)) |
| 292 | } |
| 293 | if auth := ag.Session().WriteAuthority(); auth == nil || !auth.Covers(targetPath) { |
| 294 | t.Fatal("fork was published without target write authority") |
| 295 | } |
| 296 | old, err := agent.TryAcquireSessionLease(originalPath) |
| 297 | if err != nil { |
| 298 | t.Fatalf("parent lease remained held after switch: %v", err) |
| 299 | } |
| 300 | old.Release() |
| 301 | } |
| 302 | |
| 303 | func TestPositionalCompressionPreservesCheckpointLineage(t *testing.T) { |
| 304 | c, ag, _ := runTwoTurns(t) |
| 305 | sess := ag.Session() |
| 306 | sess.Add(provider.Message{Role: provider.RoleAssistant, Content: strings.Repeat("large completed output ", 240)}) |
| 307 | beforeMessages := sess.Snapshot() |
| 308 | beforeRewrite := sess.RewriteVersion() |
| 309 | beforeRevision := atomic.LoadInt64(&c.sessionRevision) |
| 310 | c.checkpoints.mu.Lock() |
| 311 | beforeBounds := make(map[int]int, len(c.checkpoints.bound)) |
| 312 | maps.Copy(beforeBounds, c.checkpoints.bound) |
| 313 | c.checkpoints.mu.Unlock() |
| 314 | |
| 315 | if err := c.SummarizeFrom(context.Background(), 0); err != nil { |
| 316 | t.Fatalf("SummarizeFrom: %v", err) |
| 317 | } |
| 318 | if !reflect.DeepEqual(sess.Snapshot(), beforeMessages) { |
| 319 | t.Fatal("positional compression changed canonical history") |
| 320 | } |
| 321 | if got := sess.RewriteVersion(); got != beforeRewrite { |
| 322 | t.Fatalf("rewrite version = %d, want unchanged %d", got, beforeRewrite) |
| 323 | } |
| 324 | if got := atomic.LoadInt64(&c.sessionRevision); got != beforeRevision { |
| 325 | t.Fatalf("controller session revision = %d, want unchanged %d", got, beforeRevision) |
| 326 | } |
| 327 | c.checkpoints.mu.Lock() |
| 328 | afterBounds := make(map[int]int, len(c.checkpoints.bound)) |
| 329 | maps.Copy(afterBounds, c.checkpoints.bound) |
| 330 | c.checkpoints.mu.Unlock() |
| 331 | if !reflect.DeepEqual(afterBounds, beforeBounds) { |
| 332 | t.Fatalf("checkpoint boundaries changed: before=%v after=%v", beforeBounds, afterBounds) |
| 333 | } |
| 334 | state, ok, err := agent.LoadCompactionState(c.SessionPath()) |
| 335 | if err != nil || !ok { |
| 336 | t.Fatalf("load projection sidecar: ok=%v err=%v", ok, err) |
| 337 | } |
| 338 | if state.LastReceipt == nil || state.LastReceipt.Trigger != agent.CompactionTriggerManual || state.Projection.ProjectionVersion == 0 { |
| 339 | t.Fatalf("projection state = %+v", state) |
| 340 | } |
| 341 | if _, ok := c.checkpoints.boundary(1); !ok { |
| 342 | t.Fatal("conversation rewind boundary disappeared after compression") |
| 343 | } |
| 344 | plan, err := c.PrepareRewind(1, RewindConversation) |
| 345 | if err != nil || !plan.CanConversation { |
| 346 | t.Fatalf("conversation rewind unavailable after compression: plan=%+v err=%v", plan, err) |
| 347 | } |
| 348 | if err := c.SummarizeFrom(context.Background(), 0); err == nil || !strings.Contains(err.Error(), "no longer present in the model context") { |
| 349 | t.Fatalf("second positional compression error = %v, want folded-boundary explanation", err) |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // TestTailRewindKeepsCompactionProjection covers the desktop edit flow at its |
| 354 | // final boundary: edit = conversation rewind + resubmit. A rewind whose |
| 355 | // boundary lands in the live tail (past the fold start) must keep the |
| 356 | // compaction projection instead of ballooning the context back to the |
| 357 | // pre-compaction transcript and re-paying a full summary. |
| 358 | func TestTailRewindKeepsCompactionProjection(t *testing.T) { |
| 359 | dir := t.TempDir() |
| 360 | prov := &scriptedTurns{turns: [][]provider.Chunk{ |
| 361 | textTurn("first answer"), |
| 362 | textTurn("second answer"), |
| 363 | }} |
| 364 | ag := agent.New(prov, tool.NewRegistry(), agent.NewSession("sys"), agent.Options{ |
| 365 | ContextWindow: 10_000, CompactRatio: 0.80, RecentKeep: 2, |
| 366 | }, event.Discard) |
| 367 | c := newOwnedTestController(t, Options{ |
| 368 | Runner: ag, |
| 369 | Executor: ag, |
| 370 | SessionDir: dir, |
| 371 | Label: "test", |
| 372 | Sink: event.Discard, |
| 373 | }) |
| 374 | c.SetSessionPath(agent.NewSessionPath(dir, "test")) |
| 375 | ctx := context.Background() |
| 376 | if err := c.runTurnWithRaw(ctx, "first prompt", "first prompt"); err != nil { |
| 377 | t.Fatalf("turn 1: %v", err) |
| 378 | } |
| 379 | big := strings.Repeat("line\n", 200) |
| 380 | sess := ag.Session() |
| 381 | for i := range 40 { |
| 382 | id := fmt.Sprintf("bulk-%d", i) |
| 383 | sess.Add(provider.Message{Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: id, Name: "read_file", Arguments: "{}"}}}) |
| 384 | sess.Add(provider.Message{Role: provider.RoleTool, ToolCallID: id, Name: "read_file", Content: big}) |
| 385 | } |
| 386 | if err := c.runTurnWithRaw(ctx, "second prompt", "second prompt"); err != nil { |
| 387 | t.Fatalf("turn 2: %v", err) |
| 388 | } |
| 389 | if err := ag.CompactNow(ctx, ""); err != nil { |
| 390 | t.Fatalf("compact: %v", err) |
| 391 | } |
| 392 | before := ag.ContextMaintenanceSnapshot() |
| 393 | if before.ProjectionVersion == 0 || before.ProjectedTokens >= before.FoldTrigger { |
| 394 | t.Fatalf("pre-rewind snapshot = %+v, want an installed projection under the fold trigger", before) |
| 395 | } |
| 396 | |
| 397 | c.checkpoints.mu.Lock() |
| 398 | lastTurn := c.checkpoints.turn - 1 |
| 399 | c.checkpoints.mu.Unlock() |
| 400 | if err := c.Rewind(lastTurn, RewindConversation); err != nil { |
| 401 | t.Fatalf("tail rewind: %v", err) |
| 402 | } |
| 403 | |
| 404 | after := ag.ContextMaintenanceSnapshot() |
| 405 | if after.ProjectionVersion != before.ProjectionVersion { |
| 406 | t.Fatalf("projection version %d -> %d, want the fold kept across a tail-only rewind", |
| 407 | before.ProjectionVersion, after.ProjectionVersion) |
| 408 | } |
| 409 | if after.ProjectedTokens >= after.FoldTrigger { |
| 410 | t.Fatalf("post-rewind view %d tokens at or above fold %d, want the compacted size kept", |
| 411 | after.ProjectedTokens, after.FoldTrigger) |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | func TestEditPromptPersistsOriginalPrompt(t *testing.T) { |
| 416 | c, ag, _ := runTwoTurns(t) |
| 417 | |
| 418 | if err := c.Rewind(1, RewindConversation); err != nil { |
| 419 | t.Fatal(err) |
| 420 | } |
| 421 | c.SubmitEditedDisplay("edited prompt", "edited prompt", "second prompt") |
| 422 | defer c.autosaveWG.Wait() |
| 423 | |
| 424 | var loaded *agent.Session |
| 425 | deadline := time.Now().Add(time.Second) |
| 426 | for { |
| 427 | var err error |
| 428 | loaded, err = agent.LoadSession(c.SessionPath()) |
| 429 | if err == nil { |
| 430 | msgs := loaded.Snapshot() |
| 431 | if len(msgs) >= 2 { |
| 432 | last := msgs[len(msgs)-2] |
| 433 | if last.Role == provider.RoleUser && agent.StripTransientUserBlocks(last.Content) == "edited prompt" { |
| 434 | break |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | if time.Now().After(deadline) { |
| 439 | t.Fatalf("edited prompt was not persisted before deadline") |
| 440 | } |
| 441 | time.Sleep(10 * time.Millisecond) |
| 442 | } |
| 443 | msgs := loaded.Snapshot() |
| 444 | last := msgs[len(msgs)-2] |
| 445 | if last.Role != provider.RoleUser || agent.StripTransientUserBlocks(last.Content) != "edited prompt" { |
| 446 | t.Fatalf("last user message = %+v, want edited prompt", last) |
| 447 | } |
| 448 | if !last.Edited || last.Original != "second prompt" { |
| 449 | t.Fatalf("edit metadata = edited:%v original:%q, want edited:true original:%q", last.Edited, last.Original, "second prompt") |
| 450 | } |
| 451 | for _, m := range ag.Session().Snapshot() { |
| 452 | if m.Role == provider.RoleUser && m.Content == "second prompt" { |
| 453 | t.Fatalf("original prompt stayed as an active model turn: %+v", ag.Session().Snapshot()) |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 |