Skip to content

fix(two): restore work session from resurrect when server is dead - #1286

Merged
shunkakinoki merged 1 commit into
mainfrom
fix/two-resurrect-restore
Mar 27, 2026
Merged

fix(two): restore work session from resurrect when server is dead#1286
shunkakinoki merged 1 commit into
mainfrom
fix/two-resurrect-restore

Conversation

@shunkakinoki

@shunkakinoki shunkakinoki commented Mar 27, 2026

Copy link
Copy Markdown
Owner

Summary

  • Fix two function to properly restore work session via resurrect when tmux server is not running
  • Starts a temp detached session to bootstrap the server, runs resurrect restore, then attaches to work
  • Falls back to tmuxinator start work if no resurrect data exists

Test plan

  • Kill tmux server, run two, verify work session is restored from resurrect
  • Run two when work session already exists, verify it attaches
  • make shell-test passes (253/253)

Summary by cubic

Fixes two so it restores the work session when the tmux server is down. It bootstraps tmux and restores via tmux-resurrect.

  • Bug Fixes
    • If work exists, attach or switch immediately.
    • If not, and ~/.tmux/resurrect/last exists: start a detached _restore session to start tmux, run the resurrect restore script, then clean up and attach to work.
    • If restore data is missing or no work is created, fall back to tmuxinator start work.

Written for commit 0950d91. Summary will update on new commits.

Copilot AI review requested due to automatic review settings March 27, 2026 02:37
@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

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e3fdd436-a579-4aa4-b895-ccedd487c2fc

📥 Commits

Reviewing files that changed from the base of the PR and between a8f9942 and 0950d91.

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

Disabled knowledge base sources:

  • Linear integration is disabled

You can enable these sources in your CodeRabbit configuration.


📝 Walkthrough

Summary by CodeRabbit

Release Notes

  • Refactor

    • Improved tmux session initialization with refined conditional flow for handling different session states and recovery scenarios.
  • Tests

    • Updated test suite to verify new session initialization logic and recovery workflows.

Walkthrough

The _two_function fish shell function was refactored to improve tmux session lifecycle management. The logic now checks for an existing work session and attaches if present, handles tmux resurrect restoration via a temporary session when the work session is absent, and falls back to tmuxinator. Test coverage was updated to mock additional tmux subcommands and isolate resurrect state during testing.

Changes

Cohort / File(s) Summary
Function Implementation
home-manager/programs/fish/functions/_two_function.fish
Removed unconditional resurrect check; added early return for existing work session; introduced conditional restoration flow using temporary session and resurrect scripts; added fallback to tmuxinator when no resurrect path exists.
Test Coverage
spec/fish/_two_function_test.fish
Extended tmux test double to handle new-session and kill-session subcommands; added setup/teardown logic to temporarily remove ~/.tmux/resurrect/last file, isolating resurrect state from test execution.

Sequence Diagram

sequenceDiagram
    participant Fish as Fish Function
    participant Tmux as Tmux
    participant Resurrect as Resurrect Script
    participant Tmuxinator as Tmuxinator

    Fish->>Tmux: has-session -t work
    alt Session Exists
        Tmux-->>Fish: success (exit 0)
        Fish->>Tmux: attach-session -t work
    else Session Not Found
        Tmux-->>Fish: failure (exit 1)
        Fish->>Fish: check ~/.tmux/resurrect/last
        alt Resurrect Path Exists
            Fish->>Tmux: new-session -d -s _restore
            Fish->>Tmux: run-shell (restore.sh)
            Tmux->>Resurrect: execute restore script
            Resurrect-->>Tmux: restoration complete
            Fish->>Tmux: has-session -t work
            alt Work Session Created
                Fish->>Tmux: attach-session -t work
            else Work Session Not Created
                Fish->>Tmux: kill-session -t _restore
            end
        else No Resurrect Path
            Fish->>Tmuxinator: start work
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Hopping through sessions with tmux so neat,
Resurrect paths now make the flow complete,
Temporary burrows aid the restore,
Then attach with purpose, not guesswork anymore!

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/two-resurrect-restore

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

Fix two function to properly restore work session via resurrect when tmux server is not running

  • Starts a temp detached session to bootstrap the server, runs resurrect restore, then attaches to work
  • Falls back to tmuxinator start work if no resurrect data exists

What changed?

Description generated by Mesa. Update settings

@shunkakinoki
shunkakinoki merged commit 2f370aa into main Mar 27, 2026
34 of 35 checks passed
@shunkakinoki
shunkakinoki deleted the fix/two-resurrect-restore branch March 27, 2026 02:38

@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 refactors the _two_function fish function to attempt session restoration via tmux-resurrect before falling back to tmuxinator. The implementation involves creating a temporary tmux session to execute the restoration script. Feedback suggests improving the test suite's reliability by using an exit handler for file restoration to avoid potential data loss. Additionally, it is recommended to consolidate the session cleanup logic in the main function to reduce redundancy.

Comment on lines +15 to +25
set -l _bak ""
if test -f ~/.tmux/resurrect/last
set _bak (mktemp)
mv ~/.tmux/resurrect/last $_bak
end

_two_function

if test -n "$_bak"
mv $_bak ~/.tmux/resurrect/last
end

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 manual backup and restore of ~/.tmux/resurrect/last is not robust. If the test script fails or is interrupted after moving the file (line 18) but before restoring it (line 24), the user's file will be lost. This could be made safer by using an exit handler to guarantee the file is restored.

Consider using functions --on-process-exit to define a cleanup function that will run regardless of how the script terminates.

Example:

function _cleanup_resurrect_file --on-process-exit
    if set -q --global __resurrect_last_bak
        # Logic to move file back
        if test -f "$__resurrect_last_bak"
            mv "$__resurrect_last_bak" ~/.tmux/resurrect/last
        end
        set -e __resurrect_last_bak
    end
end

if test -f ~/.tmux/resurrect/last
    set -g __resurrect_last_bak (mktemp)
    mv ~/.tmux/resurrect/last $__resurrect_last_bak
end

# ... test logic ...
# No manual restore needed here

Comment on lines +20 to +26
# Clean up temp session if restore created work
if tmux has-session -t work 2>/dev/null
tmux kill-session -t _restore 2>/dev/null
tmux attach-session -t work
return
end
tmux kill-session -t _restore 2>/dev/null

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 cleanup logic for the temporary _restore session can be simplified to avoid repeating the tmux kill-session command in the source code. This makes the code more DRY (Don't Repeat Yourself) and easier to maintain.

    # Clean up temp session and attach if restore created work
    tmux has-session -t work 2>/dev/null
    set -l work_restored $status
    tmux kill-session -t _restore 2>/dev/null
    if test $work_restored -eq 0
      tmux attach-session -t work
      return
    end

@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.

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="spec/fish/_two_function_test.fish">

<violation number="1" location="spec/fish/_two_function_test.fish:18">
P2: This test mutates the real ~/.tmux/resurrect/last file and only restores it if the script reaches the cleanup block. If the test aborts early, the user’s resurrect data is lost. Use a guaranteed cleanup (fish_exit handler) or avoid moving the real file.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

set -l _bak ""
if test -f ~/.tmux/resurrect/last
set _bak (mktemp)
mv ~/.tmux/resurrect/last $_bak

@cubic-dev-ai cubic-dev-ai Bot Mar 27, 2026

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.

P2: This test mutates the real ~/.tmux/resurrect/last file and only restores it if the script reaches the cleanup block. If the test aborts early, the user’s resurrect data is lost. Use a guaranteed cleanup (fish_exit handler) or avoid moving the real file.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At spec/fish/_two_function_test.fish, line 18:

<comment>This test mutates the real ~/.tmux/resurrect/last file and only restores it if the script reaches the cleanup block. If the test aborts early, the user’s resurrect data is lost. Use a guaranteed cleanup (fish_exit handler) or avoid moving the real file.</comment>

<file context>
@@ -1,23 +1,35 @@
+set -l _bak ""
+if test -f ~/.tmux/resurrect/last
+    set _bak (mktemp)
+    mv ~/.tmux/resurrect/last $_bak
+end
+
</file context>
Fix with Cubic

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

Updates the Fish two helper to better restore the work tmux session from tmux-resurrect when the tmux server isn’t running, with fallback to tmuxinator.

Changes:

  • Reworks _two_function to (1) attach/switch if work exists, otherwise (2) bootstrap tmux and attempt resurrect restore, then (3) fall back to tmuxinator start work.
  • Updates the Fish spec for _two_function to account for the new tmux subcommands used by the restore path.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
spec/fish/_two_function_test.fish Adjusts stubs and setup for the “no resurrect file” scenario and existing-session attach behavior.
home-manager/programs/fish/functions/_two_function.fish Adds resurrect-based restore flow when work is missing and server may be down, with tmuxinator fallback.

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

Comment on lines +14 to +25
# Ensure no resurrect file interferes
set -l _bak ""
if test -f ~/.tmux/resurrect/last
set _bak (mktemp)
mv ~/.tmux/resurrect/last $_bak
end

_two_function

if test -n "$_bak"
mv $_bak ~/.tmux/resurrect/last
end

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.

This test manipulates the real ~/.tmux/resurrect/last on the machine running the suite (moves it out of the way and back). That makes the test non-hermetic and can clobber a developer’s resurrect state if the test aborts before the restore step. Prefer isolating by setting HOME to a temp dir (as other fish specs do) and creating/removing a resurrect file under that temp HOME instead of touching the real ~ path.

Copilot uses AI. Check for mistakes.
Comment on lines +13 to +16
# Start a detached session so the server is running
tmux new-session -d -s _restore 2>/dev/null
set -l restore (tmux list-keys 2>/dev/null | string match -rg '(/\S+/resurrect/scripts/restore\.sh)')
set -l restore $restore[1]

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.

Using a fixed temp session name (_restore) is risky: if a user already has a session with that name, this function will unconditionally kill it. Consider generating a unique session name (e.g., include PID/random) and only killing the session if it was actually created by this function.

Copilot uses AI. Check for mistakes.
# Clean up temp session if restore created work
if tmux has-session -t work 2>/dev/null
tmux kill-session -t _restore 2>/dev/null
tmux attach-session -t work

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.

In the resurrect-restore path, this always runs tmux attach-session -t work when work appears, even if $TMUX is set (already inside tmux). Attaching from inside tmux will fail; this should mirror the earlier logic and switch-client -t work when inside tmux.

Suggested change
tmux attach-session -t work
if test -n "$TMUX"
tmux switch-client -t work
else
tmux attach-session -t work
end

Copilot uses AI. Check for mistakes.
Comment on lines +11 to 27
# No work session — try resurrect restore
if test -f ~/.tmux/resurrect/last
# Start a detached session so the server is running
tmux new-session -d -s _restore 2>/dev/null
set -l restore (tmux list-keys 2>/dev/null | string match -rg '(/\S+/resurrect/scripts/restore\.sh)')
set -l restore $restore[1]
if test -n "$restore"
tmux run-shell "$restore"
end
# Clean up temp session if restore created work
if tmux has-session -t work 2>/dev/null
tmux kill-session -t _restore 2>/dev/null
tmux attach-session -t work
return
end
tmux kill-session -t _restore 2>/dev/null
end

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 new resurrect-restore behavior (creating a detached bootstrap session, running restore, then switching/attaching) isn’t covered by the fish specs yet. Adding a test that simulates ~/.tmux/resurrect/last existing and asserts that tmux run-shell is invoked and the function attaches/switches to work would prevent regressions in this main new code path.

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