Skip to content

Commit 384c16d

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-22198
2 parents 8f657bf + e2d3ded commit 384c16d

27 files changed

Lines changed: 545 additions & 190 deletions

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { buildProjectThreadStartTurnInput } from "../../lib/projectThreadStartTu
55
import { useWorktreeSetup } from "./use-worktree-setup";
66
import { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setup";
77
import { AppBar } from "../../components/ui/AppBar";
8-
import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
8+
import {
9+
StackActions,
10+
useNavigation,
11+
useRoute,
12+
type StaticScreenProps,
13+
} from "@react-navigation/native";
914
import { useEffect, useEffectEvent, useRef, useState, type ReactNode } from "react";
1015
import * as Option from "effect/Option";
1116
import {
@@ -74,6 +79,9 @@ import { useThreadComposerState } from "../../state/use-thread-composer-state";
7479
import { resolveMergeBackTargetThreadId } from "@t3tools/client-runtime/state/thread-relationships";
7580
import { resolveLatestMergeBackRun } from "@t3tools/client-runtime/state/thread-workflows";
7681
import { threadEnvironment, useThreadFamily } from "../../state/threads";
82+
import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads";
83+
import { useProjects } from "../../state/entities";
84+
import { newTaskDestination } from "./new-task-project-selection";
7785
import { projectThreadContentPresentation } from "./threadContentPresentation";
7886
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
7987
import {
@@ -191,6 +199,27 @@ function ThreadUnavailableScreen(props: {
191199
);
192200
}
193201

202+
/** A thread that can't load opens a new task in its environment instead of a dead end. */
203+
function UnavailableThreadDraftRedirect(props: { readonly environmentId: EnvironmentId }) {
204+
const navigation = useNavigation();
205+
const projects = useProjects();
206+
const { layout } = useAdaptiveWorkspaceLayout();
207+
const openDraft = useEffectEvent(() => {
208+
const project = projects.find((candidate) => candidate.environmentId === props.environmentId);
209+
navigation.dispatch(
210+
StackActions.replace(
211+
"Thread",
212+
newTaskDestination(layout.usesSplitView, projects, project, uuidv4()),
213+
),
214+
);
215+
});
216+
// oxlint-disable-next-line t3code/react-effect-budget -- replaces this route in the navigator
217+
useEffect(() => {
218+
openDraft();
219+
}, []);
220+
return <OpeningThreadLoadingScreen />;
221+
}
222+
194223
export function ThreadRouteScreen(props: ThreadRouteScreenProps) {
195224
return (
196225
<ThreadScreenHost>
@@ -225,6 +254,7 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) {
225254
const routeEnvironmentShellState = useEnvironmentShellReadiness(environmentId);
226255
const { onReconnectEnvironment } = useRemoteConnections();
227256
const navigation = useNavigation();
257+
const routeName = useRoute().name;
228258
const routeConnectionState =
229259
routeEnvironmentRuntime?.connectionState ?? (environmentId ? "available" : connectionState);
230260
const routeThreadKey =
@@ -261,6 +291,10 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) {
261291

262292
const stillHydrating = threadRouteIsHydrating({
263293
isLoadingConnections: !connectionsReady,
294+
environmentRegistered: routeEnvironmentRuntime !== null,
295+
localCacheRead:
296+
routeEnvironmentShellState.cacheRead &&
297+
selectedThreadDetailState !== EMPTY_ENVIRONMENT_THREAD_STATE,
264298
connectionState: routeConnectionState,
265299
shellStatus: routeEnvironmentShellState.status,
266300
shellHasError: routeEnvironmentShellState.hasError,
@@ -272,6 +306,10 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) {
272306
return withInspectorHeader(<OpeningThreadLoadingScreen />);
273307
}
274308

309+
if (routeName === "Thread" && !props.inspectorPresentation) {
310+
return <UnavailableThreadDraftRedirect environmentId={environmentId} />;
311+
}
312+
275313
return withInspectorHeader(
276314
<ThreadUnavailableScreen
277315
actionLabel={

‎apps/mobile/src/features/threads/thread-route-hydration.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { threadRouteIsHydrating } from "./thread-route-hydration";
44

55
const settled = {
66
isLoadingConnections: false,
7+
environmentRegistered: true,
8+
localCacheRead: true,
79
connectionState: "connected" as const,
810
shellStatus: "live" as const,
911
shellHasError: false,
@@ -19,6 +21,21 @@ describe("threadRouteIsHydrating", () => {
1921
expect(threadRouteIsHydrating({ ...settled, detailStatus: "empty" })).toBe(true);
2022
});
2123

24+
it("waits for the local cache before trusting an offline connection", () => {
25+
const offline = {
26+
...settled,
27+
connectionState: "offline" as const,
28+
detailStatus: "empty" as const,
29+
};
30+
expect(threadRouteIsHydrating({ ...offline, localCacheRead: false })).toBe(true);
31+
expect(threadRouteIsHydrating(offline)).toBe(false);
32+
expect(threadRouteIsHydrating({ ...offline, connectionState: "reconnecting" })).toBe(false);
33+
expect(threadRouteIsHydrating({ ...offline, connectionState: "connecting" })).toBe(true);
34+
expect(
35+
threadRouteIsHydrating({ ...offline, localCacheRead: false, environmentRegistered: false }),
36+
).toBe(false);
37+
});
38+
2239
it("stops waiting once an empty detail has an actionable outcome", () => {
2340
expect(
2441
threadRouteIsHydrating({

‎apps/mobile/src/features/threads/thread-route-hydration.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type { EnvironmentThreadStatus } from "@t3tools/client-runtime/state/thre
44

55
export function threadRouteIsHydrating(input: {
66
readonly isLoadingConnections: boolean;
7+
readonly environmentRegistered: boolean;
8+
readonly localCacheRead: boolean;
79
readonly connectionState: EnvironmentConnectionPhase;
810
readonly shellStatus: EnvironmentShellStatus;
911
readonly shellHasError: boolean;
@@ -16,16 +18,23 @@ export function threadRouteIsHydrating(input: {
1618
if (input.isLoadingConnections) {
1719
return true;
1820
}
21+
if (!input.environmentRegistered) {
22+
return false;
23+
}
24+
if (!input.localCacheRead) {
25+
return true;
26+
}
27+
// Only a first attempt is worth waiting out; a reconnecting environment has already failed.
1928
if (
2029
input.connectionState === "available" ||
2130
input.connectionState === "offline" ||
31+
input.connectionState === "reconnecting" ||
2232
input.connectionState === "error"
2333
) {
2434
return false;
2535
}
2636
return (
2737
input.connectionState === "connecting" ||
28-
input.connectionState === "reconnecting" ||
2938
input.shellStatus === "synchronizing" ||
3039
(input.connectionState === "connected" && input.shellStatus === "empty") ||
3140
input.detailStatus === "synchronizing" ||

‎apps/mobile/src/features/workspace/workspace-selection-store.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import { workspaceRoutesOf } from "../layout/workspace-overlay-routes";
1919
import type { WorkspaceSelections } from "../../persistence/mobile-preferences";
2020
import { appAtomRegistry } from "../../state/atom-registry";
2121
import { environmentPresentations } from "../../state/presentation";
22-
import { environmentThreadShells, threadEnvironment } from "../../state/threads";
22+
import {
23+
environmentThreadShells,
24+
startOpeningEnvironmentThread,
25+
threadEnvironment,
26+
} from "../../state/threads";
2327
import {
2428
rememberedSettingsPage,
2529
workspaceSelectionsIn,
@@ -137,11 +141,17 @@ export function settingsLinkPage() {
137141
*/
138142
export async function launchWorkspaceURL(): Promise<string | null> {
139143
if (!REMEMBERS) return null;
140-
await Promise.all([
141-
hydrateWorkspaceSelections(),
142-
firstNativeLayoutMetrics(NATIVE_METRICS_TIMEOUT_MS),
143-
]);
144+
const metrics = firstNativeLayoutMetrics(NATIVE_METRICS_TIMEOUT_MS);
145+
await hydrateWorkspaceSelections();
144146
const thread = selections.threads;
147+
// Read the chat's cached copy while the layout metrics are still on their way.
148+
if (thread) {
149+
startOpeningEnvironmentThread(
150+
thread.environmentId as EnvironmentId,
151+
thread.threadId as ThreadId,
152+
);
153+
}
154+
await metrics;
145155
if (!thread || !workspaceHasColumns() || !workspaceSelectionCheck.thread(thread)) return null;
146156
return Linking.createURL(
147157
`threads/${encodeURIComponent(thread.environmentId)}/${encodeURIComponent(thread.threadId)}`,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const EMPTY_ENVIRONMENT_SHELL_STATE_ATOM = Atom.make<EnvironmentShellState>(EMPT
2626

2727
const shellStatus = (state: EnvironmentShellState) => state.status;
2828
const shellHasError = (state: EnvironmentShellState) => Option.isSome(state.error);
29+
// The empty placeholder stands until the environment has read its cached shell.
30+
const shellCacheRead = (state: EnvironmentShellState) => state !== EMPTY_SHELL_STATE;
2931

3032
export function useEnvironmentShellReadiness(environmentId: EnvironmentId | null) {
3133
const atom =
@@ -35,5 +37,6 @@ export function useEnvironmentShellReadiness(environmentId: EnvironmentId | null
3537
return {
3638
status: useAtomValue(atom, shellStatus),
3739
hasError: useAtomValue(atom, shellHasError),
40+
cacheRead: useAtomValue(atom, shellCacheRead),
3841
};
3942
}

‎apps/server/src/provider/ClaudeModelCatalog.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,21 @@ function tryResolveClaudeModelCatalog(
5959
};
6060
}
6161

62-
const BUNDLED_CLAUDE_PROVIDER_CATALOG =
63-
resolveProviderCatalog(BUNDLED_MODEL_MANIFEST, CLAUDE) ?? undefined;
64-
62+
/** Resolve the fallback on each read so its new-model badges can expire. */
6563
export function resolveClaudeModelCatalog(
6664
catalog: ModelCatalog.ProviderCatalog | undefined,
6765
): ClaudeModelCatalog {
6866
return (
6967
tryResolveClaudeModelCatalog(catalog) ??
70-
tryResolveClaudeModelCatalog(BUNDLED_CLAUDE_PROVIDER_CATALOG) ?? {
68+
tryResolveClaudeModelCatalog(
69+
resolveProviderCatalog(BUNDLED_MODEL_MANIFEST, CLAUDE) ?? undefined,
70+
) ?? {
7171
models: [],
7272
}
7373
);
7474
}
7575

76-
export const BUNDLED_CLAUDE_MODEL_CATALOG = resolveClaudeModelCatalog(
77-
BUNDLED_CLAUDE_PROVIDER_CATALOG,
78-
);
76+
export const BUNDLED_CLAUDE_MODEL_CATALOG = resolveClaudeModelCatalog(undefined);
7977

8078
export function scopeClaudeModelCatalog(
8179
catalog: ClaudeModelCatalog,

‎apps/server/src/provider/ModelManifest.test.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { assert, describe, it } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { ProviderDriverKind } from "@t3tools/contracts";
4+
import * as ModelCatalog from "@t3tools/provider-core/server/ModelCatalog";
45
import * as Effect from "effect/Effect";
56
import * as FileSystem from "effect/FileSystem";
67
import * as Layer from "effect/Layer";
@@ -13,6 +14,87 @@ import * as ServerSettings from "../serverSettings.ts";
1314
import * as ModelManifest from "./ModelManifest.ts";
1415

1516
describe("resolveProviderCatalog", () => {
17+
it.each([
18+
{ releasedAt: "2026-01-01", now: "2026-01-01T00:00:00Z", badge: "new" },
19+
{ releasedAt: "2026-01-01", now: "2026-01-07T23:59:59.999Z", badge: "new" },
20+
{ releasedAt: "2026-01-01", now: "2026-01-08T00:00:00Z", badge: undefined },
21+
{ releasedAt: "2026-01-01", now: "2026-02-01T00:00:00Z", badge: undefined },
22+
{ releasedAt: "2026-01-02T00:00:00Z", now: "2026-01-01T00:00:00Z", badge: undefined },
23+
{ releasedAt: undefined, now: "2026-01-01T00:00:00Z", badge: undefined },
24+
{ releasedAt: "invalid", now: "2026-01-01T00:00:00Z", badge: undefined },
25+
])("derives the new badge from releasedAt=$releasedAt at $now", ({ releasedAt, now, badge }) => {
26+
const manifest: ModelManifest.ModelManifestData = {
27+
version: 1,
28+
currentModels: {},
29+
providers: {
30+
synthetic: {
31+
profiles: {},
32+
models: [
33+
{
34+
slug: "model-next",
35+
name: "Model Next",
36+
status: "current",
37+
...(releasedAt === undefined ? {} : { releasedAt }),
38+
},
39+
{
40+
slug: "model-legacy",
41+
name: "Model Legacy",
42+
status: "legacy",
43+
releasedAt: now,
44+
},
45+
],
46+
},
47+
},
48+
};
49+
const catalog = ModelManifest.resolveProviderCatalog(
50+
manifest,
51+
ProviderDriverKind.make("synthetic"),
52+
Date.parse(now),
53+
);
54+
assert.strictEqual(catalog?.models[0]?.badge, badge);
55+
assert.strictEqual(catalog?.models[1]?.badge, undefined);
56+
});
57+
58+
it.effect("expires badges when rereading a cached manifest without a refresh", () => {
59+
const manifest: ModelManifest.ModelManifestData = {
60+
version: 1,
61+
currentModels: {},
62+
providers: {
63+
synthetic: {
64+
profiles: {},
65+
models: [
66+
{
67+
slug: "model-next",
68+
name: "Model Next",
69+
status: "current",
70+
releasedAt: "1970-01-01T00:00:00Z",
71+
},
72+
],
73+
},
74+
},
75+
};
76+
return Effect.gen(function* () {
77+
const service = yield* ModelCatalog.ModelCatalog;
78+
const readCatalog = service.current(ProviderDriverKind.make("synthetic"));
79+
assert.strictEqual((yield* readCatalog)?.models[0]?.badge, "new");
80+
yield* TestClock.adjust("7 days");
81+
assert.strictEqual((yield* readCatalog)?.models[0]?.badge, undefined);
82+
}).pipe(
83+
Effect.provide(
84+
ModelManifest.layerModelCatalog.pipe(
85+
Layer.provide(
86+
Layer.succeed(ModelManifest.ModelManifest, {
87+
current: Effect.succeed(manifest),
88+
refresh: Effect.die("Catalog reads must not refresh the manifest"),
89+
forceRefresh: Effect.die("Catalog reads must not refresh the manifest"),
90+
refreshInBackground: Effect.die("Catalog reads must not refresh the manifest"),
91+
}),
92+
),
93+
),
94+
),
95+
);
96+
});
97+
1698
it("rejects invalid catalog references", () => {
1799
const invalidCatalog = (input: {
18100
readonly models: NonNullable<ModelManifest.ModelManifestData["providers"]>[string]["models"];

‎apps/server/src/provider/ModelManifest.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from "@t3tools/contracts";
66
import * as Clock from "effect/Clock";
77
import * as Context from "effect/Context";
8+
import * as DateTime from "effect/DateTime";
89
import * as Effect from "effect/Effect";
910
import * as FileSystem from "effect/FileSystem";
1011
import * as Layer from "effect/Layer";
@@ -30,6 +31,7 @@ const MANIFEST_TTL_MS = 60 * 60 * 1000;
3031
const MANIFEST_RETRY_MS = 5 * 60 * 1000;
3132

3233
const FETCH_TIMEOUT_MS = 10_000;
34+
const MODEL_NEW_BADGE_DURATION_MS = 7 * 24 * 60 * 60 * 1000;
3335

3436
const ManifestModelStatus = Schema.Literals(["current", "legacy"]);
3537

@@ -45,7 +47,8 @@ const ManifestProviderModel = Schema.Struct({
4547
subProvider: Schema.optional(TrimmedNonEmptyString),
4648
aliases: Schema.optional(Schema.Array(TrimmedNonEmptyString)),
4749
status: ManifestModelStatus,
48-
badge: Schema.optional(Schema.Literal("new")),
50+
/** ISO date or timestamp of the model's public release, as published by its provider. */
51+
releasedAt: Schema.optional(Schema.String),
4952
profile: Schema.optional(TrimmedNonEmptyString),
5053
adapter: Schema.optional(Schema.Unknown),
5154
});
@@ -110,6 +113,7 @@ function manifestUpdatedAtMs(manifest: ModelManifestData): number {
110113
export function resolveProviderCatalog(
111114
manifest: ModelManifestData,
112115
driverKind: ProviderDriverKind,
116+
nowMs = DateTime.toEpochMillis(DateTime.nowUnsafe()),
113117
): ModelCatalog.ProviderCatalog | null {
114118
const catalog = manifest.providers?.[driverKind];
115119
if (!catalog) return null;
@@ -123,13 +127,19 @@ export function resolveProviderCatalog(
123127
const profile = entry.profile ? catalog.profiles[entry.profile] : undefined;
124128
if (entry.profile && !profile) return null;
125129

130+
const releasedAtMs = entry.releasedAt === undefined ? NaN : Date.parse(entry.releasedAt);
131+
const showNewBadge =
132+
entry.status === "current" &&
133+
nowMs >= releasedAtMs &&
134+
nowMs - releasedAtMs < MODEL_NEW_BADGE_DURATION_MS;
135+
126136
models.push({
127137
slug: entry.slug,
128138
name: entry.name,
129139
...(entry.shortName ? { shortName: entry.shortName } : {}),
130140
...(entry.subProvider ? { subProvider: entry.subProvider } : {}),
131141
...(entry.aliases ? { aliases: entry.aliases } : {}),
132-
...(entry.badge ? { badge: entry.badge } : {}),
142+
...(showNewBadge ? { badge: "new" as const } : {}),
133143
status: entry.status,
134144
capabilities: profile?.capabilities ?? null,
135145
adapter: entry.adapter,
@@ -267,10 +277,10 @@ export const layerModelCatalog = Layer.effect(
267277
Effect.gen(function* () {
268278
const manifest = yield* ModelManifest;
269279
return ModelCatalog.ModelCatalog.of({
270-
current: Effect.fn("ModelCatalog.current")(function (driverKind) {
271-
return manifest.current.pipe(
272-
Effect.map((data) => resolveProviderCatalog(data, driverKind) ?? undefined),
273-
);
280+
current: Effect.fn("ModelCatalog.current")(function* (driverKind: ProviderDriverKind) {
281+
const data = yield* manifest.current;
282+
const nowMs = yield* Clock.currentTimeMillis;
283+
return resolveProviderCatalog(data, driverKind, nowMs) ?? undefined;
274284
}),
275285
bundled: (driverKind) =>
276286
resolveProviderCatalog(BUNDLED_MODEL_MANIFEST, driverKind) ?? undefined,

0 commit comments

Comments
 (0)