Skip to content

Commit b1da145

Browse files
saphidclaude
andcommitted
perf(server): a fork's guest attachment lookup reads only the message that holds the file
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent adba15a commit b1da145

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

‎apps/server/src/guest/GuestShareService.ts‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -721,16 +721,14 @@ const make = Effect.gen(function* () {
721721
// A fork shows its source's earlier messages, which stay in the source thread.
722722
(thread.forkedFrom === null
723723
? undefined
724-
: yield* orchestrator
725-
.getTimelinePage(thread.id, { view: "messages", limit: Number.MAX_SAFE_INTEGER })
726-
.pipe(
727-
Effect.map(({ items }) =>
728-
items
729-
.flatMap(({ item }) => (item.type === "user_message" ? item.attachments : []))
730-
.find((candidate) => candidate.id === attachmentId),
731-
),
732-
Effect.mapError(() => unavailable),
733-
));
724+
: yield* orchestrator.getTimelinePage(thread.id, { attachmentId, limit: 1 }).pipe(
725+
Effect.map(({ items }) =>
726+
items
727+
.flatMap(({ item }) => (item.type === "user_message" ? item.attachments : []))
728+
.find((candidate) => candidate.id === attachmentId),
729+
),
730+
Effect.mapError(() => unavailable),
731+
));
734732
const path =
735733
found === undefined ? null : resolveAttachmentPath({ attachmentsDir, attachment: found });
736734
if (found === undefined || path === null) return yield* unavailable;

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ export type ProjectionRecords<K extends ProjectionRecordField> = Pick<
394394
export interface ProjectionTimelinePageOptions {
395395
readonly afterPosition?: number;
396396
readonly itemId?: TurnItemId;
397+
readonly attachmentId?: string;
397398
readonly view?: "messages" | "activity";
398399
readonly limit: number;
399400
}
@@ -5815,6 +5816,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
58155816
.withTransaction(
58165817
Effect.gen(function* () {
58175818
const index = yield* readTimelineIndex(threadId, new Set());
5819+
const attachmentOwners =
5820+
options.attachmentId === undefined
5821+
? undefined
5822+
: new Set(
5823+
(yield* sql<{ thread_id: string; turn_item_id: string }>`
5824+
SELECT item.thread_id, item.turn_item_id
5825+
FROM orchestration_v2_projection_turn_items AS item,
5826+
json_each(item.payload_json, '$.attachments') AS attachment
5827+
WHERE item.thread_id IN ${sql.in([...new Set(index.visible.map((row) => row.sourceThreadId))])}
5828+
AND item.type = 'user_message'
5829+
AND json_extract(attachment.value, '$.id') = ${options.attachmentId}`).map(
5830+
(row) => `${row.thread_id}:${row.turn_item_id}`,
5831+
),
5832+
);
58185833
const matching = index.visible
58195834
.map((row, position) => ({ ...row, position }))
58205835
.filter((row) =>
@@ -5825,6 +5840,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
58255840
row.item.type,
58265841
))
58275842
: row.sourceItemId === options.itemId,
5843+
)
5844+
.filter(
5845+
(row) => attachmentOwners?.has(`${row.sourceThreadId}:${row.sourceItemId}`) ?? true,
58285846
);
58295847
const page = matching.slice(0, options.limit);
58305848
const sources = yield* encodeTimelineSources(

0 commit comments

Comments
 (0)