fix(queue): pure remove() updater + commander view consumes queue

Two behavioral fixes:

1. remove() side effects were inside the setQueue updater — React Strict Mode
   and concurrent mode can invoke updaters multiple times, causing double file
   writes and duplicate PUT requests to /api/beacon/queue. Moved localStorage
   write and writeToFile() call outside the updater; updater is now a pure
   filter. This matches the pre-existing pattern used by shift().

2. ProjectCommanderCard.advance() bypassed the queue entirely — it went straight
   to onRunWithBrain("next_best") even when items were queued. Switching to
   commander view silently broke queue dispatch. Fixed: now calls shiftQueue()
   first, injects the queued item if present, falls back to next_best otherwise.
   Also surfaces queue state in the card: context line shows queue[0] when ready,
   button shows "Send (N)" with a count tooltip instead of plain "Continue".

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Sonnet 4.6 committed 2026-05-16 17:10:42 +02:00
1 parent d851185c26
commit efe70bccec
2 files changed
+24 -15

No files matched your search

@@ -7,6 +7,7 @@ import type { ProjectState } from "@/lib/control-types";
import type { OrchestrationTaskIntentId } from "@/lib/orchestration";
import { getProjectDisplayState } from "./control-presenter";
import { useAutoContinue } from "@/hooks/use-auto-continue";
import { usePromptQueue } from "@/hooks/use-prompt-queue";
export function ProjectCommanderCard({
project,
@@ -24,6 +25,7 @@ export function ProjectCommanderCard({
const nowS = Math.floor(Date.now() / 1000);
const display = getProjectDisplayState(project, zellijTabs, nowS);
const { enabled: autoContinueEnabled, toggle: toggleAutoContinue } = useAutoContinue(project.tab);
const { queue, shift: shiftQueue } = usePromptQueue(project.tab);
const [sending, setSending] = useState(false);
const [talkOpen, setTalkOpen] = useState(false);
@@ -50,6 +52,8 @@ export function ProjectCommanderCard({
const contextLine = isRunning && project.currentPrompt
? project.currentPrompt.label
: queue.length > 0
? queue[0]
: project.session?.next
? project.session.next
: project.session?.done ?? null;
@@ -75,7 +79,12 @@ export function ProjectCommanderCard({
const advance = async () => {
setSending(true);
try {
await onRunWithBrain(project, "next_best");
const queued = shiftQueue();
if (queued) {
await onInject(project.tab, undefined, queued);
} else {
await onRunWithBrain(project, "next_best");
}
} finally {
setSending(false);
}
@@ -130,10 +139,11 @@ export function ProjectCommanderCard({
<button
onClick={advance}
disabled={sending}
title={queue.length > 0 ? `Send queued prompt (${queue.length} in queue)` : "Run next best task"}
className="inline-flex items-center gap-1.5 rounded-lg border border-accent-primary/30 bg-accent-primary/[0.08] px-3 py-1.5 text-xs font-semibold text-text-primary transition-colors hover:border-accent-primary/50 hover:bg-accent-primary/[0.14] disabled:opacity-50"
>
{sending ? <Loader2 className="h-3 w-3 animate-spin" /> : <ArrowRight className="h-3 w-3" />}
Continue
{queue.length > 0 ? `Send (${queue.length})` : "Continue"}
</button>
<button
onClick={() => setTalkOpen((v) => !v)}
+12 -13
View File
@@ -104,20 +104,19 @@ export function usePromptQueue(tab: string) {
} catch { return null; }
}, [tab, setQueue]);
// Reads localStorage for the definitive state, writes the filtered result
// to file immediately (critical for beacon auto-fire where the tab may close
// before the React effect cycle runs), then updates state.
// Writes the filtered queue to file immediately (critical for beacon auto-fire where
// the tab may close before the React effect cycle runs), then updates React state.
// State updater is kept pure (no side effects) so React Strict Mode / concurrent mode
// double-invocation doesn't cause double file writes.
const remove = useCallback((index: number) => {
// Compute next state from React state (authoritative — avoids stale localStorage reads
// when remove() is called before the useLocalStorageState effect has flushed a prior enqueue).
// Write file + update localStorage immediately so beacon auto-fire and PyQt see it
// before the next render cycle completes.
setQueue((q) => {
const next = q.filter((_, i) => i !== index);
try { localStorage.setItem(queueKey(tab), toJson(next)); } catch { /* ignore */ }
writeToFile(tab, next, lastWrittenRef);
return next;
});
try {
const raw = localStorage.getItem(queueKey(tab));
if (raw) {
const next = (JSON.parse(raw) as string[]).filter((_, i) => i !== index);
writeToFile(tab, next, lastWrittenRef); // sets lastWrittenRef → sync effect skips
}
} catch { /* ignore */ }
setQueue((q) => q.filter((_, i) => i !== index));
}, [setQueue, tab]);
const reorder = useCallback((from: number, to: number) => {