@@ -1528,7 +1528,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
15281528 });
15291529
15301530 // Runs in every command transaction; avoid reading thousands of finished leaf nodes.
1531- const readCommandProjection = (threadId: ThreadId) =>
1531+ const readCommandProjection = (
1532+ threadId: ThreadId,
1533+ recent?: ProjectionRecordFilter["sinceLastCompaction"],
1534+ ) =>
15321535 projectionStore
15331536 .getThreadRecords(
15341537 threadId,
@@ -1557,6 +1560,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
15571560 omitSettledSubagentRecords: true,
15581561 // Commands act on pending forks and merge-backs only; consumed ones stay unread.
15591562 contextTransferStatuses: ["pending"],
1563+ ...(recent === undefined ? {} : { sinceLastCompaction: recent }),
15601564 },
15611565 )
15621566 .pipe(
@@ -1645,9 +1649,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16451649 Effect.fnUntraced(function* (
16461650 threadId: ThreadId,
16471651 events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
1652+ recent?: ProjectionRecordFilter["sinceLastCompaction"],
16481653 ) {
16491654 const pending = (yield* Ref.get(events)).filter((event) => event.threadId === threadId);
1650- const stored = yield* Effect.option(readCommandProjection(threadId));
1655+ const stored = yield* Effect.option(readCommandProjection(threadId, recent ));
16511656 let projection: OrchestrationV2ThreadProjection;
16521657 if (Option.isSome(stored)) {
16531658 projection = stored.value;
@@ -6307,7 +6312,28 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
63076312 events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
63086313 effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
63096314 ) {
6310- let projection = yield* getProjectionWithPendingEvents(command.threadId, events);
6315+ // A compacted thread reads every run and turn item, but attempts, nodes, messages and provider
6316+ // turns only for its recent runs and the runs this command names. An idle compaction reads
6317+ // in full: its busy check below looks for a delegated result's message under any old run.
6318+ let projection = yield* getProjectionWithPendingEvents(
6319+ command.threadId,
6320+ events,
6321+ command.idleCompaction === undefined
6322+ ? {
6323+ keepRunIds: [
6324+ command.manualContinuationOfRunId,
6325+ command.restartContinuationOfRunId,
6326+ command.usageLimitContinuationOfRunId,
6327+ command.steerFollowUpOfRunId,
6328+ ...(command.dispatchMode.type === "steer_active" ||
6329+ command.dispatchMode.type === "restart_active"
6330+ ? [command.dispatchMode.targetRunId]
6331+ : []),
6332+ ].flatMap((runId) => (runId === undefined ? [] : [runId])),
6333+ keepMessageId: command.messageId,
6334+ }
6335+ : undefined,
6336+ );
63116337 if (command.steerFollowUpOfRunId !== undefined) {
63126338 const source = projection.runs.find((run) => run.id === command.steerFollowUpOfRunId);
63136339 if (
@@ -6344,6 +6370,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
63446370 });
63456371 return;
63466372 }
6373+ const activeProviderInstanceId = projection.providerThreads.find(
6374+ (candidate) => candidate.id === projection.thread.activeProviderThreadId,
6375+ )?.providerInstanceId;
6376+ // Another provider is handed what ran since it last saw the thread, which can be older than
6377+ // the compaction, so a provider switch reads the whole history.
6378+ if (
6379+ activeProviderInstanceId !== undefined &&
6380+ activeProviderInstanceId !==
6381+ (command.modelSelection ?? projection.thread.modelSelection).instanceId
6382+ ) {
6383+ projection = yield* getProjectionWithPendingEvents(command.threadId, events);
6384+ }
63476385 if (command.manualContinuationOfRunId !== undefined) {
63486386 const source = projection.runs.find((run) => run.id === command.manualContinuationOfRunId);
63496387 const limited = latestRootProviderFailure(source ?? null, projection.turnItems);
@@ -6547,15 +6585,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
65476585 projection = applyToProjection(projection, event);
65486586 }
65496587 const userMessages = projection.messages.filter((message) => message.role === "user");
6550- const onlyMaintenanceHistory =
6551- userMessages.length > 0 && userMessages.every(isNativeMaintenanceCommand);
6588+ // The recent runs of a compacted thread can hold only a `/compact` while an older prompt
6589+ // exists. Every prompt's turn item is loaded; without one, every user message is checked.
6590+ const onlyMaintenanceHistory = [
6591+ ...userMessages,
6592+ ...projection.turnItems.flatMap((item) => (item.type === "user_message" ? [item] : [])),
6593+ ].every(isNativeMaintenanceCommand)
6594+ ? projectionStore
6595+ .getThreadRecords(command.threadId, ["messages"], { messageRoles: ["user"] })
6596+ .pipe(
6597+ Effect.map(({ messages }) => [...messages, ...userMessages]),
6598+ Effect.map((all) => all.length > 0 && all.every(isNativeMaintenanceCommand)),
6599+ mapDispatchError(command),
6600+ )
6601+ : Effect.succeed(false);
65526602 if (
65536603 !isNativeMaintenanceCommand(command) &&
65546604 ((command.titleSeed !== undefined &&
65556605 (yield* projectionStore
65566606 .getMessageCount(command.threadId)
65576607 .pipe(mapDispatchError(command))) === 0) ||
6558- (command.createdBy === "user" && onlyMaintenanceHistory))
6608+ (command.createdBy === "user" && (yield* onlyMaintenanceHistory) ))
65596609 ) {
65606610 const now = yield* DateTime.now;
65616611 const thread: OrchestrationV2AppThread = {
0 commit comments