feat(switcher): auto-resume dedicated sessions after a swap

When a legitimate quota hit triggered a swap, killAllPoolSessions tore
down the dedicated interactive sessions (ccl-1-conformvault, ccl-2-scanyze)
along with the pool, then recreatePoolSessions re-opened them at a bare
bash prompt. The operator had to manually re-run
  CLAUDE_CONFIG_DIR=<target> claude --dangerously-skip-permissions --resume <uuid>
after every swap, losing whatever conversation was mid-flight.

saveAllSessions only iterates sessions tracked as "working" in state;
user-driven dedicated sessions are rarely in that state so their resume
UUIDs were never saved.

- saveDedicatedUUIDs: capture resume UUID for every configured dedicated
  session regardless of tracked state, before kill.
- relaunchDedicatedSessions(targetHome): after recreate, send a resume
  command on each dedicated session pointing CLAUDE_CONFIG_DIR at the
  target account's home. Missing UUID → leave at shell, no blind launch.
- isValidResumeUUID hardens against a corrupted resume-id.txt.

New TestDedicatedRelaunchAfterSwap verifies end-to-end: pane capture →
UUID persisted → resume command sent with the correct CLAUDE_CONFIG_DIR.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ubuntu 2026-04-15 20:24:38 +00:00
parent 5cad53ac7a
commit 8fdb1937fc
4 changed files with 169 additions and 14 deletions

View file

@ -1,6 +1,7 @@
package switcher
import (
"strings"
"testing"
"time"
@ -11,10 +12,11 @@ import (
// mockTmux for switcher tests.
type mockTmux struct {
sessions map[string]bool
paneOutput map[string]string
killCalls []string
createCalls []string
sessions map[string]bool
paneOutput map[string]string
killCalls []string
createCalls []string
sendKeyCalls []string
}
func newMockTmux() *mockTmux {
@ -35,7 +37,10 @@ func (m *mockTmux) KillSession(name string) error {
m.killCalls = append(m.killCalls, name)
return nil
}
func (m *mockTmux) SendKeys(_, _ string) error { return nil }
func (m *mockTmux) SendKeys(session, keys string) error {
m.sendKeyCalls = append(m.sendKeyCalls, session+":"+keys)
return nil
}
func (m *mockTmux) CapturePaneTail(session string, _ int) (string, error) {
return m.paneOutput[session], nil
}
@ -164,3 +169,50 @@ func TestKillAndRecreatePoolSessions(t *testing.T) {
t.Errorf("expected autonomous sessions recreated; createCalls=%v", tc.createCalls)
}
}
// TestDedicatedRelaunchAfterSwap verifies that a dedicated session is
// automatically restarted with `claude --resume <uuid>` on the target
// account's home after a swap, so interactive user work is preserved.
func TestDedicatedRelaunchAfterSwap(t *testing.T) {
tc := newMockTmux()
tc.sessions["dedicated-1"] = true
// Pane shows the full resume command — saveDedicatedUUIDs will extract it.
tc.paneOutput["dedicated-1"] = "claude --resume aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --dangerously-skip-permissions"
s := state.New("")
s.SetActiveAccount("compte1")
cfg := &config.Config{
Accounts: []config.AccountConfig{
{Name: "compte1", Home: "/tmp/claude-1-xxxx"},
{Name: "compte2", Home: "/tmp/claude-2-xxxx"},
},
Pool: config.PoolConfig{
Dedicated: []config.DedicatedSession{{Name: "dedicated-1", Project: "/tmp"}},
Autonomous: config.AutonomousConfig{Prefix: "ccl-auto-", Min: 0, Max: 0},
},
}
a := New(tc, s, cfg, make(chan quota.SwitchRequest), nil)
a.homeDir = t.TempDir()
a.executeSwitch(quota.SwitchRequest{From: "compte1"})
// The relaunch must send a resume command on the dedicated session,
// pointing CLAUDE_CONFIG_DIR at the target account's home.
var relaunch string
for _, k := range tc.sendKeyCalls {
if strings.HasPrefix(k, "dedicated-1:") && strings.Contains(k, "--resume") {
relaunch = k
break
}
}
if relaunch == "" {
t.Fatalf("expected dedicated-1 relaunch send-keys; got %v", tc.sendKeyCalls)
}
if !strings.Contains(relaunch, "CLAUDE_CONFIG_DIR=/tmp/claude-2-xxxx") {
t.Errorf("relaunch should set CLAUDE_CONFIG_DIR to target home; got %q", relaunch)
}
if !strings.Contains(relaunch, "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee") {
t.Errorf("relaunch should include captured UUID; got %q", relaunch)
}
}