@devries-cameron20 / t3code-1 / commits / d19467a59e
Fork 0

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

fix(server): a thread visit stores only its read state (#36233)

Julius Marminge committed Nov 13, 2026 at 09:06 UTC d19467a59e028f700f360f5f7210dc9b893dff88
6 files changed +78 -7
apps/server/src/orchestration-v2/Orchestrator.ts
+7 -5
@@ -3726,19 +3726,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
3726 command.searchQuery,
3727 )
3728 : null;
3729 - // Viewing a thread changes read state only. Loading its transcript (or
3730 - // bumping updatedAt) makes a routine read receipt scale with its history.
3729 + if (!movesForward && opens === null) return;
3730 + // Viewing a thread changes read state only. Loading its transcript, bumping
3731 + // updatedAt, or copying the thread into the event makes a read receipt scale with it.
3732 yield* emit(
3733 events,
3734 command,
3735 )({
3735 - type: "thread.visited",
3736 + type: "thread.visit-recorded",
3737 threadId: command.threadId,
3738 providerInstanceId: thread.providerInstanceId,
3739 occurredAt,
3740 payload: {
3740 - ...thread,
3741 - ...(movesForward ? { lastVisitedAt: visitedAt.value } : {}),
3741 + lastVisitedAt: movesForward ? visitedAt.value : thread.lastVisitedAt,
3742 ...(opens ? { opens } : {}),
3743 },
3744 });
@@ -14991,6 +14991,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14991 planned.events.length > 0 ||
14992 isUnobservedAcknowledgement(command) ||
14993 command.type === "thread.background-work.settle" ||
14994 + // A stale visit changes no read state.
14995 + command.type === "thread.visit" ||
14996 // A read can answer a watch again before a push's reopen lands; nothing is left to clear.
14997 command.type === "thread.pull-request-watch.checks-reopen" ||
14998 command.type === "thread.interrupt.unresponsive" ||
apps/server/src/orchestration-v2/ProjectionStore.ts
+16 -1
@@ -1,5 +1,5 @@
1 import type { ProjectThreadAwarenessV2Input } from "@t3tools/shared/agentAwareness";
2 -import { pendingGuestMessageCount } from "@t3tools/contracts";
2 +import { applyOrchestrationV2ThreadVisit, pendingGuestMessageCount } from "@t3tools/contracts";
3 import { applyThreadPullRequestLinkUpdate } from "../sourceControl/threadPullRequests.ts";
4 import { projectTurnItemForWire } from "./WireProjection.ts";
5 import * as Stream from "effect/Stream";
@@ -927,6 +927,11 @@ export function applyToProjection(
927 ...projection,
928 thread: event.payload,
929 };
930 + case "thread.visit-recorded":
931 + return {
932 + ...projection,
933 + thread: applyOrchestrationV2ThreadVisit(projection.thread, event.payload),
934 + };
935 case "run.created":
936 case "run.updated":
937 return withLocalVisibleTurnItems({
@@ -2242,6 +2247,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2247 });
2248 break;
2249 }
2250 + case "thread.visit-recorded": {
2251 + const thread = yield* getThread(event.threadId);
2252 + yield* apply({
2253 + ...event,
2254 + type: "thread.visited",
2255 + payload: applyOrchestrationV2ThreadVisit(thread, event.payload),
2256 + });
2257 + break;
2258 + }
2259 case "thread.created":
2260 case "thread.archived":
2261 case "thread.unarchived":
@@ -3074,6 +3088,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3088 event.type !== "thread.active-reordered" &&
3089 event.type !== "thread.tags-updated" &&
3090 event.type !== "thread.visited" &&
3091 + event.type !== "thread.visit-recorded" &&
3092 event.type !== "thread.marked-unread" &&
3093 event.type !== "thread.metadata-updated" &&
3094 event.type !== "thread.pull-request-synced" &&
apps/server/src/orchestration-v2/runtimeLayer.test.ts
+15 -1
@@ -9902,6 +9902,19 @@ it.layer(layerSharedApplicationDataPlaneTest)("visited projection", (it) => {
9902 assert.isNotNull(visited.thread.lastVisitedAt);
9903 assert.equal(DateTime.formatIso(visited.thread.lastVisitedAt!), visitedAt);
9904 assert.deepEqual(visited.thread.updatedAt, createdUpdatedAt);
9905 + const sql = yield* SqlClient.SqlClient;
9906 + const visitPayloadKeys = (commandId: string) =>
9907 + sql<{ readonly payload_json: string }>`
9908 + SELECT payload_json FROM orchestration_events WHERE command_id = ${commandId}
9909 + `.pipe(
9910 + Effect.map((rows) =>
9911 + rows.map((row) => Object.keys(JSON.parse(row.payload_json)).toSorted()),
9912 + ),
9913 + );
9914 + // A visit stores read state, not a copy of the thread.
9915 + for (const keys of yield* visitPayloadKeys("runtime-layer-visited-thread-visit")) {
9916 + assert.includeMembers(["lastVisitedAt", "opens", "threadId"], keys);
9917 + }
9918
9919 yield* orchestrator.dispatch({
9920 type: "thread.visit",
@@ -9911,6 +9924,8 @@ it.layer(layerSharedApplicationDataPlaneTest)("visited projection", (it) => {
9924 });
9925 const afterStaleVisit = yield* orchestrator.getThreadProjection(threadId);
9926 assert.equal(DateTime.formatIso(afterStaleVisit.thread.lastVisitedAt!), visitedAt);
9927 + // A stale visit with no open changes nothing, so it stores nothing.
9928 + assert.deepEqual(yield* visitPayloadKeys("runtime-layer-visited-thread-visit-stale"), []);
9929
9930 const shell = yield* orchestrator.getShellSnapshot();
9931 const thread = shell.threads.find((candidate) => candidate.id === threadId);
@@ -9927,7 +9942,6 @@ it.layer(layerSharedApplicationDataPlaneTest)("visited projection", (it) => {
9942 .pipe(Effect.flip);
9943 assert.instanceOf(markUnread, Orchestrator.OrchestratorDispatchError);
9944
9930 - const sql = yield* SqlClient.SqlClient;
9945 yield* sql`INSERT INTO orchestration_v2_projection_turn_items (
9946 turn_item_id, thread_id, ordinal, type, status, updated_at, payload_json
9947 ) VALUES (
apps/server/src/relay/AgentAwarenessRelay.ts
+1
@@ -117,6 +117,7 @@ function shouldPublishAgentAwarenessEvent(
117 case "thread.active-reordered":
118 case "thread.tags-updated":
119 case "thread.visited":
120 + case "thread.visit-recorded":
121 case "thread.marked-unread":
122 case "thread.runtime-mode-updated":
123 case "thread.interaction-mode-updated":
packages/client-runtime/src/state/orchestrationV2Projection.ts
+6
@@ -1,4 +1,5 @@
1 import { applyThreadPullRequestLinkUpdate } from "../threadPullRequests.ts";
2 +import { applyOrchestrationV2ThreadVisit } from "@t3tools/contracts";
3 import type {
4 OrchestrationV2DomainEvent,
5 OrchestrationV2Run,
@@ -321,6 +322,11 @@ export function applyOrchestrationV2ProjectionEvent(
322 case "thread.tags-updated":
323 case "thread.marked-unread":
324 return { ...projection, thread: event.payload };
325 + case "thread.visit-recorded":
326 + return {
327 + ...projection,
328 + thread: applyOrchestrationV2ThreadVisit(projection.thread, event.payload),
329 + };
330 case "run.created":
331 case "run.updated": {
332 const next = { ...base, runs: upsertEntity(base.runs, event.payload) };
packages/contracts/src/orchestrationV2.ts
+33
@@ -540,6 +540,24 @@ export const OrchestrationV2AppThread = Schema.Struct({
540 });
541 export type OrchestrationV2AppThread = typeof OrchestrationV2AppThread.Type;
542
543 +/** A visit changes read state only, so its event leaves the rest of the thread out. */
544 +export const OrchestrationV2ThreadVisit = Schema.Struct({
545 + lastVisitedAt: OrchestrationV2AppThread.fields.lastVisitedAt,
546 + opens: OrchestrationV2AppThread.fields.opens,
547 +});
548 +export type OrchestrationV2ThreadVisit = typeof OrchestrationV2ThreadVisit.Type;
549 +
550 +export function applyOrchestrationV2ThreadVisit<Thread extends OrchestrationV2AppThread>(
551 + thread: Thread,
552 + visit: OrchestrationV2ThreadVisit,
553 +): Thread {
554 + return {
555 + ...thread,
556 + lastVisitedAt: visit.lastVisitedAt,
557 + ...(visit.opens === undefined ? {} : { opens: visit.opens }),
558 + };
559 +}
560 +
561 /** A thread another agent drives: a local subagent or a task delegated from a linked environment. */
562 export const isDelegatedAppThread = (
563 thread: Pick<OrchestrationV2AppThread, "lineage" | "delegatedFrom">,
@@ -1973,6 +1991,11 @@ export const OrchestrationV2DomainEvent = Schema.Union([
1991 type: Schema.Literal("thread.pull-request-link-updated"),
1992 payload: ThreadPullRequestLink,
1993 }),
1994 + Schema.Struct({
1995 + ...OrchestrationV2EventBase.fields,
1996 + type: Schema.Literal("thread.visit-recorded"),
1997 + payload: OrchestrationV2ThreadVisit,
1998 + }),
1999 Schema.Struct({
2000 ...OrchestrationV2EventBase.fields,
2001 type: Schema.Literal("run.created"),
@@ -2406,6 +2429,11 @@ export const OrchestrationV2AppThreadJson = OrchestrationV2AppThread.mapFields((
2429 }));
2430 export type OrchestrationV2AppThreadJson = typeof OrchestrationV2AppThreadJson.Type;
2431
2432 +export const OrchestrationV2ThreadVisitJson = Schema.Struct({
2433 + lastVisitedAt: OrchestrationV2AppThreadJson.fields.lastVisitedAt,
2434 + opens: OrchestrationV2AppThreadJson.fields.opens,
2435 +});
2436 +
2437 export const OrchestrationV2RunJson = OrchestrationV2Run.mapFields((fields) => ({
2438 ...fields,
2439 requestedAt: Schema.DateTimeUtcFromString,
@@ -2906,6 +2934,11 @@ export const OrchestrationV2DomainEventJson = Schema.Union([
2934 type: Schema.Literal("thread.pull-request-link-updated"),
2935 payload: ThreadPullRequestLink,
2936 }),
2937 + Schema.Struct({
2938 + ...OrchestrationV2JsonEventBaseFields,
2939 + type: Schema.Literal("thread.visit-recorded"),
2940 + payload: OrchestrationV2ThreadVisitJson,
2941 + }),
2942 Schema.Struct({
2943 ...OrchestrationV2JsonEventBaseFields,
2944 type: Schema.Literal("run.created"),