@devries-cameron20 / t3code-1 / commits / 818c71d67d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

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

Co-authored-by: Julius Marminge <julius0216@outlook.com>

maria committed Oct 19, 2026 at 18:08 UTC 818c71d67d7870e4d88709828b3ee1aa6c4cf30c
4 files changed +96 -8
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+62
@@ -54,6 +54,68 @@ const layerTest = Layer.mergeAll(
54 ),
55 );
56
57 +it.effect("marks a never-visited thread's first completion unread", () =>
58 + Effect.gen(function* () {
59 + const orchestrator = yield* Orchestrator.OrchestratorV2;
60 + const projections = yield* ProjectionStore.ProjectionStoreV2;
61 + const threadId = ThreadId.make("thread:completion-watermark");
62 + yield* orchestrator.dispatch({
63 + type: "thread.create",
64 + commandId: CommandId.make("create-completion-watermark"),
65 + threadId,
66 + projectId: ProjectId.make("project:completion-watermark"),
67 + title: "Background task",
68 + modelSelection,
69 + runtimeMode: "full-access",
70 + interactionMode: "default",
71 + branch: null,
72 + worktreePath: null,
73 + createdBy: "user",
74 + creationSource: "web",
75 + });
76 + assert.isNull((yield* projections.getThreadShell(threadId))?.lastVisitedAt);
77 + const dispatch = (id: string) =>
78 + orchestrator.dispatch({
79 + type: "message.dispatch",
80 + commandId: CommandId.make(id),
81 + threadId,
82 + messageId: MessageId.make(id),
83 + text: "Run in the background",
84 + attachments: [],
85 + dispatchMode: { type: "defer_start" },
86 + createdBy: "agent",
87 + creationSource: "mcp",
88 + });
89 + yield* TestClock.adjust("1 second");
90 + const dispatchedAt = yield* DateTime.now;
91 + yield* dispatch("first-completion-watermark");
92 + yield* orchestrator.dispatch({
93 + type: "run.interrupt",
94 + commandId: CommandId.make("interrupt-completion-watermark"),
95 + threadId,
96 + runId: (yield* projections.getThreadProjection(threadId)).runs[0]!.id,
97 + });
98 + // The run ends in the dispatch's millisecond and must still be unread.
99 + const ended = yield* projections.getThreadShell(threadId);
100 + assert.ok(ended?.lastVisitedAt && ended.latestRunCompletedAt);
101 + assert.equal(
102 + DateTime.toEpochMillis(ended.lastVisitedAt),
103 + DateTime.toEpochMillis(dispatchedAt) - 1,
104 + );
105 + assert.isAbove(
106 + DateTime.toEpochMillis(ended.latestRunCompletedAt),
107 + DateTime.toEpochMillis(ended.lastVisitedAt),
108 + );
109 +
110 + yield* TestClock.adjust("1 second");
111 + yield* dispatch("second-completion-watermark");
112 + assert.deepEqual(
113 + (yield* projections.getThreadShell(threadId))?.lastVisitedAt,
114 + ended.lastVisitedAt,
115 + );
116 + }).pipe(Effect.provide(layerTest)),
117 +);
118 +
119 it.effect(
120 "dispatches metadata, queue resume and request controls without hydrating unrelated history",
121 () =>
apps/server/src/orchestration-v2/Orchestrator.ts
+21
@@ -4703,6 +4703,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
4703 }
4704 }
4705
4706 + if (projection.thread.lastVisitedAt === null && !isNativeMaintenanceCommand(command)) {
4707 + const now = yield* DateTime.now;
4708 + // Null keeps imported history read. The first fresh dispatch establishes
4709 + // a read watermark so its completion can be unread on every client.
4710 + // Offset it so a completion in the same millisecond is still unread.
4711 + const event = yield* emit(
4712 + events,
4713 + command,
4714 + )({
4715 + type: "thread.metadata-updated",
4716 + threadId: command.threadId,
4717 + providerInstanceId: projection.thread.providerInstanceId,
4718 + occurredAt: now,
4719 + payload: {
4720 + ...projection.thread,
4721 + lastVisitedAt: DateTime.subtract(now, { milliseconds: 1 }),
4722 + },
4723 + });
4724 + projection = applyToProjection(projection, event);
4725 + }
4726 +
4727 const continuedTaskId = projection.runs.find(
4728 (run) => run.id === command.restartContinuationOfRunId,
4729 )?.delegatedTaskId;
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+12 -8
@@ -734,6 +734,7 @@ it.effect.each([" /COMPACT ", "/logout"])(
734 assert.isUndefined(
735 (yield* threads.getThreadProjection(launched.threadId)).thread.titleRegeneration,
736 );
737 + assert.isNull((yield* threads.getThreadProjection(launched.threadId)).thread.lastVisitedAt);
738 assert.isFalse(
739 (yield* outbox.listByCommandId(CommandId.make("compact-title:initial-message"))).some(
740 (effect) => effect.request.type === "thread-title.generate",
@@ -3217,17 +3218,20 @@ it.effect.each(
3218 assert.equal(parent.thread.worktreePath, parentWorktree);
3219 assert.equal(parent.thread.branch, "parent-feature");
3220 if (cancelSetup) {
3221 + const afterSequence = yield* threads.getThreadEventSequence(delegated.childThreadId);
3222 yield* service.cancelTask(scope, { taskId: delegated.taskId });
3223 yield* Deferred.await(terminalClosed);
3224 if (!cancelAsync) {
3223 - yield* threads.streamStoredEventsFrom({ threadId: delegated.childThreadId }).pipe(
3224 - Stream.filter(
3225 - (stored) =>
3226 - stored.event.type === "thread.metadata-updated" &&
3227 - stored.event.payload.worktreePath === null,
3228 - ),
3229 - Stream.runHead,
3230 - );
3225 + yield* threads
3226 + .streamStoredEventsFrom({ threadId: delegated.childThreadId, afterSequence })
3227 + .pipe(
3228 + Stream.filter(
3229 + (stored) =>
3230 + stored.event.type === "thread.metadata-updated" &&
3231 + stored.event.payload.worktreePath === null,
3232 + ),
3233 + Stream.runHead,
3234 + );
3235 }
3236 assert.equal(harness.closeTerminal.mock.calls.length, 1);
3237 assert.equal(harness.removeWorktree.mock.calls.length, cancelAsync ? 0 : 1);
apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts
+1
@@ -316,6 +316,7 @@ it.layer(layerTest)("LegacyV1ThreadImporter", (it) => {
316 });
317 const projection = yield* projections.getThreadProjection(threadId);
318 assert.equal(projection.thread.title, "Renamed after shell import");
319 + assert.isNull(projection.thread.lastVisitedAt);
320 assert.equal(projection.thread.runtimeMode, "approval-required");
321 assert.equal(projection.thread.interactionMode, "plan");
322 assert.deepEqual(projection.thread.archivedAt, renamedAt);