(
integrationRow: IntegrationRow,
ref: ConnectionRef,
mode: "explicit" | "background" = "explicit",
)
| 2145 | result.incompleteReason ?? "plugin returned an incomplete tool catalog"; |
| 2146 | |
| 2147 | const produceConnectionTools = ( |
| 2148 | integrationRow: IntegrationRow, |
| 2149 | ref: ConnectionRef, |
| 2150 | mode: "explicit" | "background" = "explicit", |
| 2151 | ): Effect.Effect<readonly Tool[], IntegrationNotFoundError | StorageFailure> => |
| 2152 | Effect.gen(function* () { |
| 2153 | const runtime = runtimes.get(integrationRow.plugin_id); |
| 2154 | const keys = yield* Effect.try({ |
| 2155 | try: () => ownedKeys(ref.owner), |
| 2156 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 2157 | }); |
| 2158 | const owner = ref.owner; |
| 2159 | const where = (b: AnyCb) => |
| 2160 | b.and( |
| 2161 | byOwner(owner)(b), |
| 2162 | b("integration", "=", String(ref.integration)), |
| 2163 | b("connection", "=", String(ref.name)), |
| 2164 | ); |
| 2165 | const connectionWhere = (b: AnyCb) => |
| 2166 | b.and( |
| 2167 | byOwner(owner)(b), |
| 2168 | b("integration", "=", String(ref.integration)), |
| 2169 | b("name", "=", String(ref.name)), |
| 2170 | ); |
| 2171 | const isToolSyncHealth = (health: HealthCheckResult | null): boolean => |
| 2172 | health?.detail?.startsWith(toolSyncHealthDetailPrefix) === true; |
| 2173 | const syncedSet = (row: ConnectionRow | null) => { |
| 2174 | const health = row ? Option.getOrNull(decodeLastHealth(row.last_health)) : null; |
| 2175 | return isToolSyncHealth(health) |
| 2176 | ? { tools_synced_at: Date.now(), last_health: null, updated_at: new Date() } |
| 2177 | : { tools_synced_at: Date.now() }; |
| 2178 | }; |
| 2179 | // Every exit stamps the sync time — including the cleanup paths that |
| 2180 | // produce zero tools — so the stale-catalog check (`config_revised_at` |
| 2181 | // vs `tools_synced_at`) doesn't re-attempt this connection per read. |
| 2182 | // Successful syncs also clear stale sync-failure health records, while |
| 2183 | // preserving genuine health-check outcomes. |
| 2184 | const stampSynced = (row: ConnectionRow | null) => |
| 2185 | core.updateMany("connection", { |
| 2186 | where: connectionWhere, |
| 2187 | set: syncedSet(row), |
| 2188 | }); |
| 2189 | const stampSyncedWithHealth = (reason: string) => |
| 2190 | core.updateMany("connection", { |
| 2191 | where: connectionWhere, |
| 2192 | set: { |
| 2193 | tools_synced_at: Date.now(), |
| 2194 | last_health: toolSyncHealth(reason), |
| 2195 | updated_at: new Date(), |
| 2196 | }, |
| 2197 | }); |
| 2198 | |
| 2199 | // Defense in depth (and cleanup for rows created before the create-time |
| 2200 | // guard, or emptied by an external edit): a credentialed non-OAuth |
| 2201 | // connection with no bound credential inputs can never resolve a value, |
| 2202 | // so never advertise tools for it — every call would fail with |
| 2203 | // `connection_value_missing`. OAuth connections resolve via refresh and |
| 2204 | // carry their token outside `item_ids`; no-auth (`"none"` template) |
no test coverage detected