passcontrol 0.7.0 → 0.7.1

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.
@@ -8,6 +8,7 @@ import { createInterface } from "node:readline/promises";
8
8
  import { fileURLToPath } from "node:url";
9
9
  import { ed25519 } from "@noble/curves/ed25519";
10
10
  import {
11
+ CLOUD_GATEWAY,
11
12
  CONFIG_FILE,
12
13
  OPENAI_SHAPE_PROVIDERS,
13
14
  PROVIDERS,
@@ -91,7 +92,9 @@ const PUBLIC_REPO_URL = "https://github.com/Vertias3u/PassControl.git";
91
92
  // demo-tenant state and an aborted run would leave the public demo killed. It is
92
93
  // offered as a browser link instead — the one zero-install way to see the
93
94
  // pipeline when there is no local stack yet.
94
- const HOSTED_DEMO_URL = "https://passcontrol.vertias.eu";
95
+ // One literal, in cli/config.mjs. The demo and the Cloud gateway are the same
96
+ // origin, and keeping two copies is how they stop being the same origin.
97
+ const HOSTED_DEMO_URL = CLOUD_GATEWAY;
95
98
  const LOCAL_DASHBOARD_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
96
99
  const LOCAL_STACK_PORTS = [54321, 54322, 54324, 54327, 8079];
97
100
  // The local stack (Supabase + Redis + dashboard) lives in a PassControl repo
package/cli/config.mjs CHANGED
@@ -26,6 +26,20 @@ export const PROVIDERS = ["openai", "anthropic", "groq", "mistral", "together",
26
26
  export const OPENAI_SHAPE_PROVIDERS = new Set(["openai", "groq", "mistral", "together", "deepseek", "gemini"]);
27
27
 
28
28
  const DEFAULT_GATEWAY = "http://localhost:3000";
29
+
30
+ /**
31
+ * The hosted Cloud instance — the one origin this CLI knows by name.
32
+ *
33
+ * Declared HERE, once, because two copies drift: `bin/passcontrol.mjs` had the
34
+ * only literal (as `HOSTED_DEMO_URL`) and now imports this instead.
35
+ *
36
+ * It is NOT the same thing as DEFAULT_GATEWAY above, and the difference matters.
37
+ * DEFAULT_GATEWAY is what an already-configured operator falls back to while
38
+ * working locally. This is where a machine with NO configuration at all should
39
+ * be pointed — which is `passcontrol login` and nothing else, because login is
40
+ * the one command whose whole job is running before any config exists.
41
+ */
42
+ export const CLOUD_GATEWAY = "https://passcontrol.vertias.eu";
29
43
  const DEFAULT_PROVIDER = "anthropic";
30
44
  const CONFIG_KEYS = [
31
45
  "PASSCONTROL_GATEWAY",
package/cli/login.mjs CHANGED
@@ -38,6 +38,7 @@ import os from "node:os";
38
38
  import { ed25519 } from "@noble/curves/ed25519";
39
39
  import fs from "node:fs";
40
40
  import {
41
+ CLOUD_GATEWAY,
41
42
  fail,
42
43
  findProjectConfig,
43
44
  globalConfigPath,
@@ -49,6 +50,8 @@ import {
49
50
  warn,
50
51
  } from "./config.mjs";
51
52
 
53
+ export { CLOUD_GATEWAY };
54
+
52
55
  const b64url = (bytes) => Buffer.from(bytes).toString("base64url");
53
56
 
54
57
  /** Display form, `FKDR-8T2W`. Presentation only — the wire form has no hyphen. */
@@ -156,12 +159,19 @@ async function postJson(url, body, { timeoutMs = 15_000, fetchImpl = fetch } = {
156
159
  const controller = new AbortController();
157
160
  const timer = setTimeout(() => controller.abort(), timeoutMs);
158
161
  try {
159
- const res = await fetchImpl(url, {
160
- method: "POST",
161
- headers: { "content-type": "application/json" },
162
- body: JSON.stringify(body ?? {}),
163
- signal: controller.signal,
164
- });
162
+ let res;
163
+ try {
164
+ res = await fetchImpl(url, {
165
+ method: "POST",
166
+ headers: { "content-type": "application/json" },
167
+ body: JSON.stringify(body ?? {}),
168
+ signal: controller.signal,
169
+ });
170
+ } catch (error) {
171
+ const origin = new URL(url).origin;
172
+ const detail = error instanceof Error ? error.message : String(error);
173
+ throw new Error(`Could not reach the gateway at ${origin} (${detail}).`, { cause: error });
174
+ }
165
175
  let parsed = null;
166
176
  try {
167
177
  parsed = await res.json();
@@ -185,9 +195,51 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
185
195
  async function pollForGrant(origin, deviceCode, { interval, expiresIn, fetchImpl, now = Date.now }) {
186
196
  const deadline = now() + expiresIn * 1000;
187
197
  let waitMs = Math.max(500, interval * 1000);
198
+ // A transport failure is NOT fatal here, and this is the one place in the flow
199
+ // where that distinction matters. By the time we are polling, the operator is
200
+ // already looking at a code and waiting; ending the login on a single dropped
201
+ // request costs them the whole flow and a fresh code because a packet went
202
+ // missing or an edge function was cold. Only the deadline ends this loop.
203
+ //
204
+ // The `start` call above is the opposite case and stays fatal: nothing has been
205
+ // shown to anyone yet, and an unreachable gateway there is a configuration
206
+ // problem the operator needs told about immediately, not retried over.
207
+ // Bounded, because "survive a blip" and "hang for ten minutes" are not the same
208
+ // promise. A gateway that is genuinely down would otherwise leave the operator
209
+ // watching "Still waiting…" until the 600s code expiry, which reads as a frozen
210
+ // command. Ninety seconds of continuous failure is long enough to ride out a
211
+ // cold start or a reconnecting VPN and short enough to still feel like an answer.
212
+ const TRANSPORT_GRACE_MS = 90_000;
213
+ let transportFailures = 0;
214
+ let failingSince = 0;
188
215
  while (now() < deadline) {
189
216
  await sleep(waitMs);
190
- const res = await postJson(`${origin}/api/auth/device/token`, { device_code: deviceCode }, { fetchImpl });
217
+ let res;
218
+ try {
219
+ res = await postJson(`${origin}/api/auth/device/token`, { device_code: deviceCode }, { fetchImpl });
220
+ } catch {
221
+ // Back off like a slow_down, and say so once rather than every second — a
222
+ // wall of identical warnings buries the code the operator still needs.
223
+ transportFailures += 1;
224
+ if (transportFailures === 1) {
225
+ failingSince = now();
226
+ warn("Lost contact with the gateway. Still waiting…");
227
+ }
228
+ if (now() - failingSince > TRANSPORT_GRACE_MS) {
229
+ throw new Error(
230
+ `Lost contact with ${origin} and could not get it back. ` +
231
+ "Check the gateway is up, then run `passcontrol login` again."
232
+ );
233
+ }
234
+ waitMs = Math.min(waitMs * 2, 10_000);
235
+ continue;
236
+ }
237
+ if (transportFailures > 0) {
238
+ ok("reconnected");
239
+ transportFailures = 0;
240
+ failingSince = 0;
241
+ waitMs = Math.max(500, interval * 1000);
242
+ }
191
243
  if (res.status === 200 && res.body?.api_key) return res.body;
192
244
  const error = res.body?.error;
193
245
  if (error === "access_denied") {
@@ -266,6 +318,22 @@ async function confirmReplacingPassport(target, confirm) {
266
318
  return confirm("Replace it? [y/N] ", { default: false });
267
319
  }
268
320
 
321
+ /**
322
+ * Resolve login's destination without inheriting the ordinary localhost fallback.
323
+ *
324
+ * An explicit flag wins, then an environment/config value, then Cloud. Other
325
+ * commands keep DEFAULT_GATEWAY because they may intentionally operate a local
326
+ * stack; login is the exception because it is designed to run before any config
327
+ * exists. requireControlGateway keeps the existing bare-origin and HTTPS rules.
328
+ */
329
+ export function resolveLoginGateway(opts = {}, env = process.env) {
330
+ const fromFlag = typeof opts.gateway === "string" ? opts.gateway.trim() : "";
331
+ const fromConfig = typeof env.PASSCONTROL_GATEWAY === "string"
332
+ ? env.PASSCONTROL_GATEWAY.trim()
333
+ : "";
334
+ return requireControlGateway({ gateway: fromFlag || fromConfig || CLOUD_GATEWAY });
335
+ }
336
+
269
337
  export async function loginCommand(opts = {}, deps = {}) {
270
338
  const fetchImpl = deps.fetch ?? fetch;
271
339
  const openUrl = deps.openUrl ?? (() => {});
@@ -274,16 +342,36 @@ export async function loginCommand(opts = {}, deps = {}) {
274
342
 
275
343
  // Validate the destination BEFORE anything is requested — the ordering mintVisa
276
344
  // uses. A rejected gateway must cost zero sockets.
277
- const origin = requireControlGateway();
345
+ const origin = resolveLoginGateway(opts);
278
346
 
279
347
  const clientName = String(opts.name || os.hostname() || "this machine");
280
- const started = await postJson(
281
- `${origin}/api/auth/device/start`,
282
- { client_name: clientName },
283
- { fetchImpl }
284
- );
348
+ let started;
349
+ try {
350
+ started = await postJson(
351
+ `${origin}/api/auth/device/start`,
352
+ { client_name: clientName },
353
+ { fetchImpl }
354
+ );
355
+ } catch (error) {
356
+ // postJson has already named the host and kept the underlying failure on
357
+ // `cause`. This adds the part a first-run operator actually needs: what to do
358
+ // about it. Read `cause`, not `error.name` — the error reaching here is
359
+ // postJson's plain wrapper, so testing `error.name` for "AbortError" would
360
+ // never be true and the timeout wording would be dead code.
361
+ const underlying = error?.cause ?? error;
362
+ const reason = underlying?.name === "AbortError" ? "timed out" : "could not be reached";
363
+ throw new Error(
364
+ `${origin} ${reason} (${underlying?.message ?? "unknown"}).\n` +
365
+ ` • Self-hosting? Start your stack, or pass --gateway <origin>.\n` +
366
+ ` • Meant to use Cloud? Unset PASSCONTROL_GATEWAY, or pass --gateway ${CLOUD_GATEWAY}.`,
367
+ { cause: error }
368
+ );
369
+ }
285
370
  if (started.status !== 200 || !started.body?.user_code) {
286
- throw new Error(`Could not start a login at ${origin} (${started.status}).`);
371
+ throw new Error(
372
+ `${origin} answered ${started.status} to the login request. ` +
373
+ `If that origin is a PassControl instance, it may be older than this CLI.`
374
+ );
287
375
  }
288
376
  const { device_code: deviceCode, user_code: userCode } = started.body;
289
377
  const verificationUri = approvalUrl(origin, started.body.verification_uri, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "passcontrol",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Identity, scope and budget controls for AI agents while provider keys stay server-side.",
5
5
  "license": "BUSL-1.1",
6
6
  "homepage": "https://passcontrol.vertias.eu",