(
integrationRow: IntegrationRow,
ref: ConnectionRef,
mode: "explicit" | "background" = "explicit",
)
| 2412 | result.incompleteReason ?? "plugin returned an incomplete tool catalog"; |
| 2413 | |
| 2414 | const produceConnectionTools = ( |
| 2415 | integrationRow: IntegrationRow, |
| 2416 | ref: ConnectionRef, |
| 2417 | mode: "explicit" | "background" = "explicit", |
| 2418 | ): Effect.Effect<readonly Tool[], IntegrationNotFoundError | StorageFailure> => |
| 2419 | Effect.gen(function* () { |
| 2420 | const runtime = runtimes.get(integrationRow.plugin_id); |
| 2421 | const keys = yield* Effect.try({ |
| 2422 | try: () => ownedKeys(ref.owner), |
| 2423 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 2424 | }); |
| 2425 | const owner = ref.owner; |
| 2426 | const where = (b: AnyCb) => |
| 2427 | b.and( |
| 2428 | byOwner(owner)(b), |
| 2429 | b("integration", "=", String(ref.integration)), |
| 2430 | b("connection", "=", String(ref.name)), |
| 2431 | ); |
| 2432 | const connectionWhere = (b: AnyCb) => |
| 2433 | b.and( |
| 2434 | byOwner(owner)(b), |
| 2435 | b("integration", "=", String(ref.integration)), |
| 2436 | b("name", "=", String(ref.name)), |
| 2437 | ); |
| 2438 | const isToolSyncHealth = (health: HealthCheckResult | null): boolean => |
| 2439 | health?.detail?.startsWith(toolSyncHealthDetailPrefix) === true; |
| 2440 | const syncedSet = (row: ConnectionRow | null) => { |
| 2441 | const health = row ? Option.getOrNull(decodeLastHealth(row.last_health)) : null; |
| 2442 | return isToolSyncHealth(health) |
| 2443 | ? { tools_synced_at: Date.now(), last_health: null, updated_at: new Date() } |
| 2444 | : { tools_synced_at: Date.now() }; |
| 2445 | }; |
| 2446 | // Every exit stamps the sync time — including the cleanup paths that |
| 2447 | // produce zero tools — so the stale-catalog check (`config_revised_at` |
| 2448 | // vs `tools_synced_at`) doesn't re-attempt this connection per read. |
| 2449 | // Successful syncs also clear stale sync-failure health records, while |
| 2450 | // preserving genuine health-check outcomes. |
| 2451 | const stampSynced = (row: ConnectionRow | null) => |
| 2452 | core.updateMany("connection", { |
| 2453 | where: connectionWhere, |
| 2454 | set: syncedSet(row), |
| 2455 | }); |
| 2456 | const stampSyncedWithHealth = (reason: string) => |
| 2457 | core.updateMany("connection", { |
| 2458 | where: connectionWhere, |
| 2459 | set: { |
| 2460 | tools_synced_at: Date.now(), |
| 2461 | last_health: toolSyncHealth(reason), |
| 2462 | updated_at: new Date(), |
| 2463 | }, |
| 2464 | }); |
| 2465 | |
| 2466 | // Defense in depth (and cleanup for rows created before the create-time |
| 2467 | // guard, or emptied by an external edit): a credentialed non-OAuth |
| 2468 | // connection with no bound credential inputs can never resolve a value, |
| 2469 | // so never advertise tools for it — every call would fail with |
| 2470 | // `connection_value_missing`. OAuth connections resolve via refresh and |
| 2471 | // carry their token outside `item_ids`; no-auth (`"none"` template) |
no test coverage detected