imprint-mcp 0.6.1 → 0.6.3
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 -0
- package/package.json +3 -2
- package/prompts/audit-agent.md +10 -7
- package/prompts/build-planning.md +2 -2
- package/prompts/compile-agent.md +49 -24
- package/prompts/live-verifier-agent.md +114 -0
- package/prompts/playbook-compilation.md +8 -1
- package/src/cli.ts +121 -4
- package/src/imprint/agent.ts +7 -2
- package/src/imprint/audit.ts +430 -39
- package/src/imprint/auth-compile-tools.ts +64 -13
- package/src/imprint/backend-cache.ts +18 -11
- package/src/imprint/backend-ladder.ts +48 -34
- package/src/imprint/cdp-browser-fetch.ts +135 -42
- package/src/imprint/claude-cli-compile.ts +38 -16
- package/src/imprint/codex-cli-compile.ts +120 -21
- package/src/imprint/compile-agent-types.ts +25 -2
- package/src/imprint/compile-agent.ts +238 -36
- package/src/imprint/compile-tools.ts +331 -58
- package/src/imprint/compile-verification.ts +187 -0
- package/src/imprint/compile.ts +71 -2
- package/src/imprint/freeform-redact.ts +27 -6
- package/src/imprint/live-verifier.ts +1826 -0
- package/src/imprint/llm.ts +20 -0
- package/src/imprint/mcp-compile-server.ts +488 -84
- package/src/imprint/mcp-live-verifier-server.ts +359 -0
- package/src/imprint/mcp-server.ts +28 -3
- package/src/imprint/probe-backends.ts +147 -31
- package/src/imprint/runtime.ts +61 -11
- package/src/imprint/teach-state.ts +70 -3
- package/src/imprint/teach.ts +171 -88
- package/src/imprint/types.ts +39 -2
|
@@ -362,30 +362,81 @@ export function authWorkflowHash(workflow: Workflow): string {
|
|
|
362
362
|
return createHash('sha256').update(JSON.stringify(workflow)).digest('hex');
|
|
363
363
|
}
|
|
364
364
|
|
|
365
|
-
|
|
365
|
+
export interface AuthVerificationReceiptStatus {
|
|
366
|
+
status: 'verified' | 'stale' | 'failed' | 'missing';
|
|
367
|
+
reason: string;
|
|
368
|
+
action?: string;
|
|
369
|
+
backend?: string;
|
|
370
|
+
timestamp?: number;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Read the latest auth verification receipt without initiating authentication.
|
|
374
|
+
* The workflow hash is authoritative: a successful receipt for older generated
|
|
375
|
+
* code is diagnostic history, not current verification evidence. */
|
|
376
|
+
export function readAuthVerificationReceiptStatus(
|
|
377
|
+
toolDir: string,
|
|
378
|
+
workflow: Workflow,
|
|
379
|
+
): AuthVerificationReceiptStatus {
|
|
366
380
|
const path = pathJoin(toolDir, AUTH_VERIFICATION_ATTEMPT_SENTINEL);
|
|
367
381
|
if (!existsSync(path)) {
|
|
368
|
-
return
|
|
382
|
+
return {
|
|
383
|
+
status: 'missing',
|
|
384
|
+
reason: 'no live auth verification attempt was recorded',
|
|
385
|
+
};
|
|
369
386
|
}
|
|
370
387
|
|
|
371
|
-
let attempt: {
|
|
388
|
+
let attempt: {
|
|
389
|
+
action?: unknown;
|
|
390
|
+
ok?: unknown;
|
|
391
|
+
workflowHash?: unknown;
|
|
392
|
+
backend?: unknown;
|
|
393
|
+
timestamp?: unknown;
|
|
394
|
+
};
|
|
372
395
|
try {
|
|
373
396
|
attempt = JSON.parse(readFileSync(path, 'utf8')) as typeof attempt;
|
|
374
397
|
} catch (err) {
|
|
375
|
-
return
|
|
376
|
-
|
|
377
|
-
|
|
398
|
+
return {
|
|
399
|
+
status: 'failed',
|
|
400
|
+
reason: `live auth verification record is invalid: ${err instanceof Error ? err.message : String(err)}`,
|
|
401
|
+
};
|
|
378
402
|
}
|
|
403
|
+
const details = {
|
|
404
|
+
...(typeof attempt.action === 'string' ? { action: attempt.action } : {}),
|
|
405
|
+
...(typeof attempt.backend === 'string' ? { backend: attempt.backend } : {}),
|
|
406
|
+
...(typeof attempt.timestamp === 'number' ? { timestamp: attempt.timestamp } : {}),
|
|
407
|
+
};
|
|
379
408
|
if (attempt.ok !== true || typeof attempt.action !== 'string') {
|
|
380
|
-
return
|
|
409
|
+
return {
|
|
410
|
+
status: 'failed',
|
|
411
|
+
reason: 'the most recent live auth verification did not succeed',
|
|
412
|
+
...details,
|
|
413
|
+
};
|
|
381
414
|
}
|
|
382
415
|
if (attempt.workflowHash !== authWorkflowHash(workflow)) {
|
|
383
|
-
return
|
|
416
|
+
return {
|
|
417
|
+
status: 'stale',
|
|
418
|
+
reason: 'workflow.json changed after the most recent successful live auth verification',
|
|
419
|
+
...details,
|
|
420
|
+
};
|
|
384
421
|
}
|
|
385
422
|
const action = workflow.authConfig?.actions[attempt.action];
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
423
|
+
if (action?.outcome.type !== 'success') {
|
|
424
|
+
return {
|
|
425
|
+
status: 'failed',
|
|
426
|
+
reason: `the most recent successful live action ${JSON.stringify(attempt.action)} does not declare a success outcome`,
|
|
427
|
+
...details,
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
status: 'verified',
|
|
432
|
+
reason: `live auth verification succeeded with action ${JSON.stringify(attempt.action)}`,
|
|
433
|
+
...details,
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
function liveAttemptFailures(toolDir: string, workflow: Workflow): string[] {
|
|
438
|
+
const receipt = readAuthVerificationReceiptStatus(toolDir, workflow);
|
|
439
|
+
if (receipt.status === 'verified') return [];
|
|
440
|
+
const suffix = receipt.status === 'missing' ? '; call run_verification before done' : '';
|
|
441
|
+
return [`${receipt.reason}${suffix}`];
|
|
391
442
|
}
|
|
@@ -15,7 +15,14 @@ export type BackendsCacheStatus =
|
|
|
15
15
|
cache: BackendsCache;
|
|
16
16
|
}
|
|
17
17
|
| {
|
|
18
|
-
status: 'stale'
|
|
18
|
+
status: 'stale';
|
|
19
|
+
path: string;
|
|
20
|
+
cache: BackendsCache;
|
|
21
|
+
reason: string;
|
|
22
|
+
remediation: string;
|
|
23
|
+
}
|
|
24
|
+
| {
|
|
25
|
+
status: 'invalid';
|
|
19
26
|
path: string;
|
|
20
27
|
reason: string;
|
|
21
28
|
remediation: string;
|
|
@@ -66,6 +73,15 @@ export function loadBackendsCacheStatus(
|
|
|
66
73
|
try {
|
|
67
74
|
const raw = JSON.parse(readFileSync(path, 'utf8'));
|
|
68
75
|
const parsed = BackendsCacheSchema.parse(raw);
|
|
76
|
+
const invalidPreferredReason = invalidPreferredOrderReason(parsed);
|
|
77
|
+
if (invalidPreferredReason) {
|
|
78
|
+
if (opts.warn !== false) {
|
|
79
|
+
process.stderr.write(
|
|
80
|
+
`[imprint] backends.json at ${path} has unsafe preferred backends — ignoring (run \`${remediation}\` to regenerate): ${invalidPreferredReason}\n`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return { status: 'invalid', path, reason: invalidPreferredReason, remediation };
|
|
84
|
+
}
|
|
69
85
|
if (parsed.schemaVersion && parsed.schemaVersion >= 2 && parsed.workflowHash) {
|
|
70
86
|
const workflowPath = pathResolve(toolDir, 'workflow.json');
|
|
71
87
|
if (existsSync(workflowPath)) {
|
|
@@ -77,19 +93,10 @@ export function loadBackendsCacheStatus(
|
|
|
77
93
|
`[imprint] backends.json at ${path} is stale for current workflow — ignoring (run \`${remediation}\` to regenerate)\n`,
|
|
78
94
|
);
|
|
79
95
|
}
|
|
80
|
-
return { status: 'stale', path, reason, remediation };
|
|
96
|
+
return { status: 'stale', path, cache: parsed, reason, remediation };
|
|
81
97
|
}
|
|
82
98
|
}
|
|
83
99
|
}
|
|
84
|
-
const invalidPreferredReason = invalidPreferredOrderReason(parsed);
|
|
85
|
-
if (invalidPreferredReason) {
|
|
86
|
-
if (opts.warn !== false) {
|
|
87
|
-
process.stderr.write(
|
|
88
|
-
`[imprint] backends.json at ${path} has unsafe preferred backends — ignoring (run \`${remediation}\` to regenerate): ${invalidPreferredReason}\n`,
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
return { status: 'invalid', path, reason: invalidPreferredReason, remediation };
|
|
92
|
-
}
|
|
93
100
|
return { status: 'ok', path, cache: parsed };
|
|
94
101
|
} catch (err) {
|
|
95
102
|
const reason = err instanceof Error ? err.message : String(err);
|
|
@@ -1601,44 +1601,13 @@ export async function runWorkflowWithLadder(opts: {
|
|
|
1601
1601
|
if (!existsSync(opts.workflowPath)) {
|
|
1602
1602
|
throw new Error(`runWorkflowWithLadder: workflow.json not found at ${opts.workflowPath}`);
|
|
1603
1603
|
}
|
|
1604
|
-
const
|
|
1605
|
-
const
|
|
1604
|
+
const tool = resolveWorkflowTool(opts.workflowPath, opts.credentials);
|
|
1605
|
+
const workflow = tool.workflow;
|
|
1606
|
+
const toolDir = tool.dir;
|
|
1606
1607
|
// assetRoot only matters for playbook-rung path resolution, which this
|
|
1607
1608
|
// ladder skips. Use a conventional value for completeness.
|
|
1608
1609
|
const assetRoot = pathResolve(toolDir, '..', '..');
|
|
1609
1610
|
|
|
1610
|
-
const tool: ResolvedTool = {
|
|
1611
|
-
site: workflow.site ?? '',
|
|
1612
|
-
dir: toolDir,
|
|
1613
|
-
workflow,
|
|
1614
|
-
toolFn: async (params, fnOpts) => {
|
|
1615
|
-
// Thread ALL execution opts the rungs pass — fetchImpl (stealth), and
|
|
1616
|
-
// crucially initialState + credentials minted by fetch-bootstrap's
|
|
1617
|
-
// Chrome navigation. The production generated tool fn (tool-loader path)
|
|
1618
|
-
// forwards these to executeWorkflow; this test/probe-path toolFn must do
|
|
1619
|
-
// the same, otherwise a bootstrap-block tool's csrf/session state is
|
|
1620
|
-
// silently dropped here and the integration test fails a workflow that
|
|
1621
|
-
// actually works in production — a false waiver.
|
|
1622
|
-
const o = fnOpts as
|
|
1623
|
-
| {
|
|
1624
|
-
fetchImpl?: typeof fetch;
|
|
1625
|
-
browser?: BrowserNavigationTransport;
|
|
1626
|
-
initialState?: Record<string, unknown>;
|
|
1627
|
-
credentials?: CredentialStore;
|
|
1628
|
-
}
|
|
1629
|
-
| undefined;
|
|
1630
|
-
return executeWorkflow({
|
|
1631
|
-
workflow,
|
|
1632
|
-
params: params as Record<string, string | number | boolean>,
|
|
1633
|
-
credentials: o?.credentials ?? opts.credentials,
|
|
1634
|
-
workflowPath: opts.workflowPath,
|
|
1635
|
-
fetchImpl: o?.fetchImpl,
|
|
1636
|
-
browser: o?.browser,
|
|
1637
|
-
initialState: o?.initialState,
|
|
1638
|
-
});
|
|
1639
|
-
},
|
|
1640
|
-
};
|
|
1641
|
-
|
|
1642
1611
|
const ladder: ConcreteBackend[] = ['fetch', 'fetch-bootstrap', 'cdp-replay', 'stealth-fetch'];
|
|
1643
1612
|
|
|
1644
1613
|
const memoKey = `${tool.site}::${workflow.toolName}`;
|
|
@@ -1850,6 +1819,51 @@ export async function runWorkflowWithLadder(opts: {
|
|
|
1850
1819
|
}
|
|
1851
1820
|
}
|
|
1852
1821
|
|
|
1822
|
+
/** Build the same in-memory tool used by compile-time integration execution,
|
|
1823
|
+
* without requiring the emitted index.ts that production discovery loads.
|
|
1824
|
+
* Backend probing reuses this adapter during final pre-emission verification. */
|
|
1825
|
+
export function resolveWorkflowTool(
|
|
1826
|
+
workflowPath: string,
|
|
1827
|
+
fallbackCredentials?: CredentialStore,
|
|
1828
|
+
): ResolvedTool {
|
|
1829
|
+
if (!existsSync(workflowPath)) {
|
|
1830
|
+
throw new Error(`resolveWorkflowTool: workflow.json not found at ${workflowPath}`);
|
|
1831
|
+
}
|
|
1832
|
+
const workflow = WorkflowSchema.parse(JSON.parse(readFileSync(workflowPath, 'utf8')));
|
|
1833
|
+
const toolDir = dirname(workflowPath);
|
|
1834
|
+
return {
|
|
1835
|
+
site: workflow.site ?? '',
|
|
1836
|
+
dir: toolDir,
|
|
1837
|
+
workflow,
|
|
1838
|
+
toolFn: async (params, fnOpts) => {
|
|
1839
|
+
// Thread ALL execution opts the rungs pass — fetchImpl (stealth), and
|
|
1840
|
+
// crucially initialState + credentials minted by fetch-bootstrap's
|
|
1841
|
+
// Chrome navigation. The production generated tool fn (tool-loader path)
|
|
1842
|
+
// forwards these to executeWorkflow; this test/probe-path toolFn must do
|
|
1843
|
+
// the same, otherwise a bootstrap-block tool's csrf/session state is
|
|
1844
|
+
// silently dropped here and the integration test fails a workflow that
|
|
1845
|
+
// actually works in production — a false waiver.
|
|
1846
|
+
const o = fnOpts as
|
|
1847
|
+
| {
|
|
1848
|
+
fetchImpl?: typeof fetch;
|
|
1849
|
+
browser?: BrowserNavigationTransport;
|
|
1850
|
+
initialState?: Record<string, unknown>;
|
|
1851
|
+
credentials?: CredentialStore;
|
|
1852
|
+
}
|
|
1853
|
+
| undefined;
|
|
1854
|
+
return executeWorkflow({
|
|
1855
|
+
workflow,
|
|
1856
|
+
params: params as Record<string, string | number | boolean>,
|
|
1857
|
+
credentials: o?.credentials ?? fallbackCredentials,
|
|
1858
|
+
workflowPath,
|
|
1859
|
+
fetchImpl: o?.fetchImpl,
|
|
1860
|
+
browser: o?.browser,
|
|
1861
|
+
initialState: o?.initialState,
|
|
1862
|
+
});
|
|
1863
|
+
},
|
|
1864
|
+
};
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1853
1867
|
export interface RenderedRequest {
|
|
1854
1868
|
method: string;
|
|
1855
1869
|
/** Final, fully-substituted + transform-applied request URL. */
|
|
@@ -94,6 +94,27 @@ export function jarCookiesValidated(cookies: Array<{ name: string; value: string
|
|
|
94
94
|
return cookies.some((c) => c.name === 'bm_sv');
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
export function buildNavigationSelectorExpression(selector: string): string {
|
|
98
|
+
return `document.querySelector(${JSON.stringify(selector)}) !== null`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function buildNavigationClickTargetExpression(selector: string): string {
|
|
102
|
+
return `(() => {
|
|
103
|
+
const target = document.querySelector(${JSON.stringify(selector)});
|
|
104
|
+
if (!(target instanceof Element)) return null;
|
|
105
|
+
target.scrollIntoView({ block: 'center', inline: 'center' });
|
|
106
|
+
const rect = target.getBoundingClientRect();
|
|
107
|
+
if (rect.width <= 0 || rect.height <= 0) return null;
|
|
108
|
+
if (target.matches(':disabled')) return null;
|
|
109
|
+
const x = rect.left + rect.width / 2;
|
|
110
|
+
const y = rect.top + rect.height / 2;
|
|
111
|
+
const hit = document.elementFromPoint(x, y);
|
|
112
|
+
if (hit !== target && !target.contains(hit) && !hit?.contains(target)) return null;
|
|
113
|
+
target.focus();
|
|
114
|
+
return { x, y };
|
|
115
|
+
})()`;
|
|
116
|
+
}
|
|
117
|
+
|
|
97
118
|
export interface CdpBrowserFetch {
|
|
98
119
|
/** typeof fetch — executes the request inside the live trusted Chrome page. */
|
|
99
120
|
readonly fetchImpl: typeof fetch;
|
|
@@ -109,6 +130,9 @@ export interface CdpBrowserFetch {
|
|
|
109
130
|
timeoutMs?: number;
|
|
110
131
|
pollIntervalMs?: number;
|
|
111
132
|
urlIncludes?: string;
|
|
133
|
+
selector?: string;
|
|
134
|
+
actions?: Array<{ action: 'click'; selector: string }>;
|
|
135
|
+
resultSelector?: string;
|
|
112
136
|
cookie?: { name: string; domain?: string; path?: string };
|
|
113
137
|
},
|
|
114
138
|
): Promise<Response>;
|
|
@@ -1253,6 +1277,49 @@ export function createCdpBrowserFetch(opts: CdpBrowserFetchOptions): CdpBrowserF
|
|
|
1253
1277
|
return currentPageUrl;
|
|
1254
1278
|
};
|
|
1255
1279
|
|
|
1280
|
+
const dispatchTrustedClick = async (
|
|
1281
|
+
c: CdpClient,
|
|
1282
|
+
x: number,
|
|
1283
|
+
y: number,
|
|
1284
|
+
label: string,
|
|
1285
|
+
timeoutMs: number,
|
|
1286
|
+
): Promise<void> => {
|
|
1287
|
+
await withTimeout(c.Page.bringToFront(), `CDP Page.bringToFront(${label})`, timeoutMs);
|
|
1288
|
+
const timestamp = Date.now() / 1000;
|
|
1289
|
+
await withTimeout(
|
|
1290
|
+
c.Input.dispatchMouseEvent({ type: 'mouseMoved', x, y, timestamp }),
|
|
1291
|
+
`CDP Input.dispatchMouseEvent(${label} mouseMoved)`,
|
|
1292
|
+
timeoutMs,
|
|
1293
|
+
);
|
|
1294
|
+
await sleep(100);
|
|
1295
|
+
await withTimeout(
|
|
1296
|
+
c.Input.dispatchMouseEvent({
|
|
1297
|
+
type: 'mousePressed',
|
|
1298
|
+
button: 'left',
|
|
1299
|
+
buttons: 1,
|
|
1300
|
+
clickCount: 1,
|
|
1301
|
+
x,
|
|
1302
|
+
y,
|
|
1303
|
+
timestamp,
|
|
1304
|
+
}),
|
|
1305
|
+
`CDP Input.dispatchMouseEvent(${label} mousePressed)`,
|
|
1306
|
+
timeoutMs,
|
|
1307
|
+
);
|
|
1308
|
+
await sleep(50);
|
|
1309
|
+
await withTimeout(
|
|
1310
|
+
c.Input.dispatchMouseEvent({
|
|
1311
|
+
type: 'mouseReleased',
|
|
1312
|
+
button: 'left',
|
|
1313
|
+
clickCount: 1,
|
|
1314
|
+
x,
|
|
1315
|
+
y,
|
|
1316
|
+
timestamp: Date.now() / 1000,
|
|
1317
|
+
}),
|
|
1318
|
+
`CDP Input.dispatchMouseEvent(${label} mouseReleased)`,
|
|
1319
|
+
timeoutMs,
|
|
1320
|
+
);
|
|
1321
|
+
};
|
|
1322
|
+
|
|
1256
1323
|
const navigate = async (
|
|
1257
1324
|
rawUrl: string,
|
|
1258
1325
|
options: {
|
|
@@ -1263,6 +1330,9 @@ export function createCdpBrowserFetch(opts: CdpBrowserFetchOptions): CdpBrowserF
|
|
|
1263
1330
|
timeoutMs?: number;
|
|
1264
1331
|
pollIntervalMs?: number;
|
|
1265
1332
|
urlIncludes?: string;
|
|
1333
|
+
selector?: string;
|
|
1334
|
+
actions?: Array<{ action: 'click'; selector: string }>;
|
|
1335
|
+
resultSelector?: string;
|
|
1266
1336
|
cookie?: { name: string; domain?: string; path?: string };
|
|
1267
1337
|
} = {},
|
|
1268
1338
|
): Promise<Response> => {
|
|
@@ -1402,44 +1472,7 @@ export function createCdpBrowserFetch(opts: CdpBrowserFetchOptions): CdpBrowserF
|
|
|
1402
1472
|
log(
|
|
1403
1473
|
`form POST: trusted click on ${String(directive.tag ?? 'control')} type=${String(directive.type ?? 'unknown')} focused=${focusProbe.result.value === true} hit=${liveTarget.hit === true}`,
|
|
1404
1474
|
);
|
|
1405
|
-
|
|
1406
|
-
await withTimeout(
|
|
1407
|
-
c.Input.dispatchMouseEvent({
|
|
1408
|
-
type: 'mouseMoved',
|
|
1409
|
-
x: clickX,
|
|
1410
|
-
y: clickY,
|
|
1411
|
-
timestamp,
|
|
1412
|
-
}),
|
|
1413
|
-
'CDP Input.dispatchMouseEvent(form mouseMoved)',
|
|
1414
|
-
cdpCommandTimeoutMs,
|
|
1415
|
-
);
|
|
1416
|
-
await sleep(100);
|
|
1417
|
-
await withTimeout(
|
|
1418
|
-
c.Input.dispatchMouseEvent({
|
|
1419
|
-
type: 'mousePressed',
|
|
1420
|
-
button: 'left',
|
|
1421
|
-
buttons: 1,
|
|
1422
|
-
clickCount: 1,
|
|
1423
|
-
x: clickX,
|
|
1424
|
-
y: clickY,
|
|
1425
|
-
timestamp,
|
|
1426
|
-
}),
|
|
1427
|
-
'CDP Input.dispatchMouseEvent(form mousePressed)',
|
|
1428
|
-
cdpCommandTimeoutMs,
|
|
1429
|
-
);
|
|
1430
|
-
await sleep(50);
|
|
1431
|
-
await withTimeout(
|
|
1432
|
-
c.Input.dispatchMouseEvent({
|
|
1433
|
-
type: 'mouseReleased',
|
|
1434
|
-
button: 'left',
|
|
1435
|
-
clickCount: 1,
|
|
1436
|
-
x: clickX,
|
|
1437
|
-
y: clickY,
|
|
1438
|
-
timestamp: Date.now() / 1000,
|
|
1439
|
-
}),
|
|
1440
|
-
'CDP Input.dispatchMouseEvent(form mouseReleased)',
|
|
1441
|
-
cdpCommandTimeoutMs,
|
|
1442
|
-
);
|
|
1475
|
+
await dispatchTrustedClick(c, clickX, clickY, 'form', cdpCommandTimeoutMs);
|
|
1443
1476
|
await sleep(250);
|
|
1444
1477
|
const clickProbe = await withTimeout(
|
|
1445
1478
|
c.Runtime.evaluate({
|
|
@@ -1491,15 +1524,28 @@ export function createCdpBrowserFetch(opts: CdpBrowserFetchOptions): CdpBrowserF
|
|
|
1491
1524
|
}
|
|
1492
1525
|
}
|
|
1493
1526
|
|
|
1494
|
-
const
|
|
1527
|
+
const selectorExists = async (selector?: string): Promise<boolean> => {
|
|
1528
|
+
if (!selector) return true;
|
|
1529
|
+
const evaluated = await withTimeout(
|
|
1530
|
+
c.Runtime.evaluate({
|
|
1531
|
+
expression: buildNavigationSelectorExpression(selector),
|
|
1532
|
+
returnByValue: true,
|
|
1533
|
+
}),
|
|
1534
|
+
'CDP Runtime.evaluate(navigation selector)',
|
|
1535
|
+
cdpCommandTimeoutMs,
|
|
1536
|
+
);
|
|
1537
|
+
return evaluated.result.value === true;
|
|
1538
|
+
};
|
|
1539
|
+
const hasPredicate = Boolean(options.urlIncludes || options.selector || options.cookie);
|
|
1540
|
+
const deadline = Date.now() + timeoutMs;
|
|
1495
1541
|
if (!hasPredicate) {
|
|
1496
1542
|
await withTimeout(lifecyclePromise, `CDP Page.${options.waitUntil ?? 'load'}`, timeoutMs);
|
|
1497
1543
|
} else {
|
|
1498
|
-
const deadline = Date.now() + timeoutMs;
|
|
1499
1544
|
let lastUrl = targetUrl;
|
|
1500
1545
|
while (Date.now() < deadline) {
|
|
1501
1546
|
lastUrl = await readCurrentPageUrl(c);
|
|
1502
1547
|
const urlMatches = !options.urlIncludes || lastUrl.includes(options.urlIncludes);
|
|
1548
|
+
const selectorMatches = await selectorExists(options.selector);
|
|
1503
1549
|
let cookieMatches = !options.cookie;
|
|
1504
1550
|
if (options.cookie) {
|
|
1505
1551
|
const expectedDomain = options.cookie.domain?.replace(/^\./, '').toLowerCase();
|
|
@@ -1515,7 +1561,7 @@ export function createCdpBrowserFetch(opts: CdpBrowserFetchOptions): CdpBrowserF
|
|
|
1515
1561
|
return !options.cookie?.path || cookie.path === options.cookie.path;
|
|
1516
1562
|
});
|
|
1517
1563
|
}
|
|
1518
|
-
if (urlMatches && cookieMatches) break;
|
|
1564
|
+
if (urlMatches && selectorMatches && cookieMatches) break;
|
|
1519
1565
|
await sleep(Math.min(pollIntervalMs, Math.max(1, deadline - Date.now())));
|
|
1520
1566
|
}
|
|
1521
1567
|
|
|
@@ -1528,6 +1574,7 @@ export function createCdpBrowserFetch(opts: CdpBrowserFetchOptions): CdpBrowserF
|
|
|
1528
1574
|
await sleep(Math.min(500, Math.max(1, deadline - Date.now())));
|
|
1529
1575
|
|
|
1530
1576
|
const finalUrl = await readCurrentPageUrl(c);
|
|
1577
|
+
const finalSelectorMatches = await selectorExists(options.selector);
|
|
1531
1578
|
const allCookies = await snapshotAllCookies(c);
|
|
1532
1579
|
const expectedDomain = options.cookie?.domain?.replace(/^\./, '').toLowerCase();
|
|
1533
1580
|
const matchedCookie = !options.cookie
|
|
@@ -1542,9 +1589,14 @@ export function createCdpBrowserFetch(opts: CdpBrowserFetchOptions): CdpBrowserF
|
|
|
1542
1589
|
}
|
|
1543
1590
|
return !options.cookie?.path || cookie.path === options.cookie.path;
|
|
1544
1591
|
});
|
|
1545
|
-
if (
|
|
1592
|
+
if (
|
|
1593
|
+
(options.urlIncludes && !finalUrl.includes(options.urlIncludes)) ||
|
|
1594
|
+
!finalSelectorMatches ||
|
|
1595
|
+
!matchedCookie
|
|
1596
|
+
) {
|
|
1546
1597
|
const criteria = [
|
|
1547
1598
|
options.urlIncludes ? `URL containing ${JSON.stringify(options.urlIncludes)}` : null,
|
|
1599
|
+
options.selector ? `CSS selector ${JSON.stringify(options.selector)}` : null,
|
|
1548
1600
|
options.cookie ? `cookie ${options.cookie.name}` : null,
|
|
1549
1601
|
]
|
|
1550
1602
|
.filter(Boolean)
|
|
@@ -1555,6 +1607,47 @@ export function createCdpBrowserFetch(opts: CdpBrowserFetchOptions): CdpBrowserF
|
|
|
1555
1607
|
}
|
|
1556
1608
|
}
|
|
1557
1609
|
|
|
1610
|
+
for (const interaction of options.actions ?? []) {
|
|
1611
|
+
if (interaction.action !== 'click') continue;
|
|
1612
|
+
const target = await withTimeout(
|
|
1613
|
+
c.Runtime.evaluate({
|
|
1614
|
+
expression: buildNavigationClickTargetExpression(interaction.selector),
|
|
1615
|
+
returnByValue: true,
|
|
1616
|
+
}),
|
|
1617
|
+
`CDP Runtime.evaluate(navigation click target ${JSON.stringify(interaction.selector)})`,
|
|
1618
|
+
Math.max(1, deadline - Date.now()),
|
|
1619
|
+
).catch(() => null);
|
|
1620
|
+
const point = target?.result.value as { x?: unknown; y?: unknown } | null | undefined;
|
|
1621
|
+
if (typeof point?.x !== 'number' || typeof point.y !== 'number') {
|
|
1622
|
+
throw new Error(
|
|
1623
|
+
`browser navigation action could not find a visible click target for ${JSON.stringify(interaction.selector)}`,
|
|
1624
|
+
);
|
|
1625
|
+
}
|
|
1626
|
+
await dispatchTrustedClick(
|
|
1627
|
+
c,
|
|
1628
|
+
point.x,
|
|
1629
|
+
point.y,
|
|
1630
|
+
'navigation',
|
|
1631
|
+
Math.max(1, deadline - Date.now()),
|
|
1632
|
+
);
|
|
1633
|
+
await sleep(150);
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
if (options.resultSelector) {
|
|
1637
|
+
let matched = false;
|
|
1638
|
+
while (Date.now() < deadline) {
|
|
1639
|
+
matched = await selectorExists(options.resultSelector).catch(() => false);
|
|
1640
|
+
if (matched) break;
|
|
1641
|
+
await sleep(Math.min(pollIntervalMs, Math.max(1, deadline - Date.now())));
|
|
1642
|
+
}
|
|
1643
|
+
if (!matched) {
|
|
1644
|
+
throw new Error(
|
|
1645
|
+
`browser navigation timed out after ${timeoutMs}ms waiting for post-action CSS selector ${JSON.stringify(options.resultSelector)}`,
|
|
1646
|
+
);
|
|
1647
|
+
}
|
|
1648
|
+
await sleep(Math.min(500, Math.max(1, deadline - Date.now())));
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1558
1651
|
currentPageUrl = await readCurrentPageUrl(c);
|
|
1559
1652
|
let html = '';
|
|
1560
1653
|
try {
|
|
@@ -39,7 +39,7 @@ import type {
|
|
|
39
39
|
import { formatCandidateContext, formatToolPlan } from './compile-agent-types.ts';
|
|
40
40
|
import { preferredAgentModel } from './llm.ts';
|
|
41
41
|
import { createLog } from './log.ts';
|
|
42
|
-
import { COMPILE_SENTINELS } from './mcp-compile-server.ts';
|
|
42
|
+
import { COMPILE_SENTINELS, compileDeadlineAfterVerification } from './mcp-compile-server.ts';
|
|
43
43
|
import type { SharedCompileContext, ToolCandidate } from './tool-candidates.ts';
|
|
44
44
|
import {
|
|
45
45
|
endTraceSpan,
|
|
@@ -60,6 +60,12 @@ const CLI_PATH = pathJoin(REPO_ROOT, 'src', 'cli.ts');
|
|
|
60
60
|
const MCP_SERVER_NAME = 'imprint-compile';
|
|
61
61
|
const MAX_VERIFICATION_CYCLES = 5;
|
|
62
62
|
|
|
63
|
+
function formatRevisionMode(enabled: boolean | undefined): string {
|
|
64
|
+
return enabled
|
|
65
|
+
? 'REVISION MODE: inspect read_session_summary.revisionContext and the listed existing artifacts/diagnostics first. Preserve proven behavior; repair or honestly narrow only what evidence contradicts.'
|
|
66
|
+
: '';
|
|
67
|
+
}
|
|
68
|
+
|
|
63
69
|
/**
|
|
64
70
|
* Thinking effort for the compile agent. Deliberately `high`, not `max`:
|
|
65
71
|
* empirically, max-effort thinking generates a large volume of reasoning tokens
|
|
@@ -110,6 +116,8 @@ interface CompileViaClaudeCliOptions {
|
|
|
110
116
|
sharedModules?: SharedModuleManifestEntry[];
|
|
111
117
|
/** Per-tool implementation plan injected into the agent's initial message. */
|
|
112
118
|
toolPlan?: string;
|
|
119
|
+
/** Revise existing generated artifacts from durable verification feedback. */
|
|
120
|
+
revisionMode?: boolean;
|
|
113
121
|
/** Present → drive an auth compile rather than a data compile. */
|
|
114
122
|
authMode?: AuthCliCompileMode;
|
|
115
123
|
/** Auth segments only: resume a prior segment's claude session with a new user
|
|
@@ -275,6 +283,7 @@ async function runClaudeCliAttempt(opts: CompileViaClaudeCliOptions): Promise<Co
|
|
|
275
283
|
COMPILE_SENTINELS.done,
|
|
276
284
|
COMPILE_SENTINELS.giveUp,
|
|
277
285
|
COMPILE_SENTINELS.checkpoint,
|
|
286
|
+
COMPILE_SENTINELS.verificationState,
|
|
278
287
|
]) {
|
|
279
288
|
const p = pathJoin(opts.absoluteToolDir, name);
|
|
280
289
|
if (existsSync(p)) {
|
|
@@ -325,10 +334,13 @@ async function runClaudeCliAttempt(opts: CompileViaClaudeCliOptions): Promise<Co
|
|
|
325
334
|
sessionPathAbs,
|
|
326
335
|
'--tool-dir',
|
|
327
336
|
opts.absoluteToolDir,
|
|
337
|
+
'--provider',
|
|
338
|
+
'claude-cli',
|
|
328
339
|
...(opts.candidate ? ['--candidate-json', JSON.stringify(opts.candidate)] : []),
|
|
329
340
|
...(opts.sharedContext ? ['--shared-context-json', JSON.stringify(opts.sharedContext)] : []),
|
|
330
341
|
...(opts.buildPlanPath ? ['--build-plan-path', opts.buildPlanPath] : []),
|
|
331
342
|
...(opts.sharedModules ? ['--shared-modules-json', JSON.stringify(opts.sharedModules)] : []),
|
|
343
|
+
...(opts.revisionMode ? ['--revision-mode'] : []),
|
|
332
344
|
];
|
|
333
345
|
allowedToolNames = [
|
|
334
346
|
'read_session_summary',
|
|
@@ -355,6 +367,7 @@ Tool directory: ${opts.absoluteToolDir}
|
|
|
355
367
|
You will write artifacts into the tool directory.
|
|
356
368
|
${formatCandidateContext(opts.candidate, opts.sharedContext, assignedSharedModules)}
|
|
357
369
|
${formatToolPlan(opts.toolPlan)}
|
|
370
|
+
${formatRevisionMode(opts.revisionMode)}
|
|
358
371
|
|
|
359
372
|
Begin by calling read_session_summary to orient yourself, then proceed per the system prompt.`;
|
|
360
373
|
}
|
|
@@ -425,19 +438,14 @@ Begin by calling read_session_summary to orient yourself, then proceed per the s
|
|
|
425
438
|
child = spawn('claude', args, {
|
|
426
439
|
cwd: REPO_ROOT,
|
|
427
440
|
// Claude CLI's default MCP_TOOL_TIMEOUT is 60s. The compile MCP
|
|
428
|
-
// server's `done` tool runs external verification inline
|
|
429
|
-
//
|
|
430
|
-
//
|
|
431
|
-
//
|
|
432
|
-
//
|
|
433
|
-
//
|
|
434
|
-
//
|
|
435
|
-
//
|
|
436
|
-
// actually complete and the agent receives the failure feedback
|
|
437
|
-
// (and ships with `liveVerified: false` via the waiver path)
|
|
438
|
-
// rather than getting `-32000: Connection closed` mid-call and
|
|
439
|
-
// wasting the rest of its turn budget. Honor user-set env so an
|
|
440
|
-
// operator on a fast network can tighten without editing source.
|
|
441
|
+
// server's `done` tool runs external verification inline: one live
|
|
442
|
+
// integration suite, parser tests, typechecking, and a fresh semantic
|
|
443
|
+
// verifier agent that may make a narrowly targeted follow-up call. On
|
|
444
|
+
// bot-protected sites one suite can still take 10-15 minutes as each
|
|
445
|
+
// assertion escalates fetch → fetch-bootstrap → stealth-fetch. Keep the
|
|
446
|
+
// 30-minute cap so the agent receives semantic feedback rather than an
|
|
447
|
+
// MCP connection-close midway through review. Honor user-set env so an
|
|
448
|
+
// operator on a fast network can tighten it without editing source.
|
|
441
449
|
// Connection-startup timeout stays at 60s for cold Playwright boot.
|
|
442
450
|
env: {
|
|
443
451
|
...process.env,
|
|
@@ -516,9 +524,19 @@ async function driveStreamJson(
|
|
|
516
524
|
};
|
|
517
525
|
|
|
518
526
|
const scheduleDeadlineCheck = (): ReturnType<typeof setTimeout> => {
|
|
519
|
-
const
|
|
527
|
+
const effectiveDeadlineMs = compileDeadlineAfterVerification(
|
|
528
|
+
opts.absoluteToolDir,
|
|
529
|
+
currentDeadlineMs,
|
|
530
|
+
);
|
|
531
|
+
const remaining = Math.max(0, effectiveDeadlineMs - Date.now());
|
|
520
532
|
return setTimeout(async () => {
|
|
521
533
|
if (childExited) return;
|
|
534
|
+
// done() verification runs in the MCP child. Re-read its lifecycle clock
|
|
535
|
+
// before killing the compiler so only reasoning time counts here.
|
|
536
|
+
if (Date.now() < compileDeadlineAfterVerification(opts.absoluteToolDir, currentDeadlineMs)) {
|
|
537
|
+
deadlineTimer = scheduleDeadlineCheck();
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
522
540
|
if (opts.onDeadlineReached) {
|
|
523
541
|
const extensionMs = await opts.onDeadlineReached();
|
|
524
542
|
if (childExited) return;
|
|
@@ -748,7 +766,11 @@ async function driveStreamJson(
|
|
|
748
766
|
}
|
|
749
767
|
|
|
750
768
|
// Wall-clock deadline exceeded?
|
|
751
|
-
if (
|
|
769
|
+
if (
|
|
770
|
+
Date.now() > compileDeadlineAfterVerification(opts.absoluteToolDir, currentDeadlineMs) &&
|
|
771
|
+
!existsSync(doneSentinel) &&
|
|
772
|
+
!existsSync(giveUpSentinel)
|
|
773
|
+
) {
|
|
752
774
|
return {
|
|
753
775
|
success: false,
|
|
754
776
|
outcome: 'timeout',
|