Compare commits
42 Commits
v0.0.3
...
feat/daemo
| Author | SHA1 | Date | |
|---|---|---|---|
| 4051e7264b | |||
| 63986e7e00 | |||
| 6d15626e05 | |||
| 63cb8a4388 | |||
| 5149224000 | |||
| 95b1967e9b | |||
| d07a09d64f | |||
| c56de27f44 | |||
| 80a14502c4 | |||
| 08c7405c79 | |||
| ec0c148164 | |||
| 9aecc8b7a2 | |||
| e63bdad5e1 | |||
| b72a32bbc6 | |||
| da46340a82 | |||
| d2342f99cf | |||
| 178b4437b1 | |||
| 0725375755 | |||
| 3022e4adeb | |||
| 7b5a22618f | |||
| 53f06b604f | |||
| 50fd7be70d | |||
| 96f7c66d5f | |||
| f61788eff2 | |||
| c1b66f9f8a | |||
| fe25fcf043 | |||
| 2fa00ad510 | |||
| 412b1167a2 | |||
| 34b41be1df | |||
| de60b93bc6 | |||
| 67b994f629 | |||
| f10598601f | |||
| cadd4c8f64 | |||
| 98d1c059cf | |||
| cf65d5d707 | |||
| ef9b8e71c6 | |||
| e64060e40f | |||
| e4ab8c2136 | |||
| f312b6d345 | |||
| e6f5a94fae | |||
| c1ecba0624 | |||
| 878e9370bc |
@@ -11,14 +11,19 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
- uses: jdx/mise-action@v2
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
cache: true
|
||||
|
||||
- uses: mlugg/setup-zig@v2
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
version: 0.15.2
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-
|
||||
|
||||
- name: Build libghostty-vt
|
||||
run: make deps
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,4 +7,5 @@ spike-report-*.txt
|
||||
/bin/
|
||||
/spike
|
||||
/.worktrees/
|
||||
/.claude/worktrees/
|
||||
internal/harness/.artifacts/
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
# libghostty-vt is built from a pinned upstream Ghostty commit; that
|
||||
# commit's build.zig.zon pins minimum_zig_version = 0.15.2. We match
|
||||
# it here so contributors don't have to puzzle out the version from
|
||||
# a deep upstream file.
|
||||
# a deep upstream file. The go pin matches go.mod so CI and local
|
||||
# builds use the same toolchain.
|
||||
[tools]
|
||||
zig = "0.15.2"
|
||||
go = "1.26.3"
|
||||
|
||||
177
CHANGELOG.md
177
CHANGELOG.md
@@ -6,6 +6,181 @@ loosely follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- `patterm daemon`, `patterm daemon stop`, and `patterm ls` now expose
|
||||
a local unix-socket daemon lifecycle for the daemon/client split.
|
||||
- The local daemon protocol now supports attach, explicit detach,
|
||||
project listing, focused-pane snapshots, pane chunks, resize/focus
|
||||
updates, and daemon-owned command spawn requests while keeping child
|
||||
processes alive after a client disconnects.
|
||||
- The default `patterm [dir]` startup now auto-starts the local daemon
|
||||
on demand and attaches a thin terminal client over the unix-socket
|
||||
transport; `--in-process` or `PATTERM_NO_DAEMON=1` keeps the legacy
|
||||
single-process path available as an escape hatch.
|
||||
- `patterm daemon --listen HOST:PORT` can now opt into a TCP listener
|
||||
for remote human clients, with the unix socket still enabled for
|
||||
local clients.
|
||||
- `patterm connect --host HOST:PORT [--token TOKEN]` attaches the thin
|
||||
client to a remote daemon over the same transport protocol.
|
||||
- TCP attaches now require a lightweight bearer token stored under
|
||||
`$XDG_DATA_HOME/patterm/clients/token`; local unix-socket attaches
|
||||
remain exempt and rely on socket file permissions.
|
||||
- The daemon now tracks a display owner per pane so a second client
|
||||
viewing the same pane does not resize the underlying PTY/emulator;
|
||||
ownership is released on detach and the next focuser can claim and
|
||||
resize the pane.
|
||||
- patterm can now keep multiple local projects loaded in one loopback
|
||||
daemon core, with command-palette entries to switch the current
|
||||
client view or open another project without tearing down processes
|
||||
in the previous project.
|
||||
- The status line now shows the current project name when multiple
|
||||
projects are loaded, and the MCP startup greeting includes
|
||||
`project_key` for diagnostics and future daemon routing.
|
||||
- MCP clients can now call `scratchpad_delete` with a scratchpad name
|
||||
to remove a shared project scratchpad.
|
||||
|
||||
### Changed
|
||||
- The tab bar now shows each visible agent tab's own summary instead
|
||||
of only rendering the focused tab's summary.
|
||||
- Grid-mode `get_process_output` now returns whitespace-normalized
|
||||
text to avoid sending padded terminal rows and repeated blank lines
|
||||
over MCP.
|
||||
|
||||
### Fixed
|
||||
- MCP scratchpad tools now route through the caller's project instead
|
||||
of always using the daemon registry's default project.
|
||||
- Injected agent input now sends the submit Enter as a separated,
|
||||
settled keystroke so messages reliably submit instead of sometimes
|
||||
sitting unsent in the composer.
|
||||
- Codex agents are no longer reported idle while a turn is still
|
||||
running.
|
||||
- Slow MCP tool calls such as `wait_for_pattern` no longer block later
|
||||
tool calls on the same MCP connection.
|
||||
- Closing an agent now escalates from SIGTERM to SIGKILL when needed,
|
||||
so agents that ignore SIGTERM disappear from the running tab bar
|
||||
after one Close action while keeping their exited pane readable.
|
||||
- Sidebar timer indicators now repaint as their visible countdown
|
||||
value changes, so labels progress from minutes to seconds without
|
||||
waiting for unrelated terminal output or focus changes.
|
||||
- Raw terminal focused actions now show a single `Close` row instead
|
||||
of separate stop/delete-style lifecycle choices that did the same
|
||||
thing for ephemeral terminal panes.
|
||||
- Restarting a process from the palette now restores the focused pane
|
||||
and host chrome before waiting for the old process to exit, so the
|
||||
tab bar and sidebar do not disappear during slow restarts.
|
||||
- Deleting the focused scratchpad now moves focus to another
|
||||
scratchpad when one exists, or back to a running terminal/agent
|
||||
instead of dropping into the empty state.
|
||||
- Multiline paste into raw terminal and command panes no longer pays
|
||||
the agent-specific per-Enter delay, making large pasted input arrive
|
||||
as one PTY write outside Claude/Codex/OpenCode panes.
|
||||
|
||||
## [0.0.7] - 2026-05-18
|
||||
|
||||
### Added
|
||||
- The top tab bar now prefixes each agent tab's label with its
|
||||
idle-state glyph (✕ error, ? permission, ◐ thinking, ○ idle, ●
|
||||
working), matching the sidebar's vocabulary so the state of every
|
||||
open agent is visible without opening or focusing each tab.
|
||||
|
||||
### Changed
|
||||
- Built-in agent presets (`claude`, `codex`, `opencode`) now live in
|
||||
memory and user preset files merge over them by name instead of
|
||||
patterm writing default preset files into `$XDG_CONFIG_HOME`. Add
|
||||
`"disabled": true` in a matching user preset to hide a built-in.
|
||||
- Generated MCP config files for agent launches now live under the
|
||||
runtime agent directory instead of `$XDG_CONFIG_HOME/patterm/mcp`.
|
||||
- Auto-summarization settings now save as soon as a changed row is
|
||||
applied, including cadence/provider/toggle changes and model edits,
|
||||
without requiring a separate save step.
|
||||
- The Agents / Auto-summarization settings screen no longer shows
|
||||
explicit Save, Cancel, or Back rows, and its footer copy no longer
|
||||
describes a separate save/cancel flow.
|
||||
- Auto-summarization setting rows now visually separate grey labels
|
||||
from regular-colour values.
|
||||
- The active-thread summary in the tab bar is now constrained to the
|
||||
active tab's width instead of spanning the whole top row.
|
||||
- Sidebar summary text now wraps from the full summary text instead of
|
||||
using an ellipsized single-line value.
|
||||
|
||||
### Fixed
|
||||
- Claude permission prompts are now detected from the rendered pane as
|
||||
well as the recent output tail, so the sidebar marks the pane as
|
||||
waiting for permission even while `Calling patterm...` continues to
|
||||
repaint.
|
||||
- Removed the redundant "Back to Settings" row from the
|
||||
Agents / Auto-summarization settings screen.
|
||||
- Pending `timer_*` entries are now cancelled when their owning or
|
||||
watched child is closed via `close_process`, preventing stale
|
||||
timer bodies from being re-delivered to the orchestrator pane
|
||||
after the work has already been handled.
|
||||
|
||||
## [0.0.6] - 2026-05-15
|
||||
|
||||
### Changed
|
||||
- Toast notifications now reserve three content rows and word-wrap
|
||||
the message body inside the box, replacing the previous
|
||||
single-line+ellipsis layout. The `Ctrl-N · N more` inline hint is
|
||||
gone; instead the host status strip surfaces a `Ctrl-N · dismiss`
|
||||
hint, shown only while a notification is on screen so the chord
|
||||
doesn't advertise itself when it has nothing to dismiss.
|
||||
|
||||
### Fixed
|
||||
- Auto-summary no longer fails immediately with `codex summarizer:
|
||||
error: unexpected argument '--ask-for-approval' found`. The codex
|
||||
CLI dropped that flag; we now rely on `--sandbox read-only` (which
|
||||
already implies no approvals) instead of passing it.
|
||||
- Toast box no longer flickers / half-erases while the focused
|
||||
child (claude, codex, opencode, etc.) repaints its TUI. The
|
||||
overlay is now stitched onto the end of the per-chunk PTY write
|
||||
under `outMu`, and wrapped in DECSET 2026 (synchronized output)
|
||||
brackets so terminals that support it batch the child's redraw +
|
||||
the box paint into a single frame instead of racing cell-by-cell.
|
||||
|
||||
## [0.0.5] - 2026-05-15
|
||||
|
||||
### Changed
|
||||
- Replaced the single-slot status-line "flash" with a stackable toast
|
||||
surface anchored at the top-right of the focused pane. `flashError`,
|
||||
`flashTransient`, and MCP `request_human_attention` now push onto
|
||||
the toast stack (cap 5, oldest drops). Toasts persist until
|
||||
dismissed with `Ctrl-N`, or cleared via the new
|
||||
"Clear notifications" palette command. The status line no longer
|
||||
shows the `[!]` prefix.
|
||||
- `Ctrl-N` is consumed by the host only when there is a toast to
|
||||
dismiss; an empty stack lets `Ctrl-N` pass through to the focused
|
||||
child so readline / nano / emacs / opencode keep their bindings.
|
||||
- Command palette is calmer when something is focused. Focused-section
|
||||
rows now read as bare verbs (`Rename`, `Close`, `Stop`, `Restart`,
|
||||
`Delete`, `Edit`) instead of repeating the focused name (`Close
|
||||
agent: codex`); the title bar's `on: codex` / `pad: notes.md`
|
||||
carries the subject. Fuzzy queries still match the dropped context
|
||||
through the row hint (e.g. typing `close codex` still finds the
|
||||
Close row).
|
||||
- Dashed `── Focused ──` / `── Open ──` / `── Spawn ──` section
|
||||
banners are gone. Sections are separated by a single blank spacer
|
||||
row, so the action labels themselves carry the visual weight.
|
||||
- The Open section no longer lists a `Switch to <current>` row for
|
||||
the pane you're already focused on.
|
||||
|
||||
## [0.0.4] - 2026-05-15
|
||||
|
||||
### Changed
|
||||
- Release workflow (`.gitea/workflows/release.yml`) now provisions
|
||||
Zig and Go through `jdx/mise-action@v2`, reading the versions from
|
||||
`.mise.toml` (zig 0.15.2, go 1.26.3). Both toolchains were
|
||||
previously installed via `mlugg/setup-zig` and `actions/setup-go`,
|
||||
whose mirror chase / GitHub fetch combined for ~8 minutes per run
|
||||
before any patterm code compiled. mise pulls each tool once and
|
||||
caches the install dir, so subsequent runs hit the cache instead of
|
||||
re-downloading. `make deps` still resolves zig via `mise which zig`
|
||||
with a PATH fallback; `go.mod` already pinned `go 1.26.3`, so the
|
||||
new `go` entry in `.mise.toml` just keeps CI and local builds on
|
||||
the same toolchain.
|
||||
- A Go module/build cache step (`actions/cache@v4`, keyed on
|
||||
`go.sum`) was added so `go build` doesn't re-download dependencies
|
||||
on every tag push.
|
||||
|
||||
## [0.0.3] - 2026-05-15
|
||||
|
||||
### Added
|
||||
@@ -70,6 +245,8 @@ loosely follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
the command field.
|
||||
|
||||
### Fixed
|
||||
- Error/status flashes now restore the currently focused pane instead
|
||||
of drawing the empty-state hint over a running agent or process.
|
||||
- Release workflow (`.gitea/workflows/release.yml`) now uses
|
||||
`mlugg/setup-zig@v2` instead of the deprecated `@v1`. v1 hard-coded
|
||||
the pre-0.14 tarball name (`zig-linux-x86_64-<ver>.tar.xz`), so
|
||||
|
||||
23
SPEC.md
23
SPEC.md
@@ -39,7 +39,7 @@ The tool is one Go process that owns: the TUI, all PTYs, vt-emulated grids, sess
|
||||
|
||||
## 3. Project state layout
|
||||
|
||||
Scratchpads (user data) live under `$XDG_DATA_HOME`; presets and config live under `$XDG_CONFIG_HOME`.
|
||||
Scratchpads (user data) live under `$XDG_DATA_HOME`; user-authored preset overlays and config live under `$XDG_CONFIG_HOME`.
|
||||
|
||||
```
|
||||
$XDG_DATA_HOME/patterm/
|
||||
@@ -53,12 +53,12 @@ $XDG_DATA_HOME/patterm/
|
||||
└── <agent-written>.md
|
||||
|
||||
$XDG_CONFIG_HOME/patterm/
|
||||
├── config.json # global settings (theme, default keymap, etc.)
|
||||
├── settings.json # global settings, written only after the user changes settings
|
||||
└── presets/
|
||||
├── agents/
|
||||
│ ├── claude.json # ships as default
|
||||
│ ├── codex.json # ships as default
|
||||
│ ├── opencode.json # ships as default
|
||||
│ ├── claude.json # optional overlay for built-in claude
|
||||
│ ├── codex.json # optional overlay for built-in codex
|
||||
│ ├── opencode.json # optional overlay for built-in opencode
|
||||
│ └── <user-defined>.json
|
||||
└── processes/
|
||||
├── dev.json # e.g. { "name": "bun run dev", "argv": ["bun", "run", "dev"] }
|
||||
@@ -66,7 +66,7 @@ $XDG_CONFIG_HOME/patterm/
|
||||
└── <user-defined>.json
|
||||
```
|
||||
|
||||
Both preset directories are scanned at startup; every file found becomes a palette entry ("Spawn agent: claude", "Run process: bun run dev", …). Presets are project-agnostic in v1 — the same set is available in every project. Per-project overrides can be added later.
|
||||
patterm always has built-in agent presets for `claude`, `codex`, and `opencode`. User preset files are scanned at startup and merged into matching built-ins by `name`, or added as standalone custom presets when the name is new. A matching file with `"disabled": true` hides a built-in. Startup does not write default preset files. Presets are project-agnostic in v1 — the same set is available in every project. Per-project overrides can be added later.
|
||||
|
||||
Project key = `sha256(realpath(project_dir))[:16]`. Used only as a scratchpad directory name — there is no daemon to look up.
|
||||
|
||||
@@ -121,7 +121,7 @@ Scratchpads and command-preset trust grants persist across runs. Sessions and ch
|
||||
Almost all application functions are driven through a single command palette opened with `Ctrl-K`. The palette is a fuzzy-searchable list of commands, scoped to whatever makes sense for the current focus. Two kinds of entries appear:
|
||||
|
||||
- **Built-in commands** — "Switch to session…", "Focus pane…", "Take input control", "Release control to orchestrator", "Open scratchpad…", "Kill child…", "Quit", etc.
|
||||
- **Preset commands** — one entry per file under `$XDG_CONFIG_HOME/patterm/presets/`. Agent presets surface as "Spawn agent: codex" / "Spawn agent: claude" / …; process presets surface as "Run process: bun run dev" / "Run process: vitest" / …. The label comes from the preset's `name` field; the action is "launch this preset into a new pane."
|
||||
- **Preset commands** — one entry per built-in or user-defined preset. Agent presets surface as "Spawn agent: codex" / "Spawn agent: claude" / …; process presets surface as "Run process: bun run dev" / "Run process: vitest" / …. The label comes from the preset's `name` field; the action is "launch this preset into a new pane."
|
||||
|
||||
Selecting a preset either launches it immediately (no required args) or opens a sub-palette for optional args — namely an **initial prompt** (agent presets only), which patterm injects into the spawned PTY's input after the agent is ready (§8). The orchestrator equivalent of this — `spawn_agent` / `spawn_process` MCP tools — uses the exact same machinery: pick a preset by name, optionally supply an initial prompt, patterm handles the rest.
|
||||
|
||||
@@ -365,11 +365,11 @@ Risks acknowledged: the orchestrator's reading of the prompt is a vision/parsing
|
||||
|
||||
## 10. Presets
|
||||
|
||||
Presets are user-editable JSON files that describe how to launch something. patterm itself has no hard-coded agent or process types — every spawnable thing is a preset. Two flavours:
|
||||
Presets describe how to launch something. patterm has built-in defaults for common agent CLIs, and user-editable JSON files can override, disable, or add presets. Two flavours:
|
||||
|
||||
### Agent presets
|
||||
|
||||
`$XDG_CONFIG_HOME/patterm/presets/agents/<name>.json`. Launches a vendor LLM CLI with MCP wired up and the conversation-protocol addendum injected.
|
||||
Built-in agent presets launch vendor LLM CLIs with MCP wired up and the conversation-protocol addendum injected. `$XDG_CONFIG_HOME/patterm/presets/agents/<name>.json` can overlay a built-in by `name` or define a new agent preset.
|
||||
|
||||
| Field | Purpose |
|
||||
|---|---|
|
||||
@@ -377,17 +377,18 @@ Presets are user-editable JSON files that describe how to launch something. patt
|
||||
| `argv` | Full launch argv (e.g. `["claude"]`, `["codex", "--no-tui-banner"]`) |
|
||||
| `env` | Env vars to set (merged over inherited env) |
|
||||
| `working_dir` | Defaults to the project root |
|
||||
| `disabled` | If `true`, hides a built-in preset with the same `name` |
|
||||
| `mcp_injection` | How to point this CLI at patterm's stdio proxy. One of: `{ "kind": "flag", "flag": "--mcp-config", "config_path": "..." }`, `{ "kind": "config_file", "path": "~/.codex/config.toml", "merge_key": "mcp_servers" }`, `{ "kind": "env_var", "var": "MCP_CONFIG_PATH" }` |
|
||||
| `ready_signal` | How to detect the TUI is ready (default: 1s idle after launch). Override per-CLI if needed. |
|
||||
| `chrome_trim_hints` | Optional regexes / row ranges for stripping vendor chrome in grid reads |
|
||||
|
||||
Default presets shipped: `claude`, `codex`, `opencode`. Authoring these is per-vendor research — each CLI has its own MCP config conventions, ready states, and TUI chrome. Users can copy and edit them, or add new ones (e.g. a second `claude` preset that launches with a specific model or system prompt file).
|
||||
Built-in presets: `claude`, `codex`, `opencode`. Authoring these is per-vendor research — each CLI has its own MCP config conventions, ready states, idle detection, and TUI chrome. Users can add small overlay files for built-ins, disable built-ins, or add new presets (e.g. a second `claude-sonnet` preset that launches with a specific model or system prompt file).
|
||||
|
||||
MCP config flow: at startup, for each agent preset, patterm renders a small JSON pointing at its own `mcp-stdio` proxy subcommand (`patterm mcp-stdio --socket <pid-sock> --identity <token>`) into a per-preset temp file. The launch then uses the preset's `mcp_injection` strategy to hand that path to the CLI. The user's global vendor config is never mutated.
|
||||
|
||||
### Process presets
|
||||
|
||||
`$XDG_CONFIG_HOME/patterm/presets/processes/<name>.json`. Launches a raw command in a PTY — no MCP, no addendum, no system prompt.
|
||||
`$XDG_CONFIG_HOME/patterm/presets/processes/<name>.json`. Launches a raw command in a PTY — no MCP, no addendum, no system prompt. There are no built-in process presets.
|
||||
|
||||
| Field | Purpose |
|
||||
|---|---|
|
||||
|
||||
115
TODO.md
115
TODO.md
@@ -1,115 +0,0 @@
|
||||
# Perf Audit (reviewed 2026-05-15)
|
||||
Findings that survived the 2026-05-15 review pass. Low and marginal
|
||||
items from the original sweep were removed; remaining items have enough
|
||||
measured or workflow evidence to justify action.
|
||||
|
||||
Baseline benchmark numbers (`go test -bench=. ./internal/app/`, AMD
|
||||
Ryzen 7 7800X3D, libghostty-vt **ReleaseFast** after the Makefile
|
||||
fix landed):
|
||||
|
||||
```
|
||||
# Renderer alone
|
||||
ViewportRenderer_PlainASCII 229 MB/s 1.3 KB/op 6 allocs/op
|
||||
ViewportRenderer_StyledLines 89 MB/s 91 KB/op 4325 allocs/op
|
||||
ViewportRenderer_RatatuiBurst 40 MB/s 365 KB/op 17306 allocs/op
|
||||
RendererThroughput_ReuseInstance 90 MB/s 316 KB/op 17380 allocs/op
|
||||
ContainsOSC_NoOSC 3050 MB/s 0 B/op 0 allocs/op
|
||||
|
||||
# ASCII-video stream (renderer only — 3 sec at the target fps)
|
||||
ASCIIVideo_Stream_8Color_120fps 260 µs/frame 3845 fps_ceiling 3.1% budget
|
||||
ASCIIVideo_Stream_TrueColor_120fps 576 µs/frame 1735 fps_ceiling 6.9% budget
|
||||
|
||||
# Full pipeline (em.Write + renderer + io.Discard write)
|
||||
Pipeline_ASCIIVideo_8Color_120fps 493 µs/frame 2030 fps_ceiling 5.9% budget
|
||||
Pipeline_ASCIIVideo_TrueColor_120fps 1075 µs/frame 931 fps_ceiling 12.9% budget
|
||||
|
||||
# Emulator alone (libghostty-vt CSI/SGR parser)
|
||||
Emulator_Write_Stream_8Color_120fps 257 µs/frame 3890 fps_ceiling
|
||||
Emulator_Write_Stream_TrueColor_120fps 488 µs/frame 2051 fps_ceiling
|
||||
```
|
||||
|
||||
The current pipeline still has large 120 fps headroom. The remaining
|
||||
renderer concern is multi-MiB styled replay latency and allocation
|
||||
churn, not normal steady-state frame budget.
|
||||
|
||||
|
||||
- [ ] **viewport renderer allocates heavily on SGR/CSI-heavy chunks.** [MEDIUM]
|
||||
- Review evidence: five benchmark reps confirmed
|
||||
`ViewportRenderer_StyledLines` at about 4,325 allocs per 16 KiB
|
||||
chunk (~91.5 KB/op, roughly 1 alloc per 3.8 input bytes), and
|
||||
`ViewportRenderer_RatatuiBurst` at about 17,306 allocs per chunk
|
||||
(~365 KB/op). A 5 MiB styled resume benchmark allocated about
|
||||
31 MB across 1.38M objects.
|
||||
- Likely hot paths: generic CSI/SGR output in
|
||||
`internal/app/viewport_renderer.go` sends many sequences through
|
||||
`vr.shifter.Shift(vr.buf)`, while `internal/app/cursorshift.go`
|
||||
returns a fresh `[]byte` via `pending.String()` on every
|
||||
`Shift` call and parses CSI params through `string(raw)` /
|
||||
`strings.Split`. The mode-helper `string(params)` conversions
|
||||
are real, but probably not the main SGR-heavy cost.
|
||||
- Fix direction: make `cursorShifter` write into caller-owned
|
||||
scratch output or directly into the viewport renderer's pending
|
||||
builder; parse CSI params from byte slices; pre-grow/reuse
|
||||
renderer and shifter buffers. Re-run styled-lines, ratatui, and
|
||||
5 MiB resume benchmarks; use pprof when available to confirm the
|
||||
top allocation sites.
|
||||
|
||||
- [ ] **large styled resume/replay dumps spend visible time in viewport rendering.** [MEDIUM]
|
||||
- Review evidence: `BenchmarkSessionResume_5MiBStyled` measured
|
||||
about 58 ms median and 63 ms p95 over five reps. The plain 5 MiB
|
||||
benchmark was about 23-24 ms with only 21 allocs. The live path
|
||||
renders focused PTY chunks through `renderer.Render`, then still
|
||||
pays emulator writes, ring writes, event dispatch, stdout writes,
|
||||
and real terminal paint.
|
||||
- Scope: this is not a Codex steady-state throughput limit. A
|
||||
100 KB/s stream is far below the styled renderer's ~80-90 MB/s
|
||||
ceiling. It matters for multi-MiB burst replay, resume/startup
|
||||
dumps, and dense full-screen churn.
|
||||
- Fix direction: do the allocation fix first, since it should also
|
||||
improve throughput. After that, invest further only if styled
|
||||
resume traces remain user-visible or the styled-lines benchmark
|
||||
is still under roughly 300 MB/s.
|
||||
|
||||
- [ ] **wait_for_pattern re-scans the entire stream/grid while waiting.** [MEDIUM]
|
||||
- `internal/app/host.go:476-493` (the `check` closure). On
|
||||
`scope="scrollback"` it calls `c.StreamRead(0)` followed by
|
||||
`stripANSIBytes(nil, b)`, so each check can copy, strip, and
|
||||
search the full 1 MiB ring. On `scope="grid"` it calls
|
||||
`PlainText()` and runs the regex against the full grid string.
|
||||
- Caveat from review: the current chunk notifier coalesces bursts
|
||||
with a buffered channel and has a 500 ms fallback, so this is not
|
||||
necessarily one full scan per PTY chunk. It is still meaningful
|
||||
for active waits on chatty panes.
|
||||
- Fix direction: for `scrollback`, track the last checked stream
|
||||
offset and search only new output plus a bounded overlap/scratch
|
||||
buffer so matches spanning chunks are not missed. For `grid`,
|
||||
dedupe on `ScreenVersion()` and skip work when the version has
|
||||
not changed.
|
||||
|
||||
- [ ] **search_output rebuilds and searches whole scrollback on every call.** [MEDIUM]
|
||||
- `internal/app/host.go:428-437` compiles a fresh regex, reads the
|
||||
stream from offset 0, strips ANSI for `kind="rendered"`, converts
|
||||
the full buffer to a string, and splits it into lines before
|
||||
applying `limit`. This is meaningful when agents poll the same
|
||||
pattern; it is low impact for ad hoc searches.
|
||||
- Fix direction: cache compiled regexes by pattern; cache stripped
|
||||
rendered output by child id and stream end offset; avoid
|
||||
`strings.Split` over the whole ring when only the first `limit`
|
||||
matches are needed. Prefer an incremental search shape if this
|
||||
becomes the standard "watch for marker" path.
|
||||
|
||||
# On Hold
|
||||
- [ ] There's a unicode <?> being displayed in opencode [ON HOLD]
|
||||
- Investigated 2026-05-14: patterm passes ghostty grapheme codepoints
|
||||
through unchanged (vt/ghostty.go:452-462), so the `<?>` glyph is
|
||||
most likely the *host* terminal's font fallback for opencode's
|
||||
Nerd Font private-use codepoints, not a patterm substitution.
|
||||
Need a concrete reproduction (which codepoint, which host
|
||||
terminal/font) before changing rendering.
|
||||
- [ ] After codex rips for like 15 minutes, the terminal becomes quite slow. [ON HOLD / VERIFYING]
|
||||
- 2026-05-14: Perf plan P1-P11 landed (see CHANGELOG). Needs a real
|
||||
long-running codex session to confirm whether the steady-state
|
||||
slowdown is gone or some hotspot remains. Capture a pprof if it
|
||||
still feels slow after ≥15 minutes — the structural drivers the
|
||||
audit named are all addressed, so a remaining symptom is a new
|
||||
one and probably wants fresh profiling.
|
||||
|
||||
@@ -14,7 +14,9 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
@@ -27,6 +29,7 @@ import (
|
||||
"github.com/hjbdev/patterm/internal/app"
|
||||
"github.com/hjbdev/patterm/internal/mcp"
|
||||
"github.com/hjbdev/patterm/internal/projectkey"
|
||||
"github.com/hjbdev/patterm/internal/protocol"
|
||||
)
|
||||
|
||||
// version is overridden at build time via `-ldflags "-X main.version=..."`.
|
||||
@@ -48,10 +51,25 @@ func main() {
|
||||
runDebugHarness()
|
||||
return
|
||||
}
|
||||
if len(os.Args) >= 2 && os.Args[1] == "daemon" {
|
||||
os.Args = append(os.Args[:1], os.Args[2:]...)
|
||||
runDaemonCommand()
|
||||
return
|
||||
}
|
||||
if len(os.Args) >= 2 && os.Args[1] == "connect" {
|
||||
os.Args = append(os.Args[:1], os.Args[2:]...)
|
||||
runConnectCommand()
|
||||
return
|
||||
}
|
||||
if len(os.Args) >= 2 && os.Args[1] == "ls" {
|
||||
runDaemonList()
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
projectDir = flag.String("project", "", "project directory (default $PWD)")
|
||||
showVersion = flag.Bool("version", false, "print version and exit")
|
||||
inProcess = flag.Bool("in-process", false, "run the legacy single-process TUI instead of attaching to the daemon")
|
||||
debugDir = flag.String("debug", "", "write debug logs + per-child raw PTY output to DIR (auto-picks a dated subdir under $XDG_STATE_HOME/patterm/debug when DIR is omitted)")
|
||||
profileDir = flag.String("profile", "", "write pprof files (cpu/heap/goroutine) and live perf counters (metrics.jsonl per-second, metrics.json + summary.txt on exit) to DIR (auto-picks a dated subdir under $XDG_STATE_HOME/patterm/profile when DIR is omitted)")
|
||||
)
|
||||
@@ -72,6 +90,8 @@ func main() {
|
||||
}
|
||||
if *projectDir != "" {
|
||||
cwd = *projectDir
|
||||
} else if flag.NArg() > 0 {
|
||||
cwd = flag.Arg(0)
|
||||
}
|
||||
key, err := projectkey.Key(cwd)
|
||||
if err != nil {
|
||||
@@ -95,11 +115,26 @@ func main() {
|
||||
defer stopProfile()
|
||||
|
||||
ctx := context.Background()
|
||||
if err := app.Run(ctx, app.Options{
|
||||
if *inProcess || os.Getenv("PATTERM_NO_DAEMON") != "" {
|
||||
if err := app.Run(ctx, app.Options{
|
||||
ProjectDir: cwd,
|
||||
ProjectKey: key,
|
||||
DebugDir: resolvedDebug,
|
||||
ProfileDir: resolvedProfile,
|
||||
}); err != nil {
|
||||
die("%v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if resolvedDebug != "" || resolvedProfile != "" {
|
||||
die("--debug and --profile currently require --in-process")
|
||||
}
|
||||
if err := app.RunAttachedClient(ctx, app.ClientOptions{
|
||||
ProjectDir: cwd,
|
||||
ProjectKey: key,
|
||||
DebugDir: resolvedDebug,
|
||||
ProfileDir: resolvedProfile,
|
||||
Stdin: os.Stdin,
|
||||
Stdout: os.Stdout,
|
||||
RawMode: true,
|
||||
AutoStart: true,
|
||||
}); err != nil {
|
||||
die("%v", err)
|
||||
}
|
||||
@@ -194,6 +229,141 @@ func runMCPProxy() {
|
||||
}
|
||||
}
|
||||
|
||||
func runDaemonCommand() {
|
||||
if len(os.Args) >= 2 && os.Args[1] == "stop" {
|
||||
runDaemonStop()
|
||||
return
|
||||
}
|
||||
if len(os.Args) >= 2 && os.Args[1] == "ls" {
|
||||
runDaemonList()
|
||||
return
|
||||
}
|
||||
var (
|
||||
projectDir = flag.String("project", "", "initial project directory (default $PWD)")
|
||||
listenAddr = flag.String("listen", "", "optional TCP listen address for remote human clients (for example 127.0.0.1:2488, 0.0.0.0:2488, or 2488)")
|
||||
)
|
||||
flag.Parse()
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
die("getwd: %v", err)
|
||||
}
|
||||
if *projectDir != "" {
|
||||
cwd = *projectDir
|
||||
} else if flag.NArg() > 0 {
|
||||
cwd = flag.Arg(0)
|
||||
}
|
||||
if err := app.RunDaemon(context.Background(), app.DaemonOptions{ProjectDir: cwd, ListenAddr: *listenAddr}); err != nil {
|
||||
die("daemon: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func runConnectCommand() {
|
||||
var (
|
||||
host = flag.String("host", "", "remote daemon host:port")
|
||||
token = flag.String("token", "", "remote daemon token (default PATTERM_TOKEN or stored token file)")
|
||||
projectDir = flag.String("project", "", "project directory to request on the daemon")
|
||||
)
|
||||
flag.Parse()
|
||||
if *host == "" && flag.NArg() > 0 {
|
||||
*host = flag.Arg(0)
|
||||
}
|
||||
if *host == "" {
|
||||
die("connect: --host HOST:PORT is required")
|
||||
}
|
||||
tok := *token
|
||||
if tok == "" {
|
||||
tok = os.Getenv("PATTERM_TOKEN")
|
||||
}
|
||||
if tok == "" {
|
||||
if stored, err := app.LoadClientToken(); err == nil {
|
||||
tok = stored
|
||||
}
|
||||
}
|
||||
if tok == "" {
|
||||
die("connect: token required via --token, PATTERM_TOKEN, or %s", mustTokenPath())
|
||||
}
|
||||
cwd := *projectDir
|
||||
if cwd == "" {
|
||||
var err error
|
||||
cwd, err = os.Getwd()
|
||||
if err != nil {
|
||||
die("getwd: %v", err)
|
||||
}
|
||||
}
|
||||
tr, err := app.DialTCPTransport(*host)
|
||||
if err != nil {
|
||||
die("connect: %v", err)
|
||||
}
|
||||
defer tr.Close()
|
||||
if err := app.RunAttachedClient(context.Background(), app.ClientOptions{
|
||||
ProjectDir: cwd,
|
||||
Transport: tr,
|
||||
Stdin: os.Stdin,
|
||||
Stdout: os.Stdout,
|
||||
RawMode: true,
|
||||
Token: tok,
|
||||
}); err != nil {
|
||||
die("connect: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustTokenPath() string {
|
||||
path, err := app.ClientTokenPath()
|
||||
if err != nil {
|
||||
return "$XDG_DATA_HOME/patterm/clients/token"
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func runDaemonList() {
|
||||
projects, err := daemonRequest(protocol.Frame{Type: protocol.FrameList})
|
||||
if err != nil {
|
||||
die("ls: %v", err)
|
||||
}
|
||||
for _, p := range projects.Projects {
|
||||
fmt.Printf("%s\t%d\t%s\n", p.Key, p.TabCount, p.Path)
|
||||
}
|
||||
}
|
||||
|
||||
func runDaemonStop() {
|
||||
if _, err := daemonRequest(protocol.Frame{Type: protocol.FrameStop}); err != nil {
|
||||
die("daemon stop: %v", err)
|
||||
}
|
||||
fmt.Println("stopped")
|
||||
}
|
||||
|
||||
func daemonRequest(req protocol.Frame) (protocol.ProjectList, error) {
|
||||
socket, _, err := app.RuntimeDaemonPaths()
|
||||
if err != nil {
|
||||
return protocol.ProjectList{}, err
|
||||
}
|
||||
conn, err := net.Dial("unix", socket)
|
||||
if err != nil {
|
||||
return protocol.ProjectList{}, err
|
||||
}
|
||||
defer conn.Close()
|
||||
t := protocol.NewConnTransport(conn)
|
||||
if err := t.Send(req); err != nil {
|
||||
return protocol.ProjectList{}, err
|
||||
}
|
||||
resp, err := t.Recv()
|
||||
if err != nil {
|
||||
return protocol.ProjectList{}, err
|
||||
}
|
||||
if resp.Type == protocol.FrameError {
|
||||
var msg protocol.Error
|
||||
_ = json.Unmarshal(resp.Payload, &msg)
|
||||
if msg.Message == "" {
|
||||
msg.Message = "daemon returned an error"
|
||||
}
|
||||
return protocol.ProjectList{}, fmt.Errorf("%s", msg.Message)
|
||||
}
|
||||
if resp.Type != protocol.FrameProjectList {
|
||||
return protocol.ProjectList{}, fmt.Errorf("unexpected daemon response %q", resp.Type)
|
||||
}
|
||||
return protocol.Decode[protocol.ProjectList](resp)
|
||||
}
|
||||
|
||||
func versionString() string {
|
||||
commit, date := "unknown", "unknown"
|
||||
if info, ok := debug.ReadBuildInfo(); ok {
|
||||
|
||||
@@ -108,7 +108,7 @@ func run(argv []string, cols, rows uint16, idleMS int, followHost, stdinPassthro
|
||||
}
|
||||
defer em.Close()
|
||||
|
||||
child, err := pty.Start(argv, nil, cols, rows)
|
||||
child, err := pty.Start(argv, nil, "", cols, rows)
|
||||
if err != nil {
|
||||
return fmt.Errorf("pty: %w", err)
|
||||
}
|
||||
|
||||
273
docs/daemon-client-plan.md
Normal file
273
docs/daemon-client-plan.md
Normal file
@@ -0,0 +1,273 @@
|
||||
# patterm: persistent daemon + thin networked client — implementation plan
|
||||
|
||||
Status: implemented — Phases 0–4 landed on this branch. Branch: `feat/daemon-client-split`.
|
||||
|
||||
> Implemented: pty workdir/process-group + protocol/Transport/loopback foundation;
|
||||
> multi-project `ProjectRegistry`; out-of-process unix-socket daemon with auto-start,
|
||||
> `daemon stop`/`ls`, detach (Ctrl-]) + reconnect; opt-in LAN TCP listener with a
|
||||
> lightweight bearer token + `patterm connect`; per-pane display-owner sizing for
|
||||
> multi-client viewing. Deferred (not built): TLS (transport kept pluggable),
|
||||
> remote MCP, durable restore of live PTYs across daemon restart.
|
||||
|
||||
## Goal
|
||||
|
||||
Turn patterm from a single foreground process into a persistent background
|
||||
**daemon** that owns all process/project state, plus a thin **client** that
|
||||
renders and forwards input. A client on another LAN device can attach,
|
||||
navigate projects via the command palette, detach, and reconnect — with child
|
||||
processes surviving across client disconnects.
|
||||
|
||||
## Locked decisions
|
||||
|
||||
1. **Scope:** build all phases; land as one PR off this branch.
|
||||
2. **Remote access:** human UI clients only. MCP for agents stays local
|
||||
(per-daemon unix socket); no remote MCP transport in this work.
|
||||
3. **Multi-client = per-client independent view.** The daemon holds pure
|
||||
process/project state. Each client connection owns a `ClientView`
|
||||
(selected project, focused pane/pad, scroll offset, palette state,
|
||||
terminal size). Two clients may sit on different projects at once.
|
||||
4. **Daemon lifecycle:** auto-start on demand (tmux/docker model). `patterm`
|
||||
starts the daemon if absent and attaches; `patterm daemon stop|ls` manage it.
|
||||
5. **Durability:** "persistent" = survive client disconnect while the daemon
|
||||
process lives. Daemon restart only rehydrates today's persist model
|
||||
(top-level commands, fresh IDs). No attempt to resurrect live PTYs/agents
|
||||
after daemon death.
|
||||
6. **Auth (trusted-network stance):** Harry runs this on a trusted LAN and is
|
||||
fine with LAN exposure. Keep it lightweight: localhost default, opt-in LAN
|
||||
bind (`--listen`), a simple pairing/bearer token to prevent accidental
|
||||
drive-by access. TLS/cert-pinning is NOT required now but the transport must
|
||||
stay pluggable so TLS can be layered in later.
|
||||
7. **Detach gesture:** explicit detach via a palette command and/or a dedicated
|
||||
host chord. Ctrl-D stays as PTY input (shell EOF), as today. Quit-project and
|
||||
stop-daemon are explicit actions.
|
||||
|
||||
## Current architecture (baseline facts — verify before editing)
|
||||
|
||||
- `app.Run` (`internal/app/app.go:49`) wires the entire process: presets,
|
||||
settings, scratchpad/trust/persist stores, in-process MCP server, ONE
|
||||
`Session`, the `uiState` TUI, classifier, SIGWINCH, 60Hz chrome ticker,
|
||||
blocking `stdinLoop`.
|
||||
- **The seam:** `ChildEventListener` (`internal/app/session.go:83`) —
|
||||
`OnChildSpawned`/`OnChildExited`/`OnPTYOut`/`OnChildStateChanged`/
|
||||
`OnChildClosed`. Today `uiState` is the only real listener (subscribed at
|
||||
`app.go:198`). A remote client = a serialized listener + reverse command
|
||||
channel.
|
||||
- One `Session` (`session.go:28`) holds a flat `children map[string]*Child` +
|
||||
`order`. Tabs are derived: `KindAgent` children with `ParentID==""`
|
||||
(`tree.go` `runningTopLevels`). The whole tree is reconstructed from
|
||||
`Child.ParentID`.
|
||||
- `Child` (`child.go:72`) owns `*pty.PTY`, `*vt.GhosttyEmulator`, raw ring,
|
||||
status/owner atomics. Lifecycle: `Session.Spawn` (`session.go:222`) →
|
||||
`startPTY` → `pumpChild` (`session.go:423`, PTY→emulator→ring→`emitPTYOut`)
|
||||
+ `reapChild` (`session.go:488`, exit→`killDescendantsOf`).
|
||||
- Stores already keyed by projectKey on `Open`
|
||||
(`scratchpad`/`trust`/`persist`); `projectkey.Key(dir)` =
|
||||
`sha256(realpath)[:16]`.
|
||||
- `SerializeChild` (`session.go:687`) already yields a full VT snapshot for
|
||||
stateless repaint.
|
||||
- Rendering writes ANSI to `os.Stdout` under `outMu`; `viewportRenderer`
|
||||
(`internal/app/viewport_renderer.go`) is a stateful ANSI rewriter confining
|
||||
child output to the viewport. Input: raw `os.Stdin` via `stdinLoop`
|
||||
(`app.go:1433`)/`processStdin`.
|
||||
- MCP: in-process `Server` (`internal/mcp/mcp.go:26`), newline-JSON over a
|
||||
per-PID unix socket `$XDG_RUNTIME_DIR/patterm/<pid>.sock`. Agents launch
|
||||
`patterm mcp-stdio --socket S --identity T`. Identity → `callerID` via
|
||||
`host.ResolveCallerIdentity` → `Session.FindChildByIdentity`.
|
||||
- **No TCP/TLS anywhere today.** All `net.Listen`/`net.Dial` are unix sockets.
|
||||
- **Must-fix:** `pty.Start` (`internal/pty/pty.go:26`) does not set `cmd.Dir`;
|
||||
today the process `os.Chdir`s once. A daemon can't chdir globally, so
|
||||
`SpawnSpec.WorkDir` must propagate to `exec.Cmd.Dir`.
|
||||
|
||||
## Target component model
|
||||
|
||||
| Component | Owns |
|
||||
|---|---|
|
||||
| `internal/daemon` (`pattermd`) | Project registry (N `Session`s), all PTYs, emulators, MCP server, per-project stores, classifier, timers. No TTY. |
|
||||
| `internal/client` (`patterm`) | Real terminal: raw mode, alt-screen, SIGWINCH, stdin/stdout; `uiState`, `viewportRenderer`, chrome draws, palette, input. Holds `ClientView`. |
|
||||
| `internal/transport` | `Transport` interface + framing; loopback, unix, TCP/TLS impls; auth handshake. |
|
||||
| `internal/protocol` | Wire message types shared by daemon + client. |
|
||||
|
||||
### `Transport` interface (migration linchpin)
|
||||
|
||||
```go
|
||||
type Transport interface {
|
||||
Send(Frame) error // client→daemon command, or daemon→client push
|
||||
Recv() (Frame, error)
|
||||
Close() error
|
||||
}
|
||||
```
|
||||
|
||||
- **Loopback impl:** in-process channels, zero serialization. Default
|
||||
`patterm` = client + loopback daemon in one process → today's UX preserved
|
||||
exactly, single binary.
|
||||
- **Net impl:** framed JSON-per-line over `net.Conn`, reusing the
|
||||
`mcp.go:handleConn` pattern; unix socket first, then TCP/TLS.
|
||||
|
||||
### Per-client state vs daemon state
|
||||
|
||||
```go
|
||||
// daemon-side, pure process/project state
|
||||
type Registry struct { projects map[string]*Project } // key = projectKey
|
||||
type Project struct {
|
||||
Key, Dir, Name string
|
||||
Session *Session
|
||||
Pads *scratchpad.Store
|
||||
Trust *trust.Store
|
||||
Persist *persist.Store
|
||||
Launcher *Launcher
|
||||
Host *ToolHost
|
||||
}
|
||||
|
||||
// per-connection, client-owned view state (lives client-side; daemon tracks
|
||||
// only what it must to size emulators + route subscriptions)
|
||||
type ClientView struct {
|
||||
ID string
|
||||
ProjectKey string // which project this client is looking at
|
||||
FocusedID string // pane (Child) or pad
|
||||
ScrollOff int
|
||||
Cols, Rows uint16
|
||||
// palette state is fully client-local
|
||||
}
|
||||
```
|
||||
|
||||
Project switch = re-point this client's subscription to another `Project`'s
|
||||
Session + send `chrome` + `pane_snapshot`. No process teardown.
|
||||
|
||||
### Wire protocol (control + UI channel)
|
||||
|
||||
Bidirectional framed JSON-per-line.
|
||||
|
||||
Daemon → client:
|
||||
- `hello` / `auth_challenge` / `auth_ok` — handshake.
|
||||
- `project_list` — `[{key, path, name, last_active, tab_count}]` for the
|
||||
palette switcher.
|
||||
- `chrome` — semantic model for the client's current project+view: tab list
|
||||
(`runningTopLevels`), sidebar tree (`sidebarNav`), status/owner, toasts,
|
||||
scratchpad list + selected preview. Client draws chrome locally
|
||||
(reuses `tabbar.go`/`sidebar.go`).
|
||||
- `pane_snapshot{paneID, vtBytes}` — full repaint on focus/attach/switch via
|
||||
`SerializeChild`.
|
||||
- `pane_chunk{paneID, bytes}` — live focused-pane PTY output (serialized
|
||||
`OnPTYOut`).
|
||||
- `lifecycle{spawned|exited|closed|stateChanged,...}` — serialized listener.
|
||||
- `attention` / `trust_prompt` — human-facing surfaces; render on the client
|
||||
whose view owns the relevant project.
|
||||
|
||||
Client → daemon:
|
||||
- `attach{token, term_size, project_key?}` / `detach`.
|
||||
- `input{paneID, bytes}` (the `InjectAsUser` path).
|
||||
- `focus{paneID|pad}`, `switch_project{key}`, `open_project{path}`.
|
||||
- `palette_command{...}` (spawn/kill/rename/quit-project), `trust_response`,
|
||||
`resize{cols,rows}`.
|
||||
|
||||
**Encoding decision:** ship raw focused-pane PTY bytes + periodic
|
||||
`SerializeChild` snapshots; client runs its own `viewportRenderer`. No
|
||||
daemon-side pre-render (keeps daemon size-agnostic), no grid diffs in v1.
|
||||
Requires in-order delivery only (TCP gives it). Diffs are a later optimization.
|
||||
|
||||
### Emulator sizing with per-client views
|
||||
|
||||
Each `Child` emulator has one size. Rules:
|
||||
- A pane is sized by the client(s) viewing it. If exactly one client focuses a
|
||||
pane, that client's cols/rows drive `ResizeAll` for that pane.
|
||||
- If two clients focus the **same** pane, one is the **display owner** (first
|
||||
to focus, or explicit take-control); the owner's size drives the emulator;
|
||||
the other letterboxes/clips. Surface a toast.
|
||||
- Because clients are usually on different projects/panes, contention is rare.
|
||||
|
||||
### Security (human clients, LAN — trusted-network stance)
|
||||
|
||||
Harry runs this on a trusted LAN (decision #6). Keep it lightweight but not
|
||||
wide open:
|
||||
- localhost-only by default. LAN bind (`--listen 0.0.0.0:PORT`) is explicit
|
||||
opt-in, never default.
|
||||
- A simple pairing/bearer token gates network attach so a stray host on the LAN
|
||||
can't drive-by-attach. Daemon prints the token on `--listen`; client presents
|
||||
it in `attach`; store a per-client token after first pairing.
|
||||
- Local unix-socket clients keep `0600` perms (sufficient for same-user).
|
||||
- Keep the transport pluggable so TLS + cert pinning can be layered in later
|
||||
without reworking the protocol. Not building TLS now.
|
||||
- Trust prompts may now be approved from another device — deliberate; route to
|
||||
the client whose view owns the project.
|
||||
|
||||
### Daemon lifecycle (auto-start)
|
||||
|
||||
- Well-known local socket `$XDG_RUNTIME_DIR/patterm/daemon.sock` +
|
||||
pidfile/lockfile (single daemon per user).
|
||||
- `patterm [dir]`: dial the socket; if absent, fork-exec the daemon, wait for
|
||||
readiness, attach. `--project`/dir selects the initial project for the view.
|
||||
- `patterm daemon` (foreground), `patterm daemon stop`, `patterm ls`.
|
||||
- **Detach = explicit** palette command and/or a dedicated host chord; PTYs keep
|
||||
running. Ctrl-D stays as PTY input (shell EOF). Quitting a project / killing
|
||||
the daemon are explicit palette/CLI actions.
|
||||
- Idle-shutdown policy: configurable; default keep alive until explicit stop.
|
||||
|
||||
## Package-by-package changes
|
||||
|
||||
- **`cmd/patterm`** (`main.go`): add `daemon` subcommand (headless core);
|
||||
default invocation becomes client (auto-start/attach); `mcp-stdio` dials the
|
||||
shared daemon socket (not per-PID); `debug-harness` drives a daemon (or
|
||||
loopback).
|
||||
- **`internal/app` split:**
|
||||
- new **`internal/daemon`**: headless half — move `session.go`, `child.go`,
|
||||
`host.go`, `tree.go`, `launch.go`, classifier, timers, `Shutdown`,
|
||||
kill-cascade. Add `Registry`/`Project`.
|
||||
- **`internal/client`**: TTY half — `uiState`, `viewport_renderer.go`,
|
||||
`screen_renderer.go`, `tabbar.go`, `sidebar.go`, status, `palette.go`,
|
||||
`stdinLoop`/`processStdin`, SIGWINCH/chrome ticker, markdown/marquee/toast.
|
||||
Consumes events + chrome over `Transport` instead of `sess.Subscribe`.
|
||||
- **new `internal/transport` + `internal/protocol`**: messages, framing,
|
||||
loopback/unix/TCP-TLS impls, auth handshake.
|
||||
- **`internal/mcp`**: `SocketPath` per-daemon (not per-PID);
|
||||
`ResolveCallerIdentity` becomes daemon-wide across projects (token already
|
||||
carries `PATTERM_PROJECT_KEY` via `ChildEnv`).
|
||||
- **`internal/pty`**: set `cmd.Dir` from `SpawnSpec.WorkDir`; add process-group
|
||||
handling for reliable tree teardown.
|
||||
- **`internal/vt`**: unchanged grid source of truth; enforce per-child
|
||||
serialization around emulator access (interface isn't concurrency-safe) since
|
||||
clients + MCP + pump all snapshot.
|
||||
- **`internal/{scratchpad,trust,persist}`**: per-`Project` instances in the
|
||||
registry (already keyed by projectKey).
|
||||
- **`internal/preset`**: project-agnostic; daemon loads once, shares.
|
||||
- **`internal/projectkey`**: doc update (key is now load-bearing for routing).
|
||||
- **`internal/harness`**: add daemon/loopback mode; assert child survives client
|
||||
disconnect/reconnect, project-switch preserves each project's tree, two
|
||||
clients on different projects, unauth TCP rejected.
|
||||
|
||||
## Backpressure
|
||||
|
||||
`pumpChild`'s listener calls are synchronous (`session.go:149`). A slow network
|
||||
client must not block the PTY pump. Introduce a per-client event bus with a
|
||||
bounded buffer that coalesces/ drops to a snapshot under pressure, decoupled
|
||||
from `pumpChild`.
|
||||
|
||||
## Phased roadmap (all phases land on this branch)
|
||||
|
||||
0. **Extract headless core behind loopback transport.** `daemon.Core` +
|
||||
`client` over in-process `Transport`. Zero behavior change; harness green.
|
||||
1. **Multi-project registry + per-client view scaffolding.** Registry, per-
|
||||
project stores, `ClientView`, palette "Switch/Open project…", project tier
|
||||
in chrome. Still single local process.
|
||||
2. **Out-of-process daemon over unix socket.** Auto-start/attach; PTYs survive
|
||||
client exit; reconnect + snapshot-on-attach; Ctrl-D = detach; pidfile/lock.
|
||||
3. **TCP + TLS + auth.** localhost TCP, then opt-in LAN bind; pairing token /
|
||||
cert pinning; remote trust-prompt routing.
|
||||
4. **Per-client view fully realized + emulator sizing/display-owner.**
|
||||
Independent focus/scroll/palette per client; multi-client on same/different
|
||||
projects; resize negotiation + letterbox.
|
||||
5. **Hardening.** systemd/launchd autostart, `daemon stop|ls`, idle-shutdown,
|
||||
backpressure, security review, CHANGELOG.
|
||||
|
||||
## Risks / open questions for review
|
||||
|
||||
- Heterogeneous client sizes vs one-PTY-one-size (display-owner + letterbox is
|
||||
the v1 answer — is it sufficient?).
|
||||
- Security escalation: a network client spawns processes / runs shell / injects
|
||||
input. Auth/TLS scope adequate?
|
||||
- Ctrl-D semantics flip — acceptable UX?
|
||||
- Backpressure design — bounded bus + snapshot-on-pressure correct?
|
||||
- MCP identity uniqueness across projects after per-PID socket removal.
|
||||
- Is per-client view (decision #3) worth doing from Phase 1, or staged after a
|
||||
shared-focus interim that's faster to ship?
|
||||
- Splitting `uiState` (focus/palette/render caches/trust prompt/dims/outMu) out
|
||||
of the daemon is the largest refactor — sequencing concerns?
|
||||
@@ -1,61 +0,0 @@
|
||||
claude + new │ Processes
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━───────│ ─────────────────────────
|
||||
- abc1234 if no tag exists yet
|
||||
|
||||
4. Wire version into the release workflow
|
||||
|
||||
Update .gitea/workflows/release.yml lines 31-35 to inject the pushed tag:
|
||||
|
||||
go build -trimpath \
|
||||
-ldflags="-s -w -X main.version=${{ github.ref_name }}" \
|
||||
-o dist/patterm-${{ github.ref_name }}-linux-amd64 \
|
||||
./cmd/patterm
|
||||
|
||||
github.ref_name is the tag name (e.g. v0.0.1) because the workflow only
|
||||
triggers on tags: ['v*'].
|
||||
|
||||
5. Update inline doc comment
|
||||
|
||||
cmd/patterm/main.go header comment (lines 5-11) — add the --version form
|
||||
to the usage block. SPEC.md/CLAUDE.md already use --, no change needed there.
|
||||
|
||||
Out of scope
|
||||
|
||||
- Surfacing version in MCP whoami (the hardcoded "version": "0.1.0" in
|
||||
internal/mcp/protocol.go:27 is the MCP protocol version, not the patterm
|
||||
binary version — leave it).
|
||||
- Renaming any existing flags.
|
||||
- Adding short forms like -p for --project.
|
||||
|
||||
Critical files
|
||||
|
||||
- cmd/patterm/main.go — import swap, --version wiring, version var, header comment
|
||||
- cmd/patterm/debug_harness.go — import swap
|
||||
- Makefile lines 38-39 — VERSION var + ldflags
|
||||
- .gitea/workflows/release.yml lines 31-35 — ldflags
|
||||
- go.mod / go.sum — add github.com/spf13/pflag
|
||||
|
||||
Verification
|
||||
|
||||
1. go build -o ./bin/patterm ./cmd/patterm (without Makefile) → still builds, version reports dev.
|
||||
2. make patterm → ./bin/patterm --version prints patterm v0.0.1 (commit <sha>, built <date>).
|
||||
3. ./bin/patterm -h → help text shows --project string and --version lines.
|
||||
4. ./bin/patterm -project /tmp → pflag rejects with usage error (confirms -- is enforced).
|
||||
5. ./bin/patterm --project /tmp → starts normally.
|
||||
6. ./bin/patterm mcp-stdio --socket /tmp/s --identity x → existing path still works (will fail to connect, but should parse flags fine).
|
||||
7. ./bin/patterm debug-harness --scenario internal/harness/scenarios/spawn_process_via_palette.json → harness still runs.
|
||||
8. go test ./... and go test ./internal/harness/... — both green.
|
||||
9. Push a temporary tag locally and inspect git describe output; confirm release workflow's ${{ github.ref_name }} substitution matches the tag.
|
||||
╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
|
||||
|
||||
Claude has written up a plan and is ready to execute. Would you like to proceed?
|
||||
|
||||
❯ 1. Yes, and use auto mode
|
||||
2. Yes, manually approve edits
|
||||
3. No, refine with Ultraplan on Claude Code on the web
|
||||
4. Tell Claude what to change
|
||||
shift+tab to approve with this feedback
|
||||
|
||||
ctrl-g to edit in VS Code · ~/.claude/plans/flags-in-this-project-vectorized-gosling.md
|
||||
|
||||
claude · you have control Ctrl-A/D · tabs · Ctrl-W/S · tree · Ctrl-K · palette
|
||||
File diff suppressed because it is too large
Load Diff
@@ -26,6 +26,11 @@ import (
|
||||
// false positives (timestamps, exit codes, etc.).
|
||||
var portRegex = regexp.MustCompile(`https?://[^\s:/]+:(\d{2,5})(?:/[^\s]*)?`)
|
||||
|
||||
const (
|
||||
agentInterPieceDelay = 15 * time.Millisecond
|
||||
agentSubmitSettleDelay = 100 * time.Millisecond
|
||||
)
|
||||
|
||||
type ChildStatus string
|
||||
|
||||
const (
|
||||
@@ -223,7 +228,7 @@ func (c *Child) startPTY(cols, rows uint16) (uint64, error) {
|
||||
}
|
||||
starting := StatusStarting
|
||||
c.status.Store(&starting)
|
||||
p, err := pkgpty.Start(c.Argv, c.Env, cols, rows)
|
||||
p, err := pkgpty.Start(c.Argv, c.Env, c.WorkDir, cols, rows)
|
||||
if err != nil {
|
||||
em.Close()
|
||||
errored := StatusErrored
|
||||
@@ -625,25 +630,25 @@ func (c *Child) InjectAsOrchestrator(b []byte) error {
|
||||
}
|
||||
|
||||
// writeInput is the shared PTY write path used by both injection
|
||||
// flavours. Each Enter byte (CR or LF) is split onto its own write
|
||||
// with a brief delay so TUI agents with paste-detection (claude,
|
||||
// flavours. Agent panes split each Enter byte (CR or LF) onto its own
|
||||
// write with a brief delay so TUI agents with paste-detection (claude,
|
||||
// codex, opencode) don't coalesce a trailing CR into the text that
|
||||
// preceded it. Without the split, `pty.Write([]byte("hello\r"))`
|
||||
// arrives at the agent as one read() and gets treated as multi-line
|
||||
// pasted content rather than "key Enter".
|
||||
// preceded it. Raw terminals and command panes receive the original
|
||||
// byte stream in one write; otherwise a multiline paste pays the agent
|
||||
// workaround's delay once per line.
|
||||
func (c *Child) writeInput(b []byte) error {
|
||||
pty := c.PTY()
|
||||
if pty == nil {
|
||||
return errors.New("child has no pty")
|
||||
}
|
||||
pieces := splitOnEnter(b)
|
||||
pieces := inputWritePieces(c.Kind, b)
|
||||
if len(pieces) <= 1 {
|
||||
_, err := pty.Write(b)
|
||||
return err
|
||||
}
|
||||
for i, piece := range pieces {
|
||||
if i > 0 {
|
||||
time.Sleep(15 * time.Millisecond)
|
||||
if delay := pieceWriteDelay(i, len(pieces), piece); delay > 0 {
|
||||
time.Sleep(delay)
|
||||
}
|
||||
if _, err := pty.Write(piece); err != nil {
|
||||
return err
|
||||
@@ -652,6 +657,27 @@ func (c *Child) writeInput(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func inputWritePieces(kind ChildKind, b []byte) [][]byte {
|
||||
if kind != KindAgent {
|
||||
return [][]byte{b}
|
||||
}
|
||||
return splitOnEnter(b)
|
||||
}
|
||||
|
||||
func pieceWriteDelay(index, total int, piece []byte) time.Duration {
|
||||
if index == 0 {
|
||||
return 0
|
||||
}
|
||||
if index == total-1 && isLoneEnter(piece) {
|
||||
return agentSubmitSettleDelay
|
||||
}
|
||||
return agentInterPieceDelay
|
||||
}
|
||||
|
||||
func isLoneEnter(piece []byte) bool {
|
||||
return len(piece) == 1 && (piece[0] == '\r' || piece[0] == '\n')
|
||||
}
|
||||
|
||||
func mintIdentity() string {
|
||||
var buf [12]byte
|
||||
_, _ = rand.Read(buf[:])
|
||||
|
||||
90
internal/app/child_input_test.go
Normal file
90
internal/app/child_input_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestInputWritePiecesOnlySplitAgentEnters(t *testing.T) {
|
||||
in := []byte("alpha\nbeta\rgamma")
|
||||
for _, kind := range []ChildKind{KindTerminal, KindCommand} {
|
||||
t.Run(string(kind), func(t *testing.T) {
|
||||
got := inputWritePieces(kind, in)
|
||||
if len(got) != 1 || !bytes.Equal(got[0], in) {
|
||||
t.Fatalf("inputWritePieces(%s) = %#v, want one original chunk", kind, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
got := inputWritePieces(KindAgent, in)
|
||||
if len(got) != 5 {
|
||||
t.Fatalf("agent pieces len = %d, want 5 (%#v)", len(got), got)
|
||||
}
|
||||
want := [][]byte{[]byte("alpha"), []byte("\n"), []byte("beta"), []byte("\r"), []byte("gamma")}
|
||||
for i := range want {
|
||||
if !bytes.Equal(got[i], want[i]) {
|
||||
t.Fatalf("agent piece %d = %q, want %q", i, got[i], want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPieceWriteDelay(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
index int
|
||||
total int
|
||||
piece []byte
|
||||
want time.Duration
|
||||
}{
|
||||
{
|
||||
name: "first piece",
|
||||
index: 0,
|
||||
total: 3,
|
||||
piece: []byte("body"),
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "middle body piece",
|
||||
index: 1,
|
||||
total: 3,
|
||||
piece: []byte("body"),
|
||||
want: agentInterPieceDelay,
|
||||
},
|
||||
{
|
||||
name: "final carriage return submit",
|
||||
index: 1,
|
||||
total: 2,
|
||||
piece: []byte("\r"),
|
||||
want: agentSubmitSettleDelay,
|
||||
},
|
||||
{
|
||||
name: "final newline submit",
|
||||
index: 1,
|
||||
total: 2,
|
||||
piece: []byte("\n"),
|
||||
want: agentSubmitSettleDelay,
|
||||
},
|
||||
{
|
||||
name: "final non-enter piece",
|
||||
index: 2,
|
||||
total: 3,
|
||||
piece: []byte("tail"),
|
||||
want: agentInterPieceDelay,
|
||||
},
|
||||
{
|
||||
name: "standalone enter fast path",
|
||||
index: 0,
|
||||
total: 1,
|
||||
piece: []byte("\r"),
|
||||
want: 0,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := pieceWriteDelay(tc.index, tc.total, tc.piece); got != tc.want {
|
||||
t.Fatalf("pieceWriteDelay(%d, %d, %q) = %s, want %s", tc.index, tc.total, tc.piece, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
80
internal/app/chrome_model.go
Normal file
80
internal/app/chrome_model.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package app
|
||||
|
||||
import "github.com/hjbdev/patterm/internal/scratchpad"
|
||||
|
||||
// chromeModel is the semantic host chrome state. Renderers continue to own
|
||||
// ANSI output; this model is the serializable shape a client can draw locally.
|
||||
type chromeModel struct {
|
||||
ProjectKey string `json:"project_key"`
|
||||
ProjectName string `json:"project_name,omitempty"`
|
||||
FocusedID string `json:"focused_id,omitempty"`
|
||||
FocusedPad string `json:"focused_pad,omitempty"`
|
||||
ActiveAgentID string `json:"active_agent_id,omitempty"`
|
||||
Tabs []childModel `json:"tabs"`
|
||||
Processes []childModel `json:"processes"`
|
||||
AgentTree []childModel `json:"agent_tree"`
|
||||
Sidebar []navEntryModel `json:"sidebar"`
|
||||
Scratchpads []scratchpadModel `json:"scratchpads"`
|
||||
}
|
||||
|
||||
type childModel struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Kind string `json:"kind"`
|
||||
ParentID string `json:"parent_id,omitempty"`
|
||||
Status string `json:"status"`
|
||||
Owner string `json:"owner"`
|
||||
}
|
||||
|
||||
type navEntryModel struct {
|
||||
ChildID string `json:"child_id,omitempty"`
|
||||
Pad string `json:"pad,omitempty"`
|
||||
}
|
||||
|
||||
type scratchpadModel struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func buildChromeModel(projectKey string, view ClientView, children []*Child, pads []scratchpad.Entry) chromeModel {
|
||||
active := view.ActiveAgentID
|
||||
if active == "" {
|
||||
active = activeRootID(children, view.FocusedID)
|
||||
}
|
||||
model := chromeModel{
|
||||
ProjectKey: projectKey,
|
||||
ProjectName: view.ProjectName,
|
||||
FocusedID: view.FocusedID,
|
||||
FocusedPad: view.FocusedPad,
|
||||
ActiveAgentID: active,
|
||||
}
|
||||
for _, c := range runningTopLevels(children) {
|
||||
model.Tabs = append(model.Tabs, serializeChildModel(c))
|
||||
}
|
||||
for _, c := range processList(children) {
|
||||
model.Processes = append(model.Processes, serializeChildModel(c))
|
||||
}
|
||||
for _, c := range visibleAgentTree(children, active) {
|
||||
model.AgentTree = append(model.AgentTree, serializeChildModel(c))
|
||||
}
|
||||
for _, n := range sidebarNav(children, active, pads) {
|
||||
model.Sidebar = append(model.Sidebar, navEntryModel{ChildID: n.childID, Pad: n.pad})
|
||||
}
|
||||
for _, p := range pads {
|
||||
model.Scratchpads = append(model.Scratchpads, scratchpadModel{Name: p.Name})
|
||||
}
|
||||
return model
|
||||
}
|
||||
|
||||
func serializeChildModel(c *Child) childModel {
|
||||
if c == nil {
|
||||
return childModel{}
|
||||
}
|
||||
return childModel{
|
||||
ID: c.ID,
|
||||
Name: c.DisplayName(),
|
||||
Kind: string(c.Kind),
|
||||
ParentID: c.ParentID,
|
||||
Status: string(c.Status()),
|
||||
Owner: string(c.Owner()),
|
||||
}
|
||||
}
|
||||
24
internal/app/chrome_model_test.go
Normal file
24
internal/app/chrome_model_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package app
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestBuildChromeModelSeparatesProcessesTabsAndSidebar(t *testing.T) {
|
||||
running := StatusRunning
|
||||
proc := testProcess("p1", "server", running)
|
||||
agent := testAgent("a1", "codex", "", running)
|
||||
sub := testAgent("a2", "worker", "a1", running)
|
||||
|
||||
model := buildChromeModel("project", ClientView{FocusedID: "p1", ActiveAgentID: "a1"}, []*Child{proc, agent, sub}, nil)
|
||||
if len(model.Tabs) != 1 || model.Tabs[0].ID != "a1" {
|
||||
t.Fatalf("tabs = %#v, want only top-level agent", model.Tabs)
|
||||
}
|
||||
if len(model.Processes) != 1 || model.Processes[0].ID != "p1" {
|
||||
t.Fatalf("processes = %#v, want process section", model.Processes)
|
||||
}
|
||||
if len(model.AgentTree) != 2 || model.AgentTree[0].ID != "a1" || model.AgentTree[1].ID != "a2" {
|
||||
t.Fatalf("agent tree = %#v", model.AgentTree)
|
||||
}
|
||||
if len(model.Sidebar) != 3 || model.Sidebar[0].ChildID != "p1" || model.Sidebar[1].ChildID != "a1" {
|
||||
t.Fatalf("sidebar = %#v", model.Sidebar)
|
||||
}
|
||||
}
|
||||
@@ -50,8 +50,14 @@ func (s *Session) classifyOne(c *Child) {
|
||||
idleMS := c.IdleMS()
|
||||
titleIdleMS := c.TitleIdleMS()
|
||||
title := c.Title()
|
||||
tail := c.tailBytes(classifierTailBytes)
|
||||
state, reason := classify(c.idleDetection, exited, exitNonZero, idleMS, titleIdleMS, title, tail)
|
||||
tail := stripANSIBytes(nil, c.tailBytes(classifierTailBytes))
|
||||
var screen []byte
|
||||
if em := c.Emulator(); em != nil {
|
||||
if txt, err := em.ScreenText(); err == nil {
|
||||
screen = []byte(txt)
|
||||
}
|
||||
}
|
||||
state, reason := classify(c.idleDetection, exited, exitNonZero, idleMS, titleIdleMS, title, tail, screen)
|
||||
if c.setIdleState(state, reason) {
|
||||
s.emitStateChanged(c.ID, state)
|
||||
}
|
||||
|
||||
677
internal/app/client_net.go
Normal file
677
internal/app/client_net.go
Normal file
@@ -0,0 +1,677 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
cpty "github.com/creack/pty"
|
||||
"golang.org/x/term"
|
||||
|
||||
"github.com/hjbdev/patterm/internal/protocol"
|
||||
)
|
||||
|
||||
const (
|
||||
clientKeyCtrlK byte = 0x0b
|
||||
clientKeyCtrlBracket byte = 0x1d
|
||||
)
|
||||
|
||||
type ClientOptions struct {
|
||||
ProjectDir string
|
||||
Transport protocol.Transport
|
||||
Stdin io.Reader
|
||||
Stdout io.Writer
|
||||
RawMode bool
|
||||
AutoStart bool
|
||||
Token string
|
||||
Cols uint16
|
||||
Rows uint16
|
||||
}
|
||||
|
||||
func RunAttachedClient(ctx context.Context, opts ClientOptions) error {
|
||||
if opts.ProjectDir == "" {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.ProjectDir = cwd
|
||||
}
|
||||
if opts.Stdin == nil {
|
||||
opts.Stdin = os.Stdin
|
||||
}
|
||||
if opts.Stdout == nil {
|
||||
opts.Stdout = os.Stdout
|
||||
}
|
||||
if opts.Transport == nil {
|
||||
t, err := dialDaemonTransport(opts.ProjectDir, opts.AutoStart)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.Transport = t
|
||||
defer t.Close()
|
||||
}
|
||||
if opts.Cols == 0 || opts.Rows == 0 {
|
||||
opts.Cols, opts.Rows = clientHostSize(opts.Stdin)
|
||||
}
|
||||
c := newNetClient(opts)
|
||||
return c.run(ctx)
|
||||
}
|
||||
|
||||
func DialTCPTransport(addr string) (protocol.Transport, error) {
|
||||
conn, err := net.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return protocol.NewConnTransport(conn), nil
|
||||
}
|
||||
|
||||
func dialDaemonTransport(projectDir string, autoStart bool) (protocol.Transport, error) {
|
||||
socket, _, err := RuntimeDaemonPaths()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn, err := net.Dial("unix", socket)
|
||||
if err == nil {
|
||||
return protocol.NewConnTransport(conn), nil
|
||||
}
|
||||
if !autoStart {
|
||||
return nil, err
|
||||
}
|
||||
if err := startDaemonProcess(projectDir); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
var last error
|
||||
for time.Now().Before(deadline) {
|
||||
conn, err = net.Dial("unix", socket)
|
||||
if err == nil {
|
||||
return protocol.NewConnTransport(conn), nil
|
||||
}
|
||||
last = err
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
return nil, fmt.Errorf("daemon did not become ready: %w", last)
|
||||
}
|
||||
|
||||
func startDaemonProcess(projectDir string) error {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd := exec.Command(exe, "daemon", "--project", projectDir)
|
||||
devNull, err := os.OpenFile(os.DevNull, os.O_RDWR, 0)
|
||||
if err == nil {
|
||||
defer devNull.Close()
|
||||
cmd.Stdin = devNull
|
||||
cmd.Stdout = devNull
|
||||
cmd.Stderr = devNull
|
||||
}
|
||||
cmd.Env = os.Environ()
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
return cmd.Process.Release()
|
||||
}
|
||||
|
||||
type netClient struct {
|
||||
t protocol.Transport
|
||||
in io.Reader
|
||||
out io.Writer
|
||||
raw bool
|
||||
projectDir string
|
||||
token string
|
||||
layout terminalLayout
|
||||
|
||||
mu sync.Mutex
|
||||
focusedID string
|
||||
paneSize protocol.Size
|
||||
ownerView bool
|
||||
chrome chromeModel
|
||||
renderer *viewportRenderer
|
||||
palette *clientCommandPrompt
|
||||
}
|
||||
|
||||
type clientCommandPrompt struct {
|
||||
buf []byte
|
||||
}
|
||||
|
||||
func newNetClient(opts ClientOptions) *netClient {
|
||||
layout := newTerminalLayout(opts.Cols, opts.Rows)
|
||||
return &netClient{
|
||||
t: opts.Transport,
|
||||
in: opts.Stdin,
|
||||
out: opts.Stdout,
|
||||
raw: opts.RawMode,
|
||||
projectDir: opts.ProjectDir,
|
||||
token: opts.Token,
|
||||
layout: layout,
|
||||
renderer: newViewportRenderer(layout),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *netClient) run(ctx context.Context) error {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
var restore *term.State
|
||||
if c.raw {
|
||||
if f, ok := c.in.(*os.File); ok && term.IsTerminal(int(f.Fd())) {
|
||||
st, err := term.MakeRaw(int(f.Fd()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
restore = st
|
||||
defer term.Restore(int(f.Fd()), restore)
|
||||
}
|
||||
}
|
||||
c.enterScreen()
|
||||
defer c.leaveScreen()
|
||||
|
||||
if err := c.sendAttach(); err != nil {
|
||||
return err
|
||||
}
|
||||
errCh := make(chan error, 2)
|
||||
go func() { errCh <- c.recvLoop(ctx, cancel) }()
|
||||
go func() { errCh <- c.stdinLoop(ctx, cancel) }()
|
||||
if f, ok := c.in.(*os.File); ok && term.IsTerminal(int(f.Fd())) {
|
||||
winch := make(chan os.Signal, 1)
|
||||
signal.Notify(winch, syscall.SIGWINCH)
|
||||
defer signal.Stop(winch)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-winch:
|
||||
cols, rows := clientHostSize(c.in)
|
||||
_ = c.resize(cols, rows)
|
||||
c.enterScreen()
|
||||
c.drawChrome()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
_ = c.t.Close()
|
||||
return nil
|
||||
case err := <-errCh:
|
||||
cancel()
|
||||
_ = c.t.Close()
|
||||
if errors.Is(err, io.EOF) || errors.Is(err, protocol.ErrTransportClosed) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (c *netClient) sendAttach() error {
|
||||
f, err := protocol.NewFrame(protocol.FrameAttach, protocol.Attach{
|
||||
ProjectPath: c.projectPath(),
|
||||
Token: c.token,
|
||||
TermSize: protocol.Size{
|
||||
Cols: c.layout.childCols(),
|
||||
Rows: c.layout.childRows(),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.t.Send(f)
|
||||
}
|
||||
|
||||
func (c *netClient) projectPath() string {
|
||||
return c.projectDir
|
||||
}
|
||||
|
||||
func (c *netClient) recvLoop(ctx context.Context, cancel func()) error {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
default:
|
||||
}
|
||||
f, err := c.t.Recv()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.handleFrame(f); err != nil {
|
||||
return err
|
||||
}
|
||||
if f.Type == protocol.FrameDetach {
|
||||
cancel()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *netClient) handleFrame(f protocol.Frame) error {
|
||||
switch f.Type {
|
||||
case protocol.FrameError:
|
||||
msg, _ := protocol.Decode[protocol.Error](f)
|
||||
if msg.Message == "" {
|
||||
msg.Message = "daemon error"
|
||||
}
|
||||
return fmt.Errorf("%s", msg.Message)
|
||||
case protocol.FrameHello:
|
||||
return nil
|
||||
case protocol.FrameProjectList:
|
||||
return nil
|
||||
case protocol.FrameChrome:
|
||||
msg, err := protocol.Decode[protocol.Chrome](f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var model chromeModel
|
||||
if err := json.Unmarshal(msg.Model, &model); err != nil {
|
||||
return err
|
||||
}
|
||||
c.mu.Lock()
|
||||
c.chrome = model
|
||||
if model.FocusedID != "" {
|
||||
c.focusedID = model.FocusedID
|
||||
}
|
||||
c.mu.Unlock()
|
||||
c.drawChrome()
|
||||
case protocol.FramePaneSnapshot:
|
||||
msg, err := protocol.Decode[protocol.PaneSnapshot](f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.mu.Lock()
|
||||
c.focusedID = msg.PaneID
|
||||
c.paneSize = msg.Size
|
||||
c.ownerView = msg.DisplayOwner
|
||||
c.renderer = newViewportRenderer(c.renderLayoutLocked(msg.Size))
|
||||
renderer := c.renderer
|
||||
c.mu.Unlock()
|
||||
c.clearViewport()
|
||||
c.drawChrome()
|
||||
c.writeWrapped(renderer.Render(msg.Bytes))
|
||||
case protocol.FramePaneChunk:
|
||||
msg, err := protocol.Decode[protocol.PaneChunk](f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.mu.Lock()
|
||||
focused := c.focusedID
|
||||
renderer := c.renderer
|
||||
c.paneSize = msg.Size
|
||||
c.ownerView = msg.DisplayOwner
|
||||
if renderer != nil && (msg.Size.Cols != 0 || msg.Size.Rows != 0) {
|
||||
renderer.SetLayout(c.renderLayoutLocked(msg.Size))
|
||||
}
|
||||
c.mu.Unlock()
|
||||
if msg.PaneID == focused && renderer != nil {
|
||||
c.writeWrapped(renderer.Render(msg.Bytes))
|
||||
}
|
||||
case protocol.FrameLifecycle:
|
||||
// The daemon follows lifecycle changes with chrome/snapshot updates
|
||||
// when focus changes. Keep this as a wake point for future richer
|
||||
// client-side state without blocking the frame stream.
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *netClient) stdinLoop(ctx context.Context, cancel func()) error {
|
||||
buf := make([]byte, 4096)
|
||||
for {
|
||||
n, err := c.in.Read(buf)
|
||||
if n > 0 {
|
||||
if done, perr := c.processInput(buf[:n]); perr != nil || done {
|
||||
cancel()
|
||||
return perr
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *netClient) processInput(chunk []byte) (bool, error) {
|
||||
c.mu.Lock()
|
||||
if c.palette != nil {
|
||||
p := c.palette
|
||||
c.mu.Unlock()
|
||||
return c.processPaletteInput(p, chunk)
|
||||
}
|
||||
c.mu.Unlock()
|
||||
|
||||
forward := make([]byte, 0, len(chunk))
|
||||
flush := func() error {
|
||||
if len(forward) == 0 {
|
||||
return nil
|
||||
}
|
||||
c.mu.Lock()
|
||||
paneID := c.focusedID
|
||||
c.mu.Unlock()
|
||||
if paneID != "" {
|
||||
f, err := protocol.NewFrame(protocol.FrameInput, protocol.Input{PaneID: paneID, Bytes: append([]byte(nil), forward...)})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.t.Send(f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
forward = forward[:0]
|
||||
return nil
|
||||
}
|
||||
for _, b := range chunk {
|
||||
switch b {
|
||||
case clientKeyCtrlBracket:
|
||||
if err := flush(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, c.sendDetach()
|
||||
case clientKeyCtrlK:
|
||||
if err := flush(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
c.mu.Lock()
|
||||
c.palette = &clientCommandPrompt{}
|
||||
c.mu.Unlock()
|
||||
c.drawPrompt()
|
||||
case 0x17: // Ctrl-W: previous focus
|
||||
if err := flush(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
_ = c.focusRelative(-1)
|
||||
case 0x13: // Ctrl-S: next focus
|
||||
if err := flush(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
_ = c.focusRelative(1)
|
||||
default:
|
||||
forward = append(forward, b)
|
||||
}
|
||||
}
|
||||
return false, flush()
|
||||
}
|
||||
|
||||
func (c *netClient) processPaletteInput(p *clientCommandPrompt, chunk []byte) (bool, error) {
|
||||
for _, b := range chunk {
|
||||
switch b {
|
||||
case 0x1b: // ESC
|
||||
c.mu.Lock()
|
||||
c.palette = nil
|
||||
c.mu.Unlock()
|
||||
c.drawChrome()
|
||||
return false, nil
|
||||
case 'd':
|
||||
if len(p.buf) == 0 {
|
||||
c.mu.Lock()
|
||||
c.palette = nil
|
||||
c.mu.Unlock()
|
||||
return true, c.sendDetach()
|
||||
}
|
||||
p.buf = append(p.buf, b)
|
||||
case '\r', '\n':
|
||||
command := strings.TrimSpace(string(p.buf))
|
||||
c.mu.Lock()
|
||||
c.palette = nil
|
||||
c.mu.Unlock()
|
||||
if command == "" {
|
||||
c.drawChrome()
|
||||
return false, nil
|
||||
}
|
||||
return false, c.sendSpawnCommand(command)
|
||||
case 0x7f, 0x08:
|
||||
if len(p.buf) > 0 {
|
||||
p.buf = p.buf[:len(p.buf)-1]
|
||||
}
|
||||
c.drawPrompt()
|
||||
default:
|
||||
if b >= 0x20 {
|
||||
p.buf = append(p.buf, b)
|
||||
c.drawPrompt()
|
||||
}
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (c *netClient) sendDetach() error {
|
||||
f, err := protocol.NewFrame(protocol.FrameDetach, protocol.Detach{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.t.Send(f)
|
||||
}
|
||||
|
||||
func (c *netClient) sendSpawnCommand(command string) error {
|
||||
data, err := json.Marshal(map[string]any{
|
||||
"argv": []string{command},
|
||||
"name": command,
|
||||
"shell": true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := protocol.NewFrame(protocol.FramePaletteCommand, protocol.PaletteCommand{
|
||||
Kind: "spawn_command",
|
||||
Data: data,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.t.Send(f)
|
||||
}
|
||||
|
||||
func (c *netClient) focusRelative(delta int) error {
|
||||
c.mu.Lock()
|
||||
model := c.chrome
|
||||
current := c.focusedID
|
||||
c.mu.Unlock()
|
||||
ids := make([]string, 0, len(model.Processes)+len(model.AgentTree)+len(model.Tabs))
|
||||
for _, n := range model.Sidebar {
|
||||
if n.ChildID != "" {
|
||||
ids = append(ids, n.ChildID)
|
||||
}
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
for _, p := range model.Processes {
|
||||
ids = append(ids, p.ID)
|
||||
}
|
||||
for _, p := range model.Tabs {
|
||||
ids = append(ids, p.ID)
|
||||
}
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
idx := 0
|
||||
for i, id := range ids {
|
||||
if id == current {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
idx = (idx + delta + len(ids)) % len(ids)
|
||||
f, err := protocol.NewFrame(protocol.FrameFocus, protocol.Focus{PaneID: ids[idx]})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.t.Send(f)
|
||||
}
|
||||
|
||||
func (c *netClient) resize(cols, rows uint16) error {
|
||||
c.mu.Lock()
|
||||
c.layout = newTerminalLayout(cols, rows)
|
||||
if c.renderer != nil {
|
||||
c.renderer.SetLayout(c.renderLayoutLocked(c.paneSize))
|
||||
}
|
||||
size := protocol.Size{Cols: c.layout.childCols(), Rows: c.layout.childRows()}
|
||||
c.mu.Unlock()
|
||||
f, err := protocol.NewFrame(protocol.FrameResize, protocol.Resize{Size: size})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.t.Send(f)
|
||||
}
|
||||
|
||||
func (c *netClient) renderLayoutLocked(size protocol.Size) terminalLayout {
|
||||
l := c.layout
|
||||
if size.Cols != 0 && size.Cols < l.mainCols {
|
||||
l.mainCols = size.Cols
|
||||
}
|
||||
if size.Rows != 0 && size.Rows < l.mainRows {
|
||||
l.mainRows = size.Rows
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (c *netClient) enterScreen() {
|
||||
_, _ = c.out.Write([]byte("\x1b[?1049h\x1b[H\x1b[2J\x1b[?25h\x1b[?1000h\x1b[?1006h"))
|
||||
c.installScrollRegion()
|
||||
}
|
||||
|
||||
func (c *netClient) leaveScreen() {
|
||||
_, _ = c.out.Write([]byte("\x1b[r\x1b[?6l\x1b[?1006l\x1b[?1000l\x1b[?25h\x1b[?1049l"))
|
||||
}
|
||||
|
||||
func (c *netClient) installScrollRegion() {
|
||||
mainBottom := int(c.layout.statusRow) - statusRows
|
||||
if mainBottom < int(c.layout.mainTop) {
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(c.out, "\x1b[?6l\x1b[%d;%dr\x1b[%d;%dH",
|
||||
int(c.layout.mainTop), mainBottom,
|
||||
int(c.layout.mainTop), int(c.layout.mainLeft))
|
||||
}
|
||||
|
||||
func (c *netClient) clearViewport() {
|
||||
for row := int(c.layout.mainTop); row < int(c.layout.statusRow); row++ {
|
||||
fmt.Fprintf(c.out, "\x1b[%d;%dH\x1b[%dX", row, int(c.layout.mainLeft), int(c.layout.childCols()))
|
||||
}
|
||||
fmt.Fprintf(c.out, "\x1b[%d;%dH", int(c.layout.mainTop), int(c.layout.mainLeft))
|
||||
}
|
||||
|
||||
func (c *netClient) writeWrapped(out []byte) {
|
||||
if len(out) == 0 {
|
||||
return
|
||||
}
|
||||
wrapped := make([]byte, 0, len(out)+10)
|
||||
wrapped = append(wrapped, "\x1b[?7l"...)
|
||||
wrapped = append(wrapped, out...)
|
||||
wrapped = append(wrapped, "\x1b[?7h"...)
|
||||
_, _ = c.out.Write(wrapped)
|
||||
}
|
||||
|
||||
func (c *netClient) drawChrome() {
|
||||
c.mu.Lock()
|
||||
model := c.chrome
|
||||
prompt := c.palette
|
||||
c.mu.Unlock()
|
||||
var b strings.Builder
|
||||
width := int(c.layout.childCols())
|
||||
fmt.Fprintf(&b, "\x1b[1;1H\x1b[%dX\x1b[2;1H\x1b[%dX\x1b[3;1H\x1b[%dX", width, width, width)
|
||||
if len(model.Tabs) == 0 {
|
||||
fmt.Fprintf(&b, "\x1b[1;2H%s+ new%s", styleDim, styleReset)
|
||||
} else {
|
||||
col := 1
|
||||
for _, tab := range model.Tabs {
|
||||
label := fitName(tab.Name, 18)
|
||||
style := styleHint
|
||||
if tab.ID == model.ActiveAgentID || tab.ID == model.FocusedID {
|
||||
style = styleActive
|
||||
}
|
||||
fmt.Fprintf(&b, "\x1b[1;%dH%s %s %s", col, style, label, styleReset)
|
||||
col += visibleLen(label) + 3
|
||||
if col >= width {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(&b, "\x1b[3;1H%s%s%s", styleBorder, strings.Repeat("─", width), styleReset)
|
||||
if c.layout.sidebarVisible {
|
||||
c.appendSidebar(&b, model)
|
||||
}
|
||||
status := "Ctrl-K command palette · Ctrl-] detach"
|
||||
if model.FocusedID != "" {
|
||||
status = fmt.Sprintf("%s · %s", model.FocusedID, status)
|
||||
}
|
||||
c.mu.Lock()
|
||||
size := c.paneSize
|
||||
ownerView := c.ownerView
|
||||
c.mu.Unlock()
|
||||
if model.FocusedID != "" && !ownerView && size.Cols != 0 && size.Rows != 0 {
|
||||
status = fmt.Sprintf("viewing at owner size %dx%d · %s", size.Cols, size.Rows, status)
|
||||
}
|
||||
if prompt != nil {
|
||||
status = "command: " + string(prompt.buf)
|
||||
}
|
||||
fmt.Fprintf(&b, "\x1b[%d;1H\x1b[7m%s%s", int(c.layout.statusRow), fitName(status, int(c.layout.hostCols)), styleReset)
|
||||
_, _ = c.out.Write([]byte(b.String()))
|
||||
}
|
||||
|
||||
func (c *netClient) appendSidebar(b *strings.Builder, model chromeModel) {
|
||||
border := int(c.layout.sidebarLeft) - 1
|
||||
for row := 1; row <= int(c.layout.statusRow)-1; row++ {
|
||||
fmt.Fprintf(b, "\x1b[%d;%dH%s│%s", row, border, styleBorder, styleReset)
|
||||
}
|
||||
col := int(c.layout.sidebarLeft)
|
||||
row := 1
|
||||
write := func(text string) {
|
||||
if row >= int(c.layout.statusRow) {
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(b, "\x1b[%d;%dH%-*s", row, col, int(c.layout.sidebarWidth)-1, fitName(text, int(c.layout.sidebarWidth)-1))
|
||||
row++
|
||||
}
|
||||
write(styleActive + "Processes" + styleReset)
|
||||
for _, p := range model.Processes {
|
||||
prefix := " "
|
||||
if p.ID == model.FocusedID {
|
||||
prefix = "▎ "
|
||||
}
|
||||
write(prefix + p.Name)
|
||||
}
|
||||
row++
|
||||
write(styleActive + "Agent Tree" + styleReset)
|
||||
for _, p := range model.AgentTree {
|
||||
prefix := " "
|
||||
if p.ID == model.FocusedID {
|
||||
prefix = "▎ "
|
||||
}
|
||||
write(prefix + p.Name)
|
||||
}
|
||||
row++
|
||||
write(styleActive + "Scratchpads" + styleReset)
|
||||
for _, p := range model.Scratchpads {
|
||||
write(" " + p.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *netClient) drawPrompt() {
|
||||
c.drawChrome()
|
||||
}
|
||||
|
||||
func clientHostSize(r io.Reader) (cols, rows uint16) {
|
||||
if f, ok := r.(*os.File); ok {
|
||||
ws, err := cpty.GetsizeFull(f)
|
||||
if err == nil && ws.Cols > 0 && ws.Rows > 0 {
|
||||
return ws.Cols, ws.Rows
|
||||
}
|
||||
}
|
||||
return 120, 40
|
||||
}
|
||||
157
internal/app/client_net_test.go
Normal file
157
internal/app/client_net_test.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hjbdev/patterm/internal/protocol"
|
||||
)
|
||||
|
||||
func TestNetClientFrameLoopSendsFocusedInput(t *testing.T) {
|
||||
clientT, daemonT := protocol.NewLoopbackPair()
|
||||
inR, inW := ioPipe(t)
|
||||
out := &lockedBuffer{}
|
||||
|
||||
gotInput := make(chan protocol.Input, 1)
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
f, err := daemonT.Recv()
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
return
|
||||
}
|
||||
if f.Type != protocol.FrameAttach {
|
||||
t.Errorf("first frame = %s, want attach", f.Type)
|
||||
errCh <- nil
|
||||
return
|
||||
}
|
||||
sendTestFrame(t, daemonT, protocol.FrameHello, protocol.Hello{Version: 1, ClientID: "test", ProjectKey: "project"})
|
||||
sendTestFrame(t, daemonT, protocol.FrameProjectList, protocol.ProjectList{})
|
||||
model := chromeModel{
|
||||
ProjectKey: "project",
|
||||
FocusedID: "p1",
|
||||
Processes: []childModel{{ID: "p1", Name: "shell", Kind: string(KindCommand), Status: string(StatusRunning)}},
|
||||
Sidebar: []navEntryModel{{ChildID: "p1"}},
|
||||
}
|
||||
sendTestFrame(t, daemonT, protocol.FrameChrome, protocol.Chrome{ProjectKey: "project", Model: mustMarshalTest(t, model)})
|
||||
sendTestFrame(t, daemonT, protocol.FramePaneSnapshot, protocol.PaneSnapshot{PaneID: "p1", Bytes: []byte("READY")})
|
||||
for {
|
||||
f, err := daemonT.Recv()
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
return
|
||||
}
|
||||
if f.Type != protocol.FrameInput {
|
||||
continue
|
||||
}
|
||||
input, err := protocol.Decode[protocol.Input](f)
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
return
|
||||
}
|
||||
gotInput <- input
|
||||
_ = daemonT.Close()
|
||||
errCh <- nil
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
runCh := make(chan error, 1)
|
||||
go func() {
|
||||
runCh <- RunAttachedClient(ctx, ClientOptions{
|
||||
Transport: clientT,
|
||||
Stdin: inR,
|
||||
Stdout: out,
|
||||
Cols: 80,
|
||||
Rows: 24,
|
||||
})
|
||||
}()
|
||||
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) && !bytes.Contains(out.Bytes(), []byte("READY")) {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
if !bytes.Contains(out.Bytes(), []byte("READY")) {
|
||||
t.Fatalf("snapshot was not rendered before input; output=%q", out.String())
|
||||
}
|
||||
if _, err := inW.Write([]byte("echo hi\r")); err != nil {
|
||||
t.Fatalf("write stdin: %v", err)
|
||||
}
|
||||
select {
|
||||
case input := <-gotInput:
|
||||
if input.PaneID != "p1" || string(input.Bytes) != "echo hi\r" {
|
||||
t.Fatalf("input = %#v", input)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatalf("client did not forward input")
|
||||
}
|
||||
cancel()
|
||||
_ = inW.Close()
|
||||
select {
|
||||
case err := <-runCh:
|
||||
if err != nil {
|
||||
t.Fatalf("client run: %v", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatalf("client did not stop")
|
||||
}
|
||||
if err := <-errCh; err != nil && err != protocol.ErrTransportClosed {
|
||||
t.Fatalf("daemon side: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type lockedBuffer struct {
|
||||
mu sync.Mutex
|
||||
b bytes.Buffer
|
||||
}
|
||||
|
||||
func (b *lockedBuffer) Write(p []byte) (int, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return b.b.Write(p)
|
||||
}
|
||||
|
||||
func (b *lockedBuffer) Bytes() []byte {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return append([]byte(nil), b.b.Bytes()...)
|
||||
}
|
||||
|
||||
func (b *lockedBuffer) String() string {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return b.b.String()
|
||||
}
|
||||
|
||||
func ioPipe(t *testing.T) (*io.PipeReader, *io.PipeWriter) {
|
||||
t.Helper()
|
||||
r, w := io.Pipe()
|
||||
return r, w
|
||||
}
|
||||
|
||||
func sendTestFrame[T any](t *testing.T, tr protocol.Transport, typ protocol.FrameType, payload T) {
|
||||
t.Helper()
|
||||
f, err := protocol.NewFrame(typ, payload)
|
||||
if err != nil {
|
||||
t.Fatalf("frame %s: %v", typ, err)
|
||||
}
|
||||
if err := tr.Send(f); err != nil {
|
||||
t.Fatalf("send %s: %v", typ, err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustMarshalTest(t *testing.T, v any) []byte {
|
||||
t.Helper()
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
135
internal/app/client_subscriber.go
Normal file
135
internal/app/client_subscriber.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
|
||||
"github.com/hjbdev/patterm/internal/protocol"
|
||||
)
|
||||
|
||||
const defaultClientSubscriberQueue = 256
|
||||
|
||||
// clientSubscriber is the daemon-to-client event bridge. Unlike daemon-local
|
||||
// listeners such as timers, debug capture, and waiters, it never blocks the PTY
|
||||
// pump: PTY chunks are copied before enqueue, and overflow marks the pane as
|
||||
// needing a fresh snapshot.
|
||||
type clientSubscriber struct {
|
||||
projectKey string
|
||||
project *Project
|
||||
clientID string
|
||||
frames chan protocol.Frame
|
||||
|
||||
mu sync.Mutex
|
||||
snapshotRequired map[string]bool
|
||||
lifecycleDirty bool
|
||||
}
|
||||
|
||||
func newClientSubscriber(project *Project, clientID string, size int) *clientSubscriber {
|
||||
if size <= 0 {
|
||||
size = defaultClientSubscriberQueue
|
||||
}
|
||||
projectKey := ""
|
||||
if project != nil {
|
||||
projectKey = project.Key
|
||||
}
|
||||
return &clientSubscriber{
|
||||
projectKey: projectKey,
|
||||
project: project,
|
||||
clientID: clientID,
|
||||
frames: make(chan protocol.Frame, size),
|
||||
snapshotRequired: make(map[string]bool),
|
||||
lifecycleDirty: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *clientSubscriber) Recv() (protocol.Frame, bool) {
|
||||
f, ok := <-s.frames
|
||||
return f, ok
|
||||
}
|
||||
|
||||
func (s *clientSubscriber) SnapshotRequired(childID string) bool {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.snapshotRequired[childID]
|
||||
}
|
||||
|
||||
func (s *clientSubscriber) OnChildSpawned(c *Child) {
|
||||
s.sendLifecycle(protocol.LifecycleSpawned, c, "")
|
||||
}
|
||||
|
||||
func (s *clientSubscriber) OnChildExited(c *Child) {
|
||||
s.sendLifecycle(protocol.LifecycleExited, c, "")
|
||||
}
|
||||
|
||||
func (s *clientSubscriber) OnChildClosed(id string) {
|
||||
s.sendFrame(protocol.Frame{Type: protocol.FrameLifecycle, Payload: mustJSON(protocol.Lifecycle{
|
||||
Kind: protocol.LifecycleClosed,
|
||||
ProjectKey: s.projectKey,
|
||||
ChildID: id,
|
||||
})})
|
||||
}
|
||||
|
||||
func (s *clientSubscriber) OnChildStateChanged(id string, state IdleState) {
|
||||
s.sendFrame(protocol.Frame{Type: protocol.FrameLifecycle, Payload: mustJSON(protocol.Lifecycle{
|
||||
Kind: protocol.LifecycleStateChanged,
|
||||
ProjectKey: s.projectKey,
|
||||
ChildID: id,
|
||||
State: string(state),
|
||||
})})
|
||||
}
|
||||
|
||||
func (s *clientSubscriber) OnPTYOut(childID string, chunk []byte) {
|
||||
cp := append([]byte(nil), chunk...)
|
||||
var size protocol.Size
|
||||
var ownerID string
|
||||
if s.project != nil {
|
||||
size, ownerID, _ = s.project.PaneDisplay(childID)
|
||||
}
|
||||
f, err := protocol.NewFrame(protocol.FramePaneChunk, protocol.PaneChunk{PaneID: childID, Bytes: cp, Size: size, DisplayOwner: ownerID == "" || ownerID == s.clientID})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case s.frames <- f:
|
||||
default:
|
||||
s.mu.Lock()
|
||||
s.snapshotRequired[childID] = true
|
||||
s.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *clientSubscriber) sendLifecycle(kind protocol.LifecycleKind, c *Child, state string) {
|
||||
var child json.RawMessage
|
||||
if c != nil {
|
||||
child = mustJSON(serializeChildModel(c))
|
||||
}
|
||||
childID := ""
|
||||
if c != nil {
|
||||
childID = c.ID
|
||||
}
|
||||
s.sendFrame(protocol.Frame{Type: protocol.FrameLifecycle, Payload: mustJSON(protocol.Lifecycle{
|
||||
Kind: kind,
|
||||
ProjectKey: s.projectKey,
|
||||
ChildID: childID,
|
||||
Child: child,
|
||||
State: state,
|
||||
})})
|
||||
}
|
||||
|
||||
func (s *clientSubscriber) sendFrame(f protocol.Frame) {
|
||||
select {
|
||||
case s.frames <- f:
|
||||
default:
|
||||
s.mu.Lock()
|
||||
s.lifecycleDirty = true
|
||||
s.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func mustJSON(v any) json.RawMessage {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return b
|
||||
}
|
||||
32
internal/app/client_subscriber_test.go
Normal file
32
internal/app/client_subscriber_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hjbdev/patterm/internal/protocol"
|
||||
)
|
||||
|
||||
func TestClientSubscriberCopiesChunksAndMarksSnapshotOnOverflow(t *testing.T) {
|
||||
sub := newClientSubscriber(&Project{Key: "project"}, "client", 1)
|
||||
chunk := []byte("first")
|
||||
sub.OnPTYOut("p_123456", chunk)
|
||||
chunk[0] = 'X'
|
||||
|
||||
f, ok := sub.Recv()
|
||||
if !ok {
|
||||
t.Fatalf("Recv closed")
|
||||
}
|
||||
payload, err := protocol.Decode[protocol.PaneChunk](f)
|
||||
if err != nil {
|
||||
t.Fatalf("Decode: %v", err)
|
||||
}
|
||||
if string(payload.Bytes) != "first" {
|
||||
t.Fatalf("payload retained pump buffer: %q", string(payload.Bytes))
|
||||
}
|
||||
|
||||
sub.OnPTYOut("p_123456", []byte("queued"))
|
||||
sub.OnPTYOut("p_123456", []byte("dropped"))
|
||||
if !sub.SnapshotRequired("p_123456") {
|
||||
t.Fatalf("overflow did not mark pane snapshot required")
|
||||
}
|
||||
}
|
||||
40
internal/app/client_view.go
Normal file
40
internal/app/client_view.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package app
|
||||
|
||||
// ClientView is the per-client UI cursor over daemon-owned project/process
|
||||
// state. In loopback mode there is one view, owned by uiState; future network
|
||||
// clients will each get their own copy.
|
||||
type ClientView struct {
|
||||
ID string
|
||||
ProjectKey string
|
||||
ProjectName string
|
||||
FocusedID string
|
||||
FocusedPad string
|
||||
ActiveAgentID string
|
||||
PadOffset int
|
||||
PadOffsetName string
|
||||
Cols uint16
|
||||
Rows uint16
|
||||
}
|
||||
|
||||
func (v *ClientView) FocusChild(id string) {
|
||||
v.FocusedID = id
|
||||
v.FocusedPad = ""
|
||||
}
|
||||
|
||||
func (v *ClientView) FocusPad(name string) {
|
||||
v.FocusedID = ""
|
||||
v.FocusedPad = name
|
||||
if v.PadOffsetName != name {
|
||||
v.PadOffset = 0
|
||||
v.PadOffsetName = name
|
||||
}
|
||||
}
|
||||
|
||||
func (v *ClientView) ClearPadFocus() {
|
||||
v.FocusedPad = ""
|
||||
}
|
||||
|
||||
func (v *ClientView) Resize(cols, rows uint16) {
|
||||
v.Cols = cols
|
||||
v.Rows = rows
|
||||
}
|
||||
530
internal/app/daemon_core.go
Normal file
530
internal/app/daemon_core.go
Normal file
@@ -0,0 +1,530 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/hjbdev/patterm/internal/mcp"
|
||||
"github.com/hjbdev/patterm/internal/persist"
|
||||
"github.com/hjbdev/patterm/internal/preset"
|
||||
"github.com/hjbdev/patterm/internal/projectkey"
|
||||
"github.com/hjbdev/patterm/internal/protocol"
|
||||
"github.com/hjbdev/patterm/internal/scratchpad"
|
||||
"github.com/hjbdev/patterm/internal/trust"
|
||||
)
|
||||
|
||||
type Project struct {
|
||||
Key string
|
||||
Dir string
|
||||
Name string
|
||||
|
||||
Session *Session
|
||||
Pads *scratchpad.Store
|
||||
Trust *trust.Store
|
||||
Persist *persist.Store
|
||||
Launcher *Launcher
|
||||
Host *toolHost
|
||||
savedProcess []persist.Entry
|
||||
|
||||
displayMu sync.Mutex
|
||||
displayOwners map[string]paneDisplayOwner
|
||||
|
||||
lastActive time.Time
|
||||
}
|
||||
|
||||
type paneDisplayOwner struct {
|
||||
ClientID string
|
||||
Size protocol.Size
|
||||
}
|
||||
|
||||
type projectSummary struct {
|
||||
Key string
|
||||
Dir string
|
||||
Name string
|
||||
TabCount int
|
||||
IsCurrent bool
|
||||
}
|
||||
|
||||
// ProjectRegistry is the daemon-owned project map. Phase 1 still runs in one
|
||||
// local process, but every project already has isolated stores, session,
|
||||
// launcher, and tool host so future clients can attach to different projects.
|
||||
type ProjectRegistry struct {
|
||||
mu sync.Mutex
|
||||
projects map[string]*Project
|
||||
|
||||
defaultProjectKey string
|
||||
presets preset.Set
|
||||
settings settings
|
||||
mcpSrv *mcp.Server
|
||||
cols, rows uint16
|
||||
}
|
||||
|
||||
func newProjectRegistry(presets preset.Set, settings settings, mcpSrv *mcp.Server, cols, rows uint16) *ProjectRegistry {
|
||||
return &ProjectRegistry{
|
||||
projects: make(map[string]*Project),
|
||||
presets: presets,
|
||||
settings: settings,
|
||||
mcpSrv: mcpSrv,
|
||||
cols: cols,
|
||||
rows: rows,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) Open(ctx context.Context, dir string) (*Project, error) {
|
||||
key, err := projectkey.Key(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
abs, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
if p := r.projects[key]; p != nil {
|
||||
p.lastActive = time.Now()
|
||||
r.mu.Unlock()
|
||||
return p, nil
|
||||
}
|
||||
r.mu.Unlock()
|
||||
|
||||
pads, err := scratchpad.Open(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("app: scratchpad init: %w", err)
|
||||
}
|
||||
trustStore, err := trust.Open(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("app: trust init: %w", err)
|
||||
}
|
||||
persistStore, err := persist.Open(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("app: persist init: %w", err)
|
||||
}
|
||||
sess := NewSession(abs, key)
|
||||
savedProcesses := persistStore.List()
|
||||
for _, e := range savedProcesses {
|
||||
_ = persistStore.Remove(e.ID)
|
||||
}
|
||||
sess.SetPersistStore(persistStore)
|
||||
socket := ""
|
||||
if r.mcpSrv != nil {
|
||||
socket = r.mcpSrv.Socket()
|
||||
}
|
||||
launcher := NewLauncher(sess, socket, r.cols, r.rows)
|
||||
host := newToolHost(sess, pads, launcher, r.presets, trustStore, r.cols, r.rows)
|
||||
go sess.runClassifier(ctx)
|
||||
|
||||
p := &Project{
|
||||
Key: key,
|
||||
Dir: abs,
|
||||
Name: filepath.Base(abs),
|
||||
Session: sess,
|
||||
Pads: pads,
|
||||
Trust: trustStore,
|
||||
Persist: persistStore,
|
||||
Launcher: launcher,
|
||||
Host: host,
|
||||
savedProcess: savedProcesses,
|
||||
displayOwners: make(map[string]paneDisplayOwner),
|
||||
lastActive: time.Now(),
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
if existing := r.projects[key]; existing != nil {
|
||||
r.mu.Unlock()
|
||||
sess.Shutdown()
|
||||
return existing, nil
|
||||
}
|
||||
r.projects[key] = p
|
||||
if r.defaultProjectKey == "" {
|
||||
r.defaultProjectKey = key
|
||||
}
|
||||
r.mu.Unlock()
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) Project(key string) *Project {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.projects[key]
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) Count() int {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return len(r.projects)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) DefaultProject() *Project {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.projects[r.defaultProjectKey]
|
||||
}
|
||||
|
||||
func (p *Project) ClaimPaneDisplay(clientID, paneID string, size protocol.Size) (protocol.Size, bool) {
|
||||
if p == nil || paneID == "" {
|
||||
return size, true
|
||||
}
|
||||
if size.Cols == 0 || size.Rows == 0 {
|
||||
size = protocol.Size{Cols: 80, Rows: 24}
|
||||
}
|
||||
p.displayMu.Lock()
|
||||
if p.displayOwners == nil {
|
||||
p.displayOwners = make(map[string]paneDisplayOwner)
|
||||
}
|
||||
owner, ok := p.displayOwners[paneID]
|
||||
if !ok || owner.ClientID == "" || owner.ClientID == clientID {
|
||||
p.displayOwners[paneID] = paneDisplayOwner{ClientID: clientID, Size: size}
|
||||
p.displayMu.Unlock()
|
||||
p.Session.ResizeChild(paneID, size.Cols, size.Rows)
|
||||
return size, true
|
||||
}
|
||||
p.displayMu.Unlock()
|
||||
return owner.Size, false
|
||||
}
|
||||
|
||||
func (p *Project) ResizeClientDisplays(clientID string, size protocol.Size) {
|
||||
if p == nil || size.Cols == 0 || size.Rows == 0 {
|
||||
return
|
||||
}
|
||||
p.displayMu.Lock()
|
||||
var panes []string
|
||||
for paneID, owner := range p.displayOwners {
|
||||
if owner.ClientID != clientID {
|
||||
continue
|
||||
}
|
||||
owner.Size = size
|
||||
p.displayOwners[paneID] = owner
|
||||
panes = append(panes, paneID)
|
||||
}
|
||||
p.displayMu.Unlock()
|
||||
for _, paneID := range panes {
|
||||
p.Session.ResizeChild(paneID, size.Cols, size.Rows)
|
||||
}
|
||||
p.Launcher.SetSize(size.Cols, size.Rows)
|
||||
p.Host.SetSize(size.Cols, size.Rows)
|
||||
}
|
||||
|
||||
func (p *Project) ReleaseClientDisplays(clientID string) {
|
||||
if p == nil {
|
||||
return
|
||||
}
|
||||
p.displayMu.Lock()
|
||||
for paneID, owner := range p.displayOwners {
|
||||
if owner.ClientID == clientID {
|
||||
delete(p.displayOwners, paneID)
|
||||
}
|
||||
}
|
||||
p.displayMu.Unlock()
|
||||
}
|
||||
|
||||
func (p *Project) PaneDisplay(paneID string) (protocol.Size, string, bool) {
|
||||
if p == nil || paneID == "" {
|
||||
return protocol.Size{}, "", false
|
||||
}
|
||||
p.displayMu.Lock()
|
||||
defer p.displayMu.Unlock()
|
||||
owner, ok := p.displayOwners[paneID]
|
||||
return owner.Size, owner.ClientID, ok
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) Shutdown() {
|
||||
r.mu.Lock()
|
||||
projects := make([]*Project, 0, len(r.projects))
|
||||
for _, p := range r.projects {
|
||||
projects = append(projects, p)
|
||||
}
|
||||
r.mu.Unlock()
|
||||
for _, p := range projects {
|
||||
p.Session.Shutdown()
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) ResizeAll(cols, rows uint16) {
|
||||
r.mu.Lock()
|
||||
r.cols, r.rows = cols, rows
|
||||
projects := make([]*Project, 0, len(r.projects))
|
||||
for _, p := range r.projects {
|
||||
projects = append(projects, p)
|
||||
}
|
||||
r.mu.Unlock()
|
||||
for _, p := range projects {
|
||||
p.Session.ResizeAll(cols, rows)
|
||||
p.Launcher.SetSize(cols, rows)
|
||||
p.Host.SetSize(cols, rows)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) Summaries(currentKey string) []projectSummary {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
out := make([]projectSummary, 0, len(r.projects))
|
||||
for _, p := range r.projects {
|
||||
out = append(out, projectSummary{
|
||||
Key: p.Key,
|
||||
Dir: p.Dir,
|
||||
Name: p.Name,
|
||||
TabCount: len(runningTopLevels(p.Session.Children())),
|
||||
IsCurrent: p.Key == currentKey,
|
||||
})
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool {
|
||||
if out[i].IsCurrent != out[j].IsCurrent {
|
||||
return out[i].IsCurrent
|
||||
}
|
||||
return out[i].Name < out[j].Name
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) findProjectByChild(id string) (*Project, *Child) {
|
||||
if id == "" {
|
||||
return nil, nil
|
||||
}
|
||||
r.mu.Lock()
|
||||
projects := make([]*Project, 0, len(r.projects))
|
||||
for _, p := range r.projects {
|
||||
projects = append(projects, p)
|
||||
}
|
||||
r.mu.Unlock()
|
||||
for _, p := range projects {
|
||||
if c := p.Session.FindChild(id); c != nil {
|
||||
return p, c
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) projectForCaller(callerID string) *Project {
|
||||
if p, _ := r.findProjectByChild(callerID); p != nil {
|
||||
return p
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.projects[r.defaultProjectKey]
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) hostForCaller(callerID string) *toolHost {
|
||||
if p := r.projectForCaller(callerID); p != nil {
|
||||
return p.Host
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) hostForProcess(processID string) *toolHost {
|
||||
if p, _ := r.findProjectByChild(processID); p != nil {
|
||||
return p.Host
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) ResolveCallerIdentity(identity string) string {
|
||||
r.mu.Lock()
|
||||
projects := make([]*Project, 0, len(r.projects))
|
||||
for _, p := range r.projects {
|
||||
projects = append(projects, p)
|
||||
}
|
||||
r.mu.Unlock()
|
||||
for _, p := range projects {
|
||||
if c := p.Session.FindChildByIdentity(identity); c != nil {
|
||||
return c.ID
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) CallerRole(processID string) mcp.CallerRole {
|
||||
if h := r.hostForCaller(processID); h != nil {
|
||||
return h.CallerRole(processID)
|
||||
}
|
||||
return mcp.RoleOrchestrator
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) SpawnAgent(callerID string, args mcp.SpawnAgentArgs) (mcp.ProcessInfo, error) {
|
||||
return r.hostForCaller(callerID).SpawnAgent(callerID, args)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) SpawnProcess(callerID string, args mcp.SpawnProcessArgs) (mcp.ProcessInfo, error) {
|
||||
return r.hostForCaller(callerID).SpawnProcess(callerID, args)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) StartProcess(callerID, processID string) (mcp.ProcessInfo, error) {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.StartProcess(callerID, processID)
|
||||
}
|
||||
return mcp.ProcessInfo{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) RestartProcess(callerID, processID string, sig syscall.Signal) (mcp.ProcessInfo, error) {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.RestartProcess(callerID, processID, sig)
|
||||
}
|
||||
return mcp.ProcessInfo{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) StopProcess(callerID, processID string, sig syscall.Signal) (mcp.ProcessInfo, error) {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.StopProcess(callerID, processID, sig)
|
||||
}
|
||||
return mcp.ProcessInfo{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) CloseProcess(callerID, processID string) error {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.CloseProcess(callerID, processID)
|
||||
}
|
||||
return mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) RenameProcess(callerID, processID, name string) error {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.RenameProcess(callerID, processID, name)
|
||||
}
|
||||
return mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) SelectProcess(callerID, processID string) error {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.SelectProcess(callerID, processID)
|
||||
}
|
||||
return mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) ListProcesses(callerID, kindFilter string) []mcp.ProcessInfo {
|
||||
if h := r.hostForCaller(callerID); h != nil {
|
||||
return h.ListProcesses(callerID, kindFilter)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) GetProcessStatus(callerID, processID string) (mcp.ProcessStatus, error) {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.GetProcessStatus(callerID, processID)
|
||||
}
|
||||
return mcp.ProcessStatus{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) GetProjectStatus(callerID string) (mcp.ProjectStatus, error) {
|
||||
return r.hostForCaller(callerID).GetProjectStatus(callerID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) GetProcessOutput(callerID, processID, mode string, sinceOffset int64) (mcp.ProcessOutput, error) {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.GetProcessOutput(callerID, processID, mode, sinceOffset)
|
||||
}
|
||||
return mcp.ProcessOutput{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) GetProcessRawOutput(callerID, processID string, sinceOffset int64) (mcp.RawOutput, error) {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.GetProcessRawOutput(callerID, processID, sinceOffset)
|
||||
}
|
||||
return mcp.RawOutput{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) SearchOutput(callerID, processID, pattern, kind string, limit int) (mcp.SearchResult, error) {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.SearchOutput(callerID, processID, pattern, kind, limit)
|
||||
}
|
||||
return mcp.SearchResult{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) WaitForPattern(callerID, processID, pattern string, timeoutSeconds float64, scope string) (bool, string, error) {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.WaitForPattern(callerID, processID, pattern, timeoutSeconds, scope)
|
||||
}
|
||||
return false, "", mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) GetProcessPorts(callerID, processID string) ([]mcp.PortSighting, error) {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.GetProcessPorts(callerID, processID)
|
||||
}
|
||||
return nil, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) SendInput(callerID string, args mcp.SendInputArgs) (mcp.SendInputResult, error) {
|
||||
if h := r.hostForProcess(args.ProcessID); h != nil {
|
||||
return h.SendInput(callerID, args)
|
||||
}
|
||||
return mcp.SendInputResult{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", args.ProcessID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) SendMessage(callerID, targetID, message string) error {
|
||||
if h := r.hostForProcess(targetID); h != nil {
|
||||
return h.SendMessage(callerID, targetID, message)
|
||||
}
|
||||
return mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", targetID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) RequestHumanAttention(callerID, processID, reason string) error {
|
||||
if h := r.hostForProcess(processID); h != nil {
|
||||
return h.RequestHumanAttention(callerID, processID, reason)
|
||||
}
|
||||
return mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) TimerWait(callerID string, seconds float64, label string) (string, error) {
|
||||
return r.hostForCaller(callerID).TimerWait(callerID, seconds, label)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) TimerSet(callerID string, args mcp.TimerSetArgs) (mcp.TimerHandle, error) {
|
||||
return r.hostForCaller(callerID).TimerSet(callerID, args)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) TimerFireWhenIdleAny(callerID string, args mcp.TimerFireWhenIdleArgs) (mcp.TimerFireWhenIdleResponse, error) {
|
||||
return r.hostForCaller(callerID).TimerFireWhenIdleAny(callerID, args)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) TimerFireWhenIdleAll(callerID string, args mcp.TimerFireWhenIdleArgs) (mcp.TimerFireWhenIdleResponse, error) {
|
||||
return r.hostForCaller(callerID).TimerFireWhenIdleAll(callerID, args)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) TimerCancel(callerID, id string) error {
|
||||
return r.hostForCaller(callerID).TimerCancel(callerID, id)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) TimerPause(callerID, id string) error {
|
||||
return r.hostForCaller(callerID).TimerPause(callerID, id)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) TimerResume(callerID, id string) error {
|
||||
return r.hostForCaller(callerID).TimerResume(callerID, id)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) TimerList(callerID string) ([]mcp.TimerInfo, error) {
|
||||
return r.hostForCaller(callerID).TimerList(callerID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) ScratchpadList(callerID string) ([]scratchpad.Entry, error) {
|
||||
return r.hostForCaller(callerID).ScratchpadList(callerID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) ScratchpadRead(callerID, name string) (string, string, error) {
|
||||
return r.hostForCaller(callerID).ScratchpadRead(callerID, name)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) ScratchpadWrite(callerID, name, content, expectedRevision string) (string, error) {
|
||||
return r.hostForCaller(callerID).ScratchpadWrite(callerID, name, content, expectedRevision)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) ScratchpadAppend(callerID, name, content string) error {
|
||||
return r.hostForCaller(callerID).ScratchpadAppend(callerID, name, content)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) ScratchpadDelete(callerID, name string) error {
|
||||
return r.hostForCaller(callerID).ScratchpadDelete(callerID, name)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) WhoAmI(callerID string) mcp.WhoAmI {
|
||||
return r.hostForCaller(callerID).WhoAmI(callerID)
|
||||
}
|
||||
|
||||
func (r *ProjectRegistry) Help(callerID, topic string) mcp.HelpResponse {
|
||||
return r.hostForCaller(callerID).Help(callerID, topic)
|
||||
}
|
||||
481
internal/app/daemon_net.go
Normal file
481
internal/app/daemon_net.go
Normal file
@@ -0,0 +1,481 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/hjbdev/patterm/internal/mcp"
|
||||
"github.com/hjbdev/patterm/internal/preset"
|
||||
"github.com/hjbdev/patterm/internal/protocol"
|
||||
)
|
||||
|
||||
type DaemonOptions struct {
|
||||
ProjectDir string
|
||||
SocketPath string
|
||||
PidPath string
|
||||
ListenAddr string
|
||||
Token string
|
||||
TokenOut io.Writer
|
||||
ListenReady chan string
|
||||
Cols uint16
|
||||
Rows uint16
|
||||
}
|
||||
|
||||
type DaemonStatus struct {
|
||||
PID int
|
||||
Socket string
|
||||
Projects []protocol.Project
|
||||
}
|
||||
|
||||
func RuntimeDaemonPaths() (socketPath, pidPath string, err error) {
|
||||
base := os.Getenv("XDG_RUNTIME_DIR")
|
||||
if base == "" {
|
||||
base = os.TempDir()
|
||||
}
|
||||
dir := filepath.Join(base, "patterm")
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return filepath.Join(dir, "daemon.sock"), filepath.Join(dir, "daemon.pid"), nil
|
||||
}
|
||||
|
||||
func RunDaemon(ctx context.Context, opts DaemonOptions) error {
|
||||
if opts.ProjectDir == "" {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.ProjectDir = cwd
|
||||
}
|
||||
if opts.SocketPath == "" || opts.PidPath == "" {
|
||||
socket, pid, err := RuntimeDaemonPaths()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if opts.SocketPath == "" {
|
||||
opts.SocketPath = socket
|
||||
}
|
||||
if opts.PidPath == "" {
|
||||
opts.PidPath = pid
|
||||
}
|
||||
}
|
||||
if opts.Cols == 0 {
|
||||
opts.Cols = 80
|
||||
}
|
||||
if opts.Rows == 0 {
|
||||
opts.Rows = 24
|
||||
}
|
||||
lockPath, err := prepareDaemonSocket(opts.SocketPath, opts.PidPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(lockPath)
|
||||
ln, err := net.Listen("unix", opts.SocketPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("daemon: listen %s: %w", opts.SocketPath, err)
|
||||
}
|
||||
defer ln.Close()
|
||||
defer os.Remove(opts.SocketPath)
|
||||
if err := os.Chmod(opts.SocketPath, 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(opts.PidPath, []byte(strconv.Itoa(os.Getpid())+"\n"), 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(opts.PidPath)
|
||||
|
||||
presets, err := preset.Load()
|
||||
if err != nil {
|
||||
return fmt.Errorf("daemon: load presets: %w", err)
|
||||
}
|
||||
appSettings, _, err := loadSettings()
|
||||
if err != nil {
|
||||
logf("daemon settings load: %v", err)
|
||||
}
|
||||
mcpSrv, err := mcp.Start()
|
||||
if err != nil {
|
||||
return fmt.Errorf("daemon: mcp start: %w", err)
|
||||
}
|
||||
defer mcpSrv.Close()
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
registry := newProjectRegistry(presets, appSettings, mcpSrv, opts.Cols, opts.Rows)
|
||||
defer registry.Shutdown()
|
||||
mcpSrv.SetHost(registry)
|
||||
if _, err := registry.Open(ctx, opts.ProjectDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var tcpLn net.Listener
|
||||
tcpToken := opts.Token
|
||||
if opts.ListenAddr != "" {
|
||||
addr := normalizeListenAddr(opts.ListenAddr)
|
||||
tcpToken, err = ensureDaemonToken(tcpToken)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tcpLn, err = net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("daemon: listen tcp %s: %w", addr, err)
|
||||
}
|
||||
defer tcpLn.Close()
|
||||
if opts.ListenReady != nil {
|
||||
select {
|
||||
case opts.ListenReady <- tcpLn.Addr().String():
|
||||
default:
|
||||
}
|
||||
}
|
||||
out := opts.TokenOut
|
||||
if out == nil {
|
||||
out = os.Stderr
|
||||
}
|
||||
fmt.Fprintf(out, "patterm daemon listening on %s\npatterm token: %s\n", tcpLn.Addr().String(), tcpToken)
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
_ = ln.Close()
|
||||
if tcpLn != nil {
|
||||
_ = tcpLn.Close()
|
||||
}
|
||||
}()
|
||||
errCh := make(chan error, 2)
|
||||
go acceptDaemonLoop(ctx, &wg, ln, "", cancel, registry, errCh)
|
||||
if tcpLn != nil {
|
||||
go acceptDaemonLoop(ctx, &wg, tcpLn, tcpToken, cancel, registry, errCh)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case err := <-errCh:
|
||||
cancel()
|
||||
wg.Wait()
|
||||
return err
|
||||
}
|
||||
wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
func acceptDaemonLoop(ctx context.Context, wg *sync.WaitGroup, ln net.Listener, authToken string, stop func(), registry *ProjectRegistry, errCh chan<- error) {
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
if errors.Is(err, net.ErrClosed) || ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case errCh <- err:
|
||||
default:
|
||||
}
|
||||
return
|
||||
}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
handleDaemonConn(ctx, stop, registry, protocol.NewConnTransport(conn), authToken)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeListenAddr(addr string) string {
|
||||
addr = strings.TrimSpace(addr)
|
||||
if addr == "" {
|
||||
return ""
|
||||
}
|
||||
if _, _, err := net.SplitHostPort(addr); err == nil {
|
||||
return addr
|
||||
}
|
||||
if strings.HasPrefix(addr, ":") {
|
||||
return addr
|
||||
}
|
||||
if _, err := strconv.Atoi(addr); err == nil {
|
||||
return ":" + addr
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
func ensureDaemonToken(token string) (string, error) {
|
||||
if strings.TrimSpace(token) != "" {
|
||||
return strings.TrimSpace(token), nil
|
||||
}
|
||||
return LoadOrCreateClientToken()
|
||||
}
|
||||
|
||||
func prepareDaemonSocket(socketPath, pidPath string) (string, error) {
|
||||
if err := os.MkdirAll(filepath.Dir(socketPath), 0o700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
lockPath := pidPath + ".lock"
|
||||
if data, err := os.ReadFile(pidPath); err == nil {
|
||||
if pid, perr := strconv.Atoi(strings.TrimSpace(string(data))); perr == nil && pid > 0 {
|
||||
if sigErr := syscallSignal0(pid); sigErr == nil {
|
||||
return "", fmt.Errorf("daemon already running with pid %d", pid)
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = os.Remove(socketPath)
|
||||
_ = os.Remove(pidPath)
|
||||
_ = os.Remove(lockPath)
|
||||
f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("daemon: lock %s: %w", lockPath, err)
|
||||
}
|
||||
_, _ = f.WriteString(strconv.Itoa(os.Getpid()) + "\n")
|
||||
_ = f.Close()
|
||||
return lockPath, nil
|
||||
}
|
||||
|
||||
func syscallSignal0(pid int) error {
|
||||
return syscall.Kill(pid, 0)
|
||||
}
|
||||
|
||||
func handleDaemonConn(ctx context.Context, stop func(), registry *ProjectRegistry, t protocol.Transport, authToken string) {
|
||||
defer t.Close()
|
||||
f, err := t.Recv()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
switch f.Type {
|
||||
case protocol.FrameList:
|
||||
_ = sendProjectList(t, registry, "")
|
||||
return
|
||||
case protocol.FrameStop:
|
||||
_ = sendProjectList(t, registry, "")
|
||||
stop()
|
||||
return
|
||||
case protocol.FrameAttach:
|
||||
if authToken != "" {
|
||||
attach, err := protocol.Decode[protocol.Attach](f)
|
||||
if err != nil {
|
||||
_ = sendProtocolError(t, err.Error())
|
||||
return
|
||||
}
|
||||
if attach.Token != authToken {
|
||||
_ = sendProtocolError(t, "auth denied")
|
||||
return
|
||||
}
|
||||
}
|
||||
handleDaemonAttach(ctx, registry, t, f)
|
||||
default:
|
||||
_ = sendProtocolError(t, fmt.Sprintf("first frame must be attach, list, or stop; got %q", f.Type))
|
||||
}
|
||||
}
|
||||
|
||||
func handleDaemonAttach(ctx context.Context, registry *ProjectRegistry, t protocol.Transport, first protocol.Frame) {
|
||||
attach, err := protocol.Decode[protocol.Attach](first)
|
||||
if err != nil {
|
||||
_ = sendProtocolError(t, err.Error())
|
||||
return
|
||||
}
|
||||
project := registry.Project(attach.ProjectKey)
|
||||
if project == nil && attach.ProjectPath != "" {
|
||||
project, err = registry.Open(ctx, attach.ProjectPath)
|
||||
if err != nil {
|
||||
_ = sendProtocolError(t, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
if project == nil {
|
||||
project = registry.DefaultProject()
|
||||
}
|
||||
if project == nil {
|
||||
_ = sendProtocolError(t, "no project open")
|
||||
return
|
||||
}
|
||||
clientID := fmt.Sprintf("c-%d", time.Now().UnixNano())
|
||||
view := ClientView{
|
||||
ID: clientID,
|
||||
ProjectKey: project.Key,
|
||||
ProjectName: project.Name,
|
||||
Cols: attach.TermSize.Cols,
|
||||
Rows: attach.TermSize.Rows,
|
||||
}
|
||||
if child := firstRunningTopLevel(project.Session.Children()); child != nil {
|
||||
view.FocusChild(child.ID)
|
||||
project.ClaimPaneDisplay(clientID, child.ID, attach.TermSize)
|
||||
}
|
||||
sub := newClientSubscriber(project, clientID, defaultClientSubscriberQueue)
|
||||
project.Session.SubscribeClient(sub)
|
||||
defer project.Session.UnsubscribeClient(sub)
|
||||
defer project.ReleaseClientDisplays(clientID)
|
||||
|
||||
_ = sendHello(t, project, view.ID)
|
||||
_ = sendProjectList(t, registry, project.Key)
|
||||
_ = sendChrome(t, project, view)
|
||||
if view.FocusedID != "" {
|
||||
_ = sendSnapshot(t, project, clientID, view.FocusedID)
|
||||
}
|
||||
|
||||
// Close the transport when the daemon context is cancelled (shutdown or
|
||||
// `daemon stop`). Without this the t.Recv() loop below blocks forever on a
|
||||
// still-connected client and the accept loop's wg.Wait() never returns.
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
_ = t.Close()
|
||||
}()
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
for {
|
||||
f, ok := sub.Recv()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := t.Send(f); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
f, err := t.Recv()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
switch f.Type {
|
||||
case protocol.FrameDetach:
|
||||
return
|
||||
case protocol.FrameInput:
|
||||
msg, err := protocol.Decode[protocol.Input](f)
|
||||
if err == nil {
|
||||
if c := project.Session.FindChild(msg.PaneID); c != nil {
|
||||
_ = c.InjectAsUser(msg.Bytes)
|
||||
}
|
||||
}
|
||||
case protocol.FrameResize:
|
||||
msg, err := protocol.Decode[protocol.Resize](f)
|
||||
if err == nil {
|
||||
view.Resize(msg.Size.Cols, msg.Size.Rows)
|
||||
if view.FocusedID != "" {
|
||||
if _, _, ok := project.PaneDisplay(view.FocusedID); !ok {
|
||||
project.ClaimPaneDisplay(clientID, view.FocusedID, msg.Size)
|
||||
}
|
||||
}
|
||||
project.ResizeClientDisplays(clientID, msg.Size)
|
||||
}
|
||||
case protocol.FrameFocus:
|
||||
msg, err := protocol.Decode[protocol.Focus](f)
|
||||
if err == nil && msg.PaneID != "" {
|
||||
view.FocusChild(msg.PaneID)
|
||||
project.ClaimPaneDisplay(clientID, msg.PaneID, protocol.Size{Cols: view.Cols, Rows: view.Rows})
|
||||
_ = sendChrome(t, project, view)
|
||||
_ = sendSnapshot(t, project, clientID, msg.PaneID)
|
||||
}
|
||||
case protocol.FramePaletteCommand:
|
||||
if child := handleDaemonPaletteCommand(project, f); child != nil {
|
||||
view.FocusChild(child.ID)
|
||||
project.ClaimPaneDisplay(clientID, child.ID, protocol.Size{Cols: view.Cols, Rows: view.Rows})
|
||||
_ = sendChrome(t, project, view)
|
||||
_ = sendSnapshot(t, project, clientID, child.ID)
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleDaemonPaletteCommand(project *Project, f protocol.Frame) *Child {
|
||||
msg, err := protocol.Decode[protocol.PaletteCommand](f)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
switch msg.Kind {
|
||||
case "spawn_command":
|
||||
var p struct {
|
||||
Argv []string `json:"argv"`
|
||||
Name string `json:"name"`
|
||||
WorkDir string `json:"working_dir"`
|
||||
Shell bool `json:"shell"`
|
||||
}
|
||||
if err := json.Unmarshal(msg.Data, &p); err != nil || len(p.Argv) == 0 {
|
||||
return nil
|
||||
}
|
||||
name := p.Name
|
||||
if name == "" {
|
||||
name = strings.Join(p.Argv, " ")
|
||||
}
|
||||
c, err := project.Launcher.LaunchCommandArgv(p.Argv, name, "", p.WorkDir, nil, p.Shell)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return c
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendHello(t protocol.Transport, p *Project, clientID string) error {
|
||||
f, err := protocol.NewFrame(protocol.FrameHello, protocol.Hello{Version: 1, DaemonID: strconv.Itoa(os.Getpid()), ClientID: clientID, ProjectKey: p.Key})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return t.Send(f)
|
||||
}
|
||||
|
||||
func sendProjectList(t protocol.Transport, registry *ProjectRegistry, current string) error {
|
||||
summaries := registry.Summaries(current)
|
||||
projects := make([]protocol.Project, 0, len(summaries))
|
||||
for _, p := range summaries {
|
||||
projects = append(projects, protocol.Project{Key: p.Key, Path: p.Dir, Name: p.Name, TabCount: p.TabCount})
|
||||
}
|
||||
f, err := protocol.NewFrame(protocol.FrameProjectList, protocol.ProjectList{Projects: projects})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return t.Send(f)
|
||||
}
|
||||
|
||||
func sendChrome(t protocol.Transport, p *Project, view ClientView) error {
|
||||
pads, _ := p.Pads.List()
|
||||
model := buildChromeModel(p.Key, view, p.Session.Children(), pads)
|
||||
b, err := json.Marshal(model)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := protocol.NewFrame(protocol.FrameChrome, protocol.Chrome{ProjectKey: p.Key, Model: b})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return t.Send(f)
|
||||
}
|
||||
|
||||
func sendSnapshot(t protocol.Transport, p *Project, clientID, paneID string) error {
|
||||
b, err := p.Session.SerializeChild(paneID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
size, ownerID, _ := p.PaneDisplay(paneID)
|
||||
f, err := protocol.NewFrame(protocol.FramePaneSnapshot, protocol.PaneSnapshot{
|
||||
PaneID: paneID,
|
||||
Bytes: b,
|
||||
Size: size,
|
||||
DisplayOwner: ownerID == "" || ownerID == clientID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return t.Send(f)
|
||||
}
|
||||
|
||||
func sendProtocolError(t protocol.Transport, msg string) error {
|
||||
f, err := protocol.NewFrame(protocol.FrameError, protocol.Error{Message: msg})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return t.Send(f)
|
||||
}
|
||||
477
internal/app/daemon_net_test.go
Normal file
477
internal/app/daemon_net_test.go
Normal file
@@ -0,0 +1,477 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hjbdev/patterm/internal/preset"
|
||||
"github.com/hjbdev/patterm/internal/protocol"
|
||||
)
|
||||
|
||||
func TestDaemonDetachReattachPreservesProcess(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(root, "config"))
|
||||
t.Setenv("XDG_DATA_HOME", filepath.Join(root, "data"))
|
||||
t.Setenv("XDG_RUNTIME_DIR", filepath.Join(root, "runtime"))
|
||||
projectDir := filepath.Join(root, "project")
|
||||
if err := os.MkdirAll(projectDir, 0o700); err != nil {
|
||||
t.Fatalf("mkdir project: %v", err)
|
||||
}
|
||||
socket := filepath.Join(root, "runtime", "patterm", "daemon.sock")
|
||||
pid := filepath.Join(root, "runtime", "patterm", "daemon.pid")
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
errCh <- RunDaemon(ctx, DaemonOptions{
|
||||
ProjectDir: projectDir,
|
||||
SocketPath: socket,
|
||||
PidPath: pid,
|
||||
Cols: 80,
|
||||
Rows: 24,
|
||||
})
|
||||
}()
|
||||
waitForSocket(t, socket, errCh)
|
||||
|
||||
client1 := dialDaemon(t, socket)
|
||||
sendFrame(t, client1, protocol.FrameAttach, protocol.Attach{
|
||||
ProjectPath: projectDir,
|
||||
TermSize: protocol.Size{Cols: 80, Rows: 24},
|
||||
})
|
||||
expectFrame(t, client1, protocol.FrameHello)
|
||||
expectFrame(t, client1, protocol.FrameProjectList)
|
||||
expectFrame(t, client1, protocol.FrameChrome)
|
||||
|
||||
data, _ := json.Marshal(map[string]any{
|
||||
"argv": []string{"sh", "-c", "trap 'exit 0' TERM; while :; do echo STILL-HERE; sleep 1; done"},
|
||||
"name": "survivor",
|
||||
})
|
||||
sendFrame(t, client1, protocol.FramePaletteCommand, protocol.PaletteCommand{
|
||||
Kind: "spawn_command",
|
||||
Data: data,
|
||||
})
|
||||
waitForLifecycle(t, client1, protocol.LifecycleSpawned, 3*time.Second)
|
||||
sendFrame(t, client1, protocol.FrameDetach, protocol.Detach{})
|
||||
_ = client1.Close()
|
||||
|
||||
client2 := dialDaemon(t, socket)
|
||||
defer client2.Close()
|
||||
sendFrame(t, client2, protocol.FrameAttach, protocol.Attach{
|
||||
ProjectPath: projectDir,
|
||||
TermSize: protocol.Size{Cols: 80, Rows: 24},
|
||||
})
|
||||
expectFrame(t, client2, protocol.FrameHello)
|
||||
expectFrame(t, client2, protocol.FrameProjectList)
|
||||
chrome := expectChrome(t, client2)
|
||||
if !chromeHasProcess(chrome, "survivor") {
|
||||
t.Fatalf("reattached chrome did not include surviving process: %s", string(chrome.Model))
|
||||
}
|
||||
expectFrame(t, client2, protocol.FramePaneSnapshot)
|
||||
|
||||
cancel()
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
t.Fatalf("daemon returned error: %v", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatalf("daemon did not stop")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonTCPTokenAuthAndUnixExemption(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(root, "config"))
|
||||
t.Setenv("XDG_DATA_HOME", filepath.Join(root, "data"))
|
||||
t.Setenv("XDG_RUNTIME_DIR", filepath.Join(root, "runtime"))
|
||||
projectDir := filepath.Join(root, "project")
|
||||
if err := os.MkdirAll(projectDir, 0o700); err != nil {
|
||||
t.Fatalf("mkdir project: %v", err)
|
||||
}
|
||||
socket := filepath.Join(root, "runtime", "patterm", "daemon.sock")
|
||||
pid := filepath.Join(root, "runtime", "patterm", "daemon.pid")
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
errCh := make(chan error, 1)
|
||||
ready := make(chan string, 1)
|
||||
go func() {
|
||||
errCh <- RunDaemon(ctx, DaemonOptions{
|
||||
ProjectDir: projectDir,
|
||||
SocketPath: socket,
|
||||
PidPath: pid,
|
||||
ListenAddr: "127.0.0.1:0",
|
||||
Token: "secret-token",
|
||||
TokenOut: io.Discard,
|
||||
ListenReady: ready,
|
||||
Cols: 80,
|
||||
Rows: 24,
|
||||
})
|
||||
}()
|
||||
waitForSocket(t, socket, errCh)
|
||||
tcpAddr := waitForTCPAddr(t, ready, errCh)
|
||||
|
||||
assertTCPAttachDenied(t, tcpAddr, "")
|
||||
assertTCPAttachDenied(t, tcpAddr, "wrong-token")
|
||||
|
||||
tcpClient := dialTCPDaemon(t, tcpAddr)
|
||||
defer tcpClient.Close()
|
||||
sendFrame(t, tcpClient, protocol.FrameAttach, protocol.Attach{
|
||||
Token: "secret-token",
|
||||
ProjectPath: projectDir,
|
||||
TermSize: protocol.Size{Cols: 80, Rows: 24},
|
||||
})
|
||||
expectFrame(t, tcpClient, protocol.FrameHello)
|
||||
expectFrame(t, tcpClient, protocol.FrameProjectList)
|
||||
expectFrame(t, tcpClient, protocol.FrameChrome)
|
||||
data, _ := json.Marshal(map[string]any{
|
||||
"argv": []string{"sh", "-c", "trap 'exit 0' TERM; echo TCP-SNAPSHOT; sleep 30"},
|
||||
"name": "tcp-survivor",
|
||||
})
|
||||
sendFrame(t, tcpClient, protocol.FramePaletteCommand, protocol.PaletteCommand{
|
||||
Kind: "spawn_command",
|
||||
Data: data,
|
||||
})
|
||||
expectFrame(t, tcpClient, protocol.FramePaneSnapshot)
|
||||
|
||||
unixClient := dialDaemon(t, socket)
|
||||
defer unixClient.Close()
|
||||
sendFrame(t, unixClient, protocol.FrameAttach, protocol.Attach{
|
||||
ProjectPath: projectDir,
|
||||
TermSize: protocol.Size{Cols: 80, Rows: 24},
|
||||
})
|
||||
expectFrame(t, unixClient, protocol.FrameHello)
|
||||
|
||||
cancel()
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
t.Fatalf("daemon returned error: %v", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatalf("daemon did not stop")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonPaneDisplayOwnerSizing(t *testing.T) {
|
||||
t.Setenv("XDG_DATA_HOME", t.TempDir())
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
projectDir := t.TempDir()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
reg := newProjectRegistry(preset.Set{}, defaultSettings(), nil, 80, 24)
|
||||
defer reg.Shutdown()
|
||||
project, err := reg.Open(ctx, projectDir)
|
||||
if err != nil {
|
||||
t.Fatalf("open project: %v", err)
|
||||
}
|
||||
|
||||
client1, daemon1 := protocol.NewLoopbackPair()
|
||||
go handleDaemonConn(ctx, cancel, reg, daemon1, "")
|
||||
sendFrame(t, client1, protocol.FrameAttach, protocol.Attach{
|
||||
ProjectPath: projectDir,
|
||||
TermSize: protocol.Size{Cols: 80, Rows: 24},
|
||||
})
|
||||
expectFrame(t, client1, protocol.FrameHello)
|
||||
expectFrame(t, client1, protocol.FrameProjectList)
|
||||
expectFrame(t, client1, protocol.FrameChrome)
|
||||
|
||||
data, _ := json.Marshal(map[string]any{
|
||||
"argv": []string{"sh", "-c", "trap 'exit 0' TERM; while :; do sleep 1; done"},
|
||||
"name": "owner-pane",
|
||||
})
|
||||
sendFrame(t, client1, protocol.FramePaletteCommand, protocol.PaletteCommand{
|
||||
Kind: "spawn_command",
|
||||
Data: data,
|
||||
})
|
||||
paneID := waitForLifecycleID(t, client1, protocol.LifecycleSpawned, 3*time.Second)
|
||||
snap1 := waitForSnapshot(t, client1, paneID, 3*time.Second)
|
||||
if !snap1.DisplayOwner || snap1.Size != (protocol.Size{Cols: 80, Rows: 24}) {
|
||||
t.Fatalf("owner snapshot = owner:%v size:%+v, want owner true size 80x24", snap1.DisplayOwner, snap1.Size)
|
||||
}
|
||||
waitForEmulatorSize(t, project, paneID, 80, 24)
|
||||
|
||||
client2, daemon2 := protocol.NewLoopbackPair()
|
||||
go handleDaemonConn(ctx, cancel, reg, daemon2, "")
|
||||
sendFrame(t, client2, protocol.FrameAttach, protocol.Attach{
|
||||
ProjectPath: projectDir,
|
||||
TermSize: protocol.Size{Cols: 100, Rows: 30},
|
||||
})
|
||||
expectFrame(t, client2, protocol.FrameHello)
|
||||
expectFrame(t, client2, protocol.FrameProjectList)
|
||||
expectFrame(t, client2, protocol.FrameChrome)
|
||||
snap2 := waitForSnapshot(t, client2, paneID, 3*time.Second)
|
||||
if snap2.DisplayOwner || snap2.Size != (protocol.Size{Cols: 80, Rows: 24}) {
|
||||
t.Fatalf("viewer snapshot = owner:%v size:%+v, want owner false size 80x24", snap2.DisplayOwner, snap2.Size)
|
||||
}
|
||||
sendFrame(t, client2, protocol.FrameResize, protocol.Resize{Size: protocol.Size{Cols: 100, Rows: 30}})
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
waitForEmulatorSize(t, project, paneID, 80, 24)
|
||||
|
||||
sendFrame(t, client1, protocol.FrameDetach, protocol.Detach{})
|
||||
_ = client1.Close()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
sendFrame(t, client2, protocol.FrameFocus, protocol.Focus{PaneID: paneID})
|
||||
snap3 := waitForSnapshot(t, client2, paneID, 3*time.Second)
|
||||
if !snap3.DisplayOwner || snap3.Size != (protocol.Size{Cols: 100, Rows: 30}) {
|
||||
t.Fatalf("claimed snapshot = owner:%v size:%+v, want owner true size 100x30", snap3.DisplayOwner, snap3.Size)
|
||||
}
|
||||
waitForEmulatorSize(t, project, paneID, 100, 30)
|
||||
|
||||
sendFrame(t, client2, protocol.FrameDetach, protocol.Detach{})
|
||||
_ = client2.Close()
|
||||
}
|
||||
|
||||
func waitForSocket(t *testing.T, socket string, errCh <-chan error) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if _, err := os.Stat(socket); err == nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil && strings.Contains(err.Error(), "operation not permitted") {
|
||||
t.Skipf("unix sockets unavailable in this sandbox: %v", err)
|
||||
}
|
||||
t.Fatalf("daemon exited before creating socket: %v", err)
|
||||
default:
|
||||
}
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("socket %s was not created", socket)
|
||||
}
|
||||
|
||||
func dialDaemon(t *testing.T, socket string) protocol.Transport {
|
||||
t.Helper()
|
||||
conn, err := net.Dial("unix", socket)
|
||||
if err != nil {
|
||||
t.Fatalf("dial daemon: %v", err)
|
||||
}
|
||||
return protocol.NewConnTransport(conn)
|
||||
}
|
||||
|
||||
func dialTCPDaemon(t *testing.T, addr string) protocol.Transport {
|
||||
t.Helper()
|
||||
conn, err := net.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
t.Fatalf("dial tcp daemon: %v", err)
|
||||
}
|
||||
return protocol.NewConnTransport(conn)
|
||||
}
|
||||
|
||||
func waitForTCPAddr(t *testing.T, ready <-chan string, errCh <-chan error) string {
|
||||
t.Helper()
|
||||
select {
|
||||
case addr := <-ready:
|
||||
return addr
|
||||
case err := <-errCh:
|
||||
if err != nil && strings.Contains(err.Error(), "operation not permitted") {
|
||||
t.Skipf("tcp sockets unavailable in this sandbox: %v", err)
|
||||
}
|
||||
t.Fatalf("daemon exited before TCP listener was ready: %v", err)
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatalf("tcp listener was not ready")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func assertTCPAttachDenied(t *testing.T, addr, token string) {
|
||||
t.Helper()
|
||||
client := dialTCPDaemon(t, addr)
|
||||
defer client.Close()
|
||||
sendFrame(t, client, protocol.FrameAttach, protocol.Attach{
|
||||
Token: token,
|
||||
TermSize: protocol.Size{Cols: 80, Rows: 24},
|
||||
})
|
||||
f := expectFrame(t, client, protocol.FrameError)
|
||||
msg, err := protocol.Decode[protocol.Error](f)
|
||||
if err != nil {
|
||||
t.Fatalf("decode error frame: %v", err)
|
||||
}
|
||||
if !strings.Contains(msg.Message, "auth denied") {
|
||||
t.Fatalf("error message = %q, want auth denied", msg.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func sendFrame[T any](t *testing.T, tr protocol.Transport, typ protocol.FrameType, payload T) {
|
||||
t.Helper()
|
||||
f, err := protocol.NewFrame(typ, payload)
|
||||
if err != nil {
|
||||
t.Fatalf("frame %s: %v", typ, err)
|
||||
}
|
||||
if err := tr.Send(f); err != nil {
|
||||
t.Fatalf("send %s: %v", typ, err)
|
||||
}
|
||||
}
|
||||
|
||||
func expectFrame(t *testing.T, tr protocol.Transport, typ protocol.FrameType) protocol.Frame {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
f, err, ok := recvFrameWithin(tr, time.Until(deadline))
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("recv %s: %v", typ, err)
|
||||
}
|
||||
if f.Type == typ {
|
||||
return f
|
||||
}
|
||||
}
|
||||
t.Fatalf("frame %s not received", typ)
|
||||
return protocol.Frame{}
|
||||
}
|
||||
|
||||
func expectChrome(t *testing.T, tr protocol.Transport) protocol.Chrome {
|
||||
t.Helper()
|
||||
f := expectFrame(t, tr, protocol.FrameChrome)
|
||||
chrome, err := protocol.Decode[protocol.Chrome](f)
|
||||
if err != nil {
|
||||
t.Fatalf("decode chrome: %v", err)
|
||||
}
|
||||
return chrome
|
||||
}
|
||||
|
||||
func waitForLifecycle(t *testing.T, tr protocol.Transport, kind protocol.LifecycleKind, timeout time.Duration) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
f, err, ok := recvFrameWithin(tr, time.Until(deadline))
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("recv lifecycle: %v", err)
|
||||
}
|
||||
if f.Type != protocol.FrameLifecycle {
|
||||
continue
|
||||
}
|
||||
msg, err := protocol.Decode[protocol.Lifecycle](f)
|
||||
if err != nil {
|
||||
t.Fatalf("decode lifecycle: %v", err)
|
||||
}
|
||||
if msg.Kind == kind {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("lifecycle %s not received", kind)
|
||||
}
|
||||
|
||||
func waitForLifecycleID(t *testing.T, tr protocol.Transport, kind protocol.LifecycleKind, timeout time.Duration) string {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
f, err, ok := recvFrameWithin(tr, time.Until(deadline))
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("recv lifecycle: %v", err)
|
||||
}
|
||||
if f.Type != protocol.FrameLifecycle {
|
||||
continue
|
||||
}
|
||||
msg, err := protocol.Decode[protocol.Lifecycle](f)
|
||||
if err != nil {
|
||||
t.Fatalf("decode lifecycle: %v", err)
|
||||
}
|
||||
if msg.Kind == kind {
|
||||
return msg.ChildID
|
||||
}
|
||||
}
|
||||
t.Fatalf("lifecycle %s not received", kind)
|
||||
return ""
|
||||
}
|
||||
|
||||
func waitForSnapshot(t *testing.T, tr protocol.Transport, paneID string, timeout time.Duration) protocol.PaneSnapshot {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
f, err, ok := recvFrameWithin(tr, time.Until(deadline))
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("recv snapshot: %v", err)
|
||||
}
|
||||
if f.Type != protocol.FramePaneSnapshot {
|
||||
continue
|
||||
}
|
||||
msg, err := protocol.Decode[protocol.PaneSnapshot](f)
|
||||
if err != nil {
|
||||
t.Fatalf("decode snapshot: %v", err)
|
||||
}
|
||||
if msg.PaneID == paneID {
|
||||
return msg
|
||||
}
|
||||
}
|
||||
t.Fatalf("snapshot for %s not received", paneID)
|
||||
return protocol.PaneSnapshot{}
|
||||
}
|
||||
|
||||
func waitForEmulatorSize(t *testing.T, project *Project, paneID string, cols, rows uint16) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if c := project.Session.FindChild(paneID); c != nil {
|
||||
if em := c.Emulator(); em != nil {
|
||||
gotCols, gotRows := em.Size()
|
||||
if gotCols == cols && gotRows == rows {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
}
|
||||
if c := project.Session.FindChild(paneID); c != nil {
|
||||
if em := c.Emulator(); em != nil {
|
||||
gotCols, gotRows := em.Size()
|
||||
t.Fatalf("emulator size = %dx%d, want %dx%d", gotCols, gotRows, cols, rows)
|
||||
}
|
||||
}
|
||||
t.Fatalf("pane %s missing emulator", paneID)
|
||||
}
|
||||
|
||||
func recvFrameWithin(tr protocol.Transport, timeout time.Duration) (protocol.Frame, error, bool) {
|
||||
type result struct {
|
||||
f protocol.Frame
|
||||
err error
|
||||
}
|
||||
ch := make(chan result, 1)
|
||||
go func() {
|
||||
f, err := tr.Recv()
|
||||
ch <- result{f: f, err: err}
|
||||
}()
|
||||
select {
|
||||
case r := <-ch:
|
||||
return r.f, r.err, true
|
||||
case <-time.After(timeout):
|
||||
return protocol.Frame{}, nil, false
|
||||
}
|
||||
}
|
||||
|
||||
func chromeHasProcess(chrome protocol.Chrome, name string) bool {
|
||||
var model struct {
|
||||
Processes []childModel `json:"processes"`
|
||||
}
|
||||
if err := json.Unmarshal(chrome.Model, &model); err != nil {
|
||||
return false
|
||||
}
|
||||
for _, p := range model.Processes {
|
||||
if p.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -111,6 +111,13 @@ func (d *debugCapture) OnChildStateChanged(id string, state IdleState) {
|
||||
})
|
||||
}
|
||||
|
||||
func (d *debugCapture) OnChildClosed(id string) {
|
||||
d.writeEvent("child_closed", map[string]any{
|
||||
"time": time.Now().Format(time.RFC3339Nano),
|
||||
"id": id,
|
||||
})
|
||||
}
|
||||
|
||||
func (d *debugCapture) OnPTYOut(childID string, chunk []byte) {
|
||||
if len(chunk) == 0 {
|
||||
return
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/hjbdev/patterm/internal/mcp"
|
||||
"github.com/hjbdev/patterm/internal/preset"
|
||||
@@ -86,10 +87,10 @@ func newToolHost(sess *Session, pads *scratchpad.Store, launcher *Launcher, pres
|
||||
return h
|
||||
}
|
||||
|
||||
// timerListenerAdapter forwards OnChildStateChanged into the timer
|
||||
// manager and ignores the other ChildEventListener methods. The
|
||||
// session's listener API is by-interface, so we wrap the manager
|
||||
// rather than make it implement the full surface.
|
||||
// timerListenerAdapter forwards OnChildStateChanged and OnChildClosed
|
||||
// into the timer manager and ignores the other ChildEventListener
|
||||
// methods. The session's listener API is by-interface, so we wrap
|
||||
// the manager rather than make it implement the full surface.
|
||||
type timerListenerAdapter struct{ m *timerManager }
|
||||
|
||||
func (a timerListenerAdapter) OnChildSpawned(*Child) {}
|
||||
@@ -98,6 +99,9 @@ func (a timerListenerAdapter) OnPTYOut(string, []byte) {}
|
||||
func (a timerListenerAdapter) OnChildStateChanged(id string, st IdleState) {
|
||||
a.m.onChildStateChanged(id, st)
|
||||
}
|
||||
func (a timerListenerAdapter) OnChildClosed(id string) {
|
||||
a.m.onChildClosed(id)
|
||||
}
|
||||
|
||||
func (h *toolHost) SetSize(cols, rows uint16) {
|
||||
h.sizeMu.Lock()
|
||||
@@ -395,7 +399,7 @@ func (h *toolHost) GetProcessOutput(callerID, processID, mode string, sinceOffse
|
||||
if c.Kind == KindAgent {
|
||||
txt = applyChromeTrim(txt, h.chromeHintsFor(c.PresetRef))
|
||||
}
|
||||
out.Content = txt
|
||||
out.Content = normalizeGridText(txt)
|
||||
return out, nil
|
||||
case "stream":
|
||||
b, end := c.StreamRead(sinceOffset)
|
||||
@@ -553,6 +557,7 @@ func (n *chunkNotifier) OnPTYOut(id string, chunk []byte) {
|
||||
}
|
||||
}
|
||||
func (n *chunkNotifier) OnChildStateChanged(string, IdleState) {}
|
||||
func (n *chunkNotifier) OnChildClosed(string) {}
|
||||
|
||||
func (h *toolHost) GetProcessPorts(callerID, processID string) ([]mcp.PortSighting, error) {
|
||||
c := h.sess.FindChild(processID)
|
||||
@@ -806,13 +811,13 @@ func (h *toolHost) TimerList(callerID string) ([]mcp.TimerInfo, error) {
|
||||
// Scratchpads / Meta
|
||||
// ───────────────────────────────────────────────────────────────────
|
||||
|
||||
func (h *toolHost) ScratchpadList() ([]scratchpad.Entry, error) { return h.pads.List() }
|
||||
func (h *toolHost) ScratchpadList(string) ([]scratchpad.Entry, error) { return h.pads.List() }
|
||||
|
||||
func (h *toolHost) ScratchpadRead(name string) (string, string, error) {
|
||||
func (h *toolHost) ScratchpadRead(_ string, name string) (string, string, error) {
|
||||
return h.pads.Read(name)
|
||||
}
|
||||
|
||||
func (h *toolHost) ScratchpadWrite(name, content, expectedRevision string) (string, error) {
|
||||
func (h *toolHost) ScratchpadWrite(_, name, content, expectedRevision string) (string, error) {
|
||||
rev, err := h.pads.Write(name, content, expectedRevision)
|
||||
if err == nil && h.scratch != nil {
|
||||
h.scratch.scratchpadsChanged()
|
||||
@@ -820,7 +825,7 @@ func (h *toolHost) ScratchpadWrite(name, content, expectedRevision string) (stri
|
||||
return rev, err
|
||||
}
|
||||
|
||||
func (h *toolHost) ScratchpadAppend(name, content string) error {
|
||||
func (h *toolHost) ScratchpadAppend(_, name, content string) error {
|
||||
err := h.pads.Append(name, content)
|
||||
if err == nil && h.scratch != nil {
|
||||
h.scratch.scratchpadsChanged()
|
||||
@@ -828,6 +833,14 @@ func (h *toolHost) ScratchpadAppend(name, content string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (h *toolHost) ScratchpadDelete(_, name string) error {
|
||||
err := h.pads.Delete(name)
|
||||
if err == nil && h.scratch != nil {
|
||||
h.scratch.scratchpadsChanged()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (h *toolHost) WhoAmI(callerID string) mcp.WhoAmI {
|
||||
w := mcp.WhoAmI{
|
||||
ProcessID: callerID,
|
||||
@@ -1006,6 +1019,30 @@ func stripANSI(s string) string {
|
||||
return ansiRegexp.ReplaceAllString(s, "")
|
||||
}
|
||||
|
||||
func normalizeGridText(s string) string {
|
||||
s = strings.ReplaceAll(s, "\r\n", "\n")
|
||||
s = strings.ReplaceAll(s, "\r", "\n")
|
||||
|
||||
lines := strings.Split(s, "\n")
|
||||
out := make([]string, 0, len(lines))
|
||||
pendingBlank := false
|
||||
for _, line := range lines {
|
||||
line = strings.TrimRightFunc(line, unicode.IsSpace)
|
||||
if line == "" {
|
||||
if len(out) > 0 {
|
||||
pendingBlank = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
if pendingBlank {
|
||||
out = append(out, "")
|
||||
pendingBlank = false
|
||||
}
|
||||
out = append(out, line)
|
||||
}
|
||||
return strings.Join(out, "\n")
|
||||
}
|
||||
|
||||
// stripANSIBytes is the byte-slice form of stripANSI. Skips the
|
||||
// string conversion and the regex DFA — useful when the caller will
|
||||
// itself walk the result line-by-line (SearchOutput) or feed it to a
|
||||
@@ -1087,7 +1124,7 @@ func availableToolsForRole(role mcp.CallerRole) []string {
|
||||
"send_input", "send_message", "request_human_attention",
|
||||
"timer_wait", "timer_set", "timer_fire_when_idle_any", "timer_fire_when_idle_all",
|
||||
"timer_cancel", "timer_pause", "timer_resume", "timer_list",
|
||||
"scratchpad_list", "scratchpad_read", "scratchpad_write", "scratchpad_append",
|
||||
"scratchpad_list", "scratchpad_read", "scratchpad_write", "scratchpad_append", "scratchpad_delete",
|
||||
"whoami", "help",
|
||||
}
|
||||
if role == mcp.RoleOrchestrator {
|
||||
@@ -1142,8 +1179,8 @@ func helpFor(topic string) mcp.HelpResponse {
|
||||
case "scratchpads":
|
||||
return mcp.HelpResponse{
|
||||
Topic: "scratchpads",
|
||||
Content: "Project-scoped markdown files. Read returns content + revision; pass that back as expected_revision on write to get last-write-wins-with-detection. Append is unconditional.",
|
||||
RelatedTools: []string{"scratchpad_list", "scratchpad_read", "scratchpad_write", "scratchpad_append"},
|
||||
Content: "Project-scoped markdown files. Read returns content + revision; pass that back as expected_revision on write to get last-write-wins-with-detection. Append is unconditional; delete removes a pad by name.",
|
||||
RelatedTools: []string{"scratchpad_list", "scratchpad_read", "scratchpad_write", "scratchpad_append", "scratchpad_delete"},
|
||||
}
|
||||
case "timers":
|
||||
return mcp.HelpResponse{
|
||||
|
||||
@@ -118,7 +118,8 @@ func compilePatterns(ps []string) []*regexp.Regexp {
|
||||
// - titleIdleMS: ms since the last OSC title change (0 if no title yet)
|
||||
// - title: current OSC title
|
||||
// - tail: recent output bytes for regex matching
|
||||
func classify(cfg *resolvedIdleDetection, exited, exitNonZero bool, idleMS, titleIdleMS int64, title string, tail []byte) (IdleState, string) {
|
||||
// - screen: current rendered screen text for persistent prompt matching
|
||||
func classify(cfg *resolvedIdleDetection, exited, exitNonZero bool, idleMS, titleIdleMS int64, title string, tail, screen []byte) (IdleState, string) {
|
||||
if exited {
|
||||
if exitNonZero {
|
||||
return StateError, "process exited non-zero"
|
||||
@@ -128,14 +129,14 @@ func classify(cfg *resolvedIdleDetection, exited, exitNonZero bool, idleMS, titl
|
||||
if cfg == nil {
|
||||
cfg = &resolvedIdleDetection{strategy: StrategyOutputActivity, idleThresholdMS: defaultIdleThresholdMS}
|
||||
}
|
||||
if len(tail) > 0 {
|
||||
if matchAny(cfg.errorRegexes, tail) {
|
||||
if len(tail) > 0 || len(screen) > 0 {
|
||||
if matchAny(cfg.errorRegexes, tail, screen) {
|
||||
return StateError, "error regex matched"
|
||||
}
|
||||
if matchAny(cfg.permissionRegexes, tail) {
|
||||
if matchAny(cfg.permissionRegexes, tail, screen) {
|
||||
return StatePermission, "permission regex matched"
|
||||
}
|
||||
if matchAny(cfg.thinkingRegexes, tail) {
|
||||
if matchAny(cfg.thinkingRegexes, tail, screen) {
|
||||
return StateThinking, "thinking regex matched"
|
||||
}
|
||||
}
|
||||
@@ -172,10 +173,12 @@ func baseStateFromIdleMS(idleMS, threshold int64) (IdleState, string) {
|
||||
return StateIdle, "quiet for threshold"
|
||||
}
|
||||
|
||||
func matchAny(res []*regexp.Regexp, tail []byte) bool {
|
||||
func matchAny(res []*regexp.Regexp, texts ...[]byte) bool {
|
||||
for _, re := range res {
|
||||
if re.Match(tail) {
|
||||
return true
|
||||
for _, text := range texts {
|
||||
if len(text) > 0 && re.Match(text) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
|
||||
@@ -30,7 +30,7 @@ func TestClassifyOutputActivity(t *testing.T) {
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, _ := classify(cfg, false, false, tc.idleMS, 0, "", nil)
|
||||
got, _ := classify(cfg, false, false, tc.idleMS, 0, "", nil, nil)
|
||||
if got != tc.want {
|
||||
t.Fatalf("got %q want %q", got, tc.want)
|
||||
}
|
||||
@@ -41,22 +41,37 @@ func TestClassifyOutputActivity(t *testing.T) {
|
||||
func TestClassifyTitleStability(t *testing.T) {
|
||||
cfg := &resolvedIdleDetection{strategy: StrategyOSCTitleStability, idleThresholdMS: 2000}
|
||||
// Title change recent → working.
|
||||
if got, _ := classify(cfg, false, false, 9999, 500, "step 3", nil); got != StateWorking {
|
||||
if got, _ := classify(cfg, false, false, 9999, 500, "step 3", nil, nil); got != StateWorking {
|
||||
t.Fatalf("recent title change: got %q", got)
|
||||
}
|
||||
// Title stable past threshold → idle.
|
||||
if got, _ := classify(cfg, false, false, 9999, 5000, "step 3", nil); got != StateIdle {
|
||||
if got, _ := classify(cfg, false, false, 9999, 5000, "step 3", nil, nil); got != StateIdle {
|
||||
t.Fatalf("stable title: got %q", got)
|
||||
}
|
||||
// No title yet: fall back to output activity.
|
||||
if got, _ := classify(cfg, false, false, 100, 0, "", nil); got != StateWorking {
|
||||
if got, _ := classify(cfg, false, false, 100, 0, "", nil, nil); got != StateWorking {
|
||||
t.Fatalf("no title yet, recent output: got %q", got)
|
||||
}
|
||||
if got, _ := classify(cfg, false, false, 5000, 0, "", nil); got != StateIdle {
|
||||
if got, _ := classify(cfg, false, false, 5000, 0, "", nil, nil); got != StateIdle {
|
||||
t.Fatalf("no title yet, output idle: got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyTitleStabilityThinkingPatternOverridesIdle(t *testing.T) {
|
||||
cfg := &resolvedIdleDetection{
|
||||
strategy: StrategyOSCTitleStability,
|
||||
idleThresholdMS: 2000,
|
||||
thinkingRegexes: []*regexp.Regexp{mustCompile(t, `(?i)esc to interrupt`)},
|
||||
}
|
||||
screen := []byte("• Working (5s • esc to interrupt)")
|
||||
if got, _ := classify(cfg, false, false, 9999, 5000, "codex", nil, screen); got != StateThinking {
|
||||
t.Fatalf("thinking screen marker: got %q want %q", got, StateThinking)
|
||||
}
|
||||
if got, _ := classify(cfg, false, false, 9999, 5000, "codex", nil, []byte(">_")); got != StateIdle {
|
||||
t.Fatalf("stable title without marker: got %q want %q", got, StateIdle)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyTitleStatus(t *testing.T) {
|
||||
cfg := &resolvedIdleDetection{
|
||||
strategy: StrategyOSCTitleStatus,
|
||||
@@ -67,46 +82,51 @@ func TestClassifyTitleStatus(t *testing.T) {
|
||||
"error": StateError,
|
||||
},
|
||||
}
|
||||
if got, _ := classify(cfg, false, false, 9999, 500, "Thinking…", nil); got != StateThinking {
|
||||
if got, _ := classify(cfg, false, false, 9999, 500, "Thinking…", nil, nil); got != StateThinking {
|
||||
t.Fatalf("thinking title: got %q", got)
|
||||
}
|
||||
if got, _ := classify(cfg, false, false, 9999, 500, "Waiting for permission", nil); got != StatePermission {
|
||||
if got, _ := classify(cfg, false, false, 9999, 500, "Waiting for permission", nil, nil); got != StatePermission {
|
||||
t.Fatalf("permission title: got %q", got)
|
||||
}
|
||||
// No match in map → fall back to stability.
|
||||
if got, _ := classify(cfg, false, false, 9999, 5000, "ready", nil); got != StateIdle {
|
||||
if got, _ := classify(cfg, false, false, 9999, 5000, "ready", nil, nil); got != StateIdle {
|
||||
t.Fatalf("unmatched title, stable: got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyPromoterRegex(t *testing.T) {
|
||||
cfg := &resolvedIdleDetection{
|
||||
strategy: StrategyOutputActivity,
|
||||
idleThresholdMS: 2000,
|
||||
permissionRegexes: []*regexp.Regexp{mustCompile(t, `Approve\?`)},
|
||||
errorRegexes: []*regexp.Regexp{mustCompile(t, `panic:`)},
|
||||
thinkingRegexes: []*regexp.Regexp{mustCompile(t, `Thinking`)},
|
||||
strategy: StrategyOutputActivity,
|
||||
idleThresholdMS: 2000,
|
||||
permissionRegexes: []*regexp.Regexp{mustCompile(t, `Approve\?`)},
|
||||
errorRegexes: []*regexp.Regexp{mustCompile(t, `panic:`)},
|
||||
thinkingRegexes: []*regexp.Regexp{mustCompile(t, `Thinking`)},
|
||||
}
|
||||
// Permission promoter beats idle.
|
||||
if got, _ := classify(cfg, false, false, 5000, 0, "", []byte("Approve? [y/n]")); got != StatePermission {
|
||||
if got, _ := classify(cfg, false, false, 5000, 0, "", []byte("Approve? [y/n]"), nil); got != StatePermission {
|
||||
t.Fatalf("permission promoter: got %q", got)
|
||||
}
|
||||
// Error trumps permission.
|
||||
if got, _ := classify(cfg, false, false, 5000, 0, "", []byte("panic: bad\nApprove?")); got != StateError {
|
||||
if got, _ := classify(cfg, false, false, 5000, 0, "", []byte("panic: bad\nApprove?"), nil); got != StateError {
|
||||
t.Fatalf("error promoter beats permission: got %q", got)
|
||||
}
|
||||
// Thinking promoter on idle output.
|
||||
if got, _ := classify(cfg, false, false, 5000, 0, "", []byte("Thinking…")); got != StateThinking {
|
||||
if got, _ := classify(cfg, false, false, 5000, 0, "", []byte("Thinking…"), nil); got != StateThinking {
|
||||
t.Fatalf("thinking promoter: got %q", got)
|
||||
}
|
||||
// Rendered-screen prompts still promote even when the raw tail no
|
||||
// longer contains the original prompt bytes.
|
||||
if got, _ := classify(cfg, false, false, 100, 0, "", []byte("Calling patterm..."), []byte("Approve? [y/n]")); got != StatePermission {
|
||||
t.Fatalf("screen permission promoter: got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyExitTerminal(t *testing.T) {
|
||||
cfg := &resolvedIdleDetection{strategy: StrategyOutputActivity, idleThresholdMS: 2000}
|
||||
if got, _ := classify(cfg, true, true, 0, 0, "", nil); got != StateError {
|
||||
if got, _ := classify(cfg, true, true, 0, 0, "", nil, nil); got != StateError {
|
||||
t.Fatalf("non-zero exit: got %q", got)
|
||||
}
|
||||
if got, _ := classify(cfg, true, false, 0, 0, "", nil); got != StateIdle {
|
||||
if got, _ := classify(cfg, true, false, 0, 0, "", nil, nil); got != StateIdle {
|
||||
t.Fatalf("clean exit: got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,15 +261,11 @@ func (l *Launcher) LaunchTerminal(argv []string, displayName, parentID, workDir
|
||||
}
|
||||
|
||||
func (l *Launcher) writeMCPConfig(identity string) (string, error) {
|
||||
dir, err := preset.ConfigDir()
|
||||
dir, err := mcpRuntimeDir(identity)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dir = filepath.Join(dir, "mcp")
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
path := filepath.Join(dir, identity+".json")
|
||||
path := filepath.Join(dir, "mcp.json")
|
||||
cfg := map[string]any{
|
||||
"mcpServers": map[string]any{
|
||||
"patterm": map[string]any{
|
||||
|
||||
30
internal/app/launch_test.go
Normal file
30
internal/app/launch_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWriteMCPConfigUsesRuntimeDir(t *testing.T) {
|
||||
runtimeDir := t.TempDir()
|
||||
configHome := filepath.Join(t.TempDir(), "config")
|
||||
t.Setenv("XDG_RUNTIME_DIR", runtimeDir)
|
||||
t.Setenv("XDG_CONFIG_HOME", configHome)
|
||||
|
||||
l := &Launcher{bin: "patterm", mcpSocket: "/tmp/patterm.sock"}
|
||||
path, err := l.writeMCPConfig("abc123")
|
||||
if err != nil {
|
||||
t.Fatalf("writeMCPConfig: %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(path, filepath.Join(runtimeDir, "patterm", "agents", "abc123")) {
|
||||
t.Fatalf("path = %q, want under runtime dir", path)
|
||||
}
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
t.Fatalf("config file stat: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(configHome, "patterm")); !os.IsNotExist(err) {
|
||||
t.Fatalf("writeMCPConfig created XDG config dir or unexpected stat error: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,9 @@ type paletteAction struct {
|
||||
|
||||
// For settings actions, the updated settings snapshot to persist.
|
||||
settings *settings
|
||||
|
||||
projectKey string
|
||||
projectPath string
|
||||
}
|
||||
|
||||
// Group ids order the section bands the palette renders when no query
|
||||
@@ -48,20 +51,13 @@ type paletteAction struct {
|
||||
// an equally tight Spawn-section hit.
|
||||
const (
|
||||
groupFocused = iota
|
||||
groupProject
|
||||
groupOpen
|
||||
groupSpawn
|
||||
groupSettings
|
||||
groupQuit
|
||||
)
|
||||
|
||||
var groupLabels = map[int]string{
|
||||
groupFocused: "Focused",
|
||||
groupOpen: "Open",
|
||||
groupSpawn: "Spawn",
|
||||
groupSettings: "Settings",
|
||||
groupQuit: "Quit",
|
||||
}
|
||||
|
||||
type paletteItem struct {
|
||||
label string
|
||||
hint string
|
||||
@@ -72,6 +68,14 @@ type paletteItem struct {
|
||||
matches []int
|
||||
}
|
||||
|
||||
type paletteProject struct {
|
||||
Key string
|
||||
Dir string
|
||||
Name string
|
||||
TabCount int
|
||||
IsCurrent bool
|
||||
}
|
||||
|
||||
// paletteMode toggles the palette between its fuzzy-picker UI and the
|
||||
// freeform "spawn process" form. The form lives inside the palette so
|
||||
// it shares the same modal-input contract (every byte intercepted; no
|
||||
@@ -110,10 +114,9 @@ type renameForm struct {
|
||||
}
|
||||
|
||||
type settingsInputForm struct {
|
||||
title string
|
||||
field string
|
||||
value []rune
|
||||
subtitle string
|
||||
title string
|
||||
field string
|
||||
value []rune
|
||||
}
|
||||
|
||||
// paletteState is the in-memory model for the overlay. SPEC §4: a
|
||||
@@ -129,10 +132,12 @@ type paletteState struct {
|
||||
|
||||
items []paletteItem
|
||||
|
||||
mode paletteMode
|
||||
form *spawnProcessForm
|
||||
renameForm *renameForm
|
||||
settingsInput *settingsInputForm
|
||||
mode paletteMode
|
||||
form *spawnProcessForm
|
||||
renameForm *renameForm
|
||||
settingsInput *settingsInputForm
|
||||
projects []paletteProject
|
||||
currentProject string
|
||||
|
||||
// showHelp swaps the item list for a static keybinding cheat-sheet
|
||||
// until the next keystroke. Toggled by `?` in picker mode.
|
||||
@@ -198,6 +203,12 @@ func newPalette(children []*Child, focused, focusedPad string, presets preset.Se
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *paletteState) setProjects(current string, projects []paletteProject) {
|
||||
p.currentProject = current
|
||||
p.projects = append(p.projects[:0], projects...)
|
||||
p.rebuild()
|
||||
}
|
||||
|
||||
func (p *paletteState) rebuild() {
|
||||
// Macro is resolved on the *original-case* query; the returned rest
|
||||
// keeps the user's casing intact (useful when Tab cycles chips).
|
||||
@@ -205,8 +216,10 @@ func (p *paletteState) rebuild() {
|
||||
all := p.buildItems(macro)
|
||||
|
||||
if rest == "" {
|
||||
// No textual filter: render with section headers between groups.
|
||||
p.items = itemsWithHeaders(all)
|
||||
// No textual filter: render with blank spacer rows between
|
||||
// groups so sections read as scannable bands without dashed
|
||||
// headers stealing visual weight.
|
||||
p.items = itemsWithSpacers(all)
|
||||
p.clampCursor()
|
||||
return
|
||||
}
|
||||
@@ -243,25 +256,28 @@ func (p *paletteState) rebuild() {
|
||||
}
|
||||
|
||||
// buildItems assembles every selectable row in fixed group order
|
||||
// (Focused → Open → Spawn → Quit). Headers are added by
|
||||
// itemsWithHeaders for the no-query case; scored mode drops them.
|
||||
// (Focused → Open → Spawn → Quit). Blank spacer rows are added by
|
||||
// itemsWithSpacers for the no-query case; scored mode drops them.
|
||||
// When macro is non-empty the result is filtered down to the kinds
|
||||
// that macro retains.
|
||||
func (p *paletteState) buildItems(macro string) []paletteItem {
|
||||
var out []paletteItem
|
||||
|
||||
// Group 0: Focused — context-aware actions for whatever owns focus.
|
||||
// A focused scratchpad shadows any focused child.
|
||||
// A focused scratchpad shadows any focused child. Labels are bare
|
||||
// verbs because the title bar already carries the subject ("on:
|
||||
// codex" / "pad: notes.md"); the noun + name move into the hint so
|
||||
// fuzzy queries like "close codex" still surface the row.
|
||||
switch {
|
||||
case p.focusedPad != "":
|
||||
name := p.focusedPad
|
||||
out = append(out,
|
||||
paletteItem{label: "Delete scratchpad: " + name, hint: "remove the file from disk",
|
||||
action: paletteAction{kind: "pad-delete", padName: name}, group: groupFocused},
|
||||
paletteItem{label: "Rename scratchpad: " + name, hint: "inline rename · enter to commit",
|
||||
action: paletteAction{kind: "pad-rename-form", padName: name}, group: groupFocused},
|
||||
paletteItem{label: "Edit scratchpad: " + name, hint: "open in external editor (zed)",
|
||||
paletteItem{label: "Edit", hint: "edit scratchpad · " + name + " (opens $EDITOR)",
|
||||
action: paletteAction{kind: "pad-edit", padName: name}, group: groupFocused},
|
||||
paletteItem{label: "Rename", hint: "rename scratchpad · " + name,
|
||||
action: paletteAction{kind: "pad-rename-form", padName: name}, group: groupFocused},
|
||||
paletteItem{label: "Delete", hint: "delete scratchpad · " + name,
|
||||
action: paletteAction{kind: "pad-delete", padName: name}, group: groupFocused},
|
||||
)
|
||||
case p.focused != "":
|
||||
if c := findChildByID(p.children, p.focused); c != nil {
|
||||
@@ -269,40 +285,74 @@ func (p *paletteState) buildItems(macro string) []paletteItem {
|
||||
switch c.Kind {
|
||||
case KindAgent:
|
||||
out = append(out,
|
||||
paletteItem{label: "Rename agent: " + name, hint: "inline rename · enter to commit",
|
||||
paletteItem{label: "Rename", hint: "rename agent · " + name,
|
||||
action: paletteAction{kind: "agent-rename-form", childID: c.ID}, group: groupFocused},
|
||||
paletteItem{label: "Close agent: " + name, hint: "SIGTERM " + strings.Join(c.Argv, " "),
|
||||
paletteItem{label: "Close", hint: "close agent · " + name + " (SIGTERM, escalates)",
|
||||
action: paletteAction{kind: "agent-close", childID: c.ID}, group: groupFocused},
|
||||
)
|
||||
case KindTerminal:
|
||||
out = append(out,
|
||||
paletteItem{label: "Rename", hint: "rename terminal · " + name,
|
||||
action: paletteAction{kind: "proc-rename-form", childID: c.ID}, group: groupFocused},
|
||||
paletteItem{label: "Close", hint: "close terminal · " + name + " (SIGTERM)",
|
||||
action: paletteAction{kind: "proc-stop", childID: c.ID}, group: groupFocused},
|
||||
paletteItem{label: "Restart", hint: "restart terminal · " + name,
|
||||
action: paletteAction{kind: "proc-restart", childID: c.ID}, group: groupFocused},
|
||||
)
|
||||
default:
|
||||
out = append(out,
|
||||
paletteItem{label: "Rename process: " + name, hint: "inline rename · enter to commit",
|
||||
paletteItem{label: "Rename", hint: "rename process · " + name,
|
||||
action: paletteAction{kind: "proc-rename-form", childID: c.ID}, group: groupFocused},
|
||||
paletteItem{label: "Delete process: " + name, hint: "remove entry; SIGKILL if alive",
|
||||
action: paletteAction{kind: "proc-delete", childID: c.ID}, group: groupFocused},
|
||||
paletteItem{label: "Stop process: " + name, hint: "SIGTERM · keep entry for restart",
|
||||
paletteItem{label: "Stop", hint: "stop process · " + name + " (SIGTERM, keeps entry)",
|
||||
action: paletteAction{kind: "proc-stop", childID: c.ID}, group: groupFocused},
|
||||
paletteItem{label: "Restart process: " + name, hint: "SIGTERM then start with same argv",
|
||||
paletteItem{label: "Restart", hint: "restart process · " + name,
|
||||
action: paletteAction{kind: "proc-restart", childID: c.ID}, group: groupFocused},
|
||||
paletteItem{label: "Delete", hint: "delete process · " + name + " (SIGKILL if alive)",
|
||||
action: paletteAction{kind: "proc-delete", childID: c.ID}, group: groupFocused},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Group 1: Open — switch entries for every running child. Dead
|
||||
if p.projects != nil {
|
||||
// Group 1: Project — move the current client view without tearing
|
||||
// down processes owned by the previous project.
|
||||
for _, pr := range p.projects {
|
||||
if pr.IsCurrent || pr.Key == p.currentProject {
|
||||
continue
|
||||
}
|
||||
hint := pr.Dir
|
||||
if pr.TabCount > 0 {
|
||||
hint = fmt.Sprintf("%s · %d tabs", hint, pr.TabCount)
|
||||
}
|
||||
out = append(out, paletteItem{
|
||||
label: "Switch project: " + pr.Name,
|
||||
hint: hint,
|
||||
action: paletteAction{kind: "project-switch", projectKey: pr.Key},
|
||||
group: groupProject,
|
||||
})
|
||||
}
|
||||
out = append(out, paletteItem{
|
||||
label: "Open project…",
|
||||
hint: "attach this client view to another local directory",
|
||||
action: paletteAction{kind: "project-open-form"},
|
||||
group: groupProject,
|
||||
})
|
||||
}
|
||||
|
||||
// Group 2: Open — switch entries for every running child *other than*
|
||||
// the one already focused (no point offering a no-op switch). Dead
|
||||
// agents are filtered out (no restart path); dead command processes
|
||||
// remain so they can be restarted. The currently-focused child is
|
||||
// marked with a leading ▶ instead of the older "• … (current)" suffix
|
||||
// so the row reads cleaner.
|
||||
// remain so they can be restarted.
|
||||
for _, c := range p.children {
|
||||
if c.ID == p.focused {
|
||||
continue
|
||||
}
|
||||
if c.Kind == KindAgent && c.Status() != StatusRunning {
|
||||
continue
|
||||
}
|
||||
label := "Switch to " + c.DisplayName()
|
||||
hint := strings.Join(c.Argv, " ")
|
||||
if c.ID == p.focused {
|
||||
label = "▶ " + label
|
||||
}
|
||||
if c.Status() != StatusRunning {
|
||||
label = label + " [" + string(c.Status()) + "]"
|
||||
}
|
||||
@@ -353,6 +403,12 @@ func (p *paletteState) buildItems(macro string) []paletteItem {
|
||||
action: paletteAction{kind: "settings-open"},
|
||||
group: groupSettings,
|
||||
})
|
||||
out = append(out, paletteItem{
|
||||
label: "Clear notifications",
|
||||
hint: "dismiss all toasts in the top-right of the focused pane",
|
||||
action: paletteAction{kind: "toasts-clear"},
|
||||
group: groupSettings,
|
||||
})
|
||||
|
||||
// Group 4: Quit.
|
||||
out = append(out, paletteItem{
|
||||
@@ -378,9 +434,11 @@ func (p *paletteState) buildItems(macro string) []paletteItem {
|
||||
return out
|
||||
}
|
||||
|
||||
// itemsWithHeaders splices a non-selectable header row in front of
|
||||
// each new group so the (unfiltered) list reads as scannable bands.
|
||||
func itemsWithHeaders(items []paletteItem) []paletteItem {
|
||||
// itemsWithSpacers splices a non-selectable blank row between groups
|
||||
// so the (unfiltered) list reads as scannable bands without dashed
|
||||
// section headers stealing weight from the actions themselves. The
|
||||
// first group never gets a leading spacer.
|
||||
func itemsWithSpacers(items []paletteItem) []paletteItem {
|
||||
if len(items) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -388,16 +446,13 @@ func itemsWithHeaders(items []paletteItem) []paletteItem {
|
||||
currentGroup := -1
|
||||
for _, it := range items {
|
||||
if it.group != currentGroup {
|
||||
currentGroup = it.group
|
||||
label, ok := groupLabels[it.group]
|
||||
if !ok {
|
||||
label = ""
|
||||
if currentGroup != -1 {
|
||||
result = append(result, paletteItem{
|
||||
action: paletteAction{kind: "header"},
|
||||
group: it.group,
|
||||
})
|
||||
}
|
||||
result = append(result, paletteItem{
|
||||
label: "── " + label + " ──",
|
||||
action: paletteAction{kind: "header"},
|
||||
group: it.group,
|
||||
})
|
||||
currentGroup = it.group
|
||||
}
|
||||
result = append(result, it)
|
||||
}
|
||||
@@ -646,6 +701,9 @@ func (p *paletteState) acceptOrEnterForm(adv int) (paletteAction, bool, int) {
|
||||
p.cursor = 0
|
||||
p.rebuildSettings()
|
||||
return paletteAction{}, false, adv
|
||||
case "project-open-form":
|
||||
p.enterRenameForm("project", "", "", "project path")
|
||||
return paletteAction{}, false, adv
|
||||
case "pad-rename-form":
|
||||
p.enterRenameForm("pad", a.padName, a.padName, "scratchpad: "+a.padName)
|
||||
return paletteAction{}, false, adv
|
||||
@@ -904,6 +962,9 @@ func (p *paletteState) submitRename() paletteAction {
|
||||
return paletteAction{kind: "cancel"}
|
||||
}
|
||||
newName := strings.TrimSpace(string(p.renameForm.name))
|
||||
if p.renameForm.subject == "project" {
|
||||
return paletteAction{kind: "project-open-submit", projectPath: newName}
|
||||
}
|
||||
if newName == "" {
|
||||
return paletteAction{kind: "cancel"}
|
||||
}
|
||||
@@ -1276,8 +1337,11 @@ func (p *paletteState) handleSettingsTextInput(chunk []byte, i int) (paletteActi
|
||||
}
|
||||
switch b {
|
||||
case '\r', '\n':
|
||||
p.applySettingsInput()
|
||||
changed := p.applySettingsInput()
|
||||
p.mode = paletteModeAutoSummary
|
||||
if changed {
|
||||
return p.settingsAction("settings-save"), false, 1
|
||||
}
|
||||
case 0x7f, 0x08:
|
||||
if len(p.settingsInput.value) > 0 {
|
||||
p.settingsInput.value = p.settingsInput.value[:len(p.settingsInput.value)-1]
|
||||
@@ -1307,9 +1371,6 @@ func autoSummaryRows() []autoSummaryRow {
|
||||
{key: "cadence", label: "Cadence"},
|
||||
{key: "test", label: "Test summarizer"},
|
||||
{key: "run_now", label: "Summarize current top-level agent now"},
|
||||
{key: "save", label: "Save settings"},
|
||||
{key: "cancel", label: "Cancel"},
|
||||
{key: "back", label: "Back to Settings"},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1321,6 +1382,8 @@ func (p *paletteState) activateAutoSummaryRow() (paletteAction, bool, int) {
|
||||
switch rows[p.cursor].key {
|
||||
case "enabled":
|
||||
p.settings.AutoSummary.Enabled = !p.settings.AutoSummary.Enabled
|
||||
p.settings.normalize()
|
||||
return p.settingsAction("settings-save"), false, 1
|
||||
case "provider":
|
||||
switch p.settings.AutoSummary.Provider {
|
||||
case "codex":
|
||||
@@ -1330,13 +1393,14 @@ func (p *paletteState) activateAutoSummaryRow() (paletteAction, bool, int) {
|
||||
default:
|
||||
p.settings.AutoSummary.Provider = "codex"
|
||||
}
|
||||
p.settings.normalize()
|
||||
return p.settingsAction("settings-save"), false, 1
|
||||
case "codex_model", "opencode_model", "claude_model":
|
||||
provider := strings.TrimSuffix(rows[p.cursor].key, "_model")
|
||||
p.settingsInput = &settingsInputForm{
|
||||
title: provider + " model",
|
||||
field: rows[p.cursor].key,
|
||||
value: []rune(p.settings.AutoSummary.modelFor(provider)),
|
||||
subtitle: "model flag passed to " + provider,
|
||||
title: provider + " model",
|
||||
field: rows[p.cursor].key,
|
||||
value: []rune(p.settings.AutoSummary.modelFor(provider)),
|
||||
}
|
||||
p.mode = paletteModeSettingsInput
|
||||
case "cadence":
|
||||
@@ -1348,48 +1412,42 @@ func (p *paletteState) activateAutoSummaryRow() (paletteAction, bool, int) {
|
||||
default:
|
||||
p.settings.AutoSummary.Cadence = "15s"
|
||||
}
|
||||
p.settings.normalize()
|
||||
return p.settingsAction("settings-save"), false, 1
|
||||
case "test":
|
||||
return p.settingsAction("settings-test"), true, 1
|
||||
case "run_now":
|
||||
return p.settingsAction("settings-run-now"), true, 1
|
||||
case "save":
|
||||
return p.settingsAction("settings-close"), true, 1
|
||||
case "cancel":
|
||||
return paletteAction{kind: "cancel"}, true, 1
|
||||
case "back":
|
||||
p.mode = paletteModeSettings
|
||||
p.cursor = 0
|
||||
p.query = nil
|
||||
p.rebuildSettings()
|
||||
}
|
||||
p.settings.normalize()
|
||||
return paletteAction{}, false, 1
|
||||
}
|
||||
|
||||
func (p *paletteState) applySettingsInput() {
|
||||
func (p *paletteState) applySettingsInput() bool {
|
||||
if p.settingsInput == nil {
|
||||
return
|
||||
return false
|
||||
}
|
||||
val := strings.TrimSpace(string(p.settingsInput.value))
|
||||
if val == "" {
|
||||
return
|
||||
return false
|
||||
}
|
||||
if p.settings.AutoSummary.Models == nil {
|
||||
p.settings.AutoSummary.Models = defaultSummaryModels()
|
||||
}
|
||||
changed := false
|
||||
switch p.settingsInput.field {
|
||||
case "codex_model":
|
||||
changed = p.settings.AutoSummary.Models["codex"] != val
|
||||
p.settings.AutoSummary.Models["codex"] = val
|
||||
case "opencode_model":
|
||||
changed = p.settings.AutoSummary.Models["opencode"] != val
|
||||
p.settings.AutoSummary.Models["opencode"] = val
|
||||
case "claude_model":
|
||||
changed = p.settings.AutoSummary.Models["claude"] != val
|
||||
p.settings.AutoSummary.Models["claude"] = val
|
||||
}
|
||||
p.settings.normalize()
|
||||
}
|
||||
|
||||
func (p *paletteState) settingsCloseAction() paletteAction {
|
||||
return p.settingsAction("settings-close")
|
||||
return changed
|
||||
}
|
||||
|
||||
func (p *paletteState) settingsAction(kind string) paletteAction {
|
||||
@@ -1398,7 +1456,7 @@ func (p *paletteState) settingsAction(kind string) paletteAction {
|
||||
}
|
||||
|
||||
func (p *paletteState) renderSettings(out writeFlusher, cols, rows int) {
|
||||
p.renderSimplePicker(out, cols, rows, "Settings", "esc cancel", "search settings")
|
||||
p.renderSimplePicker(out, cols, rows, "Settings", "esc close", "search settings")
|
||||
}
|
||||
|
||||
func (p *paletteState) renderSimplePicker(out writeFlusher, cols, rows int, title, hint, placeholder string) {
|
||||
@@ -1434,7 +1492,7 @@ func (p *paletteState) renderSimplePicker(out writeFlusher, cols, rows int, titl
|
||||
moveTo(&b, row, leftPad)
|
||||
b.WriteString(styleBorder + "├" + strings.Repeat("─", width-2) + "┤" + styleReset)
|
||||
row++
|
||||
footer := styleHint + "↵ open · esc cancel · ↑↓ navigate" + styleReset
|
||||
footer := styleHint + "↵ open · esc close · ↑↓ navigate" + styleReset
|
||||
moveTo(&b, row, leftPad)
|
||||
b.WriteString(styleBorder + "│" + styleReset + " " + footer + strings.Repeat(" ", max(0, content-visibleLen(footer))) + " " + styleBorder + "│" + styleReset)
|
||||
row++
|
||||
@@ -1450,7 +1508,7 @@ func (p *paletteState) renderAutoSummary(out writeFlusher, cols, rows int) {
|
||||
b.WriteString("\x1b[?25l\x1b[H\x1b[2J\x1b[3J")
|
||||
row := 2
|
||||
title := "Auto-summarization"
|
||||
hint := "esc cancel"
|
||||
hint := "esc close"
|
||||
moveTo(&b, row, leftPad)
|
||||
b.WriteString(styleBorder + "╭─ " + styleActive + title + styleReset + styleBorder + " " + strings.Repeat("─", max(2, width-visibleLen(title)-visibleLen(hint)-9)) + " " + styleHint + hint + styleReset + styleBorder + " ─╮" + styleReset)
|
||||
row++
|
||||
@@ -1472,7 +1530,7 @@ func (p *paletteState) renderAutoSummary(out writeFlusher, cols, rows int) {
|
||||
moveTo(&b, row, leftPad)
|
||||
b.WriteString(styleBorder + "├" + strings.Repeat("─", width-2) + "┤" + styleReset)
|
||||
row++
|
||||
footer := styleHint + "↵ edit/toggle · cadence 15s/30s/1m · save row commits · esc cancel" + styleReset
|
||||
footer := styleHint + "↵ edit/toggle · esc close" + styleReset
|
||||
if visibleLen(footer) > content {
|
||||
footer = clipRunes(footer, content-1) + "…"
|
||||
}
|
||||
@@ -1502,7 +1560,7 @@ func (p *paletteState) autoSummaryDisplayRows() []string {
|
||||
var out []string
|
||||
for _, row := range autoSummaryRows() {
|
||||
if v, ok := values[row.key]; ok {
|
||||
out = append(out, row.label+": "+v)
|
||||
out = append(out, styleHint+row.label+":"+styleReset+" "+v)
|
||||
} else {
|
||||
out = append(out, row.label)
|
||||
}
|
||||
@@ -1519,19 +1577,10 @@ func (p *paletteState) renderSettingsInput(out writeFlusher, cols, rows int) {
|
||||
b.WriteString("\x1b[?25l\x1b[H\x1b[2J\x1b[3J")
|
||||
row := 2
|
||||
title := p.settingsInput.title
|
||||
hint := "esc cancel"
|
||||
hint := "esc back"
|
||||
moveTo(&b, row, leftPad)
|
||||
b.WriteString(styleBorder + "╭─ " + styleActive + title + styleReset + styleBorder + " " + strings.Repeat("─", max(2, width-visibleLen(title)-visibleLen(hint)-9)) + " " + styleHint + hint + styleReset + styleBorder + " ─╮" + styleReset)
|
||||
row++
|
||||
if p.settingsInput.subtitle != "" {
|
||||
sub := p.settingsInput.subtitle
|
||||
if visibleLen(sub) > content {
|
||||
sub = clipRunes(sub, content-1) + "…"
|
||||
}
|
||||
moveTo(&b, row, leftPad)
|
||||
b.WriteString(styleBorder + "│" + styleReset + " " + styleHint + sub + styleReset + strings.Repeat(" ", max(0, content-visibleLen(sub))) + " " + styleBorder + "│" + styleReset)
|
||||
row++
|
||||
}
|
||||
value := string(p.settingsInput.value)
|
||||
if visibleLen(value) > content-2 {
|
||||
value = clipRunes(value, content-3) + "…"
|
||||
@@ -1543,7 +1592,7 @@ func (p *paletteState) renderSettingsInput(out writeFlusher, cols, rows int) {
|
||||
moveTo(&b, row, leftPad)
|
||||
b.WriteString(styleBorder + "├" + strings.Repeat("─", width-2) + "┤" + styleReset)
|
||||
row++
|
||||
footer := styleHint + "↵ save · esc cancel · ⌃u clear" + styleReset
|
||||
footer := styleHint + "↵ apply · esc back · ⌃u clear" + styleReset
|
||||
moveTo(&b, row, leftPad)
|
||||
b.WriteString(styleBorder + "│" + styleReset + " " + footer + strings.Repeat(" ", max(0, content-visibleLen(footer))) + " " + styleBorder + "│" + styleReset)
|
||||
row++
|
||||
|
||||
@@ -31,16 +31,17 @@ func findItem(p *paletteState, want string) (int, *paletteItem) {
|
||||
|
||||
func TestContextItemsScratchpad(t *testing.T) {
|
||||
p := newPalette(nil, "", "notes.md", preset.Set{})
|
||||
// pad-delete is the first selectable row; the Focused section header
|
||||
// (a non-selectable row) sits above it.
|
||||
if i, _ := findItem(p, "pad-delete"); i != 1 {
|
||||
t.Fatalf("pad-delete at %d; want 1 (after Focused header)", i)
|
||||
// With the dashed section header gone, pad-edit is the first row;
|
||||
// pad-rename-form follows, with destructive pad-delete last in the
|
||||
// Focused section.
|
||||
if i, _ := findItem(p, "pad-edit"); i != 0 {
|
||||
t.Fatalf("pad-edit at %d; want 0", i)
|
||||
}
|
||||
if _, it := findItem(p, "pad-rename-form"); it == nil || it.action.padName != "notes.md" {
|
||||
t.Fatalf("pad-rename-form missing or wrong padName: %+v", it)
|
||||
}
|
||||
if _, it := findItem(p, "pad-edit"); it == nil {
|
||||
t.Fatalf("pad-edit missing")
|
||||
if i, _ := findItem(p, "pad-delete"); i < 0 {
|
||||
t.Fatalf("pad-delete missing")
|
||||
}
|
||||
// No focused child → no agent/proc context items.
|
||||
if i, _ := findItem(p, "agent-rename-form"); i != -1 {
|
||||
@@ -82,9 +83,31 @@ func TestContextItemsProcess(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextItemsTerminalUsesCloseNotStop(t *testing.T) {
|
||||
c := makeFakeChild("tid", "terminal", KindTerminal)
|
||||
p := newPalette([]*Child{c}, "tid", "", preset.Set{})
|
||||
if _, it := findItem(p, "proc-stop"); it == nil || it.label != "Close" {
|
||||
t.Fatalf("terminal close row missing or mislabelled: %+v", it)
|
||||
}
|
||||
if _, it := findItem(p, "proc-restart"); it == nil {
|
||||
t.Fatalf("terminal restart row missing")
|
||||
}
|
||||
if i, _ := findItem(p, "proc-delete"); i != -1 {
|
||||
t.Fatalf("terminal should not show a separate delete/close row, found at %d", i)
|
||||
}
|
||||
for i, it := range p.items {
|
||||
if it.label == "Stop" {
|
||||
t.Fatalf("terminal should not show Stop row, found at %d", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextItemsAppearAboveSwitch(t *testing.T) {
|
||||
c := makeFakeChild("pid", "devserver", KindCommand)
|
||||
p := newPalette([]*Child{c}, "pid", "", preset.Set{})
|
||||
// Two children so there's still a non-focused switch entry to compare
|
||||
// against (the focused child is suppressed from the Open section).
|
||||
focused := makeFakeChild("pid", "devserver", KindCommand)
|
||||
other := makeFakeChild("oid", "worker", KindCommand)
|
||||
p := newPalette([]*Child{focused, other}, "pid", "", preset.Set{})
|
||||
procIdx, _ := findItem(p, "proc-rename-form")
|
||||
switchIdx, _ := findItem(p, "switch")
|
||||
if procIdx < 0 || switchIdx < 0 {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -57,22 +58,45 @@ func TestPaletteDropsGlobalCloseList(t *testing.T) {
|
||||
|
||||
// -- Phase 2: section headers and cursor skip ------------------------
|
||||
|
||||
func TestPaletteSectionHeadersPresent(t *testing.T) {
|
||||
func TestPaletteSectionsSeparatedBySpacers(t *testing.T) {
|
||||
// Section-named dashed headers are gone; groups are visually
|
||||
// separated by a single non-selectable blank row. Verify that the
|
||||
// build emits one such spacer between every pair of adjacent groups
|
||||
// and never a leading spacer.
|
||||
c := makeFakeChild("a", "claude", KindAgent)
|
||||
p := newPalette([]*Child{c}, "a", "", preset.Set{Agents: []*preset.Preset{{Name: "codex"}}})
|
||||
wantSections := []string{"Focused", "Open", "Spawn", "Quit"}
|
||||
for _, w := range wantSections {
|
||||
found := false
|
||||
for _, it := range p.items {
|
||||
if it.action.kind == "header" && strings.Contains(it.label, w) {
|
||||
found = true
|
||||
break
|
||||
other := makeFakeChild("b", "worker", KindCommand)
|
||||
p := newPalette([]*Child{c, other}, "a", "",
|
||||
preset.Set{Agents: []*preset.Preset{{Name: "codex"}}})
|
||||
|
||||
if len(p.items) == 0 {
|
||||
t.Fatalf("palette built no items")
|
||||
}
|
||||
if p.items[0].action.kind == "header" {
|
||||
t.Fatalf("first row is a spacer; should be a selectable item")
|
||||
}
|
||||
transitions := 0
|
||||
prevGroup := p.items[0].group
|
||||
for i := 1; i < len(p.items); i++ {
|
||||
it := p.items[i]
|
||||
if it.group != prevGroup {
|
||||
if it.action.kind != "header" || it.label != "" {
|
||||
t.Fatalf("group transition at %d not a blank spacer: %+v", i, it)
|
||||
}
|
||||
transitions++
|
||||
// The row immediately after the spacer must be selectable.
|
||||
if i+1 >= len(p.items) || p.items[i+1].action.kind == "header" {
|
||||
t.Fatalf("spacer at %d not followed by selectable row", i)
|
||||
}
|
||||
prevGroup = p.items[i+1].group
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("section header %q missing from items", w)
|
||||
// No dashed banners anywhere.
|
||||
if it.action.kind == "header" && strings.Contains(it.label, "──") {
|
||||
t.Errorf("dashed section header still present at %d: %q", i, it.label)
|
||||
}
|
||||
}
|
||||
if transitions == 0 {
|
||||
t.Fatalf("no section transitions found in palette items")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaletteCursorSkipsHeaders(t *testing.T) {
|
||||
@@ -333,20 +357,95 @@ func TestAutoSummaryCadenceCyclesSoloValues(t *testing.T) {
|
||||
if p.settings.AutoSummary.Cadence != "1m" {
|
||||
t.Fatalf("initial cadence = %q", p.settings.AutoSummary.Cadence)
|
||||
}
|
||||
p.activateAutoSummaryRow()
|
||||
action, done, _ := p.activateAutoSummaryRow()
|
||||
if done || action.kind != "settings-save" {
|
||||
t.Fatalf("first cycle action = %+v done=%v, want settings-save without close", action, done)
|
||||
}
|
||||
if p.settings.AutoSummary.Cadence != "15s" {
|
||||
t.Fatalf("first cycle cadence = %q", p.settings.AutoSummary.Cadence)
|
||||
}
|
||||
p.activateAutoSummaryRow()
|
||||
action, done, _ = p.activateAutoSummaryRow()
|
||||
if done || action.kind != "settings-save" {
|
||||
t.Fatalf("second cycle action = %+v done=%v, want settings-save without close", action, done)
|
||||
}
|
||||
if p.settings.AutoSummary.Cadence != "30s" {
|
||||
t.Fatalf("second cycle cadence = %q", p.settings.AutoSummary.Cadence)
|
||||
}
|
||||
p.activateAutoSummaryRow()
|
||||
action, done, _ = p.activateAutoSummaryRow()
|
||||
if done || action.kind != "settings-save" {
|
||||
t.Fatalf("third cycle action = %+v done=%v, want settings-save without close", action, done)
|
||||
}
|
||||
if p.settings.AutoSummary.Cadence != "1m" {
|
||||
t.Fatalf("third cycle cadence = %q", p.settings.AutoSummary.Cadence)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoSummaryScreenOmitsExplicitSaveCancelBackRows(t *testing.T) {
|
||||
omitted := map[string]bool{
|
||||
"Save settings": true,
|
||||
"Cancel": true,
|
||||
"Back to Settings": true,
|
||||
}
|
||||
for _, row := range autoSummaryRows() {
|
||||
if omitted[row.label] {
|
||||
t.Fatalf("auto-summary settings should not show %q", row.label)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoSummaryRenderOmitsStaleSettingsHelp(t *testing.T) {
|
||||
p := newPalette(nil, "", "", preset.Set{}, defaultSettings())
|
||||
p.mode = paletteModeAutoSummary
|
||||
var b bytes.Buffer
|
||||
p.renderAutoSummary(wrapWriter(&b), 100, 30)
|
||||
out := b.String()
|
||||
for _, text := range []string{
|
||||
"Save settings",
|
||||
"Cancel",
|
||||
"Back to Settings",
|
||||
"changes save",
|
||||
"applies immediately",
|
||||
} {
|
||||
if strings.Contains(out, text) {
|
||||
t.Fatalf("auto-summary render should not contain %q:\n%s", text, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoSummaryValueRowsStyleLabelAndValueSeparately(t *testing.T) {
|
||||
p := newPalette(nil, "", "", preset.Set{}, defaultSettings())
|
||||
rows := p.autoSummaryDisplayRows()
|
||||
for _, row := range rows {
|
||||
if strings.Contains(row, "Cadence:") {
|
||||
if !strings.HasPrefix(row, styleHint+"Cadence:"+styleReset+" ") {
|
||||
t.Fatalf("cadence row styling = %q", row)
|
||||
}
|
||||
if strings.Contains(strings.TrimPrefix(row, styleHint+"Cadence:"+styleReset+" "), styleHint) {
|
||||
t.Fatalf("cadence value should use regular text styling: %q", row)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatal("missing cadence display row")
|
||||
}
|
||||
|
||||
func TestAutoSummaryTextInputSavesWhenSubmitted(t *testing.T) {
|
||||
p := newPalette(nil, "", "", preset.Set{}, defaultSettings())
|
||||
p.mode = paletteModeSettingsInput
|
||||
p.settingsInput = &settingsInputForm{
|
||||
title: "codex model",
|
||||
field: "codex_model",
|
||||
value: []rune("custom-model"),
|
||||
}
|
||||
action, done, _ := p.handleSettingsTextInput([]byte{'\r'}, 0)
|
||||
if done || action.kind != "settings-save" {
|
||||
t.Fatalf("submit action = %+v done=%v, want settings-save without close", action, done)
|
||||
}
|
||||
if got := p.settings.AutoSummary.modelFor("codex"); got != "custom-model" {
|
||||
t.Fatalf("codex model = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaletteFormCtrlRTogglesRelaunchFromCommandField(t *testing.T) {
|
||||
p := newPalette(nil, "", "", preset.Set{})
|
||||
p.mode = paletteModeSpawnForm
|
||||
|
||||
162
internal/app/project_registry_test.go
Normal file
162
internal/app/project_registry_test.go
Normal file
@@ -0,0 +1,162 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/hjbdev/patterm/internal/preset"
|
||||
)
|
||||
|
||||
func TestSwitchProjectPreservesProjectProcessTrees(t *testing.T) {
|
||||
t.Setenv("XDG_DATA_HOME", t.TempDir())
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
reg := newProjectRegistry(preset.Set{}, defaultSettings(), nil, 80, 24)
|
||||
defer reg.Shutdown()
|
||||
|
||||
projectA, err := reg.Open(ctx, t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatalf("open project A: %v", err)
|
||||
}
|
||||
projectB, err := reg.Open(ctx, t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatalf("open project B: %v", err)
|
||||
}
|
||||
|
||||
a, err := projectA.Session.Spawn(SpawnSpec{
|
||||
Kind: KindCommand,
|
||||
Argv: []string{"sh", "-c", "trap 'exit 0' TERM; while :; do sleep 1; done"},
|
||||
Name: "a-loop",
|
||||
}, 80, 24)
|
||||
if err != nil {
|
||||
t.Fatalf("spawn project A command: %v", err)
|
||||
}
|
||||
b, err := projectB.Session.Spawn(SpawnSpec{
|
||||
Kind: KindCommand,
|
||||
Argv: []string{"sh", "-c", "trap 'exit 0' TERM; while :; do sleep 1; done"},
|
||||
Name: "b-loop",
|
||||
}, 80, 24)
|
||||
if err != nil {
|
||||
t.Fatalf("spawn project B command: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = projectA.Session.Kill(a.ID, syscall.SIGTERM)
|
||||
_ = projectB.Session.Kill(b.ID, syscall.SIGTERM)
|
||||
})
|
||||
waitUntilLive(t, a)
|
||||
waitUntilLive(t, b)
|
||||
|
||||
st := &uiState{
|
||||
registry: reg,
|
||||
project: projectA,
|
||||
sess: projectA.Session,
|
||||
launcher: projectA.Launcher,
|
||||
pads: projectA.Pads,
|
||||
trust: projectA.Trust,
|
||||
timers: projectA.Host.timers,
|
||||
chromeWake: make(chan struct{}, 1),
|
||||
view: ClientView{
|
||||
ID: "test",
|
||||
ProjectKey: projectA.Key,
|
||||
ProjectName: projectA.Name,
|
||||
Cols: 80,
|
||||
Rows: 24,
|
||||
},
|
||||
}
|
||||
st.focusChildLocked(a)
|
||||
projectA.Session.Subscribe(st)
|
||||
|
||||
st.switchProject(projectB)
|
||||
if st.view.ProjectKey != projectB.Key {
|
||||
t.Fatalf("view project key = %q, want %q", st.view.ProjectKey, projectB.Key)
|
||||
}
|
||||
if st.sess != projectB.Session {
|
||||
t.Fatalf("ui session did not move to project B")
|
||||
}
|
||||
if projectA.Session.FindChild(a.ID) == nil {
|
||||
t.Fatalf("project A child disappeared after switch")
|
||||
}
|
||||
if projectB.Session.FindChild(b.ID) == nil {
|
||||
t.Fatalf("project B child disappeared after switch")
|
||||
}
|
||||
if !a.IsLive() {
|
||||
t.Fatalf("project A child stopped after switch")
|
||||
}
|
||||
if !b.IsLive() {
|
||||
t.Fatalf("project B child stopped after switch")
|
||||
}
|
||||
|
||||
st.switchProject(projectA)
|
||||
if st.view.ProjectKey != projectA.Key {
|
||||
t.Fatalf("view project key after switching back = %q, want %q", st.view.ProjectKey, projectA.Key)
|
||||
}
|
||||
if projectA.Session.FindChild(a.ID) == nil || projectB.Session.FindChild(b.ID) == nil {
|
||||
t.Fatalf("switching back should preserve both project process trees")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectRegistryScratchpadsRouteByCallerProject(t *testing.T) {
|
||||
t.Setenv("XDG_DATA_HOME", t.TempDir())
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
reg := newProjectRegistry(preset.Set{}, defaultSettings(), nil, 80, 24)
|
||||
defer reg.Shutdown()
|
||||
|
||||
projectA, err := reg.Open(ctx, t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatalf("open project A: %v", err)
|
||||
}
|
||||
projectB, err := reg.Open(ctx, t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatalf("open project B: %v", err)
|
||||
}
|
||||
|
||||
a, err := projectA.Session.Spawn(SpawnSpec{
|
||||
Kind: KindCommand,
|
||||
Argv: []string{"sh", "-c", "trap 'exit 0' TERM; while :; do sleep 1; done"},
|
||||
Name: "a-caller",
|
||||
}, 80, 24)
|
||||
if err != nil {
|
||||
t.Fatalf("spawn project A caller: %v", err)
|
||||
}
|
||||
b, err := projectB.Session.Spawn(SpawnSpec{
|
||||
Kind: KindCommand,
|
||||
Argv: []string{"sh", "-c", "trap 'exit 0' TERM; while :; do sleep 1; done"},
|
||||
Name: "b-caller",
|
||||
}, 80, 24)
|
||||
if err != nil {
|
||||
t.Fatalf("spawn project B caller: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = projectA.Session.Kill(a.ID, syscall.SIGTERM)
|
||||
_ = projectB.Session.Kill(b.ID, syscall.SIGTERM)
|
||||
})
|
||||
waitUntilLive(t, a)
|
||||
waitUntilLive(t, b)
|
||||
|
||||
if _, err := reg.ScratchpadWrite(a.ID, "note.md", "project A", ""); err != nil {
|
||||
t.Fatalf("write project A scratchpad: %v", err)
|
||||
}
|
||||
if _, err := reg.ScratchpadWrite(b.ID, "note.md", "project B", ""); err != nil {
|
||||
t.Fatalf("write project B scratchpad: %v", err)
|
||||
}
|
||||
|
||||
gotA, _, err := reg.ScratchpadRead(a.ID, "note.md")
|
||||
if err != nil {
|
||||
t.Fatalf("read project A scratchpad: %v", err)
|
||||
}
|
||||
gotB, _, err := reg.ScratchpadRead(b.ID, "note.md")
|
||||
if err != nil {
|
||||
t.Fatalf("read project B scratchpad: %v", err)
|
||||
}
|
||||
if gotA != "project A" || gotB != "project B" {
|
||||
t.Fatalf("scratchpad routing leaked between projects: A=%q B=%q", gotA, gotB)
|
||||
}
|
||||
}
|
||||
@@ -104,3 +104,44 @@ func TestStripANSIBytesEquivalence(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeGridText(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
in string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "line endings",
|
||||
in: "one\r\ntwo\rthree",
|
||||
want: "one\ntwo\nthree",
|
||||
},
|
||||
{
|
||||
name: "trailing whitespace",
|
||||
in: "one \ntwo\t\t\nthree",
|
||||
want: "one\ntwo\nthree",
|
||||
},
|
||||
{
|
||||
name: "collapse blank runs",
|
||||
in: "one\n\n\n two\n \n\t\nthree",
|
||||
want: "one\n\n two\n\nthree",
|
||||
},
|
||||
{
|
||||
name: "trim leading and trailing blanks",
|
||||
in: "\n \n\t\none\n\n",
|
||||
want: "one",
|
||||
},
|
||||
{
|
||||
name: "already clean",
|
||||
in: "one\n\ntwo\nthree",
|
||||
want: "one\n\ntwo\nthree",
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := normalizeGridText(tc.in); got != tc.want {
|
||||
t.Fatalf("normalizeGridText(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
137
internal/app/scratchpad_delete_test.go
Normal file
137
internal/app/scratchpad_delete_test.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/hjbdev/patterm/internal/preset"
|
||||
"github.com/hjbdev/patterm/internal/scratchpad"
|
||||
)
|
||||
|
||||
func silenceStdout(t *testing.T) {
|
||||
t.Helper()
|
||||
old := os.Stdout
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatalf("pipe stdout: %v", err)
|
||||
}
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
_, _ = io.Copy(io.Discard, r)
|
||||
close(done)
|
||||
}()
|
||||
os.Stdout = w
|
||||
t.Cleanup(func() {
|
||||
os.Stdout = old
|
||||
_ = w.Close()
|
||||
<-done
|
||||
_ = r.Close()
|
||||
})
|
||||
}
|
||||
|
||||
func newScratchpadDeleteTestState(t *testing.T) (*uiState, *scratchpad.Store) {
|
||||
t.Helper()
|
||||
t.Setenv("XDG_DATA_HOME", t.TempDir())
|
||||
pads, err := scratchpad.Open("scratchpad-delete-test")
|
||||
if err != nil {
|
||||
t.Fatalf("scratchpad.Open: %v", err)
|
||||
}
|
||||
sess := NewSession(t.TempDir(), "scratchpad-delete-test")
|
||||
t.Cleanup(sess.Shutdown)
|
||||
st := &uiState{
|
||||
sess: sess,
|
||||
pads: pads,
|
||||
hostCols: 120,
|
||||
hostRows: 40,
|
||||
chromeWake: make(chan struct{}, 1),
|
||||
}
|
||||
return st, pads
|
||||
}
|
||||
|
||||
func TestDeletingFocusedScratchpadFocusesAnotherPad(t *testing.T) {
|
||||
silenceStdout(t)
|
||||
st, pads := newScratchpadDeleteTestState(t)
|
||||
if _, err := pads.Write("alpha.md", "alpha", ""); err != nil {
|
||||
t.Fatalf("write alpha: %v", err)
|
||||
}
|
||||
if _, err := pads.Write("beta.md", "beta", ""); err != nil {
|
||||
t.Fatalf("write beta: %v", err)
|
||||
}
|
||||
st.focusedPad = "alpha.md"
|
||||
st.focusedName = "alpha.md"
|
||||
st.padOffsetName = "alpha.md"
|
||||
st.padOffset = 3
|
||||
|
||||
st.handlePadDelete("alpha.md")
|
||||
|
||||
if st.focusedPad != "beta.md" {
|
||||
t.Fatalf("focusedPad = %q, want beta.md", st.focusedPad)
|
||||
}
|
||||
if st.focusedID != "" {
|
||||
t.Fatalf("focusedID = %q, want empty while another pad is focused", st.focusedID)
|
||||
}
|
||||
if st.padOffset != 0 || st.padOffsetName != "beta.md" {
|
||||
t.Fatalf("pad offset = (%q,%d), want (beta.md,0)", st.padOffsetName, st.padOffset)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeletingLastFocusedScratchpadFocusesRunningChild(t *testing.T) {
|
||||
silenceStdout(t)
|
||||
st, pads := newScratchpadDeleteTestState(t)
|
||||
if _, err := pads.Write("only.md", "only", ""); err != nil {
|
||||
t.Fatalf("write only: %v", err)
|
||||
}
|
||||
child := makeFakeChild("pid", "devserver", KindCommand)
|
||||
addChild(st.sess, child)
|
||||
st.focusedPad = "only.md"
|
||||
st.focusedName = "only.md"
|
||||
|
||||
st.handlePadDelete("only.md")
|
||||
|
||||
if st.focusedPad != "" {
|
||||
t.Fatalf("focusedPad = %q, want empty after falling back to child", st.focusedPad)
|
||||
}
|
||||
if st.focusedID != "pid" {
|
||||
t.Fatalf("focusedID = %q, want pid", st.focusedID)
|
||||
}
|
||||
}
|
||||
|
||||
type scratchpadChangeRecorder struct {
|
||||
count int
|
||||
}
|
||||
|
||||
func (r *scratchpadChangeRecorder) scratchpadsChanged() {
|
||||
r.count++
|
||||
}
|
||||
|
||||
func TestToolHostScratchpadDeleteRemovesPadAndRefreshes(t *testing.T) {
|
||||
t.Setenv("XDG_DATA_HOME", t.TempDir())
|
||||
pads, err := scratchpad.Open("scratchpad-delete-host-test")
|
||||
if err != nil {
|
||||
t.Fatalf("scratchpad.Open: %v", err)
|
||||
}
|
||||
if _, err := pads.Write("doomed.md", "content", ""); err != nil {
|
||||
t.Fatalf("write doomed.md: %v", err)
|
||||
}
|
||||
recorder := &scratchpadChangeRecorder{}
|
||||
host := newToolHost(nil, pads, nil, preset.Set{}, nil, 120, 40)
|
||||
host.scratch = recorder
|
||||
|
||||
if err := host.ScratchpadDelete("", "doomed.md"); err != nil {
|
||||
t.Fatalf("ScratchpadDelete: %v", err)
|
||||
}
|
||||
if recorder.count != 1 {
|
||||
t.Fatalf("scratchpadsChanged calls = %d, want 1", recorder.count)
|
||||
}
|
||||
if _, _, err := pads.Read("doomed.md"); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("read deleted pad error = %v, want os.ErrNotExist", err)
|
||||
}
|
||||
if err := host.ScratchpadDelete("", "doomed.md"); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("delete missing error = %v, want os.ErrNotExist", err)
|
||||
}
|
||||
if recorder.count != 1 {
|
||||
t.Fatalf("scratchpadsChanged calls after failed delete = %d, want 1", recorder.count)
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,13 @@ type Session struct {
|
||||
listenersMu sync.Mutex
|
||||
listeners atomic.Pointer[[]ChildEventListener]
|
||||
|
||||
// clientListeners is the network-client subscriber path. These
|
||||
// listeners must be non-blocking and copy PTY chunks before enqueueing;
|
||||
// daemon-internal observers (timers, debug capture, waiters) stay on
|
||||
// listeners above so backpressure policy is isolated to clients.
|
||||
clientListenersMu sync.Mutex
|
||||
clientListeners atomic.Pointer[[]ChildEventListener]
|
||||
|
||||
// persistStore records top-level command entries to a per-project
|
||||
// JSON file so they can be re-spawned after patterm restarts.
|
||||
// Optional; nil means "no persistence" (used by unit tests).
|
||||
@@ -91,6 +98,12 @@ type ChildEventListener interface {
|
||||
// updates a child's IdleState. Listeners use this to repaint the
|
||||
// sidebar badge and to evaluate idle-aware timers.
|
||||
OnChildStateChanged(childID string, state IdleState)
|
||||
// OnChildClosed fires when a child is being removed from the
|
||||
// session (either via close_process, or — for agent/terminal
|
||||
// kinds — when the PTY exits and the entry will never be
|
||||
// restarted). It signals that any pending references to childID
|
||||
// (e.g. timers owned by or watching it) should be dropped.
|
||||
OnChildClosed(childID string)
|
||||
}
|
||||
|
||||
func NewSession(projectDir, projectKey string) *Session {
|
||||
@@ -112,6 +125,16 @@ func (s *Session) Subscribe(l ChildEventListener) {
|
||||
s.listeners.Store(&next)
|
||||
}
|
||||
|
||||
func (s *Session) SubscribeClient(l ChildEventListener) {
|
||||
s.clientListenersMu.Lock()
|
||||
defer s.clientListenersMu.Unlock()
|
||||
prev := s.clientListenersSnapshot()
|
||||
next := make([]ChildEventListener, 0, len(prev)+1)
|
||||
next = append(next, prev...)
|
||||
next = append(next, l)
|
||||
s.clientListeners.Store(&next)
|
||||
}
|
||||
|
||||
// Unsubscribe removes a previously-registered listener. Safe to call
|
||||
// with a listener that wasn't registered (no-op).
|
||||
func (s *Session) Unsubscribe(l ChildEventListener) {
|
||||
@@ -130,6 +153,24 @@ func (s *Session) Unsubscribe(l ChildEventListener) {
|
||||
s.listeners.Store(&next)
|
||||
}
|
||||
|
||||
// UnsubscribeClient removes a previously-registered network client listener.
|
||||
// Safe to call with a listener that was never registered.
|
||||
func (s *Session) UnsubscribeClient(l ChildEventListener) {
|
||||
s.clientListenersMu.Lock()
|
||||
defer s.clientListenersMu.Unlock()
|
||||
prev := s.clientListenersSnapshot()
|
||||
if len(prev) == 0 {
|
||||
return
|
||||
}
|
||||
next := make([]ChildEventListener, 0, len(prev))
|
||||
for _, e := range prev {
|
||||
if e != l {
|
||||
next = append(next, e)
|
||||
}
|
||||
}
|
||||
s.clientListeners.Store(&next)
|
||||
}
|
||||
|
||||
// listenersSnapshot returns the frozen listener slice. Safe to call
|
||||
// without the listeners mutex.
|
||||
func (s *Session) listenersSnapshot() []ChildEventListener {
|
||||
@@ -140,16 +181,30 @@ func (s *Session) listenersSnapshot() []ChildEventListener {
|
||||
return *p
|
||||
}
|
||||
|
||||
func (s *Session) clientListenersSnapshot() []ChildEventListener {
|
||||
p := s.clientListeners.Load()
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
return *p
|
||||
}
|
||||
|
||||
func (s *Session) emitSpawn(c *Child) {
|
||||
for _, l := range s.listenersSnapshot() {
|
||||
l.OnChildSpawned(c)
|
||||
}
|
||||
for _, l := range s.clientListenersSnapshot() {
|
||||
l.OnChildSpawned(c)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) emitExit(c *Child) {
|
||||
for _, l := range s.listenersSnapshot() {
|
||||
l.OnChildExited(c)
|
||||
}
|
||||
for _, l := range s.clientListenersSnapshot() {
|
||||
l.OnChildExited(c)
|
||||
}
|
||||
}
|
||||
|
||||
// emitPTYOut dispatches a fresh PTY chunk to every listener. Listeners
|
||||
@@ -159,12 +214,27 @@ func (s *Session) emitPTYOut(id string, chunk []byte) {
|
||||
for _, l := range s.listenersSnapshot() {
|
||||
l.OnPTYOut(id, chunk)
|
||||
}
|
||||
for _, l := range s.clientListenersSnapshot() {
|
||||
l.OnPTYOut(id, chunk)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) emitStateChanged(id string, state IdleState) {
|
||||
for _, l := range s.listenersSnapshot() {
|
||||
l.OnChildStateChanged(id, state)
|
||||
}
|
||||
for _, l := range s.clientListenersSnapshot() {
|
||||
l.OnChildStateChanged(id, state)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) emitClosed(id string) {
|
||||
for _, l := range s.listenersSnapshot() {
|
||||
l.OnChildClosed(id)
|
||||
}
|
||||
for _, l := range s.clientListenersSnapshot() {
|
||||
l.OnChildClosed(id)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) ChildEnv() []string {
|
||||
@@ -214,6 +284,9 @@ func (s *Session) Spawn(spec SpawnSpec, cols, rows uint16) (*Child, error) {
|
||||
if spec.Env == nil {
|
||||
spec.Env = s.ChildEnv()
|
||||
}
|
||||
if spec.WorkDir == "" {
|
||||
spec.WorkDir = s.projectDir
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
id := s.mintUniqueIDLocked()
|
||||
@@ -374,10 +447,29 @@ func (s *Session) Close(id string, sig syscall.Signal) error {
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
// Notify listeners outside s.mu so they can take their own locks
|
||||
// without inversion. Timer manager uses this to drop pending
|
||||
// timers owned by or watching the closed child — otherwise the
|
||||
// next classifier tick can deliver a stale fire to the parent.
|
||||
s.emitClosed(id)
|
||||
s.forgetPersisted(id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Terminate stops a live child with SIGTERM/SIGKILL escalation but
|
||||
// leaves its session entry intact so callers can keep showing the
|
||||
// exited pane.
|
||||
func (s *Session) Terminate(id string, sig syscall.Signal) error {
|
||||
c := s.FindChild(id)
|
||||
if c == nil {
|
||||
return fmt.Errorf("no such process %q", id)
|
||||
}
|
||||
if c.IsLive() {
|
||||
terminateAndWait(c, sig, childStopTimeout)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// mintUniqueIDLocked mints an opaque process_id (SPEC §7) and retries
|
||||
// if it collides with an existing entry. Caller holds s.mu.
|
||||
func (s *Session) mintUniqueIDLocked() string {
|
||||
@@ -486,6 +578,7 @@ func (s *Session) reapChild(c *Child, runID uint64) {
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
s.emitClosed(c.ID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -649,6 +742,22 @@ func (s *Session) ResizeAll(cols, rows uint16) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) ResizeChild(id string, cols, rows uint16) {
|
||||
if cols == 0 || rows == 0 {
|
||||
return
|
||||
}
|
||||
c := s.FindChild(id)
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
if pty := c.PTY(); pty != nil {
|
||||
_ = pty.Resize(cols, rows)
|
||||
}
|
||||
if em := c.Emulator(); em != nil {
|
||||
_ = em.Resize(cols, rows)
|
||||
}
|
||||
}
|
||||
|
||||
// SerializeChild returns the VT bytes that reproduce the child's
|
||||
// current screen state. Used to repaint a child after the user switches
|
||||
// focus or closes the palette.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -101,6 +102,50 @@ func TestSpawnInstallsIdleDetectionBeforePublish(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTerminateEscalatesWithoutRemovingEntry(t *testing.T) {
|
||||
sess := NewSession(t.TempDir(), "test")
|
||||
c, err := sess.Spawn(SpawnSpec{
|
||||
Kind: KindAgent,
|
||||
Argv: []string{"sh", "-c", "trap '' TERM; echo ready; while :; do sleep 1; done"},
|
||||
}, 80, 24)
|
||||
if err != nil {
|
||||
t.Fatalf("spawn: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if c.IsLive() {
|
||||
_ = c.signal(syscall.SIGKILL)
|
||||
}
|
||||
})
|
||||
waitUntilLive(t, c)
|
||||
waitForStreamText(t, c, "ready")
|
||||
|
||||
start := time.Now()
|
||||
if err := sess.Terminate(c.ID, syscall.SIGTERM); err != nil {
|
||||
t.Fatalf("Terminate: %v", err)
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed < childStopTimeout {
|
||||
t.Fatalf("Terminate returned before SIGKILL fallback: elapsed=%s timeout=%s", elapsed, childStopTimeout)
|
||||
}
|
||||
waitUntilNotLive(t, c)
|
||||
|
||||
if got := sess.FindChild(c.ID); got == nil {
|
||||
t.Fatalf("Terminate removed child entry %s", c.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func waitForStreamText(t *testing.T, c *Child, want string) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
b, _ := c.StreamRead(0)
|
||||
if strings.Contains(string(b), want) {
|
||||
return
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("child %s never wrote %q", c.ID, want)
|
||||
}
|
||||
|
||||
func waitUntilLive(t *testing.T, c *Child) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
|
||||
@@ -331,7 +331,7 @@ func (st *uiState) drawSidebar() {
|
||||
write(prefix + openStyle + nameCell + styleReset + suffix)
|
||||
}
|
||||
|
||||
if summary := st.activeSummaryText(width - 4); summary != "" && row+2 <= maxRow {
|
||||
if summary := st.activeSummaryRaw(); summary != "" && row+2 <= maxRow {
|
||||
write("")
|
||||
for _, line := range wrapSidebarSummary(summary, width-4) {
|
||||
if row > maxRow {
|
||||
@@ -417,7 +417,13 @@ func wrapSidebarSummary(s string, width int) []string {
|
||||
out = append(out, cur)
|
||||
cur = ""
|
||||
}
|
||||
out = append(out, clipRunes(word, width-1)+"…")
|
||||
for visibleLen(word) > width {
|
||||
out = append(out, clipRunes(word, width))
|
||||
word = string([]rune(word)[width:])
|
||||
}
|
||||
if word != "" {
|
||||
cur = word
|
||||
}
|
||||
continue
|
||||
}
|
||||
if cur == "" {
|
||||
|
||||
@@ -404,7 +404,7 @@ func runSummarizerCommand(ctx context.Context, cfg autoSummarySettings, projectD
|
||||
case "claude":
|
||||
cmd = exec.CommandContext(ctx, "claude", "--print", "--model", model, prompt)
|
||||
default:
|
||||
cmd = exec.CommandContext(ctx, "codex", "exec", "--ephemeral", "--skip-git-repo-check", "--sandbox", "read-only", "--ask-for-approval", "never", "--model", model, "-")
|
||||
cmd = exec.CommandContext(ctx, "codex", "exec", "--ephemeral", "--skip-git-repo-check", "--sandbox", "read-only", "--model", model, "-")
|
||||
cmd.Stdin = strings.NewReader(prompt)
|
||||
}
|
||||
cmd.Dir = projectDir
|
||||
|
||||
@@ -42,8 +42,48 @@ func TestWrapSidebarSummaryKeepsWordBoundaries(t *testing.T) {
|
||||
}
|
||||
}
|
||||
long := wrapSidebarSummary("supercalifragilistic short", 8)
|
||||
if len(long) == 0 || !strings.HasSuffix(long[0], "…") {
|
||||
t.Fatalf("long word should clip with ellipsis: %#v", long)
|
||||
if len(long) == 0 || strings.Contains(strings.Join(long, ""), "…") {
|
||||
t.Fatalf("long word should wrap without ellipsis: %#v", long)
|
||||
}
|
||||
for _, line := range long {
|
||||
if visibleLen(line) > 8 {
|
||||
t.Fatalf("line %q exceeds width", line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSummaryTextForSelectsChildAndClips(t *testing.T) {
|
||||
sess := NewSession(t.TempDir(), "test")
|
||||
cfg := defaultSettings()
|
||||
st := &uiState{
|
||||
sess: sess,
|
||||
settings: cfg,
|
||||
summaries: newSummaryManager(sess, t.TempDir(), preset.Set{}, func() autoSummarySettings {
|
||||
return cfg.AutoSummary.clone()
|
||||
}, nil, nil),
|
||||
}
|
||||
st.summaries.mu.Lock()
|
||||
st.summaries.entries["a1"] = &summaryEntry{state: summaryState{Text: " alpha summary "}}
|
||||
st.summaries.entries["a2"] = &summaryEntry{state: summaryState{Text: "beta summary"}}
|
||||
st.summaries.entries["empty"] = &summaryEntry{state: summaryState{Text: " "}}
|
||||
st.summaries.entries["long"] = &summaryEntry{state: summaryState{Text: "abcdefghijklmnopqrstuvwxyz"}}
|
||||
st.summaries.mu.Unlock()
|
||||
|
||||
if got := st.summaryTextFor("a2", 20); got != "beta summary" {
|
||||
t.Fatalf("summaryTextFor(a2) = %q, want beta summary", got)
|
||||
}
|
||||
if got := st.summaryTextFor("empty", 20); got != "" {
|
||||
t.Fatalf("summaryTextFor(empty) = %q, want empty", got)
|
||||
}
|
||||
if got := st.summaryTextFor("long", 8); got != "abcdefg…" {
|
||||
t.Fatalf("summaryTextFor(long) = %q, want abcdefg…", got)
|
||||
}
|
||||
|
||||
st.settingsMu.Lock()
|
||||
st.settings.AutoSummary.Enabled = false
|
||||
st.settingsMu.Unlock()
|
||||
if got := st.summaryTextFor("a1", 20); got != "" {
|
||||
t.Fatalf("summaryTextFor disabled = %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,12 +59,14 @@ func (st *uiState) drawTabBar() {
|
||||
newHintW := utf8.RuneCountInString(newHint) + 2 // " + new " framing
|
||||
|
||||
type tabRect struct {
|
||||
startCol int
|
||||
width int
|
||||
label string
|
||||
active bool
|
||||
childID string
|
||||
startCol int
|
||||
width int
|
||||
label string
|
||||
glyph string
|
||||
glyphStyle string
|
||||
active bool
|
||||
}
|
||||
|
||||
// Reserve space at the right edge for "+ new". If there are too
|
||||
// many tabs to fit even at minTabWidth, drop tabs from the right
|
||||
// until they do. The current focus stays visible.
|
||||
@@ -114,9 +116,16 @@ func (st *uiState) drawTabBar() {
|
||||
if i < extra {
|
||||
w++
|
||||
}
|
||||
active := c.ID == focus
|
||||
glyph, glyphStyle := tabIdleGlyph(c.IdleState(), active)
|
||||
label := c.DisplayName()
|
||||
labelW := utf8.RuneCountInString(label)
|
||||
maxLabelW := w - 2 // one pad on each side
|
||||
// Reserve room for the glyph + its trailing space when present
|
||||
// (1 + 1 runes), on top of the one-cell pad on each side.
|
||||
maxLabelW := w - 2
|
||||
if glyph != "" {
|
||||
maxLabelW -= 2
|
||||
}
|
||||
if maxLabelW < 1 {
|
||||
maxLabelW = 1
|
||||
}
|
||||
@@ -129,10 +138,13 @@ func (st *uiState) drawTabBar() {
|
||||
labelW = utf8.RuneCountInString(label)
|
||||
}
|
||||
tabs = append(tabs, tabRect{
|
||||
startCol: col,
|
||||
width: w,
|
||||
label: label,
|
||||
active: c.ID == focus,
|
||||
childID: c.ID,
|
||||
startCol: col,
|
||||
width: w,
|
||||
label: label,
|
||||
glyph: glyph,
|
||||
glyphStyle: glyphStyle,
|
||||
active: active,
|
||||
})
|
||||
col += w
|
||||
}
|
||||
@@ -151,23 +163,37 @@ func (st *uiState) drawTabBar() {
|
||||
fmt.Fprintf(&b, "\x1b[3;1H\x1b[%dX", width)
|
||||
|
||||
for _, t := range tabs {
|
||||
// Row 1: centre-ish label inside the tab cell.
|
||||
// Row 1: centre-ish glyph+label inside the tab cell.
|
||||
labelW := utf8.RuneCountInString(t.label)
|
||||
leftPad := (t.width - labelW) / 2
|
||||
visibleW := labelW
|
||||
if t.glyph != "" {
|
||||
visibleW += 2 // glyph + separator space
|
||||
}
|
||||
leftPad := (t.width - visibleW) / 2
|
||||
if leftPad < 1 {
|
||||
leftPad = 1
|
||||
}
|
||||
rightPad := t.width - labelW - leftPad
|
||||
rightPad := t.width - visibleW - leftPad
|
||||
if rightPad < 0 {
|
||||
rightPad = 0
|
||||
}
|
||||
fmt.Fprintf(&b, "\x1b[1;%dH", t.startCol)
|
||||
cellStyle := styleHint
|
||||
if t.active {
|
||||
b.WriteString(styleActive)
|
||||
} else {
|
||||
b.WriteString(styleHint)
|
||||
cellStyle = styleActive
|
||||
}
|
||||
fmt.Fprintf(&b, "\x1b[1;%dH", t.startCol)
|
||||
b.WriteString(cellStyle)
|
||||
b.WriteString(strings.Repeat(" ", leftPad))
|
||||
if t.glyph != "" {
|
||||
// Glyph uses its own colour so error/permission states pop
|
||||
// regardless of tab focus, matching the sidebar's vocabulary.
|
||||
b.WriteString(styleReset)
|
||||
b.WriteString(t.glyphStyle)
|
||||
b.WriteString(t.glyph)
|
||||
b.WriteString(styleReset)
|
||||
b.WriteString(cellStyle)
|
||||
b.WriteString(" ")
|
||||
}
|
||||
b.WriteString(t.label)
|
||||
b.WriteString(strings.Repeat(" ", rightPad))
|
||||
b.WriteString(styleReset)
|
||||
@@ -195,8 +221,11 @@ func (st *uiState) drawTabBar() {
|
||||
hintCol, styleBorder, strings.Repeat("─", newHintW), styleReset)
|
||||
}
|
||||
|
||||
if summary := st.activeSummaryText(width - 2); summary != "" {
|
||||
fmt.Fprintf(&b, "\x1b[2;1H %s%s%s", styleDim, summary, styleReset)
|
||||
for _, tab := range tabs {
|
||||
summaryWidth := tab.width - 2
|
||||
if summary := st.summaryTextFor(tab.childID, summaryWidth); summary != "" {
|
||||
fmt.Fprintf(&b, "\x1b[2;%dH %s%s%s", tab.startCol, styleDim, summary, styleReset)
|
||||
}
|
||||
}
|
||||
|
||||
frame := b.String()
|
||||
@@ -218,3 +247,29 @@ func (st *uiState) drawTabBar() {
|
||||
defer st.outMu.Unlock()
|
||||
fmt.Fprintf(os.Stdout, "\x1b7%s\x1b8", frame)
|
||||
}
|
||||
|
||||
// tabIdleGlyph returns the one-rune state indicator (and its SGR style)
|
||||
// to render before a tab's label. Mirrors the sidebar's vocabulary so
|
||||
// users learn the symbols in one place: ✕ error, ? permission, ◐
|
||||
// thinking, ○ idle, ● working. Returns ("", "") for StateUnknown so the
|
||||
// first frame after spawn doesn't show a misleading badge.
|
||||
func tabIdleGlyph(state IdleState, active bool) (string, string) {
|
||||
base := styleHint
|
||||
if active {
|
||||
base = styleAccent
|
||||
}
|
||||
switch state {
|
||||
case StateError:
|
||||
return "✕", styleError
|
||||
case StatePermission:
|
||||
return "?", styleAccent
|
||||
case StateThinking:
|
||||
return "◐", base
|
||||
case StateIdle:
|
||||
return "○", base
|
||||
case StateWorking:
|
||||
return "●", base
|
||||
default:
|
||||
return "", ""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,9 +55,10 @@ type pendingTimer struct {
|
||||
type timerManager struct {
|
||||
sess *Session
|
||||
|
||||
mu sync.Mutex
|
||||
nextID int
|
||||
timers map[string]*pendingTimer
|
||||
mu sync.Mutex
|
||||
nextID int
|
||||
timers map[string]*pendingTimer
|
||||
changes chan struct{}
|
||||
|
||||
// fireFn is the callback used to deliver the body to the owning
|
||||
// process. Decoupled so tests can substitute a recorder. Defaults
|
||||
@@ -67,13 +68,25 @@ type timerManager struct {
|
||||
|
||||
func newTimerManager(sess *Session) *timerManager {
|
||||
m := &timerManager{
|
||||
sess: sess,
|
||||
timers: make(map[string]*pendingTimer),
|
||||
sess: sess,
|
||||
timers: make(map[string]*pendingTimer),
|
||||
changes: make(chan struct{}, 1),
|
||||
}
|
||||
m.fireFn = defaultFireFn
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *timerManager) changeEvents() <-chan struct{} {
|
||||
return m.changes
|
||||
}
|
||||
|
||||
func (m *timerManager) notifyChanged() {
|
||||
select {
|
||||
case m.changes <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func defaultFireFn(owner *Child, body, label string) {
|
||||
if owner == nil || !owner.IsLive() {
|
||||
return
|
||||
@@ -121,6 +134,7 @@ func (m *timerManager) TimerSet(ownerID string, body, label string, seconds floa
|
||||
m.timers[id] = t
|
||||
m.mu.Unlock()
|
||||
t.rt = time.AfterFunc(d, func() { m.fireDelay(id) })
|
||||
m.notifyChanged()
|
||||
return id, nil
|
||||
}
|
||||
|
||||
@@ -136,6 +150,7 @@ func (m *timerManager) fireDelay(id string) {
|
||||
body, label := t.body, t.label
|
||||
delete(m.timers, id)
|
||||
m.mu.Unlock()
|
||||
m.notifyChanged()
|
||||
m.fireFn(owner, body, label)
|
||||
}
|
||||
|
||||
@@ -214,6 +229,7 @@ func (m *timerManager) registerIdleTimer(kind pendingTimerKind, ownerID, body, l
|
||||
}
|
||||
m.timers[id] = t
|
||||
m.mu.Unlock()
|
||||
m.notifyChanged()
|
||||
resp.ID = id
|
||||
resp.Status = "pending"
|
||||
return resp, nil
|
||||
@@ -231,6 +247,7 @@ func (m *timerManager) fireIdleMaxWait(id string) {
|
||||
body, label := t.body, t.label
|
||||
delete(m.timers, id)
|
||||
m.mu.Unlock()
|
||||
m.notifyChanged()
|
||||
m.fireFn(owner, body, label)
|
||||
}
|
||||
|
||||
@@ -291,11 +308,79 @@ func (m *timerManager) onChildStateChanged(childID string, state IdleState) {
|
||||
delete(m.timers, id)
|
||||
}
|
||||
m.mu.Unlock()
|
||||
if len(firedIDs) > 0 {
|
||||
m.notifyChanged()
|
||||
}
|
||||
for _, f := range fires {
|
||||
m.fireFn(f.owner, f.body, f.label)
|
||||
}
|
||||
}
|
||||
|
||||
// onChildClosed drops pending timer references to childID. Called
|
||||
// from Session.Close (and the terminal-corpse cleanup in reapChild)
|
||||
// via the session listener bus — a deliberate signal from the host
|
||||
// that childID is gone and the parent is not waiting on it anymore.
|
||||
//
|
||||
// Semantics:
|
||||
// - timers owned by childID are cancelled and deleted: their owner
|
||||
// is gone, so even if defaultFireFn's IsLive guard would no-op
|
||||
// the delivery, the entry has no business surviving a close.
|
||||
// - timers watching childID have childID pruned from t.watched
|
||||
// (and t.idleBaseline). If t.watched becomes empty the timer is
|
||||
// cancelled and deleted; we deliberately do NOT synthesise a
|
||||
// fire here. The parent already received any legitimate idle
|
||||
// transition before close_process — see allWatchedIdleLocked's
|
||||
// "treat as satisfied" comment, which only applies to a
|
||||
// concurrent re-evaluation, not to this explicit-removal hook.
|
||||
//
|
||||
// The natural-exit path (reapChild → emitExit for agent/command
|
||||
// kinds) is NOT routed through here: the classifier emits a final
|
||||
// idle transition on exit, which fires and deletes any watching
|
||||
// timers exactly once. Cancelling on exit would swallow that
|
||||
// legitimate fire and leave the parent never notified.
|
||||
func (m *timerManager) onChildClosed(childID string) {
|
||||
m.mu.Lock()
|
||||
changed := false
|
||||
for id, t := range m.timers {
|
||||
if t.ownerID == childID {
|
||||
if t.rt != nil {
|
||||
t.rt.Stop()
|
||||
t.rt = nil
|
||||
}
|
||||
t.status = timerStatusCanceled
|
||||
delete(m.timers, id)
|
||||
changed = true
|
||||
continue
|
||||
}
|
||||
if !contains(t.watched, childID) {
|
||||
continue
|
||||
}
|
||||
pruned := t.watched[:0]
|
||||
for _, w := range t.watched {
|
||||
if w != childID {
|
||||
pruned = append(pruned, w)
|
||||
}
|
||||
}
|
||||
t.watched = pruned
|
||||
if t.idleBaseline != nil {
|
||||
delete(t.idleBaseline, childID)
|
||||
}
|
||||
changed = true
|
||||
if len(t.watched) == 0 {
|
||||
if t.rt != nil {
|
||||
t.rt.Stop()
|
||||
t.rt = nil
|
||||
}
|
||||
t.status = timerStatusCanceled
|
||||
delete(m.timers, id)
|
||||
}
|
||||
}
|
||||
m.mu.Unlock()
|
||||
if changed {
|
||||
m.notifyChanged()
|
||||
}
|
||||
}
|
||||
|
||||
// allWatchedIdleLocked reports whether every watched child is now
|
||||
// idle. Called with m.mu held — uses live Child.IdleState() under the
|
||||
// child's own atomic, not under m.mu.
|
||||
@@ -315,19 +400,21 @@ func (m *timerManager) allWatchedIdleLocked(t *pendingTimer) bool {
|
||||
// TimerCancel removes a pending or paused timer owned by ownerID.
|
||||
func (m *timerManager) TimerCancel(ownerID, id string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
t, ok := m.timers[id]
|
||||
if !ok {
|
||||
m.mu.Unlock()
|
||||
return mcp.Errorf(mcp.ErrorKindNotFound, "no such timer %q", id)
|
||||
}
|
||||
// Empty ownerID = top-level orchestrator caller (e.g. a non-agent
|
||||
// MCP client); allow it to manage every timer in the session.
|
||||
// Otherwise the caller's own id must match the timer's owner.
|
||||
if ownerID != "" && t.ownerID != ownerID {
|
||||
m.mu.Unlock()
|
||||
return mcp.Errorf(mcp.ErrorKindRoleForbidden, "timer %q is not owned by caller", id)
|
||||
}
|
||||
if t.status == timerStatusFired || t.status == timerStatusCanceled {
|
||||
// Cancelling a fired/cancelled timer is idempotent.
|
||||
m.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
if t.rt != nil {
|
||||
@@ -336,6 +423,8 @@ func (m *timerManager) TimerCancel(ownerID, id string) error {
|
||||
}
|
||||
t.status = timerStatusCanceled
|
||||
delete(m.timers, id)
|
||||
m.mu.Unlock()
|
||||
m.notifyChanged()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -343,18 +432,20 @@ func (m *timerManager) TimerCancel(ownerID, id string) error {
|
||||
// keeps the timer in the registry.
|
||||
func (m *timerManager) TimerPause(ownerID, id string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
t, ok := m.timers[id]
|
||||
if !ok {
|
||||
m.mu.Unlock()
|
||||
return mcp.Errorf(mcp.ErrorKindNotFound, "no such timer %q", id)
|
||||
}
|
||||
// Empty ownerID = top-level orchestrator caller (e.g. a non-agent
|
||||
// MCP client); allow it to manage every timer in the session.
|
||||
// Otherwise the caller's own id must match the timer's owner.
|
||||
if ownerID != "" && t.ownerID != ownerID {
|
||||
m.mu.Unlock()
|
||||
return mcp.Errorf(mcp.ErrorKindRoleForbidden, "timer %q is not owned by caller", id)
|
||||
}
|
||||
if t.status != timerStatusPending {
|
||||
m.mu.Unlock()
|
||||
return mcp.Errorf(mcp.ErrorKindInvalidArgs, "timer %q is not pending", id)
|
||||
}
|
||||
if t.rt != nil {
|
||||
@@ -370,6 +461,8 @@ func (m *timerManager) TimerPause(ownerID, id string) error {
|
||||
t.pausedWasMaxWait = t.kind != timerKindDelay
|
||||
}
|
||||
t.status = timerStatusPaused
|
||||
m.mu.Unlock()
|
||||
m.notifyChanged()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -448,6 +541,7 @@ func (m *timerManager) TimerResume(ownerID, id string) error {
|
||||
delete(m.timers, id)
|
||||
}
|
||||
m.mu.Unlock()
|
||||
m.notifyChanged()
|
||||
if fireNow {
|
||||
m.fireFn(owner, body, label)
|
||||
}
|
||||
@@ -528,6 +622,56 @@ func (m *timerManager) activeForChild(id string) *mcp.TimerInfo {
|
||||
return &info
|
||||
}
|
||||
|
||||
const (
|
||||
timerSidebarMinRefresh = 50 * time.Millisecond
|
||||
timerSidebarSubsecondRefresh = 100 * time.Millisecond
|
||||
)
|
||||
|
||||
func nextTimerSidebarLabelChange(d time.Duration) time.Duration {
|
||||
if d <= 0 {
|
||||
return 0
|
||||
}
|
||||
if d < time.Second {
|
||||
if d < timerSidebarSubsecondRefresh {
|
||||
return d
|
||||
}
|
||||
return timerSidebarSubsecondRefresh
|
||||
}
|
||||
|
||||
step := time.Second
|
||||
if d >= time.Hour {
|
||||
step = time.Hour
|
||||
} else if d >= time.Minute {
|
||||
step = time.Minute
|
||||
}
|
||||
wait := d % step
|
||||
if wait <= 0 || wait < timerSidebarMinRefresh {
|
||||
return timerSidebarMinRefresh
|
||||
}
|
||||
return wait
|
||||
}
|
||||
|
||||
func (m *timerManager) nextSidebarRefreshAfter(now time.Time) (time.Duration, bool) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
var best time.Duration
|
||||
found := false
|
||||
for _, t := range m.timers {
|
||||
if t.status != timerStatusPending || t.firesAt.IsZero() {
|
||||
continue
|
||||
}
|
||||
wait := nextTimerSidebarLabelChange(t.firesAt.Sub(now))
|
||||
if wait <= 0 {
|
||||
wait = timerSidebarMinRefresh
|
||||
}
|
||||
if !found || wait < best {
|
||||
best = wait
|
||||
found = true
|
||||
}
|
||||
}
|
||||
return best, found
|
||||
}
|
||||
|
||||
func isIdleState(s IdleState) bool {
|
||||
return s == StateIdle
|
||||
}
|
||||
|
||||
@@ -65,6 +65,93 @@ func newTestManager(t *testing.T) (*Session, *timerManager, *recorderFire) {
|
||||
return sess, mgr, rec
|
||||
}
|
||||
|
||||
func waitTimerChange(t *testing.T, mgr *timerManager) {
|
||||
t.Helper()
|
||||
select {
|
||||
case <-mgr.changeEvents():
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("timed out waiting for timer change signal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNextTimerSidebarLabelChange(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
d time.Duration
|
||||
want time.Duration
|
||||
}{
|
||||
{name: "minutes", d: 2*time.Minute + 10*time.Second, want: 10 * time.Second},
|
||||
{name: "minute_to_seconds", d: time.Minute + 500*time.Millisecond, want: 500 * time.Millisecond},
|
||||
{name: "seconds", d: 59*time.Second + 500*time.Millisecond, want: 500 * time.Millisecond},
|
||||
{name: "subsecond", d: 500 * time.Millisecond, want: timerSidebarSubsecondRefresh},
|
||||
{name: "nearly_done", d: 30 * time.Millisecond, want: 30 * time.Millisecond},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := nextTimerSidebarLabelChange(tt.d); got != tt.want {
|
||||
t.Fatalf("nextTimerSidebarLabelChange(%s) = %s, want %s", tt.d, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimerSidebarRefreshAfterUsesSoonestActiveBoundary(t *testing.T) {
|
||||
_, mgr, _ := newTestManager(t)
|
||||
now := time.Unix(123, 0)
|
||||
mgr.mu.Lock()
|
||||
mgr.timers["slow"] = &pendingTimer{
|
||||
id: "slow",
|
||||
status: timerStatusPending,
|
||||
firesAt: now.Add(2*time.Minute + 10*time.Second),
|
||||
}
|
||||
mgr.timers["fast"] = &pendingTimer{
|
||||
id: "fast",
|
||||
status: timerStatusPending,
|
||||
firesAt: now.Add(59*time.Second + 500*time.Millisecond),
|
||||
}
|
||||
mgr.timers["paused"] = &pendingTimer{
|
||||
id: "paused",
|
||||
status: timerStatusPaused,
|
||||
firesAt: now.Add(100 * time.Millisecond),
|
||||
}
|
||||
mgr.mu.Unlock()
|
||||
|
||||
got, ok := mgr.nextSidebarRefreshAfter(now)
|
||||
if !ok {
|
||||
t.Fatal("nextSidebarRefreshAfter did not find active timers")
|
||||
}
|
||||
if got != 500*time.Millisecond {
|
||||
t.Fatalf("nextSidebarRefreshAfter = %s, want 500ms", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimerManagerSignalsChangesForSidebar(t *testing.T) {
|
||||
sess, mgr, _ := newTestManager(t)
|
||||
owner := fakeChild("p_owner")
|
||||
addChild(sess, owner)
|
||||
|
||||
id, err := mgr.TimerSet("p_owner", "x", "", 60)
|
||||
if err != nil {
|
||||
t.Fatalf("TimerSet: %v", err)
|
||||
}
|
||||
waitTimerChange(t, mgr)
|
||||
|
||||
if err := mgr.TimerPause("p_owner", id); err != nil {
|
||||
t.Fatalf("TimerPause: %v", err)
|
||||
}
|
||||
waitTimerChange(t, mgr)
|
||||
|
||||
if err := mgr.TimerResume("p_owner", id); err != nil {
|
||||
t.Fatalf("TimerResume: %v", err)
|
||||
}
|
||||
waitTimerChange(t, mgr)
|
||||
|
||||
if err := mgr.TimerCancel("p_owner", id); err != nil {
|
||||
t.Fatalf("TimerCancel: %v", err)
|
||||
}
|
||||
waitTimerChange(t, mgr)
|
||||
}
|
||||
|
||||
func TestTimerSetDelivers(t *testing.T) {
|
||||
sess, mgr, rec := newTestManager(t)
|
||||
c := fakeChild("p_owner")
|
||||
@@ -411,3 +498,178 @@ func TestTimerRecordsRemovedOnIdleFire(t *testing.T) {
|
||||
t.Fatalf("fired idle timer %s was not removed from registry", resp.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTimerCloseChildPrunesWatched covers the happy partial-prune
|
||||
// case: an idle_any timer watches two children, one is closed, the
|
||||
// timer stays pending and the remaining child can still satisfy it.
|
||||
func TestTimerCloseChildPrunesWatched(t *testing.T) {
|
||||
sess, mgr, rec := newTestManager(t)
|
||||
owner := fakeChild("p_owner")
|
||||
a := fakeChild("p_a")
|
||||
b := fakeChild("p_b")
|
||||
addChild(sess, owner)
|
||||
addChild(sess, a)
|
||||
addChild(sess, b)
|
||||
working := StateWorking
|
||||
a.idleState.Store(&working)
|
||||
b.idleState.Store(&working)
|
||||
|
||||
resp, err := mgr.TimerFireWhenIdleAny("p_owner", "one done", "", []string{"p_a", "p_b"}, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("TimerFireWhenIdleAny: %v", err)
|
||||
}
|
||||
|
||||
mgr.onChildClosed("p_a")
|
||||
|
||||
mgr.mu.Lock()
|
||||
t1, ok := mgr.timers[resp.ID]
|
||||
if !ok {
|
||||
mgr.mu.Unlock()
|
||||
t.Fatalf("timer was removed but still has live watched")
|
||||
}
|
||||
watched := append([]string(nil), t1.watched...)
|
||||
mgr.mu.Unlock()
|
||||
if len(watched) != 1 || watched[0] != "p_b" {
|
||||
t.Fatalf("watched after close: %v, want [p_b]", watched)
|
||||
}
|
||||
if got := rec.snapshot(); len(got) != 0 {
|
||||
t.Fatalf("close synthesised a fire: %+v", got)
|
||||
}
|
||||
|
||||
// p_b can still satisfy the timer.
|
||||
idle := StateIdle
|
||||
b.idleState.Store(&idle)
|
||||
mgr.onChildStateChanged("p_b", StateIdle)
|
||||
if got := rec.snapshot(); len(got) != 1 || got[0].Body != "one done" {
|
||||
t.Fatalf("post-prune fire: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTimerCloseLastWatchedCancels is the regression for the
|
||||
// reported stale-fire symptom: the only watched child is closed,
|
||||
// so the timer must be cancelled — no synthetic fire, and the
|
||||
// registry entry must be gone so a trailing classifier tick for the
|
||||
// removed child cannot re-deliver later.
|
||||
func TestTimerCloseLastWatchedCancels(t *testing.T) {
|
||||
sess, mgr, rec := newTestManager(t)
|
||||
owner := fakeChild("p_owner")
|
||||
a := fakeChild("p_a")
|
||||
addChild(sess, owner)
|
||||
addChild(sess, a)
|
||||
working := StateWorking
|
||||
a.idleState.Store(&working)
|
||||
|
||||
resp, err := mgr.TimerFireWhenIdleAny("p_owner", "stale body", "", []string{"p_a"}, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("TimerFireWhenIdleAny: %v", err)
|
||||
}
|
||||
|
||||
mgr.onChildClosed("p_a")
|
||||
|
||||
mgr.mu.Lock()
|
||||
_, stillThere := mgr.timers[resp.ID]
|
||||
mgr.mu.Unlock()
|
||||
if stillThere {
|
||||
t.Fatalf("timer with no remaining watched should be removed")
|
||||
}
|
||||
if got := rec.snapshot(); len(got) != 0 {
|
||||
t.Fatalf("close synthesised a fire: %+v", got)
|
||||
}
|
||||
|
||||
// Simulate the trailing classifier tick for the now-closed child —
|
||||
// must not fire.
|
||||
mgr.onChildStateChanged("p_a", StateIdle)
|
||||
if got := rec.snapshot(); len(got) != 0 {
|
||||
t.Fatalf("trailing state change re-fired: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTimerCloseChildIdleAllPartialPrune mirrors the idle_any
|
||||
// partial-prune for idle_all: pruning a watched child shrinks the
|
||||
// list; the remaining child going idle then satisfies the timer.
|
||||
func TestTimerCloseChildIdleAllPartialPrune(t *testing.T) {
|
||||
sess, mgr, rec := newTestManager(t)
|
||||
owner := fakeChild("p_owner")
|
||||
a := fakeChild("p_a")
|
||||
b := fakeChild("p_b")
|
||||
addChild(sess, owner)
|
||||
addChild(sess, a)
|
||||
addChild(sess, b)
|
||||
working := StateWorking
|
||||
a.idleState.Store(&working)
|
||||
b.idleState.Store(&working)
|
||||
|
||||
resp, err := mgr.TimerFireWhenIdleAll("p_owner", "all done", "", []string{"p_a", "p_b"}, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("TimerFireWhenIdleAll: %v", err)
|
||||
}
|
||||
if resp.Status != "pending" {
|
||||
t.Fatalf("status: got %q want pending", resp.Status)
|
||||
}
|
||||
|
||||
mgr.onChildClosed("p_a")
|
||||
|
||||
idle := StateIdle
|
||||
b.idleState.Store(&idle)
|
||||
mgr.onChildStateChanged("p_b", StateIdle)
|
||||
if got := rec.snapshot(); len(got) != 1 || got[0].Body != "all done" {
|
||||
t.Fatalf("idle_all after partial prune: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTimerCloseOwnerCancelsDelay ensures a delay timer is dropped
|
||||
// when its owner is closed: no delivery, registry empty, the
|
||||
// underlying time.Timer is stopped.
|
||||
func TestTimerCloseOwnerCancelsDelay(t *testing.T) {
|
||||
sess, mgr, rec := newTestManager(t)
|
||||
c := fakeChild("p_owner")
|
||||
addChild(sess, c)
|
||||
id, err := mgr.TimerSet("p_owner", "x", "", 0.1)
|
||||
if err != nil {
|
||||
t.Fatalf("TimerSet: %v", err)
|
||||
}
|
||||
|
||||
mgr.onChildClosed("p_owner")
|
||||
|
||||
mgr.mu.Lock()
|
||||
_, stillThere := mgr.timers[id]
|
||||
mgr.mu.Unlock()
|
||||
if stillThere {
|
||||
t.Fatalf("delay timer was not removed when owner closed")
|
||||
}
|
||||
time.Sleep(200 * time.Millisecond) // past the original firesAt
|
||||
if got := rec.snapshot(); len(got) != 0 {
|
||||
t.Fatalf("delay timer fired after owner close: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTimerCloseWatchedSubAgent is the exact shape of the reported
|
||||
// stale-fire bug: orchestrator registers a watcher on a sub-agent,
|
||||
// the sub-agent is closed, and the orchestrator must receive
|
||||
// nothing (no stale body delivered after close_process).
|
||||
func TestTimerCloseWatchedSubAgent(t *testing.T) {
|
||||
sess, mgr, rec := newTestManager(t)
|
||||
parent := fakeChild("p_owner")
|
||||
sub := fakeChild("p_sub")
|
||||
addChild(sess, parent)
|
||||
addChild(sess, sub)
|
||||
working := StateWorking
|
||||
sub.idleState.Store(&working)
|
||||
|
||||
if _, err := mgr.TimerFireWhenIdleAny(
|
||||
"p_owner",
|
||||
"codex-review-591 finished. Read your own pane …",
|
||||
"", []string{"p_sub"}, 0,
|
||||
); err != nil {
|
||||
t.Fatalf("TimerFireWhenIdleAny: %v", err)
|
||||
}
|
||||
|
||||
mgr.onChildClosed("p_sub")
|
||||
|
||||
// Trailing classifier emission for the closed sub-agent must
|
||||
// not deliver anything to the parent.
|
||||
mgr.onChildStateChanged("p_sub", StateIdle)
|
||||
if got := rec.snapshot(); len(got) != 0 {
|
||||
t.Fatalf("stale fire delivered to parent after sub-agent close: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
361
internal/app/toast.go
Normal file
361
internal/app/toast.go
Normal file
@@ -0,0 +1,361 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// toastKind classifies a toast for styling and for migrating the
|
||||
// pre-existing flashError / flashTransient / notifyAttention call
|
||||
// sites onto the new stack.
|
||||
type toastKind int
|
||||
|
||||
const (
|
||||
toastInfo toastKind = iota
|
||||
toastError
|
||||
toastAttention
|
||||
)
|
||||
|
||||
// toast is one entry in the host-level notification stack. Toasts
|
||||
// persist until the user dismisses them with Ctrl-N or the
|
||||
// "Clear notifications" palette command — there's no auto-expiry.
|
||||
type toast struct {
|
||||
id uint64
|
||||
kind toastKind
|
||||
text string
|
||||
}
|
||||
|
||||
// toastStackCap caps how many toasts can be visible at once.
|
||||
// Older entries drop off the bottom when a new push would exceed it.
|
||||
const toastStackCap = 5
|
||||
|
||||
// toastBoxMaxWidth bounds the rendered box width so a wide pane
|
||||
// doesn't produce huge toasts. Boxes shrink below this when the pane
|
||||
// is narrow.
|
||||
const toastBoxMaxWidth = 50
|
||||
|
||||
// toastBoxMinWidth is the floor below which we refuse to render —
|
||||
// any narrower and there's not enough room for borders + content.
|
||||
const toastBoxMinWidth = 20
|
||||
|
||||
// toastContentRows is how many lines of message body each toast box
|
||||
// reserves. The dismiss hint lives on the host status strip, so the
|
||||
// box itself is purely the message.
|
||||
const toastContentRows = 3
|
||||
|
||||
// toastStack owns the ordered list of live toasts. Oldest at
|
||||
// index 0, newest (visually topmost) at the end. The stack's own
|
||||
// mutex is intentionally separate from uiState.mu so push / dismiss
|
||||
// can be called from any goroutine without participating in the
|
||||
// host's bigger lock-ordering rules.
|
||||
type toastStack struct {
|
||||
mu sync.Mutex
|
||||
items []toast
|
||||
next uint64
|
||||
}
|
||||
|
||||
func (s *toastStack) push(kind toastKind, text string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.next++
|
||||
s.items = append(s.items, toast{id: s.next, kind: kind, text: text})
|
||||
if len(s.items) > toastStackCap {
|
||||
s.items = s.items[len(s.items)-toastStackCap:]
|
||||
}
|
||||
}
|
||||
|
||||
// dismissTop pops the most recent toast (the one rendered at the
|
||||
// top of the stack). Returns true if something was removed so
|
||||
// callers can decide whether to repaint.
|
||||
func (s *toastStack) dismissTop() bool {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if len(s.items) == 0 {
|
||||
return false
|
||||
}
|
||||
s.items = s.items[:len(s.items)-1]
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *toastStack) clear() bool {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if len(s.items) == 0 {
|
||||
return false
|
||||
}
|
||||
s.items = s.items[:0]
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *toastStack) snapshot() []toast {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if len(s.items) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]toast, len(s.items))
|
||||
copy(out, s.items)
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *toastStack) length() int {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return len(s.items)
|
||||
}
|
||||
|
||||
// notifyToast is the single entry point that the former flash
|
||||
// helpers now delegate to. It pushes onto the stack and triggers a
|
||||
// repaint of the focused surface so the new toast appears
|
||||
// immediately; the repaint path also re-renders the stack on top.
|
||||
func (st *uiState) notifyToast(kind toastKind, text string) {
|
||||
st.toasts.push(kind, text)
|
||||
st.refreshToastSurface()
|
||||
}
|
||||
|
||||
// refreshToastSurface re-renders whatever surface the toasts are
|
||||
// drawn over (focused child, focused pad, or the empty-state
|
||||
// canvas). Each of those paths calls renderToasts at the end, so
|
||||
// the toast layer is always reapplied on top of a freshly-drawn
|
||||
// pane. Centralised so push / dismiss / clear share one code path.
|
||||
//
|
||||
// The status strip also gains/loses the "Ctrl-N · dismiss" hint as
|
||||
// the stack toggles between empty and non-empty, so we redraw it
|
||||
// here too rather than waiting for the chrome ticker.
|
||||
func (st *uiState) refreshToastSurface() {
|
||||
st.mu.Lock()
|
||||
focusedPad := st.focusedPad
|
||||
focusedID := st.focusedID
|
||||
palOpen := st.palette != nil
|
||||
st.mu.Unlock()
|
||||
if palOpen {
|
||||
// Palette owns the whole screen while it's open; toasts will
|
||||
// repaint via closePalette's restore path.
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case focusedPad != "":
|
||||
st.repaintFocusedPad()
|
||||
case focusedID != "":
|
||||
st.repaintFocused()
|
||||
default:
|
||||
st.renderEmptyState()
|
||||
}
|
||||
st.drawStatusLine()
|
||||
}
|
||||
|
||||
// renderToasts draws the toast stack over the top-right of the
|
||||
// focused pane. Called from repaintFocused / repaintFocusedPad /
|
||||
// renderEmptyState after they finish so toasts always sit on top of
|
||||
// freshly-redrawn pane content. Safe to call when the stack is
|
||||
// empty (no-op).
|
||||
func (st *uiState) renderToasts() {
|
||||
bytes := st.toastOverlayBytes()
|
||||
if len(bytes) == 0 {
|
||||
return
|
||||
}
|
||||
st.outMu.Lock()
|
||||
defer st.outMu.Unlock()
|
||||
_, _ = os.Stdout.Write(bytes)
|
||||
}
|
||||
|
||||
// toastOverlayBytes builds the toast layer as a single byte buffer
|
||||
// without writing to stdout. Returns nil when the stack is empty or
|
||||
// the layout can't accommodate a box. Callers either write it
|
||||
// directly (renderToasts) or stitch it onto the end of another
|
||||
// stdout write so claude/codex/opencode redraws that paint over the
|
||||
// top-right region can't leave the toast half-erased.
|
||||
func (st *uiState) toastOverlayBytes() []byte {
|
||||
items := st.toasts.snapshot()
|
||||
if len(items) == 0 {
|
||||
return nil
|
||||
}
|
||||
st.mu.Lock()
|
||||
palOpen := st.palette != nil
|
||||
st.mu.Unlock()
|
||||
if palOpen {
|
||||
return nil
|
||||
}
|
||||
layout := st.layoutSnapshot()
|
||||
paneCols := int(layout.childCols())
|
||||
paneRows := int(layout.childRows())
|
||||
if paneCols < toastBoxMinWidth+2 || paneRows < toastContentRows+2 {
|
||||
return nil
|
||||
}
|
||||
boxWidth := toastBoxMaxWidth
|
||||
if max := paneCols - 4; max < boxWidth {
|
||||
boxWidth = max
|
||||
}
|
||||
if boxWidth < toastBoxMinWidth {
|
||||
return nil
|
||||
}
|
||||
contentWidth := boxWidth - 4 // 2 border cells + 2 inner padding
|
||||
// Reserve two columns for the icon prefix on row 1 so wrapped rows
|
||||
// indent under the body text rather than under the glyph.
|
||||
const iconCols = 2
|
||||
bodyRoom := contentWidth - iconCols
|
||||
if bodyRoom < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
// Wrap the whole overlay in DECSET 2026 (synchronized output)
|
||||
// brackets so terminals that support BSU/ESU buffer the box paint
|
||||
// into a single frame — without this, claude's continuous redraws
|
||||
// and our overlay race on each cell, producing visible flicker.
|
||||
// Terminals that don't recognise 2026 ignore the brackets, so the
|
||||
// fallback behaviour is the same as before.
|
||||
b.WriteString("\x1b[?2026h\x1b7\x1b[?25l")
|
||||
|
||||
row := int(layout.mainTop) + 1
|
||||
col := int(layout.mainLeft) + paneCols - boxWidth - 1
|
||||
if col < int(layout.mainLeft) {
|
||||
col = int(layout.mainLeft)
|
||||
}
|
||||
|
||||
// Render newest first (visually on top), iterating items in
|
||||
// reverse so the most recent push lands at the smallest row.
|
||||
for idx := len(items) - 1; idx >= 0; idx-- {
|
||||
t := items[idx]
|
||||
height := toastContentRows + 2
|
||||
// Stop if we'd run off the bottom of the pane.
|
||||
if row+height > int(layout.mainTop)+paneRows {
|
||||
break
|
||||
}
|
||||
border := toastBorderStyle(t.kind)
|
||||
wrapped := wrapToastBody(t.text, bodyRoom)
|
||||
|
||||
// Top border.
|
||||
moveTo(&b, row, col)
|
||||
b.WriteString(border)
|
||||
b.WriteString("╭")
|
||||
b.WriteString(strings.Repeat("─", boxWidth-2))
|
||||
b.WriteString("╮")
|
||||
b.WriteString(styleReset)
|
||||
row++
|
||||
|
||||
// Content rows. Row 0 carries the kind glyph; rows 1..N indent
|
||||
// by iconCols spaces so wrapped text lines up under the body.
|
||||
for i := 0; i < toastContentRows; i++ {
|
||||
moveTo(&b, row, col)
|
||||
b.WriteString(border)
|
||||
b.WriteString("│")
|
||||
b.WriteString(styleReset)
|
||||
b.WriteString(" ")
|
||||
if i == 0 {
|
||||
b.WriteString(toastIcon(t.kind))
|
||||
} else {
|
||||
b.WriteString(strings.Repeat(" ", iconCols))
|
||||
}
|
||||
line := wrapped[i]
|
||||
b.WriteString(line)
|
||||
b.WriteString(strings.Repeat(" ", max(0, bodyRoom-visibleLen(line))))
|
||||
b.WriteString(" ")
|
||||
b.WriteString(border)
|
||||
b.WriteString("│")
|
||||
b.WriteString(styleReset)
|
||||
row++
|
||||
}
|
||||
|
||||
// Bottom border.
|
||||
moveTo(&b, row, col)
|
||||
b.WriteString(border)
|
||||
b.WriteString("╰")
|
||||
b.WriteString(strings.Repeat("─", boxWidth-2))
|
||||
b.WriteString("╯")
|
||||
b.WriteString(styleReset)
|
||||
row++
|
||||
|
||||
// 1-row gap between stacked toasts.
|
||||
row++
|
||||
}
|
||||
|
||||
b.WriteString("\x1b[?25h\x1b8\x1b[?2026l")
|
||||
return []byte(b.String())
|
||||
}
|
||||
|
||||
func toastBorderStyle(kind toastKind) string {
|
||||
switch kind {
|
||||
case toastError:
|
||||
return styleError
|
||||
case toastAttention:
|
||||
return styleAccent
|
||||
default:
|
||||
return styleBorder
|
||||
}
|
||||
}
|
||||
|
||||
// wrapToastBody word-wraps text into exactly toastContentRows lines,
|
||||
// each at most width visible runes wide. Short messages are padded
|
||||
// with empty trailing lines so callers can iterate a fixed-size
|
||||
// slice; messages that don't fit get ellipsized on the last line.
|
||||
func wrapToastBody(text string, width int) []string {
|
||||
out := make([]string, toastContentRows)
|
||||
if width < 1 {
|
||||
return out
|
||||
}
|
||||
all := wrapToastWords(text, width)
|
||||
if len(all) > toastContentRows {
|
||||
all = all[:toastContentRows]
|
||||
last := all[len(all)-1]
|
||||
if visibleLen(last) >= width {
|
||||
last = clipRunes(last, width-1) + "…"
|
||||
} else {
|
||||
last = last + "…"
|
||||
}
|
||||
all[len(all)-1] = last
|
||||
}
|
||||
for i, l := range all {
|
||||
out[i] = l
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// wrapToastWords is a small word-wrapper sized for toast bodies:
|
||||
// greedy, breaks overlong words on rune boundaries, drops collapsing
|
||||
// whitespace via strings.Fields.
|
||||
func wrapToastWords(text string, width int) []string {
|
||||
var lines []string
|
||||
var cur string
|
||||
flush := func() {
|
||||
if cur != "" {
|
||||
lines = append(lines, cur)
|
||||
cur = ""
|
||||
}
|
||||
}
|
||||
for _, word := range strings.Fields(text) {
|
||||
for visibleLen(word) > width {
|
||||
flush()
|
||||
head := clipRunes(word, width)
|
||||
lines = append(lines, head)
|
||||
word = word[len(head):]
|
||||
}
|
||||
if word == "" {
|
||||
continue
|
||||
}
|
||||
if cur == "" {
|
||||
cur = word
|
||||
continue
|
||||
}
|
||||
if visibleLen(cur)+1+visibleLen(word) <= width {
|
||||
cur += " " + word
|
||||
continue
|
||||
}
|
||||
flush()
|
||||
cur = word
|
||||
}
|
||||
flush()
|
||||
return lines
|
||||
}
|
||||
|
||||
func toastIcon(kind toastKind) string {
|
||||
switch kind {
|
||||
case toastError:
|
||||
return styleError + "✗ " + styleReset
|
||||
case toastAttention:
|
||||
return styleAccent + "! " + styleReset
|
||||
default:
|
||||
return styleHint + "• " + styleReset
|
||||
}
|
||||
}
|
||||
164
internal/app/toast_test.go
Normal file
164
internal/app/toast_test.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestToastStackPushAndOrder(t *testing.T) {
|
||||
var s toastStack
|
||||
s.push(toastInfo, "one")
|
||||
s.push(toastError, "two")
|
||||
s.push(toastAttention, "three")
|
||||
|
||||
snap := s.snapshot()
|
||||
if len(snap) != 3 {
|
||||
t.Fatalf("snapshot len = %d, want 3", len(snap))
|
||||
}
|
||||
if snap[0].text != "one" || snap[1].text != "two" || snap[2].text != "three" {
|
||||
t.Fatalf("snapshot order wrong: %#v", snap)
|
||||
}
|
||||
if snap[0].kind != toastInfo || snap[1].kind != toastError || snap[2].kind != toastAttention {
|
||||
t.Fatalf("snapshot kinds wrong: %#v", snap)
|
||||
}
|
||||
// IDs strictly increase.
|
||||
if !(snap[0].id < snap[1].id && snap[1].id < snap[2].id) {
|
||||
t.Fatalf("ids not increasing: %#v", snap)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToastStackCapDropsOldest(t *testing.T) {
|
||||
var s toastStack
|
||||
for i := 0; i < toastStackCap+3; i++ {
|
||||
s.push(toastInfo, "msg")
|
||||
}
|
||||
snap := s.snapshot()
|
||||
if len(snap) != toastStackCap {
|
||||
t.Fatalf("len = %d, want %d", len(snap), toastStackCap)
|
||||
}
|
||||
// The earliest IDs should have been dropped, leaving the highest
|
||||
// toastStackCap IDs.
|
||||
for i := 1; i < len(snap); i++ {
|
||||
if snap[i].id <= snap[i-1].id {
|
||||
t.Fatalf("ordering broken after cap: %#v", snap)
|
||||
}
|
||||
}
|
||||
// First retained id should be 4 (1,2,3 dropped; cap=5 leaves 4..8).
|
||||
want := uint64(toastStackCap + 3 - toastStackCap + 1)
|
||||
if snap[0].id != want {
|
||||
t.Fatalf("first retained id = %d, want %d", snap[0].id, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToastStackDismissTop(t *testing.T) {
|
||||
var s toastStack
|
||||
if s.dismissTop() {
|
||||
t.Fatalf("dismissTop on empty stack returned true")
|
||||
}
|
||||
s.push(toastInfo, "a")
|
||||
s.push(toastError, "b")
|
||||
if !s.dismissTop() {
|
||||
t.Fatalf("dismissTop returned false with items present")
|
||||
}
|
||||
snap := s.snapshot()
|
||||
if len(snap) != 1 || snap[0].text != "a" {
|
||||
t.Fatalf("after dismissTop: %#v", snap)
|
||||
}
|
||||
if !s.dismissTop() {
|
||||
t.Fatalf("dismissTop on last item returned false")
|
||||
}
|
||||
if s.length() != 0 {
|
||||
t.Fatalf("length after final dismiss = %d, want 0", s.length())
|
||||
}
|
||||
}
|
||||
|
||||
func TestToastStackClear(t *testing.T) {
|
||||
var s toastStack
|
||||
if s.clear() {
|
||||
t.Fatalf("clear on empty returned true")
|
||||
}
|
||||
s.push(toastInfo, "a")
|
||||
s.push(toastError, "b")
|
||||
s.push(toastAttention, "c")
|
||||
if !s.clear() {
|
||||
t.Fatalf("clear returned false with items present")
|
||||
}
|
||||
if s.length() != 0 {
|
||||
t.Fatalf("length after clear = %d, want 0", s.length())
|
||||
}
|
||||
if snap := s.snapshot(); snap != nil {
|
||||
t.Fatalf("snapshot after clear = %#v, want nil", snap)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToastStackSnapshotIsCopy(t *testing.T) {
|
||||
var s toastStack
|
||||
s.push(toastInfo, "a")
|
||||
snap := s.snapshot()
|
||||
snap[0].text = "mutated"
|
||||
again := s.snapshot()
|
||||
if again[0].text != "a" {
|
||||
t.Fatalf("snapshot is not an independent copy: %#v", again)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapToastBodyFixedHeight(t *testing.T) {
|
||||
got := wrapToastBody("short", 20)
|
||||
if len(got) != toastContentRows {
|
||||
t.Fatalf("len = %d, want %d", len(got), toastContentRows)
|
||||
}
|
||||
if got[0] != "short" {
|
||||
t.Fatalf("line 0 = %q, want \"short\"", got[0])
|
||||
}
|
||||
if got[1] != "" || got[2] != "" {
|
||||
t.Fatalf("trailing pads not empty: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapToastBodyWrapsOnWordBoundary(t *testing.T) {
|
||||
got := wrapToastBody("the quick brown fox jumps over", 10)
|
||||
// Expect greedy fill: "the quick" (9), "brown fox" (9), "jumps over" (10).
|
||||
want := []string{"the quick", "brown fox", "jumps over"}
|
||||
for i, w := range want {
|
||||
if got[i] != w {
|
||||
t.Fatalf("line %d = %q, want %q (full=%#v)", i, got[i], w, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapToastBodyEllipsizesOverflow(t *testing.T) {
|
||||
got := wrapToastBody("alpha beta gamma delta epsilon zeta eta theta", 6)
|
||||
if len(got) != toastContentRows {
|
||||
t.Fatalf("len = %d, want %d", len(got), toastContentRows)
|
||||
}
|
||||
last := got[toastContentRows-1]
|
||||
if !strings.HasSuffix(last, "…") {
|
||||
t.Fatalf("overflow should ellipsize last line, got %q (full=%#v)", last, got)
|
||||
}
|
||||
if visibleLen(last) > 6 {
|
||||
t.Fatalf("last line %q exceeds width 6", last)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapToastBodyBreaksOverlongWord(t *testing.T) {
|
||||
got := wrapToastBody("supercalifragilistic", 6)
|
||||
if got[0] != "superc" {
|
||||
t.Fatalf("line 0 = %q, want \"superc\"", got[0])
|
||||
}
|
||||
if got[1] != "alifra" {
|
||||
t.Fatalf("line 1 = %q, want \"alifra\"", got[1])
|
||||
}
|
||||
// Third line should hold the rest (possibly ellipsized).
|
||||
if got[2] == "" {
|
||||
t.Fatalf("line 2 unexpectedly empty: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapToastBodyEmptyInput(t *testing.T) {
|
||||
got := wrapToastBody("", 20)
|
||||
for i, l := range got {
|
||||
if l != "" {
|
||||
t.Fatalf("line %d = %q, want \"\"", i, l)
|
||||
}
|
||||
}
|
||||
}
|
||||
63
internal/app/token.go
Normal file
63
internal/app/token.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ClientTokenPath() (string, error) {
|
||||
base := os.Getenv("XDG_DATA_HOME")
|
||||
if base == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
base = filepath.Join(home, ".local", "share")
|
||||
}
|
||||
return filepath.Join(base, "patterm", "clients", "token"), nil
|
||||
}
|
||||
|
||||
func LoadClientToken() (string, error) {
|
||||
path, err := ClientTokenPath()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSpace(string(b)), nil
|
||||
}
|
||||
|
||||
func LoadOrCreateClientToken() (string, error) {
|
||||
if token, err := LoadClientToken(); err == nil && token != "" {
|
||||
return token, nil
|
||||
}
|
||||
token, err := generateClientToken()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
path, err := ClientTokenPath()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(token+"\n"), 0o600); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func generateClientToken() (string, error) {
|
||||
var b [32]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
return "", fmt.Errorf("token: random: %w", err)
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(b[:]), nil
|
||||
}
|
||||
@@ -23,9 +23,9 @@ func TestRestartRestoresUserCommandProcess(t *testing.T) {
|
||||
}
|
||||
|
||||
sc := &Scenario{
|
||||
Name: "restart_persist",
|
||||
Cols: 120,
|
||||
Rows: 40,
|
||||
Name: "restart_persist",
|
||||
Cols: 120,
|
||||
Rows: 40,
|
||||
Trust: []string{"persist-target"},
|
||||
Presets: ScenarioPresets{
|
||||
Processes: []ScenarioPreset{{
|
||||
@@ -143,7 +143,7 @@ func openSession(t *testing.T, env *testEnv, childEnv []string) *Session {
|
||||
if err != nil {
|
||||
t.Fatalf("vt emulator: %v", err)
|
||||
}
|
||||
p, err := pkgpty.Start([]string{env.PattermBin, "--project", env.ProjectDir}, childEnv, env.Cols, env.Rows)
|
||||
p, err := pkgpty.Start([]string{env.PattermBin, "--in-process", "--project", env.ProjectDir}, childEnv, "", env.Cols, env.Rows)
|
||||
if err != nil {
|
||||
_ = em.Close()
|
||||
t.Fatalf("pty start: %v", err)
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "error_flash_preserves_focused_pane",
|
||||
"presets": {
|
||||
"processes": [
|
||||
{
|
||||
"name": "steady",
|
||||
"argv": ["sh", "-lc", "printf 'STEADY READY\\n'; sleep 5"]
|
||||
}
|
||||
]
|
||||
},
|
||||
"trust": ["steady"],
|
||||
"steps": [
|
||||
{
|
||||
"type": "mcp_call",
|
||||
"method": "spawn_process",
|
||||
"params": {"kind": "command", "preset": "steady", "name": "steady"},
|
||||
"save_as": "proc"
|
||||
},
|
||||
{ "type": "wait_text", "contains": "STEADY READY", "timeout_ms": 5000 },
|
||||
{ "type": "send_chord", "chord": "ctrl-k" },
|
||||
{ "type": "send_text", "text": "Open Settings" },
|
||||
{ "type": "send_chord", "chord": "enter" },
|
||||
{ "type": "send_chord", "chord": "enter" },
|
||||
{ "type": "send_chord", "chord": "ctrl-n" },
|
||||
{ "type": "send_chord", "chord": "ctrl-n" },
|
||||
{ "type": "send_chord", "chord": "ctrl-n" },
|
||||
{ "type": "send_chord", "chord": "ctrl-n" },
|
||||
{ "type": "send_chord", "chord": "ctrl-n" },
|
||||
{ "type": "send_chord", "chord": "ctrl-n" },
|
||||
{ "type": "send_chord", "chord": "ctrl-n" },
|
||||
{ "type": "send_chord", "chord": "enter" },
|
||||
{ "type": "wait_text", "contains": "no active top-level agent to summarize", "timeout_ms": 5000 },
|
||||
{ "type": "wait_text", "contains": "STEADY READY", "timeout_ms": 5000 },
|
||||
{ "type": "assert_contains", "contains": "STEADY READY" },
|
||||
{ "type": "assert_not_contains", "contains": "Press Ctrl-K to spawn an agent or process" }
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "idle_screen_permission_prompt",
|
||||
"presets": {
|
||||
"processes": [
|
||||
{
|
||||
"name": "screen-permission",
|
||||
"argv": [
|
||||
"sh",
|
||||
"-lc",
|
||||
"printf '\\033[2J\\033[HCalling patterm...\\n\\nTool use\\n\\nDo you want to proceed?\\n 1. Yes\\n'; i=0; while [ $i -lt 300 ]; do printf '\\033[HCalling patterm... %03d' $i; i=$((i+1)); done; sleep 60"
|
||||
],
|
||||
"idle_detection": {
|
||||
"strategy": "output_activity",
|
||||
"idle_threshold_ms": 500,
|
||||
"permission_patterns": ["Do you want to proceed\\?"]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"trust": ["screen-permission"],
|
||||
"steps": [
|
||||
{
|
||||
"type": "mcp_call",
|
||||
"method": "spawn_process",
|
||||
"params": {"kind": "command", "preset": "screen-permission", "name": "screen-permission"},
|
||||
"save_as": "proc"
|
||||
},
|
||||
{
|
||||
"type": "wait_until_mcp",
|
||||
"method": "get_process_status",
|
||||
"params": {"process_id": "{{proc.process_id}}"},
|
||||
"path": "idle_state",
|
||||
"equals": "permission",
|
||||
"timeout_ms": 4000
|
||||
}
|
||||
]
|
||||
}
|
||||
32
internal/harness/scenarios/restart_process_keeps_chrome.json
Normal file
32
internal/harness/scenarios/restart_process_keeps_chrome.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "restart_process_keeps_chrome",
|
||||
"cols": 120,
|
||||
"rows": 40,
|
||||
"scripts": [
|
||||
{
|
||||
"name": "slow-restart",
|
||||
"body": "#!/bin/sh\ncount_file=\"$XDG_RUNTIME_DIR/slow-restart-count\"\nif [ -f \"$count_file\" ]; then\n n=$(cat \"$count_file\")\nelse\n n=0\nfi\nn=$((n + 1))\nprintf '%s\\n' \"$n\" > \"$count_file\"\nprintf 'SLOW READY %s\\n' \"$n\"\ntrap 'sleep 3; exit 0' TERM\nwhile true; do sleep 1; done\n"
|
||||
}
|
||||
],
|
||||
"steps": [
|
||||
{
|
||||
"type": "mcp_call",
|
||||
"method": "spawn_process",
|
||||
"params": { "kind": "command", "argv": ["slow-restart"], "name": "slow-restart" },
|
||||
"save_as": "spawned"
|
||||
},
|
||||
{
|
||||
"type": "mcp_call",
|
||||
"method": "select_process",
|
||||
"params": { "process_id": "{{spawned.process_id}}" }
|
||||
},
|
||||
{ "type": "wait_text", "contains": "SLOW READY 1", "timeout_ms": 5000 },
|
||||
{ "type": "wait_stable", "timeout_ms": 2000 },
|
||||
{ "type": "assert_contains", "contains": "Processes" },
|
||||
{ "type": "send_text", "text": "\u000brestart\r" },
|
||||
{ "type": "wait_stable", "timeout_ms": 2000 },
|
||||
{ "type": "assert_contains", "contains": "Processes" },
|
||||
{ "type": "assert_contains", "contains": "slow-restart" },
|
||||
{ "type": "wait_text", "contains": "SLOW READY 2", "timeout_ms": 7000 }
|
||||
]
|
||||
}
|
||||
32
internal/harness/scenarios/toast_dismiss.json
Normal file
32
internal/harness/scenarios/toast_dismiss.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "toast_dismiss",
|
||||
"presets": {
|
||||
"processes": [
|
||||
{
|
||||
"name": "steady",
|
||||
"argv": ["sh", "-lc", "printf 'STEADY READY\\n'; sleep 30"]
|
||||
}
|
||||
]
|
||||
},
|
||||
"trust": ["steady"],
|
||||
"steps": [
|
||||
{
|
||||
"type": "mcp_call",
|
||||
"method": "spawn_process",
|
||||
"params": {"kind": "command", "preset": "steady", "name": "steady"},
|
||||
"save_as": "proc"
|
||||
},
|
||||
{ "type": "wait_text", "contains": "STEADY READY", "timeout_ms": 5000 },
|
||||
{
|
||||
"type": "mcp_call",
|
||||
"method": "request_human_attention",
|
||||
"params": {"process_id": "{{proc.process_id}}", "reason": "needs eyes on the deploy"}
|
||||
},
|
||||
{ "type": "wait_text", "contains": "needs eyes on the deploy", "timeout_ms": 5000 },
|
||||
{ "type": "assert_contains", "contains": "STEADY READY" },
|
||||
{ "type": "send_chord", "chord": "ctrl-n" },
|
||||
{ "type": "wait_stable", "timeout_ms": 2000 },
|
||||
{ "type": "assert_contains", "contains": "STEADY READY" },
|
||||
{ "type": "assert_not_contains", "contains": "needs eyes on the deploy" }
|
||||
]
|
||||
}
|
||||
@@ -55,7 +55,7 @@ func NewCLI(opts Options) (*Session, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := pkgpty.Start([]string{env.PattermBin, "--project", env.ProjectDir}, childEnv, env.Cols, env.Rows)
|
||||
p, err := pkgpty.Start([]string{env.PattermBin, "--in-process", "--project", env.ProjectDir}, childEnv, "", env.Cols, env.Rows)
|
||||
if err != nil {
|
||||
_ = em.Close()
|
||||
return nil, err
|
||||
|
||||
@@ -96,10 +96,34 @@ func (s *Server) acceptLoop() {
|
||||
// identity token (SPEC §10); we resolve it to a child id and stash that
|
||||
// as the caller for every subsequent tool call.
|
||||
func (s *Server) handleConn(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
var writeMu sync.Mutex
|
||||
var wg sync.WaitGroup
|
||||
defer func() {
|
||||
wg.Wait()
|
||||
_ = conn.Close()
|
||||
}()
|
||||
r := bufio.NewReader(conn)
|
||||
|
||||
var callerID string
|
||||
writeResp := func(resp []byte) bool {
|
||||
if resp == nil {
|
||||
return true
|
||||
}
|
||||
resp = append(resp, '\n')
|
||||
writeMu.Lock()
|
||||
defer writeMu.Unlock()
|
||||
for len(resp) > 0 {
|
||||
n, err := conn.Write(resp)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if n == 0 {
|
||||
return false
|
||||
}
|
||||
resp = resp[n:]
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
greeting, err := r.ReadBytes('\n')
|
||||
if err != nil {
|
||||
@@ -115,24 +139,21 @@ func (s *Server) handleConn(conn net.Conn) {
|
||||
} else {
|
||||
// Treat as a real request from an unknown caller.
|
||||
resp := s.dispatch("", greeting)
|
||||
if resp != nil {
|
||||
resp = append(resp, '\n')
|
||||
if _, werr := conn.Write(resp); werr != nil {
|
||||
return
|
||||
}
|
||||
if !writeResp(resp) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
line, err := r.ReadBytes('\n')
|
||||
if len(line) > 0 {
|
||||
resp := s.dispatch(callerID, line)
|
||||
if resp != nil {
|
||||
resp = append(resp, '\n')
|
||||
if _, werr := conn.Write(resp); werr != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
req := append([]byte(nil), line...)
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
resp := s.dispatch(callerID, req)
|
||||
_ = writeResp(resp)
|
||||
}()
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
@@ -167,6 +188,9 @@ func RunStdioProxy(socket, identity string) error {
|
||||
// "<token>"} + newline. Real protocol handshake is a later
|
||||
// milestone.
|
||||
greeting := map[string]string{"patterm_identity": identity}
|
||||
if key := os.Getenv("PATTERM_PROJECT_KEY"); key != "" {
|
||||
greeting["project_key"] = key
|
||||
}
|
||||
gb, _ := json.Marshal(greeting)
|
||||
gb = append(gb, '\n')
|
||||
if _, err := conn.Write(gb); err != nil {
|
||||
|
||||
190
internal/mcp/mcp_test.go
Normal file
190
internal/mcp/mcp_test.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hjbdev/patterm/internal/scratchpad"
|
||||
)
|
||||
|
||||
func TestHandleConnDispatchesRequestsConcurrently(t *testing.T) {
|
||||
serverConn, clientConn := net.Pipe()
|
||||
t.Cleanup(func() { _ = clientConn.Close() })
|
||||
|
||||
host := &blockingToolHost{
|
||||
waitEntered: make(chan struct{}),
|
||||
waitRelease: make(chan struct{}),
|
||||
}
|
||||
s := &Server{}
|
||||
s.SetHost(host)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
s.handleConn(serverConn)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
reader := bufio.NewReader(clientConn)
|
||||
writeLine(t, clientConn, `{"patterm_identity":"ident"}`)
|
||||
writeLine(t, clientConn, `{"jsonrpc":"2.0","id":1,"method":"wait_for_pattern","params":{"process_id":"p_slow","pattern":"never","timeout_seconds":300}}`)
|
||||
select {
|
||||
case <-host.waitEntered:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("wait_for_pattern did not enter fake host")
|
||||
}
|
||||
|
||||
writeLine(t, clientConn, `{"jsonrpc":"2.0","id":2,"method":"get_process_status","params":{"process_id":"p_fast"}}`)
|
||||
fast := readJSONRPCResponse(t, clientConn, reader, time.Second)
|
||||
if got := string(fast.ID); got != "2" {
|
||||
t.Fatalf("first response id = %s, want 2; response=%s", got, fast.Raw)
|
||||
}
|
||||
if fast.Error != nil {
|
||||
t.Fatalf("fast response returned error: %+v", fast.Error)
|
||||
}
|
||||
|
||||
_ = clientConn.SetReadDeadline(time.Now().Add(50 * time.Millisecond))
|
||||
if line, err := reader.ReadBytes('\n'); err == nil {
|
||||
t.Fatalf("slow response arrived before release: %s", line)
|
||||
}
|
||||
|
||||
close(host.waitRelease)
|
||||
slow := readJSONRPCResponse(t, clientConn, reader, time.Second)
|
||||
if got := string(slow.ID); got != "1" {
|
||||
t.Fatalf("second response id = %s, want 1; response=%s", got, slow.Raw)
|
||||
}
|
||||
if slow.Error != nil {
|
||||
t.Fatalf("slow response returned error: %+v", slow.Error)
|
||||
}
|
||||
|
||||
_ = clientConn.Close()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("handleConn did not exit after client close")
|
||||
}
|
||||
}
|
||||
|
||||
type jsonRPCResponse struct {
|
||||
Raw string
|
||||
ID json.RawMessage `json:"id"`
|
||||
Result map[string]any `json:"result"`
|
||||
Error *jsonRPCErrorShape `json:"error"`
|
||||
}
|
||||
|
||||
type jsonRPCErrorShape struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func writeLine(t *testing.T, conn net.Conn, line string) {
|
||||
t.Helper()
|
||||
_ = conn.SetWriteDeadline(time.Now().Add(time.Second))
|
||||
if _, err := fmt.Fprintln(conn, line); err != nil {
|
||||
t.Fatalf("write %s: %v", line, err)
|
||||
}
|
||||
}
|
||||
|
||||
func readJSONRPCResponse(t *testing.T, conn net.Conn, reader *bufio.Reader, timeout time.Duration) jsonRPCResponse {
|
||||
t.Helper()
|
||||
_ = conn.SetReadDeadline(time.Now().Add(timeout))
|
||||
line, err := reader.ReadBytes('\n')
|
||||
if err != nil {
|
||||
t.Fatalf("read response: %v", err)
|
||||
}
|
||||
var resp jsonRPCResponse
|
||||
resp.Raw = string(line)
|
||||
if err := json.Unmarshal(line, &resp); err != nil {
|
||||
t.Fatalf("parse response %s: %v", line, err)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
type blockingToolHost struct {
|
||||
waitEntered chan struct{}
|
||||
waitRelease chan struct{}
|
||||
waitOnce sync.Once
|
||||
}
|
||||
|
||||
func (h *blockingToolHost) ResolveCallerIdentity(identity string) string { return "caller-" + identity }
|
||||
func (h *blockingToolHost) CallerRole(string) CallerRole { return RoleOrchestrator }
|
||||
func (h *blockingToolHost) SpawnAgent(string, SpawnAgentArgs) (ProcessInfo, error) {
|
||||
return ProcessInfo{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) SpawnProcess(string, SpawnProcessArgs) (ProcessInfo, error) {
|
||||
return ProcessInfo{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) StartProcess(string, string) (ProcessInfo, error) {
|
||||
return ProcessInfo{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) RestartProcess(string, string, syscall.Signal) (ProcessInfo, error) {
|
||||
return ProcessInfo{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) StopProcess(string, string, syscall.Signal) (ProcessInfo, error) {
|
||||
return ProcessInfo{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) CloseProcess(string, string) error { return nil }
|
||||
func (h *blockingToolHost) RenameProcess(string, string, string) error { return nil }
|
||||
func (h *blockingToolHost) SelectProcess(string, string) error { return nil }
|
||||
func (h *blockingToolHost) ListProcesses(string, string) []ProcessInfo { return nil }
|
||||
func (h *blockingToolHost) GetProcessStatus(string, string) (ProcessStatus, error) {
|
||||
return ProcessStatus{ProcessInfo: ProcessInfo{ID: "p_fast", Status: "running"}}, nil
|
||||
}
|
||||
func (h *blockingToolHost) GetProjectStatus(string) (ProjectStatus, error) {
|
||||
return ProjectStatus{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) GetProcessOutput(string, string, string, int64) (ProcessOutput, error) {
|
||||
return ProcessOutput{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) GetProcessRawOutput(string, string, int64) (RawOutput, error) {
|
||||
return RawOutput{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) SearchOutput(string, string, string, string, int) (SearchResult, error) {
|
||||
return SearchResult{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) WaitForPattern(string, string, string, float64, string) (bool, string, error) {
|
||||
h.waitOnce.Do(func() { close(h.waitEntered) })
|
||||
<-h.waitRelease
|
||||
return true, "matched", nil
|
||||
}
|
||||
func (h *blockingToolHost) GetProcessPorts(string, string) ([]PortSighting, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (h *blockingToolHost) SendInput(string, SendInputArgs) (SendInputResult, error) {
|
||||
return SendInputResult{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) SendMessage(string, string, string) error { return nil }
|
||||
func (h *blockingToolHost) RequestHumanAttention(string, string, string) error { return nil }
|
||||
func (h *blockingToolHost) TimerWait(string, float64, string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
func (h *blockingToolHost) TimerSet(string, TimerSetArgs) (TimerHandle, error) {
|
||||
return TimerHandle{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) TimerFireWhenIdleAny(string, TimerFireWhenIdleArgs) (TimerFireWhenIdleResponse, error) {
|
||||
return TimerFireWhenIdleResponse{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) TimerFireWhenIdleAll(string, TimerFireWhenIdleArgs) (TimerFireWhenIdleResponse, error) {
|
||||
return TimerFireWhenIdleResponse{}, nil
|
||||
}
|
||||
func (h *blockingToolHost) TimerCancel(string, string) error { return nil }
|
||||
func (h *blockingToolHost) TimerPause(string, string) error { return nil }
|
||||
func (h *blockingToolHost) TimerResume(string, string) error { return nil }
|
||||
func (h *blockingToolHost) TimerList(string) ([]TimerInfo, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (h *blockingToolHost) ScratchpadList(string) ([]scratchpad.Entry, error) { return nil, nil }
|
||||
func (h *blockingToolHost) ScratchpadRead(string, string) (string, string, error) {
|
||||
return "", "", nil
|
||||
}
|
||||
func (h *blockingToolHost) ScratchpadWrite(string, string, string, string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
func (h *blockingToolHost) ScratchpadAppend(string, string, string) error { return nil }
|
||||
func (h *blockingToolHost) ScratchpadDelete(string, string) error { return nil }
|
||||
func (h *blockingToolHost) WhoAmI(string) WhoAmI { return WhoAmI{} }
|
||||
func (h *blockingToolHost) Help(string, string) HelpResponse { return HelpResponse{} }
|
||||
@@ -358,6 +358,13 @@ func toolCatalog() []toolDescriptor {
|
||||
"content": stringProp("Text to append."),
|
||||
}, []string{"name", "content"}),
|
||||
},
|
||||
{
|
||||
Name: "scratchpad_delete",
|
||||
Description: "Delete a scratchpad entry.",
|
||||
InputSchema: objectSchema(map[string]any{
|
||||
"name": stringProp("Scratchpad name."),
|
||||
}, []string{"name"}),
|
||||
},
|
||||
{
|
||||
Name: "whoami",
|
||||
Description: "Return the caller's identity, role, parent, project metadata, and available tools.",
|
||||
|
||||
@@ -97,10 +97,11 @@ type ToolHost interface {
|
||||
TimerList(callerID string) ([]TimerInfo, error)
|
||||
|
||||
// Scratchpads.
|
||||
ScratchpadList() ([]scratchpad.Entry, error)
|
||||
ScratchpadRead(name string) (content string, revision string, err error)
|
||||
ScratchpadWrite(name, content, expectedRevision string) (revision string, err error)
|
||||
ScratchpadAppend(name, content string) error
|
||||
ScratchpadList(callerID string) ([]scratchpad.Entry, error)
|
||||
ScratchpadRead(callerID, name string) (content string, revision string, err error)
|
||||
ScratchpadWrite(callerID, name, content, expectedRevision string) (revision string, err error)
|
||||
ScratchpadAppend(callerID, name, content string) error
|
||||
ScratchpadDelete(callerID, name string) error
|
||||
|
||||
// Meta.
|
||||
WhoAmI(callerID string) WhoAmI
|
||||
@@ -244,8 +245,8 @@ type TimerInfo struct {
|
||||
ID string `json:"timer_id"`
|
||||
Label string `json:"label,omitempty"`
|
||||
Body string `json:"body,omitempty"`
|
||||
Kind string `json:"kind"` // "delay" | "idle_any" | "idle_all"
|
||||
Status string `json:"status"` // "pending" | "paused"
|
||||
Kind string `json:"kind"` // "delay" | "idle_any" | "idle_all"
|
||||
Status string `json:"status"` // "pending" | "paused"
|
||||
OwnerID string `json:"owner_process_id"`
|
||||
WatchedIDs []string `json:"watched,omitempty"`
|
||||
FiresAtUnixMS int64 `json:"fires_at_unix_ms,omitempty"`
|
||||
@@ -723,7 +724,7 @@ func callTool(h ToolHost, callerID, method string, params json.RawMessage) (any,
|
||||
return ts, 0, "", nil
|
||||
|
||||
case "scratchpad_list":
|
||||
entries, err := h.ScratchpadList()
|
||||
entries, err := h.ScratchpadList(callerID)
|
||||
if err != nil {
|
||||
return nil, codeInternal, err.Error(), nil
|
||||
}
|
||||
@@ -736,7 +737,7 @@ func callTool(h ToolHost, callerID, method string, params json.RawMessage) (any,
|
||||
if err := unmarshalParams(params, &p); err != nil {
|
||||
return nil, codeInvalidParams, err.Error(), nil
|
||||
}
|
||||
content, rev, err := h.ScratchpadRead(p.Name)
|
||||
content, rev, err := h.ScratchpadRead(callerID, p.Name)
|
||||
if err != nil {
|
||||
return nil, codeInternal, err.Error(), nil
|
||||
}
|
||||
@@ -751,7 +752,7 @@ func callTool(h ToolHost, callerID, method string, params json.RawMessage) (any,
|
||||
if err := unmarshalParams(params, &p); err != nil {
|
||||
return nil, codeInvalidParams, err.Error(), nil
|
||||
}
|
||||
rev, err := h.ScratchpadWrite(p.Name, p.Content, p.ExpectedRevision)
|
||||
rev, err := h.ScratchpadWrite(callerID, p.Name, p.Content, p.ExpectedRevision)
|
||||
if err != nil {
|
||||
// Optimistic-concurrency miss returns ok:false + current_revision
|
||||
// rather than a JSON-RPC error so callers can re-read + merge.
|
||||
@@ -771,7 +772,19 @@ func callTool(h ToolHost, callerID, method string, params json.RawMessage) (any,
|
||||
if err := unmarshalParams(params, &p); err != nil {
|
||||
return nil, codeInvalidParams, err.Error(), nil
|
||||
}
|
||||
if err := h.ScratchpadAppend(p.Name, p.Content); err != nil {
|
||||
if err := h.ScratchpadAppend(callerID, p.Name, p.Content); err != nil {
|
||||
return nil, codeInternal, err.Error(), nil
|
||||
}
|
||||
return map[string]any{"ok": true}, 0, "", nil
|
||||
|
||||
case "scratchpad_delete":
|
||||
var p struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := unmarshalParams(params, &p); err != nil {
|
||||
return nil, codeInvalidParams, err.Error(), nil
|
||||
}
|
||||
if err := h.ScratchpadDelete(callerID, p.Name); err != nil {
|
||||
return nil, codeInternal, err.Error(), nil
|
||||
}
|
||||
return map[string]any{"ok": true}, 0, "", nil
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package preset
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -35,15 +36,16 @@ type Preset struct {
|
||||
Argv []string `json:"argv"`
|
||||
Env map[string]string `json:"env,omitempty"`
|
||||
WorkingDir string `json:"working_dir,omitempty"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
|
||||
// Process-only.
|
||||
Shell bool `json:"shell,omitempty"`
|
||||
|
||||
// Agent-only. SPEC §10.
|
||||
MCPInjection *MCPInjection `json:"mcp_injection,omitempty"`
|
||||
ReadySignal *ReadySignal `json:"ready_signal,omitempty"`
|
||||
ChromeTrimHints []string `json:"chrome_trim_hints,omitempty"`
|
||||
IdleDetection *IdleDetection `json:"idle_detection,omitempty"`
|
||||
MCPInjection *MCPInjection `json:"mcp_injection,omitempty"`
|
||||
ReadySignal *ReadySignal `json:"ready_signal,omitempty"`
|
||||
ChromeTrimHints []string `json:"chrome_trim_hints,omitempty"`
|
||||
IdleDetection *IdleDetection `json:"idle_detection,omitempty"`
|
||||
}
|
||||
|
||||
// IdleDetection configures steady-state idle classification for an
|
||||
@@ -119,28 +121,22 @@ type Set struct {
|
||||
Processes []*Preset
|
||||
}
|
||||
|
||||
// Load scans the standard locations under $XDG_CONFIG_HOME/patterm/
|
||||
// presets/{agents,processes}/*.json. Unknown files are skipped with a
|
||||
// warning to stderr; the spec is forgiving here.
|
||||
// Load returns the built-in presets plus user overlays from
|
||||
// $XDG_CONFIG_HOME/patterm/presets/{agents,processes}/*.json. Startup
|
||||
// does not write default files; user files only override or extend the
|
||||
// in-memory defaults. A user overlay with {"disabled": true} hides a
|
||||
// built-in preset of the same name.
|
||||
func Load() (Set, error) {
|
||||
base, err := ConfigDir()
|
||||
if err != nil {
|
||||
return Set{}, err
|
||||
}
|
||||
if err := os.MkdirAll(base, 0o700); err != nil {
|
||||
return Set{}, fmt.Errorf("preset: mkdir %s: %w", base, err)
|
||||
}
|
||||
|
||||
// Make sure the default-preset files exist on first run. Idempotent.
|
||||
if err := ensureDefaults(base); err != nil {
|
||||
return Set{}, err
|
||||
}
|
||||
|
||||
agents, err := loadDir(filepath.Join(base, "presets", "agents"), KindAgent)
|
||||
agents, err := loadWithDefaults(filepath.Join(base, "presets", "agents"), KindAgent, defaultAgentPresets())
|
||||
if err != nil {
|
||||
return Set{}, err
|
||||
}
|
||||
procs, err := loadDir(filepath.Join(base, "presets", "processes"), KindCommand)
|
||||
procs, err := loadWithDefaults(filepath.Join(base, "presets", "processes"), KindCommand, nil)
|
||||
if err != nil {
|
||||
return Set{}, err
|
||||
}
|
||||
@@ -160,51 +156,154 @@ func ConfigDir() (string, error) {
|
||||
return filepath.Join(home, ".config", "patterm"), nil
|
||||
}
|
||||
|
||||
func loadDir(dir string, kind Kind) ([]*Preset, error) {
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return nil, fmt.Errorf("preset: mkdir %s: %w", dir, err)
|
||||
func loadWithDefaults(dir string, kind Kind, defaults []*Preset) ([]*Preset, error) {
|
||||
byName := make(map[string]*Preset, len(defaults))
|
||||
for _, p := range defaults {
|
||||
cp := clonePreset(p)
|
||||
cp.Kind = kind
|
||||
byName[cp.Name] = cp
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return sortedPresets(byName), nil
|
||||
}
|
||||
return nil, fmt.Errorf("preset: read %s: %w", dir, err)
|
||||
}
|
||||
var out []*Preset
|
||||
for _, e := range entries {
|
||||
if e.IsDir() || !strings.HasSuffix(e.Name(), ".json") {
|
||||
continue
|
||||
}
|
||||
path := filepath.Join(dir, e.Name())
|
||||
p, err := loadFile(path, kind)
|
||||
p, err := loadFileOverlay(path, kind, byName)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "patterm: preset %s: %v\n", path, err)
|
||||
continue
|
||||
}
|
||||
if p.Disabled {
|
||||
delete(byName, p.Name)
|
||||
continue
|
||||
}
|
||||
byName[p.Name] = p
|
||||
}
|
||||
return sortedPresets(byName), nil
|
||||
}
|
||||
|
||||
func sortedPresets(byName map[string]*Preset) []*Preset {
|
||||
out := make([]*Preset, 0, len(byName))
|
||||
for _, p := range byName {
|
||||
out = append(out, p)
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
||||
return out, nil
|
||||
return out
|
||||
}
|
||||
|
||||
func loadFile(path string, kind Kind) (*Preset, error) {
|
||||
func loadFileOverlay(path string, kind Kind, defaults map[string]*Preset) (*Preset, error) {
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var header struct {
|
||||
Name string `json:"name"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
}
|
||||
if err := json.Unmarshal(b, &header); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if header.Name == "" {
|
||||
return nil, errors.New("missing 'name'")
|
||||
}
|
||||
if def := defaults[header.Name]; def != nil {
|
||||
p, err := mergePreset(def, b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.Path = path
|
||||
p.Kind = kind
|
||||
return p, validatePreset(p)
|
||||
}
|
||||
var p Preset
|
||||
if err := json.Unmarshal(b, &p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.Path = path
|
||||
p.Kind = kind
|
||||
return &p, validatePreset(&p)
|
||||
}
|
||||
|
||||
func validatePreset(p *Preset) error {
|
||||
if p.Name == "" {
|
||||
return errors.New("missing 'name'")
|
||||
}
|
||||
if p.Disabled {
|
||||
return nil
|
||||
}
|
||||
if len(p.Argv) == 0 && !p.Shell {
|
||||
return errors.New("missing 'argv'")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mergePreset(def *Preset, overlay []byte) (*Preset, error) {
|
||||
base, err := presetMap(def)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var over map[string]any
|
||||
dec := json.NewDecoder(bytes.NewReader(overlay))
|
||||
dec.UseNumber()
|
||||
if err := dec.Decode(&over); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deepMerge(base, over)
|
||||
b, err := json.Marshal(base)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var p Preset
|
||||
if err := json.Unmarshal(b, &p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if p.Name == "" {
|
||||
return nil, errors.New("missing 'name'")
|
||||
}
|
||||
if len(p.Argv) == 0 && !p.Shell {
|
||||
return nil, errors.New("missing 'argv'")
|
||||
}
|
||||
p.Path = path
|
||||
p.Kind = kind
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
func presetMap(p *Preset) (map[string]any, error) {
|
||||
b, err := json.Marshal(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var m map[string]any
|
||||
dec := json.NewDecoder(bytes.NewReader(b))
|
||||
dec.UseNumber()
|
||||
if err := dec.Decode(&m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func deepMerge(dst, src map[string]any) {
|
||||
for k, v := range src {
|
||||
if sm, ok := v.(map[string]any); ok {
|
||||
if dm, ok := dst[k].(map[string]any); ok {
|
||||
deepMerge(dm, sm)
|
||||
continue
|
||||
}
|
||||
}
|
||||
dst[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
func clonePreset(p *Preset) *Preset {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
b, _ := json.Marshal(p)
|
||||
var out Preset
|
||||
_ = json.Unmarshal(b, &out)
|
||||
return &out
|
||||
}
|
||||
|
||||
// ResolvedArgv returns the argv to actually exec, handling the
|
||||
// process-preset "shell: true" case (SPEC §10).
|
||||
func (p *Preset) ResolvedArgv() []string {
|
||||
@@ -214,17 +313,9 @@ func (p *Preset) ResolvedArgv() []string {
|
||||
return p.Argv
|
||||
}
|
||||
|
||||
// ensureDefaults writes default agent presets (claude/codex/opencode)
|
||||
// and a sample process preset on first run. Never overwrites existing
|
||||
// user files.
|
||||
func ensureDefaults(base string) error {
|
||||
defaults := []struct {
|
||||
rel string
|
||||
body string
|
||||
}{
|
||||
{
|
||||
"presets/agents/claude.json",
|
||||
`{
|
||||
func defaultAgentPresets() []*Preset {
|
||||
bodies := []string{
|
||||
`{
|
||||
"name": "claude",
|
||||
"argv": ["claude"],
|
||||
"mcp_injection": { "kind": "flag", "flag": "--mcp-config" },
|
||||
@@ -249,10 +340,7 @@ func ensureDefaults(base string) error {
|
||||
]
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
"presets/agents/codex.json",
|
||||
`{
|
||||
`{
|
||||
"name": "codex",
|
||||
"argv": ["codex"],
|
||||
"mcp_injection": {
|
||||
@@ -264,7 +352,10 @@ func ensureDefaults(base string) error {
|
||||
"ready_signal": { "idle_ms": 1000 },
|
||||
"idle_detection": {
|
||||
"strategy": "osc_title_stability",
|
||||
"idle_threshold_ms": 2000
|
||||
"idle_threshold_ms": 2000,
|
||||
"thinking_patterns": [
|
||||
"(?i)esc to interrupt"
|
||||
]
|
||||
},
|
||||
"chrome_trim_hints": [
|
||||
"^OpenAI Codex",
|
||||
@@ -275,10 +366,7 @@ func ensureDefaults(base string) error {
|
||||
]
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
"presets/agents/opencode.json",
|
||||
`{
|
||||
`{
|
||||
"name": "opencode",
|
||||
"argv": ["opencode"],
|
||||
"mcp_injection": {
|
||||
@@ -301,19 +389,15 @@ func ensureDefaults(base string) error {
|
||||
]
|
||||
}
|
||||
`,
|
||||
},
|
||||
}
|
||||
for _, d := range defaults {
|
||||
full := filepath.Join(base, d.rel)
|
||||
if _, err := os.Stat(full); err == nil {
|
||||
continue
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(full), 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(full, []byte(d.body), 0o600); err != nil {
|
||||
return err
|
||||
out := make([]*Preset, 0, len(bodies))
|
||||
for _, body := range bodies {
|
||||
var p Preset
|
||||
if err := json.Unmarshal([]byte(body), &p); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
p.Kind = KindAgent
|
||||
out = append(out, &p)
|
||||
}
|
||||
return nil
|
||||
return out
|
||||
}
|
||||
|
||||
131
internal/preset/preset_test.go
Normal file
131
internal/preset/preset_test.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package preset
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadUsesBuiltInDefaultsWithoutWritingConfig(t *testing.T) {
|
||||
configHome := filepath.Join(t.TempDir(), "config")
|
||||
t.Setenv("XDG_CONFIG_HOME", configHome)
|
||||
|
||||
set, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(configHome, "patterm")); !os.IsNotExist(err) {
|
||||
t.Fatalf("Load created config dir or unexpected stat error: %v", err)
|
||||
}
|
||||
if len(set.Agents) != 3 {
|
||||
t.Fatalf("agents len = %d, want 3", len(set.Agents))
|
||||
}
|
||||
claude := presetByName(set.Agents, "claude")
|
||||
if claude == nil {
|
||||
t.Fatal("missing built-in claude preset")
|
||||
}
|
||||
if claude.IdleDetection == nil || len(claude.IdleDetection.PermissionPatterns) == 0 {
|
||||
t.Fatalf("built-in claude missing permission patterns: %+v", claude.IdleDetection)
|
||||
}
|
||||
codex := presetByName(set.Agents, "codex")
|
||||
if codex == nil {
|
||||
t.Fatal("missing built-in codex preset")
|
||||
}
|
||||
if codex.IdleDetection == nil || len(codex.IdleDetection.ThinkingPatterns) == 0 {
|
||||
t.Fatalf("built-in codex missing thinking patterns: %+v", codex.IdleDetection)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadMergesUserOverlayIntoBuiltInPreset(t *testing.T) {
|
||||
configHome := t.TempDir()
|
||||
t.Setenv("XDG_CONFIG_HOME", configHome)
|
||||
dir := filepath.Join(configHome, "patterm", "presets", "agents")
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "claude.json"), `{
|
||||
"name": "claude",
|
||||
"argv": ["claude", "--model", "sonnet"],
|
||||
"idle_detection": { "idle_threshold_ms": 3500 }
|
||||
}`)
|
||||
|
||||
set, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
claude := presetByName(set.Agents, "claude")
|
||||
if claude == nil {
|
||||
t.Fatal("missing claude preset")
|
||||
}
|
||||
if got := claude.Argv; len(got) != 3 || got[0] != "claude" || got[2] != "sonnet" {
|
||||
t.Fatalf("argv = %#v", got)
|
||||
}
|
||||
if claude.IdleDetection.IdleThresholdMS != 3500 {
|
||||
t.Fatalf("idle threshold = %d", claude.IdleDetection.IdleThresholdMS)
|
||||
}
|
||||
if len(claude.IdleDetection.PermissionPatterns) == 0 {
|
||||
t.Fatalf("permission patterns were not inherited: %+v", claude.IdleDetection)
|
||||
}
|
||||
if claude.MCPInjection == nil || claude.MCPInjection.Kind != "flag" {
|
||||
t.Fatalf("mcp injection was not inherited: %+v", claude.MCPInjection)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadCanDisableBuiltInPreset(t *testing.T) {
|
||||
configHome := t.TempDir()
|
||||
t.Setenv("XDG_CONFIG_HOME", configHome)
|
||||
dir := filepath.Join(configHome, "patterm", "presets", "agents")
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "opencode.json"), `{"name":"opencode","disabled":true}`)
|
||||
|
||||
set, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if presetByName(set.Agents, "opencode") != nil {
|
||||
t.Fatal("opencode preset was not disabled")
|
||||
}
|
||||
if presetByName(set.Agents, "claude") == nil || presetByName(set.Agents, "codex") == nil {
|
||||
t.Fatalf("other built-ins missing: %+v", set.Agents)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadAddsCustomUserPreset(t *testing.T) {
|
||||
configHome := t.TempDir()
|
||||
t.Setenv("XDG_CONFIG_HOME", configHome)
|
||||
dir := filepath.Join(configHome, "patterm", "presets", "processes")
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeFile(t, filepath.Join(dir, "test.json"), `{"name":"test","argv":["go","test","./..."]}`)
|
||||
|
||||
set, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
proc := presetByName(set.Processes, "test")
|
||||
if proc == nil {
|
||||
t.Fatal("missing custom process preset")
|
||||
}
|
||||
if proc.Kind != KindCommand {
|
||||
t.Fatalf("kind = %q", proc.Kind)
|
||||
}
|
||||
}
|
||||
|
||||
func presetByName(ps []*Preset, name string) *Preset {
|
||||
for _, p := range ps {
|
||||
if p.Name == name {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeFile(t *testing.T, path, body string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
176
internal/protocol/frame.go
Normal file
176
internal/protocol/frame.go
Normal file
@@ -0,0 +1,176 @@
|
||||
// Package protocol defines the daemon/client control frames shared by
|
||||
// transports. It intentionally contains data shapes only; app behavior stays
|
||||
// in internal/app until the headless daemon split is complete.
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// FrameType identifies one protocol message kind.
|
||||
type FrameType string
|
||||
|
||||
const (
|
||||
FrameHello FrameType = "hello"
|
||||
FrameAuthChallenge FrameType = "auth_challenge"
|
||||
FrameAuthOK FrameType = "auth_ok"
|
||||
FrameAttach FrameType = "attach"
|
||||
FrameDetach FrameType = "detach"
|
||||
FrameProjectList FrameType = "project_list"
|
||||
FrameChrome FrameType = "chrome"
|
||||
FramePaneSnapshot FrameType = "pane_snapshot"
|
||||
FramePaneChunk FrameType = "pane_chunk"
|
||||
FrameLifecycle FrameType = "lifecycle"
|
||||
FrameAttention FrameType = "attention"
|
||||
FrameTrustPrompt FrameType = "trust_prompt"
|
||||
FrameInput FrameType = "input"
|
||||
FrameFocus FrameType = "focus"
|
||||
FrameSwitchProject FrameType = "switch_project"
|
||||
FrameOpenProject FrameType = "open_project"
|
||||
FramePaletteCommand FrameType = "palette_command"
|
||||
FrameTrustResponse FrameType = "trust_response"
|
||||
FrameResize FrameType = "resize"
|
||||
FrameList FrameType = "list"
|
||||
FrameStop FrameType = "stop"
|
||||
FrameError FrameType = "error"
|
||||
)
|
||||
|
||||
// Frame is the transport envelope. Payload is deliberately raw JSON so
|
||||
// network transports can frame without knowing every message type; loopback
|
||||
// transports may pass the same bytes without JSON re-encoding.
|
||||
type Frame struct {
|
||||
Type FrameType `json:"type"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
Payload json.RawMessage `json:"payload,omitempty"`
|
||||
}
|
||||
|
||||
// NewFrame marshals payload into a protocol frame.
|
||||
func NewFrame[T any](typ FrameType, payload T) (Frame, error) {
|
||||
b, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return Frame{}, fmt.Errorf("protocol: marshal %s: %w", typ, err)
|
||||
}
|
||||
return Frame{Type: typ, Payload: b}, nil
|
||||
}
|
||||
|
||||
// Decode unmarshals f.Payload into v.
|
||||
func Decode[T any](f Frame) (T, error) {
|
||||
var v T
|
||||
if len(f.Payload) == 0 {
|
||||
return v, nil
|
||||
}
|
||||
if err := json.Unmarshal(f.Payload, &v); err != nil {
|
||||
return v, fmt.Errorf("protocol: decode %s: %w", f.Type, err)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
type Hello struct {
|
||||
Version int `json:"version"`
|
||||
DaemonID string `json:"daemon_id,omitempty"`
|
||||
ClientID string `json:"client_id,omitempty"`
|
||||
ProjectKey string `json:"project_key,omitempty"`
|
||||
}
|
||||
|
||||
type Attach struct {
|
||||
Token string `json:"token,omitempty"`
|
||||
ProjectKey string `json:"project_key,omitempty"`
|
||||
ProjectPath string `json:"project_path,omitempty"`
|
||||
TermSize Size `json:"term_size"`
|
||||
}
|
||||
|
||||
type Detach struct {
|
||||
ClientID string `json:"client_id,omitempty"`
|
||||
}
|
||||
|
||||
type Size struct {
|
||||
Cols uint16 `json:"cols"`
|
||||
Rows uint16 `json:"rows"`
|
||||
}
|
||||
|
||||
type Project struct {
|
||||
Key string `json:"key"`
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
LastActive time.Time `json:"last_active,omitempty"`
|
||||
TabCount int `json:"tab_count"`
|
||||
}
|
||||
|
||||
type ProjectList struct {
|
||||
Projects []Project `json:"projects"`
|
||||
}
|
||||
|
||||
type Chrome struct {
|
||||
ProjectKey string `json:"project_key"`
|
||||
Model json.RawMessage `json:"model"`
|
||||
}
|
||||
|
||||
type PaneSnapshot struct {
|
||||
PaneID string `json:"pane_id"`
|
||||
Bytes []byte `json:"bytes"`
|
||||
Size Size `json:"size,omitempty"`
|
||||
DisplayOwner bool `json:"display_owner,omitempty"`
|
||||
}
|
||||
|
||||
type PaneChunk struct {
|
||||
PaneID string `json:"pane_id"`
|
||||
Bytes []byte `json:"bytes"`
|
||||
Size Size `json:"size,omitempty"`
|
||||
DisplayOwner bool `json:"display_owner,omitempty"`
|
||||
}
|
||||
|
||||
type LifecycleKind string
|
||||
|
||||
const (
|
||||
LifecycleSpawned LifecycleKind = "spawned"
|
||||
LifecycleExited LifecycleKind = "exited"
|
||||
LifecycleClosed LifecycleKind = "closed"
|
||||
LifecycleStateChanged LifecycleKind = "state_changed"
|
||||
)
|
||||
|
||||
type Lifecycle struct {
|
||||
Kind LifecycleKind `json:"kind"`
|
||||
ProjectKey string `json:"project_key,omitempty"`
|
||||
ChildID string `json:"child_id,omitempty"`
|
||||
Child json.RawMessage `json:"child,omitempty"`
|
||||
State string `json:"state,omitempty"`
|
||||
}
|
||||
|
||||
type Input struct {
|
||||
PaneID string `json:"pane_id"`
|
||||
Bytes []byte `json:"bytes"`
|
||||
}
|
||||
|
||||
type Focus struct {
|
||||
PaneID string `json:"pane_id,omitempty"`
|
||||
Pad string `json:"pad,omitempty"`
|
||||
}
|
||||
|
||||
type SwitchProject struct {
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
type OpenProject struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
type PaletteCommand struct {
|
||||
Kind string `json:"kind"`
|
||||
Data json.RawMessage `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
type TrustResponse struct {
|
||||
ProcessID string `json:"process_id"`
|
||||
Preset string `json:"preset"`
|
||||
Allow bool `json:"allow"`
|
||||
}
|
||||
|
||||
type Resize struct {
|
||||
Size Size `json:"size"`
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
67
internal/protocol/loopback.go
Normal file
67
internal/protocol/loopback.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
const defaultLoopbackBuffer = 64
|
||||
|
||||
// NewLoopbackPair returns connected in-process transports. Frames cross the
|
||||
// same Send/Recv boundary as network transports, but payload bytes are passed
|
||||
// directly without JSON re-encoding.
|
||||
func NewLoopbackPair() (client Transport, daemon Transport) {
|
||||
c2d := make(chan Frame, defaultLoopbackBuffer)
|
||||
d2c := make(chan Frame, defaultLoopbackBuffer)
|
||||
return &loopbackTransport{send: c2d, recv: d2c}, &loopbackTransport{send: d2c, recv: c2d}
|
||||
}
|
||||
|
||||
type loopbackTransport struct {
|
||||
send chan<- Frame
|
||||
recv <-chan Frame
|
||||
once sync.Once
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func (t *loopbackTransport) init() {
|
||||
if t.done == nil {
|
||||
t.done = make(chan struct{})
|
||||
}
|
||||
}
|
||||
|
||||
func (t *loopbackTransport) Send(f Frame) error {
|
||||
t.init()
|
||||
select {
|
||||
case <-t.done:
|
||||
return ErrTransportClosed
|
||||
case t.send <- cloneFrame(f):
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t *loopbackTransport) Recv() (Frame, error) {
|
||||
t.init()
|
||||
select {
|
||||
case <-t.done:
|
||||
return Frame{}, ErrTransportClosed
|
||||
case f, ok := <-t.recv:
|
||||
if !ok {
|
||||
return Frame{}, ErrTransportClosed
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t *loopbackTransport) Close() error {
|
||||
t.init()
|
||||
t.once.Do(func() {
|
||||
close(t.done)
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func cloneFrame(f Frame) Frame {
|
||||
if len(f.Payload) > 0 {
|
||||
f.Payload = append([]byte(nil), f.Payload...)
|
||||
}
|
||||
return f
|
||||
}
|
||||
51
internal/protocol/loopback_test.go
Normal file
51
internal/protocol/loopback_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package protocol
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLoopbackUsesFramePayload(t *testing.T) {
|
||||
client, daemon := NewLoopbackPair()
|
||||
defer client.Close()
|
||||
defer daemon.Close()
|
||||
|
||||
sent, err := NewFrame(FrameInput, Input{PaneID: "p_123456", Bytes: []byte("hello")})
|
||||
if err != nil {
|
||||
t.Fatalf("NewFrame: %v", err)
|
||||
}
|
||||
if err := client.Send(sent); err != nil {
|
||||
t.Fatalf("Send: %v", err)
|
||||
}
|
||||
got, err := daemon.Recv()
|
||||
if err != nil {
|
||||
t.Fatalf("Recv: %v", err)
|
||||
}
|
||||
if got.Type != FrameInput {
|
||||
t.Fatalf("type = %q, want %q", got.Type, FrameInput)
|
||||
}
|
||||
payload, err := Decode[Input](got)
|
||||
if err != nil {
|
||||
t.Fatalf("Decode: %v", err)
|
||||
}
|
||||
if payload.PaneID != "p_123456" || string(payload.Bytes) != "hello" {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoopbackCopiesPayloadOnSend(t *testing.T) {
|
||||
client, daemon := NewLoopbackPair()
|
||||
defer client.Close()
|
||||
defer daemon.Close()
|
||||
|
||||
f := Frame{Type: FramePaneChunk, Payload: []byte(`{"pane_id":"p","bytes":"aGVsbG8="}`)}
|
||||
if err := client.Send(f); err != nil {
|
||||
t.Fatalf("Send: %v", err)
|
||||
}
|
||||
f.Payload[0] = 'x'
|
||||
|
||||
got, err := daemon.Recv()
|
||||
if err != nil {
|
||||
t.Fatalf("Recv: %v", err)
|
||||
}
|
||||
if got.Payload[0] != '{' {
|
||||
t.Fatalf("payload was retained instead of copied: %q", string(got.Payload))
|
||||
}
|
||||
}
|
||||
80
internal/protocol/transport.go
Normal file
80
internal/protocol/transport.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var ErrTransportClosed = errors.New("protocol: transport closed")
|
||||
|
||||
// Transport carries framed daemon/client protocol messages.
|
||||
type Transport interface {
|
||||
Send(Frame) error
|
||||
Recv() (Frame, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
// ConnTransport is a JSON-lines implementation over a stream connection. Send
|
||||
// is guarded by a mutex so the daemon can push frames from its subscriber pump
|
||||
// and its command handlers concurrently; Close may be called from any goroutine
|
||||
// (e.g. on context cancellation) to unblock a pending Recv.
|
||||
type ConnTransport struct {
|
||||
conn net.Conn
|
||||
r *bufio.Reader
|
||||
wmu sync.Mutex
|
||||
w *bufio.Writer
|
||||
}
|
||||
|
||||
func NewConnTransport(conn net.Conn) *ConnTransport {
|
||||
return &ConnTransport{
|
||||
conn: conn,
|
||||
r: bufio.NewReader(conn),
|
||||
w: bufio.NewWriter(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ConnTransport) Send(f Frame) error {
|
||||
if t == nil || t.conn == nil {
|
||||
return ErrTransportClosed
|
||||
}
|
||||
b, err := json.Marshal(f)
|
||||
if err != nil {
|
||||
return fmt.Errorf("protocol: encode frame: %w", err)
|
||||
}
|
||||
t.wmu.Lock()
|
||||
defer t.wmu.Unlock()
|
||||
if _, err := t.w.Write(append(b, '\n')); err != nil {
|
||||
return err
|
||||
}
|
||||
return t.w.Flush()
|
||||
}
|
||||
|
||||
func (t *ConnTransport) Recv() (Frame, error) {
|
||||
if t == nil || t.conn == nil {
|
||||
return Frame{}, ErrTransportClosed
|
||||
}
|
||||
line, err := t.r.ReadBytes('\n')
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return Frame{}, ErrTransportClosed
|
||||
}
|
||||
return Frame{}, err
|
||||
}
|
||||
var f Frame
|
||||
if err := json.Unmarshal(line, &f); err != nil {
|
||||
return Frame{}, fmt.Errorf("protocol: decode frame: %w", err)
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func (t *ConnTransport) Close() error {
|
||||
if t == nil || t.conn == nil {
|
||||
return nil
|
||||
}
|
||||
return t.conn.Close()
|
||||
}
|
||||
@@ -6,12 +6,22 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
cpty "github.com/creack/pty"
|
||||
)
|
||||
|
||||
// PTY holds a child process attached to a pseudo-terminal master fd.
|
||||
//
|
||||
// mu guards the master field only. Read/Write/Resize capture the *os.File
|
||||
// under the lock and then do the (potentially blocking) I/O without holding
|
||||
// it, so Close can swap master to nil and close the fd concurrently — closing
|
||||
// the captured *os.File unblocks an in-flight Read. This avoids a data race
|
||||
// between pumpChild's Read and Session.Shutdown's Close, which the daemon now
|
||||
// hits routinely (daemon stop, not just process exit).
|
||||
type PTY struct {
|
||||
mu sync.Mutex
|
||||
master *os.File
|
||||
cmd *exec.Cmd
|
||||
}
|
||||
@@ -19,11 +29,13 @@ type PTY struct {
|
||||
// Start spawns argv with stdin/stdout/stderr attached to a new PTY sized
|
||||
// (cols, rows). The returned PTY exposes the master fd for the parent to
|
||||
// read from and write to.
|
||||
func Start(argv []string, env []string, cols, rows uint16) (*PTY, error) {
|
||||
func Start(argv []string, env []string, workDir string, cols, rows uint16) (*PTY, error) {
|
||||
if len(argv) == 0 {
|
||||
return nil, fmt.Errorf("pty: empty argv")
|
||||
}
|
||||
cmd := exec.Command(argv[0], argv[1:]...)
|
||||
cmd.Dir = workDir
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true}
|
||||
if env != nil {
|
||||
cmd.Env = ensureTerm(env)
|
||||
} else {
|
||||
@@ -42,24 +54,33 @@ func Start(argv []string, env []string, cols, rows uint16) (*PTY, error) {
|
||||
}
|
||||
|
||||
func (p *PTY) Read(b []byte) (int, error) {
|
||||
if p.master == nil {
|
||||
p.mu.Lock()
|
||||
m := p.master
|
||||
p.mu.Unlock()
|
||||
if m == nil {
|
||||
return 0, io.ErrClosedPipe
|
||||
}
|
||||
return p.master.Read(b)
|
||||
return m.Read(b)
|
||||
}
|
||||
|
||||
func (p *PTY) Write(b []byte) (int, error) {
|
||||
if p.master == nil {
|
||||
p.mu.Lock()
|
||||
m := p.master
|
||||
p.mu.Unlock()
|
||||
if m == nil {
|
||||
return 0, io.ErrClosedPipe
|
||||
}
|
||||
return p.master.Write(b)
|
||||
return m.Write(b)
|
||||
}
|
||||
|
||||
func (p *PTY) Resize(cols, rows uint16) error {
|
||||
if p.master == nil {
|
||||
p.mu.Lock()
|
||||
m := p.master
|
||||
p.mu.Unlock()
|
||||
if m == nil {
|
||||
return io.ErrClosedPipe
|
||||
}
|
||||
return cpty.Setsize(p.master, &cpty.Winsize{Cols: cols, Rows: rows})
|
||||
return cpty.Setsize(m, &cpty.Winsize{Cols: cols, Rows: rows})
|
||||
}
|
||||
|
||||
// Wait blocks until the child exits and returns its exit error if any.
|
||||
@@ -80,14 +101,21 @@ func (p *PTY) Pid() int {
|
||||
|
||||
// Close terminates the child (best effort) and releases the master fd.
|
||||
func (p *PTY) Close() error {
|
||||
p.mu.Lock()
|
||||
m := p.master
|
||||
p.master = nil
|
||||
p.mu.Unlock()
|
||||
var firstErr error
|
||||
if p.master != nil {
|
||||
if err := p.master.Close(); err != nil && firstErr == nil {
|
||||
if m != nil {
|
||||
if err := m.Close(); err != nil {
|
||||
firstErr = err
|
||||
}
|
||||
p.master = nil
|
||||
}
|
||||
if p.cmd != nil && p.cmd.Process != nil {
|
||||
pid := p.cmd.Process.Pid
|
||||
if pid > 0 {
|
||||
_ = syscall.Kill(-pid, syscall.SIGKILL)
|
||||
}
|
||||
_ = p.cmd.Process.Kill()
|
||||
}
|
||||
return firstErr
|
||||
|
||||
84
internal/pty/pty_test.go
Normal file
84
internal/pty/pty_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package pty
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestStartUsesWorkDir(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
p, err := Start([]string{"sh", "-c", "pwd"}, nil, dir, 80, 24)
|
||||
if err != nil {
|
||||
t.Fatalf("Start: %v", err)
|
||||
}
|
||||
defer p.Close()
|
||||
|
||||
var out bytes.Buffer
|
||||
buf := make([]byte, 256)
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
n, err := p.Read(buf)
|
||||
if n > 0 {
|
||||
out.Write(buf[:n])
|
||||
if strings.Contains(out.String(), dir) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
_ = p.Wait()
|
||||
|
||||
if got := strings.TrimSpace(out.String()); got != dir {
|
||||
t.Fatalf("pwd output = %q, want %q", got, dir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloseKillsProcessGroup(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
pidFile := filepath.Join(dir, "sleep.pid")
|
||||
env := append(os.Environ(), "PIDFILE="+pidFile)
|
||||
p, err := Start([]string{"sh", "-c", "sleep 30 & echo $! > \"$PIDFILE\"; wait"}, env, "", 80, 24)
|
||||
if err != nil {
|
||||
t.Fatalf("Start: %v", err)
|
||||
}
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
var childPID int
|
||||
for time.Now().Before(deadline) {
|
||||
b, err := os.ReadFile(pidFile)
|
||||
if err == nil {
|
||||
childPID, _ = strconv.Atoi(strings.TrimSpace(string(b)))
|
||||
if childPID > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if childPID <= 0 {
|
||||
_ = p.Close()
|
||||
t.Fatalf("background child pid was not written")
|
||||
}
|
||||
|
||||
if err := p.Close(); err != nil {
|
||||
t.Fatalf("Close: %v", err)
|
||||
}
|
||||
_ = p.Wait()
|
||||
|
||||
deadline = time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
err := syscall.Kill(childPID, 0)
|
||||
if errors.Is(err, syscall.ESRCH) {
|
||||
return
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("background child pid %d still exists after PTY.Close", childPID)
|
||||
}
|
||||
Reference in New Issue
Block a user