14 Commits
Author SHA1 Message Date
harry 4051e7264b docs: mark daemon/client plan as implemented 2026-05-27 14:36:02 +01:00
harry 63986e7e00 Fix data race on PTY master between Read and Close
pumpChild's PTY.Read raced Session.Shutdown's PTY.Close on the master
field (Close set it nil while Read read it). Benign at process exit on
main, but the daemon now runs Shutdown routinely (daemon stop). Guard
the field with a mutex, capturing the fd under the lock and doing the
blocking I/O outside it so Close still unblocks an in-flight Read.

Caught under: go test -race -run 'Daemon|NetClient|Owner' -count=5.
2026-05-27 14:35:33 +01:00
harry 6d15626e05 add per-pane display ownership 2026-05-27 14:30:47 +01:00
harry 63cb8a4388 add tcp daemon listener with token auth 2026-05-27 14:19:14 +01:00
harry 5149224000 attach default client to local daemon 2026-05-27 14:09:51 +01:00
harry 95b1967e9b Fix daemon shutdown hang and concurrent-send race
- daemon_net: close the client transport on context cancellation so the
  per-connection Recv loop unblocks; otherwise wg.Wait() in the accept loop
  hung on a still-connected client and the daemon never stopped.
- protocol: guard ConnTransport.Send with a mutex so the subscriber pump and
  command handlers can push frames concurrently without racing the bufio.Writer.

Fixes TestDaemonDetachReattachPreservesProcess (now passes under -race).
2026-05-27 13:59:47 +01:00
harry d07a09d64f add local daemon socket protocol 2026-05-27 13:55:38 +01:00
harry c56de27f44 fix scratchpad routing by caller project 2026-05-27 13:50:17 +01:00
harry 80a14502c4 app: add loopback multi-project registry 2026-05-27 13:40:59 +01:00
harry 08c7405c79 docs: add daemon client implementation plan 2026-05-27 13:25:59 +01:00
harry ec0c148164 Update PTY start call sites 2026-05-27 13:21:18 +01:00
harry 9aecc8b7a2 Scaffold loopback daemon client split 2026-05-27 13:19:56 +01:00
harry e63bdad5e1 Add daemon client protocol frames 2026-05-27 13:19:42 +01:00
harry b72a32bbc6 Fix PTY workdir and process group teardown 2026-05-27 13:19:35 +01:00
55 changed files with 4469 additions and 2911 deletions
@@ -1,358 +0,0 @@
# Task Sidebar Simplification Plan
## User Decisions
- First pass is manual project-local tasks only. Future GitHub/Linear linking should not be built now.
- The right sidebar should put Tasks first, while keeping existing process, agent tree, and scratchpad navigation secondary.
- Generic palette-spawned top-level agents must not inherit a selected task. Only explicit task-scoped actions, such as `Start agent for task`, create task-bound agents.
- Agents should receive task context only when they are task-bound, and task-bound agents should be told to register task worktrees through MCP.
## UX Review: Missing Pieces Today
- There is no task model. The current right rail in `internal/app/sidebar.go` is `Processes`, `Agent Tree`, and `Scratchpads` only.
- There is no task list, task detail view, task-focused navigation entry, or visible way to discover existing tasks.
- Task creation is absent. Existing creation flows are process/agent palette actions in `internal/app/palette.go` and `internal/app/app.go`.
- There is no explicit task-scoped launch action, so a user cannot intentionally start an agent for a task.
- MCP has no task context and no worktree registration tool, so agents cannot tell patterm which worktrees belong to a task.
- Current process rows are not grouped by task and do not surface task membership.
- Sidebar empty states are generic process/scratchpad placeholders and do not advertise task creation.
- The sidebar has no click or row activation model. First pass should stay keyboard/palette driven rather than inventing mouse interactions.
- Cleanup/status/archive and external issue links are real task-list UX needs, but they are out of scope for this first simplification pass unless requested later.
## Recommended Scope
Implement a minimal first-class task feature:
- Persist manual tasks per project.
- Render Tasks as the first sidebar section.
- Let users create, open, and rename tasks through the palette.
- Render a focused task detail view in the main viewport.
- Add explicit `Start agent for task` actions for focused tasks and task-bound children.
- Thread `TaskID` through children spawned from task context.
- Expose task context and `task_register_worktree` only to task-bound MCP callers.
- Fix PTY working-directory propagation, because task worktree paths are otherwise misleading.
Do not add GitHub/Linear linking, task import, agent-side task creation, task list MCP tools, click handling, or broad task/project management UI in this pass.
## Implementation Steps
### 1. Add Project Task Store
Add `internal/task/task.go` and `internal/task/task_test.go`.
Persist to `$XDG_DATA_HOME/patterm/projects/<projectKey>/tasks.json`, matching the existing project-local persistence pattern in `internal/persist/persist.go`.
Minimal structs:
```go
type Task struct {
ID string `json:"id"`
Title string `json:"title"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Worktrees []Worktree `json:"worktrees,omitempty"`
}
type Worktree struct {
Path string `json:"path"`
Branch string `json:"branch,omitempty"`
CreatedByProcessID string `json:"created_by_process_id,omitempty"`
RegisteredAt string `json:"registered_at"`
}
```
Store API:
```go
Open(projectKey string) (*Store, error)
(*Store).List() []Task
(*Store).Get(id string) (Task, bool)
(*Store).Create(title string) (Task, error)
(*Store).Rename(id, title string) (Task, error)
(*Store).RegisterWorktree(taskID string, wt Worktree) (Task, error)
```
Keep it deliberately small:
- Generate opaque IDs like `task_<hex>`.
- Validate non-empty titles.
- Use stable list ordering.
- De-duplicate worktrees by cleaned absolute path.
- Atomic write via `tasks.json.tmp` then rename.
- No status, assignee, priority, external refs, labels, or issue links yet.
### 2. Fix PTY Working Directory
Change `internal/pty/pty.go`:
- Update `Start(argv, env, cols, rows)` to `Start(argv, env, workDir, cols, rows)`.
- Set `cmd.Dir = workDir` when non-empty.
Update call sites:
- `internal/app/child.go: startPTY` passes `c.WorkDir`.
- `internal/harness/session.go` and `internal/harness/restart_persist_test.go` pass an empty workdir or project dir as appropriate.
- `cmd/spike/main.go` passes an empty workdir.
Add a focused PTY/app test that spawns `sh -lc pwd` with a temp workdir and verifies the child runs there.
### 3. Thread Task Context Through Runtime State
Modify `internal/app/child.go`:
- Add `TaskID string` to `Child`.
- Add `taskID` to `newChildEntry`.
Modify `internal/app/session.go`:
- Add `TaskID string` to `SpawnSpec`.
- Pass it into `newChildEntry`.
- Keep persistence limited to top-level command entries. Do not persist agent/task process trees in `processes.json`.
Modify `internal/app/launch.go`:
- Avoid adding more positional string parameters. Introduce a small launch context struct, for example:
```go
type LaunchContext struct {
ParentID string
TaskID string
WorkDir string
}
```
- Update `LaunchAgent`, `LaunchCommandPreset`, `LaunchCommandArgv`, and `LaunchTerminal` to accept this context or an equivalent minimal option.
- Generic palette spawns pass empty `TaskID` even when a task is focused.
- Explicit task actions pass `TaskID`.
- MCP spawns inherit `TaskID` from the caller only when the caller is task-bound.
### 4. Wire Task Store Into App State
Modify `internal/app/app.go`:
- Open `task.Open(opts.ProjectKey)` during `Run`, near scratchpads/trust/persist stores.
- Add `tasks *task.Store` to `uiState`.
- Add `focusedTaskID string`, mutually exclusive with `focusedID` and `focusedPad`.
- Add `tasksCacheMu` / `tasksCache`, mirroring `padsList()`.
- Add `tasksList()`, `invalidateTasksCache()`, and `tasksChanged()`.
- Pass the task store to `newToolHost`.
Modify `internal/app/host.go`:
- Add `tasks *task.Store` to `toolHost`.
- Extend `newToolHost` to accept it.
- Add a small `taskSink` interface like `tasksChanged()` so MCP worktree registration can refresh the sidebar/detail view.
### 5. Make Tasks First In The Sidebar
Modify `internal/app/sidebar.go:drawSidebar`:
- Render `Tasks` before `Processes`.
- Show empty state as something actionable, for example `(Ctrl-K create task)`.
- Show task rows with focus marker, title, and a compact suffix such as `2 worktrees` or `2w`.
- Keep current `Processes`, `Agent Tree`, and `Scratchpads` sections after Tasks.
- Do not add mouse or click behavior in this pass.
Modify `internal/app/tree.go`:
- Extend `navEntry` with `taskID string` and `isTask()`.
- Change `empty()` to include task entries.
- Change `sidebarNav` and `nextNavEntry` to accept tasks and order entries as `tasks -> processes -> active agent tree -> scratchpads`.
- Keep `nextChildID` compatibility tests working for process-only behavior.
Modify `internal/app/app.go`:
- Add `focusTask(taskID string)`.
- Add `renderTaskView(taskID string)` and `repaintFocusedTask()`.
- Update `focusProcess` and `focusScratchpad` to clear `focusedTaskID`.
- Update `repaintFocused`, `repaintFocusedWithChrome`, `restoreView` inside `closePalette`, and Ctrl-W/S pending nav handling to route task entries correctly.
- Update `drawStatusLine` to show `task: <title>` when a task is focused, and optionally `task: <title>` when a focused child is task-bound.
- Update `renderEmptyState` copy to mention tasks, for example `Press Ctrl-K to create a task or spawn an agent/process`.
Focused task detail view should be read-only and compact:
- Title and task ID.
- Registered worktrees.
- Task-bound running children if cheap to derive from `Session.Children()`.
- Hints: `Ctrl-K task actions`, `Ctrl-W/S navigate`.
### 6. Add Palette Task Actions
Modify `internal/app/palette.go`:
- Add task list and focused task fields to `paletteState`.
- Add `taskID string` to `paletteAction`.
- Add task action kinds:
- `task-create-form`
- `task-create-submit`
- `task-switch`
- `task-rename-form`
- `task-rename-submit`
- `task-start-agent`
- Add a task macro if useful, but do not make it required for the first patch.
- Reuse `renameForm` for create/rename by allowing subject `task` and different submit kinds.
Palette item behavior:
- Global/Open group includes `Create task...` and `Open task: <title>`.
- When a task is focused, show `Rename task` and `Start agent for task: <preset>` rows.
- When a task-bound child is focused, show `Open task: <title>` and `Start another agent for task: <preset>` rows.
- Normal `Spawn agent: <preset>` remains unscoped and passes empty `TaskID`.
Modify `internal/app/app.go:openPaletteLocked`:
- Pass `tasksList()`, `focusedTaskID`, and current focused child task information into `newPalette`.
Modify `internal/app/app.go:closePalette`:
- `task-create-submit`: create task, invalidate cache, focus the new task.
- `task-switch`: focus task.
- `task-rename-submit`: rename task, invalidate cache, redraw/focus task.
- `task-start-agent`: launch selected agent preset with the task ID and a task initial prompt.
Task initial prompt should be injected only for task-bound agents. Keep it single-line because agent input submission is line-sensitive:
```text
[system: you are working on patterm task "<title>" (<task_id>). If you create or use git worktrees for this task, call task_register_worktree with the path and branch.]
```
Include known registered worktrees in the prompt only if the list is short; otherwise rely on `whoami`.
### 7. Add Conditional MCP Task Context And Worktree Registration
Modify `internal/mcp/tools.go`:
- Add MCP structs `TaskInfo`, `TaskWorktree`, and `TaskRegisterWorktreeArgs`.
- Add optional `Task *TaskInfo `json:"task,omitempty"`` to `WhoAmI` and `ProjectStatus`.
- Do not add task fields to `ProcessInfo` in this pass, so unbound agents cannot discover task assignments through `list_processes`.
- Extend `ToolHost` with:
```go
CallerTask(processID string) (TaskInfo, bool)
RegisterTaskWorktree(callerID string, args TaskRegisterWorktreeArgs) (TaskInfo, error)
```
`TaskRegisterWorktreeArgs` should contain only `path` and optional `branch`. Do not accept a task ID from the caller; the host infers the task from `callerID`.
Modify `internal/mcp/protocol.go`:
- Replace the constant-only `serverInstructions` path with a helper that can append task-bound instructions only when `host.CallerTask(callerID)` is present.
- Change `toolCatalog(role)` to `toolCatalog(role, taskBound bool)`.
- Advertise `task_register_worktree` only when `taskBound` is true.
- Keep `spawn_agent` hidden from sub-agents as today.
Modify `internal/mcp/tools.go:callTool`:
- Add case `task_register_worktree`.
- Reject unbound callers with `role_forbidden`.
- Call `h.RegisterTaskWorktree(callerID, args)` and return the updated task.
Modify `internal/app/host.go`:
- Add helper `callerTaskID(callerID string) string` based on `Child.TaskID`.
- Implement `CallerTask` by resolving the caller child and task store entry.
- `WhoAmI` includes `Task` only when the caller child has a valid task ID.
- `GetProjectStatus` includes the same optional task only for task-bound callers.
- `SpawnAgent` and `SpawnProcess` inherit `TaskID` from the caller.
- Replace `wrapSubAgentPrompt` with a small prompt builder that can include sub-agent instructions, task instructions, or both. Task instructions should be added whenever the spawned agent is task-bound, even when caller `agent_instructions` is empty.
- `RegisterTaskWorktree` resolves relative paths against the caller child `WorkDir`, falling back to the project dir, cleans/absolutizes the path, stores it, and triggers `tasksChanged()`.
Important security/UX rule:
- Unbound MCP callers should not receive task context, task tool availability, task worktree data, or task IDs.
### 8. Tests
Add or update focused unit tests before broad harness work.
Task store:
- `internal/task/task_test.go`: create/list persists across reopen.
- Validate empty title errors.
- Rename updates title and `updated_at`.
- Register worktree de-duplicates cleaned absolute paths.
Runtime/spawn:
- `internal/app/session_test.go` or focused launch tests: `TaskID` lands on spawned child.
- MCP spawn from task-bound caller propagates `TaskID` to child.
- Generic palette spawn while `focusedTaskID` is set keeps `TaskID == ""`.
- Explicit `task-start-agent` sets `TaskID` and injects task prompt.
Sidebar/navigation/palette:
- `internal/app/tree_test.go`: task nav entries precede processes, agent tree, and scratchpads.
- `internal/app/palette_context_test.go`: focused task shows task actions; generic spawn remains unscoped.
- Add render-level tests only where existing helpers make that cheap; avoid brittle full-frame snapshots.
MCP:
- `internal/mcp/protocol_test.go`: unbound tools/list omits `task_register_worktree`; task-bound tools/list includes it.
- `internal/mcp/tools.go` tests or app host tests: unbound registration is rejected.
- `internal/app/host_test.go`: task-bound `whoami` includes task; unbound `whoami` omits task.
- `internal/app/host_test.go`: registration stores an absolute path and returns updated task info.
PTY working dir:
- Add the smallest test proving a PTY child honors configured `WorkDir`.
Harness:
- Add one lightweight scenario under `internal/harness/scenarios/`:
- Open palette.
- Create a task.
- Assert sidebar shows `Tasks` and the task title.
- Start a fake agent through `Start agent for task`.
- Use MCP `whoami` from that fake agent if feasible, or assert the prompt/context path indirectly through captured output.
### 9. Changelog
Update `CHANGELOG.md` under `[Unreleased]`.
Suggested bullets:
```md
### Added
- Added project-local manual tasks in the right sidebar, with palette actions to create tasks, open tasks, rename tasks, and start task-scoped agents.
- Added task-bound MCP context and `task_register_worktree` so agents launched for a task can register git worktrees they create.
### Fixed
- Child PTYs now honor their configured working directory when launched.
```
## Verification Commands
Run focused checks first:
```sh
go test ./internal/task/...
go test ./internal/pty/...
go test ./internal/app/...
go test ./internal/mcp/...
```
Then run broader checks:
```sh
go test ./internal/harness/...
go test ./...
go build -o ./bin/patterm ./cmd/patterm
```
Manual TUI smoke test:
```sh
./bin/patterm --project /home/harry/Dev/patterm
```
Manual flow to verify:
- Press `Ctrl-K`, create a task, and confirm it appears first in the sidebar.
- Focus the task from the sidebar navigation with `Ctrl-W/S`.
- Press `Ctrl-K`, start an agent for that task, and confirm the task remains visible and the agent is task-bound.
- Start a normal agent through `Spawn agent` while the task is selected and confirm it is not task-bound.
- From a task-bound fake/test agent, call `whoami` and confirm task context appears.
- From an unbound agent, call `whoami` and confirm no task context appears.
- From a task-bound agent, call `task_register_worktree` and confirm the task detail view/sidebar suffix updates.
If harness tests fail with Unix socket or PTY permission errors, rerun them in an environment that permits sockets and PTYs, as documented in `AGENTS.md`.
+34 -16
View File
@@ -7,30 +7,48 @@ loosely follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [Unreleased]
### Added ### Added
- Project-local manual tasks now appear first in the right sidebar, - `patterm daemon`, `patterm daemon stop`, and `patterm ls` now expose
with palette actions to create tasks, open tasks, rename tasks, and a local unix-socket daemon lifecycle for the daemon/client split.
start task-scoped agents. - The local daemon protocol now supports attach, explicit detach,
- Task-bound agents now receive task MCP context and can call project listing, focused-pane snapshots, pane chunks, resize/focus
`task_register_worktree` to register git worktrees they create or updates, and daemon-owned command spawn requests while keeping child
use for the task. 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 - MCP clients can now call `scratchpad_delete` with a scratchpad name
to remove a shared project scratchpad. to remove a shared project scratchpad.
### Changed ### Changed
- The tab bar now shows each visible agent tab's own summary instead - The tab bar now shows each visible agent tab's own summary instead
of only rendering the focused tab's summary. of only rendering the focused tab's summary.
- `get_process_output` now returns aggressively canonical terminal text - Grid-mode `get_process_output` now returns whitespace-normalized
by default, removing ANSI/control noise, decorative borders, duplicate text to avoid sending padded terminal rows and repeated blank lines
status churn, and volatile progress/timer fragments; raw PTY bytes are over MCP.
opt-in with `raw:true`.
- MCP responses now use slimmer defaults: tool-call JSON is no longer
duplicated into text content, large output and scratchpad reads are
capped with truncation metadata, and `whoami` / `get_project_status`
only include full tool lists when `include_tools` is requested.
### Fixed ### Fixed
- Child PTYs now honor their configured working directory when - MCP scratchpad tools now route through the caller's project instead
launched. of always using the daemon registry's default project.
- Injected agent input now sends the submit Enter as a separated, - Injected agent input now sends the submit Enter as a separated,
settled keystroke so messages reliably submit instead of sometimes settled keystroke so messages reliably submit instead of sometimes
sitting unsent in the composer. sitting unsent in the composer.
-1
View File
@@ -1 +0,0 @@
- [ ] Pasting into codex is no longer clean, it sends loads of messages rather than one clean paste.
+170
View File
@@ -14,7 +14,9 @@ package main
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"net"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@@ -27,6 +29,7 @@ import (
"github.com/hjbdev/patterm/internal/app" "github.com/hjbdev/patterm/internal/app"
"github.com/hjbdev/patterm/internal/mcp" "github.com/hjbdev/patterm/internal/mcp"
"github.com/hjbdev/patterm/internal/projectkey" "github.com/hjbdev/patterm/internal/projectkey"
"github.com/hjbdev/patterm/internal/protocol"
) )
// version is overridden at build time via `-ldflags "-X main.version=..."`. // version is overridden at build time via `-ldflags "-X main.version=..."`.
@@ -48,10 +51,25 @@ func main() {
runDebugHarness() runDebugHarness()
return 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 ( var (
projectDir = flag.String("project", "", "project directory (default $PWD)") projectDir = flag.String("project", "", "project directory (default $PWD)")
showVersion = flag.Bool("version", false, "print version and exit") 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)") 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)") 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 != "" { if *projectDir != "" {
cwd = *projectDir cwd = *projectDir
} else if flag.NArg() > 0 {
cwd = flag.Arg(0)
} }
key, err := projectkey.Key(cwd) key, err := projectkey.Key(cwd)
if err != nil { if err != nil {
@@ -95,6 +115,7 @@ func main() {
defer stopProfile() defer stopProfile()
ctx := context.Background() ctx := context.Background()
if *inProcess || os.Getenv("PATTERM_NO_DAEMON") != "" {
if err := app.Run(ctx, app.Options{ if err := app.Run(ctx, app.Options{
ProjectDir: cwd, ProjectDir: cwd,
ProjectKey: key, ProjectKey: key,
@@ -103,6 +124,20 @@ func main() {
}); err != nil { }); err != nil {
die("%v", err) die("%v", err)
} }
return
}
if resolvedDebug != "" || resolvedProfile != "" {
die("--debug and --profile currently require --in-process")
}
if err := app.RunAttachedClient(ctx, app.ClientOptions{
ProjectDir: cwd,
Stdin: os.Stdin,
Stdout: os.Stdout,
RawMode: true,
AutoStart: true,
}); err != nil {
die("%v", err)
}
} }
// resolveDiagDir turns the raw flag value into an absolute directory // resolveDiagDir turns the raw flag value into an absolute directory
@@ -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 { func versionString() string {
commit, date := "unknown", "unknown" commit, date := "unknown", "unknown"
if info, ok := debug.ReadBuildInfo(); ok { if info, ok := debug.ReadBuildInfo(); ok {
+273
View File
@@ -0,0 +1,273 @@
# patterm: persistent daemon + thin networked client — implementation plan
Status: implemented — Phases 04 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?
+246 -439
View File
File diff suppressed because it is too large Load Diff
-143
View File
@@ -1,143 +0,0 @@
package app
import (
"regexp"
"strings"
"unicode"
"unicode/utf8"
)
var (
statusVolatileRE = regexp.MustCompile(`\b(?:\d+h\s*)?\d+m\s*\d+s\b|\b\d{1,2}:\d{2}(?::\d{2})?\b|\b\d+(?:\.\d+)?s\b`)
counterRE = regexp.MustCompile(`\b\d+\s*/\s*\d+\b|\b\d{1,3}%`)
spinnerGlyphRE = regexp.MustCompile(`^[\s⠁⠂⠄⡀⢀⠠⠐⠈⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏•·∙◐◓◑◒]+`)
)
func canonicalizeTerminalText(s string, maxLines int) (string, bool, int) {
s = string(stripANSIBytes(nil, []byte(s)))
s = strings.ReplaceAll(s, "\r\n", "\n")
s = carriageReturnToLines(s)
s = strings.ReplaceAll(s, "\r", "\n")
lines := strings.Split(s, "\n")
out := make([]string, 0, len(lines))
pendingBlank := false
for _, raw := range lines {
line := strings.TrimRightFunc(stripControlRunes(raw), unicode.IsSpace)
if strings.TrimSpace(line) == "" {
if len(out) > 0 {
pendingBlank = true
}
continue
}
if isBorderOnlyLine(line) {
continue
}
line = canonicalStatusLine(line)
if len(out) > 0 && out[len(out)-1] == line {
pendingBlank = false
continue
}
if pendingBlank {
out = append(out, "")
pendingBlank = false
}
out = append(out, line)
}
if maxLines > 0 && len(out) > maxLines {
dropped := strings.Join(out[:len(out)-maxLines], "\n")
out = out[len(out)-maxLines:]
return strings.Join(out, "\n"), true, len(dropped)
}
return strings.Join(out, "\n"), false, 0
}
func carriageReturnToLines(s string) string {
var out []string
var current strings.Builder
flush := func() {
out = append(out, current.String())
current.Reset()
}
for len(s) > 0 {
r, size := utf8.DecodeRuneInString(s)
s = s[size:]
switch r {
case '\r':
current.Reset()
case '\n':
flush()
default:
current.WriteRune(r)
}
}
if current.Len() > 0 || len(out) == 0 {
flush()
}
return strings.Join(out, "\n")
}
func stripControlRunes(s string) string {
return strings.Map(func(r rune) rune {
if r == '\t' || r == '\n' {
return r
}
if unicode.IsControl(r) {
return -1
}
return r
}, s)
}
func isBorderOnlyLine(s string) bool {
trimmed := strings.TrimSpace(s)
if trimmed == "" {
return false
}
seenBox := false
for _, r := range trimmed {
if r >= 0x2500 && r <= 0x257f {
seenBox = true
continue
}
switch r {
case ' ', '\t', '-', '_', '=', '+', '|', ':', '.', '\'', '"', '`', '*':
continue
default:
return false
}
}
return seenBox
}
func canonicalStatusLine(s string) string {
if !looksStatusLike(s) {
return s
}
leading := len(s) - len(strings.TrimLeftFunc(s, unicode.IsSpace))
prefix := s[:leading]
body := s[leading:]
body = spinnerGlyphRE.ReplaceAllString(body, "")
body = statusVolatileRE.ReplaceAllString(body, "[time]")
body = counterRE.ReplaceAllString(body, "[count]")
return prefix + strings.TrimRightFunc(body, unicode.IsSpace)
}
func looksStatusLike(s string) bool {
lower := strings.ToLower(s)
for _, token := range []string{
"status", "running", "remaining", "progress", "loading",
"building", "installing", "downloading", "waiting", "working",
} {
if strings.Contains(lower, token) {
return true
}
}
trimmed := strings.TrimSpace(s)
if trimmed == "" {
return false
}
r, _ := utf8.DecodeRuneInString(trimmed)
return strings.ContainsRune("⠁⠂⠄⡀⢀⠠⠐⠈⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏•·∙◐◓◑◒", r)
}
-167
View File
@@ -1,167 +0,0 @@
package app
import (
"strings"
"testing"
"github.com/hjbdev/patterm/internal/mcp"
"github.com/hjbdev/patterm/internal/preset"
)
func TestCanonicalizeTerminalText(t *testing.T) {
cases := []struct {
name string
in string
want string
}{
{
name: "ansi osc and controls",
in: "\x1b]0;title\x07\x1b[31mred\x1b[0m\x00\nok",
want: "red\nok",
},
{
name: "noisy harness stream",
in: "\x1b]0;noise\x07\x1b[31mStatus: running 12s\x1b[0m\nStatus: running 13s\n╭────╮\n│ │\nDownloading 10%\rDownloading 100%\nFINAL: deploy ready\n",
want: "Status: running [time]\nDownloading [count]\nFINAL: deploy ready",
},
{
name: "repeated blank collapse",
in: "one\n\n\n two\n \n\t\nthree",
want: "one\n\n two\n\nthree",
},
{
name: "border only box drawing removal",
in: "╭────────╮\n│ │\nimportant\n╰────────╯",
want: "important",
},
{
name: "carriage return progress coalesces final frame",
in: "Downloading 10%\rDownloading 20%\rDownloading 100%\nDone",
want: "Downloading [count]\nDone",
},
{
name: "volatile timer duplicate collapse",
in: "Status: running 12s\nStatus: running 13s\nStatus: running 01:23",
want: "Status: running [time]",
},
{
name: "duplicate status row collapse",
in: "⠋ Building 1/4\n⠙ Building 2/4\n⠹ Building 3/4\nready",
want: "Building [count]\nready",
},
{
name: "preserve meaningful indented code and tables",
in: " if elapsed == 12s {\n return value\n }\n| name | value |\n| a | 1 |",
want: " if elapsed == 12s {\n return value\n }\n| name | value |\n| a | 1 |",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, truncated, _ := canonicalizeTerminalText(tc.in, 120)
if truncated {
t.Fatalf("unexpected truncation")
}
if got != tc.want {
t.Fatalf("got %q want %q", got, tc.want)
}
})
}
}
func TestCanonicalizeTerminalTextMaxLines(t *testing.T) {
got, truncated, dropped := canonicalizeTerminalText("one\ntwo\nthree", 2)
if !truncated {
t.Fatalf("expected truncation")
}
if dropped == 0 {
t.Fatalf("expected dropped bytes")
}
if got != "two\nthree" {
t.Fatalf("got %q", got)
}
}
func TestGetProcessOutputStreamCanonicalByDefault(t *testing.T) {
sess := NewSession(t.TempDir(), "test")
c := newChildEntry("p1", "proc", KindCommand, nil, nil, "", "", "", "")
addChild(sess, c)
c.recordWrite([]byte("\x1b[31mStatus: running 12s\x1b[0m\nStatus: running 13s\nresult\n"))
host := newToolHost(sess, nil, nil, nil, preset.Set{}, nil, 80, 24)
out, err := host.GetProcessOutput("", mcp.ProcessOutputArgs{ProcessID: c.ID, Mode: "stream"})
if err != nil {
t.Fatal(err)
}
if !out.Canonicalized {
t.Fatalf("expected canonicalized output")
}
if out.Content != "Status: running [time]\nresult" {
t.Fatalf("content = %q", out.Content)
}
if out.Cursor != nil || out.Rows != 0 || out.Cols != 0 || out.ScreenVersion != 0 || out.IdleMS != 0 {
t.Fatalf("default output should be metadata-light: %#v", out)
}
}
func TestGetProcessOutputRawReturnsStreamBytes(t *testing.T) {
sess := NewSession(t.TempDir(), "test")
c := newChildEntry("p1", "proc", KindCommand, nil, nil, "", "", "", "")
addChild(sess, c)
c.recordWrite([]byte("\x1b[31mred\x1b[0m"))
host := newToolHost(sess, nil, nil, nil, preset.Set{}, nil, 80, 24)
out, err := host.GetProcessOutput("", mcp.ProcessOutputArgs{ProcessID: c.ID, Mode: "grid", Raw: true})
if err != nil {
t.Fatal(err)
}
if out.Mode != "stream" {
t.Fatalf("raw grid mode should report stream semantics, got %q", out.Mode)
}
if out.Canonicalized {
t.Fatalf("raw output should not be canonicalized")
}
if out.Content != "\x1b[31mred\x1b[0m" {
t.Fatalf("content = %q", out.Content)
}
if out.NewOffset != int64(len(out.Content)) {
t.Fatalf("new_offset=%d want %d", out.NewOffset, len(out.Content))
}
}
func TestGetProcessOutputCanonicalAfterRawRead(t *testing.T) {
sess := NewSession(t.TempDir(), "test")
c := newChildEntry("p1", "proc", KindCommand, nil, nil, "", "", "", "")
addChild(sess, c)
c.recordWrite([]byte("\x1b[31mStatus: running 12s\x1b[0m\nStatus: running 13s\nDownloading 10%\rDownloading 100%\nFINAL: deploy ready\n"))
host := newToolHost(sess, nil, nil, nil, preset.Set{}, nil, 80, 24)
if _, err := host.GetProcessOutput("", mcp.ProcessOutputArgs{ProcessID: c.ID, Mode: "stream", Raw: true}); err != nil {
t.Fatal(err)
}
out, err := host.GetProcessOutput("", mcp.ProcessOutputArgs{ProcessID: c.ID, Mode: "stream", MaxLines: 20})
if err != nil {
t.Fatal(err)
}
if out.Content != "Status: running [time]\nDownloading [count]\nFINAL: deploy ready" {
t.Fatalf("content = %q", out.Content)
}
}
func TestGetProcessOutputIncludeMetaRestoresFields(t *testing.T) {
sess := NewSession(t.TempDir(), "test")
c := newChildEntry("p1", "proc", KindCommand, nil, nil, "", "", "", "")
addChild(sess, c)
c.recordWrite([]byte("ok"))
host := newToolHost(sess, nil, nil, nil, preset.Set{}, nil, 80, 24)
out, err := host.GetProcessOutput("", mcp.ProcessOutputArgs{ProcessID: c.ID, Mode: "stream", IncludeMeta: true})
if err != nil {
t.Fatal(err)
}
if out.ScreenVersion == 0 {
t.Fatalf("screen_version missing with include_meta: %#v", out)
}
if !strings.Contains(out.Content, "ok") {
t.Fatalf("content = %q", out.Content)
}
}
+1 -9
View File
@@ -77,7 +77,6 @@ type Child struct {
WorkDir string WorkDir string
Kind ChildKind Kind ChildKind
ParentID string // empty for top-level sessions ParentID string // empty for top-level sessions
TaskID string // empty unless launched from an explicit task context
// PresetRef names the source preset (when known). Used by trust // PresetRef names the source preset (when known). Used by trust
// gating to re-check on restart_process. Empty for freeform-argv // gating to re-check on restart_process. Empty for freeform-argv
@@ -192,7 +191,7 @@ type PortSighting struct {
const ringCap = 1 << 20 // 1 MiB per SPEC §5 const ringCap = 1 << 20 // 1 MiB per SPEC §5
// newChildEntry builds the in-memory Child record but does NOT start a PTY. // newChildEntry builds the in-memory Child record but does NOT start a PTY.
func newChildEntry(id, name string, kind ChildKind, argv, env []string, parentID, taskID, workDir, presetRef string) *Child { func newChildEntry(id, name string, kind ChildKind, argv, env []string, parentID, workDir, presetRef string) *Child {
c := &Child{ c := &Child{
ID: id, ID: id,
Name: name, Name: name,
@@ -201,7 +200,6 @@ func newChildEntry(id, name string, kind ChildKind, argv, env []string, parentID
WorkDir: workDir, WorkDir: workDir,
Kind: kind, Kind: kind,
ParentID: parentID, ParentID: parentID,
TaskID: taskID,
PresetRef: presetRef, PresetRef: presetRef,
ring: make([]byte, ringCap), ring: make([]byte, ringCap),
} }
@@ -534,12 +532,6 @@ func (c *Child) StreamRead(since int64) ([]byte, int64) {
return out, end return out, end
} }
func (c *Child) StreamOffset() int64 {
c.ringMu.Lock()
defer c.ringMu.Unlock()
return c.ringWrites
}
func (c *Child) signal(sig syscall.Signal) error { func (c *Child) signal(sig syscall.Signal) error {
pty := c.PTY() pty := c.PTY()
if pty == nil { if pty == nil {
+80
View 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
View 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)
}
}
+677
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
}
+58 -323
View File
@@ -2,7 +2,6 @@ package app
import ( import (
"fmt" "fmt"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"sync" "sync"
@@ -13,7 +12,6 @@ import (
"github.com/hjbdev/patterm/internal/mcp" "github.com/hjbdev/patterm/internal/mcp"
"github.com/hjbdev/patterm/internal/preset" "github.com/hjbdev/patterm/internal/preset"
"github.com/hjbdev/patterm/internal/scratchpad" "github.com/hjbdev/patterm/internal/scratchpad"
"github.com/hjbdev/patterm/internal/task"
"github.com/hjbdev/patterm/internal/trust" "github.com/hjbdev/patterm/internal/trust"
pkgvt "github.com/hjbdev/patterm/internal/vt" pkgvt "github.com/hjbdev/patterm/internal/vt"
) )
@@ -43,16 +41,11 @@ type scratchpadSink interface {
scratchpadsChanged() scratchpadsChanged()
} }
type taskSink interface {
tasksChanged()
}
// toolHost adapts the running session + scratchpad store + trust store // toolHost adapts the running session + scratchpad store + trust store
// to the MCP ToolHost interface. SPEC §7 tools route through here. // to the MCP ToolHost interface. SPEC §7 tools route through here.
type toolHost struct { type toolHost struct {
sess *Session sess *Session
pads *scratchpad.Store pads *scratchpad.Store
tasks *task.Store
launcher *Launcher launcher *Launcher
presets preset.Set presets preset.Set
trust *trust.Store trust *trust.Store
@@ -68,27 +61,14 @@ type toolHost struct {
focus focusSink focus focusSink
prompter trustPrompter prompter trustPrompter
scratch scratchpadSink scratch scratchpadSink
taskUI taskSink
timers *timerManager timers *timerManager
} }
const ( func newToolHost(sess *Session, pads *scratchpad.Store, launcher *Launcher, presets preset.Set, tr *trust.Store, cols, rows uint16) *toolHost {
defaultMCPContentBytes = 12_000
maxMCPContentBytes = 65_536
defaultMCPCanonicalLines = 120
maxMCPCanonicalLines = 500
defaultMCPTailBytes = 8_000
defaultScratchpadReadBytes = 12_000
defaultSearchLineBytes = 2_000
maxSearchMatches = 50
)
func newToolHost(sess *Session, pads *scratchpad.Store, tasks *task.Store, launcher *Launcher, presets preset.Set, tr *trust.Store, cols, rows uint16) *toolHost {
h := &toolHost{ h := &toolHost{
sess: sess, sess: sess,
pads: pads, pads: pads,
tasks: tasks,
launcher: launcher, launcher: launcher,
presets: presets, presets: presets,
trust: tr, trust: tr,
@@ -166,21 +146,6 @@ func (h *toolHost) CallerRole(processID string) mcp.CallerRole {
return mcp.RoleSubAgent return mcp.RoleSubAgent
} }
func (h *toolHost) CallerTask(processID string) (mcp.TaskInfo, bool) {
if h == nil || h.sess == nil || h.tasks == nil || processID == "" {
return mcp.TaskInfo{}, false
}
c := h.sess.FindChild(processID)
if c == nil || c.TaskID == "" {
return mcp.TaskInfo{}, false
}
t, ok := h.tasks.Get(c.TaskID)
if !ok {
return mcp.TaskInfo{}, false
}
return taskInfoOf(t), true
}
// ─────────────────────────────────────────────────────────────────── // ───────────────────────────────────────────────────────────────────
// Lifecycle // Lifecycle
// ─────────────────────────────────────────────────────────────────── // ───────────────────────────────────────────────────────────────────
@@ -200,14 +165,8 @@ func (h *toolHost) SpawnAgent(callerID string, args mcp.SpawnAgentArgs) (mcp.Pro
if display == "" { if display == "" {
display = args.Agent display = args.Agent
} }
var taskInfo *mcp.TaskInfo prompt := wrapSubAgentPrompt(args.AgentInstructions, h.sess.FindChild(callerID) != nil)
ctx := LaunchContext{ParentID: callerID} c, err := h.launcher.LaunchAgent(p, display, prompt, callerID)
if ti, ok := h.CallerTask(callerID); ok {
ctx.TaskID = ti.ID
taskInfo = &ti
}
prompt := buildAgentPrompt(args.AgentInstructions, h.sess.FindChild(callerID) != nil, taskInfo)
c, err := h.launcher.LaunchAgent(p, display, prompt, ctx)
if err != nil { if err != nil {
return mcp.ProcessInfo{}, err return mcp.ProcessInfo{}, err
} }
@@ -223,12 +182,8 @@ func (h *toolHost) SpawnProcess(callerID string, args mcp.SpawnProcessArgs) (mcp
return mcp.ProcessInfo{}, mcp.Errorf(mcp.ErrorKindInvalidKind, "spawn_process: kind must be 'command' or 'terminal'") return mcp.ProcessInfo{}, mcp.Errorf(mcp.ErrorKindInvalidKind, "spawn_process: kind must be 'command' or 'terminal'")
} }
env := h.mergeEnv(args.Env) env := h.mergeEnv(args.Env)
ctx := LaunchContext{ParentID: callerID, WorkDir: args.WorkingDir}
if ti, ok := h.CallerTask(callerID); ok {
ctx.TaskID = ti.ID
}
if args.Kind == "terminal" { if args.Kind == "terminal" {
c, err := h.launcher.LaunchTerminal(args.Argv, h.terminalName(args.Name), ctx, env) c, err := h.launcher.LaunchTerminal(args.Argv, h.terminalName(args.Name), callerID, args.WorkingDir, env)
if err != nil { if err != nil {
return mcp.ProcessInfo{}, err return mcp.ProcessInfo{}, err
} }
@@ -249,7 +204,7 @@ func (h *toolHost) SpawnProcess(callerID string, args mcp.SpawnProcessArgs) (mcp
if display == "" { if display == "" {
display = ps.Name display = ps.Name
} }
c, err := h.launcher.LaunchCommandPreset(ps, display, ctx) c, err := h.launcher.LaunchCommandPreset(ps, display, callerID)
if err != nil { if err != nil {
return mcp.ProcessInfo{}, err return mcp.ProcessInfo{}, err
} }
@@ -263,7 +218,7 @@ func (h *toolHost) SpawnProcess(callerID string, args mcp.SpawnProcessArgs) (mcp
if display == "" { if display == "" {
display = args.Argv[0] display = args.Argv[0]
} }
c, err := h.launcher.LaunchCommandArgv(args.Argv, display, ctx, env, args.Shell) c, err := h.launcher.LaunchCommandArgv(args.Argv, display, callerID, args.WorkingDir, env, args.Shell)
if err != nil { if err != nil {
return mcp.ProcessInfo{}, err return mcp.ProcessInfo{}, err
} }
@@ -398,64 +353,39 @@ func (h *toolHost) GetProcessStatus(callerID, processID string) (mcp.ProcessStat
return st, nil return st, nil
} }
func (h *toolHost) GetProjectStatus(callerID string, includeTools bool) (mcp.ProjectStatus, error) { func (h *toolHost) GetProjectStatus(callerID string) (mcp.ProjectStatus, error) {
caller := h.WhoAmI(callerID, includeTools) caller := h.WhoAmI(callerID)
processes := h.ListProcesses(callerID, "") processes := h.ListProcesses(callerID, "")
pads, _ := h.pads.List() pads, _ := h.pads.List()
status := mcp.ProjectStatus{ return mcp.ProjectStatus{
Project: caller.Project, Project: caller.Project,
Caller: caller, Caller: caller,
Processes: processes, Processes: processes,
Scratchpads: pads, Scratchpads: pads,
} }, nil
if caller.Task != nil {
status.Task = caller.Task
}
return status, nil
} }
func (h *toolHost) GetProcessOutput(callerID string, args mcp.ProcessOutputArgs) (mcp.ProcessOutput, error) { func (h *toolHost) GetProcessOutput(callerID, processID, mode string, sinceOffset int64) (mcp.ProcessOutput, error) {
processID, mode, sinceOffset := args.ProcessID, args.Mode, args.SinceOffset
c := h.sess.FindChild(processID) c := h.sess.FindChild(processID)
if c == nil { if c == nil {
return mcp.ProcessOutput{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID) return mcp.ProcessOutput{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
} }
if mode == "" {
mode = "grid"
}
if args.Raw {
b, end := c.StreamRead(sinceOffset)
content, contentBytes, truncated, truncatedBytes := capBytesTail(b, capLimit(args.MaxBytes, defaultMCPContentBytes))
return mcp.ProcessOutput{
Content: content,
Mode: "stream",
NewOffset: end,
Status: string(c.Status()),
ContentBytes: contentBytes,
Truncated: truncated,
TruncatedBytes: truncatedBytes,
}, nil
}
out := mcp.ProcessOutput{ out := mcp.ProcessOutput{
Mode: mode, Mode: mode,
IdleMS: c.IdleMS(),
Status: string(c.Status()), Status: string(c.Status()),
Canonicalized: true, ScreenVersion: c.ScreenVersion(),
} }
if args.IncludeMeta {
out.IdleMS = c.IdleMS()
out.ScreenVersion = c.ScreenVersion()
if em := c.Emulator(); em != nil { if em := c.Emulator(); em != nil {
if sc, err := em.ActiveScreen(); err == nil { if sc, err := em.ActiveScreen(); err == nil {
out.ActiveScreen = activeScreenName(sc) out.ActiveScreen = activeScreenName(sc)
} }
if cur, err := em.Cursor(); err == nil { if cur, err := em.Cursor(); err == nil {
out.Cursor = &mcp.Cursor{X: int(cur.Col), Y: int(cur.Row)} out.Cursor = mcp.Cursor{X: int(cur.Col), Y: int(cur.Row)}
} }
cols, rows := em.Size() cols, rows := em.Size()
out.Cols, out.Rows = int(cols), int(rows) out.Cols, out.Rows = int(cols), int(rows)
} }
}
maxLines := canonicalLineLimit(args.MaxLines)
switch mode { switch mode {
case "grid": case "grid":
em := c.Emulator() em := c.Emulator()
@@ -469,21 +399,11 @@ func (h *toolHost) GetProcessOutput(callerID string, args mcp.ProcessOutputArgs)
if c.Kind == KindAgent { if c.Kind == KindAgent {
txt = applyChromeTrim(txt, h.chromeHintsFor(c.PresetRef)) txt = applyChromeTrim(txt, h.chromeHintsFor(c.PresetRef))
} }
content, lineTruncated, lineDroppedBytes := canonicalizeTerminalText(txt, maxLines) out.Content = normalizeGridText(txt)
out.Content, out.ContentBytes, out.Truncated, out.TruncatedBytes = capTextMiddle(content, capLimit(args.MaxBytes, defaultMCPContentBytes))
if lineTruncated {
out.Truncated = true
out.TruncatedBytes += lineDroppedBytes
}
return out, nil return out, nil
case "stream": case "stream":
b, end := c.StreamRead(sinceOffset) b, end := c.StreamRead(sinceOffset)
content, lineTruncated, lineDroppedBytes := canonicalizeTerminalText(string(b), maxLines) out.Content = string(stripANSIBytes(nil, b))
out.Content, out.ContentBytes, out.Truncated, out.TruncatedBytes = capTextTail(content, capLimit(args.MaxBytes, defaultMCPContentBytes))
if lineTruncated {
out.Truncated = true
out.TruncatedBytes += lineDroppedBytes
}
out.NewOffset = end out.NewOffset = end
return out, nil return out, nil
default: default:
@@ -491,46 +411,34 @@ func (h *toolHost) GetProcessOutput(callerID string, args mcp.ProcessOutputArgs)
} }
} }
func (h *toolHost) GetProcessRawOutput(callerID string, args mcp.RawOutputArgs) (mcp.RawOutput, error) { func (h *toolHost) GetProcessRawOutput(callerID, processID string, sinceOffset int64) (mcp.RawOutput, error) {
c := h.sess.FindChild(args.ProcessID) c := h.sess.FindChild(processID)
if c == nil { if c == nil {
return mcp.RawOutput{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", args.ProcessID) return mcp.RawOutput{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
} }
b, end := c.StreamRead(args.SinceOffset) b, end := c.StreamRead(sinceOffset)
content, contentBytes, truncated, truncatedBytes := capBytesTail(b, capLimit(args.MaxBytes, defaultMCPContentBytes))
return mcp.RawOutput{ return mcp.RawOutput{
Content: content, Content: string(b),
NewOffset: end, NewOffset: end,
Status: string(c.Status()), Status: string(c.Status()),
ContentBytes: contentBytes,
Truncated: truncated,
TruncatedBytes: truncatedBytes,
}, nil }, nil
} }
func (h *toolHost) SearchOutput(callerID string, args mcp.SearchOutputArgs) (mcp.SearchResult, error) { func (h *toolHost) SearchOutput(callerID, processID, pattern, kind string, limit int) (mcp.SearchResult, error) {
c := h.sess.FindChild(args.ProcessID) c := h.sess.FindChild(processID)
if c == nil { if c == nil {
return mcp.SearchResult{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", args.ProcessID) return mcp.SearchResult{}, mcp.Errorf(mcp.ErrorKindNotFound, "no such process %q", processID)
} }
re, err := regexp.Compile(args.Pattern) re, err := regexp.Compile(pattern)
if err != nil { if err != nil {
return mcp.SearchResult{}, mcp.Errorf(mcp.ErrorKindInvalidArgs, "regex: %v", err) return mcp.SearchResult{}, mcp.Errorf(mcp.ErrorKindInvalidArgs, "regex: %v", err)
} }
b, _ := c.StreamRead(0) b, _ := c.StreamRead(0)
if args.Kind == "rendered" { if kind == "rendered" {
b = stripANSIBytes(nil, b) b = stripANSIBytes(nil, b)
} }
text := string(b) text := string(b)
lines := strings.Split(text, "\n") lines := strings.Split(text, "\n")
limit := args.Limit
if limit <= 0 {
limit = 10
}
if limit > maxSearchMatches {
limit = maxSearchMatches
}
lineLimit := capLimit(args.MaxBytes, defaultSearchLineBytes)
matches := make([]mcp.SearchMatch, 0, limit) matches := make([]mcp.SearchMatch, 0, limit)
truncated := false truncated := false
for i, line := range lines { for i, line := range lines {
@@ -539,8 +447,6 @@ func (h *toolHost) SearchOutput(callerID string, args mcp.SearchOutputArgs) (mcp
truncated = true truncated = true
break break
} }
line, _, lineTruncated, _ := capTextTail(line, lineLimit)
truncated = truncated || lineTruncated
matches = append(matches, mcp.SearchMatch{LineNo: i + 1, Text: line}) matches = append(matches, mcp.SearchMatch{LineNo: i + 1, Text: line})
} }
} }
@@ -682,7 +588,6 @@ func (h *toolHost) SendInput(callerID string, args mcp.SendInputArgs) (mcp.SendI
if err != nil { if err != nil {
return mcp.SendInputResult{}, err return mcp.SendInputResult{}, err
} }
tailSince := c.StreamOffset()
if err := c.InjectAsOrchestrator(payload); err != nil { if err := c.InjectAsOrchestrator(payload); err != nil {
return mcp.SendInputResult{}, err return mcp.SendInputResult{}, err
} }
@@ -694,12 +599,7 @@ func (h *toolHost) SendInput(callerID string, args mcp.SendInputArgs) (mcp.SendI
} }
if mode != "none" { if mode != "none" {
time.Sleep(time.Duration(args.WaitMS) * time.Millisecond) time.Sleep(time.Duration(args.WaitMS) * time.Millisecond)
tail, err := h.GetProcessOutput(callerID, mcp.ProcessOutputArgs{ tail, err := h.GetProcessOutput(callerID, args.ProcessID, mode, 0)
ProcessID: args.ProcessID,
Mode: mode,
SinceOffset: tailSince,
MaxBytes: capLimit(args.TailMaxBytes, defaultMCPTailBytes),
})
if err == nil { if err == nil {
res.Tail = &tail res.Tail = &tail
} }
@@ -911,35 +811,13 @@ func (h *toolHost) TimerList(callerID string) ([]mcp.TimerInfo, error) {
// Scratchpads / Meta // 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(args mcp.ScratchpadReadArgs) (mcp.ScratchpadReadResult, error) { func (h *toolHost) ScratchpadRead(_ string, name string) (string, string, error) {
content, rev, err := h.pads.Read(args.Name) return h.pads.Read(name)
if err != nil {
return mcp.ScratchpadReadResult{}, err
}
offset := args.Offset
if offset < 0 {
offset = 0
}
if offset > len(content) {
offset = len(content)
}
limited, contentBytes, truncated, truncatedBytes := capTextHead(content[offset:], capLimit(args.MaxBytes, defaultScratchpadReadBytes))
next := offset + contentBytes
return mcp.ScratchpadReadResult{
Content: limited,
Revision: rev,
Offset: offset,
NextOffset: next,
ContentBytes: contentBytes,
TotalBytes: len(content),
Truncated: truncated,
TruncatedBytes: truncatedBytes,
}, nil
} }
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) rev, err := h.pads.Write(name, content, expectedRevision)
if err == nil && h.scratch != nil { if err == nil && h.scratch != nil {
h.scratch.scratchpadsChanged() h.scratch.scratchpadsChanged()
@@ -947,7 +825,7 @@ func (h *toolHost) ScratchpadWrite(name, content, expectedRevision string) (stri
return rev, err 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) err := h.pads.Append(name, content)
if err == nil && h.scratch != nil { if err == nil && h.scratch != nil {
h.scratch.scratchpadsChanged() h.scratch.scratchpadsChanged()
@@ -955,7 +833,7 @@ func (h *toolHost) ScratchpadAppend(name, content string) error {
return err return err
} }
func (h *toolHost) ScratchpadDelete(name string) error { func (h *toolHost) ScratchpadDelete(_, name string) error {
err := h.pads.Delete(name) err := h.pads.Delete(name)
if err == nil && h.scratch != nil { if err == nil && h.scratch != nil {
h.scratch.scratchpadsChanged() h.scratch.scratchpadsChanged()
@@ -963,59 +841,15 @@ func (h *toolHost) ScratchpadDelete(name string) error {
return err return err
} }
func (h *toolHost) RegisterTaskWorktree(callerID string, args mcp.TaskRegisterWorktreeArgs) (mcp.TaskInfo, error) { func (h *toolHost) WhoAmI(callerID string) mcp.WhoAmI {
current, ok := h.CallerTask(callerID)
if !ok {
return mcp.TaskInfo{}, mcp.Errorf(mcp.ErrorKindRoleForbidden, "task_register_worktree: caller is not attached to a task")
}
if h.tasks == nil {
return mcp.TaskInfo{}, mcp.Errorf(mcp.ErrorKindNotFound, "task_register_worktree: task store unavailable")
}
path := strings.TrimSpace(args.Path)
if path == "" {
return mcp.TaskInfo{}, mcp.Errorf(mcp.ErrorKindInvalidArgs, "task_register_worktree: path required")
}
if !filepath.IsAbs(path) {
base := h.sess.projectDir
if c := h.sess.FindChild(callerID); c != nil && c.WorkDir != "" {
base = c.WorkDir
}
path = filepath.Join(base, path)
}
abs, err := filepath.Abs(path)
if err != nil {
return mcp.TaskInfo{}, err
}
updated, err := h.tasks.RegisterWorktree(current.ID, task.Worktree{
Path: abs,
Branch: args.Branch,
CreatedByProcessID: callerID,
})
if err != nil {
return mcp.TaskInfo{}, err
}
if h.taskUI != nil {
h.taskUI.tasksChanged()
}
return taskInfoOf(updated), nil
}
func (h *toolHost) WhoAmI(callerID string, includeTools bool) mcp.WhoAmI {
role := h.CallerRole(callerID)
taskInfo, taskBound := h.CallerTask(callerID)
w := mcp.WhoAmI{ w := mcp.WhoAmI{
ProcessID: callerID, ProcessID: callerID,
Role: role, Role: h.CallerRole(callerID),
Project: mcp.ProjectMeta{ Project: mcp.ProjectMeta{
Path: h.sess.projectDir, Path: h.sess.projectDir,
Key: h.sess.projectKey, Key: h.sess.projectKey,
}, },
} AvailableTools: availableToolsForRole(h.CallerRole(callerID)),
if taskBound {
w.Task = &taskInfo
}
if includeTools {
w.AvailableTools = availableToolsForRole(role, taskBound)
} }
if c := h.sess.FindChild(callerID); c != nil { if c := h.sess.FindChild(callerID); c != nil {
w.Name = c.DisplayName() w.Name = c.DisplayName()
@@ -1056,27 +890,6 @@ func (h *toolHost) processInfoOf(c *Child) mcp.ProcessInfo {
return info return info
} }
func taskInfoOf(t task.Task) mcp.TaskInfo {
out := mcp.TaskInfo{
ID: t.ID,
Title: t.Title,
CreatedAt: t.CreatedAt,
UpdatedAt: t.UpdatedAt,
}
if len(t.Worktrees) > 0 {
out.Worktrees = make([]mcp.TaskWorktree, 0, len(t.Worktrees))
for _, wt := range t.Worktrees {
out.Worktrees = append(out.Worktrees, mcp.TaskWorktree{
Path: wt.Path,
Branch: wt.Branch,
CreatedByProcessID: wt.CreatedByProcessID,
RegisteredAt: wt.RegisteredAt,
})
}
}
return out
}
func (h *toolHost) chromeHintsFor(presetName string) []string { func (h *toolHost) chromeHintsFor(presetName string) []string {
if presetName == "" { if presetName == "" {
return nil return nil
@@ -1135,25 +948,23 @@ func (h *toolHost) askForTrust(callerID, presetName, reason string) {
h.prompter.promptTrust(callerID, presetName, reason) h.prompter.promptTrust(callerID, presetName, reason)
} }
// buildAgentPrompt prepends one-line orientation blocks to the initial // wrapSubAgentPrompt prepends a one-line orientation block to the
// prompt. The blocks are single-line on purpose: writeInput splits on // caller-supplied agent_instructions. patterm injects nothing on its
// CR/LF, so any embedded newline would submit prematurely. // own (SPEC §7), but vendor TUIs that learn their role purely from
func buildAgentPrompt(instructions string, hasParent bool, taskInfo *mcp.TaskInfo) string { // their first turn need to be told they're a sub-agent — otherwise
var parts []string // they finish without reporting back to the parent or cleaning up
if hasParent && (instructions != "" || taskInfo != nil) { // processes/scratchpads they spawned. The block is single-line on
parts = append(parts, "[system: you are a patterm sub-agent. When your work is done, call send_message to your parent (use whoami to get parent_process_id) with a summary, and close_process / scratchpad cleanup anything you created. See help('conventions').]") // purpose: writeInput splits on CR/LF, so any embedded newline would
// submit prematurely.
func wrapSubAgentPrompt(instructions string, hasParent bool) string {
if !hasParent {
return instructions
} }
if taskInfo != nil { if instructions == "" {
parts = append(parts, fmt.Sprintf("[system: you are working on patterm task %q (%s). If you create or use git worktrees for this task, call task_register_worktree with the path and branch.]", sanitizePromptText(taskInfo.Title), sanitizePromptText(taskInfo.ID))) return ""
} }
if instructions != "" { const preface = "[system: you are a patterm sub-agent. When your work is done, call send_message to your parent (use whoami to get parent_process_id) with a summary, and close_process / scratchpad cleanup anything you created. See help('conventions').] "
parts = append(parts, instructions) return preface + instructions
}
return strings.Join(parts, " ")
}
func sanitizePromptText(s string) string {
return strings.NewReplacer("\r", " ", "\n", " ", `"`, "'").Replace(s)
} }
// applyChromeTrim deletes lines matching any of the given regexes. // applyChromeTrim deletes lines matching any of the given regexes.
@@ -1198,10 +1009,11 @@ func activeScreenName(s pkgvt.Screen) string {
} }
} }
// ansiRegexp strips CSI/OSC escape sequences and common single-character // ansiRegexp strips CSI escape sequences and common single-character
// controls from the stream. The vt emulator already handles full // controls (BEL, OSC terminators) from the stream. The vt emulator
// rendering for grid mode; this is only for stream-mode text output. // already handles full rendering for grid mode; this is only for
var ansiRegexp = regexp.MustCompile(`\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]|\x1b[\x40-\x5f]|\x07`) // stream-mode ANSI-stripped output.
var ansiRegexp = regexp.MustCompile(`\x1b\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]|\x1b[\x40-\x5f]|\x07`)
func stripANSI(s string) string { func stripANSI(s string) string {
return ansiRegexp.ReplaceAllString(s, "") return ansiRegexp.ReplaceAllString(s, "")
@@ -1231,68 +1043,12 @@ func normalizeGridText(s string) string {
return strings.Join(out, "\n") return strings.Join(out, "\n")
} }
func capLimit(requested, def int) int {
if requested <= 0 {
requested = def
}
if requested > maxMCPContentBytes {
requested = maxMCPContentBytes
}
if requested < 0 {
return 0
}
return requested
}
func canonicalLineLimit(requested int) int {
if requested <= 0 {
return defaultMCPCanonicalLines
}
if requested > maxMCPCanonicalLines {
return maxMCPCanonicalLines
}
return requested
}
func capBytesTail(b []byte, limit int) (string, int, bool, int) {
if limit <= 0 || len(b) <= limit {
return string(b), len(b), false, 0
}
dropped := len(b) - limit
return string(b[dropped:]), limit, true, dropped
}
func capTextTail(s string, limit int) (string, int, bool, int) {
return capBytesTail([]byte(s), limit)
}
func capTextHead(s string, limit int) (string, int, bool, int) {
if limit <= 0 || len(s) <= limit {
return s, len(s), false, 0
}
return s[:limit], limit, true, len(s) - limit
}
func capTextMiddle(s string, limit int) (string, int, bool, int) {
if limit <= 0 || len(s) <= limit {
return s, len(s), false, 0
}
const marker = "\n...[truncated]...\n"
if limit <= len(marker)+2 {
return s[len(s)-limit:], limit, true, len(s) - limit
}
head := (limit - len(marker)) / 2
tail := limit - len(marker) - head
return s[:head] + marker + s[len(s)-tail:], limit, true, len(s) - limit
}
// stripANSIBytes is the byte-slice form of stripANSI. Skips the // stripANSIBytes is the byte-slice form of stripANSI. Skips the
// string conversion and the regex DFA — useful when the caller will // string conversion and the regex DFA — useful when the caller will
// itself walk the result line-by-line (SearchOutput) or feed it to a // itself walk the result line-by-line (SearchOutput) or feed it to a
// pattern match (WaitForPattern scrollback). Recognises the same // pattern match (WaitForPattern scrollback). Recognises the same
// shapes the regex did: // shapes the regex did:
// - `\x1b[ <params> <intermediate> <final-byte>` (CSI / SGR) // - `\x1b[ <params> <intermediate> <final-byte>` (CSI / SGR)
// - `\x1b] ... (BEL|ST)` (OSC)
// - `\x1b<final-byte>` for `@..._` (one-byte escapes) // - `\x1b<final-byte>` for `@..._` (one-byte escapes)
// - `\x07` (BEL) // - `\x07` (BEL)
// //
@@ -1322,24 +1078,6 @@ func stripANSIBytes(dst, src []byte) []byte {
continue continue
} }
next := src[i+1] next := src[i+1]
if next == ']' {
j := i + 2
for j < len(src) {
if src[j] == 0x07 {
i = j + 1
break
}
if src[j] == 0x1b && j+1 < len(src) && src[j+1] == '\\' {
i = j + 2
break
}
j++
}
if j >= len(src) {
i = len(src)
}
continue
}
if next != '[' { if next != '[' {
// One-byte ESC sequence (`\x1b<final>` where final is // One-byte ESC sequence (`\x1b<final>` where final is
// `@..._` per the regex; we drop anything that follows). // `@..._` per the regex; we drop anything that follows).
@@ -1376,7 +1114,7 @@ func stripANSIBytes(dst, src []byte) []byte {
// availableToolsForRole — SPEC §7 whoami exposes the list a caller can // availableToolsForRole — SPEC §7 whoami exposes the list a caller can
// invoke from its current role. Sub-agents lose `spawn_agent` (§8 // invoke from its current role. Sub-agents lose `spawn_agent` (§8
// two-level-tree rule). // two-level-tree rule).
func availableToolsForRole(role mcp.CallerRole, taskBound bool) []string { func availableToolsForRole(role mcp.CallerRole) []string {
tools := []string{ tools := []string{
"spawn_process", "start_process", "restart_process", "stop_process", "spawn_process", "start_process", "restart_process", "stop_process",
"close_process", "rename_process", "select_process", "close_process", "rename_process", "select_process",
@@ -1392,9 +1130,6 @@ func availableToolsForRole(role mcp.CallerRole, taskBound bool) []string {
if role == mcp.RoleOrchestrator { if role == mcp.RoleOrchestrator {
tools = append([]string{"spawn_agent"}, tools...) tools = append([]string{"spawn_agent"}, tools...)
} }
if taskBound {
tools = append(tools, "task_register_worktree")
}
return tools return tools
} }
@@ -1425,7 +1160,7 @@ func helpFor(topic string) mcp.HelpResponse {
case "inspection": case "inspection":
return mcp.HelpResponse{ return mcp.HelpResponse{
Topic: "inspection", Topic: "inspection",
Content: "get_process_output gives you canonical terminal text by default: the visible pane (grid mode) or recent stream text from since_offset (stream mode), with ANSI/control noise, borders, duplicate status churn, and volatile timers removed. Use raw:true only when you need diagnostic PTY bytes; include_meta:true restores cursor, geometry, and screen-version fields. list_processes is for the whole session. get_project_status batches everything you need to orient yourself.", Content: "get_process_output gives you the visible pane (grid mode) or a byte slice from since_offset (stream mode). list_processes is for the whole session. get_project_status batches everything you need to orient yourself.",
RelatedTools: []string{"list_processes", "get_process_status", "get_process_output", "search_output", "wait_for_pattern", "get_project_status"}, RelatedTools: []string{"list_processes", "get_process_status", "get_process_output", "search_output", "wait_for_pattern", "get_project_status"},
} }
case "io": case "io":
+7 -136
View File
@@ -1,14 +1,10 @@
package app package app
import ( import (
"path/filepath"
"strings" "strings"
"testing" "testing"
"github.com/hjbdev/patterm/internal/mcp" "github.com/hjbdev/patterm/internal/mcp"
"github.com/hjbdev/patterm/internal/preset"
"github.com/hjbdev/patterm/internal/scratchpad"
taskstore "github.com/hjbdev/patterm/internal/task"
) )
// mkChild builds a Child without starting a PTY. Use sparingly — the // mkChild builds a Child without starting a PTY. Use sparingly — the
@@ -103,8 +99,8 @@ func TestClassifySendMessageNilCallerRejectsNonTopLevelTarget(t *testing.T) {
} }
} }
func TestBuildAgentPromptPrependsSystemBlockWhenParented(t *testing.T) { func TestWrapSubAgentPromptPrependsSystemBlockWhenParented(t *testing.T) {
out := buildAgentPrompt("ship feature X", true, nil) out := wrapSubAgentPrompt("ship feature X", true)
if !strings.HasPrefix(out, "[system:") { if !strings.HasPrefix(out, "[system:") {
t.Fatalf("expected prepended [system: …] block, got %q", out) t.Fatalf("expected prepended [system: …] block, got %q", out)
} }
@@ -122,58 +118,22 @@ func TestBuildAgentPromptPrependsSystemBlockWhenParented(t *testing.T) {
} }
} }
func TestBuildAgentPromptPassthroughWhenNoParent(t *testing.T) { func TestWrapSubAgentPromptPassthroughWhenNoParent(t *testing.T) {
out := buildAgentPrompt("hello", false, nil) out := wrapSubAgentPrompt("hello", false)
if out != "hello" { if out != "hello" {
t.Fatalf("expected passthrough for top-level spawn, got %q", out) t.Fatalf("expected passthrough for top-level spawn, got %q", out)
} }
} }
func TestBuildAgentPromptEmptyStaysEmpty(t *testing.T) { func TestWrapSubAgentPromptEmptyStaysEmpty(t *testing.T) {
// Empty instructions mean "no inject" upstream; we must not synthesize // Empty instructions mean "no inject" upstream; we must not synthesize
// content here or LaunchAgent would type the system block into an // content here or LaunchAgent would type the system block into an
// otherwise-idle agent. // otherwise-idle agent.
if out := buildAgentPrompt("", true, nil); out != "" { if out := wrapSubAgentPrompt("", true); out != "" {
t.Fatalf("empty instructions should stay empty, got %q", out) t.Fatalf("empty instructions should stay empty, got %q", out)
} }
} }
func TestMCPContentCapsPreferRecentStreamBytes(t *testing.T) {
got, gotBytes, truncated, dropped := capBytesTail([]byte("abcdefghijklmnop"), 6)
if got != "klmnop" || gotBytes != 6 || !truncated || dropped != 10 {
t.Fatalf("capBytesTail = (%q, %d, %v, %d)", got, gotBytes, truncated, dropped)
}
}
func TestMCPGridCapKeepsHeadAndTail(t *testing.T) {
got, gotBytes, truncated, dropped := capTextMiddle("abcdefghijklmnopqrstuvwxyz", 24)
if gotBytes != 24 || !truncated || dropped != 2 {
t.Fatalf("capTextMiddle metadata = (%d, %v, %d), content %q", gotBytes, truncated, dropped, got)
}
if !strings.Contains(got, "...[truncated]...") {
t.Fatalf("capTextMiddle missing marker: %q", got)
}
}
func TestScratchpadReadPagesLargeContent(t *testing.T) {
t.Setenv("XDG_DATA_HOME", t.TempDir())
store, err := scratchpad.Open("test-project")
if err != nil {
t.Fatalf("scratchpad open: %v", err)
}
if _, err := store.Write("notes.md", "abcdefghijklmnopqrstuvwxyz", ""); err != nil {
t.Fatalf("scratchpad write: %v", err)
}
h := &toolHost{pads: store}
res, err := h.ScratchpadRead(mcp.ScratchpadReadArgs{Name: "notes.md", Offset: 5, MaxBytes: 7})
if err != nil {
t.Fatalf("ScratchpadRead: %v", err)
}
if res.Content != "fghijkl" || !res.Truncated || res.NextOffset != 12 || res.TotalBytes != 26 {
t.Fatalf("ScratchpadRead result = %+v", res)
}
}
func TestHelpLifecycleTopicCoversCleanup(t *testing.T) { func TestHelpLifecycleTopicCoversCleanup(t *testing.T) {
resp := helpFor("lifecycle") resp := helpFor("lifecycle")
if resp.Topic != "lifecycle" { if resp.Topic != "lifecycle" {
@@ -218,7 +178,7 @@ func TestAvailableToolsAdvertisesAllTimerTools(t *testing.T) {
"timer_cancel", "timer_pause", "timer_resume", "timer_list", "timer_cancel", "timer_pause", "timer_resume", "timer_list",
} }
for _, role := range []mcp.CallerRole{mcp.RoleOrchestrator, mcp.RoleSubAgent} { for _, role := range []mcp.CallerRole{mcp.RoleOrchestrator, mcp.RoleSubAgent} {
tools := availableToolsForRole(role, false) tools := availableToolsForRole(role)
for _, w := range want { for _, w := range want {
if !containsString(tools, w) { if !containsString(tools, w) {
t.Fatalf("role %q missing %q in available tools: %v", role, w, tools) t.Fatalf("role %q missing %q in available tools: %v", role, w, tools)
@@ -227,95 +187,6 @@ func TestAvailableToolsAdvertisesAllTimerTools(t *testing.T) {
} }
} }
func TestWhoAmITaskContextIsConditional(t *testing.T) {
t.Setenv("XDG_DATA_HOME", t.TempDir())
tasks, err := taskstore.Open("projkey")
if err != nil {
t.Fatalf("task open: %v", err)
}
task, err := tasks.Create("Fix sidebar")
if err != nil {
t.Fatalf("task create: %v", err)
}
sess := NewSession(t.TempDir(), "projkey")
unbound := newChildEntry("p_unbound", "agent", KindAgent, []string{"sh"}, nil, "", "", "", "")
bound := newChildEntry("p_bound", "agent", KindAgent, []string{"sh"}, nil, "", task.ID, "", "")
addTestChild(sess, unbound)
addTestChild(sess, bound)
h := newToolHost(sess, nil, tasks, nil, preset.Set{}, nil, 80, 24)
if got := h.WhoAmI(unbound.ID, true); got.Task != nil || containsString(got.AvailableTools, "task_register_worktree") {
t.Fatalf("unbound whoami leaked task context/tools: %+v", got)
}
got := h.WhoAmI(bound.ID, true)
if got.Task == nil || got.Task.ID != task.ID || got.Task.Title != task.Title {
t.Fatalf("bound whoami missing task: %+v", got)
}
if !containsString(got.AvailableTools, "task_register_worktree") {
t.Fatalf("bound whoami missing task_register_worktree: %+v", got.AvailableTools)
}
}
func TestRegisterTaskWorktreeResolvesAgainstCallerWorkDir(t *testing.T) {
t.Setenv("XDG_DATA_HOME", t.TempDir())
tasks, err := taskstore.Open("projkey")
if err != nil {
t.Fatalf("task open: %v", err)
}
task, err := tasks.Create("Fix sidebar")
if err != nil {
t.Fatalf("task create: %v", err)
}
workDir := t.TempDir()
sess := NewSession(t.TempDir(), "projkey")
caller := newChildEntry("p_bound", "agent", KindAgent, []string{"sh"}, nil, "", task.ID, workDir, "")
addTestChild(sess, caller)
h := newToolHost(sess, nil, tasks, nil, preset.Set{}, nil, 80, 24)
info, err := h.RegisterTaskWorktree(caller.ID, mcp.TaskRegisterWorktreeArgs{Path: "../worktree", Branch: "task-branch"})
if err != nil {
t.Fatalf("register: %v", err)
}
wantPath := filepath.Clean(filepath.Join(workDir, "../worktree"))
if len(info.Worktrees) != 1 || info.Worktrees[0].Path != wantPath || info.Worktrees[0].Branch != "task-branch" || info.Worktrees[0].CreatedByProcessID != caller.ID {
t.Fatalf("registered worktree = %+v, want path %q branch/task", info.Worktrees, wantPath)
}
}
func TestSpawnProcessInheritsCallerTask(t *testing.T) {
t.Setenv("XDG_DATA_HOME", t.TempDir())
tasks, err := taskstore.Open("projkey")
if err != nil {
t.Fatalf("task open: %v", err)
}
task, err := tasks.Create("Fix sidebar")
if err != nil {
t.Fatalf("task create: %v", err)
}
sess := NewSession(t.TempDir(), "projkey")
defer sess.Shutdown()
caller := newChildEntry("p_bound", "agent", KindAgent, []string{"sh"}, nil, "", task.ID, "", "")
addTestChild(sess, caller)
launcher := NewLauncher(sess, "", 80, 24)
h := newToolHost(sess, nil, tasks, launcher, preset.Set{}, nil, 80, 24)
info, err := h.SpawnProcess(caller.ID, mcp.SpawnProcessArgs{Argv: []string{"sh", "-lc", "exit 0"}})
if err != nil {
t.Fatalf("spawn process: %v", err)
}
spawned := sess.FindChild(info.ID)
if spawned == nil || spawned.TaskID != task.ID {
t.Fatalf("spawned child task = %+v, want %q", spawned, task.ID)
}
}
func addTestChild(sess *Session, c *Child) {
sess.mu.Lock()
defer sess.mu.Unlock()
sess.children[c.ID] = c
sess.order = append(sess.order, c.ID)
}
// TestHelpTimersDocumentsAllTools mirrors the whoami check for the // TestHelpTimersDocumentsAllTools mirrors the whoami check for the
// help("timers") topic — the related-tools list must enumerate every // help("timers") topic — the related-tools list must enumerate every
// timer_* tool so callers reading help can dispatch them. // timer_* tool so callers reading help can dispatch them.
+13 -33
View File
@@ -24,12 +24,6 @@ type Launcher struct {
cols, rows uint16 cols, rows uint16
} }
type LaunchContext struct {
ParentID string
TaskID string
WorkDir string
}
func NewLauncher(sess *Session, mcpSocket string, cols, rows uint16) *Launcher { func NewLauncher(sess *Session, mcpSocket string, cols, rows uint16) *Launcher {
bin, err := os.Executable() bin, err := os.Executable()
if err != nil { if err != nil {
@@ -53,7 +47,7 @@ func (l *Launcher) size() (uint16, uint16) {
// LaunchAgent spawns the agent preset, applies the preset's MCP // LaunchAgent spawns the agent preset, applies the preset's MCP
// injection, waits for the ready signal, and types initial_prompt into // injection, waits for the ready signal, and types initial_prompt into
// the PTY. SPEC §7 spawn_agent, §8 conversation protocol. // the PTY. SPEC §7 spawn_agent, §8 conversation protocol.
func (l *Launcher) LaunchAgent(p *preset.Preset, displayName, initialPrompt string, ctx LaunchContext) (*Child, error) { func (l *Launcher) LaunchAgent(p *preset.Preset, displayName, initialPrompt, parentID string) (*Child, error) {
if p.Kind != preset.KindAgent { if p.Kind != preset.KindAgent {
return nil, fmt.Errorf("launch: %q is not an agent preset", p.Name) return nil, fmt.Errorf("launch: %q is not an agent preset", p.Name)
} }
@@ -137,9 +131,7 @@ func (l *Launcher) LaunchAgent(p *preset.Preset, displayName, initialPrompt stri
Argv: argv, Argv: argv,
Env: env, Env: env,
Name: displayName, Name: displayName,
ParentID: ctx.ParentID, ParentID: parentID,
TaskID: ctx.TaskID,
WorkDir: firstNonEmpty(ctx.WorkDir, p.WorkingDir),
PresetRef: p.Name, PresetRef: p.Name,
Identity: identity, Identity: identity,
CleanupPaths: cleanupPaths, CleanupPaths: cleanupPaths,
@@ -171,7 +163,7 @@ func (l *Launcher) LaunchAgent(p *preset.Preset, displayName, initialPrompt stri
// LaunchCommandPreset spawns a process preset as a SPEC §7 command // LaunchCommandPreset spawns a process preset as a SPEC §7 command
// entry. No MCP injection; just argv. The entry is session-persistent // entry. No MCP injection; just argv. The entry is session-persistent
// (survives PTY exit so it can be Restart'd). // (survives PTY exit so it can be Restart'd).
func (l *Launcher) LaunchCommandPreset(p *preset.Preset, displayName string, ctx LaunchContext) (*Child, error) { func (l *Launcher) LaunchCommandPreset(p *preset.Preset, displayName, parentID string) (*Child, error) {
if p.Kind != preset.KindCommand { if p.Kind != preset.KindCommand {
return nil, fmt.Errorf("launch: %q is not a command preset", p.Name) return nil, fmt.Errorf("launch: %q is not a command preset", p.Name)
} }
@@ -185,9 +177,8 @@ func (l *Launcher) LaunchCommandPreset(p *preset.Preset, displayName string, ctx
Argv: p.ResolvedArgv(), Argv: p.ResolvedArgv(),
Env: env, Env: env,
Name: displayName, Name: displayName,
ParentID: ctx.ParentID, ParentID: parentID,
TaskID: ctx.TaskID, WorkDir: p.WorkingDir,
WorkDir: firstNonEmpty(ctx.WorkDir, p.WorkingDir),
PresetRef: p.Name, PresetRef: p.Name,
IdleDetection: resolveIdleDetection(p.IdleDetection), IdleDetection: resolveIdleDetection(p.IdleDetection),
}, cols, rows) }, cols, rows)
@@ -200,7 +191,7 @@ func (l *Launcher) LaunchCommandPreset(p *preset.Preset, displayName string, ctx
// LaunchCommandArgv spawns a freeform-argv command entry. Trust gating // LaunchCommandArgv spawns a freeform-argv command entry. Trust gating
// (SPEC §7) lives one level up in toolHost — by the time we get here // (SPEC §7) lives one level up in toolHost — by the time we get here
// trust is settled (freeform argv is implicitly trusted). // trust is settled (freeform argv is implicitly trusted).
func (l *Launcher) LaunchCommandArgv(argv []string, displayName string, ctx LaunchContext, env []string, shell bool) (*Child, error) { func (l *Launcher) LaunchCommandArgv(argv []string, displayName, parentID, workDir string, env []string, shell bool) (*Child, error) {
if shell && len(argv) > 0 { if shell && len(argv) > 0 {
argv = []string{"sh", "-lc", strings.Join(argv, " ")} argv = []string{"sh", "-lc", strings.Join(argv, " ")}
} }
@@ -213,9 +204,8 @@ func (l *Launcher) LaunchCommandArgv(argv []string, displayName string, ctx Laun
Argv: argv, Argv: argv,
Env: env, Env: env,
Name: displayName, Name: displayName,
ParentID: ctx.ParentID, ParentID: parentID,
TaskID: ctx.TaskID, WorkDir: workDir,
WorkDir: ctx.WorkDir,
}, cols, rows) }, cols, rows)
} }
@@ -233,7 +223,7 @@ func (l *Launcher) RestoreCommand(e persist.Entry, presets preset.Set) (*Child,
if e.PresetRef != "" { if e.PresetRef != "" {
for _, p := range presets.Processes { for _, p := range presets.Processes {
if p.Name == e.PresetRef { if p.Name == e.PresetRef {
return l.LaunchCommandPreset(p, e.Name, LaunchContext{}) return l.LaunchCommandPreset(p, e.Name, "")
} }
} }
// Preset has been deleted since the entry was saved. Fall // Preset has been deleted since the entry was saved. Fall
@@ -243,12 +233,12 @@ func (l *Launcher) RestoreCommand(e persist.Entry, presets preset.Set) (*Child,
if len(e.Argv) == 0 { if len(e.Argv) == 0 {
return nil, fmt.Errorf("restore: entry %s has no argv", e.ID) return nil, fmt.Errorf("restore: entry %s has no argv", e.ID)
} }
return l.LaunchCommandArgv(e.Argv, e.Name, LaunchContext{WorkDir: e.WorkDir}, nil, false) return l.LaunchCommandArgv(e.Argv, e.Name, "", e.WorkDir, nil, false)
} }
// LaunchTerminal spawns a bare interactive shell. SPEC §7 kind=terminal. // LaunchTerminal spawns a bare interactive shell. SPEC §7 kind=terminal.
// argv defaults to $SHELL -i when empty. // argv defaults to $SHELL -i when empty.
func (l *Launcher) LaunchTerminal(argv []string, displayName string, ctx LaunchContext, env []string) (*Child, error) { func (l *Launcher) LaunchTerminal(argv []string, displayName, parentID, workDir string, env []string) (*Child, error) {
if len(argv) == 0 { if len(argv) == 0 {
sh := os.Getenv("SHELL") sh := os.Getenv("SHELL")
if sh == "" { if sh == "" {
@@ -265,21 +255,11 @@ func (l *Launcher) LaunchTerminal(argv []string, displayName string, ctx LaunchC
Argv: argv, Argv: argv,
Env: env, Env: env,
Name: displayName, Name: displayName,
ParentID: ctx.ParentID, ParentID: parentID,
TaskID: ctx.TaskID, WorkDir: workDir,
WorkDir: ctx.WorkDir,
}, cols, rows) }, cols, rows)
} }
func firstNonEmpty(values ...string) string {
for _, v := range values {
if v != "" {
return v
}
}
return ""
}
func (l *Launcher) writeMCPConfig(identity string) (string, error) { func (l *Launcher) writeMCPConfig(identity string) (string, error) {
dir, err := mcpRuntimeDir(identity) dir, err := mcpRuntimeDir(identity)
if err != nil { if err != nil {
+1 -1
View File
@@ -50,7 +50,7 @@ func TestSpawnSizingUsesViewportDimensions(t *testing.T) {
t.Fatalf("launcher size: got %dx%d want 91x36", cols, rows) t.Fatalf("launcher size: got %dx%d want 91x36", cols, rows)
} }
host := newToolHost(nil, nil, nil, nil, preset.Set{}, nil, l.childCols(), l.childRows()) host := newToolHost(nil, nil, nil, preset.Set{}, nil, l.childCols(), l.childRows())
cols, rows = host.size() cols, rows = host.size()
if cols != 91 || rows != 36 { if cols != 91 || rows != 36 {
t.Fatalf("tool host size: got %dx%d want 91x36", cols, rows) t.Fatalf("tool host size: got %dx%d want 91x36", cols, rows)
+62 -111
View File
@@ -7,7 +7,6 @@ import (
"unicode/utf8" "unicode/utf8"
"github.com/hjbdev/patterm/internal/preset" "github.com/hjbdev/patterm/internal/preset"
"github.com/hjbdev/patterm/internal/task"
) )
// paletteAction is what the palette returns when the user picks an item. // paletteAction is what the palette returns when the user picks an item.
@@ -36,14 +35,14 @@ type paletteAction struct {
// For pad-* actions, the scratchpad name to operate on. // For pad-* actions, the scratchpad name to operate on.
padName string padName string
// For task-* actions, the task to operate on. // For *-rename-submit actions, the user-typed new name.
taskID string
// For *-rename-submit and task-create-submit actions, the user-typed name.
newName string newName string
// For settings actions, the updated settings snapshot to persist. // For settings actions, the updated settings snapshot to persist.
settings *settings settings *settings
projectKey string
projectPath string
} }
// Group ids order the section bands the palette renders when no query // Group ids order the section bands the palette renders when no query
@@ -52,6 +51,7 @@ type paletteAction struct {
// an equally tight Spawn-section hit. // an equally tight Spawn-section hit.
const ( const (
groupFocused = iota groupFocused = iota
groupProject
groupOpen groupOpen
groupSpawn groupSpawn
groupSettings groupSettings
@@ -68,6 +68,14 @@ type paletteItem struct {
matches []int 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 // paletteMode toggles the palette between its fuzzy-picker UI and the
// freeform "spawn process" form. The form lives inside the palette so // freeform "spawn process" form. The form lives inside the palette so
// it shares the same modal-input contract (every byte intercepted; no // it shares the same modal-input contract (every byte intercepted; no
@@ -94,12 +102,12 @@ type spawnProcessForm struct {
// renameForm is a one-field inline form used by the "Rename scratchpad / // renameForm is a one-field inline form used by the "Rename scratchpad /
// agent / process" context palette entries. The submit action kind // agent / process" context palette entries. The submit action kind
// determines what gets renamed; the target name (pad name, task id, or child id) // determines what gets renamed; the target name (pad name or child id)
// is carried alongside so closePalette knows what to apply the new // is carried alongside so closePalette knows what to apply the new
// name to. // name to.
type renameForm struct { type renameForm struct {
name []rune name []rune
subject string // "pad" | "task" | "task-create" | "agent" | "proc" subject string // "pad" | "agent" | "proc"
target string // padName for "pad"; childID for "agent"/"proc" target string // padName for "pad"; childID for "agent"/"proc"
title string // e.g. "Rename" title string // e.g. "Rename"
subjectLine string // e.g. "scratchpad: notes.md" rendered above the input subjectLine string // e.g. "scratchpad: notes.md" rendered above the input
@@ -119,9 +127,6 @@ type paletteState struct {
children []*Child children []*Child
focused string focused string
focusedPad string focusedPad string
focusedTaskID string
tasksEnabled bool
tasks []task.Task
presets preset.Set presets preset.Set
settings settings settings settings
@@ -131,6 +136,8 @@ type paletteState struct {
form *spawnProcessForm form *spawnProcessForm
renameForm *renameForm renameForm *renameForm
settingsInput *settingsInputForm settingsInput *settingsInputForm
projects []paletteProject
currentProject string
// showHelp swaps the item list for a static keybinding cheat-sheet // showHelp swaps the item list for a static keybinding cheat-sheet
// until the next keystroke. Toggled by `?` in picker mode. // until the next keystroke. Toggled by `?` in picker mode.
@@ -142,9 +149,9 @@ type paletteState struct {
// macro is active. Typing `sw <query>` filters to switch entries only, // macro is active. Typing `sw <query>` filters to switch entries only,
// `k <query>` to close entries, `sp <query>` to spawn entries. // `k <query>` to close entries, `sp <query>` to spawn entries.
var macroPrefixes = map[string][]string{ var macroPrefixes = map[string][]string{
"sw": {"switch", "task-switch"}, "sw": {"switch"},
"k": {"kill", "agent-close", "proc-stop", "proc-delete"}, "k": {"kill", "agent-close", "proc-stop", "proc-delete"},
"sp": {"spawn-agent", "spawn-process", "spawn-terminal", "spawn-process-form", "task-start-agent"}, "sp": {"spawn-agent", "spawn-process", "spawn-terminal", "spawn-process-form"},
} }
// chipOrder is the cycle order for Tab / Shift-Tab when the user // chipOrder is the cycle order for Tab / Shift-Tab when the user
@@ -186,39 +193,22 @@ func findChildByID(children []*Child, id string) *Child {
return nil return nil
} }
func (p *paletteState) taskByID(id string) (task.Task, bool) {
if id == "" {
return task.Task{}, false
}
for _, t := range p.tasks {
if t.ID == id {
return t, true
}
}
return task.Task{}, false
}
func taskHint(t task.Task) string {
if len(t.Worktrees) == 0 {
return t.ID
}
return fmt.Sprintf("%s · %d worktrees", t.ID, len(t.Worktrees))
}
func newPalette(children []*Child, focused, focusedPad string, presets preset.Set, appSettings ...settings) *paletteState { func newPalette(children []*Child, focused, focusedPad string, presets preset.Set, appSettings ...settings) *paletteState {
return newPaletteWithTasks(children, focused, focusedPad, "", nil, presets, appSettings...)
}
func newPaletteWithTasks(children []*Child, focused, focusedPad, focusedTaskID string, tasks []task.Task, presets preset.Set, appSettings ...settings) *paletteState {
st := defaultSettings() st := defaultSettings()
if len(appSettings) > 0 { if len(appSettings) > 0 {
st = appSettings[0].clone() st = appSettings[0].clone()
} }
p := &paletteState{children: children, focused: focused, focusedPad: focusedPad, focusedTaskID: focusedTaskID, tasksEnabled: tasks != nil || focusedTaskID != "", tasks: tasks, presets: presets, settings: st} p := &paletteState{children: children, focused: focused, focusedPad: focusedPad, presets: presets, settings: st}
p.rebuild() p.rebuild()
return p 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() { func (p *paletteState) rebuild() {
// Macro is resolved on the *original-case* query; the returned rest // Macro is resolved on the *original-case* query; the returned rest
// keeps the user's casing intact (useful when Tab cycles chips). // keeps the user's casing intact (useful when Tab cycles chips).
@@ -289,21 +279,6 @@ func (p *paletteState) buildItems(macro string) []paletteItem {
paletteItem{label: "Delete", hint: "delete scratchpad · " + name, paletteItem{label: "Delete", hint: "delete scratchpad · " + name,
action: paletteAction{kind: "pad-delete", padName: name}, group: groupFocused}, action: paletteAction{kind: "pad-delete", padName: name}, group: groupFocused},
) )
case p.focusedTaskID != "":
if t, ok := p.taskByID(p.focusedTaskID); ok {
out = append(out,
paletteItem{label: "Rename task", hint: "rename task · " + t.Title,
action: paletteAction{kind: "task-rename-form", taskID: t.ID}, group: groupFocused},
)
for _, pr := range p.presets.Agents {
out = append(out, paletteItem{
label: "Start agent for task: " + pr.Name,
hint: t.Title + " · task-scoped",
action: paletteAction{kind: "task-start-agent", taskID: t.ID, preset: pr},
group: groupFocused,
})
}
}
case p.focused != "": case p.focused != "":
if c := findChildByID(p.children, p.focused); c != nil { if c := findChildByID(p.children, p.focused); c != nil {
name := c.DisplayName() name := c.DisplayName()
@@ -336,46 +311,39 @@ func (p *paletteState) buildItems(macro string) []paletteItem {
action: paletteAction{kind: "proc-delete", childID: c.ID}, group: groupFocused}, action: paletteAction{kind: "proc-delete", childID: c.ID}, group: groupFocused},
) )
} }
if c.TaskID != "" {
if t, ok := p.taskByID(c.TaskID); ok {
out = append(out, paletteItem{label: "Open task", hint: "open task · " + t.Title,
action: paletteAction{kind: "task-switch", taskID: t.ID}, group: groupFocused})
for _, pr := range p.presets.Agents {
out = append(out, paletteItem{
label: "Start another agent for task: " + pr.Name,
hint: t.Title + " · task-scoped",
action: paletteAction{kind: "task-start-agent", taskID: t.ID, preset: pr},
group: groupFocused,
})
}
}
}
} }
} }
// Group 1: Open — tasks first, then switch entries for every running child *other than* 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 // the one already focused (no point offering a no-op switch). Dead
// agents are filtered out (no restart path); dead command processes // agents are filtered out (no restart path); dead command processes
// remain so they can be restarted. // remain so they can be restarted.
if p.tasksEnabled {
out = append(out, paletteItem{
label: "Create task...",
hint: "manual project task",
action: paletteAction{kind: "task-create-form"},
group: groupOpen,
})
for _, t := range p.tasks {
if t.ID == p.focusedTaskID {
continue
}
out = append(out, paletteItem{
label: "Open task: " + t.Title,
hint: taskHint(t),
action: paletteAction{kind: "task-switch", taskID: t.ID},
group: groupOpen,
})
}
}
for _, c := range p.children { for _, c := range p.children {
if c.ID == p.focused { if c.ID == p.focused {
continue continue
@@ -727,22 +695,15 @@ func (p *paletteState) acceptOrEnterForm(adv int) (paletteAction, bool, int) {
p.mode = paletteModeSpawnForm p.mode = paletteModeSpawnForm
p.form = &spawnProcessForm{} p.form = &spawnProcessForm{}
return paletteAction{}, false, adv return paletteAction{}, false, adv
case "task-create-form":
p.enterNameForm("task-create", "", "", "new task", "Create task")
return paletteAction{}, false, adv
case "task-rename-form":
current := ""
if t, ok := p.taskByID(a.taskID); ok {
current = t.Title
}
p.enterNameForm("task", a.taskID, current, "task: "+current, "Rename task")
return paletteAction{}, false, adv
case "settings-open": case "settings-open":
p.mode = paletteModeSettings p.mode = paletteModeSettings
p.query = nil p.query = nil
p.cursor = 0 p.cursor = 0
p.rebuildSettings() p.rebuildSettings()
return paletteAction{}, false, adv return paletteAction{}, false, adv
case "project-open-form":
p.enterRenameForm("project", "", "", "project path")
return paletteAction{}, false, adv
case "pad-rename-form": case "pad-rename-form":
p.enterRenameForm("pad", a.padName, a.padName, "scratchpad: "+a.padName) p.enterRenameForm("pad", a.padName, a.padName, "scratchpad: "+a.padName)
return paletteAction{}, false, adv return paletteAction{}, false, adv
@@ -764,16 +725,12 @@ func (p *paletteState) acceptOrEnterForm(adv int) (paletteAction, bool, int) {
} }
func (p *paletteState) enterRenameForm(subject, target, current, subjectLine string) { func (p *paletteState) enterRenameForm(subject, target, current, subjectLine string) {
p.enterNameForm(subject, target, current, subjectLine, "Rename")
}
func (p *paletteState) enterNameForm(subject, target, current, subjectLine, title string) {
p.mode = paletteModeRenameForm p.mode = paletteModeRenameForm
p.renameForm = &renameForm{ p.renameForm = &renameForm{
name: []rune(current), name: []rune(current),
subject: subject, subject: subject,
target: target, target: target,
title: title, title: "Rename",
subjectLine: subjectLine, subjectLine: subjectLine,
} }
} }
@@ -1005,6 +962,9 @@ func (p *paletteState) submitRename() paletteAction {
return paletteAction{kind: "cancel"} return paletteAction{kind: "cancel"}
} }
newName := strings.TrimSpace(string(p.renameForm.name)) newName := strings.TrimSpace(string(p.renameForm.name))
if p.renameForm.subject == "project" {
return paletteAction{kind: "project-open-submit", projectPath: newName}
}
if newName == "" { if newName == "" {
return paletteAction{kind: "cancel"} return paletteAction{kind: "cancel"}
} }
@@ -1013,10 +973,6 @@ func (p *paletteState) submitRename() paletteAction {
case "pad": case "pad":
kind = "pad-rename-submit" kind = "pad-rename-submit"
return paletteAction{kind: kind, padName: p.renameForm.target, newName: newName} return paletteAction{kind: kind, padName: p.renameForm.target, newName: newName}
case "task-create":
return paletteAction{kind: "task-create-submit", newName: newName}
case "task":
return paletteAction{kind: "task-rename-submit", taskID: p.renameForm.target, newName: newName}
case "agent": case "agent":
kind = "agent-rename-submit" kind = "agent-rename-submit"
case "proc": case "proc":
@@ -1247,17 +1203,12 @@ func (p *paletteState) selectableIndex() int {
} }
// focusedSubject returns the short context string shown in the title // focusedSubject returns the short context string shown in the title
// bar — "on: <child>" / "pad: <name>" / "task: <title>" / "" — so the // bar — "on: <child>" / "pad: <name>" / "" — so the user knows which
// user knows which focus the context-section is targeting. // focus the context-section is targeting.
func (p *paletteState) focusedSubject() string { func (p *paletteState) focusedSubject() string {
if p.focusedPad != "" { if p.focusedPad != "" {
return "pad: " + p.focusedPad return "pad: " + p.focusedPad
} }
if p.focusedTaskID != "" {
if t, ok := p.taskByID(p.focusedTaskID); ok {
return "task: " + t.Title
}
}
if p.focused != "" { if p.focused != "" {
if c := findChildByID(p.children, p.focused); c != nil { if c := findChildByID(p.children, p.focused); c != nil {
return "on: " + c.DisplayName() return "on: " + c.DisplayName()
-49
View File
@@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/hjbdev/patterm/internal/preset" "github.com/hjbdev/patterm/internal/preset"
"github.com/hjbdev/patterm/internal/task"
) )
// makeFakeChild builds a Child with just enough state for the palette // makeFakeChild builds a Child with just enough state for the palette
@@ -84,54 +83,6 @@ func TestContextItemsProcess(t *testing.T) {
} }
} }
func TestFocusedTaskShowsTaskActions(t *testing.T) {
agent := &preset.Preset{Name: "codex", Kind: preset.KindAgent, Argv: []string{"codex"}}
taskList := []task.Task{{ID: "task_1", Title: "Fix sidebar"}}
p := newPaletteWithTasks(nil, "", "", "task_1", taskList, preset.Set{Agents: []*preset.Preset{agent}})
if got := p.focusedSubject(); got != "task: Fix sidebar" {
t.Fatalf("focused subject = %q", got)
}
if _, it := findItem(p, "task-rename-form"); it == nil || it.action.taskID != "task_1" {
t.Fatalf("task rename missing or wrong: %+v", it)
}
if _, it := findItem(p, "task-start-agent"); it == nil || it.action.taskID != "task_1" || it.action.preset != agent {
t.Fatalf("task start-agent missing or wrong: %+v", it)
}
if i, _ := findItem(p, "spawn-agent"); i < 0 {
t.Fatalf("normal unscoped spawn-agent row should remain available")
}
}
func TestTaskCreateFormSubmitsTitle(t *testing.T) {
p := newPaletteWithTasks(nil, "", "", "", []task.Task{}, preset.Set{})
idx, _ := findItem(p, "task-create-form")
if idx < 0 {
t.Fatalf("task-create-form missing")
}
p.cursor = idx
_, done, _ := p.handleInput([]byte("\r"), 0)
if done || p.mode != paletteModeRenameForm || p.renameForm == nil || p.renameForm.subject != "task-create" {
t.Fatalf("create task did not open name form: done=%v form=%+v", done, p.renameForm)
}
for _, b := range []byte("New task") {
_, _, _ = p.handleInput([]byte{b}, 0)
}
action, done, _ := p.handleInput([]byte("\r"), 0)
if !done || action.kind != "task-create-submit" || action.newName != "New task" {
t.Fatalf("submit = %+v done=%v", action, done)
}
}
func TestTaskBoundChildShowsOpenTask(t *testing.T) {
c := makeFakeChild("aid", "codex", KindAgent)
c.TaskID = "task_1"
taskList := []task.Task{{ID: "task_1", Title: "Fix sidebar"}}
p := newPaletteWithTasks([]*Child{c}, "aid", "", "", taskList, preset.Set{})
if _, it := findItem(p, "task-switch"); it == nil || it.action.taskID != "task_1" {
t.Fatalf("task-switch missing for task-bound child: %+v", it)
}
}
func TestContextItemsTerminalUsesCloseNotStop(t *testing.T) { func TestContextItemsTerminalUsesCloseNotStop(t *testing.T) {
c := makeFakeChild("tid", "terminal", KindTerminal) c := makeFakeChild("tid", "terminal", KindTerminal)
p := newPalette([]*Child{c}, "tid", "", preset.Set{}) p := newPalette([]*Child{c}, "tid", "", preset.Set{})
+162
View 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)
}
}
+1 -3
View File
@@ -6,7 +6,7 @@ import (
) )
func newRingChild() *Child { func newRingChild() *Child {
return newChildEntry("id", "name", KindCommand, nil, nil, "", "", "", "") return newChildEntry("id", "name", KindCommand, nil, nil, "", "", "")
} }
func TestRingShortWrite(t *testing.T) { func TestRingShortWrite(t *testing.T) {
@@ -90,8 +90,6 @@ func TestStripANSIBytesEquivalence(t *testing.T) {
cases := []string{ cases := []string{
"hello world", "hello world",
"\x1b[31mred\x1b[0m text", "\x1b[31mred\x1b[0m text",
"\x1b]0;title\x07after osc",
"\x1b]2;title\x1b\\after st",
"line1\nline2\r\nline3", "line1\nline2\r\nline3",
"bell\x07ish", "bell\x07ish",
"weird \x1bA escape", "weird \x1bA escape",
+3 -3
View File
@@ -116,10 +116,10 @@ func TestToolHostScratchpadDeleteRemovesPadAndRefreshes(t *testing.T) {
t.Fatalf("write doomed.md: %v", err) t.Fatalf("write doomed.md: %v", err)
} }
recorder := &scratchpadChangeRecorder{} recorder := &scratchpadChangeRecorder{}
host := newToolHost(nil, pads, nil, nil, preset.Set{}, nil, 120, 40) host := newToolHost(nil, pads, nil, preset.Set{}, nil, 120, 40)
host.scratch = recorder host.scratch = recorder
if err := host.ScratchpadDelete("doomed.md"); err != nil { if err := host.ScratchpadDelete("", "doomed.md"); err != nil {
t.Fatalf("ScratchpadDelete: %v", err) t.Fatalf("ScratchpadDelete: %v", err)
} }
if recorder.count != 1 { if recorder.count != 1 {
@@ -128,7 +128,7 @@ func TestToolHostScratchpadDeleteRemovesPadAndRefreshes(t *testing.T) {
if _, _, err := pads.Read("doomed.md"); !errors.Is(err, os.ErrNotExist) { if _, _, err := pads.Read("doomed.md"); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("read deleted pad error = %v, want os.ErrNotExist", err) t.Fatalf("read deleted pad error = %v, want os.ErrNotExist", err)
} }
if err := host.ScratchpadDelete("doomed.md"); !errors.Is(err, os.ErrNotExist) { if err := host.ScratchpadDelete("", "doomed.md"); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("delete missing error = %v, want os.ErrNotExist", err) t.Fatalf("delete missing error = %v, want os.ErrNotExist", err)
} }
if recorder.count != 1 { if recorder.count != 1 {
+78 -2
View File
@@ -46,6 +46,13 @@ type Session struct {
listenersMu sync.Mutex listenersMu sync.Mutex
listeners atomic.Pointer[[]ChildEventListener] 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 // persistStore records top-level command entries to a per-project
// JSON file so they can be re-spawned after patterm restarts. // JSON file so they can be re-spawned after patterm restarts.
// Optional; nil means "no persistence" (used by unit tests). // Optional; nil means "no persistence" (used by unit tests).
@@ -118,6 +125,16 @@ func (s *Session) Subscribe(l ChildEventListener) {
s.listeners.Store(&next) 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 // Unsubscribe removes a previously-registered listener. Safe to call
// with a listener that wasn't registered (no-op). // with a listener that wasn't registered (no-op).
func (s *Session) Unsubscribe(l ChildEventListener) { func (s *Session) Unsubscribe(l ChildEventListener) {
@@ -136,6 +153,24 @@ func (s *Session) Unsubscribe(l ChildEventListener) {
s.listeners.Store(&next) 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 // listenersSnapshot returns the frozen listener slice. Safe to call
// without the listeners mutex. // without the listeners mutex.
func (s *Session) listenersSnapshot() []ChildEventListener { func (s *Session) listenersSnapshot() []ChildEventListener {
@@ -146,16 +181,30 @@ func (s *Session) listenersSnapshot() []ChildEventListener {
return *p return *p
} }
func (s *Session) clientListenersSnapshot() []ChildEventListener {
p := s.clientListeners.Load()
if p == nil {
return nil
}
return *p
}
func (s *Session) emitSpawn(c *Child) { func (s *Session) emitSpawn(c *Child) {
for _, l := range s.listenersSnapshot() { for _, l := range s.listenersSnapshot() {
l.OnChildSpawned(c) l.OnChildSpawned(c)
} }
for _, l := range s.clientListenersSnapshot() {
l.OnChildSpawned(c)
}
} }
func (s *Session) emitExit(c *Child) { func (s *Session) emitExit(c *Child) {
for _, l := range s.listenersSnapshot() { for _, l := range s.listenersSnapshot() {
l.OnChildExited(c) l.OnChildExited(c)
} }
for _, l := range s.clientListenersSnapshot() {
l.OnChildExited(c)
}
} }
// emitPTYOut dispatches a fresh PTY chunk to every listener. Listeners // emitPTYOut dispatches a fresh PTY chunk to every listener. Listeners
@@ -165,18 +214,27 @@ func (s *Session) emitPTYOut(id string, chunk []byte) {
for _, l := range s.listenersSnapshot() { for _, l := range s.listenersSnapshot() {
l.OnPTYOut(id, chunk) l.OnPTYOut(id, chunk)
} }
for _, l := range s.clientListenersSnapshot() {
l.OnPTYOut(id, chunk)
}
} }
func (s *Session) emitStateChanged(id string, state IdleState) { func (s *Session) emitStateChanged(id string, state IdleState) {
for _, l := range s.listenersSnapshot() { for _, l := range s.listenersSnapshot() {
l.OnChildStateChanged(id, state) l.OnChildStateChanged(id, state)
} }
for _, l := range s.clientListenersSnapshot() {
l.OnChildStateChanged(id, state)
}
} }
func (s *Session) emitClosed(id string) { func (s *Session) emitClosed(id string) {
for _, l := range s.listenersSnapshot() { for _, l := range s.listenersSnapshot() {
l.OnChildClosed(id) l.OnChildClosed(id)
} }
for _, l := range s.clientListenersSnapshot() {
l.OnChildClosed(id)
}
} }
func (s *Session) ChildEnv() []string { func (s *Session) ChildEnv() []string {
@@ -201,7 +259,6 @@ type SpawnSpec struct {
WorkDir string WorkDir string
Name string Name string
ParentID string ParentID string
TaskID string
PresetRef string PresetRef string
Identity string // pre-minted; otherwise the constructor mints one for agents Identity string // pre-minted; otherwise the constructor mints one for agents
// CleanupPaths are owned runtime files/dirs removed when the child exits // CleanupPaths are owned runtime files/dirs removed when the child exits
@@ -227,6 +284,9 @@ func (s *Session) Spawn(spec SpawnSpec, cols, rows uint16) (*Child, error) {
if spec.Env == nil { if spec.Env == nil {
spec.Env = s.ChildEnv() spec.Env = s.ChildEnv()
} }
if spec.WorkDir == "" {
spec.WorkDir = s.projectDir
}
s.mu.Lock() s.mu.Lock()
id := s.mintUniqueIDLocked() id := s.mintUniqueIDLocked()
@@ -236,7 +296,7 @@ func (s *Session) Spawn(spec SpawnSpec, cols, rows uint16) (*Child, error) {
} }
s.mu.Unlock() s.mu.Unlock()
c := newChildEntry(id, spec.Name, spec.Kind, spec.Argv, spec.Env, spec.ParentID, spec.TaskID, spec.WorkDir, spec.PresetRef) c := newChildEntry(id, spec.Name, spec.Kind, spec.Argv, spec.Env, spec.ParentID, spec.WorkDir, spec.PresetRef)
if spec.Identity != "" { if spec.Identity != "" {
c.Identity = spec.Identity c.Identity = spec.Identity
} }
@@ -682,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 // SerializeChild returns the VT bytes that reproduce the child's
// current screen state. Used to repaint a child after the user switches // current screen state. Used to repaint a child after the user switches
// focus or closes the palette. // focus or closes the palette.
-37
View File
@@ -168,7 +168,6 @@ func (st *uiState) drawSidebar() {
palOpen := st.palette != nil palOpen := st.palette != nil
focus := st.focusedID focus := st.focusedID
focusPad := st.focusedPad focusPad := st.focusedPad
focusTask := st.focusedTaskID
activeAgent := st.activeAgentID activeAgent := st.activeAgentID
st.mu.Unlock() st.mu.Unlock()
if palOpen { if palOpen {
@@ -263,42 +262,6 @@ func (st *uiState) drawSidebar() {
} }
} }
// Tasks section — manual project-local tasks. Task selection is a real
// focus target, but it does not implicitly scope generic spawns.
writeHeader("Tasks")
tasks := st.tasksList()
if len(tasks) == 0 {
write(" " + styleDim + "(Ctrl-K create task)" + styleReset)
}
for _, t := range tasks {
if row > maxRow {
break
}
focused := t.ID == focusTask
var prefix, openStyle string
if focused {
prefix = " " + styleAccent + "▎" + styleReset + " "
openStyle = styleBold
} else {
prefix = " "
openStyle = styleHint
}
suffix := ""
if n := len(t.Worktrees); n > 0 {
suffix = " " + styleDim + fmt.Sprintf("%dw", n) + styleReset
}
budget := width - visibleLen(prefix) - visibleLen(suffix)
if budget < 1 {
budget = 1
}
nameCell := st.rowNameSlot("task:"+t.ID, t.Title, budget, focused)
write(prefix + openStyle + nameCell + styleReset + suffix)
}
if row+2 <= maxRow {
write("")
}
// Processes section — top-level command/terminal processes, // Processes section — top-level command/terminal processes,
// session-wide (does not change when the user switches agent tabs). // session-wide (does not change when the user switches agent tabs).
writeHeader("Processes") writeHeader("Processes")
+3 -3
View File
@@ -12,11 +12,11 @@ func TestOnChildSpawnedAgentChildKeepsFocus(t *testing.T) {
sess := NewSession(t.TempDir(), "test") sess := NewSession(t.TempDir(), "test")
st := &uiState{sess: sess} st := &uiState{sess: sess}
parent := newChildEntry("p_parent", "parent", KindAgent, nil, nil, "", "", "", "") parent := newChildEntry("p_parent", "parent", KindAgent, nil, nil, "", "", "")
st.focusedID = parent.ID st.focusedID = parent.ID
st.focusedName = parent.Name st.focusedName = parent.Name
subAgent := newChildEntry("p_sub", "sub", KindAgent, nil, nil, parent.ID, "", "", "") subAgent := newChildEntry("p_sub", "sub", KindAgent, nil, nil, parent.ID, "", "")
st.OnChildSpawned(subAgent) st.OnChildSpawned(subAgent)
@@ -36,7 +36,7 @@ func TestOnChildSpawnedPaletteChildTakesFocus(t *testing.T) {
st := &uiState{sess: sess} st := &uiState{sess: sess}
st.lastExit.Store(-1) st.lastExit.Store(-1)
c := newChildEntry("p_new", "newchild", KindAgent, nil, nil, "", "", "", "") c := newChildEntry("p_new", "newchild", KindAgent, nil, nil, "", "", "")
st.OnChildSpawned(c) st.OnChildSpawned(c)
+2 -2
View File
@@ -89,7 +89,7 @@ func TestSummaryTextForSelectsChildAndClips(t *testing.T) {
func TestSummaryManagerArmsOnlyTrackedTopLevelAgents(t *testing.T) { func TestSummaryManagerArmsOnlyTrackedTopLevelAgents(t *testing.T) {
sess := NewSession(t.TempDir(), "test") sess := NewSession(t.TempDir(), "test")
c := newChildEntry("a1", "agent", KindAgent, []string{"fake"}, nil, "", "", "", "") c := newChildEntry("a1", "agent", KindAgent, []string{"fake"}, nil, "", "", "")
running := StatusRunning running := StatusRunning
c.status.Store(&running) c.status.Store(&running)
sess.children[c.ID] = c sess.children[c.ID] = c
@@ -112,7 +112,7 @@ func TestSummaryManagerArmsOnlyTrackedTopLevelAgents(t *testing.T) {
t.Fatalf("tracked top-level agent not armed/dirty: %+v", e) t.Fatalf("tracked top-level agent not armed/dirty: %+v", e)
} }
sub := newChildEntry("a2", "sub", KindAgent, []string{"fake"}, nil, c.ID, "", "", "") sub := newChildEntry("a2", "sub", KindAgent, []string{"fake"}, nil, c.ID, "", "")
sub.status.Store(&running) sub.status.Store(&running)
m.RegisterChild(sub) m.RegisterChild(sub)
m.ObserveHumanInput(sub.ID, []byte("please summarize")) m.ObserveHumanInput(sub.ID, []byte("please summarize"))
+1 -11
View File
@@ -561,12 +561,10 @@ func (m *timerManager) TimerList(ownerID string) []mcp.TimerInfo {
if t.status != timerStatusPending && t.status != timerStatusPaused { if t.status != timerStatusPending && t.status != timerStatusPaused {
continue continue
} }
body, bodyTruncated := timerBodyPreview(t.body)
info := mcp.TimerInfo{ info := mcp.TimerInfo{
ID: t.id, ID: t.id,
Label: t.label, Label: t.label,
Body: body, Body: t.body,
BodyTruncated: bodyTruncated,
Kind: string(t.kind), Kind: string(t.kind),
Status: t.status, Status: t.status,
OwnerID: t.ownerID, OwnerID: t.ownerID,
@@ -583,14 +581,6 @@ func (m *timerManager) TimerList(ownerID string) []mcp.TimerInfo {
return out return out
} }
func timerBodyPreview(body string) (string, bool) {
const max = 500
if len(body) <= max {
return body, false
}
return body[:max], true
}
// activeForChild returns the nearest pending or paused timer attached // activeForChild returns the nearest pending or paused timer attached
// to child id (either owned by it or watching it). Used by the sidebar // to child id (either owned by it or watching it). Used by the sidebar
// for the "⏱ 12s" indicator. nil when none. // for the "⏱ 12s" indicator. nil when none.
+1 -1
View File
@@ -41,7 +41,7 @@ func (r *recorderFire) snapshot() []recordedFire {
// Doesn't open a PTY — fireFn is overridden so InjectAsOrchestrator is // Doesn't open a PTY — fireFn is overridden so InjectAsOrchestrator is
// never reached. // never reached.
func fakeChild(id string) *Child { func fakeChild(id string) *Child {
c := newChildEntry(id, id, KindAgent, []string{"echo"}, nil, "", "", "", "") c := newChildEntry(id, id, KindAgent, []string{"echo"}, nil, "", "", "")
running := StatusRunning running := StatusRunning
c.status.Store(&running) c.status.Store(&running)
return c return c
+63
View 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
}
+13 -23
View File
@@ -1,21 +1,16 @@
package app package app
import ( import "github.com/hjbdev/patterm/internal/scratchpad"
"github.com/hjbdev/patterm/internal/scratchpad"
"github.com/hjbdev/patterm/internal/task"
)
// navEntry is one row in the unified sidebar navigation list. Exactly // navEntry is one row in the unified sidebar navigation list. Exactly
// one of taskID, childID, or pad is set. childID points at a Child by ID; // one of childID or pad is set. childID points at a Child by ID; pad
// pad names a scratchpad entry. Empty zero-value means "no target". // names a scratchpad entry. Empty zero-value means "no target".
type navEntry struct { type navEntry struct {
taskID string
childID string childID string
pad string pad string
} }
func (n navEntry) empty() bool { return n.taskID == "" && n.childID == "" && n.pad == "" } func (n navEntry) empty() bool { return n.childID == "" && n.pad == "" }
func (n navEntry) isTask() bool { return n.taskID != "" }
func (n navEntry) isPad() bool { return n.pad != "" } func (n navEntry) isPad() bool { return n.pad != "" }
func (n navEntry) isChild() bool { return n.childID != "" } func (n navEntry) isChild() bool { return n.childID != "" }
@@ -226,14 +221,12 @@ func sidebarNavList(children []*Child, activeAgentID string) []*Child {
return out return out
} }
// sidebarNav returns the combined Tasks + Processes + Agent Tree + Scratchpads // sidebarNav returns the combined Processes + Agent Tree + Scratchpads
// navigation list. Order matches the right rail top-to-bottom. // navigation list. Scratchpads always appear after children so the
func sidebarNav(children []*Child, activeAgentID string, tasks []task.Task, pads []scratchpad.Entry) []navEntry { // existing "step past the tree" expectation still holds.
func sidebarNav(children []*Child, activeAgentID string, pads []scratchpad.Entry) []navEntry {
flat := sidebarNavList(children, activeAgentID) flat := sidebarNavList(children, activeAgentID)
out := make([]navEntry, 0, len(tasks)+len(flat)+len(pads)) out := make([]navEntry, 0, len(flat)+len(pads))
for _, t := range tasks {
out = append(out, navEntry{taskID: t.ID})
}
for _, c := range flat { for _, c := range flat {
out = append(out, navEntry{childID: c.ID}) out = append(out, navEntry{childID: c.ID})
} }
@@ -244,18 +237,15 @@ func sidebarNav(children []*Child, activeAgentID string, tasks []task.Task, pads
} }
// nextNavEntry returns the entry `step` positions away from the // nextNavEntry returns the entry `step` positions away from the
// current focus in the unified nav list. Exactly one focus identifier is // current focus in the unified nav list. Either focusChildID or
// usually set (or all empty for "nothing focused yet"). // focusPad will be set (or both empty for "nothing focused yet").
// Empty when there's nothing else to land on. // Empty when there's nothing else to land on.
func nextNavEntry(children []*Child, focusChildID, focusPad, focusTaskID, activeAgentID string, tasks []task.Task, pads []scratchpad.Entry, step int) navEntry { func nextNavEntry(children []*Child, focusChildID, focusPad, activeAgentID string, pads []scratchpad.Entry, step int) navEntry {
flat := sidebarNav(children, activeAgentID, tasks, pads) flat := sidebarNav(children, activeAgentID, pads)
if len(flat) == 0 { if len(flat) == 0 {
return navEntry{} return navEntry{}
} }
matches := func(e navEntry) bool { matches := func(e navEntry) bool {
if focusTaskID != "" && e.taskID != "" {
return e.taskID == focusTaskID
}
if focusPad != "" && e.pad != "" { if focusPad != "" && e.pad != "" {
return e.pad == focusPad return e.pad == focusPad
} }
+1 -33
View File
@@ -1,10 +1,6 @@
package app package app
import ( import "testing"
"testing"
"github.com/hjbdev/patterm/internal/task"
)
func TestVisibleSessionTreeScopesToFocusedRoot(t *testing.T) { func TestVisibleSessionTreeScopesToFocusedRoot(t *testing.T) {
root1 := testChild("c1", "root1", "", StatusRunning) root1 := testChild("c1", "root1", "", StatusRunning)
@@ -129,34 +125,6 @@ func TestSidebarNavListIncludesProcessesAboveAgentTree(t *testing.T) {
} }
} }
func TestSidebarNavOrdersTasksBeforeProcessesAndPads(t *testing.T) {
p := testProcess("p1", "bun", StatusRunning)
r := testAgent("a1", "claude", "", StatusRunning)
tasks := []task.Task{{ID: "t1", Title: "Fix sidebar"}}
nav := sidebarNav([]*Child{p, r}, "a1", tasks, nil)
if len(nav) != 3 {
t.Fatalf("nav len = %d, want 3 (%+v)", len(nav), nav)
}
if !nav[0].isTask() || nav[0].taskID != "t1" {
t.Fatalf("first nav entry = %+v, want task t1", nav[0])
}
if !nav[1].isChild() || nav[1].childID != "p1" {
t.Fatalf("second nav entry = %+v, want process p1", nav[1])
}
if !nav[2].isChild() || nav[2].childID != "a1" {
t.Fatalf("third nav entry = %+v, want agent a1", nav[2])
}
}
func TestNextNavEntryWalksFromTaskToProcess(t *testing.T) {
p := testProcess("p1", "bun", StatusRunning)
tasks := []task.Task{{ID: "t1", Title: "Fix sidebar"}}
next := nextNavEntry([]*Child{p}, "", "", "t1", "", tasks, nil, +1)
if !next.isChild() || next.childID != "p1" {
t.Fatalf("task -> next = %+v, want process p1", next)
}
}
func TestSidebarNavListIncludesExitedProcesses(t *testing.T) { func TestSidebarNavListIncludesExitedProcesses(t *testing.T) {
p := testProcess("p1", "shell", StatusExited) p := testProcess("p1", "shell", StatusExited)
r := testAgent("a1", "claude", "", StatusRunning) r := testAgent("a1", "claude", "", StatusRunning)
+1 -1
View File
@@ -143,7 +143,7 @@ func openSession(t *testing.T, env *testEnv, childEnv []string) *Session {
if err != nil { if err != nil {
t.Fatalf("vt emulator: %v", err) t.Fatalf("vt emulator: %v", err)
} }
p, err := pkgpty.Start([]string{env.PattermBin, "--project", env.ProjectDir}, childEnv, env.ProjectDir, env.Cols, env.Rows) p, err := pkgpty.Start([]string{env.PattermBin, "--in-process", "--project", env.ProjectDir}, childEnv, "", env.Cols, env.Rows)
if err != nil { if err != nil {
_ = em.Close() _ = em.Close()
t.Fatalf("pty start: %v", err) t.Fatalf("pty start: %v", err)
@@ -1,62 +0,0 @@
{
"name": "canonical_output_noise",
"steps": [
{
"type": "mcp_call",
"method": "spawn_process",
"params": {
"kind": "command",
"argv": [
"sh",
"-lc",
"printf '\\033[31mStatus: running 12s\\033[0m\\nStatus: running 13s\\n╭────╮\\n│ │\\nDownloading 10%%\\rDownloading 100%%\\nFINAL: deploy ready\\n'; sleep 5"
],
"name": "noisy"
},
"save_as": "proc"
},
{
"type": "wait_until_mcp",
"method": "get_process_output",
"params": {
"process_id": "{{proc.process_id}}",
"mode": "stream",
"raw": true,
"max_lines": 20
},
"path": "content",
"contains": "FINAL: deploy ready",
"timeout_ms": 5000,
"save_as": "raw"
},
{
"type": "assert_saved",
"from": "raw",
"path": "content",
"contains": "FINAL: deploy ready"
},
{
"type": "mcp_call",
"method": "get_process_output",
"params": {
"process_id": "{{proc.process_id}}",
"mode": "stream",
"since_offset": 0,
"max_lines": 20
},
"save_as": "canonical"
},
{
"type": "assert_saved",
"from": "canonical",
"path": "content",
"equals": "Status: running [time]\nDownloading [count]\nFINAL: deploy ready"
},
{
"type": "assert_saved",
"from": "canonical",
"path": "canonicalized",
"equals": true
}
]
}
@@ -22,7 +22,7 @@
{ "type": "assert_contains", "contains": "Scratchpads" }, { "type": "assert_contains", "contains": "Scratchpads" },
{ {
"type": "assert_regex", "type": "assert_regex",
"regex": "(?m)^[^\\n]*\\+ new[^\\n]*Tasks[^\\n]*$" "regex": "(?m)^[^\\n]*\\+ new[^\\n]*Processes[^\\n]*$"
}, },
{ {
"type": "assert_regex", "type": "assert_regex",
@@ -1,42 +0,0 @@
{
"name": "create_task_and_start_agent",
"presets": {
"agents": [
{
"name": "fake-agent",
"argv": ["fake-agent"]
}
]
},
"scripts": [
{
"name": "fake-agent",
"body": "#!/bin/sh\necho FAKE READY\ncat\n"
}
],
"steps": [
{ "type": "wait_stable", "timeout_ms": 3000 },
{ "type": "assert_contains", "contains": "Tasks" },
{ "type": "assert_contains", "contains": "Ctrl-K create task" },
{ "type": "send_chord", "chord": "ctrl-k" },
{ "type": "send_text", "text": "Create task" },
{ "type": "send_chord", "chord": "enter" },
{ "type": "wait_text", "contains": "Create task", "timeout_ms": 3000 },
{ "type": "send_text", "text": "Harness task" },
{ "type": "send_chord", "chord": "enter" },
{ "type": "wait_text", "contains": "task: Harness task", "timeout_ms": 5000 },
{ "type": "assert_contains", "contains": "Harness task" },
{ "type": "send_chord", "chord": "ctrl-k" },
{ "type": "send_text", "text": "fake-agent" },
{ "type": "send_chord", "chord": "enter" },
{ "type": "wait_text", "contains": "task_register_worktree", "timeout_ms": 8000 },
{
"type": "assert_mcp",
"method": "list_processes",
"path": "0.name",
"contains": "Harness task"
}
]
}
+1 -1
View File
@@ -55,7 +55,7 @@ func NewCLI(opts Options) (*Session, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
p, err := pkgpty.Start([]string{env.PattermBin, "--project", env.ProjectDir}, childEnv, env.ProjectDir, env.Cols, env.Rows) p, err := pkgpty.Start([]string{env.PattermBin, "--in-process", "--project", env.ProjectDir}, childEnv, "", env.Cols, env.Rows)
if err != nil { if err != nil {
_ = em.Close() _ = em.Close()
return nil, err return nil, err
+3
View File
@@ -188,6 +188,9 @@ func RunStdioProxy(socket, identity string) error {
// "<token>"} + newline. Real protocol handshake is a later // "<token>"} + newline. Real protocol handshake is a later
// milestone. // milestone.
greeting := map[string]string{"patterm_identity": identity} greeting := map[string]string{"patterm_identity": identity}
if key := os.Getenv("PATTERM_PROJECT_KEY"); key != "" {
greeting["project_key"] = key
}
gb, _ := json.Marshal(greeting) gb, _ := json.Marshal(greeting)
gb = append(gb, '\n') gb = append(gb, '\n')
if _, err := conn.Write(gb); err != nil { if _, err := conn.Write(gb); err != nil {
+11 -21
View File
@@ -108,17 +108,10 @@ type blockingToolHost struct {
waitEntered chan struct{} waitEntered chan struct{}
waitRelease chan struct{} waitRelease chan struct{}
waitOnce sync.Once waitOnce sync.Once
task TaskInfo
} }
func (h *blockingToolHost) ResolveCallerIdentity(identity string) string { return "caller-" + identity } func (h *blockingToolHost) ResolveCallerIdentity(identity string) string { return "caller-" + identity }
func (h *blockingToolHost) CallerRole(string) CallerRole { return RoleOrchestrator } func (h *blockingToolHost) CallerRole(string) CallerRole { return RoleOrchestrator }
func (h *blockingToolHost) CallerTask(string) (TaskInfo, bool) {
if h.task.ID == "" {
return TaskInfo{}, false
}
return h.task, true
}
func (h *blockingToolHost) SpawnAgent(string, SpawnAgentArgs) (ProcessInfo, error) { func (h *blockingToolHost) SpawnAgent(string, SpawnAgentArgs) (ProcessInfo, error) {
return ProcessInfo{}, nil return ProcessInfo{}, nil
} }
@@ -141,16 +134,16 @@ func (h *blockingToolHost) ListProcesses(string, string) []ProcessInfo { return
func (h *blockingToolHost) GetProcessStatus(string, string) (ProcessStatus, error) { func (h *blockingToolHost) GetProcessStatus(string, string) (ProcessStatus, error) {
return ProcessStatus{ProcessInfo: ProcessInfo{ID: "p_fast", Status: "running"}}, nil return ProcessStatus{ProcessInfo: ProcessInfo{ID: "p_fast", Status: "running"}}, nil
} }
func (h *blockingToolHost) GetProjectStatus(string, bool) (ProjectStatus, error) { func (h *blockingToolHost) GetProjectStatus(string) (ProjectStatus, error) {
return ProjectStatus{}, nil return ProjectStatus{}, nil
} }
func (h *blockingToolHost) GetProcessOutput(string, ProcessOutputArgs) (ProcessOutput, error) { func (h *blockingToolHost) GetProcessOutput(string, string, string, int64) (ProcessOutput, error) {
return ProcessOutput{}, nil return ProcessOutput{}, nil
} }
func (h *blockingToolHost) GetProcessRawOutput(string, RawOutputArgs) (RawOutput, error) { func (h *blockingToolHost) GetProcessRawOutput(string, string, int64) (RawOutput, error) {
return RawOutput{}, nil return RawOutput{}, nil
} }
func (h *blockingToolHost) SearchOutput(string, SearchOutputArgs) (SearchResult, error) { func (h *blockingToolHost) SearchOutput(string, string, string, string, int) (SearchResult, error) {
return SearchResult{}, nil return SearchResult{}, nil
} }
func (h *blockingToolHost) WaitForPattern(string, string, string, float64, string) (bool, string, error) { func (h *blockingToolHost) WaitForPattern(string, string, string, float64, string) (bool, string, error) {
@@ -184,17 +177,14 @@ func (h *blockingToolHost) TimerResume(string, string) error { return nil }
func (h *blockingToolHost) TimerList(string) ([]TimerInfo, error) { func (h *blockingToolHost) TimerList(string) ([]TimerInfo, error) {
return nil, nil return nil, nil
} }
func (h *blockingToolHost) ScratchpadList() ([]scratchpad.Entry, error) { return nil, nil } func (h *blockingToolHost) ScratchpadList(string) ([]scratchpad.Entry, error) { return nil, nil }
func (h *blockingToolHost) ScratchpadRead(ScratchpadReadArgs) (ScratchpadReadResult, error) { func (h *blockingToolHost) ScratchpadRead(string, string) (string, string, error) {
return ScratchpadReadResult{}, nil return "", "", nil
} }
func (h *blockingToolHost) ScratchpadWrite(string, string, string) (string, error) { func (h *blockingToolHost) ScratchpadWrite(string, string, string, string) (string, error) {
return "", nil return "", nil
} }
func (h *blockingToolHost) ScratchpadAppend(string, string) error { return nil } func (h *blockingToolHost) ScratchpadAppend(string, string, string) error { return nil }
func (h *blockingToolHost) ScratchpadDelete(string) error { return nil } func (h *blockingToolHost) ScratchpadDelete(string, string) error { return nil }
func (h *blockingToolHost) RegisterTaskWorktree(string, TaskRegisterWorktreeArgs) (TaskInfo, error) { func (h *blockingToolHost) WhoAmI(string) WhoAmI { return WhoAmI{} }
return TaskInfo{}, nil
}
func (h *blockingToolHost) WhoAmI(string, bool) WhoAmI { return WhoAmI{} }
func (h *blockingToolHost) Help(string, string) HelpResponse { return HelpResponse{} } func (h *blockingToolHost) Help(string, string) HelpResponse { return HelpResponse{} }
+43 -158
View File
@@ -3,8 +3,6 @@ package mcp
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/hjbdev/patterm/internal/scratchpad"
) )
// MCP protocol surface. The patterm server originally exposed each // MCP protocol surface. The patterm server originally exposed each
@@ -29,7 +27,7 @@ var serverInfo = map[string]any{
"version": "0.1.0", "version": "0.1.0",
} }
// baseServerInstructions is returned in the MCP `initialize` response. MCP // serverInstructions is returned in the MCP `initialize` response. MCP
// clients show this to the underlying LLM as context for how to use // clients show this to the underlying LLM as context for how to use
// the server. Failure modes we've seen and want to head off: // the server. Failure modes we've seen and want to head off:
// - The agent assumes patterm is something it has to launch (running // - The agent assumes patterm is something it has to launch (running
@@ -45,14 +43,7 @@ var serverInfo = map[string]any{
// up as sub-agents and won't be tied into the patterm lifecycle. // up as sub-agents and won't be tied into the patterm lifecycle.
// //
// Keep this short — clients vary in how much they surface to the LLM. // Keep this short — clients vary in how much they surface to the LLM.
const baseServerInstructions = "You are inside patterm. Use these MCP tools; do not launch patterm or poke its Unix socket yourself. Use spawn_agent for sub-agents, close spawned panes when done, and use timer_fire_when_idle_* instead of wait_for_pattern to wait for send_message replies." const serverInstructions = "You are already running INSIDE patterm; the `patterm` MCP server is connected over the same stdio MCP transport you use for any other MCP server. Use the MCP tools you see in tools/list — do NOT (a) try to launch `patterm` or `patterm mcp-stdio` yourself, (b) poke the Unix socket through perl / nc / socat / curl, or (c) shell out to `claude` / `codex` / `opencode` to start a peer. Any of those bypasses caller-identity and the new agent will land as a stray top-level tab instead of a child under you. Start with `whoami` for your role and the full tool list, then `help('topics')` for orientation. `spawn_agent` is the only correct way to start a sub-agent; `spawn_process` is for non-LLM commands; `list_processes` / `get_process_output` inspect them; `send_input` / `send_message` drive them. Whatever you spawn is yours to `close_process` when done. When you `send_message` a sub-agent, its reply comes back into YOUR pane as `[sub-agent:<name>] …`, not into the sub-agent's output — to wait for it, use `timer_fire_when_idle_any([sub_agent])` and then read your own pane; do NOT `wait_for_pattern` on the sub-agent, that will deadlock until timeout."
func serverInstructions(taskBound bool) string {
if !taskBound {
return baseServerInstructions
}
return baseServerInstructions + " This MCP connection is task-bound; call whoami for the task, and register git worktrees you create or use with task_register_worktree."
}
// toolDescriptor is the shape returned by `tools/list`. inputSchema is // toolDescriptor is the shape returned by `tools/list`. inputSchema is
// a JSON Schema object — we provide a minimal `{type: "object"}` schema // a JSON Schema object — we provide a minimal `{type: "object"}` schema
@@ -85,29 +76,25 @@ func objectSchema(properties map[string]any, required []string) map[string]any {
} }
func stringProp(desc string) map[string]any { func stringProp(desc string) map[string]any {
_ = desc return map[string]any{"type": "string", "description": desc}
return map[string]any{"type": "string"}
} }
func numberProp(desc string) map[string]any { func numberProp(desc string) map[string]any {
_ = desc return map[string]any{"type": "number", "description": desc}
return map[string]any{"type": "number"}
} }
func integerProp(desc string) map[string]any { func integerProp(desc string) map[string]any {
_ = desc return map[string]any{"type": "integer", "description": desc}
return map[string]any{"type": "integer"}
} }
func booleanProp(desc string) map[string]any { func booleanProp(desc string) map[string]any {
_ = desc return map[string]any{"type": "boolean", "description": desc}
return map[string]any{"type": "boolean"}
} }
func arrayOfStringsProp(desc string) map[string]any { func arrayOfStringsProp(desc string) map[string]any {
_ = desc
return map[string]any{ return map[string]any{
"type": "array", "type": "array",
"description": desc,
"items": map[string]any{"type": "string"}, "items": map[string]any{"type": "string"},
} }
} }
@@ -115,11 +102,11 @@ func arrayOfStringsProp(desc string) map[string]any {
// toolCatalog is the full list advertised via tools/list. Descriptions // toolCatalog is the full list advertised via tools/list. Descriptions
// are intentionally short — clients are expected to fetch help() for // are intentionally short — clients are expected to fetch help() for
// detail. Schemas mirror the param structs in tools.go. // detail. Schemas mirror the param structs in tools.go.
func toolCatalog(role CallerRole, taskBound bool) []toolDescriptor { func toolCatalog() []toolDescriptor {
tools := []toolDescriptor{ return []toolDescriptor{
{ {
Name: "spawn_agent", Name: "spawn_agent",
Description: "Spawn a sub-agent from an agent preset.", Description: "Spawn a sub-agent from an agent preset and optionally seed it with initial instructions. This is the ONLY correct way to start a sub-agent under you — do not shell out to `claude` / `codex` / `opencode` and do not poke patterm's Unix socket via perl / nc / socat. Either bypasses caller identity and the new agent lands as a stray top-level tab instead of your child. Caller owns lifecycle: when the sub-agent's work is done (it reports back via send_message, or you no longer need it), call close_process on its process_id to free the pane and tear down the PTY. See help('spawning') and help('lifecycle').",
InputSchema: objectSchema(map[string]any{ InputSchema: objectSchema(map[string]any{
"agent": stringProp("Preset name (e.g. \"claude\", \"codex\")."), "agent": stringProp("Preset name (e.g. \"claude\", \"codex\")."),
"agent_instructions": stringProp("Initial prompt typed into the agent after it's ready."), "agent_instructions": stringProp("Initial prompt typed into the agent after it's ready."),
@@ -128,14 +115,14 @@ func toolCatalog(role CallerRole, taskBound bool) []toolDescriptor {
}, },
{ {
Name: "spawn_process", Name: "spawn_process",
Description: "Spawn a terminal, process preset, or argv command.", Description: "Spawn a process: a terminal, a process preset, or a freeform argv command. Caller owns lifecycle: when the process is no longer needed, call close_process to remove its entry (live children are SIGKILL'd first). See help('lifecycle').",
InputSchema: objectSchema(map[string]any{ InputSchema: objectSchema(map[string]any{
"kind": stringProp("\"terminal\" or \"command\"."), "kind": stringProp("\"terminal\" or \"command\"."),
"preset": stringProp("Process preset name (mutually exclusive with argv)."), "preset": stringProp("Process preset name (mutually exclusive with argv)."),
"argv": map[string]any{"type": "array", "items": map[string]any{"type": "string"}}, "argv": map[string]any{"type": "array", "items": map[string]any{"type": "string"}, "description": "Argv vector for freeform commands."},
"name": stringProp("Display name for the pane."), "name": stringProp("Display name for the pane."),
"working_dir": stringProp("Working directory for the spawned process."), "working_dir": stringProp("Working directory for the spawned process."),
"env": map[string]any{"type": "object", "additionalProperties": map[string]any{"type": "string"}}, "env": map[string]any{"type": "object", "additionalProperties": map[string]any{"type": "string"}, "description": "Extra environment variables."},
"shell": booleanProp("Run argv through sh -lc."), "shell": booleanProp("Run argv through sh -lc."),
}, nil), }, nil),
}, },
@@ -201,30 +188,23 @@ func toolCatalog(role CallerRole, taskBound bool) []toolDescriptor {
{ {
Name: "get_project_status", Name: "get_project_status",
Description: "One-shot orientation: project, caller, processes, scratchpads.", Description: "One-shot orientation: project, caller, processes, scratchpads.",
InputSchema: objectSchema(map[string]any{ InputSchema: objectSchema(nil, nil),
"include_tools": booleanProp("Include available_tools in caller metadata."),
}, nil),
}, },
{ {
Name: "get_process_output", Name: "get_process_output",
Description: "Read canonical terminal text by default: visible grid (\"grid\") or recent stream (\"stream\") with ANSI/control noise, borders, duplicate status churn, and volatile timers removed. Set raw=true only for diagnostic ANSI-preserved PTY bytes.", Description: "Read rendered grid (\"grid\") or ANSI-stripped stream (\"stream\") output, with screen-version watermark.",
InputSchema: objectSchema(map[string]any{ InputSchema: objectSchema(map[string]any{
"process_id": stringProp("Target process id."), "process_id": stringProp("Target process id."),
"mode": stringProp("\"grid\" (default) or \"stream\"."), "mode": stringProp("\"grid\" (default) or \"stream\"."),
"since_offset": integerProp("Watermark offset from a previous call."), "since_offset": integerProp("Watermark offset from a previous call."),
"max_bytes": integerProp("Maximum content bytes to return."),
"max_lines": integerProp("Maximum canonical lines to return (default 120, max 500)."),
"raw": booleanProp("Return raw ANSI-preserved stream bytes instead of canonical text."),
"include_meta": booleanProp("Include verbose cursor, geometry, active screen, idle, and screen-version metadata."),
}, []string{"process_id"}), }, []string{"process_id"}),
}, },
{ {
Name: "get_process_raw_output", Name: "get_process_raw_output",
Description: "Compatibility alias for raw=true get_process_output: read the raw ANSI byte stream since since_offset.", Description: "Read the raw ANSI byte stream since since_offset.",
InputSchema: objectSchema(map[string]any{ InputSchema: objectSchema(map[string]any{
"process_id": stringProp("Target process id."), "process_id": stringProp("Target process id."),
"since_offset": integerProp("Byte offset from a previous call."), "since_offset": integerProp("Byte offset from a previous call."),
"max_bytes": integerProp("Maximum content bytes to return."),
}, []string{"process_id"}), }, []string{"process_id"}),
}, },
{ {
@@ -234,13 +214,12 @@ func toolCatalog(role CallerRole, taskBound bool) []toolDescriptor {
"process_id": stringProp("Target process id."), "process_id": stringProp("Target process id."),
"pattern": stringProp("Regex pattern."), "pattern": stringProp("Regex pattern."),
"kind": stringProp("\"rendered\" (default) or \"raw\"."), "kind": stringProp("\"rendered\" (default) or \"raw\"."),
"limit": integerProp("Max matches (default 10)."), "limit": integerProp("Max matches (default 20)."),
"max_bytes": integerProp("Max bytes per returned match line."),
}, []string{"process_id", "pattern"}), }, []string{"process_id", "pattern"}),
}, },
{ {
Name: "wait_for_pattern", Name: "wait_for_pattern",
Description: "Block until pattern appears in the target process output.", Description: "Block until pattern appears in the TARGET process's own output, or timeout elapses. Use this for waiting on text the target itself will emit (a shell prompt, a build's \"tests passed\" line, etc.). Anti-pattern: do NOT use this to wait for a sub-agent's reply to send_message — replies are routed into the CALLER's pane tagged `[sub-agent:<name>]`, not into the sub-agent's output, so this call will spin to timeout. For sub-agent coordination use `timer_fire_when_idle_any` and then read your own pane.",
InputSchema: objectSchema(map[string]any{ InputSchema: objectSchema(map[string]any{
"process_id": stringProp("Target process id."), "process_id": stringProp("Target process id."),
"pattern": stringProp("Regex pattern."), "pattern": stringProp("Regex pattern."),
@@ -266,12 +245,11 @@ func toolCatalog(role CallerRole, taskBound bool) []toolDescriptor {
"submit": booleanProp("Whether to append a submit keystroke."), "submit": booleanProp("Whether to append a submit keystroke."),
"wait_ms": integerProp("After sending, wait this many ms before tailing."), "wait_ms": integerProp("After sending, wait this many ms before tailing."),
"tail_mode": stringProp("\"none\" (default), \"stream\", or \"grid\"."), "tail_mode": stringProp("\"none\" (default), \"stream\", or \"grid\"."),
"tail_max_bytes": integerProp("Maximum bytes in returned tail."),
}, []string{"process_id", "kind"}), }, []string{"process_id", "kind"}),
}, },
{ {
Name: "send_message", Name: "send_message",
Description: "Send a tagged message to a parent or child process.", Description: "Deliver a text message to another process as orchestrator-owned input. Fire-and-forget: returns immediately, without waiting for the recipient to read or act. If the recipient replies via send_message, that reply arrives in YOUR pane tagged `[sub-agent:<name>]` (child→parent) or `[orchestrator]` (parent→child) — NOT in the recipient's output. To wait for a sub-agent's reply, schedule `timer_fire_when_idle_any([sub_agent_id], body=…)` and then read your own pane when the timer fires. Do not `wait_for_pattern` on the recipient for a reply; it will deadlock.",
InputSchema: objectSchema(map[string]any{ InputSchema: objectSchema(map[string]any{
"target_process_id": stringProp("Recipient process id."), "target_process_id": stringProp("Recipient process id."),
"message": stringProp("Message body."), "message": stringProp("Message body."),
@@ -305,7 +283,7 @@ func toolCatalog(role CallerRole, taskBound bool) []toolDescriptor {
}, },
{ {
Name: "timer_fire_when_idle_any", Name: "timer_fire_when_idle_any",
Description: "Fire when any watched process becomes idle.", Description: "Canonical way to wait for a sub-agent to finish working: send_message the sub-agent, then schedule this with watched=[sub_agent_id]; when it fires, the reply is already sitting in your own pane tagged `[sub-agent:<name>]`. Schedules a timer that fires when any watched process enters idle (already-idle entries excluded), or when max_wait_seconds elapses.",
InputSchema: objectSchema(map[string]any{ InputSchema: objectSchema(map[string]any{
"watched": arrayOfStringsProp("Process ids to watch."), "watched": arrayOfStringsProp("Process ids to watch."),
"body": stringProp("Message delivered verbatim to the owning agent when the timer fires."), "body": stringProp("Message delivered verbatim to the owning agent when the timer fires."),
@@ -316,7 +294,7 @@ func toolCatalog(role CallerRole, taskBound bool) []toolDescriptor {
}, },
{ {
Name: "timer_fire_when_idle_all", Name: "timer_fire_when_idle_all",
Description: "Fire when all watched processes are idle.", Description: "Canonical way to wait for several sub-agents to finish working in parallel: send_message each one, then schedule this with watched=[…ids]; when it fires, each reply is in your own pane tagged `[sub-agent:<name>]`. Schedules a timer that fires when all watched processes are idle (already-idle entries count as satisfied), or when max_wait_seconds elapses.",
InputSchema: objectSchema(map[string]any{ InputSchema: objectSchema(map[string]any{
"watched": arrayOfStringsProp("Process ids to watch."), "watched": arrayOfStringsProp("Process ids to watch."),
"body": stringProp("Message delivered verbatim to the owning agent when the timer fires."), "body": stringProp("Message delivered verbatim to the owning agent when the timer fires."),
@@ -361,8 +339,6 @@ func toolCatalog(role CallerRole, taskBound bool) []toolDescriptor {
Description: "Read a scratchpad entry, returning content and revision.", Description: "Read a scratchpad entry, returning content and revision.",
InputSchema: objectSchema(map[string]any{ InputSchema: objectSchema(map[string]any{
"name": stringProp("Scratchpad name."), "name": stringProp("Scratchpad name."),
"offset": integerProp("Byte offset to start reading."),
"max_bytes": integerProp("Maximum content bytes to return."),
}, []string{"name"}), }, []string{"name"}),
}, },
{ {
@@ -389,20 +365,10 @@ func toolCatalog(role CallerRole, taskBound bool) []toolDescriptor {
"name": stringProp("Scratchpad name."), "name": stringProp("Scratchpad name."),
}, []string{"name"}), }, []string{"name"}),
}, },
{
Name: "task_register_worktree",
Description: "Register a git worktree path for the current task.",
InputSchema: objectSchema(map[string]any{
"path": stringProp("Absolute path, or path relative to this process working directory."),
"branch": stringProp("Git branch name, if known."),
}, []string{"path"}),
},
{ {
Name: "whoami", Name: "whoami",
Description: "Return caller identity, role, parent, and project metadata.", Description: "Return the caller's identity, role, parent, project metadata, and available tools.",
InputSchema: objectSchema(map[string]any{ InputSchema: objectSchema(nil, nil),
"include_tools": booleanProp("Include full available tool list."),
}, nil),
}, },
{ {
Name: "help", Name: "help",
@@ -412,17 +378,6 @@ func toolCatalog(role CallerRole, taskBound bool) []toolDescriptor {
}, nil), }, nil),
}, },
} }
filtered := tools[:0]
for _, tool := range tools {
if role == RoleSubAgent && tool.Name == "spawn_agent" {
continue
}
if !taskBound && tool.Name == "task_register_worktree" {
continue
}
filtered = append(filtered, tool)
}
return filtered
} }
// handleProtocolMethod handles MCP protocol-level methods. Returns // handleProtocolMethod handles MCP protocol-level methods. Returns
@@ -442,20 +397,13 @@ func (s *Server) handleProtocolMethod(callerID, method string, params json.RawMe
if protoVersion == "" { if protoVersion == "" {
protoVersion = supportedProtocolVersion protoVersion = supportedProtocolVersion
} }
taskBound := false
s.mu.Lock()
host := s.host
s.mu.Unlock()
if host != nil {
_, taskBound = host.CallerTask(callerID)
}
result := map[string]any{ result := map[string]any{
"protocolVersion": protoVersion, "protocolVersion": protoVersion,
"capabilities": map[string]any{ "capabilities": map[string]any{
"tools": map[string]any{"listChanged": false}, "tools": map[string]any{"listChanged": false},
}, },
"serverInfo": serverInfo, "serverInfo": serverInfo,
"instructions": serverInstructions(taskBound), "instructions": serverInstructions,
} }
return result, true, 0, "", nil return result, true, 0, "", nil
@@ -468,16 +416,7 @@ func (s *Server) handleProtocolMethod(callerID, method string, params json.RawMe
return map[string]any{}, true, 0, "", nil return map[string]any{}, true, 0, "", nil
case "tools/list": case "tools/list":
role := RoleOrchestrator return map[string]any{"tools": toolCatalog()}, true, 0, "", nil
taskBound := false
s.mu.Lock()
host := s.host
s.mu.Unlock()
if host != nil {
role = host.CallerRole(callerID)
_, taskBound = host.CallerTask(callerID)
}
return map[string]any{"tools": toolCatalog(role, taskBound)}, true, 0, "", nil
case "tools/call": case "tools/call":
var p struct { var p struct {
@@ -533,12 +472,25 @@ func (s *Server) handleProtocolMethod(callerID, method string, params json.RawMe
return nil, false, 0, "", nil return nil, false, 0, "", nil
} }
// wrapToolResult turns a tool result into an MCP tools/call response. // wrapToolResult turns a structured tool result into an MCP tools/call
// Structured values are exposed once under structuredContent; content // response. Plain strings (e.g. "ok") become text content; structured
// carries only a short model-readable summary to avoid duplicating // values are JSON-encoded into a single text block and also exposed
// large JSON payloads into the transcript. // under structuredContent so capable clients can read the shape.
func wrapToolResult(result any) map[string]any { func wrapToolResult(result any) map[string]any {
text := summarizeToolResult(result) var text string
switch v := result.(type) {
case nil:
text = "ok"
case string:
text = v
default:
b, err := json.Marshal(v)
if err != nil {
text = fmt.Sprintf("%v", v)
} else {
text = string(b)
}
}
out := map[string]any{ out := map[string]any{
"content": []map[string]any{{"type": "text", "text": text}}, "content": []map[string]any{{"type": "text", "text": text}},
"isError": false, "isError": false,
@@ -553,70 +505,3 @@ func wrapToolResult(result any) map[string]any {
} }
return out return out
} }
func summarizeToolResult(result any) string {
switch v := result.(type) {
case nil:
return "ok"
case string:
return v
case ProcessInfo:
return fmt.Sprintf("%s %s %s", v.ID, v.Kind, v.Status)
case []ProcessInfo:
return fmt.Sprintf("%d processes", len(v))
case ProcessStatus:
return fmt.Sprintf("%s %s %s", v.ID, v.Kind, v.Status)
case ProjectStatus:
return fmt.Sprintf("%d processes, %d scratchpads", len(v.Processes), len(v.Scratchpads))
case ProcessOutput:
return outputSummary(v.Mode, v.ContentBytes, v.Truncated, v.NewOffset)
case RawOutput:
return outputSummary("raw", v.ContentBytes, v.Truncated, v.NewOffset)
case SearchResult:
if v.Truncated {
return fmt.Sprintf("%d matches (truncated)", len(v.Matches))
}
return fmt.Sprintf("%d matches", len(v.Matches))
case SendInputResult:
if v.Tail != nil {
return "ok; tail included"
}
return "ok"
case TimerHandle:
return "timer " + v.ID
case TimerFireWhenIdleResponse:
if v.ID != "" {
return fmt.Sprintf("%s timer %s", v.Status, v.ID)
}
return v.Status
case []TimerInfo:
return fmt.Sprintf("%d timers", len(v))
case []scratchpad.Entry:
return fmt.Sprintf("%d scratchpads", len(v))
case ScratchpadReadResult:
if v.Truncated {
return fmt.Sprintf("%d/%d bytes from offset %d", v.ContentBytes, v.TotalBytes, v.Offset)
}
return fmt.Sprintf("%d bytes", v.ContentBytes)
case WhoAmI:
if v.ProcessID == "" {
return string(v.Role)
}
return fmt.Sprintf("%s %s", v.ProcessID, v.Role)
case HelpResponse:
return fmt.Sprintf("help: %s", v.Topic)
default:
return "ok"
}
}
func outputSummary(mode string, bytes int, truncated bool, offset int64) string {
s := fmt.Sprintf("%s output: %d bytes", mode, bytes)
if offset > 0 {
s += fmt.Sprintf(", offset %d", offset)
}
if truncated {
s += " (truncated)"
}
return s
}
-117
View File
@@ -2,7 +2,6 @@ package mcp
import ( import (
"encoding/json" "encoding/json"
"strings"
"testing" "testing"
) )
@@ -44,9 +43,6 @@ func TestInitializeReturnsCapabilities(t *testing.T) {
if !ok || instructions == "" { if !ok || instructions == "" {
t.Fatalf("instructions missing or wrong type: %+v", parsed.Result) t.Fatalf("instructions missing or wrong type: %+v", parsed.Result)
} }
if len(instructions) > 320 {
t.Fatalf("instructions too verbose: %d chars", len(instructions))
}
} }
func TestInitializedNotificationSuppressesResponse(t *testing.T) { func TestInitializedNotificationSuppressesResponse(t *testing.T) {
@@ -78,9 +74,6 @@ func TestToolsListReturnsConcreteSchemas(t *testing.T) {
if parsed.Error != nil { if parsed.Error != nil {
t.Fatalf("tools/list returned error: %+v", parsed.Error) t.Fatalf("tools/list returned error: %+v", parsed.Error)
} }
if len(resp) > 12000 {
t.Fatalf("tools/list response too large: %d bytes", len(resp))
}
tools, ok := parsed.Result["tools"].([]interface{}) tools, ok := parsed.Result["tools"].([]interface{})
if !ok { if !ok {
t.Fatalf("tools not array: %+v", parsed.Result) t.Fatalf("tools not array: %+v", parsed.Result)
@@ -119,53 +112,6 @@ func TestToolsListReturnsConcreteSchemas(t *testing.T) {
} }
} }
func TestToolsListTaskRegisterWorktreeIsTaskBound(t *testing.T) {
unbound := toolsListNames(t, &Server{})
if containsString(unbound, "task_register_worktree") {
t.Fatalf("unbound tools leaked task_register_worktree: %v", unbound)
}
s := &Server{}
s.SetHost(&blockingToolHost{task: TaskInfo{ID: "task_1", Title: "Fix sidebar"}})
bound := toolsListNames(t, s)
if !containsString(bound, "task_register_worktree") {
t.Fatalf("task-bound tools missing task_register_worktree: %v", bound)
}
}
func TestInitializeTaskInstructionsAreTaskBound(t *testing.T) {
unbound := initializeInstructions(t, &Server{})
if strings.Contains(unbound, "task_register_worktree") {
t.Fatalf("unbound initialize leaked task instruction: %q", unbound)
}
s := &Server{}
s.SetHost(&blockingToolHost{task: TaskInfo{ID: "task_1", Title: "Fix sidebar"}})
bound := initializeInstructions(t, s)
if !strings.Contains(bound, "task_register_worktree") {
t.Fatalf("task-bound initialize missing worktree instruction: %q", bound)
}
}
func TestWrapToolResultDoesNotDuplicateStructuredJSON(t *testing.T) {
result := ProcessOutput{
Content: strings.Repeat("x", 1024),
Mode: "stream",
NewOffset: 2048,
ContentBytes: 1024,
}
wrapped := wrapToolResult(result)
if wrapped["structuredContent"] == nil {
t.Fatalf("structuredContent missing: %#v", wrapped)
}
content := wrapped["content"].([]map[string]any)
text := content[0]["text"].(string)
if strings.Contains(text, result.Content) {
t.Fatalf("content duplicated structured payload: %q", text)
}
if !strings.Contains(text, "stream output") {
t.Fatalf("summary text should identify output, got %q", text)
}
}
func TestPingReturnsEmptyObject(t *testing.T) { func TestPingReturnsEmptyObject(t *testing.T) {
s := &Server{} s := &Server{}
req := []byte(`{"jsonrpc":"2.0","id":3,"method":"ping"}`) req := []byte(`{"jsonrpc":"2.0","id":3,"method":"ping"}`)
@@ -188,69 +134,6 @@ func TestPingReturnsEmptyObject(t *testing.T) {
} }
} }
func toolsListNames(t *testing.T, s *Server) []string {
t.Helper()
req := []byte(`{"jsonrpc":"2.0","id":2,"method":"tools/list"}`)
resp := s.dispatch("caller", req)
if resp == nil {
t.Fatal("expected response for tools/list")
}
var parsed struct {
Result struct {
Tools []struct {
Name string `json:"name"`
} `json:"tools"`
} `json:"result"`
Error *struct {
Message string `json:"message"`
} `json:"error"`
}
if err := json.Unmarshal(resp, &parsed); err != nil {
t.Fatalf("parse tools/list: %v\n%s", err, resp)
}
if parsed.Error != nil {
t.Fatalf("tools/list error: %+v", parsed.Error)
}
out := make([]string, 0, len(parsed.Result.Tools))
for _, tool := range parsed.Result.Tools {
out = append(out, tool.Name)
}
return out
}
func initializeInstructions(t *testing.T, s *Server) string {
t.Helper()
req := []byte(`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}}`)
resp := s.dispatch("caller", req)
if resp == nil {
t.Fatal("expected response for initialize")
}
var parsed struct {
Result struct {
Instructions string `json:"instructions"`
} `json:"result"`
Error *struct {
Message string `json:"message"`
} `json:"error"`
}
if err := json.Unmarshal(resp, &parsed); err != nil {
t.Fatalf("parse initialize: %v\n%s", err, resp)
}
if parsed.Error != nil {
t.Fatalf("initialize error: %+v", parsed.Error)
}
return parsed.Result.Instructions
}
func containsString(haystack []string, needle string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}
func TestTypedInvalidArgsMapToInvalidParams(t *testing.T) { func TestTypedInvalidArgsMapToInvalidParams(t *testing.T) {
for _, errKind := range []string{ErrorKindInvalidArgs, ErrorKindInvalidKind} { for _, errKind := range []string{ErrorKindInvalidArgs, ErrorKindInvalidKind} {
_, code, msg, data := mapToolError(Errorf(errKind, "bad args")) _, code, msg, data := mapToolError(Errorf(errKind, "bad args"))
+43 -130
View File
@@ -60,7 +60,6 @@ type ToolHost interface {
// callers default to RoleOrchestrator (treated as a top-level peer) // callers default to RoleOrchestrator (treated as a top-level peer)
// so they don't get silently denied. // so they don't get silently denied.
CallerRole(processID string) CallerRole CallerRole(processID string) CallerRole
CallerTask(processID string) (TaskInfo, bool)
// Lifecycle (SPEC §7). // Lifecycle (SPEC §7).
SpawnAgent(callerID string, args SpawnAgentArgs) (ProcessInfo, error) SpawnAgent(callerID string, args SpawnAgentArgs) (ProcessInfo, error)
@@ -75,10 +74,10 @@ type ToolHost interface {
// Inspection. // Inspection.
ListProcesses(callerID, kindFilter string) []ProcessInfo ListProcesses(callerID, kindFilter string) []ProcessInfo
GetProcessStatus(callerID, processID string) (ProcessStatus, error) GetProcessStatus(callerID, processID string) (ProcessStatus, error)
GetProjectStatus(callerID string, includeTools bool) (ProjectStatus, error) GetProjectStatus(callerID string) (ProjectStatus, error)
GetProcessOutput(callerID string, args ProcessOutputArgs) (ProcessOutput, error) GetProcessOutput(callerID, processID, mode string, sinceOffset int64) (ProcessOutput, error)
GetProcessRawOutput(callerID string, args RawOutputArgs) (RawOutput, error) GetProcessRawOutput(callerID, processID string, sinceOffset int64) (RawOutput, error)
SearchOutput(callerID string, args SearchOutputArgs) (SearchResult, error) SearchOutput(callerID, processID, pattern, kind string, limit int) (SearchResult, error)
WaitForPattern(callerID, processID, pattern string, timeoutSeconds float64, scope string) (matched bool, snippet string, err error) WaitForPattern(callerID, processID, pattern string, timeoutSeconds float64, scope string) (matched bool, snippet string, err error)
GetProcessPorts(callerID, processID string) ([]PortSighting, error) GetProcessPorts(callerID, processID string) ([]PortSighting, error)
@@ -98,15 +97,14 @@ type ToolHost interface {
TimerList(callerID string) ([]TimerInfo, error) TimerList(callerID string) ([]TimerInfo, error)
// Scratchpads. // Scratchpads.
ScratchpadList() ([]scratchpad.Entry, error) ScratchpadList(callerID string) ([]scratchpad.Entry, error)
ScratchpadRead(args ScratchpadReadArgs) (ScratchpadReadResult, error) ScratchpadRead(callerID, name string) (content string, revision string, err error)
ScratchpadWrite(name, content, expectedRevision string) (revision string, err error) ScratchpadWrite(callerID, name, content, expectedRevision string) (revision string, err error)
ScratchpadAppend(name, content string) error ScratchpadAppend(callerID, name, content string) error
ScratchpadDelete(name string) error ScratchpadDelete(callerID, name string) error
RegisterTaskWorktree(callerID string, args TaskRegisterWorktreeArgs) (TaskInfo, error)
// Meta. // Meta.
WhoAmI(callerID string, includeTools bool) WhoAmI WhoAmI(callerID string) WhoAmI
Help(callerID, topic string) HelpResponse Help(callerID, topic string) HelpResponse
} }
@@ -155,44 +153,18 @@ type Cursor struct {
type ProjectStatus struct { type ProjectStatus struct {
Project ProjectMeta `json:"project"` Project ProjectMeta `json:"project"`
Caller WhoAmI `json:"caller"` Caller WhoAmI `json:"caller"`
Task *TaskInfo `json:"task,omitempty"`
Processes []ProcessInfo `json:"processes"` Processes []ProcessInfo `json:"processes"`
Scratchpads []scratchpad.Entry `json:"scratchpads"` Scratchpads []scratchpad.Entry `json:"scratchpads"`
} }
type ProjectStatusArgs struct {
IncludeTools bool `json:"include_tools"`
}
// ProjectMeta is the project root info echoed in many payloads. // ProjectMeta is the project root info echoed in many payloads.
type ProjectMeta struct { type ProjectMeta struct {
Path string `json:"path"` Path string `json:"path"`
Key string `json:"key"` Key string `json:"key"`
} }
type TaskInfo struct { // ProcessOutput is the get_process_output payload. SPEC §7 enriches
ID string `json:"id"` // the old read_output result with screen geometry + version.
Title string `json:"title"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
Worktrees []TaskWorktree `json:"worktrees,omitempty"`
}
type TaskWorktree struct {
Path string `json:"path"`
Branch string `json:"branch,omitempty"`
CreatedByProcessID string `json:"created_by_process_id,omitempty"`
RegisteredAt string `json:"registered_at,omitempty"`
}
type TaskRegisterWorktreeArgs struct {
Path string `json:"path"`
Branch string `json:"branch,omitempty"`
}
// ProcessOutput is the get_process_output payload. By default it is
// canonical text with light metadata; include_meta restores screen
// geometry + version, and raw requests return stream bytes.
type ProcessOutput struct { type ProcessOutput struct {
Content string `json:"content"` Content string `json:"content"`
Mode string `json:"mode"` Mode string `json:"mode"`
@@ -200,24 +172,10 @@ type ProcessOutput struct {
ActiveScreen string `json:"active_screen,omitempty"` ActiveScreen string `json:"active_screen,omitempty"`
Rows int `json:"rows,omitempty"` Rows int `json:"rows,omitempty"`
Cols int `json:"cols,omitempty"` Cols int `json:"cols,omitempty"`
Cursor *Cursor `json:"cursor,omitempty"` Cursor Cursor `json:"cursor"`
IdleMS int64 `json:"idle_ms,omitempty"` IdleMS int64 `json:"idle_ms,omitempty"`
Status string `json:"status,omitempty"` Status string `json:"status,omitempty"`
ScreenVersion int64 `json:"screen_version,omitempty"` ScreenVersion int64 `json:"screen_version,omitempty"`
ContentBytes int `json:"content_bytes,omitempty"`
Truncated bool `json:"truncated,omitempty"`
TruncatedBytes int `json:"truncated_bytes,omitempty"`
Canonicalized bool `json:"canonicalized,omitempty"`
}
type ProcessOutputArgs struct {
ProcessID string `json:"process_id"`
Mode string `json:"mode"`
SinceOffset int64 `json:"since_offset"`
MaxBytes int `json:"max_bytes"`
MaxLines int `json:"max_lines"`
Raw bool `json:"raw"`
IncludeMeta bool `json:"include_meta"`
} }
// RawOutput is the get_process_raw_output payload — ANSI preserved. // RawOutput is the get_process_raw_output payload — ANSI preserved.
@@ -225,15 +183,6 @@ type RawOutput struct {
Content string `json:"content"` Content string `json:"content"`
NewOffset int64 `json:"new_offset"` NewOffset int64 `json:"new_offset"`
Status string `json:"status,omitempty"` Status string `json:"status,omitempty"`
ContentBytes int `json:"content_bytes,omitempty"`
Truncated bool `json:"truncated,omitempty"`
TruncatedBytes int `json:"truncated_bytes,omitempty"`
}
type RawOutputArgs struct {
ProcessID string `json:"process_id"`
SinceOffset int64 `json:"since_offset"`
MaxBytes int `json:"max_bytes"`
} }
// SearchResult is search_output's payload. // SearchResult is search_output's payload.
@@ -242,14 +191,6 @@ type SearchResult struct {
Truncated bool `json:"truncated"` Truncated bool `json:"truncated"`
} }
type SearchOutputArgs struct {
ProcessID string `json:"process_id"`
Pattern string `json:"pattern"`
Kind string `json:"kind"`
Limit int `json:"limit"`
MaxBytes int `json:"max_bytes"`
}
type SearchMatch struct { type SearchMatch struct {
LineNo int `json:"line_no"` LineNo int `json:"line_no"`
Text string `json:"text"` Text string `json:"text"`
@@ -304,7 +245,6 @@ type TimerInfo struct {
ID string `json:"timer_id"` ID string `json:"timer_id"`
Label string `json:"label,omitempty"` Label string `json:"label,omitempty"`
Body string `json:"body,omitempty"` Body string `json:"body,omitempty"`
BodyTruncated bool `json:"body_truncated,omitempty"`
Kind string `json:"kind"` // "delay" | "idle_any" | "idle_all" Kind string `json:"kind"` // "delay" | "idle_any" | "idle_all"
Status string `json:"status"` // "pending" | "paused" Status string `json:"status"` // "pending" | "paused"
OwnerID string `json:"owner_process_id"` OwnerID string `json:"owner_process_id"`
@@ -348,7 +288,6 @@ type SendInputArgs struct {
Submit *bool `json:"submit"` Submit *bool `json:"submit"`
WaitMS int `json:"wait_ms"` WaitMS int `json:"wait_ms"`
TailMode string `json:"tail_mode"` // "none" | "stream" | "grid" TailMode string `json:"tail_mode"` // "none" | "stream" | "grid"
TailMaxBytes int `json:"tail_max_bytes"`
} }
// SendInputResult is the return shape of send_input. // SendInputResult is the return shape of send_input.
@@ -364,31 +303,9 @@ type WhoAmI struct {
Role CallerRole `json:"role"` Role CallerRole `json:"role"`
ParentProcessID string `json:"parent_process_id,omitempty"` ParentProcessID string `json:"parent_process_id,omitempty"`
Project ProjectMeta `json:"project"` Project ProjectMeta `json:"project"`
Task *TaskInfo `json:"task,omitempty"`
AvailableTools []string `json:"available_tools"` AvailableTools []string `json:"available_tools"`
} }
type WhoAmIArgs struct {
IncludeTools bool `json:"include_tools"`
}
type ScratchpadReadArgs struct {
Name string `json:"name"`
Offset int `json:"offset"`
MaxBytes int `json:"max_bytes"`
}
type ScratchpadReadResult struct {
Content string `json:"content"`
Revision string `json:"revision"`
Offset int `json:"offset,omitempty"`
NextOffset int `json:"next_offset,omitempty"`
ContentBytes int `json:"content_bytes,omitempty"`
TotalBytes int `json:"total_bytes,omitempty"`
Truncated bool `json:"truncated,omitempty"`
TruncatedBytes int `json:"truncated_bytes,omitempty"`
}
// HelpResponse is the help return shape. // HelpResponse is the help return shape.
type HelpResponse struct { type HelpResponse struct {
Topic string `json:"topic"` Topic string `json:"topic"`
@@ -590,51 +507,61 @@ func callTool(h ToolHost, callerID, method string, params json.RawMessage) (any,
return st, 0, "", nil return st, 0, "", nil
case "get_project_status": case "get_project_status":
var p ProjectStatusArgs ps, err := h.GetProjectStatus(callerID)
_ = unmarshalParamsOptional(params, &p)
ps, err := h.GetProjectStatus(callerID, p.IncludeTools)
if err != nil { if err != nil {
return mapToolError(err) return mapToolError(err)
} }
return ps, 0, "", nil return ps, 0, "", nil
case "get_process_output": case "get_process_output":
var p ProcessOutputArgs var p struct {
ProcessID string `json:"process_id"`
Mode string `json:"mode"`
SinceOffset int64 `json:"since_offset"`
}
if err := unmarshalParams(params, &p); err != nil { if err := unmarshalParams(params, &p); err != nil {
return nil, codeInvalidParams, err.Error(), nil return nil, codeInvalidParams, err.Error(), nil
} }
if p.Mode == "" { if p.Mode == "" {
p.Mode = "grid" p.Mode = "grid"
} }
out, err := h.GetProcessOutput(callerID, p) out, err := h.GetProcessOutput(callerID, p.ProcessID, p.Mode, p.SinceOffset)
if err != nil { if err != nil {
return mapToolError(err) return mapToolError(err)
} }
return out, 0, "", nil return out, 0, "", nil
case "get_process_raw_output": case "get_process_raw_output":
var p RawOutputArgs var p struct {
ProcessID string `json:"process_id"`
SinceOffset int64 `json:"since_offset"`
}
if err := unmarshalParams(params, &p); err != nil { if err := unmarshalParams(params, &p); err != nil {
return nil, codeInvalidParams, err.Error(), nil return nil, codeInvalidParams, err.Error(), nil
} }
out, err := h.GetProcessRawOutput(callerID, p) out, err := h.GetProcessRawOutput(callerID, p.ProcessID, p.SinceOffset)
if err != nil { if err != nil {
return mapToolError(err) return mapToolError(err)
} }
return out, 0, "", nil return out, 0, "", nil
case "search_output": case "search_output":
var p SearchOutputArgs var p struct {
ProcessID string `json:"process_id"`
Pattern string `json:"pattern"`
Kind string `json:"kind"`
Limit int `json:"limit"`
}
if err := unmarshalParams(params, &p); err != nil { if err := unmarshalParams(params, &p); err != nil {
return nil, codeInvalidParams, err.Error(), nil return nil, codeInvalidParams, err.Error(), nil
} }
if p.Limit <= 0 { if p.Limit <= 0 {
p.Limit = 10 p.Limit = 20
} }
if p.Kind == "" { if p.Kind == "" {
p.Kind = "rendered" p.Kind = "rendered"
} }
res, err := h.SearchOutput(callerID, p) res, err := h.SearchOutput(callerID, p.ProcessID, p.Pattern, p.Kind, p.Limit)
if err != nil { if err != nil {
return mapToolError(err) return mapToolError(err)
} }
@@ -797,22 +724,24 @@ func callTool(h ToolHost, callerID, method string, params json.RawMessage) (any,
return ts, 0, "", nil return ts, 0, "", nil
case "scratchpad_list": case "scratchpad_list":
entries, err := h.ScratchpadList() entries, err := h.ScratchpadList(callerID)
if err != nil { if err != nil {
return nil, codeInternal, err.Error(), nil return nil, codeInternal, err.Error(), nil
} }
return entries, 0, "", nil return entries, 0, "", nil
case "scratchpad_read": case "scratchpad_read":
var p ScratchpadReadArgs var p struct {
Name string `json:"name"`
}
if err := unmarshalParams(params, &p); err != nil { if err := unmarshalParams(params, &p); err != nil {
return nil, codeInvalidParams, err.Error(), nil return nil, codeInvalidParams, err.Error(), nil
} }
res, err := h.ScratchpadRead(p) content, rev, err := h.ScratchpadRead(callerID, p.Name)
if err != nil { if err != nil {
return nil, codeInternal, err.Error(), nil return nil, codeInternal, err.Error(), nil
} }
return res, 0, "", nil return map[string]any{"content": content, "revision": rev}, 0, "", nil
case "scratchpad_write": case "scratchpad_write":
var p struct { var p struct {
@@ -823,7 +752,7 @@ func callTool(h ToolHost, callerID, method string, params json.RawMessage) (any,
if err := unmarshalParams(params, &p); err != nil { if err := unmarshalParams(params, &p); err != nil {
return nil, codeInvalidParams, err.Error(), 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 { if err != nil {
// Optimistic-concurrency miss returns ok:false + current_revision // Optimistic-concurrency miss returns ok:false + current_revision
// rather than a JSON-RPC error so callers can re-read + merge. // rather than a JSON-RPC error so callers can re-read + merge.
@@ -843,7 +772,7 @@ func callTool(h ToolHost, callerID, method string, params json.RawMessage) (any,
if err := unmarshalParams(params, &p); err != nil { if err := unmarshalParams(params, &p); err != nil {
return nil, codeInvalidParams, err.Error(), 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 nil, codeInternal, err.Error(), nil
} }
return map[string]any{"ok": true}, 0, "", nil return map[string]any{"ok": true}, 0, "", nil
@@ -855,29 +784,13 @@ func callTool(h ToolHost, callerID, method string, params json.RawMessage) (any,
if err := unmarshalParams(params, &p); err != nil { if err := unmarshalParams(params, &p); err != nil {
return nil, codeInvalidParams, err.Error(), nil return nil, codeInvalidParams, err.Error(), nil
} }
if err := h.ScratchpadDelete(p.Name); err != nil { if err := h.ScratchpadDelete(callerID, p.Name); err != nil {
return nil, codeInternal, err.Error(), nil return nil, codeInternal, err.Error(), nil
} }
return map[string]any{"ok": true}, 0, "", nil return map[string]any{"ok": true}, 0, "", nil
case "task_register_worktree":
if _, ok := h.CallerTask(callerID); !ok {
return nil, codeRoleForbidden, "task_register_worktree: caller is not attached to a task", structuredKind(ErrorKindRoleForbidden)
}
var p TaskRegisterWorktreeArgs
if err := unmarshalParams(params, &p); err != nil {
return nil, codeInvalidParams, err.Error(), nil
}
if p.Path == "" {
return nil, codeInvalidParams, "task_register_worktree: path required", nil
}
info, err := h.RegisterTaskWorktree(callerID, p)
return mapToolResult(info, err)
case "whoami": case "whoami":
var p WhoAmIArgs return h.WhoAmI(callerID), 0, "", nil
_ = unmarshalParamsOptional(params, &p)
return h.WhoAmI(callerID, p.IncludeTools), 0, "", nil
case "help": case "help":
var p struct { var p struct {
+176
View 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
View 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
View 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
View 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()
}
+36 -11
View File
@@ -6,12 +6,22 @@ import (
"io" "io"
"os" "os"
"os/exec" "os/exec"
"sync"
"syscall"
cpty "github.com/creack/pty" cpty "github.com/creack/pty"
) )
// PTY holds a child process attached to a pseudo-terminal master fd. // 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 { type PTY struct {
mu sync.Mutex
master *os.File master *os.File
cmd *exec.Cmd cmd *exec.Cmd
} }
@@ -24,9 +34,8 @@ func Start(argv []string, env []string, workDir string, cols, rows uint16) (*PTY
return nil, fmt.Errorf("pty: empty argv") return nil, fmt.Errorf("pty: empty argv")
} }
cmd := exec.Command(argv[0], argv[1:]...) cmd := exec.Command(argv[0], argv[1:]...)
if workDir != "" {
cmd.Dir = workDir cmd.Dir = workDir
} cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true}
if env != nil { if env != nil {
cmd.Env = ensureTerm(env) cmd.Env = ensureTerm(env)
} else { } else {
@@ -45,24 +54,33 @@ func Start(argv []string, env []string, workDir string, cols, rows uint16) (*PTY
} }
func (p *PTY) Read(b []byte) (int, 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 0, io.ErrClosedPipe
} }
return p.master.Read(b) return m.Read(b)
} }
func (p *PTY) Write(b []byte) (int, error) { 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 0, io.ErrClosedPipe
} }
return p.master.Write(b) return m.Write(b)
} }
func (p *PTY) Resize(cols, rows uint16) error { 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 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. // Wait blocks until the child exits and returns its exit error if any.
@@ -83,14 +101,21 @@ func (p *PTY) Pid() int {
// Close terminates the child (best effort) and releases the master fd. // Close terminates the child (best effort) and releases the master fd.
func (p *PTY) Close() error { func (p *PTY) Close() error {
p.mu.Lock()
m := p.master
p.master = nil
p.mu.Unlock()
var firstErr error var firstErr error
if p.master != nil { if m != nil {
if err := p.master.Close(); err != nil && firstErr == nil { if err := m.Close(); err != nil {
firstErr = err firstErr = err
} }
p.master = nil
} }
if p.cmd != nil && p.cmd.Process != 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() _ = p.cmd.Process.Kill()
} }
return firstErr return firstErr
+56 -12
View File
@@ -1,22 +1,29 @@
package pty package pty
import ( import (
"io" "bytes"
"errors"
"os"
"path/filepath"
"strconv"
"strings" "strings"
"syscall"
"testing" "testing"
"time"
) )
func TestStartHonorsWorkDir(t *testing.T) { func TestStartUsesWorkDir(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
p, err := Start([]string{"sh", "-lc", "pwd"}, nil, dir, 80, 24) p, err := Start([]string{"sh", "-c", "pwd"}, nil, dir, 80, 24)
if err != nil { if err != nil {
t.Fatalf("start: %v", err) t.Fatalf("Start: %v", err)
} }
defer p.Close() defer p.Close()
var out strings.Builder var out bytes.Buffer
buf := make([]byte, 256) buf := make([]byte, 256)
for { deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
n, err := p.Read(buf) n, err := p.Read(buf)
if n > 0 { if n > 0 {
out.Write(buf[:n]) out.Write(buf[:n])
@@ -25,16 +32,53 @@ func TestStartHonorsWorkDir(t *testing.T) {
} }
} }
if err != nil { if err != nil {
if err == io.EOF || strings.Contains(err.Error(), "input/output error") {
break break
} }
t.Fatalf("read: %v", err) }
_ = p.Wait()
if got := strings.TrimSpace(out.String()); got != dir {
t.Fatalf("pwd output = %q, want %q", got, dir)
} }
} }
if err := p.Wait(); err != nil {
t.Fatalf("wait: %v", err) 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)
} }
if got := out.String(); !strings.Contains(got, dir) { deadline := time.Now().Add(5 * time.Second)
t.Fatalf("pwd output %q does not contain %q", got, dir) 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)
}
-264
View File
@@ -1,264 +0,0 @@
// Package task stores manual project-local tasks and the worktrees agents
// register while working on those tasks.
package task
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// Task is one manual project-local task.
type Task struct {
ID string `json:"id"`
Title string `json:"title"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Worktrees []Worktree `json:"worktrees,omitempty"`
}
// Worktree is a git worktree path registered by a task-bound agent.
type Worktree struct {
Path string `json:"path"`
Branch string `json:"branch,omitempty"`
CreatedByProcessID string `json:"created_by_process_id,omitempty"`
RegisteredAt string `json:"registered_at"`
}
// Store is one project's tasks file. Safe for concurrent use.
type Store struct {
path string
mu sync.Mutex
tasks map[string]Task
order []string
}
// Open loads or creates the task store for projectKey.
func Open(projectKey string) (*Store, error) {
if projectKey == "" {
return nil, errors.New("task.Open: empty project key")
}
base, err := dataDir()
if err != nil {
return nil, err
}
dir := filepath.Join(base, "projects", projectKey)
if err := os.MkdirAll(dir, 0o700); err != nil {
return nil, fmt.Errorf("task: mkdir %s: %w", dir, err)
}
path := filepath.Join(dir, "tasks.json")
s := &Store{path: path, tasks: make(map[string]Task)}
if err := s.loadLocked(); err != nil {
return nil, err
}
return s, nil
}
func dataDir() (string, error) {
if h := os.Getenv("XDG_DATA_HOME"); h != "" {
return filepath.Join(h, "patterm"), nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".local", "share", "patterm"), nil
}
// Path returns the on-disk file path. Used by tests and diagnostics.
func (s *Store) Path() string { return s.path }
// List returns tasks in creation order.
func (s *Store) List() []Task {
s.mu.Lock()
defer s.mu.Unlock()
out := make([]Task, 0, len(s.order))
for _, id := range s.order {
if t, ok := s.tasks[id]; ok {
out = append(out, cloneTask(t))
}
}
return out
}
// Get returns one task by id.
func (s *Store) Get(id string) (Task, bool) {
s.mu.Lock()
defer s.mu.Unlock()
t, ok := s.tasks[id]
if !ok {
return Task{}, false
}
return cloneTask(t), true
}
// Create inserts a new task with title.
func (s *Store) Create(title string) (Task, error) {
title = strings.TrimSpace(title)
if title == "" {
return Task{}, errors.New("task.Create: empty title")
}
s.mu.Lock()
defer s.mu.Unlock()
now := timestamp()
t := Task{ID: s.mintIDLocked(), Title: title, CreatedAt: now, UpdatedAt: now}
s.tasks[t.ID] = t
s.order = append(s.order, t.ID)
if err := s.saveLocked(); err != nil {
return Task{}, err
}
return cloneTask(t), nil
}
// Rename updates a task title.
func (s *Store) Rename(id, title string) (Task, error) {
title = strings.TrimSpace(title)
if title == "" {
return Task{}, errors.New("task.Rename: empty title")
}
s.mu.Lock()
defer s.mu.Unlock()
t, ok := s.tasks[id]
if !ok {
return Task{}, fmt.Errorf("task.Rename: no such task %q", id)
}
t.Title = title
t.UpdatedAt = timestamp()
s.tasks[id] = t
if err := s.saveLocked(); err != nil {
return Task{}, err
}
return cloneTask(t), nil
}
// RegisterWorktree records or updates a worktree for a task. Paths are
// de-duplicated by cleaned absolute path.
func (s *Store) RegisterWorktree(taskID string, wt Worktree) (Task, error) {
path := strings.TrimSpace(wt.Path)
if path == "" {
return Task{}, errors.New("task.RegisterWorktree: empty path")
}
if !filepath.IsAbs(path) {
abs, err := filepath.Abs(path)
if err != nil {
return Task{}, err
}
path = abs
}
wt.Path = filepath.Clean(path)
wt.Branch = strings.TrimSpace(wt.Branch)
if wt.RegisteredAt == "" {
wt.RegisteredAt = timestamp()
}
s.mu.Lock()
defer s.mu.Unlock()
t, ok := s.tasks[taskID]
if !ok {
return Task{}, fmt.Errorf("task.RegisterWorktree: no such task %q", taskID)
}
replaced := false
for i, existing := range t.Worktrees {
if filepath.Clean(existing.Path) == wt.Path {
if wt.CreatedByProcessID == "" {
wt.CreatedByProcessID = existing.CreatedByProcessID
}
t.Worktrees[i] = wt
replaced = true
break
}
}
if !replaced {
t.Worktrees = append(t.Worktrees, wt)
}
t.UpdatedAt = timestamp()
s.tasks[taskID] = t
if err := s.saveLocked(); err != nil {
return Task{}, err
}
return cloneTask(t), nil
}
type fileShape struct {
Tasks []Task `json:"tasks"`
}
func (s *Store) loadLocked() error {
b, err := os.ReadFile(s.path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return fmt.Errorf("task: read %s: %w", s.path, err)
}
if len(b) == 0 {
return nil
}
var f fileShape
if err := json.Unmarshal(b, &f); err != nil {
return fmt.Errorf("task: parse %s: %w", s.path, err)
}
for _, t := range f.Tasks {
if t.ID == "" || strings.TrimSpace(t.Title) == "" {
continue
}
t.Title = strings.TrimSpace(t.Title)
if _, exists := s.tasks[t.ID]; !exists {
s.order = append(s.order, t.ID)
}
s.tasks[t.ID] = cloneTask(t)
}
return nil
}
func (s *Store) saveLocked() error {
out := make([]Task, 0, len(s.tasks))
for _, id := range s.order {
if t, ok := s.tasks[id]; ok {
out = append(out, cloneTask(t))
}
}
body, err := json.MarshalIndent(fileShape{Tasks: out}, "", " ")
if err != nil {
return err
}
body = append(body, '\n')
tmp := s.path + ".tmp"
if err := os.WriteFile(tmp, body, 0o600); err != nil {
return fmt.Errorf("task: write %s: %w", tmp, err)
}
if err := os.Rename(tmp, s.path); err != nil {
return fmt.Errorf("task: rename %s: %w", s.path, err)
}
return nil
}
func (s *Store) mintIDLocked() string {
for {
var b [4]byte
if _, err := rand.Read(b[:]); err != nil {
return fmt.Sprintf("task_%d", time.Now().UnixNano())
}
id := "task_" + hex.EncodeToString(b[:])
if _, exists := s.tasks[id]; !exists {
return id
}
}
}
func cloneTask(t Task) Task {
if t.Worktrees != nil {
t.Worktrees = append([]Worktree(nil), t.Worktrees...)
}
return t
}
func timestamp() string { return time.Now().UTC().Format(time.RFC3339) }
-113
View File
@@ -1,113 +0,0 @@
package task
import (
"os"
"path/filepath"
"testing"
)
func TestCreateListAndReload(t *testing.T) {
dir := t.TempDir()
t.Setenv("XDG_DATA_HOME", dir)
s1, err := Open("projkey")
if err != nil {
t.Fatalf("open: %v", err)
}
created, err := s1.Create(" Ship task sidebar ")
if err != nil {
t.Fatalf("create: %v", err)
}
if created.ID == "" || created.Title != "Ship task sidebar" || created.CreatedAt == "" || created.UpdatedAt == "" {
t.Fatalf("created task incomplete: %+v", created)
}
s2, err := Open("projkey")
if err != nil {
t.Fatalf("reopen: %v", err)
}
got := s2.List()
if len(got) != 1 {
t.Fatalf("tasks len = %d, want 1 (%v)", len(got), got)
}
if got[0].ID != created.ID || got[0].Title != "Ship task sidebar" {
t.Fatalf("reload mismatch: got %+v want %+v", got[0], created)
}
if _, err := os.Stat(s2.Path()); err != nil {
t.Fatalf("stat tasks.json: %v", err)
}
}
func TestCreateRequiresTitle(t *testing.T) {
t.Setenv("XDG_DATA_HOME", t.TempDir())
s, err := Open("projkey")
if err != nil {
t.Fatalf("open: %v", err)
}
if _, err := s.Create(" "); err == nil {
t.Fatalf("create with empty title should fail")
}
}
func TestRenameTask(t *testing.T) {
t.Setenv("XDG_DATA_HOME", t.TempDir())
s, err := Open("projkey")
if err != nil {
t.Fatalf("open: %v", err)
}
created, err := s.Create("Old")
if err != nil {
t.Fatalf("create: %v", err)
}
renamed, err := s.Rename(created.ID, "New")
if err != nil {
t.Fatalf("rename: %v", err)
}
if renamed.Title != "New" {
t.Fatalf("title = %q, want New", renamed.Title)
}
got, ok := s.Get(created.ID)
if !ok || got.Title != "New" {
t.Fatalf("get after rename = %+v, %v", got, ok)
}
}
func TestRegisterWorktreeDedupesByPath(t *testing.T) {
t.Setenv("XDG_DATA_HOME", t.TempDir())
s, err := Open("projkey")
if err != nil {
t.Fatalf("open: %v", err)
}
task, err := s.Create("Worktree task")
if err != nil {
t.Fatalf("create: %v", err)
}
path := filepath.Join(t.TempDir(), "repo", "..", "repo")
updated, err := s.RegisterWorktree(task.ID, Worktree{Path: path, Branch: "one", CreatedByProcessID: "p1"})
if err != nil {
t.Fatalf("register: %v", err)
}
if len(updated.Worktrees) != 1 {
t.Fatalf("worktrees len = %d, want 1", len(updated.Worktrees))
}
clean := filepath.Clean(path)
if updated.Worktrees[0].Path != clean {
t.Fatalf("path = %q, want %q", updated.Worktrees[0].Path, clean)
}
updated, err = s.RegisterWorktree(task.ID, Worktree{Path: clean, Branch: "two"})
if err != nil {
t.Fatalf("register duplicate: %v", err)
}
if len(updated.Worktrees) != 1 {
t.Fatalf("duplicate should replace, got %+v", updated.Worktrees)
}
if got := updated.Worktrees[0]; got.Branch != "two" || got.CreatedByProcessID != "p1" {
t.Fatalf("duplicate replace mismatch: %+v", got)
}
}
func TestOpenRequiresProjectKey(t *testing.T) {
if _, err := Open(""); err == nil {
t.Fatalf("open with empty project key should fail")
}
}