Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(ssh): the reconnect resync reads a 64 KB transcript tail, not 5 MB

enes46aa8d747419
diff --git a/src/main/index.ts b/src/main/index.ts
index b1a0b507b..ec42bfd47 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -140,7 +140,7 @@ import { connectRelayClient, type RelayClientSession } from './remote/relay-clie
import { decodeOffer } from './remote/pairing'
import { loadOrCreatePeerKeyPair } from './remote/peer-identity'
import { initSshProject } from './remote-ssh/ssh-project'
-import { resyncProjectAgents } from './remote-ssh/agent-resync'
+import { resyncProjectAgents, RESYNC_TRANSCRIPT_TAIL_BYTES } from './remote-ssh/agent-resync'
import { setGitRemoteResolver, type GitRemoteRef } from '../core/remote-ssh/remote-git'
import { SshFs, sshAppendArgs, sshTailArgs, sshSizeArgs, sshWriteArgs } from './ssh-fs'
import { makeRemoteWorkspaceIO } from './remote-workspace-io'
@@ -1283,13 +1283,22 @@ app.whenReady().then(async () => {
/**
* Read a remote transcript through a ref, forgetting refs WE located once they stop reading.
+ * `cap` is the tail window in bytes; it defaults to the full reader's cap, so every caller that
+ * wants a transcript to READ is unchanged. A caller that only wants to know how the last few
+ * records look (the reconnect resync) passes a much smaller one — see
+ * RESYNC_TRANSCRIPT_TAIL_BYTES.
+ *
* Without this the panel's Retry replays a dead path forever (the transcript was deleted, or the
* session moved) because the cache lookup comes first. A hook-fed ref is deliberately left in
* place on an empty read — that is usually a transient master hiccup, and dropping it would send
* the next read down the LOCAL resolver, i.e. to the wrong machine.
*/
- const readRemoteTranscript = async (sessionId: string, ref: RemoteFileRef): Promise<string> => {
- const text = await remoteFile.readTail(ref, REMOTE_TRANSCRIPT_CAP)
+ const readRemoteTranscript = async (
+ sessionId: string,
+ ref: RemoteFileRef,
+ cap: number = REMOTE_TRANSCRIPT_CAP
+ ): Promise<string> => {
+ const text = await remoteFile.readTail(ref, cap)
if (!text && locatedTranscriptSessions.delete(sessionId)) {
remoteTranscriptBySession.delete(sessionId)
}
@@ -1919,7 +1928,11 @@ app.whenReady().then(async () => {
conn,
controlPath
})
- return ref ? await readRemoteTranscript(sessionId, ref) : null
+ // A small tail, NOT the read path's 5 MB cap: the verdict is about the last few records,
+ // and a wider window only lets an ancient unmatched tool_use pin the node at `working`.
+ return ref
+ ? await readRemoteTranscript(sessionId, ref, RESYNC_TRANSCRIPT_TAIL_BYTES)
+ : null
},
emit: emitAgentStatus
}).catch(() => {
diff --git a/src/main/remote-ssh/agent-resync-wiring.test.ts b/src/main/remote-ssh/agent-resync-wiring.test.ts
new file mode 100644
index 000000000..7498e3971
--- /dev/null
+++ b/src/main/remote-ssh/agent-resync-wiring.test.ts
@@ -0,0 +1,51 @@
+// Structural guard on the ONE thing about the resync that lives in `src/main/index.ts`: which byte
+// cap its transcript dep asks for.
+//
+// index.ts is the Electron boot file — it imports `electron` and wires everything inside one big
+// closure, so there is no way to call `readRemoteTranscript` from a test. The property still has to
+// be pinned, because getting it wrong is silent: the resync keeps working, it just stops repairing
+// long sessions (a stale `tool_use` earlier in a 5 MB window pins the verdict at `working`). So we
+// assert it the same way `core/no-electron.test.ts` asserts its boundary — over the source.
+
+import { describe, it, expect } from 'vitest'
+import fs from 'fs'
+import path from 'path'
+import { RESYNC_TRANSCRIPT_TAIL_BYTES } from './agent-resync'
+
+const src = fs.readFileSync(path.resolve(__dirname, '..', 'index.ts'), 'utf8')
+
+/** Every `readRemoteTranscript(...)` call site, as its raw argument list. */
+function callArgs(): string[][] {
+ return [...src.matchAll(/await readRemoteTranscript\(([^)]*)\)/g)].map((m) =>
+ m[1]
+ .split(',')
+ .map((a) => a.trim())
+ .filter(Boolean)
+ )
+}
+
+describe('resync transcript read wiring (src/main/index.ts)', () => {
+ it('the resync dep asks for the small tail, not the 5 MB read cap', () => {
+ const dep = src.indexOf('readTranscriptTail: async (nodeId, sessionId)')
+ expect(dep, 'the resync readTranscriptTail dep must still exist').toBeGreaterThan(-1)
+ const body = src.slice(dep, dep + 900)
+ expect(body).toMatch(/readRemoteTranscript\([^)]*RESYNC_TRANSCRIPT_TAIL_BYTES\s*\)/)
+ })
+
+ it('exactly one call site overrides the cap — every other reader keeps the 5 MB default', () => {
+ const calls = callArgs()
+ expect(calls.length).toBeGreaterThanOrEqual(2) // the ⌘M/search read channel + the resync
+ const capped = calls.filter((a) => a.length > 2)
+ expect(capped).toEqual([['sessionId', 'ref', 'RESYNC_TRANSCRIPT_TAIL_BYTES']])
+ // The transcript IPC leg (chat panel + find bar) must still read a whole transcript.
+ expect(calls.filter((a) => a.length === 2).length).toBe(calls.length - 1)
+ })
+
+ it('the cap parameter defaults to the existing full-read cap', () => {
+ expect(src).toMatch(/cap: number = REMOTE_TRANSCRIPT_CAP/)
+ const m = src.match(/const REMOTE_TRANSCRIPT_CAP = (\d+) \* (\d+) \* (\d+)/)
+ expect(m, 'REMOTE_TRANSCRIPT_CAP must still be the full-read cap').toBeTruthy()
+ const full = Number(m![1]) * Number(m![2]) * Number(m![3])
+ expect(RESYNC_TRANSCRIPT_TAIL_BYTES).toBeLessThan(full)
+ })
+})
diff --git a/src/main/remote-ssh/agent-resync.test.ts b/src/main/remote-ssh/agent-resync.test.ts
index 7fa6d6eee..0d223ff79 100644
--- a/src/main/remote-ssh/agent-resync.test.ts
+++ b/src/main/remote-ssh/agent-resync.test.ts
@@ -1,5 +1,9 @@
import { describe, it, expect, vi } from 'vitest'
-import { resyncProjectAgents, type AgentResyncDeps } from './agent-resync'
+import {
+ resyncProjectAgents,
+ RESYNC_TRANSCRIPT_TAIL_BYTES,
+ type AgentResyncDeps
+} from './agent-resync'
import type { NormalizedAgentEvent } from '@shared/agents/normalize'
const assistantText = (text: string): string =>
@@ -168,6 +172,14 @@ describe('resyncProjectAgents', () => {
expect(d.emitted.map((e) => e.nodeId)).toEqual(['n1'])
})
+ it('reads the transcript only through a small tail window', () => {
+ // The read path's cap is 5 MB. `decideFromTranscriptTail` only ever tracks tool calls opened
+ // INSIDE the window it is given, so a wide window lets one stale unmatched `tool_use` from
+ // hours earlier pin the node at `working` forever — measured against real transcripts, that
+ // silently loses the repair on the longest sessions.
+ expect(RESYNC_TRANSCRIPT_TAIL_BYTES).toBe(64 * 1024)
+ })
+
it('handles several nodes independently', async () => {
const d = deps({
workingNodes: () => [
diff --git a/src/main/remote-ssh/agent-resync.ts b/src/main/remote-ssh/agent-resync.ts
index a53b1f2a8..4b2ba6fd6 100644
--- a/src/main/remote-ssh/agent-resync.ts
+++ b/src/main/remote-ssh/agent-resync.ts
@@ -14,6 +14,20 @@ import { sessionName } from '../../core/tmux-naming'
import type { NormalizedAgentEvent } from '@shared/agents/normalize'
import type { AgentId } from '@shared/agents/config'
+/**
+ * How much of a remote transcript the resync reads — a SMALL tail, deliberately.
+ *
+ * This is not a transcript to display, it is a verdict on the last few records, and
+ * `decideFromTranscriptTail` is built for exactly that: it only tracks tool calls opened INSIDE the
+ * window, so anything older cannot pin the verdict. Hand it the read path's 5 MB cap instead and
+ * that premise collapses — one stale unmatched `tool_use` from hours earlier in a long session
+ * reads as `working` forever, and the sessions with the biggest transcripts are precisely the ones
+ * whose badge the user most wants repaired. 64 KB comfortably holds the last few records (the
+ * title poll's TITLE_TAIL_BYTES is the same order of magnitude) while staying cheap enough to pull
+ * per working node, over a link that just proved flaky, on a watchdog that retries every 45s.
+ */
+export const RESYNC_TRANSCRIPT_TAIL_BYTES = 64 * 1024
+
export interface AgentResyncDeps {
/** Nodes the mirror still believes are working (agent-status-mirror.workingNodes). */
workingNodes: () => { nodeId: string; agentId?: string; sessionId?: string }[]