Skip to content

Commit 2615e21

Browse files
fix(server): a portable fork with no surviving history rejects /compact
Counting every run fork as conversation let a portable fork whose handoff budget omitted all inherited messages compact an empty thread. A fork now counts only when it resolves natively or its portable handoff carries at least one message. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4bc67e6 commit 2615e21

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,26 @@ const activeRunStatuses: ReadonlySet<ProjectionRun["status"]> = new Set([
351351
"waiting",
352352
]);
353353

354+
// A fork inherits its source's history before it has messages of its own,
355+
// unless a portable handoff's budget left none of that history to deliver.
356+
const inheritsForkConversation = ({
357+
thread,
358+
contextTransfers,
359+
contextHandoffs,
360+
}: Pick<OrchestrationV2ThreadProjection, "thread" | "contextTransfers" | "contextHandoffs">) => {
361+
if (thread.forkedFrom?.type !== "run") return false;
362+
const resolution = contextTransfers.find(
363+
(transfer) => transfer.type === "fork" && transfer.targetThreadId === thread.id,
364+
)?.resolution;
365+
return (
366+
resolution?.strategy !== "portable_context" ||
367+
contextHandoffs.some(
368+
(handoff) =>
369+
handoff.id === resolution.contextHandoffId && (handoff.history?.messages.length ?? 0) > 0,
370+
)
371+
);
372+
};
373+
354374
// In-memory twin of the omitSettledLeafNodes SQL clause in readCanonicalProjection.
355375
const isSettledLeafNode = (node: ProjectionNode, runs: ReadonlyArray<ProjectionRun>) =>
356376
settledLeafNodeKinds.has(node.kind) &&
@@ -4265,8 +4285,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
42654285
) AS present
42664286
`;
42674287
return {
4268-
// A fork inherits its source's history before it has messages of its own.
4269-
hasConversation: thread.forkedFrom?.type === "run" || conversation[0]?.present === 1,
4288+
hasConversation:
4289+
conversation[0]?.present === 1 ||
4290+
inheritsForkConversation({ thread, contextTransfers, contextHandoffs }),
42704291
thread,
42714292
runs,
42724293
attempts,
@@ -7251,13 +7272,12 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
72517272
Effect.map((projection) => ({
72527273
...projection,
72537274
hasConversation:
7254-
projection.thread.forkedFrom?.type === "run" ||
72557275
projection.messages.some(
72567276
(message) =>
72577277
message.role === "user" &&
72587278
(message.text.trim().toLowerCase() !== "/compact" ||
72597279
message.attachments.length > 0),
7260-
),
7280+
) || inheritsForkConversation(projection),
72617281
turnItems: projection.turnItems.filter((item) => item.runId === runId),
72627282
})),
72637283
),

‎apps/server/src/orchestration-v2/TurnStartReads.test.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
EventId,
77
MessageId,
88
ProjectId,
9+
ProviderDriverKind,
910
ProviderInstanceId,
1011
RunId,
1112
ThreadId,
@@ -369,7 +370,7 @@ it.effect.each(["sqlite", "memory"] as const)(
369370
);
370371

371372
it.effect.each(["sqlite", "memory"] as const)(
372-
"counts a fork's inherited history as conversation for its first /compact in %s",
373+
"counts a native fork's inherited history as conversation for its first /compact in %s",
373374
(storage) =>
374375
Effect.gen(function* () {
375376
const store = yield* ProjectionStore.ProjectionStoreV2;
@@ -485,10 +486,17 @@ it.effect.each(["sqlite", "memory"] as const)(
485486
sourcePoint: { threadId: sourceId, runId: sourceRunId },
486487
basePoint: null,
487488
sourceProviderInstanceId: instanceId,
488-
targetProviderInstanceId: null,
489-
targetRunId: null,
489+
targetProviderInstanceId: instanceId,
490+
targetRunId: forkRunId,
490491
status: "pending",
491-
resolution: null,
492+
resolution: {
493+
strategy: "native_fork",
494+
providerThreadRef: {
495+
driver: ProviderDriverKind.make("claudeAgent"),
496+
nativeId: "native-fork-source",
497+
strength: "strong",
498+
},
499+
},
492500
createdBy: "user",
493501
error: null,
494502
createdAt: now,

0 commit comments

Comments
 (0)