Skip to content

Feature: Per-terminal session affinity for --continue #19541

@kitaekatt

Description

@kitaekatt

Note: This is a resubmission of #18624, which was automatically closed as a duplicate of #15047. These are different issues:

Issue Type Problem
#15047 Bug Ralph-wiggum stop hook triggers in a different session (cross-session hook interference)
#18624 Feature --continue resumes wrong session (should prefer terminal-local history)

#15047 is about hooks leaking across sessions. This issue is about improving --continue UX with per-terminal affinity. Related scenario (multi-terminal), but different problems requiring different solutions.


Problem

When using --continue, Claude Code resumes the most recently active session globally. This causes friction for users with multiple long-running terminal sessions:

  1. User has a long-running Claude session in Terminal A
  2. Other Claude sessions occur in Terminals B, C, D
  3. User needs to restart Terminal A's session (e.g., to pick up config changes)
  4. --continue resumes the wrong session (most recent globally, not Terminal A's session)

The /resume list becomes unwieldy with many sessions, and while users can specify a session ID explicitly, this is high-friction and users often don't discover the session is "stale" until after attempting to continue.

Proposed Solution

Add per-terminal session affinity for --continue. The goal: when a user types --continue in a terminal, it should resume the session that was last active in that specific terminal, not globally.

Example Implementation Approach

One possible approach: persist session-to-terminal mappings using a "best available terminal identifier" pattern. Claude Code may have other internal mechanisms that could achieve the same goal.

On session exit: Store a mapping from terminal identifier → session ID

On --continue (without explicit session): Check for a stored mapping for the current terminal identifier before falling back to global recency

Terminal identifier priority list (use best available):

Priority Identifier Source Notes
1 $ITERM_SESSION_ID iTerm2 Unique per tab, survives splits
2 $KITTY_WINDOW_ID Kitty Unique per window
3 $WT_SESSION Windows Terminal Session GUID
4 $TERM_SESSION_ID macOS Terminal.app Per-window
5 $TMUX + $TMUX_PANE tmux Survives detach/reattach
6 $STY + window GNU Screen Screen session identifier
7 tty output Universal fallback Recycled on terminal close

Storage format (example):

{
  "identifier_type": "KITTY_WINDOW_ID",
  "identifier_value": "12345",
  "session_id": "abc-def-123",
  "timestamp": "2025-01-16T..."
}

Edge case - TTY recycling: For the TTY fallback (which gets recycled when terminals close/reopen), add a staleness check (e.g., ignore mappings older than 24 hours).

User Experience

Before: User must remember session IDs or scroll through /resume to find the right session

After: --continue "just works" - it continues whatever session was last active in this terminal, regardless of activity in other terminals

Notes

  • The above is one possible implementation approach - Claude Code may have other internal mechanisms that could achieve the same goal more elegantly
  • The identifier list is extensible as terminal emulators add new session identifiers
  • Falls back gracefully: terminals without recognized identifiers behave as today (global recency)
  • No breaking changes to existing behavior

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions