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.

feat(messaging): refuse self-sends and cross-project targets, resolved off the store

Claude Code8fc078376f1b
diff --git a/src/core/agents/agent-message-scope.test.ts b/src/core/agents/agent-message-scope.test.ts
new file mode 100644
index 000000000..1db4237fc
--- /dev/null
+++ b/src/core/agents/agent-message-scope.test.ts
@@ -0,0 +1,176 @@
+import { describe, it, expect } from 'vitest'
+import { resolveDeliveryScope, scopeRefusal, type ScopeProject } from './agent-message-scope'
+import { decideDelivery, decidePreProbe, RETRYABLE } from './agent-message-decide'
+import { MANAGED_SCRIPT_REVISION } from './hooks/managed-script'
+import type { DeliveryFacts } from './agent-message-decide'
+
+/** Two projects, both with sessions running — the store holds every open AND closed project. */
+const projects: ScopeProject[] = [
+ { id: 'p-alpha', nodes: [{ id: 'a-1' }, { id: 'a-2' }, { id: 'a-3' }] },
+ { id: 'p-beta', nodes: [{ id: 'b-1' }, { id: 'b-2' }] }
+]
+
+/** Every other gate passing, so the only thing a refusal can be attributed to is the scope. */
+const facts = (over: Partial<DeliveryFacts> = {}): DeliveryFacts => ({
+ targetLive: true,
+ pane: 'agent',
+ target: { state: 'done', updatedAt: 1000, stateVerified: true, clientRevision: MANAGED_SCRIPT_REVISION },
+ tokenFilePresent: true,
+ pasteAware: true,
+ ...over
+})
+
+describe('resolveDeliveryScope', () => {
+ it('admits two nodes in the same project, and names it', () => {
+ expect(resolveDeliveryScope(projects, 'a-1', 'a-2')).toEqual({
+ kind: 'same-project',
+ projectId: 'p-alpha'
+ })
+ expect(resolveDeliveryScope(projects, 'b-1', 'b-2')).toEqual({
+ kind: 'same-project',
+ projectId: 'p-beta'
+ })
+ })
+
+ it('refuses a target in ANOTHER project, and says the target exists', () => {
+ expect(resolveDeliveryScope(projects, 'a-1', 'b-1')).toEqual({
+ kind: 'refused',
+ reason: 'cross-project',
+ targetFound: true
+ })
+ })
+
+ it('refuses a target that resolves nowhere, and says so', () => {
+ // Same refusal, different fact behind it: `targetFound: false` is what separates "aimed at
+ // another project" from "aimed at nothing", and only one of those is worth a human's time.
+ expect(resolveDeliveryScope(projects, 'a-1', 'ghost')).toEqual({
+ kind: 'refused',
+ reason: 'cross-project',
+ targetFound: false
+ })
+ })
+
+ it('refuses when the SOURCE belongs to no project the store knows — fail closed', () => {
+ // The thing being authorised is a write into someone else's terminal. Anything that cannot be
+ // positively resolved to one shared project is refused; there is no benefit of the doubt.
+ expect(resolveDeliveryScope(projects, 'ghost', 'a-1')).toEqual({
+ kind: 'refused',
+ reason: 'cross-project',
+ targetFound: true
+ })
+ expect(resolveDeliveryScope([], 'a-1', 'a-2')).toEqual({
+ kind: 'refused',
+ reason: 'cross-project',
+ targetFound: false
+ })
+ })
+
+ it('refuses a self-send wherever the id lives, or does not', () => {
+ expect(resolveDeliveryScope(projects, 'a-1', 'a-1')).toEqual({
+ kind: 'refused',
+ reason: 'self-send',
+ targetFound: true
+ })
+ expect(resolveDeliveryScope(projects, 'ghost', 'ghost')).toEqual({
+ kind: 'refused',
+ reason: 'self-send',
+ targetFound: false
+ })
+ })
+
+ it('a node the LIVE canvas has but the store does not is refused, not admitted', () => {
+ // The guarantee behind "resolved against the serialized store, not `nodesRef`": there is no
+ // parameter here for a live node list, so a node that exists only on the live canvas is simply
+ // absent — and absent fails closed. A freshly created node is not a verified idle agent yet
+ // either, so nothing deliverable is lost.
+ const justCreatedOnCanvas = 'a-9'
+ expect(resolveDeliveryScope(projects, 'a-1', justCreatedOnCanvas)).toEqual({
+ kind: 'refused',
+ reason: 'cross-project',
+ targetFound: false
+ })
+ })
+
+ it('resolves a DUPLICATE node id to the sender’s own project', () => {
+ // A cloned `project.json` gives two projects the same node id — this repo has shipped that bug
+ // once. The sender addresses the node it can see, and the ambiguity never reaches a pane.
+ const cloned: ScopeProject[] = [
+ { id: 'p-alpha', nodes: [{ id: 'a-1' }, { id: 'dup' }] },
+ { id: 'p-clone', nodes: [{ id: 'c-1' }, { id: 'dup' }] }
+ ]
+ expect(resolveDeliveryScope(cloned, 'a-1', 'dup')).toEqual({
+ kind: 'same-project',
+ projectId: 'p-alpha'
+ })
+ expect(resolveDeliveryScope(cloned, 'c-1', 'dup')).toEqual({
+ kind: 'same-project',
+ projectId: 'p-clone'
+ })
+ })
+
+ it('does not care whether the project tab is closed — a delivery goes to a pane', () => {
+ // A closed project's tmux sessions keep running and are re-adopted on the next start; that is
+ // the premise `routeControlSource` is built on. Filtering closed projects out would refuse a
+ // perfectly deliverable message on the grounds that a tab is shut.
+ const closed = [{ id: 'p-alpha', closed: true, nodes: [{ id: 'a-1' }, { id: 'a-2' }] }]
+ expect(resolveDeliveryScope(closed, 'a-1', 'a-2')).toEqual({
+ kind: 'same-project',
+ projectId: 'p-alpha'
+ })
+ })
+
+ it('ids are matched exactly — a case-folded near-miss is not the same node', () => {
+ expect(resolveDeliveryScope(projects, 'a-1', 'A-2').kind).toBe('refused')
+ })
+})
+
+describe('scopeRefusal feeds decideDelivery, and the refusal is terminal', () => {
+ it('a cross-project target is notPermitted{cross-project} and is NOT retryable', () => {
+ const scope = resolveDeliveryScope(projects, 'a-1', 'b-1')
+ const outcome = decideDelivery(facts({ notPermitted: scopeRefusal(scope) }))
+ expect(outcome).toEqual({ kind: 'notPermitted', reason: 'cross-project' })
+ expect(RETRYABLE.notPermitted).toBe(false)
+ })
+
+ it('a same-project target contributes no refusal at all', () => {
+ const scope = resolveDeliveryScope(projects, 'a-1', 'a-2')
+ expect(scopeRefusal(scope)).toBeUndefined()
+ expect(decideDelivery(facts({ notPermitted: scopeRefusal(scope) })).kind).toBe('proceed')
+ })
+
+ it('the refusal is decided PRE-PROBE, with no pane fact at all', () => {
+ const scope = resolveDeliveryScope(projects, 'a-1', 'b-1')
+ expect(
+ decidePreProbe({
+ targetLive: true,
+ tokenFilePresent: true,
+ target: undefined,
+ notPermitted: scopeRefusal(scope)
+ })
+ ).toEqual({ kind: 'notPermitted', reason: 'cross-project' })
+ })
+})
+
+describe('the self-send guard that already exists in the decider (PR #207) still holds', () => {
+ it('refuses a self-send even when the caller passes no scope verdict at all', () => {
+ // Verified, not re-implemented: `decidePreProbe` carries its own backstop so that no future
+ // caller — PR 7's deliver-on-idle queue above all — can forget the outer guard and let a node
+ // message itself in a loop with a rate limiter for a brake.
+ expect(decideDelivery(facts({ sourceNodeId: 'a-1', targetNodeId: 'a-1' }))).toEqual({
+ kind: 'notPermitted',
+ reason: 'self-send'
+ })
+ })
+
+ it('the two guards agree on the word, so a trace cannot depend on which one fired', () => {
+ const outer = scopeRefusal(resolveDeliveryScope(projects, 'a-1', 'a-1'))
+ const inner = decideDelivery(facts({ sourceNodeId: 'a-1', targetNodeId: 'a-1' }))
+ expect(inner).toEqual({ kind: 'notPermitted', reason: outer })
+ })
+
+ it('the backstop is ahead of the rate limiter, so a self-send is never reported as a wait', () => {
+ expect(
+ decideDelivery(facts({ sourceNodeId: 'a-1', targetNodeId: 'a-1', retryAfterMs: 9999 })).kind
+ ).toBe('notPermitted')
+ })
+})
diff --git a/src/core/agents/agent-message-scope.ts b/src/core/agents/agent-message-scope.ts
new file mode 100644
index 000000000..416dafd0a
--- /dev/null
+++ b/src/core/agents/agent-message-scope.ts
@@ -0,0 +1,102 @@
+import type { NotPermittedReason } from './agent-message-decide'
+
+/**
+ * WHO MAY BE ADDRESSED — the project boundary, resolved off the SERIALIZED store.
+ *
+ * A message crosses into another agent's session, so the question "is this target mine to write
+ * to?" has to be answered before anything else happens, and it has to be answered from a source of
+ * truth that covers every project rather than the one that happens to be on screen.
+ *
+ * ── WHY THE SERIALIZED STORE AND NOT THE LIVE CANVAS ────────────────────────────────────────────
+ *
+ * React Flow only ever holds the ACTIVE project's nodes (`nodesRef`), while the tmux sessions of
+ * every other open project keep running. Resolving a target against `nodesRef` therefore answers
+ * "not on the canvas" for every node outside the current tab — the same defect `routeControlSource`
+ * exists to fix on the source side, where it surfaced as a lost capability rather than as the wrong
+ * canvas answering.
+ *
+ * The fix is NOT to travel to the target's project. `send` is declared outside `needsLiveCanvas`
+ * precisely so it never can: travelling would yank the human's view to another tab on a background
+ * agent's say-so, and `setActive` on the way would clear that node's unread badge — a delivery
+ * silently erasing the signal a human relies on (G5). So this resolver takes the projects store and
+ * NOTHING else. There is no parameter for a live node list, which is a guarantee a test can check
+ * by running it: a node the live canvas has but the store has not is refused, not admitted.
+ *
+ * ── THREE SURFACES ──────────────────────────────────────────────────────────────────────────────
+ *
+ * - **Desktop (Electron):** the caller. It passes `useProjects.getState().projects`, the same
+ * serialized array `routeControlSource` and `storedNodeListing` already read.
+ * - **Server Edition:** ships, unused — messaging does not exist there (Task 5.3). Pure, with no
+ * electron or main-shell import, so it ships on both shells like everything else in `src/core`.
+ * - **Mobile (phone):** never a sender, so it never calls this. A phone-spawned node IS a valid
+ * target and is resolved exactly like any other: it is a node id in a persisted project.
+ *
+ * ── WHICH WAY IT FAILS ──────────────────────────────────────────────────────────────────────────
+ *
+ * CLOSED, because the thing being authorised is a write into someone else's terminal. Anything this
+ * cannot positively resolve to a single shared project is refused: a source that belongs to no
+ * project the store knows, a target that resolves nowhere, a target in a project the store has but
+ * the sender does not share. `notPermitted` is not retryable, so a refusal here is terminal — which
+ * is correct for a boundary and would be wrong for a timing problem, and none of these are timing
+ * problems.
+ */
+
+/** The little this needs from a project — a structural subset of both `Project` and the store's
+ * serialized `ProjectFileV1`, so either can be passed without adaptation. */
+export interface ScopeProject {
+ id: string
+ nodes: readonly { id: string }[]
+}
+
+export type ScopeRefusal = Extract<NotPermittedReason, 'self-send' | 'cross-project'>
+
+export type DeliveryScope =
+ | { kind: 'same-project'; projectId: string }
+ | {
+ kind: 'refused'
+ reason: ScopeRefusal
+ /** Does this target id exist in ANY project the store knows? `false` separates "aimed at
+ * another project" from "aimed at nothing" — the same refusal, but only one of them is
+ * worth telling a human about. */
+ targetFound: boolean
+ }
+
+/**
+ * Resolve a (source, target) pair to the project they share, or to the refusal that stops it.
+ *
+ * `self-send` is checked first and is the same rule, in the same words, that the manual
+ * `onConnect` already applies to a canvas edge — a node may not be wired to itself. It is decided
+ * before project membership because it is true regardless of where either id lives, including when
+ * neither lives anywhere.
+ *
+ * Note that this is the OUTER guard, not the only one: `decidePreProbe` carries its own self-send
+ * backstop so that no future caller can forget this one. Both produce the identical
+ * `notPermitted{reason:'self-send'}`, which the test asserts by running them.
+ *
+ * A duplicate node id across two projects (a cloned `project.json` — this repo has shipped that
+ * bug once) resolves to the SOURCE's project when the source's project is one of them: the sender
+ * addresses the node it can see, and the ambiguity never reaches the pane.
+ *
+ * `closed` and `unavailable` projects are deliberately NOT filtered out. A closed project's tmux
+ * sessions keep running and are re-adopted on the next start — that is the whole premise of
+ * `routeControlSource` — and a delivery goes to a pane, not to a canvas. Filtering them would
+ * refuse a perfectly deliverable message on the grounds that a tab is shut.
+ */
+export function resolveDeliveryScope(
+ projects: readonly ScopeProject[],
+ sourceNodeId: string,
+ targetNodeId: string
+): DeliveryScope {
+ const has = (p: ScopeProject, id: string): boolean => p.nodes.some((n) => n.id === id)
+ const targetFound = projects.some((p) => has(p, targetNodeId))
+ if (sourceNodeId === targetNodeId) return { kind: 'refused', reason: 'self-send', targetFound }
+ const owner = projects.find((p) => has(p, sourceNodeId))
+ if (owner && has(owner, targetNodeId)) return { kind: 'same-project', projectId: owner.id }
+ return { kind: 'refused', reason: 'cross-project', targetFound }
+}
+
+/** The `notPermitted` fact `decideDelivery` takes, or `undefined` when the pair may proceed.
+ * Keeping the mapping here means a caller cannot invent a third answer. */
+export function scopeRefusal(scope: DeliveryScope): NotPermittedReason | undefined {
+ return scope.kind === 'refused' ? scope.reason : undefined
+}
diff --git a/src/main/canvas-control-core.test.ts b/src/main/canvas-control-core.test.ts
index 14e994998..60f51e3eb 100644
--- a/src/main/canvas-control-core.test.ts
+++ b/src/main/canvas-control-core.test.ts
@@ -234,3 +234,24 @@ describe('the strict identity bucket is pre-positioned, not live', () => {
expect(STRICT_CONTROL_VERBS.has('open-browser')).toBe(false)
})
})
+
+/**
+ * The same pre-positioning, for messaging. `needsLiveCanvas` already declares `send`/`reply` as
+ * store-answered so that a delivery can never travel the camera — a routing decision made once,
+ * ahead of the dispatch that will consume it.
+ *
+ * This test is what stops "it ships inert" from becoming a false claim: it FAILS on the day the
+ * verbs land, which is exactly when the PR body has to stop saying nothing changes for anyone —
+ * and when Canvas's store-answered branch, which today only knows how to answer `list`, has to
+ * learn to deliver instead of replying with a node listing.
+ */
+describe('the messaging verbs are declared but do not exist yet', () => {
+ it('send and reply are refused by the parser, so nothing routes them today', () => {
+ expect(parseControlRequest('send', { node: 'n-1', text: 'hi' })).toEqual({
+ error: 'Unknown verb: send'
+ })
+ expect(parseControlRequest('reply', { node: 'n-1', text: 'hi' })).toEqual({
+ error: 'Unknown verb: reply'
+ })
+ })
+})
diff --git a/src/renderer/lib/controlRouting.test.ts b/src/renderer/lib/controlRouting.test.ts
index f73853c79..105024598 100644
--- a/src/renderer/lib/controlRouting.test.ts
+++ b/src/renderer/lib/controlRouting.test.ts
@@ -72,6 +72,14 @@ describe('needsLiveCanvas', () => {
expect(needsLiveCanvas(verb)).toBe(true)
}
})
+
+ it('is false for send and reply — a delivery must never travel the camera', () => {
+ // Travelling to the target's project would hijack the human's view on a background agent's
+ // say-so, and the `setActive` on the way would clear that node's unread badge — a message
+ // silently erasing the signal a human relies on. A delivery goes to a pane; it needs no canvas.
+ expect(needsLiveCanvas('send')).toBe(false)
+ expect(needsLiveCanvas('reply')).toBe(false)
+ })
})
describe('sourceIsControlCapable', () => {
diff --git a/src/renderer/lib/controlRouting.ts b/src/renderer/lib/controlRouting.ts
index 927e840df..e0388b598 100644
--- a/src/renderer/lib/controlRouting.ts
+++ b/src/renderer/lib/controlRouting.ts
@@ -65,14 +65,33 @@ export function routeControlSource(
return travel
}
+/**
+ * Verbs that are answered from the SERIALIZED store instead of the live canvas.
+ *
+ * `list` reads names only, and it is the verb an agent calls most — answering it out of the store
+ * keeps a background agent's polling from yanking the user's view to another project tab on every
+ * call.
+ *
+ * `send`/`reply` are here for a stronger reason than politeness. A delivery goes to a tmux PANE,
+ * not to a canvas, so travelling to the target's project buys nothing — and it costs twice: it
+ * hijacks the human's view on a background agent's say-so, and the `setActive` on the way clears
+ * that node's unread badge, so a message silently erases the signal a human relies on. The target
+ * is therefore resolved off the store (`resolveDeliveryScope`), which is also the only source of
+ * truth that can see nodes outside the active tab at all.
+ *
+ * INERT AS OF THIS COMMIT: neither verb is in `ControlVerb` yet, so `parseControlRequest` refuses
+ * both before any of this is reached (asserted by running it, in `canvas-control-core.test.ts`).
+ * The declaration lands first so the routing decision is made once, here, rather than inside the
+ * dispatch that Task 5.1 adds.
+ */
+const STORE_ANSWERED_VERBS: ReadonlySet<string> = new Set(['list', 'send', 'reply'])
+
/**
* Does this verb have to run against the LIVE canvas? Everything that creates, moves, writes to or
- * closes a node does. `list` reads names only, and it is the verb an agent calls most — answering
- * it out of the store keeps a background agent's polling from yanking the user's view to another
- * project tab on every call.
+ * closes a node does.
*/
export function needsLiveCanvas(verb: string): boolean {
- return verb !== 'list'
+ return !STORE_ANSWERED_VERBS.has(verb)
}
/**