Skip to content

Commit 818c71d

Browse files
fix(server): mark unvisited thread completions unread (pingdotgg#15607)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 83cf6db commit 818c71d

4 files changed

Lines changed: 96 additions & 8 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,68 @@ const layerTest = Layer.mergeAll(
5454
),
5555
);
5656

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+
57119
it.effect(
58120
"dispatches metadata, queue resume and request controls without hydrating unrelated history",
59121
() =>

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4703,6 +4703,27 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
47034703
}
47044704
}
47054705

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+
47064727
const continuedTaskId = projection.runs.find(
47074728
(run) => run.id === command.restartContinuationOfRunId,
47084729
)?.delegatedTaskId;

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ it.effect.each([" /COMPACT ", "/logout"])(
734734
assert.isUndefined(
735735
(yield* threads.getThreadProjection(launched.threadId)).thread.titleRegeneration,
736736
);
737+
assert.isNull((yield* threads.getThreadProjection(launched.threadId)).thread.lastVisitedAt);
737738
assert.isFalse(
738739
(yield* outbox.listByCommandId(CommandId.make("compact-title:initial-message"))).some(
739740
(effect) => effect.request.type === "thread-title.generate",
@@ -3217,17 +3218,20 @@ it.effect.each(
32173218
assert.equal(parent.thread.worktreePath, parentWorktree);
32183219
assert.equal(parent.thread.branch, "parent-feature");
32193220
if (cancelSetup) {
3221+
const afterSequence = yield* threads.getThreadEventSequence(delegated.childThreadId);
32203222
yield* service.cancelTask(scope, { taskId: delegated.taskId });
32213223
yield* Deferred.await(terminalClosed);
32223224
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+
);
32313235
}
32323236
assert.equal(harness.closeTerminal.mock.calls.length, 1);
32333237
assert.equal(harness.removeWorktree.mock.calls.length, cancelAsync ? 0 : 1);

‎apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ it.layer(layerTest)("LegacyV1ThreadImporter", (it) => {
316316
});
317317
const projection = yield* projections.getThreadProjection(threadId);
318318
assert.equal(projection.thread.title, "Renamed after shell import");
319+
assert.isNull(projection.thread.lastVisitedAt);
319320
assert.equal(projection.thread.runtimeMode, "approval-required");
320321
assert.equal(projection.thread.interactionMode, "plan");
321322
assert.deepEqual(projection.thread.archivedAt, renamedAt);

0 commit comments

Comments
 (0)