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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(messaging): wait for a held node to start, and queue a send to a fresh one
Fabricio Casali82a0381511bf
diff --git a/CLAUDE.md b/CLAUDE.mdindex 9d30ab5a3..b37acc3d2 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -2009,8 +2009,14 @@ When the owner is unproven AND no session exists AND this machine holds a launch(`WorkspaceStore.heldLaunch`, the machine-local `localExec` overlay), the outcome is`targetNotStarted` and the deliver-on-idle queue holds it; the flush re-runs every gate against thepane the spawn will have proven. A LIVE pane with no proven owner stays `unproven-target-owner` —-that refusal is the security property. The queue TTL still runs, so a project nobody opens expires-the message (sender told). Both shells wire `heldLaunch`.+that refusal is the security property. Such a message waits up to 24 hours+(`NOT_STARTED_TTL_MS` = `QUEUE_PERSIST_TTL_MAX`): the start waits for a person to open the project,+and the ordinary 5-minute TTL lost the message in the field (queued 19:12, expired 19:17, project+opened 19:27). It is not carried across an app restart (no session was recorded to bind it to), and+a node deleted before it starts keeps it until the TTL. A `targetStatusStale` target — a station+started a moment ago (`--run-now`, `run`) that has not posted its first hook — is queued too, with the+ordinary TTL: a retry cannot help before that hook, and its first verified `done` flushes the queue.+Both shells wire `heldLaunch`.**A board comment that @mentions a session is a message from a PERSON** (`@shared/board-comment`,`deliverBoardCommentFromUi` in `core/agents/agent-messaging.ts`). The comment composer's @ pickerdiff --git a/src/core/agents/agent-messaging.ts b/src/core/agents/agent-messaging.tsindex e42435f97..6b300df2d 100644--- a/src/core/agents/agent-messaging.ts+++ b/src/core/agents/agent-messaging.ts@@ -58,6 +58,7 @@ import {} from '../../shared/board-comment'import {DeliveryQueue,+ QUEUE_PERSIST_TTL_MAX,type DeliveryQueueDeps,type PersistedQueueEntry,type QueuedDeliveryRequest@@ -871,10 +872,19 @@ const QUEUE_ON_BUSY: ReadonlySet<AgentMessageOutcome['kind']> = new Set(['targetBusy','targetNotIdleUnknown',// Opened with its launch held (not on screen, no `--run-now`): flushed on its first idle after- // the launch lands. Its TTL runs as for any queued message, so a project nobody opens expires it.- 'targetNotStarted'+ // the launch lands, with the long TTL below — the start waits for a person to open the project.+ 'targetNotStarted',+ // Its session has a node identity but has not posted a verified status yet — in practice a CLI+ // started a moment ago (`--run-now`, `run`) that has not sent its first hook. A retry cannot+ // help until it does, and its first verified `done` is exactly what flushes the queue.+ 'targetStatusStale'])+/** How long a message to a target that has not STARTED waits (`targetNotStarted`). The start+ * waits for a person to open the project, which can be hours away; 5 minutes lost the message in+ * the field. The queue caps it at the longest TTL a restored entry may claim. */+const NOT_STARTED_TTL_MS = QUEUE_PERSIST_TTL_MAX+/*** One control-verb delivery, end to end, WITH deliver-on-idle: attempt it (`runDelivery`), and when* a queue is wired, enqueue a permitted-but-not-ready target instead of refusing it.@@ -915,7 +925,7 @@ async function deliverWithQueue(const queue = deps.queueif (queue) {const ident = requestIdentity(req)- const queued = (hibernated: boolean): Promise<AgentMessageOutcome> =>+ const queued = (hibernated: boolean, ttlMs?: number): Promise<AgentMessageOutcome> =>queue.enqueue({...req,@@ -925,9 +935,12 @@ async function deliverWithQueue(sourceTitle: ident.sourceTitle,body: ident.body},- { hibernated }+ { hibernated, ...(ttlMs !== undefined ? { ttlMs } : {}) }+ )+ if (QUEUE_ON_BUSY.has(outcome.kind))+ return answer(+ await queued(false, outcome.kind === 'targetNotStarted' ? NOT_STARTED_TTL_MS : undefined))- if (QUEUE_ON_BUSY.has(outcome.kind)) return answer(await queued(false))if (req.verb === 'board-comment' && BOARD_QUEUE_ON.has(outcome.kind)) {const held = await queued(false)// Held by the pair window, not by the target's turn: nothing will report "idle" when thediff --git a/src/core/agents/delivery-queue.ts b/src/core/agents/delivery-queue.tsindex 3a4d81e1c..f8e0f4eaf 100644--- a/src/core/agents/delivery-queue.ts+++ b/src/core/agents/delivery-queue.ts@@ -225,7 +225,7 @@ function reducedRequest(req: QueuedDeliveryRequest): QueuedDeliveryRequest {/** At most this many entries on disk. `DELIVERY_QUEUE_CAPACITY` per target bounds it in practice. */export const QUEUE_PERSIST_MAX = 1024/** The longest TTL a restored entry may claim — a hand-edited `ttlMs` must not keep one forever. */-const QUEUE_PERSIST_TTL_MAX = 24 * 60 * 60 * 1000+export const QUEUE_PERSIST_TTL_MAX = 24 * 60 * 60 * 1000/** The verbs a restored entry may still deliver. See the header: a board comment and a station* notice are expired at restore instead. */@@ -362,7 +362,7 @@ export class DeliveryQueue {*/async enqueue(req: QueuedDeliveryRequest,- opts: { hibernated?: boolean } = {}+ opts: { hibernated?: boolean; ttlMs?: number } = {}): Promise<Extract<AgentMessageOutcome, { kind: 'queued' } | { kind: 'queueFull' }>> {@@ -380,12 +380,15 @@ export class DeliveryQueue {bodyChars: req.body.length}, req)const binding = this.deps.bindingOf?.(req.targetNodeId)+ // A caller may ask for a longer wait than the queue's default (a target that has not STARTED+ // yet waits for a person to open its project), bounded by what a restored entry may claim.+ const ttlMs = Math.min(opts.ttlMs ?? this.ttlMs, QUEUE_PERSIST_TTL_MAX)const entry: QueueEntry = {req,enqueuedAt: now,- ttlMs: this.ttlMs,+ ttlMs,queuedTraceId: t.traceId,- cancelTimer: this.schedule(this.ttlMs, () => void this.expire(req.targetNodeId, entry)),+ cancelTimer: this.schedule(ttlMs, () => void this.expire(req.targetNodeId, entry)),...(binding ? { binding: { ...binding } } : {})}list.push(entry)@@ -396,7 +399,7 @@ export class DeliveryQueue {// event, not on the wake. A busy (non-hibernated) target needs nothing — it will go idle on its// own turn end.if (opts.hibernated) this.deps.wake?.(req.targetNodeId)- return { kind: 'queued', traceId: t.traceId, position: list.length, ttlMs: this.ttlMs }+ return { kind: 'queued', traceId: t.traceId, position: list.length, ttlMs }}/**diff --git a/src/core/canvas-control-core.ts b/src/core/canvas-control-core.tsindex 476b6cbef..df02df1ac 100644--- a/src/core/canvas-control-core.ts+++ b/src/core/canvas-control-core.ts@@ -873,10 +873,11 @@ export function buildCanvasControlInstructions(shimPath: string): string {' and do not report the session as started. `--cwd`/`--count`/`--group`/`--after`/`--prompt`',' all still apply. If your project is CLOSED the node is still saved into it and the reply',' says the project is closed; the tab is not reopened for you.',- ' A cold-opened node has NO session yet, so a `send` to it cannot land: it is queued and flushed',- ' after the node starts and finishes its first turn (its TTL still runs, so a project nobody opens',- ' expires it; `targetNotStarted` when it cannot be queued). To coordinate with a station now,',- ' open it with `--run-now` or start it with `run --node <id>`.',+ ' A cold-opened node has NO session yet, so a `send` to it cannot land: it is queued for up to',+ ' 24 hours and flushed after the node starts and finishes its first turn (`targetNotStarted` when',+ ' it cannot be queued; a message queued before the node started does not survive an app restart).',+ ' To coordinate with a station now, open it with `--run-now` or start it with `run --node <id>`.',+ ' A station started a moment ago that has not reported its status yet is queued the same way.',' Add `--run-now` to start a cold-opened session immediately instead. Put it LAST on the line,',' in either form (`--run-now` or `--run-now=1`): an older shim can still sit on an SSH host (it',' is rewritten only on connect), and it takes the token after any flag as that flag\'s value,',diff --git a/src/main/agent-messaging-stale-queue.test.ts b/src/main/agent-messaging-stale-queue.test.tsnew file mode 100644index 000000000..0e42e7b84--- /dev/null+++ b/src/main/agent-messaging-stale-queue.test.ts@@ -0,0 +1,56 @@+/**+ * A station started a moment ago (`--run-now`, `run`) has a node identity but has not posted a+ * verified status yet, so a `send` right after the start answers `targetStatusStale`. A retry+ * cannot help until its first hook lands, and its first verified `done` is what flushes the queue,+ * so the message is queued instead of refused.+ */+import { describe, it, expect, vi } from 'vitest'++vi.mock('../core/agents/node-token-files', async (orig) => ({+ ...(await orig<typeof import('../core/agents/node-token-files')>()),+ nodeTokenFilePresent: () => true+}))++import { deliverFromControl, createDeliveryQueue, type AgentMessagingDeps } from '../core/agents/agent-messaging'+import { DELIVERY_QUEUE_TTL_MS } from '../core/agents/delivery-queue'++const deps = (): AgentMessagingDeps => {+ const projects = () => [+ { id: 'p1', nodes: [{ id: 'a1', title: 'A', agentId: 'claude' }, { id: 'b1', title: 'B', agentId: 'claude' }] }+ ]+ return {+ paneOwner: async () => ({ tty: '/dev/pts/9', panePid: 1, paneId: '%1', command: 'claude', argv: ['claude'], pids: [2] }),+ sendEnvelope: async () => true,+ hasLiveSession: () => true,+ // Never seen: no hook has arrived from the fresh session yet.+ mirrorEntry: () => undefined,+ projects,+ isRemoteNode: () => false,+ messagingEnabled: () => true,+ paneOwnerProject: () => 'p1',+ customAgents: () => undefined,+ appendBoardLog: async () => false,+ now: () => 1_000_000+ }+}++describe('a freshly started station that has not reported yet', () => {+ it('is refused targetStatusStale without a queue', async () => {+ const { outcome } = await deliverFromControl(+ { verb: 'send', sourceNodeId: 'a1', targetNodeId: 'b1', body: 'hi' } as never,+ deps()+ )+ expect(outcome.kind).toBe('targetStatusStale')+ })++ it('is queued with the ordinary TTL when a queue is wired', async () => {+ const d = deps()+ d.queue = createDeliveryQueue(d, { schedule: () => () => {} })+ const { outcome } = await deliverFromControl(+ { verb: 'send', sourceNodeId: 'a1', targetNodeId: 'b1', body: 'hi' } as never,+ d+ )+ expect(outcome.kind).toBe('queued')+ expect(outcome.kind === 'queued' && outcome.ttlMs).toBe(DELIVERY_QUEUE_TTL_MS)+ })+})diff --git a/src/main/agent-messaging.test.ts b/src/main/agent-messaging.test.tsindex b485c6f25..a2aaa10bb 100644--- a/src/main/agent-messaging.test.ts+++ b/src/main/agent-messaging.test.ts@@ -283,6 +283,7 @@ describe('renderMessageOutcome', () => {{ kind: 'targetNotAgentPane', observed: 'bash' },{ kind: 'targetNotPasteAware' },{ kind: 'targetGone' },+ { kind: 'targetNotStarted' },{ kind: 'notPermitted', reason: 'switch-off' }]for (const o of samples) {@@ -538,10 +539,19 @@ describe('a target that has not started yet (launch held off screen)', () => {it('is queued, then delivered once the spawn proves ownership', async () => {const { deps, start } = unstarted()- const queue = createDeliveryQueue(deps, { schedule: () => () => {} })+ const waits: number[] = []+ const queue = createDeliveryQueue(deps, {+ schedule: (ms) => {+ waits.push(ms)+ return () => {}+ }+ })deps.queue = queueconst { outcome } = await deliverFromControl(req(), deps)expect(outcome.kind).toBe('queued')+ // The start waits for a person to open the project: 5 minutes lost the message in the field.+ expect(outcome.kind === 'queued' && outcome.ttlMs).toBe(24 * 60 * 60 * 1000)+ expect(waits).toEqual([24 * 60 * 60 * 1000])expect(deps.rec.sent).toHaveLength(0)start()await queue.onTargetIdle('b1')