Skip to content

Commit 6710fe5

Browse files
fix(clients): live working timers count on the host's clock
Live "Working" timers subtracted a host timestamp from the client's clock, so a host clock running ahead froze them at 0s and one running behind inflated them. The host now stamps its time on the config snapshot each connection receives; the client keeps a per-environment offset and the live working timers (chat row, sidebar, mobile floating control and thread list) count on that environment's clock. Older hosts send no stamp and keep the previous behavior. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 48cdefe commit 6710fe5

17 files changed

Lines changed: 190 additions & 28 deletions

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,11 @@ function ThreadDetailBody(
829829
return { kind: "compacting" };
830830
}
831831
if (props.activeWorkStartedAt !== null && contentPresentationKind === "ready") {
832-
return { kind: "working", startedAt: props.activeWorkStartedAt };
832+
return {
833+
kind: "working",
834+
environmentId: props.environmentId,
835+
startedAt: props.activeWorkStartedAt,
836+
};
833837
}
834838
if (pendingBackgroundWork !== null && contentPresentationKind === "ready") {
835839
return {

‎apps/mobile/src/features/threads/floating-working-control.tsx‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { toServerClock } from "@t3tools/client-runtime/clock";
12
import type { SubagentPillSegment } from "@t3tools/client-runtime/state/thread-subagents";
3+
import type { EnvironmentId } from "@t3tools/contracts";
24
import { formatDuration } from "@t3tools/shared/orchestrationTiming";
35
import { GlassContainer, GlassView } from "expo-glass-effect";
46
import { type ReactNode, useEffect, useRef, useState } from "react";
@@ -597,7 +599,12 @@ function FloatingStatusLabel(props: {
597599
);
598600
}
599601
return (
600-
<WorkingDuration key="working" startedAt={props.status.startedAt} onLayout={props.onLayout} />
602+
<WorkingDuration
603+
key="working"
604+
environmentId={props.status.environmentId}
605+
startedAt={props.status.startedAt}
606+
onLayout={props.onLayout}
607+
/>
601608
);
602609
}
603610

@@ -638,25 +645,29 @@ function StatusLabelRow(props: {
638645
}
639646

640647
function WorkingDuration(props: {
648+
readonly environmentId: EnvironmentId;
641649
readonly startedAt: string;
642650
readonly onLayout: (event: LayoutChangeEvent) => void;
643651
}) {
644652
return (
645653
<StatusLabelRow onLayout={props.onLayout}>
646-
<WorkingTimer startedAt={props.startedAt} />
654+
<WorkingTimer environmentId={props.environmentId} startedAt={props.startedAt} />
647655
</StatusLabelRow>
648656
);
649657
}
650658

651-
function WorkingTimer(props: { readonly startedAt: string }) {
659+
function WorkingTimer(props: {
660+
readonly environmentId: EnvironmentId;
661+
readonly startedAt: string;
662+
}) {
652663
const nowMs = useVisibleSecondClock(true);
653664
return (
654665
<SystemText
655666
className="shrink text-xs text-foreground"
656667
numberOfLines={1}
657668
style={{ fontVariant: ["tabular-nums"], fontWeight: "500" }}
658669
>
659-
Working {formatWorkingDuration(props.startedAt, nowMs)}
670+
Working {formatWorkingDuration(props.startedAt, toServerClock(props.environmentId, nowMs))}
660671
</SystemText>
661672
);
662673
}

‎apps/mobile/src/features/threads/floating-working-status.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2+
import type { EnvironmentId } from "@t3tools/contracts";
23

34
export interface FloatingStatusAction {
45
readonly label: string;
@@ -8,7 +9,7 @@ export interface FloatingStatusAction {
89
}
910

1011
export type FloatingWorkingStatus =
11-
| { readonly kind: "working"; readonly startedAt: string }
12+
| { readonly kind: "working"; readonly environmentId: EnvironmentId; readonly startedAt: string }
1213
| { readonly kind: "syncing"; readonly label: string }
1314
| { readonly kind: "compacting" }
1415
| {

‎apps/mobile/src/features/threads/thread-list-v2-items.tsx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ import {
2525
formatWorkingDurationLabel,
2626
resolveThreadWorkingStartedAt,
2727
} from "@t3tools/client-runtime/state/models";
28-
import { AuthOrchestrationOperateScope, type EnvironmentMachineKind } from "@t3tools/contracts";
28+
import {
29+
AuthOrchestrationOperateScope,
30+
type EnvironmentId,
31+
type EnvironmentMachineKind,
32+
} from "@t3tools/contracts";
33+
import { toServerClock } from "@t3tools/client-runtime/clock";
2934
import type { SnoozePresetSetting } from "@t3tools/contracts/settings";
3035
import { canSnooze } from "@t3tools/client-runtime/state/thread-settled";
3136
import { threadChipCopyTarget } from "@t3tools/client-runtime/thread-reference";
@@ -153,6 +158,7 @@ const TABULAR_DURATION_STYLE = {
153158
} as const;
154159

155160
function ThreadListV2Duration(props: {
161+
readonly environmentId: EnvironmentId;
156162
readonly startedAt: string | null;
157163
readonly leadingSpace: boolean;
158164
}) {
@@ -163,7 +169,7 @@ function ThreadListV2Duration(props: {
163169
<>
164170
{props.leadingSpace ? " " : null}
165171
<RNText style={TABULAR_DURATION_STYLE}>
166-
{formatWorkingDurationLabel(nowMs - startedAtMs)}
172+
{formatWorkingDurationLabel(toServerClock(props.environmentId, nowMs) - startedAtMs)}
167173
</RNText>
168174
</>
169175
);
@@ -1254,11 +1260,13 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
12541260
{statusLabel.label}
12551261
{status === "working" ? (
12561262
<ThreadListV2Duration
1263+
environmentId={thread.environmentId}
12571264
startedAt={resolveThreadWorkingStartedAt(thread)}
12581265
leadingSpace={statusLabel.label !== null}
12591266
/>
12601267
) : status === "waiting" ? (
12611268
<ThreadListV2Duration
1269+
environmentId={thread.environmentId}
12621270
startedAt={thread.latestRun?.completedAt ?? null}
12631271
leadingSpace={statusLabel.label !== null}
12641272
/>

‎apps/server/src/ws.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,7 @@ export const layerWsRpc = (
18851885
}),
18861886
newProjectsRoot: managedFolders.namedProjectsRoot,
18871887
...(lastEventLoopStall === undefined ? {} : { lastEventLoopStall }),
1888+
serverTime: DateTime.formatIso(yield* DateTime.now),
18881889
};
18891890
});
18901891

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { toServerClock } from "@t3tools/client-runtime/clock";
12
import { type OrchestrationV2ProviderTurn, type RunAttemptId } from "@t3tools/contracts";
23
import {
34
deriveLatestThreadRun,
@@ -979,15 +980,19 @@ export interface LocalDispatchSnapshot {
979980
export function createLocalDispatchSnapshot(
980981
activeThread: Thread | undefined,
981982
options?: {
983+
environmentId?: EnvironmentId;
982984
preparingWorktree?: boolean;
983985
latestUserMessageId?: ChatMessage["id"] | null;
984986
submissionIntent?: ComposerSubmissionIntent;
985987
},
986988
): LocalDispatchSnapshot {
989+
const environmentId = options?.environmentId ?? activeThread?.environmentId;
987990
const latestRun = activeThread?.latestRun ?? null;
988991
const runtime = activeThread?.runtime ?? null;
989992
return {
990-
startedAt: new Date().toISOString(),
993+
startedAt: new Date(
994+
environmentId === undefined ? Date.now() : toServerClock(environmentId, Date.now()),
995+
).toISOString(),
991996
preparingWorktree: Boolean(options?.preparingWorktree),
992997
submissionIntent: options?.submissionIntent ?? "foreground",
993998
latestUserMessageId: options?.latestUserMessageId ?? null,

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,7 @@ export default function ChatView(props: ChatViewProps) {
14331433
isSendBusy,
14341434
backgroundSubmissionPending,
14351435
} = useLocalDispatchState({
1436+
environmentId,
14361437
activeThread,
14371438
activeLatestRun,
14381439
latestUserMessageId:
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1+
import { toServerClock } from "@t3tools/client-runtime/clock";
2+
import type { EnvironmentId } from "@t3tools/contracts";
13
import { formatDuration } from "@t3tools/shared/orchestrationTiming";
24

3-
function formatWorkingTimer(startIso: string, endIso: string): string | null {
5+
export function formatWorkingTimerNow(startIso: string, environmentId: EnvironmentId): string {
46
const startedAtMs = Date.parse(startIso);
5-
const endedAtMs = Date.parse(endIso);
6-
if (!Number.isFinite(startedAtMs) || !Number.isFinite(endedAtMs)) {
7-
return null;
7+
if (!Number.isFinite(startedAtMs)) {
8+
return "0s";
89
}
910

10-
const elapsedSeconds = Math.max(0, Math.floor((endedAtMs - startedAtMs) / 1000));
11+
const elapsedSeconds = Math.max(
12+
0,
13+
Math.floor((toServerClock(environmentId, Date.now()) - startedAtMs) / 1000),
14+
);
1115
if (elapsedSeconds < 60) {
1216
return `${elapsedSeconds}s`;
1317
}
1418

1519
return formatDuration(elapsedSeconds * 1_000);
1620
}
17-
18-
export function formatWorkingTimerNow(startIso: string): string {
19-
return formatWorkingTimer(startIso, new Date().toISOString()) ?? "0s";
20-
}

‎apps/web/src/components/chat/TimelineWorkGroupRows.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ export const WorkGroupSection = memo(function WorkGroupSection({
127127
});
128128

129129
function WorkingTimer({ createdAt }: { createdAt: string }) {
130+
const { activeThreadEnvironmentId } = use(TimelineRowCtx);
130131
return (
131132
<span
132-
ref={tickTextContent(() => formatWorkingTimerNow(createdAt), true)}
133+
ref={tickTextContent(() => formatWorkingTimerNow(createdAt, activeThreadEnvironmentId), true)}
133134
className="tabular-nums"
134135
/>
135136
);

‎apps/web/src/components/chat/useLocalDispatchState.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type MessageId, type RuntimeRequestId } from "@t3tools/contracts";
1+
import { type EnvironmentId, type MessageId, type RuntimeRequestId } from "@t3tools/contracts";
22
import { useState } from "react";
33
import { type ComposerSubmissionIntent } from "../../composer-logic";
44
import { type SessionPhase, type Thread } from "../../types";
@@ -9,6 +9,7 @@ import {
99
} from "../ChatView.logic";
1010

1111
export function useLocalDispatchState(input: {
12+
environmentId: EnvironmentId;
1213
activeThread: Thread | undefined;
1314
activeLatestRun: Thread["latestRun"] | null;
1415
latestUserMessageId: MessageId | null;
@@ -50,6 +51,7 @@ export function useLocalDispatchState(input: {
5051
}
5152
return createLocalDispatchSnapshot(input.activeThread, {
5253
...options,
54+
environmentId: input.environmentId,
5355
latestUserMessageId: input.latestUserMessageId,
5456
});
5557
});

0 commit comments

Comments
 (0)