@@ -1530,7 +1530,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
15301530 // Runs in every command transaction; avoid reading thousands of finished leaf nodes.
15311531 const readCommandProjection = (
15321532 threadId: ThreadId,
1533- recent?: { keepRunIds: ReadonlyArray<RunId> } ,
1533+ recent?: ProjectionRecordFilter["sinceLastCompaction"] ,
15341534 ) =>
15351535 projectionStore
15361536 .getThreadRecords(
@@ -1556,6 +1556,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
15561556 // Commands here read only undelivered or active tasks; a dispatcher thread keeps
15571557 // thousands of delivered ones, each carrying its full prompt.
15581558 omitSettledSubagents: true,
1559+ // Commands act on pending forks and merge-backs only; consumed ones stay unread.
1560+ contextTransferStatuses: ["pending"],
15591561 ...(recent === undefined ? {} : { sinceLastCompaction: recent }),
15601562 },
15611563 )
@@ -1645,7 +1647,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16451647 Effect.fnUntraced(function* (
16461648 threadId: ThreadId,
16471649 events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
1648- recent?: { keepRunIds: ReadonlyArray<RunId> } ,
1650+ recent?: ProjectionRecordFilter["sinceLastCompaction"] ,
16491651 ) {
16501652 const pending = (yield* Ref.get(events)).filter((event) => event.threadId === threadId);
16511653 const stored = yield* Effect.option(readCommandProjection(threadId, recent));
@@ -6308,34 +6310,28 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
63086310 events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
63096311 effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
63106312 ) {
6311- // A compacted thread needs only its recent runs, plus any older run the command names.
6312- const targetRunId =
6313- command.dispatchMode.type === "steer_active" || command.dispatchMode.type === "restart_active"
6314- ? command.dispatchMode.targetRunId
6315- : undefined;
6316- const keepRunIds = [
6317- command.manualContinuationOfRunId,
6318- command.restartContinuationOfRunId,
6319- command.usageLimitContinuationOfRunId,
6320- command.delegatedCompletion?.parentRunId,
6321- targetRunId,
6322- command.steerFollowUpOfRunId,
6323- ].flatMap((runId) => (runId === undefined ? [] : [runId]));
6324- let projection = yield* getProjectionWithPendingEvents(command.threadId, events, {
6325- keepRunIds,
6326- });
6327- // Replacing a run's input settles the wake it carried, which needs that wake's parent run.
6328- // Untraced: the enclosing dispatchMessage span already covers both reloads.
6329- const keepWakeParentOf = Effect.fnUntraced(function* (runId: RunId | undefined) {
6330- const parentRunId = projection.messages.find(
6331- (message) => message.id === projection.runs.find((run) => run.id === runId)?.userMessageId,
6332- )?.delegatedCompletion?.parentRunId;
6333- if (parentRunId === undefined || projection.runs.some((run) => run.id === parentRunId))
6334- return;
6335- keepRunIds.push(parentRunId);
6336- projection = yield* getProjectionWithPendingEvents(command.threadId, events, { keepRunIds });
6337- });
6338- yield* keepWakeParentOf(targetRunId);
6313+ // A compacted thread reads every run and turn item, but attempts, nodes, messages and provider
6314+ // turns only for its recent runs and the runs this command names. An idle compaction reads
6315+ // in full: its busy check below looks for a delegated result's message under any old run.
6316+ let projection = yield* getProjectionWithPendingEvents(
6317+ command.threadId,
6318+ events,
6319+ command.idleCompaction === undefined
6320+ ? {
6321+ keepRunIds: [
6322+ command.manualContinuationOfRunId,
6323+ command.restartContinuationOfRunId,
6324+ command.usageLimitContinuationOfRunId,
6325+ command.steerFollowUpOfRunId,
6326+ ...(command.dispatchMode.type === "steer_active" ||
6327+ command.dispatchMode.type === "restart_active"
6328+ ? [command.dispatchMode.targetRunId]
6329+ : []),
6330+ ].flatMap((runId) => (runId === undefined ? [] : [runId])),
6331+ keepMessageId: command.messageId,
6332+ }
6333+ : undefined,
6334+ );
63396335 if (command.steerFollowUpOfRunId !== undefined) {
63406336 const source = projection.runs.find((run) => run.id === command.steerFollowUpOfRunId);
63416337 if (
@@ -6375,12 +6371,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
63756371 const activeProviderInstanceId = projection.providerThreads.find(
63766372 (candidate) => candidate.id === projection.thread.activeProviderThreadId,
63776373 )?.providerInstanceId;
6378- // A fresh session or another provider is handed the whole history, so it reads all of it.
6374+ // Another provider is handed what ran since it last saw the thread, which can be older than
6375+ // the compaction, so a provider switch reads the whole history.
63796376 if (
6380- command.freshContext === true ||
6381- (activeProviderInstanceId !== undefined &&
6382- activeProviderInstanceId !==
6383- (command.modelSelection ?? projection.thread.modelSelection).instanceId)
6377+ activeProviderInstanceId !== undefined &&
6378+ activeProviderInstanceId !==
6379+ (command.modelSelection ?? projection.thread.modelSelection).instanceId
63846380 ) {
63856381 projection = yield* getProjectionWithPendingEvents(command.threadId, events);
63866382 }
@@ -6586,16 +6582,28 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
65866582 });
65876583 projection = applyToProjection(projection, event);
65886584 }
6585+ const userMessages = projection.messages.filter((message) => message.role === "user");
6586+ // The recent runs of a compacted thread can hold only a `/compact` while an older prompt
6587+ // exists. Every prompt's turn item is loaded; without one, every user message is checked.
6588+ const onlyMaintenanceHistory = [
6589+ ...userMessages,
6590+ ...projection.turnItems.flatMap((item) => (item.type === "user_message" ? [item] : [])),
6591+ ].every(isNativeMaintenanceCommand)
6592+ ? projectionStore
6593+ .getThreadRecords(command.threadId, ["messages"], { messageRoles: ["user"] })
6594+ .pipe(
6595+ Effect.map(({ messages }) => [...messages, ...userMessages]),
6596+ Effect.map((all) => all.length > 0 && all.every(isNativeMaintenanceCommand)),
6597+ mapDispatchError(command),
6598+ )
6599+ : Effect.succeed(false);
65896600 if (
65906601 !isNativeMaintenanceCommand(command) &&
65916602 ((command.titleSeed !== undefined &&
65926603 (yield* projectionStore
65936604 .getMessageCount(command.threadId)
65946605 .pipe(mapDispatchError(command))) === 0) ||
6595- (command.createdBy === "user" &&
6596- (yield* projectionStore
6597- .hasOnlyMaintenanceUserMessages(command.threadId)
6598- .pipe(mapDispatchError(command)))))
6606+ (command.createdBy === "user" && (yield* onlyMaintenanceHistory)))
65996607 ) {
66006608 const now = yield* DateTime.now;
66016609 const thread: OrchestrationV2AppThread = {
@@ -6629,9 +6637,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
66296637 command.dispatchMode,
66306638 command.deliveryIntent,
66316639 );
6632- if (dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active") {
6633- yield* keepWakeParentOf(dispatchMode.targetRunId);
6634- }
66356640 if (dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active") {
66366641 const targetRunId = dispatchMode.targetRunId;
66376642 const target = projection.runs.find((run) => run.id === targetRunId);
0 commit comments