| 5544 | // Best-effort: a failed rebuild leaves the stale-but-working catalog in |
| 5545 | // place and retries on the next read. |
| 5546 | const syncStaleConnectionTools = (mode: "converge" | "bounded") => |
| 5547 | Effect.gen(function* () { |
| 5548 | // The platform view can never persist a rebuilt catalog (writes are |
| 5549 | // denied at the storage boundary), so attempting the sync would only |
| 5550 | // fire upstream `resolveTools` calls whose results are thrown away — |
| 5551 | // network side effects on a read-only credential. Skip it entirely: |
| 5552 | // read-only-ness of the platform read path is a stated invariant here, |
| 5553 | // not an accident of the best-effort catch below. |
| 5554 | if (config.platformView === true) return; |
| 5555 | const integrations = yield* core.findMany("integration", {}); |
| 5556 | if (integrations.length === 0) return; |
| 5557 | const integrationBySlug = new Map(integrations.map((row) => [row.slug, row] as const)); |
| 5558 | // The TTL only matters when a loaded plugin actually lists a live remote |
| 5559 | // catalog; otherwise skip it so age alone never widens the stale query. |
| 5560 | const anyRemoteCatalog = Array.from(runtimes.values()).some( |
| 5561 | (runtime) => runtime.plugin.remoteToolCatalog === true, |
| 5562 | ); |
| 5563 | const cutoff = |
| 5564 | toolsSyncTtlMs == null || !anyRemoteCatalog ? null : Date.now() - toolsSyncTtlMs; |
| 5565 | |
| 5566 | // Bound the scan to potentially-stale rows: stale-marked (NULL stamp) or |
| 5567 | // synced before the latest instant any trigger could fire at (the TTL |
| 5568 | // cutoff / the newest config revision). Per-row trigger checks below |
| 5569 | // re-verify against each row's own integration; in steady state this |
| 5570 | // query returns nothing and the read pays one indexed lookup. |
| 5571 | const latestRevision = integrations.reduce<number | null>( |
| 5572 | (max, row) => |
| 5573 | row.config_revised_at == null |
| 5574 | ? max |
| 5575 | : Math.max(max ?? Number(row.config_revised_at), Number(row.config_revised_at)), |
| 5576 | null, |
| 5577 | ); |
| 5578 | const staleBefore = |
| 5579 | cutoff === null && latestRevision === null |
| 5580 | ? null |
| 5581 | : Math.max( |
| 5582 | cutoff ?? Number.MIN_SAFE_INTEGER, |
| 5583 | latestRevision ?? Number.MIN_SAFE_INTEGER, |
| 5584 | ); |
| 5585 | |
| 5586 | const connections = yield* core.findMany("connection", { |
| 5587 | where: (b: AnyCb) => |
| 5588 | staleBefore === null |
| 5589 | ? b.isNull("tools_synced_at") |
| 5590 | : b.or(b.isNull("tools_synced_at"), b("tools_synced_at", "<", staleBefore)), |
| 5591 | }); |
| 5592 | // Each rebuild is an independent upstream listing, so they run together |
| 5593 | // rather than one after another: a host with many stale remote-catalog |
| 5594 | // connections otherwise pays the sum of every server's latency on the |
| 5595 | // read that trips the TTL. Only the listings overlap — `persistCatalog` |
| 5596 | // keeps the catalog writes in a single-file queue, so this fan-out never |
| 5597 | // opens two transactions on a one-connection database. |
| 5598 | // |
| 5599 | // Two urgency classes. A stale-MARKED or config-revised catalog is known |
| 5600 | // wrong (the upstream said so, or the integration's config changed), so |
| 5601 | // the read waits for it within the grace budget. A catalog that is only |
| 5602 | // older than the TTL is stale-but-working: in bounded mode its rebuild |
| 5603 | // runs entirely in the background and the read answers from the |