Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/dsh-loopx-plugin/src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,7 @@ export class LoopXContinuationDriver {

private async evaluate(state: DriverState): Promise<boolean> {
if (!this.ready(state)) return false
const session = state.agent.session
const startedAt = performance.now()
let stage: AdmissionStage = 'maintenance-slot'
let admissionSignal: AbortSignal | undefined
Expand Down Expand Up @@ -1056,6 +1057,9 @@ export class LoopXContinuationDriver {
if (!waitingForIdle && state.controller === controller) state.controller = undefined
}

// Cancellation can land after the maintenance callback has built its plan.
if (admissionSignal?.aborted || state.agent.session !== session) return false

if (plan.kind === 'wait') {
if (plan.schedulerToken !== undefined) state.schedulerToken = plan.schedulerToken
if (plan.unchangedPolls !== undefined) state.unchangedPolls = plan.unchangedPolls
Expand Down
43 changes: 43 additions & 0 deletions packages/dsh-loopx-plugin/tests/driver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2340,6 +2340,49 @@ describe('GoalBar exact-Session Driver bridge', () => {
await driver.dispose()
})

it.each([
{ plan: 'queue', shouldRun: true, interruption: 'Pause' },
{ plan: 'wait', shouldRun: false, interruption: 'Pause' },
{ plan: 'queue', shouldRun: true, interruption: 'maintenance cancellation' },
{ plan: 'wait', shouldRun: false, interruption: 'maintenance cancellation' },
])('discards a $plan plan returned after $interruption', async ({ shouldRun, interruption }) => {
const fixture = runnerFixture({ shouldRun, waitMinutes: 5 })
const clock = schedulerClock()
const host = fakeAgent()
const driver = new LoopXContinuationDriver({
runner: fixture.runner,
isLiveAgent: () => true,
retryDelaysMs: [0, 0],
clock,
})
const capture = { agent: host.agent, session: host.agent.session }
vi.spyOn(host.agent, 'runMaintenance').mockImplementationOnce(async task => {
const maintenance = new AbortController()
const plan = await task(maintenance.signal)
if (interruption === 'Pause') {
await expect(driver.cancelQueued(capture)).resolves.toEqual({ kind: 'applied' })
} else {
maintenance.abort()
}
return plan
})
try {
driver.observeAgent(host.agent)
host.appendEvent(userSkillInvocationEvent())
const interrupted = await driver.evaluateActivatedSession(capture)
expect(host.nextTurn).toHaveLength(0)
expect(clock.timers).toHaveLength(0)
expect(interrupted).toEqual({ kind: 'unavailable', reason: 'evaluation_unavailable' })

await expect(driver.evaluateActivatedSession(capture)).resolves.toEqual({ kind: 'applied' })
expect(fixture.quotaCalls).toHaveLength(2)
expect(host.nextTurn).toHaveLength(shouldRun ? 1 : 0)
expect(clock.timers).toHaveLength(shouldRun ? 0 : 1)
} finally {
await driver.dispose()
}
})

it('Pause removes only the captured Session scheduled continuation', async () => {
const fixture = runnerFixture({ shouldRun: false, waitMinutes: 5 })
const first = fakeAgent()
Expand Down
Loading