diff --git a/server/src/services/fresh-continuation-fallback.test.ts b/server/src/services/fresh-continuation-fallback.test.ts index b7c82ae21d5..1cc5121b76c 100644 --- a/server/src/services/fresh-continuation-fallback.test.ts +++ b/server/src/services/fresh-continuation-fallback.test.ts @@ -318,10 +318,23 @@ suite("fresh-continuation fallback", () => { // A gate after the hook must never consume the recovery: it stays pending // and is delivered once the gate clears. it("keeps the continuation pending while the agent's daily run limit declines it, then delivers it", async () => { - const f = await unfinishedRun({ runtime: "native", errorCode: "native_session_interrupted", hold: "settled_blocked" }); - await db.update(agents).set({ runtimeConfig: { heartbeat: { maxConcurrentRuns: 1, maxDailyRuns: 1 } } }).where(eq(agents.id, agentId)); + // The daily limit counts runs started in the current UTC day, and it reads + // the clock on every check. Freeze Date (timers stay real) so every check in + // this test sees the same day, even when UTC midnight passes mid-test. + const now = new Date(); + vi.useFakeTimers({ toFake: ["Date"], now }); try { - // The source run already used today's single run. + const f = await unfinishedRun({ runtime: "native", errorCode: "native_session_interrupted", hold: "settled_blocked" }); + await db.update(agents).set({ runtimeConfig: { heartbeat: { maxConcurrentRuns: 1, maxDailyRuns: 1 } } }).where(eq(agents.id, agentId)); + // Pin the runs to that day so the result does not depend on the hour: + // the source run started yesterday (as it does just after 00:00 UTC), and + // another run started today used the single run. + const todayStart = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()); + await db.update(heartbeatRuns).set({ startedAt: new Date(todayStart - 60_000), createdAt: new Date(todayStart - 120_000) }) + .where(eq(heartbeatRuns.id, f.runId)); + const [other] = await db.insert(issues).values({ companyId, title: "Used today's run", status: "done", assigneeAgentId: agentId }).returning(); + await db.insert(heartbeatRuns).values({ companyId, agentId, issueId: other!.id, status: "succeeded", + startedAt: now, finishedAt: now, contextSnapshot: { issueId: other!.id } }); expect(await sweepWake(f.issueId)).toBeNull(); await deliver(); expect(await successors(f.runId)).toHaveLength(0); @@ -332,6 +345,7 @@ suite("fresh-continuation fallback", () => { await deliver(); expect(await successors(f.runId)).toHaveLength(1); } finally { + vi.useRealTimers(); await db.update(agents).set({ runtimeConfig: { heartbeat: { maxConcurrentRuns: 1 } } }).where(eq(agents.id, agentId)); } }, 60_000);