Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/mobile/src/state/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
);
export const environmentThreadShells = createEnvironmentThreadShellAtoms({
catalogValueAtom: environmentCatalog.catalogValueAtom,
connectionStateAtom: environmentCatalog.stateAtom,
snapshotAtom: environmentSnapshotAtom,
});

Expand Down
1 change: 1 addition & 0 deletions apps/web/src/state/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
);
export const environmentThreadShells = createEnvironmentThreadShellAtoms({
catalogValueAtom: environmentCatalog.catalogValueAtom,
connectionStateAtom: environmentCatalog.stateAtom,
snapshotAtom: environmentSnapshotAtom,
});

Expand Down
4 changes: 4 additions & 0 deletions docs/user/thread-sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ one environment.
Pinned threads still move to **Settled** when they become inactive. They also move when their pull
request merges if **Auto-settle merged threads** is enabled.

When a remote environment is unavailable, its cached threads are hidden from the combined sidebar
while you are online. The cache is kept and appears again after reconnecting; while your device is
offline, cached threads remain visible for browsing.

When you un-settle a thread, it returns to the top of the active list so you can find it right
away. Its timestamps do not change. Other threads keep their positions.

Expand Down
99 changes: 98 additions & 1 deletion packages/client-runtime/src/state/entities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import { describe, expect, it } from "@effect/vitest";
import * as Option from "effect/Option";
import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";

import { PrimaryConnectionTarget } from "../connection/model.ts";
import {
AVAILABLE_CONNECTION_STATE,
ConnectionTransientError,
PrimaryConnectionTarget,
type SupervisorConnectionState,
} from "../connection/model.ts";
import {
InvalidScopedProjectKeyError,
InvalidScopedProjectRefCollectionKeyError,
Expand Down Expand Up @@ -150,6 +155,18 @@ function shellState(snapshot: OrchestrationShellSnapshot): EnvironmentShellState
}

function makeHarness() {
const connectionStateAtoms = Atom.family((_environmentId: EnvironmentId) =>
Atom.make(
AsyncResult.success<SupervisorConnectionState>({
...AVAILABLE_CONNECTION_STATE,
desired: true,
network: "online",
phase: "connected",
attempt: 1,
generation: 1,
}),
),
);
const shellStateAtoms = Atom.family((_environmentId: EnvironmentId) =>
Atom.make(AsyncResult.success(shellState(SNAPSHOT))),
);
Expand Down Expand Up @@ -180,6 +197,7 @@ function makeHarness() {
});
const threadShells = createEnvironmentThreadShellAtoms({
catalogValueAtom,
connectionStateAtom: connectionStateAtoms,
snapshotAtom,
});
const threadDetails = createEnvironmentThreadDetailAtoms((environmentId, threadId) =>
Expand All @@ -188,6 +206,7 @@ function makeHarness() {

return {
registry: AtomRegistry.make(),
connectionStateAtom: connectionStateAtoms,
shellStateAtom: shellStateAtoms(ENVIRONMENT_ID),
threadStateAtom: (threadId: ThreadId) => threadStateAtoms(`${ENVIRONMENT_ID}\u0000${threadId}`),
projects,
Expand All @@ -197,6 +216,84 @@ function makeHarness() {
}

describe("environment entity projections", () => {
it("hides unavailable environment caches from the global thread list while online", () => {
const harness = makeHarness();
const failure = new ConnectionTransientError({
reason: "remote-unavailable",
detail: "Remote environment is unavailable.",
});
const environmentRefs = harness.registry.get(
harness.threadShells.environmentThreadRefsAtom(ENVIRONMENT_ID),
);
const projectThreadsAtom = harness.threadShells.threadShellsForProjectRefsAtom([
{ environmentId: ENVIRONMENT_ID, projectId: PROJECT_ID },
]);
const projectThreads = harness.registry.get(projectThreadsAtom);

harness.registry.set(
harness.connectionStateAtom(ENVIRONMENT_ID),
AsyncResult.success({
...AVAILABLE_CONNECTION_STATE,
desired: true,
network: "online",
phase: "connecting",
stage: "preparing",
attempt: 1,
}),
);
expect(harness.registry.get(harness.threadShells.threadRefsAtom)).toEqual(environmentRefs);
expect(harness.registry.get(projectThreadsAtom)).toBe(projectThreads);

const unavailable = {
desired: true,
network: "online",
phase: "backoff",
stage: null,
attempt: 1,
generation: 0,
lastFailure: failure,
retryAt: 1,
} as const;
harness.registry.set(
harness.connectionStateAtom(ENVIRONMENT_ID),
AsyncResult.success(unavailable),
);

expect(harness.registry.get(harness.threadShells.threadRefsAtom)).toEqual([]);
expect(harness.registry.get(projectThreadsAtom)).toEqual([]);
expect(
harness.registry.get(harness.threadShells.environmentThreadRefsAtom(ENVIRONMENT_ID)),
).toBe(environmentRefs);

harness.registry.set(
harness.connectionStateAtom(ENVIRONMENT_ID),
AsyncResult.success({
...unavailable,
phase: "connecting",
stage: "preparing",
attempt: 2,
retryAt: null,
}),
);

expect(harness.registry.get(harness.threadShells.threadRefsAtom)).toEqual([]);
expect(harness.registry.get(projectThreadsAtom)).toEqual([]);

harness.registry.set(
harness.connectionStateAtom(ENVIRONMENT_ID),
AsyncResult.success({
...unavailable,
network: "offline",
phase: "offline",
attempt: 2,
retryAt: null,
}),
);

expect(harness.registry.get(harness.threadShells.threadRefsAtom)).toEqual(environmentRefs);
expect(harness.registry.get(projectThreadsAtom)).toEqual(projectThreads);
});

it("composes detail collections with authoritative shell workspace metadata", () => {
const messages: OrchestrationThread["messages"] = [];
const detail = {
Expand Down
29 changes: 27 additions & 2 deletions packages/client-runtime/src/state/threadShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import type {
ScopedThreadRef,
ThreadId,
} from "@t3tools/contracts";
import { Atom } from "effect/unstable/reactivity";
import * as Option from "effect/Option";
import { AsyncResult, Atom } from "effect/unstable/reactivity";

import { AVAILABLE_CONNECTION_STATE, type SupervisorConnectionState } from "../connection/model.ts";
import type { EnvironmentThreadShell } from "./models.ts";
import { scopeThreadShell } from "./models.ts";
import type { EnvironmentCatalogState } from "./connections.ts";
Expand All @@ -29,8 +31,11 @@ const EMPTY_THREAD_REFS_BY_PROJECT: ReadonlyMap<
ReadonlyArray<ScopedThreadRef>
> = new Map();

export function createEnvironmentThreadShellAtoms(input: {
export function createEnvironmentThreadShellAtoms<E>(input: {
readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
readonly connectionStateAtom: (
environmentId: EnvironmentId,
) => Atom.Atom<AsyncResult.AsyncResult<SupervisorConnectionState, E>>;
readonly snapshotAtom: (
environmentId: EnvironmentId,
) => Atom.Atom<OrchestrationShellSnapshot | null>;
Expand Down Expand Up @@ -67,6 +72,20 @@ export function createEnvironmentThreadShellAtoms(input: {
}).pipe(Atom.withLabel(`environment-thread-refs:${environmentId}`));
});

const environmentThreadsVisibleAtom = Atom.family((environmentId: EnvironmentId) =>
Atom.make((get) => {
const connection = Option.getOrElse(
AsyncResult.value(get(input.connectionStateAtom(environmentId))),
() => AVAILABLE_CONNECTION_STATE,
);
const isInitialConnection =
connection.phase === "connecting" && connection.lastFailure === null;
return (
connection.network !== "online" || connection.phase === "connected" || isInitialConnection
);
}).pipe(Atom.withLabel(`environment-threads-visible:${environmentId}`)),
);

const environmentThreadRefsByProjectAtom = Atom.family((environmentId: EnvironmentId) => {
let previous: ReadonlyMap<
ProjectId,
Expand Down Expand Up @@ -122,6 +141,9 @@ export function createEnvironmentThreadShellAtoms(input: {
const next: EnvironmentThreadShell[] = [];
const seen = new Set<string>();
for (const projectRef of projectRefs) {
if (!get(environmentThreadsVisibleAtom(projectRef.environmentId))) {
continue;
}
const refs =
get(environmentThreadRefsByProjectAtom(projectRef.environmentId)).get(
projectRef.projectId,
Expand Down Expand Up @@ -150,6 +172,9 @@ export function createEnvironmentThreadShellAtoms(input: {
const threadRefsAtom = Atom.make((get) => {
const refs: ScopedThreadRef[] = [];
for (const environmentId of get(input.catalogValueAtom).entries.keys()) {
if (!get(environmentThreadsVisibleAtom(environmentId))) {
continue;
}
refs.push(...get(environmentThreadRefsAtom(environmentId)));
}
if (threadRefsEqual(previousThreadRefs, refs)) {
Expand Down
Loading