diff --git a/packages/core/agent-loop/src/agent.ts b/packages/core/agent-loop/src/agent.ts index 2d25185733..26e4fb6f45 100644 --- a/packages/core/agent-loop/src/agent.ts +++ b/packages/core/agent-loop/src/agent.ts @@ -312,7 +312,11 @@ export class ReactLoopAgent implements Agent { this.abort = controller this.acceptsNextStep = true const signal = controller.signal - const turn = this.lastTurn + 1 + // The log is the turn-number authority: out-of-band zero-step turns + // (command lifecycle on an idle log) advance it behind this cached + // counter, so re-derive the successor at open instead of trusting it. + const loggedLast = this.session.events.findLast(event => event.type === 'turn/start')?.data.turn ?? 0 + const turn = Math.max(this.lastTurn, loggedLast) + 1 let step = 0 let opened = false let reason: TurnEndReason = { kind: 'completed' } diff --git a/packages/goal/command-goal/tests/command-goal.spec.ts b/packages/goal/command-goal/tests/command-goal.spec.ts index d77c64a089..2bf06c3f3e 100644 --- a/packages/goal/command-goal/tests/command-goal.spec.ts +++ b/packages/goal/command-goal/tests/command-goal.spec.ts @@ -16,9 +16,13 @@ interface Harness { readonly plugin: Awaited> } -/** Append one idle injection using the public Agent contract. */ +/** Append one idle injection using the public Agent contract (idle inject wraps in a one-shot injection turn, per turn enclosure). */ function appendInjection(session: Session, input: UserMessageData): void { + const lastStart = session.events.findLast(event => event.type === 'turn/start') + const turn = (lastStart?.data.turn ?? 0) + 1 + session.append('turn/start', { turn, trigger: { kind: 'injection', source: input.source } }) session.append('user/message', input, { surfaceOp: 'append' }) + session.append('turn/end', { turn, reason: { kind: 'completed' } }) } /** Build a live idle agent accepted by the exact-identity goal service. */