返回 DeepSeek-Reasonix
shutdown_save_test.go
根目录 / internal / control / shutdown_save_test.go
1 package control
2
3 import (
4 "os"
5 "path/filepath"
6 "testing"
7 "time"
8
9 "reasonix/internal/agent"
10 "reasonix/internal/event"
11 "reasonix/internal/provider"
12 "reasonix/internal/store"
13 )
14
15 func TestSnapshotForShutdownAppendsToSessionLogInsteadOfForking(t *testing.T) {
16 dir := t.TempDir()
17 path := filepath.Join(dir, "session.jsonl")
18 base := agent.NewSession("sys")
19 base.Add(provider.Message{Role: provider.RoleUser, Content: "persisted"})
20 if err := base.SaveSnapshot(path); err != nil {
21 t.Fatalf("seed session: %v", err)
22 }
23 current, err := agent.LoadSession(path)
24 if err != nil {
25 t.Fatalf("LoadSession: %v", err)
26 }
27 current.Add(provider.Message{Role: provider.RoleAssistant, Content: "shutdown tail"})
28 exec := agent.New(nil, nil, current, agent.Options{}, event.Discard)
29 sink := &noticeSink{}
30 c := newOwnedTestController(t, Options{
31 Executor: exec, SessionDir: dir, SessionPath: path, Label: "shutdown", Sink: sink,
32 OnSessionRecovered: func(info SessionRecoveryInfo) error {
33 t.Errorf("a session log must not enter the recovery handoff: %+v", info)
34 return nil
35 },
36 })
37 t.Cleanup(agent.SetSessionFileLockWaitForTest(40*time.Millisecond, 5*time.Millisecond))
38 release, err := agent.HoldSessionFileLockForTest(path)
39 if err != nil {
40 t.Fatal(err)
41 }
42 t.Cleanup(release)
43
44 if err := c.SnapshotForShutdown(); err != nil {
45 t.Fatalf("SnapshotForShutdown: %v", err)
46 }
47 release()
48 if c.SessionPath() != path {
49 t.Fatalf("session path after shutdown = %q, want %q unchanged", c.SessionPath(), path)
50 }
51 entries, err := os.ReadDir(dir)
52 if err != nil {
53 t.Fatal(err)
54 }
55 for _, entry := range entries {
56 if store.IsSessionTranscriptName(entry.Name()) && entry.Name() != filepath.Base(path) {
57 t.Fatalf("shutdown wrote a transcript copy %s", entry.Name())
58 }
59 }
60 heads, err := agent.ListSessionHeads(path)
61 if err != nil || len(heads) != 2 || heads[1].Kind != agent.HeadKindConcurrent || !heads[1].Selected {
62 t.Fatalf("heads = %+v err=%v, want the tail on a concurrent head", heads, err)
63 }
64 reloaded, err := agent.LoadSession(path)
65 if err != nil {
66 t.Fatal(err)
67 }
68 if got := reloaded.Snapshot(); len(got) != 3 || got[2].Content != "shutdown tail" {
69 t.Fatalf("reloaded transcript = %+v", got)
70 }
71 if notice, ok := sink.lastNotice(); ok && notice.Code == event.NoticeCodeSessionShutdownRecoveryForked {
72 t.Fatalf("shutdown emitted the recovery-copy notice: %+v", notice)
73 }
74 }
75
75 lines GO