diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 75a92e037..c7dc26782 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,6 +10,11 @@ # release builds the renderer on each OS again. # Whenever node-pty, better-sqlite3 or Electron changes, each runner builds # the native modules against Electron on its own OS, proving they build there. +# +# macOS runs only for main: PRs into it, pushes to it and runs by hand. PRs +# into develop skip it, since they are tested on a Mac before merging and the +# macOS runners can queue for many minutes; the release PR into main still +# checks macOS on a clean runner. name: CI @@ -116,7 +121,7 @@ jobs: strategy: fail-fast: false matrix: - os: [macos-15, ubuntu-latest, windows-latest] + os: ${{ fromJSON(github.base_ref == 'develop' && '["ubuntu-latest","windows-latest"]' || '["macos-15","ubuntu-latest","windows-latest"]') }} steps: - name: Checkout @@ -131,6 +136,10 @@ jobs: # and the costly one was cpu-features (ssh2's optional CPU probe): a # node-gyp build of one to two minutes on Windows. The native module # steps below are the rest of our postinstall. + env: + # Windows checks out on D: while the default cache is in the home + # directory on C:. Bun can hardlink packages on the same volume. + BUN_INSTALL_CACHE_DIR: ${{ github.workspace }}/.cache/bun-install run: bun install --frozen-lockfile --ignore-scripts - name: Cache native modules @@ -165,7 +174,7 @@ jobs: strategy: fail-fast: false matrix: - os: [macos-15, ubuntu-latest, windows-latest] + os: ${{ fromJSON(github.base_ref == 'develop' && '["ubuntu-latest","windows-latest"]' || '["macos-15","ubuntu-latest","windows-latest"]') }} steps: - name: Checkout diff --git a/.nexus/context/conventions.md b/.nexus/context/conventions.md index 96c0b8a0e..39d6ef19f 100644 --- a/.nexus/context/conventions.md +++ b/.nexus/context/conventions.md @@ -54,4 +54,4 @@ - 로그는 반드시 facade만 사용한다: main 프로세스는 `src/shared/log/main.ts`의 `createLogger(source)`, 렌더러는 `src/shared/log/renderer.ts`의 `createLogger(source)`. `console.log` / `console.error` 등 직접 호출 금지. - `source`는 모듈 또는 서브시스템 식별자를 바인딩하고, 관련 로그를 연결할 때 `correlationId`를 `meta`로 전달한다. - `internalMessage`는 로그에만 기록하고 화면에 노출하지 않는다. -- 전역 안전망(`src/main/error-safety-net.ts`, `src/renderer/services/window-error-handler.ts`, `src/renderer/components/ui/error-boundary.tsx`)은 놓친 에러의 최후 수단이다. 각 핸들러에서 이미 잡힌 에러를 다시 전역 안전망에 의존하지 않는다. +- 전역 안전망(`src/main/infra/error-safety-net.ts`, `src/renderer/services/window-error-handler.ts`, `src/renderer/components/ui/error-boundary.tsx`)은 놓친 에러의 최후 수단이다. 각 핸들러에서 이미 잡힌 에러를 다시 전역 안전망에 의존하지 않는다. diff --git a/build/store-release-notes.json b/build/store-release-notes.json index 70cad6e18..ac239a04c 100644 --- a/build/store-release-notes.json +++ b/build/store-release-notes.json @@ -1,5 +1,5 @@ { - "version": "0.34.1", - "ko-kr": "• 다른 컴퓨터의 세션 목록에서 워크스페이스별 워크트리를 펼치거나 접을 수 있습니다. 접힌 항목도 검색할 수 있습니다.\n• 원격 문서 미리보기는 앱으로 돌아와도 스크롤 위치를 유지합니다. 구버전 기기와 대화할 때 메시지가 중복 표시되는 문제와 Codex 분기에 실패한 뒤 빈 탭이 남는 문제를 고쳤습니다.", - "en-us": "• Expand or collapse worktrees by workspace in another computer’s session list. Search still finds collapsed items.\n• Remote document previews keep their scroll position when you return to the app. Fixed duplicate messages with older peers and an empty tab left after a failed Codex fork." + "version": "0.35.0", + "ko-kr": "• 사이드바에서 워크스페이스 이름을 바꾸고 색을 고를 수 있습니다. 고른 색은 에이전트 뷰에도 보이고, nexus workspace edit로도 바꿀 수 있습니다.\n• nexus file pull·push로 다른 컴퓨터의 워크스페이스와 파일·폴더를 주고받습니다. 그 컴퓨터의 SSH·WSL 워크스페이스로도 보낼 수 있습니다.\n• Codex 탭에서 Enter로 보낸 메시지는 실행 중인 도구가 끝난 뒤 들어가, 쓰고 있던 응답을 버리지 않습니다.\n• 긴 Claude 대화를 최근 부분부터 빠르게 엽니다. 백그라운드로 보낸 명령은 끝날 때까지 실행 중으로 보입니다.\n• 브라우저 탭이 빈 화면으로 뜨는 문제와 끝난 Codex 서브에이전트가 실행 중에 남는 문제를 고쳤고, 안정성을 전반적으로 높였습니다.\n• 최근 버전에서 더해진 기능: 다른 컴퓨터의 세션을 열고 이름을 바꾸고 닫거나 그 요청에 답할 수 있고, 워크트리를 워크스페이스별로 펼칩니다. Codex 채팅은 옆 패널(컨텍스트·사용량·목표), /config·/plan, 분기·되감기, Fast 모드를 지원합니다.", + "en-us": "• Rename a workspace and pick its color from the sidebar. The color also shows in the agent view, and nexus workspace edit can change it.\n• nexus file pull and push copy files and folders to and from another computer's workspace. Push also reaches that computer's SSH and WSL workspaces.\n• In Codex tabs, a message sent with Enter goes in after the running tool finishes, so the response in progress is kept.\n• Long Claude chats open from their latest part. A command sent to the background shows as running until it ends.\n• Fixed browser tabs that came up blank and finished Codex subagents left as running, along with many stability fixes.\n• Also from recent versions: open, rename and close sessions on another computer, answer its requests, and expand its worktrees by workspace. Codex chats have a side panel (context, usage, goals), /config, /plan, branching and rewind, and Fast mode." } diff --git a/cmd/agent/daemon.go b/cmd/agent/daemon.go index 01262bea2..05abb9096 100644 --- a/cmd/agent/daemon.go +++ b/cmd/agent/daemon.go @@ -104,6 +104,9 @@ const reattachGrace = 300 * time.Second // only exits this much later. const lockRetryWindow = 250 * time.Millisecond +// daemonListeningMessage announces that the daemon can accept its first dialer. +const daemonListeningMessage = "daemon listening" + // shutdownReplyDelay is how long the daemon waits after answering // agent.shutdown before it exits, so the reply reaches the dialer. const shutdownReplyDelay = 100 * time.Millisecond @@ -226,6 +229,8 @@ func runDaemon(root string) { // Step 6: bind the Unix socket. The run directory (0700) is already // ensured above; explicit socket chmod 0600 guards against any umask. + // The socket trusts callers running as this user: they can already read + // the user's files, so the daemon requires no separate connection token. ln, err := net.Listen("unix", paths.Sock) if err != nil { agentLogger.Error("daemon listen failed", "path", paths.Sock, "err", err) @@ -303,31 +308,35 @@ func runDaemon(root string) { // reconnect state must be discarded. epoch := agentrun.NewEpoch() - // cleanupAndExit tears down all shared resources and terminates. - // Called only on genuine daemon shutdown — not on dialer disconnect. - // Only the first caller tears down; any other blocks until it exits. + // Release the workspace before acknowledging shutdown. A new launcher + // may try this socket and lock as soon as the reply reaches the app. var shutdown sync.Once - cleanupAndExit := func(code int) { + releaseWorkspace := func() { shutdown.Do(func() { pty.Close() // hang up every terminal, as closing its tab does chat.Close() // and end every Claude tab's program lsp.Close() + fsys.DropUploads(nil) if hooksrv != nil { _ = hooksrv.Close() } _ = ln.Close() _ = os.Remove(paths.Sock) lock.Unlock() - _ = logFile.Close() - os.Exit(code) }) } + cleanupAndExit := func(code int) { + releaseWorkspace() + _ = logFile.Close() + os.Exit(code) + } // agent.shutdown comes from the app removing the workspace: no dialer // will come back, so the daemon exits now instead of holding its // terminals and language servers through reattachGrace. d.Register("agent.shutdown", func(_ context.Context, _ json.RawMessage) (any, error) { agentLogger.Info("daemon shutdown requested") + releaseWorkspace() time.AfterFunc(shutdownReplyDelay, func() { cleanupAndExit(0) }) return struct{}{}, nil }) @@ -385,6 +394,7 @@ func runDaemon(root string) { var preempted atomic.Int32 host := stdioserver.New(d, conn, conn, agentLogger) + host.RegisterShutdownHook(func() { fsys.DropUploads(host.Context()) }) // Ready goes first: the client takes the first frame it gets for the // handshake, and an event ahead of it reads as an agent without an @@ -404,21 +414,12 @@ func runDaemon(root string) { } // Wire event sinks to this host. On the next dialer, they are re-wired. - fsys.SetEventSink(func(event string, payload any) error { - emitErr := host.EmitEvent(event, payload) - if event == "fs.changed" { - if changed, ok := payload.(agentfs.FsChangedPayload); ok { - if routeErr := lsp.HandleFSChanged(changed); emitErr == nil { - emitErr = routeErr - } - } - } - return emitErr - }) + fsys.SetEventSink(fsEventSink(host, lsp)) git.SetEventSink(host.EmitEvent) lsp.SetEventSink(host.EmitEvent) - pty.SetEventSink(host.EmitEvent) - chat.SetEventSink(host.EmitEvent) + // Installing this sink also hands renderer credit over after Ready. + pty.SetEventSink(host.Context(), host.EmitEvent) + chat.SetConnectionSink(host.Context(), host.EmitEvent) search.SetEventSink(host.EmitEvent) if hooksrv != nil { hooksrv.SetEventSink(host.EmitEvent) @@ -474,15 +475,6 @@ func runDaemon(root string) { close(hostDone) }) - // PTY output debt the previous dialer never acked belongs to it; the - // new renderer will never ack it. Left in place, a session that went - // past the high watermark during a silent disconnect stays paused for - // good, and pty.replay blocks on the same window. Reset after Ready so - // the resumed output does not reach the dialer ahead of that frame, - // and before Run so the dialer's first replay already sees open - // windows. - pty.ResetFlowControl() - // 송신 4s < 판정 5s — 도착 지터 마진. 근거는 proto.HeartbeatSendMs 주석 참조. host.StartHeartbeat(proto.HeartbeatSendMs * time.Millisecond) // The idle watchdog fires after reattachGrace of no inbound traffic. @@ -550,6 +542,7 @@ func runDaemon(root string) { } // Main serve loop. + agentLogger.Info(daemonListeningMessage) conn := waitForDialer("initial-connect") for conn != nil { diff --git a/cmd/agent/daemon_connection_test.go b/cmd/agent/daemon_connection_test.go index 3338ec7de..0fb32fe54 100644 --- a/cmd/agent/daemon_connection_test.go +++ b/cmd/agent/daemon_connection_test.go @@ -6,9 +6,11 @@ import ( "bufio" "encoding/json" "errors" + "net" "os" "os/exec" "path/filepath" + "runtime" "strings" "syscall" "testing" @@ -16,6 +18,7 @@ import ( "github.com/nexus-code/nexus-code/internal/agentrun" "github.com/nexus-code/nexus-code/internal/proto" + "golang.org/x/sys/unix" ) // dialerProcess is an `agent --dial` run as the SSH session runs it, its @@ -100,6 +103,50 @@ func daemonLog(runDir, root string) string { return string(data) } +// waitForUnreadOutput observes pending data without consuming it, so the +// connection really closes with unread bytes. +func waitForUnreadOutput(t *testing.T, conn net.Conn) { + t.Helper() + raw, err := conn.(*net.UnixConn).SyscallConn() + if err != nil { + t.Fatal(err) + } + _ = conn.SetReadDeadline(time.Now().Add(3 * time.Second)) + defer func() { _ = conn.SetReadDeadline(time.Time{}) }() + var n int + var readErr error + if err := raw.Read(func(fd uintptr) bool { + var firstByte [1]byte + n, _, readErr = unix.Recvfrom(int(fd), firstByte[:], unix.MSG_PEEK) + return readErr != unix.EAGAIN && readErr != unix.EWOULDBLOCK + }); err != nil { + t.Fatal(err) + } + if n != 1 || readErr != nil { + t.Fatalf("daemon had no unread output: n=%d err=%v", n, readErr) + } +} + +// waitForDialerLog waits for the daemon to record its disconnect decision +// before another connection can turn that decision into a takeover. +func waitForDialerLog(t *testing.T, runDir, root string, matches func(string) bool) { + t.Helper() + deadline := time.NewTimer(3 * time.Second) + defer deadline.Stop() + poll := time.NewTicker(20 * time.Millisecond) + defer poll.Stop() + for { + if log := daemonLog(runDir, root); matches(log) { + return + } + select { + case <-poll.C: + case <-deadline.C: + t.Fatalf("daemon did not record the expected disconnect:\n%s", daemonLog(runDir, root)) + } + } +} + // A connection that ends with a reset rather than a close is a dialer gone // mid-output: the SSH session ended under it while the daemon still wrote, and // Linux reports a socket closed with unread data as reset. That is a dialer @@ -110,14 +157,17 @@ func TestDaemonSurvivesAResetConnection(t *testing.T) { root := t.TempDir() cmd, runDir := startDaemon(t, bin, root, shortHome(t)) defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() - sockPath := waitForSocket(t, runDir) + sockPath := waitForSocket(t, runDir, root) first, readyA := readReady(t, sockPath) + defer first.Close() spawnOn(t, first, "tab", root, "while :; do echo tick; sleep 0.01; done") - // What the daemon sends meanwhile is left unread when the connection closes. - time.Sleep(300 * time.Millisecond) + waitForUnreadOutput(t, first) _ = first.Close() - time.Sleep(200 * time.Millisecond) + waitForDialerLog(t, runDir, root, func(log string) bool { + return strings.Contains(log, "dialer connection broke") || + (runtime.GOOS != "linux" && strings.Contains(log, "dialer disconnected cleanly")) + }) next, readyB := readReady(t, sockPath) defer next.Close() @@ -148,7 +198,7 @@ func TestDialerWithItsOutputClosedLeavesTheDaemonCleanly(t *testing.T) { root := t.TempDir() cmd, runDir := startDaemon(t, bin, root, shortHome(t)) defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() - sockPath := waitForSocket(t, runDir) + sockPath := waitForSocket(t, runDir, root) dialer := startDialer(t, bin, sockPath) if ready := dialer.nextLine(t); !strings.Contains(ready, `"ready"`) { @@ -164,19 +214,20 @@ func TestDialerWithItsOutputClosedLeavesTheDaemonCleanly(t *testing.T) { if _, err := dialer.stdin.Write(append(spawn, '\n')); err != nil { t.Fatal(err) } - _ = dialer.nextLine(t) + for !strings.Contains(dialer.nextLine(t), `"event":"pty.data"`) { + } _ = dialer.stdout.Close() - time.Sleep(300 * time.Millisecond) + // The ping reply still has to be drained after the output pipe closes. + if _, err := dialer.stdin.Write([]byte(`{"id":"p","method":"ping"}` + "\n")); err != nil { + t.Fatal(err) + } _ = dialer.stdin.Close() - _ = dialer.exitCode(t, 5*time.Second) - - deadline := time.Now().Add(3 * time.Second) - for !strings.Contains(daemonLog(runDir, root), "dialer disconnected cleanly") { - if time.Now().After(deadline) { - t.Fatalf("the daemon did not see the dialer leave cleanly:\n%s", daemonLog(runDir, root)) - } - time.Sleep(20 * time.Millisecond) + if code := dialer.exitCode(t, 5*time.Second); code != 0 { + t.Fatalf("dialer exit code %d, want 0", code) } + waitForDialerLog(t, runDir, root, func(log string) bool { + return strings.Contains(log, "dialer disconnected cleanly") + }) if next, _ := readReady(t, sockPath); next != nil { _, _ = next.Write([]byte(`{"id":"bye","method":"agent.shutdown"}` + "\n")) _ = next.Close() @@ -192,7 +243,7 @@ func TestDialerEndsWhenItsDaemonGoes(t *testing.T) { root := t.TempDir() cmd, runDir := startDaemon(t, bin, root, shortHome(t)) defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() - sockPath := waitForSocket(t, runDir) + sockPath := waitForSocket(t, runDir, root) dialer := startDialer(t, bin, sockPath) _ = dialer.nextLine(t) @@ -213,7 +264,7 @@ func TestDialerTakenOverIsToldSo(t *testing.T) { root := t.TempDir() cmd, runDir := startDaemon(t, bin, root, shortHome(t)) defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() - sockPath := waitForSocket(t, runDir) + sockPath := waitForSocket(t, runDir, root) dialer := startDialer(t, bin, sockPath) _ = dialer.nextLine(t) @@ -244,7 +295,7 @@ func TestDialerEndsCleanlyWhenTheAppCloses(t *testing.T) { root := t.TempDir() cmd, runDir := startDaemon(t, bin, root, shortHome(t)) defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() - sockPath := waitForSocket(t, runDir) + sockPath := waitForSocket(t, runDir, root) dialer := startDialer(t, bin, sockPath) _ = dialer.nextLine(t) @@ -268,11 +319,14 @@ func TestDaemonGreetsEachDialerWithReadyFirst(t *testing.T) { root := t.TempDir() cmd, runDir := startDaemon(t, bin, root, shortHome(t)) defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() - sockPath := waitForSocket(t, runDir) + sockPath := waitForSocket(t, runDir, root) conn, _ := readReady(t, sockPath) spawnOn(t, conn, "tab", root, "yes") - time.Sleep(100 * time.Millisecond) + frames := newPtyFrames(conn) + if output, _ := frames.until(t, conn, 3*time.Second, func(text string, _ bool) bool { return text != "" }); output == "" { + t.Fatal("terminal never started writing") + } for i := 0; i < 60; i++ { next, first := readReady(t, sockPath) _ = conn.Close() diff --git a/cmd/agent/daemon_signals_test.go b/cmd/agent/daemon_signals_test.go index 43a4a6660..c935ab512 100644 --- a/cmd/agent/daemon_signals_test.go +++ b/cmd/agent/daemon_signals_test.go @@ -89,7 +89,7 @@ func startDaemonForSignals(t *testing.T) (net.Conn, string) { bin := agentBinary(t) root := t.TempDir() runDir := launchDaemonLikeBootstrap(t, bin, root, shortHome(t)) - conn, _ := readReady(t, waitForSocket(t, runDir)) + conn, _ := readReady(t, waitForSocket(t, runDir, root)) t.Cleanup(func() { _, _ = conn.Write([]byte(`{"id":"bye","method":"agent.shutdown"}` + "\n")) _ = conn.Close() @@ -175,7 +175,7 @@ func TestDaemonSurvivesHangupAndInterrupt(t *testing.T) { root := t.TempDir() cmd, runDir := startDaemon(t, bin, root, shortHome(t)) defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() - sockPath := waitForSocket(t, runDir) + sockPath := waitForSocket(t, runDir, root) first, _ := readReady(t, sockPath) defer first.Close() diff --git a/cmd/agent/daemon_test.go b/cmd/agent/daemon_test.go index e500673a2..4f896f8e5 100644 --- a/cmd/agent/daemon_test.go +++ b/cmd/agent/daemon_test.go @@ -7,6 +7,7 @@ import ( "encoding/base64" "encoding/json" "errors" + "fmt" "io" "net" "os" @@ -17,8 +18,10 @@ import ( "testing" "time" + "github.com/fsnotify/fsnotify" "github.com/nexus-code/nexus-code/internal/agentrun" "github.com/nexus-code/nexus-code/internal/proto" + "github.com/nexus-code/nexus-code/internal/pty" ) // shortHome returns a temp dir with a SHORT absolute path for use as the @@ -40,7 +43,7 @@ func shortHome(t *testing.T) string { // startDaemon starts `bin --daemon root` with HOME redirected to fakeHome so // the daemon writes its socket/lock/log under fakeHome/.nexus-code/run/. -// Returns the started *exec.Cmd and the run directory to poll for the socket. +// Returns the started command and the daemon's run directory. func startDaemon(t *testing.T, bin, root, fakeHome string) (*exec.Cmd, string) { t.Helper() nexusRunDir := filepath.Join(fakeHome, ".nexus-code", "run") @@ -55,31 +58,53 @@ func startDaemon(t *testing.T, bin, root, fakeHome string) (*exec.Cmd, string) { return cmd, nexusRunDir } -// waitForSocket polls runDir until a *.sock file appears, then returns its path. -func waitForSocket(t *testing.T, runDir string) string { +// waitForSocket waits for the daemon to announce that it is listening. A +// bound Unix socket can exist on disk before it accepts connections. +func waitForSocket(t *testing.T, runDir, root string) string { t.Helper() - deadline := time.Now().Add(5 * time.Second) - for time.Now().Before(deadline) { - entries, _ := os.ReadDir(runDir) - for _, e := range entries { - if strings.HasSuffix(e.Name(), ".sock") { - return filepath.Join(runDir, e.Name()) - } + id := agentrun.WsID(root, proto.ServerVersion) + logPath := filepath.Join(runDir, id+".log") + // Create the log if the daemon has not reached it yet. Keep existing + // contents so the announcement is visible even if it has already run. + // Watch the regular file directly: on kqueue watching the directory + // also tries to watch its socket entries, which is unsupported. + logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, 0600) + if err != nil { + t.Fatalf("open daemon log %s: %v", logPath, err) + } + if err := logFile.Close(); err != nil { + t.Fatalf("close daemon log %s: %v", logPath, err) + } + watcher, err := fsnotify.NewWatcher() + if err != nil { + t.Fatalf("watch daemon log: %v", err) + } + defer watcher.Close() + if err := watcher.Add(logPath); err != nil { + t.Fatalf("watch daemon log %s: %v", logPath, err) + } + + deadline := time.NewTimer(5 * time.Second) + defer deadline.Stop() + for { + // Read after installing the watch so a write before registration + // is seen along with writes delivered as events. + data, err := os.ReadFile(logPath) + if err != nil { + t.Fatalf("read daemon log %s: %v", logPath, err) } - time.Sleep(30 * time.Millisecond) - } - // Boot failed — dump the run directory and any boot log so the failure - // is diagnosable from CI output alone. - entries, _ := os.ReadDir(runDir) - for _, e := range entries { - t.Logf("runDir entry: %s", e.Name()) - if strings.HasSuffix(e.Name(), ".log") { - data, _ := os.ReadFile(filepath.Join(runDir, e.Name())) - t.Logf("boot log %s:\n%s", e.Name(), data) + if strings.Contains(string(data), `"msg":"`+daemonListeningMessage+`"`) { + return filepath.Join(runDir, id+".sock") + } + + select { + case <-watcher.Events: + case err := <-watcher.Errors: + t.Fatalf("daemon log watcher: %v", err) + case <-deadline.C: + t.Fatalf("daemon did not announce its listener within 5 s:\n%s", data) } } - t.Fatal("daemon socket did not appear within 5 s") - return "" } // readReady connects to sockPath and reads the first NDJSON frame (Ready). @@ -118,7 +143,7 @@ func TestDaemonTakeover(t *testing.T) { cmd, runDir := startDaemon(t, bin, root, fakeHome) defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() - sockPath := waitForSocket(t, runDir) + sockPath := waitForSocket(t, runDir, root) // Dialer A. connA, lineA := readReady(t, sockPath) @@ -178,7 +203,7 @@ func TestDaemonLongSessionThenReattach(t *testing.T) { cmd, runDir := startDaemon(t, bin, root, fakeHome) defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() - sockPath := waitForSocket(t, runDir) + sockPath := waitForSocket(t, runDir, root) // Dialer A: connect, get Ready. connA, lineA := readReady(t, sockPath) @@ -218,7 +243,7 @@ func TestDaemonLongSessionThenReattach(t *testing.T) { // (a renderer that vanished while the socket still absorbed writes), so the // session pauses at the high watermark with ~100 KB of debt. After dialer B // takes over, that stale debt must be dropped and the rest of the output must -// reach B; before ResetFlowControl was wired in, the session stayed paused +// reach B; before takeover reset renderer credit, the session stayed paused // forever. func TestDaemonTakeoverResumesPausedPTY(t *testing.T) { t.Parallel() @@ -228,7 +253,7 @@ func TestDaemonTakeoverResumesPausedPTY(t *testing.T) { cmd, runDir := startDaemon(t, bin, root, fakeHome) defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() - sockPath := waitForSocket(t, runDir) + sockPath := waitForSocket(t, runDir, root) connA, _ := readReady(t, sockPath) defer connA.Close() @@ -284,6 +309,109 @@ func TestDaemonTakeoverResumesPausedPTY(t *testing.T) { t.Fatalf("dialer B never received the output left after the pause (got %d bytes): %v", tail.Len(), sc.Err()) } +// TestDaemonTakeoverCancelsCreditBlockedReplay withholds A's replay ACKs +// while B takes over the daemon's Unix socket. B receives its own full replay. +func TestDaemonTakeoverCancelsCreditBlockedReplay(t *testing.T) { + t.Parallel() + root := t.TempDir() + cmd, runDir := startDaemon(t, agentBinary(t), root, shortHome(t)) + defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() + sockPath := waitForSocket(t, runDir, root) + + type frame struct { + proto.Response + Type string `json:"type"` + Event string `json:"event"` + Payload pty.DataPayload `json:"payload"` + } + connect := func() (net.Conn, func() frame) { + conn, err := net.DialTimeout("unix", sockPath, 2*time.Second) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = conn.Close() }) + _ = conn.SetDeadline(time.Now().Add(5 * time.Second)) + decoder := json.NewDecoder(conn) + next := func() frame { + var f frame + if err := decoder.Decode(&f); err != nil { + t.Fatalf("read dialer frame: %v", err) + } + if f.Error != nil { + t.Fatalf("request %s failed: %+v", f.ID, f.Error) + } + return f + } + if f := next(); f.Type != "ready" { + t.Fatalf("first frame type = %q, want ready", f.Type) + } + return conn, next + } + send := func(conn net.Conn, id, method, params string) { + if err := json.NewEncoder(conn).Encode(proto.Request{ID: id, Method: method, Params: json.RawMessage(params)}); err != nil { + t.Fatalf("send %s: %v", method, err) + } + } + ack := func(conn net.Conn, id string, count int) { + send(conn, id, "pty.ack", fmt.Sprintf(`{"workspaceId":"ws","tabId":"tab","bytesConsumed":%d}`, count)) + } + chunk := func(f frame) []byte { + data, err := base64.StdEncoding.DecodeString(f.Payload.Chunk) + if err != nil { + t.Fatal(err) + } + return data + } + + a, nextA := connect() + spawnOn(t, a, "tab", root, `stty -echo; head -c 120000 /dev/zero | tr '\0' a; printf '\nHISTORY-END\n'; read -r stop`) + var history strings.Builder + credited := 0 + for replies := 0; replies < 2; { + f := nextA() + if f.ID == "first-ack" || f.ID == "last-ack" { + replies++ + } + if f.Event != pty.EventData { + continue + } + history.Write(chunk(f)) + if credited == 0 && history.Len() >= pty.HighWatermarkBytes { + credited = history.Len() + ack(a, "first-ack", credited) + } + if strings.HasSuffix(history.String(), "HISTORY-END\r\n") { + ack(a, "last-ack", history.Len()-credited) + } + } + + const tab = `{"workspaceId":"ws","tabId":"tab"}` + send(a, "old-replay", "pty.replay", tab) + for received := 0; received < pty.HighWatermarkBytes; { + f := nextA() + if f.ID == "old-replay" { + t.Fatal("old replay finished without credit") + } + if f.Event == pty.EventData && f.Payload.Replay { + received += len(chunk(f)) + } + } + + b, nextB := connect() + send(b, "new-replay", "pty.replay", tab) + var replay strings.Builder + for f := nextB(); f.ID != "new-replay"; f = nextB() { + if f.Event == pty.EventData && f.Payload.Replay { + data := chunk(f) + replay.Write(data) + ack(b, "ack", len(data)) + } + } + if want := "\x1bc" + history.String(); replay.String() != want { + t.Fatalf("replacement replay received %d bytes, want exactly %d bytes starting with terminal reset", replay.Len(), len(want)) + } +} + // A daemon of another workspace sweeping stale run files takes this // workspace's lock for a moment to prove its daemon dead. A daemon starting in // that moment must wait it out instead of exiting with ExitCodeLockHeld as if @@ -335,7 +463,7 @@ func TestDaemonStartsWhileLockBrieflyHeld(t *testing.T) { t.Fatalf("daemon exited while the lock was briefly held: %v", waitErr) case <-time.After(300 * time.Millisecond): } - conn, ready := readReady(t, waitForSocket(t, runDir)) + conn, ready := readReady(t, waitForSocket(t, runDir, root)) defer conn.Close() if !strings.Contains(ready, `"ready"`) { t.Fatalf("want ready frame, got %s", ready) @@ -355,7 +483,7 @@ func TestDaemonExitsOnShutdown(t *testing.T) { exited := make(chan error, 1) go func() { exited <- cmd.Wait() }() defer func() { _ = cmd.Process.Kill() }() - sockPath := waitForSocket(t, runDir) + sockPath := waitForSocket(t, runDir, root) conn, _ := readReady(t, sockPath) defer conn.Close() @@ -380,6 +508,11 @@ func TestDaemonExitsOnShutdown(t *testing.T) { } } + // The client can re-add the workspace as soon as it receives the reply. + if _, err := os.Stat(sockPath); !os.IsNotExist(err) { + t.Errorf("old daemon socket is still present after shutdown reply: %v", err) + } + select { case err := <-exited: if err != nil { @@ -438,17 +571,8 @@ func TestDaemonStopsAnotherDaemon(t *testing.T) { go func() { exitedB <- cmdB.Wait() }() defer func() { _ = cmdB.Process.Kill() }() - sockA := filepath.Join(runDir, agentrun.WsID(rootA, proto.ServerVersion)+".sock") - sockB := filepath.Join(runDir, agentrun.WsID(rootB, proto.ServerVersion)+".sock") - for _, sock := range []string{sockA, sockB} { - deadline := time.Now().Add(5 * time.Second) - for _, err := os.Stat(sock); err != nil; _, err = os.Stat(sock) { - if time.Now().After(deadline) { - t.Fatalf("daemon socket %s did not appear: %v", sock, err) - } - time.Sleep(30 * time.Millisecond) - } - } + sockA := waitForSocket(t, runDir, rootA) + waitForSocket(t, runDir, rootB) conn, _ := readReady(t, sockA) defer conn.Close() diff --git a/cmd/agent/fs_events.go b/cmd/agent/fs_events.go new file mode 100644 index 000000000..e0fe240f2 --- /dev/null +++ b/cmd/agent/fs_events.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/nexus-code/nexus-code/internal/filewatch" + agentfs "github.com/nexus-code/nexus-code/internal/fs" + agentlsp "github.com/nexus-code/nexus-code/internal/lsp" + "github.com/nexus-code/nexus-code/internal/stdioserver" +) + +// fsEventSink forwards filesystem events to the client and also tells any +// language servers that registered for file changes about the same batch. +func fsEventSink(host *stdioserver.Host, lsp *agentlsp.Service) agentfs.EventSink { + return func(event string, payload any) error { + err := host.EmitEvent(event, payload) + if event == "fs.changed" { + if changed, ok := payload.(filewatch.FsChangedPayload); ok { + if routeErr := lsp.HandleFSChanged(changed); err == nil { + err = routeErr + } + } + } + return err + } +} diff --git a/cmd/agent/local_shutdown_test.go b/cmd/agent/local_shutdown_test.go new file mode 100644 index 000000000..d14d9c984 --- /dev/null +++ b/cmd/agent/local_shutdown_test.go @@ -0,0 +1,149 @@ +//go:build !windows + +package main + +import ( + "context" + "encoding/json" + "errors" + "io" + "os" + "os/exec" + "syscall" + "testing" + "time" + + "github.com/nexus-code/nexus-code/internal/lsp" + "github.com/nexus-code/nexus-code/internal/proto" +) + +// TestLocalEOFKillsLanguageServer verifies that the local agent reaps an LSP +// child that stays alive after stdin closes before exiting itself. +func TestLocalEOFKillsLanguageServer(t *testing.T) { + root := t.TempDir() + bin := agentBinary(t) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + cmd := exec.CommandContext(ctx, bin, root) + cmd.Env = append(os.Environ(), "HOME="+shortHome(t)) + stdin, err := cmd.StdinPipe() + if err != nil { + t.Fatal(err) + } + stdout, err := cmd.StdoutPipe() + if err != nil { + t.Fatal(err) + } + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }() + + frames := json.NewDecoder(stdout) + var ready proto.ReadyFrame + if err := frames.Decode(&ready); err != nil || ready.Type != "ready" { + t.Fatalf("ready frame = %+v, %v", ready, err) + } + params, err := json.Marshal(lsp.SpawnParams{ + WorkspaceID: "test", LanguageID: "fake", BinaryPath: os.Args[0], WorkspaceRoot: root, + Args: []string{"-test.run=^TestLocalShutdownLanguageServerHelper$"}, + Env: map[string]string{"NEXUS_TEST_LOCAL_SHUTDOWN_LSP": "1"}, + }) + if err != nil { + t.Fatal(err) + } + if err := json.NewEncoder(stdin).Encode(proto.Request{ID: "spawn", Method: "lsp.spawn", Params: params}); err != nil { + t.Fatal(err) + } + pid := 0 + for pid == 0 { + var frame struct { + ID string `json:"id"` + Error *proto.ErrorFrame `json:"error"` + Result struct { + Capabilities struct { + PID int `json:"testPid"` + } `json:"capabilities"` + } `json:"result"` + } + if err := frames.Decode(&frame); err != nil { + t.Fatal(err) + } + if frame.ID == "spawn" { + pid = frame.Result.Capabilities.PID + if frame.Error != nil || pid == 0 { + t.Fatalf("spawn result = %+v", frame) + } + } + } + childReaped := false + defer func() { + if !childReaped { + _ = syscall.Kill(pid, syscall.SIGKILL) + } + }() + if err := stdin.Close(); err != nil { + t.Fatal(err) + } + done := make(chan error, 1) + go func() { done <- cmd.Wait() }() + select { + case err := <-done: + if err != nil { + t.Fatalf("agent exit: %v", err) + } + case <-time.After(5 * time.Second): + _ = cmd.Process.Kill() + <-done + t.Fatal("agent failed to exit") + } + if err := syscall.Kill(pid, 0); !errors.Is(err, syscall.ESRCH) { + t.Fatalf("LSP child %d survived the agent: %v", pid, err) + } + childReaped = true +} + +// TestLocalShutdownLanguageServerHelper runs a child that initializes normally +// and deliberately survives closed stdin. +func TestLocalShutdownLanguageServerHelper(t *testing.T) { + if os.Getenv("NEXUS_TEST_LOCAL_SHUTDOWN_LSP") != "1" { + return + } + decoder := lsp.NewDecoder() + buf := make([]byte, 4*1024) + for { + n, err := os.Stdin.Read(buf) + if errors.Is(err, io.EOF) { + <-time.After(time.Minute) + os.Exit(0) + } + if err != nil { + t.Fatal(err) + } + messages, err := decoder.Append(buf[:n]) + if err != nil { + t.Fatal(err) + } + for _, message := range messages { + var req struct { + ID string `json:"id"` + Method string `json:"method"` + } + if err := json.Unmarshal(message, &req); err != nil { + t.Fatal(err) + } + if req.Method == "initialize" { + response, err := lsp.EncodeMessage(map[string]any{ + "jsonrpc": "2.0", "id": req.ID, + "result": map[string]any{"capabilities": map[string]int{"testPid": os.Getpid()}}, + }) + if err != nil { + t.Fatal(err) + } + if _, err := os.Stdout.Write(response); err != nil { + t.Fatal(err) + } + } + } + } +} diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 6a2c800ee..0b93c4799 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -14,6 +14,7 @@ import ( "encoding/json" "log/slog" "os" + "sync" "time" agentchat "github.com/nexus-code/nexus-code/internal/chat" @@ -36,6 +37,9 @@ func main() { // What the agent was started with stays with it: on Linux it brings // along Chromium's descriptors, which would reach every terminal. inheritfd.CloseOnExec() + if code, handled := hookclient.PrintChildHelper(os.Args[1:]); handled { + os.Exit(code) + } // hook 서브커맨드 분기 — PTY/FS/Git/LSP 서비스 init 전에 처리해 빠른 시작을 보장한다. // Claude Code는 hook 이벤트 발생 시 `agent hook ` 형태로 호출한다. @@ -161,39 +165,30 @@ func main() { }) host := stdioserver.New(d, os.Stdin, os.Stdout, agentLogger) - fsys.SetEventSink(func(event string, payload any) error { - err := host.EmitEvent(event, payload) - if event == "fs.changed" { - if changed, ok := payload.(agentfs.FsChangedPayload); ok { - if routeErr := lsp.HandleFSChanged(changed); err == nil { - err = routeErr - } - } - } - return err - }) + fsys.SetEventSink(fsEventSink(host, lsp)) git.SetEventSink(host.EmitEvent) lsp.SetEventSink(host.EmitEvent) - pty.SetEventSink(host.EmitEvent) - chat.SetEventSink(host.EmitEvent) + pty.SetEventSink(host.Context(), host.EmitEvent) + chat.SetConnectionSink(host.Context(), host.EmitEvent) search.SetEventSink(host.EmitEvent) if hooksrv != nil { // hookserver의 EventSink를 host가 생성된 뒤 연결한다. // 다른 서비스(fs/git/pty)의 SetEventSink와 같은 패턴. hooksrv.SetEventSink(host.EmitEvent) } - defer fsys.Close() - defer git.Close() - defer lsp.Close() - // PTY cleanup runs as a shutdown hook, not a defer: every termination path - // (EOF, SIGTERM, idle watchdog) ends in drainAndExit → os.Exit, which skips - // defers. Pdeathsig (Linux only) and the master-fd-close SIGHUP are not - // enough on their own — a non-Linux remote has no Pdeathsig, and a shell - // that ignores SIGHUP would orphan. The hook hangs up each shell and kills - // what is left of its session if it does not end (pty.Service.Close). - host.RegisterShutdownHook(pty.Close) - // The Claude tabs' programs end with the agent the same way. - host.RegisterShutdownHook(chat.Close) + // Every stdio exit path ends at os.Exit. Start independent cleanup in + // parallel so a slow watcher or shell cannot keep other children alive. + host.RegisterShutdownHook(func() { + var cleanup sync.WaitGroup + for _, closeService := range []func(){pty.Close, chat.Close, lsp.Close, fsys.Close, git.Close} { + cleanup.Add(1) + go func() { + defer cleanup.Done() + closeService() + }() + } + cleanup.Wait() + }) if hooksrv != nil { // SIGTERM 경로는 os.Exit가 defer를 우회하므로 shutdown hook으로 등록한다. // hookserver는 /tmp/nexus-h-*.sock 파일을 생성하므로 정리하지 않으면 @@ -271,6 +266,9 @@ func newHookGetInfoHandler(hs hookInfoProvider) dispatch.Handler { if err := hs.WaitReady(waitCtx); err != nil { return nil, proto.NewError(proto.CodeUnavailable, "hookserver not ready: "+err.Error()) } + // The daemon's 0600 socket trusts this user, whose processes can + // already read the user's files. The master hook token can be returned + // over that connection without a separate connection token. return map[string]any{ "socketPath": hs.SocketPath(), "token": hs.Token(), diff --git a/cmd/agent/stop_daemon.go b/cmd/agent/stop_daemon.go index d25dbbd1d..352fbc960 100644 --- a/cmd/agent/stop_daemon.go +++ b/cmd/agent/stop_daemon.go @@ -13,14 +13,9 @@ package main import ( - "bufio" "context" "encoding/json" - "errors" - "fmt" - "net" "path/filepath" - "syscall" "time" "github.com/nexus-code/nexus-code/internal/agentrun" @@ -63,65 +58,10 @@ func newStopDaemonHandler(ownRoot string) dispatch.Handler { } ctx, cancel := context.WithTimeout(ctx, stopDaemonTimeout) defer cancel() - stopped, err := stopDaemonAt(ctx, paths.Sock) + stopped, err := agentrun.StopDaemon(ctx, paths.Sock) if err != nil { return nil, err } return stopDaemonResult{Stopped: stopped}, nil } } - -// stopDaemonAt asks the daemon listening on sock to exit and waits for its -// reply. It returns false, without error, when nothing listens there: no -// socket, or one left by a daemon that is gone. -func stopDaemonAt(ctx context.Context, sock string) (bool, error) { - var dialer net.Dialer - conn, err := dialer.DialContext(ctx, "unix", sock) - if err != nil { - if errors.Is(err, syscall.ENOENT) || errors.Is(err, syscall.ECONNREFUSED) { - return false, nil - } - return false, fmt.Errorf("agent.stopDaemon: dial: %w", err) - } - defer conn.Close() - if deadline, ok := ctx.Deadline(); ok { - _ = conn.SetDeadline(deadline) - } - // Close the connection when ctx is canceled, so a blocked read returns. - stop := context.AfterFunc(ctx, func() { _ = conn.Close() }) - defer stop() - - sc := bufio.NewScanner(conn) - sc.Buffer(make([]byte, 64*1024), 16*1024*1024) - // The daemon greets every dialer with its Ready frame first. - if !sc.Scan() { - return false, fmt.Errorf("agent.stopDaemon: no ready frame: %w", scanErr(sc)) - } - const id = "stop-daemon" - if _, err := conn.Write([]byte(`{"id":"` + id + `","method":"agent.shutdown"}` + "\n")); err != nil { - return false, fmt.Errorf("agent.stopDaemon: write: %w", err) - } - // Events and heartbeats may come before the reply. - for sc.Scan() { - var frame struct { - ID string `json:"id"` - Error json.RawMessage `json:"error"` - } - if json.Unmarshal(sc.Bytes(), &frame) != nil || frame.ID != id { - continue - } - if len(frame.Error) > 0 && string(frame.Error) != "null" { - return false, fmt.Errorf("agent.stopDaemon: agent.shutdown failed: %s", frame.Error) - } - return true, nil - } - return false, fmt.Errorf("agent.stopDaemon: no reply: %w", scanErr(sc)) -} - -// scanErr is why a scan ended: its error, or EOF when it has none. -func scanErr(sc *bufio.Scanner) error { - if err := sc.Err(); err != nil { - return err - } - return errors.New("EOF") -} diff --git a/docs/CLI.md b/docs/CLI.md index 7d192d64a..31b0ea47d 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -146,6 +146,7 @@ nexus chat resume 5f2a # 지난 대화 이어 - `chat answer` 는 터미널에서 도는 Claude Code 의 요청에도 답합니다(앱의 요청 카드와 같음). `--stop` 은 거절하면서 턴도 멈춥니다. - `chat interrupt` 는 탭의 멈춤 버튼과 같습니다. 대기열의 메시지는 그대로 남습니다. - `chat panel` 은 탭 옆 패널의 내용입니다: 목표, 할 일, 작업(백그라운드 실행, `chat stop-task` 가 받는 id), 서브에이전트, 바뀐 파일, 컨텍스트 · 비용 · 플랜 사용량, MCP 서버, 원격 제어, 보류 메시지. 로그인한 계정은 보이지 않습니다. +- 작업 패널의 백그라운드 명령은 현재 실행에서 수집하며, 앱을 재시작하면 대화 기록에서 복원되지 않습니다. - `chat unsend <탭> ` 는 보류 메시지를 id 로 회수하고 그 내용을 출력합니다. 번호를 주면 턴 중에 보내 아직 읽지 않은 메시지(`chat read` 에 `(unread)` 로 표시)를 회수합니다(Claude 탭만). 이미 읽었으면 종료 코드 1 입니다. - `chat rewind <탭> <번호>` 는 되감기 버튼과 같습니다. 그 메시지 전으로 대화를, `--files` 면 그 뒤 Claude 가 바꾼 파일도 되돌립니다. `--preview` 는 되돌릴 파일만 보여줍니다. 턴 중에는 안 되고, 사용자에게 매번 묻습니다(Claude 탭만). - `chat fork <탭> [번호]` 는 대화를 새 탭(뒤쪽)으로 복사합니다. 번호가 없으면 전부, 답변 번호면 그 답변까지, 메시지 번호면 그 메시지 전까지입니다. 메시지에서 갈라내면 그 메시지는 보내지 않고 출력합니다(Claude 탭만). @@ -193,6 +194,26 @@ nexus chat send 1a2b --device "Mac mini" --wait - # 그 컴퓨터의 탭에 - `workspace list --device` 는 그 컴퓨터의 워크스페이스 목록입니다. `--workspace` 에 쓸 이름을 여기서 봅니다. - 서브에이전트(`agent`), 브라우저, 화면 배치 · 에이전트 뷰, `worktree new`, 포트, `problems`, 워크스페이스 추가 · 이름 · 고정 · 순서는 이 컴퓨터에서만 됩니다. 다른 컴퓨터에 워크스페이스를 추가하는 일은 사용자만 합니다(원격 프로토콜이 에이전트의 요청을 받지 않습니다). +### 다른 컴퓨터와 파일 복사하기 + +```bash +nexus file pull --device "Mac mini" --workspace site dist/report.pdf +nexus file pull --device "Windows PC" --workspace site -r 'C:/work/site/dist' ./snapshot --json +``` + +`file pull` 과 `file push` 에는 기기와 원격 워크스페이스를 모두 지정합니다. 원격 상대 경로는 지정한 워크스페이스를 기준으로 해석합니다. 앱이 실행 중인 컴퓨터의 로컬 워크스페이스에서 실행하며, 로컬 상대 경로는 CLI 의 작업 디렉터리를 기준으로 해석합니다. + +```bash +nexus file push --device "Mac mini" --workspace site ./report.pdf ./notes.txt dist/ +nexus file push --device "Mac mini" --workspace site -r ./snapshot /Users/me/Downloads --json +``` + +- `pull` 에서 로컬 대상을 생략하거나 기존 폴더를 지정하면 원래 이름으로 저장합니다. `push` 에서 원격 대상이 기존 폴더이면 그 안에 원래 이름으로 넣습니다. 여러 소스를 보내려면 대상이 기존 폴더여야 합니다. 각 파일의 부모 폴더가 있어야 하며, `-r` 은 복사할 폴더 트리를 만듭니다. +- 워크스페이스 안 읽기에는 파일 공유 권한이 필요합니다. 워크스페이스 안 쓰기에는 채팅 제어와 에이전트 요청을 허용해야 합니다. 워크스페이스 밖 절대 경로를 읽거나 쓰려면 대상 컴퓨터에서 터미널 제어와 에이전트 요청을 허용해야 합니다. 에이전트 설정이 ‘묻기’이면 쓰기와 워크스페이스 밖 읽기를 승인해야 합니다. 한 번 승인하면 같은 복사의 파일 조각과 승인된 폴더의 하위 경로에 적용됩니다. `push` 와 워크스페이스 밖 전송에는 대상 앱의 원격 프로토콜 0.14.0 이상이 필요합니다. SSH·WSL 워크스페이스로 보내려면 대상 파일 에이전트가 업로드를 지원해야 합니다. 워크스페이스 밖 절대 경로는 대상 앱의 로컬 워크스페이스에서 사용할 수 있습니다. +- 각 파일은 전송을 마친 뒤 대상에 배치되며 기존 파일을 덮어씁니다. `push` 는 파일 하나당 최대 20 GiB 를 보낼 수 있으며 기존 대상 파일의 권한 비트를 유지합니다. `-r` 은 심볼릭 링크를 건너뜁니다. `pull` 은 원격 폴더 목록이 잘렸으면 복사를 시작하기 전에 실패합니다. +- Windows에서 `push`로 파일을 덮어쓰면 기존 파일의 ACL과 읽기 전용·숨김 속성은 유지되지 않습니다. +- 출력에는 저장 경로, 바이트 수, SHA-256 이 들어갑니다. `--json` 은 기기와 두 컴퓨터의 경로도 출력합니다. `-r` 또는 여러 소스를 지정한 `push` 의 JSON 결과는 배열이며 건너뛴 링크에는 `"skipped": "symlink"` 가 붙습니다. + ## 세션의 에이전트 따라가기 Claude Code 세션 안의 서브에이전트와, 그 세션이 Bash 로 실행한 `claude -p` · `codex exec` 를 봅니다. 세션이 `claude -p` 나 `codex exec` 를 서브에이전트처럼 쓸 때 유용합니다. Codex 탭이 명령으로 실행한 `claude -p` · `codex exec` 도 그 탭의 실행으로 보입니다. @@ -219,11 +240,12 @@ codex exec resume <세션 id> "이어서 문서도" # codex 도 같 - 다른 터미널의 세션은 `--tab`(다른 워크스페이스면 `--workspace` 도)으로 고릅니다. - 앱을 업데이트하기 전부터 돌던 Claude · Codex 탭의 프로그램은 예전 환경 그대로라, 그 탭이 띄운 실행은 탭의 프로그램이 다시 시작된 뒤부터 보입니다. -## 워크스페이스 추가 · 제거 · 정리 +## 워크스페이스 추가 · 제거 · 이름 · 색 · 정리 ```bash nexus workspace add ../lib --name 라이브러리 --show # 이 터미널 위치 기준 폴더를 사이드바 끝에 추가 nexus workspace remove 라이브러리 # 앱 목록에서 제거(폴더는 그대로) +nexus workspace edit lib --name 라이브러리 --color blue # 이름 바꾸기 · 색 지정 (--color auto 는 앱이 고름) nexus workspace pin lib # 사이드바 위쪽에 고정 (workspace unpin 으로 해제) nexus workspace move lib --before api # api 바로 위로 (--after · --top · --bottom) ``` @@ -232,17 +254,22 @@ nexus workspace move lib --before api # api 바로 위로 (--after - 폴더 이름이 사이드바 이름이 됩니다. `--name` 으로 바꿀 수 있습니다. - SSH · WSL 터미널에서 실행하면 그 터미널과 같은 호스트(같은 접속 방법)의 폴더를 추가합니다. - 처음 보여줄 때 연결합니다. `--show` 면 바로 그 워크스페이스로 전환합니다. + - `--color` 로 색을 정해 추가할 수 있습니다. - 이미 목록에 있는 폴더면 새로 만들지 않고 그 워크스페이스를 알려줍니다. 묻지 않습니다. - `workspace remove` 는 사이드바 메뉴의 **목록에서 제거** 와 같습니다. - 그 워크스페이스와 작업들의 터미널을 닫고(실행 중인 프로그램 포함) 사이드바에서 뺍니다. 폴더는 지우지 않습니다. 작업의 워크트리 폴더와 브랜치도 디스크에 남습니다. - 저장하지 않은 파일이 있거나, 명령을 실행한 터미널이 그 워크스페이스(또는 그 작업) 안에 있으면 거절합니다. - 작업 하나만 지우려면 `worktree remove` 를 씁니다. - 항상 다른 워크스페이스라서 아래 "다른 워크스페이스 바꾸기" 설정을 따릅니다. +- `workspace edit` 는 사이드바 메뉴의 **이름 바꾸기** · **색** 과 같습니다. `--name` · `--color` 중 하나 이상을 줍니다. + - 색은 `red` · `amber` · `lime` · `green` · `cyan` · `blue` · `violet` · `pink` 이거나 `auto` 입니다. 사이드바 행의 폴더 아이콘과 에이전트 뷰에서 그 워크스페이스를 가르는 색이 되고, `auto` 는 앱이 워크스페이스마다 골라 줍니다. 새로 추가한 워크스페이스는 `auto` 입니다. + - 워크스페이스의 작업(워크트리)은 그 워크스페이스의 색을 따르므로 따로 바꾸지 않습니다. + - 지금 값과 같으면 아무것도 바꾸지 않고 묻지도 않습니다. `workspace list --json` 에 `color` 가 나옵니다. - `workspace pin` 은 사이드바의 고정 버튼과 같습니다. 고정한 것들의 맨 아래로 갑니다. - `workspace move` 는 사이드바에서 끌어 옮기는 것과 같습니다. `--before` · `--after` 로 다른 묶음(고정 · 고정 안 함)의 워크스페이스 옆에 두면 그 묶음으로 고정되거나 풀립니다. `--top` · `--bottom` 은 지금 묶음 안에서 맨 위 · 맨 아래로 옮깁니다. -- 작업(워크트리)은 사이드바에서 워크스페이스 아래에 보여서 이름 · 고정 · 순서를 바꾸지 않습니다. +- 작업(워크트리)은 사이드바에서 워크스페이스 아래에 보여서 이름 · 색 · 고정 · 순서를 바꾸지 않습니다. - `workspace list` 는 사이드바 순서(고정한 것 먼저)이고, 고정한 것에 `pinned` 가 붙습니다. -- 다른 워크스페이스의 이름 · 고정 · 순서 바꾸기는 "다른 워크스페이스 바꾸기" 설정을 따릅니다. +- 다른 워크스페이스의 이름 · 색 · 고정 · 순서 바꾸기는 "다른 워크스페이스 바꾸기" 설정을 따릅니다. ## 작업(워크트리) 만들기 · 지우기 diff --git a/docs/RELEASING.md b/docs/RELEASING.md index 05f5d9606..61b50f5b0 100644 --- a/docs/RELEASING.md +++ b/docs/RELEASING.md @@ -12,7 +12,7 @@ | `develop` | 통합 브랜치. 모든 기능/수정이 여기 쌓인다. | | `main` | 릴리스 브랜치. 태그와 GitHub Release는 여기서만 만든다. | -- **CI (`ci.yml`)** 는 `main` · `develop` 으로의 **PR** 과 `main` **push** 에서 돈다 (typecheck · lint · test, electron-builder 미실행). macOS · Ubuntu · Windows 러너에서 각각 돌며, 세 OS 모두 통과해야 한다. develop push 에는 게이트가 없으므로, 품질 게이트는 PR 에서 통과시킨다. +- **CI (`ci.yml`)** 는 `main` · `develop` 으로의 **PR** 과 `main` **push** 에서 돈다 (typecheck · lint · test, electron-builder 미실행). `main` 으로의 PR · push 와 수동 실행은 macOS · Ubuntu · Windows 러너에서 각각 돌며, 세 OS 모두 통과해야 한다. `develop` 으로의 PR 은 Ubuntu · Windows 만 돈다 — macOS 러너는 대기가 길고, macOS 는 머지 전에 Mac 에서 로컬로 테스트한다. develop push 에는 게이트가 없으므로, 품질 게이트는 PR 에서 통과시킨다. - **Release (`release.yml`)** 는 **GitHub Release 가 created** 될 때 트리거된다. ### 서명 인증서 diff --git a/internal/agentpaths/profile.go b/internal/agentpaths/profile.go new file mode 100644 index 000000000..4c23491a7 --- /dev/null +++ b/internal/agentpaths/profile.go @@ -0,0 +1,51 @@ +package agentpaths + +import ( + "os" + "path/filepath" + "runtime" + "strings" +) + +// ExpandProfileDir resolves "~" and "~/…" against home and cleans the path. +func ExpandProfileDir(dir, home string) string { + dir = strings.TrimSpace(dir) + switch { + case dir == "": + return "" + case dir == "~": + dir = home + case strings.HasPrefix(dir, "~/") || strings.HasPrefix(dir, `~\`): + if home == "" { + return "" + } + rest := dir[2:] + if dir[1] == '\\' && runtime.GOOS != "windows" { + // A folder picked on Windows ("~\.claude\work"), on a host + // whose separator is "/". + rest = strings.ReplaceAll(rest, `\`, "/") + } + dir = filepath.Join(home, rest) + } + return filepath.Clean(dir) +} + +// ProfileEnvDir expands an explicit provider config directory on the host +// that will run the CLI. Other environment variables keep their exact values. +func ProfileEnvDir(key, value string) string { + if runtime.GOOS == "windows" { + key = strings.ToUpper(key) + } + if key != "CLAUDE_CONFIG_DIR" && key != "CODEX_HOME" { + return value + } + home, _ := os.UserHomeDir() + dir := ExpandProfileDir(value, home) + if dir == "" { + return "" + } + if absolute, err := filepath.Abs(dir); err == nil { + return absolute + } + return dir +} diff --git a/internal/agentrun/stop_unix.go b/internal/agentrun/stop_unix.go new file mode 100644 index 000000000..342814b07 --- /dev/null +++ b/internal/agentrun/stop_unix.go @@ -0,0 +1,68 @@ +//go:build !windows + +package agentrun + +import ( + "bufio" + "context" + "encoding/json" + "errors" + "fmt" + "net" + "syscall" +) + +// StopDaemon asks the daemon listening on sock to exit and waits for its +// reply. It returns false, without error, when nothing listens there: no +// socket, or one left by a daemon that is gone. +func StopDaemon(ctx context.Context, sock string) (bool, error) { + var dialer net.Dialer + conn, err := dialer.DialContext(ctx, "unix", sock) + if err != nil { + if errors.Is(err, syscall.ENOENT) || errors.Is(err, syscall.ECONNREFUSED) { + return false, nil + } + return false, fmt.Errorf("agent.stopDaemon: dial: %w", err) + } + defer conn.Close() + if deadline, ok := ctx.Deadline(); ok { + _ = conn.SetDeadline(deadline) + } + // Close the connection when ctx is canceled, so a blocked read returns. + stop := context.AfterFunc(ctx, func() { _ = conn.Close() }) + defer stop() + + sc := bufio.NewScanner(conn) + sc.Buffer(make([]byte, 64*1024), 16*1024*1024) + // The daemon greets every dialer with its Ready frame first. + if !sc.Scan() { + return false, fmt.Errorf("agent.stopDaemon: no ready frame: %w", scanErr(sc)) + } + const id = "stop-daemon" + if _, err := conn.Write([]byte(`{"id":"` + id + `","method":"agent.shutdown"}` + "\n")); err != nil { + return false, fmt.Errorf("agent.stopDaemon: write: %w", err) + } + // Events and heartbeats may come before the reply. + for sc.Scan() { + var frame struct { + ID string `json:"id"` + Error json.RawMessage `json:"error"` + } + if json.Unmarshal(sc.Bytes(), &frame) != nil || frame.ID != id { + continue + } + if len(frame.Error) > 0 && string(frame.Error) != "null" { + return false, fmt.Errorf("agent.stopDaemon: agent.shutdown failed: %s", frame.Error) + } + return true, nil + } + return false, fmt.Errorf("agent.stopDaemon: no reply: %w", scanErr(sc)) +} + +// scanErr is why a scan ended: its error, or EOF when it has none. +func scanErr(sc *bufio.Scanner) error { + if err := sc.Err(); err != nil { + return err + } + return errors.New("EOF") +} diff --git a/internal/chat/claude_config.go b/internal/chat/claude_config.go index 1997f0086..ed6fada4b 100644 --- a/internal/chat/claude_config.go +++ b/internal/chat/claude_config.go @@ -91,7 +91,7 @@ func (s *Service) ClaudeConfig(_ context.Context, raw json.RawMessage) (any, err func claudeConfigPath(given string) string { given = strings.TrimSpace(given) if given == "" { - given = os.Getenv("CLAUDE_CONFIG_DIR") + given = defaultProfileEnv("CLAUDE_CONFIG_DIR") } var path string if given != "" { diff --git a/internal/chat/codex_context.go b/internal/chat/codex_context.go index d85e1823a..ba7d7af1f 100644 --- a/internal/chat/codex_context.go +++ b/internal/chat/codex_context.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io" + "maps" "os" "path/filepath" "runtime" @@ -67,11 +68,12 @@ func (p *process) noteCodexInitialize(data []byte) { p.codexExperimental = request.Params.Capabilities != nil && request.Params.Capabilities.ExperimentalAPI } -func (p *process) withCodexContext(data []byte, context codexContext) []byte { +// prepareCodexContext stages guidance changes until stdin accepts the frame. +func (p *process) prepareCodexContext(data []byte, context codexContext) ([]byte, func()) { // Without experimentalApi Codex rejects every turn/start that carries // additionalContext, so a turn goes without the guidance instead. if !p.codexExperimental { - return data + return data, nil } var request map[string]json.RawMessage var method string @@ -81,10 +83,7 @@ func (p *process) withCodexContext(data []byte, context codexContext) []byte { json.Unmarshal(request["method"], &method) != nil || method != "turn/start" || json.Unmarshal(request["params"], ¶ms) != nil || json.Unmarshal(params["threadId"], &threadID) != nil || !sessionIDPattern.MatchString(threadID) { - return data - } - if p.codexGuidance == nil { - p.codexGuidance = make(map[string]*codexGuidance) + return data, nil } state := p.codexGuidance[threadID] compactions := p.codexCompactions.Load() @@ -99,12 +98,15 @@ func (p *process) withCodexContext(data []byte, context codexContext) []byte { hadCLI: inherited[codexCLI] != "" || (state != nil && state.hadCLI), compactions: compactions, } - p.codexGuidance[threadID] = state + } else { + copy := *state + copy.inherited = maps.Clone(state.inherited) + state = © } var fragments map[string]json.RawMessage if raw := params["additionalContext"]; raw != nil && json.Unmarshal(raw, &fragments) != nil { - return data + return data, nil } if fragments == nil { fragments = make(map[string]json.RawMessage) @@ -133,8 +135,14 @@ func (p *process) withCodexContext(data []byte, context codexContext) []byte { fragments[key], _ = json.Marshal(map[string]string{"kind": "application", "value": value}) added = true } + commit := func() { + if p.codexGuidance == nil { + p.codexGuidance = make(map[string]*codexGuidance) + } + p.codexGuidance[threadID] = state + } if !added { - return data + return data, commit } params["additionalContext"], _ = json.Marshal(fragments) // Encode the edited request once. In particular, params may contain @@ -145,7 +153,7 @@ func (p *process) withCodexContext(data []byte, context codexContext) []byte { } out["params"] = params result, _ := json.Marshal(out) - return result + return result, commit } // Use the environment of the running app server, after profile resolution on diff --git a/internal/chat/codex_context_test.go b/internal/chat/codex_context_test.go index 8c533b68f..6dc8c4729 100644 --- a/internal/chat/codex_context_test.go +++ b/internal/chat/codex_context_test.go @@ -3,6 +3,7 @@ package chat import ( "bufio" "bytes" + "context" "encoding/json" "io" "os" @@ -20,18 +21,18 @@ func TestCodexContextAugmentsOnlyTurnStart(t *testing.T) { context := codexContext{Files: "File links", CLI: &codexCLIContext{Status: "CLI guidance", Commands: "Commands"}} start := `{"method":"turn/start","id":"turn","params":{"threadId":"` + notesThread + `","input":[{"type":"text","text":"Hello"}],"additionalContext":{"other":{"kind":"untrusted","value":"other context"}}}}` - fragments := testCodexContext(t, proc.withCodexContext([]byte(start), context)) + fragments := testCodexContext(t, applyTestCodexContext(proc, []byte(start), context)) if len(fragments) != 4 || fragments["other"].Value != "other context" || fragments[codexFileLinks].Kind != "application" || fragments[codexCLI].Kind != "application" { t.Fatalf("unexpected fragments: %#v", fragments) } for _, method := range []string{"thread/start", "thread/resume", "thread/fork", "turn/steer"} { request := `{"method":"` + method + `","params":{"threadId":"` + notesThread + `"}}` - if got := string(proc.withCodexContext([]byte(request), context)); got != request { + if got := string(applyTestCodexContext(proc, []byte(request), context)); got != request { t.Errorf("%s unexpectedly changed: %s", method, got) } } - disabled := testCodexContext(t, proc.withCodexContext([]byte(start), codexContext{Files: "File links"})) + disabled := testCodexContext(t, applyTestCodexContext(proc, []byte(start), codexContext{Files: "File links"})) if disabled[codexCLI].Value != cliDisabled { t.Fatalf("did not revoke earlier CLI guidance: %#v", disabled) } @@ -68,14 +69,14 @@ func TestCodexContextUsesHostedProfileAndForkBoundary(t *testing.T) { proc := testCodexContextProcess(profile) turn := `{"method":"turn/start","params":{"threadId":"` + notesFork + `"}}` context := codexContext{Files: filePrompt, CLI: &codexCLIContext{Status: cli, Commands: commands}} - if got := string(proc.withCodexContext([]byte(turn), context)); got != turn { + if got := string(applyTestCodexContext(proc, []byte(turn), context)); got != turn { t.Fatalf("repeated inherited guidance: %s", got) } - changed := testCodexContext(t, proc.withCodexContext([]byte(turn), codexContext{Files: filePrompt, CLI: &codexCLIContext{Status: "Other tab", Commands: commands}})) + changed := testCodexContext(t, applyTestCodexContext(proc, []byte(turn), codexContext{Files: filePrompt, CLI: &codexCLIContext{Status: "Other tab", Commands: commands}})) if len(changed) != 1 || changed[codexCLI].Kind != "application" { t.Fatalf("did not refresh only the CLI guidance: %#v", changed) } - disabled := testCodexContext(t, proc.withCodexContext([]byte(turn), codexContext{Files: filePrompt})) + disabled := testCodexContext(t, applyTestCodexContext(proc, []byte(turn), codexContext{Files: filePrompt})) if len(disabled) != 1 || disabled[codexCLI].Value != cliDisabled { t.Fatalf("did not revoke inherited CLI guidance: %#v", disabled) } @@ -84,7 +85,7 @@ func TestCodexContextUsesHostedProfileAndForkBoundary(t *testing.T) { func TestCodexContextDisabledForNewThread(t *testing.T) { proc := testCodexContextProcess(t.TempDir()) turn := `{"method":"turn/start","params":{"threadId":"` + notesThread + `"}}` - fragments := testCodexContext(t, proc.withCodexContext([]byte(turn), codexContext{Files: "File links"})) + fragments := testCodexContext(t, applyTestCodexContext(proc, []byte(turn), codexContext{Files: "File links"})) if len(fragments) != 1 || fragments[codexFileLinks].Value != "File links" { t.Fatalf("expected file guidance only: %#v", fragments) } @@ -95,10 +96,10 @@ func TestCodexContextWriteKeepsJSONLFraming(t *testing.T) { var stdin bytes.Buffer proc.stdin = testCodexStdin{&stdin} turn := `{"method":"turn/start","params":{"threadId":"` + notesThread + `"}}` - if err := proc.write([]byte(turn), &codexContext{Files: "File links"}); err != nil { + if _, err := proc.writeLocked(context.Background(), []byte(turn), &codexContext{Files: "File links"}); err != nil { t.Fatal(err) } - if err := proc.write([]byte(`{"method":"initialized"}`), nil); err != nil { + if _, err := proc.writeLocked(context.Background(), []byte(`{"method":"initialized"}`), nil); err != nil { t.Fatal(err) } reader := bufio.NewReader(&stdin) @@ -120,6 +121,10 @@ func TestCodexContextWriteKeepsJSONLFraming(t *testing.T) { type testCodexStdin struct{ *bytes.Buffer } +func (w testCodexStdin) WriteContext(_ context.Context, p []byte) (int, error) { + return w.Write(p) +} + func (testCodexStdin) Close() error { return nil } func TestCodexContextAfterCompaction(t *testing.T) { @@ -137,7 +142,7 @@ func TestCodexContextAfterCompaction(t *testing.T) { proc := testCodexContextProcess(profile) turn := `{"method":"turn/start","params":{"threadId":"` + notesThread + `"}}` context := codexContext{Files: filePrompt} - if got := string(proc.withCodexContext([]byte(turn), context)); got != turn { + if got := string(applyTestCodexContext(proc, []byte(turn), context)); got != turn { t.Fatalf("repeated existing guidance: %s", got) } @@ -150,11 +155,11 @@ func TestCodexContextAfterCompaction(t *testing.T) { t.Fatal("did not recognize the compacted notification") } proc.codexCompactions.Add(1) - result := proc.withCodexContext([]byte(turn), context) + result := applyTestCodexContext(proc, []byte(turn), context) if got := testCodexContext(t, result)[codexFileLinks].Value; got != filePrompt+refreshSuffix+"1" { t.Fatalf("did not refresh removed guidance: %q", got) } - if got := proc.withCodexContext([]byte(turn), context); !bytes.Equal(result, got) { + if got := applyTestCodexContext(proc, []byte(turn), context); !bytes.Equal(result, got) { t.Fatalf("subsequent turns require the same value for Codex deduplication: %s", got) } @@ -164,7 +169,7 @@ func TestCodexContextAfterCompaction(t *testing.T) { t.Fatal(err) } proc.codexCompactions.Add(1) - if got := string(proc.withCodexContext([]byte(turn), context)); got != turn { + if got := string(applyTestCodexContext(proc, []byte(turn), context)); got != turn { t.Fatalf("repeated guidance kept in the replacement history: %s", got) } } @@ -218,9 +223,18 @@ func TestCodexContextWaitsForAnInitializeThatAskedForExperimentalApi(t *testing. for _, request := range c.requests { proc.noteCodexInitialize(request) } - got := proc.withCodexContext([]byte(turn), context) + got := applyTestCodexContext(proc, []byte(turn), context) if c.guidanceOn != (string(got) != turn) { t.Errorf("%s: guidance added = %v, want %v: %s", c.name, string(got) != turn, c.guidanceOn, got) } } } + +// applyTestCodexContext models guidance accepted by the child. +func applyTestCodexContext(p *process, data []byte, guidance codexContext) []byte { + edited, commit := p.prepareCodexContext(data, guidance) + if commit != nil { + commit() + } + return edited +} diff --git a/internal/chat/exec.go b/internal/chat/exec.go index a611c7872..d5b1b28d0 100644 --- a/internal/chat/exec.go +++ b/internal/chat/exec.go @@ -36,7 +36,7 @@ func (s *Service) Exec(ctx context.Context, raw json.RawMessage) (any, error) { stderr := &cappedBuffer{limit: execOutputCap} cmd.Stdout = stdout cmd.Stderr = stderr - err := cmd.Run() + err := runChatCommand(cmd, p.Args) result := ExecResult{Code: 0, Stdout: stdout.String(), Stderr: stderr.String()} if runCtx.Err() != nil { @@ -46,7 +46,7 @@ func (s *Service) Exec(ctx context.Context, raw json.RawMessage) (any, error) { } var exitErr *exec.ExitError switch { - case err == nil: + case err == nil, errors.Is(err, exec.ErrWaitDelay): case errors.As(err, &exitErr): result.Code = exitErr.ExitCode() default: @@ -58,6 +58,21 @@ func (s *Service) Exec(ctx context.Context, raw json.RawMessage) (any, error) { return result, nil } +// runChatCommand owns normal one-shot helpers. The app's Claude background +// command places --bg first and owns its own lifetime after launch. +func runChatCommand(cmd *exec.Cmd, args []string) error { + if len(args) != 0 && (args[0] == "--bg" || args[0] == "--background") { + return cmd.Run() + } + p := &process{cmd: cmd} + cmd.Cancel = p.signalKill + if err := p.startOwned(); err != nil { + return err + } + defer p.releaseTree() + return cmd.Wait() +} + // cappedBuffer keeps the first limit bytes written to it. type cappedBuffer struct { buf bytes.Buffer diff --git a/internal/chat/exec_test.go b/internal/chat/exec_test.go new file mode 100644 index 000000000..52209c243 --- /dev/null +++ b/internal/chat/exec_test.go @@ -0,0 +1,107 @@ +package chat + +import ( + "context" + "encoding/json" + "errors" + "io" + "net" + "os" + "os/exec" + "testing" + "time" +) + +const execTreeFixtureEnv = "NEXUS_TEST_CHAT_EXEC_TREE" + +// runExecTreeFixture leaves a helper holding both output pipes. The test +// keeps a connection to it and can detect whether it survived the launcher. +func runExecTreeFixture(address string) int { + if len(os.Args) > 1 && os.Args[1] == "--tree-helper" { + conn, err := net.Dial("tcp", address) + if err != nil { + return 1 + } + defer conn.Close() + _, _ = io.Copy(conn, conn) + return 0 + } + cmd := exec.Command(os.Args[0], "--tree-helper") + cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr + if err := cmd.Start(); err != nil { + return 1 + } + defer cmd.Process.Release() + time.Sleep(time.Minute) + return 0 +} + +// TestExecCancelsOnlyOwnedProcessTrees checks explicit cleanup while leaving +// the background task available after its launcher has gone. +func TestExecCancelsOnlyOwnedProcessTrees(t *testing.T) { + for _, tc := range []struct { + name, flag string + detached bool + }{ + {"foreground", "--version", false}, + {"background", "--bg", true}, + } { + t.Run(tc.name, func(t *testing.T) { + listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)}) + if err != nil { + t.Fatal(err) + } + defer listener.Close() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + params, err := json.Marshal(ExecParams{ + Program: os.Args[0], Cwd: t.TempDir(), Args: []string{tc.flag}, + Env: map[string]string{execTreeFixtureEnv: listener.Addr().String()}, + }) + if err != nil { + t.Fatal(err) + } + type result struct { + value any + err error + } + done := make(chan result, 1) + go func() { + value, err := New().Exec(ctx, params) + done <- result{value, err} + }() + _ = listener.SetDeadline(time.Now().Add(5 * time.Second)) + helper, err := listener.AcceptTCP() + if err != nil { + t.Fatal("launcher did not start its child:", err) + } + defer helper.Close() + cancel() + select { + case got := <-done: + if got.err != nil || !got.value.(ExecResult).TimedOut { + t.Fatalf("canceled exec returned %+v", got) + } + case <-time.After(5 * time.Second): + t.Fatal("chat.exec did not finish after cancellation") + } + _ = helper.SetDeadline(time.Now().Add(2 * time.Second)) + if tc.detached { + if _, err := helper.Write([]byte("x")); err != nil { + t.Fatal(err) + } + if _, err := io.ReadFull(helper, make([]byte, 1)); err != nil { + t.Fatal("detached child exited with the launcher:", err) + } + } else { + n, err := helper.Read(make([]byte, 1)) + // Terminating the Windows job can reset the connection. + // A read deadline instead means the helper is still connected. + var networkErr net.Error + if n != 0 || err == nil || (errors.As(err, &networkErr) && networkErr.Timeout()) { + t.Fatalf("owned child's connection did not close: read %d bytes, error %v", n, err) + } + } + }) + } +} diff --git a/internal/chat/gate.go b/internal/chat/gate.go new file mode 100644 index 000000000..433cd8971 --- /dev/null +++ b/internal/chat/gate.go @@ -0,0 +1,34 @@ +package chat + +import ( + "context" + "sync" +) + +// chatGate orders work like a mutex and lets a waiting RPC abandon its turn. +type chatGate struct { + once sync.Once + held chan struct{} +} + +func (g *chatGate) Lock() { + _ = g.lockContext(context.Background()) +} + +func (g *chatGate) lockContext(ctx context.Context) error { + g.once.Do(func() { g.held = make(chan struct{}, 1) }) + select { + case g.held <- struct{}{}: + if err := ctx.Err(); err != nil { + g.Unlock() + return err + } + return nil + case <-ctx.Done(): + return ctx.Err() + } +} + +func (g *chatGate) Unlock() { + <-g.held +} diff --git a/internal/chat/log.go b/internal/chat/log.go index c7cfa04f9..69db22d23 100644 --- a/internal/chat/log.go +++ b/internal/chat/log.go @@ -1,8 +1,10 @@ package chat import ( + "bufio" "bytes" "encoding/json" + "errors" "time" ) @@ -65,6 +67,19 @@ func (l *lineLog) add(dir string, message json.RawMessage, text string) *entry { return e } +// finishInput fills a reserved slot, if the log still includes it. +func (l *lineLog) finishInput(slot *entry, dir string, message json.RawMessage) { + for _, e := range l.since(slot.seq - 1) { + if e == slot { + l.bytes += len(message) + break + } + } + slot.dir = dir + slot.message = message + l.trim() +} + // addPinned appends a line the cap keeps. What it holds is bounded by // whoever adds it. func (l *lineLog) addPinned(dir string, message json.RawMessage) *entry { @@ -183,3 +198,30 @@ func messageType(message json.RawMessage) string { } return probe.Type } + +var errLineTooLong = errors.New("line too long") + +// readLine reads one line of at most limit bytes. A longer line is read to +// its end and dropped, with errLineTooLong. +func readLine(reader *bufio.Reader, limit int) ([]byte, error) { + var line []byte + tooLong := false + for { + chunk, err := reader.ReadSlice('\n') + if !tooLong { + if len(line)+len(chunk) > limit { + tooLong = true + line = nil + } else { + line = append(line, chunk...) + } + } + if errors.Is(err, bufio.ErrBufferFull) { + continue + } + if tooLong && err == nil { + return nil, errLineTooLong + } + return line, err + } +} diff --git a/internal/chat/log_test.go b/internal/chat/log_test.go new file mode 100644 index 000000000..d93a6b0c1 --- /dev/null +++ b/internal/chat/log_test.go @@ -0,0 +1,55 @@ +package chat + +import ( + "encoding/json" + "fmt" + "strings" + "testing" +) + +func TestLogCapDropsOldestLines(t *testing.T) { + log := newLineLog() + log.cap = 1000 + for i := 0; i < 50; i++ { + log.add(DirOut, json.RawMessage(fmt.Sprintf(`{"type":"assistant","n":%d}`, i)), "") + } + if !log.truncated || log.bytes > log.cap { + t.Fatalf("truncated %v, bytes %d", log.truncated, log.bytes) + } + entries := log.since(0) + if got := string(entries[len(entries)-1].message); got != `{"type":"assistant","n":49}` { + t.Fatalf("newest = %s", got) + } + if entries[0].seq == 1 { + t.Fatal("oldest line kept") + } +} + +func TestLogKeepsStreamOfMessageInProgress(t *testing.T) { + log := newLineLog() + log.add(DirOut, json.RawMessage(`{"type":"stream_event","n":1}`), "") + log.add(DirIn, json.RawMessage(`{"type":"user"}`), "") + log.add(DirOut, json.RawMessage(`{"type":"assistant"}`), "") + log.add(DirOut, json.RawMessage(`{"type":"stream_event","n":2}`), "") + var kinds []string + for _, e := range log.since(0) { + kinds = append(kinds, messageType(e.message)) + } + if got := strings.Join(kinds, ","); got != "user,assistant,stream_event" { + t.Fatalf("log = %s", got) + } +} + +func TestMessageType(t *testing.T) { + cases := map[string]string{ + `{"type":"assistant","message":{}}`: "assistant", + `{"message":{"type":"x"},"type":"result"}`: "result", + `{"type":"a\"b"}`: `a"b`, + `not json`: "", + } + for input, want := range cases { + if got := messageType(json.RawMessage(input)); got != want { + t.Errorf("messageType(%s) = %q, want %q", input, got, want) + } + } +} diff --git a/internal/chat/login_env.go b/internal/chat/login_env.go index b7d0ae4b1..d63093e19 100644 --- a/internal/chat/login_env.go +++ b/internal/chat/login_env.go @@ -5,9 +5,12 @@ import ( "os" "os/exec" "runtime" + "sort" "strings" "sync" "time" + + "github.com/nexus-code/nexus-code/internal/agentpaths" ) // A tab's program gets the environment the user's login shell sets up, as a @@ -119,3 +122,86 @@ func fromLoginShell(key string) bool { } return !inheritedOnly(key) } + +// mergeEnv overlays the login shell's environment, then +// the tab's variables, on the agent's environment. The app's wrapper +// directory goes first on the PATH, as for terminals, and variables a +// parent Claude Code session sets are left out, so the program starts as +// its own session. +func mergeEnv(overrides map[string]string) []string { + merged := make(map[string]string) + for _, entry := range os.Environ() { + key, value, ok := strings.Cut(entry, "=") + if !ok || inheritedOnly(key) { + continue + } + merged[key] = agentpaths.ProfileEnvDir(key, value) + } + for key, value := range loginEnv() { + merged[key] = agentpaths.ProfileEnvDir(key, value) + } + for key, value := range overrides { + for existing := range merged { + if sameEnvKey(existing, key) { + delete(merged, existing) + } + } + merged[key] = agentpaths.ProfileEnvDir(key, value) + } + if dir := overrides[wrapperDirEnv]; dir != "" { + prependPath(merged, dir) + } + if !hasLocale(merged) { + merged["LANG"] = "C.UTF-8" + } + keys := make([]string, 0, len(merged)) + for key := range merged { + keys = append(keys, key) + } + sort.Strings(keys) + env := make([]string, 0, len(keys)) + for _, key := range keys { + env = append(env, key+"="+merged[key]) + } + return env +} + +const wrapperDirEnv = "NEXUS_WRAPPER_SELF_DIR" + +// inheritedOnly names the variables of a Claude Code session the agent may +// have been started from, which would make the program take itself for a +// part of that session. +func inheritedOnly(key string) bool { + switch key { + case "CLAUDECODE", "CLAUDE_CODE_ENTRYPOINT", "CLAUDE_CODE_SSE_PORT": + return true + } + return false +} + +// prependPath puts dir first on env's PATH, unless it is first already. +func prependPath(env map[string]string, dir string) { + sep := string(os.PathListSeparator) + for key, value := range env { + if sameEnvKey(key, "PATH") { + if value == dir || strings.HasPrefix(value, dir+sep) { + return + } + if value != "" { + dir += sep + value + } + env[key] = dir + return + } + } + env["PATH"] = dir +} + +func hasLocale(env map[string]string) bool { + for _, key := range []string{"LANG", "LC_ALL", "LC_CTYPE"} { + if env[key] != "" { + return true + } + } + return false +} diff --git a/internal/chat/login_env_test.go b/internal/chat/login_env_test.go index 98da521e5..01461a21e 100644 --- a/internal/chat/login_env_test.go +++ b/internal/chat/login_env_test.go @@ -1,6 +1,7 @@ package chat import ( + "fmt" "os" "path/filepath" "reflect" @@ -10,6 +11,9 @@ import ( ) func TestMain(m *testing.M) { + if address := os.Getenv(execTreeFixtureEnv); address != "" { + os.Exit(runExecTreeFixture(address)) + } // The tests' programs get the test's environment, not the login shell // of whoever runs them. loginEnv = func() map[string]string { return nil } @@ -99,3 +103,55 @@ func TestMergeEnvPutsTheLoginShellBetweenTheAgentAndTheTab(t *testing.T) { } } } + +func TestLaunchAndHistoryUseTheSameDefaultClaudeDirectory(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(home, "agent-default")) + was := loginEnv + loginEnv = func() map[string]string { + return map[string]string{"CLAUDE_CONFIG_DIR": "~/login-default"} + } + t.Cleanup(func() { loginEnv = was }) + + dir := configDir("") + want := filepath.Join(home, "login-default") + if dir != want { + t.Fatalf("history directory = %q, want %q", dir, want) + } + for _, entry := range mergeEnv(nil) { + if entry == "CLAUDE_CONFIG_DIR="+dir { + return + } + } + t.Fatalf("launch did not use history directory %q", dir) +} + +func TestEnvPutsWrapperFirstAndDropsParentSession(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("PATH separator") + } + t.Setenv("CLAUDECODE", "1") + service := New() + rec := newRecorder() + service.SetEventSink(rec.sink) + defer service.Close() + + wrapper := t.TempDir() + startFake(t, service, "tab", map[string]string{ + "PATH": "/usr/bin:/bin", + "NEXUS_WRAPPER_SELF_DIR": wrapper, + "NEXUS_TAB_ID": "tab", + }) + _ = send(t, service, "tab", "env") + rec.waitFor(t, "the env", func(lines []LinePayload, _ []ExitPayload) bool { + return hasOut(lines, `"type":"env"`) + }) + rec.mu.Lock() + defer rec.mu.Unlock() + want := fmt.Sprintf(`{"type":"env","path":%q,"tab":"tab","claudecode":""}`, wrapper+":/usr/bin:/bin") + if !hasOut(rec.lines, want) { + t.Fatalf("want %s in %s", want, describe(rec.lines)) + } +} diff --git a/internal/chat/process_unix.go b/internal/chat/process_unix.go index b843f509b..f677eaf3d 100644 --- a/internal/chat/process_unix.go +++ b/internal/chat/process_unix.go @@ -3,39 +3,102 @@ package chat import ( - "errors" "os" + "sync" "syscall" + + hostproc "github.com/nexus-code/nexus-code/internal/process" + "golang.org/x/sys/unix" ) -// tree is nothing of its own on Unix: the program's process group and its -// descendants, found when it is signalled, are the commands it runs. -type tree struct{} +// tree records the commands seen at explicit stop so escalation can still +// reach them after the parent exits and the OS reparents its children. +type tree struct { + mu sync.Mutex + start uint64 + targets map[int]uint64 + released bool +} + +// holdTree records the launched group's leader. +func (p *process) holdTree() error { + p.tree.mu.Lock() + defer p.tree.mu.Unlock() + p.tree.start = processStart(p.cmd.Process.Pid) + return nil +} -func (p *process) holdTree() {} -func (p *process) releaseTree() {} +// releaseTree completes any explicit termination before reporting exit. +func (p *process) releaseTree() { + p.tree.mu.Lock() + defer p.tree.mu.Unlock() + if p.tree.targets != nil { + _ = p.signalOwnedTargets(syscall.SIGKILL, false) + p.tree.targets = nil + } + p.tree.released = true +} // signalTerm asks the program and the commands it runs to exit. func (p *process) signalTerm() error { - return signalTree(p.cmd.Process.Pid, syscall.SIGTERM) + return p.signalOwnedTree(syscall.SIGTERM) } -// kill kills the program and the commands it runs. -func (p *process) kill() { - _ = signalTree(p.cmd.Process.Pid, syscall.SIGKILL) +// signalKill forcefully ends the program and the commands it runs. +func (p *process) signalKill() error { + return p.signalOwnedTree(syscall.SIGKILL) +} + +// signalOwnedTree discovers detached tool sessions before signalling. +func (p *process) signalOwnedTree(signal syscall.Signal) error { + p.tree.mu.Lock() + defer p.tree.mu.Unlock() + if p.tree.released { + return os.ErrProcessDone + } + pid := p.cmd.Process.Pid + start := processStart(pid) + if p.tree.start == 0 { + p.tree.start = start + } + leaderAlive := start != 0 && start == p.tree.start + if p.tree.targets == nil { + p.tree.targets = make(map[int]uint64) + } + if leaderAlive { + for _, child := range descendants(pid) { + if start := processStart(child); start != 0 { + p.tree.targets[child] = start + } + } + } + err := p.signalOwnedTargets(signal, leaderAlive) + if !leaderAlive && start == 0 && p.tree.start == 0 { + // The proc table may be unavailable even while the launcher runs. + return p.cmd.Process.Signal(signal) + } + return err } -// signalTree signals the program's process group, and every process under -// the program: Claude and Codex start each command in a session of its -// own, out of the group. Ending the program alone left them running, as a -// server Claude started was once the agent exited. Its descendants are -// found first, while the program runs: once it has exited, they are -// another process's children. A command left running when the program -// exits by itself is not found. -func signalTree(pid int, signal syscall.Signal) error { - under := descendants(pid) - err := signalGroup(pid, signal) - for _, child := range under { +// signalOwnedTargets checks start times so recorded PIDs do not identify +// unrelated jobs after the grace period and PID reuse. +func (p *process) signalOwnedTargets(signal syscall.Signal, leaderAlive bool) error { + pid := p.cmd.Process.Pid + groupAlive := leaderAlive + targets := make([]int, 0, len(p.tree.targets)) + for child, start := range p.tree.targets { + if processStart(child) == start { + targets = append(targets, child) + if pgid, err := unix.Getpgid(child); err == nil && pgid == pid { + groupAlive = true + } + } + } + var err error + if groupAlive { + err = hostproc.SignalGroup(pid, signal) + } + for _, child := range targets { _ = syscall.Kill(child, signal) } return err @@ -66,14 +129,6 @@ func descendants(pid int) []int { return found } -func signalGroup(pid int, signal syscall.Signal) error { - err := syscall.Kill(-pid, signal) - if errors.Is(err, syscall.ESRCH) { - return nil - } - return err -} - // exitPayload translates the program's exit into the chat.exit wire shape. func exitPayload(key tabKey, state *os.ProcessState, err error) ExitPayload { payload := ExitPayload{WorkspaceID: key.workspaceID, TabID: key.tabID} diff --git a/internal/chat/process_unix_test.go b/internal/chat/process_unix_test.go index 823c3403f..7de614c2c 100644 --- a/internal/chat/process_unix_test.go +++ b/internal/chat/process_unix_test.go @@ -87,3 +87,29 @@ func TestDescendantsOfAProcessWithNone(t *testing.T) { t.Fatalf("descendants of a pid no process has = %v", got) } } + +// TestStopReapsDetachedToolAfterItsParentExits checks the escalation after +// the shell exits on TERM but its app-owned tool ignores the signal. +func TestStopReapsDetachedToolAfterItsParentExits(t *testing.T) { + service := New() + rec := newRecorder() + service.SetEventSink(rec.sink) + defer service.Close() + startFake(t, service, "tab", map[string]string{"NEXUS_CHAT_OWN_SESSION": "1", "NEXUS_CHAT_RESIST_TERM": "1"}) + pid := childPid(t, service, rec) + defer func() { _ = syscall.Kill(pid, syscall.SIGKILL) }() + rec.waitFor(t, "the child's TERM handler", func(lines []LinePayload, _ []ExitPayload) bool { + for _, line := range lines { + if strings.Contains(string(line.Message), "resistant-child-ready") { + return true + } + } + return false + }) + if _, err := service.Stop(context.Background(), tabParams("tab")); err != nil { + t.Fatal(err) + } + if !ended(t, pid) { + t.Fatal("app-owned child survived Stop after its parent exited") + } +} diff --git a/internal/chat/process_windows.go b/internal/chat/process_windows.go index 5a2a2d326..08a7d4231 100644 --- a/internal/chat/process_windows.go +++ b/internal/chat/process_windows.go @@ -3,18 +3,19 @@ package chat import ( - "errors" + "fmt" "os" "strings" "sync" "syscall" - "unsafe" + + hostproc "github.com/nexus-code/nexus-code/internal/process" "golang.org/x/sys/windows" ) func newSysProcAttr() *syscall.SysProcAttr { - return &syscall.SysProcAttr{} + return &syscall.SysProcAttr{CreationFlags: windows.CREATE_SUSPENDED} } // tree is the job object the program runs in. It holds the processes the @@ -27,38 +28,21 @@ type tree struct { job windows.Handle } -// holdTree puts the program in a job of its own. A process it starts -// before it is in the job is left out; one that waits on the program's -// input still ends when the input is closed. -func (p *process) holdTree() { - job, err := windows.CreateJobObject(nil, nil) - if err != nil { - return - } - var info windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION - // What is in the job when it is let go of ends with it. No process may - // leave it: Git Bash, which runs Claude's commands, starts each program - // out of any job that lets it. - info.BasicLimitInformation.LimitFlags = windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE - if _, err := windows.SetInformationJobObject(job, windows.JobObjectExtendedLimitInformation, - uintptr(unsafe.Pointer(&info)), uint32(unsafe.Sizeof(info))); err != nil { - _ = windows.CloseHandle(job) - return - } - process, err := windows.OpenProcess(windows.PROCESS_SET_QUOTA|windows.PROCESS_TERMINATE, - false, uint32(p.cmd.Process.Pid)) +// holdTree assigns the suspended launcher before it can start any children. +func (p *process) holdTree() error { + p.tree.mu.Lock() + defer p.tree.mu.Unlock() + pid := p.cmd.Process.Pid + job, err := hostproc.AssignJob(pid, true) if err != nil { - _ = windows.CloseHandle(job) - return + return fmt.Errorf("assign chat process to job: %w", err) } - defer windows.CloseHandle(process) - if err := windows.AssignProcessToJobObject(job, process); err != nil { + if err := hostproc.ResumeSuspended(uint32(pid)); err != nil { _ = windows.CloseHandle(job) - return + return fmt.Errorf("resume chat process: %w", err) } - p.tree.mu.Lock() p.tree.job = job - p.tree.mu.Unlock() + return nil } // releaseTree lets go of the job once the program has exited, which ends @@ -80,23 +64,18 @@ func (p *process) endTree() bool { return p.tree.job != 0 && windows.TerminateJobObject(p.tree.job, 1) == nil } -// signalTerm ends the program and what it runs. Windows has no signal to -// ask it with. +// signalTerm uses the job on Windows, where there is no portable TERM. func (p *process) signalTerm() error { - if p.endTree() { - return nil - } - err := p.cmd.Process.Kill() - if errors.Is(err, os.ErrProcessDone) { - return nil - } - return err + return p.signalKill() } -func (p *process) kill() { - if !p.endTree() { - _ = p.cmd.Process.Kill() +// signalKill terminates the job, or the launcher if it was cancelled during +// creation before it was able to join the job. +func (p *process) signalKill() error { + if p.endTree() { + return nil } + return p.cmd.Process.Kill() } // exitPayload translates the program's exit into the chat.exit wire shape. diff --git a/internal/chat/proctable_darwin.go b/internal/chat/proctable_darwin.go index 194956dab..dc475af08 100644 --- a/internal/chat/proctable_darwin.go +++ b/internal/chat/proctable_darwin.go @@ -16,3 +16,12 @@ func parents() map[int]int { } return table } + +// processStart identifies a process by its kernel start timestamp. +func processStart(pid int) uint64 { + p, err := unix.SysctlKinfoProc("kern.proc.pid", pid) + if err != nil { + return 0 + } + return uint64(p.Proc.P_starttime.Sec)*1_000_000 + uint64(p.Proc.P_starttime.Usec) +} diff --git a/internal/chat/proctable_linux.go b/internal/chat/proctable_linux.go index 61ad12f92..d566849fb 100644 --- a/internal/chat/proctable_linux.go +++ b/internal/chat/proctable_linux.go @@ -6,6 +6,8 @@ import ( "bytes" "os" "strconv" + + hostproc "github.com/nexus-code/nexus-code/internal/process" ) // parents maps each process to its parent, read from /proc. @@ -37,14 +39,27 @@ func parents() map[int]int { // parentheses itself, so fields are counted from the last ")": the state, // then the parent. func parentInStat(data []byte) (int, bool) { - end := bytes.LastIndexByte(data, ')') - if end < 0 { - return 0, false - } - fields := bytes.Fields(data[end+1:]) + _, suffix := hostproc.SplitPIDStat(data) + fields := bytes.Fields(suffix) if len(fields) < 2 { return 0, false } ppid, err := strconv.Atoi(string(fields[1])) return ppid, err == nil } + +// processStart identifies a process by the kernel's start ticks. +func processStart(pid int) uint64 { + data, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat") + if err != nil { + return 0 + } + _, suffix := hostproc.SplitPIDStat(data) + fields := bytes.Fields(suffix) + const startTimeField = 22 - 3 // suffix begins with field 3, state. + if len(fields) <= startTimeField { + return 0 + } + start, _ := strconv.ParseUint(string(fields[startTimeField]), 10, 64) + return start +} diff --git a/internal/chat/profile.go b/internal/chat/profile.go index 67eb21f78..6ebd79ed6 100644 --- a/internal/chat/profile.go +++ b/internal/chat/profile.go @@ -17,7 +17,8 @@ import ( "runtime" "strings" - nexusfs "github.com/nexus-code/nexus-code/internal/fs" + "github.com/nexus-code/nexus-code/internal/agentpaths" + "github.com/nexus-code/nexus-code/internal/filelink" ) // ProfileDirParams is the wire shape for chat.profileDir. @@ -55,7 +56,7 @@ func (s *Service) ProfileDir(_ context.Context, raw json.RawMessage) (any, error return nil, protocolError("chat.profileDir agent must be claude or codex") } home, _ := os.UserHomeDir() - dir := expandProfileDir(p.Dir, home) + dir := agentpaths.ExpandProfileDir(p.Dir, home) if dir == "" || !filepath.IsAbs(dir) { return nil, protocolError("chat.profileDir dir must be absolute or start with ~/") } @@ -99,29 +100,6 @@ func (s *Service) ProfileDir(_ context.Context, raw json.RawMessage) (any, error return result, nil } -// expandProfileDir resolves "~" and "~/…" against home and cleans the path. -func expandProfileDir(dir, home string) string { - dir = strings.TrimSpace(dir) - switch { - case dir == "": - return "" - case dir == "~": - dir = home - case strings.HasPrefix(dir, "~/") || strings.HasPrefix(dir, `~\`): - if home == "" { - return "" - } - rest := dir[2:] - if dir[1] == '\\' && runtime.GOOS != "windows" { - // A folder picked on Windows ("~\.claude\work"), on a host - // whose separator is "/". - rest = strings.ReplaceAll(rest, `\`, "/") - } - dir = filepath.Join(home, rest) - } - return filepath.Clean(dir) -} - // defaultProfileDir is the folder the agent uses with its variable unset, or // set by the user's login shell. func defaultProfileDir(agent, home string) string { @@ -129,11 +107,8 @@ func defaultProfileDir(agent, home string) string { if agent == "codex" { key, name = "CODEX_HOME", ".codex" } - if value := strings.TrimSpace(loginEnv()[key]); value != "" { - return expandProfileDir(value, home) - } - if value := strings.TrimSpace(os.Getenv(key)); value != "" { - return expandProfileDir(value, home) + if value := defaultProfileEnv(key); value != "" { + return agentpaths.ProfileEnvDir(key, value) } if home == "" { return "" @@ -141,6 +116,14 @@ func defaultProfileDir(agent, home string) string { return filepath.Join(home, name) } +// defaultProfileEnv matches the precedence of the login shell in chat launches. +func defaultProfileEnv(key string) string { + if value := strings.TrimSpace(loginEnv()[key]); value != "" { + return value + } + return strings.TrimSpace(os.Getenv(key)) +} + func sameDir(a, b string) bool { if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { return strings.EqualFold(filepath.Clean(a), filepath.Clean(b)) @@ -206,7 +189,7 @@ func makeLink(link, target string, dir bool) error { return os.Symlink(target, link) } if dir { - return nexusfs.Junction(link, target) + return filelink.Junction(link, target) } if err := os.Symlink(target, link); err == nil { return nil diff --git a/internal/chat/profile_test.go b/internal/chat/profile_test.go index dd6d6239d..00fa4ba68 100644 --- a/internal/chat/profile_test.go +++ b/internal/chat/profile_test.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" "testing" + + "github.com/nexus-code/nexus-code/internal/agentpaths" ) func profileDirCall(t *testing.T, params ProfileDirParams) (ProfileDirResult, error) { @@ -118,7 +120,7 @@ func TestProfileFoldersPickedOnWindowsReadOnEveryHost(t *testing.T) { t.Setenv("USERPROFILE", home) want := filepath.Join(home, ".claude-work", "main") for _, dir := range []string{`~\.claude-work\main`, "~/.claude-work/main"} { - if got := expandProfileDir(dir, home); got != want { + if got := agentpaths.ExpandProfileDir(dir, home); got != want { t.Errorf("expandProfileDir(%q) = %q, want %q", dir, got, want) } // The readers of sessions, transcripts and settings take it alike. diff --git a/internal/chat/profile_windows_test.go b/internal/chat/profile_windows_test.go index 644589604..b2e51379b 100644 --- a/internal/chat/profile_windows_test.go +++ b/internal/chat/profile_windows_test.go @@ -5,7 +5,7 @@ import ( "path/filepath" "testing" - nexusfs "github.com/nexus-code/nexus-code/internal/fs" + "github.com/nexus-code/nexus-code/internal/filelink" ) // Without Developer Mode or an administrator, a profile's shared folder is a @@ -25,7 +25,7 @@ func TestJunctionsAndHardLinksAreKnownAsLinks(t *testing.T) { t.Fatal(err) } } - if err := nexusfs.Junction(filepath.Join(own, "skills"), filepath.Join(def, "skills")); err != nil { + if err := filelink.Junction(filepath.Join(own, "skills"), filepath.Join(def, "skills")); err != nil { t.Fatal(err) } if err := os.Link(filepath.Join(def, "config.toml"), filepath.Join(own, "config.toml")); err != nil { diff --git a/internal/chat/service.go b/internal/chat/service.go index 1e98732f8..90aac076a 100644 --- a/internal/chat/service.go +++ b/internal/chat/service.go @@ -3,12 +3,14 @@ package chat import ( "bufio" "bytes" + "container/list" "context" + "crypto/rand" + "encoding/hex" "encoding/json" "errors" "fmt" "io" - "os" "os/exec" "path/filepath" "sort" @@ -20,13 +22,19 @@ import ( "github.com/nexus-code/nexus-code/internal/dispatch" "github.com/nexus-code/nexus-code/internal/nodeshim" "github.com/nexus-code/nexus-code/internal/proto" + "github.com/nexus-code/nexus-code/internal/redact" ) // Service owns the tabs' programs and their logs. type Service struct { - mu sync.Mutex - sink EventSink - sessions map[tabKey]*session + mu sync.Mutex + sink *connectionSink + sessions map[tabKey]*session + closedTabs map[closedTabGeneration]struct{} + // closedOrder lets expiry and capacity discard the oldest closes first. + closedOrder list.List + closed bool + transcriptPages transcriptSnapshotCache } type tabKey struct { @@ -34,6 +42,22 @@ type tabKey struct { tabID string } +const ( + maxClosedTabGenerations = 4096 + // Covers a reconnect queue wait, an agent request, and login environment setup. + closedTabRetention = 2 * time.Minute +) + +type closedTabGeneration struct { + key tabKey + generation string +} + +type closedTabRecord struct { + closedTabGeneration + expiresAt time.Time +} + // session is one tab: its log, and the program running for it, if any. A // program that exits leaves the log, so the tab can still show the // conversation and start the program again on it. @@ -44,26 +68,31 @@ type session struct { // mu orders the log and its events: a line is added and sent under it, // and chat.replay holds it while it sends the log, so live lines follow. - mu sync.Mutex + mu chatGate log *lineLog proc *process + // pending reserves the next line while stdin is writing. Output readers + // can fill the capped log while publication waits for the write result. + pending *entry // snaps takes the originals of the files a Codex program changes. snaps *snapshots // closed is set once the tab is closed: its program, still ending, // sends nothing more, as the tab may already run another. - closed bool + closed bool + tabGeneration string } // process is one run of a tab's program. type process struct { + generation string canonicalCwd string cmd *exec.Cmd - stdin io.WriteCloser + stdin stdinWriter // outputs are the stdout and stderr pipes, closed once the exit is // reported, in case a process the program left behind holds them open. outputs []io.Closer // writeMu orders writes to stdin. - writeMu sync.Mutex + writeMu chatGate // codexGuidance is used under writeMu, alongside the requests it accompanies. codexGuidance map[string]*codexGuidance codexCompactions atomic.Uint64 @@ -74,8 +103,9 @@ type process struct { codexExperimental bool done chan struct{} - stderrMu sync.Mutex - stderrTail []byte + stderrMu sync.Mutex + stderrTail []byte + stderrTruncated bool stopOnce sync.Once @@ -86,7 +116,7 @@ type process struct { // New constructs an empty service. func New() *Service { - return &Service{sessions: make(map[tabKey]*session)} + return &Service{sessions: make(map[tabKey]*session), closedTabs: make(map[closedTabGeneration]struct{})} } // Register binds the chat.* methods onto the dispatcher. @@ -99,6 +129,7 @@ func Register(d *dispatch.Dispatcher, service *Service) { d.Register("chat.list", service.List) d.Register("chat.exec", service.Exec) d.Register("chat.transcript", service.Transcript) + d.Register("chat.transcriptPage", service.TranscriptPage) d.Register("chat.subagentTranscript", service.SubagentTranscript) d.Register("chat.sessionFile", service.SessionFile) d.Register("chat.stageThread", service.StageThread) @@ -111,16 +142,28 @@ func Register(d *dispatch.Dispatcher, service *Service) { d.Register("chat.profileDir", service.ProfileDir) } -// SetEventSink wires the service to the stdio host. +// connectionSink binds a sink to the host that installed it. +type connectionSink struct { + ctx context.Context + emit EventSink +} + +// SetEventSink wires an in-process sink without a connection context. func (s *Service) SetEventSink(sink EventSink) { + s.SetConnectionSink(nil, sink) +} + +// SetConnectionSink advances the generation used by chat replay. +func (s *Service) SetConnectionSink(ctx context.Context, sink EventSink) { s.mu.Lock() - s.sink = sink - s.mu.Unlock() + defer s.mu.Unlock() + s.sink = &connectionSink{ctx: ctx, emit: sink} } // Close kills every program, as the agent shuts down. func (s *Service) Close() { s.mu.Lock() + s.closed = true sessions := make([]*session, 0, len(s.sessions)) for key, sess := range s.sessions { sessions = append(sessions, sess) @@ -128,7 +171,11 @@ func (s *Service) Close() { } s.mu.Unlock() for _, sess := range sessions { - if p := sess.running(); p != nil { + sess.mu.Lock() + sess.closed = true + p := sess.proc + sess.mu.Unlock() + if p != nil { p.kill() } } @@ -150,44 +197,80 @@ func (s *Service) Start(_ context.Context, raw json.RawMessage) (any, error) { // Out of the tab's lock: the login shell may take a while to answer. env := mergeEnv(p.Env) - sess := s.sessionFor(key) + sess, err := s.sessionFor(key, p.TabGeneration) + if err != nil { + return nil, err + } + return s.startSession(sess, p, env) +} + +// startSession starts only while the session still owns the tab. +func (s *Service) startSession(sess *session, p StartParams, env []string) (any, error) { sess.mu.Lock() defer sess.mu.Unlock() + s.mu.Lock() + stale := s.sessions[sess.key] != sess || s.tabClosedLocked(sess.key, p.TabGeneration) + s.mu.Unlock() + if stale { + return nil, staleTab(sess.key) + } + if p.TabGeneration != "" { + sess.tabGeneration = p.TabGeneration + } if sess.proc != nil { return StartResult{PID: sess.proc.cmd.Process.Pid, Attached: true, CanonicalCwd: sess.proc.canonicalCwd}, nil } + defer func() { + if sess.proc == nil { + s.dropIfEmpty(sess) + } + }() + var generation [16]byte + if _, err := rand.Read(generation[:]); err != nil { + return nil, requestFailed("chat.start process generation: %s", err) + } // A node package's command runs as node on its script: through npm's // .cmd, cmd.exe would end a multi-line argument at its first line. cmd := nodeshim.Command(p.Program, p.Args...) cmd.Dir = p.Cwd cmd.Env = env - cmd.SysProcAttr = newSysProcAttr() - stdin, err := cmd.StdinPipe() + generationID := hex.EncodeToString(generation[:]) + stdin, childStdin, err := newChatStdin(generationID) if err != nil { return nil, requestFailed("chat.start: %s", err) } + defer childStdin.Close() + cmd.Stdin = childStdin + started := false + defer func() { + if !started { + _ = stdin.Close() + } + }() stdout, err := cmd.StdoutPipe() if err != nil { return nil, requestFailed("chat.start: %s", err) } stderr, err := cmd.StderrPipe() if err != nil { + _ = stdout.Close() return nil, requestFailed("chat.start: %s", err) } - if err := cmd.Start(); err != nil { - s.dropIfEmpty(sess) + proc := &process{ + generation: generationID, + cmd: cmd, + stdin: stdin, + outputs: []io.Closer{stdout, stderr}, + done: make(chan struct{}), + } + if err := proc.startOwned(); err != nil { return nil, requestFailed("chat.start: %s", err) } + _ = childStdin.Close() + started = true canonicalCwd, _ := filepath.EvalSymlinks(p.Cwd) - proc := &process{ - canonicalCwd: canonicalCwd, - cmd: cmd, - stdin: stdin, - outputs: []io.Closer{stdout, stderr}, - done: make(chan struct{}), - } - proc.holdTree() + proc.canonicalCwd = canonicalCwd sess.proc = proc if p.ResetLog { sess.log.reset() @@ -196,7 +279,7 @@ func (s *Service) Start(_ context.Context, raw json.RawMessage) (any, error) { if p.ResetLog || sess.snaps == nil || sess.snaps.cwd != p.Cwd { sess.snaps = newSnapshots(p.Cwd) } - start := map[string]any{"event": "start", "pid": cmd.Process.Pid} + start := map[string]any{"event": "start", "pid": cmd.Process.Pid, "processGeneration": proc.generation} if canonicalCwd != "" { start["canonicalCwd"] = canonicalCwd } @@ -209,7 +292,7 @@ func (s *Service) Start(_ context.Context, raw json.RawMessage) (any, error) { stderrDone := make(chan struct{}) go func() { defer close(stdoutDone) - sess.readStdout(stdout) + sess.readStdout(proc, stdout) }() go func() { defer close(stderrDone) @@ -220,7 +303,7 @@ func (s *Service) Start(_ context.Context, raw json.RawMessage) (any, error) { } // Send writes one message to the tab's program and logs it. -func (s *Service) Send(_ context.Context, raw json.RawMessage) (any, error) { +func (s *Service) Send(ctx context.Context, raw json.RawMessage) (any, error) { var p SendParams if err := decodeParams(raw, &p, "chat.send params must include workspaceId, tabId and message"); err != nil { return nil, err @@ -229,6 +312,9 @@ func (s *Service) Send(_ context.Context, raw json.RawMessage) (any, error) { if err != nil { return nil, err } + if err := ctx.Err(); err != nil { + return nil, err + } var line bytes.Buffer if len(p.Message) == 0 || json.Compact(&line, p.Message) != nil { return nil, protocolError("chat.send message must be JSON") @@ -237,24 +323,62 @@ func (s *Service) Send(_ context.Context, raw json.RawMessage) (any, error) { if sess == nil { return nil, notRunning(key) } - sess.mu.Lock() + if err := sess.mu.lockContext(ctx); err != nil { + return nil, err + } proc := sess.proc + sess.mu.Unlock() if proc == nil { + return nil, notRunning(key) + } + if err := proc.writeMu.lockContext(ctx); err != nil { + return nil, err + } + defer proc.writeMu.Unlock() + if err := sess.mu.lockContext(ctx); err != nil { + return nil, err + } + if sess.closed || sess.proc != proc { sess.mu.Unlock() return nil, notRunning(key) } - message := json.RawMessage(bytes.Clone(line.Bytes())) - sess.addLocked(DirIn, message, "") + slot := sess.log.add(DirSys, nil, "") + sess.pending = slot sess.mu.Unlock() - // Written outside the log's lock: a program that stops reading its - // input must not hold up its output. - if err := proc.write(line.Bytes(), p.CodexContext); err != nil { - return nil, requestFailed("chat.send: %s", err) + message := json.RawMessage(bytes.Clone(line.Bytes())) + written, err := proc.writeLocked(ctx, line.Bytes(), p.CodexContext) + if err != nil { + code := "chat.stdin-write-failed" + if written > 0 { + code = "chat.stdin-incomplete" + // The next request must not be appended to a partial JSON line. + _ = proc.stdin.Close() + } + failure, _ := json.Marshal(map[string]string{"event": "stdin-failed", "code": code}) + sess.finishInput(slot, DirSys, failure) + return nil, proto.NewError(code, fmt.Sprintf("chat.send: %s", err)) } + sess.finishInput(slot, DirIn, message) return struct{}{}, nil } +// finishInput publishes the write outcome before any output produced while +// the write was in flight. Earlier logs still use the same DirIn format. +func (sess *session) finishInput(slot *entry, dir string, message json.RawMessage) { + sess.mu.Lock() + defer sess.mu.Unlock() + sess.pending = nil + if sess.closed { + return + } + sess.log.finishInput(slot, dir, message) + sess.emitLineLocked(slot) + for _, e := range sess.log.since(slot.seq) { + sess.emitLineLocked(e) + } +} + // Stop ends the tab's program and keeps its log. func (s *Service) Stop(_ context.Context, raw json.RawMessage) (any, error) { var p TabParams @@ -286,12 +410,26 @@ func (s *Service) CloseTab(_ context.Context, raw json.RawMessage) (any, error) return nil, err } s.mu.Lock() + if p.TabGeneration != "" { + s.rememberClosedTabLocked(closedTabGeneration{key, p.TabGeneration}, time.Now()) + } sess := s.sessions[key] - delete(s.sessions, key) s.mu.Unlock() if sess != nil { sess.mu.Lock() + s.mu.Lock() + current := s.sessions[key] == sess + if !current || (p.TabGeneration != "" && sess.tabGeneration != "" && sess.tabGeneration != p.TabGeneration) { + s.mu.Unlock() + sess.mu.Unlock() + return struct{}{}, nil + } + delete(s.sessions, key) + s.mu.Unlock() sess.closed = true + if p.TabGeneration != "" { + sess.tabGeneration = p.TabGeneration + } proc := sess.proc sess.mu.Unlock() if proc != nil { @@ -303,7 +441,7 @@ func (s *Service) CloseTab(_ context.Context, raw json.RawMessage) (any, error) } // Replay sends the tab's log again as chat.line events, then answers. -func (s *Service) Replay(_ context.Context, raw json.RawMessage) (any, error) { +func (s *Service) Replay(ctx context.Context, raw json.RawMessage) (any, error) { var p ReplayParams if err := decodeParams(raw, &p, "chat.replay params must include workspaceId and tabId"); err != nil { return nil, err @@ -312,21 +450,42 @@ func (s *Service) Replay(_ context.Context, raw json.RawMessage) (any, error) { if err != nil { return nil, err } - sess := s.lookup(key) + s.mu.Lock() + sink, sess := s.sink, s.sessions[key] + s.mu.Unlock() + if err := s.checkReplayConnection(ctx, sink); err != nil { + return nil, err + } if sess == nil { return ReplayResult{Found: false}, nil } - sess.mu.Lock() + if err := sess.mu.lockContext(ctx); err != nil { + return nil, err + } defer sess.mu.Unlock() last := p.AfterSeq for _, e := range sess.log.since(p.AfterSeq) { + if sess.pending != nil && e.seq >= sess.pending.seq { + break + } payload := sess.payload(e) payload.Replay = true - if err := s.emit(EventLine, payload); err != nil { - return nil, requestFailed("chat.replay: %s", err) + if err := s.checkReplayConnection(ctx, sink); err != nil { + return nil, err + } + if sink != nil && sink.emit != nil { + if err := sink.emit(EventLine, payload); err != nil { + if ctxErr := s.checkReplayConnection(ctx, sink); ctxErr != nil { + return nil, ctxErr + } + return nil, requestFailed("chat.replay: %s", err) + } } last = e.seq } + if err := s.checkReplayConnection(ctx, sink); err != nil { + return nil, err + } return ReplayResult{ Found: true, Running: sess.proc != nil, @@ -335,6 +494,21 @@ func (s *Service) Replay(_ context.Context, raw json.RawMessage) (any, error) { }, nil } +// checkReplayConnection rejects work from a superseded host even when its +// previous write just completed. Dispatch passes the host's context through. +func (s *Service) checkReplayConnection(ctx context.Context, sink *connectionSink) error { + if err := ctx.Err(); err != nil { + return err + } + s.mu.Lock() + current := s.sink == sink + s.mu.Unlock() + if !current || sink != nil && sink.ctx != nil && sink.ctx.Done() != ctx.Done() { + return context.Canceled + } + return nil +} + // List reports the tabs the agent has logs for. func (s *Service) List(_ context.Context, raw json.RawMessage) (any, error) { var p ListParams @@ -370,16 +544,59 @@ func (s *Service) List(_ context.Context, raw json.RawMessage) (any, error) { return result, nil } -// sessionFor returns the tab's session, making one when there is none. -func (s *Service) sessionFor(key tabKey) *session { +// sessionFor returns or creates the session for a still-open generation. +func (s *Service) sessionFor(key tabKey, generation string) (*session, error) { s.mu.Lock() defer s.mu.Unlock() + if s.tabClosedLocked(key, generation) { + return nil, staleTab(key) + } if sess := s.sessions[key]; sess != nil { - return sess + return sess, nil } - sess := &session{service: s, key: key, createdAt: time.Now(), log: newLineLog()} + sess := &session{service: s, key: key, createdAt: time.Now(), log: newLineLog(), tabGeneration: generation} s.sessions[key] = sess - return sess + return sess, nil +} + +// tabClosedLocked checks close intents even when they arrived before session creation. +func (s *Service) tabClosedLocked(key tabKey, generation string) bool { + s.expireClosedTabsLocked(time.Now()) + _, closed := s.closedTabs[closedTabGeneration{key, generation}] + return s.closed || closed +} + +// rememberClosedTabLocked keeps a bounded window for rejecting late starts. +func (s *Service) rememberClosedTabLocked(tab closedTabGeneration, now time.Time) { + s.expireClosedTabsLocked(now) + if _, exists := s.closedTabs[tab]; exists { + return + } + if len(s.closedTabs) == maxClosedTabGenerations { + s.forgetClosedTabLocked(s.closedOrder.Front()) + } + s.closedTabs[tab] = struct{}{} + s.closedOrder.PushBack(closedTabRecord{tab, now.Add(closedTabRetention)}) +} + +// expireClosedTabsLocked purges expired generations without a background timer. +func (s *Service) expireClosedTabsLocked(now time.Time) { + for oldest := s.closedOrder.Front(); oldest != nil; oldest = s.closedOrder.Front() { + if now.Before(oldest.Value.(closedTabRecord).expiresAt) { + return + } + s.forgetClosedTabLocked(oldest) + } +} + +// forgetClosedTabLocked removes one generation from the index and expiry order. +func (s *Service) forgetClosedTabLocked(element *list.Element) { + record := s.closedOrder.Remove(element).(closedTabRecord) + delete(s.closedTabs, record.closedTabGeneration) +} + +func staleTab(key tabKey) error { + return proto.NewError("chat.tab-closed", "chat tab "+key.tabID+" is closed") } // dropIfEmpty forgets a session made for a program that did not start. @@ -404,10 +621,10 @@ func (s *Service) emit(event string, payload any) error { s.mu.Lock() sink := s.sink s.mu.Unlock() - if sink == nil { + if sink == nil || sink.emit == nil { return nil } - return sink(event, payload) + return sink.emit(event, payload) } // running returns the session's program, nil when none runs. @@ -424,7 +641,18 @@ func (sess *session) addLocked(dir string, message json.RawMessage, text string) return } e := sess.log.add(dir, message, text) - _ = sess.service.emit(EventLine, sess.payload(e)) + if sess.pending == nil { + sess.emitLineLocked(e) + } +} + +// emitLineLocked relays one committed input, output or system event. +func (sess *session) emitLineLocked(e *entry) { + payload := sess.payload(e) + if sess.proc != nil { + payload.ProcessGeneration = sess.proc.generation + } + _ = sess.service.emit(EventLine, payload) } // addSysLocked logs a line of the agent's own. @@ -450,7 +678,9 @@ func (sess *session) addPinnedLocked(fields any) { return } e := sess.log.addPinned(DirSys, bytes.TrimRight(data.Bytes(), "\n")) - _ = sess.service.emit(EventLine, sess.payload(e)) + if sess.pending == nil { + sess.emitLineLocked(e) + } } func (sess *session) payload(e *entry) LinePayload { @@ -466,16 +696,18 @@ func (sess *session) payload(e *entry) LinePayload { } // readStdout logs each line the program writes. -func (sess *session) readStdout(stdout io.Reader) { +func (sess *session) readStdout(proc *process, stdout io.Reader) { reader := bufio.NewReaderSize(stdout, 64*1024) for { line, err := readLine(reader, maxLineBytes) if len(line) > 0 { - sess.addOutput(line) + sess.addOutput(proc, line) } if errors.Is(err, errLineTooLong) { sess.mu.Lock() - sess.addSysLocked(map[string]any{"event": "dropped", "reason": "line too long"}) + if sess.proc == proc { + sess.addSysLocked(map[string]any{"event": "dropped", "reason": "line too long"}) + } sess.mu.Unlock() continue } @@ -485,16 +717,20 @@ func (sess *session) readStdout(stdout io.Reader) { } } -func (sess *session) addOutput(line []byte) { +// addOutput records only lines from the process that still owns the session. +func (sess *session) addOutput(proc *process, line []byte) { line = bytes.TrimRight(line, "\r\n") if len(bytes.TrimSpace(line)) == 0 { return } sess.mu.Lock() defer sess.mu.Unlock() + if sess.closed || sess.proc != proc { + return + } if json.Valid(line) { - if sess.proc != nil && isCodexCompaction(line) { - sess.proc.codexCompactions.Add(1) + if isCodexCompaction(line) { + proc.codexCompactions.Add(1) } sess.addLocked(DirOut, json.RawMessage(line), "") if sess.snaps != nil { @@ -530,10 +766,11 @@ drain: exit := exitPayload(sess.key, state, err) exit.StderrTail = proc.stderr() + exit.ProcessGeneration = proc.generation + // Closing stdin releases any blocked write. Its outcome precedes exit. + proc.writeMu.Lock() + defer proc.writeMu.Unlock() sess.mu.Lock() - if sess.proc == proc { - sess.proc = nil - } fields := map[string]any{"event": "exit", "code": exit.Code} if exit.Signal != nil { fields["signal"] = *exit.Signal @@ -542,34 +779,74 @@ drain: fields["stderrTail"] = exit.StderrTail } sess.addSysLocked(fields) - // A closed tab's program is told gone only while no other runs for the tab. - told := !sess.closed || sess.service.lookup(sess.key) == nil - sess.mu.Unlock() - close(proc.done) + // Explicit close already told the host its generation was gone. Legacy + // close still expects the exit while no replacement session is present. + told := !sess.closed || (sess.tabGeneration == "" && sess.service.lookup(sess.key) == nil) if told { _ = sess.service.emit(EventExit, exit) } + if sess.proc == proc { + sess.proc = nil + } + close(proc.done) + sess.mu.Unlock() } -func (p *process) write(data []byte, context *codexContext) error { - p.writeMu.Lock() - defer p.writeMu.Unlock() - p.noteCodexInitialize(data) - if context != nil { - data = p.withCodexContext(data, *context) +// writeLocked writes one full line as part of the process's ordered send. +func (p *process) writeLocked(ctx context.Context, data []byte, guidance *codexContext) (int, error) { + if err := ctx.Err(); err != nil { + return 0, err + } + original := data + var commit func() + if guidance != nil { + data, commit = p.prepareCodexContext(data, *guidance) + } + line := append(data, '\n') + n, err := p.stdin.WriteContext(ctx, line) + if n == len(line) { + // The child owns this frame even if cancellation raced with Write. + p.noteCodexInitialize(original) + if commit != nil { + commit() + } + return n, nil + } + if err == nil { + err = io.ErrShortWrite } - _, err := p.stdin.Write(append(data, '\n')) - return err + return n, err +} + +// startOwned starts a process with the platform's tree ownership policy. +func (p *process) startOwned() error { + p.cmd.SysProcAttr = newSysProcAttr() + if err := p.cmd.Start(); err != nil { + return err + } + if err := p.holdTree(); err != nil { + _ = p.cmd.Process.Kill() + _ = p.cmd.Wait() + return err + } + return nil +} + +// kill immediately terminates the owned process tree. +func (p *process) kill() { + _ = p.signalKill() } // stop asks the program to exit, and kills it if it has not after stopGrace. func (p *process) stop() { p.stopOnce.Do(func() { _ = p.signalTerm() + timer := time.NewTimer(stopGrace) go func() { + defer timer.Stop() select { case <-p.done: - case <-time.After(stopGrace): + case <-timer.C: p.kill() } }() @@ -584,6 +861,7 @@ func (p *process) readStderr(stderr io.Reader) { p.stderrMu.Lock() p.stderrTail = append(p.stderrTail, buf[:n]...) if over := len(p.stderrTail) - stderrTailBytes; over > 0 { + p.stderrTruncated = true p.stderrTail = append([]byte(nil), p.stderrTail[over:]...) } p.stderrMu.Unlock() @@ -597,117 +875,12 @@ func (p *process) readStderr(stderr io.Reader) { func (p *process) stderr() string { p.stderrMu.Lock() defer p.stderrMu.Unlock() - return strings.ToValidUTF8(string(p.stderrTail), "") -} - -var errLineTooLong = errors.New("line too long") - -// readLine reads one line of at most limit bytes. A longer line is read to -// its end and dropped, with errLineTooLong. -func readLine(reader *bufio.Reader, limit int) ([]byte, error) { - var line []byte - tooLong := false - for { - chunk, err := reader.ReadSlice('\n') - if !tooLong { - if len(line)+len(chunk) > limit { - tooLong = true - line = nil - } else { - line = append(line, chunk...) - } - } - if errors.Is(err, bufio.ErrBufferFull) { - continue - } - if tooLong && err == nil { - return nil, errLineTooLong - } - return line, err - } -} - -// mergeEnv overlays the login shell's environment (./login_env.go), then -// the tab's variables, on the agent's environment. The app's wrapper -// directory goes first on the PATH, as for terminals, and variables a -// parent Claude Code session sets are left out, so the program starts as -// its own session. -func mergeEnv(overrides map[string]string) []string { - merged := make(map[string]string) - for _, entry := range os.Environ() { - key, value, ok := strings.Cut(entry, "=") - if !ok || inheritedOnly(key) { - continue - } - merged[key] = value - } - for key, value := range loginEnv() { - merged[key] = value - } - for key, value := range overrides { - for existing := range merged { - if sameEnvKey(existing, key) { - delete(merged, existing) - } - } - merged[key] = value - } - if dir := overrides[wrapperDirEnv]; dir != "" { - prependPath(merged, dir) - } - if !hasLocale(merged) { - merged["LANG"] = "C.UTF-8" - } - keys := make([]string, 0, len(merged)) - for key := range merged { - keys = append(keys, key) - } - sort.Strings(keys) - env := make([]string, 0, len(keys)) - for _, key := range keys { - env = append(env, key+"="+merged[key]) - } - return env -} - -const wrapperDirEnv = "NEXUS_WRAPPER_SELF_DIR" - -// inheritedOnly names the variables of a Claude Code session the agent may -// have been started from, which would make the program take itself for a -// part of that session. -func inheritedOnly(key string) bool { - switch key { - case "CLAUDECODE", "CLAUDE_CODE_ENTRYPOINT", "CLAUDE_CODE_SSE_PORT": - return true - } - return false -} - -// prependPath puts dir first on env's PATH, unless it is first already. -func prependPath(env map[string]string, dir string) { - sep := string(os.PathListSeparator) - for key, value := range env { - if sameEnvKey(key, "PATH") { - if value == dir || strings.HasPrefix(value, dir+sep) { - return - } - if value != "" { - dir += sep + value - } - env[key] = dir - return - } - } - env["PATH"] = dir -} - -func hasLocale(env map[string]string) bool { - for _, key := range []string{"LANG", "LC_ALL", "LC_CTYPE"} { - if env[key] != "" { - return true - } + tail := strings.ToValidUTF8(string(p.stderrTail), "") + if p.stderrTruncated { + _, tail, _ = strings.Cut(tail, "\n") + return "[Chat stderr truncated]\n" + redact.Credentials(tail) } - return false + return redact.Credentials(tail) } func decodeParams(raw json.RawMessage, target any, message string) error { @@ -734,7 +907,7 @@ func notRunning(key tabKey) error { } func requestFailed(format string, args ...any) error { - return proto.CodedError{Code: proto.CodeRequestFailed, Msg: fmt.Sprintf(format, args...)} + return proto.RequestFailed(format, args...) } func protocolError(message string) error { diff --git a/internal/chat/service_test.go b/internal/chat/service_test.go index 0b0c50e90..d8f8da692 100644 --- a/internal/chat/service_test.go +++ b/internal/chat/service_test.go @@ -2,8 +2,10 @@ package chat import ( "bufio" + "bytes" "context" "encoding/json" + "errors" "fmt" "io" "os" @@ -66,6 +68,9 @@ func TestFakeProgramHelper(t *testing.T) { // Start a child that runs until it is ended, and tell its pid. child := exec.Command(os.Args[0], "-test.run=^TestFakeSleeperHelper$") child.Env = append(os.Environ(), "NEXUS_CHAT_SLEEPER=1") + if os.Getenv("NEXUS_CHAT_RESIST_TERM") == "1" { + child.Stdout = os.Stdout + } // As Claude and Codex start each command, out of the program's group. if os.Getenv("NEXUS_CHAT_OWN_SESSION") == "1" { ownSession(child) @@ -103,6 +108,10 @@ func TestFakeSleeperHelper(t *testing.T) { if os.Getenv("NEXUS_CHAT_SLEEPER") != "1" { return } + if os.Getenv("NEXUS_CHAT_RESIST_TERM") == "1" { + signal.Ignore(syscall.SIGTERM) + fmt.Println(`{"type":"resistant-child-ready"}`) + } time.Sleep(time.Minute) os.Exit(0) } @@ -245,6 +254,125 @@ func tabParams(tabID string) json.RawMessage { return params } +type testStdin func([]byte) (int, error) + +func (w testStdin) WriteContext(_ context.Context, data []byte) (int, error) { return w(data) } +func (w testStdin) Close() error { return nil } + +type partialStdin struct { + closed bool +} + +func (p *partialStdin) WriteContext(_ context.Context, data []byte) (int, error) { + if p.closed { + return 0, io.ErrClosedPipe + } + return len(data) - 1, io.ErrShortWrite +} + +func (p *partialStdin) Close() error { + p.closed = true + return nil +} + +func TestPartialWriteClosesTheUnframedStdin(t *testing.T) { + service := New() + sess, err := service.sessionFor(tabKey{workspaceID: "ws", tabID: "tab"}, "") + if err != nil { + t.Fatal(err) + } + pipe := &partialStdin{} + sess.proc = &process{stdin: pipe} + if err := send(t, service, "tab", "partially written"); err == nil { + t.Fatal("partial write reported success") + } + if !pipe.closed { + t.Fatal("a subsequent message could be written onto the partial JSON line") + } +} + +func TestSendLogsAConfirmedInputBeforeEarlyOutput(t *testing.T) { + service := New() + rec := newRecorder() + service.SetEventSink(rec.sink) + key := tabKey{workspaceID: "ws", tabID: "tab"} + sess, err := service.sessionFor(key, "") + if err != nil { + t.Fatal(err) + } + reading := make(chan struct{}) + unblock := make(chan struct{}) + sess.proc = &process{ + stdin: testStdin(func(data []byte) (int, error) { + if !bytes.Contains(data, []byte("hello")) { + return len(data), nil + } + // Simulates output the CLI produced before Write returned. + sess.addOutput(sess.proc, []byte(`{"type":"result"}`)) + close(reading) + <-unblock + return len(data), nil + }), + } + sent := make(chan error, 1) + go func() { sent <- send(t, service, "tab", "hello") }() + select { + case <-reading: + case <-time.After(10 * time.Second): + t.Fatal("stdout could not be consumed while stdin was writing") + } + queued := make(chan error, 1) + go func() { queued <- send(t, service, "tab", "second") }() + replay, replayErr := service.Replay(context.Background(), tabParams("tab")) + close(unblock) + if err := <-sent; err != nil { + t.Fatal(err) + } + if err := <-queued; err != nil { + t.Fatal(err) + } + if replayErr != nil || replay.(ReplayResult).LastSeq != 0 { + t.Fatalf("replay exposed an input still being written: %+v %v", replay, replayErr) + } + rec.mu.Lock() + defer rec.mu.Unlock() + if len(rec.lines) != 3 || rec.lines[0].Dir != DirIn || rec.lines[1].Dir != DirOut || + rec.lines[2].Dir != DirIn || !bytes.Contains(rec.lines[2].Message, []byte("second")) { + t.Fatalf("want confirmed input followed by its output, got %v", describe(rec.lines)) + } +} + +func TestFailedStdinWriteReplaysFailureInsteadOfApproval(t *testing.T) { + service := New() + rec := newRecorder() + service.SetEventSink(rec.sink) + key := tabKey{workspaceID: "ws", tabID: "tab"} + sess, err := service.sessionFor(key, "") + if err != nil { + t.Fatal(err) + } + sess.proc = &process{ + stdin: testStdin(func(_ []byte) (int, error) { + return 0, io.ErrClosedPipe + }), + } + sess.addOutput(sess.proc, []byte(`{"type":"control_request","request_id":"p","request":{"subtype":"can_use_tool"}}`)) + params := json.RawMessage(`{"workspaceId":"ws","tabId":"tab","message":{"type":"control_response","response":{"request_id":"p"}}}`) + if _, err := service.Send(context.Background(), params); err == nil { + t.Fatal("the write succeeded despite the broken pipe") + } + rec.reset() + if _, err := service.Replay(context.Background(), tabParams("tab")); err != nil { + t.Fatal(err) + } + rec.mu.Lock() + defer rec.mu.Unlock() + if len(rec.lines) != 2 || rec.lines[0].Dir != DirOut || rec.lines[1].Dir != DirSys || + !strings.Contains(string(rec.lines[1].Message), `"stdin-failed"`) { + t.Fatalf("the failed approval was replayed as sent: %v", describe(rec.lines)) + } +} + func TestReplayDropsStreamEventsOfCompleteMessages(t *testing.T) { service := New() rec := newRecorder() @@ -539,6 +667,164 @@ func TestCloseForgetsTheTab(t *testing.T) { } } +func TestClosedTabGenerationRejectsLateStart(t *testing.T) { + service := New() + defer service.Close() + closed := json.RawMessage(`{"workspaceId":"ws","tabId":"tab","tabGeneration":"old"}`) + if _, err := service.CloseTab(context.Background(), closed); err != nil { + t.Fatal(err) + } + start, _ := json.Marshal(map[string]any{ + "workspaceId": "ws", "tabId": "tab", "tabGeneration": "old", + "cwd": t.TempDir(), "program": os.Args[0], + "args": []string{"-test.run=^TestFakeProgramHelper$"}, "env": map[string]string{"NEXUS_CHAT_FAKE": "1"}, + }) + if _, err := service.Start(context.Background(), start); err == nil { + t.Fatal("a start from the closed tab reached the process") + } + if service.lookup(tabKey{workspaceID: "ws", tabID: "tab"}) != nil { + t.Fatal("late start registered a chat session") + } +} + +func TestClosedTabGenerationsStayBounded(t *testing.T) { + useAttachRoot(t) + service := New() + defer service.Close() + ctx := context.Background() + const limit = maxClosedTabGenerations + closeTab := func(tabID, generation string) { + t.Helper() + raw, _ := json.Marshal(TabParams{WorkspaceID: "ws", TabID: tabID, TabGeneration: generation}) + if _, err := service.CloseTab(ctx, raw); err != nil { + t.Fatal(err) + } + } + for i := range limit + 1 { + closeTab(fmt.Sprintf("tab-%d", i), "first") + } + if got := len(service.closedTabs); got > limit { + t.Fatalf("recorded %d closed tabs, limit %d", got, limit) + } + for i := range limit + 1 { + closeTab("reopened", fmt.Sprintf("generation-%d", i)) + } + if got := len(service.closedTabs); got > limit { + t.Fatalf("recorded %d closed generations, limit %d", got, limit) + } + if got := service.closedOrder.Len(); got != len(service.closedTabs) { + t.Fatalf("expiry queue has %d records for %d closed generations", got, len(service.closedTabs)) + } + key := tabKey{workspaceID: "ws", tabID: "reopened"} + if _, err := service.sessionFor(key, fmt.Sprintf("generation-%d", limit)); err == nil { + t.Fatal("the latest closed generation could start") + } + if _, err := service.sessionFor(key, "generation-0"); err != nil { + t.Fatalf("the oldest generation was not evicted: %v", err) + } +} + +func TestClosedTabGenerationsExpire(t *testing.T) { + service := New() + key := tabKey{workspaceID: "ws", tabID: "tab"} + tab := closedTabGeneration{key, "old"} + service.mu.Lock() + service.rememberClosedTabLocked(tab, time.Now().Add(-closedTabRetention)) + service.mu.Unlock() + if _, err := service.sessionFor(key, tab.generation); err != nil { + t.Fatalf("the expired generation was not removed: %v", err) + } + if len(service.closedTabs) != 0 || service.closedOrder.Len() != 0 { + t.Fatal("the expired generation stayed in the index or expiry queue") + } +} + +func TestStartCannotUseARemovedSession(t *testing.T) { + for _, closeTab := range []bool{true, false} { + name := "failed start" + if closeTab { + name = "close" + } + t.Run(name, func(t *testing.T) { + service := New() + defer service.Close() + key := tabKey{workspaceID: "ws", tabID: "tab"} + held, err := service.sessionFor(key, "") + if err != nil { + t.Fatal(err) + } + dir := t.TempDir() + if closeTab { + if _, err := service.CloseTab(context.Background(), tabParams("tab")); err != nil { + t.Fatal(err) + } + } else if _, err := service.startSession(held, StartParams{Cwd: dir, Program: filepath.Join(dir, "missing")}, nil); err == nil { + t.Fatal("the invalid process started") + } + if _, err := service.startSession(held, StartParams{ + Cwd: dir, Program: os.Args[0], Args: []string{"-test.run=^TestFakeProgramHelper$"}, + }, mergeEnv(map[string]string{"NEXUS_CHAT_FAKE": "1"})); err == nil { + t.Fatal("a retained session could start an untracked process") + } + startFake(t, service, "tab", nil) + }) + } +} + +func TestStopPublishesExitBeforeReleasingProcess(t *testing.T) { + service := New() + defer service.Close() + exiting := make(chan struct{}) + proceed := make(chan struct{}) + service.SetEventSink(func(event string, _ any) error { + if event == EventExit { + close(exiting) + <-proceed + } + return nil + }) + startFake(t, service, "tab", nil) + proc := service.lookup(tabKey{workspaceID: "ws", tabID: "tab"}).running() + stopped := make(chan struct{}) + go func() { + defer close(stopped) + _, _ = service.Stop(context.Background(), tabParams("tab")) + }() + defer func() { + close(proceed) + <-stopped + }() + select { + case <-exiting: + case <-time.After(10 * time.Second): + t.Fatal("agent did not emit the exit") + } + select { + case <-proc.done: + t.Fatal("stop released the process before publishing the exit") + default: + } +} + +func TestDelayedStdoutCannotBeAttributedToTheReplacement(t *testing.T) { + service := New() + rec := newRecorder() + service.SetEventSink(rec.sink) + sess, err := service.sessionFor(tabKey{workspaceID: "ws", tabID: "tab"}, "") + if err != nil { + t.Fatal(err) + } + old := &process{generation: "old"} + replacement := &process{generation: "current"} + sess.proc = replacement + sess.addOutput(old, []byte(`{"type":"result","subtype":"interrupted"}`)) + sess.addOutput(replacement, []byte(`{"type":"result","subtype":"success"}`)) + if len(rec.lines) != 1 || rec.lines[0].ProcessGeneration != "current" || + !bytes.Contains(rec.lines[0].Message, []byte("success")) { + t.Fatalf("a delayed result was attributed to the replacement: %v", describe(rec.lines)) + } +} + func TestAClosedTabsProgramSendsNothingToTheTabsNext(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("signals") @@ -589,338 +875,128 @@ func TestAClosedTabsProgramSendsNothingToTheTabsNext(t *testing.T) { } } -func TestEnvPutsWrapperFirstAndDropsParentSession(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("PATH separator") - } - t.Setenv("CLAUDECODE", "1") - service := New() - rec := newRecorder() - service.SetEventSink(rec.sink) - defer service.Close() - - wrapper := t.TempDir() - startFake(t, service, "tab", map[string]string{ - "PATH": "/usr/bin:/bin", - "NEXUS_WRAPPER_SELF_DIR": wrapper, - "NEXUS_TAB_ID": "tab", - }) - _ = send(t, service, "tab", "env") - rec.waitFor(t, "the env", func(lines []LinePayload, _ []ExitPayload) bool { - return hasOut(lines, `"type":"env"`) - }) - rec.mu.Lock() - defer rec.mu.Unlock() - want := fmt.Sprintf(`{"type":"env","path":%q,"tab":"tab","claudecode":""}`, wrapper+":/usr/bin:/bin") - if !hasOut(rec.lines, want) { - t.Fatalf("want %s in %s", want, describe(rec.lines)) - } -} - -func TestLogCapDropsOldestLines(t *testing.T) { - log := newLineLog() - log.cap = 1000 - for i := 0; i < 50; i++ { - log.add(DirOut, json.RawMessage(fmt.Sprintf(`{"type":"assistant","n":%d}`, i)), "") - } - if !log.truncated || log.bytes > log.cap { - t.Fatalf("truncated %v, bytes %d", log.truncated, log.bytes) - } - entries := log.since(0) - if got := string(entries[len(entries)-1].message); got != `{"type":"assistant","n":49}` { - t.Fatalf("newest = %s", got) - } - if entries[0].seq == 1 { - t.Fatal("oldest line kept") - } -} - -func TestLogKeepsStreamOfMessageInProgress(t *testing.T) { - log := newLineLog() - log.add(DirOut, json.RawMessage(`{"type":"stream_event","n":1}`), "") - log.add(DirIn, json.RawMessage(`{"type":"user"}`), "") - log.add(DirOut, json.RawMessage(`{"type":"assistant"}`), "") - log.add(DirOut, json.RawMessage(`{"type":"stream_event","n":2}`), "") - var kinds []string - for _, e := range log.since(0) { - kinds = append(kinds, messageType(e.message)) - } - if got := strings.Join(kinds, ","); got != "user,assistant,stream_event" { - t.Fatalf("log = %s", got) - } -} - -func TestMessageType(t *testing.T) { - cases := map[string]string{ - `{"type":"assistant","message":{}}`: "assistant", - `{"message":{"type":"x"},"type":"result"}`: "result", - `{"type":"a\"b"}`: `a"b`, - `not json`: "", - } - for input, want := range cases { - if got := messageType(json.RawMessage(input)); got != want { - t.Errorf("messageType(%s) = %q, want %q", input, got, want) - } - } -} - -func TestTranscriptFindsSessionInAnyProject(t *testing.T) { - dir := t.TempDir() - project := filepath.Join(dir, "projects", "-some-project") - if err := os.MkdirAll(project, 0o755); err != nil { - t.Fatal(err) - } - id := "0b6a3c2e-6f1e-4a59-9d0e-2f6d7c1a8b90" - content := `{"type":"user","n":1}` + "\n" + "garbage\n" + `{"type":"assistant","n":2}` + "\n" - if err := os.WriteFile(filepath.Join(project, id+".jsonl"), []byte(content), 0o644); err != nil { - t.Fatal(err) - } - service := New() - params, _ := json.Marshal(TranscriptParams{SessionID: id, ConfigDir: dir}) - result, err := service.Transcript(context.Background(), params) +// TestCanceledSendDoesNotWaitForEarlierWrites verifies a queued send can +// leave without writing or consuming an input sequence number. +func TestCanceledSendDoesNotWaitForEarlierWrites(t *testing.T) { + s := New() + sess, err := s.sessionFor(tabKey{workspaceID: "ws", tabID: "tab"}, "") if err != nil { - t.Fatalf("Transcript: %v", err) - } - got := result.(TranscriptResult) - if !got.Found || len(got.Lines) != 2 || string(got.Lines[1]) != `{"type":"assistant","n":2}` { - t.Fatalf("transcript = %+v", got) - } - - params, _ = json.Marshal(TranscriptParams{SessionID: "../../etc/passwd", ConfigDir: dir}) - if _, err := service.Transcript(context.Background(), params); err == nil { - t.Fatal("Transcript took a path for a session id") - } -} - -func TestSubagentTranscriptFindsAnAgentByItsIdOrItsCall(t *testing.T) { - dir := t.TempDir() - id := "0b6a3c2e-6f1e-4a59-9d0e-2f6d7c1a8b90" - folder := filepath.Join(dir, "projects", "-some-project", id, "subagents") - body := `{"parentUuid":null,"isSidechain":true,"agentId":"a1b2c3","type":"user","message":{"role":"user","content":"go"}}` + "\n" + - `{"type":"api-request-blob","base":"..."}` + "\n" + - "garbage\n" + - `{"parentUuid":"x","isSidechain":true,"type":"assistant","message":{"content":[]}}` + "\n" + - `{"type":"attachment","attachment":{}}` + "\n" - writeTestFile(t, filepath.Join(folder, "agent-a1b2c3.jsonl"), body) - writeTestFile(t, filepath.Join(folder, "agent-a1b2c3.meta.json"), `{"agentType":"Explore","toolUseId":"toolu_01"}`) - writeTestFile(t, filepath.Join(folder, "agent-other.jsonl"), `{"type":"user","message":{}}`+"\n") - writeTestFile(t, filepath.Join(folder, "agent-other.meta.json"), `{"agentType":"Plan","toolUseId":"toolu_02"}`) - service := New() - read := func(params SubagentTranscriptParams) SubagentTranscriptResult { - t.Helper() - raw, _ := json.Marshal(params) - result, err := service.SubagentTranscript(context.Background(), raw) - if err != nil { - t.Fatalf("SubagentTranscript(%+v): %v", params, err) - } - return result.(SubagentTranscriptResult) - } - - got := read(SubagentTranscriptParams{SessionID: id, AgentID: "a1b2c3", ConfigDir: dir}) - if !got.Found || len(got.Lines) != 2 { - t.Fatalf("by agent id = %+v", got) - } - if typ := messageType(got.Lines[1]); typ != "assistant" { - t.Fatalf("second line is %q", typ) - } - if got := read(SubagentTranscriptParams{SessionID: id, ToolUseID: "toolu_01", ConfigDir: dir}); !got.Found || len(got.Lines) != 2 { - t.Fatalf("by call = %+v", got) - } - // An agent id with no file of its own is looked for by its call. - if got := read(SubagentTranscriptParams{SessionID: id, AgentID: "gone", ToolUseID: "toolu_02", ConfigDir: dir}); !got.Found || len(got.Lines) != 1 { - t.Fatalf("by call after the id = %+v", got) - } - if got := read(SubagentTranscriptParams{SessionID: id, ToolUseID: "toolu_03", ConfigDir: dir}); got.Found { - t.Fatalf("a call with no agent = %+v", got) - } - - for _, bad := range []SubagentTranscriptParams{ - {SessionID: "../../etc/passwd", AgentID: "a1b2c3", ConfigDir: dir}, - {SessionID: id, AgentID: "../../x", ConfigDir: dir}, - {SessionID: id, ConfigDir: dir}, - } { - raw, _ := json.Marshal(bad) - if _, err := service.SubagentTranscript(context.Background(), raw); err == nil { - t.Fatalf("SubagentTranscript took %+v", bad) - } - } -} - -func writeTestFile(t *testing.T, path, body string) { - t.Helper() - if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(path, []byte(body), 0o644); err != nil { t.Fatal(err) } -} - -func TestSessionFileFindsClaudeSessions(t *testing.T) { - service := New() - find := func(params SessionFileParams) SessionFileResult { - t.Helper() - raw, _ := json.Marshal(params) - result, err := service.SessionFile(context.Background(), raw) - if err != nil { - t.Fatalf("SessionFile(%+v): %v", params, err) + writes := make(chan struct{}, 1) + p := &process{stdin: testStdin(func(data []byte) (int, error) { + writes <- struct{}{} + return len(data), nil + })} + sess.proc = p + p.writeMu.Lock() + defer p.writeMu.Unlock() + ctx, cancel := context.WithCancel(context.Background()) + cancel() + result := make(chan error, 1) + go func() { + _, err := s.Send(ctx, json.RawMessage(`{"workspaceId":"ws","tabId":"tab","message":{"text":"canceled"}}`)) + result <- err + }() + select { + case err := <-result: + if !errors.Is(err, context.Canceled) || sess.log.lastSeq() != 0 { + t.Fatalf("canceled send returned %v with log sequence %d", err, sess.log.lastSeq()) } - return result.(SessionFileResult) - } - id := "0b6a3c2e-6f1e-4a59-9d0e-2f6d7c1a8b90" - claude := t.TempDir() - transcript := filepath.Join(claude, "projects", "-w", id+".jsonl") - writeTestFile(t, transcript, "{}\n") - if got := find(SessionFileParams{SessionID: id, ConfigDir: claude}); !got.Found || got.Path != transcript { - t.Fatalf("claude = %+v", got) - } - if got := find(SessionFileParams{SessionID: "1b6a3c2e-6f1e-4a59-9d0e-2f6d7c1a8b90", ConfigDir: claude}); got.Found { - t.Fatalf("a session not there = %+v", got) + case <-time.After(2 * time.Second): + t.Fatal("canceled send waited behind a blocked write") } - raw, _ := json.Marshal(SessionFileParams{SessionID: "../../etc/passwd", ConfigDir: claude}) - if _, err := service.SessionFile(context.Background(), raw); err == nil { - t.Fatal("SessionFile took a path for a session id") + select { + case <-writes: + t.Fatal("canceled request wrote to the child") + default: } } -func TestStageThreadPutsACopyWhereTheProfileFindsIt(t *testing.T) { - service := New() - stage := func(params StageThreadParams) StageThreadResult { - t.Helper() - raw, _ := json.Marshal(params) - result, err := service.StageThread(context.Background(), raw) - if err != nil { - t.Fatalf("StageThread(%+v): %v", params, err) - } - return result.(StageThreadResult) - } - id := "01a1c494-518d-7651-938d-00031c0ec9fb" - name := "rollout-2026-11-10T17-30-17-" + id + ".jsonl" - from, to := t.TempDir(), t.TempDir() - writeTestFile(t, filepath.Join(from, "sessions", "2026", "11", "10", name), `{"type":"session_meta"}`+"\n") - staged := filepath.Join(to, "sessions", stagedDir, name) - - if got := stage(StageThreadParams{ThreadID: id, From: from, To: to}); !got.Found { - t.Fatalf("stage = %+v", got) - } - if body, err := os.ReadFile(staged); err != nil || string(body) != `{"type":"session_meta"}`+"\n" { - t.Fatalf("staged copy = %q, %v", body, err) - } - // The copy is not one of the profile's own threads. - if got := rolloutPath(to, id); got != "" { - t.Fatalf("rolloutPath took the staged copy: %s", got) - } - // The copy stays once the thread is forked: the fork reads its history - // from it each time it resumes. - if got := stage(StageThreadParams{ThreadID: id, From: from, To: to}); !got.Found { - t.Fatalf("stage again = %+v", got) - } - if _, err := os.Stat(staged); err != nil { - t.Fatalf("the staged copy went: %v", err) - } - - // An archived thread is staged too: the fork unarchives nothing. - if err := os.RemoveAll(filepath.Join(from, "sessions")); err != nil { - t.Fatal(err) - } - if err := os.RemoveAll(filepath.Join(to, "sessions")); err != nil { +// TestReplayCannotFollowAReplacedConnection changes sinks while the first +// old frame is still being delivered. +func TestReplayCannotFollowAReplacedConnection(t *testing.T) { + s := New() + sess, err := s.sessionFor(tabKey{workspaceID: "ws", tabID: "tab"}, "") + if err != nil { t.Fatal(err) } - writeTestFile(t, filepath.Join(from, "archived_sessions", name), "{}\n") - if got := stage(StageThreadParams{ThreadID: id, From: from, To: to}); !got.Found { - t.Fatalf("archived stage = %+v", got) - } - if _, err := os.Stat(staged); err != nil { - t.Fatalf("archived thread not staged: %v", err) - } - - // A copy longer than the thread is not cut back: a fork made from it - // reads it to where it was forked. - writeTestFile(t, staged, "{}\n{}\n") - stage(StageThreadParams{ThreadID: id, From: from, To: to}) - if body, _ := os.ReadFile(staged); string(body) != "{}\n{}\n" { - t.Fatalf("a longer copy was cut back: %q", body) - } - - // Profiles of one folder need no copy; a thread not there is not found. - if got := stage(StageThreadParams{ThreadID: id, From: from, To: from}); !got.Found { - t.Fatalf("same folder = %+v", got) - } - if _, err := os.Stat(filepath.Join(from, "sessions", stagedDir)); !os.IsNotExist(err) { - t.Fatalf("a copy was staged in the folder that keeps the thread: %v", err) + sess.log.add(DirOut, nil, "one") + sess.log.add(DirOut, nil, "two") + oldFrame := make(chan struct{}, 2) + release := make(chan struct{}) + oldCtx, cancelOld := context.WithCancel(context.Background()) + defer cancelOld() + s.SetConnectionSink(oldCtx, func(string, any) error { + oldFrame <- struct{}{} + <-release + return nil + }) + result := make(chan error, 1) + go func() { + _, err := s.Replay(oldCtx, tabParams("tab")) + result <- err + }() + select { + case <-oldFrame: + case <-time.After(2 * time.Second): + close(release) + t.Fatal("old replay did not reach its first frame") + } + var fresh []LinePayload + newCtx, cancelNew := context.WithCancel(context.Background()) + defer cancelNew() + s.SetConnectionSink(newCtx, func(_ string, payload any) error { + fresh = append(fresh, payload.(LinePayload)) + return nil + }) + close(release) + select { + case err := <-result: + if !errors.Is(err, context.Canceled) { + t.Errorf("old replay returned %v", err) + } + case <-time.After(2 * time.Second): + t.Fatal("old replay did not finish after cancellation") } - if got := stage(StageThreadParams{ThreadID: "1b6a3c2e-6f1e-4a59-9d0e-2f6d7c1a8b90", From: from, To: to}); got.Found { - t.Fatalf("a thread not there = %+v", got) + if len(fresh) != 0 { + t.Fatalf("new connection got %d old replay frames", len(fresh)) } - raw, _ := json.Marshal(StageThreadParams{ThreadID: "../../etc", From: from, To: to}) - if _, err := service.StageThread(context.Background(), raw); err == nil { - t.Fatal("StageThread took a path for a thread id") + reply, err := s.Replay(newCtx, tabParams("tab")) + if err != nil || reply.(ReplayResult).LastSeq != 2 || len(fresh) != 2 || fresh[0].Text != "one" || fresh[1].Text != "two" { + t.Fatalf("new replay got %+v, %v and %+v", reply, err, fresh) } } -func TestStageThreadBringsTheThreadsItWasForkedFrom(t *testing.T) { - service := New() - meta := func(id, parent string) string { - payload := map[string]string{"id": id} - if parent != "" { - payload["forked_from_id"] = parent - } - line, _ := json.Marshal(map[string]any{"type": "session_meta", "payload": payload}) - return string(line) + "\n" - } - rollout := func(id string) string { return "rollout-2026-11-11T10-00-00-" + id + ".jsonl" } - first := "01a1c8c5-b25a-7e12-808a-07e9fcbe1a9e" - second := "01a1c8ca-8ced-7802-8567-1af7c18bc127" - third := "01a1c9d0-1111-7222-8333-444455556666" - from, to := t.TempDir(), t.TempDir() - // The profile keeps the first thread, a copy of the second it forked - // from another profile, and the third, forked from that copy in turn. - writeTestFile(t, filepath.Join(from, "archived_sessions", rollout(first)), meta(first, "")) - writeTestFile(t, filepath.Join(from, "sessions", stagedDir, rollout(second)), meta(second, first)) - writeTestFile(t, filepath.Join(from, "sessions", "2026", "11", "11", rollout(third)), meta(third, second)) - - raw, _ := json.Marshal(StageThreadParams{ThreadID: third, From: from, To: to}) - if result, err := service.StageThread(context.Background(), raw); err != nil || !result.(StageThreadResult).Found { - t.Fatalf("StageThread = %+v, %v", result, err) - } - for _, id := range []string{first, second, third} { - if _, err := os.Stat(filepath.Join(to, "sessions", stagedDir, rollout(id))); err != nil { - t.Fatalf("%s not staged: %v", id, err) - } - } - - // A thread the profile keeps itself is not staged, nor those before it. - other := t.TempDir() - writeTestFile(t, filepath.Join(other, "sessions", "2026", "11", "11", rollout(second)), meta(second, first)) - raw, _ = json.Marshal(StageThreadParams{ThreadID: third, From: from, To: other}) - if _, err := service.StageThread(context.Background(), raw); err != nil { +// TestReplayStopsWhenItsConnectionCancels keeps the sink installed while +// canceling the host after its first frame. +func TestReplayStopsWhenItsConnectionCancels(t *testing.T) { + s := New() + sess, err := s.sessionFor(tabKey{workspaceID: "ws", tabID: "tab"}, "") + if err != nil { t.Fatal(err) } - entries, _ := os.ReadDir(filepath.Join(other, "sessions", stagedDir)) - if len(entries) != 1 || entries[0].Name() != rollout(third) { - t.Fatalf("staged in a profile that keeps the second thread: %v", entries) + sess.log.add(DirOut, nil, "one") + sess.log.add(DirOut, nil, "two") + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + frames := 0 + s.SetConnectionSink(ctx, func(string, any) error { + frames++ + cancel() + return nil + }) + if _, err := s.Replay(ctx, tabParams("tab")); !errors.Is(err, context.Canceled) || frames != 1 { + t.Fatalf("replay emitted %d frames, returned %v", frames, err) } } -func TestReadJSONLinesKeepsTheEnd(t *testing.T) { - path := filepath.Join(t.TempDir(), "t.jsonl") - var b strings.Builder - for i := 0; i < 100; i++ { - fmt.Fprintf(&b, `{"n":%d}`+"\n", i) - } - if err := os.WriteFile(path, []byte(b.String()), 0o644); err != nil { - t.Fatal(err) - } - lines, truncated, err := readJSONLines(path, 50) - if err != nil || !truncated { - t.Fatalf("err %v, truncated %v", err, truncated) - } - if len(lines) == 0 || string(lines[len(lines)-1]) != `{"n":99}` { - t.Fatalf("lines = %s", lines) - } - for _, line := range lines { - if !json.Valid(line) { - t.Fatalf("partial line %s", line) - } +// TestChatStderrTailMasksCredentials checks what exit events and logs will see. +func TestChatStderrTailMasksCredentials(t *testing.T) { + p := &process{} + p.readStderr(strings.NewReader("connection to https://example:fake-pass@host.test failed\nAuthorization: Bearer fake-auth\nAPP_SECRET=fake-env\n")) + got := p.stderr() + if strings.Contains(got, "fake-") || !strings.Contains(got, "@host.test failed") { + t.Fatalf("unexpected child diagnostic %q", got) } } diff --git a/internal/chat/stage.go b/internal/chat/stage.go index 8da0c3f29..801efc400 100644 --- a/internal/chat/stage.go +++ b/internal/chat/stage.go @@ -6,6 +6,9 @@ import ( "io" "os" "path/filepath" + "sync" + + "github.com/nexus-code/nexus-code/internal/agentpaths" ) // stagedDir is where a thread another Codex profile keeps is put for this @@ -22,6 +25,10 @@ const stagedDir = "nexus-staged" // followed, so a lineage that loops ends. const stagedLineage = 64 +// stagedPublishMu keeps the final length check and replacement together +// when this agent stages the same rollout for concurrent forks. +var stagedPublishMu sync.Mutex + // StageThread puts a copy of a Codex thread another profile keeps where the // tab's profile's program finds it, for thread/fork, with the threads it was // forked from that the profile does not keep: Codex reads a fork's history @@ -112,21 +119,37 @@ func copyGrown(from, to string) error { return copyFile(from, to) } +// copyFile writes a private sibling file before replacing the rollout a fork +// may still be reading. A failed copy leaves the previous rollout intact. func copyFile(from, to string) error { in, err := os.Open(from) if err != nil { return err } defer in.Close() - out, err := os.OpenFile(to, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600) + out, err := os.CreateTemp(filepath.Dir(to), ".nexus-rollout-*") + if err != nil { + return err + } + defer os.Remove(out.Name()) + defer out.Close() + n, err := io.Copy(out, in) if err != nil { return err } - if _, err := io.Copy(out, in); err != nil { - out.Close() + if err := out.Sync(); err != nil { return err } - return out.Close() + if err := out.Close(); err != nil { + return err + } + + stagedPublishMu.Lock() + defer stagedPublishMu.Unlock() + if copied, err := os.Stat(to); err == nil && copied.Size() >= n { + return nil + } + return os.Rename(out.Name(), to) } // rolloutPath is the file Codex keeps a thread in, "" when none is found: @@ -148,7 +171,7 @@ func rolloutPath(home, threadID string) string { // else CODEX_HOME as the login shell or the agent has it, else ~/.codex. func codexHome(given string) string { home, _ := os.UserHomeDir() - if dir := expandProfileDir(given, home); dir != "" { + if dir := agentpaths.ProfileEnvDir("CODEX_HOME", given); dir != "" { return dir } return defaultProfileDir("codex", home) diff --git a/internal/chat/stage_test.go b/internal/chat/stage_test.go new file mode 100644 index 000000000..952a83c51 --- /dev/null +++ b/internal/chat/stage_test.go @@ -0,0 +1,147 @@ +package chat + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "testing" +) + +// TestFailedStagedCopyKeepsExistingRollout covers a source that fails during +// reading after its open succeeds. The old rollout remains usable by forks. +func TestFailedStagedCopyKeepsExistingRollout(t *testing.T) { + dir := t.TempDir() + to := filepath.Join(dir, "copied.jsonl") + const old = "{\"type\":\"session_meta\"}\n" + writeTestFile(t, to, old) + // os.Open can open a directory, but it cannot provide rollout bytes. + if err := copyFile(t.TempDir(), to); err == nil { + t.Fatal("copying from a directory succeeded") + } + if got, err := os.ReadFile(to); err != nil || string(got) != old { + t.Fatalf("existing rollout changed: %q, %v", got, err) + } + if entries, err := os.ReadDir(dir); err != nil || len(entries) != 1 { + t.Fatalf("failed copy left temporary files: %v, %v", entries, err) + } +} + +func TestStageThreadPutsACopyWhereTheProfileFindsIt(t *testing.T) { + service := New() + stage := func(params StageThreadParams) StageThreadResult { + t.Helper() + raw, _ := json.Marshal(params) + result, err := service.StageThread(context.Background(), raw) + if err != nil { + t.Fatalf("StageThread(%+v): %v", params, err) + } + return result.(StageThreadResult) + } + id := "01a1c494-518d-7651-938d-00031c0ec9fb" + name := "rollout-2026-11-10T17-30-17-" + id + ".jsonl" + from, to := t.TempDir(), t.TempDir() + writeTestFile(t, filepath.Join(from, "sessions", "2026", "11", "10", name), `{"type":"session_meta"}`+"\n") + staged := filepath.Join(to, "sessions", stagedDir, name) + + if got := stage(StageThreadParams{ThreadID: id, From: from, To: to}); !got.Found { + t.Fatalf("stage = %+v", got) + } + if body, err := os.ReadFile(staged); err != nil || string(body) != `{"type":"session_meta"}`+"\n" { + t.Fatalf("staged copy = %q, %v", body, err) + } + // The copy is not one of the profile's own threads. + if got := rolloutPath(to, id); got != "" { + t.Fatalf("rolloutPath took the staged copy: %s", got) + } + // The copy stays once the thread is forked: the fork reads its history + // from it each time it resumes. + if got := stage(StageThreadParams{ThreadID: id, From: from, To: to}); !got.Found { + t.Fatalf("stage again = %+v", got) + } + if _, err := os.Stat(staged); err != nil { + t.Fatalf("the staged copy went: %v", err) + } + + // An archived thread is staged too: the fork unarchives nothing. + if err := os.RemoveAll(filepath.Join(from, "sessions")); err != nil { + t.Fatal(err) + } + if err := os.RemoveAll(filepath.Join(to, "sessions")); err != nil { + t.Fatal(err) + } + writeTestFile(t, filepath.Join(from, "archived_sessions", name), "{}\n") + if got := stage(StageThreadParams{ThreadID: id, From: from, To: to}); !got.Found { + t.Fatalf("archived stage = %+v", got) + } + if _, err := os.Stat(staged); err != nil { + t.Fatalf("archived thread not staged: %v", err) + } + + // A copy longer than the thread is not cut back: a fork made from it + // reads it to where it was forked. + writeTestFile(t, staged, "{}\n{}\n") + stage(StageThreadParams{ThreadID: id, From: from, To: to}) + if body, _ := os.ReadFile(staged); string(body) != "{}\n{}\n" { + t.Fatalf("a longer copy was cut back: %q", body) + } + + // Profiles of one folder need no copy; a thread not there is not found. + if got := stage(StageThreadParams{ThreadID: id, From: from, To: from}); !got.Found { + t.Fatalf("same folder = %+v", got) + } + if _, err := os.Stat(filepath.Join(from, "sessions", stagedDir)); !os.IsNotExist(err) { + t.Fatalf("a copy was staged in the folder that keeps the thread: %v", err) + } + if got := stage(StageThreadParams{ThreadID: "1b6a3c2e-6f1e-4a59-9d0e-2f6d7c1a8b90", From: from, To: to}); got.Found { + t.Fatalf("a thread not there = %+v", got) + } + raw, _ := json.Marshal(StageThreadParams{ThreadID: "../../etc", From: from, To: to}) + if _, err := service.StageThread(context.Background(), raw); err == nil { + t.Fatal("StageThread took a path for a thread id") + } +} + +func TestStageThreadBringsTheThreadsItWasForkedFrom(t *testing.T) { + service := New() + meta := func(id, parent string) string { + payload := map[string]string{"id": id} + if parent != "" { + payload["forked_from_id"] = parent + } + line, _ := json.Marshal(map[string]any{"type": "session_meta", "payload": payload}) + return string(line) + "\n" + } + rollout := func(id string) string { return "rollout-2026-11-11T10-00-00-" + id + ".jsonl" } + first := "01a1c8c5-b25a-7e12-808a-07e9fcbe1a9e" + second := "01a1c8ca-8ced-7802-8567-1af7c18bc127" + third := "01a1c9d0-1111-7222-8333-444455556666" + from, to := t.TempDir(), t.TempDir() + // The profile keeps the first thread, a copy of the second it forked + // from another profile, and the third, forked from that copy in turn. + writeTestFile(t, filepath.Join(from, "archived_sessions", rollout(first)), meta(first, "")) + writeTestFile(t, filepath.Join(from, "sessions", stagedDir, rollout(second)), meta(second, first)) + writeTestFile(t, filepath.Join(from, "sessions", "2026", "11", "11", rollout(third)), meta(third, second)) + + raw, _ := json.Marshal(StageThreadParams{ThreadID: third, From: from, To: to}) + if result, err := service.StageThread(context.Background(), raw); err != nil || !result.(StageThreadResult).Found { + t.Fatalf("StageThread = %+v, %v", result, err) + } + for _, id := range []string{first, second, third} { + if _, err := os.Stat(filepath.Join(to, "sessions", stagedDir, rollout(id))); err != nil { + t.Fatalf("%s not staged: %v", id, err) + } + } + + // A thread the profile keeps itself is not staged, nor those before it. + other := t.TempDir() + writeTestFile(t, filepath.Join(other, "sessions", "2026", "11", "11", rollout(second)), meta(second, first)) + raw, _ = json.Marshal(StageThreadParams{ThreadID: third, From: from, To: other}) + if _, err := service.StageThread(context.Background(), raw); err != nil { + t.Fatal(err) + } + entries, _ := os.ReadDir(filepath.Join(other, "sessions", stagedDir)) + if len(entries) != 1 || entries[0].Name() != rollout(third) { + t.Fatalf("staged in a profile that keeps the second thread: %v", entries) + } +} diff --git a/internal/chat/stdin.go b/internal/chat/stdin.go new file mode 100644 index 000000000..469c05ccd --- /dev/null +++ b/internal/chat/stdin.go @@ -0,0 +1,12 @@ +package chat + +import ( + "context" + "io" +) + +// stdinWriter stops an uncompleted frame when its request is cancelled. +type stdinWriter interface { + io.Closer + WriteContext(context.Context, []byte) (int, error) +} diff --git a/internal/chat/stdin_test.go b/internal/chat/stdin_test.go new file mode 100644 index 000000000..060cf260c --- /dev/null +++ b/internal/chat/stdin_test.go @@ -0,0 +1,140 @@ +package chat + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "testing" + "time" + + "github.com/nexus-code/nexus-code/internal/proto" +) + +// TestChatStdinInterruptsAnInFlightWrite holds the actual child pipe open +// without draining it after the first byte. +func TestChatStdinInterruptsAnInFlightWrite(t *testing.T) { + writer, reader, err := newChatStdin(fmt.Sprintf("%d-test-stdin", os.Getpid())) + if err != nil { + t.Fatal(err) + } + defer writer.Close() + defer reader.Close() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + type outcome struct { + n int + err error + } + data := bytes.Repeat([]byte("x"), 4<<20) + done := make(chan outcome, 1) + go func() { + n, err := writer.WriteContext(ctx, data) + done <- outcome{n, err} + }() + read := make(chan error, 1) + go func() { + _, err := io.ReadFull(reader, make([]byte, 1)) + read <- err + }() + select { + case err := <-read: + if err != nil { + t.Fatal(err) + } + case <-time.After(3 * time.Second): + t.Fatal("child stdin did not receive the first byte") + } + cancel() + select { + case got := <-done: + if !errors.Is(got.err, context.Canceled) || got.n <= 0 || got.n >= len(data) { + t.Fatalf("interrupted pipe returned %d, %v", got.n, got.err) + } + case <-time.After(3 * time.Second): + t.Fatal("canceled writer waited for the child to read again") + } +} + +// blockedFrame simulates a child that has received either no bytes or a +// prefix when the request leaves. +type blockedFrame struct { + entered chan struct{} + written int + closed bool +} + +func (w *blockedFrame) Close() error { + w.closed = true + return nil +} + +func (w *blockedFrame) WriteContext(ctx context.Context, data []byte) (int, error) { + if w.closed { + return 0, io.ErrClosedPipe + } + if w.entered != nil { + close(w.entered) + w.entered = nil + <-ctx.Done() + return w.written, ctx.Err() + } + return len(data), nil +} + +// TestCanceledStdinFrameOnlyClosesAfterAPartialWrite covers the recovery +// contract the renderer uses to decide whether a request can be resent. +func TestCanceledStdinFrameOnlyClosesAfterAPartialWrite(t *testing.T) { + for _, tc := range []struct { + name string + written int + code string + }{ + {"no bytes", 0, "chat.stdin-write-failed"}, + {"partial", 1, "chat.stdin-incomplete"}, + } { + t.Run(tc.name, func(t *testing.T) { + s := New() + sess, err := s.sessionFor(tabKey{workspaceID: "ws", tabID: "tab"}, "") + if err != nil { + t.Fatal(err) + } + entered := make(chan struct{}) + writer := &blockedFrame{entered: entered, written: tc.written} + sess.proc = &process{stdin: writer} + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + result := make(chan error, 1) + go func() { + _, err := s.Send(ctx, json.RawMessage(`{"workspaceId":"ws","tabId":"tab","message":{"method":"initialize"}}`)) + result <- err + }() + select { + case <-entered: + case <-time.After(2 * time.Second): + t.Fatal("send did not reach stdin") + } + cancel() + select { + case err := <-result: + var wireErr proto.CodedError + if !errors.As(err, &wireErr) || wireErr.Code != tc.code || writer.closed != (tc.written > 0) { + t.Fatalf("send result %v, pipe closed: %t", err, writer.closed) + } + case <-time.After(2 * time.Second): + t.Fatal("send did not observe cancellation") + } + if sess.proc.codexInitialized { + t.Fatal("an unwritten initialize was recorded as delivered") + } + if tc.written == 0 { + if err := send(t, s, "tab", "next frame"); err != nil { + t.Fatalf("unwritten cancellation prevented a later send: %v", err) + } + } + }) + } +} diff --git a/internal/chat/stdin_unix.go b/internal/chat/stdin_unix.go new file mode 100644 index 000000000..2c1153bac --- /dev/null +++ b/internal/chat/stdin_unix.go @@ -0,0 +1,46 @@ +//go:build darwin || linux + +package chat + +import ( + "context" + "errors" + "os" + "time" +) + +type deadlineStdin struct{ *os.File } + +// newChatStdin creates a pollable write end and the child's ordinary stdin. +func newChatStdin(_ string) (stdinWriter, *os.File, error) { + child, parent, err := os.Pipe() + if err != nil { + return nil, nil, err + } + if err := parent.SetWriteDeadline(time.Time{}); err != nil { + _ = parent.Close() + _ = child.Close() + return nil, nil, err + } + return deadlineStdin{parent}, child, nil +} + +func (w deadlineStdin) WriteContext(ctx context.Context, data []byte) (int, error) { + if err := ctx.Err(); err != nil { + return 0, err + } + finished := make(chan struct{}) + stop := context.AfterFunc(ctx, func() { + _ = w.SetWriteDeadline(time.Now()) + close(finished) + }) + n, err := w.Write(data) + if !stop() { + <-finished + _ = w.SetWriteDeadline(time.Time{}) + if errors.Is(err, os.ErrDeadlineExceeded) { + err = ctx.Err() + } + } + return n, err +} diff --git a/internal/chat/stdin_windows.go b/internal/chat/stdin_windows.go new file mode 100644 index 000000000..aa08baf8b --- /dev/null +++ b/internal/chat/stdin_windows.go @@ -0,0 +1,136 @@ +//go:build windows + +package chat + +import ( + "context" + "errors" + "io" + "os" + "runtime" + "sync" + "unsafe" + + "golang.org/x/sys/windows" +) + +// overlappedStdin owns the write end. The child's synchronous handle reads +// the same byte stream without needing to know about cancellation. +type overlappedStdin struct { + mu sync.Mutex + handle windows.Handle + closed context.Context + cancel context.CancelFunc +} + +// newChatStdin opens an overlapped write end because anonymous Windows pipes +// do not support write deadlines in Go. +func newChatStdin(generation string) (stdinWriter, *os.File, error) { + name, err := windows.UTF16PtrFromString(`\\.\pipe\nexus-chat-` + generation) + if err != nil { + return nil, nil, err + } + user, err := windows.GetCurrentProcessToken().GetTokenUser() + if err != nil { + return nil, nil, err + } + sd, err := windows.SecurityDescriptorFromString("D:P(A;;GA;;;" + user.User.Sid.String() + ")") + if err != nil { + return nil, nil, err + } + security := windows.SecurityAttributes{SecurityDescriptor: sd} + security.Length = uint32(unsafe.Sizeof(security)) + handle, err := windows.CreateNamedPipe(name, + windows.PIPE_ACCESS_OUTBOUND|windows.FILE_FLAG_OVERLAPPED|windows.FILE_FLAG_FIRST_PIPE_INSTANCE, + windows.PIPE_TYPE_MESSAGE|windows.PIPE_REJECT_REMOTE_CLIENTS, 1, 64<<10, 0, 0, &security) + if err != nil { + return nil, nil, err + } + child, err := windows.CreateFile(name, windows.GENERIC_READ, 0, nil, windows.OPEN_EXISTING, 0, 0) + if err != nil { + _ = windows.CloseHandle(handle) + return nil, nil, err + } + // CreateFile already connected the client; its default read mode is byte. + closed, cancel := context.WithCancel(context.Background()) + return &overlappedStdin{handle: handle, closed: closed, cancel: cancel}, os.NewFile(uintptr(child), "chat stdin"), nil +} + +// Close cancels and joins an active write before releasing its handle. +func (w *overlappedStdin) Close() error { + w.cancel() + w.mu.Lock() + defer w.mu.Unlock() + if w.handle == 0 { + return nil + } + err := windows.CloseHandle(w.handle) + w.handle = 0 + return err +} + +func (w *overlappedStdin) WriteContext(request context.Context, data []byte) (int, error) { + w.mu.Lock() + defer w.mu.Unlock() + if w.closed.Err() != nil { + return 0, io.ErrClosedPipe + } + ctx, cancel := context.WithCancel(request) + defer cancel() + stopClosing := context.AfterFunc(w.closed, cancel) + defer stopClosing() + + event, err := windows.CreateEvent(nil, 1, 0, nil) + if err != nil { + return 0, err + } + defer windows.CloseHandle(event) + // A message fits inside the pipe's quota. Counting completed messages + // preserves the distinction between an untouched and a partial frame. + written := 0 + for len(data) != 0 { + chunk := data[:min(len(data), 16<<10)] + n, err := w.writeChunk(ctx, event, chunk) + written += n + if err != nil { + return written, err + } + if n != len(chunk) { + return written, io.ErrShortWrite + } + data = data[n:] + } + return written, nil +} + +// writeChunk cancels the specific operation and waits for kernel completion +// before its buffer and OVERLAPPED structure can be reused. +func (w *overlappedStdin) writeChunk(ctx context.Context, event windows.Handle, data []byte) (int, error) { + if err := ctx.Err(); err != nil { + return 0, err + } + op := &windows.Overlapped{HEvent: event} + var n uint32 + err := windows.WriteFile(w.handle, data, &n, op) + if err != nil && !errors.Is(err, windows.ERROR_IO_PENDING) { + return int(n), err + } + if err == nil { + err = windows.GetOverlappedResult(w.handle, op, &n, false) + } else { + finished := make(chan struct{}) + stop := context.AfterFunc(ctx, func() { + _ = windows.CancelIoEx(w.handle, op) + close(finished) + }) + err = windows.GetOverlappedResult(w.handle, op, &n, true) + if !stop() { + <-finished + } + } + runtime.KeepAlive(data) + if errors.Is(err, windows.ERROR_OPERATION_ABORTED) && ctx.Err() != nil { + err = ctx.Err() + } + return int(n), err +} diff --git a/internal/chat/stdin_windows_test.go b/internal/chat/stdin_windows_test.go new file mode 100644 index 000000000..6bdbc9f48 --- /dev/null +++ b/internal/chat/stdin_windows_test.go @@ -0,0 +1,128 @@ +//go:build windows + +package chat + +import ( + "bytes" + "context" + "encoding/json" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" +) + +// TestWindowsChatStdinReachesNodeThroughCmd exercises libuv reading the +// inherited stdin handle through a batch launcher, including end of input. +func TestWindowsChatStdinReachesNodeThroughCmd(t *testing.T) { + if _, err := exec.LookPath("node"); err != nil { + t.Skip("node is not on PATH") + } + cmd, err := exec.LookPath("cmd.exe") + if err != nil { + t.Fatal(err) + } + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, "echo-input.cmd"), []byte("@echo off\r\nnode \"%~dp0echo-input.cjs\"\r\n"), 0o644); err != nil { + t.Fatal(err) + } + const script = ` +process.stdin.pipe(process.stdout, { end: false }); +process.stdin.on("end", () => process.stdout.write('{"eof":true}\n')); +` + if err := os.WriteFile(filepath.Join(dir, "echo-input.cjs"), []byte(script), 0o644); err != nil { + t.Fatal(err) + } + + service := New() + defer service.Close() + rec := newRecorder() + lastEcho := make(chan struct{}, 1) + service.SetEventSink(func(event string, payload any) error { + if line, ok := payload.(LinePayload); ok && line.Dir == DirOut && string(line.Message) == `"three"` { + select { + case lastEcho <- struct{}{}: + default: + } + } + return rec.sink(event, payload) + }) + // A whole run takes well under a second, but the first cmd and Node start + // on a fresh Windows runner has taken over 10 s, so allow 30 s here alone. + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + key := tabKey{workspaceID: "ws", tabID: "windows-node-stdin"} + start, err := json.Marshal(StartParams{ + WorkspaceID: key.workspaceID, + TabID: key.tabID, + Cwd: dir, + Program: cmd, + Args: []string{"/d", "/c", "echo-input.cmd"}, + }) + if err != nil { + t.Fatal(err) + } + if _, err := service.Start(ctx, start); err != nil { + t.Fatal(err) + } + proc := service.lookup(key).running() + if proc == nil { + t.Fatal("launcher exited before receiving input") + } + + var want []json.RawMessage + for _, line := range []string{strings.Repeat("x", 1<<20), "one", "두 번째", "three"} { + message, err := json.Marshal(line) + if err != nil { + t.Fatal(err) + } + want = append(want, message) + params, err := json.Marshal(SendParams{WorkspaceID: key.workspaceID, TabID: key.tabID, Message: message}) + if err != nil { + t.Fatal(err) + } + if _, err := service.Send(ctx, params); err != nil { + t.Fatal(err) + } + } + select { + case <-lastEcho: + case <-proc.done: + t.Fatal("Node exited before echoing the input") + case <-ctx.Done(): + t.Fatal("Node did not echo the input") + } + + want = append(want, json.RawMessage(`{"eof":true}`)) + if err := proc.stdin.Close(); err != nil { + t.Fatal(err) + } + select { + case <-proc.done: + case <-ctx.Done(): + t.Fatal("Node did not exit after stdin EOF") + } + + rec.mu.Lock() + defer rec.mu.Unlock() + var got []json.RawMessage + for _, line := range rec.lines { + if line.Dir == DirOut { + got = append(got, line.Message) + } + } + if len(got) != len(want) { + t.Fatalf("Node echoed %d lines, want %d including EOF, exit: %+v", len(got), len(want), rec.exits) + } + for i, message := range got { + if !bytes.Equal(message, want[i]) { + t.Fatalf("echoed line %d differs from input (%d vs %d bytes)", i+1, len(message), len(want[i])) + } + } + if len(rec.exits) != 1 || rec.exits[0].Code == nil || *rec.exits[0].Code != 0 { + t.Fatalf("expected a successful exit, got %+v", rec.exits) + } +} diff --git a/internal/chat/testutil_test.go b/internal/chat/testutil_test.go new file mode 100644 index 000000000..65eb84bc0 --- /dev/null +++ b/internal/chat/testutil_test.go @@ -0,0 +1,17 @@ +package chat + +import ( + "os" + "path/filepath" + "testing" +) + +func writeTestFile(t *testing.T, path, body string) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(body), 0o644); err != nil { + t.Fatal(err) + } +} diff --git a/internal/chat/transcript.go b/internal/chat/transcript.go index 2f0f0f038..adc4f1824 100644 --- a/internal/chat/transcript.go +++ b/internal/chat/transcript.go @@ -11,6 +11,8 @@ import ( "path/filepath" "regexp" "strings" + + "github.com/nexus-code/nexus-code/internal/agentpaths" ) // sessionIDPattern is what a Claude Code session id looks like. Anything @@ -179,21 +181,10 @@ func newestFile(paths []string) string { // configDir resolves Claude Code's config directory on this host. func configDir(given string) string { home, _ := os.UserHomeDir() - given = strings.TrimSpace(given) - if given == "" { - given = os.Getenv("CLAUDE_CONFIG_DIR") - } - if given == "" { - if home == "" { - return "" - } - return filepath.Join(home, ".claude") - } - // A profile's folder, "~\…" too as one picked on Windows reads. - if dir := expandProfileDir(given, home); dir != "" { + if dir := agentpaths.ProfileEnvDir("CLAUDE_CONFIG_DIR", given); dir != "" { return dir } - return given + return defaultProfileDir("claude", home) } // readJSONLines reads a JSON-lines file, keeping its last lines within diff --git a/internal/chat/transcript_page.go b/internal/chat/transcript_page.go new file mode 100644 index 000000000..7d769fcbf --- /dev/null +++ b/internal/chat/transcript_page.go @@ -0,0 +1,545 @@ +package chat + +import ( + "bufio" + "bytes" + "context" + "crypto/hmac" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/binary" + "encoding/json" + "io" + "os" + "sync" + + "github.com/nexus-code/nexus-code/internal/proto" +) + +const ( + transcriptPageBytes = 512 * 1024 + transcriptPageTurns = 20 + transcriptPageOverhead = 448 + transcriptReadBlock = 64 * 1024 + transcriptAnchorBytes = 2048 + transcriptSnapshots = 128 +) + +type transcriptPageParams struct { + TranscriptParams + Cursor string `json:"cursor,omitempty"` + BeforeAnchor string `json:"beforeAnchor,omitempty"` + UpTo string `json:"upTo,omitempty"` + Before []string `json:"before,omitempty"` +} + +type transcriptPage struct { + Found bool `json:"found"` + Lines []json.RawMessage `json:"lines"` + Positions []int64 `json:"positions"` + Truncated bool `json:"truncated"` + Snapshot string `json:"snapshot,omitempty"` + Cursor string `json:"cursor,omitempty"` + BeforeAnchor string `json:"beforeAnchor,omitempty"` + Anchored bool `json:"anchored,omitempty"` + Fragment *transcriptFragment `json:"fragment,omitempty"` +} + +type transcriptFragment struct { + Position int64 `json:"position"` + Data []byte `json:"data"` + First bool `json:"first"` + Last bool `json:"last"` +} + +type transcriptSnapshot struct { + key [32]byte + session, dir, path string + info os.FileInfo + floor, end int64 + anchor [32]byte +} + +type transcriptSnapshotCache struct { + mu sync.Mutex + entries [transcriptSnapshots]*transcriptSnapshot + next int +} + +func (c *transcriptSnapshotCache) add(snapshot *transcriptSnapshot) { + c.mu.Lock() + defer c.mu.Unlock() + c.entries[c.next] = snapshot + c.next = (c.next + 1) % len(c.entries) +} + +func (s *transcriptSnapshot) id() string { + return base64.RawURLEncoding.EncodeToString(s.key[:16]) +} + +// The cursor carries a signed, exclusive file position and the start of a +// large row if the preceding page stopped inside it. The file identity and +// profile stay in the agent's bounded snapshot cache. +func (s *transcriptSnapshot) cursor(pos, fragment int64, anchor [32]byte) string { + var token [80]byte + copy(token[:16], s.key[:16]) + binary.BigEndian.PutUint64(token[16:24], uint64(pos)) + binary.BigEndian.PutUint64(token[24:32], uint64(fragment)) + copy(token[32:64], anchor[:]) + copy(token[64:], s.sign(token[:64])) + return base64.RawURLEncoding.EncodeToString(token[:]) +} + +func (s *transcriptSnapshot) sign(data []byte) []byte { + mac := hmac.New(sha256.New, s.key[16:]) + _, _ = mac.Write(data) + return mac.Sum(nil)[:16] +} + +func (c *transcriptSnapshotCache) decode(encoded string) (*transcriptSnapshot, int64, int64, [32]byte) { + var anchor [32]byte + if len(encoded) != base64.RawURLEncoding.EncodedLen(80) { + return nil, 0, 0, anchor + } + token, err := base64.RawURLEncoding.DecodeString(encoded) + if err != nil || len(token) != 80 { + return nil, 0, 0, anchor + } + c.mu.Lock() + defer c.mu.Unlock() + for _, snapshot := range c.entries { + if snapshot != nil && bytes.Equal(snapshot.key[:16], token[:16]) && hmac.Equal(snapshot.sign(token[:64]), token[64:]) { + copy(anchor[:], token[32:64]) + return snapshot, int64(binary.BigEndian.Uint64(token[16:24])), int64(binary.BigEndian.Uint64(token[24:32])), anchor + } + } + return nil, 0, 0, anchor +} + +var errStaleTranscriptCursor = proto.NewError("chat.transcript-cursor-stale", "the transcript changed or its snapshot expired") + +// TranscriptPage reads a bounded page of the same retained transcript window +// as Transcript. Append-only changes remain beyond the snapshot's fixed end. +func (s *Service) TranscriptPage(ctx context.Context, raw json.RawMessage) (any, error) { + var p transcriptPageParams + if err := decodeParams(raw, &p, "chat.transcriptPage params must include sessionId"); err != nil { + return nil, err + } + if !sessionIDPattern.MatchString(p.SessionID) || + (p.Cursor != "" && p.BeforeAnchor != "") || + ((p.Cursor != "" || p.BeforeAnchor != "") && (len(p.Before) > 0 || p.UpTo != "")) { + return nil, protocolError("invalid chat.transcriptPage params") + } + page, err := s.readTranscriptPage(ctx, p, transcriptCapBytes, transcriptPageBytes) + if err != nil { + if ctx.Err() != nil { + return nil, ctx.Err() + } + if err == errStaleTranscriptCursor { + return nil, err + } + return nil, requestFailed("chat.transcriptPage: %s", err) + } + return page, nil +} + +func (s *Service) readTranscriptPage(ctx context.Context, p transcriptPageParams, retention int64, budget int) (transcriptPage, error) { + page := transcriptPage{Lines: []json.RawMessage{}, Positions: []int64{}} + if err := ctx.Err(); err != nil { + return page, err + } + dir := configDir(p.ConfigDir) + path := transcriptPath(dir, p.SessionID) + var snapshot *transcriptSnapshot + var pos, fragment int64 + var boundary [32]byte + if p.Cursor != "" { + snapshot, pos, fragment, boundary = s.transcriptPages.decode(p.Cursor) + if snapshot == nil || snapshot.session != p.SessionID || snapshot.dir != dir || snapshot.path != path { + return page, errStaleTranscriptCursor + } + } + if path == "" { + return page, nil + } + file, err := os.Open(path) + if err != nil { + if snapshot != nil && os.IsNotExist(err) { + return page, errStaleTranscriptCursor + } + return page, err + } + defer file.Close() + info, err := file.Stat() + if err != nil { + return page, err + } + if snapshot == nil { + snapshot = &transcriptSnapshot{session: p.SessionID, dir: dir, path: path, info: info, floor: max(0, info.Size()-retention), end: info.Size()} + if _, err := rand.Read(snapshot.key[:]); err != nil { + return page, err + } + snapshot.anchor, err = snapshotAnchor(file, snapshot.floor, info.Size()) + if err != nil { + return page, err + } + if p.BeforeAnchor != "" { + var matched bool + snapshot.end, matched, err = transcriptEndBeforeAnchor(file, snapshot, p.BeforeAnchor) + if err != nil || !matched { + return page, err + } + page.Anchored = true + } else if p.UpTo != "" || len(p.Before) > 0 { + snapshot.end, err = transcriptCutEnd(ctx, file, snapshot, p) + if err != nil { + return page, err + } + } + pos = snapshot.end + } else { + if err := snapshot.check(file, info); err != nil { + return page, err + } + actual, err := cursorAnchor(file, snapshot, pos) + if err != nil || actual != boundary { + return page, errStaleTranscriptCursor + } + } + page.Found = true + page.Truncated = snapshot.floor > 0 + page.Snapshot = snapshot.id() + scanner := transcriptReverseReader{ctx: ctx, file: file, floor: snapshot.floor} + if fragment >= 0 && p.Cursor != "" { + if pos, fragment, err = page.readFragment(&scanner, pos, fragment, budget, false); err != nil { + return page, err + } + } else { + if pos, fragment, err = page.readLines(&scanner, pos, budget); err != nil { + return page, err + } + } + if err := ctx.Err(); err != nil { + return page, err + } + current, err := os.Stat(path) + if os.IsNotExist(err) { + return page, errStaleTranscriptCursor + } + if err != nil { + return page, err + } + if err := snapshot.check(file, current); err != nil { + return page, err + } + if pos > snapshot.floor { + boundary, err := cursorAnchor(file, snapshot, pos) + if err != nil { + return page, err + } + page.Cursor = snapshot.cursor(pos, fragment, boundary) + if fragment < 0 { + page.BeforeAnchor, err = transcriptBeforeAnchor(file, pos, snapshot.end) + if err != nil { + return page, err + } + } + if p.Cursor == "" { + s.transcriptPages.add(snapshot) + } + } + return page, nil +} + +func (s *transcriptSnapshot) check(file *os.File, info os.FileInfo) error { + if !os.SameFile(s.info, info) || info.Size() < s.info.Size() || + (info.Size() == s.info.Size() && info.ModTime() != s.info.ModTime()) { + return errStaleTranscriptCursor + } + anchor, err := snapshotAnchor(file, s.floor, s.info.Size()) + if err != nil || anchor != s.anchor { + return errStaleTranscriptCursor + } + return nil +} + +func snapshotAnchor(file *os.File, floor, end int64) ([32]byte, error) { + hash := sha256.New() + for _, offset := range []int64{floor, max(floor, end-transcriptAnchorBytes)} { + var buf [transcriptAnchorBytes]byte + part := buf[:min(transcriptAnchorBytes, end-offset)] + if _, err := file.ReadAt(part, offset); err != nil { + return [32]byte{}, err + } + _, _ = hash.Write(part) + } + return [32]byte(hash.Sum(nil)), nil +} + +func cursorAnchor(file *os.File, snapshot *transcriptSnapshot, pos int64) ([32]byte, error) { + var buf [transcriptAnchorBytes]byte + start := max(snapshot.floor, pos-transcriptAnchorBytes/2) + part := buf[:min(transcriptAnchorBytes, snapshot.end-start)] + _, err := file.ReadAt(part, start) + return sha256.Sum256(part), err +} + +// A finished page can be resumed across agent lifetimes. Fingerprint bytes +// on the displayed side of its exclusive boundary so an append leaves it +// valid. The new agent still verifies that boundary against the session file. +const transcriptBeforeAnchorSize = 8 + 2 + sha256.Size + +func transcriptBeforeAnchor(file *os.File, pos, end int64) (string, error) { + var block [transcriptAnchorBytes]byte + sample := block[:min(transcriptAnchorBytes, end-pos)] + if _, err := file.ReadAt(sample, pos); err != nil { + return "", err + } + var token [transcriptBeforeAnchorSize]byte + binary.BigEndian.PutUint64(token[:8], uint64(pos)) + binary.BigEndian.PutUint16(token[8:10], uint16(len(sample))) + digest := sha256.Sum256(sample) + copy(token[10:], digest[:]) + return base64.RawURLEncoding.EncodeToString(token[:]), nil +} + +func transcriptEndBeforeAnchor(file *os.File, snapshot *transcriptSnapshot, anchor string) (int64, bool, error) { + if len(anchor) != base64.RawURLEncoding.EncodedLen(transcriptBeforeAnchorSize) { + return 0, false, nil + } + token, err := base64.RawURLEncoding.DecodeString(anchor) + if err != nil || len(token) != transcriptBeforeAnchorSize { + return 0, false, nil + } + pos := int64(binary.BigEndian.Uint64(token[:8])) + length := int64(binary.BigEndian.Uint16(token[8:10])) + if pos <= 0 || pos < snapshot.floor || pos >= snapshot.end || + length <= 0 || length > transcriptAnchorBytes || length > snapshot.end-pos { + return 0, false, nil + } + var block [transcriptAnchorBytes + 1]byte + if _, err := file.ReadAt(block[:length+1], pos-1); err != nil { + return 0, false, err + } + hash := sha256.Sum256(block[1 : length+1]) + return pos, block[0] == '\n' && bytes.Equal(hash[:], token[10:]), nil +} + +// A cut uses the first matching UUID in the retained window, as the +// renderer's full-transcript cut did. It does not transfer the scan. +func transcriptCutEnd(ctx context.Context, file *os.File, snapshot *transcriptSnapshot, p transcriptPageParams) (int64, error) { + reader := bufio.NewReaderSize(io.NewSectionReader(file, snapshot.floor, snapshot.end-snapshot.floor), transcriptReadBlock) + before := make(map[string]bool, len(p.Before)) + for _, uuid := range p.Before { + before[uuid] = true + } + pos := snapshot.floor + for pos < snapshot.end { + start := pos + var line []byte + for { + if err := ctx.Err(); err != nil { + return 0, err + } + chunk, err := reader.ReadSlice('\n') + pos += int64(len(chunk)) + if start != snapshot.floor || snapshot.floor == 0 { + line = append(line, chunk...) + } + if err != bufio.ErrBufferFull { + if err != nil && err != io.EOF { + return 0, err + } + break + } + } + var entry struct { + UUID string `json:"uuid"` + Attachment struct { + Type string `json:"type"` + Source *string `json:"source_uuid"` + } `json:"attachment"` + } + if json.Unmarshal(line, &entry) != nil { + continue + } + if entry.Attachment.Type == "queued_command" && entry.Attachment.Source != nil { + entry.UUID = *entry.Attachment.Source + } + if before[entry.UUID] { + return start, nil + } + if p.UpTo != "" && p.UpTo == entry.UUID { + return pos, nil + } + } + return snapshot.end, nil +} + +type transcriptReverseReader struct { + ctx context.Context + file *os.File + floor, blockAt int64 + block []byte +} + +func (r *transcriptReverseReader) blockBefore(pos int64) ([]byte, error) { + if err := r.ctx.Err(); err != nil { + return nil, err + } + if len(r.block) == 0 || pos <= r.blockAt || pos > r.blockAt+int64(len(r.block)) { + r.blockAt = max(r.floor, pos-transcriptReadBlock) + if r.block == nil { + r.block = make([]byte, transcriptReadBlock) + } + r.block = r.block[:pos-r.blockAt] + if _, err := r.file.ReadAt(r.block, r.blockAt); err != nil { + return nil, err + } + } + return r.block[:pos-r.blockAt], nil +} + +func (r *transcriptReverseReader) previous(pos int64) (int64, int64, error) { + block, err := r.blockBefore(pos) + if err != nil { + return 0, 0, err + } + end := pos + if block[len(block)-1] == '\n' { + end-- + } + for at := end; at > r.floor; at = r.blockAt { + block, err := r.blockBefore(at) + if err != nil { + return 0, 0, err + } + if lf := bytes.LastIndexByte(block, '\n'); lf >= 0 { + return r.blockAt + int64(lf) + 1, end, nil + } + } + return r.floor, end, nil +} + +func (r *transcriptReverseReader) read(start, end int64) ([]byte, error) { + if start >= r.blockAt && end <= r.blockAt+int64(len(r.block)) { + return r.block[start-r.blockAt : end-r.blockAt], nil + } + data := make([]byte, end-start) + for pos := start; pos < end; pos += transcriptReadBlock { + if err := r.ctx.Err(); err != nil { + return nil, err + } + part := data[pos-start : min(end, pos+transcriptReadBlock)-start] + if _, err := r.file.ReadAt(part, pos); err != nil { + return nil, err + } + } + return data, nil +} + +func (p *transcriptPage) readLines(r *transcriptReverseReader, pos int64, budget int) (int64, int64, error) { + // Allow space for the RPC envelope, cursor and per-row positions. + remaining := budget - transcriptPageOverhead + turns, turnRows, turnPos := 0, 0, int64(0) + for pos > r.floor { + start, end, err := r.previous(pos) + if err != nil { + return 0, 0, err + } + if start == r.floor && r.floor != 0 { + pos = r.floor + break + } + line, err := r.read(start, end) + if err != nil { + return 0, 0, err + } + line = bytes.TrimSpace(line) + if !json.Valid(line) { + pos = start + continue + } + cost := len(line) + 24 + var wire []byte + if cost <= remaining { + wire, err = json.Marshal(json.RawMessage(line)) + if err != nil { + return 0, 0, err + } + cost = len(wire) + 24 + } + if cost > remaining { + if len(p.Lines) == 0 { + return p.readFragment(r, end, start, budget, true) + } + if turnRows > 0 { + p.Lines = p.Lines[:turnRows] + p.Positions = p.Positions[:turnRows] + pos = turnPos + } + break + } + remaining -= cost + p.Lines = append(p.Lines, wire) + p.Positions = append(p.Positions, start) + pos = start + if transcriptTurnStart(line) { + turns++ + turnRows, turnPos = len(p.Lines), pos + if turns == transcriptPageTurns { + break + } + } + } + for i, j := 0, len(p.Lines)-1; i < j; i, j = i+1, j-1 { + p.Lines[i], p.Lines[j] = p.Lines[j], p.Lines[i] + p.Positions[i], p.Positions[j] = p.Positions[j], p.Positions[i] + } + return pos, -1, nil +} + +func (p *transcriptPage) readFragment(r *transcriptReverseReader, end, start int64, budget int, last bool) (int64, int64, error) { + // Base64 keeps a cut through a UTF-8 character or JSON escape reversible. + pos := max(start, end-int64((budget-transcriptPageOverhead)/4*3)) + data, err := r.read(pos, end) + if err != nil { + return 0, 0, err + } + p.Fragment = &transcriptFragment{Position: start, Data: bytes.Clone(data), First: pos == start, Last: last} + if pos == start { + return pos, -1, nil + } + return pos, start, nil +} + +func transcriptTurnStart(line []byte) bool { + var row struct { + Type string `json:"type"` + IsSidechain bool `json:"isSidechain"` + IsMeta bool `json:"isMeta"` + PromptSource string `json:"promptSource"` + Message struct { + Content json.RawMessage `json:"content"` + } `json:"message"` + } + if json.Unmarshal(line, &row) != nil || row.Type != "user" || row.IsSidechain || row.IsMeta || row.PromptSource == "system" || len(row.Message.Content) == 0 { + return false + } + if row.Message.Content[0] == '"' { + return true + } + var blocks []struct { + Type string `json:"type"` + } + if json.Unmarshal(row.Message.Content, &blocks) != nil || len(blocks) == 0 { + return false + } + for _, block := range blocks { + if block.Type == "tool_result" { + return false + } + } + return true +} diff --git a/internal/chat/transcript_page_test.go b/internal/chat/transcript_page_test.go new file mode 100644 index 000000000..aeb155147 --- /dev/null +++ b/internal/chat/transcript_page_test.go @@ -0,0 +1,182 @@ +package chat + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/nexus-code/nexus-code/internal/proto" +) + +func TestTranscriptPages(t *testing.T) { + const budget = 768 + const session = "session-for-test" + line := func(uuid, content string) string { + encoded, _ := json.Marshal(content) + return `{"type":"user","uuid":"` + uuid + `","message":{"content":` + string(encoded) + `}}` + } + old := line("old", "처음") + big := line("large", strings.Repeat("한<", 150)) + tip := line("recent", "최근") + base := old + "\r\n" + big + "\r\n" + tip + "\r\n" + withPartial := base + `{"type":` + + for _, tc := range []struct { + name, input, change string + retention int64 + upTo string + before []string + want []string + stale bool + }{ + {name: "UTF-8 CRLF large row and partial tail", input: withPartial, want: []string{old, big, tip}}, + {name: "complete row at EOF", input: strings.TrimSuffix(base, "\r\n"), want: []string{old, big, tip}}, + {name: "append preserves the old end", input: withPartial, change: "append", want: []string{old, big, tip}}, + {name: "retention discards the first fragment", input: base, retention: int64(len(base) - len(old)/2), want: []string{big, tip}}, + {name: "fork ends at the first inclusive UUID", input: base + old + "\n", upTo: "old", want: []string{old}}, + {name: "resume uses the first exclusive UUID", input: base + big + "\n", before: []string{"large", "recent"}, want: []string{old}}, + {name: "truncate", input: base, change: "truncate", stale: true}, + {name: "replace at the same size", input: base, change: "replace", stale: true}, + {name: "profile mismatch", input: base, change: "profile", stale: true}, + {name: "new agent", input: base, change: "agent", stale: true}, + {name: "new agent resumes exclusively before the displayed page after append", input: base, change: "resume", want: []string{old, big}}, + {name: "resume rejects an overwritten boundary", input: base, change: "anchor changed"}, + } { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + project := filepath.Join(dir, "projects", "workspace") + if err := os.MkdirAll(project, 0700); err != nil { + t.Fatal(err) + } + path := filepath.Join(project, session+".jsonl") + write := func(path, data string) { + if err := os.WriteFile(path, []byte(data), 0600); err != nil { + t.Fatal(err) + } + } + write(path, tc.input) + service := New() + params := transcriptPageParams{TranscriptParams: TranscriptParams{SessionID: session, ConfigDir: dir}, UpTo: tc.upTo, Before: tc.before} + retention := tc.retention + if retention == 0 { + retention = transcriptCapBytes + } + ctx := context.Background() + page, err := service.readTranscriptPage(ctx, params, retention, budget) + if err != nil { + t.Fatal(err) + } + params.UpTo, params.Before = "", nil + switch tc.change { + case "append": + write(path, tc.input+`"added"}`+"\n"+old+"\n") + case "truncate": + write(path, old+"\n") + case "replace": + write(path+".new", tc.input) + if err := os.Rename(path+".new", path); err != nil { + t.Fatal(err) + } + case "profile": + params.ConfigDir = t.TempDir() + case "agent": + service = New() + case "resume", "anchor changed": + if len(page.Lines) != 1 || !equalRows([]string{string(page.Lines[0])}, []string{tip}) || page.BeforeAnchor == "" { + t.Fatal("fixture did not return an anchor before the displayed last row") + } + if tc.change == "resume" { + write(path, tc.input+line("appended", "new text")+"\n") + } else { + write(path, strings.Replace(tc.input, tip, line("recent", "다른"), 1)) + } + service = New() + params.BeforeAnchor = page.BeforeAnchor + page, err = service.readTranscriptPage(ctx, params, retention, budget) + if err != nil { + t.Fatal(err) + } + params.BeforeAnchor = "" + if tc.change == "anchor changed" { + if page.Found || page.Anchored { + t.Fatal("a different row matched the old anchor") + } + return + } + if !page.Anchored { + t.Fatal("the new agent could not verify the boundary") + } + } + if tc.stale { + if page.Cursor == "" { + t.Fatal("fixture did not page") + } + params.Cursor = page.Cursor + _, err := service.readTranscriptPage(ctx, params, retention, budget) + if !errors.Is(err, errStaleTranscriptCursor) { + t.Fatalf("expected stale cursor, got %v", err) + } + return + } + var actual []string + var fragments []byte + snapshot := page.Snapshot + for count := 0; ; count++ { + if count > 16 { + t.Fatal("cursor did not advance") + } + if !page.Found || page.Snapshot != snapshot || len(page.Positions) != len(page.Lines) { + t.Fatal("page metadata changed") + } + wire, err := proto.MarshalFrame(proto.Success("request-id", page)) + if err != nil || len(wire) > budget { + t.Fatalf("response length %d, %v", len(wire), err) + } + if page.Fragment != nil { + fragments = append(bytes.Clone(page.Fragment.Data), fragments...) + if page.Fragment.First { + actual = append([]string{string(bytes.TrimSpace(fragments))}, actual...) + fragments = nil + } + } + for i := len(page.Lines) - 1; i >= 0; i-- { + var compact bytes.Buffer + if err := json.Compact(&compact, page.Lines[i]); err != nil { + t.Fatal(err) + } + actual = append([]string{compact.String()}, actual...) + } + if page.Cursor == "" { + break + } + params.Cursor = page.Cursor + page, err = service.readTranscriptPage(ctx, params, retention, budget) + if err != nil { + t.Fatal(err) + } + } + if len(fragments) != 0 || !equalRows(actual, tc.want) { + t.Fatalf("assembled rows differ: %d vs %d", len(actual), len(tc.want)) + } + }) + } +} + +func equalRows(a, b []string) bool { + if len(a) != len(b) { + return false + } + for i, want := range b { + wire, _ := json.Marshal(json.RawMessage(want)) + got, _ := json.Marshal(json.RawMessage(a[i])) + if !bytes.Equal(got, wire) { + return false + } + } + return true +} diff --git a/internal/chat/transcript_test.go b/internal/chat/transcript_test.go new file mode 100644 index 000000000..4f3792e24 --- /dev/null +++ b/internal/chat/transcript_test.go @@ -0,0 +1,143 @@ +package chat + +import ( + "context" + "encoding/json" + "fmt" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestTranscriptFindsSessionInAnyProject(t *testing.T) { + dir := t.TempDir() + project := filepath.Join(dir, "projects", "-some-project") + if err := os.MkdirAll(project, 0o755); err != nil { + t.Fatal(err) + } + id := "0b6a3c2e-6f1e-4a59-9d0e-2f6d7c1a8b90" + content := `{"type":"user","n":1}` + "\n" + "garbage\n" + `{"type":"assistant","n":2}` + "\n" + if err := os.WriteFile(filepath.Join(project, id+".jsonl"), []byte(content), 0o644); err != nil { + t.Fatal(err) + } + service := New() + params, _ := json.Marshal(TranscriptParams{SessionID: id, ConfigDir: dir}) + result, err := service.Transcript(context.Background(), params) + if err != nil { + t.Fatalf("Transcript: %v", err) + } + got := result.(TranscriptResult) + if !got.Found || len(got.Lines) != 2 || string(got.Lines[1]) != `{"type":"assistant","n":2}` { + t.Fatalf("transcript = %+v", got) + } + + params, _ = json.Marshal(TranscriptParams{SessionID: "../../etc/passwd", ConfigDir: dir}) + if _, err := service.Transcript(context.Background(), params); err == nil { + t.Fatal("Transcript took a path for a session id") + } +} + +func TestSubagentTranscriptFindsAnAgentByItsIdOrItsCall(t *testing.T) { + dir := t.TempDir() + id := "0b6a3c2e-6f1e-4a59-9d0e-2f6d7c1a8b90" + folder := filepath.Join(dir, "projects", "-some-project", id, "subagents") + body := `{"parentUuid":null,"isSidechain":true,"agentId":"a1b2c3","type":"user","message":{"role":"user","content":"go"}}` + "\n" + + `{"type":"api-request-blob","base":"..."}` + "\n" + + "garbage\n" + + `{"parentUuid":"x","isSidechain":true,"type":"assistant","message":{"content":[]}}` + "\n" + + `{"type":"attachment","attachment":{}}` + "\n" + writeTestFile(t, filepath.Join(folder, "agent-a1b2c3.jsonl"), body) + writeTestFile(t, filepath.Join(folder, "agent-a1b2c3.meta.json"), `{"agentType":"Explore","toolUseId":"toolu_01"}`) + writeTestFile(t, filepath.Join(folder, "agent-other.jsonl"), `{"type":"user","message":{}}`+"\n") + writeTestFile(t, filepath.Join(folder, "agent-other.meta.json"), `{"agentType":"Plan","toolUseId":"toolu_02"}`) + service := New() + read := func(params SubagentTranscriptParams) SubagentTranscriptResult { + t.Helper() + raw, _ := json.Marshal(params) + result, err := service.SubagentTranscript(context.Background(), raw) + if err != nil { + t.Fatalf("SubagentTranscript(%+v): %v", params, err) + } + return result.(SubagentTranscriptResult) + } + + got := read(SubagentTranscriptParams{SessionID: id, AgentID: "a1b2c3", ConfigDir: dir}) + if !got.Found || len(got.Lines) != 2 { + t.Fatalf("by agent id = %+v", got) + } + if typ := messageType(got.Lines[1]); typ != "assistant" { + t.Fatalf("second line is %q", typ) + } + if got := read(SubagentTranscriptParams{SessionID: id, ToolUseID: "toolu_01", ConfigDir: dir}); !got.Found || len(got.Lines) != 2 { + t.Fatalf("by call = %+v", got) + } + // An agent id with no file of its own is looked for by its call. + if got := read(SubagentTranscriptParams{SessionID: id, AgentID: "gone", ToolUseID: "toolu_02", ConfigDir: dir}); !got.Found || len(got.Lines) != 1 { + t.Fatalf("by call after the id = %+v", got) + } + if got := read(SubagentTranscriptParams{SessionID: id, ToolUseID: "toolu_03", ConfigDir: dir}); got.Found { + t.Fatalf("a call with no agent = %+v", got) + } + + for _, bad := range []SubagentTranscriptParams{ + {SessionID: "../../etc/passwd", AgentID: "a1b2c3", ConfigDir: dir}, + {SessionID: id, AgentID: "../../x", ConfigDir: dir}, + {SessionID: id, ConfigDir: dir}, + } { + raw, _ := json.Marshal(bad) + if _, err := service.SubagentTranscript(context.Background(), raw); err == nil { + t.Fatalf("SubagentTranscript took %+v", bad) + } + } +} + +func TestSessionFileFindsClaudeSessions(t *testing.T) { + service := New() + find := func(params SessionFileParams) SessionFileResult { + t.Helper() + raw, _ := json.Marshal(params) + result, err := service.SessionFile(context.Background(), raw) + if err != nil { + t.Fatalf("SessionFile(%+v): %v", params, err) + } + return result.(SessionFileResult) + } + id := "0b6a3c2e-6f1e-4a59-9d0e-2f6d7c1a8b90" + claude := t.TempDir() + transcript := filepath.Join(claude, "projects", "-w", id+".jsonl") + writeTestFile(t, transcript, "{}\n") + if got := find(SessionFileParams{SessionID: id, ConfigDir: claude}); !got.Found || got.Path != transcript { + t.Fatalf("claude = %+v", got) + } + if got := find(SessionFileParams{SessionID: "1b6a3c2e-6f1e-4a59-9d0e-2f6d7c1a8b90", ConfigDir: claude}); got.Found { + t.Fatalf("a session not there = %+v", got) + } + raw, _ := json.Marshal(SessionFileParams{SessionID: "../../etc/passwd", ConfigDir: claude}) + if _, err := service.SessionFile(context.Background(), raw); err == nil { + t.Fatal("SessionFile took a path for a session id") + } +} + +func TestReadJSONLinesKeepsTheEnd(t *testing.T) { + path := filepath.Join(t.TempDir(), "t.jsonl") + var b strings.Builder + for i := 0; i < 100; i++ { + fmt.Fprintf(&b, `{"n":%d}`+"\n", i) + } + if err := os.WriteFile(path, []byte(b.String()), 0o644); err != nil { + t.Fatal(err) + } + lines, truncated, err := readJSONLines(path, 50) + if err != nil || !truncated { + t.Fatalf("err %v, truncated %v", err, truncated) + } + if len(lines) == 0 || string(lines[len(lines)-1]) != `{"n":99}` { + t.Fatalf("lines = %s", lines) + } + for _, line := range lines { + if !json.Valid(line) { + t.Fatalf("partial line %s", line) + } + } +} diff --git a/internal/chat/types.go b/internal/chat/types.go index 1820ab22d..9e66ecc9a 100644 --- a/internal/chat/types.go +++ b/internal/chat/types.go @@ -55,7 +55,9 @@ type EventSink func(event string, payload any) error type StartParams struct { WorkspaceID string `json:"workspaceId"` TabID string `json:"tabId"` - Cwd string `json:"cwd"` + // TabGeneration identifies the open tab that requested the start. + TabGeneration string `json:"tabGeneration,omitempty"` + Cwd string `json:"cwd"` // Program is the program to run, an absolute path on the agent's host. Program string `json:"program"` Args []string `json:"args,omitempty"` @@ -91,6 +93,8 @@ type SendParams struct { type TabParams struct { WorkspaceID string `json:"workspaceId"` TabID string `json:"tabId"` + // TabGeneration limits a close to one opening of the tab. + TabGeneration string `json:"tabGeneration,omitempty"` } // ReplayParams is the wire shape for chat.replay. @@ -143,6 +147,8 @@ type LinePayload struct { Dir string `json:"dir"` Message json.RawMessage `json:"message,omitempty"` Text string `json:"text,omitempty"` + // ProcessGeneration identifies the running program that produced a live line. + ProcessGeneration string `json:"processGeneration,omitempty"` // At is when the agent got the line, in Unix milliseconds. At int64 `json:"at"` // Replay marks a line sent again by chat.replay. @@ -151,11 +157,12 @@ type LinePayload struct { // ExitPayload is the chat.exit event. type ExitPayload struct { - WorkspaceID string `json:"workspaceId"` - TabID string `json:"tabId"` - Code *int `json:"code"` - Signal *string `json:"signal,omitempty"` - StderrTail string `json:"stderrTail,omitempty"` + WorkspaceID string `json:"workspaceId"` + TabID string `json:"tabId"` + Code *int `json:"code"` + Signal *string `json:"signal,omitempty"` + StderrTail string `json:"stderrTail,omitempty"` + ProcessGeneration string `json:"processGeneration,omitempty"` } // ExecParams is the wire shape for chat.exec: a short run of the program, diff --git a/internal/dispatch/dispatch.go b/internal/dispatch/dispatch.go index 25fca7833..b789432df 100644 --- a/internal/dispatch/dispatch.go +++ b/internal/dispatch/dispatch.go @@ -25,12 +25,12 @@ type Handler func(ctx context.Context, params json.RawMessage) (any, error) // do not need synchronization. type Dispatcher struct { handlers map[string]Handler - ordered map[string]bool + ordered map[string]func(json.RawMessage) string } // New constructs an empty Dispatcher ready for Register calls. func New() *Dispatcher { - return &Dispatcher{handlers: make(map[string]Handler), ordered: make(map[string]bool)} + return &Dispatcher{handlers: make(map[string]Handler), ordered: make(map[string]func(json.RawMessage) string)} } // Register binds `method` to `handler`. The last registration wins — @@ -45,13 +45,30 @@ func (d *Dispatcher) Register(method string, handler Handler) { // its requests one at a time, in the order they arrived, instead of each // on its own goroutine. The order holds across every ordered method. func (d *Dispatcher) RegisterOrdered(method string, handler Handler) { + d.RegisterOrderedBy(method, handler, nil) +} + +// RegisterOrderedBy runs matching requests in arrival order for each key. +// Methods with the same key share a queue, including consecutive requests +// that use different methods. A nil key function uses the default queue. +func (d *Dispatcher) RegisterOrderedBy(method string, handler Handler, key func(json.RawMessage) string) { d.handlers[method] = handler - d.ordered[method] = true + d.ordered[method] = key } // Ordered reports whether `method` was registered with RegisterOrdered. func (d *Dispatcher) Ordered(method string) bool { - return d.ordered[method] + _, ok := d.ordered[method] + return ok +} + +// OrderKey returns the queue for a request registered as ordered. +func (d *Dispatcher) OrderKey(req proto.Request) (string, bool) { + key, ordered := d.ordered[req.Method] + if key == nil { + return "", ordered + } + return key(req.Params), true } // Methods returns a sorted slice of all registered method names. diff --git a/internal/filelink/junction_other.go b/internal/filelink/junction_other.go new file mode 100644 index 000000000..2750af573 --- /dev/null +++ b/internal/filelink/junction_other.go @@ -0,0 +1,10 @@ +//go:build !windows + +package filelink + +import "errors" + +// Junction is Windows's: elsewhere a symbolic link links a folder. +func Junction(_, _ string) error { + return errors.ErrUnsupported +} diff --git a/internal/filelink/junction_windows.go b/internal/filelink/junction_windows.go new file mode 100644 index 000000000..a9233c7bc --- /dev/null +++ b/internal/filelink/junction_windows.go @@ -0,0 +1,64 @@ +//go:build windows + +package filelink + +import ( + "encoding/binary" + "os" + "unicode/utf16" + + "golang.org/x/sys/windows" +) + +// Junction makes link a junction to the directory at the absolute path +// target: a link to a folder that, unlike a symbolic link, needs no rights. +func Junction(link, target string) error { + if err := os.Mkdir(link, 0o755); err != nil { + return err + } + if err := setMountPoint(link, target); err != nil { + _ = os.Remove(link) + return err + } + return nil +} + +// setMountPoint turns the empty directory dir into a junction to target. +func setMountPoint(dir, target string) error { + path, err := windows.UTF16PtrFromString(dir) + if err != nil { + return err + } + h, err := windows.CreateFile(path, windows.GENERIC_WRITE, 0, nil, windows.OPEN_EXISTING, + windows.FILE_FLAG_BACKUP_SEMANTICS|windows.FILE_FLAG_OPEN_REPARSE_POINT, 0) + if err != nil { + return err + } + defer windows.CloseHandle(h) + + buf := mountPointReparseData(target) + var returned uint32 + return windows.DeviceIoControl(h, windows.FSCTL_SET_REPARSE_POINT, &buf[0], uint32(len(buf)), nil, 0, &returned, nil) +} + +// mountPointReparseData lays out a REPARSE_DATA_BUFFER for a junction to +// target: the NT path it leads to (\??\C:\...) and the path shown for it, each +// ending in NUL. +func mountPointReparseData(target string) []byte { + substitute := utf16.Encode([]rune(`\??\` + target)) + shown := utf16.Encode([]rune(target)) + names := append(append(append(substitute, 0), shown...), 0) + + dataLen := 8 + 2*len(names) + buf := make([]byte, 8+dataLen) + binary.LittleEndian.PutUint32(buf[0:], windows.IO_REPARSE_TAG_MOUNT_POINT) + binary.LittleEndian.PutUint16(buf[4:], uint16(dataLen)) + binary.LittleEndian.PutUint16(buf[8:], 0) // SubstituteNameOffset + binary.LittleEndian.PutUint16(buf[10:], uint16(2*len(substitute))) // SubstituteNameLength + binary.LittleEndian.PutUint16(buf[12:], uint16(2*len(substitute)+2)) // PrintNameOffset + binary.LittleEndian.PutUint16(buf[14:], uint16(2*len(shown))) // PrintNameLength + for i, c := range names { + binary.LittleEndian.PutUint16(buf[16+2*i:], c) + } + return buf +} diff --git a/internal/filewatch/events.go b/internal/filewatch/events.go new file mode 100644 index 000000000..d41383926 --- /dev/null +++ b/internal/filewatch/events.go @@ -0,0 +1,23 @@ +// Package filewatch defines the agent's file change events. +package filewatch + +// FsChangeKind is the wire enum used by fs.changed event payloads. +type FsChangeKind string + +const ( + FsChangeAdded FsChangeKind = "added" + FsChangeModified FsChangeKind = "modified" + FsChangeDeleted FsChangeKind = "deleted" +) + +// FsChange is one path change in an fs.changed event. +type FsChange struct { + RelPath string `json:"relPath"` + Kind FsChangeKind `json:"kind"` +} + +// FsChangedPayload is emitted by the agent. Electron adds workspaceId before +// forwarding it to renderer IPC subscribers. +type FsChangedPayload struct { + Changes []FsChange `json:"changes"` +} diff --git a/internal/fs/copy.go b/internal/fs/copy.go index f553ab042..a719489e1 100644 --- a/internal/fs/copy.go +++ b/internal/fs/copy.go @@ -42,15 +42,15 @@ func (s *Service) CopyFile(ctx context.Context, raw json.RawMessage) (any, error return nil, mapWriteError(err, srcAbs) } - // Destination existence check. By default the destination must NOT exist - // (O_EXCL semantics). With Overwrite=true the caller has confirmed a - // replace, so an existing destination is moved aside and only deleted - // once the copy has succeeded; a failed copy puts it back. + // Keep the old destination until the replacement has fully succeeded. backup := "" if _, err := os.Lstat(dstAbs); err == nil { if !p.Overwrite { return nil, FSError{Code: CodeAlreadyExists, Path: dstAbs} } + if err := ctx.Err(); err != nil { + return nil, err + } if backup, err = moveAside(dstAbs); err != nil { return nil, mapWriteError(err, dstAbs) } @@ -58,18 +58,14 @@ func (s *Service) CopyFile(ctx context.Context, raw json.RawMessage) (any, error return nil, mapWriteError(err, dstAbs) } - if srcInfo.IsDir() { - err = copyDir(srcAbs, dstAbs) - } else if srcInfo.Mode()&os.ModeSymlink != 0 { - err = copySymlink(srcAbs, dstAbs) - } else { - err = copyRegularFile(srcAbs, dstAbs, srcInfo) + created, err := copyPath(ctx, srcAbs, dstAbs, srcInfo) + if err == nil { + err = ctx.Err() } if err != nil { - // Take away the part copied, then put back what was there, if - // anything was. - _ = os.RemoveAll(dstAbs) - restoreAside(backup, dstAbs) + if rollbackErr := rollbackCopy(dstAbs, backup, created); rollbackErr != nil { + return nil, errors.Join(mapWriteError(err, srcAbs), rollbackErr) + } return nil, mapWriteError(err, srcAbs) } if err := discardAside(backup); err != nil { @@ -79,80 +75,116 @@ func (s *Service) CopyFile(ctx context.Context, raw json.RawMessage) (any, error return struct{}{}, nil } -// copyRegularFile copies the bytes and permission bits of a regular file. -func copyRegularFile(src, dst string, srcInfo os.FileInfo) error { - return copyFileContents(src, dst, srcInfo) +// rollbackCopy removes only the path created by this copy. A destination +// that appeared before creation succeeded belongs to another operation. +func rollbackCopy(dst, backup string, created bool) error { + if created { + if err := os.RemoveAll(dst); err != nil { + return err + } + } + if backup == "" { + return nil + } + if _, err := os.Lstat(dst); err == nil { + return FSError{Code: CodeAlreadyExists, Path: dst} + } else if !errors.Is(err, iofs.ErrNotExist) { + return err + } + return os.Rename(backup, dst) } -// copyDir creates the destination directory and recursively copies every entry. -func copyDir(src, dst string) error { - srcInfo, err := os.Stat(src) - if err != nil { - return err +// copyPath reports whether this operation owns the destination, including +// on error, so a failed attempt can remove its partial copy safely. +func copyPath(ctx context.Context, src, dst string, info os.FileInfo) (bool, error) { + if err := ctx.Err(); err != nil { + return false, err } - if err := os.Mkdir(dst, srcInfo.Mode()); err != nil { - return err + switch { + case info.IsDir(): + if err := os.Mkdir(dst, info.Mode()); err != nil { + return false, err + } + return true, copyDirEntries(ctx, src, dst) + case info.Mode()&os.ModeSymlink != 0: + return copySymlink(ctx, src, dst) + case info.Mode().IsRegular(): + return copyFileContents(ctx, src, dst, info) + default: + return false, proto.ProtocolError("fs.copyFile source must be a regular file, directory, or symlink") } +} +// copyDirEntries copies each child into the directory owned by this copy. +func copyDirEntries(ctx context.Context, src, dst string) error { entries, err := os.ReadDir(src) if err != nil { return err } for _, entry := range entries { + if err := ctx.Err(); err != nil { + return err + } srcEntry := filepath.Join(src, entry.Name()) - dstEntry := filepath.Join(dst, entry.Name()) - info, err := os.Lstat(srcEntry) if err != nil { return err } - if info.IsDir() { - if err := copyDir(srcEntry, dstEntry); err != nil { - return err - } - } else if info.Mode()&os.ModeSymlink != 0 { - if err := copySymlink(srcEntry, dstEntry); err != nil { - return err - } - } else { - if err := copyRegularFile(srcEntry, dstEntry, info); err != nil { - return err - } + if _, err := copyPath(ctx, srcEntry, filepath.Join(dst, entry.Name()), info); err != nil { + return err } } return nil } -// copySymlink preserves the symlink by reading the link target and creating -// a new symlink at the destination with the same target, or what stands in -// for one where it cannot be created (see symlinkFallback). -func copySymlink(src, dst string) error { +// copySymlink preserves the original link target or uses the platform fallback. +func copySymlink(ctx context.Context, src, dst string) (bool, error) { target, err := os.Readlink(src) if err != nil { - return err + return false, err } if err := os.Symlink(target, dst); err != nil { - return symlinkFallback(src, dst, target, err) + return symlinkFallback(ctx, src, dst, target, err) } - return nil + return true, nil } -// copyFileContents is the single io.Copy path used by copyRegularFile. -func copyFileContents(src, dst string, srcInfo os.FileInfo) error { +// copyFileContents keeps the permission bits and checks cancellation +// between chunks. Wrapping the reader avoids an uncancellable file fast path. +func copyFileContents(ctx context.Context, src, dst string, srcInfo os.FileInfo) (bool, error) { in, err := os.Open(src) if err != nil { - return err + return false, err } defer in.Close() - out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcInfo.Mode()) + out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_EXCL, srcInfo.Mode()) if err != nil { - return err + return false, err } defer out.Close() - if _, err := io.Copy(out, in); err != nil { - return err + if _, err := io.Copy(out, copyReader{ctx, in}); err != nil { + return true, err + } + if err := ctx.Err(); err != nil { + return true, err + } + if err := out.Sync(); err != nil { + return true, err + } + return true, out.Close() +} + +type copyReader struct { + ctx context.Context + src io.Reader +} + +// Read checks cancellation before io.Copy reads the next block. +func (r copyReader) Read(p []byte) (int, error) { + if err := r.ctx.Err(); err != nil { + return 0, err } - return out.Sync() + return r.src.Read(p) } diff --git a/internal/fs/copy_test.go b/internal/fs/copy_test.go new file mode 100644 index 000000000..da946f8ec --- /dev/null +++ b/internal/fs/copy_test.go @@ -0,0 +1,65 @@ +package fs + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "testing" +) + +// cancelCopyAfterWrite cancels after a real write to the copied file. +type cancelCopyAfterWrite struct { + context.Context + path string + cancel context.CancelFunc +} + +func (c cancelCopyAfterWrite) Err() error { + if stat, err := os.Stat(c.path); err == nil && stat.Size() > 0 { + c.cancel() + } + return c.Context.Err() +} + +// TestCopyCancellationRollsBackPartialDirectory covers cancellation after a +// write both with and without a destination to restore. +func TestCopyCancellationRollsBackPartialDirectory(t *testing.T) { + for _, name := range []string{"new directory", "overwrite"} { + t.Run(name, func(t *testing.T) { + root := t.TempDir() + must(t, os.MkdirAll(filepath.Join(root, "src", "sub"), 0o755)) + must(t, os.WriteFile(filepath.Join(root, "src", "sub", "file"), []byte(strings.Repeat("a", 96<<10)), 0o644)) + dst := filepath.Join(root, "dst") + overwrite := name == "overwrite" + if overwrite { + must(t, os.Mkdir(dst, 0o755)) + must(t, os.WriteFile(filepath.Join(dst, "original"), []byte("original contents"), 0o644)) + } + partial := filepath.Join(dst, "sub", "file") + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + _, err := mustFS(t, root).CopyFile(cancelCopyAfterWrite{ctx, partial, cancel}, mustJSON(t, CopyFileParams{ + FromRelPath: "src", + ToRelPath: "dst", + Overwrite: overwrite, + })) + if !errors.Is(err, context.Canceled) { + t.Fatalf("copy returned %v, want cancellation after first write", err) + } + if _, err := os.Stat(partial); !os.IsNotExist(err) { + t.Fatalf("partial copy remains: %v", err) + } + if overwrite { + original, err := os.ReadFile(filepath.Join(dst, "original")) + if err != nil || string(original) != "original contents" { + t.Fatalf("overwrite lost original: %q, %v", original, err) + } + } else if _, err := os.Stat(dst); !os.IsNotExist(err) { + t.Fatalf("partial destination remains: %v", err) + } + assertNoTmpResidue(t, root) + }) + } +} diff --git a/internal/fs/link_unix.go b/internal/fs/link_unix.go index abf175fcb..b20c0a33a 100644 --- a/internal/fs/link_unix.go +++ b/internal/fs/link_unix.go @@ -2,15 +2,10 @@ package fs -import "errors" +import "context" // symlinkFallback stands in for a symbolic link the agent could not create. // Elsewhere than on Windows there is nothing to stand in: err is returned. -func symlinkFallback(_, _, _ string, err error) error { - return err -} - -// Junction is Windows's: elsewhere a symbolic link links a folder. -func Junction(_, _ string) error { - return errors.ErrUnsupported +func symlinkFallback(_ context.Context, _, _, _ string, err error) (bool, error) { + return false, err } diff --git a/internal/fs/link_windows.go b/internal/fs/link_windows.go index de58a2008..5f2b79093 100644 --- a/internal/fs/link_windows.go +++ b/internal/fs/link_windows.go @@ -3,11 +3,12 @@ package fs import ( - "encoding/binary" + "context" "errors" "os" "path/filepath" - "unicode/utf16" + + "github.com/nexus-code/nexus-code/internal/filelink" "golang.org/x/sys/windows" ) @@ -18,79 +19,21 @@ import ( // a junction pnpm puts in node_modules, failed. A link to a directory becomes // a junction to it, which needs no privilege; a link to a file becomes a copy // of the file. A relative target leads where the copied link would. -func symlinkFallback(src, dst, target string, err error) error { +func symlinkFallback(ctx context.Context, src, dst, target string, err error) (bool, error) { if !errors.Is(err, windows.ERROR_PRIVILEGE_NOT_HELD) { - return err + return false, err } info, statErr := os.Stat(src) if statErr != nil { // A dangling link leads nowhere to stand in for. - return err + return false, err } if !info.IsDir() { - return copyFileContents(src, dst, info) + return copyFileContents(ctx, src, dst, info) } if !filepath.IsAbs(target) { target = filepath.Join(filepath.Dir(dst), target) } - return createJunction(dst, filepath.Clean(target)) -} - -// Junction makes link a junction to the directory at the absolute path -// target: a link to a folder that, unlike a symbolic link, needs no rights. -func Junction(link, target string) error { - return createJunction(link, target) -} - -// createJunction makes link a junction to the directory at the absolute path -// target. -func createJunction(link, target string) error { - if err := os.Mkdir(link, 0o755); err != nil { - return err - } - if err := setMountPoint(link, target); err != nil { - _ = os.Remove(link) - return err - } - return nil -} - -// setMountPoint turns the empty directory dir into a junction to target. -func setMountPoint(dir, target string) error { - path, err := windows.UTF16PtrFromString(dir) - if err != nil { - return err - } - h, err := windows.CreateFile(path, windows.GENERIC_WRITE, 0, nil, windows.OPEN_EXISTING, - windows.FILE_FLAG_BACKUP_SEMANTICS|windows.FILE_FLAG_OPEN_REPARSE_POINT, 0) - if err != nil { - return err - } - defer windows.CloseHandle(h) - - buf := mountPointReparseData(target) - var returned uint32 - return windows.DeviceIoControl(h, windows.FSCTL_SET_REPARSE_POINT, &buf[0], uint32(len(buf)), nil, 0, &returned, nil) -} - -// mountPointReparseData lays out a REPARSE_DATA_BUFFER for a junction to -// target: the NT path it leads to (\??\C:\...) and the path shown for it, each -// ending in NUL. -func mountPointReparseData(target string) []byte { - substitute := utf16.Encode([]rune(`\??\` + target)) - shown := utf16.Encode([]rune(target)) - names := append(append(append(substitute, 0), shown...), 0) - - dataLen := 8 + 2*len(names) - buf := make([]byte, 8+dataLen) - binary.LittleEndian.PutUint32(buf[0:], windows.IO_REPARSE_TAG_MOUNT_POINT) - binary.LittleEndian.PutUint16(buf[4:], uint16(dataLen)) - binary.LittleEndian.PutUint16(buf[8:], 0) // SubstituteNameOffset - binary.LittleEndian.PutUint16(buf[10:], uint16(2*len(substitute))) // SubstituteNameLength - binary.LittleEndian.PutUint16(buf[12:], uint16(2*len(substitute)+2)) // PrintNameOffset - binary.LittleEndian.PutUint16(buf[14:], uint16(2*len(shown))) // PrintNameLength - for i, c := range names { - binary.LittleEndian.PutUint16(buf[16+2*i:], c) - } - return buf + err = filelink.Junction(dst, filepath.Clean(target)) + return err == nil, err } diff --git a/internal/fs/link_windows_test.go b/internal/fs/link_windows_test.go index 3fc3d57fa..8d104687c 100644 --- a/internal/fs/link_windows_test.go +++ b/internal/fs/link_windows_test.go @@ -60,7 +60,7 @@ func TestSymlinkFallbackCopiesAFileLinkAsItsFile(t *testing.T) { dst := filepath.Join(root, "copy.txt") refused := &os.LinkError{Op: "symlink", Old: "real.txt", New: dst, Err: windows.ERROR_PRIVILEGE_NOT_HELD} - if err := symlinkFallback(src, dst, "real.txt", refused); err != nil { + if _, err := symlinkFallback(context.Background(), src, dst, "real.txt", refused); err != nil { t.Fatalf("symlinkFallback: %v", err) } if got, err := os.ReadFile(dst); err != nil || string(got) != "payload" { @@ -80,7 +80,7 @@ func TestSymlinkFallbackKeepsARelativeDirectoryLinkRelative(t *testing.T) { refused := &os.LinkError{Op: "symlink", Old: "real", New: dst, Err: windows.ERROR_PRIVILEGE_NOT_HELD} // src stands for the link: os.Stat follows a link, so the directory it // leads to does as well. - if err := symlinkFallback(filepath.Join(root, "src", "real"), dst, "real", refused); err != nil { + if _, err := symlinkFallback(context.Background(), filepath.Join(root, "src", "real"), dst, "real", refused); err != nil { t.Fatalf("symlinkFallback: %v", err) } if _, err := os.Stat(filepath.Join(dst, "mine.txt")); err != nil { @@ -94,7 +94,7 @@ func TestSymlinkFallbackLeavesOtherErrors(t *testing.T) { src := filepath.Join(root, "real.txt") must(t, os.WriteFile(src, nil, 0o644)) other := &os.LinkError{Op: "symlink", Err: windows.ERROR_ACCESS_DENIED} - if err := symlinkFallback(src, filepath.Join(root, "copy.txt"), "real.txt", other); err != other { + if _, err := symlinkFallback(context.Background(), src, filepath.Join(root, "copy.txt"), "real.txt", other); err != other { t.Fatalf("symlinkFallback = %v, want the error back", err) } if _, err := os.Lstat(filepath.Join(root, "copy.txt")); err == nil { diff --git a/internal/fs/path.go b/internal/fs/path.go index 3b1260869..f2d7c1883 100644 --- a/internal/fs/path.go +++ b/internal/fs/path.go @@ -65,3 +65,7 @@ func parseAbsolutePath(raw json.RawMessage) (string, error) { } return filepath.Clean(p.AbsolutePath), nil } + +func hasDotDotPrefix(rel string) bool { + return rel == ".." || len(rel) > 3 && rel[:3] == ".."+string(filepath.Separator) +} diff --git a/internal/fs/preview_unix.go b/internal/fs/preview_unix.go index 5a077e14a..ddc9adab6 100644 --- a/internal/fs/preview_unix.go +++ b/internal/fs/preview_unix.go @@ -13,15 +13,22 @@ import ( // Open the resolved absolute path one component at a time. O_NOFOLLOW // protects each component if it changes after the grant's symlink checks. func openPreviewFile(real string) (*os.File, error) { + return openCanonicalPath(real, false) +} + +func openCanonicalPath(real string, directory bool) (*os.File, error) { const flags = unix.O_CLOEXEC | unix.O_NOFOLLOW fd, err := unix.Open("/", flags|previewDirOpenFlags, 0) if err != nil { return nil, err } + if real == "/" && directory { + return os.NewFile(uintptr(fd), real), nil + } parts := strings.Split(strings.TrimPrefix(real, "/"), "/") for i, part := range parts { mode := flags | unix.O_RDONLY | unix.O_NONBLOCK - if i < len(parts)-1 { + if directory || i < len(parts)-1 { mode = flags | previewDirOpenFlags } next, err := unix.Openat(fd, part, mode, 0) diff --git a/internal/fs/service.go b/internal/fs/service.go index 5ffa7d0da..8a44569b9 100644 --- a/internal/fs/service.go +++ b/internal/fs/service.go @@ -17,6 +17,7 @@ import ( "time" "github.com/nexus-code/nexus-code/internal/dispatch" + "github.com/nexus-code/nexus-code/internal/filewatch" ) // MaxReadableFileSize caps how many bytes one fs.readFile / fs.writeFile call @@ -39,6 +40,10 @@ type EventSink func(event string, payload any) error type Service struct { root string + uploadMu sync.Mutex + uploads map[string]*fileUpload + uploadClosed bool + // watchMu serializes fs.watch, fs.unwatch and Close, so fsnotify Add and // Remove run in call order without holding mu, which the event loop // needs before it can take the next event from fsnotify. @@ -47,7 +52,7 @@ type Service struct { mu sync.Mutex watches map[string]*watchEntry - buffers map[string]FsChangeKind + buffers map[string]filewatch.FsChangeKind timer *time.Timer sink EventSink } @@ -62,8 +67,9 @@ func New(root string) (*Service, error) { } return &Service{ root: filepath.Clean(abs), + uploads: make(map[string]*fileUpload), watches: make(map[string]*watchEntry), - buffers: make(map[string]FsChangeKind), + buffers: make(map[string]filewatch.FsChangeKind), }, nil } @@ -74,8 +80,9 @@ func (s *Service) SetEventSink(sink EventSink) { s.sink = sink } -// Close stops watcher goroutines and clears buffered fs events. +// Close discards uploads, stops watchers, and clears buffered fs events. func (s *Service) Close() { + s.DropUploads(nil) s.watchMu.Lock() defer s.watchMu.Unlock() s.mu.Lock() @@ -107,6 +114,13 @@ func Register(d *dispatch.Dispatcher, fsys *Service) { d.Register("fs.readAbsoluteRange", fsys.ReadAbsoluteRange) d.Register("fs.readPreview", fsys.ReadPreview) d.Register("fs.writeFile", fsys.WriteFile) + d.Register("fs.upload.resolve", bindUpload(fsys.resolveUpload)) + d.Register("fs.upload.mkdir", bindUpload(fsys.mkdirUpload)) + d.Register("fs.upload.begin", bindUpload(fsys.beginUpload)) + d.Register("fs.upload.chunk", bindUpload(fsys.chunkUpload)) + d.Register("fs.upload.verify", bindUpload(fsys.verifyUpload)) + d.Register("fs.upload.commit", bindUpload(fsys.commitUpload)) + d.Register("fs.upload.abort", bindUpload(fsys.abortUpload)) d.Register("fs.createFile", fsys.CreateFile) d.Register("fs.mkdir", fsys.Mkdir) d.Register("fs.unlink", fsys.Unlink) @@ -114,6 +128,6 @@ func Register(d *dispatch.Dispatcher, fsys *Service) { d.Register("fs.rename", fsys.Rename) d.Register("fs.copyFile", fsys.CopyFile) d.Register("fs.removeAll", fsys.RemoveAll) - d.Register("fs.watch", fsys.Watch) - d.Register("fs.unwatch", fsys.Unwatch) + d.RegisterOrderedBy("fs.watch", fsys.Watch, fsys.watchOrderKey) + d.RegisterOrderedBy("fs.unwatch", fsys.Unwatch, fsys.watchOrderKey) } diff --git a/internal/fs/types.go b/internal/fs/types.go index 0d3665d3e..8f739696e 100644 --- a/internal/fs/types.go +++ b/internal/fs/types.go @@ -11,27 +11,6 @@ type DirEntry struct { Type string `json:"type"` } -// FsChangeKind is the wire enum used by fs.changed event payloads. -type FsChangeKind string - -const ( - FsChangeAdded FsChangeKind = "added" - FsChangeModified FsChangeKind = "modified" - FsChangeDeleted FsChangeKind = "deleted" -) - -// FsChange is one path change in an fs.changed event. -type FsChange struct { - RelPath string `json:"relPath"` - Kind FsChangeKind `json:"kind"` -} - -// FsChangedPayload is emitted by the agent. Electron adds workspaceId before -// forwarding it to renderer IPC subscribers. -type FsChangedPayload struct { - Changes []FsChange `json:"changes"` -} - // StatResult is the wire shape returned by fs.stat. type StatResult struct { Type string `json:"type"` diff --git a/internal/fs/upload.go b/internal/fs/upload.go new file mode 100644 index 000000000..fe78933f8 --- /dev/null +++ b/internal/fs/upload.go @@ -0,0 +1,516 @@ +package fs + +import ( + "context" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "encoding/json" + "errors" + "log/slog" + "os" + "path/filepath" + "regexp" + "strings" + "sync" + "time" + + "github.com/nexus-code/nexus-code/internal/proto" +) + +const ( + maxUploads = 8 + maxUploadSize = 20 * 1024 * 1024 * 1024 + maxUploadChunk = 512 * 1024 + uploadIdle = 2 * time.Minute + staleUploadAge = 24 * time.Hour + uploadPrefix = ".nexus-push-" + uploadSuffix = ".tmp" +) + +var uploadIDPattern = regexp.MustCompile(`^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$`) + +type uploadParams struct { + RelPath string `json:"relPath"` + ExpectedPath string `json:"expectedPath"` + Directory bool `json:"directory"` + UploadID string `json:"uploadId"` + Size int64 `json:"size"` + Offset int64 `json:"offset"` + Data string `json:"data"` + SHA256 string `json:"sha256"` +} + +type uploadDestination struct { + path, parent string + info os.FileInfo +} + +type uploadSpace struct { + volume uint64 + free uint64 +} + +type fileUpload struct { + mu sync.Mutex + owner context.Context + ctx context.Context + cancel context.CancelFunc + stop func() bool + timer *time.Timer + deadline time.Time + + relPath string + dest uploadDestination + dir *os.File + file *os.File + temp string + size int64 + received int64 + verified bool + + // Guarded by Service.uploadMu. + space *uploadSpace + reserved uint64 +} + +func (s *Service) uploadDestination(rel string, directory bool) (uploadDestination, error) { + var dest uploadDestination + local := filepath.FromSlash(rel) + if strings.ContainsRune(local, 0) || (local != "" && !filepath.IsLocal(local)) { + return dest, FSError{Code: CodeOutOfWorkspace, Path: rel} + } + asked, err := s.Resolve(local) + if err != nil { + return dest, err + } + relative, err := filepath.Rel(s.root, asked) + if err != nil { + return dest, err + } + if relative == "." && !directory { + return dest, proto.ProtocolError("the destination is a folder") + } + base, err := filepath.EvalSymlinks(s.root) + if err != nil { + return dest, mapPathError(err, s.root) + } + parent := base + if relative != "." { + parts := strings.Split(relative, string(filepath.Separator)) + for _, part := range parts[:len(parts)-1] { + parent = filepath.Join(parent, part) + info, err := os.Lstat(parent) + if err != nil { + return dest, mapPathError(err, parent) + } + if info.Mode()&os.ModeSymlink != 0 { + return dest, FSError{Code: CodeOutOfWorkspace, Path: "the path goes through a symbolic link"} + } + if !info.IsDir() { + return dest, FSError{Code: CodeNotDirectory, Path: parent} + } + } + } + parent, err = filepath.EvalSymlinks(parent) + if err != nil { + return dest, mapPathError(err, parent) + } + within, err := filepath.Rel(base, parent) + if err != nil || !filepath.IsLocal(within) { + return dest, FSError{Code: CodeOutOfWorkspace, Path: "the path leads outside the workspace's folder"} + } + info, err := os.Stat(parent) + if err != nil { + return dest, mapPathError(err, parent) + } + if !info.IsDir() { + return dest, FSError{Code: CodeNotDirectory, Path: parent} + } + target := parent + if relative != "." { + target = filepath.Join(parent, filepath.Base(asked)) + } + return uploadDestination{path: target, parent: parent, info: info}, nil +} + +func (s *Service) resolveUpload(_ context.Context, p uploadParams) (any, error) { + dest, err := s.uploadDestination(p.RelPath, p.Directory) + return map[string]string{"path": dest.path}, err +} + +func changedUploadDestination() error { + return FSError{Code: CodeOutOfWorkspace, Path: "the destination folder changed during the upload"} +} + +func (dest uploadDestination) openParent(expected string) (*os.File, error) { + if dest.path != expected { + return nil, changedUploadDestination() + } + dir, err := openUploadParent(dest.parent) + if err != nil { + return nil, mapWriteError(err, dest.parent) + } + info, err := dir.Stat() + if err != nil || !os.SameFile(info, dest.info) { + _ = dir.Close() + return nil, changedUploadDestination() + } + return dir, nil +} + +func (s *Service) mkdirUpload(ctx context.Context, p uploadParams) (any, error) { + dest, err := s.uploadDestination(p.RelPath, true) + if err != nil { + return nil, err + } + dir, err := dest.openParent(p.ExpectedPath) + if err != nil { + return nil, err + } + defer dir.Close() + if err := ctx.Err(); err != nil { + return nil, err + } + if dest.path == dest.parent { + return struct{}{}, nil + } + err = mkdirUploadAt(dir, filepath.Base(dest.path)) + if errors.Is(err, os.ErrExist) { + _, err = uploadTarget(dest.path, true) + } + if err != nil { + return nil, mapWriteError(err, dest.path) + } + return struct{}{}, nil +} + +func uploadTarget(path string, directory bool) (os.FileInfo, error) { + info, err := os.Lstat(path) + if errors.Is(err, os.ErrNotExist) && !directory { + return nil, nil + } + if err != nil { + return nil, mapWriteError(err, path) + } + if info.Mode()&os.ModeSymlink != 0 { + return nil, FSError{Code: CodeOutOfWorkspace, Path: "the destination is a symbolic link"} + } + if directory && !info.IsDir() { + return nil, proto.ProtocolError("the destination is not a folder") + } + if !directory && !info.Mode().IsRegular() { + return nil, proto.ProtocolError("the destination is not a regular file") + } + return info, nil +} + +func (s *Service) beginUpload(ctx context.Context, p uploadParams) (_ any, err error) { + if !uploadIDPattern.MatchString(p.UploadID) || p.Size < 0 || p.Size > maxUploadSize { + return nil, proto.ProtocolError("invalid upload ID or size") + } + s.uploadMu.Lock() + if s.uploadClosed { + s.uploadMu.Unlock() + return nil, proto.NewError(proto.CodeUnavailable, "the filesystem agent is shutting down") + } + if len(s.uploads) >= maxUploads { + s.uploadMu.Unlock() + return nil, proto.NewError(proto.CodeUnavailable, "the host is busy writing files") + } + if s.uploads[p.UploadID] != nil { + s.uploadMu.Unlock() + return nil, proto.ProtocolError("the upload ID is already in use") + } + uploadCtx, cancel := context.WithCancel(ctx) + u := &fileUpload{owner: ctx, ctx: uploadCtx, cancel: cancel, relPath: p.RelPath, size: p.Size} + u.mu.Lock() + s.uploads[p.UploadID] = u + s.uploadMu.Unlock() + defer u.mu.Unlock() + defer func() { + if err != nil { + s.removeUpload(p.UploadID, u) + } + }() + u.stop = context.AfterFunc(ctx, func() { s.abortPendingUpload(p.UploadID, u) }) + u.dest, err = s.uploadDestination(p.RelPath, false) + if err != nil { + return nil, err + } + u.dir, err = u.dest.openParent(p.ExpectedPath) + if err != nil { + return nil, err + } + if _, err = uploadTarget(u.dest.path, false); err != nil { + return nil, err + } + removeStaleUploads(u.ctx, u.dir, filepath.Base(u.dest.path)) + if err = s.reserveUpload(u); err != nil { + return nil, err + } + if err = u.ctx.Err(); err != nil { + return nil, err + } + name := uploadPrefix + p.UploadID + uploadSuffix + u.file, err = createUploadAt(u.dir, name) + if err != nil { + return nil, mapWriteError(err, u.dest.path) + } + u.temp = name + s.touchUpload(p.UploadID, u) + return struct{}{}, nil +} + +// Reap only regular files under the generated name and older than a day. +func removeStaleUploads(ctx context.Context, dir *os.File, target string) { + listing, err := openUploadListing(dir) + if err != nil { + return + } + defer listing.Close() + cutoff := time.Now().Add(-staleUploadAge) + for ctx.Err() == nil { + names, err := listing.Readdirnames(64) + for _, name := range names { + id, hasPrefix := strings.CutPrefix(name, uploadPrefix) + id, hasSuffix := strings.CutSuffix(id, uploadSuffix) + if hasPrefix && hasSuffix && uploadIDPattern.MatchString(id) && !strings.EqualFold(name, target) { + removeStaleUploadAt(dir, name, cutoff) + } + } + if err != nil { + return + } + } +} + +func (s *Service) reserveUpload(u *fileUpload) error { + space, err := availableUploadSpace(u.dir) + if err != nil || space == nil { + return err + } + s.uploadMu.Lock() + defer s.uploadMu.Unlock() + needed := uint64(u.size) + for _, other := range s.uploads { + if other.space != nil && other.space.volume == space.volume { + needed += other.reserved + } + } + if needed > space.free { + return FSError{Code: CodeNoSpace, Path: u.dest.path} + } + u.space = space + u.reserved = uint64(u.size) + return nil +} + +func (s *Service) pendingUpload(ctx context.Context, id string) *fileUpload { + s.uploadMu.Lock() + defer s.uploadMu.Unlock() + if u := s.uploads[id]; u != nil && u.owner == ctx { + return u + } + return nil +} + +// withUpload serializes disk operations for one upload. The room bounds +// parallel pieces and checks their disjoint coverage before relaying them. +func (s *Service) withUpload(ctx context.Context, id string, run func(*fileUpload) error) (any, error) { + u := s.pendingUpload(ctx, id) + if u == nil { + return nil, FSError{Code: CodeNotFound, Path: "no such pending file upload"} + } + u.mu.Lock() + defer u.mu.Unlock() + if err := u.ctx.Err(); err != nil { + return nil, err + } + if err := run(u); err != nil { + s.removeUpload(id, u) + return nil, err + } + return struct{}{}, nil +} + +func (s *Service) chunkUpload(ctx context.Context, p uploadParams) (any, error) { + return s.withUpload(ctx, p.UploadID, func(u *fileUpload) error { + if u.verified || len(p.Data) > base64.StdEncoding.EncodedLen(maxUploadChunk) { + return proto.ProtocolError("the upload is not accepting this piece") + } + data, err := base64.StdEncoding.Strict().DecodeString(p.Data) + size := int64(len(data)) + if err != nil || size == 0 || size > maxUploadChunk || + base64.StdEncoding.EncodeToString(data) != p.Data { + return proto.ProtocolError("the piece has an invalid length or encoding") + } + if p.Offset < 0 || p.Offset > u.size-size || u.received > u.size-size { + return proto.ProtocolError("the piece exceeds the declared file size") + } + if _, err := u.file.WriteAt(data, p.Offset); err != nil { + return mapWriteError(err, u.dest.path) + } + u.received += size + if u.space != nil { + s.uploadMu.Lock() + u.reserved -= uint64(size) + s.uploadMu.Unlock() + } + s.touchUpload(p.UploadID, u) + return nil + }) +} + +// Verify before the desktop checks consent again and asks for the rename. +func (s *Service) verifyUpload(ctx context.Context, p uploadParams) (any, error) { + return s.withUpload(ctx, p.UploadID, func(u *fileUpload) error { + info, err := u.file.Stat() + if err != nil { + return err + } + if u.received != u.size || info.Size() != u.size { + return proto.ProtocolError("the upload did not receive all the declared bytes") + } + hash := sha256.New() + buf := make([]byte, maxUploadChunk) + for offset := int64(0); offset < u.size; { + if err := u.ctx.Err(); err != nil { + return err + } + n, err := u.file.ReadAt(buf[:min(u.size-offset, int64(len(buf)))], offset) + if err != nil { + return err + } + _, _ = hash.Write(buf[:n]) + offset += int64(n) + } + if hex.EncodeToString(hash.Sum(nil)) != p.SHA256 { + return proto.ProtocolError("the upload's SHA-256 did not match") + } + u.verified = true + s.touchUpload(p.UploadID, u) + return nil + }) +} + +func (s *Service) commitUpload(ctx context.Context, p uploadParams) (any, error) { + return s.withUpload(ctx, p.UploadID, func(u *fileUpload) error { + if !u.verified { + return proto.ProtocolError("the upload has not been verified") + } + dest, err := s.uploadDestination(u.relPath, false) + if err != nil { + return err + } + if dest.path != u.dest.path || !os.SameFile(dest.info, u.dest.info) { + return changedUploadDestination() + } + existing, err := uploadTarget(dest.path, false) + if err != nil { + return err + } + if !sameUploadAt(u.dir, u.temp, u.file) { + return FSError{Code: CodeOutOfWorkspace, Path: "the temporary file changed during the upload"} + } + if err := u.ctx.Err(); err != nil { + return err + } + if err := publishUploadAt(u.dir, u.temp, filepath.Base(dest.path), u.file, existing); err != nil { + return mapWriteError(err, dest.path) + } + u.temp = "" + s.removeUpload(p.UploadID, u) + return nil + }) +} + +func (s *Service) touchUpload(id string, u *fileUpload) { + u.deadline = time.Now().Add(uploadIdle) + if u.timer == nil { + u.timer = time.AfterFunc(uploadIdle, func() { + u.mu.Lock() + defer u.mu.Unlock() + if u.ctx.Err() != nil { + return + } + if remaining := time.Until(u.deadline); remaining > 0 { + u.timer.Reset(remaining) + } else { + s.removeUpload(id, u) + } + }) + } +} + +func (s *Service) removeUpload(id string, u *fileUpload) { + u.cancel() + if u.stop != nil { + u.stop() + } + if u.timer != nil { + u.timer.Stop() + } + if u.file != nil { + if u.temp != "" && sameUploadAt(u.dir, u.temp, u.file) { + if err := removeUploadAt(u.dir, u.temp); err != nil && !errors.Is(err, os.ErrNotExist) { + slog.Warn("could not remove temporary upload", "path", u.dest.path, "err", err) + } + } + _ = u.file.Close() + u.file = nil + } + if u.dir != nil { + _ = u.dir.Close() + u.dir = nil + } + s.uploadMu.Lock() + if s.uploads[id] == u { + delete(s.uploads, id) + } + s.uploadMu.Unlock() +} + +func (s *Service) abortPendingUpload(id string, u *fileUpload) { + u.cancel() + u.mu.Lock() + defer u.mu.Unlock() + s.removeUpload(id, u) +} + +func (s *Service) abortUpload(ctx context.Context, p uploadParams) (any, error) { + if u := s.pendingUpload(ctx, p.UploadID); u != nil { + s.abortPendingUpload(p.UploadID, u) + } + return struct{}{}, nil +} + +// DropUploads drains uploads owned by a closed connection, or all uploads +// when the service shuts down. Daemon reattach keeps the other services alive. +func (s *Service) DropUploads(owner context.Context) { + s.uploadMu.Lock() + if owner == nil { + s.uploadClosed = true + } + pending := make(map[string]*fileUpload) + for id, u := range s.uploads { + if owner == nil || u.owner == owner { + u.cancel() + pending[id] = u + } + } + s.uploadMu.Unlock() + for id, u := range pending { + s.abortPendingUpload(id, u) + } +} + +func bindUpload(handler func(context.Context, uploadParams) (any, error)) func(context.Context, json.RawMessage) (any, error) { + return func(ctx context.Context, raw json.RawMessage) (any, error) { + var p uploadParams + if err := json.Unmarshal(raw, &p); err != nil { + return nil, proto.ProtocolError("invalid upload parameters") + } + return handler(ctx, p) + } +} diff --git a/internal/fs/upload_other.go b/internal/fs/upload_other.go new file mode 100644 index 000000000..54eaa1e08 --- /dev/null +++ b/internal/fs/upload_other.go @@ -0,0 +1,53 @@ +//go:build !darwin && !linux + +package fs + +import ( + "os" + "path/filepath" + "time" +) + +func openUploadParent(path string) (*os.File, error) { + return os.Open(path) +} + +func openUploadListing(dir *os.File) (*os.File, error) { + return os.Open(dir.Name()) +} + +func removeStaleUploadAt(dir *os.File, name string, cutoff time.Time) { + info, err := os.Lstat(filepath.Join(dir.Name(), name)) + if err != nil || !info.Mode().IsRegular() || !info.ModTime().Before(cutoff) { + return + } + opened, err := dir.Stat() + if err != nil { + return + } + current, err := os.Stat(dir.Name()) + if err == nil && os.SameFile(opened, current) { + _ = removeUploadAt(dir, name) + } +} + +func availableUploadSpace(_ *os.File) (*uploadSpace, error) { + return nil, nil +} + +func mkdirUploadAt(dir *os.File, name string) error { + return os.Mkdir(filepath.Join(dir.Name(), name), 0o777) +} + +func sameUploadAt(dir *os.File, name string, file *os.File) bool { + opened, err := file.Stat() + if err != nil { + return false + } + named, err := os.Lstat(filepath.Join(dir.Name(), name)) + return err == nil && os.SameFile(opened, named) +} + +func removeUploadAt(dir *os.File, name string) error { + return os.Remove(filepath.Join(dir.Name(), name)) +} diff --git a/internal/fs/upload_portable.go b/internal/fs/upload_portable.go new file mode 100644 index 000000000..4df706484 --- /dev/null +++ b/internal/fs/upload_portable.go @@ -0,0 +1,16 @@ +//go:build !darwin && !linux && !windows + +package fs + +import ( + "os" + "path/filepath" +) + +func createUploadAt(dir *os.File, name string) (*os.File, error) { + return os.OpenFile(filepath.Join(dir.Name(), name), os.O_CREATE|os.O_EXCL|os.O_RDWR, 0o600) +} + +func publishUploadAt(dir *os.File, temp, target string, _ *os.File, _ os.FileInfo) error { + return os.Rename(filepath.Join(dir.Name(), temp), filepath.Join(dir.Name(), target)) +} diff --git a/internal/fs/upload_unix.go b/internal/fs/upload_unix.go new file mode 100644 index 000000000..53c7a9eb3 --- /dev/null +++ b/internal/fs/upload_unix.go @@ -0,0 +1,90 @@ +//go:build darwin || linux + +package fs + +import ( + "os" + "path/filepath" + "time" + + "golang.org/x/sys/unix" +) + +// Capture the inherited mask during package initialization, before requests +// can create files. Chmod at publish time must apply the mask explicitly. +var newUploadMode = func() os.FileMode { + mask := unix.Umask(0o777) + unix.Umask(mask) + return 0o666 &^ os.FileMode(mask) +}() + +func openUploadParent(path string) (*os.File, error) { + return openCanonicalPath(path, true) +} + +// The upload's parent handle only has search access. Open a listing +// relative to it so a path rename cannot redirect the sweep. +func openUploadListing(dir *os.File) (*os.File, error) { + fd, err := unix.Openat(int(dir.Fd()), ".", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) + if err != nil { + return nil, err + } + return os.NewFile(uintptr(fd), dir.Name()), nil +} + +func removeStaleUploadAt(dir *os.File, name string, cutoff time.Time) { + var info unix.Stat_t + if unix.Fstatat(int(dir.Fd()), name, &info, unix.AT_SYMLINK_NOFOLLOW) == nil && + info.Mode&unix.S_IFMT == unix.S_IFREG && + time.Unix(int64(info.Mtim.Sec), int64(info.Mtim.Nsec)).Before(cutoff) { + _ = removeUploadAt(dir, name) + } +} + +func availableUploadSpace(dir *os.File) (*uploadSpace, error) { + fd := int(dir.Fd()) + var stat unix.Stat_t + if err := unix.Fstat(fd, &stat); err != nil { + return nil, err + } + var space unix.Statfs_t + if err := unix.Fstatfs(fd, &space); err != nil { + return nil, err + } + return &uploadSpace{volume: uint64(stat.Dev), free: uint64(space.Bavail) * uint64(space.Bsize)}, nil +} + +func createUploadAt(dir *os.File, name string) (*os.File, error) { + fd, err := unix.Openat(int(dir.Fd()), name, unix.O_CREAT|unix.O_EXCL|unix.O_RDWR|unix.O_CLOEXEC, 0o600) + if err != nil { + return nil, err + } + return os.NewFile(uintptr(fd), filepath.Join(dir.Name(), name)), nil +} + +func mkdirUploadAt(dir *os.File, name string) error { + return unix.Mkdirat(int(dir.Fd()), name, 0o777) +} + +func sameUploadAt(dir *os.File, name string, file *os.File) bool { + var opened, named unix.Stat_t + return unix.Fstat(int(file.Fd()), &opened) == nil && + unix.Fstatat(int(dir.Fd()), name, &named, unix.AT_SYMLINK_NOFOLLOW) == nil && + opened.Dev == named.Dev && opened.Ino == named.Ino +} + +func removeUploadAt(dir *os.File, name string) error { + return unix.Unlinkat(int(dir.Fd()), name, 0) +} + +func publishUploadAt(dir *os.File, temp, target string, file *os.File, existing os.FileInfo) error { + mode := newUploadMode + if existing != nil { + mode = existing.Mode() & (os.ModePerm | os.ModeSetuid | os.ModeSetgid | os.ModeSticky) + } + if err := file.Chmod(mode); err != nil { + return err + } + fd := int(dir.Fd()) + return unix.Renameat(fd, temp, fd, target) +} diff --git a/internal/fs/upload_windows.go b/internal/fs/upload_windows.go new file mode 100644 index 000000000..aaaf53689 --- /dev/null +++ b/internal/fs/upload_windows.go @@ -0,0 +1,31 @@ +package fs + +import ( + "os" + "path/filepath" + + "golang.org/x/sys/windows" +) + +func createUploadAt(dir *os.File, name string) (*os.File, error) { + path := filepath.Join(dir.Name(), name) + p, err := windows.UTF16PtrFromString(path) + if err != nil { + return nil, err + } + // Permit rename/unlink while holding the identity of the temporary file. + handle, err := windows.CreateFile(p, windows.GENERIC_READ|windows.GENERIC_WRITE, + windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, + nil, windows.CREATE_NEW, windows.FILE_ATTRIBUTE_NORMAL, 0) + if err != nil { + return nil, err + } + return os.NewFile(uintptr(handle), path), nil +} + +// Path-based operations assume openUploadParent's os.Open holds the parent +// without FILE_SHARE_DELETE, preventing its name from being swapped while open. +func publishUploadAt(dir *os.File, temp, target string, _ *os.File, _ os.FileInfo) error { + // Windows uses inherited ACLs and the Go rename replaces existing files. + return os.Rename(filepath.Join(dir.Name(), temp), filepath.Join(dir.Name(), target)) +} diff --git a/internal/fs/watch.go b/internal/fs/watch.go index 9d3b965ae..bc67cefff 100644 --- a/internal/fs/watch.go +++ b/internal/fs/watch.go @@ -10,6 +10,7 @@ import ( "time" "github.com/fsnotify/fsnotify" + "github.com/nexus-code/nexus-code/internal/filewatch" "github.com/nexus-code/nexus-code/internal/proto" ) @@ -149,6 +150,13 @@ func (s *Service) Unwatch(ctx context.Context, raw json.RawMessage) (any, error) return struct{}{}, nil } +// watchOrderKey makes watch and unwatch of the same resolved directory share +// a dispatch queue, even when a caller stops waiting before the watch replies. +func (s *Service) watchOrderKey(raw json.RawMessage) string { + path, _ := s.parseWatchPath(raw) + return "fs.watch:" + path +} + func (s *Service) parseWatchPath(raw json.RawMessage) (string, error) { var p WatchParams if len(raw) == 0 || json.Unmarshal(raw, &p) != nil { @@ -246,20 +254,20 @@ func (s *Service) handleWatchEvent(event fsnotify.Event) { s.bufferChange(filepath.ToSlash(rel), kind) } -func changeKind(event fsnotify.Event) (FsChangeKind, bool) { +func changeKind(event fsnotify.Event) (filewatch.FsChangeKind, bool) { switch { case event.Op&(fsnotify.Remove|fsnotify.Rename) != 0: - return FsChangeDeleted, true + return filewatch.FsChangeDeleted, true case event.Op&fsnotify.Create != 0: - return FsChangeAdded, true + return filewatch.FsChangeAdded, true case event.Op&fsnotify.Write != 0: - return FsChangeModified, true + return filewatch.FsChangeModified, true default: return "", false } } -func (s *Service) bufferChange(relPath string, kind FsChangeKind) { +func (s *Service) bufferChange(relPath string, kind filewatch.FsChangeKind) { s.mu.Lock() defer s.mu.Unlock() @@ -277,19 +285,15 @@ func (s *Service) flushChanges() { s.mu.Unlock() return } - changes := make([]FsChange, 0, len(s.buffers)) + changes := make([]filewatch.FsChange, 0, len(s.buffers)) for relPath, kind := range s.buffers { - changes = append(changes, FsChange{RelPath: relPath, Kind: kind}) + changes = append(changes, filewatch.FsChange{RelPath: relPath, Kind: kind}) } clear(s.buffers) sink := s.sink s.mu.Unlock() if sink != nil { - _ = sink("fs.changed", FsChangedPayload{Changes: changes}) + _ = sink("fs.changed", filewatch.FsChangedPayload{Changes: changes}) } } - -func hasDotDotPrefix(rel string) bool { - return rel == ".." || len(rel) > 3 && rel[:3] == ".."+string(filepath.Separator) -} diff --git a/internal/fs/watch_test.go b/internal/fs/watch_test.go index fe1577273..8f4402449 100644 --- a/internal/fs/watch_test.go +++ b/internal/fs/watch_test.go @@ -8,6 +8,8 @@ import ( "path/filepath" "testing" "time" + + "github.com/nexus-code/nexus-code/internal/filewatch" ) // A watched directory that is deleted and recreated at the same path must be @@ -27,9 +29,9 @@ func TestWatchRecreatedDirectoryIsWatchedAgain(t *testing.T) { } defer fsys.Close() - changes := make(chan FsChange, 64) + changes := make(chan filewatch.FsChange, 64) fsys.SetEventSink(func(event string, payload any) error { - if p, ok := payload.(FsChangedPayload); ok && event == "fs.changed" { + if p, ok := payload.(filewatch.FsChangedPayload); ok && event == "fs.changed" { for _, c := range p.Changes { changes <- c } @@ -184,10 +186,10 @@ func watchParams(rel string) json.RawMessage { return raw } -func collectChanges(fsys *Service) <-chan FsChange { - changes := make(chan FsChange, 1024) +func collectChanges(fsys *Service) <-chan filewatch.FsChange { + changes := make(chan filewatch.FsChange, 1024) fsys.SetEventSink(func(event string, payload any) error { - if p, ok := payload.(FsChangedPayload); ok && event == "fs.changed" { + if p, ok := payload.(filewatch.FsChangedPayload); ok && event == "fs.changed" { for _, c := range p.Changes { changes <- c } @@ -199,7 +201,7 @@ func collectChanges(fsys *Service) <-chan FsChange { // waitForChanges waits until every relPath in want has been reported and // returns every relPath seen up to then. -func waitForChanges(t *testing.T, changes <-chan FsChange, want ...string) map[string]bool { +func waitForChanges(t *testing.T, changes <-chan filewatch.FsChange, want ...string) map[string]bool { t.Helper() seen := make(map[string]bool) deadline := time.After(5 * time.Second) diff --git a/internal/fs/write_test.go b/internal/fs/write_test.go index 5444a280c..487dfea3b 100644 --- a/internal/fs/write_test.go +++ b/internal/fs/write_test.go @@ -2,6 +2,7 @@ package fs import ( "context" + "crypto/sha256" "encoding/json" "fmt" "os" @@ -9,6 +10,7 @@ import ( "runtime" "strings" "testing" + "time" "github.com/nexus-code/nexus-code/internal/dispatch" "github.com/nexus-code/nexus-code/internal/proto" @@ -713,3 +715,197 @@ func TestWriteFile_HardLinkWrittenInPlace(t *testing.T) { t.Fatalf("hard link content %q, want v2", got) } } + +func TestUploadPathEscape(t *testing.T) { + for _, kind := range []string{"relative", "absolute", "parent link", "destination link", "replaced parent"} { + t.Run(kind, func(t *testing.T) { + if runtime.GOOS == "windows" && strings.Contains(kind, "link") { + t.Skip("creating symlinks requires privileges on Windows") + } + if kind == "replaced parent" && runtime.GOOS != "linux" && runtime.GOOS != "darwin" { + t.Skip("directory handles anchor the temporary file on Linux and macOS") + } + root, outside := t.TempDir(), t.TempDir() + s, err := New(root) + if err != nil { + t.Fatal(err) + } + t.Cleanup(s.Close) + ctx := context.Background() + p := uploadParams{UploadID: "a0000000-0000-4000-8000-000000000000"} + switch kind { + case "relative": + p.RelPath = "../outside" + case "absolute": + p.RelPath = filepath.Join(outside, "file") + case "parent link", "destination link": + if err := os.Symlink(outside, filepath.Join(root, "link")); err != nil { + t.Fatal(err) + } + p.RelPath = "link" + if kind == "parent link" { + p.RelPath += "/file" + } + case "replaced parent": + parent := filepath.Join(root, "nested") + moved := filepath.Join(root, "moved") + if err := os.Mkdir(parent, 0o700); err != nil { + t.Fatal(err) + } + p.RelPath = "nested/file" + dest, err := s.uploadDestination(p.RelPath, false) + if err != nil { + t.Fatal(err) + } + p.ExpectedPath = dest.path + if _, err := s.beginUpload(ctx, p); err != nil { + t.Fatal(err) + } + if err := os.Rename(parent, moved); err != nil { + t.Fatal(err) + } + if err := os.Symlink(outside, parent); err != nil { + t.Fatal(err) + } + p.SHA256 = fmt.Sprintf("%x", sha256.Sum256(nil)) + if _, err := s.verifyUpload(ctx, p); err != nil { + t.Fatal(err) + } + _, err = s.commitUpload(ctx, p) + if proto.ErrorCode(err) != CodeOutOfWorkspace { + t.Fatalf("committing through a replaced parent: %v", err) + } + entries, err := os.ReadDir(moved) + if err != nil || len(entries) != 0 { + t.Fatalf("temporary file in moved directory: %v, %v", entries, err) + } + } + if kind != "replaced parent" { + dest, err := s.uploadDestination(p.RelPath, false) + if err == nil { + p.ExpectedPath = dest.path + _, err = s.beginUpload(ctx, p) + } + if proto.ErrorCode(err) != CodeOutOfWorkspace { + t.Fatalf("upload destination escaped: %v", err) + } + } + entries, err := os.ReadDir(outside) + if err != nil || len(entries) != 0 { + t.Fatalf("upload changed the outside directory: %v, %v", entries, err) + } + }) + } +} + +func TestUploadCleanup(t *testing.T) { + for _, kind := range []string{"verified commit", "hash mismatch", "abort", "disconnect"} { + t.Run(kind, func(t *testing.T) { + root := t.TempDir() + s, err := New(root) + if err != nil { + t.Fatal(err) + } + t.Cleanup(s.Close) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + p := uploadParams{UploadID: "b0000000-0000-4000-8000-000000000000", RelPath: "data", Size: 3} + dest, err := s.uploadDestination(p.RelPath, false) + if err != nil { + t.Fatal(err) + } + p.ExpectedPath = dest.path + if err := os.WriteFile(dest.path, []byte("old"), 0o600); err != nil { + t.Fatal(err) + } + if _, err := s.beginUpload(ctx, p); err != nil { + t.Fatal(err) + } + for _, piece := range []struct { + offset int64 + base64 string + }{{1, "ZXc="}, {0, "bg=="}} { + p.Offset, p.Data = piece.offset, piece.base64 + if _, err := s.chunkUpload(ctx, p); err != nil { + t.Fatal(err) + } + } + want := "old" + switch kind { + case "verified commit": + p.SHA256 = fmt.Sprintf("%x", sha256.Sum256([]byte("new"))) + if _, err := s.verifyUpload(ctx, p); err != nil { + t.Fatal(err) + } + if _, err := s.commitUpload(ctx, p); err != nil { + t.Fatal(err) + } + want = "new" + if runtime.GOOS != "windows" { + info, err := os.Stat(dest.path) + if err != nil || info.Mode().Perm() != 0o600 { + t.Fatalf("destination mode changed: %v, %v", info, err) + } + } + case "hash mismatch": + p.SHA256 = fmt.Sprintf("%x", sha256.Sum256([]byte("bad"))) + if _, err := s.verifyUpload(ctx, p); err == nil || !strings.Contains(err.Error(), "SHA-256 did not match") { + t.Fatalf("verification error: %v", err) + } + case "abort": + if _, err := s.abortUpload(ctx, p); err != nil { + t.Fatal(err) + } + case "disconnect": + cancel() + s.DropUploads(ctx) + } + if entries, err := os.ReadDir(root); err != nil || len(entries) != 1 { + t.Fatalf("temporary upload remains: %v, %v", entries, err) + } + if data, err := os.ReadFile(dest.path); err != nil || string(data) != want { + t.Fatalf("destination changed: %q, %v", data, err) + } + }) + } +} + +func TestBeginUploadRemovesStaleTemporaryFiles(t *testing.T) { + for _, test := range []struct { + kind, name string + kept bool + }{ + {"abandoned file", uploadPrefix + "a0000000-0000-4000-8000-000000000000" + uploadSuffix, false}, + {"recent file", uploadPrefix + "b0000000-0000-4000-8000-000000000000" + uploadSuffix, true}, + {"unrelated file", uploadPrefix + "not-an-upload" + uploadSuffix, true}, + {"matching directory", uploadPrefix + "c0000000-0000-4000-8000-000000000000" + uploadSuffix, true}, + } { + t.Run(test.kind, func(t *testing.T) { + root := t.TempDir() + entry := filepath.Join(root, test.name) + if test.kind == "matching directory" { + must(t, os.Mkdir(entry, 0o700)) + } else { + must(t, os.WriteFile(entry, nil, 0o600)) + } + if test.kind != "recent file" { + old := time.Now().Add(-25 * time.Hour) + must(t, os.Chtimes(entry, old, old)) + } + s, err := New(root) + must(t, err) + t.Cleanup(s.Close) + dest, err := s.uploadDestination("data", false) + must(t, err) + _, err = s.beginUpload(context.Background(), uploadParams{ + UploadID: "d0000000-0000-4000-8000-000000000000", + RelPath: "data", ExpectedPath: dest.path, + }) + must(t, err) + _, err = os.Lstat(entry) + if test.kept && err != nil || !test.kept && !os.IsNotExist(err) { + t.Fatalf("expected kept=%v, got stat error %v", test.kept, err) + } + }) + } +} diff --git a/internal/git/blob.go b/internal/git/blob.go index 4eb7670e8..6b11225dd 100644 --- a/internal/git/blob.go +++ b/internal/git/blob.go @@ -2,7 +2,6 @@ package git import ( "bufio" - "bytes" "context" "encoding/base64" "encoding/json" @@ -76,13 +75,10 @@ func (s *Service) Blob(ctx context.Context, raw json.RawMessage) (any, error) { return nil, err } cmd.Stdin = strings.NewReader(objectSpec + "\n") - stdout, err := cmd.StdoutPipe() - if err != nil { - return nil, err - } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr - if err := cmd.Start(); err != nil { + stdout, finish, err := startGitOutput(cmdCtx, cmd) + if err != nil { return nil, mapGitStartError(err, []string{"cat-file", "--batch"}) } @@ -99,7 +95,7 @@ func (s *Service) Blob(ctx context.Context, raw json.RawMessage) (any, error) { if readErr != nil || result.Truncated { cancel() } - waitErr := cmd.Wait() + waitErr := finish() if readErr != nil { return nil, readErr } @@ -109,7 +105,7 @@ func (s *Service) Blob(ctx context.Context, raw json.RawMessage) (any, error) { if result.Truncated && cmdCtx.Err() != nil { return result, nil } - code, fatal := gitExitCode(waitErr) + code, fatal := gitExitCode(cmd.ProcessState, waitErr) if fatal != nil { return nil, fatal } diff --git a/internal/git/branch.go b/internal/git/branch.go index 90462e0a5..998dd9e2d 100644 --- a/internal/git/branch.go +++ b/internal/git/branch.go @@ -1,9 +1,9 @@ package git import ( - "bytes" "context" "encoding/json" + "errors" "fmt" "strings" @@ -140,14 +140,14 @@ func (s *Service) BranchDelete(ctx context.Context, raw json.RawMessage) (any, e if err != nil { return nil, err } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr runErr := cmd.Run() if ctxErr := ctx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(runErr) + code, fatal := gitExitCode(cmd.ProcessState, runErr) if fatal != nil { return nil, fatal } @@ -421,12 +421,15 @@ func (s *Service) branchListOutput(ctx context.Context, cwd string, remotes bool if err != nil { return "", err } - var stdout bytes.Buffer - cmd.Stdout = &stdout - if runErr := cmd.Run(); runErr != nil { + stdout, _, code, err := runCapturedCommand(ctx, cmd) + var outputFailure Failure + if errors.As(err, &outputFailure) && outputFailure.Kind == KindOutputTooLarge { + return "", err + } + if err != nil || code != 0 { return "", nil // non-fatal fallthrough } - return stdout.String(), nil + return stdout, nil } // normalizeBranchFetchRef converts an upstream-style ref (e.g. "origin/main") diff --git a/internal/git/classify.go b/internal/git/classify.go index 8d18f1ec3..ca5a58f0b 100644 --- a/internal/git/classify.go +++ b/internal/git/classify.go @@ -4,6 +4,8 @@ import ( "fmt" "strconv" "strings" + + "github.com/nexus-code/nexus-code/internal/redact" ) // MessageContext supplies process details used when stderr is empty. @@ -190,7 +192,7 @@ func HintForKind(kind Kind) *ActionHint { func MessageForKind(kind Kind, ctx MessageContext) string { trimmed := strings.TrimSpace(ctx.Stderr) if trimmed != "" { - return trimmed + return redact.Credentials(trimmed) } if kind == KindOutputTooLarge && ctx.LimitBytes > 0 { return fmt.Sprintf("Git output exceeded %s limit", formatBytes(ctx.LimitBytes)) @@ -317,7 +319,11 @@ func MessageForKind(kind Kind, ctx MessageContext) string { // renderGitCommand formats a compact command label without environment values. func renderGitCommand(args []string) string { - return strings.Join(append([]string{"git"}, args...), " ") + safe := []string{"git"} + for _, arg := range args { + safe = append(safe, redact.Argument(arg)) + } + return redact.Credentials(strings.Join(safe, " ")) } // formatBytes converts a byte count to the same small MB label used by TS. diff --git a/internal/git/classify_test.go b/internal/git/classify_test.go index d6344a039..fc7c12f2d 100644 --- a/internal/git/classify_test.go +++ b/internal/git/classify_test.go @@ -3,11 +3,16 @@ package git import ( "context" "encoding/json" + "errors" + "net" "os" "path/filepath" "reflect" + "runtime" "sort" + "strings" "testing" + "time" ) type expectedClassifiedError struct { @@ -64,10 +69,15 @@ func TestClassifyStderrFixtures(t *testing.T) { // TestRunStdoutOverflowIsClassifiedData guards the run.go transport envelope. func TestRunStdoutOverflowIsClassifiedData(t *testing.T) { - t.Parallel() service := New(t.TempDir()) - raw := json.RawMessage(`{"args":["--no-pager","help","-a"],"stdoutCapBytes":1024}`) - result, err := service.Run(context.Background(), raw) + var result any + var err error + if runtime.GOOS == "windows" { + result, err = runOverflowingGitLauncher(t, service) + } else { + setFakeGitExecutable(t, "output-budget") + result, err = service.Run(context.Background(), json.RawMessage(`{"args":["bulk","1025"],"stdoutCapBytes":1024}`)) + } if err != nil { t.Fatalf("Run returned transport error: %v", err) } @@ -86,6 +96,55 @@ func TestRunStdoutOverflowIsClassifiedData(t *testing.T) { } } +// runOverflowingGitLauncher keeps the child alive even after its output +// pipe closes. Run must stop the child before the checkout can be removed. +func runOverflowingGitLauncher(t *testing.T, service *Service) (any, error) { + t.Helper() + setupInheritedPipeFixture(t) + listener := listenForPipeHolder(t) + ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second) + defer cancel() + type response struct { + value any + err error + } + done := make(chan response, 1) + go func() { + value, err := service.Run(ctx, json.RawMessage(`{"args":["overflow"],"stdoutCapBytes":1024}`)) + done <- response{value, err} + }() + conn, err := listener.AcceptTCP() + if err != nil { + t.Fatal("Git child never connected:", err) + } + defer conn.Close() + // Let the child exceed the output limit once it holds the working directory. + if _, err := conn.Write([]byte{1}); err != nil { + t.Fatal(err) + } + select { + case reply := <-done: + if reply.err != nil { + return nil, reply.err + } + if err := os.Remove(service.root); err != nil { + t.Fatalf("Git's working directory is still in use after Run: %v", err) + } + if err := conn.SetReadDeadline(time.Now().Add(time.Second)); err != nil { + t.Fatal(err) + } + n, err := conn.Read(make([]byte, 1)) + var networkErr net.Error + if n != 0 || err == nil || (errors.As(err, &networkErr) && networkErr.Timeout()) { + t.Fatalf("overflow left Git's child running: read %d bytes, error %v", n, err) + } + return reply.value, nil + case <-ctx.Done(): + t.Fatal("git.run did not return after overflow") + return nil, nil + } +} + // hintMap marshals a Go hint through JSON so its shape matches fixture JSON. func hintMap(t *testing.T, hint *ActionHint) map[string]interface{} { t.Helper() @@ -117,3 +176,17 @@ func normalizeMap(value map[string]interface{}) { } } } + +// TestGitFailureMasksCredentials checks both stderr and the argv fallback. +func TestGitFailureMasksCredentials(t *testing.T) { + result := buildRunResult("", "fatal: https://user:fake-password@example.test/a?token=fake-query&ok=1 Authorization: Bearer fake-auth API_KEY=\"fake with space\" APP_TOKEN=fake-token APP_SECRET=fake-secret", 1, nil) + for _, got := range []string{result.Stderr, result.ErrorMessage} { + if strings.Contains(got, "fake") || !strings.Contains(got, "example.test/a?token=") || !strings.Contains(got, "&ok=1") { + t.Errorf("unexpected Git diagnostic %q", got) + } + } + got := buildRunResult("", "", 128, []string{"fetch", "https://user:fake-argv@example.test/a"}).ErrorMessage + if strings.Contains(got, "fake") || !strings.Contains(got, "git fetch") || !strings.Contains(got, "exited with code 128") { + t.Errorf("unexpected Git command diagnostic %q", got) + } +} diff --git a/internal/git/commit_detail.go b/internal/git/commit_detail.go index d7a9199a9..cbcf099f7 100644 --- a/internal/git/commit_detail.go +++ b/internal/git/commit_detail.go @@ -1,7 +1,6 @@ package git import ( - "bytes" "context" "encoding/json" "strings" @@ -91,22 +90,14 @@ func (s *Service) commitDetailGitOutput(ctx context.Context, params CommitDetail if err != nil { return nil, err } - var stdout bytes.Buffer - var stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - err = cmd.Run() - if ctxErr := ctx.Err(); ctxErr != nil { - return nil, ctxErr - } - code, fatal := gitExitCode(err) + stdout, stderr, code, fatal := runCapturedCommand(ctx, cmd) if fatal != nil { return nil, fatal } if code != 0 { - return nil, commitDetailGitError(stderr.String()) + return nil, commitDetailGitError(stderr) } - return stdout.Bytes(), nil + return []byte(stdout), nil } func commitDetailGitError(stderr string) error { diff --git a/internal/git/diff.go b/internal/git/diff.go index fada06121..a65a4f0af 100644 --- a/internal/git/diff.go +++ b/internal/git/diff.go @@ -58,14 +58,10 @@ func (s *Service) Diff(ctx context.Context, raw json.RawMessage) (any, error) { if err != nil { return nil, err } - stdout, err := cmd.StdoutPipe() - if err != nil { - return nil, err - } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr - - if err := cmd.Start(); err != nil { + stdout, finish, err := startGitOutput(streamCtx, cmd) + if err != nil { return nil, mapGitStartError(err, diffArgs(params)) } result, readErr := s.emitDiffChunks(streamCtx, params.StreamID, stdout, params.MaxChunkBytes, params.MaxBytes) @@ -73,14 +69,14 @@ func (s *Service) Diff(ctx context.Context, raw json.RawMessage) (any, error) { // Stop git before waiting; it would block on a full stdout pipe. cancel() } - waitErr := cmd.Wait() + waitErr := finish() if readErr != nil && !errors.Is(readErr, context.Canceled) { return nil, readErr } if ctxErr := streamCtx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(waitErr) + code, fatal := gitExitCode(cmd.ProcessState, waitErr) if fatal != nil { return nil, fatal } diff --git a/internal/git/errors.go b/internal/git/errors.go index a16ad650b..f4fbc1201 100644 --- a/internal/git/errors.go +++ b/internal/git/errors.go @@ -1,6 +1,9 @@ package git -import "github.com/nexus-code/nexus-code/internal/proto" +import ( + "github.com/nexus-code/nexus-code/internal/proto" + "github.com/nexus-code/nexus-code/internal/redact" +) // Kind is the Go mirror of src/shared/types/git.ts GitErrorKindSchema. type Kind string @@ -99,7 +102,7 @@ type failureData struct { // classifiedFailure builds the Failure for a git exit already classified as // kind, with kind's recovery hint. func classifiedFailure(kind Kind, msg string) error { - return Failure{Kind: kind, Msg: msg, Hint: HintForKind(kind)} + return Failure{Kind: kind, Msg: redact.Credentials(msg), Hint: HintForKind(kind)} } // ClassifiedError is the external Go surface for git stderr classification. diff --git a/internal/git/ignore.go b/internal/git/ignore.go index de24069a2..04a2d9b96 100644 --- a/internal/git/ignore.go +++ b/internal/git/ignore.go @@ -69,16 +69,34 @@ func readExistingGitignore(ignorePath string) (string, error) { return string(buf), nil } +// atomicWriteText retains an existing file's mode while replacing its contents. func atomicWriteText(targetPath string, content string) error { + mode := os.FileMode(0o644) + info, err := os.Stat(targetPath) + if err == nil { + mode = info.Mode() + } else if !errors.Is(err, iofs.ErrNotExist) { + return err + } + tmpPath := targetPath + "." + time.Now().UTC().Format("20060102150405.000000000") + ".tmp" - if err := os.WriteFile(tmpPath, []byte(content), 0o644); err != nil { + file, err := os.OpenFile(tmpPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode) + if err != nil { return err } - if err := os.Rename(tmpPath, targetPath); err != nil { - _ = os.Remove(tmpPath) + defer os.Remove(tmpPath) + _, writeErr := file.WriteString(content) + closeErr := file.Close() + if err := errors.Join(writeErr, closeErr); err != nil { return err } - return nil + if info != nil { + // OpenFile applied the umask. A replacement keeps the previous mode. + if err := os.Chmod(tmpPath, mode); err != nil { + return err + } + } + return os.Rename(tmpPath, targetPath) } func containsIgnorePattern(content string, pattern string) bool { diff --git a/internal/git/ignore_test.go b/internal/git/ignore_test.go new file mode 100644 index 000000000..18dd69590 --- /dev/null +++ b/internal/git/ignore_test.go @@ -0,0 +1,37 @@ +package git + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "runtime" + "strings" + "testing" +) + +// TestAddToGitignorePreservesMode guards replacements of private ignore files. +func TestAddToGitignorePreservesMode(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Windows only represents the owner write bit through chmod") + } + root := t.TempDir() + path := filepath.Join(root, ".gitignore") + if err := os.WriteFile(path, []byte("existing.log\n"), 0o600); err != nil { + t.Fatal(err) + } + _, err := New(root).AddToGitignore(context.Background(), json.RawMessage(`{"relPath":"new.log"}`)) + if err != nil { + t.Fatal(err) + } + info, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if got := info.Mode().Perm(); got != 0o600 { + t.Fatalf("replacement mode is %o, want 600", got) + } + if content := readFile(t, path); !strings.Contains(content, "existing.log\nnew.log\n") { + t.Fatalf("unexpected ignore content %q", content) + } +} diff --git a/internal/git/info.go b/internal/git/info.go index dc0301b7c..a63571d2e 100644 --- a/internal/git/info.go +++ b/internal/git/info.go @@ -1,15 +1,16 @@ package git import ( - "bytes" "context" "encoding/json" "errors" "os" "os/exec" + "path" "path/filepath" "runtime" "strings" + "time" "github.com/nexus-code/nexus-code/internal/proto" ) @@ -93,9 +94,10 @@ func inspectGitCandidate(ctx context.Context, candidate string) (string, string, cmd := exec.CommandContext(ctx, abs, "--version") cmd.Env = append(cmd.Environ(), "GIT_TERMINAL_PROMPT=0") - var stdout bytes.Buffer + stdout := cappedBuffer{cap: defaultStdoutCapBytes} cmd.Stdout = &stdout - if err := cmd.Run(); err != nil { + cmd.WaitDelay = time.Second + if err := runGitProbe(ctx, runtime.GOOS, cmd, (*exec.Cmd).Run); stdout.overflow || err != nil && !errors.Is(err, exec.ErrWaitDelay) { return "", "", false } @@ -181,12 +183,17 @@ func runRevParse(ctx context.Context, cwd string) (topLevel, gitDir string, err ) cmd.Env = withSafeGitConfig(cmd.Env) - var stdout bytes.Buffer - var stderr bytes.Buffer + stdout := cappedBuffer{cap: defaultStdoutCapBytes} + var stderr gitStderr cmd.Stdout = &stdout + cmd.WaitDelay = time.Second cmd.Stderr = &stderr - if runErr := cmd.Run(); runErr != nil { + runErr := runGitProbe(ctx, runtime.GOOS, cmd, (*exec.Cmd).Run) + if stdout.overflow { + return "", "", outputLimitFailure(defaultStdoutCapBytes) + } + if runErr != nil && !errors.Is(runErr, exec.ErrWaitDelay) { return "", "", runErr } @@ -210,6 +217,36 @@ func runRevParse(ctx context.Context, cwd string) (topLevel, gitDir string, err return logical, rebasePath(gd, tl, logical), nil } +// runGitProbe checks Apple's Git launcher for an active developer directory +// before running it. On a fresh Mac starting the launcher itself can prompt +// to install Command Line Tools. Third-party Git works independently. +func runGitProbe(ctx context.Context, goos string, cmd *exec.Cmd, run func(*exec.Cmd) error) error { + if cmd.Err != nil { + return cmd.Err + } + if goos == "darwin" { + const appleGit = "/usr/bin/git" + candidate := path.Clean(filepath.ToSlash(cmd.Path)) + if candidate != appleGit { + if resolved, err := filepath.EvalSymlinks(cmd.Path); err == nil { + candidate = path.Clean(filepath.ToSlash(resolved)) + } + } + if candidate == appleGit { + probe := exec.CommandContext(ctx, "/usr/bin/xcode-select", "--print-path") + probe.Env = cmd.Environ() + if err := run(probe); err != nil { + var exit *exec.ExitError + if ctx.Err() == nil && errors.As(err, &exit) { + return exec.ErrNotFound + } + return err + } + } + } + return run(cmd) +} + // logicalTopLevel returns the repository root git reports — with symlinks // resolved, so /private/tmp/repo on macOS for a workspace opened at /tmp/repo // — as reached from cwd by the path cwd was given in: /tmp/repo. Workspace diff --git a/internal/git/info_test.go b/internal/git/info_test.go index aee091877..04bbe4f2e 100644 --- a/internal/git/info_test.go +++ b/internal/git/info_test.go @@ -3,12 +3,61 @@ package git import ( "context" "encoding/json" + "errors" "os" + "os/exec" "path/filepath" "strings" "testing" ) +// TestGitProbeSkipsTheMacStubWithoutDeveloperTools simulates xcode-select on +// a fresh Mac. The probe must report Git missing before launching the stub. +func TestGitProbeSkipsTheMacStubWithoutDeveloperTools(t *testing.T) { + ctx := context.Background() + for _, args := range [][]string{{"--version"}, {"rev-parse", "--show-toplevel"}} { + cmd := exec.CommandContext(ctx, "/usr/bin/git", args...) + cmd.Env = append(cmd.Environ(), "DEVELOPER_DIR=/missing") + err := runGitProbe(ctx, "darwin", cmd, func(actual *exec.Cmd) error { + if actual.Path != "/usr/bin/xcode-select" || len(actual.Args) != 2 || actual.Args[1] != "--print-path" { + t.Fatalf("unexpected command %q", actual.Args) + } + if !strings.Contains(strings.Join(actual.Env, "\n"), "DEVELOPER_DIR=/missing") { + t.Fatal("probe used a different developer directory") + } + return &exec.ExitError{} + }) + if !errors.Is(err, exec.ErrNotFound) { + t.Fatalf("probe did not report Git missing for %q", args) + } + } +} + +// TestGitProbePreservesRepoErrors verifies that an installed Git's errors +// still reach Detect, including on a Mac using a third-party Git binary. +func TestGitProbePreservesRepoErrors(t *testing.T) { + ctx := context.Background() + notRepo := errors.New("not a git repository") + for _, path := range []string{"/usr/bin/git", "/opt/homebrew/bin/git"} { + cmd := exec.CommandContext(ctx, path, "rev-parse") + var calls []string + err := runGitProbe(ctx, "darwin", cmd, func(actual *exec.Cmd) error { + calls = append(calls, actual.Path) + if actual == cmd { + return notRepo + } + return nil + }) + want := []string{path} + if path == "/usr/bin/git" { + want = append([]string{"/usr/bin/xcode-select"}, path) + } + if strings.Join(calls, ",") != strings.Join(want, ",") || !errors.Is(err, notRepo) { + t.Fatalf("probe %s ran %v and returned %v, want %v and the Git error", path, calls, err, want) + } + } +} + // --------------------------------------------------------------------------- // git.info tests // --------------------------------------------------------------------------- diff --git a/internal/git/log.go b/internal/git/log.go index 834882107..38758251f 100644 --- a/internal/git/log.go +++ b/internal/git/log.go @@ -2,13 +2,11 @@ package git import ( "bufio" - "bytes" "context" "encoding/json" "errors" "io" "strings" - "time" "github.com/nexus-code/nexus-code/internal/proto" ) @@ -94,20 +92,10 @@ func (s *Service) Log(ctx context.Context, raw json.RawMessage) (any, error) { } return cmd.Process.Kill() } - // The git on a Windows PATH is a launcher that runs the real git as its - // child, which the kill leaves running with the output pipes open. Wait - // would wait for those to close while that git waits for room in the - // stdout pipe no one reads any more; this closes them instead. - cmd.WaitDelay = time.Second - - stdout, err := cmd.StdoutPipe() - if err != nil { - return nil, err - } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr - - if err := cmd.Start(); err != nil { + stdout, finish, err := startGitOutput(streamCtx, cmd) + if err != nil { return nil, mapGitStartError(err, args) } @@ -120,7 +108,8 @@ func (s *Service) Log(ctx context.Context, raw json.RawMessage) (any, error) { // Stop git before waiting; it would block on a full stdout pipe. cancel() } - waitErr := cmd.Wait() + // A page limit or sink error can stop the parser before it drains stdout. + waitErr := finish() if readErr != nil && !(killedForLimit && isClosedPipeReadError(readErr)) { return nil, readErr @@ -128,7 +117,7 @@ func (s *Service) Log(ctx context.Context, raw json.RawMessage) (any, error) { if ctxErr := streamCtx.Err(); ctxErr != nil && !killedForLimit { return nil, ctxErr } - code, fatal := gitExitCode(waitErr) + code, fatal := gitExitCode(cmd.ProcessState, waitErr) if fatal != nil { return nil, fatal } @@ -280,6 +269,7 @@ func (s *Service) consumeLogOutput(ctx context.Context, reader io.Reader, params } hasMore := false batch := make([]LogEntry, 0, LogBatchSize) + batchBytes := 0 emitBatch := func() error { if len(batch) == 0 { @@ -287,6 +277,7 @@ func (s *Service) consumeLogOutput(ctx context.Context, reader io.Reader, params } entries := append([]LogEntry(nil), batch...) batch = batch[:0] + batchBytes = 0 return s.emitLogBatch(params.StreamID, entries) } @@ -295,7 +286,10 @@ func (s *Service) consumeLogOutput(ctx context.Context, reader io.Reader, params if err := ctx.Err(); err != nil { return LogResult{}, err } - record, err := scanner.ReadString(logRecordSeparator[0]) + record, err := readLogRecord(scanner) + if errors.Is(err, errGitOutputLimit) { + return LogResult{}, outputLimitFailure(defaultStdoutCapBytes) + } if len(record) > 0 { record = strings.TrimSuffix(record, logRecordSeparator) entry, ok := ParseLogRecord(record, hasSource) @@ -310,7 +304,13 @@ func (s *Service) consumeLogOutput(ctx context.Context, reader io.Reader, params } break } else { + if batchBytes+len(record) > defaultStdoutCapBytes { + if err := emitBatch(); err != nil { + return LogResult{}, err + } + } batch = append(batch, entry) + batchBytes += len(record) count++ if len(batch) >= LogBatchSize { if err := emitBatch(); err != nil { @@ -340,6 +340,21 @@ func (s *Service) consumeLogOutput(ctx context.Context, reader io.Reader, params return result, nil } +// readLogRecord bounds an individual commit record without limiting page scans. +func readLogRecord(reader *bufio.Reader) (string, error) { + var record strings.Builder + for { + part, err := reader.ReadSlice(logRecordSeparator[0]) + if record.Len()+len(part) > defaultStdoutCapBytes { + return "", errGitOutputLimit + } + _, _ = record.Write(part) + if err != bufio.ErrBufferFull { + return record.String(), err + } + } +} + func (s *Service) emitLogBatch(streamID string, entries []LogEntry) error { s.mu.Lock() sink := s.sink diff --git a/internal/git/option_like_test.go b/internal/git/option_like_test.go index fb6c6aa24..984063435 100644 --- a/internal/git/option_like_test.go +++ b/internal/git/option_like_test.go @@ -2,7 +2,6 @@ package git import ( "context" - "encoding/json" "os" "path/filepath" "strings" @@ -56,12 +55,3 @@ func TestRefsThatReadAsOptionsAreRefused(t *testing.T) { }) } } - -func mustJSON(t *testing.T, value any) json.RawMessage { - t.Helper() - raw, err := json.Marshal(value) - if err != nil { - t.Fatal(err) - } - return raw -} diff --git a/internal/git/output.go b/internal/git/output.go new file mode 100644 index 000000000..f00d00076 --- /dev/null +++ b/internal/git/output.go @@ -0,0 +1,120 @@ +package git + +import ( + "context" + "errors" + "io" + "os/exec" + "strings" + + "github.com/nexus-code/nexus-code/internal/redact" +) + +const ( + defaultStdoutCapBytes = 16 << 20 + maxStdoutCapBytes = 64 << 20 + captureTailBytes = 1 << 20 + outputTruncated = "[Git output truncated to the last 1 MiB]\n" +) + +var errGitOutputLimit = errors.New("git stdout budget exceeded") + +// stdoutBudget bounds the caller's requested stdout budget. +func stdoutBudget(requested int64) int64 { + if requested <= 0 { + return defaultStdoutCapBytes + } + return min(requested, maxStdoutCapBytes) +} + +// tailBuffer retains the newest output without reallocating on each write. +type tailBuffer struct { + data []byte + start int + overflow bool +} + +func (b *tailBuffer) Write(p []byte) (int, error) { + n := len(p) + if n >= captureTailBytes { + b.overflow = b.overflow || len(b.data)+n > captureTailBytes + b.data = append(b.data[:0], p[n-captureTailBytes:]...) + b.start = 0 + return n, nil + } + fill := min(n, captureTailBytes-len(b.data)) + b.data = append(b.data, p[:fill]...) + if fill < n { + b.overflow = true + p = p[fill:] + written := copy(b.data[b.start:], p) + b.start = (b.start + written) % captureTailBytes + b.start += copy(b.data[b.start:], p[written:]) + } + return n, nil +} + +// String reconstructs the tail in chronological order. +func (b *tailBuffer) String() string { + return string(b.data[b.start:]) + string(b.data[:b.start]) +} + +// gitStderr bounds and masks diagnostics. When the cap splits a line, its +// remaining suffix can contain a credential whose key was already evicted. +type gitStderr struct{ tailBuffer } + +func (b *gitStderr) String() string { + s := b.tailBuffer.String() + if b.overflow { + _, s, _ = strings.Cut(s, "\n") + return outputTruncated + redact.Credentials(s) + } + return redact.Credentials(s) +} + +// startGitOutput makes stdout an exec-managed pipe so Wait can reap Git and +// enforce WaitDelay while the caller still reads. finish must be called even +// if the caller stops reading early. +func startGitOutput(ctx context.Context, cmd *exec.Cmd) (stdout io.Reader, finish func() error, err error) { + reader, writer := io.Pipe() + cmd.Stdout = writer + if err := cmd.Start(); err != nil { + _ = reader.Close() + _ = writer.Close() + return nil, nil, err + } + stop := context.AfterFunc(ctx, func() { _ = reader.CloseWithError(ctx.Err()) }) + waited := make(chan error, 1) + go func() { + err := cmd.Wait() + _ = writer.Close() + waited <- err + }() + return reader, func() error { + stop() + _ = reader.Close() + return <-waited + }, nil +} + +// runCapturedCommand bounds commands whose stdout is parsed as one value. +func runCapturedCommand(ctx context.Context, cmd *exec.Cmd) (string, string, int, error) { + stdout := cappedBuffer{cap: defaultStdoutCapBytes} + var stderr gitStderr + cmd.Stdout = &stdout + cmd.Stderr = &stderr + runErr := cmd.Run() + if err := ctx.Err(); err != nil { + return "", "", 0, err + } + if stdout.overflow { + return "", "", 0, outputLimitFailure(defaultStdoutCapBytes) + } + code, err := gitExitCode(cmd.ProcessState, runErr) + return stdout.String(), stderr.String(), code, err +} + +// outputLimitFailure identifies a result that could not be captured in full. +func outputLimitFailure(limit int64) error { + return classifiedFailure(KindOutputTooLarge, MessageForKind(KindOutputTooLarge, MessageContext{LimitBytes: limit})) +} diff --git a/internal/git/porcelain_test.go b/internal/git/porcelain_test.go index 631dfb7f9..3493488a7 100644 --- a/internal/git/porcelain_test.go +++ b/internal/git/porcelain_test.go @@ -1,12 +1,15 @@ package git import ( + "bufio" "context" "encoding/json" + "fmt" "os" "path/filepath" "reflect" "sort" + "strings" "testing" ) @@ -136,3 +139,61 @@ func TestStatusIgnoresRepoConfiguredFsmonitorCommand(t *testing.T) { t.Fatal("repo-configured fsmonitor command ran during status") } } + +const largeStatusFiles = 17_000 + +// largeStatusPath gives each fixture record a realistic path. The 17,000 +// NUL-delimited records total more than 2 MiB. +func largeStatusPath(index int) string { + return fmt.Sprintf("%s-%05d.txt", strings.Repeat("f", 125), index) +} + +// runLargeStatusFixture serves porcelain data and the auxiliary status +// commands without creating the untracked files. +func runLargeStatusFixture() int { + if len(os.Args) < 2 { + return 1 + } + switch os.Args[1] { + case "status": + out := bufio.NewWriter(os.Stdout) + for i := range largeStatusFiles { + if _, err := fmt.Fprintf(out, "? %s\x00", largeStatusPath(i)); err != nil { + return 1 + } + } + if err := out.Flush(); err != nil { + return 1 + } + case "rev-parse": + root, err := os.Getwd() + if err != nil { + return 1 + } + _, _ = fmt.Fprintln(os.Stdout, filepath.Join(root, ".git")) + case "remote", "stash", "tag": + // The repository has no remotes, stashes or tags. + default: + return 1 + } + return 0 +} + +// TestStatusPreservesLargePorcelainOutput checks that the status RPC parses +// every entry even when its porcelain output exceeds two MiB. +func TestStatusPreservesLargePorcelainOutput(t *testing.T) { + setFakeGitExecutable(t, "large-status") + result, err := New(t.TempDir()).Status(context.Background(), nil) + if err != nil { + t.Fatal(err) + } + entries := result.(GitStatus).Untracked + if len(entries) != largeStatusFiles { + t.Fatalf("status returned %d untracked files, want %d", len(entries), largeStatusFiles) + } + for i, entry := range entries { + if want := largeStatusPath(i); entry.RelPath != want { + t.Fatalf("status entry %d = %q, want %q", i, entry.RelPath, want) + } + } +} diff --git a/internal/git/remote.go b/internal/git/remote.go index b7c47cabe..29c8cbf09 100644 --- a/internal/git/remote.go +++ b/internal/git/remote.go @@ -1,7 +1,6 @@ package git import ( - "bytes" "context" "encoding/json" "strings" @@ -50,14 +49,14 @@ func (s *Service) RemoteAdd(ctx context.Context, raw json.RawMessage) (any, erro if err != nil { return nil, err } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr err = cmd.Run() if ctxErr := ctx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(err) + code, fatal := gitExitCode(cmd.ProcessState, err) if fatal != nil { return nil, fatal } @@ -90,14 +89,14 @@ func (s *Service) RemoteRemove(ctx context.Context, raw json.RawMessage) (any, e if err != nil { return nil, err } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr err = cmd.Run() if ctxErr := ctx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(err) + code, fatal := gitExitCode(cmd.ProcessState, err) if fatal != nil { return nil, fatal } diff --git a/internal/git/run.go b/internal/git/run.go index 5557eeac8..2ece12bc4 100644 --- a/internal/git/run.go +++ b/internal/git/run.go @@ -12,13 +12,11 @@ import ( "runtime" "strconv" "strings" + "time" "github.com/nexus-code/nexus-code/internal/msix" "github.com/nexus-code/nexus-code/internal/proto" -) - -const ( - defaultStdoutCapBytes = 10 * 1024 * 1024 + "github.com/nexus-code/nexus-code/internal/redact" ) type RunParams struct { @@ -54,45 +52,49 @@ func (s *Service) Run(ctx context.Context, raw json.RawMessage) (any, error) { return nil, err } defer done() - cmd, err := s.command(ctx, params.Args, params.Cwd, params.Env, params.Interactive) + cmdCtx, cancel := context.WithCancel(ctx) + defer cancel() + cmd, err := s.command(cmdCtx, params.Args, params.Cwd, params.Env, params.Interactive) if err != nil { return nil, err } - stdoutCap := params.StdoutCapBytes - if stdoutCap <= 0 { - stdoutCap = defaultStdoutCapBytes - } - var stdout cappedBuffer - stdout.cap = stdoutCap - var stderr bytes.Buffer + stdoutCap := stdoutBudget(params.StdoutCapBytes) + stdout := cappedBuffer{cap: stdoutCap, cancel: cancel} + var stderr gitStderr cmd.Stdout = &stdout cmd.Stderr = &stderr - err = cmd.Run() + err, cleanupErr := runGitWithOutputLimit(cmd, &stdout) + if cleanupErr != nil { + return nil, mapGitStartError(cleanupErr, nil) + } if stdout.overflow { - return RunResult{ - Stdout: "", - Stderr: stderr.String(), - Code: 0, - ErrorKind: KindOutputTooLarge, - ErrorHint: HintForKind(KindOutputTooLarge), - ErrorMessage: MessageForKind(KindOutputTooLarge, MessageContext{Args: params.Args, LimitBytes: stdoutCap}), - }, nil + return outputLimitResult(stderr.String(), stdoutCap), nil } if ctxErr := ctx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(err) + code, fatal := gitExitCode(cmd.ProcessState, err) if fatal != nil { return nil, fatal } return buildRunResult(stdout.String(), stderr.String(), code, params.Args), nil } +// outputLimitResult uses the existing classification for generic Git calls. +func outputLimitResult(stderr string, limit int64) RunResult { + return RunResult{ + Stderr: stderr, + ErrorKind: KindOutputTooLarge, + ErrorHint: HintForKind(KindOutputTooLarge), + ErrorMessage: MessageForKind(KindOutputTooLarge, MessageContext{LimitBytes: limit}), + } +} + // buildRunResult attaches classification fields when Git exits unsuccessfully. func buildRunResult(stdout string, stderr string, code int, args []string) RunResult { - result := RunResult{Stdout: stdout, Stderr: stderr, Code: code} + result := RunResult{Stdout: stdout, Stderr: redact.Credentials(stderr), Code: code} if code == 0 { return result } @@ -121,18 +123,7 @@ func (s *Service) captureEnv(ctx context.Context, cwd string, args []string, env if err != nil { return "", "", 0, err } - var outBuf, errBuf bytes.Buffer - cmd.Stdout = &outBuf - cmd.Stderr = &errBuf - runErr := cmd.Run() - if ctxErr := ctx.Err(); ctxErr != nil { - return "", "", 0, ctxErr - } - code, fatal := gitExitCode(runErr) - if fatal != nil { - return "", "", 0, fatal - } - return outBuf.String(), errBuf.String(), code, nil + return runCapturedCommand(ctx, cmd) } // gitError converts a non-zero git exit into a typed CodedError using the @@ -146,7 +137,7 @@ func gitError(args []string, stderr string, code int) error { message = strings.TrimSpace(stderr) } if message == "" { - message = fmt.Sprintf("git %s exited with code %d", strings.Join(args, " "), code) + message = fmt.Sprintf("%s exited with code %d", renderGitCommand(args), code) } return classifiedFailure(kind, message) } @@ -191,6 +182,7 @@ func (s *Service) command(ctx context.Context, args []string, cwd string, env ma askpass.runID = runIDFrom(ctx) } cmd := exec.CommandContext(ctx, gitBinary(), args...) + cmd.WaitDelay = s.commandWaitDelay cmd.Dir = resolvedCwd cmd.Env = gitEnv(env, interactive, askpass) return cmd, nil @@ -228,11 +220,15 @@ func gitTopLevel(ctx context.Context, root string) (string, error) { cmd := exec.CommandContext(ctx, gitBinary(), "rev-parse", "--show-toplevel", "--show-cdup") cmd.Dir = root cmd.Env = gitEnv(nil, false, commandAskpass{}) - out, err := cmd.Output() + cmd.WaitDelay = time.Second + out, stderr, code, err := runCapturedCommand(ctx, cmd) if err != nil { return "", err } - lines := parseRevParseLines(string(out)) + if code != 0 { + return "", gitError(cmd.Args[1:], stderr, code) + } + lines := parseRevParseLines(out) cdup := "" if len(lines) > 1 { cdup = lines[1] @@ -357,10 +353,15 @@ func appendOrReplaceEnv(env []string, key string, value string) []string { return append(env, prefix+value) } -func gitExitCode(err error) (int, error) { +// gitExitCode separates Git's exit status from command startup and I/O errors. +func gitExitCode(state *os.ProcessState, err error) (int, error) { if err == nil { return 0, nil } + // WaitDelay can close pipes inherited by a helper after Git succeeded. + if errors.Is(err, exec.ErrWaitDelay) && state != nil { + return state.ExitCode(), nil + } var exitErr *exec.ExitError if errors.As(err, &exitErr) { return exitErr.ExitCode(), nil @@ -372,13 +373,14 @@ func mapGitStartError(err error, args []string) error { if errors.Is(err, exec.ErrNotFound) { return classifiedFailure(KindGitMissing, "git executable not found") } - return proto.CodedError{Code: proto.CodeRequestFailed, Msg: err.Error()} + return proto.CodedError{Code: proto.CodeRequestFailed, Msg: redact.Credentials(err.Error())} } type cappedBuffer struct { buf bytes.Buffer cap int64 overflow bool + cancel context.CancelFunc } // String returns the captured stdout prefix. @@ -388,18 +390,13 @@ func (b *cappedBuffer) String() string { // Write records stdout until the configured cap and detects overflow. func (b *cappedBuffer) Write(p []byte) (int, error) { - if b.cap <= 0 { - return b.buf.Write(p) - } - remaining := b.cap - int64(b.buf.Len()) - if remaining <= 0 { - b.overflow = true - return len(p), nil - } - if int64(len(p)) > remaining { + if remaining := b.cap - int64(b.buf.Len()); int64(len(p)) > remaining { b.overflow = true _, _ = b.buf.Write(p[:remaining]) - return len(p), nil + if b.cancel != nil { + b.cancel() + } + return int(remaining), errGitOutputLimit } return b.buf.Write(p) } diff --git a/internal/git/run_other.go b/internal/git/run_other.go new file mode 100644 index 000000000..a5de360cf --- /dev/null +++ b/internal/git/run_other.go @@ -0,0 +1,10 @@ +//go:build !windows + +package git + +import "os/exec" + +// runGitWithOutputLimit runs Git with the configured stdout overflow handler. +func runGitWithOutputLimit(cmd *exec.Cmd, _ *cappedBuffer) (runErr, cleanupErr error) { + return cmd.Run(), nil +} diff --git a/internal/git/run_windows.go b/internal/git/run_windows.go new file mode 100644 index 000000000..fe3bb010e --- /dev/null +++ b/internal/git/run_windows.go @@ -0,0 +1,50 @@ +//go:build windows + +package git + +import ( + "fmt" + "os/exec" + "syscall" + + hostproc "github.com/nexus-code/nexus-code/internal/process" + "golang.org/x/sys/windows" +) + +// runGitWithOutputLimit assigns the launcher before it can spawn Git or a +// helper. The job is terminated on overflow and simply released on success. +func runGitWithOutputLimit(cmd *exec.Cmd, stdout *cappedBuffer) (runErr, cleanupErr error) { + cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: windows.CREATE_SUSPENDED} + jobReady := make(chan struct{}) + var job windows.Handle + cancel := stdout.cancel + stdout.cancel = func() { + <-jobReady + if job != 0 { + cleanupErr = hostproc.TerminateJobAndWait(job, cmd.WaitDelay) + if cleanupErr != nil { + cleanupErr = fmt.Errorf("terminate Git process tree after stdout overflow: %w", cleanupErr) + } + } + cancel() + } + if err := cmd.Start(); err != nil { + return err, nil + } + var err error + job, err = hostproc.AssignJob(cmd.Process.Pid, false) + if err == nil { + defer windows.CloseHandle(job) + err = hostproc.ResumeSuspended(uint32(cmd.Process.Pid)) + } + close(jobReady) + if err != nil { + _ = cmd.Process.Kill() + _ = cmd.Wait() + return fmt.Errorf("start Git in a job: %w", err), nil + } + + // Wait joins the stdout copier, including overflow cleanup. + runErr = cmd.Wait() + return runErr, cleanupErr +} diff --git a/internal/git/service.go b/internal/git/service.go index 6d21e5b11..10b3bff7f 100644 --- a/internal/git/service.go +++ b/internal/git/service.go @@ -1,9 +1,9 @@ package git import ( - "bytes" "context" "encoding/json" + "errors" "fmt" "io" "net" @@ -24,6 +24,8 @@ const maxGitFileContentBytes = 5 * 1024 * 1024 type Service struct { root string + // commandWaitDelay bounds draining output inherited by Git's helpers. + commandWaitDelay time.Duration mu sync.Mutex sink EventSink @@ -52,11 +54,12 @@ func New(root string) *Service { abs = root } return &Service{ - root: filepath.Clean(abs), - streams: make(map[string]context.CancelFunc), - watches: make(map[string]*watchEntry), - gitDirty: make(map[string]struct{}), - askpassPending: make(map[string]*askpassPending), + root: filepath.Clean(abs), + commandWaitDelay: time.Second, + streams: make(map[string]context.CancelFunc), + watches: make(map[string]*watchEntry), + gitDirty: make(map[string]struct{}), + askpassPending: make(map[string]*askpassPending), } } @@ -71,8 +74,11 @@ func Register(d *dispatch.Dispatcher, service *Service) { d.Register("git.diff", service.Diff) d.Register("git.commitDetail", service.CommitDetail) d.Register("git.changeStats", service.ChangeStats) - d.Register("git.watch", service.Watch) - d.Register("git.unwatch", service.Unwatch) + // Git directory resolution can run Git itself. Keep that work out of + // the dispatch scanner and order watch changes on their own lane. + watchQueue := func(json.RawMessage) string { return "git.watch" } + d.RegisterOrderedBy("git.watch", service.Watch, watchQueue) + d.RegisterOrderedBy("git.unwatch", service.Unwatch, watchQueue) d.Register("git.addToGitignore", service.AddToGitignore) d.Register("git.getFileContent", service.GetFileContent) d.Register("git.blob", service.Blob) @@ -158,6 +164,9 @@ func (s *Service) GetFileContent(ctx context.Context, raw json.RawMessage) (any, stdout, stderr, tooLarge, err := runGitShow(ctx, s.root, objectSpec, maxGitFileContentBytes) if err != nil { + if ctx.Err() != nil { + return nil, ctx.Err() + } if isMissing(stderr) { return map[string]any{"kind": "missing", "reason": missingReasonForRef(ref)}, nil } @@ -180,10 +189,11 @@ func (s *Service) GetFileContent(ctx context.Context, raw json.RawMessage) (any, // limit+1 bytes of it: when the blob is larger than limit, git is killed and // tooLarge is reported instead of buffering the whole blob only to reject it. func runGitShow(ctx context.Context, root string, objectSpec string, limit int) (stdout []byte, stderr string, tooLarge bool, err error) { - ctx, cancel := context.WithCancel(ctx) + cmdCtx, cancel := context.WithCancel(ctx) defer cancel() - cmd := exec.CommandContext(ctx, gitBinary(), "show", "--no-ext-diff", objectSpec) + cmd := exec.CommandContext(cmdCtx, gitBinary(), "show", "--no-ext-diff", objectSpec) cmd.Dir = root + cmd.WaitDelay = time.Second cmd.Env = append(cmd.Environ(), "GIT_TERMINAL_PROMPT=0", "GIT_ASKPASS=echo", @@ -192,24 +202,27 @@ func runGitShow(ctx context.Context, root string, objectSpec string, limit int) "GIT_FLUSH=1", ) cmd.Env = withSafeGitConfig(cmd.Env) - var stderrBuf bytes.Buffer + var stderrBuf gitStderr cmd.Stderr = &stderrBuf - pipe, err := cmd.StdoutPipe() + pipe, finish, err := startGitOutput(cmdCtx, cmd) if err != nil { return nil, "", false, err } - if err := cmd.Start(); err != nil { - return nil, "", false, err - } stdout, readErr := io.ReadAll(io.LimitReader(pipe, int64(limit)+1)) if len(stdout) > limit || readErr != nil { // Stop git before waiting; it would block on a full stdout pipe. cancel() } - waitErr := cmd.Wait() + waitErr := finish() + if err := ctx.Err(); err != nil { + return nil, stderrBuf.String(), false, err + } if len(stdout) > limit { return nil, stderrBuf.String(), true, nil } + if errors.Is(waitErr, exec.ErrWaitDelay) { + waitErr = nil + } if waitErr == nil { waitErr = readErr } diff --git a/internal/git/service_test.go b/internal/git/service_test.go index ee4f2250e..9abea0a8c 100644 --- a/internal/git/service_test.go +++ b/internal/git/service_test.go @@ -131,14 +131,14 @@ func TestGetFileContentSizeCap(t *testing.T) { runGit(t, root, "add", "at-cap.txt", "over-cap.txt") service := New(root) - res, err := service.GetFileContent(context.Background(), mustRawJSON(t, FileContentParams{Ref: "INDEX", RelPath: "at-cap.txt"})) + res, err := service.GetFileContent(context.Background(), mustJSON(t, FileContentParams{Ref: "INDEX", RelPath: "at-cap.txt"})) if err != nil { t.Fatalf("blob at the cap: %v", err) } if got := res.(map[string]any); got["kind"] != "ok" || got["sizeBytes"] != maxGitFileContentBytes || len(got["content"].(string)) != maxGitFileContentBytes { t.Fatalf("blob at the cap: kind=%v sizeBytes=%v", got["kind"], got["sizeBytes"]) } - _, err = service.GetFileContent(context.Background(), mustRawJSON(t, FileContentParams{Ref: "INDEX", RelPath: "over-cap.txt"})) + _, err = service.GetFileContent(context.Background(), mustJSON(t, FileContentParams{Ref: "INDEX", RelPath: "over-cap.txt"})) if err == nil || !strings.Contains(err.Error(), "exceeds") { t.Fatalf("blob over the cap: err = %v, want read-limit error", err) } @@ -156,7 +156,7 @@ func TestGetFileContentStopsReadingAtCap(t *testing.T) { done := make(chan error, 1) go func() { - _, err := service.GetFileContent(context.Background(), mustRawJSON(t, FileContentParams{Ref: "HEAD", RelPath: "big.bin"})) + _, err := service.GetFileContent(context.Background(), mustJSON(t, FileContentParams{Ref: "HEAD", RelPath: "big.bin"})) done <- err }() select { @@ -169,15 +169,6 @@ func TestGetFileContentStopsReadingAtCap(t *testing.T) { } } -func mustRawJSON(t *testing.T, v any) json.RawMessage { - t.Helper() - data, err := json.Marshal(v) - if err != nil { - t.Fatal(err) - } - return data -} - // A blob not in UTF-8 goes to the reader as its bytes, as fs.readFile's do. func TestGetFileContentTextNotInUTF8IsBytes(t *testing.T) { t.Parallel() diff --git a/internal/git/stage_content.go b/internal/git/stage_content.go index 2f902e8a7..159b8c59d 100644 --- a/internal/git/stage_content.go +++ b/internal/git/stage_content.go @@ -1,7 +1,6 @@ package git import ( - "bytes" "context" "encoding/json" "strings" @@ -141,22 +140,15 @@ func (s *Service) writeBlob(ctx context.Context, topLevel string, relPath string if err != nil { return "", err } - var stdout, stderr bytes.Buffer cmd.Stdin = strings.NewReader(content) - cmd.Stdout = &stdout - cmd.Stderr = &stderr - runErr := cmd.Run() - if ctxErr := ctx.Err(); ctxErr != nil { - return "", ctxErr - } - code, fatal := gitExitCode(runErr) + stdout, stderr, code, fatal := runCapturedCommand(ctx, cmd) if fatal != nil { return "", fatal } if code != 0 { - return "", gitError(args, stderr.String(), code) + return "", gitError(args, stderr, code) } - hash := strings.TrimSpace(stdout.String()) + hash := strings.TrimSpace(stdout) if hash == "" { return "", proto.CodedError{Code: proto.CodeRequestFailed, Msg: "git hash-object returned no object id"} } diff --git a/internal/git/stash.go b/internal/git/stash.go index cf52954cd..55b6db7a8 100644 --- a/internal/git/stash.go +++ b/internal/git/stash.go @@ -1,7 +1,6 @@ package git import ( - "bytes" "context" "encoding/json" "fmt" @@ -95,27 +94,18 @@ func (s *Service) StashList(ctx context.Context, raw json.RawMessage) (any, erro if err != nil { return nil, err } - var stdout bytes.Buffer - var stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - - err = cmd.Run() - if ctxErr := ctx.Err(); ctxErr != nil { - return nil, ctxErr - } - code, fatal := gitExitCode(err) + stdout, stderr, code, fatal := runCapturedCommand(ctx, cmd) if fatal != nil { return nil, fatal } if code != 0 { - stderrStr := stderr.String() + stderrStr := stderr kind := Classify(stderrStr) msg := MessageForKind(kind, MessageContext{Stderr: stderrStr, Args: args, ExitCode: &code}) return nil, classifiedFailure(kind, msg) } - entries := parseStashList(stdout.String()) + entries := parseStashList(stdout) return entries, nil } @@ -139,21 +129,12 @@ func (s *Service) StashApply(ctx context.Context, raw json.RawMessage) (any, err if err != nil { return nil, err } - var stdout bytes.Buffer - var stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - - err = cmd.Run() - if ctxErr := ctx.Err(); ctxErr != nil { - return nil, ctxErr - } - code, fatal := gitExitCode(err) + stdout, stderr, code, fatal := runCapturedCommand(ctx, cmd) if fatal != nil { return nil, fatal } if code != 0 { - return classifyStashApplyError(stdout.String(), stderr.String(), args, code), nil + return classifyStashApplyError(stdout, stderr, args, code), nil } return StashApplyResult{}, nil } @@ -177,14 +158,14 @@ func (s *Service) StashDrop(ctx context.Context, raw json.RawMessage) (any, erro if err != nil { return nil, err } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr err = cmd.Run() if ctxErr := ctx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(err) + code, fatal := gitExitCode(cmd.ProcessState, err) if fatal != nil { return nil, fatal } @@ -215,21 +196,12 @@ func (s *Service) StashPop(ctx context.Context, raw json.RawMessage) (any, error if err != nil { return nil, err } - var stdout bytes.Buffer - var stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - - err = cmd.Run() - if ctxErr := ctx.Err(); ctxErr != nil { - return nil, ctxErr - } - code, fatal := gitExitCode(err) + stdout, stderr, code, fatal := runCapturedCommand(ctx, cmd) if fatal != nil { return nil, fatal } if code != 0 { - return classifyStashApplyError(stdout.String(), stderr.String(), args, code), nil + return classifyStashApplyError(stdout, stderr, args, code), nil } return StashApplyResult{}, nil } @@ -271,14 +243,10 @@ func (s *Service) StashShow(ctx context.Context, raw json.RawMessage) (any, erro if err != nil { return nil, err } - stdout, err := cmd.StdoutPipe() - if err != nil { - return nil, err - } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr - - if err := cmd.Start(); err != nil { + stdout, finish, err := startGitOutput(streamCtx, cmd) + if err != nil { return nil, mapGitStartError(err, args) } @@ -287,7 +255,7 @@ func (s *Service) StashShow(ctx context.Context, raw json.RawMessage) (any, erro // Stop git before waiting; it would block on a full stdout pipe. cancel() } - waitErr := cmd.Wait() + waitErr := finish() if readErr != nil { return nil, readErr @@ -295,7 +263,7 @@ func (s *Service) StashShow(ctx context.Context, raw json.RawMessage) (any, erro if ctxErr := streamCtx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(waitErr) + code, fatal := gitExitCode(cmd.ProcessState, waitErr) if fatal != nil { return nil, fatal } @@ -346,14 +314,14 @@ func (s *Service) StashGroup(ctx context.Context, raw json.RawMessage) (any, err if err != nil { return nil, err } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr err = cmd.Run() if ctxErr := ctx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(err) + code, fatal := gitExitCode(cmd.ProcessState, err) if fatal != nil { return nil, fatal } diff --git a/internal/git/streaming.go b/internal/git/streaming.go index 8cd36f356..2d4a662fd 100644 --- a/internal/git/streaming.go +++ b/internal/git/streaming.go @@ -11,17 +11,19 @@ import ( "strings" "github.com/nexus-code/nexus-code/internal/proto" + "github.com/nexus-code/nexus-code/internal/redact" ) const streamChunkBytes = 64 * 1024 type StreamParams struct { - StreamID string `json:"streamId"` - Args []string `json:"args"` - Cwd string `json:"cwd,omitempty"` - Env map[string]string `json:"env,omitempty"` - Interactive bool `json:"interactive,omitempty"` - StreamStderr bool `json:"streamStderr,omitempty"` + StreamID string `json:"streamId"` + Args []string `json:"args"` + Cwd string `json:"cwd,omitempty"` + Env map[string]string `json:"env,omitempty"` + Interactive bool `json:"interactive,omitempty"` + StreamStderr bool `json:"streamStderr,omitempty"` + StdoutCapBytes int64 `json:"stdoutCapBytes,omitempty"` } type StreamChunkPayload struct { @@ -65,34 +67,33 @@ func (s *Service) Stream(ctx context.Context, raw json.RawMessage) (any, error) if params.StreamStderr { return s.streamCommandStderr(streamCtx, cancel, cmd, params) } - stdout, err := cmd.StdoutPipe() - if err != nil { - cancel() - return nil, err - } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr - - if err := cmd.Start(); err != nil { + stdout, finish, err := startGitOutput(streamCtx, cmd) + if err != nil { cancel() return nil, mapGitStartError(err, params.Args) } - readErr := s.emitStreamChunks(streamCtx, params.StreamID, stdout) + limit := stdoutBudget(params.StdoutCapBytes) + readErr := s.emitStreamChunks(streamCtx, params.StreamID, stdout, limit) if readErr != nil { // Nobody reads stdout any more (the event sink failed or the stream // was cancelled): kill git first, or it blocks on a full pipe and // Wait never returns. cancel() } - waitErr := cmd.Wait() + waitErr := finish() + if errors.Is(readErr, errGitOutputLimit) { + return outputLimitResult(stderr.String(), limit), nil + } if readErr != nil && !errors.Is(readErr, context.Canceled) { return nil, readErr } if ctxErr := streamCtx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(waitErr) + code, fatal := gitExitCode(cmd.ProcessState, waitErr) if fatal != nil { return nil, fatal } @@ -100,13 +101,14 @@ func (s *Service) Stream(ctx context.Context, raw json.RawMessage) (any, error) } func (s *Service) streamCommandStderr(streamCtx context.Context, cancel context.CancelFunc, cmd *exec.Cmd, params StreamParams) (any, error) { - var stderr bytes.Buffer + var stderr gitStderr cmd.Stdout = io.Discard - cmd.Stderr = io.MultiWriter(&stderr, streamChunkWriter{ + diagnostics := &stderrStream{output: streamChunkWriter{ ctx: streamCtx, service: s, streamID: params.StreamID, - }) + }} + cmd.Stderr = io.MultiWriter(&stderr, diagnostics) if err := cmd.Start(); err != nil { cancel() return nil, mapGitStartError(err, params.Args) @@ -115,10 +117,13 @@ func (s *Service) streamCommandStderr(streamCtx context.Context, cancel context. if ctxErr := streamCtx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(waitErr) + code, fatal := gitExitCode(cmd.ProcessState, waitErr) if fatal != nil { return nil, fatal } + if err := diagnostics.flush(); err != nil { + return nil, err + } return buildRunResult("", stderr.String(), code, params.Args), nil } @@ -156,22 +161,17 @@ func (s *Service) unregisterStream(streamID string) { delete(s.streams, streamID) } -func (s *Service) emitStreamChunks(ctx context.Context, streamID string, stdout io.Reader) error { +func (s *Service) emitStreamChunks(ctx context.Context, streamID string, stdout io.Reader, remaining int64) error { buf := make([]byte, streamChunkBytes) for { n, err := stdout.Read(buf) + if int64(n) > remaining { + return errGitOutputLimit + } if n > 0 { - payload := StreamChunkPayload{ - StreamID: streamID, - Chunk: base64.StdEncoding.EncodeToString(buf[:n]), - } - s.mu.Lock() - sink := s.sink - s.mu.Unlock() - if sink != nil { - if emitErr := sink("git.streamChunk", payload); emitErr != nil { - return emitErr - } + remaining -= int64(n) + if err := s.emitStreamChunk(streamID, buf[:n]); err != nil { + return err } } if err == io.EOF { @@ -189,6 +189,53 @@ func (s *Service) emitStreamChunks(ctx context.Context, streamID string, stdout } } +// stderrStream masks complete diagnostic lines so a secret split across +// reads never reaches a stream event. Git progress also delimits with CR. +type stderrStream struct { + output streamChunkWriter + pending []byte + oversized bool +} + +func (s *stderrStream) Write(p []byte) (int, error) { + written := 0 + for len(p) > 0 { + end := bytes.IndexAny(p, "\r\n") + line := p + if end >= 0 { + line = p[:end+1] + } + if !s.oversized { + if len(s.pending)+len(line) > captureTailBytes { + s.pending = s.pending[:0] + s.oversized = true + } else { + s.pending = append(s.pending, line...) + } + } + if end >= 0 { + if err := s.flush(); err != nil { + return written, err + } + } + written += len(line) + p = p[len(line):] + } + return written, nil +} + +// flush emits one masked line or an explicit diagnostic for an oversized line. +func (s *stderrStream) flush() error { + line := redact.Credentials(string(s.pending)) + if s.oversized { + line = "[Git stderr line exceeded the 1 MiB limit]\n" + } + s.pending = s.pending[:0] + s.oversized = false + _, err := s.output.Write([]byte(line)) + return err +} + type streamChunkWriter struct { ctx context.Context service *Service diff --git a/internal/git/sync.go b/internal/git/sync.go index 9326cada8..30555b794 100644 --- a/internal/git/sync.go +++ b/internal/git/sync.go @@ -1,7 +1,6 @@ package git import ( - "bytes" "context" "encoding/json" "regexp" @@ -220,18 +219,11 @@ func (s *Service) runSyncGit(ctx context.Context, cwd string, args []string, int if err != nil { return "", "", 0, err } - var stdout, stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - runErr := cmd.Run() - if ctxErr := ctx.Err(); ctxErr != nil { - return "", "", 0, ctxErr - } - code, fatal := gitExitCode(runErr) + stdout, stderr, code, fatal := runCapturedCommand(ctx, cmd) if fatal != nil { return "", "", 0, fatal } - return stdout.String(), stderr.String(), code, nil + return stdout, stderr, code, nil } // nonEmpty filters empty strings for summary joining. diff --git a/internal/git/tag.go b/internal/git/tag.go index f2753c075..a29af0d97 100644 --- a/internal/git/tag.go +++ b/internal/git/tag.go @@ -1,7 +1,6 @@ package git import ( - "bytes" "context" "encoding/json" "fmt" @@ -93,27 +92,18 @@ func (s *Service) TagList(ctx context.Context, raw json.RawMessage) (any, error) if err != nil { return nil, err } - var stdout bytes.Buffer - var stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - - err = cmd.Run() - if ctxErr := ctx.Err(); ctxErr != nil { - return nil, ctxErr - } - code, fatal := gitExitCode(err) + stdout, stderr, code, fatal := runCapturedCommand(ctx, cmd) if fatal != nil { return nil, fatal } if code != 0 { - stderrStr := stderr.String() + stderrStr := stderr kind := Classify(stderrStr) msg := MessageForKind(kind, MessageContext{Stderr: stderrStr, Args: args, ExitCode: &code}) return nil, classifiedFailure(kind, msg) } - tags := parseTagList(stdout.String()) + tags := parseTagList(stdout) return tags, nil } @@ -143,27 +133,18 @@ func (s *Service) TagListRemote(ctx context.Context, raw json.RawMessage) (any, if err != nil { return nil, err } - var stdout bytes.Buffer - var stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - - err = cmd.Run() - if ctxErr := ctx.Err(); ctxErr != nil { - return nil, ctxErr - } - code, fatal := gitExitCode(err) + stdout, stderr, code, fatal := runCapturedCommand(ctx, cmd) if fatal != nil { return nil, fatal } if code != 0 { - stderrStr := stderr.String() + stderrStr := stderr kind := Classify(stderrStr) msg := MessageForKind(kind, MessageContext{Stderr: stderrStr, Args: args, ExitCode: &code}) return nil, classifiedFailure(kind, msg) } - tags := parseRemoteTagList(stdout.String(), remote) + tags := parseRemoteTagList(stdout, remote) return tags, nil } @@ -200,14 +181,14 @@ func (s *Service) TagCreate(ctx context.Context, raw json.RawMessage) (any, erro if err != nil { return nil, err } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr err = cmd.Run() if ctxErr := ctx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(err) + code, fatal := gitExitCode(cmd.ProcessState, err) if fatal != nil { return nil, fatal } @@ -239,14 +220,14 @@ func (s *Service) TagDelete(ctx context.Context, raw json.RawMessage) (any, erro if err != nil { return nil, err } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr err = cmd.Run() if ctxErr := ctx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(err) + code, fatal := gitExitCode(cmd.ProcessState, err) if fatal != nil { return nil, fatal } @@ -288,14 +269,14 @@ func (s *Service) TagDeleteRemote(ctx context.Context, raw json.RawMessage) (any if err != nil { return nil, err } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr err = cmd.Run() if ctxErr := ctx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(err) + code, fatal := gitExitCode(cmd.ProcessState, err) if fatal != nil { return nil, fatal } @@ -339,14 +320,14 @@ func (s *Service) TagPush(ctx context.Context, raw json.RawMessage) (any, error) if err != nil { return nil, err } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr err = cmd.Run() if ctxErr := ctx.Err(); ctxErr != nil { return nil, ctxErr } - code, fatal := gitExitCode(err) + code, fatal := gitExitCode(cmd.ProcessState, err) if fatal != nil { return nil, fatal } @@ -489,14 +470,14 @@ func (s *Service) assertTagTargetExists(ctx context.Context, cwd string, ref str if err != nil { return err } - var stderr bytes.Buffer + var stderr gitStderr cmd.Stderr = &stderr err = cmd.Run() if ctxErr := ctx.Err(); ctxErr != nil { return ctxErr } - code, fatal := gitExitCode(err) + code, fatal := gitExitCode(cmd.ProcessState, err) if fatal != nil { return fatal } diff --git a/internal/git/testutil_test.go b/internal/git/testutil_test.go index 92362a3a7..b8bcb1b76 100644 --- a/internal/git/testutil_test.go +++ b/internal/git/testutil_test.go @@ -1,22 +1,34 @@ package git import ( + "encoding/json" "errors" "io/fs" "os" "os/exec" "path/filepath" + "runtime" "sync" "testing" ) -// TestMain lets the test binary double as the askpass helper: interactive -// commands name os.Executable() as GIT_ASKPASS, as the agent does. +const gitFixtureEnv = "NEXUS_TEST_GIT_FIXTURE" + +// TestMain lets the test binary double as Git or the askpass helper on +// Windows and Unix. Interactive calls use os.Executable() as GIT_ASKPASS. // // It also keeps the developer's global and system git config, such as their // own pull.rebase, out of every git command the tests run. Setting it here // rather than with t.Setenv lets those tests run in parallel. func TestMain(m *testing.M) { + switch os.Getenv(gitFixtureEnv) { + case "wait-delay": + os.Exit(runWaitDelayFixture(os.Getenv(waitDelayFixtureAddr))) + case "output-budget": + os.Exit(runOutputBudgetFixture()) + case "large-status": + os.Exit(runLargeStatusFixture()) + } if socketPath, ok := AskpassSocketFromEnv(); ok { os.Exit(RunAskpassHelper(socketPath, os.Args[1:], os.Stdout, os.Stderr)) } @@ -35,6 +47,34 @@ func TestMain(m *testing.M) { os.Exit(code) } +// setFakeGitExecutable exposes the test binary as Git on Windows and Unix. +func setFakeGitExecutable(t *testing.T, fixture string) { + t.Helper() + exe, err := os.Executable() + if err != nil { + t.Fatal(err) + } + dir := t.TempDir() + name := "git" + if runtime.GOOS == "windows" { + name += ".exe" + t.Setenv("PATHEXT", ".EXE") + } + path := filepath.Join(dir, name) + if err := os.Link(exe, path); err != nil { + // The test executable and TempDir can be on different filesystems. + data, err := os.ReadFile(exe) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, data, 0o700); err != nil { + t.Fatal(err) + } + } + t.Setenv("PATH", dir) + t.Setenv(gitFixtureEnv, fixture) +} + // runGit runs git in dir and returns its stdout. A non-zero exit fails the // test with git's stderr. func runGit(t *testing.T, dir string, args ...string) string { @@ -226,3 +266,13 @@ func newRemoteAndClient(t *testing.T, behind bool) (remote, client string) { runGit(t, client, "remote", "set-url", "origin", remote) return remote, client } + +// mustJSON marshals a request fixture for the Git service tests. +func mustJSON(t *testing.T, value any) json.RawMessage { + t.Helper() + raw, err := json.Marshal(value) + if err != nil { + t.Fatal(err) + } + return raw +} diff --git a/internal/git/wait_delay_test.go b/internal/git/wait_delay_test.go new file mode 100644 index 000000000..07bb9390d --- /dev/null +++ b/internal/git/wait_delay_test.go @@ -0,0 +1,305 @@ +package git + +import ( + "context" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "os" + "os/exec" + "reflect" + "strconv" + "strings" + "testing" + "time" +) + +const ( + waitDelayFixtureAddr = "NEXUS_TEST_GIT_WAIT_ADDR" + waitDelayFixtureExe = "NEXUS_TEST_GIT_WAIT_EXE" + waitDelayStdout = "git's stdout\n" + waitDelayStderr = "git's stderr\n" + fixtureWaitDelay = 50 * time.Millisecond +) + +// runWaitDelayFixture either runs the fake Git or holds the inherited pipes +// until the test closes its loopback connection. +func runWaitDelayFixture(addr string) int { + if len(os.Args) < 2 { + return 1 + } + if os.Args[1] == "--hold-pipes" || os.Args[1] == "--overflow-child" { + conn, err := net.DialTimeout("tcp", addr, 5*time.Second) + if err != nil { + return 1 + } + defer conn.Close() + _ = conn.SetReadDeadline(time.Now().Add(10 * time.Second)) + if os.Args[1] == "--overflow-child" { + if _, err := io.ReadFull(conn, make([]byte, 1)); err != nil { + return 1 + } + _, _ = io.WriteString(os.Stdout, strings.Repeat("x", 2048)) + } + _, _ = io.Copy(conn, conn) + return 0 + } + if os.Args[1] == "log" { + _, _ = os.Stdout.Write(syntheticLogStdout(1)) + } else { + _, _ = io.WriteString(os.Stdout, waitDelayStdout) + } + _, _ = io.WriteString(os.Stderr, waitDelayStderr) + helperArg := "--hold-pipes" + if os.Args[1] == "overflow" { + helperArg = "--overflow-child" + } + helper := exec.Command(os.Getenv(waitDelayFixtureExe), helperArg) + helper.Stdout = os.Stdout + helper.Stderr = os.Stderr + if err := helper.Start(); err != nil { + fmt.Fprintln(os.Stderr, err) + return 1 + } + if os.Args[1] == "wait-for-helper" || os.Args[1] == "overflow" { + // The cancelled launcher is still running when the helper connects. + if err := helper.Wait(); err != nil { + return 1 + } + } else { + _ = helper.Process.Release() + } + return 0 +} + +// setupInheritedPipeFixture lets the fake Git start a descendant which +// holds its pipes open until the test closes a separate control connection. +func setupInheritedPipeFixture(t *testing.T) { + t.Helper() + setFakeGitExecutable(t, "wait-delay") + exe, err := os.Executable() + if err != nil { + t.Fatal(err) + } + t.Setenv(waitDelayFixtureExe, exe) +} + +// listenForPipeHolder exposes the control connection for one child process. +func listenForPipeHolder(t *testing.T) *net.TCPListener { + t.Helper() + listener, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = listener.Close() }) + t.Setenv(waitDelayFixtureAddr, listener.Addr().String()) + if err := listener.SetDeadline(time.Now().Add(5 * time.Second)); err != nil { + t.Fatal(err) + } + return listener +} + +// TestGitSuccessWithInheritedOutputPipes starts an executable fixture on +// both Windows and Unix. A helper holds the pipes open past Git's exit. +func TestGitSuccessWithInheritedOutputPipes(t *testing.T) { + setupInheritedPipeFixture(t) + wantRun := RunResult{Stdout: waitDelayStdout, Stderr: waitDelayStderr, Code: 0} + logCall := func(s *Service, ctx context.Context) (any, error) { + return s.Log(ctx, json.RawMessage(`{"source":false}`)) + } + for _, tc := range []struct { + name string + call func(*Service, context.Context) (any, error) + want any + logBatch bool + }{ + {"run", func(s *Service, ctx context.Context) (any, error) { + return s.Run(ctx, json.RawMessage(`{"args":["status"]}`)) + }, wantRun, false}, + {"capture", func(s *Service, ctx context.Context) (any, error) { + out, stderr, code, err := s.capture(ctx, "", []string{"status"}, false) + return RunResult{Stdout: out, Stderr: stderr, Code: code}, err + }, wantRun, false}, + {"stream", func(s *Service, ctx context.Context) (any, error) { + return s.Stream(ctx, json.RawMessage(`{"streamId":"status","args":["status"]}`)) + }, RunResult{Stderr: waitDelayStderr, Code: 0}, false}, + {"log", logCall, LogResult{Count: 1}, true}, + } { + t.Run(tc.name, func(t *testing.T) { + listener := listenForPipeHolder(t) + ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second) + defer cancel() + svc := New(t.TempDir()) + svc.commandWaitDelay = fixtureWaitDelay + var entries []LogEntry + var streamed strings.Builder + svc.SetEventSink(func(_ string, payload any) error { + switch data := payload.(type) { + case LogBatchPayload: + entries = append(entries, data.Entries...) + case StreamChunkPayload: + chunk, err := base64.StdEncoding.DecodeString(data.Chunk) + if err != nil { + return err + } + streamed.Write(chunk) + } + return nil + }) + type response struct { + value any + err error + } + finished := make(chan response, 1) + go func() { + value, err := tc.call(svc, ctx) + finished <- response{value, err} + }() + conn, err := listener.AcceptTCP() + if err != nil { + t.Fatal("helper never connected:", err) + } + defer conn.Close() + select { + case got := <-finished: + if got.err != nil || !reflect.DeepEqual(got.value, tc.want) { + t.Fatalf("result = %+v, %v; want %+v", got.value, got.err, tc.want) + } + // A successful command can leave a credential or SSH helper alive. + if err := conn.SetDeadline(time.Now().Add(time.Second)); err != nil { + t.Fatal(err) + } + if _, err := conn.Write([]byte{1}); err != nil { + t.Fatal("Git's successful reply terminated its helper:", err) + } + if _, err := io.ReadFull(conn, make([]byte, 1)); err != nil { + t.Fatal("Git's successful reply terminated its helper:", err) + } + if tc.name == "stream" && streamed.String() != waitDelayStdout { + t.Fatalf("streamed stdout = %q, want %q", streamed.String(), waitDelayStdout) + } + if tc.logBatch && (len(entries) != 1 || entries[0].Subject != "Subject 1") { + t.Fatalf("Git's log entry was not delivered: %+v", entries) + } + case <-time.After(3 * time.Second): + _ = conn.Close() + <-finished + t.Fatal("Git's successful reply waited for the helper to close its pipes") + } + }) + } +} + +// TestCancelledGitReturnsWithInheritedOutputPipes cancels a running Git +// command while a live descendant holds its stdout open. +func TestCancelledGitReturnsWithInheritedOutputPipes(t *testing.T) { + setupInheritedPipeFixture(t) + listener := listenForPipeHolder(t) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + svc := New(t.TempDir()) + svc.commandWaitDelay = fixtureWaitDelay + cmd, err := svc.command(ctx, []string{"wait-for-helper"}, "", nil, false) + if err != nil { + t.Fatal(err) + } + cmd.Stdout = io.Discard + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + finished := make(chan error, 1) + go func() { finished <- cmd.Wait() }() + conn, err := listener.AcceptTCP() + if err != nil { + t.Fatal("helper never connected:", err) + } + defer conn.Close() + + cancel() + select { + case err := <-finished: + if err == nil { + t.Fatal("cancelled Git unexpectedly succeeded") + } + case <-time.After(3 * time.Second): + _ = conn.Close() + <-finished + t.Fatal("Git wait is still blocked on the helper's output pipe") + } +} + +// runOutputBudgetFixture produces controlled stdout and, when requested, +// enough stderr to exceed the tail budget. +func runOutputBudgetFixture() int { + if len(os.Args) < 3 { + return 1 + } + size, err := strconv.Atoi(os.Args[2]) + if err != nil { + return 1 + } + chunk := strings.Repeat("a", 32<<10) + for size > 0 { + n := min(len(chunk), size) + if _, err := io.WriteString(os.Stdout, chunk[:n]); err != nil { + return 1 + } + size -= n + } + if len(os.Args) > 3 && os.Args[3] == "stderr-tail" { + _, _ = io.WriteString(os.Stderr, "first diagnostic\n"+strings.Repeat("discard me\n", 100_000)+"last diagnostic\n") + } + return 0 +} + +// TestGitOutputBudgets exercises default, requested and hard limits through +// the public command path, and retains only the latest diagnostic stderr. +func TestGitOutputBudgets(t *testing.T) { + setFakeGitExecutable(t, "output-budget") + svc := New(t.TempDir()) + for _, tc := range []struct { + name string + bytes int + budget int + want Kind + stderrTail bool + }{ + {"default", 12 << 20, 0, "", true}, + {"default overflow", (16 << 20) + 1, 0, KindOutputTooLarge, false}, + {"requested", 17 << 20, 20 << 20, "", false}, + {"hard limit", (64 << 20) + 1, 65 << 20, KindOutputTooLarge, false}, + } { + t.Run(tc.name, func(t *testing.T) { + args := []string{"bulk", strconv.Itoa(tc.bytes)} + if tc.stderrTail { + args = append(args, "stderr-tail") + } + raw, err := json.Marshal(RunParams{Args: args, StdoutCapBytes: int64(tc.budget)}) + if err != nil { + t.Fatal(err) + } + value, err := svc.Run(context.Background(), raw) + if err != nil { + t.Fatal(err) + } + got := value.(RunResult) + if got.ErrorKind != tc.want { + t.Fatalf("Git error kind = %q, want %q", got.ErrorKind, tc.want) + } + if tc.want == "" && len(got.Stdout) != tc.bytes { + t.Fatalf("Git retained %d stdout bytes, want %d", len(got.Stdout), tc.bytes) + } + if tc.stderrTail && (strings.Contains(got.Stderr, "first diagnostic") || !strings.HasSuffix(got.Stderr, "last diagnostic\n")) { + t.Fatalf("Git retained %d stderr bytes with expected tail: %t", len(got.Stderr), strings.HasSuffix(got.Stderr, "last diagnostic\n")) + } + }) + } + stdout, _, _, err := svc.capture(context.Background(), "", []string{"bulk", strconv.Itoa((16 << 20) + 1)}, false) + var failure Failure + if !errors.As(err, &failure) || failure.Kind != KindOutputTooLarge || stdout != "" { + t.Fatalf("capture returned %d bytes, error %v", len(stdout), err) + } +} diff --git a/internal/git/watch.go b/internal/git/watch.go index c7d136c65..0e7867fff 100644 --- a/internal/git/watch.go +++ b/internal/git/watch.go @@ -13,7 +13,10 @@ import ( "github.com/nexus-code/nexus-code/internal/proto" ) -const gitWatchDebounce = 300 * time.Millisecond +const ( + gitWatchDebounce = 300 * time.Millisecond + maxDirtyRepositories = 128 +) type WatchParams struct { GitDir string `json:"gitDir"` @@ -162,8 +165,18 @@ func (s *Service) handleGitWatchEvent(entry *watchEntry, event fsnotify.Event) { s.bufferGitDirty(entry.gitDir) } +// bufferGitDirty coalesces repository refreshes until the batch fills. A +// repository that cannot join the batch gets the same refresh immediately. func (s *Service) bufferGitDirty(gitDir string) { s.mu.Lock() + if _, queued := s.gitDirty[gitDir]; !queued && len(s.gitDirty) >= maxDirtyRepositories { + sink := s.sink + s.mu.Unlock() + if sink != nil { + _ = sink("git.changed", ChangedPayload{GitDir: gitDir}) + } + return + } defer s.mu.Unlock() s.gitDirty[gitDir] = struct{}{} if s.timer != nil { @@ -172,27 +185,27 @@ func (s *Service) bufferGitDirty(gitDir string) { s.timer = time.AfterFunc(gitWatchDebounce, s.flushGitDirty) } +// flushGitDirty allows at most one in-flight debounce batch, even when its +// event sink is blocked. New notifications accumulate in the next batch. func (s *Service) flushGitDirty() { s.mu.Lock() - s.timer = nil - if len(s.gitDirty) == 0 { - s.mu.Unlock() - return - } - dirty := make([]string, 0, len(s.gitDirty)) - for gitDir := range s.gitDirty { - dirty = append(dirty, gitDir) - } - clear(s.gitDirty) + dirty := s.gitDirty + s.gitDirty = make(map[string]struct{}) sink := s.sink s.mu.Unlock() - if sink == nil { - return + if sink != nil { + for gitDir := range dirty { + _ = sink("git.changed", ChangedPayload{GitDir: gitDir}) + } } - for _, gitDir := range dirty { - _ = sink("git.changed", ChangedPayload{GitDir: gitDir}) + s.mu.Lock() + defer s.mu.Unlock() + if s.timer == nil || len(s.gitDirty) == 0 { + s.timer = nil + return } + s.timer = time.AfterFunc(gitWatchDebounce, s.flushGitDirty) } // addGitWatchDirs watches root and the directories below it that diff --git a/internal/git/watch_test.go b/internal/git/watch_test.go index 96732fae9..4e1882f8e 100644 --- a/internal/git/watch_test.go +++ b/internal/git/watch_test.go @@ -4,7 +4,9 @@ import ( "os" "path/filepath" "slices" + "strconv" "testing" + "time" "github.com/fsnotify/fsnotify" ) @@ -132,3 +134,23 @@ func assertWatched(t *testing.T, watcher *fsnotify.Watcher, gitDir string, want t.Fatalf("watched dirs:\n got %v\n want %v", got, want) } } + +// TestGitWatchRefreshesRepositoriesBeyondBatchBudget checks that the existing +// per-repository refresh event reaches the client when the batch fills up. +func TestGitWatchRefreshesRepositoriesBeyondBatchBudget(t *testing.T) { + s := New(t.TempDir()) + s.timer = time.NewTimer(time.Hour) + defer s.timer.Stop() + var refreshed []string + s.SetEventSink(func(_ string, payload any) error { + refreshed = append(refreshed, payload.(ChangedPayload).GitDir) + return nil + }) + for i := 0; i < 128; i++ { + s.bufferGitDirty(strconv.Itoa(i)) + } + s.bufferGitDirty("overflow") + if len(s.gitDirty) != 128 || !slices.Equal(refreshed, []string{"overflow"}) { + t.Fatalf("queued %d repositories, refreshed %v", len(s.gitDirty), refreshed) + } +} diff --git a/internal/hookclient/print.go b/internal/hookclient/print.go index 953ef4d3d..a30c99a86 100644 --- a/internal/hookclient/print.go +++ b/internal/hookclient/print.go @@ -225,6 +225,12 @@ func runPrint( } } cmd := nodeshim.Command(claude, args...) + releaseChild, err := preparePrintChild(cmd) + if err != nil { + fmt.Fprintf(stderr, "nexus: %v\n", err) + return 127, 0 + } + defer releaseChild() cmd.Stdin = stdin cmd.Stderr = stderr // A json or stream-json run's output tells of the run, and a text run's diff --git a/internal/hookclient/print_child_darwin.go b/internal/hookclient/print_child_darwin.go new file mode 100644 index 000000000..d84e23af2 --- /dev/null +++ b/internal/hookclient/print_child_darwin.go @@ -0,0 +1,151 @@ +package hookclient + +import ( + "fmt" + "io" + "os" + "os/exec" + "strconv" + "syscall" + + "golang.org/x/sys/unix" +) + +const printChildArg = "--print-child" +const printWatcherArg = "--print-child-watch" + +// preparePrintChild passes a pipe whose writer belongs only to the recorder. +// The bootstrap arms the watcher before replacing itself with Claude, so the +// recorder keeps the real child's PID, wait status and terminal process group. +func preparePrintChild(cmd *exec.Cmd) (func(), error) { + if cmd.Err != nil { + return nil, cmd.Err + } + self, err := os.Executable() + if err != nil { + return nil, err + } + read, write, err := os.Pipe() + if err != nil { + return nil, err + } + cmd.Args = append([]string{self, printChildArg, cmd.Path}, cmd.Args...) + cmd.Path = self + cmd.ExtraFiles = []*os.File{read} + return func() { + _ = read.Close() + _ = write.Close() + }, nil +} + +// PrintChildHelper handles the private child bootstrap and watcher commands. +func PrintChildHelper(args []string) (int, bool) { + if len(args) == 0 { + return 0, false + } + switch args[0] { + case printChildArg: + if len(args) < 3 { + return 2, true + } + if err := execWatchedPrintChild(args[1], args[2:]); err != nil { + fmt.Fprintf(os.Stderr, "nexus: %v\n", err) + return 127, true + } + case printWatcherArg: + if len(args) != 2 { + return 2, true + } + pid, err := strconv.Atoi(args[1]) + if err != nil || pid <= 1 { + return 2, true + } + if err := watchPrintChild(pid); err != nil { + fmt.Fprintf(os.Stderr, "nexus: print child watcher: %v\n", err) + return 1, true + } + default: + return 0, false + } + return 0, true +} + +// execWatchedPrintChild waits for the watcher to arm before exec'ing Claude. +// Only the watcher leaves the terminal session and its foreground signals. +func execWatchedPrintChild(path string, args []string) error { + parent := os.NewFile(3, "print recorder lifetime") + defer parent.Close() + unix.CloseOnExec(int(parent.Fd())) + self, err := os.Executable() + if err != nil { + return err + } + ready, readyWrite, err := os.Pipe() + if err != nil { + return err + } + defer ready.Close() + watcher, err := os.StartProcess(self, []string{self, printWatcherArg, strconv.Itoa(os.Getpid())}, &os.ProcAttr{ + Files: []*os.File{parent, readyWrite, os.Stderr}, + Sys: &syscall.SysProcAttr{Setsid: true}, + }) + _ = readyWrite.Close() + if err != nil { + return err + } + var armed [1]byte + if _, err := io.ReadFull(ready, armed[:]); err != nil { + _, _ = watcher.Wait() + return fmt.Errorf("arm print child watcher: %w", err) + } + _ = watcher.Release() + return syscall.Exec(path, args, os.Environ()) +} + +// watchPrintChild waits for the recorder's pipe to close or for Claude to +// exit. The process filter keeps the watcher from outliving a completed run. +func watchPrintChild(pid int) error { + if os.Getppid() != pid { + return nil + } + queue, err := unix.Kqueue() + if err != nil { + return err + } + defer unix.Close(queue) + filters := []unix.Kevent_t{ + {Ident: uint64(os.Stdin.Fd()), Filter: unix.EVFILT_READ, Flags: unix.EV_ADD}, + {Ident: uint64(pid), Filter: unix.EVFILT_PROC, Flags: unix.EV_ADD, Fflags: unix.NOTE_EXIT}, + } + if _, err := unix.Kevent(queue, filters, nil, nil); err != nil { + return err + } + if _, err := os.Stdout.Write([]byte{1}); err != nil { + return err + } + _ = os.Stdout.Close() + + var events [2]unix.Kevent_t + for { + n, err := unix.Kevent(queue, nil, events[:], nil) + if err == unix.EINTR { + continue + } + if err != nil { + return err + } + for _, event := range events[:n] { + if event.Filter == unix.EVFILT_PROC { + return nil + } + } + for _, event := range events[:n] { + if event.Filter == unix.EVFILT_READ && event.Flags&unix.EV_EOF != 0 { + if os.Getppid() == pid { + _ = unix.Kill(pid, unix.SIGKILL) + } + return nil + } + } + } +} diff --git a/internal/hookclient/print_child_helper_other.go b/internal/hookclient/print_child_helper_other.go new file mode 100644 index 000000000..b15a31bd3 --- /dev/null +++ b/internal/hookclient/print_child_helper_other.go @@ -0,0 +1,6 @@ +//go:build !darwin + +package hookclient + +// PrintChildHelper handles the macOS-only print child and watcher entrypoints. +func PrintChildHelper([]string) (int, bool) { return 0, false } diff --git a/internal/hookclient/print_child_linux.go b/internal/hookclient/print_child_linux.go new file mode 100644 index 000000000..074bca77b --- /dev/null +++ b/internal/hookclient/print_child_linux.go @@ -0,0 +1,15 @@ +package hookclient + +import ( + "os/exec" + "runtime" + "syscall" +) + +// preparePrintChild binds Claude to the thread that starts it. Keep that +// thread alive until the child has been waited for. +func preparePrintChild(cmd *exec.Cmd) (func(), error) { + runtime.LockOSThread() + cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} + return runtime.UnlockOSThread, nil +} diff --git a/internal/hookclient/print_child_other.go b/internal/hookclient/print_child_other.go new file mode 100644 index 000000000..134e8bcb6 --- /dev/null +++ b/internal/hookclient/print_child_other.go @@ -0,0 +1,10 @@ +//go:build !darwin && !linux + +package hookclient + +import "os/exec" + +// preparePrintChild leaves process setup to the existing platform supervision. +func preparePrintChild(*exec.Cmd) (func(), error) { + return func() {}, nil +} diff --git a/internal/hookclient/print_child_unix_test.go b/internal/hookclient/print_child_unix_test.go new file mode 100644 index 000000000..fd5dae336 --- /dev/null +++ b/internal/hookclient/print_child_unix_test.go @@ -0,0 +1,76 @@ +//go:build darwin || linux + +package hookclient + +import ( + "bufio" + "errors" + "io" + "os" + "os/exec" + "strconv" + "strings" + "syscall" + "testing" + "time" +) + +// TestMain dispatches the private bootstrap for child processes of the test binary. +func TestMain(m *testing.M) { + if code, handled := PrintChildHelper(os.Args[1:]); handled { + os.Exit(code) + } + os.Exit(m.Run()) +} + +// TestPrintWrapperKillStopsChild uses an idle process with no hook server or +// pipe writes after startup, so only wrapper-death handling can stop it. +func TestPrintWrapperKillStopsChild(t *testing.T) { + if os.Getenv("NEXUS_TEST_PRINT_PARENT_DEATH") == "1" { + os.Exit(RunPrint([]string{"/bin/sh", "-c", `printf '%s\n' "$$"; exec /bin/sleep 60`})) + } + + read, write, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + defer read.Close() + cmd := exec.Command(os.Args[0], "-test.run=^TestPrintWrapperKillStopsChild$") + cmd.Env = append(os.Environ(), "NEXUS_TEST_PRINT_PARENT_DEATH=1", envAgentSocket+"=") + cmd.Stdout, cmd.Stderr = write, os.Stderr + err = cmd.Start() + _ = write.Close() + if err != nil { + t.Fatal(err) + } + defer func() { + _ = cmd.Process.Kill() + _ = cmd.Wait() + }() + _ = read.SetReadDeadline(time.Now().Add(5 * time.Second)) + out := bufio.NewReader(read) + line, err := out.ReadString('\n') + if err != nil { + t.Fatal(err) + } + pid, err := strconv.Atoi(strings.TrimSpace(line)) + if err != nil || pid <= 1 { + t.Fatalf("invalid test child pid %q", line) + } + ended := false + defer func() { + if !ended { + _ = syscall.Kill(pid, syscall.SIGKILL) + } + }() + + if err := cmd.Process.Kill(); err != nil { + t.Fatal(err) + } + _ = cmd.Wait() + _ = read.SetReadDeadline(time.Now().Add(time.Second)) + if _, err := out.ReadByte(); !errors.Is(err, io.EOF) { + t.Fatalf("child survived killing its wrapper: %v", err) + } + ended = true +} diff --git a/internal/hookclient/print_windows.go b/internal/hookclient/print_windows.go index 8284daffd..4820ea64a 100644 --- a/internal/hookclient/print_windows.go +++ b/internal/hookclient/print_windows.go @@ -6,6 +6,8 @@ import ( "os" "sync" + hostproc "github.com/nexus-code/nexus-code/internal/process" + "golang.org/x/sys/windows" ) @@ -32,21 +34,8 @@ type processTree struct { // process it starts before it is in the job is left out. What is in the // job when it is let go of keeps running, as it did before. func holdTree(p *os.Process) *processTree { - job, err := windows.CreateJobObject(nil, nil) - if err != nil { - return nil - } - // The job sets no limits, so no process may leave it: Git Bash, which - // runs codex's commands, starts each program out of any job that lets it. - process, err := windows.OpenProcess(windows.PROCESS_SET_QUOTA|windows.PROCESS_TERMINATE, - false, uint32(p.Pid)) + job, err := hostproc.AssignJob(p.Pid, false) if err != nil { - _ = windows.CloseHandle(job) - return nil - } - defer windows.CloseHandle(process) - if err := windows.AssignProcessToJobObject(job, process); err != nil { - _ = windows.CloseHandle(job) return nil } return &processTree{job: job} diff --git a/internal/hookclient/run.go b/internal/hookclient/run.go index 6010ec88c..3c66035f9 100644 --- a/internal/hookclient/run.go +++ b/internal/hookclient/run.go @@ -20,6 +20,8 @@ import ( "net" "os" "time" + + "github.com/nexus-code/nexus-code/internal/proto" ) // 환경 변수 키 상수 — hook 클라이언트가 읽는 연결 정보 집합. @@ -125,7 +127,7 @@ func Run(args []string) int { } // stdin에서 Claude Code가 전달한 hook 이벤트 JSON payload를 읽는다. - payload, err := readStdinPayload() + payload, err := readStdinPayload(os.Stdin) if err != nil { // payload 읽기 실패는 경고만 출력하고 silent fallback한다. fmt.Fprintf(os.Stderr, "nexus hook: stdin read failed: %v\n", err) @@ -263,13 +265,19 @@ func readNDJSON(r io.Reader) (hookResponse, error) { return resp, nil } -// readStdinPayload 는 stdin 전체를 읽어 JSON RawMessage로 반환한다. -// 읽은 내용이 유효한 JSON이 아니어도 그대로 전달해 agent에서 판단하도록 한다. -func readStdinPayload() (json.RawMessage, error) { - data, err := io.ReadAll(os.Stdin) +// readStdinPayload bounds buffering by the largest hook frame the server +// accepts. Drain excess input so the hook caller can finish writing its pipe. +func readStdinPayload(in io.Reader) (json.RawMessage, error) { + data, err := io.ReadAll(io.LimitReader(in, proto.MaxHookRequestBytes+1)) if err != nil { return nil, err } + if len(data) > proto.MaxHookRequestBytes { + if _, err := io.Copy(io.Discard, in); err != nil { + return nil, err + } + return nil, fmt.Errorf("payload exceeds the %d-byte hook request limit", proto.MaxHookRequestBytes) + } if len(data) == 0 { // 빈 stdin은 빈 JSON object로 대체한다. return json.RawMessage("{}"), nil diff --git a/internal/hookclient/run_test.go b/internal/hookclient/run_test.go index 940f80827..e07051b23 100644 --- a/internal/hookclient/run_test.go +++ b/internal/hookclient/run_test.go @@ -12,6 +12,19 @@ import ( "time" ) +// TestOversizedHookPayloadIsDrainedWithoutForwarding covers the server's +// frame limit while keeping the caller's input pipe drained. +func TestOversizedHookPayloadIsDrainedWithoutForwarding(t *testing.T) { + in := bytes.NewReader(make([]byte, 16*1024*1024+4096)) + payload, err := readStdinPayload(in) + if err == nil || payload != nil { + t.Fatalf("oversized payload returned %d bytes, %v", len(payload), err) + } + if in.Len() != 0 { + t.Fatalf("%d bytes of the hook's input were not drained", in.Len()) + } +} + // ----------------------------------------------------------------------- // 헬퍼 — 테스트용 Unix 소켓 mock 서버 // ----------------------------------------------------------------------- diff --git a/internal/hookclient/send_message.go b/internal/hookclient/send_message.go index 221d61a24..921accd53 100644 --- a/internal/hookclient/send_message.go +++ b/internal/hookclient/send_message.go @@ -4,6 +4,8 @@ import ( "encoding/json" "io" "strings" + + "github.com/nexus-code/nexus-code/internal/proto" ) // sendMessageSubcmd is the PreToolUse hook on Claude's SendMessage tool @@ -54,10 +56,15 @@ func sendMessageDecision(payload []byte) string { if call.ToolName != "SendMessage" || !strings.HasPrefix(strings.TrimSpace(call.ToolInput.To), bridgePrefix) { return "" } + return denySendMessage(sendMessageDenyReason) +} + +// denySendMessage formats a denial using the PreToolUse contract. +func denySendMessage(reason string) string { var out preToolUseOutput out.HookSpecificOutput.HookEventName = "PreToolUse" out.HookSpecificOutput.PermissionDecision = "deny" - out.HookSpecificOutput.PermissionDecisionReason = sendMessageDenyReason + out.HookSpecificOutput.PermissionDecisionReason = reason b, err := json.Marshal(out) if err != nil { return "" @@ -68,7 +75,12 @@ func sendMessageDecision(payload []byte) string { // runSendMessage reads the hook's payload from in and writes its decision to // out. It always exits 0: a hook that fails leaves the call as it was. func runSendMessage(in io.Reader, out io.Writer) int { - payload, err := io.ReadAll(in) + payload, err := io.ReadAll(io.LimitReader(in, proto.MaxHookRequestBytes+1)) + if len(payload) > proto.MaxHookRequestBytes { + _, _ = io.Copy(io.Discard, in) + _, _ = io.WriteString(out, denySendMessage("SendMessage denied: hook input exceeds the 16 MiB limit.")) + return 0 + } if err != nil { return 0 } diff --git a/internal/hookclient/send_message_test.go b/internal/hookclient/send_message_test.go index 27a123580..b0f3c86e1 100644 --- a/internal/hookclient/send_message_test.go +++ b/internal/hookclient/send_message_test.go @@ -64,3 +64,22 @@ func TestRunSendMessage_WritesTheDecisionAndExitsZero(t *testing.T) { t.Fatalf("got exit %d and %q, want 0 and nothing", code, out.String()) } } + +// TestRunSendMessageDeniesOversizedInput checks the tool hook's fail-closed response. +func TestRunSendMessageDeniesOversizedInput(t *testing.T) { + input := strings.NewReader(string(payloadOf("SendMessage", "teammate")) + strings.Repeat(" ", 16<<20)) + var out bytes.Buffer + if code := runSendMessage(input, &out); code != 0 { + t.Fatalf("hook exited %d", code) + } + var decision preToolUseOutput + if err := json.Unmarshal(out.Bytes(), &decision); err != nil { + t.Fatalf("expected a denial, got %q", out.String()) + } + if got := decision.HookSpecificOutput; got.PermissionDecision != "deny" || !strings.Contains(got.PermissionDecisionReason, "16 MiB") { + t.Fatalf("unexpected permission decision %+v", got) + } + if input.Len() != 0 { + t.Fatal("hook left input unread") + } +} diff --git a/internal/hookserver/cli.go b/internal/hookserver/cli.go index 56faede05..e56b1c628 100644 --- a/internal/hookserver/cli.go +++ b/internal/hookserver/cli.go @@ -3,7 +3,6 @@ package hookserver import ( "encoding/json" "fmt" - "io" "net" "time" ) @@ -71,7 +70,10 @@ func (s *Server) handleCLI(conn net.Conn, req hookWireRequest, verified bool) { } _ = conn.SetDeadline(time.Time{}) - s.cliInflight.Store(requestID, conn) + request := s.storeRequest(&s.cliInflight, requestID, conn) + if request == nil { + return + } payload := CLIEventPayload{ RequestID: requestID, WorkspaceID: req.WorkspaceID, @@ -80,8 +82,7 @@ func (s *Server) handleCLI(conn net.Conn, req hookWireRequest, verified bool) { Verified: verified, } if err := sink("cli.request", payload); err != nil { - if val, ok := s.cliInflight.LoadAndDelete(requestID); ok { - c := val.(net.Conn) + if c, ok := takeRequest(&s.cliInflight, requestID); ok { _ = writeNDJSON(c, cliWireResponse{Error: "NexusCode is not connected"}) c.Close() } @@ -89,25 +90,24 @@ func (s *Server) handleCLI(conn net.Conn, req hookWireRequest, verified bool) { } go func() { - // The client sends nothing after its frame, so the read ends only - // when it goes away or the answer closes the connection. - _, _ = io.Copy(io.Discard, conn) - if val, ok := s.cliInflight.LoadAndDelete(requestID); ok { - val.(net.Conn).Close() + waitForClientGone(conn) + if c, ok := takeRequest(&s.cliInflight, requestID); ok { + c.Close() _ = sink("cli.cancel", CLICancelPayload{RequestID: requestID, WorkspaceID: req.WorkspaceID}) } }() deadline := s.cliDeadline() go func() { - timer := time.NewTimer(deadline) - defer timer.Stop() + expires, stop := s.newTimer(deadline) + defer stop() select { - case <-timer.C: + case <-expires: case <-s.done: + case <-request.done: + return } - if val, ok := s.cliInflight.LoadAndDelete(requestID); ok { - c := val.(net.Conn) + if c, ok := takeRequest(&s.cliInflight, requestID); ok { _ = writeNDJSON(c, cliWireResponse{Error: "NexusCode did not answer"}) c.Close() } @@ -117,11 +117,10 @@ func (s *Server) handleCLI(conn net.Conn, req hookWireRequest, verified bool) { // RespondCLI writes main's answer to the CLI request and closes its // connection. func (s *Server) RespondCLI(requestID string, result json.RawMessage) error { - val, ok := s.cliInflight.LoadAndDelete(requestID) + conn, ok := takeRequest(&s.cliInflight, requestID) if !ok { return fmt.Errorf("hookserver: cli request %q not found", requestID) } - conn := val.(net.Conn) err := writeNDJSON(conn, cliWireResponse{OK: true, Result: result}) conn.Close() return err @@ -129,11 +128,11 @@ func (s *Server) RespondCLI(requestID string, result json.RawMessage) error { // closeCLI answers every CLI request still waiting when the server closes. func (s *Server) closeCLI() { - s.cliInflight.Range(func(key, val any) bool { - s.cliInflight.Delete(key) - conn := val.(net.Conn) - _ = writeNDJSON(conn, cliWireResponse{Error: "NexusCode is not connected"}) - conn.Close() + s.cliInflight.Range(func(key, _ any) bool { + if conn, ok := takeRequest(&s.cliInflight, key.(string)); ok { + _ = writeNDJSON(conn, cliWireResponse{Error: "NexusCode is not connected"}) + conn.Close() + } return true }) } diff --git a/internal/hookserver/cli_test.go b/internal/hookserver/cli_test.go index ce39baddb..ffea9f00a 100644 --- a/internal/hookserver/cli_test.go +++ b/internal/hookserver/cli_test.go @@ -5,6 +5,8 @@ import ( "encoding/json" "errors" "net" + "os" + "sync" "sync/atomic" "testing" "time" @@ -91,6 +93,72 @@ func TestCLIClientGoneIsToldToMain(t *testing.T) { } } +// missedCloseConn models the Windows AF_UNIX read which misses an immediate +// peer close. A read deadline frees the stalled read so EOF can be observed. +type missedCloseConn struct { + net.Conn + deadline chan struct{} + peerGone chan struct{} + stop chan struct{} + armed sync.Once + firstRead bool +} + +// SetReadDeadline signals that the watcher can recover from the missed close. +func (c *missedCloseConn) SetReadDeadline(deadline time.Time) error { + if !deadline.IsZero() { + c.armed.Do(func() { close(c.deadline) }) + } + return nil +} + +// Read holds the first read after disconnect until its simulated deadline. +func (c *missedCloseConn) Read(p []byte) (int, error) { + if c.firstRead { + return c.Conn.Read(p) + } + c.firstRead = true + <-c.peerGone + select { + case <-c.deadline: + return 0, os.ErrDeadlineExceeded + case <-c.stop: + return 0, net.ErrClosed + } +} + +// TestCLIClientGoneAfterMissedRead verifies the CLI still cancels when the +// transport delivers EOF only after the first read has timed out. +func TestCLIClientGoneAfterMissedRead(t *testing.T) { + requests := make(chan CLIEventPayload, 1) + cancels := make(chan CLICancelPayload, 1) + srv := newTestServer(t, func(_ string, payload any) error { + switch event := payload.(type) { + case CLIEventPayload: + requests <- event + case CLICancelPayload: + cancels <- event + } + return nil + }) + server, client := net.Pipe() + conn := &missedCloseConn{Conn: server, deadline: make(chan struct{}), peerGone: make(chan struct{}), stop: make(chan struct{})} + defer server.Close() + defer close(conn.stop) + srv.handleCLI(conn, cliRequest(srv.Token()), false) + request := <-requests + _ = client.Close() + close(conn.peerGone) + select { + case event := <-cancels: + if event.RequestID != request.RequestID { + t.Fatalf("cancel = %+v, want request %s", event, request.RequestID) + } + case <-time.After(time.Second): + t.Fatal("CLI cancellation was lost when the read missed the disconnect") + } +} + // A request main never answers is answered with an error at the deadline. func TestCLIRequestTimesOut(t *testing.T) { srv := newTestServer(t, func(string, any) error { return nil }) diff --git a/internal/hookserver/reply_deadline_test.go b/internal/hookserver/reply_deadline_test.go new file mode 100644 index 000000000..fe0fc4d75 --- /dev/null +++ b/internal/hookserver/reply_deadline_test.go @@ -0,0 +1,59 @@ +package hookserver + +import ( + "encoding/json" + "errors" + "net" + "testing" + "time" +) + +// replyDeadlineConn expires writes immediately whenever a deadline is set. +type replyDeadlineConn struct { + net.Conn + deadline time.Time +} + +func (c *replyDeadlineConn) SetWriteDeadline(deadline time.Time) error { + c.deadline = deadline + return c.Conn.SetWriteDeadline(time.Now()) +} + +// TestReplyDeadlineBoundsBlockedHookAndCLI covers a client that never reads. +func TestReplyDeadlineBoundsBlockedHookAndCLI(t *testing.T) { + for _, kind := range []string{"hook", "cli"} { + t.Run(kind, func(t *testing.T) { + server, client := net.Pipe() + defer server.Close() + defer client.Close() + conn := &replyDeadlineConn{Conn: server} + s := &Server{} + if kind == "hook" { + s.storeRequest(&s.inflight, "reply", conn) + } else { + s.storeRequest(&s.cliInflight, "reply", conn) + } + started := time.Now() + result := make(chan error, 1) + go func() { + if kind == "hook" { + result <- s.Respond("reply", HookResponse{Stdout: "answer"}) + } else { + result <- s.RespondCLI("reply", json.RawMessage(`{"answer":true}`)) + } + }() + select { + case err := <-result: + var timeout net.Error + if !errors.As(err, &timeout) || !timeout.Timeout() { + t.Fatalf("blocked client reply returned %v", err) + } + if conn.deadline.Before(started.Add(10*time.Second)) || conn.deadline.After(time.Now().Add(10*time.Second)) { + t.Fatalf("write deadline was %v", conn.deadline) + } + case <-time.After(2 * time.Second): + t.Fatal("reply is stuck behind a client that never reads") + } + }) + } +} diff --git a/internal/hookserver/server.go b/internal/hookserver/server.go index 2db858ea7..c9cb0d70f 100644 --- a/internal/hookserver/server.go +++ b/internal/hookserver/server.go @@ -45,6 +45,7 @@ import ( "time" "github.com/nexus-code/nexus-code/internal/agentpaths" + "github.com/nexus-code/nexus-code/internal/proto" ) // connDeadline 은 hook 클라이언트 연결 1개에 허용하는 최대 응답 대기 시간이다. @@ -64,7 +65,7 @@ const permissionRequestSubcmd = "permission-request" // printWatchSubcmd is the hook a `claude -p` run holds open while it goes on. const printWatchSubcmd = "print-watch" -// goneRecheck is how often watchGone reads again for a client that went +// goneRecheck is how often waitForClientGone reads again for a client that went // away. On Windows a read started as the client closes can wait on with // the close missed, where a read started again returns at once. const goneRecheck = 500 * time.Millisecond @@ -77,7 +78,7 @@ const maxSocketPathLen = 104 // Edit의 old/new 문자열, PostToolUse의 원본 파일과 patch까지 — 그래서 bufio.Scanner // 기본값 64 KiB로는 큰 파일을 쓰는 permission-request가 이벤트 없이 버려졌다. // 실제 payload는 모델 출력 한도에 묶여 수 MiB 이내이므로 16 MiB로 상한을 둔다. -const maxRequestBytes = 16 * 1024 * 1024 +const maxRequestBytes = proto.MaxHookRequestBytes // EventSink 는 hook 이벤트를 main으로 push하는 콜백 타입이다. type EventSink func(name string, payload any) error @@ -142,6 +143,39 @@ type hookWireResponse struct { Error string `json:"error,omitempty"` } +// pendingRequest ties the response deadline to ownership of the connection. +type pendingRequest struct { + conn net.Conn + done chan struct{} +} + +// storeRequest registers a waiting connection in the hook or CLI map. +// A closing server may have finished draining that map already. +func (s *Server) storeRequest(requests *sync.Map, id string, conn net.Conn) *pendingRequest { + request := &pendingRequest{conn: conn, done: make(chan struct{})} + requests.Store(id, request) + select { + case <-s.done: + if c, ok := takeRequest(requests, id); ok { + _ = c.Close() + } + return nil + default: + return request + } +} + +// takeRequest claims the only right to reply and releases the deadline wait. +func takeRequest(requests *sync.Map, id string) (net.Conn, bool) { + value, ok := requests.LoadAndDelete(id) + if !ok { + return nil, false + } + request := value.(*pendingRequest) + close(request.done) + return request.conn, true +} + // Server 는 hook 클라이언트 연결을 수락하고 in-flight 연결 맵을 관리한다. type Server struct { socketPath string @@ -151,13 +185,16 @@ type Server struct { sinkMu sync.Mutex sink EventSink - // inflight 는 hookId → net.Conn 맵이다. sync.Map으로 lock-free 동시 접근을 허용한다. + // inflight holds pending hook requests by hook ID. inflight sync.Map // cliInflight holds the CLI requests waiting on main, by request id. cliInflight sync.Map + // handshakes holds accepted connections until their handlers return. + handshakes sync.Map closeOnce sync.Once done chan struct{} // Close() 호출 시 닫혀 accept loop를 깨운다 + newTimer func(time.Duration) (<-chan time.Time, func()) // readyCh 는 net.Listener가 성공적으로 생성된 직후 한 번 닫힌다. // WaitReady(ctx)가 이 채널로 대기하므로 pull 기반 hook.getInfo RPC가 @@ -176,6 +213,14 @@ type Server struct { // 0600 퍼미션으로 생성한 후 accept loop goroutine을 시작한다. // socketPath가 macOS 104자 제한을 초과하면 에러를 반환한다. func New(agentKey string, sink EventSink) (*Server, error) { + return newServer(agentKey, sink, func(delay time.Duration) (<-chan time.Time, func()) { + timer := time.NewTimer(delay) + return timer.C, func() { timer.Stop() } + }) +} + +// newServer constructs a server with the supplied request deadline clock. +func newServer(agentKey string, sink EventSink, newTimer func(time.Duration) (<-chan time.Time, func())) (*Server, error) { // 1. 소켓 경로 결정 socketPath, socketsDir, err := makeSocketPath(agentKey) if err != nil { @@ -224,6 +269,7 @@ func New(agentKey string, sink EventSink) (*Server, error) { sink: sink, done: make(chan struct{}), readyCh: make(chan struct{}), + newTimer: newTimer, } // net.Listener 생성 직후 readyCh를 닫아 WaitReady 대기자를 깨운다. @@ -262,11 +308,10 @@ func (s *Server) SetEventSink(sink EventSink) { // Respond 는 hookId에 해당하는 in-flight 연결에 응답을 write하고 연결을 닫는다. // hookId가 없거나 이미 닫힌 경우 에러를 반환한다. func (s *Server) Respond(hookID string, response HookResponse) error { - val, ok := s.inflight.LoadAndDelete(hookID) + conn, ok := takeRequest(&s.inflight, hookID) if !ok { return fmt.Errorf("hookserver: hookId %q not found in inflight map", hookID) } - conn := val.(net.Conn) resp := hookWireResponse{ OK: true, Stdout: response.Stdout, @@ -290,6 +335,10 @@ func (s *Server) Close() error { // 아직 살아 있는 in-flight 연결에 응답 생략 동등(exit 0) 응답을 보내고 닫는다. s.ReleaseHooks() s.closeCLI() + s.handshakes.Range(func(key, _ any) bool { + _ = key.(net.Conn).Close() + return true + }) _ = os.Remove(s.socketPath) }) @@ -317,6 +366,15 @@ func (s *Server) acceptLoop() { // 요청 1줄을 읽고 검증한 뒤 in-flight 맵에 등록하고 EventSink를 호출한다. // 120초 deadline 내에 Respond()가 호출되지 않으면 exit 0 응답으로 연결을 닫는다. func (s *Server) handleConn(conn net.Conn) { + s.handshakes.Store(conn, struct{}{}) + defer s.handshakes.Delete(conn) + select { + case <-s.done: + _ = conn.Close() + return + default: + } + // 120초 deadline을 소켓 read에 적용한다. _ = conn.SetDeadline(time.Now().Add(s.connDeadline())) @@ -360,7 +418,10 @@ func (s *Server) handleConn(conn net.Conn) { conn.Close() return } - s.inflight.Store(hookID, conn) + request := s.storeRequest(&s.inflight, hookID, conn) + if request == nil { + return + } // read deadline을 해제하고 응답 대기 상태로 전환한다. // 이후 Respond()에서 conn.Close()가 호출될 때까지 연결이 살아 있어야 한다. @@ -406,16 +467,16 @@ func (s *Server) handleConn(conn net.Conn) { go s.watchGone(hookID, conn, req) } - // deadline goroutine: connDeadline 이내에 Respond()가 없으면 default 응답을 보낸다. - // Respond()가 먼저 호출되면 inflight.LoadAndDelete가 entry를 제거하므로 - // 이 goroutine은 아무것도 하지 않고 종료된다. + // Default to the hook's native response if main never answers. deadline := s.deadlineFor(req.Subcommand) go func() { - timer := time.NewTimer(deadline) - defer timer.Stop() + expires, stop := s.newTimer(deadline) + defer stop() select { - case <-timer.C: + case <-expires: case <-s.done: + case <-request.done: + return } s.respondDefault(hookID) }() @@ -424,19 +485,10 @@ func (s *Server) handleConn(conn net.Conn) { // watchGone tells main when the client of a waiting hook goes away before // it is answered: Claude Code ends a PermissionRequest hook as the user // interrupts at its dialog, and the app must stop offering an answer; a -// print watch's client ends with its run. The client writes nothing after -// its request, so a read returns only once the connection closes, from -// either side, or as its deadline passes, when it reads again (see -// goneRecheck). +// print watch's client ends with its run. func (s *Server) watchGone(hookID string, conn net.Conn, req hookWireRequest) { - var b [1]byte - for { - _ = conn.SetReadDeadline(time.Now().Add(goneRecheck)) - if _, err := conn.Read(b[:]); err != nil && !errors.Is(err, os.ErrDeadlineExceeded) { - break - } - } - if _, waiting := s.inflight.LoadAndDelete(hookID); !waiting { + waitForClientGone(conn) + if _, waiting := takeRequest(&s.inflight, hookID); !waiting { return } conn.Close() @@ -460,6 +512,19 @@ func (s *Server) watchGone(hookID string, conn net.Conn, req hookWireRequest) { } } +// waitForClientGone observes either side closing a connection after the +// request frame. Refreshing the read deadline lets Windows report an EOF +// that the read racing the peer's close missed (see goneRecheck). +func waitForClientGone(conn net.Conn) { + var b [1]byte + for { + _ = conn.SetReadDeadline(time.Now().Add(goneRecheck)) + if _, err := conn.Read(b[:]); err != nil && !errors.Is(err, os.ErrDeadlineExceeded) { + return + } + } +} + // ReleaseHooks answers every hook still waiting with the default response. // The daemon calls it when the app that heard them goes away: an app that // attaches next never heard them, so would never answer them. @@ -475,9 +540,7 @@ func (s *Server) ReleaseHooks() { // fallback. A hook Respond() already answered is gone from inflight and left // alone. func (s *Server) respondDefault(hookID string) { - val, deleted := s.inflight.LoadAndDelete(hookID) - if deleted { - c := val.(net.Conn) + if c, ok := takeRequest(&s.inflight, hookID); ok { _ = writeNDJSON(c, hookWireResponse{OK: true}) c.Close() } @@ -599,13 +662,18 @@ func newHookID() (string, error) { return hex.EncodeToString(buf[:]), nil } -// writeNDJSON 은 v를 JSON으로 직렬화해 개행 문자와 함께 conn에 쓴다. -func writeNDJSON(w interface{ Write([]byte) (int, error) }, v any) error { - data, err := json.Marshal(v) +const replyWriteTimeout = 10 * time.Second + +// writeNDJSON gives each terminal reply its own deadline after the read +// deadline has been cleared. The caller closes the connection afterwards. +func writeNDJSON(conn net.Conn, v any) error { + data, err := proto.MarshalFrame(v) if err != nil { return err } - data = append(data, '\n') - _, err = w.Write(data) + if err := conn.SetWriteDeadline(time.Now().Add(replyWriteTimeout)); err != nil { + return err + } + _, err = conn.Write(data) return err } diff --git a/internal/hookserver/server_test.go b/internal/hookserver/server_test.go index 55ca10570..2e6d21ecb 100644 --- a/internal/hookserver/server_test.go +++ b/internal/hookserver/server_test.go @@ -10,12 +10,53 @@ import ( "path/filepath" "runtime" "strings" + "sync" "sync/atomic" "syscall" "testing" "time" ) +// handshakingConn signals that the handler is waiting for the first line. +type handshakingConn struct { + net.Conn + reading chan struct{} + started sync.Once +} + +func (c *handshakingConn) Read(p []byte) (int, error) { + c.started.Do(func() { close(c.reading) }) + return c.Conn.Read(p) +} + +// TestCloseReleasesIncompleteRequests covers accepted connections that are +// still waiting to finish their first frame when the server closes. +func TestCloseReleasesIncompleteRequests(t *testing.T) { + srv := newTestServer(t, nil) + server, client := net.Pipe() + defer server.Close() + defer client.Close() + conn := &handshakingConn{Conn: server, reading: make(chan struct{})} + finished := make(chan struct{}) + go func() { + defer close(finished) + srv.handleConn(conn) + }() + select { + case <-conn.reading: + case <-time.After(2 * time.Second): + t.Fatal("handler did not begin reading") + } + if err := srv.Close(); err != nil { + t.Fatal(err) + } + select { + case <-finished: + case <-time.After(time.Second): + t.Fatal("the incomplete request is still waiting after Close") + } +} + // TestMain points HOME at a scratch directory, so the servers the tests open // put their sockets there, and the stale-socket sweep in New runs there, not // in the developer's ~/.nexus-code/sockets. @@ -56,6 +97,62 @@ func newTestServer(t *testing.T, sink EventSink) *Server { return srv } +// TestAnsweredRequestsStopDeadlineTimers uses a timer that cannot expire. +// Taking the response must release the timer while the server stays open. +func TestAnsweredRequestsStopDeadlineTimers(t *testing.T) { + for _, kind := range []string{"hook", "cli"} { + t.Run(kind, func(t *testing.T) { + ids := make(chan string, 1) + armed, stopped := make(chan struct{}), make(chan struct{}) + srv, err := newServer(fmt.Sprintf("timer-%d", os.Getpid()), func(_ string, payload any) error { + switch event := payload.(type) { + case HookEventPayload: + ids <- event.HookID + case CLIEventPayload: + ids <- event.RequestID + } + return nil + }, func(time.Duration) (<-chan time.Time, func()) { + close(armed) + return nil, func() { close(stopped) } + }) + if err != nil { + t.Fatal(err) + } + defer srv.Close() + req := cliRequest(srv.Token()) + req.Type = kind + req.Subcommand = "permission-request" + conn := dialAndSendRequest(t, srv.SocketPath(), req) + defer conn.Close() + var id string + select { + case id = <-ids: + case <-time.After(2 * time.Second): + t.Fatal("request event did not arrive") + } + select { + case <-armed: + case <-time.After(2 * time.Second): + t.Fatal("request deadline was never armed") + } + if kind == "cli" { + err = srv.RespondCLI(id, json.RawMessage("null")) + } else { + err = srv.Respond(id, HookResponse{}) + } + if err != nil { + t.Fatal(err) + } + select { + case <-stopped: + case <-time.After(time.Second): + t.Fatal("answered request left its deadline running") + } + }) + } +} + // dialAndSendRequest 는 테스트에서 hookclient를 흉내내 요청을 전송한다. func dialAndSendRequest(t *testing.T, socketPath string, req hookWireRequest) net.Conn { t.Helper() diff --git a/internal/lsp/correlation_test.go b/internal/lsp/correlation_test.go index f388b0330..443c51ade 100644 --- a/internal/lsp/correlation_test.go +++ b/internal/lsp/correlation_test.go @@ -27,7 +27,7 @@ func TestServerRequestCorrelationCleansThousandRequests(t *testing.T) { var writes bytes.Buffer server := newServerProcess(service, "lsp-test", SpawnParams{}, 0) server.stdin = bufferWriteCloser{Buffer: &writes} - service.storeServer(server) + service.servers[server.id] = server const requestCount = 1000 for i := 0; i < requestCount; i++ { diff --git a/internal/lsp/errors.go b/internal/lsp/errors.go index 2e02ffb2d..e2d3f04eb 100644 --- a/internal/lsp/errors.go +++ b/internal/lsp/errors.go @@ -1,13 +1,11 @@ package lsp import ( - "fmt" - "github.com/nexus-code/nexus-code/internal/proto" ) func requestFailed(format string, args ...any) error { - return proto.CodedError{Code: proto.CodeRequestFailed, Msg: fmt.Sprintf(format, args...)} + return proto.RequestFailed(format, args...) } func protocolError(message string) error { diff --git a/internal/lsp/find_binary.go b/internal/lsp/find_binary.go index 2bdd1668c..a9bfc9e42 100644 --- a/internal/lsp/find_binary.go +++ b/internal/lsp/find_binary.go @@ -1,6 +1,7 @@ package lsp import ( + "bytes" "context" "encoding/json" "errors" @@ -34,6 +35,8 @@ type FindBinaryParams struct { // Path is where the user said it is, "~" standing for their home. It is // tried first; when nothing is there the search goes on. Path string `json:"path,omitempty"` + // SearchPath is an optional PATH from an agent profile, tried before the host paths. + SearchPath string `json:"searchPath,omitempty"` // Dir is where the server will run, the workspace root. A rustup proxy // picks its toolchain there. Dir string `json:"dir,omitempty"` @@ -59,7 +62,15 @@ func (s *Service) FindBinary(_ context.Context, raw json.RawMessage) (any, error home, _ := os.UserHomeDir() shellPath := loginShellPath() - dirs := searchDirs(home, shellPath) + var preferred []string + for _, dir := range filepath.SplitList(p.SearchPath) { + dir = expandHome(dir, home) + if !filepath.IsAbs(dir) { + dir = filepath.Join(p.Dir, dir) + } + preferred = append(preferred, dir) + } + dirs := append(preferred, searchDirs(home, shellPath)...) path := findBinary(p.Name, expandHome(strings.TrimSpace(p.Path), home), p.Dir, dirs) if path == "" { return FindBinaryResult{}, nil @@ -74,7 +85,7 @@ func findBinary(name, userPath, workDir string, dirs []string) string { if userPath != "" { // On Windows "gopls" stands for "gopls.exe", as in a shell. for _, candidate := range executableNames(userPath) { - if canRunServer(candidate, workDir) { + if canRunServer(candidate, workDir) && !isAgentWrapper(name, candidate) { return candidate } } @@ -84,7 +95,7 @@ func findBinary(name, userPath, workDir string, dirs []string) string { continue } for _, candidate := range executableNames(filepath.Join(dir, name)) { - if canRunServer(candidate, workDir) { + if canRunServer(candidate, workDir) && !isAgentWrapper(name, candidate) { return candidate } } @@ -92,6 +103,23 @@ func findBinary(name, userPath, workDir string, dirs []string) string { return "" } +// isAgentWrapper keeps CLI selection on the real executable even when wrappers +// appear on the login shell's or profile's PATH. +func isAgentWrapper(name, path string) bool { + if name != "claude" && name != "codex" { + return false + } + file, err := os.Open(path) + if err != nil { + return false + } + defer file.Close() + var header [512]byte + n, _ := file.Read(header[:]) + return bytes.Contains(header[:n], []byte("nexus-code "+name+" wrapper")) || + bytes.Contains(header[:n], []byte("cmux claude wrapper")) +} + func canRunServer(path, workDir string) bool { return isExecutableFile(path) && !isRustupProxyWithoutTool(path, workDir) } diff --git a/internal/lsp/find_binary_test.go b/internal/lsp/find_binary_test.go index 57fbe00ca..7fddc070f 100644 --- a/internal/lsp/find_binary_test.go +++ b/internal/lsp/find_binary_test.go @@ -214,6 +214,31 @@ func TestFindBinaryGivesTheServerTheInstallDirectories(t *testing.T) { } } +func TestFindBinaryUsesProfilePathAndSkipsClaudeWrappers(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("SHELL", filepath.Join(home, "no-shell")) + wrapper := writeExecutable(t, filepath.Join(home, "wrapper"), "claude") + if err := os.WriteFile(wrapper, []byte("#!/bin/bash\n# nexus-code claude wrapper\n"), 0o755); err != nil { + t.Fatal(err) + } + fallback := writeExecutable(t, filepath.Join(home, "fallback"), "claude") + t.Setenv("PATH", filepath.Dir(fallback)) + want := writeExecutable(t, filepath.Join(home, "profile"), "claude") + got, err := New().FindBinary(context.Background(), mustJSON(t, map[string]string{ + "name": "claude", + "dir": home, + "searchPath": strings.Join([]string{filepath.Dir(wrapper), filepath.Dir(want)}, string(os.PathListSeparator)), + })) + if err != nil { + t.Fatal(err) + } + if result := got.(FindBinaryResult).Path; result != want { + t.Fatalf("FindBinary() = %q, want %q", result, want) + } +} + func TestLoginShellPathIsAskedEachTime(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("Windows has no login shell") diff --git a/internal/lsp/procstats.go b/internal/lsp/procstats.go index 230c1e7d9..bc5a75c02 100644 --- a/internal/lsp/procstats.go +++ b/internal/lsp/procstats.go @@ -1,9 +1,10 @@ package lsp import ( - "bytes" "strconv" "strings" + + hostproc "github.com/nexus-code/nexus-code/internal/process" ) // processEntry is one row of the system's process table. @@ -52,11 +53,8 @@ func treeRSS(table map[int]processEntry, roots []int) map[int]uint64 { // ")": field 3 (state) comes first, then field 4 (ppid), and field 24 (rss) // is the 22nd. func parseProcStat(data []byte, pageSize uint64) (processEntry, bool) { - end := bytes.LastIndexByte(data, ')') - if end < 0 { - return processEntry{}, false - } - fields := strings.Fields(string(data[end+1:])) + _, suffix := hostproc.SplitPIDStat(data) + fields := strings.Fields(string(suffix)) if len(fields) < 22 { return processEntry{}, false } diff --git a/internal/lsp/server.go b/internal/lsp/server.go index beb966464..b38854a35 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -31,7 +31,9 @@ type serverProcess struct { stdout io.ReadCloser stderr io.ReadCloser - writeMu sync.Mutex + writeSlot chan struct{} + inputDone chan struct{} + abortOnce sync.Once mu sync.Mutex internalSeq uint64 @@ -76,6 +78,8 @@ func newServerProcess(service *Service, id string, p SpawnParams, idleTimeout ti pendingServerRequests: make(map[string]json.RawMessage), pendingWatchedFileRegistrations: make(map[string][]watchedFileRegistration), done: make(chan struct{}), + writeSlot: make(chan struct{}, 1), + inputDone: make(chan struct{}), } } @@ -192,7 +196,7 @@ func (p *serverProcess) initialize(ctx context.Context) (json.RawMessage, *Serve } } - if err := p.notify("initialized", map[string]any{}); err != nil { + if err := p.notify(ctx, "initialized", map[string]any{}); err != nil { return nil, nil, err } return capabilities, serverInfo, nil @@ -267,7 +271,7 @@ func (p *serverProcess) request(ctx context.Context, method string, params any) p.deleteInternal(key) return nil, err } - if err := p.sendRaw(raw); err != nil { + if err := p.sendRaw(ctx, raw); err != nil { p.deleteInternal(key) return nil, err } @@ -293,7 +297,7 @@ func (p *serverProcess) request(ctx context.Context, method string, params any) } } -func (p *serverProcess) notify(method string, params any) error { +func (p *serverProcess) notify(ctx context.Context, method string, params any) error { message := struct { JSONRPC string `json:"jsonrpc"` Method string `json:"method"` @@ -307,10 +311,10 @@ func (p *serverProcess) notify(method string, params any) error { if err != nil { return err } - return p.sendRaw(raw) + return p.sendRaw(ctx, raw) } -func (p *serverProcess) cancel(requestID json.RawMessage) error { +func (p *serverProcess) cancel(ctx context.Context, requestID json.RawMessage) error { message := struct { JSONRPC string `json:"jsonrpc"` Method string `json:"method"` @@ -326,27 +330,59 @@ func (p *serverProcess) cancel(requestID json.RawMessage) error { if err != nil { return err } - return p.sendRaw(raw) + return p.sendRaw(ctx, raw) } -func (p *serverProcess) sendRaw(message json.RawMessage) error { +// sendRaw bounds both writer acquisition and the pipe write. An interrupted +// write may leave a partial frame, so that server's input is closed permanently. +func (p *serverProcess) sendRaw(ctx context.Context, message json.RawMessage) error { frame, err := EncodeRawMessage(message) if err != nil { return protocolError(err.Error()) } - p.mu.Lock() - exited := p.exited - p.mu.Unlock() - if exited { + writeCtx, cancel := context.WithTimeout(ctx, stdinWriteTimeout) + defer cancel() + select { + case p.writeSlot <- struct{}{}: + defer func() { <-p.writeSlot }() + case <-writeCtx.Done(): + return writeCtx.Err() + case <-p.inputDone: + return requestFailed("lsp server input closed") + case <-p.done: + return requestFailed("lsp server exited") + } + if err := writeCtx.Err(); err != nil { + return err + } + select { + case <-p.inputDone: + return requestFailed("lsp server input closed") + case <-p.done: return requestFailed("lsp server exited") + default: } - p.writeMu.Lock() - defer p.writeMu.Unlock() - _, err = p.stdin.Write(frame) - if err != nil { - return requestFailed("lsp write failed: %s", err) + // A write deadline is unavailable on some child pipes. Keep the waiter + // cancellable independently of the OS write and abort the pipe on timeout. + written := make(chan error, 1) + go func() { + _, err := p.stdin.Write(frame) + written <- err + }() + select { + case err := <-written: + if err != nil { + return requestFailed("lsp write failed: %s", err) + } + return nil + case <-writeCtx.Done(): + p.abortInput() + return writeCtx.Err() + case <-p.inputDone: + return requestFailed("lsp server input closed") + case <-p.done: + return requestFailed("lsp server exited") } - return nil } diff --git a/internal/lsp/server_lifecycle.go b/internal/lsp/server_lifecycle.go index d31d634ef..fd5d3bf45 100644 --- a/internal/lsp/server_lifecycle.go +++ b/internal/lsp/server_lifecycle.go @@ -26,12 +26,22 @@ func (p *serverProcess) doShutdown(ctx context.Context) error { _, requestErr := p.request(requestCtx, "shutdown", nil) cancel() - _ = p.notify("exit", nil) - if p.waitForExit(shutdownExitGrace) { - return requestErr + // A server may ignore shutdown yet still honor exit. Give the exit frame + // and process a shared grace period, including any wait for its writer. + if ctx.Err() == nil { + exitCtx, cancelExit := context.WithTimeout(ctx, shutdownExitGrace) + if err := p.notify(exitCtx, "exit", nil); err == nil { + select { + case <-p.done: + cancelExit() + return requestErr + case <-exitCtx.Done(): + } + } + cancelExit() } - p.kill() + p.abortInput() if p.waitForExit(shutdownExitGrace) { return requestErr } @@ -41,10 +51,20 @@ func (p *serverProcess) doShutdown(ctx context.Context) error { func (p *serverProcess) forceClose() { p.stopIdleTimer() p.markShuttingDown() - p.kill() + p.abortInput() p.waitForExit(shutdownExitGrace) } +// abortInput rejects future writes immediately and unblocks the current one. +// Kill the reader before closing the pipe, which can wait for OS I/O. +func (p *serverProcess) abortInput() { + p.abortOnce.Do(func() { + close(p.inputDone) + p.kill() + go func() { _ = p.stdin.Close() }() + }) +} + // markShuttingDown has serverExited say the agent stopped the server. func (p *serverProcess) markShuttingDown() { p.mu.Lock() @@ -58,7 +78,7 @@ func (p *serverProcess) resetIdleTimer() { } p.mu.Lock() defer p.mu.Unlock() - if p.exited { + if p.exited || p.shuttingDown { return } if p.idleTimer == nil { diff --git a/internal/lsp/server_messages.go b/internal/lsp/server_messages.go index f3d7ed845..defeea458 100644 --- a/internal/lsp/server_messages.go +++ b/internal/lsp/server_messages.go @@ -1,6 +1,7 @@ package lsp import ( + "context" "encoding/json" "io" ) @@ -122,7 +123,7 @@ func (p *serverProcess) handleServerRequest(id json.RawMessage, method string, p } } -func (p *serverProcess) respondServerRequest(params RespondServerRequestParams) error { +func (p *serverProcess) respondServerRequest(ctx context.Context, params RespondServerRequestParams) error { p.mu.Lock() id := p.pendingServerRequests[params.AgentRequestID] watchedFileRegistrations := p.pendingWatchedFileRegistrations[params.AgentRequestID] @@ -151,7 +152,7 @@ func (p *serverProcess) respondServerRequest(params RespondServerRequestParams) if err != nil { return err } - if err := p.sendRaw(raw); err != nil { + if err := p.sendRaw(ctx, raw); err != nil { return err } if len(params.Error) == 0 { @@ -173,5 +174,5 @@ func (p *serverProcess) sendErrorResponse(id json.RawMessage, code int, message if err != nil { return } - _ = p.sendRaw(raw) + _ = p.sendRaw(context.Background(), raw) } diff --git a/internal/lsp/service.go b/internal/lsp/service.go index 9cb4a5885..e12b98407 100644 --- a/internal/lsp/service.go +++ b/internal/lsp/service.go @@ -23,6 +23,7 @@ type Service struct { mu sync.Mutex sink EventSink servers map[string]*serverProcess + closed bool // serverIDPrefix sets this agent's server ids apart from those of the // agent it replaced: when a local agent restarts, calls queued for the @@ -53,15 +54,25 @@ func newServerIDPrefix() string { // document wrong until it is reopened. func Register(d *dispatch.Dispatcher, service *Service) { d.Register("lsp.spawn", service.Spawn) - d.RegisterOrdered("lsp.send", service.Send) - d.RegisterOrdered("lsp.notify", service.Notify) - d.RegisterOrdered("lsp.cancel", service.Cancel) + d.RegisterOrderedBy("lsp.send", service.Send, serverQueueKey) + d.RegisterOrderedBy("lsp.notify", service.Notify, serverQueueKey) + d.RegisterOrderedBy("lsp.cancel", service.Cancel, serverQueueKey) d.Register("lsp.shutdown", service.Shutdown) - d.RegisterOrdered("lsp.respondServerRequest", service.RespondServerRequest) + d.RegisterOrderedBy("lsp.respondServerRequest", service.RespondServerRequest, serverQueueKey) d.Register("lsp.stats", service.Stats) d.Register("lsp.findBinary", service.FindBinary) } +// serverQueueKey keeps document updates, requests and cancellations for one +// server in the same arrival queue. +func serverQueueKey(raw json.RawMessage) string { + var params struct { + ServerID string `json:"serverId"` + } + _ = json.Unmarshal(raw, ¶ms) + return "lsp:" + params.ServerID +} + // SetEventSink wires the service to the stdio host after both are constructed. func (s *Service) SetEventSink(sink EventSink) { s.mu.Lock() @@ -72,6 +83,7 @@ func (s *Service) SetEventSink(sink EventSink) { // Close terminates all active language server processes. func (s *Service) Close() { s.mu.Lock() + s.closed = true servers := make([]*serverProcess, 0, len(s.servers)) for id, server := range s.servers { servers = append(servers, server) @@ -79,9 +91,15 @@ func (s *Service) Close() { } s.mu.Unlock() + var cleanup sync.WaitGroup for _, server := range servers { - server.forceClose() + cleanup.Add(1) + go func() { + defer cleanup.Done() + server.forceClose() + }() } + cleanup.Wait() } func (s *Service) Spawn(ctx context.Context, raw json.RawMessage) (any, error) { @@ -133,10 +151,7 @@ func (s *Service) Spawn(ctx context.Context, raw json.RawMessage) (any, error) { InitializationOptions: p.InitializationOptions, }, idleTimeout) - s.storeServer(server) - - if err := server.start(ctx); err != nil { - s.removeServer(serverID, server) + if err := s.startServer(ctx, server); err != nil { return nil, err } @@ -162,6 +177,21 @@ func (s *Service) Spawn(ctx context.Context, raw json.RawMessage) (any, error) { return SpawnResult{ServerID: serverID, Capabilities: capabilities, ServerInfo: serverInfo}, nil } +// startServer publishes the child and its pipes together. A concurrent close +// or early process exit can only see the fully started process. +func (s *Service) startServer(ctx context.Context, server *serverProcess) error { + s.mu.Lock() + defer s.mu.Unlock() + if s.closed { + return requestFailed("lsp service closed") + } + if err := server.start(ctx); err != nil { + return err + } + s.servers[server.id] = server + return nil +} + // Stats reports the memory each server takes, with the processes it // started. func (s *Service) Stats(_ context.Context, _ json.RawMessage) (any, error) { @@ -192,7 +222,7 @@ func (s *Service) Stats(_ context.Context, _ json.RawMessage) (any, error) { return result, nil } -func (s *Service) Send(_ context.Context, raw json.RawMessage) (any, error) { +func (s *Service) Send(ctx context.Context, raw json.RawMessage) (any, error) { var p SendParams if err := decodeParams(raw, &p, "lsp.send params must include serverId and message"); err != nil { return nil, err @@ -202,7 +232,7 @@ func (s *Service) Send(_ context.Context, raw json.RawMessage) (any, error) { return nil, err } server.resetIdleTimer() - if err := server.sendRaw(p.Message); err != nil { + if err := server.sendRaw(ctx, p.Message); err != nil { return nil, err } return struct{}{}, nil @@ -214,7 +244,7 @@ func (s *Service) Send(_ context.Context, raw json.RawMessage) (any, error) { // a semantically-empty round-trip on every keystroke. The handler forwards // the message to the LSP child process and returns a void ack just like Send, // but having a dedicated method name keeps the intent unambiguous on both sides. -func (s *Service) Notify(_ context.Context, raw json.RawMessage) (any, error) { +func (s *Service) Notify(ctx context.Context, raw json.RawMessage) (any, error) { var p NotifyParams if err := decodeParams(raw, &p, "lsp.notify params must include serverId and message"); err != nil { return nil, err @@ -224,13 +254,13 @@ func (s *Service) Notify(_ context.Context, raw json.RawMessage) (any, error) { return nil, err } server.resetIdleTimer() - if err := server.sendRaw(p.Message); err != nil { + if err := server.sendRaw(ctx, p.Message); err != nil { return nil, err } return struct{}{}, nil } -func (s *Service) Cancel(_ context.Context, raw json.RawMessage) (any, error) { +func (s *Service) Cancel(ctx context.Context, raw json.RawMessage) (any, error) { var p CancelParams if err := decodeParams(raw, &p, "lsp.cancel params must include serverId and requestId"); err != nil { return nil, err @@ -243,7 +273,7 @@ func (s *Service) Cancel(_ context.Context, raw json.RawMessage) (any, error) { return nil, err } server.resetIdleTimer() - if err := server.cancel(p.RequestID); err != nil { + if err := server.cancel(ctx, p.RequestID); err != nil { return nil, err } return struct{}{}, nil @@ -266,7 +296,7 @@ func (s *Service) Shutdown(ctx context.Context, raw json.RawMessage) (any, error return struct{}{}, nil } -func (s *Service) RespondServerRequest(_ context.Context, raw json.RawMessage) (any, error) { +func (s *Service) RespondServerRequest(ctx context.Context, raw json.RawMessage) (any, error) { var p RespondServerRequestParams if err := decodeParams(raw, &p, "lsp.respondServerRequest params must include serverId and agentRequestId"); err != nil { return nil, err @@ -289,7 +319,7 @@ func (s *Service) RespondServerRequest(_ context.Context, raw json.RawMessage) ( return nil, err } server.resetIdleTimer() - if err := server.respondServerRequest(p); err != nil { + if err := server.respondServerRequest(ctx, p); err != nil { return nil, err } return struct{}{}, nil @@ -309,12 +339,6 @@ func (s *Service) nextAgentRequestID(serverID string) string { return fmt.Sprintf("%s-server-request-%d", serverID, s.nextAgentSeq) } -func (s *Service) storeServer(server *serverProcess) { - s.mu.Lock() - defer s.mu.Unlock() - s.servers[server.id] = server -} - func (s *Service) removeServer(id string, expected *serverProcess) { s.mu.Lock() defer s.mu.Unlock() diff --git a/internal/lsp/service_test.go b/internal/lsp/service_test.go index 2c7b99682..1a5f13729 100644 --- a/internal/lsp/service_test.go +++ b/internal/lsp/service_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - agentfs "github.com/nexus-code/nexus-code/internal/fs" + "github.com/nexus-code/nexus-code/internal/filewatch" ) type recordedEvent struct { @@ -276,10 +276,10 @@ func TestServiceRoutesFSChangedToRegisteredWatchedFiles(t *testing.T) { } waitUntil(t, 2*time.Second, func() bool { return service.serverRequestCount(result.ServerID) == 0 }) - err := service.HandleFSChanged(agentfs.FsChangedPayload{ - Changes: []agentfs.FsChange{ - {RelPath: "src/main.go", Kind: agentfs.FsChangeModified}, - {RelPath: "README.md", Kind: agentfs.FsChangeModified}, + err := service.HandleFSChanged(filewatch.FsChangedPayload{ + Changes: []filewatch.FsChange{ + {RelPath: "src/main.go", Kind: filewatch.FsChangeModified}, + {RelPath: "README.md", Kind: filewatch.FsChangeModified}, }, }) if err != nil { diff --git a/internal/lsp/stall_test.go b/internal/lsp/stall_test.go new file mode 100644 index 000000000..9a597981d --- /dev/null +++ b/internal/lsp/stall_test.go @@ -0,0 +1,73 @@ +package lsp + +import ( + "context" + "encoding/json" + "io" + "sync" + "testing" + "time" +) + +// blockedInput models a pipe whose child stopped reading. +type blockedInput struct { + started chan struct{} + closed chan struct{} + once sync.Once + start sync.Once +} + +// Write blocks until the input is force-closed. +func (p *blockedInput) Write([]byte) (int, error) { + p.start.Do(func() { close(p.started) }) + <-p.closed + return 0, io.ErrClosedPipe +} + +// Close releases the blocked writer. +func (p *blockedInput) Close() error { + p.once.Do(func() { close(p.closed) }) + return nil +} + +// TestShutdownClosesBlockedInput verifies that a canceled shutdown can tear +// down a server even when an earlier LSP frame still owns its stdin writer. +func TestShutdownClosesBlockedInput(t *testing.T) { + svc := New() + server := newServerProcess(svc, "stalled", SpawnParams{}, 0) + input := &blockedInput{started: make(chan struct{}), closed: make(chan struct{})} + server.stdin = input + t.Cleanup(func() { _ = input.Close() }) + svc.servers[server.id] = server + go func() { + <-input.closed + server.markExited(nil) + }() + + notify := make(chan error, 1) + go func() { + _, err := svc.Notify(context.Background(), json.RawMessage(`{"serverId":"stalled","message":{"jsonrpc":"2.0","method":"textDocument/didChange"}}`)) + notify <- err + }() + <-input.started + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + shutdown := make(chan error, 1) + go func() { shutdown <- server.Shutdown(ctx) }() + timer := time.NewTimer(time.Second) + defer timer.Stop() + select { + case <-shutdown: + case <-timer.C: + t.Fatal("shutdown waited on the blocked stdin writer") + } + select { + case err := <-notify: + if err == nil { + t.Fatal("blocked write succeeded after shutdown") + } + case <-timer.C: + t.Fatal("shutdown left the write blocked") + } +} diff --git a/internal/lsp/types.go b/internal/lsp/types.go index 523f7c1de..3121720ae 100644 --- a/internal/lsp/types.go +++ b/internal/lsp/types.go @@ -13,6 +13,7 @@ const ( defaultIdleTimeout = 30 * time.Minute initializeTimeout = 5 * time.Second + stdinWriteTimeout = 2 * time.Second shutdownRequestTimeout = 750 * time.Millisecond shutdownExitGrace = 750 * time.Millisecond diff --git a/internal/lsp/watched_files_router.go b/internal/lsp/watched_files_router.go index bcad2f741..6dfd544cf 100644 --- a/internal/lsp/watched_files_router.go +++ b/internal/lsp/watched_files_router.go @@ -1,13 +1,14 @@ package lsp import ( + "context" "encoding/json" "errors" "path" "path/filepath" "strings" - agentfs "github.com/nexus-code/nexus-code/internal/fs" + "github.com/nexus-code/nexus-code/internal/filewatch" ) const ( @@ -44,7 +45,7 @@ type watchedFileGlob struct { // HandleFSChanged routes one already-debounced fs.changed payload to active LSP // servers that successfully registered workspace/didChangeWatchedFiles. -func (s *Service) HandleFSChanged(payload agentfs.FsChangedPayload) error { +func (s *Service) HandleFSChanged(payload filewatch.FsChangedPayload) error { if len(payload.Changes) == 0 { return nil } @@ -57,7 +58,7 @@ func (s *Service) HandleFSChanged(payload agentfs.FsChangedPayload) error { continue } server.resetIdleTimer() - if err := server.notify(methodDidChangeWatchedFiles, didChangeWatchedFilesParams{Changes: changes}); err != nil { + if err := server.notify(context.Background(), methodDidChangeWatchedFiles, didChangeWatchedFilesParams{Changes: changes}); err != nil { errs = append(errs, err) } } @@ -172,7 +173,7 @@ func (p *serverProcess) addWatchedFileRegistrations(registrations []watchedFileR p.watchedFileRegistrations = append(p.watchedFileRegistrations, registrations...) } -func (p *serverProcess) matchingWatchedFileChanges(changes []agentfs.FsChange) []lspFileEvent { +func (p *serverProcess) matchingWatchedFileChanges(changes []filewatch.FsChange) []lspFileEvent { p.mu.Lock() registrations := append([]watchedFileRegistration(nil), p.watchedFileRegistrations...) p.mu.Unlock() @@ -227,13 +228,13 @@ func (r watchedFileRegistration) matchesNormalized(relPath string) bool { return false } -func lspFileChangeType(kind agentfs.FsChangeKind) (int, bool) { +func lspFileChangeType(kind filewatch.FsChangeKind) (int, bool) { switch kind { - case agentfs.FsChangeAdded: + case filewatch.FsChangeAdded: return lspFileChangeCreated, true - case agentfs.FsChangeModified: + case filewatch.FsChangeModified: return lspFileChangeChanged, true - case agentfs.FsChangeDeleted: + case filewatch.FsChangeDeleted: return lspFileChangeDeleted, true default: return 0, false diff --git a/internal/ports/parse.go b/internal/ports/parse.go index 0153e618d..a75dde082 100644 --- a/internal/ports/parse.go +++ b/internal/ports/parse.go @@ -1,10 +1,11 @@ package ports import ( - "bytes" "encoding/binary" "strconv" "strings" + + hostproc "github.com/nexus-code/nexus-code/internal/process" ) // baseName strips a program's folder: ps names some programs by their path. @@ -64,12 +65,11 @@ func parseLsof(out []byte) []Listener { // spaces and parentheses itself, so the fields after it are counted from the // last ")": field 3 (state) comes first, then field 4 (ppid). func parseProcStat(data []byte) (Process, bool) { - open := bytes.IndexByte(data, '(') - end := bytes.LastIndexByte(data, ')') - if open < 0 || end < open { + name, suffix := hostproc.SplitPIDStat(data) + if name == nil { return Process{}, false } - fields := strings.Fields(string(data[end+1:])) + fields := strings.Fields(string(suffix)) if len(fields) < 2 { return Process{}, false } @@ -77,7 +77,7 @@ func parseProcStat(data []byte) (Process, bool) { if err != nil { return Process{}, false } - return Process{PPID: ppid, Name: string(data[open+1 : end])}, true + return Process{PPID: ppid, Name: string(name)}, true } // tcpListen is the state /proc/net/tcp gives a listening socket. diff --git a/internal/process/group_unix.go b/internal/process/group_unix.go new file mode 100644 index 000000000..1ff920a1c --- /dev/null +++ b/internal/process/group_unix.go @@ -0,0 +1,18 @@ +//go:build darwin || linux + +package process + +import ( + "errors" + "syscall" +) + +// SignalGroup sends signal to the process group whose ID is pid. A group +// that has already exited counts as success. +func SignalGroup(pid int, signal syscall.Signal) error { + err := syscall.Kill(-pid, signal) + if errors.Is(err, syscall.ESRCH) { + return nil + } + return err +} diff --git a/internal/process/job_windows.go b/internal/process/job_windows.go new file mode 100644 index 000000000..cd9bfcbdb --- /dev/null +++ b/internal/process/job_windows.go @@ -0,0 +1,129 @@ +//go:build windows + +package process + +import ( + "errors" + "os" + "time" + "unsafe" + + "golang.org/x/sys/windows" +) + +// AssignJob creates a job and assigns pid to it. The caller owns the returned +// handle. A job with killOnClose ends its remaining processes when released. +func AssignJob(pid int, killOnClose bool) (windows.Handle, error) { + job, err := windows.CreateJobObject(nil, nil) + if err != nil { + return 0, err + } + // Neither policy permits breakaway. Git Bash would start its programs + // outside the job if it were allowed to. + if killOnClose { + var info windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION + info.BasicLimitInformation.LimitFlags = windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE + if _, err := windows.SetInformationJobObject(job, windows.JobObjectExtendedLimitInformation, + uintptr(unsafe.Pointer(&info)), uint32(unsafe.Sizeof(info))); err != nil { + _ = windows.CloseHandle(job) + return 0, err + } + } + target, err := windows.OpenProcess(windows.PROCESS_SET_QUOTA|windows.PROCESS_TERMINATE, false, uint32(pid)) + if err != nil { + _ = windows.CloseHandle(job) + return 0, err + } + defer windows.CloseHandle(target) + if err := windows.AssignProcessToJobObject(job, target); err != nil { + _ = windows.CloseHandle(job) + return 0, err + } + return job, nil +} + +// ResumeSuspended opens the primary thread, whose handle os/exec closes. +// A newly created suspended process has not had a chance to create another. +func ResumeSuspended(pid uint32) error { + snapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPTHREAD, 0) + if err != nil { + return err + } + defer windows.CloseHandle(snapshot) + var entry windows.ThreadEntry32 + entry.Size = uint32(unsafe.Sizeof(entry)) + for err := windows.Thread32First(snapshot, &entry); err == nil; err = windows.Thread32Next(snapshot, &entry) { + if entry.OwnerProcessID != pid { + continue + } + thread, err := windows.OpenThread(windows.THREAD_SUSPEND_RESUME, false, entry.ThreadID) + if err != nil { + return err + } + defer windows.CloseHandle(thread) + _, err = windows.ResumeThread(thread) + return err + } + return os.ErrProcessDone +} + +// TerminateJobAndWait stops the job and waits on the process handles captured +// before termination. A process handle is signaled after its resources are +// released, including the working directory. +func TerminateJobAndWait(job windows.Handle, timeout time.Duration) error { + pids, listErr := jobProcessIDs(job) + var handles []windows.Handle + var openErr error + for _, pid := range pids { + process, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid)) + if errors.Is(err, windows.ERROR_INVALID_PARAMETER) { + continue // The process exited before OpenProcess. + } + if err != nil { + openErr = errors.Join(openErr, err) + continue + } + handles = append(handles, process) + } + defer func() { + for _, process := range handles { + _ = windows.CloseHandle(process) + } + }() + if err := windows.TerminateJobObject(job, 1); err != nil { + return errors.Join(listErr, openErr, err) + } + deadline := time.Now().Add(timeout) + for _, process := range handles { + remaining := max(0, time.Until(deadline)) + waitMillis := uint32((remaining + time.Millisecond - 1) / time.Millisecond) + event, err := windows.WaitForSingleObject(process, waitMillis) + if err != nil { + return errors.Join(listErr, openErr, err) + } + if event == uint32(windows.WAIT_TIMEOUT) { + return errors.Join(listErr, openErr, os.ErrDeadlineExceeded) + } + } + return errors.Join(listErr, openErr) +} + +// jobProcessIDs decodes JOBOBJECT_BASIC_PROCESS_ID_LIST. Its two DWORD +// counts precede the pointer-sized array of process IDs. +func jobProcessIDs(job windows.Handle) ([]uintptr, error) { + const wordSize = int(unsafe.Sizeof(uintptr(0))) + const headerWords = 8 / wordSize + for capacity := 16; ; capacity *= 2 { + words := make([]uintptr, headerWords+capacity) + header := (*[2]uint32)(unsafe.Pointer(&words[0])) + err := windows.QueryInformationJobObject(job, windows.JobObjectBasicProcessIdList, + uintptr(unsafe.Pointer(&words[0])), uint32(len(words)*wordSize), nil) + if errors.Is(err, windows.ERROR_MORE_DATA) { + continue + } + if err != nil { + return nil, err + } + return words[headerWords : headerWords+int(header[1])], nil + } +} diff --git a/internal/process/procstat.go b/internal/process/procstat.go new file mode 100644 index 000000000..5b2fa3e1b --- /dev/null +++ b/internal/process/procstat.go @@ -0,0 +1,18 @@ +package process + +import "bytes" + +// SplitPIDStat splits a Linux /proc//stat record into its command name +// and the fields beginning with state. The name may contain spaces and +// parentheses, so the last closing parenthesis ends it. Callers interested +// in the fields alone can still read them if the opening parenthesis is absent. +func SplitPIDStat(data []byte) (name, fields []byte) { + end := bytes.LastIndexByte(data, ')') + if end < 0 { + return nil, nil + } + if start := bytes.IndexByte(data[:end], '('); start >= 0 { + name = data[start+1 : end] + } + return name, data[end+1:] +} diff --git a/internal/proto/proto.go b/internal/proto/proto.go index 61687f185..f202f6509 100644 --- a/internal/proto/proto.go +++ b/internal/proto/proto.go @@ -26,6 +26,9 @@ import ( // -X); the default is what a plain `go build` or test reports. var ServerVersion = "0.1.0" +// MaxHookRequestBytes bounds the NDJSON request accepted by the hook socket. +const MaxHookRequestBytes = 16 * 1024 * 1024 + // Protocol version constants advertised on the boot Ready frame so the // client can refuse to talk to a server with an incompatible envelope // shape. Bump ProtocolVersion when the wire format changes in a way @@ -280,6 +283,11 @@ func ProtocolError(message string) CodedError { return CodedError{Code: CodeProtocolError, Msg: message} } +// RequestFailed formats a domain failure using the default RPC error code. +func RequestFailed(format string, args ...any) CodedError { + return CodedError{Code: CodeRequestFailed, Msg: fmt.Sprintf(format, args...)} +} + // NewError builds a CodedError with an arbitrary stable code. // Domain handlers use this to attach a specific wire code (e.g. // CodeUnavailable) to an error return without constructing a diff --git a/internal/pty/errors.go b/internal/pty/errors.go index e1c0904e5..93bfdf297 100644 --- a/internal/pty/errors.go +++ b/internal/pty/errors.go @@ -1,14 +1,12 @@ package pty import ( - "fmt" - "github.com/nexus-code/nexus-code/internal/proto" ) // requestFailed formats a PTY-domain failure into the standard agent error code. func requestFailed(format string, args ...any) error { - return proto.CodedError{Code: proto.CodeRequestFailed, Msg: fmt.Sprintf(format, args...)} + return proto.RequestFailed(format, args...) } // protocolError marks malformed PTY RPC parameters as envelope-level failures. diff --git a/internal/pty/hangup_test.go b/internal/pty/hangup_test.go index d970241f6..09ff4930e 100644 --- a/internal/pty/hangup_test.go +++ b/internal/pty/hangup_test.go @@ -13,6 +13,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "syscall" "testing" "time" @@ -220,60 +221,90 @@ while :; do sleep 1; done requireGone(t, job, 2*time.Second, "the background job") } -// zombieChildren lists this process's children that ended and were never -// waited for. -func zombieChildren(t *testing.T) []string { - t.Helper() - out, err := exec.Command("ps", "-A", "-o", "pid=,ppid=,stat=").Output() - if err != nil { - t.Fatalf("ps: %v", err) - } - self := strconv.Itoa(os.Getpid()) - var zombies []string - for _, line := range strings.Split(string(out), "\n") { - fields := strings.Fields(line) - if len(fields) == 3 && fields[1] == self && strings.HasPrefix(fields[2], "Z") { - zombies = append(zombies, fields[0]) - } - } - return zombies +// spawnWaitContext signals once the second request has reached its wait. +type spawnWaitContext struct { + context.Context + waiting func() } -// Two spawns of one tab racing each other start two shells; the one that -// loses is killed, and must be waited for too or it stays a zombie of the -// daemon. Not parallel: other tests' shells are briefly zombies as they end. -func TestRacingSpawnsOfATabLeaveNoZombie(t *testing.T) { +// Done exposes the wait barrier and the context's original cancellation. +func (c spawnWaitContext) Done() <-chan struct{} { + c.waiting() + return c.Context.Done() +} + +// TestRacingSpawnsOfATabForkOnlyOnce checks that overlapping requests +// share the shell even before it has been published in the session map. +func TestRacingSpawnsOfATabForkOnlyOnce(t *testing.T) { service, _ := newTestService(t) - for i := 0; i < 5; i++ { - params := mustJSON(t, SpawnParams{ - WorkspaceID: testWorkspaceID, TabID: fmt.Sprintf("race-%d", i), Cwd: t.TempDir(), - Shell: "/bin/sh", Args: []string{"-c", "sleep 30"}, Cols: 80, Rows: 24, - }) - start := make(chan struct{}) - var wg sync.WaitGroup - for j := 0; j < 2; j++ { - wg.Add(1) - go func() { - defer wg.Done() - <-start - if _, err := service.Spawn(context.Background(), params); err != nil { - t.Errorf("spawn: %v", err) - } - }() + first, duplicate, release := make(chan struct{}), make(chan struct{}), make(chan struct{}) + unblock := sync.OnceFunc(func() { close(release) }) + var starts atomic.Int32 + service.startPTY = func(spec spawnSpec) (*terminal, *child, error) { + if starts.Add(1) != 1 { + close(duplicate) + return nil, nil, errors.New("a second shell was started") } - close(start) - wg.Wait() + close(first) + <-release + return startTerminal(spec) } - deadline := time.Now().Add(2 * time.Second) - for { - zombies := zombieChildren(t) - if len(zombies) == 0 { - return - } - if time.Now().After(deadline) { - t.Fatalf("shells never waited for: %v", zombies) - } - time.Sleep(50 * time.Millisecond) + params := SpawnParams{ + WorkspaceID: testWorkspaceID, TabID: "racing-spawns", Cwd: t.TempDir(), + Shell: "/bin/sh", Args: []string{"-c", "exec /bin/sleep 60"}, Cols: 80, Rows: 24, + } + type response struct { + value any + err error + } + var calls sync.WaitGroup + spawn := func(ctx context.Context, params SpawnParams) <-chan response { + raw := mustJSON(t, params) + done := make(chan response, 1) + calls.Add(1) + go func() { + defer calls.Done() + value, err := service.Spawn(ctx, raw) + done <- response{value, err} + }() + return done + } + defer func() { unblock(); calls.Wait() }() + + owner := spawn(context.Background(), params) + select { + case <-first: + case <-time.After(2 * time.Second): + t.Fatal("first request never attempted to start a shell") + } + waiting := make(chan struct{}) + params.Cols, params.Rows = 120, 40 + joined := spawn(spawnWaitContext{context.Background(), sync.OnceFunc(func() { close(waiting) })}, params) + select { + case <-waiting: + case <-duplicate: + t.Fatal("second request forked a shell for an already starting tab") + case <-time.After(2 * time.Second): + t.Fatal("second request neither started nor joined the first") + } + + unblock() + a, b := <-owner, <-joined + firstSpawn, firstOK := a.value.(SpawnResult) + attached, attachedOK := b.value.(SpawnResult) + if a.err != nil || b.err != nil || !firstOK || !attachedOK || starts.Load() != 1 || + firstSpawn.Attached || !attached.Attached || firstSpawn.PID != attached.PID { + t.Fatalf("spawns returned %+v and %+v with %d starts", a, b, starts.Load()) + } + session := service.lookup(tabKey{workspaceID: testWorkspaceID, tabID: params.TabID}) + if session == nil { + t.Fatal("the shared shell exited before the attach finished") + } + session.sizeMu.Lock() + cols, rows := session.lastCols, session.lastRows + session.sizeMu.Unlock() + if cols != params.Cols || rows != params.Rows { + t.Fatalf("joined terminal is %dx%d, want %dx%d", cols, rows, params.Cols, params.Rows) } } diff --git a/internal/pty/process_unix.go b/internal/pty/process_unix.go index 50f7c6899..d0817a040 100644 --- a/internal/pty/process_unix.go +++ b/internal/pty/process_unix.go @@ -3,10 +3,11 @@ package pty import ( - "errors" "os" "syscall" "time" + + hostproc "github.com/nexus-code/nexus-code/internal/process" ) // hangupGrace is how long a closed tab's shell has to end on the hangup @@ -43,7 +44,7 @@ func (s *session) stopChild() (finish func()) { // killSession kills the shell and every process left in its session, which // the shell leads. Processes started meanwhile are caught by another pass. func (c *child) killSession() { - _ = c.signalGroup(syscall.SIGKILL) + _ = hostproc.SignalGroup(c.Pid, syscall.SIGKILL) for pass := 0; pass < 3; pass++ { left := sessionProcesses(c.Pid) if len(left) == 0 { @@ -55,15 +56,6 @@ func (c *child) killSession() { } } -// signalGroup sends signal to the child process group rooted at the shell. -func (c *child) signalGroup(signal syscall.Signal) error { - err := syscall.Kill(-c.Pid, signal) - if errors.Is(err, syscall.ESRCH) { - return nil - } - return err -} - // exitPayload translates the shell's exit into the PTY exit wire shape. func exitPayload(key tabKey, state *os.ProcessState, err error) ExitPayload { if err != nil { diff --git a/internal/pty/replay_cancel_test.go b/internal/pty/replay_cancel_test.go new file mode 100644 index 000000000..11e524b9c --- /dev/null +++ b/internal/pty/replay_cancel_test.go @@ -0,0 +1,144 @@ +package pty + +import ( + "bytes" + "context" + "encoding/base64" + "encoding/json" + "errors" + "sync" + "testing" + "time" +) + +// TestReplayCreditAcrossDialers holds ACKs at the high watermark and verifies +// that cancellation and sink replacement each end the old replay. +func TestReplayCreditAcrossDialers(t *testing.T) { + for _, tc := range []struct { + name string + cancelFirst bool + }{{"disconnect before replacement", true}, {"replace before cancellation", false}} { + t.Run(tc.name, func(t *testing.T) { + svc := New() + ctxA, cancelA := context.WithCancel(context.Background()) + defer cancelA() + ctxB, cancelB := context.WithCancel(context.Background()) + defer cancelB() + key := tabKey{workspaceID: "ws", tabID: "tab"} + sess := newSession(svc, key, nil, nil) + svc.storeSession(key, sess) + history := bytes.Repeat([]byte("a"), HighWatermarkBytes+2*MaxChunkSize) + sess.ringAppend(history) + t.Cleanup(func() { + svc.mu.Lock() + sess.stopped = true + sess.flowCond.Broadcast() + svc.mu.Unlock() + }) + + var gotA, gotB replayCapture + gotA.high = make(chan struct{}) + gotB.high = make(chan struct{}) + svc.SetEventSink(ctxA, gotA.emit) + params := json.RawMessage(`{"workspaceId":"ws","tabId":"tab"}`) + ack := json.RawMessage(`{"workspaceId":"ws","tabId":"tab","bytesConsumed":200000}`) + startReplay := func(ctx context.Context) <-chan error { + done := make(chan error, 1) + go func() { + _, err := svc.Replay(ctx, params) + done <- err + }() + return done + } + oldReplay := startReplay(ctxA) + awaitReplayStep(t, gotA.high) + attachB := func() { + svc.SetEventSink(ctxB, gotB.emit) + } + if tc.cancelFirst { + cancelA() + } else { + attachB() + } + if err := awaitReplayStep(t, oldReplay); !errors.Is(err, context.Canceled) { + t.Fatalf("old replay returned %v, want context cancellation", err) + } + if tc.cancelFirst { + attachB() + } + + newReplay := startReplay(ctxB) + awaitReplayStep(t, gotB.high) + // In the takeover case A's request context is still active. A late + // ACK from it must leave B paused until B sends its own ACK. + _, _ = svc.Ack(ctxA, ack) + svc.mu.Lock() + paused, debt := sess.paused, sess.outstanding + svc.mu.Unlock() + if !paused || debt < HighWatermarkBytes { + t.Fatalf("old ACK changed new dialer's window: paused=%t debt=%d", paused, debt) + } + + liveDone := make(chan struct{}) + go func() { + sess.deliver([]byte("live")) + close(liveDone) + }() + _, _ = svc.Ack(ctxB, ack) + if err := awaitReplayStep(t, newReplay); err != nil { + t.Fatalf("new replay failed: %v", err) + } + awaitReplayStep(t, liveDone) + gotB.mu.Lock() + defer gotB.mu.Unlock() + want := terminalReset + string(history) + if gotB.replay.String() != want || gotB.live.String() != "live" || gotB.replayAfterLive { + t.Fatalf("new dialer output: replay bytes=%d want=%d, live=%q, replay after live=%t", gotB.replay.Len(), len(want), gotB.live.String(), gotB.replayAfterLive) + } + }) + } +} + +// replayCapture signals when replay output fills a renderer's credit window. +type replayCapture struct { + mu sync.Mutex + replay bytes.Buffer + live bytes.Buffer + replayAfterLive bool + high chan struct{} +} + +// emit records output in arrival order and signals the watermark once. +func (c *replayCapture) emit(_ string, payload any) error { + data := payload.(DataPayload) + chunk, err := base64.StdEncoding.DecodeString(data.Chunk) + if err != nil { + return err + } + c.mu.Lock() + defer c.mu.Unlock() + if data.Replay { + c.replayAfterLive = c.replayAfterLive || c.live.Len() > 0 + wasBelow := c.replay.Len() < HighWatermarkBytes + c.replay.Write(chunk) + if wasBelow && c.replay.Len() >= HighWatermarkBytes { + close(c.high) + } + } else { + c.live.Write(chunk) + } + return nil +} + +// awaitReplayStep waits for a test barrier with a deadline for regressions. +func awaitReplayStep[T any](t *testing.T, step <-chan T) T { + t.Helper() + select { + case value := <-step: + return value + case <-time.After(2 * time.Second): + t.Fatal("timed out waiting for replay or output") + var zero T + return zero + } +} diff --git a/internal/pty/ring_test.go b/internal/pty/ring_test.go index d54476b57..8ab5adeca 100644 --- a/internal/pty/ring_test.go +++ b/internal/pty/ring_test.go @@ -37,7 +37,6 @@ func newBareSession(ringCap int) *session { readDone: make(chan struct{}), createdAt: time.Now(), } - s.flowCond = sync.NewCond(&s.flowMu) return s } @@ -205,7 +204,7 @@ func TestAccumulateReplayContentMatch(t *testing.T) { t.Parallel() rec := &sinkRecorder{} svc := New() - svc.SetEventSink(rec.sink) + svc.SetEventSink(context.Background(), rec.sink) t.Cleanup(svc.Close) tabID := "replay-match" @@ -246,7 +245,7 @@ func TestReplayRedrawsDeliveredOutput(t *testing.T) { var mu sync.Mutex var live, replayed []byte svc := New() - svc.SetEventSink(func(event string, payload any) error { + svc.SetEventSink(context.Background(), func(event string, payload any) error { dp, ok := payload.(DataPayload) if event != EventData || !ok { return nil @@ -308,7 +307,7 @@ func TestSpawnAttachesToRunningSession(t *testing.T) { t.Parallel() rec := &sinkRecorder{} svc := New() - svc.SetEventSink(rec.sink) + svc.SetEventSink(context.Background(), rec.sink) t.Cleanup(svc.Close) tabID := "spawn-attach" @@ -353,7 +352,7 @@ func TestReplayOrderingNoReversal(t *testing.T) { // then succeed. failRemaining := 1 // fail the first emit call svc := New() - svc.SetEventSink(func(event string, payload any) error { + svc.SetEventSink(context.Background(), func(event string, payload any) error { if event != EventData { return nil } @@ -435,7 +434,7 @@ func TestRingReleasedOnSessionExit(t *testing.T) { svc := New() var mu sync.Mutex var exits []ExitPayload - svc.SetEventSink(func(event string, payload any) error { + svc.SetEventSink(context.Background(), func(event string, payload any) error { if event == EventExit { if ep, ok := payload.(ExitPayload); ok { mu.Lock() @@ -496,21 +495,20 @@ exit 0 } } -// TestResetFlowControlUnblocksZombieDeadlock verifies the fix for the zombie +// TestSetEventSinkClearsPreviousDialerDebt verifies the fix for the zombie // window deadlock scenario: // -// 1. noteEmitted accumulates debt past HighWatermarkBytes without matching acks +// 1. The sink accumulates debt past HighWatermarkBytes without matching acks // (simulating a zombie dialer whose OS socket buffer absorbs writes). // 2. readLoop blocks in waitForOutputWindow — paused=true. -// 3. ResetFlowControl is called (simulating a new dialer wiring its sink). +// 3. A new dialer installs its sink. // 4. pty.replay completes successfully and live output resumes. // -// Without the fix, step 4 would block forever because emitBytes calls -// waitForOutputWindow which re-enters the same paused condition. -func TestResetFlowControlUnblocksZombieDeadlock(t *testing.T) { +// The replay would otherwise block waiting on credit from the previous dialer. +func TestSetEventSinkClearsPreviousDialerDebt(t *testing.T) { t.Parallel() // Phase 1: spawn a PTY that produces continuous output, then drive - // noteEmitted past HighWatermarkBytes without any acks. + // output debt past HighWatermarkBytes without any acks. svc := New() // Sink that succeeds but never triggers acks — models the OS buffer @@ -518,7 +516,7 @@ func TestResetFlowControlUnblocksZombieDeadlock(t *testing.T) { var mu sync.Mutex var afterReset []string zombieActive := true - svc.SetEventSink(func(event string, payload any) error { + sink := func(event string, payload any) error { if event != EventData { return nil } @@ -535,7 +533,8 @@ func TestResetFlowControlUnblocksZombieDeadlock(t *testing.T) { } } return nil - }) + } + svc.SetEventSink(context.Background(), sink) t.Cleanup(svc.Close) tabID := "zombie-deadlock" @@ -562,7 +561,7 @@ sleep 10 sessionFound: // Directly manipulate the session's flow state to simulate the zombie window: - // set outstanding to HighWatermarkBytes and paused=true, as if noteEmitted + // set outstanding to HighWatermarkBytes and paused=true, as if the sink // accumulated that debt without any acks arriving. key := tabKey{workspaceID: testWorkspaceID, tabID: tabID} svc.mu.Lock() @@ -572,27 +571,27 @@ sessionFound: t.Fatal("session not in map") } - sess.flowMu.Lock() + svc.mu.Lock() sess.outstanding = HighWatermarkBytes sess.paused = true - sess.flowMu.Unlock() + svc.mu.Unlock() - // Phase 2: simulate new dialer wiring — ResetFlowControl must unblock. + // Phase 2: simulate new dialer wiring — SetEventSink must unblock. mu.Lock() zombieActive = false mu.Unlock() - svc.ResetFlowControl() + svc.SetEventSink(context.Background(), sink) // Phase 3: verify the flow gate is now open. - sess.flowMu.Lock() + svc.mu.Lock() stillPaused := sess.paused stillOutstanding := sess.outstanding - sess.flowMu.Unlock() + svc.mu.Unlock() if stillPaused { - t.Fatal("paused still true after ResetFlowControl — readLoop would deadlock") + t.Fatal("paused still true after SetEventSink — readLoop would deadlock") } if stillOutstanding != 0 { - t.Fatalf("outstanding = %d after ResetFlowControl, want 0", stillOutstanding) + t.Fatalf("outstanding = %d after SetEventSink, want 0", stillOutstanding) } // Phase 4: replay must complete without blocking (would time out if paused). @@ -611,7 +610,7 @@ sessionFound: t.Fatalf("replay error: %v", err) } case <-time.After(3 * time.Second): - t.Fatal("replay blocked — zombie deadlock not fixed (ResetFlowControl had no effect)") + t.Fatal("replay blocked — zombie deadlock not fixed (SetEventSink had no effect)") } } @@ -634,7 +633,7 @@ func TestReplayWigglesSIGWINCHAfterFlush(t *testing.T) { t.Parallel() rec := &sinkRecorder{} svc := New() - svc.SetEventSink(rec.sink) + svc.SetEventSink(context.Background(), rec.sink) t.Cleanup(svc.Close) tabID := "wiggle-sigwinch" diff --git a/internal/pty/service.go b/internal/pty/service.go index e6d893e3a..cb325aeb2 100644 --- a/internal/pty/service.go +++ b/internal/pty/service.go @@ -8,13 +8,14 @@ import ( "errors" "io" "os" + "runtime" "sort" "strings" "sync" "sync/atomic" - "syscall" "time" + "github.com/nexus-code/nexus-code/internal/agentpaths" "github.com/nexus-code/nexus-code/internal/dispatch" "github.com/nexus-code/nexus-code/internal/ports" ) @@ -22,13 +23,32 @@ import ( // Service owns the registry of active PTY-backed terminal sessions. type Service struct { mu sync.Mutex - sink EventSink + output *outputSink sessions map[tabKey]*session + startPTY func(spawnSpec) (*terminal, *child, error) + spawning map[tabKey]*pendingSpawn + starts sync.WaitGroup + closed bool // closedTabs are the tabs a kill came for while they had no session, // and when: see Kill. closedTabs map[tabKey]time.Time } +// pendingSpawn publishes one shell startup result to concurrent callers. +type pendingSpawn struct { + done chan struct{} + session *session + err error +} + +// outputSink identifies a connection and captures its event destination. +// The pointer distinguishes successive sink installations, and the host's +// Done channel identifies the connection that dispatched a replay or ACK. +type outputSink struct { + ctx context.Context + emit EventSink +} + // closedTabMemory is how long a kill that found no session is kept for the // spawn it may have overtaken. const closedTabMemory = 10 * time.Second @@ -69,7 +89,8 @@ type session struct { writeMu sync.Mutex - flowMu sync.Mutex + // The credit window is guarded by service.mu so it changes atomically + // with the active output sink. flowCond *sync.Cond outstanding int paused bool @@ -140,7 +161,13 @@ const wiggleRestoreDelay = 200 * time.Millisecond // New constructs an empty PTY service registry. func New() *Service { - return &Service{sessions: make(map[tabKey]*session), closedTabs: make(map[tabKey]time.Time)} + return &Service{ + output: &outputSink{ctx: context.Background()}, + sessions: make(map[tabKey]*session), + closedTabs: make(map[tabKey]time.Time), + startPTY: startTerminal, + spawning: make(map[tabKey]*pendingSpawn), + } } // Register binds every pty.* and session.* method onto the dispatcher. @@ -156,46 +183,28 @@ func Register(d *dispatch.Dispatcher, service *Service) { d.Register("session.list", service.SessionList) } -// SetEventSink wires the service to the stdio host after both are constructed. -func (s *Service) SetEventSink(sink EventSink) { +// SetEventSink binds output to the context the host passes to its handlers. +// Outstanding renderer debt belongs to the previous connection, so replacing +// the sink also clears that debt and wakes any paused replay or reader. +func (s *Service) SetEventSink(ctx context.Context, sink EventSink) { s.mu.Lock() defer s.mu.Unlock() - s.sink = sink -} - -// ResetFlowControl zeroes outstanding renderer debt and unblocks every paused -// readLoop across all live sessions. -// -// Call this immediately after wiring a new dialer's event sink. During the -// zombie window between a silent disconnect and the new dialer's takeover, the -// old dialer's OS socket buffer may have absorbed writes that the renderer -// never received — so noteEmitted debt accumulates without matching acks. If -// that debt crossed HighWatermarkBytes the readLoop is already blocked in -// waitForOutputWindow. The new renderer has no knowledge of the old debt and -// will never send acks for it, causing permanent deadlock ("reconnected but -// terminal frozen"). -// -// Resetting here is correct because the stale debt belongs to the old -// renderer generation. In-flight bytes that landed only in the OS buffer (not -// in the renderer) are already lost — the plan explicitly accepts this as -// in-flight loss. Carrying forward a fictitious debt would only harm the new -// renderer. -func (s *Service) ResetFlowControl() { - s.mu.Lock() - sessions := make([]*session, 0, len(s.sessions)) + s.output = &outputSink{ctx: ctx, emit: sink} for _, sess := range s.sessions { - sessions = append(sessions, sess) - } - s.mu.Unlock() - - for _, sess := range sessions { - sess.resetFlow() + sess.outstanding = 0 + sess.paused = false + sess.flowCond.Broadcast() } } // Close ends every PTY session, as closing each tab does, and returns once // their shells are gone: hung up, or killed hangupGrace later. func (s *Service) Close() { + s.mu.Lock() + s.closed = true + s.mu.Unlock() + s.starts.Wait() + s.mu.Lock() sessions := make([]*session, 0, len(s.sessions)) for key, session := range s.sessions { @@ -214,7 +223,7 @@ func (s *Service) Close() { } // Spawn starts one shell process under a PTY and begins forwarding output events. -func (s *Service) Spawn(_ context.Context, raw json.RawMessage) (any, error) { +func (s *Service) Spawn(ctx context.Context, raw json.RawMessage) (any, error) { var p SpawnParams if err := decodeParams(raw, &p, "pty.spawn params must include workspaceId, tabId, cwd, cols, and rows"); err != nil { return nil, err @@ -230,19 +239,51 @@ func (s *Service) Spawn(_ context.Context, raw json.RawMessage) (any, error) { return nil, err } - if existing := s.lookup(key); existing != nil { - // The session outlived its previous client (an app restart or a window - // reload). Fit it to the new client, which replays it to redraw the - // screen. Best-effort: a session that is exiting reports exit anyway. - _ = existing.resize(p.Cols, p.Rows) - return SpawnResult{PID: existing.child.Pid, Attached: true}, nil + s.mu.Lock() + if s.closed { + s.mu.Unlock() + return nil, requestFailed("PTY service is closed") + } + if starting := s.spawning[key]; starting != nil { + s.mu.Unlock() + select { + case <-starting.done: + if starting.err != nil { + return nil, starting.err + } + return starting.session.attach(p.Cols, p.Rows), nil + case <-ctx.Done(): + return nil, ctx.Err() + } + } + if existing := s.sessions[key]; existing != nil { + s.mu.Unlock() + return existing.attach(p.Cols, p.Rows), nil + } + attempt := &pendingSpawn{done: make(chan struct{})} + s.spawning[key] = attempt + s.starts.Add(1) + s.mu.Unlock() + defer s.starts.Done() + + attempt.session, attempt.err = s.spawnSession(key, p) + s.mu.Lock() + delete(s.spawning, key) + close(attempt.done) + s.mu.Unlock() + if attempt.err != nil { + return nil, attempt.err } + return SpawnResult{PID: attempt.session.child.Pid}, nil +} +// spawnSession starts and registers a shell for a reserved tab. +func (s *Service) spawnSession(key tabKey, p SpawnParams) (*session, error) { shell := strings.TrimSpace(p.Shell) if shell == "" { shell = defaultShell() } - master, child, err := startTerminal(spawnSpec{ + master, child, err := s.startPTY(spawnSpec{ shell: shell, args: p.Args, dir: p.Cwd, @@ -260,12 +301,7 @@ func (s *Service) Spawn(_ context.Context, raw json.RawMessage) (any, error) { session.lastRows = p.Rows session.sizeMu.Unlock() - stored, closed := s.storeSession(key, session) - if stored != session { - // Another spawn of the tab, racing this one, got there first. - session.discard() - return SpawnResult{PID: stored.child.Pid}, nil - } + closed := s.storeSession(key, session) go session.readLoop() go session.waitLoop() @@ -273,7 +309,14 @@ func (s *Service) Spawn(_ context.Context, raw json.RawMessage) (any, error) { // The tab's kill overtook this spawn: end the shell as it would have. session.stop() } - return SpawnResult{PID: child.Pid}, nil + return session, nil +} + +// attach fits a running shell to its caller, which replays the session to +// redraw the screen. A shell exiting during the resize reports its exit. +func (s *session) attach(cols, rows int) SpawnResult { + _ = s.resize(cols, rows) + return SpawnResult{PID: s.child.Pid, Attached: true} } // Write sends user input bytes to the child PTY. @@ -320,7 +363,7 @@ func (s *Service) Resize(_ context.Context, raw json.RawMessage) (any, error) { } // Ack reduces one session's renderer debt and resumes output once the low watermark is reached. -func (s *Service) Ack(_ context.Context, raw json.RawMessage) (any, error) { +func (s *Service) Ack(ctx context.Context, raw json.RawMessage) (any, error) { var p AckParams if err := decodeParams(raw, &p, "pty.ack params must include workspaceId, tabId, and bytesConsumed"); err != nil { return nil, err @@ -333,11 +376,11 @@ func (s *Service) Ack(_ context.Context, raw json.RawMessage) (any, error) { if bytesConsumed < 0 { return nil, protocolError("pty.ack bytesConsumed must be non-negative") } - session := s.lookup(key) - if session == nil { - return struct{}{}, nil + s.mu.Lock() + defer s.mu.Unlock() + if sess := s.sessions[key]; sess != nil && ctx.Done() == s.output.ctx.Done() && ctx.Err() == nil { + sess.ackLocked(bytesConsumed) } - session.ack(bytesConsumed) return struct{}{}, nil } @@ -468,7 +511,7 @@ func newSession(service *Service, key tabKey, child *child, master *terminal) *s ring: make([]byte, RingCapBytes), createdAt: time.Now(), } - s.flowCond = sync.NewCond(&s.flowMu) + s.flowCond = sync.NewCond(&service.mu) return s } @@ -478,7 +521,7 @@ func (s *session) readLoop() { defer close(s.readDone) buf := make([]byte, MaxChunkSize) for { - if !s.waitForOutputWindow() { + if err := s.waitForOutputWindow(context.Background(), nil); err != nil { return } n, err := s.master.Read(buf) @@ -499,13 +542,7 @@ func (s *session) deliver(chunk []byte) { s.outputMu.Lock() defer s.outputMu.Unlock() s.ringAppend(chunk) - s.noteEmitted(len(chunk)) - if err := s.service.emit(EventData, DataPayload{WorkspaceID: s.key.workspaceID, TabID: s.key.tabID, Chunk: base64.StdEncoding.EncodeToString(chunk)}); err != nil { - // Undo the renderer debt — there is no renderer to send acks, so leaving - // the debt would permanently pause readLoop on the next - // waitForOutputWindow check once a dialer reconnects. - s.ack(len(chunk)) - } + _ = s.emitData(chunk, nil) } // resize applies a PTY geometry and records it for wiggleSIGWINCH. @@ -536,30 +573,31 @@ func (s *session) waitLoop() { }) } -// waitForOutputWindow blocks while a prior emission has crossed the high watermark. -func (s *session) waitForOutputWindow() bool { - s.flowMu.Lock() - defer s.flowMu.Unlock() - for s.paused && !s.stopped { +// waitForOutputWindow waits for credit or the end of the session or replay. +// A nil replay lets the live reader use the current connection's window. +func (s *session) waitForOutputWindow(ctx context.Context, replay *outputSink) error { + s.service.mu.Lock() + defer s.service.mu.Unlock() + for { + if err := ctx.Err(); err != nil { + return err + } + if replay != nil && replay != s.service.output { + return context.Canceled + } + if s.stopped { + return errors.New("session stopped") + } + if !s.paused { + return nil + } s.flowCond.Wait() } - return !s.stopped } -// noteEmitted adds raw bytes to renderer debt and toggles the paused state at HIGH. -func (s *session) noteEmitted(rawBytes int) { - s.flowMu.Lock() - defer s.flowMu.Unlock() - s.outstanding += rawBytes - if s.outstanding >= HighWatermarkBytes { - s.paused = true - } -} - -// ack applies renderer credit and wakes the reader only after LOW is reached. -func (s *session) ack(rawBytes int) { - s.flowMu.Lock() - defer s.flowMu.Unlock() +// ackLocked applies renderer credit and wakes waiters when LOW is reached. +// The caller holds service.mu and has checked the sink generation. +func (s *session) ackLocked(rawBytes int) { if rawBytes >= s.outstanding { s.outstanding = 0 } else { @@ -571,19 +609,6 @@ func (s *session) ack(rawBytes int) { } } -// resetFlow clears outstanding renderer debt and wakes any paused readLoop. -// Called by Service.ResetFlowControl on dialer generation change — the old -// renderer's debt must not carry over to the new one. -func (s *session) resetFlow() { - s.flowMu.Lock() - defer s.flowMu.Unlock() - s.outstanding = 0 - if s.paused { - s.paused = false - s.flowCond.Broadcast() - } -} - // wiggleSIGWINCH forces a full TUI repaint after replay by shrinking the PTY // by one row, holding that geometry for wiggleRestoreDelay, then restoring // the original size. The hold is what makes the shrink SIGWINCH observable @@ -647,7 +672,7 @@ func (s *session) wiggleSIGWINCH() { _ = setSize(cols, rows) } -// write serializes and chunks input writes to preserve one-session input order. +// write keeps each input frame whole even when multiple RPCs write at once. func (s *session) write(data []byte) error { s.writeMu.Lock() defer s.writeMu.Unlock() @@ -677,25 +702,13 @@ func (s *session) stop() <-chan struct{} { return s.stopDone } -// discard ends the shell of a session no tab took, as another spawn of the -// tab won: it never ran anything of the user's, so it is killed outright. -// Nothing else waits for it, so this does, or it stays a zombie. -func (s *session) discard() { - s.stopOnce.Do(func() { - _ = s.child.signalGroup(syscall.SIGKILL) - s.stopReading() - close(s.stopDone) - }) - go func() { _, _ = s.child.wait() }() -} - // stopReading wakes any paused reader and closes the PTY master. func (s *session) stopReading() { - s.flowMu.Lock() + s.service.mu.Lock() s.stopped = true s.paused = false s.flowCond.Broadcast() - s.flowMu.Unlock() + s.service.mu.Unlock() _ = s.master.Close() } @@ -790,11 +803,16 @@ func mergeEnv(overrides map[string]string) []string { delete(merged, existing) } } - merged[key] = value + merged[key] = agentpaths.ProfileEnvDir(key, value) } // The app's wrapper directory goes first on this host's own PATH. The app // cannot build that PATH: for an SSH workspace it is the remote's. - if dir := overrides[wrapperDirEnv]; dir != "" && !hasEnvKey(overrides, "PATH") { + if !hasEnvKey(overrides, "PATH") { + if dir := overrides[wrapperDirEnv]; dir != "" { + prependPath(merged, dir) + } + } + if dir := overrides["NEXUS_CMD_CODEX_DIR"]; runtime.GOOS == "windows" && dir != "" { prependPath(merged, dir) } if _, ok := merged["TERM"]; !ok { @@ -846,20 +864,16 @@ func writeFull(writer io.Writer, data []byte) error { return nil } -// storeSession inserts a session unless another goroutine already claimed -// the key, and reports whether a kill for the tab came first (see Kill). -func (s *Service) storeSession(key tabKey, session *session) (stored *session, closed bool) { +// storeSession publishes the tab's reserved shell and reports an early kill. +func (s *Service) storeSession(key tabKey, session *session) (closed bool) { s.mu.Lock() defer s.mu.Unlock() - if existing := s.sessions[key]; existing != nil { - return existing, false - } s.sessions[key] = session if at, ok := s.closedTabs[key]; ok { delete(s.closedTabs, key) closed = time.Since(at) <= closedTabMemory } - return session, closed + return closed } // lookup returns the active session for key, if any. @@ -972,7 +986,7 @@ func (s *Service) SessionList(_ context.Context, raw json.RawMessage) (any, erro // Ordering guarantee: Replay holds outputMu for the whole send, so live PTY // output read meanwhile waits in readLoop and follows the replayed output on // the wire with no interleaving. -func (s *Service) Replay(_ context.Context, raw json.RawMessage) (any, error) { +func (s *Service) Replay(ctx context.Context, raw json.RawMessage) (any, error) { var p ReplayParams if err := decodeParams(raw, &p, "pty.replay params must include workspaceId and tabId"); err != nil { return nil, err @@ -981,16 +995,21 @@ func (s *Service) Replay(_ context.Context, raw json.RawMessage) (any, error) { if err != nil { return nil, err } - sess := s.lookup(key) + s.mu.Lock() + sess, output := s.sessions[key], s.output + s.mu.Unlock() if sess == nil { return struct{}{}, nil } + if ctx.Done() != output.ctx.Done() { + return nil, context.Canceled + } sess.outputMu.Lock() sess.ringMu.Lock() replay := append([]byte(terminalReset), sess.ringContentsLocked()...) sess.ringMu.Unlock() - err = s.emitReplay(sess, replay) + err = sess.emitReplay(ctx, output, replay) sess.outputMu.Unlock() if err != nil { // Dialer disappeared again mid-replay; the ring still holds everything @@ -1011,38 +1030,72 @@ func (s *Service) Replay(_ context.Context, raw json.RawMessage) (any, error) { return struct{}{}, nil } -// emitReplay sends replayed bytes in MaxChunkSize chunks through the -// ack-credit gate, exactly as readLoop does for live output. Stops and -// returns the first emit error. -func (s *Service) emitReplay(sess *session, data []byte) error { +// emitReplay sends chunks through the credit window until it has flushed the +// retained output, is canceled, or encounters a sink error. +func (s *session) emitReplay(ctx context.Context, output *outputSink, data []byte) error { + stopWake := context.AfterFunc(ctx, func() { + // Take the waiter's lock so cancellation cannot signal before Wait. + s.service.mu.Lock() + defer s.service.mu.Unlock() + s.flowCond.Broadcast() + }) + defer stopWake() + for len(data) > 0 { - if !sess.waitForOutputWindow() { - return errors.New("session stopped") - } - n := len(data) - if n > MaxChunkSize { - n = MaxChunkSize + if err := s.waitForOutputWindow(ctx, output); err != nil { + return err } - chunk := data[:n] - data = data[n:] - sess.noteEmitted(len(chunk)) - if err := s.emit(EventData, DataPayload{ - WorkspaceID: sess.key.workspaceID, - TabID: sess.key.tabID, - Chunk: base64.StdEncoding.EncodeToString(chunk), - Replay: true, - }); err != nil { - sess.ack(len(chunk)) // undo debt on failure + n := min(len(data), MaxChunkSize) + if err := s.emitData(data[:n], output); err != nil { return err } + data = data[n:] } return nil } +// emitData charges the selected connection before writing to its captured +// sink. A nil replay uses the active sink for live output. The write can +// block, so it runs outside service.mu and rolls back debt only for its own +// generation. +func (s *session) emitData(chunk []byte, replay *outputSink) error { + svc := s.service + svc.mu.Lock() + output := svc.output + if replay != nil && replay != output { + svc.mu.Unlock() + return context.Canceled + } + if err := output.ctx.Err(); err != nil || output.emit == nil { + svc.mu.Unlock() + return err + } + s.outstanding += len(chunk) + if s.outstanding >= HighWatermarkBytes { + s.paused = true + } + svc.mu.Unlock() + + err := output.emit(EventData, DataPayload{ + WorkspaceID: s.key.workspaceID, + TabID: s.key.tabID, + Chunk: base64.StdEncoding.EncodeToString(chunk), + Replay: replay != nil, + }) + if err != nil { + svc.mu.Lock() + if svc.output == output { + s.ackLocked(len(chunk)) + } + svc.mu.Unlock() + } + return err +} + // emit writes one event through the configured sink, if any. func (s *Service) emit(event string, payload any) error { s.mu.Lock() - sink := s.sink + sink := s.output.emit s.mu.Unlock() if sink == nil { return nil diff --git a/internal/pty/service_test.go b/internal/pty/service_test.go index 369b3a328..80ad783a2 100644 --- a/internal/pty/service_test.go +++ b/internal/pty/service_test.go @@ -19,33 +19,40 @@ import ( const testWorkspaceID = "00000000-0000-0000-0000-000000000001" -// TestBurstWriteFrameOrderingPreserved verifies sequential write RPCs return in order. -func TestBurstWriteFrameOrderingPreserved(t *testing.T) { +// TestConcurrentWritesKeepFramesWhole verifies that simultaneous RPCs do +// not splice their input together when each write spans several PTY chunks. +func TestConcurrentWritesKeepFramesWhole(t *testing.T) { t.Parallel() service, recorder := newTestService(t) tabID := "burst-order" spawnTestScript(t, service, tabID, ` -stty -echo -echo READY -cat +stty raw -echo +printf READY +exec cat `) recorder.requireTranscriptContains(t, "READY", 2*time.Second) - const frames = 80 - for i := range frames { - writeSession(t, service, tabID, fmt.Sprintf("FRAME%03d\n", i)) + a := strings.Repeat("A", 32*maxWriteChunkSize) + b := strings.Repeat("B", 32*maxWriteChunkSize) + start := make(chan struct{}) + done := make(chan error, 2) + for _, frame := range []string{a, b} { + params := mustJSON(t, WriteParams{WorkspaceID: testWorkspaceID, TabID: tabID, Data: frame}) + go func() { + <-start + _, err := service.Write(context.Background(), params) + done <- err + }() } - recorder.requireTranscriptContains(t, "FRAME079", 2*time.Second) - - transcript := recorder.transcript() - cursor := 0 - for i := range frames { - marker := fmt.Sprintf("FRAME%03d", i) - idx := strings.Index(transcript[cursor:], marker) - if idx < 0 { - t.Fatalf("missing %s after byte %d in transcript:\n%s", marker, cursor, transcript) + close(start) + recorder.requireDataBytesAtLeast(t, len("READY")+len(a)+len(b), 2*time.Second) + for range 2 { + if err := <-done; err != nil { + t.Fatal(err) } - cursor += idx + len(marker) + } + if got := strings.TrimPrefix(recorder.transcript(), "READY"); got != a+b && got != b+a { + t.Fatalf("PTY input frames interleaved or changed: got %d bytes", len(got)) } } @@ -221,7 +228,7 @@ func newTestService(t *testing.T) (*Service, *eventRecorder) { t.Helper() recorder := &eventRecorder{} service := New() - service.SetEventSink(recorder.sink) + service.SetEventSink(context.Background(), recorder.sink) t.Cleanup(service.Close) return service, recorder } diff --git a/internal/pty/terminal_windows_test.go b/internal/pty/terminal_windows_test.go index a88807702..c0f5f0dd7 100644 --- a/internal/pty/terminal_windows_test.go +++ b/internal/pty/terminal_windows_test.go @@ -26,7 +26,7 @@ func newConsoleService(t *testing.T) (*Service, *consoleRecorder) { t.Helper() svc := New() rec := &consoleRecorder{} - svc.SetEventSink(func(event string, payload any) error { + svc.SetEventSink(context.Background(), func(event string, payload any) error { rec.mu.Lock() defer rec.mu.Unlock() switch p := payload.(type) { @@ -195,3 +195,17 @@ func TestMergeEnvOverridesPathWhateverItsCase(t *testing.T) { t.Fatalf("PATH entries = %q, want only the override", paths) } } + +// TestMergeEnvKeepsBothCmdLaunchers verifies codex.cmd takes precedence +// while nexus.exe stays on PATH and the directory names stay intact. +func TestMergeEnvKeepsBothCmdLaunchers(t *testing.T) { + t.Setenv("Path", `C:\Windows`) + env := mergeEnv(map[string]string{ + wrapperDirEnv: `C:\Nexus Work\cli`, + "NEXUS_CMD_CODEX_DIR": `C:\Nexus Work & Notes\cmd!`, + }) + want := `C:\Nexus Work & Notes\cmd!;C:\Nexus Work\cli;C:\Windows` + if got := pathValues(env); len(got) != 1 || got[0] != want { + t.Fatalf("PATH = %q, want [%q]", got, want) + } +} diff --git a/internal/redact/credentials.go b/internal/redact/credentials.go new file mode 100644 index 000000000..f166e879b --- /dev/null +++ b/internal/redact/credentials.go @@ -0,0 +1,25 @@ +// Package redact removes common credential forms from diagnostic text. +package redact + +import "regexp" + +var ( + userinfo = regexp.MustCompile(`(?i)([a-z][a-z0-9+.-]*://)[^/\s?#"'<>]+@`) + authorization = regexp.MustCompile(`(?i)(\bauthorization\b["']?\s*[:=]\s*["']?(?:(?:bearer|basic|token)\s+)?|\bbearer\s+)[^\s"';,]+`) + assignment = regexp.MustCompile(`(?i)(\b(?:token|[a-z0-9_]+_(?:key|token|secret))\b["']?\s*[:=]\s*)("(?:\\.|[^"\\])*"|'[^']*'|[^\s"'&;,<>]+)`) + // argv already has argument boundaries, so an unquoted assignment can + // contain spaces that would be ambiguous in printed diagnostics. + argumentAssignment = regexp.MustCompile(`(?i)(^|\s)([a-z0-9_]+_(?:key|token|secret)\s*=).+$`) +) + +// Credentials masks recognizable secrets while preserving diagnostic context. +func Credentials(s string) string { + s = userinfo.ReplaceAllString(s, "${1}[REDACTED]@") + s = authorization.ReplaceAllString(s, "${1}[REDACTED]") + return assignment.ReplaceAllString(s, "${1}[REDACTED]") +} + +// Argument also uses the delimiter at the end of a process argument. +func Argument(s string) string { + return Credentials(argumentAssignment.ReplaceAllString(s, "${1}${2}[REDACTED]")) +} diff --git a/internal/search/service.go b/internal/search/service.go index b44b1603f..62a616087 100644 --- a/internal/search/service.go +++ b/internal/search/service.go @@ -22,10 +22,13 @@ import ( const ( defaultMaxResults = 2000 defaultMaxFileSize = 5 * 1024 * 1024 + maxSearchFileSize = 16 * 1024 * 1024 batchCountTrigger = 50 batchMatchesTrigger = 200 batchTimeTrigger = 30 * time.Millisecond perFileMatchCap = 1000 + earlyCancelLifetime = 30 * time.Second + maxEarlyCancels = 1024 // previewMaxBytes bounds Match.Preview. Lines up to this length are sent // whole; longer lines (minified bundles, single-line JSON) are cut to a @@ -50,9 +53,10 @@ type EventSink func(event string, payload any) error type Service struct { root string - mu sync.Mutex - sink EventSink - cancels map[string]context.CancelFunc + mu sync.Mutex + sink EventSink + cancels map[string]context.CancelFunc + earlyCancels map[string]time.Time } type Params struct { @@ -129,7 +133,10 @@ func New(root string) (*Service, error) { if err != nil { return nil, err } - return &Service{root: filepath.Clean(abs), cancels: make(map[string]context.CancelFunc)}, nil + return &Service{ + root: filepath.Clean(abs), cancels: make(map[string]context.CancelFunc), + earlyCancels: make(map[string]time.Time), + }, nil } func (s *Service) SetEventSink(sink EventSink) { @@ -152,23 +159,22 @@ func (s *Service) Text(ctx context.Context, raw json.RawMessage) (any, error) { if p.SearchID == "" { return nil, proto.ProtocolError("search.text searchId is required") } + searchCtx, finish, err := s.registerSearch(ctx, p.SearchID) + if err != nil { + return nil, err + } + defer finish() + if p.Query.MaxFileSize > maxSearchFileSize { + return nil, proto.ProtocolError("search.text maxFileSize must be at most 16 MiB") + } normalizeQuery(&p.Query) - re, err := compilePattern(p.Query) if err != nil { return nil, err } - - searchCtx, cancel := context.WithCancel(ctx) - s.mu.Lock() - s.cancels[p.SearchID] = cancel - s.mu.Unlock() - defer func() { - cancel() - s.mu.Lock() - delete(s.cancels, p.SearchID) - s.mu.Unlock() - }() + if err := searchCtx.Err(); err != nil { + return nil, err + } start := time.Now() result, err := s.walk(searchCtx, p.SearchID, p.Query, re) @@ -179,20 +185,68 @@ func (s *Service) Text(ctx context.Context, raw json.RawMessage) (any, error) { return result, nil } +// registerSearch consumes a cancellation that overtook this request or +// publishes its cancel function before compilation or walking begins. +func (s *Service) registerSearch(ctx context.Context, id string) (context.Context, func(), error) { + s.mu.Lock() + defer s.mu.Unlock() + if expiry, ok := s.earlyCancels[id]; ok { + delete(s.earlyCancels, id) + if time.Now().Before(expiry) { + return nil, nil, context.Canceled + } + } + if err := ctx.Err(); err != nil { + return nil, nil, err + } + if s.cancels[id] != nil { + return nil, nil, proto.ProtocolError("search.text searchId is already running") + } + searchCtx, cancel := context.WithCancel(ctx) + s.cancels[id] = cancel + return searchCtx, func() { + s.mu.Lock() + defer s.mu.Unlock() + cancel() + delete(s.cancels, id) + }, nil +} + func (s *Service) Cancel(_ context.Context, raw json.RawMessage) (any, error) { var p CancelParams if len(raw) == 0 || json.Unmarshal(raw, &p) != nil || p.SearchID == "" { return nil, proto.ProtocolError("search.cancel params must include searchId") } s.mu.Lock() - cancel := s.cancels[p.SearchID] - s.mu.Unlock() - if cancel != nil { + defer s.mu.Unlock() + if cancel := s.cancels[p.SearchID]; cancel != nil { cancel() + } else { + s.rememberCancel(p.SearchID, time.Now()) } return struct{}{}, nil } +// rememberCancel keeps recent unmatched cancellations with a fixed capacity. +// The caller holds mu so lookup and registration see each cancel exactly once. +func (s *Service) rememberCancel(id string, now time.Time) { + var oldestID string + var oldest time.Time + for key, expiry := range s.earlyCancels { + if !now.Before(expiry) { + delete(s.earlyCancels, key) + continue + } + if oldest.IsZero() || expiry.Before(oldest) { + oldestID, oldest = key, expiry + } + } + if len(s.earlyCancels) >= maxEarlyCancels && s.earlyCancels[id].IsZero() { + delete(s.earlyCancels, oldestID) + } + s.earlyCancels[id] = now.Add(earlyCancelLifetime) +} + func normalizeQuery(q *Query) { if q.MaxResults <= 0 { q.MaxResults = defaultMaxResults @@ -277,7 +331,10 @@ func (s *Service) walk(ctx context.Context, searchID string, q Query, re *regexp if err != nil || info.Size() == 0 || info.Size() > q.MaxFileSize { return nil } - buf, err := readForSearch(abs, info.Size()) + buf, err := readForSearch(ctx, abs, info.Size()) + if ctxErr := ctx.Err(); ctxErr != nil { + return ctxErr + } if err != nil || buf == nil { return nil } @@ -345,7 +402,8 @@ func (s *Service) emitBatch(searchID string, state *batchState) { } } -func readForSearch(abs string, size int64) ([]byte, error) { +// readForSearch probes binary files before reading the bounded text content. +func readForSearch(ctx context.Context, abs string, size int64) ([]byte, error) { file, err := os.Open(abs) if err != nil { return nil, err @@ -357,7 +415,7 @@ func readForSearch(abs string, size int64) ([]byte, error) { probeLen = size } probe := make([]byte, probeLen) - n, err := io.ReadFull(file, probe) + n, err := io.ReadFull(searchReader{ctx, file}, probe) if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF { return nil, err } @@ -371,7 +429,20 @@ func readForSearch(abs string, size int64) ([]byte, error) { if _, err := file.Seek(0, io.SeekStart); err != nil { return nil, err } - return io.ReadAll(io.LimitReader(file, size)) + return io.ReadAll(io.LimitReader(searchReader{ctx, file}, size)) +} + +// searchReader checks for cancellation between bounded reads. +type searchReader struct { + ctx context.Context + source io.Reader +} + +func (r searchReader) Read(p []byte) (int, error) { + if err := r.ctx.Err(); err != nil { + return 0, err + } + return r.source.Read(p[:min(len(p), 32<<10)]) } func findMatches(buf []byte, re *regexp.Regexp, perFileCap int, remainingGlobal int) []Match { diff --git a/internal/search/service_test.go b/internal/search/service_test.go index 63f73496c..ed69fb128 100644 --- a/internal/search/service_test.go +++ b/internal/search/service_test.go @@ -3,6 +3,7 @@ package search import ( "context" "encoding/json" + "errors" "os" "path/filepath" "slices" @@ -12,6 +13,32 @@ import ( "unicode/utf8" ) +// TestCancelBeforeSearchRegistration verifies that a cancel dispatched first +// still stops its search before any progress reaches the client. +func TestCancelBeforeSearchRegistration(t *testing.T) { + root := t.TempDir() + must(t, os.WriteFile(filepath.Join(root, "match.txt"), []byte("needle\n"), 0o644)) + service := mustSearch(t, root) + service.SetEventSink(func(_ string, payload any) error { + if progress := payload.(ProgressPayload); progress.SearchID == "closed" { + t.Error("canceled search sent progress") + } + return nil + }) + ctx := context.Background() + _, err := service.Cancel(ctx, mustJSON(t, CancelParams{SearchID: "closed"})) + must(t, err) + _, err = service.Text(ctx, mustJSON(t, Params{SearchID: "closed", Query: Query{Pattern: "needle"}})) + if !errors.Is(err, context.Canceled) { + t.Fatalf("canceled search returned %v", err) + } + result, err := service.Text(ctx, mustJSON(t, Params{SearchID: "next", Query: Query{Pattern: "needle"}})) + must(t, err) + if result.(Complete).MatchesFound != 1 { + t.Fatalf("replacement search returned %+v", result) + } +} + func TestTextSearchBoundsPreviewOnLongLines(t *testing.T) { root := t.TempDir() // One 256 KiB line: before the preview window existed, every one of the @@ -131,3 +158,15 @@ func TestSplitLinesReusesCompiledPattern(t *testing.T) { t.Fatalf("splitLines allocated %v times per call; the pattern is being recompiled", allocs) } } + +// TestTextRejectsOversizedFileBudget checks the request fails before walking. +func TestTextRejectsOversizedFileBudget(t *testing.T) { + service := mustSearch(t, t.TempDir()) + _, err := service.Text(context.Background(), mustJSON(t, Params{ + SearchID: "oversized-budget", + Query: Query{Pattern: "needle", MaxFileSize: 16<<20 + 1}, + })) + if err == nil || !strings.Contains(err.Error(), "16 MiB") { + t.Fatalf("expected a file budget error, got %v", err) + } +} diff --git a/internal/stdioserver/host.go b/internal/stdioserver/host.go index 7f6929d58..a52c33a26 100644 --- a/internal/stdioserver/host.go +++ b/internal/stdioserver/host.go @@ -85,9 +85,9 @@ type Host struct { // Guarded by outMu. outClosed bool - // orderedTail is closed when the last ordered request so far has been - // handled; the next one waits on it. Only Run's goroutine touches it. - orderedTail chan struct{} + // Each queue's tail closes once its last request has been handled. + orderedMu sync.Mutex + orderedTail map[string]chan struct{} ctx context.Context cancel context.CancelFunc @@ -95,6 +95,10 @@ type Host struct { termOnce sync.Once // ensures drainAndExit fires at most once accepting bool // accept new requests until SIGTERM flips this acceptMu sync.Mutex + regular dispatchUsage + control dispatchUsage + rejected []string + rejecting bool // shutdownHooks are run in order by drainAndExit right before it exits. // os.Exit skips defers, so a resource that needs explicit cleanup, such @@ -132,29 +136,36 @@ type Host struct { func New(d *dispatch.Dispatcher, in io.Reader, out io.Writer, logger *slog.Logger) *Host { ctx, cancel := context.WithCancel(context.Background()) return &Host{ - dispatcher: d, - in: in, - out: out, - logger: logger, - ctx: ctx, - cancel: cancel, - accepting: true, - exit: os.Exit, - startMono: time.Now(), + dispatcher: d, + in: in, + out: out, + logger: logger, + ctx: ctx, + cancel: cancel, + accepting: true, + orderedTail: make(map[string]chan struct{}), + exit: os.Exit, + startMono: time.Now(), } } +// Context returns the connection context passed to request handlers. The +// host cancels it once the drain finishes or reaches its deadline. +func (h *Host) Context() context.Context { + return h.ctx +} + // stampInbound records "now" (monotonic, relative to startMono) as the last // time an inbound line arrived. Single encoding point for lastInbound so the // watchdog's idleElapsed reads the same units. -func (h *Host) stampInbound() { - h.lastInbound.Store(int64(time.Since(h.startMono))) +func (h *Host) stampInbound(now time.Time) { + h.lastInbound.Store(int64(now.Sub(h.startMono))) } // idleElapsed reports how long it has been since the last inbound line, using // the monotonic clock so it cannot be skewed by wall-clock adjustments. -func (h *Host) idleElapsed() time.Duration { - return time.Since(h.startMono) - time.Duration(h.lastInbound.Load()) +func (h *Host) idleElapsed(now time.Time) time.Duration { + return now.Sub(h.startMono) - time.Duration(h.lastInbound.Load()) } // WriteFrame serializes one frame as NDJSON onto `out`. Used by the @@ -166,7 +177,7 @@ func (h *Host) idleElapsed() time.Duration { // backpressure, the goroutine calling WriteFrame will block on the OS // pipe write. This is the designed transport-level backpressure signal — // a backed-up pipe propagates naturally into handler goroutine -// concurrency, which is bounded by the client request rate. SIGTERM +// concurrency, which is bounded by the connection's dispatch budget. SIGTERM // triggers a 75 ms force-exit ceiling so no stuck goroutine can prevent // orderly shutdown. func (h *Host) WriteFrame(frame any) error { @@ -244,37 +255,71 @@ func (h *Host) Run() { continue } // Any inbound line proves the client is alive — reset the idle watchdog. - h.stampInbound() - if !h.isAccepting() { + h.stampInbound(time.Now()) + if tooLong { + if h.isAccepting() { + h.rejectOversizedLine(line) + } continue } - if tooLong { - h.rejectOversizedLine(line) + req, parseErr := proto.ParseRequest(line) + size := len(line) + finishRequest, accepting := h.beginRequest(req.Method, size) + if !accepting { continue } - h.wg.Add(1) - if h.dispatcher.Ordered(methodAtStart(line)) { - prev := h.orderedTail - done := make(chan struct{}) - h.orderedTail = done - go func() { - defer h.wg.Done() - defer close(done) - if prev != nil { - <-prev - } - h.handleLine(line) - }() + if finishRequest == nil { + if !h.rejectBusy(requestID(line, req)) { + h.drainAndExit(TransportErrorExitCode) + return + } continue } + if parseErr == nil { + line = nil // The parsed envelope owns the data the handler needs. + } + var prev <-chan struct{} + var finish func() + if key, ordered := h.dispatcher.OrderKey(req); parseErr == nil && ordered { + prev, finish = h.enqueueOrdered(key) + } go func() { - defer h.wg.Done() - h.handleLine(line) + defer finishRequest() + if finish != nil { + defer finish() + if prev != nil { + select { + case <-prev: + case <-h.ctx.Done(): + return + } + } + } + if h.ctx.Err() == nil { + h.handleRequest(line, req, parseErr) + } }() } h.drainAndExit(0) } +// enqueueOrdered appends a request in input order and removes idle queues. +func (h *Host) enqueueOrdered(key string) (<-chan struct{}, func()) { + h.orderedMu.Lock() + defer h.orderedMu.Unlock() + prev := h.orderedTail[key] + done := make(chan struct{}) + h.orderedTail[key] = done + return prev, func() { + h.orderedMu.Lock() + defer h.orderedMu.Unlock() + close(done) + if h.orderedTail[key] == done { + delete(h.orderedTail, key) + } + } +} + // maxLineBytes caps one inbound request line. It matches the largest request // shape we expect: a writeFile whose content is up to MaxReadableFileSize // (50 MiB), JSON-escaped, plus envelope overhead. 64 MiB leaves headroom for @@ -341,21 +386,6 @@ func dropCR(line []byte) []byte { // Anchoring keeps an "id" inside params from being mistaken for it. var requestIDAtStart = regexp.MustCompile(`^\s*\{\s*"id"\s*:\s*"((?:[^"\\]|\\.)*)"`) -// requestMethodAtStart matches the method of a request line serialized as -// {id, method, params}. -var requestMethodAtStart = regexp.MustCompile(`^\s*\{\s*"id"\s*:\s*"(?:[^"\\]|\\.)*"\s*,\s*"method"\s*:\s*"([^"\\]*)"`) - -// methodAtStart returns the method of a request line without parsing the -// rest of it, which can be megabytes of params, or "" when the line is not -// laid out as the client writes requests. Such a line is not ordered. -func methodAtStart(line []byte) string { - head := line[:min(len(line), lineHeadBytes)] - if match := requestMethodAtStart.FindSubmatch(head); match != nil { - return string(match[1]) - } - return "" -} - // rejectOversizedLine answers a request line that exceeded maxLineBytes // with a protocol error when its id can be read from the line's head, so the // caller fails fast instead of waiting for its timeout; otherwise it is only @@ -419,23 +449,31 @@ func (h *Host) StartHeartbeat(interval time.Duration) { // orphan linger. A non-positive limit disables the watchdog. Call before Run(); // the goroutine stops when h.ctx is cancelled (drain). func (h *Host) StartIdleWatchdog(limit time.Duration) { + h.startIdleWatchdog(limit, func(check time.Duration) (<-chan time.Time, func()) { + ticker := time.NewTicker(check) + return ticker.C, ticker.Stop + }) +} + +// startIdleWatchdog checks elapsed time at each tick of the supplied clock. +func (h *Host) startIdleWatchdog(limit time.Duration, newTicker func(time.Duration) (<-chan time.Time, func())) { if limit <= 0 { return } - h.stampInbound() + h.stampInbound(time.Now()) // Check at limit/6 (independent of the old limit/3) so raising the limit // keeps the kill window tight: silence trips between limit and limit+limit/6. check := limit / 6 if check <= 0 { check = limit } + ticks, stop := newTicker(check) go func() { - ticker := time.NewTicker(check) - defer ticker.Stop() + defer stop() for { select { - case <-ticker.C: - if h.idleElapsed() >= limit { + case now := <-ticks: + if h.idleElapsed(now) >= limit { // Exit non-zero so the client distinguishes a watchdog reap // from a clean shutdown: its handleClose treats code 0 as a // terminal exit (no reconnect) but reconnects on any other @@ -508,26 +546,113 @@ func (h *Host) InstallSigtermHandler() { }() } -// handleLine parses one NDJSON line, dispatches the request, and writes +// handleRequest dispatches a parsed request and writes // the resulting response. Parse failures are reported with the best id // recoverable from the raw bytes so the client can still correlate. -func (h *Host) handleLine(line []byte) { - req, err := proto.ParseRequest(line) +func (h *Host) handleRequest(line []byte, req proto.Request, err error) { if err != nil { - id := proto.IDFromParsedFrame(line) - if id == "" { - id = proto.IDFromMalformedLine(string(line)) - } - if id == "" { - id = proto.ProtocolErrorID - } - _ = h.WriteFrame(proto.ProtocolFailure(id, protocolMessage(err))) + _ = h.WriteFrame(proto.ProtocolFailure(requestID(line, req), protocolMessage(err))) return } _ = h.WriteFrame(h.dispatcher.Dispatch(h.ctx, req)) } +// Dispatch admits four full desktop reconnect queues or two maximum frames. +// Control traffic gets a separate budget so it can free regular slots. +const ( + maxDispatchRequests = 128 + maxDispatchBytes = 2 * maxLineBytes + maxControlRequests = 32 + maxControlBytes = 1 << 20 + maxBusyReplies = 64 + maxBusyIDBytes = 4 << 10 +) + +type dispatchUsage struct { + requests int + bytes int +} + +// requestID recovers a request's correlation ID even from malformed input. +func requestID(line []byte, req proto.Request) string { + if req.ID != "" { + return req.ID + } + if id := proto.IDFromParsedFrame(line); id != "" { + return id + } + if id := proto.IDFromMalformedLine(string(line)); id != "" { + return id + } + return proto.ProtocolErrorID +} + +// beginRequest reserves a drain slot and the retained line's dispatch budget. +func (h *Host) beginRequest(method string, size int) (release func(), accepting bool) { + h.acceptMu.Lock() + defer h.acceptMu.Unlock() + if !h.accepting { + return nil, false + } + usage, countLimit, byteLimit := &h.regular, maxDispatchRequests, maxDispatchBytes + switch method { + case "pty.ack", "search.cancel", "git.cancel", "lsp.cancel", "chat.stop", "lsp.shutdown", "fs.upload.abort": + usage, countLimit, byteLimit = &h.control, maxControlRequests, maxControlBytes + } + if usage.requests >= countLimit || size > byteLimit-usage.bytes { + return nil, true + } + usage.requests++ + usage.bytes += size + h.wg.Add(1) + return func() { + h.acceptMu.Lock() + usage.requests-- + usage.bytes -= size + h.acceptMu.Unlock() + h.wg.Done() + }, true +} + +// rejectBusy keeps overload responses off the input loop. If the peer keeps +// sending while refusing to drain the bounded reply queue, Run closes it. +func (h *Host) rejectBusy(id string) bool { + h.acceptMu.Lock() + defer h.acceptMu.Unlock() + if !h.accepting { + return true + } + if len(h.rejected) >= maxBusyReplies || len(id) > maxBusyIDBytes { + return false + } + h.rejected = append(h.rejected, id) + if !h.rejecting { + h.rejecting = true + h.wg.Add(1) + go h.writeBusyReplies() + } + return true +} + +// writeBusyReplies writes correlated failures without holding admission locks. +func (h *Host) writeBusyReplies() { + defer h.wg.Done() + for { + h.acceptMu.Lock() + if len(h.rejected) == 0 { + h.rejecting = false + h.acceptMu.Unlock() + return + } + id := h.rejected[0] + h.rejected[0] = "" + h.rejected = h.rejected[1:] + h.acceptMu.Unlock() + _ = h.WriteFrame(proto.Failure(id, proto.CodeUnavailable, "Agent is busy: dispatch limit exceeded")) + } +} + // isAccepting reports whether the loop should still spawn handlers. // Flipped to false during drainAndExit so requests arriving after // SIGTERM are ignored rather than queued behind in-flight work. diff --git a/internal/stdioserver/host_idle_test.go b/internal/stdioserver/host_idle_test.go index 2fb19a74d..b6f5a8fed 100644 --- a/internal/stdioserver/host_idle_test.go +++ b/internal/stdioserver/host_idle_test.go @@ -21,82 +21,59 @@ func newTestHost() *Host { return New(dispatch.New(), strings.NewReader(""), io.Discard, logger) } -// Watchdog fires when no inbound line arrives within the limit. -func TestIdleWatchdogExitsWhenClientVanishes(t *testing.T) { +// TestIdleWatchdogWaitsForSilence advances watchdog ticks independently of +// the scheduler and checks that each inbound line restarts the idle window. +func TestIdleWatchdogWaitsForSilence(t *testing.T) { + t.Parallel() host := newTestHost() + t.Cleanup(host.cancel) exited := make(chan int, 1) - host.exit = func(code int) { + host.exit = func(code int) { exited <- code } + ticks := make(chan time.Time) + stopped := make(chan struct{}) + const limit = time.Minute + host.startIdleWatchdog(limit, func(time.Duration) (<-chan time.Time, func()) { + return ticks, func() { close(stopped) } + }) + start := host.startMono.Add(time.Duration(host.lastInbound.Load())) + + deadline := time.NewTimer(2 * time.Second) + defer deadline.Stop() + tick := func(at time.Time) { + t.Helper() select { - case exited <- code: - default: + case ticks <- at: + case code := <-exited: + t.Fatalf("watchdog exited before tick %s with code %d", at.Sub(start), code) + case <-deadline.C: + t.Fatal("watchdog never accepted the tick") } } - - host.StartIdleWatchdog(30 * time.Millisecond) - + for i := 1; i <= 6; i++ { + inbound := start.Add(time.Duration(i) * limit / 2) + host.stampInbound(inbound) + tick(inbound.Add(limit / 4)) + } + last := start.Add(3 * limit) + tick(last.Add(limit - time.Nanosecond)) + tick(last.Add(limit)) select { case code := <-exited: - // Non-zero (EX_TEMPFAIL) so the client reconnects instead of treating - // the close as a clean shutdown. if code != IdleWatchdogExitCode { t.Fatalf("exit code = %d, want %d", code, IdleWatchdogExitCode) } - case <-time.After(2 * time.Second): - t.Fatal("idle watchdog did not terminate within 2s of client silence") + case <-deadline.C: + t.Fatal("watchdog did not exit at its idle limit") } + <-stopped } -// Watchdog must NOT fire while inbound lines keep resetting lastInbound. -// -// The window runs three limits, so a watchdog that ignored the traffic would -// fire in it. The limit is many times the stamping interval so that a busy CI -// runner stalling this goroutine for a while does not read as client silence: -// a 60ms limit with 20ms stamps failed on macOS runners. -func TestIdleWatchdogStaysAliveWithTraffic(t *testing.T) { - host := newTestHost() - exited := make(chan int, 1) - host.exit = func(code int) { - select { - case exited <- code: - default: - } - } - - const limit = 400 * time.Millisecond - host.StartIdleWatchdog(limit) - - stop := time.After(3 * limit) - tick := time.NewTicker(10 * time.Millisecond) - defer tick.Stop() - for { - select { - case <-tick.C: - host.stampInbound() - case code := <-exited: - t.Fatalf("watchdog fired during active traffic (code=%d)", code) - case <-stop: - return // survived the window without firing - } - } -} - -// A non-positive limit disables the watchdog entirely. +// TestIdleWatchdogDisabledWhenLimitNonPositive checks the disabled option +// without waiting for a watchdog that should never start. func TestIdleWatchdogDisabledWhenLimitNonPositive(t *testing.T) { host := newTestHost() - exited := make(chan int, 1) - host.exit = func(code int) { - select { - case exited <- code: - default: - } - } - - host.StartIdleWatchdog(0) - - select { - case <-exited: - t.Fatal("watchdog fired despite a non-positive (disabled) limit") - case <-time.After(100 * time.Millisecond): - // expected: no termination - } + host.startIdleWatchdog(0, func(time.Duration) (<-chan time.Time, func()) { + t.Fatal("disabled idle watchdog started a ticker") + return nil, func() {} + }) } diff --git a/internal/stdioserver/host_order_test.go b/internal/stdioserver/host_order_test.go index e3eb85183..b1ce97915 100644 --- a/internal/stdioserver/host_order_test.go +++ b/internal/stdioserver/host_order_test.go @@ -81,18 +81,84 @@ func TestRunKeepsOrderedMethodsInArrivalOrder(t *testing.T) { } } -func TestMethodAtStart(t *testing.T) { - cases := map[string]string{ - `{"id":"r-1","method":"lsp.notify","params":{"serverId":"lsp-1"}}`: "lsp.notify", - ` { "id" : "r-\"2" , "method" : "lsp.send" }`: "lsp.send", - // The method must follow the id: one inside params is not it. - `{"params":{"id":"x","method":"lsp.send"},"id":"r-3","method":"fs.readFile"}`: "", - `{"method":"lsp.send","id":"r-4"}`: "", - `not json`: "", +// TestOrderedRequestsStayWithTheirServer verifies that LSP document updates, +// requests and cancellations for one server can pass a stalled peer's queue. +// The stalled queue's next request is discarded when the host cancels. +func TestOrderedRequestsStayWithTheirServer(t *testing.T) { + type params struct { + ServerID string `json:"serverId"` + Step string `json:"step"` } - for line, want := range cases { - if got := methodAtStart([]byte(line)); got != want { - t.Errorf("methodAtStart(%s) = %q, want %q", line, got, want) + started, release := make(chan struct{}), make(chan struct{}) + steps := make(chan string, 4) + d := dispatch.New() + for _, method := range []string{"lsp.notify", "lsp.send", "lsp.cancel"} { + d.RegisterOrderedBy(method, func(_ context.Context, raw json.RawMessage) (any, error) { + var p params + if err := json.Unmarshal(raw, &p); err != nil { + return nil, err + } + if p.Step == "stall" { + close(started) + <-release + } else { + steps <- p.Step + } + return struct{}{}, nil + }, func(raw json.RawMessage) string { + var p params + _ = json.Unmarshal(raw, &p) + return p.ServerID + }) + } + in, feed := io.Pipe() + defer feed.Close() + host := New(d, in, io.Discard, slog.New(slog.NewTextHandler(io.Discard, nil))) + ended := make(chan struct{}) + host.exit = func(int) { close(ended) } + go host.Run() + releaseStall := sync.OnceFunc(func() { close(release) }) + defer releaseStall() + timeout := time.NewTimer(2 * time.Second) + defer timeout.Stop() + write := func(method, server, step string) { + t.Helper() + // Parsing the full envelope keeps the order independent of key layout. + if _, err := fmt.Fprintf(feed, `{"method":%q,"id":%q,"params":{"serverId":%q,"step":%q}}`+"\n", method, step, server, step); err != nil { + t.Fatal(err) } } + write("lsp.notify", "stalled", "stall") + select { + case <-started: + case <-timeout.C: + t.Fatal("first request did not start") + } + write("lsp.send", "stalled", "obsolete") + write("lsp.notify", "healthy", "change") + write("lsp.send", "healthy", "request") + write("lsp.cancel", "healthy", "cancel") + for _, want := range []string{"change", "request", "cancel"} { + select { + case got := <-steps: + if got != want { + t.Fatalf("request %q ran before %q", got, want) + } + case <-timeout.C: + t.Fatalf("healthy server never handled %q", want) + } + } + host.cancel() + releaseStall() + _ = feed.Close() + select { + case <-ended: + case <-timeout.C: + t.Fatal("canceled host did not end") + } + select { + case got := <-steps: + t.Fatalf("canceled request ran: %q", got) + default: + } } diff --git a/internal/stdioserver/host_stress_test.go b/internal/stdioserver/host_stress_test.go index 5592a9bcb..e1fea69f9 100644 --- a/internal/stdioserver/host_stress_test.go +++ b/internal/stdioserver/host_stress_test.go @@ -133,6 +133,7 @@ func (r *constReader) Read(p []byte) (int, error) { // daemon's closes a channel and panics on a second call — and the host // context must be cancelled so the stuck handler can unwind. func TestDrainAndExitCallsExitOnceWhenHandlerIsStuck(t *testing.T) { + t.Parallel() host := New(dispatch.New(), io.NopCloser(strings_reader("")), io.Discard, slog.Default()) var exits atomic.Int32 exited := make(chan struct{}) @@ -164,6 +165,7 @@ func TestDrainAndExitCallsExitOnceWhenHandlerIsStuck(t *testing.T) { // hooks from running: they are what kills the terminals and removes the // hook socket, and an exit without them left both behind. func TestDrainThatGivesUpStillRunsShutdownHooks(t *testing.T) { + t.Parallel() host := New(dispatch.New(), io.NopCloser(strings_reader("")), io.Discard, slog.Default()) exited := make(chan struct{}) host.SetExitFunc(func(int) { close(exited) }) @@ -188,6 +190,7 @@ func TestDrainThatGivesUpStillRunsShutdownHooks(t *testing.T) { // A shutdown hook that hangs cannot hold the exit off for good. func TestHungShutdownHookDoesNotHoldTheExit(t *testing.T) { + t.Parallel() host := New(dispatch.New(), io.NopCloser(strings_reader("")), io.Discard, slog.Default()) exited := make(chan struct{}) host.SetExitFunc(func(int) { close(exited) }) diff --git a/internal/stdioserver/host_transport_test.go b/internal/stdioserver/host_transport_test.go index ce673a16f..dc89a5249 100644 --- a/internal/stdioserver/host_transport_test.go +++ b/internal/stdioserver/host_transport_test.go @@ -14,6 +14,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "io" "log/slog" "reflect" @@ -23,6 +24,7 @@ import ( "time" "github.com/nexus-code/nexus-code/internal/dispatch" + "github.com/nexus-code/nexus-code/internal/proto" ) // errReader returns a non-EOF error on the first Read, simulating an SSH @@ -188,3 +190,69 @@ func TestLineReaderMatchesScanLines(t *testing.T) { t.Fatalf("lines = %#v, want %#v", lines, want) } } + +// TestDispatchRejectsOverloadWithoutQueuingAck holds the ordered queue full +// and verifies that an ACK still enters its handler. +func TestDispatchRejectsOverloadWithoutQueuingAck(t *testing.T) { + in, writer := io.Pipe() + defer writer.Close() + out, replies := io.Pipe() + defer out.Close() + defer replies.Close() + d := dispatch.New() + release := make(chan struct{}) + defer close(release) + acked := make(chan struct{}) + d.RegisterOrdered("slow", func(ctx context.Context, _ json.RawMessage) (any, error) { + select { + case <-release: + return nil, nil + case <-ctx.Done(): + return nil, ctx.Err() + } + }) + d.Register("pty.ack", func(context.Context, json.RawMessage) (any, error) { + close(acked) + return nil, nil + }) + host := New(d, in, replies, slog.New(slog.NewTextHandler(io.Discard, nil))) + host.SetExitFunc(func(int) {}) + go host.Run() + go func() { + var frames strings.Builder + for i := 0; i < 128; i++ { + fmt.Fprintf(&frames, "{\"id\":\"%d\",\"method\":\"slow\"}\n", i) + } + frames.WriteString("{\"id\":\"overload\",\"method\":\"slow\"}\n{\"id\":\"ack\",\"method\":\"pty.ack\"}\n") + _, _ = io.WriteString(writer, frames.String()) + }() + timer := time.NewTimer(2 * time.Second) + defer timer.Stop() + select { + case <-acked: + case <-timer.C: + t.Fatal("ACK stalled behind regular requests") + } + got := make(chan proto.Response, 1) + go func() { + reader := json.NewDecoder(out) + for range 2 { + var response proto.Response + if reader.Decode(&response) != nil { + return + } + if response.ID == "overload" { + got <- response + return + } + } + }() + select { + case response := <-got: + if response.Error == nil || response.Error.Code != proto.CodeUnavailable { + t.Fatalf("overload response = %+v", response) + } + case <-timer.C: + t.Fatal("overload did not produce a rejection") + } +} diff --git a/package.json b/package.json index ea8b700a9..2b996a3d5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nexus-code", "productName": "NexusCode", - "version": "0.34.1", + "version": "0.35.0", "description": "Multi-workspace VSCode-style editor. Monaco editor + terminal in one window.", "homepage": "https://github.com/moreih29/nexus-code", "license": "MIT", diff --git a/packages/remote-protocol/README.md b/packages/remote-protocol/README.md index 1eeb79e97..127f1e65b 100644 --- a/packages/remote-protocol/README.md +++ b/packages/remote-protocol/README.md @@ -14,6 +14,7 @@ Beside them: - **The account key's bundle** (`account.ts`): the account key locked with the account password, as the server keeps it. See `ACCOUNT_KEYS.md`. - **The voice socket** (`voice.ts`): audio to the server, text back while the user speaks. +- **Codex client commands** (`commands.ts`): the slash commands a Codex client runs itself (`CODEX_COMMANDS`: name, aliases, argument hint) and the ones another device may run (`CODEX_REMOTE_COMMANDS`). Sealing is XChaCha20-Poly1305. The key is the room key, derived from the account key. The associated data comes from `aadOf()` and binds a message to its routing. What is sealed is an envelope (`SealedEnvelopeSchema`): the message, the sender's epoch and the receiver's. Each connection to the relay has a random epoch, which devices learn from each other's hellos in three steps. A device takes a message only when it names its epoch now, is addressed to it, and has a seq higher than the last it took from that sender's epoch, so the relay cannot send an old message again or pass one on to another device. `sealed.ts` has the rules. Protocol version 2 (0.7.0) brought these; devices of version 1 open nothing of version 2. diff --git a/packages/remote-protocol/package.json b/packages/remote-protocol/package.json index a2d3fcf59..a114c60b2 100644 --- a/packages/remote-protocol/package.json +++ b/packages/remote-protocol/package.json @@ -1,6 +1,6 @@ { "name": "@nexus-code/remote-protocol", - "version": "0.13.4", + "version": "0.14.0", "description": "NexusCode's remote protocol: the relay's frames and the end-to-end RPC between a user's devices", "license": "MIT", "repository": { diff --git a/packages/remote-protocol/src/commands.ts b/packages/remote-protocol/src/commands.ts new file mode 100644 index 000000000..df98f943b --- /dev/null +++ b/packages/remote-protocol/src/commands.ts @@ -0,0 +1,58 @@ +/** + * The slash commands a Codex client runs itself. Codex's app-server leaves + * them to the client, so the desktop app and a remote device both need the + * same list: names, aliases and argument hints, nothing else. + */ + +export interface CodexClientCommand { + name: string; + aliases?: string[]; + argumentHint?: string; +} + +/** Codex's slash commands the client handles, not the app-server. */ +export const CODEX_COMMANDS: readonly CodexClientCommand[] = [ + { name: "model" }, + { name: "effort" }, + { name: "plan" }, + { name: "default" }, + { name: "fast", argumentHint: "[on|off|status]" }, + { name: "config", aliases: ["settings"] }, + { name: "new", aliases: ["clear"] }, + { name: "resume", argumentHint: "[search]" }, + { name: "fork" }, + { name: "rename", argumentHint: "" }, + { name: "compact" }, + { name: "review", argumentHint: "[instructions]" }, + { name: "init" }, + { name: "goal", argumentHint: "[|clear|pause|resume]" }, + { name: "status" }, + { name: "context" }, + { name: "usage" }, + { name: "diff" }, + { name: "copy", argumentHint: "[N]" }, + { name: "export", argumentHint: "[filename]" }, + { name: "mcp" }, + { name: "skills" }, + { name: "logout" }, + { name: "help" }, + { name: "exit", aliases: ["quit"] }, +]; + +/** Codex's client commands that another device may run, by name. */ +export const CODEX_REMOTE_COMMANDS: ReadonlySet = new Set([ + "new", + "compact", + "fast", + "plan", + "default", + "review", + "init", + "goal", + "status", + "rename", + "fork", + "export", + "mcp", + "skills", +]); diff --git a/packages/remote-protocol/src/index.ts b/packages/remote-protocol/src/index.ts index 1f4e4c143..0f88669f8 100644 --- a/packages/remote-protocol/src/index.ts +++ b/packages/remote-protocol/src/index.ts @@ -9,9 +9,11 @@ * - ./device-keys: each device's key, envelopes and rotation, pairing, and * the password's proof (account keys v2); * - ./voice: the voice socket, speech to text while the user speaks; - * - ./info: what the server offers (GET /info). + * - ./info: what the server offers (GET /info); + * - ./commands: the slash commands a Codex client runs itself. */ export * from "./account.js"; +export * from "./commands.js"; export * from "./device-keys.js"; export * from "./info.js"; export * from "./relay.js"; diff --git a/packages/remote-protocol/src/rpc.ts b/packages/remote-protocol/src/rpc.ts index 821bddde5..a491b99e9 100644 --- a/packages/remote-protocol/src/rpc.ts +++ b/packages/remote-protocol/src/rpc.ts @@ -802,6 +802,24 @@ export type TerminalUpdate = z.infer; export const SessionsUpdateSchema = SessionsSchema.extend({ watchId: z.string() }); export type SessionsUpdate = z.infer; +/** Maximum size negotiated at the start of a file transfer. */ +export const MAX_FILE_WRITE_BYTES = 20 * 1024 ** 3; + +const FileScope = { + /** An absolute path on the serving device, with terminal control. Since 0.14.0. */ + anywhere: z.literal(true).optional(), +}; +const FileTransferRef = { + /** Keep a one-time approval within the paths used by this copy. */ + transferId: z.string().uuid().optional(), +}; +const FileUploadRef = { + ...FileScope, + uploadId: z.string().min(1).max(64), + from: AgentSenderSchema.optional(), +}; +const FileSha256Schema = z.string().regex(/^[0-9a-f]{64}$/); + const Ok = z.object({}); // --------------------------------------------------------------------------- @@ -1674,7 +1692,15 @@ export const RPC_METHODS = { */ "files.list": { capability: "files", - params: z.object({ workspaceId: z.string(), path: z.string().max(4096) }), + params: z.object({ + workspaceId: z.string(), + path: z.string().max(4096), + ...FileScope, + ...FileTransferRef, + /** Recursive push checks its destination without listing its contents. */ + writeTarget: z.literal(true).optional(), + ...ReadBy, + }), result: z.object({ /** The folder, absolute. */ path: z.string(), @@ -1705,6 +1731,9 @@ export const RPC_METHODS = { params: z.object({ workspaceId: z.string(), path: z.string().min(1).max(4096), + ...FileScope, + ...FileTransferRef, + ...ReadBy, maxBytes: z.number().int().positive().max(MAX_READ_BYTES).optional(), as: z.literal("bytes").optional(), offset: z.number().int().nonnegative().optional(), @@ -1756,6 +1785,60 @@ export const RPC_METHODS = { }), ]), }, + /** Begin writing to a final file path whose parent already exists. Since 0.14.0. */ + "files.write.begin": { + capability: "files.write", + params: z.object({ + ...FileScope, + ...FileTransferRef, + workspaceId: z.string(), + path: z.string().min(1).max(4096), + size: z.number().int().nonnegative().max(MAX_FILE_WRITE_BYTES), + from: AgentSenderSchema, + }), + result: z.object({ uploadId: z.string() }), + }, + /** One positioned piece. An upload retains the permission from its begin request. */ + "files.write.chunk": { + capability: "agents", + params: z.object({ + ...FileUploadRef, + offset: z.number().int().nonnegative().max(MAX_FILE_WRITE_BYTES), + data: z + .string() + .min(1) + .max(Math.ceil(MAX_BYTES_CHUNK / 3) * 4), + }), + result: Ok, + }, + /** Verify the entire file and place it at its destination. */ + "files.write.end": { + capability: "agents", + params: z.object({ ...FileUploadRef, sha256: FileSha256Schema }), + result: z.object({ + path: z.string(), + size: z.number().int().nonnegative(), + sha256: FileSha256Schema, + }), + }, + /** Cancel a pending upload and remove its temporary file. */ + "files.write.abort": { + capability: "agents", + params: z.object(FileUploadRef), + result: Ok, + }, + /** Make one directory. Succeeds if it is already a directory. Since 0.14.0. */ + "files.mkdir": { + capability: "files.write", + params: z.object({ + ...FileScope, + ...FileTransferRef, + workspaceId: z.string(), + path: z.string().min(1).max(4096), + from: AgentSenderSchema, + }), + result: z.object({ path: z.string() }), + }, /** * Files by name, as quick open finds them: `query`'s characters in order * in the path (case and spaces not minded), the best matches first (in diff --git a/packages/remote-protocol/src/sealed.ts b/packages/remote-protocol/src/sealed.ts index 95f015c50..4e6164588 100644 --- a/packages/remote-protocol/src/sealed.ts +++ b/packages/remote-protocol/src/sealed.ts @@ -49,7 +49,7 @@ export const PROTOCOL_VERSION = 2; * the methods a device serves are those of its version, so another can * leave out what an older one does not have. Kept equal to package.json's. */ -export const SCHEMA_VERSION = "0.13.4"; +export const SCHEMA_VERSION = "0.14.0"; /** The HKDF info that makes the room key from the account key. */ export const ROOM_KEY_INFO = "nexus-remote/room-key/v1"; @@ -83,6 +83,8 @@ export const CAPABILITIES = [ "terminal.control", /** files.list, files.read, files.find and files.search, in a workspace's folders. */ "files", + /** Workspace file writes and directory creation, with agent control. Since 0.14.0. */ + "files.write", /** git.status, git.diff, git.log and git.show of a workspace. */ "git", /** notes.list and notes.attach: a workspace's notes, and photos put into one. */ diff --git a/packages/remote-protocol/src/voice.ts b/packages/remote-protocol/src/voice.ts index 0f27091d1..eb1f10e96 100644 --- a/packages/remote-protocol/src/voice.ts +++ b/packages/remote-protocol/src/voice.ts @@ -36,7 +36,10 @@ * it lets the device send the session's recording once to POST * /voice/transcriptions for a second pass over the whole file (a more * accurate text and speakers). Only the recording's seconds beyond the - * live session's count against the quota again. + * live session's count against the quota again. An `error` may carry one + * too, when the session had audio worth hearing again; a device that gets + * none from it (or from a server that never sends one) has no second pass + * for that session. * * The second pass (`purpose=note` sessions only), signed in as /voice is: * - POST /voice/transcriptions?v=1&lang=, header @@ -67,7 +70,7 @@ export const VoiceLanguageSchema = z.string().regex(/^([a-z]{2,3}(-[A-Z]{2})?|mu export const VoicePurposeSchema = z.enum(["dictation", "note"]); export type VoicePurpose = z.infer; -/** How long a `done`'s transcribeToken stays good for. */ +/** How long a `done`'s (or `error`'s) transcribeToken stays good for. */ export const TRANSCRIBE_TOKEN_MS = 24 * 60 * 60 * 1000; /** Most bytes of a recording POST /voice/transcriptions takes (about 8 hours of 24 kbps Opus). */ @@ -126,6 +129,12 @@ export const VoiceServerMessageSchema = z.discriminatedUnion("t", [ */ code: z.enum(["bad-frame", "provider-error", "unauthorized"]), message: z.string(), + /** + * A note's session that had audio worth hearing again: as `done`'s, lets + * its recording go once to POST /voice/transcriptions. Missing from a + * server that does not send one. + */ + transcribeToken: z.string().min(1).optional(), }), ]); export type VoiceServerMessage = z.infer; diff --git a/scripts/assets/claude-wrapper.sh b/scripts/assets/claude-wrapper.sh index 70b2d09e2..6df985f64 100755 --- a/scripts/assets/claude-wrapper.sh +++ b/scripts/assets/claude-wrapper.sh @@ -69,7 +69,14 @@ is_print() { return 1 } -REAL_CLAUDE="$(find_real_claude)" || { echo "claude not found in PATH" >&2; exit 127; } +# The chat host already selected and probed this executable. Consume the +# path here so nested commands still find their own Claude on PATH. +if [[ -n "${NEXUS_CLAUDE_PROGRAM:-}" ]]; then + REAL_CLAUDE="$NEXUS_CLAUDE_PROGRAM" + unset NEXUS_CLAUDE_PROGRAM +else + REAL_CLAUDE="$(find_real_claude)" || { echo "claude not found in PATH" >&2; exit 127; } +fi # 3) A `claude -p` run inside the tab's Claude session runs under the agent, # which tells the app of it as one of the session's agents: its start and diff --git a/scripts/check-agent-commands.ts b/scripts/check-agent-commands.ts index 5e6cfe463..e661e85f2 100644 --- a/scripts/check-agent-commands.ts +++ b/scripts/check-agent-commands.ts @@ -20,8 +20,13 @@ import { existsSync, mkdtempSync, readFileSync, realpathSync, rmSync } from "nod import { tmpdir } from "node:os"; import path from "node:path"; import { APP_COMMANDS, type AppCommand } from "../src/renderer/services/chat/app-commands"; -import { CONFIG_KEYS, parseConfigUsage } from "../src/renderer/services/chat/claude-config"; -import { CODEX_COMMANDS } from "../src/renderer/services/chat/codex-commands"; +import { + CONFIG_KEYS, + type ListedKey, + parseConfigUsage, +} from "../src/renderer/services/chat/claude/config"; +import { CODEX_COMMANDS } from "../src/renderer/services/chat/codex/commands"; +import { THREAD_STATUS_CHANGED } from "../src/renderer/services/chat/codex/model"; /** A command its definition turns off; the check says when it is on again. */ const OFF = "off in its definition"; @@ -59,14 +64,30 @@ const CLAUDE_LEFT_OUT: Record = { "terminal-setup": TERMINAL, theme: TERMINAL, tui: TERMINAL, - // https://code.claude.com/docs/en/ultrareview - ultrareview: "an account-gated cloud review alias omitted by the headless slash catalog", "use-case": "an account-gated picker (Life Sciences)", version: OFF, voice: "dictation in the terminal", wellbeing: OFF, }; +/** Commands the tab gets from the CLI when its account and feature flags expose them. */ +const CLAUDE_LEFT_OUT_WHEN_UNAVAILABLE: Record = { + advisor: "chooses a second model for advice when the advisor tool is enabled", + "extra-usage": "the former name of /usage-credits, gated by the same billing options", + import: "imports another agent's configuration when the import feature is enabled", + "skill-doctor": "reports skill usage and context cost when feature-flag fetching is enabled", + ultrareview: "runs a cloud code review on eligible accounts", + "usage-credits": "manages usage credits or requests them from an admin on eligible accounts", +}; + +/** Keys a bare /config can omit when Claude Code is signed out. */ +const CLAUDE_SIGN_IN_CONFIG_KEYS = new Set([ + "remoteControl", + "agentPushNotifEnabled", + "inputNeededNotifEnabled", + "promptSuggestionEnabled", +]); + /** Claude Code's commands the tab could run and does not yet. */ const CLAUDE_LATER: Record = { restart: @@ -150,14 +171,14 @@ interface Report { notes: string[]; } -function run(command: string, args: string[]): string { +function run(command: string, args: string[], exitCodes: readonly number[] = [0]): string { const result = spawnSync(command, args, { encoding: "utf8", maxBuffer: 64 * 1024 * 1024, timeout: 120_000, }); if (result.error) throw result.error; - if (result.status !== 0) { + if (result.status === null || !exitCodes.includes(result.status)) { throw new Error( `${command} ${args.join(" ")} exited ${result.status}: ${result.stderr.trim()}`, ); @@ -308,7 +329,8 @@ function claudeHeadless(program: string): { commands: string[]; configUsage: str "-p", "--no-session-persistence", "--settings", - '{"disableAllHooks":true}', + // /config lists teammateMode only with agent teams enabled. + '{"disableAllHooks":true,"env":{"CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS":"1"}}', "--output-format", "stream-json", "--verbose", @@ -334,40 +356,57 @@ function claudeHeadless(program: string): { commands: string[]; configUsage: str return { commands, configUsage }; } +function claudeSignedOut(program: string): boolean { + // auth status exits 1 when signed out. Require the JSON to confirm that state. + const status = JSON.parse(run(program, ["auth", "status", "--json"], [0, 1])) as { + loggedIn?: boolean; + }; + if (typeof status?.loggedIn !== "boolean") throw new Error("no loggedIn in claude auth status"); + return !status.loggedIn; +} + +/** Compare the keys the dialog gets from a bare /config with the table of value sources. */ +export function claudeConfigProblems(listed: readonly ListedKey[], signedOut: boolean): string[] { + if (listed.length === 0) return ["/config: no keys in a bare /config's answer"]; + const problems: string[] = []; + const listedKeys = new Set(listed.map((k) => k.key)); + const tableKeys = new Set(CONFIG_KEYS.map((k) => k.key)); + for (const key of listedKeys) { + if (!tableKeys.has(key)) { + problems.push( + `/config ${key}: new; the settings dialog shows it under Other, its value read from nowhere (CONFIG_KEYS)`, + ); + } + } + for (const key of tableKeys) { + if (listedKeys.has(key) || (signedOut && CLAUDE_SIGN_IN_CONFIG_KEYS.has(key))) continue; + problems.push(`/config ${key}: Claude Code no longer lists it (CONFIG_KEYS)`); + } + return problems; +} + function checkClaude(): Report { const program = findProgram("claude"); const version = run(program, ["--version"]).trim().split(/\s+/)[0]; const cli = claudeCommandsIn(readFileSync(program).toString("latin1")); const headless = claudeHeadless(program); + const fromCli = new Set(headless.commands); const { problems, notes } = accountFor("Claude Code", cli, { - fromCli: new Set(headless.commands), + fromCli, app: APP_COMMANDS, appTable: "APP_COMMANDS", - leftOut: CLAUDE_LEFT_OUT, + leftOut: { + ...CLAUDE_LEFT_OUT, + ...Object.fromEntries( + Object.entries(CLAUDE_LEFT_OUT_WHEN_UNAVAILABLE).filter(([name]) => !fromCli.has(name)), + ), + }, later: CLAUDE_LATER, appOnly: CLAUDE_APP_ONLY, }); - // The settings dialog shows every key a bare /config lists; the table - // says where each one's value is read. const listed = parseConfigUsage(headless.configUsage); - if (listed.length === 0) { - problems.push("/config: no keys in a bare /config's answer"); - } else { - const listedKeys = new Set(listed.map((k) => k.key)); - const tableKeys = new Set(CONFIG_KEYS.map((k) => k.key)); - for (const key of listedKeys) { - if (!tableKeys.has(key)) { - problems.push( - `/config ${key}: new; the settings dialog shows it under Other, its value read from nowhere (CONFIG_KEYS)`, - ); - } - } - for (const key of tableKeys) { - if (!listedKeys.has(key)) - problems.push(`/config ${key}: Claude Code no longer lists it (CONFIG_KEYS)`); - } - } + problems.push(...claudeConfigProblems(listed, claudeSignedOut(program))); notes.push(`/config: ${listed.length} keys, the table ${CONFIG_KEYS.length}`); return { title: `Claude Code ${version} (${program})`, problems, notes }; } @@ -464,20 +503,22 @@ function codexMethods(program: string, experimental = false) { function methodsTheAppUses() { const renderer = path.join(ROOT, "src/renderer"); const source = (file: string) => readFileSync(path.join(renderer, file), "utf8"); - const controller = source("services/chat/codex-controller.ts"); + const controller = source("services/chat/codex/controller.ts"); const callers = [ controller, - source("services/chat/run-codex-command.ts"), + source("services/chat/codex/run-command.ts"), source("components/chat/codex-tab.tsx"), ].join("\n"); const requests = new Set( [...callers.matchAll(/\b(?:request|ask)\(\s*"([a-z][\w/]*)"/g)].map((m) => m[1]), ); const read = new Set( - [...source("services/chat/codex-model.ts").matchAll(/case "([a-z]\w*(?:\/\w+)+)"/g)].map( + [...source("services/chat/codex/model.ts").matchAll(/case "([a-z]\w*(?:\/\w+)+)"/g)].map( (m) => m[1], ), ); + // The shared predicate accepts a compatibility alias. The schema must list the current name. + read.add(THREAD_STATUS_CHANGED); const experimentalOptIn = /capabilities:\s*\{[^}]*\bexperimentalApi:\s*true\b/.test(controller); return { requests, read, experimentalOptIn }; } diff --git a/src/main/features/agent-profiles/local.ts b/src/main/features/agent-profiles/local.ts index 4ebd8fd71..ed65edcb3 100644 --- a/src/main/features/agent-profiles/local.ts +++ b/src/main/features/agent-profiles/local.ts @@ -8,6 +8,11 @@ import type { Stats } from "node:fs"; import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; +import { + mergeAgentEnv, + profileDirectory, + profileVariables, +} from "../../../shared/agent-profiles/env"; import { type AgentProfile, PROFILE_DIR_ENV, @@ -45,9 +50,8 @@ export function localDefaultDir(agent: AgentProfile["agent"]): string { /** The profile's folder here, absolute; the default folder for the default profile. */ export function localProfileDir(profile: AgentProfile): string { - return profile.dir.trim() === "" - ? localDefaultDir(profile.agent) - : path.resolve(expandHome(profile.dir)); + const dir = profileDirectory(profile, windows); + return dir ? path.resolve(expandHome(dir)) : localDefaultDir(profile.agent); } /** The environment the agent's program runs with here, for the profile. */ @@ -56,27 +60,19 @@ export function localProfileEnv( base: NodeJS.ProcessEnv = process.env, onWindows = windows, ): NodeJS.ProcessEnv { - const env: NodeJS.ProcessEnv = { ...base }; - for (const key of INHERITED_ONLY) delete env[key]; - for (const [key, value] of Object.entries(profile.env ?? {})) setVar(env, key, value, onWindows); - if (profile.dir.trim() !== "") - setVar(env, PROFILE_DIR_ENV[profile.agent], localProfileDir(profile), onWindows); - return env; -} - -/** - * Sets a variable. A Windows variable is one whatever the case of its name, - * so there it takes the place of one named in another case ("Path" of - * "PATH"): left beside it, the program would get either. - */ -function setVar(env: NodeJS.ProcessEnv, key: string, value: string, onWindows: boolean): void { - if (onWindows) { - const lower = key.toLowerCase(); - for (const other of Object.keys(env)) { - if (other !== key && other.toLowerCase() === lower) delete env[other]; - } - } - env[key] = value; + const inherited = Object.fromEntries( + Object.entries(base).filter( + ([key]) => !INHERITED_ONLY.includes(onWindows ? key.toUpperCase() : key), + ), + ); + const dir = profileDirectory(profile, onWindows); + const variables = profileVariables(profile.env, onWindows); + return mergeAgentEnv( + onWindows, + inherited, + variables, + dir ? { [PROFILE_DIR_ENV[profile.agent]]: path.resolve(expandHome(dir)) } : {}, + ); } /** diff --git a/src/main/features/app-state/index.ts b/src/main/features/app-state/index.ts index 1814334bd..6f628c594 100644 --- a/src/main/features/app-state/index.ts +++ b/src/main/features/app-state/index.ts @@ -1,11 +1,17 @@ import { ipcContract } from "../../../shared/ipc/contract"; +import { ipcErr, ipcOk } from "../../../shared/ipc/result"; import type { AppState } from "../../../shared/types/app-state"; +import type { WorkspaceMeta } from "../../../shared/types/workspace"; import { register, validateArgs } from "../../infra/ipc-router"; import type { StateService } from "../../infra/storage/state-service"; +import { restoreLayoutOrigins } from "./layout-origins"; + const c = ipcContract.appState.call; export interface AppStateChannelOptions { + /** Current workspace metadata for restoring legacy layout records. */ + listWorkspaces?: () => WorkspaceMeta[]; /** * Invoked synchronously after a `set` call that includes a `language` * field, with the newly requested language value. @@ -56,17 +62,37 @@ export interface AppStateChannelOptions { } export function registerAppStateChannel( - stateService: StateService, + stateService: Pick, opts: AppStateChannelOptions = {}, ): void { register("appState", { + keepOnRendererClose: ["set"], call: { get: (_args: unknown) => { - return stateService.getState(); + const state = stateService.getState(); + return opts.listWorkspaces ? restoreLayoutOrigins(state, opts.listWorkspaces()) : state; }, set: (args: unknown) => { const patch = validateArgs(c.set.args, args); - stateService.setState(patch); + if (patch.layoutByWorkspace) { + // A window sends only changed workspaces. Merge into the latest state + // so closing it cannot replace another window's unrelated layouts. + const latest = { + ...stateService.getState().layoutByWorkspace, + ...patch.layoutByWorkspace, + }; + const workspaceIds = opts.listWorkspaces?.().map(({ id }) => id); + patch.layoutByWorkspace = workspaceIds + ? Object.fromEntries( + workspaceIds.flatMap((id) => (latest[id] ? [[id, latest[id]]] : [])), + ) + : latest; + } + try { + stateService.setState(patch); + } catch (error) { + return ipcErr("state-write-failed", String(error), { category: "failed" }); + } // Detect a language change and invoke the injected callback so the // caller can update the main-process i18n instance, rebuild the menu, @@ -100,6 +126,7 @@ export function registerAppStateChannel( if (patch.themePreference !== undefined) { opts.onThemeChanged?.(patch.themePreference); } + return ipcOk(undefined); }, }, listen: {}, diff --git a/src/main/features/app-state/layout-origins.ts b/src/main/features/app-state/layout-origins.ts new file mode 100644 index 000000000..b28bfbf59 --- /dev/null +++ b/src/main/features/app-state/layout-origins.ts @@ -0,0 +1,53 @@ +import { toSlashPath } from "../../../shared/fs/drive-path"; +import { isWithinWorkspace } from "../../../shared/fs/workspace-path"; +import type { AppState } from "../../../shared/types/app-state"; +import type { SerializedTab } from "../../../shared/types/layout"; +import type { WorkspaceMeta } from "../../../shared/types/workspace"; + +/** Recognize absolute paths in the formats Open File stores. */ +function pathStyle(path: string): "posix" | "drive" | null { + if (path.split("/").some((part) => part === "." || part === "..")) return null; + if (/^[A-Z]:\//.test(path)) return "drive"; + return path.startsWith("/") && !path.startsWith("//") ? "posix" : null; +} + +/** + * Restore a legacy file tab using Open File's containment check. A remote + * or ambiguous path cannot establish that the file is safe to route to the + * local external loader. Omit it so the user can reopen it explicitly. + */ +function restoreTabOrigin(tab: SerializedTab, workspace: WorkspaceMeta): SerializedTab | null { + if (tab.type !== "editor" || tab.props.origin !== undefined) return tab; + const filePath = toSlashPath(tab.props.filePath); + const root = toSlashPath(workspace.rootPath); + const style = pathStyle(filePath); + if (tab.props.workspaceId !== workspace.id || !style || style !== pathStyle(root)) return null; + if (isWithinWorkspace(filePath, root)) return tab; + if ( + workspace.location.kind !== "local" || + isWithinWorkspace(filePath.toLowerCase(), root.toLowerCase()) + ) { + return null; + } + return { ...tab, props: { ...tab.props, filePath, origin: "external" } }; +} + +/** Restore layouts for surviving workspaces and upgrade origin-less records. */ +export function restoreLayoutOrigins( + state: Readonly, + workspaces: WorkspaceMeta[], +): Readonly { + if (!state.layoutByWorkspace) return state; + const byId = new Map(workspaces.map((workspace) => [workspace.id, workspace])); + return { + ...state, + layoutByWorkspace: Object.fromEntries( + Object.entries(state.layoutByWorkspace).flatMap(([id, snapshot]) => { + const workspace = byId.get(id); + if (!workspace) return []; + const tabs = snapshot.tabs.flatMap((tab) => restoreTabOrigin(tab, workspace) ?? []); + return [[id, { ...snapshot, tabs }]]; + }), + ), + }; +} diff --git a/src/main/features/browser/ipc.ts b/src/main/features/browser/ipc.ts index 303c4f9b9..8b9c41301 100644 --- a/src/main/features/browser/ipc.ts +++ b/src/main/features/browser/ipc.ts @@ -1,12 +1,6 @@ /** * Browser tab IPC channel — bridges renderer commands to BrowserTabRegistry - * and broadcasts WebContents lifecycle events back to all renderer windows. - * - * BROADCASTS - * ---------- - * Every browser.* event (navigated, loadingChanged, error, titleUpdated) is - * sent via the shared `broadcast` helper so all open renderer windows receive - * it. The renderer is responsible for filtering by tabId. + * and routes WebContents lifecycle events to the renderer that created the view. * * SECURITY * -------- @@ -24,9 +18,9 @@ */ import { ipcContract } from "../../../shared/ipc/contract"; -import { ipcOk } from "../../../shared/ipc/result"; +import { ipcErr, ipcOk } from "../../../shared/ipc/result"; import { COMMANDS } from "../../../shared/keybindings/commands"; -import { broadcast, register, validateArgs } from "../../infra/ipc-router"; +import { broadcast, type CallContext, register, validateArgs } from "../../infra/ipc-router"; import type { GlobalStorage } from "../../infra/storage/global-storage"; import type { WorkspaceStorage } from "../../infra/storage/workspace-storage"; import { getActiveAppWindow } from "../window/app-windows"; @@ -67,7 +61,12 @@ export interface BrowserChannelDeps { * directly; URL focus needs the renderer, so it is bounced over IPC. App * commands (tabs, groups, workspaces…) run in the app window. */ -function runBrowserCommand(registry: BrowserTabRegistry, command: string, tabId: string): void { +function runBrowserCommand( + registry: BrowserTabRegistry, + command: string, + tabId: string, + focusUrl: () => void, +): void { switch (command) { case COMMANDS.browserReload: registry.reload({ tabId }); @@ -82,7 +81,7 @@ function runBrowserCommand(registry: BrowserTabRegistry, command: string, tabId: registry.goForward({ tabId }); break; case COMMANDS.browserFocusUrl: - broadcast("browser", "focusUrl", { tabId }); + focusUrl(); break; default: { // As if the key had been pressed in the app window: that window takes @@ -99,10 +98,36 @@ export function registerBrowserChannel( registry: BrowserTabRegistry, channelDeps?: BrowserChannelDeps, ): void { + type Owner = { viewId: string; sender: NonNullable }; + const owners = new Map(); + + /** Publish current-generation events directly to the view's renderer. */ + function sendViewEvent( + tabId: string, + event: string, + payload: Record, + owner = owners.get(tabId), + ): void { + if (!owner || owners.get(tabId) !== owner || owner.sender.isDestroyed()) return; + owner.sender.send("ipc:event", "browser", event, { ...payload, tabId, viewId: owner.viewId }); + } + + /** Resolve a request from the owning renderer, optionally for a particular generation. */ + function ownsView(tabId: string, ctx: CallContext | undefined, viewId?: string): boolean { + const owner = owners.get(tabId); + return ( + owner !== undefined && + owner.sender.id === ctx?.sender?.id && + (viewId === undefined || viewId === owner.viewId) + ); + } + + registry.onLoadFailure((tabId, failure) => { + sendViewEvent(tabId, "loadFailureChanged", { failure }); + }); registry.onRefused((tabId, url, error) => { const { kind, reason } = error as { kind?: unknown; reason?: unknown }; - broadcast("browser", "refused", { - tabId, + sendViewEvent(tabId, "refused", { url, kind: typeof kind === "string" ? kind : "failed", message: typeof reason === "string" ? reason : error.message, @@ -110,30 +135,54 @@ export function registerBrowserChannel( }); register("browser", { call: { - create: (args: unknown) => { - const { tabId, workspaceId, url, partition } = validateArgs(c.create.args, args); - registry.create({ tabId, workspaceId, url, partition }); + create: (args: unknown, ctx) => { + const { tabId, viewId, workspaceId, url, partition } = validateArgs(c.create.args, args); + if (!ctx?.sender) return ipcErr("not-found", "Browser renderer is unavailable"); + const previous = owners.get(tabId); + if (previous && previous.sender.id !== ctx.sender.id && !previous.sender.isDestroyed()) { + return ipcErr("conflict", "Browser tab is open in another window"); + } + // Dispose the old generation before allowing the new one to emit. + owners.delete(tabId); + if (registry.get(tabId)) registry.destroy({ tabId }); + const owner = { viewId, sender: ctx.sender }; + owners.set(tabId, owner); + try { + registry.create({ tabId, workspaceId, url, partition }); + } catch (error) { + owners.delete(tabId); + return ipcErr("browser-create-failed", String(error), { category: "bug" }); + } // Wire WebContents lifecycle events for broadcasts. // The entry was just created so it MUST be present. const entry = registry.get(tabId); - if (!entry) return ipcOk(undefined); // Should never happen; guard for safety. + if (!entry) { + owners.delete(tabId); + return ipcErr("browser-create-failed", "Browser view was not created", { + category: "bug", + }); + } const wc = entry.view.webContents; + wc.once("destroyed", () => { + if (owners.get(tabId) === owner) owners.delete(tabId); + }); + const emit = (event: string, value: Record) => + sendViewEvent(tabId, event, value, owner); noticeDownloads(wc.session, (_from, file) => broadcast("browser", "downloaded", { file })); wc.on("did-navigate", (_event, navUrl, _httpResponseCode, _httpStatusText) => { - broadcast("browser", "navigated", { - tabId, + emit("navigated", { url: navUrl, canGoBack: wc.navigationHistory.canGoBack(), canGoForward: wc.navigationHistory.canGoForward(), }); }); - wc.on("did-navigate-in-page", (_event, navUrl) => { - broadcast("browser", "navigated", { - tabId, + wc.on("did-navigate-in-page", (_event, navUrl, isMainFrame) => { + if (!isMainFrame) return; + emit("navigated", { url: navUrl, canGoBack: wc.navigationHistory.canGoBack(), canGoForward: wc.navigationHistory.canGoForward(), @@ -141,36 +190,22 @@ export function registerBrowserChannel( }); wc.on("did-start-loading", () => { - broadcast("browser", "loadingChanged", { tabId, isLoading: true }); + emit("loadingChanged", { isLoading: true }); }); wc.on("did-stop-loading", () => { - broadcast("browser", "loadingChanged", { tabId, isLoading: false }); - }); - - wc.on("did-fail-load", (_event, errorCode, errorDescription, validatedURL) => { - broadcast("browser", "error", { - tabId, - code: errorCode, - description: errorDescription, - url: validatedURL, - }); - // Loading has ended (failed) — ensure isLoading is toggled off - // even when did-stop-loading fires before the renderer processes - // this event. - broadcast("browser", "loadingChanged", { tabId, isLoading: false }); + emit("loadingChanged", { isLoading: false }); }); wc.on("page-title-updated", (_event, title) => { - broadcast("browser", "titleUpdated", { tabId, title }); + emit("titleUpdated", { title }); }); // page-favicon-updated: fires when Chromium finds favicon URL candidates. // If the page does not declare link rel="icon", the fallback `/favicon.ico` is tried. // The renderer shows the first candidate as the tab icon (the default Globe if none). wc.on("page-favicon-updated", (_event, favicons: string[]) => { - broadcast("browser", "faviconUpdated", { - tabId, + emit("faviconUpdated", { favicons: favicons.filter(isLoadableFaviconUrl), }); }); @@ -182,7 +217,7 @@ export function registerBrowserChannel( // behaviour of every other panel type). Fires only on focus *gain*, so // it does not spam on repeated clicks inside an already-focused view. wc.on("focus", () => { - broadcast("browser", "focused", { tabId }); + emit("focused", {}); }); // Intercept keystrokes that land while the PAGE has focus — they @@ -191,95 +226,95 @@ export function registerBrowserChannel( // browser command. (The docked DevTools view is covered by the // registry's DevTools key interceptor wired above.) installBrowserKeyInterceptor(wc, tabId, (command, t) => - runBrowserCommand(registry, command, t), + runBrowserCommand(registry, command, t, () => emit("focusUrl", {})), ); return ipcOk(undefined); }, - destroy: (args: unknown) => { - const { tabId } = validateArgs(c.destroy.args, args); + destroy: (args: unknown, ctx) => { + const { tabId, viewId } = validateArgs(c.destroy.args, args); + if (!ownsView(tabId, ctx, viewId)) return ipcOk(undefined); + owners.delete(tabId); registry.destroy({ tabId }); return ipcOk(undefined); }, - setBounds: (args: unknown) => { - const { tabId, x, y, width, height } = validateArgs(c.setBounds.args, args); + setBounds: (args: unknown, ctx) => { + const { tabId, viewId, x, y, width, height } = validateArgs(c.setBounds.args, args); + if (!ownsView(tabId, ctx, viewId)) return ipcOk(undefined); registry.setBounds({ tabId, x, y, width, height }); return ipcOk(undefined); }, - setActive: (args: unknown) => { - const { tabId, active } = validateArgs(c.setActive.args, args); + setActive: (args: unknown, ctx) => { + const { tabId, viewId, active } = validateArgs(c.setActive.args, args); + if (!ownsView(tabId, ctx, viewId)) return ipcOk(undefined); registry.setActive({ tabId, active }); return ipcOk(undefined); }, - navigate: (args: unknown) => { + navigate: (args: unknown, ctx) => { const { tabId, url } = validateArgs(c.navigate.args, args); + if (!ownsView(tabId, ctx)) return ipcOk(undefined); registry.navigate({ tabId, url }); return ipcOk(undefined); }, - goBack: (args: unknown) => { + goBack: (args: unknown, ctx) => { const { tabId } = validateArgs(c.goBack.args, args); + if (!ownsView(tabId, ctx)) return ipcOk(undefined); registry.goBack({ tabId }); return ipcOk(undefined); }, - goForward: (args: unknown) => { + goForward: (args: unknown, ctx) => { const { tabId } = validateArgs(c.goForward.args, args); + if (!ownsView(tabId, ctx)) return ipcOk(undefined); registry.goForward({ tabId }); return ipcOk(undefined); }, - reload: (args: unknown) => { + reload: (args: unknown, ctx) => { const { tabId, ignoreCache } = validateArgs(c.reload.args, args); + if (!ownsView(tabId, ctx)) return ipcOk(undefined); registry.reload({ tabId, ignoreCache }); return ipcOk(undefined); }, - openDevTools: (args: unknown) => { + openDevTools: (args: unknown, ctx) => { const { tabId } = validateArgs(c.openDevTools.args, args); + if (!ownsView(tabId, ctx)) return ipcOk(undefined); const { open } = registry.openDevTools({ tabId }); // Tell the renderer to show/hide the splitter and start/stop // reporting setDevToolsBounds for this tab. - broadcast("browser", "devtoolsToggled", { tabId, open }); + sendViewEvent(tabId, "devtoolsToggled", { open }); return ipcOk(undefined); }, - setDevToolsBounds: (args: unknown) => { - const { tabId, x, y, width, height } = validateArgs(c.setDevToolsBounds.args, args); + setDevToolsBounds: (args: unknown, ctx) => { + const { tabId, viewId, x, y, width, height } = validateArgs(c.setDevToolsBounds.args, args); + if (!ownsView(tabId, ctx, viewId)) return ipcOk(undefined); registry.setDevToolsBounds({ tabId, x, y, width, height }); return ipcOk(undefined); }, suspendAll: async (args: unknown) => { const { captureSnapshot } = validateArgs(c.suspendAll.args, args); - // The registry's suspendAll captures BEFORE hiding (VSCode pattern) so - // the renderer can overlay each snapshot on top of the still-visible - // placeholder before the native view goes dark — no flash, no blank. + // Capture before hiding so the renderer can show the last frame under the overlay. + const capturing = new Map(owners); const snapshots = await registry.suspendAll({ captureSnapshot }); for (const { tabId, dataUrl } of snapshots) { - // Only broadcast when capture succeeded and produced a usable - // payload; tiny/empty captures arrive as null and the renderer - // keeps the prior placeholder without harm. - if (dataUrl !== null) { - broadcast("browser", "snapshot", { kind: "set", tabId, dataUrl }); - } + const owner = capturing.get(tabId); + if (owner) sendViewEvent(tabId, "snapshot", { kind: "set", dataUrl }, owner); } return ipcOk(undefined); }, resumeAll: (args: unknown) => { validateArgs(c.resumeAll.args, args); - const resumed = registry.resumeAll(); - for (const tabId of resumed) { - // Drop the cached snapshot in the renderer so the live view shows - // through again. Sent even when no snapshot was previously - // broadcast for this tab (e.g. drag-mode suspend with - // captureSnapshot=false) — harmless on the renderer side. - broadcast("browser", "snapshot", { kind: "cleared", tabId }); + for (const tabId of registry.resumeAll()) { + sendViewEvent(tabId, "snapshot", { kind: "cleared" }); } return ipcOk(undefined); }, @@ -288,7 +323,7 @@ export function registerBrowserChannel( listen: { navigated: {}, loadingChanged: {}, - error: {}, + loadFailureChanged: {}, refused: {}, titleUpdated: {}, snapshot: {}, diff --git a/src/main/features/browser/local-files.ts b/src/main/features/browser/local-files.ts new file mode 100644 index 000000000..61da719f6 --- /dev/null +++ b/src/main/features/browser/local-files.ts @@ -0,0 +1,116 @@ +import { realpathSync, statSync } from "node:fs"; +import { homedir } from "node:os"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import type { OnBeforeRequestListenerDetails, Session, WebContents, WebFrameMain } from "electron"; +import { createLogger } from "../../../shared/log/main"; +import { isScriptReadableRequest } from "../../infra/security/script-readable-request"; + +const logger = createLogger("browser-security"); +const guardedSessions = new WeakSet(); +const committedUrls = new WeakMap(); +const htmlExtensions = new Set([".html", ".htm", ".xhtml"]); + +function isDescendant(folder: string, file: string, paths: typeof path): boolean { + const relative = paths.relative(folder, file); + return ( + relative !== "" && + relative !== ".." && + !relative.startsWith(`..${paths.sep}`) && + !paths.isAbsolute(relative) + ); +} + +/** The inputs have already been resolved to their real filesystem paths. */ +export function isScopedFilePath( + folder: string, + file: string, + home: string, + paths = path, +): boolean { + return ( + paths.relative(folder, paths.parse(folder).root) !== "" && + paths.relative(folder, home) !== "" && + !isDescendant(folder, home, paths) && + isDescendant(folder, file, paths) + ); +} + +function isFileInHtmlFolder(pageUrl: string, currentUrl: string, requestUrl: string): boolean { + if (!pageUrl.startsWith("file:") || !requestUrl.startsWith("file:")) return false; + try { + const page = fileURLToPath(pageUrl); + // Fragments and query strings can change within the document. The file + // that establishes the scope comes from its cross-document commit. + if (path.relative(page, fileURLToPath(currentUrl)) !== "") return false; + if (!htmlExtensions.has(path.extname(page).toLowerCase())) return false; + const target = fileURLToPath(requestUrl); + const folder = path.dirname(realpathSync.native(page)); + // Reject obvious out-of-folder paths before following the target's links. + if (!isDescendant(path.dirname(page), target, path) && !isDescendant(folder, target, path)) { + return false; + } + const realTarget = realpathSync.native(target); + return ( + isScopedFilePath(folder, realTarget, realpathSync.native(homedir())) && + statSync(realTarget).isFile() + ); + } catch { + return false; + } +} + +function canReadLocalFile(details: OnBeforeRequestListenerDetails): boolean { + const { frame, webContents, resourceType, url, initiatorOrigin } = details; + if (!frame || !webContents || frame.detached) return false; + const committedUrl = committedUrls.get(webContents); + if (!committedUrl) return false; + if (initiatorOrigin !== undefined && initiatorOrigin !== "file://") return false; + const main = webContents.mainFrame; + // A subframe navigation can report the destination frame. Reads made by + // subframe documents themselves receive no separate grant. + if (frame !== main && (resourceType !== "subFrame" || frame.parent !== main)) return false; + return isFileInHtmlFolder(committedUrl, main.url, url); +} + +/** Only the main HTML document can embed a local file in a direct child. */ +export function canNavigateLocalFrame( + contents: WebContents, + url: string, + frame: WebFrameMain | null, + initiator: WebFrameMain | null | undefined, +): boolean { + const committedUrl = committedUrls.get(contents); + if (!url.startsWith("file:") || !committedUrl || !frame || frame.detached) { + return false; + } + const main = contents.mainFrame; + return ( + initiator === main && frame.parent === main && isFileInHtmlFolder(committedUrl, main.url, url) + ); +} + +/** Adds a tab to the session-wide guard. Popups share the guard without gaining a scope. */ +export function installLocalFileReadGuard(contents: WebContents): void { + if (!committedUrls.has(contents)) { + committedUrls.set(contents, ""); + contents.on("did-navigate", (_event, url) => committedUrls.set(contents, url)); + } + const session = contents.session; + if (guardedSessions.has(session)) return; + guardedSessions.add(session); + session.webRequest.onBeforeRequest({ urls: ["file://*/*"] }, (details, callback) => { + const { resourceType, url } = details; + let cancel = resourceType === "object"; + if (resourceType === "subFrame" || isScriptReadableRequest(resourceType, url)) { + cancel = true; + try { + cancel = !canReadLocalFile(details); + } catch { + // A frame or tab may have gone away before Electron delivered the request. + } + } + if (cancel) logger.warn(`[file-read] blocked ${resourceType}`); + callback({ cancel }); + }); +} diff --git a/src/main/features/browser/registry.ts b/src/main/features/browser/registry.ts index 8db631cb1..f2a76081e 100644 --- a/src/main/features/browser/registry.ts +++ b/src/main/features/browser/registry.ts @@ -4,8 +4,7 @@ * * LIFECYCLE * --------- - * create() — allocates a WebContentsView, applies security policy, and - * attaches it to the main window's contentView. + * create() — allocates a detached WebContentsView and applies security policy. * destroy() — closes the WebContents, detaches the view, removes the entry. * setActive() — attach (active=true) or detach (active=false) the view. * setBounds() — resize/reposition. Renderer sends CSS-layout pixels; we @@ -15,7 +14,7 @@ * needs no conversion. * suspendAll() — capture (optional) + hide every active view so DOM * overlays (modals/menus/drag indicators) can render above. - * resumeAll() — show every previously-active view. + * resumeAll() — show every currently active view and clear snapshots. * * SUSPEND VS SETACTIVE * -------------------- @@ -56,11 +55,11 @@ import { createLogger } from "../../../shared/log/main"; import { isTabLoadAllowed } from "../../../shared/security/navigation-allowlist"; import { excludeFromBroadcast } from "../../infra/ipc-router"; import { showDialogsOverWindow } from "./dialogs"; +import { installLocalFileReadGuard } from "./local-files"; import { installAppScrollbarStyle } from "./page-style"; import { PopupTracker } from "./popup-tracker"; import { buildBrowserTabWebPreferences, - installLocalFileReadGuard, installNavigationGuards, installPermissionHandler, type PermissionHandlerDeps, @@ -76,19 +75,17 @@ interface TabEntry { view: WebContentsView; workspaceId: string; partition: string; + loadFailure: BrowserLoadFailure | null; + navigationRequest: number; + forwardedAddress: { requested: string; target: string } | null; /** * Whether this tab is the active tab in its group. * - * The view is actually attached to the window iff `active && !registry.suspended`. - * Decoupling these two flags lets the global suspendAll/resumeAll cycle - * detach every active view temporarily without losing the per-tab activation - * state we restore on resume. + * Active views stay attached while an overlay temporarily hides them. */ active: boolean; /** - * Last bounds applied via setBounds(). Cached so resumeAll() can re-apply - * them after re-attaching — Electron does NOT preserve bounds across - * remove/addChildView pairs. + * Last CSS bounds applied via setBounds(), restored on reattach. */ lastBounds: CssBounds | null; /** @@ -102,7 +99,7 @@ interface TabEntry { /** * Last bounds applied via `setDevToolsBounds`. Same role as `lastBounds` * — re-applied whenever the devtools view is re-attached after a - * suspend/resume or setActive(true) cycle. + * setActive(true) or DevTools toggle. */ devtoolsBounds: CssBounds | null; /** @@ -143,10 +140,15 @@ export interface BrowserAddressResolver { resolve(workspaceId: string, url: string): Promise; } -/** One snapshot result for `suspendAll` — `dataUrl` is null when capture failed. */ -export interface SuspendSnapshot { +export interface BrowserLoadFailure { + url: string; + description: string; +} + +/** A usable page snapshot captured by `suspendAll`. */ +interface SuspendSnapshot { tabId: string; - dataUrl: string | null; + dataUrl: string; } export class BrowserTabRegistry { @@ -174,11 +176,16 @@ export class BrowserTabRegistry { * be visible. */ private suspendGeneration = 0; + /** Tabs whose overlay snapshot needs clearing on resume, even after detaching. */ + private readonly snapshotTabs = new Set(); /** Told of each tab's WebContents as it is created, before its first page loads. */ private readonly createListeners = new Set<(tabId: string, contents: WebContents) => void>(); private addressResolver: BrowserAddressResolver | null = null; /** Told of a URL the resolver would not open, and why. */ private readonly refusedListeners = new Set<(tabId: string, url: string, error: Error) => void>(); + private readonly loadFailureListeners = new Set< + (tabId: string, failure: BrowserLoadFailure | null) => void + >(); constructor(win: BrowserWindow, permissionDeps?: PermissionHandlerDeps) { this.win = win; @@ -190,8 +197,7 @@ export class BrowserTabRegistry { // ------------------------------------------------------------------------- /** - * Creates a new WebContentsView for `tabId`, navigates it to `url`, and - * adds it (hidden initially) to the main window's contentView hierarchy. + * Creates a detached WebContentsView for `tabId` and navigates it to `url`. * * If a view for `tabId` already exists it is destroyed before the new one * is created — see class doc for the rationale. @@ -213,7 +219,7 @@ export class BrowserTabRegistry { // workspaceId is bound here (not derived from the session) because Electron // does not expose the partition string on the Session object. installPermissionHandler(view.webContents.session, this.permissionDeps, workspaceId); - installLocalFileReadGuard(view.webContents.session); + installLocalFileReadGuard(view.webContents); // Wire navigation guards. The onNavigate callback is intentionally // stubbed here; callers (ipc.ts) attach broadcast callbacks by listening @@ -240,6 +246,9 @@ export class BrowserTabRegistry { view, workspaceId, partition, + loadFailure: null, + navigationRequest: 0, + forwardedAddress: null, active: false, lastBounds: null, devtoolsView: null, @@ -247,13 +256,26 @@ export class BrowserTabRegistry { devtoolsOpen: false, }; this.tabs.set(tabId, entry); + view.webContents.on("did-fail-load", (_event, code, description, url, isMainFrame) => { + if (!isMainFrame || code === -3) return; // ERR_ABORTED + const forward = entry.forwardedAddress; + this.setLoadFailure(tabId, entry, { + url: forward?.target === url ? forward.requested : url, + description, + }); + }); + view.webContents.on("did-navigate", () => this.setLoadFailure(tabId, entry, null)); for (const listener of this.createListeners) listener(tabId, view.webContents); // A page's own move to a local address of an SSH workspace goes to the // address's forward instead. Guards that blocked it already win. view.webContents.on("will-navigate", (event, target) => { if (event.defaultPrevented) return; - if (!this.addressResolver?.rewrites(workspaceId, target)) return; + if (!this.addressResolver?.rewrites(workspaceId, target)) { + entry.navigationRequest++; + entry.forwardedAddress = null; + return; + } event.preventDefault(); this.load(tabId, entry, target, "will-navigate"); }); @@ -284,17 +306,29 @@ export class BrowserTabRegistry { return () => this.refusedListeners.delete(listener); } + onLoadFailure(listener: (tabId: string, failure: BrowserLoadFailure | null) => void): void { + this.loadFailureListeners.add(listener); + } + + private setLoadFailure(tabId: string, entry: TabEntry, failure: BrowserLoadFailure | null): void { + if (this.tabs.get(tabId) !== entry || (failure === null && entry.loadFailure === null)) return; + entry.loadFailure = failure; + if (entry.active) entry.view.setVisible(!this.suspended && failure === null); + for (const listener of this.loadFailureListeners) listener(tabId, failure); + } + /** Loads `url` in the tab, through the address resolver when it has a say. */ private load(tabId: string, entry: TabEntry, url: string, label: string): void { + const request = ++entry.navigationRequest; + entry.forwardedAddress = null; if (!isTabLoadAllowed(url)) { - const error = new Error("a browser tab opens only http, https and file addresses"); - logger.warn(`[${label}] ${url} not opened in ${tabId}: ${error.message}`); - for (const listener of this.refusedListeners) listener(tabId, url, error); + logger.warn(`[${label}] disallowed address in ${tabId}`); + this.setLoadFailure(tabId, entry, { url, description: "ERR_DISALLOWED_URL_SCHEME" }); return; } const loadNow = (target: string) => { - entry.view.webContents.loadURL(target).catch((err: Error) => { - logger.warn(`[${label}] loadURL failed for ${tabId}: ${err.message}`); + entry.view.webContents.loadURL(target).catch(() => { + logger.warn(`[${label}] loadURL failed for ${tabId}`); }); }; const resolver = this.addressResolver; @@ -302,15 +336,23 @@ export class BrowserTabRegistry { loadNow(url); return; } + const isCurrent = () => + this.tabs.get(tabId) === entry && + entry.navigationRequest === request && + !entry.view.webContents.isDestroyed(); resolver.resolve(entry.workspaceId, url).then( (resolved) => { - // The tab may have closed while the forward was set up. - if (this.tabs.get(tabId) !== entry || entry.view.webContents.isDestroyed()) return; + if (!isCurrent()) return; + entry.forwardedAddress = { requested: url, target: resolved }; loadNow(resolved); }, (error: Error) => { - if (this.tabs.get(tabId) !== entry) return; - logger.warn(`[${label}] ${url} not opened in ${tabId}: ${error.message}`); + if (!isCurrent()) return; + logger.warn(`[${label}] forwarded address not opened in ${tabId}`); + const { kind } = error as { kind?: unknown }; + const description = + kind === "not-connected" ? "ERR_SSH_NOT_CONNECTED" : "ERR_PORT_FORWARD_FAILED"; + this.setLoadFailure(tabId, entry, { url, description }); for (const listener of this.refusedListeners) listener(tabId, url, error); }, ); @@ -374,6 +416,7 @@ export class BrowserTabRegistry { } this.tabs.delete(tabId); + this.snapshotTabs.delete(tabId); logger.debug(`[destroy] tab ${tabId} destroyed`); } @@ -420,10 +463,7 @@ export class BrowserTabRegistry { return; } - // Cache so resumeAll() can re-apply after a suspend/resume cycle — - // Electron does not preserve bounds across removeChildView/addChildView. - // Cached in CSS px (verbatim from the renderer) so a later re-apply picks - // up the *current* zoom factor, not the one in effect when this arrived. + // Keep CSS pixels so reattachment uses the current shell zoom factor. entry.lastBounds = { x, y, width, height }; this.applyCssBounds(entry.view, entry.lastBounds); @@ -470,16 +510,10 @@ export class BrowserTabRegistry { /** * Activates or deactivates the view for `tabId`. * - * Active → view is added to the main window's contentView (unless the - * registry is currently suspended), background throttling is - * disabled so animations stay smooth. + * Active → view is added to the main window's contentView and follows + * the current suspend state. Background throttling is disabled. * Inactive → view is removed from the main window's contentView, background * throttling is enabled to conserve CPU/GPU resources. - * - * The actual attachment depends on `active && !this.suspended` — during a - * global suspend window (open dropdown / modal / drag) every active view is - * detached on the registry side without disturbing the per-tab `active` - * flag, so `resumeAll` can restore exactly the same set. */ setActive(args: { tabId: string; active: boolean }): void { const { tabId, active } = args; @@ -502,16 +536,6 @@ export class BrowserTabRegistry { if (entry.devtoolsOpen && entry.devtoolsView !== null) { this.attachAndRestoreDevtoolsBounds(entry); } - // If the registry is currently suspended (an overlay is in progress), - // immediately hide the freshly-attached view so it doesn't paint above - // the modal. resumeAll() will call setVisible(true) once the overlay - // closes. - if (this.suspended && !entry.view.webContents.isDestroyed()) { - entry.view.setVisible(false); - if (entry.devtoolsView !== null && !entry.devtoolsView.webContents.isDestroyed()) { - entry.devtoolsView.setVisible(false); - } - } entry.view.webContents.setBackgroundThrottling(false); } else { this.safeRemoveChildView(entry); @@ -531,6 +555,8 @@ export class BrowserTabRegistry { * the view would reappear at (0, 0) sized 0×0. */ private attachAndRestoreBounds(entry: TabEntry): void { + // An overlay may have ended while the view was detached. + entry.view.setVisible(!this.suspended && entry.loadFailure === null); this.win.contentView.addChildView(entry.view); const b = entry.lastBounds; if (b) { @@ -558,6 +584,7 @@ export class BrowserTabRegistry { private attachAndRestoreDevtoolsBounds(entry: TabEntry): void { const dt = entry.devtoolsView; if (dt === null) return; + dt.setVisible(!this.suspended); this.win.contentView.addChildView(dt); const b = entry.devtoolsBounds; if (b !== null) { @@ -614,25 +641,22 @@ export class BrowserTabRegistry { this.suspended = true; const gen = ++this.suspendGeneration; - const activeEntries: Array<[string, TabEntry]> = []; + const activeEntries = new Map(); for (const [tabId, entry] of this.tabs) { if (entry.active && !entry.view.webContents.isDestroyed()) { - activeEntries.push([tabId, entry]); + activeEntries.set(tabId, entry); } } - let snapshots: SuspendSnapshot[] = []; - if (opts.captureSnapshot) { - snapshots = await this.captureActiveSnapshots(activeEntries); - } + const snapshots = opts.captureSnapshot ? await this.captureActiveSnapshots(activeEntries) : []; // Bail if resumeAll ran while we were capturing — the view is supposed // to be visible now, hiding here would re-introduce the blank area the // user just dismissed. if (gen !== this.suspendGeneration) return []; - for (const [, entry] of activeEntries) { - if (entry.view.webContents.isDestroyed()) continue; + for (const entry of activeEntries.values()) { + if (!entry.active || entry.view.webContents.isDestroyed()) continue; entry.view.setVisible(false); // Mirror the hide onto the DevTools host view so its OS-level overlay // doesn't paint above a DOM modal either. @@ -645,16 +669,16 @@ export class BrowserTabRegistry { } } - return snapshots; + return snapshots.filter(({ tabId }) => { + if (this.tabs.get(tabId) !== activeEntries.get(tabId)) return false; + this.snapshotTabs.add(tabId); + return true; + }); } /** - * Re-show every WebContentsView that was active when the matching - * `suspendAll` ran. - * - * Returns the list of tabIds that were re-shown — the caller broadcasts a - * `snapshot {cleared: true}` event for each so the renderer drops its - * cached image and exposes the live view again. + * Re-show currently attached views and return the IDs of all tabs with + * snapshots to clear, including tabs that detached during the overlay. */ resumeAll(): string[] { if (!this.suspended) return []; @@ -662,10 +686,9 @@ export class BrowserTabRegistry { // Bump generation so any in-flight suspendAll capture skips its hide step. this.suspendGeneration++; - const resumed: string[] = []; - for (const [tabId, entry] of this.tabs) { + for (const entry of this.tabs.values()) { if (entry.active && !entry.view.webContents.isDestroyed()) { - entry.view.setVisible(true); + entry.view.setVisible(entry.loadFailure === null); if ( entry.devtoolsOpen && entry.devtoolsView !== null && @@ -673,10 +696,11 @@ export class BrowserTabRegistry { ) { entry.devtoolsView.setVisible(true); } - resumed.push(tabId); } } - return resumed; + const snapshotTabs = [...this.snapshotTabs]; + this.snapshotTabs.clear(); + return snapshotTabs; } /** @@ -686,33 +710,33 @@ export class BrowserTabRegistry { * returns from `nativeImage.toDataURL()`, which matters because the result * crosses the IPC boundary as a broadcast payload to the renderer. * - * `dataUrl` is set to `null` when capture failed, returned an empty image - * (page not yet rendered), or produced a payload too small to be a real - * page snapshot — the renderer treats `null` as "no overlay available" - * and keeps the existing placeholder. + * Failed, empty, and very small captures leave the existing placeholder visible. */ private async captureActiveSnapshots( - activeEntries: Array<[string, TabEntry]>, + activeEntries: ReadonlyMap, ): Promise { const TINY_DATA_URL_THRESHOLD = 2_000; // ~150x150 solid-colour JPEG floor - const captures = activeEntries.map(async ([tabId, entry]): Promise => { - try { - const img = await entry.view.webContents.capturePage(); - if (img.isEmpty()) return { tabId, dataUrl: null }; - const buf = img.toJPEG(75); - const dataUrl = `data:image/jpeg;base64,${buf.toString("base64")}`; - if (dataUrl.length < TINY_DATA_URL_THRESHOLD) { - return { tabId, dataUrl: null }; + const captures = [...activeEntries].map( + async ([tabId, entry]): Promise => { + try { + if (entry.loadFailure) return null; + const img = await entry.view.webContents.capturePage(); + if (img.isEmpty()) return null; + const buf = img.toJPEG(75); + const dataUrl = `data:image/jpeg;base64,${buf.toString("base64")}`; + if (dataUrl.length < TINY_DATA_URL_THRESHOLD) { + return null; + } + return { tabId, dataUrl }; + } catch (err) { + logger.warn(`[suspendAll] capturePage failed for ${tabId}: ${(err as Error).message}`); + return null; } - return { tabId, dataUrl }; - } catch (err) { - logger.warn(`[suspendAll] capturePage failed for ${tabId}: ${(err as Error).message}`); - return { tabId, dataUrl: null }; - } - }); + }, + ); - return Promise.all(captures); + return (await Promise.all(captures)).filter((snapshot) => snapshot !== null); } // ------------------------------------------------------------------------- @@ -740,6 +764,7 @@ export class BrowserTabRegistry { return; } if (entry.view.webContents.navigationHistory.canGoBack()) { + entry.navigationRequest++; entry.view.webContents.navigationHistory.goBack(); } } @@ -751,6 +776,7 @@ export class BrowserTabRegistry { return; } if (entry.view.webContents.navigationHistory.canGoForward()) { + entry.navigationRequest++; entry.view.webContents.navigationHistory.goForward(); } } @@ -762,6 +788,11 @@ export class BrowserTabRegistry { logger.warn(`[reload] unknown tabId: ${tabId}`); return; } + if (entry.loadFailure) { + this.load(tabId, entry, entry.loadFailure.url, "retry"); + return; + } + entry.navigationRequest++; if (ignoreCache) { entry.view.webContents.reloadIgnoringCache(); } else { @@ -818,11 +849,6 @@ export class BrowserTabRegistry { // bounds yet — that's fine, attach lazily on the next setActive(true). if (entry.active) { this.attachAndRestoreDevtoolsBounds(entry); - // Mirror the suspend state — a global suspendAll may be in progress - // when the user opens DevTools (unlikely but harmless to handle). - if (this.suspended && !entry.devtoolsView.webContents.isDestroyed()) { - entry.devtoolsView.setVisible(false); - } } entry.view.webContents.setDevToolsWebContents(entry.devtoolsView.webContents); diff --git a/src/main/features/browser/security.ts b/src/main/features/browser/security.ts index e37eedf41..3585cca6c 100644 --- a/src/main/features/browser/security.ts +++ b/src/main/features/browser/security.ts @@ -8,7 +8,7 @@ * 1. webPreferences — constructor-time hardening. * 2. Permission handler — deny-by-default, allow-list only. * 3. Navigation guards — will-navigate / will-frame-navigate / - * setWindowOpenHandler — block anything that is not http/https. + * setWindowOpenHandler — restrict schemes and embedded local files. * * CRITICAL: every guard must remain in place. Removing any one of them * creates a code-execution or data-exfiltration vector. @@ -16,7 +16,7 @@ * - `javascript:` → executes arbitrary code in the renderer process. * - `data:`/`blob:` → injects arbitrary HTML. * - `file:` → reads the local filesystem. - * - New window (`window.open`) → opened as a hardened popup (http/https/file + * - New window (`window.open`) → opened as a hardened popup (http/https * only) that inherits the tab's partition/session and is recursively * subjected to these same guards; other schemes are denied. It does not * escape the partition sandbox. @@ -35,8 +35,8 @@ import { isSubframeNavigationAllowed, } from "../../../shared/security/navigation-allowlist"; import { excludeFromBroadcast } from "../../infra/ipc-router"; -import { isScriptReadableRequest } from "../../infra/security/script-readable-request"; import { UserActivation, watchActivation } from "../window/activation"; +import { canNavigateLocalFrame } from "./local-files"; import { resolvePermission } from "./permission-policy"; import type { BrowserPermissionPromptManager } from "./permission-prompt-manager"; @@ -210,31 +210,6 @@ export function installPermissionHandler( }); } -/** - * Cancels file:// requests through which page script could read a local file - * (see isScriptReadableRequest). Electron grants file:// pages privileges - * Chrome does not, so a page opened from disk in a tab could otherwise read - * anything the user can — `~/.ssh/id_rsa` included — and send it out. - * Navigating to, rendering and embedding local files still works. - */ -export function installLocalFileReadGuard(session: import("electron").Session): void { - session.webRequest.onBeforeRequest({ urls: ["file://*/*"] }, (details, callback) => { - const cancel = cancelsLocalFileRequest(details.resourceType, details.url); - if (cancel) logger.warn(`[file-read] blocked ${details.resourceType}: ${details.url}`); - callback({ cancel }); - }); -} - -/** - * Whether a browser tab's file:// request is cancelled: one page script - * could read (fetch, XHR, a JSON module), and a frame or object of a local - * file, which the file:// page that holds it could read as well. - */ -export function cancelsLocalFileRequest(resourceType: string, url: string): boolean { - if (resourceType === "subFrame" || resourceType === "object") return true; - return isScriptReadableRequest(resourceType, url); -} - /** WebContents whose page lifetime already ends their pending prompts. */ const promptLifetimeTracked = new Set(); @@ -307,9 +282,12 @@ export function installNavigationGuards( // Covers: location.href assignments, clicks, and // programmatic webContents.loadURL calls that originate from the page. webContents.on("will-navigate", (event, url) => { - if (!allowed(url)) { + if ( + !allowed(url) || + (url.startsWith("file:") && event.initiator && event.initiator !== webContents.mainFrame) + ) { event.preventDefault(); - logger.warn(`[will-navigate] blocked: ${url}`); + logger.warn("[will-navigate] blocked"); return; } onNavigate?.(url); @@ -350,9 +328,17 @@ export function installNavigationGuards( // rendering without widening the scheme allowlist. if (isBuiltinPdfViewerUrl(url)) return; - if (!isSubframeNavigationAllowed(url)) { + if ( + !isSubframeNavigationAllowed(url) && + !canNavigateLocalFrame(webContents, url, details.frame, details.initiator) + ) { + details.preventDefault(); + logger.warn("[will-frame-navigate] blocked"); + } + }); + webContents.on("will-redirect", (details) => { + if (details.url.startsWith("file:") && (popup || !details.isMainFrame)) { details.preventDefault(); - logger.warn(`[will-frame-navigate] blocked: ${url}`); } }); @@ -380,7 +366,7 @@ export function installNavigationGuards( watchActivation(webContents, activation); webContents.setWindowOpenHandler(({ url }) => { if (isPopupNavigationAllowed(url) && !activation.take()) { - logger.warn(`[window-open] blocked without user input: ${url}`); + logger.warn("[window-open] blocked without user input"); return { action: "deny" }; } if (isPopupNavigationAllowed(url)) { @@ -396,7 +382,7 @@ export function installNavigationGuards( }, }; } - logger.warn(`[window-open] blocked: ${url}`); + logger.warn("[window-open] blocked"); return { action: "deny" }; }); diff --git a/src/main/features/chat/host.ts b/src/main/features/chat/host.ts index 122689d98..e0b290595 100644 --- a/src/main/features/chat/host.ts +++ b/src/main/features/chat/host.ts @@ -1,12 +1,23 @@ // Main-process side of the Claude tabs: starts each tab's program through // the workspace's agent and relays its chat.* events to the renderer. +import { randomUUID } from "node:crypto"; +import { + agentEnvValue, + mergeAgentEnv, + profileDirectory, + profileVariables, +} from "../../../shared/agent-profiles/env"; import { type AgentProfile, + defaultProfileId, PROFILE_DIR_ENV, SHAREABLE, } from "../../../shared/agent-profiles/profiles"; +import { type ClaudeTurnResult, claudeTurnResult } from "../../../shared/claude/turn-result"; +import type { TranscriptPageResult } from "../../../shared/ipc/contract"; import { createLogger } from "../../../shared/log/main"; +import type { ChatAgent } from "../../../shared/sessions/task-agent"; import type { AgentChannel, ChannelLifecycleEvent } from "../../infra/agent/channel"; import type { ChatAgentContext } from "../cli/commands/agent-context"; import type { WindowsAgentFiles } from "../pty/windows-shell-shim"; @@ -23,19 +34,24 @@ import { parseClaudeVersion, type ResumeOptions, } from "./launch"; +import { OAuthCallbacks } from "./oauth-callback"; const log = createLogger("chat-host"); /** How long `claude --version` may take: a first run can unpack itself. */ const VERSION_TIMEOUT_MS = 20_000; +/** How long workspace removal waits for its chats, including queued requests. */ +const WORKSPACE_CHAT_CLOSE_MS = 5000; /** How long a `claude` command may take: an install fetches the plugin. */ const COMMAND_TIMEOUT_MS = 30_000; +/** The agent needs time to close the child's pipes and return its timed-out result. */ +const COMMAND_REPLY_TIMEOUT_MS = COMMAND_TIMEOUT_MS + 5_000; export interface ChatWorkspaceManager { getAgentChannel(workspaceId: string): Promise; tryGetAgentChannel(workspaceId: string): Promise; onProviderReady(listener: (workspaceId: string) => void): () => void; - getHookInfo(workspaceId: string): HookInfo | null; + getSpawnHookInfo(workspaceId: string, channel: AgentChannel): Promise; getWrapperBinDir(workspaceId: string): string | null; getWrapperAgentBin(workspaceId: string): string | null; getWrapperShimDir(workspaceId: string): string | null; @@ -47,10 +63,14 @@ export interface ChatHostOptions { broadcast: (event: string, args: unknown) => void; /** On Windows, where local claude runs without the bash wrapper. */ windowsAgentFiles?: () => Promise; - /** A tab's turn ended short (interrupted, failed), which no hook tells. */ - onTurnStopped?: (workspaceId: string, tabId: string) => void; + /** The result a Claude chat reported on its stream. */ + onTurnResult?: (workspaceId: string, tabId: string, result: ClaudeTurnResult) => void; + /** The lifecycle messages for a Claude chat's background tasks. */ + onTaskEvent?: (workspaceId: string, tabId: string, message: Record) => void; /** A tab's program exited. */ onProgramExit?: (workspaceId: string, tabId: string) => void; + /** Saved chat tabs that may still have a program even before their view opens. */ + knownChatTabs?: (workspaceId: string) => readonly string[]; /** The agent profile of that id, as the app state has it; null when gone. */ profile?: (id: string) => AgentProfile | null; /** CLI guidance for the node hosting this chat tab, when enabled in Settings. */ @@ -61,13 +81,33 @@ export type StartResult = | { ok: true; pid: number; attached: boolean; version: string | null; canonicalCwd?: string } | { ok: false; reason: "not-found" | "failed"; message: string }; +/** A profile choice that is no longer valid for this agent. */ +export class AgentProfileSelectionError extends Error {} + interface Subscription { channel: AgentChannel; + revision: number; + processes: Map; dispose: () => void; } +interface TabIntent { + generation: string; + starts: Set>; +} + +interface WorkspaceChatClose { + readonly channel: AgentChannel | null; + readonly deadline: number; + readonly expired: Promise; +} + +/** Owns chat programs and events for the desktop's workspace channels. */ export class ChatHost { + readonly oauth = new OAuthCallbacks(); private readonly subscriptions = new Map(); + private readonly tabIntents = new Map(); + private readonly closingTabs = new Map>(); /** Workspaces whose tabs have started a program through this host. */ private readonly workspaces = new Set(); private readonly disposeProviderReady: () => void; @@ -83,6 +123,8 @@ export class ChatHost { .then(async (channel) => { if (!channel) return; await channel.ready; + await this.options.workspaceManager.getSpawnHookInfo(workspaceId, channel); + if (this.disposed || !this.workspaces.has(workspaceId)) return; this.subscribe(workspaceId, channel); this.options.broadcast("resync", { workspaceId }); }) @@ -99,41 +141,45 @@ export class ChatHost { resume: ResumeOptions, profileId?: string, ): Promise { - const channel = await this.channelFor(workspaceId); + let profile: AgentProfile; + try { + profile = this.profile("claude", profileId); + } catch (error) { + return { ok: false, reason: "failed", message: (error as Error).message }; + } + const key = `${workspaceId}:${tabId}`; + const intent = this.openTab(key); + const channel = await this.channelForStart(workspaceId, key, intent); const { workspaceManager } = this.options; - const windows = - this.options.windowsAgentFiles !== undefined && - workspaceManager.isLocal?.(workspaceId) === true; + const windows = this.windowsHost(workspaceId); + const withoutWrapper = windows && this.options.windowsAgentFiles !== undefined; - const found = await channel.call<{ path: string; envPath?: string }>("lsp.findBinary", { - name: "claude", - dir: cwd, - }); + let profileEnv: Record; + try { + profileEnv = await this.profileEnv(channel, profile, windows); + } catch (error) { + return { ok: false, reason: "failed", message: (error as Error).message }; + } + const found = await this.findBinary(channel, "claude", cwd, profileEnv, windows); if (!found.path) { return { ok: false, reason: "not-found", message: "claude was not found on the PATH" }; } - let version: string | null = null; - try { - const run = await channel.call<{ code: number; stdout: string }>("chat.exec", { - cwd, - program: found.path, - args: ["--version"], - env: found.envPath ? { PATH: found.envPath } : {}, - timeoutMs: VERSION_TIMEOUT_MS, - }); - if (run.code === 0) version = parseClaudeVersion(run.stdout); - } catch (err) { - log.warn(`claude --version failed: ${(err as Error).message}`); - } + const path = agentEnvValue(profileEnv, "PATH", windows) ?? found.envPath; + const version = await this.probeVersion( + channel, + "claude", + cwd, + found.path, + mergeAgentEnv(windows, profileEnv, path === undefined ? {} : { PATH: path }), + ); - const hookInfo = workspaceManager.getHookInfo(workspaceId); const wrapperBinDir = workspaceManager.getWrapperBinDir(workspaceId); const agentBin = workspaceManager.getWrapperAgentBin(workspaceId); let program = found.path; let hooksSettings: string | undefined; let gitBash: { wrapperDir: string; bashEnv: string } | undefined; - if (windows) { + if (withoutWrapper) { // cmd and PowerShell pass claude the hooks file the same way. const files = await this.options.windowsAgentFiles?.(); hooksSettings = files?.settingsPath; @@ -143,30 +189,31 @@ export class ChatHost { } } else if (wrapperBinDir !== null) { // The wrapper adds the hooks and the tab's mark, then runs the claude - // the PATH has after it. + // version probe selected. program = hostJoin(wrapperBinDir, "claude", false); } - const env = claudeTabEnv({ - workspaceId, - tabId, - path: found.envPath, - wrapperBinDir, - agentBin, - agentSocket: hookInfo?.socketPath, - hookToken: hookInfo ? tabHookToken(hookInfo, workspaceId, tabId) : undefined, - withoutWrapper: windows, - ...(gitBash ? { gitBash } : {}), - }); - try { - Object.assign(env, await this.profileEnv(channel, "claude", profileId)); - const result = await channel.call<{ pid: number; attached?: boolean }>("chat.start", { + await this.closingTabs.get(key); + const hookInfo = await workspaceManager.getSpawnHookInfo(workspaceId, channel); + const env = claudeTabEnv({ + workspaceId, + tabId, + path, + wrapperBinDir, + agentBin, + agentSocket: hookInfo?.socketPath, + hookToken: hookInfo ? tabHookToken(hookInfo, workspaceId, tabId) : undefined, + withoutWrapper, + ...(gitBash ? { gitBash } : {}), + }); + if (program !== found.path) env.NEXUS_CLAUDE_PROGRAM = found.path; + const result = await this.spawn(key, intent, channel, { workspaceId, tabId, cwd, program, args: claudeTabArgs(resume, hooksSettings, version), - env, + env: mergeAgentEnv(windows, profileEnv, env), ...(resume.resetLog ? { resetLog: true } : {}), ...(resume.resume !== undefined ? { resumed: true } : {}), }); @@ -189,57 +236,58 @@ export class ChatHost { cwd: string, options: { resetLog?: boolean; profileId?: string }, ): Promise { - const channel = await this.channelFor(workspaceId); - const found = await channel.call<{ path: string; envPath?: string }>("lsp.findBinary", { - name: "codex", - dir: cwd, - }); + let profile: AgentProfile; + try { + profile = this.profile("codex", options.profileId); + } catch (error) { + return { ok: false, reason: "failed", message: (error as Error).message }; + } + const key = `${workspaceId}:${tabId}`; + const intent = this.openTab(key); + const channel = await this.channelForStart(workspaceId, key, intent); + const windows = this.windowsHost(workspaceId); + let profileEnv: Record; + try { + profileEnv = await this.profileEnv(channel, profile, windows); + } catch (error) { + return { ok: false, reason: "failed", message: (error as Error).message }; + } + const found = await this.findBinary(channel, "codex", cwd, profileEnv, windows); if (!found.path) { return { ok: false, reason: "not-found", message: "codex was not found on the PATH" }; } - let version: string | null = null; + const path = agentEnvValue(profileEnv, "PATH", windows) ?? found.envPath; + const version = await this.probeVersion( + channel, + "codex", + cwd, + found.path, + mergeAgentEnv(windows, profileEnv, path === undefined ? {} : { PATH: path }), + ); + + const { workspaceManager } = this.options; try { - const run = await channel.call<{ code: number; stdout: string }>("chat.exec", { + await this.closingTabs.get(key); + const hookInfo = await workspaceManager.getSpawnHookInfo(workspaceId, channel); + const env = codexTabEnv({ + workspaceId, + tabId, + path, + wrapperBinDir: workspaceManager.getWrapperBinDir(workspaceId), + agentBin: workspaceManager.getWrapperAgentBin(workspaceId), + agentSocket: hookInfo?.socketPath, + hookToken: hookInfo ? tabHookToken(hookInfo, workspaceId, tabId) : undefined, + shimDir: windows ? null : workspaceManager.getWrapperShimDir(workspaceId), + }); + const result = await this.spawn(key, intent, channel, { + workspaceId, + tabId, cwd, program: found.path, - args: ["--version"], - env: found.envPath ? { PATH: found.envPath } : {}, - timeoutMs: VERSION_TIMEOUT_MS, + args: ["app-server"], + env: mergeAgentEnv(windows, profileEnv, env), + ...(options.resetLog ? { resetLog: true } : {}), }); - if (run.code === 0) version = parseClaudeVersion(run.stdout); - } catch (err) { - log.warn(`codex --version failed: ${(err as Error).message}`); - } - const { workspaceManager } = this.options; - const hookInfo = workspaceManager.getHookInfo(workspaceId); - // Windows commands run in PowerShell or cmd, which read no shim files. - const windows = - this.options.windowsAgentFiles !== undefined && - workspaceManager.isLocal?.(workspaceId) === true; - const env = codexTabEnv({ - workspaceId, - tabId, - path: found.envPath, - wrapperBinDir: workspaceManager.getWrapperBinDir(workspaceId), - agentBin: workspaceManager.getWrapperAgentBin(workspaceId), - agentSocket: hookInfo?.socketPath, - hookToken: hookInfo ? tabHookToken(hookInfo, workspaceId, tabId) : undefined, - shimDir: windows ? null : workspaceManager.getWrapperShimDir(workspaceId), - }); - try { - Object.assign(env, await this.profileEnv(channel, "codex", options.profileId)); - const result = await channel.call<{ pid: number; attached?: boolean; canonicalCwd?: string }>( - "chat.start", - { - workspaceId, - tabId, - cwd, - program: found.path, - args: ["app-server"], - env, - ...(options.resetLog ? { resetLog: true } : {}), - }, - ); return { ok: true, pid: result.pid, @@ -252,6 +300,85 @@ export class ChatHost { } } + /** Marks a tab as wanted before any asynchronous startup preparation. */ + private openTab(key: string): TabIntent { + const known = this.tabIntents.get(key); + if (known) return known; + const intent = { generation: randomUUID(), starts: new Set>() }; + this.tabIntents.set(key, intent); + return intent; + } + + /** Starts only the still-wanted tab. */ + private async spawn( + key: string, + intent: TabIntent, + channel: AgentChannel, + params: Record, + ): Promise<{ pid: number; attached?: boolean; canonicalCwd?: string }> { + if (this.tabIntents.get(key) !== intent) throw new Error("chat tab closed before it started"); + const run = channel.call<{ pid: number; attached?: boolean; canonicalCwd?: string }>( + "chat.start", + { + ...params, + tabGeneration: intent.generation, + }, + ); + intent.starts.add(run); + try { + return await run; + } finally { + intent.starts.delete(run); + } + } + + /** Invalidates preparation at once and closes any start already sent to the agent. */ + private async closeTab( + workspaceId: string, + tabId: string, + removal?: WorkspaceChatClose, + ): Promise { + const key = `${workspaceId}:${tabId}`; + const intent = this.tabIntents.get(key); + const previous = this.closingTabs.get(key); + if (previous && !intent && !removal) return previous; + this.tabIntents.delete(key); + const close = async () => { + if (removal?.channel === null) return; + if (previous) { + if (!intent) return previous; + await previous.catch(() => undefined); + } + if (intent) await Promise.allSettled(intent.starts); + const params = { + workspaceId, + tabId, + ...(intent ? { tabGeneration: intent.generation } : {}), + }; + if (removal) { + const timeoutMs = removal.deadline - Date.now(); + if (timeoutMs <= 0) return removal.expired; + return removal.channel.call("chat.close", params, { timeoutMs }); + } + const channel = await this.options.workspaceManager.tryGetAgentChannel(workspaceId); + return channel?.call("chat.close", params); + }; + const run = (async () => { + try { + return await (removal ? Promise.race([close(), removal.expired]) : close()); + } finally { + this.subscriptions.get(workspaceId)?.processes.delete(tabId); + this.options.onProgramExit?.(workspaceId, tabId); + } + })(); + this.closingTabs.set(key, run); + try { + return await run; + } finally { + if (this.closingTabs.get(key) === run) this.closingTabs.delete(key); + } + } + async send(workspaceId: string, tabId: string, message: Record): Promise { const channel = await this.channelFor(workspaceId); const note = @@ -270,19 +397,44 @@ export class ChatHost { method: "stop" | "close" | "replay" | "attach" | "pruneAttachments", args: unknown, ): Promise { - const workspaceId = (args as { workspaceId: string }).workspaceId; - const channel = - method === "close" - ? await this.options.workspaceManager.tryGetAgentChannel(workspaceId) - : await this.channelFor(workspaceId); - if (!channel) return undefined; - if (method !== "close") this.subscribe(workspaceId, channel); - return channel.call(`chat.${method}`, args); + const { workspaceId, tabId } = args as { workspaceId: string; tabId: string }; + if (method === "close") return this.closeTab(workspaceId, tabId); + return (await this.channelFor(workspaceId)).call(`chat.${method}`, args); } async transcript(workspaceId: string, sessionId: string, profileId?: string): Promise { + const config = this.configDir(workspaceId, profileId); const channel = await this.channelFor(workspaceId); - return channel.call("chat.transcript", { sessionId, ...this.configDir(profileId) }); + return channel.call("chat.transcript", { + sessionId, + ...config, + }); + } + + async transcriptPage( + workspaceId: string, + sessionId: string, + profileId: string | undefined, + page: { cursor?: string; beforeAnchor?: string; upTo?: string; before?: string[] }, + ): Promise { + const args = { sessionId, ...this.configDir(workspaceId, profileId) }; + const channel = await this.channelFor(workspaceId); + try { + return await channel.call("chat.transcriptPage", { ...args, ...page }); + } catch (error) { + if (page.cursor || (error as { code?: unknown } | null)?.code !== "unsupported-method") { + throw error; + } + const result = await channel.call< + Pick + >("chat.transcript", args); + // The renderer applies its cut to the old agent's unpaged result. + return { + found: result.found === true, + lines: result.lines ?? [], + truncated: result.truncated === true, + }; + } } async subagentTranscript( @@ -291,17 +443,22 @@ export class ChatHost { agent: { agentId?: string; toolUseId?: string }, profileId?: string, ): Promise { + const config = this.configDir(workspaceId, profileId); const channel = await this.channelFor(workspaceId); return channel.call("chat.subagentTranscript", { sessionId, ...agent, - ...this.configDir(profileId), + ...config, }); } async sessionFile(workspaceId: string, sessionId: string, profileId?: string): Promise { + const config = this.configDir(workspaceId, profileId); const channel = await this.channelFor(workspaceId); - return channel.call("chat.sessionFile", { sessionId, ...this.configDir(profileId) }); + return channel.call("chat.sessionFile", { + sessionId, + ...config, + }); } async stageThread( @@ -309,9 +466,9 @@ export class ChatHost { threadId: string, options: { fromProfileId?: string; toProfileId?: string }, ): Promise { + const from = this.profileDir(workspaceId, "codex", options.fromProfileId); + const to = this.profileDir(workspaceId, "codex", options.toProfileId); const channel = await this.channelFor(workspaceId); - const from = this.codexDir(options.fromProfileId); - const to = this.codexDir(options.toProfileId); return channel.call("chat.stageThread", { threadId, ...(from ? { from } : {}), @@ -320,8 +477,9 @@ export class ChatHost { } async claudeConfig(workspaceId: string, profileId?: string): Promise { + const config = this.configDir(workspaceId, profileId); const channel = await this.channelFor(workspaceId); - return channel.call("chat.claudeConfig", this.configDir(profileId)); + return channel.call("chat.claudeConfig", config); } /** Runs one of the `claude` commands a tab may (`claude agents`, `claude plugin`…) in the folder. */ @@ -331,27 +489,29 @@ export class ChatHost { command: ClaudeCommand, profileId?: string, ): Promise<{ code: number; stdout: string; stderr: string; timedOut?: boolean }> { + const profile = this.profile("claude", profileId); const channel = await this.channelFor(workspaceId); - const found = await channel.call<{ path: string; envPath?: string }>("lsp.findBinary", { - name: "claude", - dir: cwd, - }); + const windows = this.windowsHost(workspaceId); + const env = await this.profileEnv(channel, profile, windows); + const found = await this.findBinary(channel, "claude", cwd, env, windows); if (!found.path) throw new Error("claude was not found on the PATH"); - return channel.call("chat.exec", { - cwd, - program: found.path, - args: claudeCommandArgs(command, cwd), - env: { - ...(found.envPath ? { PATH: found.envPath } : {}), - ...(await this.profileEnv(channel, "claude", profileId)), + return channel.call( + "chat.exec", + { + cwd, + program: found.path, + args: claudeCommandArgs(command, cwd), + env: mergeAgentEnv(windows, found.envPath ? { PATH: found.envPath } : {}, env), + timeoutMs: COMMAND_TIMEOUT_MS, }, - timeoutMs: COMMAND_TIMEOUT_MS, - }); + { timeoutMs: COMMAND_REPLY_TIMEOUT_MS }, + ); } async sessions(workspaceId: string, cwd: string, profileId?: string): Promise { + const config = this.configDir(workspaceId, profileId); const channel = await this.channelFor(workspaceId); - return channel.call("chat.sessions", { cwd, ...this.configDir(profileId) }); + return channel.call("chat.sessions", { cwd, ...config }); } async deleteSession( @@ -360,8 +520,13 @@ export class ChatHost { sessionId: string, profileId?: string, ): Promise { + const config = this.configDir(workspaceId, profileId); const channel = await this.channelFor(workspaceId); - return channel.call("chat.deleteSession", { cwd, sessionId, ...this.configDir(profileId) }); + return channel.call("chat.deleteSession", { + cwd, + sessionId, + ...config, + }); } async fileSizes(workspaceId: string, paths: readonly string[]): Promise { @@ -369,77 +534,198 @@ export class ChatHost { return channel.call("chat.fileSizes", { paths }); } - /** The Claude profile's folder, as the agent's readers take it; none for the default. */ - private configDir(profileId: string | undefined): { configDir?: string } { - const profile = profileId ? this.options.profile?.(profileId) : null; - if (!profile || profile.agent !== "claude" || profile.dir.trim() === "") return {}; - return { configDir: profile.dir.trim() }; + /** Only workspaces on this Windows machine use case-insensitive environment names. */ + private windowsHost(workspaceId: string): boolean { + return ( + (process.platform === "win32" || this.options.windowsAgentFiles !== undefined) && + this.options.workspaceManager.isLocal?.(workspaceId) === true + ); } - /** A Codex profile's folder; "" for the default profile, or one gone. */ - private codexDir(profileId: string | undefined): string { - const profile = profileId ? this.options.profile?.(profileId) : null; - return profile?.agent === "codex" ? profile.dir.trim() : ""; + /** Selects the same executable for the probe and for launching the provider. */ + private findBinary( + channel: AgentChannel, + agent: ChatAgent, + cwd: string, + env: Record, + windows: boolean, + ): Promise<{ path: string; envPath?: string }> { + const searchPath = agentEnvValue(env, "PATH", windows); + return channel.call("lsp.findBinary", { + name: agent, + dir: cwd, + ...(searchPath ? { searchPath } : {}), + }); } - /** - * What a terminal opened for a profile runs with in the workspace, as a - * chat of it does: its folder's variable and its own variables. Null for - * a profile there is not. - */ + /** Probes the selected executable in its profile's environment before adding launch flags. */ + private async probeVersion( + channel: AgentChannel, + agent: ChatAgent, + cwd: string, + program: string, + env: Record, + ): Promise { + try { + const run = await channel.call<{ code: number; stdout: string }>("chat.exec", { + cwd, + program, + args: ["--version"], + env, + timeoutMs: VERSION_TIMEOUT_MS, + }); + return run.code === 0 ? parseClaudeVersion(run.stdout) : null; + } catch (error) { + log.warn(`${agent} --version failed: ${(error as Error).message}`); + return null; + } + } + + /** Rejects explicit stale choices and resolves omitted IDs to the stored default. */ + private profile(agent: ChatAgent, profileId: string | undefined): AgentProfile { + const id = profileId ?? defaultProfileId(agent); + const profile = this.options.profile?.(id); + if (profile?.agent === agent) return profile; + if (!profile && id === defaultProfileId(agent)) { + return { id, agent, dir: "", name: "", color: "" }; + } + const error = new AgentProfileSelectionError( + profile || id === defaultProfileId(agent === "claude" ? "codex" : "claude") + ? `Profile ${id} does not belong to ${agent}` + : `Profile ${id} was not found`, + ); + log.warn(error.message); + throw error; + } + + /** Selects the same folder when reading history as when starting a profile. */ + private profileDir(workspaceId: string, agent: ChatAgent, profileId: string | undefined): string { + return profileDirectory(this.profile(agent, profileId), this.windowsHost(workspaceId)); + } + + /** The Claude profile's folder, as the agent's readers take it. */ + private configDir(workspaceId: string, profileId: string | undefined): { configDir?: string } { + const configDir = this.profileDir(workspaceId, "claude", profileId); + return configDir ? { configDir } : {}; + } + + /** A terminal in an agent profile uses the same variables and folder as its chat. */ async terminalProfileEnv( workspaceId: string, - profileId: string, + profileId: string | undefined, + defaultAgent: ChatAgent = "claude", ): Promise | null> { - const profile = this.options.profile?.(profileId); - if (!profile) return null; + const profile = + profileId === undefined + ? this.profile(defaultAgent, undefined) + : this.options.profile?.(profileId); + if (!profile) { + log.warn(`Profile ${profileId} was not found for terminal spawn`); + return null; + } const channel = await this.options.workspaceManager.getAgentChannel(workspaceId); await channel.ready; - return this.profileEnv(channel, profile.agent, profile.id); + return this.profileEnv(channel, profile, this.windowsHost(workspaceId)); } - /** - * What the agent's program runs with for the profile: its folder, made on - * the workspace's host and named by its variable, and its own variables. - * None for the default profile, or one gone. - */ + /** Prepares named profile folders on the workspace host and overlays their directory variable. */ private async profileEnv( channel: AgentChannel, - agent: AgentProfile["agent"], - profileId: string | undefined, + profile: AgentProfile, + windows: boolean, ): Promise> { - const profile = profileId ? this.options.profile?.(profileId) : null; - if (!profile || profile.agent !== agent) return {}; - const env: Record = { ...(profile.env ?? {}) }; + const { agent } = profile; + const key = PROFILE_DIR_ENV[agent]; + const env = profileVariables(profile.env, windows); + let dir = profileDirectory(profile, windows); if (profile.dir.trim() !== "") { const result = await channel.call<{ dir: string; failed?: string[] }>("chat.profileDir", { agent, - dir: profile.dir.trim(), + dir, shared: profile.shared ?? [], shareable: SHAREABLE[agent], }); if (result.failed && result.failed.length > 0) { log.warn(`profile ${profile.id}: could not link ${result.failed.join(", ")}`); } - env[PROFILE_DIR_ENV[agent]] = result.dir; + dir = result.dir; + } + if (!dir) { + return Object.fromEntries( + Object.entries(env).filter(([name]) => (windows ? name.toUpperCase() : name) !== key), + ); } - return env; + return mergeAgentEnv(windows, env, { [key]: dir }); } - /** Forgets a removed workspace. Its agent ends its programs as it shuts down. */ - closeWorkspace(workspaceId: string): void { + /** Closes tracked and restored chats before the workspace's agent is torn down. */ + async closeWorkspace(workspaceId: string, channel: AgentChannel | null): Promise { + const subscription = this.subscriptions.get(workspaceId); + const tabIds = new Set(this.options.knownChatTabs?.(workspaceId)); + for (const tabId of subscription?.processes.keys() ?? []) tabIds.add(tabId); + const prefix = `${workspaceId}:`; + for (const keys of [this.tabIntents.keys(), this.closingTabs.keys()]) { + for (const key of keys) { + if (key.startsWith(prefix)) tabIds.add(key.slice(prefix.length)); + } + } this.workspaces.delete(workspaceId); - this.subscriptions.get(workspaceId)?.dispose(); + subscription?.dispose(); this.subscriptions.delete(workspaceId); + if (tabIds.size === 0) return; + const deadline = Date.now() + WORKSPACE_CHAT_CLOSE_MS; + let timer: ReturnType | undefined; + const expired = new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error("chat.close timed out")), WORKSPACE_CHAT_CLOSE_MS); + }); + const removal = { channel, deadline, expired }; + const results = await Promise.allSettled( + Array.from(tabIds, (tabId) => this.closeTab(workspaceId, tabId, removal)), + ).finally(() => clearTimeout(timer)); + for (const result of results) { + if (result.status === "rejected") + log.warn( + `could not close a chat in removed workspace ${workspaceId}: ${String(result.reason)}`, + ); + } } dispose(): void { if (this.disposed) return; this.disposed = true; + this.oauth.dispose(); this.disposeProviderReady(); for (const subscription of this.subscriptions.values()) subscription.dispose(); this.subscriptions.clear(); this.workspaces.clear(); + this.tabIntents.clear(); + } + + /** Drops exit and result events that belong to a replaced program. */ + private acceptProcessEvent(event: string, payload: Record): boolean { + const { workspaceId, tabId, replay } = payload; + if (typeof workspaceId !== "string" || typeof tabId !== "string") return true; + const processes = this.subscriptions.get(workspaceId)?.processes; + if (!processes) return true; + const current = processes.get(tabId); + const message = payload.message as { event?: unknown; processGeneration?: unknown } | undefined; + if (event === "line" && payload.dir === "sys" && message?.event === "start") { + const startSeq = typeof payload.seq === "number" ? payload.seq : 0; + if (typeof message.processGeneration === "string") { + if (!replay || !current || startSeq > current.startSeq) { + processes.set(tabId, { generation: message.processGeneration, startSeq }); + } + } else if (!replay) { + processes.delete(tabId); + } + return true; + } + return ( + replay === true || + typeof payload.processGeneration !== "string" || + !current || + current.generation === payload.processGeneration + ); } private async channelFor(workspaceId: string): Promise { @@ -450,6 +736,21 @@ export class ChatHost { return channel; } + /** Releases an intent whose start could not reach a workspace agent. */ + private async channelForStart( + workspaceId: string, + key: string, + intent: TabIntent, + ): Promise { + try { + return await this.channelFor(workspaceId); + } catch (error) { + if (this.tabIntents.get(key) === intent && intent.starts.size === 0) + this.tabIntents.delete(key); + throw error; + } + } + /** What the hooks leave untold of a tab's Claude: a turn stopped, the program gone. */ private noteForStatus(event: string, payload: Record): void { const workspaceId = payload.workspaceId as string; @@ -459,13 +760,17 @@ export class ChatHost { this.options.onProgramExit?.(workspaceId, tabId); return; } - if (payload.replay === true || payload.dir !== "out") return; + if (payload.dir !== "out") return; const message = payload.message as Record | null | undefined; - if (message?.type !== "result") return; - const reason = typeof message.terminal_reason === "string" ? message.terminal_reason : ""; - if (message.subtype !== "success" || reason.startsWith("aborted")) { - this.options.onTurnStopped?.(workspaceId, tabId); + if ( + message?.type === "system" && + (message.subtype === "task_started" || message.subtype === "task_notification") + ) { + this.options.onTaskEvent?.(workspaceId, tabId, message); + return; } + if (payload.replay === true || message?.type !== "result") return; + this.options.onTurnResult?.(workspaceId, tabId, claudeTurnResult(message)); } /** Relays one channel's chat events, once per channel. */ @@ -483,33 +788,52 @@ export class ChatHost { ); return; } + const frame = payload as Record; + if (!this.acceptProcessEvent(event, frame)) return; this.options.broadcast(event, payload); - this.noteForStatus(event, payload as Record); + this.noteForStatus(event, frame); }; const offLine = channel.on("chat.line", relay("line")); const offExit = channel.on("chat.exit", relay("exit")); - const offLifecycle = channel.onLifecycle((event) => this.handleLifecycle(workspaceId, event)); - this.subscriptions.set(workspaceId, { + const subscription: Subscription = { channel, + revision: 0, + processes: new Map(), dispose: () => { offLine(); offExit(); offLifecycle(); }, - }); + }; + const offLifecycle = channel.onLifecycle((event) => + this.handleLifecycle(workspaceId, subscription, event), + ); + this.subscriptions.set(workspaceId, subscription); } /** - * A dropped connection holds the tabs; the same agent back (a daemon that - * survived) has them replay what they missed. A new agent comes through - * onProviderReady. + * A dropped connection holds the tabs. A replacement local process needs + * its hook endpoint before the tabs can resume. A surviving daemon can + * replay immediately. */ - private handleLifecycle(workspaceId: string, event: ChannelLifecycleEvent): void { + private handleLifecycle( + workspaceId: string, + subscription: Subscription, + event: ChannelLifecycleEvent, + ): void { switch (event.type) { case "reconnecting": + subscription.revision += 1; + if (this.options.workspaceManager.isLocal?.(workspaceId)) subscription.processes.clear(); this.options.broadcast("held", { workspaceId }); return; case "ready": + if (this.options.workspaceManager.isLocal?.(workspaceId)) { + void this.resyncAfterHooks(workspaceId, subscription); + return; + } + this.options.broadcast("resync", { workspaceId }); + return; // A new daemon behind the same channel: the tabs find it has no logs. case "held-then-expired": this.options.broadcast("resync", { workspaceId }); @@ -524,4 +848,20 @@ export class ChatHost { return; } } + + /** Resyncs only if this ready event still belongs to the live connection. */ + private async resyncAfterHooks(workspaceId: string, subscription: Subscription): Promise { + const { revision, channel } = subscription; + try { + await this.options.workspaceManager.getSpawnHookInfo(workspaceId, channel); + if ( + this.subscriptions.get(workspaceId) === subscription && + revision === subscription.revision + ) { + this.options.broadcast("resync", { workspaceId }); + } + } catch (error) { + log.warn(`chat reconnect failed for ${workspaceId}: ${(error as Error).message}`); + } + } } diff --git a/src/main/features/chat/ipc.ts b/src/main/features/chat/ipc.ts index b72f76c4f..a96e1ef2e 100644 --- a/src/main/features/chat/ipc.ts +++ b/src/main/features/chat/ipc.ts @@ -1,19 +1,34 @@ // Claude tab IPC channel: renderer ↔ main ↔ the workspace's agent. import { ipcContract } from "../../../shared/ipc/contract"; -import { ipcOk } from "../../../shared/ipc/result"; +import { type IpcResult, ipcErr, ipcOk } from "../../../shared/ipc/result"; import { broadcast, register, validateArgs } from "../../infra/ipc-router"; -import { ChatHost, type ChatHostOptions } from "./host"; -import { OAuthCallbacks } from "./oauth-callback"; +import { agentCallError } from "../../infra/ipc-router/agent-errors"; +import { AgentProfileSelectionError, ChatHost, type ChatHostOptions } from "./host"; +import { OAuthCallbacksClosedError } from "./oauth-callback"; const c = ipcContract.chat.call; +/** Surfaces a stale profile as a recoverable error at the IPC boundary. */ +function withProfileError(run: (args: unknown) => Promise>) { + return async (args: unknown): Promise> => { + try { + return await run(args); + } catch (error) { + if (error instanceof AgentProfileSelectionError) { + return ipcErr("not-found", error.message, { category: "failed" }); + } + throw error; + } + }; +} + export function registerChatChannel(options: Omit): ChatHost { const host = new ChatHost({ ...options, broadcast: (event, args) => broadcast("chat", event, args), }); - const oauth = new OAuthCallbacks(); + const { oauth } = host; register("chat", { call: { @@ -39,8 +54,16 @@ export function registerChatChannel(options: Omit) }, send: async (args: unknown) => { const { workspaceId, tabId, message } = validateArgs(c.send.args, args); - await host.send(workspaceId, tabId, message); - return ipcOk(undefined); + try { + await host.send(workspaceId, tabId, message); + return ipcOk(undefined); + } catch (error) { + const code = (error as { code?: unknown } | null)?.code; + const detail = error instanceof Error ? error.message : String(error); + return typeof code === "string" + ? ipcErr(code, detail, { category: "failed" }) + : ipcErr("chat.send-failed", detail, { category: "bug" }); + } }, stop: async (args: unknown) => { await host.call("stop", validateArgs(c.stop.args, args)); @@ -72,7 +95,7 @@ export function registerChatChannel(options: Omit) await host.call("pruneAttachments", validateArgs(c.pruneAttachments.args, args)); return ipcOk(undefined); }, - transcript: async (args: unknown) => { + transcript: withProfileError(async (args: unknown) => { const { workspaceId, sessionId, profileId } = validateArgs(c.transcript.args, args); const result = (await host.transcript(workspaceId, sessionId, profileId)) as { found?: boolean; @@ -84,8 +107,27 @@ export function registerChatChannel(options: Omit) lines: result.lines ?? [], truncated: result.truncated === true, }); - }, - subagentTranscript: async (args: unknown) => { + }), + transcriptPage: withProfileError(async (args: unknown) => { + const { workspaceId, sessionId, profileId, ...page } = validateArgs( + c.transcriptPage.args, + args, + ); + try { + return ipcOk(await host.transcriptPage(workspaceId, sessionId, profileId, page)); + } catch (error) { + const code = (error as { code?: unknown } | null)?.code; + if (code === "chat.transcript-cursor-stale") { + return ipcErr(code, "The chat history changed. Try reading earlier messages again.", { + category: "failed", + }); + } + const failure = agentCallError(error); + if (failure) return failure; + throw error; + } + }), + subagentTranscript: withProfileError(async (args: unknown) => { const { workspaceId, sessionId, agentId, toolUseId, profileId } = validateArgs( c.subagentTranscript.args, args, @@ -101,8 +143,8 @@ export function registerChatChannel(options: Omit) lines: result.lines ?? [], truncated: result.truncated === true, }); - }, - sessionFile: async (args: unknown) => { + }), + sessionFile: withProfileError(async (args: unknown) => { const { workspaceId, sessionId, profileId } = validateArgs(c.sessionFile.args, args); const result = (await host.sessionFile(workspaceId, sessionId, profileId)) as { found?: boolean; @@ -113,15 +155,15 @@ export function registerChatChannel(options: Omit) ? { found: true, path: result.path } : { found: false }, ); - }, - stageThread: async (args: unknown) => { + }), + stageThread: withProfileError(async (args: unknown) => { const { workspaceId, threadId, ...options } = validateArgs(c.stageThread.args, args); const result = (await host.stageThread(workspaceId, threadId, options)) as { found?: boolean; }; return ipcOk({ found: result.found === true }); - }, - claudeConfig: async (args: unknown) => { + }), + claudeConfig: withProfileError(async (args: unknown) => { const { workspaceId, profileId } = validateArgs(c.claudeConfig.args, args); const result = (await host.claudeConfig(workspaceId, profileId)) as { found?: boolean; @@ -131,8 +173,18 @@ export function registerChatChannel(options: Omit) return ipcOk( validateArgs(c.claudeConfig.result, { found: true, values: result.values ?? {} }), ); + }), + oauthListen: async () => { + try { + return ipcOk(await oauth.listen()); + } catch (error) { + if (error instanceof OAuthCallbacksClosedError) { + return ipcErr("chat.oauth-closed", error.message, { category: "cancelled" }); + } + const message = error instanceof Error ? error.message : String(error); + return ipcErr("chat.oauth-listen-failed", message, { category: "failed" }); + } }, - oauthListen: async () => ipcOk(await oauth.listen()), oauthWait: async (args: unknown) => { const { id } = validateArgs(c.oauthWait.args, args); return ipcOk({ url: await oauth.wait(id) }); @@ -141,24 +193,24 @@ export function registerChatChannel(options: Omit) oauth.cancel(validateArgs(c.oauthCancel.args, args).id); return ipcOk(undefined); }, - cli: async (args: unknown) => { + cli: withProfileError(async (args: unknown) => { const { workspaceId, cwd, command, profileId } = validateArgs(c.cli.args, args); return ipcOk(await host.cli(workspaceId, cwd, command, profileId)); - }, - sessions: async (args: unknown) => { + }), + sessions: withProfileError(async (args: unknown) => { const { workspaceId, cwd, profileId } = validateArgs(c.sessions.args, args); const result = (await host.sessions(workspaceId, cwd, profileId)) as { sessions?: unknown; }; return ipcOk(validateArgs(c.sessions.result, result.sessions ?? [])); - }, - deleteSession: async (args: unknown) => { + }), + deleteSession: withProfileError(async (args: unknown) => { const { workspaceId, cwd, sessionId, profileId } = validateArgs(c.deleteSession.args, args); const result = (await host.deleteSession(workspaceId, cwd, sessionId, profileId)) as { deleted?: unknown; }; return ipcOk({ deleted: result.deleted === true }); - }, + }), fileSizes: async (args: unknown) => { const { workspaceId, paths } = validateArgs(c.fileSizes.args, args); const result = (await host.fileSizes(workspaceId, paths)) as { sizes?: unknown }; diff --git a/src/main/features/chat/launch.ts b/src/main/features/chat/launch.ts index 3e39e4fca..2f7235d62 100644 --- a/src/main/features/chat/launch.ts +++ b/src/main/features/chat/launch.ts @@ -254,8 +254,14 @@ export function claudeCommandArgs(command: ClaudeCommand, cwd: string): string[] * of `codex --version`'s ("codex-cli 0.166.0"); null without one. */ export function parseClaudeVersion(output: string): string | null { - const match = /(\d+)\.(\d+)\.(\d+)/.exec(output); - return match ? `${match[1]}.${match[2]}.${match[3]}` : null; + for (const line of output.split("\n")) { + const match = + /^(?:(?:codex(?:-cli)?|claude(?:-code)?)\s+)?(\d+\.\d+\.\d+)(?:[-+][\w.+-]+)?(?:\s+\(Claude Code\))?$/i.exec( + line.trim(), + ); + if (match) return match[1] ?? null; + } + return null; } /** Joins a directory and a file name on the workspace's host. */ diff --git a/src/main/features/chat/oauth-callback.ts b/src/main/features/chat/oauth-callback.ts index 90b362be2..f8f03ea79 100644 --- a/src/main/features/chat/oauth-callback.ts +++ b/src/main/features/chat/oauth-callback.ts @@ -31,14 +31,26 @@ interface Pending { /** How long an address the browser came back to is kept for a wait that comes late. */ const KEPT_MS = 60_000; +/** Indicates a sign-in that was cancelled by chat host shutdown. */ +export class OAuthCallbacksClosedError extends Error { + constructor() { + super("OAuth callbacks are closed"); + } +} + export class OAuthCallbacks { private readonly pending = new Map(); - private readonly arrived = new Map(); + private readonly arrived = new Map< + string, + { url: string; timer: ReturnType } + >(); + private disposed = false; constructor(private readonly timeoutMs = OAUTH_CALLBACK_TIMEOUT_MS) {} /** Serves a page for one sign-in; its address is the redirect to give. */ async listen(): Promise<{ id: string; redirectUri: string }> { + if (this.disposed) throw new OAuthCallbacksClosedError(); const id = randomUUID(); const server = createServer((req, res) => { const url = new URL(req.url ?? "/", "http://localhost"); @@ -58,6 +70,10 @@ export class OAuthCallbacks { server.once("error", reject); server.listen(0, "127.0.0.1", () => resolve()); }); + if (this.disposed) { + server.close(); + throw new OAuthCallbacksClosedError(); + } const timer = setTimeout(() => this.finish(id, null), this.timeoutMs); timer.unref?.(); this.pending.set(id, { server, timer, waiters: [] }); @@ -68,7 +84,7 @@ export class OAuthCallbacks { /** The address the browser came back to; null when it timed out, was cancelled or is unknown. */ wait(id: string): Promise { const arrived = this.arrived.get(id); - if (arrived !== undefined) return Promise.resolve(arrived); + if (arrived !== undefined) return Promise.resolve(arrived.url); const entry = this.pending.get(id); if (!entry) return Promise.resolve(null); return new Promise((resolve) => entry.waiters.push(resolve)); @@ -79,8 +95,11 @@ export class OAuthCallbacks { this.finish(id, null); } + /** Stops pending sign-ins and drops callbacks saved for a late waiter. */ dispose(): void { + this.disposed = true; for (const id of [...this.pending.keys()]) this.finish(id, null); + for (const { timer } of this.arrived.values()) clearTimeout(timer); this.arrived.clear(); } @@ -92,8 +111,9 @@ export class OAuthCallbacks { // The page was answered first; the server stops taking new connections. entry.server.close(); if (url !== null) { - this.arrived.set(id, url); - setTimeout(() => this.arrived.delete(id), KEPT_MS).unref?.(); + const timer = setTimeout(() => this.arrived.delete(id), KEPT_MS); + timer.unref?.(); + this.arrived.set(id, { url, timer }); } for (const waiter of entry.waiters.splice(0)) waiter(url); } diff --git a/src/main/features/claude/hook-handler.ts b/src/main/features/claude/hook-handler.ts index c16b80be7..66a44764e 100644 --- a/src/main/features/claude/hook-handler.ts +++ b/src/main/features/claude/hook-handler.ts @@ -103,9 +103,9 @@ export interface HookHandlerDeps { */ broadcastFn?: (channel: string, event: string, args: unknown) => void; /** - * Workspace activation callback — called when a notification is clicked. + * Connects the workspace so the clicked notification can reveal its tab. */ - activateWorkspace?: (workspaceId: string) => Promise; + connectWorkspace?: (workspaceId: string) => Promise; /** App window focus callback — called when a notification is clicked. */ focusMainWindow?: () => void; /** @@ -321,15 +321,17 @@ const pendingNotifications = new Set(); function trackNotification(n: import("electron").Notification): void { pendingNotifications.add(n); const cleanup = () => { + clearTimeout(timer); pendingNotifications.delete(n); }; + // Release the reference even if the OS tells us neither event. + const timer = setTimeout(cleanup, 5 * 60 * 1000); + timer.unref?.(); // close — when the OS dismisses the notification or the user closes it. // ts: Notification is an EventEmitter, so the `on` call signature is loose. (n as unknown as { on(event: string, cb: () => void): void }).on("close", cleanup); // When click fires, clean up together just before close (event order does not matter). (n as unknown as { on(event: string, cb: () => void): void }).on("click", cleanup); - // Safety timeout — release the reference after 5 minutes even if neither event arrives. - setTimeout(cleanup, 5 * 60 * 1000).unref?.(); } /** @@ -362,8 +364,8 @@ function fireOsNotification( const n = new NotificationCtor({ title, body }); n.on("click", () => { deps.focusMainWindow?.(); - deps.activateWorkspace?.(workspaceId)?.catch((err: unknown) => { - log.warn(`activateWorkspace failed: ${(err as Error).message}`); + deps.connectWorkspace?.(workspaceId)?.catch((err: unknown) => { + log.warn(`connectWorkspace failed: ${(err as Error).message}`); }); broadcastFn("pty", "notificationClick", { workspaceId, tabId }); }); @@ -686,7 +688,8 @@ async function handleHookEvent( } default: - // Unknown subcommand — ignore (a Claude Code update may add new events). + // Future hooks use Claude's native fallback without waiting for the socket deadline. + await respondHook(deps.channelProvider, workspaceId, hookId, { exitCode: 0 }); break; } @@ -816,9 +819,11 @@ export async function answerHook( * Called once from setupClaudeFeature. * The return value is the unsubscribe function. */ -export function registerHookHandler(deps: HookHandlerDeps): () => void { - const turns = new TurnOrder(); - const dialogs = new PermissionDialogs(); +export function registerHookHandler( + deps: HookHandlerDeps, + turns = new TurnOrder(), + dialogs = new PermissionDialogs(), +): () => void { const offHook = deps.agentHost.on("claude.hook", (payload) => { handleHookEvent(payload, deps, turns, dialogs).catch((err: unknown) => { log.warn(`handleHookEvent error: ${(err as Error).message}`); diff --git a/src/main/features/claude/idle-title.ts b/src/main/features/claude/idle-title.ts new file mode 100644 index 000000000..5cb5bb287 --- /dev/null +++ b/src/main/features/claude/idle-title.ts @@ -0,0 +1,18 @@ +/** The mark Claude Code puts at the start of its title between turns. */ +const IDLE_MARK = "✳"; + +// biome-ignore lint/suspicious/noControlCharactersInRegex: OSC sequences start with ESC +const OSC_TITLE_START = /\x1b\][02];/g; + +/** Whether the title beginning at offset has Claude's idle mark. */ +export function isClaudeIdleTitle(title: string | undefined, offset = 0): boolean { + return title?.startsWith(IDLE_MARK, offset) ?? false; +} + +/** Whether output contains an idle OSC title prefix, possibly still open. */ +export function showsClaudeIdleTitle(output: string): boolean { + for (const start of output.matchAll(OSC_TITLE_START)) { + if (isClaudeIdleTitle(output, start.index + start[0].length)) return true; + } + return false; +} diff --git a/src/main/features/claude/index.ts b/src/main/features/claude/index.ts index 71f99b82c..97470fa9e 100644 --- a/src/main/features/claude/index.ts +++ b/src/main/features/claude/index.ts @@ -2,6 +2,7 @@ // // setupClaudeFeature() is called after app.whenReady() in src/main/index.ts. +import type { ClaudeTurnResult } from "../../../shared/claude/turn-result"; import { broadcast } from "../../infra/ipc-router"; import type { PtyHostHandle } from "../pty/types"; import { getActiveAppWindow, getFocusedAppWindow } from "../window/app-windows"; @@ -12,9 +13,11 @@ import { answerHook, PRINT_STOP_ANSWER, registerHookHandler } from "./hook-handl import { ClaudeInterruptWatcher } from "./interrupt"; import { registerClaudeChannel } from "./ipc"; import { answersDialog, PermissionAsks } from "./permission-asks"; +import { MAIN_AGENT, PermissionDialogs } from "./permission-dialogs"; import { ClaudeReconnectCheck } from "./reconnect"; import { SessionActivityLog } from "./session-activity"; import { ClaudeStatusBroker } from "./status"; +import { TurnOrder } from "./turn-order"; // --------------------------------------------------------------------------- // Dependency types @@ -28,7 +31,7 @@ export interface SetupClaudeFeatureOptions { tryGetAgentChannel( id: string, ): Promise; - activate(id: string): Promise; + connect(id: string): Promise; }; /** BrowserWindow.getFocusedWindow — for test injection. */ getFocusedWindow?: () => import("electron").BrowserWindow | null; @@ -56,7 +59,7 @@ export interface SetupClaudeFeatureResult { * A Claude tab's turn ended short: interrupted, or failed. No hook says so, * and the tab's program tells it in a result line. */ - noteTurnStopped: (workspaceId: string, tabId: string) => void; + noteTurnResult: (workspaceId: string, tabId: string, result: ClaudeTurnResult) => void; /** A Claude tab's program exited, maybe without its SessionEnd hook. */ noteProgramExit: (workspaceId: string, tabId: string) => void; /** Function that releases every subscription. */ @@ -117,29 +120,35 @@ export function setupClaudeFeature(options: SetupClaudeFeatureOptions): SetupCla // 3. Register hook-handler. const getFocusedWindow = options.getFocusedWindow ?? getFocusedAppWindow; + const turns = new TurnOrder(); + const dialogs = new PermissionDialogs(); - const offHook = registerHookHandler({ - broker, - activeContext, - activity, - asks, - chatAnswers, - agentHost, - channelProvider: workspaceManager, - workspaceManager, - getFocusedWindow, - electronNotificationCtor: options.electronNotificationCtor, - broadcastFn, - notificationsEnabled: options.notificationsEnabled, - activateWorkspace: (id) => workspaceManager.activate(id), - focusMainWindow: () => { - const win = getActiveAppWindow(); - if (!win) return; - if (win.isMinimized()) win.restore(); - win.show(); - win.focus(); + const offHook = registerHookHandler( + { + broker, + activeContext, + activity, + asks, + chatAnswers, + agentHost, + channelProvider: workspaceManager, + workspaceManager, + getFocusedWindow, + electronNotificationCtor: options.electronNotificationCtor, + broadcastFn, + notificationsEnabled: options.notificationsEnabled, + connectWorkspace: (id) => workspaceManager.connect(id), + focusMainWindow: () => { + const win = getActiveAppWindow(); + if (!win) return; + if (win.isMinimized()) win.restore(); + win.show(); + win.focus(); + }, }, - }); + turns, + dialogs, + ); // Claude gone without its SessionEnd hook (its exit seen, its state cleared // by hand) takes its record along: the agents it ran stopped with it. @@ -166,15 +175,22 @@ export function setupClaudeFeature(options: SetupClaudeFeatureOptions): SetupCla return name === "" ? null : name; }); + /** Clears the live hook state when either a chat program or a terminal exits. */ + const noteProgramExit = (workspaceId: string, tabId: string): void => { + turns.end(workspaceId, tabId); + dialogs.clear(`${workspaceId}:${tabId}`); + broker.clear(workspaceId, tabId); + activity.clear(workspaceId, tabId); + asks.clear(workspaceId, tabId); + }; + // 4. broker.clear on PTY exit — remove the status entry when a tab closes (prevents a memory leak). const offExit = agentHost.on("exit", (args) => { const a = args as Record; const workspaceId = typeof a?.workspaceId === "string" ? a.workspaceId : null; const tabId = typeof a?.tabId === "string" ? a.tabId : null; if (workspaceId && tabId) { - broker.clear(workspaceId, tabId); - activity.clear(workspaceId, tabId); - asks.clear(workspaceId, tabId); + noteProgramExit(workspaceId, tabId); interrupts.forget(workspaceId, tabId); reconnects.forget(workspaceId, tabId); } @@ -212,17 +228,34 @@ export function setupClaudeFeature(options: SetupClaudeFeatureOptions): SetupCla // The dialog answered in the terminal no longer waits on the app. if (answersDialog(data)) asks.answeredInTerminal(workspaceId, tabId); }, - noteTurnStopped: (workspaceId, tabId) => { - const status = broker.get(workspaceId, tabId)?.status; - if (status === "running" || status === "permissionPending") { - broker.set(workspaceId, tabId, "idle"); + noteTurnResult: (workspaceId, tabId, result) => { + if (!turns.acceptsResult(workspaceId, tabId, result.promptId)) return; + const key = `${workspaceId}:${tabId}`; + dialogs.closeAgent(key, MAIN_AGENT); + asks.retain(workspaceId, tabId, (agent) => agent !== MAIN_AGENT); + const focused = getFocusedWindow(); + const viewing = + focused !== null && !focused.isMinimized() && activeContext.isActive(workspaceId, tabId); + const status = + result.outcome === "error" + ? "error" + : result.outcome === "interrupted" || viewing + ? "idle" + : "completed"; + const message = result.outcome === "interrupted" ? undefined : result.detail?.slice(0, 500); + if (dialogs.isOpen(key)) { + dialogs.setUnder(key, { status, message }); + } else { + const previous = broker.get(workspaceId, tabId); + broker.set( + workspaceId, + tabId, + status, + message ?? (status === previous?.status ? previous.message : undefined), + ); } }, - noteProgramExit: (workspaceId, tabId) => { - broker.clear(workspaceId, tabId); - activity.clear(workspaceId, tabId); - asks.clear(workspaceId, tabId); - }, + noteProgramExit, dispose: () => { offHook(); offCleared(); @@ -230,6 +263,7 @@ export function setupClaudeFeature(options: SetupClaudeFeatureOptions): SetupCla offData(); offRestored(); offReplayed(); + reconnects.dispose(); }, broker, activeContext, diff --git a/src/main/features/claude/interrupt.ts b/src/main/features/claude/interrupt.ts index 787db4a18..500b56563 100644 --- a/src/main/features/claude/interrupt.ts +++ b/src/main/features/claude/interrupt.ts @@ -12,6 +12,7 @@ * The dialog's title already shows the idle mark, so there only the line * counts. */ +import { showsClaudeIdleTitle } from "./idle-title"; import type { ClaudeStatusBroker } from "./status"; /** How long after the key Claude's sign counts. */ @@ -23,10 +24,6 @@ const INTERRUPTIBLE = new Set(["running", "permissionPending"]); /** The end of the output kept, since a chunk can end inside a title. */ const TAIL_CHARS = 64; -/** A terminal title (OSC 0 or 2) starting with the ✳ Claude shows between turns. */ -// biome-ignore lint/suspicious/noControlCharactersInRegex: OSC sequences start with ESC -const IDLE_TITLE = /\x1b\][02];✳/; - /** Esc alone is the key; longer input starting with it is an escape sequence. */ function isInterruptKey(data: string): boolean { return data === "\x1b" || data.includes("\x03"); @@ -219,7 +216,7 @@ export class ClaudeInterruptWatcher { private showsTurnEnded(armed: Armed, chunk: string): boolean { const output = armed.tail + chunk; armed.tail = output.slice(-TAIL_CHARS); - if (armed.byTitle && IDLE_TITLE.test(output)) return true; + if (armed.byTitle && showsClaudeIdleTitle(output)) return true; let shown = false; armed.rows.read(chunk, (row) => { shown ||= INTERRUPTED_LINES.some((line) => rowShows(row, line)); diff --git a/src/main/features/claude/reconnect.ts b/src/main/features/claude/reconnect.ts index e98e4fa0a..e3ec55068 100644 --- a/src/main/features/claude/reconnect.ts +++ b/src/main/features/claude/reconnect.ts @@ -15,11 +15,9 @@ * and only on such a sign, so it never flickers while the replay comes in. */ import { isLoginShell } from "../../../shared/pty/login-shell"; +import { isClaudeIdleTitle } from "./idle-title"; import type { ClaudeStatusBroker } from "./status"; -/** The mark Claude Code puts at the start of its title between turns. */ -const IDLE_MARK = "✳"; - /** A terminal title, set with OSC 0 or 2 and ended by BEL or ST. */ // biome-ignore lint/suspicious/noControlCharactersInRegex: OSC sequences start with ESC const TITLE = /\x1b\][02];([^\x07\x1b]*)(?:\x07|\x1b\\)/g; @@ -29,8 +27,8 @@ const MAX_CARRY = 1024; interface Restoring { /** The state when the terminal came back, to see whether a hook changed it since. */ - readonly status: string; - readonly since: number; + readonly entry: NonNullable>; + checking?: boolean; /** The last title in the replayed output so far. */ title: string | undefined; /** A sequence the last chunk ended inside. */ @@ -58,8 +56,7 @@ export class ClaudeReconnectCheck { return; } this.restoring.set(key, { - status: entry.status, - since: entry.since, + entry, title: undefined, carry: "", }); @@ -68,7 +65,7 @@ export class ClaudeReconnectCheck { /** Output replayed to a terminal. */ noteReplay(workspaceId: string, tabId: string, chunk: string): void { const restoring = this.restoring.get(`${workspaceId}:${tabId}`); - if (restoring === undefined) return; + if (!restoring || restoring.checking) return; const text = restoring.carry + chunk; let end = 0; for (const match of text.matchAll(TITLE)) { @@ -84,18 +81,22 @@ export class ClaudeReconnectCheck { async noteReplayed(workspaceId: string, tabId: string): Promise { const key = `${workspaceId}:${tabId}`; const restoring = this.restoring.get(key); - if (restoring === undefined) return; - this.restoring.delete(key); - if (!this.unchanged(workspaceId, tabId, restoring)) return; - + if (!restoring || restoring.checking) return; + if (this.broker.get(workspaceId, tabId) !== restoring.entry) { + this.restoring.delete(key); + return; + } + restoring.checking = true; const program = await this.foregroundProcess(workspaceId, tabId).catch(() => null); - // A hook may have come in meanwhile, and it knows better. - if (!this.unchanged(workspaceId, tabId, restoring)) return; + if (this.restoring.get(key) !== restoring) return; + this.restoring.delete(key); + // A new prompt can replace the entry while keeping both its status and its `since` time. + if (this.broker.get(workspaceId, tabId) !== restoring.entry) return; if (program !== null && isLoginShell(program)) { this.broker.clear(workspaceId, tabId); return; } - if (restoring.status === "running" && restoring.title?.startsWith(IDLE_MARK)) { + if (restoring.entry.status === "running" && isClaudeIdleTitle(restoring.title)) { this.broker.set(workspaceId, tabId, "needsInput"); } } @@ -105,8 +106,8 @@ export class ClaudeReconnectCheck { this.restoring.delete(`${workspaceId}:${tabId}`); } - private unchanged(workspaceId: string, tabId: string, restoring: Restoring): boolean { - const entry = this.broker.get(workspaceId, tabId); - return entry !== null && entry.status === restoring.status && entry.since === restoring.since; + /** Invalidates outstanding terminal replay checks as the feature shuts down. */ + dispose(): void { + this.restoring.clear(); } } diff --git a/src/main/features/claude/session-activity.ts b/src/main/features/claude/session-activity.ts index db98da8db..6f106c18b 100644 --- a/src/main/features/claude/session-activity.ts +++ b/src/main/features/claude/session-activity.ts @@ -33,8 +33,11 @@ import { /** The tools that start a subagent: "Agent", "Task" before Claude Code 2.1. */ const AGENT_TOOLS = new Set(["Agent", "Task"]); +const STOP_TASK_TOOLS = new Set(["TaskStop", "KillShell", "KillBash"]); /** How many Agent tool calls wait for their agent at most; a call whose agent never starts is dropped. */ const AGENT_CALLS_KEPT = 16; +/** Bounds the task links kept while stream lines and async hooks arrive. */ +const TASK_LINKS_KEPT = 128; /** The tools that run a command, which may start a `claude -p` run. */ const COMMAND_TOOLS = new Set(["Bash", "PowerShell"]); /** The tools that write files, and the field naming the file. */ @@ -52,6 +55,17 @@ interface AgentCall { readonly prompt?: string; readonly background?: boolean; readonly model?: string; + readonly launchToolUseId?: string; + readonly outputFile?: string; +} + +type TaskEnd = "completed" | "failed" | "stopped"; + +interface TaskLink { + toolUseId?: string; + outputFile?: string; + runId?: string; + state?: TaskEnd; } interface AgentEntry extends Omit { @@ -64,6 +78,8 @@ interface AgentEntry extends Omit { watch?: string; /** The ids of the messages a subagent wrote that it has: each is kept once. */ said?: Set; + launchToolUseId?: string; + outputFile?: string; } /** A message a subagent wrote, as hookclient read it from its transcript. */ @@ -88,6 +104,7 @@ interface SessionEntry { calls: AgentCall[]; /** Agent tool calls whose agent's id the tool's answer told. */ callsByAgent: Map; + taskLinks: Map; /** The `claude -p` runs that went on an earlier run, by run id: that run's agent id. */ runs: Map; } @@ -170,16 +187,59 @@ export class SessionActivityLog { const agent = this.agentOf(session, agentId, p); const answer = text(p.last_assistant_message); if (answer !== undefined) agent.answer = answer; - agent.end = this.now(); - agent.asking = undefined; - this.endOpenTools(agent.tools); - this.setAgentState(session, agent, "completed", true); + this.finishAgent(session, agent, "completed"); this.pruneAgents(session); } break; } } + /** Correlates a stream task with its subagent through the Agent call or output file. */ + noteTask(workspaceId: string, tabId: string, message: Payload): void { + const taskId = text(message.task_id); + const type = text(message.task_type); + const state = taskEndOf(message.status); + if ( + !taskId || + (type && type !== "local_agent") || + (message.subtype !== "task_started" && !state) + ) + return; + const existing = this.sessions.get(`${workspaceId}:${tabId}`); + if (!existing && type !== "local_agent") return; + const session = existing ?? this.sessionOf(workspaceId, tabId); + const sessionId = text(message.session_id); + if (sessionId && session.sessionId && sessionId !== session.sessionId) return; + session.sessionId ??= sessionId; + const runId = text(message.run_id); + const previous = session.taskLinks.get(taskId); + const differentRun = runId && previous?.runId && runId !== previous.runId; + if (differentRun && message.subtype !== "task_started") return; + const link = differentRun ? undefined : previous; + this.rememberTask(session, taskId, { + toolUseId: text(message.tool_use_id) ?? link?.toolUseId, + outputFile: text(message.output_file) ?? link?.outputFile, + runId: runId ?? link?.runId, + state: state ?? link?.state, + }); + } + + /** Keeps a recent task update and applies it when its Agent result is known. */ + private rememberTask(session: SessionEntry, taskId: string, link: TaskLink): void { + session.taskLinks.delete(taskId); + session.taskLinks.set(taskId, link); + if (session.taskLinks.size > TASK_LINKS_KEPT) { + const oldest = session.taskLinks.keys().next().value; + if (oldest !== undefined) session.taskLinks.delete(oldest); + } + if (!link.state) return; + const agent = [...session.agents.values()].find((agent) => matchesTask(agent, taskId, link)); + if (agent) { + this.finishAgent(session, agent, link.state); + this.pruneAgents(session); + } + } + /** * Records what a `claude -p` run started inside the session in `tabId` * tells of itself (hookclient's print-run): its start, claude's start, the @@ -395,6 +455,7 @@ export class SessionActivityLog { agents: new Map(), calls: [], callsByAgent: new Map(), + taskLinks: new Map(), runs: new Map(), }; this.sessions.set(key, session); @@ -426,7 +487,8 @@ export class SessionActivityLog { const known = session.agents.get(agentId); if (known) return known; const type = text(p.agent_type) ?? "general-purpose"; - const call = session.callsByAgent.get(agentId) ?? takeCall(session.calls, type); + const linkedCall = session.callsByAgent.get(agentId); + const call = linkedCall ?? takeCall(session.calls, type); session.callsByAgent.delete(agentId); const agent: AgentEntry = { id: agentId, @@ -441,9 +503,12 @@ export class SessionActivityLog { tools: [], toolCount: 0, failedCount: 0, + launchToolUseId: linkedCall?.launchToolUseId, + outputFile: linkedCall?.outputFile, }; session.agents.set(agentId, agent); this.emit(session, agent); + this.finishKnownTask(session, agent); return agent; } @@ -606,7 +671,14 @@ export class SessionActivityLog { const shown = relative(file, session.cwd); if (!session.files.includes(shown)) session.files.push(shown); } - if (AGENT_TOOLS.has(name)) this.agentToolAnswered(session, input, response); + if (AGENT_TOOLS.has(name)) this.agentToolAnswered(session, input, response, id); + if (STOP_TASK_TOOLS.has(name)) { + const taskId = text(response.task_id) ?? text(input.task_id) ?? text(input.shell_id); + const type = text(response.task_type); + if (taskId && (!type || type === "local_agent")) { + this.rememberTask(session, taskId, { ...session.taskLinks.get(taskId), state: "stopped" }); + } + } if (name === "TaskCreate") { const task = record(response.task); const subject = text(input.subject) ?? text(task.subject); @@ -628,20 +700,53 @@ export class SessionActivityLog { * The Agent tool answered: a background agent's answer names it, before * it starts. Its call is kept for it by that id. */ - private agentToolAnswered(session: SessionEntry, input: Payload, response: Payload): void { + private agentToolAnswered( + session: SessionEntry, + input: Payload, + response: Payload, + toolUseId: string | undefined, + ): void { const agentId = text(response.agentId); if (agentId === undefined) return; const model = text(response.resolvedModel); + const background = response.status === "async_launched"; + const outputFile = text(response.outputFile); const started = session.agents.get(agentId); if (started) { // A foreground agent the user sent to the background. - if (response.status === "async_launched") started.background = true; + if (background) started.background = true; if (model !== undefined) started.model = model; + if (toolUseId) started.launchToolUseId = toolUseId; + if (outputFile) started.outputFile = outputFile; + this.finishKnownTask(session, started); return; } const type = text(input.subagent_type) ?? "general-purpose"; const call = takeCall(session.calls, type, text(input.description)); - if (call) session.callsByAgent.set(agentId, model ? { ...call, model } : call); + if (call || background) { + session.callsByAgent.set(agentId, { + ...call, + type, + model: model ?? call?.model, + background: background || call?.background, + launchToolUseId: toolUseId, + outputFile, + }); + if (session.callsByAgent.size > AGENT_CALLS_KEPT) { + const oldest = session.callsByAgent.keys().next().value; + if (oldest !== undefined) session.callsByAgent.delete(oldest); + } + } + } + + /** Applies an end received before the Agent tool result or SubagentStart. */ + private finishKnownTask(session: SessionEntry, agent: AgentEntry): void { + for (const [id, link] of session.taskLinks) { + if (!link.state || !matchesTask(agent, id, link)) continue; + this.finishAgent(session, agent, link.state); + this.pruneAgents(session); + return; + } } private setAgentState( @@ -690,11 +795,17 @@ export class SessionActivityLog { } private stopAgent(session: SessionEntry, agent: AgentEntry): void { + this.finishAgent(session, agent, "stopped"); + } + + /** Ends a run once, so a late completion hook cannot undo a stop. */ + private finishAgent(session: SessionEntry, agent: AgentEntry, state: TaskEnd): void { + if (agent.end !== undefined) return; agent.end = this.now(); agent.asking = undefined; agent.watch = undefined; this.endOpenTools(agent.tools); - this.setAgentState(session, agent, "stopped", true); + this.setAgentState(session, agent, state, true); } /** Keeps the running agents and the latest ended ones. */ @@ -709,6 +820,20 @@ export class SessionActivityLog { } } +/** Matches explicit IDs first, with an equal ID fallback for older background agents. */ +function matchesTask(agent: AgentEntry, taskId: string, link: TaskLink): boolean { + if (agent.kind !== "subagent") return false; + if (agent.launchToolUseId && link.toolUseId) return agent.launchToolUseId === link.toolUseId; + if (agent.outputFile && link.outputFile) return agent.outputFile === link.outputFile; + return agent.background === true && agent.id === taskId; +} + +/** Normalizes the terminal statuses reported by current and older Claude builds. */ +function taskEndOf(status: unknown): TaskEnd | undefined { + if (status === "completed" || status === "failed" || status === "stopped") return status; + return status === "killed" ? "stopped" : undefined; +} + function snapshotOf(session: SessionEntry): SessionActivity { const agents = [...session.agents.values()]; const running = agents.filter((a) => a.end === undefined).sort((a, b) => a.start - b.start); @@ -730,7 +855,17 @@ function snapshotOf(session: SessionEntry): SessionActivity { } function agentRunOf(agent: AgentEntry): AgentRun { - const { asking: _asking, cwd: _cwd, watch: _watch, said: _said, tools, messages, ...run } = agent; + const { + asking: _asking, + cwd: _cwd, + watch: _watch, + said: _said, + launchToolUseId: _launch, + outputFile: _file, + tools, + messages, + ...run + } = agent; return { ...run, tools: tools.map((tool) => ({ ...tool })), diff --git a/src/main/features/claude/turn-order.ts b/src/main/features/claude/turn-order.ts index 36977660c..93915f039 100644 --- a/src/main/features/claude/turn-order.ts +++ b/src/main/features/claude/turn-order.ts @@ -9,6 +9,8 @@ /** How many finished turns a tab remembers; late frames come within seconds. */ const PAST_TURNS_KEPT = 16; +/** Keeps the late-hook guard bounded even when every session used a different tab. */ +const ENDED_TABS_KEPT = 512; interface TabTurns { /** The prompt id of the turn under way, if any frame has told it yet. */ @@ -19,6 +21,7 @@ interface TabTurns { export class TurnOrder { private readonly tabs = new Map(); + private readonly ended = new Set(); /** * Records a frame and says whether it belongs to a turn that is already @@ -35,6 +38,7 @@ export class TurnOrder { const key = `${workspaceId}:${tabId}`; let tab = this.tabs.get(key); if (tab?.past.includes(promptId)) return true; + this.ended.delete(key); if (!tab) { tab = { past: [] }; this.tabs.set(key, tab); @@ -51,10 +55,26 @@ export class TurnOrder { return false; } + /** An identified stream result must belong to the current prompt. */ + acceptsResult(workspaceId: string, tabId: string, promptId: string | undefined): boolean { + if (!promptId) return true; + const tab = this.tabs.get(`${workspaceId}:${tabId}`); + return !tab?.past.includes(promptId) && (!tab?.current || tab.current === promptId); + } + /** The session ended: frames of its last turn that still come are stale. */ end(workspaceId: string, tabId: string): void { - const tab = this.tabs.get(`${workspaceId}:${tabId}`); - if (tab) this.retire(tab); + const key = `${workspaceId}:${tabId}`; + const tab = this.tabs.get(key); + if (!tab || this.ended.has(key)) return; + this.retire(tab); + this.ended.add(key); + if (this.ended.size <= ENDED_TABS_KEPT) return; + const oldest = this.ended.values().next().value; + if (oldest !== undefined) { + this.ended.delete(oldest); + this.tabs.delete(oldest); + } } private retire(tab: TabTurns): void { diff --git a/src/main/features/cli/args.ts b/src/main/features/cli/args.ts index d7462bced..3719920df 100644 --- a/src/main/features/cli/args.ts +++ b/src/main/features/cli/args.ts @@ -3,6 +3,7 @@ * * `--name value`, `--name=value` and bare `--name` (true) are flags; `-h` is * `--help`; `--` ends the flags. Everything else is positional, in order. + * A value that starts with `--` is given as `--name=--value`. * Which flags take a value is the command's to say, so a bare flag followed * by a word is told apart by the spec. */ @@ -13,6 +14,8 @@ export interface FlagSpec { readonly description: string; /** A short name for the value in help, such as "N" or "URL". */ readonly value?: string; + /** A single-letter alias, spelled with one dash. */ + readonly short?: string; /** A string flag that may be given again: its values, in order, as a list. */ readonly repeat?: boolean; } @@ -57,12 +60,16 @@ export function parseArgs(argv: readonly string[], specs: Record specs[name]?.short === arg[1]) + : undefined; + if (!arg.startsWith("--") && short === undefined) { positionals.push(arg); continue; } const eq = arg.indexOf("="); - const name = eq === -1 ? arg.slice(2) : arg.slice(2, eq); + const name = short ?? (eq === -1 ? arg.slice(2) : arg.slice(2, eq)); const spec = all[name]; if (!spec) throw new CliUsageError(`unknown flag --${name}`); let raw: string | undefined = eq === -1 ? undefined : arg.slice(eq + 1); @@ -73,7 +80,9 @@ export function parseArgs(argv: readonly string[], specs: Record= argv.length) { + // A flag where the value should be is a value gone missing, not the + // value: PowerShell 5.1 drops `--name ''`, leaving `--name --json`. + if (i >= argv.length || (argv[i] as string).startsWith("--")) { // `--since #3`: the shell took "#3" for a comment. `--command ""`: // PowerShell 5.1 passes no empty argument. const hint = diff --git a/src/main/features/cli/browser/driver.ts b/src/main/features/cli/browser/driver.ts index faf85952b..4bf7b6204 100644 --- a/src/main/features/cli/browser/driver.ts +++ b/src/main/features/cli/browser/driver.ts @@ -109,6 +109,7 @@ export interface BrowserPage { readonly view: View; /** Whether the view is in the window; a tab not shown is not. */ readonly active: boolean; + readonly loadFailure?: { url: string; description: string } | null; } export interface BrowserDriver { @@ -939,6 +940,11 @@ export class CdpBrowserDriver implements BrowserDriver { if (contents.isLoading()) await once(contents, "did-stop-loading", LOAD_MS); } + private checkLoadFailure(tabId: string): void { + const failure = this.pageOf(tabId)?.loadFailure; + if (failure) throw new CliError(`Could not load ${failure.url}: ${failure.description}`); + } + async settle(tabId: string): Promise { const deadline = Date.now() + PAGE_START_MS; let page = this.pageOf(tabId); @@ -951,11 +957,13 @@ export class CdpBrowserDriver implements BrowserDriver { const done = this.driven.begin(contents); try { await this.settleOn(contents); + this.checkLoadFailure(tabId); await this.awaitDownload(contents); } finally { done(); } if (contents.isDestroyed()) return null; + this.checkLoadFailure(tabId); const download = this.downloads.get(contents); return { url: contents.getURL(), @@ -964,9 +972,14 @@ export class CdpBrowserDriver implements BrowserDriver { }; } - snapshot(tabId: string, options: { maxLines: number; within?: string }): Promise { + async snapshot( + tabId: string, + options: { maxLines: number; within?: string }, + ): Promise { + this.checkLoadFailure(tabId); return this.with(tabId, async (contents, state) => { if (contents.isLoading()) await once(contents, "did-stop-loading", LOAD_MS); + this.checkLoadFailure(tabId); const within = options.within === undefined ? undefined diff --git a/src/main/features/cli/claude-exit.ts b/src/main/features/cli/claude-exit.ts index 605b4ed39..c829ce3f6 100644 --- a/src/main/features/cli/claude-exit.ts +++ b/src/main/features/cli/claude-exit.ts @@ -24,6 +24,7 @@ export type ForegroundGroupLookup = ( /** Claude Code's state per terminal, as the hooks' broker keeps it. */ export interface ClaudeEntries { + /** Returns the same entry until a hook or a clear replaces it. */ get(workspaceId: string, tabId: string): { since: number } | null; clear(workspaceId: string, tabId: string): void; } @@ -33,6 +34,7 @@ export class ClaudeExitWatch { private readonly lastOutputAt = new Map(); /** Each Claude tab's timer for its output going quiet. */ private readonly timers = new Map>(); + private readonly lookups = new Map(); private readonly disposers: Array<() => void> = []; constructor( @@ -63,10 +65,12 @@ export class ClaudeExitWatch { dispose(): void { for (const dispose of this.disposers.splice(0)) dispose(); for (const key of [...this.timers.keys()]) this.forget(key); + this.lookups.clear(); } private output(workspaceId: string, tabId: string): void { const key = `${workspaceId}:${tabId}`; + this.lookups.delete(key); this.lastOutputAt.set(key, this.now()); if (!this.timers.has(key)) this.arm(workspaceId, tabId, this.quietMs); } @@ -89,24 +93,29 @@ export class ClaudeExitWatch { } private async check(workspaceId: string, tabId: string): Promise { - const askedAt = this.now(); + const entry = this.claude.get(workspaceId, tabId); + if (!entry) return; + const key = `${workspaceId}:${tabId}`; + const lookup = Symbol(); + this.lookups.set(key, lookup); let front: Awaited>; try { front = await this.foreground(workspaceId, tabId); } catch { - return; + front = null; } + if (this.lookups.get(key) !== lookup) return; + this.lookups.delete(key); // Claude, or a program it runs: Claude Code installed with npm is "node". // A shell not alone is a script's, waiting for the Claude it started. if (front === null || !isLoginShell(front.name) || front.alone === false) return; - const entry = this.claude.get(workspaceId, tabId); - // A state set while the program was looked up is a Claude started since. - if (entry && entry.since <= askedAt) this.claude.clear(workspaceId, tabId); + if (this.claude.get(workspaceId, tabId) === entry) this.claude.clear(workspaceId, tabId); } private forget(key: string): void { clearTimeout(this.timers.get(key)); this.timers.delete(key); this.lastOutputAt.delete(key); + this.lookups.delete(key); } } diff --git a/src/main/features/cli/commands/agent-context.ts b/src/main/features/cli/commands/agent-context.ts index be745b9ba..d4f2087a5 100644 --- a/src/main/features/cli/commands/agent-context.ts +++ b/src/main/features/cli/commands/agent-context.ts @@ -51,6 +51,7 @@ export function chatAgentContext( const remoteInstructions = [ "The user's other computers in the remote (nexus device list) are reached the same way:", "session list --device all, then chat or terminal read/send/wait ... --device NAME.", + "Copy files with nexus file pull --device DEVICE --workspace WS REMOTE [LOCAL] or nexus file push --device DEVICE --workspace WS LOCAL... REMOTE (add -r for a folder).", ]; function locationOf( @@ -87,7 +88,7 @@ function agentContextNote(heading: string, inRoom: boolean): string { "listens on, as nexus port list shows), then snapshot (the page with element refs), click e12,", "fill e3 TEXT, press Enter, screenshot, console and eval. The workbench can be arranged", "too: nexus layout show/split/resize, tab move/rename/pin, panel show/hide, reveal,", - "workspace move/pin (the sidebar's rows),", + "workspace move/pin/edit (the sidebar's rows: order, pin, name and color),", "and nexus view agents|workspaces, view layout/open/split for the agent view's panels;", "nexus problems lists the language servers' errors in open files.", "The user's notes are the app's (its Notes panel): a note they speak of is one of these,", diff --git a/src/main/features/cli/commands/chat-control.ts b/src/main/features/cli/commands/chat-control.ts index c6ae5648d..4978f2307 100644 --- a/src/main/features/cli/commands/chat-control.ts +++ b/src/main/features/cli/commands/chat-control.ts @@ -153,7 +153,7 @@ export function panelText(title: string, panel: ChatPanel, now: number): string blocks.push(`Remote Control: ${enabled ? `on${url ? ` (${url})` : ""}` : "off"}`); } block( - "Held for after the turn (chat unsend / chat edit ):", + "Held messages (chat unsend / chat edit ):", (panel.held ?? []).map((held) => { const carries = [ held.images ? `${held.images} image${held.images === 1 ? "" : "s"}` : "", diff --git a/src/main/features/cli/commands/chat-remote.ts b/src/main/features/cli/commands/chat-remote.ts index e97157864..6e51fdbe1 100644 --- a/src/main/features/cli/commands/chat-remote.ts +++ b/src/main/features/cli/commands/chat-remote.ts @@ -158,14 +158,14 @@ export async function sendToPeer( ); // The agent there is told whether this one waits to read its answer, or is to be written back to. const from = { ...(await agentSenderOf(ctx)), waits: timeoutMs !== null }; - const { at } = await askDevice(ctx, device, "chat.send", { + const { at, heldId } = await askDevice(ctx, device, "chat.send", { ...target(peer), text, from, ...(now ? { when: "now" as const } : {}), }); - const sent = `Sent to ${named(peer)}.`; - const json = { ...targetJson(peer), text }; + const sent = heldId ? `Held by ${named(peer)}, as ${heldId}.` : `Sent to ${named(peer)}.`; + const json = { ...targetJson(peer), text, ...(heldId ? { heldId } : {}) }; if (timeoutMs === null) return output(sent, json); const result = await waitOnPeerChats(ctx, [peer], timeoutMs, at); return peerWaitOutput(`${sent}\n${result.text}`, json, result); diff --git a/src/main/features/cli/commands/chat.ts b/src/main/features/cli/commands/chat.ts index cdc18b498..237d19d26 100644 --- a/src/main/features/cli/commands/chat.ts +++ b/src/main/features/cli/commands/chat.ts @@ -174,6 +174,7 @@ export async function waitOnChats( workspace: WorkspaceMeta, tabs: readonly CliTabInfo[], timeoutMs: number, + submissionId?: string, ): Promise { const { clock } = ctx.services; const startedAt = clock.now(); @@ -190,6 +191,7 @@ export async function waitOnChats( const info = await ctx.services.ui("chat.state", { workspaceId: workspace.id, tabId: tab.id, + ...(submissionId ? { submissionId } : {}), }); if (info.state !== "working") { return { @@ -223,8 +225,9 @@ export function waitOnChat( workspace: WorkspaceMeta, tab: CliTabInfo, timeoutMs: number, + submissionId?: string, ): Promise { - return waitOnChats(ctx, workspace, [tab], timeoutMs); + return waitOnChats(ctx, workspace, [tab], timeoutMs, submissionId); } /** A wait's result as the command prints it; exit 124 when the wait ran out. */ @@ -736,16 +739,16 @@ const chatSend: CliCommand = { args: " [text...]", details: "As its composer sends it: during a turn, the agent reads it after the tool it runs. --now\n" + - "puts it into a Claude tab's turn at once; --now with no text has the tab read the\n" + - "messages it has not read yet. --after holds it until the turn ends, its id printed for\n" + - 'chat unsend and chat edit. "-" reads the text from standard input. --file attaches a\n' + + "puts it into the turn at once. With no text, --now has a Claude tab read its unread\n" + + "messages. --after holds it until the turn ends. For held messages, use the printed id\n" + + 'with chat unsend and chat edit. "-" reads the text from standard input. --file attaches a\n' + "file of this computer (this terminal's), written where the tab's agent reads it, its\n" + "path after the text. Refused while the tab waits for the user to answer a request.\n" + "--wait then waits as chat wait does and prints how it ended. Another computer's tab\n" + "(--device) takes it as its composer sends it, or --now: it holds no agent's message for\n" + "after the turn.", flags: { - now: { type: "boolean", description: "During a turn: into it at once (a Claude tab's)" }, + now: { type: "boolean", description: "During a turn: into it at once" }, after: { type: "boolean", description: "During a turn: held until it ends" }, file: { type: "string", @@ -810,7 +813,7 @@ const chatSend: CliCommand = { const sender = await senderOf(ctx); const from = sender ? { ...sender, waits: timeoutMs !== null } : undefined; // Starting a program that is gone comes before the message. - const { heldId } = await ctx.services.ui( + const { heldId, submissionId } = await ctx.services.ui( "chat.send", { ...target, @@ -818,12 +821,14 @@ const chatSend: CliCommand = { ...(from ? { from } : {}), ...(attached.length > 0 ? { files: attached } : {}), ...(when ? { when } : {}), + ...(timeoutMs === null ? {} : { wait: {} }), }, SEND_TIMEOUT_MS, ); + const named = `${tab.title} (${shortId(tab.id)})`; const sent = heldId - ? `Held for after the turn of ${tab.title} (${shortId(tab.id)}), as ${heldId}.` - : `Sent to ${tab.title} (${shortId(tab.id)}).`; + ? `Held ${when === "after" ? "for after the turn of" : "by"} ${named}, as ${heldId}.` + : `Sent to ${named}.`; const json = { ...target, text, @@ -831,7 +836,7 @@ const chatSend: CliCommand = { ...(heldId ? { heldId } : {}), }; if (timeoutMs === null) return output(sent, json); - const result = await waitOnChat(ctx, workspace, tab, timeoutMs); + const result = await waitOnChat(ctx, workspace, tab, timeoutMs, submissionId); return waitOutput(`${sent}\n${result.text}`, json, result); }, }; diff --git a/src/main/features/cli/commands/file.ts b/src/main/features/cli/commands/file.ts new file mode 100644 index 000000000..6b5bd9b79 --- /dev/null +++ b/src/main/features/cli/commands/file.ts @@ -0,0 +1,107 @@ +import { CliError } from "../context"; +import { pull } from "../file-pull"; +import { push } from "../file-push"; +import type { FileTransfer } from "../file-transfer"; +import { type CliCommand, output } from "../registry"; +import { deviceFlag } from "../remote"; +import { WORKSPACE_FLAG } from "./flags"; + +const flagsOfFile = { + device: { + type: "string", + value: "DEVICE", + description: "The remote computer (id, id prefix or name)", + }, + workspace: { ...WORKSPACE_FLAG, description: "The remote workspace (id, id prefix or name)" }, + recursive: { type: "boolean", short: "r", description: "Copy a directory and its contents" }, +} as const; + +const details = + "Give both --device and --workspace. Remote relative paths start at that workspace. " + + "Absolute paths outside it require terminal control. Local relative paths start at this shell's cwd."; + +async function copy( + verb: "pull" | "push", + signal: AbortSignal | undefined, + array: boolean, + run: () => Promise, +) { + try { + const result = await run(); + const records = result.entries.map((entry) => ({ device: result.device, ...entry })); + const lines = result.entries.map((entry) => { + if ("skipped" in entry) + return `Skipped symlink ${JSON.stringify(entry.remotePath ?? entry.path)}`; + const destination = verb === "pull" ? entry.path : entry.remotePath; + return `${JSON.stringify(destination)} ${entry.size} bytes sha256 ${entry.sha256}`; + }); + return output( + lines.join("\n") || `Copied directory to ${JSON.stringify(result.destination)}`, + array ? records : records[0], + ); + } catch (error) { + if (signal?.aborted) throw new CliError(`file ${verb} cancelled`, 3); + if (!(error instanceof Error)) throw error; + const message = error.message.replace(/[\r\n]+/g, " "); + if (message === error.message) throw error; + throw error instanceof CliError + ? new CliError(message, error.exitCode, error.json) + : new CliError(message); + } +} + +const filePull: CliCommand = { + path: ["file", "pull"], + args: " [local-path]", + summary: "Copy a file from another NexusCode computer", + details, + flags: flagsOfFile, + async run(ctx, { positionals: [remotePath, localPath, ...extra], flags }) { + const device = deviceFlag(flags); + if (!device) throw new CliError("give --device to choose a remote computer", 2); + if (!remotePath || extra.length > 0 || localPath === "") { + throw new CliError("give [local-path]", 2); + } + const workspace = typeof flags.workspace === "string" ? flags.workspace : undefined; + const recursive = flags.recursive === true; + return copy("pull", ctx.signal, recursive, () => + pull(ctx, { + device, + remotePath, + recursive, + ...(workspace !== undefined ? { workspace } : {}), + ...(localPath !== undefined ? { localPath } : {}), + }), + ); + }, +}; + +const filePush: CliCommand = { + path: ["file", "push"], + args: " ", + summary: "Copy files to another NexusCode computer", + details, + flags: flagsOfFile, + async run(ctx, { positionals, flags }) { + const device = deviceFlag(flags); + if (!device) throw new CliError("give --device to choose a remote computer", 2); + const localPaths = positionals.slice(0, -1); + const remotePath = positionals.at(-1); + if (!remotePath || localPaths.length === 0 || localPaths.includes("")) { + throw new CliError("give ", 2); + } + const workspace = typeof flags.workspace === "string" ? flags.workspace : undefined; + const recursive = flags.recursive === true; + return copy("push", ctx.signal, recursive || localPaths.length > 1, () => + push(ctx, { + device, + localPaths, + remotePath, + recursive, + ...(workspace !== undefined ? { workspace } : {}), + }), + ); + }, +}; + +export const fileCommands: CliCommand[] = [filePull, filePush]; diff --git a/src/main/features/cli/commands/index.ts b/src/main/features/cli/commands/index.ts index c0d9df7a3..4e923d5c4 100644 --- a/src/main/features/cli/commands/index.ts +++ b/src/main/features/cli/commands/index.ts @@ -10,6 +10,7 @@ import { chatControlCommands } from "./chat-control"; import { deviceCommands } from "./device"; import { editorCommands } from "./editor"; import { eventCommands } from "./events"; +import { fileCommands } from "./file"; import { layoutCommands, tabArrangeCommands } from "./layout"; import { noteCommands } from "./note"; import { notifyCommands } from "./notify"; @@ -27,6 +28,7 @@ export const CLI_COMMANDS: readonly CliCommand[] = [ ...chatControlCommands, ...eventCommands, ...deviceCommands, + ...fileCommands, ...tabArrangeCommands, ...layoutCommands, ...viewCommands, diff --git a/src/main/features/cli/commands/note.ts b/src/main/features/cli/commands/note.ts index 66a77d80b..01d616465 100644 --- a/src/main/features/cli/commands/note.ts +++ b/src/main/features/cli/commands/note.ts @@ -11,7 +11,7 @@ import { noteMarkdown } from "../../notes/store"; import { CliUsageError, exactArgs, type ParsedArgs } from "../args"; import { type CliContext, CliError, mayChange, pick, resolveWorkspace } from "../context"; import { ago, oneLine, table } from "../format"; -import { callerPath } from "../paths"; +import { callerPath, onAppMachine } from "../paths"; import { type CliCommand, output } from "../registry"; import { WORKSPACE_FLAG } from "./flags"; @@ -22,15 +22,6 @@ function when(at: number, now: number): string { return new Date(at).toISOString().slice(0, 10); } -/** - * Whether the command runs in a terminal on the machine the app runs on (a - * local workspace's), where the notes' folders are. - */ -function onAppMachine(ctx: CliContext): boolean { - const own = ctx.services.listWorkspaces().find((w) => w.id === ctx.caller.workspaceId); - return own?.location.kind === "local"; -} - const NOTE_ARG_HELP = "A note is named by its id, an id prefix, or its title (as nexus note list shows them)."; diff --git a/src/main/features/cli/commands/terminal.ts b/src/main/features/cli/commands/terminal.ts index 19452570f..53e47c7aa 100644 --- a/src/main/features/cli/commands/terminal.ts +++ b/src/main/features/cli/commands/terminal.ts @@ -26,7 +26,6 @@ import { pickPeerWorkspace, resolveDevice, } from "../remote"; -import { senderOf } from "../sender"; import { DEFAULT_QUIET_MS, DEFAULT_WAIT_MS, diff --git a/src/main/features/cli/commands/workspace.ts b/src/main/features/cli/commands/workspace.ts index 3ee3127c9..04df1a422 100644 --- a/src/main/features/cli/commands/workspace.ts +++ b/src/main/features/cli/commands/workspace.ts @@ -1,6 +1,12 @@ /** nexus status, nexus workspace ... and nexus worktree ... (the app's tasks). */ import type { CliTaskStart, CliView } from "../../../../shared/cli/ui"; import type { WorkspaceLocation, WorkspaceMeta } from "../../../../shared/types/workspace"; +import { + WORKSPACE_COLOR_AUTO, + WORKSPACE_COLORS, + type WorkspaceColor, + WorkspaceColorSchema, +} from "../../../../shared/types/workspace-color"; import { compareWorkspaceOrder } from "../../../../shared/workspace-order"; import { CliUsageError, exactArgs } from "../args"; import { @@ -31,6 +37,7 @@ export function workspaceJson(ctx: CliContext, workspace: WorkspaceMeta) { name: workspace.name, location: workspace.location, pinned: workspace.pinned, + color: workspace.color, current: workspace.id === ctx.caller.workspaceId, shown: shown?.workspaceId === workspace.id, ...(workspace.task @@ -526,6 +533,24 @@ function locationBeside(beside: WorkspaceLocation, folder: string): WorkspaceLoc : { ...beside, remotePath: folder }; } +const COLOR_FLAG = { + type: "string", + value: "COLOR", + description: `One of ${WORKSPACE_COLORS.join(", ")}, or ${WORKSPACE_COLOR_AUTO} for the app's own pick`, +} as const; + +/** The color a --color flag names; undefined when it is not given. */ +export function colorFlag(value: unknown): WorkspaceColor | undefined { + if (value === undefined) return undefined; + const parsed = WorkspaceColorSchema.safeParse(String(value).trim().toLowerCase()); + if (!parsed.success) { + throw new CliUsageError( + `--color is one of ${WORKSPACE_COLORS.join(", ")}, or ${WORKSPACE_COLOR_AUTO}`, + ); + } + return parsed.data; +} + const workspaceAdd: CliCommand = { path: ["workspace", "add"], summary: "Add a folder to the app as a workspace, on this terminal's host", @@ -534,9 +559,11 @@ const workspaceAdd: CliCommand = { "As Add Workspace in the app: the folder, from this terminal's directory, goes at the end\n" + "of the sidebar, named after it unless --name. An SSH or WSL terminal adds a folder on its\n" + "own host, reached as its workspace is. It connects once it is first shown, so --show it\n" + - "or click it. A folder already in the list gives that workspace.", + "or click it. --color gives it a color, as the sidebar's menu does. A folder already in\n" + + "the list gives that workspace as it is.", flags: { name: { type: "string", value: "NAME", description: "The name the sidebar shows" }, + color: COLOR_FLAG, show: { type: "boolean", description: "Show it to the user" }, }, async run(ctx, { positionals, flags }) { @@ -544,7 +571,8 @@ const workspaceAdd: CliCommand = { const caller = resolveWorkspace(ctx, undefined); const location = locationBeside(caller.location, absolutePath(ctx, caller, input)); const name = (flags.name as string | undefined)?.trim() || undefined; - const { workspace, added } = await ctx.services.addWorkspace(location, name); + const color = colorFlag(flags.color); + const { workspace, added } = await ctx.services.addWorkspace(location, name, color); if (flags.show === true) await ctx.services.ui("workspace.show", { workspaceId: workspace.id }); const what = added ? "Added" : "Already listed"; return output( @@ -632,6 +660,47 @@ function workspacePinCommand(pinned: boolean): CliCommand { }; } +const workspaceEdit: CliCommand = { + path: ["workspace", "edit"], + summary: "Rename a workspace or change its color, as the sidebar's menu does", + args: "", + details: + "Give --name, --color or both. The color marks the workspace in the sidebar and the agent\n" + + `view; ${WORKSPACE_COLOR_AUTO} leaves it to the app. A worktree is shown under its workspace and\n` + + "takes its color, so it is not edited here. Another workspace's is changed with the user's leave.", + flags: { + name: { type: "string", value: "NAME", description: "The name the sidebar shows" }, + color: COLOR_FLAG, + }, + async run(ctx, { positionals, flags }) { + const [selector] = exactArgs(positionals, "workspace") as [string]; + const name = (flags.name as string | undefined)?.trim(); + const color = colorFlag(flags.color); + if (flags.name !== undefined && !name) throw new CliUsageError("--name is empty"); + if (name === undefined && color === undefined) { + throw new CliUsageError("give --name, --color or both"); + } + const target = sidebarWorkspace(ctx, selector); + const edit = { + ...(name !== undefined && name !== target.name ? { name } : {}), + ...(color !== undefined && color !== target.color ? { color } : {}), + }; + if (Object.keys(edit).length === 0) { + return output(`${target.name} is as asked.`, workspaceJson(ctx, target)); + } + await mayChange(ctx, target, { kind: "workspaceEdit" }); + const changed = ctx.services.editWorkspace(target.id, edit); + const parts = [ + ...(edit.name !== undefined ? [`renamed from ${target.name}`] : []), + ...(edit.color !== undefined ? [`color ${changed.color}`] : []), + ]; + return output( + `${changed.name} (${shortId(changed.id)}): ${parts.join(", ")}.`, + workspaceJson(ctx, changed), + ); + }, +}; + /** What a group of the sidebar is called. */ const GROUP_NAMES = { pinned: "pinned workspaces", unpinned: "workspaces not pinned" } as const; @@ -696,6 +765,7 @@ export const workspaceCommands: CliCommand[] = [ workspaceSwitch, workspaceAdd, workspaceRemove, + workspaceEdit, workspacePinCommand(true), workspacePinCommand(false), workspaceMove, diff --git a/src/main/features/cli/context.ts b/src/main/features/cli/context.ts index f0a90572e..2a4527128 100644 --- a/src/main/features/cli/context.ts +++ b/src/main/features/cli/context.ts @@ -19,6 +19,7 @@ import type { TranscriptFile } from "../../../shared/notes/transcript"; import type { NoteAsset, NoteContent, NoteSummary } from "../../../shared/notes/types"; import type { ListeningPort, PortForward } from "../../../shared/pty/ports"; import type { WorkspaceLocation, WorkspaceMeta } from "../../../shared/types/workspace"; +import type { WorkspaceColor } from "../../../shared/types/workspace-color"; import type { TabActivity } from "./activity"; import type { BrowserDriver } from "./browser/driver"; import type { ChangeRequest } from "./consent"; @@ -67,6 +68,7 @@ export interface CliRemote { method: M, params: RpcParams, timeoutMs?: number, + signal?: AbortSignal, ): Promise>; } @@ -114,7 +116,10 @@ export interface CliServices { addWorkspace( location: WorkspaceLocation, name: string | undefined, + color?: WorkspaceColor, ): Promise<{ workspace: WorkspaceMeta; added: boolean }>; + /** Renames a workspace and sets its color, as the sidebar's menu does; the workspace as it is now. */ + editWorkspace(id: string, edit: { name?: string; color?: WorkspaceColor }): WorkspaceMeta; /** * Pins or unpins a workspace as the sidebar's pin does (to the end of its * new group); the workspace as it is now. diff --git a/src/main/features/cli/file-pull.ts b/src/main/features/cli/file-pull.ts new file mode 100644 index 000000000..fadb91b0a --- /dev/null +++ b/src/main/features/cli/file-pull.ts @@ -0,0 +1,332 @@ +import { createHash, randomUUID } from "node:crypto"; +import { lstat, mkdir, open, rename, stat, unlink } from "node:fs/promises"; +import path from "node:path"; +import { + MAX_BYTES_CHUNK, + RpcErrorCode, + type RpcResult, +} from "../../../../packages/remote-protocol/src"; +import { speaks } from "../../../shared/remote/room"; +import { validFileName, writeFileChunk } from "../remote/file-io"; +import { bytesOf, supportsFileVersions } from "../remote/peer-file-reads"; +import { type CliContext, CliError } from "./context"; +import { + type FilePeer, + type FileTransfer, + fileTransferTarget, + rpcMessage, + transferChunks, + withFileTransfer, +} from "./file-transfer"; +import { callerPath } from "./paths"; +import { deviceRef } from "./remote"; + +type Bytes = Omit, { kind: "bytes" }>, "data"> & { data: Buffer }; +type Source = + | { kind: "file"; remotePath: string } + | { kind: "link"; remotePath: string } + | { kind: "dir"; remotePath: string; children: { name: string; source: Source }[] }; +type Copy = { path: string; source: Source }; + +interface SavedFile { + path: string; + remotePath: string; + size: number; + sha256: string; +} + +type PullEntry = SavedFile | { remotePath: string; skipped: "symlink" }; + +function isFolder(error: unknown): boolean { + return ( + rpcMessage(error, RpcErrorCode.InvalidParams)?.endsWith("is a folder: files.list lists it") ?? + false + ); +} + +function changed(remotePath: string): CliError { + return new CliError(`${JSON.stringify(remotePath)} changed while copying`); +} + +async function readBytes( + peer: FilePeer, + request: { path: string; offset: number; length: number; versioned?: true }, + signal: AbortSignal, + generation?: Bytes, +): Promise { + const { path: remotePath, offset, length } = request; + const version = generation?.version; + let result: RpcResult<"files.read">; + try { + result = await peer( + "files.read", + { + ...request, + as: "bytes", + ...(version !== undefined ? { requireVersion: version } : {}), + }, + signal, + ); + } catch (error) { + if ( + rpcMessage(error, RpcErrorCode.InvalidParams) === "file version changed" || + rpcMessage(error, RpcErrorCode.Unavailable) === "the file changed while it was being read" || + (generation && (isFolder(error) || rpcMessage(error, RpcErrorCode.NotFound) !== undefined)) + ) + throw changed(remotePath); + throw error; + } + if (result.kind !== "bytes") { + throw new CliError('the remote NexusCode does not support files.read as "bytes"', 3); + } + if ( + !Number.isSafeInteger(result.size) || + (generation && + (result.size !== generation.size || (version !== undefined && result.version !== version))) + ) + throw changed(remotePath); + try { + bytesOf(result, offset, length); + } catch { + throw changed(remotePath); + } + const data = Buffer.from(result.data, "base64"); + if (data.length !== Math.max(0, Math.min(length, result.size - offset))) { + throw changed(remotePath); + } + return { ...result, data }; +} + +async function listTree( + peer: FilePeer, + remotePath: string, + rules: path.PlatformPath, + signal: AbortSignal, +): Promise { + // Discover truncation anywhere in the tree before creating local entries. + const listing = await peer("files.list", { path: remotePath }, signal); + if (listing.truncated) { + throw new CliError(`directory listing for ${JSON.stringify(listing.path)} is truncated`); + } + const children: Extract["children"] = []; + for (const entry of listing.entries) { + if (!validFileName(entry.name, rules)) { + throw new CliError(`invalid entry name from the remote: ${JSON.stringify(entry.name)}`); + } + const child = rules.join(listing.path, entry.name); + const source = + entry.kind === "dir" + ? await listTree(peer, child, rules, signal) + : { kind: entry.kind, remotePath: child }; + children.push({ name: entry.name, source }); + } + return { kind: "dir", remotePath: listing.path, children }; +} + +async function localIO(destination: string, act: () => Promise): Promise { + try { + return await act(); + } catch (error) { + throw new CliError(`cannot write ${JSON.stringify(destination)}: ${(error as Error).message}`); + } +} + +function localChild(parent: string, name: string): string { + if (!validFileName(name, path)) { + throw new CliError(`cannot save the remote name ${JSON.stringify(name)} on this computer`); + } + return path.join(parent, name); +} + +async function destinationOf( + ctx: CliContext, + given: string | undefined, + sourceName: string, +): Promise { + if (given === undefined) return localChild(ctx.invocation.cwd, sourceName); + const destination = callerPath(ctx, given); + const existing = await localIO(destination, async () => { + try { + return await stat(destination); + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; + } + }); + return existing?.isDirectory() ? localChild(destination, sourceName) : destination; +} + +function planCopies(source: Source, destination: string, copies: Copy[]): void { + copies.push({ path: destination, source }); + if (source.kind !== "dir") return; + for (const { name, source: child } of source.children) { + planCopies(child, child.kind === "link" ? destination : localChild(destination, name), copies); + } +} + +async function makeDirectory(destination: string): Promise { + try { + await mkdir(destination); + } catch (error) { + if ( + (error as NodeJS.ErrnoException).code !== "EEXIST" || + !(await lstat(destination)).isDirectory() + ) { + throw error; + } + } +} + +/** Use the current destination's mode when replacing a regular file. */ +async function destinationMode(destination: string): Promise { + try { + const existing = await lstat(destination); + if (existing.isFile()) return existing.mode & 0o7777; + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; + } + return 0o666 & ~process.umask(); +} + +async function copyFile( + peer: FilePeer, + remotePath: string, + destination: string, + first: Bytes, + signal: AbortSignal, +): Promise { + signal.throwIfAborted(); + const temporary = path.join(path.dirname(destination), `.nexus-pull-${randomUUID()}.tmp`); + const file = await localIO(destination, () => open(temporary, "wx", 0o600)); + const hash = createHash("sha256"); + let saved = false; + try { + try { + await localIO(destination, () => writeFileChunk(file, first.data, 0)); + hash.update(first.data); + await transferChunks( + first.data.length, + first.size, + signal, + async (offset, length, pieceSignal) => { + const { data } = await readBytes( + peer, + { path: remotePath, offset, length }, + pieceSignal, + first, + ); + pieceSignal.throwIfAborted(); + await localIO(destination, () => writeFileChunk(file, data, offset)); + return data; + }, + (bytes) => hash.update(bytes), + ); + if (first.version === undefined) { + // Older peers cannot pin a version. This final probe detects a size + // or prefix change; the remaining bytes have no snapshot guarantee. + const probe = await readBytes( + peer, + { path: remotePath, offset: 0, length: 1 }, + signal, + first, + ); + if (!probe.data.equals(first.data.subarray(0, 1))) throw changed(remotePath); + } + signal.throwIfAborted(); + if (process.platform !== "win32") { + await localIO(destination, async () => file.chmod(await destinationMode(destination))); + } + } finally { + await localIO(destination, () => file.close()); + } + signal.throwIfAborted(); + await localIO(destination, () => rename(temporary, destination)); + saved = true; + return { + path: destination, + remotePath: first.path, + size: first.size, + sha256: hash.digest("hex"), + }; + } finally { + if (!saved) await localIO(destination, () => unlink(temporary)); + } +} + +export async function pull( + ctx: CliContext, + options: { + device: string; + workspace?: string; + remotePath: string; + localPath?: string; + recursive: boolean; + }, +): Promise { + return withFileTransfer(ctx, "pull", async (signal) => { + const { device, rules, anywhere, peer } = await fileTransferTarget( + ctx, + options, + "pull", + signal, + ); + const versioned = supportsFileVersions(device.schema); + const readFirst = (remotePath: string) => + readBytes( + peer, + { + path: remotePath, + offset: 0, + length: MAX_BYTES_CHUNK, + ...(versioned ? { versioned: true } : {}), + }, + signal, + ); + let first: Bytes | undefined; + let source: Source | undefined; + if (anywhere && options.recursive && speaks(device.schema, "0.14.0")) { + // Approve the tree at its root before reading children. + try { + source = await listTree(peer, options.remotePath, rules, signal); + } catch (error) { + if ( + !rpcMessage(error, RpcErrorCode.InvalidParams)?.endsWith("is a file: files.read reads it") + ) + throw error; + } + } + if (!source) { + try { + first = await readFirst(options.remotePath); + source = { kind: "file", remotePath: options.remotePath }; + } catch (error) { + if (!isFolder(error)) throw error; + if (!options.recursive) { + throw new CliError( + `${JSON.stringify(options.remotePath)} is a directory (use -r to copy it)`, + ); + } + source = await listTree(peer, options.remotePath, rules, signal); + } + } + const destination = await destinationOf( + ctx, + options.localPath, + rules.basename(first?.path ?? source.remotePath), + ); + const copies: Copy[] = []; + planCopies(source, destination, copies); + const entries: PullEntry[] = []; + for (const { source: item, path: target } of copies) { + signal.throwIfAborted(); + if (item.kind === "dir") await localIO(target, () => makeDirectory(target)); + else if (item.kind === "link") + entries.push({ remotePath: item.remotePath, skipped: "symlink" }); + else { + const head = first ?? (await readFirst(item.remotePath)); + entries.push(await copyFile(peer, item.remotePath, target, head, signal)); + } + } + signal.throwIfAborted(); + return { device: deviceRef(device), destination, entries }; + }); +} diff --git a/src/main/features/cli/file-push.ts b/src/main/features/cli/file-push.ts new file mode 100644 index 000000000..e11e2a786 --- /dev/null +++ b/src/main/features/cli/file-push.ts @@ -0,0 +1,226 @@ +import { createHash } from "node:crypto"; +import { type BigIntStats, constants } from "node:fs"; +import { lstat, open, readdir } from "node:fs/promises"; +import path from "node:path"; +import { MAX_FILE_WRITE_BYTES, RpcErrorCode } from "../../../../packages/remote-protocol/src"; +import { readFileChunk, validFileName } from "../remote/file-io"; +import { type CliContext, CliError } from "./context"; +import { + type FilePeer, + type FileTransfer, + fileTransferTarget, + rpcMessage, + transferChunks, + withFileTransfer, +} from "./file-transfer"; +import { callerPath } from "./paths"; +import { deviceRef } from "./remote"; + +type Copy = + | { kind: "file"; path: string; remotePath: string; stat: BigIntStats } + | { kind: "dir"; path: string; remotePath: string } + | { kind: "link"; path: string; remotePath: string }; + +function remoteChild(rules: path.PlatformPath, parent: string, name: string): string { + if (!validFileName(name, rules)) { + throw new CliError(`cannot save the local name ${JSON.stringify(name)} on the remote computer`); + } + return rules.join(parent, name); +} + +async function sourceStat(source: string): Promise { + try { + return await lstat(source, { bigint: true }); + } catch (error) { + throw new CliError(`cannot read ${JSON.stringify(source)}: ${(error as Error).message}`); + } +} + +async function planCopies( + copies: Copy[], + local: string, + remote: string, + info: BigIntStats, + rules: path.PlatformPath, + recursive: boolean, + signal: AbortSignal, +): Promise { + signal.throwIfAborted(); + if (info.isSymbolicLink() && recursive) { + copies.push({ kind: "link", path: local, remotePath: remote }); + } else if (info.isDirectory()) { + if (!recursive) + throw new CliError(`${JSON.stringify(local)} is a directory (use -r to copy it)`); + copies.push({ kind: "dir", path: local, remotePath: remote }); + for (const name of (await readdir(local)).sort()) { + const child = path.join(local, name); + const childInfo = await sourceStat(child); + const target = childInfo.isSymbolicLink() ? remote : remoteChild(rules, remote, name); + await planCopies(copies, child, target, childInfo, rules, recursive, signal); + } + } else { + if (!info.isFile()) throw new CliError(`${JSON.stringify(local)} is not a regular file`); + if (info.size > MAX_FILE_WRITE_BYTES) { + throw new CliError( + `${JSON.stringify(local)} exceeds the ${MAX_FILE_WRITE_BYTES} byte upload limit`, + ); + } + copies.push({ kind: "file", path: local, remotePath: remote, stat: info }); + } +} + +async function remoteDirectory( + peer: FilePeer, + remotePath: string, + signal: AbortSignal, + writeTarget: boolean, +): Promise { + try { + return ( + await peer( + "files.list", + { path: remotePath, ...(writeTarget ? { writeTarget: true } : {}) }, + signal, + ) + ).path; + } catch (error) { + if ( + rpcMessage(error, RpcErrorCode.NotFound) !== undefined || + rpcMessage(error, RpcErrorCode.InvalidParams)?.endsWith("is a file: files.read reads it") + ) + return; + throw error; + } +} + +function unchanged(before: BigIntStats, after: BigIntStats): boolean { + return ( + after.isFile() && + before.dev === after.dev && + before.ino === after.ino && + before.size === after.size && + before.mtimeNs === after.mtimeNs && + before.ctimeNs === after.ctimeNs + ); +} + +async function sendFile( + peer: FilePeer, + source: Extract, + signal: AbortSignal, +) { + const file = await open( + source.path, + constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0), + ); + const changed = () => new CliError(`${JSON.stringify(source.path)} changed while copying`); + let uploadId: string | undefined; + try { + if (!unchanged(source.stat, await file.stat({ bigint: true }))) throw changed(); + const size = Number(source.stat.size); + const upload = await peer("files.write.begin", { path: source.remotePath, size }, signal); + uploadId = upload.uploadId; + const hash = createHash("sha256"); + await transferChunks( + 0, + size, + signal, + async (offset, length, pieceSignal) => { + const bytes = await readFileChunk(file, offset, length); + await peer( + "files.write.chunk", + { uploadId: upload.uploadId, offset, data: bytes.toString("base64") }, + pieceSignal, + ); + return bytes; + }, + (bytes) => hash.update(bytes), + ); + if (!unchanged(source.stat, await file.stat({ bigint: true }))) throw changed(); + const sha256 = hash.digest("hex"); + const saved = await peer("files.write.end", { uploadId, sha256 }, signal); + uploadId = undefined; + if (saved.size !== size || saved.sha256 !== sha256) { + throw new CliError("the remote did not confirm the expected file size and SHA-256"); + } + return { path: source.path, remotePath: saved.path, size, sha256 }; + } finally { + try { + await file.close(); + } finally { + if (uploadId) { + // The transfer signal may already be cancelled. Still give the host a chance to clean up. + await peer("files.write.abort", { uploadId }, AbortSignal.timeout(5000)).catch(() => {}); + } + } + } +} + +export async function push( + ctx: CliContext, + options: { + device: string; + workspace?: string; + remotePath: string; + localPaths: string[]; + recursive: boolean; + }, +): Promise { + return withFileTransfer(ctx, "push", async (signal) => { + const { device, rules, anywhere, peer } = await fileTransferTarget( + ctx, + options, + "push", + signal, + ); + const inspectSources = () => + Promise.all( + options.localPaths.map(async (given) => { + const local = callerPath(ctx, given); + return { local, info: await sourceStat(local) }; + }), + ); + const sources = anywhere && options.recursive ? await inspectSources() : undefined; + const writeTarget = sources?.some(({ info }) => info.isDirectory()) === true; + // Use the caller's path spelling to compose children. In workspace mode + // the host's listing may return a differently spelled absolute root. + const directory = await remoteDirectory(peer, options.remotePath, signal, writeTarget); + if (!directory && options.localPaths.length > 1) { + throw new CliError( + "the remote destination must be an existing directory for multiple sources", + ); + } + const copies: Copy[] = []; + for (const { local, info } of sources ?? (await inspectSources())) { + if ( + !directory && + !info.isDirectory() && + (options.remotePath.endsWith("/") || + (rules === path.win32 && options.remotePath.endsWith("\\"))) + ) { + throw new CliError( + "the remote destination must be an existing directory when its path ends with a separator", + ); + } + const remote = directory + ? remoteChild(rules, options.remotePath, path.basename(local)) + : options.remotePath; + await planCopies(copies, local, remote, info, rules, options.recursive, signal); + } + const entries: FileTransfer["entries"] = []; + let destination = directory ?? options.remotePath; + for (const copy of copies) { + signal.throwIfAborted(); + if (copy.kind === "link") { + entries.push({ path: copy.path, skipped: "symlink" }); + } else if (copy.kind === "dir") { + const created = await peer("files.mkdir", { path: copy.remotePath }, signal); + if (copy === copies[0] && options.localPaths.length === 1) destination = created.path; + } else { + entries.push(await sendFile(peer, copy, signal)); + } + } + signal.throwIfAborted(); + return { device: deviceRef(device), destination, entries }; + }); +} diff --git a/src/main/features/cli/file-transfer.ts b/src/main/features/cli/file-transfer.ts new file mode 100644 index 000000000..17537614d --- /dev/null +++ b/src/main/features/cli/file-transfer.ts @@ -0,0 +1,242 @@ +import { randomUUID } from "node:crypto"; +import path from "node:path"; +import { performance } from "node:perf_hooks"; +import { setTimeout as delay } from "node:timers/promises"; +import { + type AgentSender, + MAX_BYTES_CHUNK, + RpcErrorCode, + type RpcParams, + type RpcResult, +} from "../../../../packages/remote-protocol/src"; +import { speaks } from "../../../shared/remote/room"; +import { FILE_WRITE_BUSY, filePathRules } from "../remote/file-io"; +import { RpcFailure } from "../remote/room"; +import { type CliContext, type CliDevice, CliError, type CliRemote } from "./context"; +import { onAppMachine } from "./paths"; +import { + agentSenderOf, + askDevice, + deviceRef, + offline, + peerSessions, + pickPeerWorkspace, + resolveDevice, +} from "./remote"; + +type FileMethod = + | "files.list" + | "files.read" + | "files.mkdir" + | "files.write.begin" + | "files.write.chunk" + | "files.write.end" + | "files.write.abort"; +type FilePeerParams = Omit, "workspaceId" | "from" | "anywhere">; +export type FilePeer = ( + method: M, + params: FilePeerParams, + signal: AbortSignal, +) => Promise>; + +function outsideWorkspaceError(device: CliDevice): CliError { + return new CliError( + `"${device.name}" cannot serve file access outside the workspace. Update NexusCode there and enable terminal control (Settings › Remote)`, + 3, + { error: "outside-workspace", device: deviceRef(device) }, + ); +} + +export interface FileTransfer { + device: ReturnType; + destination: string; + entries: Array< + | { path: string; remotePath: string; size: number; sha256: string } + | { path?: string; remotePath?: string; skipped: "symlink" } + >; +} + +const PARALLEL_CHUNKS = 4; +const BUSY_RETRIES = 5; +const BUSY_RETRY_BASE_MS = 250; +// Leave room for chats and terminals on the relay's 50-frame-per-second budget. +const REQUESTS_PER_SECOND = 24; +// Pulls and pushes on the same app connection share its request budget. +const sentByRemote = new WeakMap(); +// Leave time to unwind before the hook server (35m) and client (36m) deadlines. +const TRANSFER_TIMEOUT_MS = 34 * 60_000; + +async function waitForFileRequest(sent: number[], signal: AbortSignal): Promise { + for (;;) { + signal.throwIfAborted(); + const now = performance.now(); + while (sent.length > 0 && now - (sent[0] as number) >= 1000) sent.shift(); + if (sent.length < REQUESTS_PER_SECOND) { + sent.push(now); + return; + } + await delay(Math.ceil(1000 - (now - (sent[0] as number))), undefined, { signal }); + } +} + +export function rpcMessage(error: unknown, code: number): string | undefined { + if (error instanceof CliError && error.cause instanceof RpcFailure && error.cause.code === code) { + return error.cause.message; + } +} + +/** Process up to four pieces together, reporting them in offset order for hashing. */ +export async function transferChunks( + start: number, + size: number, + signal: AbortSignal, + transfer: (offset: number, length: number, signal: AbortSignal) => Promise, + received: (bytes: Buffer) => void, +): Promise { + const controller = new AbortController(); + const pieceSignal = AbortSignal.any([signal, controller.signal]); + for (let offset = start; offset < size; ) { + signal.throwIfAborted(); + const batch: Promise[] = []; + for (let i = 0; i < PARALLEL_CHUNKS && offset < size; i++) { + const length = Math.min(MAX_BYTES_CHUNK, size - offset); + batch.push(transfer(offset, length, pieceSignal)); + offset += length; + } + try { + for (const bytes of await Promise.all(batch)) received(bytes); + } catch (error) { + controller.abort(); + // A positioned operation must finish before its file handle is closed. + await Promise.allSettled(batch); + throw error; + } + } +} + +export async function withFileTransfer( + ctx: CliContext, + verb: "pull" | "push", + run: (signal: AbortSignal) => Promise, +): Promise { + if (!onAppMachine(ctx)) { + throw new CliError(`run file ${verb} in a local workspace on the app's computer`); + } + const deadline = new AbortController(); + const timer = setTimeout( + () => deadline.abort(new CliError(`file ${verb} timed out`, 3)), + TRANSFER_TIMEOUT_MS, + ); + const signal = ctx.signal ? AbortSignal.any([ctx.signal, deadline.signal]) : deadline.signal; + try { + signal.throwIfAborted(); + return await run(signal); + } catch (error) { + signal.throwIfAborted(); + throw error; + } finally { + clearTimeout(timer); + } +} + +function outsideWorkspace(rules: path.PlatformPath, root: string, asked: string): boolean { + const absolute = + rules === path.win32 ? /^[A-Za-z]:[\\/]|^[\\/]{2}[^\\/]/.test(asked) : rules.isAbsolute(asked); + if (!absolute) return false; + const relative = rules.relative(root, asked); + return relative === ".." || relative.startsWith(`..${rules.sep}`) || rules.isAbsolute(relative); +} + +function filePeer( + ctx: CliContext, + device: CliDevice, + workspaceId: string, + anywhere: boolean, +): FilePeer { + const current = speaks(device.schema, "0.14.0"); + let sender: Promise | undefined; + const remote = ctx.services.remote; + const previous = sentByRemote.get(remote); + const sent = previous ?? []; + if (!previous) sentByRemote.set(remote, sent); + const transferId = current ? randomUUID() : undefined; + return async ( + method: M, + params: FilePeerParams, + signal: AbortSignal, + ) => { + const continuation = + method === "files.write.chunk" || + method === "files.write.end" || + method === "files.write.abort"; + if (current) sender ??= agentSenderOf(ctx); + const request = { + ...params, + ...(!continuation ? { workspaceId, ...(transferId ? { transferId } : {}) } : {}), + ...(sender ? { from: await sender } : {}), + ...(anywhere ? { anywhere: true } : {}), + } as RpcParams; + for (let retry = 0; ; retry++) { + await waitForFileRequest(sent, signal); + try { + return await askDevice(ctx, device, method, request, signal); + } catch (error) { + const refusal = rpcMessage(error, RpcErrorCode.Forbidden); + if ( + anywhere && + !current && + refusal && + /outside the workspace|goes through a link/.test(refusal) + ) { + throw outsideWorkspaceError(device); + } + const failure = rpcMessage(error, RpcErrorCode.Unavailable); + if ( + retry >= BUSY_RETRIES || + (failure !== FILE_WRITE_BUSY && + !(method === "files.read" && failure === "the host is busy reading files")) + ) + throw error; + } + await delay(BUSY_RETRY_BASE_MS * 2 ** retry, undefined, { signal }); + } + }; +} + +export async function fileTransferTarget( + ctx: CliContext, + options: { device: string; workspace?: string; remotePath: string }, + verb: "pull" | "push", + signal: AbortSignal, +) { + const device = await resolveDevice(ctx, options.device); + if (!device) throw new CliError("give --device to choose a remote computer", 2); + if (!device.online) throw offline(device, ctx.services.clock.now()); + const current = speaks(device.schema, "0.14.0"); + if (verb === "push" && !current) { + throw new CliError( + `"${device.name}" runs an older NexusCode. Update it to use file push (remote protocol 0.14.0 or later)`, + 3, + { error: "older", device: deviceRef(device), method: "files.write.begin" }, + ); + } + if (!device.serves.includes("files")) { + throw new CliError(`"${device.name}" does not share files (Settings › Remote)`); + } + const { workspaces } = await peerSessions(ctx, device, signal); + const workspace = pickPeerWorkspace(device, workspaces, options.workspace); + const rules = filePathRules(workspace.path); + const anywhere = outsideWorkspace(rules, workspace.path, options.remotePath); + if (anywhere && !device.serves.includes("terminal.control")) { + if (!current) throw outsideWorkspaceError(device); + throw new CliError( + `"${device.name}" requires terminal control for file access outside the workspace (Settings › Remote)`, + ); + } + if (verb === "push" && !anywhere && !device.serves.includes("files.write")) { + throw new CliError( + `"${device.name}" requires chat control and agent access for workspace file writes (Settings › Remote)`, + ); + } + return { device, rules, anywhere, peer: filePeer(ctx, device, workspace.id, anywhere) }; +} diff --git a/src/main/features/cli/index.ts b/src/main/features/cli/index.ts index e99d5e9e7..351508cef 100644 --- a/src/main/features/cli/index.ts +++ b/src/main/features/cli/index.ts @@ -16,10 +16,12 @@ import type { CliUiOp, CliUiOps } from "../../../shared/cli/ui"; import { FS_READ_ABSOLUTE_BINARY_METHOD } from "../../../shared/fs/protocol"; import { FileReadBinaryResultSchema, FsStatSchema } from "../../../shared/fs/types"; import { ipcContract } from "../../../shared/ipc/contract"; -import { ipcOk } from "../../../shared/ipc/result"; +import { ipcErr, ipcOk } from "../../../shared/ipc/result"; import { createLogger } from "../../../shared/log/main"; +import { redactLogSecrets } from "../../../shared/log/redact-secrets"; import type { ListeningPort } from "../../../shared/pty/ports"; import type { WorkspaceLocation, WorkspaceMeta } from "../../../shared/types/workspace"; +import type { WorkspaceColor } from "../../../shared/types/workspace-color"; import type { AgentChannel } from "../../infra/agent/channel"; import { register, validateArgs } from "../../infra/ipc-router"; import { getBrowserRegistry, onBrowserTabCreated } from "../browser"; @@ -70,15 +72,22 @@ export interface SetupCliFeatureOptions { agentHost: ActivitySource & { call(method: string, args: unknown): Promise }; workspaceManager: { list(): WorkspaceMeta[]; - create(opts: { location: WorkspaceLocation; name?: string }): WorkspaceMeta; - update(id: string, partial: { pinned?: boolean }): WorkspaceMeta; + create(opts: { + location: WorkspaceLocation; + name?: string; + color?: WorkspaceColor; + }): WorkspaceMeta; + update( + id: string, + partial: { pinned?: boolean; name?: string; color?: WorkspaceColor }, + ): WorkspaceMeta; reorder( id: string, opts: { beforeId?: string; afterId?: string; targetGroup: "pinned" | "unpinned" }, ): WorkspaceMeta; getName(id: string): string | null; tryGetAgentChannel(id: string): Promise; - activate(id: string): Promise; + connect(id: string): Promise; }; claudeBroker: ClaudeStatusBroker; /** The agents of Claude sessions, for nexus agent. */ @@ -167,7 +176,14 @@ export function setupCliFeature(options: SetupCliFeatureOptions): CliFeature { }); const browser = new CdpBrowserDriver((tabId) => { const entry = getBrowserRegistry()?.get(tabId); - return entry && { contents: entry.view.webContents, view: entry.view, active: entry.active }; + return ( + entry && { + contents: entry.view.webContents, + view: entry.view, + active: entry.active, + loadFailure: entry.loadFailure, + } + ); }); const stopWatchingBrowsers = onBrowserTabCreated((tabId, contents) => browser.watch(tabId, contents), @@ -183,12 +199,18 @@ export function setupCliFeature(options: SetupCliFeatureOptions): CliFeature { waits: async () => ipcOk(waits.snapshot()), pickElement: async (args: unknown) => { const { tabId } = validateArgs(ipcContract.cli.call.pickElement.args, args); - const result = await browser.pick(tabId, { timeoutMs: BUTTON_PICK_MS }); - if ("cancelled" in result) return ipcOk({ cancelled: result.cancelled }); - // The click left the keyboard in the page; the agent's terminal is next. - options.getWindow()?.webContents.focus(); - const screenshot = await pickPicture(tabId, result.picked); - return ipcOk({ text: formatPick(result.picked, screenshot, shortId(tabId)) }); + try { + const result = await browser.pick(tabId, { timeoutMs: BUTTON_PICK_MS }); + if ("cancelled" in result) return ipcOk({ cancelled: result.cancelled }); + // The click left the keyboard in the page; the agent's terminal is next. + options.getWindow()?.webContents.focus(); + const screenshot = await pickPicture(tabId, result.picked); + return ipcOk({ text: formatPick(result.picked, screenshot, shortId(tabId)) }); + } catch (error) { + if (error instanceof CliError) + return ipcErr("pick-failed", error.message, { category: "failed" }); + throw error; + } }, cancelPick: async (args: unknown) => { browser.cancelPick(validateArgs(ipcContract.cli.call.cancelPick.args, args).tabId); @@ -210,15 +232,19 @@ export function setupCliFeature(options: SetupCliFeatureOptions): CliFeature { try { return await savePickPicture(tmpdir(), shortId(tabId), picked.png, Date.now()); } catch (error) { - log.warn(`saving a picked element's picture failed: ${(error as Error).message}`); + log.warn( + `saving a picked element's picture failed: ${redactLogSecrets(error instanceof Error ? error.message : String(error))}`, + ); return undefined; } }; const reveal = (workspaceId: string, tabId: string) => { options.focusWindow(); - workspaceManager.activate(workspaceId).catch((error: unknown) => { - log.warn(`activating ${workspaceId} failed: ${(error as Error).message}`); + workspaceManager.connect(workspaceId).catch((error: unknown) => { + log.warn( + `connecting ${workspaceId} failed: ${redactLogSecrets(error instanceof Error ? error.message : String(error))}`, + ); }); options.broadcast("pty", "notificationClick", { workspaceId, tabId }); }; @@ -226,7 +252,7 @@ export function setupCliFeature(options: SetupCliFeatureOptions): CliFeature { const services: CliServices = { appVersion: options.appVersion, listWorkspaces: () => workspaceManager.list(), - addWorkspace: async (location, name) => { + addWorkspace: async (location, name, color) => { const listed = await listedAt(workspaceManager.list(), location); if (listed) return { workspace: listed, added: false }; if (location.kind === "local") { @@ -234,10 +260,15 @@ export function setupCliFeature(options: SetupCliFeatureOptions): CliFeature { if (!found?.isDirectory()) throw new CliError(`no folder at ${location.rootPath}`); } return { - workspace: workspaceManager.create({ location, ...(name ? { name } : {}) }), + workspace: workspaceManager.create({ + location, + ...(name ? { name } : {}), + ...(color ? { color } : {}), + }), added: true, }; }, + editWorkspace: (id, edit) => workspaceManager.update(id, edit), pinWorkspace: (id, pinned) => workspaceManager.update(id, { pinned }), moveWorkspace: (id, { group, ...anchor }) => workspaceManager.reorder(id, { ...anchor, targetGroup: group }), @@ -258,12 +289,12 @@ export function setupCliFeature(options: SetupCliFeatureOptions): CliFeature { remote: { inRoom: () => options.remote?.()?.inRoom() ?? false, devices: async () => (await options.remote?.()?.devices()) ?? [], - request: (deviceId, method, params, timeoutMs) => { + request: (deviceId, method, params, timeoutMs, signal) => { const remote = options.remote?.(); if (!remote) { throw new CliError("this computer is not in the remote room (Settings › Remote)", 3); } - return remote.request(deviceId, method, params, timeoutMs); + return remote.request(deviceId, method, params, timeoutMs, signal); }, }, consents, @@ -358,7 +389,9 @@ export function setupCliFeature(options: SetupCliFeatureOptions): CliFeature { if (!controller.signal.aborted) return respond(workspaceId, requestId, result); }) .catch((error: unknown) => { - log.warn(`cli request ${requestId} failed: ${(error as Error).message}`); + log.warn( + `cli request ${requestId} failed: ${redactLogSecrets(error instanceof Error ? error.message : String(error))}`, + ); }) .finally(() => running.delete(requestId)); }); diff --git a/src/main/features/cli/launchers.ts b/src/main/features/cli/launchers.ts index 3c7c74cfb..8f8565611 100644 --- a/src/main/features/cli/launchers.ts +++ b/src/main/features/cli/launchers.ts @@ -13,6 +13,7 @@ import fs from "node:fs"; import path from "node:path"; import { cliExecutableName, windowsGitBashLauncher } from "../../../shared/cli/launcher"; import { createLogger } from "../../../shared/log/main"; +import { redactLogSecrets } from "../../../shared/log/redact-secrets"; import { getAgentBinaryPath } from "../../infra/agent/getAgentBinDir"; import { binDir, root } from "../../infra/agent/runtimeDirs"; @@ -91,7 +92,9 @@ export function windowsCliLauncherDir(): string | null { writeWindowsCli(agent, dir, binDir()); written = dir; } catch (error) { - log.warn(`writing the nexus command failed: ${(error as Error).message}`); + log.warn( + `writing the nexus command failed: ${redactLogSecrets(error instanceof Error ? error.message : String(error))}`, + ); written = null; } return written; diff --git a/src/main/features/cli/notifier.ts b/src/main/features/cli/notifier.ts index 47f87565b..e2c8d7f16 100644 --- a/src/main/features/cli/notifier.ts +++ b/src/main/features/cli/notifier.ts @@ -4,6 +4,7 @@ * when notifications are off, and a click brings the user to the terminal. */ import { createLogger } from "../../../shared/log/main"; +import { redactLogSecrets } from "../../../shared/log/redact-secrets"; const log = createLogger("cli-notify"); @@ -41,7 +42,9 @@ export function notifyTab( try { deps.reveal(workspaceId, tabId); } catch (error) { - log.warn(`revealing the tab failed: ${(error as Error).message}`); + log.warn( + `revealing the tab failed: ${redactLogSecrets(error instanceof Error ? error.message : String(error))}`, + ); } }); n.on("close", release); diff --git a/src/main/features/cli/paths.ts b/src/main/features/cli/paths.ts index 7bae99e84..eaae72e56 100644 --- a/src/main/features/cli/paths.ts +++ b/src/main/features/cli/paths.ts @@ -10,6 +10,12 @@ import { toSlashPath } from "../../../shared/fs/drive-path"; import type { WorkspaceMeta } from "../../../shared/types/workspace"; import { type CliContext, CliError } from "./context"; +/** Whether the caller's shell paths refer to the app's own filesystem. */ +export function onAppMachine(ctx: CliContext): boolean { + const own = ctx.services.listWorkspaces().find((w) => w.id === ctx.caller.workspaceId); + return own?.location.kind === "local"; +} + /** The path rules of the host `absolute` is a path on. */ function rulesOf(absolute: string): path.PlatformPath { return /^(?:[A-Za-z]:[\\/]|\\\\)/.test(absolute) ? path.win32 : path.posix; diff --git a/src/main/features/cli/registry.ts b/src/main/features/cli/registry.ts index e5858c1b7..dd887afd4 100644 --- a/src/main/features/cli/registry.ts +++ b/src/main/features/cli/registry.ts @@ -5,6 +5,7 @@ */ import type { CliFile, CliResult } from "../../../shared/cli/protocol"; import { createLogger } from "../../../shared/log/main"; +import { redactLogSecrets } from "../../../shared/log/redact-secrets"; import { CliUsageError, type FlagSpec, GLOBAL_FLAGS, type ParsedArgs, parseArgs } from "./args"; import { type CliContext, CliError } from "./context"; @@ -56,7 +57,8 @@ function usageLine(command: CliCommand): string { function flagLines(flags: Record): string[] { const rows = Object.entries(flags).map(([name, spec]) => { const value = spec.type === "boolean" ? "" : ` ${spec.value ?? spec.type.toUpperCase()}`; - return [`--${name}${value}`, spec.description] as const; + const short = spec.short ? `-${spec.short}, ` : ""; + return [`${short}--${name}${value}`, spec.description] as const; }); const width = Math.max(0, ...rows.map(([left]) => left.length)); return rows.map(([left, right]) => ` ${left.padEnd(width)} ${right}`); @@ -177,7 +179,9 @@ export async function runCli(commands: readonly CliCommand[], ctx: CliContext): error.exitCode, ); } - log.warn(`nexus ${command.path.join(" ")} failed: ${(error as Error).message}`); + log.warn( + `nexus ${command.path.join(" ")} failed: ${redactLogSecrets(error instanceof Error ? error.message : String(error))}`, + ); return result("", `nexus: ${(error as Error).message}\n`, 1); } } diff --git a/src/main/features/cli/remote.ts b/src/main/features/cli/remote.ts index c6f990c57..12692b5e4 100644 --- a/src/main/features/cli/remote.ts +++ b/src/main/features/cli/remote.ts @@ -6,6 +6,7 @@ import { AGENT_CONSENT_MS, type AgentSender, + MAX_FILE_WRITE_BYTES, MAX_WAIT_MS, type RemoteSession, type RemoteWorkspace, @@ -14,6 +15,7 @@ import { type RpcParams, type RpcResult, } from "../../../../packages/remote-protocol/src"; +import { fileVerifyTimeoutMs, isFileTransferMethod, needsFileConsent } from "../remote/file-io"; import { type CliContext, type CliDevice, CliError, pick } from "./context"; import { ago } from "./format"; import { senderOf } from "./sender"; @@ -31,6 +33,8 @@ export const NOT_IN_ROOM = "this computer is not in the remote room (Settings const AGENT_REQUEST_MS = AGENT_CONSENT_MS + 10_000; /** How long a wait's request waits: its own time and the way there and back. */ const WAIT_REQUEST_MS = MAX_WAIT_MS + 5_000; +// The peer may need to verify a maximum-size upload and then commit it. +const FILE_WRITE_END_REQUEST_MS = fileVerifyTimeoutMs(MAX_FILE_WRITE_BYTES) + 35_000; /** A device as JSON names it beside what came from it. */ export function deviceRef(device: CliDevice): { id: string; name: string } { @@ -197,7 +201,9 @@ export async function askDevice( device: CliDevice, method: M, given: RpcParams, + signal?: AbortSignal, ): Promise> { + signal?.throwIfAborted(); const now = ctx.services.clock.now(); if (!device.online) throw offline(device, now); // A read says who reads: a device whose user turned agents down is read by people alone. @@ -221,13 +227,21 @@ export async function askDevice( const timeoutMs = method === "chat.wait" || method === "terminal.wait" ? WAIT_REQUEST_MS - : from && !reads - ? AGENT_REQUEST_MS - : undefined; + : method === "files.write.end" + ? FILE_WRITE_END_REQUEST_MS + : from && + !reads && + (!isFileTransferMethod(method) || + needsFileConsent(method, params as { anywhere?: true })) + ? AGENT_REQUEST_MS + : undefined; try { - return await ctx.services.remote.request(device.id, method, params, timeoutMs); + return await ctx.services.remote.request(device.id, method, params, timeoutMs, signal); } catch (error) { - throw failureOf(device, method, error, ctx.services.clock.now()); + signal?.throwIfAborted(); + const failure = failureOf(device, method, error, ctx.services.clock.now()); + if (failure !== error) failure.cause = error; + throw failure; } } @@ -235,8 +249,9 @@ export async function askDevice( export async function peerSessions( ctx: CliContext, device: CliDevice, + signal?: AbortSignal, ): Promise<{ workspaces: RemoteWorkspace[]; sessions: RemoteSession[] }> { - return askDevice(ctx, device, "sessions.list", {}); + return askDevice(ctx, device, "sessions.list", {}, signal); } /** The workspace a --workspace flag names on `device`; "." is no workspace there. */ diff --git a/src/main/features/cli/renderer-bridge.ts b/src/main/features/cli/renderer-bridge.ts index 5628526e3..ff7c9b49a 100644 --- a/src/main/features/cli/renderer-bridge.ts +++ b/src/main/features/cli/renderer-bridge.ts @@ -33,7 +33,11 @@ export class RendererBridge { timeoutMs, onTimeout: () => new CliError("NexusCode's window did not answer"), }); - win.webContents.send("ipc:event", "cli", "uiRequest", { requestId, op, args }); + try { + win.webContents.send("ipc:event", "cli", "uiRequest", { requestId, op, args }); + } catch (error) { + this.pending.reject(requestId, error instanceof Error ? error : new Error(String(error))); + } const response = await answer; if (!response.ok) throw new CliError(response.error ?? `${op} failed`); return response.value as CliUiOps[K]["result"]; diff --git a/src/main/features/fs/bridge/agent-watch.ts b/src/main/features/fs/bridge/agent-watch.ts index d37231673..a072c618f 100644 --- a/src/main/features/fs/bridge/agent-watch.ts +++ b/src/main/features/fs/bridge/agent-watch.ts @@ -3,179 +3,209 @@ import { FsChangeSchema } from "../../../../shared/fs/types"; import { createLogger } from "../../../../shared/log/main"; import type { BroadcastFn, WorkspaceManager } from "../../workspace/manager"; import { type AgentBackedProvider, isAgentBackedProvider } from "./provider"; +import { enqueueWatchOperation, subscribeAgentWatch } from "./watch-lifecycle"; const log = createLogger("fs"); - -const AgentFsChangedPayloadSchema = z.object({ - changes: z.array(FsChangeSchema), -}); +const AgentFsChangedPayloadSchema = z.object({ changes: z.array(FsChangeSchema) }); interface WatchSubscription { readonly provider: AgentBackedProvider; readonly unsubscribe: () => void; - readonly watchedRelPaths: Set; } -/** - * Bridges agent-emitted fs.changed events into renderer IPC events. - * - * The OS watcher itself lives inside the Go agent. This class only owns - * per-workspace event subscription and workspaceId fan-out. - */ +interface WorkspaceWatches { + readonly paths: Set; + readonly pending: Map>; + subscription?: WatchSubscription; +} + +/** Owns each workspace's desired paths and forwards changes from its current agent. */ export class AgentFsWatcher { - private readonly subscriptions = new Map(); + private readonly workspaces = new Map(); private readonly disposeProviderReady: () => void; + private disposed = false; constructor( private readonly manager: WorkspaceManager, private readonly broadcast: BroadcastFn, ) { - // After the channel dies, the next boot brings up a new agent with no - // watches, and the listeners on the old provider never fire again. Move - // existing subscriptions over as soon as the replacement is up rather - // than waiting for the renderer to watch again, which it does not do for - // directories that were already expanded. this.disposeProviderReady = manager.onProviderReady((workspaceId, provider) => { - if (this.subscriptions.has(workspaceId) && isAgentBackedProvider(provider)) { - this.ensureSubscription(workspaceId, provider); - } + const state = this.workspaces.get(workspaceId); + if (state && isAgentBackedProvider(provider)) this.bind(workspaceId, state, provider); }); } + /** Tracks a requested path before its RPC completes so teardown and rebind can see it. */ async watch(workspaceId: string, relPath: string): Promise { - const provider = await this.requireReadyAgentProvider(workspaceId); - const subscription = this.ensureSubscription(workspaceId, provider); - await provider.callAgentMethod("fs.watch", { relPath }); - subscription.watchedRelPaths.add(relPath); + const state = this.state(workspaceId); + await this.enqueue(workspaceId, state, relPath, async () => { + if (this.workspaces.get(workspaceId) !== state) return; + const provider = await this.requireReadyAgentProvider(workspaceId); + if (this.workspaces.get(workspaceId) !== state) return; + const subscription = this.bind(workspaceId, state, provider); + const wasWatched = state.paths.has(relPath); + state.paths.add(relPath); + try { + await provider.callAgentMethod("fs.watch", { relPath }); + } catch (error) { + if (!wasWatched && state.subscription === subscription) { + state.paths.delete(relPath); + if ((error as { code?: string }).code === "transport.timeout") { + await this.cleanup(workspaceId, provider, relPath); + } + } + throw error; + } + if (state.subscription !== subscription) { + await this.cleanup(workspaceId, provider, relPath); + } + }); } + /** Stops a path after any earlier watch or replay of that same path. */ async unwatch(workspaceId: string, relPath: string): Promise { - const provider = await this.requireReadyAgentProvider(workspaceId); - await provider.callAgentMethod("fs.unwatch", { relPath }); - - const subscription = this.subscriptions.get(workspaceId); - subscription?.watchedRelPaths.delete(relPath); - if (subscription && subscription.watchedRelPaths.size === 0) { - subscription.unsubscribe(); - this.subscriptions.delete(workspaceId); - } + const state = this.state(workspaceId); + await this.enqueue(workspaceId, state, relPath, async () => { + if (this.workspaces.get(workspaceId) !== state) return; + state.paths.delete(relPath); + const provider = await this.requireReadyAgentProvider(workspaceId); + if (this.workspaces.get(workspaceId) !== state) return; + if (state.paths.size > 0) this.bind(workspaceId, state, provider); + await provider.callAgentMethod("fs.unwatch", { relPath }); + }); } + /** Detaches the listener immediately and clears watches after pending registrations settle. */ disposeWorkspace(workspaceId: string): void { - const subscription = this.subscriptions.get(workspaceId); + const state = this.workspaces.get(workspaceId); + if (!state) return; + this.workspaces.delete(workspaceId); + const subscription = state.subscription; if (!subscription) return; subscription.unsubscribe(); - this.subscriptions.delete(workspaceId); + for (const relPath of state.paths) { + void this.enqueue(workspaceId, state, relPath, () => + this.cleanup(workspaceId, subscription.provider, relPath), + ); + } } + /** Stops new subscriptions and drops every workspace. */ dispose(): void { + this.disposed = true; this.disposeProviderReady(); - for (const subscription of this.subscriptions.values()) { - subscription.unsubscribe(); - } - this.subscriptions.clear(); + for (const workspaceId of this.workspaces.keys()) this.disposeWorkspace(workspaceId); } - /** - * Returns the workspace's subscription on `provider`. One made on an - * earlier provider is moved over: its listeners are dropped and its paths - * are registered with the new agent. - */ - private ensureSubscription( - workspaceId: string, - provider: AgentBackedProvider, - ): WatchSubscription { - const existing = this.subscriptions.get(workspaceId); - if (existing?.provider === provider) return existing; - - existing?.unsubscribe(); - const subscription = this.subscribe(workspaceId, provider); - if (existing) { - for (const relPath of existing.watchedRelPaths) { - subscription.watchedRelPaths.add(relPath); - } - this.replayWatches(workspaceId, provider, subscription); - } - return subscription; + /** Finds or creates the state before awaiting a provider. */ + private state(workspaceId: string): WorkspaceWatches { + if (this.disposed) throw new Error("file watcher is disposed"); + const existing = this.workspaces.get(workspaceId); + if (existing) return existing; + const state = { paths: new Set(), pending: new Map>() }; + this.workspaces.set(workspaceId, state); + return state; } - private subscribe(workspaceId: string, provider: AgentBackedProvider): WatchSubscription { - const unsubscribeChanged = provider.onAgentEvent("fs.changed", (payload) => { - const parsed = AgentFsChangedPayloadSchema.safeParse(payload); - if (!parsed.success || parsed.data.changes.length === 0) return; - this.broadcast("fs", "changed", { - workspaceId, - changes: parsed.data.changes, - }); + /** Orders RPCs for one path while allowing other directories to progress independently. */ + private enqueue( + workspaceId: string, + state: WorkspaceWatches, + relPath: string, + run: () => Promise, + ): Promise { + return enqueueWatchOperation(state.pending, relPath, run, () => { + if ( + this.workspaces.get(workspaceId) === state && + state.paths.size === 0 && + state.pending.size === 0 + ) { + state.subscription?.unsubscribe(); + this.workspaces.delete(workspaceId); + } }); + } - // Watch registrations live in the agent process, so a respawned agent - // starts with zero watches even though the channel recovered - // transparently. Replay the full set on the `ready` lifecycle event - // (successful reconnect handshake) — re-registering an existing watch is - // a no-op on the agent side, so the replay is safe even when the agent - // actually survived. - const unsubscribeLifecycle = provider.onAgentLifecycle((event) => { - if (event.type !== "ready") return; - const subscription = this.subscriptions.get(workspaceId); - if (!subscription) return; - this.replayWatches(workspaceId, provider, subscription); - }); + /** Moves the listener and desired paths to the new provider. */ + private bind( + workspaceId: string, + state: WorkspaceWatches, + provider: AgentBackedProvider, + ): WatchSubscription { + const previous = state.subscription; + if (previous?.provider === provider) return previous; + previous?.unsubscribe(); - const subscription = { + const unsubscribe = subscribeAgentWatch( provider, - unsubscribe: () => { - unsubscribeChanged(); - unsubscribeLifecycle(); + "fs.changed", + () => this.workspaces.get(workspaceId) === state && state.subscription === subscription, + (payload) => { + const parsed = AgentFsChangedPayloadSchema.safeParse(payload); + if (!parsed.success || parsed.data.changes.length === 0) return; + this.broadcast("fs", "changed", { workspaceId, changes: parsed.data.changes }); }, - watchedRelPaths: new Set(), - }; - this.subscriptions.set(workspaceId, subscription); + () => this.replay(workspaceId, state, subscription), + ); + const subscription = { provider, unsubscribe }; + state.subscription = subscription; + if (previous) this.replay(workspaceId, state, subscription); return subscription; } - /** - * Registers every watched path with an agent that does not have them. - */ - private replayWatches( + /** Restores still-wanted paths and refreshes directories changed during the outage. */ + private replay( workspaceId: string, - provider: AgentBackedProvider, + state: WorkspaceWatches, subscription: WatchSubscription, ): void { - for (const relPath of subscription.watchedRelPaths) { - provider.callAgentMethod("fs.watch", { relPath }).catch((error: unknown) => { + for (const relPath of state.paths) { + void this.enqueue(workspaceId, state, relPath, async () => { + if ( + this.workspaces.get(workspaceId) !== state || + state.subscription !== subscription || + !state.paths.has(relPath) + ) { + return; + } + await subscription.provider.callAgentMethod("fs.watch", { relPath }); + if (state.subscription !== subscription) { + await this.cleanup(workspaceId, subscription.provider, relPath); + } + }).catch((error: unknown) => { log.warn( `fs.watch replay failed (workspace=${workspaceId}, relPath=${relPath}): ${(error as Error).message}`, ); }); } - // Changes made while the agent was down were never reported. Replay - // every watched directory as "added" so the renderer re-reads each one - // (the file tree treats an added directory as recreated) and its - // fs.changed hint refreshes git status. - if (subscription.watchedRelPaths.size > 0) { + if (state.paths.size > 0) { this.broadcast("fs", "changed", { workspaceId, - changes: [...subscription.watchedRelPaths].map((relPath) => ({ - relPath, - kind: "added" as const, - })), + changes: [...state.paths].map((relPath) => ({ relPath, kind: "added" as const })), }); } } - /** - * Resolves the workspace's agent-backed fs provider only after the - * underlying channel is ready. Without this gate, fs.watch IPC that - * arrives before SSH bootstrap or local channel boot completes would - * subscribe against the inert provider returned by createInitialFsProvider - * and throw `channel not yet wired` from the channel accessor. - */ + /** Releases a retired path on the provider that received its watch. */ + private async cleanup( + workspaceId: string, + provider: AgentBackedProvider, + relPath: string, + ): Promise { + try { + await provider.callAgentMethod("fs.unwatch", { relPath }); + } catch (error) { + log.warn( + `fs.unwatch cleanup failed (workspace=${workspaceId}, relPath=${relPath}): ${(error as Error).message}`, + ); + } + } + + /** Waits for the live agent instead of using the inert provider during bootstrap. */ private async requireReadyAgentProvider(workspaceId: string): Promise { const provider = await this.manager.getFs(workspaceId); - if (!isAgentBackedProvider(provider)) { + if (!isAgentBackedProvider(provider)) throw new Error("workspace agent provider is not available"); - } return provider; } } diff --git a/src/main/features/fs/bridge/watch-lifecycle.ts b/src/main/features/fs/bridge/watch-lifecycle.ts new file mode 100644 index 000000000..f9a955b4c --- /dev/null +++ b/src/main/features/fs/bridge/watch-lifecycle.ts @@ -0,0 +1,37 @@ +import type { AgentBackedProvider } from "./provider"; + +/** Binds change and reconnect signals only while their provider registration is current. */ +export function subscribeAgentWatch( + provider: AgentBackedProvider, + eventName: string, + isCurrent: () => boolean, + onChanged: (payload: unknown) => void, + onReady: () => void, +): () => void { + const offChanged = provider.onAgentEvent(eventName, (payload) => { + if (isCurrent()) onChanged(payload); + }); + const offLifecycle = provider.onAgentLifecycle((event) => { + if (event.type === "ready" && isCurrent()) onReady(); + }); + return () => { + offChanged(); + offLifecycle(); + }; +} + +/** Runs one watch operation after any earlier operation for the same key. */ +export function enqueueWatchOperation( + pending: Map>, + key: Key, + run: () => Promise, + onSettled?: () => void, +): Promise { + const previous = pending.get(key); + const operation = (previous ? previous.then(run, run) : run()).finally(() => { + if (pending.get(key) === operation) pending.delete(key); + onSettled?.(); + }); + pending.set(key, operation); + return operation; +} diff --git a/src/main/features/fs/ipc/fs-result.ts b/src/main/features/fs/ipc/fs-result.ts index 19a3c0298..205275ed6 100644 --- a/src/main/features/fs/ipc/fs-result.ts +++ b/src/main/features/fs/ipc/fs-result.ts @@ -1,45 +1,14 @@ -/** - * Shared result helpers for fs IPC handlers. - * - * Why this exists: - * The Electron `ipcMain.handle` invocation logger prints - * "Error occurred in handler for 'ipc:call': …" - * for every rejected handler promise. That is correct for genuine - * bugs, but noisy for *expected* filesystem outcomes — for example, a - * `readdir` issued during tree-restore against a folder the user has - * since deleted on disk. We turn agent-thrown fs errors into a typed - * `ipcErr` envelope so the router returns them silently. The renderer - * side is unchanged: `unwrapIpcResult` still throws an `Error("CODE: …")` - * that `hasFsErrorCode` can classify. - * - * This mirrors the long-standing pattern in `git/ipc/git-result.ts`. - */ - -import { FS_ERROR } from "../../../../shared/fs/errors"; -import { type IpcErrResult, ipcErr } from "../../../../shared/ipc/result"; - -export const FS_IPC_ERROR_KIND = "fs-error" as const; -export type FsIpcErrorResult = IpcErrResult; - -/** - * True when the error's message starts with one of our known FS_ERROR codes - * (`"NOT_FOUND: /path"`, `"ALREADY_EXISTS: /path"`, …). Used to keep genuine - * unexpected errors flowing to the router-level logger. - */ -export function isFsError(error: unknown): boolean { - if (!(error instanceof Error)) return false; - for (const code of Object.values(FS_ERROR)) { - if (error.message.startsWith(`${code}:`)) return true; +import { type IpcResult, ipcOk } from "../../../../shared/ipc/result"; +import { agentCallError } from "../../../infra/ipc-router/agent-errors"; +import { fsCallError } from "../../../infra/ipc-router/fs-errors"; + +/** Envelopes a file operation, including failures while resolving its provider. */ +export async function runFsCall(run: () => Promise): Promise> { + try { + return ipcOk(await run()); + } catch (error) { + const expected = fsCallError(error) ?? agentCallError(error); + if (expected) return expected; + throw error; } - return false; -} - -/** - * Convert a thrown fs error into a typed `ipcErr` envelope. The original - * message is preserved verbatim so `hasFsErrorCode` keeps working on the - * renderer side. - */ -export function fsErrorToIpcResult(error: unknown): FsIpcErrorResult { - const message = error instanceof Error ? error.message : String(error); - return ipcErr(FS_IPC_ERROR_KIND, message); } diff --git a/src/main/features/fs/ipc/index.ts b/src/main/features/fs/ipc/index.ts index 41ad8ecee..08a5cead7 100644 --- a/src/main/features/fs/ipc/index.ts +++ b/src/main/features/fs/ipc/index.ts @@ -40,6 +40,7 @@ export function registerFsChannel( storage: WorkspaceStorage, ): void { register("fs", { + keepOnRendererClose: ["writeFile", "createFile"], call: { readdir: readdirHandler(manager), stat: statHandler(manager), diff --git a/src/main/features/fs/ipc/read-handlers.ts b/src/main/features/fs/ipc/read-handlers.ts index b4d12e220..ad6cf5be6 100644 --- a/src/main/features/fs/ipc/read-handlers.ts +++ b/src/main/features/fs/ipc/read-handlers.ts @@ -17,10 +17,10 @@ import type { } from "../../../../shared/fs/types"; import { ipcContract } from "../../../../shared/ipc/contract"; import { validateArgs } from "../../../infra/ipc-router"; +import { type FsIpcErrorResult, fsCallError } from "../../../infra/ipc-router/fs-errors"; import type { WorkspaceManager } from "../../workspace/manager"; import type { FsProvider } from "../bridge/provider"; import { isOutsideGranted, resolveOutsideGrant, setOutsideGrant } from "../outside-grants"; -import { type FsIpcErrorResult, fsErrorToIpcResult, isFsError } from "./fs-result"; const c = ipcContract.fs.call; @@ -33,7 +33,8 @@ export function readdirHandler( try { return await fs.readdir(relPath, { includeHidden }); } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); + const expected = fsCallError(error); + if (expected) return expected; throw error; } }; @@ -48,7 +49,8 @@ export function statHandler( try { return await fs.stat(relPath); } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); + const expected = fsCallError(error); + if (expected) return expected; throw error; } }; diff --git a/src/main/features/fs/ipc/watch-handlers.ts b/src/main/features/fs/ipc/watch-handlers.ts index 8ead5081e..25281d4a7 100644 --- a/src/main/features/fs/ipc/watch-handlers.ts +++ b/src/main/features/fs/ipc/watch-handlers.ts @@ -1,49 +1,22 @@ -/** - * fs watch handlers — watch / unwatch lifecycle. - * - * Both handlers catch agent-thrown fs errors and return them as a typed - * `ipcErr` envelope (matches the read-handlers pattern). The motivating - * case is `ensureRoot`'s hydration loop: every persisted expanded path - * triggers a `fs.watch` call, and if the user has deleted the folder on - * disk between sessions the agent throws NOT_FOUND. Without this - * envelope the rejection reaches Electron's `ipcMain.handle` invocation - * logger, which prints - * "Error occurred in handler for 'ipc:call': Error: NOT_FOUND: …" - * on every stale row. The renderer side is unchanged — `unwrapIpcResult` - * still throws the same `"CODE: …"` message that `hasFsErrorCode` can - * classify — so call sites that previously surfaced toasts keep working. - */ import { ipcContract } from "../../../../shared/ipc/contract"; import { validateArgs } from "../../../infra/ipc-router"; import type { AgentFsWatcher } from "../bridge/agent-watch"; -import { type FsIpcErrorResult, fsErrorToIpcResult, isFsError } from "./fs-result"; +import { runFsCall } from "./fs-result"; const c = ipcContract.fs.call; -export function watchHandler( - watcher: AgentFsWatcher, -): (args: unknown) => Promise { - return async (args: unknown): Promise => { +/** Registers a directory watch and returns any expected failure in the call envelope. */ +export function watchHandler(watcher: AgentFsWatcher) { + return (args: unknown) => { const { workspaceId, relPath } = validateArgs(c.watch.args, args); - try { - await watcher.watch(workspaceId, relPath); - } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); - throw error; - } + return runFsCall(() => watcher.watch(workspaceId, relPath)); }; } -export function unwatchHandler( - watcher: AgentFsWatcher, -): (args: unknown) => Promise { - return async (args: unknown): Promise => { +/** Removes a directory watch through the same call contract. */ +export function unwatchHandler(watcher: AgentFsWatcher) { + return (args: unknown) => { const { workspaceId, relPath } = validateArgs(c.unwatch.args, args); - try { - await watcher.unwatch(workspaceId, relPath); - } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); - throw error; - } + return runFsCall(() => watcher.unwatch(workspaceId, relPath)); }; } diff --git a/src/main/features/fs/ipc/write-handlers.ts b/src/main/features/fs/ipc/write-handlers.ts index f05ccd034..86cd61fea 100644 --- a/src/main/features/fs/ipc/write-handlers.ts +++ b/src/main/features/fs/ipc/write-handlers.ts @@ -1,146 +1,95 @@ -/** - * Write fs handlers — thin IPC adapters over the workspace's agent-backed - * filesystem provider. - * - * Every handler catches agent-thrown fs errors (ALREADY_EXISTS, NOT_FOUND, - * PERMISSION_DENIED, IS_DIRECTORY, NOT_EMPTY, …) and returns them as a typed - * `ipcErr` envelope instead of letting them throw. Without this, Electron's - * `ipcMain.handle` invocation logger prints - * "Error occurred in handler for 'ipc:call': Error: ALREADY_EXISTS: …" - * on every expected user-facing failure (creating a name that already exists, - * renaming to a colliding name, deleting a path the watcher hasn't dropped - * yet, …) — duplicating the toast we already raise in the renderer. The - * renderer side is unchanged: `unwrapIpcResult` rethrows the envelope as an - * `Error("CODE: …")` so `toFsToast` keeps classifying it. - * - * Mirrors the read/watch handlers, which have used this wrap since they were - * introduced. - */ - -import type { WriteFileResult } from "../../../../shared/fs/types"; +/** Write adapters envelope both provider startup and the agent operation. */ import { ipcContract } from "../../../../shared/ipc/contract"; import { validateArgs } from "../../../infra/ipc-router"; import type { WorkspaceManager } from "../../workspace/manager"; -import { type FsIpcErrorResult, fsErrorToIpcResult, isFsError } from "./fs-result"; +import { runFsCall } from "./fs-result"; const c = ipcContract.fs.call; -export function writeFileHandler( - manager: WorkspaceManager, -): (args: unknown) => Promise { - return async (args: unknown): Promise => { +/** Runs the agent's writeFile operation on the selected workspace. */ +export function writeFileHandler(manager: WorkspaceManager) { + return (args: unknown) => { const { workspaceId, relPath, content, expected, bytes } = validateArgs(c.writeFile.args, args); - const fs = await manager.getFs(workspaceId); - try { - return await fs.writeFile(relPath, content, expected, bytes); - } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); - throw error; - } + return runFsCall(async () => { + const fs = await manager.getFs(workspaceId); + return fs.writeFile(relPath, content, expected, bytes); + }); }; } -export function createFileHandler( - manager: WorkspaceManager, -): (args: unknown) => Promise { - return async (args: unknown): Promise => { +/** Runs the agent's createFile operation on the selected workspace. */ +export function createFileHandler(manager: WorkspaceManager) { + return (args: unknown) => { const { workspaceId, relPath } = validateArgs(c.createFile.args, args); - const fs = await manager.getFs(workspaceId); - try { - await fs.createFile(relPath); - } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); - throw error; - } + return runFsCall(async () => { + const fs = await manager.getFs(workspaceId); + return fs.createFile(relPath); + }); }; } -export function mkdirHandler( - manager: WorkspaceManager, -): (args: unknown) => Promise { - return async (args: unknown): Promise => { +/** Runs the agent's mkdir operation on the selected workspace. */ +export function mkdirHandler(manager: WorkspaceManager) { + return (args: unknown) => { const { workspaceId, relPath, recursive } = validateArgs(c.mkdir.args, args); - const fs = await manager.getFs(workspaceId); - try { - await fs.mkdir(relPath, recursive); - } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); - throw error; - } + return runFsCall(async () => { + const fs = await manager.getFs(workspaceId); + return fs.mkdir(relPath, recursive); + }); }; } -export function unlinkHandler( - manager: WorkspaceManager, -): (args: unknown) => Promise { - return async (args: unknown): Promise => { +/** Runs the agent's unlink operation on the selected workspace. */ +export function unlinkHandler(manager: WorkspaceManager) { + return (args: unknown) => { const { workspaceId, relPath } = validateArgs(c.unlink.args, args); - const fs = await manager.getFs(workspaceId); - try { - await fs.unlink(relPath); - } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); - throw error; - } + return runFsCall(async () => { + const fs = await manager.getFs(workspaceId); + return fs.unlink(relPath); + }); }; } -export function rmdirHandler( - manager: WorkspaceManager, -): (args: unknown) => Promise { - return async (args: unknown): Promise => { +/** Runs the agent's rmdir operation on the selected workspace. */ +export function rmdirHandler(manager: WorkspaceManager) { + return (args: unknown) => { const { workspaceId, relPath } = validateArgs(c.rmdir.args, args); - const fs = await manager.getFs(workspaceId); - try { - await fs.rmdir(relPath); - } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); - throw error; - } + return runFsCall(async () => { + const fs = await manager.getFs(workspaceId); + return fs.rmdir(relPath); + }); }; } -export function renameHandler( - manager: WorkspaceManager, -): (args: unknown) => Promise { - return async (args: unknown): Promise => { +/** Runs the agent's rename operation on the selected workspace. */ +export function renameHandler(manager: WorkspaceManager) { + return (args: unknown) => { const { workspaceId, fromRelPath, toRelPath, overwrite } = validateArgs(c.rename.args, args); - const fs = await manager.getFs(workspaceId); - try { - await fs.rename(fromRelPath, toRelPath, overwrite); - } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); - throw error; - } + return runFsCall(async () => { + const fs = await manager.getFs(workspaceId); + return fs.rename(fromRelPath, toRelPath, overwrite); + }); }; } -export function copyFileHandler( - manager: WorkspaceManager, -): (args: unknown) => Promise { - return async (args: unknown): Promise => { +/** Runs the agent's copyFile operation on the selected workspace. */ +export function copyFileHandler(manager: WorkspaceManager) { + return (args: unknown) => { const { workspaceId, fromRelPath, toRelPath, overwrite } = validateArgs(c.copyFile.args, args); - const fs = await manager.getFs(workspaceId); - try { - await fs.copyFile(fromRelPath, toRelPath, overwrite); - } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); - throw error; - } + return runFsCall(async () => { + const fs = await manager.getFs(workspaceId); + return fs.copyFile(fromRelPath, toRelPath, overwrite); + }); }; } -export function removeAllHandler( - manager: WorkspaceManager, -): (args: unknown) => Promise { - return async (args: unknown): Promise => { +/** Runs the agent's removeAll operation on the selected workspace. */ +export function removeAllHandler(manager: WorkspaceManager) { + return (args: unknown) => { const { workspaceId, relPath } = validateArgs(c.removeAll.args, args); - const fs = await manager.getFs(workspaceId); - try { - await fs.removeAll(relPath); - } catch (error) { - if (isFsError(error)) return fsErrorToIpcResult(error); - throw error; - } + return runFsCall(async () => { + const fs = await manager.getFs(workspaceId); + return fs.removeAll(relPath); + }); }; } diff --git a/src/main/features/git/bridge/agent-executor/index.ts b/src/main/features/git/bridge/agent-executor/index.ts index ed1e03032..0531b8b8b 100644 --- a/src/main/features/git/bridge/agent-executor/index.ts +++ b/src/main/features/git/bridge/agent-executor/index.ts @@ -291,6 +291,7 @@ export class AgentGitExecutor implements GitExecutor { env: normalizeEnv(options.env), interactive: options.interactive ?? false, streamStderr: options.streamStderr === true ? true : undefined, + stdoutCapBytes: options.stdoutCapBytes, }, options.interactive ? INTERACTIVE_CALL_OPTIONS : undefined, ) diff --git a/src/main/features/git/bridge/agent-watch.ts b/src/main/features/git/bridge/agent-watch.ts index 21d52fa86..d634c9fe9 100644 --- a/src/main/features/git/bridge/agent-watch.ts +++ b/src/main/features/git/bridge/agent-watch.ts @@ -6,6 +6,7 @@ import { } from "../../../../shared/git/protocol"; import { createLogger } from "../../../../shared/log/main"; import { type AgentBackedProvider, isAgentBackedProvider } from "../../fs/bridge/provider"; +import { enqueueWatchOperation, subscribeAgentWatch } from "../../fs/bridge/watch-lifecycle"; import type { WorkspaceManager } from "../../workspace/manager"; const log = createLogger("git"); @@ -16,29 +17,29 @@ interface GitWatchEntry { readonly gitDir: string; readonly provider: AgentBackedProvider; readonly unsubscribe: () => void; + ready: Promise; + registering: boolean; } -/** - * Bridges Go-agent git.changed events into the existing status coalescer. - * The actual .git directory watcher lives in the agent, so this works for - * local and SSH workspaces through the same provider channel. - */ +/** Owns each workspace's agent-side Git watch and forwards changes to the coalescer. */ export class AgentGitWatcher { private readonly entries = new Map(); + private readonly pending = new Map>(); private readonly disposeProviderReady: () => void; + private disposed = false; constructor( private readonly manager: WorkspaceManager, private readonly onDirty: GitDirtyCallback, ) { - // A replacement agent booted after the channel died has no .git watch, - // and repo detection does not run again to ask for one. Re-register on - // the new agent and refresh status for what changed while none watched. - this.disposeProviderReady = manager.onProviderReady((workspaceId) => { + this.disposeProviderReady = manager.onProviderReady((workspaceId, provider) => { const entry = this.entries.get(workspaceId); - if (!entry) return; - this.watch(workspaceId, entry.gitDir).then( - () => this.onDirty(workspaceId), + if (!entry || !isAgentBackedProvider(provider) || entry.provider === provider) return; + const next = this.bind(workspaceId, entry.gitDir, provider); + void next.ready.then( + () => { + if (this.entries.get(workspaceId) === next) this.onDirty(workspaceId); + }, (error: unknown) => { log.warn( `git.watch rebind failed (workspace=${workspaceId}, gitDir=${entry.gitDir}): ${(error as Error).message}`, @@ -48,95 +49,110 @@ export class AgentGitWatcher { }); } + /** Registers a repository or joins its pending registration. */ async watch(workspaceId: string, gitDir: string): Promise { - const provider = this.requireAgentProvider(workspaceId); - const existing = this.entries.get(workspaceId); - if (existing?.gitDir === gitDir && existing.provider === provider) { - return; - } - if (existing?.provider === provider) { - await this.unwatch(workspaceId).catch(() => {}); - } else if (existing) { - // Made on an agent that has since been replaced; nothing to unwatch. - this.entries.delete(workspaceId); - existing.unsubscribe(); - } - - const unsubscribeChanged = provider.onAgentEvent(GIT_CHANGED_EVENT, (payload) => { - const parsed = AgentGitChangedPayloadSchema.safeParse(payload); - if (!parsed.success || parsed.data.gitDir !== gitDir) return; - this.onDirty(workspaceId); - }); - - // The .git watcher lives in the agent process, so a respawned agent - // starts without it even though the channel recovered transparently. - // Replay the registration on the `ready` lifecycle event (successful - // reconnect handshake) — re-registering is a no-op agent-side when the - // watch already exists, so the replay is safe either way. - const unsubscribeLifecycle = provider.onAgentLifecycle((event) => { - if (event.type !== "ready") return; - if (this.entries.get(workspaceId)?.gitDir !== gitDir) return; - void provider.callAgentMethod(GIT_WATCH_METHOD, { gitDir }).catch((error: unknown) => { - log.warn( - `git.watch replay failed (workspace=${workspaceId}, gitDir=${gitDir}): ${(error as Error).message}`, - ); - }); - // .git changes made while the agent was down were never reported. - this.onDirty(workspaceId); - }); - - await provider.callAgentMethod(GIT_WATCH_METHOD, { gitDir }); - this.entries.set(workspaceId, { - gitDir, - provider, - unsubscribe: () => { - unsubscribeChanged(); - unsubscribeLifecycle(); - }, - }); + if (this.disposed) throw new Error("Git watcher is disposed"); + const provider = this.manager.requireContext(workspaceId).fs; + if (!isAgentBackedProvider(provider)) + throw new Error("workspace agent provider is not available"); + await this.bind(workspaceId, gitDir, provider).ready; } + /** Detaches the entry now and unwatches on the agent that received the registration. */ async unwatch(workspaceId: string): Promise { const entry = this.entries.get(workspaceId); if (!entry) return; - this.entries.delete(workspaceId); - entry.unsubscribe(); - const provider = this.tryAgentProvider(workspaceId); - if (provider) { - await provider.callAgentMethod(GIT_UNWATCH_METHOD, { gitDir: entry.gitDir }); - } + this.detach(workspaceId, entry); + await enqueueWatchOperation(this.pending, workspaceId, async () => { + await entry.provider.callAgentMethod(GIT_UNWATCH_METHOD, { gitDir: entry.gitDir }); + }); } + /** Drops a workspace without blocking its removal broadcast. */ disposeWorkspace(workspaceId: string): void { - const entry = this.entries.get(workspaceId); - if (!entry) return; - this.entries.delete(workspaceId); - entry.unsubscribe(); - const provider = this.tryAgentProvider(workspaceId); - void provider?.callAgentMethod(GIT_UNWATCH_METHOD, { gitDir: entry.gitDir }).catch(() => {}); + void this.unwatch(workspaceId).catch((error: unknown) => { + log.warn( + `git.unwatch cleanup failed (workspace=${workspaceId}): ${(error as Error).message}`, + ); + }); } + /** Stops rebinding and drops all active and pending registrations. */ dispose(): void { + this.disposed = true; this.disposeProviderReady(); - for (const workspaceId of Array.from(this.entries.keys())) { - this.disposeWorkspace(workspaceId); - } + for (const workspaceId of this.entries.keys()) this.disposeWorkspace(workspaceId); } - private requireAgentProvider(workspaceId: string): AgentBackedProvider { - const provider = this.manager.requireContext(workspaceId).fs; - if (!isAgentBackedProvider(provider)) { - throw new Error("workspace agent provider is not available"); + /** Publishes the entry before RPC so failure, removal and rebind all find its listeners. */ + private bind(workspaceId: string, gitDir: string, provider: AgentBackedProvider): GitWatchEntry { + const previous = this.entries.get(workspaceId); + if (previous?.gitDir === gitDir && previous.provider === provider) return previous; + if (previous) { + if (previous.provider === provider || previous.registering) + this.disposeWorkspace(workspaceId); + else this.detach(workspaceId, previous); } - return provider; + + const unsubscribe = subscribeAgentWatch( + provider, + GIT_CHANGED_EVENT, + () => this.entries.get(workspaceId) === entry, + (payload) => { + const parsed = AgentGitChangedPayloadSchema.safeParse(payload); + if (parsed.success && parsed.data.gitDir === gitDir) this.onDirty(workspaceId); + }, + () => { + void enqueueWatchOperation(this.pending, workspaceId, async () => { + if (this.entries.get(workspaceId) === entry) { + entry.registering = true; + try { + await provider.callAgentMethod(GIT_WATCH_METHOD, { gitDir }); + } finally { + entry.registering = false; + } + } + }).catch((error: unknown) => { + log.warn( + `git.watch replay failed (workspace=${workspaceId}, gitDir=${gitDir}): ${(error as Error).message}`, + ); + }); + // Refresh status for changes the disconnected agent did not see. + this.onDirty(workspaceId); + }, + ); + + const entry: GitWatchEntry = { + gitDir, + provider, + unsubscribe, + ready: Promise.resolve(), + registering: true, + }; + this.entries.set(workspaceId, entry); + entry.ready = enqueueWatchOperation(this.pending, workspaceId, async () => { + if (this.entries.get(workspaceId) !== entry) return; + try { + await provider.callAgentMethod(GIT_WATCH_METHOD, { gitDir }); + } catch (error) { + if (this.entries.get(workspaceId) === entry) { + if ((error as { code?: string }).code === "transport.timeout") { + this.disposeWorkspace(workspaceId); + } else { + this.detach(workspaceId, entry); + } + } + throw error; + } finally { + entry.registering = false; + } + }); + return entry; } - private tryAgentProvider(workspaceId: string): AgentBackedProvider | null { - try { - const provider = this.manager.requireContext(workspaceId).fs; - return isAgentBackedProvider(provider) ? provider : null; - } catch { - return null; - } + /** Removes local ownership before any asynchronous cleanup. */ + private detach(workspaceId: string, entry: GitWatchEntry): void { + this.entries.delete(workspaceId); + entry.unsubscribe(); } } diff --git a/src/main/features/git/bridge/types.ts b/src/main/features/git/bridge/types.ts index 070c4cdbc..b790f8440 100644 --- a/src/main/features/git/bridge/types.ts +++ b/src/main/features/git/bridge/types.ts @@ -39,12 +39,11 @@ export interface GitProcessOptions { readonly interactive?: boolean; /** True streams stderr chunks instead of stdout while preserving final stderr classification. */ readonly streamStderr?: boolean; + readonly stdoutCapBytes?: number; readonly signal?: AbortSignal; } -export interface RunGitOptions extends GitProcessOptions { - readonly stdoutCapBytes?: number; -} +export type RunGitOptions = GitProcessOptions; export interface RunGitResult { readonly stdout: string; diff --git a/src/main/features/git/domain/error.ts b/src/main/features/git/domain/error.ts index b56ed540a..b1eca252e 100644 --- a/src/main/features/git/domain/error.ts +++ b/src/main/features/git/domain/error.ts @@ -32,19 +32,40 @@ export class GitError extends Error { readonly hint?: GitActionHint; constructor(kind: GitErrorKind, message: string, options: GitErrorOptions = {}) { - super(message, { cause: options.cause }); + super(maskCredentials(message), { cause: options.cause }); this.name = "GitError"; this.kind = kind; - this.stderr = options.stderr ?? ""; + this.stderr = maskCredentials(options.stderr ?? ""); this.stdout = options.stdout ?? ""; this.code = options.code ?? options.exitCode ?? null; this.exitCode = this.code; this.signal = options.signal ?? null; - this.argv = options.argv ?? []; + this.argv = options.argv?.map(maskArgument) ?? []; this.hint = options.hint; } } +/** Masks credentials in diagnostics, including messages reconstructed from older agents. */ +function maskCredentials(text: string): string { + return text + .replace(/([a-z][a-z0-9+.-]*:\/\/)[^/\s?#"'<>]+@/gi, "$1[REDACTED]@") + .replace( + /(\bauthorization\b["']?\s*[:=]\s*["']?(?:(?:bearer|basic|token)\s+)?|\bbearer\s+)[^\s"';,]+/gi, + "$1[REDACTED]", + ) + .replace( + /(\b(?:token|[a-z0-9_]+_(?:key|token|secret))\b["']?\s*[:=]\s*)("(?:\\.|[^"\\])*"|'[^']*'|[^\s"'&;,<>]+)/gi, + "$1[REDACTED]", + ); +} + +/** Uses the argument boundary to cover unquoted assignment values containing spaces. */ +function maskArgument(arg: string): string { + return maskCredentials( + arg.replace(/(^|\s)([a-z0-9_]+_(?:key|token|secret)\s*=)[\s\S]+$/i, "$1$2[REDACTED]"), + ); +} + /** * Re-wraps the agent-classified git.run envelope as the TS GitError marker. */ @@ -87,7 +108,7 @@ export function gitErrorFromAgentError(error: unknown): GitError | undefined { /** * Builds a minimal typed error for the local-process fallback that fires * when the agent envelope is unavailable. Stderr classification belongs to - * the agent path; this helper carries the raw stream verbatim. + * the agent path; this helper carries the sanitized diagnostic. */ export function gitErrorFromExit(options: { readonly args: readonly string[]; @@ -172,7 +193,7 @@ function processExitMessage( ): string { const trimmed = stderr.trim(); if (trimmed.length > 0) return trimmed; - const renderedCommand = renderGitCommand(args); + const renderedCommand = renderGitCommand(args.map(maskArgument)); if (signal) return `${renderedCommand} exited with signal ${signal}`; if (exitCode !== null) return `${renderedCommand} exited with code ${exitCode}`; return `${renderedCommand} failed`; diff --git a/src/main/features/git/domain/helpers/ipc.ts b/src/main/features/git/domain/helpers/ipc.ts index 323257f8a..6bbf9b0b1 100644 --- a/src/main/features/git/domain/helpers/ipc.ts +++ b/src/main/features/git/domain/helpers/ipc.ts @@ -25,6 +25,14 @@ import { type GitHelperConnection, setDefaultGitHelperConnection } from "./launc type BroadcastFn = (channelName: string, event: string, args: unknown) => void; type HelperRoute = "editor.open"; +const MAX_HELPER_REQUEST_BYTES = 1024 * 1024; +const MAX_EDITOR_FILE_BYTES = 4 * 1024 * 1024; +const DISCONNECTED: HelperWireResponse = { ok: false, error: "Git helper disconnected." }; + +interface HelperRequestLifetime { + readonly signal: AbortSignal; + promptOpened?(): void; +} interface GitHelpersIpcManagerOptions { readonly userDataDir: string; @@ -87,6 +95,8 @@ export class GitHelpersIpcManager { private readonly platform: NodeJS.Platform; private readonly pid: number; private readonly token: string; + /** Connected helpers that have not yet submitted the request they came for. */ + private readonly awaitingRequests = new Map(); private readonly pending = new Map(); private server: net.Server | null = null; private endpoint: GitHelpersEndpoint | null = null; @@ -146,10 +156,19 @@ export class GitHelpersIpcManager { setDefaultGitHelperConnection(null); if (server) { - await new Promise((resolve) => server.close(() => resolve())); + const closed = new Promise((resolve) => server.close(() => resolve())); + // Clients with a prompt receive its shutdown reply. An idle socket + // has no pending prompt to resolve and can keep server.close waiting. + for (const [socket, lifetime] of this.awaitingRequests) { + lifetime.abort(); + socket.destroy(); + } + await closed; } if (endpoint?.kind === "unix") { - await fs.unlink(endpoint.path).catch(() => {}); + await fs.unlink(endpoint.path).catch((error: NodeJS.ErrnoException) => { + if (error.code !== "ENOENT") throw error; + }); } } @@ -168,12 +187,15 @@ export class GitHelpersIpcManager { * Processes one decoded helper payload. Tests call this directly to exercise * token and active-prompt validation without opening an OS socket. */ - async handleHelperRequest(payload: HelperRequestPayload): Promise { + async handleHelperRequest( + payload: HelperRequestPayload, + lifetime?: HelperRequestLifetime, + ): Promise { this.assertAuthorizedPayload(payload); switch (payload.route as HelperRoute) { case "editor.open": - return this.openEditorPrompt(payload); + return this.openEditorPrompt(payload, lifetime); default: throw new Error(`Unsupported Git helper route: ${String(payload.route)}`); } @@ -276,34 +298,66 @@ export class GitHelpersIpcManager { * JSON-line response. Each helper invocation uses one short-lived socket. */ private handleSocket(socket: net.Socket): void { - let buffer = ""; + let chunks: Buffer[] = []; + let bytes = 0; let handled = false; - socket.setEncoding("utf8"); + const disconnected = new AbortController(); + this.awaitingRequests.set(socket, disconnected); + socket.once("close", () => { + chunks = []; + this.awaitingRequests.delete(socket); + disconnected.abort(); + }); + + /** Writes a single compatible JSON reply while the connection can still receive it. */ + const reply = (response: HelperWireResponse): void => { + this.awaitingRequests.delete(socket); + if (!socket.destroyed && !disconnected.signal.aborted) { + socket.end(`${JSON.stringify(response)}\n`); + } + }; + /** Dispatches one complete line and drops the receive buffer immediately. */ const finish = async (line: string): Promise => { - if (handled) return; handled = true; + chunks = []; try { const payload = JSON.parse(line) as HelperRequestPayload; - const response = await this.handleHelperRequest(payload); - socket.end(`${JSON.stringify(response)}\n`); + const response = await this.handleHelperRequest(payload, { + signal: disconnected.signal, + promptOpened: () => this.awaitingRequests.delete(socket), + }); + reply(response); } catch (error) { - socket.end(`${JSON.stringify({ ok: false, error: errorMessage(error) })}\n`); + reply({ ok: false, error: errorMessage(error) }); } }; - socket.on("data", (chunk) => { - buffer += chunk; - const newline = buffer.indexOf("\n"); - if (newline >= 0) { - void finish(buffer.slice(0, newline)); + socket.on("data", (chunk: Buffer) => { + if (handled) return; + const newline = chunk.indexOf(0x0a); + const part = newline < 0 ? chunk : chunk.subarray(0, newline); + bytes += part.length; + if (bytes > MAX_HELPER_REQUEST_BYTES) { + handled = true; + chunks = []; + reply({ + ok: false, + error: `Git helper request exceeds ${MAX_HELPER_REQUEST_BYTES} bytes.`, + }); + return; } + chunks.push(part); + if (newline >= 0) void finish(Buffer.concat(chunks, bytes).toString("utf8")); }); - socket.on("end", () => { - if (buffer.trim().length > 0) { - void finish(buffer.trim()); + socket.once("end", () => { + if (handled) { + disconnected.abort(); + return; } + const line = Buffer.concat(chunks, bytes).toString("utf8").trim(); + if (line) void finish(line); }); socket.on("error", () => {}); @@ -326,9 +380,15 @@ export class GitHelpersIpcManager { * Broadcasts a commit-message editor prompt after reading Git's temporary * message file. The renderer's save/cancel handler owns the final write. */ - private async openEditorPrompt(payload: HelperRequestPayload): Promise { + private async openEditorPrompt( + payload: HelperRequestPayload, + lifetime?: HelperRequestLifetime, + ): Promise { if (!payload.filePath) throw new Error("Git editor helper request missing file path."); - const initialContent = await fs.readFile(payload.filePath, "utf8").catch(() => ""); + const { signal } = lifetime ?? {}; + if (signal?.aborted) return DISCONNECTED; + const initialContent = await readEditorFile(payload.filePath, signal); + if (signal?.aborted) return DISCONNECTED; const promptId = crypto.randomUUID(); const event = GitEditorPromptSchema.parse({ promptId, @@ -339,12 +399,22 @@ export class GitHelpersIpcManager { }) satisfies GitEditorPrompt; return new Promise((resolve) => { + const closePrompt = () => { + this.pending.delete(promptId); + resolve(DISCONNECTED); + this.broadcast("editor", "closed", { promptId }); + }; + signal?.addEventListener("abort", closePrompt, { once: true }); this.pending.set(promptId, { kind: "editor", promptId, filePath: payload.filePath ?? "", - resolve, + resolve: (response) => { + signal?.removeEventListener("abort", closePrompt); + resolve(response); + }, }); + lifetime?.promptOpened?.(); this.broadcast("editor", "prompt", event); }); } @@ -365,6 +435,28 @@ export class GitHelpersIpcManager { } } +/** Reads only the bounded prefix needed to distinguish a complete file from an oversized one. */ +async function readEditorFile(filePath: string, signal?: AbortSignal): Promise { + const file = await fs.open(filePath, "r").catch(() => null); + if (!file) return ""; + try { + const chunks: Buffer[] = []; + let bytes = 0; + while (!signal?.aborted && bytes <= MAX_EDITOR_FILE_BYTES) { + const chunk = Buffer.allocUnsafe(Math.min(64 * 1024, MAX_EDITOR_FILE_BYTES + 1 - bytes)); + const read = await file.read(chunk).catch(() => null); + if (!read || signal?.aborted) return ""; + if (read.bytesRead === 0) return Buffer.concat(chunks, bytes).toString("utf8"); + bytes += read.bytesRead; + chunks.push(chunk.subarray(0, read.bytesRead)); + } + if (signal?.aborted) return ""; + throw new Error(`Git editor file exceeds ${MAX_EDITOR_FILE_BYTES} bytes.`); + } finally { + await file.close(); + } +} + /** * Registers renderer-response IPC methods for askpass and editor prompts. */ @@ -378,11 +470,12 @@ export function registerGitHelperIpcChannels(manager: GitHelpersIpcManager): voi }); register("editor", { + keepOnRendererClose: ["save"], call: { save: (args) => manager.saveEditor(args), cancel: (args) => manager.cancelEditor(args), }, - listen: { prompt: {} }, + listen: { prompt: {}, closed: {} }, }); } diff --git a/src/main/features/lsp/agent-host.ts b/src/main/features/lsp/agent-host.ts index d52779186..2fd53e3c6 100644 --- a/src/main/features/lsp/agent-host.ts +++ b/src/main/features/lsp/agent-host.ts @@ -559,13 +559,12 @@ class AgentLspHostHandleImpl implements LspHostHandle { return !this.disposed; } - dispose(): void { + /** Drops editor state and waits for the running servers' shutdown replies. */ + async dispose(): Promise { if (this.disposed) return; this.disposed = true; this.diagnosticsDebouncer.clearAll(); - for (const [, server] of this.serversById) { - server.dispose(); - } + const shutdowns = Array.from(this.serversById.values(), (server) => server.dispose()); this.serversById.clear(); this.workspaceServers.clear(); this.serverPromises.clear(); @@ -581,6 +580,7 @@ class AgentLspHostHandleImpl implements LspHostHandle { } this.channelDisposers.clear(); this.listeners.clear(); + await Promise.all(shutdowns); } /** diff --git a/src/main/features/lsp/agent-lsp-server.ts b/src/main/features/lsp/agent-lsp-server.ts index 4010eeaca..8c6a466ae 100644 --- a/src/main/features/lsp/agent-lsp-server.ts +++ b/src/main/features/lsp/agent-lsp-server.ts @@ -192,7 +192,8 @@ export class AgentLspServer { return true; } - dispose(): void { + /** Fails pending editor calls and waits for the server's shutdown reply. */ + async dispose(): Promise { if (this.disposed) return; this.disposed = true; this.textDocumentCache.clear(); @@ -201,7 +202,7 @@ export class AgentLspServer { pending.reject(new Error("server disposed")); } this.pending.clear(); - void this.channel.call("lsp.shutdown", { serverId: this.serverId }).catch(() => {}); + await this.channel.call("lsp.shutdown", { serverId: this.serverId }).catch(() => {}); } // disposePending mirrors dispose() but does not call lsp.shutdown — the diff --git a/src/main/features/lsp/host.ts b/src/main/features/lsp/host.ts index 26ecdeb11..e5a9ca8cf 100644 --- a/src/main/features/lsp/host.ts +++ b/src/main/features/lsp/host.ts @@ -31,7 +31,7 @@ export interface LspHostHandle { rejectServerRequest: (id: string | number, message: string) => void; on: (event: string, cb: EventCallback) => () => void; isAlive: () => boolean; - dispose: () => void; + dispose: () => Promise | void; /** * Dispose the LSP server for a single language within a workspace and * broadcast a workspaceLspReset event for that (workspace, language) pair. diff --git a/src/main/features/menu/template.ts b/src/main/features/menu/template.ts index 1bd537c96..c962ae266 100644 --- a/src/main/features/menu/template.ts +++ b/src/main/features/menu/template.ts @@ -162,8 +162,8 @@ function fileMenu(isMac: boolean, t?: TFunction): MenuItemSpec { cmd(t != null ? t("menu:file.openFile") : "Open File…", COMMANDS.fileOpen), cmd(t != null ? t("menu:file.save") : "Save", COMMANDS.fileSave), { type: "separator" }, - cmd(t != null ? t("menu:file.closeEditor") : "Close Editor", COMMANDS.tabClose), - cmd(t != null ? t("menu:file.closeOthers") : "Close Others", COMMANDS.tabCloseOthers), + cmd(t != null ? t("menu:file.closeEditor") : "Close Tab", COMMANDS.tabClose), + cmd(t != null ? t("menu:file.closeOthers") : "Close Other Tabs", COMMANDS.tabCloseOthers), { type: "separator" }, cmd(t != null ? t("menu:file.refreshFiles") : "Refresh Files", COMMANDS.filesRefresh), ]; diff --git a/src/main/features/notes/index.ts b/src/main/features/notes/index.ts index b7276493d..dd93b61b0 100644 --- a/src/main/features/notes/index.ts +++ b/src/main/features/notes/index.ts @@ -3,7 +3,7 @@ import * as path from "node:path"; import { BrowserWindow, dialog, shell } from "electron"; import { ipcContract } from "../../../shared/ipc/contract"; -import { ipcOk } from "../../../shared/ipc/result"; +import { ipcErr, ipcOk } from "../../../shared/ipc/result"; import type { DocNode } from "../../../shared/notes/markdown"; import { imageBlock, @@ -17,11 +17,24 @@ import { type WorkspaceMeta, workspaceLocationKey, } from "../../../shared/types/workspace"; -import { broadcast, register, validateArgs } from "../../infra/ipc-router"; +import { broadcast, type CallContext, register, validateArgs } from "../../infra/ipc-router"; import { VoicePrints } from "../voice-print"; -import { NotesStore } from "./store"; +import { NoteError, NotesStore } from "./store"; const c = ipcContract.notes.call; +const NOTE_WRITE_CALLS: readonly string[] = [ + "create", + "save", + "prune", + "remove", + "addFiles", + "addImage", + "saveSketch", + "removeLocation", + "saveTranscript", + "appendAudio", + "removeRecording", +] satisfies readonly (keyof typeof c)[]; const MIME: Record = { ".svg": "image/svg+xml", @@ -87,6 +100,12 @@ export interface NotesFeature { ): Promise; /** The note a window records into, the one told last; none while nothing records. */ recording(): NoteRecording | undefined; + /** Drops recordings and in-flight reports for a removed workspace. */ + forgetWorkspace(workspaceId: string): void; + /** Releases the notes feature's process and recording state at app shutdown. */ + dispose(): void; + /** Waits for the note writes that were in progress at shutdown. */ + waitForWrites(): Promise; /** Makes a note, and tells the windows. */ create(workspaceId: string, title: string): Promise; /** @@ -116,15 +135,56 @@ const IMAGE_EXTS: Readonly> = { ".webp": "webp", }; +type NoteCall = (args: unknown, ctx?: CallContext) => unknown; +type TrackWrite = (write: () => Promise) => Promise; + +/** Reports expected note failures before they reach the router's bug handler. */ +function withNoteErrors( + trackWrite: TrackWrite, + handlers: Record, +): Record { + return Object.fromEntries( + Object.entries(handlers).map(([method, handler]) => [ + method, + async (args: unknown, ctx?: CallContext) => { + try { + if (NOTE_WRITE_CALLS.includes(method)) { + return await trackWrite(async () => handler(args, ctx)); + } + return await handler(args, ctx); + } catch (error) { + if (!(error instanceof NoteError)) throw error; + if (error.code === "not-found") { + return ipcErr("not-found", error.message, { category: "failed", code: error.code }); + } + return ipcErr("invalid-args", error.message, { + category: "invalid-input", + code: error.code, + }); + } + }, + ]), + ); +} + export function registerNotesChannel(opts: { userData: string; workspaces: () => readonly WorkspaceMeta[]; }): NotesFeature { const store = new NotesStore(path.join(opts.userData, "notes"), (dir) => shell.trashItem(dir)); + const writes = new Set>(); + /** Keeps the entire write in the shutdown set, including any awaited reads. */ + const trackWrite: TrackWrite = (write) => { + const pending = write(); + writes.add(pending); + const forget = () => writes.delete(pending); + void pending.then(forget, forget); + return pending; + }; const locationOf = (workspaceId: string): NoteLocation => { const all = opts.workspaces(); const meta = all.find((w) => w.id === workspaceId); - if (!meta) throw new Error(`no workspace ${workspaceId}`); + if (!meta) throw new NoteError("not-found", `no workspace ${workspaceId}`); return noteLocationOf(meta, all); }; const changed = (event: NoteChanged) => broadcast("notes", "changed", event); @@ -133,6 +193,15 @@ export function registerNotesChannel(opts: { // The windows' recorders, by window: what each told last. const recordings = new Map(); + const pendingRecordings = new Map(); + /** Removes the current recording state of a workspace, including unfinished reports. */ + const forgetWorkspace = (workspaceId: string): void => { + for (const byWindow of [recordings, pendingRecordings]) { + for (const [senderId, value] of byWindow) { + if (value.workspaceId === workspaceId) byWindow.delete(senderId); + } + } + }; const watched = new Set(); const recording = (): NoteRecording | undefined => { let last: NoteRecording | undefined; @@ -143,60 +212,59 @@ export function registerNotesChannel(opts: { for (const r of recordings.values()) if (r.noteId === noteId) r.title = title; }; - const attachPhoto: NotesFeature["attachPhoto"] = async ( - workspaceId, - noteId, - bytes, - ext, - name, - ) => { - const location = locationOf(workspaceId); - const asset = await store.addPhoto(location, noteId, bytes, ext, name); - broadcast("notes", "photoAdded", { - locationKey: location.key, - noteId, - src: asset.src, - name: asset.name, + const attachPhoto: NotesFeature["attachPhoto"] = (workspaceId, noteId, bytes, ext, name) => + trackWrite(async () => { + const location = locationOf(workspaceId); + const asset = await store.addPhoto(location, noteId, bytes, ext, name); + broadcast("notes", "photoAdded", { + locationKey: location.key, + noteId, + src: asset.src, + name: asset.name, + }); + changed({ locationKey: location.key, noteId, change: "saved" }); + return asset; }); - changed({ locationKey: location.key, noteId, change: "saved" }); - return asset; - }; - const create: NotesFeature["create"] = async (workspaceId, title) => { - const location = locationOf(workspaceId); - const note = await store.create(location, title); - changed({ locationKey: location.key, noteId: note.id, change: "created" }); - return note; - }; - const append: NotesFeature["append"] = async (workspaceId, noteId, blocks, under) => { - const location = locationOf(workspaceId); - await store.appendBlocks(location, noteId, blocks, under); - broadcast("notes", "blocksAdded", { - locationKey: location.key, - noteId, - blocks: blocks as unknown as Record[], - ...(under !== undefined ? { under } : {}), + const create: NotesFeature["create"] = (workspaceId, title) => + trackWrite(async () => { + const location = locationOf(workspaceId); + const note = await store.create(location, title); + changed({ locationKey: location.key, noteId: note.id, change: "created" }); + return note; + }); + const append: NotesFeature["append"] = (workspaceId, noteId, blocks, under) => + trackWrite(async () => { + const location = locationOf(workspaceId); + await store.appendBlocks(location, noteId, blocks, under); + broadcast("notes", "blocksAdded", { + locationKey: location.key, + noteId, + blocks: blocks as unknown as Record[], + ...(under !== undefined ? { under } : {}), + }); + changed({ locationKey: location.key, noteId, change: "saved" }); + }); + const addAsset: NotesFeature["addAsset"] = (workspaceId, noteId, bytes, name) => + trackWrite(async () => { + const location = locationOf(workspaceId); + // A note that is not there gets no file either. + await store.read(location, noteId); + const ext = IMAGE_EXTS[path.extname(name).toLowerCase()]; + if (ext) { + const asset = await store.addImage(location, noteId, bytes, ext, name); + return { asset, block: imageBlock(asset) }; + } + const asset = await store.addFileBytes(location, noteId, bytes, name); + return { + asset, + block: { type: "noteFile", attrs: { src: asset.src, name: asset.name, size: asset.size } }, + }; }); - changed({ locationKey: location.key, noteId, change: "saved" }); - }; - const addAsset: NotesFeature["addAsset"] = async (workspaceId, noteId, bytes, name) => { - const location = locationOf(workspaceId); - // A note that is not there gets no file either. - await store.read(location, noteId); - const ext = IMAGE_EXTS[path.extname(name).toLowerCase()]; - if (ext) { - const asset = await store.addImage(location, noteId, bytes, ext, name); - return { asset, block: imageBlock(asset) }; - } - const asset = await store.addFileBytes(location, noteId, bytes, name); - return { - asset, - block: { type: "noteFile", attrs: { src: asset.src, name: asset.name, size: asset.size } }, - }; - }; register("notes", { - call: { + keepOnRendererClose: NOTE_WRITE_CALLS, + call: withNoteErrors(trackWrite, { list: async (args: unknown) => { const { workspaceId } = validateArgs(c.list.args, args); return ipcOk(await store.list(locationOf(workspaceId))); @@ -319,41 +387,55 @@ export function registerNotesChannel(opts: { removeLocation: async (args: unknown) => { const { key } = validateArgs(c.removeLocation.args, args); await store.removeLocation(key); - changed({ locationKey: key, change: "removed" }); + const workspaces = opts.workspaces(); + const workspaceIds = workspaces + .filter((workspace) => noteLocationOf(workspace, workspaces).key === key) + .map((workspace) => workspace.id); + changed({ locationKey: key, change: "removed", workspaceIds }); return ipcOk(undefined); }, recording: async (args: unknown, ctx) => { const { now } = validateArgs(c.recording.args, args); const sender = ctx?.sender; - if (!sender) return ipcOk(undefined); + if (!sender || sender.isDestroyed()) return ipcOk(undefined); + const senderId = sender.id; if (!now) { - recordings.delete(sender.id); + pendingRecordings.delete(senderId); + recordings.delete(senderId); return ipcOk(undefined); } - const before = recordings.get(sender.id); - let title = before?.noteId === now.noteId ? before.title : ""; - if (before?.noteId !== now.noteId) { - title = await store - .read(locationOf(now.workspaceId), now.noteId) - .then((n) => n.title) - .catch(() => ""); - } - // A window that goes stops recording. - if (!watched.has(sender.id)) { - watched.add(sender.id); + // Install the lifetime guard before looking up a title or touching disk. + if (!watched.has(senderId)) { + watched.add(senderId); sender.once("destroyed", () => { - recordings.delete(sender.id); - watched.delete(sender.id); + pendingRecordings.delete(senderId); + recordings.delete(senderId); + watched.delete(senderId); }); } - recordings.set(sender.id, { - workspaceId: now.workspaceId, - noteId: now.noteId, - title, - recordedMs: now.recordedMs, - at: Date.now(), - paused: now.paused, - }); + pendingRecordings.set(senderId, now); + try { + const before = recordings.get(senderId); + const title = + before?.noteId === now.noteId + ? before.title + : await store + .read(locationOf(now.workspaceId), now.noteId) + .then((note) => note.title) + .catch(() => ""); + if (pendingRecordings.get(senderId) === now) { + recordings.set(senderId, { + workspaceId: now.workspaceId, + noteId: now.noteId, + title, + recordedMs: now.recordedMs, + at: Date.now(), + paused: now.paused, + }); + } + } finally { + if (pendingRecordings.get(senderId) === now) pendingRecordings.delete(senderId); + } return ipcOk(undefined); }, readTranscript: async (args: unknown) => { @@ -395,11 +477,31 @@ export function registerNotesChannel(opts: { shell.showItemInFolder(path.join(dir, "location.json")); return ipcOk(undefined); }, - }, + }), listen: { changed: {}, }, }); - return { store, locationOf, changed, attachPhoto, recording, create, append, addAsset }; + return { + store, + locationOf, + changed, + attachPhoto, + recording, + forgetWorkspace, + waitForWrites: async () => { + const results = await Promise.allSettled(writes); + const failure = results.find((result) => result.status === "rejected"); + if (failure) throw failure.reason; + }, + dispose: () => { + pendingRecordings.clear(); + recordings.clear(); + voicePrints.stop(); + }, + create, + append, + addAsset, + }; } diff --git a/src/main/features/notes/store.ts b/src/main/features/notes/store.ts index 3920c50da..543190b68 100644 --- a/src/main/features/notes/store.ts +++ b/src/main/features/notes/store.ts @@ -16,6 +16,7 @@ import { createHash, randomBytes } from "node:crypto"; import { promises as fs, constants as fsConstants } from "node:fs"; import * as path from "node:path"; +import { createLogger } from "../../../shared/log/main"; import { withBlocks } from "../../../shared/notes/insert"; import { type DocNode, docToMarkdown } from "../../../shared/notes/markdown"; import { @@ -43,6 +44,8 @@ import { SKETCH_ID_RE, } from "../../../shared/notes/types"; +const log = createLogger("notes"); + const EMPTY_DOC: DocNode = { type: "doc", content: [{ type: "paragraph" }] }; /** How long a file the note does not use is kept: one just written may be about to be. */ @@ -541,8 +544,24 @@ async function readJson(file: string): Promise { async function writeAtomic(file: string, data: string): Promise { const tmp = `${file}.${process.pid}.${randomBytes(3).toString("hex")}.tmp`; - await fs.writeFile(tmp, data); - await fs.rename(tmp, file); + const handle = await fs.open(tmp, "wx"); + try { + try { + await fs.writeFile(handle, data); + } finally { + await handle.close(); + } + await fs.rename(tmp, file); + } catch (error) { + try { + await fs.unlink(tmp); + } catch (cleanupError) { + if ((cleanupError as NodeJS.ErrnoException).code !== "ENOENT") { + log.warn(`Could not remove temporary note file: ${String(cleanupError)}`); + } + } + throw error; + } } async function readdirSafe(dir: string): Promise { diff --git a/src/main/features/pty/agent-host.ts b/src/main/features/pty/agent-host.ts index ae855ed69..250ce1ea2 100644 --- a/src/main/features/pty/agent-host.ts +++ b/src/main/features/pty/agent-host.ts @@ -1,6 +1,8 @@ import { TextDecoder } from "node:util"; import { createLogger } from "../../../shared/log/main"; import type { AgentChannel, ChannelLifecycleEvent } from "../../infra/agent/channel"; +import type { HookInfo } from "../workspace/manager"; +import { tabHookToken } from "../workspace/tab-token"; import type { PtyHostHandle } from "./types"; const log = createLogger("agent-host"); @@ -44,6 +46,8 @@ export interface AgentPtyWorkspaceManager { * racing condition rather than a bug. */ tryGetAgentChannel(workspaceId: string): Promise; + /** The current process's hook endpoint for a wrapped terminal spawn. */ + getSpawnHookInfo?(workspaceId: string, channel: AgentChannel): Promise; /** * Tells when a workspace's agent is ready, a new one after a reconnect * included: its channel is subscribed then, not at its first PTY call. @@ -262,7 +266,16 @@ class AgentPtyHostHandle implements PtyHostHandle { throw new Error("terminal closed before it started"); } const channel = await this.channelForWorkspace(workspaceId); - // Or while its agent was being reached. + if (this.isUnwanted(workspaceId, tabId)) { + throw new Error("terminal closed before it started"); + } + const spawn = args as { env?: Record }; + const env = spawn.env; + const hooks = + env?.NEXUS_WRAPPER_SELF_DIR && this.workspaceManager.getSpawnHookInfo + ? await this.workspaceManager.getSpawnHookInfo(workspaceId, channel) + : null; + // The tab can also close while the replacement's hooks are being read. if (this.isUnwanted(workspaceId, tabId)) { throw new Error("terminal closed before it started"); } @@ -270,7 +283,19 @@ class AgentPtyHostHandle implements PtyHostHandle { let result: AgentSpawnResult; try { sent = true; - result = await channel.call("pty.spawn", args); + result = await channel.call( + "pty.spawn", + hooks + ? { + ...spawn, + env: { + ...env, + NEXUS_AGENT_SOCKET: hooks.socketPath, + NEXUS_HOOK_TOKEN: tabHookToken(hooks, workspaceId, tabId), + }, + } + : args, + ); } catch (error) { this.deleteSession(workspaceId, tabId); throw error; diff --git a/src/main/features/pty/harness-env.ts b/src/main/features/pty/harness-env.ts index efea5a905..8d5d8b530 100644 --- a/src/main/features/pty/harness-env.ts +++ b/src/main/features/pty/harness-env.ts @@ -44,8 +44,7 @@ export interface HarnessTerminalEnvContext { * 6. NEXUS_WORKSPACE_ID / NEXUS_TAB_ID — injected from context. * 7. NEXUS_AGENT_SOCKET / NEXUS_HOOK_TOKEN — set only when provided in context. * - * If the caller env sets the same key directly, the caller's value wins (spread order: defaults first, - * caller second — the existing pattern is kept). + * The app's tab and hook variables take precedence over the caller's environment. * * The input object is not modified. * If no context is provided, only TERM_PROGRAM / TERM_PROGRAM_VERSION are set to defaults and @@ -88,13 +87,6 @@ export function injectHarnessTerminalEnv( const base: Record = { TERM_PROGRAM: HARNESS_TERM_PROGRAM, TERM_PROGRAM_VERSION: HARNESS_TERM_PROGRAM_VERSION, - ...nexusBase, }; - - if (env === undefined) { - return base; - } - - // Spread order: defaults first, caller env second — caller keys win. - return { ...base, ...env }; + return { ...base, ...env, ...nexusBase }; } diff --git a/src/main/features/pty/ipc.ts b/src/main/features/pty/ipc.ts index 478e209bb..a1e1a9c2c 100644 --- a/src/main/features/pty/ipc.ts +++ b/src/main/features/pty/ipc.ts @@ -1,14 +1,13 @@ // PTY IPC channel — bridges renderer ↔ main ↔ Go agent host. // All PTY sessions are routed to the agent host unconditionally. +import { mergeAgentEnv, profileVariables } from "../../../shared/agent-profiles/env"; import { ipcContract } from "../../../shared/ipc/contract"; import { ipcErr, ipcOk } from "../../../shared/ipc/result"; import type { WorkspacePorts } from "../../../shared/pty/ports"; import type { CustomShell, ShellChoice } from "../../../shared/pty/shell"; import { broadcast, register, validateArgs } from "../../infra/ipc-router"; import { getActiveAppWindow, getFocusedAppWindow } from "../window/app-windows"; -import type { HookInfo } from "../workspace/manager"; -import { tabHookToken } from "../workspace/tab-token"; import { ForwardError, type PortForwards } from "./forwards"; import { injectHarnessTerminalEnv } from "./harness-env"; import type { WorkspaceNameLookup } from "./osc-notification"; @@ -33,13 +32,8 @@ export interface PtyChannelOptions { agentHost: PtyHostHandle; recorder?: PtyRecorderSink; /** - * Provides workspace display names, activation for OS notifications, and - * Pull-based hookserver connection info lookup (getHookInfo). - * - * `getAgentChannel` is used to guarantee that hookInfo is fresh — - * the spawn handler awaits it before capturing the env, which forces channel.ready / the hook.getInfo - * pull to complete before the hookInfo lookup. The returned channel object - * itself is not used; the call exists only for ready synchronization. + * Provides workspace display names and activation for OS notifications. + * The agent must boot before its remote wrapper locations are available. * * `getWrapperBinDir` / `getWrapperAgentBin` — return, by workspace kind, * the absolute paths of the wrapper bin directory and the agent binary. When null is returned, @@ -56,8 +50,7 @@ export interface PtyChannelOptions { * so the shim is skipped. */ workspaceManager?: WorkspaceNameLookup & { - activate(id: string): Promise; - getHookInfo(workspaceId: string): HookInfo | null; + connect(id: string): Promise; getAgentChannel(workspaceId: string): Promise; getWrapperBinDir(workspaceId: string): string | null; getWrapperAgentBin(workspaceId: string): string | null; @@ -71,7 +64,7 @@ export interface PtyChannelOptions { * variable, made on the workspace's host, and its own variables); null * for a profile there is not. */ - profileEnv?: (workspaceId: string, profileId: string) => Promise | null>; + profileEnv?: (workspaceId: string, profileId?: string) => Promise | null>; /** The default shell from Settings › Terminal, read at every spawn. */ getDefaultShell?: () => ShellChoice | undefined; /** The shells the user added in Settings › Terminal, read at every spawn. */ @@ -128,7 +121,7 @@ export function registerPtyChannel(options: PtyChannelOptions): void { getFocusedWindow: getFocusedAppWindow, isActiveTab: options.isActiveTab, notificationsEnabled: options.notificationsEnabled, - activateWorkspace: wm ? (id) => wm.activate(id) : undefined, + connectWorkspace: wm ? (id) => wm.connect(id) : undefined, focusMainWindow: () => { const win = getActiveAppWindow(); if (!win) return; @@ -200,24 +193,21 @@ export function registerPtyChannel(options: PtyChannelOptions): void { } = request; // A terminal opened for a profile (a sign-in for it) runs as its chat // does; without it, it would sign in or out of the default folder. + const windows = options.windowsAgentFiles !== undefined && wm?.isLocal?.(workspaceId) !== false; let env = givenEnv; - if (profile !== undefined) { + if (profile !== undefined || options.profileEnv) { const fromProfile = await options.profileEnv?.(workspaceId, profile); - if (!fromProfile) throw new Error(`no profile ${profile}`); - env = { ...givenEnv, ...fromProfile }; + if (profile !== undefined && !fromProfile) { + return ipcErr("not-found", `no profile ${profile}`, { category: "failed" }); + } + env = mergeAgentEnv( + windows, + givenEnv ?? {}, + profileVariables(fromProfile ?? undefined, windows), + ); } recorder.start(workspaceId, tabId, cols, rows); - // hookInfo must be read after channel.ready for it to be guaranteed fresh. - // Called right after a channel reconnect, a stale cache (the previous boot's socket path) - // could be baked into the env, creating a hook client that points at a dead socket. - // wm.getAgentChannel triggers ensureProviderReady, and at its end the hook.getInfo - // result is set in hookInfoByWorkspace, so a getHookInfo after this await is - // structurally guaranteed to be the latest. The returned channel object is not used. - if (wm) { - await wm.getAgentChannel(workspaceId); - } - // Look up the socket/token by pull with workspaceManager.getHookInfo and inject them into the env. - const hookInfo = wm?.getHookInfo(workspaceId) ?? null; + if (wm) await wm.getAgentChannel(workspaceId); // Decide the wrapper bin directory and agent binary path by workspace kind. // When null, skip the wrapper-related env injection (keeps the graceful pattern). const wrapperBinDir = wm?.getWrapperBinDir(workspaceId) ?? null; @@ -229,8 +219,6 @@ export function registerPtyChannel(options: PtyChannelOptions): void { workspaceId, tabId, agentBin: wrapperAgentBin ?? undefined, - agentSocket: hookInfo?.socketPath, - hookToken: hookInfo ? tabHookToken(hookInfo, workspaceId, tabId) : undefined, }) : injectHarnessTerminalEnv(env); @@ -283,11 +271,11 @@ export function registerPtyChannel(options: PtyChannelOptions): void { const files = await options.windowsAgentFiles(); const shimResult = applyWindowsShellShim({ shell: shellPath, - env: enrichedEnv, + env: mergeAgentEnv(windows, enrichedEnv), args: shimmedArgs, settingsPath: files?.settingsPath, wrapperDir: files?.binDir, - ...(files ? { codex: { cmd: files.cmdCodexNoDaemon } } : {}), + ...(files ? { codex: { cmdLauncherDir: files.cmdLauncherDir } } : {}), // A local agent, and so the shell, inherits this process's environment. originalPath: process.env.ORIGINAL_PATH, }); @@ -364,11 +352,18 @@ export function registerPtyChannel(options: PtyChannelOptions): void { write: async (args: unknown) => { const { workspaceId, tabId, data } = validateArgs(c.write.args, args); options.onInput?.(workspaceId, tabId, data); - // write/resize/ack/kill are fire-and-forget from the renderer and no-op - // when the workspace is already gone — agentHost.call returns undefined - // in that case. Return ipcOk so the router stays silent on either path. - await agentHost.call("write", { workspaceId, tabId, data }); - return ipcOk(undefined); + try { + const sent = await agentHost.call("write", { workspaceId, tabId, data }); + // A held session or removed workspace intentionally drops input. + // Cancel the renderer's queued suffix as well. + if (sent === undefined) return ipcErr("cancelled", "Terminal input was skipped"); + return ipcOk(undefined); + } catch (error) { + return ipcErr("terminal-input-failed", String(error), { + category: "failed", + code: "terminal-input-failed", + }); + } }, resize: async (args: unknown) => { diff --git a/src/main/features/pty/osc-notification.ts b/src/main/features/pty/osc-notification.ts index 757518893..6431eda94 100644 --- a/src/main/features/pty/osc-notification.ts +++ b/src/main/features/pty/osc-notification.ts @@ -48,8 +48,8 @@ export interface OscNotificationDispatcherDeps { isActiveTab?: (workspaceId: string, tabId: string) => boolean; /** Settings' OS notifications toggle, read at each notification. */ notificationsEnabled?: () => boolean; - /** Called on notification click to activate the workspace in main. */ - activateWorkspace?: (workspaceId: string) => Promise; + /** Called on notification click to connect the workspace. */ + connectWorkspace?: (workspaceId: string) => Promise; /** Called on notification click to bring the app window to the foreground. */ focusMainWindow?: () => void; /** Injected in tests to avoid real Electron Notification. */ @@ -186,9 +186,9 @@ export class OscNotificationDispatcher { // 1. Bring the app window to the foreground. this.deps.focusMainWindow?.(); - // 2. Activate the workspace in main (fire-and-forget). - this.deps.activateWorkspace?.(workspaceId)?.catch((err) => { - log.warn(`activateWorkspace failed: ${(err as Error).message}`); + // 2. Bring the workspace online for the tab being revealed. + this.deps.connectWorkspace?.(workspaceId)?.catch((err) => { + log.warn(`connectWorkspace failed: ${(err as Error).message}`); }); // 3. Tell the renderer to reveal the workspace + tab. diff --git a/src/main/features/pty/ports.ts b/src/main/features/pty/ports.ts index 2072a4088..e0490c31a 100644 --- a/src/main/features/pty/ports.ts +++ b/src/main/features/pty/ports.ts @@ -134,6 +134,16 @@ export class PortWatcher { } } + /** Drops polling and debounce state for a workspace that no longer exists. */ + forgetWorkspace(workspaceId: string): void { + this.lastReplay.delete(workspaceId); + const prefix = `${workspaceId}:`; + for (const key of this.lastOutput.keys()) { + if (key.startsWith(prefix)) this.lastOutput.delete(key); + } + this.stop(workspaceId); + } + dispose(): void { this.disposed = true; for (const dispose of this.disposers.splice(0)) dispose(); diff --git a/src/main/features/pty/windows-shell-shim.ts b/src/main/features/pty/windows-shell-shim.ts index 487c32623..8b92e3b18 100644 --- a/src/main/features/pty/windows-shell-shim.ts +++ b/src/main/features/pty/windows-shell-shim.ts @@ -10,7 +10,7 @@ * - Git Bash: the wrappers themselves, copied to ~/.nexus-code/bin, which * the agent puts first on PATH in place of the nexus launchers' directory. * - PowerShell: `claude` and `codex` functions, defined as the shell starts. - * - cmd: doskey macros. + * - cmd: a Claude doskey macro and a codex.cmd launcher on PATH. * * The last two hand claude the wrapper's hooks, kept in * ~/.nexus-code/claude-settings.json. Claude Code runs hook commands in Git @@ -19,10 +19,18 @@ * wrapper there, a file without an extension, for the program. */ -import { execFile } from "node:child_process"; import fs from "node:fs/promises"; import path from "node:path"; -import { CODEX_SERVER_ARGS, CODEX_WRAPPER } from "../../../shared/codex/wrapper"; +import { + CODEX_CMD_BATCH_HELP, + CODEX_CMD_BATCH_HELP_FILE, + CODEX_CMD_HELP, + CODEX_CMD_HELP_FILE, + CODEX_CMD_LAUNCHER, + CODEX_POWERSHELL_PROBE, + CODEX_SERVER_ARGS, + CODEX_WRAPPER, +} from "../../../shared/codex/wrapper"; import { createLogger } from "../../../shared/log/main"; import { getAgentBinDir } from "../../infra/agent/getAgentBinDir"; import { binDir, ensureDir, root } from "../../infra/agent/runtimeDirs"; @@ -35,8 +43,8 @@ export interface WindowsAgentFiles { readonly binDir: string; /** The hooks settings file PowerShell and cmd pass to claude, if written. */ readonly settingsPath?: string; - /** Whether the codex cmd finds takes --no-daemon. */ - readonly cmdCodexNoDaemon: boolean; + /** The directory containing codex.cmd, added to cmd's PATH. */ + readonly cmdLauncherDir?: string; } /** @@ -58,30 +66,42 @@ let installing: Promise | undefined; /** * Copies the wrappers to ~/.nexus-code/bin and writes claude's hooks to - * ~/.nexus-code/claude-settings.json, once per run of the app. The copies + * ~/.nexus-code/claude-settings.json, caching a complete install. The copies * keep Git Bash off the MSIX package's files, which a process outside the * package may not use. Null when the directory can't be written; the * terminals then run claude and codex as they are. */ export function installWindowsAgentFiles(): Promise { - installing ??= (async () => { - const dir = binDir(); - try { - await ensureDir(dir); - await fs.writeFile(path.join(dir, "codex"), CODEX_WRAPPER); - } catch (error) { - log.warn(`wrappers unavailable in Windows terminals: ${(error as Error).message}`); - return null; - } - const [settingsPath, cmdCodexNoDaemon] = await Promise.all([ - installClaudeFiles(dir), - cmdCodexTakesNoDaemon(), - ]); - return { binDir: dir, settingsPath, cmdCodexNoDaemon }; - })(); + installing ??= installFiles().then( + (files) => { + if (!files?.settingsPath || !files.cmdLauncherDir) installing = undefined; + return files; + }, + (error: unknown) => { + installing = undefined; + throw error; + }, + ); return installing; } +/** Returns the usable parts of this attempt even when another part fails. */ +async function installFiles(): Promise { + const dir = binDir(); + try { + await ensureDir(dir); + await fs.writeFile(path.join(dir, "codex"), CODEX_WRAPPER); + } catch (error) { + log.warn(`wrappers unavailable in Windows terminals: ${(error as Error).message}`); + return null; + } + const [settingsPath, cmdLauncherDir] = await Promise.all([ + installClaudeFiles(dir), + installCmdLauncher(path.join(dir, "cmd")), + ]); + return { binDir: dir, settingsPath, cmdLauncherDir }; +} + /** Copies the claude wrapper to `dir` and writes its hooks; their file, if written. */ async function installClaudeFiles(dir: string): Promise { try { @@ -98,20 +118,18 @@ async function installClaudeFiles(dir: string): Promise { } } -/** - * Whether the codex on PATH, as cmd finds it, has --no-daemon. A doskey - * macro can't ask at each run, as the wrapper and the PowerShell function - * do, so cmd's terminals go by what it had when the app started. - */ -function cmdCodexTakesNoDaemon(): Promise { - return new Promise((resolve) => { - execFile( - "cmd.exe", - ["/d", "/s", "/c", "codex --help"], - { windowsHide: true, timeout: 5000 }, - (_error, stdout) => resolve(String(stdout).includes("--no-daemon")), - ); - }); +/** Installs the launcher in a directory used only by cmd terminals. */ +async function installCmdLauncher(dir: string): Promise { + try { + await ensureDir(dir); + await fs.writeFile(path.join(dir, CODEX_CMD_HELP_FILE), CODEX_CMD_HELP); + await fs.writeFile(path.join(dir, CODEX_CMD_BATCH_HELP_FILE), CODEX_CMD_BATCH_HELP); + await fs.writeFile(path.join(dir, "codex.cmd"), CODEX_CMD_LAUNCHER); + return dir; + } catch (error) { + log.warn(`codex launcher unavailable in cmd: ${(error as Error).message}`); + return undefined; + } } /** @@ -150,7 +168,8 @@ const POWERSHELL_CODEX = `function global:codex { if (-not $real) { Write-Error 'codex: command not found'; return } $flags = @() $server = $args | Where-Object { $_ -in @(${CODEX_SERVER_ARGS.map((arg) => `'${arg}'`).join(", ")}) -or "$_" -like '--remote=*' } - if (-not $server -and ((& $real.Source --help 2>$null) -match '--no-daemon')) { $flags = , '--no-daemon' } + ${CODEX_POWERSHELL_PROBE} + if (-not $server -and (Test-NexusCodexNoDaemon $real.Source)) { $flags = , '--no-daemon' } if ($MyInvocation.ExpectingInput) { $input | & $real.Source @flags @args } else { & $real.Source @flags @args } }`; @@ -169,10 +188,10 @@ export interface WindowsShellShimInput { /** The directory of the wrappers' copies, which Git Bash runs as `claude` and `codex`. */ wrapperDir?: string; /** - * Codex's --no-daemon: PowerShell's function, and with `cmd` cmd's macro - * too. Without it codex is left as it is. + * Installs PowerShell's codex function and enables the cmd launcher when + * its directory is available. */ - codex?: { readonly cmd: boolean }; + codex?: { readonly cmdLauncherDir?: string }; /** The ORIGINAL_PATH the shell inherits from the app, if any. */ originalPath?: string; } @@ -229,28 +248,21 @@ function shimShell(input: WindowsShellShimInput): WindowsShellShimOutput { if (name === "cmd.exe") { if (args?.some((arg) => /^\/[ck]/i.test(arg))) return { env, args }; - // cmd expands the variable in the /k command, and runs each macro's - // doskey in turn. Written out, the quotes around the path would reach + const cmdEnv = codex?.cmdLauncherDir + ? { ...env, NEXUS_CMD_CODEX_DIR: codex.cmdLauncherDir } + : env; + if (settingsPath === undefined) return { env: cmdEnv, args }; + // cmd expands the variable in the /k command. Written out, the quotes around the path would reach // cmd escaped for a program, which cmd doesn't read. Inside a macro, - // `claude` and `codex` are the programs on PATH. A macro can't look at - // its arguments, so codex in cmd takes --no-daemon for `agents` and - // `queue` too, which refuse it, and claude's `-p` mark is left to the + // claude is the program on PATH, and its `-p` mark is left to the // hook client (internal/hookclient). claude carries the tab's mark // while it runs, as PowerShell's does: the escaped `&` joins the // commands of the macro, and its %NEXUS_TAB_ID% is expanded as it runs. // cmd expands a macro only as a line's first word: `cd x & claude` or a // .bat's claude runs without the settings, its hooks and all (docs/CLI.md). - const macros = [ - ...(settingsPath !== undefined - ? [ - `doskey claude=set "NEXUS_CLAUDE_TAB_ID=%NEXUS_TAB_ID%" ^& claude --settings "${settingsPath}" $* ^& set "NEXUS_CLAUDE_TAB_ID="`, - ] - : []), - ...(codex?.cmd ? ["doskey codex=codex --no-daemon $*"] : []), - ]; - if (macros.length === 0) return { env, args }; + const macro = `doskey claude=set "NEXUS_CLAUDE_TAB_ID=%NEXUS_TAB_ID%" ^& claude --settings "${settingsPath}" $* ^& set "NEXUS_CLAUDE_TAB_ID="`; return { - env: { ...env, NEXUS_CMD_INIT: macros.join(" & ") }, + env: { ...cmdEnv, NEXUS_CMD_INIT: macro }, args: [...(args ?? []), "/k", "%NEXUS_CMD_INIT%"], }; } diff --git a/src/main/features/remote/account-keys.ts b/src/main/features/remote/account-keys.ts index 1a7b562c9..ef3ea8ee6 100644 --- a/src/main/features/remote/account-keys.ts +++ b/src/main/features/remote/account-keys.ts @@ -8,6 +8,7 @@ * from an envelope after another device rotated it, rotates it when it * removes a device, and gives the server the password's proof where it asks. */ + import { z } from "zod"; import { ACCOUNT_AUTH_ERROR, @@ -22,6 +23,7 @@ import { KeyEnvelopesResponseSchema, type KeyRotation, } from "../../../../packages/remote-protocol/src"; +import { createLogger } from "../../../shared/log/main"; import type { AccountKeyState, KeyFailure, @@ -49,6 +51,8 @@ import { import { ServerError, SignedOutError, UnreachableError } from "./errors"; import type { Kept } from "./sealed-file"; +const log = createLogger("remote-account-keys"); + const BUNDLE_PATH = "/account/key-bundle"; /** The account key as this device keeps it, sealed: whose, and which version. */ @@ -366,7 +370,7 @@ export class AccountKeys { // A device with an older key, or none, asks for the password, as before v2. if (!device.key || device.key.kid !== k) continue; if (!certChecks(opened.key, userId, device.id, device.key)) { - console.warn(`[remote] device ${device.id}'s key does not check; the key was not rotated`); + log.warn(`device ${device.id}'s key does not check; the key was not rotated`); throw new KeyError("device-key"); } envelopes.push({ @@ -427,19 +431,19 @@ export class AccountKeys { if (found && this.held && found.bundle.kid > this.held.kid) { // Another device rotated the key: an envelope may carry it here. await this.takeEnvelopes().catch((error: unknown) => - console.warn("[remote] taking the new account key from an envelope failed:", error), + log.warn(`Could not open the new account key envelope: ${String(error)}`), ); } if (!found) this.set({ status: "missing" }); else if (this.held?.kid === found.bundle.kid) { this.set({ status: "unlocked", kid: found.bundle.kid, ...this.extras() }); await this.syncDeviceKey().catch((error: unknown) => - console.warn("[remote] registering this device's key failed:", error), + log.warn(`Could not register this device key: ${String(error)}`), ); } else if (this.held && this.older(found)) { // A key older than the one held: the server went back, or is made to. The newer key stays. - console.warn( - `[remote] the server's account key (version ${found.bundle.kid}) is older than this device's (${this.held.kid}); kept this one`, + log.warn( + `the server's account key (version ${found.bundle.kid}) is older than this device's (${this.held.kid}); kept this one`, ); this.set({ status: "unlocked", kid: this.held.kid, ...this.extras() }); } else this.set({ status: "locked", kid: found.bundle.kid, ...this.extras() }); @@ -494,7 +498,7 @@ export class AccountKeys { held: current, }); if (!key) { - console.warn(`[remote] an envelope of key version ${envelope.kid} did not open`); + log.warn(`an envelope of key version ${envelope.kid} did not open`); break; } current = { kid: envelope.kid, key }; @@ -627,7 +631,7 @@ export class AccountKeys { } catch (error) { if (error instanceof WrongPasswordError) throw new KeyError("wrong-password"); if (error instanceof KdfTooHeavyError) { - console.warn(`[remote] ${error.message}`); + log.warn(`${error.message}`); throw new KeyError("too-heavy"); } throw error; diff --git a/src/main/features/remote/account.ts b/src/main/features/remote/account.ts index bd8a5f97e..3e341e1af 100644 --- a/src/main/features/remote/account.ts +++ b/src/main/features/remote/account.ts @@ -10,6 +10,7 @@ import os from "node:os"; import { z } from "zod"; import { type ServerInfo, ServerInfoSchema } from "../../../../packages/remote-protocol/src"; +import { createLogger } from "../../../shared/log/main"; import { type RemoteAccountState, type RemoteDeviceInfo, @@ -18,12 +19,15 @@ import { type SignInFailure, type SignInProvider, } from "../../../shared/remote/account"; +import { ResponseTooLargeError, readResponseText } from "../../infra/http/body"; import { ServerError, SignedOutError, SignInError, UnreachableError } from "./errors"; import { type LoopbackListener, listenForCallback } from "./loopback"; import { makePkce, makeState } from "./pkce"; import { ServerRoute } from "./route"; import type { Kept } from "./sealed-file"; +const log = createLogger("remote-account"); + export { SignedOutError } from "./errors"; /** How long a call to the server may take. */ @@ -182,7 +186,7 @@ export class RemoteAccount { await this.deps.store.save(this.saved); } catch (error) { this.failure = failureOf(error); - console.warn("[remote] sign-in failed:", error instanceof Error ? error.message : error); + log.warn(`Sign-in failed: ${error instanceof Error ? error.message : String(error)}`); } finally { listener.close(); this.signingIn = null; @@ -245,17 +249,20 @@ export class RemoteAccount { /** * Asks the server as this device: the JSON it answered, or a ServerError; * a 401 signs the app out and throws SignedOutError. `headers` adds to the - * request's own (the account password's proof, say). + * request's own (the account password's proof, say). An aborted request + * cannot update the route or sign the account out. */ request( method: string, path: string, body?: unknown, headers?: Record, + signal?: AbortSignal, ): Promise { return this.call(method, path, { ...(body !== undefined ? { body } : {}), ...(headers ? { headers } : {}), + signal, }); } @@ -284,8 +291,11 @@ export class RemoteAccount { headers?: Record; signedIn?: boolean; timeoutMs?: number; + signal?: AbortSignal; } = {}, ): Promise { + const { signal } = options; + signal?.throwIfAborted(); const headers: Record = { ...options.raw?.headers, ...options.headers }; if (options.signedIn !== false) { if (!this.saved) throw new SignedOutError(); @@ -298,18 +308,21 @@ export class RemoteAccount { : options.body !== undefined ? JSON.stringify(options.body) : undefined; - const send = (base: string) => - this.fetch(this.route.url(path, { base }), { + const send = (base: string) => { + const timeout = AbortSignal.timeout(options.timeoutMs ?? REQUEST_TIMEOUT_MS); + return this.fetch(this.route.url(path, { base }), { method, headers, - signal: AbortSignal.timeout(options.timeoutMs ?? REQUEST_TIMEOUT_MS), + signal: signal ? AbortSignal.any([signal, timeout]) : timeout, ...(body !== undefined ? { body: body as NonNullable } : {}), }); + }; const base = this.route.base(); let res: Response; try { res = await send(base); } catch (error) { + signal?.throwIfAborted(); // fetch fails with a TypeError when the server cannot be reached, a TimeoutError when it // is slow. The proxy not reached, the server is asked straight, now and for a while; a // request that timed out may have gone through, and is not sent again. @@ -317,14 +330,24 @@ export class RemoteAccount { try { res = await send(this.route.base()); } catch (again) { + signal?.throwIfAborted(); throw new UnreachableError(again); } } + // A stale relay ticket can no longer change the account or its route. + signal?.throwIfAborted(); if (res.status === 401 && options.signedIn !== false) { + void res.body?.cancel().catch(() => {}); this.forget(); throw new SignedOutError(); } - const text = await res.text(); + const text = await readResponseText(res).catch((error: unknown) => { + if (error instanceof ResponseTooLargeError) { + throw new ServerError(res.status, error.code, `${method} ${path}`); + } + throw error; + }); + signal?.throwIfAborted(); const answer = parseJson(text); if (!res.ok) { const code = isRecord(answer) && typeof answer.error === "string" ? answer.error : undefined; diff --git a/src/main/features/remote/agent-file-writes.ts b/src/main/features/remote/agent-file-writes.ts new file mode 100644 index 000000000..1b616b33f --- /dev/null +++ b/src/main/features/remote/agent-file-writes.ts @@ -0,0 +1,89 @@ +import { z } from "zod"; +import { RpcErrorCode } from "../../../../packages/remote-protocol/src"; +import type { AgentCallOptions } from "../../infra/agent/channel"; +import { parseAgentResult } from "../fs/bridge/agent-provider"; +import type { AgentBackedProvider } from "../fs/bridge/provider"; +import { filePathRules, fileVerifyTimeoutMs, validFileName } from "./file-io"; +import { workspaceWritePath } from "./file-writes"; +import { RpcFailure } from "./room"; + +const pathResult = z.object({ path: z.string() }); + +export class AgentFileWrites { + private readonly relPath: string; + + constructor( + private readonly fs: AgentBackedProvider, + root: string, + path: string, + private readonly directory: boolean, + ) { + const rel = workspaceWritePath(root, path, directory); + if (rel && !rel.split("/").every((part) => validFileName(part, filePathRules(root)))) { + throw new RpcFailure(RpcErrorCode.InvalidParams, "the destination needs a file name"); + } + this.relPath = rel; + } + + async resolve(): Promise { + const result = await this.call("resolve", { relPath: this.relPath, directory: this.directory }); + return parseAgentResult(pathResult, result).path; + } + + async mkdir(expectedPath: string): Promise<{ path: string }> { + await this.call("mkdir", { relPath: this.relPath, expectedPath }); + return { path: expectedPath }; + } + + async begin(uploadId: string, expectedPath: string, size: number): Promise { + await this.call("begin", { relPath: this.relPath, expectedPath, uploadId, size }); + } + + async chunk(uploadId: string, offset: number, data: string): Promise { + await this.call("chunk", { uploadId, offset, data }); + } + + async verify(uploadId: string, sha256: string, size: number): Promise { + await this.call("verify", { uploadId, sha256 }, { timeoutMs: fileVerifyTimeoutMs(size) }); + } + + async commit(uploadId: string): Promise { + await this.call("commit", { uploadId }); + } + + async abort(uploadId: string): Promise { + await this.call("abort", { uploadId }); + } + + private async call(method: string, params: object, options?: AgentCallOptions): Promise { + try { + return await this.fs.callAgentMethod(`fs.upload.${method}`, params, options); + } catch (error) { + const code = (error as { code?: string } | null)?.code; + const message = error instanceof Error ? error.message : String(error); + switch (code) { + case "unsupported-method": + throw new RpcFailure( + RpcErrorCode.Unavailable, + "the remote filesystem agent must be updated to write files", + ); + case "OUT_OF_WORKSPACE": + case "PERMISSION_DENIED": + throw new RpcFailure(RpcErrorCode.Forbidden, message); + case "NOT_FOUND": + throw new RpcFailure(RpcErrorCode.NotFound, message); + case "IS_DIRECTORY": + case "NOT_DIRECTORY": + case "server.protocol-error": + throw new RpcFailure(RpcErrorCode.InvalidParams, message); + case "NO_SPACE": + throw new RpcFailure( + RpcErrorCode.Unavailable, + "the destination has insufficient disk space", + ); + default: + throw new RpcFailure(RpcErrorCode.Unavailable, message); + } + } + } +} diff --git a/src/main/features/remote/agent-gate.ts b/src/main/features/remote/agent-gate.ts index 3c6c404bf..1a85e1fa0 100644 --- a/src/main/features/remote/agent-gate.ts +++ b/src/main/features/remote/agent-gate.ts @@ -21,6 +21,7 @@ import type { RelaySender } from "../../../shared/chat/relay"; import type { CliChange } from "../../../shared/cli/ui"; import type { RemoteAgentsPolicy } from "../../../shared/types/app-state"; import type { CliServices } from "../cli/context"; +import { filePathRules } from "./file-io"; import { RpcFailure } from "./room"; const MINUTE_MS = 60_000; @@ -46,6 +47,32 @@ export interface AgentGateDeps { now?: () => number; } +const FILE_CONSENT_IDLE_MS = 120_000; +const MAX_FILE_GRANTS = 256; + +interface FileConsent { + /** A host-resolved path: reads follow links, writes resolve their parent. */ + path: string; + tree: boolean; + /** Keep approvals tied to the workspace location used when the user answered. */ + workspaceRoot: string; +} + +interface FileGrant { + readonly key: string; + readonly path: string; + readonly tree: boolean; + expiresAt: number; +} + +function covers(grant: FileGrant, target: string): boolean { + const { sep } = filePathRules(grant.path); + return ( + target === grant.path || + (grant.tree && target.startsWith(grant.path.endsWith(sep) ? grant.path : grant.path + sep)) + ); +} + const forbidden = (message: string) => new RpcFailure(RpcErrorCode.Forbidden, message); export class AgentGate { @@ -59,6 +86,8 @@ export class AgentGate { /** "device|its workspace>this workspace" pairs the user allowed until restart. */ private readonly allowed = new Set(); + private fileGrants: FileGrant[] = []; + constructor(private readonly deps: AgentGateDeps) {} private now(): number { @@ -88,11 +117,65 @@ export class AgentGate { "too many messages from that agent", ); refuseBeyondAsked(request); + await this.consent(services, request); + } + + /** File transfers use the same prompt and session approvals as other controls. */ + async checkFile(services: CliServices, request: AgentRequest, scope: FileConsent): Promise { + const { device, method, params } = request; + const reading = + method === "files.read" || (method === "files.list" && params.writeTarget !== true); + const transferId = params.transferId; + // Writing one file takes one approval at begin. A recursive copy starts + // with mkdir and may reuse that approval for the directory's children. + if (!reading && typeof transferId !== "string") { + await this.consent(services, request, scope); + return; + } + const key = JSON.stringify([ + device.id, + params.from.workspaceId, + params.from.tabId, + params.workspaceId, + scope.workspaceRoot, + reading, + params.anywhere === true, + transferId, + ]); + this.checkRead(); + const now = this.now(); + this.fileGrants = this.fileGrants.filter((grant) => grant.expiresAt > now); + const grant = this.fileGrants.find( + (entry) => + entry.key === key && + covers(entry, scope.path) && + (entry.tree || !scope.tree) && + (method !== "files.write.begin" || entry.path !== scope.path), + ); + if (grant) { + grant.expiresAt = now + FILE_CONSENT_IDLE_MS; + return; + } + const answer = await this.consent(services, request, scope); + if (answer === "once" && (reading || scope.tree)) { + if (this.fileGrants.length >= MAX_FILE_GRANTS) this.fileGrants.shift(); + this.fileGrants.push({ + key, + path: scope.path, + tree: scope.tree, + expiresAt: this.now() + FILE_CONSENT_IDLE_MS, + }); + } + } + + private async consent(services: CliServices, request: AgentRequest, scope?: FileConsent) { + const { device, params } = request; + const { from } = params; const policy = this.deps.policy(); if (policy === "deny") throw forbidden("control by agents is off there (Settings › Remote)"); if (policy === "allow") return; - const change = await changeOf(services, request); - const pair = `${device.id}|${from.workspaceId}>${params.workspaceId}`; + const change = await changeOf(services, request, scope); + const pair = JSON.stringify([device.id, from.workspaceId, params.workspaceId]); if (this.allowed.has(pair)) return; const waiting = this.prompting.get(device.id) ?? 0; if (waiting >= MAX_PENDING_PROMPTS_PER_DEVICE) { @@ -128,8 +211,11 @@ export class AgentGate { if (left > 0) this.prompting.set(device.id, left); else this.prompting.delete(device.id); }); - if (answer === "session") this.allowed.add(pair); - if (answer === "once" || answer === "session") return; + if (answer === "once" || answer === "session") { + this.checkRead(); + if (answer === "session") this.allowed.add(pair); + return answer; + } throw forbidden( answer === "timeout" ? "the user there did not answer in time; nothing was done" @@ -199,10 +285,32 @@ async function tabTitle(services: CliServices, workspaceId: string, tabId: strin async function changeOf( services: CliServices, { method, params }: AgentRequest, + scope?: FileConsent, ): Promise { const { workspaceId, tabId = "" } = params; const title = () => tabTitle(services, workspaceId, tabId); switch (method) { + case "files.write.begin": + return { + kind: "fileWrite", + detail: scope?.path ?? String(params.path), + body: `${String(params.size)} B`, + terminal: params.anywhere === true, + }; + case "files.mkdir": + return { + kind: scope?.tree ? "fileWriteTree" : "fileMkdir", + detail: scope?.path ?? String(params.path), + terminal: params.anywhere === true, + }; + case "files.list": + case "files.read": + return { + kind: + params.writeTarget === true ? "fileWriteTree" : scope?.tree ? "fileReadTree" : "fileRead", + detail: scope?.path ?? String(params.path), + terminal: true, + }; case "chat.send": return { kind: "chatInput", detail: await title(), body: String(params.text) }; case "chat.new": diff --git a/src/main/features/remote/chat-watches.ts b/src/main/features/remote/chat-watches.ts index e1f4b56dd..22bfa1b3d 100644 --- a/src/main/features/remote/chat-watches.ts +++ b/src/main/features/remote/chat-watches.ts @@ -14,14 +14,8 @@ import { } from "../../../../packages/remote-protocol/src"; import type { CliServices } from "../cli/context"; import { fitsInFrame, RpcFailure } from "./room"; -import { - DEFAULT_READ_ITEMS, - fitOversizeCodexChildren, - fitPage, - fitsAsResult, - tabOf, - unavailable, -} from "./serve"; +import { DEFAULT_READ_ITEMS, fitOversizeCodexChildren, fitPage } from "./serving/chat-details"; +import { fitsAsResult, tabOf, unavailable } from "./serving/common"; /** Most chats one device may watch here at once; a new one beyond ends its oldest. */ export const MAX_WATCHES_PER_DEVICE = 8; diff --git a/src/main/features/remote/file-io.ts b/src/main/features/remote/file-io.ts new file mode 100644 index 000000000..b35ff0598 --- /dev/null +++ b/src/main/features/remote/file-io.ts @@ -0,0 +1,82 @@ +import type { FileHandle } from "node:fs/promises"; +import path from "node:path"; + +/** RPCs used to copy files between devices. */ +export function isFileTransferMethod(method: string): boolean { + return ( + method === "files.read" || + method === "files.list" || + method === "files.mkdir" || + method.startsWith("files.write.") + ); +} + +export function needsFileConsent(method: string, params: { anywhere?: true }): boolean { + return ( + method === "files.write.begin" || + method === "files.mkdir" || + (params.anywhere === true && (method === "files.read" || method === "files.list")) + ); +} + +export const FILE_WRITE_BUSY = "the host is busy writing files"; + +/** Budget for a full re-read and hash at 32 MiB/s, plus request overhead. */ +export function fileVerifyTimeoutMs(size: number): number { + return 30_000 + Math.ceil((size * 1000) / (32 * 1024 ** 2)); +} + +export function filePathRules(root: string): path.PlatformPath { + return /^(?:[A-Za-z]:[\\/]|[\\/]{2}[^\\/])/.test(root) ? path.win32 : path.posix; +} + +export function validFileName(name: string, rules: path.PlatformPath): boolean { + if ( + !name || + name === "." || + name === ".." || + name.includes("\0") || + rules.basename(name) !== name + ) { + return false; + } + if (rules !== path.win32) return true; + return ( + !/[<>:"|?*]|[. ]$/.test(name) && + !Array.from(name).some((c) => c.charCodeAt(0) < 32) && + !/^(?:con|prn|aux|nul|com[1-9¹²³]|lpt[1-9¹²³]|conin\$|conout\$)(?:\.|$)/i.test(name) + ); +} + +export async function writeFileChunk( + file: FileHandle, + bytes: Buffer, + offset: number, +): Promise { + let written = 0; + while (written < bytes.length) { + const { bytesWritten } = await file.write( + bytes, + written, + bytes.length - written, + offset + written, + ); + if (bytesWritten === 0) throw new Error("the write made no progress"); + written += bytesWritten; + } +} + +export async function readFileChunk( + file: FileHandle, + offset: number, + length: number, +): Promise { + const bytes = Buffer.allocUnsafe(length); + let read = 0; + while (read < length) { + const { bytesRead } = await file.read(bytes, read, length - read, offset + read); + if (bytesRead === 0) throw new Error("the file ended before the expected size"); + read += bytesRead; + } + return bytes; +} diff --git a/src/main/features/remote/file-writes.ts b/src/main/features/remote/file-writes.ts new file mode 100644 index 000000000..8abf25e79 --- /dev/null +++ b/src/main/features/remote/file-writes.ts @@ -0,0 +1,517 @@ +import { createHash, randomUUID } from "node:crypto"; +import type { Stats } from "node:fs"; +import { + type FileHandle, + lstat, + mkdir, + open, + opendir, + realpath, + rename, + stat, + statfs, + unlink, +} from "node:fs/promises"; +import path from "node:path"; +import { + MAX_BYTES_CHUNK, + RpcErrorCode, + type RpcParams, + type RpcResult, +} from "../../../../packages/remote-protocol/src"; +import { createLogger } from "../../../shared/log/main"; +import type { AgentFileWrites } from "./agent-file-writes"; +import { FILE_WRITE_BUSY, readFileChunk, validFileName, writeFileChunk } from "./file-io"; +import { absoluteFilePath, placeOf } from "./files"; +import { RpcFailure } from "./room"; + +const log = createLogger("remote"); +const MAX_PER_DEVICE = 4; +const MAX_UPLOADS = 8; +const MAX_ACTIVE_CHUNKS = 4; +const MAX_RANGES = 4096; +const MAX_IDLE_MS = 120_000; +const STALE_TEMP_MS = 24 * 60 * 60_000; +const TEMP_UPLOAD = /^\.nexus-push-[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}\.tmp$/; +const BASE64 = /^[A-Za-z0-9+/]*={0,2}$/; + +type Begin = RpcParams<"files.write.begin">; +type Scope = Pick; +type UploadRef = RpcParams<"files.write.abort">; +type UploadResult = RpcResult<"files.write.end">; +type Range = [start: number, end: number]; +type Authorize = (destination?: string) => void; + +type ScheduleExpiry = (expire: () => Promise, idleMs: number) => () => void; + +const scheduleExpiry: ScheduleExpiry = (expire, idleMs) => { + const timer = setTimeout(() => void expire(), idleMs); + timer.unref(); + return () => clearTimeout(timer); +}; + +interface Destination { + path: string; + parent: string; + parentInfo: Stats; +} + +interface Upload { + device: string; + request: Begin; + root: string; + authorize: Authorize; + controller: AbortController; + work: Set>; + ranges: Range[]; + sealed: boolean; + started: boolean; + destination?: Destination; + agent?: { writer: AgentFileWrites; path: string }; + temporary?: string; + file?: FileHandle; + fileInfo?: Stats; + reserved: number; + cancelExpiry?: () => void; + cleanup?: Promise; +} + +const invalid = (message: string) => new RpcFailure(RpcErrorCode.InvalidParams, message); +const forbidden = (message: string) => new RpcFailure(RpcErrorCode.Forbidden, message); +const busy = () => new RpcFailure(RpcErrorCode.Unavailable, FILE_WRITE_BUSY); + +function fileError(error: unknown): never { + if (error instanceof RpcFailure) throw error; + const code = (error as NodeJS.ErrnoException).code; + if (code === "ENOENT" || code === "ENOTDIR") { + throw new RpcFailure(RpcErrorCode.NotFound, "the destination's parent folder does not exist"); + } + if (code === "ENOSPC" || code === "EDQUOT") { + throw new RpcFailure(RpcErrorCode.Unavailable, "the destination has insufficient disk space"); + } + if (code === "EACCES" || code === "EPERM") { + throw forbidden("the destination cannot be written by NexusCode"); + } + throw error; +} + +function sameFile(left: Stats, right: Stats): boolean { + return left.dev === right.dev && left.ino === right.ino; +} + +function optionalStat(target: string): Promise { + return lstat(target).catch((error: NodeJS.ErrnoException) => { + if (error.code !== "ENOENT") throw error; + return undefined; + }); +} + +export function workspaceWritePath(root: string, requested: string, directory: boolean): string { + const place = placeOf(root, [], requested); + if (place.kind !== "workspace") throw forbidden("the path is outside the workspace's folder"); + if (!place.rel && !directory) throw invalid("the destination is a folder"); + return place.rel; +} + +async function locate(root: string, request: Scope, directory = false): Promise { + let asked: string; + if (request.anywhere) { + asked = absoluteFilePath(request.path); + } else { + const rel = workspaceWritePath(root, request.path, directory); + asked = absoluteFilePath(path.join(root, rel)); + if (rel) { + let prefix = root; + for (const part of rel.split("/").slice(0, -1)) { + prefix = path.join(prefix, part); + if ((await lstat(prefix)).isSymbolicLink()) { + throw forbidden("the path goes through a symbolic link"); + } + } + } + } + // A workspace root (including on a mount point) is already a directory. + const workspaceRoot = !request.anywhere && !path.relative(root, asked); + const parent = await realpath(workspaceRoot ? root : path.dirname(asked)); + if (!request.anywhere) { + const base = await realpath(root); + // Canonical names use the spelling on disk, including on Windows. + if (base !== parent && !parent.startsWith(base.endsWith(path.sep) ? base : base + path.sep)) { + throw forbidden("the path leads outside the workspace's folder"); + } + } + const name = path.basename(asked); + if (!validFileName(name, path) && !(directory && asked === path.parse(asked).root)) { + throw invalid("the destination needs a file name"); + } + const parentInfo = await stat(parent); + if (!parentInfo.isDirectory()) throw invalid("the destination's parent is not a folder"); + return { path: workspaceRoot ? parent : path.join(parent, name), parent, parentInfo }; +} + +/** Resolve the path to show before a write is approved. */ +export async function fileWriteDestination( + root: string, + request: Scope, + directory: boolean, +): Promise { + try { + return (await locate(root, request, directory)).path; + } catch (error) { + return fileError(error); + } +} + +async function existingFile(target: string): Promise { + const current = await optionalStat(target); + if (current?.isSymbolicLink()) throw forbidden("the destination is a symbolic link"); + if (current && !current.isFile()) throw invalid("the destination is not a regular file"); + return current; +} + +/** Add a disjoint write to a compact set of covered byte ranges. */ +function cover(ranges: Range[], start: number, end: number): void { + const index = ranges.findIndex((range) => range[1] >= start); + const at = index === -1 ? ranges.length : index; + const left = ranges[at]; + const next = left?.[1] === start ? ranges[at + 1] : left; + if (next && next[0] < end) throw invalid("the piece overlaps bytes already received"); + const joinsLeft = left?.[1] === start; + const joinsRight = next?.[0] === end; + if (!joinsLeft && !joinsRight && ranges.length >= MAX_RANGES) { + throw invalid("the upload has too many gaps between pieces"); + } + ranges.splice(at, Number(joinsLeft) + Number(joinsRight), [ + joinsLeft ? left[0] : start, + joinsRight ? next[1] : end, + ]); +} + +export async function makeRemoteDirectory( + root: string, + request: Scope, + authorize: Authorize, +): Promise> { + try { + const destination = await locate(root, request, true); + authorize(destination.path); + try { + await mkdir(destination.path); + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error; + const current = await lstat(destination.path); + if (current.isSymbolicLink()) throw forbidden("the destination is a symbolic link"); + if (!current.isDirectory()) throw invalid("the destination is not a folder"); + } + return { path: destination.path }; + } catch (error) { + return fileError(error); + } +} + +/** Best-effort cleanup of abandoned push files from earlier processes. */ +async function removeStaleUploads( + destination: Destination, + checkDestination: () => Promise, +): Promise { + const cutoff = Date.now() - STALE_TEMP_MS; + const target = path.basename(destination.path).toLowerCase(); + try { + const folder = await opendir(destination.parent); + for await (const entry of folder) { + if (entry.name === target || !entry.isFile() || !TEMP_UPLOAD.test(entry.name)) continue; + const temporary = path.join(destination.parent, entry.name); + const info = await optionalStat(temporary).catch(() => undefined); + if (!info?.isFile() || info.mtimeMs >= cutoff) continue; + await checkDestination(); + const current = await optionalStat(temporary).catch(() => undefined); + if (current?.isFile() && current.mtimeMs < cutoff && sameFile(info, current)) { + await unlink(temporary).catch(() => {}); + } + } + } catch { + // Listing access is optional; begin rechecks the destination before it creates a file. + } +} + +/** Device-owned uploads, written to disk as pieces arrive and published by rename. */ +export class FileWrites { + private readonly uploads = new Map(); + + constructor(private readonly schedule: ScheduleExpiry = scheduleExpiry) {} + + async begin( + device: string, + root: string, + request: Begin, + authorize: Authorize, + agent?: Upload["agent"], + ): Promise> { + authorize(); + if ( + this.uploads.size >= MAX_UPLOADS || + [...this.uploads.values()].filter((upload) => upload.device === device).length >= + MAX_PER_DEVICE + ) { + throw busy(); + } + const uploadId = randomUUID(); + const upload: Upload = { + device, + request, + root, + authorize, + controller: new AbortController(), + work: new Set(), + ranges: [], + sealed: false, + started: false, + reserved: 0, + agent, + }; + this.uploads.set(uploadId, upload); + this.touch(uploadId, upload); + try { + await this.work(upload, async () => { + if (agent) { + authorize(agent.path); + await agent.writer.begin(uploadId, agent.path, request.size); + upload.controller.signal.throwIfAborted(); + return; + } + const destination = await locate(root, request); + const checkDestination = async () => { + await this.verifyParent(root, request, destination); + upload.controller.signal.throwIfAborted(); + authorize(destination.path); + }; + await existingFile(destination.path); + await checkDestination(); + await removeStaleUploads(destination, checkDestination); + const space = await statfs(destination.parent, { bigint: true }); + const reserved = [...this.uploads.values()] + .filter((other) => other.destination?.parentInfo.dev === destination.parentInfo.dev) + .reduce((sum, other) => sum + other.reserved, 0); + if (BigInt(request.size + reserved) > space.bavail * space.bsize) { + throw new RpcFailure( + RpcErrorCode.Unavailable, + "the destination has insufficient disk space", + ); + } + upload.destination = destination; + upload.reserved = request.size; + await checkDestination(); + const temporary = path.join(destination.parent, `.nexus-push-${uploadId}.tmp`); + upload.file = await open(temporary, "wx+", 0o600); + upload.temporary = temporary; + upload.fileInfo = await upload.file.stat(); + await this.verifyParent(root, request, destination); + upload.controller.signal.throwIfAborted(); + }); + upload.started = true; + return { uploadId }; + } catch (error) { + await this.discard(uploadId, upload); + return fileError(error); + } + } + + async chunk(device: string, request: RpcParams<"files.write.chunk">): Promise { + const upload = this.mine(device, request); + if (upload.sealed || !upload.started) throw invalid("the upload is not accepting pieces"); + if (upload.work.size >= MAX_ACTIVE_CHUNKS) throw busy(); + if (!BASE64.test(request.data)) throw invalid("the piece is not base64"); + const bytes = Buffer.from(request.data, "base64"); + if ( + bytes.length === 0 || + bytes.length > MAX_BYTES_CHUNK || + bytes.toString("base64") !== request.data + ) { + throw invalid("the piece has an invalid length or encoding"); + } + const end = request.offset + bytes.length; + if (end > upload.request.size) throw invalid("the piece exceeds the declared file size"); + cover(upload.ranges, request.offset, end); + this.touch(request.uploadId, upload); + try { + await this.work(upload, async () => { + if (upload.agent) { + await upload.agent.writer.chunk(request.uploadId, request.offset, request.data); + } else { + if (!upload.file) throw invalid("the upload has not started"); + await writeFileChunk(upload.file, bytes, request.offset); + upload.reserved -= bytes.length; + } + }); + } catch (error) { + await this.discard(request.uploadId, upload); + return fileError(error); + } + } + + async end(device: string, request: RpcParams<"files.write.end">): Promise { + const upload = this.mine(device, request); + const { agent } = upload; + if (upload.sealed || upload.work.size > 0) throw busy(); + if (!upload.started) throw invalid("the upload has not started"); + upload.sealed = true; + upload.cancelExpiry?.(); + const signal = upload.controller.signal; + try { + return await this.work(upload, async () => { + const size = upload.request.size; + if ( + size !== 0 && + (upload.ranges.length !== 1 || + upload.ranges[0]?.[0] !== 0 || + upload.ranges[0][1] !== size) + ) { + throw invalid("the upload did not receive all the declared bytes"); + } + let destination: string; + if (agent) { + await agent.writer.verify(request.uploadId, request.sha256, size); + signal.throwIfAborted(); + upload.authorize(agent.path); + await agent.writer.commit(request.uploadId); + delete upload.agent; + destination = agent.path; + } else { + destination = await this.commitLocal(upload, request.sha256); + } + const result = { path: destination, size, sha256: request.sha256 }; + log.info( + `Received remote file ${JSON.stringify({ device, from: upload.request.from, ...result })}`, + ); + return result; + }); + } catch (error) { + return fileError(error); + } finally { + await this.discard(request.uploadId, upload); + } + } + + private async commitLocal(upload: Upload, sha256: string): Promise { + const { file, destination, temporary, fileInfo } = upload; + if (!file || !destination || !temporary || !fileInfo) + throw invalid("the upload has not started"); + const { size } = upload.request; + if ((await file.stat()).size !== size) { + throw invalid("the upload did not receive all the declared bytes"); + } + const signal = upload.controller.signal; + const hash = createHash("sha256"); + for (let offset = 0; offset < size; offset += MAX_BYTES_CHUNK) { + signal.throwIfAborted(); + hash.update(await readFileChunk(file, offset, Math.min(MAX_BYTES_CHUNK, size - offset))); + } + if (hash.digest("hex") !== sha256) throw invalid("the upload's SHA-256 did not match"); + await this.verifyParent(upload.root, upload.request, destination); + const current = await existingFile(destination.path); + if (process.platform !== "win32") { + const mode = current ? current.mode & 0o7777 : 0o666 & ~process.umask(); + await file.chmod(mode); + } + await file.close(); + delete upload.file; + await this.verifyParent(upload.root, upload.request, destination); + if (!sameFile(fileInfo, await lstat(temporary))) { + throw forbidden("the temporary file changed during the upload"); + } + signal.throwIfAborted(); + upload.authorize(destination.path); + await rename(temporary, destination.path); + delete upload.temporary; + return destination.path; + } + + async abort(device: string, request: UploadRef): Promise { + const upload = this.uploads.get(request.uploadId); + if (upload?.device !== device) return; + this.checkScope(upload, request); + await this.discard(request.uploadId, upload); + } + + async drop(device?: string): Promise { + await Promise.all( + [...this.uploads] + .filter(([, upload]) => device === undefined || upload.device === device) + .map(([id, upload]) => this.discard(id, upload)), + ); + } + + private mine(device: string, request: UploadRef): Upload { + const upload = this.uploads.get(request.uploadId); + if (!upload || upload.device !== device || upload.controller.signal.aborted) { + throw new RpcFailure(RpcErrorCode.NotFound, "no such pending file upload"); + } + this.checkScope(upload, request); + upload.authorize(); + return upload; + } + + private checkScope(upload: Upload, request: UploadRef): void { + if (request.anywhere && !upload.request.anywhere) + throw forbidden("the upload's scope cannot change"); + } + + private async verifyParent(root: string, request: Scope, expected: Destination): Promise { + const current = await locate(root, request); + if (current.path !== expected.path || !sameFile(current.parentInfo, expected.parentInfo)) { + throw forbidden("the destination folder changed during the upload"); + } + } + + private async work(upload: Upload, operation: () => Promise): Promise { + const pending = operation(); + upload.work.add(pending); + try { + return await pending; + } finally { + upload.work.delete(pending); + } + } + + private touch(id: string, upload: Upload): void { + upload.cancelExpiry?.(); + upload.cancelExpiry = this.schedule(() => this.discard(id, upload), MAX_IDLE_MS); + } + + private discard(id: string, upload: Upload): Promise { + if (upload.cleanup) return upload.cleanup; + upload.controller.abort(new RpcFailure(RpcErrorCode.Unavailable, "the upload was cancelled")); + upload.cancelExpiry?.(); + upload.cleanup = (async () => { + try { + if (upload.started && upload.agent) { + const [abort] = await Promise.allSettled([ + upload.agent.writer.abort(id), + Promise.allSettled(upload.work), + ]); + if (abort.status === "rejected") throw abort.reason; + } else { + await Promise.allSettled(upload.work); + await upload.agent?.writer.abort(id); + } + try { + await upload.file?.close(); + } finally { + if (upload.temporary) { + const current = await optionalStat(upload.temporary); + if (current && (!upload.fileInfo || sameFile(upload.fileInfo, current))) { + await unlink(upload.temporary); + } + } + } + } catch (error) { + log.warn( + `Could not clean up remote file upload ${id} from ${upload.device}: ${String(error)}`, + ); + } finally { + this.uploads.delete(id); + } + })(); + return upload.cleanup; + } +} diff --git a/src/main/features/remote/files.ts b/src/main/features/remote/files.ts index a0dae0dcd..a959f2b0f 100644 --- a/src/main/features/remote/files.ts +++ b/src/main/features/remote/files.ts @@ -21,6 +21,7 @@ import type { FsPreviewPath } from "../../../shared/fs/protocol"; import type { DirEntry } from "../../../shared/fs/types"; import { decodable } from "../../../shared/remote/image-size"; import type { FsProvider } from "../fs/bridge/provider"; +import { validFileName } from "./file-io"; import { fitsInFrame, MAX_MESSAGE_BYTES, RpcFailure } from "./room"; /** How much of a text file a read gives when it does not say. */ @@ -159,6 +160,36 @@ export function placeOf(root: string, folders: readonly string[], asked: string) throw forbidden(`${asked} is outside the workspace's folders`); } +/** Fully qualified paths on the desktop host, without Windows device paths or streams. */ +export function absoluteFilePath(asked: string): string { + const windows = process.platform === "win32"; + if ( + asked.includes("\0") || + (windows + ? !WINDOWS_ABSOLUTE.test(asked) || /^[\\/]{2}[?.][\\/]/.test(asked) + : !path.isAbsolute(asked)) + ) { + throw forbidden("an absolute file path on this computer is required"); + } + const cleanPath = clean(windows ? WINDOWS : POSIX, asked); + const parts = cleanPath.slice(path.parse(cleanPath).root.length).split(path.sep).filter(Boolean); + if (windows && !parts.every((part) => validFileName(part, path))) { + throw forbidden("that is no file path on this computer"); + } + return cleanPath; +} + +/** Resolve an outside source to the path the user approves. */ +export async function outsideFileSource(asked: string): Promise { + const absolute = absoluteFilePath(asked); + return nodeFs.realpath(absolute).catch((error: NodeJS.ErrnoException) => { + if (error.code === "ENOENT" || error.code === "ENOTDIR") { + throw new RpcFailure(RpcErrorCode.NotFound, `${asked} does not exist`); + } + throw new RpcFailure(RpcErrorCode.Unavailable, "the file could not be resolved"); + }); +} + /** * Where `asked` is, as placeOf finds it; outside the workspace's folders, * one of the files its chats name (took, wrote, linked, or a job's output), @@ -341,10 +372,13 @@ async function listHere(absolute: string): Promise { export async function listFiles( files: RemoteFiles, root: string, - { workspaceId, path: asked }: RpcParams<"files.list">, + { workspaceId, path: asked, anywhere }: RpcParams<"files.list">, + approvedPath?: string, ): Promise> { - const folders = await files.folders(workspaceId); - const place = placeOf(root, folders, asked); + const folders = anywhere ? [] : await files.folders(workspaceId); + const place: Place = anywhere + ? { kind: "folder", absolute: absoluteFilePath(asked) } + : placeOf(root, folders, asked); const style = styleOf(root); const fs = await files.fs(workspaceId); let entries: Entry[]; @@ -365,8 +399,11 @@ export async function listFiles( "a folder outside the workspace's is listed only on this computer, not on its host", ); } - await realWithin(fs, style, folders, place.absolute); - entries = await listHere(place.absolute); + if (!anywhere) await realWithin(fs, style, folders, place.absolute); + entries = await listHere(approvedPath ?? place.absolute); + if (approvedPath && (await outsideFileSource(asked)) !== approvedPath) { + throw fileChanged(false); + } absolute = place.absolute; } entries.sort((a, b) => @@ -406,7 +443,9 @@ export async function readFile( versioned, ifVersion, requireVersion, + anywhere, }: RpcParams<"files.read">, + approvedPath?: string, ): Promise> { const optedIn = versioned === true || ifVersion !== undefined || requireVersion !== undefined; if (ifVersion !== undefined && requireVersion !== undefined) { @@ -424,8 +463,10 @@ export async function readFile( if (length === 0 && !optedIn) { throw new RpcFailure(RpcErrorCode.InvalidParams, "a zero-length read requires version opt-in"); } - const folders = await files.folders(workspaceId); - const place = await placeToRead(files, workspaceId, root, folders, asked); + const folders = anywhere ? [] : await files.folders(workspaceId); + const place = anywhere + ? { kind: "folder" as const, absolute: absoluteFilePath(asked) } + : await placeToRead(files, workspaceId, root, folders, asked); const style = styleOf(root); const fs = await files.fs(workspaceId); let source: FsPreviewPath; @@ -440,8 +481,9 @@ export async function readFile( absolute = absoluteIn(style, clean(style, root), place.rel); } else { absolute = place.absolute; - source = - "output" in place + source = anywhere + ? { absolutePath: absolute, ...(approvedPath ? { expectedRealPath: approvedPath } : {}) } + : "output" in place ? { absolutePath: absolute } : { absolutePath: absolute, @@ -454,7 +496,7 @@ export async function readFile( const readLength = as === "bytes" ? (length ?? MAX_BYTES_CHUNK) : mediaType ? 0 : maxBytes; const verifyGrant = async () => { - if (place.kind !== "folder") return; + if (anywhere || place.kind !== "folder") return; // The grant may have been revoked while the workspace's agent was reading. const currentFolders = await files.folders(workspaceId); const current = await placeToRead(files, workspaceId, root, currentFolders, asked); @@ -494,6 +536,22 @@ export async function readFile( return version === undefined ? content : { ...content, version }; } + if (anywhere) { + const target = await nodeFs.stat(absolute).catch((error: NodeJS.ErrnoException) => { + if (error.code === "ENOENT" || error.code === "ENOTDIR") { + throw new RpcFailure(RpcErrorCode.NotFound, `${asked} does not exist`); + } + throw new RpcFailure(RpcErrorCode.Unavailable, "the file could not be read"); + }); + if (target.isDirectory()) throw folderReadError(asked); + if (!target.isFile()) + throw new RpcFailure(RpcErrorCode.InvalidParams, "a regular file is required"); + // Older filesystem agents cannot safely open an arbitrary path as a regular file. + throw new RpcFailure( + RpcErrorCode.Unavailable, + "the filesystem agent must be updated to read this file", + ); + } const legacy = await legacyPreview( fs, files, diff --git a/src/main/features/remote/index.ts b/src/main/features/remote/index.ts index 9a98e6a70..465f6dd72 100644 --- a/src/main/features/remote/index.ts +++ b/src/main/features/remote/index.ts @@ -12,6 +12,7 @@ import { MAX_TRANSCRIBE_BYTES } from "../../../../packages/remote-protocol/src/v import type { GitStatus } from "../../../shared/git/types"; import { ipcContract } from "../../../shared/ipc/contract"; import { type IpcResult, ipcErr, ipcOk } from "../../../shared/ipc/result"; +import { createLogger } from "../../../shared/log/main"; import { type AccountKeyState, deviceLabel } from "../../../shared/remote/account"; import type { RoomState } from "../../../shared/remote/room"; import type { RemoteAgentsPolicy } from "../../../shared/types/app-state"; @@ -26,6 +27,7 @@ import { AgentGate } from "./agent-gate"; import { ChatWatches } from "./chat-watches"; import { remoteConsents } from "./consents"; import { ServerError, SignedOutError, UnreachableError } from "./errors"; +import { FileWrites } from "./file-writes"; import { untrackedReader } from "./files"; import type { RemoteRepository } from "./git"; import { installIdAt } from "./install-id"; @@ -45,6 +47,8 @@ import { askTicket } from "./ticket"; import { VoiceSessions } from "./voice"; import { listedComputers, VoiceNames } from "./voice-names"; +const log = createLogger("remote-index"); + const c = ipcContract.remote.call; /** @@ -186,6 +190,8 @@ export async function registerRemoteChannel(options: { keys: AccountKeys; /** The app's theme changed: the other devices are told. */ themeChanged(): void; + /** Drops the removed workspace's cached Git facts. */ + forgetWorkspace(workspaceId: string): void; /** The user's other computers, for the nexus command's --device. */ cli: CliRemote; /** Reads a file of another computer's workspace, for the previews of quick look over its chats. */ @@ -200,6 +206,7 @@ export async function registerRemoteChannel(options: { ); // In the account's room while signed in with the account key; out of it otherwise. let room: RemoteRoom | null = null; + let disposed = false; let roomFor = ""; let roomState: RoomState = { status: "off", peers: [] }; /** The room, to ask another device through; Unavailable while out of it. */ @@ -265,7 +272,7 @@ export async function registerRemoteChannel(options: { broadcast("remote", "roomChanged", state); }; let keys: AccountKeys | null = null; - const server = remoteServer(process.env, app.isPackaged, (m) => console.warn(`[remote] ${m}`)); + const server = remoteServer(process.env, app.isPackaged, (message) => log.warn(message)); const account = new RemoteAccount({ server, proxy: remoteProxy(process.env, app.isPackaged, server), @@ -315,22 +322,23 @@ export async function registerRemoteChannel(options: { }); const sessionOf = (id: string) => (noteVoice.has(id) ? noteVoice : voice); - const relayUrl = async () => { - const ticket = await askTicket(account); + /** Gets a ticket for the current attempt and builds its WebSocket URL. */ + const relayUrl = async (signal: AbortSignal) => { + const ticket = await askTicket(account, undefined, signal); + signal.throwIfAborted(); const url = account.route.url("/relay", { socket: true }); url.search = new URLSearchParams({ v: String(RELAY_VERSION), ticket }).toString(); - relayVia = url.origin; return url.toString(); }; - /** Where the relay's socket went, and the socket: one that went straight goes back through the proxy. */ - let relayVia: string | null = null; let relay: RelayConnection | null = null; - returnToProxy({ + const proxyProbe = returnToProxy({ route: account.route, - via: () => (roomState.status === "online" ? relayVia : null), + via: () => relay?.onlineRoute() ?? null, reconnect: () => relay?.reconnect(), }); + /** Keeps the relay in the room for the signed-in account and current key. */ function syncRoom(): void { + if (disposed) return; const signedIn = account.state(); const key = keys?.key() ?? null; const want = signedIn.status === "signed-in" && key ? { signedIn, key } : null; @@ -340,11 +348,13 @@ export async function registerRemoteChannel(options: { if (room && id !== roomFor) { room.stop(); room = null; + relay = null; setRoomState({ status: "off", peers: [] }); } if (room || !want) return; const { device } = want.signedIn; roomFor = id; + const fileWrites = new FileWrites(); room = new RemoteRoom({ self: { id: device.id, name: device.name, platform: device.platform }, room: { kid: want.key.kid, key: roomKeyOf(want.key.key) }, @@ -387,10 +397,12 @@ export async function registerRemoteChannel(options: { : {}), ...(options.notes ? { notes: options.notes } : {}), agents: agentGate, + fileWrites, deviceName: (id) => room?.state().peers.find((p) => p.id === id)?.name, controls, }), onChange: setRoomState, + onDisconnect: (peer) => void fileWrites.drop(peer), onNotification: (from, method, params) => { if (method === "chat.update") { broadcast("remote", "peerChatUpdate", { @@ -916,6 +928,7 @@ export async function registerRemoteChannel(options: { account, keys: accountKeys, themeChanged: () => room?.announce(), + forgetWorkspace: (workspaceId) => taskGit?.forget(workspaceId), peerFiles: async (peer, absPath, offset, length, signal) => { try { return await peerFiles.openBytes(peer, absPath, offset, length, signal); @@ -963,10 +976,14 @@ export async function registerRemoteChannel(options: { }; }); }, - request: (deviceId, method, params, timeoutMs) => - inRoom().request(deviceId, method, params, timeoutMs), + request: (deviceId, method, params, timeoutMs, signal) => + inRoom().request(deviceId, method, params, timeoutMs, signal), }, dispose: () => { + if (disposed) return; + disposed = true; + proxyProbe.stop(); + pairing.stop(); account.cancelSignIn(); voice.cancel(); noteVoice.cancel(); diff --git a/src/main/features/remote/peer-file-reads.ts b/src/main/features/remote/peer-file-reads.ts index d82a5f74b..4c4c1c4dd 100644 --- a/src/main/features/remote/peer-file-reads.ts +++ b/src/main/features/remote/peer-file-reads.ts @@ -29,7 +29,9 @@ const MAX_FILE_BYTES = 8 * 1024 * 1024; const MAX_PENDING = 64; const MAX_WHOLE_READS = 4; const MAX_SPAN_READS = 32; -const FILE_VERSIONS = "0.13.4"; +export function supportsFileVersions(schema: string | undefined): boolean { + return speaks(schema, "0.13.4"); +} interface Entry { file: string; @@ -52,7 +54,7 @@ function changed(): RpcFailure { return new RpcFailure(RpcErrorCode.InvalidParams, "file version changed"); } -function bytesOf(result: Content, offset: number, length: number): Bytes { +export function bytesOf(result: Result, offset: number, length: number): Bytes { if ( result.kind !== "bytes" || result.offset !== offset || @@ -107,7 +109,7 @@ export class PeerFileReads { if ( !peer || scope.rejoins !== (peer.rejoins ?? 0) || - scope.versioned !== speaks(peer.schema, FILE_VERSIONS) || + scope.versioned !== supportsFileVersions(peer.schema) || scope.files !== peer.serves?.includes("files") ) { this.invalidateDevice(id); @@ -118,7 +120,7 @@ export class PeerFileReads { this.peers.set(id, { controller: new AbortController(), rejoins: peer.rejoins ?? 0, - versioned: speaks(peer.schema, FILE_VERSIONS), + versioned: supportsFileVersions(peer.schema), files: peer.serves?.includes("files"), }); } diff --git a/src/main/features/remote/relay-connection.ts b/src/main/features/remote/relay-connection.ts index 65aa11fc1..34f74e33f 100644 --- a/src/main/features/remote/relay-connection.ts +++ b/src/main/features/remote/relay-connection.ts @@ -68,8 +68,9 @@ export interface RelayConnectionDeps { /** * The socket's address, with a fresh ticket: ws(s)://server/relay?v=2&ticket=... * SignedOutError and OutdatedError give up; any other failure tries again. + * The signal is aborted if this attempt is replaced or stopped. */ - url(): Promise; + url(signal: AbortSignal): Promise; onOpen(): void; onFrame(frame: ServerFrame): void; onStatus(status: RelayStatus): void; @@ -85,14 +86,23 @@ export interface RelayConnectionDeps { heartbeat?: { every: number; wait: number }; } +/** A socket and the origin it reached, stable for the life of this connection. */ +interface ActiveSocket { + readonly socket: Socket; + readonly origin: string; +} + /** 1 s doubling to 30 s, give or take a fifth, so devices do not come back all at once. */ export function defaultBackoff(attempt: number): number { const base = Math.min(30_000, 1000 * 2 ** attempt); return Math.round(base * (0.8 + Math.random() * 0.4)); } +/** Owns the current relay attempt from ticket acquisition through socket close. */ export class RelayConnection { - private socket: Socket | null = null; + private connection: ActiveSocket | null = null; + /** The controller identifies the current attempt even before a socket exists. */ + private owner: AbortController | null = null; private timer: ReturnType | null = null; private attempt = 0; private running = false; @@ -130,15 +140,22 @@ export class RelayConnection { /** Sends a frame while online; false when it cannot go (offline, or too big). */ send(frame: ClientFrame): boolean { - if (this.status !== "online" || !this.socket) return false; + const socket = this.connection?.socket; + if (this.status !== "online" || !socket) return false; const text = JSON.stringify(frame); if (Buffer.byteLength(text, "utf8") > MAX_FRAME_BYTES) return false; - this.socket.send(text); + socket.send(text); return true; } get bufferedAmount(): number { - return this.socket?.bufferedAmount ?? 0; + return this.connection?.socket.bufferedAmount ?? 0; + } + + /** The route of the actual open socket, with identity stable across probes. */ + onlineRoute(): { readonly origin: string } | null { + const connection = this.connection; + return this.status === "online" && connection?.socket.readyState === 1 ? connection : null; } /** The room answered: the connection is good, and the next loss starts the backoff over. */ @@ -146,32 +163,49 @@ export class RelayConnection { this.attempt = 0; } + /** Gets a ticket and opens a socket only while this attempt owns the relay. */ private async connect(): Promise { + if (!this.running) return; + const owner = new AbortController(); + this.owner = owner; this.set("connecting"); + if (!this.owns(owner)) return; + let url: string; + let connection: ActiveSocket; try { - url = await this.deps.url(); + url = await this.deps.url(owner.signal); + if (!this.owns(owner)) return; + const origin = new URL(url).origin; + const socket = (this.deps.socket ?? ((address) => new WebSocket(address) as Socket))(url); + if (!this.owns(owner)) { + if (socket.readyState <= 1) socket.close(1000, "bye"); + return; + } + connection = { origin, socket }; } catch (error) { + if (!this.owns(owner)) return; if (error instanceof SignedOutError) { this.giveUp("stopped"); this.deps.onSignedOut(); } else if (error instanceof OutdatedError) this.giveUp("outdated"); - else this.retry(); + else this.retry(owner); return; } - if (!this.running) return; - const socket = (this.deps.socket ?? ((u) => new WebSocket(u) as Socket))(url); - this.socket = socket; + + const { socket } = connection; + this.connection = connection; let opened = false; socket.addEventListener("open", () => { - if (this.socket !== socket) return; + if (this.connection !== connection) return; opened = true; this.set("online"); - this.startHeartbeat(socket); + if (this.connection !== connection) return; + this.startHeartbeat(connection, owner); this.deps.onOpen(); }); socket.addEventListener("message", (event: { data: unknown }) => { - if (this.socket !== socket) return; + if (this.connection !== connection) return; // Anything from the relay answers the heartbeat. this.clearPongTimer(); if (typeof event.data !== "string" || event.data === RELAY_PONG) return; @@ -185,8 +219,8 @@ export class RelayConnection { if (parsed.success) this.deps.onFrame(parsed.data); }); socket.addEventListener("close", (event: { code: number }) => { - if (this.socket !== socket) return; - this.socket = null; + if (this.connection !== connection) return; + this.connection = null; this.stopHeartbeat(); if (event.code === CLOSE_REPLACED) this.giveUp("replaced"); else if (event.code === CLOSE_SIGNED_OUT) { @@ -196,7 +230,7 @@ export class RelayConnection { void this.connect(); } else { if (!opened) this.deps.unreachable?.(url); - this.retry(); + this.retry(owner); } }); // An error is followed by a close, which decides what comes next. @@ -208,14 +242,15 @@ export class RelayConnection { * not answered in time lets the socket go and joins the room again, as a * socket that closed does. Its own close may come long after, or never. */ - private startHeartbeat(socket: Socket): void { + private startHeartbeat(connection: ActiveSocket, owner: AbortController): void { + const { socket } = connection; this.stopHeartbeat(); const { every, wait } = this.deps.heartbeat ?? { every: HEARTBEAT_EVERY_MS, wait: PONG_WAIT_MS, }; this.beat = setInterval(() => { - if (this.socket !== socket || this.pongTimer) return; + if (this.connection !== connection || this.pongTimer) return; try { socket.send(RELAY_PING); } catch { @@ -223,8 +258,8 @@ export class RelayConnection { } this.pongTimer = setTimeout(() => { this.pongTimer = null; - if (this.socket !== socket) return; - this.socket = null; + if (this.connection !== connection) return; + this.connection = null; this.stopHeartbeat(); try { socket.close(); @@ -232,7 +267,7 @@ export class RelayConnection { // Closing already. } if (this.quickRetry()) void this.connect(); - else this.retry(); + else this.retry(owner); }, wait); }, every); } @@ -257,15 +292,17 @@ export class RelayConnection { return true; } - private retry(): void { - if (!this.running) return; + /** Schedules backoff only for the attempt that reported the loss. */ + private retry(owner: AbortController): void { + if (!this.owns(owner)) return; this.set("waiting"); + if (!this.owns(owner)) return; const delay = (this.deps.backoff ?? defaultBackoff)(this.attempt); this.attempt += 1; this.clearTimer(); this.timer = setTimeout(() => { this.timer = null; - if (this.running) void this.connect(); + if (this.owns(owner)) void this.connect(); }, delay); } @@ -276,13 +313,22 @@ export class RelayConnection { this.set(status); } + /** Invalidates the attempt and detaches its socket before closing it. */ private drop(): void { - const socket = this.socket; - this.socket = null; + const owner = this.owner; + const socket = this.connection?.socket; + this.owner = null; + this.connection = null; this.stopHeartbeat(); + owner?.abort(); if (socket && socket.readyState <= 1) socket.close(1000, "bye"); } + /** Whether pending work still belongs to the active relay attempt. */ + private owns(owner: AbortController): boolean { + return this.running && this.owner === owner; + } + private clearTimer(): void { if (this.timer) clearTimeout(this.timer); this.timer = null; diff --git a/src/main/features/remote/room.ts b/src/main/features/remote/room.ts index 6dbc41bed..5054a2176 100644 --- a/src/main/features/remote/room.ts +++ b/src/main/features/remote/room.ts @@ -5,6 +5,7 @@ * It takes only what was sealed for it, now, and each message once: the * epochs and seqs of packages/remote-protocol's ./sealed.ts. */ + import { randomBytes } from "node:crypto"; import { posix, win32 } from "node:path"; import { @@ -32,10 +33,14 @@ import { type SealedMessage, type ServerFrame, } from "../../../../packages/remote-protocol/src"; +import { createLogger } from "../../../shared/log/main"; import type { RoomPeer, RoomState, RoomStatus } from "../../../shared/remote/room"; +import { isFileTransferMethod } from "./file-io"; import type { RelayConnectionDeps, RelayStatus } from "./relay-connection"; import { openMessage, type RoomKey, sealMessage } from "./seal"; +const log = createLogger("remote-room"); + /** How long an answer may take. */ export const REQUEST_TIMEOUT_MS = 15_000; @@ -102,19 +107,17 @@ class FileReadBudget { } } -const imageReadBudget = new FileReadBudget(2, 16); -const fileReadBudget = new FileReadBudget(8, 32); const FILE_READ_SOCKET_HIGH_WATER = 4 * MAX_FRAME_BYTES; -function readBudget(params: unknown): FileReadBudget { +function readsWholeImage(params: unknown): boolean { const { as, path } = (params ?? {}) as { as?: unknown; path?: unknown }; const image = /\.(png|jpe?g|gif|webp)$/i; - return as !== "bytes" && + return ( + as !== "bytes" && typeof path === "string" && (image.test(posix.normalize(path).replace(/\/+$/, "")) || image.test(win32.normalize(path).replace(/\\+$/, ""))) - ? imageReadBudget - : fileReadBudget; + ); } /** A request that failed, with its JSON-RPC error. */ @@ -150,8 +153,15 @@ export interface RemoteRoomDeps { handlers: Pick, ): RoomConnection; /** Answers another device's request; a RpcFailure for an error it should see. */ - serve(request: { from: string; method: RpcMethod; params: unknown }): Promise; + serve(request: { + from: string; + method: RpcMethod; + params: unknown; + isCurrent?: () => boolean; + }): Promise; onChange(state: RoomState): void; + /** Drop device-owned work when a peer disconnects, or all work when this device does. */ + onDisconnect?(peer?: string): void; onNotification?(from: string, method: string, params: unknown): void; /** * A message sealed with a newer version of the account key came: it may @@ -176,6 +186,8 @@ interface Pending { export class RemoteRoom { private readonly connection: RoomConnection; + private readonly imageReadBudget = new FileReadBudget(2, 16); + private readonly fileReadBudget = new FileReadBudget(8, 32); private status: RoomStatus = "off"; private peers = new Map(); private seq = 0; @@ -217,6 +229,7 @@ export class RemoteRoom { stop(): void { this.connection.stop(); this.failPending("left the room"); + this.deps.onDisconnect?.(); this.status = "off"; this.cancelQueuedReads(); this.changed(); @@ -232,6 +245,7 @@ export class RemoteRoom { /** Starts a new epoch: nothing sealed for the last is taken, so what it asked in it fails now. */ private renew(why: string): void { + this.deps.onDisconnect?.(); this.epoch = newEpoch(); this.heard.clear(); this.past.clear(); @@ -402,6 +416,7 @@ export class RemoteRoom { private onStatus(status: RelayStatus): void { this.status = status; if (status !== "online") { + this.deps.onDisconnect?.(); // Out of the room: none of the others is reachable until it is back. for (const peer of this.peers.values()) peer.online = false; this.cancelQueuedReads(); @@ -428,6 +443,9 @@ export class RemoteRoom { ...(known?.rejoins ? { rejoins: known.rejoins } : {}), }); } + for (const peer of this.peers.values()) { + if (peer.online && !next.get(peer.id)?.online) this.deps.onDisconnect?.(peer.id); + } this.peers = next; this.cancelQueuedReads(); if (!this.greeted) { @@ -440,7 +458,7 @@ export class RemoteRoom { if (frame.t === "message") { this.inbox = this.inbox .then(() => this.take(frame)) - .catch((error) => console.warn("[remote] taking a message failed:", error)); + .catch((error) => log.warn(`Could not process a relay message: ${String(error)}`)); return; } if (frame.t === "account") { @@ -455,7 +473,7 @@ export class RemoteRoom { p.reject(new RpcFailure(RpcErrorCode.Unavailable, "that device is not in the room")); } } else if (frame.code !== "unauthorized") { - console.warn(`[remote] relay: ${frame.code} ${frame.message}`); + log.warn(`relay: ${frame.code} ${frame.message}`); } } @@ -522,6 +540,7 @@ export class RemoteRoom { * there, which is asked for again as its `rejoins` grows. */ private rejoined(from: string): void { + this.deps.onDisconnect?.(from); this.cancelQueuedReads(); const peer = this.peers.get(from); if (!peer) return; @@ -556,12 +575,12 @@ export class RemoteRoom { return; } if ("method" in message && "id" in message) { + const current = () => + this.status === "online" && + this.heard.get(from)?.epoch === epoch && + this.peers.get(from)?.online === true; if (message.method === "files.read") { - const budget = readBudget(message.params); - const current = () => - this.status === "online" && - this.heard.get(from)?.epoch === epoch && - this.peers.get(from)?.online === true; + const budget = readsWholeImage(message.params) ? this.imageReadBudget : this.fileReadBudget; if (!current()) return; if (!(await budget.acquire(current))) { // Every refused request is told, each with its own small reply: left @@ -584,14 +603,21 @@ export class RemoteRoom { try { await this.waitForFileReadSocket(current); if (!current()) return; - const answer = await this.answer(from, message.method, message.params); + const answer = await this.answer(from, message.method, message.params, current); if (current()) await this.reply(from, epoch, message, answer, current); } finally { budget.release(); } return; } - const answer = await this.answer(from, message.method, message.params); + const transfer = isFileTransferMethod(message.method); + if (transfer && !current()) return; + const answer = await this.answer( + from, + message.method, + message.params, + transfer ? current : undefined, + ); void this.reply(from, epoch, message, answer); return; } @@ -652,14 +678,15 @@ export class RemoteRoom { } private cancelQueuedReads(): void { - imageReadBudget.cancelWaiting(); - fileReadBudget.cancelWaiting(); + this.imageReadBudget.cancelWaiting(); + this.fileReadBudget.cancelWaiting(); } private async answer( from: string, method: string, params: unknown, + isCurrent?: () => boolean, ): Promise<{ result: unknown } | { error: RpcError }> { if (!isRpcMethod(method)) { return { error: { code: RpcErrorCode.MethodNotFound, message: `no method ${method}` } }; @@ -689,7 +716,24 @@ export class RemoteRoom { } // An agent may do only what people may do from other devices here, and only when agents may. const serves = this.serves(); - if (!serves.includes(spec.capability as Capability)) { + const anywhere = parsed.success && (parsed.data as { anywhere?: true }).anywhere === true; + const capability = + anywhere && (method === "files.write.begin" || method === "files.mkdir") + ? "terminal.control" + : spec.capability; + if ( + anywhere && + (method === "files.list" || method === "files.read") && + !serves.includes("terminal.control") + ) { + return { + error: { + code: RpcErrorCode.Forbidden, + message: "file access outside the workspace requires terminal control", + }, + }; + } + if (!serves.includes(capability as Capability)) { return { error: { code: RpcErrorCode.Forbidden, message: `${method} is not served here` } }; } if (byAgent && !serves.includes("agents")) { @@ -704,7 +748,14 @@ export class RemoteRoom { return { error: { code: RpcErrorCode.InvalidParams, message: parsed.error.message } }; } try { - return { result: await this.deps.serve({ from, method, params: parsed.data }) }; + return { + result: await this.deps.serve({ + from, + method, + params: parsed.data, + ...(isCurrent ? { isCurrent } : {}), + }), + }; } catch (error) { if (error instanceof RpcFailure) { return { @@ -715,7 +766,9 @@ export class RemoteRoom { }, }; } - console.warn(`[remote] serving ${method} failed:`, error); + log.warn( + `Serving ${method} failed: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`, + ); return { error: { code: RpcErrorCode.InternalError, message: "the device could not answer" }, }; diff --git a/src/main/features/remote/route.ts b/src/main/features/remote/route.ts index 6499e8cf9..29cc141c9 100644 --- a/src/main/features/remote/route.ts +++ b/src/main/features/remote/route.ts @@ -6,6 +6,10 @@ * PROXY_DOWN_MS, then try it again. */ +import { createLogger } from "../../../shared/log/main"; + +const log = createLogger("remote-route"); + /** How long requests go straight to the server once the proxy could not be reached. */ export const PROXY_DOWN_MS = 5 * 60_000; @@ -46,7 +50,7 @@ export class ServerRoute { unreachable(address: string): boolean { if (this.proxy === null || !sameHost(address, this.proxy)) return false; if (this.now() >= this.downUntil) { - console.warn("[remote] the proxy could not be reached; going to the server straight"); + log.warn("The proxy could not be reached, using the direct server route."); } this.downUntil = this.now() + PROXY_DOWN_MS; return true; @@ -54,44 +58,60 @@ export class ServerRoute { } /** - * Brings a socket that went straight back through the proxy, as the relay's - * may stay for days on a desktop left on: once requests go through the - * proxy again while `via()` (the address the socket is open to, null while - * none is) is the server's, the proxy's /info is asked, and when it - * answers the socket is opened anew (`reconnect`), through it. When it - * does not, the proxy is passed by for another while. `check` looks now; - * the returned function stops the looking every RETURN_CHECK_MS. + * Checks whether a socket that went straight can return through the proxy. + * The result belongs to the open socket and route that initiated the probe. */ export function returnToProxy(options: { route: ServerRoute; - via: () => string | null; + via: () => { readonly origin: string } | null; reconnect: () => void; fetch?: typeof fetch; }): { check(): Promise; stop(): void } { const { route } = options; const fetchFn = options.fetch ?? fetch; + const lifetime = new AbortController(); let asking = false; - const check = async () => { + + /** Probes the proxy while the current socket is open to the server directly. */ + async function check(): Promise { const proxy = route.proxy; - const via = options.via(); - if (proxy === null || via === null || sameHost(via, proxy) || route.base() !== proxy) return; - if (asking) return; + if (lifetime.signal.aborted || asking || proxy === null || route.base() !== proxy) return; + const connection = options.via(); + if ( + connection === null || + sameHost(connection.origin, proxy) || + !sameHost(connection.origin, route.server) + ) { + return; + } + asking = true; + let reachable: boolean; try { const res = await fetchFn(new URL("/info", proxy), { - signal: AbortSignal.timeout(RETURN_PROBE_TIMEOUT_MS), + signal: AbortSignal.any([lifetime.signal, AbortSignal.timeout(RETURN_PROBE_TIMEOUT_MS)]), }); - if (res.ok) options.reconnect(); - else route.unreachable(proxy); + reachable = res.ok; } catch { - route.unreachable(proxy); + reachable = false; } finally { asking = false; } - }; + + if (lifetime.signal.aborted || options.via() !== connection || route.base() !== proxy) return; + if (reachable) options.reconnect(); + else route.unreachable(proxy); + } + const timer = setInterval(() => void check(), RETURN_CHECK_MS); timer.unref?.(); - return { check, stop: () => clearInterval(timer) }; + return { + check, + stop() { + clearInterval(timer); + lifetime.abort(); + }, + }; } /** Whether two addresses are of one host, a websocket's (wss:) and its https: alike. */ diff --git a/src/main/features/remote/search.ts b/src/main/features/remote/search.ts index 606ea3f0f..a0c68ead5 100644 --- a/src/main/features/remote/search.ts +++ b/src/main/features/remote/search.ts @@ -5,27 +5,19 @@ * workspace is searched where its files are. Paths are relative to the * workspace's folder, with "/" between their parts on every system. */ -import { randomUUID } from "node:crypto"; import { RpcErrorCode, type RpcParams, type RpcResult, } from "../../../../packages/remote-protocol/src"; -import { - AgentSearchCompleteSchema, - AgentSearchFilesResultSchema, - AgentSearchProgressPayloadSchema, - SEARCH_CANCEL_METHOD, - SEARCH_FILES_METHOD, - SEARCH_PROGRESS_EVENT, - SEARCH_TEXT_METHOD, -} from "../../../shared/search/protocol"; +import { AgentSearchFilesResultSchema, SEARCH_FILES_METHOD } from "../../../shared/search/protocol"; import { type FileMatch, TextSearchQuerySchema } from "../../../shared/search/types"; import { type AgentBackedProvider, type FsProvider, isAgentBackedProvider, } from "../fs/bridge/provider"; +import { createAgentSearchSession } from "../search/session"; import { fitsInFrame, RpcFailure } from "./room"; /** How many files a find gives when it does not say. */ @@ -106,37 +98,26 @@ export async function searchFiles( // One more than asked: whether there are more is then known. maxResults: limit + 1, }); - const searchId = randomUUID(); const found: FileMatch[] = []; let matches = 0; let enough: () => void = () => {}; const enoughFound = new Promise<"enough">((resolve) => { enough = () => resolve("enough"); }); - const unsubscribe = agent.onAgentEvent(SEARCH_PROGRESS_EVENT, (payload) => { - const parsed = AgentSearchProgressPayloadSchema.safeParse(payload); - if (!parsed.success || parsed.data.searchId !== searchId) return; - for (const file of parsed.data.batch) { + const session = createAgentSearchSession(agent, (batch) => { + for (const file of batch) { found.push(file); matches += file.matches.length; } if (matches > limit) enough(); }); - // True once the agent answered; a search left otherwise is still running there. - let answered = false; - const complete = agent - .callAgentMethod(SEARCH_TEXT_METHOD, { searchId, query }, { timeoutMs: timeMs + 5_000 }) - .then((result) => { - answered = true; - AgentSearchCompleteSchema.parse(result); - return "done" as const; - }); + const complete = session.start(query, { timeoutMs: timeMs + 5_000 }); complete.catch(() => {}); let timer: ReturnType | undefined; const outOfTime = new Promise<"time">((resolve) => { timer = setTimeout(() => resolve("time"), timeMs); }); - let ended: "done" | "enough" | "time"; + let ended: Awaited | "enough" | "time"; try { ended = await Promise.race([complete, enoughFound, outOfTime]); } catch (error) { @@ -146,8 +127,7 @@ export async function searchFiles( unavailable(error); } finally { clearTimeout(timer); - unsubscribe(); - if (!answered) void agent.callAgentMethod(SEARCH_CANCEL_METHOD, { searchId }).catch(() => {}); + session.dispose(); } // The first `limit` matches, in the order the agent found them. diff --git a/src/main/features/remote/serve.ts b/src/main/features/remote/serve.ts index 1a3d2ca44..3b0be585e 100644 --- a/src/main/features/remote/serve.ts +++ b/src/main/features/remote/serve.ts @@ -4,50 +4,72 @@ * session list` would. */ -import { posix, win32 } from "node:path"; -import { isDeepStrictEqual } from "node:util"; import { type AgentSender, - type Capability, - type ChatPage, - type ChatRun, - MAX_AGENT_DESCRIPTION, - MAX_CHAT_RUNS, - MAX_FILE_SUGGESTIONS, - MAX_RUN_TEXT, - MAX_SESSION_AGENTS, - type RemoteSession, - type RemoteSessionAgent, - type RemoteWorkspace, RpcErrorCode, - type RpcMethod, type RpcParams, - type RpcResult, - type SessionState, - type ViewToolCall, } from "../../../../packages/remote-protocol/src"; import type { RelaySender } from "../../../shared/chat/relay"; -import type { AgentRun } from "../../../shared/claude/activity"; -import type { CliChatChoices, CliChatState } from "../../../shared/cli/ui"; -import { describeTerminal, isAgentSession } from "../../../shared/sessions/agent-state"; -import { CHAT_MODES, chatModeNamed } from "../../../shared/sessions/chat-modes"; -import type { ChatAgent } from "../../../shared/sessions/task-agent"; -import type { WorkspaceMeta } from "../../../shared/types/workspace"; -import { compareWorkspaceOrder } from "../../../shared/workspace-order"; -import { tryGetMainT } from "../../i18n"; import type { CliServices } from "../cli/context"; -import type { FsProvider } from "../fs/bridge/provider"; -import { type AgentGate, type AgentRequest, relaySenderOf } from "./agent-gate"; +import { type AgentRequest, relaySenderOf } from "./agent-gate"; import { Attachments } from "./attachments"; -import type { ChatWatches } from "./chat-watches"; -import { answerConsent, type ConsentControls, remoteConsents } from "./consents"; -import { folderIn, listFiles, type RemoteFiles, readFile } from "./files"; -import { type FolderSystem, folderToAdd, listFolders, SYSTEM_FOLDERS } from "./folders"; -import { gitDiff, gitLog, gitShow, gitStatus, type RemoteGit } from "./git"; -import { attachToNote, listNotes, PhotoQuota, type RemoteNotes } from "./notes"; -import { fitsInFrame, MAX_MESSAGE_BYTES, RpcFailure } from "./room"; -import { findFiles, searchFiles } from "./search"; -import type { SessionWatches } from "./session-watches"; +import { answerConsent } from "./consents"; +import { isFileTransferMethod } from "./file-io"; +import { FileWrites } from "./file-writes"; +import { listFolders } from "./folders"; +import { gitDiff, gitLog, gitShow, gitStatus } from "./git"; +import { attachToNote, listNotes, PhotoQuota } from "./notes"; +import { RpcFailure } from "./room"; +import { + answerChat, + attachToChat, + backgroundChatTool, + beginUpload, + changeChatProfile, + chatCommands, + chatFileSuggestions, + chatImage, + chatSettings, + checkChatSignIn, + closeChat, + configureChat, + editHeldChat, + forkChat, + hurryChat, + interruptChat, + listProfiles, + newChat, + renameChat, + restartChat, + resumeChat, + rewindChat, + runChatCommand, + sendChat, + setChatRemoteControl, + stopChatRun, + stopChatTask, + unqueueChat, + withdrawChat, + workChatConfig, + workChatMcp, +} from "./serving/chat"; +import { + chatOriginal, + chatPanel, + chatRun, + chatRuns, + chatTool, + listHistory, + previewChatRewind, + readChat, + readEvents, +} from "./serving/chat-details"; +import { workspaceOf } from "./serving/common"; +import { serveFileDestination, serveFileRead, serveFileSearch } from "./serving/files"; +import { serveSessionsList } from "./serving/sessions"; +import { readTerminal } from "./serving/terminal"; +import type { RemoteServeOptions, RemoteServeRequest } from "./serving/types"; +import { createWorkspace } from "./serving/workspaces"; import { answerTerminal, closeTerminal, @@ -56,42 +78,70 @@ import { renameTerminal, terminalShells, } from "./terminal-control"; -import type { TerminalWatches } from "./terminal-watches"; import { Uploads } from "./uploads"; import { waitOnChat, waitOnTerminalFor } from "./waits"; -/** What this version serves; a hello says so, and the others ask for nothing more. */ -export const SERVES: readonly Capability[] = [ - "sessions", - "chat.read", - "chat.control", - "terminal.read", - "terminal.control", - "files", - "git", - "notes", - "workspace.create", - "agents", -]; - -/** - * What this device serves as the user set it: its chats' controls, and - * its terminals', each only when the user lets other devices have them - * (Settings › Remote), else they may only look; agents' requests unless - * the user turned them down. - */ -export function servesWith( - allowControl: boolean, - allowTerminal = false, - allowAgents = false, -): readonly Capability[] { - return SERVES.filter( - (c) => - ((c !== "chat.control" && c !== "workspace.create") || allowControl) && - (c !== "terminal.control" || allowTerminal) && - (c !== "agents" || allowAgents), - ); -} +export { + answerChat, + attachToChat, + backgroundChatTool, + beginUpload, + changeChatProfile, + chatCommands, + chatFileSuggestions, + chatImage, + chatSettings, + checkChatSignIn, + closeChat, + configureChat, + editHeldChat, + forkChat, + hurryChat, + interruptChat, + listProfiles, + newChat, + renameChat, + restartChat, + resumeChat, + rewindChat, + runChatCommand, + sendChat, + setChatRemoteControl, + settingsOf, + stopChatRun, + stopChatTask, + unqueueChat, + withdrawChat, + workChatConfig, + workChatMcp, +} from "./serving/chat"; +export { + chatOriginal, + chatPanel, + chatRun, + chatRuns, + chatTool, + DEFAULT_READ_ITEMS, + fitOversizeCodexChildren, + fitPage, + listHistory, + previewChatRewind, + readChat, + readEvents, + runDetailOf, + runRowOf, +} from "./serving/chat-details"; +export { + fitsAsResult, + SERVES, + servesWith, + tabOf, + unavailable, + workspaceOf, +} from "./serving/common"; +export { listSessions, sessionAgents } from "./serving/sessions"; +export { DEFAULT_TERMINAL_LINES, readTerminal } from "./serving/terminal"; +export { createWorkspace } from "./serving/workspaces"; /** What an agent reads (`from`, since 0.12.0): taken where agents are, with no prompt. */ const AGENT_READS: ReadonlySet = new Set([ @@ -103,1282 +153,16 @@ const AGENT_READS: ReadonlySet = new Set([ "events.read", ]); -/** How many lines of a terminal a read gives when it does not say. */ -export const DEFAULT_TERMINAL_LINES = 1000; - -/** A chat's state as the agent lists show it. */ -const CHAT_STATES: Record = { - working: "running", - asking: "needs-you", - idle: "idle", - error: "idle", -}; - -/** - * A terminal agent's state: Claude Code's from its hooks (idle, running, - * completed, needsInput, permissionPending, error), or another agent's - * from the terminal's activity (busy, completed, quiet). - */ -function terminalSessionState(state: string): SessionState { - if (state === "needsInput" || state === "permissionPending") return "needs-you"; - if (state === "running" || state === "busy") return "running"; - if (state === "completed") return "new-result"; - return "idle"; -} - -/** - * The workspaces as the sidebar lists them: roots, pinned first and each - * group in its own order, each followed by its task worktrees. - */ -function ordered(workspaces: readonly WorkspaceMeta[]): WorkspaceMeta[] { - const roots = workspaces.filter((w) => !w.task).sort(compareWorkspaceOrder); - const out: WorkspaceMeta[] = []; - for (const root of roots) - out.push(root, ...workspaces.filter((w) => w.task?.parentId === root.id)); - for (const w of workspaces) if (!out.includes(w)) out.push(w); - return out; -} - -function oneLine(text: string, max = 200): string { - const line = text.replace(/\s+/g, " ").trim(); - return line.length > max ? `${line.slice(0, max - 1)}…` : line; -} - -/** - * The agents a session runs that still run, as the agent list shows them - * under its row: what each was asked to do on a line (a run told nothing - * more, its prompt's start); none when there are none. - */ -export function sessionAgents(agents: readonly AgentRun[]): RemoteSessionAgent[] | undefined { - const live = agents.filter((agent) => agent.end === undefined).slice(0, MAX_SESSION_AGENTS); - if (live.length === 0) return undefined; - return live.map((agent) => { - const said = agent.description ?? (agent.kind === "print" ? agent.prompt : undefined); - const description = said ? oneLine(said, MAX_AGENT_DESCRIPTION) : ""; - return { - id: agent.id, - kind: agent.kind, - ...(agent.program ? { program: agent.program } : {}), - ...(agent.type ? { type: oneLine(agent.type, 60) } : {}), - ...(description ? { description } : {}), - state: agent.state, - start: agent.start, - }; - }); -} - -function agentsField( - services: CliServices, - workspaceId: string, - tabId: string, -): { agents?: RemoteSessionAgent[] } { - const agents = sessionAgents(services.claudeAgents.list(workspaceId, tabId)); - return agents ? { agents } : {}; -} - -/** A task's git facts as last looked at; none for a workspace that is no task, or not looked at yet. */ -export type WorkspaceGit = (workspace: WorkspaceMeta) => RemoteWorkspace["git"] | undefined; - -/** - * The workspaces and their sessions; a task's git facts from `git`, which - * looks at them now and then rather than now. - */ -export async function listSessions( - services: CliServices, - git?: WorkspaceGit, -): Promise> { - const workspaces = ordered(services.listWorkspaces()); - const sessions: RemoteSession[] = []; - const now = Date.now(); - for (const workspace of workspaces) { - let tabs: Awaited>>["tabs"]; - try { - tabs = (await services.ui("tabs.list", { workspaceId: workspace.id })).tabs; - } catch { - // A workspace the renderer does not hold (not opened yet): nothing to list. - continue; - } - for (const tab of tabs) { - if (tab.type === "claude" || tab.type === "codex") { - const info = await services - .ui("chat.state", { workspaceId: workspace.id, tabId: tab.id, peek: true }) - .catch(() => null); - if (!info) continue; - sessions.push({ - workspaceId: workspace.id, - tabId: tab.id, - kind: tab.type, - title: tab.title, - state: CHAT_STATES[info.state], - ...(info.since !== undefined ? { since: info.since } : {}), - ...(info.message ? { last: oneLine(info.message) } : {}), - ...(info.profile ? { profile: info.profile } : {}), - ...(info.cwd ? { cwd: info.cwd } : {}), - ...(info.state === "asking" && info.waiting - ? { waiting: { kind: info.waiting.kind, text: oneLine(info.waiting.text) } } - : {}), - ...agentsField(services, workspace.id, tab.id), - }); - } else if (tab.type === "terminal" && !tab.unstarted) { - // One not started has nothing to show another device until its workspace is opened here. - const claude = services.claudeStatus(workspace.id, tab.id); - const state = describeTerminal( - claude, - services.activity(workspace.id, tab.id), - tab.title, - now, - ); - // A Claude Code there asking to run a tool: what it asks, as the app holds it. - const ask = - claude?.status === "permissionPending" - ? services.claudeAsks(workspace.id, tab.id)[0] - : undefined; - sessions.push({ - workspaceId: workspace.id, - tabId: tab.id, - kind: "terminal", - title: tab.title, - state: isAgentSession(state) ? terminalSessionState(state.state) : "idle", - ...(isAgentSession(state) ? { agent: state.agent ?? "agent" } : {}), - ...(state.since !== undefined ? { since: state.since } : {}), - ...(state.message ? { last: oneLine(state.message) } : {}), - ...(ask - ? { - waiting: { - kind: "permission" as const, - text: oneLine(ask.target ? `${ask.tool} ${ask.target}` : ask.tool), - requestId: ask.id, - always: ask.always.length > 0, - subagent: ask.agentId !== undefined, - }, - } - : {}), - ...agentsField(services, workspace.id, tab.id), - }); - } - } - } - const listed: RemoteWorkspace[] = workspaces.map((w) => { - const facts = w.task ? git?.(w) : undefined; - return { - id: w.id, - name: w.name, - path: w.rootPath, - ...(w.task ? { parentId: w.task.parentId } : {}), - ...(!w.task && w.pinned ? { pinned: true } : {}), - ...(facts ? { git: facts } : {}), - }; - }); - return { workspaces: listed, sessions }; -} - -/** The tab, when the workspace holds one of that kind; else NotFound. */ -export async function tabOf( - services: CliServices, - workspaceId: string, - tabId: string, - kinds: readonly string[], -): Promise<{ id: string; type: string; title: string }> { - if (!workspaceOf(services, workspaceId)) { - throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); - } - const { tabs } = await services - .ui("tabs.list", { workspaceId }) - .catch(() => ({ tabs: [] as { id: string; type: string; title: string }[] })); - const tab = tabs.find((t) => t.id === tabId); - if (!tab || !kinds.includes(tab.type)) { - throw new RpcFailure(RpcErrorCode.NotFound, `no such ${kinds.join(" or ")} tab`); - } - return tab; -} - -/** A workspace the sessions' list shows. */ -export function workspaceOf(services: CliServices, workspaceId: string): WorkspaceMeta | undefined { - return services.listWorkspaces().find((w) => w.id === workspaceId); -} - -/** - * The app failing or refusing to do it as the session stands (nothing to - * stop, a request answered since), as another device is told it. - */ -export function unavailable(error: unknown): never { - throw new RpcFailure( - RpcErrorCode.Unavailable, - error instanceof Error ? error.message : String(error), - ); -} - -/** How many items a chat read gives when it does not say (the renderer's default too). */ -export const DEFAULT_READ_ITEMS = 200; - -/** Room left in a frame for the answer's envelope around a result. */ -const ENVELOPE_BYTES = 1024; - -export function fitsAsResult(result: unknown): boolean { - return fitsInFrame({ result, pad: "x".repeat(ENVELOPE_BYTES) }); -} - -/** - * A page of the chat, of up to `limit` items: fewer when that many would - * not go in one frame (long tool results, a long reply), down to one item. - */ -export async function readChat( - services: CliServices, - { workspaceId, tabId, before, limit }: RpcParams<"chat.read">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - let count = limit ?? DEFAULT_READ_ITEMS; - for (;;) { - const page = await services - .ui("chat.page", { - workspaceId, - tabId, - ...(before !== undefined ? { before } : {}), - limit: count, - }) - .catch(unavailable); - if (fitsAsResult(page)) return page; - const fittedChildren = fitOversizeCodexChildren(page); - if (fittedChildren) return fittedChildren; - if (page.items.length <= 1) { - const fitted = fitPage(page); - if (fitted) return fitted; - throw new RpcFailure(RpcErrorCode.Unavailable, "that part of the chat is too large to send"); - } - count = Math.max(1, Math.floor(page.items.length / 2)); - } -} - -/** - * Native Codex children appear on the latest page regardless of its parent - * item count. If their messages alone exceed the frame, the existing - * subagent fallback can keep the parent page before it is narrowed. - */ -export function fitOversizeCodexChildren

(page: P): P | null { - if ( - page.status?.agent !== "codex" || - page.start + page.items.length !== page.total || - fitsAsResult({ agentItems: page.agentItems }) - ) { - return null; - } - const parent = withoutSubagentItems(page); - return fitsAsResult(parent) ? parent : null; -} - -function withoutSubagentItems

(page: P): P { - const tools = Object.fromEntries( - Object.entries(page.tools).filter(([, tool]) => tool.parentToolUseId === null), - ); - return { ...page, tools, agentItems: {} }; -} - -/** The most of each tool's result a page keeps, in turn, when one item is too large to send. */ -const FITTED_RESULT_CHARS = [8 * 1024, 1024, 0]; - -/** - * A page of one item too large for a frame as it is (a reply whose calls gave long results, a - * subagent's whole run), made to go in one: its tools' results cut shorter, each marked - * `truncated` (the other device reads a call whole by chat.tool), and at last its subagents' - * messages and calls left out. Refused, the item would stop the chat being read further back - * there at all. Null when even that is too large. - */ -export function fitPage

(page: P): P | null { - for (const most of FITTED_RESULT_CHARS) { - const cut = { ...page, tools: cutResults(page.tools, most) }; - if (fitsAsResult(cut)) return cut; - } - const parent = withoutSubagentItems(page); - const bare = { ...parent, tools: cutResults(parent.tools, 0) }; - return fitsAsResult(bare) ? bare : null; -} - -/** Tools with at most `most` characters of each result, and no structured result where cut. */ -function cutResults( - tools: Record, - most: number, -): Record { - const cut: Record = {}; - for (const [id, tool] of Object.entries(tools)) { - const result = tool.result; - if (!result || (result.text.length <= most && result.structured === undefined)) { - cut[id] = tool; - continue; - } - const { structured: _left, ...rest } = result; - let text = result.text.slice(0, most); - // A character of two halves is not split. - if (/[\uD800-\uDBFF]$/.test(text)) text = text.slice(0, -1); - cut[id] = { ...tool, result: { ...rest, text, truncated: true } }; - } - return cut; -} - -export async function readTerminal( - services: CliServices, - { workspaceId, tabId, lines, ansi }: RpcParams<"terminal.read">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["terminal"]); - const { text, caret } = await services - .ui("terminal.read", { - workspaceId, - tabId, - lines: lines ?? DEFAULT_TERMINAL_LINES, - ...(ansi ? { ansi } : {}), - }) - .catch(unavailable); - // The end of it, as much as goes in a frame, from a line's start: no escape cut in two. The - // cursor goes up with the lines cut; above them, it is not told. - let shown = text; - let line = caret?.line; - while (!fitsAsResult({ text: shown, caret })) { - const cut = shown.slice(Math.floor(shown.length / 4)); - const at = cut.indexOf("\n"); - const kept = at >= 0 ? cut.slice(at + 1) : cut; - // Lines cut whole take the cursor up with them; one cut in two loses it. - if (line !== undefined) { - line = - at >= 0 ? line - (shown.slice(0, shown.length - kept.length).split("\n").length - 1) : -1; - } - shown = kept; - } - return caret && line !== undefined && line >= 0 - ? { text: shown, caret: { line, column: caret.column } } - : { text: shown }; -} - -/* - * The chat's controls, as its composer, request card, stop button and tab - * do: the device asking is the user's own, holding the account key, so - * what it sends is the user's, not an agent's. - */ - -/** - * Sends the chat a message, with the images and files the device attached - * for it (chat.attach, chat.upload), which then go; text or an attachment - * at least. - */ -export async function sendChat( - services: CliServices, - { workspaceId, tabId, text, attachments: ids = [], when, clientNonce }: RpcParams<"chat.send">, - attached?: { device: string; attachments: Attachments }, - relay?: RelaySender, -): Promise> { - if (!text.trim() && ids.length === 0) { - throw new RpcFailure(RpcErrorCode.InvalidParams, "a message needs text or an attachment"); - } - if (ids.length > 0 && !attached) { - throw new RpcFailure(RpcErrorCode.InvalidParams, "no attachments are kept here"); - } - // An agent's message goes, or is turned down, while its request is weighed: - // held for later, it would go after the user may have turned agents off. - if (relay && when === "after") { - throw new RpcFailure( - RpcErrorCode.InvalidParams, - "an agent's message is not held for after the turn; send it as next or now", - ); - } - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const held = - attached && ids.length > 0 - ? attached.attachments.find(attached.device, workspaceId, tabId, ids) - : []; - const images = held.flatMap((h) => - h.kind === "image" ? [{ mediaType: h.mediaType, data: h.data }] : [], - ); - const files = held.flatMap((h) => (h.kind === "file" ? [{ name: h.name, data: h.data }] : [])); - // Before it goes: what the chat shows from here on is from this message on. - const at = services.clock.now(); - const submission = - !relay && attached && clientNonce - ? { clientNonce, clientDeviceId: attached.device } - : undefined; - const { heldId, itemKey, rejected } = await services - .ui("chat.send", { - workspaceId, - tabId, - text, - // An agent's shows as its, and waits while the chat asks the user something. - ...(relay ? { from: relay } : { user: true }), - ...(images.length > 0 ? { images } : {}), - ...(files.length > 0 ? { files } : {}), - ...(when ? { when } : {}), - // Held, it is dropped if control from other computers is turned off first. - ...(when === "after" && attached ? { device: attached.device } : {}), - ...(submission ? { submission } : {}), - }) - .catch((error: unknown) => { - throw new RpcFailure( - RpcErrorCode.Unavailable, - error instanceof Error ? error.message : String(error), - { outcome: "unknown" }, - ); - }); - if (rejected !== undefined) { - throw new RpcFailure(RpcErrorCode.Unavailable, rejected, { outcome: "rejected" }); - } - attached?.attachments.drop(ids); - return { - at, - ...(heldId ? { heldId } : {}), - ...(itemKey ? { itemKey } : {}), - ...(submission ? { clientNonce: submission.clientNonce } : {}), - }; -} - -/** Takes back a message a chat holds for after its turn, with its whole text. */ -export async function unqueueChat( - services: CliServices, - { workspaceId, tabId, id }: RpcParams<"chat.unqueue">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - return services.ui("chat.unqueue", { workspaceId, tabId, id }).catch(chatFailure); -} - -/** Changes the text of a message a chat holds for after its turn. */ -export async function editHeldChat( - services: CliServices, - { workspaceId, tabId, id, text }: RpcParams<"chat.editHeld">, - device?: string, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - // Changed by the user there, it is theirs now: an agent's no longer. - await services - .ui("chat.editHeld", { workspaceId, tabId, id, text, ...(device ? { device } : {}) }) - .catch(chatFailure); - return {}; -} - -/** Takes back one of the user's messages a Claude chat has not read. */ -export async function withdrawChat( - services: CliServices, - { workspaceId, tabId, itemKey }: RpcParams<"chat.withdraw">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude"]); - return services.ui("chat.withdraw", { workspaceId, tabId, itemKey }).catch(chatFailure); -} - -/** Has a Claude chat read its unread messages now. */ -export async function hurryChat( - services: CliServices, - { workspaceId, tabId }: RpcParams<"chat.hurry">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude"]); - await services.ui("chat.hurry", { workspaceId, tabId }).catch(chatFailure); - return {}; -} - -/** - * Adds one of this device's folders as a workspace, as Add Workspace does; - * the user is told which device added it. - */ -export async function createWorkspace( - services: CliServices, - { path: asked, name }: RpcParams<"workspace.create">, - device: string | undefined, - system: FolderSystem = SYSTEM_FOLDERS, -): Promise> { - const rootPath = await folderToAdd(asked, system); - const { workspace, added } = await services - .addWorkspace({ kind: "local", rootPath }, name) - .catch(unavailable); - if (added) { - const t = tryGetMainT(); - const from = device ?? "another device"; - const title = t ? String(t("common:remoteNotice.workspaceAdded", { device: from })) : ""; - services.notify(workspace.id, "", title || `Workspace added from ${from}`, rootPath); - } - return { - workspace: { id: workspace.id, name: workspace.name, path: workspace.rootPath }, - created: added, - }; -} - -/** Room an image's base64 may take in its answer: the frame, less the envelope and its other fields. */ -const IMAGE_CHARS = MAX_MESSAGE_BYTES - ENVELOPE_BYTES - 256; - -/** An image of the chat, drawn smaller where it would not go in a frame. */ -export async function chatImage( - services: CliServices, - { workspaceId, tabId, ref, maxEdge, versioned, ifVersion }: RpcParams<"chat.image">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const image = await services - .ui("chat.image", { - workspaceId, - tabId, - ref, - maxChars: IMAGE_CHARS, - ...(maxEdge !== undefined ? { maxEdge } : {}), - ...(versioned ? { versioned } : {}), - ...(ifVersion !== undefined ? { ifVersion } : {}), - }) - .catch((error: unknown) => { - const message = error instanceof Error ? error.message : String(error); - if (/no such image/.test(message)) throw new RpcFailure(RpcErrorCode.NotFound, message); - return unavailable(error); - }); - if (!fitsAsResult(image)) { - throw new RpcFailure(RpcErrorCode.Unavailable, "the image would not go in a frame"); - } - return image; -} - -/** Keeps an image for the device's next message to the chat. */ -export async function attachToChat( - services: CliServices, - { workspaceId, tabId, mediaType, data }: RpcParams<"chat.attach">, - device: string, - attachments: Attachments, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - return { attachmentId: attachments.take(device, workspaceId, tabId, { mediaType, data }) }; -} - -/** Starts a file's upload for the device's next message to the chat. */ -export async function beginUpload( - services: CliServices, - { workspaceId, tabId, name, size, mediaType }: RpcParams<"chat.upload.begin">, - device: string, - uploads: Uploads, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - return { - uploadId: uploads.begin(device, workspaceId, tabId, { - name, - size, - ...(mediaType ? { mediaType } : {}), - }), - }; -} - -export async function answerChat( - services: CliServices, - { - workspaceId, - tabId, - requestId, - decision, - message, - updatedInput, - stop, - always, - planMode, - }: RpcParams<"chat.answer">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - await services - .ui("chat.answer", { - workspaceId, - tabId, - decision, - expect: requestId, - ...(message !== undefined ? { message } : {}), - ...(updatedInput ? { updatedInput } : {}), - ...(stop ? { stop } : {}), - ...(decision === "allow" && always?.length ? { always } : {}), - ...(decision === "allow" && planMode ? { planMode } : {}), - }) - .catch(unavailable); - return {}; -} - -/** What a tab's choices are, as the protocol tells them; the modes named in this app's language. */ -export function settingsOf(choices: CliChatChoices): RpcResult<"chat.settings"> { - const set = CHAT_MODES[choices.agent]; - const t = tryGetMainT(); - return { - agent: choices.agent, - model: choices.model, - effort: choices.effort, - permissionMode: choices.mode, - options: { - models: choices.models.map((m) => ({ id: m.value, label: m.name })), - efforts: [...choices.efforts], - modes: set.modes.map((id) => ({ - id, - label: t ? String(t(`common:${set.prefix}.${id}`)) : id, - ...(id === set.dangerous ? { dangerous: true } : {}), - })), - }, - }; -} - -export async function chatSettings( - services: CliServices, - { workspaceId, tabId }: RpcParams<"chat.settings">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const choices = await services - .ui("chat.settings", { workspaceId, tabId, settings: {} }) - .catch(unavailable); - return settingsOf(choices); -} - -/** - * Sets what the user asked from another device, as the composer's chips - * do: the mode that lets everything run too, unless a policy keeps it off - * here (Forbidden, with why). - */ -export async function configureChat( - services: CliServices, - { workspaceId, tabId, model, effort, permissionMode }: RpcParams<"chat.configure">, - byAgent = false, -): Promise> { - const tab = await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const settings = { - ...(model !== undefined ? { model } : {}), - ...(effort !== undefined ? { effort } : {}), - ...(permissionMode !== undefined ? { mode: permissionMode } : {}), - }; - if (permissionMode !== undefined) { - const set = CHAT_MODES[tab.type as ChatAgent]; - if (chatModeNamed(tab.type as ChatAgent, permissionMode) === set.dangerous) { - const now = await services - .ui("chat.settings", { workspaceId, tabId, settings: {} }) - .catch(unavailable); - if (now.dangerousBlocked) { - throw new RpcFailure( - RpcErrorCode.Forbidden, - `${set.dangerous} is turned off on this computer: ${now.dangerousBlocked}`, - ); - } - } - } - const choices = await services - .ui( - "chat.settings", - // An agent never takes the mode that lets everything run. - { workspaceId, tabId, settings, ...(byAgent ? {} : { user: true }) }, - SETTINGS_TIMEOUT_MS, - ) - .catch(unavailable); - return settingsOf(choices); -} - -/** How long a change of settings may take: a model change restarts nothing, but waits to be shown. */ -const SETTINGS_TIMEOUT_MS = 60_000; - -export async function chatCommands( - services: CliServices, - { workspaceId, tabId }: RpcParams<"chat.commands">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - return services.ui("chat.commands", { workspaceId, tabId }).catch(unavailable); -} - -/** What an @ in the tab's composer may name, as its agent suggests it. */ -export async function chatFileSuggestions( - services: CliServices, - { workspaceId, tabId, query }: RpcParams<"chat.fileSuggestions">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const { paths } = await services - .ui("chat.fileSuggestions", { workspaceId, tabId, query }) - .catch(unavailable); - return { paths: paths.slice(0, MAX_FILE_SUGGESTIONS) }; -} - -/** - * Runs one of the app's commands the tab lists, as its composer runs it - * typed; one the agent runs goes with chat.send, and the model and effort - * with chat.configure. - */ -export async function runChatCommand( - services: CliServices, - { workspaceId, tabId, name, args: given }: RpcParams<"chat.command">, - system: FolderSystem = SYSTEM_FOLDERS, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const bare = name.replace(/^\//, ""); - const lower = bare.toLowerCase(); - const { commands } = await services - .ui("chat.commands", { workspaceId, tabId }) - .catch(unavailable); - const command = - commands.find((c) => c.name.toLowerCase() === lower) ?? - commands.find((c) => c.aliases?.some((alias) => alias.toLowerCase() === lower)); - if (command?.runs !== "command") { - throw new RpcFailure( - RpcErrorCode.InvalidParams, - !command - ? `/${bare} is not a command this chat lists (chat.commands)` - : command.runs === "send" - ? `/${bare} is the agent's own: send it as a message (chat.send)` - : `/${bare} sets what chat.configure sets`, - ); - } - // A folder the chat may read, which the files methods then read too: by its - // real path, and not one that would open all of the disk. - const args = - command.name === "add-dir" && given?.trim() - ? await folderToAdd(await addDirPath(services, workspaceId, tabId, given, system), system) - : given; - return services - .ui( - "chat.command", - { workspaceId, tabId, name: command.name, ...(args ? { args } : {}) }, - SETTINGS_TIMEOUT_MS, - ) - .catch(unavailable); -} - -/** The folder /add-dir names, as the composer reads it: from the chat's folder, or the home's. */ -async function addDirPath( - services: CliServices, - workspaceId: string, - tabId: string, - given: string, - system: FolderSystem, -): Promise { - const p = system.platform === "win32" ? win32 : posix; - const path = given.trim().replace(/^(["'])(.*)\1$/, "$2"); - if (path === "~" || path.startsWith("~/") || path.startsWith("~\\")) { - return p.join(system.home(), path.slice(1)); - } - if (p.isAbsolute(path)) return path; - const info = await services - .ui("chat.state", { workspaceId, tabId, peek: true }) - .catch(() => null); - const cwd = info?.cwd ?? workspaceOf(services, workspaceId)?.rootPath ?? system.home(); - return p.resolve(cwd, path); -} - -/** - * Has a chat run with another profile, as the CLI's chat profile does, the - * user asking from another device: no one here is asked again. - */ -export async function changeChatProfile( - services: CliServices, - { workspaceId, tabId, profile, fresh, stop }: RpcParams<"chat.profile">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - return services - .ui( - "chat.profile", - { workspaceId, tabId, profile, fresh: fresh ?? false, ...(stop ? { stop } : {}) }, - SETTINGS_TIMEOUT_MS, - ) - .catch((error: unknown) => { - const message = error instanceof Error ? error.message : String(error); - if (/ has no profile /.test(message)) - throw new RpcFailure(RpcErrorCode.InvalidParams, message); - return unavailable(error); - }); -} - -export async function interruptChat( - services: CliServices, - { workspaceId, tabId }: RpcParams<"chat.interrupt">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - await services.ui("chat.interrupt", { workspaceId, tabId }).catch(unavailable); - return {}; -} - -/** Starts a chat's program again, as its banner does: the user's, from another device. */ -export async function restartChat( - services: CliServices, - { workspaceId, tabId }: RpcParams<"chat.restart">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - await services.ui("chat.restart", { workspaceId, tabId }).catch(chatFailure); - return {}; -} - -/** Asks a Codex chat's program again whether it is signed in, as its banner does: the user's. */ -export async function checkChatSignIn( - services: CliServices, - { workspaceId, tabId }: RpcParams<"chat.checkSignIn">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["codex"]); - return services.ui("chat.checkSignIn", { workspaceId, tabId }).catch(chatFailure); -} - -export async function newChat( - services: CliServices, - { - workspaceId, - agent, - prompt, - model, - effort, - permissionMode, - profile, - folder, - }: RpcParams<"chat.new">, - fs?: (workspaceId: string) => Promise, - relay?: RelaySender, -): Promise> { - const workspace = workspaceOf(services, workspaceId); - if (!workspace) throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); - const cwd = folder - ? await folderIn( - fs ? await fs(workspaceId).catch(unavailable) : null, - workspace.rootPath, - folder, - ) - : workspace.rootPath; - const settings = { - ...(model !== undefined ? { model } : {}), - ...(effort !== undefined ? { effort } : {}), - ...(permissionMode !== undefined ? { mode: permissionMode } : {}), - }; - const opened = await services - .ui( - "chat.open", - { - workspaceId, - agent, - cwd, - prompt: prompt ?? "", - // Behind what the window shows: whoever sits at this computer keeps their place. - placement: { background: true }, - ...(Object.keys(settings).length > 0 ? { settings } : {}), - ...(profile !== undefined ? { profile } : {}), - // An agent's prompt shows as its; its settings never the mode that lets everything run. - ...(relay ? { from: relay } : { user: true }), - }, - SETTINGS_TIMEOUT_MS, - ) - .catch((error: unknown) => { - const message = error instanceof Error ? error.message : String(error); - if (/ has no profile /.test(message)) - throw new RpcFailure(RpcErrorCode.InvalidParams, message); - return unavailable(error); - }); - return { tabId: opened.tabId, ...(opened.error ? { error: opened.error } : {}) }; -} - -/** How many session changes an events.read with no `limit` reads. */ -export const DEFAULT_EVENTS = 100; - -/** - * The sessions' state changes, as `nexus events` lists them: after `since` - * (the first ones, `more` when some were left out), or the last ones. As - * many as go in a frame, each message in a line. - */ -export async function readEvents( - services: CliServices, - { since, limit }: RpcParams<"events.read">, -): Promise> { - const read = await services - .ui("events.read", { - ...(since !== undefined ? { since } : {}), - limit: limit ?? DEFAULT_EVENTS, - }) - .catch(unavailable); - let events: RpcResult<"events.read">["events"] = read.events.map((e) => ({ - seq: e.seq, - at: e.at, - workspaceId: e.workspaceId, - tabId: e.tabId, - title: e.title, - kind: e.kind, - ...(e.agent ? { agent: e.agent } : {}), - from: e.from, - to: e.to, - ...(e.message ? { message: oneLine(e.message, 500) } : {}), - })); - let more = read.more; - while (events.length > 0 && !fitsAsResult({ events, cursor: read.cursor, more })) { - const half = Math.ceil(events.length / 2); - if (since === undefined) { - // The last ones: the latest are kept. - events = events.slice(half); - } else { - // After `since`: the first are kept, and the rest left for the next read. - events = events.slice(0, events.length - half); - more = true; - } - } - return { events, cursor: read.cursor, more }; -} - -/** The agent profiles a new chat may run with. */ -export async function listProfiles( - services: CliServices, - { agent }: RpcParams<"profiles.list">, -): Promise> { - return services.ui("chat.profiles", agent ? { agent } : {}).catch(unavailable); -} - -/** Renames the chat, as its tab's rename does. */ -export async function renameChat( - services: CliServices, - { workspaceId, tabId, title }: RpcParams<"chat.rename">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - if (!title.trim()) - throw new RpcFailure(RpcErrorCode.InvalidParams, "a title needs more than spaces"); - return services.ui("chat.rename", { workspaceId, tabId, title }).catch(unavailable); -} - -/** Most characters of a tool's result asked for: more than a frame takes anyway. */ -const MAX_TOOL_TEXT = MAX_MESSAGE_BYTES; - -/** One tool call, a subagent's too, with as much of its result as a frame takes. */ -export async function chatTool( - services: CliServices, - { workspaceId, tabId, toolUseId }: RpcParams<"chat.tool">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const ask = (maxText: number) => - services - .ui("chat.toolCall", { workspaceId, tabId, toolUseId, maxText }) - .catch((error: unknown) => { - const message = error instanceof Error ? error.message : String(error); - if (/^no tool call /.test(message)) throw new RpcFailure(RpcErrorCode.NotFound, message); - return unavailable(error); - }); - let tool = await ask(MAX_TOOL_TEXT); - // Its input, or text that escapes long in JSON: asked again with less of the result. - for (let maxText = MAX_TOOL_TEXT / 2; !fitsAsResult(tool); maxText = Math.floor(maxText / 2)) { - if (maxText < 1024) - throw new RpcFailure(RpcErrorCode.Unavailable, "that call is too large to send"); - tool = await ask(maxText); - } - return tool; -} - -/** A file the chat changed as it was before, for its diff, where it is kept and goes in a frame. */ -export async function chatOriginal( - services: CliServices, - { workspaceId, tabId, path }: RpcParams<"chat.original">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const { found, before } = await services - .ui("chat.original", { workspaceId, tabId, path }) - .catch(unavailable); - if (!found) throw new RpcFailure(RpcErrorCode.NotFound, `no original kept of ${path}`); - const result = { before }; - if (!fitsAsResult(result)) { - throw new RpcFailure(RpcErrorCode.Unavailable, "that file is too large to send"); - } - return result; -} - -/** What the chat's side panel shows; its changed files cut to what goes in a frame. */ -export async function chatPanel( - services: CliServices, - { workspaceId, tabId }: RpcParams<"chat.panel">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const panel = await services.ui("chat.panel", { workspaceId, tabId }).catch(unavailable); - let { changedFiles } = panel; - while (changedFiles.length > 0 && !fitsAsResult({ ...panel, changedFiles })) { - changedFiles = changedFiles.slice(0, Math.floor(changedFiles.length / 2)); - } - return { ...panel, changedFiles }; -} - -/** Text cut to `max` characters, an ellipsis where it was cut. */ -function cut(text: string, max: number): string { - return text.length > max ? `${text.slice(0, max - 1)}…` : text; -} - -/** What a run's row and its pane both tell of it. */ -function runFactsOf(run: AgentRun): ChatRun { - return { - id: run.id, - ...(run.program ? { program: run.program } : {}), - ...(run.endpoint ? { endpoint: cut(run.endpoint, 200) } : {}), - ...(run.model ? { model: cut(run.model, 200) } : {}), - ...(run.description ? { description: oneLine(run.description, MAX_AGENT_DESCRIPTION) } : {}), - start: run.start, - ...(run.end !== undefined ? { end: run.end } : {}), - state: run.state, - toolCount: Math.max(0, run.toolCount), - failedCount: Math.max(0, run.failedCount), - ...(run.toolsTold ? { toolsTold: true } : {}), - ...(run.exitCode !== undefined ? { exitCode: run.exitCode } : {}), - ...(run.sessionId ? { sessionId: cut(run.sessionId, 200) } : {}), - }; -} - -/** How long a run row's prompt, tool and last words are. */ -const RUN_ROW_TEXT = 300; - -/** - * A `claude -p` or `codex exec` run as the chat's agents list it: what it - * is, how it stands, the tool it runs now and what it wrote last. - */ -export function runRowOf(run: AgentRun): ChatRun { - const tool = [...run.tools].reverse().find((t) => t.end === undefined && !t.background); - const last = run.answer ?? run.messages?.at(-1)?.text; - return { - ...runFactsOf(run), - ...(run.prompt ? { prompt: cut(run.prompt, RUN_ROW_TEXT) } : {}), - ...(tool ? { current: cut(tool.target ? `${tool.name} ${tool.target}` : tool.name, 200) } : {}), - ...(last ? { last: cut(last, RUN_ROW_TEXT) } : {}), - }; -} - -/** A run as its pane shows it: its tools, messages and answer too, fewer messages where they would not fit. */ -export function runDetailOf(run: AgentRun): ChatRun { - const detail = (messages: NonNullable | undefined): ChatRun => ({ - ...runFactsOf(run), - ...(run.prompt ? { prompt: cut(run.prompt, MAX_RUN_TEXT) } : {}), - tools: run.tools.slice(-30).map((tool) => ({ - id: cut(tool.id, 200), - name: cut(tool.name, 200), - ...(tool.target ? { target: cut(tool.target, 2000) } : {}), - ...(tool.note ? { note: cut(tool.note, 2000) } : {}), - start: tool.start, - ...(tool.end !== undefined ? { end: tool.end } : {}), - ...(tool.failed ? { failed: true } : {}), - ...(tool.error ? { error: cut(tool.error, 2000) } : {}), - ...(tool.background ? { background: true } : {}), - ...(tool.denied ? { denied: true } : {}), - })), - ...(messages - ? { messages: messages.map((m) => ({ at: m.at, text: cut(m.text, MAX_RUN_TEXT) })) } - : {}), - ...(run.answer ? { answer: cut(run.answer, MAX_RUN_TEXT) } : {}), - }); - let messages = run.messages?.slice(-20); - let shown = detail(messages); - while (messages && messages.length > 0 && !fitsAsResult({ run: shown })) { - messages = messages.slice(Math.ceil(messages.length / 2)); - shown = detail(messages); - } - return shown; -} - -/** The `claude -p` and `codex exec` runs of the chat's session, as its agents list them. */ -export async function chatRuns( - services: CliServices, - { workspaceId, tabId }: RpcParams<"chat.runs">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const runs = services.claudeAgents - .list(workspaceId, tabId) - .filter((agent) => agent.kind === "print") - .slice(0, MAX_CHAT_RUNS); - return { runs: runs.map(runRowOf) }; -} - -/** One of the chat's runs, as its pane shows it. */ -export async function chatRun( - services: CliServices, - { workspaceId, tabId, runId }: RpcParams<"chat.run">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const run = services.claudeAgents - .list(workspaceId, tabId) - .find((agent) => agent.kind === "print" && agent.id === runId); - if (!run) throw new RpcFailure(RpcErrorCode.NotFound, `no run ${runId}: it is not kept`); - return { run: runDetailOf(run) }; -} - -/** Stops one of the chat's runs, as its pane's stop button does. */ -export async function stopChatRun( - services: CliServices, - { workspaceId, tabId, runId }: RpcParams<"chat.stopRun">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - return { stopped: await services.claudeAgents.stop(workspaceId, tabId, runId) }; -} - -/** Stops one of the chat's tasks, as its panel's stop button does. */ -export async function stopChatTask( - services: CliServices, - { workspaceId, tabId, taskId }: RpcParams<"chat.stopTask">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - await services.ui("chat.stopTask", { workspaceId, tabId, taskId }).catch((error: unknown) => { - const message = error instanceof Error ? error.message : String(error); - if (/^no task /.test(message)) throw new RpcFailure(RpcErrorCode.NotFound, message); - return unavailable(error); - }); - return {}; -} - -/** A tab's failure as another device is told it: an item or a call it does not have is not found. */ -function chatFailure(error: unknown): never { - const message = error instanceof Error ? error.message : String(error); - if (/^no (item|tool call|message|held message) /.test(message)) { - throw new RpcFailure(RpcErrorCode.NotFound, message); - } - if (/^a message needs /.test(message)) throw new RpcFailure(RpcErrorCode.InvalidParams, message); - return unavailable(error); -} - -/** Whether rewinding to before a user message can restore files. */ -export async function previewChatRewind( - services: CliServices, - { workspaceId, tabId, itemKey }: RpcParams<"chat.rewindPreview">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - const preview = await services - .ui("chat.rewindPreview", { workspaceId, tabId, itemKey }) - .catch(chatFailure); - // A rewind that touches many files names as many as go in a frame. - let { filesChanged } = preview; - while (filesChanged.length > 0 && !fitsAsResult({ ...preview, filesChanged })) { - filesChanged = filesChanged.slice(0, Math.floor(filesChanged.length / 2)); - } - return { ...preview, filesChanged }; -} - -/** Rewinds a chat to before one of the user's messages. */ -export async function rewindChat( - services: CliServices, - { workspaceId, tabId, itemKey, files }: RpcParams<"chat.rewind">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - await services.ui("chat.rewind", { workspaceId, tabId, itemKey, files }).catch(chatFailure); - return {}; -} - -/** Forks a chat into a new tab in the background. */ -export async function forkChat( - services: CliServices, - { workspaceId, tabId, itemKey }: RpcParams<"chat.fork">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - return services - .ui("chat.fork", { workspaceId, tabId, ...(itemKey ? { itemKey } : {}) }) - .catch(chatFailure); -} - -/** Sends a Claude chat's running command or subagent to the background. */ -export async function backgroundChatTool( - services: CliServices, - { workspaceId, tabId, toolUseId }: RpcParams<"chat.background">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude"]); - await services.ui("chat.background", { workspaceId, tabId, toolUseId }).catch(chatFailure); - return {}; -} - -/** Turns a Claude chat's Remote Control on or off. */ -export async function setChatRemoteControl( - services: CliServices, - { workspaceId, tabId, enabled }: RpcParams<"chat.remoteControl">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude"]); - await services.ui("chat.remoteControl", { workspaceId, tabId, enabled }).catch(chatFailure); - return {}; -} - -/** - * A Claude chat's Claude Code settings, or a Codex chat's defaults, read or - * changed as its dialog does: the user's, from another device. - */ -export async function workChatConfig( - services: CliServices, - params: RpcParams<"chat.config">, -): Promise> { - await tabOf(services, params.workspaceId, params.tabId, ["claude", "codex"]); - return services.ui("chat.config", params, SETTINGS_TIMEOUT_MS).catch(chatFailure); -} - -/** Works a Claude chat's MCP server, as its panel does: the user's, from another device. */ -export async function workChatMcp( - services: CliServices, - params: RpcParams<"chat.mcp">, -): Promise> { - await tabOf(services, params.workspaceId, params.tabId, ["claude"]); - return services.ui("chat.mcp", params, SETTINGS_TIMEOUT_MS).catch(chatFailure); -} - -/** The workspace's conversations of the past that a new chat can go on with. */ -export async function listHistory( - services: CliServices, - { workspaceId, agent, query, limit }: RpcParams<"history.list">, -): Promise> { - if (!workspaceOf(services, workspaceId)) { - throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); - } - const { sessions } = await services - .ui("chat.history", { - workspaceId, - agent, - ...(agent === "codex" ? { peer: true } : {}), - ...(query ? { query } : {}), - ...(limit ? { limit } : {}), - }) - .catch(unavailable); - // Long first prompts for titles: as many as go in a frame. - let shown = sessions; - while (shown.length > 0 && !fitsAsResult({ sessions: shown })) { - shown = shown.slice(0, Math.floor(shown.length / 2)); - } - return { sessions: shown }; -} - -/** Goes on with a conversation of the past in a new chat, behind what this computer shows. */ -export async function resumeChat( - services: CliServices, - { workspaceId, agent, sessionId, tabId }: RpcParams<"chat.resume">, -): Promise> { - if (!workspaceOf(services, workspaceId)) { - throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); - } - if (tabId) await tabOf(services, workspaceId, tabId, [agent]); - return services - .ui("chat.resume", { workspaceId, agent, sessionId, ...(tabId ? { tabId } : {}) }) - .catch((error: unknown) => { - const message = error instanceof Error ? error.message : String(error); - if (/^no conversation /.test(message)) throw new RpcFailure(RpcErrorCode.NotFound, message); - return unavailable(error); - }); -} - -export async function closeChat( - services: CliServices, - { workspaceId, tabId }: RpcParams<"chat.close">, -): Promise> { - await tabOf(services, workspaceId, tabId, ["claude", "codex"]); - await services.ui("tab.close", { workspaceId, tabId, force: false }).catch(unavailable); - return {}; -} - -/** Most files.find and files.search one device may have running here at once. */ -export const MAX_SEARCHES_PER_DEVICE = 2; - /** Answers another device's request with this device's services. */ -export function serveWith( - services: () => CliServices | null, - watches: { - chats?: ChatWatches; - sessions?: SessionWatches; - terminals?: TerminalWatches; - /** The tasks' git facts the sessions' list tells. */ - git?: WorkspaceGit; - /** The images and files devices attached for their next messages. */ - attachments?: Attachments; - /** The files devices are sending a piece at a time; fresh ones when not given. */ - uploads?: Uploads; - /** The workspaces' file systems, and how an image is drawn smaller. */ - files?: Omit; - /** The workspaces' repositories, for git.status, git.diff, git.log and git.show. */ - repositories?: RemoteGit; - /** The workspaces' notes, for notes.list and notes.attach, and the note it records into. */ - notes?: RemoteNotes; - /** How many photos each device put into notes lately; a fresh one when not given. */ - photoQuota?: PhotoQuota; - /** Whether an agent on another device may do what it asks (a request with `from`). */ - agents?: AgentGate; - /** A device of the room by its id, as it names itself. */ - deviceName?: (id: string) => string | undefined; - /** The folders folders.list goes through; the system's when not given. */ - folders?: FolderSystem; - /** - * What the user lets other devices control here, for the consent prompts - * they may answer; none when not told. - */ - controls?: () => ConsentControls; - } = {}, -) { +export function serveWith(services: () => CliServices | null, watches: RemoteServeOptions = {}) { const { chats, sessions, terminals, git } = watches; const attachments = watches.attachments ?? new Attachments(); const uploads = watches.uploads ?? new Uploads(attachments); + const fileWrites = watches.fileWrites ?? new FileWrites(); const photoQuota = watches.photoQuota ?? new PhotoQuota(); /** files.find and files.search running now, by the device that asked. */ const searching = new Map(); - return async (request: { from: string; method: RpcMethod; params: unknown }) => { + return async (request: RemoteServeRequest) => { const s = services(); if (!s) throw new RpcFailure(RpcErrorCode.Unavailable, "the app is still starting"); const { from, method, params } = request; @@ -1405,8 +189,11 @@ export function serveWith( if (by && !watches.agents) { throw new RpcFailure(RpcErrorCode.Forbidden, "agents' requests are not taken here"); } - if (by && AGENT_READS.has(method)) watches.agents?.checkRead(); - if (by && watches.agents && !AGENT_READS.has(method)) { + // File controls ask in their handlers, after validating their scope. + const checkInHandler = isFileTransferMethod(method); + const readOnly = AGENT_READS.has(method); + if (by && (readOnly || checkInHandler)) watches.agents?.checkRead(); + if (by && watches.agents && !readOnly && !checkInHandler) { const device = { id: from, name: watches.deviceName?.(from) ?? from.slice(0, 8) }; await watches.agents.check(s, { device, @@ -1415,17 +202,15 @@ export function serveWith( }); relay = relaySenderOf(by, device); } + const checkCurrent = () => { + if (request.isCurrent?.() === false) { + throw new RpcFailure(RpcErrorCode.Unavailable, "the requesting device left the room"); + } + }; const controls = watches.controls?.() ?? { chat: false, terminal: false }; switch (request.method) { - case "sessions.list": { - const list = await listSessions(s, git); - const recording = watches.notes?.recording(); - return { - ...list, - consents: await remoteConsents(s, from, controls), - ...(recording ? { recording } : {}), - }; - } + case "sessions.list": + return serveSessionsList(s, git, watches.notes, from, controls); case "notes.list": case "notes.attach": { const notes = watches.notes; @@ -1460,71 +245,23 @@ export function serveWith( { device: from, attachments }, relay, ); + case "files.write.begin": + case "files.mkdir": + return serveFileDestination(s, request, watches, fileWrites, checkCurrent, by); + case "files.write.chunk": + await fileWrites.chunk(from, request.params as RpcParams<"files.write.chunk">); + return {}; + case "files.write.end": + return fileWrites.end(from, request.params as RpcParams<"files.write.end">); + case "files.write.abort": + await fileWrites.abort(from, request.params as RpcParams<"files.write.abort">); + return {}; case "files.list": - case "files.read": { - if (!watches.files) { - throw new RpcFailure(RpcErrorCode.MethodNotFound, `${request.method} is not served yet`); - } - const { workspaceId } = request.params as { workspaceId: string }; - const workspace = workspaceOf(s, workspaceId); - if (!workspace) throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); - const files: RemoteFiles = { - ...watches.files, - folders: (id) => - s - .ui("workspace.folders", { workspaceId: id }) - .then((r) => r.folders) - .catch(() => []), - named: (id) => - s - .ui("chat.outsideFiles", { workspaceId: id }) - .then((r) => r.files) - .catch(() => []), - }; - if (request.method === "files.list") { - return listFiles(files, workspace.rootPath, request.params as RpcParams<"files.list">); - } - const rootPath = workspace.rootPath; - const location = structuredClone(workspace.location); - const result = await readFile(files, rootPath, request.params as RpcParams<"files.read">); - const current = workspaceOf(s, workspaceId); - if ( - !current || - current.rootPath !== rootPath || - !isDeepStrictEqual(current.location, location) - ) - throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); - return result; - } + case "files.read": + return serveFileRead(s, request, watches, checkCurrent, by); case "files.find": - case "files.search": { - if (!watches.files) { - throw new RpcFailure(RpcErrorCode.MethodNotFound, `${request.method} is not served yet`); - } - const { workspaceId } = request.params as { workspaceId: string }; - if (!workspaceOf(s, workspaceId)) { - throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); - } - // Each runs through a workspace's files on its host: a device runs a few at once, no more. - const running = searching.get(from) ?? 0; - if (running >= MAX_SEARCHES_PER_DEVICE) { - throw new RpcFailure( - RpcErrorCode.Unavailable, - `that device has ${running} searches running here already: ask again once one ends`, - ); - } - searching.set(from, running + 1); - try { - const fs = await watches.files.fs(workspaceId).catch(unavailable); - return await (request.method === "files.find" - ? findFiles(fs, request.params as RpcParams<"files.find">) - : searchFiles(fs, request.params as RpcParams<"files.search">)); - } finally { - const left = (searching.get(from) ?? 1) - 1; - if (left > 0) searching.set(from, left); - else searching.delete(from); - } - } + case "files.search": + return serveFileSearch(s, request, watches, searching); case "git.status": case "git.diff": case "git.log": @@ -1669,3 +406,7 @@ export function serveWith( } }; } + +export { DEFAULT_EVENTS } from "./serving/chat-details"; +export { MAX_SEARCHES_PER_DEVICE } from "./serving/files"; +export type { WorkspaceGit } from "./serving/sessions"; diff --git a/src/main/features/remote/serving/chat-details.ts b/src/main/features/remote/serving/chat-details.ts new file mode 100644 index 000000000..9b5a689ae --- /dev/null +++ b/src/main/features/remote/serving/chat-details.ts @@ -0,0 +1,359 @@ +import { + type ChatPage, + type ChatRun, + MAX_AGENT_DESCRIPTION, + MAX_CHAT_RUNS, + MAX_RUN_TEXT, + RpcErrorCode, + type RpcParams, + type RpcResult, + type ViewToolCall, +} from "../../../../../packages/remote-protocol/src"; +import type { AgentRun } from "../../../../shared/claude/activity"; +import type { CliServices } from "../../cli/context"; +import { MAX_MESSAGE_BYTES, RpcFailure } from "../room"; +import { chatFailure } from "./chat"; +import { fitsAsResult, oneLine, tabOf, unavailable, workspaceOf } from "./common"; + +/** How many items a chat read gives when it does not say (the renderer's default too). */ +export const DEFAULT_READ_ITEMS = 200; + +/** + * A page of the chat, of up to `limit` items: fewer when that many would + * not go in one frame (long tool results, a long reply), down to one item. + */ +export async function readChat( + services: CliServices, + { workspaceId, tabId, before, limit }: RpcParams<"chat.read">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + let count = limit ?? DEFAULT_READ_ITEMS; + for (;;) { + const page = await services + .ui("chat.page", { + workspaceId, + tabId, + ...(before !== undefined ? { before } : {}), + limit: count, + }) + .catch(unavailable); + if (fitsAsResult(page)) return page; + const fittedChildren = fitOversizeCodexChildren(page); + if (fittedChildren) return fittedChildren; + if (page.items.length <= 1) { + const fitted = fitPage(page); + if (fitted) return fitted; + throw new RpcFailure(RpcErrorCode.Unavailable, "that part of the chat is too large to send"); + } + count = Math.max(1, Math.floor(page.items.length / 2)); + } +} + +/** + * Native Codex children appear on the latest page regardless of its parent + * item count. If their messages alone exceed the frame, the existing + * subagent fallback can keep the parent page before it is narrowed. + */ +export function fitOversizeCodexChildren

(page: P): P | null { + if ( + page.status?.agent !== "codex" || + page.start + page.items.length !== page.total || + fitsAsResult({ agentItems: page.agentItems }) + ) { + return null; + } + const parent = withoutSubagentItems(page); + return fitsAsResult(parent) ? parent : null; +} + +function withoutSubagentItems

(page: P): P { + const tools = Object.fromEntries( + Object.entries(page.tools).filter(([, tool]) => tool.parentToolUseId === null), + ); + return { ...page, tools, agentItems: {} }; +} + +/** The most of each tool's result a page keeps, in turn, when one item is too large to send. */ +const FITTED_RESULT_CHARS = [8 * 1024, 1024, 0]; + +/** + * A page of one item too large for a frame as it is (a reply whose calls gave long results, a + * subagent's whole run), made to go in one: its tools' results cut shorter, each marked + * `truncated` (the other device reads a call whole by chat.tool), and at last its subagents' + * messages and calls left out. Refused, the item would stop the chat being read further back + * there at all. Null when even that is too large. + */ +export function fitPage

(page: P): P | null { + for (const most of FITTED_RESULT_CHARS) { + const cut = { ...page, tools: cutResults(page.tools, most) }; + if (fitsAsResult(cut)) return cut; + } + const parent = withoutSubagentItems(page); + const bare = { ...parent, tools: cutResults(parent.tools, 0) }; + return fitsAsResult(bare) ? bare : null; +} + +/** Tools with at most `most` characters of each result, and no structured result where cut. */ +function cutResults( + tools: Record, + most: number, +): Record { + const cut: Record = {}; + for (const [id, tool] of Object.entries(tools)) { + const result = tool.result; + if (!result || (result.text.length <= most && result.structured === undefined)) { + cut[id] = tool; + continue; + } + const { structured: _left, ...rest } = result; + let text = result.text.slice(0, most); + // A character of two halves is not split. + if (/[\uD800-\uDBFF]$/.test(text)) text = text.slice(0, -1); + cut[id] = { ...tool, result: { ...rest, text, truncated: true } }; + } + return cut; +} + +/** + * The sessions' state changes, as `nexus events` lists them: after `since` + * (the first ones, `more` when some were left out), or the last ones. As + * many as go in a frame, each message in a line. + */ +export async function readEvents( + services: CliServices, + { since, limit }: RpcParams<"events.read">, +): Promise> { + const read = await services + .ui("events.read", { + ...(since !== undefined ? { since } : {}), + limit: limit ?? DEFAULT_EVENTS, + }) + .catch(unavailable); + let events: RpcResult<"events.read">["events"] = read.events.map((e) => ({ + seq: e.seq, + at: e.at, + workspaceId: e.workspaceId, + tabId: e.tabId, + title: e.title, + kind: e.kind, + ...(e.agent ? { agent: e.agent } : {}), + from: e.from, + to: e.to, + ...(e.message ? { message: oneLine(e.message, 500) } : {}), + })); + let more = read.more; + while (events.length > 0 && !fitsAsResult({ events, cursor: read.cursor, more })) { + const half = Math.ceil(events.length / 2); + if (since === undefined) { + // The last ones: the latest are kept. + events = events.slice(half); + } else { + // After `since`: the first are kept, and the rest left for the next read. + events = events.slice(0, events.length - half); + more = true; + } + } + return { events, cursor: read.cursor, more }; +} + +/** Most characters of a tool's result asked for: more than a frame takes anyway. */ +const MAX_TOOL_TEXT = MAX_MESSAGE_BYTES; + +/** One tool call, a subagent's too, with as much of its result as a frame takes. */ +export async function chatTool( + services: CliServices, + { workspaceId, tabId, toolUseId }: RpcParams<"chat.tool">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const ask = (maxText: number) => + services + .ui("chat.toolCall", { workspaceId, tabId, toolUseId, maxText }) + .catch((error: unknown) => { + const message = error instanceof Error ? error.message : String(error); + if (/^no tool call /.test(message)) throw new RpcFailure(RpcErrorCode.NotFound, message); + return unavailable(error); + }); + let tool = await ask(MAX_TOOL_TEXT); + // Its input, or text that escapes long in JSON: asked again with less of the result. + for (let maxText = MAX_TOOL_TEXT / 2; !fitsAsResult(tool); maxText = Math.floor(maxText / 2)) { + if (maxText < 1024) + throw new RpcFailure(RpcErrorCode.Unavailable, "that call is too large to send"); + tool = await ask(maxText); + } + return tool; +} + +/** A file the chat changed as it was before, for its diff, where it is kept and goes in a frame. */ +export async function chatOriginal( + services: CliServices, + { workspaceId, tabId, path }: RpcParams<"chat.original">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const { found, before } = await services + .ui("chat.original", { workspaceId, tabId, path }) + .catch(unavailable); + if (!found) throw new RpcFailure(RpcErrorCode.NotFound, `no original kept of ${path}`); + const result = { before }; + if (!fitsAsResult(result)) { + throw new RpcFailure(RpcErrorCode.Unavailable, "that file is too large to send"); + } + return result; +} + +/** What the chat's side panel shows; its changed files cut to what goes in a frame. */ +export async function chatPanel( + services: CliServices, + { workspaceId, tabId }: RpcParams<"chat.panel">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const panel = await services.ui("chat.panel", { workspaceId, tabId }).catch(unavailable); + let { changedFiles } = panel; + while (changedFiles.length > 0 && !fitsAsResult({ ...panel, changedFiles })) { + changedFiles = changedFiles.slice(0, Math.floor(changedFiles.length / 2)); + } + return { ...panel, changedFiles }; +} + +/** Text cut to `max` characters, an ellipsis where it was cut. */ +function cut(text: string, max: number): string { + return text.length > max ? `${text.slice(0, max - 1)}…` : text; +} + +/** What a run's row and its pane both tell of it. */ +function runFactsOf(run: AgentRun): ChatRun { + return { + id: run.id, + ...(run.program ? { program: run.program } : {}), + ...(run.endpoint ? { endpoint: cut(run.endpoint, 200) } : {}), + ...(run.model ? { model: cut(run.model, 200) } : {}), + ...(run.description ? { description: oneLine(run.description, MAX_AGENT_DESCRIPTION) } : {}), + start: run.start, + ...(run.end !== undefined ? { end: run.end } : {}), + state: run.state, + toolCount: Math.max(0, run.toolCount), + failedCount: Math.max(0, run.failedCount), + ...(run.toolsTold ? { toolsTold: true } : {}), + ...(run.exitCode !== undefined ? { exitCode: run.exitCode } : {}), + ...(run.sessionId ? { sessionId: cut(run.sessionId, 200) } : {}), + }; +} + +/** How long a run row's prompt, tool and last words are. */ +const RUN_ROW_TEXT = 300; + +/** + * A `claude -p` or `codex exec` run as the chat's agents list it: what it + * is, how it stands, the tool it runs now and what it wrote last. + */ +export function runRowOf(run: AgentRun): ChatRun { + const tool = [...run.tools].reverse().find((t) => t.end === undefined && !t.background); + const last = run.answer ?? run.messages?.at(-1)?.text; + return { + ...runFactsOf(run), + ...(run.prompt ? { prompt: cut(run.prompt, RUN_ROW_TEXT) } : {}), + ...(tool ? { current: cut(tool.target ? `${tool.name} ${tool.target}` : tool.name, 200) } : {}), + ...(last ? { last: cut(last, RUN_ROW_TEXT) } : {}), + }; +} + +/** A run as its pane shows it: its tools, messages and answer too, fewer messages where they would not fit. */ +export function runDetailOf(run: AgentRun): ChatRun { + const detail = (messages: NonNullable | undefined): ChatRun => ({ + ...runFactsOf(run), + ...(run.prompt ? { prompt: cut(run.prompt, MAX_RUN_TEXT) } : {}), + tools: run.tools.slice(-30).map((tool) => ({ + id: cut(tool.id, 200), + name: cut(tool.name, 200), + ...(tool.target ? { target: cut(tool.target, 2000) } : {}), + ...(tool.note ? { note: cut(tool.note, 2000) } : {}), + start: tool.start, + ...(tool.end !== undefined ? { end: tool.end } : {}), + ...(tool.failed ? { failed: true } : {}), + ...(tool.error ? { error: cut(tool.error, 2000) } : {}), + ...(tool.background ? { background: true } : {}), + ...(tool.denied ? { denied: true } : {}), + })), + ...(messages + ? { messages: messages.map((m) => ({ at: m.at, text: cut(m.text, MAX_RUN_TEXT) })) } + : {}), + ...(run.answer ? { answer: cut(run.answer, MAX_RUN_TEXT) } : {}), + }); + let messages = run.messages?.slice(-20); + let shown = detail(messages); + while (messages && messages.length > 0 && !fitsAsResult({ run: shown })) { + messages = messages.slice(Math.ceil(messages.length / 2)); + shown = detail(messages); + } + return shown; +} + +/** The `claude -p` and `codex exec` runs of the chat's session, as its agents list them. */ +export async function chatRuns( + services: CliServices, + { workspaceId, tabId }: RpcParams<"chat.runs">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const runs = services.claudeAgents + .list(workspaceId, tabId) + .filter((agent) => agent.kind === "print") + .slice(0, MAX_CHAT_RUNS); + return { runs: runs.map(runRowOf) }; +} + +/** One of the chat's runs, as its pane shows it. */ +export async function chatRun( + services: CliServices, + { workspaceId, tabId, runId }: RpcParams<"chat.run">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const run = services.claudeAgents + .list(workspaceId, tabId) + .find((agent) => agent.kind === "print" && agent.id === runId); + if (!run) throw new RpcFailure(RpcErrorCode.NotFound, `no run ${runId}: it is not kept`); + return { run: runDetailOf(run) }; +} + +/** Whether rewinding to before a user message can restore files. */ +export async function previewChatRewind( + services: CliServices, + { workspaceId, tabId, itemKey }: RpcParams<"chat.rewindPreview">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const preview = await services + .ui("chat.rewindPreview", { workspaceId, tabId, itemKey }) + .catch(chatFailure); + // A rewind that touches many files names as many as go in a frame. + let { filesChanged } = preview; + while (filesChanged.length > 0 && !fitsAsResult({ ...preview, filesChanged })) { + filesChanged = filesChanged.slice(0, Math.floor(filesChanged.length / 2)); + } + return { ...preview, filesChanged }; +} + +/** The workspace's conversations of the past that a new chat can go on with. */ +export async function listHistory( + services: CliServices, + { workspaceId, agent, query, limit }: RpcParams<"history.list">, +): Promise> { + if (!workspaceOf(services, workspaceId)) { + throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); + } + const { sessions } = await services + .ui("chat.history", { + workspaceId, + agent, + ...(agent === "codex" ? { peer: true } : {}), + ...(query ? { query } : {}), + ...(limit ? { limit } : {}), + }) + .catch(unavailable); + // Long first prompts for titles: as many as go in a frame. + let shown = sessions; + while (shown.length > 0 && !fitsAsResult({ sessions: shown })) { + shown = shown.slice(0, Math.floor(shown.length / 2)); + } + return { sessions: shown }; +} + +/** How many session changes an events.read with no `limit` reads. */ +export const DEFAULT_EVENTS = 100; diff --git a/src/main/features/remote/serving/chat.ts b/src/main/features/remote/serving/chat.ts new file mode 100644 index 000000000..32a3a98a7 --- /dev/null +++ b/src/main/features/remote/serving/chat.ts @@ -0,0 +1,645 @@ +import { posix, win32 } from "node:path"; +import { + MAX_FILE_SUGGESTIONS, + RpcErrorCode, + type RpcParams, + type RpcResult, +} from "../../../../../packages/remote-protocol/src"; +import type { RelaySender } from "../../../../shared/chat/relay"; +import type { CliChatChoices } from "../../../../shared/cli/ui"; +import { CHAT_MODES, chatModeNamed } from "../../../../shared/sessions/chat-modes"; +import type { ChatAgent } from "../../../../shared/sessions/task-agent"; +import { tryGetMainT } from "../../../i18n"; +import type { CliServices } from "../../cli/context"; +import type { FsProvider } from "../../fs/bridge/provider"; +import type { Attachments } from "../attachments"; +import { folderIn } from "../files"; +import { type FolderSystem, folderToAdd, SYSTEM_FOLDERS } from "../folders"; +import { MAX_MESSAGE_BYTES, RpcFailure } from "../room"; +import type { Uploads } from "../uploads"; +import { ENVELOPE_BYTES, fitsAsResult, tabOf, unavailable, workspaceOf } from "./common"; + +/* + * The chat's controls, as its composer, request card, stop button and tab + * do: the device asking is the user's own, holding the account key, so + * what it sends is the user's, not an agent's. + */ + +let lastWaitReceiptAt = Number.NEGATIVE_INFINITY; + +/** + * Sends the chat a message, with the images and files the device attached + * for it (chat.attach, chat.upload), which then go; text or an attachment + * at least. + */ +export async function sendChat( + services: CliServices, + { workspaceId, tabId, text, attachments: ids = [], when, clientNonce }: RpcParams<"chat.send">, + attached?: { device: string; attachments: Attachments }, + relay?: RelaySender, +): Promise> { + if (!text.trim() && ids.length === 0) { + throw new RpcFailure(RpcErrorCode.InvalidParams, "a message needs text or an attachment"); + } + if (ids.length > 0 && !attached) { + throw new RpcFailure(RpcErrorCode.InvalidParams, "no attachments are kept here"); + } + // An agent's message goes, or is turned down, while its request is weighed: + // held for later, it would go after the user may have turned agents off. + if (relay && when === "after") { + throw new RpcFailure( + RpcErrorCode.InvalidParams, + "an agent's message is not held for after the turn; send it as next or now", + ); + } + const tab = await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const held = + attached && ids.length > 0 + ? attached.attachments.find(attached.device, workspaceId, tabId, ids) + : []; + const images = held.flatMap((h) => + h.kind === "image" ? [{ mediaType: h.mediaType, data: h.data }] : [], + ); + const files = held.flatMap((h) => (h.kind === "file" ? [{ name: h.name, data: h.data }] : [])); + // Before it goes: what the chat shows from here on is from this message on. + const now = services.clock.now(); + const at = relay?.waits ? Math.max(now, lastWaitReceiptAt + 0.001) : now; + if (relay?.waits) lastWaitReceiptAt = at; + const submission = + !relay && attached && clientNonce + ? { clientNonce, clientDeviceId: attached.device } + : undefined; + const { heldId, itemKey, rejected } = await services + .ui("chat.send", { + workspaceId, + tabId, + text, + // An agent's shows as its, and waits while the chat asks the user something. + ...(relay ? { from: relay } : { user: true }), + ...(images.length > 0 ? { images } : {}), + ...(files.length > 0 ? { files } : {}), + ...(when ? { when } : {}), + // Held, it is dropped if control from other computers is turned off first. + ...(attached && (when === "after" || tab.type === "codex") + ? { device: attached.device } + : {}), + ...(submission ? { submission } : {}), + ...(relay?.waits ? { wait: { receiptAt: at } } : {}), + }) + .catch((error: unknown) => { + throw new RpcFailure( + RpcErrorCode.Unavailable, + error instanceof Error ? error.message : String(error), + { outcome: "unknown" }, + ); + }); + if (rejected !== undefined) { + throw new RpcFailure(RpcErrorCode.Unavailable, rejected, { outcome: "rejected" }); + } + attached?.attachments.drop(ids); + return { + at, + ...(heldId ? { heldId } : {}), + ...(itemKey ? { itemKey } : {}), + ...(submission ? { clientNonce: submission.clientNonce } : {}), + }; +} + +/** Takes back a message a chat holds for after its turn, with its whole text. */ +export async function unqueueChat( + services: CliServices, + { workspaceId, tabId, id }: RpcParams<"chat.unqueue">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + return services.ui("chat.unqueue", { workspaceId, tabId, id }).catch(chatFailure); +} + +/** Changes the text of a message a chat holds for after its turn. */ +export async function editHeldChat( + services: CliServices, + { workspaceId, tabId, id, text }: RpcParams<"chat.editHeld">, + device?: string, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + // Changed by the user there, it is theirs now: an agent's no longer. + await services + .ui("chat.editHeld", { workspaceId, tabId, id, text, ...(device ? { device } : {}) }) + .catch(chatFailure); + return {}; +} + +/** Takes back one of the user's messages a Claude chat has not read. */ +export async function withdrawChat( + services: CliServices, + { workspaceId, tabId, itemKey }: RpcParams<"chat.withdraw">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude"]); + return services.ui("chat.withdraw", { workspaceId, tabId, itemKey }).catch(chatFailure); +} + +/** Has a Claude chat read its unread messages now. */ +export async function hurryChat( + services: CliServices, + { workspaceId, tabId }: RpcParams<"chat.hurry">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude"]); + await services.ui("chat.hurry", { workspaceId, tabId }).catch(chatFailure); + return {}; +} + +/** Room an image's base64 may take in its answer: the frame, less the envelope and its other fields. */ +const IMAGE_CHARS = MAX_MESSAGE_BYTES - ENVELOPE_BYTES - 256; + +/** An image of the chat, drawn smaller where it would not go in a frame. */ +export async function chatImage( + services: CliServices, + { workspaceId, tabId, ref, maxEdge, versioned, ifVersion }: RpcParams<"chat.image">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const image = await services + .ui("chat.image", { + workspaceId, + tabId, + ref, + maxChars: IMAGE_CHARS, + ...(maxEdge !== undefined ? { maxEdge } : {}), + ...(versioned ? { versioned } : {}), + ...(ifVersion !== undefined ? { ifVersion } : {}), + }) + .catch((error: unknown) => { + const message = error instanceof Error ? error.message : String(error); + if (/no such image/.test(message)) throw new RpcFailure(RpcErrorCode.NotFound, message); + return unavailable(error); + }); + if (!fitsAsResult(image)) { + throw new RpcFailure(RpcErrorCode.Unavailable, "the image would not go in a frame"); + } + return image; +} + +/** Keeps an image for the device's next message to the chat. */ +export async function attachToChat( + services: CliServices, + { workspaceId, tabId, mediaType, data }: RpcParams<"chat.attach">, + device: string, + attachments: Attachments, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + return { attachmentId: attachments.take(device, workspaceId, tabId, { mediaType, data }) }; +} + +/** Starts a file's upload for the device's next message to the chat. */ +export async function beginUpload( + services: CliServices, + { workspaceId, tabId, name, size, mediaType }: RpcParams<"chat.upload.begin">, + device: string, + uploads: Uploads, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + return { + uploadId: uploads.begin(device, workspaceId, tabId, { + name, + size, + ...(mediaType ? { mediaType } : {}), + }), + }; +} + +export async function answerChat( + services: CliServices, + { + workspaceId, + tabId, + requestId, + decision, + message, + updatedInput, + stop, + always, + planMode, + }: RpcParams<"chat.answer">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + await services + .ui("chat.answer", { + workspaceId, + tabId, + decision, + expect: requestId, + ...(message !== undefined ? { message } : {}), + ...(updatedInput ? { updatedInput } : {}), + ...(stop ? { stop } : {}), + ...(decision === "allow" && always?.length ? { always } : {}), + ...(decision === "allow" && planMode ? { planMode } : {}), + }) + .catch(unavailable); + return {}; +} + +/** What a tab's choices are, as the protocol tells them; the modes named in this app's language. */ +export function settingsOf(choices: CliChatChoices): RpcResult<"chat.settings"> { + const set = CHAT_MODES[choices.agent]; + const t = tryGetMainT(); + return { + agent: choices.agent, + model: choices.model, + effort: choices.effort, + permissionMode: choices.mode, + options: { + models: choices.models.map((m) => ({ id: m.value, label: m.name })), + efforts: [...choices.efforts], + modes: set.modes.map((id) => ({ + id, + label: t ? String(t(`common:${set.prefix}.${id}`)) : id, + ...(id === set.dangerous ? { dangerous: true } : {}), + })), + }, + }; +} + +export async function chatSettings( + services: CliServices, + { workspaceId, tabId }: RpcParams<"chat.settings">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const choices = await services + .ui("chat.settings", { workspaceId, tabId, settings: {} }) + .catch(unavailable); + return settingsOf(choices); +} + +/** + * Sets what the user asked from another device, as the composer's chips + * do: the mode that lets everything run too, unless a policy keeps it off + * here (Forbidden, with why). + */ +export async function configureChat( + services: CliServices, + { workspaceId, tabId, model, effort, permissionMode }: RpcParams<"chat.configure">, + byAgent = false, +): Promise> { + const tab = await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const settings = { + ...(model !== undefined ? { model } : {}), + ...(effort !== undefined ? { effort } : {}), + ...(permissionMode !== undefined ? { mode: permissionMode } : {}), + }; + if (permissionMode !== undefined) { + const set = CHAT_MODES[tab.type as ChatAgent]; + if (chatModeNamed(tab.type as ChatAgent, permissionMode) === set.dangerous) { + const now = await services + .ui("chat.settings", { workspaceId, tabId, settings: {} }) + .catch(unavailable); + if (now.dangerousBlocked) { + throw new RpcFailure( + RpcErrorCode.Forbidden, + `${set.dangerous} is turned off on this computer: ${now.dangerousBlocked}`, + ); + } + } + } + const choices = await services + .ui( + "chat.settings", + // An agent never takes the mode that lets everything run. + { workspaceId, tabId, settings, ...(byAgent ? {} : { user: true }) }, + SETTINGS_TIMEOUT_MS, + ) + .catch(unavailable); + return settingsOf(choices); +} + +/** How long a change of settings may take: a model change restarts nothing, but waits to be shown. */ +const SETTINGS_TIMEOUT_MS = 60_000; + +export async function chatCommands( + services: CliServices, + { workspaceId, tabId }: RpcParams<"chat.commands">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + return services.ui("chat.commands", { workspaceId, tabId }).catch(unavailable); +} + +/** What an @ in the tab's composer may name, as its agent suggests it. */ +export async function chatFileSuggestions( + services: CliServices, + { workspaceId, tabId, query }: RpcParams<"chat.fileSuggestions">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const { paths } = await services + .ui("chat.fileSuggestions", { workspaceId, tabId, query }) + .catch(unavailable); + return { paths: paths.slice(0, MAX_FILE_SUGGESTIONS) }; +} + +/** + * Runs one of the app's commands the tab lists, as its composer runs it + * typed; one the agent runs goes with chat.send, and the model and effort + * with chat.configure. + */ +export async function runChatCommand( + services: CliServices, + { workspaceId, tabId, name, args: given }: RpcParams<"chat.command">, + system: FolderSystem = SYSTEM_FOLDERS, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + const bare = name.replace(/^\//, ""); + const lower = bare.toLowerCase(); + const { commands } = await services + .ui("chat.commands", { workspaceId, tabId }) + .catch(unavailable); + const command = + commands.find((c) => c.name.toLowerCase() === lower) ?? + commands.find((c) => c.aliases?.some((alias) => alias.toLowerCase() === lower)); + if (command?.runs !== "command") { + throw new RpcFailure( + RpcErrorCode.InvalidParams, + !command + ? `/${bare} is not a command this chat lists (chat.commands)` + : command.runs === "send" + ? `/${bare} is the agent's own: send it as a message (chat.send)` + : `/${bare} sets what chat.configure sets`, + ); + } + // A folder the chat may read, which the files methods then read too: by its + // real path, and not one that would open all of the disk. + const args = + command.name === "add-dir" && given?.trim() + ? await folderToAdd(await addDirPath(services, workspaceId, tabId, given, system), system) + : given; + return services + .ui( + "chat.command", + { workspaceId, tabId, name: command.name, ...(args ? { args } : {}) }, + SETTINGS_TIMEOUT_MS, + ) + .catch(unavailable); +} + +/** The folder /add-dir names, as the composer reads it: from the chat's folder, or the home's. */ +async function addDirPath( + services: CliServices, + workspaceId: string, + tabId: string, + given: string, + system: FolderSystem, +): Promise { + const p = system.platform === "win32" ? win32 : posix; + const path = given.trim().replace(/^(["'])(.*)\1$/, "$2"); + if (path === "~" || path.startsWith("~/") || path.startsWith("~\\")) { + return p.join(system.home(), path.slice(1)); + } + if (p.isAbsolute(path)) return path; + const info = await services + .ui("chat.state", { workspaceId, tabId, peek: true }) + .catch(() => null); + const cwd = info?.cwd ?? workspaceOf(services, workspaceId)?.rootPath ?? system.home(); + return p.resolve(cwd, path); +} + +/** + * Has a chat run with another profile, as the CLI's chat profile does, the + * user asking from another device: no one here is asked again. + */ +export async function changeChatProfile( + services: CliServices, + { workspaceId, tabId, profile, fresh, stop }: RpcParams<"chat.profile">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + return services + .ui( + "chat.profile", + { workspaceId, tabId, profile, fresh: fresh ?? false, ...(stop ? { stop } : {}) }, + SETTINGS_TIMEOUT_MS, + ) + .catch((error: unknown) => { + const message = error instanceof Error ? error.message : String(error); + if (/ has no profile /.test(message)) + throw new RpcFailure(RpcErrorCode.InvalidParams, message); + return unavailable(error); + }); +} + +export async function interruptChat( + services: CliServices, + { workspaceId, tabId }: RpcParams<"chat.interrupt">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + await services.ui("chat.interrupt", { workspaceId, tabId }).catch(unavailable); + return {}; +} + +/** Starts a chat's program again, as its banner does: the user's, from another device. */ +export async function restartChat( + services: CliServices, + { workspaceId, tabId }: RpcParams<"chat.restart">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + await services.ui("chat.restart", { workspaceId, tabId }).catch(chatFailure); + return {}; +} + +/** Asks a Codex chat's program again whether it is signed in, as its banner does: the user's. */ +export async function checkChatSignIn( + services: CliServices, + { workspaceId, tabId }: RpcParams<"chat.checkSignIn">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["codex"]); + return services.ui("chat.checkSignIn", { workspaceId, tabId }).catch(chatFailure); +} + +export async function newChat( + services: CliServices, + { + workspaceId, + agent, + prompt, + model, + effort, + permissionMode, + profile, + folder, + }: RpcParams<"chat.new">, + fs?: (workspaceId: string) => Promise, + relay?: RelaySender, +): Promise> { + const workspace = workspaceOf(services, workspaceId); + if (!workspace) throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); + const cwd = folder + ? await folderIn( + fs ? await fs(workspaceId).catch(unavailable) : null, + workspace.rootPath, + folder, + ) + : workspace.rootPath; + const settings = { + ...(model !== undefined ? { model } : {}), + ...(effort !== undefined ? { effort } : {}), + ...(permissionMode !== undefined ? { mode: permissionMode } : {}), + }; + const opened = await services + .ui( + "chat.open", + { + workspaceId, + agent, + cwd, + prompt: prompt ?? "", + // Behind what the window shows: whoever sits at this computer keeps their place. + placement: { background: true }, + ...(Object.keys(settings).length > 0 ? { settings } : {}), + ...(profile !== undefined ? { profile } : {}), + // An agent's prompt shows as its; its settings never the mode that lets everything run. + ...(relay ? { from: relay } : { user: true }), + }, + SETTINGS_TIMEOUT_MS, + ) + .catch((error: unknown) => { + const message = error instanceof Error ? error.message : String(error); + if (/ has no profile /.test(message)) + throw new RpcFailure(RpcErrorCode.InvalidParams, message); + return unavailable(error); + }); + return { tabId: opened.tabId, ...(opened.error ? { error: opened.error } : {}) }; +} + +/** The agent profiles a new chat may run with. */ +export async function listProfiles( + services: CliServices, + { agent }: RpcParams<"profiles.list">, +): Promise> { + return services.ui("chat.profiles", agent ? { agent } : {}).catch(unavailable); +} + +/** Renames the chat, as its tab's rename does. */ +export async function renameChat( + services: CliServices, + { workspaceId, tabId, title }: RpcParams<"chat.rename">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + if (!title.trim()) + throw new RpcFailure(RpcErrorCode.InvalidParams, "a title needs more than spaces"); + return services.ui("chat.rename", { workspaceId, tabId, title }).catch(unavailable); +} + +/** Stops one of the chat's runs, as its pane's stop button does. */ +export async function stopChatRun( + services: CliServices, + { workspaceId, tabId, runId }: RpcParams<"chat.stopRun">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + return { stopped: await services.claudeAgents.stop(workspaceId, tabId, runId) }; +} + +/** Stops one of the chat's tasks, as its panel's stop button does. */ +export async function stopChatTask( + services: CliServices, + { workspaceId, tabId, taskId }: RpcParams<"chat.stopTask">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + await services.ui("chat.stopTask", { workspaceId, tabId, taskId }).catch((error: unknown) => { + const message = error instanceof Error ? error.message : String(error); + if (/^no task /.test(message)) throw new RpcFailure(RpcErrorCode.NotFound, message); + return unavailable(error); + }); + return {}; +} + +/** A tab's failure as another device is told it: an item or a call it does not have is not found. */ +export function chatFailure(error: unknown): never { + const message = error instanceof Error ? error.message : String(error); + if (/^no (item|tool call|message|held message) /.test(message)) { + throw new RpcFailure(RpcErrorCode.NotFound, message); + } + if (/^a message needs /.test(message)) throw new RpcFailure(RpcErrorCode.InvalidParams, message); + return unavailable(error); +} + +/** Rewinds a chat to before one of the user's messages. */ +export async function rewindChat( + services: CliServices, + { workspaceId, tabId, itemKey, files }: RpcParams<"chat.rewind">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + await services.ui("chat.rewind", { workspaceId, tabId, itemKey, files }).catch(chatFailure); + return {}; +} + +/** Forks a chat into a new tab in the background. */ +export async function forkChat( + services: CliServices, + { workspaceId, tabId, itemKey }: RpcParams<"chat.fork">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + return services + .ui("chat.fork", { workspaceId, tabId, ...(itemKey ? { itemKey } : {}) }) + .catch(chatFailure); +} + +/** Sends a Claude chat's running command or subagent to the background. */ +export async function backgroundChatTool( + services: CliServices, + { workspaceId, tabId, toolUseId }: RpcParams<"chat.background">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude"]); + await services.ui("chat.background", { workspaceId, tabId, toolUseId }).catch(chatFailure); + return {}; +} + +/** Turns a Claude chat's Remote Control on or off. */ +export async function setChatRemoteControl( + services: CliServices, + { workspaceId, tabId, enabled }: RpcParams<"chat.remoteControl">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude"]); + await services.ui("chat.remoteControl", { workspaceId, tabId, enabled }).catch(chatFailure); + return {}; +} + +/** + * A Claude chat's Claude Code settings, or a Codex chat's defaults, read or + * changed as its dialog does: the user's, from another device. + */ +export async function workChatConfig( + services: CliServices, + params: RpcParams<"chat.config">, +): Promise> { + await tabOf(services, params.workspaceId, params.tabId, ["claude", "codex"]); + return services.ui("chat.config", params, SETTINGS_TIMEOUT_MS).catch(chatFailure); +} + +/** Works a Claude chat's MCP server, as its panel does: the user's, from another device. */ +export async function workChatMcp( + services: CliServices, + params: RpcParams<"chat.mcp">, +): Promise> { + await tabOf(services, params.workspaceId, params.tabId, ["claude"]); + return services.ui("chat.mcp", params, SETTINGS_TIMEOUT_MS).catch(chatFailure); +} + +/** Goes on with a conversation of the past in a new chat, behind what this computer shows. */ +export async function resumeChat( + services: CliServices, + { workspaceId, agent, sessionId, tabId }: RpcParams<"chat.resume">, +): Promise> { + if (!workspaceOf(services, workspaceId)) { + throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); + } + if (tabId) await tabOf(services, workspaceId, tabId, [agent]); + return services + .ui("chat.resume", { workspaceId, agent, sessionId, ...(tabId ? { tabId } : {}) }) + .catch((error: unknown) => { + const message = error instanceof Error ? error.message : String(error); + if (/^no conversation /.test(message)) throw new RpcFailure(RpcErrorCode.NotFound, message); + return unavailable(error); + }); +} + +export async function closeChat( + services: CliServices, + { workspaceId, tabId }: RpcParams<"chat.close">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["claude", "codex"]); + await services.ui("tab.close", { workspaceId, tabId, force: false }).catch(unavailable); + return {}; +} diff --git a/src/main/features/remote/serving/common.ts b/src/main/features/remote/serving/common.ts new file mode 100644 index 000000000..17dc1de66 --- /dev/null +++ b/src/main/features/remote/serving/common.ts @@ -0,0 +1,87 @@ +import { type Capability, RpcErrorCode } from "../../../../../packages/remote-protocol/src"; +import type { WorkspaceMeta } from "../../../../shared/types/workspace"; +import type { CliServices } from "../../cli/context"; +import { fitsInFrame, RpcFailure } from "../room"; + +/** What this version serves; a hello says so, and the others ask for nothing more. */ +export const SERVES: readonly Capability[] = [ + "sessions", + "chat.read", + "chat.control", + "terminal.read", + "terminal.control", + "files", + "files.write", + "git", + "notes", + "workspace.create", + "agents", +]; + +/** + * What this device serves as the user set it: its chats' controls, and + * its terminals', each only when the user lets other devices have them + * (Settings › Remote), else they may only look; agents' requests unless + * the user turned them down. + */ +export function servesWith( + allowControl: boolean, + allowTerminal = false, + allowAgents = false, +): readonly Capability[] { + return SERVES.filter( + (c) => + ((c !== "chat.control" && c !== "workspace.create") || allowControl) && + (c !== "files.write" || (allowControl && allowAgents)) && + (c !== "terminal.control" || allowTerminal) && + (c !== "agents" || allowAgents), + ); +} + +export function oneLine(text: string, max = 200): string { + const line = text.replace(/\s+/g, " ").trim(); + return line.length > max ? `${line.slice(0, max - 1)}…` : line; +} + +/** The tab, when the workspace holds one of that kind; else NotFound. */ +export async function tabOf( + services: CliServices, + workspaceId: string, + tabId: string, + kinds: readonly string[], +): Promise<{ id: string; type: string; title: string }> { + if (!workspaceOf(services, workspaceId)) { + throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); + } + const { tabs } = await services + .ui("tabs.list", { workspaceId }) + .catch(() => ({ tabs: [] as { id: string; type: string; title: string }[] })); + const tab = tabs.find((t) => t.id === tabId); + if (!tab || !kinds.includes(tab.type)) { + throw new RpcFailure(RpcErrorCode.NotFound, `no such ${kinds.join(" or ")} tab`); + } + return tab; +} + +/** A workspace the sessions' list shows. */ +export function workspaceOf(services: CliServices, workspaceId: string): WorkspaceMeta | undefined { + return services.listWorkspaces().find((w) => w.id === workspaceId); +} + +/** + * The app failing or refusing to do it as the session stands (nothing to + * stop, a request answered since), as another device is told it. + */ +export function unavailable(error: unknown): never { + throw new RpcFailure( + RpcErrorCode.Unavailable, + error instanceof Error ? error.message : String(error), + ); +} + +/** Room left in a frame for the answer's envelope around a result. */ +export const ENVELOPE_BYTES = 1024; + +export function fitsAsResult(result: unknown): boolean { + return fitsInFrame({ result, pad: "x".repeat(ENVELOPE_BYTES) }); +} diff --git a/src/main/features/remote/serving/files.ts b/src/main/features/remote/serving/files.ts new file mode 100644 index 000000000..4357b12c8 --- /dev/null +++ b/src/main/features/remote/serving/files.ts @@ -0,0 +1,274 @@ +import { stat } from "node:fs/promises"; +import { isDeepStrictEqual } from "node:util"; +import { + type AgentSender, + RpcErrorCode, + type RpcParams, +} from "../../../../../packages/remote-protocol/src"; +import type { CliServices } from "../../cli/context"; +import { isAgentBackedProvider } from "../../fs/bridge/provider"; +import { AgentFileWrites } from "../agent-file-writes"; +import type { AgentGate } from "../agent-gate"; +import type { ConsentControls } from "../consents"; +import { type FileWrites, fileWriteDestination, makeRemoteDirectory } from "../file-writes"; +import { + absoluteFilePath, + listFiles, + outsideFileSource, + type RemoteFiles, + readFile, +} from "../files"; +import { RpcFailure } from "../room"; +import { findFiles, searchFiles } from "../search"; +import { unavailable, workspaceOf } from "./common"; +import type { RemoteServeOptions, RemoteServeRequest } from "./types"; + +function checkFileControl( + anywhere: true | undefined, + by: AgentSender | undefined, + agents: AgentGate | undefined, + controls: ConsentControls | undefined, +): void { + if (!by || !agents) throw new RpcFailure(RpcErrorCode.Forbidden, "an agent sender is required"); + agents.checkRead(); + if (anywhere ? !controls?.terminal : !controls?.chat) { + throw new RpcFailure( + RpcErrorCode.Forbidden, + anywhere + ? "file access outside the workspace requires terminal control" + : "workspace file writes require chat control", + ); + } +} + +/** Validates and serves the start of a write or directory creation. */ +export async function serveFileDestination( + s: CliServices, + request: RemoteServeRequest, + watches: RemoteServeOptions, + fileWrites: FileWrites, + checkCurrent: () => void, + by: AgentSender | undefined, +) { + const { from, method } = request; + const write = request.params as RpcParams<"files.write.begin">; + const workspace = workspaceOf(s, write.workspaceId); + if (!workspace) throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); + const root = workspace.rootPath; + const location = structuredClone(workspace.location); + let approvedPath: string | undefined; + const authorize = (destination?: string) => { + checkCurrent(); + checkFileControl(write.anywhere, by, watches.agents, watches.controls?.()); + if (destination && approvedPath && destination !== approvedPath) { + throw new RpcFailure(RpcErrorCode.Forbidden, "the approved destination changed"); + } + const current = workspaceOf(s, write.workspaceId); + if (!current || current.rootPath !== root || !isDeepStrictEqual(current.location, location)) { + throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); + } + if (write.anywhere && location.kind !== "local") { + throw new RpcFailure( + RpcErrorCode.Unavailable, + "absolute file access requires a local workspace on the serving computer", + ); + } + }; + authorize(); + const directory = method === "files.mkdir"; + let agent: AgentFileWrites | undefined; + if (location.kind !== "local") { + const fs = await watches.files?.fs(write.workspaceId).catch(unavailable); + if (!fs || fs.kind !== location.kind || !isAgentBackedProvider(fs)) { + throw new RpcFailure( + RpcErrorCode.Unavailable, + "the workspace's filesystem agent is unavailable", + ); + } + agent = new AgentFileWrites(fs, root, write.path, directory); + } + approvedPath = await (agent?.resolve() ?? fileWriteDestination(root, write, directory)); + authorize(); + await watches.agents?.checkFile( + s, + { + device: { id: from, name: watches.deviceName?.(from) ?? from.slice(0, 8) }, + method, + params: write, + }, + { + path: approvedPath, + tree: directory && write.transferId !== undefined, + workspaceRoot: root, + }, + ); + authorize(); + if (directory) { + return agent ? agent.mkdir(approvedPath) : makeRemoteDirectory(root, write, authorize); + } + return fileWrites.begin( + from, + root, + write, + authorize, + agent && { writer: agent, path: approvedPath }, + ); +} + +/** Lists or reads a file with the request's workspace and access checks. */ +export async function serveFileRead( + s: CliServices, + request: RemoteServeRequest, + watches: RemoteServeOptions, + checkCurrent: () => void, + by: AgentSender | undefined, +) { + const { from, method } = request; + if (!watches.files) { + throw new RpcFailure(RpcErrorCode.MethodNotFound, `${request.method} is not served yet`); + } + const read = request.params as RpcParams<"files.list">; + const { workspaceId, anywhere } = read; + const workspace = workspaceOf(s, workspaceId); + if (!workspace) throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); + const rootPath = workspace.rootPath; + const location = structuredClone(workspace.location); + const checkWorkspace = () => { + const current = workspaceOf(s, workspaceId); + if ( + !current || + current.rootPath !== rootPath || + !isDeepStrictEqual(current.location, location) + ) { + throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); + } + }; + const authorizeAnywhere = () => { + if (anywhere) { + checkCurrent(); + checkFileControl(true, by, watches.agents, watches.controls?.()); + checkWorkspace(); + if (location.kind !== "local") { + throw new RpcFailure( + RpcErrorCode.Unavailable, + "absolute file access requires a local workspace on the serving computer", + ); + } + } + }; + const writeTarget = method === "files.list" && read.writeTarget === true; + if (writeTarget && (!anywhere || !read.transferId)) { + throw new RpcFailure( + RpcErrorCode.InvalidParams, + "a write target requires an absolute path and transfer ID", + ); + } + let approvedPath: string | undefined; + if (anywhere) { + authorizeAnywhere(); + const absolute = absoluteFilePath(read.path); + const source = await outsideFileSource(absolute) + .then(async (path) => ({ + path, + directory: method === "files.list" && (await stat(path)).isDirectory(), + })) + .catch((error: unknown) => ({ error })); + const target = + "path" in source + ? source.path + : writeTarget + ? await fileWriteDestination(rootPath, read, true) + : absolute; + await watches.agents?.checkFile( + s, + { + device: { id: from, name: watches.deviceName?.(from) ?? from.slice(0, 8) }, + method, + params: { ...read, from: by as AgentSender }, + }, + { + path: target, + tree: writeTarget || ("directory" in source && source.directory), + workspaceRoot: rootPath, + }, + ); + authorizeAnywhere(); + if ("error" in source) throw source.error; + approvedPath = source.path; + if (writeTarget) { + if (!source.directory) { + throw new RpcFailure( + RpcErrorCode.InvalidParams, + `${read.path} is a file: files.read reads it`, + ); + } + return { path: absolute, entries: [] }; + } + } + const files: RemoteFiles = { + ...watches.files, + folders: (id) => + s + .ui("workspace.folders", { workspaceId: id }) + .then((r) => r.folders) + .catch(() => []), + named: (id) => + s + .ui("chat.outsideFiles", { workspaceId: id }) + .then((r) => r.files) + .catch(() => []), + }; + if (request.method === "files.list") { + const result = await listFiles(files, workspace.rootPath, read, approvedPath); + authorizeAnywhere(); + return result; + } + const result = await readFile( + files, + rootPath, + request.params as RpcParams<"files.read">, + approvedPath, + ); + authorizeAnywhere(); + checkWorkspace(); + return result; +} + +/** Serves a search within the requesting device's concurrency allowance. */ +export async function serveFileSearch( + s: CliServices, + request: RemoteServeRequest, + watches: RemoteServeOptions, + searching: Map, +) { + const { from } = request; + if (!watches.files) { + throw new RpcFailure(RpcErrorCode.MethodNotFound, `${request.method} is not served yet`); + } + const { workspaceId } = request.params as { workspaceId: string }; + if (!workspaceOf(s, workspaceId)) { + throw new RpcFailure(RpcErrorCode.NotFound, "no such workspace"); + } + // Each runs through a workspace's files on its host: a device runs a few at once, no more. + const running = searching.get(from) ?? 0; + if (running >= MAX_SEARCHES_PER_DEVICE) { + throw new RpcFailure( + RpcErrorCode.Unavailable, + `that device has ${running} searches running here already: ask again once one ends`, + ); + } + searching.set(from, running + 1); + try { + const fs = await watches.files.fs(workspaceId).catch(unavailable); + return await (request.method === "files.find" + ? findFiles(fs, request.params as RpcParams<"files.find">) + : searchFiles(fs, request.params as RpcParams<"files.search">)); + } finally { + const left = (searching.get(from) ?? 1) - 1; + if (left > 0) searching.set(from, left); + else searching.delete(from); + } +} + +/** Most files.find and files.search one device may have running here at once. */ +export const MAX_SEARCHES_PER_DEVICE = 2; diff --git a/src/main/features/remote/serving/sessions.ts b/src/main/features/remote/serving/sessions.ts new file mode 100644 index 000000000..e4aa0e4af --- /dev/null +++ b/src/main/features/remote/serving/sessions.ts @@ -0,0 +1,198 @@ +import { + MAX_AGENT_DESCRIPTION, + MAX_SESSION_AGENTS, + type RemoteSession, + type RemoteSessionAgent, + type RemoteWorkspace, + type RpcResult, + type SessionState, +} from "../../../../../packages/remote-protocol/src"; +import type { AgentRun } from "../../../../shared/claude/activity"; +import type { CliChatState } from "../../../../shared/cli/ui"; +import { describeTerminal, isAgentSession } from "../../../../shared/sessions/agent-state"; +import type { WorkspaceMeta } from "../../../../shared/types/workspace"; +import { compareWorkspaceOrder } from "../../../../shared/workspace-order"; +import type { CliServices } from "../../cli/context"; +import { type ConsentControls, remoteConsents } from "../consents"; +import type { RemoteNotes } from "../notes"; +import { oneLine } from "./common"; + +/** A chat's state as the agent lists show it. */ +const CHAT_STATES: Record = { + working: "running", + asking: "needs-you", + idle: "idle", + error: "idle", +}; + +/** + * A terminal agent's state: Claude Code's from its hooks (idle, running, + * completed, needsInput, permissionPending, error), or another agent's + * from the terminal's activity (busy, completed, quiet). + */ +function terminalSessionState(state: string): SessionState { + if (state === "needsInput" || state === "permissionPending") return "needs-you"; + if (state === "running" || state === "busy") return "running"; + if (state === "completed") return "new-result"; + return "idle"; +} + +/** + * The workspaces as the sidebar lists them: roots, pinned first and each + * group in its own order, each followed by its task worktrees. + */ +function ordered(workspaces: readonly WorkspaceMeta[]): WorkspaceMeta[] { + const roots = workspaces.filter((w) => !w.task).sort(compareWorkspaceOrder); + const out: WorkspaceMeta[] = []; + for (const root of roots) + out.push(root, ...workspaces.filter((w) => w.task?.parentId === root.id)); + for (const w of workspaces) if (!out.includes(w)) out.push(w); + return out; +} + +/** + * The agents a session runs that still run, as the agent list shows them + * under its row: what each was asked to do on a line (a run told nothing + * more, its prompt's start); none when there are none. + */ +export function sessionAgents(agents: readonly AgentRun[]): RemoteSessionAgent[] | undefined { + const live = agents.filter((agent) => agent.end === undefined).slice(0, MAX_SESSION_AGENTS); + if (live.length === 0) return undefined; + return live.map((agent) => { + const said = agent.description ?? (agent.kind === "print" ? agent.prompt : undefined); + const description = said ? oneLine(said, MAX_AGENT_DESCRIPTION) : ""; + return { + id: agent.id, + kind: agent.kind, + ...(agent.program ? { program: agent.program } : {}), + ...(agent.type ? { type: oneLine(agent.type, 60) } : {}), + ...(description ? { description } : {}), + state: agent.state, + start: agent.start, + }; + }); +} + +function agentsField( + services: CliServices, + workspaceId: string, + tabId: string, +): { agents?: RemoteSessionAgent[] } { + const agents = sessionAgents(services.claudeAgents.list(workspaceId, tabId)); + return agents ? { agents } : {}; +} + +/** + * The workspaces and their sessions; a task's git facts from `git`, which + * looks at them now and then rather than now. + */ +export async function listSessions( + services: CliServices, + git?: WorkspaceGit, +): Promise> { + const workspaces = ordered(services.listWorkspaces()); + // Read Git facts with the workspace snapshot. Waiting for tabs first would + // let an old list recreate the cache entry for a workspace removed meanwhile. + const listed: RemoteWorkspace[] = workspaces.map((w) => { + const facts = w.task ? git?.(w) : undefined; + return { + id: w.id, + name: w.name, + path: w.rootPath, + ...(w.task ? { parentId: w.task.parentId } : {}), + ...(!w.task && w.pinned ? { pinned: true } : {}), + ...(facts ? { git: facts } : {}), + }; + }); + const sessions: RemoteSession[] = []; + const now = Date.now(); + for (const workspace of workspaces) { + let tabs: Awaited>>["tabs"]; + try { + tabs = (await services.ui("tabs.list", { workspaceId: workspace.id })).tabs; + } catch { + // A workspace the renderer does not hold (not opened yet): nothing to list. + continue; + } + for (const tab of tabs) { + if (tab.type === "claude" || tab.type === "codex") { + const info = await services + .ui("chat.state", { workspaceId: workspace.id, tabId: tab.id, peek: true }) + .catch(() => null); + if (!info) continue; + sessions.push({ + workspaceId: workspace.id, + tabId: tab.id, + kind: tab.type, + title: tab.title, + state: CHAT_STATES[info.state], + ...(info.since !== undefined ? { since: info.since } : {}), + ...(info.message ? { last: oneLine(info.message) } : {}), + ...(info.profile ? { profile: info.profile } : {}), + ...(info.cwd ? { cwd: info.cwd } : {}), + ...(info.state === "asking" && info.waiting + ? { waiting: { kind: info.waiting.kind, text: oneLine(info.waiting.text) } } + : {}), + ...agentsField(services, workspace.id, tab.id), + }); + } else if (tab.type === "terminal" && !tab.unstarted) { + // One not started has nothing to show another device until its workspace is opened here. + const claude = services.claudeStatus(workspace.id, tab.id); + const state = describeTerminal( + claude, + services.activity(workspace.id, tab.id), + tab.title, + now, + ); + // A Claude Code there asking to run a tool: what it asks, as the app holds it. + const ask = + claude?.status === "permissionPending" + ? services.claudeAsks(workspace.id, tab.id)[0] + : undefined; + sessions.push({ + workspaceId: workspace.id, + tabId: tab.id, + kind: "terminal", + title: tab.title, + state: isAgentSession(state) ? terminalSessionState(state.state) : "idle", + ...(isAgentSession(state) ? { agent: state.agent ?? "agent" } : {}), + ...(state.since !== undefined ? { since: state.since } : {}), + ...(state.message ? { last: oneLine(state.message) } : {}), + ...(ask + ? { + waiting: { + kind: "permission" as const, + text: oneLine(ask.target ? `${ask.tool} ${ask.target}` : ask.tool), + requestId: ask.id, + always: ask.always.length > 0, + subagent: ask.agentId !== undefined, + }, + } + : {}), + ...agentsField(services, workspace.id, tab.id), + }); + } + } + } + return { workspaces: listed, sessions }; +} + +/** Returns the workspace and session snapshot with the requester's consents. */ +export async function serveSessionsList( + s: CliServices, + git: WorkspaceGit | undefined, + notes: RemoteNotes | undefined, + from: string, + controls: ConsentControls, +) { + const list = await listSessions(s, git); + const recording = notes?.recording(); + return { + ...list, + consents: await remoteConsents(s, from, controls), + ...(recording ? { recording } : {}), + }; +} + +/** A task's git facts as last looked at; none for a workspace that is no task, or not looked at yet. */ +export type WorkspaceGit = (workspace: WorkspaceMeta) => RemoteWorkspace["git"] | undefined; diff --git a/src/main/features/remote/serving/terminal.ts b/src/main/features/remote/serving/terminal.ts new file mode 100644 index 000000000..a913565d0 --- /dev/null +++ b/src/main/features/remote/serving/terminal.ts @@ -0,0 +1,39 @@ +import type { RpcParams, RpcResult } from "../../../../../packages/remote-protocol/src"; +import type { CliServices } from "../../cli/context"; +import { fitsAsResult, tabOf, unavailable } from "./common"; + +/** How many lines of a terminal a read gives when it does not say. */ +export const DEFAULT_TERMINAL_LINES = 1000; + +export async function readTerminal( + services: CliServices, + { workspaceId, tabId, lines, ansi }: RpcParams<"terminal.read">, +): Promise> { + await tabOf(services, workspaceId, tabId, ["terminal"]); + const { text, caret } = await services + .ui("terminal.read", { + workspaceId, + tabId, + lines: lines ?? DEFAULT_TERMINAL_LINES, + ...(ansi ? { ansi } : {}), + }) + .catch(unavailable); + // The end of it, as much as goes in a frame, from a line's start: no escape cut in two. The + // cursor goes up with the lines cut; above them, it is not told. + let shown = text; + let line = caret?.line; + while (!fitsAsResult({ text: shown, caret })) { + const cut = shown.slice(Math.floor(shown.length / 4)); + const at = cut.indexOf("\n"); + const kept = at >= 0 ? cut.slice(at + 1) : cut; + // Lines cut whole take the cursor up with them; one cut in two loses it. + if (line !== undefined) { + line = + at >= 0 ? line - (shown.slice(0, shown.length - kept.length).split("\n").length - 1) : -1; + } + shown = kept; + } + return caret && line !== undefined && line >= 0 + ? { text: shown, caret: { line, column: caret.column } } + : { text: shown }; +} diff --git a/src/main/features/remote/serving/types.ts b/src/main/features/remote/serving/types.ts new file mode 100644 index 000000000..3cdacb35e --- /dev/null +++ b/src/main/features/remote/serving/types.ts @@ -0,0 +1,56 @@ +import type { RpcMethod } from "../../../../../packages/remote-protocol/src"; +import type { AgentGate } from "../agent-gate"; +import type { Attachments } from "../attachments"; +import type { ChatWatches } from "../chat-watches"; +import type { ConsentControls } from "../consents"; +import type { FileWrites } from "../file-writes"; +import type { RemoteFiles } from "../files"; +import type { FolderSystem } from "../folders"; +import type { RemoteGit } from "../git"; +import type { PhotoQuota, RemoteNotes } from "../notes"; +import type { SessionWatches } from "../session-watches"; +import type { TerminalWatches } from "../terminal-watches"; +import type { Uploads } from "../uploads"; + +/** Optional services available to the remote request router. */ +export interface RemoteServeOptions { + chats?: ChatWatches; + sessions?: SessionWatches; + terminals?: TerminalWatches; + /** The tasks' git facts the sessions' list tells. */ + git?: WorkspaceGit; + /** The images and files devices attached for their next messages. */ + attachments?: Attachments; + /** The files devices are sending a piece at a time; fresh ones when not given. */ + uploads?: Uploads; + fileWrites?: FileWrites; + /** The workspaces' file systems, and how an image is drawn smaller. */ + files?: Omit; + /** The workspaces' repositories, for git.status, git.diff, git.log and git.show. */ + repositories?: RemoteGit; + /** The workspaces' notes, for notes.list and notes.attach, and the note it records into. */ + notes?: RemoteNotes; + /** How many photos each device put into notes lately; a fresh one when not given. */ + photoQuota?: PhotoQuota; + /** Whether an agent on another device may do what it asks (a request with `from`). */ + agents?: AgentGate; + /** A device of the room by its id, as it names itself. */ + deviceName?: (id: string) => string | undefined; + /** The folders folders.list goes through; the system's when not given. */ + folders?: FolderSystem; + /** + * What the user lets other devices control here, for the consent prompts + * they may answer; none when not told. + */ + controls?: () => ConsentControls; +} + +/** The method, parameters and lifetime of a request from another device. */ +export interface RemoteServeRequest { + from: string; + method: RpcMethod; + params: unknown; + isCurrent?: () => boolean; +} + +import type { WorkspaceGit } from "./sessions"; diff --git a/src/main/features/remote/serving/workspaces.ts b/src/main/features/remote/serving/workspaces.ts new file mode 100644 index 000000000..089f195e2 --- /dev/null +++ b/src/main/features/remote/serving/workspaces.ts @@ -0,0 +1,31 @@ +import type { RpcParams, RpcResult } from "../../../../../packages/remote-protocol/src"; +import { tryGetMainT } from "../../../i18n"; +import type { CliServices } from "../../cli/context"; +import { type FolderSystem, folderToAdd, SYSTEM_FOLDERS } from "../folders"; +import { unavailable } from "./common"; + +/** + * Adds one of this device's folders as a workspace, as Add Workspace does; + * the user is told which device added it. + */ +export async function createWorkspace( + services: CliServices, + { path: asked, name }: RpcParams<"workspace.create">, + device: string | undefined, + system: FolderSystem = SYSTEM_FOLDERS, +): Promise> { + const rootPath = await folderToAdd(asked, system); + const { workspace, added } = await services + .addWorkspace({ kind: "local", rootPath }, name) + .catch(unavailable); + if (added) { + const t = tryGetMainT(); + const from = device ?? "another device"; + const title = t ? String(t("common:remoteNotice.workspaceAdded", { device: from })) : ""; + services.notify(workspace.id, "", title || `Workspace added from ${from}`, rootPath); + } + return { + workspace: { id: workspace.id, name: workspace.name, path: workspace.rootPath }, + created: added, + }; +} diff --git a/src/main/features/remote/session-watches.ts b/src/main/features/remote/session-watches.ts index e9695dfe9..2e7e36281 100644 --- a/src/main/features/remote/session-watches.ts +++ b/src/main/features/remote/session-watches.ts @@ -7,6 +7,7 @@ * change within a second. The consent prompts waiting on the user go with * it, as that watcher may answer them. */ + import { randomUUID } from "node:crypto"; import { type RemoteConsent, @@ -15,8 +16,11 @@ import { type RpcResult, type SessionsUpdate, } from "../../../../packages/remote-protocol/src"; +import { createLogger } from "../../../shared/log/main"; import { RpcFailure } from "./room"; +const log = createLogger("remote-session-watches"); + /** How often the list is made again while watched. */ export const SESSIONS_POLL_MS = 1000; @@ -114,7 +118,7 @@ export class SessionWatches { await this.deps.notify(watch.from, { ...list, watchId }); } } catch (error) { - console.warn("[remote] the sessions' list could not be made:", error); + log.warn(`Could not list sessions: ${String(error)}`); } finally { this.polling = false; } diff --git a/src/main/features/remote/task-git.ts b/src/main/features/remote/task-git.ts index 3f8f1881c..d1b3ee66c 100644 --- a/src/main/features/remote/task-git.ts +++ b/src/main/features/remote/task-git.ts @@ -52,7 +52,7 @@ export class TaskGit { return kept?.facts ?? undefined; }; - /** A workspace gone: its facts too. */ + /** Drops the entry; a pending lookup can only update its detached copy. */ forget(workspaceId: string): void { this.kept.delete(workspaceId); } diff --git a/src/main/features/remote/terminal-control.ts b/src/main/features/remote/terminal-control.ts index 6aed9f495..c53491383 100644 --- a/src/main/features/remote/terminal-control.ts +++ b/src/main/features/remote/terminal-control.ts @@ -15,7 +15,7 @@ import { keySequence } from "../cli/keys"; import type { FsProvider } from "../fs/bridge/provider"; import { folderIn } from "./files"; import { RpcFailure } from "./room"; -import { tabOf, unavailable, workspaceOf } from "./serve"; +import { tabOf, unavailable, workspaceOf } from "./serving/common"; /** * Types into the terminal: the text pasted (or as keystrokes, `typed`, a line break as the diff --git a/src/main/features/remote/terminal-watches.ts b/src/main/features/remote/terminal-watches.ts index 07ff4affa..d9f44991e 100644 --- a/src/main/features/remote/terminal-watches.ts +++ b/src/main/features/remote/terminal-watches.ts @@ -15,7 +15,8 @@ import { } from "../../../../packages/remote-protocol/src"; import type { CliServices } from "../cli/context"; import { fitsInFrame, RpcFailure } from "./room"; -import { DEFAULT_TERMINAL_LINES, fitsAsResult, tabOf, unavailable } from "./serve"; +import { fitsAsResult, tabOf, unavailable } from "./serving/common"; +import { DEFAULT_TERMINAL_LINES } from "./serving/terminal"; /** Most terminals one device may watch here at once; a new one beyond ends its oldest. */ export const MAX_TERMINAL_WATCHES_PER_DEVICE = 4; diff --git a/src/main/features/remote/ticket.ts b/src/main/features/remote/ticket.ts index 42a7791d0..19b94a727 100644 --- a/src/main/features/remote/ticket.ts +++ b/src/main/features/remote/ticket.ts @@ -16,11 +16,21 @@ export const TicketSchema = z.object({ ticket: z.string().min(1) }); export async function askTicket( account: Pick, body?: unknown, + signal?: AbortSignal, ): Promise { try { - const answer = await account.request("POST", `/relay/ticket?v=${RELAY_VERSION}`, body); + signal?.throwIfAborted(); + const answer = await account.request( + "POST", + `/relay/ticket?v=${RELAY_VERSION}`, + body, + undefined, + signal, + ); + signal?.throwIfAborted(); return TicketSchema.parse(answer).ticket; } catch (error) { + signal?.throwIfAborted(); if ( error instanceof ServerError && error.status === 400 && diff --git a/src/main/features/remote/voice.ts b/src/main/features/remote/voice.ts index ddb2fec22..3f54be790 100644 --- a/src/main/features/remote/voice.ts +++ b/src/main/features/remote/voice.ts @@ -38,6 +38,11 @@ import { type VoiceStartResult, VoiceTermsSchema, } from "../../../shared/remote/voice"; +import { + API_RESPONSE_MAX_BYTES, + RESPONSE_TOO_LARGE, + ResponseTooLargeError, +} from "../../infra/http/body"; import type { RemoteAccount } from "./account"; import { OutdatedError, ServerError, SignedOutError, UnreachableError } from "./errors"; import { askTicket } from "./ticket"; @@ -67,8 +72,10 @@ export interface VoiceSocket { /** The refused upgrade's response, as much of it as is read. */ export interface UpgradeRefusal { statusCode?: number; + destroy(): void; on(event: "data", listener: (chunk: Buffer) => void): unknown; on(event: "end", listener: () => void): unknown; + on(event: "error", listener: (error: Error) => void): unknown; } export interface VoiceSessionsDeps { @@ -165,7 +172,12 @@ export class VoiceSessions { if (error instanceof SignedOutError) return failed("signed-out", error.message); if (error instanceof OutdatedError) return failed("outdated", error.message); if (error instanceof UnreachableError) return failed("network", error.message); - if (error instanceof ServerError) return failed("failed", error.message); + if (error instanceof ServerError) { + return failed( + error.code === RESPONSE_TOO_LARGE ? RESPONSE_TOO_LARGE : "failed", + error.message, + ); + } throw error; } const url = account.route.url("/voice", { socket: true }); @@ -195,12 +207,26 @@ export class VoiceSessions { }; socket.on("unexpected-response", (_request, response) => { - let body = ""; + const decoder = new TextDecoder(); + const body: string[] = []; + let bytes = 0; response.on("data", (chunk) => { - body += chunk.toString("utf8"); + if (settled) return; + bytes += chunk.byteLength; + if (bytes > API_RESPONSE_MAX_BYTES) { + const error = new ResponseTooLargeError(API_RESPONSE_MAX_BYTES); + body.length = 0; + settle(failed(error.code, error.message)); + response.destroy(); + socket.terminate(); + return; + } + body.push(decoder.decode(chunk, { stream: true })); }); response.on("end", () => { - const code = errorOf(body); + if (settled) return; + body.push(decoder.decode()); + const code = errorOf(body.join("")); settle( failed( refusalOf(response.statusCode, code), @@ -209,6 +235,11 @@ export class VoiceSessions { ); socket.terminate(); }); + response.on("error", (error) => { + if (settled) return; + settle(failed("network", error.message)); + socket.terminate(); + }); }); socket.on("error", (error) => { // Not reached before it answered: the next try goes straight when it was the proxy. diff --git a/src/main/features/remote/waits.ts b/src/main/features/remote/waits.ts index 1764d70c4..b273f5db6 100644 --- a/src/main/features/remote/waits.ts +++ b/src/main/features/remote/waits.ts @@ -14,7 +14,7 @@ import { import type { CliServices } from "../cli/context"; import { DEFAULT_QUIET_MS, WAIT_POLL_MS, type WaitTarget, waitOnTerminal } from "../cli/wait"; import { RpcFailure } from "./room"; -import { fitsAsResult, unavailable, workspaceOf } from "./serve"; +import { fitsAsResult, unavailable, workspaceOf } from "./serving/common"; /** * After a chat.send, how long a chat that shows nothing newer than the @@ -55,11 +55,20 @@ export async function waitOnChat( } first = false; const info = await services - .ui("chat.state", { workspaceId, tabId, peek: true }) + .ui("chat.state", { + workspaceId, + tabId, + peek: true, + ...(after === undefined ? {} : { after }), + }) .catch(unavailable); const now = clock.now(); // A turn that has not shown the message yet is still to come. - const unseen = after !== undefined && (info.since ?? 0) < after && now - after < SEND_GRACE_MS; + const unseen = + !info.trackedSubmission && + after !== undefined && + (info.since ?? 0) < after && + now - after < SEND_GRACE_MS; if (info.state !== "working" && !unseen) { const result = { done: true, state: info.state }; return info.message ? { ...result, message: fitting(info.message, result) } : result; diff --git a/src/main/features/search/index.ts b/src/main/features/search/index.ts index ba4dbc021..ac3351dd9 100644 --- a/src/main/features/search/index.ts +++ b/src/main/features/search/index.ts @@ -1,4 +1,3 @@ -import { randomUUID } from "node:crypto"; import { createAbortError } from "../../../shared/abort"; import type { InferArgs, @@ -6,18 +5,12 @@ import type { InferProgress, ipcContract, } from "../../../shared/ipc/contract"; -import { - AgentSearchCompleteSchema, - AgentSearchProgressPayloadSchema, - SEARCH_CANCEL_METHOD, - SEARCH_PROGRESS_EVENT, - SEARCH_TEXT_METHOD, -} from "../../../shared/search/protocol"; import type { FileMatch } from "../../../shared/search/types"; import type { StreamContext } from "../../infra/ipc-router"; import { isAgentBackedProvider } from "../fs/bridge/provider"; import { findWorkspace } from "../workspace/guards"; import type { WorkspaceManager } from "../workspace/manager"; +import { createAgentSearchSession } from "./session"; type SearchTextStreamProcedure = (typeof ipcContract)["fs"]["stream"]["searchText"]; type SearchTextArgs = InferArgs; @@ -63,41 +56,29 @@ export function searchTextStream(manager: WorkspaceManager): SearchTextStreamHan throw new Error("workspace agent provider is not available"); } - const searchId = randomUUID(); const queue = new AsyncBatchQueue(); - // True once the agent answered the search request. Any other exit (abort, - // a local request timeout, the consumer stopping early) leaves the - // search running on the agent, so it must be cancelled there. - let answered = false; - const unsubscribe = provider.onAgentEvent(SEARCH_PROGRESS_EVENT, (payload) => { - const parsed = AgentSearchProgressPayloadSchema.safeParse(payload); - if (!parsed.success || parsed.data.searchId !== searchId) return; - queue.push(parsed.data.batch); - }); - - const complete = provider - .callAgentMethod(SEARCH_TEXT_METHOD, { searchId, query }) - .then((result) => { - answered = true; - return AgentSearchCompleteSchema.parse(result); - }) - .catch((error) => { - throw normalizeSearchError(error); - }) - .finally(() => { - queue.close(); - }); - complete.catch(() => {}); - + const session = createAgentSearchSession(provider, (batch) => queue.push(batch)); const abort = (): void => { - void provider.callAgentMethod(SEARCH_CANCEL_METHOD, { searchId }).catch(() => {}); queue.fail(createAbortError()); + session.cancel(); }; ctx.signal.addEventListener("abort", abort, { once: true }); try { + if (ctx.signal.aborted) throw createAbortError(); + const complete = session + .start(query) + .catch((error) => { + throw normalizeSearchError(error); + }) + .finally(() => { + queue.close(); + }); + complete.catch(() => {}); + for (;;) { const next = await queue.next(); + if (ctx.signal.aborted) throw createAbortError(); if (next.done) { return await complete; } @@ -105,10 +86,7 @@ export function searchTextStream(manager: WorkspaceManager): SearchTextStreamHan } } finally { ctx.signal.removeEventListener("abort", abort); - unsubscribe(); - if (!answered) { - void provider.callAgentMethod(SEARCH_CANCEL_METHOD, { searchId }).catch(() => {}); - } + session.dispose(); } }; } diff --git a/src/main/features/search/session.ts b/src/main/features/search/session.ts new file mode 100644 index 000000000..b57b122dd --- /dev/null +++ b/src/main/features/search/session.ts @@ -0,0 +1,54 @@ +import { randomUUID } from "node:crypto"; +import { + AgentSearchCompleteSchema, + AgentSearchProgressPayloadSchema, + SEARCH_CANCEL_METHOD, + SEARCH_PROGRESS_EVENT, + SEARCH_TEXT_METHOD, +} from "../../../shared/search/protocol"; +import type { FileMatch, TextSearchQuery } from "../../../shared/search/types"; +import type { AgentCallOptions } from "../../infra/agent/channel"; +import type { AgentBackedProvider } from "../fs/bridge/provider"; + +/** Owns the ID, progress listener and cancellation for one agent text search. */ +export function createAgentSearchSession( + provider: AgentBackedProvider, + onBatch: (batch: FileMatch[]) => void, +) { + const searchId = randomUUID(); + let started = false; + let answered = false; + let cancelSent = false; + const unsubscribe = provider.onAgentEvent(SEARCH_PROGRESS_EVENT, (payload) => { + const parsed = AgentSearchProgressPayloadSchema.safeParse(payload); + if (parsed.success && parsed.data.searchId === searchId) onBatch(parsed.data.batch); + }); + + /** Cancels a search that still owns work on the agent. */ + function cancel(): void { + if (!started || answered || cancelSent) return; + cancelSent = true; + void provider.callAgentMethod(SEARCH_CANCEL_METHOD, { searchId }).catch(() => {}); + } + + return { + /** Sends the search after its progress listener is ready. */ + start(query: TextSearchQuery, options?: AgentCallOptions) { + started = true; + const params = { searchId, query }; + const complete = options + ? provider.callAgentMethod(SEARCH_TEXT_METHOD, params, options) + : provider.callAgentMethod(SEARCH_TEXT_METHOD, params); + return complete.then((result) => { + answered = true; + return AgentSearchCompleteSchema.parse(result); + }); + }, + cancel, + /** Leaves the progress stream and releases any unfinished agent work. */ + dispose(): void { + unsubscribe(); + cancel(); + }, + }; +} diff --git a/src/main/infra/agent/ssh/auth-prompt.ts b/src/main/features/ssh/auth-prompt.ts similarity index 75% rename from src/main/infra/agent/ssh/auth-prompt.ts rename to src/main/features/ssh/auth-prompt.ts index 668314efe..2568adfc6 100644 --- a/src/main/infra/agent/ssh/auth-prompt.ts +++ b/src/main/features/ssh/auth-prompt.ts @@ -4,8 +4,11 @@ import { SshAuthPromptSchema, SshAuthRespondArgsSchema, type SshAuthResponse, -} from "../../../../shared/ssh/auth-prompt"; -import { register, validateArgs } from "../../ipc-router"; +} from "../../../shared/ssh/auth-prompt"; +import { AuthCancelledError } from "../../infra/agent/ssh/auth"; +import { register, validateArgs } from "../../infra/ipc-router"; + +export { AuthCancelledError } from "../../infra/agent/ssh/auth"; type BroadcastFn = (channelName: string, event: string, args: unknown) => void; @@ -16,14 +19,6 @@ interface PendingSshAuthPrompt { readonly reject: (error: AuthCancelledError) => void; } -/** Error raised when the renderer cancels an active SSH authentication prompt. */ -export class AuthCancelledError extends Error { - constructor(promptId: string) { - super(`SSH authentication prompt cancelled: ${promptId}`); - this.name = "AuthCancelledError"; - } -} - /** * Coordinates main-initiated SSH authentication prompts with renderer replies. * Requests are keyed by `promptId`, so unrelated IPC request ids cannot resolve @@ -38,18 +33,31 @@ export class SshAuthPromptHub { } /** Broadcasts a validated prompt and waits for the renderer response. */ - request(prompt: SshAuthPrompt): Promise { + request(prompt: SshAuthPrompt, signal?: AbortSignal): Promise { + if (signal?.aborted) return Promise.reject(signal.reason); const parsed = SshAuthPromptSchema.parse(prompt); if (this.pending.has(parsed.promptId)) { throw new Error(`Duplicate SSH authentication prompt id: ${parsed.promptId}`); } return new Promise((resolve, reject) => { + const abort = () => { + this.pending.delete(parsed.promptId); + reject(signal?.reason); + this.broadcast("sshAuth", "dismissed", { promptId: parsed.promptId }); + }; + signal?.addEventListener("abort", abort, { once: true }); this.pending.set(parsed.promptId, { promptId: parsed.promptId, prompt: parsed, - resolve, - reject, + resolve: (response) => { + signal?.removeEventListener("abort", abort); + resolve(response); + }, + reject: (error) => { + signal?.removeEventListener("abort", abort); + reject(error); + }, }); this.broadcast("sshAuth", "prompt", parsed); }); @@ -94,6 +102,6 @@ export function registerSshAuthPromptIpcChannels(hub: SshAuthPromptHub): void { cancel: (args) => hub.cancel(args), pending: () => hub.listPending(), }, - listen: { prompt: {} }, + listen: { prompt: {}, dismissed: {} }, }); } diff --git a/src/main/features/ssh/browse-session-registry.ts b/src/main/features/ssh/browse-session-registry.ts index 14091956f..0b492731e 100644 --- a/src/main/features/ssh/browse-session-registry.ts +++ b/src/main/features/ssh/browse-session-registry.ts @@ -110,16 +110,15 @@ export class SshBrowseSessionRegistry { disposeSession(session); } - /** - * Closes all open sessions and stops the reaper timer. Call on app shutdown - * or when the renderer window is destroyed. - */ + /** Closes the current sessions when all windows have gone. */ + closeAll(): void { + for (const sessionId of this.sessions.keys()) this.close(sessionId); + } + + /** Closes the sessions and stops the reaper when the app quits. */ dispose(): void { clearInterval(this.reaperTimer); - for (const session of this.sessions.values()) { - disposeSession(session); - } - this.sessions.clear(); + this.closeAll(); } /** diff --git a/src/main/features/ssh/ipc.ts b/src/main/features/ssh/ipc.ts index 79d0bb545..38b44cee8 100644 --- a/src/main/features/ssh/ipc.ts +++ b/src/main/features/ssh/ipc.ts @@ -1,14 +1,12 @@ import { readFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; +import { FS_ERROR } from "../../../shared/fs/errors"; import type { DirEntry } from "../../../shared/fs/types"; import { DirEntrySchema } from "../../../shared/fs/types"; import { ipcContract } from "../../../shared/ipc/contract"; -import { ipcErr, ipcOk } from "../../../shared/ipc/result"; -import { createLogger } from "../../../shared/log/main"; -import type { SshErrorCode } from "../../../shared/ssh/errors"; -import { SshErrorCodeSchema } from "../../../shared/ssh/errors"; -import { AuthCancelledError } from "../../infra/agent/ssh/auth-prompt"; +import { type IpcResult, ipcErr, ipcOk } from "../../../shared/ipc/result"; +import { messageForSshErrorCode } from "../../infra/agent/pipe"; import type { SshAuthPromptHandler } from "../../infra/agent/ssh/auth-pty"; import { createSshChannel } from "../../infra/agent/ssh/channel"; import type { SshControlMaster } from "../../infra/agent/ssh/master"; @@ -23,11 +21,11 @@ import { type SshTarget, } from "../../infra/agent/ssh/ssh2/config"; import { register, validateArgs } from "../../infra/ipc-router"; +import { agentCallError } from "../../infra/ipc-router/agent-errors"; import { BROWSE_MAX_ENTRIES, type SshBrowseSessionRegistry } from "./browse-session-registry"; import { parseSshConfig, type SshConfigHost } from "./config"; const c = ipcContract.ssh.call; -const log = createLogger("ssh"); /** The host fields a browse session connects with. */ type SshUserTarget = Pick; @@ -279,7 +277,7 @@ export function openBrowseSessionHandler( channel?.dispose(); // If bootstrap succeeded but channel setup failed, dispose bootstrap. bootstrapResult?.dispose?.(); - throw mapToBrowseError(error); + throw error; } }; } @@ -302,21 +300,15 @@ export function openBrowseSessionResultHandler( options: EnsureRemoteAgentOptions, onProgress?: (event: LspBootstrapProgressEvent) => void, ) => ReturnType, -): (args: unknown) => Promise | ReturnType> { +): (args: unknown) => Promise> { const inner = openBrowseSessionHandler(registry, promptHandler, broadcast, bootstrap); return async (args: unknown) => { try { const result = await inner(args); return ipcOk(result); } catch (error) { - if (isAuthCancellation(error)) { - return ipcErr("cancelled", "SSH authentication cancelled"); - } - const code = sshErrorCodeFromError(error); - if (code) { - return ipcErr("auth-failed", messageForSshErrorCode(code), { code }); - } - // Unexpected bug — rethrow so the router logs it. + const expected = agentCallError(error, "auth-failed"); + if (expected) return expected; throw error; } }; @@ -353,30 +345,44 @@ function buildMasterHandle( */ export function browseSessionHandler( registry: SshBrowseSessionRegistry, -): (args: unknown) => Promise<{ entries: DirEntry[]; truncated: boolean }> { - return async (args: unknown): Promise<{ entries: DirEntry[]; truncated: boolean }> => { +): (args: unknown) => Promise> { + return async (args: unknown) => { const { sessionId, path: dirPath } = validateArgs(c.browseSession.args, args); const session = registry.get(sessionId); if (!session) { - throw createSshErrorObject("ssh.session-expired"); + const code = "ssh.session-expired"; + return ipcErr("session-expired", messageForSshErrorCode(code), { + category: "failed", + domain: "ssh", + code, + }); } let rawEntries: unknown; try { rawEntries = await session.channel.call("fs.readdir", { relPath: dirPath }); } catch (error) { - throw mapToBrowseError(error); + if (error instanceof Error && "code" in error && error.code === FS_ERROR.NOT_FOUND) { + const code = "ssh.path-not-found"; + return ipcErr("not-found", messageForSshErrorCode(code), { + category: "failed", + domain: "ssh", + code, + }); + } + const expected = agentCallError(error); + if (expected) return expected; + throw error; } // Parse the raw response — the agent returns an array of DirEntry objects. - const parseResult = DirEntrySchema.array().safeParse(rawEntries); - const allEntries: DirEntry[] = parseResult.success ? parseResult.data : []; + const allEntries = DirEntrySchema.array().parse(rawEntries); const truncated = allEntries.length > BROWSE_MAX_ENTRIES; const entries = truncated ? allEntries.slice(0, BROWSE_MAX_ENTRIES) : allEntries; - return { entries, truncated }; + return ipcOk({ entries, truncated }); }; } @@ -388,12 +394,11 @@ export function browseSessionHandler( * Builds the closeBrowseSession IPC handler. Idempotent — safe to call on * an already-closed or unknown session. */ -export function closeBrowseSessionHandler( - registry: SshBrowseSessionRegistry, -): (args: unknown) => void { - return (args: unknown): void => { +export function closeBrowseSessionHandler(registry: SshBrowseSessionRegistry) { + return (args: unknown) => { const { sessionId } = validateArgs(c.closeBrowseSession.args, args); registry.close(sessionId); + return ipcOk(undefined); }; } @@ -418,94 +423,3 @@ function closeBrowseSessionStub(): (args: unknown) => void { throw new Error("SSH browse session handler not registered"); }; } - -// --------------------------------------------------------------------------- -// Error helpers -// --------------------------------------------------------------------------- - -/** - * Walks the error cause chain to detect AuthCancelledError at any depth. - * User cancellation is a normal control-flow outcome, not a failure, so the - * caller must treat it differently from genuine authentication failures. - */ -function isAuthCancellation(error: unknown): boolean { - if (error instanceof AuthCancelledError) return true; - if (error instanceof Error && error.cause !== undefined) { - return isAuthCancellation(error.cause); - } - return false; -} - -/** - * Maps arbitrary errors from the agent channel or bootstrap to a typed SSH - * error with a sanitized message. Raw stderr is never forwarded. - * - * User-initiated cancellation (AuthCancelledError anywhere in the cause chain) - * is returned as ssh.auth-cancelled — no console.error, since it is expected. - * Truly unmapped errors are still logged for diagnosability. - */ -function mapToBrowseError(error: unknown): Error { - if (isAuthCancellation(error)) { - // Cancellation is a normal outcome — no noise in the console. - return createSshErrorObject("ssh.auth-cancelled"); - } - const code = sshErrorCodeFromError(error) ?? "ssh.unknown"; - if (code === "ssh.unknown") { - // The renderer only ever sees the sanitized code. Log the raw cause to - // the main-process console so an unmapped failure is still diagnosable. - log.error(`unmapped browse-session error: ${(error as Error).message}`); - } - return createSshErrorObject(code); -} - -function createSshErrorObject(code: SshErrorCode): Error & { code: SshErrorCode } { - // The `.code` property does not survive Electron's IPC error serialization - // (only name/message/stack cross the boundary), so we also embed the code in - // the message text. The renderer classifies the failure by matching the code - // substring in error.message (see humanizeSshError / extractSshErrorKind). - const err = new Error(`${messageForSshErrorCode(code)} [${code}]`) as Error & { - code: SshErrorCode; - }; - err.code = code; - return err; -} - -function sshErrorCodeFromError(error: unknown): SshErrorCode | undefined { - if (typeof error !== "object" || error === null || !("code" in error)) { - return undefined; - } - const rawCode = (error as { code?: unknown }).code; - // Agent filesystem failures surface domain codes (NOT_FOUND, …) that are not - // SSH codes. Map the ones with dedicated UX so a missing path is reported as - // "path not found" instead of the generic ssh.unknown. - if (rawCode === "NOT_FOUND") return "ssh.path-not-found"; - const parsed = SshErrorCodeSchema.safeParse(rawCode); - return parsed.success ? parsed.data : undefined; -} - -function messageForSshErrorCode(code: SshErrorCode): string { - switch (code) { - case "ssh.connect-failed": - return "SSH connection failed"; - case "ssh.auth-failed": - return "SSH authentication failed"; - case "ssh.auth-cancelled": - return "SSH authentication cancelled"; - case "ssh.session-expired": - return "SSH browse session expired"; - case "ssh.path-not-found": - return "Remote path not found"; - case "server.spawn-failed": - return "Remote agent failed to start"; - case "server.protocol-error": - return "Remote agent protocol error"; - case "server.protocol-version-mismatch": - return "Remote agent protocol version mismatch"; - case "ssh.unknown": - return "SSH workspace validation failed"; - case "transport.unknown": - return "Agent transport failed"; - case "transport.timeout": - return "Agent request timed out"; - } -} diff --git a/src/main/features/tasks/service.ts b/src/main/features/tasks/service.ts index 1eb85da42..51c7e0a7b 100644 --- a/src/main/features/tasks/service.ts +++ b/src/main/features/tasks/service.ts @@ -140,7 +140,7 @@ export async function removeTask( throw new TaskError("worktree-dirty", `${task.worktreePath} has changes`); } - deps.manager.remove(id); + await deps.manager.remove(id); if (!repo) return; // Read before the worktree goes: the branch may have been renamed there. diff --git a/src/main/features/updates/index.ts b/src/main/features/updates/index.ts index 235a8d67f..59ce098b7 100644 --- a/src/main/features/updates/index.ts +++ b/src/main/features/updates/index.ts @@ -42,7 +42,7 @@ import { shell } from "electron"; import { ipcContract } from "../../../shared/ipc/contract"; -import { ipcOk } from "../../../shared/ipc/result"; +import { ipcErr, ipcOk } from "../../../shared/ipc/result"; import { createLogger } from "../../../shared/log/main"; import { isExternalSchemeAllowed } from "../../../shared/security/url-scheme"; import { @@ -114,6 +114,18 @@ const logger = createLogger("updates"); const c = ipcContract.updates.call; +/** Completes the IPC reply when the system has accepted or rejected the link. */ +async function openUpdateLink(url: string) { + try { + await shell.openExternal(url); + return ipcOk(undefined); + } catch { + // Native shell errors may include the full URL, including credentials. + logger.warn("Could not open link to updates"); + return ipcErr("open-external-failed", "Could not open link to updates"); + } +} + /** * Installs the updates domain: registers IPC handlers and returns a handle * whose `startAutoChecks()` should be called inside `app.whenReady()`. @@ -360,19 +372,17 @@ export function installUpdatesDomain(options: InstallUpdatesDomainOptions): Upda openReleasePage: (args: unknown) => { const { url } = validateArgs(c.openReleasePage.args, args); - if (isExternalSchemeAllowed(url)) { - void shell.openExternal(url); - } else { - logger.warn(`updates: blocked openReleasePage with disallowed scheme: ${url}`); + if (!isExternalSchemeAllowed(url)) { + logger.warn("Blocked update link with disallowed scheme"); + return ipcErr("invalid-args", "Unsupported update link"); } - return ipcOk(undefined); + return openUpdateLink(url); }, getStatus: () => ipcOk({ status: currentStatus, progress: currentProgress }), openStorePage: () => { - if (process.windowsStore) void shell.openExternal(STORE_PAGE_URL); - return ipcOk(undefined); + return process.windowsStore ? openUpdateLink(STORE_PAGE_URL) : ipcOk(undefined); }, releaseNotes: async () => { diff --git a/src/main/features/updates/poller.ts b/src/main/features/updates/poller.ts index 9fca4c058..b03278763 100644 --- a/src/main/features/updates/poller.ts +++ b/src/main/features/updates/poller.ts @@ -28,13 +28,19 @@ */ import { gt, rcompare, valid } from "semver"; +import { ResponseTooLargeError, readResponseText } from "../../infra/http/body"; // --------------------------------------------------------------------------- // Public types // --------------------------------------------------------------------------- /** Why an update check failed; the renderer words the user's message from it. */ -export type PollFailureReason = "rate-limited" | "http" | "network" | "unexpected"; +export type PollFailureReason = + | "rate-limited" + | "http" + | "network" + | "unexpected" + | "response-too-large"; export type PollResult = | { kind: "newer"; current: string; latest: string; releaseUrl: string } @@ -162,7 +168,7 @@ export async function pollGithubReleases(options: PollGithubReleasesOptions): Pr } else if (!response.ok) { return httpError(response.status); } else { - const text = await response.text(); + const text = await readResponseText(response); resolved = channel === "stable" ? parseLatestYml(text, REPO_BASE) : parseAtomFeed(text); if (cache !== undefined) { @@ -172,6 +178,9 @@ export async function pollGithubReleases(options: PollGithubReleasesOptions): Pr } } } catch (err) { + if (err instanceof ResponseTooLargeError) { + return { kind: "error", reason: err.code, message: err.message }; + } const message = err instanceof Error ? err.message : String(err); return { kind: "error", reason: "network", message: `Network error: ${message}` }; } diff --git a/src/main/features/updates/release-notes.ts b/src/main/features/updates/release-notes.ts index 77e5b0e06..196f4b04b 100644 --- a/src/main/features/updates/release-notes.ts +++ b/src/main/features/updates/release-notes.ts @@ -9,10 +9,12 @@ import { prerelease, rcompare, valid } from "semver"; import type { ReleaseNote, ReleaseNotesResult } from "../../../shared/updates/release-notes"; +import { ResponseTooLargeError, readResponseText } from "../../infra/http/body"; import { REPO_BASE } from "./poller"; const FEED_URL = `${REPO_BASE}/releases.atom`; const TIMEOUT_MS = 15_000; +const RELEASE_NOTES_MAX_BYTES = 1024 * 1024; /** The last feed read, to ask GitHub whether it changed since. */ export interface ReleaseNotesCache { @@ -50,14 +52,17 @@ export async function fetchReleaseNotes( } else if (!response.ok) { return { kind: "error", reason: "http" }; } else { - notes = parseReleaseNotes(await response.text()); + notes = parseReleaseNotes(await readResponseText(response, RELEASE_NOTES_MAX_BYTES)); if (cache) { cache.etag = response.headers.get("etag"); cache.notes = notes; } } - } catch { - return { kind: "error", reason: "network" }; + } catch (error) { + return { + kind: "error", + reason: error instanceof ResponseTooLargeError ? error.code : "network", + }; } return { kind: "ok", diff --git a/src/main/features/window/close-persistence.ts b/src/main/features/window/close-persistence.ts new file mode 100644 index 000000000..54887128a --- /dev/null +++ b/src/main/features/window/close-persistence.ts @@ -0,0 +1,127 @@ +import { PendingRequestMap } from "../../../shared/ipc/pending-request-map"; +import type { TimerScheduler } from "../../../shared/util/timer-scheduler"; +import { MAIN_QUIT_GRACE_MS } from "../../infra/main-shutdown"; + +/** How long close waits for a renderer to finish its durable layout write. */ +const CLOSE_FLUSH_TIMEOUT_MS = 1_000; + +interface CloseWindow { + readonly webContents: { + readonly id: number; + send(channel: string, ...args: unknown[]): void; + }; + isDestroyed(): boolean; + close(): void; +} + +interface CloseEvent { + preventDefault(): void; +} + +/** Coordinates a bounded layout flush before Electron runs its unload guards. */ +export class ClosePersistence { + private readonly answers: PendingRequestMap; + private readonly windowFlushes = new Map>(); + private readonly requestedCloses = new Set(); + private replayingClose: CloseWindow | null = null; + private quitPhase: "idle" | "flushing" | "closing" = "idle"; + private startedQuitAt?: number; + private nextAttempt = 1; + + constructor( + private readonly warn: (message: string) => void, + scheduler?: TimerScheduler, + private readonly now: () => number = () => performance.now(), + ) { + this.answers = new PendingRequestMap(scheduler); + } + + /** Only the window that received this attempt can complete it. */ + acknowledge(senderId: number, attempt: number, saved: boolean): void { + this.answers.resolve(`${senderId}:${attempt}`, saved); + } + + /** Pause an individual close, then replay it through the native unload guard. */ + interceptClose(event: CloseEvent, win: CloseWindow): void { + if (this.replayingClose === win || this.quitPhase === "closing") return; + event.preventDefault(); + if (this.quitPhase === "flushing" || this.requestedCloses.has(win)) return; + + this.requestedCloses.add(win); + void this.flushWindow(win).then(() => { + this.requestedCloses.delete(win); + if (win.isDestroyed() || this.quitPhase !== "idle") return; + this.replayingClose = win; + try { + win.close(); + } finally { + this.replayingClose = null; + } + }); + } + + /** + * A prevented BrowserWindow close also cancels app.quit(). Flush all app + * windows first, then let one normal Electron quit run its close events. + */ + interceptQuit(event: CloseEvent, windows: CloseWindow[], quit: () => void): void { + this.startedQuitAt ??= this.now(); + if (this.quitPhase === "closing" || windows.length === 0) return; + event.preventDefault(); + if (this.quitPhase === "flushing") return; + + this.quitPhase = "flushing"; + void Promise.all(windows.map((win) => this.flushWindow(win))).then(() => { + this.quitPhase = "closing"; + quit(); + }); + } + + /** The native updater owns its window close sequence. */ + allowUpdaterClose(): void { + this.quitPhase = "closing"; + } + + /** A cancelled unload leaves subsequent close attempts eligible to flush again. */ + cancelQuit(): void { + if (this.quitPhase === "closing") { + this.quitPhase = "idle"; + this.startedQuitAt = undefined; + } + } + + /** Counts the time spent preparing renderer closes toward the same quit deadline. */ + remainingQuitMs(): number { + const elapsed = this.startedQuitAt === undefined ? 0 : this.now() - this.startedQuitAt; + return Math.max(0, MAIN_QUIT_GRACE_MS - elapsed); + } + + /** Request at most one flush per window while a close or quit is pending. */ + private flushWindow(win: CloseWindow): Promise { + const senderId = win.webContents.id; + const current = this.windowFlushes.get(senderId); + if (current) return current; + + const attempt = this.nextAttempt++; + const key = `${senderId}:${attempt}`; + const answer = this.answers.register({ key, timeoutMs: CLOSE_FLUSH_TIMEOUT_MS }); + try { + win.webContents.send("ipc:event", "window", "prepareClose", { attempt }); + } catch (error) { + this.answers.reject(key, new Error(String(error))); + } + + const flush = answer + .then( + (saved) => { + if (!saved) this.warn(`Layout save failed before closing window ${senderId}`); + }, + (error: unknown) => { + this.warn(`Layout flush did not complete for window ${senderId}: ${String(error)}`); + }, + ) + .finally(() => this.windowFlushes.delete(senderId)); + this.windowFlushes.set(senderId, flush); + return flush; + } +} diff --git a/src/main/features/window/index.ts b/src/main/features/window/index.ts index 8bab94862..ee9c95fdb 100644 --- a/src/main/features/window/index.ts +++ b/src/main/features/window/index.ts @@ -20,6 +20,7 @@ import { } from "./activation"; import type { AppPage } from "./app-csp"; import { getAppWindows, trackAppWindow } from "./app-windows"; +import { ClosePersistence } from "./close-persistence"; import { opensTransparentOnLinux } from "./linux-transparency"; import { isAllowedMainWindowNavigation } from "./navigation"; import { DEATH_WINDOW_MS, pageDiedQuestion, reloadsPage, watchPageHealth } from "./page-health"; @@ -30,6 +31,8 @@ const TITLEBAR_HEIGHT = 36; const logger = createLogger("window-open-handler"); +export const closePersistence = new ClosePersistence((message) => logger.warn(message)); + // --------------------------------------------------------------------------- // titleBarOverlay color helpers // @@ -224,12 +227,13 @@ export function createMainWindow(appState?: Readonly): BrowserWindow { win.webContents.on("will-navigate", (event, url) => { if (isAllowedMainWindowNavigation(url, devServerUrl)) return; event.preventDefault(); - logger.warn(`Blocked main window navigation: ${url}`); + logger.warn("Blocked main window navigation"); }); // The renderer cancels unload (quit, window close, reload) while buffers // are unsaved. Electron cancels silently, so ask here; preventDefault lets // the unload go ahead when the user chooses to discard. + win.on("close", (event) => closePersistence.interceptClose(event, win)); win.webContents.on("will-prevent-unload", (event) => { const t = getMainT(); const choice = dialog.showMessageBoxSync(win, { @@ -241,6 +245,7 @@ export function createMainWindow(appState?: Readonly): BrowserWindow { cancelId: 1, }); if (choice === 0) event.preventDefault(); + else closePersistence.cancelQuit(); }); // Popups need fresh user input, like Chrome's popup blocker: a previewed @@ -261,13 +266,13 @@ export function createMainWindow(appState?: Readonly): BrowserWindow { !focused || (focused.processId === main.processId && focused.routingId === main.routingId); const previewLink = frameName === PREVIEW_LINK_WINDOW; if (!isExternalSchemeAllowed(url)) { - logger.warn(`Blocked window.open with disallowed scheme: ${url}`); + logger.warn("Blocked window.open with disallowed scheme"); } else if (!opensHere(previewLink, keyboardInApp)) { - logger.warn(`Blocked window.open from where the keyboard is not: ${url}`); + logger.warn("Blocked window.open from where the keyboard is not"); } else if (!activation.take(previewLink ? USER_ACTIVATION_MS : APP_ACTIVATION_MS)) { - logger.warn(`Blocked window.open without user input: ${url}`); + logger.warn("Blocked window.open without user input"); } else { - shell.openExternal(url); + void shell.openExternal(url).catch(() => logger.warn("Could not open external window link")); } return { action: "deny" }; }); diff --git a/src/main/features/window/ipc.ts b/src/main/features/window/ipc.ts index c51b77913..e90bf2e4b 100644 --- a/src/main/features/window/ipc.ts +++ b/src/main/features/window/ipc.ts @@ -4,7 +4,9 @@ import { BrowserWindow, Menu, type WebContents } from "electron"; import { ipcContract } from "../../../shared/ipc/contract"; +import { ipcOk } from "../../../shared/ipc/result"; import { type CallContext, register, validateArgs } from "../../infra/ipc-router"; +import type { ClosePersistence } from "./close-persistence"; /** * Builds the handler used by the "Open in new window" action. The callback is @@ -69,20 +71,27 @@ export function windowControlHandlers( * Registers the window channel: controls for a window whose frame draws none, * and the app menu for a window that shows no menu bar. */ -export function registerWindowChannel(): void { +export function registerWindowChannel(closePersistence: ClosePersistence): void { register("window", { - call: windowControlHandlers( - (sender) => BrowserWindow.fromWebContents(sender), - (win, at) => { - // The page's CSS pixels grow with its zoom; the menu takes the window's. - const zoom = win.webContents.getZoomFactor(); - Menu.getApplicationMenu()?.popup({ - window: win, - x: Math.round(at.x * zoom), - y: Math.round(at.y * zoom), - }); + call: { + ...windowControlHandlers( + (sender) => BrowserWindow.fromWebContents(sender), + (win, at) => { + // The page's CSS pixels grow with its zoom; the menu takes the window's. + const zoom = win.webContents.getZoomFactor(); + Menu.getApplicationMenu()?.popup({ + window: win, + x: Math.round(at.x * zoom), + y: Math.round(at.y * zoom), + }); + }, + ), + closePrepared: (args, ctx) => { + const { attempt, saved } = validateArgs(ipcContract.window.call.closePrepared.args, args); + if (ctx?.sender) closePersistence.acknowledge(ctx.sender.id, attempt, saved); + return ipcOk(undefined); }, - ), + }, listen: {}, }); } diff --git a/src/main/features/workspace/connection/hooks.ts b/src/main/features/workspace/connection/hooks.ts new file mode 100644 index 000000000..8fb106ca8 --- /dev/null +++ b/src/main/features/workspace/connection/hooks.ts @@ -0,0 +1,75 @@ +import { createLogger } from "../../../../shared/log/main"; +import type { AgentChannel } from "../../../infra/agent/channel"; + +const log = createLogger("workspace"); + +/** hookserver connection info the agent returns from the hook.getInfo RPC. */ +export interface HookInfo { + readonly socketPath: string; + readonly token: string; + /** Whether the server takes tabs' own tokens (./tab-token.ts): an older agent does not. */ + readonly tabTokens?: boolean; +} + +/** + * Type guard for the hook.getInfo response. + * A response is valid only when both socketPath and token are strings. + */ +function isHookInfo(value: unknown): value is HookInfo { + if (typeof value !== "object" || value === null) return false; + const v = value as Record; + return typeof v.socketPath === "string" && typeof v.token === "string"; +} + +/** + * Decides whether a `hook.getInfo` response carries the `server.unavailable` code. + * It is a graceful-degrade signal that the agent failed to create the hookserver; the workspace + * must keep booting and run with only the Claude hook feature disabled. + * CodedError and errorFromServerFrame attach a `code` property to the + * Error, so that value is checked. + */ +function isHookUnavailable(err: unknown): boolean { + if (typeof err !== "object" || err === null) return false; + const code = (err as { code?: unknown }).code; + return code === "server.unavailable"; +} + +/** Stores hook endpoints for the current workspace agents. */ +export class WorkspaceHooks { + // Cache of pull-based hookserver connection info — filled by the hook.getInfo RPC right after channel.ready. + private readonly hookInfoByWorkspace = new Map(); + + /** + * Returns the hookserver connection info that was fetched by pull and stored in memory. + * Returns null before ensureProviderReady completes or when the RPC failed. + */ + getHookInfo(workspaceId: string): HookInfo | null { + return this.hookInfoByWorkspace.get(workspaceId) ?? null; + } + + /** Reads the hook endpoint, allowing workspaces whose agent cannot host hooks. */ + async readHookInfo(workspaceId: string, channel: AgentChannel): Promise { + try { + const raw = await channel.call("hook.getInfo", {}); + if (!isHookInfo(raw)) throw new Error("hook.getInfo returned no socketPath or token"); + return raw; + } catch (error) { + if (!isHookUnavailable(error)) throw error; + log.warn( + `hookserver unavailable for ${workspaceId}; hook integration disabled for this session.`, + ); + return null; + } + } + + /** Installs the current agent's endpoint or clears it when hooks are unavailable. */ + cacheHookInfo(workspaceId: string, info: HookInfo | null): void { + if (info) this.hookInfoByWorkspace.set(workspaceId, info); + else this.hookInfoByWorkspace.delete(workspaceId); + } + + /** Drops an endpoint when its channel or workspace goes away. */ + clear(workspaceId: string): void { + this.hookInfoByWorkspace.delete(workspaceId); + } +} diff --git a/src/main/features/workspace/connection/local.ts b/src/main/features/workspace/connection/local.ts new file mode 100644 index 000000000..a132baf22 --- /dev/null +++ b/src/main/features/workspace/connection/local.ts @@ -0,0 +1,262 @@ +import { createLogger } from "../../../../shared/log/main"; +import type { WorkspaceMeta } from "../../../../shared/types/workspace"; +import type { AgentChannel } from "../../../infra/agent/channel"; +import type { SshChannelLifecycleEvent } from "../../../infra/agent/ssh/channel"; +import { AgentFsProvider } from "../../fs/bridge/agent-provider"; +import type { WorkspaceContext } from "../context"; +import type { WorkspaceLocalAgentCommandResolver, WorkspaceLocalChannelFactory } from "../manager"; +import type { HookInfo, WorkspaceHooks } from "./hooks"; +import { + createInitialFsProvider, + createWorkspaceRemovedError, + type WorkspaceConnectionState, +} from "./state"; + +const log = createLogger("workspace"); + +interface LocalHookRefresh { + promise: Promise; + resolve: () => void; +} + +/** Owns local providers and their hook refreshes across agent restarts. */ +export class LocalWorkspaceConnections { + readonly localChannels = new Map(); + + private readonly localProviderReady = new Map>(); + + /** One wait per local reconnect, until the replacement's hooks are known. */ + private readonly localHookRefreshes = new WeakMap(); + + constructor( + private readonly state: WorkspaceConnectionState, + private readonly hooks: WorkspaceHooks, + private readonly localChannelFactory: WorkspaceLocalChannelFactory, + private readonly localAgentCommandResolver: WorkspaceLocalAgentCommandResolver, + ) {} + + /** Releases the local channel after its workspace context is closed. */ + remove(workspaceId: string): void { + this.localChannels.get(workspaceId)?.dispose(); + this.localChannels.delete(workspaceId); + this.localProviderReady.delete(workspaceId); + } + + /** Releases all remaining local channels at app shutdown. */ + close(): void { + for (const channel of this.localChannels.values()) { + channel.dispose(); + } + this.localChannels.clear(); + this.localProviderReady.clear(); + } + + /** Returns the hooks for the channel about to start a chat or terminal. */ + async getSpawnHookInfo( + workspaceId: string, + channel: AgentChannel, + currentChannel: () => AgentChannel | undefined, + ): Promise { + let refresh = this.localHookRefreshes.get(channel); + while (refresh) { + await refresh.promise; + refresh = this.localHookRefreshes.get(channel); + } + if (currentChannel() !== channel) { + throw new Error(`agent channel changed for workspace: ${workspaceId}`); + } + return this.hooks.getHookInfo(workspaceId); + } + + /** + * Starts the local agent and wires the context only after the ready handshake. + */ + async ensureLocalProviderReady(ctx: WorkspaceContext): Promise { + const meta = ctx.getMeta(); + if (meta.location.kind !== "local") { + return; + } + + const pending = this.localProviderReady.get(meta.id); + if (pending) { + await pending; + return; + } + + const ready = this.startLocalProvider(ctx, meta); + this.localProviderReady.set(meta.id, ready); + try { + await ready; + } catch (error) { + if (this.localProviderReady.get(meta.id) === ready) { + this.localProviderReady.delete(meta.id); + } + throw error; + } + } + + /** + * Owns the explicit local boot sequence: spawn → ready → context provider. + */ + private async startLocalProvider(ctx: WorkspaceContext, meta: WorkspaceMeta): Promise { + if (meta.location.kind !== "local") { + return; + } + + const command = this.localAgentCommandResolver(); + const channel = this.localChannelFactory({ ...command, rootPath: meta.location.rootPath }); + this.localChannels.set(meta.id, channel); + const disposeLifecycleListener = channel.onLifecycle((event) => { + this.handleLocalChannelLifecycle(meta.id, channel, event); + }); + + try { + await channel.ready; + } catch (error) { + disposeLifecycleListener(); + if (this.localChannels.get(meta.id) === channel) { + this.localChannels.delete(meta.id); + } + channel.dispose(); + ctx.setFsProvider(createInitialFsProvider(meta)); + throw error; + } + + try { + const info = await this.hooks.readHookInfo(meta.id, channel); + if (this.localChannels.get(meta.id) === channel && !this.localHookRefreshes.has(channel)) { + this.hooks.cacheHookInfo(meta.id, info); + } + } catch (err) { + disposeLifecycleListener(); + if (this.localChannels.get(meta.id) === channel) { + this.localChannels.delete(meta.id); + } + this.finishLocalHookRefresh(channel); + channel.dispose(); + ctx.setFsProvider(createInitialFsProvider(meta)); + log.warn(`agent hook setup failed for ${meta.id}: ${(err as Error).message}`); + throw new Error(`Agent hook setup failed: ${(err as Error).message}`, { cause: err }); + } + + // Write the PTY shim files into the workspace-specific directory. + // On failure only the PATH priority breaks (the hook itself works via the absolute path in settings.json), + // so warn gracefully and continue booting. + try { + await this.state.writeShimFiles(meta.id); + } catch (shimErr) { + log.warn( + `shim file write failed for ${meta.id}; shell PATH priority may be degraded: ${(shimErr as Error).message}`, + ); + } + + if (!this.state.isLiveContext(meta.id, ctx)) { + disposeLifecycleListener(); + channel.dispose(); + // remove() already deleted the shim dir; this boot rewrote it. + this.state.removeShimDirQuietly(meta.id); + throw createWorkspaceRemovedError(meta.id); + } + + const provider = new AgentFsProvider("local", channel); + ctx.setFsProvider(provider, () => { + disposeLifecycleListener(); + this.finishLocalHookRefresh(channel); + provider.dispose(); + channel.dispose(); + if (this.localChannels.get(meta.id) === channel) { + this.localChannels.delete(meta.id); + } + this.localProviderReady.delete(meta.id); + }); + this.state.notifyProviderReady(meta.id, provider); + } + + /** + * Handles terminal local channel lifecycle events and restores the inert provider. + */ + private handleLocalChannelLifecycle( + workspaceId: string, + channel: AgentChannel, + event: SshChannelLifecycleEvent, + ): void { + if (this.localChannels.get(workspaceId) !== channel) { + return; + } + + const ctx = this.state.contexts.get(workspaceId); + if (!ctx) { + this.localChannels.delete(workspaceId); + this.localProviderReady.delete(workspaceId); + this.finishLocalHookRefresh(channel); + return; + } + + if (event.type === "reconnecting") { + const previous = this.localHookRefreshes.get(channel); + let resolve!: () => void; + const promise = new Promise((done) => { + resolve = done; + }); + this.localHookRefreshes.set(channel, { promise, resolve }); + this.hooks.clear(workspaceId); + previous?.resolve(); + return; + } + if (event.type === "ready") { + const refresh = this.localHookRefreshes.get(channel); + if (refresh) void this.refreshLocalHooks(workspaceId, channel, refresh); + return; + } + if (event.type === "disposed") { + this.finishLocalHookRefresh(channel); + return; + } + + // Teardown is reserved for genuine terminal events (`exit`, `failure`). + // Heartbeat quality signals leave the provider and its watches wired. + if (event.type !== "exit" && event.type !== "failure") { + return; + } + + this.localChannels.delete(workspaceId); + this.localProviderReady.delete(workspaceId); + this.finishLocalHookRefresh(channel); + // Stale hookInfo is invalid once the channel dies — the next boot fills it again. + // Keeping it could let a spawn right after reconnect put a dead socket path in the env. + this.hooks.clear(workspaceId); + ctx.setFsProvider(createInitialFsProvider(ctx.getMeta())); + // Clean up the PTY shim directory — fire-and-forget, errors are swallowed as a warn. + this.state.removeShimDir(workspaceId).catch((err: unknown) => { + log.warn(`shim dir removal failed for ${workspaceId}: ${(err as Error).message}`); + }); + } + + /** Refreshes the endpoint for the process that just completed its handshake. */ + private async refreshLocalHooks( + workspaceId: string, + channel: AgentChannel, + refresh: LocalHookRefresh, + ): Promise { + try { + const info = await this.hooks.readHookInfo(workspaceId, channel); + if (this.localHookRefreshes.get(channel) !== refresh) return; + this.hooks.cacheHookInfo(workspaceId, info); + this.finishLocalHookRefresh(channel); + } catch (error) { + if (this.localHookRefreshes.get(channel) !== refresh) return; + log.warn(`agent hook setup failed for ${workspaceId}: ${(error as Error).message}`); + this.handleLocalChannelLifecycle(workspaceId, channel, { + type: "failure", + error: error instanceof Error ? error : new Error(String(error)), + }); + } + } + + /** Releases chat callers so they can check whether their channel is still current. */ + private finishLocalHookRefresh(channel: AgentChannel): void { + const refresh = this.localHookRefreshes.get(channel); + this.localHookRefreshes.delete(channel); + refresh?.resolve(); + } +} diff --git a/src/main/features/workspace/connection/remote.ts b/src/main/features/workspace/connection/remote.ts new file mode 100644 index 000000000..127d9fe1f --- /dev/null +++ b/src/main/features/workspace/connection/remote.ts @@ -0,0 +1,771 @@ +import { createLogger } from "../../../../shared/log/main"; +import { + type WorkspaceLocation, + type WorkspaceMeta, + workspaceLocationKey, +} from "../../../../shared/types/workspace"; +import type { + CreateSshChannelOptions, + SshChannel, + SshChannelLifecycleEvent, +} from "../../../infra/agent/ssh/channel"; +import type { SshControlMaster } from "../../../infra/agent/ssh/master"; +import type { + EnsureRemoteAgentResult, + LspBootstrapProgressEvent, +} from "../../../infra/agent/ssh/ssh-bootstrap/index"; +import type { GlobalStorage } from "../../../infra/storage/global-storage"; +import { createFsProvider } from "../../fs/bridge/create-provider"; +import type { WorkspaceContext } from "../context"; +import type { WorkspaceSshBootstrap, WorkspaceSshChannelFactory } from "../manager"; +import type { HookInfo, WorkspaceHooks } from "./hooks"; +import { + createInitialFsProvider, + createWorkspaceRemovedError, + type WorkspaceConnectionState, +} from "./state"; +import type { WorkspaceWslTransport } from "./wsl"; + +const log = createLogger("workspace"); + +/** Delays before the three automatic SSH reauthentication attempts. */ +const SSH_REAUTH_BACKOFF_MS = [0, 5_000, 30_000]; + +/** + * How long a removed SSH workspace's agent has to answer agent.shutdown, + * asked again included: one being reconnected to is asked once the link is + * back, as long as the channel's reconnect queue keeps a call. + */ +const SSH_AGENT_SHUTDOWN_TIMEOUT_MS = 30_000; +/** The pause before agent.shutdown is asked again, after the link dropped under it. */ +const SSH_AGENT_SHUTDOWN_RETRY_MS = 250; +/** + * How long another agent on the host has to stop a removed workspace's + * daemon; it dials that daemon and waits for its own agent.shutdown reply. + */ +const SSH_PEER_DAEMON_STOP_TIMEOUT_MS = 5_000; + +type SshWorkspaceLocation = Extract; + +/** + * Asks a removed workspace's remote agent to exit, then closes its channel + * and connection. Closing them alone leaves the daemon holding the + * workspace's terminals and language servers until its reattach grace ends. + * A channel that is reconnecting holds the request until the link is back; + * one the link drops under (an unstable link's ssh giving up) is asked + * again, all within SSH_AGENT_SHUTDOWN_TIMEOUT_MS: a call's own timeout + * starts only once it leaves the reconnect queue. Resolves whether the + * agent answered. + */ +async function stopSshAgent( + channel: SshChannel, + bootstrap: EnsureRemoteAgentResult | undefined, +): Promise { + const deadline = Date.now() + SSH_AGENT_SHUTDOWN_TIMEOUT_MS; + let timer: NodeJS.Timeout | undefined; + const expired = new Promise<"expired">((resolve) => { + timer = setTimeout(() => resolve("expired"), SSH_AGENT_SHUTDOWN_TIMEOUT_MS); + }); + let stopped = false; + try { + for (;;) { + try { + const answer = await Promise.race([ + channel + .call("agent.shutdown", undefined, { timeoutMs: Math.max(1, deadline - Date.now()) }) + .then(() => "answered" as const), + expired, + ]); + if (answer === "expired") { + log.warn("agent.shutdown got no answer in time"); + break; + } + stopped = true; + break; + } catch (error) { + const retryable = (error as { retryable?: unknown }).retryable === true; + if (!retryable || Date.now() + SSH_AGENT_SHUTDOWN_RETRY_MS >= deadline) { + log.warn(`agent.shutdown failed: ${(error as Error).message}`); + break; + } + await new Promise((resolve) => setTimeout(resolve, SSH_AGENT_SHUTDOWN_RETRY_MS)); + } + } + } finally { + clearTimeout(timer); + } + channel.dispose(); + bootstrap?.dispose?.(); + return stopped; +} + +/** + * Asks the remote agent on `channel` to stop the daemon of another + * workspace on its host, at `root`: one removed while not connected, whose + * daemon would otherwise hold its terminals until its reattach grace ends. + */ +async function stopPeerDaemon(channel: SshChannel, root: string): Promise { + await channel + .call("agent.stopDaemon", { root }, { timeoutMs: SSH_PEER_DAEMON_STOP_TIMEOUT_MS }) + .catch((error: unknown) => { + log.warn(`agent.stopDaemon failed: ${(error as Error).message}`); + }); +} + +/** Whether two remote locations are on one host, as one user, where their daemons share a run folder. */ +function sameRemoteHost(a: WorkspaceLocation, b: WorkspaceLocation): boolean { + if (a.kind === "ssh" && b.kind === "ssh") { + return a.host === b.host && a.user === b.user && (a.port ?? 22) === (b.port ?? 22); + } + return a.kind === "wsl" && b.kind === "wsl" && a.distro === b.distro; +} + +/** + * Builds the SSH channel options used when activating a remote workspace. + */ +function sshChannelOptionsFromLocation( + location: SshWorkspaceLocation, + remoteCommand: string, + controlPath?: string, +): CreateSshChannelOptions { + return { + host: location.host, + user: location.user, + port: location.port, + identityFile: location.identityFile, + authMode: location.authMode, + remoteCommand, + controlPath, + }; +} + +/** Owns SSH and WSL provider boot, reattachment and daemon shutdown. */ +export class RemoteWorkspaceConnections { + readonly sshChannels = new Map(); + + /** Channels of removed workspaces, kept open until their agent exits. */ + private readonly stoppingSshChannels = new Set(); + + /** + * Other daemons a connected agent is stopping, by its channel. Its own + * stop waits for them: its agent exits, and drops them, once it answers. + */ + private readonly peerDaemonStops = new Map>(); + + /** + * Stops of removed remote workspaces' daemons, by location. A workspace + * on that location connects once they have ended: the daemon is one per + * folder, so a stop that ends after it connects stops its terminals, and + * a removed workspace's channel still reconnecting takes the daemon over. + */ + private readonly daemonStopsByLocation = new Map>(); + + readonly sshBootstraps = new Map(); + + /** Bootstrap still owns its SSH/WSL processes until it hands off the channel. */ + private readonly remoteBootstrapControllers = new Map(); + + private readonly sshProviderReady = new Map>(); + + // ControlMasters handed off from an SSH browse session, keyed by workspace + // id, awaiting their first provider boot. Consumed by startRemoteProvider. + private readonly adoptedSshMasters = new Map(); + + /** One retry loop per workspace, including the gaps between attempts. */ + private readonly sshReauth = new Map< + string, + { controller: AbortController; ready: Promise } + >(); + + /** + * Pending timers for `degraded-recovered` debounce. + * Clears the "unstable" badge ~1 s after the last heartbeat recovery signal + * to prevent rapid unstable↔connected flapping. + */ + private readonly degradedRecoveryTimers = new Map(); + + /** + * Callback to restore held PTY sessions after a successful re-authentication. + * Injected from main/index.ts after construction to break the circular dep. + * Manager calls this immediately after ensureRemoteProviderReady resolves on a + * reauth path. + */ + ptyRestoreAfterReauth: ((workspaceId: string) => Promise) | null = null; + + /** + * Callback to release held PTY sessions when re-authentication will not be + * attempted (auth-cancelled, backoff exhausted, non-interactive, ctx gone). + * Injected from main/index.ts after construction to break the circular dep. + * Manager calls this on every terminal failure path that does NOT reauth. + */ + ptyReleaseHeld: ((workspaceId: string) => void) | null = null; + + constructor( + private readonly state: WorkspaceConnectionState, + private readonly hooks: WorkspaceHooks, + private readonly globalStorage: GlobalStorage, + private readonly sshChannelFactory: WorkspaceSshChannelFactory, + private readonly sshBootstrap: WorkspaceSshBootstrap, + private readonly wsl: WorkspaceWslTransport, + ) {} + + /** Cancels workspace-specific bootstrap, retry and recovery work. */ + cancelPending(id: string): void { + this.remoteBootstrapControllers.get(id)?.abort(createWorkspaceRemovedError(id)); + this.cancelSshReauth(id); + this.clearDegradedRecovery(id); + } + + /** Retains the channel long enough to stop a removed workspace's daemon. */ + stopRemovedAgent(id: string, ctx: WorkspaceContext): void { + // Step 1c — a removed workspace's remote agent will not be dialed again, + // so it is asked to exit. Its channel stays open for that, past the + // provider teardown in ctx.close(), and after the daemons it is + // stopping for others. One reconnecting is asked once its link is back. + // One not connected, or whose agent cannot be reached, is stopped by + // another agent on its host, if one is connected. + const sshChannel = this.sshChannels.get(id); + if (sshChannel) { + this.stoppingSshChannels.add(sshChannel); + const bootstrap = this.sshBootstraps.get(id); + const meta = ctx.getMeta(); + const stop = async () => { + if (!(await stopSshAgent(sshChannel, bootstrap))) this.stopDaemonThroughPeer(meta); + }; + const pending = this.peerDaemonStops.get(sshChannel); + const stopping = (pending ? pending.then(stop) : stop()).finally(() => { + this.stoppingSshChannels.delete(sshChannel); + }); + this.trackDaemonStop(meta.location, stopping); + } else { + this.stopDaemonThroughPeer(ctx.getMeta()); + } + } + + /** Discards remote state after the removed workspace's context is closed. */ + remove(id: string): void { + this.sshChannels.delete(id); + this.sshBootstraps.delete(id); + this.sshProviderReady.delete(id); + // An adopted master never consumed by a provider boot would otherwise + // leak its ssh process. + this.adoptedSshMasters.get(id)?.dispose(); + this.adoptedSshMasters.delete(id); + } + + /** Drops remote clients at app shutdown while keeping their daemons available. */ + close(): void { + for (const channel of this.sshChannels.values()) { + channel.dispose(); + } + this.sshChannels.clear(); + for (const bootstrap of this.sshBootstraps.values()) { + bootstrap.dispose?.(); + } + this.sshBootstraps.clear(); + this.sshProviderReady.clear(); + for (const master of this.adoptedSshMasters.values()) { + master.dispose(); + } + this.adoptedSshMasters.clear(); + } + + /** + * Asks another connected agent on the removed remote workspace's host — + * its parent's first — to stop that workspace's daemon, which may still + * run its terminals from an earlier connection. + */ + private stopDaemonThroughPeer(meta: WorkspaceMeta): void { + const { location } = meta; + if (location.kind === "local") return; + const channel = this.connectedPeerChannel(meta); + if (!channel) return; + const stop = stopPeerDaemon(channel, location.remotePath); + this.trackDaemonStop(location, stop); + const pending = this.peerDaemonStops.get(channel); + const all = pending ? Promise.all([pending, stop]).then(() => {}) : stop; + this.peerDaemonStops.set(channel, all); + void all.finally(() => { + if (this.peerDaemonStops.get(channel) === all) this.peerDaemonStops.delete(channel); + }); + } + + /** Notes a stop of the daemon at `location`, which a new connection there waits for. */ + private trackDaemonStop(location: WorkspaceLocation, stop: Promise): void { + const key = workspaceLocationKey(location); + const pending = this.daemonStopsByLocation.get(key); + const all = (pending ? Promise.all([pending, stop]) : stop).then( + () => {}, + () => {}, + ); + this.daemonStopsByLocation.set(key, all); + void all.then(() => { + if (this.daemonStopsByLocation.get(key) === all) this.daemonStopsByLocation.delete(key); + }); + } + + /** + * Waits for the stops of a removed workspace's daemon at `location`, one + * tracked meanwhile (a peer asked after its own agent could not be + * reached) included. + */ + private async daemonStopped(location: WorkspaceLocation): Promise { + const key = workspaceLocationKey(location); + for (;;) { + const pending = this.daemonStopsByLocation.get(key); + if (!pending) return; + await pending; + } + } + + /** The channel of a connected workspace on `meta`'s host other than its own daemon's. */ + private connectedPeerChannel(meta: WorkspaceMeta): SshChannel | undefined { + const { location } = meta; + if (location.kind === "local") return undefined; + const ids = [meta.task?.parentId, ...this.state.contexts.keys()]; + for (const id of ids) { + if (id === undefined || id === meta.id) continue; + const other = this.state.contexts.get(id)?.getMeta().location; + const channel = this.sshChannels.get(id); + const status = this.state.connectionStatuses.get(id); + if (!other || other.kind === "local" || !channel) continue; + if (status !== "connected" && status !== "unstable") continue; + if (this.stoppingSshChannels.has(channel)) continue; + if (!sameRemoteHost(other, location) || other.remotePath === location.remotePath) continue; + return channel; + } + return undefined; + } + + /** + * Lazily connects one SSH or WSL channel per workspace and injects it into + * the context. + */ + async ensureRemoteProviderReady(ctx: WorkspaceContext, reauth = false): Promise { + const meta = ctx.getMeta(); + if (meta.location.kind === "local") { + return; + } + + const recovery = this.sshReauth.get(meta.id); + if (recovery && !reauth) { + await recovery.ready; + return; + } + + const pending = this.sshProviderReady.get(meta.id); + if (pending) { + await pending; + return; + } + + const ready = this.startRemoteProvider(ctx, meta); + this.sshProviderReady.set(meta.id, ready); + try { + await ready; + } catch (error) { + if (this.sshProviderReady.get(meta.id) === ready) { + this.sshProviderReady.delete(meta.id); + } + throw error; + } + } + + /** + * Owns the explicit remote boot sequence: bootstrap → spawn → ready → + * context provider. A WSL workspace takes it like an SSH one; only the + * bootstrap and the channel's transport differ. + */ + private async startRemoteProvider(ctx: WorkspaceContext, meta: WorkspaceMeta): Promise { + const location = meta.location; + if (location.kind === "local") { + return; + } + + this.state.broadcastConnectionStatus(meta.id, "connecting"); + // A workspace removed and added again connects once its old daemon has + // been stopped, not to it as that stop ends. + await this.daemonStopped(location); + if (!this.state.isLiveContext(meta.id, ctx)) throw createWorkspaceRemovedError(meta.id); + // A workspace created from a browse session inherits that session's + // already-authenticated ControlMaster; reusing its socket lets bootstrap + // skip the interactive auth round entirely (no second password prompt). + const adoptedMaster = this.adoptedSshMasters.get(meta.id); + this.adoptedSshMasters.delete(meta.id); + const controller = new AbortController(); + this.remoteBootstrapControllers.set(meta.id, controller); + const signal = controller.signal; + let bootstrap: EnsureRemoteAgentResult; + const onProgress = (event: LspBootstrapProgressEvent) => { + if (!signal.aborted) this.state.broadcastConnectionProgress(meta.id, event); + }; + try { + bootstrap = + location.kind === "wsl" + ? await this.wsl.bootstrap( + { + distro: location.distro, + remotePath: location.remotePath, + cachedRemoteArch: location.remoteArch, + workspaceId: meta.id, + signal, + }, + { onProgress }, + ) + : await this.sshBootstrap( + { + host: location.host, + user: location.user, + port: location.port, + identityFile: location.identityFile, + authMode: location.authMode, + remotePath: location.remotePath, + cachedRemoteArch: location.remoteArch, + controlPath: adoptedMaster?.controlPath, + // Pass workspaceId so the bootstrap also uploads the shim rc + // files (`.zshrc`/`.zshenv`/`bashrc`) into the remote's shared + // `~/.nexus-code/shim/shared/`, making them available to the + // remote PTY's zsh `ZDOTDIR` / bash `--rcfile` activation. + workspaceId: meta.id, + signal, + }, + { onProgress }, + ); + } catch (error) { + // Bootstrap failed before any channel existed. Release the adopted + // master (we own it now) and surface the error state instead of + // leaving the renderer stuck on "connecting". + adoptedMaster?.dispose(); + if (!this.state.isLiveContext(meta.id, ctx)) throw createWorkspaceRemovedError(meta.id); + if (!this.sshReauth.has(meta.id)) this.state.broadcastConnectionStatus(meta.id, "error"); + throw error; + } finally { + this.remoteBootstrapControllers.delete(meta.id); + } + // ensureRemoteAgent only returns a dispose handle for a master it + // authenticated itself. When we supplied an adopted master, wire its + // dispose in so the existing teardown paths release that socket too. + if (adoptedMaster && !bootstrap.dispose) { + bootstrap = { ...bootstrap, dispose: () => adoptedMaster.dispose() }; + } + // The bootstrap can span a password prompt and an agent upload; the + // workspace may have been removed meanwhile, and nothing would dispose a + // channel or master created for it from here on. + if (!this.state.isLiveContext(meta.id, ctx)) { + bootstrap.dispose?.(); + throw createWorkspaceRemovedError(meta.id); + } + let providerMeta = meta; + if (!location.remoteArch) { + providerMeta = { + ...meta, + location: + location.kind === "wsl" + ? { ...location, remoteArch: { os: "linux", arch: bootstrap.platform.arch } } + : { ...location, remoteArch: bootstrap.platform }, + }; + this.globalStorage.updateWorkspace(meta.id, { location: providerMeta.location }); + ctx.setMeta(providerMeta); + this.state.broadcastFn("workspace", "changed", providerMeta); + } + if (!this.state.isLiveContext(meta.id, ctx)) { + bootstrap.dispose?.(); + throw createWorkspaceRemovedError(meta.id); + } + let channel: SshChannel; + try { + channel = + location.kind === "wsl" + ? this.wsl.createChannel({ + distro: location.distro, + remoteCommand: bootstrap.remoteCommand, + }) + : this.sshChannelFactory( + sshChannelOptionsFromLocation( + { ...location, remoteArch: bootstrap.platform }, + bootstrap.remoteCommand, + bootstrap.controlPath, + ), + ); + } catch (error) { + bootstrap.dispose?.(); + if (!this.sshReauth.has(meta.id)) this.state.broadcastConnectionStatus(meta.id, "error"); + throw error; + } + this.sshBootstraps.set(meta.id, bootstrap); + this.sshChannels.set(meta.id, channel); + let installed = false; + let bootFailure: Error | undefined; + const disposeLifecycleListener = channel.onLifecycle((event) => { + if (installed) this.handleSshChannelLifecycle(meta.id, channel, event); + else if (event.type === "failure") bootFailure = event.error; + else if (event.type === "exit" || event.type === "held-then-expired") { + bootFailure = new Error("Agent connection ended during provider setup"); + } + }); + + /** Checks ownership after each asynchronous stage of provider setup. */ + const checkBoot = () => { + if (!this.state.isLiveContext(meta.id, ctx)) throw createWorkspaceRemovedError(meta.id); + if (bootFailure) throw bootFailure; + }; + let shimStarted = false; + try { + await channel.ready; + checkBoot(); + + let info: HookInfo | null; + try { + info = await this.hooks.readHookInfo(meta.id, channel); + } catch (error) { + checkBoot(); + log.warn(`agent hook setup failed for ${meta.id}: ${(error as Error).message}`); + throw new Error(`Agent hook setup failed: ${(error as Error).message}`, { cause: error }); + } + checkBoot(); + if (this.sshChannels.get(meta.id) === channel) this.hooks.cacheHookInfo(meta.id, info); + + // The remote workspace's local shim supplies env/args for local PTYs. + shimStarted = true; + try { + await this.state.writeShimFiles(meta.id); + } catch (error) { + log.warn( + `shim file write failed for ${meta.id}; shell PATH priority may be degraded: ${(error as Error).message}`, + ); + } + checkBoot(); + } catch (error) { + disposeLifecycleListener(); + if (shimStarted) this.state.removeShimDirQuietly(meta.id); + if (!this.state.isLiveContext(meta.id, ctx)) throw createWorkspaceRemovedError(meta.id); + this.sshChannels.delete(meta.id); + this.sshBootstraps.delete(meta.id); + this.hooks.clear(meta.id); + channel.dispose(); + bootstrap.dispose?.(); + if (!this.sshReauth.has(meta.id)) this.state.broadcastConnectionStatus(meta.id, "error"); + throw error; + } + + installed = true; + const provider = createFsProvider(providerMeta, channel); + ctx.setFsProvider(provider, () => { + disposeLifecycleListener(); + // remove() closes a channel it is stopping once the agent has exited. + if (!this.stoppingSshChannels.has(channel)) { + channel.dispose(); + bootstrap.dispose?.(); + } + if (this.sshChannels.get(meta.id) === channel) { + this.sshChannels.delete(meta.id); + } + this.sshBootstraps.delete(meta.id); + this.sshProviderReady.delete(meta.id); + // A terminal status set just before teardown says why the provider + // went away; "disconnected" would hide the error or session-expired UI. + const status = this.state.connectionStatuses.get(meta.id); + if (status !== "error" && status !== "held-then-expired") { + this.state.broadcastConnectionStatus(meta.id, "disconnected"); + } + }); + this.state.broadcastConnectionStatus(meta.id, "connected"); + this.state.notifyProviderReady(meta.id, provider); + } + + /** + * Handles terminal SSH channel lifecycle events and restores the inert SSH provider. + * + * Reattach-aware transitions: + * - `degraded`: 1 missed heartbeat → broadcast "unstable" (workspaceIsOnline stays true). + * - `degraded-recovered`: heartbeat restored → debounce ~1 s then broadcast "connected" + * to prevent rapid unstable↔connected flapping. + * - `reconnecting`: transient — channel may yet recover; keep references, broadcast + * "reconnecting" so the renderer shows the hold overlay. + * - `ready` (epoch-match): session restored — broadcast "connected" and clear unstable. + * - `held-then-expired`: daemon replaced — broadcast "held-then-expired" so the renderer + * shows "session expired" rather than a generic error. Then tear down the provider. + * - `failure` / `exit`: terminal — if auth-failed on an interactive workspace, attempt + * automatic re-authentication with exponential backoff (plan issue 6). + */ + private handleSshChannelLifecycle( + workspaceId: string, + channel: SshChannel, + event: SshChannelLifecycleEvent, + ): void { + if (this.sshChannels.get(workspaceId) !== channel) { + return; + } + + const ctx = this.state.contexts.get(workspaceId); + if (!ctx) { + this.sshChannels.delete(workspaceId); + this.sshBootstraps.delete(workspaceId); + return; + } + + this.clearDegradedRecovery(workspaceId); + + // ── Transient quality events ────────────────────────────────────────── + if (event.type === "degraded") { + this.state.broadcastConnectionStatus(workspaceId, "unstable"); + return; + } + + if (event.type === "degraded-recovered") { + // ~1 s debounce recommended by designer (plan issue 5) to prevent flapping. + const timer = setTimeout(() => { + this.degradedRecoveryTimers.delete(workspaceId); + if (this.sshChannels.get(workspaceId) === channel) { + this.state.broadcastConnectionStatus(workspaceId, "connected"); + } + }, 1000); + timer.unref?.(); + this.degradedRecoveryTimers.set(workspaceId, timer); + return; + } + + // ── Reconnecting: keep references, update UI ────────────────────────── + if (event.type === "reconnecting") { + this.state.broadcastConnectionStatus(workspaceId, "reconnecting"); + return; + } + + // ── Epoch-match reattach ────────────────────────────────────────────── + if (event.type === "ready") { + // Channel recovered with the same daemon — sessions are being restored + // by agent-host. Broadcast "connected" to clear the reconnecting overlay. + this.state.broadcastConnectionStatus(workspaceId, "connected"); + return; + } + + // ── Held-then-expired: daemon replaced ─────────────────────────────── + if (event.type === "held-then-expired") { + this.state.broadcastConnectionStatus(workspaceId, "held-then-expired"); + this.tearDownSshProvider(workspaceId, ctx); + return; + } + + // ── Terminal events: failure / exit ─────────────────────────────────── + const isAuthFailure = + event.type === "failure" && (event.error as { code?: string }).code === "ssh.auth-failed"; + const meta = ctx.getMeta(); + const isInteractive = meta.location.kind === "ssh" && meta.location.authMode === "interactive"; + + if (event.type === "failure" && !isAuthFailure) { + this.state.broadcastConnectionStatus(workspaceId, "error"); + } + + this.tearDownSshProvider(workspaceId, ctx); + + if (isAuthFailure && isInteractive && this.state.isLiveContext(workspaceId, ctx)) { + this.startSshReauth(ctx); + return; + } + + // No reauth attempted: release any held PTY sessions so they don't leak. + this.ptyReleaseHeld?.(workspaceId); + } + + /** + * Tears down SSH provider state after a terminal channel event. + * Shared by `failure`, `exit`, and `held-then-expired` paths. + */ + private tearDownSshProvider(workspaceId: string, ctx: WorkspaceContext): void { + this.sshChannels.delete(workspaceId); + this.sshBootstraps.delete(workspaceId); + this.sshProviderReady.delete(workspaceId); + // Stale hookInfo is invalid once the channel dies — the next boot fills it again. + this.hooks.clear(workspaceId); + ctx.setFsProvider(createInitialFsProvider(ctx.getMeta())); + // Clean up the PTY shim directory — fire-and-forget, errors are swallowed as a warn. + this.state.removeShimDir(workspaceId).catch((err: unknown) => { + log.warn(`shim dir removal failed for ${workspaceId}: ${(err as Error).message}`); + }); + } + + /** Cancels a delayed transition back to connected. */ + private clearDegradedRecovery(workspaceId: string): void { + clearTimeout(this.degradedRecoveryTimers.get(workspaceId)); + this.degradedRecoveryTimers.delete(workspaceId); + } + + /** Owns the held PTYs until automatic SSH reauthentication is resolved. */ + private startSshReauth(ctx: WorkspaceContext): void { + const { id } = ctx.getMeta(); + if (this.sshReauth.has(id)) return; + const controller = new AbortController(); + const ready = Promise.resolve() + .then(() => this.retrySshReauth(ctx, controller.signal)) + .then( + () => { + if (controller.signal.aborted) throw createWorkspaceRemovedError(id); + this.sshReauth.delete(id); + this.ptyRestoreAfterReauth?.(id).catch((error: unknown) => { + log.warn(`restoreAfterReauth failed for ${id}: ${(error as Error).message}`); + }); + }, + (error: unknown) => { + if (controller.signal.aborted) throw createWorkspaceRemovedError(id); + this.sshReauth.delete(id); + const cancelled = (error as { code?: string }).code === "ssh.auth-cancelled"; + this.state.broadcastConnectionStatus(id, cancelled ? "disconnected" : "error"); + this.ptyReleaseHeld?.(id); + throw error; + }, + ); + this.sshReauth.set(id, { controller, ready }); + // Automatic recovery can run without any caller awaiting it. + void ready.catch(() => {}); + } + + /** Runs all attempts in order, keeping manual Connect on the same promise. */ + private async retrySshReauth(ctx: WorkspaceContext, signal: AbortSignal): Promise { + const { id } = ctx.getMeta(); + for (const [attempt, delay] of SSH_REAUTH_BACKOFF_MS.entries()) { + if (delay > 0 && !signal.aborted) { + await new Promise((resolve) => { + const finish = () => { + clearTimeout(timer); + signal.removeEventListener("abort", finish); + resolve(); + }; + const timer = setTimeout(finish, delay); + timer.unref?.(); + signal.addEventListener("abort", finish, { once: true }); + }); + } + if (signal.aborted) throw createWorkspaceRemovedError(id); + try { + await this.ensureRemoteProviderReady(ctx, true); + return; + } catch (error) { + if (signal.aborted || (error as { code?: string }).code === "ssh.auth-cancelled") { + throw error; + } + log.warn(`reauth attempt ${attempt + 1} failed for ${id}: ${(error as Error).message}`); + if (attempt === SSH_REAUTH_BACKOFF_MS.length - 1) throw error; + } + } + } + + /** Stops pending retries and releases held PTYs when the workspace closes. */ + private cancelSshReauth(workspaceId: string): void { + const recovery = this.sshReauth.get(workspaceId); + if (!recovery) return; + this.sshReauth.delete(workspaceId); + recovery.controller.abort(); + this.ptyReleaseHeld?.(workspaceId); + } + + /** + * Adopts a ControlMaster handed off from an SSH directory-browse session. + * The next SSH provider boot for this workspace reuses that socket, so the + * user is not prompted for credentials a second time. Safe to call before + * the provider boots; the master is consumed by startRemoteProvider. + */ + adoptSshControlMaster(workspaceId: string, master: SshControlMaster): void { + this.adoptedSshMasters.get(workspaceId)?.dispose(); + this.adoptedSshMasters.set(workspaceId, master); + } +} diff --git a/src/main/features/workspace/connection/state.ts b/src/main/features/workspace/connection/state.ts new file mode 100644 index 000000000..ee04e7e2c --- /dev/null +++ b/src/main/features/workspace/connection/state.ts @@ -0,0 +1,122 @@ +import { createAbortError } from "../../../../shared/abort"; +import { createLogger } from "../../../../shared/log/main"; +import type { + WorkspaceConnectionChangedEvent, + WorkspaceConnectionEventStatus, + WorkspaceMeta, +} from "../../../../shared/types/workspace"; +import type { LspBootstrapProgressEvent } from "../../../infra/agent/ssh/ssh-bootstrap/index"; +import { AgentFsProvider } from "../../fs/bridge/agent-provider"; +import { createFsProvider } from "../../fs/bridge/create-provider"; +import type { FsProvider } from "../../fs/bridge/provider"; +import type { WorkspaceContext } from "../context"; +import type { BroadcastFn, RemoveShimDirFn, WriteShimFilesFn } from "../manager"; + +const log = createLogger("workspace"); + +/** Rejects a provider boot whose workspace was removed while it was in flight. */ +export function createWorkspaceRemovedError(id: string): Error { + return createAbortError(`workspace ${id} was removed while its agent was starting`); +} + +/** + * Builds an inert provider for unopened workspaces. Activation replaces it + * only after the workspace agent has completed its ready handshake. + */ +export function createInitialFsProvider(meta: WorkspaceMeta): AgentFsProvider { + if (meta.location.kind === "local") { + return new AgentFsProvider("local"); + } + return createFsProvider(meta) as AgentFsProvider; +} + +/** Shared workspace context, shim operations and notifications for agent connections. */ +export class WorkspaceConnectionState { + readonly connectionStatuses = new Map(); + + private readonly providerReadyListeners = new Set< + (workspaceId: string, provider: FsProvider) => void + >(); + + constructor( + readonly contexts: ReadonlyMap, + readonly broadcastFn: BroadcastFn, + readonly writeShimFiles: WriteShimFilesFn, + readonly removeShimDir: RemoveShimDirFn, + ) {} + + /** + * Subscribes to agent boots: fires each time a workspace gets a newly + * started agent provider, on first activation and again when the previous + * channel died and a later call booted a replacement. The new agent knows + * nothing the old one had registered (fs and .git watches), and listeners + * attached to the old provider never hear from it. + */ + onProviderReady(listener: (workspaceId: string, provider: FsProvider) => void): () => void { + this.providerReadyListeners.add(listener); + return () => { + this.providerReadyListeners.delete(listener); + }; + } + + /** + * True while `ctx` is still the open context for `id`, i.e. the workspace + * was not removed during an await in a provider boot. + */ + isLiveContext(id: string, ctx: WorkspaceContext): boolean { + return this.contexts.get(id) === ctx; + } + + /** Deletes the workspace's PTY shim directory in the background. */ + removeShimDirQuietly(id: string): void { + this.removeShimDir(id).catch((err: unknown) => { + log.warn(`shim dir removal failed for ${id}: ${(err as Error).message}`); + }); + } + + /** + * Runs the provider-ready listeners; one that throws does not skip the rest. + */ + notifyProviderReady(workspaceId: string, provider: FsProvider): void { + for (const listener of this.providerReadyListeners) { + try { + listener(workspaceId, provider); + } catch (error) { + log.warn(`provider-ready listener failed for ${workspaceId}: ${(error as Error).message}`); + } + } + } + + /** Current remote connection statuses for a newly opened renderer. */ + connectionSnapshot(): WorkspaceConnectionChangedEvent[] { + return Array.from(this.connectionStatuses, ([workspaceId, status]) => ({ + workspaceId, + status, + })); + } + + /** + * Broadcasts a workspace connection status only when it actually changes. + */ + broadcastConnectionStatus(workspaceId: string, status: WorkspaceConnectionEventStatus): void { + if (this.connectionStatuses.get(workspaceId) === status) { + return; + } + this.connectionStatuses.set(workspaceId, status); + this.broadcastFn("workspace", "connectionChanged", { workspaceId, status }); + } + + /** + * Forwards agent bootstrap progress events to the renderer. + * Scoped by workspaceId, so it covers both the "add new workspace" and "reconnect at app start" flows. + */ + broadcastConnectionProgress(workspaceId: string, event: LspBootstrapProgressEvent): void { + this.broadcastFn("workspace", "connectionProgress", { + workspaceId, + name: event.name, + phase: event.phase, + bytesDone: event.bytesDone, + bytesTotal: event.bytesTotal, + }); + } +} diff --git a/src/main/features/workspace/connection/wsl.ts b/src/main/features/workspace/connection/wsl.ts new file mode 100644 index 000000000..9bb6d4af5 --- /dev/null +++ b/src/main/features/workspace/connection/wsl.ts @@ -0,0 +1,13 @@ +import { createWslChannel, ensureWslAgent, ensureWslLspServer } from "../../../infra/agent/wsl"; + +/** How a WSL workspace's agent is installed, reached, and given language servers. */ +export interface WorkspaceWslTransport { + readonly bootstrap: typeof ensureWslAgent; + readonly createChannel: typeof createWslChannel; + readonly lspBootstrap: typeof ensureWslLspServer; +} +export const defaultWslTransport: WorkspaceWslTransport = { + bootstrap: ensureWslAgent, + createChannel: createWslChannel, + lspBootstrap: ensureWslLspServer, +}; diff --git a/src/main/features/workspace/ipc.ts b/src/main/features/workspace/ipc.ts index 9b6340db0..0130ce398 100644 --- a/src/main/features/workspace/ipc.ts +++ b/src/main/features/workspace/ipc.ts @@ -1,8 +1,8 @@ import { createAbortError, isAbortError, throwIfAborted } from "../../../shared/abort"; import { ipcContract } from "../../../shared/ipc/contract"; import { ipcErr, ipcOk } from "../../../shared/ipc/result"; -import { type SshErrorCode, SshErrorCodeSchema } from "../../../shared/ssh/errors"; -import { AuthCancelledError } from "../../infra/agent/ssh/auth-prompt"; +import type { SshErrorCode } from "../../../shared/ssh/errors"; +import { messageForSshErrorCode } from "../../infra/agent/pipe"; import { type CreateSshChannelOptions, createSshChannel, @@ -14,6 +14,7 @@ import { ensureRemoteAgent, } from "../../infra/agent/ssh/ssh-bootstrap/index"; import { type CallContext, register, validateArgs } from "../../infra/ipc-router"; +import { agentCallError, sshErrorCode } from "../../infra/ipc-router/agent-errors"; import type { SshBrowseSessionRegistry } from "../ssh/browse-session-registry"; import type { WorkspaceManager } from "./manager"; @@ -49,6 +50,7 @@ export function registerWorkspaceChannel( list: (_args: unknown) => { return manager.list(); }, + connectionSnapshot: () => ipcOk(manager.connectionSnapshot()), active: (_args: unknown) => { return manager.getActiveId(); }, @@ -78,7 +80,8 @@ export function registerWorkspaceChannel( // When a browse session id is supplied the user already authenticated // via the SSH directory picker; use the legacy create+claimMaster path // which reuses that session's ControlMaster without a second prompt. - if (createArgs && "location" in createArgs && createArgs.location.kind !== "ssh") { + // The legacy `{ rootPath }` shape is always a local folder. + if (!("location" in createArgs) || createArgs.location.kind !== "ssh") { // Local: commit immediately — no connection step needed. const meta = manager.create(createArgs); return ipcOk(meta); @@ -102,18 +105,8 @@ export function registerWorkspaceChannel( const meta = await manager.createAndConnectSsh(createArgs); return ipcOk(meta); } catch (error) { - // User-initiated cancellation (password dialog closed / signal aborted). - // Return a Result so the router passes through silently — no log. - if (isSshAuthCancellation(error) || isAbortError(error)) { - return ipcErr("cancelled", "SSH authentication cancelled"); - } - // Typed SSH failures (wrong credentials, host unreachable, …) — - // surface as auth-failed so the renderer can show a human message. - const code = sshErrorCodeFromError(error); - if (code) { - return ipcErr("auth-failed", messageForSshErrorCode(code), { code }); - } - // Unexpected bug — rethrow so the router logs it. + const expected = agentCallError(error, "auth-failed"); + if (expected) return expected; throw error; } }, @@ -125,17 +118,15 @@ export function registerWorkspaceChannel( const { id, beforeId, afterId, targetGroup } = validateArgs(c.reorder.args, args); return manager.reorder(id, { beforeId, afterId, targetGroup }); }, - remove: (args: unknown) => { + remove: async (args: unknown) => { const { id } = validateArgs(c.remove.args, args); // remove() is idempotent — a missing workspace is silently ignored. // Return ipcOk so the router passes this through without logging. - manager.remove(id); + await manager.remove(id); return ipcOk(undefined); }, - activate: (args: unknown) => { - const { id } = validateArgs(c.activate.args, args); - return manager.activate(id); - }, + activate: workspaceConnectionHandler(manager, "activate"), + connect: workspaceConnectionHandler(manager, "connect"), testSsh: testSshHandler(dependencies.createSshChannel, dependencies.sshBootstrap), }, listen: { @@ -148,6 +139,24 @@ export function registerWorkspaceChannel( }); } +/** Report connection and selection outcomes through the IPC result envelope. */ +function workspaceConnectionHandler(manager: WorkspaceManager, action: "activate" | "connect") { + return async (args: unknown) => { + const { id } = validateArgs(c[action].args, args); + if (manager.getName(id) === null) { + return ipcErr("not-found", `Workspace not found: ${id}`, { category: "failed" }); + } + try { + await manager[action](id); + return ipcOk(undefined); + } catch (error) { + const expected = agentCallError(error, "connection-failed"); + if (expected) return expected; + throw error; + } + }; +} + /** * Builds the SSH workspace validation handler with an injectable channel * factory so unit tests can exercise lifecycle behavior without OpenSSH. @@ -177,6 +186,7 @@ export function testSshHandler( identityFile: testArgs.identityFile, authMode: testArgs.authMode, remotePath: testArgs.remotePath, + signal, }); disposeBootstrap = bootstrap.dispose; throwIfAborted(signal); @@ -225,65 +235,10 @@ export function testSshHandler( * Converts SSH transport and server failures to the public testSsh result. */ function sshFailureResult(error: unknown): TestSshResult { - const code = sshErrorCodeFromError(error) ?? "ssh.unknown"; + const code = sshErrorCode(error) ?? "ssh.unknown"; return { ok: false, code, message: messageForSshErrorCode(code), }; } - -/** - * Reads a stable SshErrorCode from errors without trusting arbitrary strings. - */ -function sshErrorCodeFromError(error: unknown): SshErrorCode | undefined { - if (typeof error !== "object" || error === null || !("code" in error)) { - return undefined; - } - - const parsed = SshErrorCodeSchema.safeParse((error as { code?: unknown }).code); - return parsed.success ? parsed.data : undefined; -} - -/** - * Maps SshErrorCode values to sanitized messages that never include stderr. - */ -function messageForSshErrorCode(code: SshErrorCode): string { - switch (code) { - case "ssh.connect-failed": - return "SSH connection failed"; - case "ssh.auth-failed": - return "SSH authentication failed"; - case "ssh.auth-cancelled": - return "SSH authentication cancelled"; - case "ssh.session-expired": - return "SSH browse session expired"; - case "ssh.path-not-found": - return "Remote path not found"; - case "server.spawn-failed": - return "Remote agent failed to start"; - case "server.protocol-error": - return "Remote agent protocol error"; - case "server.protocol-version-mismatch": - return "Remote agent protocol version mismatch"; - case "ssh.unknown": - return "SSH workspace validation failed"; - case "transport.unknown": - return "Agent transport failed"; - case "transport.timeout": - return "Agent request timed out"; - } -} - -/** - * Walks the error cause chain to detect user-initiated SSH authentication - * cancellations. AuthCancelledError can be wrapped inside a createSshError - * envelope by auth-pty, so the chain must be traversed recursively. - */ -function isSshAuthCancellation(error: unknown): boolean { - if (error instanceof AuthCancelledError) return true; - if (error instanceof Error && error.cause !== undefined) { - return isSshAuthCancellation(error.cause); - } - return false; -} diff --git a/src/main/features/workspace/manager.ts b/src/main/features/workspace/manager.ts index 527c5e937..3ae2ec5b5 100644 --- a/src/main/features/workspace/manager.ts +++ b/src/main/features/workspace/manager.ts @@ -1,18 +1,18 @@ import { randomUUID } from "node:crypto"; import fs from "node:fs"; import path from "node:path"; -import { createAbortError, isAbortError } from "../../../shared/abort"; import { AgentManifestSchema } from "../../../shared/agent/manifest"; import { createLogger } from "../../../shared/log/main"; import { rootPathFromLocation, - type WorkspaceConnectionEventStatus, + type WorkspaceConnectionChangedEvent, type WorkspaceLocation, WorkspaceLocationSchema, type WorkspaceMeta, type WorkspaceTask, workspaceLocationKey, } from "../../../shared/types/workspace"; +import type { WorkspaceColor } from "../../../shared/types/workspace-color"; import type { AgentChannel } from "../../infra/agent/channel"; import { type CreateLocalChannelOptions, @@ -37,7 +37,6 @@ import { type CreateSshChannelOptions, createSshChannel, type SshChannel, - type SshChannelLifecycleEvent, } from "../../infra/agent/ssh/channel"; import type { SshForwarding } from "../../infra/agent/ssh/forward"; import type { SshControlMaster } from "../../infra/agent/ssh/master"; @@ -54,33 +53,28 @@ import { type SshBootstrapDependencies, } from "../../infra/agent/ssh/ssh-bootstrap/index"; import { absoluteRemotePath } from "../../infra/agent/ssh/ssh-bootstrap/manifest"; -import { createWslChannel, ensureWslAgent, ensureWslLspServer } from "../../infra/agent/wsl"; import { isWindows, loginShell } from "../../infra/platform"; import type { GlobalStorage } from "../../infra/storage/global-storage"; import type { StateService } from "../../infra/storage/state-service"; import type { WorkspaceStorage } from "../../infra/storage/workspace-storage"; import { windowsCliLauncherDir } from "../cli/launchers"; -import { AgentFsProvider } from "../fs/bridge/agent-provider"; -import { createFsProvider } from "../fs/bridge/create-provider"; import type { FsProvider } from "../fs/bridge/provider"; import { agentAliasPath } from "../git/domain/helpers/launcher"; +import { type HookInfo, WorkspaceHooks } from "./connection/hooks"; +import { LocalWorkspaceConnections } from "./connection/local"; +import { RemoteWorkspaceConnections } from "./connection/remote"; +import { + createInitialFsProvider, + createWorkspaceRemovedError, + WorkspaceConnectionState, +} from "./connection/state"; +import { defaultWslTransport, type WorkspaceWslTransport } from "./connection/wsl"; import { WorkspaceContext } from "./context"; -const log = createLogger("workspace"); +export type { HookInfo } from "./connection/hooks"; +export type { WorkspaceWslTransport } from "./connection/wsl"; -/** - * How long a removed SSH workspace's agent has to answer agent.shutdown, - * asked again included: one being reconnected to is asked once the link is - * back, as long as the channel's reconnect queue keeps a call. - */ -const SSH_AGENT_SHUTDOWN_TIMEOUT_MS = 30_000; -/** The pause before agent.shutdown is asked again, after the link dropped under it. */ -const SSH_AGENT_SHUTDOWN_RETRY_MS = 250; -/** - * How long another agent on the host has to stop a removed workspace's - * daemon; it dials that daemon and waits for its own agent.shutdown reply. - */ -const SSH_PEER_DAEMON_STOP_TIMEOUT_MS = 5_000; +const log = createLogger("workspace"); // --------------------------------------------------------------------------- // Broadcast callback type — injected so the manager has no hard import on @@ -89,9 +83,8 @@ const SSH_PEER_DAEMON_STOP_TIMEOUT_MS = 5_000; export type BroadcastFn = (channelName: string, event: string, args: unknown) => void; export type WorkspaceCreateOptions = - | { location: WorkspaceLocation; name?: string; task?: WorkspaceTask } - | { rootPath: string; name?: string; task?: WorkspaceTask }; -type SshWorkspaceLocation = Extract; + | { location: WorkspaceLocation; name?: string; color?: WorkspaceColor; task?: WorkspaceTask } + | { rootPath: string; name?: string; color?: WorkspaceColor; task?: WorkspaceTask }; export type WorkspaceSshChannelFactory = (options: CreateSshChannelOptions) => SshChannel; export type WorkspaceSshBootstrap = ( options: EnsureRemoteAgentOptions, @@ -102,17 +95,6 @@ export type WorkspaceSshLspBootstrap = ( dependencies?: Pick, ) => Promise; export type WorkspaceLocalChannelFactory = (options: CreateLocalChannelOptions) => AgentChannel; -/** How a WSL workspace's agent is installed, reached, and given language servers. */ -export interface WorkspaceWslTransport { - readonly bootstrap: typeof ensureWslAgent; - readonly createChannel: typeof createWslChannel; - readonly lspBootstrap: typeof ensureWslLspServer; -} -const defaultWslTransport: WorkspaceWslTransport = { - bootstrap: ensureWslAgent, - createChannel: createWslChannel, - lspBootstrap: ensureWslLspServer, -}; export type WorkspaceLocalAgentCommandResolver = () => LocalAgentCommand; export type WriteShimFilesFn = (workspaceId: string) => Promise<{ dir: string; @@ -175,119 +157,6 @@ function normalizeWorkspaceUpdate( return partial; } -// --------------------------------------------------------------------------- -// hook.getInfo response type — holds each workspace's hookserver connection info, obtained by pull. -// --------------------------------------------------------------------------- - -/** hookserver connection info the agent returns from the hook.getInfo RPC. */ -export interface HookInfo { - readonly socketPath: string; - readonly token: string; - /** Whether the server takes tabs' own tokens (./tab-token.ts): an older agent does not. */ - readonly tabTokens?: boolean; -} - -/** - * Type guard for the hook.getInfo response. - * A response is valid only when both socketPath and token are strings. - */ -function isHookInfo(value: unknown): value is HookInfo { - if (typeof value !== "object" || value === null) return false; - const v = value as Record; - return typeof v.socketPath === "string" && typeof v.token === "string"; -} - -/** - * Asks a removed workspace's remote agent to exit, then closes its channel - * and connection. Closing them alone leaves the daemon holding the - * workspace's terminals and language servers until its reattach grace ends. - * A channel that is reconnecting holds the request until the link is back; - * one the link drops under (an unstable link's ssh giving up) is asked - * again, all within SSH_AGENT_SHUTDOWN_TIMEOUT_MS: a call's own timeout - * starts only once it leaves the reconnect queue. Resolves whether the - * agent answered. - */ -async function stopSshAgent( - channel: SshChannel, - bootstrap: EnsureRemoteAgentResult | undefined, -): Promise { - const deadline = Date.now() + SSH_AGENT_SHUTDOWN_TIMEOUT_MS; - let timer: NodeJS.Timeout | undefined; - const expired = new Promise<"expired">((resolve) => { - timer = setTimeout(() => resolve("expired"), SSH_AGENT_SHUTDOWN_TIMEOUT_MS); - }); - let stopped = false; - try { - for (;;) { - try { - const answer = await Promise.race([ - channel - .call("agent.shutdown", undefined, { timeoutMs: Math.max(1, deadline - Date.now()) }) - .then(() => "answered" as const), - expired, - ]); - if (answer === "expired") { - log.warn("agent.shutdown got no answer in time"); - break; - } - stopped = true; - break; - } catch (error) { - const retryable = (error as { retryable?: unknown }).retryable === true; - if (!retryable || Date.now() + SSH_AGENT_SHUTDOWN_RETRY_MS >= deadline) { - log.warn(`agent.shutdown failed: ${(error as Error).message}`); - break; - } - await new Promise((resolve) => setTimeout(resolve, SSH_AGENT_SHUTDOWN_RETRY_MS)); - } - } - } finally { - clearTimeout(timer); - } - channel.dispose(); - bootstrap?.dispose?.(); - return stopped; -} - -/** - * Asks the remote agent on `channel` to stop the daemon of another - * workspace on its host, at `root`: one removed while not connected, whose - * daemon would otherwise hold its terminals until its reattach grace ends. - */ -async function stopPeerDaemon(channel: SshChannel, root: string): Promise { - await channel - .call("agent.stopDaemon", { root }, { timeoutMs: SSH_PEER_DAEMON_STOP_TIMEOUT_MS }) - .catch((error: unknown) => { - log.warn(`agent.stopDaemon failed: ${(error as Error).message}`); - }); -} - -/** Whether two remote locations are on one host, as one user, where their daemons share a run folder. */ -function sameRemoteHost(a: WorkspaceLocation, b: WorkspaceLocation): boolean { - if (a.kind === "ssh" && b.kind === "ssh") { - return a.host === b.host && a.user === b.user && (a.port ?? 22) === (b.port ?? 22); - } - return a.kind === "wsl" && b.kind === "wsl" && a.distro === b.distro; -} - -/** Rejects a provider boot whose workspace was removed while it was in flight. */ -function createWorkspaceRemovedError(id: string): Error { - return createAbortError(`workspace ${id} was removed while its agent was starting`); -} - -/** - * Decides whether a `hook.getInfo` response carries the `server.unavailable` code. - * It is a graceful-degrade signal that the agent failed to create the hookserver; the workspace - * must keep booting and run with only the Claude hook feature disabled. - * CodedError and errorFromServerFrame attach a `code` property to the - * Error, so that value is checked. - */ -function isHookUnavailable(err: unknown): boolean { - if (typeof err !== "object" || err === null) return false; - const code = (err as { code?: unknown }).code; - return code === "server.unavailable"; -} - // --------------------------------------------------------------------------- // WorkspaceManager — global singleton, created once in main/index.ts. // --------------------------------------------------------------------------- @@ -297,14 +166,9 @@ export class WorkspaceManager { private readonly workspaceStorage: WorkspaceStorage; private readonly stateService: StateService; private readonly broadcastFn: BroadcastFn; - private readonly sshChannelFactory: WorkspaceSshChannelFactory; private readonly sshBootstrap: WorkspaceSshBootstrap; private readonly sshLspBootstrap: WorkspaceSshLspBootstrap; private readonly wsl: WorkspaceWslTransport; - private readonly localChannelFactory: WorkspaceLocalChannelFactory; - private readonly localAgentCommandResolver: WorkspaceLocalAgentCommandResolver; - private readonly writeShimFiles: WriteShimFilesFn; - private readonly removeShimDir: RemoveShimDirFn; /** * Optional callback invoked by `remove()` before the workspace context is @@ -316,22 +180,11 @@ export class WorkspaceManager { * tries to kill sessions via IPC after the context is already gone. */ private ptySessionCloser: ((workspaceId: string) => void) | null = null; - - /** - * Callback to restore held PTY sessions after a successful re-authentication. - * Injected from main/index.ts after construction to break the circular dep. - * Manager calls this immediately after ensureRemoteProviderReady resolves on a - * reauth path. - */ - private ptyRestoreAfterReauth: ((workspaceId: string) => Promise) | null = null; - - /** - * Callback to release held PTY sessions when re-authentication will not be - * attempted (auth-cancelled, backoff exhausted, non-interactive, ctx gone). - * Injected from main/index.ts after construction to break the circular dep. - * Manager calls this on every terminal failure path that does NOT reauth. - */ - private ptyReleaseHeld: ((workspaceId: string) => void) | null = null; + /** Closes chats on the already-open channel before removal closes it. */ + private chatSessionCloser: + | ((workspaceId: string, channel: AgentChannel | null) => Promise) + | null = null; + private readonly workspaceRemovals = new Map>(); /** * Optional async callback invoked by `remove()` after the PTY sessions are @@ -343,55 +196,13 @@ export class WorkspaceManager { */ private browserCloser: ((workspaceId: string) => Promise) | null = null; - private readonly providerReadyListeners = new Set< - (workspaceId: string, provider: FsProvider) => void - >(); - private readonly contexts = new Map(); - private readonly localChannels = new Map(); - private readonly localProviderReady = new Map>(); - private readonly sshChannels = new Map(); - /** Channels of removed workspaces, kept open until their agent exits. */ - private readonly stoppingSshChannels = new Set(); - /** - * Other daemons a connected agent is stopping, by its channel. Its own - * stop waits for them: its agent exits, and drops them, once it answers. - */ - private readonly peerDaemonStops = new Map>(); - /** - * Stops of removed remote workspaces' daemons, by location. A workspace - * on that location connects once they have ended: the daemon is one per - * folder, so a stop that ends after it connects stops its terminals, and - * a removed workspace's channel still reconnecting takes the daemon over. - */ - private readonly daemonStopsByLocation = new Map>(); - private readonly sshBootstraps = new Map(); - private readonly sshProviderReady = new Map>(); - // ControlMasters handed off from an SSH browse session, keyed by workspace - // id, awaiting their first provider boot. Consumed by startRemoteProvider. - private readonly adoptedSshMasters = new Map(); - private readonly connectionStatuses = new Map(); - // Cache of pull-based hookserver connection info — filled by the hook.getInfo RPC right after channel.ready. - private readonly hookInfoByWorkspace = new Map(); - /** - * Tracks workspaces with an in-flight automatic re-authentication attempt so - * the manager never opens a second prompt for the same workspace concurrently. - * Also prevents racing with a manual "Connect" action. - */ - private readonly reauthInFlight = new Set(); - /** - * Counts consecutive manager-level re-authentication attempts per workspace. - * Reset to 0 on a successful connection. Used for exponential backoff: - * 0 → 0 ms, 1 → 5 000 ms, 2 → 30 000 ms, then give up. - */ - private readonly reauthAttempts = new Map(); - /** - * Pending timers for `degraded-recovered` debounce. - * Clears the "unstable" badge ~1 s after the last heartbeat recovery signal - * to prevent rapid unstable↔connected flapping. - */ - private readonly degradedRecoveryTimers = new Map(); + private readonly connections: WorkspaceConnectionState; + private readonly hooks = new WorkspaceHooks(); + private readonly local: LocalWorkspaceConnections; + private readonly remote: RemoteWorkspaceConnections; private activeId: string | null = null; + private selectionVersion = 0; constructor( globalStorage: GlobalStorage, @@ -411,14 +222,29 @@ export class WorkspaceManager { this.workspaceStorage = workspaceStorage; this.stateService = stateService; this.broadcastFn = broadcastFn; - this.sshChannelFactory = sshChannelFactory; this.sshBootstrap = sshBootstrap; this.sshLspBootstrap = sshLspBootstrap; - this.localChannelFactory = localChannelFactory; - this.localAgentCommandResolver = localAgentCommandResolver; - this.writeShimFiles = writeShimFiles; - this.removeShimDir = removeShimDir; this.wsl = wsl; + this.connections = new WorkspaceConnectionState( + this.contexts, + broadcastFn, + writeShimFiles, + removeShimDir, + ); + this.local = new LocalWorkspaceConnections( + this.connections, + this.hooks, + localChannelFactory, + localAgentCommandResolver, + ); + this.remote = new RemoteWorkspaceConnections( + this.connections, + this.hooks, + globalStorage, + sshChannelFactory, + sshBootstrap, + wsl, + ); } // --------------------------------------------------------------------------- @@ -476,25 +302,13 @@ export class WorkspaceManager { */ close(): void { for (const [id, ctx] of this.contexts) { + this.remote.cancelPending(id); ctx.close(); this.contexts.delete(id); - this.connectionStatuses.delete(id); - } - for (const channel of this.localChannels.values()) { - channel.dispose(); - } - this.localChannels.clear(); - this.localProviderReady.clear(); - for (const channel of this.sshChannels.values()) { - channel.dispose(); + this.connections.connectionStatuses.delete(id); } - this.sshChannels.clear(); - this.sshBootstraps.clear(); - this.sshProviderReady.clear(); - for (const master of this.adoptedSshMasters.values()) { - master.dispose(); - } - this.adoptedSshMasters.clear(); + this.local.close(); + this.remote.close(); this.globalStorage.close(); } @@ -522,6 +336,11 @@ export class WorkspaceManager { return null; } + /** Current remote connection statuses for a newly opened renderer. */ + connectionSnapshot(): WorkspaceConnectionChangedEvent[] { + return this.connections.connectionSnapshot(); + } + getActiveId(): string | null { return this.activeId; } @@ -543,9 +362,12 @@ export class WorkspaceManager { * Returns the ready workspace-scoped agent channel, booting it if needed. */ async getAgentChannel(id: string): Promise { + if (this.workspaceRemovals.has(id)) throw createWorkspaceRemovedError(id); const ctx = this.requireContext(id); await this.ensureProviderReady(ctx); - const channel = this.localChannels.get(id) ?? this.sshChannels.get(id); + if (this.workspaceRemovals.has(id) || !this.connections.isLiveContext(id, ctx)) + throw createWorkspaceRemovedError(id); + const channel = this.local.localChannels.get(id) ?? this.remote.sshChannels.get(id); if (!channel) { throw new Error(`agent channel not available for workspace: ${id}`); } @@ -582,7 +404,16 @@ export class WorkspaceManager { * Returns null before ensureProviderReady completes or when the RPC failed. */ getHookInfo(workspaceId: string): HookInfo | null { - return this.hookInfoByWorkspace.get(workspaceId) ?? null; + return this.hooks.getHookInfo(workspaceId); + } + + /** Returns the hooks for the channel about to start a chat or terminal. */ + getSpawnHookInfo(workspaceId: string, channel: AgentChannel): Promise { + return this.local.getSpawnHookInfo( + workspaceId, + channel, + () => this.local.localChannels.get(workspaceId) ?? this.remote.sshChannels.get(workspaceId), + ); } /** @@ -605,7 +436,7 @@ export class WorkspaceManager { if (isWindows()) return windowsCliLauncherDir(); return getAgentBinDir(); } - const bootstrap = this.sshBootstraps.get(workspaceId); + const bootstrap = this.remote.sshBootstraps.get(workspaceId); if (!bootstrap) return null; return bootstrap.remoteBinDir; } @@ -631,7 +462,7 @@ export class WorkspaceManager { : null; return alias ?? getAgentBinaryPath() ?? null; } - const bootstrap = this.sshBootstraps.get(workspaceId); + const bootstrap = this.remote.sshBootstraps.get(workspaceId); if (!bootstrap) return null; const { remoteBinDir, platform } = bootstrap; const binaryName = resolveRemoteAgentBinaryName(platform); @@ -658,7 +489,7 @@ export class WorkspaceManager { if (meta.location.kind === "local") { return shimDir(workspaceId); } - const bootstrap = this.sshBootstraps.get(workspaceId); + const bootstrap = this.remote.sshBootstraps.get(workspaceId); if (!bootstrap) return null; return bootstrap.remoteShimDir ?? null; } @@ -686,7 +517,7 @@ export class WorkspaceManager { if (isWindows()) return null; return loginShell(); } - const bootstrap = this.sshBootstraps.get(workspaceId); + const bootstrap = this.remote.sshBootstraps.get(workspaceId); if (!bootstrap) return null; return bootstrap.remoteShell ?? null; } @@ -707,13 +538,13 @@ export class WorkspaceManager { const meta = this.contexts.get(workspaceId)?.getMeta(); if (!meta) return null; if (meta.location.kind === "local") return worktreesDir(); - const bootstrap = this.sshBootstraps.get(workspaceId); + const bootstrap = this.remote.sshBootstraps.get(workspaceId); if (!bootstrap) return null; return absoluteRemotePath(bootstrap.remoteHome, `${REMOTE_AGENT_ROOT}/worktrees`); } async tryGetAgentChannel(id: string): Promise { - if (!this.contexts.has(id)) return null; + if (!this.contexts.has(id) || this.workspaceRemovals.has(id)) return null; return this.getAgentChannel(id); } @@ -727,9 +558,9 @@ export class WorkspaceManager { const ctx = this.contexts.get(id); if (!ctx) return null; if (ctx.getMeta().location.kind === "local") return this.getAgentChannel(id); - const status = this.connectionStatuses.get(id); + const status = this.connections.connectionStatuses.get(id); if (status !== "connected" && status !== "unstable") return null; - return this.sshChannels.get(id) ?? null; + return this.remote.sshChannels.get(id) ?? null; } /** @@ -740,8 +571,8 @@ export class WorkspaceManager { getSshForwarding(id: string): SshForwarding | null { const location = this.contexts.get(id)?.getMeta().location; if (location?.kind !== "ssh") return null; - const status = this.connectionStatuses.get(id); - const bootstrap = this.sshBootstraps.get(id); + const status = this.connections.connectionStatuses.get(id); + const bootstrap = this.remote.sshBootstraps.get(id); const connected = status === "connected" || status === "unstable"; const { host, user, port, identityFile } = location; return { @@ -774,8 +605,8 @@ export class WorkspaceManager { restoreAfterReauth: (workspaceId: string) => Promise, releaseHeld: (workspaceId: string) => void, ): void { - this.ptyRestoreAfterReauth = restoreAfterReauth; - this.ptyReleaseHeld = releaseHeld; + this.remote.ptyRestoreAfterReauth = restoreAfterReauth; + this.remote.ptyReleaseHeld = releaseHeld; } /** @@ -799,10 +630,7 @@ export class WorkspaceManager { * attached to the old provider never hear from it. */ onProviderReady(listener: (workspaceId: string, provider: FsProvider) => void): () => void { - this.providerReadyListeners.add(listener); - return () => { - this.providerReadyListeners.delete(listener); - }; + return this.connections.onProviderReady(listener); } // --------------------------------------------------------------------------- @@ -902,7 +730,7 @@ export class WorkspaceManager { name, location, rootPath, - colorTone: "default", + color: opts.color ?? "auto", pinned: false, lastOpenedAt: new Date().toISOString(), tabs: [], @@ -928,8 +756,7 @@ export class WorkspaceManager { * the provider boots; the master is consumed by startRemoteProvider. */ adoptSshControlMaster(workspaceId: string, master: SshControlMaster): void { - this.adoptedSshMasters.get(workspaceId)?.dispose(); - this.adoptedSshMasters.set(workspaceId, master); + this.remote.adoptSshControlMaster(workspaceId, master); } update(id: string, partial: Partial>): WorkspaceMeta { @@ -1073,17 +900,31 @@ export class WorkspaceManager { return this.update(id, { lastOpenedAt: new Date().toISOString() }); } - remove(id: string): void { + /** Installs chat cleanup without coupling the workspace manager to the chat host. */ + setChatSessionCloser( + closer: (workspaceId: string, channel: AgentChannel | null) => Promise, + ): void { + this.chatSessionCloser = closer; + } + + /** Removes the workspace and waits for its chats to close on its current agent. */ + remove(id: string): Promise { + const pending = this.workspaceRemovals.get(id); + if (pending) return pending; const ctx = this.contexts.get(id); - if (!ctx) { - return; - } + if (!ctx) return Promise.resolve(); + const removal = this.removeWorkspace(id, ctx).finally(() => this.workspaceRemovals.delete(id)); + this.workspaceRemovals.set(id, removal); + return removal; + } + /** Tears down the removed workspace's sessions before releasing its resources. */ + private async removeWorkspace(id: string, ctx: WorkspaceContext): Promise { // A workspace's tasks go with it. Their worktrees stay on disk; git // forgets one once its folder is deleted (`git worktree prune`). - for (const task of this.list()) { - if (task.task?.parentId === id) this.remove(task.id); - } + const taskRemovals = this.list() + .filter((task) => task.task?.parentId === id) + .map((task) => this.remove(task.id)); // Step 1 — terminate all PTY sessions for this workspace *before* the // context is deleted. This prevents the renderer's post-removal pty.kill @@ -1092,6 +933,20 @@ export class WorkspaceManager { // The PTY host emits pty.exit events for each live session so the // renderer's dead-terminal banner fires without waiting for IPC. this.ptySessionCloser?.(id); + this.remote.cancelPending(id); + if (this.chatSessionCloser) { + const status = this.connections.connectionStatuses.get(id); + const channel = + this.local.localChannels.get(id) ?? + (status === "connected" || status === "unstable" + ? this.remote.sshChannels.get(id) + : undefined); + const closingChats = this.chatSessionCloser(id, channel ?? null).catch((error: unknown) => { + log.warn(`chat cleanup failed for removed workspace ${id}: ${String(error)}`); + }); + // Start the parent's close while child workspaces close their chats. + await Promise.all([...taskRemovals, closingChats]); + } // Step 1b — destroy all browser views for this workspace and schedule // clearStorageData. View destroys happen synchronously inside the async @@ -1103,50 +958,16 @@ export class WorkspaceManager { }); } - // Step 1c — a removed workspace's remote agent will not be dialed again, - // so it is asked to exit. Its channel stays open for that, past the - // provider teardown in ctx.close(), and after the daemons it is - // stopping for others. One reconnecting is asked once its link is back. - // One not connected, or whose agent cannot be reached, is stopped by - // another agent on its host, if one is connected. - const sshChannel = this.sshChannels.get(id); - const status = this.connectionStatuses.get(id); - if ( - sshChannel && - (status === "connected" || status === "unstable" || status === "reconnecting") - ) { - this.stoppingSshChannels.add(sshChannel); - const bootstrap = this.sshBootstraps.get(id); - const meta = ctx.getMeta(); - const stop = async () => { - if (!(await stopSshAgent(sshChannel, bootstrap))) this.stopDaemonThroughPeer(meta); - }; - const pending = this.peerDaemonStops.get(sshChannel); - const stopping = (pending ? pending.then(stop) : stop()).finally(() => { - this.stoppingSshChannels.delete(sshChannel); - }); - this.trackDaemonStop(meta.location, stopping); - } else { - this.stopDaemonThroughPeer(ctx.getMeta()); - } + this.remote.stopRemovedAgent(id, ctx); // Step 2 — dispose the workspace storage handle and agent channels. ctx.close(); this.contexts.delete(id); - this.localChannels.get(id)?.dispose(); - this.localChannels.delete(id); - this.localProviderReady.delete(id); - this.sshChannels.get(id)?.dispose(); - this.sshChannels.delete(id); - this.sshBootstraps.delete(id); - this.sshProviderReady.delete(id); - // An adopted master never consumed by a provider boot would otherwise - // leak its ssh process. - this.adoptedSshMasters.get(id)?.dispose(); - this.adoptedSshMasters.delete(id); + this.local.remove(id); + this.remote.remove(id); // The hookserver connection info is deleted together with the workspace. - this.hookInfoByWorkspace.delete(id); - this.removeShimDirQuietly(id); + this.hooks.clear(id); + this.connections.removeShimDirQuietly(id); this.globalStorage.removeWorkspace(id); if (this.activeId === id) { @@ -1160,89 +981,28 @@ export class WorkspaceManager { // point PTY sessions are already gone so the renderer's cleanup handlers // arrive after the fact — idempotent, not a race. this.broadcastFn("workspace", "removed", { id }); - this.connectionStatuses.delete(id); + this.connections.connectionStatuses.delete(id); } /** - * Asks another connected agent on the removed remote workspace's host — - * its parent's first — to stop that workspace's daemon, which may still - * run its terminals from an earlier connection. + * Record a user's selection before connecting. Readiness can finish in + * any order, so only the latest selection can update recency afterward. */ - private stopDaemonThroughPeer(meta: WorkspaceMeta): void { - const { location } = meta; - if (location.kind === "local") return; - const channel = this.connectedPeerChannel(meta); - if (!channel) return; - const stop = stopPeerDaemon(channel, location.remotePath); - this.trackDaemonStop(location, stop); - const pending = this.peerDaemonStops.get(channel); - const all = pending ? Promise.all([pending, stop]).then(() => {}) : stop; - this.peerDaemonStops.set(channel, all); - void all.finally(() => { - if (this.peerDaemonStops.get(channel) === all) this.peerDaemonStops.delete(channel); - }); - } - - /** Notes a stop of the daemon at `location`, which a new connection there waits for. */ - private trackDaemonStop(location: WorkspaceLocation, stop: Promise): void { - const key = workspaceLocationKey(location); - const pending = this.daemonStopsByLocation.get(key); - const all = (pending ? Promise.all([pending, stop]) : stop).then( - () => {}, - () => {}, - ); - this.daemonStopsByLocation.set(key, all); - void all.then(() => { - if (this.daemonStopsByLocation.get(key) === all) this.daemonStopsByLocation.delete(key); - }); - } - - /** - * Waits for the stops of a removed workspace's daemon at `location`, one - * tracked meanwhile (a peer asked after its own agent could not be - * reached) included. - */ - private async daemonStopped(location: WorkspaceLocation): Promise { - const key = workspaceLocationKey(location); - for (;;) { - const pending = this.daemonStopsByLocation.get(key); - if (!pending) return; - await pending; - } - } + async activate(id: string): Promise { + const ctx = this.requireContext(id); + this.stateService.setState({ lastActiveWorkspaceId: id }); + this.activeId = id; + const version = ++this.selectionVersion; - /** The channel of a connected workspace on `meta`'s host other than its own daemon's. */ - private connectedPeerChannel(meta: WorkspaceMeta): SshChannel | undefined { - const { location } = meta; - if (location.kind === "local") return undefined; - const ids = [meta.task?.parentId, ...this.contexts.keys()]; - for (const id of ids) { - if (id === undefined || id === meta.id) continue; - const other = this.contexts.get(id)?.getMeta().location; - const channel = this.sshChannels.get(id); - const status = this.connectionStatuses.get(id); - if (!other || other.kind === "local" || !channel) continue; - if (status !== "connected" && status !== "unstable") continue; - if (this.stoppingSshChannels.has(channel)) continue; - if (!sameRemoteHost(other, location) || other.remotePath === location.remotePath) continue; - return channel; + await this.ensureProviderReady(ctx); + if (version === this.selectionVersion && this.contexts.get(id) === ctx) { + this.touchLastOpened(id); } - return undefined; } - async activate(id: string): Promise { - const ctx = this.contexts.get(id); - if (!ctx) { - throw new Error(`workspace not found: ${id}`); - } - - await this.ensureProviderReady(ctx); - this.activeId = id; - this.stateService.setState({ lastActiveWorkspaceId: id }); - // Explicit activation counts as "opening" the workspace — bump recency so - // it sorts to the top of the list on the next load. Startup restoration - // does not route through activate(), so it never reorders the list. - this.touchLastOpened(id); + /** Bring a workspace online for its existing views without changing selection. */ + async connect(id: string): Promise { + await this.ensureProviderReady(this.requireContext(id)); } async ensureRemoteLspServer( @@ -1280,7 +1040,7 @@ export class WorkspaceManager { return { binaryPath: result.binaryPath, args: result.args }; } - const bootstrap = this.sshBootstraps.get(workspaceId); + const bootstrap = this.remote.sshBootstraps.get(workspaceId); const result = await this.sshLspBootstrap( { host: refreshedMeta.location.host, @@ -1306,684 +1066,10 @@ export class WorkspaceManager { private async ensureProviderReady(ctx: WorkspaceContext): Promise { const meta = ctx.getMeta(); if (meta.location.kind === "local") { - await this.ensureLocalProviderReady(ctx); - return; - } - await this.ensureRemoteProviderReady(ctx); - } - - /** - * Starts the local agent and wires the context only after the ready handshake. - */ - private async ensureLocalProviderReady(ctx: WorkspaceContext): Promise { - const meta = ctx.getMeta(); - if (meta.location.kind !== "local") { - return; - } - - const pending = this.localProviderReady.get(meta.id); - if (pending) { - await pending; - return; - } - - const ready = this.startLocalProvider(ctx, meta); - this.localProviderReady.set(meta.id, ready); - try { - await ready; - } catch (error) { - if (this.localProviderReady.get(meta.id) === ready) { - this.localProviderReady.delete(meta.id); - } - throw error; - } - } - - /** - * Owns the explicit local boot sequence: spawn → ready → context provider. - */ - private async startLocalProvider(ctx: WorkspaceContext, meta: WorkspaceMeta): Promise { - if (meta.location.kind !== "local") { - return; - } - - const command = this.localAgentCommandResolver(); - const channel = this.localChannelFactory({ ...command, rootPath: meta.location.rootPath }); - this.localChannels.set(meta.id, channel); - const disposeLifecycleListener = channel.onLifecycle((event) => { - this.handleLocalChannelLifecycle(meta.id, channel, event); - }); - - try { - await channel.ready; - } catch (error) { - disposeLifecycleListener(); - if (this.localChannels.get(meta.id) === channel) { - this.localChannels.delete(meta.id); - } - channel.dispose(); - ctx.setFsProvider(createInitialFsProvider(meta)); - throw error; - } - - // Right after channel.ready, pull hook.getInfo and cache the hookserver connection info. - // The agent waits at most 5s internally, so a response always arrives within the 30s default timeout. - // - // The server.unavailable code is a graceful-degrade signal that the agent failed to create the - // hookserver — boot continues with hookInfo left empty instead of killing the whole workspace. - // (Same as the push-era behavior: without a hookserver only the Claude hook feature is off and the workspace is fine.) - try { - const raw = await channel.call("hook.getInfo", {}); - if (!isHookInfo(raw)) { - throw new Error("hook.getInfo returned no socketPath or token"); - } - this.hookInfoByWorkspace.set(meta.id, raw); - } catch (err) { - if (isHookUnavailable(err)) { - log.warn( - `hookserver unavailable for ${meta.id}; Claude Code hook integration disabled for this session.`, - ); - this.hookInfoByWorkspace.delete(meta.id); - } else { - disposeLifecycleListener(); - if (this.localChannels.get(meta.id) === channel) { - this.localChannels.delete(meta.id); - } - channel.dispose(); - ctx.setFsProvider(createInitialFsProvider(meta)); - log.warn(`agent hook setup failed for ${meta.id}: ${(err as Error).message}`); - throw new Error(`Agent hook setup failed: ${(err as Error).message}`, { cause: err }); - } - } - - // Write the PTY shim files into the workspace-specific directory. - // On failure only the PATH priority breaks (the hook itself works via the absolute path in settings.json), - // so warn gracefully and continue booting. - try { - await this.writeShimFiles(meta.id); - } catch (shimErr) { - log.warn( - `shim file write failed for ${meta.id}; shell PATH priority may be degraded: ${(shimErr as Error).message}`, - ); - } - - if (!this.isLiveContext(meta.id, ctx)) { - disposeLifecycleListener(); - channel.dispose(); - // remove() already deleted the shim dir; this boot rewrote it. - this.removeShimDirQuietly(meta.id); - throw createWorkspaceRemovedError(meta.id); - } - - const provider = new AgentFsProvider("local", channel, { disposeChannel: true }); - ctx.setFsProvider(provider, () => { - disposeLifecycleListener(); - provider.dispose(); - if (this.localChannels.get(meta.id) === channel) { - this.localChannels.delete(meta.id); - } - this.localProviderReady.delete(meta.id); - }); - this.notifyProviderReady(meta.id, provider); - } - - /** - * Lazily connects one SSH or WSL channel per workspace and injects it into - * the context. - */ - private async ensureRemoteProviderReady(ctx: WorkspaceContext): Promise { - const meta = ctx.getMeta(); - if (meta.location.kind === "local") { - return; - } - - const pending = this.sshProviderReady.get(meta.id); - if (pending) { - await pending; + await this.local.ensureLocalProviderReady(ctx); return; } - - const ready = this.startRemoteProvider(ctx, meta); - this.sshProviderReady.set(meta.id, ready); - try { - await ready; - } catch (error) { - if (this.sshProviderReady.get(meta.id) === ready) { - this.sshProviderReady.delete(meta.id); - } - throw error; - } - } - - /** - * Owns the explicit remote boot sequence: bootstrap → spawn → ready → - * context provider. A WSL workspace takes it like an SSH one; only the - * bootstrap and the channel's transport differ. - */ - private async startRemoteProvider(ctx: WorkspaceContext, meta: WorkspaceMeta): Promise { - const location = meta.location; - if (location.kind === "local") { - return; - } - - this.broadcastConnectionStatus(meta.id, "connecting"); - // A workspace removed and added again connects once its old daemon has - // been stopped, not to it as that stop ends. - await this.daemonStopped(location); - // A workspace created from a browse session inherits that session's - // already-authenticated ControlMaster; reusing its socket lets bootstrap - // skip the interactive auth round entirely (no second password prompt). - const adoptedMaster = this.adoptedSshMasters.get(meta.id); - this.adoptedSshMasters.delete(meta.id); - let bootstrap: EnsureRemoteAgentResult; - const onProgress = (event: LspBootstrapProgressEvent) => - this.broadcastConnectionProgress(meta.id, event); - try { - bootstrap = - location.kind === "wsl" - ? await this.wsl.bootstrap( - { - distro: location.distro, - remotePath: location.remotePath, - cachedRemoteArch: location.remoteArch, - workspaceId: meta.id, - }, - { onProgress }, - ) - : await this.sshBootstrap( - { - host: location.host, - user: location.user, - port: location.port, - identityFile: location.identityFile, - authMode: location.authMode, - remotePath: location.remotePath, - cachedRemoteArch: location.remoteArch, - controlPath: adoptedMaster?.controlPath, - // Pass workspaceId so the bootstrap also uploads the shim rc - // files (`.zshrc`/`.zshenv`/`bashrc`) into the remote's shared - // `~/.nexus-code/shim/shared/`, making them available to the - // remote PTY's zsh `ZDOTDIR` / bash `--rcfile` activation. - workspaceId: meta.id, - }, - { onProgress }, - ); - } catch (error) { - // Bootstrap failed before any channel existed. Release the adopted - // master (we own it now) and surface the error state instead of - // leaving the renderer stuck on "connecting". - this.broadcastConnectionStatus(meta.id, "error"); - adoptedMaster?.dispose(); - throw error; - } - // ensureRemoteAgent only returns a dispose handle for a master it - // authenticated itself. When we supplied an adopted master, wire its - // dispose in so the existing teardown paths release that socket too. - if (adoptedMaster && !bootstrap.dispose) { - bootstrap = { ...bootstrap, dispose: () => adoptedMaster.dispose() }; - } - // The bootstrap can span a password prompt and an agent upload; the - // workspace may have been removed meanwhile, and nothing would dispose a - // channel or master created for it from here on. - if (!this.isLiveContext(meta.id, ctx)) { - bootstrap.dispose?.(); - throw createWorkspaceRemovedError(meta.id); - } - this.sshBootstraps.set(meta.id, bootstrap); - let providerMeta = meta; - if (!location.remoteArch) { - providerMeta = { - ...meta, - location: - location.kind === "wsl" - ? { ...location, remoteArch: { os: "linux", arch: bootstrap.platform.arch } } - : { ...location, remoteArch: bootstrap.platform }, - }; - this.globalStorage.updateWorkspace(meta.id, { location: providerMeta.location }); - ctx.setMeta(providerMeta); - this.broadcastFn("workspace", "changed", providerMeta); - } - const channel = - location.kind === "wsl" - ? this.wsl.createChannel({ - distro: location.distro, - remoteCommand: bootstrap.remoteCommand, - }) - : this.sshChannelFactory( - sshChannelOptionsFromLocation( - { ...location, remoteArch: bootstrap.platform }, - bootstrap.remoteCommand, - bootstrap.controlPath, - ), - ); - this.sshChannels.set(meta.id, channel); - const disposeLifecycleListener = channel.onLifecycle((event) => { - this.handleSshChannelLifecycle(meta.id, channel, event); - }); - - try { - await channel.ready; - } catch (error) { - // channel.ready failed: bootstrap already succeeded and transferred - // ownership of the ControlMaster to us, so we must dispose both - // bootstrap (ControlMaster) and channel here before re-throwing. - disposeLifecycleListener(); - this.broadcastConnectionStatus(meta.id, "error"); - if (this.sshChannels.get(meta.id) === channel) { - this.sshChannels.delete(meta.id); - } - this.sshBootstraps.delete(meta.id); - bootstrap.dispose?.(); - channel.dispose(); - ctx.setFsProvider(createInitialFsProvider(ctx.getMeta())); - if (isAbortError(error) && this.sshChannels.get(meta.id) !== channel) { - throw error; - } - throw error; - } - - // Right after channel.ready, pull hook.getInfo and cache the hookserver connection info. - // Latency may be higher over SSH, but the 30s default timeout is enough. - // - // The server.unavailable code is a graceful-degrade signal that the agent failed to create the - // hookserver. On an SSH remote the socket path is more likely than locally to hit the length - // limit (104 chars on macOS), so graceful handling matters especially. - try { - const raw = await channel.call("hook.getInfo", {}); - if (!isHookInfo(raw)) { - throw new Error("hook.getInfo returned no socketPath or token"); - } - this.hookInfoByWorkspace.set(meta.id, raw); - } catch (err) { - if (isHookUnavailable(err)) { - log.warn( - `hookserver unavailable for ${meta.id}; Claude Code hook integration disabled for this session.`, - ); - this.hookInfoByWorkspace.delete(meta.id); - } else { - disposeLifecycleListener(); - this.broadcastConnectionStatus(meta.id, "error"); - if (this.sshChannels.get(meta.id) === channel) { - this.sshChannels.delete(meta.id); - } - this.sshBootstraps.delete(meta.id); - bootstrap.dispose?.(); - channel.dispose(); - ctx.setFsProvider(createInitialFsProvider(ctx.getMeta())); - log.warn(`agent hook setup failed for ${meta.id}: ${(err as Error).message}`); - throw new Error(`Agent hook setup failed: ${(err as Error).message}`, { cause: err }); - } - } - - // Write the PTY shim files into the workspace-specific directory. - // An SSH workspace's shim serves local processes — its purpose is not remote PATH control but - // injecting env/args when a local PTY spawns. On failure, warn gracefully. - try { - await this.writeShimFiles(meta.id); - } catch (shimErr) { - log.warn( - `shim file write failed for ${meta.id}; shell PATH priority may be degraded: ${(shimErr as Error).message}`, - ); - } - - if (!this.isLiveContext(meta.id, ctx)) { - disposeLifecycleListener(); - channel.dispose(); - bootstrap.dispose?.(); - // remove() already deleted the shim dir; this boot rewrote it. - this.removeShimDirQuietly(meta.id); - throw createWorkspaceRemovedError(meta.id); - } - - const provider = createFsProvider(providerMeta, channel); - ctx.setFsProvider(provider, () => { - disposeLifecycleListener(); - // remove() closes a channel it is stopping once the agent has exited. - if (!this.stoppingSshChannels.has(channel)) { - channel.dispose(); - bootstrap.dispose?.(); - } - if (this.sshChannels.get(meta.id) === channel) { - this.sshChannels.delete(meta.id); - } - this.sshBootstraps.delete(meta.id); - this.sshProviderReady.delete(meta.id); - // A terminal status set just before teardown says why the provider - // went away; "disconnected" would hide the error or session-expired UI. - const status = this.connectionStatuses.get(meta.id); - if (status !== "error" && status !== "held-then-expired") { - this.broadcastConnectionStatus(meta.id, "disconnected"); - } - }); - this.broadcastConnectionStatus(meta.id, "connected"); - this.notifyProviderReady(meta.id, provider); - } - - /** - * True while `ctx` is still the open context for `id`, i.e. the workspace - * was not removed during an await in a provider boot. - */ - private isLiveContext(id: string, ctx: WorkspaceContext): boolean { - return this.contexts.get(id) === ctx; - } - - /** Deletes the workspace's PTY shim directory in the background. */ - private removeShimDirQuietly(id: string): void { - this.removeShimDir(id).catch((err: unknown) => { - log.warn(`shim dir removal failed for ${id}: ${(err as Error).message}`); - }); - } - - /** - * Runs the provider-ready listeners; one that throws does not skip the rest. - */ - private notifyProviderReady(workspaceId: string, provider: FsProvider): void { - for (const listener of this.providerReadyListeners) { - try { - listener(workspaceId, provider); - } catch (error) { - log.warn(`provider-ready listener failed for ${workspaceId}: ${(error as Error).message}`); - } - } - } - - /** - * Broadcasts a workspace connection status only when it actually changes. - */ - private broadcastConnectionStatus( - workspaceId: string, - status: WorkspaceConnectionEventStatus, - ): void { - if (this.connectionStatuses.get(workspaceId) === status) { - return; - } - this.connectionStatuses.set(workspaceId, status); - this.broadcastFn("workspace", "connectionChanged", { workspaceId, status }); - } - - /** - * Forwards agent bootstrap progress events to the renderer. - * Scoped by workspaceId, so it covers both the "add new workspace" and "reconnect at app start" flows. - */ - private broadcastConnectionProgress(workspaceId: string, event: LspBootstrapProgressEvent): void { - this.broadcastFn("workspace", "connectionProgress", { - workspaceId, - name: event.name, - phase: event.phase, - bytesDone: event.bytesDone, - bytesTotal: event.bytesTotal, - }); - } - - /** - * Handles terminal SSH channel lifecycle events and restores the inert SSH provider. - * - * Reattach-aware transitions: - * - `degraded`: 1 missed heartbeat → broadcast "unstable" (workspaceIsOnline stays true). - * - `degraded-recovered`: heartbeat restored → debounce ~1 s then broadcast "connected" - * to prevent rapid unstable↔connected flapping. - * - `reconnecting`: transient — channel may yet recover; keep references, broadcast - * "reconnecting" so the renderer shows the hold overlay. - * - `ready` (epoch-match): session restored — broadcast "connected" and clear unstable. - * - `held-then-expired`: daemon replaced — broadcast "held-then-expired" so the renderer - * shows "session expired" rather than a generic error. Then tear down the provider. - * - `failure` / `exit`: terminal — if auth-failed on an interactive workspace, attempt - * automatic re-authentication with exponential backoff (plan issue 6). - */ - private handleSshChannelLifecycle( - workspaceId: string, - channel: SshChannel, - event: SshChannelLifecycleEvent, - ): void { - if (this.sshChannels.get(workspaceId) !== channel) { - return; - } - - const ctx = this.contexts.get(workspaceId); - if (!ctx) { - this.sshChannels.delete(workspaceId); - this.sshBootstraps.delete(workspaceId); - return; - } - - // ── Transient quality events ────────────────────────────────────────── - if (event.type === "degraded") { - this.broadcastConnectionStatus(workspaceId, "unstable"); - return; - } - - if (event.type === "degraded-recovered") { - // Cancel any pending debounce timer so rapid transitions don't stack. - const pending = this.degradedRecoveryTimers.get(workspaceId); - if (pending) clearTimeout(pending); - // ~1 s debounce recommended by designer (plan issue 5) to prevent flapping. - const timer = setTimeout(() => { - this.degradedRecoveryTimers.delete(workspaceId); - if (this.sshChannels.get(workspaceId) === channel) { - this.broadcastConnectionStatus(workspaceId, "connected"); - } - }, 1000); - timer.unref?.(); - this.degradedRecoveryTimers.set(workspaceId, timer); - return; - } - - // ── Reconnecting: keep references, update UI ────────────────────────── - if (event.type === "reconnecting") { - // Cancel any pending degraded-recovery timer: we're reconnecting now. - const pendingTimer = this.degradedRecoveryTimers.get(workspaceId); - if (pendingTimer) { - clearTimeout(pendingTimer); - this.degradedRecoveryTimers.delete(workspaceId); - } - this.broadcastConnectionStatus(workspaceId, "reconnecting"); - return; - } - - // ── Epoch-match reattach ────────────────────────────────────────────── - if (event.type === "ready") { - // Channel recovered with the same daemon — sessions are being restored - // by agent-host. Broadcast "connected" to clear the reconnecting overlay. - this.broadcastConnectionStatus(workspaceId, "connected"); - return; - } - - // ── Held-then-expired: daemon replaced ─────────────────────────────── - if (event.type === "held-then-expired") { - this.broadcastConnectionStatus(workspaceId, "held-then-expired"); - this.tearDownSshProvider(workspaceId, ctx); - return; - } - - // ── Terminal events: failure / exit ─────────────────────────────────── - const isAuthFailure = - event.type === "failure" && (event.error as { code?: string }).code === "ssh.auth-failed"; - const meta = ctx.getMeta(); - const isInteractive = meta.location.kind === "ssh" && meta.location.authMode === "interactive"; - - if (event.type === "failure" && !isAuthFailure) { - this.broadcastConnectionStatus(workspaceId, "error"); - } - - this.tearDownSshProvider(workspaceId, ctx); - - // ── Auto re-authentication (plan issue 6) ───────────────────────────── - // Conditions: auth-failed terminal failure + interactive auth + context - // and meta still present (CRITICAL: re-confirm after tearDown) + no - // concurrent reauth in flight. - if (isAuthFailure && isInteractive && !this.reauthInFlight.has(workspaceId)) { - void this.scheduleReauth(workspaceId); - // PTY hold state is preserved — scheduleReauth owns the resolution - // (restoreAfterReauth on success, releaseHeld on all non-reauth exits). - return; - } - - // No reauth attempted: release any held PTY sessions so they don't leak. - this.ptyReleaseHeld?.(workspaceId); - } - - /** - * Tears down SSH provider state after a terminal channel event. - * Shared by `failure`, `exit`, and `held-then-expired` paths. - */ - private tearDownSshProvider(workspaceId: string, ctx: WorkspaceContext): void { - this.sshChannels.delete(workspaceId); - this.sshBootstraps.delete(workspaceId); - this.sshProviderReady.delete(workspaceId); - // Cancel any pending degraded-recovery timer. - const pendingTimer = this.degradedRecoveryTimers.get(workspaceId); - if (pendingTimer) { - clearTimeout(pendingTimer); - this.degradedRecoveryTimers.delete(workspaceId); - } - // Stale hookInfo is invalid once the channel dies — the next boot fills it again. - this.hookInfoByWorkspace.delete(workspaceId); - ctx.setFsProvider(createInitialFsProvider(ctx.getMeta())); - // Clean up the PTY shim directory — fire-and-forget, errors are swallowed as a warn. - this.removeShimDir(workspaceId).catch((err: unknown) => { - log.warn(`shim dir removal failed for ${workspaceId}: ${(err as Error).message}`); - }); - } - - /** - * Schedules (or immediately starts) an automatic SSH re-authentication - * attempt after an auth-failed terminal failure on an interactive workspace. - * - * Backoff schedule (plan issue 6): - * attempt 0 → immediate (0 ms) - * attempt 1 → 5 000 ms - * attempt 2 → 30 000 ms - * attempt 3 → give up, surface error - * - * Guards: - * - `reauthInFlight` prevents concurrent prompts (including manual Connect). - * - ctx/meta re-confirmed inside the scheduled callback (CRITICAL: prevents - * re-booting a workspace that was removed while the timer was pending). - * - ssh.auth-cancelled → disconnected (no retry loop). - */ - private async scheduleReauth(workspaceId: string): Promise { - const attempts = this.reauthAttempts.get(workspaceId) ?? 0; - const BACKOFF_MS = [0, 5_000, 30_000]; - - if (attempts >= BACKOFF_MS.length) { - // Exhausted retries — surface permanent error, release held sessions. - log.warn(`reauth exhausted for ${workspaceId} after ${attempts} attempts`); - this.broadcastConnectionStatus(workspaceId, "error"); - this.reauthAttempts.delete(workspaceId); - this.ptyReleaseHeld?.(workspaceId); - return; - } - - const delayMs = BACKOFF_MS[attempts]; - this.reauthInFlight.add(workspaceId); - - const attemptReauth = async (): Promise => { - // CRITICAL: re-confirm ctx/meta existence before attempting re-boot. - const ctx = this.contexts.get(workspaceId); - if (!ctx) { - this.reauthInFlight.delete(workspaceId); - this.reauthAttempts.delete(workspaceId); - // Workspace gone — release any still-held sessions. - this.ptyReleaseHeld?.(workspaceId); - return; - } - const meta = ctx.getMeta(); - if (meta.location.kind !== "ssh" || meta.location.authMode !== "interactive") { - this.reauthInFlight.delete(workspaceId); - this.reauthAttempts.delete(workspaceId); - this.ptyReleaseHeld?.(workspaceId); - return; - } - - this.reauthAttempts.set(workspaceId, attempts + 1); - try { - await this.ensureRemoteProviderReady(ctx); - // Success — reset attempt counter and trigger PTY session restore. - this.reauthAttempts.delete(workspaceId); - // restoreAfterReauth acquires the new channel, subscribes lifecycle, - // and runs session.list reconcile → replay/exit. Fire-and-forget: - // internal failures are logged inside restoreAfterReauth itself. - this.ptyRestoreAfterReauth?.(workspaceId).catch((err: unknown) => { - log.warn(`restoreAfterReauth failed for ${workspaceId}: ${(err as Error).message}`); - }); - } catch (err) { - // ssh.auth-cancelled: user explicitly cancelled — stop the retry loop - // and release held sessions. - const code = (err as { code?: string }).code; - if (code === "ssh.auth-cancelled") { - log.info(`reauth cancelled by user for ${workspaceId}`); - this.broadcastConnectionStatus(workspaceId, "disconnected"); - this.reauthAttempts.delete(workspaceId); - this.ptyReleaseHeld?.(workspaceId); - } else { - // Other failures (connect error, etc.) handled by the new lifecycle - // event from the fresh channel — don't double-broadcast here. Hold - // state is preserved so the next channel's lifecycle can restore it. - log.warn( - `reauth attempt ${attempts + 1} failed for ${workspaceId}: ${(err as Error).message}`, - ); - } - } finally { - this.reauthInFlight.delete(workspaceId); - } - }; - - if (delayMs === 0) { - void attemptReauth(); - } else { - const timer = setTimeout(() => void attemptReauth(), delayMs); - timer.unref?.(); - } - } - - /** - * Handles terminal local channel lifecycle events and restores the inert provider. - */ - private handleLocalChannelLifecycle( - workspaceId: string, - channel: AgentChannel, - event: SshChannelLifecycleEvent, - ): void { - if (this.localChannels.get(workspaceId) !== channel) { - return; - } - - const ctx = this.contexts.get(workspaceId); - if (!ctx) { - this.localChannels.delete(workspaceId); - this.localProviderReady.delete(workspaceId); - return; - } - - // Teardown is reserved for genuine terminal events (`exit`, `failure`). - // Everything else must keep the provider wired: - // - `reconnecting` is transient — the internal reconnect path recovers - // transparently. - // - `degraded` / `degraded-recovered` are heartbeat-quality signals - // (one late heartbeat fires `degraded`) and `ready` is a successful - // recovery — the channel is alive in all three cases. Before v0.6.1 - // these fell into the teardown branch below, which abandoned a live - // agent WITHOUT disposing it (orphan process keeping every fs/git - // watch) and lazily booted a fresh watch-less agent: push events - // (fs.changed / git.changed) went silent while RPC kept working, and - // existing PTY sessions were stranded on the abandoned channel. - // - `held-then-expired` cannot occur on local channels (no epoch). - // - `disposed` is an intentional teardown whose cleanup already ran via - // the setFsProvider dispose callback. - if (event.type !== "exit" && event.type !== "failure") { - return; - } - - this.localChannels.delete(workspaceId); - this.localProviderReady.delete(workspaceId); - // Stale hookInfo is invalid once the channel dies — the next boot fills it again. - // Keeping it could let a spawn right after reconnect put a dead socket path in the env. - this.hookInfoByWorkspace.delete(workspaceId); - ctx.setFsProvider(createInitialFsProvider(ctx.getMeta())); - // Clean up the PTY shim directory — fire-and-forget, errors are swallowed as a warn. - this.removeShimDir(workspaceId).catch((err: unknown) => { - log.warn(`shim dir removal failed for ${workspaceId}: ${(err as Error).message}`); - }); + await this.remote.ensureRemoteProviderReady(ctx); } } @@ -2000,36 +1086,6 @@ function shouldAutoConnect(meta: WorkspaceMeta): boolean { return meta.location.authMode === "key-only"; } -/** - * Builds an inert provider for unopened workspaces. Activation replaces it - * only after the workspace agent has completed its ready handshake. - */ -function createInitialFsProvider(meta: WorkspaceMeta): AgentFsProvider { - if (meta.location.kind === "local") { - return new AgentFsProvider("local"); - } - return createFsProvider(meta) as AgentFsProvider; -} - -/** - * Builds the SSH channel options used when activating a remote workspace. - */ -function sshChannelOptionsFromLocation( - location: SshWorkspaceLocation, - remoteCommand: string, - controlPath?: string, -): CreateSshChannelOptions { - return { - host: location.host, - user: location.user, - port: location.port, - identityFile: location.identityFile, - authMode: location.authMode, - remoteCommand, - controlPath, - }; -} - /** * Derives the agent binary filename for the given remote platform by reading * the local manifest. The installed binary on the remote follows the same diff --git a/src/main/features/wsl/ipc.ts b/src/main/features/wsl/ipc.ts index 0029990c4..d516f6ae4 100644 --- a/src/main/features/wsl/ipc.ts +++ b/src/main/features/wsl/ipc.ts @@ -3,11 +3,14 @@ // folder. The session joins the SSH browse sessions' registry, so // `ssh.browseSession` and `ssh.closeBrowseSession` serve it unchanged. +import { throwIfAborted } from "../../../shared/abort"; import { ipcContract } from "../../../shared/ipc/contract"; +import { type IpcResult, ipcErr, ipcOk } from "../../../shared/ipc/result"; import type { AgentChannel } from "../../infra/agent/channel"; import type { LspBootstrapProgressEvent } from "../../infra/agent/ssh/ssh-bootstrap/index"; import { createWslChannel, ensureWslAgent, listWslDistros } from "../../infra/agent/wsl"; -import { register, validateArgs } from "../../infra/ipc-router"; +import { type CallContext, register, validateArgs } from "../../infra/ipc-router"; +import { agentCallError } from "../../infra/ipc-router/agent-errors"; import type { SshBrowseSessionRegistry } from "../ssh/browse-session-registry"; import type { BrowseProgressBroadcast } from "../ssh/ipc"; @@ -27,7 +30,7 @@ export function registerWslChannel( const listDistros = dependencies.listDistros ?? listWslDistros; register("wsl", { call: { - listDistros: () => listDistros(), + listDistros: async () => ipcOk(await listDistros()), openBrowseSession: openWslBrowseSessionHandler(registry, broadcast, dependencies), }, listen: {}, @@ -43,10 +46,13 @@ export function openWslBrowseSessionHandler( registry: SshBrowseSessionRegistry, broadcast?: BrowseProgressBroadcast, dependencies: WslChannelDependencies = {}, -): (args: unknown) => Promise<{ sessionId: string; initialPath: string }> { +): ( + args: unknown, + ctx?: CallContext, +) => Promise> { const bootstrap = dependencies.bootstrap ?? ensureWslAgent; const createChannel = dependencies.createChannel ?? createWslChannel; - return async (args) => { + return async (args, ctx) => { const { distro, progressId } = validateArgs(c.openBrowseSession.args, args); const onProgress = progressId && broadcast @@ -62,21 +68,41 @@ export function openWslBrowseSessionHandler( : undefined; let channel: AgentChannel | null = null; + const signal = ctx?.signal; + const disposeChannel = () => { + channel?.dispose(); + channel = null; + }; try { - const result = await bootstrap({ distro, remotePath: "/" }, { onProgress }); + throwIfAborted(signal); + const result = await bootstrap( + { distro, remotePath: "/", ...(signal ? { signal } : {}) }, + { onProgress }, + ); + throwIfAborted(signal); channel = createChannel({ distro, remoteCommand: result.remoteCommand }); + signal?.addEventListener("abort", disposeChannel, { once: true }); await channel.ready; + throwIfAborted(signal); const sessionId = registry.register(channel, null); - return { sessionId, initialPath: result.remoteHome }; + return ipcOk({ sessionId, initialPath: result.remoteHome }); } catch (error) { - channel?.dispose(); - throw new Error(wslFailureMessage(error), { cause: error }); + disposeChannel(); + if (signal?.aborted) return ipcErr("cancelled", "Operation cancelled"); + const expected = agentCallError(error); + if (expected) { + if (expected.kind === "cancelled") return expected; + return { ...expected, domain: undefined, message: wslFailureMessage(error) }; + } + throw error; + } finally { + signal?.removeEventListener("abort", disposeChannel); } }; } /** - * What went wrong, for the dialog: a transport error's generic message hides + * Keeps diagnostic detail in the envelope for logging: the generic message hides * what wsl.exe said (an unknown distribution, WSL not installed), which it * carries as its cause. */ diff --git a/src/main/index.ts b/src/main/index.ts index a31cafff8..97ef83dfc 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,5 +1,5 @@ import path from "node:path"; -import { app, type BrowserWindow, powerMonitor, session } from "electron"; +import { app, autoUpdater, type BrowserWindow, powerMonitor, session } from "electron"; import { EMPTY_AGENT_PROFILES } from "../shared/agent-profiles/profiles"; import { DEFAULT_THEME } from "../shared/design-tokens"; import { createLogger, initMainLogger } from "../shared/log/main"; @@ -10,9 +10,9 @@ import { lspSettingsFromState, } from "../shared/lsp/settings"; import { ListeningPortsResultSchema } from "../shared/pty/ports"; +import { asTaskAgent, chatAgentOf, DEFAULT_TASK_AGENT } from "../shared/sessions/task-agent"; import type { WorkspaceConnectionEventStatus } from "../shared/types/workspace"; import { GIT_STATUS_COALESCE_DEBOUNCE_MS } from "../shared/util/timing-constants"; -import { installErrorSafetyNet } from "./error-safety-net"; import { findProfile, registerAgentProfilesChannel } from "./features/agent-profiles"; import { registerAppStateChannel } from "./features/app-state"; import { @@ -72,6 +72,7 @@ import type { PtyHostHandle } from "./features/pty/types"; import { installWindowsAgentFiles } from "./features/pty/windows-shell-shim"; import { registerRemoteChannel } from "./features/remote"; import { registerSystemChannel } from "./features/shell/ipc"; +import { registerSshAuthPromptIpcChannels, SshAuthPromptHub } from "./features/ssh/auth-prompt"; import { SshBrowseSessionRegistry } from "./features/ssh/browse-session-registry"; import { registerSshBrowseHandlers, registerSshChannel } from "./features/ssh/ipc"; import { systemStatsHandler } from "./features/system-stats"; @@ -82,11 +83,17 @@ import { canUpdateInPlace, createElectronUpdateBackend, } from "./features/updates/electron-update-backend"; -import { appPage, createMainWindow, updateTitleBarOverlays } from "./features/window"; +import { + appPage, + closePersistence, + createMainWindow, + updateTitleBarOverlays, +} from "./features/window"; import { installAppCsp } from "./features/window/app-csp"; import { installAppSessionPermissions } from "./features/window/app-session-permissions"; import { getActiveAppWindow, + getAppWindows, getFocusedAppWindow, hasAppWindow, showAppWindow, @@ -97,16 +104,18 @@ import { registerWorkspaceChannel } from "./features/workspace/ipc"; import { WorkspaceManager } from "./features/workspace/manager"; import { registerWslChannel } from "./features/wsl/ipc"; import { getMainI18n, getMainT, initMainI18n } from "./i18n"; +import { waitForLocalAgentShutdowns } from "./infra/agent/channel/local-channel"; import { NEXUS_AGENT_MODE_ENV } from "./infra/agent/local-agent-resolver"; -import { registerSshAuthPromptIpcChannels, SshAuthPromptHub } from "./infra/agent/ssh/auth-prompt"; import { createSshChannel } from "./infra/agent/ssh/channel"; import { createCachingAuthPromptHandler } from "./infra/agent/ssh/credential-cache"; import { sweepStaleControlDirs } from "./infra/agent/ssh/master"; import { ensureRemoteAgent } from "./infra/agent/ssh/ssh-bootstrap/index"; +import { installErrorSafetyNet } from "./infra/error-safety-net"; import { broadcast, setupRouter } from "./infra/ipc-router"; +import { finishMainShutdown } from "./infra/main-shutdown"; import { isMac, isWindows, loginShell } from "./infra/platform"; import { startDesktopNotifications } from "./infra/platform/desktop-notifications"; -import { syncUserShellPath } from "./infra/shell-path"; +import { syncUserShellPath } from "./infra/platform/shell-path"; import { GlobalStorage } from "./infra/storage/global-storage"; import { StateService } from "./infra/storage/state-service"; import { WorkspaceStorage } from "./infra/storage/workspace-storage"; @@ -128,7 +137,9 @@ if (!app.requestSingleInstanceLock()) { } /** Opens the app window again after every one was closed; set once the app is ready. */ let reopenAppWindow: (() => void) | null = null; +let quitStarted = false; app.on("second-instance", () => { + if (quitStarted) return; if (hasAppWindow()) showAppWindow(); else reopenAppWindow?.(); }); @@ -191,6 +202,7 @@ let chatHost: ChatHost | null = null; let agentProfiles: { dispose(): void } | null = null; let remote: { themeChanged(): void; + forgetWorkspace(workspaceId: string): void; cli: CliRemote; peerFiles: PeerFileReader; dispose(): void; @@ -216,8 +228,8 @@ const sshAuthPromptHub = new SshAuthPromptHub(forwardBroadcast); // Session-scoped credential cache: wraps the hub so successful passwords are // reused on automatic reconnects without re-prompting the user. The cache // invalidates itself on retry=true prompts (SSH "Permission denied, try again"). -const cachedSshAuthPromptHandler = createCachingAuthPromptHandler((prompt) => - sshAuthPromptHub.request(prompt), +const cachedSshAuthPromptHandler = createCachingAuthPromptHandler((prompt, signal) => + sshAuthPromptHub.request(prompt, signal), ); function wrappedBroadcast(channelName: string, event: string, args: unknown): void { @@ -235,12 +247,15 @@ function wrappedBroadcast(channelName: string, event: string, args: unknown): vo // than registering a parallel listener. const removedWorkspaceId = (args as { id: string }).id; portForwards?.removed(removedWorkspaceId); + portWatcher?.forgetWorkspace(removedWorkspaceId); agentFsWatcher?.disposeWorkspace(removedWorkspaceId); gitWatcher?.disposeWorkspace(removedWorkspaceId); gitStatusCoalescer?.cancel(removedWorkspaceId); gitAutofetch?.disposeWorkspace(removedWorkspaceId); gitRegistry?.dispose(removedWorkspaceId); lspHost?.forgetWorkspace(removedWorkspaceId); + notesFeature?.forgetWorkspace(removedWorkspaceId); + remote?.forgetWorkspace(removedWorkspaceId); // Clean up the workspace's turned-off LSP languages so deleted // workspaces don't accumulate in appState indefinitely. const currentState = stateService.getState(); @@ -305,6 +320,7 @@ registerWorkspaceChannel(workspaceManager, { }); registerDialogChannel(); registerAppStateChannel(stateService, { + listWorkspaces: () => workspaceManager.list(), /** * Called synchronously inside the appState.set IPC handler whenever the * renderer persists a new language preference. By the time any renderer @@ -428,7 +444,7 @@ registerSystemChannel({ }, }), }); -registerWindowChannel(); +registerWindowChannel(closePersistence); registerClipboardChannel(); registerPreviewChannel(previewDocuments); @@ -575,7 +591,6 @@ app.whenReady().then(async () => { // is created first, PTY host second, then they are linked here. workspaceManager.setPtySessionCloser((workspaceId) => { agentPtyHost?.closeWorkspaceSessions(workspaceId); - chatHost?.closeWorkspace(workspaceId); }); // Wire held-session restore / release callbacks so manager can drive PTY // session lifecycle across reauth boundaries (plan issue 4 + 6). @@ -740,7 +755,6 @@ app.whenReady().then(async () => { }, }); - // The hookserver connection info is looked up by pull from workspaceManager.getHookInfo(). // A program's own OS notifications (OSC 9/777/99) follow Claude's rules. registerPtyChannel({ agentHost: agentPtyHost, @@ -754,7 +768,11 @@ app.whenReady().then(async () => { ports: portWatcher, forwards, profileEnv: async (workspaceId, profileId) => - (await chatHost?.terminalProfileEnv(workspaceId, profileId)) ?? null, + (await chatHost?.terminalProfileEnv( + workspaceId, + profileId, + chatAgentOf(asTaskAgent(stateService.getState().taskAgent) ?? DEFAULT_TASK_AGENT), + )) ?? null, getDefaultShell: () => stateService.getState().terminalShell, getCustomShells: () => stateService.getState().terminalShells, ...(isWindows() ? { windowsAgentFiles: installWindowsAgentFiles } : {}), @@ -763,12 +781,21 @@ app.whenReady().then(async () => { // Claude tabs: their programs run through the same agents as terminals. chatHost = registerChatChannel({ workspaceManager, + knownChatTabs: (workspaceId) => + (stateService.getState().layoutByWorkspace?.[workspaceId]?.tabs ?? []) + .filter((tab) => tab.type === "claude" || tab.type === "codex") + .map((tab) => tab.id), agentContext: (workspaceId, tabId) => cliFeature?.agentContextForChat(workspaceId, tabId), profile: (id) => findProfile(stateService, id), ...(isWindows() ? { windowsAgentFiles: installWindowsAgentFiles } : {}), - onTurnStopped: (workspaceId, tabId) => claudeSetup.noteTurnStopped(workspaceId, tabId), + onTurnResult: claudeSetup.noteTurnResult, + onTaskEvent: (workspaceId, tabId, message) => + claudeSetup.activity.noteTask(workspaceId, tabId, message), onProgramExit: (workspaceId, tabId) => claudeSetup.noteProgramExit(workspaceId, tabId), }); + workspaceManager.setChatSessionCloser( + (workspaceId, channel) => chatHost?.closeWorkspace(workspaceId, channel) ?? Promise.resolve(), + ); // The host reads the settings at each spawn; the lsp IPC handlers stop // the servers a settings change turns off. @@ -831,7 +858,7 @@ app.whenReady().then(async () => { // With every window closed, as macOS keeps the app running: a click on its // Dock icon, or a second launch, opens one again. reopenAppWindow = () => { - if (hasAppWindow()) return; + if (quitStarted || hasAppWindow()) return; const newWin = createMainWindow(stateService.getState()); wireAutofetchWindowFocus(newWin); // Reuse the existing permission manager with the new window. @@ -856,43 +883,73 @@ app.whenReady().then(async () => { app.on("activate", reopenAppWindow); }); +app.on("before-quit", (event) => { + closePersistence.interceptQuit(event, getAppWindows(), () => app.quit()); +}); +// Native updates close their windows before before-quit and own the replay. +autoUpdater?.on("before-quit-for-update", () => closePersistence.allowUpdaterClose()); + app.on("window-all-closed", () => { - // All renderers are gone — no consumer can use browse sessions any more. - sshBrowseRegistry.dispose(); + // A reopened window can start new sessions with the same reaper. + sshBrowseRegistry.closeAll(); if (!isMac()) { app.quit(); } }); -// Single dispose path on quit. Order: process hosts first (kill children), -// then process-local watchers/coalescers, then the registry that owns repo -// queues, then storage close. Each disposer must be idempotent so this stays -// safe regardless of how far whenReady() progressed. -// // `will-quit`, not `before-quit`: a window can still cancel the quit after // before-quit (unsaved buffers), which would leave the app running with its // agents, terminals and storage already torn down. will-quit fires only // once every window has closed. -app.on("will-quit", () => { - sshBrowseRegistry.dispose(); - cliFeature?.dispose(); - disposeClaudeFeature?.(); - portWatcher?.dispose(); - portForwards?.dispose(); - agentPtyHost?.dispose(); - chatHost?.dispose(); - agentProfiles?.dispose(); - remote?.dispose(); - lspHost?.dispose(); - agentFsWatcher?.dispose(); - gitStatusCoalescer?.clearAll(); - gitWatcher?.dispose(); - gitAutofetch?.dispose(); - void gitHelpersIpc?.dispose(); - gitRegistry?.disposeAll(); - workspaceManager.close(); +let quitReady = false; +app.on("will-quit", (event) => { + if (quitReady) return; + event.preventDefault(); + if (quitStarted) return; + quitStarted = true; + void shutDownMain() + .catch((error) => logger.error("App shutdown failed", error)) + .finally(() => { + quitReady = true; + app.quit(); + }); }); +/** Stops main services and waits for writes and children within the quit budget. */ +async function shutDownMain(): Promise { + let lspShutdown: Promise | undefined; + try { + sshBrowseRegistry.dispose(); + cliFeature?.dispose(); + disposeClaudeFeature?.(); + portWatcher?.dispose(); + portForwards?.dispose(); + agentPtyHost?.dispose(); + chatHost?.dispose(); + agentProfiles?.dispose(); + remote?.dispose(); + notesFeature?.dispose(); + lspShutdown = lspHost?.dispose() ?? undefined; + agentFsWatcher?.dispose(); + gitStatusCoalescer?.clearAll(); + gitWatcher?.dispose(); + gitAutofetch?.dispose(); + gitRegistry?.disposeAll(); + } finally { + await finishMainShutdown( + { + waitForNotes: () => notesFeature?.waitForWrites(), + disposeGitHelpers: () => gitHelpersIpc?.dispose(), + ...(lspShutdown ? { lspShutdown } : {}), + closeWorkspaces: () => workspaceManager.close(), + waitForLocalAgents: waitForLocalAgentShutdowns, + warn: (message) => logger.warn(message), + }, + closePersistence.remainingQuitMs(), + ); + } +} + /** * BrowserWindow focus/blur globally pauses only scheduler due checks. Any * in-flight fetch continues through the repository queue; focus recomputes diff --git a/src/main/infra/agent/channel/local-channel.ts b/src/main/infra/agent/channel/local-channel.ts index 0b7bd1b63..424e3cabc 100644 --- a/src/main/infra/agent/channel/local-channel.ts +++ b/src/main/infra/agent/channel/local-channel.ts @@ -10,9 +10,9 @@ * and tests (ad-hoc). Keeping path resolution outside this module also makes * the channel a thin glue layer that is straightforward to fake in unit tests. * - * Lifecycle mirrors `ssh-channel`: a SIGTERM-then-SIGKILL dispose path, a - * `failure` lifecycle event on spawn or terminal pipe errors, and an `exit` - * event for clean closes that happened after `ready` settled. + * Disposal lets stdin EOF run the local agent's shutdown hooks before the + * hard-kill fallback. Transport failures and clean closes surface through the + * shared channel lifecycle. */ import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process"; import fs from "node:fs"; @@ -25,6 +25,38 @@ import { createReconnectingProcessChannel, } from "./reconnecting-process-channel"; +// Allow the Go host's 75 ms request drain and 3 s of shutdown hooks to finish. +const LOCAL_AGENT_SHUTDOWN_MS = 3_500; +const pendingLocalShutdowns = new Set>(); + +/** Waits for local agents already being disposed, including removed workspaces. */ +export async function waitForLocalAgentShutdowns(): Promise { + await Promise.all(pendingLocalShutdowns); +} + +/** Tracks EOF-driven shutdown until the agent closes or the kill deadline. */ +function finishLocalAgentShutdown(child: ChildProcessWithoutNullStreams): void { + if (child.pid === undefined || child.exitCode !== null || child.signalCode !== null) return; + const shutdown = new Promise((resolve) => { + const finish = () => { + clearTimeout(timer); + child.off("close", finish); + resolve(); + }; + const timer = setTimeout(() => { + try { + child.kill("SIGKILL"); + } finally { + finish(); + } + }, LOCAL_AGENT_SHUTDOWN_MS); + timer.unref?.(); + child.once("close", finish); + }); + pendingLocalShutdowns.add(shutdown); + void shutdown.then(() => pendingLocalShutdowns.delete(shutdown)); +} + export interface CreateLocalChannelOptions { /** Absolute path to the `agent` binary. Caller resolves dev/prod. */ readonly binaryPath: string; @@ -73,6 +105,7 @@ export function createLocalChannel( cwd: options.cwd, env, }), + disposeChild: finishLocalAgentShutdown, // Local stderr is not classified — the binary writes only human-readable // hints (e.g. usage), and terminal failures surface via exit code below. classifyStderr: () => null, diff --git a/src/main/infra/agent/channel/reconnecting-process-channel.ts b/src/main/infra/agent/channel/reconnecting-process-channel.ts index a29ea1733..7467be51d 100644 --- a/src/main/infra/agent/channel/reconnecting-process-channel.ts +++ b/src/main/infra/agent/channel/reconnecting-process-channel.ts @@ -1,7 +1,7 @@ import type { ChildProcessWithoutNullStreams } from "node:child_process"; import { createLogger } from "../../../../shared/log/main"; import type { StderrClassifier } from "../pipe"; -import { createNdjsonPipe, type NdjsonPipe, type SshError } from "../pipe"; +import { AgentLineTooLongError, createNdjsonPipe, type NdjsonPipe, type SshError } from "../pipe"; import { ChannelEventRegistry } from "./event-registry"; import type { AgentCallOptions, @@ -36,6 +36,7 @@ function getChannelLogger(): ReturnType { /** Max chars of a child's stderr tail to embed in one close-diagnostic line. */ const CLOSE_LOG_STDERR_TAIL_CHARS = 300; +const AGENT_READY_TIMEOUT_MS = 30_000; const DISPOSE_KILL_GRACE_MS = 100; const DEFAULT_MAX_PENDING_RECONNECT_CALLS = 32; const DEFAULT_RECONNECT_DELAY_MS = 100; @@ -78,6 +79,8 @@ export interface ChannelCloseContext { export interface ReconnectingProcessChannelOptions { readonly spawn: () => ChildProcessWithoutNullStreams; + /** Starts transport-specific cleanup after stdin has been ended. */ + readonly disposeChild?: (child: ChildProcessWithoutNullStreams) => void; readonly classifyStderr: StderrClassifier; readonly closeError: (wasReady: boolean, context?: ChannelCloseContext) => Error; /** @@ -108,6 +111,7 @@ interface ActiveProcess { ready: boolean; /** The daemon said another connection took it over (AGENT_PREEMPTED_EVENT). */ preempted: boolean; + reconnectAfterClose?: SshError; forceKillTimer: NodeJS.Timeout | null; } @@ -192,7 +196,10 @@ export function createReconnectingProcessChannel( active?.pipe.dispose(); if (active && !active.child.stdin.destroyed) active.child.stdin.end(); if (!terminalError) emitLifecycle({ type: "disposed" }); - if (active) terminateChild(active); + if (active && !active.closed) { + if (options.disposeChild) options.disposeChild(active.child); + else terminateChild(active); + } }, }; @@ -222,8 +229,9 @@ export function createReconnectingProcessChannel( stderr: child.stderr, stdin: child.stdin, classifyStderr: options.classifyStderr, - onTerminalError: (error) => handlePipeFailure(attempt, error, phase), + onTerminalError: (error, wasReady) => handlePipeFailure(attempt, error, phase, wasReady), requestTimeoutMs: options.requestTimeoutMs, + readyTimeoutMs: AGENT_READY_TIMEOUT_MS, expectedProtocolMajor: options.expectedProtocolMajor, onDegraded: () => { if (state === "disposed" || active !== attempt) return; @@ -319,13 +327,22 @@ export function createReconnectingProcessChannel( attempt: ActiveProcess, error: SshError, phase: "connecting" | "reconnecting", + wasReady: boolean, ): void { if (state === "disposed" || active !== attempt) return; getChannelLogger().warn( `[${label}] agent pipe failure (phase=${phase}, pid=${attempt.child.pid}, code=${error.code}): ${error.message}`, ); - if (phase === "reconnecting") { + if (wasReady && error.cause instanceof AgentLineTooLongError) { + attempt.reconnectAfterClose = error; terminateChild(attempt); + return; + } + // A stalled handshake must be stopped before the next dialer starts. + // This process has no active RPCs to drain. + if (!attempt.ready && error.code === "transport.timeout") attempt.child.kill("SIGKILL"); + else terminateChild(attempt); + if (phase === "reconnecting") { if (options.isFatalReconnectError?.(error)) { consecutiveFatalFailures += 1; if (consecutiveFatalFailures >= MAX_CONSECUTIVE_FATAL_RECONNECT_FAILURES) { @@ -343,7 +360,6 @@ export function createReconnectingProcessChannel( state = "terminal"; terminalError = error; emitLifecycle({ type: "failure", error }); - terminateChild(attempt); } /** @@ -407,7 +423,7 @@ export function createReconnectingProcessChannel( ); if (state === "disposed" || terminalError) return; - if (wasReady && (code === 0 || attempt.preempted)) { + if (wasReady && (attempt.preempted || (code === 0 && !attempt.reconnectAfterClose))) { getChannelLogger().info( attempt.preempted ? `[${label}] another connection took the agent over; channel is now terminal` @@ -429,7 +445,9 @@ export function createReconnectingProcessChannel( if (!wasAlreadyReconnecting) { emitLifecycle({ type: "reconnecting", - cause: code === null ? null : new Error(`agent process exited with code ${code}`), + cause: + attempt.reconnectAfterClose ?? + (code === null ? null : new Error(`agent process exited with code ${code}`)), hadEpoch: lastAgentEpoch !== 0, }); } diff --git a/src/main/infra/agent/collect-process.ts b/src/main/infra/agent/collect-process.ts new file mode 100644 index 000000000..12f3e042c --- /dev/null +++ b/src/main/infra/agent/collect-process.ts @@ -0,0 +1,86 @@ +import type { ChildProcessWithoutNullStreams } from "node:child_process"; +import { createSshError } from "./pipe"; + +/** Maximum combined output of one short transport command. */ +const MAX_COLLECTED_BYTES = 4 * 1024 * 1024; + +interface CollectProcessOptions { + readonly name: string; + readonly input?: Buffer | string; + readonly signal?: AbortSignal; + readonly timeoutMs: number; +} + +/** Collects one transport command and kills it on cancellation or deadline. */ +export function collectProcess( + child: ChildProcessWithoutNullStreams, + { name, input, signal, timeoutMs }: CollectProcessOptions, +): Promise<{ stdout: Buffer; stderr: Buffer; code: number | null }> { + return new Promise((resolve, reject) => { + const stdout: Buffer[] = []; + const stderr: Buffer[] = []; + let received = 0; + let settled = false; + const timer = setTimeout(() => { + stop(createSshError("transport.timeout", new Error(`${name} exceeded ${timeoutMs}ms`))); + }, timeoutMs); + timer.unref?.(); + + /** Drops cancellation hooks once the caller has its result. */ + const cleanup = () => { + settled = true; + clearTimeout(timer); + signal?.removeEventListener("abort", abort); + stdout.length = 0; + stderr.length = 0; + }; + /** Keeps the original transport error while terminating the child. */ + const stop = (error: unknown) => { + if (settled) return; + cleanup(); + reject(error); + child.kill("SIGKILL"); + }; + const abort = () => stop(signal?.reason); + + /** Collects output only while the command is within its shared byte budget. */ + const collect = (chunks: Buffer[], chunk: Buffer) => { + if (settled) return; + received += chunk.byteLength; + if (received > MAX_COLLECTED_BYTES) { + stop( + createSshError( + "transport.unknown", + new Error(`${name} output exceeded ${MAX_COLLECTED_BYTES} bytes`), + ), + ); + return; + } + chunks.push(chunk); + }; + + child.stdout.on("data", (chunk: Buffer) => collect(stdout, chunk)); + child.stderr.on("data", (chunk: Buffer) => collect(stderr, chunk)); + child.on("error", (error) => { + if (settled) return; + cleanup(); + reject(error); + }); + child.on("close", (code) => { + if (settled) return; + const result = { stdout: Buffer.concat(stdout), stderr: Buffer.concat(stderr), code }; + cleanup(); + resolve(result); + }); + child.stdin.on("error", (error: NodeJS.ErrnoException) => { + // A child closing its input can still report the cause on stderr. + if (error.code !== "EPIPE" && error.code !== "ERR_STREAM_DESTROYED") stop(error); + }); + if (signal?.aborted) { + abort(); + } else { + signal?.addEventListener("abort", abort, { once: true }); + child.stdin.end(input); + } + }); +} diff --git a/src/main/infra/agent/pipe.ts b/src/main/infra/agent/pipe.ts index ee2faa557..78796565b 100644 --- a/src/main/infra/agent/pipe.ts +++ b/src/main/infra/agent/pipe.ts @@ -15,10 +15,20 @@ import { z } from "zod"; import { AGENT_PROTOCOL_VERSION } from "../../../shared/agent/envelope"; import { PendingRequestMap } from "../../../shared/ipc/pending-request-map"; import { createLogger } from "../../../shared/log/main"; +import { redactLogSecrets } from "../../../shared/log/redact-secrets"; import type { LogLevel } from "../../../shared/log/types"; import type { SshErrorCode } from "../../../shared/ssh/errors"; const DEFAULT_REQUEST_TIMEOUT_MS = 30_000; +/** Match the Go agent's per-line limit, measured before UTF-8 decoding. */ +const MAX_AGENT_LINE_BYTES = 64 * 1024 * 1024; + +/** The active transport must be replaced after an oversized agent line. */ +export class AgentLineTooLongError extends Error { + constructor() { + super(`Agent output line exceeded ${MAX_AGENT_LINE_BYTES} bytes`); + } +} /** How many timed-out request ids to remember for absorbing late answers. */ const ABANDONED_REQUEST_IDS_MAX = 1024; @@ -170,8 +180,9 @@ export interface NdjsonPipeDependencies { readonly stderr: Readable; readonly stdin: Writable; readonly classifyStderr: StderrClassifier; - readonly onTerminalError: (error: SshError) => void; + readonly onTerminalError: (error: SshError, wasReady: boolean) => void; readonly requestTimeoutMs?: number; + readonly readyTimeoutMs?: number; readonly expectedProtocolMajor?: string; /** * Called when the first heartbeat miss is detected (advertisedInterval × 1). @@ -233,8 +244,8 @@ export interface NdjsonPipe { /** Marks the pipe terminally failed from the orchestrator's side. */ fail(error: Error): void; /** - * Flushes buffered lines and reports whether ready had settled before close. - * `stderrTail` carries the last few raw stderr lines (loader errors, panics, + * Flushes buffered lines and reports whether the handshake had succeeded before close. + * `stderrTail` carries the last few masked stderr lines (loader errors, panics, * unclassified warnings) so the orchestrator can attach them to the terminal * error's cause — the file log is then self-sufficient for diagnosis. */ @@ -260,15 +271,18 @@ export function createNdjsonPipe(deps: NdjsonPipeDependencies): NdjsonPipe { // dead workspace. Insertion-ordered, so the oldest id is evicted first. const abandonedRequestIds = new Set(); const listeners = new Map>(); - const stdoutLines = createLineSplitter(handleStdoutLine, { + /** Stops this transport without buffering or logging the oversized line. */ + const onLineTooLong = () => + selfFail(createSshError("server.protocol-error", new AgentLineTooLongError())); + const stdoutLines = createLineSplitter(handleStdoutLine, onLineTooLong, { hwm: STDOUT_BACKPRESSURE_HWM, lwm: STDOUT_BACKPRESSURE_LWM, pause: () => deps.stdout.pause(), resume: () => deps.stdout.resume(), }); - const stderrLines = createLineSplitter(handleStderrLine); + const stderrLines = createLineSplitter(handleStderrLine, onLineTooLong); - // Ring buffer of the most recent raw stderr lines. Retained regardless of + // Ring buffer of the most recent masked stderr lines. Retained regardless of // classification so an unrecognized fatal line (e.g. a glibc loader error) // is still available as diagnostic context when the process closes. const STDERR_TAIL_MAX_LINES = 20; @@ -286,6 +300,8 @@ export function createNdjsonPipe(deps: NdjsonPipeDependencies): NdjsonPipe { let resolveReady!: () => void; let rejectReady!: (error: Error) => void; let readySettled = false; + let readyReceived = false; + let readyTimer: NodeJS.Timeout | undefined; // Capabilities from the ready frame — populated once ready arrives. let capabilityMethods: readonly string[] | undefined; @@ -317,11 +333,14 @@ export function createNdjsonPipe(deps: NdjsonPipeDependencies): NdjsonPipe { resolveReady = () => { if (readySettled) return; readySettled = true; + readyReceived = true; + clearTimeout(readyTimer); resolve(); }; rejectReady = (error) => { if (readySettled) return; readySettled = true; + clearTimeout(readyTimer); reject(error); }; }); @@ -360,11 +379,13 @@ export function createNdjsonPipe(deps: NdjsonPipeDependencies): NdjsonPipe { /** Records a terminal pipe failure detected from stream data. */ function selfFail(error: SshError): void { - if (terminalError) return; + if (disposed || terminalError) return; terminalError = error; + clearHeartbeatWatchdog(); + clearKeepalive(); rejectReady(error); rejectPendingRequests(error); - deps.onTerminalError(error); + deps.onTerminalError(error, readyReceived); } /** Parses one stdout NDJSON line and routes it to ready, call, or event state. */ @@ -560,7 +581,7 @@ export function createNdjsonPipe(deps: NdjsonPipeDependencies): NdjsonPipe { } } - // Retain the raw line before classification so it survives even when no + // Retain the diagnostic before classification so it survives even when no // pattern matches — the close handler reads this tail for the cause. recordStderr(line); @@ -584,7 +605,7 @@ export function createNdjsonPipe(deps: NdjsonPipeDependencies): NdjsonPipe { /** Appends one stderr line to the bounded diagnostic tail buffer. */ function recordStderr(line: string): void { - recentStderr.push(line.slice(0, STDERR_TAIL_MAX_LINE_CHARS)); + recentStderr.push(redactLogSecrets(line).slice(0, STDERR_TAIL_MAX_LINE_CHARS)); if (recentStderr.length > STDERR_TAIL_MAX_LINES) { recentStderr.shift(); } @@ -755,13 +776,25 @@ export function createNdjsonPipe(deps: NdjsonPipeDependencies): NdjsonPipe { notifyClose(): { wasReady: boolean; stderrTail: string } { stdoutLines.flush(); stderrLines.flush(); - const wasReady = readySettled; - if (!wasReady && deferredSpawnFailure !== null) { + const wasReady = readyReceived; + if (!readySettled && deferredSpawnFailure !== null) { selfFail(createSshError("server.spawn-failed", deferredSpawnFailure)); } return { wasReady, stderrTail: recentStderr.join(" | ") }; }, }; + const readyTimeoutMs = deps.readyTimeoutMs; + if (readyTimeoutMs !== undefined && !readySettled) { + readyTimer = setTimeout(() => { + selfFail( + createSshError( + "transport.timeout", + new Error(`Agent handshake exceeded ${readyTimeoutMs}ms`), + ), + ); + }, readyTimeoutMs); + readyTimer.unref?.(); + } return pipe; } @@ -783,8 +816,11 @@ export function createSshError(code: SshErrorCode, cause?: unknown): SshError { cause instanceof Error ? cause.message : cause === undefined ? "" : String(cause); // 600 chars (up from 300) so a close diagnostic carrying an exit code plus // a multi-line stderr tail is not truncated before the actual error text. - const causeSnippet = causeMsg.slice(0, 600); - const stack = (error.stack ?? "").split("\n").slice(1, 6).join(" | "); + const causeSnippet = redactLogSecrets(causeMsg).slice(0, 600); + const stack = redactLogSecrets(error.stack ?? "") + .split("\n") + .slice(1, 6) + .join(" | "); getMalformedStdoutLogger().warn( `SshError throw: code=${code} cause=${causeSnippet} stack=${stack}`, ); @@ -806,7 +842,7 @@ export function createDisposedError(): Error { /** * Maps stable SSH error codes to non-sensitive caller-facing messages. */ -function messageForSshErrorCode(code: SshErrorCode): string { +export function messageForSshErrorCode(code: SshErrorCode): string { switch (code) { case "ssh.connect-failed": return "SSH connection failed"; @@ -855,18 +891,25 @@ function getMalformedStdoutLogger(): ReturnType { } function logMalformedStdoutLine(line: string, ready: boolean, error: unknown): void { - const preview = escapeControlChars(line.slice(0, MALFORMED_LINE_PREVIEW_CHARS)); - const truncated = line.length > MALFORMED_LINE_PREVIEW_CHARS ? "…" : ""; - const reason = error instanceof Error ? error.message : String(error); + const preview = malformedLinePreview(line); + // JSON.parse may quote a fragment of the original input with no credential label. + const reason = error instanceof SyntaxError ? error.name : redactLogSecrets(String(error)); // Route this through `createLogger` so it reaches the electron-log file transport. It used to // call `console.warn` directly, so it never reached main.log in a packaged app, but // this line is the top clue when diagnosing stdout pollution from a remote SSH agent, so file // visibility matters. getMalformedStdoutLogger().warn( - `stdout NDJSON parse failed (ready=${ready}, len=${line.length}, reason=${reason}): ${preview}${truncated}`, + `stdout NDJSON parse failed (ready=${ready}, len=${line.length}, reason=${reason}): ${preview}`, ); } +/** Redacts the whole line before clipping so a partially visible value cannot leak. */ +function malformedLinePreview(line: string): string { + const redacted = redactLogSecrets(line); + const truncated = redacted.length > MALFORMED_LINE_PREVIEW_CHARS ? "…" : ""; + return escapeControlChars(redacted.slice(0, MALFORMED_LINE_PREVIEW_CHARS)) + truncated; +} + /** * Replaces ASCII control characters and the DEL byte with `\xNN` escapes so * shell motd output, ANSI escape sequences, and binary chunks are readable in @@ -920,7 +963,7 @@ function parseFrame(line: string): ParsedFrame { if (!isRecord(parsed)) { getMalformedStdoutLogger().warn( - `parseFrame: parsed value is not a record. line=${escapeControlChars(line.slice(0, MALFORMED_LINE_PREVIEW_CHARS))}`, + `parseFrame: parsed value is not a record. line=${malformedLinePreview(line)}`, ); throw createSshError("server.protocol-error"); } @@ -930,7 +973,7 @@ function parseFrame(line: string): ParsedFrame { const hasEvent = hasOwnKey(parsed, "event"); if (hasResult && hasError) { getMalformedStdoutLogger().warn( - `parseFrame: frame has both result and error. line=${escapeControlChars(line.slice(0, MALFORMED_LINE_PREVIEW_CHARS))}`, + `parseFrame: frame has both result and error. line=${malformedLinePreview(line)}`, ); throw createSshError("server.protocol-error"); } @@ -976,7 +1019,7 @@ function parseFrame(line: string): ParsedFrame { } getMalformedStdoutLogger().warn( - `parseFrame: frame matched no known schema. hasResult=${hasResult} hasError=${hasError} hasEvent=${hasEvent} line=${escapeControlChars(line.slice(0, MALFORMED_LINE_PREVIEW_CHARS))}`, + `parseFrame: frame matched no known schema. hasResult=${hasResult} hasError=${hasError} hasEvent=${hasEvent} line=${malformedLinePreview(line)}`, ); throw createSshError("server.protocol-error"); } @@ -1062,6 +1105,7 @@ interface LineSplitterBackpressure { */ function createLineSplitter( onLine: (line: string) => void, + onLineTooLong: () => void, backpressure?: LineSplitterBackpressure, ): { push(chunk: Buffer): void; @@ -1074,7 +1118,9 @@ function createLineSplitter( // growing string for "\n" on every chunk made a large frame quadratic. // 0x0A never occurs inside a multi-byte UTF-8 sequence, so splitting the // raw bytes on it is safe. - let pending: Buffer[] = []; + let pending: Buffer | undefined; + let pendingBytes = 0; + let oversized = false; // Running tally of bytes from completed lines dispatched since the last // gate transition. Resets to zero after each pause() or resume() call // so each measurement window starts fresh. @@ -1129,34 +1175,67 @@ function createLineSplitter( } } + /** Reject an oversized line before retaining or concatenating its bytes. */ + function fits(bytes: number): boolean { + if (pendingBytes + bytes <= MAX_AGENT_LINE_BYTES) return true; + pending = undefined; + pendingBytes = 0; + oversized = true; + onLineTooLong(); + return false; + } + + /** Packs small chunks so retained buffer objects cannot grow with the number of reads. */ + function append(tail: Buffer): Buffer { + const end = pendingBytes + tail.length; + if (!pending || pending.length < end) { + const capacity = Math.min(MAX_AGENT_LINE_BYTES, Math.max(end, (pending?.length ?? 64) * 2)); + const grown = Buffer.allocUnsafe(capacity); + pending?.copy(grown, 0, 0, pendingBytes); + pending = grown; + } + tail.copy(pending, pendingBytes); + pendingBytes = end; + return pending.subarray(0, end); + } + return { push(chunk) { + if (oversized) return; let start = 0; let newlineIndex = chunk.indexOf(0x0a); while (newlineIndex !== -1) { const tail = chunk.subarray(start, newlineIndex); - const bytes = pending.length === 0 ? tail : Buffer.concat([...pending, tail]); - pending = []; + if (!fits(tail.length)) return; + const bytes = pending ? append(tail) : tail; + pending = undefined; + pendingBytes = 0; start = newlineIndex + 1; try { dispatchLine(bytes.toString("utf8").replace(/\r$/, "")); } catch (err) { // Keep the chunk's remaining lines for the next push, as before. - if (start < chunk.length) pending.push(chunk.subarray(start)); + if (start < chunk.length && fits(chunk.length - start)) { + append(chunk.subarray(start)); + } throw err; } newlineIndex = chunk.indexOf(0x0a, start); } - if (start < chunk.length) pending.push(chunk.subarray(start)); + if (start < chunk.length) { + if (!fits(chunk.length - start)) return; + append(chunk.subarray(start)); + } postBurstResumeCheck(); }, flush() { - if (pending.length === 0) { + if (!pending) { postBurstResumeCheck(); return; } - const line = Buffer.concat(pending).toString("utf8").replace(/\r$/, ""); - pending = []; + const line = pending.toString("utf8", 0, pendingBytes).replace(/\r$/, ""); + pending = undefined; + pendingBytes = 0; dispatchLine(line); postBurstResumeCheck(); }, diff --git a/src/main/infra/agent/ssh/auth-pty.ts b/src/main/infra/agent/ssh/auth-pty.ts index 60ccac59a..493eed4bd 100644 --- a/src/main/infra/agent/ssh/auth-pty.ts +++ b/src/main/infra/agent/ssh/auth-pty.ts @@ -2,6 +2,7 @@ import type { IPty } from "node-pty"; import type { SshAuthPrompt, SshAuthResponse } from "../../../../shared/ssh/auth-prompt"; import type { SshErrorCode } from "../../../../shared/ssh/errors"; import { createSshError } from "../pipe"; +import { AuthCancelledError } from "./auth"; import { buildSshControlMasterArgs, createSshControlMaster, @@ -18,7 +19,10 @@ const HOST_KEY_PROMPT_PATTERN = const FINGERPRINT_PATTERN = /([A-Z0-9]+) key fingerprint is:? (SHA256:[^\s.]+)/i; const DEFAULT_AUTH_TIMEOUT_MS = 30_000; -export type SshAuthPromptHandler = (prompt: SshAuthPrompt) => Promise; +export type SshAuthPromptHandler = ( + prompt: SshAuthPrompt, + signal?: AbortSignal, +) => Promise; export type SpawnPty = ( command: string, args: string[], @@ -31,6 +35,7 @@ export interface AuthenticateSshControlMasterDependencies { readonly unlink?: (path: string) => void; readonly promptIdPrefix?: string; readonly authTimeoutMs?: number; + readonly signal?: AbortSignal; } /** @@ -44,8 +49,10 @@ export function authenticateSshControlMaster( promptHandler: SshAuthPromptHandler, dependencies: AuthenticateSshControlMasterDependencies = {}, ): Promise { + const { signal } = dependencies; + if (signal?.aborted) return Promise.reject(signal.reason); if (dependencies.spawnPty === undefined && usesSsh2Client()) { - return authenticateSsh2Master(options, promptHandler); + return authenticateSsh2Master(options, promptHandler, { signal }); } const master = createSshControlMaster(options, { spawn: dependencies.spawn, @@ -53,12 +60,18 @@ export function authenticateSshControlMaster( }); const spawnPty = dependencies.spawnPty ?? defaultSpawnPty; const authTimeoutMs = dependencies.authTimeoutMs ?? DEFAULT_AUTH_TIMEOUT_MS; - const ptyProcess = spawnPty("ssh", buildSshControlMasterArgs(master), { - name: "xterm-256color", - cols: 80, - rows: 24, - env: process.env, - }); + let ptyProcess: IPty; + try { + ptyProcess = spawnPty("ssh", buildSshControlMasterArgs(master), { + name: "xterm-256color", + cols: 80, + rows: 24, + env: process.env, + }); + } catch (error) { + master.dispose(); + return Promise.reject(error); + } const sessionPrefix = dependencies.promptIdPrefix ?? `ssh-auth:${master.controlPath}`; let buffer = ""; @@ -66,6 +79,8 @@ export function authenticateSshControlMaster( let promptInFlight = false; return new Promise((resolve, reject) => { + const prompts = new AbortController(); + const abort = () => failAuth(signal?.reason); const dataDisposable = ptyProcess.onData((chunk) => { buffer = trimAuthBuffer(buffer + chunk); void maybeHandlePrompt(); @@ -86,6 +101,8 @@ export function authenticateSshControlMaster( master.dispose(); reject(createSshError(classifyPtyExit(buffer))); }); + if (signal?.aborted) abort(); + else signal?.addEventListener("abort", abort, { once: true }); async function maybeHandlePrompt(): Promise { if (settled || promptInFlight) return; @@ -95,7 +112,7 @@ export function authenticateSshControlMaster( promptInFlight = true; disarmAuthTimer(); try { - const response = await promptHandler(prompt); + const response = await promptHandler(prompt, prompts.signal); if (settled) return; armAuthTimer(); if (response.kind === "password" && prompt.kind === "password") { @@ -110,7 +127,12 @@ export function authenticateSshControlMaster( } throw createSshError("ssh.auth-failed"); } catch (error) { - failAuth(createSshError("ssh.auth-failed", error)); + failAuth( + createSshError( + error instanceof AuthCancelledError ? "ssh.auth-cancelled" : "ssh.auth-failed", + error, + ), + ); } finally { promptInFlight = false; } @@ -130,7 +152,7 @@ export function authenticateSshControlMaster( authTimer = null; } - function failAuth(error: Error): void { + function failAuth(error: unknown): void { if (settled) return; cleanupListeners(); settled = true; @@ -140,6 +162,8 @@ export function authenticateSshControlMaster( } function cleanupListeners(): void { + signal?.removeEventListener("abort", abort); + prompts.abort(); disarmAuthTimer(); dataDisposable.dispose(); exitDisposable.dispose(); diff --git a/src/main/infra/agent/ssh/auth.ts b/src/main/infra/agent/ssh/auth.ts index ce5121672..e08245380 100644 --- a/src/main/infra/agent/ssh/auth.ts +++ b/src/main/infra/agent/ssh/auth.ts @@ -9,3 +9,11 @@ import { classifyStderrLine } from "./stderr-patterns"; export function classifyAuthLine(line: string): SshErrorCode | null { return classifyStderrLine(line); } + +/** Error raised when the renderer cancels an active SSH authentication prompt. */ +export class AuthCancelledError extends Error { + constructor(promptId: string) { + super(`SSH authentication prompt cancelled: ${promptId}`); + this.name = "AuthCancelledError"; + } +} diff --git a/src/main/infra/agent/ssh/channel.ts b/src/main/infra/agent/ssh/channel.ts index 011929567..0f137e3b1 100644 --- a/src/main/infra/agent/ssh/channel.ts +++ b/src/main/infra/agent/ssh/channel.ts @@ -129,11 +129,13 @@ function createAuthenticatedSshChannel( let disposeInnerLifecycle: (() => void) | null = null; let disposeMaster: (() => void) | null = null; + const auth = new AbortController(); // Reached only when no controlPath was provided by the caller — i.e. the // bootstrap path was not used and we must authenticate our own ControlMaster. const ready = authenticateSshControlMaster(options, promptHandler, { ...dependencies.auth, spawn: dependencies.auth?.spawn ?? dependencies.spawn, + signal: auth.signal, }) .then((master) => { if (disposed) { @@ -161,6 +163,7 @@ function createAuthenticatedSshChannel( }) .catch((error) => { rejectPendingCalls(error); + if (disposed) throw error; emitLifecycle({ type: "failure", error: error instanceof Error ? error : createSshError("ssh.unknown", error), @@ -212,6 +215,7 @@ function createAuthenticatedSshChannel( dispose(): void { if (disposed) return; disposed = true; + auth.abort(createDisposedError()); rejectPendingCalls(createDisposedError()); disposeInnerLifecycle?.(); inner?.dispose(); diff --git a/src/main/infra/agent/ssh/credential-cache.ts b/src/main/infra/agent/ssh/credential-cache.ts index ead06eaa5..20cb52ea0 100644 --- a/src/main/infra/agent/ssh/credential-cache.ts +++ b/src/main/infra/agent/ssh/credential-cache.ts @@ -29,10 +29,14 @@ function credentialKey(prompt: Extract): st export function createCachingAuthPromptHandler(inner: SshAuthPromptHandler): SshAuthPromptHandler { const cache = new Map(); - return async function cachingAuthPromptHandler(prompt: SshAuthPrompt): Promise { + return async function cachingAuthPromptHandler( + prompt: SshAuthPrompt, + signal?: AbortSignal, + ): Promise { + signal?.throwIfAborted(); // Host-key prompts are never cached — always ask the user. if (prompt.kind !== "password") { - return inner(prompt); + return inner(prompt, signal); } const key = credentialKey(prompt); @@ -48,7 +52,7 @@ export function createCachingAuthPromptHandler(inner: SshAuthPromptHandler): Ssh } } - const response = await inner(prompt); + const response = await inner(prompt, signal); if (response.kind === "password" && response.value) { cache.set(key, response.value); } diff --git a/src/main/infra/agent/ssh/forward.ts b/src/main/infra/agent/ssh/forward.ts index a962cd19e..39bdbdb78 100644 --- a/src/main/infra/agent/ssh/forward.ts +++ b/src/main/infra/agent/ssh/forward.ts @@ -16,7 +16,7 @@ import net from "node:net"; import type { Duplex } from "node:stream"; -import { defaultSpawnSsh, type SpawnSshProcess } from "./master"; +import { defaultSpawnSsh, type SpawnSshProcess, sshDestination } from "./master"; import { createSsh2Forwarder, type Ssh2Forwarder, usesSsh2Client } from "./ssh2"; /** Where a workspace's SSH connection goes, and the master it shares, if any. */ @@ -71,10 +71,6 @@ export function forwardSpec(localPort: number, remotePort: number): string { return `127.0.0.1:${localPort}:${REMOTE_HOST}:${remotePort}`; } -function destination(target: SshForwardTarget): string { - return target.user ? `${target.user}@${target.host}` : target.host; -} - function connectionArgs(target: SshForwardTarget): string[] { const args: string[] = []; if (target.port !== undefined) args.push("-p", String(target.port)); @@ -98,7 +94,7 @@ export function buildSshMuxForwardArgs( forwardSpec(localPort, remotePort), ...connectionArgs(target), "--", - destination(target), + sshDestination(target), ]; } @@ -121,7 +117,7 @@ export function buildSshOwnForwardArgs( forwardSpec(localPort, remotePort), ...connectionArgs(target), "--", - destination(target), + sshDestination(target), // ssh binds the forward before it opens the session, so the line means // the forward listens; cat ends when the app closes its stdin. `echo ${READY_LINE}; exec cat >/dev/null`, diff --git a/src/main/infra/agent/ssh/master.ts b/src/main/infra/agent/ssh/master.ts index e828f93a9..c967cd323 100644 --- a/src/main/infra/agent/ssh/master.ts +++ b/src/main/infra/agent/ssh/master.ts @@ -93,7 +93,7 @@ export function buildSshArgs(options: SshMasterOptions): string[] { if (options.identityFile) { args.push("-i", options.identityFile); } - args.push("--", destinationForOptions(options), options.remoteCommand); + args.push("--", sshDestination(options), options.remoteCommand); return args; } @@ -185,7 +185,7 @@ export function buildSshControlMasterArgs( ]; if (options.port !== undefined) args.push("-p", String(options.port)); if (options.identityFile) args.push("-i", options.identityFile); - args.push("--", destinationForOptions(options)); + args.push("--", sshDestination(options)); return args; } @@ -196,14 +196,14 @@ export function buildSshControlExitArgs( const args = ["-S", options.controlPath, "-O", "exit"]; if (options.port !== undefined) args.push("-p", String(options.port)); if (options.identityFile) args.push("-i", options.identityFile); - args.push("--", destinationForOptions(options)); + args.push("--", sshDestination(options)); return args; } /** * Renders the OpenSSH destination from an optional user and host. */ -function destinationForOptions(options: Omit): string { +export function sshDestination(options: Pick): string { return options.user ? `${options.user}@${options.host}` : options.host; } diff --git a/src/main/infra/agent/ssh/ssh-bootstrap/index.ts b/src/main/infra/agent/ssh/ssh-bootstrap/index.ts index 3c75093fa..d0dd8cd17 100644 --- a/src/main/infra/agent/ssh/ssh-bootstrap/index.ts +++ b/src/main/infra/agent/ssh/ssh-bootstrap/index.ts @@ -36,11 +36,7 @@ import { ZSHENV_CONTENT, ZSHRC_CONTENT, } from "../../runtimeDirs"; -import { - type AuthenticateSshControlMasterDependencies, - authenticateSshControlMaster, - type SshAuthPromptHandler, -} from "../auth-pty"; +import { authenticateSshControlMaster, type SshAuthPromptHandler } from "../auth-pty"; import { absoluteRemotePath, agentArtifactKey, @@ -73,6 +69,7 @@ import { LSP_BOOTSTRAP_PROGRESS_EVENT, type LspBootstrapProgressEvent, type LspBootstrapProgressPhase, + REMOTE_AGENT_BOOTSTRAP_TIMEOUT_MS, REMOTE_AGENT_MANIFEST, REMOTE_AGENT_PROTOCOL_MAJOR, REMOTE_AGENT_ROOT, @@ -126,7 +123,10 @@ interface ArtifactEnsureResult { // Process-wide deduplication of concurrent ensure() calls for the same // artifact on the same remote. Each key resolves to the in-flight promise // so multiple workspaces booting against the same host upload once. -const artifactLocks = new Map>(); +const artifactLocks = new Map< + string, + { ready: Promise; signal?: AbortSignal } +>(); /** * Ensures the agent binary is present at the canonical path on the remote. @@ -139,11 +139,31 @@ export async function ensureRemoteAgent( dependencies: SshBootstrapDependencies = {}, ): Promise { const context = await createBootstrapContext(options, dependencies); - const { runner, distDir, now, sshOptions, authenticatedMaster } = context; + const { distDir, now, authenticatedMaster } = context; + // Interactive authentication has its own timeout, paused for user input. + const deadline = new AbortController(); + const timer = setTimeout(() => { + deadline.abort( + createSshError( + "transport.timeout", + new Error(`Remote agent bootstrap exceeded ${REMOTE_AGENT_BOOTSTRAP_TIMEOUT_MS}ms`), + ), + ); + }, REMOTE_AGENT_BOOTSTRAP_TIMEOUT_MS); + timer.unref?.(); + const signal = options.signal + ? AbortSignal.any([options.signal, deadline.signal]) + : deadline.signal; + const sshOptions = { ...context.sshOptions, signal }; + const runner: SshBootstrapRunner = (command, args, input) => + untilAborted(() => context.runner(command, args, input, signal), signal); try { + signal.throwIfAborted(); const localManifest = AgentManifestSchema.parse( - JSON.parse(await fs.readFile(path.join(distDir, "manifest.json"), "utf8")), + JSON.parse( + await fs.readFile(path.join(distDir, "manifest.json"), { encoding: "utf8", signal }), + ), ); const platform = options.cachedRemoteArch ?? (await detectRemotePlatform(sshOptions, runner)); const binary = findAgentBinary(localManifest, platform); @@ -218,6 +238,7 @@ export async function ensureRemoteAgent( ? await ensureRemoteShimFiles(sshOptions, runner, remoteHome) : undefined; + signal.throwIfAborted(); // On success, ownership of a ControlMaster authenticated *here* passes to // the caller via `dispose`. When the caller supplied its own controlPath // (a reused master), we surface that path back but no dispose handle — @@ -241,7 +262,9 @@ export async function ensureRemoteAgent( // This function never returned, so the caller has no dispose handle. // Dispose the ControlMaster here before propagating the error. authenticatedMaster?.dispose(); - throw error; + throw signal.aborted ? signal.reason : error; + } finally { + clearTimeout(timer); } } @@ -420,6 +443,26 @@ export function buildRemoteAgentCommand( return `bash -lc ${singleQuoteShellArg(script)}`; } +/** Waits for a bootstrap step while allowing the caller to abandon the wait. */ +async function untilAborted(run: () => Promise, signal?: AbortSignal): Promise { + signal?.throwIfAborted(); + if (!signal) return run(); + let onAbort!: () => void; + const cancelled = new Promise((_resolve, reject) => { + onAbort = () => reject(signal.reason); + signal.addEventListener("abort", onAbort, { once: true }); + }); + try { + const result = Promise.resolve().then(() => { + signal.throwIfAborted(); + return run(); + }); + return await Promise.race([cancelled, result]); + } finally { + signal.removeEventListener("abort", onAbort); + } +} + /** * Resolves the dependency defaults and, when this is the first call against * this remote in interactive mode, opens the ControlMaster up front so every @@ -435,6 +478,7 @@ async function createBootstrapContext( readonly sshOptions: EnsureRemoteAgentOptions; readonly authenticatedMaster: Awaited> | null; }> { + options.signal?.throwIfAborted(); const runner = dependencies.runner ?? defaultRunner; const distDir = dependencies.distDir ?? getAgentDistDir(); const now = dependencies.now ?? (() => new Date()); @@ -444,7 +488,7 @@ async function createBootstrapContext( ? await authenticateSshControlMaster( options, dependencies.promptHandler as SshAuthPromptHandler, - dependencies.auth as AuthenticateSshControlMasterDependencies | undefined, + { ...dependencies.auth, signal: options.signal }, ) : null; const sshOptions = authenticatedMaster @@ -513,16 +557,27 @@ async function ensureRemoteArtifact( ): Promise { const lockKey = artifactLockKey(options, request.key, request.record.sha256); const existing = artifactLocks.get(lockKey); - if (existing) return existing; + if (existing) { + try { + return await untilAborted(() => existing.ready, options.signal); + } catch (error) { + // A cancelled installer releases the lock. A surviving workspace can + // take ownership with its own transport and remaining deadline. + if (existing.signal?.aborted && !options.signal?.aborted) { + return ensureRemoteArtifact(options, runner, now, request, onProgress); + } + throw error; + } + } const pending = ensureRemoteArtifactUnlocked(options, runner, now, request, onProgress).finally( () => { - if (artifactLocks.get(lockKey) === pending) { + if (artifactLocks.get(lockKey)?.ready === pending) { artifactLocks.delete(lockKey); } }, ); - artifactLocks.set(lockKey, pending); + artifactLocks.set(lockKey, { ready: pending, signal: options.signal }); return pending; } diff --git a/src/main/infra/agent/ssh/ssh-bootstrap/manifest.ts b/src/main/infra/agent/ssh/ssh-bootstrap/manifest.ts index ca29931d1..95e3aa006 100644 --- a/src/main/infra/agent/ssh/ssh-bootstrap/manifest.ts +++ b/src/main/infra/agent/ssh/ssh-bootstrap/manifest.ts @@ -3,6 +3,7 @@ * remote artifacts. The bootstrap orchestrator reads and writes this * manifest on each ensure() pass to skip work the remote already has. */ +import { randomBytes } from "node:crypto"; import { z } from "zod"; import { AgentArtifactPlatformSchema, @@ -11,7 +12,7 @@ import { SshHostOsSchema, } from "../../../../../shared/agent/manifest"; import { createSshError } from "../../pipe"; -import { runSsh } from "./transport"; +import { quoteShellArg, runSsh, singleQuoteShellArg } from "./transport"; import { type EnsureRemoteAgentOptions, KEEP_REMOTE_VERSIONS, @@ -84,17 +85,37 @@ export async function readRemoteManifest( return { artifacts: {} }; } -/** Replaces the remote manifest atomically. */ +/** Publishes a complete manifest by renaming a sibling on the remote host. */ export async function writeRemoteManifest( options: EnsureRemoteAgentOptions, runner: SshBootstrapRunner, manifest: RemoteArtifactManifest, ): Promise { + const suffix = randomBytes(8).toString("hex"); + const payload = `${JSON.stringify(manifest, null, 2)}\n`; + // Claim a new name without clobbering an existing file. Make it private + // before copying any data, and retain the existing manifest's permissions + // if it is being replaced. A fresh manifest is private to its owner. + const script = [ + "manifest=$1", + "temporary=$manifest.tmp.$2", + '(set -C; : > "$temporary") || exit 1', + "trap 'rm -f \"$temporary\"' 0", + "trap 'exit 1' HUP INT TERM", + 'chmod go-rwx "$temporary" || exit 1', + 'if [ -e "$manifest" ] || [ -L "$manifest" ]; then', + ' [ -f "$manifest" ] && cp -p "$manifest" "$temporary" || exit 1', + "fi", + 'cat > "$temporary" || exit 1', + // A disconnected sender can deliver a short stream that cat reads as EOF. + 'written=$(wc -c < "$temporary") || exit 1', + '[ "$((written + 0))" -eq "$3" ] && [ ! -d "$manifest" ] && mv -f "$temporary" "$manifest"', + ].join("\n"); await runSsh( options, runner, - `mkdir -p ${REMOTE_AGENT_ROOT} && cat > ${REMOTE_AGENT_MANIFEST}`, - `${JSON.stringify(manifest, null, 2)}\n`, + `mkdir -p ${quoteShellArg(REMOTE_AGENT_ROOT)} && sh -c ${singleQuoteShellArg(script)} sh ${quoteShellArg(REMOTE_AGENT_MANIFEST)} ${suffix} ${Buffer.byteLength(payload)}`, + payload, ); } diff --git a/src/main/infra/agent/ssh/ssh-bootstrap/transport.ts b/src/main/infra/agent/ssh/ssh-bootstrap/transport.ts index 8a2432ee2..83e2e9e35 100644 --- a/src/main/infra/agent/ssh/ssh-bootstrap/transport.ts +++ b/src/main/infra/agent/ssh/ssh-bootstrap/transport.ts @@ -8,13 +8,15 @@ import { createHash, randomBytes } from "node:crypto"; import fs from "node:fs/promises"; import path from "node:path"; +import { collectProcess } from "../../collect-process"; import { createSshError } from "../../pipe"; -import { defaultSpawnSsh } from "../master"; -import type { - EnsureRemoteAgentOptions, - LspBootstrapProgressEvent, - SshBootstrapRunner, - SshBootstrapRunnerResult, +import { defaultSpawnSsh, sshDestination } from "../master"; +import { + type EnsureRemoteAgentOptions, + type LspBootstrapProgressEvent, + REMOTE_AGENT_BOOTSTRAP_TIMEOUT_MS, + type SshBootstrapRunner, + type SshBootstrapRunnerResult, } from "./types"; /** @@ -26,26 +28,23 @@ export async function defaultRunner( command: string, args: string[], input?: Buffer | string, + signal?: AbortSignal, ): Promise { - return new Promise((resolve, reject) => { - const child = defaultSpawnSsh(command, args, { - detached: false, - stdio: ["pipe", "pipe", "pipe"], - }); - const stdout: Buffer[] = []; - const stderr: Buffer[] = []; - child.stdout.on("data", (chunk: Buffer) => stdout.push(chunk)); - child.stderr.on("data", (chunk: Buffer) => stderr.push(chunk)); - child.on("error", reject); - child.on("close", (code) => { - const out = Buffer.concat(stdout).toString("utf8"); - const err = Buffer.concat(stderr).toString("utf8"); - if (code === 0) resolve({ stdout: out, stderr: err }); - else reject(createSshError("ssh.unknown", new Error(err || `${command} exited ${code}`))); - }); - if (input !== undefined) child.stdin.end(input); - else child.stdin.end(); + signal?.throwIfAborted(); + const child = defaultSpawnSsh(command, args, { + detached: false, + stdio: ["pipe", "pipe", "pipe"], }); + const { stdout, stderr, code } = await collectProcess(child, { + name: command, + input, + signal, + timeoutMs: REMOTE_AGENT_BOOTSTRAP_TIMEOUT_MS, + }); + const err = stderr.toString("utf8").trim(); + if (code !== 0) + throw createSshError("ssh.unknown", new Error(err || `${command} exited ${code}`)); + return { stdout: stdout.toString("utf8"), stderr: err }; } /** Runs one remote command through ssh with the standard transport args. */ @@ -57,7 +56,7 @@ export async function runSsh( ): Promise { return runner( "ssh", - [...buildSshTransportArgs(options), "--", destinationForOptions(options), remoteCommand], + [...buildSshTransportArgs(options), "--", sshDestination(options), remoteCommand], input, ); } @@ -68,7 +67,7 @@ export function buildSftpArgs(options: EnsureRemoteAgentOptions): string[] { if (options.port !== undefined) args.push("-P", String(options.port)); if (options.identityFile) args.push("-i", options.identityFile); if (options.controlPath) args.push("-o", `ControlPath=${options.controlPath}`); - args.push(destinationForOptions(options)); + args.push(sshDestination(options)); return args; } @@ -90,11 +89,6 @@ export function buildSshTransportArgs(options: EnsureRemoteAgentOptions): string return args; } -/** Composes `user@host` if a user was supplied, otherwise just the host. */ -export function destinationForOptions(options: EnsureRemoteAgentOptions): string { - return options.user ? `${options.user}@${options.host}` : options.host; -} - /** Uploads a local file to a remote path, retries once, and verifies sha256. */ export async function uploadAndVerifyFile(args: { readonly options: EnsureRemoteAgentOptions; diff --git a/src/main/infra/agent/ssh/ssh-bootstrap/types.ts b/src/main/infra/agent/ssh/ssh-bootstrap/types.ts index 5be38fbc4..ce6745296 100644 --- a/src/main/infra/agent/ssh/ssh-bootstrap/types.ts +++ b/src/main/infra/agent/ssh/ssh-bootstrap/types.ts @@ -16,6 +16,8 @@ import type { SshMasterOptions } from "../master"; // bricking every local workspace — see the envelope.ts comment.) export const REMOTE_AGENT_PROTOCOL_MAJOR = AGENT_PROTOCOL_VERSION; export const REMOTE_AGENT_VERSION = "0.1.0"; +/** Maximum duration of agent installation after authentication. */ +export const REMOTE_AGENT_BOOTSTRAP_TIMEOUT_MS = 120_000; /** * Root path on the remote host where the agent is installed. @@ -59,6 +61,7 @@ export interface LspBootstrapProgressEvent { export interface EnsureRemoteAgentOptions extends Omit { readonly remotePath: string; + readonly signal?: AbortSignal; readonly cachedRemoteArch?: RemoteAgentPlatform; readonly authMode?: "interactive" | "key-only"; /** @@ -126,6 +129,7 @@ export type SshBootstrapRunner = ( command: string, args: string[], input?: Buffer | string, + signal?: AbortSignal, ) => Promise; export interface SshBootstrapDependencies { diff --git a/src/main/infra/agent/ssh/ssh2/connect.ts b/src/main/infra/agent/ssh/ssh2/connect.ts index 91a627a07..812e0d269 100644 --- a/src/main/infra/agent/ssh/ssh2/connect.ts +++ b/src/main/infra/agent/ssh/ssh2/connect.ts @@ -38,6 +38,7 @@ import type { import type { SshAuthPrompt } from "../../../../../shared/ssh/auth-prompt"; import type { SshErrorCode } from "../../../../../shared/ssh/errors"; import { createSshError } from "../../pipe"; +import { AuthCancelledError } from "../auth"; import type { SshAuthPromptHandler } from "../auth-pty"; import { resolveSshHostConfig, type SshHostConfig, type SshTarget } from "./config"; import { @@ -59,6 +60,7 @@ const PASSPHRASE_ATTEMPTS = 3; export interface ConnectSsh2Dependencies { readonly resolveConfig?: (target: SshTarget) => Promise; readonly connectTimeoutMs?: number; + readonly signal?: AbortSignal; } /** An open connection and the jump hosts and proxy process it runs through. */ @@ -77,13 +79,17 @@ export async function connectSsh2( dependencies: ConnectSsh2Dependencies = {}, ): Promise { const resolveConfig = dependencies.resolveConfig ?? ((t: SshTarget) => resolveSshHostConfig(t)); + const { signal } = dependencies; + signal?.throwIfAborted(); const config = await resolveConfig(target); + signal?.throwIfAborted(); const hops: Ssh2Connection[] = []; let proxy: ChildProcess | null = null; const endAll = (): void => { proxy?.kill(); for (const hop of [...hops].reverse()) hop.end(); }; + signal?.addEventListener("abort", endAll, { once: true }); try { let sock: Duplex | undefined; if (config.proxyJump.length > 0) { @@ -92,9 +98,10 @@ export async function connectSsh2( for (const jump of config.proxyJump) { const hop = await connectThrough(parseJumpHost(jump), via, promptHandler, dependencies); hops.push(hop); + signal?.throwIfAborted(); via = hop; } - sock = await forwardTo(via as Ssh2Connection, config.hostname, config.port); + sock = await forwardTo(via as Ssh2Connection, config.hostname, config.port, signal); } else if (config.proxyCommand !== null) { proxy = spawnProxyCommand(config); sock = proxySocket(proxy); @@ -112,6 +119,8 @@ export async function connectSsh2( } catch (error) { endAll(); throw error; + } finally { + signal?.removeEventListener("abort", endAll); } } @@ -125,7 +134,7 @@ async function connectThrough( const resolveConfig = dependencies.resolveConfig ?? ((t: SshTarget) => resolveSshHostConfig(t)); // A jump host's own ProxyJump is not followed; it is reached through the one before it. const config = { ...(await resolveConfig(target)), proxyJump: [], proxyCommand: null }; - const sock = await forwardTo(via, config.hostname, config.port); + const sock = await forwardTo(via, config.hostname, config.port, dependencies.signal); const client = await authenticate(config, sock, promptHandler, dependencies); return { client, config, end: () => client.end() }; } @@ -148,14 +157,25 @@ export function parseJumpHost(spec: string): SshTarget { return { host, user: user || undefined, port }; } -function forwardTo(via: Ssh2Connection, hostname: string, port: number): Promise { +/** Opens a forwarding stream without keeping a cancelled jump attempt alive. */ +function forwardTo( + via: Ssh2Connection, + hostname: string, + port: number, + signal?: AbortSignal, +): Promise { return new Promise((resolve, reject) => { + signal?.throwIfAborted(); + const abort = () => reject(signal?.reason); + signal?.addEventListener("abort", abort, { once: true }); via.client.forwardOut("127.0.0.1", 0, hostname, port, (error, stream) => { + signal?.removeEventListener("abort", abort); if (error) { reject(createSshError("ssh.connect-failed", error)); return; } - resolve(stream); + if (signal?.aborted) stream.destroy(); + else resolve(stream); }); }); } @@ -213,6 +233,8 @@ function authenticate( // ssh2 is loaded on first use: only the Windows SSH path needs it. // eslint-disable-next-line @typescript-eslint/no-require-imports const { Client: ClientClass } = require("ssh2") as typeof import("ssh2"); + const { signal } = dependencies; + signal?.throwIfAborted(); const client = new ClientClass(); const timeoutMs = dependencies.connectTimeoutMs ?? config.connectTimeoutMs ?? CONNECT_TIMEOUT_MS; @@ -221,6 +243,7 @@ function authenticate( let hostKeyRefused: string | null = null; let timer: ReturnType | null = null; let promptsOpen = 0; + const prompts = new AbortController(); const armTimer = (): void => { disarmTimer(); @@ -237,7 +260,10 @@ function authenticate( promptsOpen += 1; disarmTimer(); try { - return await promptHandler(prompt); + return await promptHandler(prompt, prompts.signal); + } catch (error) { + if (error instanceof AuthCancelledError) fail("ssh.auth-cancelled", error); + throw error; } finally { promptsOpen -= 1; if (promptsOpen === 0 && !settled) armTimer(); @@ -246,17 +272,25 @@ function authenticate( // The error is made only for a connection still opening: making an // SshError logs it. const fail = (code: SshErrorCode, cause: unknown): void => { + if (!settled) stop(createSshError(code, cause)); + }; + const stop = (error: unknown): void => { if (settled) return; + settle(); + client.end(); + reject(error); + }; + const abort = () => stop(signal?.reason); + const settle = (): void => { settled = true; disarmTimer(); - client.end(); - reject(createSshError(code, cause)); + prompts.abort(); + signal?.removeEventListener("abort", abort); }; client.once("ready", () => { if (settled) return; - settled = true; - disarmTimer(); + settle(); resolve(client); }); // Kept for the connection's life: an error with no listener would throw. @@ -295,7 +329,12 @@ function authenticate( ...(config.forwardAgent !== null ? { agent: config.forwardAgent, agentForward: true } : {}), }; armTimer(); - client.connect(connectConfig); + signal?.addEventListener("abort", abort, { once: true }); + try { + client.connect(connectConfig); + } catch (error) { + stop(error); + } }); } @@ -331,7 +370,6 @@ async function verifyHostKey( username: config.user, keyType: type, fingerprint, - message: `The authenticity of host '${name}' can't be established.\n${type} key fingerprint is ${fingerprint}.`, }); } catch { return "Host key verification failed: the key was not trusted"; diff --git a/src/main/infra/agent/ssh/ssh2/index.ts b/src/main/infra/agent/ssh/ssh2/index.ts index 9c8884103..42abbb03d 100644 --- a/src/main/infra/agent/ssh/ssh2/index.ts +++ b/src/main/infra/agent/ssh/ssh2/index.ts @@ -39,7 +39,7 @@ export function usesSsh2Client(env: NodeJS.ProcessEnv = process.env): boolean { /** One authenticated connection that commands share, opened again when it drops. */ class SharedConnection { private current: Promise | null = null; - private disposed = false; + private readonly lifetime = new AbortController(); constructor( private readonly target: SshTarget, @@ -48,16 +48,22 @@ class SharedConnection { ) {} client(): Promise { - if (this.disposed) return Promise.reject(new Error("the ssh2 connection was closed")); + if (this.lifetime.signal.aborted) return Promise.reject(this.lifetime.signal.reason); if (this.current === null) { - const opening = connectSsh2(this.target, this.promptHandler, this.dependencies); + const signal = this.dependencies.signal + ? AbortSignal.any([this.dependencies.signal, this.lifetime.signal]) + : this.lifetime.signal; + const opening = connectSsh2(this.target, this.promptHandler, { + ...this.dependencies, + signal, + }); this.current = opening; opening.then( (connection) => { connection.client.on("close", () => { if (this.current === opening) this.current = null; }); - if (this.disposed) connection.end(); + if (signal.aborted) connection.end(); }, () => { if (this.current === opening) this.current = null; @@ -68,8 +74,8 @@ class SharedConnection { } dispose(): void { - if (this.disposed) return; - this.disposed = true; + if (this.lifetime.signal.aborted) return; + this.lifetime.abort(); const current = this.current; this.current = null; void current?.then( diff --git a/src/main/infra/agent/wsl/index.ts b/src/main/infra/agent/wsl/index.ts index 4c3e628bc..f857e3fec 100644 --- a/src/main/infra/agent/wsl/index.ts +++ b/src/main/infra/agent/wsl/index.ts @@ -12,6 +12,7 @@ import { type AgentReconnectOptions, createReconnectingProcessChannel, } from "../channel/reconnecting-process-channel"; +import { collectProcess } from "../collect-process"; import { createSshError } from "../pipe"; import { formatCloseDiagnostic } from "../ssh/channel"; import { @@ -23,10 +24,11 @@ import { REMOTE_AGENT_PROTOCOL_MAJOR, type RemoteAgentPlatform, type SshBootstrapRunner, - type SshBootstrapRunnerResult, } from "../ssh/ssh-bootstrap"; const WSL_EXE = "wsl.exe"; +const WSL_COMMAND_TIMEOUT_MS = 120_000; +const WSL_LIST_TIMEOUT_MS = 10_000; /** Starts `argv` in `distro`, in the distribution user's home. */ export type SpawnWslProcess = ( @@ -38,6 +40,7 @@ export interface WslAgentOptions { readonly distro: string; /** Absolute path inside the distribution; the agent's root. */ readonly remotePath: string; + readonly signal?: AbortSignal; /** Installs the workspace's shell shim files too, as for SSH. */ readonly workspaceId?: string; readonly cachedRemoteArch?: RemoteAgentPlatform; @@ -89,26 +92,6 @@ export function decodeWslOutput(buffer: Buffer): string { return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text; } -function collect( - child: ChildProcessWithoutNullStreams, - input?: Buffer | string, -): Promise { - return new Promise((resolve, reject) => { - const stdout: Buffer[] = []; - const stderr: Buffer[] = []; - child.stdout.on("data", (chunk: Buffer) => stdout.push(chunk)); - child.stderr.on("data", (chunk: Buffer) => stderr.push(chunk)); - child.on("error", (error) => reject(createSshError("transport.unknown", error))); - child.on("close", (code) => { - const out = Buffer.concat(stdout).toString("utf8"); - const err = decodeWslOutput(Buffer.concat(stderr)).trim(); - if (code === 0) resolve({ stdout: out, stderr: err }); - else reject(createSshError("transport.unknown", new Error(err || `wsl.exe exited ${code}`))); - }); - child.stdin.end(input); - }); -} - /** * Runs the SSH bootstrap's remote steps in `distro`. The bootstrap hands * each step over as `ssh -- `; the command @@ -120,12 +103,27 @@ export function createWslBootstrapRunner( distro: string, spawnImpl: SpawnWslProcess = defaultSpawnWsl, ): SshBootstrapRunner { - return (command, args, input) => { + return async (command, args, input, signal) => { if (command !== "ssh") { - return Promise.reject(new Error(`${command} is not used in WSL`)); + throw new Error(`${command} is not used in WSL`); } const remoteCommand = args[args.length - 1] ?? ""; - return collect(spawnImpl(distro, ["sh", "-c", remoteCommand]), input); + signal?.throwIfAborted(); + const child = spawnImpl(distro, ["sh", "-c", remoteCommand]); + const { stdout, stderr, code } = await collectProcess(child, { + name: WSL_EXE, + input, + signal, + timeoutMs: WSL_COMMAND_TIMEOUT_MS, + }).catch((error: unknown) => { + if (signal?.aborted || (error as { code?: string }).code === "transport.timeout") throw error; + throw createSshError("transport.unknown", error); + }); + const err = decodeWslOutput(stderr).trim(); + if (code !== 0) { + throw createSshError("transport.unknown", new Error(err || `wsl.exe exited ${code}`)); + } + return { stdout: stdout.toString("utf8"), stderr: err }; }; } @@ -135,6 +133,7 @@ function bootstrapOptions(options: WslAgentOptions) { host: `wsl:${options.distro}`, authMode: "key-only" as const, remotePath: options.remotePath, + signal: options.signal, cachedRemoteArch: options.cachedRemoteArch, workspaceId: options.workspaceId, }; @@ -229,15 +228,13 @@ export function listWslDistros( ); } -function runWslExe(args: readonly string[]): Promise { - return new Promise((resolve, reject) => { - const child = spawn(WSL_EXE, [...args], { windowsHide: true, env: wslEnv() }); - const stdout: Buffer[] = []; - child.stdout.on("data", (chunk: Buffer) => stdout.push(chunk)); - child.on("error", reject); - child.on("close", (code) => { - if (code === 0) resolve(Buffer.concat(stdout)); - else reject(new Error(`wsl.exe exited ${code}`)); - }); +/** Runs WSL discovery with the same child ownership as bootstrap commands. */ +export async function runWslExe(args: readonly string[], spawnImpl = spawn): Promise { + const child = spawnImpl(WSL_EXE, [...args], { windowsHide: true, env: wslEnv() }); + const { stdout, code } = await collectProcess(child, { + name: WSL_EXE, + timeoutMs: WSL_LIST_TIMEOUT_MS, }); + if (code !== 0) throw new Error(`wsl.exe exited ${code}`); + return stdout; } diff --git a/src/main/error-safety-net.ts b/src/main/infra/error-safety-net.ts similarity index 98% rename from src/main/error-safety-net.ts rename to src/main/infra/error-safety-net.ts index 242a9254b..8c2e31a9b 100644 --- a/src/main/error-safety-net.ts +++ b/src/main/infra/error-safety-net.ts @@ -26,7 +26,7 @@ * needs to change. */ -import { createLogger } from "../shared/log/main"; +import { createLogger } from "../../shared/log/main"; const log = createLogger("main"); diff --git a/src/main/infra/http/body.ts b/src/main/infra/http/body.ts new file mode 100644 index 000000000..219474e22 --- /dev/null +++ b/src/main/infra/http/body.ts @@ -0,0 +1,40 @@ +export const API_RESPONSE_MAX_BYTES = 8 * 1024 * 1024; +export const RESPONSE_TOO_LARGE = "response-too-large"; + +/** Identifies a remote response that exceeded the caller's byte ceiling. */ +export class ResponseTooLargeError extends Error { + readonly code = RESPONSE_TOO_LARGE; + + constructor(maxBytes: number) { + super(`HTTP response exceeded ${maxBytes} bytes`); + } +} + +/** Reads a fetch body as UTF-8, cancelling it as soon as its decoded bytes exceed the budget. */ +export async function readResponseText( + response: Response, + maxBytes = API_RESPONSE_MAX_BYTES, +): Promise { + if (!response.body) return ""; + const reader = response.body.getReader(); + const decoder = new TextDecoder(); + const text: string[] = []; + let bytes = 0; + try { + for (;;) { + const result = await reader.read(); + if (result.done) break; + bytes += result.value.byteLength; + if (bytes > maxBytes) { + const error = new ResponseTooLargeError(maxBytes); + void reader.cancel(error).catch(() => {}); + throw error; + } + text.push(decoder.decode(result.value, { stream: true })); + } + text.push(decoder.decode()); + return text.join(""); + } finally { + reader.releaseLock(); + } +} diff --git a/src/main/infra/ipc-router/agent-errors.ts b/src/main/infra/ipc-router/agent-errors.ts new file mode 100644 index 000000000..13323298c --- /dev/null +++ b/src/main/infra/ipc-router/agent-errors.ts @@ -0,0 +1,50 @@ +import { type IpcErrResult, ipcErr } from "../../../shared/ipc/result"; +import { type SshErrorCode, SshErrorCodeSchema } from "../../../shared/ssh/errors"; +import { messageForSshErrorCode } from "../agent/pipe"; + +/** Reads a known SSH or agent transport code without exposing raw stderr. */ +export function sshErrorCode(error: unknown): SshErrorCode | undefined { + if (typeof error !== "object" || error === null || !("code" in error)) return; + const parsed = SshErrorCodeSchema.safeParse(error.code); + return parsed.success ? parsed.data : undefined; +} + +/** Recognizes cancellation even when the SSH transport wrapped the prompt error. */ +function isAuthCancellation(error: unknown): boolean { + const seen = new Set(); + while (error instanceof Error && !seen.has(error)) { + if (error.name === "AbortError" || error.name === "AuthCancelledError") return true; + seen.add(error); + error = error.cause; + } + return false; +} + +/** Envelopes expected agent failures while leaving unexpected exceptions to the router. */ +export function agentCallError( + error: unknown, + kind: "unavailable" | "auth-failed" | "connection-failed" = "unavailable", +): IpcErrResult | undefined { + const code = sshErrorCode(error); + if (code === "ssh.auth-cancelled" || isAuthCancellation(error)) { + return ipcErr("cancelled", "Operation cancelled", { + category: "cancelled", + ...(code ? { domain: "ssh", code } : {}), + }); + } + if (code) { + return ipcErr( + code === "ssh.session-expired" ? "session-expired" : kind, + messageForSshErrorCode(code), + { category: "failed", domain: "ssh", code }, + ); + } + if ( + error instanceof Error && + error.name === "AgentReconnectError" && + "code" in error && + typeof error.code === "string" + ) { + return ipcErr("unavailable", error.message, { category: "failed", code: error.code }); + } +} diff --git a/src/main/infra/ipc-router/fs-errors.ts b/src/main/infra/ipc-router/fs-errors.ts new file mode 100644 index 000000000..f7790c272 --- /dev/null +++ b/src/main/infra/ipc-router/fs-errors.ts @@ -0,0 +1,13 @@ +import { FS_ERROR } from "../../../shared/fs/errors"; +import { type IpcErrResult, ipcErr } from "../../../shared/ipc/result"; + +export type FsIpcErrorResult = IpcErrResult<"fs-error">; + +/** Envelopes filesystem failures while preserving the agent's code and message. */ +export function fsCallError(error: unknown): FsIpcErrorResult | undefined { + if (!(error instanceof Error)) return; + const { message } = error; + const code = Object.values(FS_ERROR).find((value) => message.startsWith(`${value}:`)); + if (!code) return; + return ipcErr("fs-error", message, { category: "failed", domain: "fs", code }); +} diff --git a/src/main/infra/ipc-router/index.ts b/src/main/infra/ipc-router/index.ts index 724904808..358c43e91 100644 --- a/src/main/infra/ipc-router/index.ts +++ b/src/main/infra/ipc-router/index.ts @@ -1,7 +1,7 @@ import * as crypto from "node:crypto"; import type { WebContents } from "electron"; import type { z } from "zod"; -import { createAbortError, isAbortError } from "../../../shared/abort"; +import { createAbortError } from "../../../shared/abort"; import type { AppError } from "../../../shared/error/app-error"; import { IPC_GIT_ERROR_MARK, type IpcGitErrorPayload } from "../../../shared/git/error-ipc"; import { @@ -12,8 +12,20 @@ import { type StreamProcedure, } from "../../../shared/ipc/contract"; import { PendingRequestMap } from "../../../shared/ipc/pending-request-map"; -import { type IpcErrResult, ipcErr, isIpcErrResult } from "../../../shared/ipc/result"; +import { + type IpcErrResult, + type IpcStreamStartResult, + ipcErr, + ipcOk, + isIpcErrResult, +} from "../../../shared/ipc/result"; +import { createLogger } from "../../../shared/log/main"; +import { redactLogSecrets } from "../../../shared/log/redact-secrets"; import { GitError } from "../../features/git/domain/error"; +import { agentCallError } from "./agent-errors"; +import { fsCallError } from "./fs-errors"; + +const log = createLogger("main"); export interface CallContext { requestId?: string; @@ -78,36 +90,29 @@ interface PendingStream { closed: boolean; errorSent: boolean; generatorDone: boolean; + releaseSender?: () => void; } interface ChannelDef { call: CallHandlers; listen: ListenHandlers; stream?: StreamHandlers; + /** Writes that must finish if their initiating renderer goes away. */ + keepOnRendererClose?: readonly string[]; } interface RegisteredChannelDef { call: CallHandlers; listen: ListenHandlers; stream?: RegisteredStreamHandlers; + keepOnRendererClose?: readonly string[]; } type RegisterChannelDef = C extends ContractChannelName ? RegisteredChannelDef : ChannelDef; -/** - * Thrown by `validateArgs` when the caller-supplied arguments fail Zod - * schema validation. The `ipc:call` router catches this error at the IPC - * boundary and converts it into an `IpcErrResult<"invalid-args">` with - * `category:"invalid-input"` so the renderer can branch on the result - * without catching exceptions. - * - * Handlers do not need to catch this error themselves — throwing it is the - * correct signal that propagates to the router-level conversion. Handlers - * that opt into the result-based API should use `isIpcErrResult` on the - * return value of `validateArgs` after T7 migration. - */ +/** Signals argument validation failure to the IPC boundary. */ export class IpcValidationError extends Error { readonly kind = "invalid-args" as const; readonly category = "invalid-input" as const; @@ -123,6 +128,7 @@ const pendingCallControllers = new Map(); const pendingStreamControllers = new Map(); const preCanceledRequests = new PendingRequestMap(); const PRECANCELED_REQUEST_TTL_MS = 30_000; +const MAX_PRECANCELED_REQUESTS = 1024; export function register(channelName: C, def: RegisterChannelDef): void { channels.set(channelName, def as ChannelDef); @@ -156,61 +162,41 @@ export function setupRouter(): void { requestId?: unknown, ) => { const channel = channels.get(channelName); - if (!channel) throw new Error(`ipc:call — unknown channel: ${channelName}`); - const handler = channel.call[method]; - if (typeof handler !== "function") - throw new Error(`ipc:call — unknown method: ${channelName}.${method}`); + const methods = channel?.call; + const handler = methods && Object.hasOwn(methods, method) ? methods[method] : undefined; + if (typeof handler !== "function") { + return ipcErr("invalid-args", `Unknown IPC call: ${channelName}.${method}`, { + category: "invalid-input", + }); + } const correlationId = crypto.randomUUID(); - const callContext = createCallContext(event, requestId, correlationId); + const callContext = createCallContext( + event, + requestId, + correlationId, + channel?.keepOnRendererClose?.includes(method) ?? false, + ); + const procedure = `${channelName}.${method}`; try { - const handlerResult = await handler(args, callContext.ctx); - if ( - isIpcErrResult(handlerResult) && - // `category` is an extension field set by `ipcErr(..., { category })` and - // is not declared on the base IpcErrResult interface; cast to access it. - (handlerResult as unknown as { category?: string }).category === "bug" - ) { - // Lazy-require avoids pulling electron into module scope at load time - // (shared/log/main imports electron.app which is unavailable in test env). - // The try-catch guards against module-cache mismatches in test workers - // where another file already loaded the real electron without app mocked. - try { - // eslint-disable-next-line @typescript-eslint/no-require-imports - const { createLogger } = - require("../../../shared/log/main") as typeof import("../../../shared/log/main"); - createLogger("main").error( - `ipc:call — bug result from ${channelName}.${method}: ${handlerResult.message}`, - { correlationId }, - ); - } catch { - /* logger unavailable — suppress to allow the IpcErrResult to be returned */ - } + const result = await handler(args, callContext.ctx); + if (isIpcErrResult(result) && result.category === "bug") { + return { + ...result, + ...reportCallBug(procedure, correlationId, result.message), + kind: result.kind, + }; } - return handlerResult; + return result; } catch (error) { if (error instanceof IpcValidationError) { - // Handler called validateArgs with invalid args — convert to IpcErrResult. - // This is the router-boundary enforcement: the renderer always receives - // a typed result rather than a raw rejection. - return ipcErr("invalid-args", error.message, { category: "invalid-input" as const }); - } - if (isAbortError(error)) { - try { - // eslint-disable-next-line @typescript-eslint/no-require-imports - const { createLogger } = - require("../../../shared/log/main") as typeof import("../../../shared/log/main"); - createLogger("main").debug( - `ipc:call — cancelled ${channelName}.${method} req=${requestId ?? "(none)"}`, - { correlationId }, - ); - } catch { - /* logger unavailable — suppress */ - } - return ipcErr("cancelled", "operation cancelled"); + return ipcErr("invalid-args", error.message, { category: "invalid-input" }); } - throw error; + const expected = fsCallError(error) ?? agentCallError(error); + if (expected) return expected; + const detail = error instanceof Error ? (error.stack ?? error.message) : String(error); + return reportCallBug(procedure, correlationId, detail); } finally { - if (callContext.key) pendingCallControllers.delete(callContext.key); + callContext.release?.(); } }, ); @@ -221,13 +207,15 @@ export function setupRouter(): void { channelName: string, method: string, args: unknown, - ) => { - const channel = channels.get(channelName); - if (!channel) throw new Error(`ipc:streamStart — unknown channel: ${channelName}`); - const handler = channel.stream?.[method]; + ): Promise => { + const methods = channels.get(channelName)?.stream; + const handler = methods && Object.hasOwn(methods, method) ? methods[method] : undefined; const descriptor = getStreamDescriptor(channelName, method); - if (typeof handler !== "function" || !descriptor) - throw new Error(`ipc:streamStart — unknown method: ${channelName}.${method}`); + if (typeof handler !== "function" || !descriptor) { + return ipcErr("invalid-args", `Unknown IPC stream: ${channelName}.${method}`, { + category: "invalid-input", + }); + } const streamId = crypto.randomUUID(); const key = requestKey(event, streamId); const pendingStream: PendingStream = { @@ -240,14 +228,62 @@ export function setupRouter(): void { generatorDone: false, }; pendingStreamControllers.set(key, pendingStream); + pendingStream.releaseSender = trackSender(event.sender, () => { + detachStream(key, pendingStream); + pendingStream.controller.abort(); + void closeStreamGenerator(pendingStream); + }); setImmediate(() => { void runStream(key, pendingStream, descriptor, handler, args); }); - return { streamId }; + return ipcOk({ streamId }); }, ); } +/** Records masked diagnostic detail while exposing only a correlation ID and safe message. */ +function reportCallBug(procedure: string, correlationId: string, detail: string): IpcErrResult { + log.error(`ipc:call — ${procedure}: ${redactLogSecrets(detail)}`, { correlationId }); + return ipcErr("bug", "An unexpected error occurred", { category: "bug", correlationId }); +} + +interface SenderLifetime { + readonly cleanup: Set<() => void>; + readonly destroyed: () => void; +} + +const senderLifetimes = new WeakMap(); + +/** Owns pending work with one destroy listener per renderer. */ +function trackSender(sender: WebContents | undefined, abandon: () => void): () => void { + if (!sender?.once) return () => {}; + if (sender.isDestroyed()) { + abandon(); + return () => {}; + } + + let lifetime = senderLifetimes.get(sender); + if (!lifetime) { + const cleanup = new Set<() => void>(); + const destroyed = () => { + senderLifetimes.delete(sender); + const callbacks = [...cleanup]; + cleanup.clear(); + for (const cancel of callbacks) cancel(); + }; + lifetime = { cleanup, destroyed }; + senderLifetimes.set(sender, lifetime); + sender.once("destroyed", destroyed); + } + const { cleanup, destroyed } = lifetime; + cleanup.add(abandon); + return () => { + if (!cleanup.delete(abandon) || cleanup.size > 0) return; + sender.removeListener("destroyed", destroyed); + senderLifetimes.delete(sender); + }; +} + function senderId(event: { sender?: { id?: number } }): string { return typeof event.sender?.id === "number" ? String(event.sender.id) : "unknown"; } @@ -258,6 +294,9 @@ function requestKey(event: { sender?: { id?: number } }, requestId: string): str function rememberPreCanceledRequest(key: string): void { if (preCanceledRequests.has(key)) preCanceledRequests.reject(key, new Error("replaced")); + else if (preCanceledRequests.size >= MAX_PRECANCELED_REQUESTS) { + preCanceledRequests.rejectOldest(new Error("pre-cancellation limit reached")); + } preCanceledRequests.register({ key, timeoutMs: PRECANCELED_REQUEST_TTL_MS }).catch(() => {}); } @@ -267,18 +306,31 @@ function consumePreCanceledRequest(key: string): boolean { function createCallContext( event: import("electron").IpcMainInvokeEvent, - requestId: unknown, + suppliedId: unknown, correlationId: string, -): { key?: string; ctx?: CallContext } { + keepOnRendererClose: boolean, +): { release?: () => void; ctx: CallContext } { const sender = event.sender; - if (typeof requestId !== "string" || requestId.length === 0) { - return { ctx: { correlationId, sender } }; - } + const requestId = + typeof suppliedId === "string" && suppliedId.length > 0 ? suppliedId : crypto.randomUUID(); const key = requestKey(event, requestId); const controller = new AbortController(); pendingCallControllers.set(key, controller); + const remove = () => { + if (pendingCallControllers.get(key) === controller) pendingCallControllers.delete(key); + }; + const releaseSender = trackSender(sender, () => { + remove(); + if (!keepOnRendererClose) controller.abort(); + }); if (consumePreCanceledRequest(key)) controller.abort(); - return { key, ctx: { requestId, signal: controller.signal, correlationId, sender } }; + return { + release: () => { + remove(); + releaseSender(); + }, + ctx: { requestId, signal: controller.signal, correlationId, sender }, + }; } function getStreamDescriptor(channelName: string, method: string): AnyStreamProcedure | undefined { @@ -373,15 +425,23 @@ async function runStream( } } -async function cleanupStream(key: string, pendingStream: PendingStream): Promise { - const shouldCloseGenerator = - pendingStream.generator && - !pendingStream.generatorDone && - !pendingStream.closed && - !pendingStream.controller.signal.aborted; +/** Drops routing and renderer ownership before waiting for handler cleanup. */ +function detachStream(key: string, pendingStream: PendingStream): void { pendingStream.closed = true; - pendingStreamControllers.delete(key); - if (shouldCloseGenerator) await pendingStream.generator?.return(undefined).catch(() => {}); + if (pendingStreamControllers.get(key) === pendingStream) pendingStreamControllers.delete(key); + pendingStream.releaseSender?.(); +} + +/** Returns an unfinished iterator once, including one supplied after cancellation. */ +async function closeStreamGenerator(pendingStream: PendingStream): Promise { + if (!pendingStream.generator || pendingStream.generatorDone) return; + pendingStream.generatorDone = true; + await pendingStream.generator.return(undefined).catch(() => {}); +} + +async function cleanupStream(key: string, pendingStream: PendingStream): Promise { + detachStream(key, pendingStream); + await closeStreamGenerator(pendingStream); } function shouldStopStream(key: string, pendingStream: PendingStream): boolean { @@ -396,9 +456,8 @@ function cancelStream(key: string, pendingStream: PendingStream): void { pendingStream.controller.abort(); if (pendingStream.cancelMode === "handler") return; sendStreamError(pendingStream, createAbortError()); - pendingStream.closed = true; - pendingStreamControllers.delete(key); - void pendingStream.generator?.return(undefined).catch(() => {}); + detachStream(key, pendingStream); + void closeStreamGenerator(pendingStream); } function sendStreamEvent(pendingStream: PendingStream, payload: StreamEventPayload): void { @@ -488,28 +547,7 @@ export function broadcast(channelName: string, event: string, args: unknown): vo } } -/** - * Validate handler arguments against a Zod schema. - * - * On success returns the parsed (typed) value. On failure throws - * `IpcValidationError` — the `ipc:call` router catches this at the IPC - * boundary and converts it into an `IpcErrResult<"invalid-args">` with - * `category:"invalid-input"` so the renderer always receives a typed result. - * - * Handlers do not need to catch `IpcValidationError` themselves. Handlers - * that adopt the result-based API (T7 migration) should instead call this - * function and check `isIpcErrResult` on the return — until then the throw - * path is fully supported and backward-compatible with all existing handlers. - * - * @example - * // Legacy throw style (pre-T7): - * const { workspaceId, relPath } = validateArgs(c.readdir.args, args); - * - * // Result style (post-T7): - * const parsed = validateArgs(c.readdir.args, args); - * if (isIpcErrResult(parsed)) return parsed; - * const { workspaceId, relPath } = parsed; - */ +/** Parses handler arguments and lets the router envelope validation failures. */ export function validateArgs(schema: T, args: unknown): z.infer { const parsed = schema.safeParse(args); if (!parsed.success) { diff --git a/src/main/infra/main-shutdown.ts b/src/main/infra/main-shutdown.ts new file mode 100644 index 000000000..080368e41 --- /dev/null +++ b/src/main/infra/main-shutdown.ts @@ -0,0 +1,77 @@ +import { defaultTimerScheduler, type TimerScheduler } from "../../shared/util/timer-scheduler"; + +export const MAIN_QUIT_GRACE_MS = 3_000; +// Match the Go LSP request, exit and forced-exit grace periods. +const LSP_QUIT_GRACE_MS = 2_250; + +interface ShutdownWork { + readonly waitForNotes: () => Promise | undefined; + readonly disposeGitHelpers: () => Promise | undefined; + readonly lspShutdown?: Promise; + readonly closeWorkspaces: () => void; + readonly waitForLocalAgents: () => Promise; + readonly warn: (message: string) => void; +} + +/** Drains writes and children in parallel, with a shared deadline for the whole quit. */ +export async function finishMainShutdown( + work: ShutdownWork, + remainingMs: number, + timers: TimerScheduler = defaultTimerScheduler, +): Promise { + let deadline: unknown; + const expired = new Promise((resolve) => { + deadline = timers.setTimeout(() => resolve(false), remainingMs); + }); + let lspTimer: unknown; + let workspacesClosed = false; + + /** Closes agents once, including when the total deadline beats LSP. */ + function closeWorkspaces(): void { + if (workspacesClosed) return; + workspacesClosed = true; + work.closeWorkspaces(); + } + + /** Gives LSP its own grace while keeping the transports it needs alive. */ + async function stopAgents(): Promise { + try { + if (work.lspShutdown) { + const lspGrace = new Promise((resolve) => { + lspTimer = timers.setTimeout(resolve, LSP_QUIT_GRACE_MS); + }); + await Promise.race([work.lspShutdown, lspGrace, expired]); + } + } finally { + closeWorkspaces(); + await work.waitForLocalAgents(); + } + } + + const tasks = new Map([ + ["note writes", work.waitForNotes], + ["Git helper socket", work.disposeGitHelpers], + ["agents", stopAgents], + ]); + const completed = Promise.all( + [...tasks].map(async ([name, finish]) => { + try { + await finish(); + } catch (error) { + work.warn(`Shutdown failed for ${name}: ${String(error)}`); + } finally { + tasks.delete(name); + } + }), + ).then(() => true); + + try { + if (!(await Promise.race([completed, expired]))) { + work.warn(`App shutdown exceeded ${MAIN_QUIT_GRACE_MS} ms (${[...tasks.keys()].join(", ")})`); + } + } finally { + timers.clearTimeout(deadline); + if (lspTimer !== undefined) timers.clearTimeout(lspTimer); + closeWorkspaces(); + } +} diff --git a/src/main/infra/shell-path.ts b/src/main/infra/platform/shell-path.ts similarity index 97% rename from src/main/infra/shell-path.ts rename to src/main/infra/platform/shell-path.ts index f53c90e45..9cd0a288a 100644 --- a/src/main/infra/shell-path.ts +++ b/src/main/infra/platform/shell-path.ts @@ -1,5 +1,5 @@ import { execFileSync } from "node:child_process"; -import { isLinux, isMac, loginShell } from "./platform"; +import { isLinux, isMac, loginShell } from "./"; /** * A macOS GUI app is started by launchd, so it only gets the system default PATH diff --git a/src/main/infra/security/script-readable-request.ts b/src/main/infra/security/script-readable-request.ts index ce0597164..458617433 100644 --- a/src/main/infra/security/script-readable-request.ts +++ b/src/main/infra/security/script-readable-request.ts @@ -8,9 +8,8 @@ import path from "node:path"; // Access-Control-Allow-Origin, and file:// pages get extra privileges by // default, so a page script can read `.env`, `.git/config` or `~/.ssh` and // send it elsewhere. Requests are filtered by type instead: subresources a -// page renders with (images, fonts, styles, frames, JS/CSS module scripts) -// load; script-readable fetches (XHR/fetch, JSON modules) are cancelled — -// what Chrome allows a page opened from file://. +// page renders with (images, fonts, styles, JS/CSS module scripts) can load +// without granting script-readable fetches (XHR/fetch, JSON modules). // --------------------------------------------------------------------------- const MODULE_SCRIPT_EXTENSIONS = new Set([".js", ".mjs", ".cjs", ".css"]); diff --git a/src/main/infra/storage/global-storage.ts b/src/main/infra/storage/global-storage.ts index f81db7dc9..32da1d4ca 100644 --- a/src/main/infra/storage/global-storage.ts +++ b/src/main/infra/storage/global-storage.ts @@ -10,6 +10,7 @@ import { type WorkspaceTask, WorkspaceTaskSchema, } from "../../../shared/types/workspace"; +import { toWorkspaceColor } from "../../../shared/types/workspace-color"; import { applyMigrations, type SqliteDb } from "./migrations"; // --------------------------------------------------------------------------- @@ -132,7 +133,7 @@ interface WorkspaceRow { name: string; root_path: string; location: string | null; - color_tone: string; + color: string; pinned: number; last_opened_at: number; sort_order: number; @@ -231,7 +232,7 @@ function rowToMeta(row: WorkspaceRow): WorkspaceMeta { name: row.name, location, rootPath: rootPathFromLocation(location), - colorTone: row.color_tone as WorkspaceMeta["colorTone"], + color: toWorkspaceColor(row.color), pinned: row.pinned === 1, lastOpenedAt: new Date(row.last_opened_at).toISOString(), tabs: [], @@ -293,7 +294,7 @@ export class GlobalStorage { this.db .prepare( `INSERT INTO workspaces - (id, name, root_path, location, color_tone, pinned, last_opened_at, + (id, name, root_path, location, color, pinned, last_opened_at, sort_order, pinned_sort_order, task) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, ) @@ -302,7 +303,7 @@ export class GlobalStorage { normalized.name, rootPath, JSON.stringify(location), - normalized.colorTone ?? "default", + normalized.color, normalized.pinned ? 1 : 0, normalized.lastOpenedAt ? new Date(normalized.lastOpenedAt).getTime() : Date.now(), normalized.sortOrder, @@ -334,14 +335,14 @@ export class GlobalStorage { this.db .prepare( `UPDATE workspaces - SET name = ?, root_path = ?, location = ?, color_tone = ?, pinned = ?, last_opened_at = ? + SET name = ?, root_path = ?, location = ?, color = ?, pinned = ?, last_opened_at = ? WHERE id = ?`, ) .run( partial.name ?? row.name, rootPath, locationJson, - (partial.colorTone ?? row.color_tone) as string, + partial.color ?? toWorkspaceColor(row.color), partial.pinned !== undefined ? (partial.pinned ? 1 : 0) : row.pinned, partial.lastOpenedAt ? new Date(partial.lastOpenedAt).getTime() : row.last_opened_at, id, diff --git a/src/main/infra/storage/migrations.ts b/src/main/infra/storage/migrations.ts index d3a3c2555..dd94ffb05 100644 --- a/src/main/infra/storage/migrations.ts +++ b/src/main/infra/storage/migrations.ts @@ -289,6 +289,19 @@ export const MIGRATIONS: Migration[] = [ db.exec(`ALTER TABLE session_spans ADD COLUMN agent TEXT`); }, }, + // A workspace's color replaces its tone (subdued, default, prominent, pinned), + // which no screen used: every workspace starts as "auto", whatever its tone + // was. The `color_tone` column stays, unused, so an older build opening the + // same database still inserts into it (it has a DEFAULT) and a downgrade is + // safe; nothing reads or writes it now. Idempotent. + { + version: 10, + up: (db) => { + if (!hasColumn(db, "workspaces", "color")) { + db.exec("ALTER TABLE workspaces ADD COLUMN color TEXT NOT NULL DEFAULT 'auto';"); + } + }, + }, ]; export function applyMigrations(db: SqliteDb): void { diff --git a/src/main/infra/storage/state-service.ts b/src/main/infra/storage/state-service.ts index 3c6ea8837..2c0a7c34c 100644 --- a/src/main/infra/storage/state-service.ts +++ b/src/main/infra/storage/state-service.ts @@ -1,6 +1,7 @@ import fs from "node:fs"; import path from "node:path"; import { z } from "zod"; +import { createLogger } from "../../../shared/log/main"; import { type AppState, AppStateSchema, @@ -9,6 +10,8 @@ import { } from "../../../shared/types/app-state"; import { migrateAppState } from "./app-state-migrations"; +const log = createLogger("storage"); + export { type AppState, AppStateSchema, type WindowBounds, WindowBoundsSchema }; // --------------------------------------------------------------------------- @@ -61,9 +64,10 @@ export class StateService { } setState(partial: Partial): void { - AppStateSchema.parse({ ...this.state, ...partial }); - this.state = { ...this.state, ...partial }; - this.flush(); + const next = { ...this.state, ...partial }; + AppStateSchema.parse(next); + this.flush(next); + this.state = next; } mergeState(partial: Partial): void { @@ -82,20 +86,31 @@ export class StateService { this.flush(); } - private flush(): void { + private flush(state = this.state): void { const dir = path.dirname(this.filePath); fs.mkdirSync(dir, { recursive: true }); - const serialized = JSON.stringify(this.state, null, 2); + const serialized = JSON.stringify(state, null, 2); // fsync before the rename so a crash or power loss cannot leave the // renamed file empty. const fd = fs.openSync(this.tmpPath, "w"); try { - fs.writeSync(fd, serialized, null, "utf8"); - fs.fsyncSync(fd); - } finally { - fs.closeSync(fd); + try { + fs.writeSync(fd, serialized, null, "utf8"); + fs.fsyncSync(fd); + } finally { + fs.closeSync(fd); + } + fs.renameSync(this.tmpPath, this.filePath); + } catch (error) { + try { + fs.unlinkSync(this.tmpPath); + } catch (cleanupError) { + if ((cleanupError as NodeJS.ErrnoException).code !== "ENOENT") { + log.warn(`Could not remove temporary app state: ${String(cleanupError)}`); + } + } + throw error; } - fs.renameSync(this.tmpPath, this.filePath); } } diff --git a/src/preload/index.ts b/src/preload/index.ts index 4b05fe573..3a3491fae 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -1,4 +1,5 @@ import { contextBridge, ipcRenderer, webUtils } from "electron"; +import type { IpcStreamStartResult } from "../shared/ipc/result"; import { OPAQUE_WINDOW_ARG, TRANSPARENT_WINDOW_ARG } from "../shared/window/transparent-window"; type ListenCallback = (args: unknown) => void; @@ -49,14 +50,20 @@ const ipcApi = { args: unknown, requestId?: IpcRequestId, ): Promise { - return ipcRenderer.invoke("ipc:call", channelName, method, args, requestId); + return ipcRenderer.invoke( + "ipc:call", + channelName, + method, + args, + requestId ?? crypto.randomUUID(), + ); }, cancel(requestId: IpcRequestId): void { ipcRenderer.send("ipc:cancel", requestId); }, - streamStart(channelName: string, method: string, args: unknown): Promise<{ streamId: string }> { + streamStart(channelName: string, method: string, args: unknown): Promise { return ipcRenderer.invoke("ipc:streamStart", channelName, method, args); }, diff --git a/src/renderer/app.tsx b/src/renderer/app.tsx index af3242b47..8c3cfbc81 100644 --- a/src/renderer/app.tsx +++ b/src/renderer/app.tsx @@ -1,8 +1,8 @@ import { useMonaco } from "@monaco-editor/react"; -import { useCallback, useEffect, useRef } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { createLogger } from "../shared/log/renderer"; import type { WorkspaceMeta } from "../shared/types/workspace"; -import { bootstrapAppState, bootstrapClaudeStatus, bootstrapWorkspaces } from "./bootstrap"; +import { bootstrapClaudeStatus, bootstrapWorkbench } from "./bootstrap"; import { useCommandBridge } from "./commands/use-command-bridge"; import { AgentSidebar } from "./components/agent-view/agent-sidebar"; import { AgentView } from "./components/agent-view/agent-view"; @@ -40,6 +40,7 @@ import { LOOK_AREA_ATTR } from "./services/chat/look-area"; import { initializeEditorServices } from "./services/editor"; import { rehydrateLspForWorkspace } from "./services/editor/model/cache"; import { countUnsavedBuffers } from "./services/editor/save/unsaved-guard"; +import { activateWorkspace, focusWorkspace } from "./state/operations/workspace-selection"; import { useActiveStore } from "./state/stores/active"; import { useAddWorkspaceUIStore } from "./state/stores/add-workspace-ui"; import { useAgentViewStore } from "./state/stores/agent-view"; @@ -60,9 +61,8 @@ export function App() { // Picked one by one: the whole store would render the app, every // workspace's tabs below it, for each connection status and progress. const workspaces = useWorkspacesStore((s) => s.workspaces); - const setAll = useWorkspacesStore((s) => s.setAll); const activeWorkspaceId = useActiveStore((s) => s.activeWorkspaceId); - const setActiveWorkspaceId = useActiveStore((s) => s.setActiveWorkspaceId); + const [restoringWorkbench, setRestoringWorkbench] = useState(true); const settingsOpen = useSettingsUIStore((s) => s.settingsOpen); const settingsInitialActiveId = useSettingsUIStore((s) => s.initialActiveId); const closeSettings = useSettingsUIStore((s) => s.closeSettings); @@ -92,22 +92,13 @@ export function App() { initializeEditorServices(monaco); }, [monaco]); - // Boot: hydrate UI state (sidebar width, files panel) and layout/tabs from persisted app state. - useEffect(() => { - bootstrapAppState(); - }, []); - - // Boot: Claude 세션 상태 snapshot 로드 및 status broadcast 구독 설정. + // The workspace list arrives before the restored selection. Wait for both + // before showing a welcome screen. useEffect(() => { + void bootstrapWorkbench().finally(() => setRestoringWorkbench(false)); void bootstrapClaudeStatus(); }, []); - // Boot: load workspaces from main, activate first. - // biome-ignore lint/correctness/useExhaustiveDependencies: boot-once effect; store setters are stable - useEffect(() => { - bootstrapWorkspaces(setAll, setActiveWorkspaceId); - }, []); - // Mark the active workspace as mounted (one-way; lazy-mount + persist). useEffect(() => { if (!activeWorkspaceId) return; @@ -146,62 +137,43 @@ export function App() { // When the active workspace disappears (deleted, etc.), fall back to its // parent for a task, else to the first workspace, or null. useEffect(() => { + if (useActiveStore.getState().activeWorkspaceId !== activeWorkspaceId) return; if (activeWorkspaceId && !workspaces.some((w) => w.id === activeWorkspaceId)) { const parentId = activeParentIdRef.current; const next = (parentId !== null && workspaces.some((w) => w.id === parentId) ? parentId : visibleWorkspaces(workspaces)[0]?.id) ?? null; - setActiveWorkspaceId(next); - if (next) { - // Fire-and-forget: UI is already updated; notify main of workspace switch. - void ipcCallResult("workspace", "activate", { id: next }).then((result) => { - if (!result.ok) log.warn(`workspace activate failed: ${result.message}`); - }); - } + if (next) activateWorkspace(next, "preserve"); + else focusWorkspace(null, "preserve"); } - }, [workspaces, activeWorkspaceId, setActiveWorkspaceId]); + }, [workspaces, activeWorkspaceId]); // Read from the stores when called, so it stays the same function across // switches and the sidebar's rows are not handed a new one each time. - const handleSelectWorkspace = useCallback( - (id: string) => { - // The sidebar row stands for the workspace and its tasks; clicking the - // row the active task belongs to keeps that task. - const activeWorkspaceId = useActiveStore.getState().activeWorkspaceId; - const workspaces = useWorkspacesStore.getState().workspaces; - if ( - activeWorkspaceId !== null && - activeWorkspaceId !== id && - rootWorkspaceId(workspaces, activeWorkspaceId) === id - ) { - return; - } - setActiveWorkspaceId(id); - // Fire-and-forget: UI is already updated; notify main of workspace switch. - void ipcCallResult("workspace", "activate", { id }).then((result) => { - if (!result.ok) log.warn(`workspace activate failed: ${result.message}`); - }); - }, - [setActiveWorkspaceId], - ); + const handleSelectWorkspace = useCallback((id: string) => { + // The sidebar row stands for the workspace and its tasks; clicking the + // row the active task belongs to keeps that task. + const activeWorkspaceId = useActiveStore.getState().activeWorkspaceId; + const workspaces = useWorkspacesStore.getState().workspaces; + if ( + activeWorkspaceId !== null && + activeWorkspaceId !== id && + rootWorkspaceId(workspaces, activeWorkspaceId) === id + ) { + return; + } + activateWorkspace(id, "preserve"); + }, []); const handleAddWorkspace = useCallback(() => { openAddWorkspace(); }, [openAddWorkspace]); - const handleWorkspaceCreated = useCallback( - async (meta: WorkspaceMeta) => { - useAgentViewStore.getState().setMode("workspace"); - setActiveWorkspaceId(meta.id); - // Fire-and-forget: UI is already updated; notify main of new workspace activation. - void ipcCallResult("workspace", "activate", { id: meta.id }).then((result) => { - if (!result.ok) log.warn(`workspace activate failed: ${result.message}`); - }); - // Tab seeding is handled by on first mount. - }, - [setActiveWorkspaceId], - ); + const handleWorkspaceCreated = useCallback(async (meta: WorkspaceMeta) => { + activateWorkspace(meta.id); + // Tab seeding is handled by on first mount. + }, []); const handleCloseAddWorkspace = useCallback(() => { closeAddWorkspace(); @@ -311,9 +283,13 @@ export function App() { className="relative grid grid-cols-1 grid-rows-1 flex-1 min-w-0 overflow-hidden" {...{ [LOOK_AREA_ATTR]: "" }} > - {workspaces.length === 0 && !agentView && ( -

- + {/* In the one cell, as the panels are: the agent view, kept + mounted there, would push it into a row of its own below. */} + {!restoringWorkbench && activeWorkspaceId === null && !agentView && ( +
+
)} {mountedWorkspaces.map((ws) => ( diff --git a/src/renderer/bootstrap.ts b/src/renderer/bootstrap.ts index cd29b857d..70e0845fa 100644 --- a/src/renderer/bootstrap.ts +++ b/src/renderer/bootstrap.ts @@ -7,30 +7,27 @@ import { createLogger } from "../shared/log/renderer"; import { lspSettingsFromState } from "../shared/lsp/settings"; -import type { WorkspaceMeta } from "../shared/types/workspace"; import { ipcCallResult, ipcListen, mustSucceed } from "./ipc/client"; import { startTabActivityPolling } from "./services/agent-sessions/activity-poll"; import { startSessionEvents } from "./services/agent-sessions/session-events"; import { startAgentViewPruning, tabExists } from "./services/agent-view/show"; -import { markCliUiReady, startCliUiListener } from "./services/cli/ui-requests"; +import { initBrowserOverlayAutoSuspend } from "./services/browser/suspend-auto"; +import { startCliUiListener } from "./services/cli/ui-requests"; +import { initGitErrorToasts } from "./services/error-surface/git-error-toasts"; +import { initUpdatesSubscriptions } from "./services/updates/operations"; import { startClaudeActiveContextSync } from "./state/claude-active-context-sync"; import { startNoteRemovalListener } from "./state/note-removal"; import { startNotificationClickListener } from "./state/notification-click"; -import { - initBrowserLastUrlPersistence, - initBrowserRuntimeSubscriptions, -} from "./state/operations/browser"; +import { initBrowserRuntimeSubscriptions } from "./state/operations/browser"; import { initBrowserPermissionSubscriptions } from "./state/operations/browser-permission"; -import { initBrowserOverlayAutoSuspend } from "./state/operations/browser-suspend-auto"; import { initFocusResync } from "./state/operations/focus-resync"; -import { initGitErrorToasts } from "./state/operations/git-error-toasts"; import { initLspSettingsSync } from "./state/operations/lsp-settings"; import { initLspStatusSync } from "./state/operations/lsp-status"; import { initPortsSync } from "./state/operations/ports"; import { hydrateReleaseNotesSeen } from "./state/operations/release-notes"; -import { normalizeWorkspaceTabOrder } from "./state/operations/tab-order"; -import { initUpdatesSubscriptions } from "./state/operations/updates"; -import { registerStatePersistence } from "./state/persistence"; +import { focusWorkspace } from "./state/operations/workspace-selection"; +import { hydrateLayoutSnapshots, registerStatePersistence } from "./state/persistence"; +import { useActiveStore } from "./state/stores/active"; import { useAgentCliStore } from "./state/stores/agent-cli"; import { useAgentListViewStore } from "./state/stores/agent-list-view"; import { loadProfilesHome, useAgentProfilesStore } from "./state/stores/agent-profiles"; @@ -44,18 +41,16 @@ import { useIconThemeStore } from "./state/stores/icon-theme"; import { useInactivePanelDimStore } from "./state/stores/inactive-panel-dim"; import { useKeybindingsStore } from "./state/stores/keybindings"; import { useLanguageStore } from "./state/stores/language"; -import { useLayoutStore } from "./state/stores/layout"; import { useLspSettingsStore } from "./state/stores/lsp-settings"; import { useNotificationsStore } from "./state/stores/notifications"; import { usePreviewDefaultsStore } from "./state/stores/preview-defaults"; -import { restoredTitles, useTabsStore } from "./state/stores/tabs"; import { useTerminalStore } from "./state/stores/terminal"; import { useThemeStore } from "./state/stores/theme"; import { useUIStore } from "./state/stores/ui"; import { useUpdatesStore } from "./state/stores/updates"; import { useVoiceStore } from "./state/stores/voice"; import { useWindowOpacityStore } from "./state/stores/window-opacity"; -import { restoredWorkspace } from "./state/stores/workspaces"; +import { restoredWorkspace, useWorkspacesStore } from "./state/stores/workspaces"; import { startTerminalExitListener } from "./state/terminal-exit"; import { initializeWorkspaceLifecycle } from "./state/workspace-cleanup"; @@ -98,7 +93,7 @@ function installGlobalFileDropGuard(): void { }); } -export async function bootstrapAppState(): Promise { +async function bootstrapAppState(): Promise { // Install the central workspace:removed listener before any async I/O — // registered cleanup functions sit in memory regardless, but the listener // itself must be live before the first user-initiated workspace removal. @@ -108,12 +103,9 @@ export async function bootstrapAppState(): Promise { // installGlobalFileDropGuard for the file:// navigation hazard it prevents. installGlobalFileDropGuard(); - // Wire the OS notification click → workspace activate + tab reveal listener. + // Reveal the tab a clicked OS notification points to. startNotificationClickListener(); - // Answer the nexus CLI's requests; they wait until the tabs are restored. - startCliUiListener(); - // Mark terminal tabs dead on PTY exit even while their view is unmounted. startTerminalExitListener(); @@ -127,11 +119,6 @@ export async function bootstrapAppState(): Promise { // error / titleUpdated) so useBrowserRuntimeStore stays up-to-date. initBrowserRuntimeSubscriptions(); - // Wire last-URL persistence: debounces currentUrl changes from the runtime - // store and flushes them to the tabs store so the URL survives app restarts. - // Must be called after initBrowserRuntimeSubscriptions. - initBrowserLastUrlPersistence(); - // Wire the browser permission prompt subscription so browserPermission:prompt // broadcasts from main are forwarded to the PermissionPromptRoot queue. initBrowserPermissionSubscriptions(); @@ -283,41 +270,7 @@ export async function bootstrapAppState(): Promise { // reflects persisted state on first render. useBrowserPermissionsStore.getState().hydrate(state.browserPermissionGrants); - if (state.layoutByWorkspace) { - for (const [wsId, snap] of Object.entries(state.layoutByWorkspace)) { - try { - const tabsMap: Record< - string, - (typeof snap.tabs)[number] & { - isPreview: boolean; - isPinned: boolean; - defaultTitle: string; - } - > = {}; - for (const t of snap.tabs) { - const isPreview = - "isPreview" in t && typeof t.isPreview === "boolean" ? t.isPreview : false; - const isPinned = "isPinned" in t && typeof t.isPinned === "boolean" ? t.isPinned : false; - // Its titles as saved: a rename stays the user's, and a name the - // app gives follows its language (restoredTitles). - tabsMap[t.id] = { ...t, isPreview, isPinned, ...restoredTitles(t) }; - } - useTabsStore.setState((s) => ({ - byWorkspace: { ...s.byWorkspace, [wsId]: tabsMap }, - })); - - const knownTabIds = new Set(snap.tabs.map((t) => t.id)); - useLayoutStore - .getState() - .hydrate(wsId, { root: snap.root, activeGroupId: snap.activeGroupId }, knownTabIds); - // Layouts saved before pinning reordered tabs can hold pinned tabs - // behind unpinned ones. - normalizeWorkspaceTabOrder(wsId); - } catch { - // Silent repair: skip invalid snapshot for this workspace - } - } - } + hydrateLayoutSnapshots(state.layoutByWorkspace); // The agent view's panels hold tabs, so they come back after the tabs, // keeping those that still exist, and let go of each tab as it closes. @@ -325,7 +278,6 @@ export async function bootstrapAppState(): Promise { startAgentViewPruning(); registerStatePersistence(); - markCliUiReady(); // Dev-only console helper for testing window transparency before a settings // UI exists. `__setWindowOpacity(0.5)` dual-writes localStorage + appState @@ -343,26 +295,41 @@ export async function bootstrapAppState(): Promise { } /** - * Load the workspace list from main, push it into the store, and - * restore the active workspace selection. - * - * Does NOT call workspace:activate at startup. Main's init() is the source - * of truth for the restored active workspace and its conditional auto-connect - * (key-only SSH and local connect automatically; interactive SSH restores in - * the disconnected state), so the selection is the workspace main restored. - * Calling activate here would both trigger a connection and wrongly bump the - * recency list on every app launch. + * Load the workspace list and restore the selection main recorded. Main + * owns startup connection policy, including interactive SSH workspaces + * that wait for a user to connect. */ -export async function bootstrapWorkspaces( - setAll: (list: WorkspaceMeta[]) => void, - setActiveWorkspaceId: (id: string | null) => void, -): Promise { +export async function bootstrapWorkspaces(): Promise { // Bootstrap is an initialization path — no recovery possible if workspace list is unavailable. - const list = mustSucceed(await ipcCallResult("workspace", "list", undefined)); - setAll(list); + const [workspaces, connections] = await Promise.all([ + ipcCallResult("workspace", "list", undefined), + ipcCallResult("workspace", "connectionSnapshot", undefined).catch((error: unknown) => { + log.warn(`Workspace connection snapshot failed: ${String(error)}`); + }), + ]); + if (connections && !connections.ok) + log.warn(`Workspace connection snapshot failed: ${connections.message}`); + const list = mustSucceed(workspaces); + useWorkspacesStore.getState().setAll(list, connections?.ok ? connections.value : undefined); const active = await ipcCallResult("workspace", "active", undefined); - const selected = restoredWorkspace(list, active.ok ? active.value : null); - if (selected) setActiveWorkspaceId(selected.id); + + // The sidebar can already be used while the active-ID reply is pending. + if (useActiveStore.getState().activeWorkspaceId !== null) return; + const currentList = useWorkspacesStore.getState().workspaces; + const selected = restoredWorkspace(currentList, active.ok ? active.value : null); + if (selected) focusWorkspace(selected.id, "preserve"); +} + +/** + * Restore tabs before the workspace can mount and seed a terminal. CLI + * requests can arrive throughout startup and wait for both restorations. + */ +export function bootstrapWorkbench( + hydrateAppState: () => Promise = bootstrapAppState, +): Promise { + const ready = hydrateAppState().then(bootstrapWorkspaces); + startCliUiListener(ready); + return ready; } /** diff --git a/src/renderer/commands/domains/palette.ts b/src/renderer/commands/domains/palette.ts index 25b3454a9..5773d4f3d 100644 --- a/src/renderer/commands/domains/palette.ts +++ b/src/renderer/commands/domains/palette.ts @@ -1,6 +1,6 @@ import { COMMANDS } from "../../../shared/keybindings/commands"; import { registerCommand } from "../../commands/registry"; -import { openWorkspaceSymbolPalette } from "../../components/symbol-palette/workspace-symbol-palette-state"; +import { openWorkspaceSymbolPalette } from "../../state/stores/workspace-symbol-palette"; export function registerPaletteCommands(): Array<() => void> { return [registerCommand(COMMANDS.workspaceSymbolSearch, openWorkspaceSymbolPalette)]; diff --git a/src/renderer/commands/domains/workspace.ts b/src/renderer/commands/domains/workspace.ts index f230af19c..db4e0da5d 100644 --- a/src/renderer/commands/domains/workspace.ts +++ b/src/renderer/commands/domains/workspace.ts @@ -1,6 +1,7 @@ import { COMMANDS } from "../../../shared/keybindings/commands"; import { registerCommand } from "../../commands/registry"; import { goToNextWaiting } from "../../services/agent-sessions/use-inbox"; +import { activateWorkspace } from "../../state/operations/workspace-selection"; import { useActiveStore } from "../../state/stores/active"; import { useAddWorkspaceUIStore } from "../../state/stores/add-workspace-ui"; import { useTaskUIStore } from "../../state/stores/task-ui"; @@ -16,22 +17,13 @@ import { * existing `compareSortKey` order). Wraps at both ends so ⌘⌃↑ from * the first workspace lands on the last, and vice-versa. * - * No-op when there is at most one workspace, or when the active id is - * absent from the list (e.g. mid-removal); we fall back to the first - * row in that case so the shortcut still feels responsive. A task counts - * as its parent's row, so the step starts there. + * If the selection is missing from the list, start at the natural end + * for the direction. A task counts as its parent's row. */ export function cycleActiveWorkspace(delta: 1 | -1): void { const all = useWorkspacesStore.getState().workspaces; const workspaces = visibleWorkspaces(all); if (workspaces.length === 0) return; - if (workspaces.length === 1) { - // Still set it explicitly so a fresh boot with no active id picks - // up the only available workspace on the very first keystroke. - useActiveStore.getState().setActiveWorkspaceId(workspaces[0].id); - return; - } - const activeId = useActiveStore.getState().activeWorkspaceId; const rowId = activeId !== null ? rootWorkspaceId(all, activeId) : null; const currentIndex = rowId !== null ? workspaces.findIndex((w) => w.id === rowId) : -1; @@ -44,7 +36,8 @@ export function cycleActiveWorkspace(delta: 1 | -1): void { nextIndex = (currentIndex + delta + workspaces.length) % workspaces.length; } - useActiveStore.getState().setActiveWorkspaceId(workspaces[nextIndex].id); + const next = workspaces[nextIndex].id; + if (next !== activeId) activateWorkspace(next, "preserve"); } export function registerWorkspaceCommands(): Array<() => void> { diff --git a/src/renderer/components/agent-list/status-view.tsx b/src/renderer/components/agent-list/status-view.tsx index 4117af7ff..88c2b1cae 100644 --- a/src/renderer/components/agent-list/status-view.tsx +++ b/src/renderer/components/agent-list/status-view.tsx @@ -46,8 +46,8 @@ import { import type { WorktreeChanges } from "../../services/agent-sessions/use-family"; import { markRead } from "../../services/agent-sessions/use-inbox"; import { useFirstPermission, useRowFacts } from "../../services/agent-sessions/use-row-facts"; -import { existingCodexController } from "../../services/chat/codex-controller"; -import { existingChatController } from "../../services/chat/controller"; +import { existingChatController } from "../../services/chat/claude/controller"; +import { existingCodexController } from "../../services/chat/codex/controller"; import { useUIStore } from "../../state/stores/ui"; import { ClaudeIcon, CodexIcon } from "../chat/agent-icons"; import { Button } from "../ui/button"; diff --git a/src/renderer/components/agent-list/task-view.tsx b/src/renderer/components/agent-list/task-view.tsx index 851c3f2bf..5c645a78d 100644 --- a/src/renderer/components/agent-list/task-view.tsx +++ b/src/renderer/components/agent-list/task-view.tsx @@ -315,7 +315,7 @@ function TaskHeader({ - copyTextViaIpc(path)}> + void copyTextViaIpc(path)}> {t("agentList.task.copy_path")} {!worktree.isRoot && ( diff --git a/src/renderer/components/agent-view/agent-pane.tsx b/src/renderer/components/agent-view/agent-pane.tsx index 87f591dc1..60957ab63 100644 --- a/src/renderer/components/agent-view/agent-pane.tsx +++ b/src/renderer/components/agent-view/agent-pane.tsx @@ -38,8 +38,8 @@ import { } from "@/state/stores/workspaces"; import { cn } from "@/utils/cn"; import { COMMANDS } from "../../../shared/keybindings/commands"; -import { createLogger } from "../../../shared/log/renderer"; -import { ipcCallResult } from "../../ipc/client"; +import { retryWorkspaceConnection } from "../../state/operations/workspace-selection"; +import type { DropZone } from "../../types/workbench-dnd"; import { StateGlyph, stateLabel } from "../agent-list/session-status"; import { AgentMark } from "../agent-list/status-view"; import { Button } from "../ui/button"; @@ -47,7 +47,6 @@ import { EmptyState } from "../ui/empty-state"; import { IconButton } from "../ui/icon-button"; import { AGENT_VIEW_SLOTS, slotRegistry } from "../workspace/content/slot-registry"; import { DropIndicator } from "../workspace/dnd/drop-indicator"; -import type { DropZone } from "../workspace/dnd/types"; import { zoneFromCoords } from "../workspace/dnd/use-drop-target"; import { OverflowTabsMenu } from "../workspace/tabs/overflow-tabs-menu"; import { @@ -63,8 +62,6 @@ import { EndedBar } from "./ended-bar"; import { RemoteSessionPane } from "./remote-pane"; import { DeviceChip, RemoteTabLabel } from "./remote-sessions"; -const log = createLogger("agent-view"); - function update(change: (layout: AgentLayout) => AgentLayout): void { useAgentViewStore.getState().update(change); } @@ -379,6 +376,7 @@ export const AgentPane = memo(function AgentPane({ function WorkspaceChip({ workspaceId, className }: { workspaceId: string; className?: string }) { const name = useWorkspacesStore((s) => s.workspaces.find((w) => w.id === workspaceId)?.name); const rootId = useWorkspacesStore((s) => rootWorkspaceId(s.workspaces, workspaceId)); + const color = useWorkspacesStore((s) => s.workspaces.find((w) => w.id === rootId)?.color); return ( {name} @@ -642,11 +640,7 @@ function OfflinePane({ workspaceId }: { workspaceId: string }) { workspace.location.kind === "ssh" ? `${workspace.location.user ? `${workspace.location.user}@` : ""}${workspace.location.host}` : workspace.location.distro; - const connect = () => { - void ipcCallResult("workspace", "activate", { id: workspaceId }).then((result) => { - if (!result.ok) log.warn(`activate failed: ${result.message}`); - }); - }; + const connect = () => retryWorkspaceConnection(workspaceId); return (
{root.name} @@ -693,7 +693,7 @@ function WorkspaceTag({ workspace, root }: { workspace: WorkspaceMeta; root: Wor {workspace.name} diff --git a/src/renderer/components/agent-view/drag.ts b/src/renderer/components/agent-view/drag.ts index b31ef1cc7..d49baeb24 100644 --- a/src/renderer/components/agent-view/drag.ts +++ b/src/renderer/components/agent-view/drag.ts @@ -4,7 +4,7 @@ * workspace's agents. The payload is the agent's session key. */ -import type { DropZone } from "../workspace/dnd/types"; +import type { DropZone } from "../../types/workbench-dnd"; export const MIME_AGENT = "application/x-nexus-agent"; diff --git a/src/renderer/components/agent-view/remote-chat-actions.ts b/src/renderer/components/agent-view/remote-chat-actions.ts index e92c20060..9cf715782 100644 --- a/src/renderer/components/agent-view/remote-chat-actions.ts +++ b/src/renderer/components/agent-view/remote-chat-actions.ts @@ -10,7 +10,7 @@ import i18next from "i18next"; import type { IpcResult } from "../../../shared/ipc/result"; import { ipcCallResult } from "../../ipc/client"; -import type { ChatController } from "../../services/chat/controller"; +import type { ChatController } from "../../services/chat/claude/controller"; import type { ImageBlock, UserItem } from "../../services/chat/model"; import { putPeerDraft } from "../../services/remote/peer-drafts"; import { type RemoteTarget, useRemoteViewStore } from "../../state/stores/remote-view"; diff --git a/src/renderer/components/agent-view/remote-pane.tsx b/src/renderer/components/agent-view/remote-pane.tsx index 48b310b08..e81a03991 100644 --- a/src/renderer/components/agent-view/remote-pane.tsx +++ b/src/renderer/components/agent-view/remote-pane.tsx @@ -11,15 +11,16 @@ import { } from "react"; import { useTranslation } from "react-i18next"; import type { ViewGoal } from "../../../../packages/remote-protocol/src/view"; +import { appErrorFailed } from "../../../shared/error/app-error"; import type { IpcResult } from "../../../shared/ipc/result"; import { useNow } from "../../hooks/use-now"; import { ipcCallResult } from "../../ipc/client"; import { remoteSessionKey } from "../../services/agent-view/layout"; import { ChatFilesContext } from "../../services/chat/chat-files"; -import { configCommandOf } from "../../services/chat/claude-config"; -import { CODEX_COMMANDS, codexStatusMarkdown } from "../../services/chat/codex-commands"; -import { emptyCodexModel } from "../../services/chat/codex-model"; -import type { PermissionAnswer } from "../../services/chat/controller"; +import { configCommandOf } from "../../services/chat/claude/config"; +import type { PermissionAnswer } from "../../services/chat/claude/controller"; +import { CODEX_COMMANDS, codexStatusMarkdown } from "../../services/chat/codex/commands"; +import { emptyCodexModel } from "../../services/chat/codex/model"; import type { GoalView } from "../../services/chat/goal"; import { isUnder } from "../../services/chat/links"; import { lookAreaOf } from "../../services/chat/look-area"; @@ -32,6 +33,7 @@ import type { import { agentTranscript } from "../../services/chat/panel"; import { registerPanelToggle } from "../../services/chat/panel-toggle"; import type { CommandCard } from "../../services/chat/store"; +import { surfaceError } from "../../services/error-surface/surface-error"; import { modelOfPage } from "../../services/remote/chat-model"; import { remoteGoalView } from "../../services/remote/goal"; import { usePeerChat, type WatchedChat } from "../../services/remote/peer-chat"; @@ -52,7 +54,7 @@ import { recoverPeerSubmission, usePeerSubmissions, } from "../../services/remote/peer-submissions"; -import { usePeerTerminal } from "../../services/remote/peer-terminal"; +import { usePeerTerminal, watchPeerTerminalTyping } from "../../services/remote/peer-terminal"; import { peerWorkspaceId, setPeerRoot } from "../../services/remote/peer-workspace"; import { chatProfileIdThere, @@ -67,7 +69,10 @@ import { usePeerTypes, } from "../../services/remote/room"; import { useSelfDeviceId } from "../../services/remote/self-device"; -import { TerminalTyping } from "../../services/remote/terminal-typing"; +import { + TerminalInputQueueFullError, + type TerminalTyping, +} from "../../services/remote/terminal-typing"; import { registerTabFocusTarget } from "../../services/tab-focus"; import { useChatFontStyle } from "../../state/stores/chat-font"; import { askOf, type RemoteTarget, useRemoteViewStore } from "../../state/stores/remote-view"; @@ -1351,28 +1356,15 @@ function RemoteTerminal({ return session ? askOf(session) : undefined; })() : target.ask; - const typing = useMemo( - () => - new TerminalTyping( - async (input) => { - const result = await ipcCallResult("remote", "peerTerminalInput", { - deviceId, - workspaceId, - tabId, - ...input, - }); - if (!result.ok) throw new Error(result.message); - }, - (error) => - showToast({ - kind: "error", - message: t("agentView.remote.failed", { - message: error instanceof Error ? error.message : String(error), - }), - }), - ), - [deviceId, workspaceId, tabId, t], - ); + const [typing, setTyping] = useState(null); + useLayoutEffect(() => { + setTyping(null); + return watchPeerTerminalTyping({ deviceId, workspaceId, tabId }, setTyping, (error) => { + const code = + error instanceof TerminalInputQueueFullError ? error.code : "terminal-input-failed"; + surfaceError(appErrorFailed(String(error), { code }), { surface: "toast" }); + }); + }, [deviceId, workspaceId, tabId]); if (read.status !== "ready") { return ; } @@ -1381,7 +1373,7 @@ function RemoteTerminal({ {reach === "online" && controls && ask && ask.requestId !== answered && ( { - void controller.answerPermission(request, answer).catch(() => {}); + void controller.answerPermission(request, answer).catch(surfaceChatSendError); }; const [rewinding, setRewinding] = useState(null); @@ -222,19 +225,25 @@ export function ClaudeTabView({ workspaceId, tabId, props, isVisible }: ClaudeTa const running = state.model.running; const canFork = controller.forkPoint("", { before: false }) !== null; const asking = state.model.permissions; + const unverifiedStart = controller.unverifiedHistoryStart(); // Set below, where the agents are known; read when a row renders. const taskShower = useRef>(() => undefined); const actions = useMemo( () => ({ running, canFork, + canForkItem: (item) => + !!item.uuid && + (item.kind !== "user" || (item.state === "taken" && item.uuid !== unverifiedStart)), + canRewindItem: (item) => + item.state === "taken" && !!item.uuid && item.uuid !== unverifiedStart, onRewind: (item) => setRewinding(item), onFork: (item) => forkFrom(controller, workspaceId, props.cwd, item), onBackground: (toolUseId) => backgroundCall(controller, toolUseId), waiting: new Set(asking.flatMap((p) => (p.toolUseId ? [p.toolUseId] : []))), taskShower: (item) => taskShower.current(item), }), - [running, canFork, controller, workspaceId, props.cwd, asking], + [running, canFork, controller, workspaceId, props.cwd, asking, unverifiedStart], ); // Where the side panel goes follows the tab's width. @@ -302,7 +311,11 @@ export function ClaudeTabView({ workspaceId, tabId, props, isVisible }: ClaudeTa const printRuns = usePrintRuns(workspaceId, tabId, isVisible); const runRows = useMemo(() => printRunRows(printRuns), [printRuns]); // Its history's subagents too, which ended: a conversation resumed shows them. - const pastModel = state.history; + const pastHistory = state.history; + const pastModel = useMemo( + () => (pastHistory ? historyForDisplay(pastHistory) : null), + [pastHistory], + ); const { tasks: agentTasks, tools: agentTools, agentItems, items: agentCalls } = state.model; const pastAgents = useMemo(() => (pastModel ? pastAgentsOf(pastModel) : []), [pastModel]); const liveAgents = useMemo( @@ -385,10 +398,14 @@ export function ClaudeTabView({ workspaceId, tabId, props, isVisible }: ClaudeTa }, [isVisible]); // The previews may read what the tab links outside the workspace, while it is open. const [looked, setLooked] = useState([]); - // Taken from the parts it reads, so a line that changes none of them (a - // usage report, a turn's end) does not walk the whole session again. const history = state.history; const { items: liveItems, tools: liveTools, tasks } = state.model; + const toolStatuses = useMemo( + () => jobToolStatuses({ tasks, tools: liveTools, running }), + [tasks, liveTools, running], + ); + // Taken from the parts it reads, so a line that changes none of them (a + // usage report, a turn's end) does not walk the whole session again. const outside = useMemo(() => { const items = history ? [...history.items, ...liveItems] : liveItems; const tools = history ? { ...history.tools, ...liveTools } : liveTools; @@ -667,13 +684,27 @@ export function ClaudeTabView({ workspaceId, tabId, props, isVisible }: ClaudeTa tabId={tabId} cwd={props.cwd} rootPath={rootPath} - history={state.history} + history={pastModel} model={state.model} + toolStatuses={toolStatuses} actions={shown ? undefined : actions} items={shownTranscript} reveal={shown ? null : callAsk} moves={props.moves} relayed={props.relayed} + older={ + !shown && state.historyPage + ? { + ...state.historyPage, + load: () => + void controller + .loadEarlier() + .catch((error: Error) => + showToast({ kind: "error", message: error.message }), + ), + } + : undefined + } empty={ state.phase === "ready" || state.phase === "ended" ? (
diff --git a/src/renderer/components/chat/codex-config-dialog.tsx b/src/renderer/components/chat/codex-config-dialog.tsx index d636c3c50..aa6d62af0 100644 --- a/src/renderer/components/chat/codex-config-dialog.tsx +++ b/src/renderer/components/chat/codex-config-dialog.tsx @@ -5,7 +5,7 @@ import { CODEX_CONFIG_PROFILE_KEY, type CodexConfigSet, codexNumericConfigValue, -} from "../../services/chat/codex-config"; +} from "../../services/chat/codex/config"; import { SettingsGroup, SettingsRow } from "../settings/section"; import { Button } from "../ui/button"; import { Dialog } from "../ui/dialog"; diff --git a/src/renderer/components/chat/codex-tab.tsx b/src/renderer/components/chat/codex-tab.tsx index 61e431b13..694187982 100644 --- a/src/renderer/components/chat/codex-tab.tsx +++ b/src/renderer/components/chat/codex-tab.tsx @@ -13,15 +13,20 @@ import { isExternalSchemeAllowed } from "../../../shared/security/url-scheme"; import { codexTabHooks } from "../../services/chat/agent-tab"; import { parseAppCommand, revealOf } from "../../services/chat/app-commands"; import { useStateBesideStream } from "../../services/chat/beside-stream"; -import { codexCommandsListed, parseCodexCommand } from "../../services/chat/codex-commands"; +import type { PermissionAnswer } from "../../services/chat/claude/controller"; +import { codexCommandsListed, parseCodexCommand } from "../../services/chat/codex/commands"; import { type CodexController, codexController, MIN_CODEX_VERSION, -} from "../../services/chat/codex-controller"; -import { codexHistoryAvailable, codexHistoryBusy } from "../../services/chat/codex-history"; -import { type CodexModel, originalsFor } from "../../services/chat/codex-model"; -import type { PermissionAnswer } from "../../services/chat/controller"; +} from "../../services/chat/codex/controller"; +import { + codexHistoryAvailable, + codexHistoryBusy, + codexServerVersion, +} from "../../services/chat/codex/history"; +import { type CodexModel, originalsFor } from "../../services/chat/codex/model"; +import { runCodexCommand } from "../../services/chat/codex/run-command"; import { filesThere } from "../../services/chat/file-presence"; import { goalViewOf } from "../../services/chat/goal"; import { imageLook, lookAtImage, type ShownImage } from "../../services/chat/image-look"; @@ -47,7 +52,6 @@ import { } from "../../services/chat/print-runs"; import { withRelayed } from "../../services/chat/relayed"; import { revertChangedFile } from "../../services/chat/revert-file"; -import { runCodexCommand } from "../../services/chat/run-codex-command"; import { type ChatTabState, chatKey, @@ -96,6 +100,7 @@ import { focusChatTab, PermissionCard } from "./permission-card"; import { PrintRunPane } from "./print-run-pane"; import { QuickLook, type QuickLookList } from "./quick-look"; import { RewindDialog } from "./rewind-dialog"; +import { surfaceChatSendError } from "./send-error"; import { CodexSessionList } from "./session-list"; import { StatusLine } from "./status-line"; import { SummaryLine } from "./summary-line"; @@ -155,7 +160,7 @@ export function CodexTabView({ workspaceId, tabId, props, isVisible }: CodexTabV }, [isVisible, controller]); const onAnswer = (request: PermissionRequest, answer: PermissionAnswer) => { - void controller.answerPermission(request, answer).catch(() => {}); + void controller.answerPermission(request, answer).catch(surfaceChatSendError); }; // The pane shows the conversation or one of the runs its commands @@ -551,7 +556,7 @@ export function CodexTabView({ workspaceId, tabId, props, isVisible }: CodexTabV ); const [rewinding, setRewinding] = useState(null); - const historyAvailable = codexHistoryAvailable(model.appServerVersion ?? state.version); + const historyAvailable = codexHistoryAvailable(codexServerVersion(model, state.version)); const forkFrom = (item: UserItem | AssistantItem) => { try { const fork = controller.forkPoint(item); diff --git a/src/renderer/components/chat/composer.tsx b/src/renderer/components/chat/composer.tsx index 391f61835..638da2764 100644 --- a/src/renderer/components/chat/composer.tsx +++ b/src/renderer/components/chat/composer.tsx @@ -30,6 +30,7 @@ import type { RelayedMessage } from "../../../shared/chat/relay"; import { CHAT_MODES, type ModeSet } from "../../../shared/sessions/chat-modes"; import { useFitStep } from "../../hooks/use-fit-step"; import { appCommandsBeside, listedCommands } from "../../services/chat/app-commands"; +import { firstVersionOf } from "../../services/chat/claude/versions"; import { applyCompletion, completionAt, @@ -46,13 +47,8 @@ import { type UserItem, } from "../../services/chat/model"; import { withRelayed } from "../../services/chat/relayed"; -import { - type ChatTabState, - type QueuedMessage, - type RevealPart, - takeReveal, -} from "../../services/chat/store"; -import { firstVersionOf } from "../../services/chat/versions"; +import { type ChatTabState, type RevealPart, takeReveal } from "../../services/chat/store"; +import type { QueuedMessage } from "../../services/chat/transport/queue"; import { useChatFontStyle } from "../../state/stores/chat-font"; import { cn } from "../../utils/cn"; import { formatBytes } from "../../utils/format-bytes"; @@ -74,6 +70,7 @@ import { type ChatAgent, ChatAgentContext, useAgentContext } from "./chat-agent" import { type CompletionItem, CompletionMenu } from "./completion-menu"; import { maxTextHeight } from "./composer-height"; import { DictationOverlay, MicButton, useComposerDictation } from "./dictation"; +import { surfaceChatSendError } from "./send-error"; /** What the composer drives: a Claude tab's controller, or a Codex tab's. */ export interface ComposerController { @@ -485,25 +482,32 @@ export const Composer = memo(function Composer({ if (controller.submitPeer(as, body, images, files)) clear(); return; } - let message: string; - let uploads: string[]; setSending(true); try { - ({ message, uploads } = await withAttachments(body)); - } catch (err) { - showError(err as Error); - return; + const { message, uploads } = await withAttachments(body); + clear(); + try { + if (uploads.length > 0 && controller.sendUploaded) { + await controller.sendUploaded(as, message, images, uploads); + } else if (as === "queue") { + controller.enqueue(message, images); + } else if (as === "now" && controller.sendNow) { + await controller.sendNow(message, images); + } else { + await controller.send(message, images); + } + } catch (error) { + // Restore the original text and attachments for another attempt. + if (body) setText((prev) => (prev ? `${body}\n${prev}` : body)); + if (images.length) setImages((prev) => [...images, ...prev]); + if (files.length) setFiles((prev) => [...files, ...prev]); + throw error; + } + } catch (error) { + surfaceChatSendError(error); } finally { setSending(false); } - const sendUploaded = controller.sendUploaded?.bind(controller); - if (uploads.length > 0 && sendUploaded) { - void sendUploaded(as, message, images, uploads).catch((err: Error) => showError(err)); - } else if (how === "queue" && running) controller.enqueue(message, images); - else if (how === "now" && running && controller.sendNow) { - void controller.sendNow(message, images).catch((err: Error) => showError(err)); - } else void controller.send(message, images).catch((err: Error) => showError(err)); - clear(); }; // The submit of the latest render, for a send that waits on the dictation's end. const submitRef = useRef(submit); @@ -733,7 +737,15 @@ export const Composer = memo(function Composer({ {state.queue.map((item) => ( pullBack(item.id) : undefined} onRemove={ @@ -1113,13 +1125,7 @@ function QueueRow({ export const SEND_BUTTON_CLASS = "flex shrink-0 items-center gap-1 rounded-(--radius-control) bg-primary py-1.5 text-primary-foreground hover:opacity-90 disabled:pointer-events-none disabled:opacity-40"; -/** - * The box's main button, with the key that presses it. During a turn a - * Claude tab's message waits for the call that runs, so its label says it - * queues; a Codex tab's goes into the turn at once. It is drawn the same - * either way: the chat shows a queued message as queued. Its tooltip tells - * the other keys. - */ +/** The box's main button and the keys for the available send timings. */ function SendButton({ running, waits, @@ -1129,7 +1135,7 @@ function SendButton({ keycap, }: { running: boolean; - /** During a turn, the message waits for the current call (Claude's ↩). */ + /** During a turn, the message waits for a tool completion (↩). */ waits: boolean; program: ChatAgent; onClick: () => void; diff --git a/src/renderer/components/chat/config-dialog.tsx b/src/renderer/components/chat/config-dialog.tsx index 79bcff1ca..c5e4fd41a 100644 --- a/src/renderer/components/chat/config-dialog.tsx +++ b/src/renderer/components/chat/config-dialog.tsx @@ -26,8 +26,8 @@ import { defaultEffortOf, effortModelOf, modelChoiceOf, -} from "../../services/chat/claude-config"; -import type { ChatController } from "../../services/chat/controller"; +} from "../../services/chat/claude/config"; +import type { ChatController } from "../../services/chat/claude/controller"; import type { ModelInfo } from "../../services/chat/model"; import type { ChatTabState } from "../../services/chat/store"; import { cn } from "../../utils/cn"; diff --git a/src/renderer/components/chat/goal-bar.tsx b/src/renderer/components/chat/goal-bar.tsx index ae391ecaf..afcc1dcff 100644 --- a/src/renderer/components/chat/goal-bar.tsx +++ b/src/renderer/components/chat/goal-bar.tsx @@ -23,7 +23,7 @@ import { useEffect, useMemo, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { useShallow } from "zustand/shallow"; import { useNow } from "../../hooks/use-now"; -import type { CodexModel } from "../../services/chat/codex-model"; +import type { CodexModel } from "../../services/chat/codex/model"; import { clockLabel, firstLineOf, diff --git a/src/renderer/components/chat/message-list.tsx b/src/renderer/components/chat/message-list.tsx index a28b070f5..ae8b71baf 100644 --- a/src/renderer/components/chat/message-list.tsx +++ b/src/renderer/components/chat/message-list.tsx @@ -72,6 +72,7 @@ import { } from "../../services/chat/panel"; import { followsMessage } from "../../services/chat/profile-moves"; import { RELAY_KINDS, withRelayed } from "../../services/chat/relayed"; +import type { ToolRowStatuses } from "../../services/chat/running"; import { isOwnScroll, nearBottom, scrolledDown } from "../../services/chat/stick-to-bottom"; import { foldToolRuns, tallyRun } from "../../services/chat/tool-runs"; import { showPeerSession } from "../../services/remote/show-peer"; @@ -98,6 +99,8 @@ import { AnsweredQuestions, PlanOutcome, showsAsPrompt } from "./prompt-rows"; import { SentFiles } from "./sent-files"; import { ToolRow } from "./tool-row"; +const NO_TOOL_STATUSES: ToolRowStatuses = {}; + interface MessageListProps { workspaceId: string; /** The tab the conversation is, if it is one. */ @@ -106,6 +109,7 @@ interface MessageListProps { rootPath: string; history: ChatModel | null; model: ChatModel; + toolStatuses?: ToolRowStatuses; /** Shown when there is no message yet. */ empty?: React.ReactNode; actions?: MessageActions; @@ -126,7 +130,9 @@ interface MessageListProps { } export interface OlderMessages { - count: number; + count?: number; + /** A local transcript cursor has an unknown number of preceding messages. */ + hasEarlier?: boolean; loading: boolean; load(): void; } @@ -217,6 +223,7 @@ export function MessageList({ rootPath, history, model, + toolStatuses = NO_TOOL_STATUSES, empty, actions, items, @@ -239,7 +246,13 @@ export function MessageList({ // Earlier messages asked for beyond the last WINDOW_ITEMS, and where the // view was when they were, to stay on what it showed. const [more, setMore] = useState(0); - const anchor = useRef<{ height: number; top: number } | null>(null); + const anchor = useRef<{ + height: number; + top: number; + row?: HTMLElement; + rowTop?: number; + historyCount?: number; + } | null>(null); const toBottom = useCallback(() => { const el = scrollRef.current; @@ -315,7 +328,8 @@ export function MessageList({ [items, model.items, relayed], ); const streaming = items ? null : model.streaming; - const nothing = historyItems.length === 0 && liveItems.length === 0 && !streaming; + const nothing = + historyItems.length === 0 && liveItems.length === 0 && !streaming && !older?.hasEarlier; emptyShown.current = nothing && empty !== undefined; const tools = useMemo( () => (history ? { ...history.tools, ...model.tools } : model.tools), @@ -419,25 +433,60 @@ export function MessageList({ const unread = older?.count ?? 0; const showEarlier = useCallback(() => { const el = scrollRef.current; - if (el) anchor.current = { height: el.scrollHeight, top: el.scrollTop }; + if (el) { + const top = el.getBoundingClientRect().top; + const row = Array.from( + contentRef.current?.querySelectorAll("[data-chat-row]") ?? [], + ) + .map((wrapper) => wrapper.firstElementChild) + .find( + (node): node is HTMLElement => + node instanceof HTMLElement && node.getBoundingClientRect().bottom > top, + ); + anchor.current = { + height: el.scrollHeight, + top: el.scrollTop, + row, + rowTop: row?.getBoundingClientRect().top, + historyCount: start === 0 && older?.hasEarlier ? historyItems.length : undefined, + }; + } stuck.current = false; if (start > 0) setMore((m) => m + WINDOW_ITEMS); else older?.load(); - }, [start, older]); + }, [start, older, historyItems.length]); // Messages read in before the first ones render at once. const wasUnread = useRef(unread); useLayoutEffect(() => { - const read = wasUnread.current - unread; + const count = wasUnread.current - unread; wasUnread.current = unread; - if (read > 0) setMore((m) => m + read); + if (count > 0) setMore((m) => m + count); }, [unread]); - // The messages put above keep the view on what it showed. + const historyRead = useRef(false); + useLayoutEffect(() => { + const before = anchor.current?.historyCount; + if (before === undefined || older?.loading) return; + const added = historyItems.length - before; + // A page that completed a tool or only carried part of a large row can add no items. + if (added > 0) { + historyRead.current = true; + setMore((m) => m + added); + } else { + anchor.current = null; + } + }, [historyItems.length, older?.loading]); + // Keep the same row at the same height, even when live items also arrived below it. useLayoutEffect(() => { const el = scrollRef.current; const was = anchor.current; - if (!el || !was || more === 0) return; + if (!el || !was || more === 0 || (was.historyCount !== undefined && !historyRead.current)) + return; anchor.current = null; - el.scrollTop = was.top + (el.scrollHeight - was.height); + historyRead.current = false; + el.scrollTop = + was.row?.isConnected && was.rowTop !== undefined + ? el.scrollTop + was.row.getBoundingClientRect().top - was.rowTop + : was.top + (el.scrollHeight - was.height); ownTop.current = el.scrollTop; lastTop.current = el.scrollTop; }, [more]); @@ -451,14 +500,22 @@ export function MessageList({ ); const under = useMemo(() => callsUnder(tools), [tools]); const views = useMemo(() => { - const sources = { tools, callsUnder: under, shown, extras, earlier, asked: shownCall }; + const sources = { + tools, + toolStatuses, + callsUnder: under, + shown, + extras, + earlier, + asked: shownCall, + }; const out = new Map(); for (const item of windowItems) { const view = itemView(item, sources); if (view) out.set(item, view); } return out; - }, [windowItems, tools, under, shown, extras, earlier, shownCall]); + }, [windowItems, tools, toolStatuses, under, shown, extras, earlier, shownCall]); const list = (
@@ -477,19 +534,23 @@ export function MessageList({ )} > {nothing && empty} - {start + unread > 0 && ( + {(start + unread > 0 || older?.hasEarlier) && ( )} {historyItems.slice(Math.min(start, historyItems.length)).map((item) => ( - +
+ +
{movesAfter.get(item)?.map((move) => ( ))} @@ -497,7 +558,9 @@ export function MessageList({ ))} {liveItems.slice(Math.max(0, start - historyItems.length)).map((item) => ( - +
+ +
{movesAfter.get(item)?.map((move) => ( ))} @@ -508,6 +571,7 @@ export function MessageList({ streaming={streaming} ctx={ctx} tools={tools} + toolStatuses={toolStatuses} shownEarlier={earlier.inLastTurn} /> )} @@ -564,7 +628,14 @@ const Item = memo(function Item({ item, ctx, view }: ItemProps) { ); case "notice": @@ -1014,6 +1085,7 @@ function AssistantMessage({ toolUseId={block.toolUseId} ctx={ctx} tools={view.tools} + toolStatuses={view.toolStatuses} shownEarlier={view.earlier.get(block.toolUseId)} /> ), @@ -1050,10 +1122,6 @@ function runKey(blocks: AssistantBlock[]): string { return first?.type === "tool" ? first.toolUseId : ""; } -function isLive(tool: ToolCall | undefined): boolean { - return !tool || tool.status === "running" || tool.status === "streaming"; -} - /** The kinds of call a run's line counts, in its order. */ const RUN_KINDS = ["commands", "reads", "edits", "searches", "others"] as const; @@ -1073,7 +1141,7 @@ function ToolRun({ }) { const { t } = useTranslation(); const [open, setOpen] = useState(false); - const { tools } = view; + const { tools, toolStatuses } = view; // Opens when the side panel asks to see a call of the run's. const asked = view.asked; // biome-ignore lint/correctness/useExhaustiveDependencies: each ask once; a call joining the run later does not open it again @@ -1083,14 +1151,17 @@ function ToolRun({ } }, [asked]); const calls = blocks.flatMap((block) => (block.type === "tool" ? [tools[block.toolUseId]] : [])); - const running = calls.some(isLive); - const tally = tallyRun(calls); + const live = blocks.filter((block) => { + if (block.type !== "tool") return false; + const status = toolStatuses[block.toolUseId] ?? tools[block.toolUseId]?.status; + return !status || status === "running" || status === "streaming" || status === "background"; + }); + const running = live.length > 0; + const tally = tallyRun(calls, toolStatuses); const label = RUN_KINDS.filter((kind) => tally[kind] > 0) .map((kind) => t(`chat.tool.run.${kind}`, { count: tally[kind] })) .join(" · "); - const rows = open - ? blocks - : blocks.filter((block) => block.type === "tool" && isLive(tools[block.toolUseId])); + const rows = open ? blocks : live; return (
@@ -1141,6 +1212,7 @@ function ToolRun({ toolUseId={block.toolUseId} tool={tools[block.toolUseId]} tools={tools} + toolStatuses={toolStatuses} cwd={ctx.cwd} onBackground={backgroundOf(ctx.actions, block.toolUseId)} /> @@ -1160,12 +1232,14 @@ function CallRow({ toolUseId, ctx, tools, + toolStatuses, shownEarlier, }: { toolUseId: string; ctx: ItemContext; /** Its call and the calls it runs, by id; more is fine. */ tools: Readonly>; + toolStatuses: ToolRowStatuses; /** The image files it sends that its turn showed before. */ shownEarlier?: ReadonlySet; }) { @@ -1204,6 +1278,7 @@ function CallRow({ toolUseId={toolUseId} tool={tool} tools={tools} + toolStatuses={toolStatuses} cwd={ctx.cwd} onBackground={backgroundOf(ctx.actions, toolUseId)} /> @@ -1252,11 +1327,13 @@ function Streaming({ streaming, ctx, tools, + toolStatuses, shownEarlier, }: { streaming: StreamingMessage; ctx: ItemContext; tools: Record; + toolStatuses: ToolRowStatuses; /** What the last turn shows in full so far, which it shows as chips. */ shownEarlier: ReadonlySet; }) { @@ -1279,7 +1356,13 @@ function Streaming({ ) : block.type === "thinking" ? ( ) : block.toolUseId ? ( - + ) : null, )}
diff --git a/src/renderer/components/chat/permission-card.tsx b/src/renderer/components/chat/permission-card.tsx index 0efb1c6bb..e7f9959d6 100644 --- a/src/renderer/components/chat/permission-card.tsx +++ b/src/renderer/components/chat/permission-card.tsx @@ -29,7 +29,7 @@ import { useState, } from "react"; import { useTranslation } from "react-i18next"; -import type { PermissionAnswer } from "../../services/chat/controller"; +import type { PermissionAnswer } from "../../services/chat/claude/controller"; import type { PermissionRequest } from "../../services/chat/model"; import { type Scope, scopesOf } from "../../services/chat/permission-scopes"; import { cn } from "../../utils/cn"; @@ -505,17 +505,24 @@ export { scopesOf }; // --------------------------------------------------------------------------- export interface Question { + id?: string; question: string; header?: string; multiSelect?: boolean; options: { label: string; description?: string }[]; } +/** Codex names each question; Claude's questions are keyed by their text. */ +export function questionKey(question: Question): string { + return question.id ?? question.question; +} + export function readQuestions(input: Record): Question[] { if (!Array.isArray(input.questions)) return []; return (input.questions as Record[]) .filter((q) => typeof q?.question === "string") .map((q) => ({ + ...(typeof q.id === "string" ? { id: q.id } : {}), question: q.question as string, header: typeof q.header === "string" ? q.header : undefined, multiSelect: q.multiSelect === true, @@ -567,8 +574,8 @@ function QuestionForm({ request, onAnswer }: FormProps) { ); const answerOf = (q: Question): string => { - const labels = [...(picked[q.question] ?? [])]; - const typed = other[q.question]?.trim(); + const labels = [...(picked[questionKey(q)] ?? [])]; + const typed = other[questionKey(q)]?.trim(); if (typed) labels.push(typed); return labels.join(", "); }; @@ -588,8 +595,9 @@ function QuestionForm({ request, onAnswer }: FormProps) { }; const submit = () => { if (!complete) return; - const answers: Record = {}; - for (const each of questions) answers[each.question] = answerOf(each); + const answers = Object.fromEntries( + questions.map((each) => [questionKey(each), answerOf(each)]), + ); onAnswer(request, { kind: "allow", updatedInput: { ...request.input, answers } }); }; const onward = () => { @@ -608,18 +616,18 @@ function QuestionForm({ request, onAnswer }: FormProps) { }; const toggle = (question: Question, label: string) => { setPicked((prev) => { - const now = prev[question.question] ?? []; + const now = prev[questionKey(question)] ?? []; if (question.multiSelect) { return { ...prev, - [question.question]: now.includes(label) + [questionKey(question)]: now.includes(label) ? now.filter((l) => l !== label) : [...now, label], }; } - return { ...prev, [question.question]: [label] }; + return { ...prev, [questionKey(question)]: [label] }; }); - if (!question.multiSelect) setOther((prev) => ({ ...prev, [question.question]: "" })); + if (!question.multiSelect) setOther((prev) => ({ ...prev, [questionKey(question)]: "" })); }; if (!q) return null; @@ -666,7 +674,7 @@ function QuestionForm({ request, onAnswer }: FormProps) { - {row.background && ( - - {t("chat.panel.running.background")} - - )} + {row.background && } {foreground && onBackground && ( { - showToast({ kind: "error", message: `${failed}: ${err.message}` }); + log.warn(`Background task stop failed: ${err.message}`); + showToast({ kind: "error", message: failed }); }); } @@ -1100,18 +1104,23 @@ function McpSection({ controller, state }: { controller: ChatController; state: key={server.name} title={signingIn ? t("chat.panel.mcp.signing_in_hint") : server.name} > - {server.name} {signingIn ? ( - - {t("chat.panel.mcp.signing_in")} - controller.reopenMcpSignIn()}> - {t("chat.panel.mcp.reopen")} - - controller.cancelMcpSignIn()}> - {t("chat.panel.mcp.cancel")} - - - ) : server.status === "needs-auth" ? ( +
+ {server.name} +
+ {t("chat.panel.mcp.signing_in")} + controller.reopenMcpSignIn()}> + {t("chat.panel.mcp.reopen")} + + controller.cancelMcpSignIn()}> + {t("chat.panel.mcp.cancel")} + +
+
+ ) : ( + {server.name} + )} + {signingIn ? null : server.status === "needs-auth" ? ( controller.mcpAuthenticate(server.name).catch(fail)}> {t("chat.panel.mcp.sign_in")} diff --git a/src/renderer/components/chat/status-line.tsx b/src/renderer/components/chat/status-line.tsx index 88db42e88..c94b332b1 100644 --- a/src/renderer/components/chat/status-line.tsx +++ b/src/renderer/components/chat/status-line.tsx @@ -1,4 +1,4 @@ -// The running turn in one line: what Claude is doing, for how long, and how +// The running turn in one line: what the agent is doing, for how long, and how // much it has written. Shown only while a turn runs. import { LoaderCircle } from "lucide-react"; @@ -14,12 +14,18 @@ import { GROUP_STATE, GROUP_TINT } from "../agent-list/status-view"; export function StatusLine({ model }: { model: ChatModel }) { const { t } = useTranslation(); const turn = model.turn; - const now = useNow(model.running && turn?.startedAt !== undefined); + const now = useNow( + model.running && (turn?.startedAt !== undefined || turn?.lastActivityAt !== undefined), + ); if (!model.running || !turn) return null; - const activity = activityOf(model, t); + const quiet = generatingElapsed(model, now); + const activity = + quiet === undefined + ? activityOf(model, t) + : t("chat.status.generating", { elapsed: elapsedLabel(quiet) }); const elapsed = - turn.startedAt !== undefined ? Math.max(0, Math.floor((now - turn.startedAt) / 1000)) : null; + quiet === undefined && turn.startedAt !== undefined ? now - turn.startedAt : undefined; const tokens = turn.outputTokens + turn.thinkingTokens; const agents = agentsOf(model).filter((a) => a.status === "running").length; const waiting = model.permissions.length > 0 && !model.interrupting; @@ -41,7 +47,7 @@ export function StatusLine({ model }: { model: ChatModel }) { )} {activity} - {elapsed !== null && {elapsedLabel(elapsed * 1_000)}} + {elapsed !== undefined && {elapsedLabel(elapsed)}} {tokens > 0 && {t("chat.status.tokens", { count: formatTokens(tokens) })}} {agents > 0 && {t("chat.status.agents", { count: agents })}}
@@ -50,6 +56,26 @@ export function StatusLine({ model }: { model: ChatModel }) { type Translate = ReturnType["t"]; +/** Time since the latest signal when model generation is the only visible activity. */ +export function generatingElapsed(model: ChatModel, now: number): number | undefined { + const turn = model.turn; + if (!model.running || turn?.lastActivityAt === undefined) return undefined; + const elapsed = now - turn.lastActivityAt; + if ( + elapsed < 3_000 || + model.interrupting || + model.permissions.length > 0 || + turn.retry || + turn.status === "compacting" || + Object.values(model.tools).some( + (tool) => + tool.parentToolUseId === null && (tool.status === "running" || tool.status === "streaming"), + ) + ) + return undefined; + return elapsed; +} + function activityOf(model: ChatModel, t: Translate): string { const turn = model.turn; if (model.interrupting) return t("chat.status.interrupting"); diff --git a/src/renderer/components/chat/tool-row.tsx b/src/renderer/components/chat/tool-row.tsx index 00c33e30a..9db02f1b3 100644 --- a/src/renderer/components/chat/tool-row.tsx +++ b/src/renderer/components/chat/tool-row.tsx @@ -16,7 +16,11 @@ import { useTranslation } from "react-i18next"; import { imageFileOf } from "../../services/chat/image-look"; import { slashWindowsPath } from "../../services/chat/links"; import type { ToolCall } from "../../services/chat/model"; -import { canBackground } from "../../services/chat/running"; +import { + canBackground, + type ToolRowStatus, + type ToolRowStatuses, +} from "../../services/chat/running"; import { cn } from "../../utils/cn"; import { ChatImage, ToolThumbs } from "./chat-image"; import { Highlighted, highlightCode, languageOf } from "./code-highlight"; @@ -26,6 +30,7 @@ interface ToolRowProps { toolUseId: string; /** Every tool call the tab knows, for an Agent call's own calls. */ tools: Record; + toolStatuses: ToolRowStatuses; cwd: string; /** Sends a running command or subagent to the background, as Ctrl+B does in the terminal. */ onBackground?: (toolUseId: string) => void; @@ -51,21 +56,35 @@ export function toolTint(name: string): string { */ export const BackgroundIcon = Layers2; +/** The label for a job running beside the turn, in the conversation and the panel. */ +export function BackgroundBadge({ testId }: { testId?: string }) { + const { t } = useTranslation(); + return ( + + {t("chat.panel.running.background")} + + ); +} + /** Longest output an open row shows; the rest is cut. */ const MAX_OUTPUT_CHARS = 20_000; -export function ToolRow({ tool, toolUseId, tools, cwd, onBackground }: ToolRowProps) { +export function ToolRow({ tool, toolUseId, tools, toolStatuses, cwd, onBackground }: ToolRowProps) { const { t } = useTranslation(); const [open, setOpen] = useState(false); const name = tool?.displayName || tool?.name || "tool"; const summary = tool ? toolSummary(tool, cwd) : ""; - const status = tool?.status ?? "running"; + const jobStatus = toolStatuses[toolUseId]; + const status = jobStatus ?? tool?.status ?? "running"; const unsandboxed = tool ? isUnsandboxed(tool.input) : false; const children = tool && AGENT_TOOLS.has(tool.name) ? Object.values(tools).filter((t) => t.parentToolUseId === toolUseId) : []; - const backgroundable = onBackground && tool && canBackground(tool); + const backgroundable = onBackground && tool && jobStatus === undefined && canBackground(tool); return (
@@ -88,6 +107,7 @@ export function ToolRow({ tool, toolUseId, tools, cwd, onBackground }: ToolRowPr {t("chat.tool.unsandboxed")} )} + {status === "background" && } {status === "denied" && ( {t("chat.tool.denied")} )} @@ -122,7 +142,14 @@ export function ToolRow({ tool, toolUseId, tools, cwd, onBackground }: ToolRowPr {open && children.length > 0 && (
{children.map((child) => ( - + ))}
)} @@ -143,11 +170,12 @@ export function isUnsandboxed(input: unknown): boolean { ); } -function StatusIcon({ status }: { status: ToolCall["status"] }) { +function StatusIcon({ status }: { status: ToolRowStatus }) { const cls = "size-3.5 shrink-0"; switch (status) { case "streaming": case "running": + case "background": return ( ); diff --git a/src/renderer/components/chat/usage-line.tsx b/src/renderer/components/chat/usage-line.tsx index e1874692e..b63e8a285 100644 --- a/src/renderer/components/chat/usage-line.tsx +++ b/src/renderer/components/chat/usage-line.tsx @@ -12,7 +12,7 @@ import { useTranslation } from "react-i18next"; import { cn } from "@/utils/cn"; import { useFitStep } from "../../hooks/use-fit-step"; import { useNow } from "../../hooks/use-now"; -import type { CodexModel } from "../../services/chat/codex-model"; +import type { CodexModel } from "../../services/chat/codex/model"; import type { ChatModel } from "../../services/chat/model"; import { usePlanClaudeUsage, usePlanCodexLimits } from "../../services/chat/plan-usage"; import { diff --git a/src/renderer/components/editor/browser/pick-button.tsx b/src/renderer/components/editor/browser/pick-button.tsx index 0293658c0..95d0a8e57 100644 --- a/src/renderer/components/editor/browser/pick-button.tsx +++ b/src/renderer/components/editor/browser/pick-button.tsx @@ -24,16 +24,19 @@ import { } from "@/components/ui/dropdown-menu"; import { IconButton } from "@/components/ui/icon-button"; import { showToast } from "@/components/ui/toast"; +import { normaliseError } from "@/hooks/use-ipc-action"; import { ipcCallResult } from "@/ipc/client"; import { showTerminal } from "@/services/agent-sessions/actions"; import { type AgentSession, sessionKey } from "@/services/agent-sessions/model"; import { pasteForTerminal, pickTargets, targetLabels } from "@/services/browser/element-pick"; +import { surfaceError } from "@/services/error-surface/surface-error"; import { getTerminalController } from "@/services/terminal/terminal-registry"; import { useClaudeStatusStore } from "@/state/stores/claude-status"; import { useTabsStore } from "@/state/stores/tabs"; import { useWorkspacesStore } from "@/state/stores/workspaces"; import { copyTextViaIpc } from "@/utils/clipboard"; import { cn } from "@/utils/cn"; +import { ipcErrorToAppError } from "../../../../shared/ipc/result"; interface PickButtonProps { tabId: string; @@ -101,13 +104,12 @@ export function PickButton({ tabId, workspaceId }: PickButtonProps) { [tabId], ); - const copyInstead = (text: string, message: string) => { - copyTextViaIpc(text); - showToast({ kind: "info", message }); + const copyInstead = async (text: string, message: string) => { + if (await copyTextViaIpc(text)) showToast({ kind: "info", message }); }; const deliver = (session: AgentSession, text: string) => { - if (!pasteInto(session, text)) copyInstead(text, t("browser.pick_terminal_closed")); + if (!pasteInto(session, text)) void copyInstead(text, t("browser.pick_terminal_closed")); }; const start = async () => { @@ -117,19 +119,24 @@ export function PickButton({ tabId, workspaceId }: PickButtonProps) { } pickingRef.current = true; setPicking(true); - const result = await ipcCallResult("cli", "pickElement", { tabId }); - pickingRef.current = false; - setPicking(false); - if (!result.ok) { - showToast({ kind: "error", message: t("browser.pick_failed", { message: result.message }) }); - return; + try { + const result = await ipcCallResult("cli", "pickElement", { tabId }); + if (!result.ok) { + surfaceError(ipcErrorToAppError(result), { surface: "toast" }); + return; + } + if (!("text" in result.value)) return; + const { text } = result.value; + const sessions = await agentSessions(workspaceId); + if (sessions.length === 0) void copyInstead(text, t("browser.pick_no_agent")); + else if (sessions.length === 1 && sessions[0]) deliver(sessions[0], text); + else setChoice({ text, sessions }); + } catch (error) { + surfaceError(normaliseError(error), { surface: "toast" }); + } finally { + pickingRef.current = false; + setPicking(false); } - if (!("text" in result.value)) return; - const { text } = result.value; - const sessions = await agentSessions(workspaceId); - if (sessions.length === 0) copyInstead(text, t("browser.pick_no_agent")); - else if (sessions.length === 1 && sessions[0]) deliver(sessions[0], text); - else setChoice({ text, sessions }); }; const label = picking ? t("browser.cancel_pick") : t("browser.pick_element"); diff --git a/src/renderer/components/editor/commit-tab.tsx b/src/renderer/components/editor/commit-tab.tsx index 6d6fbf1d8..9f9a4559e 100644 --- a/src/renderer/components/editor/commit-tab.tsx +++ b/src/renderer/components/editor/commit-tab.tsx @@ -2,11 +2,14 @@ import i18next from "i18next"; import { type ReactNode, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import type { CommitDetail, CommitFileChange } from "../../../shared/git/types"; +import { createLogger } from "../../../shared/log/renderer"; import { ipcCallResult, unwrapGitResult } from "../../ipc/client"; import { closeTab, openDiffTab } from "../../state/operations/tabs"; import { useTabsStore } from "../../state/stores/tabs"; import { Button } from "../ui/button"; +const log = createLogger("commit-tab"); + const EMPTY_TREE_SHA = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"; interface CommitTabProps { @@ -48,7 +51,9 @@ export function CommitTab({ workspaceId, sha, tabId }: CommitTabProps) { }) .catch((fetchError) => { if (controller.signal.aborted) return; - setError(commitTabErrorFrom(fetchError, sha)); + const error = commitTabErrorFrom(fetchError, sha); + if (!error.notFound) log.warn(`Commit details failed to load: ${String(fetchError)}`); + setError(error); }) .finally(() => { if (!controller.signal.aborted) setLoading(false); @@ -67,7 +72,7 @@ export function CommitTab({ workspaceId, sha, tabId }: CommitTabProps) {

- {detail?.subject ?? `commit ${sha.slice(0, 7)}`} + {detail?.subject ?? t("editor.commit_title", { sha: sha.slice(0, 7) })}

{detail?.sha ?? sha} @@ -194,15 +199,13 @@ export function formatCommitTime(value: string, locale: string): string { /** * Converts IPC failures into commit-tab rendering state. */ -function commitTabErrorFrom(error: unknown, sha: string): CommitTabError { +export function commitTabErrorFrom(error: unknown, sha: string): CommitTabError { const notFound = isCommitNotFoundError(error); return { notFound, message: notFound ? i18next.t("editor.commit_not_found", { sha: sha.slice(0, 7) }) - : error instanceof Error - ? error.message - : i18next.t("editor.commit_load_failed"), + : i18next.t("editor.commit_load_failed"), }; } diff --git a/src/renderer/components/files/file-tree/row.tsx b/src/renderer/components/files/file-tree/row.tsx index e84189231..c1c6023be 100644 --- a/src/renderer/components/files/file-tree/row.tsx +++ b/src/renderer/components/files/file-tree/row.tsx @@ -2,12 +2,12 @@ import { ChevronRight } from "lucide-react"; import { useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { useDragSource } from "@/components/ui/use-drag-source"; -import { buildFileDragPayload } from "@/components/workspace/dnd/payload"; -import { MIME_FILE } from "@/components/workspace/dnd/types"; import { distinctParents } from "@/services/fs-mutations/distinct-parents"; import { useFilesStore } from "@/state/stores/files"; +import { MIME_FILE } from "@/types/workbench-dnd"; import { cn } from "@/utils/cn"; import { basename } from "@/utils/path"; +import { buildFileDragPayload } from "@/utils/workbench-dnd-payload"; import type { TreeNode } from "../../../state/stores/files"; import { FileIcon } from "./file-icon"; import { type GitDecorationKind, kindToColorVar, kindToLetter } from "./git-decoration"; diff --git a/src/renderer/components/files/git/branch/picker-source.ts b/src/renderer/components/files/git/branch/picker-source.ts index bfad5c4b6..68e8228fd 100644 --- a/src/renderer/components/files/git/branch/picker-source.ts +++ b/src/renderer/components/files/git/branch/picker-source.ts @@ -72,7 +72,7 @@ export interface CreateBranchPickerSourceInput { export function createBranchPickerSource( input: CreateBranchPickerSourceInput, ): PaletteSource { - const mode = normalizeBranchPickerSourceMode(input.mode); + const mode = normalizeBranchPickerMode(input.mode); return { id: "git.branch-picker", @@ -229,7 +229,7 @@ export function createBranchPickerSource( * Keeps the historic `checkout` mode spelling as an alias for the default * switch/checkout behavior used by the Source Control panel. */ -function normalizeBranchPickerSourceMode( +export function normalizeBranchPickerMode( mode: BranchPickerSourceMode | undefined, ): NormalizedBranchPickerSourceMode { if (!mode || mode === "checkout") return "switch"; diff --git a/src/renderer/components/files/git/branch/picker.tsx b/src/renderer/components/files/git/branch/picker.tsx index d3d8bd43e..f4b5283a2 100644 --- a/src/renderer/components/files/git/branch/picker.tsx +++ b/src/renderer/components/files/git/branch/picker.tsx @@ -10,8 +10,8 @@ import type React from "react"; import type { Dispatch, SetStateAction } from "react"; import { useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; -import type { GitStoreError } from "../../../../state/stores/git"; import { useGitStore } from "../../../../state/stores/git"; +import { classifyGitError } from "../../../../utils/error-classification"; import { Button } from "../../../ui/button"; import { DIALOG_OVERLAY_CLASS, dialogContentClass } from "../../../ui/dialog"; import { DialogHeader } from "../../../ui/dialog-header"; @@ -22,6 +22,7 @@ import { type BranchPickerSourceMode, type BranchPickItem, createBranchPickerSource, + normalizeBranchPickerMode, } from "./picker-source"; interface BranchPickerProps { @@ -198,17 +199,6 @@ export interface ConfirmDeleteDeps { setDeleteRequest: Dispatch>; } -/** - * Keeps the legacy `checkout` spelling as a compatibility alias for the - * default switch mode. - */ -function normalizeBranchPickerMode( - mode: BranchPickerMode | undefined, -): Exclude { - if (!mode || mode === "checkout") return "switch"; - return mode; -} - /** * Describes the shortcut footer for each branch picker mode. */ @@ -260,7 +250,7 @@ export async function confirmBranchDelete( } deps.setDeleteRequest(null); } catch (error) { - const gitError = gitStoreErrorFromUnknown(error); + const gitError = classifyGitError(error); if (request.kind === "local" && gitError.kind === "branch-not-fully-merged" && !request.force) { deps.setDeleteRequest({ ...request, @@ -391,20 +381,3 @@ function splitRemoteRef(remoteRef: string): { remote: string; name: string } | n if (slash <= 0 || slash === remoteRef.length - 1) return null; return { remote: remoteRef.slice(0, slash), name: remoteRef.slice(slash + 1) }; } - -/** - * Normalizes thrown IPC errors into the subset needed by the delete flow. - */ -function gitStoreErrorFromUnknown(error: unknown): Pick { - if (typeof error === "object" && error !== null) { - const record = error as { kind?: unknown; message?: unknown }; - return { - kind: typeof record.kind === "string" ? record.kind : "unknown", - message: - typeof record.message === "string" - ? record.message - : i18next.t("files:git.gitOperationFailed"), - }; - } - return { kind: "unknown", message: String(error) }; -} diff --git a/src/renderer/components/files/git/file-row/group.tsx b/src/renderer/components/files/git/file-row/group.tsx index 0f5b176c5..21feda081 100644 --- a/src/renderer/components/files/git/file-row/group.tsx +++ b/src/renderer/components/files/git/file-row/group.tsx @@ -13,12 +13,12 @@ import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import type { GitExpandedGroupKey, GitStatusEntry } from "../../../../../shared/git/types"; import type { ViewMode } from "../../../../../shared/types/panel"; -import { type GitDecorationKind, kindFromEntry, maxKind } from "../../file-tree/git-decoration"; import { buildPathTree, collectDescendantLeafPaths, type PathTreeNode, -} from "../../file-tree/tree-builder"; +} from "../../../../utils/path-tree"; +import { type GitDecorationKind, kindFromEntry, maxKind } from "../../file-tree/git-decoration"; import type { TreeKeyboardRow } from "../../file-tree/use-tree-keyboard"; import { useTreeKeyboard } from "../../file-tree/use-tree-keyboard"; import { collectGitEntryPaths } from "../utils/status-utils"; diff --git a/src/renderer/components/files/git/hooks/use-helper-prompts.ts b/src/renderer/components/files/git/hooks/use-helper-prompts.ts index 6f4b4fc17..a4f15d7b0 100644 --- a/src/renderer/components/files/git/hooks/use-helper-prompts.ts +++ b/src/renderer/components/files/git/hooks/use-helper-prompts.ts @@ -129,7 +129,7 @@ function subscribeGitHelperPromptSnapshot(listener: () => void): () => void { } /** - * Installs the single renderer listener pair for Git helper prompts. + * Installs the global renderer listeners for Git helper prompts. */ export function installGitHelperPromptListeners(listen: typeof ipcListen = ipcListen): () => void { const unlistenAskpass = listen("askpass", "prompt", (prompt) => { @@ -141,10 +141,14 @@ export function installGitHelperPromptListeners(listen: typeof ipcListen = ipcLi const unlistenEditor = listen("editor", "prompt", (prompt) => { enqueuePrompt({ kind: "editor", prompt }); }); + const unlistenEditorClosed = listen("editor", "closed", ({ promptId }) => { + clearEditorPrompt(promptId); + }); return () => { unlistenAskpass(); unlistenAskpassClosed(); unlistenEditor(); + unlistenEditorClosed(); }; } diff --git a/src/renderer/components/files/git/panel/panel-actions.ts b/src/renderer/components/files/git/panel/panel-actions.ts index 61ab20c85..471c0d92c 100644 --- a/src/renderer/components/files/git/panel/panel-actions.ts +++ b/src/renderer/components/files/git/panel/panel-actions.ts @@ -4,9 +4,9 @@ */ import i18next from "i18next"; import type { Tag } from "../../../../../shared/git/types"; +import { isAuthGitError } from "../../../../services/error-surface/git-error-placement"; import { openTerminal } from "../../../../services/terminal"; import type { GitStoreError } from "../../../../state/stores/git"; -import { isAuthGitError } from "../../../../state/stores/git/error-placement"; import type { PromptRequest } from "../../../ui/prompt-dialog"; /** diff --git a/src/renderer/components/files/git/panel/panel.tsx b/src/renderer/components/files/git/panel/panel.tsx index 2e7ade574..6ccccf3a6 100644 --- a/src/renderer/components/files/git/panel/panel.tsx +++ b/src/renderer/components/files/git/panel/panel.tsx @@ -20,10 +20,10 @@ import { type RepoCapabilities, } from "../../../../../shared/git/types"; import { useStableHandlers } from "../../../../hooks/use-stable-handlers"; +import { gitErrorPlacement } from "../../../../services/error-surface/git-error-placement"; import { useGitSession, useGitStore } from "../../../../state/stores/git"; import { selectGitActionButton } from "../../../../state/stores/git/action-button"; import { getCommitDraft, useCommitDraftStore } from "../../../../state/stores/git/commit-drafts"; -import { gitErrorPlacement } from "../../../../state/stores/git/error-placement"; import { usePanelViewOptionsStore, useViewOptions, diff --git a/src/renderer/components/files/hooks/use-file-tree-drop-target.ts b/src/renderer/components/files/hooks/use-file-tree-drop-target.ts index c9bfacf1a..b2c43430a 100644 --- a/src/renderer/components/files/hooks/use-file-tree-drop-target.ts +++ b/src/renderer/components/files/hooks/use-file-tree-drop-target.ts @@ -34,9 +34,9 @@ import { parentOf } from "@/state/stores/files/helpers"; import { useFilesStore } from "@/state/stores/files/store"; import { basename, relPath } from "@/utils/path"; import { createLogger } from "../../../../shared/log/renderer"; +import { MIME_FILE } from "../../../types/workbench-dnd"; import { isMac } from "../../../utils/platform"; -import { parseFileDragPayload } from "../../workspace/dnd/payload"; -import { MIME_FILE } from "../../workspace/dnd/types"; +import { parseFileDragPayload } from "../../../utils/workbench-dnd-payload"; /** * Delay (ms) before a closed directory under the cursor auto-expands diff --git a/src/renderer/components/files/panel.tsx b/src/renderer/components/files/panel.tsx index 815a84498..32d2c8df8 100644 --- a/src/renderer/components/files/panel.tsx +++ b/src/renderer/components/files/panel.tsx @@ -1,10 +1,12 @@ -import i18next from "i18next"; import { Bot, Folder, GitBranch, NotebookPen, Search } from "lucide-react"; -import { memo, useState } from "react"; +import { memo, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { cn } from "@/utils/cn"; -import { ipcCallResult } from "../../ipc/client"; +import { AppErrorSchema, appErrorFailed } from "../../../shared/error/app-error"; +import { useIpcAction } from "../../hooks/use-ipc-action"; +import { surfaceError } from "../../services/error-surface/surface-error"; import { openDiffTab } from "../../state/operations"; +import { connectWorkspace } from "../../state/operations/workspace-selection"; import { useActiveStore } from "../../state/stores/active"; import { useGitStore } from "../../state/stores/git"; import { @@ -23,7 +25,6 @@ import { Button } from "../ui/button"; import { EmptyState } from "../ui/empty-state"; import { ErrorBoundary } from "../ui/error-boundary"; import { ResizeHandle } from "../ui/resize-handle"; -import { showToast } from "../ui/toast"; import { FileTree } from "./file-tree"; import { GitPanel, type GitPanelOpenDiffInput } from "./git"; import { SearchPanel } from "./search/panel"; @@ -44,33 +45,6 @@ const MODE_BUTTONS: ModeButton[] = [ { mode: "notes", labelKey: "files:panel.tab.notes", Icon: NotebookPen }, ]; -// --------------------------------------------------------------------------- -// Reconnect helper — exported so unit tests can verify the async logic -// without mounting the full component. -// --------------------------------------------------------------------------- - -/** - * Attempts to reconnect the SSH workspace. - * - Success: the main process broadcasts a connectionChanged event and the store updates. - * - Failure: the user is told with a toast. - * - * The `workspace.activate` IPC calls `ensureProviderReady` internally, so calling it again - * after the previous connection failed (the sshProviderReady cache was removed) starts a - * new SSH bootstrap. - */ -export async function reconnectWorkspace( - workspaceId: string, - deps: { - callActivate: (id: string) => Promise<{ ok: boolean; message?: string }>; - onError: (message: string) => void; - }, -): Promise { - const result = await deps.callActivate(workspaceId); - if (!result.ok) { - deps.onError(i18next.t("files:panel.reconnectError")); - } -} - // Memo: kept while hidden, it would render again with each of the app's. export const FilesPanel = memo(function FilesPanel() { const { t } = useTranslation("files"); @@ -98,19 +72,23 @@ export const FilesPanel = memo(function FilesPanel() { activeWorkspace !== null && activeWorkspace.location.kind !== "local" && !workspaceOnline; // Whether a reconnect is in progress — used to disable the Reconnect button and change its label. - const [isReconnecting, setIsReconnecting] = useState(false); + const reconnect = useIpcAction(); + const isReconnecting = reconnect.state.status === "loading"; + const connectionError = reconnect.state.status === "error" ? reconnect.state.error : null; + useEffect(() => { + if (connectionError) surfaceError(connectionError, { surface: "toast" }); + }, [connectionError]); // Bumped by each click on the Search button so the panel focuses its input, // whether the click mounts it or it is already showing. const [searchFocusRequest, setSearchFocusRequest] = useState(0); const handleReconnect = (): void => { if (!activeWorkspace || isReconnecting) return; - setIsReconnecting(true); - void reconnectWorkspace(activeWorkspace.id, { - callActivate: (id) => ipcCallResult("workspace", "activate", { id }), - onError: (message) => showToast({ kind: "error", message }), - }).finally(() => { - setIsReconnecting(false); + void reconnect.run(async (signal) => { + const result = await connectWorkspace(activeWorkspace.id, signal); + if (result.ok) return; + const error = AppErrorSchema.safeParse(result); + throw error.success ? error.data : appErrorFailed(result.message); }); }; diff --git a/src/renderer/components/files/search/results-list.tsx b/src/renderer/components/files/search/results-list.tsx index f75bbb4ef..e6f06af4d 100644 --- a/src/renderer/components/files/search/results-list.tsx +++ b/src/renderer/components/files/search/results-list.tsx @@ -22,9 +22,9 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { revealEditorAt } from "@/services/editor/tabs"; import type { FileGroup } from "../../../state/stores/search"; +import type { PathTreeNode } from "../../../utils/path-tree"; +import { buildPathTree } from "../../../utils/path-tree"; import { ROW_HEIGHT_PX } from "../file-tree/metrics"; -import type { PathTreeNode } from "../file-tree/tree-builder"; -import { buildPathTree } from "../file-tree/tree-builder"; import type { TreeKeyboardRow } from "../file-tree/use-tree-keyboard"; import { useTreeKeyboard } from "../file-tree/use-tree-keyboard"; import { SearchResultFileRow } from "./result-file-row"; diff --git a/src/renderer/components/files/search/use-loader-delay.ts b/src/renderer/components/files/search/use-loader-delay.ts index 90d2eb2a5..98844872e 100644 --- a/src/renderer/components/files/search/use-loader-delay.ts +++ b/src/renderer/components/files/search/use-loader-delay.ts @@ -1,44 +1,8 @@ -/** - * useLoaderDelay — converts a search status into a delayed `showLoader` boolean. - * - * Contract: - * - Returns `true` only when `status === "running"` AND the status has been - * running for at least `LOADER_DELAY_MS` (250 ms, imported from - * SearchStatusHeader so the two values stay in sync). - * - When the status transitions away from "running" the boolean resets to - * `false` immediately (no trailing visibility). - * - The hook owns the `setTimeout` ref internally; the timer is cleared both - * on status change and on unmount, so stale state updates never occur. - * - * The delay prevents a loader flash for fast searches that complete before - * the timeout fires. - */ - -import { useEffect, useRef, useState } from "react"; +import { useDelayedLoading } from "../../../hooks/use-delayed-loading"; import type { SearchStatus } from "../../../state/stores/search"; import { LOADER_DELAY_MS } from "./status-header"; +/** Applies the search and Source Control loading delay to a running search. */ export function useLoaderDelay(status: SearchStatus | undefined): boolean { - const [showLoader, setShowLoader] = useState(false); - const timerRef = useRef | null>(null); - - useEffect(() => { - if (status === "running") { - timerRef.current = setTimeout(() => setShowLoader(true), LOADER_DELAY_MS); - } else { - if (timerRef.current !== null) { - clearTimeout(timerRef.current); - timerRef.current = null; - } - setShowLoader(false); - } - return () => { - if (timerRef.current !== null) { - clearTimeout(timerRef.current); - timerRef.current = null; - } - }; - }, [status]); - - return showLoader; + return useDelayedLoading(status === "running", LOADER_DELAY_MS); } diff --git a/src/renderer/components/notes/editor/asset-nodes.tsx b/src/renderer/components/notes/editor/asset-nodes.tsx index 89edb7bd9..c398054ad 100644 --- a/src/renderer/components/notes/editor/asset-nodes.tsx +++ b/src/renderer/components/notes/editor/asset-nodes.tsx @@ -8,6 +8,7 @@ import { FileText, FolderOpen } from "lucide-react"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { cn } from "@/utils/cn"; +import { formatBytes } from "../../../utils/format-bytes"; import type { NoteHost } from "./host"; export interface AssetNodeOptions { @@ -84,13 +85,6 @@ function ImageView({ node, selected, extension }: ReactNodeViewProps) { ); } -export function formatBytes(n: number): string { - if (n < 1024) return `${n} B`; - if (n < 1024 * 1024) return `${(n / 1024).toFixed(n < 10 * 1024 ? 1 : 0)} KB`; - if (n < 1024 * 1024 * 1024) return `${(n / 1024 / 1024).toFixed(1)} MB`; - return `${(n / 1024 / 1024 / 1024).toFixed(1)} GB`; -} - function FileView({ node, selected, extension }: ReactNodeViewProps) { const { t } = useTranslation("notes"); const host = (extension.options as AssetNodeOptions).host; @@ -113,7 +107,7 @@ function FileView({ node, selected, extension }: ReactNodeViewProps) { {name} - {formatBytes(size)} · {t("file.copied")} + {formatBytes(size, "notes")} · {t("file.copied")} + ); +} diff --git a/src/renderer/components/settings/panels/about-panel.tsx b/src/renderer/components/settings/panels/about-panel.tsx index a718ac449..ab6139769 100644 --- a/src/renderer/components/settings/panels/about-panel.tsx +++ b/src/renderer/components/settings/panels/about-panel.tsx @@ -39,9 +39,9 @@ import { type ReactNode, useCallback } from "react"; import { useTranslation } from "react-i18next"; import type { UpdateProgress, UpdateStatus } from "../../../../shared/updates/status"; import { ipcCallResult } from "../../../ipc/client"; +import { installUpdate } from "../../../services/updates/operations"; +import { updateCheckFailedMessage } from "../../../services/updates/update-check-failure"; import { showReleaseNotes } from "../../../state/operations/release-notes"; -import { updateCheckFailedMessage } from "../../../state/operations/update-check-failure"; -import { installUpdate } from "../../../state/operations/updates"; import { useUpdatesStore } from "../../../state/stores/updates"; import { formatBytes } from "../../../utils/format-bytes"; import { Button } from "../../ui/button"; diff --git a/src/renderer/components/settings/panels/appearance-panel.tsx b/src/renderer/components/settings/panels/appearance-panel.tsx index 7fc00190a..8384c9c35 100644 --- a/src/renderer/components/settings/panels/appearance-panel.tsx +++ b/src/renderer/components/settings/panels/appearance-panel.tsx @@ -332,10 +332,10 @@ interface ThemeCardProps { function ThemeCard({ source, selected }: ThemeCardProps) { const { t } = useTranslation("settings"); // Surface chrome first, then the syntax accents. - const swatches: { value: string; label: string }[] = [ - { value: source.bg.primary, label: "bg" }, + const swatches = [ + { value: source.bg.primary, label: "background" }, { value: source.bg.secondary, label: "panel" }, - { value: source.bg.floating, label: "float" }, + { value: source.bg.floating, label: "floating" }, { value: source.fg.muted, label: "muted" }, { value: source.accent, label: "accent" }, { value: source.syntax.keyword, label: "keyword" }, @@ -343,7 +343,7 @@ function ThemeCard({ source, selected }: ThemeCardProps) { { value: source.syntax.number, label: "number" }, { value: source.syntax.function, label: "function" }, { value: source.syntax.type, label: "type" }, - ]; + ] as const; return ( {swatches.map((s) => ( - + ))}

diff --git a/src/renderer/components/settings/panels/keybindings-panel.tsx b/src/renderer/components/settings/panels/keybindings-panel.tsx index 4effc2b12..9a7324cdd 100644 --- a/src/renderer/components/settings/panels/keybindings-panel.tsx +++ b/src/renderer/components/settings/panels/keybindings-panel.tsx @@ -675,17 +675,18 @@ function KeybindingRecorder({ {conflicts.map((c, i) => { const key = `${c.kind}-${c.command ?? c.reserved?.accelerator ?? i}`; + const note = c.reserved ? t(`keybindings.reservedNote.${c.reserved.noteKey}`) : ""; let message: string; if (c.kind === "blocking" || c.kind === "overlap") { message = t(`keybindings.conflict.${c.kind}`, { command: c.command !== undefined ? commandLabel(c.command) : "?", }); } else if (c.kind === "system") { - message = t("keybindings.conflict.system", { note: c.reserved?.note ?? "" }); + message = t("keybindings.conflict.system", { note: note }); } else { message = t("keybindings.conflict.shadow", { owner: c.reserved !== undefined ? t(`keybindings.owner.${c.reserved.source}`) : "?", - note: c.reserved?.note ?? "", + note: note, }); } const severe = c.kind === "blocking" || c.kind === "system"; diff --git a/src/renderer/components/settings/panels/notes-panel.tsx b/src/renderer/components/settings/panels/notes-panel.tsx index 1cc036069..d9c0a2065 100644 --- a/src/renderer/components/settings/panels/notes-panel.tsx +++ b/src/renderer/components/settings/panels/notes-panel.tsx @@ -7,12 +7,15 @@ import { FolderOpen, Trash2 } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { cn } from "@/utils/cn"; +import { ipcErrorToAppError } from "../../../../shared/ipc/result"; import type { NoteStorageEntry } from "../../../../shared/notes/types"; -import { ipcCallResult, unwrapIpcResult } from "../../../ipc/client"; +import { useIpcAction } from "../../../hooks/use-ipc-action"; +import { ipcCallResult } from "../../../ipc/client"; +import { surfaceError } from "../../../services/error-surface/surface-error"; import { notesApi } from "../../../services/notes/api"; -import { formatBytes } from "../../notes/editor/asset-nodes"; +import { formatBytes } from "../../../utils/format-bytes"; +import { Button } from "../../ui/button"; import { showConfirmDialog } from "../../ui/confirm-dialog"; -import { showToast } from "../../ui/toast"; import { SettingsGroup, SettingsPage } from "../section"; type Entry = NoteStorageEntry & { workspaceIds: string[] }; @@ -30,35 +33,67 @@ export function NotesPanel() { const { t } = useTranslation("notes"); const [entries, setEntries] = useState(null); - const load = useCallback(async () => { - try { - setEntries(unwrapIpcResult(await ipcCallResult("notes", "storage", undefined)) as Entry[]); - } catch (e) { - showToast({ kind: "error", message: e instanceof Error ? e.message : String(e) }); + const { + run: runLoad, + state: loadState, + cancel: cancelLoad, + } = useIpcAction({ + onSuccess: setEntries, + }); + const load = useCallback(() => { + runLoad(async (signal) => { + const result = await ipcCallResult("notes", "storage", undefined, { signal }); + if (!result.ok) throw ipcErrorToAppError(result); + return result.value as Entry[]; + }); + }, [runLoad]); + useEffect(() => { + load(); + return notesApi.onChanged(() => { + cancelLoad(); + load(); + }); + }, [cancelLoad, load]); + + const loadError = loadState.status === "error" ? loadState.error : null; + const [loadMessage, setLoadMessage] = useState(null); + useEffect(() => { + if (!loadError) { + setLoadMessage(null); + return; } - }, []); + const present = surfaceError(loadError, { surface: "banner", onRetry: load }); + setLoadMessage( + present.surface === "banner" || present.surface === "inline" ? present.userMessage : null, + ); + }, [loadError, load]); + + const { run: runRemove, state: removeState } = useIpcAction({ onSuccess: load }); + const removeError = removeState.status === "error" ? removeState.error : null; useEffect(() => { - void load(); - return notesApi.onChanged(() => void load()); - }, [load]); + if (removeError) surfaceError(removeError, { surface: "toast" }); + }, [removeError]); const remove = async (entry: Entry) => { const ok = await showConfirmDialog({ title: t("storage.removeConfirm.title", { label: entry.location.label }), description: t("storage.removeConfirm.description", { count: entry.notes, - size: formatBytes(total(entry)), + size: formatBytes(total(entry), "notes"), }), confirmLabel: t("storage.removeConfirm.confirm"), variant: "destructive", }); if (!ok) return; - try { - unwrapIpcResult(await ipcCallResult("notes", "removeLocation", { key: entry.location.key })); - } catch (e) { - showToast({ kind: "error", message: e instanceof Error ? e.message : String(e) }); - } - void load(); + runRemove(async (signal) => { + const result = await ipcCallResult( + "notes", + "removeLocation", + { key: entry.location.key }, + { signal }, + ); + if (!result.ok) throw ipcErrorToAppError(result); + }); }; const sum = (entries ?? []).reduce((a, e) => a + total(e), 0); @@ -67,12 +102,24 @@ export function NotesPanel() { return ( + {loadError && ( +
+ {loadMessage && ( +

+ {loadMessage} +

+ )} + +
+ )} {entries === null ? null : entries.length === 0 ? (

{t("storage.empty")}

) : (

- {t("storage.total", { size: formatBytes(sum), count })} + {t("storage.total", { size: formatBytes(sum, "notes"), count })}

@@ -106,7 +153,7 @@ export function NotesPanel() {
- {formatBytes(total(entry))} + {formatBytes(total(entry), "notes")} {t("storage.notes", { count: entry.notes })} @@ -129,6 +176,7 @@ export function NotesPanel() { aria-label={t("storage.remove")} title={t("storage.remove")} onClick={() => void remove(entry)} + disabled={removeState.status === "loading"} >
); diff --git a/src/renderer/components/workbench/workspace-color-menu.tsx b/src/renderer/components/workbench/workspace-color-menu.tsx new file mode 100644 index 000000000..cb7d540c2 --- /dev/null +++ b/src/renderer/components/workbench/workspace-color-menu.tsx @@ -0,0 +1,53 @@ +// A workspace's "Color ›" submenu in the sidebar: "Auto" or one of the theme's +// lane colors, which tint the workspace's row and its agents in the agent view. + +import { useTranslation } from "react-i18next"; +import { + WORKSPACE_COLOR_AUTO, + WORKSPACE_COLORS, + type WorkspaceColor, + workspaceColorToken, +} from "../../../shared/types/workspace-color"; +import { + ContextMenuChoice, + ContextMenuSeparator, + ContextMenuSub, + ContextMenuSubContent, + ContextMenuSubTrigger, +} from "../ui/context-menu"; + +export function WorkspaceColorMenu({ + color, + onSelect, +}: { + color: WorkspaceColor; + onSelect: (color: WorkspaceColor) => void; +}) { + const { t } = useTranslation(); + return ( + + + {t("sidebar.color")} + + + onSelect(WORKSPACE_COLOR_AUTO)} + > + {t("sidebar.color_auto")} + + + {WORKSPACE_COLORS.map((name) => ( + onSelect(name)}> + + {t(`sidebar.colors.${name}`)} + + ))} + + + ); +} diff --git a/src/renderer/components/workspace/add-workspace/add-workspace-dialog.tsx b/src/renderer/components/workspace/add-workspace/add-workspace-dialog.tsx index bc604fa95..be0c262e3 100644 --- a/src/renderer/components/workspace/add-workspace/add-workspace-dialog.tsx +++ b/src/renderer/components/workspace/add-workspace/add-workspace-dialog.tsx @@ -59,6 +59,7 @@ export function AddWorkspaceDialog({ // Imperative handle to the directory picker — the footer "Add workspace" // button lives here in the shell and drives the picker's action through it. const pickerRef = useRef(null); + const cancelConnectRef = useRef<(() => void) | null>(null); // Open effect — reset + load SSH config hosts useEffect(() => { @@ -95,6 +96,7 @@ export function AddWorkspaceDialog({ // Handlers function closeAndAbort(): void { + cancelConnectRef.current?.(); onClose(); } @@ -152,6 +154,7 @@ export function AddWorkspaceDialog({ if (view === "ssh-server-list") { setView("main-list"); } else if (view === "ssh-new-connection") { + cancelConnectRef.current?.(); setPrefillProfile(null); setView("ssh-server-list"); } else if (view === "ssh-directory-picker") { @@ -233,6 +236,7 @@ export function AddWorkspaceDialog({ void) | null>; readonly view: ModalView; readonly pickerRef: React.RefObject; readonly browseSession: SshBrowseSession | WslBrowseSession | null; @@ -334,6 +339,7 @@ function ViewBody(props: ViewBodyProps): React.JSX.Element { const { view, pickerRef, + cancelConnectRef, browseSession, prefillProfile, onViewChange, @@ -396,6 +402,7 @@ function ViewBody(props: ViewBodyProps): React.JSX.Element { return ( { + const surface = surfaceError(normaliseError(error), { surface: "inline" }); + if (surface.surface === "inline") { + setErrorId(profile.id); + setErrorHuman(surface.userMessage); + } + }; try { - // openBrowseSession is migrated to the IpcResult contract — auth - // cancellation arrives as ipcErr("cancelled") so the router stays silent - // and the renderer branches without showing an error banner. const result = await openSshBrowseSession({ host: profile.host, user: profile.user, @@ -89,10 +95,7 @@ export function SshConnectionListView({ progressId: beginProgress(), }); if (!result.ok) { - // User cancelled the SSH auth prompt — silent stop, no error banner. - if (result.kind === "cancelled") return; - setErrorId(profile.id); - setErrorHuman(humanizeSshError(result)); + presentError(result); return; } // Update usage record @@ -116,6 +119,8 @@ export function SshConnectionListView({ connectionProfileId: profile.id, }; onConnected(session); + } catch (error) { + presentError(error); } finally { setConnectingId(null); clearProgress(); diff --git a/src/renderer/components/workspace/add-workspace/ssh-directory-picker-view.tsx b/src/renderer/components/workspace/add-workspace/ssh-directory-picker-view.tsx index 543d5c3d0..37bca9977 100644 --- a/src/renderer/components/workspace/add-workspace/ssh-directory-picker-view.tsx +++ b/src/renderer/components/workspace/add-workspace/ssh-directory-picker-view.tsx @@ -2,7 +2,10 @@ import { ChevronRight, CornerLeftUp, Folder } from "lucide-react"; import type { FormEvent } from "react"; import { useCallback, useEffect, useImperativeHandle, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; +import type { AppError } from "../../../../shared/error/app-error"; import type { DirEntry } from "../../../../shared/fs/types"; +import { normaliseError } from "../../../hooks/use-ipc-action"; +import { surfaceError } from "../../../services/error-surface/surface-error"; import { browseSshSession, closeSshBrowseSession, @@ -14,7 +17,6 @@ import { import { EmptyState } from "../../ui/empty-state"; import { Skeleton, SkeletonLine } from "../../ui/skeleton"; import { ErrorNotice } from "./error-notice"; -import { humanizeSshError } from "./ssh-helpers"; import type { SshDirectoryPickerViewProps } from "./types"; import { type BrowseCacheEntry, usePathAutocomplete } from "./use-path-autocomplete"; @@ -58,14 +60,10 @@ function parentPath(path: string): string { return clean.slice(0, idx); } -/** Extract SSH error kind from an IPC error. */ -function extractSshErrorKind(error: unknown): BrowseErrorKind { - if (!(error instanceof Error)) return "retryable"; - const msg = error.message; - if (msg.includes("ssh.path-not-found")) return "path-not-found"; - for (const code of SESSION_FATAL_CODES) { - if (msg.includes(code)) return "session-expired"; - } +/** Selects a recovery action from the browse error's typed code. */ +function browseErrorKindFor(error: AppError): BrowseErrorKind { + if (error.code === "ssh.path-not-found") return "path-not-found"; + if (error.code && SESSION_FATAL_CODES.has(error.code)) return "session-expired"; return "retryable"; } @@ -143,29 +141,36 @@ export function SshDirectoryPickerView({ setBrowseErrorHuman(null); setBrowseErrorKind(null); + const presentError = (error: unknown) => { + const appError = normaliseError(error); + const surface = surfaceError(appError, { surface: "inline" }); + if (surface.surface !== "inline") return; + const kind = browseErrorKindFor(appError); + if (kind === "path-not-found") { + setPathErrorHuman(surface.userMessage); + } else { + setBrowseErrorHuman(surface.userMessage); + setBrowseErrorKind(kind); + } + }; + try { const result = await browseSshSession(sessionId, path); if (abortSignal?.aborted) return; + if (!result.ok) { + presentError(result); + return; + } - browseCache.current.set(path, { entries: result.entries, truncated: result.truncated }); + browseCache.current.set(path, result.value); setCurrentPath(path); - setEntries(result.entries); - setTruncated(result.truncated); + setEntries(result.value.entries); + setTruncated(result.value.truncated); setBrowseErrorHuman(null); setBrowseErrorKind(null); setPathErrorHuman(null); } catch (error) { - if (abortSignal?.aborted) return; - const kind = extractSshErrorKind(error); - if (kind === "path-not-found") { - // Non-destructive: the typed path is missing, but the previous listing - // (currentPath/entries) is untouched. Surface the error inline by the - // path bar and leave the tree on screen. - setPathErrorHuman(humanizeSshError(error)); - } else { - setBrowseErrorHuman(humanizeSshError(error)); - setBrowseErrorKind(kind); - } + if (!abortSignal?.aborted) presentError(error); } finally { if (!abortSignal?.aborted) setListLoading(false); } @@ -299,10 +304,7 @@ export function SshDirectoryPickerView({ if (!result.ok) { setAddPhase("idle"); - // User cancelled the auth prompt — silent stop, no error banner. - if (result.kind === "cancelled") return; - // Auth failure after the browse session — surface error. - setAddErrorHuman(humanizeSshError(result)); + presentAddError(result); return; } @@ -319,11 +321,17 @@ export function SshDirectoryPickerView({ await onWorkspaceCreated(result.value); onClose(); } catch (error) { - setAddErrorHuman(humanizeSshError(error)); + presentAddError(error); setAddPhase("idle"); } } + /** Presents an add failure once, beside the picker when it can be retried there. */ + function presentAddError(error: unknown): void { + const surface = surfaceError(normaliseError(error), { surface: "inline" }); + setAddErrorHuman(surface.surface === "inline" ? surface.userMessage : null); + } + // A WSL workspace needs no connection handoff: the distribution is reached // through wsl.exe again. It records no bookmark (bookmarks are local or SSH). async function addWslWorkspace(distro: string): Promise { @@ -331,13 +339,13 @@ export function SshDirectoryPickerView({ const result = await createWslWorkspace({ distro, remotePath: currentPath }); if (!result.ok) { setAddPhase("idle"); - setAddErrorHuman(result.message); + presentAddError(result); return; } await onWorkspaceCreated(result.value); onClose(); } catch (error) { - setAddErrorHuman(error instanceof Error ? error.message : String(error)); + presentAddError(error); setAddPhase("idle"); } } diff --git a/src/renderer/components/workspace/add-workspace/ssh-helpers.ts b/src/renderer/components/workspace/add-workspace/ssh-helpers.ts index 9c3182089..39c87384f 100644 --- a/src/renderer/components/workspace/add-workspace/ssh-helpers.ts +++ b/src/renderer/components/workspace/add-workspace/ssh-helpers.ts @@ -2,7 +2,6 @@ // Pure SSH utility functions — exported for unit tests. // --------------------------------------------------------------------------- -import i18next from "i18next"; import type { SshConfigHost } from "./types"; export function parseSshDestination(input: string): { host: string; user?: string } | null { @@ -90,54 +89,6 @@ export function formatProfileSubtitle(profile: { return `${userPrefix}${profile.host}${portSuffix}`; } -// --------------------------------------------------------------------------- -// SSH error humanisation -// --------------------------------------------------------------------------- - -/** - * Maps an SSH failure to human-readable cause + recovery sentences: an Error - * whose message embeds the SshErrorCode string, or an `ipcErr` result that - * carries the code in its `code` field. - * raw error.message is never surfaced directly in UI. - */ -export function humanizeSshError(error: unknown): string { - const t = i18next.t.bind(i18next); - if (typeof error !== "object" || error === null) { - return t("common:ssh.error.unknown_error"); - } - const { code, message } = error as { code?: unknown; message?: unknown }; - if (typeof code !== "string" && !(error instanceof Error)) { - return t("common:ssh.error.unknown_error"); - } - const msg = `${typeof code === "string" ? code : ""} ${typeof message === "string" ? message : ""}`; - if (msg.includes("ssh.connect-failed")) { - return t("common:ssh.error.connect_failed"); - } - if (msg.includes("ssh.auth-failed")) { - return t("common:ssh.error.auth_failed"); - } - if (msg.includes("ssh.session-expired")) { - return t("common:ssh.error.session_expired"); - } - if (msg.includes("ssh.path-not-found")) { - return t("common:ssh.error.path_not_found"); - } - if (msg.includes("server.spawn-failed")) { - return t("common:ssh.error.spawn_failed"); - } - if (msg.includes("server.protocol-version-mismatch")) { - return t("common:ssh.error.protocol_version_mismatch"); - } - if (msg.includes("server.protocol-error")) { - return t("common:ssh.error.protocol_error"); - } - if (msg.includes("transport.unknown") || msg.includes("ssh.unknown")) { - return t("common:ssh.error.ssh_unknown"); - } - // Fallback — intentionally generic, no raw message leak - return t("common:ssh.error.connection_failed"); -} - // --------------------------------------------------------------------------- // Path helpers // --------------------------------------------------------------------------- diff --git a/src/renderer/components/workspace/add-workspace/ssh-new-connection-view.tsx b/src/renderer/components/workspace/add-workspace/ssh-new-connection-view.tsx index c1c52ce3a..77d769d1c 100644 --- a/src/renderer/components/workspace/add-workspace/ssh-new-connection-view.tsx +++ b/src/renderer/components/workspace/add-workspace/ssh-new-connection-view.tsx @@ -4,9 +4,15 @@ import type { FormEvent, KeyboardEvent } from "react"; import { useEffect, useMemo, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { isImeComposing } from "@/utils/ime"; -import { appErrorCancelled, appErrorFailed } from "../../../../shared/error/app-error"; +import { appErrorInvalidInput } from "../../../../shared/error/app-error"; +import { ipcErrorToAppError } from "../../../../shared/ipc/result"; import { useIpcAction } from "../../../hooks/use-ipc-action"; -import { openSshBrowseSession, saveConnectionProfileResult } from "../../../services/workspace"; +import { surfaceError } from "../../../services/error-surface/surface-error"; +import { + closeSshBrowseSession, + openSshBrowseSession, + saveConnectionProfileResult, +} from "../../../services/workspace"; import { Button } from "../../ui/button"; import { showToast } from "../../ui/toast"; import { BootstrapProgressBar } from "../bootstrap-progress-bar"; @@ -16,7 +22,6 @@ import { filterSshConfigHosts, findSshConfigHost, formatSshHostSummary, - humanizeSshError, parseSshDestination, parseSshPort, sshHostOptionId, @@ -78,6 +83,7 @@ function showSaveFailedToast(onRetrySave: () => void): void { export function SshNewConnectionView({ onConnected, + cancelConnectRef, configHosts, configHostsLoading, onConnectPhaseChange, @@ -117,7 +123,14 @@ export function SshNewConnectionView({ // 'success', 'error', or 'idle' (cancel), so "Connecting…" can never freeze // regardless of which await fails or when the component unmounts. // --------------------------------------------------------------------------- - const { state: connectState, run: runConnect, isPending } = useIpcAction(); + const { state: connectState, run: runConnect, cancel, isPending } = useIpcAction(); + useEffect(() => { + if (!cancelConnectRef) return; + cancelConnectRef.current = cancel; + return () => { + if (cancelConnectRef.current === cancel) cancelConnectRef.current = null; + }; + }, [cancelConnectRef, cancel]); // Bootstrap progress for the in-flight connect (agent upload/verify happens // inside openSshBrowseSession, before any sessionId exists — keyed by a @@ -269,7 +282,7 @@ export function SshNewConnectionView({ // We throw an AppError so the hook normalises it into state:'error' and // the inline banner below renders the user message. runConnect(async () => { - throw appErrorFailed(t("ssh.invalid_host"), { + throw appErrorInvalidInput("Invalid SSH destination", { domain: "ssh", code: "invalid-host", }); @@ -292,13 +305,10 @@ export function SshNewConnectionView({ const capturedName = name.trim() || undefined; const profileId = crypto.randomUUID(); - runConnect(async (_signal) => { + runConnect(async (signal) => { // --- Stage 1: primary effect — establish SSH browse session --- - // openBrowseSession is migrated to the IpcResult contract. - // auth cancellation arrives as ipcErr("cancelled") so we throw an - // AppError category:"cancelled" and the hook silently returns to idle. - // _signal is kept in the signature for forward compatibility (AbortSignal - // passthrough to openSshBrowseSession once it supports cancellation). + // Keep receiving the result after cancellation so a session created + // by main can be closed even when the view has already gone away. const result = await openSshBrowseSession({ host: capturedDest.host, user: capturedDest.user, @@ -308,16 +318,7 @@ export function SshNewConnectionView({ progressId: beginProgress(), }); - if (!result.ok) { - if (result.kind === "cancelled") { - // Auth prompt dismissed by the user — throw category:'cancelled' so the - // hook routes this to idle without surfacing any error UI. - throw appErrorCancelled("SSH authentication was cancelled.", { domain: "ssh" }); - } - // Connection failure → throw so the hook transitions to state:'error'. - // The inline banner below renders state.error.message. - throw appErrorFailed(humanizeSshError(result), { domain: "ssh", code: result.kind }); - } + if (!result.ok) throw ipcErrorToAppError(result); // `result.value.user` is the user actually connected as — when the form // had only a host, the main process resolved it to the local account name, @@ -326,63 +327,82 @@ export function SshNewConnectionView({ const sessionId = result.value.sessionId; const initialPath = result.value.initialPath; - // --- Stage 2: secondary effect — persist the connection profile --- - // Partial-failure policy: a failed save must NOT block the workspace flow. - // We run the save and inspect the result, but onConnected is called - // unconditionally as long as stage 1 succeeded. - const saveResult = await saveConnectionProfileResult({ - id: profileId, - host: capturedDest.host, - user: connectedUser, - port: capturedPort, - identityFile: capturedIdentityFile, - authMode: "interactive", - label: capturedName, - }); - - if (!saveResult.ok) { - // Save failed — build a retry callback that replays only the save step. - // The signal from this run() is no longer active after the function - // returns (the hook clears it in finally), so the retry is a fresh - // independent call not tied to any AbortController. - // - // "Retry save" re-runs only the secondary effect; it never re-connects - // or re-authenticates. The already-established session is untouched. - const retrySave = (): void => { - void saveConnectionProfileResult({ - id: profileId, - host: capturedDest.host, - user: connectedUser, - port: capturedPort, - identityFile: capturedIdentityFile, - authMode: "interactive", - label: capturedName, - }).then((retryResult) => { - if (!retryResult.ok) { - // Second attempt also failed — offer another retry via a fresh toast. - showSaveFailedToast(retrySave); - } - }); - }; - - // Non-blocking warning toast: the workspace flow proceeds immediately. - // The toast uses role="alert" (action toast) so it never auto-dismisses - // and the user can act on the "Retry save" button at their convenience. - showSaveFailedToast(retrySave); + let owned = true; + /** Release the browse session until the directory picker takes ownership. */ + const release = (): void => { + if (!owned) return; + owned = false; + void closeSshBrowseSession(sessionId); + }; + if (signal.aborted) { + release(); + return; } + signal.addEventListener("abort", release, { once: true }); + try { + // --- Stage 2: secondary effect — persist the connection profile --- + // Partial-failure policy: a failed save must NOT block the workspace flow. + // We run the save and inspect the result, but onConnected is called + // unconditionally as long as stage 1 succeeded. + const saveResult = await saveConnectionProfileResult({ + id: profileId, + host: capturedDest.host, + user: connectedUser, + port: capturedPort, + identityFile: capturedIdentityFile, + authMode: "interactive", + label: capturedName, + }); - // Complete the workspace flow regardless of save outcome. - // onConnected transitions the parent to the directory picker view. - onConnected({ - sessionId, - initialPath, - host: capturedDest.host, - user: connectedUser, - port: capturedPort, - identityFile: capturedIdentityFile, - profileId, - connectionProfileId: profileId, - }); + if (signal.aborted) return; + if (!saveResult.ok) { + // Save failed — build a retry callback that replays only the save step. + // The signal from this run() is no longer active after the function + // returns (the hook clears it in finally), so the retry is a fresh + // independent call not tied to any AbortController. + // + // "Retry save" re-runs only the secondary effect; it never re-connects + // or re-authenticates. The already-established session is untouched. + const retrySave = (): void => { + void saveConnectionProfileResult({ + id: profileId, + host: capturedDest.host, + user: connectedUser, + port: capturedPort, + identityFile: capturedIdentityFile, + authMode: "interactive", + label: capturedName, + }).then((retryResult) => { + if (!retryResult.ok) { + // Second attempt also failed — offer another retry via a fresh toast. + showSaveFailedToast(retrySave); + } + }); + }; + + // Non-blocking warning toast: the workspace flow proceeds immediately. + // The toast uses role="alert" (action toast) so it never auto-dismisses + // and the user can act on the "Retry save" button at their convenience. + showSaveFailedToast(retrySave); + } + + // Complete the workspace flow regardless of save outcome. + // onConnected transitions the parent to the directory picker view. + onConnected({ + sessionId, + initialPath, + host: capturedDest.host, + user: connectedUser, + port: capturedPort, + identityFile: capturedIdentityFile, + profileId, + connectionProfileId: profileId, + }); + owned = false; + } finally { + signal.removeEventListener("abort", release); + release(); + } }); } @@ -397,9 +417,15 @@ export function SshNewConnectionView({ const connecting = connectState.status === "loading"; - // Inline error message for connection failures. - // Cancelled (state:'idle') and save failures (surfaced as toast) do not reach here. - const inlineError = connectState.status === "error" ? connectState.error.message : null; + const [inlineError, setInlineError] = useState(null); + useEffect(() => { + if (connectState.status !== "error") { + setInlineError(null); + return; + } + const surface = surfaceError(connectState.error, { surface: "inline" }); + setInlineError(surface.surface === "inline" ? surface.userMessage : null); + }, [connectState]); return (
void) | null>; /** Callback after successful connect — transitions to directory picker. */ readonly onConnected: (params: SshBrowseSession) => void; /** ~/.ssh/config candidate list (pre-loaded by parent). */ diff --git a/src/renderer/components/workspace/add-workspace/use-path-autocomplete.ts b/src/renderer/components/workspace/add-workspace/use-path-autocomplete.ts index 44da250b8..39ee6a0c6 100644 --- a/src/renderer/components/workspace/add-workspace/use-path-autocomplete.ts +++ b/src/renderer/components/workspace/add-workspace/use-path-autocomplete.ts @@ -97,9 +97,9 @@ export function usePathAutocomplete({ try { const result = await browseSshSession(sessionId, dir); if (abortSignal.aborted) return; - browseCache.current.set(dir, { entries: result.entries, truncated: result.truncated }); + if (result.ok) browseCache.current.set(dir, result.value); setSuggestionDir(dir); - setSuggestionEntries(result.entries); + setSuggestionEntries(result.ok ? result.value.entries : []); } catch { if (abortSignal.aborted) return; // Directory missing or not listable — surface no completions. diff --git a/src/renderer/components/workspace/add-workspace/wsl-distro-list-view.tsx b/src/renderer/components/workspace/add-workspace/wsl-distro-list-view.tsx index a1e9895e4..6003bfa22 100644 --- a/src/renderer/components/workspace/add-workspace/wsl-distro-list-view.tsx +++ b/src/renderer/components/workspace/add-workspace/wsl-distro-list-view.tsx @@ -1,7 +1,14 @@ import { ChevronRight, LoaderCircle, SquareTerminal } from "lucide-react"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; -import { listWslDistros, openWslBrowseSession } from "../../../services/workspace"; +import { ipcErrorToAppError } from "../../../../shared/ipc/result"; +import { useIpcAction } from "../../../hooks/use-ipc-action"; +import { surfaceError } from "../../../services/error-surface/surface-error"; +import { + closeSshBrowseSession, + listWslDistros, + openWslBrowseSession, +} from "../../../services/workspace"; import { EmptyState } from "../../ui/empty-state"; import { Skeleton, SkeletonLine } from "../../ui/skeleton"; import { BootstrapProgressBar } from "../bootstrap-progress-bar"; @@ -20,8 +27,10 @@ import { useBrowseProgress } from "./use-browse-progress"; export function WslDistroListView({ onConnected }: WslDistroListViewProps): React.JSX.Element { const { t } = useTranslation(); const [distros, setDistros] = useState(null); - const [connecting, setConnecting] = useState(null); - const [error, setError] = useState<{ distro: string; message: string } | null>(null); + const [selected, setSelected] = useState(null); + const [errorMessage, setErrorMessage] = useState(null); + const { state, run, isPending } = useIpcAction(); + const connecting = isPending ? selected : null; const { progress, begin: beginProgress, clear: clearProgress } = useBrowseProgress(); useEffect(() => { @@ -36,19 +45,30 @@ export function WslDistroListView({ onConnected }: WslDistroListViewProps): Reac }; }, []); - async function connect(distro: string): Promise { - if (connecting) return; - setConnecting(distro); - setError(null); - try { - const session = await openWslBrowseSession(distro, beginProgress()); - onConnected({ ...session, distro }); - } catch (err) { - setError({ distro, message: err instanceof Error ? err.message : String(err) }); - } finally { - clearProgress(); - setConnecting(null); - } + useEffect(() => { + if (state.status !== "error") return; + const surface = surfaceError(state.error, { surface: "inline" }); + if (surface.surface === "inline") setErrorMessage(surface.userMessage); + }, [state]); + + /** Starts one distribution and surfaces any failed attempt beside its row. */ + function connect(distro: string): void { + if (isPending) return; + setSelected(distro); + setErrorMessage(null); + run(async (signal) => { + try { + const result = await openWslBrowseSession(distro, beginProgress(), signal); + if (!result.ok) throw ipcErrorToAppError(result); + if (signal.aborted) { + void closeSshBrowseSession(result.value.sessionId); + return; + } + onConnected({ ...result.value, distro }); + } finally { + clearProgress(); + } + }); } if (distros === null) { @@ -77,7 +97,7 @@ export function WslDistroListView({ onConnected }: WslDistroListViewProps): Reac
    {distros.map((distro) => { const isConnecting = connecting === distro; - const errorMessage = error?.distro === distro ? error.message : null; + const rowError = selected === distro ? errorMessage : null; return (
  • - {errorMessage ? ( + {rowError ? ( diff --git a/src/renderer/components/workspace/content/browser-view.tsx b/src/renderer/components/workspace/content/browser-view.tsx index bb608f349..0d80380c1 100644 --- a/src/renderer/components/workspace/content/browser-view.tsx +++ b/src/renderer/components/workspace/content/browser-view.tsx @@ -56,9 +56,14 @@ import { BrowserEmptyState } from "@/components/editor/browser/empty-state"; import { NavControls } from "@/components/editor/browser/nav-controls"; import { PickButton } from "@/components/editor/browser/pick-button"; import { UrlBar } from "@/components/editor/browser/url-bar"; +import { EmptyState } from "@/components/ui/empty-state"; import { IconButton } from "@/components/ui/icon-button"; import { ipcCallResult } from "@/ipc/client"; -import { resolveInitialBrowserUrl } from "@/state/operations/browser"; +import { + beginBrowserView, + endBrowserView, + resolveInitialBrowserUrl, +} from "@/state/operations/browser"; import { useBrowserRuntimeStore } from "@/state/stores/browser-runtime"; import { cn } from "@/utils/cn"; @@ -124,8 +129,8 @@ export function BrowserTabView({ // updates both regions so a splitter drag still emits a single IPC pair // per paint cycle. const rafIdRef = useRef(null); - // Whether browser.create has been sent for this tabId. - const createdRef = useRef(false); + // The generation of the native view owned by this mount. + const viewIdRef = useRef(null); // User-controlled height of the inline DevTools region. Per-tab-mount; // does not survive a tab close. const [devtoolsHeight, setDevtoolsHeight] = useState(DEFAULT_DEVTOOLS_HEIGHT); @@ -141,6 +146,7 @@ export function BrowserTabView({ const canGoBack = runtime?.canGoBack ?? false; const canGoForward = runtime?.canGoForward ?? false; const isLoading = runtime?.isLoading ?? false; + const loadFailure = runtime?.loadFailure ?? null; // Page snapshot painted as an absolute overlay while the native view is // hidden by a suspendAll cycle. When set, it shows the last frame of the // page captured by main right before `setVisible(false)` — letting a @@ -159,19 +165,21 @@ export function BrowserTabView({ // `about:blank` is used as the synthetic initial load and is treated as // "no real content" for display purposes. Once the user navigates to an // http/https URL the empty state disappears. - const showEmptyState = (currentUrl === "" || currentUrl === BLANK_TAB_URL) && lastUrl === ""; + const showEmptyState = + !loadFailure && (currentUrl === "" || currentUrl === BLANK_TAB_URL) && lastUrl === ""; // ------------------------------------------------------------------------- // Mount: browser.create // ------------------------------------------------------------------------- // biome-ignore lint/correctness/useExhaustiveDependencies: tabId/workspaceId/partition are stable for lifetime useEffect(() => { - if (createdRef.current) return; - createdRef.current = true; + const viewId = beginBrowserView(workspaceId, tabId); + viewIdRef.current = viewId; const url = resolveInitialBrowserUrl({ initialUrl, lastUrl, partition }) ?? BLANK_TAB_URL; void ipcCallResult("browser", "create", { tabId, + viewId, workspaceId, url, partition, @@ -183,8 +191,9 @@ export function BrowserTabView({ cancelAnimationFrame(rafIdRef.current); rafIdRef.current = null; } - void ipcCallResult("browser", "destroy", { tabId }); - useBrowserRuntimeStore.getState().removeRuntime(tabId); + endBrowserView(tabId, viewId); + viewIdRef.current = null; + void ipcCallResult("browser", "destroy", { tabId, viewId }); }; }, []); @@ -193,6 +202,8 @@ export function BrowserTabView({ // ------------------------------------------------------------------------- const sendBounds = useCallback(() => { rafIdRef.current = null; + const viewId = viewIdRef.current; + if (viewId === null) return; const pageEl = placeholderRef.current; if (pageEl !== null) { @@ -203,6 +214,7 @@ export function BrowserTabView({ // — Chromium handles HiDPI scaling internally. void ipcCallResult("browser", "setBounds", { tabId, + viewId, x: rect.left, y: rect.top, width: rect.width, @@ -215,6 +227,7 @@ export function BrowserTabView({ const rect = dtEl.getBoundingClientRect(); void ipcCallResult("browser", "setDevToolsBounds", { tabId, + viewId, x: rect.left, y: rect.top, width: rect.width, @@ -239,7 +252,9 @@ export function BrowserTabView({ // Active state: browser.setActive // ------------------------------------------------------------------------- useEffect(() => { - void ipcCallResult("browser", "setActive", { tabId, active: isActive }); + const viewId = viewIdRef.current; + if (viewId === null) return; + void ipcCallResult("browser", "setActive", { tabId, viewId, active: isActive }); // On (re)activation the main process re-attaches the view and re-applies its // cached bounds. If the surrounding layout shifted while this tab was hidden // (e.g. the sidebar was toggled) those cached bounds are stale — re-measure. @@ -363,7 +378,7 @@ export function BrowserTabView({
    )} + {loadFailure && ( +
    + void ipcCallResult("browser", "reload", { tabId })} + disabled={isLoading} + tone="status" + className="[overflow-wrap:anywhere]" + /> +
    + )} {/* Placeholder: ResizeObserver target. The WebContentsView is overlaid by the main process using the CSS-pixel bounds of this element. */}