Skip to content

Commit 3e66c21

Browse files
committed
fix(server): mark unread works after an automatic compaction
1 parent 0a3b561 commit 3e66c21

2 files changed

Lines changed: 104 additions & 2 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3193,8 +3193,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
31933193
}
31943194
let markUnreadVisitedAt: DateTime.Utc | null = null;
31953195
if (command.type === "thread.mark-unread") {
3196-
const projection = yield* loadProjectionForCommand(command, ["runs"]);
3197-
const latestRunCompletedAt = projection.runs.at(-1)?.completedAt ?? null;
3196+
const shell = yield* projectionStore
3197+
.getThreadShell(command.threadId)
3198+
.pipe(
3199+
Effect.mapError(
3200+
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
3201+
),
3202+
);
3203+
const latestRunCompletedAt = shell?.latestRunCompletedAt ?? null;
31983204
if (latestRunCompletedAt === null) {
31993205
return yield* new OrchestratorDispatchError({
32003206
commandId: command.commandId,

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5688,6 +5688,102 @@ it.layer(layerSharedApplicationDataPlaneTest)("visited projection", (it) => {
56885688
assert.instanceOf(invalidVisit, Orchestrator.OrchestratorDispatchError);
56895689
}),
56905690
);
5691+
5692+
it.effect("marks unread before the user's last turn when an automatic compaction followed", () =>
5693+
Effect.gen(function* () {
5694+
const orchestrator = yield* Orchestrator.OrchestratorV2;
5695+
const threadId = ThreadId.make("runtime-layer-mark-unread-compacted");
5696+
const turnCompletedAt = DateTime.makeUnsafe("2026-07-24T10:00:00.000Z");
5697+
yield* orchestrator.dispatch({
5698+
type: "thread.create",
5699+
createdBy: "user",
5700+
creationSource: "web",
5701+
commandId: CommandId.make(`${threadId}:create`),
5702+
threadId,
5703+
projectId: ProjectId.make(`${threadId}:project`),
5704+
title: "Compacted thread",
5705+
modelSelection,
5706+
runtimeMode: "full-access",
5707+
interactionMode: "default",
5708+
branch: null,
5709+
worktreePath: null,
5710+
});
5711+
const runs = [
5712+
{ text: "Continue", at: turnCompletedAt },
5713+
{
5714+
text: "/compact",
5715+
idleCompaction: "beforeCacheExpires" as const,
5716+
at: DateTime.makeUnsafe("2026-07-24T11:00:00.000Z"),
5717+
},
5718+
];
5719+
yield* (yield* EventSink.EventSinkV2).write({
5720+
commandId: CommandId.make(`${threadId}:seed`),
5721+
events: runs.flatMap(({ at, ...message }, index) => {
5722+
const runId = RunId.make(`${threadId}:run-${index}`);
5723+
const messageId = MessageId.make(`${threadId}:message-${index}`);
5724+
return [
5725+
{
5726+
id: EventId.make(`${messageId}:event`),
5727+
type: "message.updated" as const,
5728+
threadId,
5729+
occurredAt: at,
5730+
payload: {
5731+
...message,
5732+
id: messageId,
5733+
threadId,
5734+
runId,
5735+
nodeId: null,
5736+
role: "user" as const,
5737+
attachments: [],
5738+
streaming: false,
5739+
createdBy: "user" as const,
5740+
creationSource: "web" as const,
5741+
createdAt: at,
5742+
updatedAt: at,
5743+
},
5744+
},
5745+
{
5746+
id: EventId.make(`${runId}:event`),
5747+
type: "run.created" as const,
5748+
threadId,
5749+
runId,
5750+
providerInstanceId: modelSelection.instanceId,
5751+
occurredAt: at,
5752+
payload: {
5753+
id: runId,
5754+
threadId,
5755+
ordinal: index + 1,
5756+
providerInstanceId: modelSelection.instanceId,
5757+
modelSelection,
5758+
providerThreadId: null,
5759+
userMessageId: messageId,
5760+
rootNodeId: null,
5761+
activeAttemptId: null,
5762+
status: "completed" as const,
5763+
queuePosition: null,
5764+
requestedAt: at,
5765+
startedAt: at,
5766+
completedAt: at,
5767+
checkpointId: null,
5768+
contextHandoffId: null,
5769+
},
5770+
},
5771+
];
5772+
}),
5773+
});
5774+
5775+
yield* orchestrator.dispatch({
5776+
type: "thread.mark-unread",
5777+
commandId: CommandId.make(`${threadId}:mark-unread`),
5778+
threadId,
5779+
});
5780+
const projection = yield* orchestrator.getThreadProjection(threadId);
5781+
assert.deepEqual(
5782+
projection.thread.lastVisitedAt,
5783+
DateTime.subtract(turnCompletedAt, { milliseconds: 1 }),
5784+
);
5785+
}),
5786+
);
56915787
});
56925788

56935789
it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("thread opens", (it) => {

0 commit comments

Comments
 (0)