Skip to content

fix(smart-routing): type the first prompt switch as keys, launch unrouted without the socket - #1860

Draft
xsh310 wants to merge 1 commit into
omni-pinned-model-first-prompt-routingfrom
first-prompt-switch-in-pane
Draft

xsh310 wants to merge 1 commit into
omni-pinned-model-first-prompt-routingfrom
first-prompt-switch-in-pane

Conversation

@xsh310

@xsh310 xsh310 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

Two fixes for Claude's first prompt routing when another program types into Claude's terminal. Both showed up when the first prompt of a run was long, and when a launch can't route.

  1. A long held prompt broke the model switch. After the hook holds the first prompt, Claude keeps it in the input box. ug clears the box with two Ctrl-U presses per wrapped row and types /model <pick> and Enter, all in one write. Claude Code reads 64 or more bytes that arrive together as a paste, so it ignored the Ctrl-U presses and the Enter and kept /model <pick> as text. The switch then timed out, and ug pasted the prompt next to the held one. Claude ran the prompt twice, followed by the /model text, on the default model. A 3,900 character prompt in a 160 column terminal needs 103 presses.
  2. A launch that can't start the prompt routing socket exited. ug claude stopped without starting Claude. It now starts Claude without Smart Routing and says so.

First user: Omnigent automation runs ("first prompt in an Omnigent pane", item 7 of the Automations design). The runner launches Claude without --model so ug routes the run's first prompt, then pastes that prompt, which is often thousands of characters. An unattended run that exits never runs.

Stacked on #1887, on top of #1848, which adds the preset these runs use (stack: #1848, #1887, #1860).

Contract

Keys ug types into Claude's input box:

clear    Ctrl-U x (2 * wrapped rows + 1)   written 16 bytes at a time, 20 ms apart
switch   "/model <pick>\r"                  same
replay   ESC[200~ <prompt> ESC[201~         one write (bracketed paste), Enter 0.3 s later

Setup failure:

run_claude_pty   raises ClaudeRoutingSetupError("Smart routing could not start its local prompt-routing socket")
ug claude        prints "Smart routing could not start its local prompt-routing socket;
                         starting Claude Code without Smart Routing."
                 then starts Claude with ENABLE_SMART_ROUTING_V2=0 and ENABLE_SMART_ROUTING_SUBAGENT_ONLY=0

The fallback launch is the existing one for routing files ug can't write, which now prints the same warning.

What happens now, in order

  1. The first prompt hook holds the prompt and asks the router for a model, as before. A long pasted prompt shows in the box as [Pasted text #1 +N lines].
  2. Once Claude's output is quiet, ug types the clear presses in 16 byte writes, 20 ms apart. It keeps forwarding Claude's output between writes, so Claude's redraws can't stall it and merge the writes back into one paste.
  3. ug arms the confirmation dialog and the "Set model to" check, then types /model <pick> and Enter the same way. The switch timeout starts after the command is typed.
  4. After the switch, or after any of the existing timeouts, ug types the clear again and pastes the prompt as one bracketed paste. Enter follows PASTE_SUBMIT_DELAY_S after the paste, not after the start of the step.
  5. If the socket never started, no Claude process is started inside the wrapper. ug claude catches ClaudeRoutingSetupError, prints the warning and starts Claude normally with routing off.

Tests

  • tests/test_claude_pty_replay.py: a new fake TUI reads a burst of 64 or more bytes as a paste, like Claude Code 2.1.325. With a 2,407 character held prompt, Claude must receive exactly /model sonnet and then the prompt. On main it receives the mangled prompt instead.
  • tests/test_claude_smart_routing_v2.py: the socket failure raises ClaudeRoutingSetupError, and a setup failure starts Claude once, with both routing flags 0 and the warning.
  • The six Claude launch and PTY test files: 748 passed. Full suite: 9312 passed. The 21 failures in test_catalog_browsing.py, test_desktop_mdm.py and test_integration_contract.py fail the same way without this change, and the 8 collection errors come from my local venv missing OpenTelemetry.
  • ruff check src/ tests/ and ruff format --check src/ tests/ pass. ty check src/ reports only the 8 OpenTelemetry import errors of my local venv, the same as on main.
  • Measured on Claude Code 2.1.325 in tmux: 63 Ctrl-U bytes in one write cleared a pasted prompt, 64 did not. Split into 16 byte writes, 300 presses cleared it.
  • Live run with stubbed router and model catalog: ug claude with first_prompt_and_subagent_orch_v0_claude_only in a tmux pane, and a 3,900 character first prompt delivered by Omnigent's own runner code (bridge.inject_user_message).
    • Before: the switch timed out, and Claude ran the prompt twice plus the /model text on the default model.
    • After: /model switched the model, the prompt ran once, and it carried the orchestrator workflow.
    • Both runs: the runner saw the box empty 0.5 s after its one Enter, before the hold landed at about 3.5 s, so it reported the prompt delivered and sent no extra Enter. The runner needs no change.
  • Not run here: the live integration suite (/integration-test).

Checklist before requesting review

  • Comment /ug-review and address the comments.
  • Comment /integration-test and address any failures.

This pull request and its description were written by Isaac.

@xsh310
xsh310 added this pull request to stack #1861 November 23, 2026 18:34
@xsh310
xsh310 force-pushed the first-prompt-switch-in-pane branch 2 times, most recently from 027e324 to 6a62e3c Compare November 24, 2026 17:04
…uted without the socket

Claude Code reads 64 or more bytes that arrive together as a paste. ug
cleared a held first prompt and typed /model in one write, so a long
prompt's clear was ignored, the switch timed out, and the replay ran next
to the held prompt: twice, plus the /model text, on the default model. ug
now types the clear and the model command in 16 byte writes 20 ms apart,
forwarding Claude's output in between.

When the prompt routing socket can't start, ug now launches Claude without
Smart Routing and warns, through the existing routing setup fallback,
instead of exiting.

Co-authored-by: Isaac <no-reply@databricks.com>
@xsh310
xsh310 force-pushed the first-prompt-switch-in-pane branch from 6a62e3c to 5a8fb8f Compare November 25, 2026 18:19
@xsh310
xsh310 removed this pull request from stack #1861 November 25, 2026 18:22
@xsh310
xsh310 changed the base branch from automation-first-prompt-orch-preset to omni-pinned-model-first-prompt-routing November 25, 2026 18:22
@xsh310
xsh310 added this pull request to stack #1909 November 25, 2026 18:22
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.

1 participant