summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOgulcan Celik <ogulcancelik@gmail.com>2026-03-23 00:36:52 +0300
committerOgulcan Celik <ogulcancelik@gmail.com>2026-03-27 21:37:05 +0300
commita57b97285c3308fbdcc167ab3da9ae1dd78767b6 (patch)
tree25a0eb4df100ea9b09c7551fa7cc25f8d86027ef
initial release
terminal workspace manager for AI coding agents. single rust binary, runs inside your existing terminal. workspaces, tiled panes, intelligent agent state detection. https://herdr.dev
-rw-r--r--.github/workflows/release.yml74
-rw-r--r--.gitignore5
-rw-r--r--AGENTS.md31
-rw-r--r--Cargo.lock2054
-rw-r--r--Cargo.toml26
-rw-r--r--LICENSE661
-rw-r--r--README.md239
-rw-r--r--assets/logo.pngbin0 -> 120644 bytes
-rw-r--r--assets/screenshot.pngbin0 -> 256906 bytes
-rw-r--r--justfile29
-rw-r--r--src/app/actions.rs417
-rw-r--r--src/app/input.rs562
-rw-r--r--src/app/mod.rs159
-rw-r--r--src/app/state.rs130
-rw-r--r--src/config.rs279
-rw-r--r--src/detect.rs1163
-rw-r--r--src/events.rs26
-rw-r--r--src/input.rs339
-rw-r--r--src/layout.rs470
-rw-r--r--src/main.rs228
-rw-r--r--src/pane.rs370
-rw-r--r--src/persist.rs446
-rw-r--r--src/platform/fallback.rs11
-rw-r--r--src/platform/linux.rs27
-rw-r--r--src/platform/macos.rs98
-rw-r--r--src/platform/mod.rs19
-rw-r--r--src/pty_callbacks.rs374
-rw-r--r--src/selection.rs253
-rw-r--r--src/ui.rs870
-rw-r--r--src/update.rs359
-rw-r--r--src/workspace.rs220
-rw-r--r--tests/integration/ext/herdr-test.ts146
-rwxr-xr-xtests/integration/pdeathsig.py44
-rw-r--r--tests/integration/results/.gitignore2
-rwxr-xr-xtests/integration/run_all.sh116
-rwxr-xr-xtests/integration/run_spec.sh126
-rw-r--r--tests/integration/specs/create_workspace.md12
-rw-r--r--tests/integration/specs/sidebar_collapse.md13
-rw-r--r--tests/integration/specs/split_and_navigate.md11
-rw-r--r--tests/integration/specs/workspace_switching.md12
-rw-r--r--tests/integration/system.md48
41 files changed, 10469 insertions, 0 deletions
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
new file mode 100644
index 00000000..75047cdd
--- /dev/null
+++ b/.github/workflows/release.yml
@@ -0,0 +1,74 @@
+name: Release
+
+on:
+ push:
+ tags:
+ - "v*"
+
+permissions:
+ contents: write
+
+jobs:
+ build:
+ strategy:
+ matrix:
+ include:
+ - target: x86_64-unknown-linux-gnu
+ os: ubuntu-latest
+ name: herdr-linux-x86_64
+ - target: aarch64-unknown-linux-gnu
+ os: ubuntu-latest
+ name: herdr-linux-aarch64
+ - target: x86_64-apple-darwin
+ os: macos-latest
+ name: herdr-macos-x86_64
+ - target: aarch64-apple-darwin
+ os: macos-latest
+ name: herdr-macos-aarch64
+
+ runs-on: ${{ matrix.os }}
+
+ steps:
+ - uses: actions/checkout@v4
+
+ - name: Install Rust
+ uses: dtolnay/rust-toolchain@stable
+ with:
+ targets: ${{ matrix.target }}
+
+ - name: Install cross-compilation tools (Linux aarch64)
+ if: matrix.target == 'aarch64-unknown-linux-gnu'
+ run: |
+ sudo apt-get update
+ sudo apt-get install -y gcc-aarch64-linux-gnu
+ echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> $GITHUB_ENV
+
+ - name: Build
+ run: cargo build --release --target ${{ matrix.target }}
+
+ - name: Rename binary
+ run: cp target/${{ matrix.target }}/release/herdr ${{ matrix.name }}
+
+ - name: Upload artifact
+ uses: actions/upload-artifact@v4
+ with:
+ name: ${{ matrix.name }}
+ path: ${{ matrix.name }}
+
+ release:
+ needs: build
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Download all artifacts
+ uses: actions/download-artifact@v4
+
+ - name: Create release
+ uses: softprops/action-gh-release@v2
+ with:
+ files: |
+ herdr-linux-x86_64/herdr-linux-x86_64
+ herdr-linux-aarch64/herdr-linux-aarch64
+ herdr-macos-x86_64/herdr-macos-x86_64
+ herdr-macos-aarch64/herdr-macos-aarch64
+ generate_release_notes: true
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..84b8c03e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+/target
+*.swp
+*.swo
+*~
+.DS_Store
diff --git a/AGENTS.md b/AGENTS.md
new file mode 100644
index 00000000..f65fa0ef
--- /dev/null
+++ b/AGENTS.md
@@ -0,0 +1,31 @@
+# herdr
+
+Terminal workspace manager for AI coding agents. Rust + ratatui.
+
+## Principles
+
+- **State is separated from runtime.** `AppState` is pure data, testable without PTYs or async. `PaneState` is separate from `PaneRuntime`. Workspace logic doesn't need real terminals.
+- **Render is pure.** `compute_view()` handles geometry and mutations. `render()` takes `&AppState` and only draws. Never mutate state during render.
+- **No god objects.** If a module is doing too many things, split it. `app/` is already split into state, actions, and input. Keep it that way.
+- **Platform code is isolated.** OS-specific behavior lives in `src/platform/`. Core modules don't have `#[cfg(target_os)]`.
+- **Detection is decoupled.** The detector reads a screen snapshot, never touches the parser or viewport state.
+
+## Testing
+
+```bash
+just test # unit tests
+just test-integration # LLM-based integration tests (needs pi + tmux)
+just test-all # both
+just clean-tests # kill orphaned test tmux sessions
+```
+
+Unit tests live next to the code (`#[cfg(test)] mod tests`). If you add behavior to `AppState` or `Workspace`, it should be testable with `AppState::test_new()` and `Workspace::test_new()` — no PTYs.
+
+Integration tests are markdown specs in `tests/integration/specs/`. A pi agent executes them against herdr in an isolated tmux server. See `tests/integration/system.md` for the test agent prompt.
+
+## Conventions
+
+- Conventional commits, lowercase, no emojis.
+- Rust: no `unwrap()` in production code. `tracing` for logging. `#[allow]` only with a comment explaining why.
+- Don't bypass checks. If tests fail, fix them before committing.
+- Don't add dependencies without a reason. Check if the existing deps cover it first.
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 00000000..ce358691
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,2054 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "aho-corasick"
+version = "1.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "allocator-api2"
+version = "0.2.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
+
+[[package]]
+name = "anyhow"
+version = "1.0.102"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c"
+
+[[package]]
+name = "arrayvec"
+version = "0.7.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
+
+[[package]]
+name = "atomic"
+version = "0.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a89cbf775b137e9b968e67227ef7f775587cde3fd31b0d8599dbd0f598a48340"
+dependencies = [
+ "bytemuck",
+]
+
+[[package]]
+name = "autocfg"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
+
+[[package]]
+name = "base64"
+version = "0.22.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
+
+[[package]]
+name = "bit-set"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1"
+dependencies = [
+ "bit-vec",
+]
+
+[[package]]
+name = "bit-vec"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
+
+[[package]]
+name = "bitflags"
+version = "1.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
+
+[[package]]
+name = "bitflags"
+version = "2.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
+
+[[package]]
+name = "block-buffer"
+version = "0.10.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
+dependencies = [
+ "generic-array",
+]
+
+[[package]]
+name = "bumpalo"
+version = "3.20.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb"
+
+[[package]]
+name = "bytemuck"
+version = "1.25.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c8efb64bd706a16a1bdde310ae86b351e4d21550d98d056f22f8a7f7a2183fec"
+
+[[package]]
+name = "bytes"
+version = "1.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33"
+
+[[package]]
+name = "castaway"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a"
+dependencies = [
+ "rustversion",
+]
+
+[[package]]
+name = "cfg-if"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
+
+[[package]]
+name = "cfg_aliases"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
+
+[[package]]
+name = "cfg_aliases"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
+
+[[package]]
+name = "compact_str"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fdb1325a1cece981e8a296ab8f0f9b63ae357bd0784a9faaf548cc7b480707a"
+dependencies = [
+ "castaway",
+ "cfg-if",
+ "itoa",
+ "rustversion",
+ "ryu",
+ "static_assertions",
+]
+
+[[package]]
+name = "convert_case"
+version = "0.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "633458d4ef8c78b72454de2d54fd6ab2e60f9e02be22f3c6104cdc8a4e0fceb9"
+dependencies = [
+ "unicode-segmentation",
+]
+
+[[package]]
+name = "cpufeatures"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "crossterm"
+version = "0.29.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d8b9f2e4c67f833b660cdb0a3523065869fb35570177239812ed4c905aeff87b"
+dependencies = [
+ "bitflags 2.11.0",
+ "crossterm_winapi",
+ "derive_more",
+ "document-features",
+ "mio",
+ "parking_lot",
+ "rustix",
+ "signal-hook",
+ "signal-hook-mio",
+ "winapi",
+]
+
+[[package]]
+name = "crossterm_winapi"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "crypto-common"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
+dependencies = [
+ "generic-array",
+ "typenum",
+]
+
+[[package]]
+name = "csscolorparser"
+version = "0.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eb2a7d3066da2de787b7f032c736763eb7ae5d355f81a68bab2675a96008b0bf"
+dependencies = [
+ "lab",
+ "phf",
+]
+
+[[package]]
+name = "darling"
+version = "0.23.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "25ae13da2f202d56bd7f91c25fba009e7717a1e4a1cc98a76d844b65ae912e9d"
+dependencies = [
+ "darling_core",
+ "darling_macro",
+]
+
+[[package]]
+name = "darling_core"
+version = "0.23.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9865a50f7c335f53564bb694ef660825eb8610e0a53d3e11bf1b0d3df31e03b0"
+dependencies = [
+ "ident_case",
+ "proc-macro2",
+ "quote",
+ "strsim",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "darling_macro"
+version = "0.23.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3984ec7bd6cfa798e62b4a642426a5be0e68f9401cfc2a01e3fa9ea2fcdb8d"
+dependencies = [
+ "darling_core",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "deltae"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5729f5117e208430e437df2f4843f5e5952997175992d1414f94c57d61e270b4"
+
+[[package]]
+name = "deranged"
+version = "0.5.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c"
+dependencies = [
+ "powerfmt",
+]
+
+[[package]]
+name = "derive_more"
+version = "2.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d751e9e49156b02b44f9c1815bcb94b984cdcc4396ecc32521c739452808b134"
+dependencies = [
+ "derive_more-impl",
+]
+
+[[package]]
+name = "derive_more-impl"
+version = "2.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb"
+dependencies = [
+ "convert_case",
+ "proc-macro2",
+ "quote",
+ "rustc_version",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "digest"
+version = "0.10.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
+dependencies = [
+ "block-buffer",
+ "crypto-common",
+]
+
+[[package]]
+name = "document-features"
+version = "0.2.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d4b8a88685455ed29a21542a33abd9cb6510b6b129abadabdcef0f4c55bc8f61"
+dependencies = [
+ "litrs",
+]
+
+[[package]]
+name = "downcast-rs"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2"
+
+[[package]]
+name = "either"
+version = "1.15.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
+
+[[package]]
+name = "equivalent"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
+
+[[package]]
+name = "errno"
+version = "0.3.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
+dependencies = [
+ "libc",
+ "windows-sys",
+]
+
+[[package]]
+name = "euclid"
+version = "0.22.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1a05365e3b1c6d1650318537c7460c6923f1abdd272ad6842baa2b509957a06"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
+name = "fancy-regex"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b95f7c0680e4142284cf8b22c14a476e87d61b004a3a0861872b32ef7ead40a2"
+dependencies = [
+ "bit-set",
+ "regex",
+]
+
+[[package]]
+name = "filedescriptor"
+version = "0.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e40758ed24c9b2eeb76c35fb0aebc66c626084edd827e07e1552279814c6682d"
+dependencies = [
+ "libc",
+ "thiserror 1.0.69",
+ "winapi",
+]
+
+[[package]]
+name = "finl_unicode"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9844ddc3a6e533d62bba727eb6c28b5d360921d5175e9ff0f1e621a5c590a4d5"
+
+[[package]]
+name = "fixedbitset"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
+
+[[package]]
+name = "fnv"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
+
+[[package]]
+name = "foldhash"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
+
+[[package]]
+name = "foldhash"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
+
+[[package]]
+name = "generic-array"
+version = "0.14.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
+dependencies = [
+ "typenum",
+ "version_check",
+]
+
+[[package]]
+name = "getrandom"
+version = "0.3.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "r-efi 5.3.0",
+ "wasip2",
+]
+
+[[package]]
+name = "getrandom"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0de51e6874e94e7bf76d726fc5d13ba782deca734ff60d5bb2fb2607c7406555"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "r-efi 6.0.0",
+ "wasip2",
+ "wasip3",
+]
+
+[[package]]
+name = "hashbrown"
+version = "0.15.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
+dependencies = [
+ "foldhash 0.1.5",
+]
+
+[[package]]
+name = "hashbrown"
+version = "0.16.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
+dependencies = [
+ "allocator-api2",
+ "equivalent",
+ "foldhash 0.2.0",
+]
+
+[[package]]
+name = "heck"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
+
+[[package]]
+name = "herdr"
+version = "0.1.0"
+dependencies = [
+ "base64",
+ "bytes",
+ "crossterm",
+ "portable-pty",
+ "ratatui",
+ "serde",
+ "serde_json",
+ "tokio",
+ "toml",
+ "tracing",
+ "tracing-subscriber",
+ "tui-term",
+ "vt100",
+]
+
+[[package]]
+name = "hex"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
+
+[[package]]
+name = "id-arena"
+version = "2.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954"
+
+[[package]]
+name = "ident_case"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
+
+[[package]]
+name = "indexmap"
+version = "2.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017"
+dependencies = [
+ "equivalent",
+ "hashbrown 0.16.1",
+ "serde",
+ "serde_core",
+]
+
+[[package]]
+name = "indoc"
+version = "2.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706"
+dependencies = [
+ "rustversion",
+]
+
+[[package]]
+name = "instability"
+version = "0.3.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5eb2d60ef19920a3a9193c3e371f726ec1dafc045dac788d0fb3704272458971"
+dependencies = [
+ "darling",
+ "indoc",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "itertools"
+version = "0.14.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
+dependencies = [
+ "either",
+]
+
+[[package]]
+name = "itoa"
+version = "1.0.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
+
+[[package]]
+name = "js-sys"
+version = "0.3.91"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b49715b7073f385ba4bc528e5747d02e66cb39c6146efb66b781f131f0fb399c"
+dependencies = [
+ "once_cell",
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "kasuari"
+version = "0.4.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bde5057d6143cc94e861d90f591b9303d6716c6b9602309150bd068853c10899"
+dependencies = [
+ "hashbrown 0.16.1",
+ "portable-atomic",
+ "thiserror 2.0.18",
+]
+
+[[package]]
+name = "lab"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bf36173d4167ed999940f804952e6b08197cae5ad5d572eb4db150ce8ad5d58f"
+
+[[package]]
+name = "lazy_static"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
+
+[[package]]
+name = "leb128fmt"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2"
+
+[[package]]
+name = "libc"
+version = "0.2.183"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d"
+
+[[package]]
+name = "line-clipping"
+version = "0.3.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5f4de44e98ddbf09375cbf4d17714d18f39195f4f4894e8524501726fd9a8a4a"
+dependencies = [
+ "bitflags 2.11.0",
+]
+
+[[package]]
+name = "linux-raw-sys"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
+
+[[package]]
+name = "litrs"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "11d3d7f243d5c5a8b9bb5d6dd2b1602c0cb0b9db1621bafc7ed66e35ff9fe092"
+
+[[package]]
+name = "lock_api"
+version = "0.4.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965"
+dependencies = [
+ "scopeguard",
+]
+
+[[package]]
+name = "log"
+version = "0.4.29"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
+
+[[package]]
+name = "lru"
+version = "0.16.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1dc47f592c06f33f8e3aea9591776ec7c9f9e4124778ff8a3c3b87159f7e593"
+dependencies = [
+ "hashbrown 0.16.1",
+]
+
+[[package]]
+name = "mac_address"
+version = "1.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c0aeb26bf5e836cc1c341c8106051b573f1766dfa05aa87f0b98be5e51b02303"
+dependencies = [
+ "nix 0.29.0",
+ "winapi",
+]
+
+[[package]]
+name = "matchers"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9"
+dependencies = [
+ "regex-automata",
+]
+
+[[package]]
+name = "memchr"
+version = "2.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
+
+[[package]]
+name = "memmem"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a64a92489e2744ce060c349162be1c5f33c6969234104dbd99ddb5feb08b8c15"
+
+[[package]]
+name = "memoffset"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "minimal-lexical"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
+
+[[package]]
+name = "mio"
+version = "1.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc"
+dependencies = [
+ "libc",
+ "log",
+ "wasi",
+ "windows-sys",
+]
+
+[[package]]
+name = "nix"
+version = "0.28.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4"
+dependencies = [
+ "bitflags 2.11.0",
+ "cfg-if",
+ "cfg_aliases 0.1.1",
+ "libc",
+]
+
+[[package]]
+name = "nix"
+version = "0.29.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
+dependencies = [
+ "bitflags 2.11.0",
+ "cfg-if",
+ "cfg_aliases 0.2.1",
+ "libc",
+ "memoffset",
+]
+
+[[package]]
+name = "nom"
+version = "7.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
+dependencies = [
+ "memchr",
+ "minimal-lexical",
+]
+
+[[package]]
+name = "nu-ansi-term"
+version = "0.50.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
+dependencies = [
+ "windows-sys",
+]
+
+[[package]]
+name = "num-conv"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c6673768db2d862beb9b39a78fdcb1a69439615d5794a1be50caa9bc92c81967"
+
+[[package]]
+name = "num-derive"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "num-traits"
+version = "0.2.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "num_threads"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "once_cell"
+version = "1.21.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
+
+[[package]]
+name = "ordered-float"
+version = "4.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7bb71e1b3fa6ca1c61f383464aaf2bb0e2f8e772a1f01d486832464de363b951"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
+name = "parking_lot"
+version = "0.12.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a"
+dependencies = [
+ "lock_api",
+ "parking_lot_core",
+]
+
+[[package]]
+name = "parking_lot_core"
+version = "0.9.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "redox_syscall",
+ "smallvec",
+ "windows-link",
+]
+
+[[package]]
+name = "pest"
+version = "2.8.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e0848c601009d37dfa3430c4666e147e49cdcf1b92ecd3e63657d8a5f19da662"
+dependencies = [
+ "memchr",
+ "ucd-trie",
+]
+
+[[package]]
+name = "pest_derive"
+version = "2.8.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "11f486f1ea21e6c10ed15d5a7c77165d0ee443402f0780849d1768e7d9d6fe77"
+dependencies = [
+ "pest",
+ "pest_generator",
+]
+
+[[package]]
+name = "pest_generator"
+version = "2.8.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8040c4647b13b210a963c1ed407c1ff4fdfa01c31d6d2a098218702e6664f94f"
+dependencies = [
+ "pest",
+ "pest_meta",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "pest_meta"
+version = "2.8.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "89815c69d36021a140146f26659a81d6c2afa33d216d736dd4be5381a7362220"
+dependencies = [
+ "pest",
+ "sha2",
+]
+
+[[package]]
+name = "phf"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078"
+dependencies = [
+ "phf_macros",
+ "phf_shared",
+]
+
+[[package]]
+name = "phf_codegen"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "aef8048c789fa5e851558d709946d6d79a8ff88c0440c587967f8e94bfb1216a"
+dependencies = [
+ "phf_generator",
+ "phf_shared",
+]
+
+[[package]]
+name = "phf_generator"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d"
+dependencies = [
+ "phf_shared",
+ "rand",
+]
+
+[[package]]
+name = "phf_macros"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f84ac04429c13a7ff43785d75ad27569f2951ce0ffd30a3321230db2fc727216"
+dependencies = [
+ "phf_generator",
+ "phf_shared",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "phf_shared"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5"
+dependencies = [
+ "siphasher",
+]
+
+[[package]]
+name = "pin-project-lite"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
+
+[[package]]
+name = "portable-atomic"
+version = "1.13.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
+
+[[package]]
+name = "portable-pty"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4a596a2b3d2752d94f51fac2d4a96737b8705dddd311a32b9af47211f08671e"
+dependencies = [
+ "anyhow",
+ "bitflags 1.3.2",
+ "downcast-rs",
+ "filedescriptor",
+ "lazy_static",
+ "libc",
+ "log",
+ "nix 0.28.0",
+ "serial2",
+ "shared_library",
+ "shell-words",
+ "winapi",
+ "winreg",
+]
+
+[[package]]
+name = "powerfmt"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
+
+[[package]]
+name = "prettyplease"
+version = "0.2.37"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b"
+dependencies = [
+ "proc-macro2",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.106"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "r-efi"
+version = "5.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
+
+[[package]]
+name = "r-efi"
+version = "6.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
+
+[[package]]
+name = "rand"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
+dependencies = [
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+
+[[package]]
+name = "ratatui"
+version = "0.30.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d1ce67fb8ba4446454d1c8dbaeda0557ff5e94d39d5e5ed7f10a65eb4c8266bc"
+dependencies = [
+ "instability",
+ "ratatui-core",
+ "ratatui-crossterm",
+ "ratatui-macros",
+ "ratatui-termwiz",
+ "ratatui-widgets",
+]
+
+[[package]]
+name = "ratatui-core"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5ef8dea09a92caaf73bff7adb70b76162e5937524058a7e5bff37869cbbec293"
+dependencies = [
+ "bitflags 2.11.0",
+ "compact_str",
+ "hashbrown 0.16.1",
+ "indoc",
+ "itertools",
+ "kasuari",
+ "lru",
+ "strum",
+ "thiserror 2.0.18",
+ "unicode-segmentation",
+ "unicode-truncate",
+ "unicode-width",
+]
+
+[[package]]
+name = "ratatui-crossterm"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "577c9b9f652b4c121fb25c6a391dd06406d3b092ba68827e6d2f09550edc54b3"
+dependencies = [
+ "cfg-if",
+ "crossterm",
+ "instability",
+ "ratatui-core",
+]
+
+[[package]]
+name = "ratatui-macros"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a7f1342a13e83e4bb9d0b793d0ea762be633f9582048c892ae9041ef39c936f4"
+dependencies = [
+ "ratatui-core",
+ "ratatui-widgets",
+]
+
+[[package]]
+name = "ratatui-termwiz"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0f76fe0bd0ed4295f0321b1676732e2454024c15a35d01904ddb315afd3d545c"
+dependencies = [
+ "ratatui-core",
+ "termwiz",
+]
+
+[[package]]
+name = "ratatui-widgets"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d7dbfa023cd4e604c2553483820c5fe8aa9d71a42eea5aa77c6e7f35756612db"
+dependencies = [
+ "bitflags 2.11.0",
+ "hashbrown 0.16.1",
+ "indoc",
+ "instability",
+ "itertools",
+ "line-clipping",
+ "ratatui-core",
+ "strum",
+ "time",
+ "unicode-segmentation",
+ "unicode-width",
+]
+
+[[package]]
+name = "redox_syscall"
+version = "0.5.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
+dependencies = [
+ "bitflags 2.11.0",
+]
+
+[[package]]
+name = "regex"
+version = "1.12.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-automata",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-automata"
+version = "0.4.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.8.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
+
+[[package]]
+name = "rustc_version"
+version = "0.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
+dependencies = [
+ "semver",
+]
+
+[[package]]
+name = "rustix"
+version = "1.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
+dependencies = [
+ "bitflags 2.11.0",
+ "errno",
+ "libc",
+ "linux-raw-sys",
+ "windows-sys",
+]
+
+[[package]]
+name = "rustversion"
+version = "1.0.22"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
+
+[[package]]
+name = "ryu"
+version = "1.0.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
+
+[[package]]
+name = "scopeguard"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
+
+[[package]]
+name = "semver"
+version = "1.0.27"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2"
+
+[[package]]
+name = "serde"
+version = "1.0.228"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
+dependencies = [
+ "serde_core",
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_core"
+version = "1.0.228"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
+dependencies = [
+ "serde_derive",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.228"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "serde_json"
+version = "1.0.149"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
+dependencies = [
+ "itoa",
+ "memchr",
+ "serde",
+ "serde_core",
+ "zmij",
+]
+
+[[package]]
+name = "serde_spanned"
+version = "0.6.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3"
+dependencies = [
+ "serde",
+]
+
+[[package]]
+name = "serial2"
+version = "0.2.34"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9e1401f562d358cdfdbdf8946e51a7871ede1db68bd0fd99bedc79e400241550"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "sha2"
+version = "0.10.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
+dependencies = [
+ "cfg-if",
+ "cpufeatures",
+ "digest",
+]
+
+[[package]]
+name = "sharded-slab"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
+dependencies = [
+ "lazy_static",
+]
+
+[[package]]
+name = "shared_library"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11"
+dependencies = [
+ "lazy_static",
+ "libc",
+]
+
+[[package]]
+name = "shell-words"
+version = "1.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dc6fe69c597f9c37bfeeeeeb33da3530379845f10be461a66d16d03eca2ded77"
+
+[[package]]
+name = "signal-hook"
+version = "0.3.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2"
+dependencies = [
+ "libc",
+ "signal-hook-registry",
+]
+
+[[package]]
+name = "signal-hook-mio"
+version = "0.2.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b75a19a7a740b25bc7944bdee6172368f988763b744e3d4dfe753f6b4ece40cc"
+dependencies = [
+ "libc",
+ "mio",
+ "signal-hook",
+]
+
+[[package]]
+name = "signal-hook-registry"
+version = "1.4.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
+dependencies = [
+ "errno",
+ "libc",
+]
+
+[[package]]
+name = "siphasher"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b2aa850e253778c88a04c3d7323b043aeda9d3e30d5971937c1855769763678e"
+
+[[package]]
+name = "smallvec"
+version = "1.15.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
+
+[[package]]
+name = "static_assertions"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
+
+[[package]]
+name = "strsim"
+version = "0.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
+
+[[package]]
+name = "strum"
+version = "0.27.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "af23d6f6c1a224baef9d3f61e287d2761385a5b88fdab4eb4c6f11aeb54c4bcf"
+dependencies = [
+ "strum_macros",
+]
+
+[[package]]
+name = "strum_macros"
+version = "0.27.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7695ce3845ea4b33927c055a39dc438a45b059f7c1b3d91d38d10355fb8cbca7"
+dependencies = [
+ "heck",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.109"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "syn"
+version = "2.0.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "terminfo"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d4ea810f0692f9f51b382fff5893887bb4580f5fa246fde546e0b13e7fcee662"
+dependencies = [
+ "fnv",
+ "nom",
+ "phf",
+ "phf_codegen",
+]
+
+[[package]]
+name = "termios"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "411c5bf740737c7918b8b1fe232dca4dc9f8e754b8ad5e20966814001ed0ac6b"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "termwiz"
+version = "0.23.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4676b37242ccbd1aabf56edb093a4827dc49086c0ffd764a5705899e0f35f8f7"
+dependencies = [
+ "anyhow",
+ "base64",
+ "bitflags 2.11.0",
+ "fancy-regex",
+ "filedescriptor",
+ "finl_unicode",
+ "fixedbitset",
+ "hex",
+ "lazy_static",
+ "libc",
+ "log",
+ "memmem",
+ "nix 0.29.0",
+ "num-derive",
+ "num-traits",
+ "ordered-float",
+ "pest",
+ "pest_derive",
+ "phf",
+ "sha2",
+ "signal-hook",
+ "siphasher",
+ "terminfo",
+ "termios",
+ "thiserror 1.0.69",
+ "ucd-trie",
+ "unicode-segmentation",
+ "vtparse",
+ "wezterm-bidi",
+ "wezterm-blob-leases",
+ "wezterm-color-types",
+ "wezterm-dynamic",
+ "wezterm-input-types",
+ "winapi",
+]
+
+[[package]]
+name = "thiserror"
+version = "1.0.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
+dependencies = [
+ "thiserror-impl 1.0.69",
+]
+
+[[package]]
+name = "thiserror"
+version = "2.0.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4"
+dependencies = [
+ "thiserror-impl 2.0.18",
+]
+
+[[package]]
+name = "thiserror-impl"
+version = "1.0.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "thiserror-impl"
+version = "2.0.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "thread_local"
+version = "1.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "time"
+version = "0.3.47"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "743bd48c283afc0388f9b8827b976905fb217ad9e647fae3a379a9283c4def2c"
+dependencies = [
+ "deranged",
+ "libc",
+ "num-conv",
+ "num_threads",
+ "powerfmt",
+ "serde_core",
+ "time-core",
+]
+
+[[package]]
+name = "time-core"
+version = "0.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7694e1cfe791f8d31026952abf09c69ca6f6fa4e1a1229e18988f06a04a12dca"
+
+[[package]]
+name = "tokio"
+version = "1.50.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "27ad5e34374e03cfffefc301becb44e9dc3c17584f414349ebe29ed26661822d"
+dependencies = [
+ "pin-project-lite",
+ "tokio-macros",
+]
+
+[[package]]
+name = "tokio-macros"
+version = "2.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c55a2eff8b69ce66c84f85e1da1c233edc36ceb85a2058d11b0d6a3c7e7569c"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "toml"
+version = "0.8.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362"
+dependencies = [
+ "serde",
+ "serde_spanned",
+ "toml_datetime",
+ "toml_edit",
+]
+
+[[package]]
+name = "toml_datetime"
+version = "0.6.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c"
+dependencies = [
+ "serde",
+]
+
+[[package]]
+name = "toml_edit"
+version = "0.22.27"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a"
+dependencies = [
+ "indexmap",
+ "serde",
+ "serde_spanned",
+ "toml_datetime",
+ "toml_write",
+ "winnow",
+]
+
+[[package]]
+name = "toml_write"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801"
+
+[[package]]
+name = "tracing"
+version = "0.1.44"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
+dependencies = [
+ "pin-project-lite",
+ "tracing-attributes",
+ "tracing-core",
+]
+
+[[package]]
+name = "tracing-attributes"
+version = "0.1.31"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
+[[package]]
+name = "tracing-core"
+version = "0.1.36"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
+dependencies = [
+ "once_cell",
+ "valuable",
+]
+
+[[package]]
+name = "tracing-log"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
+dependencies = [
+ "log",
+ "once_cell",
+ "tracing-core",
+]
+
+[[package]]
+name = "tracing-subscriber"
+version = "0.3.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319"
+dependencies = [
+ "matchers",
+ "nu-ansi-term",
+ "once_cell",
+ "regex-automata",
+ "sharded-slab",
+ "smallvec",
+ "thread_local",
+ "tracing",
+ "tracing-core",
+ "tracing-log",
+]
+
+[[package]]
+name = "tui-term"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c40c57cf174390b66d2386cb1288907c65ec389bbf6e99c919c375dc7e08e4d3"
+dependencies = [
+ "ratatui-core",
+ "ratatui-widgets",
+ "vt100",
+]
+
+[[package]]
+name = "typenum"
+version = "1.19.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb"
+
+[[package]]
+name = "ucd-trie"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971"
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
+
+[[package]]
+name = "unicode-segmentation"
+version = "1.13.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "da36089a805484bcccfffe0739803392c8298778a2d2f09febf76fac5ad9025b"
+
+[[package]]
+name = "unicode-truncate"
+version = "2.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "16b380a1238663e5f8a691f9039c73e1cdae598a30e9855f541d29b08b53e9a5"
+dependencies = [
+ "itertools",
+ "unicode-segmentation",
+ "unicode-width",
+]
+
+[[package]]
+name = "unicode-width"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254"
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
+
+[[package]]
+name = "utf8parse"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
+
+[[package]]
+name = "uuid"
+version = "1.22.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a68d3c8f01c0cfa54a75291d83601161799e4a89a39e0929f4b0354d88757a37"
+dependencies = [
+ "atomic",
+ "getrandom 0.4.2",
+ "js-sys",
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "valuable"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
+
+[[package]]
+name = "version_check"
+version = "0.9.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
+
+[[package]]
+name = "vt100"
+version = "0.16.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "054ff75fb8fa83e609e685106df4faeffdf3a735d3c74ebce97ec557d5d36fd9"
+dependencies = [
+ "itoa",
+ "unicode-width",
+ "vte",
+]
+
+[[package]]
+name = "vte"
+version = "0.15.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a5924018406ce0063cd67f8e008104968b74b563ee1b85dde3ed1f7cb87d3dbd"
+dependencies = [
+ "arrayvec",
+ "memchr",
+]
+
+[[package]]
+name = "vtparse"
+version = "0.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6d9b2acfb050df409c972a37d3b8e08cdea3bddb0c09db9d53137e504cfabed0"
+dependencies = [
+ "utf8parse",
+]
+
+[[package]]
+name = "wasi"
+version = "0.11.1+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
+
+[[package]]
+name = "wasip2"
+version = "1.0.2+wasi-0.2.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9517f9239f02c069db75e65f174b3da828fe5f5b945c4dd26bd25d89c03ebcf5"
+dependencies = [
+ "wit-bindgen",
+]
+
+[[package]]
+name = "wasip3"
+version = "0.4.0+wasi-0.3.0-rc-2026-01-06"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5"
+dependencies = [
+ "wit-bindgen",
+]
+
+[[package]]
+name = "wasm-bindgen"
+version = "0.2.114"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6532f9a5c1ece3798cb1c2cfdba640b9b3ba884f5db45973a6f442510a87d38e"
+dependencies = [
+ "cfg-if",
+ "once_cell",
+ "rustversion",
+ "wasm-bindgen-macro",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-macro"
+version = "0.2.114"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "18a2d50fcf105fb33bb15f00e7a77b772945a2ee45dcf454961fd843e74c18e6"
+dependencies = [
+ "quote",
+ "wasm-bindgen-macro-support",
+]
+
+[[package]]
+name = "wasm-bindgen-macro-support"
+version = "0.2.114"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "03ce4caeaac547cdf713d280eda22a730824dd11e6b8c3ca9e42247b25c631e3"
+dependencies = [
+ "bumpalo",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-shared"
+version = "0.2.114"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "75a326b8c223ee17883a4251907455a2431acc2791c98c26279376490c378c16"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "wasm-encoder"
+version = "0.244.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319"
+dependencies = [
+ "leb128fmt",
+ "wasmparser",
+]
+
+[[package]]
+name = "wasm-metadata"
+version = "0.244.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909"
+dependencies = [
+ "anyhow",
+ "indexmap",
+ "wasm-encoder",
+ "wasmparser",
+]
+
+[[package]]
+name = "wasmparser"
+version = "0.244.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe"
+dependencies = [
+ "bitflags 2.11.0",
+ "hashbrown 0.15.5",
+ "indexmap",
+ "semver",
+]
+
+[[package]]
+name = "wezterm-bidi"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0c0a6e355560527dd2d1cf7890652f4f09bb3433b6aadade4c9b5ed76de5f3ec"
+dependencies = [
+ "log",
+ "wezterm-dynamic",
+]
+
+[[package]]
+name = "wezterm-blob-leases"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "692daff6d93d94e29e4114544ef6d5c942a7ed998b37abdc19b17136ea428eb7"
+dependencies = [
+ "getrandom 0.3.4",
+ "mac_address",
+ "sha2",
+ "thiserror 1.0.69",
+ "uuid",
+]
+
+[[package]]
+name = "wezterm-color-types"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7de81ef35c9010270d63772bebef2f2d6d1f2d20a983d27505ac850b8c4b4296"
+dependencies = [
+ "csscolorparser",
+ "deltae",
+ "lazy_static",
+ "wezterm-dynamic",
+]
+
+[[package]]
+name = "wezterm-dynamic"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5f2ab60e120fd6eaa68d9567f3226e876684639d22a4219b313ff69ec0ccd5ac"
+dependencies = [
+ "log",
+ "ordered-float",
+ "strsim",
+ "thiserror 1.0.69",
+ "wezterm-dynamic-derive",
+]
+
+[[package]]
+name = "wezterm-dynamic-derive"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "46c0cf2d539c645b448eaffec9ec494b8b19bd5077d9e58cb1ae7efece8d575b"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 1.0.109",
+]
+
+[[package]]
+name = "wezterm-input-types"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7012add459f951456ec9d6c7e6fc340b1ce15d6fc9629f8c42853412c029e57e"
+dependencies = [
+ "bitflags 1.3.2",
+ "euclid",
+ "lazy_static",
+ "serde",
+ "wezterm-dynamic",
+]
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+
+[[package]]
+name = "windows-link"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
+
+[[package]]
+name = "windows-sys"
+version = "0.61.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
+dependencies = [
+ "windows-link",
+]
+
+[[package]]
+name = "winnow"
+version = "0.7.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "winreg"
+version = "0.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "wit-bindgen"
+version = "0.51.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5"
+dependencies = [
+ "wit-bindgen-rust-macro",
+]
+
+[[package]]
+name = "wit-bindgen-core"
+version = "0.51.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc"
+dependencies = [
+ "anyhow",
+ "heck",
+ "wit-parser",
+]
+
+[[package]]
+name = "wit-bindgen-rust"
+version = "0.51.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21"
+dependencies = [
+ "anyhow",
+ "heck",
+ "indexmap",
+ "prettyplease",
+ "syn 2.0.117",
+ "wasm-metadata",
+ "wit-bindgen-core",
+ "wit-component",
+]
+
+[[package]]
+name = "wit-bindgen-rust-macro"
+version = "0.51.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a"
+dependencies = [
+ "anyhow",
+ "prettyplease",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+ "wit-bindgen-core",
+ "wit-bindgen-rust",
+]
+
+[[package]]
+name = "wit-component"
+version = "0.244.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2"
+dependencies = [
+ "anyhow",
+ "bitflags 2.11.0",
+ "indexmap",
+ "log",
+ "serde",
+ "serde_derive",
+ "serde_json",
+ "wasm-encoder",
+ "wasm-metadata",
+ "wasmparser",
+ "wit-parser",
+]
+
+[[package]]
+name = "wit-parser"
+version = "0.244.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736"
+dependencies = [
+ "anyhow",
+ "id-arena",
+ "indexmap",
+ "log",
+ "semver",
+ "serde",
+ "serde_derive",
+ "serde_json",
+ "unicode-xid",
+ "wasmparser",
+]
+
+[[package]]
+name = "zmij"
+version = "1.0.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 00000000..2fc9ea48
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,26 @@
+[package]
+name = "herdr"
+version = "0.1.0"
+edition = "2021"
+description = "terminal workspace manager for AI coding agents"
+license = "AGPL-3.0-or-later"
+repository = "https://github.com/ogulcancelik/herdr"
+homepage = "https://herdr.dev"
+keywords = ["terminal", "tui", "ai", "agents", "multiplexer"]
+categories = ["command-line-utilities"]
+include = ["src/**/*", "README.md", "LICENSE", "Cargo.toml"]
+
+[dependencies]
+base64 = "0.22.1"
+bytes = "1"
+crossterm = "0.29"
+portable-pty = "0.9"
+ratatui = "0.30"
+serde = { version = "1", features = ["derive"] }
+serde_json = "1"
+tokio = { version = "1", features = ["rt-multi-thread", "macros", "sync", "time"] }
+toml = "0.8"
+tracing = "0.1.44"
+tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
+tui-term = "0.3"
+vt100 = "0.16"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 00000000..be3f7b28
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,661 @@
+ GNU AFFERO GENERAL PUBLIC LICENSE
+ Version 3, 19 November 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU Affero General Public License is a free, copyleft license for
+software and other kinds of works, specifically designed to ensure
+cooperation with the community in the case of network server software.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+our General Public Licenses are intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+ Developers that use our General Public Licenses protect your rights
+with two steps: (1) assert copyright on the software, and (2) offer
+you this License which gives you legal permission to copy, distribute
+and/or modify the software.
+
+ A secondary benefit of defending all users' freedom is that
+improvements made in alternate versions of the program, if they
+receive widespread use, become available for other developers to
+incorporate. Many developers of free software are heartened and
+encouraged by the resulting cooperation. However, in the case of
+software used on network servers, this result may fail to come about.
+The GNU General Public License permits making a modified version and
+letting the public access it on a server without ever releasing its
+source code to the public.
+
+ The GNU Affero General Public License is designed specifically to
+ensure that, in such cases, the modified source code becomes available
+to the community. It requires the operator of a network server to
+provide the source code of the modified version running there to the
+users of that server. Therefore, public use of a modified version, on
+a publicly accessible server, gives the public access to the source
+code of the modified version.
+
+ An older license, called the Affero General Public License and
+published by Affero, was designed to accomplish similar goals. This is
+a different license, not a version of the Affero GPL, but Affero has
+released a new version of the Affero GPL which permits relicensing under
+this license.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ TERMS AND CONDITIONS
+
+ 0. Definitions.
+
+ "This License" refers to version 3 of the GNU Affero General Public License.
+
+ "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+ "The Program" refers to any copyrightable work licensed under this
+License. Each licensee is addressed as "you". "Licensees" and
+"recipients" may be individuals or organizations.
+
+ To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy. The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+ A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+ To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy. Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+ To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+ An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License. If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+ 1. Source Code.
+
+ The "source code" for a work means the preferred form of the work
+for making modifications to it. "Object code" means any non-source
+form of a work.
+
+ A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+ The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form. A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+ The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities. However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+ The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+ The Corresponding Source for a work in source code form is that
+same work.
+
+ 2. Basic Permissions.
+
+ All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met. This License explicitly affirms your unlimited
+permission to run the unmodified Program. The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work. This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+ You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force. You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright. Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+ Conveying under any other circumstances is permitted solely under
+the conditions stated below. Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+ No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+ When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+ 4. Conveying Verbatim Copies.
+
+ You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+ You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+ 5. Conveying Modified Source Versions.
+
+ You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified
+ it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is
+ released under this License and any conditions added under section
+ 7. This requirement modifies the requirement in section 4 to
+ "keep intact all notices".
+
+ c) You must license the entire work, as a whole, under this
+ License to anyone who comes into possession of a copy. This
+ License will therefore apply, along with any applicable section 7
+ additional terms, to the whole of the work, and all its parts,
+ regardless of how they are packaged. This License gives no
+ permission to license the work in any other way, but it does not
+ invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display
+ Appropriate Legal Notices; however, if the Program has interactive
+ interfaces that do not display Appropriate Legal Notices, your
+ work need not make them do so.
+
+ A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+ 6. Conveying Non-Source Forms.
+
+ You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by the
+ Corresponding Source fixed on a durable physical medium
+ customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by a
+ written offer, valid for at least three years and valid for as
+ long as you offer spare parts or customer support for that product
+ model, to give anyone who possesses the object code either (1) a
+ copy of the Corresponding Source for all the software in the
+ product that is covered by this License, on a durable physical
+ medium customarily used for software interchange, for a price no
+ more than your reasonable cost of physically performing this
+ conveying of source, or (2) access to copy the
+ Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the
+ written offer to provide the Corresponding Source. This
+ alternative is allowed only occasionally and noncommercially, and
+ only if you received the object code with such an offer, in accord
+ with subsection 6b.
+
+ d) Convey the object code by offering access from a designated
+ place (gratis or for a charge), and offer equivalent access to the
+ Corresponding Source in the same way through the same place at no
+ further charge. You need not require recipients to copy the
+ Corresponding Source along with the object code. If the place to
+ copy the object code is a network server, the Corresponding Source
+ may be on a different server (operated by you or a third party)
+ that supports equivalent copying facilities, provided you maintain
+ clear directions next to the object code saying where to find the
+ Corresponding Source. Regardless of what server hosts the
+ Corresponding Source, you remain obligated to ensure that it is
+ available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided
+ you inform other peers where the object code and Corresponding
+ Source of the work are being offered to the general public at no
+ charge under subsection 6d.
+
+ A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+ A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling. In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage. For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product. A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+ "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source. The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+ If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information. But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+ The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed. Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+ Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+ 7. Additional Terms.
+
+ "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law. If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+ When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it. (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.) You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+ Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the
+ terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or
+ author attributions in that material or in the Appropriate Legal
+ Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or
+ requiring that modified versions of such material be marked in
+ reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or
+ authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some
+ trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that
+ material by anyone who conveys the material (or modified versions of
+ it) with contractual assumptions of liability to the recipient, for
+ any liability that these contractual assumptions directly impose on
+ those licensors and authors.
+
+ All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10. If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term. If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+ If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+ Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+ 8. Termination.
+
+ You may not propagate or modify a covered work except as expressly
+provided under this License. Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+ However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+ Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+ Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License. If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+ 9. Acceptance Not Required for Having Copies.
+
+ You are not required to accept this License in order to receive or
+run a copy of the Program. Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance. However,
+nothing other than this License grants you permission to propagate or
+modify any covered work. These actions infringe copyright if you do
+not accept this License. Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+ 10. Automatic Licensing of Downstream Recipients.
+
+ Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License. You are not responsible
+for enforcing compliance by third parties with this License.
+
+ An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations. If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+ You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License. For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+ 11. Patents.
+
+ A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The
+work thus licensed is called the contributor's "contributor version".
+
+ A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version. For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+ In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement). To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+ If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients. "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+ If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+ A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License. You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+ Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+ 12. No Surrender of Others' Freedom.
+
+ If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all. For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+ 13. Remote Network Interaction; Use with the GNU General Public License.
+
+ Notwithstanding any other provision of this License, if you modify the
+Program, your modified version must prominently offer all users
+interacting with it remotely through a computer network (if your version
+supports such interaction) an opportunity to receive the Corresponding
+Source of your version by providing access to the Corresponding Source
+from a network server at no charge, through some standard or customary
+means of facilitating copying of software. This Corresponding Source
+shall include the Corresponding Source for any work covered by version 3
+of the GNU General Public License that is incorporated pursuant to the
+following paragraph.
+
+ Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU General Public License into a single
+combined work, and to convey the resulting work. The terms of this
+License will continue to apply to the part which is the covered work,
+but the work with which it is combined will remain governed by version
+3 of the GNU General Public License.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU Affero General Public License from time to time. Such new versions
+will be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Program specifies that a certain numbered version of the GNU Affero General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation. If the Program does not specify a version number of the
+GNU Affero General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+ If the Program specifies that a proxy can decide which future
+versions of the GNU Affero General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+ Later license versions may give you additional or different
+permissions. However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+ 15. Disclaimer of Warranty.
+
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. Limitation of Liability.
+
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+ 17. Interpretation of Sections 15 and 16.
+
+ If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If your software can interact with users remotely through a computer
+network, you should also make sure that it provides a way for users to
+get its source. For example, if your program is a web application, its
+interface could display a "Source" link that leads users to an archive
+of the code. There are many ways you could offer source, and different
+solutions will be better for different programs; see section 13 for the
+specific requirements.
+
+ You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU AGPL, see
+<https://www.gnu.org/licenses/>.
diff --git a/README.md b/README.md
new file mode 100644
index 00000000..2e9e157f
--- /dev/null
+++ b/README.md
@@ -0,0 +1,239 @@
+<p align="center">
+ <img src="assets/logo.png" alt="herdr" width="120" />
+</p>
+
+<h1 align="center">herdr</h1>
+
+<p align="center">herd your agents.</p>
+
+<p align="center">
+ <a href="https://herdr.dev">herdr.dev</a> · <a href="#install">install</a> · <a href="#usage">usage</a>
+</p>
+
+---
+
+herdr is a terminal workspace manager for AI coding agents. it runs inside your existing terminal — ghostty, alacritty, kitty, wezterm, even inside tmux. a single rust binary that gives you workspaces, tiled panes, and intelligent agent state detection.
+
+you keep your terminal. herdr keeps track of your agents.
+
+<p align="center">
+ <img src="assets/screenshot.png" alt="herdr screenshot" width="900" />
+</p>
+
+## philosophy
+
+every tool in this space is building more. desktop apps, electron wrappers, web dashboards — all trying to replace your terminal with their own environment.
+
+herdr takes the opposite approach. it's a tool that lives where you already work. it sits alongside tmux, inside your terminal emulator, part of your existing workflow. it does one thing well: lets you run multiple coding agents in parallel and know when they need you.
+
+the terminal is already a great environment for coding agents. what's been missing is awareness — seeing at a glance which agent is idle, which is working, and which needs your input. herdr adds that layer.
+
+## how it works
+
+herdr embeds real terminal emulators using PTY and vt100 parsing. each pane is a full terminal — your shell, your agent, your tools, exactly as they'd run anywhere else.
+
+the sidebar shows your workspaces. each workspace can have multiple tiled panes. the agent detection system reads terminal output in real time and determines what state each agent is in:
+
+- **●** red — agent is waiting for your input
+- **●** yellow — agent is working
+- **●** blue — agent finished (you haven't looked yet)
+- **○** green — agent is idle, you've seen it
+
+when an agent finishes work in a background workspace, its dot turns blue. you see it at a glance and switch over. it just reads the terminal.
+
+## supported agents
+
+herdr detects agent state by identifying the foreground process and reading terminal output patterns. the following agents have been tested:
+
+| agent | idle | busy | waiting |
+|-------|------|------|---------|
+| [pi](https://pi.dev) | ✓ | ✓ | ✓ |
+| [claude code](https://docs.anthropic.com/en/docs/claude-code) | ✓ | ✓ | ✓ |
+| [codex](https://github.com/openai/codex) | ✓ | ✓ | ✓ |
+| [droid](https://factory.ai) | ✓ | ✓ | ✓ |
+| [amp](https://ampcode.com) | ✓ | ✓ | — |
+| [opencode](https://github.com/anomalyco/opencode) | ✓ | ✓ | ✓ |
+
+detection heuristics also exist for these agents but haven't been fully tested yet. if you use them and run into issues, please [open an issue](https://github.com/ogulcancelik/herdr/issues):
+
+- [gemini cli](https://github.com/google-gemini/gemini-cli)
+- [cursor agent](https://cursor.com/cli)
+- [cline](https://github.com/cline/cline)
+- [kimi](https://kimi.ai)
+- [github copilot cli](https://cli.github.com)
+
+for any other CLI agent, herdr still works as a workspace manager — you get workspaces, panes, and tiling. a hook system for custom agent state reporting is coming soon.
+
+## install
+
+```bash
+curl -fsSL https://herdr.dev/install.sh | sh
+```
+
+or download the binary directly from [releases](https://github.com/ogulcancelik/herdr/releases).
+
+requirements: linux or macos.
+
+### update
+
+herdr checks for updates automatically in the background. when a new version is ready, you'll see a notification in the UI — just restart to apply. you can also update manually:
+
+```bash
+herdr update
+```
+
+## usage
+
+launch herdr:
+
+```bash
+herdr
+```
+
+herdr starts in **navigate mode**. press `n` to create your first workspace, type a name, press enter. you're in **terminal mode** with a shell ready.
+
+press `ctrl+s` (the prefix key) to switch back to navigate mode. from there you can manage workspaces and panes.
+
+### navigate mode (prefix: ctrl+s)
+
+| key | action | configurable |
+|-----|--------|:---:|
+| `n` | new workspace | |
+| `N` | rename workspace | |
+| `d` | close workspace | |
+| `1`-`9` | switch to workspace by number | |
+| `↑` `↓` | select workspace | |
+| `enter` | open selected workspace | |
+| `v` | split pane vertically | ✓ |
+| `-` | split pane horizontally | ✓ |
+| `h` `j` `k` `l` | navigate between panes | |
+| `tab` | cycle panes | |
+| `f` | toggle fullscreen | ✓ |
+| `x` | close pane | ✓ |
+| `r` | enter resize mode | |
+| `b` | toggle sidebar collapse | |
+| `q` | quit | |
+
+keys marked ✓ can be changed in `~/.config/herdr/config.toml` under `[keys]`. the prefix key is also configurable.
+
+### resize mode
+
+| key | action |
+|-----|--------|
+| `h` `l` | resize width |
+| `j` `k` | resize height |
+| `esc` | exit resize mode |
+
+### mouse
+
+- click a workspace in the sidebar to switch
+- click a pane to focus it
+- drag split borders to resize
+- right-click a workspace for context menu
+- scroll in sidebar to navigate workspaces
+- click `«` / `»` at the sidebar bottom to collapse/expand
+
+### terminal mode
+
+you're in a real terminal. everything works — your shell, vim, htop, ssh, anything. press the prefix key (`ctrl+s`) to go back to navigate mode.
+
+## configuration
+
+config file: `~/.config/herdr/config.toml`
+
+generate the default config with all options:
+
+```bash
+herdr --default-config
+```
+
+```toml
+[keys]
+# prefix key to enter navigate mode
+prefix = "ctrl+s"
+
+# pane controls (in navigate mode)
+split_vertical = "v"
+split_horizontal = "-"
+close_pane = "x"
+fullscreen = "f"
+
+[ui]
+# accent color: hex (#89b4fa), named (cyan, blue), or rgb(r,g,b)
+accent = "cyan"
+
+# ask for confirmation before closing a workspace
+confirm_close = true
+```
+
+### environment variables
+
+| variable | description |
+|----------|-------------|
+| `HERDR_LOG` | log level filter (default: `herdr=info`) |
+
+logs: `~/.config/herdr/herdr.log`
+
+## session persistence
+
+herdr saves your workspace layout, pane working directories, and focused pane on exit. when you restart, everything is restored. sessions are stored at `~/.config/herdr/session.json`.
+
+use `--no-session` to start fresh.
+
+## how agent detection works
+
+herdr doesn't require hooks or agent-side configuration. detection works by:
+
+1. identifying the foreground process of each pane's PTY (via `/proc` on linux, `proc_pidinfo` on macos)
+2. matching the process name against known agents
+3. reading terminal screen content and applying per-agent heuristics to determine state
+
+this means detection works with any supported agent, installed any way, with zero setup. if it runs in a terminal, herdr can see it.
+
+the heuristics are pattern-matched against each agent's actual terminal output — prompt boxes, spinners, "waiting for input" messages, tool execution indicators. detection runs on a separate async task per pane, polled every 300-500ms, decoupled from terminal rendering.
+
+## what's coming
+
+- **notification hooks** — a socket API so any agent or script can report its state to herdr. for agents without built-in detection, wire up a simple hook.
+- **native notifications** — OS-level notifications when an agent needs attention and herdr isn't in focus.
+- **agent API** — `herdr create`, `herdr split`, `herdr send` — so agents and scripts can manage herdr workspaces programmatically.
+
+## built with agents
+
+i had never written rust before starting this project. herdr was built almost entirely through AI coding agents — the same ones it's designed to manage. i supervised the architecture and specs, agents wrote the code.
+
+this is a proof of concept in more ways than one. it's a functional tool, but it's also a statement about what's possible right now. if you can build a terminal multiplexer in a language you don't know, by directing the same agents the tool is built for — that says something about where we are.
+
+there will be rough edges. if you hit one, [open an issue](https://github.com/ogulcancelik/herdr/issues). that's why it's open source.
+
+## cli
+
+```
+herdr launch herdr
+herdr update download and install the latest version
+herdr --version print version
+herdr --default-config print default configuration
+herdr --no-session start without restoring or saving sessions
+herdr --help show help
+```
+
+## building from source
+
+```bash
+git clone https://github.com/ogulcancelik/herdr
+cd herdr
+cargo build --release
+./target/release/herdr
+```
+
+## testing
+
+```bash
+just test # unit tests (157 tests)
+just test-integration # LLM-based integration tests
+just test-all # both
+```
+
+## license
+
+AGPL-3.0 — free to use, modify, and distribute. if you distribute a modified version, you must open-source your changes under the same license.
diff --git a/assets/logo.png b/assets/logo.png
new file mode 100644
index 00000000..9c2e60c1
--- /dev/null
+++ b/assets/logo.png
Binary files differ
diff --git a/assets/screenshot.png b/assets/screenshot.png
new file mode 100644
index 00000000..b1731264
--- /dev/null
+++ b/assets/screenshot.png
Binary files differ
diff --git a/justfile b/justfile
new file mode 100644
index 00000000..3700b040
--- /dev/null
+++ b/justfile
@@ -0,0 +1,29 @@
+# herdr task runner
+
+# Run unit tests
+test:
+ cargo test
+
+# Run integration tests (LLM-based, requires pi + tmux)
+test-integration:
+ ./tests/integration/run_all.sh
+
+# Run all tests
+test-all: test test-integration
+
+# Build release binary
+build:
+ cargo build --release
+
+# Kill any leftover test tmux sessions and clean results
+clean-tests:
+ @for sock in ${TMPDIR:-/tmp}/herdr-test-sockets/*/tmux.sock; do \
+ [ -S "$$sock" ] && tmux -S "$$sock" kill-server 2>/dev/null || true; \
+ done
+ @rm -rf ${TMPDIR:-/tmp}/herdr-test-sockets 2>/dev/null || true
+ @rm -f tests/integration/results/*.json tests/integration/results/*.txt 2>/dev/null || true
+ @echo "cleaned"
+
+# Print default config
+default-config:
+ cargo run --release -- --default-config
diff --git a/src/app/actions.rs b/src/app/actions.rs
new file mode 100644
index 00000000..8e2e9297
--- /dev/null
+++ b/src/app/actions.rs
@@ -0,0 +1,417 @@
+//! Pure state mutations on AppState.
+//! These don't need channels, async, or PTY runtime.
+
+use tracing::{info, warn};
+
+use crate::detect::AgentState;
+use crate::events::AppEvent;
+use crate::layout::{find_in_direction, NavDirection, PaneId};
+
+use super::state::{AppState, Mode};
+
+// ---------------------------------------------------------------------------
+// Workspace operations
+// ---------------------------------------------------------------------------
+
+impl AppState {
+ pub fn switch_workspace(&mut self, idx: usize) {
+ if idx < self.workspaces.len() {
+ self.active = Some(idx);
+ self.selected = idx;
+ for pane in self.workspaces[idx].panes.values_mut() {
+ pane.seen = true;
+ }
+ }
+ }
+
+ pub fn close_selected_workspace(&mut self) {
+ if self.workspaces.is_empty() {
+ return;
+ }
+ let name = self.workspaces[self.selected].name.clone();
+ info!(workspace = %name, "workspace closed");
+ self.workspaces.remove(self.selected);
+ if self.workspaces.is_empty() {
+ self.active = None;
+ self.selected = 0;
+ } else {
+ if self.selected >= self.workspaces.len() {
+ self.selected = self.workspaces.len() - 1;
+ }
+ self.active = Some(self.selected);
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Pane operations
+// ---------------------------------------------------------------------------
+
+impl AppState {
+ pub fn navigate_pane(&mut self, direction: NavDirection) {
+ let panes = &self.view.pane_infos;
+ if let Some(focused) = panes.iter().find(|p| p.is_focused) {
+ if let Some(target) = find_in_direction(focused, direction, panes) {
+ if let Some(ws) = self.active.and_then(|i| self.workspaces.get_mut(i)) {
+ ws.layout.focus_pane(target);
+ }
+ }
+ }
+ }
+
+ pub fn resize_pane(&mut self, direction: NavDirection) {
+ if let Some(first) = self.view.pane_infos.first() {
+ let area = self
+ .view.pane_infos
+ .iter()
+ .fold(first.rect, |acc, p| acc.union(p.rect));
+ if let Some(ws) = self.active.and_then(|i| self.workspaces.get_mut(i)) {
+ ws.layout.resize_focused(direction, 0.05, area);
+ }
+ }
+ }
+
+ pub fn cycle_pane(&mut self, reverse: bool) {
+ if let Some(ws) = self.active.and_then(|i| self.workspaces.get_mut(i)) {
+ if reverse {
+ ws.layout.focus_prev();
+ } else {
+ ws.layout.focus_next();
+ }
+ }
+ }
+
+ pub fn toggle_fullscreen(&mut self) {
+ if let Some(ws) = self.active.and_then(|i| self.workspaces.get_mut(i)) {
+ if ws.layout.pane_count() > 1 {
+ ws.zoomed = !ws.zoomed;
+ }
+ }
+ }
+
+ pub fn close_pane(&mut self) {
+ if let Some(ws) = self.active.and_then(|i| self.workspaces.get_mut(i)) {
+ ws.close_focused();
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Selection
+// ---------------------------------------------------------------------------
+
+impl AppState {
+ pub fn clear_selection(&mut self) {
+ self.selection = None;
+ }
+
+ pub fn copy_selection(&mut self) {
+ let sel = match self.selection.as_mut() {
+ Some(s) => {
+ if !s.finish() {
+ self.selection = None;
+ return;
+ }
+ s
+ }
+ None => return,
+ };
+
+ let ws = match self.active.and_then(|i| self.workspaces.get(i)) {
+ Some(ws) => ws,
+ None => return,
+ };
+
+ let rt = match ws.runtimes.get(&sel.pane_id) {
+ Some(r) => r,
+ None => return,
+ };
+
+ if let Ok(parser) = rt.parser.read() {
+ let text = crate::selection::extract_text(parser.screen(), sel);
+ if !text.is_empty() {
+ crate::selection::write_osc52(&text);
+ info!(len = text.len(), "copied selection to clipboard");
+ }
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Event handling
+// ---------------------------------------------------------------------------
+
+impl AppState {
+ pub fn handle_app_event(&mut self, event: AppEvent) {
+ match event {
+ AppEvent::PaneDied { pane_id } => self.handle_pane_died(pane_id),
+ AppEvent::UpdateReady { version } => {
+ self.update_available = Some(version);
+ self.update_dismissed = false;
+ }
+ AppEvent::StateChanged {
+ pane_id,
+ agent,
+ state,
+ } => {
+ for (ws_idx, ws) in self.workspaces.iter_mut().enumerate() {
+ if let Some(pane) = ws.panes.get_mut(&pane_id) {
+ let is_active_ws = self.active == Some(ws_idx);
+ if state == AgentState::Idle
+ && pane.state != AgentState::Idle
+ && !is_active_ws
+ {
+ pane.seen = false;
+ }
+ pane.detected_agent = agent;
+ pane.state = state;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ fn handle_pane_died(&mut self, pane_id: PaneId) {
+ let ws_idx = self
+ .workspaces
+ .iter()
+ .position(|ws| ws.panes.contains_key(&pane_id));
+
+ let Some(ws_idx) = ws_idx else {
+ warn!(pane = pane_id.raw(), "PaneDied for unknown pane");
+ return;
+ };
+
+ let ws = &mut self.workspaces[ws_idx];
+
+ if ws.layout.pane_count() <= 1 {
+ self.workspaces.remove(ws_idx);
+ if self.workspaces.is_empty() {
+ self.active = None;
+ self.selected = 0;
+ if self.mode == Mode::Terminal {
+ self.mode = Mode::Navigate;
+ }
+ } else {
+ if let Some(active) = self.active {
+ if active >= self.workspaces.len() {
+ self.active = Some(self.workspaces.len() - 1);
+ }
+ }
+ if self.selected >= self.workspaces.len() {
+ self.selected = self.workspaces.len() - 1;
+ }
+ }
+ } else {
+ if ws.layout.focused() == pane_id {
+ ws.layout.close_focused();
+ } else {
+ let prev_focus = ws.layout.focused();
+ ws.layout.focus_pane(pane_id);
+ ws.layout.close_focused();
+ ws.layout.focus_pane(prev_focus);
+ }
+ ws.panes.remove(&pane_id);
+ ws.runtimes.remove(&pane_id);
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Tests
+// ---------------------------------------------------------------------------
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::detect::{Agent, AgentState};
+ use crate::workspace::Workspace;
+ use ratatui::layout::Direction;
+
+ fn app_with_workspaces(names: &[&str]) -> AppState {
+ let mut state = AppState::test_new();
+ for name in names {
+ let ws = Workspace::test_new(name);
+ state.workspaces.push(ws);
+ }
+ if !state.workspaces.is_empty() {
+ state.active = Some(0);
+ state.mode = Mode::Terminal;
+ }
+ state
+ }
+
+ #[test]
+ fn switch_workspace_updates_active_and_selected() {
+ let mut state = app_with_workspaces(&["a", "b", "c"]);
+ state.switch_workspace(2);
+ assert_eq!(state.active, Some(2));
+ assert_eq!(state.selected, 2);
+ }
+
+ #[test]
+ fn switch_workspace_marks_panes_seen() {
+ let mut state = app_with_workspaces(&["a", "b"]);
+ // Mark a pane in workspace 1 as unseen
+ let id = *state.workspaces[1].panes.keys().next().unwrap();
+ state.workspaces[1].panes.get_mut(&id).unwrap().seen = false;
+
+ state.switch_workspace(1);
+ assert!(state.workspaces[1].panes.get(&id).unwrap().seen);
+ }
+
+ #[test]
+ fn switch_workspace_out_of_bounds_is_noop() {
+ let mut state = app_with_workspaces(&["a"]);
+ state.switch_workspace(5);
+ assert_eq!(state.active, Some(0));
+ }
+
+ #[test]
+ fn close_workspace_adjusts_indices() {
+ let mut state = app_with_workspaces(&["a", "b", "c"]);
+ state.selected = 1;
+ state.active = Some(1);
+
+ state.close_selected_workspace();
+
+ assert_eq!(state.workspaces.len(), 2);
+ assert_eq!(state.selected, 1);
+ assert_eq!(state.active, Some(1));
+ assert_eq!(state.workspaces[1].name, "c");
+ }
+
+ #[test]
+ fn close_last_workspace_clears_active() {
+ let mut state = app_with_workspaces(&["only"]);
+ state.selected = 0;
+ state.close_selected_workspace();
+
+ assert!(state.workspaces.is_empty());
+ assert_eq!(state.active, None);
+ assert_eq!(state.selected, 0);
+ }
+
+ #[test]
+ fn close_workspace_at_end_adjusts_selected() {
+ let mut state = app_with_workspaces(&["a", "b"]);
+ state.selected = 1;
+ state.active = Some(1);
+
+ state.close_selected_workspace();
+
+ assert_eq!(state.workspaces.len(), 1);
+ assert_eq!(state.selected, 0);
+ assert_eq!(state.active, Some(0));
+ }
+
+ #[test]
+ fn pane_died_last_pane_removes_workspace() {
+ let mut state = app_with_workspaces(&["a", "b"]);
+ let pane_id = *state.workspaces[0].panes.keys().next().unwrap();
+
+ state.handle_pane_died(pane_id);
+
+ assert_eq!(state.workspaces.len(), 1);
+ assert_eq!(state.workspaces[0].name, "b");
+ }
+
+ #[test]
+ fn pane_died_last_workspace_enters_navigate() {
+ let mut state = app_with_workspaces(&["only"]);
+ state.mode = Mode::Terminal;
+ let pane_id = *state.workspaces[0].panes.keys().next().unwrap();
+
+ state.handle_pane_died(pane_id);
+
+ assert!(state.workspaces.is_empty());
+ assert_eq!(state.mode, Mode::Navigate);
+ }
+
+ #[test]
+ fn pane_died_multi_pane_keeps_workspace() {
+ let mut state = app_with_workspaces(&["test"]);
+ let second_id = state.workspaces[0].test_split(Direction::Horizontal);
+
+ state.handle_pane_died(second_id);
+
+ assert_eq!(state.workspaces.len(), 1);
+ assert_eq!(state.workspaces[0].panes.len(), 1);
+ }
+
+ #[test]
+ fn pane_died_unknown_pane_is_noop() {
+ let mut state = app_with_workspaces(&["test"]);
+ let fake_id = PaneId::from_raw(9999);
+
+ state.handle_pane_died(fake_id);
+
+ assert_eq!(state.workspaces.len(), 1);
+ }
+
+ #[test]
+ fn state_changed_updates_pane() {
+ let mut state = app_with_workspaces(&["test"]);
+ let pane_id = *state.workspaces[0].panes.keys().next().unwrap();
+
+ state.handle_app_event(AppEvent::StateChanged {
+ pane_id,
+ agent: Some(Agent::Pi),
+ state: AgentState::Busy,
+ });
+
+ let pane = state.workspaces[0].panes.get(&pane_id).unwrap();
+ assert_eq!(pane.state, AgentState::Busy);
+ assert_eq!(pane.detected_agent, Some(Agent::Pi));
+ }
+
+ #[test]
+ fn state_changed_idle_in_background_marks_unseen() {
+ let mut state = app_with_workspaces(&["active", "background"]);
+ state.active = Some(0);
+ let bg_pane_id = *state.workspaces[1].panes.keys().next().unwrap();
+
+ // First set it to Busy
+ state.workspaces[1].panes.get_mut(&bg_pane_id).unwrap().state = AgentState::Busy;
+
+ // Now transition to Idle while in background
+ state.handle_app_event(AppEvent::StateChanged {
+ pane_id: bg_pane_id,
+ agent: Some(Agent::Pi),
+ state: AgentState::Idle,
+ });
+
+ let pane = state.workspaces[1].panes.get(&bg_pane_id).unwrap();
+ assert!(!pane.seen);
+ }
+
+ #[test]
+ fn toggle_fullscreen_works() {
+ let mut state = app_with_workspaces(&["test"]);
+ state.workspaces[0].test_split(Direction::Horizontal);
+
+ assert!(!state.workspaces[0].zoomed);
+ state.toggle_fullscreen();
+ assert!(state.workspaces[0].zoomed);
+ state.toggle_fullscreen();
+ assert!(!state.workspaces[0].zoomed);
+ }
+
+ #[test]
+ fn toggle_fullscreen_single_pane_noop() {
+ let mut state = app_with_workspaces(&["test"]);
+ state.toggle_fullscreen();
+ assert!(!state.workspaces[0].zoomed);
+ }
+
+ #[test]
+ fn close_pane_removes_from_workspace() {
+ let mut state = app_with_workspaces(&["test"]);
+ state.workspaces[0].test_split(Direction::Horizontal);
+ assert_eq!(state.workspaces[0].panes.len(), 2);
+
+ state.close_pane();
+ assert_eq!(state.workspaces[0].panes.len(), 1);
+ }
+}
diff --git a/src/app/input.rs b/src/app/input.rs
new file mode 100644
index 00000000..b9a52bda
--- /dev/null
+++ b/src/app/input.rs
@@ -0,0 +1,562 @@
+//! Input handling — translates crossterm key/mouse events into state mutations.
+
+use bytes::Bytes;
+use crossterm::event::{KeyCode, KeyEvent, MouseButton, MouseEvent, MouseEventKind};
+use ratatui::layout::Direction;
+use tracing::warn;
+
+use crate::layout::{NavDirection, PaneInfo, SplitBorder};
+use crate::selection::Selection;
+
+use super::state::{AppState, ContextMenuState, DragState, Mode, CONTEXT_MENU_ITEMS};
+use super::App;
+
+// ---------------------------------------------------------------------------
+// Key handling
+// ---------------------------------------------------------------------------
+
+impl App {
+ pub(super) async fn handle_key(&mut self, key: KeyEvent) {
+ match self.state.mode {
+ Mode::Navigate => handle_navigate_key(&mut self.state, key),
+ Mode::Terminal => self.handle_terminal_key(key).await,
+ Mode::CreateSession => handle_create_key(self, key),
+ Mode::RenameSession => handle_rename_key(&mut self.state, key),
+ Mode::Resize => handle_resize_key(&mut self.state, key),
+ Mode::ConfirmClose => handle_confirm_close_key(&mut self.state, key),
+ Mode::ContextMenu => handle_context_menu_key(&mut self.state, key),
+ }
+ }
+
+ pub(super) async fn handle_paste(&mut self, text: String) {
+ if self.state.mode != Mode::Terminal {
+ return;
+ }
+ if let Some(ws) = self.state.active.and_then(|i| self.state.workspaces.get(i)) {
+ if let Some(rt) = ws.focused_runtime() {
+ let bracketed = rt
+ .parser
+ .read()
+ .map(|p| p.screen().bracketed_paste())
+ .unwrap_or(false);
+
+ let payload = if bracketed {
+ format!("\x1b[200~{text}\x1b[201~")
+ } else {
+ text
+ };
+ let _ = rt.sender.send(Bytes::from(payload)).await;
+ }
+ }
+ }
+
+ async fn handle_terminal_key(&mut self, key: KeyEvent) {
+ self.state.clear_selection();
+ self.state.update_dismissed = true;
+
+ if self.state.is_prefix(&key) {
+ self.state.mode = Mode::Navigate;
+ return;
+ }
+
+ if let Some(ws) = self.state.active.and_then(|i| self.state.workspaces.get(i)) {
+ if let Some(rt) = ws.focused_runtime() {
+ rt.scroll_reset();
+ let kitty = rt.kitty_keyboard.load(std::sync::atomic::Ordering::Relaxed);
+ let bytes = crate::input::encode_key(key, kitty);
+ if bytes.is_empty() {
+ warn!(code = ?key.code, mods = ?key.modifiers, state = ?key.state, "key produced empty encoding");
+ } else {
+ let _ = rt.sender.send(Bytes::from(bytes)).await;
+ }
+ }
+ }
+ }
+}
+
+fn handle_navigate_key(state: &mut AppState, key: KeyEvent) {
+ state.update_dismissed = true;
+
+ if state.is_prefix(&key) || key.code == KeyCode::Esc {
+ if state.active.is_some() {
+ state.mode = Mode::Terminal;
+ }
+ return;
+ }
+
+ // Configurable keybinds (checked before fixed binds)
+ let kb = &state.keybinds;
+ if key.code == kb.split_vertical.0 && key.modifiers.contains(kb.split_vertical.1) {
+ state.split_pane(Direction::Horizontal);
+ return;
+ }
+ if key.code == kb.split_horizontal.0 && key.modifiers.contains(kb.split_horizontal.1) {
+ state.split_pane(Direction::Vertical);
+ return;
+ }
+ if key.code == kb.close_pane.0 && key.modifiers.contains(kb.close_pane.1) {
+ state.close_pane();
+ return;
+ }
+ if key.code == kb.fullscreen.0 && key.modifiers.contains(kb.fullscreen.1) {
+ state.toggle_fullscreen();
+ return;
+ }
+
+ match key.code {
+ KeyCode::Char('q') => state.should_quit = true,
+ KeyCode::Char('n') => {
+ state.mode = Mode::CreateSession;
+ state.name_input.clear();
+ }
+ KeyCode::Char('N') => {
+ if !state.workspaces.is_empty() {
+ state.name_input = state.workspaces[state.selected].name.clone();
+ state.mode = Mode::RenameSession;
+ }
+ }
+ KeyCode::Char('d') => {
+ if !state.workspaces.is_empty() {
+ if state.confirm_close {
+ state.mode = Mode::ConfirmClose;
+ } else {
+ state.close_selected_workspace();
+ if state.workspaces.is_empty() {
+ state.mode = Mode::Navigate;
+ } else {
+ state.mode = Mode::Terminal;
+ }
+ }
+ }
+ }
+ KeyCode::Up => {
+ if state.selected > 0 {
+ state.selected -= 1;
+ }
+ }
+ KeyCode::Down => {
+ if !state.workspaces.is_empty() && state.selected < state.workspaces.len() - 1 {
+ state.selected += 1;
+ }
+ }
+ KeyCode::Enter => {
+ if !state.workspaces.is_empty() {
+ state.switch_workspace(state.selected);
+ state.mode = Mode::Terminal;
+ }
+ }
+ KeyCode::Char('h') | KeyCode::Left => state.navigate_pane(NavDirection::Left),
+ KeyCode::Char('j') => state.navigate_pane(NavDirection::Down),
+ KeyCode::Char('k') => state.navigate_pane(NavDirection::Up),
+ KeyCode::Char('l') | KeyCode::Right => state.navigate_pane(NavDirection::Right),
+ KeyCode::Tab => state.cycle_pane(false),
+ KeyCode::BackTab => state.cycle_pane(true),
+ KeyCode::Char('r') => state.mode = Mode::Resize,
+ KeyCode::Char('b') => state.sidebar_collapsed = !state.sidebar_collapsed,
+ KeyCode::Char(c @ '1'..='9') => {
+ let idx = (c as usize) - ('1' as usize);
+ if idx < state.workspaces.len() {
+ state.switch_workspace(idx);
+ state.mode = Mode::Terminal;
+ }
+ }
+ _ => {}
+ }
+}
+
+fn handle_create_key(app: &mut App, key: KeyEvent) {
+ match key.code {
+ KeyCode::Enter => {
+ let name = if app.state.name_input.trim().is_empty() {
+ format!("workspace-{}", app.state.workspaces.len() + 1)
+ } else {
+ app.state.name_input.trim().to_string()
+ };
+ app.state.name_input.clear();
+ app.create_workspace(name);
+ }
+ KeyCode::Esc => {
+ app.state.name_input.clear();
+ app.state.mode = Mode::Navigate;
+ }
+ KeyCode::Backspace => {
+ app.state.name_input.pop();
+ }
+ KeyCode::Char(c) => {
+ app.state.name_input.push(c);
+ }
+ _ => {}
+ }
+}
+
+fn handle_rename_key(state: &mut AppState, key: KeyEvent) {
+ match key.code {
+ KeyCode::Enter => {
+ let new_name = if state.name_input.trim().is_empty() {
+ state.name_input.clone()
+ } else {
+ state.name_input.trim().to_string()
+ };
+ if !new_name.is_empty() && !state.workspaces.is_empty() {
+ state.workspaces[state.selected].name = new_name;
+ }
+ state.name_input.clear();
+ state.mode = Mode::Navigate;
+ }
+ KeyCode::Esc => {
+ state.name_input.clear();
+ state.mode = Mode::Navigate;
+ }
+ KeyCode::Backspace => {
+ state.name_input.pop();
+ }
+ KeyCode::Char(c) => {
+ state.name_input.push(c);
+ }
+ _ => {}
+ }
+}
+
+fn handle_resize_key(state: &mut AppState, key: KeyEvent) {
+ match key.code {
+ KeyCode::Esc | KeyCode::Enter | KeyCode::Char('r') => {
+ if state.active.is_some() {
+ state.mode = Mode::Terminal;
+ } else {
+ state.mode = Mode::Navigate;
+ }
+ }
+ KeyCode::Char('h') | KeyCode::Left => state.resize_pane(NavDirection::Left),
+ KeyCode::Char('l') | KeyCode::Right => state.resize_pane(NavDirection::Right),
+ KeyCode::Char('j') | KeyCode::Down => state.resize_pane(NavDirection::Down),
+ KeyCode::Char('k') | KeyCode::Up => state.resize_pane(NavDirection::Up),
+ _ => {}
+ }
+}
+
+fn handle_confirm_close_key(state: &mut AppState, key: KeyEvent) {
+ match key.code {
+ KeyCode::Char('y') | KeyCode::Enter => {
+ state.close_selected_workspace();
+ if state.workspaces.is_empty() {
+ state.mode = Mode::Navigate;
+ } else {
+ state.mode = Mode::Terminal;
+ }
+ }
+ _ => {
+ state.mode = Mode::Navigate;
+ }
+ }
+}
+
+fn handle_context_menu_key(state: &mut AppState, key: KeyEvent) {
+ match key.code {
+ KeyCode::Esc => {
+ state.context_menu = None;
+ state.mode = Mode::Navigate;
+ }
+ KeyCode::Up => {
+ if let Some(menu) = &mut state.context_menu {
+ if menu.selected > 0 {
+ menu.selected -= 1;
+ }
+ }
+ }
+ KeyCode::Down => {
+ if let Some(menu) = &mut state.context_menu {
+ if menu.selected < CONTEXT_MENU_ITEMS.len() - 1 {
+ menu.selected += 1;
+ }
+ }
+ }
+ KeyCode::Enter => {
+ if let Some(menu) = state.context_menu.take() {
+ match CONTEXT_MENU_ITEMS[menu.selected] {
+ "Rename" => {
+ state.selected = menu.ws_idx;
+ state.name_input = state.workspaces[menu.ws_idx].name.clone();
+ state.mode = Mode::RenameSession;
+ }
+ "Close" => {
+ state.selected = menu.ws_idx;
+ if state.confirm_close {
+ state.mode = Mode::ConfirmClose;
+ } else {
+ state.close_selected_workspace();
+ state.mode = Mode::Navigate;
+ }
+ }
+ _ => state.mode = Mode::Navigate,
+ }
+ }
+ }
+ _ => {}
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Mouse handling
+// ---------------------------------------------------------------------------
+
+impl AppState {
+ pub(crate) fn handle_mouse(&mut self, mouse: MouseEvent) {
+ let sidebar = self.view.sidebar_rect;
+ let in_sidebar = mouse.column >= sidebar.x
+ && mouse.column < sidebar.x + sidebar.width
+ && mouse.row >= sidebar.y
+ && mouse.row < sidebar.y + sidebar.height;
+
+ match mouse.kind {
+ MouseEventKind::Down(MouseButton::Left) => {
+ self.selection = None;
+
+ if self.mode == Mode::ContextMenu {
+ if let Some(menu) = &self.context_menu {
+ let item_idx = self.context_menu_item_at(mouse.column, mouse.row);
+ if let Some(idx) = item_idx {
+ let ws_idx = menu.ws_idx;
+ self.context_menu = None;
+ match CONTEXT_MENU_ITEMS[idx] {
+ "Rename" => {
+ self.selected = ws_idx;
+ self.name_input = self.workspaces[ws_idx].name.clone();
+ self.mode = Mode::RenameSession;
+ }
+ "Close" => {
+ self.selected = ws_idx;
+ if self.confirm_close {
+ self.mode = Mode::ConfirmClose;
+ } else {
+ self.close_selected_workspace();
+ self.mode = Mode::Navigate;
+ }
+ }
+ _ => self.mode = Mode::Navigate,
+ }
+ } else {
+ self.context_menu = None;
+ self.mode = Mode::Navigate;
+ }
+ }
+ return;
+ }
+
+ if !in_sidebar {
+ if let Some(border) = self.find_border_at(mouse.column, mouse.row) {
+ self.drag = Some(DragState {
+ path: border.path.clone(),
+ direction: border.direction,
+ area: border.area,
+ });
+ return;
+ }
+ }
+
+ if in_sidebar {
+ let bottom_y = sidebar.y + sidebar.height.saturating_sub(1);
+ if mouse.row == bottom_y {
+ self.sidebar_collapsed = !self.sidebar_collapsed;
+ return;
+ }
+
+ let inner_y = sidebar.y + 1;
+ if mouse.row >= inner_y {
+ let idx = (mouse.row - inner_y) as usize;
+ if idx < self.workspaces.len() {
+ self.switch_workspace(idx);
+ self.mode = Mode::Terminal;
+ }
+ }
+ } else if let Some(info) = self.pane_at(mouse.column, mouse.row).cloned() {
+ let (row, col) = (
+ mouse.row - info.inner_rect.y,
+ mouse.column - info.inner_rect.x,
+ );
+ self.selection = Some(Selection::anchor(
+ info.id,
+ row,
+ col,
+ info.inner_rect,
+ ));
+
+ if let Some(ws) = self.active.and_then(|i| self.workspaces.get_mut(i)) {
+ if ws.layout.focused() != info.id {
+ ws.layout.focus_pane(info.id);
+ }
+ }
+ if self.mode != Mode::Terminal {
+ self.mode = Mode::Terminal;
+ }
+ } else {
+ if let Some(info) = self.view.pane_infos.iter().find(|p| {
+ mouse.column >= p.rect.x
+ && mouse.column < p.rect.x + p.rect.width
+ && mouse.row >= p.rect.y
+ && mouse.row < p.rect.y + p.rect.height
+ }) {
+ let id = info.id;
+ if let Some(ws) = self.active.and_then(|i| self.workspaces.get_mut(i)) {
+ if ws.layout.focused() != id {
+ ws.layout.focus_pane(id);
+ }
+ }
+ if self.mode != Mode::Terminal {
+ self.mode = Mode::Terminal;
+ }
+ }
+ }
+ }
+
+ MouseEventKind::Drag(MouseButton::Left) => {
+ if let Some(drag) = &self.drag {
+ let ratio = match drag.direction {
+ Direction::Horizontal => {
+ (mouse.column.saturating_sub(drag.area.x)) as f32
+ / drag.area.width.max(1) as f32
+ }
+ Direction::Vertical => {
+ (mouse.row.saturating_sub(drag.area.y)) as f32
+ / drag.area.height.max(1) as f32
+ }
+ };
+ let ratio = ratio.clamp(0.1, 0.9);
+ let path = drag.path.clone();
+ if let Some(ws) = self.active.and_then(|i| self.workspaces.get_mut(i)) {
+ ws.layout.set_ratio_at(&path, ratio);
+ }
+ } else if let Some(sel) = &mut self.selection {
+ sel.drag(mouse.column, mouse.row);
+ }
+ }
+
+ MouseEventKind::Up(MouseButton::Left) => {
+ if self.drag.take().is_some() {
+ // Drag ended
+ } else {
+ let was_click = self
+ .selection
+ .as_ref()
+ .is_some_and(|s| s.was_just_click());
+ if was_click {
+ self.selection = None;
+ } else {
+ self.copy_selection();
+ }
+ }
+ }
+
+ MouseEventKind::ScrollUp if !in_sidebar => {
+ self.selection = None;
+ if let Some(ws) = self.active.and_then(|i| self.workspaces.get(i)) {
+ if let Some(rt) = ws.focused_runtime() {
+ rt.scroll_up(3);
+ }
+ }
+ }
+ MouseEventKind::ScrollDown if !in_sidebar => {
+ self.selection = None;
+ if let Some(ws) = self.active.and_then(|i| self.workspaces.get(i)) {
+ if let Some(rt) = ws.focused_runtime() {
+ rt.scroll_down(3);
+ }
+ }
+ }
+
+ MouseEventKind::ScrollUp if in_sidebar => {
+ if self.selected > 0 {
+ self.selected -= 1;
+ }
+ }
+ MouseEventKind::ScrollDown if in_sidebar => {
+ if !self.workspaces.is_empty() && self.selected < self.workspaces.len() - 1 {
+ self.selected += 1;
+ }
+ }
+
+ MouseEventKind::Down(MouseButton::Right) if in_sidebar => {
+ let inner_y = sidebar.y + 1;
+ if mouse.row >= inner_y {
+ let idx = (mouse.row - inner_y) as usize;
+ if idx < self.workspaces.len() {
+ self.selected = idx;
+ self.context_menu = Some(ContextMenuState {
+ ws_idx: idx,
+ x: mouse.column,
+ y: mouse.row,
+ selected: 0,
+ });
+ self.mode = Mode::ContextMenu;
+ }
+ }
+ }
+
+ _ => {}
+ }
+ }
+
+ fn context_menu_item_at(&self, col: u16, row: u16) -> Option<usize> {
+ let menu = self.context_menu.as_ref()?;
+ let menu_w = 14u16;
+ let inner_x = menu.x;
+ let inner_y = menu.y + 1;
+ if col >= inner_x
+ && col < inner_x + menu_w
+ && row >= inner_y
+ && row < inner_y + CONTEXT_MENU_ITEMS.len() as u16
+ {
+ Some((row - inner_y) as usize)
+ } else {
+ None
+ }
+ }
+
+ fn find_border_at(&self, col: u16, row: u16) -> Option<&SplitBorder> {
+ self.view.split_borders.iter().find(|b| match b.direction {
+ Direction::Horizontal => {
+ (col as i32 - b.pos as i32).unsigned_abs() <= 1
+ && row >= b.area.y
+ && row < b.area.y + b.area.height
+ }
+ Direction::Vertical => {
+ (row as i32 - b.pos as i32).unsigned_abs() <= 1
+ && col >= b.area.x
+ && col < b.area.x + b.area.width
+ }
+ })
+ }
+
+ fn pane_at(&self, col: u16, row: u16) -> Option<&PaneInfo> {
+ self.view.pane_infos.iter().find(|p| {
+ col >= p.inner_rect.x
+ && col < p.inner_rect.x + p.inner_rect.width
+ && row >= p.inner_rect.y
+ && row < p.inner_rect.y + p.inner_rect.height
+ })
+ }
+}
+
+// Note: split_pane needs runtime (event_tx for PTY spawn), so it lives on App
+impl AppState {
+ pub(crate) fn split_pane(&mut self, direction: Direction) {
+ // Actual PTY spawning happens in Workspace::split_focused
+ // which needs events channel — this is called from navigate_key
+ // where we don't have async context, so the workspace handles it
+ let (rows, cols) = self.estimate_pane_size();
+ let new_rows = (rows / 2).max(4);
+ let new_cols = (cols / 2).max(10);
+
+ let cwd = self
+ .active
+ .and_then(|i| self.workspaces.get(i))
+ .and_then(|ws| ws.focused_runtime())
+ .and_then(|rt| rt.cwd());
+
+ if let Some(ws) = self.active.and_then(|i| self.workspaces.get_mut(i)) {
+ if let Ok(new_id) = ws.split_focused(direction, new_rows, new_cols, cwd) {
+ ws.layout.focus_pane(new_id);
+ self.mode = Mode::Terminal;
+ }
+ }
+ }
+}
diff --git a/src/app/mod.rs b/src/app/mod.rs
new file mode 100644
index 00000000..62250fb8
--- /dev/null
+++ b/src/app/mod.rs
@@ -0,0 +1,159 @@
+//! Application orchestration.
+//!
+//! - `state.rs` — AppState, Mode, and pure data structs
+//! - `actions.rs` — state mutations (testable without PTYs/async)
+//! - `input.rs` — key/mouse → action translation
+
+mod actions;
+mod input;
+pub mod state;
+
+use std::io;
+use std::time::Duration;
+
+use crossterm::event::{self, Event, KeyEventKind};
+use ratatui::layout::Rect;
+use ratatui::DefaultTerminal;
+use tokio::sync::mpsc;
+use tracing::{error, info};
+
+use crate::config::Config;
+use crate::events::AppEvent;
+use crate::workspace::Workspace;
+
+pub use state::{AppState, Mode, ViewState, CONTEXT_MENU_ITEMS};
+
+/// Full application: AppState + runtime concerns (event channels, async I/O).
+pub struct App {
+ pub state: AppState,
+ pub event_tx: mpsc::Sender<AppEvent>,
+ event_rx: mpsc::Receiver<AppEvent>,
+ no_session: bool,
+}
+
+impl App {
+ pub fn new(config: &Config, no_session: bool) -> Self {
+ let (prefix_code, prefix_mods) = config.prefix_key();
+ let (event_tx, event_rx) = mpsc::channel::<AppEvent>(64);
+
+ // Try to restore previous session
+ let (workspaces, active, selected) = if no_session {
+ (Vec::new(), None, 0)
+ } else if let Some(snap) = crate::persist::load() {
+ let ws = crate::persist::restore(&snap, 24, 80, event_tx.clone());
+ if ws.is_empty() {
+ info!("session file found but no workspaces restored");
+ (Vec::new(), None, 0)
+ } else {
+ info!(count = ws.len(), "session restored");
+ let active = snap.active.filter(|&i| i < ws.len());
+ let selected = snap.selected.min(ws.len().saturating_sub(1));
+ (ws, active, selected)
+ }
+ } else {
+ (Vec::new(), None, 0)
+ };
+
+ let mode = if active.is_some() {
+ state::Mode::Terminal
+ } else {
+ state::Mode::Navigate
+ };
+
+ let state = AppState {
+ workspaces,
+ active,
+ selected,
+ mode,
+ should_quit: false,
+ name_input: String::new(),
+ view: state::ViewState {
+ sidebar_rect: Rect::default(),
+ terminal_area: Rect::default(),
+ pane_infos: Vec::new(),
+ split_borders: Vec::new(),
+ },
+ drag: None,
+ selection: None,
+ context_menu: None,
+ update_available: None,
+ update_dismissed: false,
+ prefix_code,
+ prefix_mods,
+ prefix_label: config.prefix_label(),
+ sidebar_width: config.ui.sidebar_width,
+ sidebar_collapsed: false,
+ confirm_close: config.ui.confirm_close,
+ accent: crate::config::parse_color(&config.ui.accent),
+ keybinds: config.keybinds(),
+ };
+
+ // Background auto-update (skipped in --no-session / test mode)
+ if !no_session {
+ let update_tx = event_tx.clone();
+ std::thread::spawn(move || crate::update::auto_update(update_tx));
+ }
+
+ Self {
+ state,
+ event_tx,
+ event_rx,
+ no_session,
+ }
+ }
+
+ pub async fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
+ while !self.state.should_quit {
+ terminal.draw(|frame| {
+ crate::ui::compute_view(&mut self.state, frame.area());
+ crate::ui::render(&self.state, frame);
+ })?;
+
+ // Drain internal events
+ while let Ok(ev) = self.event_rx.try_recv() {
+ self.state.handle_app_event(ev);
+ }
+
+ if event::poll(Duration::from_millis(16))? {
+ match event::read()? {
+ Event::Key(key) if key.kind == KeyEventKind::Press => {
+ self.handle_key(key).await;
+ }
+ Event::Paste(text) => self.handle_paste(text).await,
+ Event::Mouse(mouse) => self.state.handle_mouse(mouse),
+ Event::Resize(_, _) => {}
+ _ => {}
+ }
+ }
+ }
+
+ // Save session on exit (skip in --no-session mode)
+ if !self.no_session && !self.state.workspaces.is_empty() {
+ let snap = crate::persist::capture(
+ &self.state.workspaces,
+ self.state.active,
+ self.state.selected,
+ );
+ crate::persist::save(&snap);
+ }
+
+ Ok(())
+ }
+
+ /// Create a workspace with a real PTY (needs event_tx).
+ fn create_workspace(&mut self, name: String) {
+ let (rows, cols) = self.state.estimate_pane_size();
+ match Workspace::new(name, rows, cols, self.event_tx.clone()) {
+ Ok(ws) => {
+ self.state.workspaces.push(ws);
+ let idx = self.state.workspaces.len() - 1;
+ self.state.switch_workspace(idx);
+ self.state.mode = Mode::Terminal;
+ }
+ Err(e) => {
+ error!(err = %e, "failed to create workspace");
+ self.state.mode = Mode::Navigate;
+ }
+ }
+ }
+}
diff --git a/src/app/state.rs b/src/app/state.rs
new file mode 100644
index 00000000..b0a81165
--- /dev/null
+++ b/src/app/state.rs
@@ -0,0 +1,130 @@
+use crossterm::event::{KeyCode, KeyModifiers};
+use crate::config::Keybinds;
+use ratatui::layout::{Direction, Rect};
+use ratatui::style::Color;
+
+use crate::layout::{PaneInfo, SplitBorder};
+use crate::selection::Selection;
+use crate::workspace::Workspace;
+
+/// Computed view geometry — derived from AppState + terminal size.
+/// Updated before each render, consumed by render and mouse handling.
+pub struct ViewState {
+ pub sidebar_rect: Rect,
+ pub terminal_area: Rect,
+ pub pane_infos: Vec<PaneInfo>,
+ pub split_borders: Vec<SplitBorder>,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum Mode {
+ Navigate,
+ Terminal,
+ CreateSession,
+ RenameSession,
+ Resize,
+ ConfirmClose,
+ ContextMenu,
+}
+
+/// Active mouse drag on a split border.
+pub(crate) struct DragState {
+ pub path: Vec<bool>,
+ pub direction: Direction,
+ pub area: Rect,
+}
+
+/// Right-click context menu state.
+pub struct ContextMenuState {
+ pub ws_idx: usize,
+ pub x: u16,
+ pub y: u16,
+ pub selected: usize,
+}
+
+pub const CONTEXT_MENU_ITEMS: &[&str] = &["Rename", "Close"];
+
+/// All application state — pure data, no channels or async runtime.
+/// Testable without PTYs or a tokio runtime.
+pub struct AppState {
+ pub workspaces: Vec<Workspace>,
+ pub active: Option<usize>,
+ pub selected: usize,
+ pub mode: Mode,
+ pub should_quit: bool,
+ pub name_input: String,
+ // View geometry (computed before render, consumed by render + mouse)
+ pub view: ViewState,
+ pub(crate) drag: Option<DragState>,
+ pub selection: Option<Selection>,
+ pub context_menu: Option<ContextMenuState>,
+ // Update notification
+ pub update_available: Option<String>,
+ pub update_dismissed: bool,
+ // Config
+ pub prefix_code: KeyCode,
+ pub prefix_mods: KeyModifiers,
+ pub prefix_label: String,
+ pub sidebar_width: u16,
+ pub sidebar_collapsed: bool,
+ pub confirm_close: bool,
+ pub accent: Color,
+ pub keybinds: Keybinds,
+}
+
+impl AppState {
+ pub fn is_prefix(&self, key: &crossterm::event::KeyEvent) -> bool {
+ key.code == self.prefix_code && key.modifiers.contains(self.prefix_mods)
+ }
+
+ pub fn estimate_pane_size(&self) -> (u16, u16) {
+ if let Some(info) = self.view.pane_infos.first() {
+ (info.rect.height, info.rect.width)
+ } else {
+ (24, 80)
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Test helpers
+// ---------------------------------------------------------------------------
+
+#[cfg(test)]
+impl AppState {
+ /// Create an AppState for testing — no channels, no PTYs.
+ pub fn test_new() -> Self {
+ Self {
+ workspaces: Vec::new(),
+ active: None,
+ selected: 0,
+ mode: Mode::Navigate,
+ should_quit: false,
+ name_input: String::new(),
+ view: ViewState {
+ sidebar_rect: Rect::default(),
+ terminal_area: Rect::default(),
+ pane_infos: Vec::new(),
+ split_borders: Vec::new(),
+ },
+ drag: None,
+ selection: None,
+ context_menu: None,
+ update_available: None,
+ update_dismissed: false,
+ prefix_code: KeyCode::Char('s'),
+ prefix_mods: KeyModifiers::CONTROL,
+ prefix_label: "ctrl+s".into(),
+ sidebar_width: 26,
+ sidebar_collapsed: false,
+ confirm_close: true,
+ accent: Color::Cyan,
+ keybinds: Keybinds {
+ split_vertical: (KeyCode::Char('v'), KeyModifiers::empty()),
+ split_horizontal: (KeyCode::Char('-'), KeyModifiers::empty()),
+ close_pane: (KeyCode::Char('x'), KeyModifiers::empty()),
+ fullscreen: (KeyCode::Char('f'), KeyModifiers::empty()),
+ },
+ }
+ }
+}
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 00000000..0022d230
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,279 @@
+use std::path::PathBuf;
+
+use crossterm::event::{KeyCode, KeyModifiers};
+use serde::Deserialize;
+use tracing::warn;
+
+#[derive(Debug, Default, Deserialize)]
+#[serde(default)]
+pub struct Config {
+ pub keys: KeysConfig,
+ pub ui: UiConfig,
+}
+
+#[derive(Debug, Deserialize)]
+#[serde(default)]
+pub struct KeysConfig {
+ /// Prefix key to toggle navigate mode (e.g. "ctrl+s", "ctrl+a", "ctrl+b").
+ pub prefix: String,
+ /// Split pane vertically (side by side). Default: "v"
+ pub split_vertical: String,
+ /// Split pane horizontally (stacked). Default: "-"
+ pub split_horizontal: String,
+ /// Close the focused pane. Default: "x"
+ pub close_pane: String,
+ /// Toggle fullscreen for the focused pane. Default: "f"
+ pub fullscreen: String,
+}
+
+#[derive(Debug, Deserialize)]
+#[serde(default)]
+pub struct UiConfig {
+ pub sidebar_width: u16,
+ /// Ask for confirmation before closing a workspace. Default: true.
+ pub confirm_close: bool,
+ /// Accent color for highlights, borders, and navigation UI.
+ /// Accepts hex (#89b4fa), named colors (cyan, blue), or RGB (rgb(137,180,250)).
+ pub accent: String,
+}
+
+impl Default for KeysConfig {
+ fn default() -> Self {
+ Self {
+ prefix: "ctrl+s".into(),
+ split_vertical: "v".into(),
+ split_horizontal: "-".into(),
+ close_pane: "x".into(),
+ fullscreen: "f".into(),
+ }
+ }
+}
+
+impl Default for UiConfig {
+ fn default() -> Self {
+ Self {
+ sidebar_width: 26,
+ confirm_close: true,
+ accent: "cyan".into(),
+ }
+ }
+}
+
+impl Config {
+ pub fn load() -> Self {
+ let path = config_path();
+ if path.exists() {
+ match std::fs::read_to_string(&path) {
+ Ok(content) => match toml::from_str(&content) {
+ Ok(config) => return config,
+ Err(e) => warn!(err = %e, "config parse error, using defaults"),
+ },
+ Err(e) => warn!(err = %e, "config read error, using defaults"),
+ }
+ }
+ Self::default()
+ }
+
+ pub fn prefix_key(&self) -> (KeyCode, KeyModifiers) {
+ parse_key_combo(&self.keys.prefix)
+ }
+
+ /// Human-readable label for the prefix key (shown in status bar).
+ pub fn prefix_label(&self) -> String {
+ self.keys.prefix.clone()
+ }
+
+ /// Parsed keybinds for navigate mode actions.
+ pub fn keybinds(&self) -> Keybinds {
+ Keybinds {
+ split_vertical: parse_key_combo(&self.keys.split_vertical),
+ split_horizontal: parse_key_combo(&self.keys.split_horizontal),
+ close_pane: parse_key_combo(&self.keys.close_pane),
+ fullscreen: parse_key_combo(&self.keys.fullscreen),
+ }
+ }
+}
+
+/// Parsed keybinds for navigate mode actions.
+#[derive(Debug, Clone)]
+pub struct Keybinds {
+ pub split_vertical: (KeyCode, KeyModifiers),
+ pub split_horizontal: (KeyCode, KeyModifiers),
+ pub close_pane: (KeyCode, KeyModifiers),
+ pub fullscreen: (KeyCode, KeyModifiers),
+}
+
+/// Parse a color string into a ratatui Color.
+/// Supports: hex (#rrggbb, #rgb), named colors, rgb(r,g,b).
+pub fn parse_color(s: &str) -> ratatui::style::Color {
+ use ratatui::style::Color;
+ let s = s.trim().to_lowercase();
+
+ // Hex: #rrggbb or #rgb
+ if let Some(hex) = s.strip_prefix('#') {
+ if hex.len() == 6 {
+ if let (Ok(r), Ok(g), Ok(b)) = (
+ u8::from_str_radix(&hex[0..2], 16),
+ u8::from_str_radix(&hex[2..4], 16),
+ u8::from_str_radix(&hex[4..6], 16),
+ ) {
+ return Color::Rgb(r, g, b);
+ }
+ } else if hex.len() == 3 {
+ let chars: Vec<u8> = hex
+ .chars()
+ .filter_map(|c| u8::from_str_radix(&c.to_string(), 16).ok())
+ .collect();
+ if chars.len() == 3 {
+ return Color::Rgb(chars[0] * 17, chars[1] * 17, chars[2] * 17);
+ }
+ }
+ }
+
+ // rgb(r, g, b)
+ if let Some(inner) = s.strip_prefix("rgb(").and_then(|s| s.strip_suffix(')')) {
+ let parts: Vec<&str> = inner.split(',').collect();
+ if parts.len() == 3 {
+ if let (Ok(r), Ok(g), Ok(b)) = (
+ parts[0].trim().parse::<u8>(),
+ parts[1].trim().parse::<u8>(),
+ parts[2].trim().parse::<u8>(),
+ ) {
+ return Color::Rgb(r, g, b);
+ }
+ }
+ }
+
+ // Named colors
+ match s.as_str() {
+ "black" => Color::Black,
+ "red" => Color::Red,
+ "green" => Color::Green,
+ "yellow" => Color::Yellow,
+ "blue" => Color::Blue,
+ "magenta" | "purple" => Color::Magenta,
+ "cyan" => Color::Cyan,
+ "white" => Color::White,
+ "gray" | "grey" => Color::Gray,
+ "darkgray" | "darkgrey" => Color::DarkGray,
+ "lightred" => Color::LightRed,
+ "lightgreen" => Color::LightGreen,
+ "lightyellow" => Color::LightYellow,
+ "lightblue" => Color::LightBlue,
+ "lightmagenta" => Color::LightMagenta,
+ "lightcyan" => Color::LightCyan,
+ _ => {
+ warn!(color = s, "unknown color, defaulting to cyan");
+ Color::Cyan
+ }
+ }
+}
+
+fn config_path() -> PathBuf {
+ if let Ok(dir) = std::env::var("XDG_CONFIG_HOME") {
+ PathBuf::from(dir).join("herdr/config.toml")
+ } else if let Ok(home) = std::env::var("HOME") {
+ PathBuf::from(home).join(".config/herdr/config.toml")
+ } else {
+ PathBuf::from("/tmp/herdr/config.toml")
+ }
+}
+
+fn parse_key_combo(s: &str) -> (KeyCode, KeyModifiers) {
+ let parts: Vec<&str> = s.split('+').collect();
+ let mut modifiers = KeyModifiers::empty();
+ let mut key_str = "";
+
+ for part in &parts {
+ match part.trim().to_lowercase().as_str() {
+ "ctrl" | "control" => modifiers |= KeyModifiers::CONTROL,
+ "shift" => modifiers |= KeyModifiers::SHIFT,
+ "alt" | "meta" => modifiers |= KeyModifiers::ALT,
+ _ => key_str = part.trim(),
+ }
+ }
+
+ let code = match key_str.to_lowercase().as_str() {
+ "space" | " " => KeyCode::Char(' '),
+ "enter" | "return" => KeyCode::Enter,
+ "esc" | "escape" => KeyCode::Esc,
+ "tab" => KeyCode::Tab,
+ "backspace" | "bs" => KeyCode::Backspace,
+ s if s.len() == 1 => KeyCode::Char(s.chars().next().unwrap()),
+ s if s.starts_with('f') => s[1..]
+ .parse::<u8>()
+ .map(KeyCode::F)
+ .unwrap_or(KeyCode::Char('s')),
+ _ => KeyCode::Char('s'),
+ };
+
+ (code, modifiers)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crossterm::event::{KeyCode, KeyModifiers};
+
+ #[test]
+ fn parse_simple_char() {
+ assert_eq!(parse_key_combo("v"), (KeyCode::Char('v'), KeyModifiers::empty()));
+ }
+
+ #[test]
+ fn parse_ctrl_combo() {
+ assert_eq!(parse_key_combo("ctrl+s"), (KeyCode::Char('s'), KeyModifiers::CONTROL));
+ }
+
+ #[test]
+ fn parse_special_key() {
+ assert_eq!(parse_key_combo("enter"), (KeyCode::Enter, KeyModifiers::empty()));
+ assert_eq!(parse_key_combo("tab"), (KeyCode::Tab, KeyModifiers::empty()));
+ assert_eq!(parse_key_combo("esc"), (KeyCode::Esc, KeyModifiers::empty()));
+ }
+
+ #[test]
+ fn parse_ctrl_shift() {
+ assert_eq!(
+ parse_key_combo("ctrl+shift+a"),
+ (KeyCode::Char('a'), KeyModifiers::CONTROL | KeyModifiers::SHIFT)
+ );
+ }
+
+ #[test]
+ fn parse_f_key() {
+ assert_eq!(parse_key_combo("f5"), (KeyCode::F(5), KeyModifiers::empty()));
+ }
+
+ #[test]
+ fn default_keybinds_parse() {
+ let config = Config::default();
+ let kb = config.keybinds();
+ assert_eq!(kb.split_vertical.0, KeyCode::Char('v'));
+ assert_eq!(kb.split_horizontal.0, KeyCode::Char('-'));
+ assert_eq!(kb.close_pane.0, KeyCode::Char('x'));
+ assert_eq!(kb.fullscreen.0, KeyCode::Char('f'));
+ }
+
+ #[test]
+ fn custom_keybinds_from_toml() {
+ let toml = r#"
+[keys]
+prefix = "ctrl+a"
+split_vertical = "s"
+split_horizontal = "shift+s"
+close_pane = "ctrl+w"
+fullscreen = "z"
+"#;
+ let config: Config = toml::from_str(toml).unwrap();
+ let (code, mods) = config.prefix_key();
+ assert_eq!(code, KeyCode::Char('a'));
+ assert_eq!(mods, KeyModifiers::CONTROL);
+
+ let kb = config.keybinds();
+ assert_eq!(kb.split_vertical.0, KeyCode::Char('s'));
+ assert_eq!(kb.split_horizontal, (KeyCode::Char('s'), KeyModifiers::SHIFT));
+ assert_eq!(kb.close_pane, (KeyCode::Char('w'), KeyModifiers::CONTROL));
+ assert_eq!(kb.fullscreen.0, KeyCode::Char('z'));
+ }
+}
diff --git a/src/detect.rs b/src/detect.rs
new file mode 100644
index 00000000..235a1603
--- /dev/null
+++ b/src/detect.rs
@@ -0,0 +1,1163 @@
+//! Agent state detection via screen content pattern matching.
+//!
+//! Each pane's `vt100::Screen` content is read periodically and matched
+//! against known agent output patterns to determine state.
+
+/// The detected state of a terminal pane.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum AgentState {
+ /// Agent finished, prompt visible, nothing happening.
+ Idle,
+ /// Agent is actively working/processing.
+ Busy,
+ /// Agent needs human input — approval, question, error.
+ Waiting,
+ /// Plain shell or unrecognized program.
+ Unknown,
+}
+
+impl AgentState {
+ /// Priority for rolling up pane states to a workspace indicator.
+ /// Higher = more urgent.
+ pub fn priority(self) -> u8 {
+ match self {
+ Self::Unknown => 0,
+ Self::Idle => 1,
+ Self::Busy => 2,
+ Self::Waiting => 3,
+ }
+ }
+}
+
+/// Which agent we detected running in a pane.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum Agent {
+ Pi,
+ Claude,
+ Codex,
+ Gemini,
+ Cursor,
+ Cline,
+ OpenCode,
+ GithubCopilot,
+ Kimi,
+ Droid,
+ Amp,
+}
+
+/// Identify which agent is running from the process name.
+/// Returns `None` for plain shells or unrecognized programs.
+pub fn identify_agent(process_name: &str) -> Option<Agent> {
+ let name = process_name.to_lowercase();
+ // Match against known binary names
+ match name.as_str() {
+ "pi" => Some(Agent::Pi),
+ "claude" | "claude-code" => Some(Agent::Claude),
+ "codex" => Some(Agent::Codex),
+ "gemini" => Some(Agent::Gemini),
+ "cursor" => Some(Agent::Cursor),
+ "cline" => Some(Agent::Cline),
+ "opencode" | "open-code" => Some(Agent::OpenCode),
+ "github-copilot" | "ghcs" => Some(Agent::GithubCopilot),
+ "kimi" => Some(Agent::Kimi),
+ "droid" => Some(Agent::Droid),
+ "amp" | "amp-local" => Some(Agent::Amp),
+ _ => None,
+ }
+}
+
+/// Detect the state of an agent from the visible screen content.
+/// If `agent` is `None`, returns `Unknown`.
+pub fn detect_state(agent: Option<Agent>, screen_content: &str) -> AgentState {
+ let Some(agent) = agent else {
+ return AgentState::Unknown;
+ };
+ match agent {
+ Agent::Pi => detect_pi(screen_content),
+ Agent::Claude => detect_claude(screen_content),
+ Agent::Codex => detect_codex(screen_content),
+ Agent::Gemini => detect_gemini(screen_content),
+ Agent::Cursor => detect_cursor(screen_content),
+ Agent::Cline => detect_cline(screen_content),
+ Agent::OpenCode => detect_opencode(screen_content),
+ Agent::GithubCopilot => detect_github_copilot(screen_content),
+ Agent::Kimi => detect_kimi(screen_content),
+ Agent::Droid => detect_droid(screen_content),
+ Agent::Amp => detect_amp(screen_content),
+ }
+}
+
+/// Roll up multiple pane states into a single workspace state.
+/// Returns the highest-priority state among all panes.
+pub fn workspace_state(pane_states: &[AgentState]) -> AgentState {
+ pane_states
+ .iter()
+ .max_by_key(|s| s.priority())
+ .copied()
+ .unwrap_or(AgentState::Unknown)
+}
+
+// ---------------------------------------------------------------------------
+// Per-agent detectors
+// ---------------------------------------------------------------------------
+
+fn detect_pi(content: &str) -> AgentState {
+ // pi shows "Working..." when the agent is processing
+ if content.contains("Working...") {
+ return AgentState::Busy;
+ }
+ AgentState::Idle
+}
+
+/// Claude Code detection. The most complex — it has a structured prompt box UI.
+///
+/// Screen layout:
+/// ```text
+/// (agent output / tool results)
+/// ───────────────────────── (top border)
+/// ❯ _ (prompt line)
+/// ───────────────────────── (bottom border)
+/// ```
+fn detect_claude(content: &str) -> AgentState {
+ let lower = content.to_lowercase();
+
+ // Search prompt is always idle
+ if content.contains("⌕ Search…") {
+ return AgentState::Idle;
+ }
+
+ // ctrl+r toggle — don't change state
+ // (we return Idle as a safe default since we don't have previous state here)
+ if lower.contains("ctrl+r to toggle") {
+ return AgentState::Idle;
+ }
+
+ // --- Waiting detection (full content including prompt box) ---
+
+ // "Do you want" or "Would you like" followed by yes/❯
+ if has_confirmation_prompt(&lower) {
+ return AgentState::Waiting;
+ }
+
+ // Selection prompt: ❯ followed by numbered option
+ if has_selection_prompt(content) {
+ return AgentState::Waiting;
+ }
+
+ // "esc to cancel" indicates permission/confirmation dialog
+ if lower.contains("esc to cancel") {
+ return AgentState::Waiting;
+ }
+
+ // --- Busy detection (content above the prompt box) ---
+
+ let above = content_above_prompt_box(content);
+ let above_lower = above.to_lowercase();
+
+ if above_lower.contains("esc to interrupt") || above_lower.contains("ctrl+c to interrupt") {
+ return AgentState::Busy;
+ }
+
+ if has_spinner_activity(above) {
+ return AgentState::Busy;
+ }
+
+ AgentState::Idle
+}
+
+fn detect_codex(content: &str) -> AgentState {
+ let lower = content.to_lowercase();
+
+ // Waiting patterns
+ if lower.contains("press enter to confirm or esc to cancel")
+ || lower.contains("| enter to submit answer")
+ || lower.contains("allow command?")
+ || lower.contains("[y/n]")
+ || lower.contains("yes (y)")
+ {
+ return AgentState::Waiting;
+ }
+ if has_confirmation_prompt(&lower) {
+ return AgentState::Waiting;
+ }
+
+ // Busy
+ if has_interrupt_pattern(&lower) {
+ return AgentState::Busy;
+ }
+
+ AgentState::Idle
+}
+
+fn detect_gemini(content: &str) -> AgentState {
+ let lower = content.to_lowercase();
+
+ // Waiting — explicit confirmation
+ if lower.contains("waiting for user confirmation") {
+ return AgentState::Waiting;
+ }
+
+ // Waiting — box-drawing confirmation prompts
+ if content.contains("│ Apply this change")
+ || content.contains("│ Allow execution")
+ || content.contains("│ Do you want to proceed")
+ {
+ return AgentState::Waiting;
+ }
+ if has_confirmation_prompt(&lower) {
+ return AgentState::Waiting;
+ }
+
+ // Busy
+ if lower.contains("esc to cancel") {
+ return AgentState::Busy;
+ }
+
+ AgentState::Idle
+}
+
+fn detect_cursor(content: &str) -> AgentState {
+ let lower = content.to_lowercase();
+
+ // Waiting
+ if lower.contains("(y) (enter)")
+ || lower.contains("keep (n)")
+ || lower.contains("skip (esc or n)")
+ {
+ return AgentState::Waiting;
+ }
+ // "allow ...(y)" or "run ...(y)" patterns
+ if lower.contains("(y)") && (lower.contains("allow") || lower.contains("run")) {
+ return AgentState::Waiting;
+ }
+
+ // Busy
+ if lower.contains("ctrl+c to stop") {
+ return AgentState::Busy;
+ }
+ if has_cursor_spinner(content) {
+ return AgentState::Busy;
+ }
+
+ AgentState::Idle
+}
+
+fn detect_cline(content: &str) -> AgentState {
+ let lower = content.to_lowercase();
+
+ // Waiting
+ if lower.contains("let cline use this tool") {
+ return AgentState::Waiting;
+ }
+ // [act mode] or [plan mode] followed by "yes"
+ if (lower.contains("[act mode]") || lower.contains("[plan mode]"))
+ && lower.contains("yes")
+ {
+ return AgentState::Waiting;
+ }
+
+ // Idle
+ if lower.contains("cline is ready for your message") {
+ return AgentState::Idle;
+ }
+
+ // Cline defaults to busy (unlike most agents that default to idle)
+ AgentState::Busy
+}
+
+fn detect_opencode(content: &str) -> AgentState {
+ // Waiting
+ if content.contains("△ Permission required") {
+ return AgentState::Waiting;
+ }
+
+ // Busy
+ if has_interrupt_pattern(&content.to_lowercase()) {
+ return AgentState::Busy;
+ }
+
+ AgentState::Idle
+}
+
+fn detect_github_copilot(content: &str) -> AgentState {
+ let lower = content.to_lowercase();
+
+ // Waiting
+ if lower.contains("│ do you want") {
+ return AgentState::Waiting;
+ }
+ if lower.contains("confirm with") && lower.contains("enter") {
+ return AgentState::Waiting;
+ }
+
+ // Busy
+ if lower.contains("esc to cancel") {
+ return AgentState::Busy;
+ }
+
+ AgentState::Idle
+}
+
+fn detect_kimi(content: &str) -> AgentState {
+ let lower = content.to_lowercase();
+
+ // Waiting
+ if lower.contains("allow?")
+ || lower.contains("confirm?")
+ || lower.contains("approve?")
+ || lower.contains("proceed?")
+ || lower.contains("[y/n]")
+ || lower.contains("(y/n)")
+ {
+ return AgentState::Waiting;
+ }
+
+ // Busy
+ if lower.contains("thinking")
+ || lower.contains("processing")
+ || lower.contains("generating")
+ || lower.contains("waiting for response")
+ || lower.contains("ctrl+c to cancel")
+ || lower.contains("ctrl-c to cancel")
+ {
+ return AgentState::Busy;
+ }
+
+ AgentState::Idle
+}
+
+/// Droid detection.
+///
+/// Busy: braille spinner line (⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏) + "Thinking..." + "(Press ESC to stop)"
+/// Waiting: EXECUTE prompt with selection box ("Yes, allow" / "No, cancel") +
+/// "Use ↑↓ to navigate, Enter to select"
+/// Idle: prompt box visible, no spinner, no selection prompt
+fn detect_droid(content: &str) -> AgentState {
+ let lower = content.to_lowercase();
+
+ // Waiting: EXECUTE approval prompt with selection UI chrome
+ // Primary (AND): structural keyword + chrome text = certain
+ let has_execute = content.contains("EXECUTE");
+ let has_selection_chrome = lower.contains("enter to select")
+ || lower.contains("↑↓ to navigate")
+ || lower.contains("esc to cancel");
+ let has_selection_options = lower.contains("> yes, allow") || lower.contains("> no, cancel");
+
+ if has_execute && (has_selection_chrome || has_selection_options) {
+ return AgentState::Waiting;
+ }
+ // Secondary: selection chrome + options together (no EXECUTE needed)
+ if has_selection_chrome && has_selection_options {
+ return AgentState::Waiting;
+ }
+
+ // Busy: braille spinner character at start of a line + "Thinking..."
+ // The braille chars (⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏) are very specific — won't appear in normal content
+ if has_braille_spinner(content) && lower.contains("esc to stop") {
+ return AgentState::Busy;
+ }
+ // Fallback: "ESC to stop" alone is still a strong signal (it's UI chrome)
+ if lower.contains("esc to stop") {
+ return AgentState::Busy;
+ }
+
+ AgentState::Idle
+}
+
+/// Amp (Sourcegraph) detection.
+///
+/// Screen layout when busy:
+/// ```text
+/// ✓ Search Map the core runtime architecture...
+/// ⋯ Oracle ▼
+/// ≈ Running tools... Esc to cancel
+/// ```
+///
+/// "Esc to cancel" is the reliable busy indicator — it only appears
+/// while amp is actively running tools or thinking.
+fn detect_amp(content: &str) -> AgentState {
+ let lower = content.to_lowercase();
+
+ if lower.contains("esc to cancel") {
+ return AgentState::Busy;
+ }
+
+ AgentState::Idle
+}
+
+/// Check for braille spinner characters at the start of a line.
+/// These are the Unicode braille pattern dots used by CLI spinners.
+fn has_braille_spinner(content: &str) -> bool {
+ for line in content.lines() {
+ let trimmed = line.trim();
+ if let Some(c) = trimmed.chars().next() {
+ if ('\u{2800}'..='\u{28FF}').contains(&c) {
+ return true;
+ }
+ }
+ }
+ false
+}
+
+// ---------------------------------------------------------------------------
+// Shared helpers
+// ---------------------------------------------------------------------------
+
+/// Check for "do you want"/"would you like" followed by "yes" or "❯"
+fn has_confirmation_prompt(lower_content: &str) -> bool {
+ if let Some(pos) = lower_content
+ .find("do you want")
+ .or_else(|| lower_content.find("would you like"))
+ {
+ let after = &lower_content[pos..];
+ return after.contains("yes") || after.contains('❯');
+ }
+ false
+}
+
+/// Check for "❯" followed by numbered options like "1."
+fn has_selection_prompt(content: &str) -> bool {
+ for line in content.lines() {
+ let trimmed = line.trim();
+ if trimmed.starts_with('❯') {
+ // Check if there's a digit followed by a dot nearby
+ if trimmed.chars().any(|c| c.is_ascii_digit())
+ && trimmed.contains('.')
+ {
+ return true;
+ }
+ }
+ }
+ false
+}
+
+/// Check for "esc" + "interrupt" pattern
+fn has_interrupt_pattern(lower_content: &str) -> bool {
+ lower_content.contains("esc to interrupt")
+ || lower_content.contains("ctrl+c to interrupt")
+ || (lower_content.contains("esc") && lower_content.contains("interrupt"))
+}
+
+/// Claude Code spinner characters + activity label
+/// Pattern: one of ✱✲✳... followed by a word ending in "ing" and "…"
+fn has_spinner_activity(content: &str) -> bool {
+ const SPINNER_CHARS: &str = "✱✲✳✴✵✶✷✸✹✺✻✼✽✾✿❀❁❂❃❇❈❉❊❋✢✣✤✥✦✧✨⊛⊕⊙◉◎◍⁂⁕※⍟☼★☆";
+ for line in content.lines() {
+ let trimmed = line.trim();
+ let mut chars = trimmed.chars();
+ if let Some(first) = chars.next() {
+ if SPINNER_CHARS.contains(first) {
+ // Next should be a space, then a word ending in "ing", then "…"
+ let rest: String = chars.collect();
+ if rest.starts_with(' ') && rest.contains("ing") && rest.contains('\u{2026}') {
+ return true;
+ }
+ }
+ }
+ }
+ false
+}
+
+/// Cursor spinner: ⬡ or ⬢ followed by a word ending in "ing"
+fn has_cursor_spinner(content: &str) -> bool {
+ for line in content.lines() {
+ let trimmed = line.trim();
+ if (trimmed.starts_with('⬡') || trimmed.starts_with('⬢'))
+ && trimmed.contains("ing")
+ {
+ return true;
+ }
+ }
+ false
+}
+
+/// Extract content above Claude's prompt box.
+/// The prompt box is two ─── border lines with ❯ between them.
+fn content_above_prompt_box(content: &str) -> &str {
+ let lines: Vec<&str> = content.lines().collect();
+ let mut border_count = 0;
+
+ for i in (0..lines.len()).rev() {
+ let trimmed = lines[i].trim();
+ if !trimmed.is_empty() && trimmed.chars().all(|c| c == '─') {
+ border_count += 1;
+ if border_count == 2 {
+ // Return everything above this border
+ let byte_offset: usize = lines[..i].iter().map(|l| l.len() + 1).sum();
+ return &content[..byte_offset.min(content.len())];
+ }
+ }
+ }
+
+ // No prompt box found, return all content
+ content
+}
+
+// ---------------------------------------------------------------------------
+// Process identification (platform-specific)
+// ---------------------------------------------------------------------------
+
+/// Get the foreground process name for a given child PID.
+/// Delegates to platform-specific implementation.
+pub fn foreground_process_name(child_pid: u32) -> Option<String> {
+ crate::platform::foreground_process_name(child_pid)
+}
+
+// ---------------------------------------------------------------------------
+// Tests
+// ---------------------------------------------------------------------------
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ // ---- Agent identification ----
+
+ #[test]
+ fn identify_known_agents() {
+ assert_eq!(identify_agent("pi"), Some(Agent::Pi));
+ assert_eq!(identify_agent("claude"), Some(Agent::Claude));
+ assert_eq!(identify_agent("claude-code"), Some(Agent::Claude));
+ assert_eq!(identify_agent("codex"), Some(Agent::Codex));
+ assert_eq!(identify_agent("gemini"), Some(Agent::Gemini));
+ assert_eq!(identify_agent("cursor"), Some(Agent::Cursor));
+ assert_eq!(identify_agent("cline"), Some(Agent::Cline));
+ assert_eq!(identify_agent("opencode"), Some(Agent::OpenCode));
+ assert_eq!(identify_agent("kimi"), Some(Agent::Kimi));
+ assert_eq!(identify_agent("ghcs"), Some(Agent::GithubCopilot));
+ }
+
+ #[test]
+ fn identify_unknown_processes() {
+ assert_eq!(identify_agent("bash"), None);
+ assert_eq!(identify_agent("zsh"), None);
+ assert_eq!(identify_agent("vim"), None);
+ assert_eq!(identify_agent("node"), None);
+ }
+
+ #[test]
+ fn identify_case_insensitive() {
+ assert_eq!(identify_agent("Pi"), Some(Agent::Pi));
+ assert_eq!(identify_agent("CLAUDE"), Some(Agent::Claude));
+ assert_eq!(identify_agent("Codex"), Some(Agent::Codex));
+ }
+
+ // ---- Workspace state rollup ----
+
+ #[test]
+ fn workspace_state_waiting_wins() {
+ let states = [AgentState::Idle, AgentState::Busy, AgentState::Waiting];
+ assert_eq!(workspace_state(&states), AgentState::Waiting);
+ }
+
+ #[test]
+ fn workspace_state_busy_over_idle() {
+ let states = [AgentState::Idle, AgentState::Busy, AgentState::Unknown];
+ assert_eq!(workspace_state(&states), AgentState::Busy);
+ }
+
+ #[test]
+ fn workspace_state_empty() {
+ assert_eq!(workspace_state(&[]), AgentState::Unknown);
+ }
+
+ // ---- No agent → Unknown ----
+
+ #[test]
+ fn no_agent_returns_unknown() {
+ assert_eq!(detect_state(None, "anything"), AgentState::Unknown);
+ }
+
+ // ---- Pi ----
+
+ #[test]
+ fn pi_busy_when_working() {
+ assert_eq!(detect_pi("some output\nWorking..."), AgentState::Busy);
+ }
+
+ #[test]
+ fn pi_busy_working_in_middle() {
+ assert_eq!(
+ detect_pi("line1\nWorking...\nline3"),
+ AgentState::Busy
+ );
+ }
+
+ #[test]
+ fn pi_idle_at_prompt() {
+ assert_eq!(detect_pi("❯ "), AgentState::Idle);
+ }
+
+ #[test]
+ fn pi_idle_no_working_text() {
+ assert_eq!(
+ detect_pi("some output\n\n> ready"),
+ AgentState::Idle
+ );
+ }
+
+ // ---- Claude Code ----
+
+ #[test]
+ fn claude_busy_esc_to_interrupt() {
+ let screen = "Reading file src/main.rs\nesc to interrupt\n─────────\n❯ \n─────────";
+ assert_eq!(detect_claude(screen), AgentState::Busy);
+ }
+
+ #[test]
+ fn claude_busy_ctrl_c_to_interrupt() {
+ let screen = "Editing code\nctrl+c to interrupt\n─────────\n❯ \n─────────";
+ assert_eq!(detect_claude(screen), AgentState::Busy);
+ }
+
+ #[test]
+ fn claude_busy_spinner() {
+ let screen = "✽ Tempering…\n─────────\n❯ \n─────────";
+ assert_eq!(detect_claude(screen), AgentState::Busy);
+ }
+
+ #[test]
+ fn claude_busy_spinner_with_detail() {
+ let screen = "✳ Simplifying recompute_tangents…\n─────────\n❯ \n─────────";
+ assert_eq!(detect_claude(screen), AgentState::Busy);
+ }
+
+ #[test]
+ fn claude_waiting_do_you_want() {
+ let screen = "Do you want to run this command?\n\nYes No";
+ assert_eq!(detect_claude(screen), AgentState::Waiting);
+ }
+
+ #[test]
+ fn claude_waiting_would_you_like() {
+ let screen = "Would you like to apply these changes?\n\n❯ Yes";
+ assert_eq!(detect_claude(screen), AgentState::Waiting);
+ }
+
+ #[test]
+ fn claude_waiting_selection_prompt() {
+ let screen = "Choose an option:\n❯ 1. Apply\n 2. Skip\n 3. Cancel";
+ assert_eq!(detect_claude(screen), AgentState::Waiting);
+ }
+
+ #[test]
+ fn claude_waiting_esc_to_cancel() {
+ let screen = "Allow bash: rm -rf /tmp/test?\n\nesc to cancel";
+ assert_eq!(detect_claude(screen), AgentState::Waiting);
+ }
+
+ #[test]
+ fn claude_idle_prompt_box() {
+ let screen = "Task complete.\n─────────────\n❯ \n─────────────";
+ assert_eq!(detect_claude(screen), AgentState::Idle);
+ }
+
+ #[test]
+ fn claude_idle_search() {
+ let screen = "⌕ Search…\nsome content";
+ assert_eq!(detect_claude(screen), AgentState::Idle);
+ }
+
+ #[test]
+ fn claude_busy_not_confused_by_old_prompt() {
+ // The "esc to interrupt" is ABOVE the prompt box — should be busy
+ let screen = "✽ Writing…\nesc to interrupt\n──────\n❯ \n──────";
+ assert_eq!(detect_claude(screen), AgentState::Busy);
+ }
+
+ // ---- Codex ----
+
+ #[test]
+ fn codex_waiting_confirm() {
+ assert_eq!(
+ detect_codex("press enter to confirm or esc to cancel"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn codex_waiting_allow_command() {
+ assert_eq!(
+ detect_codex("allow command?\n[y/n]"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn codex_waiting_submit_answer() {
+ assert_eq!(
+ detect_codex("Question about approach\n| enter to submit answer"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn codex_busy_interrupt() {
+ assert_eq!(
+ detect_codex("generating code\nesc to interrupt"),
+ AgentState::Busy
+ );
+ }
+
+ #[test]
+ fn codex_idle() {
+ assert_eq!(detect_codex("❯ "), AgentState::Idle);
+ }
+
+ // ---- Gemini ----
+
+ #[test]
+ fn gemini_waiting_confirmation() {
+ assert_eq!(
+ detect_gemini("waiting for user confirmation"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn gemini_waiting_apply() {
+ assert_eq!(
+ detect_gemini("│ Apply this change\n│ Yes │ No"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn gemini_waiting_allow_execution() {
+ assert_eq!(
+ detect_gemini("│ Allow execution of: rm test.txt"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn gemini_busy() {
+ assert_eq!(
+ detect_gemini("thinking...\nesc to cancel"),
+ AgentState::Busy
+ );
+ }
+
+ #[test]
+ fn gemini_idle() {
+ assert_eq!(detect_gemini("❯ "), AgentState::Idle);
+ }
+
+ // ---- Cursor ----
+
+ #[test]
+ fn cursor_waiting_accept() {
+ assert_eq!(
+ detect_cursor("Apply changes? (y) (enter) or keep (n)"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn cursor_waiting_allow() {
+ assert_eq!(
+ detect_cursor("allow file edit (y)"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn cursor_busy_spinner() {
+ assert_eq!(
+ detect_cursor("⬡ Grepping.."),
+ AgentState::Busy
+ );
+ }
+
+ #[test]
+ fn cursor_busy_ctrl_c() {
+ assert_eq!(
+ detect_cursor("processing\nctrl+c to stop"),
+ AgentState::Busy
+ );
+ }
+
+ #[test]
+ fn cursor_idle() {
+ assert_eq!(detect_cursor("> "), AgentState::Idle);
+ }
+
+ // ---- Cline ----
+
+ #[test]
+ fn cline_waiting_tool_use() {
+ assert_eq!(
+ detect_cline("let cline use this tool"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn cline_waiting_act_mode() {
+ assert_eq!(
+ detect_cline("[act mode] execute command?\nyes"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn cline_idle_ready() {
+ assert_eq!(
+ detect_cline("cline is ready for your message"),
+ AgentState::Idle
+ );
+ }
+
+ #[test]
+ fn cline_defaults_to_busy() {
+ // Cline's default is busy (unlike other agents)
+ assert_eq!(detect_cline("some random output"), AgentState::Busy);
+ }
+
+ // ---- OpenCode ----
+
+ #[test]
+ fn opencode_waiting_permission() {
+ assert_eq!(
+ detect_opencode("△ Permission required"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn opencode_busy() {
+ assert_eq!(
+ detect_opencode("running tool\nesc to interrupt"),
+ AgentState::Busy
+ );
+ }
+
+ #[test]
+ fn opencode_idle() {
+ assert_eq!(detect_opencode("> "), AgentState::Idle);
+ }
+
+ // ---- GitHub Copilot ----
+
+ #[test]
+ fn copilot_waiting_confirm() {
+ assert_eq!(
+ detect_github_copilot("confirm with enter"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn copilot_waiting_do_you_want() {
+ assert_eq!(
+ detect_github_copilot("│ do you want to apply?"),
+ AgentState::Waiting
+ );
+ }
+
+ #[test]
+ fn copilot_busy() {
+ assert_eq!(
+ detect_github_copilot("generating\nesc to cancel"),
+ AgentState::Busy
+ );
+ }
+
+ #[test]
+ fn copilot_idle() {
+ assert_eq!(detect_github_copilot("> "), AgentState::Idle);
+ }
+
+ // ---- Kimi ----
+
+ #[test]
+ fn kimi_waiting_approve() {
+ assert_eq!(detect_kimi("approve?"), AgentState::Waiting);
+ }
+
+ #[test]
+ fn kimi_waiting_yn() {
+ assert_eq!(detect_kimi("continue? [y/n]"), AgentState::Waiting);
+ }
+
+ #[test]
+ fn kimi_busy_thinking() {
+ assert_eq!(detect_kimi("thinking"), AgentState::Busy);
+ }
+
+ #[test]
+ fn kimi_busy_generating() {
+ assert_eq!(detect_kimi("generating code"), AgentState::Busy);
+ }
+
+ #[test]
+ fn kimi_idle() {
+ assert_eq!(detect_kimi("> "), AgentState::Idle);
+ }
+
+ // ---- Droid ----
+
+ #[test]
+ fn droid_busy_thinking_with_spinner() {
+ let screen = "> how u doin\n\n⠴ Thinking... (Press ESC to stop)\n\nAuto (Off)";
+ assert_eq!(detect_droid(screen), AgentState::Busy);
+ }
+
+ #[test]
+ fn droid_busy_esc_to_stop_alone() {
+ // ESC to stop without spinner is still busy (UI chrome)
+ let screen = "Processing\n(Press ESC to stop)";
+ assert_eq!(detect_droid(screen), AgentState::Busy);
+ }
+
+ #[test]
+ fn droid_waiting_execute_approval() {
+ let screen = concat!(
+ "⛬ I'll create some folders.\n\n",
+ " EXECUTE (mkdir -p /tmp/test, impact: medium)\n\n",
+ "╭────────────────────╮\n",
+ "│ > Yes, allow │\n",
+ "│ Yes, always allow │\n",
+ "│ No, cancel │\n",
+ "╰────────────────────╯\n",
+ " Use ↑↓ to navigate, Enter to select, Esc to cancel\n",
+ );
+ assert_eq!(detect_droid(screen), AgentState::Waiting);
+ }
+
+ #[test]
+ fn droid_waiting_selection_with_chrome() {
+ let screen = "│ > Yes, allow │\n│ No, cancel │\n Use ↑↓ to navigate, Enter to select, Esc to cancel";
+ assert_eq!(detect_droid(screen), AgentState::Waiting);
+ }
+
+ #[test]
+ fn droid_not_waiting_on_options_text_alone() {
+ // "Yes, allow" in normal conversation should NOT trigger waiting
+ let screen = "The user said > Yes, allow the changes";
+ assert_eq!(detect_droid(screen), AgentState::Idle);
+ }
+
+ #[test]
+ fn droid_idle_prompt() {
+ let screen = "╭──────────────────╮\n│ > Try something │\n╰──────────────────╯\n? for help";
+ assert_eq!(detect_droid(screen), AgentState::Idle);
+ }
+
+ #[test]
+ fn droid_idle_after_response() {
+ let screen = "⛬ Doing well, thanks!\n\nAuto (Off)\n╭──────────╮\n│ > │\n╰──────────╯";
+ assert_eq!(detect_droid(screen), AgentState::Idle);
+ }
+
+ #[test]
+ fn droid_braille_spinner_detected() {
+ assert!(has_braille_spinner("⠴ Thinking..."));
+ assert!(has_braille_spinner(" ⠧ Loading..."));
+ assert!(has_braille_spinner("text\n⠋ Working\nmore"));
+ }
+
+ #[test]
+ fn droid_braille_spinner_no_false_positive() {
+ assert!(!has_braille_spinner("normal text"));
+ assert!(!has_braille_spinner("Thinking..."));
+ assert!(!has_braille_spinner("some ⠴ in middle of text"));
+ }
+
+ #[test]
+ fn droid_identified_by_process_name() {
+ assert_eq!(identify_agent("droid"), Some(Agent::Droid));
+ }
+
+ // ---- Amp ----
+
+ #[test]
+ fn amp_busy_running_tools() {
+ let screen = " ✓ Search Map the core runtime architecture\n ⋯ Oracle ▼\n ≈ Running tools... Esc to cancel";
+ assert_eq!(detect_state(Some(Agent::Amp), screen), AgentState::Busy);
+ }
+
+ #[test]
+ fn amp_idle() {
+ let screen = " Response complete.\n\n╭─100% of 272k · $1.20─────────────────────────╮\n│ │\n╰───────────────────────~/Projects/herdr (master)╯";
+ assert_eq!(detect_state(Some(Agent::Amp), screen), AgentState::Idle);
+ }
+
+ #[test]
+ fn amp_identified_by_process_name() {
+ assert_eq!(identify_agent("amp"), Some(Agent::Amp));
+ assert_eq!(identify_agent("amp-local"), Some(Agent::Amp));
+ }
+
+ // ---- Helpers ----
+
+ #[test]
+ fn content_above_prompt_box_extracts_correctly() {
+ let screen = "line1\nline2\n──────\n❯ \n──────";
+ let above = content_above_prompt_box(screen);
+ assert!(above.contains("line1"));
+ assert!(above.contains("line2"));
+ assert!(!above.contains('❯'));
+ }
+
+ #[test]
+ fn content_above_prompt_box_no_box() {
+ let screen = "just some text\nno borders here";
+ let above = content_above_prompt_box(screen);
+ assert_eq!(above, screen);
+ }
+
+ #[test]
+ fn spinner_activity_detected() {
+ assert!(has_spinner_activity("✽ Tempering…"));
+ assert!(has_spinner_activity("✳ Simplifying recompute_tangents…"));
+ assert!(has_spinner_activity(" ✶ Reading…")); // with leading whitespace
+ }
+
+ #[test]
+ fn spinner_activity_not_false_positive() {
+ assert!(!has_spinner_activity("normal text"));
+ assert!(!has_spinner_activity("✽ no ellipsis here"));
+ assert!(!has_spinner_activity("some ✽ in the middle"));
+ }
+
+ #[test]
+ fn cursor_spinner_detected() {
+ assert!(has_cursor_spinner("⬡ Grepping.."));
+ assert!(has_cursor_spinner("⬢ Reading…"));
+ }
+
+ #[test]
+ fn cursor_spinner_not_false_positive() {
+ assert!(!has_cursor_spinner("normal text"));
+ assert!(!has_cursor_spinner("some ⬡ in middle"));
+ }
+
+ // ---- Process identification (real PTY) ----
+
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn foreground_process_name_detects_sleep() {
+ use portable_pty::{native_pty_system, CommandBuilder, PtySize};
+
+ let pty_system = native_pty_system();
+ let pair = pty_system
+ .openpty(PtySize {
+ rows: 24,
+ cols: 80,
+ pixel_width: 0,
+ pixel_height: 0,
+ })
+ .expect("failed to open pty");
+
+ // Spawn "sleep 999" — a known, deterministic process
+ let mut cmd = CommandBuilder::new("sleep");
+ cmd.arg("999");
+ let mut child = pair.slave.spawn_command(cmd).expect("failed to spawn");
+ let pid = child.process_id().expect("no pid");
+
+ // Give the process a moment to become the foreground group
+ std::thread::sleep(std::time::Duration::from_millis(50));
+
+ let name = foreground_process_name(pid);
+ assert_eq!(name.as_deref(), Some("sleep"), "expected 'sleep', got {name:?}");
+
+ // Clean up
+ child.kill().ok();
+ child.wait().ok();
+ }
+
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn foreground_process_name_detects_shell_running_command() {
+ use portable_pty::{native_pty_system, CommandBuilder, PtySize};
+ use std::io::Write;
+
+ let pty_system = native_pty_system();
+ let pair = pty_system
+ .openpty(PtySize {
+ rows: 24,
+ cols: 80,
+ pixel_width: 0,
+ pixel_height: 0,
+ })
+ .expect("failed to open pty");
+
+ // Spawn a shell, then run a command inside it
+ let cmd = CommandBuilder::new("sh");
+ let mut child = pair.slave.spawn_command(cmd).expect("failed to spawn");
+ let pid = child.process_id().expect("no pid");
+
+ // Write a command to the shell
+ let mut writer = pair.master.take_writer().expect("no writer");
+ // Use exec so sleep replaces sh as the foreground process
+ writer.write_all(b"exec sleep 999\n").ok();
+ drop(writer);
+
+ std::thread::sleep(std::time::Duration::from_millis(100));
+
+ let name = foreground_process_name(pid);
+ // The foreground process should now be "sleep", not "sh"
+ assert_eq!(name.as_deref(), Some("sleep"), "expected 'sleep', got {name:?}");
+
+ child.kill().ok();
+ child.wait().ok();
+ }
+
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn proc_stat_parsing_handles_spaces_in_comm() {
+ // Verify our /proc/pid/stat parser correctly extracts fields
+ // even when (comm) could contain spaces.
+ let pid = std::process::id();
+ let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).unwrap();
+
+ // Our parsing: find last ')' then split the rest
+ let close_paren = stat.rfind(')').expect("should have closing paren");
+ let rest = &stat[close_paren + 2..];
+ let fields: Vec<&str> = rest.split_whitespace().collect();
+
+ // We should have enough fields (at least 6 for tpgid)
+ assert!(fields.len() >= 6, "not enough fields in stat: {}", fields.len());
+
+ // Field 0 should be a valid state char (S, R, D, etc.)
+ let state = fields[0];
+ assert!(
+ ["S", "R", "D", "Z", "T", "t", "W", "X", "I"].contains(&state),
+ "unexpected state: {state}"
+ );
+
+ // Field 5 (tpgid) should parse as i32 (can be -1 if no controlling terminal)
+ let tpgid: i32 = fields[5].parse().expect("tpgid should be a number");
+ // In CI/test environments without a terminal, tpgid is typically -1
+ let _ = tpgid;
+ }
+
+ // ---- VT100 integration ----
+
+ #[test]
+ fn vt100_screen_content_works_with_detection() {
+ let mut parser = vt100::Parser::new(24, 80, 0);
+ parser.process(b"Working...");
+ let content = parser.screen().contents();
+ assert_eq!(detect_pi(&content), AgentState::Busy);
+ }
+
+ #[test]
+ fn vt100_screen_with_ansi_colors() {
+ let mut parser = vt100::Parser::new(24, 80, 0);
+ // Red "Working..." with ANSI codes — contents() strips formatting
+ parser.process(b"\x1b[31mWorking...\x1b[0m");
+ let content = parser.screen().contents();
+ assert_eq!(detect_pi(&content), AgentState::Busy);
+ }
+
+ #[test]
+ fn vt100_screen_claude_prompt_box() {
+ let mut parser = vt100::Parser::new(24, 80, 0);
+ let screen = "Task complete.\n─────────────\n❯ \n─────────────";
+ parser.process(screen.as_bytes());
+ let content = parser.screen().contents();
+ assert_eq!(detect_claude(&content), AgentState::Idle);
+ }
+}
diff --git a/src/events.rs b/src/events.rs
new file mode 100644
index 00000000..e86850cb
--- /dev/null
+++ b/src/events.rs
@@ -0,0 +1,26 @@
+//! Internal app events delivered via channel.
+//!
+//! Background tasks (PTY child watchers, future hook listeners, etc.) send
+//! events to the main loop through this channel. No polling needed.
+
+use crate::detect::{Agent, AgentState};
+use crate::layout::PaneId;
+
+/// An event from a background task to the main loop.
+#[derive(Debug)]
+pub enum AppEvent {
+ /// A pane's child process exited.
+ PaneDied {
+ pane_id: PaneId,
+ },
+ /// Agent state changed in a pane (detected by the PTY reader).
+ StateChanged {
+ pane_id: PaneId,
+ agent: Option<Agent>,
+ state: AgentState,
+ },
+ /// A new version was downloaded and installed. Restart to use it.
+ UpdateReady {
+ version: String,
+ },
+}
diff --git a/src/input.rs b/src/input.rs
new file mode 100644
index 00000000..dbbf65df
--- /dev/null
+++ b/src/input.rs
@@ -0,0 +1,339 @@
+use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
+
+/// Encode a key event for a PTY child.
+/// If `kitty_mode` is true, modified keys use CSI u format.
+pub fn encode_key(key: KeyEvent, kitty_mode: bool) -> Vec<u8> {
+ if kitty_mode {
+ if let Some(bytes) = try_encode_csi_u(&key) {
+ return bytes;
+ }
+ }
+ encode_legacy(key)
+}
+
+/// CSI u encoding: \e[{codepoint};{modifiers}u
+/// Used when the child has pushed Kitty keyboard enhancement.
+/// Returns None if the key doesn't need CSI u (unmodified basic keys).
+fn try_encode_csi_u(key: &KeyEvent) -> Option<Vec<u8>> {
+ let mods = key.modifiers;
+
+ // Unmodified keys use legacy encoding (more compatible)
+ if mods.is_empty() {
+ return None;
+ }
+
+ // Plain Ctrl+letter is well-represented in legacy (bytes 1-26)
+ if mods == KeyModifiers::CONTROL {
+ if let KeyCode::Char(c) = key.code {
+ if c.is_ascii_alphabetic() {
+ return None; // let legacy handle it
+ }
+ }
+ }
+
+ // Special keys (arrows, F-keys, etc.) have well-established legacy
+ // xterm modified formats (\x1b[1;3A for Alt+Up, etc.) that are universally
+ // understood. Even Ghostty sends these in legacy format with kitty mode on.
+ // Only use CSI u for character keys and keys without legacy representations.
+ match key.code {
+ KeyCode::Up | KeyCode::Down | KeyCode::Left | KeyCode::Right
+ | KeyCode::Home | KeyCode::End | KeyCode::PageUp | KeyCode::PageDown
+ | KeyCode::Insert | KeyCode::Delete | KeyCode::F(_) => {
+ return None; // let legacy handle these
+ }
+ _ => {}
+ }
+
+ let codepoint = match key.code {
+ KeyCode::Char(c) => c as u32,
+ KeyCode::Enter => 13,
+ KeyCode::Tab => 9,
+ KeyCode::Backspace => 127,
+ KeyCode::Esc => 27,
+ _ => return None, // fall back to legacy for unhandled keys
+ };
+
+ let modifier = kitty_modifier(mods);
+
+ Some(format!("\x1b[{codepoint};{modifier}u").into_bytes())
+}
+
+/// Legacy terminal encoding (standard escape sequences).
+fn encode_legacy(key: KeyEvent) -> Vec<u8> {
+ let mods = key.modifiers;
+
+ // Modified special keys (arrows, home, end, etc.) use xterm format:
+ // \x1b[1;{modifier}A for arrows/home/end
+ // \x1b[{n};{modifier}~ for insert/delete/pgup/pgdn
+ // The ESC-prefix hack doesn't work for these since they're already escape sequences.
+ if !mods.is_empty() {
+ if let Some(bytes) = encode_modified_special(key.code, mods) {
+ return bytes;
+ }
+ }
+
+ // Alt modifier on character keys: prefix with ESC
+ if mods.contains(KeyModifiers::ALT) {
+ let inner = KeyEvent::new(key.code, mods.difference(KeyModifiers::ALT));
+ let mut bytes = vec![0x1b];
+ bytes.extend(encode_legacy_inner(inner));
+ return bytes;
+ }
+ encode_legacy_inner(key)
+}
+
+/// xterm-style encoding for modified special keys.
+/// Modifier value: 1 + (shift?1:0) + (alt?2:0) + (ctrl?4:0)
+fn encode_modified_special(code: KeyCode, mods: KeyModifiers) -> Option<Vec<u8>> {
+ let modifier = xterm_modifier(mods);
+ if modifier <= 1 {
+ return None; // no modifiers to encode
+ }
+
+ match code {
+ // CSI 1;{mod}{letter} format
+ KeyCode::Up => Some(format!("\x1b[1;{modifier}A").into_bytes()),
+ KeyCode::Down => Some(format!("\x1b[1;{modifier}B").into_bytes()),
+ KeyCode::Right => Some(format!("\x1b[1;{modifier}C").into_bytes()),
+ KeyCode::Left => Some(format!("\x1b[1;{modifier}D").into_bytes()),
+ KeyCode::Home => Some(format!("\x1b[1;{modifier}H").into_bytes()),
+ KeyCode::End => Some(format!("\x1b[1;{modifier}F").into_bytes()),
+ // CSI {n};{mod}~ format
+ KeyCode::Insert => Some(format!("\x1b[2;{modifier}~").into_bytes()),
+ KeyCode::Delete => Some(format!("\x1b[3;{modifier}~").into_bytes()),
+ KeyCode::PageUp => Some(format!("\x1b[5;{modifier}~").into_bytes()),
+ KeyCode::PageDown => Some(format!("\x1b[6;{modifier}~").into_bytes()),
+ // F1-F4: CSI 1;{mod}{P-S}
+ KeyCode::F(1) => Some(format!("\x1b[1;{modifier}P").into_bytes()),
+ KeyCode::F(2) => Some(format!("\x1b[1;{modifier}Q").into_bytes()),
+ KeyCode::F(3) => Some(format!("\x1b[1;{modifier}R").into_bytes()),
+ KeyCode::F(4) => Some(format!("\x1b[1;{modifier}S").into_bytes()),
+ // F5-F12: CSI {n};{mod}~
+ KeyCode::F(n @ 5..=12) => {
+ let code = match n {
+ 5 => 15,
+ 6 => 17,
+ 7 => 18,
+ 8 => 19,
+ 9 => 20,
+ 10 => 21,
+ 11 => 23,
+ 12 => 24,
+ _ => unreachable!(),
+ };
+ Some(format!("\x1b[{code};{modifier}~").into_bytes())
+ }
+ _ => None,
+ }
+}
+
+/// xterm modifier encoding: 1 + shift(1) + alt(2) + ctrl(4)
+/// Used for legacy modified special keys (arrows, function keys, etc.)
+fn xterm_modifier(mods: KeyModifiers) -> u32 {
+ let mut m = 1u32;
+ if mods.contains(KeyModifiers::SHIFT) {
+ m += 1;
+ }
+ if mods.contains(KeyModifiers::ALT) {
+ m += 2;
+ }
+ if mods.contains(KeyModifiers::CONTROL) {
+ m += 4;
+ }
+ m
+}
+
+/// Kitty protocol modifier encoding: 1 + shift(1) + alt(2) + ctrl(4) + super(8) + hyper(16) + meta(32)
+/// Superset of xterm — adds Super/Hyper/Meta bits.
+fn kitty_modifier(mods: KeyModifiers) -> u32 {
+ let mut m = xterm_modifier(mods);
+ if mods.contains(KeyModifiers::SUPER) {
+ m += 8;
+ }
+ if mods.contains(KeyModifiers::HYPER) {
+ m += 16;
+ }
+ if mods.contains(KeyModifiers::META) {
+ m += 32;
+ }
+ m
+}
+
+fn encode_legacy_inner(key: KeyEvent) -> Vec<u8> {
+ match key.code {
+ KeyCode::Char(ch) => {
+ if key.modifiers.contains(KeyModifiers::CONTROL) {
+ let upper = ch.to_ascii_uppercase();
+ match upper {
+ 'A'..='Z' => vec![upper as u8 - 64],
+ ' ' | '@' | '2' => vec![0],
+ '[' | '3' => vec![27],
+ '\\' | '4' => vec![28],
+ ']' | '5' => vec![29],
+ '^' | '6' => vec![30],
+ '_' | '7' | '-' => vec![31],
+ _ => vec![ch as u8],
+ }
+ } else {
+ let mut buf = [0u8; 4];
+ ch.encode_utf8(&mut buf).as_bytes().to_vec()
+ }
+ }
+ KeyCode::Enter => vec![b'\r'],
+ KeyCode::Backspace => vec![127],
+ KeyCode::Tab => vec![9],
+ KeyCode::BackTab => vec![27, 91, 90],
+ KeyCode::Esc => vec![27],
+ KeyCode::Left => vec![27, 91, 68],
+ KeyCode::Right => vec![27, 91, 67],
+ KeyCode::Up => vec![27, 91, 65],
+ KeyCode::Down => vec![27, 91, 66],
+ KeyCode::Home => vec![27, 91, 72],
+ KeyCode::End => vec![27, 91, 70],
+ KeyCode::PageUp => vec![27, 91, 53, 126],
+ KeyCode::PageDown => vec![27, 91, 54, 126],
+ KeyCode::Delete => vec![27, 91, 51, 126],
+ KeyCode::Insert => vec![27, 91, 50, 126],
+ KeyCode::F(n) => encode_f_key(n),
+ _ => vec![],
+ }
+}
+
+fn encode_f_key(n: u8) -> Vec<u8> {
+ match n {
+ 1 => vec![27, 79, 80],
+ 2 => vec![27, 79, 81],
+ 3 => vec![27, 79, 82],
+ 4 => vec![27, 79, 83],
+ 5 => vec![27, 91, 49, 53, 126],
+ 6 => vec![27, 91, 49, 55, 126],
+ 7 => vec![27, 91, 49, 56, 126],
+ 8 => vec![27, 91, 49, 57, 126],
+ 9 => vec![27, 91, 50, 48, 126],
+ 10 => vec![27, 91, 50, 49, 126],
+ 11 => vec![27, 91, 50, 51, 126],
+ 12 => vec![27, 91, 50, 52, 126],
+ _ => vec![],
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn legacy_enter() {
+ let key = KeyEvent::new(KeyCode::Enter, KeyModifiers::empty());
+ assert_eq!(encode_key(key, false), vec![b'\r']);
+ }
+
+ #[test]
+ fn legacy_ctrl_c() {
+ let key = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
+ assert_eq!(encode_key(key, false), vec![3]);
+ }
+
+ #[test]
+ fn legacy_shift_enter_is_just_cr() {
+ let key = KeyEvent::new(KeyCode::Enter, KeyModifiers::SHIFT);
+ // Enter/Tab/Backspace/Esc aren't special keys with xterm modifier encoding,
+ // so Shift+Enter falls through to legacy which just sends CR
+ assert_eq!(encode_key(key, false), vec![b'\r']);
+ }
+
+ #[test]
+ fn legacy_alt_up() {
+ let key = KeyEvent::new(KeyCode::Up, KeyModifiers::ALT);
+ // xterm modified key format: CSI 1;3A (3 = 1 + Alt)
+ assert_eq!(encode_key(key, false), b"\x1b[1;3A");
+ }
+
+ #[test]
+ fn legacy_shift_right() {
+ let key = KeyEvent::new(KeyCode::Right, KeyModifiers::SHIFT);
+ assert_eq!(encode_key(key, false), b"\x1b[1;2C");
+ }
+
+ #[test]
+ fn legacy_ctrl_left() {
+ let key = KeyEvent::new(KeyCode::Left, KeyModifiers::CONTROL);
+ assert_eq!(encode_key(key, false), b"\x1b[1;5D");
+ }
+
+ #[test]
+ fn legacy_ctrl_shift_end() {
+ let key = KeyEvent::new(
+ KeyCode::End,
+ KeyModifiers::CONTROL | KeyModifiers::SHIFT,
+ );
+ assert_eq!(encode_key(key, false), b"\x1b[1;6F");
+ }
+
+ #[test]
+ fn legacy_alt_delete() {
+ let key = KeyEvent::new(KeyCode::Delete, KeyModifiers::ALT);
+ assert_eq!(encode_key(key, false), b"\x1b[3;3~");
+ }
+
+ #[test]
+ fn legacy_shift_f5() {
+ let key = KeyEvent::new(KeyCode::F(5), KeyModifiers::SHIFT);
+ assert_eq!(encode_key(key, false), b"\x1b[15;2~");
+ }
+
+ #[test]
+ fn legacy_alt_char_still_esc_prefix() {
+ // Alt+a on character keys still uses ESC prefix (not xterm modified)
+ let key = KeyEvent::new(KeyCode::Char('a'), KeyModifiers::ALT);
+ assert_eq!(encode_key(key, false), b"\x1ba");
+ }
+
+ #[test]
+ fn kitty_shift_enter() {
+ let key = KeyEvent::new(KeyCode::Enter, KeyModifiers::SHIFT);
+ assert_eq!(encode_key(key, true), b"\x1b[13;2u");
+ }
+
+ #[test]
+ fn kitty_ctrl_shift_a() {
+ let key = KeyEvent::new(
+ KeyCode::Char('a'),
+ KeyModifiers::CONTROL | KeyModifiers::SHIFT,
+ );
+ assert_eq!(encode_key(key, true), b"\x1b[97;6u");
+ }
+
+ #[test]
+ fn kitty_alt_enter() {
+ let key = KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT);
+ assert_eq!(encode_key(key, true), b"\x1b[13;3u");
+ }
+
+ #[test]
+ fn kitty_plain_ctrl_c_uses_legacy() {
+ // Plain Ctrl+letter is well-represented in legacy
+ let key = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
+ assert_eq!(encode_key(key, true), vec![3]);
+ }
+
+ #[test]
+ fn kitty_unmodified_uses_legacy() {
+ let key = KeyEvent::new(KeyCode::Char('a'), KeyModifiers::empty());
+ assert_eq!(encode_key(key, true), b"a");
+ }
+
+ #[test]
+ fn kitty_shift_tab() {
+ let key = KeyEvent::new(KeyCode::Tab, KeyModifiers::SHIFT);
+ assert_eq!(encode_key(key, true), b"\x1b[9;2u");
+ }
+
+ #[test]
+ fn kitty_ctrl_shift_enter() {
+ let key = KeyEvent::new(
+ KeyCode::Enter,
+ KeyModifiers::CONTROL | KeyModifiers::SHIFT,
+ );
+ assert_eq!(encode_key(key, true), b"\x1b[13;6u");
+ }
+}
diff --git a/src/layout.rs b/src/layout.rs
new file mode 100644
index 00000000..23bdbb7d
--- /dev/null
+++ b/src/layout.rs
@@ -0,0 +1,470 @@
+//! BSP tree layout for tiling panes within a workspace.
+
+use ratatui::layout::{Direction, Rect};
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
+pub struct PaneId(u32);
+
+/// Global atomic counter for unique PaneId generation across all workspaces.
+static NEXT_PANE_ID: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(1);
+
+impl PaneId {
+ /// Allocate a globally unique PaneId.
+ pub fn alloc() -> Self {
+ Self(NEXT_PANE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed))
+ }
+
+ pub fn raw(self) -> u32 {
+ self.0
+ }
+
+ /// Reconstruct from a saved u32 (persistence only).
+ pub fn from_raw(id: u32) -> Self {
+ Self(id)
+ }
+}
+
+/// Snapshot of a pane's position and focus state after layout.
+#[derive(Clone)]
+pub struct PaneInfo {
+ pub id: PaneId,
+ /// Outer rect (including borders if present).
+ pub rect: Rect,
+ /// Inner rect (content area, excluding borders). Used for selection.
+ pub inner_rect: Rect,
+ pub is_focused: bool,
+}
+
+/// Info about a split boundary, used for mouse drag resize.
+#[derive(Clone)]
+pub struct SplitBorder {
+ /// Position of the divider line (x for horizontal split, y for vertical).
+ pub pos: u16,
+ /// Direction of the split that created this border.
+ pub direction: Direction,
+ /// Total area of the split node.
+ pub area: Rect,
+ /// Path from root to this split node (false=first, true=second).
+ pub path: Vec<bool>,
+}
+
+/// Cardinal direction for pane navigation.
+#[derive(Debug, Clone, Copy)]
+pub enum NavDirection {
+ Left,
+ Right,
+ Up,
+ Down,
+}
+
+/// A node in the BSP tree. Public for serialization.
+pub enum Node {
+ Pane(PaneId),
+ Split {
+ direction: Direction,
+ ratio: f32,
+ first: Box<Node>,
+ second: Box<Node>,
+ },
+}
+
+/// BSP tiling layout. Tracks a tree of splits and a focused pane.
+pub struct TileLayout {
+ root: Node,
+ focus: PaneId,
+}
+
+impl TileLayout {
+ /// Create a new layout with a single pane (globally unique ID).
+ /// Returns (layout, root_pane_id) so the caller can create the pane.
+ pub fn new() -> (Self, PaneId) {
+ let root_id = PaneId::alloc();
+ (
+ Self {
+ root: Node::Pane(root_id),
+ focus: root_id,
+ },
+ root_id,
+ )
+ }
+
+ pub fn focused(&self) -> PaneId {
+ self.focus
+ }
+
+ pub fn pane_count(&self) -> usize {
+ count_panes(&self.root)
+ }
+
+ /// Compute rects for all panes given the available area.
+ pub fn panes(&self, area: Rect) -> Vec<PaneInfo> {
+ let mut result = Vec::new();
+ collect_panes(&self.root, area, self.focus, &mut result);
+ result
+ }
+
+ /// Collect all split boundaries for mouse drag resize.
+ pub fn splits(&self, area: Rect) -> Vec<SplitBorder> {
+ let mut result = Vec::new();
+ collect_splits(&self.root, area, vec![], &mut result);
+ result
+ }
+
+ /// Split the focused pane. Returns the new pane's id.
+ pub fn split_focused(&mut self, direction: Direction) -> PaneId {
+ let new_id = PaneId::alloc();
+ let placeholder = PaneId::from_raw(0);
+ let old = std::mem::replace(&mut self.root, Node::Pane(placeholder));
+ self.root = split_at(old, self.focus, direction, new_id);
+ self.focus = new_id;
+ new_id
+ }
+
+ /// Close the focused pane. Returns false if it's the last pane.
+ pub fn close_focused(&mut self) -> bool {
+ if self.pane_count() <= 1 {
+ return false;
+ }
+ let target = self.focus;
+ let ids = self.pane_ids();
+ let pos = ids.iter().position(|id| *id == target).unwrap();
+ let new_focus = if pos + 1 < ids.len() {
+ ids[pos + 1]
+ } else {
+ ids[pos - 1]
+ };
+ let placeholder = PaneId::from_raw(0);
+ let old = std::mem::replace(&mut self.root, Node::Pane(placeholder));
+ if let Some(new_root) = remove_pane(old, target) {
+ self.root = new_root;
+ self.focus = new_focus;
+ true
+ } else {
+ false
+ }
+ }
+
+ pub fn focus_next(&mut self) {
+ let ids = self.pane_ids();
+ if let Some(pos) = ids.iter().position(|id| *id == self.focus) {
+ self.focus = ids[(pos + 1) % ids.len()];
+ }
+ }
+
+ pub fn focus_prev(&mut self) {
+ let ids = self.pane_ids();
+ if let Some(pos) = ids.iter().position(|id| *id == self.focus) {
+ self.focus = ids[(pos + ids.len() - 1) % ids.len()];
+ }
+ }
+
+ pub fn focus_pane(&mut self, id: PaneId) {
+ if self.pane_ids().contains(&id) {
+ self.focus = id;
+ }
+ }
+
+ /// Set the ratio of a split node at the given path.
+ pub fn set_ratio_at(&mut self, path: &[bool], ratio: f32) {
+ set_ratio_at(&mut self.root, path, ratio.clamp(0.1, 0.9));
+ }
+
+ /// Adjust the nearest split in the given direction for the focused pane.
+ /// `delta` is positive to grow, negative to shrink.
+ pub fn resize_focused(&mut self, nav: NavDirection, delta: f32, area: Rect) {
+ let panes = self.panes(area);
+ let Some(focused) = panes.iter().find(|p| p.is_focused) else {
+ return;
+ };
+ let focused_rect = focused.rect;
+ let splits = self.splits(area);
+
+ // Find the split whose border is adjacent to the focused pane in the given direction
+ let target_dir = match nav {
+ NavDirection::Left | NavDirection::Right => Direction::Horizontal,
+ NavDirection::Up | NavDirection::Down => Direction::Vertical,
+ };
+ let grows = matches!(nav, NavDirection::Right | NavDirection::Down);
+
+ // Find the closest matching split border
+ let best = splits
+ .iter()
+ .filter(|s| s.direction == target_dir)
+ .filter(|s| match target_dir {
+ Direction::Horizontal => {
+ // Border must be near the focused pane's left or right edge
+ let near_right = (s.pos as i32 - (focused_rect.x + focused_rect.width) as i32).unsigned_abs() <= 1;
+ let near_left = (s.pos as i32 - focused_rect.x as i32).unsigned_abs() <= 1;
+ near_right || near_left
+ }
+ Direction::Vertical => {
+ let near_bottom = (s.pos as i32 - (focused_rect.y + focused_rect.height) as i32).unsigned_abs() <= 1;
+ let near_top = (s.pos as i32 - focused_rect.y as i32).unsigned_abs() <= 1;
+ near_bottom || near_top
+ }
+ })
+ .min_by_key(|s| {
+ // Prefer the border in the direction we're resizing toward
+ match (target_dir, grows) {
+ (Direction::Horizontal, true) => {
+ ((focused_rect.x + focused_rect.width) as i32 - s.pos as i32).unsigned_abs()
+ }
+ (Direction::Horizontal, false) => {
+ (focused_rect.x as i32 - s.pos as i32).unsigned_abs()
+ }
+ (Direction::Vertical, true) => {
+ ((focused_rect.y + focused_rect.height) as i32 - s.pos as i32).unsigned_abs()
+ }
+ (Direction::Vertical, false) => {
+ (focused_rect.y as i32 - s.pos as i32).unsigned_abs()
+ }
+ }
+ });
+
+ if let Some(split) = best {
+ let path = split.path.clone();
+ let current_ratio = get_ratio_at(&self.root, &path).unwrap_or(0.5);
+ let adj = if grows { delta } else { -delta };
+ self.set_ratio_at(&path, current_ratio + adj);
+ }
+ }
+
+ pub fn pane_ids(&self) -> Vec<PaneId> {
+ let mut ids = Vec::new();
+ collect_ids(&self.root, &mut ids);
+ ids
+ }
+
+ /// Access the tree root for serialization.
+ pub fn root(&self) -> &Node {
+ &self.root
+ }
+
+ /// Reconstruct a layout from a saved tree.
+ /// Reconstruct a layout from a saved tree.
+ pub fn from_saved(root: Node, focus: PaneId) -> Self {
+ Self { root, focus }
+ }
+}
+
+// --- Directional pane navigation ---
+
+/// Find the nearest pane in the given direction from `focused`.
+pub fn find_in_direction(
+ focused: &PaneInfo,
+ direction: NavDirection,
+ panes: &[PaneInfo],
+) -> Option<PaneId> {
+ let fr = focused.rect;
+
+ panes
+ .iter()
+ .filter(|p| p.id != focused.id)
+ .filter(|p| {
+ let r = p.rect;
+ match direction {
+ NavDirection::Left => {
+ r.x + r.width <= fr.x && ranges_overlap(r.y, r.height, fr.y, fr.height)
+ }
+ NavDirection::Right => {
+ r.x >= fr.x + fr.width && ranges_overlap(r.y, r.height, fr.y, fr.height)
+ }
+ NavDirection::Up => {
+ r.y + r.height <= fr.y && ranges_overlap(r.x, r.width, fr.x, fr.width)
+ }
+ NavDirection::Down => {
+ r.y >= fr.y + fr.height && ranges_overlap(r.x, r.width, fr.x, fr.width)
+ }
+ }
+ })
+ .min_by_key(|p| {
+ let r = p.rect;
+ match direction {
+ NavDirection::Left => fr.x.saturating_sub(r.x + r.width),
+ NavDirection::Right => r.x.saturating_sub(fr.x + fr.width),
+ NavDirection::Up => fr.y.saturating_sub(r.y + r.height),
+ NavDirection::Down => r.y.saturating_sub(fr.y + fr.height),
+ }
+ })
+ .map(|p| p.id)
+}
+
+fn ranges_overlap(a_start: u16, a_len: u16, b_start: u16, b_len: u16) -> bool {
+ a_start < b_start + b_len && a_start + a_len > b_start
+}
+
+// --- Tree operations ---
+
+fn count_panes(node: &Node) -> usize {
+ match node {
+ Node::Pane(_) => 1,
+ Node::Split { first, second, .. } => count_panes(first) + count_panes(second),
+ }
+}
+
+fn collect_panes(node: &Node, area: Rect, focus: PaneId, result: &mut Vec<PaneInfo>) {
+ match node {
+ Node::Pane(id) => {
+ result.push(PaneInfo {
+ id: *id,
+ rect: area,
+ // inner_rect is set during render when we know if borders are shown
+ inner_rect: area,
+ is_focused: *id == focus,
+ });
+ }
+ Node::Split {
+ direction,
+ ratio,
+ first,
+ second,
+ } => {
+ let (a, b) = split_rect(area, *direction, *ratio);
+ collect_panes(first, a, focus, result);
+ collect_panes(second, b, focus, result);
+ }
+ }
+}
+
+fn collect_splits(node: &Node, area: Rect, path: Vec<bool>, result: &mut Vec<SplitBorder>) {
+ if let Node::Split {
+ direction,
+ ratio,
+ first,
+ second,
+ } = node
+ {
+ let (a, b) = split_rect(area, *direction, *ratio);
+ let pos = match direction {
+ Direction::Horizontal => a.x + a.width,
+ Direction::Vertical => a.y + a.height,
+ };
+ result.push(SplitBorder {
+ pos,
+ direction: *direction,
+ area,
+ path: path.clone(),
+ });
+ let mut lp = path.clone();
+ lp.push(false);
+ collect_splits(first, a, lp, result);
+ let mut rp = path;
+ rp.push(true);
+ collect_splits(second, b, rp, result);
+ }
+}
+
+fn collect_ids(node: &Node, ids: &mut Vec<PaneId>) {
+ match node {
+ Node::Pane(id) => ids.push(*id),
+ Node::Split { first, second, .. } => {
+ collect_ids(first, ids);
+ collect_ids(second, ids);
+ }
+ }
+}
+
+fn split_at(node: Node, target: PaneId, direction: Direction, new_id: PaneId) -> Node {
+ match node {
+ Node::Pane(id) if id == target => Node::Split {
+ direction,
+ ratio: 0.5,
+ first: Box::new(Node::Pane(id)),
+ second: Box::new(Node::Pane(new_id)),
+ },
+ Node::Pane(_) => node,
+ Node::Split {
+ direction: d,
+ ratio,
+ first,
+ second,
+ } => Node::Split {
+ direction: d,
+ ratio,
+ first: Box::new(split_at(*first, target, direction, new_id)),
+ second: Box::new(split_at(*second, target, direction, new_id)),
+ },
+ }
+}
+
+fn remove_pane(node: Node, target: PaneId) -> Option<Node> {
+ match node {
+ Node::Pane(id) if id == target => None,
+ Node::Pane(_) => Some(node),
+ Node::Split {
+ direction,
+ ratio,
+ first,
+ second,
+ } => match (remove_pane(*first, target), remove_pane(*second, target)) {
+ (None, Some(s)) => Some(s),
+ (Some(f), None) => Some(f),
+ (Some(f), Some(s)) => Some(Node::Split {
+ direction,
+ ratio,
+ first: Box::new(f),
+ second: Box::new(s),
+ }),
+ (None, None) => None,
+ },
+ }
+}
+
+fn set_ratio_at(node: &mut Node, path: &[bool], new_ratio: f32) {
+ if let Node::Split {
+ ratio,
+ first,
+ second,
+ ..
+ } = node
+ {
+ if path.is_empty() {
+ *ratio = new_ratio;
+ } else if path[0] {
+ set_ratio_at(second, &path[1..], new_ratio);
+ } else {
+ set_ratio_at(first, &path[1..], new_ratio);
+ }
+ }
+}
+
+fn get_ratio_at(node: &Node, path: &[bool]) -> Option<f32> {
+ if let Node::Split {
+ ratio,
+ first,
+ second,
+ ..
+ } = node
+ {
+ if path.is_empty() {
+ Some(*ratio)
+ } else if path[0] {
+ get_ratio_at(second, &path[1..])
+ } else {
+ get_ratio_at(first, &path[1..])
+ }
+ } else {
+ None
+ }
+}
+
+fn split_rect(area: Rect, direction: Direction, ratio: f32) -> (Rect, Rect) {
+ match direction {
+ Direction::Horizontal => {
+ let first_w = ((area.width as f32) * ratio).round() as u16;
+ let second_w = area.width.saturating_sub(first_w);
+ (
+ Rect::new(area.x, area.y, first_w, area.height),
+ Rect::new(area.x + first_w, area.y, second_w, area.height),
+ )
+ }
+ Direction::Vertical => {
+ let first_h = ((area.height as f32) * ratio).round() as u16;
+ let second_h = area.height.saturating_sub(first_h);
+ (
+ Rect::new(area.x, area.y, area.width, first_h),
+ Rect::new(area.x, area.y + first_h, area.width, second_h),
+ )
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 00000000..a76b913e
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,228 @@
+use std::io;
+
+use crossterm::event::{
+ DisableBracketedPaste, DisableMouseCapture, EnableBracketedPaste, EnableMouseCapture,
+ KeyboardEnhancementFlags, PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags,
+};
+use crossterm::execute;
+use tracing::info;
+
+mod app;
+mod config;
+mod detect;
+mod events;
+mod input;
+mod layout;
+mod pane;
+mod persist;
+mod platform;
+mod pty_callbacks;
+mod selection;
+mod ui;
+mod update;
+mod workspace;
+
+fn init_logging() {
+ use std::fs::{self, OpenOptions};
+ use tracing_subscriber::EnvFilter;
+
+ let log_dir = if let Ok(dir) = std::env::var("XDG_CONFIG_HOME") {
+ std::path::PathBuf::from(dir).join("herdr")
+ } else if let Ok(home) = std::env::var("HOME") {
+ std::path::PathBuf::from(home).join(".config/herdr")
+ } else {
+ std::path::PathBuf::from("/tmp/herdr")
+ };
+ let _ = fs::create_dir_all(&log_dir);
+ let log_path = log_dir.join("herdr.log");
+
+ // Rotate: truncate if over 5MB
+ if let Ok(meta) = fs::metadata(&log_path) {
+ if meta.len() > 5 * 1024 * 1024 {
+ let _ = fs::remove_file(&log_path);
+ }
+ }
+
+ let file = match OpenOptions::new().create(true).append(true).open(&log_path) {
+ Ok(f) => f,
+ Err(_) => return, // can't open log file, proceed without logging
+ };
+
+ let filter = EnvFilter::try_from_env("HERDR_LOG")
+ .unwrap_or_else(|_| EnvFilter::new("herdr=info"));
+
+ tracing_subscriber::fmt()
+ .with_env_filter(filter)
+ .with_writer(file)
+ .with_ansi(false)
+ .with_target(false)
+ .init();
+}
+
+const DEFAULT_CONFIG: &str = r#"# herdr configuration
+# Place this file at ~/.config/herdr/config.toml
+
+[keys]
+# Prefix key to enter navigate mode (e.g. "ctrl+s", "ctrl+a", "ctrl+b")
+# prefix = "ctrl+s"
+
+# Pane controls (in navigate mode)
+# split_vertical = "v"
+# split_horizontal = "-"
+# close_pane = "x"
+# fullscreen = "f"
+
+[ui]
+# Sidebar width (auto-scaled based on workspace names, this sets the default)
+# sidebar_width = 26
+
+# Ask for confirmation before closing a workspace
+# confirm_close = true
+
+# Accent color for highlights, borders, and navigation UI.
+# Accepts: hex (#89b4fa), named colors (cyan, blue, magenta), or rgb(r,g,b)
+# accent = "cyan"
+"#;
+
+fn main() -> io::Result<()> {
+ let args: Vec<String> = std::env::args().collect();
+
+ // Subcommands and flags (no TUI, no logging needed)
+ if args.get(1).map(|s| s.as_str()) == Some("update") {
+ match update::self_update() {
+ Ok(_) => return Ok(()),
+ Err(e) => {
+ eprintln!("update failed: {e}");
+ std::process::exit(1);
+ }
+ }
+ }
+
+ if args.iter().any(|a| a == "--help" || a == "-h") {
+ println!("herdr — terminal workspace manager for AI coding agents");
+ println!();
+ println!("Usage: herdr [options]");
+ println!(" herdr update");
+ println!();
+ println!("Commands:");
+ println!(" update Download and install the latest version");
+ println!();
+ println!("Options:");
+ println!(" --no-session Don't restore or save sessions");
+ println!(" --default-config Print default configuration and exit");
+ println!(" --version, -V Print version and exit");
+ println!(" --help, -h Show this help");
+ println!();
+ println!("Config: ~/.config/herdr/config.toml");
+ println!("Logs: ~/.config/herdr/herdr.log");
+ println!("Home: https://herdr.dev");
+ return Ok(());
+ }
+
+ if args.iter().any(|a| a == "--version" || a == "-V") {
+ println!("herdr {}", env!("CARGO_PKG_VERSION"));
+ return Ok(());
+ }
+
+ if args.iter().any(|a| a == "--default-config") {
+ print!("{DEFAULT_CONFIG}");
+ return Ok(());
+ }
+
+ // Reject unknown flags
+ let known_flags = ["--no-session", "--version", "-V", "--default-config", "--help", "-h"];
+ for arg in &args[1..] {
+ if arg.starts_with('-') && !known_flags.contains(&arg.as_str()) {
+ eprintln!("unknown option: {arg}");
+ eprintln!("run 'herdr --help' for usage");
+ std::process::exit(1);
+ }
+ if !arg.starts_with('-') && arg != "update" {
+ eprintln!("unknown command: {arg}");
+ eprintln!("run 'herdr --help' for usage");
+ std::process::exit(1);
+ }
+ }
+
+ init_logging();
+
+ let no_session = std::env::args().any(|a| a == "--no-session");
+ let in_tmux = std::env::var("TMUX").is_ok();
+
+ let original_hook = std::panic::take_hook();
+ let panic_in_tmux = in_tmux;
+ std::panic::set_hook(Box::new(move |info| {
+ tracing::error!("PANIC: {info}");
+ if panic_in_tmux {
+ let _ = std::io::Write::write_all(&mut io::stdout(), b"\x1b[>4;0m");
+ }
+ let _ = execute!(
+ io::stdout(),
+ PopKeyboardEnhancementFlags,
+ DisableBracketedPaste,
+ DisableMouseCapture
+ );
+ ratatui::restore();
+ original_hook(info);
+ }));
+
+ let config = config::Config::load();
+ info!("herdr starting, pid={}", std::process::id());
+
+ // Background auto-update (non-blocking, best-effort)
+ // Downloads and installs new version silently, notifies TUI when done.
+ // Skipped in --no-session mode (testing).
+
+ let rt = tokio::runtime::Builder::new_multi_thread()
+ .enable_all()
+ .build()
+ .expect("failed to create tokio runtime");
+
+ let result = rt.block_on(async {
+ let mut terminal = ratatui::init();
+ execute!(
+ io::stdout(),
+ EnableMouseCapture,
+ EnableBracketedPaste,
+ PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES)
+ )?;
+
+ // tmux doesn't understand kitty keyboard protocol push (\e[>1u).
+ // It uses modifyOtherKeys mode to send CSI u sequences for modified keys.
+ // Enable modifyOtherKeys mode 2 so tmux sends Shift+Enter as \e[13;2u etc.
+ if in_tmux {
+ use std::io::Write;
+ std::io::stdout().write_all(b"\x1b[>4;2m")?;
+ std::io::stdout().flush()?;
+ }
+
+ let mut app = app::App::new(&config, no_session);
+ let result = app.run(&mut terminal).await;
+
+ // Reset modifyOtherKeys if we enabled it
+ if in_tmux {
+ use std::io::Write;
+ std::io::stdout().write_all(b"\x1b[>4;0m")?;
+ std::io::stdout().flush()?;
+ }
+
+ execute!(
+ io::stdout(),
+ PopKeyboardEnhancementFlags,
+ DisableBracketedPaste,
+ DisableMouseCapture
+ )?;
+ ratatui::restore();
+
+ // Drop app (and all workspaces/panes) before runtime shuts down
+ drop(app);
+
+ result
+ });
+
+ // Shut down runtime immediately — kills lingering PTY reader/writer tasks
+ rt.shutdown_timeout(std::time::Duration::from_millis(100));
+
+ info!("herdr exiting");
+ result
+}
diff --git a/src/pane.rs b/src/pane.rs
new file mode 100644
index 00000000..821cdec9
--- /dev/null
+++ b/src/pane.rs
@@ -0,0 +1,370 @@
+use std::io::{BufWriter, Read, Write};
+use std::cell::Cell;
+use std::sync::{
+ atomic::{AtomicBool, AtomicU32, Ordering},
+ Arc, RwLock,
+};
+
+use bytes::Bytes;
+use portable_pty::{native_pty_system, CommandBuilder, PtySize};
+use tokio::sync::mpsc;
+use tracing::{debug, error, info, warn};
+
+use crate::detect::{Agent, AgentState};
+use crate::events::AppEvent;
+use crate::layout::PaneId;
+use crate::pty_callbacks::PtyResponses;
+
+// ---------------------------------------------------------------------------
+// PaneState — pure data, constructable without PTYs, testable
+// ---------------------------------------------------------------------------
+
+/// Observable state for a single pane.
+/// This is the only part of a pane that workspace logic and tests need.
+pub struct PaneState {
+ pub detected_agent: Option<Agent>,
+ pub state: AgentState,
+ /// Whether the user has seen this pane since its last state change to Idle.
+ /// False = "Done" (agent finished while user was in another workspace).
+ pub seen: bool,
+}
+
+impl PaneState {
+ pub fn new() -> Self {
+ Self {
+ detected_agent: None,
+ state: AgentState::Unknown,
+ seen: true,
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// PaneRuntime — PTY, parser, channels, background tasks
+// ---------------------------------------------------------------------------
+
+/// PTY runtime for a pane. Owns the terminal, I/O channels, and background tasks.
+/// Dropping this shuts down all background tasks and closes the PTY.
+pub struct PaneRuntime {
+ pub parser: Arc<RwLock<vt100::Parser<PtyResponses>>>,
+ pub sender: mpsc::Sender<Bytes>,
+ resize_tx: mpsc::Sender<(u16, u16)>,
+ current_size: Cell<(u16, u16)>,
+ child_pid: Arc<AtomicU32>,
+ pub kitty_keyboard: Arc<AtomicBool>,
+ /// Live screen content snapshot — updated by reader, read by detector.
+ /// Decouples detection from parser viewport state (scrollback).
+ /// Kept alive here so the Arc isn't dropped; tasks hold their own clones.
+ #[allow(dead_code)]
+ screen_content: Arc<RwLock<String>>,
+ // Task handles for deterministic shutdown
+ detect_handle: tokio::task::AbortHandle,
+}
+
+impl Drop for PaneRuntime {
+ fn drop(&mut self) {
+ // Abort detection task immediately.
+ // Reader/writer/resize tasks shut down naturally via channel close
+ // and PTY EOF when the rest of PaneRuntime is dropped.
+ self.detect_handle.abort();
+ }
+}
+
+impl PaneRuntime {
+ pub fn spawn(
+ pane_id: PaneId,
+ rows: u16,
+ cols: u16,
+ cwd: std::path::PathBuf,
+ events: mpsc::Sender<AppEvent>,
+ ) -> std::io::Result<Self> {
+ let pty_system = native_pty_system();
+ let pair = pty_system
+ .openpty(PtySize {
+ rows,
+ cols,
+ pixel_width: 0,
+ pixel_height: 0,
+ })
+ .map_err(|e| std::io::Error::other(e.to_string()))?;
+
+ let responses = PtyResponses::new();
+ let kitty_keyboard = responses.kitty_keyboard.clone();
+ let parser = Arc::new(RwLock::new(vt100::Parser::new_with_callbacks(
+ rows, cols, 10000, responses.clone(),
+ )));
+
+ let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".into());
+ let mut cmd = CommandBuilder::new(&shell);
+ cmd.cwd(cwd);
+
+ let reader = pair
+ .master
+ .try_clone_reader()
+ .map_err(|e| std::io::Error::other(e.to_string()))?;
+ let writer = pair
+ .master
+ .take_writer()
+ .map_err(|e| std::io::Error::other(e.to_string()))?;
+
+ // --- Child watcher task ---
+ let child_pid = Arc::new(AtomicU32::new(0));
+ {
+ let child_pid = child_pid.clone();
+ let slave = pair.slave;
+ let events = events.clone();
+ let rt = tokio::runtime::Handle::current();
+ tokio::task::spawn_blocking(move || {
+ match slave.spawn_command(cmd) {
+ Ok(mut child) => {
+ if let Some(pid) = child.process_id() {
+ child_pid.store(pid, Ordering::Release);
+ info!(pane = pane_id.raw(), pid, "child spawned");
+ }
+ match child.wait() {
+ Ok(status) => info!(pane = pane_id.raw(), ?status, "child exited"),
+ Err(e) => error!(pane = pane_id.raw(), err = %e, "child wait failed"),
+ }
+ }
+ Err(e) => error!(pane = pane_id.raw(), err = %e, "failed to spawn shell"),
+ }
+ // Use blocking send — PaneDied is critical, must not be dropped
+ if let Err(e) = rt.block_on(events.send(AppEvent::PaneDied { pane_id })) {
+ error!(pane = pane_id.raw(), err = %e, "failed to send PaneDied event");
+ }
+ });
+ }
+
+ // --- Writer channel ---
+ let (input_tx, mut input_rx) = mpsc::channel::<Bytes>(32);
+
+ // Live screen snapshot for detection (decoupled from parser scrollback)
+ let screen_content = Arc::new(RwLock::new(String::new()));
+
+ // --- Reader task: PTY → parser + screen snapshot + terminal query responses ---
+ {
+ let mut reader = reader;
+ let parser = parser.clone();
+ let screen_content = screen_content.clone();
+ let response_writer = input_tx.clone();
+ tokio::task::spawn_blocking(move || {
+ let mut buf = [0u8; 8192];
+ loop {
+ match reader.read(&mut buf) {
+ Ok(0) => break,
+ Err(e) => {
+ debug!(pane = pane_id.raw(), err = %e, "pty reader closed");
+ break;
+ }
+ Ok(n) => {
+ if let Ok(mut p) = parser.write() {
+ p.process(&buf[..n]);
+ // Snapshot live screen content for detection.
+ // Always reads at scrollback 0 (current view),
+ // without touching the user's scroll position.
+ let scrollback = p.screen().scrollback();
+ if scrollback > 0 {
+ p.screen_mut().set_scrollback(0);
+ }
+ let content = p.screen().contents();
+ if scrollback > 0 {
+ p.screen_mut().set_scrollback(scrollback);
+ }
+ if let Ok(mut sc) = screen_content.write() {
+ *sc = content;
+ }
+ } else {
+ error!(pane = pane_id.raw(), "parser lock poisoned in reader");
+ break;
+ }
+ let resp = responses.take();
+ if !resp.is_empty() {
+ if let Err(e) = response_writer.try_send(Bytes::from(resp)) {
+ warn!(pane = pane_id.raw(), err = %e, "dropped terminal query response");
+ }
+ }
+ }
+ }
+ }
+ debug!(pane = pane_id.raw(), "reader task exiting");
+ });
+ }
+
+ // --- Detection task ---
+ let detect_handle = {
+ use crate::detect;
+ use std::time::{Duration, Instant};
+
+ const TICK_UNIDENTIFIED: Duration = Duration::from_millis(500);
+ const TICK_IDENTIFIED: Duration = Duration::from_millis(300);
+ const PROCESS_RECHECK: Duration = Duration::from_secs(5);
+
+ let child_pid = child_pid.clone();
+ let screen_content = screen_content.clone();
+ let state_events = events.clone();
+
+ let handle = tokio::spawn(async move {
+ let mut agent: Option<Agent> = None;
+ let mut state = AgentState::Unknown;
+ let mut last_process_check = Instant::now();
+
+ tokio::time::sleep(Duration::from_millis(50)).await;
+
+ loop {
+ let tick = if agent.is_none() {
+ TICK_UNIDENTIFIED
+ } else {
+ TICK_IDENTIFIED
+ };
+ tokio::time::sleep(tick).await;
+
+ let now = Instant::now();
+ let should_check_process = agent.is_none()
+ || now.duration_since(last_process_check) >= PROCESS_RECHECK;
+
+ let mut agent_changed = false;
+ if should_check_process {
+ last_process_check = now;
+ let pid = child_pid.load(Ordering::Acquire);
+ if pid > 0 {
+ if let Some(name) = detect::foreground_process_name(pid) {
+ let new_agent = detect::identify_agent(&name);
+ if new_agent != agent {
+ info!(
+ pane = pane_id.raw(),
+ ?new_agent,
+ process = %name,
+ "agent changed"
+ );
+ agent = new_agent;
+ agent_changed = true;
+ }
+ }
+ }
+ }
+
+ let new_state = if let Ok(content) = screen_content.read() {
+ detect::detect_state(agent, &content)
+ } else {
+ continue;
+ };
+
+ if new_state != state || agent_changed {
+ debug!(
+ pane = pane_id.raw(),
+ ?state,
+ ?new_state,
+ ?agent,
+ "state changed"
+ );
+ state = new_state;
+ if let Err(e) = state_events.try_send(AppEvent::StateChanged {
+ pane_id,
+ agent,
+ state: new_state,
+ }) {
+ warn!(
+ pane = pane_id.raw(),
+ err = %e,
+ "dropped StateChanged event"
+ );
+ }
+ }
+ }
+ });
+ handle.abort_handle()
+ };
+
+ // --- Writer task: channel → PTY ---
+ {
+ let mut writer = BufWriter::new(writer);
+ tokio::task::spawn_blocking(move || {
+ let rt = tokio::runtime::Handle::current();
+ while let Some(bytes) = rt.block_on(input_rx.recv()) {
+ if let Err(e) = writer.write_all(&bytes) {
+ warn!(pane = pane_id.raw(), err = %e, "pty write failed");
+ break;
+ }
+ if let Err(e) = writer.flush() {
+ warn!(pane = pane_id.raw(), err = %e, "pty flush failed");
+ break;
+ }
+ }
+ debug!(pane = pane_id.raw(), "writer task exiting");
+ });
+ }
+
+ // --- Resize task ---
+ let (resize_tx, mut resize_rx) = mpsc::channel::<(u16, u16)>(4);
+ {
+ let master = pair.master;
+ tokio::task::spawn_blocking(move || {
+ let rt = tokio::runtime::Handle::current();
+ while let Some((rows, cols)) = rt.block_on(resize_rx.recv()) {
+ if let Err(e) = master.resize(PtySize {
+ rows,
+ cols,
+ pixel_width: 0,
+ pixel_height: 0,
+ }) {
+ warn!(pane = pane_id.raw(), err = %e, rows, cols, "pty resize failed");
+ }
+ }
+ });
+ }
+
+ Ok(Self {
+ parser,
+ sender: input_tx,
+ resize_tx,
+ current_size: Cell::new((rows, cols)),
+ child_pid,
+ kitty_keyboard,
+ screen_content,
+ detect_handle,
+ })
+ }
+
+ /// Resize if the dimensions actually changed.
+ pub fn resize(&self, rows: u16, cols: u16) {
+ let rows = rows.max(2);
+ let cols = cols.max(4);
+ if self.current_size.get() == (rows, cols) {
+ return;
+ }
+ self.current_size.set((rows, cols));
+ if let Ok(mut p) = self.parser.write() {
+ p.screen_mut().set_size(rows, cols);
+ }
+ let _ = self.resize_tx.try_send((rows, cols));
+ }
+
+ /// Scroll up by N lines (into scrollback history).
+ pub fn scroll_up(&self, lines: usize) {
+ if let Ok(mut p) = self.parser.write() {
+ let current = p.screen().scrollback();
+ p.screen_mut().set_scrollback(current + lines);
+ }
+ }
+
+ /// Scroll down by N lines (toward live output).
+ pub fn scroll_down(&self, lines: usize) {
+ if let Ok(mut p) = self.parser.write() {
+ let current = p.screen().scrollback();
+ p.screen_mut().set_scrollback(current.saturating_sub(lines));
+ }
+ }
+
+ /// Reset scroll to live view (offset = 0).
+ pub fn scroll_reset(&self) {
+ if let Ok(mut p) = self.parser.write() {
+ p.screen_mut().set_scrollback(0);
+ }
+ }
+
+ /// Get the current working directory of the child shell process.
+ pub fn cwd(&self) -> Option<std::path::PathBuf> {
+ let pid = self.child_pid.load(Ordering::Relaxed);
+ crate::platform::process_cwd(pid)
+ }
+}
diff --git a/src/persist.rs b/src/persist.rs
new file mode 100644
index 00000000..31a37576
--- /dev/null
+++ b/src/persist.rs
@@ -0,0 +1,446 @@
+//! Session persistence — save/restore workspaces, layouts, and working directories.
+//!
+//! Stored at `~/.config/herdr/session.json`.
+
+use std::collections::HashMap;
+use std::path::PathBuf;
+
+use ratatui::layout::Direction;
+use serde::{Deserialize, Serialize};
+use tracing::{error, info, warn};
+
+use tokio::sync::mpsc;
+
+use crate::events::AppEvent;
+use crate::layout::{Node, PaneId, TileLayout};
+use crate::pane::{PaneRuntime, PaneState};
+use crate::workspace::Workspace;
+
+/// Current snapshot format version.
+const SNAPSHOT_VERSION: u32 = 1;
+
+/// Serializable snapshot of the entire herdr session.
+#[derive(Serialize, Deserialize)]
+pub struct SessionSnapshot {
+ /// Format version — used to detect incompatible changes.
+ #[serde(default)]
+ pub version: u32,
+ pub workspaces: Vec<WorkspaceSnapshot>,
+ pub active: Option<usize>,
+ pub selected: usize,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct WorkspaceSnapshot {
+ pub name: String,
+ pub layout: LayoutSnapshot,
+ pub panes: HashMap<u32, PaneSnapshot>,
+ pub zoomed: bool,
+ /// Raw pane ID that was focused when saved.
+ #[serde(default)]
+ pub focused: Option<u32>,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct PaneSnapshot {
+ pub cwd: PathBuf,
+}
+
+/// Serializable BSP tree.
+#[derive(Serialize, Deserialize)]
+pub enum LayoutSnapshot {
+ Pane(u32),
+ Split {
+ direction: DirectionSnapshot,
+ ratio: f32,
+ first: Box<LayoutSnapshot>,
+ second: Box<LayoutSnapshot>,
+ },
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum DirectionSnapshot {
+ Horizontal,
+ Vertical,
+}
+
+// --- Capture ---
+
+/// Capture the current app state into a serializable snapshot.
+pub fn capture(
+ workspaces: &[Workspace],
+ active: Option<usize>,
+ selected: usize,
+) -> SessionSnapshot {
+ SessionSnapshot {
+ version: SNAPSHOT_VERSION,
+ workspaces: workspaces.iter().map(capture_workspace).collect(),
+ active,
+ selected,
+ }
+}
+
+fn capture_workspace(ws: &Workspace) -> WorkspaceSnapshot {
+ let mut panes = HashMap::new();
+ for id in ws.panes.keys() {
+ let cwd = ws.runtimes.get(id)
+ .and_then(|rt| rt.cwd())
+ .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| "/".into()));
+ panes.insert(id.raw(), PaneSnapshot { cwd });
+ }
+ WorkspaceSnapshot {
+ name: ws.name.clone(),
+ layout: capture_node(ws.layout.root()),
+ panes,
+ zoomed: ws.zoomed,
+ focused: Some(ws.layout.focused().raw()),
+ }
+}
+
+fn capture_node(node: &Node) -> LayoutSnapshot {
+ match node {
+ Node::Pane(id) => LayoutSnapshot::Pane(id.raw()),
+ Node::Split {
+ direction,
+ ratio,
+ first,
+ second,
+ } => LayoutSnapshot::Split {
+ direction: match direction {
+ Direction::Horizontal => DirectionSnapshot::Horizontal,
+ Direction::Vertical => DirectionSnapshot::Vertical,
+ },
+ ratio: *ratio,
+ first: Box::new(capture_node(first)),
+ second: Box::new(capture_node(second)),
+ },
+ }
+}
+
+// --- Restore ---
+
+/// Restore workspaces from a snapshot. Each pane gets a fresh shell in its saved cwd.
+pub fn restore(
+ snapshot: &SessionSnapshot,
+ rows: u16,
+ cols: u16,
+ events: mpsc::Sender<AppEvent>,
+) -> Vec<Workspace> {
+ snapshot
+ .workspaces
+ .iter()
+ .filter_map(|ws_snap| restore_workspace(ws_snap, rows, cols, events.clone()))
+ .collect()
+}
+
+fn restore_workspace(
+ snap: &WorkspaceSnapshot,
+ rows: u16,
+ cols: u16,
+ events: mpsc::Sender<AppEvent>,
+) -> Option<Workspace> {
+ let (node, id_map) = restore_node_remapped(&snap.layout);
+ let pane_ids = collect_pane_ids(&node);
+
+ // Restore focused pane: map saved raw ID to new ID, fall back to first pane
+ let focus = snap
+ .focused
+ .and_then(|old_raw| id_map.get(&old_raw).copied())
+ .or_else(|| pane_ids.first().copied())
+ .unwrap_or(PaneId::from_raw(0));
+ let layout = TileLayout::from_saved(node, focus);
+
+ let mut panes = HashMap::new();
+ let mut runtimes = HashMap::new();
+ for id in &pane_ids {
+ let old_id = id_map.iter().find(|(_, new)| **new == *id).map(|(old, _)| old);
+ let cwd = old_id
+ .and_then(|old| snap.panes.get(old))
+ .map(|p| p.cwd.clone())
+ .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| "/".into()));
+
+ match PaneRuntime::spawn(*id, rows, cols, cwd, events.clone()) {
+ Ok(runtime) => {
+ panes.insert(*id, PaneState::new());
+ runtimes.insert(*id, runtime);
+ }
+ Err(e) => {
+ error!(workspace = %snap.name, err = %e, "failed to restore pane");
+ return None;
+ }
+ }
+ }
+
+ Some(Workspace {
+ name: snap.name.clone(),
+ layout,
+ panes,
+ runtimes,
+ zoomed: snap.zoomed,
+ events,
+ })
+}
+
+/// Restore a layout tree, remapping every pane ID to a fresh globally unique one.
+/// Returns the new tree and a map of old_raw_id → new PaneId.
+fn restore_node_remapped(snap: &LayoutSnapshot) -> (Node, HashMap<u32, PaneId>) {
+ let mut id_map = HashMap::new();
+ let node = remap_inner(snap, &mut id_map);
+ (node, id_map)
+}
+
+fn remap_inner(snap: &LayoutSnapshot, id_map: &mut HashMap<u32, PaneId>) -> Node {
+ match snap {
+ LayoutSnapshot::Pane(old_id) => {
+ let new_id = PaneId::alloc();
+ id_map.insert(*old_id, new_id);
+ Node::Pane(new_id)
+ }
+ LayoutSnapshot::Split {
+ direction,
+ ratio,
+ first,
+ second,
+ } => {
+ let first_node = remap_inner(first, id_map);
+ let second_node = remap_inner(second, id_map);
+ let dir = match direction {
+ DirectionSnapshot::Horizontal => Direction::Horizontal,
+ DirectionSnapshot::Vertical => Direction::Vertical,
+ };
+ Node::Split {
+ direction: dir,
+ ratio: *ratio,
+ first: Box::new(first_node),
+ second: Box::new(second_node),
+ }
+ }
+ }
+}
+
+fn collect_pane_ids(node: &Node) -> Vec<PaneId> {
+ let mut ids = Vec::new();
+ collect_ids_inner(node, &mut ids);
+ ids
+}
+
+fn collect_ids_inner(node: &Node, ids: &mut Vec<PaneId>) {
+ match node {
+ Node::Pane(id) => ids.push(*id),
+ Node::Split { first, second, .. } => {
+ collect_ids_inner(first, ids);
+ collect_ids_inner(second, ids);
+ }
+ }
+}
+
+// --- File I/O ---
+
+fn session_path() -> PathBuf {
+ if let Ok(dir) = std::env::var("XDG_CONFIG_HOME") {
+ PathBuf::from(dir).join("herdr/session.json")
+ } else if let Ok(home) = std::env::var("HOME") {
+ PathBuf::from(home).join(".config/herdr/session.json")
+ } else {
+ PathBuf::from("/tmp/herdr/session.json")
+ }
+}
+
+pub fn save(snapshot: &SessionSnapshot) {
+ let path = session_path();
+ if let Some(parent) = path.parent() {
+ if let Err(e) = std::fs::create_dir_all(parent) {
+ error!(err = %e, "failed to create session directory");
+ return;
+ }
+ }
+ let json = match serde_json::to_string_pretty(snapshot) {
+ Ok(j) => j,
+ Err(e) => {
+ error!(err = %e, "failed to serialize session");
+ return;
+ }
+ };
+ // Atomic write: write to temp file, then rename
+ let tmp_path = path.with_extension("json.tmp");
+ if let Err(e) = std::fs::write(&tmp_path, &json) {
+ error!(err = %e, "failed to write session temp file");
+ return;
+ }
+ if let Err(e) = std::fs::rename(&tmp_path, &path) {
+ error!(err = %e, "failed to rename session file");
+ // Clean up temp file
+ let _ = std::fs::remove_file(&tmp_path);
+ return;
+ }
+ info!(workspaces = snapshot.workspaces.len(), "session saved");
+}
+
+pub fn load() -> Option<SessionSnapshot> {
+ let path = session_path();
+ if !path.exists() {
+ return None;
+ }
+ let content = match std::fs::read_to_string(&path) {
+ Ok(c) => c,
+ Err(e) => {
+ warn!(err = %e, "failed to read session file");
+ return None;
+ }
+ };
+ match serde_json::from_str::<SessionSnapshot>(&content) {
+ Ok(snap) => {
+ if snap.version > SNAPSHOT_VERSION {
+ warn!(
+ file_version = snap.version,
+ supported = SNAPSHOT_VERSION,
+ "session file is from a newer herdr version, ignoring"
+ );
+ return None;
+ }
+ Some(snap)
+ }
+ Err(e) => {
+ warn!(err = %e, "failed to parse session file, ignoring");
+ None
+ }
+ }
+}
+
+// --- Tests ---
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn round_trip_empty_session() {
+ let snap = SessionSnapshot {
+ version: 1,
+ workspaces: vec![],
+ active: None,
+ selected: 0,
+ };
+ let json = serde_json::to_string(&snap).unwrap();
+ let restored: SessionSnapshot = serde_json::from_str(&json).unwrap();
+ assert!(restored.workspaces.is_empty());
+ assert_eq!(restored.active, None);
+ }
+
+ #[test]
+ fn round_trip_layout_snapshot() {
+ let layout = LayoutSnapshot::Split {
+ direction: DirectionSnapshot::Horizontal,
+ ratio: 0.6,
+ first: Box::new(LayoutSnapshot::Pane(0)),
+ second: Box::new(LayoutSnapshot::Split {
+ direction: DirectionSnapshot::Vertical,
+ ratio: 0.5,
+ first: Box::new(LayoutSnapshot::Pane(1)),
+ second: Box::new(LayoutSnapshot::Pane(2)),
+ }),
+ };
+ let json = serde_json::to_string(&layout).unwrap();
+ let restored: LayoutSnapshot = serde_json::from_str(&json).unwrap();
+
+ // Verify structure
+ match restored {
+ LayoutSnapshot::Split { ratio, .. } => assert!((ratio - 0.6).abs() < 0.01),
+ _ => panic!("expected split"),
+ }
+ }
+
+ #[test]
+ fn round_trip_full_workspace_snapshot() {
+ let mut panes = HashMap::new();
+ panes.insert(
+ 0,
+ PaneSnapshot {
+ cwd: PathBuf::from("/home/can/Projects/herdr"),
+ },
+ );
+ panes.insert(
+ 1,
+ PaneSnapshot {
+ cwd: PathBuf::from("/home/can/Projects/website"),
+ },
+ );
+
+ let snap = SessionSnapshot {
+ workspaces: vec![WorkspaceSnapshot {
+ name: "pi-mono".to_string(),
+ layout: LayoutSnapshot::Split {
+ direction: DirectionSnapshot::Horizontal,
+ ratio: 0.5,
+ first: Box::new(LayoutSnapshot::Pane(0)),
+ second: Box::new(LayoutSnapshot::Pane(1)),
+ },
+ panes,
+ zoomed: false,
+ focused: Some(0),
+ }],
+ active: Some(0),
+ selected: 0,
+ version: 1,
+ };
+
+ let json = serde_json::to_string_pretty(&snap).unwrap();
+ let restored: SessionSnapshot = serde_json::from_str(&json).unwrap();
+
+ assert_eq!(restored.workspaces.len(), 1);
+ assert_eq!(restored.workspaces[0].name, "pi-mono");
+ assert_eq!(restored.workspaces[0].panes.len(), 2);
+ assert_eq!(
+ restored.workspaces[0].panes[&0].cwd,
+ PathBuf::from("/home/can/Projects/herdr")
+ );
+ }
+
+ #[test]
+ fn capture_and_restore_node_round_trip() {
+ // Create a tree: Split(H, 0.5, Pane(0), Split(V, 0.3, Pane(1), Pane(2)))
+ let node = Node::Split {
+ direction: Direction::Horizontal,
+ ratio: 0.5,
+ first: Box::new(Node::Pane(PaneId::from_raw(0))),
+ second: Box::new(Node::Split {
+ direction: Direction::Vertical,
+ ratio: 0.3,
+ first: Box::new(Node::Pane(PaneId::from_raw(1))),
+ second: Box::new(Node::Pane(PaneId::from_raw(2))),
+ }),
+ };
+
+ let snap = capture_node(&node);
+ let (restored, id_map) = restore_node_remapped(&snap);
+
+ assert_eq!(id_map.len(), 3);
+ let ids = collect_pane_ids(&restored);
+ assert_eq!(ids.len(), 3);
+ let unique: std::collections::HashSet<u32> = ids.iter().map(|id| id.raw()).collect();
+ assert_eq!(unique.len(), 3);
+ }
+
+ #[test]
+ fn old_unversioned_snapshot_loads_as_version_0() {
+ // Simulate a snapshot from before versioning was added
+ let json = r#"{"workspaces":[],"active":null,"selected":0}"#;
+ let snap: SessionSnapshot = serde_json::from_str(json).unwrap();
+ assert_eq!(snap.version, 0); // #[serde(default)] gives 0
+ }
+
+ #[test]
+ fn future_version_is_rejected() {
+ let json = r#"{"version":999,"workspaces":[],"active":null,"selected":0}"#;
+ let snap: SessionSnapshot = serde_json::from_str(json).unwrap();
+ assert!(snap.version > SNAPSHOT_VERSION);
+ // load() would reject this — tested via the version check in load()
+ }
+
+ #[test]
+ fn focused_pane_default_is_none() {
+ let json = r#"{"name":"test","layout":{"Pane":0},"panes":{},"zoomed":false}"#;
+ let ws: WorkspaceSnapshot = serde_json::from_str(json).unwrap();
+ assert_eq!(ws.focused, None); // #[serde(default)]
+ }
+}
diff --git a/src/platform/fallback.rs b/src/platform/fallback.rs
new file mode 100644
index 00000000..735f7c11
--- /dev/null
+++ b/src/platform/fallback.rs
@@ -0,0 +1,11 @@
+use std::path::PathBuf;
+
+/// Unsupported platform stub.
+pub fn foreground_process_name(_child_pid: u32) -> Option<String> {
+ None
+}
+
+/// Unsupported platform stub.
+pub fn process_cwd(_pid: u32) -> Option<PathBuf> {
+ None
+}
diff --git a/src/platform/linux.rs b/src/platform/linux.rs
new file mode 100644
index 00000000..001ef03e
--- /dev/null
+++ b/src/platform/linux.rs
@@ -0,0 +1,27 @@
+use std::path::PathBuf;
+
+/// Get the foreground process name for a given child PID.
+/// Uses /proc/<pid>/stat to find the tpgid, then /proc/<tpgid>/comm.
+pub fn foreground_process_name(child_pid: u32) -> Option<String> {
+ // /proc/<pid>/stat format: "pid (comm) state ppid pgrp session tty_nr tpgid ..."
+ // The (comm) field can contain spaces and parens, so we find the last ')' first.
+ let stat = std::fs::read_to_string(format!("/proc/{child_pid}/stat")).ok()?;
+ let rest = stat.get(stat.rfind(')')? + 2..)?;
+ let fields: Vec<&str> = rest.split_whitespace().collect();
+ // After (comm): state(0) ppid(1) pgrp(2) session(3) tty_nr(4) tpgid(5)
+ let tpgid: i32 = fields.get(5)?.parse().ok()?;
+ if tpgid <= 0 {
+ return None;
+ }
+ let comm = std::fs::read_to_string(format!("/proc/{tpgid}/comm")).ok()?;
+ Some(comm.trim().to_string())
+}
+
+/// Get the current working directory of a process.
+/// Uses /proc/<pid>/cwd symlink.
+pub fn process_cwd(pid: u32) -> Option<PathBuf> {
+ if pid == 0 {
+ return None;
+ }
+ std::fs::read_link(format!("/proc/{pid}/cwd")).ok()
+}
diff --git a/src/platform/macos.rs b/src/platform/macos.rs
new file mode 100644
index 00000000..218d00e9
--- /dev/null
+++ b/src/platform/macos.rs
@@ -0,0 +1,98 @@
+use std::ffi::OsStr;
+use std::os::unix::ffi::OsStrExt;
+use std::path::PathBuf;
+
+/// Get the foreground process name for a given child PID.
+///
+/// Uses `proc_pidinfo(PROC_PIDTBSDINFO)` to read `e_tpgid` — the foreground
+/// process group ID of the child's controlling terminal. Then `proc_name()`
+/// to resolve that PID to a short command name.
+///
+/// This is the macOS equivalent of Linux's `/proc/{pid}/stat` → tpgid → `/proc/{tpgid}/comm`.
+pub fn foreground_process_name(child_pid: u32) -> Option<String> {
+ if child_pid == 0 {
+ return None;
+ }
+
+ // Get BSD info for the child process — contains e_tpgid
+ let mut info: libc::proc_bsdinfo = unsafe { std::mem::zeroed() };
+ let size = std::mem::size_of::<libc::proc_bsdinfo>() as libc::c_int;
+
+ let ret = unsafe {
+ libc::proc_pidinfo(
+ child_pid as libc::c_int,
+ libc::PROC_PIDTBSDINFO,
+ 0,
+ &mut info as *mut _ as *mut libc::c_void,
+ size,
+ )
+ };
+
+ if ret != size {
+ return None;
+ }
+
+ let fg_pid = info.e_tpgid;
+ if fg_pid == 0 {
+ return None;
+ }
+
+ // Resolve the foreground PID to a process name
+ let mut buf = [0u8; libc::MAXCOMLEN + 1];
+ let len = unsafe {
+ libc::proc_name(
+ fg_pid as libc::c_int,
+ buf.as_mut_ptr() as *mut libc::c_void,
+ buf.len() as u32,
+ )
+ };
+
+ if len <= 0 {
+ return None;
+ }
+
+ let len = len as usize;
+ let end = buf[..len].iter().position(|&b| b == 0).unwrap_or(len);
+ Some(String::from_utf8_lossy(&buf[..end]).into_owned())
+}
+
+/// Get the current working directory of a process.
+///
+/// Uses `proc_pidinfo(PROC_PIDVNODEPATHINFO)` to read `pvi_cdir.vip_path`.
+pub fn process_cwd(pid: u32) -> Option<PathBuf> {
+ if pid == 0 {
+ return None;
+ }
+
+ let mut pathinfo: libc::proc_vnodepathinfo = unsafe { std::mem::zeroed() };
+ let size = std::mem::size_of::<libc::proc_vnodepathinfo>() as libc::c_int;
+
+ let ret = unsafe {
+ libc::proc_pidinfo(
+ pid as libc::c_int,
+ libc::PROC_PIDVNODEPATHINFO,
+ 0,
+ &mut pathinfo as *mut _ as *mut libc::c_void,
+ size,
+ )
+ };
+
+ if ret != size {
+ return None;
+ }
+
+ // vip_path is [[c_char; 32]; 32] in libc (workaround for old Rust const generics).
+ // Reinterpret as flat bytes (total MAXPATHLEN = 1024).
+ let vip_path = unsafe {
+ std::slice::from_raw_parts(
+ pathinfo.pvi_cdir.vip_path.as_ptr() as *const u8,
+ libc::MAXPATHLEN as usize,
+ )
+ };
+
+ let nul = vip_path.iter().position(|&b| b == 0)?;
+ if nul == 0 {
+ return None;
+ }
+ Some(PathBuf::from(OsStr::from_bytes(&vip_path[..nul])))
+}
diff --git a/src/platform/mod.rs b/src/platform/mod.rs
new file mode 100644
index 00000000..df8869c7
--- /dev/null
+++ b/src/platform/mod.rs
@@ -0,0 +1,19 @@
+//! Platform-specific process and filesystem operations.
+//!
+//! Centralizes OS-dependent behavior behind a clean boundary so core
+//! modules don't scatter `#[cfg]` branches through product logic.
+
+#[cfg(target_os = "linux")]
+mod linux;
+#[cfg(target_os = "linux")]
+pub use linux::*;
+
+#[cfg(target_os = "macos")]
+mod macos;
+#[cfg(target_os = "macos")]
+pub use macos::*;
+
+#[cfg(not(any(target_os = "linux", target_os = "macos")))]
+mod fallback;
+#[cfg(not(any(target_os = "linux", target_os = "macos")))]
+pub use fallback::*;
diff --git a/src/pty_callbacks.rs b/src/pty_callbacks.rs
new file mode 100644
index 00000000..6d5971b0
--- /dev/null
+++ b/src/pty_callbacks.rs
@@ -0,0 +1,374 @@
+//! Terminal query responses.
+//!
+//! Programs like fzf, htop, and vim send escape sequences asking "what terminal am I?"
+//! and "where's the cursor?". A real terminal responds immediately. We need to do the same.
+//!
+//! There are ~15 query types that real programs use. We handle the common ones.
+//! The vt100 parser handles all OUTPUT sequences (formatting, cursor, etc.) — those
+//! don't need responses. Only QUERY sequences need us to reply.
+
+use std::sync::{
+ atomic::{AtomicBool, Ordering},
+ Arc, Mutex,
+};
+
+/// Collects response bytes that need to be written back to the PTY.
+/// Also tracks whether the child has requested the Kitty keyboard protocol.
+#[derive(Clone, Default)]
+pub struct PtyResponses {
+ pending: Arc<Mutex<Vec<u8>>>,
+ /// Stack of kitty keyboard enhancement flags pushed by child programs.
+ /// Each push adds a flags value; pop removes the top. This correctly handles
+ /// nested programs (e.g. fish pushes mode 1, neovim pushes mode 3, neovim
+ /// pops → fish's mode 1 is restored).
+ kitty_stack: Arc<Mutex<Vec<u16>>>,
+ /// Derived from kitty_stack: true when stack is non-empty.
+ /// Shared with PaneRuntime for fast-path key encoding decisions.
+ pub kitty_keyboard: Arc<AtomicBool>,
+}
+
+impl PtyResponses {
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ /// Take any pending response bytes (clears the buffer).
+ pub fn take(&self) -> Vec<u8> {
+ let mut pending = self.pending.lock().unwrap();
+ std::mem::take(&mut *pending)
+ }
+
+ fn push(&self, bytes: &[u8]) {
+ self.pending.lock().unwrap().extend_from_slice(bytes);
+ }
+}
+
+impl vt100::Callbacks for PtyResponses {
+ fn unhandled_csi(
+ &mut self,
+ screen: &mut vt100::Screen,
+ i1: Option<u8>,
+ _i2: Option<u8>,
+ params: &[&[u16]],
+ c: char,
+ ) {
+ let param0 = params.first().and_then(|p| p.first()).copied().unwrap_or(0);
+
+ match (i1, c) {
+ // === Device Attributes ===
+
+ // DA1: \e[c or \e[0c → "what terminal are you?"
+ // Respond as VT220 with ANSI color
+ (None, 'c') if param0 == 0 => {
+ self.push(b"\x1b[?62;22c");
+ }
+
+ // DA2: \e[>c or \e[>0c → "secondary device attributes"
+ (Some(b'>'), 'c') => {
+ // Type 0 (VT100), firmware version 0, ROM version 0
+ self.push(b"\x1b[>0;0;0c");
+ }
+
+ // === Cursor / Status Reports ===
+
+ // DSR: \e[Nn where N selects the report type
+ (None, 'n') => match param0 {
+ // CPR: \e[6n → cursor position report
+ 5 => {
+ // Device status: "OK"
+ self.push(b"\x1b[0n");
+ }
+ 6 => {
+ let (row, col) = screen.cursor_position();
+ let response = format!("\x1b[{};{}R", row + 1, col + 1);
+ self.push(response.as_bytes());
+ }
+ _ => {}
+ },
+
+ // DECXCPR: \e[?6n → extended cursor position report (with page)
+ (Some(b'?'), 'n') if param0 == 6 => {
+ let (row, col) = screen.cursor_position();
+ let response = format!("\x1b[?{};{}R", row + 1, col + 1);
+ self.push(response.as_bytes());
+ }
+
+ // === Mode Queries (DECRQM) ===
+
+ // DECRQM: \e[?Np → "is DEC private mode N set?"
+ // Response: \e[?N;Ps$y where Ps = 1 (set), 2 (reset), 0 (unknown)
+ (Some(b'?'), 'p') => {
+ // Report all queried modes as "reset" (2) — safe default
+ let response = format!("\x1b[?{param0};2$y");
+ self.push(response.as_bytes());
+ }
+
+ // ANSI DECRQM: \e[Np → "is ANSI mode N set?"
+ (None, 'p') => {
+ let response = format!("\x1b[{param0};2$y");
+ self.push(response.as_bytes());
+ }
+
+ // === Keyboard Protocol ===
+
+ // Kitty keyboard query: \e[?u → "what keyboard flags are active?"
+ (Some(b'?'), 'u') => {
+ let stack = self.kitty_stack.lock().unwrap();
+ let flags = stack.last().copied().unwrap_or(0);
+ self.push(format!("\x1b[?{flags}u").as_bytes());
+ }
+
+ // Kitty keyboard push: \e[>Nu → child wants Kitty key encoding
+ (Some(b'>'), 'u') => {
+ let mut stack = self.kitty_stack.lock().unwrap();
+ stack.push(param0);
+ self.kitty_keyboard.store(true, Ordering::Relaxed);
+ }
+
+ // Kitty keyboard pop: \e[<Nu → child reverts to previous mode
+ // N = number of entries to pop (default 1)
+ (Some(b'<'), 'u') => {
+ let mut stack = self.kitty_stack.lock().unwrap();
+ let count = (param0 as usize).max(1);
+ for _ in 0..count {
+ if stack.pop().is_none() {
+ break;
+ }
+ }
+ self.kitty_keyboard.store(!stack.is_empty(), Ordering::Relaxed);
+ }
+
+ // === Terminal Identification ===
+
+ // XTVERSION: \e[>q → "what terminal version?"
+ (Some(b'>'), 'q') => {
+ // Respond in DCS format: \eP>|herdr 0.1\e\\
+ self.push(b"\x1bP>|herdr 0.1\x1b\\");
+ }
+
+ _ => {}
+ }
+ }
+
+ fn unhandled_osc(
+ &mut self,
+ _screen: &mut vt100::Screen,
+ params: &[&[u8]],
+ ) {
+ let Some(cmd) = params.first() else { return };
+
+ match *cmd {
+ // OSC 10 ; ? ST → query foreground color
+ b"10" => {
+ if params.get(1) == Some(&&b"?"[..]) {
+ // Respond with a default light foreground
+ self.push(b"\x1b]10;rgb:cccc/cccc/cccc\x1b\\");
+ }
+ }
+ // OSC 11 ; ? ST → query background color
+ b"11" => {
+ if params.get(1) == Some(&&b"?"[..]) {
+ // Respond with a default dark background
+ self.push(b"\x1b]11;rgb:1e1e/1e1e/2e2e\x1b\\");
+ }
+ }
+ // OSC 12 ; ? ST → query cursor color
+ b"12" => {
+ if params.get(1) == Some(&&b"?"[..]) {
+ self.push(b"\x1b]12;rgb:cccc/cccc/cccc\x1b\\");
+ }
+ }
+ _ => {}
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn make_parser(responses: PtyResponses) -> vt100::Parser<PtyResponses> {
+ vt100::Parser::new_with_callbacks(24, 80, 0, responses)
+ }
+
+ #[test]
+ fn responds_to_da1() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b[c");
+ assert_eq!(r.take(), b"\x1b[?62;22c");
+ }
+
+ #[test]
+ fn responds_to_da1_explicit_zero() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b[0c");
+ assert_eq!(r.take(), b"\x1b[?62;22c");
+ }
+
+ #[test]
+ fn responds_to_da2() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b[>c");
+ assert_eq!(r.take(), b"\x1b[>0;0;0c");
+ }
+
+ #[test]
+ fn responds_to_cpr() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b[5;10H"); // move cursor to row 5, col 10
+ p.process(b"\x1b[6n");
+ assert_eq!(r.take(), b"\x1b[5;10R");
+ }
+
+ #[test]
+ fn responds_to_dsr_status() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b[5n"); // device status report
+ assert_eq!(r.take(), b"\x1b[0n"); // "OK"
+ }
+
+ #[test]
+ fn responds_to_extended_cpr() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b[3;7H"); // move cursor
+ p.process(b"\x1b[?6n"); // extended CPR
+ assert_eq!(r.take(), b"\x1b[?3;7R");
+ }
+
+ #[test]
+ fn responds_to_decrqm_private() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b[?25p"); // query: is cursor visible (mode 25)?
+ assert_eq!(r.take(), b"\x1b[?25;2$y"); // "reset" (2)
+ }
+
+ #[test]
+ fn responds_to_kitty_keyboard_query() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b[?u");
+ assert_eq!(r.take(), b"\x1b[?0u");
+ }
+
+ #[test]
+ fn kitty_push_pop_stack() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+
+ // Initially off
+ assert!(!r.kitty_keyboard.load(Ordering::Relaxed));
+
+ // Fish pushes mode 1
+ p.process(b"\x1b[>1u");
+ assert!(r.kitty_keyboard.load(Ordering::Relaxed));
+ p.process(b"\x1b[?u");
+ assert_eq!(r.take(), b"\x1b[?1u");
+
+ // Neovim pushes mode 3
+ p.process(b"\x1b[>3u");
+ assert!(r.kitty_keyboard.load(Ordering::Relaxed));
+ p.process(b"\x1b[?u");
+ assert_eq!(r.take(), b"\x1b[?3u");
+
+ // Neovim pops → fish's mode 1 is restored
+ p.process(b"\x1b[<u");
+ assert!(r.kitty_keyboard.load(Ordering::Relaxed));
+ p.process(b"\x1b[?u");
+ assert_eq!(r.take(), b"\x1b[?1u");
+
+ // Fish pops → back to legacy
+ p.process(b"\x1b[<u");
+ assert!(!r.kitty_keyboard.load(Ordering::Relaxed));
+ p.process(b"\x1b[?u");
+ assert_eq!(r.take(), b"\x1b[?0u");
+ }
+
+ #[test]
+ fn kitty_pop_on_empty_is_harmless() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+
+ // Pop with nothing on stack — should not crash
+ p.process(b"\x1b[<u");
+ assert!(!r.kitty_keyboard.load(Ordering::Relaxed));
+ }
+
+ #[test]
+ fn kitty_pop_count() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+
+ // Push three times
+ p.process(b"\x1b[>1u");
+ p.process(b"\x1b[>3u");
+ p.process(b"\x1b[>5u");
+ assert!(r.kitty_keyboard.load(Ordering::Relaxed));
+
+ // Pop 2 at once
+ p.process(b"\x1b[<2u");
+ assert!(r.kitty_keyboard.load(Ordering::Relaxed));
+ p.process(b"\x1b[?u");
+ assert_eq!(r.take(), b"\x1b[?1u"); // only first push remains
+
+ // Pop last one
+ p.process(b"\x1b[<u");
+ assert!(!r.kitty_keyboard.load(Ordering::Relaxed));
+ }
+
+ #[test]
+ fn responds_to_xtversion() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b[>q");
+ assert_eq!(r.take(), b"\x1bP>|herdr 0.1\x1b\\");
+ }
+
+ #[test]
+ fn responds_to_osc_fg_color_query() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b]10;?\x1b\\");
+ assert_eq!(r.take(), b"\x1b]10;rgb:cccc/cccc/cccc\x1b\\");
+ }
+
+ #[test]
+ fn responds_to_osc_bg_color_query() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b]11;?\x1b\\");
+ assert_eq!(r.take(), b"\x1b]11;rgb:1e1e/1e1e/2e2e\x1b\\");
+ }
+
+ #[test]
+ fn responds_to_osc_cursor_color_query() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b]12;?\x1b\\");
+ assert_eq!(r.take(), b"\x1b]12;rgb:cccc/cccc/cccc\x1b\\");
+ }
+
+ #[test]
+ fn no_response_for_regular_output() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"hello world\x1b[31mred\x1b[0m");
+ assert!(r.take().is_empty());
+ }
+
+ #[test]
+ fn multiple_queries_accumulate() {
+ let r = PtyResponses::new();
+ let mut p = make_parser(r.clone());
+ p.process(b"\x1b[c\x1b[6n");
+ let bytes = r.take();
+ assert!(bytes.starts_with(b"\x1b[?62;22c"));
+ assert!(bytes.ends_with(b"\x1b[1;1R"));
+ }
+}
diff --git a/src/selection.rs b/src/selection.rs
new file mode 100644
index 00000000..7b020575
--- /dev/null
+++ b/src/selection.rs
@@ -0,0 +1,253 @@
+//! Text selection and clipboard support.
+//!
+//! Selection lifecycle:
+//!
+//! MouseDown in pane → Anchor recorded (no visual yet)
+//! MouseDrag → Selection becomes active, cells highlighted
+//! MouseUp → Text extracted, copied via OSC 52, highlight stays
+//! Next click / key → Selection cleared
+//!
+//! Coordinates are stored relative to the pane's inner area (the region
+//! where terminal content is rendered, excluding borders).
+
+use ratatui::layout::Rect;
+use std::io::Write;
+
+use crate::layout::PaneId;
+
+/// Current phase of a selection.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum Phase {
+ /// Mouse is down but hasn't moved yet. If released without
+ /// moving, this was just a click — no selection created.
+ Anchored,
+ /// Mouse has moved from the anchor point. Cells are being highlighted.
+ Dragging,
+ /// Mouse released after dragging. Selection is visible and text
+ /// has been copied to clipboard. Cleared on next interaction.
+ Done,
+}
+
+/// A text selection within a terminal pane.
+#[derive(Debug, Clone)]
+pub struct Selection {
+ /// Which pane the selection belongs to.
+ pub pane_id: PaneId,
+ /// Anchor position in pane-relative coordinates (row, col).
+ anchor: (u16, u16),
+ /// Current/final position in pane-relative coordinates (row, col).
+ cursor: (u16, u16),
+ /// Selection phase.
+ phase: Phase,
+ /// The inner rect of the pane (for clamping during drag).
+ /// This is the content area, excluding borders.
+ pane_inner: Rect,
+}
+
+impl Selection {
+ /// Start a potential selection. This records the anchor but doesn't
+ /// make anything visible yet — the user might just be clicking.
+ pub fn anchor(pane_id: PaneId, row: u16, col: u16, pane_inner: Rect) -> Self {
+ Self {
+ pane_id,
+ anchor: (row, col),
+ cursor: (row, col),
+ phase: Phase::Anchored,
+ pane_inner,
+ }
+ }
+
+ /// Extend the selection as the mouse drags. Activates highlighting
+ /// once the cursor moves to a different cell than the anchor.
+ /// Screen coordinates are clamped to the pane boundary.
+ pub fn drag(&mut self, screen_col: u16, screen_row: u16) {
+ let (row, col) = self.clamp_to_pane(screen_col, screen_row);
+ self.cursor = (row, col);
+ if self.cursor != self.anchor {
+ self.phase = Phase::Dragging;
+ }
+ }
+
+ /// Finalize the selection. Returns the selected range if the user
+ /// actually dragged (not just clicked). Returns None for plain clicks.
+ pub fn finish(&mut self) -> bool {
+ if self.phase == Phase::Dragging {
+ self.phase = Phase::Done;
+ true
+ } else {
+ false
+ }
+ }
+
+ /// Whether this selection should be rendered (highlight visible).
+ pub fn is_visible(&self) -> bool {
+ self.phase == Phase::Dragging || self.phase == Phase::Done
+ }
+
+ /// Whether the user just clicked without dragging (not a selection).
+ pub fn was_just_click(&self) -> bool {
+ self.phase == Phase::Anchored
+ }
+
+ /// Returns (start, end) in reading order (top-left to bottom-right).
+ fn ordered(&self) -> ((u16, u16), (u16, u16)) {
+ let (ar, ac) = self.anchor;
+ let (cr, cc) = self.cursor;
+ if ar < cr || (ar == cr && ac <= cc) {
+ ((ar, ac), (cr, cc))
+ } else {
+ ((cr, cc), (ar, ac))
+ }
+ }
+
+ /// Check whether a pane-relative cell (row, col) is inside the selection.
+ pub fn contains(&self, row: u16, col: u16) -> bool {
+ if !self.is_visible() {
+ return false;
+ }
+ let ((sr, sc), (er, ec)) = self.ordered();
+ if row < sr || row > er {
+ return false;
+ }
+ if sr == er {
+ // Single-line: from sc to ec (inclusive)
+ col >= sc && col <= ec
+ } else if row == sr {
+ // First line: from sc to end of line
+ col >= sc
+ } else if row == er {
+ // Last line: from start to ec
+ col <= ec
+ } else {
+ // Middle rows: fully selected
+ true
+ }
+ }
+
+ /// Clamp screen coordinates to the pane's inner area and convert
+ /// to pane-relative coordinates.
+ fn clamp_to_pane(&self, screen_col: u16, screen_row: u16) -> (u16, u16) {
+ let r = &self.pane_inner;
+ let clamped_col = screen_col.clamp(r.x, r.x + r.width.saturating_sub(1));
+ let clamped_row = screen_row.clamp(r.y, r.y + r.height.saturating_sub(1));
+ (clamped_row - r.y, clamped_col - r.x)
+ }
+}
+
+/// Extract selected text from a vt100 screen.
+pub fn extract_text(screen: &vt100::Screen, selection: &Selection) -> String {
+ let ((sr, sc), (er, ec)) = selection.ordered();
+ // contents_between end_col is exclusive in vt100
+ screen.contents_between(sr, sc, er, ec + 1)
+}
+
+/// Write text to the system clipboard via OSC 52.
+///
+/// OSC 52 format: `ESC ] 52 ; c ; <base64> BEL`
+///
+/// The terminal emulator (Ghostty, kitty, etc.) intercepts this
+/// and sets the system clipboard. Ghostty has `clipboard-write = allow`
+/// by default.
+pub fn write_osc52(text: &str) {
+ use base64::Engine;
+ let encoded = base64::engine::general_purpose::STANDARD.encode(text);
+ // Use ST (ESC \) terminator — more widely supported than BEL in some terminals
+ let sequence = format!("\x1b]52;c;{encoded}\x1b\\");
+ let _ = std::io::stdout().write_all(sequence.as_bytes());
+ let _ = std::io::stdout().flush();
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn make_sel(sr: u16, sc: u16, er: u16, ec: u16) -> Selection {
+ let mut sel = Selection::anchor(PaneId::from_raw(0), sr, sc, Rect::new(0, 0, 80, 24));
+ sel.cursor = (er, ec);
+ sel.phase = Phase::Dragging;
+ sel
+ }
+
+ #[test]
+ fn ordering_forward() {
+ let sel = make_sel(2, 5, 4, 10);
+ assert_eq!(sel.ordered(), ((2, 5), (4, 10)));
+ }
+
+ #[test]
+ fn ordering_backward() {
+ let sel = make_sel(4, 10, 2, 5);
+ assert_eq!(sel.ordered(), ((2, 5), (4, 10)));
+ }
+
+ #[test]
+ fn single_line_contains() {
+ let sel = make_sel(2, 5, 2, 15);
+ assert!(!sel.contains(2, 4));
+ assert!(sel.contains(2, 5));
+ assert!(sel.contains(2, 10));
+ assert!(sel.contains(2, 15)); // inclusive
+ assert!(!sel.contains(2, 16));
+ assert!(!sel.contains(1, 10));
+ assert!(!sel.contains(3, 10));
+ }
+
+ #[test]
+ fn multi_line_contains() {
+ let sel = make_sel(2, 5, 4, 10);
+ // Row 2: from col 5 to end
+ assert!(!sel.contains(2, 4));
+ assert!(sel.contains(2, 5));
+ assert!(sel.contains(2, 79));
+
+ // Row 3: fully selected
+ assert!(sel.contains(3, 0));
+ assert!(sel.contains(3, 79));
+
+ // Row 4: from start to col 10
+ assert!(sel.contains(4, 0));
+ assert!(sel.contains(4, 10));
+ assert!(!sel.contains(4, 11));
+ }
+
+ #[test]
+ fn anchored_not_visible() {
+ let sel = Selection::anchor(PaneId::from_raw(0), 5, 10, Rect::new(0, 0, 80, 24));
+ assert!(!sel.is_visible());
+ assert!(!sel.contains(5, 10));
+ }
+
+ #[test]
+ fn click_without_drag() {
+ let mut sel = Selection::anchor(PaneId::from_raw(0), 5, 10, Rect::new(0, 0, 80, 24));
+ assert!(sel.was_just_click());
+ let copied = sel.finish();
+ assert!(!copied);
+ }
+
+ #[test]
+ fn drag_then_finish() {
+ let mut sel = Selection::anchor(PaneId::from_raw(0), 5, 10, Rect::new(10, 5, 80, 24));
+ sel.drag(20, 7); // screen coords → clamped to pane
+ assert!(sel.is_visible());
+ assert!(!sel.was_just_click());
+ let copied = sel.finish();
+ assert!(copied);
+ }
+
+ #[test]
+ fn clamp_to_pane_bounds() {
+ let sel = Selection::anchor(PaneId::from_raw(0), 0, 0, Rect::new(10, 5, 80, 24));
+ // Drag way outside pane bounds
+ let (row, col) = sel.clamp_to_pane(200, 100);
+ assert_eq!(row, 23); // clamped to height - 1
+ assert_eq!(col, 79); // clamped to width - 1
+
+ // Drag left of pane
+ let (row, col) = sel.clamp_to_pane(0, 0);
+ assert_eq!(row, 0);
+ assert_eq!(col, 0);
+ }
+
+
+}
diff --git a/src/ui.rs b/src/ui.rs
new file mode 100644
index 00000000..242853ea
--- /dev/null
+++ b/src/ui.rs
@@ -0,0 +1,870 @@
+use ratatui::{
+ layout::{Constraint, Layout, Rect},
+ style::{Color, Modifier, Style},
+ text::{Line, Span},
+ widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph},
+ Frame,
+};
+use tui_term::widget::PseudoTerminal;
+
+use crate::app::{AppState, Mode};
+use crate::detect::AgentState;
+use crate::layout::PaneInfo;
+
+const COLLAPSED_WIDTH: u16 = 4; // num + space + dot + separator
+const MIN_SIDEBAR_WIDTH: u16 = 16;
+const MAX_SIDEBAR_WIDTH: u16 = 36;
+
+/// Compute view geometry and reconcile pane sizes.
+/// Called before render to separate mutation from drawing.
+pub fn compute_view(app: &mut AppState, area: Rect) {
+ let sidebar_w = if app.sidebar_collapsed {
+ COLLAPSED_WIDTH
+ } else {
+ compute_sidebar_width(app)
+ };
+
+ let [sidebar_area, main_area] =
+ Layout::horizontal([Constraint::Length(sidebar_w), Constraint::Min(1)]).areas(area);
+
+ let terminal_area = main_area;
+
+ // Compute split borders
+ let split_borders = app
+ .active
+ .and_then(|i| app.workspaces.get(i))
+ .map(|ws| ws.layout.splits(terminal_area))
+ .unwrap_or_default();
+
+ // Compute pane layout + reconcile sizes
+ let pane_infos = compute_pane_infos(app, terminal_area);
+
+ app.view = crate::app::ViewState {
+ sidebar_rect: sidebar_area,
+ terminal_area,
+ pane_infos,
+ split_borders,
+ };
+}
+
+/// Render the UI — reads AppState but does not mutate it.
+pub fn render(app: &AppState, frame: &mut Frame) {
+ let sidebar_area = app.view.sidebar_rect;
+ let terminal_area = app.view.terminal_area;
+
+ if app.sidebar_collapsed {
+ render_sidebar_collapsed(app, frame, sidebar_area);
+ } else {
+ render_sidebar(app, frame, sidebar_area);
+ }
+ render_panes(app, frame, terminal_area);
+
+ match app.mode {
+ Mode::Navigate => render_navigate_overlay(app, frame, terminal_area),
+ Mode::Resize => render_resize_overlay(app, frame, terminal_area),
+ Mode::ConfirmClose => render_confirm_close_overlay(app, frame, terminal_area),
+ Mode::ContextMenu => {
+ render_navigate_overlay(app, frame, terminal_area);
+ render_context_menu(app, frame);
+ }
+ Mode::CreateSession | Mode::RenameSession => {}
+ Mode::Terminal => {}
+ }
+
+ // Update notification (rendered on top of everything)
+ if let Some(version) = &app.update_available {
+ if !app.update_dismissed {
+ render_update_notification(frame, terminal_area, version, app.accent);
+ }
+ }
+}
+
+/// Compute pane layout info and resize pane runtimes to match.
+fn compute_pane_infos(app: &AppState, area: Rect) -> Vec<PaneInfo> {
+ let Some(ws_idx) = app.active else {
+ return Vec::new();
+ };
+ let Some(ws) = app.workspaces.get(ws_idx) else {
+ return Vec::new();
+ };
+
+ if ws.zoomed {
+ let focused_id = ws.layout.focused();
+ if let Some(rt) = ws.runtimes.get(&focused_id) {
+ rt.resize(area.height, area.width);
+ }
+ return vec![PaneInfo {
+ id: focused_id,
+ rect: area,
+ inner_rect: area,
+ is_focused: true,
+ }];
+ }
+
+ let multi_pane = ws.layout.pane_count() > 1;
+ let terminal_active = app.mode == Mode::Terminal;
+ let mut pane_infos = ws.layout.panes(area);
+
+ for info in &mut pane_infos {
+ let inner = if multi_pane {
+ let border_set = if info.is_focused && terminal_active {
+ ratatui::symbols::border::THICK
+ } else {
+ ratatui::symbols::border::PLAIN
+ };
+ let block = Block::default()
+ .borders(Borders::ALL)
+ .border_set(border_set);
+ block.inner(info.rect)
+ } else {
+ area
+ };
+ info.inner_rect = inner;
+
+ if let Some(rt) = ws.runtimes.get(&info.id) {
+ rt.resize(inner.height, inner.width);
+ }
+ }
+
+ pane_infos
+}
+
+/// Auto-scale sidebar width based on longest workspace name.
+fn compute_sidebar_width(app: &AppState) -> u16 {
+ if app.workspaces.is_empty() {
+ return app.sidebar_width; // config default for empty state
+ }
+ let max_name = app
+ .workspaces
+ .iter()
+ .map(|ws| {
+ let pane_count = ws.layout.pane_count();
+ let bracket = if pane_count > 1 { 3 + pane_count } else { 0 }; // " [●●]"
+ ws.name.len() + bracket
+ })
+ .max()
+ .unwrap_or(8);
+ // marker(2) + dot(2) + name + bracket + padding(2) + borders(2)
+ let needed = (max_name as u16) + 8;
+ needed.clamp(MIN_SIDEBAR_WIDTH, MAX_SIDEBAR_WIDTH)
+}
+
+/// Collapsed sidebar: borderless strip of state dots with selection highlight.
+fn render_sidebar_collapsed(app: &AppState, frame: &mut Frame, area: Rect) {
+ let is_navigating = matches!(
+ app.mode,
+ Mode::Navigate | Mode::CreateSession | Mode::RenameSession | Mode::Resize | Mode::ConfirmClose | Mode::ContextMenu
+ );
+
+ // Thin vertical separator line on the right edge
+ let sep_style = if is_navigating {
+ Style::default().fg(app.accent)
+ } else {
+ Style::default().fg(Color::DarkGray)
+ };
+ let sep_x = area.x + area.width.saturating_sub(1);
+ let buf = frame.buffer_mut();
+ for y in area.y..area.y + area.height {
+ buf[(sep_x, y)].set_symbol("│");
+ buf[(sep_x, y)].set_style(sep_style);
+ }
+
+ let content_w = area.width.saturating_sub(1); // leave room for separator
+
+ for (i, ws) in app.workspaces.iter().enumerate() {
+ if i as u16 >= area.height {
+ break;
+ }
+ let (agg_state, agg_seen) = ws.aggregate_state();
+ let (icon, icon_style) = state_icon_style(agg_state, agg_seen);
+ let is_selected = i == app.selected && is_navigating;
+
+ let num_label = format!("{}", i + 1);
+
+ let row_style = if is_selected {
+ Style::default().bg(app.accent).fg(Color::Black)
+ } else {
+ Style::default()
+ };
+ let dim_style = if is_selected {
+ row_style
+ } else {
+ Style::default().fg(Color::DarkGray)
+ };
+
+ let y = area.y + i as u16;
+ let row_area = Rect::new(area.x, y, content_w, 1);
+
+ // Fill background for selected row
+ if is_selected {
+ let buf = frame.buffer_mut();
+ for x in row_area.x..row_area.x + row_area.width {
+ buf[(x, y)].set_style(row_style);
+ }
+ }
+
+ let line = Line::from(vec![
+ Span::styled(&num_label, dim_style),
+ Span::styled(" ", if is_selected { row_style } else { Style::default() }),
+ Span::styled(
+ icon,
+ if is_selected {
+ // Keep icon's fg color, just add the highlight bg
+ icon_style.bg(app.accent)
+ } else {
+ icon_style
+ },
+ ),
+ ]);
+ frame.render_widget(Paragraph::new(line), row_area);
+ }
+
+ // Toggle button at bottom
+ render_sidebar_toggle(frame, area, true);
+}
+
+fn render_sidebar(app: &AppState, frame: &mut Frame, area: Rect) {
+ let is_navigating = matches!(
+ app.mode,
+ Mode::Navigate | Mode::CreateSession | Mode::RenameSession | Mode::Resize | Mode::ConfirmClose | Mode::ContextMenu
+ );
+
+ let highlight_style = if is_navigating {
+ Style::default()
+ .fg(Color::Black)
+ .bg(app.accent)
+ .add_modifier(Modifier::BOLD)
+ } else {
+ Style::default().fg(Color::Black).bg(Color::DarkGray)
+ };
+
+ // Thin separator on right edge (consistent with collapsed)
+ let sep_style = if is_navigating {
+ Style::default().fg(app.accent)
+ } else {
+ Style::default().fg(Color::DarkGray)
+ };
+ let sep_x = area.x + area.width.saturating_sub(1);
+ let buf = frame.buffer_mut();
+ for y in area.y..area.y + area.height {
+ buf[(sep_x, y)].set_symbol("│");
+ buf[(sep_x, y)].set_style(sep_style);
+ }
+
+ // Content area (left of separator)
+ let content = Rect::new(area.x, area.y, area.width.saturating_sub(1), area.height);
+
+ // Title line
+ let title = match app.mode {
+ Mode::Terminal => format!(" {} ⏎", app.prefix_label),
+ Mode::Navigate | Mode::ContextMenu => " NAVIGATE".to_string(),
+ Mode::Resize => " RESIZE".to_string(),
+ Mode::CreateSession => " NEW".to_string(),
+ Mode::RenameSession => " RENAME".to_string(),
+ Mode::ConfirmClose => " CLOSE?".to_string(),
+ };
+ let title_style = if is_navigating {
+ Style::default()
+ .fg(app.accent)
+ .add_modifier(Modifier::BOLD)
+ } else {
+ Style::default().fg(Color::DarkGray)
+ };
+ frame.render_widget(
+ Paragraph::new(Span::styled(&title, title_style)),
+ Rect::new(content.x, content.y, content.width, 1),
+ );
+
+ // Workspace list (below title)
+ let list_area = Rect::new(
+ content.x,
+ content.y + 1,
+ content.width,
+ content.height.saturating_sub(1),
+ );
+
+ let items: Vec<ListItem> = app
+ .workspaces
+ .iter()
+ .enumerate()
+ .map(|(i, ws)| {
+ let selected = i == app.selected && is_navigating;
+ // When highlighted: text goes black on highlight bg, but icons keep their color
+ let text_style = if selected {
+ highlight_style
+ } else {
+ Style::default()
+ };
+ let dim_style = if selected {
+ highlight_style
+ } else {
+ Style::default().fg(Color::DarkGray)
+ };
+
+ let marker = if Some(i) == app.active { "▸" } else { " " };
+ let (agg_state, agg_seen) = ws.aggregate_state();
+ let (rollup_icon, rollup_style) = state_icon_style(agg_state, agg_seen);
+ // Icon keeps its fg color, but picks up the highlight bg when selected
+ let icon_style = if selected {
+ rollup_style.bg(highlight_style.bg.unwrap_or(app.accent))
+ } else {
+ rollup_style
+ };
+
+ let num_label = format!("{}", i + 1);
+ let num_style = if selected {
+ highlight_style
+ } else {
+ Style::default().fg(Color::DarkGray)
+ };
+
+ let mut spans = vec![
+ Span::styled(num_label, num_style),
+ Span::styled(format!("{marker}"), text_style),
+ Span::styled(rollup_icon, icon_style),
+ Span::styled(format!(" {}", ws.name), text_style),
+ ];
+
+ let pane_states = ws.pane_states();
+ let has_any_agent = pane_states.iter().any(|(s, _)| *s != AgentState::Unknown);
+
+ if ws.layout.pane_count() > 1 {
+ if has_any_agent {
+ spans.push(Span::styled(" [", dim_style));
+ for (pane_state, pane_seen) in &pane_states {
+ let (icon, style) = state_icon_style(*pane_state, *pane_seen);
+ let pane_icon_style = if selected {
+ style.bg(highlight_style.bg.unwrap_or(app.accent))
+ } else {
+ style
+ };
+ spans.push(Span::styled(icon, pane_icon_style));
+ }
+ spans.push(Span::styled("]", dim_style));
+ } else {
+ let count = ws.layout.pane_count();
+ spans.push(Span::styled(format!(" [{count}]"), dim_style));
+ }
+ }
+
+ ListItem::new(Line::from(spans))
+ })
+ .collect();
+
+ // No highlight_style on List — we handle it per-span above to preserve icon colors
+ let list = List::new(items);
+ let mut state = ListState::default().with_selected(Some(app.selected));
+ frame.render_stateful_widget(list, list_area, &mut state);
+
+ if app.mode == Mode::CreateSession {
+ let input_y = list_area.y + list_area.height.saturating_sub(1);
+ let input_area = Rect::new(content.x, input_y, content.width, 1);
+ let text = format!(" Name: {}\u{2588}", app.name_input);
+ frame.render_widget(
+ Paragraph::new(text).style(Style::default().fg(Color::Yellow)),
+ input_area,
+ );
+ }
+
+ if app.mode == Mode::RenameSession {
+ let input_y = list_area.y + app.selected as u16;
+ let input_area = Rect::new(content.x, input_y, content.width, 1);
+ let text = format!(" ✎ {}\u{2588}", app.name_input);
+ frame.render_widget(Clear, input_area);
+ frame.render_widget(
+ Paragraph::new(text).style(Style::default().fg(Color::Yellow)),
+ input_area,
+ );
+ }
+
+ // Toggle button at bottom (skip when input field occupies that row)
+ if app.mode != Mode::CreateSession {
+ render_sidebar_toggle(frame, area, false);
+ }
+}
+
+fn render_sidebar_toggle(frame: &mut Frame, area: Rect, collapsed: bool) {
+ let bottom_y = area.y + area.height.saturating_sub(1);
+ let content_w = area.width.saturating_sub(1); // exclude separator
+ if content_w == 0 || area.height == 0 {
+ return;
+ }
+ let icon = if collapsed { "»" } else { "«" };
+ // Center the icon in the content area
+ let x = area.x + content_w / 2;
+ let toggle_area = Rect::new(x, bottom_y, 1, 1);
+ frame.render_widget(
+ Paragraph::new(Span::styled(
+ icon,
+ Style::default().fg(Color::DarkGray),
+ )),
+ toggle_area,
+ );
+}
+
+fn render_panes(app: &AppState, frame: &mut Frame, area: Rect) {
+ let Some(ws_idx) = app.active else {
+ render_empty(frame, area, app.accent);
+ return;
+ };
+ let Some(ws) = app.workspaces.get(ws_idx) else {
+ render_empty(frame, area, app.accent);
+ return;
+ };
+
+ let multi_pane = ws.layout.pane_count() > 1;
+ let terminal_active = app.mode == Mode::Terminal;
+
+ for info in &app.view.pane_infos {
+ if let Some(rt) = ws.runtimes.get(&info.id) {
+ // Draw borders for multi-pane layouts
+ if multi_pane {
+ let (border_style, border_set) = if info.is_focused && terminal_active {
+ (
+ Style::default().fg(app.accent),
+ ratatui::symbols::border::THICK,
+ )
+ } else if info.is_focused {
+ (
+ Style::default().fg(app.accent),
+ ratatui::symbols::border::PLAIN,
+ )
+ } else {
+ (
+ Style::default().fg(Color::DarkGray),
+ ratatui::symbols::border::PLAIN,
+ )
+ };
+
+ let block = Block::default()
+ .borders(Borders::ALL)
+ .border_style(border_style)
+ .border_set(border_set);
+ frame.render_widget(block, info.rect);
+ }
+
+ // Draw terminal content
+ if let Ok(parser) = rt.parser.read() {
+ let pt = PseudoTerminal::new(parser.screen());
+ frame.render_widget(pt, info.inner_rect);
+ }
+
+ // Dim unfocused panes only in navigate mode
+ let should_dim = !info.is_focused && multi_pane && !terminal_active;
+ if should_dim {
+ let inner = info.inner_rect;
+ let buf = frame.buffer_mut();
+ for y in inner.y..inner.y + inner.height {
+ for x in inner.x..inner.x + inner.width {
+ let cell = &mut buf[(x, y)];
+ let style = cell.style();
+ let fg = style.fg.unwrap_or(Color::White);
+ let dimmed_fg = dim_color(fg);
+ cell.set_style(style.fg(dimmed_fg));
+ }
+ }
+ }
+
+ // Selection highlight
+ render_selection_highlight(&app.selection, frame, info.id, info.inner_rect);
+ }
+ }
+}
+
+/// Render selection highlight for a pane by inverting fg/bg colors.
+/// Reduce a color's brightness by blending it toward black.
+fn dim_color(color: Color) -> Color {
+ match color {
+ Color::Rgb(r, g, b) => Color::Rgb(r / 3, g / 3, b / 3),
+ Color::White => Color::DarkGray,
+ Color::Gray => Color::DarkGray,
+ Color::DarkGray => Color::Rgb(30, 30, 30),
+ Color::Red => Color::Rgb(60, 0, 0),
+ Color::Green => Color::Rgb(0, 60, 0),
+ Color::Yellow => Color::Rgb(60, 60, 0),
+ Color::Blue => Color::Rgb(0, 0, 60),
+ Color::Magenta => Color::Rgb(60, 0, 60),
+ Color::Cyan => Color::Rgb(0, 60, 60),
+ Color::LightRed => Color::Rgb(80, 30, 30),
+ Color::LightGreen => Color::Rgb(30, 80, 30),
+ Color::LightYellow => Color::Rgb(80, 80, 30),
+ Color::LightBlue => Color::Rgb(30, 30, 80),
+ Color::LightMagenta => Color::Rgb(80, 30, 80),
+ Color::LightCyan => Color::Rgb(30, 80, 80),
+ // Indexed colors and others: just use DIM modifier as fallback
+ _ => Color::DarkGray,
+ }
+}
+
+fn render_selection_highlight(
+ selection: &Option<crate::selection::Selection>,
+ frame: &mut Frame,
+ pane_id: crate::layout::PaneId,
+ inner: Rect,
+) {
+ if let Some(sel) = selection {
+ if sel.is_visible() && sel.pane_id == pane_id {
+ let buf = frame.buffer_mut();
+ for y in 0..inner.height {
+ for x in 0..inner.width {
+ if sel.contains(y, x) {
+ let cell = &mut buf[(inner.x + x, inner.y + y)];
+ // Fixed highlight: white text on blue background.
+ // Consistent regardless of the cell's original colors.
+ cell.set_style(
+ Style::default()
+ .fg(Color::White)
+ .bg(Color::Rgb(40, 80, 140)),
+ );
+ }
+ }
+ }
+ }
+ }
+}
+
+fn render_empty(frame: &mut Frame, area: Rect, accent: Color) {
+ let lines = vec![
+ Line::from(""),
+ Line::from(""),
+ Line::from(Span::styled(
+ " No active workspace",
+ Style::default().fg(Color::DarkGray),
+ )),
+ Line::from(""),
+ Line::from(vec![
+ Span::styled(" Press ", Style::default().fg(Color::DarkGray)),
+ Span::styled(
+ "n",
+ Style::default()
+ .fg(accent)
+ .add_modifier(Modifier::BOLD),
+ ),
+ Span::styled(" to create one", Style::default().fg(Color::DarkGray)),
+ ]),
+ ];
+ frame.render_widget(
+ Paragraph::new(lines).block(
+ Block::default()
+ .borders(Borders::ALL)
+ .border_style(Style::default().fg(Color::DarkGray)),
+ ),
+ area,
+ );
+}
+
+/// Floating overlay for navigate mode — appears at bottom of terminal area.
+fn render_navigate_overlay(app: &AppState, frame: &mut Frame, area: Rect) {
+ let key = Style::default()
+ .fg(app.accent)
+ .add_modifier(Modifier::BOLD);
+ let dim = Style::default().fg(Color::DarkGray);
+ let label = Style::default().fg(Color::White);
+
+ let is_full = app
+ .active
+ .and_then(|i| app.workspaces.get(i))
+ .is_some_and(|ws| ws.zoomed);
+
+ let full_key = if is_full { "unfull" } else { "full" };
+
+ let line1 = Line::from(vec![
+ Span::styled(" n", key),
+ Span::styled(" new ", dim),
+ Span::styled("N", key),
+ Span::styled(" rename ", dim),
+ Span::styled("d", key),
+ Span::styled(" close ", dim),
+ Span::styled("v", key),
+ Span::styled(" split│ ", dim),
+ Span::styled("-", key),
+ Span::styled(" split─ ", dim),
+ Span::styled("⇥", key),
+ Span::styled(" pane ", dim),
+ Span::styled("f", key),
+ Span::styled(format!(" {full_key} "), dim),
+ Span::styled("r", key),
+ Span::styled(" resize ", dim),
+ Span::styled("b", key),
+ Span::styled(" sidebar ", dim),
+ Span::styled("q", key),
+ Span::styled(" quit", dim),
+ ]);
+
+ let ws_name = app
+ .active
+ .and_then(|i| app.workspaces.get(i))
+ .map(|ws| ws.name.as_str())
+ .unwrap_or("—");
+
+ let pane_info = app
+ .active
+ .and_then(|i| app.workspaces.get(i))
+ .filter(|ws| ws.layout.pane_count() > 1)
+ .map(|ws| {
+ let ids = ws.layout.pane_ids();
+ let pos = ids
+ .iter()
+ .position(|id| *id == ws.layout.focused())
+ .unwrap_or(0);
+ format!(" [{}/{}]", pos + 1, ids.len())
+ })
+ .unwrap_or_default();
+
+ let mode_style = Style::default()
+ .fg(Color::Black)
+ .bg(app.accent)
+ .add_modifier(Modifier::BOLD);
+
+ let line2 = Line::from(vec![
+ Span::styled(" NAVIGATE ", mode_style),
+ Span::raw(" "),
+ Span::styled(ws_name, label),
+ Span::styled(&pane_info, dim),
+ Span::raw(" "),
+ Span::styled("esc", key),
+ Span::styled(" back ", dim),
+ Span::styled("↑↓", key),
+ Span::styled(" select ", dim),
+ Span::styled("⏎", key),
+ Span::styled(" open", dim),
+ ]);
+
+ let overlay_height = 2;
+ let overlay_y = area.y + area.height.saturating_sub(overlay_height);
+ let overlay_area = Rect::new(area.x, overlay_y, area.width, overlay_height);
+
+ // Clear the area behind the overlay
+ frame.render_widget(Clear, overlay_area);
+
+ let bg = Style::default().bg(Color::Black);
+ let buf = frame.buffer_mut();
+ for y in overlay_area.y..overlay_area.y + overlay_area.height {
+ for x in overlay_area.x..overlay_area.x + overlay_area.width {
+ buf[(x, y)].set_style(bg);
+ }
+ }
+
+ let [row1, row2] =
+ Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(overlay_area);
+ frame.render_widget(Paragraph::new(line1), row1);
+ frame.render_widget(Paragraph::new(line2), row2);
+}
+
+/// Floating overlay for resize mode.
+fn render_resize_overlay(app: &AppState, frame: &mut Frame, area: Rect) {
+ let key = Style::default()
+ .fg(app.accent)
+ .add_modifier(Modifier::BOLD);
+ let dim = Style::default().fg(Color::DarkGray);
+
+ let mode_style = Style::default()
+ .fg(Color::Black)
+ .bg(Color::Magenta)
+ .add_modifier(Modifier::BOLD);
+
+ let line = Line::from(vec![
+ Span::styled(" RESIZE ", mode_style),
+ Span::raw(" "),
+ Span::styled("h/l", key),
+ Span::styled(" width ", dim),
+ Span::styled("j/k", key),
+ Span::styled(" height ", dim),
+ Span::styled("esc", key),
+ Span::styled(" done", dim),
+ ]);
+
+ let overlay_y = area.y + area.height.saturating_sub(1);
+ let overlay_area = Rect::new(area.x, overlay_y, area.width, 1);
+
+ frame.render_widget(Clear, overlay_area);
+ let bg = Style::default().bg(Color::Black);
+ let buf = frame.buffer_mut();
+ for x in overlay_area.x..overlay_area.x + overlay_area.width {
+ buf[(x, overlay_y)].set_style(bg);
+ }
+ frame.render_widget(Paragraph::new(line), overlay_area);
+}
+
+/// Centered popup confirmation dialog with dimmed background.
+fn render_confirm_close_overlay(app: &AppState, frame: &mut Frame, area: Rect) {
+ let ws_name = app
+ .workspaces
+ .get(app.selected)
+ .map(|ws| ws.name.as_str())
+ .unwrap_or("?");
+ let pane_count = app
+ .workspaces
+ .get(app.selected)
+ .map(|ws| ws.layout.pane_count())
+ .unwrap_or(0);
+
+ let pane_text = if pane_count == 1 {
+ "1 pane".to_string()
+ } else {
+ format!("{pane_count} panes")
+ };
+
+ // Dim the entire background
+ let buf = frame.buffer_mut();
+ for y in area.y..area.y + area.height {
+ for x in area.x..area.x + area.width {
+ let cell = &mut buf[(x, y)];
+ cell.set_style(cell.style().add_modifier(Modifier::DIM));
+ }
+ }
+
+ // Centered popup
+ let popup_w = 44u16.min(area.width.saturating_sub(4));
+ let popup_h = 5u16;
+ let popup_x = area.x + (area.width.saturating_sub(popup_w)) / 2;
+ let popup_y = area.y + (area.height.saturating_sub(popup_h)) / 2;
+ let popup = Rect::new(popup_x, popup_y, popup_w, popup_h);
+
+ let key = Style::default()
+ .fg(app.accent)
+ .add_modifier(Modifier::BOLD);
+ let warn = Style::default()
+ .fg(Color::Red)
+ .add_modifier(Modifier::BOLD);
+ let dim = Style::default().fg(Color::DarkGray);
+
+ let title_line = Line::from(vec![
+ Span::styled(" Close workspace?", warn),
+ ]);
+
+ let detail_line = Line::from(vec![
+ Span::styled(format!(" {ws_name}"), Style::default().fg(Color::White).add_modifier(Modifier::BOLD)),
+ Span::styled(format!(" — {pane_text}"), dim),
+ ]);
+
+ let action_line = Line::from(vec![
+ Span::raw(" "),
+ Span::styled("y", key),
+ Span::styled("/", dim),
+ Span::styled("enter", key),
+ Span::styled(" confirm ", dim),
+ Span::styled("any key", key),
+ Span::styled(" cancel", dim),
+ ]);
+
+ let block = Block::default()
+ .borders(Borders::ALL)
+ .border_style(Style::default().fg(Color::Red))
+ .style(Style::default().bg(Color::Black));
+
+ let inner = block.inner(popup);
+ frame.render_widget(Clear, popup);
+ frame.render_widget(block, popup);
+
+ if inner.height >= 3 {
+ let rows = Layout::vertical([
+ Constraint::Length(1),
+ Constraint::Length(1),
+ Constraint::Length(1),
+ ])
+ .areas::<3>(inner);
+ frame.render_widget(Paragraph::new(title_line), rows[0]);
+ frame.render_widget(Paragraph::new(detail_line), rows[1]);
+ frame.render_widget(Paragraph::new(action_line), rows[2]);
+ }
+}
+
+/// Right-click context menu popup anchored near the click position.
+fn render_context_menu(app: &AppState, frame: &mut Frame) {
+ use crate::app::CONTEXT_MENU_ITEMS;
+
+ let Some(menu) = &app.context_menu else {
+ return;
+ };
+
+ let menu_w = 14u16;
+ let menu_h = CONTEXT_MENU_ITEMS.len() as u16 + 2; // +2 for border
+ let area = frame.area();
+
+ // Position: try to place below-right of click, clamp to screen
+ let x = menu.x.min(area.width.saturating_sub(menu_w));
+ let y = menu.y.min(area.height.saturating_sub(menu_h));
+ let menu_rect = Rect::new(x, y, menu_w, menu_h);
+
+ let block = Block::default()
+ .borders(Borders::ALL)
+ .border_style(Style::default().fg(app.accent))
+ .style(Style::default().bg(Color::Black));
+ let inner = block.inner(menu_rect);
+
+ frame.render_widget(Clear, menu_rect);
+ frame.render_widget(block, menu_rect);
+
+ let highlight = Style::default()
+ .fg(Color::Black)
+ .bg(app.accent)
+ .add_modifier(Modifier::BOLD);
+ let normal = Style::default().fg(Color::White);
+
+ for (i, item) in CONTEXT_MENU_ITEMS.iter().enumerate() {
+ if i as u16 >= inner.height {
+ break;
+ }
+ let style = if i == menu.selected { highlight } else { normal };
+ let row = Rect::new(inner.x, inner.y + i as u16, inner.width, 1);
+ frame.render_widget(
+ Paragraph::new(format!(" {item}")).style(style),
+ row,
+ );
+ }
+}
+
+fn render_update_notification(frame: &mut Frame, area: Rect, version: &str, accent: Color) {
+ let text = format!(" ✦ herdr v{version} installed — restart to update ");
+ let width = text.len() as u16 + 2;
+ let x = area.x + area.width.saturating_sub(width) / 2;
+ let y = area.y + area.height.saturating_sub(3);
+ let notif_area = Rect::new(x, y, width.min(area.width), 1);
+
+ frame.render_widget(Clear, notif_area);
+ frame.render_widget(
+ Paragraph::new(Span::styled(
+ text,
+ Style::default()
+ .fg(Color::Black)
+ .bg(accent)
+ .add_modifier(Modifier::BOLD),
+ )),
+ notif_area,
+ );
+}
+
+/// Visual badge for a pane's state + seen flag.
+///
+/// | State | Icon | Color |
+/// |--------------------|------|--------|
+/// | Busy | ● | Yellow |
+/// | Done (idle+unseen) | ● | Blue |
+/// | Idle (seen) | ○ | Green |
+/// | Unknown | · | Gray |
+///
+/// Filled dot = needs attention (working, or finished unseen).
+/// Hollow dot = nothing to do here.
+fn state_icon_style(state: AgentState, seen: bool) -> (&'static str, Style) {
+ match (state, seen) {
+ (AgentState::Waiting, _) => ("●", Style::default().fg(Color::Red)),
+ (AgentState::Busy, _) => ("●", Style::default().fg(Color::Yellow)),
+ (AgentState::Idle, false) => ("●", Style::default().fg(Color::Blue)), // Done
+ (AgentState::Idle, true) => ("○", Style::default().fg(Color::Green)),
+ (AgentState::Unknown, _) => ("·", Style::default().fg(Color::DarkGray)),
+ }
+}
+
+fn _build_hints(items: &[(&str, &str)], key_style: Style, dim_style: Style) -> Vec<Span<'static>> {
+ let mut spans = Vec::new();
+ spans.push(Span::raw(" "));
+ for (i, (k, desc)) in items.iter().enumerate() {
+ if i > 0 {
+ spans.push(Span::styled(" ", dim_style));
+ }
+ spans.push(Span::styled(k.to_string(), key_style));
+ spans.push(Span::styled(format!(" {desc}"), dim_style));
+ }
+ spans
+}
diff --git a/src/update.rs b/src/update.rs
new file mode 100644
index 00000000..f025f847
--- /dev/null
+++ b/src/update.rs
@@ -0,0 +1,359 @@
+//! Self-update mechanism.
+//!
+//! Checks GitHub releases for newer versions and downloads the binary.
+//! Uses `curl` as a subprocess for HTTP — no additional Rust HTTP dependencies.
+//! JSON parsing uses serde_json (already in deps for persistence).
+
+use std::env;
+use std::fs;
+use std::process::Command;
+
+use serde::Deserialize;
+
+const GITHUB_REPO: &str = "ogulcancelik/herdr";
+const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");
+
+// ---------------------------------------------------------------------------
+// Version
+// ---------------------------------------------------------------------------
+
+/// Parsed semver version for comparison.
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
+pub struct Version {
+ pub major: u32,
+ pub minor: u32,
+ pub patch: u32,
+}
+
+impl Version {
+ pub fn parse(s: &str) -> Option<Self> {
+ let s = s.strip_prefix('v').unwrap_or(s);
+ let parts: Vec<&str> = s.split('.').collect();
+ if parts.len() != 3 {
+ return None;
+ }
+ Some(Self {
+ major: parts[0].parse().ok()?,
+ minor: parts[1].parse().ok()?,
+ patch: parts[2].parse().ok()?,
+ })
+ }
+
+ pub fn current() -> Self {
+ Self::parse(CURRENT_VERSION).expect("invalid CARGO_PKG_VERSION")
+ }
+}
+
+impl std::fmt::Display for Version {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
+ }
+}
+
+// ---------------------------------------------------------------------------
+// GitHub release API types
+// ---------------------------------------------------------------------------
+
+#[derive(Deserialize)]
+struct GitHubRelease {
+ tag_name: String,
+ assets: Vec<GitHubAsset>,
+}
+
+#[derive(Deserialize)]
+struct GitHubAsset {
+ name: String,
+ browser_download_url: String,
+}
+
+// ---------------------------------------------------------------------------
+// Release info
+// ---------------------------------------------------------------------------
+
+/// Information about an available update.
+struct ReleaseInfo {
+ version: Version,
+ download_url: String,
+}
+
+/// Check GitHub for the latest release. Returns release info if newer.
+fn check_latest() -> Result<Option<ReleaseInfo>, String> {
+ let current = Version::current();
+ let url = format!("https://api.github.com/repos/{GITHUB_REPO}/releases/latest");
+
+ let output = Command::new("curl")
+ .args([
+ "-sfL",
+ "--max-time", "10",
+ "-H", "Accept: application/vnd.github+json",
+ "-H", "User-Agent: herdr-updater",
+ &url,
+ ])
+ .output()
+ .map_err(|e| format!("curl failed: {e}"))?;
+
+ if !output.status.success() {
+ return Err("failed to fetch release info".into());
+ }
+
+ let release: GitHubRelease = serde_json::from_slice(&output.stdout)
+ .map_err(|e| format!("failed to parse release JSON: {e}"))?;
+
+ let latest = Version::parse(&release.tag_name)
+ .ok_or_else(|| format!("invalid version tag: {}", release.tag_name))?;
+
+ if latest <= current {
+ return Ok(None); // up to date
+ }
+
+ // Find the asset for our platform
+ let (os, arch) = platform_target();
+ let asset_name = format!("herdr-{os}-{arch}");
+
+ let download_url = release
+ .assets
+ .iter()
+ .find(|a| a.name == asset_name)
+ .map(|a| a.browser_download_url.clone())
+ .ok_or_else(|| format!("no binary for {os}-{arch} in release {}", release.tag_name))?;
+
+ Ok(Some(ReleaseInfo {
+ version: latest,
+ download_url,
+ }))
+}
+
+// ---------------------------------------------------------------------------
+// Download + install
+// ---------------------------------------------------------------------------
+
+/// Download and install a release. Returns the installed version.
+fn download_and_install(release: &ReleaseInfo) -> Result<(), String> {
+ let current_exe = env::current_exe()
+ .map_err(|e| format!("can't find current binary: {e}"))?;
+
+ let parent = current_exe
+ .parent()
+ .ok_or("can't find binary directory")?;
+
+ // Check write permissions early
+ let test_path = parent.join(".herdr-write-test");
+ if let Err(e) = fs::write(&test_path, b"") {
+ let _ = fs::remove_file(&test_path);
+ return Err(format!(
+ "install directory not writable: {} ({}). Try running with appropriate permissions.",
+ parent.display(),
+ e
+ ));
+ }
+ let _ = fs::remove_file(&test_path);
+
+ // Unique temp file (avoids races with concurrent instances)
+ let tmp_path = parent.join(format!(
+ ".herdr-update-{}.tmp",
+ std::process::id()
+ ));
+
+ // Download the exact asset URL (pinned to the release we checked)
+ let status = Command::new("curl")
+ .args(["-sfL", "--max-time", "120", "-o"])
+ .arg(&tmp_path)
+ .arg(&release.download_url)
+ .status()
+ .map_err(|e| format!("download failed: {e}"))?;
+
+ if !status.success() {
+ let _ = fs::remove_file(&tmp_path);
+ return Err("download failed".into());
+ }
+
+ // Make executable
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ if let Err(e) = fs::set_permissions(&tmp_path, fs::Permissions::from_mode(0o755)) {
+ let _ = fs::remove_file(&tmp_path);
+ return Err(format!("chmod failed: {e}"));
+ }
+ }
+
+ // Atomic replace — rename over the current binary.
+ // On Linux, the running process keeps its fd to the old inode.
+ // Next launch picks up the new file.
+ if let Err(e) = fs::rename(&tmp_path, &current_exe) {
+ let _ = fs::remove_file(&tmp_path);
+ return Err(format!("failed to replace binary: {e}"));
+ }
+
+ Ok(())
+}
+
+// ---------------------------------------------------------------------------
+// Public API
+// ---------------------------------------------------------------------------
+
+/// Manual self-update command (`herdr update`).
+pub fn self_update() -> Result<Version, String> {
+ eprintln!("checking for updates...");
+
+ let current = Version::current();
+
+ let release = match check_latest()? {
+ Some(r) => r,
+ None => {
+ eprintln!("already up to date (v{current})");
+ return Ok(current);
+ }
+ };
+
+ eprintln!("downloading v{}...", release.version);
+ download_and_install(&release)?;
+ eprintln!("updated to v{}", release.version);
+
+ Ok(release.version)
+}
+
+/// Background auto-update: check, download, install, notify TUI.
+/// Runs in a background thread at startup.
+pub fn auto_update(events: tokio::sync::mpsc::Sender<crate::events::AppEvent>) {
+ let release = match check_latest() {
+ Ok(Some(r)) => r,
+ _ => return, // up to date or failed — silently do nothing
+ };
+
+ tracing::info!(
+ "new version v{} available, downloading from {}",
+ release.version,
+ release.download_url
+ );
+
+ if let Err(e) = download_and_install(&release) {
+ tracing::warn!("auto-update failed: {e}");
+ return;
+ }
+
+ tracing::info!("auto-update: v{} installed, restart to use", release.version);
+
+ // Notify the TUI — blocking_send is safe from a std::thread
+ let _ = events.blocking_send(crate::events::AppEvent::UpdateReady {
+ version: release.version.to_string(),
+ });
+}
+
+// ---------------------------------------------------------------------------
+// Helpers
+// ---------------------------------------------------------------------------
+
+fn platform_target() -> (&'static str, &'static str) {
+ let os = if cfg!(target_os = "linux") {
+ "linux"
+ } else if cfg!(target_os = "macos") {
+ "macos"
+ } else {
+ "unknown"
+ };
+
+ let arch = if cfg!(target_arch = "x86_64") {
+ "x86_64"
+ } else if cfg!(target_arch = "aarch64") {
+ "aarch64"
+ } else {
+ "unknown"
+ };
+
+ (os, arch)
+}
+
+// ---------------------------------------------------------------------------
+// Tests
+// ---------------------------------------------------------------------------
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn parse_version_basic() {
+ assert_eq!(
+ Version::parse("1.2.3"),
+ Some(Version { major: 1, minor: 2, patch: 3 })
+ );
+ }
+
+ #[test]
+ fn parse_version_with_v_prefix() {
+ assert_eq!(
+ Version::parse("v0.1.0"),
+ Some(Version { major: 0, minor: 1, patch: 0 })
+ );
+ }
+
+ #[test]
+ fn parse_version_invalid() {
+ assert_eq!(Version::parse("1.2"), None);
+ assert_eq!(Version::parse("abc"), None);
+ assert_eq!(Version::parse(""), None);
+ }
+
+ #[test]
+ fn version_ordering() {
+ let v010 = Version::parse("0.1.0").unwrap();
+ let v011 = Version::parse("0.1.1").unwrap();
+ let v020 = Version::parse("0.2.0").unwrap();
+ let v100 = Version::parse("1.0.0").unwrap();
+
+ assert!(v010 < v011);
+ assert!(v011 < v020);
+ assert!(v020 < v100);
+ assert!(v010 == Version::parse("0.1.0").unwrap());
+ }
+
+ #[test]
+ fn version_display() {
+ let v = Version { major: 0, minor: 1, patch: 0 };
+ assert_eq!(v.to_string(), "0.1.0");
+ }
+
+ #[test]
+ fn current_version_parses() {
+ let v = Version::current();
+ assert!(v.major < 100);
+ }
+
+ #[test]
+ fn platform_target_is_known() {
+ let (os, arch) = platform_target();
+ assert!(os == "linux" || os == "macos", "os: {os}");
+ assert!(arch == "x86_64" || arch == "aarch64", "arch: {arch}");
+ }
+
+ #[test]
+ fn github_release_deserializes() {
+ let json = r#"{
+ "tag_name": "v0.2.0",
+ "assets": [
+ {"name": "herdr-linux-x86_64", "browser_download_url": "https://example.com/herdr-linux-x86_64"},
+ {"name": "herdr-macos-aarch64", "browser_download_url": "https://example.com/herdr-macos-aarch64"}
+ ]
+ }"#;
+ let release: GitHubRelease = serde_json::from_str(json).unwrap();
+ assert_eq!(release.tag_name, "v0.2.0");
+ assert_eq!(release.assets.len(), 2);
+ assert_eq!(release.assets[0].name, "herdr-linux-x86_64");
+ }
+
+ #[test]
+ fn github_release_ignores_extra_fields() {
+ let json = r#"{
+ "tag_name": "v0.1.0",
+ "name": "Release v0.1.0",
+ "body": "Some notes",
+ "draft": false,
+ "prerelease": false,
+ "assets": []
+ }"#;
+ let release: GitHubRelease = serde_json::from_str(json).unwrap();
+ assert_eq!(release.tag_name, "v0.1.0");
+ assert!(release.assets.is_empty());
+ }
+}
diff --git a/src/workspace.rs b/src/workspace.rs
new file mode 100644
index 00000000..2b8a2902
--- /dev/null
+++ b/src/workspace.rs
@@ -0,0 +1,220 @@
+use std::collections::HashMap;
+
+use ratatui::layout::Direction;
+use tokio::sync::mpsc;
+use tracing::info;
+
+use crate::detect::{self, AgentState};
+use crate::events::AppEvent;
+use crate::layout::{PaneId, TileLayout};
+use crate::pane::{PaneRuntime, PaneState};
+
+/// A named workspace containing tiled terminal panes.
+pub struct Workspace {
+ pub name: String,
+ pub layout: TileLayout,
+ /// Pane state — always present, testable without PTYs.
+ pub panes: HashMap<PaneId, PaneState>,
+ /// Pane runtimes — only present in production (empty in tests).
+ pub runtimes: HashMap<PaneId, PaneRuntime>,
+ pub zoomed: bool,
+ pub events: mpsc::Sender<AppEvent>,
+}
+
+impl Workspace {
+ pub fn new(
+ name: String,
+ rows: u16,
+ cols: u16,
+ events: mpsc::Sender<AppEvent>,
+ ) -> std::io::Result<Self> {
+ let (layout, root_id) = TileLayout::new();
+
+ let cwd = std::env::current_dir().unwrap_or_else(|_| "/".into());
+ let runtime = PaneRuntime::spawn(root_id, rows, cols, cwd, events.clone())?;
+
+ let mut panes = HashMap::new();
+ panes.insert(root_id, PaneState::new());
+ let mut runtimes = HashMap::new();
+ runtimes.insert(root_id, runtime);
+
+ info!(workspace = %name, root_pane = root_id.raw(), "workspace created");
+ Ok(Self {
+ name,
+ layout,
+ panes,
+ runtimes,
+ zoomed: false,
+ events,
+ })
+ }
+
+ /// Split the focused pane. Returns the new pane id.
+ pub fn split_focused(
+ &mut self,
+ direction: Direction,
+ rows: u16,
+ cols: u16,
+ cwd: Option<std::path::PathBuf>,
+ ) -> std::io::Result<PaneId> {
+ let new_id = self.layout.split_focused(direction);
+ let actual_cwd = cwd.unwrap_or_else(|| {
+ std::env::current_dir().unwrap_or_else(|_| "/".into())
+ });
+ let runtime = PaneRuntime::spawn(new_id, rows, cols, actual_cwd, self.events.clone())?;
+ self.panes.insert(new_id, PaneState::new());
+ self.runtimes.insert(new_id, runtime);
+ self.zoomed = false;
+ Ok(new_id)
+ }
+
+ /// Close the focused pane. Returns the removed pane id, or None if last pane.
+ pub fn close_focused(&mut self) -> Option<PaneId> {
+ if self.layout.pane_count() <= 1 {
+ return None;
+ }
+ let closed = self.layout.focused();
+ if self.layout.close_focused() {
+ self.panes.remove(&closed);
+ self.runtimes.remove(&closed);
+ self.zoomed = false;
+ Some(closed)
+ } else {
+ None
+ }
+ }
+
+ /// Get the runtime for the focused pane.
+ pub fn focused_runtime(&self) -> Option<&PaneRuntime> {
+ self.runtimes.get(&self.layout.focused())
+ }
+
+ /// Aggregate state + seen across all panes.
+ /// Returns the highest-priority state and the worst-case seen flag.
+ pub fn aggregate_state(&self) -> (AgentState, bool) {
+ let states: Vec<AgentState> = self.panes.values().map(|p| p.state).collect();
+ let state = detect::workspace_state(&states);
+ let seen = self.panes.values().all(|p| p.seen);
+ (state, seen)
+ }
+
+ /// Per-pane (state, seen) in BSP tree order (left-to-right, top-to-bottom).
+ pub fn pane_states(&self) -> Vec<(AgentState, bool)> {
+ self.layout
+ .pane_ids()
+ .iter()
+ .map(|id| {
+ self.panes
+ .get(id)
+ .map(|p| (p.state, p.seen))
+ .unwrap_or((AgentState::Unknown, true))
+ })
+ .collect()
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Test helpers — construct workspaces without PTYs
+// ---------------------------------------------------------------------------
+
+#[cfg(test)]
+impl Workspace {
+ /// Create a test workspace with one pane, no PTY runtime.
+ pub fn test_new(name: &str) -> Self {
+ let (events, _) = mpsc::channel(64);
+ let (layout, root_id) = TileLayout::new();
+ let mut panes = HashMap::new();
+ panes.insert(root_id, PaneState::new());
+ Self {
+ name: name.to_string(),
+ layout,
+ panes,
+ runtimes: HashMap::new(),
+ zoomed: false,
+ events,
+ }
+ }
+
+ /// Add a test pane (splits focused, no PTY runtime).
+ pub fn test_split(&mut self, direction: Direction) -> PaneId {
+ let new_id = self.layout.split_focused(direction);
+ self.panes.insert(new_id, PaneState::new());
+ new_id
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::detect::AgentState;
+
+ #[test]
+ fn aggregate_state_all_unknown() {
+ let ws = Workspace::test_new("test");
+ let (state, seen) = ws.aggregate_state();
+ assert_eq!(state, AgentState::Unknown);
+ assert!(seen);
+ }
+
+ #[test]
+ fn aggregate_state_priority() {
+ let mut ws = Workspace::test_new("test");
+ let id2 = ws.test_split(Direction::Horizontal);
+
+ // Set one pane to Busy, one to Idle
+ let root_id = *ws.panes.keys().find(|id| **id != id2).unwrap();
+ ws.panes.get_mut(&root_id).unwrap().state = AgentState::Idle;
+ ws.panes.get_mut(&id2).unwrap().state = AgentState::Busy;
+
+ let (state, _) = ws.aggregate_state();
+ // Busy should be higher priority than Idle
+ assert_eq!(state, AgentState::Busy);
+ }
+
+ #[test]
+ fn aggregate_seen_any_unseen_means_unseen() {
+ let mut ws = Workspace::test_new("test");
+ let id2 = ws.test_split(Direction::Horizontal);
+
+ ws.panes.get_mut(&id2).unwrap().seen = false;
+
+ let (_, seen) = ws.aggregate_state();
+ assert!(!seen);
+ }
+
+ #[test]
+ fn close_focused_removes_pane() {
+ let mut ws = Workspace::test_new("test");
+ let _id2 = ws.test_split(Direction::Horizontal);
+ assert_eq!(ws.panes.len(), 2);
+
+ let closed = ws.close_focused();
+ assert!(closed.is_some());
+ assert_eq!(ws.panes.len(), 1);
+ }
+
+ #[test]
+ fn close_focused_last_pane_returns_none() {
+ let mut ws = Workspace::test_new("test");
+ assert_eq!(ws.panes.len(), 1);
+
+ let closed = ws.close_focused();
+ assert!(closed.is_none());
+ assert_eq!(ws.panes.len(), 1);
+ }
+
+ #[test]
+ fn pane_states_matches_layout_order() {
+ let mut ws = Workspace::test_new("test");
+ let id2 = ws.test_split(Direction::Horizontal);
+
+ // Set second pane to Waiting
+ ws.panes.get_mut(&id2).unwrap().state = AgentState::Waiting;
+
+ let states = ws.pane_states();
+ assert_eq!(states.len(), 2);
+ // One should be Unknown, one Waiting
+ assert!(states.iter().any(|(s, _)| *s == AgentState::Waiting));
+ assert!(states.iter().any(|(s, _)| *s == AgentState::Unknown));
+ }
+}
diff --git a/tests/integration/ext/herdr-test.ts b/tests/integration/ext/herdr-test.ts
new file mode 100644
index 00000000..4b6c17d5
--- /dev/null
+++ b/tests/integration/ext/herdr-test.ts
@@ -0,0 +1,146 @@
+/**
+ * Minimal tmux extension for herdr integration testing.
+ *
+ * Two actions: send and read. No wait, no pane creation, no discovery loops.
+ * Talks directly to a pane tagged @pi_name=herdr.
+ */
+
+import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
+import { StringEnum } from "@mariozechner/pi-ai";
+import { Text } from "@mariozechner/pi-tui";
+import { Type } from "@sinclair/typebox";
+
+function stripAnsi(text: string): string {
+ return text
+ .replace(/\x1b\].*?(?:\x07|\x1b\\)/g, "")
+ .replace(/\x1bPtmux;.*?\x1b\\/g, "")
+ .replace(/\x1b\[[0-9;]*[a-zA-Z]/g, "")
+ .replace(/\x1b[^[\]P]/g, "")
+ .replace(/\r/g, "");
+}
+
+export default function (pi: ExtensionAPI) {
+ let herdrPaneId: string | null = null;
+ let windowId: string | null = null;
+
+ pi.on("session_start", async () => {
+ if (!process.env.TMUX) return;
+
+ try {
+ const winResult = await pi.exec("tmux", [
+ "display-message", "-p", "-t", process.env.TMUX_PANE || "",
+ "#{window_id}",
+ ]);
+ if (winResult.code === 0) windowId = winResult.stdout.trim();
+
+ if (windowId) {
+ const listResult = await pi.exec("tmux", [
+ "list-panes", "-t", windowId,
+ "-F", "#{pane_id}\t#{@pi_name}",
+ ]);
+ if (listResult.code === 0) {
+ for (const line of listResult.stdout.trim().split("\n")) {
+ const [paneId, name] = line.split("\t");
+ if (name === "herdr") {
+ herdrPaneId = paneId;
+ break;
+ }
+ }
+ }
+ }
+ } catch {}
+ });
+
+ pi.registerTool({
+ name: "herdr",
+ label: "herdr",
+ description:
+ "Interact with the herdr instance under test. " +
+ "Actions: send (send keys or text to herdr), read (capture herdr screen).",
+ parameters: Type.Object({
+ action: StringEnum(["send", "read"] as const, {
+ description: "Action: send or read",
+ }),
+ keys: Type.Optional(
+ Type.String({
+ description:
+ "Keys to send, space-separated (for send). Examples: C-s, Enter, n, v, Escape",
+ }),
+ ),
+ text: Type.Optional(
+ Type.String({ description: "Literal text to type (for send). Sent as-is." }),
+ ),
+ lines: Type.Optional(
+ Type.Number({ description: "Scrollback lines to capture (for read, default: 50)" }),
+ ),
+ }),
+
+ async execute(_toolCallId, params) {
+ if (!herdrPaneId) {
+ throw new Error("herdr pane not found. Is herdr running in this tmux window?");
+ }
+
+ switch (params.action) {
+ case "send": {
+ if (!params.keys && !params.text) {
+ throw new Error("send requires 'keys' or 'text'");
+ }
+ if (params.text) {
+ await pi.exec("tmux", [
+ "send-keys", "-l", "-t", herdrPaneId, params.text,
+ ]);
+ }
+ if (params.keys) {
+ const keyArgs = params.keys.split(/\s+/).filter(Boolean);
+ await pi.exec("tmux", [
+ "send-keys", "-t", herdrPaneId, ...keyArgs,
+ ]);
+ }
+ const desc = [
+ params.text && `"${params.text}"`,
+ params.keys,
+ ].filter(Boolean).join(" + ");
+ return {
+ content: [{ type: "text", text: `Sent: ${desc}` }],
+ };
+ }
+
+ case "read": {
+ const lines = params.lines ?? 50;
+ const result = await pi.exec("tmux", [
+ "capture-pane", "-t", herdrPaneId, "-p", "-S", `-${lines}`,
+ ]);
+ if (result.code !== 0) {
+ throw new Error(`capture-pane failed: ${result.stderr}`);
+ }
+ let output = stripAnsi(result.stdout);
+ output = output.replace(/\n+$/, "\n");
+ return {
+ content: [{ type: "text", text: output }],
+ };
+ }
+
+ default:
+ throw new Error(`Unknown action: ${params.action}`);
+ }
+ },
+
+ renderCall(args, theme) {
+ const action = args.action || "?";
+ let t = theme.fg("toolTitle", theme.bold("herdr "));
+ t += theme.fg("accent", action);
+ if (args.text) t += theme.fg("dim", ` "${args.text}"`);
+ if (args.keys) t += theme.fg("dim", ` ${args.keys}`);
+ return new Text(t, 0, 0);
+ },
+
+ renderResult(result, _opts, theme) {
+ const c = result.content?.[0];
+ const text = c?.type === "text" ? c.text : "";
+ if (text.length > 200) {
+ return new Text(theme.fg("dim", text.slice(0, 200) + "…"), 0, 0);
+ }
+ return new Text(theme.fg("dim", text), 0, 0);
+ },
+ });
+}
diff --git a/tests/integration/pdeathsig.py b/tests/integration/pdeathsig.py
new file mode 100755
index 00000000..d4a25ac7
--- /dev/null
+++ b/tests/integration/pdeathsig.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+"""Exec a command with PR_SET_PDEATHSIG — receive a signal when parent dies.
+
+Usage: pdeathsig.py SIGNAL COMMAND [ARGS...]
+
+Fallback for systems without `setpriv --pdeathsig`.
+"""
+import ctypes
+import ctypes.util
+import os
+import signal
+import sys
+
+PR_SET_PDEATHSIG = 1
+
+SIGNALS = {
+ "TERM": signal.SIGTERM, "SIGTERM": signal.SIGTERM,
+ "INT": signal.SIGINT, "SIGINT": signal.SIGINT,
+ "HUP": signal.SIGHUP, "SIGHUP": signal.SIGHUP,
+ "KILL": signal.SIGKILL, "SIGKILL": signal.SIGKILL,
+}
+
+def main() -> int:
+ if len(sys.argv) < 3:
+ print(f"usage: {sys.argv[0]} SIGNAL COMMAND [ARGS...]", file=sys.stderr)
+ return 2
+ sig_name = sys.argv[1]
+ argv = sys.argv[2:]
+ if sig_name not in SIGNALS:
+ print(f"unknown signal: {sig_name}", file=sys.stderr)
+ return 2
+ sig = SIGNALS[sig_name]
+ libc = ctypes.CDLL(ctypes.util.find_library("c") or "libc.so.6", use_errno=True)
+ if libc.prctl(PR_SET_PDEATHSIG, sig, 0, 0, 0) != 0:
+ err = ctypes.get_errno()
+ raise OSError(err, os.strerror(err))
+ # Race fix: parent may have died before prctl took effect
+ if os.getppid() == 1:
+ os.kill(os.getpid(), sig)
+ os.execvp(argv[0], argv)
+ return 127
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/tests/integration/results/.gitignore b/tests/integration/results/.gitignore
new file mode 100644
index 00000000..d6b7ef32
--- /dev/null
+++ b/tests/integration/results/.gitignore
@@ -0,0 +1,2 @@
+*
+!.gitignore
diff --git a/tests/integration/run_all.sh b/tests/integration/run_all.sh
new file mode 100755
index 00000000..cd373af4
--- /dev/null
+++ b/tests/integration/run_all.sh
@@ -0,0 +1,116 @@
+#!/usr/bin/env bash
+#
+# Run all herdr integration tests in parallel.
+#
+# Each spec gets its own tmux server (isolated socket).
+# Workers are launched with PDEATHSIG so they get SIGTERM if this script dies.
+# A stale sweep cleans up sockets from any previous crashed run.
+#
+set -uo pipefail
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+SPECS_DIR="$SCRIPT_DIR/specs"
+RESULTS_DIR="$SCRIPT_DIR/results"
+TMUX_ROOT="${TMPDIR:-/tmp}/herdr-test-sockets"
+
+mkdir -p "$RESULTS_DIR" "$TMUX_ROOT"
+
+# Clean previous results
+rm -f "$RESULTS_DIR"/*.json "$RESULTS_DIR"/*.txt 2>/dev/null
+
+# Sweep stale tmux sockets from crashed previous runs
+for dir in "$TMUX_ROOT"/*/; do
+ [ -d "$dir" ] || continue
+ owner_pid=""
+ [ -f "$dir/owner.pid" ] && owner_pid="$(<"$dir/owner.pid")"
+ # If owning worker is still alive, leave it
+ if [ -n "$owner_pid" ] && kill -0 "$owner_pid" 2>/dev/null; then
+ continue
+ fi
+ # Dead owner — kill the tmux server and remove the dir
+ [ -S "$dir/tmux.sock" ] && tmux -S "$dir/tmux.sock" kill-server 2>/dev/null || true
+ rm -rf "$dir"
+done
+
+# Collect specs
+SPECS=("$SPECS_DIR"/*.md)
+if [ ${#SPECS[@]} -eq 0 ]; then
+ echo "No test specs found in $SPECS_DIR"
+ exit 0
+fi
+
+echo "Running ${#SPECS[@]} integration tests..."
+
+# Detect PDEATHSIG support
+PDEATHSIG_CMD=()
+if command -v setpriv >/dev/null 2>&1 && setpriv --help 2>&1 | grep -q -- '--pdeathsig'; then
+ PDEATHSIG_CMD=(setpriv --pdeathsig TERM --)
+elif [ -f "$SCRIPT_DIR/pdeathsig.py" ]; then
+ PDEATHSIG_CMD=(python3 "$SCRIPT_DIR/pdeathsig.py" TERM)
+fi
+
+# Launch all specs in parallel — directly from this shell (no subshell wrapper)
+PIDS=()
+NAMES=()
+
+for spec in "${SPECS[@]}"; do
+ name="$(basename "$spec" .md)"
+ NAMES+=("$name")
+
+ "${PDEATHSIG_CMD[@]}" "$SCRIPT_DIR/run_spec.sh" "$spec" "$RESULTS_DIR" "$TMUX_ROOT" &
+ PIDS+=($!)
+done
+
+# Wait for all, ignoring individual exit codes (we read results from files)
+for pid in "${PIDS[@]}"; do
+ wait "$pid" 2>/dev/null || true
+done
+
+echo ""
+
+# Print summary from result files
+PASSED=0
+FAILED=0
+SKIPPED=0
+
+for name in "${NAMES[@]}"; do
+ result_file="$RESULTS_DIR/$name.json"
+
+ if [[ ! -f "$result_file" ]]; then
+ echo " ? $name (no result file)"
+ FAILED=$((FAILED + 1))
+ continue
+ fi
+
+ result=$(python3 -c "import sys,json; print(json.load(sys.stdin).get('result','unknown'))" < "$result_file" 2>/dev/null || echo "unknown")
+
+ case "$result" in
+ pass)
+ echo " ✓ $name"
+ PASSED=$((PASSED + 1))
+ ;;
+ skip)
+ echo " - $name (skipped)"
+ SKIPPED=$((SKIPPED + 1))
+ ;;
+ *)
+ echo " ✗ $name"
+ python3 -c "
+import sys, json
+data = json.load(sys.stdin)
+for c in data.get('checks', []):
+ if not c.get('pass', True):
+ print(f' ✗ {c[\"name\"]}: {c.get(\"detail\", \"\")}')
+notes = data.get('notes', '')
+if notes and data.get('result') == 'error':
+ print(f' {notes}')
+" < "$result_file" 2>/dev/null
+ FAILED=$((FAILED + 1))
+ ;;
+ esac
+done
+
+echo ""
+echo "$PASSED passed, $FAILED failed, $SKIPPED skipped"
+
+[ $FAILED -eq 0 ]
diff --git a/tests/integration/run_spec.sh b/tests/integration/run_spec.sh
new file mode 100755
index 00000000..43056f6e
--- /dev/null
+++ b/tests/integration/run_spec.sh
@@ -0,0 +1,126 @@
+#!/usr/bin/env bash
+#
+# Run a single herdr integration test spec via pi.
+#
+# Usage: ./run_spec.sh <spec.md> [results_dir] [tmux_socket_root]
+#
+# Each invocation creates its own tmux server (isolated socket).
+# Cleanup kills the entire server — no orphaned sessions possible.
+#
+set -euo pipefail
+
+SPEC="$1"
+SPEC_NAME="$(basename "$SPEC" .md)"
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+PROJECT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
+RESULTS_DIR="${2:-$SCRIPT_DIR/results}"
+TMUX_ROOT="${3:-${TMPDIR:-/tmp}/herdr-test-sockets}"
+SYSTEM_PROMPT_FILE="$SCRIPT_DIR/system.md"
+HERDR="$PROJECT_DIR/target/release/herdr"
+EXTENSION="$SCRIPT_DIR/ext/herdr-test.ts"
+MODEL="${HERDR_TEST_MODEL:-minimax/MiniMax-M2.7}"
+
+mkdir -p "$RESULTS_DIR" "$TMUX_ROOT"
+
+# Per-spec tmux socket — this spec owns its entire tmux server
+TMUX_DIR="$(mktemp -d "$TMUX_ROOT/${SPEC_NAME}.XXXXXX")"
+TMUX_SOCKET="$TMUX_DIR/tmux.sock"
+SESSION_NAME="test"
+
+# Track ownership for stale sweep
+printf '%s\n' "$$" > "$TMUX_DIR/owner.pid"
+
+# Temp files (initialized for safe cleanup)
+PROMPT_FILE=""
+RAW_OUTPUT=""
+
+cleanup() {
+ # Kill our entire tmux server — takes everything with it
+ tmux -S "$TMUX_SOCKET" kill-server 2>/dev/null || true
+ [[ -n "$PROMPT_FILE" ]] && rm -f "$PROMPT_FILE" 2>/dev/null || true
+ rm -rf "$TMUX_DIR" 2>/dev/null || true
+ # RAW_OUTPUT is moved to results dir, not cleaned here
+}
+trap cleanup EXIT INT TERM HUP
+
+TMUX=(tmux -S "$TMUX_SOCKET")
+
+# Prerequisites
+RESULT_FILE="$RESULTS_DIR/$SPEC_NAME.json"
+if ! command -v tmux &>/dev/null; then echo '{"test":"'"$SPEC_NAME"'","result":"skip","checks":[],"notes":"tmux not found"}' > "$RESULT_FILE"; exit 0; fi
+if ! command -v pi &>/dev/null; then echo '{"test":"'"$SPEC_NAME"'","result":"skip","checks":[],"notes":"pi not found"}' > "$RESULT_FILE"; exit 0; fi
+if [[ ! -x "$HERDR" ]]; then echo '{"test":"'"$SPEC_NAME"'","result":"error","checks":[],"notes":"herdr binary not found"}' > "$RESULT_FILE"; exit 1; fi
+
+# Start herdr in its own tmux server
+"${TMUX[@]}" new-session -d -s "$SESSION_NAME" -x 120 -y 50
+"${TMUX[@]}" send-keys -t "$SESSION_NAME" "$HERDR --no-session" Enter
+HERDR_PANE=$("${TMUX[@]}" list-panes -t "$SESSION_NAME" -F '#{pane_id}' | head -1)
+"${TMUX[@]}" set-option -p -t "$HERDR_PANE" @pi_name "herdr"
+
+sleep 1
+
+# Build prompt
+PROMPT_FILE=$(mktemp /tmp/herdr-test-prompt-XXXXX.md)
+RAW_OUTPUT=$(mktemp /tmp/herdr-test-raw-XXXXX.txt)
+cat "$SYSTEM_PROMPT_FILE" > "$PROMPT_FILE"
+echo -e "\n---\n" >> "$PROMPT_FILE"
+cat "$SPEC" >> "$PROMPT_FILE"
+
+# Run pi in the same tmux server
+SESSION_DIR="$RESULTS_DIR/sessions"
+mkdir -p "$SESSION_DIR"
+
+"${TMUX[@]}" split-window -t "$SESSION_NAME" -h \
+ "cd $PROJECT_DIR && pi -p \
+ --session-dir '$SESSION_DIR' \
+ --no-extensions --no-skills --no-prompt-templates \
+ -e '$EXTENSION' \
+ --no-tools \
+ --model '$MODEL' \
+ --system-prompt '$PROMPT_FILE' \
+ 'Execute the test now. Use the herdr tool to send keys and read the screen. Output the JSON result block when done.' \
+ > '$RAW_OUTPUT' 2>&1; \
+ echo '___DONE___' >> '$RAW_OUTPUT'; \
+ sleep 1"
+
+# Wait for completion
+TIMEOUT=180
+ELAPSED=0
+while [ $ELAPSED -lt $TIMEOUT ]; do
+ if grep -q '___DONE___' "$RAW_OUTPUT" 2>/dev/null; then
+ break
+ fi
+ sleep 2
+ ELAPSED=$((ELAPSED + 2))
+done
+
+if [ $ELAPSED -ge $TIMEOUT ]; then
+ echo '{"test":"'"$SPEC_NAME"'","result":"error","checks":[],"notes":"pi timed out after '"$TIMEOUT"'s"}' > "$RESULT_FILE"
+ cp "$RAW_OUTPUT" "$RESULTS_DIR/$SPEC_NAME.raw.txt" 2>/dev/null || true
+ rm -f "$RAW_OUTPUT"
+ exit 1
+fi
+
+# Always save raw output
+cp "$RAW_OUTPUT" "$RESULTS_DIR/$SPEC_NAME.raw.txt" 2>/dev/null || true
+rm -f "$RAW_OUTPUT"
+
+# Extract JSON
+JSON_RESULT=$(awk '/^```json$/,/^```$/' "$RESULTS_DIR/$SPEC_NAME.raw.txt" | grep -v '^```')
+
+if [[ -z "$JSON_RESULT" ]]; then
+ echo '{"test":"'"$SPEC_NAME"'","result":"error","checks":[],"notes":"no JSON in pi output"}' > "$RESULT_FILE"
+ exit 1
+fi
+
+if echo "$JSON_RESULT" | python3 -m json.tool > "$RESULT_FILE" 2>/dev/null; then
+ RESULT=$(python3 -c "import sys,json; print(json.load(sys.stdin).get('result','unknown'))" < "$RESULT_FILE")
+ if [[ "$RESULT" == "pass" ]]; then
+ exit 0
+ else
+ exit 1
+ fi
+else
+ echo '{"test":"'"$SPEC_NAME"'","result":"error","checks":[],"notes":"invalid JSON from pi"}' > "$RESULT_FILE"
+ exit 1
+fi
diff --git a/tests/integration/specs/create_workspace.md b/tests/integration/specs/create_workspace.md
new file mode 100644
index 00000000..0f588c69
--- /dev/null
+++ b/tests/integration/specs/create_workspace.md
@@ -0,0 +1,12 @@
+# Test: Create workspace
+
+## Steps
+
+1. Read the screen to verify herdr started in empty state (should show "No active workspace" or "Press n to create one")
+2. Press `n` to enter create mode
+3. Type `test-workspace`
+4. Press Enter to create
+5. Read the screen and verify:
+ - The sidebar shows `1` followed by `test-workspace`
+ - A shell prompt is visible in the main pane area
+ - The active marker `▸` is next to the workspace
diff --git a/tests/integration/specs/sidebar_collapse.md b/tests/integration/specs/sidebar_collapse.md
new file mode 100644
index 00000000..b5d42c25
--- /dev/null
+++ b/tests/integration/specs/sidebar_collapse.md
@@ -0,0 +1,13 @@
+# Test: Sidebar collapse and expand
+
+## Steps
+
+1. Press `n` to create a workspace named `test`
+2. Read the screen and verify the expanded sidebar shows the full workspace name `test`
+3. Press prefix (ctrl+s), then `b` to collapse the sidebar
+4. Read the screen and verify:
+ - The sidebar is now narrow (just numbers and state dots, no workspace name visible)
+ - The `»` expand icon is visible at the bottom of the sidebar
+ - The terminal pane takes up more horizontal space
+5. Press prefix (ctrl+s), then `b` again to expand
+6. Verify the full workspace name `test` is visible again in the sidebar
diff --git a/tests/integration/specs/split_and_navigate.md b/tests/integration/specs/split_and_navigate.md
new file mode 100644
index 00000000..cb75d72d
--- /dev/null
+++ b/tests/integration/specs/split_and_navigate.md
@@ -0,0 +1,11 @@
+# Test: Split pane and navigate
+
+## Steps
+
+1. Press `n` to create a workspace named `dev`
+2. Press prefix (ctrl+s), then `v` to split vertically
+3. Read the screen and verify two panes are visible side by side, each with a shell prompt
+4. Verify the sidebar shows a pane count indicator like `[2]`
+5. Press prefix (ctrl+s), then `h` to navigate to the left pane
+6. Press prefix (ctrl+s), then `l` to navigate back to the right pane
+7. Verify the thick border (focused indicator) moved between panes
diff --git a/tests/integration/specs/workspace_switching.md b/tests/integration/specs/workspace_switching.md
new file mode 100644
index 00000000..0711c575
--- /dev/null
+++ b/tests/integration/specs/workspace_switching.md
@@ -0,0 +1,12 @@
+# Test: Workspace switching with prefix+N
+
+## Steps
+
+1. Press `n` to create a workspace named `first`
+2. Press prefix (ctrl+s), then `n` to create a second workspace named `second`
+3. Read the screen and verify the sidebar shows two workspaces numbered 1 and 2
+4. Verify `second` is the active workspace (has the `▸` marker)
+5. Press prefix (ctrl+s), then `1` to switch to the first workspace
+6. Read the screen and verify `first` is now active (has the `▸` marker)
+7. Press prefix (ctrl+s), then `2` to switch back to `second`
+8. Verify `second` is active again
diff --git a/tests/integration/system.md b/tests/integration/system.md
new file mode 100644
index 00000000..d6ccb05b
--- /dev/null
+++ b/tests/integration/system.md
@@ -0,0 +1,48 @@
+# herdr Integration Test Agent
+
+You are a test executor for herdr, a terminal workspace manager. Your job is to execute a test specification by interacting with herdr through tmux, then report whether the test passed or failed.
+
+## Environment
+
+- herdr is running in tmux pane `herdr` with `--no-session` (clean state, no saved workspaces)
+- You have the `tmux` tool to interact with it: `send` keys, `read` screen output
+- herdr starts in Navigate mode with no workspaces (empty state)
+
+## herdr Controls
+
+- **Navigate mode**: press prefix key (ctrl+s) from Terminal mode, or start with no workspaces
+- **Create workspace**: press `n`, type name, press Enter
+- **Switch workspace**: press `1`-`9` (by number) or arrow keys + Enter
+- **Split pane**: press `v` (vertical split) or `-` (horizontal split)
+- **Close pane**: press `x`
+- **Close workspace**: press `d` (then `y` to confirm)
+- **Collapse sidebar**: press `b`
+- **Resize mode**: press `r`, then h/j/k/l to resize, Esc to exit
+- **Back to terminal**: press Esc or prefix key again
+- **Quit**: press `q` in Navigate mode
+
+After creating a workspace or pressing Enter on one, you enter Terminal mode. Press ctrl+s (prefix) to go back to Navigate mode.
+
+## How to Execute
+
+1. Read the test specification carefully
+2. Execute each step using tmux send/read
+3. After each action, wait briefly then read the screen to verify
+4. When done, output your result as a JSON object
+
+## Output Format
+
+Your final message MUST be a JSON code block with this structure:
+
+```json
+{
+ "test": "<test name>",
+ "result": "pass" | "fail",
+ "checks": [
+ {"name": "<what was checked>", "pass": true|false, "detail": "<what you saw>"}
+ ],
+ "notes": "<any additional observations>"
+}
+```
+
+Be precise. Read the screen carefully. Don't assume — verify by reading.