Skip to content

Commit 4c92a01

Browse files
npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7wpfleger96
andcommitted
fix(observer-feed): replace activeChannelId token with resetGeneration counter
Channel-string equality fails the A→B→A case: an in-flight old-A request snapshotting channelId="A" passes every guard after the user returns to A, allowing it to mark fresh-A exhausted and steal fresh-A's fetch lock. Replace activeChannelId-as-ownership with a monotonically increasing resetGeneration counter in ArchivePagingState. applyChannelReset() increments it on every call. fetchOlderArchived snapshots the counter at request start and rechecks snapshot === ps.resetGeneration after every async boundary (backfill, Tauri read, ingest). The finally block clears ps.isFetching only if the snapshot still matches. A→B→A produces gen sequences 1→2→3, so old-A (gen=1) is always rejected by fresh-A (gen=3) regardless of channel name. activeChannelId is retained as a diagnostic label only — not as an ownership token. Mounted-hook regressions updated: - test (a) comment corrected: this test was GREEN at dfb2d03 (stale closure was already fixed in round 1, not this round) - test (b) extended with a lock-probe step: after stale A resolves, call fetchOlderArchived() directly while B's first read is in flight and assert no new Tauri read starts (lock held). This makes the test fail independently when the finally guard is removed — not just when the post-ingest exhaustion write is present. - test (c) new: A→B→A mounted regression. Old-A's decrypt deferred, switch A→B→A, fresh-A's first read deferred. Resolves old-A first, then lock-probes (concurrent call must be rejected), then resolves fresh-A and asserts it reads ≥2 pages. Mutation verification (run before this commit): - Remove post-ingest check (line ~342): tests (b) and (c) go RED ✓ - Remove finally guard (line ~357): tests (b) and (c) go RED ✓ Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent af45fbf commit 4c92a01

3 files changed

Lines changed: 330 additions & 83 deletions

File tree

‎desktop/src/features/agents/ui/archivePagingState.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ export interface ArchivePagingState {
3333
* so switching channels triggers a fresh hydration pass. */
3434
initialHydrationDone: boolean;
3535
/** The channelId that this paging state is currently scoped to.
36-
* Captured by fetchOlderArchived at request start; compared on resolution
37-
* to detect and discard cursor/exhaustion writes from in-flight reads that
38-
* completed after a channel switch. */
36+
* Kept for diagnostics only; NOT used as a generation token (see
37+
* resetGeneration). Channel equality is not a unique request identifier:
38+
* A→B→A makes old-A channel checks pass again. */
3939
activeChannelId: string | null;
40+
/** Monotonically increasing counter incremented by applyChannelReset.
41+
* Each fetch snapshots this value at request start and checks it again
42+
* after every async boundary — a mismatch means a channel switch occurred
43+
* mid-flight (even A→B→A), and results are discarded. */
44+
resetGeneration: number;
4045
}
4146

4247
/**
@@ -54,6 +59,7 @@ export function createArchivePagingState(): ArchivePagingState {
5459
cursor: null,
5560
initialHydrationDone: false,
5661
activeChannelId: null,
62+
resetGeneration: 0,
5763
};
5864
state.backfillPromise = new Promise<void>((resolve) => {
5965
state.backfillResolve = resolve;
@@ -64,11 +70,17 @@ export function createArchivePagingState(): ArchivePagingState {
6470
/**
6571
* Reset per-channel paging state when the viewed channel changes.
6672
*
67-
* Only cursor, exhaustion flag, fetch lock, and channel token are
68-
* channel-scoped. Backfill state is identity-level (the index covers ALL
73+
* Only cursor, exhaustion flag, fetch lock, channel label, and generation token
74+
* are channel-scoped. Backfill state is identity-level (the index covers ALL
6975
* channels and needs to run only once per identity mount), so it is
7076
* intentionally NOT touched here.
7177
*
78+
* `resetGeneration` is incremented on every call. In-flight fetches snapshot
79+
* the generation at start and recheck it after every async boundary — a
80+
* mismatch (including A→B→A) means the request is stale, so results are
81+
* discarded. Channel ID is retained for diagnostics only; it does NOT serve
82+
* as the generation token.
83+
*
7284
* Called by the useEffect([channelId]) in useLoadArchivedObserverEvents.
7385
* Exported so tests can verify the reset semantics without a React runtime.
7486
*/
@@ -81,6 +93,7 @@ export function applyChannelReset(
8193
state.hasOlderArchived = true;
8294
state.initialHydrationDone = false;
8395
state.activeChannelId = newChannelId;
96+
state.resetGeneration += 1;
8497
}
8598

8699
/**

0 commit comments

Comments
 (0)