Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,67 @@ it.layer(layerTest)("delegated completion delivery repairs", (it) => {
}),
);

it.effect.each([
{ name: "no result is waiting", delivery: "none", compacts: true },
{ name: "a result is waiting to be sent", delivery: "pending", compacts: false },
{ name: "a result was just sent", delivery: "sent", compacts: false },
] as const)("compacts before the cache expires only when $name", ({ delivery, compacts }) =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const now = yield* DateTime.now;
const threadId = ThreadId.make(`thread:idle-compaction-delivery-${delivery}`);
const runId = RunId.make(`run:idle-compaction-delivery-${delivery}`);
const taskId = NodeId.make(`node:idle-compaction-delivery-${delivery}-task`);

yield* seedParentWithTerminalTask({
threadId,
projectId: ProjectId.make(`project:idle-compaction-delivery-${delivery}`),
runId,
rootNodeId: NodeId.make(`node:idle-compaction-delivery-${delivery}-root`),
taskId,
deliveryState: delivery === "none" ? "delivered" : "claimed",
completionWake: "always",
parentRunStatus: "completed",
...(delivery === "none" ? {} : { deliveryTaskIds: [taskId] }),
now,
});
if (delivery === "sent") {
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make(`command:idle-compaction-delivery-${delivery}`),
threadId,
messageId: MessageId.make(`message:delegated-delivery:${threadId}`),
text: `Delegated task ${taskId} reached a terminal state.`,
attachments: [],
dispatchMode: { type: "queue_after_active" },
createdBy: "agent",
creationSource: "server",
delegatedCompletion: { parentRunId: runId, generation: 1, taskIds: [taskId] },
});
}

const compactionId = MessageId.make(`idle-compact:${threadId}:${runId}`);
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make(`server:idle-compact:${threadId}:${runId}`),
threadId,
messageId: compactionId,
text: "/compact",
attachments: [],
createdBy: "agent",
creationSource: "server",
idleCompaction: "beforeCacheExpires",
dispatchMode: { type: "start_immediately" },
});

const projection = yield* orchestrator.getThreadProjection(threadId);
assert.equal(
projection.messages.some((message) => message.id === compactionId),
compacts,
);
}),
);

it.effect.each([
{
name: "oversized UTF-8 result",
Expand Down
27 changes: 27 additions & 0 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5949,6 +5949,33 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}
}

// The service checked idleness before this command; a run or a delegated result that landed
// since makes the thread busy, and that turn refreshes the cache anyway.
if (
command.idleCompaction === "beforeCacheExpires" &&
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
projection.runs.some(
(run) =>
run.status === "queued" ||
isActiveRun(run) ||
(run.delegatedCompletion?.disposition === "open" &&
run.delegatedCompletion.delivery !== null &&
!projection.messages.some(
(message) => message.id === run.delegatedCompletion?.delivery?.messageId,
)),
Comment thread
Byntham marked this conversation as resolved.
Resolved
)
) {
yield* emit(
events,
command,
)({
type: "thread.metadata-updated",
threadId: command.threadId,
occurredAt: yield* DateTime.now,
payload: projection.thread,
});
return;
}

if (projection.thread.lastVisitedAt === null && !isNativeMaintenanceCommand(command)) {
const now = yield* DateTime.now;
// Null keeps imported history read. The first fresh dispatch establishes
Expand Down
Loading