Skip to content

Commit 9a3f3b7

Browse files
authored
fix(ui): release losing fallback payloads on composer adoption (#124391)
Global-scope composer fallback adoption coalesces sibling candidates (bare-global, default-main, qualified-main) into one winner and deletes the losers from chatComposerFallbackByScope — but never released their attachment payload-store entries, unlike every clearChatComposerMemory- Fallback caller. The dropped data URLs and object URLs leaked for the pane's lifetime. Release dropped candidates' payloads at the adoption site, retaining ids still referenced by the live composer or any surviving fallback (the same retention rule the pane-handoff owner uses).
1 parent c6ae99b commit 9a3f3b7

4 files changed

Lines changed: 135 additions & 8 deletions

File tree

‎ui/src/pages/chat/attachment-payload-store.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ export function releaseChatAttachmentPayloads(attachments: readonly ChatAttachme
8787
}
8888
}
8989

90+
/**
91+
* Releases displaced attachments except ids still referenced by a retained
92+
* owner (live composer, surviving fallbacks). Attachments are backups of
93+
* composer state, so shared ids across owners are the norm — dropping one
94+
* owner must never revoke another owner's payload.
95+
*/
96+
export function releaseDisplacedChatAttachmentPayloads(
97+
displaced: readonly ChatAttachment[],
98+
retained: ReadonlyArray<readonly ChatAttachment[]>,
99+
): void {
100+
const retainedIds = new Set(retained.flat().map((attachment) => attachment.id));
101+
releaseChatAttachmentPayloads(displaced.filter((attachment) => !retainedIds.has(attachment.id)));
102+
}
103+
90104
export function generateAttachmentId(): string {
91105
return `att-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
92106
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* @vitest-environment jsdom */
2+
3+
import { describe, expect, it } from "vitest";
4+
import type { ChatAttachment } from "../../lib/chat/chat-types.ts";
5+
import {
6+
getChatAttachmentDataUrl,
7+
registerChatAttachmentPayload,
8+
} from "./attachment-payload-store.ts";
9+
import { retainChatComposerMemoryFallback } from "./chat-composer-memory-fallback.ts";
10+
import type { ChatComposerMemoryFallback, ChatPageHost } from "./chat-state-host.ts";
11+
import { storedChatOutboxScopeKey } from "./composer-persistence.ts";
12+
13+
function storedAttachment(id: string, mimeType = "image/png"): ChatAttachment {
14+
return registerChatAttachmentPayload({
15+
attachment: { id, mimeType },
16+
dataUrl: `data:${mimeType};base64,${id}`,
17+
file: new File([id], id, { type: mimeType }),
18+
});
19+
}
20+
21+
function globalHost(
22+
fallbacks: Record<string, ChatComposerMemoryFallback>,
23+
attachments: ChatAttachment[] = [],
24+
): ChatPageHost {
25+
return {
26+
agentsList: { defaultId: "main", mainKey: "main" },
27+
assistantAgentId: "main",
28+
chatAttachments: attachments,
29+
chatComposerFallbackByScope: fallbacks,
30+
hello: null,
31+
sessionKey: "global",
32+
settings: { gatewayUrl: "ws://example.test" },
33+
} as unknown as ChatPageHost;
34+
}
35+
36+
describe("chat composer memory fallback adoption", () => {
37+
it("releases losing sibling fallback payloads when adoption drops them", () => {
38+
const losing = storedAttachment("losing-sibling-attachment");
39+
const winning = storedAttachment("winning-attachment");
40+
const scope = { sessionKey: "global", agentId: "main" } as const;
41+
const scopeKey = storedChatOutboxScopeKey(scope);
42+
const bareGlobalKey = storedChatOutboxScopeKey({ sessionKey: "global" });
43+
const host = globalHost({
44+
[bareGlobalKey]: {
45+
message: "older sibling draft",
46+
attachments: [losing],
47+
storageFailed: true,
48+
sequence: 1,
49+
},
50+
[scopeKey]: {
51+
message: "newest draft",
52+
attachments: [winning],
53+
storageFailed: true,
54+
sequence: 2,
55+
},
56+
});
57+
58+
const ownership = retainChatComposerMemoryFallback(host, scope, {
59+
message: "newest draft",
60+
attachments: [winning],
61+
});
62+
63+
expect(ownership).toEqual({ sequence: 2 });
64+
expect(Object.keys(host.chatComposerFallbackByScope)).toEqual([scopeKey]);
65+
// The losing sibling was dropped for good: its payload-store entry must be
66+
// released with it, while the adopted fallback's payload stays available.
67+
expect(getChatAttachmentDataUrl({ id: losing.id, mimeType: losing.mimeType })).toBeNull();
68+
expect(getChatAttachmentDataUrl({ id: winning.id, mimeType: winning.mimeType })).toBe(
69+
"data:image/png;base64,winning-attachment",
70+
);
71+
});
72+
73+
it("keeps payloads shared with the live composer when a sibling is dropped", () => {
74+
const shared = storedAttachment("shared-with-composer");
75+
const scope = { sessionKey: "global", agentId: "main" } as const;
76+
const scopeKey = storedChatOutboxScopeKey(scope);
77+
const bareGlobalKey = storedChatOutboxScopeKey({ sessionKey: "global" });
78+
const host = globalHost(
79+
{
80+
[bareGlobalKey]: {
81+
message: "older sibling draft",
82+
attachments: [shared],
83+
storageFailed: true,
84+
sequence: 1,
85+
},
86+
[scopeKey]: {
87+
message: "newest draft",
88+
attachments: [],
89+
storageFailed: true,
90+
sequence: 2,
91+
},
92+
},
93+
[shared],
94+
);
95+
96+
retainChatComposerMemoryFallback(host, scope, {
97+
message: "newest draft",
98+
attachments: [],
99+
});
100+
101+
expect(getChatAttachmentDataUrl({ id: shared.id, mimeType: shared.mimeType })).toBe(
102+
"data:image/png;base64,shared-with-composer",
103+
);
104+
});
105+
});

‎ui/src/pages/chat/chat-composer-memory-fallback.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
resolveUiDefaultAgentId,
77
resolveUiKnownSelectedGlobalAgentId,
88
} from "../../lib/sessions/session-key.ts";
9+
import { releaseDisplacedChatAttachmentPayloads } from "./attachment-payload-store.ts";
910
import type { ChatComposerMemoryFallback, ChatPageHost } from "./chat-state-host.ts";
1011
import {
1112
loadChatComposerCommittedDraftRevision,
@@ -106,6 +107,13 @@ function resolveChatComposerMemoryFallback(
106107
delete nextFallbacks[candidate.scopeKey];
107108
}
108109
nextFallbacks[scopeKey] = adoptedFallback;
110+
// Losing sibling fallbacks are dropped for good here; release their
111+
// payload-store entries (like the pane-handoff owner does) or the data URLs
112+
// leak for the pane's lifetime.
113+
releaseDisplacedChatAttachmentPayloads(
114+
candidates.flatMap((candidate) => candidate.fallback.attachments),
115+
[state.chatAttachments, ...Object.values(nextFallbacks).map((f) => f.attachments)],
116+
);
109117
state.chatComposerFallbackByScope = nextFallbacks;
110118
return { fallback: adoptedFallback, scopeKey };
111119
}

‎ui/src/pages/chat/chat-pane-attachment-handoff.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { ApplicationContext } from "../../app/context.ts";
22
import type { ChatAttachment } from "../../lib/chat/chat-types.ts";
3-
import { releaseChatAttachmentPayload } from "./attachment-payload-store.ts";
3+
import {
4+
releaseChatAttachmentPayload,
5+
releaseDisplacedChatAttachmentPayloads,
6+
} from "./attachment-payload-store.ts";
47
import type { ChatPageHost } from "./chat-state-host.ts";
58
import { resolveStoredChatOutboxScope, storedChatOutboxScopeKey } from "./composer-persistence.ts";
69
import { panesOf, type ChatSplitLayout, visiblePanesOf } from "./split-layout.ts";
@@ -51,13 +54,10 @@ export function restorePaneStagedAttachments(
5154
...restored.fallbacks,
5255
...state.chatComposerFallbackByScope,
5356
};
54-
const retainedIds = new Set(state.chatAttachments.map((attachment) => attachment.id));
55-
for (const fallback of Object.values(state.chatComposerFallbackByScope)) {
56-
for (const attachment of fallback.attachments) {
57-
retainedIds.add(attachment.id);
58-
}
59-
}
60-
releaseAttachments(displaced, retainedIds);
57+
releaseDisplacedChatAttachmentPayloads(displaced, [
58+
state.chatAttachments,
59+
...Object.values(state.chatComposerFallbackByScope).map((fallback) => fallback.attachments),
60+
]);
6161
}
6262

6363
export function preparePaneStagedAttachments(

0 commit comments

Comments
 (0)