mirror of
https://github.com/patriceckhart/zot.git
synced 2026-06-26 21:36:31 +02:00
Until now an interactive session's turns lived only in the running agent's in-memory transcript; the session file on disk got nothing new until iv.Run returned and the deferred WriteNewTranscript at the bottom of runInteractive fired. That meant any process death that bypassed the deferred flush \u2014 closed terminal window (SIGHUP), system shutdown (SIGTERM), kill -9, an OS / power crash \u2014 took the entire session with it. The window of data-at-risk was "everything since the TUI started or last switched session." Two changes that close the window: - Add OnMessageAppended / OnUsage hooks on core.Agent, fired right after each transcript message is appended (user prompt, finalised assistant turn, tool-results message, the OpenAI image mirror) and after each usage event arrives. The interactive runtime in cli.go binds them to sess.AppendMessage / sess.AppendUsage so every finished turn lands in the session JSONL the moment it's durable in memory. A persistMu mutex coordinates the agent goroutine's per-message writes with the TUI goroutine's session swap (/sessions) and explicit flushes (/session export). sessBaselineMsgs advances in lock-step so the exit-time WriteNewTranscript no longer double-writes rows already on disk. - Install a SIGTERM / SIGHUP handler in runInteractive that flushes any not-yet-persisted in-flight turn before exiting. SIGINT is intentionally NOT handled \u2014 the TUI consumes Ctrl+C as a regular key event for cancel/clear semantics, so installing a SIGINT notifier here would swallow it. The handler exits with os.Exit(0) rather than re-raising, because re-raising would skip the chance to flush and the only at-risk state we care about (the session file) is already flushed by the time we get there. Build closures (BuildAgent / BuildAgentFor) are re-wrapped after the persistence wiring exists so any agent the TUI constructs on login or /model swap also gets the hooks; otherwise switching provider would silently revert to the old in-memory-only behaviour. Print and JSON modes are unaffected: they run a single turn and already flush via WriteNewTranscript at the end of their handler. |
||
|---|---|---|
| .. | ||
| agent | ||
| assets | ||
| auth | ||
| core | ||
| extproto | ||
| provider | ||
| skills | ||
| tui | ||