The best way to control Claude Code, Codex and more from any computer or phone

main
ts 1,149 lines 43.7 KB
Raw
1 import {
2 ConnectionCatalogDocument,
3 type ConnectionCatalogDocument as ConnectionCatalogDocumentType,
4 EMPTY_CONNECTION_CATALOG_DOCUMENT,
5 ORCHESTRATION_CACHE_SCHEMA_VERSION,
6 StoredOrchestrationShellSnapshot,
7 StoredOrchestrationThreadSnapshot,
8 decodeOrDiscardOrchestrationCache,
9 putRemoteDpopTokenInCatalog,
10 registerConnectionInCatalog,
11 removeCatalogValue,
12 removeConnectionFromCatalog,
13 setConnectionEnabledInCatalog,
14 setRoutesInCatalog,
15 replaceCatalogValue,
16 Persistence,
17 makeStoredShellSnapshotEncoder,
18 makeStoredThreadSnapshotEncoder,
19 makeThreadCacheWriteOrder,
20 } from "@t3tools/client-runtime/platform";
21 import { TokenStore } from "@t3tools/client-runtime/authorization";
22 import { msg } from "gt-react";
23 import {
24 ConnectionTransientError,
25 ConnectionBlockedError,
26 CredentialStore,
27 ProfileStore,
28 PullRequestRoutingPermissions,
29 StoredPullRequestRoutingPermission,
30 pullRequestRoutingConnectionKey,
31 pullRequestRoutingPermissionFor,
32 } from "@t3tools/client-runtime/connection";
33 import { EnvironmentQueryStore } from "@t3tools/client-runtime/state/query-persistence";
34 import { EnvironmentId, ServerConfig, ThreadId, VcsListRefsResult } from "@t3tools/contracts";
35 import * as Clock from "effect/Clock";
36 import * as Context from "effect/Context";
37 import * as Effect from "effect/Effect";
38 import * as Layer from "effect/Layer";
39 import * as Option from "effect/Option";
40 import * as Ref from "effect/Ref";
41 import * as Queue from "effect/Queue";
42 import * as Schema from "effect/Schema";
43 import * as Semaphore from "effect/Semaphore";
44 import * as Stream from "effect/Stream";
45 import { m } from "../i18n/messages";
46 import { projectFaviconCache } from "../assets/projectFaviconCache";
47
48 const DATABASE_NAME = "t3code:connection-runtime";
49 const DATABASE_VERSION = 5;
50 const CATALOG_STORE_NAME = "catalog";
51 const SHELL_STORE_NAME = "shell";
52 const THREAD_STORE_NAME = "thread";
53 const SERVER_CONFIG_STORE_NAME = "server-config";
54 const VCS_REFS_STORE_NAME = "vcs-refs";
55 const QUERY_STORE_NAME = "query";
56 const QUERY_UPDATED_INDEX_NAME = "environment-updated";
57 const QUERY_MAX_ROWS = 256;
58 const QUERY_MAX_BYTES = 32 * 1024 * 1024;
59 const QUERY_MAX_PAYLOAD_BYTES = 4 * 1024 * 1024;
60 const QUERY_MAX_AGE_MS = 30 * 24 * 60 * 60_000;
61 // A stats answer saves one row per listed PR, so the bounds index is walked once per batch of
62 // saves. Each walk trims a batch below the caps, so the caps hold between walks. The batch count
63 // lives in the database, so tabs sharing it share one count.
64 const QUERY_PRUNE_BATCH_ROWS = 32;
65 const QUERY_PRUNE_BATCH_BYTES = 1024 * 1024;
66 const CATALOG_KEY = "document";
67 const StoredShellSnapshot = StoredOrchestrationShellSnapshot;
68 const StoredShellSnapshotJson = Schema.fromJsonString(StoredShellSnapshot);
69 const StoredThreadSnapshot = StoredOrchestrationThreadSnapshot;
70 const StoredThreadSnapshotJson = Schema.fromJsonString(StoredThreadSnapshot);
71 const StoredServerConfig = Schema.Struct({
72 schemaVersion: Schema.Literal(1),
73 environmentId: EnvironmentId,
74 config: ServerConfig,
75 });
76 const StoredServerConfigJson = Schema.fromJsonString(StoredServerConfig);
77 const StoredVcsRefs = Schema.Struct({
78 schemaVersion: Schema.Literal(1),
79 environmentId: EnvironmentId,
80 cwd: Schema.String,
81 refs: VcsListRefsResult,
82 });
83 const StoredVcsRefsJson = Schema.fromJsonString(StoredVcsRefs);
84 const isStoredQuery = Schema.is(
85 Schema.Struct({ environmentId: Schema.String, payload: Schema.String, updatedAt: Schema.Number }),
86 );
87 const isQueryBatch = Schema.is(Schema.Struct({ rows: Schema.Number, bytes: Schema.Number }));
88 const ConnectionCatalogDocumentJson = Schema.fromJsonString(ConnectionCatalogDocument);
89 const decodeConnectionCatalogDocument = Schema.decodeUnknownEffect(ConnectionCatalogDocumentJson);
90 const encodeConnectionCatalogDocument = Schema.encodeEffect(ConnectionCatalogDocumentJson);
91 const decodeStoredShellSnapshot = Schema.decodeUnknownEffect(StoredShellSnapshotJson);
92 const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(StoredThreadSnapshotJson);
93 const decodeStoredServerConfig = Schema.decodeUnknownEffect(StoredServerConfigJson);
94 const encodeStoredServerConfig = Schema.encodeEffect(StoredServerConfigJson);
95 const decodeStoredVcsRefs = Schema.decodeUnknownEffect(StoredVcsRefsJson);
96 const encodeStoredVcsRefs = Schema.encodeEffect(StoredVcsRefsJson);
97
98 const CATALOG_FAILURE = {
99 open: msg("Could not open the local connection catalog: {cause}"),
100 read: msg("Could not read the local connection catalog: {cause}"),
101 write: msg("Could not write the local connection catalog: {cause}"),
102 remove: msg("Could not remove the local connection catalog: {cause}"),
103 decode: msg("Could not decode the local connection catalog: {cause}"),
104 encode: msg("Could not encode the local connection catalog: {cause}"),
105 load: msg("Could not load the local connection catalog: {cause}"),
106 save: msg("Could not save the local connection catalog: {cause}"),
107 "save pull request routing permissions in": msg(
108 "Could not save pull request routing permissions in the local connection catalog: {cause}",
109 ),
110 };
111
112 function catalogError(operation: keyof typeof CATALOG_FAILURE, cause: unknown) {
113 return new ConnectionTransientError({
114 reason: "remote-unavailable",
115 detail: m(CATALOG_FAILURE[operation], { cause: String(cause) }),
116 });
117 }
118
119 const PERSISTENCE_FAILURE = {
120 "list-targets": msg("Could not list targets: {cause}"),
121 "list-disabled-targets": msg("Could not list disabled targets: {cause}"),
122 "register-connection": msg("Could not register connection: {cause}"),
123 "set-connection-routes": msg("Could not set connection routes: {cause}"),
124 "remove-connection": msg("Could not remove connection: {cause}"),
125 "set-connection-enabled": msg("Could not set connection enabled: {cause}"),
126 "load-shell": msg("Could not load shell: {cause}"),
127 "save-shell": msg("Could not save shell: {cause}"),
128 "load-thread": msg("Could not load thread: {cause}"),
129 "save-thread": msg("Could not save thread: {cause}"),
130 "remove-thread": msg("Could not remove thread: {cause}"),
131 "load-server-config": msg("Could not load server config: {cause}"),
132 "save-server-config": msg("Could not save server config: {cause}"),
133 "load-vcs-refs": msg("Could not load vcs refs: {cause}"),
134 "save-vcs-refs": msg("Could not save vcs refs: {cause}"),
135 "remove-vcs-refs": msg("Could not remove vcs refs: {cause}"),
136 "clear-vcs-refs": msg("Could not clear vcs refs: {cause}"),
137 "clear-environment": msg("Could not clear environment: {cause}"),
138 };
139
140 function persistenceError(operation: keyof typeof PERSISTENCE_FAILURE, cause: unknown) {
141 return new Persistence.ConnectionPersistenceError({
142 operation,
143 message: m(PERSISTENCE_FAILURE[operation], { cause: String(cause) }),
144 });
145 }
146
147 const openDatabase = Effect.fn("web.connectionStorage.openDatabase")(function* () {
148 return yield* Effect.callback<IDBDatabase, ConnectionTransientError>((resume) => {
149 if (typeof indexedDB === "undefined") {
150 resume(
151 Effect.fail(
152 catalogError("open", m(msg("IndexedDB is unavailable in this browser context."))),
153 ),
154 );
155 return;
156 }
157 try {
158 const request = indexedDB.open(DATABASE_NAME, DATABASE_VERSION);
159 request.addEventListener("upgradeneeded", () => {
160 if (!request.result.objectStoreNames.contains(CATALOG_STORE_NAME)) {
161 request.result.createObjectStore(CATALOG_STORE_NAME);
162 }
163 if (!request.result.objectStoreNames.contains(SHELL_STORE_NAME)) {
164 request.result.createObjectStore(SHELL_STORE_NAME);
165 }
166 if (!request.result.objectStoreNames.contains(THREAD_STORE_NAME)) {
167 request.result.createObjectStore(THREAD_STORE_NAME);
168 }
169 if (!request.result.objectStoreNames.contains(SERVER_CONFIG_STORE_NAME)) {
170 request.result.createObjectStore(SERVER_CONFIG_STORE_NAME);
171 }
172 if (!request.result.objectStoreNames.contains(VCS_REFS_STORE_NAME)) {
173 request.result.createObjectStore(VCS_REFS_STORE_NAME);
174 }
175 if (!request.result.objectStoreNames.contains(QUERY_STORE_NAME)) {
176 request.result
177 .createObjectStore(QUERY_STORE_NAME)
178 .createIndex(QUERY_UPDATED_INDEX_NAME, ["environmentId", "updatedAt", "bytes"]);
179 }
180 });
181 request.addEventListener("error", () => {
182 resume(
183 Effect.fail(catalogError("open", request.error ?? m(msg("Unknown IndexedDB error")))),
184 );
185 });
186 request.addEventListener("success", () => {
187 resume(Effect.succeed(request.result));
188 });
189 } catch (cause) {
190 resume(Effect.fail(catalogError("open", cause)));
191 }
192 });
193 });
194
195 interface DatabaseHandle {
196 readonly get: Effect.Effect<IDBDatabase, ConnectionTransientError>;
197 readonly invalidate: (database: IDBDatabase) => Effect.Effect<void>;
198 }
199
200 const makeDatabaseHandle = Effect.fn("web.connectionStorage.makeDatabaseHandle")(function* () {
201 const lock = yield* Semaphore.make(1);
202 let current: IDBDatabase | null = null;
203 const forget = (database: IDBDatabase) => {
204 if (current === database) current = null;
205 };
206 const get = Effect.suspend(() =>
207 current !== null
208 ? Effect.succeed(current)
209 : lock.withPermits(1)(
210 Effect.gen(function* () {
211 if (current !== null) return current;
212 const opened = yield* openDatabase();
213 current = opened;
214 opened.addEventListener("close", () => forget(opened));
215 // Another tab upgrading the schema waits on this connection.
216 opened.addEventListener("versionchange", () => {
217 forget(opened);
218 opened.close();
219 });
220 return opened;
221 }),
222 ),
223 );
224 const close = lock.withPermits(1)(
225 Effect.sync(() => {
226 current?.close();
227 current = null;
228 }),
229 );
230 const handle: DatabaseHandle = {
231 get,
232 invalidate: (database) => Effect.sync(() => forget(database)),
233 };
234 return { handle, close };
235 });
236
237 // Browsers can omit close events; retry InvalidStateError with a fresh connection.
238 function withDatabase<A>(
239 database: DatabaseHandle,
240 use: (opened: IDBDatabase) => Effect.Effect<A, ConnectionTransientError>,
241 ) {
242 const closedConnection = Symbol("closedConnection");
243 const attempt = Effect.flatMap(database.get, (opened) =>
244 use(opened).pipe(
245 Effect.catchIf(
246 (error) => error.detail.includes("InvalidStateError"),
247 (error) =>
248 database
249 .invalidate(opened)
250 .pipe(Effect.andThen(Effect.fail({ [closedConnection]: error } as const))),
251 ),
252 ),
253 );
254 return attempt.pipe(
255 Effect.catchIf(
256 (error): error is { readonly [closedConnection]: ConnectionTransientError } =>
257 closedConnection in error,
258 () => Effect.flatMap(database.get, use),
259 ),
260 );
261 }
262
263 function readDatabaseValueOnConnection(database: IDBDatabase, storeName: string, key: IDBValidKey) {
264 return Effect.callback<unknown, ConnectionTransientError>((resume) => {
265 try {
266 const request = database.transaction(storeName, "readonly").objectStore(storeName).get(key);
267 request.addEventListener("error", () => {
268 resume(
269 Effect.fail(
270 catalogError("read", request.error ?? m(msg("Unknown IndexedDB read error"))),
271 ),
272 );
273 });
274 request.addEventListener("success", () => {
275 resume(Effect.succeed(request.result));
276 });
277 } catch (cause) {
278 resume(Effect.fail(catalogError("read", cause)));
279 }
280 }).pipe(Effect.withSpan("web.connectionStorage.readDatabaseValue"));
281 }
282
283 function writeDatabaseValueOnConnection(
284 database: IDBDatabase,
285 storeName: string,
286 key: IDBValidKey,
287 value: unknown,
288 ) {
289 return Effect.callback<void, ConnectionTransientError>((resume) => {
290 try {
291 const transaction = database.transaction(storeName, "readwrite");
292 // Commit failures can fire abort without error; every failed write fires abort.
293 transaction.addEventListener("abort", () => {
294 resume(
295 Effect.fail(
296 catalogError("write", transaction.error ?? m(msg("Unknown IndexedDB write error"))),
297 ),
298 );
299 });
300 transaction.addEventListener("complete", () => {
301 resume(Effect.void);
302 });
303 transaction.objectStore(storeName).put(value, key);
304 } catch (cause) {
305 resume(Effect.fail(catalogError("write", cause)));
306 }
307 }).pipe(Effect.withSpan("web.connectionStorage.writeDatabaseValue"));
308 }
309
310 function removeDatabaseValueOnConnection(
311 database: IDBDatabase,
312 storeName: string,
313 key: IDBValidKey,
314 ) {
315 return Effect.callback<void, ConnectionTransientError>((resume) => {
316 try {
317 const transaction = database.transaction(storeName, "readwrite");
318 transaction.addEventListener("abort", () => {
319 resume(
320 Effect.fail(
321 catalogError("remove", transaction.error ?? m(msg("Unknown IndexedDB remove error"))),
322 ),
323 );
324 });
325 transaction.addEventListener("complete", () => {
326 resume(Effect.void);
327 });
328 transaction.objectStore(storeName).delete(key);
329 } catch (cause) {
330 resume(Effect.fail(catalogError("remove", cause)));
331 }
332 }).pipe(Effect.withSpan("web.connectionStorage.removeDatabaseValue"));
333 }
334
335 function removeDatabaseValuesInRangeOnConnection(
336 database: IDBDatabase,
337 storeName: string,
338 range: IDBKeyRange,
339 ) {
340 return Effect.callback<void, ConnectionTransientError>((resume) => {
341 try {
342 const transaction = database.transaction(storeName, "readwrite");
343 transaction.addEventListener("abort", () => {
344 resume(
345 Effect.fail(
346 catalogError("remove", transaction.error ?? m(msg("Unknown IndexedDB cursor error"))),
347 ),
348 );
349 });
350 transaction.addEventListener("complete", () => {
351 resume(Effect.void);
352 });
353 const request = transaction.objectStore(storeName).openCursor(range);
354 request.addEventListener("error", () => {
355 resume(
356 Effect.fail(
357 catalogError("remove", request.error ?? m(msg("Unknown IndexedDB cursor error"))),
358 ),
359 );
360 });
361 request.addEventListener("success", () => {
362 const cursor = request.result;
363 if (cursor === null) {
364 return;
365 }
366 try {
367 cursor.delete();
368 cursor.continue();
369 } catch (cause) {
370 resume(Effect.fail(catalogError("remove", cause)));
371 }
372 });
373 } catch (cause) {
374 resume(Effect.fail(catalogError("remove", cause)));
375 }
376 }).pipe(Effect.withSpan("web.connectionStorage.removeDatabaseValuesInRange"));
377 }
378
379 // Mirrors mobile's pull-request cache bounds, per environment.
380 function saveQueryOnConnection(
381 database: IDBDatabase,
382 environmentId: EnvironmentId,
383 key: string,
384 payload: string,
385 updatedAt: number,
386 ) {
387 return Effect.callback<void, ConnectionTransientError>((resume) => {
388 try {
389 const bytes = new TextEncoder().encode(payload).length;
390 const transaction = database.transaction(QUERY_STORE_NAME, "readwrite");
391 transaction.addEventListener("abort", () => {
392 resume(
393 Effect.fail(
394 catalogError("write", transaction.error ?? m(msg("Unknown IndexedDB write error"))),
395 ),
396 );
397 });
398 transaction.addEventListener("complete", () => {
399 resume(Effect.void);
400 });
401 const store = transaction.objectStore(QUERY_STORE_NAME);
402 // An answer too large to keep must not leave its older copy to paint on the next start.
403 if (bytes > QUERY_MAX_PAYLOAD_BYTES) {
404 store.delete(queryCacheKey(environmentId, key));
405 return;
406 }
407 store.put({ environmentId, payload, updatedAt, bytes }, queryCacheKey(environmentId, key));
408 const batchKey = queryBatchKey(environmentId);
409 const batchRequest = store.get(batchKey);
410 batchRequest.addEventListener("success", () => {
411 const batch: unknown = batchRequest.result;
412 const rows = (isQueryBatch(batch) ? batch.rows : 0) + 1;
413 const written = (isQueryBatch(batch) ? batch.bytes : 0) + bytes;
414 if (
415 isQueryBatch(batch) &&
416 rows < QUERY_PRUNE_BATCH_ROWS &&
417 written < QUERY_PRUNE_BATCH_BYTES
418 ) {
419 store.put({ rows, bytes: written }, batchKey);
420 return;
421 }
422 store.put({ rows: 0, bytes: 0 }, batchKey);
423 const request = store
424 .index(QUERY_UPDATED_INDEX_NAME)
425 .openKeyCursor(IDBKeyRange.bound([environmentId], [environmentId, []]), "prev");
426 let kept = 0;
427 let totalBytes = 0;
428 request.addEventListener("success", () => {
429 const cursor = request.result;
430 if (cursor === null) return;
431 const [, rowUpdatedAt, rowBytes] = cursor.key as [string, number, number];
432 kept += 1;
433 totalBytes += rowBytes;
434 if (
435 kept > QUERY_MAX_ROWS - QUERY_PRUNE_BATCH_ROWS ||
436 totalBytes > QUERY_MAX_BYTES - QUERY_PRUNE_BATCH_BYTES ||
437 rowUpdatedAt < updatedAt - QUERY_MAX_AGE_MS
438 ) {
439 store.delete(cursor.primaryKey);
440 }
441 cursor.continue();
442 });
443 });
444 } catch (cause) {
445 resume(Effect.fail(catalogError("write", cause)));
446 }
447 }).pipe(Effect.withSpan("web.connectionStorage.saveQuery"));
448 }
449
450 function readDatabaseValue(database: DatabaseHandle, storeName: string, key: IDBValidKey) {
451 return withDatabase(database, (opened) => readDatabaseValueOnConnection(opened, storeName, key));
452 }
453
454 function writeDatabaseValue(
455 database: DatabaseHandle,
456 storeName: string,
457 key: IDBValidKey,
458 value: unknown,
459 ) {
460 return withDatabase(database, (opened) =>
461 writeDatabaseValueOnConnection(opened, storeName, key, value),
462 );
463 }
464
465 function removeDatabaseValue(database: DatabaseHandle, storeName: string, key: IDBValidKey) {
466 return withDatabase(database, (opened) =>
467 removeDatabaseValueOnConnection(opened, storeName, key),
468 );
469 }
470
471 function removeDatabaseValuesInRange(
472 database: DatabaseHandle,
473 storeName: string,
474 range: IDBKeyRange,
475 ) {
476 return withDatabase(database, (opened) =>
477 removeDatabaseValuesInRangeOnConnection(opened, storeName, range),
478 );
479 }
480
481 function threadCacheKey(environmentId: EnvironmentId, threadId: ThreadId) {
482 return `${environmentId}:${threadId}`;
483 }
484
485 function queryCacheKey(environmentId: EnvironmentId, key: string) {
486 return `${environmentId}:v1:${key}`;
487 }
488
489 function queryBatchKey(environmentId: EnvironmentId) {
490 return `${environmentId}:batch`;
491 }
492
493 function vcsRefsCacheKey(environmentId: EnvironmentId, cwd: string) {
494 return `${environmentId}:${cwd}`;
495 }
496
497 const decodeCatalog = Effect.fn("web.connectionStorage.decodeCatalog")(function* (raw: string) {
498 return yield* decodeConnectionCatalogDocument(raw).pipe(
499 Effect.mapError((cause) => catalogError("decode", cause)),
500 );
501 });
502
503 const encodeCatalog = Effect.fn("web.connectionStorage.encodeCatalog")(function* (
504 catalog: ConnectionCatalogDocumentType,
505 ) {
506 return yield* encodeConnectionCatalogDocument(catalog).pipe(
507 Effect.mapError((cause) => catalogError("encode", cause)),
508 );
509 });
510
511 export interface CatalogBackend {
512 readonly read: Effect.Effect<string | null, ConnectionTransientError>;
513 readonly write: (raw: string) => Effect.Effect<void, ConnectionTransientError>;
514 readonly quarantine?: (raw: string) => Effect.Effect<void, ConnectionTransientError>;
515 }
516
517 export function makeCatalogBackend(database: DatabaseHandle): CatalogBackend {
518 const bridge = window.desktopBridge;
519 if (bridge !== undefined) {
520 return {
521 read: Effect.tryPromise({
522 try: () => bridge.getConnectionCatalog(),
523 catch: (cause) => catalogError("load", cause),
524 }),
525 write: (raw) =>
526 Effect.tryPromise({
527 try: () => bridge.setConnectionCatalog(raw),
528 catch: (cause) => catalogError("save", cause),
529 }).pipe(
530 Effect.flatMap((stored) =>
531 stored
532 ? Effect.void
533 : Effect.fail(
534 catalogError(
535 "save",
536 m(msg("Desktop secure storage is unavailable in this system context.")),
537 ),
538 ),
539 ),
540 ),
541 };
542 }
543
544 return {
545 read: readDatabaseValue(database, CATALOG_STORE_NAME, CATALOG_KEY).pipe(
546 Effect.map((value) => (typeof value === "string" ? value : null)),
547 ),
548 write: (raw) => writeDatabaseValue(database, CATALOG_STORE_NAME, CATALOG_KEY, raw),
549 quarantine: (raw) =>
550 writeDatabaseValue(database, CATALOG_STORE_NAME, `${CATALOG_KEY}:corrupt:${Date.now()}`, raw),
551 };
552 }
553
554 interface CatalogStore {
555 readonly read: Effect.Effect<ConnectionCatalogDocumentType, ConnectionTransientError>;
556 readonly update: (
557 transform: (catalog: ConnectionCatalogDocumentType) => ConnectionCatalogDocumentType,
558 ) => Effect.Effect<void, ConnectionTransientError>;
559 }
560
561 export const makeCatalogStore = Effect.fn("web.connectionStorage.makeCatalogStore")(function* (
562 backend: CatalogBackend,
563 ) {
564 const state = yield* Ref.make<Option.Option<ConnectionCatalogDocumentType>>(Option.none());
565 const lock = yield* Semaphore.make(1);
566
567 const loadUnlocked = Effect.fn("web.connectionStorage.loadCatalog")(function* () {
568 const cached = yield* Ref.get(state);
569 if (Option.isSome(cached)) {
570 return cached.value;
571 }
572 const raw = yield* backend.read;
573 let catalog = EMPTY_CONNECTION_CATALOG_DOCUMENT;
574 if (raw !== null && raw.trim() !== "") {
575 catalog = yield* decodeCatalog(raw).pipe(
576 Effect.catch((error) =>
577 Effect.gen(function* () {
578 yield* Effect.logWarning("Discarding a corrupt web connection catalog.", {
579 error: error.message,
580 });
581 if (backend.quarantine !== undefined) {
582 yield* backend.quarantine(raw).pipe(
583 Effect.catch((cause) =>
584 Effect.logWarning("Could not quarantine the corrupt web connection catalog.", {
585 error: cause.message,
586 }),
587 ),
588 );
589 }
590 const encoded = yield* encodeCatalog(EMPTY_CONNECTION_CATALOG_DOCUMENT);
591 yield* backend.write(encoded).pipe(
592 Effect.catch((cause) =>
593 Effect.logWarning("Could not persist the recovered web connection catalog.", {
594 error: cause.message,
595 }),
596 ),
597 );
598 return EMPTY_CONNECTION_CATALOG_DOCUMENT;
599 }),
600 ),
601 );
602 }
603 yield* Ref.set(state, Option.some(catalog));
604 return catalog;
605 });
606
607 const read = lock.withPermits(1)(loadUnlocked());
608 const update: CatalogStore["update"] = Effect.fn("web.connectionStorage.updateCatalog")(
609 function* (transform) {
610 yield* lock.withPermits(1)(
611 Effect.gen(function* () {
612 const next = transform(yield* loadUnlocked());
613 yield* backend.write(yield* encodeCatalog(next));
614 yield* Ref.set(state, Option.some(next));
615 }),
616 );
617 },
618 );
619
620 return { read, update } satisfies CatalogStore;
621 });
622
623 // The stored names predate the rename and stay, so a tab still on an older version and an
624 // updated one read and revoke the same grants.
625 const PULL_REQUEST_ROUTING_KEY_PREFIX = "t3code:github-routing:";
626 const PULL_REQUEST_ROUTING_CHANGED = "t3code:github-routing-changed";
627 const isStoredPullRequestRoutingPermission = Schema.is(StoredPullRequestRoutingPermission);
628 const encodeStoredPullRequestRoutingPermission = Schema.encodeSync(
629 Schema.fromJsonString(StoredPullRequestRoutingPermission),
630 );
631
632 /** Each grant has its own key so stale tabs and unrelated catalog saves cannot restore trust. */
633 export function makeBrowserPullRequestRoutingPermissions(
634 browser: Pick<Window, "localStorage"> & EventTarget = window,
635 ) {
636 const read = (key: string): StoredPullRequestRoutingPermission | null => {
637 try {
638 const raw = browser.localStorage.getItem(key);
639 const value: unknown = raw === null ? null : JSON.parse(raw);
640 return isStoredPullRequestRoutingPermission(value) &&
641 key === `${PULL_REQUEST_ROUTING_KEY_PREFIX}${value.environmentId}`
642 ? value
643 : null;
644 } catch {
645 return null;
646 }
647 };
648 const readAll = (): ReadonlyArray<StoredPullRequestRoutingPermission> => {
649 try {
650 const values: StoredPullRequestRoutingPermission[] = [];
651 const storage = browser.localStorage;
652 for (let index = 0; index < storage.length; index++) {
653 const key = storage.key(index);
654 if (key?.startsWith(PULL_REQUEST_ROUTING_KEY_PREFIX)) {
655 const value = read(key);
656 if (value !== null) values.push(value);
657 }
658 }
659 return values;
660 } catch {
661 return [];
662 }
663 };
664 const write = (environmentId: EnvironmentId, value: StoredPullRequestRoutingPermission | null) =>
665 Effect.try({
666 try: () => {
667 const key = `${PULL_REQUEST_ROUTING_KEY_PREFIX}${environmentId}`;
668 if (value === null) browser.localStorage.removeItem(key);
669 else browser.localStorage.setItem(key, encodeStoredPullRequestRoutingPermission(value));
670 browser.dispatchEvent(new Event(PULL_REQUEST_ROUTING_CHANGED));
671 },
672 catch: (cause) => catalogError("save pull request routing permissions in", cause),
673 });
674 return PullRequestRoutingPermissions.of({
675 get: Effect.fn("PullRequestRoutingPermissions.get")(function (entry) {
676 return Effect.sync(() => {
677 const value = read(`${PULL_REQUEST_ROUTING_KEY_PREFIX}${entry.target.environmentId}`);
678 return pullRequestRoutingPermissionFor(entry, value === null ? [] : [value]);
679 });
680 }),
681 changes: Stream.callback<ReadonlyArray<StoredPullRequestRoutingPermission>>((queue) =>
682 Effect.acquireRelease(
683 Effect.sync(() => {
684 const listener = (event: Event) => {
685 if (event.type === "storage") {
686 const key = (event as StorageEvent).key;
687 if (key !== null && !key?.startsWith(PULL_REQUEST_ROUTING_KEY_PREFIX)) return;
688 }
689 Queue.offerUnsafe(queue, readAll());
690 };
691 browser.addEventListener("storage", listener);
692 browser.addEventListener(PULL_REQUEST_ROUTING_CHANGED, listener);
693 Queue.offerUnsafe(queue, readAll());
694 return listener;
695 }),
696 (listener) =>
697 Effect.sync(() => {
698 browser.removeEventListener("storage", listener);
699 browser.removeEventListener(PULL_REQUEST_ROUTING_CHANGED, listener);
700 }),
701 ).pipe(Effect.asVoid),
702 ),
703 set: Effect.fn("PullRequestRoutingPermissions.set")(function* (entry, permission) {
704 const connectionKey = pullRequestRoutingConnectionKey(entry);
705 if (connectionKey === null)
706 return yield* new ConnectionBlockedError({
707 reason: "configuration",
708 detail: m(msg("This environment does not have a saved connection endpoint.")),
709 });
710 return yield* write(
711 entry.target.environmentId,
712 permission === "off"
713 ? null
714 : {
715 environmentId: entry.target.environmentId,
716 connectionKey,
717 permission,
718 },
719 );
720 }),
721 forget: Effect.fn("PullRequestRoutingPermissions.forget")(function (environmentId) {
722 return write(environmentId, null);
723 }),
724 });
725 }
726
727 // MessageChannel yields without nested-timer clamping or background throttling.
728 const yieldToBrowserTask = Effect.callback<void>((resume) => {
729 const { port1, port2 } = new MessageChannel();
730 port1.addEventListener(
731 "message",
732 () => {
733 port1.close();
734 resume(Effect.void);
735 },
736 { once: true },
737 );
738 port1.start();
739 port2.postMessage(null);
740 // A closed port delivers nothing, so interruption needs no listener bookkeeping.
741 return Effect.sync(() => port1.close());
742 });
743
744 export const layer = Layer.effectContext(
745 Effect.gen(function* () {
746 const { handle: database } = yield* Effect.acquireRelease(
747 makeDatabaseHandle(),
748 (owned) => owned.close,
749 );
750 const catalog = yield* makeCatalogStore(makeCatalogBackend(database));
751 const pullRequestRoutingPermissions = makeBrowserPullRequestRoutingPermissions();
752 // Remembers each thread row's encoding, so a save only encodes rows that changed.
753 const encodeStoredShellSnapshot = makeStoredShellSnapshotEncoder({
754 yieldToHost: yieldToBrowserTask,
755 });
756 const encodeStoredThreadSnapshot = makeStoredThreadSnapshotEncoder({
757 format: "full",
758 yieldToHost: yieldToBrowserTask,
759 });
760 const threadWriteOrder = makeThreadCacheWriteOrder();
761
762 const targetStore = Persistence.ConnectionTargetStore.of({
763 list: catalog.read.pipe(
764 Effect.map((document) => document.targets),
765 Effect.mapError((cause) => persistenceError("list-targets", cause)),
766 ),
767 listDisabled: catalog.read.pipe(
768 Effect.map((document) => document.disabledEnvironmentIds),
769 Effect.mapError((cause) => persistenceError("list-disabled-targets", cause)),
770 ),
771 });
772 const registrationStore = Persistence.ConnectionRegistrationStore.of({
773 register: Effect.fn("Persistence.ConnectionRegistrationStore.register")(
774 function (registration, routes) {
775 return catalog
776 .update((document) => registerConnectionInCatalog(document, registration, routes))
777 .pipe(Effect.mapError((cause) => persistenceError("register-connection", cause)));
778 },
779 ),
780 setRoutes: Effect.fn("Persistence.ConnectionRegistrationStore.setRoutes")(
781 function (environmentId, routes) {
782 return catalog
783 .update((document) => setRoutesInCatalog(document, environmentId, routes))
784 .pipe(Effect.mapError((cause) => persistenceError("set-connection-routes", cause)));
785 },
786 ),
787 remove: Effect.fn("Persistence.ConnectionRegistrationStore.remove")(function (environmentId) {
788 return catalog
789 .update((document) => removeConnectionFromCatalog(document, environmentId))
790 .pipe(Effect.mapError((cause) => persistenceError("remove-connection", cause)));
791 }),
792 setEnabled: Effect.fn("Persistence.ConnectionRegistrationStore.setEnabled")(
793 function (environmentId, enabled) {
794 return catalog
795 .update((document) => setConnectionEnabledInCatalog(document, environmentId, enabled))
796 .pipe(Effect.mapError((cause) => persistenceError("set-connection-enabled", cause)));
797 },
798 ),
799 });
800 const profileStore = ProfileStore.make({
801 get: (connectionId) =>
802 catalog.read.pipe(
803 Effect.map((document) =>
804 Option.fromUndefinedOr(
805 document.profiles.find((profile) => profile.connectionId === connectionId),
806 ),
807 ),
808 ),
809 put: (profile) =>
810 catalog.update((document) => ({
811 ...document,
812 profiles: replaceCatalogValue(document.profiles, (value) => value.connectionId, profile),
813 })),
814 remove: (connectionId) =>
815 catalog.update((document) => ({
816 ...document,
817 profiles: removeCatalogValue(
818 document.profiles,
819 (value) => value.connectionId,
820 connectionId,
821 ),
822 })),
823 });
824 const credentialStore = CredentialStore.make({
825 get: (connectionId) =>
826 catalog.read.pipe(
827 Effect.map((document) =>
828 Option.fromUndefinedOr(
829 document.credentials.find((entry) => entry.connectionId === connectionId)?.credential,
830 ),
831 ),
832 ),
833 put: (connectionId, credential) =>
834 catalog.update((document) => ({
835 ...document,
836 credentials: replaceCatalogValue(document.credentials, (value) => value.connectionId, {
837 connectionId,
838 credential,
839 }),
840 })),
841 remove: (connectionId) =>
842 catalog.update((document) => ({
843 ...document,
844 credentials: removeCatalogValue(
845 document.credentials,
846 (value) => value.connectionId,
847 connectionId,
848 ),
849 })),
850 });
851 const remoteTokenStore = TokenStore.make({
852 get: (environmentId) =>
853 catalog.read.pipe(
854 Effect.map((document) =>
855 Option.fromUndefinedOr(
856 document.remoteDpopTokens.find((token) => token.environmentId === environmentId),
857 ),
858 ),
859 ),
860 put: (token) => catalog.update((document) => putRemoteDpopTokenInCatalog(document, token)),
861 remove: (environmentId) =>
862 catalog.update((document) => ({
863 ...document,
864 remoteDpopTokens: removeCatalogValue(
865 document.remoteDpopTokens,
866 (value) => value.environmentId,
867 environmentId,
868 ),
869 })),
870 });
871 const cacheStore = Persistence.EnvironmentCacheStore.of({
872 loadShell: Effect.fn("Persistence.EnvironmentCacheStore.loadShell")(function (environmentId) {
873 return readDatabaseValue(database, SHELL_STORE_NAME, environmentId).pipe(
874 Effect.tap(() => Effect.promise(() => projectFaviconCache.hydrate())),
875 Effect.flatMap((raw) => {
876 if (typeof raw !== "string") {
877 return Effect.succeedNone;
878 }
879 return decodeOrDiscardOrchestrationCache(
880 decodeStoredShellSnapshot(raw).pipe(
881 Effect.mapError((cause) => persistenceError("load-shell", cause)),
882 Effect.map((stored) =>
883 stored.environmentId === environmentId
884 ? Option.some(
885 stored.origin === undefined
886 ? stored.snapshot
887 : { ...stored.snapshot, origin: stored.origin },
888 )
889 : Option.none(),
890 ),
891 ),
892 removeDatabaseValue(database, SHELL_STORE_NAME, environmentId),
893 );
894 }),
895 Effect.mapError((cause) => persistenceError("load-shell", cause)),
896 );
897 }),
898 saveShell: Effect.fn("storage.saveShell")(
899 function* (environmentId, snapshot, origin) {
900 const encoded = yield* encodeStoredShellSnapshot({
901 schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
902 environmentId,
903 snapshot,
904 ...(origin === undefined ? {} : { origin }),
905 }).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
906 yield* writeDatabaseValue(database, SHELL_STORE_NAME, environmentId, encoded);
907 },
908 Effect.mapError((cause) =>
909 cause._tag === "ConnectionPersistenceError"
910 ? cause
911 : persistenceError("save-shell", cause),
912 ),
913 ),
914 loadServerConfig: Effect.fn("Persistence.EnvironmentCacheStore.loadServerConfig")(
915 function (environmentId) {
916 return readDatabaseValue(database, SERVER_CONFIG_STORE_NAME, environmentId).pipe(
917 Effect.flatMap((raw) => {
918 if (typeof raw !== "string") {
919 return Effect.succeedNone;
920 }
921 return decodeStoredServerConfig(raw).pipe(
922 Effect.mapError((cause) => persistenceError("load-server-config", cause)),
923 Effect.map((stored) =>
924 stored.environmentId === environmentId
925 ? Option.some(stored.config)
926 : Option.none(),
927 ),
928 );
929 }),
930 Effect.mapError((cause) =>
931 cause._tag === "ConnectionPersistenceError"
932 ? cause
933 : persistenceError("load-server-config", cause),
934 ),
935 );
936 },
937 ),
938 saveServerConfig: Effect.fn("storage.saveServerConfig")(
939 function* (environmentId, config) {
940 const encoded = yield* encodeStoredServerConfig({
941 schemaVersion: 1,
942 environmentId,
943 config,
944 }).pipe(Effect.mapError((cause) => persistenceError("save-server-config", cause)));
945 yield* writeDatabaseValue(database, SERVER_CONFIG_STORE_NAME, environmentId, encoded);
946 },
947 Effect.mapError((cause) =>
948 cause._tag === "ConnectionPersistenceError"
949 ? cause
950 : persistenceError("save-server-config", cause),
951 ),
952 ),
953 loadThread: Effect.fn("Persistence.EnvironmentCacheStore.loadThread")(
954 function (environmentId, threadId) {
955 return readDatabaseValue(
956 database,
957 THREAD_STORE_NAME,
958 threadCacheKey(environmentId, threadId),
959 ).pipe(
960 Effect.flatMap((raw) => {
961 if (typeof raw !== "string") {
962 return Effect.succeedNone;
963 }
964 return decodeOrDiscardOrchestrationCache(
965 decodeStoredThreadSnapshot(raw).pipe(
966 Effect.mapError((cause) => persistenceError("load-thread", cause)),
967 Effect.map((stored) =>
968 stored.environmentId === environmentId && stored.threadId === threadId
969 ? Option.some(stored.snapshot)
970 : Option.none(),
971 ),
972 ),
973 removeDatabaseValue(
974 database,
975 THREAD_STORE_NAME,
976 threadCacheKey(environmentId, threadId),
977 ),
978 );
979 }),
980 Effect.mapError((cause) => persistenceError("load-thread", cause)),
981 );
982 },
983 ),
984 saveThread: Effect.fn("storage.saveThread")(
985 function* (environmentId, snapshot) {
986 const key = threadCacheKey(environmentId, snapshot.projection.thread.id);
987 const isLatest = threadWriteOrder.beginSave(environmentId, snapshot.projection.thread.id);
988 const encoded = yield* encodeStoredThreadSnapshot({
989 environmentId,
990 threadId: snapshot.projection.thread.id,
991 snapshot,
992 }).pipe(Effect.mapError((cause) => persistenceError("save-thread", cause)));
993 if (!isLatest()) return;
994 // An older copy of a thread now too large to keep would paint stale on reopen.
995 yield* Option.match(encoded, {
996 onNone: () => removeDatabaseValue(database, THREAD_STORE_NAME, key),
997 onSome: (payload) => writeDatabaseValue(database, THREAD_STORE_NAME, key, payload),
998 });
999 },
1000 Effect.mapError((cause) =>
1001 cause._tag === "ConnectionPersistenceError"
1002 ? cause
1003 : persistenceError("save-thread", cause),
1004 ),
1005 ),
1006 loadVcsRefs: Effect.fn("Persistence.EnvironmentCacheStore.loadVcsRefs")(
1007 function (environmentId, cwd) {
1008 return readDatabaseValue(
1009 database,
1010 VCS_REFS_STORE_NAME,
1011 vcsRefsCacheKey(environmentId, cwd),
1012 ).pipe(
1013 Effect.flatMap((raw) => {
1014 if (typeof raw !== "string") {
1015 return Effect.succeedNone;
1016 }
1017 return decodeStoredVcsRefs(raw).pipe(
1018 Effect.mapError((cause) => persistenceError("load-vcs-refs", cause)),
1019 Effect.map((stored) =>
1020 stored.environmentId === environmentId && stored.cwd === cwd
1021 ? Option.some(stored.refs)
1022 : Option.none(),
1023 ),
1024 );
1025 }),
1026 Effect.mapError((cause) =>
1027 cause._tag === "ConnectionPersistenceError"
1028 ? cause
1029 : persistenceError("load-vcs-refs", cause),
1030 ),
1031 );
1032 },
1033 ),
1034 saveVcsRefs: Effect.fn("storage.saveVcsRefs")(
1035 function* (environmentId, cwd, refs) {
1036 const encoded = yield* encodeStoredVcsRefs({
1037 schemaVersion: 1,
1038 environmentId,
1039 cwd,
1040 refs,
1041 }).pipe(Effect.mapError((cause) => persistenceError("save-vcs-refs", cause)));
1042 yield* writeDatabaseValue(
1043 database,
1044 VCS_REFS_STORE_NAME,
1045 vcsRefsCacheKey(environmentId, cwd),
1046 encoded,
1047 );
1048 },
1049 Effect.mapError((cause) =>
1050 cause._tag === "ConnectionPersistenceError"
1051 ? cause
1052 : persistenceError("save-vcs-refs", cause),
1053 ),
1054 ),
1055 removeVcsRefs: Effect.fn("Persistence.EnvironmentCacheStore.removeVcsRefs")(
1056 function (environmentId, cwd) {
1057 return removeDatabaseValue(
1058 database,
1059 VCS_REFS_STORE_NAME,
1060 vcsRefsCacheKey(environmentId, cwd),
1061 ).pipe(Effect.mapError((cause) => persistenceError("remove-vcs-refs", cause)));
1062 },
1063 ),
1064 clearVcsRefs: Effect.fn("Persistence.EnvironmentCacheStore.clearVcsRefs")(
1065 function (environmentId) {
1066 return removeDatabaseValuesInRange(
1067 database,
1068 VCS_REFS_STORE_NAME,
1069 IDBKeyRange.bound(`${environmentId}:`, `${environmentId}:\uffff`),
1070 ).pipe(Effect.mapError((cause) => persistenceError("clear-vcs-refs", cause)));
1071 },
1072 ),
1073 removeThread: Effect.fn("Persistence.EnvironmentCacheStore.removeThread")(
1074 function (environmentId, threadId) {
1075 threadWriteOrder.removeThread(environmentId, threadId);
1076 return removeDatabaseValue(
1077 database,
1078 THREAD_STORE_NAME,
1079 threadCacheKey(environmentId, threadId),
1080 ).pipe(Effect.mapError((cause) => persistenceError("remove-thread", cause)));
1081 },
1082 ),
1083 clear: Effect.fn("Persistence.EnvironmentCacheStore.clear")(function (environmentId) {
1084 threadWriteOrder.clearEnvironment(environmentId);
1085 return Effect.all(
1086 [
1087 Effect.promise(() => projectFaviconCache.clearEnvironment(environmentId)),
1088 removeDatabaseValue(database, SHELL_STORE_NAME, environmentId),
1089 removeDatabaseValuesInRange(
1090 database,
1091 THREAD_STORE_NAME,
1092 IDBKeyRange.bound(`${environmentId}:`, `${environmentId}:\uffff`),
1093 ),
1094 removeDatabaseValue(database, SERVER_CONFIG_STORE_NAME, environmentId),
1095 removeDatabaseValuesInRange(
1096 database,
1097 VCS_REFS_STORE_NAME,
1098 IDBKeyRange.bound(`${environmentId}:`, `${environmentId}:\uffff`),
1099 ),
1100 removeDatabaseValuesInRange(
1101 database,
1102 QUERY_STORE_NAME,
1103 IDBKeyRange.bound(`${environmentId}:`, `${environmentId}:\uffff`),
1104 ),
1105 ],
1106 { concurrency: "unbounded", discard: true },
1107 ).pipe(Effect.mapError((cause) => persistenceError("clear-environment", cause)));
1108 }),
1109 });
1110
1111 const queryStore: NonNullable<typeof EnvironmentQueryStore.Service> = {
1112 load: Effect.fn("EnvironmentQueryStore.load")(
1113 function* (environmentId, key) {
1114 const stored = yield* readDatabaseValue(
1115 database,
1116 QUERY_STORE_NAME,
1117 queryCacheKey(environmentId, key),
1118 );
1119 const now = yield* Clock.currentTimeMillis;
1120 return isStoredQuery(stored) &&
1121 stored.environmentId === environmentId &&
1122 stored.updatedAt >= now - QUERY_MAX_AGE_MS
1123 ? Option.some(stored.payload)
1124 : Option.none();
1125 },
1126 Effect.catch(() => Effect.succeedNone),
1127 ),
1128 save: (environmentId, key, payload) =>
1129 Clock.currentTimeMillis.pipe(
1130 Effect.flatMap((now) =>
1131 withDatabase(database, (opened) =>
1132 saveQueryOnConnection(opened, environmentId, key, payload, now),
1133 ),
1134 ),
1135 Effect.catch(() => Effect.logWarning("Offline query database save failed")),
1136 ),
1137 };
1138
1139 return Context.make(Persistence.ConnectionTargetStore, targetStore).pipe(
1140 Context.add(PullRequestRoutingPermissions, pullRequestRoutingPermissions),
1141 Context.add(Persistence.ConnectionRegistrationStore, registrationStore),
1142 Context.add(ProfileStore.ConnectionProfileStore, profileStore),
1143 Context.add(CredentialStore.ConnectionCredentialStore, credentialStore),
1144 Context.add(TokenStore.RemoteDpopAccessTokenStore, remoteTokenStore),
1145 Context.add(Persistence.EnvironmentCacheStore, cacheStore),
1146 Context.add(EnvironmentQueryStore, queryStore),
1147 );
1148 }),
1149 );