passport-bridge 0.27.0 → 0.28.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/cliHook.js +2 -2
- package/dist/cliHookDecide.d.ts +8 -1
- package/dist/cliHookDecide.js +52 -4
- package/dist/cliHookSessionEnd.d.ts +48 -0
- package/dist/cliHookSessionEnd.js +162 -0
- package/dist/pluginPin.d.ts +1 -1
- package/dist/pluginPin.js +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -246,7 +246,7 @@ Two hooks per agent. `passport hook` runs after each shell command and records t
|
|
|
246
246
|
|
|
247
247
|
The environment comes from each vendor's own flags (`src/cliHookEnvironment.ts`): Railway `-e/--environment`, Wrangler and Sentry `-e/--env`, Vercel `--prod`/`--target` (a deploy without either is a preview), Supabase `--local`/`--linked`/`--project-ref` (`db reset` is local unless `--linked`), Stripe `--live` (test mode otherwise), `git push <remote> <branch>`, `psql` hosts (`localhost` is Development), fly and Heroku `-a/--app`, kubectl `--context`/`-n`, helm `--kube-context`/`-n`, AWS `--profile`, gcloud `--project`, plus any flags the server adds. Names follow Passport's rules (`prod|production|live|main` → Production, `stag|qa|uat|preview` → Staging, `dev|local|test|sandbox|feature|branch` → Development), and the workspace's own placement of a name wins. Anything else, or no flag at all (a linked project the hook can't see), counts as Production, and the reason says `Environment unknown, treated as Production.`
|
|
248
248
|
|
|
249
|
-
**Attended or unattended** (`src/cliHookMode.ts`). With a person at the keyboard, an ask is the agent's own prompt with Passport's reason. With nobody there, the guard asks Passport instead (`POST /api/my/cli/decide`, inside the hook's budget): the server knows grants already given this session, every rule, and the workspace's environments. An allow from it runs the command (for example, a retry after approval); an ask becomes an approval request on the web and the person's phone, and the agent reads ``Waiting for approval from Ramish. Run `passport wait <id>` to wait, or continue with other work.`` If Passport can't answer in time, the local decision stands and the reason adds `Passport couldn't be reached, so this computer decided.` Signals, strongest first:
|
|
249
|
+
**Attended or unattended** (`src/cliHookMode.ts`). With a person at the keyboard, an ask is the agent's own prompt with Passport's reason, except an irreversible command: it is approved only in Passport, with a passkey check, so the guard files the request and holds as below, and blocks with `Passport can't be reached. Irreversible commands need approval in Passport.` if Passport doesn't answer. With nobody there, the guard asks Passport instead (`POST /api/my/cli/decide`, inside the hook's budget): the server knows grants already given this session, every rule, and the workspace's environments. An allow from it runs the command (for example, a retry after approval); an ask becomes an approval request on the web and the person's phone, and the agent reads ``Waiting for approval from Ramish. Run `passport wait <id>` to wait, or continue with other work.`` If Passport can't answer in time, the local decision stands and the reason adds `Passport couldn't be reached, so this computer decided.` Signals, strongest first:
|
|
250
250
|
|
|
251
251
|
| Signal | Means |
|
|
252
252
|
| ------------------------------------------------------------------------------------------------- | --------------------------------------- |
|
|
@@ -261,7 +261,7 @@ Codex has no field for `codex exec`; its prompt-free modes count as unattended,
|
|
|
261
261
|
|
|
262
262
|
The approval request carries a command summary with secrets removed on this computer (`src/cliHookRedact.ts`, using `passport scan`'s key formats plus secret-named flags, variables, JSON fields, URL credentials, and long opaque values; heredoc bodies are dropped). It is the only part of a command line that ever leaves the machine, and only approvers see it.
|
|
263
263
|
|
|
264
|
-
The guard decides from local data inside `HOOK_BUDGET_MS` (1.5 s) and prints first; only an unattended ask waits for the server, within the same budget. After answering it never waits on the network: its report, the batches, and, at most every ten minutes, a refresh of a policy cache older than an hour go through the detached `passport hook flush`. Any failure is silent and fails open. Neither hook sends the raw command line, output, folder path, or environment. Both hooks exchange the profile's refresh credential without rotating it and keep the short-lived access token in `$PASSPORT_HOME/.<profile>.access.json` (`src/accessTokenCache.ts`, mode 600, atomic replace), so most runs send one request instead of an exchange and then the request. A cached token is used only with more than a minute left; a 401 on it drops it, exchanges once, and retries once.
|
|
264
|
+
The guard decides from local data inside `HOOK_BUDGET_MS` (1.5 s) and prints first; only an unattended ask (or an irreversible one) waits for the server, within the same budget. The one exception is End session (`src/cliHookSessionEnd.ts`): before a command it would let run on its own (an allow, or nothing to decide), a signed-in guard asks `GET /api/my/cli/session` (agent and session id only) whether a person ended that session, at most every 15 s per session and for at most 300 ms; an ended session is cached and refused with the server's line from then on, and no answer means the command proceeds. It is a guardrail, not a security boundary: the launcher's fast-path reads and a guard running with `PASSPORT_AGENT_KEY` don't check. After answering it never waits on the network: its report, the batches, and, at most every ten minutes, a refresh of a policy cache older than an hour go through the detached `passport hook flush`. Any failure is silent and fails open. Neither hook sends the raw command line, output, folder path, or environment. Both hooks exchange the profile's refresh credential without rotating it and keep the short-lived access token in `$PASSPORT_HOME/.<profile>.access.json` (`src/accessTokenCache.ts`, mode 600, atomic replace), so most runs send one request instead of an exchange and then the request. A cached token is used only with more than a minute left; a 401 on it drops it, exchanges once, and retries once.
|
|
265
265
|
|
|
266
266
|
An ask becomes one Activity row. In Claude Code, with the audit hook registered, the guard parks its report in `$PASSPORT_HOME/hook-pending/` (`src/cliHookPending.ts`, mode 600, claimed by atomic rename). The audit hook for the same tool-use id sends the run with `decision: "ask", ran: true` ("asked first, then ran"). An ask nobody claims within 30 minutes (no agent fires a hook for a declined prompt) is sent by the next hook run as `ran: false` ("asked first, not run"). An allow the server gave is parked the same way, so the run's row says `Approved earlier this session`. Cursor has no tool-use id to match, so its asks are sent at once as their own "asked first" row. Denies are always sent at once; asks and blocks the server decided are recorded by the server.
|
|
267
267
|
|
package/dist/cliHook.js
CHANGED
|
@@ -505,9 +505,9 @@ async function runGuard(args, runtime, deps) {
|
|
|
505
505
|
const plan = await planGuard(decision, policy, {
|
|
506
506
|
client,
|
|
507
507
|
mode,
|
|
508
|
-
canAsk:
|
|
508
|
+
canAsk: canReport && !event.cursorImported,
|
|
509
509
|
openApi: () => profileApi(profile, budget, deps.fetch ?? fetch).catch(() => null),
|
|
510
|
-
report: base,
|
|
510
|
+
report: base ?? context(input, runtime.env, declared),
|
|
511
511
|
local,
|
|
512
512
|
command: event.command,
|
|
513
513
|
budget,
|
package/dist/cliHookDecide.d.ts
CHANGED
|
@@ -99,13 +99,20 @@ export declare function offlineVerdict(decision: GuardDecision, policy: CachedCl
|
|
|
99
99
|
secondPerson: boolean;
|
|
100
100
|
attended: boolean;
|
|
101
101
|
}, now: number): OfflineVerdict;
|
|
102
|
+
/** What an attended irreversible ask says when Passport can't be reached. */
|
|
103
|
+
export declare const IRREVERSIBLE_UNREACHABLE = "Passport can't be reached. Irreversible commands need approval in Passport.";
|
|
104
|
+
/** What an irreversible ask tells the agent while it waits in Passport. */
|
|
105
|
+
export declare const IRREVERSIBLE_IN_PASSPORT = "Approve this in Passport. Irreversible actions need a passkey check.";
|
|
102
106
|
/**
|
|
103
107
|
* Attended, or no v2 policy: the local decision, printed at once. Unattended
|
|
104
108
|
* ask: the server decides within the budget, and an approval request replaces
|
|
105
109
|
* the prompt nobody would answer; a server that can't answer in time leaves
|
|
106
110
|
* the local decision with one line saying so.
|
|
107
111
|
*/
|
|
108
|
-
export declare function planGuard(
|
|
112
|
+
export declare function planGuard(...args: Parameters<typeof decidePlan>): Promise<GuardPlan & {
|
|
113
|
+
api?: ApiCall;
|
|
114
|
+
}>;
|
|
115
|
+
declare function decidePlan(decision: GuardDecision | null, policy: CachedCliPolicy | null, context: {
|
|
109
116
|
client: GuardClient;
|
|
110
117
|
mode: HookMode;
|
|
111
118
|
/** Signed in with a member profile that may report. */
|
package/dist/cliHookDecide.js
CHANGED
|
@@ -23,6 +23,7 @@ import { OFFLINE_CACHE_MAX_MS, OFFLINE_DEFAULT, OFFLINE_NOTES, offlineDecision }
|
|
|
23
23
|
import { commandKey } from "./cliHookKey.js";
|
|
24
24
|
import { unknownVerb, UNRECOGNIZED_VERB } from "./cliHookClassify.js";
|
|
25
25
|
import { redactCommand } from "./cliHookRedact.js";
|
|
26
|
+
import { endedSessionLine, rememberEnded } from "./cliHookSessionEnd.js";
|
|
26
27
|
export const CLI_DECIDE_PATH = "/api/my/cli/decide";
|
|
27
28
|
const MAX_RESPONSE_BYTES = 16 * 1024;
|
|
28
29
|
const MAX_FINDINGS = 8;
|
|
@@ -128,7 +129,11 @@ export async function decideOnServer(api, request, signal) {
|
|
|
128
129
|
await response.body?.cancel().catch(() => undefined);
|
|
129
130
|
return null;
|
|
130
131
|
}
|
|
131
|
-
|
|
132
|
+
const decided = parseServerDecision(await readBoundedJson(response, MAX_RESPONSE_BYTES));
|
|
133
|
+
// Passport refused because a person ended this session: remember it here.
|
|
134
|
+
if (decided?.decision === "block")
|
|
135
|
+
rememberEnded(request, decided.message);
|
|
136
|
+
return decided;
|
|
132
137
|
}
|
|
133
138
|
catch {
|
|
134
139
|
return null;
|
|
@@ -223,13 +228,48 @@ function serverOutput(client, decision, server) {
|
|
|
223
228
|
: {};
|
|
224
229
|
return guardOutput(client, decided, options);
|
|
225
230
|
}
|
|
231
|
+
/** What an attended irreversible ask says when Passport can't be reached. */
|
|
232
|
+
export const IRREVERSIBLE_UNREACHABLE = "Passport can't be reached. Irreversible commands need approval in Passport.";
|
|
233
|
+
/** What an irreversible ask tells the agent while it waits in Passport. */
|
|
234
|
+
export const IRREVERSIBLE_IN_PASSPORT = "Approve this in Passport. Irreversible actions need a passkey check.";
|
|
235
|
+
/** Whether any command in the line that asked is irreversible. */
|
|
236
|
+
function irreversibleAsk(decision) {
|
|
237
|
+
return (decision.actionClass === "irreversible" ||
|
|
238
|
+
(decision.decided ?? []).some((entry) => entry.decision === "ask" && entry.actionClass === "irreversible"));
|
|
239
|
+
}
|
|
240
|
+
function attendedIrreversible(decision, mode) {
|
|
241
|
+
return mode !== "unattended" && irreversibleAsk(decision);
|
|
242
|
+
}
|
|
243
|
+
/** An attended ask the agent's own prompt answers (Codex can't ask from a hook). */
|
|
244
|
+
function staysLocal(context, toPassport) {
|
|
245
|
+
return context.mode !== "unattended" && !toPassport && context.client !== "codex";
|
|
246
|
+
}
|
|
247
|
+
function unreachableIrreversible(client, decision, api) {
|
|
248
|
+
const output = guardOutput(client, { ...decision, verdict: "deny" }, { waiting: IRREVERSIBLE_UNREACHABLE });
|
|
249
|
+
return { output, server: null, offline: true, verdict: "deny", ...(api ? { api } : {}) };
|
|
250
|
+
}
|
|
251
|
+
/** A person ended this session in Passport: nothing more runs in it, even a
|
|
252
|
+
* command this computer would let run (`cliHookSessionEnd.ts`). */
|
|
253
|
+
async function endedPlan(decision, context) {
|
|
254
|
+
// Only what would run without anyone deciding: an ask is a person's call.
|
|
255
|
+
if (!context.canAsk || (decision && decision.verdict !== "allow"))
|
|
256
|
+
return null;
|
|
257
|
+
const ended = await endedSessionLine(context.report, context.openApi, context);
|
|
258
|
+
if (!ended)
|
|
259
|
+
return null;
|
|
260
|
+
const refused = decision ?? { verdict: "deny", label: "command", findings: [], destructive: false };
|
|
261
|
+
return { output: guardOutput(context.client, refused, { waiting: ended }), server: null, offline: false };
|
|
262
|
+
}
|
|
226
263
|
/**
|
|
227
264
|
* Attended, or no v2 policy: the local decision, printed at once. Unattended
|
|
228
265
|
* ask: the server decides within the budget, and an approval request replaces
|
|
229
266
|
* the prompt nobody would answer; a server that can't answer in time leaves
|
|
230
267
|
* the local decision with one line saying so.
|
|
231
268
|
*/
|
|
232
|
-
export async function planGuard(
|
|
269
|
+
export async function planGuard(...args) {
|
|
270
|
+
return (await endedPlan(args[0], args[2])) ?? decidePlan(...args);
|
|
271
|
+
}
|
|
272
|
+
async function decidePlan(decision, policy, context) {
|
|
233
273
|
const local = { output: decision ? guardOutput(context.client, decision) : null, server: null, offline: false };
|
|
234
274
|
// A Production action someone else must approve goes to Passport even when
|
|
235
275
|
// someone is at the keyboard: the agent's own prompt would let them approve it.
|
|
@@ -240,9 +280,12 @@ export async function planGuard(decision, policy, context) {
|
|
|
240
280
|
Boolean(policy.autopilot.secondPerson?.classes.includes(decision.actionClass)));
|
|
241
281
|
if (!decision || decision.verdict !== "ask" || policy?.version !== 2)
|
|
242
282
|
return local;
|
|
283
|
+
// An irreversible action is approved only in Passport, with a passkey check:
|
|
284
|
+
// the one attended ask that waits on the network (docs/DECISIONS.md).
|
|
285
|
+
const irreversible = attendedIrreversible(decision, context.mode);
|
|
243
286
|
// Codex can't ask from a hook: like an unattended run, the ask goes to
|
|
244
287
|
// Passport as an approval request instead of a prompt nobody would see.
|
|
245
|
-
if (context
|
|
288
|
+
if (staysLocal(context, secondPerson || irreversible))
|
|
246
289
|
return local;
|
|
247
290
|
if (!context.canAsk)
|
|
248
291
|
return local;
|
|
@@ -262,6 +305,9 @@ export async function planGuard(decision, policy, context) {
|
|
|
262
305
|
const server = api && left > 50
|
|
263
306
|
? await decideOnServer(api, request, AbortSignal.any([context.budget, AbortSignal.timeout(left)]))
|
|
264
307
|
: null;
|
|
308
|
+
// Never the agent's own Approve prompt, even with Passport out of reach.
|
|
309
|
+
if (!server && irreversible)
|
|
310
|
+
return unreachableIrreversible(context.client, decision, api);
|
|
265
311
|
if (!server) {
|
|
266
312
|
// Passport didn't answer in time: the environment's offline setting decides.
|
|
267
313
|
const offline = offlineVerdict(decision, policy, { client: context.client, secondPerson, attended: context.mode === "attended" }, context.now());
|
|
@@ -288,7 +334,9 @@ async function heldPlan(decision, server, request, api, context) {
|
|
|
288
334
|
return { output: serverOutput(context.client, decision, server), server, offline: false, api };
|
|
289
335
|
const hold = await import("./cliHookHold.js");
|
|
290
336
|
const pending = (who, expiresAt, approvalId) => guardOutput(context.client, { ...decision, verdict: "deny" }, {
|
|
291
|
-
|
|
337
|
+
// An irreversible action says first that it's approved in Passport.
|
|
338
|
+
waiting: (irreversibleAsk(decision) ? `${IRREVERSIBLE_IN_PASSPORT} ` : "") +
|
|
339
|
+
hold.pendingMessage(approvalId ?? waiting.approvalId, who ?? waiting.waitingFor, expiresAt ?? waiting.expiresAt, context.now())
|
|
292
340
|
});
|
|
293
341
|
// Only Claude Code holds: Codex can't run an approved command from a hook,
|
|
294
342
|
// and Cursor's behaviour past a hook's timeout isn't documented.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* End session, on this computer. When a person ends an agent's session in
|
|
3
|
+
* Passport, the server refuses everything that reaches it under that session.
|
|
4
|
+
* The guard decides most commands here, so before a command it would let run
|
|
5
|
+
* on its own (an allow, or nothing to decide; an ask is a person's call) it
|
|
6
|
+
* asks Passport whether the session was ended (`GET /api/my/cli/session`),
|
|
7
|
+
* and refuses with the server's own line when it was.
|
|
8
|
+
*
|
|
9
|
+
* It costs little: a session checked in the last 15 seconds isn't asked
|
|
10
|
+
* again, a session known to be ended is never asked again, and the question
|
|
11
|
+
* never waits more than 300 ms. On a timeout or any error the command
|
|
12
|
+
* proceeds as it would have: the guard is a guardrail, not a security
|
|
13
|
+
* boundary. A refusal from the decide fast path teaches it too.
|
|
14
|
+
*
|
|
15
|
+
* State lives in owner-only files named by a digest of the agent and session
|
|
16
|
+
* id, holding only when it was checked and, once ended, the server's line.
|
|
17
|
+
* Bounded in count and age. Never throws.
|
|
18
|
+
*/
|
|
19
|
+
export declare const CLI_SESSION_PATH = "/api/my/cli/session";
|
|
20
|
+
/** A session checked this recently isn't asked about again. */
|
|
21
|
+
export declare const SESSION_FRESH_MS = 15000;
|
|
22
|
+
/** The longest a command the guard would let run waits on the question. */
|
|
23
|
+
export declare const SESSION_CHECK_MS = 300;
|
|
24
|
+
/** The server's line, exactly: "Session ended by Maya at 14:05 UTC. Start a new session to continue." */
|
|
25
|
+
export declare const ENDED_LINE: RegExp;
|
|
26
|
+
type ApiCall = (path: string, init: RequestInit) => Promise<Response>;
|
|
27
|
+
export type SessionRef = {
|
|
28
|
+
runtime: string;
|
|
29
|
+
sessionId?: string;
|
|
30
|
+
} | null;
|
|
31
|
+
/** A refusal Passport sent for this session (decide): remember it, so the
|
|
32
|
+
* guard refuses the rest here without asking. */
|
|
33
|
+
export declare function rememberEnded(session: SessionRef, message: string | undefined, now?: number): void;
|
|
34
|
+
/** Validate what Passport said: the line when the session ended, "" when it
|
|
35
|
+
* runs, null for anything unexpected. */
|
|
36
|
+
export declare function parseSessionState(raw: unknown): string | null;
|
|
37
|
+
/**
|
|
38
|
+
* The line to refuse with when a person ended this session, else undefined.
|
|
39
|
+
* Known ended: no network. Checked in the last 15 s: no network. Otherwise one
|
|
40
|
+
* question, bounded by 300 ms (and the hook's own budget); no answer in time
|
|
41
|
+
* means the command proceeds, and the session is asked again after 15 s.
|
|
42
|
+
*/
|
|
43
|
+
export declare function endedSessionLine(session: SessionRef, openApi: () => Promise<ApiCall | null>, options: {
|
|
44
|
+
now: () => number;
|
|
45
|
+
budget: AbortSignal;
|
|
46
|
+
timeoutMs?: number;
|
|
47
|
+
}): Promise<string | undefined>;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* End session, on this computer. When a person ends an agent's session in
|
|
3
|
+
* Passport, the server refuses everything that reaches it under that session.
|
|
4
|
+
* The guard decides most commands here, so before a command it would let run
|
|
5
|
+
* on its own (an allow, or nothing to decide; an ask is a person's call) it
|
|
6
|
+
* asks Passport whether the session was ended (`GET /api/my/cli/session`),
|
|
7
|
+
* and refuses with the server's own line when it was.
|
|
8
|
+
*
|
|
9
|
+
* It costs little: a session checked in the last 15 seconds isn't asked
|
|
10
|
+
* again, a session known to be ended is never asked again, and the question
|
|
11
|
+
* never waits more than 300 ms. On a timeout or any error the command
|
|
12
|
+
* proceeds as it would have: the guard is a guardrail, not a security
|
|
13
|
+
* boundary. A refusal from the decide fast path teaches it too.
|
|
14
|
+
*
|
|
15
|
+
* State lives in owner-only files named by a digest of the agent and session
|
|
16
|
+
* id, holding only when it was checked and, once ended, the server's line.
|
|
17
|
+
* Bounded in count and age. Never throws.
|
|
18
|
+
*/
|
|
19
|
+
import { createHash } from "node:crypto";
|
|
20
|
+
import { lstatSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync } from "node:fs";
|
|
21
|
+
import { basename, join } from "node:path";
|
|
22
|
+
import { readBoundedJson } from "./boundedResponse.js";
|
|
23
|
+
import { credentialDir, enforceOwner, ensurePrivateDirectory, writePrivateAtomic } from "./credentialStorage.js";
|
|
24
|
+
export const CLI_SESSION_PATH = "/api/my/cli/session";
|
|
25
|
+
/** A session checked this recently isn't asked about again. */
|
|
26
|
+
export const SESSION_FRESH_MS = 15_000;
|
|
27
|
+
/** The longest a command the guard would let run waits on the question. */
|
|
28
|
+
export const SESSION_CHECK_MS = 300;
|
|
29
|
+
const MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
30
|
+
const MAX_FILES = 500;
|
|
31
|
+
const MAX_RESPONSE_BYTES = 4 * 1024;
|
|
32
|
+
const DIRECTORY = "hook-sessions";
|
|
33
|
+
const SESSION = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
34
|
+
const RUNTIMES = new Set(["claude-code", "codex", "copilot", "cursor", "other"]);
|
|
35
|
+
/** The server's line, exactly: "Session ended by Maya at 14:05 UTC. Start a new session to continue." */
|
|
36
|
+
export const ENDED_LINE = /^Session ended by [^\n]{1,80} at \d\d:\d\d UTC\. Start a new session to continue\.$/;
|
|
37
|
+
function directory() {
|
|
38
|
+
return join(credentialDir(), DIRECTORY);
|
|
39
|
+
}
|
|
40
|
+
function usable(session) {
|
|
41
|
+
return Boolean(session?.sessionId && SESSION.test(session.sessionId) && RUNTIMES.has(session.runtime));
|
|
42
|
+
}
|
|
43
|
+
function statePath(runtime, sessionId) {
|
|
44
|
+
const key = createHash("sha256").update(`${runtime}\u0000${sessionId}`).digest("hex").slice(0, 40);
|
|
45
|
+
return join(directory(), `${key}.json`);
|
|
46
|
+
}
|
|
47
|
+
function readState(runtime, sessionId) {
|
|
48
|
+
try {
|
|
49
|
+
const path = statePath(runtime, sessionId);
|
|
50
|
+
if (statSync(path).size > MAX_RESPONSE_BYTES)
|
|
51
|
+
return null;
|
|
52
|
+
const raw = JSON.parse(readFileSync(path, "utf8"));
|
|
53
|
+
if (typeof raw.checkedAt !== "number")
|
|
54
|
+
return null;
|
|
55
|
+
const ended = typeof raw.ended === "string" && ENDED_LINE.test(raw.ended) ? raw.ended : undefined;
|
|
56
|
+
return { checkedAt: raw.checkedAt, ...(ended ? { ended } : {}) };
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function writeState(runtime, sessionId, state, now) {
|
|
63
|
+
try {
|
|
64
|
+
ensurePrivateDirectory(true);
|
|
65
|
+
try {
|
|
66
|
+
mkdirSync(directory(), { mode: 0o700 });
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error.code !== "EEXIST")
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
const stat = lstatSync(directory());
|
|
73
|
+
if (stat.isSymbolicLink() || !stat.isDirectory())
|
|
74
|
+
return;
|
|
75
|
+
enforceOwner(stat, "hook sessions directory");
|
|
76
|
+
const path = statePath(runtime, sessionId);
|
|
77
|
+
let names = readdirSync(directory()).filter((name) => /^[0-9a-f]{40}\.json$/.test(name));
|
|
78
|
+
if (names.length >= MAX_FILES && !names.includes(basename(path))) {
|
|
79
|
+
for (const name of names) {
|
|
80
|
+
const file = join(directory(), name);
|
|
81
|
+
if (now - statSync(file).mtimeMs > MAX_AGE_MS)
|
|
82
|
+
rmSync(file, { force: true });
|
|
83
|
+
}
|
|
84
|
+
names = readdirSync(directory()).filter((name) => /^[0-9a-f]{40}\.json$/.test(name));
|
|
85
|
+
if (names.length >= MAX_FILES)
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
writePrivateAtomic(path, ".hook-session", JSON.stringify(state), "hook session");
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
/* Asked again next time: harmless. */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** A refusal Passport sent for this session (decide): remember it, so the
|
|
95
|
+
* guard refuses the rest here without asking. */
|
|
96
|
+
export function rememberEnded(session, message, now = Date.now()) {
|
|
97
|
+
if (!usable(session) || !message || !ENDED_LINE.test(message))
|
|
98
|
+
return;
|
|
99
|
+
writeState(session.runtime, session.sessionId, { checkedAt: now, ended: message }, now);
|
|
100
|
+
}
|
|
101
|
+
/** Validate what Passport said: the line when the session ended, "" when it
|
|
102
|
+
* runs, null for anything unexpected. */
|
|
103
|
+
export function parseSessionState(raw) {
|
|
104
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
105
|
+
return null;
|
|
106
|
+
const body = raw;
|
|
107
|
+
if (body.ended === false)
|
|
108
|
+
return "";
|
|
109
|
+
return body.ended === true && typeof body.message === "string" && ENDED_LINE.test(body.message) ? body.message : null;
|
|
110
|
+
}
|
|
111
|
+
async function askPassport(openApi, session, signal) {
|
|
112
|
+
try {
|
|
113
|
+
const api = await openApi();
|
|
114
|
+
if (!api || signal.aborted)
|
|
115
|
+
return null;
|
|
116
|
+
const query = new URLSearchParams({ runtime: session.runtime, sessionId: session.sessionId });
|
|
117
|
+
const response = await api(`${CLI_SESSION_PATH}?${query.toString()}`, {
|
|
118
|
+
method: "GET",
|
|
119
|
+
headers: { accept: "application/json", "x-passport-surface": "cli" },
|
|
120
|
+
signal
|
|
121
|
+
});
|
|
122
|
+
if (!response.ok) {
|
|
123
|
+
await response.body?.cancel().catch(() => undefined);
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
return parseSessionState(await readBoundedJson(response, MAX_RESPONSE_BYTES));
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The line to refuse with when a person ended this session, else undefined.
|
|
134
|
+
* Known ended: no network. Checked in the last 15 s: no network. Otherwise one
|
|
135
|
+
* question, bounded by 300 ms (and the hook's own budget); no answer in time
|
|
136
|
+
* means the command proceeds, and the session is asked again after 15 s.
|
|
137
|
+
*/
|
|
138
|
+
export async function endedSessionLine(session, openApi, options) {
|
|
139
|
+
if (!usable(session))
|
|
140
|
+
return undefined;
|
|
141
|
+
const state = readState(session.runtime, session.sessionId);
|
|
142
|
+
if (state?.ended)
|
|
143
|
+
return state.ended;
|
|
144
|
+
const now = options.now();
|
|
145
|
+
if (state && now - state.checkedAt >= 0 && now - state.checkedAt < SESSION_FRESH_MS)
|
|
146
|
+
return undefined;
|
|
147
|
+
// A timer of our own (AbortSignal.timeout's doesn't keep the process up),
|
|
148
|
+
// and openApi may refresh a sign-in: the bound covers it too.
|
|
149
|
+
const bound = new AbortController();
|
|
150
|
+
const timer = setTimeout(() => bound.abort(), options.timeoutMs ?? SESSION_CHECK_MS);
|
|
151
|
+
const signal = AbortSignal.any([options.budget, bound.signal]);
|
|
152
|
+
const answer = await Promise.race([
|
|
153
|
+
askPassport(openApi, session, signal),
|
|
154
|
+
new Promise((resolve) => {
|
|
155
|
+
if (signal.aborted)
|
|
156
|
+
resolve(null);
|
|
157
|
+
signal.addEventListener("abort", () => resolve(null), { once: true });
|
|
158
|
+
})
|
|
159
|
+
]).finally(() => clearTimeout(timer));
|
|
160
|
+
writeState(session.runtime, session.sessionId, { checkedAt: options.now(), ...(answer ? { ended: answer } : {}) }, options.now());
|
|
161
|
+
return answer || undefined;
|
|
162
|
+
}
|
package/dist/pluginPin.d.ts
CHANGED
package/dist/pluginPin.js
CHANGED
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.28.0";
|
package/dist/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Keep in lockstep with bridge/package.json.
|
|
2
|
-
export const VERSION = "0.
|
|
2
|
+
export const VERSION = "0.28.0";
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "passport-bridge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.28.0",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "passport-bridge",
|
|
9
|
-
"version": "0.
|
|
9
|
+
"version": "0.28.0",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@modelcontextprotocol/sdk": "1.32.1"
|