Compare commits

...
Author SHA1 Message Date
Theo BrowneandClaude Fable 5 f52f1726c4 feat: sync thread read state across devices
Opening a finished thread now stamps a server-synced lastVisitedAt via a
new thread.visit command, so reading a thread on web clears the unread
cue on mobile and vice versa. Local storage stays as the offline and
old-server fallback.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-10 17:24:55 -07:00
24 changed files with 381 additions and 4 deletions

No files matched your search

@@ -56,6 +56,8 @@ import {
useThreadGitRightHeaderItems,
} from "./ThreadGitControls";
import { GitOverviewSheet } from "./git/GitOverviewSheet";
import { appAtomRegistry } from "../../state/atom-registry";
import { environmentServerConfigsAtom } from "../../state/server";
import { useAtomCommand } from "../../state/use-atom-command";
import { useSelectedThreadGitActions } from "../../state/use-selected-thread-git-actions";
import { useSelectedThreadGitState } from "../../state/use-selected-thread-git-state";
@@ -214,6 +216,28 @@ function ThreadRouteContent(
const gitActions = useSelectedThreadGitActions();
const requests = useSelectedThreadRequests();
const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, "thread interrupt");
const visitThreadMutation = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
// Server-synced "seen it" acknowledgement, mirroring web's ChatView: having
// the finished thread open stamps a visit at the turn's completedAt so the
// unread cue clears on every device, not just this one. Guarded on the
// shell's synced value so the echo of our own visit does not re-dispatch;
// the decider is monotonic, so a raced older visit is a server-side no-op.
// Version skew: never send thread.visit to a server that predates it.
useEffect(() => {
if (selectedThread === null) return;
const supportsThreadVisits =
appAtomRegistry.get(environmentServerConfigsAtom).get(selectedThread.environmentId)
?.environment.capabilities.threadVisits === true;
if (!supportsThreadVisits) return;
const completedAt = selectedThread.latestTurn?.completedAt;
if (!completedAt) return;
const syncedVisitedAt = selectedThread.lastVisitedAt;
if (syncedVisitedAt != null && Date.parse(syncedVisitedAt) >= Date.parse(completedAt)) return;
void visitThreadMutation({
environmentId: selectedThread.environmentId,
input: { threadId: selectedThread.id, visitedAt: completedAt },
});
}, [selectedThread, visitThreadMutation]);
const navigation = useNavigation();
const params = props.route.params;
const environmentIdRaw = firstRouteParam(params.environmentId);
@@ -146,6 +146,7 @@ export const make = Effect.gen(function* () {
pullRequests: true,
threadSettlement: true,
threadSnooze: true,
threadVisits: true,
threadPinning: true,
threadPinReorder: true,
threadTitleRegeneration: true,
@@ -619,6 +619,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
settledAt: null,
snoozedUntil: null,
snoozedAt: null,
lastVisitedAt: null,
pinnedAt: null,
pinOrderKey: null,
titleRegenerationRequestId: null,
@@ -727,6 +728,21 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
return;
}
case "thread.visited": {
const existingRow = yield* projectionThreadRepository.getById({
threadId: event.payload.threadId,
});
if (Option.isNone(existingRow)) {
return;
}
yield* projectionThreadRepository.upsert({
...existingRow.value,
lastVisitedAt: event.payload.lastVisitedAt,
updatedAt: event.payload.updatedAt,
});
return;
}
case "thread.pinned": {
const existingRow = yield* projectionThreadRepository.getById({
threadId: event.payload.threadId,
@@ -323,6 +323,7 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
settledAt: null,
snoozedUntil: null,
snoozedAt: null,
lastVisitedAt: null,
pinnedAt: "2026-02-24T00:00:01.000Z",
pinOrderKey: "gm",
titleRegeneration: null,
@@ -442,6 +443,7 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
settledAt: null,
snoozedUntil: null,
snoozedAt: null,
lastVisitedAt: null,
pinnedAt: "2026-02-24T00:00:01.000Z",
pinOrderKey: "gm",
titleRegeneration: null,
@@ -426,6 +426,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
settled_at AS "settledAt",
snoozed_until AS "snoozedUntil",
snoozed_at AS "snoozedAt",
last_visited_at AS "lastVisitedAt",
pinned_at AS "pinnedAt",
pin_order_key AS "pinOrderKey",
title_regeneration_request_id AS "titleRegenerationRequestId",
@@ -462,6 +463,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
settled_at AS "settledAt",
snoozed_until AS "snoozedUntil",
snoozed_at AS "snoozedAt",
last_visited_at AS "lastVisitedAt",
pinned_at AS "pinnedAt",
pin_order_key AS "pinOrderKey",
title_regeneration_request_id AS "titleRegenerationRequestId",
@@ -500,6 +502,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
settled_at AS "settledAt",
snoozed_until AS "snoozedUntil",
snoozed_at AS "snoozedAt",
last_visited_at AS "lastVisitedAt",
pinned_at AS "pinnedAt",
pin_order_key AS "pinOrderKey",
title_regeneration_request_id AS "titleRegenerationRequestId",
@@ -942,6 +945,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
settled_at AS "settledAt",
snoozed_until AS "snoozedUntil",
snoozed_at AS "snoozedAt",
last_visited_at AS "lastVisitedAt",
pinned_at AS "pinnedAt",
pin_order_key AS "pinOrderKey",
title_regeneration_request_id AS "titleRegenerationRequestId",
@@ -1575,6 +1579,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
settledAt: row.settledAt,
snoozedUntil: row.snoozedUntil,
snoozedAt: row.snoozedAt,
lastVisitedAt: row.lastVisitedAt,
pinnedAt: row.pinnedAt,
pinOrderKey: row.pinOrderKey ?? null,
titleRegeneration: mapTitleRegeneration(row),
@@ -1782,6 +1787,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
settledAt: row.settledAt,
snoozedUntil: row.snoozedUntil,
snoozedAt: row.snoozedAt,
lastVisitedAt: row.lastVisitedAt,
pinnedAt: row.pinnedAt,
pinOrderKey: row.pinOrderKey ?? null,
titleRegeneration: mapTitleRegeneration(row),
@@ -1918,6 +1924,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
settledAt: row.settledAt,
snoozedUntil: row.snoozedUntil,
snoozedAt: row.snoozedAt,
lastVisitedAt: row.lastVisitedAt,
pinnedAt: row.pinnedAt,
pinOrderKey: row.pinOrderKey ?? null,
titleRegeneration: mapTitleRegeneration(row),
@@ -2063,6 +2070,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
settledAt: row.settledAt,
snoozedUntil: row.snoozedUntil,
snoozedAt: row.snoozedAt,
lastVisitedAt: row.lastVisitedAt,
pinnedAt: row.pinnedAt,
pinOrderKey: row.pinOrderKey ?? null,
titleRegeneration: mapTitleRegeneration(row),
@@ -2342,6 +2350,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
settledAt: threadRow.value.settledAt,
snoozedUntil: threadRow.value.snoozedUntil,
snoozedAt: threadRow.value.snoozedAt,
lastVisitedAt: threadRow.value.lastVisitedAt,
pinnedAt: threadRow.value.pinnedAt,
pinOrderKey: threadRow.value.pinOrderKey ?? null,
titleRegeneration: mapTitleRegeneration(threadRow.value),
@@ -2463,6 +2472,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
settledAt: threadRow.value.settledAt,
snoozedUntil: threadRow.value.snoozedUntil,
snoozedAt: threadRow.value.snoozedAt,
lastVisitedAt: threadRow.value.lastVisitedAt,
pinnedAt: threadRow.value.pinnedAt,
pinOrderKey: threadRow.value.pinOrderKey ?? null,
titleRegeneration: mapTitleRegeneration(threadRow.value),
+2
View File
@@ -13,6 +13,7 @@ import {
ThreadUnsettledPayload as ContractsThreadUnsettledPayloadSchema,
ThreadSnoozedPayload as ContractsThreadSnoozedPayloadSchema,
ThreadUnsnoozedPayload as ContractsThreadUnsnoozedPayloadSchema,
ThreadVisitedPayload as ContractsThreadVisitedPayloadSchema,
ThreadPinnedPayload as ContractsThreadPinnedPayloadSchema,
ThreadUnpinnedPayload as ContractsThreadUnpinnedPayloadSchema,
ThreadPinReorderedPayload as ContractsThreadPinReorderedPayloadSchema,
@@ -45,6 +46,7 @@ export const ThreadUnarchivedPayload = ContractsThreadUnarchivedPayloadSchema;
export const ThreadUnsettledPayload = ContractsThreadUnsettledPayloadSchema;
export const ThreadSnoozedPayload = ContractsThreadSnoozedPayloadSchema;
export const ThreadUnsnoozedPayload = ContractsThreadUnsnoozedPayloadSchema;
export const ThreadVisitedPayload = ContractsThreadVisitedPayloadSchema;
export const ThreadPinnedPayload = ContractsThreadPinnedPayloadSchema;
export const ThreadUnpinnedPayload = ContractsThreadUnpinnedPayloadSchema;
export const ThreadPinReorderedPayload = ContractsThreadPinReorderedPayloadSchema;
+37
View File
@@ -658,6 +658,43 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
};
}
case "thread.visit": {
// requireThread, not requireThreadNotArchived: archived threads stay
// openable, and acknowledging one must not fail.
const thread = yield* requireThread({
readModel,
command,
threadId: command.threadId,
});
// Monotonic: a visit never moves the acknowledgement backwards, so a
// raced or replayed older visit re-emits the existing value (the
// engine rejects zero-event commands) and projects as a no-op. NaN
// from an unparseable stored value fails the comparison and lets the
// fresh visit through.
const existingVisitedAt = thread.lastVisitedAt ?? null;
const lastVisitedAt =
existingVisitedAt !== null && Date.parse(existingVisitedAt) >= Date.parse(command.visitedAt)
? existingVisitedAt
: command.visitedAt;
const occurredAt = yield* nowIso;
return {
...(yield* withEventBase({
aggregateKind: "thread",
aggregateId: command.threadId,
occurredAt,
commandId: command.commandId,
})),
type: "thread.visited",
payload: {
threadId: command.threadId,
lastVisitedAt,
// A visit is passive acknowledgement: always keep the thread's
// existing updatedAt so reading never churns ordering.
updatedAt: thread.updatedAt,
},
};
}
case "thread.pin": {
const thread = yield* requireThreadNotArchived({
readModel,
@@ -0,0 +1,149 @@
import {
CommandId,
ProjectId,
ProviderInstanceId,
ThreadId,
type OrchestrationReadModel,
} from "@t3tools/contracts";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import { decideOrchestrationCommand } from "./decider.ts";
const NOW = "2026-01-01T00:00:00.000Z";
const EARLIER_VISIT = "2025-12-30T00:00:00.000Z";
const LATER_VISIT = "2025-12-31T00:00:00.000Z";
function makeReadModel(input: {
readonly lastVisitedAt?: string | null;
readonly archivedAt?: string | null;
}): OrchestrationReadModel {
return {
snapshotSequence: 0,
projects: [],
threads: [
{
id: ThreadId.make("thread-1"),
projectId: ProjectId.make("project-1"),
title: "Thread",
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
latestTurn: null,
createdAt: NOW,
updatedAt: NOW,
archivedAt: input.archivedAt ?? null,
settledOverride: null,
settledAt: null,
snoozedUntil: null,
snoozedAt: null,
lastVisitedAt: input.lastVisitedAt ?? null,
deletedAt: null,
messages: [],
proposedPlans: [],
activities: [],
checkpoints: [],
session: null,
},
],
updatedAt: NOW,
};
}
it.layer(NodeServices.layer)("visited thread decider", (it) => {
it.effect("stamps a first visit without churning updatedAt", () =>
Effect.gen(function* () {
const event = yield* decideOrchestrationCommand({
command: {
type: "thread.visit",
commandId: CommandId.make("cmd-visit"),
threadId: ThreadId.make("thread-1"),
visitedAt: LATER_VISIT,
},
readModel: makeReadModel({}),
});
const events = Array.isArray(event) ? event : [event];
expect(events).toHaveLength(1);
expect(events[0]?.type).toBe("thread.visited");
if (events[0]?.type === "thread.visited") {
expect(events[0].payload.lastVisitedAt).toBe(LATER_VISIT);
// A visit is passive acknowledgement: ordering must not move.
expect(events[0].payload.updatedAt).toBe(NOW);
}
}),
);
it.effect("advances a newer visit past an older one", () =>
Effect.gen(function* () {
const event = yield* decideOrchestrationCommand({
command: {
type: "thread.visit",
commandId: CommandId.make("cmd-visit-advance"),
threadId: ThreadId.make("thread-1"),
visitedAt: LATER_VISIT,
},
readModel: makeReadModel({ lastVisitedAt: EARLIER_VISIT }),
});
const events = Array.isArray(event) ? event : [event];
if (events[0]?.type === "thread.visited") {
expect(events[0].payload.lastVisitedAt).toBe(LATER_VISIT);
}
}),
);
it.effect("keeps the existing stamp when a raced older visit arrives", () =>
Effect.gen(function* () {
// Monotonic: a stale visit from a lagging device re-emits the current
// value instead of moving the acknowledgement backwards.
const event = yield* decideOrchestrationCommand({
command: {
type: "thread.visit",
commandId: CommandId.make("cmd-visit-stale"),
threadId: ThreadId.make("thread-1"),
visitedAt: EARLIER_VISIT,
},
readModel: makeReadModel({ lastVisitedAt: LATER_VISIT }),
});
const events = Array.isArray(event) ? event : [event];
if (events[0]?.type === "thread.visited") {
expect(events[0].payload.lastVisitedAt).toBe(LATER_VISIT);
expect(events[0].payload.updatedAt).toBe(NOW);
}
}),
);
it.effect("accepts a visit on an archived thread", () =>
Effect.gen(function* () {
// Archived threads stay openable; acknowledging one must not fail.
const event = yield* decideOrchestrationCommand({
command: {
type: "thread.visit",
commandId: CommandId.make("cmd-visit-archived"),
threadId: ThreadId.make("thread-1"),
visitedAt: LATER_VISIT,
},
readModel: makeReadModel({ archivedAt: NOW }),
});
const events = Array.isArray(event) ? event : [event];
expect(events[0]?.type).toBe("thread.visited");
}),
);
it.effect("rejects a visit for an unknown thread", () =>
Effect.gen(function* () {
const error = yield* decideOrchestrationCommand({
command: {
type: "thread.visit",
commandId: CommandId.make("cmd-visit-unknown"),
threadId: ThreadId.make("thread-unknown"),
visitedAt: LATER_VISIT,
},
readModel: makeReadModel({}),
}).pipe(Effect.flip);
expect(error._tag).toBe("OrchestrationCommandInvariantError");
}),
);
});
@@ -30,6 +30,7 @@ import {
ThreadUnarchivedPayload,
ThreadUnsettledPayload,
ThreadUnsnoozedPayload,
ThreadVisitedPayload,
ThreadRevertedPayload,
ThreadSessionSetPayload,
ThreadTurnDiffCompletedPayload,
@@ -405,6 +406,17 @@ export function projectEvent(
})),
);
case "thread.visited":
return decodeForEvent(ThreadVisitedPayload, event.payload, event.type, "payload").pipe(
Effect.map((payload) => ({
...nextBase,
threads: updateThread(nextBase.threads, payload.threadId, {
lastVisitedAt: payload.lastVisitedAt,
updatedAt: payload.updatedAt,
}),
})),
);
case "thread.pinned":
return decodeForEvent(ThreadPinnedPayload, event.payload, event.type, "payload").pipe(
Effect.map((payload) => ({
@@ -96,6 +96,7 @@ projectionRepositoriesLayer("Projection repositories", (it) => {
settledAt: null,
snoozedUntil: null,
snoozedAt: null,
lastVisitedAt: null,
pinnedAt: null,
latestUserMessageAt: null,
pendingApprovalCount: 0,
@@ -159,6 +160,7 @@ projectionRepositoriesLayer("Projection repositories", (it) => {
settledAt: "2026-03-25T00:00:00.000Z",
snoozedUntil: "2026-03-26T09:00:00.000Z",
snoozedAt: "2026-03-25T00:00:00.000Z",
lastVisitedAt: "2026-03-25T00:00:00.000Z",
pinnedAt: "2026-03-25T00:00:00.000Z",
latestUserMessageAt: null,
pendingApprovalCount: 0,
@@ -178,6 +180,7 @@ projectionRepositoriesLayer("Projection repositories", (it) => {
assert.strictEqual(row.settledAt, "2026-03-25T00:00:00.000Z");
assert.strictEqual(row.snoozedUntil, "2026-03-26T09:00:00.000Z");
assert.strictEqual(row.snoozedAt, "2026-03-25T00:00:00.000Z");
assert.strictEqual(row.lastVisitedAt, "2026-03-25T00:00:00.000Z");
assert.strictEqual(row.pinnedAt, "2026-03-25T00:00:00.000Z");
// Un-settle to the keep-active pin and wake the snooze; confirm the
@@ -188,6 +191,7 @@ projectionRepositoriesLayer("Projection repositories", (it) => {
settledAt: null,
snoozedUntil: null,
snoozedAt: null,
lastVisitedAt: null,
pinnedAt: null,
});
const repersisted = yield* threads.getById({
@@ -47,6 +47,7 @@ const makeProjectionThreadRepository = Effect.gen(function* () {
settled_at,
snoozed_until,
snoozed_at,
last_visited_at,
pinned_at,
pin_order_key,
title_regeneration_request_id,
@@ -74,6 +75,7 @@ const makeProjectionThreadRepository = Effect.gen(function* () {
${row.settledAt},
${row.snoozedUntil},
${row.snoozedAt},
${row.lastVisitedAt},
${row.pinnedAt},
${row.pinOrderKey ?? null},
${row.titleRegenerationRequestId ?? null},
@@ -101,6 +103,7 @@ const makeProjectionThreadRepository = Effect.gen(function* () {
settled_at = excluded.settled_at,
snoozed_until = excluded.snoozed_until,
snoozed_at = excluded.snoozed_at,
last_visited_at = excluded.last_visited_at,
pinned_at = excluded.pinned_at,
pin_order_key = excluded.pin_order_key,
title_regeneration_request_id = excluded.title_regeneration_request_id,
@@ -135,6 +138,7 @@ const makeProjectionThreadRepository = Effect.gen(function* () {
settled_at AS "settledAt",
snoozed_until AS "snoozedUntil",
snoozed_at AS "snoozedAt",
last_visited_at AS "lastVisitedAt",
pinned_at AS "pinnedAt",
pin_order_key AS "pinOrderKey",
title_regeneration_request_id AS "titleRegenerationRequestId",
@@ -171,6 +175,7 @@ const makeProjectionThreadRepository = Effect.gen(function* () {
settled_at AS "settledAt",
snoozed_until AS "snoozedUntil",
snoozed_at AS "snoozedAt",
last_visited_at AS "lastVisitedAt",
pinned_at AS "pinnedAt",
pin_order_key AS "pinOrderKey",
title_regeneration_request_id AS "titleRegenerationRequestId",
@@ -53,6 +53,7 @@ import Migration0037 from "./Migrations/037_ProjectionTurnsKeysetIndex.ts";
import Migration0038 from "./Migrations/038_ProjectionThreadsPinOrderKey.ts";
import Migration0039 from "./Migrations/039_ProjectionProjectsDefaultThreadEnvMode.ts";
import Migration0040 from "./Migrations/040_ProjectionProjectFaviconPath.ts";
import Migration0041 from "./Migrations/041_ProjectionThreadsLastVisitedAt.ts";
/**
* Migration loader with all migrations defined inline.
@@ -105,6 +106,7 @@ export const migrationEntries = [
[38, "ProjectionThreadsPinOrderKey", Migration0038],
[39, "ProjectionProjectsDefaultThreadEnvMode", Migration0039],
[40, "ProjectionProjectFaviconPath", Migration0040],
[41, "ProjectionThreadsLastVisitedAt", Migration0041],
] as const;
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
@@ -0,0 +1,16 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/unstable/sql/SqlClient";
export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
const columns = yield* sql<{ readonly name: string }>`
PRAGMA table_info(projection_threads)
`;
if (!columns.some((column) => column.name === "last_visited_at")) {
yield* sql`
ALTER TABLE projection_threads
ADD COLUMN last_visited_at TEXT
`;
}
});
@@ -41,6 +41,7 @@ export const ProjectionThread = Schema.Struct({
settledAt: Schema.NullOr(IsoDateTime),
snoozedUntil: Schema.NullOr(IsoDateTime),
snoozedAt: Schema.NullOr(IsoDateTime),
lastVisitedAt: Schema.NullOr(IsoDateTime),
pinnedAt: Schema.NullOr(IsoDateTime),
pinOrderKey: Schema.optional(Schema.NullOr(Schema.String)),
titleRegenerationRequestId: Schema.optional(Schema.NullOr(CommandId)),
+18
View File
@@ -4078,6 +4078,24 @@ function ChatViewContent(props: ChatViewProps) {
supportsPullRequests && activeThreadPr !== null && threadRepository !== null;
const supportsSettlement = serverConfig?.environment.capabilities.threadSettlement === true;
const supportsSnooze = serverConfig?.environment.capabilities.threadSnooze === true;
const supportsThreadVisits = serverConfig?.environment.capabilities.threadVisits === true;
// Server-side twin of the markThreadVisited stamp above: reading a finished
// thread here must clear the unread cue on every device, so sync the same
// completedAt acknowledgement to the server. Guarded on the shell's current
// value so re-renders and the echo of our own visit don't re-dispatch; the
// decider is monotonic, so a raced older visit is a server-side no-op.
const visitThreadMutation = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
useEffect(() => {
if (!supportsThreadVisits || activeThreadShell === null) return;
const completedAt = activeThreadShell.latestTurn?.completedAt;
if (!completedAt) return;
const syncedVisitedAt = activeThreadShell.lastVisitedAt;
if (syncedVisitedAt != null && Date.parse(syncedVisitedAt) >= Date.parse(completedAt)) return;
void visitThreadMutation({
environmentId: activeThreadShell.environmentId,
input: { threadId: activeThreadShell.id, visitedAt: completedAt },
});
}, [activeThreadShell, supportsThreadVisits, visitThreadMutation]);
const nowMinute = useNowMinute();
const snoozeNow = new Date().toISOString();
const activeThreadSnoozed =
+4 -1
View File
@@ -372,7 +372,10 @@ export const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThr
} = props;
const threadRef = scopeThreadRef(thread.environmentId, thread.id);
const threadKey = scopedThreadKey(threadRef);
const lastVisitedAt = useUiStateStore((state) => state.threadLastVisitedAtById[threadKey]);
// Local visit stamps win; the shell's server-synced lastVisitedAt fills the
// gap so a visit on another device still clears the unread cue here.
const localLastVisitedAt = useUiStateStore((state) => state.threadLastVisitedAtById[threadKey]);
const lastVisitedAt = localLastVisitedAt ?? thread.lastVisitedAt ?? undefined;
const isSelected = useThreadSelectionStore((state) => state.selectedThreadKeys.has(threadKey));
const runningTerminalIds = useThreadRunningTerminalIds({
environmentId: thread.environmentId,
+2 -1
View File
@@ -154,7 +154,8 @@ type ThreadStatusInput = Pick<
| "session"
| "backgroundLiveness"
> & {
lastVisitedAt?: string | undefined;
// Nullable to accept the shell's server-synced lastVisitedAt directly.
lastVisitedAt?: string | null | undefined;
};
export interface ThreadJumpHintVisibilityController {
+5 -1
View File
@@ -725,7 +725,11 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: {
);
const threadKey = scopedThreadKey(threadRef);
const isRegeneratingTitle = thread.titleRegeneration != null;
const lastVisitedAt = useUiStateStore((state) => state.threadLastVisitedAtById[threadKey]);
// Local visit stamps win (they carry this device's latest intent, incl.
// mark-unread); the shell's server-synced lastVisitedAt fills the gap so a
// visit on another device still clears the unread cue here.
const localLastVisitedAt = useUiStateStore((state) => state.threadLastVisitedAtById[threadKey]);
const lastVisitedAt = localLastVisitedAt ?? thread.lastVisitedAt ?? undefined;
const isSelected = useThreadSelectionStore((state) => state.selectedThreadKeys.has(threadKey));
const openPrLink = useOpenPrLink();
const runningTerminalIds = useThreadRunningTerminalIds({
@@ -258,7 +258,9 @@ export function ThreadRowLeadingStatus({ thread }: { thread: SidebarThreadSummar
const threadStatus = resolveThreadStatusPill({
thread: {
...thread,
lastVisitedAt,
// Local stamp wins; the shell's server-synced visit fills the gap so a
// visit on another device clears the unread cue here too.
lastVisitedAt: lastVisitedAt ?? thread.lastVisitedAt ?? undefined,
},
});
@@ -39,6 +39,7 @@ export type SettleThreadInput = CommandInput<"thread.settle">;
export type UnsettleThreadInput = CommandInput<"thread.unsettle">;
export type SnoozeThreadInput = CommandInput<"thread.snooze">;
export type UnsnoozeThreadInput = CommandInput<"thread.unsnooze">;
export type VisitThreadInput = CommandInput<"thread.visit">;
export type PinThreadInput = CommandInput<"thread.pin">;
export type UnpinThreadInput = CommandInput<"thread.unpin">;
export type ReorderPinnedThreadInput = CommandInput<"thread.pin.reorder">;
@@ -200,6 +201,16 @@ export const unsnoozeThread: (input: UnsnoozeThreadInput) => CommandEffect = Eff
});
});
export const visitThread: (input: VisitThreadInput) => CommandEffect = Effect.fn(
"EnvironmentCommands.visitThread",
)(function* (input) {
return yield* dispatch({
...input,
type: "thread.visit",
commandId: yield* commandId(input),
});
});
export const pinThread: (input: PinThreadInput) => CommandEffect = Effect.fn(
"EnvironmentCommands.pinThread",
)(function* (input) {
@@ -23,6 +23,7 @@ import {
type UnsettleThreadInput,
type UnsnoozeThreadInput,
type UpdateThreadMetadataInput,
type VisitThreadInput,
archiveThread,
createThread,
deleteThread,
@@ -43,6 +44,7 @@ import {
unsettleThread,
unsnoozeThread,
updateThreadMetadata,
visitThread,
} from "../operations/commands.ts";
import type { EnvironmentRegistry } from "../connection/registry.ts";
@@ -67,6 +69,7 @@ export type {
UnsettleThreadInput,
UnsnoozeThreadInput,
UpdateThreadMetadataInput,
VisitThreadInput,
} from "../operations/commands.ts";
export function createThreadEnvironmentAtoms<R, E>(
@@ -127,6 +130,12 @@ export function createThreadEnvironmentAtoms<R, E>(
scheduler,
concurrency,
}),
visit: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:visit",
execute: (input: VisitThreadInput) => visitThread(input),
scheduler,
concurrency,
}),
pin: createEnvironmentCommand(runtime, {
label: "environment-data:commands:thread:pin",
execute: (input: PinThreadInput) => pinThread(input),
@@ -167,6 +167,16 @@ export function applyThreadDetailEvent(
},
};
case "thread.visited":
return {
kind: "updated",
thread: {
...thread,
lastVisitedAt: event.payload.lastVisitedAt,
updatedAt: event.payload.updatedAt,
},
};
case "thread.pinned":
return {
kind: "updated",
+3
View File
@@ -58,6 +58,9 @@ export const ExecutionEnvironmentCapabilities = Schema.Struct({
/** Server understands thread.snooze / thread.unsnooze commands. Same
version-skew contract as threadSettlement. */
threadSnooze: Schema.optionalKey(Schema.Boolean),
/** Server understands the thread.visit command (server-synced last-opened
acknowledgement). Same version-skew contract as threadSettlement. */
threadVisits: Schema.optionalKey(Schema.Boolean),
/** Server understands thread.pin / thread.unpin commands. Same
version-skew contract as threadSettlement. */
threadPinning: Schema.optionalKey(Schema.Boolean),
+35
View File
@@ -386,6 +386,11 @@ export const OrchestrationThread = Schema.Struct({
// Optional so payloads from pre-snooze servers still decode.
snoozedUntil: Schema.optional(Schema.NullOr(IsoDateTime)),
snoozedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
// When the user last opened this thread (stamped with the acknowledged
// turn's completedAt — see ThreadVisitCommand). Server-synced so "seen on
// one device" clears the unread cue on every device. Optional so payloads
// from pre-visit servers still decode.
lastVisitedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
// A pin overrides the settled/snoozed lifecycle: while pinnedAt is set the
// thread renders in the pinned block and never classifies into a shelf.
// Optional so payloads from pre-pinning servers still decode.
@@ -452,6 +457,9 @@ export const OrchestrationThreadShell = Schema.Struct({
settledAt: Schema.NullOr(IsoDateTime).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
snoozedUntil: Schema.optional(Schema.NullOr(IsoDateTime)),
snoozedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
// Same contract as OrchestrationThread.lastVisitedAt: server-synced
// last-opened acknowledgement, optional for pre-visit servers.
lastVisitedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
pinnedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
pinOrderKey: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
titleRegeneration: Schema.optional(Schema.NullOr(ThreadTitleRegeneration)),
@@ -721,6 +729,17 @@ const ThreadUnsnoozeCommand = Schema.Struct({
reason: Schema.Literal("user"),
});
const ThreadVisitCommand = Schema.Struct({
type: Schema.Literal("thread.visit"),
commandId: CommandId,
threadId: ThreadId,
// The completion being acknowledged, not "now": clients stamp the
// latestTurn.completedAt they are looking at, so a completion that lands
// while the thread is open still reads as unseen. Server-side timestamps
// on both ends of the unread comparison also dodge client clock skew.
visitedAt: IsoDateTime,
});
const ThreadPinCommand = Schema.Struct({
type: Schema.Literal("thread.pin"),
commandId: CommandId,
@@ -907,6 +926,7 @@ const DispatchableClientOrchestrationCommand = Schema.Union([
ThreadUnsettleCommand,
ThreadSnoozeCommand,
ThreadUnsnoozeCommand,
ThreadVisitCommand,
ThreadPinCommand,
ThreadUnpinCommand,
ThreadPinReorderCommand,
@@ -935,6 +955,7 @@ export const ClientOrchestrationCommand = Schema.Union([
ThreadUnsettleCommand,
ThreadSnoozeCommand,
ThreadUnsnoozeCommand,
ThreadVisitCommand,
ThreadPinCommand,
ThreadUnpinCommand,
ThreadPinReorderCommand,
@@ -1053,6 +1074,7 @@ export const OrchestrationEventType = Schema.Literals([
"thread.unsettled",
"thread.snoozed",
"thread.unsnoozed",
"thread.visited",
"thread.pinned",
"thread.unpinned",
"thread.pin-reordered",
@@ -1168,6 +1190,14 @@ export const ThreadUnsnoozedPayload = Schema.Struct({
updatedAt: IsoDateTime,
});
export const ThreadVisitedPayload = Schema.Struct({
threadId: ThreadId,
lastVisitedAt: IsoDateTime,
// Deliberately the thread's EXISTING updatedAt: a visit is passive
// acknowledgement, so it must never churn updatedAt-based ordering.
updatedAt: IsoDateTime,
});
export const ThreadPinnedPayload = Schema.Struct({
threadId: ThreadId,
pinnedAt: IsoDateTime,
@@ -1382,6 +1412,11 @@ export const OrchestrationEvent = Schema.Union([
type: Schema.Literal("thread.unsnoozed"),
payload: ThreadUnsnoozedPayload,
}),
Schema.Struct({
...EventBaseFields,
type: Schema.Literal("thread.visited"),
payload: ThreadVisitedPayload,
}),
Schema.Struct({
...EventBaseFields,
type: Schema.Literal("thread.pinned"),