prompt-audit-manifest.ts
114 lines · scripts/prompt-audit-manifest.ts at 3f79d81
1#!/usr/bin/env -S bun --no-env-file --config=/dev/null
2/**
3 * Prompt-audit manifest: the files a contributor feeds to Anthropic's
4 * `/claude-api prompt-audit` (a Claude Code skill, run in your own Claude Code
5 * session) at each frontier-model release, cut into slices small enough for
6 * one audit pass each. Procedure: CONTRIBUTING.md "Prompt audit at each
7 * frontier-model release".
8 *
9 * Slices, in order: shared instructions every skill reads (CLAUDE.md, model
10 * overlays, preamble resolvers), the other resolvers, the skill templates
11 * (a skill's SKILL.md.tmpl and sections stay in one slice), the code that
12 * builds model requests, and the tests that pin skill wording (they read a
13 * template, skill or resolver and assert a literal of 40+ characters).
14 *
15 * Usage: bun run audit:manifest [--json] [--budget-kb <n>]
16 */
17import { readdirSync, readFileSync, statSync } from 'node:fs';
18import { join, relative, resolve } from 'node:path';
20const ROOT = resolve(import.meta.dir, '..');
21const DEFAULT_BUDGET_KB = 200;
23export interface AuditSlice { id: string; group: string; files: string[]; bytes: number }
25function walk(dir: string, keep: (rel: string) => boolean): string[] {
26 const out: string[] = [];
27 const visit = (d: string) => {
28 for (const entry of readdirSync(d, { withFileTypes: true })) {
29 const p = join(d, entry.name);
30 const rel = relative(ROOT, p).replaceAll('\\', '/');
31 if (entry.isDirectory()) {
32 if (!entry.name.startsWith('.') && entry.name !== 'node_modules') visit(p);
33 } else if (keep(rel)) {
34 out.push(rel);
35 }
36 }
37 };
38 visit(dir);
39 return out.sort();
40}
42const size = (rel: string) => statSync(join(ROOT, rel)).size;
43const read = (rel: string) => readFileSync(join(ROOT, rel), 'utf8');
45/** Pack units (each a list of files that must stay together) into slices under the budget. */
46function pack(group: string, units: string[][], budget: number): Omit<AuditSlice, 'id'>[] {
47 const slices: Omit<AuditSlice, 'id'>[] = [];
48 let current: Omit<AuditSlice, 'id'> | null = null;
49 for (const unit of units) {
50 const bytes = unit.reduce((sum, f) => sum + size(f), 0);
51 if (!current || (current.bytes > 0 && current.bytes + bytes > budget)) {
52 current = { group, files: [], bytes: 0 };
53 slices.push(current);
54 }
55 current.files.push(...unit);
56 current.bytes += bytes;
57 }
58 return slices;
59}
61export function buildManifest(budgetKb = DEFAULT_BUDGET_KB): AuditSlice[] {
62 const budget = budgetKb * 1024;
63 const preamble = walk(join(ROOT, 'scripts/resolvers/preamble'), rel => rel.endsWith('.ts'));
64 const shared = ['CLAUDE.md', ...walk(join(ROOT, 'model-overlays'), rel => rel.endsWith('.md')), ...preamble];
65 const resolvers = walk(join(ROOT, 'scripts/resolvers'), rel => rel.endsWith('.ts') && !preamble.includes(rel));
67 const templates = walk(ROOT, rel => rel.endsWith('.tmpl') && !rel.startsWith('test/'));
68 const bySkill = new Map<string, string[]>();
69 for (const rel of templates) {
70 const skill = rel.includes('/') ? rel.split('/')[0] : '(root)';
71 bySkill.set(skill, [...(bySkill.get(skill) ?? []), rel]);
72 }
74 const requestCode = [...walk(join(ROOT, 'lib'), rel => rel.endsWith('.ts')),
75 ...walk(join(ROOT, 'design/src'), rel => rel.endsWith('.ts')),
76 ...walk(join(ROOT, 'test/helpers'), rel => rel.endsWith('.ts'))]
77 .filter(rel => /api\.anthropic\.com|api\.openai\.com|\/v1\/messages|messages\.create|responses\.create/.test(read(rel)));
79 const pinningTests = walk(join(ROOT, 'test'), rel => /^test\/[^/]+\.test\.ts$/.test(rel))
80 .filter(rel => {
81 const src = read(rel);
82 return /SKILL\.md|\.md\.tmpl|scripts\/resolvers|model-overlays/.test(src) && /\.(toContain|toMatch)\((['"`])[^'"`]{40,}/.test(src);
83 });
85 const slices = [
86 ...pack('shared instructions', [shared], Infinity),
87 ...pack('resolvers', resolvers.map(f => [f]), budget),
88 ...pack('skill templates', [...bySkill.values()], budget),
89 ...pack('model-request code', requestCode.map(f => [f]), budget),
90 ...pack('tests that pin skill wording', pinningTests.map(f => [f]), budget),
91 ];
92 return slices.map((slice, i) => ({ id: `s${String(i + 1).padStart(2, '0')}`, ...slice }));
93}
95if (import.meta.main) {
96 const args = process.argv.slice(2);
97 const budgetArg = args.indexOf('--budget-kb');
98 const budgetKb = budgetArg >= 0 ? Number(args[budgetArg + 1]) : DEFAULT_BUDGET_KB;
99 if (!Number.isFinite(budgetKb) || budgetKb <= 0) {
100 console.error('--budget-kb needs a positive number of kilobytes');
101 process.exit(2);
102 }
103 const manifest = buildManifest(budgetKb);
104 if (args.includes('--json')) {
105 console.log(JSON.stringify(manifest, null, 2));
106 } else {
107 const files = manifest.reduce((n, s) => n + s.files.length, 0);
108 console.log(`Prompt-audit manifest: ${manifest.length} slices, ${files} files. Run /claude-api prompt-audit in Claude Code on one slice at a time (CONTRIBUTING.md).`);
109 for (const slice of manifest) {
110 console.log(`\n## ${slice.id} ${slice.group} (${slice.files.length} files, ${Math.round(slice.bytes / 1024)} KB)`);
111 for (const file of slice.files) console.log(file);
112 }
113 }
114}