feat(linkedin-studio): RE-R3a — persist relevance score on the store record + rank the morning brief on it [skip-docs]

R3 slice 1 (research-deepening). Stop discarding the relevance judgment the
trend-spotter already computes: persist a 4-field TrendScore {mode, dimensions,
composite, priority} on TrendRecord (schema v2->v3, additive lossless migrate),
computed by the existing score.ts composite()+band() (one owner, no new arithmetic),
threaded item->store; then rankForBrief sorts each bucket composite-first (sentinel
-1 for unscored) and renderBrief surfaces "· <priority> (<mode>)" per body entry
(briefSummary shows the band only). First-sight only; mode-blind ranking with the mode
shown so the operator can disambiguate instruments.

- score.ts: TrendScore + requiredDimensions(mode) (ordered) + scoreEnvelope (composes
  composite+band; throws on bad dim by contract)
- types.ts: SCHEMA_VERSION 2->3; TrendRecord.score?
- store.ts: TrendInput.score?; addTrend persists first-sight (duplicate keeps it);
  migrate comment v1->v2->v3 (logic unchanged, JSON.stringify preserves the field)
- item.ts: TrendItem.score?; normalizeItem validates (non-array score/dimensions + the
  mode's five dims in [1,10]) -> structured error never throw, carries validated dims;
  itemToInput -> scoreEnvelope (no throw on the capture path; direct call throws by contract)
- brief.ts: composite-primary comparator; band+mode render; exact ranking: descriptor
- cli.ts: capture persists score via itemToInput (doc-only); add/score paths unchanged
- agents/trend-spotter.md Step 4.5: capture batch carries the Step-2 dimensions
- gate: TRENDS_TESTS_FLOOR 104->146; new unconditional Section 16j; ASSERT floor 94->99

Tests: trends 146/146 (RED two-phase: logic-RED store/brief/cli; stub-first then
assertion-RED score/item). Gate green (Passed 114 / Failed 0; 113 checks >= 99).
Hook suite 139/139 untouched. Counts 27/19/29 unchanged. No new source file/agent/command.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VmHCQjJHUyWwxGAVVjNLgp
This commit is contained in:
Kjell Tore Guttormsen 2026-06-24 14:05:27 +02:00
commit e169c78710
14 changed files with 829 additions and 40 deletions

View file

@ -275,6 +275,67 @@ describe("trends store", () => {
assert.equal(res2.merged, true, "topic union still reported");
assert.equal("publishedAt" in res2.store.trends[0], false, "no back-fill of first-sight provenance");
});
// ── RE-R3a: relevance score first-sight persistence (SC3) ──
const kortScore = {
mode: "kortform" as const,
dimensions: { pillar: 9, audience: 8, timing: 9, angle: 7, authority: 6 },
composite: 8.1,
priority: "Immediate" as const,
};
test("RED: persists score on a new record when present (first-sight)", () => {
const res = addTrend(emptyStore(), {
title: "Scored trend",
url: "https://example.com/sc",
source: "tavily",
capturedAt: "2026-06-24",
topics: ["ai"],
score: kortScore,
});
assert.deepEqual(res.store.trends[0].score, kortScore);
});
test("regression guard: omits score when absent (no undefined-valued key)", () => {
const res = addTrend(emptyStore(), {
title: "Unscored trend",
url: "https://example.com/us",
source: "tavily",
capturedAt: "2026-06-24",
topics: ["ai"],
});
assert.equal("score" in res.store.trends[0], false);
});
test("RED: re-capture keeps the first sighting's score (no overwrite), unions topics", () => {
let store = emptyStore();
store = addTrend(store, {
title: "Same scored trend",
url: "https://example.com/ssc",
source: "tavily",
capturedAt: "2026-06-01",
topics: ["a"],
score: kortScore,
}).store;
const lowerScore = {
mode: "kortform" as const,
dimensions: { pillar: 6, audience: 5, timing: 6, angle: 5, authority: 5 },
composite: 5.6,
priority: "Medium" as const,
};
const res2 = addTrend(store, {
title: "Same scored trend",
url: "https://example.com/ssc",
source: "gemini",
capturedAt: "2026-06-20",
topics: ["a", "b"],
score: lowerScore,
});
assert.equal(res2.added, false);
assert.equal(res2.merged, true);
assert.deepEqual(res2.store.trends[0].score, kortScore, "first-sight score kept (D3)");
assert.deepEqual([...res2.store.trends[0].topics].sort(), ["a", "b"]);
});
});
describe("queryByTopic", () => {
@ -429,7 +490,7 @@ describe("trends store", () => {
});
withFixture(v1, (path) => {
const s = loadStore(path);
assert.equal(s.schemaVersion, 2, "v1 store must migrate to v2");
assert.equal(s.schemaVersion, SCHEMA_VERSION, "v1 store must migrate to the current version");
assert.equal(s.trends.length, 1);
assert.equal(s.trends[0].title, "Old trend");
assert.equal(s.trends[0].capturedAt, "2026-05-01");
@ -438,22 +499,22 @@ describe("trends store", () => {
});
});
test("RED: round-trip loadStore→saveStore writes schemaVersion:2 to disk", () => {
test("RED: round-trip loadStore→saveStore writes the current schemaVersion to disk", () => {
withFixture(JSON.stringify({ schemaVersion: 1, trends: [] }), (path) => {
saveStore(path, loadStore(path));
const onDisk = JSON.parse(readFileSync(path, "utf8"));
assert.equal(onDisk.schemaVersion, 2);
assert.equal(onDisk.schemaVersion, SCHEMA_VERSION);
});
});
test("RED: a non-numeric schemaVersion is coerced to v2 (old code passes the string through)", () => {
test("RED: a non-numeric schemaVersion is coerced to the current version (old code passes the string through)", () => {
withFixture(JSON.stringify({ schemaVersion: "weird", trends: [] }), (path) => {
assert.equal(loadStore(path).schemaVersion, 2);
assert.equal(loadStore(path).schemaVersion, SCHEMA_VERSION);
});
});
// ── GREEN-only regression guards: old code already returns the current version ──
test("regression guard: a v2 store loads as v2, idempotent (records + publishedAt intact)", () => {
test("regression guard: a v2 store loads idempotent (records + publishedAt intact)", () => {
const v2 = JSON.stringify({
schemaVersion: 2,
trends: [
@ -470,7 +531,7 @@ describe("trends store", () => {
});
withFixture(v2, (path) => {
const s = loadStore(path);
assert.equal(s.schemaVersion, 2);
assert.equal(s.schemaVersion, SCHEMA_VERSION);
assert.equal(s.trends[0].publishedAt, "2026-05-30");
});
});
@ -493,4 +554,114 @@ describe("trends store", () => {
});
});
});
describe("schema migration (RE-R3a / score v2→v3)", () => {
const withFixture = (contents: string, fn: (path: string) => void) => {
const dir = tmp();
const path = join(dir, "trends.json");
try {
writeFileSync(path, contents, "utf8");
fn(path);
} finally {
rmSync(dir, { recursive: true, force: true });
}
};
// ── genuinely RED: pre-bump SCHEMA_VERSION=2 → loadStore(v2).schemaVersion===2 ≠ 3 ──
test("RED: a v2 store (no score) loads stamped as v3, records intact, no score invented", () => {
const v2 = JSON.stringify({
schemaVersion: 2,
trends: [
{
id: "abc123",
title: "Old dated trend",
url: "https://example.com/o",
source: "tavily",
capturedAt: "2026-05-01",
topics: ["ai"],
publishedAt: "2026-04-30",
},
],
});
withFixture(v2, (path) => {
const s = loadStore(path);
assert.equal(s.schemaVersion, 3, "v2 store must migrate to v3");
assert.equal(s.trends.length, 1);
assert.equal(s.trends[0].title, "Old dated trend");
assert.equal(s.trends[0].capturedAt, "2026-05-01");
assert.equal(s.trends[0].publishedAt, "2026-04-30");
assert.deepEqual(s.trends[0].topics, ["ai"]);
assert.equal("score" in s.trends[0], false, "migration must not invent a score");
});
});
test("RED: round-trip loadStore→saveStore writes schemaVersion:3 to disk", () => {
withFixture(JSON.stringify({ schemaVersion: 2, trends: [] }), (path) => {
saveStore(path, loadStore(path));
const onDisk = JSON.parse(readFileSync(path, "utf8"));
assert.equal(onDisk.schemaVersion, 3);
});
});
test("RED: a v3 store with score on records loads idempotent", () => {
const v3 = JSON.stringify({
schemaVersion: 3,
trends: [
{
id: "x",
title: "Scored",
url: "https://example.com/s",
source: "tavily",
capturedAt: "2026-06-01",
topics: ["ai"],
score: {
mode: "kortform",
dimensions: { pillar: 9, audience: 8, timing: 9, angle: 7, authority: 6 },
composite: 8.1,
priority: "Immediate",
},
},
],
});
withFixture(v3, (path) => {
const s = loadStore(path);
assert.equal(s.schemaVersion, 3);
assert.deepEqual(s.trends[0].score, {
mode: "kortform",
dimensions: { pillar: 9, audience: 8, timing: 9, angle: 7, authority: 6 },
composite: 8.1,
priority: "Immediate",
});
});
});
test("RED: a v3 store's score survives load → save → load (field preservation)", () => {
const score = {
mode: "kortform",
dimensions: { pillar: 9, audience: 8, timing: 9, angle: 7, authority: 6 },
composite: 8.1,
priority: "Immediate",
};
const v3 = JSON.stringify({
schemaVersion: 3,
trends: [
{
id: "x",
title: "Survivor",
url: "https://example.com/sv",
source: "tavily",
capturedAt: "2026-06-01",
topics: ["ai"],
score,
},
],
});
withFixture(v3, (path) => {
const first = loadStore(path);
saveStore(path, first);
const second = loadStore(path);
assert.deepEqual(second.trends[0].score, score, "score must survive a load+resave (no field stripping)");
});
});
});
});