feat(linkedin-studio): N11 — serie-destillat + skjelett-sjekk + specifics-bank bruks-logging [skip-docs]

Every long-form gate agent sees ONE edition, so «the reader has heard this
before» was structurally invisible — and the specifics-bank dedupe actively
encouraged re-surfacing the same material. At series cadence that is the
fastest-growing defect class. N11 makes it visible BEFORE the skeleton is
approved, at two grains:

- Series grain — new scripts/editions package: each locked edition's spent
  anecdotes/arguments/hooks are written to <serie>/linkedin/series-distillate.json
  at Step 8 lock (distil-append), and the next skeleton is checked against it at
  Step 2.5 (distil-check) with the finding folded into the annotation gate the
  operator already reads. Advisory, never blocking: a deliberate callback is a
  legitimate move, an unnoticed retread is not.
- Material grain — specifics-bank usedIn log: record-usage stamps «used in
  edition NN» on the specifics an edition actually consumed (read from the bound
  slot-map, so abstrakt/ekstern stamp nothing). At lock, so it means published;
  idempotent under a pivot re-lock. Additive-optional, schema stays v1.

Placement deviates from the plan text (${DATA}) after premise-verification:
per-series state belongs in the series root beside edition-state.json, where
Step 0 already resolves the path and no slug→path map is needed. Operator
approved. The distillate module also lands in scripts/editions now rather than
at N12, since the AC required a testable roundtrip and N12 planned that package
anyway — N12 extends it instead of creating it.

Similarity is character-trigram Jaccard, not word overlap: the plugin is
language-general and inflection (migrere/migreringen) breaks word tokens.
Calibrated on real paraphrase pairs — retellings 0.44-0.55, unrelated 0.04-0.06,
same-topic-different-story 0.24 — so the default threshold sits in the gap at
0.40. Word-Jaccard scored a shortened hook paraphrase at 0.11.

TDD (Iron Law): tests written first and verified red before implementation, for
both the new package and the bank logging.

Suites: editions 27/0 (new suite line + guard, floor 27) · specifics-bank
28 -> 45 · test-runner 163 -> 173 (Section 16r: 9 unconditional greps +
non-vacuity self-test; anti-erosion floor 146 -> 155) · trends 300/0 ·
brain 134/0 · hooks 140/0 · tests 35/0 · render 60/0. tsc --noEmit clean.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QxvWAjte7vPcF79QeSRvRJ
This commit is contained in:
Kjell Tore Guttormsen 2026-07-25 06:29:01 +02:00
commit b54e450c3e
20 changed files with 2152 additions and 19 deletions

167
scripts/editions/src/cli.ts Normal file
View file

@ -0,0 +1,167 @@
#!/usr/bin/env node
/**
* CLI for the series distillate (N11 serie-nivå-vern).
*
* node --import tsx src/cli.ts distil-append --distillate <path> --series <slug> --extract <extract.json>
* node --import tsx src/cli.ts distil-check --distillate <path> --skeleton <skeleton.json>
* [--threshold <0..1>] [--json]
*
* `distil-append` runs at Step 8 lock: the command layer writes the AI extract
* (the anecdotes/arguments/hooks the edition actually spent) to a JSON file, and
* this folds it in deterministically. `distil-check` runs at Step 2.5, before
* prose, and reports re-use into the annotation gate.
*
* Exit code: 0 on success (INCLUDING a REUSE verdict the check is advisory by
* design, unlike the binding gate's BLOCK), 2 on usage error.
*/
import { readFileSync } from "node:fs";
import {
DEFAULT_THRESHOLD,
appendEntry,
checkSkeleton,
loadDistillate,
saveDistillate,
} from "./distillate.js";
import type { DistillateEntry, SkeletonCandidate } from "./types.js";
function parseFlags(args: string[]): Record<string, string> {
const out: Record<string, string> = {};
for (let i = 0; i < args.length; i++) {
const a = args[i];
if (a.startsWith("--")) {
const key = a.slice(2);
const next = args[i + 1];
if (next === undefined || next.startsWith("--")) {
out[key] = "true";
} else {
out[key] = next;
i++;
}
}
}
return out;
}
function usage(msg: string): never {
console.error(`error: ${msg}`);
console.error(
"usage:\n" +
" distil-append --distillate <path> --series <slug> --extract <extract.json>\n" +
" distil-check --distillate <path> --skeleton <skeleton.json> [--threshold <0..1>] [--json]",
);
process.exit(2);
}
/** A flag that was given a real value (not absent, not a bare boolean flag). */
function value(flags: Record<string, string>, key: string): string | undefined {
const v = flags[key];
return v === undefined || v === "true" ? undefined : v;
}
function readJson(path: string, what: string): unknown {
try {
return JSON.parse(readFileSync(path, "utf8"));
} catch (err) {
usage(`could not read ${what} at ${path}: ${(err as Error).message}`);
}
}
function stringList(raw: unknown): string[] {
if (!Array.isArray(raw)) return [];
return raw.filter((x): x is string => typeof x === "string" && x.trim().length > 0);
}
/** Validate the AI extract at the edge — a malformed extract must not become a silent empty entry. */
function toEntry(raw: unknown): DistillateEntry {
const o = (raw ?? {}) as Record<string, unknown>;
for (const field of ["editionId", "title", "lockedAt"]) {
const v = o[field];
if (typeof v !== "string" || v.trim().length === 0) {
usage(`extract is missing required string field: ${field}`);
}
}
return {
editionId: o.editionId as string,
title: o.title as string,
lockedAt: o.lockedAt as string,
anecdotes: stringList(o.anecdotes),
arguments: stringList(o.arguments),
hooks: stringList(o.hooks),
};
}
function main(): void {
const [command, ...rest] = process.argv.slice(2);
const flags = parseFlags(rest);
const distillatePath = value(flags, "distillate");
const asJson = flags.json === "true";
if (command === "distil-append") {
if (!distillatePath) usage("distil-append needs --distillate <path>");
const series = value(flags, "series");
if (!series) usage("distil-append needs --series <slug>");
const extractPath = value(flags, "extract");
if (!extractPath) usage("distil-append needs --extract <extract.json>");
const entry = toEntry(readJson(extractPath, "extract"));
const res = appendEntry(loadDistillate(distillatePath, series), entry);
saveDistillate(distillatePath, res.distillate);
const units = entry.anecdotes.length + entry.arguments.length + entry.hooks.length;
console.log(
`${res.replaced ? "Replaced" : "Appended"} edition ${entry.editionId} (${units} narrative unit(s)) ` +
`in ${distillatePath}${res.distillate.editions.length} edition(s) in the series distillate`,
);
return;
}
if (command === "distil-check") {
if (!distillatePath) usage("distil-check needs --distillate <path>");
const skeletonPath = value(flags, "skeleton");
if (!skeletonPath) usage("distil-check needs --skeleton <skeleton.json>");
const rawThreshold = value(flags, "threshold");
let threshold = DEFAULT_THRESHOLD;
if (rawThreshold !== undefined) {
threshold = Number(rawThreshold);
if (!Number.isFinite(threshold) || threshold <= 0 || threshold > 1) {
usage("--threshold must be a number in (0, 1]");
}
}
const candidate = (readJson(skeletonPath, "skeleton") ?? {}) as SkeletonCandidate;
const report = checkSkeleton(loadDistillate(distillatePath, value(flags, "series") ?? ""), candidate, {
threshold,
});
if (asJson) {
console.log(JSON.stringify(report, null, 2));
return;
}
if (report.verdict === "CLEAR") {
console.log(
`CLEAR — nothing in this skeleton resembles the ${report.comparedAgainst} locked edition(s) ` +
`(threshold ${report.threshold}).`,
);
return;
}
console.log(
`REUSE — ${report.hits.length} unit(s) resemble earlier editions (threshold ${report.threshold}, ` +
`${report.comparedAgainst} edition(s) checked):`,
);
for (const hit of report.hits) {
console.log(`\n · ${hit.kind}${(hit.score * 100).toFixed(0)}% like edition ${hit.editionId}`);
console.log(` new: ${hit.candidate}`);
console.log(` then: ${hit.matched}`);
}
console.log("\nAdvisory: a deliberate callback is fine — an unnoticed retread is not.");
return;
}
usage(command ? `unknown command: ${command}` : "no command given");
}
main();