@@ -2403,10 +2403,129 @@ describe("runCodexAppServerAttempt", () => {
24032403 } ,
24042404 } ) ;
24052405
2406+ await harness . notify (
2407+ itemNotification ( "item/started" , { type : "contextCompaction" , id : "compact-1" } ) ,
2408+ ) ;
2409+ await harness . notify (
2410+ itemNotification ( "item/completed" , { type : "contextCompaction" , id : "compact-1" } ) ,
2411+ ) ;
2412+ const planRestoreRequest = harness . requests . find (
2413+ ( request ) => request . method === "thread/inject_items" ,
2414+ ) ;
2415+ expect ( planRestoreRequest ?. params ) . toMatchObject ( {
2416+ threadId : "thread-1" ,
2417+ items : [
2418+ {
2419+ type : "message" ,
2420+ role : "user" ,
2421+ content : [
2422+ {
2423+ type : "input_text" ,
2424+ text : expect . stringContaining ( '"step":"Restore progress","status":"in_progress"' ) ,
2425+ } ,
2426+ ] ,
2427+ } ,
2428+ ] ,
2429+ } ) ;
2430+
24062431 await harness . completeTurn ( { threadId : "thread-1" , turnId : "turn-1" } ) ;
24072432 await run ;
24082433 } ) ;
24092434
2435+ it ( "does not inject plan state after compaction when the turn has no plan" , async ( ) => {
2436+ const params = createRunParams ( ) ;
2437+ const harness = createStartedThreadHarness ( ) ;
2438+ const run = runCodexAppServerAttempt ( params ) ;
2439+ await harness . waitForMethod ( "turn/start" ) ;
2440+
2441+ await harness . notify (
2442+ itemNotification ( "item/started" , { type : "contextCompaction" , id : "compact-1" } ) ,
2443+ ) ;
2444+ await harness . notify (
2445+ itemNotification ( "item/completed" , { type : "contextCompaction" , id : "compact-1" } ) ,
2446+ ) ;
2447+ expect ( harness . requests . map ( ( request ) => request . method ) ) . not . toContain ( "thread/inject_items" ) ;
2448+
2449+ await harness . completeTurn ( { threadId : "thread-1" , turnId : "turn-1" } ) ;
2450+ await run ;
2451+ } ) ;
2452+
2453+ it ( "bounds restored plan state after compaction" , async ( ) => {
2454+ const params = createRunParams ( ) ;
2455+ const harness = createStartedThreadHarness ( ) ;
2456+ const run = runCodexAppServerAttempt ( params ) ;
2457+ await harness . waitForMethod ( "turn/start" ) ;
2458+ await harness . notify ( {
2459+ method : "turn/plan/updated" ,
2460+ params : {
2461+ threadId : "thread-1" ,
2462+ turnId : "turn-1" ,
2463+ explanation : "e" . repeat ( 10_000 ) ,
2464+ plan : Array . from ( { length : 100 } , ( _ , index ) => ( {
2465+ step : `${ index } : ${ "x" . repeat ( 2_000 ) } ` ,
2466+ status : index === 0 ? "inProgress" : "pending" ,
2467+ } ) ) ,
2468+ } ,
2469+ } ) ;
2470+ await harness . notify (
2471+ itemNotification ( "item/started" , { type : "contextCompaction" , id : "compact-1" } ) ,
2472+ ) ;
2473+ await harness . notify (
2474+ itemNotification ( "item/completed" , { type : "contextCompaction" , id : "compact-1" } ) ,
2475+ ) ;
2476+
2477+ const request = harness . requests . find ( ( entry ) => entry . method === "thread/inject_items" ) ;
2478+ const text = (
2479+ request ?. params as { items ?: Array < { content ?: Array < { text ?: string } > } > } | undefined
2480+ ) ?. items ?. [ 0 ] ?. content ?. [ 0 ] ?. text ;
2481+ expect ( text ) . toBeDefined ( ) ;
2482+ const payloadText = text ?. slice ( ( text ?. indexOf ( "\n" ) ?? - 1 ) + 1 ) ?? "" ;
2483+ const payload = JSON . parse ( payloadText ) as {
2484+ explanation ?: string ;
2485+ plan : Array < { step : string ; status : string } > ;
2486+ } ;
2487+ expect ( Buffer . byteLength ( payloadText , "utf8" ) ) . toBeLessThanOrEqual ( 32 * 1024 ) ;
2488+ expect ( Buffer . byteLength ( payload . explanation ?? "" , "utf8" ) ) . toBeLessThanOrEqual ( 2 * 1024 ) ;
2489+ expect ( payload . plan . length ) . toBeLessThanOrEqual ( 50 ) ;
2490+ expect ( payload . plan . every ( ( step ) => Buffer . byteLength ( step . step , "utf8" ) <= 512 ) ) . toBe ( true ) ;
2491+ expect ( payload . plan [ 0 ] ?. status ) . toBe ( "in_progress" ) ;
2492+
2493+ await harness . completeTurn ( { threadId : "thread-1" , turnId : "turn-1" } ) ;
2494+ await run ;
2495+ } ) ;
2496+
2497+ it ( "continues the turn when restoring plan state after compaction fails" , async ( ) => {
2498+ const params = createRunParams ( ) ;
2499+ const harness = createStartedThreadHarness ( async ( method ) => {
2500+ if ( method === "thread/inject_items" ) {
2501+ throw new Error ( "injected test failure" ) ;
2502+ }
2503+ return undefined ;
2504+ } ) ;
2505+ const run = runCodexAppServerAttempt ( params ) ;
2506+ await harness . waitForMethod ( "turn/start" ) ;
2507+ await harness . notify ( {
2508+ method : "turn/plan/updated" ,
2509+ params : {
2510+ threadId : "thread-1" ,
2511+ turnId : "turn-1" ,
2512+ explanation : "Keep working" ,
2513+ plan : [ { step : "Finish safely" , status : "inProgress" } ] ,
2514+ } ,
2515+ } ) ;
2516+ await harness . notify (
2517+ itemNotification ( "item/started" , { type : "contextCompaction" , id : "compact-1" } ) ,
2518+ ) ;
2519+ await harness . notify (
2520+ itemNotification ( "item/completed" , { type : "contextCompaction" , id : "compact-1" } ) ,
2521+ ) ;
2522+ expect ( harness . requests . map ( ( request ) => request . method ) ) . toContain ( "thread/inject_items" ) ;
2523+
2524+ await harness . completeTurn ( { threadId : "thread-1" , turnId : "turn-1" } ) ;
2525+ const result = await run ;
2526+ expect ( readAttemptTerminal ( result ) . promptError ) . toBeNull ( ) ;
2527+ } ) ;
2528+
24102529 it ( "fails closed for Codex app defaults when restricted native tools have no plugin config" , async ( ) => {
24112530 testing . setOpenClawCodingToolsFactoryForTests ( ( ) => [ createRuntimeDynamicTool ( "message" ) ] ) ;
24122531 const params = createRunParams ( ) ;
0 commit comments