railyard/policy/copy-lint.mjs
caelum0x 71336987dd feat(bridge): Scribase bridge creates the bound Railyard org, one-time CLI claims and slot proofs; denylist 2026-09-29.3 (PY1.0)
PY1.0 acceptance (C8 delta check on main f919636; Wave E Y-0 to Y-5 merged):
1. Denylist .3 (gtm/policy, published by this package) adds portfolio-ott-drm
   and portfolio-ott-comparison (DRM, Netflix-style, your own Netflix beyond
   Menivor), addon-unlaunched (ADDON_LIVE), rally-capacity-purchase
   (RALLY_CAPACITY_PURCHASE_LIVE) and at-cost (allowed only next to a live
   price source). Re-vendored byte for byte; every new rule has failing
   fixtures (policy/test/fixtures/denylist-3-new-phrases.json); .2 fixtures kept.
2. apps/console/app/api/v1/bridge/scribase/{orgs,claims,proof}: signed
   (Standard Webhooks, RAILYARD_SCRIBASE_BRIDGE_SECRET), replay-proof calls from
   Scribase control-api. The first call creates the Railyard org bound to the
   Scribase org id (deterministic owner id, no second sign-up) and mirrors the
   add-on into the entitlement (plan "addon"); a lapsed add-on pauses the org
   and keeps its data. Keys go only to the Scribase CLI: a claim is a one-time,
   pre-approved device code (120 s) exchanged at /api/v1/device/token; a grep
   test keeps every bridge handler free of stdout, stderr and log writes.
3. proof answers {slot, state, live, proof:{kind,id,at}, next}; slots whose
   package has not landed answer "arriving".
Contract: tests/contract/fixtures/scribase-bridge/contract.json, run by
tests/contract/scribase-bridge.test.ts; the Scribase carve-out tests the same file.

Migration: apps/console/db/migrations/0002_scribase_bridge.sql (idempotent;
applied twice to a scratch local cluster only by test/migration.test.ts).
Env: RAILYARD_SCRIBASE_BRIDGE_SECRET (secret; unset keeps the bridge off).

Tests: console npm test 48/48, tests/contract 2/2, policy 11/11, cli 9/9, mcp 9/9,
tsc --noEmit clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 18:39:24 +03:00

162 lines
5.3 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copy-denylist matcher for Railyard (gtm/policy/CLAIMS-RULES.md section 4).
//
// Zero dependencies, so the console, the CLI, the MCP server and the skill
// README tests all share one implementation of the normative matching steps:
// normalise, product and surface filter, path allowlist, flags, except tokens,
// match, source escape, severity. The vendored JSON next to this file is never
// edited here; change gtm/policy/copy-denylist.json and re-vendor.
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const HERE = dirname(fileURLToPath(import.meta.url));
export const DENYLIST_PATH = join(HERE, "copy-denylist.json");
export const PINNED_VERSION = "2026-09-29.3";
export const PRODUCT = "railyard";
export function loadDenylist(path = DENYLIST_PATH) {
return JSON.parse(readFileSync(path, "utf8"));
}
export function normalise(text) {
return String(text)
.replace(/[‘’]/g, "'")
.replace(/[“”]/g, '"')
.replace(/ /g, " ")
.replace(/\s+/g, " ")
.trim();
}
function escapeRegExp(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
// Glob with `**` crossing folders and `*` staying in one segment (section 4.3 step 3).
export function globToRegExp(glob) {
let out = "";
for (let i = 0; i < glob.length; i += 1) {
const ch = glob[i];
if (ch === "*" && glob[i + 1] === "*") {
out += ".*";
i += 1;
if (glob[i + 1] === "/") i += 1;
} else if (ch === "*") {
out += "[^/]*";
} else if (ch === "[" || ch === "]") {
out += ch;
} else {
out += escapeRegExp(ch);
}
}
return new RegExp(`^${out}$`);
}
export function compileRules(denylist, { product = PRODUCT, flags = {} } = {}) {
const defaults = denylist.defaults ?? {};
const out = [];
for (const raw of denylist.rules) {
if (!(raw.products ?? []).includes(product)) continue;
const rule = { ...defaults, ...raw };
// Step 4: a rule is off when any of its flags is on in the real config.
const off = (rule.unlessFlags ?? []).some((f) => ["1", "true"].includes(String(flags[f] ?? "")));
if (off) continue;
if (rule.match.type === "regex") {
const cs = rule.match.caseSensitive ?? rule.caseSensitive ?? false;
rule._re = new RegExp(rule.match.value, cs ? "" : "i");
} else if (rule.match.type === "codepoints") {
rule._ranges = rule.match.ranges.map(([lo, hi]) => [parseInt(lo, 16), parseInt(hi, 16)]);
} else {
throw new Error(`unknown match type ${rule.match.type} in ${rule.id}`);
}
rule._allow = (rule.allowPaths ?? []).map(globToRegExp);
out.push(rule);
}
return out;
}
// Steps 1, 5, 6 and 7 for one unit of text.
export function hits(rule, text) {
let unit = normalise(text);
for (const tok of rule.exceptTokens ?? []) {
unit = unit.replace(new RegExp(escapeRegExp(tok), "gi"), " ");
}
let hit;
if (rule._re) {
hit = rule._re.test(unit);
} else {
hit = [...unit].some((ch) => {
const cp = ch.codePointAt(0);
return rule._ranges.some(([lo, hi]) => cp >= lo && cp <= hi);
});
}
if (hit && rule.sourceEscape && /copy-source:\s*\S|data-copy-source="[^"]+"/.test(String(text))) {
return false;
}
return hit;
}
export function selfTest(rules) {
const errors = [];
for (const r of rules) {
for (const ex of r.examples?.hit ?? []) if (!hits(r, ex)) errors.push(`self-test: ${r.id} should hit ${JSON.stringify(ex)}`);
for (const ex of r.examples?.miss ?? []) if (hits(r, ex)) errors.push(`self-test: ${r.id} should miss ${JSON.stringify(ex)}`);
}
return errors;
}
/**
* Lint units of text. Each unit is { text, surface, where, path? }.
* Returns findings [{ id, severity, where, excerpt }]; excerpts are omitted for
* secret-surface rules (section 4.5).
*/
export function lintUnits(units, opts = {}) {
const denylist = opts.denylist ?? loadDenylist();
const rules = opts.rules ?? compileRules(denylist, opts);
const findings = [];
for (const u of units) {
for (const r of rules) {
if (!(r.surfaces ?? []).includes(u.surface)) continue;
if (u.path && r._allow.some((re) => re.test(u.path))) continue;
if (!hits(r, u.text)) continue;
findings.push({
id: r.id,
severity: r.severity ?? "error",
where: u.where ?? u.path ?? "",
excerpt: r.category === "secret-surface" ? "" : normalise(u.text).slice(0, 60),
});
}
}
return findings;
}
// Markdown to units: one line per unit, code fences and inline code skipped
// (commands and identifiers are not copy), link targets dropped.
export function markdownUnits(md, { surface = "docs", where = "md" } = {}) {
const units = [];
let fenced = false;
md.split(/\r?\n/).forEach((line, i) => {
if (/^\s*```/.test(line)) {
fenced = !fenced;
return;
}
if (fenced) return;
const text = line
.replace(/`[^`]*`/g, " ")
.replace(/\]\([^)]*\)/g, "]")
.replace(/<[^>]+>/g, " ");
if (text.trim()) units.push({ text, surface, where: `${where}:${i + 1}`, raw: line });
});
return units;
}
export function errors(findings) {
return findings.filter((f) => f.severity === "error");
}
export function formatFindings(findings) {
return findings
.map((f) => `${f.id} (${f.severity}): ${f.where}${f.excerpt ? `: ${JSON.stringify(f.excerpt)}` : ""}`)
.join("\n");
}