@@ -444,8 +444,10 @@ export interface ProjectionStoreV2Shape {
444444 readonly searchThread : (
445445 input : OrchestrationV2SearchThreadInput ,
446446 ) => Effect . Effect < OrchestrationV2SearchThreadResult , ProjectionStoreV2Error > ;
447+ /** `deletedWith` are threads deleted in the same commit, so they keep no files. */
447448 readonly getThreadAttachmentIds : (
448449 threadId : ThreadId ,
450+ deletedWith ?: ReadonlyArray < ThreadId > ,
449451 ) => Effect . Effect < ReadonlyArray < string > , ProjectionStoreV2Error > ;
450452 readonly getThreadHistoryPage : (
451453 threadId : ThreadId ,
@@ -1248,6 +1250,7 @@ interface FamilyAttachment {
12481250/** Forks show their source's files without copying them, so a file outlives the thread that sent it. */
12491251function unreadThreadAttachmentIds ( input : {
12501252 readonly threadId : ThreadId ;
1253+ readonly deletedWith : ReadonlyArray < ThreadId > ;
12511254 readonly threads : ReadonlyArray < FamilyThread > ;
12521255 readonly attachments : ReadonlyArray < FamilyAttachment > ;
12531256} ) : Array < string > {
@@ -1279,7 +1282,9 @@ function unreadThreadAttachmentIds(input: {
12791282 } ;
12801283 const kept = new Set (
12811284 input . threads . flatMap ( ( thread ) =>
1282- thread . deleted || thread . id === input . threadId ? [ ] : [ ...shownBy ( thread . id ) ] ,
1285+ thread . deleted || thread . id === input . threadId || input . deletedWith . includes ( thread . id )
1286+ ? [ ]
1287+ : [ ...shownBy ( thread . id ) ] ,
12831288 ) ,
12841289 ) ;
12851290 return [ ...shownBy ( input . threadId ) ] . filter ( ( id ) => ! kept . has ( id ) ) ;
@@ -5500,7 +5505,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
55005505 Effect . mapError ( controlReadError ( threadId ) ) ,
55015506 ) ;
55025507
5503- const getThreadAttachmentIds : ProjectionStoreV2Shape [ "getThreadAttachmentIds" ] = ( threadId ) => {
5508+ const getThreadAttachmentIds : ProjectionStoreV2Shape [ "getThreadAttachmentIds" ] = (
5509+ threadId ,
5510+ deletedWith = [ ] ,
5511+ ) => {
55045512 const family = sql `
55055513 SELECT thread_id, deleted_at, payload_json
55065514 FROM orchestration_v2_projection_threads
@@ -5517,6 +5525,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
55175525 run_ordinal : number | null ;
55185526 id : string ;
55195527 } ;
5528+ // CROSS JOIN keeps family as the outer loop; without planner stats SQLite scans every transcript.
55205529 return Effect . all ( [
55215530 sql < {
55225531 thread_id : ThreadId ;
@@ -5549,7 +5558,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
55495558 run.ordinal AS run_ordinal,
55505559 json_extract(attachment.value, '$.id') AS id
55515560 FROM family
5552- JOIN orchestration_v2_projection_messages AS message
5561+ CROSS JOIN orchestration_v2_projection_messages AS message
55535562 ON message.thread_id = family.thread_id
55545563 JOIN json_each(message.payload_json, '$.attachments') AS attachment
55555564 LEFT JOIN orchestration_v2_projection_runs AS run
@@ -5563,7 +5572,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
55635572 run.ordinal AS run_ordinal,
55645573 json_extract(attachment.value, '$.id') AS id
55655574 FROM family
5566- JOIN orchestration_v2_projection_turn_items AS item
5575+ CROSS JOIN orchestration_v2_projection_turn_items AS item
55675576 ON item.thread_id = family.thread_id
55685577 JOIN json_each(item.payload_json, '$.questionAnswer.attachmentsByQuestionId') AS question
55695578 JOIN json_each(question.value) AS attachment
@@ -5584,7 +5593,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
55845593 run.ordinal AS run_ordinal,
55855594 item.payload_json
55865595 FROM family
5587- JOIN orchestration_v2_projection_turn_items AS item
5596+ CROSS JOIN orchestration_v2_projection_turn_items AS item
55885597 ON item.thread_id = family.thread_id
55895598 LEFT JOIN orchestration_v2_projection_runs AS run
55905599 ON run.run_id = item.run_id AND run.thread_id = item.thread_id
@@ -5615,6 +5624,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56155624 ...new Set ( [
56165625 ...unreadThreadAttachmentIds ( {
56175626 threadId,
5627+ deletedWith,
56185628 threads : threads . map ( ( row ) => ( {
56195629 id : row . thread_id ,
56205630 deleted : row . deleted_at !== null ,
0 commit comments