Skip to content

fix(agents): resume the claude session a tab moved to with /clear (#306) - #307

Merged
simion merged 1 commit into
simion:mainfrom
GabrielDumbrava:fix/claude-session-after-clear
Sep 16, 2026
Merged

simion merged 1 commit into
simion:mainfrom
GabrielDumbrava:fix/claude-session-after-clear

Conversation

@GabrielDumbrava

Copy link
Copy Markdown
Contributor

Closes #306

Problem

termic stores one session id per claude tab and resumes it on relaunch.
/clear starts a new session inside the running claude, and /resume switches to another, so everything after either landed under an id the tab never stored.
The next relaunch resumed the session from before the /clear, and the agent said it had never worked on what the tab was doing.

Fix

  • Hook (agent_hooks.rs). claude's READY script (its SessionStart) now also reports the session id over the session <id> OSC codex and devin already use, but only when the session moved:
    • source is clear, resume or compact. Never startup: termic passed that id itself.
    • CLAUDE_CODE_ENTRYPOINT is cli. A nested claude -p inherits TERMIC_PTY but runs as sdk-cli.
    • The id must be a uuid, since it ends up in a --resume <id> command line.
    • SCHEMA_VERSION bumps to 10 so existing installs get the script on the next sync.
  • Frontend (TerminalPane.tsx). A reported id supersedes the id the spawn minted. That one is only held after the spawn survives RESUME_FAILURE_MS and only persisted on the first prompt, so a /clear before either was undone by the next Enter (sessionReportedRef).
  • The id lands on the tab whose pty it arrived on, keyed by tab id, so several agent tabs in one task, in any order, keep their own.

Measurements behind the guard (claude 2.1.273, SessionStart hook logging stdin and env) are in #306 and now in docs/agent-hooks.md.

Tests

  • cargo test: 1032 passed. New: the id is reported for clear / resume / compact with a cli entrypoint, and not for startup, sdk-cli, a missing entrypoint, an unknown source, or a non-uuid id. Control: without claude in the emit branch the reporting test fails.
  • npm test: 2180 passed.
  • agent.e2e.ts, new describe (3 cases): three fakeclaude tabs in one task, reordered first, each storing the id replayed over the hook OSC on its own tab only; a relaunch spawning --resume <id> for all three (asserted on the spawned argv); a /clear inside the first two seconds, before the first prompt, surviving it. Control: that last case fails without sessionReportedRef.
  • The full agent.e2e.ts run has 5 failures that are identical on a clean main build in two full runs (IIP redraw, pending-work done badge, two-agent footer chips, and both fakecapture resume cases), so they are pre-existing and unrelated.

Manual verification

Exercised by hand in a tauri dev build with real claude 2.1.273:

  • one tab on a cloned claude agent: a message, /clear, another message, Termic closed and reopened; the tab spawned --resume <post-clear id> and the agent had the post-clear conversation;
  • two agent tabs in one task with /clear in the second: after a restart each resumed its own session.

🤖 Generated with Claude Code

…mion#306)

termic stores one session id per tab and resumes it on relaunch. `/clear`
starts a new session inside the running claude, and `/resume` switches
to another, so everything after either landed under an id the tab never
stored. The next relaunch resumed the session from before the `/clear`,
and the agent said it had never worked on what the tab was doing.

claude's READY hook (SessionStart) now also reports the session id over
the `session <id>` OSC codex and devin already use, but only when the
session moved. Measured on claude 2.1.273: `source` is `clear` with a
new id on every /clear, `resume` with the resumed id, `compact` with the
same id, and `startup` at launch, which is skipped because termic passed
that id itself. The report also requires CLAUDE_CODE_ENTRYPOINT=cli: a
nested `claude -p` inherits TERMIC_PTY but runs as `sdk-cli`.

On the frontend a reported id supersedes the id the spawn minted. That
one is only held after the spawn survives RESUME_FAILURE_MS and only
persisted on the first prompt, so a /clear before either was undone by
the next Enter. Control: the e2e case for it fails without the guard.

The id lands on the tab whose pty it arrived on, keyed by tab id, so
several agent tabs in one task, in any order, keep their own. SCHEMA
bumps to 10 so existing installs pick the script up.

Closes simion#306

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

@simion simion left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Reviewed with Opus. Clean fix: extends the existing OSC session <id> mechanism (already used by codex/devin) to also cover claude's SessionStart hook for clear/resume/compact sources, gated correctly (never fires on startup, checks entrypoint == cli, validates uuid). The timing race on the frontend side (spawn vs. first-prompt persistence) is handled for both orderings, and per-tab id routing is correct for multiple agent tabs in one task. Solid test coverage (Rust + e2e) plus manual verification. Thanks for the second contribution!

@simion
simion merged commit 1417803 into simion:main Sep 16, 2026
3 checks passed
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.

claude tab resumes the session from before /clear after a relaunch

2 participants