fix(control,cli): survive SSH drops with mid-turn autosave and SIGHUP save#3943
Merged
Conversation
6b47fb9 to
b34723a
Compare
… save The session only persisted at turn end, so killing the process during a long agent turn lost the whole turn, and the chat TUI caught no signal at all - an SSH disconnect (SIGHUP) died without saving, which is why resume offered a stale conversation (#3772). - snapshot every 30s while a turn runs (atomic tmp+rename write, reads copy under the session lock, so racing the turn is safe) - chat TUI: SIGHUP/SIGTERM persist the conversation, then quit through the normal close path - run/serve contexts also cancel on SIGTERM (and SIGHUP for run) Closes #3772
b34723a to
f7bd990
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause (two gaps)
defer snapshotActivityIfChangedat turn end. The SSH workflow is exactly the one that breaks this: kick off a long autonomous turn, disconnect — the process dies mid-turn and everything since the previous turn is gone, which is whyresumeoffered an old conversation.tea.NewProgram(m)ran bare; onlyrun/servehadNotifyContext(os.Interrupt). An SSH drop delivers SIGHUP and the process died on the spot.Fix
runGuardednow starts a per-turn autosave ticker: while a turn runs, the session snapshots every 30s.Session.Savecopies under the session lock and lands via atomic tmp+rename, so racing the turn's appends can neither tear the read nor corrupt the file; a snapshot that ends on a dangling tool call is repaired by the existing resume-side pairing sanitizer. Worst case after a hard kill is now ~30s of one turn, not the whole turn. (This also narrows the desktop force-quit data-loss window — same mechanism.)p.Quit()so the normal close path runs.runcancels on SIGTERM/SIGHUP too (its in-flight turn then saves via the turn-end defer);serveadds SIGTERM, matching what its own comment already claimed.Interval is a package var; the new test shrinks it and asserts the session file exists while the turn is still blocked. The auto-resume-to-last-session ask from the thread is a separate UX feature and not covered here.
Closes #3772