@@ -5049,8 +5049,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
50495049 ) ;
50505050 }
50515051
5052- // A steer cancels the queued run before it starts while the steered run keeps writing, so
5053- // the queued message leaves the window but its run is still the shell's latest run.
5052+ // A steer moves the cancelled queued message outside the window.
50545053 const steeredItemId = "turn-item:bounded-sql-history:steered-output" ;
50555054 const steeredRunId = RunId . make ( "run:bounded-sql-history:1000" ) ;
50565055 yield * sql `
@@ -5088,7 +5087,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
50885087 assert . isFalse (
50895088 steeredSnapshot . projection . turnItems . some ( ( item ) => item . runId === cancelledRunId ) ,
50905089 ) ;
5091- assert . isTrue ( steeredSnapshot . projection . runs . some ( ( run ) => run . id === cancelledRunId ) ) ;
5090+ const latestRunId = ( yield * projectionStore . getShellSnapshot ( ) ) . threads . find (
5091+ ( thread ) => thread . id === threadId ,
5092+ ) ?. latestRunId ;
5093+ assert . equal ( latestRunId , hiddenRunId ) ;
5094+ assert . isFalse (
5095+ steeredSnapshot . projection . turnItems . some ( ( item ) => item . runId === latestRunId ) ,
5096+ ) ;
5097+ assert . isTrue ( steeredSnapshot . projection . runs . some ( ( run ) => run . id === latestRunId ) ) ;
50925098 yield * sql `DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id = ${ steeredItemId } ` ;
50935099
50945100 const interruptRunId = RunId . make ( "run:bounded-sql-history:interrupt" ) ;
@@ -7419,6 +7425,28 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
74197425 silent ? hidden : visible ,
74207426 ] ) ;
74217427
7428+ // A follow-up removed before it started stays latest unless a visible run outlives it.
7429+ const removedRunId = RunId . make ( `run:projection-shell-silent:${ key } :removed` ) ;
7430+ yield * projectionStore . apply ( {
7431+ id : EventId . make ( `event:projection-shell-silent:${ key } :removed` ) ,
7432+ type : "run.updated" ,
7433+ threadId,
7434+ occurredAt : now ,
7435+ payload : {
7436+ ...( yield * projectionStore . getThreadProjection ( threadId ) ) . runs [ 0 ] ! ,
7437+ id : removedRunId ,
7438+ ordinal : 2 ,
7439+ userMessageId : MessageId . make ( `message:projection-shell-silent:${ key } :removed` ) ,
7440+ status : "cancelled" ,
7441+ startedAt : null ,
7442+ completedAt : now ,
7443+ } ,
7444+ } ) ;
7445+ assert . deepStrictEqual (
7446+ ( yield * presented ) . map ( ( shell ) => shell . latestRunId ) ,
7447+ silent ? [ removedRunId , removedRunId ] : [ runId , runId ] ,
7448+ ) ;
7449+
74227450 // A run that fails outright, e.g. on a provider error, needs the user too.
74237451 const running = ( yield * projectionStore . getThreadProjection ( threadId ) ) . runs [ 0 ] ! ;
74247452 const setRunStatus = ( status : "failed" | "running" ) =>
@@ -7657,9 +7685,27 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
76577685 value == null ? null : DateTime . toEpochMillis ( value ) ;
76587686 assert . equal ( timestamp ( sqlShell . activityRunStartedAt ) , expected ) ;
76597687 assert . equal ( timestamp ( memoryShell . activityRunStartedAt ) , expected ) ;
7660- assert . equal ( sqlShell . latestRunId , "run:clock:newer" ) ;
7688+ // A queued message steered or removed before it started never ran, so the run that did presents.
7689+ for ( const shell of [ sqlShell , memoryShell ] ) {
7690+ assert . equal ( shell . latestRunId , status === "cancelled" ? runId : "run:clock:newer" ) ;
7691+ }
76617692 }
76627693 }
7694+ // Removing a message after the earlier run ended leaves the cancelled run as the latest.
7695+ yield * projectionStore . apply ( {
7696+ id : EventId . make ( "event:clock:ended-before-cancel" ) ,
7697+ type : "run.updated" ,
7698+ threadId,
7699+ occurredAt : later ,
7700+ payload : { ...run , status : "completed" , startedAt : now , completedAt : now } ,
7701+ } ) ;
7702+ const endedBeforeCancelProjection = yield * projectionStore . getThreadProjection ( threadId ) ;
7703+ for ( const shell of [
7704+ ( yield * projectionStore . getShellSnapshot ( ) ) . threads . find ( ( row ) => row . id === threadId ) ! ,
7705+ ProjectionStore . threadShellFromProjection ( endedBeforeCancelProjection ) ,
7706+ ] ) {
7707+ assert . equal ( shell . latestRunId , "run:clock:newer" ) ;
7708+ }
76637709
76647710 yield * projectionStore . apply ( {
76657711 id : EventId . make ( "event:clock:newer:held" ) ,
0 commit comments