返回 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 ctrl.UsesExclusiveSession() {
60 if _, err := ctrl.ContinueLegacySession(context.Background(), path, ""); err != nil {
61 return err
62 }
63 return nil
64 }
65 ctrl.Resume(session, path)
66 return persistCLIModelSelection(ctrl)
67 }
68
69 // commitResumedSession is a no-op without a resume target, so callers need no
70 // second guard around the takeover handover.
71 func commitResumedSession(binding *cliTakeoverBinding, manager *cliTakeoverManager, ctrl *control.Controller, session *agent.Session, target cliResumeTarget) error {
72 if target.empty() {
73 return nil
74 }
75 if target.canonical() {
76 // Final-format sessions have no transcript path to lease or load; the
77 // controller attaches to the identity and the session service's writer
78 // lease decides ownership.
79 if !ctrl.UsesExclusiveSession() {
80 return errors.New("resuming a final-format session requires the session engine")
81 }
82 _, err := ctrl.OpenSession(context.Background(), target.ref)
83 return err
84 }
85 if err := binding.commitPrevious(manager); err != nil {
86 return err
87 }
88 return resumeWithPersistedSelection(ctrl, session, target.path)
89 }
90
91 // prepareServeSessionPath picks serve's auto-save target: reuse the resumed
92 // file, adopt the caller's session id, or leave the controller to stamp a
93 // fresh path.
94 func prepareServeSessionPath(ctrl *control.Controller, session *agent.Session, resumePath, sessionID string) error {
95 if strings.TrimSpace(resumePath) != "" {
96 return resumeWithPersistedSelection(ctrl, session, resumePath)
97 }
98 if strings.TrimSpace(sessionID) == "" {
99 return nil
100 }
101 if ctrl.UsesExclusiveSession() {
102 _, err := ctrl.BindFreshSession(context.Background(), strings.TrimSpace(sessionID))
103 return err
104 }
105 freshPath, err := freshWebSessionPath(ctrl.SessionDir(), sessionID)
106 if err != nil {
107 return err
108 }
109 ctrl.SetFreshSessionPath(freshPath)
110 return nil
111 }
112
112 lines GO