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(kanban): ask the host once per card and PR set for a merge-driven move

review2473ab806efb
diff --git a/src/renderer/components/kanban/usePullAutoMove.test.tsx b/src/renderer/components/kanban/usePullAutoMove.test.tsx
index 7e4cca01c..a86a5dd62 100644
--- a/src/renderer/components/kanban/usePullAutoMove.test.tsx
+++ b/src/renderer/components/kanban/usePullAutoMove.test.tsx
@@ -6,7 +6,14 @@ import type { ProjectKanban } from '@shared/types'
import type { GitHubPullBoard } from '@shared/github-pull-status'
import { useProjects } from '../../state/projects'
import { useSettings } from '../../state/settings'
-import { usePullAutoMove } from './usePullAutoMove'
+import { planPullAutoMoves } from '../../lib/pullAutoMove'
+import { autoMoveCardsSig, usePullAutoMove } from './usePullAutoMove'
+
+// The real planner, counted: a pass that re-plans an unchanged board is the chatter this pins.
+vi.mock('../../lib/pullAutoMove', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('../../lib/pullAutoMove')>()
+ return { ...actual, planPullAutoMoves: vi.fn(actual.planPullAutoMoves) }
+})
;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
@@ -21,8 +28,9 @@ const merged: GitHubPullBoard = {
observedAt: 1, stale: false, access: { ci: true, merge: true }, undecided: false, truncated: false
}
-/** The host's rules, like GitHubPullStatusTracker: a claim needs a wait note for this card, and the
- * first ask for a key wins. `seedWaits` stands in for notes recorded while the PR was open. */
+/** The host's rules, like GitHubPullStatusTracker: a claim needs a PR that has not moved this card
+ * yet and that the card was noted waiting on; claims are per PR. `seedWaits` stands in for notes
+ * recorded while the PR was open. */
function hostClaims(seedWaits: string[] = ['p1:card-1:12']) {
const claimed = new Set<string>()
const waits = new Set(seedWaits)
@@ -33,24 +41,33 @@ function hostClaims(seedWaits: string[] = ['p1:card-1:12']) {
return request.pulls.length
}),
claimPullAutoMove: vi.fn(async (request: { projectId: string; cardId: string; pulls: number[] }) => {
- const key = `${request.projectId}:${request.cardId}:${request.pulls.join(',')}`
- if (claimed.has(key)) return false
- if (!request.pulls.some((pull) => waits.has(`${request.projectId}:${request.cardId}:${pull}`))) return false
- claimed.add(key)
+ const key = (pull: number): string => `${request.projectId}:${request.cardId}:${pull}`
+ const fresh = request.pulls.filter((pull) => !claimed.has(key(pull)))
+ if (!fresh.some((pull) => waits.has(key(pull)))) return false
+ for (const pull of fresh) claimed.add(key(pull))
return true
})
}
}
function Probe(props: {
- api: ReturnType<typeof hostClaims>
+ api: Pick<ReturnType<typeof hostClaims>, 'claimPullAutoMove' | 'notePullWaits'>
pullBoard: GitHubPullBoard
onAutoMove: (...args: unknown[]) => void
+ cards?: typeof cards
+ board?: ProjectKanban
}): null {
- usePullAutoMove({ api: props.api, projectId: 'p1', cards, board, pullBoard: props.pullBoard, onAutoMove: props.onAutoMove })
+ usePullAutoMove({
+ api: props.api, projectId: 'p1', cards: props.cards ?? cards, board: props.board ?? board,
+ pullBoard: props.pullBoard, onAutoMove: props.onAutoMove
+ })
return null
}
+/** What the board hands the hook on every canvas change: the same cards, in a fresh array of fresh
+ * objects (`sessions` is re-derived from the React Flow nodes each time). */
+const freshCards = (): typeof cards => cards.map((card) => ({ ...card }))
+
function arm(remote = false): void {
useProjects.setState({
activeProjectId: 'p1',
@@ -114,6 +131,87 @@ describe('usePullAutoMove', () => {
expect(onAutoMove).not.toHaveBeenCalled()
})
+ it('a dragged-back card asks the host once, not once per canvas change', async () => {
+ // The claim for #12 was spent when the card first moved; the user then dragged it back.
+ const api = hostClaims()
+ await api.claimPullAutoMove({ projectId: 'p1', cardId: 'card-1', pulls: [12] })
+ api.claimPullAutoMove.mockClear()
+ const onAutoMove = vi.fn()
+ const root = createRoot(document.createElement('div'))
+ for (let change = 0; change < 5; change++) {
+ act(() => root.render(<Probe api={api} pullBoard={merged} onAutoMove={onAutoMove} cards={freshCards()} />))
+ await settle()
+ }
+ expect(api.claimPullAutoMove).toHaveBeenCalledTimes(1)
+ expect(onAutoMove).not.toHaveBeenCalled()
+ })
+
+ it('re-plans only when something the planner reads about the cards changed', async () => {
+ const api = hostClaims()
+ // KanbanView hands a memoized callback; only the cards array is fresh on each canvas change.
+ const onAutoMove = vi.fn()
+ const root = createRoot(document.createElement('div'))
+ act(() => root.render(<Probe api={api} pullBoard={merged} onAutoMove={onAutoMove} cards={freshCards()} />))
+ const plan = vi.mocked(planPullAutoMoves)
+ const before = plan.mock.calls.length
+ for (let change = 0; change < 4; change++) {
+ act(() => root.render(<Probe api={api} pullBoard={merged} onAutoMove={onAutoMove} cards={freshCards()} />))
+ }
+ expect(plan.mock.calls.length).toBe(before)
+ act(() => root.render(<Probe api={api} pullBoard={merged} onAutoMove={onAutoMove}
+ cards={[{ ...cards[0], worktreeBranch: 'feat/y' }]} />))
+ expect(plan.mock.calls.length).toBe(before + 1)
+ await settle()
+ })
+
+ it('the card signature cannot be forged by an id carrying separators', () => {
+ expect(autoMoveCardsSig([{ id: 'a","terminal","feat/x', kind: 'sticky' }]))
+ .not.toBe(autoMoveCardsSig([{ id: 'a', kind: 'terminal', worktreeBranch: 'feat/x' }]))
+ })
+
+ it('does not send a second claim while the first is still in flight', async () => {
+ const pending = new Promise<boolean>(() => undefined)
+ const api = { notePullWaits: vi.fn(async () => 0), claimPullAutoMove: vi.fn(() => pending) }
+ const onAutoMove = vi.fn()
+ const root = createRoot(document.createElement('div'))
+ for (let change = 0; change < 3; change++) {
+ act(() => root.render(
+ <Probe api={api} pullBoard={{ ...merged }} onAutoMove={onAutoMove} cards={freshCards()} />
+ ))
+ }
+ await settle()
+ expect(api.claimPullAutoMove).toHaveBeenCalledTimes(1)
+ })
+
+ it('asks again when a NEW merge joins the card\'s set, and after a failed call', async () => {
+ const api = hostClaims()
+ await api.claimPullAutoMove({ projectId: 'p1', cardId: 'card-1', pulls: [12] })
+ api.claimPullAutoMove.mockClear()
+ const onAutoMove = vi.fn()
+ const root = createRoot(document.createElement('div'))
+ act(() => root.render(<Probe api={api} pullBoard={merged} onAutoMove={onAutoMove} />))
+ await settle()
+ expect(api.claimPullAutoMove).toHaveBeenCalledTimes(1)
+ const again: GitHubPullBoard = {
+ ...merged,
+ pulls: [...merged.pulls, { number: 20, lifecycle: 'merged', headRefName: 'feat/x', closes: [], openSeen: true, mergedSeenAt: 3_000 }]
+ }
+ api.waits.add('p1:card-1:20')
+ act(() => root.render(<Probe api={api} pullBoard={again} onAutoMove={onAutoMove} />))
+ await settle()
+ expect(api.claimPullAutoMove).toHaveBeenLastCalledWith({ projectId: 'p1', cardId: 'card-1', pulls: [12, 20] })
+ expect(onAutoMove).toHaveBeenCalledTimes(1)
+
+ // An IPC that failed was never answered: the next pass may ask again.
+ const failing = { notePullWaits: vi.fn(async () => 0), claimPullAutoMove: vi.fn(async () => { throw new Error('offline') }) }
+ const other = createRoot(document.createElement('div'))
+ act(() => other.render(<Probe api={failing} pullBoard={merged} onAutoMove={vi.fn()} />))
+ await settle()
+ act(() => other.render(<Probe api={failing} pullBoard={{ ...merged }} onAutoMove={vi.fn()} />))
+ await settle()
+ expect(failing.claimPullAutoMove).toHaveBeenCalledTimes(2)
+ })
+
it('never moves a card on a relay tab — that board belongs to the other machine', async () => {
arm(true)
const api = hostClaims()
diff --git a/src/renderer/components/kanban/usePullAutoMove.ts b/src/renderer/components/kanban/usePullAutoMove.ts
index da4e7a257..a94ceb94f 100644
--- a/src/renderer/components/kanban/usePullAutoMove.ts
+++ b/src/renderer/components/kanban/usePullAutoMove.ts
@@ -33,7 +33,13 @@ export function usePullAutoMove(input: {
() => relay ? undefined : sanitizeKanbanPullAutoMove(raw).projects[input.projectId],
[raw, input.projectId, relay]
)
- const { api, projectId, cards, board, pullBoard, onAutoMove } = input
+ const { api, projectId, board, pullBoard, onAutoMove } = input
+ // `cards` is re-derived from the canvas nodes on every canvas change, a fresh array of fresh
+ // objects even when nothing the planner reads changed. Keyed on what it reads, the plan re-runs
+ // only when that does.
+ const cardsSig = autoMoveCardsSig(input.cards)
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ const cards = useMemo(() => input.cards, [cardsSig])
// The BOARD's lifetime, not one effect run: the pull board is re-read every minute, and a re-render
// landing between a won claim and its answer must not throw the move away — the claim is spent
// either way, so dropping it would lose the move for good.
@@ -47,6 +53,11 @@ export function usePullAutoMove(input: {
// Wait notes already sent from this board, so a re-render does not re-send them (the host dedupes
// too; this only saves the calls).
const noted = useRef(new Set<string>())
+ // Claims already asked from this board, keyed by (project, card, PR set). The host answers each
+ // exactly once for good — a won claim is spent, a refused one (already moved, never seen waiting)
+ // stays refused — so a dragged-back card asks once, not on every pull-board read or board edit, and
+ // a claim still in flight is not sent twice. Only a call that FAILED is forgotten and asked again.
+ const asked = useRef(new Set<string>())
useEffect(() => {
if (!entry || !onAutoMove) return
const plan = planPullAutoMoves({ cards, board, pullBoard, entry })
@@ -60,6 +71,9 @@ export function usePullAutoMove(input: {
})
}
for (const move of plan.moves) {
+ const claimKey = `${projectId}\0${move.cardId}\0${move.pulls.join(',')}`
+ if (asked.current.has(claimKey)) continue
+ asked.current.add(claimKey)
void api.claimPullAutoMove({ projectId, cardId: move.cardId, pulls: move.pulls })
.then((claimed) => {
// A claim that lands after this board closed is spent, not applied: the card stays where it
@@ -69,11 +83,22 @@ export function usePullAutoMove(input: {
latest.current?.(move.cardId, move.fromColumnId, entry.columnId, autoMoveNote(move.pulls))
}
})
- .catch(() => undefined)
+ .catch(() => { asked.current.delete(claimKey) })
}
}, [api, entry, cards, board, pullBoard, projectId, onAutoMove])
}
+/** What the planner reads off each card, as one primitive. A card id is written through
+ * `JSON.stringify`, so no id can forge another card's fields. */
+export function autoMoveCardsSig(
+ cards: Array<{ id: string; kind: string; worktreeBranch?: string; issueRef?: IssueRef }>
+): string {
+ return JSON.stringify(cards.map((card) => [
+ card.id, card.kind, card.worktreeBranch ?? null,
+ card.issueRef ? [card.issueRef.owner, card.issueRef.repo, card.issueRef.number] : null
+ ]))
+}
+
/** While some PR is undecided, ask the host (only while the page is visible) whether a chase read
* is due. The host keeps the schedule and the cap. */
export function usePullChase(api: GitHubIssuesApi, projectId: string, undecided: boolean): void {