Skip to content

Commit 1fd2eeb

Browse files
Merge remote-tracking branch 'origin/main' into takeover/21140
2 parents 3d79649 + 6567ffd commit 1fd2eeb

42 files changed

Lines changed: 1272 additions & 296 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 74 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime
22
import { AsyncResult } from "effect/reactivity";
33
import * as Dialog from "../../components/ui/Dialog";
44
import { useAtomValue } from "@effect/atom-react";
5-
import { useChildThreadInputs, useThreadReportedModelSelection } from "../../state/entities";
5+
import {
6+
useChildThreadInputs,
7+
useThreadReportedModelSelection,
8+
useThreadShell,
9+
} from "../../state/entities";
610
import { UsageLimitRecoveryCard } from "./UsageLimitRecoveryCard";
711
import { useNavigation } from "@react-navigation/native";
812
import type { WorktreeSetupCardProps } from "./worktree-setup-card";
@@ -319,6 +323,70 @@ const USER_INPUT_TOGGLE_TIMING = {
319323
easing: Easing.out(Easing.cubic),
320324
};
321325

326+
// The visit watermark follows the shell's updatedAt, which ticks while a run
327+
// streams. Keeping it in a leaf lets the screen read the quiet shell.
328+
function ThreadVisitWatermark(props: {
329+
readonly environmentId: EnvironmentId;
330+
readonly selectedThread: EnvironmentThreadShell;
331+
readonly showContent: boolean;
332+
}) {
333+
const liveThread = useThreadShell({
334+
environmentId: props.environmentId,
335+
threadId: props.selectedThread.id,
336+
});
337+
const thread = liveThread ?? props.selectedThread;
338+
const showContent = props.showContent;
339+
const selectedThreadKey = scopedThreadKey(props.environmentId, thread.id);
340+
const visitThread = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
341+
const lastDispatchedVisitRef = useRef<string | null>(null);
342+
const lastVisitDispatchRef = useRef({ threadKey: selectedThreadKey, at: 0 });
343+
const selectedThreadId = thread.id;
344+
const selectedThreadUpdatedAt = thread.updatedAt;
345+
const selectedThreadLastVisitedAt = thread.lastVisitedAt;
346+
const selectedThreadCompletedAt = thread.latestRun?.completedAt;
347+
useEffect(() => {
348+
if (!showContent || selectedThreadLastVisitedAt === undefined) return;
349+
const threadUpdatedAtMs = Date.parse(selectedThreadUpdatedAt);
350+
if (Number.isNaN(threadUpdatedAtMs)) return;
351+
const lastVisitedAtMs = selectedThreadLastVisitedAt
352+
? Date.parse(selectedThreadLastVisitedAt)
353+
: NaN;
354+
if (!Number.isNaN(lastVisitedAtMs) && lastVisitedAtMs >= threadUpdatedAtMs) return;
355+
const dispatchKey = `${selectedThreadKey}:${selectedThreadUpdatedAt}`;
356+
if (lastDispatchedVisitRef.current === dispatchKey) return;
357+
const dispatch = () => {
358+
lastDispatchedVisitRef.current = dispatchKey;
359+
lastVisitDispatchRef.current = { threadKey: selectedThreadKey, at: Date.now() };
360+
void visitThread({
361+
environmentId: props.environmentId,
362+
input: { threadId: selectedThreadId, visitedAt: selectedThreadUpdatedAt },
363+
});
364+
};
365+
const completedAtMs = selectedThreadCompletedAt ? Date.parse(selectedThreadCompletedAt) : NaN;
366+
const hasUnseenCompletion =
367+
!Number.isNaN(completedAtMs) &&
368+
(Number.isNaN(lastVisitedAtMs) || completedAtMs > lastVisitedAtMs);
369+
const previous = lastVisitDispatchRef.current;
370+
const elapsed = Date.now() - previous.at;
371+
if (previous.threadKey !== selectedThreadKey || hasUnseenCompletion || elapsed >= 10_000) {
372+
dispatch();
373+
return;
374+
}
375+
const timer = setTimeout(dispatch, 10_000 - elapsed);
376+
return () => clearTimeout(timer);
377+
}, [
378+
props.environmentId,
379+
selectedThreadId,
380+
selectedThreadKey,
381+
selectedThreadLastVisitedAt,
382+
selectedThreadCompletedAt,
383+
selectedThreadUpdatedAt,
384+
showContent,
385+
visitThread,
386+
]);
387+
return null;
388+
}
389+
322390
export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetailScreenProps) {
323391
const usesNativeWorkspaceColumns = useNativeWorkspaceColumnsSupported();
324392
const navigation = useNavigation();
@@ -785,54 +853,6 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
785853
setComposerFocused(false);
786854
}, [selectedThreadKey, showContent]);
787855

788-
const visitThread = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
789-
const lastDispatchedVisitRef = useRef<string | null>(null);
790-
const lastVisitDispatchRef = useRef({ threadKey: selectedThreadKey, at: 0 });
791-
const selectedThreadId = props.selectedThread.id;
792-
const selectedThreadUpdatedAt = props.selectedThread.updatedAt;
793-
const selectedThreadLastVisitedAt = props.selectedThread.lastVisitedAt;
794-
const selectedThreadCompletedAt = props.selectedThread.latestRun?.completedAt;
795-
useEffect(() => {
796-
if (!showContent || selectedThreadLastVisitedAt === undefined) return;
797-
const threadUpdatedAtMs = Date.parse(selectedThreadUpdatedAt);
798-
if (Number.isNaN(threadUpdatedAtMs)) return;
799-
const lastVisitedAtMs = selectedThreadLastVisitedAt
800-
? Date.parse(selectedThreadLastVisitedAt)
801-
: NaN;
802-
if (!Number.isNaN(lastVisitedAtMs) && lastVisitedAtMs >= threadUpdatedAtMs) return;
803-
const dispatchKey = `${selectedThreadKey}:${selectedThreadUpdatedAt}`;
804-
if (lastDispatchedVisitRef.current === dispatchKey) return;
805-
const dispatch = () => {
806-
lastDispatchedVisitRef.current = dispatchKey;
807-
lastVisitDispatchRef.current = { threadKey: selectedThreadKey, at: Date.now() };
808-
void visitThread({
809-
environmentId: props.environmentId,
810-
input: { threadId: selectedThreadId, visitedAt: selectedThreadUpdatedAt },
811-
});
812-
};
813-
const completedAtMs = selectedThreadCompletedAt ? Date.parse(selectedThreadCompletedAt) : NaN;
814-
const hasUnseenCompletion =
815-
!Number.isNaN(completedAtMs) &&
816-
(Number.isNaN(lastVisitedAtMs) || completedAtMs > lastVisitedAtMs);
817-
const previous = lastVisitDispatchRef.current;
818-
const elapsed = Date.now() - previous.at;
819-
if (previous.threadKey !== selectedThreadKey || hasUnseenCompletion || elapsed >= 10_000) {
820-
dispatch();
821-
return;
822-
}
823-
const timer = setTimeout(dispatch, 10_000 - elapsed);
824-
return () => clearTimeout(timer);
825-
}, [
826-
props.environmentId,
827-
selectedThreadId,
828-
selectedThreadKey,
829-
selectedThreadLastVisitedAt,
830-
selectedThreadCompletedAt,
831-
selectedThreadUpdatedAt,
832-
showContent,
833-
visitThread,
834-
]);
835-
836856
useEffect(() => {
837857
setAnchorMessageId(null);
838858
setSubmittedMessageId(null);
@@ -1003,6 +1023,11 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
10031023
return (
10041024
<View className="flex-1">
10051025
{!usesNativeWorkspaceColumns ? <NativeLayoutObserver onChange={setScreenMetrics} /> : null}
1026+
<ThreadVisitWatermark
1027+
environmentId={props.environmentId}
1028+
selectedThread={props.selectedThread}
1029+
showContent={showContent}
1030+
/>
10061031
{showContent ? (
10071032
<View
10081033
style={{ flex: 1 }}

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { AnimatedLegendList } from "@legendapp/list/reanimated";
2222
import { StackActions, useIsFocused, useNavigation } from "@react-navigation/native";
2323
import {
2424
useEffect,
25+
useEffectEvent,
2526
useId,
2627
useLayoutEffect,
2728
useRef,
@@ -624,7 +625,9 @@ function ThreadWorkGroupList(props: {
624625
void listRef.current?.scrollToEnd({ animated: false });
625626
}
626627
};
627-
const onContentSizeChange = (nextHeight: number) => {
628+
// LegendList notifies from child layout effects. Effect Events see the
629+
// committed rows before those effects run, unlike a parent layout-effect ref.
630+
const onContentSizeChange = useEffectEvent((nextHeight: number) => {
628631
const previous = previousContent.current;
629632
const detailsChanged = previous.expandedRows !== props.expandedRows;
630633
const followAppend =
@@ -658,25 +661,21 @@ function ThreadWorkGroupList(props: {
658661
}
659662
finishPendingAppend();
660663
rememberPosition();
661-
};
664+
});
662665
// The native ScrollView reports its content size a frame or more after
663666
// LegendList has laid the rows out, so a detail toggle rendered the group
664667
// at its old height while the rows below already moved. Read the size
665668
// LegendList computes on the JS thread instead; it settles in the same
666669
// commit as the row measurement that changed it.
667-
const onContentSizeChangeRef = useRef(onContentSizeChange);
668670
useLayoutEffect(() => {
669-
onContentSizeChangeRef.current = onContentSizeChange;
670-
}, [onContentSizeChange]);
671-
const subscribeToContentSize = (list: LegendListRef | null) => {
672-
listRef.current = list;
671+
const list = listRef.current;
673672
if (!list) return;
674673
const unsubscribe = list.getState().listen("totalSize", () => {
675-
onContentSizeChangeRef.current(list.getState().contentLength);
674+
onContentSizeChange(list.getState().contentLength);
676675
});
677-
onContentSizeChangeRef.current(list.getState().contentLength);
676+
onContentSizeChange(list.getState().contentLength);
678677
return unsubscribe;
679-
};
678+
}, []);
680679
const getFixedItemSize = (row: ThreadFeedActivity, index: number) =>
681680
props.expandedRows[row.id] || props.rowSizing.fixedRowHeight === undefined
682681
? undefined
@@ -690,7 +689,7 @@ function ThreadWorkGroupList(props: {
690689
return (
691690
<View style={{ height, overflow: "hidden" }}>
692691
<AnimatedLegendList
693-
ref={subscribeToContentSize}
692+
ref={listRef}
694693
data={props.activities}
695694
keyExtractor={workLogRowKey}
696695
estimatedItemSize={props.rowSizing.estimatedRowHeight + WORK_ROW_GAP}

‎apps/mobile/src/features/usage/UsageLimitsPooled.tsx‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
cursorUsageWindowDetails,
1010
displayLimitWindows,
1111
formatResetsIn,
12+
paceRemainingPercent,
1213
remainingPercent,
1314
type LimitAccount,
1415
type LimitPoolWindow,
@@ -146,22 +147,38 @@ function PoolWindowCard({
146147
if (!member.window)
147148
return <View key={member.account.key} className="h-7 min-w-0 flex-1" />;
148149
const { account, window } = member;
150+
const timeLeft = paceRemainingPercent(window, now);
149151
return (
150152
<Pressable
151153
key={account.key}
152154
accessibilityRole="button"
153-
accessibilityLabel={`Segment ${index + 1}, ${accountName(account)}, ${remainingPercent(window)}% left`}
155+
accessibilityLabel={`Segment ${index + 1}, ${accountName(account)}, ${remainingPercent(window)}% left${timeLeft === null ? "" : `, ${timeLeft}% of the window left`}`}
154156
accessibilityHint="Show account details"
155157
onPress={() => openAccount(account)}
156-
className="h-7 min-w-0 flex-1 overflow-hidden rounded-md bg-subtle"
158+
className="h-7 min-w-0 flex-1 rounded-md bg-subtle"
157159
>
158-
<AccountSegment
159-
remaining={remainingPercent(window)}
160-
color={color}
161-
refillPercent={window.resetsAt ? member.refillPercent : 0}
162-
/>
163-
<View pointerEvents="none" className="absolute inset-0 items-center justify-center">
164-
<Text className="text-xs font-t3-medium tabular-nums text-foreground">
160+
<View className="absolute inset-0 overflow-hidden rounded-md">
161+
<AccountSegment
162+
remaining={remainingPercent(window)}
163+
color={color}
164+
refillPercent={window.resetsAt ? member.refillPercent : 0}
165+
/>
166+
</View>
167+
{timeLeft !== null ? (
168+
<View
169+
pointerEvents="none"
170+
className="absolute -top-1 -bottom-1 w-0.5 -translate-x-px rounded-full bg-foreground-muted"
171+
style={{ left: `${timeLeft}%` }}
172+
/>
173+
) : null}
174+
<View
175+
pointerEvents="none"
176+
className="absolute inset-0 items-center justify-center overflow-hidden rounded-md"
177+
>
178+
<Text
179+
numberOfLines={1}
180+
className="max-w-full shrink text-xs font-t3-medium tabular-nums text-foreground"
181+
>
165182
{index + 1}
166183
</Text>
167184
</View>
@@ -370,6 +387,7 @@ export function UsageLimitAccountScreen({ route }: AccountScreenProps) {
370387
.find((candidate) => candidate.driver === account?.driver)
371388
?.windows.find((candidate) => candidate.id === windowId && candidate.kind === windowKind);
372389
const window = pool?.members.find((member) => member.account.key === accountKey)?.window;
390+
const evenPace = window ? paceRemainingPercent(window, now) : null;
373391
const reset = pool?.resets.find((candidate) => candidate.member.account.key === accountKey);
374392
const [revealed, setRevealed] = useState(false);
375393
return (
@@ -424,6 +442,11 @@ export function UsageLimitAccountScreen({ route }: AccountScreenProps) {
424442
})}
425443
</Text>
426444
) : null}
445+
{evenPace !== null ? (
446+
<Text className="text-sm text-foreground-muted">
447+
Even pace: {evenPace}% left by now
448+
</Text>
449+
) : null}
427450
{reset && reset.restoresPercent > 0 ? (
428451
<Text className="text-sm text-foreground-muted">
429452
Restores {reset.restoresPercent}% of the pool

‎apps/mobile/src/features/usage/UsageLimitsSection.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import {
1010
type ServerProviderUsageWindow,
1111
} from "@t3tools/contracts";
1212
import {
13-
elapsedShare,
1413
formatDuration,
1514
formatResetsIn,
1615
limitsNotice,
1716
paceOf,
17+
paceRemainingPercent,
1818
remainingPercent,
1919
} from "@t3tools/shared/usageLimits";
2020
import { type ReactNode, useEffect, useEffectEvent, useRef, useState } from "react";
@@ -46,8 +46,7 @@ function WindowRow(props: {
4646
}) {
4747
const { window, now } = props;
4848
const remaining = remainingPercent(window);
49-
const elapsed = elapsedShare(window, now);
50-
const timeLeft = elapsed === null ? null : Math.round((1 - elapsed) * 100);
49+
const timeLeft = paceRemainingPercent(window, now);
5150
const pace = paceOf(window, now);
5251
const resetsIn = formatResetsIn(window, now);
5352
return (

‎apps/mobile/src/state/entities.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadSh
9999
);
100100
}
101101

102+
/** Streaming timestamps, item counts, and the latest message can be stale during a run. */
103+
export function useQuietThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
104+
return useAtomValue(
105+
ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.quietThreadShellAtom(ref),
106+
);
107+
}
108+
102109
export function useChildThreadInputs(ref: ScopedThreadRef) {
103110
return useAtomValue(environmentThreadShells.childThreadInputsAtom(ref));
104111
}

‎apps/mobile/src/state/use-thread-selection.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
import * as Option from "effect/Option";
2121

2222
import { scopedThreadKey } from "../lib/scopedEntities";
23-
import { useProject, useThreadShell } from "../state/entities";
23+
import { useProject, useQuietThreadShell } from "../state/entities";
2424
import { useEnvironmentThread } from "../state/threads";
2525
import {
2626
resolvePendingThreadCreation,
@@ -139,7 +139,7 @@ function useResolvedThreadSelection(params: ThreadSelectionRouteParams | undefin
139139
threadId: ThreadId.make(routeThreadId),
140140
}
141141
: lastRouteThreadRef;
142-
const selectedThreadShell = useThreadShell(selectedThreadRef);
142+
const selectedThreadShell = useQuietThreadShell(selectedThreadRef);
143143
const selectedThreadKey =
144144
selectedThreadRef === null
145145
? null

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type OrchestrationV2DomainEvent,
66
ProviderThreadId,
77
ThreadId,
8+
VcsProcessExitError,
89
} from "@t3tools/contracts";
910
import * as Context from "effect/Context";
1011
import * as DateTime from "effect/DateTime";
@@ -24,10 +25,11 @@ import {
2425
isCheckpointRestoreIsolated,
2526
SHARED_WORKSPACE_RESTORE_MESSAGE,
2627
} from "./CheckpointRestoreSafety.ts";
27-
import { CheckpointServiceV2 } from "./CheckpointService.ts";
28+
import { CheckpointRestoreError, CheckpointServiceV2 } from "./CheckpointService.ts";
2829
import { EventSinkV2 } from "./EventSink.ts";
2930
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
3031
import { ProjectionStoreV2 } from "./ProjectionStore.ts";
32+
import { GIT_INDEX_LOCKED_DETAIL } from "../vcs/VcsProcess.ts";
3133
import * as ProjectStore from "./ProjectStore.ts";
3234
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
3335
import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts";
@@ -70,6 +72,20 @@ export class CheckpointRollbackExecutionError extends Schema.TaggedError<Checkpo
7072
}
7173

7274
const isCheckpointRollbackExecutionError = Schema.is(CheckpointRollbackExecutionError);
75+
const isVcsProcessExitError = Schema.is(VcsProcessExitError);
76+
const isCheckpointRestoreError = Schema.is(CheckpointRestoreError);
77+
78+
/**
79+
* The message clients see when a rollback fails. Only a held Git index lock
80+
* gets its own explanation; other reasons carry internal ids or raw Git
81+
* output, so they fall back to the generic message.
82+
*/
83+
export function rollbackFailureClientMessage(error: CheckpointRollbackExecutionError): string {
84+
const restoreCause = isCheckpointRestoreError(error.cause) ? error.cause.cause : undefined;
85+
return isVcsProcessExitError(restoreCause) && restoreCause.detail === GIT_INDEX_LOCKED_DETAIL
86+
? GIT_INDEX_LOCKED_DETAIL
87+
: ROLLBACK_FAILED_MESSAGE;
88+
}
7389

7490
export interface CheckpointRollbackServiceV2Shape {
7591
readonly execute: (input: {

0 commit comments

Comments
 (0)