From 6a54300e0c9d8a136d43ba8d51d00e51459ee7c5 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Mon, 30 Nov 2026 12:28:08 +0000 Subject: [PATCH] fix(conversations): Do not store the execution on a lease check-in A check-in changes only the lease. Its write of the stored execution came after the Turn's end set it idle, so it read as new work and set the status to working after the Turn ended. Co-Authored-By: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com> --- .../junior/src/chat/task-execution/store.ts | 12 ++-- .../junior/src/chat/task-execution/worker.ts | 2 - .../component/conversation-sql-store.test.ts | 63 +------------------ .../api/conversations/status.test.ts | 34 +++++++++- 4 files changed, 38 insertions(+), 73 deletions(-) diff --git a/packages/junior/src/chat/task-execution/store.ts b/packages/junior/src/chat/task-execution/store.ts index 40d92733dab..376f4f6f736 100644 --- a/packages/junior/src/chat/task-execution/store.ts +++ b/packages/junior/src/chat/task-execution/store.ts @@ -518,19 +518,17 @@ export async function startConversationWork(args: { return result; } -/** Extend the durable execution lease when the worker checks in. */ +/** + * Extend the durable execution lease when the worker checks in. It does not + * write the stored execution: after a Turn ends, that write reads as new work. + */ export async function checkInConversationWork(args: { conversationId: string; leaseToken: string; - conversationStore?: ConversationStore | undefined; nowMs?: number; state?: StateAdapter | undefined; }) { - const result = await workState.checkInConversationWork(args); - if (result) { - await recordExecutionMetadata(args); - } - return result; + return await workState.checkInConversationWork(args); } /** Drain pending mailbox entries after the caller accepts responsibility. */ diff --git a/packages/junior/src/chat/task-execution/worker.ts b/packages/junior/src/chat/task-execution/worker.ts index bae10da95aa..ecd288cc26e 100644 --- a/packages/junior/src/chat/task-execution/worker.ts +++ b/packages/junior/src/chat/task-execution/worker.ts @@ -208,7 +208,6 @@ function startLeaseCheckIn(args: { void checkInConversationWork({ conversationId: args.conversationId, leaseToken: args.leaseToken, - conversationStore: args.options.conversationStore, nowMs, state: args.options.state, }).then( @@ -463,7 +462,6 @@ async function processConversationWorkInContext( const checkedIn = await checkInConversationWork({ conversationId, leaseToken: lease.leaseToken, - conversationStore: options.conversationStore, nowMs: now(options), state: options.state, }); diff --git a/packages/junior/tests/component/conversation-sql-store.test.ts b/packages/junior/tests/component/conversation-sql-store.test.ts index 9e98068cecd..4d0c0952cdb 100644 --- a/packages/junior/tests/component/conversation-sql-store.test.ts +++ b/packages/junior/tests/component/conversation-sql-store.test.ts @@ -9,7 +9,6 @@ import { requestAnotherSlice, startConversationWork, } from "@/chat/task-execution/store"; -import { processConversationWork } from "@/chat/task-execution/worker"; import { disconnectStateAdapter, getStateAdapter } from "@/chat/state/adapter"; import { upsertTurnRecord } from "@/chat/task-execution/turn-cursor"; import { @@ -20,13 +19,7 @@ import { } from "@/db/schema"; import { eq } from "drizzle-orm"; import { readConversationFeedFromSql } from "@/api/conversations/list"; -import { - CONVERSATION_ID, - conversationQueueMessage, - createConversationWorkQueueTestAdapter, - deferred, - inboundMessage, -} from "../fixtures/conversation-work"; +import { CONVERSATION_ID, inboundMessage } from "../fixtures/conversation-work"; import { createConfiguredJuniorSqlFixture, createJuniorSqlFixture, @@ -1583,60 +1576,6 @@ ORDER BY metric } }); - it("mirrors worker check-ins into SQL execution progress", async () => { - const fixture = await createJuniorSqlFixture(); - - try { - vi.useFakeTimers({ now: 1_000 }); - await disconnectStateAdapter(); - const state = getStateAdapter(); - const store = createSqlStore(fixture.sql); - await migrateSchema(fixture.sql); - await appendInboundMessage({ - message: inboundMessage("check-in"), - conversationStore: store, - nowMs: 1_000, - state, - }); - const queue = createConversationWorkQueueTestAdapter(); - const entered = deferred(); - const finish = deferred(); - - const running = processConversationWork(conversationQueueMessage(), { - checkInIntervalMs: 15_000, - conversationStore: store, - queue, - run: async (context) => { - await context.attempt.drain(async () => {}); - entered.resolve(); - await finish.promise; - return { status: "completed" }; - }, - state, - }); - await entered.promise; - - await vi.advanceTimersByTimeAsync(15_000); - await vi.waitFor(async () => { - await expect( - store.get({ conversationId: CONVERSATION_ID }), - ).resolves.toMatchObject({ - execution: { - status: "running", - updatedAtMs: 16_000, - }, - }); - }); - - finish.resolve(); - await expect(running).resolves.toEqual({ status: "completed" }); - } finally { - vi.useRealTimers(); - await disconnectStateAdapter(); - await fixture.close(); - } - }); - it("stores a paused execution as paused", async () => { const fixture = await createJuniorSqlFixture(); diff --git a/packages/junior/tests/integration/api/conversations/status.test.ts b/packages/junior/tests/integration/api/conversations/status.test.ts index 148cf3df72c..90c26554b77 100644 --- a/packages/junior/tests/integration/api/conversations/status.test.ts +++ b/packages/junior/tests/integration/api/conversations/status.test.ts @@ -11,9 +11,15 @@ import { CONVERSATION_WORK_LEASE_TTL_MS, CONVERSATION_WORK_MAX_RETRIES, } from "@/chat/task-execution/state"; -import { startConversationWork } from "@/chat/task-execution/store"; +import { + checkInConversationWork, + startConversationWork, +} from "@/chat/task-execution/store"; import { runNextPausedTurn } from "@/chat/task-execution/paused-turn"; -import { upsertTurnRecord } from "@/chat/task-execution/turn-cursor"; +import { + recordTurnSummary, + upsertTurnRecord, +} from "@/chat/task-execution/turn-cursor"; import { createPausedTurns } from "@/chat/task-execution/turn-wake"; import { getAppContext } from "@/app/context"; import { sendVercelPluginTask } from "@/chat/plugins/task-queue"; @@ -65,6 +71,30 @@ describe("conversation status", () => { await expect(fixture.status(conversationId)).resolves.toBe("failed"); }); + it("stays done when its worker checks in after its Turn ends", async () => { + const fixture = await createStatusFixture(); + const conversationId = await fixture.start("stored-check-in"); + const work = await startConversationWork({ + conversationId, + state: fixture.state, + }); + if (work.status !== "acquired") throw new Error("Expected the lease"); + await recordTurnStart(conversationId, "stored-check-in"); + await recordTurnSummary({ + conversationId, + sliceId: 1, + state: "completed", + turnId: "stored-check-in", + }); + await recordTurnCompletion(conversationId, "stored-check-in"); + await checkInConversationWork({ + conversationId, + leaseToken: work.leaseToken, + state: fixture.state, + }); + await expect(fixture.status(conversationId)).resolves.toBe("done"); + }); + it("is failed when a paused Turn cannot resume", async () => { const fixture = await createStatusFixture(); const conversationId = await recordSlackRoot(