@@ -29,6 +29,10 @@ import * as EventSink from "./EventSink.ts";
2929import * as Orchestrator from "./Orchestrator.ts" ;
3030import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts" ;
3131import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts" ;
32+ import * as ThreadManagementService from "./ThreadManagementService.ts" ;
33+ import * as ServerSettings from "../serverSettings.ts" ;
34+ import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
35+ import * as CommandReceiptStore from "./CommandReceiptStore.ts" ;
3236import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace" ;
3337import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter" ;
3438import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests" ;
@@ -2181,6 +2185,147 @@ it.effect("starts a goal that names no model on the thread's model ahead of a qu
21812185 ) ,
21822186) ;
21832187
2188+ it . effect ( "restarts by stopping the running turn when the provider cannot interrupt-restart" , ( ) =>
2189+ Effect . scoped (
2190+ Effect . gen ( function * ( ) {
2191+ const { started, steered, layer, startFirstTurn, send, read } = yield * goalHarness (
2192+ "restart-without-interrupt-restart" ,
2193+ ) ;
2194+ yield * Effect . gen ( function * ( ) {
2195+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
2196+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
2197+ const threadId = yield * startFirstTurn ;
2198+ yield * send ( "queued" , "queued" , "queue" ) ;
2199+ yield * orchestrator . dispatch ( {
2200+ type : "message.dispatch" ,
2201+ commandId : CommandId . make ( "restart" ) ,
2202+ threadId,
2203+ messageId : MessageId . make ( "message:restart" ) ,
2204+ text : "do this instead" ,
2205+ attachments : [ ] ,
2206+ modelSelection : runSelection ,
2207+ dispatchMode : { type : "restart_active" , targetRunId : started [ 0 ] ! . runId } ,
2208+ createdBy : "agent" ,
2209+ creationSource : "server" ,
2210+ } ) ;
2211+ yield * worker . drain ( ) ;
2212+
2213+ assert . deepEqual ( steered , [ ] ) ;
2214+ assert . deepEqual ( yield * read ( threadId ) , {
2215+ queue : [ "queued" ] ,
2216+ started : [ "first" , "do this instead" ] ,
2217+ stopRequested : true ,
2218+ } ) ;
2219+ } ) . pipe ( Effect . provide ( layer ) ) ;
2220+ } ) ,
2221+ ) ,
2222+ ) ;
2223+
2224+ it . effect ( "starts a restart that stops the running turn ahead of an older held queue" , ( ) =>
2225+ Effect . scoped (
2226+ Effect . gen ( function * ( ) {
2227+ const { started, layer, startFirstTurn, send, read } =
2228+ yield * goalHarness ( "restart-past-held-queue" ) ;
2229+ yield * Effect . gen ( function * ( ) {
2230+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
2231+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
2232+ const threadId = yield * startFirstTurn ;
2233+ yield * send ( "queued" , "queued" , "queue" ) ;
2234+ yield * orchestrator . dispatch ( {
2235+ type : "thread.stop" ,
2236+ commandId : CommandId . make ( "stop" ) ,
2237+ threadId,
2238+ } ) ;
2239+ yield * worker . drain ( ) ;
2240+ const secondRunning = yield * orchestrator . streamDomainEvents . pipe (
2241+ Stream . filter (
2242+ ( event ) => event . type === "provider-turn.updated" && event . payload . status === "running" ,
2243+ ) ,
2244+ Stream . take ( 1 ) ,
2245+ Stream . runDrain ,
2246+ Effect . forkScoped ,
2247+ ) ;
2248+ yield * orchestrator . dispatch ( {
2249+ type : "message.dispatch" ,
2250+ commandId : CommandId . make ( "second" ) ,
2251+ threadId,
2252+ messageId : MessageId . make ( "message:second" ) ,
2253+ text : "second" ,
2254+ attachments : [ ] ,
2255+ dispatchMode : { type : "start_immediately" } ,
2256+ createdBy : "agent" ,
2257+ creationSource : "mcp" ,
2258+ } ) ;
2259+ yield * worker . drain ( ) ;
2260+ yield * Fiber . join ( secondRunning ) ;
2261+ yield * orchestrator . dispatch ( {
2262+ type : "message.dispatch" ,
2263+ commandId : CommandId . make ( "restart" ) ,
2264+ threadId,
2265+ messageId : MessageId . make ( "message:restart" ) ,
2266+ text : "do this instead" ,
2267+ attachments : [ ] ,
2268+ dispatchMode : { type : "restart_active" , targetRunId : started [ 1 ] ! . runId } ,
2269+ createdBy : "agent" ,
2270+ creationSource : "mcp" ,
2271+ } ) ;
2272+ yield * worker . drain ( ) ;
2273+
2274+ const { queue, started : startedTexts } = yield * read ( threadId ) ;
2275+ assert . deepEqual (
2276+ { queue, started : startedTexts } ,
2277+ { queue : [ "queued" ] , started : [ "first" , "second" , "do this instead" ] } ,
2278+ ) ;
2279+ } ) . pipe ( Effect . provide ( layer ) ) ;
2280+ } ) ,
2281+ ) ,
2282+ ) ;
2283+
2284+ it . effect ( "reports a restart that stops the running turn as restarted" , ( ) =>
2285+ Effect . scoped (
2286+ Effect . gen ( function * ( ) {
2287+ const { started, layer, startFirstTurn } = yield * goalHarness ( "restart-send-delivery" ) ;
2288+ yield * Effect . gen ( function * ( ) {
2289+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
2290+ const threadManagement = yield * ThreadManagementService . ThreadManagementService ;
2291+ const threadId = yield * startFirstTurn ;
2292+ const input = {
2293+ projectId : ProjectId . make ( "project:restart-send-delivery" ) ,
2294+ commandId : CommandId . make ( "restart" ) ,
2295+ threadId,
2296+ messageId : MessageId . make ( "message:restart" ) ,
2297+ text : "do this instead" ,
2298+ attachments : [ ] ,
2299+ mode : "restart" ,
2300+ createdBy : "agent" ,
2301+ creationSource : "mcp" ,
2302+ } as const ;
2303+ const sent = yield * threadManagement . sendToThread ( input ) ;
2304+ // Stop holds the replacement before it starts; a replay must still report the restart.
2305+ yield * orchestrator . dispatch ( {
2306+ type : "thread.stop" ,
2307+ commandId : CommandId . make ( "stop-after-restart" ) ,
2308+ threadId,
2309+ } ) ;
2310+ const replayed = yield * threadManagement . sendToThread ( input ) ;
2311+
2312+ assert . equal ( started . length , 1 ) ;
2313+ assert . deepEqual ( [ sent . delivery , replayed . delivery ] , [ "restarted" , "restarted" ] ) ;
2314+ } ) . pipe (
2315+ Effect . provide (
2316+ ThreadManagementService . layer . pipe (
2317+ Layer . provideMerge ( layer ) ,
2318+ Layer . provide ( ServerSettings . layerTest ( ) ) ,
2319+ Layer . provide (
2320+ CommandReceiptStore . layer . pipe ( Layer . provide ( SqlitePersistence . layerMemory ) ) ,
2321+ ) ,
2322+ ) ,
2323+ ) ,
2324+ ) ;
2325+ } ) ,
2326+ ) ,
2327+ ) ;
2328+
21842329it . effect ( "queues a goal clear without stopping work when no goal drives it" , ( ) =>
21852330 Effect . scoped (
21862331 Effect . gen ( function * ( ) {
0 commit comments