Fix timeline sync when reopening a cached chat (#11464)

Co-authored-by: OYZH888 <16219422+oyzh888@users.noreply.github.com>
This commit is contained in:
paseo-bot[bot]
2026-11-30 14:12:43 +00:00
committed by GitHub
co-authored by OYZH888
parent c9ec9863ba
commit 8f696db2f2
2 changed files with 35 additions and 6 deletions
@@ -1349,6 +1349,33 @@ test("membership retry cannot run while disconnected", async () => {
expect(restored.agentIds).toEqual(["agent-a"]);
});
test("reopening a cached chat while its previous subscription is pending rechecks freshness", async () => {
const world = new TimelineWorld(undefined, true);
const agentId = "agent-a";
world.cursors.set(agentId, { epoch: "cached", endSeq: 9 });
world.show("workspace", [agentId]);
world.sync.setConnected(true);
try {
const previous = await world.nextMembership();
world.show("workspace", []);
world.sync.replaceOpenTabAgentIds([]);
world.show("workspace", [agentId]);
previous.succeed({ [agentId]: "current" });
const reopened = await world.nextMembership();
expect(reopened.agentIds).toEqual([agentId]);
expect(reopened.cursors).toEqual({ [agentId]: { epoch: "cached", seq: 9 } });
reopened.succeed({ [agentId]: "current" });
await vi.waitFor(() => expect(world.sync.getAgentTimelineStatus(agentId)).toBe("ready"));
expect(world.appliedCurrent).toEqual([agentId]);
world.expectNoPendingFetch();
} finally {
world.sync.dispose();
}
});
test("returning to an open hidden agent stays live after inactivity", async () => {
const world = new TimelineWorld();
world.sync.setConnected(true);
@@ -778,7 +778,7 @@ export function createViewedTimelineSync(ports: ViewedTimelineSyncPorts): Viewed
let connected = false;
let observation: {
handle: ReturnType<ViewedTimelineSyncPorts["observe"]>;
agentIds: string[];
generation: number;
} | null = null;
let disposed = false;
let desired: string[] = [];
@@ -1049,10 +1049,12 @@ export function createViewedTimelineSync(ports: ViewedTimelineSyncPorts): Viewed
}
};
const membershipIsSettled = (requested: string[]) =>
// A request from an earlier generation can have the same IDs after a tab
// closes and reopens. Its answer will be ignored, so it cannot satisfy this one.
const membershipIsCovered = (generation: number, requested: string[]) =>
!membershipNeedsRetry &&
!freshnessCheckOwed &&
sameAgentIds(requested, observation?.agentIds ?? acknowledged);
(observation ? observation.generation === generation : requested.length === 0);
// A chat with no fetch running and a cursor to compare; the daemon can answer without a fetch.
const collectFreshnessCursors = (requested: string[]) => {
@@ -1104,13 +1106,13 @@ export function createViewedTimelineSync(ports: ViewedTimelineSyncPorts): Viewed
if (disposed || !connected) return;
const generation = membershipGeneration;
const requested = desired;
if (membershipIsSettled(requested)) return;
if (membershipIsCovered(generation, requested)) return;
const canCheck = ports.canCheckFreshness();
if (canCheck) {
// The cursor sent to the daemon must include whatever the cache restores.
await Promise.all(requested.map((agentId) => cacheLoads.get(agentId)));
if (isReconcileSuperseded(generation)) return;
if (membershipIsSettled(requested)) return;
if (membershipIsCovered(generation, requested)) return;
}
membershipNeedsRetry = false;
freshnessCheckOwed = false;
@@ -1122,7 +1124,7 @@ export function createViewedTimelineSync(ports: ViewedTimelineSyncPorts): Viewed
try {
const previous = observation;
const handle = ports.observe(requested, cursors);
observation = { handle, agentIds: requested };
observation = { handle, generation };
// A subscription routes events only once the daemon accepts it, so the old one delivers until then.
const answer = await handle.ready.finally(() => {
void previous?.handle.release().catch(ports.reportError);