| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/agent" |
| 9 | "reasonix/internal/guardian" |
| 10 | "reasonix/internal/provider" |
| 11 | "reasonix/internal/store" |
| 12 | ) |
| 13 | |
| 14 | // /clear must not leave the event log behind: it is the authoritative |
| 15 | // transcript, so a leftover both leaks the discarded conversation and lets |
| 16 | // LoadSession resurrect it if the path is ever reused. |
| 17 | func TestRemoveSessionArtifactsSweepsEventLogAndSidecars(t *testing.T) { |
| 18 | dir := t.TempDir() |
| 19 | path := filepath.Join(dir, "session.jsonl") |
| 20 | s := agent.NewSession("sys") |
| 21 | s.Add(provider.Message{Role: provider.RoleUser, Content: "secret work"}) |
| 22 | if err := s.SaveSnapshot(path); err != nil { |
| 23 | t.Fatalf("SaveSnapshot: %v", err) |
| 24 | } |
| 25 | s.Add(provider.Message{Role: provider.RoleAssistant, Content: "reply"}) |
| 26 | if err := s.SaveSnapshot(path); err != nil { |
| 27 | t.Fatalf("SaveSnapshot append: %v", err) |
| 28 | } |
| 29 | for _, extra := range []string{ |
| 30 | store.SessionGoalState(path), |
| 31 | store.SessionConflictLog(path), |
| 32 | guardian.PathFor(path), |
| 33 | guardian.CursorPathFor(path), |
| 34 | store.SessionEventLog(guardian.PathFor(path)), |
| 35 | } { |
| 36 | if err := os.WriteFile(extra, []byte("{}\n"), 0o644); err != nil { |
| 37 | t.Fatalf("write %s: %v", extra, err) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | if err := removeSessionArtifacts(path); err != nil { |
| 42 | t.Fatalf("removeSessionArtifacts: %v", err) |
| 43 | } |
| 44 | |
| 45 | for _, p := range []string{ |
| 46 | path, |
| 47 | store.SessionMeta(path), |
| 48 | store.SessionGoalState(path), |
| 49 | store.SessionEventLog(path), |
| 50 | store.SessionEventIndex(path), |
| 51 | store.SessionConflictLog(path), |
| 52 | guardian.PathFor(path), |
| 53 | guardian.CursorPathFor(path), |
| 54 | store.SessionEventLog(guardian.PathFor(path)), |
| 55 | } { |
| 56 | if _, err := os.Stat(p); !os.IsNotExist(err) { |
| 57 | t.Errorf("artifact survived clear: %s (err=%v)", p, err) |
| 58 | } |
| 59 | } |
| 60 | if _, err := agent.LoadSession(path); !os.IsNotExist(err) { |
| 61 | t.Fatalf("LoadSession after clear = %v, want IsNotExist (no resurrection)", err) |
| 62 | } |
| 63 | } |
| 64 |