@rebilder/agent-readability 0.1.0

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.
@@ -0,0 +1,345 @@
1
+ /**
2
+ * types.ts — the public contract of the Agent Readability Score, ARS 0.1.
3
+ *
4
+ * Transcribed from `docs/design/universal-gateway-and-public-surfaces.md` §3.9,
5
+ * which is the decided contract. Where §3.9 abbreviates (it writes `id: string`
6
+ * for a check id whose format it then demonstrates), this file fills the gap
7
+ * consistently with the §3.4 dimension table and the §3.6 scoring tables. Two
8
+ * deliberate narrowings and one addition are marked NARROWED / ADDED below;
9
+ * nothing else departs from the design.
10
+ *
11
+ * WHY THE TYPES MATTER MORE THAN USUAL HERE. ARS is published as a standard
12
+ * (`docs/spec/ars-0.1.md`, rendered at `/spec/ars`), and its determinism
13
+ * guarantee is stated as: *for a given `evidenceHash`, `rulesetHash` and
14
+ * `corpusHash`, `score()` returns a byte-identical `ArsResult` in any conformant
15
+ * implementation, in any language.* A second implementation in Go or Python
16
+ * reproduces `expected.json` from the conformance corpus, and these types are
17
+ * the schema of that file. A field added casually here is a field an outside
18
+ * team has to reverse-engineer.
19
+ *
20
+ * SHAPE OF THE SYSTEM. The impure half (`./probe`) produces exactly one thing:
21
+ * an `ArsEvidence` bundle. The pure half (`.`) consumes exactly that, plus an
22
+ * `ArsRuleset`, and produces an `ArsResult`. There is no third input — no
23
+ * clock, no network, no randomness, no LLM. The ESLint rule in
24
+ * `eslint.config.mjs` enforces that at build time.
25
+ */
26
+ /** The spec version this package implements. See §3.1 for the versioning rules. */
27
+ declare const ARS_SPEC_VERSION: "0.1.0";
28
+ /**
29
+ * Where the scan was taken from.
30
+ * - `public` — an anonymous third-party scan. The only vantage that is robots-gated.
31
+ * - `authenticated` — a signed-in Rebilder user scanning a domain they have verified.
32
+ * - `self` — the site's own operator (Console, or the CLI run against their own
33
+ * origin). Bypasses the `rebilder-ars` token check, because the owner can
34
+ * consent for their own origin (§3.7).
35
+ */
36
+ type ArsVantage = 'public' | 'authenticated' | 'self';
37
+ /**
38
+ * Whether a check's earned points come from an observation or from an inference.
39
+ * Published per check, and the total heuristic weight is capped at 40 of 100
40
+ * (36 in 0.1) and printed. See §3.4.
41
+ */
42
+ type ArsBasis = 'measured' | 'heuristic';
43
+ type ArsGrade = 'A' | 'B' | 'C' | 'D' | 'F';
44
+ /**
45
+ * The three crawler audiences (§3.7). `training` is strictly neutral: blocking
46
+ * training crawlers while allowing assistants scores identically to a fully open
47
+ * site (conformance fixtures 021 and 022 exist to prove exactly that).
48
+ */
49
+ type ArsAudience = 'assistant' | 'training' | 'search';
50
+ type ArsUnscoredReason = 'blocked-at-edge' | 'unreachable' | 'non-2xx' | 'too-many-redirects' | 'robots-disallow-scanner' | 'robots-unavailable' | 'truncated-evidence' | 'evidence-incomplete';
51
+ /**
52
+ * The three outcomes. Two of them carry no letter grade at all: a deliberate
53
+ * opt-out is a choice, not a failure, and an unscored target is a missing
54
+ * observation, not a bad one. Neither is ever ranked or listed (§3.6).
55
+ */
56
+ type ArsOutcome = {
57
+ kind: 'scored';
58
+ grade: ArsGrade;
59
+ score: number;
60
+ } | {
61
+ kind: 'opt-out';
62
+ audience: ArsAudience;
63
+ wellFormed: true;
64
+ } | {
65
+ kind: 'unscored';
66
+ reason: ArsUnscoredReason;
67
+ detail?: string;
68
+ };
69
+ interface ArsHttpCapture {
70
+ requestedUrl: string;
71
+ finalUrl: string;
72
+ redirects: {
73
+ status: number;
74
+ location: string;
75
+ }[];
76
+ status: number;
77
+ /**
78
+ * Lowercased header names → all values. An array, not a string, because
79
+ * `Link` is routinely sent multiple times and is scored (D2.2, D6.5).
80
+ * Volatile headers (`date`, `age`, `set-cookie`, `x-request-id`, `cf-ray`,
81
+ * `report-to`) are excluded before hashing — without that exclusion the
82
+ * evidence hash changes on every scan of every real site and replay dies.
83
+ */
84
+ headers: Record<string, string[]>;
85
+ /**
86
+ * Decoded byte length. NORMATIVE MEASUREMENT — bodies are decoded per the
87
+ * `Content-Type` charset (default UTF-8) and this is the UTF-8 byte length of
88
+ * the result, so a Go implementation reproduces D3.2 and D4 offsets exactly.
89
+ */
90
+ bytes: number;
91
+ /** SHA-256 of the raw decoded body, hex. Published; the body is not. */
92
+ bodySha256: string;
93
+ /** Present in-process and for a verified owner. NEVER in a published bundle. */
94
+ body?: string;
95
+ /**
96
+ * True when the 2 MiB body cap was hit. Surfaced in the UI and the byte figure
97
+ * rendered as "≥ N bytes": silently scoring a truncated body makes the worst
98
+ * sites score best on the one number the product is built on (§3.3).
99
+ */
100
+ truncated: boolean;
101
+ }
102
+ type ArsProbeError = 'timeout' | 'unreachable' | 'non-2xx' | 'blocked-redirect' | 'too-many-redirects' | 'challenge' | 'policy-rejected';
103
+ interface ArsProbeRecord {
104
+ requestHeaders: Record<string, string>;
105
+ result: {
106
+ ok: true;
107
+ capture: ArsHttpCapture;
108
+ } | {
109
+ ok: false;
110
+ error: ArsProbeError;
111
+ detail?: string;
112
+ };
113
+ }
114
+ interface ArsEvidence {
115
+ evidenceVersion: '0.1.0';
116
+ target: {
117
+ url: string;
118
+ origin: string;
119
+ };
120
+ /**
121
+ * `agent` and `browser` differ ONLY in the `Accept` header — same User-Agent,
122
+ * same everything else. That is what makes the D2.4 parity check meaningful
123
+ * and what stops a correctly installed gateway (which negotiates on `Accept`,
124
+ * not on UA) from being scored as a cloaker (§3.3).
125
+ */
126
+ probes: {
127
+ agent: ArsProbeRecord;
128
+ browser: ArsProbeRecord;
129
+ /** Third probe, ≥30s later, taken only when divergence was detected (§3.8). */
130
+ parityConfirm: ArsProbeRecord | null;
131
+ robotsTxt: ArsProbeRecord | null;
132
+ llmsTxt: ArsProbeRecord | null;
133
+ wellKnownUcp: ArsProbeRecord | null;
134
+ };
135
+ vantage: ArsVantage;
136
+ /** Wall clock. EXCLUDED from evidenceHash — never an input to scoring. */
137
+ capturedAt: string;
138
+ }
139
+ /**
140
+ * A closed set of 8. Every kind has exactly one complete profile (see
141
+ * `./profiles`); a property test asserts the mapping is total. `unknown` is not
142
+ * an easy exit — it still scores D5.2 against "any recognised schema.org type
143
+ * present", so hiding your page kind costs points and gains nothing (§3.5).
144
+ */
145
+ type ArsPageKind = 'product' | 'collection' | 'article' | 'place' | 'service' | 'faq' | 'document' | 'unknown';
146
+ /**
147
+ * Every fact name any profile may reference. Conformance fixture `058` asserts
148
+ * that every name in every profile is a member of this union — the union is the
149
+ * vocabulary, and a profile naming a fact outside it is a spec bug.
150
+ */
151
+ type ArsFactKind = 'title' | 'description' | 'updated' | 'published' | 'price' | 'currency' | 'availability' | 'brand' | 'sku' | 'shipping' | 'returns' | 'org-name' | 'address' | 'hours' | 'phone' | 'email' | 'service-area' | 'author' | 'section' | 'authority' | 'primary-action-url' | 'eligibility' | 'duration' | 'question-answer' | 'item-count' | 'item-link';
152
+ interface ArsFactProfile {
153
+ pageKind: ArsPageKind;
154
+ core: readonly ArsFactKind[];
155
+ extended: readonly ArsFactKind[];
156
+ /**
157
+ * Byte reference for D3.2, in bytes. Varies by kind; weights never do. This
158
+ * is the mechanism that stops long-form content (articles, docs) being
159
+ * structurally capped around C — the defect in every design that used
160
+ * absolute byte bands (§3.5).
161
+ */
162
+ byteReference: number;
163
+ }
164
+ interface ArsFactObservation {
165
+ kind: ArsFactKind;
166
+ /** Normalised (money → minor units + ISO currency; dates → ISO 8601). */
167
+ normalized: string;
168
+ source: 'json-ld' | 'microdata' | 'meta' | 'html-text' | 'negotiated';
169
+ /**
170
+ * UTF-8 byte offset of the first occurrence in the decoded body. A measured
171
+ * offset of a heuristic fact — the offset itself is not a guess, which is why
172
+ * D4 reports it as evidence even though D4's checks are heuristic.
173
+ */
174
+ offset: number;
175
+ /** Observed in ≥2 sources (e.g. JSON-LD and visible text). §3.6. */
176
+ corroborated: boolean;
177
+ }
178
+ type ArsDimensionId = 'retrievability' | 'machine-representation' | 'fact-coverage' | 'fact-position' | 'structured-data' | 'contract-discovery';
179
+ /**
180
+ * Every check id in ARS 0.1, in dimension order, matching the §3.4 table row for
181
+ * row. The `<dimension>.<check>` shape is normative: the prefix IS the
182
+ * `ArsDimensionId`, so a check's dimension is derivable from its id and the two
183
+ * can never disagree. §3.9 demonstrates the format with
184
+ * `'retrievability.render-independence'` and leaves the rest implied; this is
185
+ * that list, written out.
186
+ *
187
+ * NARROWED vs §3.9: `ArsCheck['id']` is this union rather than `string`. These
188
+ * ids are published in the spec and appear in every `expected.json`; an
189
+ * unconstrained string in a frozen contract is a typo waiting to ship.
190
+ */
191
+ type ArsCheckId = 'retrievability.reachable' | 'retrievability.robots-policy' | 'retrievability.render-independence' | 'machine-representation.negotiated-response' | 'machine-representation.declared-alternates' | 'machine-representation.vary-accept' | 'machine-representation.substance-parity' | 'fact-coverage.core-facts' | 'fact-coverage.context-cost' | 'fact-position.first-core-fact-offset' | 'fact-position.front-window' | 'structured-data.present' | 'structured-data.required-properties' | 'structured-data.text-agreement' | 'contract-discovery.canonical' | 'contract-discovery.cache-validators' | 'contract-discovery.llms-txt' | 'contract-discovery.sitemap' | 'contract-discovery.machine-endpoint';
192
+ interface ArsCheckEvidence {
193
+ label: string;
194
+ value: string;
195
+ basis: ArsBasis;
196
+ }
197
+ interface ArsCheck {
198
+ /** e.g. 'retrievability.render-independence'. */
199
+ id: ArsCheckId;
200
+ label: string;
201
+ basis: ArsBasis;
202
+ weight: number;
203
+ /** Integer, 0..weight. No floating point anywhere in the scoring path (§3.6). */
204
+ earned: number;
205
+ evidence: ArsCheckEvidence[];
206
+ remedy?: string;
207
+ }
208
+ interface ArsDimension {
209
+ id: ArsDimensionId;
210
+ label: string;
211
+ weight: number;
212
+ earned: number;
213
+ /** 'heuristic' if ANY constituent check is heuristic. */
214
+ basis: ArsBasis;
215
+ checks: ArsCheck[];
216
+ }
217
+ type ArsFlagId = 'substance-divergence' | 'structured-data-divergence' | 'robots-contradiction' | 'paywalled' | 'render-dependent' | 'scanner-blocked' | 'training-opt-out' | 'assistant-opt-out' | 'vary-missing' | 'canonical-mismatch' | 'body-truncated' | 'vantage-variance';
218
+ interface ArsFlag {
219
+ id: ArsFlagId;
220
+ /**
221
+ * No 'critical'. No check publicly accuses anyone: a divergence flag reports
222
+ * *"the agent representation reported price X; the HTML representation
223
+ * reported price Y at capture time"*, which is an observation, not a charge of
224
+ * cloaking (§3.8).
225
+ */
226
+ severity: 'info' | 'warn';
227
+ basis: ArsBasis;
228
+ message: string;
229
+ evidence: ArsCheckEvidence[];
230
+ }
231
+ interface ArsAudienceDecision {
232
+ decision: 'allow' | 'disallow' | 'unspecified';
233
+ matchedGroup: string | null;
234
+ matchedRule: string | null;
235
+ }
236
+ interface ArsPolicyReport {
237
+ robotsTxtStatus: 'ok' | 'missing' | 'error' | 'unparseable';
238
+ audiences: Record<ArsAudience, ArsAudienceDecision>;
239
+ deliberateOptOut: boolean;
240
+ /** NEUTRAL. Never lowers the score. */
241
+ trainingOptOut: boolean;
242
+ sitemapDeclared: boolean;
243
+ }
244
+ interface ArsCostReport {
245
+ htmlBytes: number;
246
+ negotiatedBytes: number | null;
247
+ /** HEURISTIC (chars/4). Always rendered with ≈ and an "est." chip. */
248
+ approxHtmlTokens: number;
249
+ approxNegotiatedTokens: number | null;
250
+ reductionRatio: number | null;
251
+ firstCoreFactOffset: number | null;
252
+ truncated: boolean;
253
+ }
254
+ interface ArsRecommendation {
255
+ id: string;
256
+ title: string;
257
+ /**
258
+ * Points recoverable, after dependency dedup: a recommendation counts only
259
+ * checks not claimed by a higher-ranked recommendation, plus the checks it
260
+ * uniquely unlocks. Sums per dimension are capped at the dimension weight, and
261
+ * a property test asserts `Σ pointsAvailable ≤ 100 − score` (§3.9). Merchants
262
+ * act on this surface, so the arithmetic is specified rather than implied.
263
+ */
264
+ pointsAvailable: number;
265
+ effort: 'config' | 'template' | 'engineering';
266
+ detail: string;
267
+ /** Check ids this closes. */
268
+ checks: ArsCheckId[];
269
+ /** Check ids it unlocks (e.g. D2.1 unlocks D2.3, which is gated on it). */
270
+ unlocks: ArsCheckId[];
271
+ }
272
+ interface ArsResult {
273
+ spec: 'ars';
274
+ specVersion: string;
275
+ /** SHA-256 of RFC 8785 canonical JSON of the frozen ruleset. */
276
+ rulesetHash: string;
277
+ /** SHA-256 over the conformance corpus; pins observable behaviour. */
278
+ corpusHash: string;
279
+ evidenceHash: string;
280
+ vantage: ArsVantage;
281
+ target: {
282
+ url: string;
283
+ finalUrl: string;
284
+ origin: string;
285
+ };
286
+ outcome: ArsOutcome;
287
+ score: number | null;
288
+ grade: ArsGrade | null;
289
+ bandLabel: string | null;
290
+ pageKind: ArsPageKind;
291
+ pageKindBasis: ArsBasis;
292
+ pageKindConfidence: 'high' | 'medium' | 'low';
293
+ factProfile: ArsFactProfile;
294
+ facts: ArsFactObservation[];
295
+ /** Always 6 entries, weights always summing to 100. */
296
+ dimensions: ArsDimension[];
297
+ flags: ArsFlag[];
298
+ policy: ArsPolicyReport;
299
+ cost: ArsCostReport;
300
+ recommendations: ArsRecommendation[];
301
+ measuredWeight: number;
302
+ heuristicWeight: number;
303
+ }
304
+ interface ArsBand {
305
+ grade: ArsGrade;
306
+ min: number;
307
+ max: number;
308
+ label: string;
309
+ /** Descriptive only. Never predictive of third-party AI behaviour (§3.6). */
310
+ meaning: string;
311
+ }
312
+ /**
313
+ * The frozen, hashed ruleset. Everything a second implementation needs to
314
+ * reproduce a score, and nothing that varies between runs.
315
+ *
316
+ * ADDED vs §3.9: `timeoutMs`. §3.9's listing stops at `maxRedirects`, but §5.2
317
+ * pins the probe timeout at 5 000ms in the same breath as the body cap and the
318
+ * redirect limit, and the same argument applies to all three — two
319
+ * implementations with different timeouts disagree about which pages are
320
+ * scoreable at all, which is a score difference wearing a disguise.
321
+ */
322
+ interface ArsRuleset {
323
+ version: string;
324
+ /** Check id → points. Must sum to exactly 100. Frozen per version. */
325
+ weights: Readonly<Record<string, number>>;
326
+ /**
327
+ * The §3.6 numeric bands, as flat integer arrays. Each key documents its own
328
+ * encoding in `./ruleset`; all of them are `[threshold, value]` pairs so that a
329
+ * second implementation reads one shape, not five.
330
+ */
331
+ thresholds: Readonly<Record<string, readonly number[]>>;
332
+ /** Robots `User-agent:` tokens per audience. Matched case-insensitively. */
333
+ audienceTokens: Readonly<Record<ArsAudience, readonly string[]>>;
334
+ /** Hand-curated required properties per page kind. Provenance in the spec. */
335
+ requiredProperties: Readonly<Record<ArsPageKind, readonly string[]>>;
336
+ /** Anchor/action phrases that qualify a link as `primary-action-url` (§3.5). */
337
+ actionLexicon: readonly string[];
338
+ maxBodyBytes: number;
339
+ maxRedirects: number;
340
+ timeoutMs: number;
341
+ profiles: readonly ArsFactProfile[];
342
+ bands: readonly ArsBand[];
343
+ }
344
+
345
+ export { type ArsDimensionId as A, type ArsUnscoredReason as B, type ArsVantage as C, type ArsBasis as a, type ArsFactProfile as b, type ArsRuleset as c, type ArsBand as d, type ArsEvidence as e, type ArsResult as f, type ArsRecommendation as g, type ArsPageKind as h, ARS_SPEC_VERSION as i, type ArsAudience as j, type ArsAudienceDecision as k, type ArsCheck as l, type ArsCheckEvidence as m, type ArsCheckId as n, type ArsCostReport as o, type ArsDimension as p, type ArsFactKind as q, type ArsFactObservation as r, type ArsFlag as s, type ArsFlagId as t, type ArsGrade as u, type ArsHttpCapture as v, type ArsOutcome as w, type ArsPolicyReport as x, type ArsProbeError as y, type ArsProbeRecord as z };