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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ billing the API.
Deck finds `claude` on the PATH it was started with, which is not always the one
your shell has. A tmux server started without nvm loaded, for example, finds a
system-wide install your shell never reaches. So starting a session names the
file it ran, and for `claude` and `cathode` the `claude` version that answers,
in the footer: `started /usr/local/bin/claude (2.0.9) in …`. A version you do
not recognise is a second install.
file it ran, and for `claude` and `cathode` the `claude` it finds and its
version, in the footer: `started /usr/local/bin/claude (2.0.9) in …`. A path or
version you did not expect means that install is not the one you updated.

## Use

Expand Down
44 changes: 31 additions & 13 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ Three rules the design rests on:
for the next event or for `inbox`. Inside a subagent, where hooks also
fire with `agent_id` set, because the subagent may never pass it on. On a
`Stop` already continuing from a stop hook, because Claude Code ends a
turn after eight continuations in a row and drops the hook's output. And
turn after eight continuations in a row whatever the hook replies. And
past `mailBudget`, because context over Claude Code's 10,000-character cap
becomes a file the agent is not asked to read.

Expand All @@ -375,14 +375,28 @@ Wiring the agent is where Deck knows most about the program it hosts
second config with `-mcp`. Any other agent gets no config file at all.

Starting one also names what ran (`ui/agentinfo.go`). The footer gives the file
the agent's name resolved to on PATH, and for `claude` and `cathode` — which
starts `claude` by name from the PATH it inherits — the `claude` that answers
and its `--version`. That check runs as a command off the UI's goroutine, and
its answer replaces the notice only if nothing newer has. It exists because a
second install is easy to have and invisible otherwise: a tmux server started
without nvm loaded found a stale system-wide `claude` that the user's own shell
never reached, and every session failed with a model the API no longer serves
while the same command typed by hand worked.
the agent ran, `Runner.Path`, which is `exec.Cmd`'s own answer rather than a
second lookup: the PATH hit for a bare name, or a path as given — absolute, or
relative to the directory it ran in. Working the path out again was tried and
named the wrong file: a lexical join takes `..` differently from the kernel
inside a symlinked directory. `checkCommandFile` refuses a path that is missing
or a directory with that reason, and hands the path to the kernel as written
for the same reason. For `claude`, and for `cathode`, whose default backend
starts `claude` by name from the PATH it inherits, the footer adds the `claude`
that PATH finds and its `--version`, run in the session's directory so a
relative path finds the same file.

That check runs as a command off the UI's goroutine, bounded by
`versionTimeout`, plus `WaitDelay` for a wrapper's child that still holds the
output pipe. The timeout kills the process it started, not that process's own
children. A failure is shown with the first line `claude` wrote to stderr, and a
timeout says so rather than reading as a crash. The answer replaces the notice
only if that notice is still the one from the same start, counted by
`Model.starts`, because two starts in one directory set identical notices. It
exists because a second install is easy to have and invisible otherwise: a tmux
server started without nvm loaded found a stale system-wide `claude` that the
user's own shell never reached, and every session failed with a model the API
no longer serves while the same command typed by hand worked.

`TestLiveClaudeSeesSiblings` drives a real `claude` against the server and is
the only test that proves config shape, transport, and tool naming actually
Expand Down Expand Up @@ -853,9 +867,12 @@ directory: that would put an agent to work in a tree the user believed was
untouched. A corrupt `state.json` fails the launch rather than starting empty,
because starting empty invites duplicate worktrees over sessions we cannot see.
Closing a session leaves its worktree on disk and says where. `model.fault`
holds errors the user must see and is never auto-cleared. Every write into an
agent pane goes through `toAgent`, so no keystroke, literal `^g` or wheel
notch can drop a failed write.
holds errors the user must see. Time never clears it; the next key Deck
handles itself does, because the footer was in view when it was pressed. A key
sent to an attached agent does not, and a fault that stayed for the whole run
hid every notice after it. Every write into an agent pane goes through
`toAgent`, so no keystroke, literal `^g` or wheel notch can drop a failed
write.

Reporting is not the same as abandoning. `ui.formProblem` puts a failure *into*
the open form instead of dismissing it, so the user keeps what they typed and
Expand Down Expand Up @@ -990,7 +1007,8 @@ already reads `Status`.
Without it the model stayed attached, so every key was written to a dead PTY.
The first one was refused and surfaced `send to agent: process has exited`,
which reads as a fault in Deck rather than as the agent having quit — and
the fault line is never auto-cleared, so it stayed on screen.
every later key went to the agent, which never clears the fault line, so it
stayed on screen.

The pane says what to do next, because the banner only said what had happened.
`↵` runs `attach`, which already restarts an exited runner rather than
Expand Down
17 changes: 13 additions & 4 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ type Config struct {

// Runner owns one PTY, one child process, and one emulated screen.
type Runner struct {
cfg Config
cfg Config
// path is the file exec runs: what PATH gave for a bare name, or the
// command as given — absolute, or relative to Dir, or to Deck's own
// directory when Dir is empty. Read off exec.Cmd rather than worked out
// again, because a second resolution can disagree with it.
path string
cmd *exec.Cmd
ptmx *os.File
term *vt.SafeEmulator
Expand Down Expand Up @@ -89,8 +94,9 @@ func Start(cfg Config) (*Runner, error) {
if cfg.Height < 5 {
cfg.Height = 5
}
if _, err := exec.LookPath(cfg.Command); err != nil {
return nil, fmt.Errorf("agent %q not found on PATH: %w", cfg.Command, err)
cmd := exec.Command(cfg.Command, cfg.Args...)
if cmd.Err != nil {
return nil, fmt.Errorf("agent %q %s: %w", cfg.Command, LookupReason(cmd.Err), cmd.Err)
}
// Checked here because the PTY start cannot say it. pty.StartWithSize sets
// SysProcAttr, and with that set os.StartProcess skips its own check of the
Expand All @@ -101,8 +107,10 @@ func Start(cfg Config) (*Runner, error) {
return nil, fmt.Errorf("start %s: session directory: %w", cfg.Command, err)
}
}
if err := checkCommandFile(cmd.Path, cfg.Dir); err != nil {
return nil, err
}

cmd := exec.Command(cfg.Command, cfg.Args...)
cmd.Dir = cfg.Dir
// TERM tells the agent what it may emit; it must match what the emulator
// below can parse. COLORTERM unlocks 24-bit colour in most TUIs.
Expand All @@ -120,6 +128,7 @@ func Start(cfg Config) (*Runner, error) {
term.SetScrollbackSize(scrollbackLines)

r := &Runner{
path: cmd.Path,
cfg: cfg,
cmd: cmd,
ptmx: ptmx,
Expand Down
79 changes: 79 additions & 0 deletions internal/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,82 @@ func TestScrubbedEnvDropsWhatAnAgentMustNotInherit(t *testing.T) {
t.Error("ScrubbedEnv did not drop anything")
}
}

// TestStartRunsARelativeCommandFromTheSessionDir: the child runs in Dir, so a
// relative command is a file there. It need not exist where Deck runs, and a
// check made from there refused a command that would have run.
func TestStartRunsARelativeCommandFromTheSessionDir(t *testing.T) {
dir := t.TempDir()
t.Chdir(t.TempDir())
if err := os.WriteFile(filepath.Join(dir, "agent"), []byte("#!/bin/sh\necho from-session-dir\nexec /bin/sleep 30\n"), 0o755); err != nil {
t.Fatal(err)
}

r, err := Start(Config{Command: "./agent", Dir: dir, Width: 80, Height: 24})
if err != nil {
t.Fatalf("Start refused a command that exists in the session dir: %v", err)
}
t.Cleanup(r.Stop)
if !screenContains(t, r, "from-session-dir", 5*time.Second) {
t.Error("the session dir's command did not run")
}
if r.Path() != "./agent" {
t.Errorf("Path() = %q, want the command as exec ran it", r.Path())
}
}

// TestStartRefusesADirectoryWithThatReason: exec would refuse it at fork as
// "permission denied", which sends someone after the wrong problem.
func TestStartRefusesADirectoryWithThatReason(t *testing.T) {
dir := t.TempDir()
if err := os.Mkdir(filepath.Join(dir, "adir"), 0o755); err != nil {
t.Fatal(err)
}
_, err := Start(Config{Command: "./adir", Dir: dir, Width: 80, Height: 24})
if err == nil || !strings.Contains(err.Error(), "is a directory") {
t.Errorf("err = %v, want it to say the command is a directory", err)
}
}

// TestStartChecksDotDotWhereTheKernelResolvesIt: inside a symlinked session
// dir, ".." is the link target's parent. A check that joined the path as text
// would look in the link's parent and refuse a command that runs.
func TestStartChecksDotDotWhereTheKernelResolvesIt(t *testing.T) {
root := t.TempDir()
target := filepath.Join(root, "real", "sub")
if err := os.MkdirAll(target, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "real", "agent"), []byte("#!/bin/sh\nexec /bin/sleep 30\n"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(root, "links"), 0o755); err != nil {
t.Fatal(err)
}
link := filepath.Join(root, "links", "sess")
if err := os.Symlink(target, link); err != nil {
t.Fatal(err)
}

r, err := Start(Config{Command: "../agent", Dir: link, Width: 80, Height: 24})
if err != nil {
t.Fatalf("Start refused ../agent that the kernel finds from the link's target: %v", err)
}
r.Stop()
}

// TestARelativePathEntrySaysSo: a command found only through "." on PATH is
// refused by exec, and was found — "not found on PATH" misdirects.
func TestARelativePathEntrySaysSo(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "dotagent"), []byte("#!/bin/sh\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Chdir(dir)
t.Setenv("PATH", ".")

_, err := Start(Config{Command: "dotagent", Width: 80, Height: 24})
if err == nil || !strings.Contains(err.Error(), "relative PATH entry") {
t.Errorf("err = %v, want it to name the relative PATH entry", err)
}
}
52 changes: 52 additions & 0 deletions internal/agent/executable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package agent

// Which file an agent runs: the path exec resolved, kept on the Runner, and
// the check that refuses a missing file or a directory with that reason
// rather than with the error the PTY start would give.

import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)

// Path is the file the agent runs. See Runner.path.
func (r *Runner) Path() string { return r.path }

// checkCommandFile refuses a command named by path that is missing or a
// directory, with that reason. exec would refuse it too, but at fork as
// "permission denied" for a directory. The path is handed to the kernel as
// written, joined by a plain separator rather than filepath.Join, which would
// take ".." textually where the kernel takes it after a symlink — the stat then
// checks the file exec will run. A bare name was already found on PATH.
func checkCommandFile(path, dir string) error {
if !strings.ContainsRune(path, filepath.Separator) {
return nil
}
at := path
if dir != "" && !filepath.IsAbs(path) {
at = dir + string(filepath.Separator) + path
}
info, err := os.Stat(at)
if err != nil {
return fmt.Errorf("agent %q: %w", path, err)
}
if info.IsDir() {
return fmt.Errorf("agent %q is a directory", path)
}
return nil
}

// LookupReason says why a bare command name did not resolve on PATH, for an
// error from exec.LookPath or exec.Cmd.Err. exec refuses a file found only
// through a relative PATH entry such as ".", and that file was found:
// reporting it as "not found" sent the reader after the wrong problem.
func LookupReason(err error) string {
if errors.Is(err, exec.ErrDot) {
return "found only through a relative PATH entry, which is refused — put an absolute directory on PATH"
}
return "not found on PATH"
}
6 changes: 3 additions & 3 deletions internal/coord/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ type hookInput struct {
// there reaches the subagent, which may never pass it on.
AgentID string `json:"agent_id"`
// StopHookActive is set on a Stop that is already continuing because of a
// stop hook. Claude Code ends the turn after eight such continuations in a
// row and drops the hook's output when it does, so delivering on one risks
// mail nobody reads. One continuation per turn is enough.
// stop hook. Claude Code's hook docs say that after eight continuations in
// a row it "overrides the next block and ends the turn", so mail handed over
// on one risks never being acted on. One continuation per turn is enough.
StopHookActive bool `json:"stop_hook_active"`
}

Expand Down
2 changes: 1 addition & 1 deletion internal/coord/delivery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func TestNoDeliveryInsideASubagent(t *testing.T) {
}

// TestNoDeliveryOnAContinuedStop: Claude Code ends a turn after eight stop
// continuations in a row and drops the hook's output, so a Stop that is
// continuations in a row, overriding the hook's reply, so a Stop that is
// already continuing hands nothing over and lets the turn end.
func TestNoDeliveryOnAContinuedStop(t *testing.T) {
c := start(t)
Expand Down
6 changes: 3 additions & 3 deletions internal/store/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func (s *State) SessionsFor(projectID string) []Session {
//
// The values the project's sessions hold are reassigned in their new order
// rather than recomputed, so a session opened later by the clock still takes
// a value below all of them. Equal values — a hand-edited file with no creation times — are
// raised to be distinct first, upwards only for the same reason; reassigning
// equal values would move nothing and still report a move.
// a value below all of them. Equal values — a hand-edited file with no
// creation times — are raised to be distinct first, upwards only for the same
// reason; reassigning equal values would move nothing and still report a move.
func (s *State) MoveSession(id string, to int) bool {
sess := s.Session(id)
if sess == nil {
Expand Down
Loading
Loading