11// Codex tests cover run attempt.steering plugin behavior.
22import path from "node:path" ;
33import { GPT5_BEHAVIOR_CONTRACT as CODEX_GPT5_BEHAVIOR_CONTRACT } from "openclaw/plugin-sdk/provider-model-shared" ;
4+ import { upsertSessionEntry } from "openclaw/plugin-sdk/session-store-runtime" ;
5+ import {
6+ appendSessionTranscriptMessageByIdentity ,
7+ readSessionTranscriptEvents ,
8+ } from "openclaw/plugin-sdk/session-transcript-runtime" ;
49import { describe , expect , it , vi } from "vitest" ;
10+ import type { CodexSteeringQueueOptions } from "./attempt-steering.js" ;
511import { readAttemptTerminal } from "./attempt-terminal.test-helper.js" ;
612import type { CodexServerNotification } from "./protocol.js" ;
713import {
@@ -173,13 +179,63 @@ describe("runCodexAppServerAttempt steering", () => {
173179 it ( "accepts Gateway transcript-backed steering for the active Codex turn" , async ( ) => {
174180 const { requests, waitForMethod, completeTurn, notify } = createStartedThreadHarness ( ) ;
175181 const params = createSteeringParams ( ) ;
182+ const storePath = path . join ( tempDir , `${ params . sessionId } .sqlite` ) ;
183+ const sessionTarget = {
184+ agentId : "main" ,
185+ sessionId : params . sessionId ,
186+ sessionKey : params . sessionKey ! ,
187+ storePath,
188+ } ;
176189 params . taskSuggestionDeliveryMode = "gateway" ;
190+ params . sessionTarget = sessionTarget ;
191+ await upsertSessionEntry ( {
192+ agentId : "main" ,
193+ sessionKey : params . sessionKey ! ,
194+ storePath,
195+ entry : {
196+ sessionFile : params . sessionFile ,
197+ sessionId : params . sessionId ,
198+ updatedAt : Date . now ( ) ,
199+ } ,
200+ } ) ;
201+ let steerPersisted = false ;
202+ const userTurnTranscriptRecorder = {
203+ persistApproved : vi . fn ( async ( ) => {
204+ if ( steerPersisted ) {
205+ return undefined ;
206+ }
207+ steerPersisted = true ;
208+ return await appendSessionTranscriptMessageByIdentity ( {
209+ ...sessionTarget ,
210+ message : {
211+ role : "user" ,
212+ content : "steer this active turn" ,
213+ timestamp : Date . now ( ) ,
214+ idempotencyKey : `${ params . runId } :steer:user` ,
215+ } ,
216+ } ) ;
217+ } ) ,
218+ hasPersisted : ( ) => steerPersisted ,
219+ } as unknown as NonNullable < CodexSteeringQueueOptions [ "userTurnTranscriptRecorder" ] > ;
177220
178221 const run = runCodexAppServerAttempt ( params , {
179222 pluginConfig : { appServer : { mode : "yolo" } } ,
180223 } ) ;
181224 await waitForMethod ( "turn/start" ) ;
182225 const onQueueAccepted = vi . fn ( ) ;
226+ await notify ( {
227+ method : "item/completed" ,
228+ params : {
229+ threadId : "thread-1" ,
230+ turnId : "turn-1" ,
231+ item : {
232+ type : "agentMessage" ,
233+ id : "pre-steer-commentary" ,
234+ phase : "commentary" ,
235+ text : "PRE-STEER-COMMENTARY" ,
236+ } ,
237+ } ,
238+ } ) ;
183239
184240 await vi . waitFor ( ( ) => {
185241 expect (
@@ -198,6 +254,7 @@ describe("runCodexAppServerAttempt steering", () => {
198254 taskSuggestionDeliveryMode : "gateway" ,
199255 waitForTranscriptCommit : true ,
200256 onQueueAccepted,
257+ userTurnTranscriptRecorder,
201258 } ) ;
202259 await vi . waitFor (
203260 ( ) => expect ( requests . map ( ( entry ) => entry . method ) ) . toContain ( "turn/steer" ) ,
@@ -219,6 +276,20 @@ describe("runCodexAppServerAttempt steering", () => {
219276 item : { id : "steered-user-message" , type : "userMessage" , clientId : clientUserMessageId } ,
220277 } ,
221278 } ) ;
279+ await userTurnTranscriptRecorder . persistApproved ( ) ;
280+ await notify ( {
281+ method : "item/completed" ,
282+ params : {
283+ threadId : "thread-1" ,
284+ turnId : "turn-1" ,
285+ item : {
286+ type : "agentMessage" ,
287+ id : "final-answer" ,
288+ phase : "final_answer" ,
289+ text : "Steering completed." ,
290+ } ,
291+ } ,
292+ } ) ;
222293 await completeTurn ( { threadId : "thread-1" , turnId : "turn-1" } ) ;
223294 await run ;
224295
@@ -227,6 +298,11 @@ describe("runCodexAppServerAttempt steering", () => {
227298 expectedTurnId : "turn-1" ,
228299 input : [ { type : "text" , text : "steer this active turn" } ] ,
229300 } ) ;
301+ const roles = ( await readSessionTranscriptEvents ( sessionTarget ) ) . flatMap ( ( event ) => {
302+ const message = ( event as { message ?: { role ?: string } } ) . message ;
303+ return message ?. role ? [ message . role ] : [ ] ;
304+ } ) ;
305+ expect ( roles ) . toEqual ( [ "user" , "assistant" , "user" , "assistant" ] ) ;
230306 } ) ;
231307
232308 it ( "forwards queued text and images to the active app-server turn" , async ( ) => {
0 commit comments