Skip to content

feat(tmux): auto-save on detach/close and fix restore timing - #1287

Merged
shunkakinoki merged 2 commits into
mainfrom
feat/tmux-save-on-kill
Mar 27, 2026
Merged

feat(tmux): auto-save on detach/close and fix restore timing#1287
shunkakinoki merged 2 commits into
mainfrom
feat/tmux-save-on-kill

Conversation

@shunkakinoki

@shunkakinoki shunkakinoki commented Mar 27, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add tmux hooks to auto-save resurrect state on client detach and session close
  • Add sleep after resurrect restore in two to fix race condition

Test plan

  • Kill tmux server, run two, verify work session restores
  • Detach from tmux, check ~/.tmux/resurrect/last is updated

Summary by cubic

Auto-saves tmux-resurrect state on detach and session close, and adjusts two restore timing to avoid races and lost state.

  • New Features

    • Add tmux hooks: client-detached and session-closed run @resurrect-save-script-path to persist state.
  • Bug Fixes

    • In _two_function.fish, add sleep 1 after tmux-resurrect restore to avoid a startup race.

Written for commit c6350b5. Summary will update on new commits.

Copilot AI review requested due to automatic review settings March 27, 2026 03:26
@mesa-dot-dev

mesa-dot-dev Bot commented Mar 27, 2026

Copy link
Copy Markdown

You do not have enough credits to review this pull request. Please purchase more credits to continue.

@coderabbitai

coderabbitai Bot commented Mar 27, 2026

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 536cd61c-b3f2-4f34-9add-4d0063ad61a9

📥 Commits

Reviewing files that changed from the base of the PR and between 2f370aa and 5a888a3.

📒 Files selected for processing (2)
  • home-manager/programs/fish/functions/_two_function.fish
  • home-manager/programs/tmux/tmux.conf

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved terminal session restoration by introducing timing adjustments that ensure recovery scripts complete execution before proceeding with cleanup and reattachment logic.
  • Chores

    • Enhanced session persistence by implementing automatic state saving triggers. Terminal sessions are now automatically saved when clients disconnect or when sessions are closed.

Walkthrough

Two configuration and timing adjustments added to enhance tmux session state management: a one-second delay introduced in a fish function after tmux restoration, and two new tmux hooks configured to automatically trigger session state persistence on client detachment and session closure.

Changes

Cohort / File(s) Summary
Fish Function Timing
home-manager/programs/fish/functions/_two_function.fish
Added sleep 1 delay after tmux run-shell invocation in restore script execution to ensure proper timing before subsequent logic.
Tmux Session Persistence
home-manager/programs/tmux/tmux.conf
Configured client-detached and session-closed hooks to automatically invoke Resurrect save script on session events.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A little sleep, a hook or two,
Sessions saved when you bid adieu,
Persistence blooms with careful care,
Tmux memories floating in the air! 🌙

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main changes: adding tmux hooks for auto-save and fixing restore timing in the two function.
Description check ✅ Passed The description is directly related to the changeset, providing clear context on the tmux hooks added and the sleep delay fix in the restore process.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/tmux-save-on-kill

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mesa-dot-dev

mesa-dot-dev Bot commented Mar 27, 2026

Copy link
Copy Markdown

Mesa Description

TL;DR

Auto-saves tmux-resurrect state on client detach and session close, and adds a delay to two function to fix a race condition during session restore.

What changed?

  • home-manager/programs/fish/functions/_two_function.fish: Added a one-second delay after tmux run-shell to fix a restore timing race condition.
  • home-manager/programs/tmux/tmux.conf: Implemented tmux hooks (client-detached and session-closed) to automatically save tmux-resurrect state.

Description generated by Mesa. Update settings

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces automatic tmux session saving when a client detaches or a session closes, and adds a one-second delay in the fish shell function to prevent race conditions during session restoration. The review feedback suggests adding error handling for the tmux save hooks to avoid silent failures and documenting the hardcoded sleep duration in the fish script to improve maintainability.

set -g @continuum-save-interval '3'

# Auto-save on detach/close so kills don't lose state
set-hook -g client-detached 'run-shell #{@resurrect-save-script-path}'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The run-shell command executes the save script, but if the script fails (e.g., due to @resurrect-save-script-path being undefined, pointing to a non-executable file, or the script itself encountering an error), the save operation will silently fail. This could lead to unexpected data loss, as the user would assume their session state is being saved on detach/close. It is recommended to add error handling or logging within the script or the run-shell command (e.g., sh -c "script || tmux display-message ...") to provide feedback if the save fails.

set -l restore $restore[1]
if test -n "$restore"
tmux run-shell "$restore"
sleep 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The sleep 1 command addresses the race condition, but using a hardcoded numerical delay can be fragile. If the system is under heavy load or the tmux restore operation takes longer than expected, 1 second might not be sufficient, leading to the race condition re-emerging. Conversely, if 1 second is consistently more than needed, it introduces an unnecessary delay. Adding a comment to explain the rationale behind the 1-second delay improves maintainability by clarifying the purpose of this magic number.

      sleep 1 # Empirically determined delay to allow tmux session restoration to complete.

@shunkakinoki
shunkakinoki merged commit c2c4d33 into main Mar 27, 2026
7 checks passed
@shunkakinoki
shunkakinoki deleted the feat/tmux-save-on-kill branch March 27, 2026 03:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds tmux hooks intended to automatically persist tmux-resurrect state when detaching or closing sessions, reducing the chance of losing the latest “work” session snapshot.

Changes:

  • Add global tmux hooks for client-detached and session-closed to trigger tmux-resurrect save.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


# Auto-save on detach/close so kills don't lose state
set-hook -g client-detached 'run-shell #{@resurrect-save-script-path}'
set-hook -g session-closed 'run-shell #{@resurrect-save-script-path}'

Copilot AI Mar 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The session-closed hook runs when a session is closed, so @resurrect-save-script-path will snapshot after the session is gone. Because the post-save hook filters the saved file down to work panes, closing the work session can overwrite ~/.tmux/resurrect/last with an effectively empty snapshot, breaking restore. Consider removing the session-closed hook, or wrapping the save command to no-op unless the work session still exists (to avoid clobbering the last good snapshot).

Suggested change
set-hook -g session-closed 'run-shell #{@resurrect-save-script-path}'

Copilot uses AI. Check for mistakes.
Comment on lines +137 to +138
set-hook -g client-detached 'run-shell #{@resurrect-save-script-path}'
set-hook -g session-closed 'run-shell #{@resurrect-save-script-path}'

Copilot AI Mar 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run-shell without -b runs synchronously; on detach/close this can block the tmux hook until resurrect finishes saving (potentially slow when capturing pane contents). Consider using background mode (run-shell -b ...) to avoid UI delays/hangs during detach/close.

Suggested change
set-hook -g client-detached 'run-shell #{@resurrect-save-script-path}'
set-hook -g session-closed 'run-shell #{@resurrect-save-script-path}'
set-hook -g client-detached 'run-shell -b #{@resurrect-save-script-path}'
set-hook -g session-closed 'run-shell -b #{@resurrect-save-script-path}'

Copilot uses AI. Check for mistakes.
Comment on lines +136 to +138
# Auto-save on detach/close so kills don't lose state
set-hook -g client-detached 'run-shell #{@resurrect-save-script-path}'
set-hook -g session-closed 'run-shell #{@resurrect-save-script-path}'

Copilot AI Mar 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description mentions adding a sleep after resurrect restore in two to fix a race, but _two_function.fish currently has no such delay (it runs tmux run-shell "$restore" and immediately checks for the work session). Either include that change or update the PR description so it matches what’s being shipped.

Copilot uses AI. Check for mistakes.
set -g @continuum-restore 'off'
set -g @continuum-save-interval '3'

# Auto-save on detach/close so kills don't lose state

Copilot AI Mar 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment says “kills don't lose state”, but these hooks won’t run if the tmux server is killed (e.g., kill-server/SIGKILL). Consider rewording to reflect detach/session-close only, so the config doesn’t over-promise durability.

Suggested change
# Auto-save on detach/close so kills don't lose state
# Auto-save on detach/normal session close to preserve state

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants