@@ -21,6 +21,8 @@ import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
2121import { assert , it } from "@effect/vitest" ;
2222import * as Effect from "effect/Effect" ;
2323import * as DateTime from "effect/DateTime" ;
24+ import * as Deferred from "effect/Deferred" ;
25+ import * as Fiber from "effect/Fiber" ;
2426import * as Layer from "effect/Layer" ;
2527import * as Stdio from "effect/Stdio" ;
2628import * as Stream from "effect/Stream" ;
@@ -29,6 +31,7 @@ import * as HttpServer from "effect/unstable/http/HttpServer";
2931import * as HttpApi from "effect/unstable/httpapi/HttpApi" ;
3032import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder" ;
3133import * as CliError from "effect/unstable/cli/CliError" ;
34+ import * as TestClock from "effect/testing/TestClock" ;
3235import * as TestConsole from "effect/testing/TestConsole" ;
3336import { Command } from "effect/unstable/cli" ;
3437
@@ -43,6 +46,7 @@ import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
4346import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts" ;
4447import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngine.ts" ;
4548import { OrchestrationLayerLive } from "./orchestration/runtimeLayer.ts" ;
49+ import { OrchestrationCommandInvariantError } from "./orchestration/Errors.ts" ;
4650import { orchestrationHttpApiLayer } from "./orchestration/http.ts" ;
4751import * as ProjectCloneTracker from "./project/ProjectCloneTracker.ts" ;
4852import { layerConfig as SqlitePersistenceLayerLive } from "./persistence/Layers/Sqlite.ts" ;
@@ -366,9 +370,25 @@ it.layer(NodeServices.layer)("project lookup with unavailable workspaces", (it)
366370 ) ;
367371} ) ;
368372
369- const withLiveProjectCliServer = < A , E , R > ( baseDir : string , run : ( ) => Effect . Effect < A , E , R > ) =>
373+ type EngineDispatch = OrchestrationEngine . OrchestrationEngineService [ "Service" ] [ "dispatch" ] ;
374+
375+ const withLiveProjectCliServer = < A , E , R > (
376+ baseDir : string ,
377+ run : ( ) => Effect . Effect < A , E , R > ,
378+ wrapDispatch ?: ( dispatch : EngineDispatch ) => EngineDispatch ,
379+ ) =>
370380 Effect . gen ( function * ( ) {
371381 const config = yield * makeCliTestServerConfig ( baseDir ) ;
382+ const dispatchOverrideLayer =
383+ wrapDispatch === undefined
384+ ? Layer . empty
385+ : Layer . effect (
386+ OrchestrationEngine . OrchestrationEngineService ,
387+ Effect . map ( OrchestrationEngine . OrchestrationEngineService , ( engine ) => ( {
388+ ...engine ,
389+ dispatch : wrapDispatch ( engine . dispatch ) ,
390+ } ) ) ,
391+ ) ;
372392 const routesLayer = HttpApiBuilder . layer ( ProjectCliHttpApi ) . pipe (
373393 Layer . provide (
374394 orchestrationHttpApiLayer . pipe (
@@ -378,6 +398,7 @@ const withLiveProjectCliServer = <A, E, R>(baseDir: string, run: () => Effect.Ef
378398 discard : ( ) => Effect . void ,
379399 } ) ,
380400 ) ,
401+ Layer . provide ( dispatchOverrideLayer ) ,
381402 ) ,
382403 ) ,
383404 Layer . provide ( environmentAuthenticatedAuthLayer ) ,
@@ -900,6 +921,7 @@ const withThreadStartFixture = <A, E, R>(
900921 readonly baseDir : string ;
901922 readonly workspaceRoot : string ;
902923 } ) => Effect . Effect < A , E , R > ,
924+ wrapDispatch ?: ( dispatch : EngineDispatch ) => EngineDispatch ,
903925) =>
904926 Effect . gen ( function * ( ) {
905927 const baseDir = NodeFS . mkdtempSync ( NodePath . join ( NodeOS . tmpdir ( ) , "t3-cli-thread-state-" ) ) ;
@@ -936,7 +958,11 @@ const withThreadStartFixture = <A, E, R>(
936958 }
937959 }` ,
938960 ) ;
939- return yield * withLiveProjectCliServer ( baseDir , ( ) => run ( { baseDir, workspaceRoot } ) ) ;
961+ return yield * withLiveProjectCliServer (
962+ baseDir ,
963+ ( ) => run ( { baseDir, workspaceRoot } ) ,
964+ wrapDispatch ,
965+ ) ;
940966 } ) ;
941967
942968const readProjectThreads = ( workspaceRoot : string ) =>
@@ -1165,3 +1191,66 @@ it.layer(NodeServices.layer)("thread send", (it) => {
11651191 ) ,
11661192 ) ;
11671193} ) ;
1194+
1195+ it . layer ( NodeServices . layer ) ( "thread start rollback" , ( it ) => {
1196+ const startArgs = ( baseDir : string , workspaceRoot : string ) => [
1197+ "thread" ,
1198+ "start" ,
1199+ workspaceRoot ,
1200+ "Fix the flaky test" ,
1201+ "--base-dir" ,
1202+ baseDir ,
1203+ ] ;
1204+
1205+ it . effect ( "deletes the new thread when the server rejects the turn" , ( ) =>
1206+ withThreadStartFixture (
1207+ ( { baseDir, workspaceRoot } ) =>
1208+ Effect . gen ( function * ( ) {
1209+ yield * runCliWithRuntime ( startArgs ( baseDir , workspaceRoot ) ) . pipe ( Effect . flip ) ;
1210+
1211+ const threads = yield * readProjectThreads ( workspaceRoot ) ;
1212+ assert . lengthOf (
1213+ threads . filter ( ( thread ) => thread . deletedAt === null ) ,
1214+ 0 ,
1215+ ) ;
1216+ } ) ,
1217+ ( dispatch ) => ( command , options ) =>
1218+ command . type === "thread.turn.start"
1219+ ? Effect . fail (
1220+ new OrchestrationCommandInvariantError ( {
1221+ commandType : command . type ,
1222+ detail : "Rejected by test." ,
1223+ } ) ,
1224+ )
1225+ : dispatch ( command , options ) ,
1226+ ) ,
1227+ ) ;
1228+
1229+ it . effect ( "keeps the thread when the turn outcome is unknown" , ( ) =>
1230+ Effect . gen ( function * ( ) {
1231+ const turnAccepted = yield * Deferred . make < void > ( ) ;
1232+ yield * withThreadStartFixture (
1233+ ( { baseDir, workspaceRoot } ) =>
1234+ Effect . gen ( function * ( ) {
1235+ const starting = yield * Effect . forkChild (
1236+ runCliWithRuntime ( startArgs ( baseDir , workspaceRoot ) ) . pipe ( Effect . flip ) ,
1237+ ) ;
1238+ yield * Deferred . await ( turnAccepted ) ;
1239+ yield * TestClock . adjust ( "10 seconds" ) ;
1240+ yield * Fiber . join ( starting ) ;
1241+
1242+ const [ thread ] = yield * readProjectThreads ( workspaceRoot ) ;
1243+ assert . isNull ( thread ?. deletedAt ) ;
1244+ assert . equal ( thread ?. messages [ 0 ] ?. text , "Fix the flaky test" ) ;
1245+ } ) ,
1246+ ( dispatch ) => ( command , options ) =>
1247+ command . type === "thread.turn.start"
1248+ ? dispatch ( command , options ) . pipe (
1249+ Effect . tap ( ( ) => Deferred . succeed ( turnAccepted , undefined ) ) ,
1250+ Effect . andThen ( Effect . never ) ,
1251+ )
1252+ : dispatch ( command , options ) ,
1253+ ) ;
1254+ } ) ,
1255+ ) ;
1256+ } ) ;
0 commit comments