Skip to content

Implementation: multi-session mecatui window (#2058) - #2104

Draft
jtenniswood wants to merge 14 commits into
mainfrom
impl/tui-multi-session
Draft

jtenniswood wants to merge 14 commits into
mainfrom
impl/tui-multi-session

Conversation

@jtenniswood

@jtenniswood jtenniswood commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements the multi-session mecatui window from #2058. One window holds several sessions, ← on an empty prompt opens a session list, sessions can start in a fresh server-created worktree, and sessions can be stopped and deleted from the list, optionally removing their worktree.

  • Server: server-created session worktrees (PlacementBindRequest.NewWorktree) under $XDG_STATE_HOME/mecatl/worktrees/… on branch mecatl/<name>, with a worktree identity that survives commits (Revision: "worktree-v1", reattached by path). Stop-and-delete with optional remove_worktree (PlacementWorktreeRemover), which never forces and keeps the branch. The only forced removal is create-failure rollback, and it discards only the worktree that the failing create just added.
  • Wire: CreateSessionRequest.new_worktree, ServerCapabilities.create_worktrees, and DeleteSession{stop_active, remove_worktree}. HTTP maps a stop timeout to 504 with session_stop_timeout.
  • Client: CreateSessionRequest.NewWorktree; DeleteSession(ctx, id, DeleteSessionOptions{StopActive, RemoveWorktree}), with a cancel-then-delete fallback for older servers.
  • UI: NewWindow(Deps) becomes the program root. Each session model gets its own scoped context, status-line source, terminal title and lifecycle hooks, so background sessions can't change what the active one shows. The new Sessions key action (default ←) is remappable, and the main footer leads with ← sessions.
  • Session list (full screen): a header with the default checkout's label, status filter tabs with counts (tab/shift+tab), and a table of Sessions / Status / Updated beside a details column for the selected session (last message, placement, model, tokens, latest prompt), with divider lines between sections. The details stack under the table in narrow terminals. The selected row is a yellow bar on the theme's selection background; detail titles use the text colour and their content the muted grey. Updated tracks when a session's status or newest message last changed. List keys: enter switch, n new, w worktree, d delete, o open saved, esc close, and ctrl+c quits at once (asking first when other sessions are active).
  • Docs: user-docs/mecatui/sessions.md, keybindings.md, and the deployment hardening notes.

Rebased onto current main. Since #2105 removed ADRs and acceptance plans, the plan commit (ADR 0374 and docs/acceptance/tui-multi-session.md) and the ADR 0027 inventory rows were dropped. The worktree placement rule is recorded in AGENTS.md instead.

Issue relationship

Closes #2058

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

Baseline checks

  • Linting (task lint): 0 issues.
  • Fast offline test suite (task test): run on macOS. 17 tests fail, and all 17 fail identically on a clean origin/main worktree on the same machine (macOS socket-path bounds, the /var→/private/var symlink, TLS test key size, and microVM dev bundles), so none comes from this change. Every test in the packages this PR touches passes otherwise.
  • Pre-review race suite (task test:race): not run locally; left to CI.
  • Offline demo (go run ./cmd/mecademo)
  • Markdown changed: docs generation/link checks (task docs): 0 broken links or anchors.
  • User docs/user-facing behavior changed: site build (task site:build): not run locally; left to CI.
  • Guarded engine API affected: no engine/ changes.
  • Intentional engine API change: not applicable.

Also ran task generate (no drift) and the TypeScript SDK suite (362 tests pass).

User-facing change

Press ← on an empty prompt to see every session in the window. You can switch between them without stopping any, start new sessions (optionally in their own worktree), and delete finished ones. See user-docs/mecatui/sessions.md#work-with-several-sessions-in-one-window.

Special notes for reviewers

  • Message routing in the window root re-tags commands from each session model, including tea.Batch/tea.Sequence. Messages that can't be routed are dropped rather than misdelivered (cmd/mecatui/ui/session_window.go).
  • The configured-root ref keeps exact-HEAD reattachment; other worktrees reattach by path. TestTUIMultiSession_Scenario1_LegacyRevisionReattaches previously failed on Linux CI because its "stale" revision was the shared initial commit, which is also the root's current HEAD. The test now uses a revision that is really stale. macOS hid the problem because the unresolved /var path never matched the worktree listing.
  • create_worktrees is added to the SDK descriptor guard but not projected into the SDK's public ServerCapabilities, following the mcp_refresh precedent.
  • A process that dies between deleting a session and removing its worktree leaves the worktree on disk. Nothing sweeps it up automatically, but git worktree list still shows it.
  • Each background session keeps its own transcript and render state in memory.

jtenniswood and others added 11 commits October 6, 2026 14:37
…tree identity

ADR 0374 Decisions 1-3: CreateSession.new_worktree, create_worktrees
capability, worktree-v1 revisions reattached by path, and derived
server-created ownership. Adds the remaining #2058 proto fields.

Co-authored-by: Mecatl <[email protected]>
ADR 0374 Decision 4: DeleteSession.stop_active (lease-gated, bounded
wait) and remove_worktree (server-created, unshared, clean, never
--force, branch kept), inventory remove_worktree capability, HTTP
optional delete body, and the session_stop_timeout error code.

Co-authored-by: Mecatl <[email protected]>
Client half of the ADR 0374 / #2058 interface contract: CreateSessionRequest
with NewWorktree (sent as new_worktree) via CreateSessionWith, DeleteSession
options (stop_active, remove_worktree) and worktree outcome, and projection of
ServerCapabilities.create_worktrees plus the session-inventory remove_worktree
capability and reason. Existing callers keep plain-delete behaviour.

Co-authored-by: Mecatl <[email protected]>
Implements Scenario 4 of the multi-session mecatui plan (#2058, ADR 0374):
ui.NewWindow is the program root and holds one unchanged per-session Model
per window session. Session commands are re-tagged (including tea.Batch and
tea.Sequence children) so every message returns to its session and
background sessions keep draining their Converse streams; Bubble Tea program
commands pass through and a session's quit is handled by the window.

Input reaches the active session only; window size and terminal theme or
capability messages reach every session. Terminal title, status-line source,
and lifecycle-hook writes are scoped to the active session, and the window
owns the single status-source listener. The rebindable `Sessions` action
(default ←) opens the window list on an empty prompt with no overlay or
modal; rows show status, title (plus the ADR 0285 handle on duplicates), and
worktree branch, with an `o` action and hint for /sessions. Opening a chat
from /sessions adds it to the window (or switches to it), a background
approval ask badges the footer, and quitting with other running or awaiting
sessions asks once.

Co-authored-by: Mecatl <[email protected]>
…ndow list

Scenarios 5 and 6 of the multi-session window plan (ADR 0374, #2058).

In the ← window list, n starts a session on the default placement and w
starts one in a fresh server-created worktree (NewWorktree), both through
the normal create flow, switching to it. w is shown and live only when the
server advertises create_worktrees; a failed create removes its row,
returns to the originating session and shows the error in the list.

d asks for confirmation and deletes with stop_active, so a running or
awaiting session is stopped first; the deleted row closes only its own
session model. Deleting the active row switches to the next one, and
deleting the last opens a new default session. When the inventory row
reports remove_worktree, the confirmation also offers "delete and remove
worktree" and explains that ignored files are deleted and the branch is
kept. A refusal keeps the row; a post-stop retained worktree is reported
as "chat deleted, worktree kept" with the reason in plain words.

Co-authored-by: Mecatl <[email protected]>
AC6.3 of the multi-session window plan (ADR 0374, #2058).

user-docs/mecatui/sessions.md gains "Work with several sessions in one
window": the ← list with its statuses, labels, the /sessions hint and o,
n and w new sessions including where server-created worktrees live and
which deployments offer them, deletion with d and the worktree-removal
rules (server-created, unshared, clean, never forced, branch kept), and
the quit confirmation. keybindings.md documents the remappable Sessions
action (default left) and the quit confirmation.

The AGENTS.md placement bullet and the architecture "Server-owned session
placement" section describe new_worktree, worktree-v1 path reattach, and
derived ownership. grpc-schema.md is regenerated for the ADR 0374 fields.

Co-authored-by: Mecatl <[email protected]>
…d delete body, branch display

Panel-review fixes for the multi-session window (#2058):

- Inventory no longer runs git per row: PlacementWorktreeRemover gains
  WorktreeServerCreated, a path-shape-only (Lstat/EvalSymlinks) listing check;
  WorktreeOwnership (with the git status cleanliness check) runs only at delete
  time. Hardened git runs with GIT_OPTIONAL_LOCKS=0.
- The optional delete/clear/fork JSON body is capped at 16 KiB (413).
- The server-generated mecatl/<name> branch is exempt from the display slash
  ban, so CreateSession/GetSession placement carries it.
- keyedMutex is generic; worktree path locks reuse it.
- Worktree removal after delete runs cancel-detached and bounded; re-check and
  removal failures reach the placement diagnostic, path-redacted.
- Test git helpers run with a controlled environment (no ambient GIT_*).
- Lease positive proofs for stop_active delete.

Co-authored-by: Mecatl <[email protected]>
…w, forced-rollback exception

For #2058: POST /v1/sessions lists new_worktree and its 412 refusal; the delete,
clear, and fork routes document the 16 KiB body cap (413). ADR 0027 gains List 1
row 93 for the mecatui window's per-session models, and row 90, ADR 0374, and
deployment-and-hardening note that create-failure rollback is the only forced
worktree removal. ADR 0374 maps the stop timeout to HTTP 504
session_stop_timeout. Fix the dangling no-FS sentence in AGENTS.md.

Co-authored-by: Mecatl <[email protected]>
… period, old-server delete fallback

Panel review of the multi-session window (#2058):

- Route untagged messages fail-closed: only terminal input, broadcasts,
  window messages, and tea.ResumeMsg reach a session; anything else is
  dropped instead of falling through to the active session. A real
  tea.Program test proves a background tea.Sequence lands on its session.
- Give the host lifecycle hook one window-wide busy period: Start when the
  first session becomes busy, Stop when the last ends, approvals forwarded
  for every session.
- On a server without create_worktrees (pre stop_active), a refused delete
  cancels the session's own run and retries a plain delete.
- Exercise mouse and focus routing with real messages; cover an awaiting
  background session in the quit confirmation and the refusal notice being
  replaced; factor the per-index model update and use slices.Clone.

Refs #2058

Co-authored-by: Mecatl <[email protected]>
- Make the configured-root stale-revision assertion use a revision that is
  really stale. The pre-commit sibling HEAD is the shared initial commit, which
  the root's exact-HEAD match legitimately accepts when the root path matches
  the git worktree listing (Linux CI); macOS hid it via /var symlinks.
- Add create_worktrees to the SDK ServerCapabilities descriptor guard.
- Drop the link to the ADR deleted on main.

Co-authored-by: Mecatl <[email protected]>
@jtenniswood
jtenniswood force-pushed the impl/tui-multi-session branch from c869781 to e194491 Compare October 6, 2026 14:10
The window session list now fills the screen: a header with the default
checkout's label, status filter tabs with counts (tab/shift+tab), a table
of Sessions / Status / Updated, and a details column for the selected
session (last message, placement, model, tokens, latest prompt), stacked
under the table in a narrow terminal. Updated tracks when a session's
status or newest transcript card last changed. The main footer leads with
the session list key ("← sessions") inside the window.

Co-authored-by: Mecatl <[email protected]>
jtenniswood and others added 2 commits October 6, 2026 20:04
Rules separate the header and tabs, the table, any notice or delete
confirmation, and the footer; a vertical rule divides the table from the
details column, and rules separate the details' sections.

Co-authored-by: Mecatl <[email protected]>
The selected row is a solid highlight bar: bold warning-coloured (yellow)
text on the theme's selection background across the table. Details
section titles use the primary text colour and their content the muted
grey. ctrl+c in the list quits at once (asking first only when other
sessions are running or awaiting approval) instead of arming the
session's double-press guard; the list footer shows it.

Co-authored-by: Mecatl <[email protected]>

This branch was successfully deployed

1 active deployment
Preview — 74e5dec6 Deployed Oct 7, 2026 by vercel[bot]
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.

I want to be able to run multiple separate sessions from the same window

1 participant