feat(linkedin-studio): trend-store staleness nudge — SessionStart B-S3 [skip-docs]

SessionStart now warns (>=7d, warn-only) when the persistent trend store's
newest capture is stale, firing ONLY when the store already holds captures
(a never-scanned user is never nagged). Neutral wording — "scan for trends"
hits trend-spotter's own trigger; no hardcoded beat (de-niche invariant).

- store.ts: newestCaptureDate() — pure max-capturedAt staleness signal (SSOT)
- cli.ts: status [--json] subcommand (count + newest + daysStale)
- session-start.mjs: trendsNewestCapture() reads trends.json as raw JSON
  (no tsx spawn at session start) + the reminder line, beside import-staleness
- tests: +3 store tests (newestCaptureDate) + hook subprocess test (3 cases:
  >=7d fires, <7d silent, absent/empty silent + no crash)
- test-runner.sh: trends floor 21->24

Verified: trends 24/24 · all hook tests 131/131 · gate 89/0/0 · real render
confirms "Trend signals are N days old. Scan for trends…".

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RigJBiRFNtFZKCz21qNbQ4
This commit is contained in:
Kjell Tore Guttormsen 2026-06-23 12:23:53 +02:00
commit 0907b2b92d
6 changed files with 186 additions and 2 deletions

View file

@ -29,6 +29,28 @@ function daysSince(dateStr) {
return Math.floor((Date.now() - epoch) / 86400000);
}
// B-S3: newest trend capture (ISO date) from the persistent trend store, read
// as raw JSON. The hook must NOT spawn the tsx CLI at session start (slow +
// needs node_modules/tsx); the store schema (trends[].capturedAt) is stable and
// versioned, so a direct read is safe. Twin of scripts/trends/src/store.ts:
// newestCaptureDate (max capturedAt). Returns null when the store is absent,
// unreadable, or empty — no captures yet ⇒ no nudge.
function trendsNewestCapture(storePath) {
if (!existsSync(storePath)) return null;
try {
const parsed = JSON.parse(readFileSync(storePath, 'utf-8'));
const trends = Array.isArray(parsed?.trends) ? parsed.trends : [];
let newest = null;
for (const t of trends) {
const d = typeof t?.capturedAt === 'string' ? t.capturedAt : null;
if (d && (newest === null || d > newest)) newest = d;
}
return newest;
} catch {
return null;
}
}
function isoWeek() {
const d = new Date();
const dayNum = d.getUTCDay() || 7;
@ -309,6 +331,16 @@ if (existsSync(STATE_FILE)) {
reminders += '- No analytics data imported yet. Run /linkedin:import to start tracking performance.\\n';
}
// Trend-store staleness (B-S3): warn-only, ≥7 days. Fires ONLY when the store
// already holds captures (newest === null ⇒ no nudge), so a never-scanned user
// is never nagged. Neutral wording — "scan for trends" hits trend-spotter's own
// trigger; no hardcoded beat (de-niche invariant, §17 guard).
const newestTrendCapture = trendsNewestCapture(join(getDataRoot('trends'), 'trends.json'));
const daysSinceTrend = daysSince(newestTrendCapture);
if (daysSinceTrend !== null && daysSinceTrend >= 7) {
reminders += `- Trend signals are ${daysSinceTrend} days old. Scan for trends to refresh the digest before planning content.\\n`;
}
// Milestone reminders
if (milestonePhase && followerCount > 0) {
if (milestoneStatus === 'SIGNIFICANTLY BEHIND') {