imprint-mcp 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +64 -2
  2. package/package.json +1 -1
  3. package/prompts/auth-compile-agent.md +167 -22
  4. package/prompts/compile-agent.md +45 -8
  5. package/prompts/component-recovery.md +40 -0
  6. package/prompts/master-teach-decision.md +28 -3
  7. package/prompts/master-teach-focused-planner.md +18 -5
  8. package/src/cli.ts +26 -18
  9. package/src/imprint/agent-tool.ts +15 -0
  10. package/src/imprint/agent.ts +2 -11
  11. package/src/imprint/audit.ts +35 -13
  12. package/src/imprint/auth-browser-discovery.ts +276 -0
  13. package/src/imprint/auth-compile-agent.ts +513 -224
  14. package/src/imprint/auth-compile-tools.ts +213 -25
  15. package/src/imprint/auth-discovery-evidence.ts +81 -0
  16. package/src/imprint/auth-plan.ts +28 -0
  17. package/src/imprint/auth-verifier.ts +234 -20
  18. package/src/imprint/authentication-interaction.ts +137 -0
  19. package/src/imprint/backend-ladder.ts +218 -34
  20. package/src/imprint/build-plan.ts +2 -22
  21. package/src/imprint/cdp-browser-fetch.ts +192 -53
  22. package/src/imprint/chromium.ts +93 -29
  23. package/src/imprint/claude-cli-compile.ts +2 -0
  24. package/src/imprint/codex-cli-compile.ts +99 -46
  25. package/src/imprint/compile-agent-types.ts +9 -5
  26. package/src/imprint/compile-agent.ts +33 -0
  27. package/src/imprint/compile-tool-executor.ts +783 -0
  28. package/src/imprint/compile-tools.ts +42 -9
  29. package/src/imprint/compile-verification.ts +2 -0
  30. package/src/imprint/compile.ts +3 -0
  31. package/src/imprint/compiler-log.ts +16 -0
  32. package/src/imprint/component-recovery.ts +120 -0
  33. package/src/imprint/cookie-jar.ts +117 -7
  34. package/src/imprint/credential-extract.ts +9 -2
  35. package/src/imprint/credential-lock.ts +146 -0
  36. package/src/imprint/credential-observation.ts +261 -0
  37. package/src/imprint/credential-store.ts +247 -44
  38. package/src/imprint/live-component-recovery.ts +73 -0
  39. package/src/imprint/llm.ts +4 -3
  40. package/src/imprint/log.ts +16 -1
  41. package/src/imprint/login.ts +9 -0
  42. package/src/imprint/master-teach-agent-contracts.ts +40 -3
  43. package/src/imprint/master-teach-agents.ts +229 -14
  44. package/src/imprint/master-teach-checks.ts +8 -1
  45. package/src/imprint/master-teach-controller.ts +681 -138
  46. package/src/imprint/master-teach-prompt-projections.ts +2 -0
  47. package/src/imprint/mcp-compile-server.ts +62 -850
  48. package/src/imprint/native-teach-agents.ts +232 -108
  49. package/src/imprint/runtime.ts +357 -36
  50. package/src/imprint/stealth-chromium.ts +67 -7
  51. package/src/imprint/teach-browser-resources.ts +88 -0
  52. package/src/imprint/teach-stage-progress.ts +38 -0
  53. package/src/imprint/terminal-user-prompt.ts +42 -0
  54. package/src/imprint/types.ts +46 -18
  55. package/src/imprint/user-interaction.ts +836 -0
@@ -18,6 +18,7 @@ import { createHash } from 'node:crypto';
18
18
  import { existsSync, readFileSync } from 'node:fs';
19
19
  import { basename, dirname, resolve as pathResolve } from 'node:path';
20
20
  import type { Page } from 'playwright';
21
+ import { AUTH_DISCOVERY_POOL_KEY } from './auth-browser-discovery.ts';
21
22
  import { loadBackendsCacheStatus } from './backend-cache.ts';
22
23
  import {
23
24
  type CdpBrowserFetch,
@@ -34,7 +35,9 @@ import {
34
35
  } from './cdp-jar-cache.ts';
35
36
  import { proxyUrl } from './chromium.ts';
36
37
  import { abortSignalError, abortableDelay, withAbortSignal } from './concurrency.ts';
37
- import { RuntimeCookieJar } from './cookie-jar.ts';
38
+ import { RuntimeCookieJar, cookieScopeKey } from './cookie-jar.ts';
39
+ import { recordBrowserCookieState } from './credential-observation.ts';
40
+ import { compareAndSwapSiteCookies } from './credential-store.ts';
38
41
  import { redactFreeformText } from './freeform-redact.ts';
39
42
  import { createLog } from './log.ts';
40
43
  import { runPlaybook } from './playbook-runner.ts';
@@ -57,6 +60,7 @@ import {
57
60
  createStealthFetch,
58
61
  } from './stealth-fetch.ts';
59
62
  import { clearCachedToken, loadCachedToken, saveCachedToken } from './stealth-token-cache.ts';
63
+ import { prepareTeachBrowserContext } from './teach-browser-resources.ts';
60
64
  import type { ResolvedTool } from './tool-loader.ts';
61
65
  import { WorkflowSchema } from './types.ts';
62
66
  import type {
@@ -475,6 +479,7 @@ export async function runWithLadder(
475
479
  options?.onResponse ? onResponse : undefined,
476
480
  options?.onPreparedRequest ? onPreparedRequest : undefined,
477
481
  options?.signal,
482
+ options?.cdpPool,
478
483
  );
479
484
  break;
480
485
  case 'cdp-replay':
@@ -491,7 +496,12 @@ export async function runWithLadder(
491
496
  break;
492
497
  case 'stealth-fetch': {
493
498
  const paramsWithDefaults = withWorkflowDefaults(tool.workflow, params);
494
- const sf = await ensureStealthFetch(tool, stealthCache, paramsWithDefaults);
499
+ const sf = await ensureStealthFetch(
500
+ tool,
501
+ stealthCache,
502
+ paramsWithDefaults,
503
+ options?.signal,
504
+ );
495
505
  // When the workflow declares a bootstrap block, mint its declared
496
506
  // session-token state (CSRF cookies etc.) from the SAME stealth
497
507
  // session that provides the transport cookies. Without this, a
@@ -810,8 +820,14 @@ async function getOrMintCdpJar(
810
820
  cacheDir: string,
811
821
  recordingDir: string,
812
822
  forceFresh: boolean,
823
+ cdpPool?: Map<string, CdpBrowserFetch>,
824
+ signal?: AbortSignal,
813
825
  ): Promise<MintedJar | null> {
814
- if (cdpJarMinterForTest) return cdpJarMinterForTest(baseUrl, bootstrapUrl);
826
+ if (cdpJarMinterForTest) {
827
+ await prepareTeachBrowserContext(cdpPool);
828
+ if (signal?.aborted) throw abortSignalError(signal);
829
+ return cdpJarMinterForTest(baseUrl, bootstrapUrl);
830
+ }
815
831
  if (!forceFresh) {
816
832
  let cached = loadJar(cacheDir);
817
833
  // A recording NEWER than the cached jar supersedes it — e.g. the user
@@ -843,6 +859,8 @@ async function getOrMintCdpJar(
843
859
  }
844
860
  let cf: CdpBrowserFetch | undefined;
845
861
  try {
862
+ await prepareTeachBrowserContext(cdpPool);
863
+ if (signal?.aborted) throw abortSignalError(signal);
846
864
  cf = (cdpBrowserFetchFactoryForTest ?? createCdpBrowserFetch)({ baseUrl, bootstrapUrl });
847
865
  const jar = await cf.mintJar();
848
866
  if (jar.abckFlag !== '0') {
@@ -905,7 +923,7 @@ function jarLikelyStale(result: ToolResult): boolean {
905
923
 
906
924
  function credentialSeedCookies(credentials: CredentialStore): MintedJar['cookies'] {
907
925
  return credentials.cookies
908
- .filter((c) => c.name && c.value && c.domain)
926
+ .filter((c) => c.name && c.value && c.domain && !c.httpResponse?.deleted)
909
927
  .map((c) => ({
910
928
  name: c.name,
911
929
  value: c.value,
@@ -915,6 +933,7 @@ function credentialSeedCookies(credentials: CredentialStore): MintedJar['cookies
915
933
  httpOnly: c.httpOnly,
916
934
  secure: c.secure,
917
935
  sameSite: c.sameSite,
936
+ hostOnly: c.hostOnly,
918
937
  }));
919
938
  }
920
939
 
@@ -922,11 +941,38 @@ function uniqueSeedCookies(cookies: MintedJar['cookies']): MintedJar['cookies']
922
941
  if (cookies.length === 0) return undefined;
923
942
  const byScope = new Map<string, MintedJar['cookies'][number]>();
924
943
  for (const c of cookies) {
925
- byScope.set(`${c.domain}\t${c.path ?? '/'}\t${c.name}`, c);
944
+ byScope.set(cookieScopeKey({ ...c, path: c.path ?? '/' }), c);
926
945
  }
927
946
  return [...byScope.values()];
928
947
  }
929
948
 
949
+ /** Content identity only: no cookie names or values imply session semantics. */
950
+ function credentialStateHash(credentials: CredentialStore): string {
951
+ const canonical = (value: unknown): unknown => {
952
+ if (Array.isArray(value)) return value.map(canonical);
953
+ if (value && typeof value === 'object')
954
+ return Object.fromEntries(
955
+ Object.entries(value)
956
+ .sort(([a], [b]) => a.localeCompare(b))
957
+ .map(([key, entry]) => [key, canonical(entry)]),
958
+ );
959
+ return value;
960
+ };
961
+ return createHash('sha256')
962
+ .update(
963
+ JSON.stringify(
964
+ canonical({
965
+ cookies: credentials.cookies,
966
+ values: credentials.values,
967
+ storage: credentials.storage ?? [],
968
+ }),
969
+ ),
970
+ )
971
+ .digest('hex');
972
+ }
973
+
974
+ const cdpCredentialStates = new WeakMap<CdpBrowserFetch, string>();
975
+
930
976
  /**
931
977
  * fetch-bootstrap rung — the API anti-bot path. Mint a validated session jar via
932
978
  * cdp-browser (real Chrome, used ONLY to bootstrap), CLOSE the browser, then
@@ -946,9 +992,10 @@ async function runFetchBootstrap(
946
992
  onResponse?: (observation: ResponseObservation) => void,
947
993
  onPreparedRequest?: (observation: PreparedRequestObservation) => void,
948
994
  signal?: AbortSignal,
995
+ cdpPool?: Map<string, CdpBrowserFetch>,
949
996
  ): Promise<ToolResult> {
950
997
  const credentials = credentialOverride ??
951
- (await loadCredentialStore(tool.site)) ?? {
998
+ (await loadCredentialStore(tool.site, signal)) ?? {
952
999
  site: tool.site,
953
1000
  cookies: [],
954
1001
  values: {},
@@ -978,7 +1025,15 @@ async function runFetchBootstrap(
978
1025
  );
979
1026
 
980
1027
  for (let attempt = 0; attempt < 2; attempt++) {
981
- const jar = await getOrMintCdpJar(baseUrl, bootstrapUrl, cacheDir, recordingDir, attempt > 0);
1028
+ const jar = await getOrMintCdpJar(
1029
+ baseUrl,
1030
+ bootstrapUrl,
1031
+ cacheDir,
1032
+ recordingDir,
1033
+ attempt > 0,
1034
+ cdpPool,
1035
+ signal,
1036
+ );
982
1037
  if (signal?.aborted) throw abortSignalError(signal);
983
1038
  if (!jar) {
984
1039
  // Couldn't even launch the bootstrap browser → let the ladder escalate.
@@ -1078,7 +1133,7 @@ async function runCdpReplay(
1078
1133
  onPreparedRequest?: (observation: PreparedRequestObservation) => void,
1079
1134
  ): Promise<ToolResult> {
1080
1135
  const credentials = credentialOverride ??
1081
- (await loadCredentialStore(tool.site)) ?? {
1136
+ (await loadCredentialStore(tool.site, signal)) ?? {
1082
1137
  site: tool.site,
1083
1138
  cookies: [],
1084
1139
  values: {},
@@ -1101,9 +1156,36 @@ async function runCdpReplay(
1101
1156
  : undefined;
1102
1157
 
1103
1158
  const recordingDir = pathResolve(tool.dir, '..');
1104
- const cacheDir = compileBackendStateDir(tool.dir, 'cdp-replay');
1159
+ const cacheDir = pathResolve(
1160
+ compileBackendStateDir(tool.dir, 'cdp-replay'),
1161
+ createHash('sha256')
1162
+ .update(bootstrapUrl ?? baseUrl)
1163
+ .digest('hex'),
1164
+ );
1105
1165
  const poolKey = cdpReplayPoolKey(tool.site, tool.workflow.toolName, bootstrapUrl ?? baseUrl);
1106
- const pooled = cdpPool?.get(poolKey);
1166
+ if (tool.workflow.toolKind !== 'authenticate') await prepareTeachBrowserContext(cdpPool, poolKey);
1167
+ if (signal?.aborted) throw abortSignalError(signal);
1168
+ const credentialHash = credentialStateHash(credentials);
1169
+ let pooled = cdpPool?.get(poolKey);
1170
+ // A compiler may discover the live login before it has written a workflow.
1171
+ // Adopt that same run-owned browser for its first compiled action.
1172
+ if (!pooled && tool.workflow.toolKind === 'authenticate') {
1173
+ pooled = cdpPool?.get(AUTH_DISCOVERY_POOL_KEY);
1174
+ if (pooled) {
1175
+ cdpPool?.delete(AUTH_DISCOVERY_POOL_KEY);
1176
+ cdpPool?.set(poolKey, pooled);
1177
+ }
1178
+ }
1179
+ if (
1180
+ pooled &&
1181
+ tool.workflow.toolKind !== 'authenticate' &&
1182
+ cdpCredentialStates.has(pooled) &&
1183
+ cdpCredentialStates.get(pooled) !== credentialHash
1184
+ ) {
1185
+ cdpPool?.delete(poolKey);
1186
+ await pooled.close();
1187
+ pooled = undefined;
1188
+ }
1107
1189
  const ownsSession = !pooled;
1108
1190
 
1109
1191
  let cf: CdpBrowserFetch;
@@ -1111,7 +1193,7 @@ async function runCdpReplay(
1111
1193
  log('cdp-replay: reusing pooled Chrome session');
1112
1194
  cf = pooled;
1113
1195
  } else {
1114
- const seeds: MintedJar['cookies'] = [];
1196
+ let seeds = credentialSeedCookies(credentials);
1115
1197
  // Cached replay jars are recording-derived and may be stale, so authenticate
1116
1198
  // workflows do not inherit them implicitly. Current credential-store cookies
1117
1199
  // are different: they represent durable device/session state and remain
@@ -1123,13 +1205,26 @@ async function runCdpReplay(
1123
1205
  if (cached && rec && rec.mtimeMs > cached.bootstrapEpoch) cached = null;
1124
1206
  if (!cached && seedJarFromRecording(cacheDir, rec, bootstrapUrl))
1125
1207
  cached = loadJar(cacheDir);
1126
- if (cached?.cookies.length) seeds.push(...cached.cookies);
1208
+ const emptyCredentials =
1209
+ !credentials.cookies.length &&
1210
+ !Object.keys(credentials.values).length &&
1211
+ !credentials.storage?.length;
1212
+ if (
1213
+ cached &&
1214
+ (cached.credentialStateHash === credentialHash ||
1215
+ (cached.credentialStateHash === undefined && emptyCredentials))
1216
+ ) {
1217
+ // A retained full snapshot is authoritative, including its removals.
1218
+ // A credential refresh invalidates it rather than mixing sessions.
1219
+ seeds = cached.cookies;
1220
+ }
1127
1221
  } catch {
1128
1222
  // best-effort
1129
1223
  }
1130
1224
  }
1131
- seeds.push(...credentialSeedCookies(credentials));
1132
1225
  const seedCookies = uniqueSeedCookies(seeds);
1226
+ recordBrowserCookieState({ ...credentials, cookies: seedCookies ?? [] }, 'cdp-before-seed');
1227
+ if (signal?.aborted) throw abortSignalError(signal);
1133
1228
  cf = (cdpBrowserFetchFactoryForTest ?? createCdpBrowserFetch)({
1134
1229
  baseUrl,
1135
1230
  bootstrapUrl,
@@ -1146,6 +1241,7 @@ async function runCdpReplay(
1146
1241
  // declares it — never a blanket default. See AuthConfig.crossOriginCookieReinjection.
1147
1242
  reinjectCrossOriginCookies: tool.workflow.authConfig?.crossOriginCookieReinjection ?? false,
1148
1243
  });
1244
+ cdpCredentialStates.set(cf, credentialHash);
1149
1245
  // Register immediately so caller cancellation can close a browser that is
1150
1246
  // still launching or minting its first jar.
1151
1247
  if (cdpPool) cdpPool.set(poolKey, cf);
@@ -1154,22 +1250,27 @@ async function runCdpReplay(
1154
1250
  try {
1155
1251
  const jar = await cf.mintJar();
1156
1252
  if (signal?.aborted) throw abortSignalError(signal);
1253
+ const observedCookies = jar.cookies.map((c) => ({
1254
+ name: c.name,
1255
+ value: c.value,
1256
+ domain: c.domain,
1257
+ path: c.path,
1258
+ expires: c.expires,
1259
+ httpOnly: c.httpOnly,
1260
+ secure: c.secure,
1261
+ sameSite: c.sameSite,
1262
+ hostOnly: c.hostOnly ?? !c.domain.startsWith('.'),
1263
+ }));
1264
+ recordBrowserCookieState(
1265
+ { ...credentials, cookies: observedCookies },
1266
+ 'cdp-after-bootstrap',
1267
+ jar.seedCookieDiagnostics,
1268
+ );
1269
+ const replayCookies = new RuntimeCookieJar(credentials.cookies);
1270
+ replayCookies.restoreBrowserSnapshot(observedCookies);
1157
1271
  const bootstrappedCredentials: CredentialStore = {
1158
1272
  ...credentials,
1159
- cookies: [
1160
- ...credentials.cookies,
1161
- ...jar.cookies.map((c) => ({
1162
- name: c.name,
1163
- value: c.value,
1164
- domain: c.domain,
1165
- path: c.path,
1166
- expires: c.expires,
1167
- httpOnly: c.httpOnly,
1168
- secure: c.secure,
1169
- sameSite: c.sameSite,
1170
- hostOnly: !c.domain.startsWith('.'),
1171
- })),
1172
- ],
1273
+ cookies: replayCookies.toJSON(),
1173
1274
  };
1174
1275
  const captureResult = jarBootstrapCaptureState(
1175
1276
  tool.workflow.bootstrap,
@@ -1182,6 +1283,7 @@ async function runCdpReplay(
1182
1283
  return captureResult.result;
1183
1284
  }
1184
1285
 
1286
+ let runtimeCookies: CredentialStore['cookies'] | undefined;
1185
1287
  let result = await tool.toolFn(paramsWithDefaults, {
1186
1288
  credentials: bootstrappedCredentials,
1187
1289
  initialState: { ...callerState, ...captureResult.state },
@@ -1191,21 +1293,96 @@ async function runCdpReplay(
1191
1293
  ? {
1192
1294
  navigate: cf.navigate.bind(cf),
1193
1295
  snapshotCookies: cf.snapshotCookies.bind(cf),
1296
+ initialCookies: observedCookies,
1194
1297
  }
1195
1298
  : undefined,
1196
1299
  signal,
1300
+ onCookieState: (cookies: CredentialStore['cookies']) => {
1301
+ runtimeCookies = cookies;
1302
+ },
1197
1303
  ...(onResponse ? { onResponse } : {}),
1198
1304
  ...(onPreparedRequest ? { onPreparedRequest } : {}),
1199
1305
  });
1200
1306
 
1201
1307
  if (signal?.aborted) throw abortSignalError(signal);
1308
+ if (!result.ok && cf.snapshotExistingCookies) {
1309
+ try {
1310
+ const snapshotExistingCookies = cf.snapshotExistingCookies.bind(cf);
1311
+ const snapshotSignal = AbortSignal.timeout(3_000);
1312
+ const cookies = await withAbortSignal(
1313
+ snapshotExistingCookies,
1314
+ signal ? AbortSignal.any([signal, snapshotSignal]) : snapshotSignal,
1315
+ );
1316
+ if (cookies) {
1317
+ recordBrowserCookieState(
1318
+ { ...credentials, cookies },
1319
+ 'cdp-after-request',
1320
+ jar.seedCookieDiagnostics,
1321
+ );
1322
+ }
1323
+ } catch {
1324
+ if (signal?.aborted) throw abortSignalError(signal);
1325
+ // A failed diagnostic must preserve the original consumer failure.
1326
+ }
1327
+ }
1328
+ if (jarLikelyStale(result)) clearJar(cacheDir);
1202
1329
  if (result.ok) {
1203
1330
  if (cdpPool && ownsSession) cdpPool.set(poolKey, cf);
1204
1331
  try {
1205
1332
  const postJar = await cf.mintJar();
1206
- saveJar(cacheDir, postJar);
1207
- } catch {
1208
- // best-effort
1333
+ recordBrowserCookieState(
1334
+ { ...credentials, cookies: postJar.cookies },
1335
+ 'cdp-after-request',
1336
+ postJar.seedCookieDiagnostics,
1337
+ );
1338
+ let postCredentialHash = credentialHash;
1339
+ // Only a host-loaded durable snapshot opts into writeback. Synthetic
1340
+ // overrides and auth actions retain their existing persistence contract.
1341
+ // A partial seed must never turn restoration failures into deletions.
1342
+ if (
1343
+ tool.workflow.toolKind !== 'authenticate' &&
1344
+ credentials.durableRevision &&
1345
+ !postJar.seedCookieDiagnostics?.rejected &&
1346
+ !postJar.seedCookieDiagnostics?.failed
1347
+ ) {
1348
+ const finalCookies = new RuntimeCookieJar(runtimeCookies ?? credentials.cookies);
1349
+ finalCookies.restoreBrowserSnapshot(
1350
+ postJar.cookies.map((cookie) => ({
1351
+ ...cookie,
1352
+ hostOnly: cookie.hostOnly ?? !cookie.domain.startsWith('.'),
1353
+ })),
1354
+ );
1355
+ // Insertion counters belong to this runtime jar. Do not rotate the
1356
+ // durable version merely because another process reconstructed it.
1357
+ const previousScopes = new Map(
1358
+ credentials.cookies.map((cookie) => [cookieScopeKey(cookie), cookie]),
1359
+ );
1360
+ const cookies = finalCookies.toJSON().map((entry) => {
1361
+ const { creationIndex: _creationIndex, ...cookie } = entry;
1362
+ const previous = previousScopes.get(cookieScopeKey(cookie));
1363
+ return previous?.creationIndex === undefined
1364
+ ? cookie
1365
+ : { ...cookie, creationIndex: previous.creationIndex };
1366
+ });
1367
+ const update = await compareAndSwapSiteCookies(
1368
+ tool.site,
1369
+ credentials.durableRevision,
1370
+ cookies,
1371
+ { signal },
1372
+ );
1373
+ if (update.status !== 'stale') {
1374
+ postCredentialHash = credentialStateHash({ ...credentials, cookies });
1375
+ cdpCredentialStates.set(cf, postCredentialHash);
1376
+ }
1377
+ log(`cdp-replay: durable browser cookies ${update.status}`);
1378
+ }
1379
+ saveJar(cacheDir, { ...postJar, credentialStateHash: postCredentialHash });
1380
+ } catch (err) {
1381
+ if (signal?.aborted) throw abortSignalError(signal);
1382
+ clearJar(cacheDir);
1383
+ log(
1384
+ `cdp-replay post-run cookie snapshot unavailable: ${err instanceof Error ? err.message : String(err)}`,
1385
+ );
1209
1386
  } finally {
1210
1387
  if (!cdpPool && ownsSession) await cf.close();
1211
1388
  }
@@ -1215,9 +1392,12 @@ async function runCdpReplay(
1215
1392
  if (ownsSession) cdpPool.set(poolKey, cf);
1216
1393
  try {
1217
1394
  const postJar = await cf.mintJar();
1218
- saveJar(cacheDir, postJar);
1219
- } catch {
1220
- // best-effort
1395
+ saveJar(cacheDir, { ...postJar, credentialStateHash: credentialHash });
1396
+ } catch (err) {
1397
+ clearJar(cacheDir);
1398
+ log(
1399
+ `cdp-replay paused cookie snapshot unavailable: ${err instanceof Error ? err.message : String(err)}`,
1400
+ );
1221
1401
  }
1222
1402
  // Deliberately do NOT close cf — the pool retains it for the completion phase.
1223
1403
  } else if (cdpPool) {
@@ -1275,6 +1455,7 @@ async function runCdpReplay(
1275
1455
  }
1276
1456
  }
1277
1457
 
1458
+ if (signal?.aborted) throw abortSignalError(signal);
1278
1459
  return result;
1279
1460
  } catch (err) {
1280
1461
  // Session is dead — evict from pool so the next call creates a fresh one.
@@ -1549,8 +1730,9 @@ async function ensureStealthFetch(
1549
1730
  tool: ResolvedTool,
1550
1731
  cache: Map<string, StealthFetch>,
1551
1732
  params: Record<string, string | number | boolean>,
1733
+ signal?: AbortSignal,
1552
1734
  ): Promise<StealthFetch> {
1553
- const credentials = (await loadCredentialStore(tool.site)) ?? {
1735
+ const credentials = (await loadCredentialStore(tool.site, signal)) ?? {
1554
1736
  site: tool.site,
1555
1737
  cookies: [],
1556
1738
  values: {},
@@ -1910,6 +2092,7 @@ export function resolveWorkflowTool(
1910
2092
  initialState?: Record<string, unknown>;
1911
2093
  credentials?: CredentialStore;
1912
2094
  signal?: AbortSignal;
2095
+ onCookieState?: (cookies: CredentialStore['cookies']) => void;
1913
2096
  onResponse?: (observation: ResponseObservation) => void;
1914
2097
  onPreparedRequest?: (observation: PreparedRequestObservation) => void;
1915
2098
  onResponseEvidence?: (evidence: ResponseEvidence) => void;
@@ -1924,6 +2107,7 @@ export function resolveWorkflowTool(
1924
2107
  browser: o?.browser,
1925
2108
  initialState: o?.initialState,
1926
2109
  signal: o?.signal,
2110
+ onCookieState: o?.onCookieState,
1927
2111
  onResponse: o?.onResponse,
1928
2112
  onPreparedRequest: o?.onPreparedRequest,
1929
2113
  onResponseEvidence: o?.onResponseEvidence,
@@ -13,6 +13,8 @@
13
13
  import { existsSync, readFileSync } from 'node:fs';
14
14
  import { join as pathJoin } from 'node:path';
15
15
  import { z } from 'zod';
16
+ import { AuthCaptureSchema, AuthToolPlanSchema } from './auth-plan.ts';
17
+ export { AuthToolPlanSchema, type AuthToolPlan } from './auth-plan.ts';
16
18
  import { compactUrlForLlm } from './llm-url.ts';
17
19
  import { localSiteDir } from './paths.ts';
18
20
  import { detectPageMintedHeaders } from './redact.ts';
@@ -49,16 +51,6 @@ export const SharedModuleSpecSchema = z.object({
49
51
  });
50
52
  export type SharedModuleSpec = z.infer<typeof SharedModuleSpecSchema>;
51
53
 
52
- const AuthCaptureSchema = z.object({
53
- name: z.string().min(1),
54
- /** Capture source: json | response_header | cookie | text_regex. */
55
- source: z.string().min(1),
56
- /** Path / header name / cookie name / regex that locates the value. */
57
- locator: z.string().min(1),
58
- /** Where the captured value is injected downstream, e.g. "header:Authorization". */
59
- usedAs: z.string().default(''),
60
- });
61
-
62
54
  const AuthRecipeSchema = z
63
55
  .object({
64
56
  required: z.boolean().default(false),
@@ -194,18 +186,6 @@ const PerToolPlanSchema = z.object({
194
186
  });
195
187
  type PerToolPlan = z.infer<typeof PerToolPlanSchema>;
196
188
 
197
- export const AuthToolPlanSchema = z
198
- .object({
199
- toolName: z.string(),
200
- credentialRequestSeqs: z.array(z.number().int().nonnegative()).default([]),
201
- authRequestSeqs: z.array(z.number().int().nonnegative()).default([]),
202
- credentialNames: z.array(z.string()).default([]),
203
- captures: z.array(AuthCaptureSchema).default([]),
204
- notes: z.string().default(''),
205
- })
206
- .optional();
207
- export type AuthToolPlan = z.infer<typeof AuthToolPlanSchema>;
208
-
209
189
  export const BuildPlanSchema = z
210
190
  .object({
211
191
  sharedModules: z.array(SharedModuleSpecSchema).default([]),