Skip to content

fix(cmux): create task and secondmate workspaces focused at birth - #24

Merged
egarlock merged 6 commits into
mainfrom
fm/cmux-focus-at-birth-w7
Aug 1, 2026
Merged

egarlock merged 6 commits into
mainfrom
fm/cmux-focus-at-birth-w7

Conversation

@egarlock

@egarlock egarlock commented Aug 1, 2026

Copy link
Copy Markdown
Owner

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_secondmate and fm_backend_cmux_create_task workspace mode now create with new-workspace --focus true and 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 false remains only at shared-container creation and throwaway safe-close siblings.
  • Added fm_backend_cmux_abandon_new_workspace so 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, and fm_backend_cmux_restore_focus messages were reworded to be endpoint-neutral across all three callers.
  • Rewrote the CORRECTION note and docs/cmux-backend.md to 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.sh and the real-cmux tests/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 false placeholders 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 patched fm_backend_cmux_create_task workspace path 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 shows new-workspace --focus true followed 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 own read-screen output 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

=== cmux version ===
cmux 0.64.20 (100) [14e3400b9]

=== CONTROL: workspace created with --focus false (pre-change behavior) ===
control workspace=63F00A52-9479-40E5-89AE-E661304BBBCB surface=C9776061-DED5-4E0C-BAB9-B382C302FFE3
--- control surface screen (never displayed since birth) ---
  | printf "SIZE_REPORT rows=%s cols=%s
  | " "$(tput lines)" "$(tput cols)"Last login: Sat Aug  1 15:08:05 on ttys029
  | ---------------------------------------------------------------------------------------------------
  | | [arm] /tmp @ Eric-Garlocks-MBPM5M (ericgarlock) 
  | | => printf "SIZE_REPORT rows=%s cols=%s
  | dquote> " "$(tput lines)" "$(tput cols)"
  | SIZE_REPORT rows=35 cols=99
  | ---------------------------------------------------------------------------------------------------
  | | [arm] /tmp @ Eric-Garlocks-MBPM5M (ericgarlock) 
  | | => 

=== BEFORE: captain's focused context (cmux identify --no-caller) ===
{"is_browser_surface":false,"pane_ref":"pane:2","surface_ref":"surface:47","surface_type":"terminal","tab_ref":"tab:47","window_ref":"window:1","workspace_ref":"workspace:2"}
fm_backend_cmux_focus_context -> 'window:1 workspace:2 pane:2 surface:47'

=== RUN: fm_backend_cmux_create_task workspace fm-test-focus-birth /tmp ===
create_task exit=0 ids='7BE15956-C198-4056-8465-F6F5FEFE9AAF D8198A65-3130-46C0-94E3-7B3FA822A50D'
--- real cmux CLI calls issued by the spawn (in order) ---
  | 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-C198-4056-8465-F6F5FEFE9AAF --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 true
new workspace=7BE15956-C198-4056-8465-F6F5FEFE9AAF surface=D8198A65-3130-46C0-94E3-7B3FA822A50D

=== AFTER: captain's focused context (must equal BEFORE) ===
{"is_browser_surface":false,"pane_ref":"pane:2","surface_ref":"surface:47","surface_type":"terminal","tab_ref":"tab:47","window_ref":"window:1","workspace_ref":"workspace:2"}
fm_backend_cmux_focus_context -> 'window:1 workspace:2 pane:2 surface:47'
RESULT: focus RESTORED to the captain's prior window/workspace/pane/surface

=== PATCHED surface screen (created focused at birth, then handed focus back) ===
  | Last login: Sat Aug  1 15:10:34 on ttys027
  | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  | | [arm] /tmp @ Eric-Garlocks-MBPM5M (ericgarlock) 
  | | => printf "SIZE_REPORT rows=%s cols=%s
  | dquote> " "$(tput lines)" "$(tput cols)"
  | SIZE_REPORT rows=60 cols=177
  | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  | | [arm] /tmp @ Eric-Garlocks-MBPM5M (ericgarlock) 
  | | => 

=== done ===
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 true

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-C198-4056-8465-F6F5FEFE9AAF --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 true
Evidence: Terminal size the new surface actually resolves (the black-TUI defect), read back from the real app
CONTROL (pre-change, new-workspace --focus false, never displayed):
SIZE_REPORT rows=35 cols=99 <- unrealized default geometry

PATCHED (new-workspace --focus true, then full focus restore):
SIZE_REPORT rows=60 cols=177 <- real window geometry resolved at birth

BEFORE focus context: window:1 workspace:2 pane:2 surface:47
AFTER focus context: window:1 workspace:2 pane:2 surface:47
RESULT: focus RESTORED to the captain's prior window/workspace/pane/surface
Evidence: Harness used for the live verification (safety-guarded, fm-test-* labels only)
#!/usr/bin/env bash
# Live (real cmux app) verification of the change under test:
# fm_backend_cmux_create_task workspace mode must create the task workspace
# FOCUSED at birth and then restore the captain's full prior focus context,
# so the new surface resolves its terminal size (the black-TUI defect).
#
# Safety: only fm-test-* labels, only workspaces this script creates, torn
# down through tests/cmux-test-safety.sh's guarded closes.
set -u

ROOT=${1:?repo root required}
EV=${2:?evidence dir required}

# shellcheck source=/dev/null
. "$ROOT/bin/fm-backend.sh"
fm_backend_source cmux || { echo "skip: could not source cmux adapter"; exit 0; }
fm_backend_cmux_version_check >/dev/null 2>&1 || { echo "skip: cmux unavailable/too old"; exit 0; }
[ "$(fm_backend_cmux_ping_state)" = ok ] || { echo "skip: cmux socket unreachable"; exit 0; }
# shellcheck source=/dev/null
. "$ROOT/tests/cmux-test-safety.sh"

WS_CTRL="" WS_NEW=""
cleanup() {
  [ -z "$WS_CTRL" ] || cmux_safe_close_workspace "$WS_CTRL" "fm-test-unfocused-birth"
  [ -z "$WS_NEW" ] || cmux_safe_close_workspace "$WS_NEW" "fm-test-focus-birth"
}
trap cleanup EXIT

hr() { printf '\n=== %s ===\n' "$1"; }

probe_size() {  # <ws> <sf> -> echoes the surface's own report of its terminal size
  local ws=$1 sf=$2 out
  fm_backend_cmux_cli send --workspace "$ws" --surface "$sf" -- \
    'printf "SIZE_REPORT rows=%s cols=%s\n" "$(tput lines)" "$(tput cols)"' >/dev/null 2>&1
  sleep 0.4
  fm_backend_cmux_cli send-key --workspace "$ws" --surface "$sf" enter >/dev/null 2>&1
  sleep 1.0
  out=$(fm_backend_cmux_cli read-screen --workspace "$ws" --surface "$sf" 2>&1)
  printf '%s\n' "$out"
}

hr "cmux version"
fm_backend_cmux_cli version

# --- control: the PRE-CHANGE behavior, a workspace created --focus false -----
hr "CONTROL: workspace created with --focus false (pre-change behavior)"
ctrl_title=$(fm_backend_cmux_scoped_title fm-test-unfocused-birth)
fm_backend_cmux_cli new-workspace --name "$ctrl_title" --cwd /tmp --focus false --id-format uuids >/dev/null 2>&1
sleep 1.0
WS_CTRL=$(fm_backend_cmux_workspace_id_for_label "$ctrl_title")
SF_CTRL=$(fm_backend_cmux_surface_id_for_workspace "$WS_CTRL")
echo "control workspace=$WS_CTRL surface=$SF_CTRL"
echo "--- control surface screen (never displayed since birth) ---"
probe_size "$WS_CTRL" "$SF_CTRL" | sed 's/^/  | /'

# --- the patched path --------------------------------------------------------
hr "BEFORE: captain's focused context (cmux identify --no-caller)"
before_json=$(fm_backend_cmux_cli identify --no-caller 2>/dev/null)
printf '%s\n' "$before_json" | jq -c '.focused' 2>/dev/null || printf '%s\n' "$before_json"
before_ctx=$(fm_backend_cmux_focus_context)
echo "fm_backend_cmux_focus_context -> '$before_ctx'"

hr "RUN: fm_backend_cmux_create_task workspace fm-test-focus-birth /tmp"
: > "$EV/cmux-cli-transcript.log"
export FM_CMUX_TRACE_LOG="$EV/cmux-cli-transcript.log"
shim_dir="$EV/.shim"; mkdir -p "$shim_dir"
cat > "$shim_dir/cmux" <<'SHIM'
#!/usr/bin/env bash
{ printf 'cmux'; printf ' %s' "$@"; printf '\n'; } >> "$FM_CMUX_TRACE_LOG"
exec /Applications/cmux.app/Contents/Resources/bin/cmux "$@"
SHIM
chmod +x "$shim_dir/cmux"
ids=$(PATH="$shim_dir:$PATH" FM_CMUX_TRACE_LOG="$FM_CMUX_TRACE_LOG" \
  bash -c '. "$0/bin/backends/cmux.sh"; fm_backend_cmux_create_task workspace fm-test-focus-birth /tmp' "$ROOT" 2>"$EV/.err")
status=$?
echo "create_task exit=$status ids='$ids'"
echo "--- real cmux CLI calls issued by the spawn (in order) ---"
sed 's/^/  | /' "$FM_CMUX_TRACE_LOG"
[ ! -s "$EV/.err" ] || { echo "--- stderr ---"; sed 's/^/  | /' "$EV/.err"; }
read -r WS_NEW SF_NEW <<EOF
$ids
EOF
echo "new workspace=$WS_NEW surface=$SF_NEW"

hr "AFTER: captain's focused context (must equal BEFORE)"
after_json=$(fm_backend_cmux_cli identify --no-caller 2>/dev/null)
printf '%s\n' "$after_json" | jq -c '.focused' 2>/dev/null || printf '%s\n' "$after_json"
after_ctx=$(fm_backend_cmux_focus_context)
echo "fm_backend_cmux_focus_context -> '$after_ctx'"
if [ "$before_ctx" = "$after_ctx" ]; then
  echo "RESULT: focus RESTORED to the captain's prior window/workspace/pane/surface"
else
  echo "RESULT: focus NOT restored (before='$before_ctx' after='$after_ctx')"
fi

hr "PATCHED surface screen (created focused at birth, then handed focus back)"
probe_size "$WS_NEW" "$SF_NEW" | sed 's/^/  | /'

hr "done"

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 3 infos
  • ⚠️ 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 before fm_backend_cmux_restore_focus ever 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: wsid is 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-effort fm_backend_cmux_restore_focus &#34;$prior_context&#34; before both returns, plus fm_backend_cmux_close_workspace_safely &#34;$wsid&#34; 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 default focus to false, so an unattended spawn never steals your view." After this change the default container mode (workspace) creates with --focus true and 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's fm_backend_cmux_focus_context can 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 via fm_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_focus emits 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 OLD new-workspace --focus false workspace-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 false was; (b) fm_backend_cmux_restore_focus (bin/backends/cmux.sh:536-573) only issues cmux-internal focus-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 false behavior) 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 through fm_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 calling fm_backend_cmux_restore_focus &#34;$prior_context&#34;, 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-effort fm_backend_cmux_restore_focus &#34;$prior_context&#34; || true before the return 1, matching what fm_backend_cmux_abandon_new_workspace now does for the analogous workspace-mode id-unresolvable case.
  • ℹ️ tests/fm-backend-cmux.test.sh:1782 - Commit 38e9cda changes fm_backend_cmux_create_secondmate to new-workspace --focus true plus restore, but no test asserts it. The stateful fake (tests/cmux-fake-lib.sh:214) answers identify with empty output, so fm_backend_cmux_focus_context returns 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 extra assert_contains &#34;$log&#34; $&#39;\x1f&#39;--focus$&#39;\x1f&#39;true here would close the gap cheaply; making the stateful fake's identify emit 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 modifies fm_backend_cmux_create_secondmate again, adding fm_backend_cmux_abandon_new_workspace &#34;$prior_context&#34; (line 1365) and ... &#34;$prior_context&#34; &#34;$wsid&#34; (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_task workspace mode (lines 651-671) and fm_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 shared fm_backend_cmux_create_focused_workspace &lt;title&gt; &lt;cwd&gt; &lt;what&gt; 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_focus issues 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 issues focus-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 by fm_backend_cmux_create_secondmate: after new-workspace --focus true (line 1369), the id-unresolvable (1374) and surface-unresolvable (1376) branches return 1 with no fm_backend_cmux_restore_focus, leaving the captain parked on the mate workspace - and on line 1376 wsid is 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 through fm_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 true exiting 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 print fm_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_ids deliberately feeds an identify payload with no surface_ref (line 463), so fm_backend_cmux_focus_context yields empty and fm_backend_cmux_restore_focus short-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 issues select-workspace/reorder-surface for the prior context and still returns &lt;wsid&gt; &lt;sfid&gt; - 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 {&#34;focused&#34;:{&#34;workspace_ref&#34;:&#34;workspace:5&#34;,&#34;surface_ref&#34;:&#34;surface:9&#34;}} at 2.out, a list-pane-surfaces payload for the restore, and assert_contains ... reorder-surface --surface surface:9 plus 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-workspace rejected, 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 assertions
  • bin/fm-test-run.sh tests/fm-backend-cmux-smoke.test.sh — real cmux 0.64.20 (build 100) smoke suite, including the live create_secondmate focused-at-birth + restore leg
  • Live real-cmux workspace-mode verification: bash live-workspace-focus-at-birth.sh &lt;repo&gt; &lt;evidence-dir&gt; — created a control workspace with new-workspace --focus false (pre-change behavior) and a patched workspace via fm_backend_cmux_create_task workspace fm-test-focus-birth /tmp, probed each surface with tput lines/tput cols via cmux send + send-key enter + read-screen, and compared cmux identify --no-caller before/after
  • fm_backend_cmux_focus_context before/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/cmux
  • git diff 38e9cda ea492f4 -- bin/backends/cmux.sh scoped to fm_backend_cmux_create_secondmate (confirms the verified secondmate logic was preserved as its own commit)
  • grep -n -- &#39;--focus&#39; bin/backends/cmux.sh (confirms --focus false remains only at shared-container creation and the throwaway-sibling safe-close sites)
  • Post-run fm_backend_cmux_workspaces_all_windows check confirming both fm-test-* workspaces were torn down by the safety-guarded cleanup
⚠️ **Document** - 1 info
  • ℹ️ docs/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.

egarlock added 6 commits July 31, 2026 21:42
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.
@egarlock
egarlock merged commit 4363d94 into main Aug 1, 2026
10 of 11 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.

1 participant