Skip to content

Commit 16a1464

Browse files
fix(server): recognise scheduled fresh runs after a compaction
A compacted thread's dispatch reads messages only for runs since the latest compaction, so an older scheduled fresh run was taken for a user /clear again and the user's next message went to its session. Read the scheduled marker from the user_message turn items, which are not narrowed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0de4c9c commit 16a1464

2 files changed

Lines changed: 54 additions & 9 deletions

File tree

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,19 +1270,23 @@ function appendContextHandoffId(
12701270
/** A fresh-session schedule run starts a side conversation; the thread's own one continues past it. */
12711271
function isScheduledFreshRun(
12721272
run: Pick<OrchestrationV2Run, "freshContext" | "userMessageId">,
1273-
messages: OrchestrationV2ThreadProjection["messages"],
1273+
turnItems: OrchestrationV2ThreadProjection["turnItems"],
12741274
): boolean {
1275+
// Turn items, unlike messages, are not narrowed to the runs since the latest compaction.
12751276
return (
12761277
run.freshContext === true &&
1277-
messages.some(
1278-
(message) => message.id === run.userMessageId && message.scheduledTaskId !== undefined,
1278+
turnItems.some(
1279+
(item) =>
1280+
item.type === "user_message" &&
1281+
item.messageId === run.userMessageId &&
1282+
item.scheduledTaskId !== undefined,
12791283
)
12801284
);
12811285
}
12821286

12831287
/** Ordinal of the latest started fresh-context run. Runs before it never reach a provider again. */
12841288
function contextBoundaryOrdinal(
1285-
projection: Pick<OrchestrationV2ThreadProjection, "runs" | "messages">,
1289+
projection: Pick<OrchestrationV2ThreadProjection, "runs" | "turnItems">,
12861290
): number {
12871291
let boundary = 0;
12881292
for (const run of projection.runs) {
@@ -1291,7 +1295,7 @@ function contextBoundaryOrdinal(
12911295
run.status !== "queued" &&
12921296
!cancelledBeforeStart(run) &&
12931297
run.ordinal > boundary &&
1294-
!isScheduledFreshRun(run, projection.messages)
1298+
!isScheduledFreshRun(run, projection.turnItems)
12951299
) {
12961300
boundary = run.ordinal;
12971301
}
@@ -1303,14 +1307,14 @@ function contextBoundaryOrdinal(
13031307
function conversationProviderThread(
13041308
projection: Pick<
13051309
OrchestrationV2ThreadProjection,
1306-
"thread" | "providerThreads" | "runs" | "messages"
1310+
"thread" | "providerThreads" | "runs" | "turnItems"
13071311
>,
13081312
): OrchestrationV2ProviderThread | undefined {
13091313
const startedByScheduledFreshRun = (providerThread: OrchestrationV2ProviderThread) =>
13101314
projection.runs.some(
13111315
(run) =>
13121316
run.ordinal === providerThread.firstRunOrdinal &&
1313-
isScheduledFreshRun(run, projection.messages),
1317+
isScheduledFreshRun(run, projection.turnItems),
13141318
);
13151319
const active = projection.providerThreads.find(
13161320
(candidate) => candidate.id === projection.thread.activeProviderThreadId,
@@ -1336,7 +1340,7 @@ function conversationProviderThread(
13361340
function dispatchProviderThread(
13371341
projection: Pick<
13381342
OrchestrationV2ThreadProjection,
1339-
"thread" | "providerThreads" | "runs" | "messages"
1343+
"thread" | "providerThreads" | "runs" | "turnItems"
13401344
>,
13411345
message: Pick<OrchestrationV2ConversationMessage, "notification" | "delegatedCompletion">,
13421346
freshContext: boolean,
@@ -1387,7 +1391,7 @@ function uniqueSessions(
13871391
function rootProviderThreadsForProvider(
13881392
projection: Pick<
13891393
OrchestrationV2ThreadProjection,
1390-
"providerThreads" | "thread" | "runs" | "messages"
1394+
"providerThreads" | "thread" | "runs" | "turnItems"
13911395
>,
13921396
providerInstanceId: ModelSelection["instanceId"],
13931397
): ReadonlyArray<OrchestrationV2ProviderThread> {

‎apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,11 +3469,51 @@ describe("orchestration v2 provider switching", () => {
34693469
branch: null,
34703470
worktreePath: cwd,
34713471
});
3472+
const eventSink = yield* EventSink.EventSinkV2;
34723473
for (const [name, scheduled] of [
34733474
["user-first", false],
34743475
["scheduled", true],
34753476
["user-second", false],
3477+
["compaction", false],
3478+
["scheduled-after-compaction", true],
3479+
["user-third", false],
34763480
] as const) {
3481+
if (name === "compaction") {
3482+
// The user's run compacts, so later dispatches no longer read the first scheduled run's message.
3483+
const run = (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)!;
3484+
const compactedAt = yield* DateTime.now;
3485+
yield* eventSink.write({
3486+
events: [
3487+
{
3488+
id: EventId.make("event:scheduled-fresh-user-chat:compaction"),
3489+
type: "turn-item.updated",
3490+
threadId,
3491+
runId: run.id,
3492+
nodeId: run.rootNodeId!,
3493+
occurredAt: compactedAt,
3494+
payload: {
3495+
id: TurnItemId.make("item:scheduled-fresh-user-chat:compaction"),
3496+
threadId,
3497+
runId: run.id,
3498+
nodeId: run.rootNodeId,
3499+
providerThreadId: null,
3500+
providerTurnId: null,
3501+
nativeItemRef: null,
3502+
parentItemId: null,
3503+
ordinal: 1_000,
3504+
status: "completed",
3505+
title: null,
3506+
startedAt: compactedAt,
3507+
completedAt: compactedAt,
3508+
updatedAt: compactedAt,
3509+
type: "compaction",
3510+
driver: CLAUDE_DRIVER,
3511+
},
3512+
},
3513+
],
3514+
});
3515+
continue;
3516+
}
34773517
yield* send(name, scheduled);
34783518
yield* worker.drain();
34793519
yield* waitForIdle(threadId);
@@ -3486,6 +3526,7 @@ describe("orchestration v2 provider switching", () => {
34863526
);
34873527
// The scheduled run's fresh session must not replace the user's conversation.
34883528
assert.equal(turnFor("user-second")!.nativeThreadId, turnFor("user-first")!.nativeThreadId);
3529+
assert.equal(turnFor("user-third")!.nativeThreadId, turnFor("user-first")!.nativeThreadId);
34893530
}).pipe(
34903531
Effect.provide(
34913532
ProviderReplayHarness.layerWithRegistry(

0 commit comments

Comments
 (0)