fix(web): key the rule reads to the task id as well

The request counter alone was not enough: a rule write started for task A
holds A's loadRules closure, so when it resolved after a switch it fetched
A's rules and — being the newest request — painted them into task B's card.

The card now also reads a taskIdRef that names the task it shows. Every
read and write returns early when that ref has moved, and the counter
still drops a response that a newer request for the same task replaced.
add, saveEdit and toggleTargetGate return early too, so a late response
cannot clear the next task's half-typed form or leave its switch busy.

Verified: npx tsc --noEmit clean, npx biome ci . 0 errors (196 files),
npm run build:static compiled.
This commit is contained in:
Barry
2026-12-02 14:26:48 -05:00
parent 0837422feb
commit 19b4cb3dd0
@@ -1137,37 +1137,42 @@ function TaskInterceptRulesCard({ taskId }: { taskId: string }) {
const [editPattern, setEditPattern] = React.useState("");
const [editNote, setEditNote] = React.useState("");
// Switching between two task details keeps this component mounted, so a response can arrive after the
// operator moved to another task. Every read and write carries the request it belongs to, and a response
// that is no longer the latest one is dropped instead of painting another task's state.
// operator moved to another task. Two things guard every state write: taskIdRef names the task the card
// shows now (a write started for the previous task returns early), and the request counters drop a
// response that a newer request for the same task has already superseded.
const taskIdRef = React.useRef(taskId);
taskIdRef.current = taskId;
const rulesRequestRef = React.useRef(0);
const gateRequestRef = React.useRef(0);
// The rules and the gate are read separately: a failure in one must not hide the other, and a failed
// read must never leave the switch disabled with no way to retry.
const loadRules = React.useCallback(async () => {
if (taskIdRef.current !== taskId) return;
const request = ++rulesRequestRef.current;
try {
const list = await api.taskInterceptRules(taskId);
if (rulesRequestRef.current !== request) return;
if (taskIdRef.current !== taskId || rulesRequestRef.current !== request) return;
setRules(list);
setErr("");
} catch (e) {
if (rulesRequestRef.current !== request) return;
if (taskIdRef.current !== taskId || rulesRequestRef.current !== request) return;
setErr((e as Error).message);
}
}, [taskId]);
const loadGate = React.useCallback(async () => {
if (taskIdRef.current !== taskId) return;
const request = ++gateRequestRef.current;
setGateErr("");
try {
const task = await api.task(taskId);
if (gateRequestRef.current !== request) return;
if (taskIdRef.current !== taskId || gateRequestRef.current !== request) return;
// A backend that omits the field reads as unknown, never as "off".
setTargetGate(typeof task.target_gate === "boolean" ? task.target_gate : undefined);
setTaskFinished(task.status === "done" || task.status === "failed" || task.status === "timeout");
} catch (e) {
if (gateRequestRef.current !== request) return;
if (taskIdRef.current !== taskId || gateRequestRef.current !== request) return;
setTargetGate(undefined);
setGateErr((e as Error).message);
}
@@ -1194,13 +1199,13 @@ function TaskInterceptRulesCard({ taskId }: { taskId: string }) {
setGateErr("");
try {
const task = await api.setTaskTargetGate(taskId, next);
if (gateRequestRef.current !== request) return;
if (taskIdRef.current !== taskId || gateRequestRef.current !== request) return;
setTargetGate(typeof task.target_gate === "boolean" ? task.target_gate : undefined);
} catch (e) {
if (gateRequestRef.current !== request) return;
if (taskIdRef.current !== taskId || gateRequestRef.current !== request) return;
setGateErr((e as Error).message);
} finally {
if (gateRequestRef.current === request) setGateBusy(false);
if (taskIdRef.current === taskId && gateRequestRef.current === request) setGateBusy(false);
}
}
@@ -1216,13 +1221,15 @@ function TaskInterceptRulesCard({ taskId }: { taskId: string }) {
note: newNote.trim(),
enabled: true,
});
if (taskIdRef.current !== taskId) return;
setNewPattern("");
setNewNote("");
await loadRules();
} catch (e) {
if (taskIdRef.current !== taskId) return;
setErr((e as Error).message);
} finally {
setBusy(false);
if (taskIdRef.current === taskId) setBusy(false);
}
}
@@ -1247,12 +1254,14 @@ function TaskInterceptRulesCard({ taskId }: { taskId: string }) {
note: editNote.trim(),
enabled: r.enabled,
});
if (taskIdRef.current !== taskId) return;
setEditId(null);
await loadRules();
} catch (e) {
if (taskIdRef.current !== taskId) return;
setErr((e as Error).message);
} finally {
setBusy(false);
if (taskIdRef.current === taskId) setBusy(false);
}
}