feat(linkedin-studio): RE-R2a — item→store capture bridge + publishedAt persistence (schema v1→v2, lossless migrate) [skip-docs]

Closes the research-engine capture loop RE-R1 deferred:
- itemToInput(item, capturedAt): pure envelope→TrendInput bridge in item.ts —
  injects capturedAt, carries publishedAt verbatim; no id, no re-validate
- publishedAt persisted: TrendRecord/TrendInput gain it; addTrend conditional-spread,
  first-sight kept on re-capture (no back-fill). SCHEMA_VERSION 1→2 with a lossless
  forward migrate-on-load: Math.max(onDisk, current) + numeric-typeof coercion
  (string/NaN/absent → current; non-array trends coercion preserved verbatim)
- `capture` CLI: stdin raw item|batch → normalize → bridge → addTrend → saveStore once;
  tally {added,duplicates,merged,errors} from AddResult; content-invalid → errors[],
  exit 2 only on bad stdin; --json summary
- wiring: trend-spotter.md Step 4.5 N×`add` → one normalizing `capture` batch; README
  add/capture framing corrected; test-runner Section 16h (capture wiring, unconditional)
  + floors bumped (trends 62→79, ASSERT 87→90)

TDD: 17 new tests (12 genuinely-RED logic-RED + 5 regression guards), tsc clean,
gate 105/0/0. 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_01VmHCQjJHUyWwxGAVVjNLgp
This commit is contained in:
Kjell Tore Guttormsen 2026-06-24 11:12:50 +02:00
commit 7a158030b6
10 changed files with 465 additions and 31 deletions

View file

@ -28,6 +28,8 @@ export interface TrendInput {
url: string;
source: string;
capturedAt: string;
/** The source's own publish date (ISO-8601), if known. Distinct from capturedAt; persisted first-sight, never back-filled. */
publishedAt?: string;
topics: string[];
summary?: string;
}
@ -74,8 +76,15 @@ export function emptyStore(): TrendStore {
export function loadStore(path: string): TrendStore {
if (!existsSync(path)) return emptyStore();
const parsed = JSON.parse(readFileSync(path, "utf8")) as Partial<TrendStore>;
// Forward migrate-on-load: stamp to the current version, never downgrade. v1→v2 is
// purely additive-optional (an old record is already a valid v2 record that simply
// lacks the optional publishedAt), so the migration is the version stamp alone —
// records pass through untouched (lossless + idempotent for any well-formed store).
// A string / NaN / absent version coerces to the current version (never crashes); the
// non-array `trends` coercion below is unchanged and out of the losslessness claim.
const onDisk = typeof parsed.schemaVersion === "number" ? parsed.schemaVersion : SCHEMA_VERSION;
return {
schemaVersion: parsed.schemaVersion ?? SCHEMA_VERSION,
schemaVersion: Math.max(onDisk, SCHEMA_VERSION),
trends: Array.isArray(parsed.trends) ? parsed.trends : [],
};
}
@ -122,6 +131,7 @@ export function addTrend(store: TrendStore, input: TrendInput): AddResult {
url: input.url,
source: input.source,
capturedAt: input.capturedAt,
...(input.publishedAt !== undefined ? { publishedAt: input.publishedAt } : {}),
topics: [...input.topics],
...(input.summary !== undefined ? { summary: input.summary } : {}),
};