1- import { describe , expect , it , vi } from "vitest" ;
1+ import { beforeEach , describe , expect , it , vi } from "vitest" ;
22import {
33 createEmbeddedRunReplayState ,
44 type EmbeddedRunReplayState ,
77import { dispatchEmbeddedRunAttempt } from "./run/run-attempt-dispatch.js" ;
88
99const mocks = vi . hoisted ( ( ) => ( {
10- runAttempt : vi . fn ( async ( ) => ( { terminal : { kind : "ok" } } ) ) ,
10+ runAttempt : vi . fn ( ) ,
11+ settleRequesterAfterSessionSpawns : vi . fn ( ) ,
1112} ) ) ;
1213
1314vi . mock ( "../delegation-capability.js" , ( ) => ( {
@@ -30,8 +31,13 @@ vi.mock("./run/attempt-exec-approval-continuation.js", () => ({
3031 } ) ) ,
3132} ) ) ;
3233
33- vi . mock ( "./run/backend.js" , ( ) => ( {
34- runEmbeddedAttemptWithBackend : mocks . runAttempt ,
34+ vi . mock ( "../harness/selection.js" , ( ) => ( {
35+ runAgentHarnessAttempt : mocks . runAttempt ,
36+ runAgentHarnessSettledTurnFinalization : vi . fn ( ) ,
37+ } ) ) ;
38+
39+ vi . mock ( "../subagent-registry.js" , ( ) => ( {
40+ settleRequesterAfterSessionSpawns : mocks . settleRequesterAfterSessionSpawns ,
3541} ) ) ;
3642
3743vi . mock ( "./run/plugin-harness-prompt-images.js" , ( ) => ( {
@@ -120,6 +126,11 @@ function makeDispatchInput(
120126}
121127
122128describe ( "embedded run retry dispatch" , ( ) => {
129+ beforeEach ( ( ) => {
130+ mocks . runAttempt . mockReset ( ) . mockResolvedValue ( { terminal : { kind : "ok" } } ) ;
131+ mocks . settleRequesterAfterSessionSpawns . mockReset ( ) ;
132+ } ) ;
133+
123134 it ( "preserves caller-owned session and unsafe replay state on the next attempt" , async ( ) => {
124135 const sessionManager = { owner : "caller" } ;
125136 const replayState = observeReplayMetadata (
@@ -137,5 +148,33 @@ describe("embedded run retry dispatch", () => {
137148 expect ( replayState ) . toEqual ( { replayInvalid : true , hadPotentialSideEffects : true } ) ;
138149 expect ( result . preparedAttempt . initialReplayState ) . toBe ( replayState ) ;
139150 expect ( mocks . runAttempt ) . toHaveBeenCalledWith ( result . preparedAttempt ) ;
151+ expect ( mocks . settleRequesterAfterSessionSpawns ) . not . toHaveBeenCalled ( ) ;
140152 } ) ;
153+
154+ it . each ( [ true , false ] ) (
155+ "settles accepted spawns before a late post-compaction abort (yielded: %s)" ,
156+ async ( yieldDetected ) => {
157+ const postCompactionAbortError = new Error ( "post-compaction loop detected" ) ;
158+ const input = makeDispatchInput ( { } , createEmbeddedRunReplayState ( ) ) ;
159+ input . control . getPostCompactionAbortError = vi . fn ( ( ) => postCompactionAbortError ) ;
160+ const acceptedSessionSpawns = [
161+ { runId : "child-run" , childSessionKey : "agent:main:subagent:child" } ,
162+ ] ;
163+ mocks . runAttempt . mockResolvedValueOnce ( {
164+ terminal : { kind : "ok" } ,
165+ agentHarnessId : "codex" ,
166+ yieldDetected,
167+ acceptedSessionSpawns,
168+ } ) ;
169+
170+ await expect ( dispatchEmbeddedRunAttempt ( input ) ) . rejects . toBe ( postCompactionAbortError ) ;
171+
172+ expect ( mocks . settleRequesterAfterSessionSpawns ) . toHaveBeenCalledWith ( {
173+ requesterSessionKey : "agent:main:session-1" ,
174+ requesterTurnRunId : "run-1" ,
175+ requesterYielded : yieldDetected ,
176+ acceptedSessionSpawns,
177+ } ) ;
178+ } ,
179+ ) ;
141180} ) ;
0 commit comments