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

@ -9,15 +9,20 @@
* node --import tsx src/cli.ts status [--store <path>] [--json]
* echo '<raw item|batch>' | node --import tsx src/cli.ts normalize
* echo '<scored candidates>' | node --import tsx src/cli.ts score [--mode kortform|long-form] [--threshold N]
* echo '<raw item|batch>' | node --import tsx src/cli.ts capture [--store <path>] [--json]
*
* The capture agent (research-engine) calls `add` to fold a freshly-polled trend
* into the store, and `query`/`list` to reason over accumulated history. The
* The capture agent (research-engine) folds freshly-polled trends into the store via
* `capture` (the normalizing batch path: stdin normalizeItem(s) itemToInput
* addTrend), and reasons over accumulated history via `query`/`list`. `add` is the
* MANUAL single-trend path (raw flags, no normalization, publish-date-free). The
* polling + relevance-scoring itself lives upstream; this is the deterministic store.
*
* `normalize` + `score` (RE-R1) are the deterministic research-engine seam: both read
* their JSON PAYLOAD FROM STDIN (so they do not overload `--json`, which stays an
* output toggle) and print JSON to stdout. `normalize` validates raw items into the
* canonical envelope; `score` triages scored candidates (composite/band/threshold).
* `normalize` + `score` (RE-R1) and `capture` (RE-R2a) are the deterministic
* research-engine seam: all read their JSON PAYLOAD FROM STDIN (so they do not overload
* `--json`, which stays an output toggle). `normalize` validates raw items into the
* canonical envelope; `score` triages scored candidates (composite/band/threshold);
* `capture` normalizes + folds each valid item into the store (persisting `publishedAt`),
* reporting content-invalid items in the summary `errors[]`, never via the exit code.
*
* Exit code: 0 on success, 2 on usage error (incl. unparseable stdin / bad flag).
*/
@ -33,7 +38,7 @@ import {
queryByTopic,
saveStore,
} from "./store.js";
import { normalizeItem, normalizeItems } from "./item.js";
import { normalizeItem, normalizeItems, itemToInput } from "./item.js";
import { triage } from "./score.js";
import type { ScoreMode } from "./score.js";
@ -72,7 +77,8 @@ function usage(msg: string): never {
" list [--since <YYYY-MM-DD>] [--limit <n>] [--store <path>] [--json]\n" +
" status [--store <path>] [--json]\n" +
" normalize < raw-item-or-batch.json\n" +
" score [--mode kortform|long-form] [--threshold N] < scored-candidates.json",
" score [--mode kortform|long-form] [--threshold N] < scored-candidates.json\n" +
" capture [--store <path>] [--json] < raw-item-or-batch.json",
);
process.exit(2);
}
@ -228,6 +234,34 @@ function main(): void {
return;
}
if (command === "capture") {
const payload = readStdinJson(); // exits 2 on empty/unparseable stdin
const raw = Array.isArray(payload) ? payload : [payload];
const { items, errors } = normalizeItems(raw);
const store = loadStore(storePath);
// Tally derived from AddResult {added, merged} (no `duplicates` field): a fold is
// `added` (new), else `merged` (existing gained topics), else a plain `duplicate`.
let added = 0;
let merged = 0;
let duplicates = 0;
for (const item of items) {
const res = addTrend(store, itemToInput(item, today()));
if (res.added) added++;
else if (res.merged) merged++;
else duplicates++;
}
saveStore(storePath, store);
if (asJson) {
console.log(JSON.stringify({ added, duplicates, merged, errors }, null, 2));
return;
}
console.log(
`Captured into ${storePath}: ${added} added, ${merged} merged, ` +
`${duplicates} duplicate, ${errors.length} invalid (${store.trends.length} total)`,
);
return;
}
usage(command ? `unknown command: ${command}` : "no command given");
}