@mnemom/mnemom 0.14.6 → 0.15.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.
@@ -0,0 +1,450 @@
1
+ /**
2
+ * OAuth 2.1 client for the Mnemom Authorization Server.
3
+ *
4
+ * `mnemom login` authenticates against the Mnemom OAuth AS (the same AS that
5
+ * fronts the MCP control plane) instead of doing a raw Supabase session login.
6
+ * This yields SCOPED access tokens (`mcp:read` / `mcp:write`) — not the full
7
+ * Supabase god-token — so a leaked CLI credential can only do what the CLI was
8
+ * granted, and grants are independently revocable server-side (MNE-805/806).
9
+ *
10
+ * Two interactive flows, both standards-based:
11
+ * - Authorization Code + PKCE with a loopback redirect (the `wrangler login`
12
+ * pattern): open the browser to /authorize, catch the redirect on a
13
+ * localhost listener, exchange code+verifier at /token.
14
+ * - RFC 8628 Device Authorization Grant (`--no-browser` / headless): POST
15
+ * /device_authorization, print the user_code + verification_uri, poll
16
+ * /token honoring `authorization_pending` and `slow_down`.
17
+ *
18
+ * Everything is driven off RFC 8414 discovery
19
+ * (/.well-known/oauth-authorization-server) — endpoint paths are NOT hardcoded.
20
+ * The public client_id is obtained via RFC 7591 Dynamic Client Registration and
21
+ * cached, so there is no client secret to embed (the AS advertises
22
+ * token_endpoint_auth_method "none" — public clients only).
23
+ */
24
+ import * as http from "node:http";
25
+ import * as crypto from "node:crypto";
26
+ import { execFile } from "node:child_process";
27
+ import { getApiUrl } from "./config.js";
28
+ // The scopes the CLI requests. The AS currently advertises mcp:read + mcp:write
29
+ // (scopes_supported in discovery); we request both so a single login covers the
30
+ // full command surface. If discovery ever narrows what's available we intersect
31
+ // against scopes_supported before asking, so we never request an unknown scope.
32
+ const REQUESTED_SCOPES = ["mcp:read", "mcp:write"];
33
+ const CLIENT_NAME = "Mnemom CLI";
34
+ const DEVICE_CODE_GRANT = "urn:ietf:params:oauth:grant-type:device_code";
35
+ // Loopback login waits at most this long for the browser round-trip before
36
+ // giving up and freeing the port.
37
+ const LOOPBACK_TIMEOUT_MS = 5 * 60 * 1000;
38
+ let cachedMetadata = null;
39
+ /**
40
+ * Fetch (and process-cache) the AS metadata document. We resolve it relative to
41
+ * the active API base URL so staging/local point at their own AS.
42
+ */
43
+ export async function discover() {
44
+ if (cachedMetadata)
45
+ return cachedMetadata;
46
+ const url = `${getApiUrl()}/.well-known/oauth-authorization-server`;
47
+ const res = await fetch(url, { headers: { Accept: "application/json" } });
48
+ if (!res.ok) {
49
+ throw new Error(`Could not load OAuth metadata from ${url} (HTTP ${res.status}). ` + `Is the API reachable?`);
50
+ }
51
+ const meta = (await res.json());
52
+ if (!meta.authorization_endpoint || !meta.token_endpoint) {
53
+ throw new Error("OAuth metadata is missing authorization_endpoint/token_endpoint.");
54
+ }
55
+ cachedMetadata = meta;
56
+ return meta;
57
+ }
58
+ /** Reset the process-level discovery cache (used by tests). */
59
+ export function _resetDiscoveryCache() {
60
+ cachedMetadata = null;
61
+ }
62
+ /** Intersect our requested scopes with what the AS advertises. */
63
+ function negotiateScopes(meta) {
64
+ const supported = meta.scopes_supported;
65
+ const scopes = supported && supported.length > 0
66
+ ? REQUESTED_SCOPES.filter((s) => supported.includes(s))
67
+ : REQUESTED_SCOPES;
68
+ // If nothing intersects (misconfigured AS), fall back to asking for what the
69
+ // AS says it supports rather than sending an empty scope.
70
+ return (scopes.length > 0 ? scopes : (supported ?? REQUESTED_SCOPES)).join(" ");
71
+ }
72
+ // ============================================================================
73
+ // Dynamic Client Registration (RFC 7591)
74
+ // ============================================================================
75
+ /**
76
+ * Register a public client for the CLI and return its client_id. The AS issues
77
+ * public clients (token_endpoint_auth_method "none"), so there is no secret to
78
+ * persist. The caller is responsible for caching the returned id.
79
+ */
80
+ export async function registerClient(redirectUris) {
81
+ const meta = await discover();
82
+ if (!meta.registration_endpoint) {
83
+ throw new Error("This Authorization Server does not support dynamic client registration; " +
84
+ "no public CLI client_id is available.");
85
+ }
86
+ const res = await fetch(meta.registration_endpoint, {
87
+ method: "POST",
88
+ headers: { "Content-Type": "application/json", Accept: "application/json" },
89
+ body: JSON.stringify({
90
+ client_name: CLIENT_NAME,
91
+ redirect_uris: redirectUris,
92
+ grant_types: ["authorization_code", "refresh_token", DEVICE_CODE_GRANT],
93
+ response_types: ["code"],
94
+ token_endpoint_auth_method: "none",
95
+ }),
96
+ });
97
+ if (!res.ok) {
98
+ throw new Error(`Client registration failed (HTTP ${res.status}): ${await safeBody(res)}`);
99
+ }
100
+ const data = (await res.json());
101
+ if (!data.client_id) {
102
+ throw new Error("Client registration response did not include a client_id.");
103
+ }
104
+ return data.client_id;
105
+ }
106
+ /**
107
+ * Generate a PKCE verifier/challenge pair. The verifier is a high-entropy
108
+ * URL-safe string (RFC 7636 §4.1: 43–128 chars from the unreserved set); the
109
+ * challenge is BASE64URL(SHA256(verifier)) for the S256 method (the only method
110
+ * the AS advertises, and the only one allowed under OAuth 2.1).
111
+ */
112
+ export function generatePkce() {
113
+ const verifier = base64url(crypto.randomBytes(32));
114
+ const challenge = base64url(crypto.createHash("sha256").update(verifier).digest());
115
+ return { verifier, challenge, method: "S256" };
116
+ }
117
+ function base64url(buf) {
118
+ return buf.toString("base64url");
119
+ }
120
+ function tokensFromResponse(data) {
121
+ const now = Math.floor(Date.now() / 1000);
122
+ const expiresIn = typeof data.expires_in === "number" ? data.expires_in : 3600;
123
+ return {
124
+ accessToken: data.access_token,
125
+ tokenType: data.token_type ?? "Bearer",
126
+ refreshToken: data.refresh_token,
127
+ scope: data.scope,
128
+ expiresAt: now + expiresIn,
129
+ };
130
+ }
131
+ /**
132
+ * Run the interactive authorization-code + PKCE login. Returns the issued
133
+ * scoped tokens plus the client_id that was used (so the caller can persist it
134
+ * for refresh). `openUrl` is injectable so tests can drive the flow without
135
+ * spawning a real browser.
136
+ */
137
+ export async function loginWithLoopback(openUrl = openBrowser) {
138
+ const meta = await discover();
139
+ const pkce = generatePkce();
140
+ const state = crypto.randomBytes(16).toString("hex");
141
+ const { port, codePromise, close } = await startLoopbackServer(state);
142
+ const redirectUri = `http://127.0.0.1:${port}/callback`;
143
+ // Register a client bound to the exact loopback redirect URI. OAuth 2.1
144
+ // requires exact redirect_uri matching, and the loopback port is ephemeral,
145
+ // so we register per-login (cheap, public client, no secret).
146
+ const clientId = await registerClient([redirectUri]);
147
+ const authUrl = new URL(meta.authorization_endpoint);
148
+ authUrl.searchParams.set("response_type", "code");
149
+ authUrl.searchParams.set("client_id", clientId);
150
+ authUrl.searchParams.set("redirect_uri", redirectUri);
151
+ authUrl.searchParams.set("scope", negotiateScopes(meta));
152
+ authUrl.searchParams.set("state", state);
153
+ authUrl.searchParams.set("code_challenge", pkce.challenge);
154
+ authUrl.searchParams.set("code_challenge_method", pkce.method);
155
+ console.log("Opening browser to authenticate...");
156
+ console.log(`If the browser doesn't open, visit:\n ${authUrl.toString()}\n`);
157
+ openUrl(authUrl.toString());
158
+ console.log("Waiting for authentication...");
159
+ try {
160
+ const code = await codePromise;
161
+ const tokens = await exchangeCode(meta, clientId, code, pkce.verifier, redirectUri);
162
+ return { tokens, clientId };
163
+ }
164
+ finally {
165
+ close();
166
+ }
167
+ }
168
+ /**
169
+ * Start a loopback HTTP listener that captures the OAuth redirect
170
+ * (GET /callback?code=...&state=...). Resolves with the authorization code once
171
+ * a redirect with a matching state arrives; rejects on error/mismatch/timeout.
172
+ */
173
+ function startLoopbackServer(expectedState) {
174
+ let resolveCode;
175
+ let rejectCode;
176
+ const codePromise = new Promise((resolve, reject) => {
177
+ resolveCode = resolve;
178
+ rejectCode = reject;
179
+ });
180
+ const server = http.createServer((req, res) => {
181
+ const reqUrl = new URL(req.url ?? "/", "http://127.0.0.1");
182
+ if (req.method !== "GET" || reqUrl.pathname !== "/callback") {
183
+ res.writeHead(404, { "Content-Type": "text/plain" });
184
+ res.end("Not found");
185
+ return;
186
+ }
187
+ const error = reqUrl.searchParams.get("error");
188
+ const code = reqUrl.searchParams.get("code");
189
+ const state = reqUrl.searchParams.get("state");
190
+ if (error) {
191
+ const desc = reqUrl.searchParams.get("error_description") ?? error;
192
+ respondHtml(res, 400, "Authentication failed", desc);
193
+ rejectCode(new Error(`Authorization denied: ${desc}`));
194
+ return;
195
+ }
196
+ // Constant-time state comparison to avoid leaking timing on the CSRF token.
197
+ if (!state || !timingSafeEqual(state, expectedState)) {
198
+ respondHtml(res, 403, "Authentication failed", "State mismatch — possible CSRF.");
199
+ rejectCode(new Error("State mismatch — possible CSRF attack"));
200
+ return;
201
+ }
202
+ if (!code) {
203
+ respondHtml(res, 400, "Authentication failed", "No authorization code in callback.");
204
+ rejectCode(new Error("No authorization code in callback"));
205
+ return;
206
+ }
207
+ respondHtml(res, 200, "Authenticated!", "You can close this tab and return to the terminal.");
208
+ resolveCode(code);
209
+ });
210
+ return new Promise((resolve) => {
211
+ server.listen(0, "127.0.0.1", () => {
212
+ const port = server.address().port;
213
+ const timeout = setTimeout(() => {
214
+ rejectCode(new Error("Login timed out. Please try again."));
215
+ server.close();
216
+ }, LOOPBACK_TIMEOUT_MS);
217
+ resolve({
218
+ port,
219
+ codePromise,
220
+ close: () => {
221
+ clearTimeout(timeout);
222
+ server.close();
223
+ },
224
+ });
225
+ });
226
+ });
227
+ }
228
+ async function exchangeCode(meta, clientId, code, verifier, redirectUri) {
229
+ const res = await fetch(meta.token_endpoint, {
230
+ method: "POST",
231
+ headers: {
232
+ "Content-Type": "application/x-www-form-urlencoded",
233
+ Accept: "application/json",
234
+ },
235
+ body: new URLSearchParams({
236
+ grant_type: "authorization_code",
237
+ code,
238
+ redirect_uri: redirectUri,
239
+ client_id: clientId,
240
+ code_verifier: verifier,
241
+ }),
242
+ });
243
+ if (!res.ok) {
244
+ throw new Error(`Token exchange failed: ${await oauthError(res)}`);
245
+ }
246
+ return tokensFromResponse((await res.json()));
247
+ }
248
+ /**
249
+ * Run the RFC 8628 device authorization grant. Registers a client (the device
250
+ * flow needs no redirect, but the AS's DCR validation requires a valid
251
+ * redirect_uri — HTTPS or a loopback — so we register a loopback placeholder it
252
+ * will never redirect to), requests a device code, prints the user_code +
253
+ * verification_uri for the user to approve in any browser, then polls the token
254
+ * endpoint until approval — honoring `authorization_pending` and `slow_down`
255
+ * per the spec.
256
+ *
257
+ * `display` and `sleep` are injectable so tests can drive the poll loop
258
+ * deterministically without real timers or stdout.
259
+ */
260
+ export async function loginWithDevice(opts) {
261
+ const display = opts?.display ?? ((line) => console.log(line));
262
+ const sleep = opts?.sleep ?? defaultSleep;
263
+ const meta = await discover();
264
+ if (!meta.device_authorization_endpoint) {
265
+ throw new Error("This Authorization Server does not support the device flow.");
266
+ }
267
+ // Device flow has no redirect, but the AS's DCR validation rejects the OAuth
268
+ // OOB sentinel ("redirect_uri must be HTTPS or a loopback for native
269
+ // clients"). Register a loopback placeholder — it satisfies validation and is
270
+ // never used, since the device grant never redirects.
271
+ const clientId = await registerClient(["http://127.0.0.1/callback"]);
272
+ const authzRes = await fetch(meta.device_authorization_endpoint, {
273
+ method: "POST",
274
+ headers: {
275
+ "Content-Type": "application/x-www-form-urlencoded",
276
+ Accept: "application/json",
277
+ },
278
+ body: new URLSearchParams({ client_id: clientId, scope: negotiateScopes(meta) }),
279
+ });
280
+ if (!authzRes.ok) {
281
+ throw new Error(`Device authorization failed: ${await oauthError(authzRes)}`);
282
+ }
283
+ const authz = (await authzRes.json());
284
+ display("");
285
+ display("To authenticate, visit:");
286
+ display(` ${authz.verification_uri}`);
287
+ display("");
288
+ display(`And enter the code: ${authz.user_code}`);
289
+ if (authz.verification_uri_complete) {
290
+ display("");
291
+ display(`Or open this URL directly:`);
292
+ display(` ${authz.verification_uri_complete}`);
293
+ }
294
+ display("");
295
+ display("Waiting for authorization...");
296
+ const tokens = await pollDeviceToken(meta, clientId, authz, sleep);
297
+ return { tokens, clientId };
298
+ }
299
+ async function pollDeviceToken(meta, clientId, authz, sleep) {
300
+ // RFC 8628 §3.5: default interval is 5s if the server omits it; on slow_down
301
+ // we increase the interval by 5s and keep that as the new minimum.
302
+ let intervalMs = (authz.interval ?? 5) * 1000;
303
+ const deadline = Date.now() + authz.expires_in * 1000;
304
+ for (;;) {
305
+ if (Date.now() >= deadline) {
306
+ throw new Error("Device authorization expired before approval. Please try again.");
307
+ }
308
+ await sleep(intervalMs);
309
+ const res = await fetch(meta.token_endpoint, {
310
+ method: "POST",
311
+ headers: {
312
+ "Content-Type": "application/x-www-form-urlencoded",
313
+ Accept: "application/json",
314
+ },
315
+ body: new URLSearchParams({
316
+ grant_type: DEVICE_CODE_GRANT,
317
+ device_code: authz.device_code,
318
+ client_id: clientId,
319
+ }),
320
+ });
321
+ if (res.ok) {
322
+ return tokensFromResponse((await res.json()));
323
+ }
324
+ const body = (await res.json().catch(() => ({})));
325
+ switch (body.error) {
326
+ case "authorization_pending":
327
+ continue; // keep polling at the current interval
328
+ case "slow_down":
329
+ intervalMs += 5000; // RFC 8628 §3.5
330
+ continue;
331
+ case "expired_token":
332
+ throw new Error("Device authorization expired before approval. Please try again.");
333
+ case "access_denied":
334
+ throw new Error("Authorization was denied.");
335
+ default:
336
+ throw new Error(`Device authorization failed: ${body.error ?? `HTTP ${res.status}`}` +
337
+ (body.error_description ? ` — ${body.error_description}` : ""));
338
+ }
339
+ }
340
+ }
341
+ // ============================================================================
342
+ // Refresh (RFC 6749 §6)
343
+ // ============================================================================
344
+ /**
345
+ * Exchange a refresh token for a fresh access token. Returns null if refresh is
346
+ * not possible (no refresh token, or the AS rejects it — e.g. revoked/expired),
347
+ * so callers can fall back to prompting for re-login rather than crashing.
348
+ */
349
+ export async function refreshTokens(refreshToken, clientId) {
350
+ if (!refreshToken || !clientId)
351
+ return null;
352
+ try {
353
+ const meta = await discover();
354
+ const res = await fetch(meta.token_endpoint, {
355
+ method: "POST",
356
+ headers: {
357
+ "Content-Type": "application/x-www-form-urlencoded",
358
+ Accept: "application/json",
359
+ },
360
+ body: new URLSearchParams({
361
+ grant_type: "refresh_token",
362
+ refresh_token: refreshToken,
363
+ client_id: clientId,
364
+ }),
365
+ });
366
+ if (!res.ok)
367
+ return null;
368
+ const data = (await res.json());
369
+ const tokens = tokensFromResponse(data);
370
+ // Per RFC 6749 §6, a refresh response MAY omit a new refresh token, in which
371
+ // case the old one remains valid — preserve it so the next refresh works.
372
+ if (!tokens.refreshToken)
373
+ tokens.refreshToken = refreshToken;
374
+ return tokens;
375
+ }
376
+ catch {
377
+ return null;
378
+ }
379
+ }
380
+ // ============================================================================
381
+ // Helpers
382
+ // ============================================================================
383
+ /**
384
+ * Open `url` in the user's default browser WITHOUT a shell. The URL is always
385
+ * passed as a separate argv element (never concatenated into a command string),
386
+ * so shell metacharacters — `$(...)`, backticks, `\` — in the URL can never be
387
+ * interpreted as a command. Fire-and-forget: any failure (no opener installed,
388
+ * headless host) is swallowed because callers already print a manual-URL
389
+ * fallback, so a missing browser must not crash login.
390
+ */
391
+ export function openBrowser(url) {
392
+ // On Windows, `start` is a cmd builtin and its FIRST quoted arg is the window
393
+ // title; the empty "" makes the URL the target rather than the title.
394
+ const [command, args] = process.platform === "darwin"
395
+ ? ["open", [url]]
396
+ : process.platform === "win32"
397
+ ? ["cmd", ["/c", "start", "", url]]
398
+ : ["xdg-open", [url]];
399
+ try {
400
+ const child = execFile(command, args, () => {
401
+ /* swallow opener errors — the manual URL fallback is already printed */
402
+ });
403
+ // Don't keep the event loop alive on the opener; login flow waits on the
404
+ // loopback/device promise, not on the browser process.
405
+ child.unref?.();
406
+ }
407
+ catch {
408
+ /* execFile threw synchronously (e.g. command not found) — ignore */
409
+ }
410
+ }
411
+ function defaultSleep(ms) {
412
+ return new Promise((resolve) => setTimeout(resolve, ms));
413
+ }
414
+ /** Constant-time string compare that tolerates length differences. */
415
+ function timingSafeEqual(a, b) {
416
+ const ab = Buffer.from(a);
417
+ const bb = Buffer.from(b);
418
+ if (ab.length !== bb.length)
419
+ return false;
420
+ return crypto.timingSafeEqual(ab, bb);
421
+ }
422
+ /** Escape a string for safe interpolation into HTML text content. */
423
+ function escapeHtml(s) {
424
+ return s
425
+ .replace(/&/g, "&")
426
+ .replace(/</g, "&lt;")
427
+ .replace(/>/g, "&gt;")
428
+ .replace(/"/g, "&quot;")
429
+ .replace(/'/g, "&#39;");
430
+ }
431
+ function respondHtml(res, status, title, body) {
432
+ res.writeHead(status, { "Content-Type": "text/html; charset=utf-8" });
433
+ // title/body are escaped: on the error path `body` carries the OAuth
434
+ // error_description straight from the callback query string (attacker-
435
+ // controllable on the loopback URL), so interpolating it raw is reflected XSS
436
+ // (CodeQL js/reflected-xss). Escaping closes it; the page is plain text anyway.
437
+ res.end(`<html><body style="font-family:system-ui;text-align:center;padding:60px">` +
438
+ `<h2>${escapeHtml(title)}</h2><p>${escapeHtml(body)}</p></body></html>`);
439
+ }
440
+ /** Format a standard OAuth error response ({error, error_description}). */
441
+ async function oauthError(res) {
442
+ const body = (await res.json().catch(() => ({})));
443
+ if (body.error) {
444
+ return body.error + (body.error_description ? ` — ${body.error_description}` : "");
445
+ }
446
+ return `HTTP ${res.status}`;
447
+ }
448
+ async function safeBody(res) {
449
+ return (await res.text().catch(() => "")) || `HTTP ${res.status}`;
450
+ }
@@ -0,0 +1,181 @@
1
+ /**
2
+ * `mnemom try-me <token>` — the deterministic Dojo-onboarding skill-runner
3
+ * (MNE-934, epic MNE-931).
4
+ *
5
+ * On a fresh machine with no MCP connector configured, the Dojo `/try-me`
6
+ * onboarding works badly because every agent improvises the manifest's flow
7
+ * differently. This module is the read-only client half of the fix: it resolves
8
+ * the shipped v5.0 briefing manifest and exposes the typed pieces + IO helpers
9
+ * the `try-me` command executes deterministically (resolve → birth → claim →
10
+ * alignment → protection → hand off).
11
+ *
12
+ * It is strictly ADDITIVE and consumes Alex's shipped endpoints READ-ONLY:
13
+ * - GET /v1/dojo/try-me/resolve?token=… (zero-auth; the token IS the credential)
14
+ * - POST <gateway.endpoint>/v1/messages (birth — the agent's first model call)
15
+ * - GET /v1/agents/:id (public claim poll)
16
+ * The card-write PUTs reuse the canonical client (lib/api.ts). NOTHING in the
17
+ * dojo backend or the website is touched.
18
+ *
19
+ * The manifest contract mirrored here is mnemom-api `src/dojo/try-me.ts`
20
+ * (MANIFEST_VERSION "5.0", MNE-879 B1). We type only the fields the runner
21
+ * consumes and tolerate everything else (`[key: string]: unknown`) so a manifest
22
+ * bump that adds fields never breaks the CLI.
23
+ */
24
+ /** A canonical card object (alignment/protection) — opaque to the runner; passed through verbatim. */
25
+ export type CanonicalCard = Record<string, unknown>;
26
+ /** The MODEL credential block — births the agent + carries gateway calls (BYOK). */
27
+ export interface ManifestGateway {
28
+ /** Provider gateway route, e.g. https://gateway.mnemom.ai/anthropic */
29
+ endpoint: string;
30
+ provider: string;
31
+ /** Concrete model id to send at birth (bare aliases 404 upstream). */
32
+ model: string;
33
+ /** Provider-native header the credential rides on (x-api-key / Authorization / x-goog-api-key). */
34
+ key_header: string;
35
+ /** Per-session birth token (mnbt_…) — token mode (preferred). */
36
+ birth_token?: string;
37
+ /** Legacy shared provider key — present only when birth_token is absent. */
38
+ provider_key?: string;
39
+ /** Header carrying the agent's birth NAME. */
40
+ agent_header: string;
41
+ [key: string]: unknown;
42
+ }
43
+ /** Human-handoff surfaces (claim/grant/dojo URLs + the name question). */
44
+ export interface ManifestHandoff {
45
+ name_question: string;
46
+ name_options: string[];
47
+ signup_url: string;
48
+ /** /claim template — {agent_id} + {hash_proof}, with &dojo_config (also binds server-side). */
49
+ claim_url_template: string;
50
+ /** First-party one-time protection-grant page — {agent_id} substituted. */
51
+ grant_url_template: string;
52
+ /** Recipe describing what hash_proof is (token mode: the raw mnbt_; legacy: SHA256). */
53
+ claim_hash_recipe: string;
54
+ dojo_url: string;
55
+ [key: string]: unknown;
56
+ }
57
+ /** The GOOD cards the agent declares. */
58
+ export interface ManifestDeclare {
59
+ alignment_card: CanonicalCard;
60
+ protection_card: CanonicalCard;
61
+ protection_mode: "nudge" | string;
62
+ [key: string]: unknown;
63
+ }
64
+ export interface BriefingStep {
65
+ n: number;
66
+ title: string;
67
+ body: string;
68
+ human_handoff?: boolean;
69
+ [key: string]: unknown;
70
+ }
71
+ export interface BriefingManifest {
72
+ directive?: string;
73
+ version: string;
74
+ token: string;
75
+ reassurance?: string;
76
+ mission?: {
77
+ scenario_id?: string;
78
+ name?: string;
79
+ role_system_prompt?: string;
80
+ failure_conditions?: Record<string, unknown>;
81
+ [key: string]: unknown;
82
+ };
83
+ model?: {
84
+ provider: string;
85
+ name: string;
86
+ };
87
+ gateway: ManifestGateway;
88
+ declare: ManifestDeclare;
89
+ bind?: {
90
+ endpoint?: string;
91
+ config_id?: string;
92
+ [key: string]: unknown;
93
+ };
94
+ handoff: ManifestHandoff;
95
+ steps?: BriefingStep[];
96
+ state_machine?: {
97
+ initial?: string;
98
+ states?: Array<Record<string, unknown>>;
99
+ };
100
+ instructions_markdown?: string;
101
+ [key: string]: unknown;
102
+ }
103
+ /** True iff `token` is shaped like a try-me token (tryme_… / a bare slug). */
104
+ export declare function looksLikeTryMeToken(token: string): boolean;
105
+ /**
106
+ * Validate a URL the manifest handed us before we fetch/open it. The manifest is
107
+ * server-built so its URLs are trusted, but we guard the protocol so a malformed
108
+ * manifest can never coerce a non-http(s) open (e.g. file://) on the host.
109
+ */
110
+ export declare function assertHttpUrl(url: string, what: string): string;
111
+ /**
112
+ * Resolve a try-me token to its briefing manifest. Zero-auth — the token is the
113
+ * credential. Idempotent within the server's reuse window (a re-resolve replays
114
+ * the SAME manifest + birth credential), so a retry is safe. Maps the documented
115
+ * status codes to actionable messages:
116
+ * 400 token missing/invalid · 404 never minted · 410 used/expired ·
117
+ * 503 demo not configured yet (retryable) · 5xx server error.
118
+ */
119
+ export declare function resolveBriefing(token: string, opts?: {
120
+ apiBase?: string;
121
+ }): Promise<BriefingManifest>;
122
+ /** Is this a token-mode manifest (per-session mnbt_ birth token) vs legacy provider key? */
123
+ export declare function isTokenMode(manifest: BriefingManifest): boolean;
124
+ /**
125
+ * The single credential the agent presents on `gateway.key_header` to be born:
126
+ * the per-session birth token (token mode) or the shared provider key (legacy).
127
+ */
128
+ export declare function birthCredential(manifest: BriefingManifest): string;
129
+ /**
130
+ * The claim `hash_proof`. In token mode it IS the birth token, presented RAW (the
131
+ * server double-hashes); we cannot derive the legacy SHA256("<key>|<name>") proof
132
+ * here because the runner never holds the provider key in token mode. For legacy
133
+ * manifests the caller must supply the provider key + name separately.
134
+ */
135
+ export declare function claimProof(manifest: BriefingManifest): string;
136
+ /** Build the canonical /claim URL: substitute agent_id + hash_proof; leave dojo_config as given. */
137
+ export declare function buildClaimUrl(manifest: BriefingManifest, agentId: string, proof: string): string;
138
+ /** Build the first-party protection-grant URL: substitute agent_id. */
139
+ export declare function buildGrantUrl(manifest: BriefingManifest, agentId: string): string;
140
+ /**
141
+ * The Dojo deep link with the agent pre-selected, so the human only has to press
142
+ * Begin Sim: `<dojo_url>?agent=<agent_id>` (mirrors the manifest's step 6).
143
+ */
144
+ export declare function buildDojoDeepLink(manifest: BriefingManifest, agentId: string): string;
145
+ /** The provider-native birth path under the gateway endpoint, per provider. */
146
+ export declare function birthPath(provider: string): string;
147
+ export interface BirthResult {
148
+ /** The minted agent id (mnm-…) captured from the x-mnemom-agent response header. */
149
+ agentId: string;
150
+ /** The gateway HTTP status (200 on a successful birth). */
151
+ status: number;
152
+ }
153
+ /**
154
+ * Be born: make the first model call through the Mnemom gateway with the birth
155
+ * credential, the human-chosen name, and the CONCRETE model id, then capture the
156
+ * minted `agent_id` from the `x-mnemom-agent` RESPONSE header.
157
+ *
158
+ * Only Anthropic births are supported here (the Dojo runs Haiku); other
159
+ * providers throw a clear error rather than send a malformed body. `fetchImpl`
160
+ * is injectable for tests.
161
+ */
162
+ export declare function birthAgent(manifest: BriefingManifest, name: string, opts?: {
163
+ fetchImpl?: typeof fetch;
164
+ }): Promise<BirthResult>;
165
+ /** The slice of the public agent projection the claim poll reads. */
166
+ export interface PublicAgentStatus {
167
+ id?: string;
168
+ name?: string | null;
169
+ claimed?: boolean;
170
+ [key: string]: unknown;
171
+ }
172
+ /**
173
+ * Read the PUBLIC (no-auth) agent projection used to detect the human's claim.
174
+ * Never throws — a transient 404/5xx (the agent not yet queryable, or a blip)
175
+ * resolves to null so the caller's poll loop simply keeps waiting.
176
+ */
177
+ export declare function fetchAgentClaimed(apiBase: string, agentId: string, opts?: {
178
+ fetchImpl?: typeof fetch;
179
+ }): Promise<PublicAgentStatus | null>;
180
+ /** Small awaitable sleep used by the runner's poll loops. */
181
+ export declare function sleep(ms: number): Promise<void>;