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(canvas): pure helpers + note field for sticky context links

enes9bf37b71bfa7
diff --git a/src/renderer/lib/noteLink.test.ts b/src/renderer/lib/noteLink.test.ts
new file mode 100644
index 000000000..d513f4eb4
--- /dev/null
+++ b/src/renderer/lib/noteLink.test.ts
@@ -0,0 +1,80 @@
+import { describe, it, expect } from 'vitest'
+import { buildLinkMap, buildNotePushMessage, classifyLink } from './noteLink'
+
+const term = (contextCapable = false) => ({ kind: 'terminal', contextCapable })
+const sticky = () => ({ kind: 'sticky', contextCapable: false })
+
+describe('classifyLink', () => {
+ it('two context-capable terminals form a context link', () => {
+ expect(classifyLink(term(true), term(true))).toBe('context')
+ })
+ it('terminals that are not both context-capable form nothing', () => {
+ expect(classifyLink(term(true), term(false))).toBeNull()
+ expect(classifyLink(term(false), term(false))).toBeNull()
+ })
+ it('sticky ↔ terminal forms a note link in either direction', () => {
+ expect(classifyLink(sticky(), term(false))).toBe('note')
+ expect(classifyLink(term(true), sticky())).toBe('note')
+ })
+ it('sticky ↔ sticky and sticky ↔ non-terminal form nothing', () => {
+ expect(classifyLink(sticky(), sticky())).toBeNull()
+ expect(classifyLink(sticky(), { kind: 'editor', contextCapable: false })).toBeNull()
+ })
+})
+
+describe('buildNotePushMessage', () => {
+ it('wraps the note text with the nodeterm prefix and title', () => {
+ expect(buildNotePushMessage('Deploy notes', 'use the staging key')).toBe(
+ '[nodeterm] Sticky note "Deploy notes" linked as context: use the staging key'
+ )
+ })
+ it('returns null for empty or whitespace-only text', () => {
+ expect(buildNotePushMessage('T', '')).toBeNull()
+ expect(buildNotePushMessage('T', ' \n ')).toBeNull()
+ })
+ it('collapses newlines so the message stays single-line', () => {
+ const msg = buildNotePushMessage('T', 'line one\nline two\r\nline three')
+ expect(msg).toContain('line one ⏎ line two ⏎ line three')
+ expect(msg).not.toContain('\n')
+ })
+ it('truncates past 2000 chars and points at the skill', () => {
+ const msg = buildNotePushMessage('T', 'x'.repeat(3000))!
+ expect(msg.length).toBeLessThan(2200)
+ expect(msg).toContain('[truncated — read the full note with the get-linked-context skill]')
+ })
+})
+
+describe('buildLinkMap', () => {
+ const infoOf = (id: string) =>
+ id.startsWith('note')
+ ? { id, title: `Note ${id}`, note: `text of ${id}`, sticky: true }
+ : { id, title: `Term ${id}`, cwd: `/cwd/${id}`, sticky: false }
+
+ it('context edges map both directions with cwd', () => {
+ const map = buildLinkMap([{ source: 'a', target: 'b' }], infoOf)
+ expect(map).toEqual({
+ a: [{ id: 'b', title: 'Term b', cwd: '/cwd/b' }],
+ b: [{ id: 'a', title: 'Term a', cwd: '/cwd/a' }]
+ })
+ })
+ it('note edges map one direction only: terminal gets the note entry', () => {
+ const map = buildLinkMap(
+ [
+ { source: 'note1', target: 't1' },
+ { source: 't2', target: 'note1' }
+ ],
+ infoOf
+ )
+ expect(map).toEqual({
+ t1: [{ id: 'note1', title: 'Note note1', note: 'text of note1' }],
+ t2: [{ id: 'note1', title: 'Note note1', note: 'text of note1' }]
+ })
+ expect(map['note1']).toBeUndefined()
+ })
+ it('an empty sticky still yields an entry with empty note', () => {
+ const map = buildLinkMap([{ source: 'noteX', target: 't1' }], (id) =>
+ id === 'noteX' ? { id, title: 'N', sticky: true } : { id, title: 'T', cwd: '', sticky: false }
+ )
+ expect(map['t1']).toEqual([{ id: 'noteX', title: 'N', note: '' }])
+ })
+})
diff --git a/src/renderer/lib/noteLink.ts b/src/renderer/lib/noteLink.ts
new file mode 100644
index 000000000..3826fb4f6
--- /dev/null
+++ b/src/renderer/lib/noteLink.ts
@@ -0,0 +1,80 @@
+// Pure helpers for canvas link edges: classify a new connection (context link between two
+// Claude nodes vs. note link between a sticky and a terminal), build the one-shot push
+// message a note link injects into an agent session, and build the link map pushed to main.
+// Kept free of React/store imports so the connection matrix is unit-testable.
+import type { ContextLinkInfo, ContextLinkMap } from '@shared/types'
+
+export interface LinkEndpoint {
+ /** React Flow node type: 'terminal' | 'sticky' | 'editor' | … */
+ kind: string
+ /** Terminal node whose agent is CONTEXT_LINK_CAPABLE (Claude). */
+ contextCapable: boolean
+}
+
+export type LinkKind = 'context' | 'note'
+
+/** Decide what kind of link (if any) a new edge between two nodes forms. */
+export function classifyLink(a: LinkEndpoint, b: LinkEndpoint): LinkKind | null {
+ const stickies = (a.kind === 'sticky' ? 1 : 0) + (b.kind === 'sticky' ? 1 : 0)
+ if (stickies === 0) return a.contextCapable && b.contextCapable ? 'context' : null
+ if (stickies === 2) return null
+ const other = a.kind === 'sticky' ? b : a
+ return other.kind === 'terminal' ? 'note' : null
+}
+
+/** Longest note text pushed inline; longer notes are truncated with a pointer to the skill. */
+const NOTE_PUSH_MAX = 2000
+
+/**
+ * Build the one-shot message injected into an agent session when a note is linked.
+ * Single-line by construction: pty.sendText appends Enter and embedded newlines would act
+ * as submits in agent REPLs, so newlines are collapsed to a visible ' ⏎ '.
+ * Returns null when the note is empty (nothing to push).
+ */
+export function buildNotePushMessage(title: string, text: string): string | null {
+ if (!text.trim()) return null
+ const flat = text.replace(/\s*\r?\n\s*/g, ' ⏎ ').trim()
+ if (!flat) return null
+ const body =
+ flat.length > NOTE_PUSH_MAX
+ ? flat.slice(0, NOTE_PUSH_MAX) +
+ ' … [truncated — read the full note with the get-linked-context skill]'
+ : flat
+ return `[nodeterm] Sticky note "${title}" linked as context: ${body}`
+}
+
+export interface LinkNodeInfo {
+ id: string
+ title: string
+ cwd?: string
+ note?: string
+ sticky: boolean
+}
+
+/**
+ * Build the node → linked-nodes map pushed to main (which writes the per-node link files).
+ * Context edges map both directions; note edges map one direction only — the terminal side
+ * gets a { id, title, note } entry, the sticky side gets nothing (a sticky cannot read).
+ */
+export function buildLinkMap(
+ edges: Array<{ source: string; target: string }>,
+ infoOf: (id: string) => LinkNodeInfo
+): ContextLinkMap {
+ const map: ContextLinkMap = {}
+ const entryOf = (n: LinkNodeInfo): ContextLinkInfo =>
+ n.sticky ? { id: n.id, title: n.title, note: n.note ?? '' } : { id: n.id, title: n.title, cwd: n.cwd ?? '' }
+ for (const e of edges) {
+ const s = infoOf(e.source)
+ const t = infoOf(e.target)
+ if (s.sticky && t.sticky) continue
+ if (s.sticky) {
+ ;(map[t.id] ??= []).push(entryOf(s))
+ } else if (t.sticky) {
+ ;(map[s.id] ??= []).push(entryOf(t))
+ } else {
+ ;(map[s.id] ??= []).push(entryOf(t))
+ ;(map[t.id] ??= []).push(entryOf(s))
+ }
+ }
+ return map
+}
diff --git a/src/shared/types.ts b/src/shared/types.ts
index cb4abd083..d03381db1 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -582,6 +582,8 @@ export interface ContextLinkInfo {
title: string
/** The linked node's working dir — lets the CLI resolve a transcript when the path isn't known yet. */
cwd?: string
+ /** Set when the linked node is a sticky note: its current text. Note entries have no transcript/terminal. */
+ note?: string
}
/** Map of node id → the nodes it is context-linked to. Sent to main so it can write link files. */