Skip to content

Commit c99d65a

Browse files
committed
fix: restore model from sidecar field when not in cli_args
The save script captures the model from Claude state files, but restore never used the model field — sessions lost their model setting on every save/restore cycle. Now adds --model to the resume command when: - model is present in the sidecar JSON - tool is claude (only tool that supports --model) - --model is not already in cli_args (avoids duplication) Closes #23
1 parent d85407a commit c99d65a

2 files changed

Lines changed: 119 additions & 2 deletions

File tree

scripts/restore-assistant-sessions.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ while read -r entry; do
5858
session_id=$(echo "$entry" | jq -r '.session_id')
5959
cwd=$(echo "$entry" | jq -r '.cwd')
6060
cli_args=$(echo "$entry" | jq -r '.cli_args // empty')
61+
model=$(echo "$entry" | jq -r '.model // empty')
6162
env_json=$(echo "$entry" | jq -c '.env // {}')
6263

6364
# Check if the target pane's session exists
@@ -158,11 +159,21 @@ while read -r entry; do
158159
set +f
159160
fi
160161

162+
# Add --model from the sidecar model field if not already in cli_args.
163+
# Only for Claude — OpenCode and Codex don't support --model.
164+
safe_model_arg=""
165+
if [ -n "$model" ] && [ "$tool" = "claude" ]; then
166+
case "$cli_args" in
167+
*--model*) ;;
168+
*) safe_model_arg=" --model $(posix_quote "$model")" ;;
169+
esac
170+
fi
171+
161172
resume_cmd=""
162173
case "$tool" in
163174
claude)
164-
if [ -n "$safe_cli_args" ]; then
165-
resume_cmd="command claude${safe_cli_args} --resume ${safe_sid}"
175+
if [ -n "$safe_cli_args" ] || [ -n "$safe_model_arg" ]; then
176+
resume_cmd="command claude${safe_cli_args}${safe_model_arg} --resume ${safe_sid}"
166177
else
167178
resume_cmd="command claude --resume ${safe_sid}"
168179
fi

test/run-tests.sh

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,6 +2557,112 @@ assert_contains "Empty cli_args: tool identified" "$empty_log" "restoring openco
25572557
25582558
kill_pane_children test-restore-empty true
25592559
2560+
# --- Test 10d2: Restore adds --model from sidecar model field ---
2561+
#
2562+
# When model is set in the sidecar JSON but NOT in cli_args (e.g., model
2563+
# was set via config/env, not --model flag), restore should add --model.
2564+
2565+
echo ""
2566+
echo "=== Test 10d2: restore adds --model from sidecar field ==="
2567+
echo ""
2568+
2569+
tmux new-session -d -s test-restore-model -c /tmp 2>/dev/null || true
2570+
sleep 0.5
2571+
2572+
cat >"$HOME/.tmux/resurrect/assistant-sessions.json" <<'RMODEL'
2573+
{
2574+
"timestamp": "2026-01-01T00:00:00Z",
2575+
"sessions": [
2576+
{
2577+
"pane": "test-restore-model:0.0",
2578+
"tool": "claude",
2579+
"session_id": "ses_model_field",
2580+
"cwd": "/tmp",
2581+
"pid": "99999",
2582+
"model": "claude-opus-4-5-20250514",
2583+
"cli_args": "",
2584+
"env": {}
2585+
}
2586+
]
2587+
}
2588+
RMODEL
2589+
2590+
>"$RESTORE_LOG"
2591+
just restore 2>&1
2592+
sleep 5
2593+
2594+
model_log=$(cat "$RESTORE_LOG")
2595+
assert_contains "Model field: --model added to resume" "$model_log" "--model"
2596+
assert_contains "Model field: correct model value" "$model_log" "claude-opus-4-5-20250514"
2597+
2598+
# Verify --model is NOT duplicated when already in cli_args
2599+
tmux kill-session -t test-restore-model 2>/dev/null || true
2600+
tmux new-session -d -s test-restore-model -c /tmp 2>/dev/null || true
2601+
sleep 0.5
2602+
2603+
cat >"$HOME/.tmux/resurrect/assistant-sessions.json" <<'RMODELDUP'
2604+
{
2605+
"timestamp": "2026-01-01T00:00:00Z",
2606+
"sessions": [
2607+
{
2608+
"pane": "test-restore-model:0.0",
2609+
"tool": "claude",
2610+
"session_id": "ses_model_nodup",
2611+
"cwd": "/tmp",
2612+
"pid": "99999",
2613+
"model": "claude-opus-4-5-20250514",
2614+
"cli_args": "--model claude-opus-4-5-20250514",
2615+
"env": {}
2616+
}
2617+
]
2618+
}
2619+
RMODELDUP
2620+
2621+
>"$RESTORE_LOG"
2622+
just restore 2>&1
2623+
sleep 5
2624+
2625+
nodup_log=$(cat "$RESTORE_LOG")
2626+
# Count occurrences of --model — should be exactly 1 (from cli_args, not doubled)
2627+
nodup_count=$(echo "$nodup_log" | grep -o '\-\-model' | wc -l | tr -d ' ')
2628+
assert_eq "Model field: no duplicate --model when already in cli_args" "1" "$nodup_count"
2629+
2630+
# Verify model is NOT added for non-Claude tools
2631+
tmux kill-session -t test-restore-model 2>/dev/null || true
2632+
tmux new-session -d -s test-restore-model -c /tmp 2>/dev/null || true
2633+
sleep 0.5
2634+
2635+
cat >"$HOME/.tmux/resurrect/assistant-sessions.json" <<'RMODELOC'
2636+
{
2637+
"timestamp": "2026-01-01T00:00:00Z",
2638+
"sessions": [
2639+
{
2640+
"pane": "test-restore-model:0.0",
2641+
"tool": "opencode",
2642+
"session_id": "ses_model_oc",
2643+
"cwd": "/tmp",
2644+
"pid": "99999",
2645+
"model": "some-model",
2646+
"cli_args": "",
2647+
"env": {}
2648+
}
2649+
]
2650+
}
2651+
RMODELOC
2652+
2653+
>"$RESTORE_LOG"
2654+
just restore 2>&1
2655+
sleep 5
2656+
2657+
oc_model_log=$(cat "$RESTORE_LOG")
2658+
if echo "$oc_model_log" | grep -q '\-\-model'; then
2659+
fail "Model field: --model should NOT be added for opencode"
2660+
else
2661+
pass "Model field: --model correctly skipped for opencode"
2662+
fi
2663+
2664+
kill_pane_children test-restore-model true
2665+
25602666
# --- Test 10e: Restore filters out tmux_pane and shell from env prefix ---
25612667
25622668
echo ""

0 commit comments

Comments
 (0)