feat(linkedin-studio): RE-R3e — brief history + day-over-day diff (surfaced: frontmatter + Nytt siden sist) [skip-docs]

Closes hull #7 ("ingen brief-historikk"). Each morning brief now records the
trend ids it showed into its own YAML frontmatter (surfaced: <id-csv> =
surfacedIds(ranking)) and renders a day-over-day diff against the most recent
prior brief — a "## Nytt siden sist (<prior-date>)" section that leads the
ranked list, plus a " N nye siden sist." marker on the one-line summary the
SessionStart hook surfaces (no hook change).

- brief.ts: BRIEF_SCHEMA_VERSION 1->2 (artifact frontmatter gained surfaced:;
  the store's SCHEMA_VERSION stays 4 — no store field). Three PURE helpers
  (diffSurfaced / parseSurfacedFrontmatter / selectPriorBriefFile) + the
  surfaced: emit + the section + the summary marker. No fs/clock in brief.ts.
- cli.ts: the brief handler discovers the prior dated file (existsSync-guarded
  readdirSync -> selectPriorBriefFile, strict < today so a same-day re-run is
  byte-identical), parses its surfaced: line, computes the diff, threads it into
  renderBrief AND the shared briefSummary(ranking, diff) (one-source: file
  frontmatter == --json summary, cli.test one-source invariant). --json gains a
  diff:{priorDate,added,carried,dropped} counts object; the console line appends
  the delta. Any fs error degrades to the empty-prior (first-brief) path.

TDD two-phase: stubs -> 17 value-RED (no module-not-found) -> GREEN. Trends suite
216 -> 245 (brief +27, cli +2), 0 fail. New unconditional gate Section 16n (6
checks); ASSERT_BASELINE_FLOOR 117 -> 123; TRENDS_TESTS_FLOOR -> 245. Full gate
FAIL=0; hook suite 139/139 + R3c schedule/run-daily green untouched. Behavioural:
real two-day rename-real-write diff + same-day byte-identity confirmed. Counts
29/19/27 unchanged; no version bump (additive, v0.5.2 dev).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011vmzxpsFpc8q19LaogAWLD
This commit is contained in:
Kjell Tore Guttormsen 2026-06-26 14:40:09 +02:00
commit 5b51b4baeb
7 changed files with 462 additions and 18 deletions

View file

@ -2,7 +2,7 @@ import { describe, test } from "node:test";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { mkdtempSync, rmSync, readFileSync, existsSync, writeFileSync } from "node:fs";
import { mkdtempSync, rmSync, readFileSync, existsSync, writeFileSync, renameSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
@ -669,3 +669,74 @@ describe("trends CLI — brief temporal flags (RE-R3d / SC6)", () => {
}
});
});
describe("trends CLI — brief history + day-over-day diff (RE-R3e / SC9)", () => {
const fixture = () => {
const dir = mkdtempSync(join(tmpdir(), "trends-r3e-"));
return { dir, store: join(dir, "trends.json"), out: join(dir, "briefs") };
};
// freshIso keeps the seeds inside the freshness window regardless of when the test runs.
const fresh = new Date(Date.now() - 2 * 86400000).toISOString().slice(0, 10);
const seedOnPillar = (store: string, title: string, url: string): void => {
const batch = JSON.stringify([
{
source: "tavily",
title,
url,
topics: ["ai", "gov"],
publishedAt: fresh,
score: { mode: "kortform", dimensions: { pillar: 9, audience: 8, timing: 9, angle: 7, authority: 6 } },
},
]);
run(["capture", "--store", store], batch);
};
const briefJson = (store: string, out: string) =>
JSON.parse(run(["brief", "--pillars", "ai,gov", "--out", out, "--store", store, "--json"], "").stdout);
test("two-day (rename-real-write): day-2 shows the new trend; --json carries the diff", () => {
const { dir, store, out } = fixture();
try {
// Day 1 — two on-pillar trends. The brief records its surfaced cohort + a null-prior diff.
seedOnPillar(store, "Alpha", "https://e/a");
seedOnPillar(store, "Beta", "https://e/b");
const o1 = briefJson(store, out);
assert.equal(o1.diff.priorDate, null, "first brief has no prior (empty dir)");
assert.match(readFileSync(o1.path, "utf8"), /^surfaced: .+/m, "day-1 records a non-empty surfaced: line");
// Rename the REAL day-1 brief to a fixed past date — its surfaced: ids are genuine store
// ids, so the day-2 diff's carried/added are id-exact, clock-free (no today() advance).
const prior = "2026-06-20";
renameSync(o1.path, join(out, `${prior}.md`));
// Day 2 — capture one NEW on-pillar trend, re-run. It is the sole "added".
seedOnPillar(store, "Gamma", "https://e/g");
const o2 = briefJson(store, out);
assert.equal(o2.diff.priorDate, prior, "day-2 discovers the renamed strict-prior");
assert.ok(o2.diff.added >= 1, "at least the new trend is added");
assert.equal(o2.diff.carried, 2, "the two day-1 trends carry over");
assert.equal(o2.diff.dropped, 0, "nothing dropped");
const md2 = readFileSync(o2.path, "utf8");
assert.ok(md2.includes(`## 🆕 Nytt siden sist (${prior})`), "the diff section names the prior date");
assert.ok(md2.includes("Gamma"), "the new trend's title is in the Nytt-siden-sist section");
// The non-JSON console line appends the delta marker.
const console2 = run(["brief", "--pillars", "ai,gov", "--out", out, "--store", store], "").stdout;
assert.ok(console2.includes("nye siden sist"), `console line carries the delta: ${console2}`);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("a first run (empty dir) → diff.priorDate === null, no Nytt-siden-sist date", () => {
const { dir, store, out } = fixture();
try {
seedOnPillar(store, "Solo", "https://e/s");
const o = briefJson(store, out);
assert.equal(o.diff.priorDate, null);
const md = readFileSync(o.path, "utf8");
assert.ok(md.includes("Første brief"), "a first brief says 'Første brief'");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});