Skip to content

Commit c2d2fc2

Browse files
committed
fix(ui): keep queued payload identity independent of routing
Bind immutable attachment bytes to the queued input rather than mutable session aliases. Validate each caller before coalescing equivalent payload reads. Preserve gateway, authenticated recovery, reference, queue, and lock-confirmed tab ownership. Reproduced alias reclassification and nine concurrent validation failures before repair; 403 focused tests, 12,013 UI tests, 44 native Chromium tests, changed gates, full build, fresh isolated review, and real Gateway two-image steering/reload proof pass. Production follow-up is net -10 lines. Refs #133052, #132833.
1 parent 8023d67 commit c2d2fc2

8 files changed

Lines changed: 241 additions & 27 deletions

File tree

‎docs/web/control-ui.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,9 @@ offline, except that **Stop** can queue an exact local run ID for replay. A sess
665665
is not replayed because newer work may start in that session before the connection returns.
666666

667667
Queued attachments use binary Blobs in the browser's IndexedDB; the outbox keeps only delivery
668-
metadata and payload references in session storage. All attachments must be stored before the
669-
message is admitted, and all must be readable before sending. Failed admission leaves the draft
668+
metadata and payload references in session storage. Attachment bytes stay with the queued input
669+
when session aliases resolve or change; the queue metadata owns its destination. All attachments
670+
must be stored before the message is admitted, and all must be readable before sending. Failed admission leaves the draft
670671
unsent. Missing or unreadable queued payloads leave a visible row with recovery guidance; the
671672
browser never sends just the remaining attachments. Binary outbox storage requires browser
672673
storage access and Web Locks, available over HTTPS or localhost. Gateway attachment limits

‎ui/src/lib/chat/chat-types.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ export type ChatQueueItem = {
8282
createdAt: number;
8383
/** Operator-owned queue position; absent means "wherever arrival put it". */
8484
orderKey?: number;
85-
/** Immutable binary bundle; metadata stays in this tab’s outbox. */
86-
attachmentPayload?: { key: string; recoveryScope: string; scopeKey: string; tabId: string };
85+
/** Immutable bytes belong to this queued input; routing belongs to the outbox metadata. */
86+
attachmentPayload?: { key: string; recoveryScope: string; tabId: string };
8787
attachmentStorageError?: "capacity" | "unavailable" | "missing";
8888
attachments?: ChatAttachment[];
8989
refreshSessions?: boolean;

‎ui/src/lib/chat/outbox-payload-store.runtime.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ type PayloadOwner = {
2020
tabId: string;
2121
gatewayOwner: string;
2222
recoveryScope: string;
23-
scopeKey: string;
2423
queueId: string;
2524
};
2625
type PayloadResult<T> =
@@ -79,7 +78,6 @@ export async function writeOutboxPayload(
7978
const key = JSON.stringify([
8079
owner.gatewayOwner,
8180
owner.recoveryScope,
82-
owner.scopeKey,
8381
owner.queueId,
8482
crypto.randomUUID(),
8583
]);
@@ -90,7 +88,6 @@ export async function writeOutboxPayload(
9088
value: {
9189
key,
9290
recoveryScope: owner.recoveryScope,
93-
scopeKey: owner.scopeKey,
9491
tabId: owner.tabId,
9592
},
9693
};
@@ -103,7 +100,7 @@ export async function readOutboxPayload(
103100
owner: PayloadOwner,
104101
reference: PayloadReference,
105102
): Promise<PayloadResult<DurableComposerDraftAttachment[]>> {
106-
if (reference.recoveryScope !== owner.recoveryScope || reference.scopeKey !== owner.scopeKey) {
103+
if (reference.recoveryScope !== owner.recoveryScope) {
107104
return { status: "failed", reason: "missing" };
108105
}
109106
try {
@@ -120,7 +117,6 @@ export async function readOutboxPayload(
120117
value.key !== reference.key ||
121118
value.owner.gatewayOwner !== owner.gatewayOwner ||
122119
value.owner.recoveryScope !== owner.recoveryScope ||
123-
value.owner.scopeKey !== owner.scopeKey ||
124120
value.owner.queueId !== owner.queueId ||
125121
value.owner.tabId !== reference.tabId ||
126122
!Array.isArray(value.attachments) ||

‎ui/src/lib/chat/outbox-store-codec.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,11 @@ export function normalizeStoredQueueItem(value: unknown): ChatQueueItem | null {
8484
isRecord(payload) &&
8585
typeof payload.key === "string" &&
8686
typeof payload.recoveryScope === "string" &&
87-
typeof payload.scopeKey === "string" &&
8887
typeof payload.tabId === "string"
8988
) {
9089
item.attachmentPayload = {
9190
key: payload.key,
9291
recoveryScope: payload.recoveryScope,
93-
scopeKey: payload.scopeKey,
9492
tabId: payload.tabId,
9593
};
9694
} else {

‎ui/src/pages/chat/chat-pending-inputs.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ describe("server-owned pending input display", () => {
253253
tabId: reference.tabId,
254254
gatewayOwner: storageTargetForGateway(host.settings.gatewayUrl).gatewayOwner,
255255
recoveryScope: reference.recoveryScope,
256-
scopeKey: reference.scopeKey,
257256
queueId: queued.id,
258257
}
259258
: undefined;

‎ui/src/pages/chat/chat-send.test.ts‎

Lines changed: 227 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import {
6565
import { getChatSessionProjection, setChatSessionProjection } from "./history-merge.ts";
6666
import { handleChatInputHistoryKey } from "./input-history.ts";
6767
import { installOutboxBrowserStorage } from "./outbox-browser.test-support.ts";
68+
import { prepareOutboxPayload } from "./outbox-payloads.ts";
6869
import { handleChatScrollTakeover } from "./scroll.ts";
6970
import {
7071
cacheChatSessionSnapshot,
@@ -6886,6 +6887,232 @@ describe("handleSendChat", () => {
68866887
);
68876888
});
68886889

6890+
it("keeps queued attachment bytes when a configured main alias changes their metadata scope", async () => {
6891+
const { attachments, dataUrls } = createDeliveryAttachmentBatch();
6892+
const request = makeRequestMock({
6893+
"chat.history": () => idleChatHistory("global"),
6894+
"chat.send": (params: unknown) => ({
6895+
runId: requireRecord(params, "resolved alias send").idempotencyKey,
6896+
status: "started",
6897+
}),
6898+
});
6899+
const write = vi.spyOn(outboxPayloadStore, "writeOutboxPayload");
6900+
const source = makeChatHost({
6901+
client: clientWithRequest(request),
6902+
connected: false,
6903+
sessionKey: "agent:work:workspace",
6904+
assistantAgentId: "work",
6905+
agentsList: { defaultId: "main", mainKey: "main" },
6906+
chatMessage: "keep these bytes with the queued input",
6907+
chatAttachments: attachments,
6908+
});
6909+
await handleSendChat(source);
6910+
const original = expectDefined(
6911+
listStoredChatOutboxes(source)[0]?.queue[0],
6912+
"admitted alias input",
6913+
);
6914+
const reference = expectDefined(original.attachmentPayload, "admitted alias payload");
6915+
const [payloadOwner] = expectDefined(write.mock.calls[0], "actual admitted payload owner");
6916+
expect(original).toMatchObject({
6917+
sessionKey: "agent:work:workspace",
6918+
agentId: "work",
6919+
sendAttempts: 0,
6920+
});
6921+
expect(request.mock.calls.filter(([method]) => method === "chat.send")).toHaveLength(0);
6922+
6923+
reloadChatDocumentStorage(attachments);
6924+
expect(original.attachments?.map(getChatAttachmentDataUrl)).toEqual([null, null]);
6925+
const recovered = makeChatHost({
6926+
client: clientWithRequest(request),
6927+
sessionKey: source.sessionKey,
6928+
assistantAgentId: "work",
6929+
agentsList: { defaultId: "main", mainKey: "workspace" },
6930+
});
6931+
// Exercise today's alias migration without changing its transport-routing policy.
6932+
const moved = expectDefined(
6933+
listStoredChatOutboxes(recovered)[0]?.queue[0],
6934+
"resolved alias input",
6935+
);
6936+
expect(moved).toMatchObject({
6937+
id: original.id,
6938+
sendRunId: original.sendRunId,
6939+
sendAttempts: original.sendAttempts,
6940+
sendState: original.sendState,
6941+
sessionKey: "global",
6942+
agentId: "work",
6943+
attachmentPayload: reference,
6944+
});
6945+
const hydrated = await prepareOutboxPayload(recovered, moved);
6946+
const stored = await outboxPayloadStore.readOutboxPayload(payloadOwner, reference);
6947+
const storedAttachments = expectDefined(
6948+
stored.status === "ready" ? stored.value : undefined,
6949+
"original alias payload bytes",
6950+
);
6951+
expect(
6952+
await Promise.all(
6953+
storedAttachments.map(async (attachment) =>
6954+
Buffer.from(await attachment.blob.arrayBuffer()).toString("base64"),
6955+
),
6956+
),
6957+
).toEqual(dataUrls.map((url) => url.split(",")[1]));
6958+
expect(hydrated).toMatchObject({ status: "ready", item: { attachmentPayload: reference } });
6959+
expect(write).toHaveBeenCalledTimes(1);
6960+
6961+
await retryReconnectableQueuedChatSends(recovered);
6962+
const sends = request.mock.calls.filter(([method]) => method === "chat.send");
6963+
expect(sends).toHaveLength(1);
6964+
expect(requireRecord(sends[0]?.[1], "resolved alias wire payload")).toMatchObject({
6965+
sessionKey: moved.sessionKey,
6966+
agentId: moved.agentId,
6967+
idempotencyKey: original.sendRunId,
6968+
attachments: attachments.map((attachment, index) => ({
6969+
type: attachment.mimeType.startsWith("image/") ? "image" : "file",
6970+
mimeType: attachment.mimeType,
6971+
fileName: attachment.fileName,
6972+
content: dataUrls[index]!.split(",")[1],
6973+
})),
6974+
});
6975+
expect(listStoredChatOutboxes(recovered)[0]?.queue[0]).toMatchObject({
6976+
id: original.id,
6977+
sendRunId: original.sendRunId,
6978+
sendAttempts: 1,
6979+
attachmentPayload: reference,
6980+
});
6981+
});
6982+
6983+
it.each([
6984+
{ caller: "wrong queue", reason: "missing" },
6985+
{ caller: "wrong source tab", reason: "missing" },
6986+
{ caller: "wrong reference recovery scope", reason: "missing" },
6987+
{ caller: "Incognito", reason: "unavailable" },
6988+
{ caller: "unobserved owner", reason: "unavailable" },
6989+
{ caller: "incomplete attachment metadata", reason: "missing" },
6990+
{ caller: "wrong attachment MIME type", reason: "missing" },
6991+
{ caller: "wrong attachment filename", reason: "missing" },
6992+
{ caller: "wrong attachment size", reason: "missing" },
6993+
{ caller: "matching owner", reason: null },
6994+
{ caller: "remembered offline owner", reason: null },
6995+
] as const)(
6996+
"validates a concurrent payload caller independently ($caller)",
6997+
async ({ caller, reason }) => {
6998+
const { attachments, dataUrls } = createDeliveryAttachmentBatch();
6999+
const request = makeRequestMock();
7000+
const client = clientWithRequest(request);
7001+
const source = makeChatHost({
7002+
client,
7003+
connected: false,
7004+
chatMessage: "one immutable attachment input",
7005+
chatAttachments: attachments,
7006+
});
7007+
const write = vi.spyOn(outboxPayloadStore, "writeOutboxPayload");
7008+
await handleSendChat(source);
7009+
const original = expectDefined(
7010+
listStoredChatOutboxes(source)[0]?.queue[0],
7011+
"admitted shared input",
7012+
);
7013+
const reference = expectDefined(original.attachmentPayload, "shared input reference");
7014+
const [payloadOwner] = expectDefined(write.mock.calls[0], "actual shared payload owner");
7015+
const other = makeChatHost({ client, connected: false });
7016+
const candidate: ChatQueueItem = {
7017+
...original,
7018+
attachmentPayload: { ...reference },
7019+
attachments: original.attachments?.map((attachment) => ({ ...attachment })),
7020+
};
7021+
switch (caller) {
7022+
case "wrong queue":
7023+
candidate.id = `${original.id}-other`;
7024+
break;
7025+
case "wrong source tab":
7026+
candidate.attachmentPayload = { ...reference, tabId: "other-source-tab" };
7027+
break;
7028+
case "wrong reference recovery scope":
7029+
candidate.attachmentPayload = { ...reference, recoveryScope: "other-reference-owner" };
7030+
break;
7031+
case "Incognito":
7032+
other.selectedChatSessionIncognito = true;
7033+
break;
7034+
case "unobserved owner":
7035+
other.client = clientWithRequest(request);
7036+
vi.spyOn(other.client, "recoveryScopeReady", "get").mockReturnValue(false);
7037+
break;
7038+
case "incomplete attachment metadata":
7039+
candidate.attachments = candidate.attachments?.slice(0, 1);
7040+
break;
7041+
case "wrong attachment MIME type":
7042+
Object.assign(expectDefined(candidate.attachments?.[0], "first attachment metadata"), {
7043+
mimeType: "text/plain",
7044+
});
7045+
break;
7046+
case "wrong attachment filename":
7047+
Object.assign(expectDefined(candidate.attachments?.[0], "first attachment metadata"), {
7048+
fileName: "other-file.png",
7049+
});
7050+
break;
7051+
case "wrong attachment size":
7052+
Object.assign(expectDefined(candidate.attachments?.[0], "first attachment metadata"), {
7053+
sizeBytes: 1,
7054+
});
7055+
break;
7056+
case "remembered offline owner":
7057+
vi.spyOn(client, "recoveryScopeReady", "get").mockReturnValue(false);
7058+
break;
7059+
case "matching owner":
7060+
break;
7061+
}
7062+
const expected = reason ? { status: "failed", reason } : { status: "ready" };
7063+
expect(await prepareOutboxPayload(other, candidate)).toMatchObject(expected);
7064+
7065+
const readPayload = outboxPayloadStore.readOutboxPayload;
7066+
const readStarted = createDeferred();
7067+
const releaseRead = createDeferred();
7068+
const pending: Array<ReturnType<typeof prepareOutboxPayload>> = [];
7069+
const read = vi
7070+
.spyOn(outboxPayloadStore, "readOutboxPayload")
7071+
.mockImplementationOnce(async (...args) => {
7072+
const result = await readPayload(...args);
7073+
readStarted.resolve();
7074+
await releaseRead.promise;
7075+
return result;
7076+
});
7077+
try {
7078+
const first = prepareOutboxPayload(source, original);
7079+
pending.push(first);
7080+
await readStarted.promise;
7081+
const second = prepareOutboxPayload(other, candidate);
7082+
pending.push(second);
7083+
releaseRead.resolve();
7084+
const [valid, joined] = await Promise.all([first, second]);
7085+
expect(valid).toMatchObject({ status: "ready", item: { attachmentPayload: reference } });
7086+
expect(
7087+
valid.status === "ready" ? valid.item.attachments?.map(getChatAttachmentDataUrl) : [],
7088+
).toEqual(dataUrls);
7089+
const stored = await readPayload(payloadOwner, reference);
7090+
const retained = expectDefined(
7091+
stored.status === "ready" ? stored.value : undefined,
7092+
"retained shared bytes",
7093+
);
7094+
expect(
7095+
await Promise.all(
7096+
retained.map(async (attachment) =>
7097+
Buffer.from(await attachment.blob.arrayBuffer()).toString("base64"),
7098+
),
7099+
),
7100+
).toEqual(dataUrls.map((url) => url.split(",")[1]));
7101+
expect(request.mock.calls.filter(([method]) => method === "chat.send")).toHaveLength(0);
7102+
expect(joined).toMatchObject(expected);
7103+
if (!reason) {
7104+
expect(read).toHaveBeenCalledTimes(1);
7105+
expect(
7106+
joined.status === "ready" ? joined.item.attachments?.map(getChatAttachmentDataUrl) : [],
7107+
).toEqual(dataUrls);
7108+
}
7109+
} finally {
7110+
releaseRead.resolve();
7111+
await Promise.allSettled(pending);
7112+
}
7113+
},
7114+
);
7115+
68897116
it.each(["unavailable", "capacity"] as const)(
68907117
"preserves inline bytes across a %s migration failure and reload before retry",
68917118
async (reason) => {
@@ -7034,7 +7261,6 @@ describe("handleSendChat", () => {
70347261
tabId: oldRef.tabId,
70357262
gatewayOwner: "default",
70367263
recoveryScope: oldRef.recoveryScope,
7037-
scopeKey: oldRef.scopeKey,
70387264
queueId: current.id,
70397265
},
70407266
replacements,
@@ -8178,7 +8404,6 @@ describe("handleSendChat", () => {
81788404
tabId: reference.tabId,
81798405
gatewayOwner: "default",
81808406
recoveryScope: "test-recovery-scope",
8181-
scopeKey: reference.scopeKey,
81828407
queueId: stored.id,
81838408
},
81848409
reference,

‎ui/src/pages/chat/composer-persistence.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,6 @@ describe("chat composer persistence", () => {
490490
const reference = {
491491
key: "original-payload",
492492
recoveryScope: "versioned-owner",
493-
scopeKey: "versioned-scope",
494493
tabId: "versioned-tab",
495494
};
496495
const original: ChatQueueItem = {

0 commit comments

Comments
 (0)