Close three races the review found in the flow window

- Replay live changes over a page of older flows, so a slow flow that
  ends while the page loads is not lost from the window.
- Read list_flows' page and total from one snapshot, so a flow written
  between them is not counted twice.
- Commit a clear's flow deletion only after its bodies' deletion, so a
  failure leaves the flows to clear again rather than orphaned bodies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Gregory SchierandClaude Opus 5.5 authored and Stetta task tk_mvsazf4rsaotrdpf committed 2026-10-28 10:04:25 -07:00
1 parent 1a994f7532
commit 39a7397ebb
4 files changed
+121 -76

No files matched your search

+11
View File
@@ -128,4 +128,15 @@ describe("prependOlder", () => {
const win = prependOlder(windowFromPage([flow(5), flow(4)], 2, 5), [flow(4), flow(3)], 2);
expect(ids(win)).toEqual(["fl_03", "fl_04", "fl_05"]);
});
test("keeps a slow flow that ended while the page loaded, once its changes apply again", () => {
const before = windowFromPage([flow(5), flow(4)], 2, 5);
// Ended mid-request, older than the window, so left out, and missing from the page.
const slow = created(flow(2));
const live = applyChanges(before, [slow]);
expect(ids(live)).toEqual(["fl_04", "fl_05"]);
const merged = prependOlder(live, [flow(3), flow(1)], 2);
const replayed = applyChanges(merged, [slow]);
expect(ids(replayed)).toEqual(["fl_01", "fl_02", "fl_03", "fl_04", "fl_05"]);
});
});
+17 -7
View File
@@ -42,24 +42,33 @@ export const followAtom = atom(
},
);
let loadingOlder = false;
/**
* Changes applied while a page of older flows loads, or null when none is loading. A slow flow
* that ends meanwhile may be older than the window, so left out of it, and missing from a page
* read before it was written; applying these again once the page is in keeps it.
*/
let changesDuringPage: ModelPayload[] | null = null;
/** Loads the page of flows before the oldest one held */
export const loadOlderFlowsAtom = atom(null, async (get, set) => {
const { flows, hasOlder } = get(flowWindowAtom);
const first = flows[0];
if (loadingOlder || !hasOlder || first == null) return;
loadingOlder = true;
if (changesDuringPage != null || !hasOlder || first == null) return;
const changes: ModelPayload[] = [];
changesDuringPage = changes;
try {
const res = await rpc("list_flows", { before: first.id, limit: PAGE_SIZE });
// Dropped when the window moved on meanwhile, trimmed or cleared.
set(flowWindowAtom, (win) =>
win.flows[0]?.id === first.id ? prependOlder(win, res.flows, PAGE_SIZE) : win,
);
set(flowWindowAtom, (win) => {
if (win.flows[0]?.id !== first.id) return win;
const merged = prependOlder(win, res.flows, PAGE_SIZE);
// Counted when they first applied.
return { ...applyChanges(merged, changes), total: merged.total };
});
} catch (err) {
console.warn("Failed to load older flows", err);
} finally {
loadingOlder = false;
changesDuringPage = null;
}
});
@@ -78,6 +87,7 @@ export function syncFlows(store: ReturnType<typeof createStore>) {
if (!loaded || pending.length === 0) return;
const changes = pending;
pending = [];
changesDuringPage?.push(...changes);
store.set(flowWindowAtom, (win) => {
const next = applyChanges(win, changes);
return store.get(followingAtom) ? trimWindow(next) : next;