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:
parent
9f9c4bbd86
commit
0907b2b92d
6 changed files with 186 additions and 2 deletions
|
|
@ -0,0 +1,79 @@
|
|||
import { describe, test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, copyFileSync } from 'node:fs';
|
||||
import { join, dirname } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
// B-S3: SessionStart emits a warn-only "trend signals are N days old" nudge,
|
||||
// driven by the trend store's newest capturedAt, and ONLY when the store already
|
||||
// holds captures. session-start is a procedural hook with no exports, so we run
|
||||
// it as a subprocess with an isolated HOME + LINKEDIN_STUDIO_DATA and inspect the
|
||||
// additionalContext. Pattern: session-start-remember.test.mjs subprocess harness.
|
||||
const here = dirname(fileURLToPath(import.meta.url));
|
||||
const hookPath = join(here, '..', 'session-start.mjs');
|
||||
const PLUGIN_ROOT = join(here, '..', '..', '..');
|
||||
const STATE_TEMPLATE = join(PLUGIN_ROOT, 'config', 'state-file.template.md');
|
||||
|
||||
const NUDGE = 'Trend signals are';
|
||||
const isoDaysAgo = (n) => new Date(Date.now() - n * 86400000).toISOString().slice(0, 10);
|
||||
|
||||
// Run the hook against an isolated HOME + data root; optionally seed a trend
|
||||
// store. Returns the additionalContext string the hook injected at session start.
|
||||
function runHook({ captures }) {
|
||||
const home = mkdtempSync(join(tmpdir(), 'lis-ts-home-'));
|
||||
const data = mkdtempSync(join(tmpdir(), 'lis-ts-data-'));
|
||||
try {
|
||||
// The reminders block only runs when the state file already exists; the
|
||||
// template is the canonical valid state, so copy it into the isolated HOME.
|
||||
mkdirSync(join(home, '.claude'), { recursive: true });
|
||||
copyFileSync(STATE_TEMPLATE, join(home, '.claude', 'linkedin-studio.local.md'));
|
||||
|
||||
if (captures) {
|
||||
mkdirSync(join(data, 'trends'), { recursive: true });
|
||||
writeFileSync(
|
||||
join(data, 'trends', 'trends.json'),
|
||||
JSON.stringify({ schemaVersion: 1, trends: captures }),
|
||||
);
|
||||
}
|
||||
|
||||
const stdout = execFileSync('node', [hookPath], {
|
||||
input: '',
|
||||
env: { ...process.env, HOME: home, USERPROFILE: home, LINKEDIN_STUDIO_DATA: data },
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
return JSON.parse(stdout).hookSpecificOutput.additionalContext;
|
||||
} finally {
|
||||
rmSync(home, { recursive: true, force: true });
|
||||
rmSync(data, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
const trend = (capturedAt) => ({
|
||||
id: 't' + capturedAt,
|
||||
title: 'Some trend ' + capturedAt,
|
||||
url: 'https://example.com/' + capturedAt,
|
||||
source: 'tavily',
|
||||
capturedAt,
|
||||
topics: ['x'],
|
||||
});
|
||||
|
||||
describe('session-start — trend-store staleness nudge (B-S3)', () => {
|
||||
test('fires when newest capture is >=7 days old', () => {
|
||||
const ctx = runHook({ captures: [trend('2026-01-01'), trend(isoDaysAgo(10))] });
|
||||
assert.ok(ctx.includes(NUDGE), 'expected the trend-staleness nudge for a 10-day-old store');
|
||||
});
|
||||
|
||||
test('does NOT fire when newest capture is fresh (<7 days)', () => {
|
||||
const ctx = runHook({ captures: [trend(isoDaysAgo(2))] });
|
||||
assert.ok(!ctx.includes(NUDGE), 'a 2-day-old store must not trigger the nudge');
|
||||
});
|
||||
|
||||
test('does NOT fire (and does not crash) when the store is absent/empty', () => {
|
||||
const absent = runHook({ captures: null });
|
||||
assert.ok(!absent.includes(NUDGE), 'no store => no nudge');
|
||||
const empty = runHook({ captures: [] });
|
||||
assert.ok(!empty.includes(NUDGE), 'empty store => no nudge (never-scanned user is never nagged)');
|
||||
});
|
||||
});
|
||||
|
|
@ -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') {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue