Repository navigation
fix(cmux): create task and secondmate workspaces focused at birth - #24
Merged
Merged
Conversation
Apply the same focus-at-birth and focus-restore pattern to task workspace mode as tab mode and secondmate creation. Workspace-mode create now uses --focus true, restores prior focus, and closes the new workspace if restoration fails. Keep --focus false unchanged for non-agent placeholders only: shared container workspace creation, last-workspace sibling creation in safe-close, and last-surface sibling creation before tab close. Update cmux backend docs and cmux tests to cover the broader rule and new workspace-mode cleanup behavior.
…docs, revert secondmate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
Fix the cmux backend defect where a surface created unfocused never resolves terminal size and full-screen TUI agents can render black forever. Keep the already verified secondmate patch logic unchanged as its own commit, then apply the same focus-at-birth plus full focus restore and cleanup-on-restore-failure pattern to fm_backend_cmux_create_task workspace mode. Preserve non-agent placeholder --focus false sites only where they do not host agents (shared container creation and throwaway siblings for safe close), update the CORRECTION note and cmux backend docs to reflect the rule across task tab mode, task workspace mode, and secondmate creation, and update colocated cmux tests accordingly. Workspace mode is reasoned by parity with the verified tab and secondmate paths and is not device-verified in this home.
What Changed
fm_backend_cmux_create_secondmateandfm_backend_cmux_create_taskworkspace mode now create withnew-workspace --focus trueand immediately restore the captured prior focus context, so a surface that later hosts a full-screen TUI agent resolves its terminal size instead of staying black;--focus falseremains only at shared-container creation and throwaway safe-close siblings.fm_backend_cmux_abandon_new_workspaceso workspace-mode failures unwind transactionally: an unresolvable workspace id restores focus, an unresolvable default surface closes the new workspace and restores, and a failed restore closes the new workspace; tab mode's unparseable-acknowledgment and unresolvable-UUID bailouts now also restore focus best-effort, andfm_backend_cmux_restore_focusmessages were reworded to be endpoint-neutral across all three callers.docs/cmux-backend.mdto state the focus-at-birth rule across task tab mode, task workspace mode, and secondmate creation, scoping the restore claim to focus within cmux and recording workspace mode as parity-reasoned but not device-verified; added three fake-CLI tests for the workspace-mode failure paths plus--focus true/restore assertions for the secondmate path.The pipeline's Test stage passed both
tests/fm-backend-cmux.test.shand the real-cmuxtests/fm-backend-cmux-smoke.test.sh, including a live workspace-mode focus-at-birth and focus-restore verification.Risk Assessment
✅ Low: The change is tightly scoped to the cmux backend's creation paths, satisfies every stated intent constraint (secondmate logic verified byte-identical to its own commit 38e9cda, non-agent
--focus falseplaceholders preserved, docs and colocated tests updated, transactional unwind added for workspace mode), the prior rounds' blocking issues are resolved, and the only remaining findings are informational.Testing
I exercised the change against the real cmux app, not just fakes: a control workspace created the old way (
--focus false) reported an unrealized terminal size of 35x99, while the patchedfm_backend_cmux_create_task workspacepath reported the real 60x177 geometry, directly demonstrating the black-TUI defect being fixed; the captain's focused context (window:1 workspace:2 pane:2 surface:47) was identical before and after the spawn, and the captured real-CLI trace showsnew-workspace --focus truefollowed by the full focus-window/select-workspace/focus-pane/reorder-surface restore. The colocated fake-CLI suite and the real-cmux smoke suite both pass, the secondmate logic is byte-identical to its own verified commit, and the two live test workspaces were cleaned up by the repo's safety guards. No screenshot was captured on purpose: the only reviewer-visible surface here is the captain's own live cmux window on a shared machine, and any window capture would expose unrelated real operational content — the same hazard already recorded in docs/cmux-backend.md — so the app's ownread-screenoutput and terminal-size reports serve as the end-user-surface artifact instead.Evidence: Live real-cmux workspace-mode run: focus-at-birth, focus restore, and terminal-size resolution
Evidence: Real cmux CLI calls issued by one workspace-mode spawn (PATH-shim trace)
cmux workspace list --json --id-format uuids cmux identify --no-caller cmux new-workspace --name fm-firstmate-66d11b4d-test-focus-birth --cwd /tmp --focus true --id-format uuids cmux workspace list --json --id-format uuids cmux list-panes --workspace 7BE15956-... --json --id-format uuids cmux focus-window --window window:1 cmux select-workspace --workspace workspace:2 --window window:1 cmux focus-pane --pane pane:2 --workspace workspace:2 --window window:1 cmux list-pane-surfaces --workspace workspace:2 --json --id-format both cmux reorder-surface --surface surface:47 --workspace workspace:2 --window window:1 --index 1 --focus trueEvidence: Terminal size the new surface actually resolves (the black-TUI defect), read back from the real app
Evidence: Harness used for the live verification (safety-guarded, fm-test-* labels only)
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/backends/cmux.sh:639- Workspace mode now creates with--focus true, but the two post-create resolution failures (line 639 wsid unresolvable, line 641 surface unresolvable) return 1 beforefm_backend_cmux_restore_focusever runs. Previously (--focus false) these paths were focus-neutral; now cmux is left focused on the freshly created task workspace and the captain's view is never restored. The line-641 case is worse:wsidis already known there, yet the orphan workspace is not closed either, so the failure leaves both a stolen focus and an orphan workspace - which contradicts the transactional contract the same function documents for tab mode ("any pre-return failure ... can close ONLY that new surface"). Fix: best-effortfm_backend_cmux_restore_focus "$prior_context"before both returns, plusfm_backend_cmux_close_workspace_safely "$wsid"on the line-641 path.fm_backend_cmux_create_secondmate(lines 1337/1339) has the identical shape; per the stated intent that commit stays as-is, so treat it as a parity follow-up.docs/cmux-backend.md:60- This line still claims "workspace/surface/pane creation all defaultfocustofalse, so an unattended spawn never steals your view." After this change the default container mode (workspace) creates with--focus trueand restores, so every ordinary spawn now produces a focus flicker. The intent requires the cmux backend docs to reflect the focus-at-birth rule across task workspace mode, tab mode, and secondmate creation; this sentence directly contradicts it and is the line an operator reads when deciding whether spawning is view-safe. Reword to note the brief flicker plus immediate restore (consistent with the new line 179-180 and line 194 text).bin/backends/cmux.sh:633- Capture-create-restore is not serialized across processes, and this change extends the exposure from opt-in tab mode to the DEFAULT workspace mode. If two spawns in the same cmux app interleave, spawn B'sfm_backend_cmux_focus_contextcan capture spawn A's freshly focused task workspace/surface as "prior context": B's restore then parks the captain on a task workspace, and if A subsequently rolls back viafm_backend_cmux_close_workspace_safely, B's restore fails on a vanished surface and rolls back B's own healthy workspace (spurious spawn failure). Same accepted tradeoff as the device-verified tab path, so noting only - not a blocker for this change.bin/backends/cmux.sh:642-fm_backend_cmux_restore_focusemits tab-specific text ("created cmux task tab but ..."), which is now surfaced by three callers including two workspace-creation paths. Operators debugging a failed workspace-mode or secondmate spawn will see a message naming a tab that does not exist. Neutral wording ("created the cmux endpoint but ...") would keep the message accurate for all three callers.🔧 Fix: cmux: restore focus and clean up on workspace create failures
6 issues (1 warning, 5 infos) still open:
docs/cmux-backend.md:420- This paragraph is a historical record of the end-to-end live pass (lines 400-416), which ran under the OLDnew-workspace --focus falseworkspace-mode behavior. The rewrite now attributes the observation to behavior that did not exist during that pass: "because firstmate restores the prior focus immediately after focused-at-birth creates, cmux was not the frontmost/active application". Two problems: (a) during that pass no create ever focused anything, so restoration was not the reason cmux was backgrounded - the original text correctly said--focus falsewas; (b)fm_backend_cmux_restore_focus(bin/backends/cmux.sh:536-573) only issues cmux-internalfocus-window/select-workspace/focus-pane/reorder-surface, so it cannot return macOS app-level focus to another application - it has no mechanism to make cmux stop being frontmost after a focused create raised it. Line 422 compounds this: it still reasons that "bringing cmux to the foreground ... would yank focus away from whatever the captain ... has in the foreground", which is now exactly what every default-mode spawn does. Keep the record as observed (note it ran under the pre-change--focus falsebehavior) rather than restating it in present tense with an unsupported causal claim.docs/cmux-backend.md:201- The sentence claims that for workspace-mode and secondmate creation, "any pre-return failure - unresolvable workspace id or default surface, or a failed restoration - closes ONLY that new workspace ... and then restores the captured focus context best-effort". That close-then-restore order only holds for the id/surface branches (which route throughfm_backend_cmux_abandon_new_workspace). The failed-restoration branch (bin/backends/cmux.sh:668-671 and 1374-1377) is the reverse: the restore already failed, then the workspace is closed, and no restore is attempted afterwards. Reword so the restoration-failure branch is described as "closes the new workspace after the failed restore" instead of implying a subsequent best-effort restore.bin/backends/cmux.sh:702- Tab mode is now the only remaining hole in the invariant this change establishes ("every creation path that passes --focus true owns the captain's view until it restores it", cmux.sh:576-578). When the new surface's UUID cannot be diffed out, this branch returns 1 without callingfm_backend_cmux_restore_focus "$prior_context", so the tab was created focused and the captain is left parked on an unusable new tab. This is pre-existing (unchanged by the diff) and correctly declines to close anything it cannot identify, but the focus restoration is independent of the close and is safe here: add a best-effortfm_backend_cmux_restore_focus "$prior_context" || truebefore thereturn 1, matching whatfm_backend_cmux_abandon_new_workspacenow does for the analogous workspace-mode id-unresolvable case.tests/fm-backend-cmux.test.sh:1782- Commit 38e9cda changesfm_backend_cmux_create_secondmatetonew-workspace --focus trueplus restore, but no test asserts it. The stateful fake (tests/cmux-fake-lib.sh:214) answersidentifywith empty output, sofm_backend_cmux_focus_contextreturns empty and the whole restore/cleanup path is a silent no-op in this test - the secondmate behavior change in this diff range has zero assertion coverage, while the workspace-mode counterpart does assert--focus true(line 476). One extraassert_contains "$log" $'\x1f'--focus$'\x1f'truehere would close the gap cheaply; making the stateful fake'sidentifyemit a focused context would additionally exercise the restore.bin/backends/cmux.sh:1363- The intent states: "Keep the already verified secondmate patch logic unchanged as its own commit". Commit 38e9cda honors that, but the follow-up commit 02fc73a then modifiesfm_backend_cmux_create_secondmateagain, addingfm_backend_cmux_abandon_new_workspace "$prior_context"(line 1365) and... "$prior_context" "$wsid"(line 1371). Round 1 explicitly scoped that parity work as a follow-up ("per the stated intent that commit stays as-is, so treat it as a parity follow-up"). The change is confined to failure branches and leaves the device-verified happy path intact, so this is very likely wanted - flagging only so the deviation from the stated "unchanged" constraint is a conscious call, since the secondmate path's new failure handling is not device-verified either.bin/backends/cmux.sh:651-fm_backend_cmux_create_taskworkspace mode (lines 651-671) andfm_backend_cmux_create_secondmate(lines 1357-1377) are now byte-for-byte the same five-step sequence apart from message text: capture context,new-workspace --focus true, resolve id (abandon on failure), resolve surface (abandon with id on failure), restore (close on failure). A sharedfm_backend_cmux_create_focused_workspace <title> <cwd> <what>echoing "<wsid> <sfid>" would remove the duplication and guarantee the two stay in lockstep. Noting only - the two paths are separately verified and a refactor of device-verified code is not worth forcing here.🔧 Fix: cmux: restore focus on tab-create bailouts, fix docs, revert secondmate
3 issues (1 warning, 2 infos) still open:
docs/cmux-backend.md:60- This operator-facing line now claims a spawn "immediately restores the exact window/workspace/pane/tab you were on, so the only cost is a short flicker rather than a lasting steal." Line 426 of this same change states the opposite for the case that matters most: "fm_backend_cmux_restore_focusissues only cmux-internal window/workspace/pane/tab commands and cannot hand macOS app-level focus back to another application." Source confirms line 426:fm_backend_cmux_restore_focus(bin/backends/cmux.sh:536-573) only issuesfocus-window/select-workspace/focus-pane/reorder-surface, all cmux-internal. So when the captain is working in another application (editor, browser), a default workspace-mode spawn raises cmux and leaves it frontmost - a lasting app-level steal, not a flicker. This is the exact line an operator reads to decide whether unattended spawning is view-safe, and the change moves the default container mode from never-touching-focus to always-raising cmux. Reword line 60 to scope the "flicker, not a steal" claim to focus inside cmux, and state that if another macOS app was frontmost, cmux is left in front (consistent with line 426).bin/backends/cmux.sh:1376- The invariant asserted by the new helper's comment (cmux.sh:576-578, "a creation path that passes --focus true owns the captain's view until it restores it") is still violated byfm_backend_cmux_create_secondmate: afternew-workspace --focus true(line 1369), the id-unresolvable (1374) and surface-unresolvable (1376) branches return 1 with nofm_backend_cmux_restore_focus, leaving the captain parked on the mate workspace - and on line 1376wsidis already known, so it additionally leaks an orphan workspace. This is deliberate authorized containment, not a defect to block on: the user intent requires "Keep the already verified secondmate patch logic unchanged as its own commit", commit 2801851 correctly reverted the round-2 edits here, and docs/cmux-backend.md:203 discloses the gap honestly ("its id/surface resolution failures fail fast without the abandon unwind"). Noting only so the residual gap is tracked; a future parity follow-up would route both branches throughfm_backend_cmux_abandon_new_workspace, and docs:203 could name the two consequences (orphan workspace, unrestored focus) explicitly.bin/backends/cmux.sh:690- The new comment at lines 696-698 justifies restoring focus on an unparseable acknowledgment because "the create was invoked and may well have taken focus." The branch immediately above it (line 689-692,new-surface --focus trueexiting non-zero) has the same property but returns 1 with no restore; the workspace-mode equivalent at line 656-659 is the same shape. In practice a non-zero CLI exit most likely means nothing was created and nothing was focused, so the asymmetry is probably intentional - and adding a blind restore there would printfm_backend_cmux_restore_focus's "created the cmux endpoint but ..." text after a create that never happened, which is worse. Flagging only so the deliberate scope of the new invariant is explicit rather than an oversight; no change recommended.🔧 Fix: docs: scope cmux spawn focus-restore claim to within-app focus
3 infos still open:
tests/fm-backend-cmux.test.sh:463- The headline behavior of this change - workspace mode creates focused and then restores the captain's full prior context on the SUCCESS path - has no assertion.test_create_task_creates_and_parses_idsdeliberately feeds an identify payload with nosurface_ref(line 463), sofm_backend_cmux_focus_contextyields empty andfm_backend_cmux_restore_focusshort-circuits to a no-op; the test then only asserts--focus true(line 477). The three new tests all cover failure paths (restore fails, surface unresolvable, id unresolvable). So no test proves that a healthy workspace-mode spawn actually issuesselect-workspace/reorder-surfacefor the prior context and still returns<wsid> <sfid>- exactly the gap that was closed for the secondmate path in round 2 (tests/fm-backend-cmux.test.sh:1795). A response-numbered variant with{"focused":{"workspace_ref":"workspace:5","surface_ref":"surface:9"}}at 2.out, alist-pane-surfacespayload for the restore, andassert_contains ... reorder-surface --surface surface:9plus the existing stdout check would close it. Workspace mode is the default container mode and, per the stated intent, is not device-verified in this home, so this is the one place a fake-CLI assertion carries the most weight.docs/cmux-backend.md:61- The rewritten operator-facing sentence says "A spawn does take focus while the new terminal is created, though (...), and that restoration is only within cmux" - but nothing before it states that firstmate restores the focus, so "that restoration" has no antecedent. A reader of the Setup section (the place someone decides whether unattended spawning is view-safe) is told focus is taken and then, without being told it is given back, is told about the limits of the giving-back. Inserting the missing clause (e.g. "...take focus while the new terminal is created, then immediately restore what you were on; that restoration is only within cmux...") makes the two-sentence pair (61-62) self-contained and keeps it consistent with lines 196-197 and 429.bin/backends/cmux.sh:672- Workspace mode is the DEFAULT container mode, and this change gives it a new failure mode it never had: a purely cosmetic focus-restore failure (select-workspacerejected, or the captain's previously focused surface closed during the ~1s create window, cmux.sh:554/563) now fails the whole spawn AND tears down the healthy, already-created task workspace (line 673). The blast radius is larger than tab mode's, where the same branch closes only one surface. This is explicitly authorized - the intent requires "the same focus-at-birth plus full focus restore and cleanup-on-restore-failure pattern" for workspace mode - and it is documented (docs/cmux-backend.md:195, 204-205), so it is a deliberate, disclosed tradeoff rather than a defect. Noting only so the widened failure surface on the default path is a conscious call; no change recommended.✅ **Test** - passed
✅ No issues found.
bin/fm-test-run.sh tests/fm-backend-cmux.test.sh— 100+ fake-CLI cases including the three new workspace-mode ones (restore failure closes the new workspace; unresolvable surface closes then restores; unresolvable workspace id restores without closing) and the updated tab-mode/secondmate focus assertionsbin/fm-test-run.sh tests/fm-backend-cmux-smoke.test.sh— real cmux 0.64.20 (build 100) smoke suite, including the livecreate_secondmatefocused-at-birth + restore legLive real-cmux workspace-mode verification:bash live-workspace-focus-at-birth.sh <repo> <evidence-dir>— created a control workspace withnew-workspace --focus false(pre-change behavior) and a patched workspace viafm_backend_cmux_create_task workspace fm-test-focus-birth /tmp, probed each surface withtput lines/tput colsviacmux send+send-key enter+read-screen, and comparedcmux identify --no-callerbefore/afterfm_backend_cmux_focus_contextbefore/after equality check on the live spawn (restore proof)Real CLI call trace of the live spawn via a PATH shim around/Applications/cmux.app/Contents/Resources/bin/cmuxgit diff 38e9cda ea492f4 -- bin/backends/cmux.shscoped tofm_backend_cmux_create_secondmate(confirms the verified secondmate logic was preserved as its own commit)grep -n -- '--focus' bin/backends/cmux.sh(confirms--focus falseremains only at shared-container creation and the throwaway-sibling safe-close sites)Post-runfm_backend_cmux_workspaces_all_windowscheck confirming bothfm-test-*workspaces were torn down by the safety-guarded cleanupdocs/cmux-backend.md:183- Task workspace mode's focus-at-birth change is reasoned by parity with the device-verified tab and secondmate paths and has no live painted-versus-black confirmation. This is now recorded in the owner doc, but a follow-up live pass on a real cmux 0.64.20+ install is still needed to promote it to a verified fact.✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.