Skip to content
TypeScript

fix(server): mark unvisited thread completions unread (#15607)

maria818c71d67d78
diff --git a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
index aad904a6c42..d68fd950a20 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
@@ -54,6 +54,68 @@ const layerTest = Layer.mergeAll(
),
);
+it.effect("marks a never-visited thread's first completion unread", () =>
+ Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
+ const threadId = ThreadId.make("thread:completion-watermark");
+ yield* orchestrator.dispatch({
+ type: "thread.create",
+ commandId: CommandId.make("create-completion-watermark"),
+ threadId,
+ projectId: ProjectId.make("project:completion-watermark"),
+ title: "Background task",
+ modelSelection,
+ runtimeMode: "full-access",
+ interactionMode: "default",
+ branch: null,
+ worktreePath: null,
+ createdBy: "user",
+ creationSource: "web",
+ });
+ assert.isNull((yield* projections.getThreadShell(threadId))?.lastVisitedAt);
+ const dispatch = (id: string) =>
+ orchestrator.dispatch({
+ type: "message.dispatch",
+ commandId: CommandId.make(id),
+ threadId,
+ messageId: MessageId.make(id),
+ text: "Run in the background",
+ attachments: [],
+ dispatchMode: { type: "defer_start" },
+ createdBy: "agent",
+ creationSource: "mcp",
+ });
+ yield* TestClock.adjust("1 second");
+ const dispatchedAt = yield* DateTime.now;
+ yield* dispatch("first-completion-watermark");
+ yield* orchestrator.dispatch({
+ type: "run.interrupt",
+ commandId: CommandId.make("interrupt-completion-watermark"),
+ threadId,
+ runId: (yield* projections.getThreadProjection(threadId)).runs[0]!.id,
+ });
+ // The run ends in the dispatch's millisecond and must still be unread.
+ const ended = yield* projections.getThreadShell(threadId);
+ assert.ok(ended?.lastVisitedAt && ended.latestRunCompletedAt);
+ assert.equal(
+ DateTime.toEpochMillis(ended.lastVisitedAt),
+ DateTime.toEpochMillis(dispatchedAt) - 1,
+ );
+ assert.isAbove(
+ DateTime.toEpochMillis(ended.latestRunCompletedAt),
+ DateTime.toEpochMillis(ended.lastVisitedAt),
+ );
+
+ yield* TestClock.adjust("1 second");
+ yield* dispatch("second-completion-watermark");
+ assert.deepEqual(
+ (yield* projections.getThreadShell(threadId))?.lastVisitedAt,
+ ended.lastVisitedAt,
+ );
+ }).pipe(Effect.provide(layerTest)),
+);
+
it.effect(
"dispatches metadata, queue resume and request controls without hydrating unrelated history",
() =>
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index 48df36a3a7a..96b6bab3c03 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -4703,6 +4703,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}
}
+ if (projection.thread.lastVisitedAt === null && !isNativeMaintenanceCommand(command)) {
+ const now = yield* DateTime.now;
+ // Null keeps imported history read. The first fresh dispatch establishes
+ // a read watermark so its completion can be unread on every client.
+ // Offset it so a completion in the same millisecond is still unread.
+ const event = yield* emit(
+ events,
+ command,
+ )({
+ type: "thread.metadata-updated",
+ threadId: command.threadId,
+ providerInstanceId: projection.thread.providerInstanceId,
+ occurredAt: now,
+ payload: {
+ ...projection.thread,
+ lastVisitedAt: DateTime.subtract(now, { milliseconds: 1 }),
+ },
+ });
+ projection = applyToProjection(projection, event);
+ }
+
const continuedTaskId = projection.runs.find(
(run) => run.id === command.restartContinuationOfRunId,
)?.delegatedTaskId;
diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
index 8bdf7230fd0..b10af63ce94 100644
--- a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+++ b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
@@ -734,6 +734,7 @@ it.effect.each([" /COMPACT ", "/logout"])(
assert.isUndefined(
(yield* threads.getThreadProjection(launched.threadId)).thread.titleRegeneration,
);
+ assert.isNull((yield* threads.getThreadProjection(launched.threadId)).thread.lastVisitedAt);
assert.isFalse(
(yield* outbox.listByCommandId(CommandId.make("compact-title:initial-message"))).some(
(effect) => effect.request.type === "thread-title.generate",
@@ -3217,17 +3218,20 @@ it.effect.each(
assert.equal(parent.thread.worktreePath, parentWorktree);
assert.equal(parent.thread.branch, "parent-feature");
if (cancelSetup) {
+ const afterSequence = yield* threads.getThreadEventSequence(delegated.childThreadId);
yield* service.cancelTask(scope, { taskId: delegated.taskId });
yield* Deferred.await(terminalClosed);
if (!cancelAsync) {
- yield* threads.streamStoredEventsFrom({ threadId: delegated.childThreadId }).pipe(
- Stream.filter(
- (stored) =>
- stored.event.type === "thread.metadata-updated" &&
- stored.event.payload.worktreePath === null,
- ),
- Stream.runHead,
- );
+ yield* threads
+ .streamStoredEventsFrom({ threadId: delegated.childThreadId, afterSequence })
+ .pipe(
+ Stream.filter(
+ (stored) =>
+ stored.event.type === "thread.metadata-updated" &&
+ stored.event.payload.worktreePath === null,
+ ),
+ Stream.runHead,
+ );
}
assert.equal(harness.closeTerminal.mock.calls.length, 1);
assert.equal(harness.removeWorktree.mock.calls.length, cancelAsync ? 0 : 1);
diff --git a/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts b/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts
index 2f755fdaf7f..869cb27629c 100644
--- a/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts
+++ b/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts
@@ -316,6 +316,7 @@ it.layer(layerTest)("LegacyV1ThreadImporter", (it) => {
});
const projection = yield* projections.getThreadProjection(threadId);
assert.equal(projection.thread.title, "Renamed after shell import");
+ assert.isNull(projection.thread.lastVisitedAt);
assert.equal(projection.thread.runtimeMode, "approval-required");
assert.equal(projection.thread.interactionMode, "plan");
assert.deepEqual(projection.thread.archivedAt, renamedAt);