Handoff: support large sessions and fix menu input focus

Draft
PI
5mo ago
+12-1

What

Two fixes to the handoff_agent_here (⌘⇧G) flow:

  1. Budget the transferred context so very large source sessions hand off cleanly.
  2. Focus the spawned pane so the target-agent picker actually receives keystrokes.

Why

1. Large sessions

A long-running source session (e.g. Codex) can be hundreds of MB on disk while its live context is only a few MB — it compacts repeatedly as it runs. The handoff shells out to casr to convert the source session into the target agent's native format; replaying the entire on-disk archive blows the target's resume size / context-window limits and the handoff fails.

This passes --max-context-tokens and --max-tool-output to casr (which honors the source's compaction and trims to those caps), so the new agent ends up with roughly the same context the source agent actually had — not the full archive. Both are env-overridable (CASR_MAX_CONTEXT_TOKENS, CASR_MAX_TOOL_OUTPUT), and older casr builds that don't understand the flags simply exit non-zero and fall through to the existing prompt-handoff fallback.

2. Menu input focus

async_split_pane() creates the new pane but leaves keyboard focus on the original session. The handoff launcher prints a "Choose target agent" menu and blocks on read, so with focus still on the original pane the menu looked frozen — keystrokes went to the wrong pane. Calling async_activate() on the child after the split fixes it. (The ⌘⇧F fork path auto-runs its command and needs no input, which is why only the handoff menu was affected; activation is added to both for consistency.)

Testing locally

  1. Check out this branch and copy fork_agent_here.py into your iTerm AutoLaunch dir (~/.config/iterm2/AppSupport/Scripts/AutoLaunch/), then restart iTerm (or relaunch the script) so the daemon reloads.
  2. In a pane running a long Codex (or other supported) session, press ⌘⇧G.
  3. The new split pane should take focus immediately — type a number to pick the target agent and press Enter.
  4. It converts via casr and resumes, even for a multi-hundred-MB source session that previously failed the handoff.
  5. To tune the budget: export CASR_MAX_CONTEXT_TOKENS=120000 before launching.

The budgeting needs a casr build that understands --max-context-tokens / --max-tool-output (companion change); without it the handoff still works via the prompt-handoff fallback.


Summary by cubic

Make the ⌘⇧G handoff robust for large sessions and fix input focus so the target-agent menu accepts keystrokes. Context is trimmed during casr resume, and the new split pane is activated.

  • Bug Fixes
    • Trim handoff context via casr --max-context-tokens/--max-tool-output (env: CASR_MAX_CONTEXT_TOKENS, CASR_MAX_TOOL_OUTPUT); older casr falls back to prompt path.
    • Activate the new split pane so the target-agent picker receives input (applies to fork and handoff).