Skip to content

Commit 7266f08

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

2 files changed

Lines changed: 66 additions & 7 deletions

File tree

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,58 @@ it.effect("gives the guest a command's detail and output, but no file diffs", ()
530530
}).pipe(Effect.provide(layerTest)),
531531
);
532532

533+
it.effect("gives a fork's guest the commands the fork shows from its source thread", () =>
534+
Effect.gen(function* () {
535+
const { orchestrator, guestShares } = yield* shareThread;
536+
const command = yield* writeItem("item:command", {
537+
type: "command_execution",
538+
status: "completed",
539+
input: "ls /work/src",
540+
output: "/work/src/auth.ts\n",
541+
});
542+
const eventSink = yield* EventSink.EventSinkV2;
543+
const now = yield* DateTime.now;
544+
const run = (yield* orchestrator.getThreadRecords(threadId, ["runs"])).runs[0]!;
545+
yield* eventSink.write({
546+
events: [
547+
{
548+
id: EventId.make("event:run-done"),
549+
type: "run.updated",
550+
threadId,
551+
runId: run.id,
552+
providerInstanceId: instanceId,
553+
occurredAt: now,
554+
payload: { ...run, status: "completed", completedAt: now },
555+
},
556+
],
557+
});
558+
const forkId = ThreadId.make("thread:guest-share-fork");
559+
yield* orchestrator.dispatch({
560+
type: "thread.fork",
561+
commandId: CommandId.make("fork"),
562+
sourceThreadId: threadId,
563+
targetThreadId: forkId,
564+
sourcePoint: { type: "run", runId: run.id },
565+
createdBy: "user",
566+
creationSource: "web",
567+
});
568+
const { url } = yield* guestShares.create({ threadId: forkId });
569+
const { token: session } = yield* guestShares.redeem({
570+
credential: url.slice(url.indexOf("#") + 1),
571+
name: "Sam",
572+
});
573+
574+
const detail = (yield* guestShares.item(session, command)).item;
575+
assert.deepInclude(detail, { input: "ls /work/src", output: "/work/src/auth.ts\n" });
576+
const frames = Array.from(
577+
yield* Stream.runCollect(yield* guestShares.commandOutput(session, command)),
578+
);
579+
assert.deepEqual(frames, [
580+
{ kind: "replace", text: "/work/src/auth.ts\n", truncated: false, running: false },
581+
]);
582+
}).pipe(Effect.provide(layerTest)),
583+
);
584+
533585
it.effect("pages the guest back through turns older than the stream's snapshot", () =>
534586
Effect.gen(function* () {
535587
const { guestShares, credential } = yield* shareThread;

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -644,17 +644,24 @@ const make = Effect.gen(function* () {
644644
itemId: string,
645645
) {
646646
const { thread } = yield* authorize(session, "session");
647-
const item = yield* orchestrator
648-
.getTurnItem({ threadId: thread.id, itemId: TurnItemId.make(itemId) })
649-
.pipe(Effect.mapError(() => unavailable));
647+
const id = TurnItemId.make(itemId);
648+
const item =
649+
(yield* orchestrator
650+
.getTurnItem({ threadId: thread.id, itemId: id })
651+
.pipe(Effect.mapError(() => unavailable))) ??
652+
// A fork shows its source's earlier items, which stay in the source thread.
653+
(yield* orchestrator.getTimelinePage(thread.id, { itemId: id, limit: 1 }).pipe(
654+
Effect.map((page) => page.items[0]?.item ?? null),
655+
Effect.mapError(() => unavailable),
656+
));
650657
return item === null ? yield* unavailable : { thread, item };
651658
});
652659

653660
const render = Effect.fn("GuestShareService.render")(function* (session: string, itemId: string) {
654-
const { thread, item } = yield* threadItem(session, itemId);
661+
const { item } = yield* threadItem(session, itemId);
655662
if (item.type !== "dynamic_tool") return yield* unavailable;
656663
const reference = htmlRenderFromToolItem(item);
657-
const [attachmentId] = threadHtmlRenderAttachmentIds(thread.id, [item]);
664+
const [attachmentId] = threadHtmlRenderAttachmentIds(item.threadId, [item]);
658665
const path =
659666
attachmentId === undefined
660667
? null
@@ -690,9 +697,9 @@ const make = Effect.gen(function* () {
690697
session: string,
691698
itemId: string,
692699
) {
693-
const { thread, item } = yield* threadItem(session, itemId);
700+
const { item } = yield* threadItem(session, itemId);
694701
if (item.type !== "command_execution") return yield* unavailable;
695-
return commandOutputs.subscribe({ threadId: thread.id, itemId: item.id }).pipe(
702+
return commandOutputs.subscribe({ threadId: item.threadId, itemId: item.id }).pipe(
696703
// A lost hub stream ends the output; the row reloads it when reopened.
697704
Stream.catchCause(() => Stream.empty),
698705
Stream.interruptWhen(untilRevoked(session)),

0 commit comments

Comments
 (0)