@@ -33,6 +33,7 @@ import { waitForSessionTranscriptIndexReconcile } from "../../config/sessions/se
3333import { resolveMirroredTranscriptText } from "../../config/sessions/transcript-mirror.js" ;
3434import { withOwnedSessionTranscriptWrites } from "../../config/sessions/transcript-write-context.js" ;
3535import { getAgentRunContext } from "../../infra/agent-run-registry.js" ;
36+ import { RUN_STALE_TAKEOVER_MS } from "../../logging/diagnostic-run-activity.js" ;
3637import { runExclusiveSessionLifecycleMutation } from "../../sessions/session-lifecycle-admission.js" ;
3738import {
3839 disposeOpenClawAgentDatabaseByPath ,
@@ -1358,7 +1359,7 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
13581359 expect ( context . addChatRun ) . not . toHaveBeenCalled ( ) ;
13591360 } ) ;
13601361
1361- it ( "allows an explicit steer after its active owner advances the transcript leaf " , async ( ) => {
1362+ it ( "allows an explicit steer during injectable non-streaming tool work " , async ( ) => {
13621363 await createGatewayUserTurnSqliteFixture ( "openclaw-chat-send-steer-moving-leaf-" ) ;
13631364 await appendTranscriptMessage ( transcriptScope ( ) , {
13641365 eventId : "current-leaf" ,
@@ -1377,7 +1378,8 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
13771378 operation . attachBackend ( {
13781379 kind : "embedded" ,
13791380 cancel : ( ) => { } ,
1380- isStreaming : ( ) => true ,
1381+ isStreaming : ( ) => false ,
1382+ isStopped : ( ) => false ,
13811383 queueMessage : async ( ) => { } ,
13821384 } ) ;
13831385
@@ -1403,6 +1405,54 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
14031405 expect ( mockState . lastDispatchOriginatingLeafEntryId ) . toBe ( "leaf-before-active-run-output" ) ;
14041406 } ) ;
14051407
1408+ it ( "rejects a moved-leaf steer when the non-streaming owner evidence is stale" , async ( ) => {
1409+ await createGatewayUserTurnSqliteFixture ( "openclaw-chat-send-steer-stale-owner-" ) ;
1410+ await appendTranscriptMessage ( transcriptScope ( ) , {
1411+ eventId : "current-leaf" ,
1412+ message : { role : "assistant" , content : "stale tool work" } ,
1413+ now : 1 ,
1414+ parentId : null ,
1415+ } ) ;
1416+ const { context, respond, send } = createChatRequestFixture ( ) ;
1417+ vi . useFakeTimers ( { toFake : [ "Date" ] } ) ;
1418+ const operation = replyRunRegistry . begin ( {
1419+ sessionKey : "main" ,
1420+ sessionId : mockState . sessionId ,
1421+ resetTriggered : false ,
1422+ originatingLeafEntryId : "leaf-before-stale-run-output" ,
1423+ } ) ;
1424+ operation . setPhase ( "running" ) ;
1425+ operation . attachBackend ( {
1426+ kind : "embedded" ,
1427+ cancel : ( ) => { } ,
1428+ isStreaming : ( ) => false ,
1429+ isStopped : ( ) => false ,
1430+ queueMessage : async ( ) => { } ,
1431+ } ) ;
1432+
1433+ try {
1434+ vi . advanceTimersByTime ( RUN_STALE_TAKEOVER_MS + 1 ) ;
1435+ await send ( {
1436+ idempotencyKey : "idem-steer-stale-owner" ,
1437+ requestParams : {
1438+ expectedLeafEntryId : "leaf-before-stale-run-output" ,
1439+ queueMode : "steer" ,
1440+ } ,
1441+ waitFor : "none" ,
1442+ } ) ;
1443+ } finally {
1444+ operation . complete ( ) ;
1445+ vi . useRealTimers ( ) ;
1446+ }
1447+
1448+ expect ( lastRespondCall ( respond ) ) . toEqual ( [
1449+ false ,
1450+ undefined ,
1451+ expect . objectContaining ( { details : { reason : "active-leaf-changed" } } ) ,
1452+ ] ) ;
1453+ expect ( context . addChatRun ) . not . toHaveBeenCalled ( ) ;
1454+ } ) ;
1455+
14061456 it ( "rejects a stale explicit steer when a different leaf owns the active run" , async ( ) => {
14071457 await createGatewayUserTurnSqliteFixture ( "openclaw-chat-send-steer-different-owner-" ) ;
14081458 await appendTranscriptMessage ( transcriptScope ( ) , {
0 commit comments