passport-bridge 0.16.10 → 0.16.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +16 -7
  2. package/dist/cli.js +6 -2
  3. package/dist/cliActions.js +20 -14
  4. package/dist/cliArgs.d.ts +3 -0
  5. package/dist/cliArgs.js +50 -6
  6. package/dist/cliAsk.d.ts +28 -0
  7. package/dist/cliAsk.js +49 -0
  8. package/dist/cliAutopilotPolicy.d.ts +15 -0
  9. package/dist/cliExec.d.ts +4 -0
  10. package/dist/cliExec.js +8 -1
  11. package/dist/cliGuardState.d.ts +20 -3
  12. package/dist/cliGuardState.js +24 -12
  13. package/dist/cliHook.js +4 -2
  14. package/dist/cliHookDecide.d.ts +16 -4
  15. package/dist/cliHookDecide.js +58 -14
  16. package/dist/cliHookDeliver.d.ts +17 -1
  17. package/dist/cliHookDeliver.js +70 -19
  18. package/dist/cliHookGuard.d.ts +9 -0
  19. package/dist/cliHookGuard.js +16 -2
  20. package/dist/cliHookInstall.d.ts +2 -0
  21. package/dist/cliHookInstall.js +2 -1
  22. package/dist/cliHookStatus.js +2 -4
  23. package/dist/cliKeys.d.ts +10 -3
  24. package/dist/cliKeys.js +60 -45
  25. package/dist/cliLogin.js +5 -2
  26. package/dist/cliMain.d.ts +7 -0
  27. package/dist/cliMain.js +502 -0
  28. package/dist/cliOutput.d.ts +3 -0
  29. package/dist/cliOutput.js +115 -8
  30. package/dist/cliPolicyV2.js +53 -2
  31. package/dist/cliSuggest.d.ts +19 -0
  32. package/dist/cliSuggest.js +57 -0
  33. package/dist/cliUndo.js +3 -8
  34. package/dist/compileCache.d.ts +8 -0
  35. package/dist/compileCache.js +60 -0
  36. package/dist/execNoKey.d.ts +9 -0
  37. package/dist/execNoKey.js +25 -0
  38. package/dist/hookMain.js +7 -11
  39. package/dist/hookOutbox.d.ts +15 -2
  40. package/dist/hookOutbox.js +143 -3
  41. package/dist/initKeysStep.d.ts +1 -1
  42. package/dist/initKeysStep.js +8 -7
  43. package/dist/initOutro.js +2 -1
  44. package/dist/initUi.d.ts +7 -2
  45. package/dist/initUi.js +22 -9
  46. package/dist/passport.d.ts +1 -7
  47. package/dist/passport.js +15 -499
  48. package/dist/pluginPin.d.ts +1 -1
  49. package/dist/pluginPin.js +1 -1
  50. package/dist/version.d.ts +1 -1
  51. package/dist/version.js +1 -1
  52. package/dist/webLinks.d.ts +2 -0
  53. package/dist/webLinks.js +7 -0
  54. package/npm-shrinkwrap.json +2 -2
  55. package/package.json +1 -1
@@ -2,7 +2,8 @@
2
2
  * Reports the shell hooks send later, so no hook ever waits on the network
3
3
  * after it has answered. The after-hook (`passport hook`) and the guard's own
4
4
  * report append here and return; a detached `passport hook flush` sends them
5
- * within the hook budget, at most one starting every couple of seconds.
5
+ * within the hook budget, at most one starting every couple of seconds and
6
+ * only one running at a time (`takeFlushLock`).
6
7
  *
7
8
  * Each line is one report exactly as it would have been sent: vendors, coarse
8
9
  * verbs, risk classes, ids, a folder name; never a command line. The file is
@@ -11,7 +12,7 @@
11
12
  */
12
13
  import { spawn } from "node:child_process";
13
14
  import { randomBytes } from "node:crypto";
14
- import { appendFileSync, closeSync, constants, lstatSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, statSync, utimesSync } from "node:fs";
15
+ import { appendFileSync, closeSync, constants, lstatSync, mkdirSync, openSync, linkSync, readFileSync, renameSync, rmSync, statSync, utimesSync, writeSync } from "node:fs";
15
16
  import { join } from "node:path";
16
17
  import { credentialDir, enforceOwner, ensurePrivateDirectory } from "./credentialStorage.js";
17
18
  import { assertProfileName } from "./credentials.js";
@@ -22,6 +23,9 @@ const MAX_FILE_BYTES = 256 * 1024;
22
23
  const MAX_AGE_MS = 24 * 60 * 60 * 1000;
23
24
  /** At most one flush starts in this window. */
24
25
  export const FLUSH_GAP_MS = 2_000;
26
+ /** A flush lock this old belongs to a flush that died without letting go: a
27
+ * live one exits well before (`FLUSH_DEADLINE_MS`). */
28
+ export const FLUSH_LOCK_STALE_MS = 10_000;
25
29
  function directory() {
26
30
  return join(credentialDir(), DIRECTORY);
27
31
  }
@@ -125,11 +129,14 @@ export function hasQueued(profile) {
125
129
  return false;
126
130
  }
127
131
  }
128
- /** Claim the next flush: true at most once per FLUSH_GAP_MS. */
132
+ /** Claim the next flush: true at most once per FLUSH_GAP_MS, and never while
133
+ * another flush still runs. */
129
134
  export function claimFlush(profile, now = Date.now()) {
130
135
  try {
131
136
  ensureDirectory();
132
137
  assertProfileName(profile);
138
+ if (flushRunning(profile))
139
+ return false;
133
140
  const marker = join(directory(), `.${profile}.flush`);
134
141
  try {
135
142
  const last = statSync(marker).mtimeMs;
@@ -149,6 +156,139 @@ export function claimFlush(profile, now = Date.now()) {
149
156
  return false;
150
157
  }
151
158
  }
159
+ function lockPath(profile) {
160
+ assertProfileName(profile);
161
+ return join(directory(), `.${profile}.flushing`);
162
+ }
163
+ /** The lock as found, or null when there is none. Anything that isn't this
164
+ * person's own plain file counts as held: a flush never removes it. */
165
+ function readLock(path) {
166
+ let stat;
167
+ try {
168
+ stat = lstatSync(path);
169
+ }
170
+ catch {
171
+ return null;
172
+ }
173
+ try {
174
+ enforceOwner(stat, "hook flush lock");
175
+ if (!stat.isFile() || stat.size > 128)
176
+ throw new Error("not a lock");
177
+ const [pid, nonce] = readFileSync(path, "utf8").trim().split(" ");
178
+ const parsed = Number(pid);
179
+ return { pid: Number.isSafeInteger(parsed) && parsed > 0 ? parsed : null, nonce: nonce ?? "", at: stat.mtimeMs };
180
+ }
181
+ catch {
182
+ return { pid: null, nonce: "", at: Number.POSITIVE_INFINITY };
183
+ }
184
+ }
185
+ function alive(pid) {
186
+ try {
187
+ process.kill(pid, 0);
188
+ return true;
189
+ }
190
+ catch (error) {
191
+ return error.code === "EPERM";
192
+ }
193
+ }
194
+ /** A lock whose flush is gone: its process exited, or it is far too old. A
195
+ * lock still being written (no pid yet) is fresh until it is old. */
196
+ function stale(lock, now) {
197
+ if (lock.at === Number.POSITIVE_INFINITY)
198
+ return false;
199
+ if (lock.pid !== null && !alive(lock.pid))
200
+ return true;
201
+ return Math.abs(now - lock.at) > FLUSH_LOCK_STALE_MS;
202
+ }
203
+ /** Whether a flush for this profile is running now. */
204
+ export function flushRunning(profile, now = Date.now()) {
205
+ try {
206
+ const lock = readLock(lockPath(profile));
207
+ return lock !== null && !stale(lock, now);
208
+ }
209
+ catch {
210
+ return false;
211
+ }
212
+ }
213
+ /**
214
+ * Take the one flush slot for this profile: a release function, or null when
215
+ * another flush holds it (this one then has nothing to do). A lock left by a
216
+ * flush that died is taken over. Never throws.
217
+ */
218
+ export function takeFlushLock(profile, now = Date.now()) {
219
+ try {
220
+ ensureDirectory();
221
+ const path = lockPath(profile);
222
+ const nonce = randomBytes(8).toString("hex");
223
+ const noFollow = typeof constants.O_NOFOLLOW === "number" ? constants.O_NOFOLLOW : 0;
224
+ const create = () => {
225
+ const fd = openSync(path, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | noFollow, 0o600);
226
+ try {
227
+ writeSync(fd, `${process.pid} ${nonce}\n`);
228
+ }
229
+ finally {
230
+ closeSync(fd);
231
+ }
232
+ };
233
+ try {
234
+ create();
235
+ }
236
+ catch (error) {
237
+ if (error.code !== "EEXIST")
238
+ return null;
239
+ const found = readLock(path);
240
+ if (!found) {
241
+ // Let go between the two looks: one more try.
242
+ create();
243
+ return release(path, nonce);
244
+ }
245
+ if (!stale(found, now))
246
+ return null;
247
+ // Move the dead flush's lock aside, and take over only if what moved is
248
+ // the lock judged dead: a flush that took over first keeps its own.
249
+ const aside = `${path}.${process.pid}.${nonce}.stale`;
250
+ try {
251
+ renameSync(path, aside);
252
+ }
253
+ catch {
254
+ return null;
255
+ }
256
+ const moved = readLock(aside);
257
+ if (!moved || moved.pid !== found.pid || moved.nonce !== found.nonce) {
258
+ try {
259
+ linkSync(aside, path);
260
+ }
261
+ catch {
262
+ /* Another flush holds a newer lock already. */
263
+ }
264
+ rmSync(aside, { force: true });
265
+ return null;
266
+ }
267
+ rmSync(aside, { force: true });
268
+ create();
269
+ }
270
+ return release(path, nonce);
271
+ }
272
+ catch {
273
+ return null;
274
+ }
275
+ }
276
+ /** Let go of the lock, once, and only while it is still this flush's own. */
277
+ function release(path, nonce) {
278
+ let held = true;
279
+ return () => {
280
+ if (!held)
281
+ return;
282
+ held = false;
283
+ try {
284
+ if (readLock(path)?.nonce === nonce)
285
+ rmSync(path, { force: true });
286
+ }
287
+ catch {
288
+ /* Stale soon: its process is gone. */
289
+ }
290
+ };
291
+ }
152
292
  /**
153
293
  * Start `passport hook flush` on its own: detached, no stdio, not waited for,
154
294
  * so the hook that started it exits at once and the agent never waits on the
@@ -12,6 +12,6 @@ export type KeysPlan = {
12
12
  export declare function detectKeys(s: InitSession): Promise<KeysPlan>;
13
13
  /**
14
14
  * "move" stores every key without asking (--move-keys), "ask" asks per key
15
- * (Customize; `[y/n]`, no default), "keep" leaves them where they are.
15
+ * (Customize; `[y/N]`), "keep" leaves them where they are.
16
16
  */
17
17
  export declare function applyKeys(s: InitSession, plan: KeysPlan, mode: "move" | "ask" | "keep"): Promise<void>;
@@ -5,9 +5,10 @@
5
5
  * `passport keys move`. Signing a CLI out is never part of the recommended
6
6
  * setup: it takes --move-keys, or a yes to each key under Customize.
7
7
  *
8
- * Keys Store your Railway account token in Passport and sign the Railway CLI out on this computer? [y/n]
8
+ * Keys Store your Railway account token in Passport and sign the Railway CLI out on this computer? [y/N]
9
9
  * ✓ Railway: stored in Passport, removed from this computer
10
10
  */
11
+ import { choices } from "./cliAsk.js";
11
12
  import { EXEC_VENDOR_NAMES } from "./execVendors.js";
12
13
  import { computerName, moveKeys, scanCounts, KEYS_PATH } from "./cliKeys.js";
13
14
  import { noteExecVendor } from "./execMarker.js";
@@ -50,7 +51,7 @@ export async function detectKeys(s) {
50
51
  }
51
52
  /**
52
53
  * "move" stores every key without asking (--move-keys), "ask" asks per key
53
- * (Customize; `[y/n]`, no default), "keep" leaves them where they are.
54
+ * (Customize; `[y/N]`), "keep" leaves them where they are.
54
55
  */
55
56
  export async function applyKeys(s, plan, mode) {
56
57
  // Nothing to move: no summary entry.
@@ -75,15 +76,15 @@ export async function applyKeys(s, plan, mode) {
75
76
  };
76
77
  const summary = await moveKeys({
77
78
  api: (path, init) => s.api(path, init),
78
- // Signs a CLI out on this computer: `[y/n]` with no default. With
79
+ // Signs a CLI out on this computer: `[y/N]`, Enter keeps it. With
79
80
  // --move-keys the plan already said so: each key shows it answered yes.
80
81
  confirm: (question) => mode === "move"
81
- ? Promise.resolve(s.screen.line(`${question} [y/n] ${s.screen.paint("2", "yes")}`, label())).then(() => true)
82
+ ? Promise.resolve(s.screen.line(`${question} ${choices("destructive")} ${s.screen.paint("2", "yes")}`, label())).then(() => true)
82
83
  : s.screen.confirm(question, s.signal, label(), { destructive: true }),
83
- // Replacing a different key Passport holds changes stored credentials:
84
- // only a person answering may say yes, never --yes.
84
+ // Replacing a different key Passport holds changes what every agent uses:
85
+ // a person types the CLI's name, never --yes.
85
86
  ...(s.screen.terminal.interactive && !s.yes
86
- ? { replace: (question) => s.screen.confirm(question, s.signal, label(), { destructive: true }) }
87
+ ? { replace: (question, name) => s.screen.confirmName(question, name, s.signal, label()) }
87
88
  : {}),
88
89
  // In the repo that deploys Passport: [Y/n], and --yes keeps the key here.
89
90
  keepHere: (question) => s.screen.confirm(question, s.signal, label()),
package/dist/initOutro.js CHANGED
@@ -10,6 +10,7 @@
10
10
  */
11
11
  import { EXEC_TOKEN_PAGES, EXEC_VENDOR_NAMES } from "./execVendors.js";
12
12
  import { AGENT_NAMES } from "./initAgents.js";
13
+ import { CODEX_BLOCK_ONLY } from "./cliHookInstall.js";
13
14
  import { pathLine } from "./initInstall.js";
14
15
  import { desktopLine } from "./initLocalSteps.js";
15
16
  import { TRY_IT, appLabel } from "./initState.js";
@@ -106,7 +107,7 @@ function agentFollowUps(s) {
106
107
  if (s.codexNext)
107
108
  items.push(warn(`Codex: sign in to Passport once: ${s.codexNext}`));
108
109
  if (s.codexTrust)
109
- items.push(warn("Codex: blocks only. Trust the Passport hooks in Codex's /hooks."));
110
+ items.push(warn(`Codex: open Codex → /hooks and trust Passport's hooks. ${CODEX_BLOCK_ONLY}`));
110
111
  const rows = (s.summary.steps.agents?.agents ?? []);
111
112
  if (s.terminal.interactive && rows.some((row) => row.agent === "claude" && row.status === "needs_attention")) {
112
113
  items.push(warn(desktopLine(Boolean(s.desktopCopied))));
package/dist/initUi.d.ts CHANGED
@@ -130,13 +130,18 @@ export declare class InitScreen {
130
130
  plain(lines: string[]): void;
131
131
  /**
132
132
  * `[Y/n]`, defaulting to yes. A destructive question (it signs a CLI out or
133
- * removes a key from this computer) shows `[y/n]` and has no default: an
134
- * empty answer asks again. With --yes, answers itself and shows it did.
133
+ * removes a key from this computer) shows `[y/N]`, defaulting to no
134
+ * (`cliAsk.ts`). With --yes, answers itself and shows it did.
135
135
  */
136
136
  confirm(question: string, signal: AbortSignal, label?: string, options?: {
137
137
  destructive?: boolean;
138
138
  top?: boolean;
139
139
  }): Promise<boolean>;
140
+ /**
141
+ * A severe question: only typing `name` says yes. Never answered by --yes,
142
+ * and without a person to ask the answer is no.
143
+ */
144
+ confirmName(question: string, name: string, signal: AbortSignal, label?: string): Promise<boolean>;
140
145
  /** A numbered choice. Returns an index, or undefined when there is no answer. */
141
146
  choose(question: string, options: string[], signal: AbortSignal, label?: string): Promise<number | undefined>;
142
147
  /**
package/dist/initUi.js CHANGED
@@ -14,6 +14,7 @@
14
14
  * clipped to one row so it's always replaced cleanly.
15
15
  */
16
16
  import { createInterface } from "node:readline";
17
+ import { answerOf, ASK_AGAIN, choices, nameConfirmed, typeNamePrompt } from "./cliAsk.js";
17
18
  import { clip, mark as markOf, paint as paintText, plainText, supportsUnicode, table, termOf, visibleLength, wrap } from "./term.js";
18
19
  export { supportsUnicode, visibleLength };
19
20
  export const LABEL_WIDTH = 10;
@@ -430,12 +431,13 @@ export class InitScreen {
430
431
  }
431
432
  /**
432
433
  * `[Y/n]`, defaulting to yes. A destructive question (it signs a CLI out or
433
- * removes a key from this computer) shows `[y/n]` and has no default: an
434
- * empty answer asks again. With --yes, answers itself and shows it did.
434
+ * removes a key from this computer) shows `[y/N]`, defaulting to no
435
+ * (`cliAsk.ts`). With --yes, answers itself and shows it did.
435
436
  */
436
437
  async confirm(question, signal, label, options = {}) {
437
438
  const destructive = options.destructive === true;
438
- const prompt = `${question} ${destructive ? "[y/n]" : "[Y/n]"}`;
439
+ const stakes = destructive ? "destructive" : "safe";
440
+ const prompt = `${question} ${choices(stakes)}`;
439
441
  if (this.yes || !this.terminal.interactive) {
440
442
  // Without --yes and a person to ask, a destructive change doesn't happen.
441
443
  const yes = this.yes || !destructive;
@@ -450,17 +452,28 @@ export class InitScreen {
450
452
  this.labelled = true;
451
453
  let lead = options.top ? "" : this.leadFor(fresh ? label : undefined);
452
454
  for (;;) {
453
- const answer = (await this.question(lead, prompt, signal, options.top ? "" : CONTINUATION)).trim().toLowerCase();
454
- if ((answer === "" && !destructive) || answer === "y" || answer === "yes")
455
- return true;
456
- if (answer === "n" || answer === "no")
457
- return false;
458
- this.line("Type y or n.");
455
+ const answer = answerOf(await this.question(lead, prompt, signal, options.top ? "" : CONTINUATION), stakes);
456
+ if (answer !== undefined)
457
+ return answer;
458
+ this.line(ASK_AGAIN);
459
459
  // Asked again under the step, not as a new step.
460
460
  if (!options.top)
461
461
  lead = CONTINUATION;
462
462
  }
463
463
  }
464
+ /**
465
+ * A severe question: only typing `name` says yes. Never answered by --yes,
466
+ * and without a person to ask the answer is no.
467
+ */
468
+ async confirmName(question, name, signal, label) {
469
+ if (this.yes || !this.terminal.interactive) {
470
+ this.line(`${question} ${this.paint("2", "no")}`, label);
471
+ return false;
472
+ }
473
+ this.line(question, label);
474
+ const answer = await this.question(CONTINUATION, typeNamePrompt(name), signal);
475
+ return nameConfirmed(answer, name);
476
+ }
464
477
  /** A numbered choice. Returns an index, or undefined when there is no answer. */
465
478
  async choose(question, options, signal, label) {
466
479
  this.line(question, label);
@@ -1,8 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import { type Env, type Runtime } from "./cliSupport.js";
3
- export { refusalOf } from "./cliResult.js";
4
- export { callerFrom, sanitize } from "./cliSupport.js";
5
- export declare function main(argv?: string[], env?: Env, io?: Pick<Runtime, "stdout" | "stderr"> & {
6
- stdin?: Runtime["stdin"];
7
- invokedAs?: Runtime["invokedAs"];
8
- }): Promise<number>;
2
+ export {};