@broberg/lens-daemon 0.1.18 → 0.1.20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@broberg/lens-daemon",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
4
4
  "description": "The Lens module of the cardmem daemon (flows, critic, coverage, capture sessions, the /lens/* routes and the cardmem-lens MCP) as a package. The daemon mounts createLensRouter(deps) + createLensMcpRouter(deps). Bun runtime.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -15,8 +15,8 @@
15
15
  "sharp": "^0.33.5",
16
16
  "turndown": "^7.2.4",
17
17
  "zod": "^3.23.8",
18
- "@broberg/lens-engine": "0.13.1",
19
- "@broberg/lens-svg": "0.1.0"
18
+ "@broberg/lens-svg": "0.1.0",
19
+ "@broberg/lens-engine": "0.13.1"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@happy-dom/global-registrator": "^20.10.6",
package/src/auth/index.ts CHANGED
@@ -196,6 +196,26 @@ async function applyMintEndpoint(
196
196
  cfg: MintEndpointAuth,
197
197
  env: AuthEnv,
198
198
  ): Promise<void> {
199
+ const json = (await fetchMintJson(cfg, env, 'mintEndpoint auth')) as StorageStateShape;
200
+ if (!json.cookies?.length && !json.origins?.length) {
201
+ throw new Error(`mintEndpoint auth: ${cfg.url} returned a storageState with no cookies/origins`);
202
+ }
203
+ await applyStorageStateJson(context, json);
204
+ }
205
+
206
+ /** The narrow mint call shared by auth.mintEndpoint and F009.129's
207
+ * extension.storage_local_mint: resolve the secret (secretPath wins over
208
+ * secretEnv), POST/GET with `Authorization: Bearer`, require 2xx + JSON. Error
209
+ * messages name the url and status only — never the secret, never the body. */
210
+ export interface MintCall {
211
+ url: string;
212
+ method?: 'POST' | 'GET';
213
+ secretEnv?: string;
214
+ secretPath?: string;
215
+ body?: unknown;
216
+ headers?: Record<string, string>;
217
+ }
218
+ export async function fetchMintJson(cfg: MintCall, env: AuthEnv, label: string): Promise<unknown> {
199
219
  // Resolve the narrow mint secret — secretPath (gitignored file, no restart)
200
220
  // wins over secretEnv. The secret is NOT in the manifest/call, only its source.
201
221
  let secret: string | undefined;
@@ -204,18 +224,18 @@ async function applyMintEndpoint(
204
224
  // some other repo's secret, which would then be sent to the caller's url.
205
225
  // Callers resolve against their project root first (resolveAuthPaths).
206
226
  if (!isAbsolute(cfg.secretPath)) {
207
- throw new Error(`mintEndpoint auth: secretPath must be absolute by the time it is read (got "${cfg.secretPath}") — register the project or pass an absolute path`);
227
+ throw new Error(`${label}: secretPath must be absolute by the time it is read (got "${cfg.secretPath}") — register the project or pass an absolute path`);
208
228
  }
209
229
  try {
210
230
  secret = readFileSync(cfg.secretPath, 'utf-8').trim();
211
231
  } catch (err) {
212
- throw new Error(`mintEndpoint auth: could not read secretPath ${cfg.secretPath}: ${err instanceof Error ? err.message : err}`);
232
+ throw new Error(`${label}: could not read secretPath ${cfg.secretPath}: ${err instanceof Error ? err.message : err}`);
213
233
  }
214
234
  } else if (cfg.secretEnv) {
215
235
  secret = env[cfg.secretEnv];
216
- if (!secret) throw new Error(`mintEndpoint auth: env ${cfg.secretEnv} is not set`);
236
+ if (!secret) throw new Error(`${label}: env ${cfg.secretEnv} is not set`);
217
237
  } else {
218
- throw new Error('mintEndpoint auth: set auth.secretPath or auth.secretEnv');
238
+ throw new Error(`${label}: set secretPath or secretEnv`);
219
239
  }
220
240
 
221
241
  // Optional JSON body (POST only) — lets the endpoint mint a session for a
@@ -235,21 +255,16 @@ async function applyMintEndpoint(
235
255
  ...(sendBody ? { body: JSON.stringify(cfg.body) } : {}),
236
256
  });
237
257
  } catch (err) {
238
- throw new Error(`mintEndpoint auth: request to ${cfg.url} failed: ${err instanceof Error ? err.message : err}`);
258
+ throw new Error(`${label}: request to ${cfg.url} failed: ${err instanceof Error ? err.message : err}`);
239
259
  }
240
260
  if (!res.ok) {
241
- throw new Error(`mintEndpoint auth: ${cfg.url} returned ${res.status} ${res.statusText}`);
261
+ throw new Error(`${label}: ${cfg.url} returned ${res.status} ${res.statusText}`);
242
262
  }
243
- let json: StorageStateShape;
244
263
  try {
245
- json = (await res.json()) as StorageStateShape;
264
+ return await res.json();
246
265
  } catch (err) {
247
- throw new Error(`mintEndpoint auth: ${cfg.url} did not return JSON storageState: ${err instanceof Error ? err.message : err}`);
266
+ throw new Error(`${label}: ${cfg.url} did not return JSON: ${err instanceof Error ? err.message : err}`);
248
267
  }
249
- if (!json.cookies?.length && !json.origins?.length) {
250
- throw new Error(`mintEndpoint auth: ${cfg.url} returned a storageState with no cookies/origins`);
251
- }
252
- await applyStorageStateJson(context, json);
253
268
  }
254
269
 
255
270
  async function applyCookieJwt(
package/src/browser.ts CHANGED
@@ -320,23 +320,44 @@ export interface LoadedExtension {
320
320
  * reason this can run on a headless host (and therefore on Cloud Lens) instead
321
321
  * of needing a screen. */
322
322
  const EXTENSION_CHANNEL = 'chromium';
323
+ const EXTENSION_LAUNCH_TIMEOUT_MS = 30_000;
324
+ const EXTENSION_LAUNCH_ATTEMPTS = 2;
323
325
 
324
326
  async function launchWithExtension(
325
327
  opts: WithPageOptions,
326
328
  extPath: string,
327
329
  ): Promise<{ context: BrowserContext; extension: LoadedExtension; userDataDir: string }> {
328
- const userDataDir = await mkdtemp(join(tmpdir(), 'lens-ext-'));
329
- const context = await chromium.launchPersistentContext(userDataDir, {
330
- ...contextOptions(opts),
331
- headless: true,
332
- channel: EXTENSION_CHANNEL,
333
- args: [
334
- `--disable-extensions-except=${extPath}`,
335
- `--load-extension=${extPath}`,
336
- '--disable-dev-shm-usage',
337
- '--no-sandbox',
338
- ],
339
- });
330
+ // Bounded and retried once: Playwright's default launch timeout is 180 s, so a
331
+ // launch that stalls (seen on loaded Linux CI) used to hang the whole run past
332
+ // any test or caller deadline with no word on WHERE. Now it names the stage.
333
+ let userDataDir = '';
334
+ let context: BrowserContext | null = null;
335
+ let lastLaunchError = '';
336
+ for (let attempt = 1; attempt <= EXTENSION_LAUNCH_ATTEMPTS && !context; attempt++) {
337
+ userDataDir = await mkdtemp(join(tmpdir(), 'lens-ext-'));
338
+ try {
339
+ context = await chromium.launchPersistentContext(userDataDir, {
340
+ ...contextOptions(opts),
341
+ headless: true,
342
+ channel: EXTENSION_CHANNEL,
343
+ timeout: EXTENSION_LAUNCH_TIMEOUT_MS,
344
+ args: [
345
+ `--disable-extensions-except=${extPath}`,
346
+ `--load-extension=${extPath}`,
347
+ '--disable-dev-shm-usage',
348
+ '--no-sandbox',
349
+ ],
350
+ });
351
+ } catch (err) {
352
+ lastLaunchError = err instanceof Error ? err.message.split('\n')[0]! : String(err);
353
+ await rm(userDataDir, { recursive: true, force: true }).catch(() => {});
354
+ }
355
+ }
356
+ if (!context) {
357
+ throw new Error(
358
+ `extension browser did not start (${EXTENSION_LAUNCH_ATTEMPTS} attempts × ${EXTENSION_LAUNCH_TIMEOUT_MS} ms): ${lastLaunchError}`,
359
+ );
360
+ }
340
361
  // MV3 registers a service worker; MV2 a background page. Wait for whichever
341
362
  // appears — the id lives in its URL host either way.
342
363
  let worker = context.serviceWorkers()[0];
package/src/flow.ts CHANGED
@@ -25,7 +25,7 @@ import {
25
25
  } from '@broberg/lens-engine';
26
26
  import { withPage, type LensEngine } from './browser.ts';
27
27
  import { deadline, defaultPopupOf, prepareExtension, seedExtensionStorage, SIMULATED_ACCESS_NOTE } from './extension-flow.ts';
28
- import { authPrepare, type AuthConfig, type AuthEnv } from './auth/index.ts';
28
+ import { authPrepare, fetchMintJson, type AuthConfig, type AuthEnv } from './auth/index.ts';
29
29
  import { takeShot, settleForShot } from './capture.ts';
30
30
  import { runCritic, topFindingMessage, type CriticMode, type CriticReport } from './critic.ts';
31
31
  import { waitForBuild, observeBuild } from './build-marker.ts';
@@ -194,6 +194,19 @@ export interface FlowExtension {
194
194
  /** F009.126 — written to the extension's chrome.storage.local before step 0, so a
195
195
  * popup that keeps its login there starts signed in (OAuth cannot run headless). */
196
196
  storage_local?: Record<string, unknown>;
197
+ /** F009.129 — the daemon MINTS the value itself (Bearer secret from secretPath /
198
+ * secretEnv) and writes the JSON answer to chrome.storage.local[key], so a real
199
+ * token never passes through the calling agent. Wins over storage_local on the
200
+ * same key. */
201
+ storage_local_mint?: StorageLocalMint;
202
+ }
203
+ export interface StorageLocalMint {
204
+ key: string;
205
+ url: string;
206
+ method?: 'POST' | 'GET';
207
+ secretPath?: string;
208
+ secretEnv?: string;
209
+ body?: Record<string, unknown>;
197
210
  }
198
211
 
199
212
  /**
@@ -496,8 +509,8 @@ export function parseFlow(input: unknown): Flow {
496
509
  if (f.extension !== undefined) {
497
510
  const e = f.extension as unknown as Record<string, unknown>;
498
511
  const keys = e && typeof e === 'object' && !Array.isArray(e) ? Object.keys(e) : null;
499
- if (!keys || typeof e.path !== 'string' || !e.path || keys.some((k) => k !== 'path' && k !== 'grant_host_access' && k !== 'storage_local')) {
500
- throw new Error('flow.extension must be {path: "<unpacked build dir>", grant_host_access?: boolean, storage_local?: {…}}');
512
+ if (!keys || typeof e.path !== 'string' || !e.path || keys.some((k) => !['path', 'grant_host_access', 'storage_local', 'storage_local_mint'].includes(k))) {
513
+ throw new Error('flow.extension must be {path: "<unpacked build dir>", grant_host_access?: boolean, storage_local?: {…}, storage_local_mint?: {…}}');
501
514
  }
502
515
  if (e.grant_host_access !== undefined && typeof e.grant_host_access !== 'boolean') {
503
516
  throw new Error('flow.extension.grant_host_access must be a boolean');
@@ -506,10 +519,12 @@ export function parseFlow(input: unknown): Flow {
506
519
  if (sl !== undefined && (sl === null || typeof sl !== 'object' || Array.isArray(sl))) {
507
520
  throw new Error('flow.extension.storage_local must be an object of {key: JSON value} — it is passed to chrome.storage.local.set as-is');
508
521
  }
522
+ const mint = e.storage_local_mint === undefined ? undefined : parseStorageLocalMint(e.storage_local_mint);
509
523
  extension = {
510
524
  path: e.path,
511
525
  ...(e.grant_host_access === true ? { grant_host_access: true } : {}),
512
526
  ...(sl !== undefined ? { storage_local: sl as Record<string, unknown> } : {}),
527
+ ...(mint ? { storage_local_mint: mint } : {}),
513
528
  };
514
529
  }
515
530
  validateAndNormalizeSteps(f.steps);
@@ -562,6 +577,30 @@ function redactQuery(u: string): string {
562
577
  return q === -1 ? u : `${u.slice(0, q)}?…`;
563
578
  }
564
579
 
580
+ /** F009.129 — refused when malformed, the same way the rest of flow.extension is. */
581
+ function parseStorageLocalMint(raw: unknown): StorageLocalMint {
582
+ const m = raw as Record<string, unknown>;
583
+ const shape = 'flow.extension.storage_local_mint must be {key, url, method?, secretPath | secretEnv, body?}';
584
+ if (!m || typeof m !== 'object' || Array.isArray(m)) throw new Error(shape);
585
+ const allowed = ['key', 'url', 'method', 'secretPath', 'secretEnv', 'body'];
586
+ const unknownKey = Object.keys(m).find((k) => !allowed.includes(k));
587
+ if (unknownKey) throw new Error(`${shape} — unknown key "${unknownKey}"`);
588
+ if (typeof m.key !== 'string' || !m.key) throw new Error(`${shape} — key is required`);
589
+ if (typeof m.url !== 'string' || !/^https?:\/\//.test(m.url)) throw new Error(`${shape} — url must be http(s)`);
590
+ if (m.method !== undefined && m.method !== 'POST' && m.method !== 'GET') throw new Error(`${shape} — method must be POST or GET`);
591
+ const hasPath = typeof m.secretPath === 'string' && m.secretPath !== '';
592
+ const hasEnv = typeof m.secretEnv === 'string' && m.secretEnv !== '';
593
+ if (hasPath === hasEnv) throw new Error(`${shape} — give exactly one of secretPath or secretEnv`);
594
+ if (m.body !== undefined && (m.body === null || typeof m.body !== 'object' || Array.isArray(m.body))) throw new Error(`${shape} — body must be an object`);
595
+ return {
596
+ key: m.key,
597
+ url: m.url,
598
+ ...(m.method ? { method: m.method as 'POST' | 'GET' } : {}),
599
+ ...(hasPath ? { secretPath: m.secretPath as string } : { secretEnv: m.secretEnv as string }),
600
+ ...(m.body ? { body: m.body as Record<string, unknown> } : {}),
601
+ };
602
+ }
603
+
565
604
  function usesPopup(steps: FlowStep[]): boolean {
566
605
  return steps.some(
567
606
  (st) =>
@@ -3107,7 +3146,12 @@ export async function runFlow(flow: Flow, env: AuthEnv, opts: RunFlowOptions = {
3107
3146
  watchDownloads(context);
3108
3147
  if (ext && flow.extension && prepared) {
3109
3148
  extCtx = { context, id: ext.id, main: page, popup: null };
3110
- if (flow.extension.storage_local) await seedExtensionStorage(context, flow.extension.storage_local);
3149
+ const mint = flow.extension.storage_local_mint;
3150
+ const seed = {
3151
+ ...(flow.extension.storage_local ?? {}),
3152
+ ...(mint ? { [mint.key]: await fetchMintJson(mint, env, 'storage_local_mint') } : {}),
3153
+ };
3154
+ if (Object.keys(seed).length) await seedExtensionStorage(context, seed);
3111
3155
  extensionInfo = {
3112
3156
  id: ext.id,
3113
3157
  path: flow.extension.path,
package/src/mcp.ts CHANGED
@@ -369,6 +369,7 @@ export const TOOLS = [
369
369
  path: { type: 'string' },
370
370
  grant_host_access: { type: 'boolean' },
371
371
  storage_local: { type: 'object', description: 'F009.126 — {key: JSON value} for chrome.storage.local, set before step 0.' },
372
+ storage_local_mint: { type: 'object', description: 'F009.129 — {key, url, method?, secretPath | secretEnv, body?}: the DAEMON calls the mint url (Bearer secret; secretPath relative to local_path, sandboxed) and writes the JSON answer to chrome.storage.local[key] before step 0, so a real token never passes through you. Wins over storage_local on the same key. Value and secret never appear in the report.' },
372
373
  },
373
374
  },
374
375
  steps: {
package/src/service.ts CHANGED
@@ -943,6 +943,15 @@ export async function lensRunFlow(deps: LensDeps, input: RunFlowInput) {
943
943
  const abs = raw.startsWith('/') ? raw : input.local_path ? `${input.local_path.replace(/\/$/, '')}/${raw}` : null;
944
944
  if (!abs) throw new Error('flow.extension.path is relative — pass local_path, or give an absolute path');
945
945
  flow.extension = { ...flow.extension, path: resolveSandboxedPath(abs, deps.sandboxRoots) };
946
+ // F009.129 — the mint secret file follows auth.mintEndpoint's rule: relative to
947
+ // the project, and inside the sandbox, so a flow can never point the daemon at
948
+ // another repo's secret and send it to its own url.
949
+ const mint = flow.extension.storage_local_mint;
950
+ if (mint?.secretPath) {
951
+ const sp = mint.secretPath.startsWith('/') ? mint.secretPath : input.local_path ? `${input.local_path.replace(/\/$/, '')}/${mint.secretPath}` : null;
952
+ if (!sp) throw new Error('flow.extension.storage_local_mint.secretPath is relative — pass local_path, or give an absolute path');
953
+ flow.extension = { ...flow.extension, storage_local_mint: { ...mint, secretPath: resolveSandboxedPath(sp, deps.sandboxRoots) } };
954
+ }
946
955
  }
947
956
  // F082.6 — auth reaches flows: own auth (relative paths resolved) or inherit
948
957
  // the manifest's. Fixes the blocker where authed routes redirected to login.