Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ validated: a session workspace is squarely in pure-native-Rust territory.
| PTY process | **portable-pty** | cross-platform spawn/resize, cleaned env | Must |
| Storage | **rusqlite** (bundled) + FTS5 | embedded SQLite, WAL, full-text; no entitlement cost | Must |
| FS scan/watch | **notify** + **rayon** | debounced watch + parallel scan off the UI thread | Must |
| Async runtime | **tokio** | PTY read loops, fs watch, (later) MCP | Must |
| Async runtime | **tokio** | PTY read loops, fs watch, the MCP servers | Must |
| Errors / logging | **thiserror**/`anyhow` + **tracing** | typed errors, structured logs | Must |
| Release/update | **dist** (CLI archives) + **cargo-packager** (installers) | one GitHub release; installers unsigned for now and no self-update yet (§14) | Must (update = Should) |
| MCP/IDE server | tokio-tungstenite | per-session WS + JSON-RPC | **Deferred (Unsure)** |
| IDE bridge (termherd as MCP client, §7) | tokio-tungstenite | per-session WS + JSON-RPC | **Deferred (Unsure)** |
| MCP servers (live bridge, stdio) | `rmcp` (live bridge); `serde_json` only (stdio) | termherd as MCP *server* (§7, §15) | Shipped |
| Diff / highlight | `similar` + `syntect` | diff + syntax for the panel | **Deferred (Unsure)** |

Tabs, splits, and the keymap need **no new crates** — they are pure logic in
Expand Down Expand Up @@ -101,10 +102,12 @@ structurally prevents a new god-object.
written, so a reader looking for it is not missing something, and the file
read that would have used it goes through `ProjectScanner`.
- ~~`mcp` exists in the workspace only if/when the Unsure bet is taken; nothing
in the v1 path imports it.~~ **Superseded.** This still holds for the `mcp`
*crate* below (§15, `IdeBridge` — termherd as a **client**). It does not hold
for the MCP **control surface** (`F-mcp-control-surface`, #90), which shipped
in the v1 path as a module inside `app` — see §8.
in the v1 path imports it.~~ **Superseded.** The name now belongs to the
shipped MCP **control surface** (`F-mcp-control-surface`, #90): its live
bridge is a module inside `app` (§7, §8), and `crates/mcp` holds its stdio
server and the option catalogue both servers share (§15). What stays
deferred is the IDE bridge (`IdeBridge`, termherd as an MCP **client**,
§7), which has no crate and no port yet.
- The rule is CI-enforceable, so "main.js → 1,461 LOC" cannot recur.

## 5. The headless core (the quality keystone)
Expand Down Expand Up @@ -372,7 +375,7 @@ their job graphs.
| `app` | iced GUI: sidebar, tabs, splits, terminal; live MCP bridge | `iced` | Must |
| `mcp` | stdio MCP server binary + settings-option catalogue | `core` | Shipped |
| `private-fs` | owner-only files and directories | — | Shipped |
| IDE bridge | per-session IDE/MCP WS server (termherd as MCP client) | `tokio-tungstenite` | Deferred |
| IDE bridge | per-session IDE/MCP WS server (termherd as MCP client, sketched in §7); no crate yet, and not `mcp` | `tokio-tungstenite` | Deferred |

## 16. Migration path from the Electron app

Expand All @@ -396,5 +399,6 @@ their job graphs.
- **GUI framework churn.** Mitigation: logic is in `core`; the GUI crate is
replaceable without touching tests or adapters.
- **Claude format/OSC drift.** Mitigation: isolated, property-tested `claude`.
- **The Unsure bet (MCP/diff) returning later.** Mitigation: an `IdeBridge`
would be one more `core::ports` trait and adapter; additive, not a rework.
- **The Unsure bet (IDE bridge/diff) returning later.** Mitigation: an
`IdeBridge` would be one more `core::ports` trait and adapter; additive,
not a rework.
8 changes: 4 additions & 4 deletions docs/agents/mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ alongside the widget's `on_submit` path for human input (#246).

**Two things named `mcp`.** `crates/mcp` is built: the stdio server
`termherd-mcp` and the pure option catalogue both servers share. The live
bridge runs the other way round from it and lives in `app` (`app::mcp`).
`docs/ARCHITECTURE.md` §15 also sketches an `mcp` adapter for
`F-mcp-ide-bridge` — termherd as an MCP *client* of Claude's IDE bridge — and
that one is unbuilt.
bridge is the other server, and lives in `app` (`app::mcp`). What runs the
other way round is `F-mcp-ide-bridge` — termherd as an MCP *client* of
Claude's IDE bridge — which `docs/ARCHITECTURE.md` §7 sketches and §15 lists
on a row of its own: unbuilt, with no crate and no port.
3 changes: 3 additions & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@

- [Keyboard shortcuts](./reference/keyboard.md)
- [settings.json](./reference/settings.md)
- [Environment variables](./reference/environment.md)
- [Troubleshooting](./reference/troubleshooting.md)

# Driving termherd over MCP

- [Two surfaces](./mcp/index.md)
- [The live bridge](./mcp/live-bridge.md)
- [Driving the keyboard](./mcp/keyboard.md)
- [Attaching from outside](./mcp/attach.md)
- [Orchestrating sessions](./mcp/orchestrating.md)
- [The stdio server](./mcp/stdio.md)

# Project
Expand Down
5 changes: 3 additions & 2 deletions docs/src/guide/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ one; closing it closes TermHerd.
The `RUST_LOG` environment variable sets what is logged wherever it goes, for
example `termherd=debug`.

TermHerd is **single-instance**: an advisory lock file under the system temp
directory. A second launch carrying a `termherd://` link hands it to the
TermHerd is **single-instance**: a lock named after the system temp directory
(a file there on macOS, a named mutex on Windows, an abstract socket on
Linux). A second launch carrying a `termherd://` link hands it to the
running instance and exits with code 0; the tab opens there without bringing
the window forward. Without a link, it exits with code 2. To
run a second build alongside one that already holds the lock, point it at a
Expand Down
3 changes: 2 additions & 1 deletion docs/src/mcp/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ a terminal is a decision, not a default.
## Which one you want

- *"Split this pane and run the tests"* → **live bridge**. It only exists
inside a session TermHerd started.
inside a session TermHerd started. One session driving others through it:
[Orchestrating sessions](./orchestrating.md).
- *"Switch me to a light theme"* → either. Both carry `list_options` and
`set_option`, over one shared catalogue; the write lands in `settings.json`
and a running TermHerd applies it at once. From a session TermHerd started,
Expand Down
16 changes: 9 additions & 7 deletions docs/src/mcp/live-bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,12 @@ A worked example, from inside a session TermHerd launched:
unknown pointer word, is refused the same way.
- A wedged shell surfaces as a tool error, never a hang.

## What is still open

One follow-up:

| Gap | Issue |
| --- | --- |
| No pointer at TermHerd's own interface: the sidebar, the tab strip, a split gutter. | [#301](https://github.com/Termherd/termherd/issues/301) |
## What has shipped, and what is open

This page describes the bridge as it is built. What each release added is in
the [release notes](https://github.com/Termherd/termherd/releases) and the
project's
[`CHANGELOG.md`](https://github.com/Termherd/termherd/blob/main/CHANGELOG.md);
what is still open is the issue list filtered on the
[`area:mcp`](https://github.com/Termherd/termherd/issues?q=is%3Aissue%20is%3Aopen%20label%3Aarea%3Amcp)
label.
129 changes: 129 additions & 0 deletions docs/src/mcp/orchestrating.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Orchestrating sessions

Recipes for one Claude session driving others through the
[live bridge](./live-bridge.md): opening workers, handing them work, waiting
for them and reading what they did. Every tool named here is a live-bridge
tool; a Claude session lists them as `mcp__termherd__<tool>`.

The driving session must be one TermHerd launched as a Claude tab, or a client
[attached from outside](./attach.md). A `claude` typed by hand into a shell
pane has no bridge.

## Find a session, and keep its handle

`list_sessions` answers one row per live session. Address every later call by
the row's **`handle`**. Never use `resume_id`: Claude Code re-keys a
conversation on a fork or a plan-accept, while the handle stays the same for
as long as the session lives.

Each row also says what the session runs (`kind` is `shell` or `claude`), its
working directory, its status, and, for a Claude, the `peer_name` other Claude
sessions address it by. Two Claude tabs in the same directory are told apart
by their handle and `peer_name`, not by their title.

## Open a worker without taking the user's keyboard

```json
{ "tool": "open_session",
"args": { "project": "/path/to/worktree", "kind": "claude", "background": true } }
```

`background: true` appends the tab without activating it, so the next key the
user types still lands where they were typing. Read the new session from
`opened_handle`, not from `focused_handle`. Wait for `idle` before handing the
new session work: the wait answers at once when Claude is already at its
prompt.

Close a finished worker the same way, with `close_pane` and
`background: true`.

## Hand work over as a brief file

Write the task into a file in the worker's own directory — say
`.personal/brief.md` in its worktree — and type one short line:

```json
{ "tool": "run_in_session",
"args": { "session": "12", "text": "Read .personal/brief.md and carry it out.\n" } }
```

A file can be as long as the task needs, the user can read and amend it, and
it outlives the conversation that wrote it. A one-line prompt is typed
reliably, and the worker reads the whole brief with its own tools. A trailing
newline in `text` is sent as Enter and submits; earlier newlines stay line
breaks, so a multi-line prompt is still submitted once.

## Act, wait, observe

`run_in_session` returns as soon as the text is sent. Follow it with a wait,
then a read:

1. `run_in_session` — type the prompt.
2. `wait_for_status` with `statuses: ["busy"]` — just after typing, the
session still shows the status it had *before*, so a wait for `idle`
straight away answers at once with the old one.
3. `wait_for_status` with `statuses: ["idle", "attention"]`.
4. `read_terminal` — the last lines of the screen, as text.

Ask for more lines than the default 40 when reading a Claude session, on
`read_terminal` and `prompt_in_session` alike: Claude Code pads its screen with
blank rows between the conversation and the input box, and on a tall pane the
last 40 lines can hold none of the reply. `lines: 80` or more reaches it.

`prompt_in_session` does all three in one round trip: it types, waits up to
2 s for the status to leave its old value, waits for the target statuses, and
returns the screen text. Prefer it for a prompt; keep the separate calls when
you need to act between the steps.

Never poll `snapshot` to wait: it races the very transition you are waiting
for, and a `snapshot` loop costs far more than one wait.

`attention` means the worker is asking for something — a permission prompt, a
question. Read the screen and decide. An outward action it proposes (a push,
a public comment, closing an issue) waits for the user's answer, not yours.

## Long runs and the wait cap

A wait is bounded: `timeout_ms` defaults to 30 000 and is capped at 300 000
(five minutes), on `wait_for_status` and `prompt_in_session` alike. A timeout
is not an error: the call returns `{ status, timed_out: true }` with the
session's current status. For a task that runs longer, wait again — reading
the screen between two waits shows how far it got. A session that exits
settles any wait with `exited`.

## Prompting another Claude needs an opt-in

`prompt_in_session` refuses a **Claude** target unless nesting is allowed,
either for every call:

```json
{ "mcp": { "allow_claude_nesting": true } }
```

or for one call, with `allow_claude_nesting: true` among its arguments. A
shell target is never gated. The separate `run_in_session` +
`wait_for_status` calls are not gated either: the opt-in guards the composed
call only.

## Text already in another tab's input box

Before typing into a Claude session you did not just open, read its screen.
Text already sitting in its input box is usually a **suggestion** Claude Code
offers, drawn there for the user to accept or ignore — and in plain text it
looks exactly like something the user typed. It may also be the user's own
unfinished draft. Either way, it is not yours to send: never submit it
without asking the user.

## Leave the screenshot for last

`read_terminal` answers what a session printed. `screenshot` returns the whole
window as a PNG and weighs hundreds of times a `snapshot`: take one only for a
question text cannot answer — a colour, a glyph, a layout — and at the end of
the run rather than inside a loop.

## TermHerd's own interface

`press_keys` and `run_action` drive TermHerd, not a terminal. An open prompt
takes their keys, and `escape` leaves any prompt. `enter` on the quit prompt
quits TermHerd, and with it the bridge the driving session is talking through.
See [Driving the keyboard](./keyboard.md).
10 changes: 5 additions & 5 deletions docs/src/mcp/stdio.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ platform the server runs on.
## What it is not

It does not reach the running app: no sessions, no tabs, no terminals, no
keyboard. That is the [live bridge](./live-bridge.md), and it exists only
inside a session TermHerd launched.
keyboard. That is the [live bridge](./live-bridge.md), which reaches a session
TermHerd launched, or a client [attached from outside](./attach.md).

It is also **not** `F-mcp-ide-bridge` — a deferred, unbuilt feature that would
run the other way round, with TermHerd as an MCP *client* of Claude's IDE
bridge. `docs/ARCHITECTURE.md` §15 lists an `mcp` crate as deferred under that
name; the server described here lives in the same repository but answers a
different question.
bridge. `docs/ARCHITECTURE.md` §7 sketches that bridge, and §15 lists it as a
row of its own, apart from the `mcp` crate: the crate holds the server
described here and the option catalogue both servers share.
16 changes: 11 additions & 5 deletions docs/src/project/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ adapter.
| `claude` | a pure codec for the Claude CLI's own formats — path encode/derive, JSONL digest, OSC decode | nothing |
| `app` | the iced GUI shell; builds the adapters in `main()` and injects them; owns the one effect executor and the MCP control surface | `core` + adapters |
| `scan` | filesystem discovery — walks `~/.claude/projects` | `core` |
| `pty` | the terminal adapter (`portable-pty` + `alacritty_terminal`) | `core` |
| `mcp` | the stdio settings server | `core` |
| `pty` | the terminal adapter (`portable-pty` + `alacritty_terminal`) | `core`, `private-fs` |
| `mcp` | the stdio settings server, and the option catalogue both MCP servers share | `core` |
| `private-fs` | owner-only files and directories (`0600` / `0700`) | nothing |

The live MCP bridge is not a crate: it lives in `app`, as a bridge *into* the
shell. The IDE bridge — TermHerd as an MCP *client* of Claude's own — is
deferred and unbuilt.

When adding code the question is *which crate does this belong in?* If the
answer is "the core should call this adapter directly", the answer is wrong —
Expand Down Expand Up @@ -67,9 +72,10 @@ Non-negotiable, and CI-enforced:

- `unwrap`, `expect` and `panic` are **clippy-denied** in `core` and `claude`.
Production paths return typed errors.
- `unsafe_code` is **denied workspace-wide**. The one sanctioned exception is
the macOS AppKit FFI (the Cmd+Q quit path, the menus, the text and link
relays): a `cfg`-gated module with a `SAFETY:` note on every block.
- `unsafe_code` is **denied workspace-wide**. The two sanctioned exceptions
are the macOS AppKit FFI (the Cmd+Q quit path, the menus, the text and link
relays) and the Win32 process listing a ConPTY pane's Claude is found
through: each a `cfg`-gated module with a `SAFETY:` note on every block.
- **No global mutable state** — no `static mut`, no `lazy_static`, no
require-time singletons. Dependencies are built in `main()` and injected.
- **One logging stack**: `tracing`. No `println!` outside tests.
Expand Down
Loading
Loading