@@ -6182,32 +6182,62 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
61826182 Effect . mapError ( controlReadError ( threadId ) ) ,
61836183 ) ;
61846184
6185- // A live run fork shows its source's history through the fork run without copying it.
6186- const shownByLiveFork = ( threadId : ThreadId , runIdColumn : string ) => sql `
6187- EXISTS (
6188- SELECT 1
6189- FROM orchestration_v2_projection_threads AS fork
6190- JOIN orchestration_v2_projection_runs AS fork_run
6191- ON fork_run.run_id = json_extract(fork.payload_json, '$.forkedFrom.runId')
6192- LEFT JOIN orchestration_v2_projection_runs AS item_run
6193- ON item_run.run_id = ${ sql . literal ( runIdColumn ) }
6194- WHERE fork_run.thread_id = ${ threadId }
6195- AND fork.deleted_at IS NULL
6196- AND json_extract(fork.payload_json, '$.forkedFrom.type') = 'run'
6197- AND json_extract(fork.payload_json, '$.forkedFrom.threadId') = ${ threadId }
6198- AND json_extract(fork.payload_json, '$.lineage.sideChat') IS NOT 1
6199- AND (${ sql . literal ( runIdColumn ) } IS NULL OR item_run.ordinal <= fork_run.ordinal)
6200- )
6185+ // A run fork shows its source's history through the fork run without copying it, and every
6186+ // run fork below it shows that history too. Returns the last source run live forks show, with
6187+ // and without `leavingForkId`.
6188+ const lastRunsShownByLiveForks = ( threadId : ThreadId , leavingForkId : ThreadId ) =>
6189+ sql < { with_fork : number | null ; without_fork : number | null } > `
6190+ WITH RECURSIVE showing(thread_id, deleted_at, through) AS (
6191+ SELECT fork.thread_id, fork.deleted_at, fork_run.ordinal
6192+ FROM orchestration_v2_projection_context_transfers AS transfer
6193+ JOIN orchestration_v2_projection_threads AS fork
6194+ ON fork.thread_id = transfer.target_thread_id
6195+ JOIN orchestration_v2_projection_runs AS fork_run
6196+ ON fork_run.run_id = json_extract(fork.payload_json, '$.forkedFrom.runId')
6197+ WHERE transfer.source_thread_id = ${ threadId }
6198+ AND transfer.type = 'fork'
6199+ AND fork_run.thread_id = ${ threadId }
6200+ AND json_extract(fork.payload_json, '$.forkedFrom.type') = 'run'
6201+ AND json_extract(fork.payload_json, '$.forkedFrom.threadId') = ${ threadId }
6202+ AND json_extract(fork.payload_json, '$.lineage.sideChat') IS NOT 1
6203+ UNION
6204+ SELECT child.thread_id, child.deleted_at, showing.through
6205+ FROM showing
6206+ JOIN orchestration_v2_projection_context_transfers AS transfer
6207+ ON transfer.source_thread_id = showing.thread_id AND transfer.type = 'fork'
6208+ JOIN orchestration_v2_projection_threads AS child
6209+ ON child.thread_id = transfer.target_thread_id
6210+ WHERE json_extract(child.payload_json, '$.forkedFrom.type') = 'run'
6211+ AND json_extract(child.payload_json, '$.forkedFrom.threadId') = showing.thread_id
6212+ AND json_extract(child.payload_json, '$.lineage.sideChat') IS NOT 1
6213+ )
6214+ SELECT
6215+ MAX(through) AS with_fork,
6216+ MAX(CASE WHEN thread_id <> ${ leavingForkId } THEN through END) AS without_fork
6217+ FROM showing
6218+ WHERE deleted_at IS NULL
6219+ ` . pipe ( Effect . map ( ( rows ) => rows [ 0 ] ?? { with_fork : null , without_fork : null } ) ) ;
6220+
6221+ // A row with no run predates every run (-1), so any live fork shows it.
6222+ const runOrdinalBetween = ( runIdColumn : string , from : number , through : number ) => sql `
6223+ COALESCE((
6224+ SELECT run.ordinal FROM orchestration_v2_projection_runs AS run
6225+ WHERE run.run_id = ${ sql . literal ( runIdColumn ) }
6226+ ), -1) BETWEEN ${ from } AND ${ through }
62016227 ` ;
62026228
6203- const getThreadAttachmentIds : ProjectionStoreV2Shape [ "getThreadAttachmentIds" ] = ( threadId ) =>
6204- Effect . all ( [
6229+ const runAttachmentIds = Effect . fn ( "ProjectionStore.runAttachmentIds" ) ( function * (
6230+ threadId : ThreadId ,
6231+ from : number ,
6232+ through : number ,
6233+ ) {
6234+ const [ messages , renders , answerFiles ] = yield * Effect . all ( [
62056235 sql < { id : string } > `
62066236 SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
62076237 FROM orchestration_v2_projection_messages AS message,
62086238 json_each(message.payload_json, '$.attachments') AS attachment
62096239 WHERE message.thread_id = ${ threadId }
6210- AND NOT ${ shownByLiveFork ( threadId , "message.run_id" ) }
6240+ AND ${ runOrdinalBetween ( "message.run_id" , from , through ) }
62116241 ` ,
62126242 sql < { payload_json : string } > `
62136243 SELECT item.payload_json
@@ -6217,7 +6247,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62176247 AND (item.payload_json LIKE '%htmlRender%'
62186248 OR item.payload_json LIKE ${ `%${ MCP_APP_OUTPUT_KEY } %` }
62196249 OR item.payload_json LIKE '%preview_recording_stop%')
6220- AND NOT ${ shownByLiveFork ( threadId , "item.run_id" ) }
6250+ AND ${ runOrdinalBetween ( "item.run_id" , from , through ) }
62216251 ` ,
62226252 sql < { id : string } > `
62236253 SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
@@ -6226,37 +6256,85 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62266256 json_each(question.value) AS attachment
62276257 WHERE item.thread_id = ${ threadId }
62286258 AND item.type = 'user_input_request'
6229- AND NOT ${ shownByLiveFork ( threadId , "item.run_id" ) }
6259+ AND ${ runOrdinalBetween ( "item.run_id" , from , through ) }
62306260 ` ,
6261+ ] ) ;
6262+ return [
6263+ ...messages . map ( ( row ) => row . id ) ,
6264+ ...answerFiles . map ( ( row ) => row . id ) ,
6265+ ...threadHtmlRenderAttachmentIds (
6266+ threadId ,
6267+ renders . map ( ( row ) => {
6268+ const item = parseEncodedPayload ( row . payload_json ) ;
6269+ return {
6270+ toolName : typeof item . toolName === "string" ? item . toolName : null ,
6271+ output : item . output ,
6272+ } ;
6273+ } ) ,
6274+ ) ,
6275+ ] ;
6276+ } ) ;
6277+
6278+ // Deleting a fork also frees what its deleted sources kept only for it.
6279+ const getThreadAttachmentIds : ProjectionStoreV2Shape [ "getThreadAttachmentIds" ] = Effect . fn (
6280+ "ProjectionStore.getThreadAttachmentIds" ,
6281+ ) (
6282+ function * ( threadId ) {
6283+ const deletedSources = yield * sql < { thread_id : ThreadId } > `
6284+ WITH RECURSIVE source(thread_id) AS (
6285+ SELECT json_extract(thread.payload_json, '$.forkedFrom.threadId')
6286+ FROM orchestration_v2_projection_threads AS thread
6287+ WHERE thread.thread_id = ${ threadId }
6288+ AND json_extract(thread.payload_json, '$.forkedFrom.type') = 'run'
6289+ AND json_extract(thread.payload_json, '$.lineage.sideChat') IS NOT 1
6290+ UNION
6291+ SELECT json_extract(thread.payload_json, '$.forkedFrom.threadId')
6292+ FROM orchestration_v2_projection_threads AS thread
6293+ JOIN source USING (thread_id)
6294+ WHERE thread.deleted_at IS NOT NULL
6295+ AND json_extract(thread.payload_json, '$.forkedFrom.type') = 'run'
6296+ AND json_extract(thread.payload_json, '$.lineage.sideChat') IS NOT 1
6297+ )
6298+ SELECT thread.thread_id
6299+ FROM source
6300+ JOIN orchestration_v2_projection_threads AS thread USING (thread_id)
6301+ WHERE thread.deleted_at IS NOT NULL
6302+ ` ;
6303+ const afterRun = ( ordinal : number | null ) => ( ordinal === null ? - 1 : ordinal + 1 ) ;
6304+ const own = yield * lastRunsShownByLiveForks ( threadId , threadId ) ;
6305+ const released = yield * Effect . forEach ( deletedSources , ( { thread_id : source } ) =>
6306+ lastRunsShownByLiveForks ( source , threadId ) . pipe (
6307+ Effect . flatMap ( ( shown ) =>
6308+ shown . with_fork === null
6309+ ? Effect . succeed ( [ ] )
6310+ : runAttachmentIds ( source , afterRun ( shown . without_fork ) , shown . with_fork ) ,
6311+ ) ,
6312+ ) ,
6313+ ) ;
62316314 // A guest's waiting files are pending uploads that no message row holds yet.
6232- sql < { id : string } > `
6233- SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
6234- FROM orchestration_v2_projection_threads AS thread,
6235- json_each(thread.payload_json, '$.guestShare.messages') AS message,
6236- json_each(message.value, '$.attachments') AS attachment
6237- WHERE thread.thread_id = ${ threadId }
6238- AND json_extract(message.value, '$.status') = 'pending'
6239- ` ,
6240- ] ) . pipe (
6241- Effect . map ( ( [ messages , renders , answerFiles , guestFiles ] ) => [
6315+ const guestFiles = yield * sql < { id : string } > `
6316+ SELECT DISTINCT json_extract(attachment.value, '$.id') AS id
6317+ FROM orchestration_v2_projection_threads AS thread,
6318+ json_each(thread.payload_json, '$.guestShare.messages') AS message,
6319+ json_each(message.value, '$.attachments') AS attachment
6320+ WHERE thread.thread_id = ${ threadId }
6321+ AND json_extract(message.value, '$.status') = 'pending'
6322+ ` ;
6323+ return [
62426324 ...new Set ( [
6243- ...messages . map ( ( row ) => row . id ) ,
6244- ...guestFiles . map ( ( row ) => row . id ) ,
6245- ...answerFiles . map ( ( row ) => row . id ) ,
6246- ...threadHtmlRenderAttachmentIds (
6325+ ...( yield * runAttachmentIds (
62476326 threadId ,
6248- renders . map ( ( row ) => {
6249- const item = parseEncodedPayload ( row . payload_json ) ;
6250- return {
6251- toolName : typeof item . toolName === "string" ? item . toolName : null ,
6252- output : item . output ,
6253- } ;
6254- } ) ,
6255- ) ,
6327+ afterRun ( own . with_fork ) ,
6328+ Number . MAX_SAFE_INTEGER ,
6329+ ) ) ,
6330+ ...guestFiles . map ( ( row ) => row . id ) ,
6331+ ...released . flat ( ) ,
62566332 ] ) ,
6257- ] ) ,
6258- Effect . mapError ( ( cause ) => new ProjectionStoreReadError ( { threadId, cause } ) ) ,
6259- ) ;
6333+ ] ;
6334+ } ,
6335+ ( effect , threadId ) =>
6336+ effect . pipe ( Effect . mapError ( ( cause ) => new ProjectionStoreReadError ( { threadId, cause } ) ) ) ,
6337+ ) ;
62606338
62616339 const getMessagesById = Effect . fn ( "ProjectionStore.getMessagesById" ) (
62626340 function * ( threadId : ThreadId , messageIds : ReadonlyArray < MessageId > ) {
0 commit comments