返回 DeepSeek-Reasonix
resume_selection.go
根目录 / internal / cli / resume_selection.go
1 package cli
2
3 import (
4 "context"
5 "errors"
6 "strings"
7
8 "reasonix/internal/agent"
9 "reasonix/internal/config"
10 "reasonix/internal/control"
11 )
12
13 // modelForResumePath answers which model a resumed run should use. An explicit
14 // flag always wins; otherwise the saved selection is validated against the
15 // connection it was recorded against, so a migrated or edited connection fails
16 // closed instead of silently resuming under a different account.
17 func modelForResumePath(modelName, resumePath string, cfg *config.Config) (string, error) {
18 if strings.TrimSpace(modelName) != "" || strings.TrimSpace(resumePath) == "" {
19 return modelName, nil
20 }
21 sessionModel, identity, ok := agent.LoadSessionModelSelection(resumePath)
22 if !ok {
23 return modelName, nil
24 }
25 if cfg == nil {
26 return sessionModel, nil
27 }
28 resolved, err := cfg.ResolveSavedModel(sessionModel, identity)
29 if err != nil {
30 return "", err
31 }
32 if _, ok := cfg.ResolveModel(resolved); !ok {
33 return modelName, nil
34 }
35 return resolved, nil
36 }
37
38 func applyResumeModel(model *string, resumePath string, cfg *config.Config) error {
39 resolved, err := modelForResumePath(*model, resumePath, cfg)
40 if err != nil {
41 return err
42 }
43 *model = resolved
44 return nil
45 }
46
47 // copyResumableSession refuses to duplicate a session whose saved selection no
48 // longer resolves, so the copy cannot silently inherit an unusable connection.
49 func copyResumableSession(model, resumePath string, cfg *config.Config) (string, error) {
50 if _, err := modelForResumePath(model, resumePath, cfg); err != nil {
51 return "", err
52 }
53 return copySessionForWriting(resumePath)
54 }
55
56 // resumeWithPersistedSelection records the selection the resumed controller
57 // actually accepted, so the next restart restores it instead of re-resolving.
58 func resumeWithPersistedSelection(ctrl *control.Controller, session *agent.Session, path string) error {
59 if err := ctrl.ResumeNativeSession(session, path); err != nil {
60 return err
61 }
62 return persistCLIModelSelection(ctrl)
63 }
64
65 // commitResumedSession is a no-op without a resume target, so callers need no
66 // second guard around the takeover handover.
67 func commitResumedSession(binding *cliTakeoverBinding, manager *cliTakeoverManager, ctrl *control.Controller, session *agent.Session, target cliResumeTarget) error {
68 if target.empty() {
69 return nil
70 }
71 if target.canonical() {
72 // Final-format sessions have no transcript path to lease or load; the
73 // controller attaches to the identity and the session service's writer
74 // lease decides ownership.
75 if ctrl.SessionService() == nil {
76 return errors.New("resuming a final-format session requires the session engine")
77 }
78 _, err := ctrl.OpenSession(context.Background(), target.ref)
79 return err
80 }
81 if err := binding.commitPrevious(manager); err != nil {
82 return err
83 }
84 return resumeWithPersistedSelection(ctrl, session, target.path)
85 }
86
87 // prepareServeSessionPath picks serve's auto-save target: reuse the resumed
88 // file, adopt the caller's session id, or leave the controller to stamp a
89 // fresh path.
90 func prepareServeSessionPath(ctrl *control.Controller, session *agent.Session, resumePath, sessionID string) error {
91 if strings.TrimSpace(resumePath) != "" {
92 return resumeWithPersistedSelection(ctrl, session, resumePath)
93 }
94 if strings.TrimSpace(sessionID) == "" {
95 return nil
96 }
97 if ctrl.UsesExclusiveSession() {
98 _, err := ctrl.BindFreshSession(context.Background(), strings.TrimSpace(sessionID))
99 return err
100 }
101 freshPath, err := freshWebSessionPath(ctrl.SessionDir(), sessionID)
102 if err != nil {
103 return err
104 }
105 ctrl.SetFreshSessionPath(freshPath)
106 return nil
107 }
108
108 lines GO