Skip to content

Commit b07edfa

Browse files
fix(archive): settle loading for unavailable environments
1 parent 4e511c9 commit b07edfa

5 files changed

Lines changed: 39 additions & 9 deletions

File tree

‎apps/mobile/src/features/archive/useArchivedThreadSnapshots.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import type { EnvironmentId } from "@t3tools/contracts";
88
import { useCallback, useMemo } from "react";
99

10+
import { environmentCatalog } from "../../connection/catalog";
1011
import { appAtomRegistry } from "../../state/atom-registry";
1112
import { orchestrationEnvironment } from "../../state/orchestration";
1213

@@ -16,6 +17,7 @@ function archivedSnapshotAtom(environmentId: EnvironmentId) {
1617

1718
const archivedSnapshotsAtom = createArchivedThreadSnapshotsAtomFamily({
1819
getSnapshotAtom: archivedSnapshotAtom,
20+
getConnectionStateAtom: environmentCatalog.stateAtom,
1921
labelPrefix: "mobile:archived-thread-snapshots",
2022
});
2123

‎apps/web/src/lib/archivedThreadsState.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "@t3tools/client-runtime/state/threads";
77
import type { EnvironmentId } from "@t3tools/contracts";
88

9+
import { environmentCatalog } from "../connection/catalog";
910
import { orchestrationEnvironment } from "../state/orchestration";
1011

1112
function archivedSnapshotAtom(environmentId: EnvironmentId) {
@@ -17,6 +18,7 @@ function archivedSnapshotAtom(environmentId: EnvironmentId) {
1718

1819
const archivedSnapshotsAtom = createArchivedThreadSnapshotsAtomFamily({
1920
getSnapshotAtom: archivedSnapshotAtom,
21+
getConnectionStateAtom: environmentCatalog.stateAtom,
2022
labelPrefix: "web:archived-thread-snapshots",
2123
});
2224

‎docs/internals/performance-regressions.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ The v2 checks pin these invariants:
4242
route output deltas through `turn_item.updated`: a busy command would flood the event store and
4343
block ingestion for every other thread.
4444
- The initial shell contains active navigation rows only. Archived rows use the dedicated archive
45-
query, and transcript message bodies stay in thread detail regardless of message size.
45+
snapshot/stream, and transcript message bodies stay in thread detail regardless of message size.
4646
- Shell resume sends deltas plus compact repository-enrichment metadata, not another full project
4747
and thread snapshot.
4848
- Auto, steer, and restart sends resolve delivery from authoritative state inside the

‎packages/client-runtime/src/state/archivedThreads.test.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import * as EnvironmentRegistry from "../connection/registry.ts";
2828
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
2929
import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
3030
import type { RpcSession } from "../rpc/session.ts";
31+
import { createEnvironmentCatalogAtoms } from "./connections.ts";
3132
import { createOrchestrationEnvironmentAtoms } from "./orchestration.ts";
3233
import { v2Now, v2Project, v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
3334

@@ -54,6 +55,7 @@ it("does not expose an archived snapshot failure message", () => {
5455
Cause.fail(new Error("credential=secret-value")),
5556
),
5657
),
58+
getConnectionStateAtom: () => Atom.make(AsyncResult.initial<SupervisorConnectionState>()),
5759
labelPrefix: "test:archived-thread-snapshots",
5860
});
5961
const registry = AtomRegistry.make();
@@ -141,21 +143,33 @@ it.effect("keeps environment archives live and replaces stale data on reconnect"
141143
environmentId === envA ? a.supervisor : b.supervisor,
142144
),
143145
} as EnvironmentRegistry.EnvironmentRegistry["Service"]);
144-
const atoms = createOrchestrationEnvironmentAtoms(
145-
Atom.runtime(
146-
Layer.merge(
147-
Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environments),
148-
FetchHttpClient.layer,
149-
),
146+
const runtime = Atom.runtime(
147+
Layer.merge(
148+
Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environments),
149+
FetchHttpClient.layer,
150150
),
151151
);
152+
const atoms = createOrchestrationEnvironmentAtoms(runtime);
153+
const catalog = createEnvironmentCatalogAtoms(runtime);
152154
const snapshots = createArchivedThreadSnapshotsAtomFamily({
153155
getSnapshotAtom: (environmentId) => atoms.archivedShell({ environmentId, input: {} }),
156+
getConnectionStateAtom: catalog.stateAtom,
154157
labelPrefix: "test:archived-thread-snapshots",
155158
})(makeArchivedThreadsEnvironmentKey([envA, envB]));
156159
const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) =>
157160
Effect.sync(() => registry.dispose()),
158161
);
162+
for (const [environmentId, phase] of [
163+
[envA, "connected"],
164+
[envB, "offline"],
165+
] as const) {
166+
yield* AtomRegistry.toStream(registry, catalog.stateAtom(environmentId)).pipe(
167+
Stream.filter((result) =>
168+
Option.exists(AsyncResult.value(result), (state) => state.phase === phase),
169+
),
170+
Stream.runHead,
171+
);
172+
}
159173
registry.mount(snapshots);
160174
expect(registry.get(snapshots).isLoading).toBe(true);
161175

‎packages/client-runtime/src/state/archivedThreads.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import * as Option from "effect/Option";
99
import * as Order from "effect/Order";
1010
import { AsyncResult, Atom } from "effect/reactivity";
1111

12+
import type { SupervisorConnectionState } from "../connection/model.ts";
13+
1214
export interface ArchivedSnapshotEntry {
1315
readonly environmentId: EnvironmentId;
1416
readonly snapshot: OrchestrationV2ArchivedShellSnapshot;
@@ -64,6 +66,9 @@ export function createArchivedThreadSnapshotsAtomFamily<E>(options: {
6466
readonly getSnapshotAtom: (
6567
environmentId: EnvironmentId,
6668
) => Atom.Atom<AsyncResult.AsyncResult<OrchestrationV2ArchivedShellSnapshot, E>>;
69+
readonly getConnectionStateAtom: (
70+
environmentId: EnvironmentId,
71+
) => Atom.Atom<AsyncResult.AsyncResult<SupervisorConnectionState, unknown>>;
6772
readonly labelPrefix: string;
6873
}) {
6974
return Atom.family((environmentKey: string) =>
@@ -74,15 +79,22 @@ export function createArchivedThreadSnapshotsAtomFamily<E>(options: {
7479

7580
for (const environmentId of parseArchivedThreadsEnvironmentKey(environmentKey)) {
7681
const result = get(options.getSnapshotAtom(environmentId));
82+
const connection = get(options.getConnectionStateAtom(environmentId));
83+
const state = Option.getOrNull(AsyncResult.value(connection));
84+
const unavailable =
85+
connection._tag === "Failure" ||
86+
state?.phase === "available" ||
87+
state?.phase === "offline" ||
88+
state?.phase === "blocked";
7789
// Live snapshots stay `waiting` while subscribed; only the first load counts.
78-
isLoading ||= result._tag === "Initial";
90+
isLoading ||= result._tag === "Initial" && !unavailable;
7991

8092
const snapshot = Option.getOrNull(AsyncResult.value(result));
8193
if (snapshot !== null) {
8294
snapshots.push({ environmentId, snapshot });
8395
}
8496

85-
if (error === null && result._tag === "Failure") {
97+
if (error === null && (result._tag === "Failure" || unavailable)) {
8698
error = "Failed to load archived threads.";
8799
}
88100
}

0 commit comments

Comments
 (0)