返回 DeepSeek-Reasonix
session_lease.go
根目录 / internal / cli / session_lease.go
1 package cli
2
3 import (
4 "fmt"
5 "path/filepath"
6 "strings"
7
8 "reasonix/internal/agent"
9 "reasonix/internal/control"
10 )
11
12 // sessionLeaseResumeRefusal is the startup-time refusal for `reasonix
13 // [--resume|--continue]` and `reasonix run --resume/--continue`: it names the
14 // holder and offers the two ways out (close the holder, or continue in a
15 // duplicated session via --copy).
16 func sessionLeaseResumeRefusal(err error) string {
17 return control.SessionInUseMessage(err) +
18 "; close the other Reasonix window or process, or rerun with --copy to continue in a duplicated session"
19 }
20
21 // sessionLeaseHeldNotice is the in-TUI refusal for /resume and /switch, where
22 // exiting to rerun with --copy is not the natural move.
23 func sessionLeaseHeldNotice(err error) string {
24 return control.SessionInUseMessage(err) + "; " + control.SessionLeaseCloseHint
25 }
26
27 // rebindSessionLease moves the chat TUI's session lease to path before the
28 // controller binds it for writing. A nil keeper (tests, persistence disabled)
29 // gates nothing. On error the keeper still guards the previous session.
30 func (m *chatTUI) rebindSessionLease(path string) error {
31 if m.leases == nil {
32 return nil
33 }
34 return m.leases.Rebind(path)
35 }
36
37 // restoreSessionLease re-points the lease at the controller's current session
38 // after a switch attempt moved it but the switch itself then failed.
39 // Best-effort: the old lease was released during the rebind, so in the
40 // (unlikely) case another runtime grabbed it in between this stays silent and
41 // the next write surfaces the conflict.
42 func (m *chatTUI) restoreSessionLease() {
43 if m.leases == nil {
44 return
45 }
46 _ = m.leases.Rebind(m.ctrl.SessionPath())
47 }
48
49 // followSessionLease re-points the TUI's session lease at the controller's
50 // current session file after an operation that rotated it to a fresh path
51 // (/new, /clear, /branch, fork). A fresh path cannot be held by anyone else,
52 // so failure is theoretical — but never silent.
53 func (m *chatTUI) followSessionLease() {
54 if m.leases == nil {
55 return
56 }
57 if err := m.leases.Rebind(m.ctrl.SessionPath()); err != nil {
58 m.notice(sessionLeaseHeldNotice(err))
59 }
60 }
61
62 // cliSessionRecoveredHandler moves the single-session CLI lease during the
63 // controller's recovery commit. The callback runs before Controller changes its
64 // session path, closing the unguarded interval that event-driven follow-up
65 // calls left after ordinary turn-end and mid-turn autosaves.
66 func cliSessionRecoveredHandler(leases *control.SessionLeaseKeeper) func(control.SessionRecoveryInfo) error {
67 return leases.HandleSessionRecovered
68 }
69
70 // copySessionForWriting duplicates the session at src into a fresh session
71 // file beside it and returns the new path. It backs the --copy escape hatch:
72 // when src is held by another runtime, the copy gives this process a session
73 // it can own. The duplicate is written through Session.Save, so it is
74 // event-log aware (authoritative event log plus .jsonl checkpoint) and starts
75 // with no lease/lock sidecars of its own; src is only read. When src is being
76 // written concurrently, the copy captures the transcript as of the load — an
77 // append-only prefix, the same view a resume would see.
78 func copySessionForWriting(src string) (string, error) {
79 loaded, err := loadResumableSession(src)
80 if err != nil {
81 return "", err
82 }
83 msgs := loaded.Snapshot()
84
85 var srcMeta agent.BranchMeta
86 if meta, ok, metaErr := agent.LoadBranchMeta(src); metaErr == nil && ok {
87 srcMeta = meta
88 }
89 label := "session"
90 if model, ok := agent.LoadSessionModel(src); ok && strings.TrimSpace(model) != "" {
91 label = model
92 }
93
94 newPath := agent.NewSessionPath(filepath.Dir(src), label)
95 copySess := agent.NewSession("")
96 copySess.Messages = msgs
97 if err := copySess.Save(newPath); err != nil {
98 return "", fmt.Errorf("copy session: %w", err)
99 }
100 preview, turns := agent.SessionPreviewFromMessages(msgs)
101 meta := agent.BranchMeta{
102 ParentID: agent.BranchID(src),
103 ForkTurn: -1,
104 ForkMessageIndex: len(msgs),
105 Preview: preview,
106 Turns: turns,
107 SchemaVersion: agent.BranchMetaCountsVersion,
108 Model: srcMeta.Model,
109 }
110 if title := strings.TrimSpace(firstNonEmpty(srcMeta.CustomTitle, srcMeta.TopicTitle)); title != "" {
111 meta.CustomTitle = title + " (copy)"
112 }
113 if err := agent.SaveBranchMeta(newPath, meta); err != nil {
114 return "", fmt.Errorf("copy session meta: %w", err)
115 }
116 return newPath, nil
117 }
118
118 lines GO