返回 DeepSeek-Reasonix
session_events.go
根目录 / internal / control / session_events.go
1 package control
2
3 import (
4 "context"
5 "crypto/sha256"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "os"
10 "path/filepath"
11 "reflect"
12 "runtime"
13 "strings"
14 "sync"
15
16 "reasonix/internal/agent"
17 "reasonix/internal/event"
18 "reasonix/internal/provider"
19 "reasonix/internal/session"
20 "reasonix/internal/turnevent"
21 )
22
23 func sessionDirectory(sessionPath string) string {
24 sessionPath = filepath.Clean(strings.TrimSpace(sessionPath))
25 if sessionPath == "." || sessionPath == "" {
26 return ""
27 }
28 // Windows spellings of one path must retain one shared writer identity.
29 if runtime.GOOS == "windows" {
30 sessionPath = strings.ToLower(sessionPath)
31 }
32 parent := filepath.Dir(sessionPath)
33 root := filepath.Join(parent, "sessions-v4")
34 if filepath.Base(parent) == "sessions" {
35 root = filepath.Join(filepath.Dir(parent), "sessions-v4")
36 }
37 id := agent.BranchID(sessionPath)
38 if id == "" {
39 return ""
40 }
41 return filepath.Join(root, id)
42 }
43
44 type sharedSessionEventStore struct {
45 store *session.Session
46 refs int
47 }
48
49 var processSessionEventStores = struct {
50 sync.Mutex
51 stores map[string]*sharedSessionEventStore
52 }{stores: map[string]*sharedSessionEventStore{}}
53
54 func acquireSessionEventStore(dir, id string) (*session.Session, func(context.Context) error, bool, error) {
55 processSessionEventStores.Lock()
56 defer processSessionEventStores.Unlock()
57 if entry := processSessionEventStores.stores[dir]; entry != nil {
58 entry.refs++
59 return entry.store, releaseSessionEventStore(dir, entry), true, nil
60 }
61 store, err := session.Open(dir, id)
62 if errors.Is(err, session.ErrSessionNotFound) {
63 store, err = session.CreateStore(dir, id)
64 }
65 if err != nil {
66 return nil, nil, false, err
67 }
68 entry := &sharedSessionEventStore{store: store, refs: 1}
69 processSessionEventStores.stores[dir] = entry
70 return store, releaseSessionEventStore(dir, entry), false, nil
71 }
72
73 func releaseSessionEventStore(dir string, entry *sharedSessionEventStore) func(context.Context) error {
74 var once sync.Once
75 var releaseErr error
76 return func(ctx context.Context) error {
77 once.Do(func() {
78 // Every ownership handoff is a semantic checkpoint even when another
79 // in-process controller already retains the physical writer.
80 _, releaseErr = entry.store.Flush(ctx)
81 processSessionEventStores.Lock()
82 current := processSessionEventStores.stores[dir]
83 if current != entry {
84 processSessionEventStores.Unlock()
85 return
86 }
87 entry.refs--
88 last := entry.refs == 0
89 if last {
90 delete(processSessionEventStores.stores, dir)
91 }
92 processSessionEventStores.Unlock()
93 if last {
94 releaseErr = errors.Join(releaseErr, entry.store.Close(ctx))
95 }
96 })
97 return releaseErr
98 }
99 }
100
101 func (c *Controller) openSessionEventStore(sessionPath string) (*session.Session, func(context.Context) error, error) {
102 if c.NativeLegacySession() {
103 // The original transcript remains the only message authority. Runtime
104 // envelopes use the legacy ledger; opening must not seed a v4 copy.
105 return nil, nil, nil
106 }
107 if service, runtime, exclusive := c.v3Binding(); runtime != nil {
108 return runtime.Session(), nil, nil
109 } else if exclusive && service != nil {
110 // A service-backed controller needs a published Runtime before admission.
111 // Creating a path-derived sidecar here would reintroduce a second producer
112 // outside the v3.1 ownership boundary.
113 return nil, nil, nil
114 }
115 dir := sessionDirectory(sessionPath)
116 if dir == "" {
117 return nil, nil, nil
118 }
119 id := agent.BranchID(sessionPath)
120 store, release, shared, err := acquireSessionEventStore(dir, id)
121 if err != nil {
122 return nil, nil, err
123 }
124 if !shared {
125 if _, _, err := store.RecoverInterrupted(context.Background()); err != nil {
126 _ = release(context.Background())
127 return nil, nil, err
128 }
129 }
130 snapshot := store.StateSnapshot()
131 if snapshot.EventSequence != 0 {
132 return store, release, nil
133 }
134 return store, release, nil
135 }
136
137 // releaseLegacyEventStoreForImport stops this controller's retired path-bound
138 // event producer before the importer takes a shared freeze lock. Other
139 // controllers keep their own reference; in that case the freeze correctly
140 // refuses to race an active producer instead of copying a moving prefix.
141 func (c *Controller) releaseLegacyEventStoreForImport(ctx context.Context) (func(), error) {
142 if c == nil {
143 return func() {}, nil
144 }
145 path := c.SessionPath()
146 c.turnEvents.mu.Lock()
147 if c.turnEvents.v3 == nil || strings.HasPrefix(c.turnEvents.v3Path, "session:") {
148 c.turnEvents.mu.Unlock()
149 return func() {}, nil
150 }
151 store := c.turnEvents.v3
152 release := c.turnEvents.v3Release
153 c.turnEvents.v3 = nil
154 c.turnEvents.v3Path = ""
155 c.turnEvents.v3Runtime = nil
156 c.turnEvents.v3Release = nil
157 c.turnEvents.mu.Unlock()
158 var err error
159 if release != nil {
160 err = release(ctx)
161 } else {
162 err = store.Close(ctx)
163 }
164 restore := func() {
165 if _, runtime, _ := c.v3Binding(); runtime == nil {
166 c.rebindTurnEvents(path)
167 }
168 }
169 return restore, err
170 }
171
172 // SuspendLegacyEventStoreForImport closes this controller's path-derived
173 // compatibility producer while an idle host prepares a replacement runtime.
174 // The caller must serialize turn admission for the controller. Calling the
175 // returned function restores the producer when candidate preparation fails;
176 // after a successful publication the old controller is retired instead.
177 func (c *Controller) SuspendLegacyEventStoreForImport(ctx context.Context) (func(), error) {
178 return c.releaseLegacyEventStoreForImport(ctx)
179 }
180
181 func (c *Controller) seedSessionEventsFromExecutor(reason string) error {
182 if !c.sessionEventCommitAllowed() {
183 return nil
184 }
185 if snapshot, ok := c.sessionEventSnapshot(); !ok || snapshot.EventSequence != 0 {
186 return nil
187 }
188 if c == nil || c.executor == nil {
189 return nil
190 }
191 // Existing legacy transcripts are loaded by Resume after Controller
192 // construction. Seeding the constructor's system-only placeholder here
193 // would make it look authoritative and discard the loaded history.
194 if reason == "session-open" {
195 if info, err := os.Stat(c.SessionPath()); err == nil && info.Size() > 0 {
196 return nil
197 }
198 }
199 messages := c.executor.Session().Snapshot()
200 for i := range messages {
201 if strings.TrimSpace(messages[i].ID) == "" {
202 messages[i].ID = agent.NewMessageID()
203 }
204 }
205 if err := c.RecordSessionMessages(context.Background(), reason, messages); err != nil {
206 return err
207 }
208 return nil
209 }
210
211 // importLegacyResumeOverPlaceholder repairs the only safe pre-migration
212 // overlap: an older build may have created a system-only v3 sidecar before it
213 // loaded an existing legacy transcript. No executed turn can be present in
214 // that projection, so replacing it with the frozen loaded history is lossless.
215 func (c *Controller) importLegacyResumeOverPlaceholder(incoming *agent.Session) error {
216 if c == nil || incoming == nil {
217 return nil
218 }
219 if !c.sessionEventCommitAllowed() {
220 return nil
221 }
222 snapshot, ok := c.sessionEventSnapshot()
223 if !ok || len(snapshot.Projection.ModelMessages) != 1 || len(incoming.Snapshot()) <= 1 {
224 return nil
225 }
226 messages := incoming.Snapshot()
227 for i := range messages {
228 if strings.TrimSpace(messages[i].ID) == "" {
229 messages[i].ID = agent.NewMessageID()
230 }
231 }
232 return c.replaceSessionEventProjection(context.Background(), "legacy-resume-placeholder", messages)
233 }
234
235 func (c *Controller) restoreExecutorFromSessionEvents() {
236 if c == nil || c.executor == nil {
237 return
238 }
239 snapshot, ok := c.sessionEventSnapshot()
240 if !ok || len(snapshot.Projection.ModelMessages) == 0 {
241 return
242 }
243 if reflect.DeepEqual(c.executor.Session().Snapshot(), snapshot.Projection.ModelMessages) {
244 return
245 }
246 // Called only at construction or explicit Resume. Path rewrites and recovery
247 // rebinds must keep their prepared candidate session instead of adopting the
248 // source log again.
249 c.executor.Session().Replace(append([]provider.Message(nil), snapshot.Projection.ModelMessages...))
250 }
251
252 // replaceSessionModelContext records an Agent/configuration rebuild without
253 // rewriting the UI transcript. The exact serialized model context becomes a
254 // typed event, while the original messages remain available for history.
255 func (c *Controller) replaceSessionModelContext(ctx context.Context, messages []provider.Message, reason string) error {
256 store := c.sessionEventStore()
257 if store == nil {
258 return errors.New("exclusive v3 controller has no session store")
259 }
260 snapshot := store.ExecutionSnapshot()
261 modelMessages := provider.ModelMessages(messages)
262 if modelMessages == nil {
263 modelMessages = []provider.Message{}
264 }
265 payload, err := json.Marshal(map[string]any{
266 "messages": modelMessages,
267 "reason": reason,
268 "sourceSequences": []uint64{snapshot.EventSequence},
269 })
270 if err != nil {
271 return err
272 }
273 events := []session.Event{{Kind: "model/context-replace", Payload: payload}}
274 if strings.TrimSpace(c.ModelRef()) != "" {
275 configPayload, err := json.Marshal(map[string]string{"modelRef": c.ModelRef(), "modelIdentity": c.ModelSelectionIdentity()})
276 if err != nil {
277 return err
278 }
279 events = append(events, session.Event{Kind: "session/config", Payload: configPayload})
280 }
281 digest := sha256.Sum256(payload)
282 c.turnEvents.commitMu.Lock()
283 batch := session.Batch{
284 OperationID: fmt.Sprintf("model-context:%x", digest[:16]),
285 TurnID: snapshot.Projection.TurnID,
286 Events: events,
287 }
288 _, runtime, exclusive := c.v3Binding()
289 if exclusive && runtime != nil && runtime.Session() == store && !runtime.OwnsExecution(c.ExecutionGeneration()) {
290 prepared, prepareErr := store.PrepareBatchContext(ctx, batch.OperationID, batch)
291 if prepareErr == nil {
292 if previous := c.turnEvents.pendingExecutionCommit; previous != nil {
293 previous.Release()
294 }
295 c.turnEvents.pendingExecutionCommit = &prepared
296 }
297 err = prepareErr
298 } else {
299 _, err = c.appendSessionBatch(ctx, store, batch)
300 }
301 c.turnEvents.commitMu.Unlock()
302 if err != nil {
303 return err
304 }
305 if c.executor != nil {
306 c.executor.Session().Replace(append([]provider.Message(nil), modelMessages...))
307 }
308 return nil
309 }
310
311 func (c *Controller) adoptResumeSystemPrompt(incoming *agent.Session) error {
312 if !c.sessionEventCommitAllowed() {
313 return nil
314 }
315 store := c.sessionEventStore()
316 if store == nil || incoming == nil {
317 return nil
318 }
319 snapshot := store.ExecutionSnapshot()
320 persisted, current := snapshot.Projection.ModelMessages, incoming.Snapshot()
321 if len(persisted) == 0 || len(current) == 0 || persisted[0].Role != provider.RoleSystem || current[0].Role != provider.RoleSystem || reflect.DeepEqual(persisted[0], current[0]) {
322 return nil
323 }
324 replaced := append([]provider.Message(nil), persisted...)
325 replaced[0] = current[0]
326 payload, err := json.Marshal(map[string]any{"messages": replaced, "reason": "system-prompt-refresh"})
327 if err != nil {
328 return err
329 }
330 c.turnEvents.commitMu.Lock()
331 defer c.turnEvents.commitMu.Unlock()
332 _, err = c.appendSessionBatch(context.Background(), store, session.Batch{
333 OperationID: fmt.Sprintf("system-prompt-refresh:%d", snapshot.EventSequence+1),
334 TurnID: snapshot.Projection.TurnID,
335 Events: []session.Event{{Kind: "history/replace", Payload: payload}},
336 })
337 return err
338 }
339
340 func (c *Controller) sessionEventStore() *session.Session {
341 if c == nil {
342 return nil
343 }
344 c.turnEvents.mu.RLock()
345 defer c.turnEvents.mu.RUnlock()
346 return c.turnEvents.v3
347 }
348
349 // sessionEventCommitAllowed fences unpublished Desktop replacement runtimes.
350 // Those candidates intentionally share the active process store so they can
351 // inspect the latest in-memory prefix, but they do not own mutation authority
352 // until the tab's final lease handoff succeeds.
353 func (c *Controller) sessionEventCommitAllowed() bool {
354 if _, runtime, _ := c.v3Binding(); runtime != nil {
355 return runtime.OwnsExecution(c.ExecutionGeneration())
356 }
357 if c == nil || !c.managedSessionEvents.Load() {
358 return true
359 }
360 if c.executor == nil || c.executor.Session() == nil {
361 return false
362 }
363 path := c.SessionPath()
364 if path == "" {
365 return true
366 }
367 auth := c.executor.Session().WriteAuthority()
368 return auth != nil && auth.Covers(path)
369 }
370
371 func (c *Controller) sessionEventSnapshot() (session.Snapshot, bool) {
372 store := c.sessionEventStore()
373 if store == nil {
374 return session.Snapshot{}, false
375 }
376 return store.ExecutionSnapshot(), true
377 }
378
379 func (c *Controller) sessionStateSnapshot() (session.Snapshot, bool) {
380 store := c.sessionEventStore()
381 if store == nil {
382 return session.Snapshot{}, false
383 }
384 return store.StateSnapshot(), true
385 }
386
387 // appendSessionEventLocked mirrors the existing event.Sink lifecycle into the
388 // single typed business log. turnEvents.commitMu must be held, which preserves
389 // the exact order assigned by the compatibility envelope adapter.
390 func (c *Controller) appendSessionEventLocked(ctx context.Context, e event.Event) error {
391 if !c.sessionEventCommitAllowed() {
392 return nil
393 }
394 store := c.sessionEventStore()
395 if store == nil {
396 if c.sessionEngineEnabled() {
397 return session.ErrSessionNotRunning
398 }
399 return nil
400 }
401 snapshot := store.ExecutionSnapshot()
402 projection := snapshot.Projection
403 if projection.Recovery != nil && projection.Recovery.State == "recovery_required" && e.Kind != event.TurnDone {
404 // Recovery has sealed business-state mutation. The watchdog observes
405 // the uncooperative worker; late semantic output must never reactivate
406 // tools, interactions, Goal, or Todo.
407 return nil
408 }
409 if e.TurnID == "" {
410 if _, turnID, active := c.currentTurnToken(); active {
411 e.TurnID = turnID
412 }
413 if e.TurnID == "" {
414 if ledger := c.turnEventLedger(); ledger != nil {
415 e.TurnID = ledger.ActiveTurnID()
416 }
417 }
418 if e.TurnID == "" {
419 e.TurnID = projection.TurnID
420 }
421 }
422 events, err := c.sessionEventsFor(e, projection)
423 if err != nil || len(events) == 0 {
424 return err
425 }
426 if e.Kind == event.TurnDone {
427 if err := c.appendTerminationLocked(ctx, e, store, events); err != nil {
428 return fmt.Errorf("%w: %w", turnevent.ErrTurnLedgerUnavailable, err)
429 }
430 return nil
431 }
432 op := fmt.Sprintf("runtime:%s:%d:%d", e.TurnID, snapshot.EventSequence+1, e.Kind)
433 _, err = c.appendSessionBatch(ctx, store, session.Batch{OperationID: op, TurnID: e.TurnID, Events: events})
434 if err != nil {
435 return fmt.Errorf("%w: %w", turnevent.ErrTurnLedgerUnavailable, err)
436 }
437 c.noteCommittedMessagesLocked(events)
438 return nil
439 }
440
441 func (c *Controller) sessionEventsFor(e event.Event, projection session.Projection) ([]session.Event, error) {
442 if e.Kind == event.Notice {
443 return displayNoticeEvents(e)
444 }
445 if e.Kind == event.SessionOperation {
446 return sessionMaintenanceEvents(e)
447 }
448 return c.v3EventsFor(e, projection)
449 }
450
451 func (c *Controller) v3EventsFor(e event.Event, projection session.Projection) ([]session.Event, error) {
452 makePayload := func(value any) (json.RawMessage, error) { return json.Marshal(value) }
453 var out []session.Event
454 switch e.Kind {
455 case event.TurnStarted:
456 payload, err := makePayload(map[string]any{"status": event.TurnInProgress})
457 if err != nil {
458 return nil, err
459 }
460 out = append(out, session.Event{Kind: "turn/start", Payload: payload})
461 out = c.appendSubmissionEvent(out, e.TurnID)
462 if e.DomainKind != "" {
463 if e.DomainKind != "goal/state" || len(e.DomainPayload) == 0 {
464 return nil, fmt.Errorf("unsupported turn admission domain event %q", e.DomainKind)
465 }
466 out = append(out, session.Event{Kind: e.DomainKind, Payload: append(json.RawMessage(nil), e.DomainPayload...)})
467 }
468 case event.ToolDispatch:
469 payload, err := makePayload(v3ToolPayload(e.Tool, false))
470 if err != nil {
471 return nil, err
472 }
473 out = append(out, session.Event{Kind: "tool/call", Payload: payload})
474 case event.ToolStarted:
475 payload, err := makePayload(map[string]any{"id": e.Tool.ID, "name": e.Tool.Name})
476 if err != nil {
477 return nil, err
478 }
479 out = append(out, session.Event{Kind: "tool/start", Payload: payload})
480 case event.ToolResult:
481 // Tool results are emitted before Agent mutates its derived conversation
482 // cache. Commit the exact message and structured result together so a
483 // recovered prefix cannot contain only one half.
484 if e.CommittedMessage != nil {
485 messagePayload, err := makePayload(map[string]any{"message": *e.CommittedMessage})
486 if err != nil {
487 return nil, err
488 }
489 out = append(out, session.Event{Kind: "message/complete", Payload: messagePayload})
490 }
491 payload, err := makePayload(v3ToolPayload(e.Tool, true))
492 if err != nil {
493 return nil, err
494 }
495 if e.Tool.TodoWritten {
496 todoPayload, err := makePayload(map[string]any{"todos": e.Tool.Todos})
497 if err != nil {
498 return nil, err
499 }
500 out = append(out, session.Event{Kind: "todo/write", Payload: todoPayload})
501 }
502 out = append(out, session.Event{Kind: "tool/result", Payload: payload})
503 case event.StreamAttempt:
504 payload, err := makePayload(map[string]any{
505 "id": e.StreamAttempt.ID, "messageId": e.MessageID,
506 "action": e.StreamAttempt.Action, "attempt": e.StreamAttempt.Attempt,
507 "max": e.StreamAttempt.Max, "reason": e.StreamAttempt.Reason,
508 })
509 if err != nil {
510 return nil, err
511 }
512 out = append(out, session.Event{Kind: "assistant/attempt", Payload: payload})
513 if e.StreamAttempt.Response != nil {
514 // Required attempt payloads are strict in previous readers. Keep
515 // observational metadata in an independently skippable event.
516 payload, err := makePayload(map[string]any{
517 "schemaVersion": 1, "messageId": e.MessageID,
518 "attemptId": e.StreamAttempt.ID, "response": e.StreamAttempt.Response,
519 })
520 if err != nil {
521 return nil, err
522 }
523 out = append(out, session.Event{Kind: "diagnostic/response", Optional: true, Payload: payload})
524 }
525 case event.AskRequest, event.ApprovalRequest, event.MCPInteractionRequest:
526 kind := strings.TrimSpace(e.PromptKind)
527 if kind == "" {
528 switch e.Kind {
529 case event.AskRequest:
530 kind = "ask"
531 case event.MCPInteractionRequest:
532 kind = "mcp"
533 default:
534 kind = "approval"
535 }
536 }
537 payload, err := makePayload(map[string]any{
538 "id": e.ItemID, "toolCallId": e.ItemID, "kind": kind, "state": "pending",
539 "sessionId": e.SessionID, "headId": agent.BranchID(c.SessionPath()),
540 "turnId": e.TurnID, "runtimeEpoch": e.RuntimeEpoch,
541 })
542 if err != nil {
543 return nil, err
544 }
545 out = append(out, session.Event{Kind: "interaction/created", Payload: payload})
546 case event.PromptAnswered:
547 state := strings.TrimSpace(e.InteractionState)
548 if state == "" {
549 state = "answered"
550 }
551 payload, err := makePayload(map[string]any{"id": e.ItemID, "state": state})
552 if err != nil {
553 return nil, err
554 }
555 out = append(out, session.Event{Kind: "interaction/resolved", Payload: payload})
556 if e.DomainKind != "" {
557 out = append(out, session.Event{Kind: e.DomainKind, Payload: append(json.RawMessage(nil), e.DomainPayload...)})
558 }
559 case event.TurnStatusChanged:
560 if e.Status == event.TurnRecoveryRequired && e.Recovery != nil {
561 payload, err := makePayload(e.Recovery)
562 if err != nil {
563 return nil, err
564 }
565 out = append(out, session.Event{Kind: "runtime/recovery", Payload: payload})
566 }
567 case event.CompactionDone:
568 // Context-maintenance commits persist their exact model projection before
569 // this notification is emitted. CompactionDone is presentation-only.
570 case event.TurnDone:
571 return c.terminalSessionEvents(e, projection)
572 }
573 return out, nil
574 }
575
576 // v3ToolPayload retains the structured execution and presentation metadata
577 // emitted by the tool runtime. UI truncation may choose a smaller view later;
578 // the authoritative event must not collapse a result to name plus text.
579 func v3ToolPayload(tool event.Tool, result bool) map[string]any {
580 payload := map[string]any{
581 "id": tool.ID, "name": tool.Name, "args": tool.Args,
582 "runState": tool.RunState, "diagnostic": tool.Diagnostic,
583 "resolvedName": tool.ResolvedName, "capabilityId": tool.CapabilityID,
584 "readOnly": tool.ReadOnly, "truncated": tool.Truncated,
585 "durationMs": tool.DurationMs, "startedAt": tool.StartedAt, "endedAt": tool.EndedAt,
586 "partial": tool.Partial, "argChars": tool.ArgChars, "refreshed": tool.Refreshed,
587 "parentId": tool.ParentID, "attemptId": tool.AttemptID,
588 "subagentRef": tool.SubagentRef, "subagentStatus": tool.SubagentStatus,
589 "subagentErrorCode": tool.SubagentErrorCode, "subagentRetryable": tool.SubagentRetryable,
590 "diff": tool.Diff, "added": tool.Added, "removed": tool.Removed,
591 "profile": tool.Profile, "execution": tool.Execution,
592 "presentedFiles": tool.PresentedFiles,
593 "workspaceMutation": tool.WorkspaceMutation, "workspacePaths": tool.WorkspacePaths,
594 "workspaceAllPaths": tool.WorkspaceAllPaths,
595 }
596 if result {
597 payload["output"] = tool.Output
598 payload["error"] = tool.Err
599 payload["todos"] = tool.Todos
600 payload["todoWritten"] = tool.TodoWritten
601 }
602 return payload
603 }
604
605 // RecordSessionMessages implements agent.SessionEventRecorder. Runtime message
606 // commits arrive here directly; this code never diffs the mutable legacy
607 // transcript to infer missing business events.
608 func (c *Controller) RecordSessionMessages(ctx context.Context, reason string, messages []provider.Message) error {
609 if !c.sessionEventCommitAllowed() {
610 return nil
611 }
612 store := c.sessionEventStore()
613 if store == nil || len(messages) == 0 {
614 return nil
615 }
616 if recovery := store.ExecutionSnapshot().Projection.Recovery; recovery != nil && recovery.State == "recovery_required" {
617 return nil
618 }
619 c.turnEvents.commitMu.Lock()
620 defer c.turnEvents.commitMu.Unlock()
621 if !c.messageCommitAllowedLocked(ctx, store) {
622 return nil
623 }
624 events := make([]session.Event, 0, len(messages))
625 for _, message := range messages {
626 if strings.TrimSpace(message.ID) == "" {
627 return errors.New("record session message: missing stable message id")
628 }
629 payload, err := json.Marshal(map[string]any{"message": message})
630 if err != nil {
631 return err
632 }
633 events = append(events, session.Event{Kind: "message/complete", Payload: payload})
634 }
635 snapshot := store.ExecutionSnapshot()
636 op := fmt.Sprintf("messages:%s:%d", reason, snapshot.EventSequence+1)
637 _, err := c.appendSessionBatch(ctx, store, session.Batch{OperationID: op, TurnID: snapshot.Projection.TurnID, Events: events})
638 if err == nil {
639 c.noteCommittedMessagesLocked(events)
640 c.settleOpenStreamLocked(messages)
641 }
642 return err
643 }
644
645 // RecordSessionModelContext implements agent.SessionModelContextRecorder. It
646 // persists the exact provider-visible projection without mutating the Agent or
647 // the canonical history. A successful return means the accepted commit is on
648 // stable storage through its final sequence.
649 func (c *Controller) RecordSessionModelContext(ctx context.Context, request agent.SessionModelContextCommit) (agent.SessionModelContextCommitResult, error) {
650 var result agent.SessionModelContextCommitResult
651 if !c.sessionEventCommitAllowed() {
652 return result, session.ErrStaleExecution
653 }
654 store := c.sessionEventStore()
655 if store == nil {
656 return result, nil
657 }
658 operationID := strings.TrimSpace(request.OperationID)
659 if operationID == "" {
660 return result, errors.New("record session model context: missing operation id")
661 }
662 modelMessages := provider.ModelMessages(request.Messages)
663 if modelMessages == nil {
664 modelMessages = []provider.Message{}
665 }
666 payload, err := json.Marshal(map[string]any{
667 "messages": modelMessages,
668 "reason": strings.TrimSpace(request.Reason),
669 })
670 if err != nil {
671 return result, err
672 }
673
674 c.turnEvents.commitMu.Lock()
675 if !c.messageCommitAllowedLocked(ctx, store) {
676 c.turnEvents.commitMu.Unlock()
677 return result, session.ErrStaleExecution
678 }
679 commit, err := c.appendSessionBatch(ctx, store, session.Batch{
680 OperationID: "model-context-maintenance:" + operationID,
681 Events: []session.Event{{Kind: "model/context-replace", Payload: payload}},
682 })
683 c.turnEvents.commitMu.Unlock()
684 if err != nil {
685 return result, err
686 }
687 result.Accepted = true
688 receipt, err := store.Flush(ctx)
689 if err != nil {
690 return result, err
691 }
692 if receipt.DurableSequence < commit.LastSequence() {
693 return result, fmt.Errorf("record session model context: durable sequence %d is before commit %d", receipt.DurableSequence, commit.LastSequence())
694 }
695 result.Durable = true
696 return result, nil
697 }
698
699 // RecordSessionMessageUpsert records one explicit stable-message mutation.
700 // This is used for local metadata such as protocol recovery receipts, whose
701 // provider-visible bytes may not change but whose durable UI/business fact must
702 // survive without falling back to a full legacy transcript rewrite.
703 func (c *Controller) RecordSessionMessageUpsert(ctx context.Context, reason string, message provider.Message) error {
704 if !c.sessionEventCommitAllowed() {
705 return nil
706 }
707 store := c.sessionEventStore()
708 if store == nil {
709 return nil
710 }
711 if strings.TrimSpace(message.ID) == "" {
712 return errors.New("record session message upsert: missing stable message id")
713 }
714 payload, err := json.Marshal(map[string]any{"message": message})
715 if err != nil {
716 return err
717 }
718 c.turnEvents.commitMu.Lock()
719 defer c.turnEvents.commitMu.Unlock()
720 if !c.messageCommitAllowedLocked(ctx, store) {
721 return nil
722 }
723 snapshot := store.ExecutionSnapshot()
724 op := fmt.Sprintf("message-upsert:%s:%s:%d", reason, message.ID, snapshot.EventSequence+1)
725 _, err = c.appendSessionBatch(ctx, store, session.Batch{OperationID: op, TurnID: snapshot.Projection.TurnID, Events: []session.Event{{Kind: "message/upsert", Payload: payload}}})
726 return err
727 }
728
729 // replaceSessionEventProjection records an intentional transcript rewrite as
730 // an explicit context event. Recovery, cancellation and host prompt changes use
731 // this path so the typed log remains authoritative without diffing the legacy
732 // message cache at a later checkpoint.
733 func (c *Controller) replaceSessionEventProjection(ctx context.Context, reason string, messages []provider.Message) error {
734 if !c.sessionEventCommitAllowed() {
735 return nil
736 }
737 store := c.sessionEventStore()
738 if store == nil {
739 return nil
740 }
741 payload, err := json.Marshal(map[string]any{
742 "messages": append([]provider.Message(nil), messages...),
743 "reason": reason,
744 })
745 if err != nil {
746 return err
747 }
748 c.turnEvents.commitMu.Lock()
749 defer c.turnEvents.commitMu.Unlock()
750 snapshot := store.ExecutionSnapshot()
751 op := fmt.Sprintf("context-replace:%s:%d", reason, snapshot.EventSequence+1)
752 _, err = c.appendSessionBatch(ctx, store, session.Batch{
753 OperationID: op,
754 TurnID: snapshot.Projection.TurnID,
755 Events: []session.Event{{Kind: "history/replace", Payload: payload}},
756 })
757 return err
758 }
759
760 func (c *Controller) flushSessionEvents(ctx context.Context) (session.DurableReceipt, error) {
761 store := c.sessionEventStore()
762 if store == nil {
763 return session.DurableReceipt{}, nil
764 }
765 return store.Flush(ctx)
766 }
767
768 func (c *Controller) appendDomainState(kind string, payload json.RawMessage, reason string) error {
769 if !c.sessionEventCommitAllowed() {
770 return nil
771 }
772 store := c.sessionEventStore()
773 if store == nil || len(payload) == 0 {
774 return nil
775 }
776 if recovery := store.ExecutionSnapshot().Projection.Recovery; recovery != nil && recovery.State == "recovery_required" {
777 return nil
778 }
779 c.turnEvents.commitMu.Lock()
780 defer c.turnEvents.commitMu.Unlock()
781 if !c.messageCommitAllowedLocked(context.Background(), store) {
782 return nil
783 }
784 snapshot := store.ExecutionSnapshot()
785 op := fmt.Sprintf("domain:%s:%s:%d", kind, reason, snapshot.EventSequence+1)
786 _, err := c.appendSessionBatch(context.Background(), store, session.Batch{OperationID: op, TurnID: snapshot.Projection.TurnID, Events: []session.Event{{Kind: kind, Payload: append(json.RawMessage(nil), payload...)}}})
787 if err != nil {
788 return fmt.Errorf("%w: %w", turnevent.ErrTurnLedgerUnavailable, err)
789 }
790 return nil
791 }
792
792 lines GO