Skip to content

Commit a9468da

Browse files
saphidclaude
andauthored
fix(server): guests of a forked thread can open files on the messages it inherited (pingdotgg#29794)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 7f17f51 commit a9468da

3 files changed

Lines changed: 106 additions & 1 deletion

File tree

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,82 @@ it.effect("serves the shared thread's files to its guest, never another thread's
965965
}).pipe(Effect.scoped, Effect.provide(layerTest)),
966966
);
967967

968+
it.effect("gives a fork's guest the files on messages the fork shows from its source", () =>
969+
Effect.gen(function* () {
970+
const { orchestrator } = yield* shareThread;
971+
const guestShares = yield* GuestShareService.GuestShareService;
972+
const { attachmentsDir } = yield* ServerConfig.ServerConfig;
973+
const fileSystem = yield* FileSystem.FileSystem;
974+
const ownerImage = Effect.fn(function* (name: string) {
975+
const image = {
976+
type: "image" as const,
977+
id: ChatAttachmentId.make(createAttachmentId(threadId)!),
978+
name,
979+
mimeType: "image/png",
980+
sizeBytes: PNG.length,
981+
};
982+
yield* fileSystem.writeFile(
983+
resolveAttachmentPath({ attachmentsDir, attachment: image })!,
984+
PNG,
985+
);
986+
yield* orchestrator.dispatch({
987+
type: "message.dispatch",
988+
commandId: CommandId.make(`owner:${name}`),
989+
threadId,
990+
messageId: MessageId.make(`message:${name}`),
991+
text: name,
992+
attachments: [image],
993+
dispatchMode: { type: "defer_start" },
994+
createdBy: "user",
995+
creationSource: "web",
996+
});
997+
return image;
998+
});
999+
const inherited = yield* ownerImage("before.png");
1000+
const run = (yield* orchestrator.getThreadRecords(threadId, ["runs"])).runs[0]!;
1001+
const now = yield* DateTime.now;
1002+
yield* (yield* EventSink.EventSinkV2).write({
1003+
events: [
1004+
{
1005+
id: EventId.make("event:run-done"),
1006+
type: "run.updated",
1007+
threadId,
1008+
runId: run.id,
1009+
providerInstanceId: instanceId,
1010+
occurredAt: now,
1011+
payload: { ...run, status: "completed", completedAt: now },
1012+
},
1013+
],
1014+
});
1015+
const forkId = ThreadId.make("thread:guest-share-fork");
1016+
yield* orchestrator.dispatch({
1017+
type: "thread.fork",
1018+
commandId: CommandId.make("fork"),
1019+
sourceThreadId: threadId,
1020+
targetThreadId: forkId,
1021+
sourcePoint: { type: "run", runId: run.id },
1022+
createdBy: "user",
1023+
creationSource: "web",
1024+
});
1025+
const later = yield* ownerImage("after.png");
1026+
const { url } = yield* guestShares.create({ threadId: forkId });
1027+
const { token: session } = yield* guestShares.redeem({
1028+
credential: url.slice(url.indexOf("#") + 1),
1029+
name: "Sam",
1030+
});
1031+
1032+
assert.equal(
1033+
(yield* guestShares.attachment(session, inherited.id)).attachment.id,
1034+
inherited.id,
1035+
);
1036+
// The source's messages after the fork point are not part of the fork.
1037+
assert.equal(
1038+
(yield* Effect.flip(guestShares.attachment(session, later.id))).reason,
1039+
"unavailable",
1040+
);
1041+
}).pipe(Effect.provide(layerTest)),
1042+
);
1043+
9681044
it.effect("serves an image on the host only when an agent reply the guest sees shows it", () =>
9691045
Effect.gen(function* () {
9701046
const { orchestrator, guestShares } = yield* shareThread;

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,18 @@ const make = Effect.gen(function* () {
715715
.find((candidate) => candidate.id === attachmentId),
716716
),
717717
Effect.mapError(() => unavailable),
718-
));
718+
)) ??
719+
// A fork shows its source's earlier messages, which stay in the source thread.
720+
(thread.forkedFrom === null
721+
? undefined
722+
: yield* orchestrator.getTimelinePage(thread.id, { attachmentId, limit: 1 }).pipe(
723+
Effect.map(({ items }) =>
724+
items
725+
.flatMap(({ item }) => (item.type === "user_message" ? item.attachments : []))
726+
.find((candidate) => candidate.id === attachmentId),
727+
),
728+
Effect.mapError(() => unavailable),
729+
));
719730
const path =
720731
found === undefined ? null : resolveAttachmentPath({ attachmentsDir, attachment: found });
721732
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
}
@@ -5835,6 +5836,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
58355836
.withTransaction(
58365837
Effect.gen(function* () {
58375838
const index = yield* readTimelineIndex(threadId, new Set());
5839+
const attachmentOwners =
5840+
options.attachmentId === undefined
5841+
? undefined
5842+
: new Set(
5843+
(yield* sql<{ thread_id: string; turn_item_id: string }>`
5844+
SELECT item.thread_id, item.turn_item_id
5845+
FROM orchestration_v2_projection_turn_items AS item,
5846+
json_each(item.payload_json, '$.attachments') AS attachment
5847+
WHERE item.thread_id IN ${sql.in([...new Set(index.visible.map((row) => row.sourceThreadId))])}
5848+
AND item.type = 'user_message'
5849+
AND json_extract(attachment.value, '$.id') = ${options.attachmentId}`).map(
5850+
(row) => `${row.thread_id}:${row.turn_item_id}`,
5851+
),
5852+
);
58385853
const matching = index.visible
58395854
.map((row, position) => ({ ...row, position }))
58405855
.filter((row) =>
@@ -5845,6 +5860,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
58455860
row.item.type,
58465861
))
58475862
: row.sourceItemId === options.itemId,
5863+
)
5864+
.filter(
5865+
(row) => attachmentOwners?.has(`${row.sourceThreadId}:${row.sourceItemId}`) ?? true,
58485866
);
58495867
const page = matching.slice(0, options.limit);
58505868
const sources = yield* encodeTimelineSources(

0 commit comments

Comments
 (0)