Skip to content

Commit 6be2685

Browse files
fix(server): side chat fork context recovers history from the parent thread (pingdotgg#34845)
1 parent 28b4cee commit 6be2685

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export interface ContextHandoffServiceV2Shape {
6969
}) => Effect.Effect<OrchestrationV2ContextHandoff, ContextHandoffServiceV2Error>;
7070
readonly prepareProviderHandoff: (input: {
7171
readonly threadId: ThreadId;
72+
/** Thread the items came from, when it is not `threadId` (a fork's source). */
73+
readonly sourceThreadId?: ThreadId;
7274
readonly targetRunId: RunId;
7375
readonly transferId: OrchestrationV2ContextHandoff["transferId"];
7476
readonly fromProviderThreadIds: ReadonlyArray<ProviderThreadId>;
@@ -487,6 +489,7 @@ const makeContextHandoffService = Effect.fn("orchestrationV2.ContextHandoffServi
487489
"orchestrationV2.contextHandoff.prepareProviderHandoff",
488490
)(function* (input: {
489491
readonly threadId: ThreadId;
492+
readonly sourceThreadId?: ThreadId;
490493
readonly targetRunId: RunId;
491494
readonly transferId: OrchestrationV2ContextHandoff["transferId"];
492495
readonly fromProviderThreadIds: ReadonlyArray<ProviderThreadId>;
@@ -521,7 +524,10 @@ const makeContextHandoffService = Effect.fn("orchestrationV2.ContextHandoffServi
521524
),
522525
);
523526
const runStatuses = new Map(input.runs?.map((run) => [run.id, run.status]));
524-
const coverage = handoffCoverage(input);
527+
const coverage = handoffCoverage({
528+
...input,
529+
threadId: input.sourceThreadId ?? input.threadId,
530+
});
525531
const selected = selectHistory({
526532
messages: input.items.flatMap((item) => {
527533
const message = historicalMessage(item);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7897,6 +7897,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
78977897
: yield* contextHandoffService
78987898
.prepareProviderHandoff({
78997899
threadId: command.threadId,
7900+
sourceThreadId: sourceProjection.thread.id,
79007901
targetRunId: runId,
79017902
transferId: pendingForkTransfer.id,
79027903
fromProviderThreadIds:
@@ -8336,7 +8337,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
83368337
)
83378338
: {}),
83388339
};
8339-
const activeHandoff = portableForkHandoff ?? mergeBackHandoff ?? providerSwitchHandoff;
8340+
// A side chat's "Context from" divider already marks its copied context.
8341+
const activeHandoff =
8342+
(sideChatFork ? null : portableForkHandoff) ?? mergeBackHandoff ?? providerSwitchHandoff;
83408343
const handoffSourceRuns =
83418344
portableForkHandoff !== null
83428345
? sourceRun === null

‎apps/server/src/orchestration-v2/ThreadFork.execution.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,40 @@ it.effect("resolves an explicit checkpoint fork to its source run", () =>
935935
}).pipe(Effect.provide(forkCases[0]!.layer)),
936936
);
937937

938+
it.effect("a side chat of a busy parent points history recovery at the parent", () =>
939+
Effect.gen(function* () {
940+
const orchestrator = yield* Orchestrator.OrchestratorV2;
941+
const parentId = ThreadId.make("side-chat-recovery-parent");
942+
const sideChatId = ThreadId.make("side-chat-recovery-child");
943+
yield* createParent(parentId);
944+
yield* writeParentRun({
945+
threadId: parentId,
946+
runId: RunId.make("side-chat-recovery-stable-run"),
947+
ordinal: 1,
948+
status: "completed",
949+
text: "Earlier parent history",
950+
stable: true,
951+
});
952+
yield* writeParentRun({
953+
threadId: parentId,
954+
runId: RunId.make("side-chat-recovery-busy-run"),
955+
ordinal: 2,
956+
status: "running",
957+
text: "Parent work in progress",
958+
});
959+
yield* forkSideChat("side-chat-recovery-fork", parentId, sideChatId);
960+
yield* askSideChat(sideChatId, "side-chat-recovery-first-message");
961+
962+
const started = yield* orchestrator.getThreadProjection(sideChatId);
963+
assert.equal(started.contextTransfers[0]?.resolution?.strategy, "portable_context");
964+
const handoff = started.contextHandoffs.find((item) => item.strategy === "full_thread_summary");
965+
const coverage = handoff?.history?.coverage ?? "";
966+
assert.include(coverage, `t3_thread_read({threadId:"${parentId}"`);
967+
assert.notInclude(coverage, `threadId:"${sideChatId}"`);
968+
assert.isFalse(started.turnItems.some((item) => item.type === "handoff"));
969+
}).pipe(Effect.provide(forkCases[0]!.layer)),
970+
);
971+
938972
it.effect("a side chat of an idle parent still starts with the framing note", () =>
939973
Effect.gen(function* () {
940974
const orchestrator = yield* Orchestrator.OrchestratorV2;

0 commit comments

Comments
 (0)