返回 DeepSeek-Reasonix
pinned_context.go
根目录 / internal / control / pinned_context.go
1 package control
2
3 import (
4 "context"
5 "log/slog"
6 "time"
7
8 "reasonix/internal/agent"
9 "reasonix/internal/provider"
10 )
11
12 type controllerPromptState struct {
13 base string
14 }
15
16 // PinnedContextLoader resolves the sidecar-owned desired context for the
17 // controller's current session immediately before an admitted model turn.
18 // Implementations perform I/O without holding Controller or Session locks.
19 type PinnedContextLoader func(context.Context, string) (agent.PinnedContextSnapshot, error)
20
21 func newControllerPromptState(base string, executor *agent.Agent) controllerPromptState {
22 current := ""
23 if executor != nil && executor.Session() != nil {
24 messages := executor.Session().Snapshot()
25 if len(messages) > 0 && messages[0].Role == provider.RoleSystem {
26 current = messages[0].Content
27 if base == "" {
28 base = current
29 }
30 }
31 }
32 state := controllerPromptState{base: base}
33 if executor != nil && executor.Session() != nil && state.base != current {
34 executor.Session().SetLeadingSystemPromptWithReason(state.base, "legacy_pinned_system_migration")
35 }
36 return state
37 }
38
39 // ApplyExtensionSystemPrompt replaces only the host/extension-owned base
40 // prompt. Pinned context is transcript state and is never composed into it.
41 func (c *Controller) ApplyExtensionSystemPrompt(prompt string) {
42 if c == nil || c.executor == nil {
43 return
44 }
45 c.mu.Lock()
46 c.prompt.base = prompt
47 c.mu.Unlock()
48 c.executor.SetSession(agent.NewSession(prompt))
49 if err := c.replaceSessionEventProjection(context.Background(), "extension-system-prompt", c.executor.Session().Snapshot()); err != nil {
50 slog.Warn("controller: record extension system prompt", "err", err)
51 }
52 }
53
54 func (c *Controller) basePrompt() string {
55 c.mu.Lock()
56 defer c.mu.Unlock()
57 return c.prompt.base
58 }
59
60 // SystemPrompt returns the authoritative host/extension system prompt.
61 func (c *Controller) SystemPrompt() string {
62 if c == nil {
63 return ""
64 }
65 c.mu.Lock()
66 defer c.mu.Unlock()
67 return c.prompt.base
68 }
69
70 // runModelTurn is the sole controller-to-runner entry point. The controller's
71 // turn gate is already held, so its session path cannot rotate while the loader
72 // reads the sidecar. StagePinnedContext only mutates Agent host state; the
73 // revision is appended atomically with the real user message after
74 // agent.before_start accepts the turn.
75 func (c *Controller) runModelTurn(ctx context.Context, input string) error {
76 if c == nil || c.runner == nil {
77 return nil
78 }
79 turnID := c.RuntimeStatus().TurnID
80 c.beginProviderDiagnosticTurn(turnID)
81 ctx = provider.WithRequestObserver(ctx, func(observation provider.RequestObservation) {
82 c.recordProviderRequest(turnID, observation)
83 })
84 if c.goals.active() {
85 ctx = agent.WithContinuationPolicy(ctx, agent.ContinuationExplicitFlow)
86 }
87 if c.pinnedContextLoader != nil && c.executor != nil {
88 snapshot, err := c.pinnedContextLoader(ctx, c.SessionPath())
89 if err != nil {
90 return err
91 }
92 if err := c.executor.StagePinnedContext(snapshot); err != nil {
93 return err
94 }
95 }
96 work := c.beginGoalWork()
97 defer func() { c.recordGoalWork(work, time.Now()) }()
98 return c.runner.Run(ctx, input)
99 }
100
100 lines GO