@@ -65,6 +65,7 @@ import {
6565import { getChatSessionProjection , setChatSessionProjection } from "./history-merge.ts" ;
6666import { handleChatInputHistoryKey } from "./input-history.ts" ;
6767import { installOutboxBrowserStorage } from "./outbox-browser.test-support.ts" ;
68+ import { prepareOutboxPayload } from "./outbox-payloads.ts" ;
6869import { handleChatScrollTakeover } from "./scroll.ts" ;
6970import {
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 ,
0 commit comments