Repository navigation
fix(layout-owner): window tabs that launch an agent or carry a draft start through main - #51934
Conversation
…start through main
…er-launch-drafts-through-main
…er-launch-drafts-through-main
…tatus on every attach, typed launch refusals, safe draft paste
…er-launch-drafts-through-main
[Medium impact] The change reaches main’s local terminal starts and the window and phone views that attach to them. The PR should wait until commands that exit before attachment show their saved output instead of a blank pane. Findings
SummaryMoves eligible window-tab starts into main and gives their views an attach-only path.
Reviews (20) · Last reviewed commit: "fix(layout-owner): a frame binds a layou..." · Reviewed by Greptile |
| for (const chunk of iterateAgentDraftPasteContentChunks(draft.content)) { | ||
| host.readiness.write(target.ptyId, chunk, 'launch') | ||
| } |
There was a problem hiding this comment.
Failed draft writes stay silent
The new loop ignores whether each chunk was accepted. writePtyFromRuntimeController returns false when the provider is unavailable or refuses a write, rather than throwing. The loop therefore finishes without calling onNotPasted, so the user can lose their draft without a notice. Preserve the write result, stop on refusal, and notify the window.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
… to main's terminal
…er-launch-drafts-through-main
… outcome, none for a closed pane Main's create asks the launch what a failed pane shows before either failure writer publishes, so no raw start failure reaches the window first. Exited-during-start shows the launch refusal, as the pane's own spawn does. Main-started view facts are dropped on a failed create and when the tab closes. The draft cap counts sanitised UTF-8 bytes.
…er-launch-drafts-through-main
| if (chunk && chunkBytes + characterBytes > safeMaxChunkBytes) { | ||
| yield chunk | ||
| chunk = sanitized | ||
| chunkBytes = characterBytes | ||
| continue | ||
| } |
There was a problem hiding this comment.
Main now chunks every startup draft. When an emoji crosses the 16 KiB boundary, this branch adds the whole character, then takes continue before skipping its second string unit. The next pass adds that unit again, corrupting the text sent to the agent. For example, 16,383 ASCII characters followed by an emoji triggers this. These small drafts previously used the window's direct paste without chunking.
Advance past both string units before continuing.
| if (chunk && chunkBytes + characterBytes > safeMaxChunkBytes) { | |
| yield chunk | |
| chunk = sanitized | |
| chunkBytes = characterBytes | |
| continue | |
| } | |
| if (chunk && chunkBytes + characterBytes > safeMaxChunkBytes) { | |
| yield chunk | |
| chunk = sanitized | |
| chunkBytes = characterBytes | |
| if (codeUnitLength === 2) { | |
| index += 1 | |
| } | |
| continue | |
| } |
…r per pane in main The window asks main once who starts a new tab's first terminal (asking, then main or window) before the pane connects. A pane main starts sends pty:attachPane, built from an allow-list, which awaits main's in-flight start and answers a typed state instead of an error, so it never starts a terminal or feeds remount recovery. Main runs the window tab's start under the same per-pane gate as every other start and marks it consumed, so a later startPane never re-runs it from tab facts. Main hands the launch's view facts back with the attach. The window's starting mark, startup hand-off and give-back remount, and the main-started view map are gone.
…owner A lane-wide oracle fails any switch-on spec whose window starts a terminal in a pane main started. The window-tab spec adds a command that exits before its view, remounts during main's start, and a slow decline remounted while the window is still asking. Unit tests pin the per-pane starter, the consumed startPane rule, the one-way owner latch and the attach request's allow-list.
| return page.evaluate( | ||
| ({ id, opts, show }) => { | ||
| const state = window.__store!.getState() | ||
| const tab = state.createTab(id, undefined, undefined, opts) |
There was a problem hiding this comment.
openTab(..., { background: true }) still activates the tab because createTab activates it by default. Skipping the later setActiveTab call does not keep the pane hidden. The exit-before-view test can therefore attach before the agent exits and pass without covering the race it claims to test. Pass activate: false to createTab for background tabs.
…er-launch-drafts-through-main # Conflicts: # .github/workflows/layout-owner-switch-on.yml
…tabs crash spec Main now starts default tabs that carry startup fields, through the window's terminal.create, so stalling only pty:spawn no longer keeps every terminal from starting before the crash. The exited-before-view case no longer assumes the tab closes: it stayed open on Linux and closed on macOS.
…er-launch-drafts-through-main
…emon restart and reload - Restart (account switch, died-pane Restart) of a main-started pane goes through main's restartPane: main stops the terminal and starts the pane again through the same starter. - A daemon restart forgets the consumed start, so main's startPane runs again. - Main publishes 'starting' when it takes a start, and the launch status per terminal, so a reloaded window waits on main and re-seeds the agent status instead of starting a shell. - A refused create only closes the tab. - Attach replies: closed/owner-changed races map to states, an exited registration is 'exited', 'held' shows the newer-daemon card; an already-present binding is attached at once. - An exited-before-view tab stays open: main notes the spawn commit before the binding save, and the window treats main's first start as its fresh spawn.
…er-launch-drafts-through-main
…rted window tabs
…umed start stays consumed - One-call tab create (design step 6): the window's terminal.create carries its createTerminalTab; main writes the tab as the first step of the pane's start, under the start gate. No window or phone finds the tab recorded but unstarted, so none starts it without its --model or startup. A refusal or failed start answers with the call's frame; host_not_owned still records the tab alone and the window starts it, as before. - PaneStarter.ensureStarted refuses a consumed pane instead of clearing it: a phone opening an exited main-started tab no longer runs its command again. A stop (forget) or a layout command's start (byLayoutCommand: startPane with a resume record, wake, restartPane) still starts it. - A died pane's Restart reaches main's restartPane: the connect is marked as a restart, since the exited transport no longer names its terminal. - Tests from #52167 merged; refusal stubs follow the one call, my spec imports the shared harness.
…al ends on its own - Replace the pane-level 'consumed' flag with a fact about the start's terminal: a window-tab start is spent once the terminal it ran ends on its own (main's existing ended-on-its-own exit verdict), no later terminal holds the pane, and the pane keeps no resume record. Sleep, Restart, a daemon replacing the terminal and a held pane are not that, so wake, mailbox/CLI wake, a phone opening a slept tab and held Reopen start again with no forget call; the daemon-restart forget is gone. A failed start stays spent; a refused tab records nothing. - createTab publishes the first terminal's cwd with the tab, and the terminal bridge passes its startup command into createTab, so the one-call create carries both (nothing is queued after). - host_not_owned from the one call answers as layout.apply would (a twin's start goes to the window, a local refusal closes the tab), with no second layout.apply. - A held pane's Reopen asks main to start it, then attaches. - The switch-on window lane runs the two new window-tab specs.
…ep, reopen held, survive restarts and keep their inputs - Slept window agent and draft tabs wake once, through main, from the sidebar and from a phone. - A held window tab's Reopen after a newer terminal service gets a live terminal from main. - A daemon restart after the tab was viewed, opened on a phone and reloaded gives it a live terminal. - A repeated one-call create runs nothing new and leaves the pane startable. - ui:createTerminal's startup command and a subfolder project's first terminal run in the right cwd. - The spawn oracle no longer counts a refused or reattached one-call create as a main start.
…ll settling wakes its slept tabs Main finishes a sleep before the window commits it. Opening the workspace or one of its tabs in that gap saw terminals that were still listed, so nothing woke the slept tabs: - the window remounts the tabs when the sleep ends on a workspace that was already reopened - attach answers `sleeping` for a pane main keeps a resume record for, so the view asks main to wake it - a phone open waits for a sleep that is still running, then trusts main's resume record over the window's stale `ready` Also adds an e2e for an Agent Sleep woken by a terminal send to its handle.
… starting directory
…er-launch-drafts-through-main # Conflicts: # src/renderer/src/runtime/workspace-layout/commands/layout-apply-send.ts
| case 'exited': | ||
| // Main's start ran and its terminal is gone; nothing runs it again but a user act. | ||
| break |
There was a problem hiding this comment.
Short commands lose their output
When a command exits before its pane connects, main answers exited, and this branch does nothing. connectMainStartedPane returns before handleReattachResult, so the pane never receives the command's saved output. No later attach is scheduled. A short command can therefore leave an empty tab instead of showing its result or error.
Restore the ended terminal's output without running its startup again.
…er-launch-drafts-through-main # Conflicts: # .github/workflows/layout-owner-switch-on.yml
…er-launch-drafts-through-main # Conflicts: # src/renderer/src/runtime/workspace-layout/commands/layout-apply-send.ts # src/renderer/src/store/terminals/tabs/creation/terminal-tab-create-through-layout-owner.ts
…er-launch-drafts-through-main # Conflicts: # tests/layout-owner-e2e/helpers/orca-restart.ts
… cleared, as the full build does
ELI5
On the layout-owner path, main is supposed to start every terminal. A new tab you open in a window that launches an agent, or that carries a draft prompt for the agent, still started its own terminal from the window. Now main starts those too, the same way it already starts plain new tabs.
Before / after (layout-owner path only)
The switch-off path is unchanged.
Mechanism
pty:spawnintosrc/main/agent-launch/pane-agent-launch-lifecycle.ts.pty:spawnand main's window-tab start (terminal.createwithwindowTab) both use it, so there is one implementation.terminal.create(window-tab branch only, local desktop caller only) gains optionaltelemetry,agentLaunch, the OpenCode model start fields anddraftPaste(window-tab-start-params.ts). A bad value is dropped, never refused.window-tab-start-inputs.tsturns them into main's create options.ui:startupDraftNotPasted) and the window shows the existing "wasn't sent" notice.terminal.create {windowTab}that carries the window'screateTerminalTab. Main writes the tab as the first step of the pane's start, inside the per-pane start gate, so another window or a phone never finds the tab recorded but unstarted (they join the start instead of starting it without its--modelor startup). The window keeps its drawn tab held until the reply's frame, as forlayout.apply. A refusal ({refused}) closes the tab; a failed start ({startFailure}) shows on the pane;host_not_ownedanswers aslayout.applywould (a twin's start goes to the window, a local refusal closes the tab). The tab's startup and first cwd are published with the tab increateTab, so the one call carries them; nothing is queued after it.askingbefore the pane mounts. Main's answer settles it once:main(main took the start, or may have run it) orwindow(host_not_owned: main ran nothing). The pane's connect waits whileasking. The startup stays in the window's store until main takes it, so onwindowthe waiting pane runs its own start with the startup it captured, including its draft paste, with no remount (pane-start-owner.ts).pty:attachPane): a pane main starts sends one new IPC call built only from{worktreeId, tabId, leafId, cols, rows, initiallyHidden}; main refuses one naming anything else. Main waits for the pane's in-flight start, then attaches the view to its terminal, or answers a typed state:sleeping,held,failed,exited,unstarted,unknown. None of these is an error, so none shows a toast or triggers remount recovery. The window's own start code is never reached for such a pane (fresh-spawn-start.tsrefuses it as a backstop).PaneStarter.startWith), registered before its first await. A window tab's start is spent once the terminal it ran ends on its own (main's existing exit verdict: no close, kill, sleep, Restart or daemon replacement), while no later terminal holds the pane and the pane keeps no resume record; a failed start is spent too. A spent start is never run again by a view, a phone or a wake; Restart (a layout command that ruled on it) or a resume record starts it. Because the fact belongs to the terminal, not the pane, sleeping, restarting or replacing the terminal makes it lapse on its own; no lifecycle path has to clear a flag.'starting'on the pane the moment its starter takes a start (before any await), and clears it when the start ends. It also publishes the launch's initial agent status on the pane, tied to the terminal id like the resume outcome. A reloaded window has no owner latch, so it learns both from main's published layout: it waits on'starting'instead of starting a shell, and re-seeds the agent status on attach.restartPane. Main stops the old terminal and starts the pane again through the same per-pane starter as a new start; the pane then attaches. A daemon restart forgets the pane's consumed start too, so main'sstartPanestarts it again.runtime-window-lifecycle.tswas at the 300-line cap, so the agent-launch notifier entries moved next to their relay (agentLaunchWindowNotices).How it was tested
tests/layout-owner-e2e/window-tab-agent-starts-in-main.spec.ts(added to the window shard of the lane). After the first terminal is up it records every terminal the window's ownpty:spawnstarts (calls that only attach to a live terminal don't count), then opens a Codex tab from the tab bar's + menu and a launch-draft tab with the startup a workspace's first agent terminal carries. A fake agent logs each start with its pane and size, every resize and all input. It checks:--repeat-each=2). Without the fix it fails: the window starts both terminals itself.ui:startupDraftNotPasted), and ahost_not_ownedgive-back (the window starts that tab itself, once). The status assertion fails if the reattach-path seed is removed.host_not_ownedremounted while the window is still asking (one window start, one paste, no remount of its own).ORCA_DAEMON_MAX_HOST_TERMINALSraised when the machine is near the shared 256-terminal cap; CI is not.pty:spawnthat is not an attach) in a pane main accepted a window-tab start for, in either order (helpers/main-started-pane-spawn-oracle.ts, checked at app close). With the latch and attach disabled it fails the first spec.attachPaneView(attach only,exitedwhen the terminal died while the view registered, every miss state, close and owner-change races as states, refusal of a request naming a start input); the per-pane starter ('starting'published before the start settles,startWithjoins,settled, consumed,forgetclears consumed), the consumedstartPanerule (fails if the rule is removed), the one-way latch, the attach allow-list, andstartWindowTabWithInputsverdicts (not-started vs unconfirmed around the provider spawn, plan and create failures, exit during start, closed pane).pnpm tc:node,pnpm tc:web, oxlint and oxfmt clean on changed files.Census rows
No census row is directly fixed. This is plan item (b) (L299, L252). L308 belongs to #51731, L314 is fixed by #51734, and L312/L313 need the SSH Docker and paired lanes; this PR doesn't claim them.
Visible changes
None intended. On the layout-owner path only:
Review fixes
Round 2, start seam (replaces round 1's fences): the window's
startingmark, the startup hand-off and give-back remount,main-started-pane-view.ts,windowSpawnFencedByMainStart,mainPaneStartHandedBackand the attach-onlypty:spawnfor these panes are gone; the owner latch,pty:attachPaneand the per-pane starter above replace them. This fixes the attach-only connect looping onterminal_pane_not_live, the give-back's spurious connect, and the OpenCode branch being reachable.Round 2, typed outcomes: main's create asks the launch what a failed pane shows before either failure writer publishes, so every failure path publishes exactly one typed outcome (
agent_session_exited_during_startincluded) and a pane the user closed publishes none.Round 2: main-started view facts are dropped when a create fails or the tab closes; main's draft cap counts sanitised UTF-8 bytes.
Typed launch refusals: a refused agent launch now leaves the launch's typed refusal on the pane, so the window shows its launch notice, not a raw "could not start" toast. An agent that exits during start keeps its start-failure detail.
Dispatch point: the launch counts as dispatched only right before the provider spawn (a new create hook), as
pty:spawndoes. A failure before that point now reports "not started".Draft paste: main's paste now uses the window's draft-paste framing (ESC neutralised, CR line endings, 16 KiB chunks, 16 MiB cap). The chunker moved unchanged to
shared/terminal/input/agent-draft-paste-chunks.ts.terminal-paste: the guard is restored; such startups keep the window's spawn.Round 5b, waking during a sleep (on main too): main finishes a sleep before the window commits it, so opening the workspace or one of its tabs in that gap saw terminals that were still listed and woke nothing. Now:
sleepingfor a pane main keeps a resume record for;ready.The same gap exists on
main(the window's late commit and the phone'sreadygate are unchanged there). A new e2e covers an Agent Sleep woken by a terminal send to its handle.Round 5: the start's spent fact moved from a pane flag to the terminal it ran (fixes wake, mailbox/CLI wake, phone open of slept tabs and held Reopen); the terminal bridge and the initial-terminal seeding pass their startup and cwd into
createTab;host_not_ownedanswers in one call; a held pane's Reopen asks main to start it; the window lane runs the two new specs.Round 4: the create/start gap is removed at the root (one call, design step 6) instead of patched;
ensureStartedrefuses a consumed pane; a died pane's Restart reachesrestartPane.Round 3: in-place restart and daemon restart go through main's starter (above);
'starting'is published at receipt, so a reload during agent-launch planning no longer spawns a shell; a refused create only closes the tab (it used to start the window's spawn first); an attach whose binding is already present attaches at once;'held'shows the newer-daemon Reopen card;attachPaneViewanswers close and owner-change races and an exited registration as states; main notes the spawn commit before the binding save, and the window counts main's first start as its fresh spawn, so the exited tab stays open; the launch status is published per terminal instead of a window-memory map that was never cleared.Still started by the window
resumeSource/showSessionRestoredBanner), until main publishes its restored outcome (L251).launchDraft view field
This PR does not give main a draft record for the native chat composer (
nativeChatLaunchDraftByTabId), so the temporarylaunchDraftview field must stay for now. Main only pastes the terminal startup draft.Performance
None, because the change only moves where an existing start runs: one
terminal.createcall (which now also writes the tab, replacing the separatelayout.apply) plus onepty:attachPanereplace the pane'spty:spawnfor these tabs, with the same planning and paste code. No new polling, timers or per-frame work; the owner latch is capped at 256 entries, and main's published launch status is dropped with its terminal.