@@ -4,6 +4,7 @@ import { tmpdir } from "node:os";
44import { join } from "node:path" ;
55import { afterEach , beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
66import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js" ;
7+ import { SessionWriteLockStaleError } from "../../agents/session-write-lock-error.js" ;
78import { clearRuntimeConfigSnapshot , setRuntimeConfigSnapshot } from "../../config/config.js" ;
89import type { SessionEntry } from "../../config/sessions.js" ;
910import {
@@ -24,6 +25,7 @@ import {
2425import { createUserTurnTranscriptRecorder } from "../../sessions/user-turn-transcript.js" ;
2526import { createTestUserTurnTranscriptTarget } from "../../sessions/user-turn-transcript.test-support.js" ;
2627import type { TemplateContext } from "../templating.js" ;
28+ import { SILENT_REPLY_TOKEN } from "../tokens.js" ;
2729import type { GetReplyOptions } from "../types.js" ;
2830import {
2931 GENERIC_EXTERNAL_RUN_FAILURE_TEXT ,
@@ -2251,6 +2253,77 @@ describe("runReplyAgent pending final delivery capture", () => {
22512253 }
22522254 } ) ;
22532255
2256+ it ( "preserves an adopted restart claim when the replacement takes the SQLite lease" , async ( ) => {
2257+ const sessionEntry : SessionEntry = {
2258+ abortedLastRun : false ,
2259+ restartRecoveryDeliveryRequestFingerprint : "request-fingerprint" ,
2260+ restartRecoveryDeliveryRunId : "msg" ,
2261+ restartRecoveryDeliverySourceRunId : "control-ui-run" ,
2262+ sessionId : "session" ,
2263+ status : "running" ,
2264+ updatedAt : Date . now ( ) ,
2265+ } ;
2266+ const sessionStore = { main : sessionEntry } ;
2267+ const storePath = await createSessionStoreFile ( sessionEntry ) ;
2268+ const replyOperation = createReplyOperation ( {
2269+ sessionKey : "main" ,
2270+ sessionId : "session" ,
2271+ resetTriggered : false ,
2272+ } ) ;
2273+ replyOperation . setPhase ( "running" ) ;
2274+ state . runEmbeddedAgentMock . mockImplementationOnce ( async ( ) => {
2275+ const current = await readStoredMainSession ( storePath ) ;
2276+ expect ( current . restartRecoveryDeliveryRunId ) . toBe ( "msg" ) ;
2277+ await replaceSessionEntry (
2278+ { storePath, sessionKey : "main" } ,
2279+ {
2280+ ...current ,
2281+ abortedLastRun : true ,
2282+ status : "killed" ,
2283+ updatedAt : Date . now ( ) ,
2284+ } ,
2285+ ) ;
2286+ throw new SessionWriteLockStaleError ( {
2287+ lockPath : "sqlite:session-write:agent:main:main" ,
2288+ owner : "replacement gateway" ,
2289+ staleReasons : [ "lease-lost" ] ,
2290+ } ) ;
2291+ } ) ;
2292+
2293+ try {
2294+ const { run } = createMinimalRun ( {
2295+ replyOperation,
2296+ sessionCtx : {
2297+ Provider : "webchat" ,
2298+ OriginatingChannel : "webchat" ,
2299+ } ,
2300+ runOverrides : { agentId : "main" , messageProvider : "webchat" } ,
2301+ sessionEntry,
2302+ sessionStore,
2303+ sessionKey : "main" ,
2304+ storePath,
2305+ } ) ;
2306+
2307+ await expect ( run ( ) ) . resolves . toEqual ( { text : SILENT_REPLY_TOKEN } ) ;
2308+
2309+ expect ( replyOperation . result ) . toEqual ( {
2310+ kind : "aborted" ,
2311+ code : "aborted_for_restart" ,
2312+ } ) ;
2313+ expect ( await readStoredMainSession ( storePath ) ) . toMatchObject ( {
2314+ abortedLastRun : true ,
2315+ restartRecoveryDeliveryRunId : "msg" ,
2316+ restartRecoveryDeliverySourceRunId : "control-ui-run" ,
2317+ status : "killed" ,
2318+ } ) ;
2319+ expect (
2320+ ( await readStoredMainSession ( storePath ) ) . restartRecoveryTerminalRunIds ,
2321+ ) . toBeUndefined ( ) ;
2322+ } finally {
2323+ replyOperation . complete ( ) ;
2324+ }
2325+ } ) ;
2326+
22542327 it ( "fires onAdopted after restart recovery delivery context persist completes" , async ( ) => {
22552328 const sessionEntry : SessionEntry = {
22562329 sessionId : "session" ,
0 commit comments