Skip to content

Commit adba15a

Browse files
saphidclaude
andcommitted
fix(server): guests of a forked thread can open files on the messages it inherited
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5b9731c commit adba15a

2 files changed

Lines changed: 90 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
@@ -961,6 +961,82 @@ it.effect("serves the shared thread's files to its guest, never another thread's
961961
}).pipe(Effect.scoped, Effect.provide(layerTest)),
962962
);
963963

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

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,20 @@ const make = Effect.gen(function* () {
717717
.find((candidate) => candidate.id === attachmentId),
718718
),
719719
Effect.mapError(() => unavailable),
720-
));
720+
)) ??
721+
// A fork shows its source's earlier messages, which stay in the source thread.
722+
(thread.forkedFrom === null
723+
? 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+
));
721734
const path =
722735
found === undefined ? null : resolveAttachmentPath({ attachmentsDir, attachment: found });
723736
if (found === undefined || path === null) return yield* unavailable;

0 commit comments

Comments
 (0)