@@ -5,6 +5,11 @@ import { persistSessionTranscriptTurn } from "../../config/sessions/session-acce
55import { applyAssistantDeliveryDirectives } from "../../config/sessions/transcript-assistant-delivery.js" ;
66import type { OpenClawConfig } from "../../config/types.openclaw.js" ;
77import { formatErrorMessage } from "../../infra/errors.js" ;
8+ import {
9+ readSessionTranscriptRunId ,
10+ resolveTerminalAssistantTranscriptRunId ,
11+ } from "../../sessions/transcript-events.js" ;
12+ import { extractAssistantPhaseText } from "../../shared/chat-message-content.js" ;
813
914type AppendMessageArg = Parameters < SessionManager [ "appendMessage" ] > [ 0 ] ;
1015type AssistantMessageContent = Extract < AppendMessageArg , { role : "assistant" } > [ "content" ] ;
@@ -21,6 +26,8 @@ type GatewayInjectedTranscriptAppendResult = {
2126 ok : boolean ;
2227 messageId ?: string ;
2328 message ?: Record < string , unknown > ;
29+ /** Set when the commit predicate declined the append; not an error. */
30+ skipped ?: boolean ;
2431 error ?: string ;
2532} ;
2633
@@ -108,6 +115,7 @@ export async function appendInjectedAssistantMessageToTranscript(params: {
108115 content : [ { type : "text" , text : params . message } ] ,
109116 } ;
110117 const rawDeliveryFacts = applyAssistantDeliveryDirectives ( rawDeliveryMessage ) . openclawDelivery ;
118+ const abortRunId = params . abortMeta ?. runId ;
111119 const messageBody : AppendMessageArg & Record < string , unknown > = applyAssistantDeliveryDirectives ( {
112120 role : "assistant" ,
113121 // Gateway-injected assistant messages can include non-model content blocks (e.g. embedded TTS audio).
@@ -141,6 +149,7 @@ export async function appendInjectedAssistantMessageToTranscript(params: {
141149 if ( ! params . transcriptPath && ( ! params . storePath || ! params . sessionId || ! params . sessionKey ) ) {
142150 return { ok : false , error : "transcript identity not resolved" } ;
143151 }
152+ let predicateDeclined = false ;
144153 const turn = await persistSessionTranscriptTurn (
145154 {
146155 sessionKey : params . sessionKey ?? "" ,
@@ -158,6 +167,22 @@ export async function appendInjectedAssistantMessageToTranscript(params: {
158167 {
159168 message : messageBody ,
160169 idempotencyLookup : "scan-assistant" ,
170+ ...( params . abortMeta
171+ ? {
172+ shouldAppendInTransaction : ( latestAssistantMessage : unknown ) => {
173+ const committedRunId = resolveTerminalAssistantTranscriptRunId (
174+ latestAssistantMessage ,
175+ readSessionTranscriptRunId ( latestAssistantMessage ) ,
176+ ) ;
177+ const committedText = extractAssistantPhaseText ( latestAssistantMessage ) ?. trim ( ) ;
178+ // The same run can commit before its live buffer clears. Recheck after
179+ // BEGIN IMMEDIATE so a direct writer cannot land between this fact and insert.
180+ predicateDeclined =
181+ committedRunId === abortRunId && committedText === params . message . trim ( ) ;
182+ return ! predicateDeclined ;
183+ } ,
184+ }
185+ : { } ) ,
161186 now,
162187 useRawWhenLinear : true ,
163188 } ,
@@ -166,6 +191,10 @@ export async function appendInjectedAssistantMessageToTranscript(params: {
166191 ) ;
167192 const appended = turn . messages [ 0 ] ;
168193 if ( ! appended ) {
194+ // A declined predicate is a decision, not a failure: no row was wanted.
195+ if ( predicateDeclined ) {
196+ return { ok : true , skipped : true } ;
197+ }
169198 return { ok : false , error : "gateway-injected assistant message was not appended" } ;
170199 }
171200 return {
0 commit comments