| 1 | package boot |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "reasonix/internal/ablation" |
| 12 | "reasonix/internal/agent" |
| 13 | "reasonix/internal/event" |
| 14 | "reasonix/internal/provider" |
| 15 | ) |
| 16 | |
| 17 | // TestEffectSessionLogUpgradeKeepsModelMessagesThroughRealBuild freezes the |
| 18 | // production compatibility boundary: continuing a schema-1 transcript imports |
| 19 | // it into a final identity-bound v3 session, leaves the source bytes untouched, |
| 20 | // and preserves the provider-visible prefix (including stable message ids). |
| 21 | func TestEffectSessionLogUpgradeKeepsModelMessagesThroughRealBuild(t *testing.T) { |
| 22 | isolateConfigHome(t) |
| 23 | dir := robustTempDir(t) |
| 24 | t.Chdir(dir) |
| 25 | t.Setenv(agent.SessionLogSchemaEnv, "v1") |
| 26 | |
| 27 | rec := &effectRecordingProvider{} |
| 28 | provider.Register("boot-effect-session-log", func(provider.Config) (provider.Provider, error) { return rec, nil }) |
| 29 | writeFile(t, dir, "reasonix.toml", ` |
| 30 | default_model = "test-model" |
| 31 | |
| 32 | [agent] |
| 33 | system_prompt = "BASE" |
| 34 | |
| 35 | [environment] |
| 36 | enabled = false |
| 37 | |
| 38 | [[providers]] |
| 39 | name = "test-model" |
| 40 | kind = "boot-effect-session-log" |
| 41 | model = "x" |
| 42 | `) |
| 43 | approveWorkspace(t, dir) |
| 44 | |
| 45 | legacyDir := filepath.Join(t.TempDir(), "sessions") |
| 46 | legacyPath := agent.NewSessionPath(legacyDir, "legacy") |
| 47 | legacy := agent.NewSession("BASE") |
| 48 | legacy.Add(provider.Message{Role: provider.RoleUser, Content: "reply ok"}) |
| 49 | legacy.Add(provider.Message{Role: provider.RoleAssistant, Content: "ok"}) |
| 50 | if err := legacy.Save(legacyPath); err != nil { |
| 51 | t.Fatalf("write schema-1 source: %v", err) |
| 52 | } |
| 53 | beforeSource, err := os.ReadFile(legacyPath) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | before, err := json.Marshal(provider.ModelMessages(legacy.Snapshot())) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | if err := os.Unsetenv(agent.SessionLogSchemaEnv); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | |
| 65 | ctrl, err := Build(context.Background(), withTestSession(t, Options{SessionDir: legacyDir, Sink: event.Discard, Ablation: ablation.Set{}})) |
| 66 | if err != nil { |
| 67 | t.Fatalf("Build: %v", err) |
| 68 | } |
| 69 | ref, err := ctrl.ContinueLegacySession(context.Background(), legacyPath, "") |
| 70 | if err != nil { |
| 71 | ctrl.Close() |
| 72 | t.Fatalf("ContinueLegacySession: %v", err) |
| 73 | } |
| 74 | if ctrl.SessionPath() != "" { |
| 75 | t.Fatalf("migrated v3 runtime retained legacy execution path %q", ctrl.SessionPath()) |
| 76 | } |
| 77 | if err := ctrl.Run(context.Background(), "reply again"); err != nil { |
| 78 | ctrl.Close() |
| 79 | t.Fatalf("Run after migration: %v", err) |
| 80 | } |
| 81 | after, err := json.Marshal(provider.ModelMessages(ctrl.History())) |
| 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | service, runtime, ok := ctrl.SessionBinding() |
| 86 | if !ok || runtime.Ref() != ref { |
| 87 | t.Fatalf("bound runtime = %+v, want %+v", runtime, ref) |
| 88 | } |
| 89 | ctrl.Close() |
| 90 | |
| 91 | var beforeMsgs, afterMsgs []json.RawMessage |
| 92 | if err := json.Unmarshal(before, &beforeMsgs); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | if err := json.Unmarshal(after, &afterMsgs); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | if len(afterMsgs) <= len(beforeMsgs) { |
| 99 | t.Fatalf("migrated transcript did not grow: before %d after %d", len(beforeMsgs), len(afterMsgs)) |
| 100 | } |
| 101 | for i := range beforeMsgs { |
| 102 | if string(beforeMsgs[i]) != string(afterMsgs[i]) { |
| 103 | t.Fatalf("message %d changed across legacy import\nbefore: %s\nafter: %s", i, beforeMsgs[i], afterMsgs[i]) |
| 104 | } |
| 105 | } |
| 106 | if got, err := os.ReadFile(legacyPath); err != nil || !bytes.Equal(got, beforeSource) { |
| 107 | t.Fatalf("legacy source changed during import: err=%v", err) |
| 108 | } |
| 109 | cold, err := service.Query().History(context.Background(), ref) |
| 110 | if err != nil { |
| 111 | t.Fatalf("cold v3 history: %v", err) |
| 112 | } |
| 113 | if len(provider.ModelMessages(cold)) != len(afterMsgs) { |
| 114 | t.Fatalf("cold v3 history len=%d, want %d", len(provider.ModelMessages(cold)), len(afterMsgs)) |
| 115 | } |
| 116 | } |
| 117 |