@@ -2513,7 +2513,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
25132513 } ) ,
25142514 ) ;
25152515
2516- it . effect ( "reads only user-input activities when refreshing shell summaries " , ( ) =>
2516+ it . effect ( "counts pending questions without decoding retained question history " , ( ) =>
25172517 Effect . gen ( function * ( ) {
25182518 const projectionPipeline = yield * OrchestrationProjectionPipeline ;
25192519 const eventStore = yield * OrchestrationEventStore ;
@@ -2571,7 +2571,34 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
25712571 } ,
25722572 } ) ;
25732573
2574- // Invalid JSON proves the summary query filters tool rows before decoding payloads.
2574+ // Large resolved payloads must not be loaded into the shell. Invalid tone
2575+ // values prove the count does not decode complete activity rows.
2576+ yield * sql `
2577+ WITH RECURSIVE history(n) AS (
2578+ SELECT 1
2579+ UNION ALL
2580+ SELECT n + 1 FROM history WHERE n < 1000
2581+ ), lifecycle(kind) AS (
2582+ SELECT 'user-input.requested'
2583+ UNION ALL
2584+ SELECT 'user-input.resolved'
2585+ )
2586+ INSERT INTO projection_thread_activities (
2587+ activity_id, thread_id, turn_id, tone, kind, summary, payload_json, created_at
2588+ )
2589+ SELECT
2590+ 'history-' || kind || '-' || n,
2591+ 'thread-stale-user-input',
2592+ NULL,
2593+ 'invalid-tone',
2594+ kind,
2595+ 'Old question activity',
2596+ json_object('requestId', 'history-' || n, 'questions', ${ "x" . repeat ( 4096 ) } ),
2597+ '2026-02-25T12:00:00.000Z'
2598+ FROM history CROSS JOIN lifecycle
2599+ ` ;
2600+
2601+ // Invalid JSON proves the count excludes unrelated tool payloads too.
25752602 yield * sql `
25762603 INSERT INTO projection_thread_activities (
25772604 activity_id,
@@ -3112,6 +3139,20 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
31123139 } ,
31133140 } ) ;
31143141
3142+ // A failed reply must not decode other requests or tool output.
3143+ yield * sql `
3144+ INSERT INTO projection_thread_activities (
3145+ activity_id, thread_id, turn_id, tone, kind, summary, payload_json, created_at
3146+ ) VALUES
3147+ ('nonstale-approval-tool', 'thread-nonstale-approval', NULL, 'info', 'tool.completed',
3148+ 'Tool output', '{not-json', '2026-02-26T12:45:02.000Z'),
3149+ ('nonstale-other-request', 'thread-nonstale-approval', NULL, 'invalid-tone',
3150+ 'approval.requested', '', json_object('requestId', 'other-request'), '2026-02-26T12:45:02.000Z'),
3151+ ('nonstale-other-thread', 'thread-nonstale-approval-other', NULL, 'info',
3152+ 'approval.resolved', '', json_object('requestId', 'approval-request-nonstale-existing'),
3153+ '2026-02-26T12:45:02.000Z')
3154+ ` ;
3155+
31153156 yield * appendAndProject ( {
31163157 type : "thread.activity-appended" ,
31173158 eventId : EventId . make ( "evt-nonstale-approval-4" ) ,
@@ -3268,7 +3309,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
32683309 } ) ,
32693310 ) ;
32703311
3271- it . effect ( "does not fallback-retain messages whose turnId is removed by revert" , ( ) =>
3312+ it . effect ( "retains checkpointed messages and recomputes questions after revert" , ( ) =>
32723313 Effect . gen ( function * ( ) {
32733314 const projectionPipeline = yield * OrchestrationProjectionPipeline ;
32743315 const eventStore = yield * OrchestrationEventStore ;
@@ -3435,6 +3476,52 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
34353476 } ,
34363477 } ) ;
34373478
3479+ const questionActivities = [
3480+ { kind : "user-input.requested" , requestId : "revert-question" , turnId : null } ,
3481+ {
3482+ kind : "user-input.resolved" ,
3483+ requestId : "revert-question" ,
3484+ turnId : TurnId . make ( "turn-2" ) ,
3485+ } ,
3486+ {
3487+ kind : "user-input.requested" ,
3488+ requestId : "retained-question" ,
3489+ turnId : TurnId . make ( "turn-1" ) ,
3490+ } ,
3491+ { kind : "user-input.resolved" , requestId : "retained-question" , turnId : null } ,
3492+ ] ;
3493+ for ( const [ index , activity ] of questionActivities . entries ( ) ) {
3494+ yield * appendAndProject ( {
3495+ type : "thread.activity-appended" ,
3496+ eventId : EventId . make ( `evt-revert-question-${ index } ` ) ,
3497+ aggregateKind : "thread" ,
3498+ aggregateId : ThreadId . make ( "thread-revert" ) ,
3499+ occurredAt : "2026-02-26T12:00:03.200Z" ,
3500+ commandId : null ,
3501+ causationEventId : null ,
3502+ correlationId : null ,
3503+ metadata : { } ,
3504+ payload : {
3505+ threadId : ThreadId . make ( "thread-revert" ) ,
3506+ activity : {
3507+ id : EventId . make ( `activity-revert-question-${ index } ` ) ,
3508+ tone : "info" ,
3509+ kind : activity . kind ,
3510+ summary : "Question activity" ,
3511+ payload : { requestId : activity . requestId } ,
3512+ turnId : activity . turnId ,
3513+ createdAt : "2026-02-26T12:00:03.200Z" ,
3514+ } ,
3515+ } ,
3516+ } ) ;
3517+ }
3518+
3519+ const beforeRevert = yield * sql < { readonly count : number } > `
3520+ SELECT pending_user_input_count AS count
3521+ FROM projection_threads WHERE thread_id = 'thread-revert'
3522+ ` ;
3523+ assert . deepEqual ( beforeRevert , [ { count : 0 } ] ) ;
3524+
34383525 yield * appendAndProject ( {
34393526 type : "thread.reverted" ,
34403527 eventId : EventId . make ( "evt-revert-8" ) ,
@@ -3471,6 +3558,11 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
34713558 role : "assistant" ,
34723559 } ,
34733560 ] ) ;
3561+ const afterRevert = yield * sql < { readonly count : number } > `
3562+ SELECT pending_user_input_count AS count
3563+ FROM projection_threads WHERE thread_id = 'thread-revert'
3564+ ` ;
3565+ assert . deepEqual ( afterRevert , [ { count : 1 } ] ) ;
34743566 } ) ,
34753567 ) ;
34763568} ) ;
0 commit comments