@k2b/cloud 0.47.0 → 0.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (240) hide show
  1. package/package.json +3 -3
  2. package/src/_internal/app-access-manager-sql.ts +10 -0
  3. package/src/_internal/app-access-resources.ts +2 -1
  4. package/src/_internal/app-capabilities.ts +73 -0
  5. package/src/_internal/app-event-catalog.ts +1 -1
  6. package/src/_internal/app-queries.ts +1 -1
  7. package/src/_internal/application-package.ts +2 -1
  8. package/src/_internal/bounded-json.ts +7 -1
  9. package/src/_internal/capabilities.ts +25 -2
  10. package/src/_internal/define-app.ts +33 -2
  11. package/src/_internal/heartbeat.ts +2 -0
  12. package/src/_internal/nats-connection.ts +10 -9
  13. package/src/_internal/process-sync.ts +6 -6
  14. package/src/_internal/public-surface.ts +76 -0
  15. package/src/_internal/purpose-name.ts +7 -0
  16. package/src/_internal/registry-validation.ts +1 -0
  17. package/src/_internal/registry.ts +49 -3
  18. package/src/_internal/workload-credential.ts +2 -1
  19. package/src/ai/app-skill-store.ts +2 -1
  20. package/src/ai/app-skills.ts +2 -1
  21. package/src/ai/approvals.ts +2 -1
  22. package/src/ai/chat-tasks.ts +2 -1
  23. package/src/ai/credentials.ts +2 -1
  24. package/src/ai/dictation-runtime.ts +1 -1
  25. package/src/ai/dictations.ts +1 -1
  26. package/src/ai/files-store.ts +2 -1
  27. package/src/ai/inference-calls.ts +2 -1
  28. package/src/ai/map-store.ts +1 -1
  29. package/src/ai/map-tool.ts +1 -1
  30. package/src/ai/memories.ts +1 -1
  31. package/src/ai/memory-learning-runs.ts +1 -1
  32. package/src/ai/memory-learning.ts +1 -1
  33. package/src/ai/memory-workflow-evidence.ts +1 -1
  34. package/src/ai/message-queue.ts +2 -1
  35. package/src/ai/migrate.ts +1 -1
  36. package/src/ai/model-access-migrate.ts +1 -1
  37. package/src/ai/model-access.ts +1 -1
  38. package/src/ai/model-catalog.ts +1 -1
  39. package/src/ai/model-delete.ts +1 -1
  40. package/src/ai/model-usage.ts +2 -1
  41. package/src/ai/open-questions.ts +1 -1
  42. package/src/ai/openai-api-slots.ts +2 -1
  43. package/src/ai/prefs.ts +2 -1
  44. package/src/ai/projects.ts +2 -1
  45. package/src/ai/provider-connections.ts +1 -1
  46. package/src/ai/quota-limits.ts +2 -1
  47. package/src/ai/quota-provider.ts +2 -1
  48. package/src/ai/quota-report.ts +2 -1
  49. package/src/ai/quotas-migrate.ts +2 -1
  50. package/src/ai/quotas.ts +2 -1
  51. package/src/ai/request-headers.ts +2 -1
  52. package/src/ai/review-content.ts +2 -1
  53. package/src/ai/skill-search.ts +2 -1
  54. package/src/ai/skills.ts +2 -1
  55. package/src/ai/store.ts +2 -1
  56. package/src/ai/structured-runs.ts +2 -1
  57. package/src/ai/tool-audit.ts +2 -1
  58. package/src/ai/turn-timing.ts +1 -1
  59. package/src/ai/usage.ts +2 -1
  60. package/src/ai/web-provenance.ts +2 -1
  61. package/src/api/admin-ai-quotas.ts +1 -1
  62. package/src/api/admin-ai.ts +1 -1
  63. package/src/api/admin-app-domains.ts +62 -0
  64. package/src/api/admin-app-keys.ts +47 -3
  65. package/src/api/admin-core-settings.ts +1 -1
  66. package/src/api/admin-outgoing-mail.ts +1 -1
  67. package/src/api/app-bootstrap.ts +153 -0
  68. package/src/api/app-events.ts +1 -1
  69. package/src/api/auth.ts +1 -1
  70. package/src/api/authenticate-app-request.ts +129 -0
  71. package/src/api/capabilities.ts +87 -6
  72. package/src/api/index.ts +7 -0
  73. package/src/api/me.ts +1 -1
  74. package/src/api/search.bench.ts +1 -1
  75. package/src/app-pwa/service.ts +2 -1
  76. package/src/app-pwa/transaction.ts +4 -2
  77. package/src/capabilities/claims.ts +2 -1
  78. package/src/capabilities/executions.ts +8 -4
  79. package/src/capabilities/geo-client.ts +1 -1
  80. package/src/capabilities/migrate.ts +3 -1
  81. package/src/capabilities/resource-readers.ts +2 -2
  82. package/src/capabilities/response.ts +2 -1
  83. package/src/capabilities/server-invoke.ts +73 -20
  84. package/src/capabilities/server.ts +2 -1
  85. package/src/cli/admin/app-domains.ts +140 -0
  86. package/src/cli/admin/app-keys.ts +25 -3
  87. package/src/cli/admin/index.ts +107 -88
  88. package/src/config/env.ts +10 -4
  89. package/src/contracts/app-domains.ts +91 -0
  90. package/src/contracts/app-status.ts +104 -0
  91. package/src/contracts/capabilities.ts +37 -2
  92. package/src/contracts/index.ts +1 -0
  93. package/src/contracts/outgoing-mail.ts +1 -1
  94. package/src/contracts/registry.ts +2 -0
  95. package/src/events/app-event-delivery.ts +1 -1
  96. package/src/events/live-delivery.ts +2 -1
  97. package/src/index.ts +1 -0
  98. package/src/runtime/bootstrap-client.ts +195 -0
  99. package/src/runtime/bootstrap-protocol.ts +67 -0
  100. package/src/runtime/clients.ts +6 -2
  101. package/src/runtime/connections.ts +9 -0
  102. package/src/server/app-key.ts +14 -0
  103. package/src/server/index.ts +2 -0
  104. package/src/server/ratelimit.ts +1 -1
  105. package/src/server/services/access.ts +2 -1
  106. package/src/services/account-category-policy.ts +1 -1
  107. package/src/services/account-lifecycle/audit.ts +1 -1
  108. package/src/services/account-lifecycle/index.ts +1 -1
  109. package/src/services/account-lifecycle/ipa-backfill.ts +2 -1
  110. package/src/services/accounts/app.ts +1 -1
  111. package/src/services/accounts/avatar.ts +1 -1
  112. package/src/services/accounts/base-group.ts +1 -1
  113. package/src/services/accounts/directory-shares.ts +1 -1
  114. package/src/services/accounts/duplicate-emails.ts +1 -1
  115. package/src/services/accounts/entities.ts +2 -1
  116. package/src/services/accounts/group-sql.ts +2 -1
  117. package/src/services/accounts/groups.ts +1 -1
  118. package/src/services/accounts/identities.ts +13 -11
  119. package/src/services/accounts/ipa-data.ts +1 -1
  120. package/src/services/accounts/local-groups.ts +2 -1
  121. package/src/services/accounts/machine-group-management.ts +2 -1
  122. package/src/services/accounts/posix.ts +13 -12
  123. package/src/services/accounts/request-policy.ts +2 -1
  124. package/src/services/accounts/switching.ts +1 -1
  125. package/src/services/accounts/users.ts +2 -1
  126. package/src/services/announcements/index.ts +1 -1
  127. package/src/services/app-access-management.ts +3 -9
  128. package/src/services/app-access.ts +2 -1
  129. package/src/services/app-admin-facts.ts +160 -0
  130. package/src/services/app-approval.ts +34 -30
  131. package/src/services/app-bootstrap.ts +217 -0
  132. package/src/services/app-details.ts +3 -2
  133. package/src/services/app-domain-check.ts +152 -0
  134. package/src/services/app-domains.ts +172 -0
  135. package/src/services/app-events/accept.ts +5 -5
  136. package/src/services/app-events/admin.ts +15 -15
  137. package/src/services/app-events/declarations.ts +6 -3
  138. package/src/services/app-events/deliver.ts +6 -6
  139. package/src/services/app-events.ts +3 -2
  140. package/src/services/app-keys.ts +45 -11
  141. package/src/services/approvals/content.ts +1 -1
  142. package/src/services/approvals/index.ts +2 -1
  143. package/src/services/approvals/list.ts +1 -1
  144. package/src/services/approvals/migrate.ts +1 -1
  145. package/src/services/audit/index.ts +2 -1
  146. package/src/services/auth-flows/ipa.ts +1 -1
  147. package/src/services/auth-flows/magic-link.ts +1 -1
  148. package/src/services/auth-flows/password-reset.ts +1 -1
  149. package/src/services/auth-flows/proxy-return.ts +1 -1
  150. package/src/services/downstream-budget.ts +2 -2
  151. package/src/services/geo/admin.ts +13 -12
  152. package/src/services/geo/identity.ts +46 -0
  153. package/src/services/geo/ledger.ts +118 -106
  154. package/src/services/geo/migrate.ts +2 -1
  155. package/src/services/geo/route-chain.ts +3 -3
  156. package/src/services/geo/service.ts +4 -24
  157. package/src/services/geoip/index.ts +5 -5
  158. package/src/services/geoip/store.ts +24 -21
  159. package/src/services/help/index.ts +2 -1
  160. package/src/services/help/store.ts +2 -1
  161. package/src/services/identity/app-request.ts +44 -0
  162. package/src/services/identity/constants.ts +1 -0
  163. package/src/services/identity/index.ts +1 -0
  164. package/src/services/identity/invocation-actor.ts +2 -1
  165. package/src/services/identity/invocation-token.ts +112 -0
  166. package/src/services/identity/key-ring.ts +2 -2
  167. package/src/services/identity/resource-readers.ts +1 -1
  168. package/src/services/identity/routes.ts +11 -3
  169. package/src/services/identity/runtime-config.ts +14 -3
  170. package/src/services/identity/workload-auth.ts +2 -0
  171. package/src/services/identity/workload-runtime.ts +3 -3
  172. package/src/services/index.ts +4 -0
  173. package/src/services/ipa/groups.ts +1 -1
  174. package/src/services/ipa/profile.ts +1 -1
  175. package/src/services/ipa/search.ts +1 -1
  176. package/src/services/ipa/sync.ts +1 -1
  177. package/src/services/ipa/users.ts +1 -1
  178. package/src/services/known-browsers.ts +2 -1
  179. package/src/services/legal-consent.ts +1 -1
  180. package/src/services/links/index.ts +16 -0
  181. package/src/services/links/runtime.ts +44 -0
  182. package/src/services/links/store.ts +105 -0
  183. package/src/services/logging/index.ts +13 -8
  184. package/src/services/logging/redaction.ts +6 -1
  185. package/src/services/logging/trace.ts +9 -9
  186. package/src/services/logging/web-vitals.ts +1 -1
  187. package/src/services/mandates/index.ts +2 -1
  188. package/src/services/notifications/batches.ts +1 -1
  189. package/src/services/notifications/browser.ts +1 -1
  190. package/src/services/notifications/catalog.ts +1 -1
  191. package/src/services/notifications/dispatcher.ts +1 -1
  192. package/src/services/notifications/index.ts +1 -1
  193. package/src/services/notifications/observability.ts +2 -1
  194. package/src/services/notifications/platform.ts +1 -1
  195. package/src/services/notifications/quiet.ts +1 -1
  196. package/src/services/notifications/user.ts +1 -1
  197. package/src/services/oauth-tokens.ts +1 -1
  198. package/src/services/outbox.ts +15 -14
  199. package/src/services/outgoing-mail/admin.ts +1 -1
  200. package/src/services/outgoing-mail/dispatcher.ts +1 -1
  201. package/src/services/outgoing-mail/drain.ts +2 -1
  202. package/src/services/outgoing-mail/enqueue.ts +2 -1
  203. package/src/services/outgoing-mail/messages.ts +2 -1
  204. package/src/services/outgoing-mail/retention.ts +1 -1
  205. package/src/services/outgoing-mail/send.ts +1 -1
  206. package/src/services/outgoing-mail/store.ts +2 -1
  207. package/src/services/panel-context-consent.ts +32 -28
  208. package/src/services/providers/local/auth.ts +1 -1
  209. package/src/services/providers/local/users.ts +1 -1
  210. package/src/services/pwa-devices.ts +2 -1
  211. package/src/services/rail-preferences.ts +16 -12
  212. package/src/services/rail-shortcuts.ts +12 -10
  213. package/src/services/request-cache-redis.ts +3 -3
  214. package/src/services/service-account-credentials.ts +2 -1
  215. package/src/services/service-accounts.ts +2 -1
  216. package/src/services/session/index.ts +2 -1
  217. package/src/services/session/user.ts +2 -1
  218. package/src/services/settings/core-settings.ts +20 -0
  219. package/src/services/settings/crypto.ts +2 -2
  220. package/src/services/settings/index.ts +1 -1
  221. package/src/services/settings/store.ts +6 -1
  222. package/src/services/settings/versions.ts +2 -1
  223. package/src/services/weather/forecast.ts +2 -1
  224. package/src/services/weather/locations.ts +1 -1
  225. package/src/services/weather/migrate.ts +2 -1
  226. package/src/services/weather/short-id.ts +2 -1
  227. package/src/services/webauthn.ts +1 -1
  228. package/src/ssr/admin-navigation.ts +0 -1
  229. package/src/ssr/compact-shell.browser-harness.ts +2 -0
  230. package/src/ssr/platform-messages.ts +0 -2
  231. package/src/start.ts +35 -14
  232. package/src/workflows/ai/store.ts +2 -1
  233. package/src/workflows/store/budget.ts +2 -1
  234. package/src/workflows/store/definitions.ts +2 -1
  235. package/src/workflows/store/events.ts +2 -1
  236. package/src/workflows/store/observability.ts +2 -1
  237. package/src/workflows/store/retention.ts +2 -1
  238. package/src/workflows/store/runs.ts +2 -1
  239. package/src/workflows/store/transaction.ts +2 -1
  240. package/src/workflows/store/worker.ts +2 -1
@@ -9,8 +9,8 @@
9
9
  * env-backed entries into Postgres. Startup never replaces an existing row;
10
10
  * env bootstrap only fills missing keys.
11
11
  */
12
+ import { redis, sql } from "../../runtime/clients";
12
13
 
13
- import { redis, sql } from "bun";
14
14
  import { bindSettingValue, replaceSettingValue, unwrapSettingValue } from "./binding";
15
15
  import { decryptValue, encryptValue, getAppSecret } from "./crypto";
16
16
  import { resolveSettingPresentation, SETTINGS, SETTINGS_MAP, type SettingDef, validateSettingValue } from "./defaults";
@@ -13,9 +13,10 @@
13
13
  * snapshot exposed via `c.get("settings")` (built by snapshot.ts middleware).
14
14
  */
15
15
 
16
- import { sql } from "bun";
17
16
  import { HTTPException } from "hono/http-exception";
18
17
  import { hasRole, type User } from "../../contracts/shared";
18
+ import { sql } from "../../runtime/clients";
19
+ import { connections } from "../../runtime/connections";
19
20
  import { claimCacheFill, completeCacheFill, MISSING_SETTING } from "../cache-fill";
20
21
  import { stripImageDataUrlMetadata } from "../image-metadata";
21
22
  import { toPgTextArray } from "../postgres";
@@ -89,6 +90,8 @@ export const readKeyState = async (key: string, db: SqlClient = sql): Promise<Se
89
90
  export const bulkRead = async (keys: readonly string[]): Promise<Map<string, unknown>> => {
90
91
  const result = new Map<string, unknown>();
91
92
  if (keys.length === 0) return result;
93
+ // Missing startup is a programming error, never a cache outage or unreadable row.
94
+ connections();
92
95
  // Bound access is read with its endpoint in one database snapshot. Never
93
96
  // resolve it from an independently cached secret.
94
97
  const boundKeys = keys.filter((key) => settingOwners(key).length > 0);
@@ -163,6 +166,7 @@ export const bulkRead = async (keys: readonly string[]): Promise<Map<string, unk
163
166
  export const readStates = async (keys: readonly string[], db: SqlClient = sql): Promise<Map<string, SettingRead>> => {
164
167
  const result = new Map<string, SettingRead>();
165
168
  if (keys.length === 0) return result;
169
+ connections();
166
170
  const snapshotKeys = [...new Set(keys.flatMap((key) => [key, ...settingOwners(key).map((owner) => owner.key)]))];
167
171
  const rows = await db<StoredRow[]>`SELECT key, value FROM settings.entries WHERE key = ANY(${toPgTextArray(snapshotKeys)}::text[])`;
168
172
  const byKey = new Map(rows.map((row) => [row.key, row]));
@@ -206,6 +210,7 @@ const knownKeysWith = (extraKnownKeys: readonly string[]) =>
206
210
  Array.from(new Set([...allKnownKeys(), ...retainedLegacyKeys, ...extraKnownKeys]));
207
211
 
208
212
  export const listLegacyKeys = async (extraKnownKeys: readonly string[] = []): Promise<LegacySettingRow[]> => {
213
+ connections();
209
214
  const knownKeys = knownKeysWith(extraKnownKeys);
210
215
  const rows = await sql<LegacyStoredRow[]>`
211
216
  SELECT key, value, updated_at
@@ -1,4 +1,5 @@
1
- import { sql } from "bun";
1
+ import { sql } from "../../runtime/clients";
2
+
2
3
  import { ConfigConflict } from "../config-conflict";
3
4
  import { SETTINGS_MAP } from "./defaults";
4
5
  import { readKeyState } from "./store";
@@ -1,4 +1,5 @@
1
- import { redis } from "bun";
1
+ import { redis } from "../../runtime/clients";
2
+
2
3
  import { logger } from "../logging";
3
4
  import { coreSettings } from "../settings/api";
4
5
  import type { CurrentWeather, DailyForecast, HourlyForecast, WeatherData, WeatherIcon } from "./types";
@@ -1,6 +1,6 @@
1
1
  import { err, fail, ok, type PageParams, type Paginated, paginate, type Result } from "@k2b/stdlib";
2
- import { sql } from "bun";
3
2
  import type { GeoProvenance } from "../../contracts/geo";
3
+ import { sql } from "../../runtime/clients";
4
4
  import { logger } from "../logging";
5
5
  import { withWeatherLocationShortId } from "./short-id";
6
6
 
@@ -1,4 +1,5 @@
1
- import { sql } from "bun";
1
+ import { sql } from "../../runtime/clients";
2
+
2
3
  import { backfillWeatherLocationShortIds } from "./short-id";
3
4
 
4
5
  export const migrate = async (): Promise<void> => {
@@ -1,5 +1,6 @@
1
1
  import { crypto } from "@k2b/stdlib";
2
- import { sql } from "bun";
2
+ import { sql } from "../../runtime/clients";
3
+
3
4
  import { isUniqueViolation } from "../postgres";
4
5
 
5
6
  export const WEATHER_LOCATION_SHORT_ID_PATTERN = /^[0-9A-Za-z]{6}$/;
@@ -10,8 +10,8 @@ import {
10
10
  verifyRegistrationResponse,
11
11
  type WebAuthnCredential,
12
12
  } from "@simplewebauthn/server";
13
- import { redis, sql } from "bun";
14
13
  import type { User, WebAuthnPasskey } from "../contracts/shared";
14
+ import { redis, sql } from "../runtime/clients";
15
15
  import { accounts } from "./accounts";
16
16
  import { audit } from "./audit";
17
17
  import { logger } from "./logging";
@@ -70,7 +70,6 @@ export const buildAdminGroups = (apps: readonly RuntimeContext["apps"][number][]
70
70
  links: [
71
71
  { href: "/admin", icon: "ti-dashboard", label: t.overview },
72
72
  { href: "/admin/apps", icon: "ti-apps", label: t.apps },
73
- { href: "/admin/app-credentials", icon: "ti-key", label: t.appCredentials },
74
73
  { href: "/admin/rail", icon: "ti-layout-sidebar", label: t.rail },
75
74
  { href: "/admin/announcements", icon: "ti-speakerphone", label: t.announcements },
76
75
  ],
@@ -23,6 +23,7 @@ const { defineApp } = await import("../_internal/define-app");
23
23
  const { person } = await import("../_internal/define-app.test-fixture");
24
24
  const railSnapshot = await import("../services/rail-snapshot");
25
25
  const settingsMiddleware = await import("../server/middleware/settings");
26
+ const help = await import("./help");
26
27
  const { defaultRailPreferences } = await import("../contracts/rail-preferences");
27
28
  const { default: WorkspaceNavigation } = await import("./WorkspaceNavigation.island");
28
29
  const { default: Layout } = await import("./Layout");
@@ -367,6 +368,7 @@ export function installCompactShellHarness(launch: typeof launchBrowser) {
367
368
  spies.push(
368
369
  spyOn(railSnapshot, "readRailSnapshot").mockResolvedValue(defaultRailPreferences()),
369
370
  spyOn(settingsMiddleware, "preloadLayoutAnnouncements").mockResolvedValue(undefined),
371
+ spyOn(help, "preloadLayoutHelp").mockResolvedValue(null),
370
372
  );
371
373
  const build = await Bun.build({ entrypoints: [resolve(import.meta.dir, "../../../../styles.css")], plugins: [tailwind] });
372
374
  if (!build.success) throw new AggregateError(build.logs, "Could not compile the global stylesheet.");
@@ -40,7 +40,6 @@ export const platformMessages = i18n.define({
40
40
  general: "General",
41
41
  rail: "App bar",
42
42
  announcements: "Announcements",
43
- appCredentials: "App credentials",
44
43
  ai: "AI",
45
44
  observability: "Observability",
46
45
  baseSettings: "Base settings",
@@ -147,7 +146,6 @@ export const platformMessages = i18n.define({
147
146
  general: "Allgemein",
148
147
  rail: "App-Leiste",
149
148
  announcements: "Ankündigungen",
150
- appCredentials: "App-Schlüssel",
151
149
  ai: "KI",
152
150
  observability: "Observability",
153
151
  baseSettings: "Grundeinstellungen",
package/src/start.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  // Keep this entry independent of the platform barrel and application modules.
2
+ import { parseAppKey } from "./app-key";
3
+ import { env } from "./config/env";
4
+ import { bootstrapConnections } from "./runtime/bootstrap-client";
5
+ import type { BootstrapResponse } from "./runtime/bootstrap-protocol";
2
6
  import { initializeClients } from "./runtime/clients";
3
7
  import { bindConnections, hasConnections } from "./runtime/connections";
4
8
  import { resolveConnections } from "./runtime/resolve-connections";
@@ -9,24 +13,41 @@ export type PlatformStartOptions = {
9
13
  source?: "env";
10
14
  };
11
15
 
16
+ let initializing = false;
17
+
12
18
  /** Initialize before importing any application module, also in dev and data workers. */
13
19
  export const initializePlatform = async ({ appId, source }: PlatformStartOptions): Promise<void> => {
14
20
  if (!/^[a-z][a-z0-9-]{0,79}$/.test(appId)) throw new Error("Platform start requires a valid appId");
15
- if (hasConnections()) throw new Error("Platform has already started");
16
- if (appId === "core" || appId === "gateway") source = "env";
17
- // Slice 6b seam: validate the app key and fetch missing values from Core here,
18
- // then pass the typed response to resolveConnections({ appId, core }).
19
- // A present key must never silently fall back to the legacy env mode.
20
- if (source !== "env" && (process.env.CLOUD_APP_KEY?.trim() || process.env.CLOUD_APP_KEY_FILE?.trim())) {
21
- throw new Error("Core connection bootstrap is not available yet");
22
- }
23
- const resolved = resolveConnections({ appId });
24
- bindConnections(resolved);
21
+ if (initializing || hasConnections()) throw new Error("Platform has already started");
22
+ initializing = true;
25
23
  try {
26
- await initializeClients();
27
- } catch (error) {
28
- bindConnections(undefined);
29
- throw error;
24
+ const keyValue = env.CLOUD_APP_KEY;
25
+ const envOnly = source === "env" || appId === "core" || appId === "gateway";
26
+ if (keyValue && envOnly) throw new Error("App keys are forbidden in forced env mode (including Core and Gateway)");
27
+ let bootstrap: BootstrapResponse | undefined;
28
+ if (keyValue) {
29
+ // Parsing never prints the private value. A present invalid key cannot fall back to env.
30
+ const key = parseAppKey(keyValue);
31
+ if (key.appId !== appId) throw new Error("App-key appId does not match the declared application");
32
+ bootstrap = await bootstrapConnections(key, env.CLOUD_CORE_INTERNAL_ORIGIN);
33
+ }
34
+ const resolved = resolveConnections({ appId, core: bootstrap?.connections });
35
+ resolved.bootstrap = {
36
+ mode: bootstrap ? "core" : "env",
37
+ ...(bootstrap ? { keyId: bootstrap.identity.keyId } : {}),
38
+ sources: resolved.sources,
39
+ keyFormat: "cloud-app-key-v1",
40
+ protocol: 1,
41
+ };
42
+ bindConnections(resolved);
43
+ try {
44
+ await initializeClients();
45
+ } catch (error) {
46
+ bindConnections(undefined);
47
+ throw error;
48
+ }
49
+ } finally {
50
+ initializing = false;
30
51
  }
31
52
  };
32
53
 
@@ -1,5 +1,6 @@
1
- import { type SQL, sql } from "bun";
1
+ import type { SQL } from "bun";
2
2
  import { AI_TASK_REQUEST_MAX_BYTES, AiTaskInputTooLargeError, assertAiTaskRequestSize } from "../../ai/task-contracts";
3
+ import { sql } from "../../runtime/clients";
3
4
  import type { AccessSubject } from "../../server/services/access";
4
5
  import { type AiResolvedModelRef, AiResolvedModelRefSchema } from "../../shared/ai-models";
5
6
  import type { WorkflowJsonValue } from "../contracts";
@@ -16,7 +16,8 @@
16
16
  * rather than something a hash has to detect — whatever the plan turns out to
17
17
  * do, it still cannot exceed what was approved.
18
18
  */
19
- import { type SQL, sql } from "bun";
19
+ import type { SQL } from "bun";
20
+ import { sql } from "../../runtime/clients";
20
21
  import { withTransaction } from "./transaction";
21
22
 
22
23
  /** Caps keyed by dimension — `{ emails: 100, httpRequests: 50 }`. */
@@ -9,7 +9,8 @@
9
9
  * edit, and an activation that pins one cannot be left pointing at a plan that
10
10
  * no longer exists.
11
11
  */
12
- import { type SQL, sql } from "bun";
12
+ import type { SQL } from "bun";
13
+ import { sql } from "../../runtime/clients";
13
14
  import type { WorkflowBoundPlan, WorkflowDiagnostic, WorkflowJsonValue } from "../contracts";
14
15
  import { validateWorkflowEffectBudget } from "./budget";
15
16
  import { withTransaction } from "./transaction";
@@ -11,7 +11,8 @@
11
11
  * is an event. Nothing needs its own registration path.
12
12
  */
13
13
  import { Buffer } from "node:buffer";
14
- import { type SQL, sql } from "bun";
14
+ import type { SQL } from "bun";
15
+ import { sql } from "../../runtime/clients";
15
16
  import type { WorkflowJsonValue } from "../contracts";
16
17
  import { createWorkflowRun } from "./runs";
17
18
  import { withTransaction } from "./transaction";
@@ -11,7 +11,8 @@
11
11
  * matched nothing and said nothing, and a run that started long after the
12
12
  * occurrence that caused it.
13
13
  */
14
- import { type SQL, sql } from "bun";
14
+ import type { SQL } from "bun";
15
+ import { sql } from "../../runtime/clients";
15
16
  import type { WorkflowInvocationMode, WorkflowJsonValue, WorkflowRunState } from "../contracts";
16
17
  import type { WorkflowEffectBudget } from "./budget";
17
18
 
@@ -1,4 +1,5 @@
1
- import { type SQL, sql } from "bun";
1
+ import type { SQL } from "bun";
2
+ import { sql } from "../../runtime/clients";
2
3
  import { logger } from "../../services/logging";
3
4
  import { toPgUuidArray } from "../../services/postgres";
4
5
  import { withTransaction } from "./transaction";
@@ -16,7 +16,8 @@
16
16
  * that lost its lease writes with a stale generation and every one of its
17
17
  * updates matches zero rows. Nothing here trusts wall-clock ordering.
18
18
  */
19
- import { type SQL, sql } from "bun";
19
+ import type { SQL } from "bun";
20
+ import { sql } from "../../runtime/clients";
20
21
  import type { WorkflowBoundPlan, WorkflowDependency, WorkflowInvocationMode, WorkflowJsonValue, WorkflowRunState } from "../contracts";
21
22
  import type {
22
23
  WorkflowCoordinatorFinishState,
@@ -10,7 +10,8 @@
10
10
  * handle only has `savepoint` when it *is* a transaction, which is what makes
11
11
  * this reliable rather than a convention callers have to remember.
12
12
  */
13
- import { type SQL, sql } from "bun";
13
+ import type { SQL } from "bun";
14
+ import { sql } from "../../runtime/clients";
14
15
 
15
16
  type Savepointed = SQL & { savepoint: <T>(fn: (tx: SQL) => Promise<T>) => Promise<T> };
16
17
 
@@ -11,7 +11,8 @@
11
11
  * about *when* something runs, *which* plan it runs, and what happens if the
12
12
  * process dies belongs here.
13
13
  */
14
- import { type SQL, sql } from "bun";
14
+ import type { SQL } from "bun";
15
+ import { sql } from "../../runtime/clients";
15
16
  import type { WorkflowActor, WorkflowInvocationMode, WorkflowJsonValue } from "../contracts";
16
17
  import { hashWorkflowJson } from "../language/canonical";
17
18
  import type { DefinedWorkflowModule } from "../module";