Repository navigation
Expand file tree
/
Copy pathscan.mjs
More file actions
588 lines (534 loc) · 31 KB
/
Copy pathscan.mjs
File metadata and controls
588 lines (534 loc) · 31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env node
/**
* agentready — AI Agent Readiness scanner.
* Zero dependencies. Node 18+ (uses global fetch).
*
* Usage: node scan.mjs <url> [--out <dir>] [--json] [--top <n>] [--timeout <ms>]
* [--brand <file.json>] [--user-agent <ua>] [--pdf] [--chrome <path>]
*
* Scores how ready a website is to be read, cited and operated by AI agents:
* llms.txt, AI crawler rules, structured data, OpenAPI, MCP manifests,
* markdown content negotiation, and the machine-readable files agents look for.
*
* Exit codes: 0 = scanned, 1 = usage error, 2 = site unreachable (nothing scored).
*/
import { writeFileSync, mkdirSync } from 'node:fs';
import { join } from 'node:path';
import { randomBytes } from 'node:crypto';
import { lookup } from 'node:dns/promises';
import {
loadBrand, resolveUserAgent, escHtml, printCss, footerText, maybeWritePdf, LIGHT_COLORS,
} from './brand.mjs';
const args = process.argv.slice(2);
const flag = (name, dflt) => { const i = args.indexOf(name); return i !== -1 && args[i + 1] ? args[i + 1] : dflt; };
const valued = new Set(['--out', '--top', '--timeout', '--brand', '--user-agent', '--chrome']);
if (!args.length || args.includes('-h') || args.includes('--help')) {
console.log('usage: node scan.mjs <url> [--out <dir>] [--json] [--top <n>] [--timeout <ms>] [--brand <file.json>] [--user-agent <ua>] [--pdf] [--chrome <path>]');
process.exit(args.length ? 0 : 1);
}
const target = args.find((a, i) => !a.startsWith('--') && !valued.has(args[i - 1]));
if (!target) {
console.error('usage: node scan.mjs <url> [--out <dir>] [--json] [--top <n>] [--timeout <ms>] [--brand <file.json>] [--user-agent <ua>] [--pdf] [--chrome <path>]');
process.exit(1);
}
let brand;
try { brand = loadBrand(flag('--brand', null)); }
catch (e) { console.error(e.message); process.exit(1); }
let UA;
try { UA = resolveUserAgent(flag('--user-agent', null), brand); }
catch (e) { console.error(e.message); process.exit(1); }
const probePrefix = brand.probePrefix;
const TIMEOUT_MS = Number(flag('--timeout', 12000));
const wantPdf = args.includes('--pdf');
const chromeFlag = flag('--chrome', null);
async function get(url, headers = {}) {
const t0 = Date.now();
try {
const res = await fetch(url, {
redirect: 'follow',
signal: AbortSignal.timeout(TIMEOUT_MS),
headers: { 'user-agent': UA, accept: '*/*', ...headers },
});
const text = await res.text();
return {
ok: res.ok, status: res.status, finalUrl: res.url, text,
ct: (res.headers.get('content-type') || '').toLowerCase(),
ms: Date.now() - t0, err: null,
};
} catch (e) {
return { ok: false, status: 0, finalUrl: url, text: '', ct: '', ms: Date.now() - t0, err: errText(e) };
}
}
// fetch() hides the useful part of network errors in e.cause; surface the code (ENOTFOUND, CERT_HAS_EXPIRED, …)
function errText(e) {
const c = e?.cause;
const code = c?.code || c?.errno || e?.code || e?.name;
const msg = c?.message || e?.message || String(e);
return code && !msg.includes(code) ? `${code}: ${msg}` : msg;
}
function hostOf(input) {
let u = input.trim();
if (!/^https?:\/\//i.test(u)) u = 'https://' + u;
return new URL(u).host;
}
const looksHtml = (s) => /^\s*(<!doctype html|<html|<head[\s>]|<body[\s>])/i.test(s.slice(0, 600));
const norm = (s) => s.replace(/data-cfemail="[0-9a-f]+"|email-protection#[0-9a-f]+|nonce="[^"]*"|\b[0-9a-f]{16,}\b/gi, '').replace(/\s+/g, ' ').trim();
function hasJsonLdTypes(html) {
const types = new Set();
const re = /<script[^>]+type=["']application\/ld\+json["'][^>]*>([\s\S]*?)<\/script>/gi;
let m;
while ((m = re.exec(html)) !== null) {
const raw = m[1].trim();
try {
const walk = (node) => {
if (!node || typeof node !== 'object') return;
if (Array.isArray(node)) return node.forEach(walk);
const t = node['@type'];
if (typeof t === 'string') types.add(t);
else if (Array.isArray(t)) t.forEach((x) => typeof x === 'string' && types.add(x));
if (node['@graph']) walk(node['@graph']);
};
walk(JSON.parse(raw));
} catch { /* malformed block, skip */ }
}
return types;
}
// <meta> attributes in any order, either quote style, apostrophes inside double-quoted values
function metaContent(html, attr, value) {
for (const tag of html.match(/<meta\b[^>]*>/gi) || []) {
const a = {};
for (const m of tag.matchAll(/([a-z:-]+)\s*=\s*("([^"]*)"|'([^']*)')/gi)) a[m[1].toLowerCase()] = m[3] ?? m[4];
if ((a[attr] || '').toLowerCase() === value) return a.content ?? '';
}
return null;
}
/**
* RFC 9309-style robots.txt parse: groups of user-agent lines followed by rules. Comments are stripped,
* so a bot named only in a comment is NOT "addressed". Returns the policy each AI crawler gets.
*/
/**
* Is this a robots.txt at all? RFC 9309 records are user-agent groups (user-agent + allow/disallow); the
* sitemap record (sitemaps.org, referenced by RFC 9309 §2.2.4) stands on its own outside any group.
* A file with only "Sitemap: …" is a valid robots.txt: it exists, it just sets no crawler rules.
*/
const isRobotsTxt = (text) => text.split(/\r?\n/).some((l) => /^\s*(user-agent|sitemap)\s*:\s*\S/i.test(l.replace(/#.*/, '')));
function robotsPolicy(text, bots) {
const groups = []; let cur = null; let lastWasAgent = false;
for (const raw of text.split(/\r?\n/)) {
const line = raw.replace(/#.*/, '').trim();
const m = line.match(/^([a-z-]+)\s*:\s*(.*)$/i);
if (!m) continue;
const key = m[1].toLowerCase(); const val = m[2].trim();
if (key === 'user-agent') {
if (!cur || !lastWasAgent) { cur = { agents: [], rules: [] }; groups.push(cur); }
cur.agents.push(val.toLowerCase()); lastWasAgent = true;
} else {
lastWasAgent = false;
if (cur && (key === 'allow' || key === 'disallow')) cur.rules.push({ type: key, path: val });
}
}
const verdict = (g) => {
if (!g) return null;
const dis = g.rules.filter((r) => r.type === 'disallow' && r.path); // an empty "Disallow:" allows everything
const allowRoot = g.rules.some((r) => r.type === 'allow' && (r.path === '/' || r.path === '/*'));
if (dis.some((r) => r.path === '/' || r.path === '/*') && !allowRoot) return 'blocked';
return dis.length ? 'allowed (some paths disallowed)' : 'allowed';
};
const wildcard = verdict(groups.find((g) => g.agents.includes('*')));
const named = {};
for (const b of bots) {
const g = groups.find((x) => x.agents.includes(b.toLowerCase()));
if (g) named[b] = verdict(g);
}
return { named, wildcard, groups: groups.length };
}
async function scan(rawUrl) {
const host = hostOf(rawUrl);
// --- reachability + HTTPS: actually connect instead of trusting the URL prefix ---
const httpsHome = await get(`https://${host}/`);
let origin = `https://${host}`; let home = httpsHome; let httpHome = null;
if (httpsHome.status === 0) {
httpHome = await get(`http://${host}/`);
origin = `http://${host}`; home = httpHome;
}
const reachable = home.status !== 0;
if (!reachable) {
let dnsNote;
try { dnsNote = 'DNS: ' + (await lookup(new URL(`https://${host}`).hostname, { all: true })).map((a) => a.address).join(', '); }
catch (e) { dnsNote = `DNS: ${e.code || e.message} (domain does not resolve)`; }
return {
target: `https://${host}`, host, scannedAt: new Date().toISOString(), reachable: false,
unreachableReason: `${dnsNote}; https: ${httpsHome.err}; http: ${httpHome?.err}`,
score: null, grade: 'N/A', earned: 0, totalWeight: null, homeStatus: 0, homeMs: home.ms, homeBytes: 0,
userAgent: UA, checks: [], gaps: [],
};
}
if (home.finalUrl) origin = new URL(home.finalUrl).origin; // follow apex→www etc.
// --- catch-all probe: what does a path that cannot exist return? ---
const probePath = `/${probePrefix}-${randomBytes(6).toString('hex')}.txt`;
const probe = await get(origin + probePath);
const catchAll = probe.ok ? { status: probe.status, html: looksHtml(probe.text) || probe.ct.includes('text/html'), sig: norm(probe.text) } : null;
/**
* Shared validator for every "is this machine-readable file really there?" check.
* kind: text | xml | json | openapi. Returns { pass, evidence }.
*/
const fileCheck = (url, r, kind, validate = () => true) => {
if (r.status === 0) return { pass: false, evidence: `${url} → no response (${r.err})` };
if (!r.ok) return { pass: false, evidence: `${url} → ${r.status}` };
const want = { text: 'text/plain', xml: 'XML', json: 'JSON', openapi: 'OpenAPI JSON/YAML' }[kind];
if (catchAll && norm(r.text) === catchAll.sig) return { pass: false, evidence: `${url} → ${r.status} (soft-404: same page a random nonexistent URL returns, not ${want})` };
if (looksHtml(r.text) || (r.ct.includes('text/html') && kind !== 'xml')) return { pass: false, evidence: `${url} → ${r.status} (soft-404: HTML, not ${want})` };
if (!validate(r.text)) return { pass: false, evidence: `${url} → ${r.status} (${r.text.trim().length} bytes, but not a valid ${want} file of this type)` };
return { pass: true, evidence: `${url} → ${r.status} (${r.text.trim().length} bytes, ${r.ct.split(';')[0] || 'no content-type'})` };
};
const jsonObj = (t) => { try { const v = JSON.parse(t); return v && typeof v === 'object' && !Array.isArray(v); } catch { return false; } };
const robots = await get(origin + '/robots.txt');
const llms = await get(origin + '/llms.txt');
const mdProbe = await get(origin + '/', { accept: 'text/markdown, text/plain;q=0.9, */*;q=0.8' });
const jsonld = home.ok ? hasJsonLdTypes(home.text) : new Set();
const robotsRes = fileCheck(`${origin}/robots.txt`, robots, 'text', isRobotsTxt);
const robotsText = robotsRes.pass ? robots.text : '';
const AI_BOTS = ['GPTBot', 'OAI-SearchBot', 'ChatGPT-User', 'ClaudeBot', 'Claude-User', 'Claude-SearchBot', 'anthropic-ai',
'PerplexityBot', 'Perplexity-User', 'Google-Extended', 'Applebot-Extended', 'CCBot', 'Meta-ExternalAgent', 'Amazonbot', 'Bytespider', 'cohere-ai'];
const policy = robotsPolicy(robotsText, AI_BOTS);
const C = [];
const add = (id, name, weight, pass, evidence) => C.push({ id, name, weight, pass: !!pass, evidence });
// 1. HTTPS — a real TLS connection to https://host/ that returned an HTTP response
add('https', 'Served over HTTPS', 4, httpsHome.status !== 0,
httpsHome.status !== 0 ? `https://${host}/ → ${httpsHome.status}, TLS OK${httpsHome.finalUrl && !httpsHome.finalUrl.startsWith(`https://${host}/`) ? ` (redirected to ${httpsHome.finalUrl})` : ''}`
: `https://${host}/ failed (${httpsHome.err}); site only answered over http://`);
// 2. llms.txt
const llmsRes = fileCheck(`${origin}/llms.txt`, llms, 'text', (t) => t.trim().length > 40);
add('llms_txt', 'llms.txt present', 12, llmsRes.pass, llmsRes.evidence);
// 3. llms-full.txt
const llmsFull = await get(origin + '/llms-full.txt');
const lf = fileCheck(`${origin}/llms-full.txt`, llmsFull, 'text', (t) => t.trim().length > 40);
add('llms_full', 'llms-full.txt present', 4, lf.pass, lf.evidence);
// 4. ai.txt
const aitxt = await get(origin + '/ai.txt');
const ai = fileCheck(`${origin}/ai.txt`, aitxt, 'text', (t) => t.trim().length > 20);
add('ai_txt', 'ai.txt present', 3, ai.pass, ai.evidence);
// 5. robots.txt gives AI crawlers an explicit policy. Credit = at least one AI crawler has its own
// User-agent group (Allow OR Disallow: an explicit decision either way is the signal). Comment-only
// mentions do not count. Evidence reports allowed vs blocked so an opt-out is never mistaken for an opt-in.
const named = Object.entries(policy.named);
const allowed = named.filter(([, v]) => v.startsWith('allowed')).map(([k]) => k);
const blocked = named.filter(([, v]) => v === 'blocked').map(([k]) => k);
add('robots_ai', 'robots.txt addresses AI crawlers', 10, named.length > 0,
!robotsRes.pass ? `robots.txt missing — ${robotsRes.evidence}`
: named.length ? `explicit groups for ${named.length} AI crawlers — allowed: ${allowed.join(', ') || 'none'}; blocked: ${blocked.join(', ') || 'none'}; User-agent * → ${policy.wildcard || 'no * group'}`
: policy.groups === 0 ? 'robots.txt → 200 but it has no User-agent groups at all (Sitemap record only), so no AI crawler rules'
: `robots.txt → 200 but no User-agent group for any AI crawler (User-agent * → ${policy.wildcard || 'no * group'})`);
// 6. sitemap declared
const sitemapLine = robotsText.match(/^\s*sitemap\s*:\s*(\S+)/im);
add('robots_sitemap', 'Sitemap declared in robots.txt', 3, !!sitemapLine,
sitemapLine ? `Sitemap: ${sitemapLine[1]}` : robotsRes.pass ? 'robots.txt has no Sitemap: line' : 'no robots.txt, so no Sitemap: line');
// 7. sitemap.xml reachable
const sm = await get(origin + '/sitemap.xml');
const smRes = fileCheck(`${origin}/sitemap.xml`, sm, 'xml', (t) => /<(urlset|sitemapindex)[\s>]/i.test(t));
add('sitemap_xml', 'sitemap.xml reachable', 5, smRes.pass, smRes.evidence);
// 8. JSON-LD structured data
add('jsonld', 'JSON-LD structured data', 12, jsonld.size > 0, jsonld.size ? `${[...jsonld].slice(0, 8).join(', ')}` : 'no application/ld+json block found');
// 9. High-value schema types (not only local-business types: people, software, and articles count too)
const highValue = ['Organization', 'LocalBusiness', 'Product', 'FAQPage', 'HowTo', 'Article', 'Service', 'Offer',
'Person', 'ProfilePage', 'SoftwareApplication', 'SoftwareSourceCode', 'TechArticle', 'BlogPosting', 'NewsArticle', 'WebAPI', 'Dataset'];
const foundHigh = highValue.filter((t) => jsonld.has(t));
add('schema_types', 'High-value schema.org types', 5, foundHigh.length > 0, foundHigh.length ? foundHigh.join(', ') : 'none of ' + highValue.join('/'));
// 10. OpenAPI spec
const oaPaths = ['/openapi.json', '/openapi.yaml', '/.well-known/openapi.json', '/api/openapi.json'];
let oaHit = null; const oaMiss = [];
for (const p of oaPaths) {
const r = await get(origin + p);
const res = fileCheck(origin + p, r, p.endsWith('.yaml') ? 'openapi' : 'json',
(t) => /"(openapi|swagger)"\s*:/.test(t.slice(0, 4000)) || /^\s*(openapi|swagger)\s*:/m.test(t.slice(0, 4000)));
if (res.pass) { oaHit = res; break; }
oaMiss.push(`${p} ${res.evidence.replace(origin + p + ' → ', '')}`);
}
add('openapi', 'OpenAPI / Swagger spec', 8, !!oaHit, oaHit ? oaHit.evidence : 'not found: ' + oaMiss.join('; '));
// 11. MCP server manifest
const mcpPaths = ['/.well-known/mcp.json', '/.well-known/mcp/server.json', '/mcp.json', '/.well-known/ai-plugin.json'];
let mcpHit = null; const mcpMiss = [];
for (const p of mcpPaths) {
const r = await get(origin + p);
const res = fileCheck(origin + p, r, 'json', jsonObj);
if (res.pass) { mcpHit = res; break; }
mcpMiss.push(`${p} ${res.evidence.replace(origin + p + ' → ', '')}`);
}
add('mcp', 'Agent/MCP manifest present', 8, !!mcpHit, mcpHit ? mcpHit.evidence : 'not found: ' + mcpMiss.join('; '));
// 12. Markdown content negotiation (text/plain only counts when the body is not HTML)
const mdServed = mdProbe.ok && (mdProbe.ct.includes('text/markdown') || (mdProbe.ct.includes('text/plain') && !looksHtml(mdProbe.text)));
add('markdown', 'Markdown content negotiation', 8, mdServed, `Accept: text/markdown → content-type: ${mdProbe.ct || 'n/a'} (${mdProbe.status})`);
// 13. security.txt
const sec = await get(origin + '/.well-known/security.txt');
const secRes = fileCheck(`${origin}/.well-known/security.txt`, sec, 'text', (t) => /^\s*contact\s*:/im.test(t));
add('security_txt', 'security.txt present', 3, secRes.pass, secRes.evidence);
// 14. Discoverability meta (baseline)
const ogTitle = metaContent(home.text, 'property', 'og:title');
const desc = metaContent(home.text, 'name', 'description');
add('meta', 'OpenGraph + meta description', 5, !!ogTitle && (desc || '').length >= 20, `og:title=${!!ogTitle} description=${(desc || '').length >= 20}`);
// 15. Agent hint files
const hints = ['/.well-known/agents.json', '/agents.txt', '/.well-known/ai.txt'];
let hintHit = null; const hintMiss = [];
for (const p of hints) {
const r = await get(origin + p);
const res = fileCheck(origin + p, r, p.endsWith('.json') ? 'json' : 'text', p.endsWith('.json') ? jsonObj : (t) => t.trim().length > 10);
if (res.pass) { hintHit = res; break; }
hintMiss.push(`${p} ${res.evidence.replace(origin + p + ' → ', '')}`);
}
add('agent_hints', 'Agent hint file present', 3, !!hintHit, hintHit ? hintHit.evidence : 'not found: ' + hintMiss.join('; '));
const totalWeight = C.reduce((s, c) => s + c.weight, 0);
const earned = C.reduce((s, c) => s + (c.pass ? c.weight : 0), 0);
const score = Math.round((earned / totalWeight) * 100);
const grade = score >= 90 ? 'A' : score >= 78 ? 'B' : score >= 62 ? 'C' : score >= 45 ? 'D' : score >= 25 ? 'E' : 'F';
return {
target: origin, host, scannedAt: new Date().toISOString(), reachable: true,
score, grade, earned, totalWeight,
homeStatus: home.status, homeMs: home.ms, homeBytes: home.text.length,
userAgent: UA,
catchAll: catchAll ? { probe: probePath, status: catchAll.status, html: catchAll.html } : null,
robots: robotsRes.pass ? { aiCrawlers: policy.named, wildcard: policy.wildcard } : null,
checks: C,
gaps: C.filter((c) => !c.pass).sort((a, b) => b.weight - a.weight),
};
}
function textReport(r, top) {
const L = [];
L.push('');
L.push(` AI AGENT READINESS — ${r.host}`);
L.push(' ' + '─'.repeat(52));
if (!r.reachable) {
L.push(' UNREACHABLE — nothing was scored.');
L.push(` ${r.unreachableReason}`);
L.push(' Check the domain name, DNS, and that the site answers on https:// or http://.');
L.push('');
return L.join('\n');
}
L.push(` SCORE ${r.score}/100 GRADE ${r.grade} (${r.earned}/${r.totalWeight} weighted points)`);
if (r.catchAll) L.push(` NOTE every unknown path returns ${r.catchAll.status}${r.catchAll.html ? ' HTML' : ''} (catch-all): files matching that page are treated as missing`);
L.push(' ' + '─'.repeat(52));
for (const c of r.checks) {
L.push(` ${c.pass ? 'PASS' : 'FAIL'} ${String(c.weight).padStart(2)}pt ${c.name}`);
L.push(` ${c.evidence}`);
}
L.push(' ' + '─'.repeat(52));
const shown = r.gaps.slice(0, top);
L.push(` TOP FIXES${r.gaps.length > shown.length ? ` (${shown.length} of ${r.gaps.length}; --top N to change)` : ''}:`);
shown.forEach((c, i) => L.push(` ${String(i + 1).padStart(2)}. ${c.name} (+${c.weight} pts available)`));
if (!r.gaps.length) L.push(' none — every check passes');
L.push('');
return L.join('\n');
}
/** Default (unbranded) report — keep today's look; only addition is print CSS. */
function htmlReportDefault(r, top) {
if (!r.reachable) {
return `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Agent Readiness — ${escHtml(r.host)} (unreachable)</title>
<style>${printCss(LIGHT_COLORS)}</style></head>
<body style="font:15px/1.5 sans-serif;background:#0b0d12;color:#e8ecf4;padding:40px"><h1>AI Agent Readiness Report</h1>
<p><b>${escHtml(r.host)}</b> could not be reached, so nothing was scored.</p><pre style="white-space:pre-wrap;color:#8b95ab">${escHtml(r.unreachableReason)}</pre>
<p style="color:#8b95ab;font-size:12px">scanned ${r.scannedAt} · generated by agentready</p></body></html>`;
}
const rows = r.checks.map((c) => `
<tr class="${c.pass ? 'pass' : 'fail'}">
<td class="dot">${c.pass ? '✓' : '✗'}</td>
<td>${c.name}</td>
<td class="w">${c.weight}</td>
<td class="ev">${escHtml(c.evidence)}</td>
</tr>`).join('');
const gaps = r.gaps.slice(0, top).map((c) => `<li><b>${c.name}</b> <span class="pts">+${c.weight} pts</span></li>`).join('');
const circ = 2 * Math.PI * 54;
const dash = (r.score / 100) * circ;
return `<!doctype html>
<html lang="en"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Agent Readiness — ${r.host} (${r.score}/100)</title>
<style>
:root{--bg:#0b0d12;--card:#141821;--line:#232838;--fg:#e8ecf4;--mut:#8b95ab;--good:#3ddc84;--bad:#ff5c7a;--acc:#7c9cff}
*{box-sizing:border-box}
body{margin:0;background:var(--bg);color:var(--fg);font:15px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",Inter,sans-serif;padding:40px 20px}
.wrap{max-width:860px;margin:0 auto}
h1{font-size:20px;margin:0 0 4px;font-weight:600}
.sub{color:var(--mut);font-size:13px;margin-bottom:28px}
.hero{display:flex;gap:32px;align-items:center;background:var(--card);border:1px solid var(--line);border-radius:16px;padding:28px;margin-bottom:20px;flex-wrap:wrap}
.gauge{position:relative;flex:0 0 auto}
.grade{position:absolute;inset:0;display:flex;flex-direction:column;align-items:center;justify-content:center}
.grade b{font-size:34px;line-height:1}
.grade span{font-size:11px;color:var(--mut);letter-spacing:.08em;text-transform:uppercase}
.hero-meta{flex:1;min-width:220px}
.hero-meta h2{font-size:15px;margin:0 0 10px;font-weight:600}
ul{margin:0;padding-left:18px}
li{margin:4px 0;font-size:14px}
.pts{color:var(--acc);font-variant-numeric:tabular-nums;font-size:12px}
table{width:100%;border-collapse:collapse;background:var(--card);border:1px solid var(--line);border-radius:14px;overflow:hidden;font-size:14px}
th,td{text-align:left;padding:11px 14px;border-bottom:1px solid var(--line);vertical-align:top}
th{font-size:11px;letter-spacing:.08em;text-transform:uppercase;color:var(--mut);font-weight:600}
tr:last-child td{border-bottom:none}
.dot{width:26px;text-align:center;font-weight:700}
tr.pass .dot{color:var(--good)} tr.fail .dot{color:var(--bad)}
.w{width:46px;color:var(--mut);font-variant-numeric:tabular-nums;text-align:right}
.ev{color:var(--mut);font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;word-break:break-all}
.foot{margin-top:22px;color:var(--mut);font-size:12px}
.note{margin:0 0 20px;color:var(--mut);font-size:13px}
.cta{margin-top:22px;background:linear-gradient(135deg,#1a2133,#141821);border:1px solid var(--line);border-radius:14px;padding:20px 24px}
.cta h3{margin:0 0 6px;font-size:15px}
.cta p{margin:0;color:var(--mut);font-size:13px}
${printCss(LIGHT_COLORS)}
</style></head>
<body><div class="wrap">
<h1>AI Agent Readiness Report</h1>
<div class="sub">${r.target} · scanned ${r.scannedAt} · response ${r.homeMs} ms / ${r.homeBytes} bytes</div>
<div class="hero">
<div class="gauge">
<svg width="132" height="132" viewBox="0 0 132 132">
<circle cx="66" cy="66" r="54" fill="none" stroke="#232838" stroke-width="11"/>
<circle cx="66" cy="66" r="54" fill="none" stroke="url(#g)" stroke-width="11" stroke-linecap="round"
stroke-dasharray="${dash.toFixed(1)} ${circ.toFixed(1)}" transform="rotate(-90 66 66)"/>
<defs><linearGradient id="g" x1="0" y1="0" x2="1" y2="1">
<stop offset="0" stop-color="#7c9cff"/><stop offset="1" stop-color="${r.score >= 62 ? '#3ddc84' : '#ff5c7a'}"/>
</linearGradient></defs>
</svg>
<div class="grade"><b>${r.score}</b><span>of 100</span></div>
</div>
<div class="hero-meta">
<h2>Grade ${r.grade} · ${r.gaps.length} gap${r.gaps.length === 1 ? '' : 's'} found</h2>
<ul>${gaps || '<li>No gaps — this site is agent-native.</li>'}</ul>
</div>
</div>
${r.catchAll ? `<p class="note">Every unknown path on this site returns HTTP ${r.catchAll.status}${r.catchAll.html ? ' HTML' : ''} (catch-all). Files that match that page are reported as soft-404s, not as present.</p>` : ''}
<table>
<thead><tr><th></th><th>Check</th><th class="w">Pts</th><th>Evidence</th></tr></thead>
<tbody>${rows}</tbody>
</table>
<div class="cta">
<h3>What this means</h3>
<p>Agents (ChatGPT, Claude, Perplexity, Gemini) increasingly read sites through machine-readable files before they read HTML. A site without <code>llms.txt</code>, AI crawler rules, structured data and an API surface is invisible, misquoted, or unreachable to them — and increasingly to the buyers using them.</p>
</div>
<div class="foot">Generated by agentready · ${r.checks.length} checks, ${r.totalWeight} weighted points, zero dependencies</div>
</div></body></html>`;
}
/**
* Branded report — restrained typographic layout (score/grade first, honest evidence,
* whitespace, no decorative chrome). Agency brand reads; tool branding stays optional.
*/
function htmlReportBranded(r, top, b) {
const c = b.colors;
const title = escHtml(b.reportTitle);
const foot = footerText(b, r.host, { checks: r.checks?.length, totalWeight: r.totalWeight });
const footOut = escHtml(foot || (b.name ? `Prepared by ${b.name}` : '')).replace(/\u00a0/g, ' ');
const logo = b.logoDataUri
? `<img class="brand-mark" src="${b.logoDataUri}" alt="${escHtml(b.name)}" />`
: (b.name ? `<div class="brand-mark-text">${escHtml(b.name)}</div>` : '');
const metaBits = [
b.url ? `<a href="${escHtml(b.url)}">${escHtml(b.url.replace(/^https?:\/\//, ''))}</a>` : null,
b.contact ? escHtml(b.contact) : null,
].filter(Boolean).join(' · ');
const style = `
:root{--bg:${c.bg};--fg:${c.fg};--mut:${c.muted};--line:${c.line};--good:${c.good};--bad:${c.bad};--acc:${c.accent};--card:${c.card}}
*{box-sizing:border-box}
body{margin:0;background:var(--bg);color:var(--fg);font:16px/1.55 "Helvetica Neue",Helvetica,Arial,sans-serif;padding:48px 28px}
.wrap{max-width:720px;margin:0 auto}
.brand-mark{display:block;max-height:40px;max-width:180px;margin:0 0 28px}
.brand-mark-text{font-size:13px;font-weight:700;letter-spacing:.04em;text-transform:uppercase;margin:0 0 28px;color:var(--fg)}
.meta{color:var(--mut);font-size:13px;margin:0 0 8px}
.meta a{color:var(--mut)}
h1{font-size:22px;font-weight:700;margin:0 0 6px;letter-spacing:-.01em}
.hostline{color:var(--mut);font-size:13px;margin:0 0 36px}
.scoreboard{margin:0 0 40px;padding:0 0 28px;border-bottom:1px solid var(--line)}
.scoreboard .num{font-size:64px;font-weight:700;line-height:1;letter-spacing:-.03em;font-variant-numeric:tabular-nums}
.scoreboard .of{font-size:18px;color:var(--mut);font-weight:400;margin-left:6px}
.scoreboard .plain{margin:12px 0 0;font-size:17px;font-weight:500}
.scoreboard .detail{margin:6px 0 0;color:var(--mut);font-size:14px}
.priority{margin:0 0 36px}
.priority h2{font-size:12px;font-weight:700;letter-spacing:.08em;text-transform:uppercase;color:var(--mut);margin:0 0 12px}
.priority ol{margin:0;padding-left:20px}
.priority li{margin:6px 0;font-size:15px}
.priority .pts{color:var(--mut);font-variant-numeric:tabular-nums;font-size:13px}
.note{margin:0 0 28px;color:var(--mut);font-size:13px}
table{width:100%;border-collapse:collapse;font-size:14px;margin:0 0 36px}
th,td{text-align:left;padding:12px 10px 12px 0;border-bottom:1px solid var(--line);vertical-align:top}
th{font-size:11px;letter-spacing:.08em;text-transform:uppercase;color:var(--mut);font-weight:700;padding-top:0}
td.verdict{width:4.5em;font-weight:700;font-size:12px;letter-spacing:.04em;text-transform:uppercase}
tr.pass td.verdict{color:var(--good)} tr.fail td.verdict{color:var(--bad)}
td.check{font-weight:500}
td.w{width:3em;color:var(--mut);font-variant-numeric:tabular-nums;text-align:right;padding-right:14px}
td.evidence{color:var(--mut);font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;line-height:1.45;word-break:break-word;overflow-wrap:anywhere}
.foot{margin-top:8px;padding-top:20px;border-top:1px solid var(--line);color:var(--mut);font-size:12px}
${printCss(c)}
`.trim();
if (!r.reachable) {
return `<!doctype html><html lang="en"><head><meta charset="utf-8">
<title>${title} — ${escHtml(r.host)} (unreachable)</title>
<style>${style}</style></head>
<body><div class="wrap">
${logo}
<h1>${title}</h1>
<p class="plain"><b>${escHtml(r.host)}</b> could not be reached, so nothing was scored.</p>
<pre class="evidence" style="white-space:pre-wrap;color:var(--mut);font-size:13px">${escHtml(r.unreachableReason)}</pre>
<div class="foot">${footOut || `scanned ${escHtml(r.scannedAt)}`}</div>
</div></body></html>`;
}
const passed = r.checks.filter((x) => x.pass).length;
const failed = r.checks.length - passed;
const plain = r.score >= 90 ? 'Ready for AI agents.'
: r.score >= 62 ? 'Partly ready — a few gaps matter.'
: r.score >= 25 ? 'Not ready — agents will miss or misread this site.'
: 'Largely invisible to AI agents today.';
const rows = r.checks.map((check) => `
<tr class="${check.pass ? 'pass' : 'fail'}">
<td class="verdict">${check.pass ? 'Pass' : 'Fail'}</td>
<td class="check">${escHtml(check.name)}</td>
<td class="w">${check.weight}</td>
<td class="evidence">${escHtml(check.evidence)}</td>
</tr>`).join('');
const priorities = r.gaps.slice(0, top).map((g) =>
`<li>${escHtml(g.name)} <span class="pts">(+${g.weight} pts if fixed)</span></li>`).join('');
return `<!doctype html>
<html lang="en"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>${title} — ${escHtml(r.host)} (${r.score}/100)</title>
<style>
${style}
</style></head>
<body><div class="wrap">
${logo}
${metaBits ? `<div class="meta">${metaBits}</div>` : ''}
<h1>${title}</h1>
<p class="hostline">${escHtml(r.target)} · scanned ${escHtml(r.scannedAt)} · ${r.homeMs} ms / ${r.homeBytes} bytes</p>
<section class="scoreboard">
<div><span class="num">${r.score}</span><span class="of">/ 100 · grade ${escHtml(r.grade)}</span></div>
<p class="plain">${plain}</p>
<p class="detail">${passed} of ${r.checks.length} checks passed · ${failed} failed · ${r.earned} of ${r.totalWeight} weighted points</p>
</section>
<section class="priority">
<h2>Highest-value fixes</h2>
${priorities ? `<ol>${priorities}</ol>` : '<p class="detail">No gaps — every check passed.</p>'}
</section>
${r.catchAll ? `<p class="note">Every unknown path returns HTTP ${r.catchAll.status}${r.catchAll.html ? ' HTML' : ''} (catch-all). Matching bodies are soft-404s, not present files.</p>` : ''}
<table>
<thead><tr><th>Verdict</th><th>Check</th><th class="w">Pts</th><th>Evidence</th></tr></thead>
<tbody>${rows}</tbody>
</table>
<div class="foot">${footOut}</div>
</div></body></html>`;
}
function htmlReport(r, top, b = brand) {
return b.fromFile ? htmlReportBranded(r, top, b) : htmlReportDefault(r, top);
}
// ---- CLI ----
const outDir = flag('--out', join(process.cwd(), 'agentready-out'));
const top = Math.max(1, Number(flag('--top', Infinity)) || Infinity);
const result = await scan(target);
if (args.includes('--json')) {
console.log(JSON.stringify(result, null, 2));
} else {
console.log(textReport(result, top));
}
mkdirSync(outDir, { recursive: true });
const safe = result.host.replace(/[^a-z0-9.-]/gi, '_');
const htmlPath = join(outDir, `${safe}.html`);
const jsonPath = join(outDir, `${safe}.json`);
writeFileSync(jsonPath, JSON.stringify(result, null, 2));
writeFileSync(htmlPath, htmlReport(result, top));
console.error(` wrote ${htmlPath}`);
console.error(` wrote ${jsonPath}`);
await maybeWritePdf({ wantPdf, chromeFlag, htmlPath, pdfPath: join(outDir, `${safe}.pdf`), label: 'The HTML report' });
process.exit(result.reachable ? 0 : 2);