@@ -40,6 +40,7 @@ import * as Effect from "effect/Effect";
4040import * as DateTime from "effect/DateTime" ;
4141import * as Deferred from "effect/Deferred" ;
4242import * as Fiber from "effect/Fiber" ;
43+ import * as FileSystem from "effect/FileSystem" ;
4344import * as Layer from "effect/Layer" ;
4445import * as Option from "effect/Option" ;
4546import * as Path from "effect/Path" ;
@@ -1011,6 +1012,228 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
10111012 } ) ,
10121013 ) ;
10131014
1015+ it . effect ( "captures and repeatedly rewinds non-Git conversations without losing run links" , ( ) =>
1016+ Effect . gen ( function * ( ) {
1017+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
1018+ const eventSink = yield * EventSink . EventSinkV2 ;
1019+ const outbox = yield * EffectOutbox . EffectOutboxV2 ;
1020+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
1021+ const sessions = yield * ProviderSessionManager . ProviderSessionManagerV2 ;
1022+ const fileSystem = yield * FileSystem . FileSystem ;
1023+ const cwd = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-non-git-rewind-" } ) ;
1024+ const threadId = ThreadId . make ( "runtime-non-git-rewind" ) ;
1025+ const rollbackThread = vi . fn (
1026+ ( { providerThread } : ProviderAdapter . ProviderAdapterV2RollbackThreadInput ) =>
1027+ Effect . succeed ( {
1028+ providerThread : {
1029+ ...providerThread ,
1030+ nativeThreadRef : {
1031+ driver,
1032+ nativeId : "rewound-native-thread" ,
1033+ strength : "strong" as const ,
1034+ } ,
1035+ } ,
1036+ } ) ,
1037+ ) ;
1038+ const sessionSpy = vi . spyOn ( sessions , "open" ) . mockReturnValue (
1039+ Effect . succeed ( {
1040+ rollbackThread,
1041+ } as unknown as ProviderAdapter . ProviderAdapterV2SessionRuntime ) ,
1042+ ) ;
1043+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => sessionSpy . mockRestore ( ) ) ) ;
1044+ yield * orchestrator . dispatch ( {
1045+ type : "thread.create" ,
1046+ commandId : CommandId . make ( "non-git-create" ) ,
1047+ createdBy : "user" ,
1048+ creationSource : "web" ,
1049+ threadId,
1050+ projectId : ProjectId . make ( "non-git-project" ) ,
1051+ title : "Non-Git rewind" ,
1052+ modelSelection,
1053+ runtimeMode : "full-access" ,
1054+ interactionMode : "default" ,
1055+ branch : null ,
1056+ worktreePath : cwd ,
1057+ } ) ;
1058+
1059+ const completeMessage = ( name : string ) =>
1060+ Effect . gen ( function * ( ) {
1061+ yield * orchestrator . dispatch ( {
1062+ type : "message.dispatch" ,
1063+ commandId : CommandId . make ( name ) ,
1064+ createdBy : "user" ,
1065+ creationSource : "web" ,
1066+ threadId,
1067+ messageId : MessageId . make ( name ) ,
1068+ text : name ,
1069+ attachments : [ ] ,
1070+ dispatchMode : { type : "start_immediately" } ,
1071+ } ) ;
1072+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
1073+ const run = projection . runs . find ( ( candidate ) => candidate . userMessageId === name ) ! ;
1074+ const node = projection . nodes . find ( ( candidate ) => candidate . id === run . rootNodeId ) ! ;
1075+ const providerThread = projection . providerThreads . find (
1076+ ( candidate ) => candidate . id === run . providerThreadId ,
1077+ ) ! ;
1078+ const now = yield * DateTime . now ;
1079+ const commandId = CommandId . make ( `${ name } -complete` ) ;
1080+ // The provider completion is the fixture boundary; capture and rollback use real effects.
1081+ yield * outbox . cancelUnsettled ( {
1082+ threadId,
1083+ effectTypes : [ "provider-turn.start" ] ,
1084+ reason : "provider completion supplied by fixture" ,
1085+ } ) ;
1086+ yield * eventSink . writeWithEffects ( {
1087+ commandId,
1088+ events : [
1089+ {
1090+ id : EventId . make ( `${ name } -run` ) ,
1091+ type : "run.updated" ,
1092+ threadId,
1093+ runId : run . id ,
1094+ occurredAt : now ,
1095+ payload : { ...run , status : "waiting" , startedAt : now } ,
1096+ } ,
1097+ {
1098+ id : EventId . make ( `${ name } -node` ) ,
1099+ type : "node.updated" ,
1100+ threadId,
1101+ runId : run . id ,
1102+ nodeId : node . id ,
1103+ occurredAt : now ,
1104+ payload : { ...node , status : "completed" , completedAt : now } ,
1105+ } ,
1106+ {
1107+ id : EventId . make ( `${ name } -turn` ) ,
1108+ type : "provider-turn.updated" ,
1109+ threadId,
1110+ runId : run . id ,
1111+ occurredAt : now ,
1112+ payload : {
1113+ id : ProviderTurnId . make ( `${ name } -turn` ) ,
1114+ providerThreadId : providerThread . id ,
1115+ nodeId : node . id ,
1116+ runAttemptId : run . activeAttemptId ,
1117+ nativeTurnRef : null ,
1118+ ordinal : run . ordinal ,
1119+ status : "completed" ,
1120+ startedAt : now ,
1121+ completedAt : now ,
1122+ } ,
1123+ } ,
1124+ ] ,
1125+ effects : [
1126+ {
1127+ id : `${ name } -capture` ,
1128+ commandId,
1129+ threadId,
1130+ request : {
1131+ type : "checkpoint.capture" ,
1132+ runId : run . id ,
1133+ scopeId : node . checkpointScopeId ! ,
1134+ } ,
1135+ } ,
1136+ ] ,
1137+ } ) ;
1138+ yield * worker . drain ( ) ;
1139+ const captured = yield * orchestrator . getThreadProjection ( threadId ) ;
1140+ assert . equal (
1141+ captured . runs . find ( ( candidate ) => candidate . id === run . id ) ?. status ,
1142+ "completed" ,
1143+ ) ;
1144+ const checkpoint = captured . checkpoints . find ( ( candidate ) => candidate . runId === run . id ) ! ;
1145+ assert . equal ( checkpoint . status , "missing" ) ;
1146+ return { run, checkpoint } ;
1147+ } ) ;
1148+
1149+ const first = yield * completeMessage ( "non-git-first" ) ;
1150+ const second = yield * completeMessage ( "non-git-second" ) ;
1151+ const before = yield * orchestrator . getThreadProjection ( threadId ) ;
1152+ assert . equal (
1153+ before . checkpoints . find ( ( checkpoint ) => checkpoint . id === first . checkpoint . id ) ?. runId ,
1154+ first . run . id ,
1155+ "capturing a later run must preserve the earlier checkpoint's run link" ,
1156+ ) ;
1157+ const baseline = before . checkpoints . find (
1158+ ( checkpoint ) => checkpoint . ordinalWithinScope === 0 ,
1159+ ) ! ;
1160+ assert . equal ( baseline . status , "missing" ) ;
1161+ for ( const checkpoint of [ baseline , first . checkpoint ] ) {
1162+ for ( const restoreFiles of [ undefined , true ] ) {
1163+ const commandId = CommandId . make ( `non-git-files-${ checkpoint . id } -${ restoreFiles } ` ) ;
1164+ yield * orchestrator
1165+ . dispatch ( {
1166+ type : "checkpoint.rollback" ,
1167+ commandId,
1168+ threadId,
1169+ checkpointId : checkpoint . id ,
1170+ scopeId : checkpoint . scopeId ,
1171+ ...( restoreFiles === undefined ? { } : { restoreFiles } ) ,
1172+ } )
1173+ . pipe ( Effect . flip ) ;
1174+ assert . deepEqual ( yield * outbox . listByCommandId ( commandId ) , [ ] ) ;
1175+ }
1176+ }
1177+ assert . equal ( rollbackThread . mock . calls . length , 0 ) ;
1178+
1179+ const rewind = ( name : string ) =>
1180+ Effect . gen ( function * ( ) {
1181+ const commandId = CommandId . make ( name ) ;
1182+ yield * orchestrator . dispatch ( {
1183+ type : "checkpoint.rollback" ,
1184+ commandId,
1185+ threadId,
1186+ checkpointId : baseline . id ,
1187+ scopeId : baseline . scopeId ,
1188+ restoreFiles : false ,
1189+ } ) ;
1190+ yield * worker . drain ( ) ;
1191+ assert . deepEqual (
1192+ ( yield * outbox . listByCommandId ( commandId ) ) . map ( ( effect ) => effect . status ) ,
1193+ [ "succeeded" ] ,
1194+ ) ;
1195+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
1196+ assert . ok ( projection . runs . every ( ( run ) => run . status === "rolled_back" ) ) ;
1197+ assert . isNull ( projection . providerThreads [ 0 ] ! . lastRunOrdinal ) ;
1198+ assert . equal (
1199+ projection . providerThreads [ 0 ] ! . nativeThreadRef ?. nativeId ,
1200+ "rewound-native-thread" ,
1201+ ) ;
1202+ assert . isNull ( projection . thread . rollbackFailure ) ;
1203+ } ) ;
1204+
1205+ yield * rewind ( "non-git-rewind-first" ) ;
1206+ assert . equal ( rollbackThread . mock . calls [ 0 ] ! [ 0 ] . target . type , "thread_start" ) ;
1207+ assert . deepEqual (
1208+ rollbackThread . mock . calls [ 0 ] ! [ 0 ] . providerThreadTurns . map ( ( turn ) => turn . id ) ,
1209+ [ ProviderTurnId . make ( "non-git-first-turn" ) , ProviderTurnId . make ( "non-git-second-turn" ) ] ,
1210+ ) ;
1211+ const discardedCommandId = CommandId . make ( "non-git-discarded-target" ) ;
1212+ yield * orchestrator
1213+ . dispatch ( {
1214+ type : "checkpoint.rollback" ,
1215+ commandId : discardedCommandId ,
1216+ threadId,
1217+ checkpointId : second . checkpoint . id ,
1218+ scopeId : second . checkpoint . scopeId ,
1219+ restoreFiles : false ,
1220+ } )
1221+ . pipe ( Effect . flip ) ;
1222+ assert . deepEqual ( yield * outbox . listByCommandId ( discardedCommandId ) , [ ] ) ;
1223+ assert . equal ( rollbackThread . mock . calls . length , 1 ) ;
1224+
1225+ const resent = yield * completeMessage ( "non-git-resent" ) ;
1226+ assert . isAbove ( resent . run . ordinal , second . run . ordinal ) ;
1227+ yield * rewind ( "non-git-rewind-again" ) ;
1228+ assert . equal ( rollbackThread . mock . calls . length , 2 ) ;
1229+ assert . equal ( rollbackThread . mock . calls [ 1 ] ! [ 0 ] . target . type , "thread_start" ) ;
1230+ assert . deepEqual (
1231+ rollbackThread . mock . calls [ 1 ] ! [ 0 ] . providerThreadTurns . map ( ( turn ) => turn . id ) ,
1232+ [ ProviderTurnId . make ( "non-git-resent-turn" ) ] ,
1233+ ) ;
1234+ } ) . pipe ( Effect . provide ( Layer . merge ( Layer . fresh ( layerTest ) , NodeServices . layer ) ) ) ,
1235+ ) ;
1236+
10141237 it . effect ( "rejects non-ready rollback targets before persisting events or effects" , ( ) =>
10151238 Effect . gen ( function * ( ) {
10161239 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
0 commit comments