+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
454 lines
16 KiB
TypeScript
454 lines
16 KiB
TypeScript
import {
|
|
ORCHESTRATION_V2_WS_METHODS,
|
|
type EnvironmentId,
|
|
type OrchestrationV2ShellSnapshot,
|
|
type OrchestrationV2ShellStreamItem,
|
|
type ServerConfig,
|
|
} from "@t3tools/contracts";
|
|
import * as Cause from "effect/Cause";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Option from "effect/Option";
|
|
import * as Queue from "effect/Queue";
|
|
import * as Ref from "effect/Ref";
|
|
import * as Semaphore from "effect/Semaphore";
|
|
import * as Stream from "effect/Stream";
|
|
import * as SubscriptionRef from "effect/SubscriptionRef";
|
|
import { AsyncResult, Atom } from "effect/unstable/reactivity";
|
|
|
|
import * as EnvironmentRegistry from "../connection/registry.ts";
|
|
import { connectionProjectionPhase } from "../connection/model.ts";
|
|
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
|
|
import * as ConnectionWakeups from "../connection/wakeups.ts";
|
|
import { safeErrorLogAttributes } from "../errors/safeLog.ts";
|
|
import * as Persistence from "../platform/persistence.ts";
|
|
import { runCachePersistence } from "./cachePersistence.ts";
|
|
import { subscribeDynamic } from "../rpc/client.ts";
|
|
import type { RpcSession } from "../rpc/session.ts";
|
|
import * as ShellSnapshotLoader from "./shellSnapshotHttp.ts";
|
|
import { applyShellStreamEvent, mergeShellSnapshotProjects } from "./shellReducer.ts";
|
|
import { type EnvironmentCatalogState, enabledEnvironmentIds } from "./connections.ts";
|
|
import { followStreamInEnvironment } from "./runtime.ts";
|
|
|
|
export type EnvironmentShellStatus = "empty" | "cached" | "synchronizing" | "live";
|
|
|
|
export interface EnvironmentShellState {
|
|
readonly snapshot: Option.Option<OrchestrationV2ShellSnapshot>;
|
|
readonly status: EnvironmentShellStatus;
|
|
readonly error: Option.Option<string>;
|
|
}
|
|
|
|
const EMPTY_SHELL_STATE: EnvironmentShellState = {
|
|
snapshot: Option.none(),
|
|
status: "empty",
|
|
error: Option.none(),
|
|
};
|
|
|
|
function shellStatusForSnapshot(
|
|
snapshot: Option.Option<OrchestrationV2ShellSnapshot>,
|
|
): EnvironmentShellStatus {
|
|
return Option.isSome(snapshot) ? "cached" : "empty";
|
|
}
|
|
|
|
const SHELL_SYNCHRONIZATION_ERROR_MESSAGE = "Could not synchronize environment data.";
|
|
|
|
export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")(function* () {
|
|
const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
|
|
const cache = yield* Persistence.EnvironmentCacheStore;
|
|
const snapshotLoader = yield* ShellSnapshotLoader.ShellSnapshotLoader;
|
|
const wakeups = yield* Effect.serviceOption(ConnectionWakeups.ConnectionWakeups);
|
|
const environmentId = supervisor.target.environmentId;
|
|
const cachedSnapshot = yield* cache.loadShell(environmentId).pipe(
|
|
Effect.catch((error) =>
|
|
Effect.logWarning("Could not load cached environment shell.").pipe(
|
|
Effect.annotateLogs({
|
|
environmentId,
|
|
...safeErrorLogAttributes(error),
|
|
}),
|
|
Effect.as(Option.none<OrchestrationV2ShellSnapshot>()),
|
|
),
|
|
),
|
|
);
|
|
const state = yield* SubscriptionRef.make<EnvironmentShellState>({
|
|
snapshot: cachedSnapshot,
|
|
status: shellStatusForSnapshot(cachedSnapshot),
|
|
error: Option.none(),
|
|
});
|
|
const awaitingCompletion = yield* Ref.make(false);
|
|
const lastAuthoritativeSession = yield* Ref.make<RpcSession | null>(null);
|
|
const activeSubscriptionSession = yield* Ref.make<RpcSession | null>(null);
|
|
const latestLiveSnapshot = yield* Ref.make<Option.Option<OrchestrationV2ShellSnapshot>>(
|
|
Option.none(),
|
|
);
|
|
const persistence = yield* Queue.sliding<OrchestrationV2ShellSnapshot>(1);
|
|
|
|
const persistenceLock = yield* Semaphore.make(1);
|
|
let lastPersisted: OrchestrationV2ShellSnapshot | undefined;
|
|
const persistLatest = Effect.fn("EnvironmentShellState.persistLatest")(function* (
|
|
flush: boolean,
|
|
) {
|
|
while (true) {
|
|
const latest = yield* Ref.get(latestLiveSnapshot);
|
|
if (Option.isNone(latest) || latest.value === lastPersisted) return;
|
|
const snapshot = latest.value;
|
|
const saved = yield* cache.saveShell(environmentId, snapshot).pipe(
|
|
Effect.as(true),
|
|
Effect.catch((error) =>
|
|
Effect.logWarning("Could not persist environment shell cache.").pipe(
|
|
Effect.annotateLogs({ environmentId, ...safeErrorLogAttributes(error) }),
|
|
Effect.as(false),
|
|
),
|
|
),
|
|
);
|
|
if (!saved) return;
|
|
lastPersisted = snapshot;
|
|
// Only lifecycle flushes chase updates that arrived during an in-flight save.
|
|
// The regular worker leaves those updates for the next write window.
|
|
if (!flush) return;
|
|
}
|
|
}, persistenceLock.withPermit);
|
|
const persist = () => persistLatest(false);
|
|
const flushLiveShellSnapshot = persistLatest(true);
|
|
|
|
// Register before scoped worker fibers so reverse finalizer order interrupts
|
|
// those fibers first and this flush sees a stable latestLiveSnapshot.
|
|
yield* Effect.addFinalizer(() => flushLiveShellSnapshot);
|
|
|
|
yield* runCachePersistence(persistence, persist).pipe(Effect.forkScoped);
|
|
|
|
const setDisconnected = Ref.set(awaitingCompletion, false).pipe(
|
|
Effect.andThen(
|
|
SubscriptionRef.update(state, (current) => ({
|
|
...current,
|
|
status: shellStatusForSnapshot(current.snapshot),
|
|
})),
|
|
),
|
|
Effect.andThen(flushLiveShellSnapshot.pipe(Effect.forkScoped)),
|
|
Effect.asVoid,
|
|
);
|
|
const setSynchronizing = SubscriptionRef.update(state, (current) => ({
|
|
...current,
|
|
status: "synchronizing" as const,
|
|
error: Option.none(),
|
|
}));
|
|
const setReady = SubscriptionRef.update(state, (current) =>
|
|
current.status === "live"
|
|
? current
|
|
: {
|
|
...current,
|
|
status: "synchronizing" as const,
|
|
error: Option.none(),
|
|
},
|
|
);
|
|
const setStreamError = (error: unknown) =>
|
|
Ref.set(awaitingCompletion, false).pipe(
|
|
Effect.andThen(Effect.logWarning("Could not synchronize the environment shell.")),
|
|
Effect.annotateLogs({
|
|
environmentId,
|
|
...safeErrorLogAttributes(error),
|
|
}),
|
|
Effect.andThen(
|
|
SubscriptionRef.update(state, (current) => ({
|
|
...current,
|
|
status: shellStatusForSnapshot(current.snapshot),
|
|
error: Option.some(SHELL_SYNCHRONIZATION_ERROR_MESSAGE),
|
|
})),
|
|
),
|
|
);
|
|
|
|
// Apply each received batch with one state write. The RPC client's bounded
|
|
// buffer can split a server chunk, so a bulk action can still need several
|
|
// writes, but each write includes every event in that batch.
|
|
const applyItems = Effect.fn("EnvironmentShellState.applyItems")(function* (
|
|
items: ReadonlyArray<OrchestrationV2ShellStreamItem>,
|
|
) {
|
|
const initial = yield* SubscriptionRef.get(state);
|
|
let waiting = yield* Ref.get(awaitingCompletion);
|
|
let next = initial;
|
|
let receivedSnapshot = false;
|
|
for (const item of items) {
|
|
if (item.kind === "synchronized") {
|
|
waiting = false;
|
|
if (Option.isSome(next.snapshot)) {
|
|
next = { ...next, status: "live", error: Option.none() };
|
|
}
|
|
continue;
|
|
}
|
|
const nextSnapshot =
|
|
item.kind === "snapshot"
|
|
? mergeShellSnapshotProjects(
|
|
Option.getOrNull(next.snapshot),
|
|
item.snapshot,
|
|
item.resolvedRepositoryIdentityRoots === undefined
|
|
? undefined
|
|
: {
|
|
resolvedRepositoryIdentityRoots: item.resolvedRepositoryIdentityRoots,
|
|
},
|
|
)
|
|
: Option.match(next.snapshot, {
|
|
onNone: () => null,
|
|
onSome: (snapshot) =>
|
|
item.sequence > snapshot.snapshotSequence
|
|
? applyShellStreamEvent(snapshot, item)
|
|
: snapshot,
|
|
});
|
|
if (nextSnapshot === null) continue;
|
|
receivedSnapshot ||= item.kind === "snapshot";
|
|
next = {
|
|
snapshot: Option.some(nextSnapshot),
|
|
status: waiting ? "synchronizing" : "live",
|
|
error: Option.none(),
|
|
};
|
|
}
|
|
yield* Ref.set(awaitingCompletion, waiting);
|
|
if (next === initial) return;
|
|
if (Option.isSome(next.snapshot)) {
|
|
yield* Ref.set(latestLiveSnapshot, next.snapshot);
|
|
}
|
|
yield* SubscriptionRef.set(state, next);
|
|
if (receivedSnapshot) {
|
|
const session = yield* Ref.get(activeSubscriptionSession);
|
|
if (session !== null) {
|
|
yield* Ref.set(lastAuthoritativeSession, session);
|
|
}
|
|
}
|
|
if (next.snapshot !== initial.snapshot && Option.isSome(next.snapshot)) {
|
|
yield* Queue.offer(persistence, next.snapshot.value);
|
|
}
|
|
});
|
|
|
|
const foregroundResubscriptions = Option.match(wakeups, {
|
|
onNone: () => Stream.never,
|
|
onSome: (service) =>
|
|
service.changes.pipe(Stream.filter(ConnectionWakeups.shouldResubscribeAfterWakeup)),
|
|
});
|
|
|
|
yield* setSynchronizing;
|
|
yield* Effect.forkScoped(
|
|
subscribeDynamic(
|
|
ORCHESTRATION_V2_WS_METHODS.subscribeShell,
|
|
Effect.fn("EnvironmentShellState.makeSubscribeInput")(function* (session) {
|
|
yield* Ref.set(activeSubscriptionSession, session);
|
|
const supportsCompletionMarker = yield* session.initialConfig.pipe(
|
|
Effect.map((config) => config.shellResumeCompletionMarker === true),
|
|
Effect.orElseSucceed(() => false),
|
|
);
|
|
yield* Ref.set(awaitingCompletion, supportsCompletionMarker);
|
|
yield* setSynchronizing;
|
|
|
|
// Foreground resubscriptions on the same live session can resume from
|
|
// the in-memory cursor. A new session reloads the authoritative HTTP
|
|
// snapshot so a valid cursor cannot preserve incomplete cached data.
|
|
const hasAuthoritativeSnapshot = (yield* Ref.get(lastAuthoritativeSession)) === session;
|
|
let canResume = hasAuthoritativeSnapshot;
|
|
let current = yield* SubscriptionRef.get(state);
|
|
if (!hasAuthoritativeSnapshot || Option.isNone(current.snapshot)) {
|
|
const prepared = yield* SubscriptionRef.get(supervisor.prepared).pipe(
|
|
Effect.flatMap(
|
|
Option.match({
|
|
onSome: Effect.succeed,
|
|
onNone: () =>
|
|
SubscriptionRef.changes(supervisor.prepared).pipe(
|
|
Stream.filter(Option.isSome),
|
|
Stream.map((value) => value.value),
|
|
Stream.runHead,
|
|
Effect.map(Option.getOrThrow),
|
|
),
|
|
}),
|
|
),
|
|
);
|
|
const httpSnapshot = yield* snapshotLoader.load(prepared);
|
|
if (Option.isSome(httpSnapshot)) {
|
|
yield* applyItems([{ kind: "snapshot", snapshot: httpSnapshot.value }]);
|
|
canResume = true;
|
|
current = yield* SubscriptionRef.get(state);
|
|
}
|
|
}
|
|
|
|
// If the authoritative refresh failed, omit the cached cursor so the
|
|
// socket fallback sends a complete snapshot for this new session.
|
|
if (!canResume || Option.isNone(current.snapshot)) {
|
|
return supportsCompletionMarker ? { requestCompletionMarker: true as const } : {};
|
|
}
|
|
if (!supportsCompletionMarker) {
|
|
// Without a completion marker there is no synchronized signal for a
|
|
// resumed subscription, so report live immediately, like threads.
|
|
yield* SubscriptionRef.update(state, (value) => ({
|
|
...value,
|
|
status: "live" as const,
|
|
error: Option.none(),
|
|
}));
|
|
}
|
|
return {
|
|
afterSequence: current.snapshot.value.snapshotSequence,
|
|
...(supportsCompletionMarker ? { requestCompletionMarker: true as const } : {}),
|
|
};
|
|
}),
|
|
{
|
|
onExpectedFailure: (cause) => setStreamError(Cause.squash(cause)),
|
|
retryExpectedFailureAfter: "250 millis",
|
|
resubscribe: foregroundResubscriptions,
|
|
},
|
|
).pipe(Stream.runForEachArray(applyItems)),
|
|
);
|
|
yield* SubscriptionRef.changes(supervisor.state).pipe(
|
|
Stream.runForEach((connectionState) => {
|
|
switch (connectionProjectionPhase(connectionState)) {
|
|
case "synchronizing":
|
|
return setSynchronizing;
|
|
case "disconnected":
|
|
return setDisconnected;
|
|
case "ready":
|
|
return setReady;
|
|
}
|
|
}),
|
|
Effect.forkScoped,
|
|
);
|
|
|
|
return state;
|
|
});
|
|
|
|
function shellStateChanges(environmentId: EnvironmentId) {
|
|
return followStreamInEnvironment(
|
|
environmentId,
|
|
Stream.unwrap(makeEnvironmentShellState().pipe(Effect.map(SubscriptionRef.changes))),
|
|
);
|
|
}
|
|
|
|
export interface EnvironmentShellSummary {
|
|
readonly hasSnapshot: boolean;
|
|
readonly hasSynchronizingShell: boolean;
|
|
readonly hasCachedShell: boolean;
|
|
readonly hasLiveShell: boolean;
|
|
readonly firstError: string | null;
|
|
}
|
|
|
|
const EMPTY_ENVIRONMENT_SHELL_SUMMARY: EnvironmentShellSummary = Object.freeze({
|
|
hasSnapshot: false,
|
|
hasSynchronizingShell: false,
|
|
hasCachedShell: false,
|
|
hasLiveShell: false,
|
|
firstError: null,
|
|
});
|
|
|
|
const EMPTY_SERVER_CONFIGS: ReadonlyMap<EnvironmentId, ServerConfig> = new Map();
|
|
|
|
function shellSummariesEqual(
|
|
left: EnvironmentShellSummary,
|
|
right: EnvironmentShellSummary,
|
|
): boolean {
|
|
return (
|
|
left.hasSnapshot === right.hasSnapshot &&
|
|
left.hasSynchronizingShell === right.hasSynchronizingShell &&
|
|
left.hasCachedShell === right.hasCachedShell &&
|
|
left.hasLiveShell === right.hasLiveShell &&
|
|
left.firstError === right.firstError
|
|
);
|
|
}
|
|
|
|
function mapsEqual<K, V>(left: ReadonlyMap<K, V>, right: ReadonlyMap<K, V>): boolean {
|
|
if (left.size !== right.size) {
|
|
return false;
|
|
}
|
|
for (const [key, value] of left) {
|
|
if (right.get(key) !== value) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
export function createEnvironmentShellSummaryAtom(input: {
|
|
readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
|
|
readonly shellStateValueAtom: (environmentId: EnvironmentId) => Atom.Atom<EnvironmentShellState>;
|
|
}) {
|
|
let previousSummary = EMPTY_ENVIRONMENT_SHELL_SUMMARY;
|
|
return Atom.make((get) => {
|
|
let hasSnapshot = false;
|
|
let hasSynchronizingShell = false;
|
|
let hasCachedShell = false;
|
|
let hasLiveShell = false;
|
|
let firstError: string | null = null;
|
|
|
|
for (const environmentId of enabledEnvironmentIds(get(input.catalogValueAtom))) {
|
|
const state = get(input.shellStateValueAtom(environmentId));
|
|
hasSynchronizingShell ||= state.status === "synchronizing";
|
|
hasCachedShell ||= state.status === "cached";
|
|
hasLiveShell ||= state.status === "live";
|
|
if (firstError === null) {
|
|
firstError = Option.getOrNull(state.error);
|
|
}
|
|
if (Option.isNone(state.snapshot)) {
|
|
continue;
|
|
}
|
|
hasSnapshot = true;
|
|
}
|
|
|
|
const next: EnvironmentShellSummary = {
|
|
hasSnapshot,
|
|
hasSynchronizingShell,
|
|
hasCachedShell,
|
|
hasLiveShell,
|
|
firstError,
|
|
};
|
|
if (shellSummariesEqual(previousSummary, next)) {
|
|
return previousSummary;
|
|
}
|
|
previousSummary = next;
|
|
return previousSummary;
|
|
}).pipe(Atom.withLabel("environment-shell-summary"));
|
|
}
|
|
|
|
export function createEnvironmentServerConfigsAtom(input: {
|
|
readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
|
|
readonly serverConfigValueAtom: (environmentId: EnvironmentId) => Atom.Atom<ServerConfig | null>;
|
|
}) {
|
|
let previousServerConfigs = EMPTY_SERVER_CONFIGS;
|
|
return Atom.make((get) => {
|
|
const next = new Map<EnvironmentId, ServerConfig>();
|
|
for (const environmentId of enabledEnvironmentIds(get(input.catalogValueAtom))) {
|
|
const config = get(input.serverConfigValueAtom(environmentId));
|
|
if (config !== null) {
|
|
next.set(environmentId, config);
|
|
}
|
|
}
|
|
if (mapsEqual(previousServerConfigs, next)) {
|
|
return previousServerConfigs;
|
|
}
|
|
previousServerConfigs = next;
|
|
return previousServerConfigs;
|
|
}).pipe(Atom.withLabel("environment-server-configs"));
|
|
}
|
|
|
|
export function createEnvironmentShellAtoms<R, E>(
|
|
runtime: Atom.AtomRuntime<
|
|
| EnvironmentRegistry.EnvironmentRegistry
|
|
| Persistence.EnvironmentCacheStore
|
|
| ShellSnapshotLoader.ShellSnapshotLoader
|
|
| R,
|
|
E
|
|
>,
|
|
) {
|
|
const stateAtom = Atom.family((environmentId: EnvironmentId) =>
|
|
runtime.atom(shellStateChanges(environmentId), {
|
|
initialValue: EMPTY_SHELL_STATE,
|
|
}),
|
|
);
|
|
|
|
const stateValueAtom = Atom.family((environmentId: EnvironmentId) =>
|
|
Atom.make((get) =>
|
|
Option.getOrElse(AsyncResult.value(get(stateAtom(environmentId))), () => EMPTY_SHELL_STATE),
|
|
).pipe(Atom.withLabel(`environment-shell-state-value:${environmentId}`)),
|
|
);
|
|
|
|
return {
|
|
stateAtom,
|
|
stateValueAtom,
|
|
};
|
|
}
|
|
|
|
export * from "./models.ts";
|
|
export * from "./shellCommands.ts";
|
|
export * from "./shellReducer.ts";
|
|
export * as ShellSnapshotLoader from "./shellSnapshotHttp.ts";
|
|
export * from "./snapshots.ts";
|