@@ -2789,4 +2789,252 @@ engineLayer("OrchestrationProjectionPipeline via engine dispatch", (it) => {
27892789 ] ) ;
27902790 } ) ,
27912791 ) ;
2792+
2793+ it . effect ( "re-creating a deleted thread id purges the old incarnation's projections" , ( ) =>
2794+ Effect . gen ( function * ( ) {
2795+ const engine = yield * OrchestrationEngineService ;
2796+ const sql = yield * SqlClient . SqlClient ;
2797+ const createdAt = "2026-01-01T00:00:00.000Z" ;
2798+ const modelSelection = {
2799+ instanceId : ProviderInstanceId . make ( "codex" ) ,
2800+ model : "gpt-5-codex" ,
2801+ } ;
2802+
2803+ yield * engine . dispatch ( {
2804+ type : "project.create" ,
2805+ commandId : CommandId . make ( "cmd-recreate-project" ) ,
2806+ projectId : ProjectId . make ( "project-recreate" ) ,
2807+ title : "Recreate Project" ,
2808+ workspaceRoot : "/tmp/project-recreate" ,
2809+ defaultModelSelection : modelSelection ,
2810+ createdAt,
2811+ } ) ;
2812+ yield * engine . dispatch ( {
2813+ type : "thread.create" ,
2814+ commandId : CommandId . make ( "cmd-recreate-thread-1" ) ,
2815+ threadId : ThreadId . make ( "thread-recreate" ) ,
2816+ projectId : ProjectId . make ( "project-recreate" ) ,
2817+ title : "First incarnation" ,
2818+ modelSelection,
2819+ interactionMode : "default" ,
2820+ runtimeMode : "full-access" ,
2821+ branch : null ,
2822+ worktreePath : null ,
2823+ createdAt,
2824+ } ) ;
2825+ yield * engine . dispatch ( {
2826+ type : "thread.turn.start" ,
2827+ commandId : CommandId . make ( "cmd-recreate-turn-1" ) ,
2828+ threadId : ThreadId . make ( "thread-recreate" ) ,
2829+ message : {
2830+ messageId : MessageId . make ( "msg-recreate-1" ) ,
2831+ role : "user" ,
2832+ text : "old message" ,
2833+ attachments : [ ] ,
2834+ } ,
2835+ interactionMode : "default" ,
2836+ runtimeMode : "full-access" ,
2837+ createdAt,
2838+ } ) ;
2839+ yield * engine . dispatch ( {
2840+ type : "thread.delete" ,
2841+ commandId : CommandId . make ( "cmd-recreate-delete" ) ,
2842+ threadId : ThreadId . make ( "thread-recreate" ) ,
2843+ } ) ;
2844+
2845+ yield * engine . dispatch ( {
2846+ type : "thread.create" ,
2847+ commandId : CommandId . make ( "cmd-recreate-thread-2" ) ,
2848+ threadId : ThreadId . make ( "thread-recreate" ) ,
2849+ projectId : ProjectId . make ( "project-recreate" ) ,
2850+ title : "Second incarnation" ,
2851+ modelSelection,
2852+ interactionMode : "default" ,
2853+ runtimeMode : "full-access" ,
2854+ branch : null ,
2855+ worktreePath : null ,
2856+ createdAt : "2026-01-01T00:00:01.000Z" ,
2857+ } ) ;
2858+
2859+ const threadRows = yield * sql < {
2860+ readonly title : string ;
2861+ readonly deletedAt : string | null ;
2862+ } > `
2863+ SELECT
2864+ title,
2865+ deleted_at AS "deletedAt"
2866+ FROM projection_threads
2867+ WHERE thread_id = 'thread-recreate'
2868+ ` ;
2869+ assert . deepEqual ( threadRows , [ { title : "Second incarnation" , deletedAt : null } ] ) ;
2870+
2871+ const messageRows = yield * sql < { readonly messageId : string } > `
2872+ SELECT message_id AS "messageId"
2873+ FROM projection_thread_messages
2874+ WHERE thread_id = 'thread-recreate'
2875+ ` ;
2876+ assert . deepEqual ( messageRows , [ ] ) ;
2877+
2878+ const turnRows = yield * sql < { readonly threadId : string } > `
2879+ SELECT thread_id AS "threadId"
2880+ FROM projection_turns
2881+ WHERE thread_id = 'thread-recreate'
2882+ ` ;
2883+ assert . deepEqual ( turnRows , [ ] ) ;
2884+ } ) ,
2885+ ) ;
27922886} ) ;
2887+
2888+ it . layer ( Layer . fresh ( makeProjectionPipelinePrefixedTestLayer ( "t3-recreate-replay-" ) ) ) (
2889+ "OrchestrationProjectionPipeline" ,
2890+ ( it ) => {
2891+ it . effect ( "per-projector replay keeps the new incarnation's rows after a re-create" , ( ) =>
2892+ Effect . gen ( function * ( ) {
2893+ const projectionPipeline = yield * OrchestrationProjectionPipeline ;
2894+ const eventStore = yield * OrchestrationEventStore ;
2895+ const sql = yield * SqlClient . SqlClient ;
2896+ const now = "2026-01-01T00:00:00.000Z" ;
2897+ const modelSelection = {
2898+ instanceId : ProviderInstanceId . make ( "codex" ) ,
2899+ model : "gpt-5-codex" ,
2900+ } ;
2901+ const threadCreatedPayload = {
2902+ threadId : ThreadId . make ( "thread-replay" ) ,
2903+ projectId : ProjectId . make ( "project-replay" ) ,
2904+ modelSelection,
2905+ runtimeMode : "full-access" as const ,
2906+ branch : null ,
2907+ worktreePath : null ,
2908+ createdAt : now ,
2909+ updatedAt : now ,
2910+ } ;
2911+
2912+ yield * eventStore . append ( {
2913+ type : "project.created" ,
2914+ eventId : EventId . make ( "evt-replay-project" ) ,
2915+ aggregateKind : "project" ,
2916+ aggregateId : ProjectId . make ( "project-replay" ) ,
2917+ occurredAt : now ,
2918+ commandId : CommandId . make ( "cmd-replay-project" ) ,
2919+ causationEventId : null ,
2920+ correlationId : CommandId . make ( "cmd-replay-project" ) ,
2921+ metadata : { } ,
2922+ payload : {
2923+ projectId : ProjectId . make ( "project-replay" ) ,
2924+ title : "Replay Project" ,
2925+ workspaceRoot : "/tmp/project-replay" ,
2926+ defaultModelSelection : null ,
2927+ scripts : [ ] ,
2928+ createdAt : now ,
2929+ updatedAt : now ,
2930+ } ,
2931+ } ) ;
2932+ yield * eventStore . append ( {
2933+ type : "thread.created" ,
2934+ eventId : EventId . make ( "evt-replay-create-1" ) ,
2935+ aggregateKind : "thread" ,
2936+ aggregateId : ThreadId . make ( "thread-replay" ) ,
2937+ occurredAt : now ,
2938+ commandId : CommandId . make ( "cmd-replay-create-1" ) ,
2939+ causationEventId : null ,
2940+ correlationId : CommandId . make ( "cmd-replay-create-1" ) ,
2941+ metadata : { } ,
2942+ payload : { ...threadCreatedPayload , title : "First incarnation" } ,
2943+ } ) ;
2944+ yield * eventStore . append ( {
2945+ type : "thread.message-sent" ,
2946+ eventId : EventId . make ( "evt-replay-msg-old" ) ,
2947+ aggregateKind : "thread" ,
2948+ aggregateId : ThreadId . make ( "thread-replay" ) ,
2949+ occurredAt : now ,
2950+ commandId : CommandId . make ( "cmd-replay-msg-old" ) ,
2951+ causationEventId : null ,
2952+ correlationId : CommandId . make ( "cmd-replay-msg-old" ) ,
2953+ metadata : { } ,
2954+ payload : {
2955+ threadId : ThreadId . make ( "thread-replay" ) ,
2956+ messageId : MessageId . make ( "msg-replay-old" ) ,
2957+ role : "user" ,
2958+ text : "old incarnation" ,
2959+ turnId : null ,
2960+ streaming : false ,
2961+ createdAt : now ,
2962+ updatedAt : now ,
2963+ } ,
2964+ } ) ;
2965+ yield * eventStore . append ( {
2966+ type : "thread.deleted" ,
2967+ eventId : EventId . make ( "evt-replay-delete" ) ,
2968+ aggregateKind : "thread" ,
2969+ aggregateId : ThreadId . make ( "thread-replay" ) ,
2970+ occurredAt : now ,
2971+ commandId : CommandId . make ( "cmd-replay-delete" ) ,
2972+ causationEventId : null ,
2973+ correlationId : CommandId . make ( "cmd-replay-delete" ) ,
2974+ metadata : { } ,
2975+ payload : {
2976+ threadId : ThreadId . make ( "thread-replay" ) ,
2977+ deletedAt : now ,
2978+ } ,
2979+ } ) ;
2980+ yield * eventStore . append ( {
2981+ type : "thread.created" ,
2982+ eventId : EventId . make ( "evt-replay-create-2" ) ,
2983+ aggregateKind : "thread" ,
2984+ aggregateId : ThreadId . make ( "thread-replay" ) ,
2985+ occurredAt : now ,
2986+ commandId : CommandId . make ( "cmd-replay-create-2" ) ,
2987+ causationEventId : null ,
2988+ correlationId : CommandId . make ( "cmd-replay-create-2" ) ,
2989+ metadata : { } ,
2990+ payload : { ...threadCreatedPayload , title : "Second incarnation" } ,
2991+ } ) ;
2992+ yield * eventStore . append ( {
2993+ type : "thread.message-sent" ,
2994+ eventId : EventId . make ( "evt-replay-msg-new" ) ,
2995+ aggregateKind : "thread" ,
2996+ aggregateId : ThreadId . make ( "thread-replay" ) ,
2997+ occurredAt : now ,
2998+ commandId : CommandId . make ( "cmd-replay-msg-new" ) ,
2999+ causationEventId : null ,
3000+ correlationId : CommandId . make ( "cmd-replay-msg-new" ) ,
3001+ metadata : { } ,
3002+ payload : {
3003+ threadId : ThreadId . make ( "thread-replay" ) ,
3004+ messageId : MessageId . make ( "msg-replay-new" ) ,
3005+ role : "user" ,
3006+ text : "new incarnation" ,
3007+ turnId : null ,
3008+ streaming : false ,
3009+ createdAt : now ,
3010+ updatedAt : now ,
3011+ } ,
3012+ } ) ;
3013+
3014+ // Per-projector replay applies the whole log for each projector in
3015+ // turn, threads last. Rows the message projector rebuilt for the new
3016+ // incarnation must survive the threads projector's pass.
3017+ yield * projectionPipeline . bootstrap ;
3018+
3019+ const messageRows = yield * sql < { readonly messageId : string } > `
3020+ SELECT message_id AS "messageId"
3021+ FROM projection_thread_messages
3022+ WHERE thread_id = 'thread-replay'
3023+ ` ;
3024+ assert . deepEqual ( messageRows , [ { messageId : "msg-replay-new" } ] ) ;
3025+
3026+ const threadRows = yield * sql < {
3027+ readonly title : string ;
3028+ readonly deletedAt : string | null ;
3029+ } > `
3030+ SELECT
3031+ title,
3032+ deleted_at AS "deletedAt"
3033+ FROM projection_threads
3034+ WHERE thread_id = 'thread-replay'
3035+ ` ;
3036+ assert . deepEqual ( threadRows , [ { title : "Second incarnation" , deletedAt : null } ] ) ;
3037+ } ) ,
3038+ ) ;
3039+ } ,
3040+ ) ;
0 commit comments