Files
t3code/packages/client-runtime/src/state/boundedThreadSnapshotHttp.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
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]>
2026-10-02 12:22:22 -07:00

163 lines
6.7 KiB
TypeScript

import type { ThreadId } from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import { HttpClient } from "effect/unstable/http";
import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
import type { PreparedConnection } from "../connection/model.ts";
import { environmentEndpointUrl } from "../environment/endpoint.ts";
import * as ManagedRelay from "../relay/managedRelay.ts";
import {
executeAuthenticatedEnvironmentHttpRequest,
withOrchestrationProtocolHeader,
} from "./environmentHttpAuth.ts";
import * as ThreadSnapshotLoader from "./threadSnapshotHttp.ts";
// Same cold-open budget as the full snapshot path; bounded payloads should fit.
const DEFAULT_BOUNDED_THREAD_SNAPSHOT_TIMEOUT_MS = 6_000;
/** Load a bounded recent-window thread snapshot over HTTP. */
export const fetchEnvironmentBoundedThreadSnapshot = Effect.fn(
"clientRuntime.state.fetchEnvironmentBoundedThreadSnapshot",
)(function* (input: {
readonly prepared: PreparedConnection;
readonly threadId: ThreadId;
readonly signer: Option.Option<ManagedRelay.ManagedRelayDpopSigner["Service"]>;
readonly remoteAuthorization?: Option.Option<
RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization["Service"]
>;
readonly timeoutMs?: number;
}) {
return yield* executeAuthenticatedEnvironmentHttpRequest({
...input,
group: "orchestration",
method: "GET",
url: (httpBaseUrl) =>
environmentEndpointUrl(httpBaseUrl, `/api/orchestration/threads/${input.threadId}/bounded`),
timeoutMs: input.timeoutMs ?? DEFAULT_BOUNDED_THREAD_SNAPSHOT_TIMEOUT_MS,
request: ({ client, headers }) =>
client.threadBoundedSnapshot({
params: { threadId: input.threadId },
headers: withOrchestrationProtocolHeader(headers),
}),
});
});
/**
* Shared ThreadSnapshotLoader for clients that render progressive history.
* Seeds from the bounded HTTP snapshot when available. Older servers that lack
* the bounded route (plain/generic 404) fall back to the existing full HTTP
* thread snapshot. Structured EnvironmentResourceNotFoundError from either
* endpoint still means missing. Transient failures report `unavailable` so the
* socket path remains a last resort for connectivity issues.
*/
export const boundedThreadSnapshotLoaderLayer: Layer.Layer<
ThreadSnapshotLoader.ThreadSnapshotLoader,
never,
HttpClient.HttpClient
> = Layer.effect(
ThreadSnapshotLoader.ThreadSnapshotLoader,
Effect.gen(function* () {
const httpClient = yield* HttpClient.HttpClient;
const signer = yield* Effect.serviceOption(ManagedRelay.ManagedRelayDpopSigner);
const remoteAuthorization = yield* Effect.serviceOption(
RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization,
);
return ThreadSnapshotLoader.ThreadSnapshotLoader.of({
load: (prepared: PreparedConnection, threadId: ThreadId) => {
const loadFullFallback = ThreadSnapshotLoader.fetchEnvironmentThreadSnapshot({
prepared,
threadId,
signer,
remoteAuthorization,
}).pipe(
Effect.map((snapshot): ThreadSnapshotLoader.ThreadSnapshotLoadResult => ({
_tag: "present",
snapshot,
})),
Effect.provideService(HttpClient.HttpClient, httpClient),
Effect.catchTags({
EnvironmentResourceNotFoundError: () =>
Effect.logDebug(
"Full thread snapshot not found over HTTP after bounded fallback; treating the thread as deleted.",
).pipe(
Effect.annotateLogs({ threadId }),
Effect.as({
_tag: "missing",
} satisfies ThreadSnapshotLoader.ThreadSnapshotLoadResult),
),
}),
Effect.catchCause((cause) =>
Effect.logWarning(
"Could not load the full thread snapshot over HTTP after bounded fallback; using the socket snapshot instead.",
).pipe(
Effect.annotateLogs({ threadId, cause: Cause.pretty(cause) }),
Effect.as({
_tag: "unavailable",
} satisfies ThreadSnapshotLoader.ThreadSnapshotLoadResult),
),
),
);
return fetchEnvironmentBoundedThreadSnapshot({
prepared,
threadId,
signer,
remoteAuthorization,
}).pipe(
Effect.map((bounded): ThreadSnapshotLoader.ThreadSnapshotLoadResult => ({
_tag: "present",
snapshot: {
snapshotSequence: bounded.snapshotSequence,
projection: bounded.projection,
latestLocalTurnOrdinal: bounded.latestLocalTurnOrdinal,
},
history: {
historyCursor: bounded.historyCursor,
hasMoreHistory: bounded.hasMoreHistory,
latestLocalTurnOrdinal: bounded.latestLocalTurnOrdinal,
},
})),
Effect.provideService(HttpClient.HttpClient, httpClient),
Effect.catchTags({
EnvironmentResourceNotFoundError: () =>
Effect.logDebug(
"Bounded thread snapshot not found over HTTP; treating the thread as deleted.",
).pipe(
Effect.annotateLogs({ threadId }),
Effect.as({
_tag: "missing",
} satisfies ThreadSnapshotLoader.ThreadSnapshotLoadResult),
),
RemoteEnvironmentAuthInvalidJsonError: (error) =>
Effect.logDebug(
"Bounded thread snapshot returned an invalid response; trying the full HTTP thread snapshot for an older server.",
).pipe(
Effect.annotateLogs({ threadId, cause: error.message }),
Effect.andThen(loadFullFallback),
),
RemoteEnvironmentAuthUndeclaredStatusError: (error) =>
error.status === 404
? Effect.logDebug(
"Bounded thread snapshot route was not found; trying the full HTTP thread snapshot for an older server.",
).pipe(Effect.annotateLogs({ threadId }), Effect.andThen(loadFullFallback))
: Effect.fail(error),
}),
Effect.catchCause((cause) =>
Effect.logWarning(
"Could not load the bounded thread snapshot over HTTP; using the socket snapshot instead.",
).pipe(
Effect.annotateLogs({ threadId, cause: Cause.pretty(cause) }),
Effect.as({
_tag: "unavailable",
} satisfies ThreadSnapshotLoader.ThreadSnapshotLoadResult),
),
),
);
},
});
}),
);