Open History to see the last commit
1import {
2 AuthOrchestrationReadScope,
3 EnvironmentHttpApi,
4 type OrchestrationProjectShell,
5} from "@t3tools/contracts";
6import * as Effect from "effect/Effect";
7import * as Predicate from "effect/Predicate";
8import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
9import * as SqlClient from "effect/unstable/sql/SqlClient";
10
11import {
12 annotateEnvironmentRequest,
13 failEnvironmentInternal,
14 failEnvironmentNotFound,
15 requireEnvironmentScope,
16} from "../auth/http.ts";
17import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
18import * as OrchestrationEventStore from "../persistence/Services/OrchestrationEventStore.ts";
19import * as ProjectEnrichmentService from "../project/ProjectEnrichmentService.ts";
20import * as ThreadManagementService from "./ThreadManagementService.ts";
21
22function isThreadNotFound(error: unknown): boolean {
23 return (
24 Predicate.hasProperty(error, "cause") &&
25 Predicate.hasProperty(error.cause, "_tag") &&
26 error.cause._tag === "ProjectionStoreThreadNotFoundError"
27 );
28}
29
30/**
31 * Serves orchestration V2 snapshots over HTTP so clients can load the
32 * (potentially large) shell and thread projections off the socket — gzip
33 * compressible and cacheable — and then resume the WebSocket subscription via
34 * `afterSequence`.
35 */
36export const orchestrationHttpApiLayer = HttpApiBuilder.group(
37 EnvironmentHttpApi,
38 "orchestration",
39 Effect.fnUntraced(function* (handlers) {
40 const sql = yield* SqlClient.SqlClient;
41 const threadManagement = yield* ThreadManagementService.ThreadManagementService;
42 const applicationEvents = yield* OrchestrationEventStore.OrchestrationEventStore;
43 const projectionSnapshotQuery = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
44 const projectEnrichment = yield* ProjectEnrichmentService.ProjectEnrichmentService;
45
46 const enrichProjectShells = Effect.fn("http.orchestration.enrichProjectShells")(
47 (projects: ReadonlyArray<OrchestrationProjectShell>) =>
48 Effect.forEach(
49 projects,
50 (project) =>
51 // Use immediately available enrichment only. Awaiting git-backed
52 // identity resolution can exceed the client shell-snapshot budget
53 // (ProcessRunner allows probes up to one minute). Background workers
54 // plus the WS enrichment subscription fill in repositoryIdentity.
55 projectEnrichment.getAvailable(project.workspaceRoot).pipe(
56 Effect.map((enrichment) => ({
57 ...project,
58 repositoryIdentity: enrichment.repositoryIdentity,
59 })),
60 ),
61 { concurrency: 16 },
62 ),
63 );
64
65 const loadShellSnapshot = Effect.fn("http.orchestration.loadShellSnapshot")(function* () {
66 const base = yield* sql.withTransaction(
67 Effect.gen(function* () {
68 const projects = yield* projectionSnapshotQuery.getShellSnapshotWithoutEnrichment();
69 const threads = yield* threadManagement.getShellSnapshot();
70 return {
71 schemaVersion: threads.schemaVersion,
72 snapshotSequence: yield* applicationEvents.latestApplicationSequence,
73 projects: projects.projects,
74 threads: threads.threads,
75 archivedThreads: threads.archivedThreads,
76 } as const;
77 }),
78 );
79 const projects = yield* enrichProjectShells(base.projects);
80 return { ...base, projects };
81 });
82
83 return handlers
84 .handle(
85 "shellSnapshot",
86 Effect.fn("environment.orchestration.shellSnapshot")(function* (args) {
87 yield* annotateEnvironmentRequest(args.endpoint.name);
88 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
89 return yield* loadShellSnapshot().pipe(
90 Effect.catch((cause) =>
91 failEnvironmentInternal("orchestration_snapshot_failed", cause),
92 ),
93 );
94 }),
95 )
96 .handle(
97 "threadSnapshot",
98 Effect.fn("environment.orchestration.threadSnapshot")(function* (args) {
99 yield* annotateEnvironmentRequest(args.endpoint.name);
100 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
101 const snapshot = yield* threadManagement.getThreadSnapshot(args.params.threadId).pipe(
102 Effect.catch(
103 Effect.fnUntraced(function* (error) {
104 if (isThreadNotFound(error)) {
105 return yield* failEnvironmentNotFound("thread_not_found");
106 }
107 return yield* failEnvironmentInternal(
108 "orchestration_thread_snapshot_failed",
109 error,
110 );
111 }),
112 ),
113 );
114 return {
115 snapshotSequence: snapshot.snapshotSequence,
116 projection: snapshot.projection,
117 };
118 }),
119 );
120 }),
121);