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.

refactor(core): context/subagent tails take a send callback, move into src/core

enes15b4fac28791
diff --git a/src/main/context-tail.test.ts b/src/core/context-tail.test.ts
similarity index 96%
rename from src/main/context-tail.test.ts
rename to src/core/context-tail.test.ts
index 7c0c11f6d..1294ddc03 100644
--- a/src/main/context-tail.test.ts
+++ b/src/core/context-tail.test.ts
@@ -67,8 +67,8 @@ describe('createContextTail — task notifications', () => {
const line = notificationLine('tu-torn')
fs.writeFileSync(file, line.slice(0, 40)) // first half, no newline
const onTaskNotification = vi.fn()
- const win = { isDestroyed: () => false, webContents: { send: vi.fn() } } as never
- const tail = createContextTail(win, { onTaskNotification })
+ const send = vi.fn()
+ const tail = createContextTail(send, { onTaskNotification })
tail.track('s1', file)
await new Promise((r) => setTimeout(r, 1200)) // ≥1 poll sees the partial line
fs.appendFileSync(file, line.slice(40) + '\n')
diff --git a/src/main/context-tail.ts b/src/core/context-tail.ts
similarity index 96%
rename from src/main/context-tail.ts
rename to src/core/context-tail.ts
index 108e48bde..bf8819813 100644
--- a/src/main/context-tail.ts
+++ b/src/core/context-tail.ts
@@ -3,10 +3,8 @@
// offset-based read + shared-interval pattern of subagent-tail.ts. Pushed to the renderer
// as ContextWindowUsage keyed by sessionId.
import fs from 'fs'
-import { type BrowserWindow } from 'electron'
-import { IPC } from '../shared/ipc'
import type { ContextWindowUsage } from '../shared/types'
-import { cachedWindowFor, resolveModelWindow } from './model-window'
+import { cachedWindowFor, resolveModelWindow } from '../main/model-window'
import { splitCompleteLines } from './subagent-tail'
const POLL_MS = 1000
@@ -120,12 +118,14 @@ export interface ContextTail {
pathFor(sessionId: string | undefined): string | undefined
}
-export function createContextTail(win: BrowserWindow, opts?: ContextTailOptions): ContextTail {
+export function createContextTail(
+ send: (payload: unknown) => void,
+ opts?: ContextTailOptions
+): ContextTail {
const sessions = new Map<string, Tracked>()
let timer: ReturnType<typeof setInterval> | null = null
const push = (sessionId: string, t: Tracked): void => {
- if (win.isDestroyed()) return
const usedPercent = Math.min(100, Math.max(0, (t.used / t.window) * 100))
const payload: ContextWindowUsage = {
sessionId,
@@ -135,7 +135,7 @@ export function createContextTail(win: BrowserWindow, opts?: ContextTailOptions)
model: t.model,
updatedAt: Date.now()
}
- win.webContents.send(IPC.contextUpdate, payload)
+ send(payload)
}
// Read newly-appended transcript bytes (if any), reconcile the window from the model
diff --git a/src/main/subagent-tail.test.ts b/src/core/subagent-tail.test.ts
similarity index 90%
rename from src/main/subagent-tail.test.ts
rename to src/core/subagent-tail.test.ts
index b8dfdc8da..30bc20788 100644
--- a/src/main/subagent-tail.test.ts
+++ b/src/core/subagent-tail.test.ts
@@ -55,11 +55,6 @@ describe('splitCompleteLines', () => {
// --- createSubagentTail integration (real fs, 400ms tick) ---
-function fakeWin(): { win: never; send: ReturnType<typeof vi.fn> } {
- const send = vi.fn()
- return { win: { isDestroyed: () => false, webContents: { send } } as never, send }
-}
-
const wait = (ms: number): Promise<void> => new Promise((r) => setTimeout(r, ms))
function setup(): { transcriptPath: string; subDir: string } {
@@ -71,7 +66,7 @@ function setup(): { transcriptPath: string; subDir: string } {
}
const streamed = (send: ReturnType<typeof vi.fn>): string =>
- send.mock.calls.map((c) => (c[1] as { chunk: string }).chunk).join('')
+ send.mock.calls.map((c) => (c[0] as { chunk: string }).chunk).join('')
describe('createSubagentTail', () => {
it('does not drop a line written torn across two reads', async () => {
@@ -80,8 +75,8 @@ describe('createSubagentTail', () => {
const file = path.join(subDir, 'agent-1.jsonl')
const line = assistant('torn line survives')
fs.writeFileSync(file, line.slice(0, 25)) // first half of the line, no newline yet
- const { win, send } = fakeWin()
- const tail = createSubagentTail(win)
+ const send = vi.fn()
+ const tail = createSubagentTail(send)
tail.track('tu1', transcriptPath)
await wait(900) // two ticks land while the line is still half-written
fs.appendFileSync(file, line.slice(25) + '\n')
@@ -95,8 +90,8 @@ describe('createSubagentTail', () => {
const metaPath = path.join(subDir, 'agent-1.meta.json')
fs.writeFileSync(metaPath, '{}') // parseable but mid-write: toolUseId not there yet
fs.writeFileSync(path.join(subDir, 'agent-1.jsonl'), assistant('late meta') + '\n')
- const { win, send } = fakeWin()
- const tail = createSubagentTail(win)
+ const send = vi.fn()
+ const tail = createSubagentTail(send)
tail.track('tu1', transcriptPath)
await wait(600) // first tick parses {} — must not blacklist this meta
fs.writeFileSync(metaPath, JSON.stringify({ toolUseId: 'tu1' }))
@@ -109,8 +104,8 @@ describe('createSubagentTail', () => {
const { transcriptPath, subDir } = setup()
fs.writeFileSync(path.join(subDir, 'agent-1.meta.json'), JSON.stringify({ toolUseId: 'tu1' }))
fs.writeFileSync(path.join(subDir, 'agent-1.jsonl'), assistant('unterminated final line'))
- const { win, send } = fakeWin()
- const tail = createSubagentTail(win)
+ const send = vi.fn()
+ const tail = createSubagentTail(send)
tail.track('tu1', transcriptPath)
await wait(600) // read, but held back as a partial line
tail.finish('tu1')
diff --git a/src/main/subagent-tail.ts b/src/core/subagent-tail.ts
similarity index 95%
rename from src/main/subagent-tail.ts
rename to src/core/subagent-tail.ts
index ff9f35d7a..d3ae6ad3e 100644
--- a/src/main/subagent-tail.ts
+++ b/src/core/subagent-tail.ts
@@ -7,8 +7,6 @@
// All read-only — if Claude changes the format we just stream less (no crash).
import fs from 'fs'
import path from 'path'
-import { type BrowserWindow } from 'electron'
-import { IPC } from '../shared/ipc'
interface Tracked {
dir: string
@@ -133,12 +131,14 @@ export interface SubagentTail {
finish(toolUseId: string): void
}
-export function createSubagentTail(win: BrowserWindow): SubagentTail {
+export function createSubagentTail(
+ send: (payload: { toolUseId: string; chunk: string }) => void
+): SubagentTail {
const tracked = new Map<string, Tracked>()
let timer: ReturnType<typeof setInterval> | null = null
- const send = (toolUseId: string, chunk: string) => {
- if (chunk && !win.isDestroyed()) win.webContents.send(IPC.agentSubagentActivity, { toolUseId, chunk })
+ const emit = (toolUseId: string, chunk: string): void => {
+ if (chunk) send({ toolUseId, chunk })
}
// Async fs throughout: this ticks every 400ms per active subagent, and the sync version's
@@ -187,7 +187,7 @@ export function createSubagentTail(win: BrowserWindow): SubagentTail {
const { text, carry } = splitCompleteLines(data)
e.carry = carry
const out = formatSubagentChunk(text)
- if (out) send(toolUseId, out + '\n')
+ if (out) emit(toolUseId, out + '\n')
} catch {
// file may not exist yet / transient read error
} finally {
@@ -217,7 +217,7 @@ export function createSubagentTail(win: BrowserWindow): SubagentTail {
if (!e.carry?.length) return
const out = formatSubagentChunk(e.carry.toString('utf-8'))
e.carry = null
- if (out) send(toolUseId, out + '\n')
+ if (out) emit(toolUseId, out + '\n')
}
const e = tracked.get(toolUseId)
if (e) void readOne(toolUseId, e).then(() => flushCarry(e)) // final flush (completes well within the grace delay)
diff --git a/src/main/index.ts b/src/main/index.ts
index b9f793f39..fdd56c10f 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -17,8 +17,8 @@ import { setMainWindow, getMainWindow, sendToMain, shouldHideOnClose } from './m
import { initAgentStatusMirror, recordAgentEvent } from '../core/agent-status-mirror'
import { retainUntilDismissed } from './notifications'
import { installManagedAgentHooks } from '../core/agents/hooks'
-import { createSubagentTail } from './subagent-tail'
-import { createContextTail, type TaskNotification } from './context-tail'
+import { createSubagentTail } from '../core/subagent-tail'
+import { createContextTail, type TaskNotification } from '../core/context-tail'
import { isAsyncSubagentLaunch, type NormalizedAgentEvent } from '../shared/agents/normalize'
import {
readTranscriptLines,
@@ -446,7 +446,9 @@ app.whenReady().then(async () => {
// local HTTP server that receives hook posts and forwards normalized events to the renderer.
// A raw listener drives the transcript-tailing features (context meter + subagent transcript),
// which need the raw transcript_path the NormalizedAgentEvent intentionally drops.
- const subagentTail = createSubagentTail(win)
+ const subagentTail = createSubagentTail(({ toolUseId, chunk }) => {
+ if (!win.isDestroyed()) win.webContents.send(IPC.agentSubagentActivity, { toolUseId, chunk })
+ })
// Async subagents (Claude's default) end via a <task-notification> queued into the PARENT
// transcript — their PostToolUse is only a launch ack (see the raw listener below). The
// context tails already read that transcript, so they surface the notification here and we
@@ -469,7 +471,9 @@ app.whenReady().then(async () => {
remoteSubagentTail.untrack(n.toolUseId)
nodeSubagents.get(nodeId)?.delete(n.toolUseId)
}
- const contextTail = createContextTail(win, { onTaskNotification })
+ const contextTail = createContextTail((payload) => {
+ if (!win.isDestroyed()) win.webContents.send(IPC.contextUpdate, payload)
+ }, { onTaskNotification })
// Remote (SSH-project) counterparts: a node whose pty runs on a remote host has its Claude
// transcript on that host, so its meter / subagent transcript / search must read over the
// project's ControlMaster. One RemoteFile bound to the SSH-project manager's own ssh runner
diff --git a/src/main/remote-context-tail.ts b/src/main/remote-context-tail.ts
index 258fa15ba..31c2602d7 100644
--- a/src/main/remote-context-tail.ts
+++ b/src/main/remote-context-tail.ts
@@ -8,8 +8,8 @@ import { type BrowserWindow } from 'electron'
import { IPC } from '../shared/ipc'
import type { ContextWindowUsage } from '../shared/types'
import { cachedWindowFor, resolveModelWindow } from './model-window'
-import { parseLatestUsage, parseTaskNotifications, type ContextTailOptions } from './context-tail'
-import { splitCompleteLines } from './subagent-tail'
+import { parseLatestUsage, parseTaskNotifications, type ContextTailOptions } from '../core/context-tail'
+import { splitCompleteLines } from '../core/subagent-tail'
import type { RemoteFile, RemoteFileRef } from './remote-ssh/remote-file'
const POLL_MS = 1000
diff --git a/src/main/remote-subagent-tail.test.ts b/src/main/remote-subagent-tail.test.ts
index d51eb352e..f8e90c1df 100644
--- a/src/main/remote-subagent-tail.test.ts
+++ b/src/main/remote-subagent-tail.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from 'vitest'
import { createRemoteSubagentTail } from './remote-subagent-tail'
-import { formatSubagentChunk } from './subagent-tail'
+import { formatSubagentChunk } from '../core/subagent-tail'
import type { RemoteFileRef } from './remote-ssh/remote-file'
const ref: RemoteFileRef = { conn: { host: 'h', user: 'u' }, controlPath: '/s', path: '/abs/agent-1.jsonl' }
diff --git a/src/main/remote-subagent-tail.ts b/src/main/remote-subagent-tail.ts
index 562fc9ab7..cf2f15361 100644
--- a/src/main/remote-subagent-tail.ts
+++ b/src/main/remote-subagent-tail.ts
@@ -9,7 +9,7 @@
// listener already learned it), so each entry is tracked directly by its RemoteFileRef.
import { type BrowserWindow } from 'electron'
import { IPC } from '../shared/ipc'
-import { formatSubagentChunk, splitCompleteLines } from './subagent-tail'
+import { formatSubagentChunk, splitCompleteLines } from '../core/subagent-tail'
import type { RemoteFile, RemoteFileRef } from './remote-ssh/remote-file'
const POLL_MS = 1000