postgresai 0.18.0-dev.13 → 0.18.0-dev.15

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.
@@ -13430,9 +13430,9 @@ var require_dist2 = __commonJS(function(exports, module) {
13430
13430
  });
13431
13431
 
13432
13432
  // lib/util.ts
13433
- function requestTimeoutSignal2(timeoutMs) {
13433
+ function requestTimeoutSignal2(timeoutMs, ceilingMs = DEFAULT_HTTP_REQUEST_TIMEOUT_MS2) {
13434
13434
  const requested = typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : DEFAULT_HTTP_REQUEST_TIMEOUT_MS2;
13435
- const bounded = Math.max(1, Math.min(DEFAULT_HTTP_REQUEST_TIMEOUT_MS2, Math.floor(requested)));
13435
+ const bounded = Math.max(1, Math.min(ceilingMs, Math.floor(requested)));
13436
13436
  return { signal: AbortSignal.timeout(bounded), timeoutMs: bounded };
13437
13437
  }
13438
13438
  function maskSecret2(secret) {
@@ -13533,7 +13533,7 @@ var {
13533
13533
  // package.json
13534
13534
  var package_default = {
13535
13535
  name: "postgresai",
13536
- version: "0.18.0-dev.13",
13536
+ version: "0.18.0-dev.15",
13537
13537
  description: "postgres_ai CLI",
13538
13538
  license: "Apache-2.0",
13539
13539
  private: false,
@@ -16893,7 +16893,7 @@ var Result = import_lib.default.Result;
16893
16893
  var TypeOverrides = import_lib.default.TypeOverrides;
16894
16894
  var defaults = import_lib.default.defaults;
16895
16895
  // package.json
16896
- var version = "0.18.0-dev.13";
16896
+ var version = "0.18.0-dev.15";
16897
16897
  var package_default2 = {
16898
16898
  name: "postgresai",
16899
16899
  version,
@@ -17160,10 +17160,20 @@ function describeFetchError(operation, url, err) {
17160
17160
 
17161
17161
  class HttpStatusError extends Error {
17162
17162
  status;
17163
- constructor(message, status) {
17163
+ sqlstate;
17164
+ constructor(message, status, sqlstate) {
17164
17165
  super(message);
17165
17166
  this.name = "HttpStatusError";
17166
17167
  this.status = status;
17168
+ this.sqlstate = sqlstate;
17169
+ }
17170
+ }
17171
+ function sqlstateOf(body) {
17172
+ try {
17173
+ const code = JSON.parse(body)?.code;
17174
+ return typeof code === "string" && /^[0-9A-Z]{5}$/.test(code) ? code : undefined;
17175
+ } catch {
17176
+ return;
17167
17177
  }
17168
17178
  }
17169
17179
  function isRetryableHttpStatus(status) {
@@ -17201,9 +17211,9 @@ class HttpRequestTimeoutError extends Error {
17201
17211
  this.name = "HttpRequestTimeoutError";
17202
17212
  }
17203
17213
  }
17204
- function requestTimeoutSignal(timeoutMs) {
17214
+ function requestTimeoutSignal(timeoutMs, ceilingMs = DEFAULT_HTTP_REQUEST_TIMEOUT_MS) {
17205
17215
  const requested = typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : DEFAULT_HTTP_REQUEST_TIMEOUT_MS;
17206
- const bounded = Math.max(1, Math.min(DEFAULT_HTTP_REQUEST_TIMEOUT_MS, Math.floor(requested)));
17216
+ const bounded = Math.max(1, Math.min(ceilingMs, Math.floor(requested)));
17207
17217
  return { signal: AbortSignal.timeout(bounded), timeoutMs: bounded };
17208
17218
  }
17209
17219
  function isFetchTimeout(err) {
@@ -22285,7 +22295,7 @@ async function buildInitPlan(params) {
22285
22295
  if not exists (select 1 from pg_catalog.pg_roles where rolname = ${qRoleNameLit}) then
22286
22296
  begin
22287
22297
  create user ${qRole} with password ${qPwVerifier};
22288
- exception when duplicate_object then
22298
+ exception when duplicate_object or unique_violation then
22289
22299
  null;
22290
22300
  end;
22291
22301
  end if;${params.keepExistingPassword ? "" : `
@@ -22338,9 +22348,21 @@ async function applyInitPlan(params) {
22338
22348
  throw e;
22339
22349
  }
22340
22350
  };
22351
+ const concurrent = (e) => /tuple concurrently (updated|deleted)/.test(e instanceof Error ? e.message : String(e));
22352
+ const executeRetried = async (step) => {
22353
+ for (let attempt = 1;; attempt++) {
22354
+ try {
22355
+ return await executeStep(step);
22356
+ } catch (e) {
22357
+ if (attempt >= 3 || !concurrent(e))
22358
+ throw e;
22359
+ await new Promise((r) => setTimeout(r, 200 * attempt));
22360
+ }
22361
+ }
22362
+ };
22341
22363
  for (const step of params.plan.steps.filter((s) => !s.optional)) {
22342
22364
  try {
22343
- await executeStep(step);
22365
+ await executeRetried(step);
22344
22366
  applied.push(step.name);
22345
22367
  } catch (e) {
22346
22368
  const msg = e instanceof Error ? e.message : String(e);
@@ -22560,6 +22582,8 @@ function monitoringStatus(raw) {
22560
22582
  return "inactive";
22561
22583
  if (/fail|error/.test(raw ?? ""))
22562
22584
  return "failed";
22585
+ if (raw === "ip_reserved")
22586
+ return "action_required";
22563
22587
  return "in_progress";
22564
22588
  }
22565
22589
  function dblabStatus(deployStatus) {
@@ -22692,7 +22716,7 @@ async function callRpcRaw(params) {
22692
22716
  console.error(`Debug: Request body: ${redactSecretsForLog(payload)}`);
22693
22717
  }
22694
22718
  let response;
22695
- const requestTimeout = requestTimeoutSignal(params.timeoutMs);
22719
+ const requestTimeout = requestTimeoutSignal(params.timeoutMs, params.timeoutCeilingMs);
22696
22720
  try {
22697
22721
  response = await fetch(url.toString(), {
22698
22722
  method: "POST",
@@ -22718,7 +22742,7 @@ async function callRpcRaw(params) {
22718
22742
  ({ response, text } = await send(params.fallbackBody));
22719
22743
  }
22720
22744
  if (!response.ok) {
22721
- throw new HttpStatusError(formatHttpError(operation, response.status, text, response.statusText), response.status);
22745
+ throw new HttpStatusError(formatHttpError(operation, response.status, text, response.statusText), response.status, sqlstateOf(text));
22722
22746
  }
22723
22747
  try {
22724
22748
  return { data: JSON.parse(text), status: response.status };
@@ -22943,6 +22967,7 @@ function storedPasswordNeeds(monitoringUrl) {
22943
22967
  return needs.length ? needs.join(" and ") : undefined;
22944
22968
  }
22945
22969
  var shellWord = (s) => /^[\w./:@%+=,-]+$/.test(s) ? s : `'${s.replace(/'/g, "'\\''")}'`;
22970
+ var deleteRef = (d, list) => list.some((o) => o.id !== d.id && !disconnecting(o.status) && o.name === d.name) ? d.id : d.name;
22946
22971
  var disconnectEach = (names) => names.map((n) => `pgai mon instances delete ${shellWord(n)} --yes`).join(" and ");
22947
22972
  function loginName(url, sessionUser, role) {
22948
22973
  const user = decodeURIComponent(new URL(url).username);
@@ -22985,6 +23010,7 @@ function connectStatus(row, provider = row.provider, fresh = false) {
22985
23010
  return {
22986
23011
  ...base,
22987
23012
  status,
23013
+ ...row.egress_ip ? { egress_ip: row.egress_ip } : {},
22988
23014
  ...fresh ? { first_checkup_eta: new Date(Date.now() + FIRST_CHECKUP_DELAY_MS).toISOString() } : {},
22989
23015
  next: row.health_url || row.dashboard_url ? `Open ${row.health_url || row.dashboard_url}` : `pgai mon instances watch ${row.id}`
22990
23016
  };
@@ -22993,6 +23019,8 @@ function connectStatus(row, provider = row.provider, fresh = false) {
22993
23019
  return { ...base, status, next: "none" };
22994
23020
  if (status === "failed")
22995
23021
  return { ...base, status, next: `pgai mon instances delete ${row.id} --yes, then pgai mon deploy again` };
23022
+ if (row.status === "ip_reserved" && row.egress_ip)
23023
+ return { ...allowIpResult(row, provider, row.name, { waitMs: 0 }, ""), ...base, status: "action_required", step: "allow_ip" };
22996
23024
  return { ...base, status, next: `pgai mon instances watch ${row.id}` };
22997
23025
  }
22998
23026
  var ESCAPES = /(?:\x1b\[|\x9b)[0-?]*[ -\/]*[@-~]|(?:\x1b\]|\x9d)[^\x07\x1b\x9c]*(?:\x07|\x1b\\|\x9c)?|\x1b[ -~]?/g;
@@ -23006,6 +23034,12 @@ function boxWords(state) {
23006
23034
  return "starting";
23007
23035
  if (state === "registered")
23008
23036
  return "installing monitoring";
23037
+ if (state === "waiting_for_access")
23038
+ return "waiting for database access";
23039
+ if (state === "ip_reserving")
23040
+ return "reserving its IP";
23041
+ if (state === "ip_reserved")
23042
+ return "waiting for its IP to be allowed";
23009
23043
  return monitoringStatus(state).replace("_", " ");
23010
23044
  }
23011
23045
  async function billingFor(q, name, opts, deps, show, ask) {
@@ -23055,6 +23089,9 @@ async function connect(url, opts, deps) {
23055
23089
  if (project !== undefined && provider !== "supabase") {
23056
23090
  return { status: "action_required", provider, name: url ? databaseName2(url) : "", next: "--supabase-project applies to Supabase only: leave it out, or use --provider supabase" };
23057
23091
  }
23092
+ if ((opts.ipFirst || opts.ipAllowed) && (opts.selfHosted || provider === "supabase")) {
23093
+ return { status: "action_required", provider, name: url ? databaseName2(url) : "", next: `${opts.agent ? "ip_first / ip_allowed apply" : "--ip-first and --ip-allowed apply"} to PostgresAI Cloud monitoring of a database URL only` };
23094
+ }
23058
23095
  if (!url) {
23059
23096
  if (provider !== "supabase")
23060
23097
  return { status: "action_required", provider, name: "", next: "Pass the database URL: pgai mon deploy postgresql://user:password@host:5432/dbname (Supabase: --provider supabase)" };
@@ -23106,12 +23143,31 @@ async function connect(url, opts, deps) {
23106
23143
  if (opts.selfHosted && deps.localStackRunning()) {
23107
23144
  return { status: "action_required", provider, name, next: "A monitoring stack already runs on this machine: add the database with PGAI_DB_URL='<postgres_ai_mon URL>' pgai mon targets add" };
23108
23145
  }
23146
+ const byCluster = (d) => !!d.cluster && serverOf(d.cluster) === cluster && d.database != null && decodeURIComponent(d.database) === name.split("/").slice(1).join("/");
23147
+ const byName = (d) => (d.cluster == null || d.database == null) && d.name === name;
23109
23148
  const findDatabase = (list) => {
23110
23149
  const live = list.filter((d) => !disconnecting(d.status));
23111
- return live.find((d) => d.cluster && serverOf(d.cluster) === cluster && d.database != null && decodeURIComponent(d.database) === name.split("/").slice(1).join("/")) ?? live.find((d) => (d.cluster == null || d.database == null) && d.name === name);
23150
+ return live.find(byCluster) ?? live.find(byName);
23112
23151
  };
23113
23152
  const rows = opts.selfHosted ? [] : await deps.list();
23153
+ const known = new Map(rows.map((d) => [d.id, d.status]));
23154
+ const findMade = (list) => {
23155
+ const fresh = list.filter((d) => !known.has(d.id) || reservedStatus(known.get(d.id)) && !reservedStatus(d.status));
23156
+ return fresh.find(byCluster) ?? fresh.find(byName);
23157
+ };
23114
23158
  let row = findDatabase(rows);
23159
+ let continuing = false;
23160
+ let ownId;
23161
+ if (row && reservedStatus(row.status)) {
23162
+ if (!opts.ipAllowed) {
23163
+ const asked = await allowIpStep(row, { provider, name, url, opts, deps, progress });
23164
+ if (asked !== "continue")
23165
+ return asked;
23166
+ }
23167
+ continuing = true;
23168
+ ownId = row.id;
23169
+ row = undefined;
23170
+ }
23115
23171
  const fresh = !row;
23116
23172
  let note = "";
23117
23173
  let billing = {};
@@ -23152,7 +23208,7 @@ async function connect(url, opts, deps) {
23152
23208
  }
23153
23209
  }
23154
23210
  if (row && opts.resetPassword) {
23155
- return { status: "action_required", provider, name, id: row.id, next: `${name} is already monitored, and its monitoring uses the current password: pgai mon instances delete ${name} --yes first` };
23211
+ return { status: "action_required", provider, name, id: row.id, next: `${row.name} is already monitored, and its monitoring uses the current password. Delete it, then run this deploy again with --reset-password: ${disconnectEach([deleteRef(row, rows)])}` };
23156
23212
  }
23157
23213
  if (row && key) {
23158
23214
  const found = await findOrg();
@@ -23180,26 +23236,30 @@ async function connect(url, opts, deps) {
23180
23236
  let lockId;
23181
23237
  const unread = (d) => !d.cluster;
23182
23238
  const onServer = (d) => !!d.cluster && serverOf(d.cluster) === cluster;
23183
- const othersOnServer = (list) => list.filter((d) => !disconnecting(d.status) && (unread(d) || onServer(d)));
23239
+ const usesRole = (d) => !["deleting", "deleted", "inactive"].includes(stateOf(d.status)) && !(d.status === "failed" && !d.registered_at);
23240
+ const othersOnServer = (list) => list.filter((d) => d.id !== ownId && usesRole(d) && (unread(d) || onServer(d)));
23184
23241
  const storedPassword = !monitoringUser && rows.some((d) => d.monitoring_password_stored && !/delet/.test(d.status ?? "") && onServer(d)) || undefined;
23185
- const cutOff = (others) => {
23186
- const on = others.filter((d) => !unread(d)).map((d) => d.name).join(", ");
23187
- const maybe = others.filter(unread).map((d) => d.name).join(", ");
23242
+ const cutOff = (others, list) => {
23243
+ const label = (d) => deleteRef(d, list) === d.id ? `${d.name} (id ${d.id})` : d.name;
23244
+ const on = others.filter((d) => !unread(d)).map(label).join(", ");
23245
+ const maybe = others.filter(unread).map(label).join(", ");
23188
23246
  const what = [on && `would cut off the monitoring of ${on} on this server`, maybe && `may cut off the monitoring of ${maybe} (the platform could not read its URL, so its server is not known)`].filter(Boolean).join(", and ");
23189
23247
  const instead = [
23190
23248
  ...storedPassword ? ["deploy without --reset-password (PostgresAI keeps its password for this server, and sends it only over TLS)"] : [],
23191
23249
  "set PGAI_MON_PASSWORD to its password instead",
23192
- `${disconnectEach(others.map((d) => d.name))} first`
23250
+ `${disconnectEach(others.map((d) => deleteRef(d, list)))} first`
23193
23251
  ];
23194
23252
  return { status: "action_required", provider, name, next: `A new password for ${role} ${what}: ${instead.join(", or ")}` };
23195
23253
  };
23196
- const others = othersOnServer(rows).filter((d) => !unread(d)).map((d) => d.name);
23254
+ const onThisServer = othersOnServer(rows).filter((d) => !unread(d));
23255
+ const others = onThisServer.map((d) => d.name);
23256
+ const otherRefs = onThisServer.map((d) => deleteRef(d, rows));
23197
23257
  if (opts.resetPassword && othersOnServer(rows).length)
23198
- return cutOff(othersOnServer(rows));
23258
+ return cutOff(othersOnServer(rows), rows);
23199
23259
  if (opts.coupon !== undefined && !opts.coupon.trim()) {
23200
23260
  return { status: "action_required", provider, name, next: `${opts.agent ? "coupon" : "--coupon"} is empty: pass a promotion code, or leave ${opts.agent ? "coupon" : "--coupon"} out` };
23201
23261
  }
23202
- const prepOpts = { resetPassword: opts.resetPassword, warn, ...storedPassword ? { storedPassword, others } : {}, ...monitoringUser ? { monitoringUser } : {} };
23262
+ const prepOpts = { resetPassword: opts.resetPassword, warn, ...storedPassword ? { storedPassword, others, otherRefs } : {}, ...monitoringUser ? { monitoringUser } : {} };
23203
23263
  if (!opts.selfHosted) {
23204
23264
  const probe = await deps.prepare(url, provider, { ...prepOpts, check: true }).catch((err) => {
23205
23265
  if (err instanceof HttpStatusError || /Could not connect to/.test(errorText(err)))
@@ -23263,12 +23323,12 @@ async function connect(url, opts, deps) {
23263
23323
  try {
23264
23324
  if (lockId) {
23265
23325
  const now = await deps.list();
23266
- const same = findDatabase(now);
23326
+ const same = findDatabase(now.filter((d) => d.id !== ownId));
23267
23327
  if (same)
23268
- return { status: "action_required", provider, name, id: same.id, ...billing, next: `${name} is already monitored, and its monitoring uses the current password: pgai mon instances delete ${name} --yes first` };
23328
+ return { status: "action_required", provider, name, id: same.id, ...billing, next: `${same.name} is already monitored, and its monitoring uses the current password. Delete it, then run this deploy again with --reset-password: ${disconnectEach([deleteRef(same, now)])}` };
23269
23329
  const others = othersOnServer(now);
23270
23330
  if (others.length)
23271
- return { ...cutOff(others), ...billing };
23331
+ return { ...cutOff(others, now), ...billing };
23272
23332
  }
23273
23333
  if (deps.aborted?.())
23274
23334
  throw new CancelledError;
@@ -23279,6 +23339,16 @@ async function connect(url, opts, deps) {
23279
23339
  throw new CancelledError;
23280
23340
  }
23281
23341
  if ("next" in prepared) {
23342
+ if ((continuing || opts.ipAllowed) && /created by someone else meanwhile/.test(prepared.next)) {
23343
+ for (let tries = 0;tries < 10; tries++) {
23344
+ const box = findDatabase(await deps.list());
23345
+ if (!box)
23346
+ break;
23347
+ if (!reservedStatus(box.status))
23348
+ return follow(box, { provider, name, fresh, billing, billingUrl, undoRole, note, place, url }, opts, deps, progress);
23349
+ await deps.sleep(2000);
23350
+ }
23351
+ }
23282
23352
  const { resettable, ...refusal } = prepared;
23283
23353
  return { status: "action_required", provider, name, ...billing, ...refusal };
23284
23354
  }
@@ -23291,8 +23361,12 @@ async function connect(url, opts, deps) {
23291
23361
  return { status: "ready", provider, name, dashboard_url: "http://localhost:3000", host_metrics: !!ch, next: `pgai mon health${note}` };
23292
23362
  }
23293
23363
  progress("provisioning", `Provisioning monitoring for ${name}`);
23294
- created = await deps.create({
23295
- db_url: prepared.monitoringUrl,
23364
+ const monitoringUrl = prepared.monitoringUrl;
23365
+ const request = (ipAllowed) => deps.create({
23366
+ db_url: monitoringUrl,
23367
+ allow_ip_fallback: true,
23368
+ ...opts.ipFirst ? { ip_first: true } : {},
23369
+ ...ipAllowed ? { ip_allowed: true } : {},
23296
23370
  ...provider === "clickhouse" ? { provider } : {},
23297
23371
  ...key && ch ? { clickhouse_org_id: ch.orgId, clickhouse_key_id: key.keyId, clickhouse_key_secret: key.keySecret } : {},
23298
23372
  ...opts.coupon ? { promo_code: opts.coupon } : {},
@@ -23300,8 +23374,39 @@ async function connect(url, opts, deps) {
23300
23374
  ...opts.name ? { project_name: opts.name } : {},
23301
23375
  ...place ? { cloud: place.cloud, server_location: place.location } : opts.location ? { server_location: opts.location } : {},
23302
23376
  ...vcpus ? { vcpus } : {}
23377
+ });
23378
+ const refused = async (c) => {
23379
+ const row = await waitForIp(c.id, deps) ?? { id: c.id, name: c.name, status: c.status };
23380
+ const why = [c.error, c.billing_warning].filter(Boolean).join(" ");
23381
+ return { ...c, status: "allow_ip", result: { ...allowIpResult(row, provider, name, opts, url), next: `${why} Then: ${continueCommand(opts, url)}` } };
23382
+ };
23383
+ const continued = async () => {
23384
+ const c = await request(true);
23385
+ return reservedStatus(c.status) && c.error ? refused(c) : c;
23386
+ };
23387
+ created = await request(continuing || !!opts.ipAllowed).then(async (c) => {
23388
+ if (reservedStatus(c.status))
23389
+ known.set(c.id, c.status);
23390
+ if (reservedStatus(c.status) && c.error)
23391
+ return refused(c);
23392
+ if (!reservedStatus(c.status))
23393
+ return c;
23394
+ const reservation = await waitForIp(c.id, deps);
23395
+ if (!reservation || !reservedStatus(reservation.status))
23396
+ return { ...c, status: reservation?.status ?? "failed", error: reservation ? undefined : "The IP reservation did not report back in time; see pgai mon instances list." };
23397
+ if (continuing && c.status === "ip_reserving" && reservation.status === "ip_reserved")
23398
+ return continued();
23399
+ const asked = await allowIpStep(reservation, { provider, name, url, opts, deps, progress });
23400
+ if (asked !== "continue")
23401
+ return { ...c, status: "allow_ip", result: asked };
23402
+ return continued();
23303
23403
  }).catch(async (err) => {
23304
- if (err instanceof HttpStatusError && err.status >= 400 && err.status < 500)
23404
+ if ((continuing || opts.ipAllowed) && err instanceof HttpStatusError && err.status === 409 && /already monitored/.test(err.message)) {
23405
+ const box = findDatabase(await deps.list());
23406
+ if (box && !reservedStatus(box.status))
23407
+ return { id: box.id, name: box.name, status: box.status ?? "launch_requested" };
23408
+ }
23409
+ if (err instanceof HttpStatusError && (err.status < 500 || err.sqlstate))
23305
23410
  await undo(prepared);
23306
23411
  else
23307
23412
  unanswered = true;
@@ -23311,8 +23416,25 @@ async function connect(url, opts, deps) {
23311
23416
  return "price";
23312
23417
  if (err instanceof HttpStatusError && err.status === 409 && "storedPassword" in prepared && prepared.storedPassword && /none is stored/.test(err.message))
23313
23418
  return "stored";
23419
+ if (mayHaveRun(err))
23420
+ return "unanswered";
23314
23421
  throw err;
23315
23422
  });
23423
+ if (created === "unanswered") {
23424
+ progress("provisioning", STILL_CREATING);
23425
+ const made = await lookForBox(findMade, opts, deps, started);
23426
+ if (!made && deps.aborted?.())
23427
+ return { status: "action_required", provider, name, ...billing, next: STOPPED_LOOKING };
23428
+ if (!made)
23429
+ return { status: "action_required", provider, name, ...billing, next: opts.agent ? NOT_CONFIRMED_AGENT : NOT_CONFIRMED };
23430
+ if (stateOf(made.status) === "failed" || disconnecting(made.status)) {
23431
+ await undo(prepared);
23432
+ unanswered = false;
23433
+ return { status: "failed", provider, name, id: made.id, next: disconnecting(made.status) ? "PostgresAI could not launch the monitoring box. Re-run pgai mon deploy later." : `PostgresAI could not launch the monitoring box. Run pgai mon instances delete ${made.id} --yes, then pgai mon deploy again.` };
23434
+ }
23435
+ unanswered = false;
23436
+ created = { id: made.id, name: made.name ?? name, status: made.status ?? "launch_requested" };
23437
+ }
23316
23438
  if (created === "payment")
23317
23439
  return { status: "action_required", provider, name, ...billingPage };
23318
23440
  if (created === "declined") {
@@ -23324,6 +23446,12 @@ async function connect(url, opts, deps) {
23324
23446
  const gone = `PostgresAI no longer keeps the password of ${DEFAULT_MONITORING_USER} for this server`;
23325
23447
  return { status: "action_required", provider, name, ...billing, next: opts.agent ? `${gone}: pass its URL as database_url, or run pgai mon deploy <admin-url> --reset-password in a terminal` : `${gone}: re-run with --reset-password (anything else that logs in as ${DEFAULT_MONITORING_USER} then needs the new one), or set PGAI_MON_PASSWORD to its password` };
23326
23448
  }
23449
+ if (created.status === "allow_ip" && created.result) {
23450
+ const now = findDatabase(await deps.list().catch(() => []));
23451
+ if (!now || reservedStatus(now.status))
23452
+ await undo(prepared);
23453
+ return { ...created.result, ...billing };
23454
+ }
23327
23455
  if (created.status === "failed") {
23328
23456
  await undo(prepared);
23329
23457
  return { status: "failed", provider, name, id: created.id, next: `${created.error} Re-run pgai mon deploy later.` };
@@ -23338,7 +23466,70 @@ async function connect(url, opts, deps) {
23338
23466
  const made = prepared;
23339
23467
  undoRole = () => undo(made);
23340
23468
  }
23341
- return follow(row, { provider, name, fresh, billing, billingUrl, undoRole, note, place }, opts, deps, progress);
23469
+ return follow(row, { provider, name, fresh, billing, billingUrl, undoRole, note, place, url }, opts, deps, progress);
23470
+ }
23471
+ var reservedStatus = (status) => status === "ip_reserving" || status === "ip_reserved";
23472
+ var IP_POLL_MS = 3000;
23473
+ var IP_WAIT_MS = 120000;
23474
+ async function waitForIp(id, deps) {
23475
+ for (const deadline = deps.now() + IP_WAIT_MS;; ) {
23476
+ const row = (await deps.list().catch(() => [])).find((d) => d.id === id);
23477
+ if (row && row.status !== "ip_reserving")
23478
+ return row;
23479
+ if (deps.now() >= deadline)
23480
+ return;
23481
+ await deps.sleep(IP_POLL_MS);
23482
+ }
23483
+ }
23484
+ var continueCommand = (opts, url) => opts.agent ? "call deploy_monitoring again with ip_allowed: true" : `pgai mon deploy ${url ? quoteArg(maskConnectionString(url)) : "<database URL>"} --yes --ip-allowed`;
23485
+ var quoteArg = (s) => /^[A-Za-z0-9_@%+=:,./-]+$/.test(s) ? s : `'${s.replace(/'/g, "'\\''")}'`;
23486
+ var heldUntil = (iso) => iso ? iso.replace(/T(\d\d:\d\d).*$/, " $1 UTC") : "";
23487
+ function allowIpResult(row, provider, name, opts, url, stopped = false) {
23488
+ const [host, port] = row.cluster ? [row.cluster.replace(/:\d+$/, ""), Number(row.cluster.match(/:(\d+)$/)?.[1] ?? 5432)] : [undefined, undefined];
23489
+ const ip = row.egress_ip ?? undefined;
23490
+ const cidr = ip ? `${ip}/32` : undefined;
23491
+ const allow = `Allow inbound TCP ${port ?? 5432} from ${cidr} on ${host ?? "the database"}`;
23492
+ return {
23493
+ status: "action_required",
23494
+ step: "allow_ip",
23495
+ provider,
23496
+ name,
23497
+ id: row.id,
23498
+ ...row.cloud ? { cloud: row.cloud, location: row.location ?? undefined } : {},
23499
+ ip,
23500
+ cidr,
23501
+ host,
23502
+ port,
23503
+ ...row.reserved_until ? { reserved_until: row.reserved_until } : {},
23504
+ ...row.last_error ? { last_error: row.last_error } : {},
23505
+ ...row.precheck_error ? { precheck_error: row.precheck_error } : {},
23506
+ egress_ip: ip,
23507
+ next: stopped ? `Stopped. ${ip} stays reserved until ${heldUntil(row.reserved_until)}. ${allow}, then: ${continueCommand(opts, url)}` : `${allow}, then ${opts.agent ? continueCommand(opts, url) : `re-run with --yes --ip-allowed: ${continueCommand(opts, url)}`}`
23508
+ };
23509
+ }
23510
+ async function allowIpStep(row, c) {
23511
+ if (row.status === "ip_reserving")
23512
+ row = await waitForIp(row.id, c.deps) ?? row;
23513
+ if (!reservedStatus(row.status) || !row.egress_ip) {
23514
+ return {
23515
+ status: "action_required",
23516
+ provider: c.provider,
23517
+ name: c.name,
23518
+ id: row.id,
23519
+ next: row.status === "ip_reserving" ? `The IP is still being reserved: re-run in a minute, or see pgai mon instances list` : `See pgai mon instances list (${row.status ?? "unknown"})`
23520
+ };
23521
+ }
23522
+ const r = allowIpResult(row, c.provider, c.name, c.opts, c.url);
23523
+ c.progress("allow_ip", `Reserved IP for the monitoring box: ${r.ip}`, { ip: r.ip, cidr: r.cidr, host: r.host, port: r.port, reserved_until: r.reserved_until, id: row.id });
23524
+ if (!c.deps.allowIp)
23525
+ return r;
23526
+ const why = row.last_error ? `The box could not connect from ${r.ip}: ${row.last_error}` : c.opts.ipFirst ? "You asked for the IP first (--ip-first)." : !row.precheck_error ? "The IP was reserved first, as asked." : `PostgresAI could not connect to ${r.host}:${r.port} (${row.precheck_error}). The database likely allows only listed IPs.`;
23527
+ const answer = await c.deps.allowIp([
23528
+ `Monitoring box IP: ${r.ip}${row.cloud ? ` (${cloudWord(row.cloud)}, ${row.location})` : ""}`,
23529
+ why,
23530
+ `Allow inbound TCP ${r.port} from ${r.cidr} on ${r.host}, then continue. The IP is held until ${heldUntil(row.reserved_until)} and stays this box's IP.`
23531
+ ]);
23532
+ return answer === "continue" ? "continue" : allowIpResult(row, c.provider, c.name, c.opts, c.url, true);
23342
23533
  }
23343
23534
  async function placeBox(deps, opts, where, progress) {
23344
23535
  if (!deps.deployOptions)
@@ -23652,8 +23843,36 @@ async function supabaseBox(ref, name, opts, deps, progress, stop, label) {
23652
23843
  return follow(box, { provider, name, fresh: true, billing: b.billing, billingUrl: b.billingUrl, note: "", place }, opts, deps, progress);
23653
23844
  }
23654
23845
  var STILL_IN_PROGRESS = "Still in progress";
23846
+ var CONNECT_TIMEOUT_MS = 55000;
23847
+ var AGENT_CONNECT_TIMEOUT_MS = 25000;
23848
+ var STILL_CREATING = "Still creating the monitoring box";
23849
+ var NOT_CONFIRMED = "PostgresAI did not confirm the monitoring box within 5 minutes. Run pgai mon instances list: if this database is there, follow it with pgai mon instances watch <id>; if not, run pgai mon deploy again";
23850
+ var STOPPED_LOOKING = `${STILL_IN_PROGRESS}: deploy stopped watching before PostgresAI confirmed the monitoring box; it may still appear. Run pgai mon instances list before running deploy again`;
23851
+ var NOT_CONFIRMED_AGENT = "PostgresAI did not confirm the monitoring box yet. Call deploy_monitoring again in a minute: it continues with the box if PostgresAI created it. If pgai mon instances list still has no box for this database, ask the user to run pgai mon deploy with --reset-password in a terminal";
23852
+ var LOOKUP_MS = 5 * 60000;
23853
+ var AGENT_RUN_MS = 50000;
23854
+ function mayHaveRun(err) {
23855
+ if (err instanceof HttpRequestTimeoutError)
23856
+ return true;
23857
+ if (err instanceof HttpStatusError)
23858
+ return err.status >= 500 && !err.sqlstate;
23859
+ const message = err instanceof Error ? err.message : "";
23860
+ return / could not reach /.test(message) && !/\((ECONNREFUSED|ENOTFOUND|EAI_AGAIN|ENETUNREACH|EHOSTUNREACH|ETIMEDOUT|UND_ERR_CONNECT_TIMEOUT|CERT_\w+|ERR_TLS_\w+|UNABLE_TO_\w+|DEPTH_ZERO_\w+|SELF_SIGNED_\w+)\)/.test(message);
23861
+ }
23862
+ async function lookForBox(findMade, opts, deps, started) {
23863
+ for (const deadline = opts.agent ? started + AGENT_RUN_MS : deps.now() + LOOKUP_MS;; ) {
23864
+ const made = findMade(await deps.list().catch(() => []));
23865
+ if (made)
23866
+ return made;
23867
+ const now = deps.now();
23868
+ if (deps.aborted?.() || now >= deadline)
23869
+ return;
23870
+ await deps.sleep(Math.min(POLL_MS, deadline - now));
23871
+ }
23872
+ }
23655
23873
  async function follow(row, f, opts, deps, progress) {
23656
23874
  const { provider, name, fresh, billingUrl, undoRole, note } = f;
23875
+ const url = (_) => f.url ?? name;
23657
23876
  const billing = { ...f.billing, ...f.place };
23658
23877
  let shown;
23659
23878
  for (const deadline = deps.now() + opts.waitMs;; ) {
@@ -23669,10 +23888,16 @@ async function follow(row, f, opts, deps, progress) {
23669
23888
  next: chargeFailed(row.billing_error, "re-run", billingUrl, opts.agent ? "coupon" : "--coupon")
23670
23889
  };
23671
23890
  }
23891
+ if (row.status === "ip_reserved") {
23892
+ if (undoRole)
23893
+ await undoRole();
23894
+ return { ...allowIpResult(row, provider, name, opts, url(row)), ...billing };
23895
+ }
23672
23896
  const result = connectStatus(row, provider, fresh);
23673
23897
  const state = boxState(row);
23674
- if (state !== shown && (shown !== undefined || result.status === "in_progress"))
23675
- progress("box", `Monitoring box: ${boxWords(state)}`, { state, id: row.id });
23898
+ if (state !== shown && (shown !== undefined || result.status === "in_progress")) {
23899
+ progress("box", state === "waiting_for_access" && row.egress_ip ? `Monitoring box: waiting for database access from ${row.egress_ip}${row.last_error ? ` (${row.last_error})` : ""}` : `Monitoring box: ${boxWords(state)}`, { state, id: row.id, ...state === "waiting_for_access" && row.egress_ip ? { ip: row.egress_ip, ...row.last_error ? { error: row.last_error } : {} } : {} });
23900
+ }
23676
23901
  shown = state;
23677
23902
  if (result.status !== "in_progress" || deps.now() >= deadline) {
23678
23903
  const final = deps.links ? connectStatus(await deps.links(row), provider, fresh) : result;
@@ -23834,7 +24059,7 @@ async function prepareDatabase(url, provider, opts = {}) {
23834
24059
  const noTls = refusedTls || !usedSsl && me.ssl === "off";
23835
24060
  const tlsOnly = `PostgresAI sends the password it keeps for ${role} only over TLS`;
23836
24061
  const others = opts.others?.length ? opts.others : undefined;
23837
- const disconnectOthers = others ? disconnectEach(others) : "pgai mon instances delete the server's other databases (pgai mon instances list)";
24062
+ const disconnectOthers = others ? disconnectEach(opts.otherRefs ?? others) : "pgai mon instances delete the server's other databases (pgai mon instances list)";
23838
24063
  const reconnectOthers = `(and deploy ${others ? others.join(", ") : "them"} again with it)`;
23839
24064
  const noTlsWayOut = opts.agent ? `Pass its URL as database_url, or run pgai mon deploy in a terminal with PGAI_MON_PASSWORD set to its password. If nobody has it, in a terminal: ${disconnectOthers}, then pgai mon deploy <admin-url> --reset-password with PGAI_MON_PASSWORD set to a new one ${reconnectOthers}` : `Set PGAI_MON_PASSWORD to its password, or turn on TLS on the server (ssl = on) and put sslmode=require in the URL. If nobody has the password: ${disconnectOthers}, then re-run with --reset-password and PGAI_MON_PASSWORD set to a new one ${reconnectOthers}`;
23840
24065
  const reuse = me.mon_exists && !opts.resetPassword && !!opts.storedPassword && !(opts.agent ? "" : process.env.PGAI_MON_PASSWORD?.trim());
@@ -23946,7 +24171,16 @@ function grafanaOAuthUrl(dashboardUrl) {
23946
24171
  return `${u.origin}/login/generic_oauth?redirectTo=${encodeURIComponent(`${u.pathname}${u.search}`)}`;
23947
24172
  }
23948
24173
  function platformDeps(p) {
23949
- const rpc = (fn, body = {}) => callRpc({ apiKey: p.apiKey, apiBaseUrl: p.apiBaseUrl, fn, body, operation: fn.replace(/_/g, " "), debug: p.debug, orgScope: p.orgScope });
24174
+ const rpc = (fn, body = {}, timeoutMs) => callRpc({
24175
+ apiKey: p.apiKey,
24176
+ apiBaseUrl: p.apiBaseUrl,
24177
+ fn,
24178
+ body,
24179
+ operation: fn.replace(/_/g, " "),
24180
+ debug: p.debug,
24181
+ orgScope: p.orgScope,
24182
+ ...timeoutMs ? { timeoutMs, timeoutCeilingMs: timeoutMs } : {}
24183
+ });
23950
24184
  let orgAlias;
23951
24185
  let projects;
23952
24186
  const healthUrl = async (row) => {
@@ -23963,14 +24197,43 @@ function platformDeps(p) {
23963
24197
  orgAlias = projects = undefined;
23964
24198
  return;
23965
24199
  });
23966
- const { dashboard_url, host_metrics, ...rest } = row;
23967
- return { ...rest, ...health_url ? { health_url } : {}, dashboard_url: grafanaOAuthUrl(dashboard_url), host_metrics };
24200
+ const { dashboard_url, host_metrics, host_metrics_source, ...rest } = row;
24201
+ return {
24202
+ ...rest,
24203
+ ...health_url ? { health_url } : {},
24204
+ dashboard_url: grafanaOAuthUrl(dashboard_url),
24205
+ host_metrics,
24206
+ ...host_metrics_source !== undefined ? { host_metrics_source } : {}
24207
+ };
23968
24208
  };
23969
24209
  return {
23970
24210
  verifyTls: verifyCollectorTls,
23971
24211
  list: () => rpc("cloud_monitoring_list"),
23972
24212
  links: withLinks,
23973
- create: (body) => rpc("cloud_monitoring_connect", body),
24213
+ create: async (body) => {
24214
+ const { allow_ip_fallback: _fallback, ip_first, ip_allowed, ...older } = body;
24215
+ const asked = !!(ip_first || ip_allowed);
24216
+ const connectMs = p.agent ? AGENT_CONNECT_TIMEOUT_MS : CONNECT_TIMEOUT_MS;
24217
+ const connectTimeout = { timeoutMs: connectMs, timeoutCeilingMs: connectMs };
24218
+ try {
24219
+ return await callRpc({
24220
+ apiKey: p.apiKey,
24221
+ apiBaseUrl: p.apiBaseUrl,
24222
+ fn: "cloud_monitoring_connect",
24223
+ body,
24224
+ operation: "cloud monitoring connect",
24225
+ debug: p.debug,
24226
+ orgScope: p.orgScope,
24227
+ ...connectTimeout,
24228
+ ...asked ? {} : { fallbackBody: older }
24229
+ });
24230
+ } catch (err) {
24231
+ if (asked && err instanceof HttpStatusError && err.status === 404 && /cloud_monitoring_connect/.test(err.message)) {
24232
+ throw new Error("This PostgresAI platform does not support --ip-first or --ip-allowed yet: deploy without them");
24233
+ }
24234
+ throw err;
24235
+ }
24236
+ },
23974
24237
  disconnect: (id) => rpc("cloud_monitoring_disconnect", { instance_id: id }),
23975
24238
  setVcpus: (id, vcpus) => rpc("cloud_monitoring_set_vcpus", { instance_id: id, vcpus }),
23976
24239
  resetLock: (server) => rpc("cloud_monitoring_reset_lock", { server }),
@@ -30684,6 +30947,8 @@ async function handleToolCall(req, rootOpts, extra) {
30684
30947
  monitoringUser: args.monitoring_user === undefined || args.monitoring_user === null ? undefined : String(args.monitoring_user),
30685
30948
  cloud: args.cloud === undefined || args.cloud === null ? undefined : String(args.cloud),
30686
30949
  location: args.location === undefined || args.location === null ? undefined : String(args.location),
30950
+ ipFirst: args.ip_first === true,
30951
+ ipAllowed: args.ip_allowed === true,
30687
30952
  waitMs: 0,
30688
30953
  agent: true
30689
30954
  }, {
@@ -31258,7 +31523,7 @@ async function startMcpServer(rootOpts, extra) {
31258
31523
  },
31259
31524
  {
31260
31525
  name: "deploy_monitoring",
31261
- description: "Put a Postgres database under PostgresAI Cloud monitoring (same as `pgai mon deploy`): prepares the monitoring role (an admin URL creates it; otherwise returns the SQL), provisions the monitoring box, and returns JSON with status (ready | in_progress | deleting | deleted | inactive | action_required | failed: the same words as pgai dblab deploy), health_url (the project's Health Matrix in the console), dashboard_url (Grafana, signing in with PostgresAI) and next (the exact next action). A billed box is provisioned only with yes: true: without it the result is action_required with the price (and requires_payment_method), so show the price to the user first. It does not wait for the box: call it again to see the status. Safe to call again. ClickHouse Managed Postgres, RDS and Supabase are detected from the host. Supabase needs no password (PostgresAI prepares the database through the Supabase Management API): the first time, the result is action_required with a consent link in next; give it to the user to open and approve, then call again. Elsewhere the URL must carry its password, and only these query parameters: sslmode, channel_binding, application_name. PGAI_MON_PASSWORD, PGPASSWORD and CLICKHOUSE_KEY_ID + CLICKHOUSE_KEY_SECRET of the server process are not used (pass clickhouse_key), and a TLS failure is not retried in plaintext (say sslmode=disable for a server without TLS). For another database on a server PostgresAI already monitors for the org, an admin URL with sslmode=require (or verify-*), each query parameter once, is enough: PostgresAI fills in the password of postgres_ai_mon it keeps, over TLS only (on a server without TLS, pass the postgres_ai_mon URL; if nobody has its password, the user runs in a terminal: pgai mon instances delete the server's other databases, then pgai mon deploy <admin-url> --reset-password with PGAI_MON_PASSWORD set to a new one, then deploys them again with it).",
31526
+ description: "Put a Postgres database under PostgresAI Cloud monitoring (same as `pgai mon deploy`): prepares the monitoring role (an admin URL creates it; otherwise returns the SQL), provisions the monitoring box, and returns JSON with status (ready | in_progress | deleting | deleted | inactive | action_required | failed: the same words as pgai dblab deploy), health_url (the project's Health Matrix in the console), dashboard_url (Grafana, signing in with PostgresAI) and next (the exact next action). A billed box is provisioned only with yes: true: without it the result is action_required with the price (and requires_payment_method), so show the price to the user first. It does not wait for the box: call it again to see the status. Safe to call again. When PostgresAI can't reach the database the way an IP allowlist refuses (or with ip_first), the box's IP is reserved first and the result is action_required with step allow_ip (ip, cidr, host, port, reserved_until): give the user the cidr and port to allow on the database, then call again with ip_allowed: true. ClickHouse Managed Postgres, RDS and Supabase are detected from the host. Supabase needs no password (PostgresAI prepares the database through the Supabase Management API): the first time, the result is action_required with a consent link in next; give it to the user to open and approve, then call again. Elsewhere the URL must carry its password, and only these query parameters: sslmode, channel_binding, application_name. PGAI_MON_PASSWORD, PGPASSWORD and CLICKHOUSE_KEY_ID + CLICKHOUSE_KEY_SECRET of the server process are not used (pass clickhouse_key), and a TLS failure is not retried in plaintext (say sslmode=disable for a server without TLS). For another database on a server PostgresAI already monitors for the org, an admin URL with sslmode=require (or verify-*), each query parameter once, is enough: PostgresAI fills in the password of postgres_ai_mon it keeps, over TLS only (on a server without TLS, pass the postgres_ai_mon URL; if nobody has its password, the user runs in a terminal: pgai mon instances delete the server's other databases, then pgai mon deploy <admin-url> --reset-password with PGAI_MON_PASSWORD set to a new one, then deploys them again with it).",
31262
31527
  inputSchema: {
31263
31528
  type: "object",
31264
31529
  properties: {
@@ -31272,6 +31537,8 @@ async function startMcpServer(rootOpts, extra) {
31272
31537
  coupon: { type: "string", description: "Stripe promotion code for the organization's monitoring subscription (applies when it is first created)" },
31273
31538
  vcpus: { type: "integer", minimum: 1, maximum: 1024, description: "vCPUs of the database server. Without it there is no AAS (database load) data for this database: ask the user for it" },
31274
31539
  monitoring_user: { type: "string", description: "Monitoring role to create or use (default postgres_ai_mon). A custom role always carries its own password: PostgresAI keeps only postgres_ai_mon's, so pass the role's own URL as database_url (an admin URL creates only postgres_ai_mon). Not for Supabase, where PostgresAI creates postgres_ai_mon" },
31540
+ ip_first: { type: "boolean", description: "Reserve the monitoring box's IP and stop before the box (even when PostgresAI can reach the database), so the user can allow it on the database first. The result is action_required with step allow_ip: ip, cidr, host, port, reserved_until" },
31541
+ ip_allowed: { type: "boolean", description: "The database now allows the reserved IP (a previous result's step allow_ip): create the box with it. Give the user the cidr and port to allow first, and call with ip_allowed: true only once they confirm" },
31275
31542
  debug: { type: "boolean", description: "Enable verbose debug logs" }
31276
31543
  },
31277
31544
  required: ["database_url"],
@@ -32174,6 +32441,7 @@ function renderMarkdownForTerminal(md) {
32174
32441
  }
32175
32442
 
32176
32443
  // lib/connect.ts
32444
+ var clickhouseKeyHeld = (d) => d.host_metrics && (d.host_metrics_source === undefined || d.host_metrics_source === "clickhouse");
32177
32445
  function httpStatus2(err) {
32178
32446
  const status = err instanceof Error ? err.status : undefined;
32179
32447
  return typeof status === "number" ? status : undefined;
@@ -32320,6 +32588,7 @@ function storedPasswordNeeds2(monitoringUrl) {
32320
32588
  return needs.length ? needs.join(" and ") : undefined;
32321
32589
  }
32322
32590
  var shellWord2 = (s) => /^[\w./:@%+=,-]+$/.test(s) ? s : `'${s.replace(/'/g, "'\\''")}'`;
32591
+ var deleteRef2 = (d, list) => list.some((o) => o.id !== d.id && !disconnecting2(o.status) && o.name === d.name) ? d.id : d.name;
32323
32592
  var disconnectEach2 = (names) => names.map((n) => `pgai mon instances delete ${shellWord2(n)} --yes`).join(" and ");
32324
32593
  function loginName2(url, sessionUser, role) {
32325
32594
  const user = decodeURIComponent(new URL(url).username);
@@ -32362,6 +32631,7 @@ function connectStatus2(row, provider = row.provider, fresh = false) {
32362
32631
  return {
32363
32632
  ...base,
32364
32633
  status,
32634
+ ...row.egress_ip ? { egress_ip: row.egress_ip } : {},
32365
32635
  ...fresh ? { first_checkup_eta: new Date(Date.now() + FIRST_CHECKUP_DELAY_MS2).toISOString() } : {},
32366
32636
  next: row.health_url || row.dashboard_url ? `Open ${row.health_url || row.dashboard_url}` : `pgai mon instances watch ${row.id}`
32367
32637
  };
@@ -32370,6 +32640,8 @@ function connectStatus2(row, provider = row.provider, fresh = false) {
32370
32640
  return { ...base, status, next: "none" };
32371
32641
  if (status === "failed")
32372
32642
  return { ...base, status, next: `pgai mon instances delete ${row.id} --yes, then pgai mon deploy again` };
32643
+ if (row.status === "ip_reserved" && row.egress_ip)
32644
+ return { ...allowIpResult2(row, provider, row.name, { waitMs: 0 }, ""), ...base, status: "action_required", step: "allow_ip" };
32373
32645
  return { ...base, status, next: `pgai mon instances watch ${row.id}` };
32374
32646
  }
32375
32647
  var ESCAPES2 = /(?:\x1b\[|\x9b)[0-?]*[ -\/]*[@-~]|(?:\x1b\]|\x9d)[^\x07\x1b\x9c]*(?:\x07|\x1b\\|\x9c)?|\x1b[ -~]?/g;
@@ -32379,12 +32651,19 @@ var terminalText2 = (text) => text.replace(ESCAPES2, "").split(`
32379
32651
  function stepProgress(o) {
32380
32652
  let view;
32381
32653
  let priced = false;
32654
+ let refused = "";
32382
32655
  const progress = (e) => {
32383
- if (e.id)
32656
+ if (e.id && e.event !== "allow_ip")
32384
32657
  o.onId?.(e.id);
32385
32658
  if (o.json)
32386
32659
  return o.writeEvent(e);
32387
- e = { ...e, message: terminalText2(e.message), ...e.state ? { state: terminalText2(e.state) } : {} };
32660
+ e = {
32661
+ ...e,
32662
+ message: terminalText2(e.message),
32663
+ ...e.state ? { state: terminalText2(e.state) } : {},
32664
+ ...e.ip ? { ip: terminalText2(e.ip) } : {},
32665
+ ...e.error ? { error: terminalText2(e.error) } : {}
32666
+ };
32388
32667
  if (!o.tty)
32389
32668
  return o.writeLine(progressText(e));
32390
32669
  if (e.event === "billing") {
@@ -32400,10 +32679,26 @@ function stepProgress(o) {
32400
32679
  view.advance(e.event);
32401
32680
  if (/^(Note|Warning):/.test(e.message))
32402
32681
  view.log(e.message);
32403
- if (e.event === "box" && e.state)
32404
- view.setNote(`box: ${boxWords2(e.state)}`);
32682
+ if (e.event === "box" && e.state) {
32683
+ view.setNote(e.state === "waiting_for_access" && e.ip ? `box: waiting for database access from ${e.ip}` : `box: ${boxWords2(e.state)}`);
32684
+ if (e.state === "waiting_for_access" && e.ip && e.error && e.error !== refused) {
32685
+ refused = e.error;
32686
+ view.log(`The database refused ${e.ip}: ${e.error}. The box keeps trying: allow ${e.ip}/32 on the database.`);
32687
+ }
32688
+ }
32689
+ if (e.message === STILL_CREATING2)
32690
+ view.setNote(`still creating: PostgresAI is slow to answer (checking for up to ${LOOKUP_MS2 / 60000} min)`);
32405
32691
  };
32406
- return { progress, view: () => view };
32692
+ return { progress, view: () => view, reset: () => {
32693
+ view = undefined;
32694
+ } };
32695
+ }
32696
+ function stepViewEnd(r) {
32697
+ if (r.status === "ready")
32698
+ return "complete";
32699
+ if (r.status === "in_progress" || r.step === "allow_ip" || r.next.startsWith(STILL_IN_PROGRESS2))
32700
+ return "stop";
32701
+ return "fail";
32407
32702
  }
32408
32703
  function resultLines(r) {
32409
32704
  return personLines(r).map(terminalText2);
@@ -32421,6 +32716,7 @@ function personLines(r) {
32421
32716
  ...r.dashboard_url ? [` Grafana: ${r.dashboard_url}`] : [],
32422
32717
  ...r.price ? [` Price: ${r.price}`] : [],
32423
32718
  ...r.cloud ? [` Runs on: ${cloudWord2(r.cloud)}, ${r.location}`] : [],
32719
+ ...r.egress_ip ? [` Connects from: ${r.egress_ip}`] : [],
32424
32720
  ...!r.health_url && !r.dashboard_url && r.next && r.next !== "none" ? [`Next: ${r.next}`] : [],
32425
32721
  ...extra && (r.health_url || r.dashboard_url) ? [`Note: ${extra}`] : []
32426
32722
  ];
@@ -32430,6 +32726,15 @@ function personLines(r) {
32430
32726
  case "deleted":
32431
32727
  return [`Deleting the monitoring of ${who}.${billing ? ` Billing: ${billing}.` : ""}${r.next && r.next !== "none" ? ` ${r.next}` : ""}`];
32432
32728
  default:
32729
+ if (r.step === "allow_ip") {
32730
+ if (r.next.startsWith("Stopped."))
32731
+ return [r.next];
32732
+ return [
32733
+ `Monitoring box IP: ${r.ip}${r.cloud ? ` (${cloudWord2(r.cloud)}, ${r.location})` : ""}${r.reserved_until ? `, held until ${heldUntil2(r.reserved_until)}` : ""}`,
32734
+ r.last_error ? `The box could not connect: ${r.last_error}` : r.precheck_error ? `PostgresAI could not connect to ${r.host}:${r.port} (${r.precheck_error}).` : "The IP was reserved first, as asked.",
32735
+ r.next
32736
+ ];
32737
+ }
32433
32738
  if (r.projects?.length)
32434
32739
  return [r.next, ...supabaseProjectRows(r.projects)];
32435
32740
  return r.choices ? [r.next, ...Object.entries(r.choices).map(([cloud, codes]) => ` --cloud ${cloud} --location ${codes.join("|")}`)] : [r.next];
@@ -32450,6 +32755,12 @@ function boxWords2(state) {
32450
32755
  return "starting";
32451
32756
  if (state === "registered")
32452
32757
  return "installing monitoring";
32758
+ if (state === "waiting_for_access")
32759
+ return "waiting for database access";
32760
+ if (state === "ip_reserving")
32761
+ return "reserving its IP";
32762
+ if (state === "ip_reserved")
32763
+ return "waiting for its IP to be allowed";
32453
32764
  return monitoringStatus(state).replace("_", " ");
32454
32765
  }
32455
32766
  async function billingFor2(q, name, opts, deps, show, ask) {
@@ -32499,6 +32810,9 @@ async function connect2(url, opts, deps) {
32499
32810
  if (project !== undefined && provider !== "supabase") {
32500
32811
  return { status: "action_required", provider, name: url ? databaseName3(url) : "", next: "--supabase-project applies to Supabase only: leave it out, or use --provider supabase" };
32501
32812
  }
32813
+ if ((opts.ipFirst || opts.ipAllowed) && (opts.selfHosted || provider === "supabase")) {
32814
+ return { status: "action_required", provider, name: url ? databaseName3(url) : "", next: `${opts.agent ? "ip_first / ip_allowed apply" : "--ip-first and --ip-allowed apply"} to PostgresAI Cloud monitoring of a database URL only` };
32815
+ }
32502
32816
  if (!url) {
32503
32817
  if (provider !== "supabase")
32504
32818
  return { status: "action_required", provider, name: "", next: "Pass the database URL: pgai mon deploy postgresql://user:password@host:5432/dbname (Supabase: --provider supabase)" };
@@ -32550,12 +32864,31 @@ async function connect2(url, opts, deps) {
32550
32864
  if (opts.selfHosted && deps.localStackRunning()) {
32551
32865
  return { status: "action_required", provider, name, next: "A monitoring stack already runs on this machine: add the database with PGAI_DB_URL='<postgres_ai_mon URL>' pgai mon targets add" };
32552
32866
  }
32867
+ const byCluster = (d) => !!d.cluster && serverOf2(d.cluster) === cluster && d.database != null && decodeURIComponent(d.database) === name.split("/").slice(1).join("/");
32868
+ const byName = (d) => (d.cluster == null || d.database == null) && d.name === name;
32553
32869
  const findDatabase = (list) => {
32554
32870
  const live = list.filter((d) => !disconnecting2(d.status));
32555
- return live.find((d) => d.cluster && serverOf2(d.cluster) === cluster && d.database != null && decodeURIComponent(d.database) === name.split("/").slice(1).join("/")) ?? live.find((d) => (d.cluster == null || d.database == null) && d.name === name);
32871
+ return live.find(byCluster) ?? live.find(byName);
32556
32872
  };
32557
32873
  const rows = opts.selfHosted ? [] : await deps.list();
32874
+ const known = new Map(rows.map((d) => [d.id, d.status]));
32875
+ const findMade = (list) => {
32876
+ const fresh = list.filter((d) => !known.has(d.id) || reservedStatus2(known.get(d.id)) && !reservedStatus2(d.status));
32877
+ return fresh.find(byCluster) ?? fresh.find(byName);
32878
+ };
32558
32879
  let row = findDatabase(rows);
32880
+ let continuing = false;
32881
+ let ownId;
32882
+ if (row && reservedStatus2(row.status)) {
32883
+ if (!opts.ipAllowed) {
32884
+ const asked = await allowIpStep2(row, { provider, name, url, opts, deps, progress });
32885
+ if (asked !== "continue")
32886
+ return asked;
32887
+ }
32888
+ continuing = true;
32889
+ ownId = row.id;
32890
+ row = undefined;
32891
+ }
32559
32892
  const fresh = !row;
32560
32893
  let note = "";
32561
32894
  let billing = {};
@@ -32596,7 +32929,7 @@ async function connect2(url, opts, deps) {
32596
32929
  }
32597
32930
  }
32598
32931
  if (row && opts.resetPassword) {
32599
- return { status: "action_required", provider, name, id: row.id, next: `${name} is already monitored, and its monitoring uses the current password: pgai mon instances delete ${name} --yes first` };
32932
+ return { status: "action_required", provider, name, id: row.id, next: `${row.name} is already monitored, and its monitoring uses the current password. Delete it, then run this deploy again with --reset-password: ${disconnectEach2([deleteRef2(row, rows)])}` };
32600
32933
  }
32601
32934
  if (row && key) {
32602
32935
  const found = await findOrg();
@@ -32624,26 +32957,30 @@ async function connect2(url, opts, deps) {
32624
32957
  let lockId;
32625
32958
  const unread = (d) => !d.cluster;
32626
32959
  const onServer = (d) => !!d.cluster && serverOf2(d.cluster) === cluster;
32627
- const othersOnServer = (list) => list.filter((d) => !disconnecting2(d.status) && (unread(d) || onServer(d)));
32960
+ const usesRole = (d) => !["deleting", "deleted", "inactive"].includes(stateOf2(d.status)) && !(d.status === "failed" && !d.registered_at);
32961
+ const othersOnServer = (list) => list.filter((d) => d.id !== ownId && usesRole(d) && (unread(d) || onServer(d)));
32628
32962
  const storedPassword = !monitoringUser && rows.some((d) => d.monitoring_password_stored && !/delet/.test(d.status ?? "") && onServer(d)) || undefined;
32629
- const cutOff = (others) => {
32630
- const on = others.filter((d) => !unread(d)).map((d) => d.name).join(", ");
32631
- const maybe = others.filter(unread).map((d) => d.name).join(", ");
32963
+ const cutOff = (others, list) => {
32964
+ const label = (d) => deleteRef2(d, list) === d.id ? `${d.name} (id ${d.id})` : d.name;
32965
+ const on = others.filter((d) => !unread(d)).map(label).join(", ");
32966
+ const maybe = others.filter(unread).map(label).join(", ");
32632
32967
  const what = [on && `would cut off the monitoring of ${on} on this server`, maybe && `may cut off the monitoring of ${maybe} (the platform could not read its URL, so its server is not known)`].filter(Boolean).join(", and ");
32633
32968
  const instead = [
32634
32969
  ...storedPassword ? ["deploy without --reset-password (PostgresAI keeps its password for this server, and sends it only over TLS)"] : [],
32635
32970
  "set PGAI_MON_PASSWORD to its password instead",
32636
- `${disconnectEach2(others.map((d) => d.name))} first`
32971
+ `${disconnectEach2(others.map((d) => deleteRef2(d, list)))} first`
32637
32972
  ];
32638
32973
  return { status: "action_required", provider, name, next: `A new password for ${role} ${what}: ${instead.join(", or ")}` };
32639
32974
  };
32640
- const others = othersOnServer(rows).filter((d) => !unread(d)).map((d) => d.name);
32975
+ const onThisServer = othersOnServer(rows).filter((d) => !unread(d));
32976
+ const others = onThisServer.map((d) => d.name);
32977
+ const otherRefs = onThisServer.map((d) => deleteRef2(d, rows));
32641
32978
  if (opts.resetPassword && othersOnServer(rows).length)
32642
- return cutOff(othersOnServer(rows));
32979
+ return cutOff(othersOnServer(rows), rows);
32643
32980
  if (opts.coupon !== undefined && !opts.coupon.trim()) {
32644
32981
  return { status: "action_required", provider, name, next: `${opts.agent ? "coupon" : "--coupon"} is empty: pass a promotion code, or leave ${opts.agent ? "coupon" : "--coupon"} out` };
32645
32982
  }
32646
- const prepOpts = { resetPassword: opts.resetPassword, warn, ...storedPassword ? { storedPassword, others } : {}, ...monitoringUser ? { monitoringUser } : {} };
32983
+ const prepOpts = { resetPassword: opts.resetPassword, warn, ...storedPassword ? { storedPassword, others, otherRefs } : {}, ...monitoringUser ? { monitoringUser } : {} };
32647
32984
  if (!opts.selfHosted) {
32648
32985
  const probe = await deps.prepare(url, provider, { ...prepOpts, check: true }).catch((err) => {
32649
32986
  if (err instanceof HttpStatusError || /Could not connect to/.test(errorText2(err)))
@@ -32707,12 +33044,12 @@ async function connect2(url, opts, deps) {
32707
33044
  try {
32708
33045
  if (lockId) {
32709
33046
  const now = await deps.list();
32710
- const same = findDatabase(now);
33047
+ const same = findDatabase(now.filter((d) => d.id !== ownId));
32711
33048
  if (same)
32712
- return { status: "action_required", provider, name, id: same.id, ...billing, next: `${name} is already monitored, and its monitoring uses the current password: pgai mon instances delete ${name} --yes first` };
33049
+ return { status: "action_required", provider, name, id: same.id, ...billing, next: `${same.name} is already monitored, and its monitoring uses the current password. Delete it, then run this deploy again with --reset-password: ${disconnectEach2([deleteRef2(same, now)])}` };
32713
33050
  const others = othersOnServer(now);
32714
33051
  if (others.length)
32715
- return { ...cutOff(others), ...billing };
33052
+ return { ...cutOff(others, now), ...billing };
32716
33053
  }
32717
33054
  if (deps.aborted?.())
32718
33055
  throw new CancelledError2;
@@ -32723,6 +33060,16 @@ async function connect2(url, opts, deps) {
32723
33060
  throw new CancelledError2;
32724
33061
  }
32725
33062
  if ("next" in prepared) {
33063
+ if ((continuing || opts.ipAllowed) && /created by someone else meanwhile/.test(prepared.next)) {
33064
+ for (let tries = 0;tries < 10; tries++) {
33065
+ const box = findDatabase(await deps.list());
33066
+ if (!box)
33067
+ break;
33068
+ if (!reservedStatus2(box.status))
33069
+ return follow2(box, { provider, name, fresh, billing, billingUrl, undoRole, note, place, url }, opts, deps, progress);
33070
+ await deps.sleep(2000);
33071
+ }
33072
+ }
32726
33073
  const { resettable, ...refusal } = prepared;
32727
33074
  return { status: "action_required", provider, name, ...billing, ...refusal };
32728
33075
  }
@@ -32735,8 +33082,12 @@ async function connect2(url, opts, deps) {
32735
33082
  return { status: "ready", provider, name, dashboard_url: "http://localhost:3000", host_metrics: !!ch, next: `pgai mon health${note}` };
32736
33083
  }
32737
33084
  progress("provisioning", `Provisioning monitoring for ${name}`);
32738
- created = await deps.create({
32739
- db_url: prepared.monitoringUrl,
33085
+ const monitoringUrl = prepared.monitoringUrl;
33086
+ const request = (ipAllowed) => deps.create({
33087
+ db_url: monitoringUrl,
33088
+ allow_ip_fallback: true,
33089
+ ...opts.ipFirst ? { ip_first: true } : {},
33090
+ ...ipAllowed ? { ip_allowed: true } : {},
32740
33091
  ...provider === "clickhouse" ? { provider } : {},
32741
33092
  ...key && ch ? { clickhouse_org_id: ch.orgId, clickhouse_key_id: key.keyId, clickhouse_key_secret: key.keySecret } : {},
32742
33093
  ...opts.coupon ? { promo_code: opts.coupon } : {},
@@ -32744,8 +33095,39 @@ async function connect2(url, opts, deps) {
32744
33095
  ...opts.name ? { project_name: opts.name } : {},
32745
33096
  ...place ? { cloud: place.cloud, server_location: place.location } : opts.location ? { server_location: opts.location } : {},
32746
33097
  ...vcpus ? { vcpus } : {}
33098
+ });
33099
+ const refused = async (c) => {
33100
+ const row = await waitForIp2(c.id, deps) ?? { id: c.id, name: c.name, status: c.status };
33101
+ const why = [c.error, c.billing_warning].filter(Boolean).join(" ");
33102
+ return { ...c, status: "allow_ip", result: { ...allowIpResult2(row, provider, name, opts, url), next: `${why} Then: ${continueCommand2(opts, url)}` } };
33103
+ };
33104
+ const continued = async () => {
33105
+ const c = await request(true);
33106
+ return reservedStatus2(c.status) && c.error ? refused(c) : c;
33107
+ };
33108
+ created = await request(continuing || !!opts.ipAllowed).then(async (c) => {
33109
+ if (reservedStatus2(c.status))
33110
+ known.set(c.id, c.status);
33111
+ if (reservedStatus2(c.status) && c.error)
33112
+ return refused(c);
33113
+ if (!reservedStatus2(c.status))
33114
+ return c;
33115
+ const reservation = await waitForIp2(c.id, deps);
33116
+ if (!reservation || !reservedStatus2(reservation.status))
33117
+ return { ...c, status: reservation?.status ?? "failed", error: reservation ? undefined : "The IP reservation did not report back in time; see pgai mon instances list." };
33118
+ if (continuing && c.status === "ip_reserving" && reservation.status === "ip_reserved")
33119
+ return continued();
33120
+ const asked = await allowIpStep2(reservation, { provider, name, url, opts, deps, progress });
33121
+ if (asked !== "continue")
33122
+ return { ...c, status: "allow_ip", result: asked };
33123
+ return continued();
32747
33124
  }).catch(async (err) => {
32748
- if (err instanceof HttpStatusError && err.status >= 400 && err.status < 500)
33125
+ if ((continuing || opts.ipAllowed) && err instanceof HttpStatusError && err.status === 409 && /already monitored/.test(err.message)) {
33126
+ const box = findDatabase(await deps.list());
33127
+ if (box && !reservedStatus2(box.status))
33128
+ return { id: box.id, name: box.name, status: box.status ?? "launch_requested" };
33129
+ }
33130
+ if (err instanceof HttpStatusError && (err.status < 500 || err.sqlstate))
32749
33131
  await undo(prepared);
32750
33132
  else
32751
33133
  unanswered = true;
@@ -32755,8 +33137,25 @@ async function connect2(url, opts, deps) {
32755
33137
  return "price";
32756
33138
  if (err instanceof HttpStatusError && err.status === 409 && "storedPassword" in prepared && prepared.storedPassword && /none is stored/.test(err.message))
32757
33139
  return "stored";
33140
+ if (mayHaveRun2(err))
33141
+ return "unanswered";
32758
33142
  throw err;
32759
33143
  });
33144
+ if (created === "unanswered") {
33145
+ progress("provisioning", STILL_CREATING2);
33146
+ const made = await lookForBox2(findMade, opts, deps, started);
33147
+ if (!made && deps.aborted?.())
33148
+ return { status: "action_required", provider, name, ...billing, next: STOPPED_LOOKING2 };
33149
+ if (!made)
33150
+ return { status: "action_required", provider, name, ...billing, next: opts.agent ? NOT_CONFIRMED_AGENT2 : NOT_CONFIRMED2 };
33151
+ if (stateOf2(made.status) === "failed" || disconnecting2(made.status)) {
33152
+ await undo(prepared);
33153
+ unanswered = false;
33154
+ return { status: "failed", provider, name, id: made.id, next: disconnecting2(made.status) ? "PostgresAI could not launch the monitoring box. Re-run pgai mon deploy later." : `PostgresAI could not launch the monitoring box. Run pgai mon instances delete ${made.id} --yes, then pgai mon deploy again.` };
33155
+ }
33156
+ unanswered = false;
33157
+ created = { id: made.id, name: made.name ?? name, status: made.status ?? "launch_requested" };
33158
+ }
32760
33159
  if (created === "payment")
32761
33160
  return { status: "action_required", provider, name, ...billingPage };
32762
33161
  if (created === "declined") {
@@ -32768,6 +33167,12 @@ async function connect2(url, opts, deps) {
32768
33167
  const gone = `PostgresAI no longer keeps the password of ${DEFAULT_MONITORING_USER} for this server`;
32769
33168
  return { status: "action_required", provider, name, ...billing, next: opts.agent ? `${gone}: pass its URL as database_url, or run pgai mon deploy <admin-url> --reset-password in a terminal` : `${gone}: re-run with --reset-password (anything else that logs in as ${DEFAULT_MONITORING_USER} then needs the new one), or set PGAI_MON_PASSWORD to its password` };
32770
33169
  }
33170
+ if (created.status === "allow_ip" && created.result) {
33171
+ const now = findDatabase(await deps.list().catch(() => []));
33172
+ if (!now || reservedStatus2(now.status))
33173
+ await undo(prepared);
33174
+ return { ...created.result, ...billing };
33175
+ }
32771
33176
  if (created.status === "failed") {
32772
33177
  await undo(prepared);
32773
33178
  return { status: "failed", provider, name, id: created.id, next: `${created.error} Re-run pgai mon deploy later.` };
@@ -32782,7 +33187,73 @@ async function connect2(url, opts, deps) {
32782
33187
  const made = prepared;
32783
33188
  undoRole = () => undo(made);
32784
33189
  }
32785
- return follow2(row, { provider, name, fresh, billing, billingUrl, undoRole, note, place }, opts, deps, progress);
33190
+ return follow2(row, { provider, name, fresh, billing, billingUrl, undoRole, note, place, url }, opts, deps, progress);
33191
+ }
33192
+ function listedDatabase(d) {
33193
+ return { ...d, status: stateOf2(d.status), ...d.status === "ip_reserved" ? { step: "allow_ip" } : {} };
33194
+ }
33195
+ var reservedStatus2 = (status) => status === "ip_reserving" || status === "ip_reserved";
33196
+ var IP_POLL_MS2 = 3000;
33197
+ var IP_WAIT_MS2 = 120000;
33198
+ async function waitForIp2(id, deps) {
33199
+ for (const deadline = deps.now() + IP_WAIT_MS2;; ) {
33200
+ const row = (await deps.list().catch(() => [])).find((d) => d.id === id);
33201
+ if (row && row.status !== "ip_reserving")
33202
+ return row;
33203
+ if (deps.now() >= deadline)
33204
+ return;
33205
+ await deps.sleep(IP_POLL_MS2);
33206
+ }
33207
+ }
33208
+ var continueCommand2 = (opts, url) => opts.agent ? "call deploy_monitoring again with ip_allowed: true" : `pgai mon deploy ${url ? quoteArg2(maskConnectionString(url)) : "<database URL>"} --yes --ip-allowed`;
33209
+ var quoteArg2 = (s) => /^[A-Za-z0-9_@%+=:,./-]+$/.test(s) ? s : `'${s.replace(/'/g, "'\\''")}'`;
33210
+ var heldUntil2 = (iso) => iso ? iso.replace(/T(\d\d:\d\d).*$/, " $1 UTC") : "";
33211
+ function allowIpResult2(row, provider, name, opts, url, stopped = false) {
33212
+ const [host, port] = row.cluster ? [row.cluster.replace(/:\d+$/, ""), Number(row.cluster.match(/:(\d+)$/)?.[1] ?? 5432)] : [undefined, undefined];
33213
+ const ip = row.egress_ip ?? undefined;
33214
+ const cidr = ip ? `${ip}/32` : undefined;
33215
+ const allow = `Allow inbound TCP ${port ?? 5432} from ${cidr} on ${host ?? "the database"}`;
33216
+ return {
33217
+ status: "action_required",
33218
+ step: "allow_ip",
33219
+ provider,
33220
+ name,
33221
+ id: row.id,
33222
+ ...row.cloud ? { cloud: row.cloud, location: row.location ?? undefined } : {},
33223
+ ip,
33224
+ cidr,
33225
+ host,
33226
+ port,
33227
+ ...row.reserved_until ? { reserved_until: row.reserved_until } : {},
33228
+ ...row.last_error ? { last_error: row.last_error } : {},
33229
+ ...row.precheck_error ? { precheck_error: row.precheck_error } : {},
33230
+ egress_ip: ip,
33231
+ next: stopped ? `Stopped. ${ip} stays reserved until ${heldUntil2(row.reserved_until)}. ${allow}, then: ${continueCommand2(opts, url)}` : `${allow}, then ${opts.agent ? continueCommand2(opts, url) : `re-run with --yes --ip-allowed: ${continueCommand2(opts, url)}`}`
33232
+ };
33233
+ }
33234
+ async function allowIpStep2(row, c) {
33235
+ if (row.status === "ip_reserving")
33236
+ row = await waitForIp2(row.id, c.deps) ?? row;
33237
+ if (!reservedStatus2(row.status) || !row.egress_ip) {
33238
+ return {
33239
+ status: "action_required",
33240
+ provider: c.provider,
33241
+ name: c.name,
33242
+ id: row.id,
33243
+ next: row.status === "ip_reserving" ? `The IP is still being reserved: re-run in a minute, or see pgai mon instances list` : `See pgai mon instances list (${row.status ?? "unknown"})`
33244
+ };
33245
+ }
33246
+ const r = allowIpResult2(row, c.provider, c.name, c.opts, c.url);
33247
+ c.progress("allow_ip", `Reserved IP for the monitoring box: ${r.ip}`, { ip: r.ip, cidr: r.cidr, host: r.host, port: r.port, reserved_until: r.reserved_until, id: row.id });
33248
+ if (!c.deps.allowIp)
33249
+ return r;
33250
+ const why = row.last_error ? `The box could not connect from ${r.ip}: ${row.last_error}` : c.opts.ipFirst ? "You asked for the IP first (--ip-first)." : !row.precheck_error ? "The IP was reserved first, as asked." : `PostgresAI could not connect to ${r.host}:${r.port} (${row.precheck_error}). The database likely allows only listed IPs.`;
33251
+ const answer = await c.deps.allowIp([
33252
+ `Monitoring box IP: ${r.ip}${row.cloud ? ` (${cloudWord2(row.cloud)}, ${row.location})` : ""}`,
33253
+ why,
33254
+ `Allow inbound TCP ${r.port} from ${r.cidr} on ${r.host}, then continue. The IP is held until ${heldUntil2(row.reserved_until)} and stays this box's IP.`
33255
+ ]);
33256
+ return answer === "continue" ? "continue" : allowIpResult2(row, c.provider, c.name, c.opts, c.url, true);
32786
33257
  }
32787
33258
  async function placeBox2(deps, opts, where, progress) {
32788
33259
  if (!deps.deployOptions)
@@ -33161,8 +33632,36 @@ async function supabaseBox2(ref, name, opts, deps, progress, stop, label) {
33161
33632
  return follow2(box, { provider, name, fresh: true, billing: b.billing, billingUrl: b.billingUrl, note: "", place }, opts, deps, progress);
33162
33633
  }
33163
33634
  var STILL_IN_PROGRESS2 = "Still in progress";
33635
+ var CONNECT_TIMEOUT_MS2 = 55000;
33636
+ var AGENT_CONNECT_TIMEOUT_MS2 = 25000;
33637
+ var STILL_CREATING2 = "Still creating the monitoring box";
33638
+ var NOT_CONFIRMED2 = "PostgresAI did not confirm the monitoring box within 5 minutes. Run pgai mon instances list: if this database is there, follow it with pgai mon instances watch <id>; if not, run pgai mon deploy again";
33639
+ var STOPPED_LOOKING2 = `${STILL_IN_PROGRESS2}: deploy stopped watching before PostgresAI confirmed the monitoring box; it may still appear. Run pgai mon instances list before running deploy again`;
33640
+ var NOT_CONFIRMED_AGENT2 = "PostgresAI did not confirm the monitoring box yet. Call deploy_monitoring again in a minute: it continues with the box if PostgresAI created it. If pgai mon instances list still has no box for this database, ask the user to run pgai mon deploy with --reset-password in a terminal";
33641
+ var LOOKUP_MS2 = 5 * 60000;
33642
+ var AGENT_RUN_MS2 = 50000;
33643
+ function mayHaveRun2(err) {
33644
+ if (err instanceof HttpRequestTimeoutError)
33645
+ return true;
33646
+ if (err instanceof HttpStatusError)
33647
+ return err.status >= 500 && !err.sqlstate;
33648
+ const message = err instanceof Error ? err.message : "";
33649
+ return / could not reach /.test(message) && !/\((ECONNREFUSED|ENOTFOUND|EAI_AGAIN|ENETUNREACH|EHOSTUNREACH|ETIMEDOUT|UND_ERR_CONNECT_TIMEOUT|CERT_\w+|ERR_TLS_\w+|UNABLE_TO_\w+|DEPTH_ZERO_\w+|SELF_SIGNED_\w+)\)/.test(message);
33650
+ }
33651
+ async function lookForBox2(findMade, opts, deps, started) {
33652
+ for (const deadline = opts.agent ? started + AGENT_RUN_MS2 : deps.now() + LOOKUP_MS2;; ) {
33653
+ const made = findMade(await deps.list().catch(() => []));
33654
+ if (made)
33655
+ return made;
33656
+ const now = deps.now();
33657
+ if (deps.aborted?.() || now >= deadline)
33658
+ return;
33659
+ await deps.sleep(Math.min(POLL_MS2, deadline - now));
33660
+ }
33661
+ }
33164
33662
  async function follow2(row, f, opts, deps, progress) {
33165
33663
  const { provider, name, fresh, billingUrl, undoRole, note } = f;
33664
+ const url = (_) => f.url ?? name;
33166
33665
  const billing = { ...f.billing, ...f.place };
33167
33666
  let shown;
33168
33667
  for (const deadline = deps.now() + opts.waitMs;; ) {
@@ -33178,10 +33677,16 @@ async function follow2(row, f, opts, deps, progress) {
33178
33677
  next: chargeFailed2(row.billing_error, "re-run", billingUrl, opts.agent ? "coupon" : "--coupon")
33179
33678
  };
33180
33679
  }
33680
+ if (row.status === "ip_reserved") {
33681
+ if (undoRole)
33682
+ await undoRole();
33683
+ return { ...allowIpResult2(row, provider, name, opts, url(row)), ...billing };
33684
+ }
33181
33685
  const result = connectStatus2(row, provider, fresh);
33182
33686
  const state = boxState2(row);
33183
- if (state !== shown && (shown !== undefined || result.status === "in_progress"))
33184
- progress("box", `Monitoring box: ${boxWords2(state)}`, { state, id: row.id });
33687
+ if (state !== shown && (shown !== undefined || result.status === "in_progress")) {
33688
+ progress("box", state === "waiting_for_access" && row.egress_ip ? `Monitoring box: waiting for database access from ${row.egress_ip}${row.last_error ? ` (${row.last_error})` : ""}` : `Monitoring box: ${boxWords2(state)}`, { state, id: row.id, ...state === "waiting_for_access" && row.egress_ip ? { ip: row.egress_ip, ...row.last_error ? { error: row.last_error } : {} } : {} });
33689
+ }
33185
33690
  shown = state;
33186
33691
  if (result.status !== "in_progress" || deps.now() >= deadline) {
33187
33692
  const final = deps.links ? connectStatus2(await deps.links(row), provider, fresh) : result;
@@ -33343,7 +33848,7 @@ async function prepareDatabase2(url, provider, opts = {}) {
33343
33848
  const noTls = refusedTls || !usedSsl && me.ssl === "off";
33344
33849
  const tlsOnly = `PostgresAI sends the password it keeps for ${role} only over TLS`;
33345
33850
  const others = opts.others?.length ? opts.others : undefined;
33346
- const disconnectOthers = others ? disconnectEach2(others) : "pgai mon instances delete the server's other databases (pgai mon instances list)";
33851
+ const disconnectOthers = others ? disconnectEach2(opts.otherRefs ?? others) : "pgai mon instances delete the server's other databases (pgai mon instances list)";
33347
33852
  const reconnectOthers = `(and deploy ${others ? others.join(", ") : "them"} again with it)`;
33348
33853
  const noTlsWayOut = opts.agent ? `Pass its URL as database_url, or run pgai mon deploy in a terminal with PGAI_MON_PASSWORD set to its password. If nobody has it, in a terminal: ${disconnectOthers}, then pgai mon deploy <admin-url> --reset-password with PGAI_MON_PASSWORD set to a new one ${reconnectOthers}` : `Set PGAI_MON_PASSWORD to its password, or turn on TLS on the server (ssl = on) and put sslmode=require in the URL. If nobody has the password: ${disconnectOthers}, then re-run with --reset-password and PGAI_MON_PASSWORD set to a new one ${reconnectOthers}`;
33349
33854
  const reuse = me.mon_exists && !opts.resetPassword && !!opts.storedPassword && !(opts.agent ? "" : process.env.PGAI_MON_PASSWORD?.trim());
@@ -33471,7 +33976,16 @@ function grafanaOAuthUrl2(dashboardUrl) {
33471
33976
  return `${u.origin}/login/generic_oauth?redirectTo=${encodeURIComponent(`${u.pathname}${u.search}`)}`;
33472
33977
  }
33473
33978
  function platformDeps2(p) {
33474
- const rpc = (fn, body = {}) => callRpc({ apiKey: p.apiKey, apiBaseUrl: p.apiBaseUrl, fn, body, operation: fn.replace(/_/g, " "), debug: p.debug, orgScope: p.orgScope });
33979
+ const rpc = (fn, body = {}, timeoutMs) => callRpc({
33980
+ apiKey: p.apiKey,
33981
+ apiBaseUrl: p.apiBaseUrl,
33982
+ fn,
33983
+ body,
33984
+ operation: fn.replace(/_/g, " "),
33985
+ debug: p.debug,
33986
+ orgScope: p.orgScope,
33987
+ ...timeoutMs ? { timeoutMs, timeoutCeilingMs: timeoutMs } : {}
33988
+ });
33475
33989
  let orgAlias;
33476
33990
  let projects;
33477
33991
  const healthUrl = async (row) => {
@@ -33488,14 +34002,43 @@ function platformDeps2(p) {
33488
34002
  orgAlias = projects = undefined;
33489
34003
  return;
33490
34004
  });
33491
- const { dashboard_url, host_metrics, ...rest } = row;
33492
- return { ...rest, ...health_url ? { health_url } : {}, dashboard_url: grafanaOAuthUrl2(dashboard_url), host_metrics };
34005
+ const { dashboard_url, host_metrics, host_metrics_source, ...rest } = row;
34006
+ return {
34007
+ ...rest,
34008
+ ...health_url ? { health_url } : {},
34009
+ dashboard_url: grafanaOAuthUrl2(dashboard_url),
34010
+ host_metrics,
34011
+ ...host_metrics_source !== undefined ? { host_metrics_source } : {}
34012
+ };
33493
34013
  };
33494
34014
  return {
33495
34015
  verifyTls: verifyCollectorTls,
33496
34016
  list: () => rpc("cloud_monitoring_list"),
33497
34017
  links: withLinks,
33498
- create: (body) => rpc("cloud_monitoring_connect", body),
34018
+ create: async (body) => {
34019
+ const { allow_ip_fallback: _fallback, ip_first, ip_allowed, ...older } = body;
34020
+ const asked = !!(ip_first || ip_allowed);
34021
+ const connectMs = p.agent ? AGENT_CONNECT_TIMEOUT_MS2 : CONNECT_TIMEOUT_MS2;
34022
+ const connectTimeout = { timeoutMs: connectMs, timeoutCeilingMs: connectMs };
34023
+ try {
34024
+ return await callRpc({
34025
+ apiKey: p.apiKey,
34026
+ apiBaseUrl: p.apiBaseUrl,
34027
+ fn: "cloud_monitoring_connect",
34028
+ body,
34029
+ operation: "cloud monitoring connect",
34030
+ debug: p.debug,
34031
+ orgScope: p.orgScope,
34032
+ ...connectTimeout,
34033
+ ...asked ? {} : { fallbackBody: older }
34034
+ });
34035
+ } catch (err) {
34036
+ if (asked && err instanceof HttpStatusError && err.status === 404 && /cloud_monitoring_connect/.test(err.message)) {
34037
+ throw new Error("This PostgresAI platform does not support --ip-first or --ip-allowed yet: deploy without them");
34038
+ }
34039
+ throw err;
34040
+ }
34041
+ },
33499
34042
  disconnect: (id) => rpc("cloud_monitoring_disconnect", { instance_id: id }),
33500
34043
  setVcpus: (id, vcpus) => rpc("cloud_monitoring_set_vcpus", { instance_id: id, vcpus }),
33501
34044
  resetLock: (server) => rpc("cloud_monitoring_reset_lock", { server }),
@@ -33546,7 +34089,7 @@ async function callRpcRaw2(params) {
33546
34089
  console.error(`Debug: Request body: ${redactSecretsForLog(payload)}`);
33547
34090
  }
33548
34091
  let response;
33549
- const requestTimeout = requestTimeoutSignal(params.timeoutMs);
34092
+ const requestTimeout = requestTimeoutSignal(params.timeoutMs, params.timeoutCeilingMs);
33550
34093
  try {
33551
34094
  response = await fetch(url.toString(), {
33552
34095
  method: "POST",
@@ -33572,7 +34115,7 @@ async function callRpcRaw2(params) {
33572
34115
  ({ response, text } = await send(params.fallbackBody));
33573
34116
  }
33574
34117
  if (!response.ok) {
33575
- throw new HttpStatusError(formatHttpError(operation, response.status, text, response.statusText), response.status);
34118
+ throw new HttpStatusError(formatHttpError(operation, response.status, text, response.statusText), response.status, sqlstateOf(text));
33576
34119
  }
33577
34120
  try {
33578
34121
  return { data: JSON.parse(text), status: response.status };
@@ -35371,6 +35914,8 @@ function monitoringStatus2(raw) {
35371
35914
  return "inactive";
35372
35915
  if (/fail|error/.test(raw ?? ""))
35373
35916
  return "failed";
35917
+ if (raw === "ip_reserved")
35918
+ return "action_required";
35374
35919
  return "in_progress";
35375
35920
  }
35376
35921
  function databaseUrlArg2(positional, dbUrl, command) {
@@ -36617,7 +37162,7 @@ async function buildInitPlan2(params) {
36617
37162
  if not exists (select 1 from pg_catalog.pg_roles where rolname = ${qRoleNameLit}) then
36618
37163
  begin
36619
37164
  create user ${qRole} with password ${qPwVerifier};
36620
- exception when duplicate_object then
37165
+ exception when duplicate_object or unique_violation then
36621
37166
  null;
36622
37167
  end;
36623
37168
  end if;${params.keepExistingPassword ? "" : `
@@ -36670,9 +37215,21 @@ async function applyInitPlan2(params) {
36670
37215
  throw e;
36671
37216
  }
36672
37217
  };
37218
+ const concurrent = (e) => /tuple concurrently (updated|deleted)/.test(e instanceof Error ? e.message : String(e));
37219
+ const executeRetried = async (step) => {
37220
+ for (let attempt = 1;; attempt++) {
37221
+ try {
37222
+ return await executeStep(step);
37223
+ } catch (e) {
37224
+ if (attempt >= 3 || !concurrent(e))
37225
+ throw e;
37226
+ await new Promise((r) => setTimeout(r, 200 * attempt));
37227
+ }
37228
+ }
37229
+ };
36673
37230
  for (const step of params.plan.steps.filter((s) => !s.optional)) {
36674
37231
  try {
36675
- await executeStep(step);
37232
+ await executeRetried(step);
36676
37233
  applied.push(step.name);
36677
37234
  } catch (e) {
36678
37235
  const msg = e instanceof Error ? e.message : String(e);
@@ -45105,6 +45662,8 @@ function composeContentEqual(a, b) {
45105
45662
  async function refreshBundledComposeIfStale(projectDir, oldTag) {
45106
45663
  if (fs10.existsSync(path8.resolve(projectDir, ".git")))
45107
45664
  return false;
45665
+ if (process.env.PGAI_SKIP_COMPOSE_REFRESH === "1")
45666
+ return false;
45108
45667
  const composeFile = path8.resolve(projectDir, "docker-compose.yml");
45109
45668
  if (!fs10.existsSync(composeFile))
45110
45669
  return false;
@@ -47272,6 +47831,7 @@ Resume: pgai mon instances watch ${createdId}`);
47272
47831
  process.exit(130);
47273
47832
  };
47274
47833
  let requested = false;
47834
+ let looking = false;
47275
47835
  const onSigint = () => {
47276
47836
  if (createdId)
47277
47837
  return resume();
@@ -47283,7 +47843,7 @@ Quit. The monitoring box may exist: pgai mon instances list`);
47283
47843
  process.exit(130);
47284
47844
  }
47285
47845
  stopping = true;
47286
- const reply = requested ? "Stopping: the monitoring box is already requested; deploy stops watching when the request ends (Ctrl-C again to quit now)." : "Cancelling: nothing has been created yet (Ctrl-C again to quit now).";
47846
+ const reply = looking ? "Stopping: PostgresAI may still create the monitoring box; deploy stops at its next check (Ctrl-C again to quit now)." : requested ? "Stopping: the monitoring box is already requested; deploy stops watching when the request ends (Ctrl-C again to quit now)." : "Cancelling: nothing has been created yet (Ctrl-C again to quit now).";
47287
47847
  const view = steps.view();
47288
47848
  if (view)
47289
47849
  view.log(reply);
@@ -47335,6 +47895,27 @@ ${reply}`);
47335
47895
  closeReadline();
47336
47896
  }
47337
47897
  },
47898
+ ...interactive(opts.json) ? { allowIp: async (lines) => {
47899
+ steps.view()?.stop();
47900
+ console.error("");
47901
+ for (const line of lines)
47902
+ console.error(terminalText2(line));
47903
+ process.exitCode = 130;
47904
+ try {
47905
+ for (let tries = 0;tries < 3; tries++) {
47906
+ const answer = (await question("[Enter] continue / [s]top: ")).trim().toLowerCase();
47907
+ if (answer === "" || answer === "c" || answer === "continue")
47908
+ return "continue";
47909
+ if (answer === "s" || answer === "stop")
47910
+ return "stop";
47911
+ }
47912
+ return "stop";
47913
+ } finally {
47914
+ process.exitCode = undefined;
47915
+ closeReadline();
47916
+ steps.reset();
47917
+ }
47918
+ } } : {},
47338
47919
  ...interactive(opts.json) ? { chooseSupabaseProject: async (projects, org) => {
47339
47920
  const selectable = projects.filter((x) => x.selectable);
47340
47921
  console.error(terminalText2(`
@@ -47373,6 +47954,10 @@ Projects in the Supabase organization "${org}":`));
47373
47954
  progress: (e) => {
47374
47955
  if (e.event === "provisioning")
47375
47956
  requested = true;
47957
+ if (e.message === STILL_CREATING2)
47958
+ looking = true;
47959
+ if (e.event === "box")
47960
+ looking = false;
47376
47961
  if (e.project)
47377
47962
  shownName = terminalText2(e.project);
47378
47963
  steps.progress(e);
@@ -47396,9 +47981,16 @@ Projects in the Supabase organization "${org}":`));
47396
47981
  return resume();
47397
47982
  const view = steps.view();
47398
47983
  if (view) {
47399
- if (result.status === "ready")
47984
+ if (result.next === NOT_CONFIRMED2)
47985
+ view.setNote(`not confirmed after ${LOOKUP_MS2 / 60000} min`);
47986
+ else if (result.next === STOPPED_LOOKING2)
47987
+ view.setNote("stopped watching");
47988
+ else if (looking)
47989
+ view.setNote("");
47990
+ const end = stepViewEnd(result);
47991
+ if (end === "complete")
47400
47992
  view.complete();
47401
- else if (result.status !== "in_progress" && !result.next.startsWith(STILL_IN_PROGRESS2))
47993
+ else if (end === "fail")
47402
47994
  view.fail();
47403
47995
  view.stop();
47404
47996
  }
@@ -47413,7 +48005,7 @@ Projects in the Supabase organization "${org}":`));
47413
48005
  }
47414
48006
  }
47415
48007
  var mon = program2.command("mon").description("monitoring services management");
47416
- sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (default: host[:port]/database)", "the box's region on its cloud (default: the database's region when that cloud has it; see below)", "where the monitoring box runs: aws or hetzner (default: the database's cloud, else aws)").description("deploy PostgresAI Cloud monitoring for a database: prepare it, provision the monitoring box, print its Health Matrix and dashboard").option("--provider <provider>", "generic (direct PostgreSQL URL; alias: self-managed) | clickhouse | rds | supabase (default: detected from the host)").option("--clickhouse-key <id:secret>", "ClickHouse Cloud API key (Basic Service API Reader) for CPU, memory and disk; or CLICKHOUSE_KEY_ID + CLICKHOUSE_KEY_SECRET").option("--self-hosted", "run the monitoring stack on this machine (mon local-install) instead of PostgresAI Cloud").option("--reset-password", "the monitoring role exists and its password is lost: set a new one (admin URL; refused while another database on the server is monitored)").option("--monitoring-user <name>", "monitoring role to create or use", DEFAULT_MONITORING_USER2).option("--coupon <code>", "promotion code for the organization's monitoring subscription (applies when it is first created)").option("--vcpus <n>", "vCPUs of the database server; without it there is no AAS (database load) data (asked at a terminal; Supabase: from the project's compute size)").option("--supabase-project <name|project-id>", "the Supabase project to monitor (its exact name, or its project ID), instead of a URL; implies --provider supabase").addHelpText("after", [
48008
+ sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (default: host[:port]/database)", "the box's region on its cloud (default: the database's region when that cloud has it; see below)", "where the monitoring box runs: aws or hetzner (default: the database's cloud, else aws)").description("deploy PostgresAI Cloud monitoring for a database: prepare it, provision the monitoring box, print its Health Matrix and dashboard").option("--provider <provider>", "generic (direct PostgreSQL URL; alias: self-managed) | clickhouse | rds | supabase (default: detected from the host)").option("--clickhouse-key <id:secret>", "ClickHouse Cloud API key (Basic Service API Reader) for CPU, memory and disk; or CLICKHOUSE_KEY_ID + CLICKHOUSE_KEY_SECRET").option("--self-hosted", "run the monitoring stack on this machine (mon local-install) instead of PostgresAI Cloud").option("--reset-password", "the monitoring role exists and its password is lost: set a new one (admin URL; refused while another database on the server is monitored)").option("--monitoring-user <name>", "monitoring role to create or use", DEFAULT_MONITORING_USER2).option("--coupon <code>", "promotion code for the organization's monitoring subscription (applies when it is first created)").option("--vcpus <n>", "vCPUs of the database server; without it there is no AAS (database load) data (asked at a terminal; Supabase: from the project's compute size)").option("--supabase-project <name|project-id>", "the Supabase project to monitor (its exact name, or its project ID), instead of a URL; implies --provider supabase").option("--ip-first", "reserve the monitoring box's IP and stop before the box: the IP is kept for a week, so you can allow it on the database and continue the deploy afterwards with --ip-allowed (PostgresAI Cloud only)").option("--ip-allowed", "the database now allows the reserved IP: create the box with it").addHelpText("after", [
47417
48009
  "",
47418
48010
  "Steps (each skipped when already done; safe to re-run): sign in, prepare the database",
47419
48011
  "(an admin URL creates the postgres_ai_mon role; otherwise the SQL is printed), provision",
@@ -47432,10 +48024,14 @@ sharedDeployOptions2(withOrgOptions(mon.command("deploy")), 20, "instance name (
47432
48024
  'place, the price and the choices ("choices" in the JSON result): re-run with --yes, and with',
47433
48025
  "--cloud/--location to pick another place.",
47434
48026
  "",
47435
- "Network: the database must be reachable from this machine and from PostgresAI. Before provisioning,",
47436
- "PostgresAI tests the connection from an unpublished address, so the port must accept connections",
47437
- "from the internet; after it, the monitoring box connects from the IP shown on its Console page.",
47438
- "Behind an IP allowlist, use AWS PrivateLink (RDS) or --self-hosted.",
48027
+ "Network: the database must be reachable from this machine. The monitoring box connects from its",
48028
+ "own static IP, shown before the box is created when PostgresAI can't reach the database the way",
48029
+ "an IP allowlist refuses (timeout, refused, no pg_hba.conf entry), or with --ip-first: deploy then",
48030
+ 'stops at the IP ("step":"allow_ip" with ip, cidr, host, port, reserved_until; exit 3 when not',
48031
+ "at a terminal). Allow it on the database, then re-run with --ip-allowed (at a terminal: Enter).",
48032
+ "The IP is held 7 days and stays the box's for its life. If the box still can't connect, it waits",
48033
+ "up to 90 minutes, then is removed and the IP kept: allow it and re-run with --ip-allowed.",
48034
+ "RDS can also use AWS PrivateLink (no public IP), or monitor with --self-hosted.",
47439
48035
  "",
47440
48036
  "Billing is per Postgres cluster observed: another database in a cluster the organization already",
47441
48037
  'monitors is included. Once the URL is checked, deploy shows the price (or "free (N of M free slots)"). A billed',
@@ -47518,7 +48114,7 @@ var monPollMs = () => {
47518
48114
  var monInstances = mon.command("instances").description("monitoring instances in PostgresAI Cloud (pgai mon deploy)");
47519
48115
  withOrgOptions(monInstances.command("list")).description("list the organization's monitoring instances").option("--json", "JSON output").option("--debug", "print HTTP requests (secrets masked)").action(async (opts) => {
47520
48116
  try {
47521
- printResult((await linkedDatabases(opts)).map((d) => ({ ...d, status: stateOf2(d.status) })), opts.json);
48117
+ printResult((await linkedDatabases(opts)).map(listedDatabase), opts.json);
47522
48118
  } catch (err) {
47523
48119
  failCloud(err, opts.json);
47524
48120
  }
@@ -47558,7 +48154,7 @@ withOrgOptions(monInstances.command("delete <id-or-name>")).description("stop mo
47558
48154
  name: row.name,
47559
48155
  id: row.id,
47560
48156
  ...billing ? { billing } : {},
47561
- next: row.host_metrics ? "Delete the ClickHouse Cloud API key you gave us" : "none"
48157
+ next: clickhouseKeyHeld(row) ? "Delete the ClickHouse Cloud API key you gave us" : "none"
47562
48158
  }, opts.json);
47563
48159
  } catch (err) {
47564
48160
  failCloud(err, opts.json);