Skip to content

Commit 3bd64cf

Browse files
flambohmaria-rcks
andauthored
fix(server): mark unread works after an automatic compaction (pingdotgg#25286)
Co-authored-by: maria-rcks <[email protected]>
1 parent ef51594 commit 3bd64cf

2 files changed

Lines changed: 116 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
@@ -3229,8 +3229,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
32293229
}
32303230
let markUnreadVisitedAt: DateTime.Utc | null = null;
32313231
if (command.type === "thread.mark-unread") {
3232-
const projection = yield* loadProjectionForCommand(command, ["runs"]);
3233-
const latestRunCompletedAt = projection.runs.at(-1)?.completedAt ?? null;
3232+
const shell = yield* projectionStore
3233+
.getThreadShell(command.threadId)
3234+
.pipe(
3235+
Effect.mapError(
3236+
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
3237+
),
3238+
);
3239+
const latestRunCompletedAt = shell?.latestRunCompletedAt ?? null;
32343240
if (latestRunCompletedAt === null) {
32353241
return yield* new OrchestratorDispatchError({
32363242
commandId: command.commandId,

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6278,6 +6278,114 @@ it.layer(layerSharedApplicationDataPlaneTest)("visited projection", (it) => {
62786278
assert.instanceOf(invalidVisit, Orchestrator.OrchestratorDispatchError);
62796279
}),
62806280
);
6281+
6282+
it.effect("marks unread before the user's last turn when an automatic compaction followed", () =>
6283+
Effect.gen(function* () {
6284+
const orchestrator = yield* Orchestrator.OrchestratorV2;
6285+
const threadId = ThreadId.make("runtime-layer-mark-unread-compacted");
6286+
const turnCompletedAt = DateTime.makeUnsafe("2026-07-24T10:00:00.000Z");
6287+
yield* orchestrator.dispatch({
6288+
type: "thread.create",
6289+
createdBy: "user",
6290+
creationSource: "web",
6291+
commandId: CommandId.make(`${threadId}:create`),
6292+
threadId,
6293+
projectId: ProjectId.make(`${threadId}:project`),
6294+
title: "Compacted thread",
6295+
modelSelection,
6296+
runtimeMode: "full-access",
6297+
interactionMode: "default",
6298+
branch: null,
6299+
worktreePath: null,
6300+
});
6301+
const runs = [
6302+
{ text: "Continue", at: turnCompletedAt },
6303+
{
6304+
text: "/compact",
6305+
idleCompaction: "beforeCacheExpires" as const,
6306+
at: DateTime.makeUnsafe("2026-07-24T11:00:00.000Z"),
6307+
},
6308+
];
6309+
yield* (yield* EventSink.EventSinkV2).write({
6310+
commandId: CommandId.make(`${threadId}:seed`),
6311+
events: runs.flatMap(({ at, ...message }, index) => {
6312+
const runId = RunId.make(`${threadId}:run-${index}`);
6313+
const messageId = MessageId.make(`${threadId}:message-${index}`);
6314+
return [
6315+
{
6316+
id: EventId.make(`${messageId}:event`),
6317+
type: "message.updated" as const,
6318+
threadId,
6319+
occurredAt: at,
6320+
payload: {
6321+
...message,
6322+
id: messageId,
6323+
threadId,
6324+
runId,
6325+
nodeId: null,
6326+
role: "user" as const,
6327+
attachments: [],
6328+
streaming: false,
6329+
createdBy: "user" as const,
6330+
creationSource: "web" as const,
6331+
createdAt: at,
6332+
updatedAt: at,
6333+
},
6334+
},
6335+
{
6336+
id: EventId.make(`${runId}:event`),
6337+
type: "run.created" as const,
6338+
threadId,
6339+
runId,
6340+
providerInstanceId: modelSelection.instanceId,
6341+
occurredAt: at,
6342+
payload: {
6343+
id: runId,
6344+
threadId,
6345+
ordinal: index + 1,
6346+
providerInstanceId: modelSelection.instanceId,
6347+
modelSelection,
6348+
providerThreadId: null,
6349+
userMessageId: messageId,
6350+
rootNodeId: null,
6351+
activeAttemptId: null,
6352+
status: "completed" as const,
6353+
queuePosition: null,
6354+
requestedAt: at,
6355+
startedAt: at,
6356+
completedAt: at,
6357+
checkpointId: null,
6358+
contextHandoffId: null,
6359+
},
6360+
},
6361+
];
6362+
}),
6363+
});
6364+
6365+
yield* orchestrator.dispatch({
6366+
type: "thread.mark-unread",
6367+
commandId: CommandId.make(`${threadId}:mark-unread`),
6368+
threadId,
6369+
});
6370+
const projection = yield* orchestrator.getThreadProjection(threadId);
6371+
assert.deepEqual(
6372+
projection.thread.lastVisitedAt,
6373+
DateTime.subtract(turnCompletedAt, { milliseconds: 1 }),
6374+
);
6375+
const shell = yield* orchestrator.getThreadShell(threadId);
6376+
assert.deepEqual(shell!.latestRunCompletedAt, turnCompletedAt);
6377+
assert.deepEqual(shell!.lastVisitedAt, projection.thread.lastVisitedAt);
6378+
6379+
yield* orchestrator.dispatch({
6380+
type: "thread.visit",
6381+
commandId: CommandId.make(`${threadId}:visit`),
6382+
threadId,
6383+
visitedAt: DateTime.formatIso(turnCompletedAt),
6384+
});
6385+
const revisited = yield* orchestrator.getThreadShell(threadId);
6386+
assert.deepEqual(revisited!.lastVisitedAt, turnCompletedAt);
6387+
}),
6388+
);
62816389
});
62826390

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

0 commit comments

Comments
 (0)