@@ -32,6 +32,7 @@ import { refreshChatAvatar } from "./chat-avatar.ts";
3232import * as chatCommandExecutor from "./chat-command-executor.ts" ;
3333import type { executeSlashCommand } from "./chat-command-executor.ts" ;
3434import { makeChatHost , makeRequestMock } from "./chat-host.test-support.ts" ;
35+ import { UNCONFIRMED_CHAT_SEND_ERROR } from "./chat-outbox-drain.ts" ;
3536import { renderChatPaneComposerControls } from "./chat-pane-session-controls.ts" ;
3637import type { ChatHost } from "./chat-send-contract.ts" ;
3738import {
@@ -149,6 +150,7 @@ let loadChatHistory: typeof import("./chat-history.ts").loadChatHistory;
149150let clearPendingQueueItemsForRun : typeof import ( "./chat-queue.ts" ) . clearPendingQueueItemsForRun ;
150151let admitQueuedMessageForSession : typeof import ( "./chat-queue.ts" ) . admitQueuedMessageForSession ;
151152let removeQueuedMessage : typeof import ( "./chat-queue.ts" ) . removeQueuedMessage ;
153+ let setTransientQueuedMessageProjection : typeof import ( "./chat-queue.ts" ) . setTransientQueuedMessageProjection ;
152154let removeDeliveredQueuedChatSendForRun : typeof import ( "./chat-queue.ts" ) . removeDeliveredQueuedChatSendForRun ;
153155let removeVisibleOrScopedQueuedMessageWithoutReleasing : typeof import ( "./chat-queue.ts" ) . removeVisibleOrScopedQueuedMessageWithoutReleasing ;
154156let markQueuedChatSendsWaitingForReconnect : typeof import ( "./chat-queue.ts" ) . markQueuedChatSendsWaitingForReconnect ;
@@ -182,6 +184,7 @@ async function loadChatHelpers(): Promise<void> {
182184 clearPendingQueueItemsForRun,
183185 removeDeliveredQueuedChatSendForRun,
184186 removeQueuedMessage,
187+ setTransientQueuedMessageProjection,
185188 markQueuedChatSendsWaitingForReconnect,
186189 removeVisibleOrScopedQueuedMessageWithoutReleasing,
187190 readChatQueueForScope,
@@ -5604,6 +5607,120 @@ describe("handleSendChat", () => {
56045607 expect ( host . chatMessages ) . toStrictEqual ( [ ] ) ;
56055608 } ) ;
56065609
5610+ it ( "keeps an acknowledged live send pending while durable history is briefly stale" , async ( ) => {
5611+ let historyRequests = 0 ;
5612+ let runId : string | undefined ;
5613+ const host = makeChatHost ( {
5614+ requestHandlers : {
5615+ "chat.history" : ( ) => {
5616+ historyRequests += 1 ;
5617+ return Promise . resolve ( {
5618+ messages :
5619+ historyRequests > 2
5620+ ? [ { role : "user" , __openclaw : { idempotencyKey : `${ runId } :user` } } ]
5621+ : [ ] ,
5622+ sessionInfo : row ( "agent:main" , { hasActiveRun : false , status : "done" } ) ,
5623+ } ) ;
5624+ } ,
5625+ "chat.send" : ( params : unknown ) => {
5626+ const payload = requireRecord ( params , "live send payload" ) ;
5627+ runId = String ( payload . idempotencyKey ) ;
5628+ return Promise . resolve ( { runId, status : "started" } ) ;
5629+ } ,
5630+ } ,
5631+ } ) ;
5632+
5633+ await handleSendChat ( host , "history will catch up" ) ;
5634+ expect ( host . chatQueue ) . toEqual ( [
5635+ expect . objectContaining ( { sendState : "sending" , text : "history will catch up" } ) ,
5636+ ] ) ;
5637+ expect ( loadChatComposerSnapshot ( host , host . sessionKey ) ?. queue ) . toEqual ( [
5638+ expect . objectContaining ( { sendAttempts : 1 , sendState : "waiting-reconnect" } ) ,
5639+ ] ) ;
5640+
5641+ host . chatRunId = null ;
5642+ await flushChatQueueForEvent ( host ) ;
5643+
5644+ expect ( host . chatQueue [ 0 ] ?. sendState ) . toBe ( "sending" ) ;
5645+ expect ( host . lastError ) . toBeNull ( ) ;
5646+ await waitForFast ( ( ) => expect ( listStoredChatOutboxes ( host ) ) . toStrictEqual ( [ ] ) , {
5647+ timeout : 1_000 ,
5648+ } ) ;
5649+ expect ( historyRequests ) . toBeGreaterThanOrEqual ( 3 ) ;
5650+ expect ( host . chatQueue ) . toStrictEqual ( [ ] ) ;
5651+ expect ( host . lastError ) . toBeNull ( ) ;
5652+ } ) ;
5653+
5654+ it ( "marks an acknowledged live send unconfirmed after history stays missing" , async ( ) => {
5655+ let now = 1_000 ;
5656+ vi . spyOn ( Date , "now" ) . mockImplementation ( ( ) => now ) ;
5657+ const host = makeChatHost ( {
5658+ requestHandlers : {
5659+ "chat.history" : ( ) => idleChatHistory ( ) ,
5660+ "chat.send" : ( params : unknown ) => {
5661+ const payload = requireRecord ( params , "live send payload" ) ;
5662+ const runId = String ( payload . idempotencyKey ) ;
5663+ return Promise . resolve ( { runId, status : "started" } ) ;
5664+ } ,
5665+ } ,
5666+ } ) ;
5667+
5668+ await handleSendChat ( host , "history never catches up" ) ;
5669+ host . chatRunId = null ;
5670+ await flushChatQueueForEvent ( host ) ;
5671+
5672+ expect ( host . chatQueue [ 0 ] ?. sendState ) . toBe ( "sending" ) ;
5673+ expect ( host . lastError ) . toBeNull ( ) ;
5674+
5675+ now += 5_001 ;
5676+ await flushChatQueueForEvent ( host ) ;
5677+
5678+ expect ( host . chatQueue [ 0 ] ) . toMatchObject ( {
5679+ sendError : UNCONFIRMED_CHAT_SEND_ERROR ,
5680+ sendState : "unconfirmed" ,
5681+ text : "history never catches up" ,
5682+ } ) ;
5683+ expect ( host . lastError ) . toBe ( UNCONFIRMED_CHAT_SEND_ERROR ) ;
5684+ } ) ;
5685+
5686+ it ( "starts a fresh confirmation grace after the prior outbox scope is removed" , async ( ) => {
5687+ let now = 1_000 ;
5688+ vi . spyOn ( Date , "now" ) . mockImplementation ( ( ) => now ) ;
5689+ const host = makeChatHost ( {
5690+ requestHandlers : {
5691+ "chat.history" : ( ) => idleChatHistory ( ) ,
5692+ "chat.send" : ( params : unknown ) => {
5693+ const payload = requireRecord ( params , "live send payload" ) ;
5694+ const runId = String ( payload . idempotencyKey ) ;
5695+ return Promise . resolve ( { runId, status : "started" } ) ;
5696+ } ,
5697+ } ,
5698+ } ) ;
5699+
5700+ await handleSendChat ( host , "reuse this delivery version" ) ;
5701+ host . chatRunId = null ;
5702+ await flushChatQueueForEvent ( host ) ;
5703+
5704+ const item = expectDefined ( host . chatQueue [ 0 ] , "queued live send" ) ;
5705+ const sessionKey = expectDefined ( item . sessionKey , "queued session key" ) ;
5706+ expect ( removeQueuedMessage ( host , item . id ) ) . toBe ( "removed" ) ;
5707+
5708+ now = 5_500 ;
5709+ expect ( admitQueuedMessageForSession ( host , sessionKey , item ) ) . toBe ( true ) ;
5710+ expect ( setTransientQueuedMessageProjection ( host , sessionKey , item ) ) . toBe ( true ) ;
5711+ await flushChatQueueForEvent ( host ) ;
5712+
5713+ now = 6_001 ;
5714+ await flushChatQueueForEvent ( host ) ;
5715+
5716+ expect ( host . chatQueue [ 0 ] ) . toMatchObject ( {
5717+ id : item . id ,
5718+ sendState : "sending" ,
5719+ text : "reuse this delivery version" ,
5720+ } ) ;
5721+ expect ( host . lastError ) . toBeNull ( ) ;
5722+ } ) ;
5723+
56075724 it ( "coalesces duplicate queued local commands while the first command is running" , async ( ) => {
56085725 const command = createDeferred < { content : string } > ( ) ;
56095726 executeSlashCommandMock . mockImplementation ( ( ) => command . promise ) ;
0 commit comments