Skip to content
TypeScript

fix(server): guests of a forked thread can open files on the messages it inherited (#29794)

Alex Southwella9468dab74df
diff --git a/apps/server/src/guest/GuestShareService.test.ts b/apps/server/src/guest/GuestShareService.test.ts
index a22e6182c77..cfd8ca1363f 100644
--- a/apps/server/src/guest/GuestShareService.test.ts
+++ b/apps/server/src/guest/GuestShareService.test.ts
@@ -965,6 +965,82 @@ it.effect("serves the shared thread's files to its guest, never another thread's
}).pipe(Effect.scoped, Effect.provide(layerTest)),
);
+it.effect("gives a fork's guest the files on messages the fork shows from its source", () =>
+ Effect.gen(function* () {
+ const { orchestrator } = yield* shareThread;
+ const guestShares = yield* GuestShareService.GuestShareService;
+ const { attachmentsDir } = yield* ServerConfig.ServerConfig;
+ const fileSystem = yield* FileSystem.FileSystem;
+ const ownerImage = Effect.fn(function* (name: string) {
+ const image = {
+ type: "image" as const,
+ id: ChatAttachmentId.make(createAttachmentId(threadId)!),
+ name,
+ mimeType: "image/png",
+ sizeBytes: PNG.length,
+ };
+ yield* fileSystem.writeFile(
+ resolveAttachmentPath({ attachmentsDir, attachment: image })!,
+ PNG,
+ );
+ yield* orchestrator.dispatch({
+ type: "message.dispatch",
+ commandId: CommandId.make(`owner:${name}`),
+ threadId,
+ messageId: MessageId.make(`message:${name}`),
+ text: name,
+ attachments: [image],
+ dispatchMode: { type: "defer_start" },
+ createdBy: "user",
+ creationSource: "web",
+ });
+ return image;
+ });
+ const inherited = yield* ownerImage("before.png");
+ const run = (yield* orchestrator.getThreadRecords(threadId, ["runs"])).runs[0]!;
+ const now = yield* DateTime.now;
+ yield* (yield* EventSink.EventSinkV2).write({
+ events: [
+ {
+ id: EventId.make("event:run-done"),
+ type: "run.updated",
+ threadId,
+ runId: run.id,
+ providerInstanceId: instanceId,
+ occurredAt: now,
+ payload: { ...run, status: "completed", completedAt: now },
+ },
+ ],
+ });
+ const forkId = ThreadId.make("thread:guest-share-fork");
+ yield* orchestrator.dispatch({
+ type: "thread.fork",
+ commandId: CommandId.make("fork"),
+ sourceThreadId: threadId,
+ targetThreadId: forkId,
+ sourcePoint: { type: "run", runId: run.id },
+ createdBy: "user",
+ creationSource: "web",
+ });
+ const later = yield* ownerImage("after.png");
+ const { url } = yield* guestShares.create({ threadId: forkId });
+ const { token: session } = yield* guestShares.redeem({
+ credential: url.slice(url.indexOf("#") + 1),
+ name: "Sam",
+ });
+
+ assert.equal(
+ (yield* guestShares.attachment(session, inherited.id)).attachment.id,
+ inherited.id,
+ );
+ // The source's messages after the fork point are not part of the fork.
+ assert.equal(
+ (yield* Effect.flip(guestShares.attachment(session, later.id))).reason,
+ "unavailable",
+ );
+ }).pipe(Effect.provide(layerTest)),
+);
+
it.effect("serves an image on the host only when an agent reply the guest sees shows it", () =>
Effect.gen(function* () {
const { orchestrator, guestShares } = yield* shareThread;
diff --git a/apps/server/src/guest/GuestShareService.ts b/apps/server/src/guest/GuestShareService.ts
index 8298e10e4df..fd9a0efc3b6 100644
--- a/apps/server/src/guest/GuestShareService.ts
+++ b/apps/server/src/guest/GuestShareService.ts
@@ -715,7 +715,18 @@ const make = Effect.gen(function* () {
.find((candidate) => candidate.id === attachmentId),
),
Effect.mapError(() => unavailable),
- ));
+ )) ??
+ // A fork shows its source's earlier messages, which stay in the source thread.
+ (thread.forkedFrom === null
+ ? undefined
+ : yield* orchestrator.getTimelinePage(thread.id, { attachmentId, limit: 1 }).pipe(
+ Effect.map(({ items }) =>
+ items
+ .flatMap(({ item }) => (item.type === "user_message" ? item.attachments : []))
+ .find((candidate) => candidate.id === attachmentId),
+ ),
+ Effect.mapError(() => unavailable),
+ ));
const path =
found === undefined ? null : resolveAttachmentPath({ attachmentsDir, attachment: found });
if (found === undefined || path === null) return yield* unavailable;
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts
index d67da31d073..6186db8c6e3 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts
@@ -394,6 +394,7 @@ export type ProjectionRecords<K extends ProjectionRecordField> = Pick<
export interface ProjectionTimelinePageOptions {
readonly afterPosition?: number;
readonly itemId?: TurnItemId;
+ readonly attachmentId?: string;
readonly view?: "messages" | "activity";
readonly limit: number;
}
@@ -5835,6 +5836,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
.withTransaction(
Effect.gen(function* () {
const index = yield* readTimelineIndex(threadId, new Set());
+ const attachmentOwners =
+ options.attachmentId === undefined
+ ? undefined
+ : new Set(
+ (yield* sql<{ thread_id: string; turn_item_id: string }>`
+ SELECT item.thread_id, item.turn_item_id
+ FROM orchestration_v2_projection_turn_items AS item,
+ json_each(item.payload_json, '$.attachments') AS attachment
+ WHERE item.thread_id IN ${sql.in([...new Set(index.visible.map((row) => row.sourceThreadId))])}
+ AND item.type = 'user_message'
+ AND json_extract(attachment.value, '$.id') = ${options.attachmentId}`).map(
+ (row) => `${row.thread_id}:${row.turn_item_id}`,
+ ),
+ );
const matching = index.visible
.map((row, position) => ({ ...row, position }))
.filter((row) =>
@@ -5845,6 +5860,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
row.item.type,
))
: row.sourceItemId === options.itemId,
+ )
+ .filter(
+ (row) => attachmentOwners?.has(`${row.sourceThreadId}:${row.sourceItemId}`) ?? true,
);
const page = matching.slice(0, options.limit);
const sources = yield* encodeTimelineSources(