postgresai 0.18.0-dev.13 → 0.18.0-dev.14

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.
package/README.md CHANGED
@@ -353,6 +353,11 @@ do `pgai dblab deploy` and `pgai dblab instances`:
353
353
  | `action_required` | do what `next` says, then re-run; also when the `--wait` of `pgai mon deploy` ran out with the box still starting (`next`: "Still in progress ... resume with pgai mon instances watch <id>") | 3 |
354
354
  | `failed` | `next` has the reason | 1 |
355
355
 
356
+ When PostgresAI is slow to answer the box request for a database URL, `pgai mon deploy` says it is still
357
+ creating the monitoring box and looks for the box for up to 5 minutes (30 seconds from the MCP tool)
358
+ before `--wait` starts, also with `--no-wait`; then it follows the box as usual. If no box appears, it
359
+ stops with `action_required` and says to check `pgai mon instances list` before running deploy again.
360
+
356
361
  `pgai init` exits with 130 when cancelled at a prompt (Ctrl-C, Ctrl-D).
357
362
 
358
363
  `--self-hosted` starts `mon local-install` with the monitoring URL, the API key and the ClickHouse
@@ -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.14",
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.14";
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) {
@@ -22692,7 +22702,7 @@ async function callRpcRaw(params) {
22692
22702
  console.error(`Debug: Request body: ${redactSecretsForLog(payload)}`);
22693
22703
  }
22694
22704
  let response;
22695
- const requestTimeout = requestTimeoutSignal(params.timeoutMs);
22705
+ const requestTimeout = requestTimeoutSignal(params.timeoutMs, params.timeoutCeilingMs);
22696
22706
  try {
22697
22707
  response = await fetch(url.toString(), {
22698
22708
  method: "POST",
@@ -22718,7 +22728,7 @@ async function callRpcRaw(params) {
22718
22728
  ({ response, text } = await send(params.fallbackBody));
22719
22729
  }
22720
22730
  if (!response.ok) {
22721
- throw new HttpStatusError(formatHttpError(operation, response.status, text, response.statusText), response.status);
22731
+ throw new HttpStatusError(formatHttpError(operation, response.status, text, response.statusText), response.status, sqlstateOf(text));
22722
22732
  }
22723
22733
  try {
22724
22734
  return { data: JSON.parse(text), status: response.status };
@@ -22943,6 +22953,7 @@ function storedPasswordNeeds(monitoringUrl) {
22943
22953
  return needs.length ? needs.join(" and ") : undefined;
22944
22954
  }
22945
22955
  var shellWord = (s) => /^[\w./:@%+=,-]+$/.test(s) ? s : `'${s.replace(/'/g, "'\\''")}'`;
22956
+ var deleteRef = (d, list) => list.some((o) => o.id !== d.id && !disconnecting(o.status) && o.name === d.name) ? d.id : d.name;
22946
22957
  var disconnectEach = (names) => names.map((n) => `pgai mon instances delete ${shellWord(n)} --yes`).join(" and ");
22947
22958
  function loginName(url, sessionUser, role) {
22948
22959
  const user = decodeURIComponent(new URL(url).username);
@@ -23106,11 +23117,18 @@ async function connect(url, opts, deps) {
23106
23117
  if (opts.selfHosted && deps.localStackRunning()) {
23107
23118
  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
23119
  }
23120
+ const byCluster = (d) => !!d.cluster && serverOf(d.cluster) === cluster && d.database != null && decodeURIComponent(d.database) === name.split("/").slice(1).join("/");
23121
+ const byName = (d) => (d.cluster == null || d.database == null) && d.name === name;
23109
23122
  const findDatabase = (list) => {
23110
23123
  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);
23124
+ return live.find(byCluster) ?? live.find(byName);
23112
23125
  };
23113
23126
  const rows = opts.selfHosted ? [] : await deps.list();
23127
+ const known = new Set(rows.map((d) => d.id));
23128
+ const findMade = (list) => {
23129
+ const fresh = list.filter((d) => !known.has(d.id));
23130
+ return fresh.find(byCluster) ?? fresh.find(byName);
23131
+ };
23114
23132
  let row = findDatabase(rows);
23115
23133
  const fresh = !row;
23116
23134
  let note = "";
@@ -23152,7 +23170,7 @@ async function connect(url, opts, deps) {
23152
23170
  }
23153
23171
  }
23154
23172
  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` };
23173
+ return { status: "action_required", provider, name, id: row.id, next: `${name} is already monitored, and its monitoring uses the current password: ${disconnectEach([deleteRef(row, rows)])} first` };
23156
23174
  }
23157
23175
  if (row && key) {
23158
23176
  const found = await findOrg();
@@ -23180,26 +23198,30 @@ async function connect(url, opts, deps) {
23180
23198
  let lockId;
23181
23199
  const unread = (d) => !d.cluster;
23182
23200
  const onServer = (d) => !!d.cluster && serverOf(d.cluster) === cluster;
23183
- const othersOnServer = (list) => list.filter((d) => !disconnecting(d.status) && (unread(d) || onServer(d)));
23201
+ const usesRole = (d) => !["deleting", "deleted", "inactive"].includes(stateOf(d.status)) && !(d.status === "failed" && !d.registered_at);
23202
+ const othersOnServer = (list) => list.filter((d) => usesRole(d) && (unread(d) || onServer(d)));
23184
23203
  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(", ");
23204
+ const cutOff = (others, list) => {
23205
+ const label = (d) => deleteRef(d, list) === d.id ? `${d.name} (id ${d.id})` : d.name;
23206
+ const on = others.filter((d) => !unread(d)).map(label).join(", ");
23207
+ const maybe = others.filter(unread).map(label).join(", ");
23188
23208
  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
23209
  const instead = [
23190
23210
  ...storedPassword ? ["deploy without --reset-password (PostgresAI keeps its password for this server, and sends it only over TLS)"] : [],
23191
23211
  "set PGAI_MON_PASSWORD to its password instead",
23192
- `${disconnectEach(others.map((d) => d.name))} first`
23212
+ `${disconnectEach(others.map((d) => deleteRef(d, list)))} first`
23193
23213
  ];
23194
23214
  return { status: "action_required", provider, name, next: `A new password for ${role} ${what}: ${instead.join(", or ")}` };
23195
23215
  };
23196
- const others = othersOnServer(rows).filter((d) => !unread(d)).map((d) => d.name);
23216
+ const onThisServer = othersOnServer(rows).filter((d) => !unread(d));
23217
+ const others = onThisServer.map((d) => d.name);
23218
+ const otherRefs = onThisServer.map((d) => deleteRef(d, rows));
23197
23219
  if (opts.resetPassword && othersOnServer(rows).length)
23198
- return cutOff(othersOnServer(rows));
23220
+ return cutOff(othersOnServer(rows), rows);
23199
23221
  if (opts.coupon !== undefined && !opts.coupon.trim()) {
23200
23222
  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
23223
  }
23202
- const prepOpts = { resetPassword: opts.resetPassword, warn, ...storedPassword ? { storedPassword, others } : {}, ...monitoringUser ? { monitoringUser } : {} };
23224
+ const prepOpts = { resetPassword: opts.resetPassword, warn, ...storedPassword ? { storedPassword, others, otherRefs } : {}, ...monitoringUser ? { monitoringUser } : {} };
23203
23225
  if (!opts.selfHosted) {
23204
23226
  const probe = await deps.prepare(url, provider, { ...prepOpts, check: true }).catch((err) => {
23205
23227
  if (err instanceof HttpStatusError || /Could not connect to/.test(errorText(err)))
@@ -23265,10 +23287,10 @@ async function connect(url, opts, deps) {
23265
23287
  const now = await deps.list();
23266
23288
  const same = findDatabase(now);
23267
23289
  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` };
23290
+ return { status: "action_required", provider, name, id: same.id, ...billing, next: `${name} is already monitored, and its monitoring uses the current password: ${disconnectEach([deleteRef(same, now)])} first` };
23269
23291
  const others = othersOnServer(now);
23270
23292
  if (others.length)
23271
- return { ...cutOff(others), ...billing };
23293
+ return { ...cutOff(others, now), ...billing };
23272
23294
  }
23273
23295
  if (deps.aborted?.())
23274
23296
  throw new CancelledError;
@@ -23301,7 +23323,7 @@ async function connect(url, opts, deps) {
23301
23323
  ...place ? { cloud: place.cloud, server_location: place.location } : opts.location ? { server_location: opts.location } : {},
23302
23324
  ...vcpus ? { vcpus } : {}
23303
23325
  }).catch(async (err) => {
23304
- if (err instanceof HttpStatusError && err.status >= 400 && err.status < 500)
23326
+ if (err instanceof HttpStatusError && (err.status < 500 || err.sqlstate))
23305
23327
  await undo(prepared);
23306
23328
  else
23307
23329
  unanswered = true;
@@ -23311,8 +23333,25 @@ async function connect(url, opts, deps) {
23311
23333
  return "price";
23312
23334
  if (err instanceof HttpStatusError && err.status === 409 && "storedPassword" in prepared && prepared.storedPassword && /none is stored/.test(err.message))
23313
23335
  return "stored";
23336
+ if (mayHaveRun(err))
23337
+ return "unanswered";
23314
23338
  throw err;
23315
23339
  });
23340
+ if (created === "unanswered") {
23341
+ progress("provisioning", STILL_CREATING);
23342
+ const made = await lookForBox(findMade, opts, deps, started);
23343
+ if (!made && deps.aborted?.())
23344
+ return { status: "action_required", provider, name, ...billing, next: STOPPED_LOOKING };
23345
+ if (!made)
23346
+ return { status: "action_required", provider, name, ...billing, next: opts.agent ? NOT_CONFIRMED_AGENT : NOT_CONFIRMED };
23347
+ if (stateOf(made.status) === "failed" || disconnecting(made.status)) {
23348
+ await undo(prepared);
23349
+ unanswered = false;
23350
+ 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.` };
23351
+ }
23352
+ unanswered = false;
23353
+ created = { id: made.id, name: made.name ?? name, status: made.status ?? "launch_requested" };
23354
+ }
23316
23355
  if (created === "payment")
23317
23356
  return { status: "action_required", provider, name, ...billingPage };
23318
23357
  if (created === "declined") {
@@ -23652,6 +23691,33 @@ async function supabaseBox(ref, name, opts, deps, progress, stop, label) {
23652
23691
  return follow(box, { provider, name, fresh: true, billing: b.billing, billingUrl: b.billingUrl, note: "", place }, opts, deps, progress);
23653
23692
  }
23654
23693
  var STILL_IN_PROGRESS = "Still in progress";
23694
+ var CONNECT_TIMEOUT_MS = 55000;
23695
+ var AGENT_CONNECT_TIMEOUT_MS = 25000;
23696
+ var STILL_CREATING = "Still creating the monitoring box";
23697
+ 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";
23698
+ 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`;
23699
+ 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";
23700
+ var LOOKUP_MS = 5 * 60000;
23701
+ var AGENT_RUN_MS = 50000;
23702
+ function mayHaveRun(err) {
23703
+ if (err instanceof HttpRequestTimeoutError)
23704
+ return true;
23705
+ if (err instanceof HttpStatusError)
23706
+ return err.status >= 500 && !err.sqlstate;
23707
+ const message = err instanceof Error ? err.message : "";
23708
+ 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);
23709
+ }
23710
+ async function lookForBox(findMade, opts, deps, started) {
23711
+ for (const deadline = opts.agent ? started + AGENT_RUN_MS : deps.now() + LOOKUP_MS;; ) {
23712
+ const made = findMade(await deps.list().catch(() => []));
23713
+ if (made)
23714
+ return made;
23715
+ const now = deps.now();
23716
+ if (deps.aborted?.() || now >= deadline)
23717
+ return;
23718
+ await deps.sleep(Math.min(POLL_MS, deadline - now));
23719
+ }
23720
+ }
23655
23721
  async function follow(row, f, opts, deps, progress) {
23656
23722
  const { provider, name, fresh, billingUrl, undoRole, note } = f;
23657
23723
  const billing = { ...f.billing, ...f.place };
@@ -23834,7 +23900,7 @@ async function prepareDatabase(url, provider, opts = {}) {
23834
23900
  const noTls = refusedTls || !usedSsl && me.ssl === "off";
23835
23901
  const tlsOnly = `PostgresAI sends the password it keeps for ${role} only over TLS`;
23836
23902
  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)";
23903
+ const disconnectOthers = others ? disconnectEach(opts.otherRefs ?? others) : "pgai mon instances delete the server's other databases (pgai mon instances list)";
23838
23904
  const reconnectOthers = `(and deploy ${others ? others.join(", ") : "them"} again with it)`;
23839
23905
  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
23906
  const reuse = me.mon_exists && !opts.resetPassword && !!opts.storedPassword && !(opts.agent ? "" : process.env.PGAI_MON_PASSWORD?.trim());
@@ -23946,7 +24012,16 @@ function grafanaOAuthUrl(dashboardUrl) {
23946
24012
  return `${u.origin}/login/generic_oauth?redirectTo=${encodeURIComponent(`${u.pathname}${u.search}`)}`;
23947
24013
  }
23948
24014
  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 });
24015
+ const rpc = (fn, body = {}, timeoutMs) => callRpc({
24016
+ apiKey: p.apiKey,
24017
+ apiBaseUrl: p.apiBaseUrl,
24018
+ fn,
24019
+ body,
24020
+ operation: fn.replace(/_/g, " "),
24021
+ debug: p.debug,
24022
+ orgScope: p.orgScope,
24023
+ ...timeoutMs ? { timeoutMs, timeoutCeilingMs: timeoutMs } : {}
24024
+ });
23950
24025
  let orgAlias;
23951
24026
  let projects;
23952
24027
  const healthUrl = async (row) => {
@@ -23970,7 +24045,7 @@ function platformDeps(p) {
23970
24045
  verifyTls: verifyCollectorTls,
23971
24046
  list: () => rpc("cloud_monitoring_list"),
23972
24047
  links: withLinks,
23973
- create: (body) => rpc("cloud_monitoring_connect", body),
24048
+ create: (body) => rpc("cloud_monitoring_connect", body, p.agent ? AGENT_CONNECT_TIMEOUT_MS : CONNECT_TIMEOUT_MS),
23974
24049
  disconnect: (id) => rpc("cloud_monitoring_disconnect", { instance_id: id }),
23975
24050
  setVcpus: (id, vcpus) => rpc("cloud_monitoring_set_vcpus", { instance_id: id, vcpus }),
23976
24051
  resetLock: (server) => rpc("cloud_monitoring_reset_lock", { server }),
@@ -32320,6 +32395,7 @@ function storedPasswordNeeds2(monitoringUrl) {
32320
32395
  return needs.length ? needs.join(" and ") : undefined;
32321
32396
  }
32322
32397
  var shellWord2 = (s) => /^[\w./:@%+=,-]+$/.test(s) ? s : `'${s.replace(/'/g, "'\\''")}'`;
32398
+ var deleteRef2 = (d, list) => list.some((o) => o.id !== d.id && !disconnecting2(o.status) && o.name === d.name) ? d.id : d.name;
32323
32399
  var disconnectEach2 = (names) => names.map((n) => `pgai mon instances delete ${shellWord2(n)} --yes`).join(" and ");
32324
32400
  function loginName2(url, sessionUser, role) {
32325
32401
  const user = decodeURIComponent(new URL(url).username);
@@ -32402,6 +32478,8 @@ function stepProgress(o) {
32402
32478
  view.log(e.message);
32403
32479
  if (e.event === "box" && e.state)
32404
32480
  view.setNote(`box: ${boxWords2(e.state)}`);
32481
+ if (e.message === STILL_CREATING2)
32482
+ view.setNote(`still creating: PostgresAI is slow to answer (checking for up to ${LOOKUP_MS2 / 60000} min)`);
32405
32483
  };
32406
32484
  return { progress, view: () => view };
32407
32485
  }
@@ -32550,11 +32628,18 @@ async function connect2(url, opts, deps) {
32550
32628
  if (opts.selfHosted && deps.localStackRunning()) {
32551
32629
  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
32630
  }
32631
+ const byCluster = (d) => !!d.cluster && serverOf2(d.cluster) === cluster && d.database != null && decodeURIComponent(d.database) === name.split("/").slice(1).join("/");
32632
+ const byName = (d) => (d.cluster == null || d.database == null) && d.name === name;
32553
32633
  const findDatabase = (list) => {
32554
32634
  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);
32635
+ return live.find(byCluster) ?? live.find(byName);
32556
32636
  };
32557
32637
  const rows = opts.selfHosted ? [] : await deps.list();
32638
+ const known = new Set(rows.map((d) => d.id));
32639
+ const findMade = (list) => {
32640
+ const fresh = list.filter((d) => !known.has(d.id));
32641
+ return fresh.find(byCluster) ?? fresh.find(byName);
32642
+ };
32558
32643
  let row = findDatabase(rows);
32559
32644
  const fresh = !row;
32560
32645
  let note = "";
@@ -32596,7 +32681,7 @@ async function connect2(url, opts, deps) {
32596
32681
  }
32597
32682
  }
32598
32683
  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` };
32684
+ return { status: "action_required", provider, name, id: row.id, next: `${name} is already monitored, and its monitoring uses the current password: ${disconnectEach2([deleteRef2(row, rows)])} first` };
32600
32685
  }
32601
32686
  if (row && key) {
32602
32687
  const found = await findOrg();
@@ -32624,26 +32709,30 @@ async function connect2(url, opts, deps) {
32624
32709
  let lockId;
32625
32710
  const unread = (d) => !d.cluster;
32626
32711
  const onServer = (d) => !!d.cluster && serverOf2(d.cluster) === cluster;
32627
- const othersOnServer = (list) => list.filter((d) => !disconnecting2(d.status) && (unread(d) || onServer(d)));
32712
+ const usesRole = (d) => !["deleting", "deleted", "inactive"].includes(stateOf2(d.status)) && !(d.status === "failed" && !d.registered_at);
32713
+ const othersOnServer = (list) => list.filter((d) => usesRole(d) && (unread(d) || onServer(d)));
32628
32714
  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(", ");
32715
+ const cutOff = (others, list) => {
32716
+ const label = (d) => deleteRef2(d, list) === d.id ? `${d.name} (id ${d.id})` : d.name;
32717
+ const on = others.filter((d) => !unread(d)).map(label).join(", ");
32718
+ const maybe = others.filter(unread).map(label).join(", ");
32632
32719
  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
32720
  const instead = [
32634
32721
  ...storedPassword ? ["deploy without --reset-password (PostgresAI keeps its password for this server, and sends it only over TLS)"] : [],
32635
32722
  "set PGAI_MON_PASSWORD to its password instead",
32636
- `${disconnectEach2(others.map((d) => d.name))} first`
32723
+ `${disconnectEach2(others.map((d) => deleteRef2(d, list)))} first`
32637
32724
  ];
32638
32725
  return { status: "action_required", provider, name, next: `A new password for ${role} ${what}: ${instead.join(", or ")}` };
32639
32726
  };
32640
- const others = othersOnServer(rows).filter((d) => !unread(d)).map((d) => d.name);
32727
+ const onThisServer = othersOnServer(rows).filter((d) => !unread(d));
32728
+ const others = onThisServer.map((d) => d.name);
32729
+ const otherRefs = onThisServer.map((d) => deleteRef2(d, rows));
32641
32730
  if (opts.resetPassword && othersOnServer(rows).length)
32642
- return cutOff(othersOnServer(rows));
32731
+ return cutOff(othersOnServer(rows), rows);
32643
32732
  if (opts.coupon !== undefined && !opts.coupon.trim()) {
32644
32733
  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
32734
  }
32646
- const prepOpts = { resetPassword: opts.resetPassword, warn, ...storedPassword ? { storedPassword, others } : {}, ...monitoringUser ? { monitoringUser } : {} };
32735
+ const prepOpts = { resetPassword: opts.resetPassword, warn, ...storedPassword ? { storedPassword, others, otherRefs } : {}, ...monitoringUser ? { monitoringUser } : {} };
32647
32736
  if (!opts.selfHosted) {
32648
32737
  const probe = await deps.prepare(url, provider, { ...prepOpts, check: true }).catch((err) => {
32649
32738
  if (err instanceof HttpStatusError || /Could not connect to/.test(errorText2(err)))
@@ -32709,10 +32798,10 @@ async function connect2(url, opts, deps) {
32709
32798
  const now = await deps.list();
32710
32799
  const same = findDatabase(now);
32711
32800
  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` };
32801
+ return { status: "action_required", provider, name, id: same.id, ...billing, next: `${name} is already monitored, and its monitoring uses the current password: ${disconnectEach2([deleteRef2(same, now)])} first` };
32713
32802
  const others = othersOnServer(now);
32714
32803
  if (others.length)
32715
- return { ...cutOff(others), ...billing };
32804
+ return { ...cutOff(others, now), ...billing };
32716
32805
  }
32717
32806
  if (deps.aborted?.())
32718
32807
  throw new CancelledError2;
@@ -32745,7 +32834,7 @@ async function connect2(url, opts, deps) {
32745
32834
  ...place ? { cloud: place.cloud, server_location: place.location } : opts.location ? { server_location: opts.location } : {},
32746
32835
  ...vcpus ? { vcpus } : {}
32747
32836
  }).catch(async (err) => {
32748
- if (err instanceof HttpStatusError && err.status >= 400 && err.status < 500)
32837
+ if (err instanceof HttpStatusError && (err.status < 500 || err.sqlstate))
32749
32838
  await undo(prepared);
32750
32839
  else
32751
32840
  unanswered = true;
@@ -32755,8 +32844,25 @@ async function connect2(url, opts, deps) {
32755
32844
  return "price";
32756
32845
  if (err instanceof HttpStatusError && err.status === 409 && "storedPassword" in prepared && prepared.storedPassword && /none is stored/.test(err.message))
32757
32846
  return "stored";
32847
+ if (mayHaveRun2(err))
32848
+ return "unanswered";
32758
32849
  throw err;
32759
32850
  });
32851
+ if (created === "unanswered") {
32852
+ progress("provisioning", STILL_CREATING2);
32853
+ const made = await lookForBox2(findMade, opts, deps, started);
32854
+ if (!made && deps.aborted?.())
32855
+ return { status: "action_required", provider, name, ...billing, next: STOPPED_LOOKING2 };
32856
+ if (!made)
32857
+ return { status: "action_required", provider, name, ...billing, next: opts.agent ? NOT_CONFIRMED_AGENT2 : NOT_CONFIRMED2 };
32858
+ if (stateOf2(made.status) === "failed" || disconnecting2(made.status)) {
32859
+ await undo(prepared);
32860
+ unanswered = false;
32861
+ 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.` };
32862
+ }
32863
+ unanswered = false;
32864
+ created = { id: made.id, name: made.name ?? name, status: made.status ?? "launch_requested" };
32865
+ }
32760
32866
  if (created === "payment")
32761
32867
  return { status: "action_required", provider, name, ...billingPage };
32762
32868
  if (created === "declined") {
@@ -33161,6 +33267,33 @@ async function supabaseBox2(ref, name, opts, deps, progress, stop, label) {
33161
33267
  return follow2(box, { provider, name, fresh: true, billing: b.billing, billingUrl: b.billingUrl, note: "", place }, opts, deps, progress);
33162
33268
  }
33163
33269
  var STILL_IN_PROGRESS2 = "Still in progress";
33270
+ var CONNECT_TIMEOUT_MS2 = 55000;
33271
+ var AGENT_CONNECT_TIMEOUT_MS2 = 25000;
33272
+ var STILL_CREATING2 = "Still creating the monitoring box";
33273
+ 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";
33274
+ 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`;
33275
+ 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";
33276
+ var LOOKUP_MS2 = 5 * 60000;
33277
+ var AGENT_RUN_MS2 = 50000;
33278
+ function mayHaveRun2(err) {
33279
+ if (err instanceof HttpRequestTimeoutError)
33280
+ return true;
33281
+ if (err instanceof HttpStatusError)
33282
+ return err.status >= 500 && !err.sqlstate;
33283
+ const message = err instanceof Error ? err.message : "";
33284
+ 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);
33285
+ }
33286
+ async function lookForBox2(findMade, opts, deps, started) {
33287
+ for (const deadline = opts.agent ? started + AGENT_RUN_MS2 : deps.now() + LOOKUP_MS2;; ) {
33288
+ const made = findMade(await deps.list().catch(() => []));
33289
+ if (made)
33290
+ return made;
33291
+ const now = deps.now();
33292
+ if (deps.aborted?.() || now >= deadline)
33293
+ return;
33294
+ await deps.sleep(Math.min(POLL_MS2, deadline - now));
33295
+ }
33296
+ }
33164
33297
  async function follow2(row, f, opts, deps, progress) {
33165
33298
  const { provider, name, fresh, billingUrl, undoRole, note } = f;
33166
33299
  const billing = { ...f.billing, ...f.place };
@@ -33343,7 +33476,7 @@ async function prepareDatabase2(url, provider, opts = {}) {
33343
33476
  const noTls = refusedTls || !usedSsl && me.ssl === "off";
33344
33477
  const tlsOnly = `PostgresAI sends the password it keeps for ${role} only over TLS`;
33345
33478
  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)";
33479
+ const disconnectOthers = others ? disconnectEach2(opts.otherRefs ?? others) : "pgai mon instances delete the server's other databases (pgai mon instances list)";
33347
33480
  const reconnectOthers = `(and deploy ${others ? others.join(", ") : "them"} again with it)`;
33348
33481
  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
33482
  const reuse = me.mon_exists && !opts.resetPassword && !!opts.storedPassword && !(opts.agent ? "" : process.env.PGAI_MON_PASSWORD?.trim());
@@ -33471,7 +33604,16 @@ function grafanaOAuthUrl2(dashboardUrl) {
33471
33604
  return `${u.origin}/login/generic_oauth?redirectTo=${encodeURIComponent(`${u.pathname}${u.search}`)}`;
33472
33605
  }
33473
33606
  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 });
33607
+ const rpc = (fn, body = {}, timeoutMs) => callRpc({
33608
+ apiKey: p.apiKey,
33609
+ apiBaseUrl: p.apiBaseUrl,
33610
+ fn,
33611
+ body,
33612
+ operation: fn.replace(/_/g, " "),
33613
+ debug: p.debug,
33614
+ orgScope: p.orgScope,
33615
+ ...timeoutMs ? { timeoutMs, timeoutCeilingMs: timeoutMs } : {}
33616
+ });
33475
33617
  let orgAlias;
33476
33618
  let projects;
33477
33619
  const healthUrl = async (row) => {
@@ -33495,7 +33637,7 @@ function platformDeps2(p) {
33495
33637
  verifyTls: verifyCollectorTls,
33496
33638
  list: () => rpc("cloud_monitoring_list"),
33497
33639
  links: withLinks,
33498
- create: (body) => rpc("cloud_monitoring_connect", body),
33640
+ create: (body) => rpc("cloud_monitoring_connect", body, p.agent ? AGENT_CONNECT_TIMEOUT_MS2 : CONNECT_TIMEOUT_MS2),
33499
33641
  disconnect: (id) => rpc("cloud_monitoring_disconnect", { instance_id: id }),
33500
33642
  setVcpus: (id, vcpus) => rpc("cloud_monitoring_set_vcpus", { instance_id: id, vcpus }),
33501
33643
  resetLock: (server) => rpc("cloud_monitoring_reset_lock", { server }),
@@ -33546,7 +33688,7 @@ async function callRpcRaw2(params) {
33546
33688
  console.error(`Debug: Request body: ${redactSecretsForLog(payload)}`);
33547
33689
  }
33548
33690
  let response;
33549
- const requestTimeout = requestTimeoutSignal(params.timeoutMs);
33691
+ const requestTimeout = requestTimeoutSignal(params.timeoutMs, params.timeoutCeilingMs);
33550
33692
  try {
33551
33693
  response = await fetch(url.toString(), {
33552
33694
  method: "POST",
@@ -33572,7 +33714,7 @@ async function callRpcRaw2(params) {
33572
33714
  ({ response, text } = await send(params.fallbackBody));
33573
33715
  }
33574
33716
  if (!response.ok) {
33575
- throw new HttpStatusError(formatHttpError(operation, response.status, text, response.statusText), response.status);
33717
+ throw new HttpStatusError(formatHttpError(operation, response.status, text, response.statusText), response.status, sqlstateOf(text));
33576
33718
  }
33577
33719
  try {
33578
33720
  return { data: JSON.parse(text), status: response.status };
@@ -45105,6 +45247,8 @@ function composeContentEqual(a, b) {
45105
45247
  async function refreshBundledComposeIfStale(projectDir, oldTag) {
45106
45248
  if (fs10.existsSync(path8.resolve(projectDir, ".git")))
45107
45249
  return false;
45250
+ if (process.env.PGAI_SKIP_COMPOSE_REFRESH === "1")
45251
+ return false;
45108
45252
  const composeFile = path8.resolve(projectDir, "docker-compose.yml");
45109
45253
  if (!fs10.existsSync(composeFile))
45110
45254
  return false;
@@ -47272,6 +47416,7 @@ Resume: pgai mon instances watch ${createdId}`);
47272
47416
  process.exit(130);
47273
47417
  };
47274
47418
  let requested = false;
47419
+ let looking = false;
47275
47420
  const onSigint = () => {
47276
47421
  if (createdId)
47277
47422
  return resume();
@@ -47283,7 +47428,7 @@ Quit. The monitoring box may exist: pgai mon instances list`);
47283
47428
  process.exit(130);
47284
47429
  }
47285
47430
  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).";
47431
+ 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
47432
  const view = steps.view();
47288
47433
  if (view)
47289
47434
  view.log(reply);
@@ -47373,6 +47518,10 @@ Projects in the Supabase organization "${org}":`));
47373
47518
  progress: (e) => {
47374
47519
  if (e.event === "provisioning")
47375
47520
  requested = true;
47521
+ if (e.message === STILL_CREATING2)
47522
+ looking = true;
47523
+ if (e.event === "box")
47524
+ looking = false;
47376
47525
  if (e.project)
47377
47526
  shownName = terminalText2(e.project);
47378
47527
  steps.progress(e);
@@ -47396,6 +47545,12 @@ Projects in the Supabase organization "${org}":`));
47396
47545
  return resume();
47397
47546
  const view = steps.view();
47398
47547
  if (view) {
47548
+ if (result.next === NOT_CONFIRMED2)
47549
+ view.setNote(`not confirmed after ${LOOKUP_MS2 / 60000} min`);
47550
+ else if (result.next === STOPPED_LOOKING2)
47551
+ view.setNote("stopped watching");
47552
+ else if (looking)
47553
+ view.setNote("");
47399
47554
  if (result.status === "ready")
47400
47555
  view.complete();
47401
47556
  else if (result.status !== "in_progress" && !result.next.startsWith(STILL_IN_PROGRESS2))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postgresai",
3
- "version": "0.18.0-dev.13",
3
+ "version": "0.18.0-dev.14",
4
4
  "description": "postgres_ai CLI",
5
5
  "license": "Apache-2.0",
6
6
  "private": false,