(
integrationRow: IntegrationRow,
ref: ConnectionRef,
mode: "explicit" | "background" = "explicit",
)
| 2070 | result.incompleteReason ?? "plugin returned an incomplete tool catalog"; |
| 2071 | |
| 2072 | const produceConnectionTools = ( |
| 2073 | integrationRow: IntegrationRow, |
| 2074 | ref: ConnectionRef, |
| 2075 | mode: "explicit" | "background" = "explicit", |
| 2076 | ): Effect.Effect<readonly Tool[], IntegrationNotFoundError | StorageFailure> => |
| 2077 | Effect.gen(function* () { |
| 2078 | const runtime = runtimes.get(integrationRow.plugin_id); |
| 2079 | const keys = yield* Effect.try({ |
| 2080 | try: () => ownedKeys(ref.owner), |
| 2081 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 2082 | }); |
| 2083 | const owner = ref.owner; |
| 2084 | const where = (b: AnyCb) => |
| 2085 | b.and( |
| 2086 | byOwner(owner)(b), |
| 2087 | b("integration", "=", String(ref.integration)), |
| 2088 | b("connection", "=", String(ref.name)), |
| 2089 | ); |
| 2090 | const connectionWhere = (b: AnyCb) => |
| 2091 | b.and( |
| 2092 | byOwner(owner)(b), |
| 2093 | b("integration", "=", String(ref.integration)), |
| 2094 | b("name", "=", String(ref.name)), |
| 2095 | ); |
| 2096 | const isToolSyncHealth = (health: HealthCheckResult | null): boolean => |
| 2097 | health?.detail?.startsWith(toolSyncHealthDetailPrefix) === true; |
| 2098 | const syncedSet = (row: ConnectionRow | null) => { |
| 2099 | const health = row ? Option.getOrNull(decodeLastHealth(row.last_health)) : null; |
| 2100 | return isToolSyncHealth(health) |
| 2101 | ? { tools_synced_at: Date.now(), last_health: null, updated_at: new Date() } |
| 2102 | : { tools_synced_at: Date.now() }; |
| 2103 | }; |
| 2104 | // Every exit stamps the sync time — including the cleanup paths that |
| 2105 | // produce zero tools — so the stale-catalog check (`config_revised_at` |
| 2106 | // vs `tools_synced_at`) doesn't re-attempt this connection per read. |
| 2107 | // Successful syncs also clear stale sync-failure health records, while |
| 2108 | // preserving genuine health-check outcomes. |
| 2109 | const stampSynced = (row: ConnectionRow | null) => |
| 2110 | core.updateMany("connection", { |
| 2111 | where: connectionWhere, |
| 2112 | set: syncedSet(row), |
| 2113 | }); |
| 2114 | const stampSyncedWithHealth = (reason: string) => |
| 2115 | core.updateMany("connection", { |
| 2116 | where: connectionWhere, |
| 2117 | set: { |
| 2118 | tools_synced_at: Date.now(), |
| 2119 | last_health: toolSyncHealth(reason), |
| 2120 | updated_at: new Date(), |
| 2121 | }, |
| 2122 | }); |
| 2123 | |
| 2124 | // Defense in depth (and cleanup for rows created before the create-time |
| 2125 | // guard, or emptied by an external edit): a credentialed non-OAuth |
| 2126 | // connection with no bound credential inputs can never resolve a value, |
| 2127 | // so never advertise tools for it — every call would fail with |
| 2128 | // `connection_value_missing`. OAuth connections resolve via refresh and |
| 2129 | // carry their token outside `item_ids`; no-auth (`"none"` template) |
no test coverage detected