From fef573746bc5e2a562da0e9a6154835b85d3fc85 Mon Sep 17 00:00:00 2001 From: garrytan Date: Thu, 8 Oct 2026 06:39:58 +0000 Subject: [PATCH] feat(developer-profile): --reconcile backfills office-hours tenure from timeline.jsonl sessions[] is written only at office-hours Phase 4.5, so runs that ended earlier never counted toward tenure and the tier undercounted. Add a manual `--reconcile` that scans projects/*/timeline.jsonl for office-hours `completed` events and appends a count-only row ({date, mode, project_slug, signal_count: 0, signals: [], backfilled: true}) for each run with no sessions[] entry for the same project within 60 minutes, extended back by a duration_s under 8h. It is idempotent, skips malformed lines, and leaves the file untouched on a no-op. `--read` counts backfilled rows for SESSION_COUNT and TIER but takes LAST_*, DESIGN_*, CROSS_PROJECT and the nudge from logged sessions only. Not wired into the preamble yet; the office-hours greeting guard for backfill-only profiles is left to the office-hours owner. Adopts #2668 (rewritten on current main). Closes part (b) of #2657. Co-authored-by: Briac <137724549+briacSck@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- bin/gstack-developer-profile | 96 +++++++++++++++++++++-- test/gstack-developer-profile.test.ts | 108 ++++++++++++++++++++++++++ 2 files changed, 199 insertions(+), 5 deletions(-) diff --git a/bin/gstack-developer-profile b/bin/gstack-developer-profile index cb556d487..2592b1d29 100755 --- a/bin/gstack-developer-profile +++ b/bin/gstack-developer-profile @@ -20,6 +20,9 @@ # --log-session append a session entry (from /office-hours) to # sessions[] and update aggregates. Required fields: # date, mode. Silent skip on invalid input. +# --reconcile backfill count-only sessions[] entries from office-hours +# `completed` events in projects/*/timeline.jsonl. +# Idempotent; backfilled rows count for tenure only. # # Profile file: ~/.gstack/developer-profile.json (unified schema — see # docs/designs/PLAN_TUNING_V0.md). Event file: ~/.gstack/projects/{SLUG}/ @@ -226,6 +229,86 @@ do_log_session() { "$SCRIPT_DIR/gstack-brain-enqueue" "developer-profile.json" 2>/dev/null & } +# ----------------------------------------------------------------------- +# Reconcile: sessions[] is written only at office-hours Phase 4.5, so runs +# that ended earlier never count toward tenure. Backfill one count-only row +# per office-hours `completed` timeline event that has no sessions[] entry +# for the same project within 60 minutes (extended back by the run's +# duration_s when it is under 8h, since Phase 4.5 logs mid-run). The file is +# rewritten only when something was added. +# ----------------------------------------------------------------------- +do_reconcile() { + ensure_profile + + local TMPOUT + TMPOUT=$(mktemp "$GSTACK_HOME/developer-profile.json.tmp.XXXXXX") + trap 'rm -f "$TMPOUT"' EXIT + + PROFILE_FILE_PATH="$PROFILE_FILE" PROJECTS_DIR="$GSTACK_HOME/projects" TMPOUT_PATH="$TMPOUT" bun -e " + const fs = require('fs'); + const path = require('path'); + const profile = JSON.parse(fs.readFileSync(process.env.PROFILE_FILE_PATH, 'utf-8')); + profile.sessions = profile.sessions || []; + + const runs = []; + let slugs = []; + try { slugs = fs.readdirSync(process.env.PROJECTS_DIR); } catch {} + for (const slug of slugs) { + let lines = []; + try { lines = fs.readFileSync(path.join(process.env.PROJECTS_DIR, slug, 'timeline.jsonl'), 'utf-8').split('\n'); } catch { continue; } + for (const line of lines) { + let e; + try { e = JSON.parse(line); } catch { continue; } + if (!e || e.skill !== 'office-hours' || e.event !== 'completed') continue; + const t = Date.parse(e.ts); + if (Number.isNaN(t)) continue; + const dur = Number(e.duration_s); + runs.push({ t, ts: e.ts, slug, mode: e.mode, span: dur > 0 && dur < 8 * 3600 ? dur * 1000 : 0 }); + } + } + runs.sort((a, b) => a.t - b.t); + + const HOUR = 60 * 60 * 1000; + const covered = profile.sessions + .filter(s => s && s.mode !== 'resources') + .map(s => ({ t: Date.parse(s.ts || s.date || ''), slug: s.project_slug || null })) + .filter(c => !Number.isNaN(c.t)); + + let added = 0; + for (const run of runs) { + const matched = covered.some(c => (c.slug === null || c.slug === run.slug) + && c.t >= run.t - run.span - HOUR && c.t <= run.t + HOUR); + if (matched) continue; + covered.push({ t: run.t, slug: run.slug }); + profile.sessions.push({ + date: run.ts, + mode: run.mode && run.mode !== 'resources' ? run.mode : 'unknown', + project_slug: run.slug, + signal_count: 0, + signals: [], + backfilled: true, + }); + added++; + } + + if (added > 0) { + const at = s => { const t = Date.parse(s.ts || s.date || ''); return Number.isNaN(t) ? Infinity : t; }; + profile.sessions.sort((a, b) => at(a) - at(b)); + fs.writeFileSync(process.env.TMPOUT_PATH, JSON.stringify(profile, null, 2)); + } + console.log('RECONCILE: ' + added + ' backfilled from ' + runs.length + ' office-hours run(s)'); + " + + if [ -s "$TMPOUT" ]; then + mv "$TMPOUT" "$PROFILE_FILE" + trap - EXIT + "$SCRIPT_DIR/gstack-brain-enqueue" "developer-profile.json" 2>/dev/null & + else + rm -f "$TMPOUT" + trap - EXIT + fi +} + # ----------------------------------------------------------------------- # Read: emit legacy KEY: VALUE output for /office-hours compat. # ----------------------------------------------------------------------- @@ -241,20 +324,22 @@ do_read() { // the count (bumping TIER), clobbers LAST_PROJECT/LAST_ASSIGNMENT/LAST_DESIGN_TITLE, // and pushes NUDGE_ELIGIBLE over its threshold from bookkeeping alone. const realSessions = sessions.filter(e => e.mode !== 'resources'); + // Backfilled rows (--reconcile) carry no content: they count for tenure only. + const loggedSessions = realSessions.filter(e => !e.backfilled); const count = realSessions.length; let tier = 'introduction'; if (count >= 8) tier = 'inner_circle'; else if (count >= 4) tier = 'regular'; else if (count >= 1) tier = 'welcome_back'; - const last = realSessions[realSessions.length - 1] || {}; - const prev = realSessions[realSessions.length - 2] || {}; + const last = loggedSessions[loggedSessions.length - 1] || {}; + const prev = loggedSessions[loggedSessions.length - 2] || {}; const crossProject = prev.project_slug && last.project_slug ? prev.project_slug !== last.project_slug : false; - const designs = realSessions.map(e => e.design_doc || '').filter(Boolean); - const designTitles = realSessions + const designs = loggedSessions.map(e => e.design_doc || '').filter(Boolean); + const designTitles = loggedSessions .map(e => (e.design_doc ? (e.project_slug || 'unknown') : '')) .filter(Boolean); @@ -267,7 +352,7 @@ do_read() { // someone who keeps returning to *build*, not startup-mode diagnostics and not // resources bookkeeping. (Was \`e.mode !== 'startup'\`, which counted resources // entries and excluded real startup sessions.) - const builderSessions = realSessions.filter(e => e.mode === 'builder').length; + const builderSessions = loggedSessions.filter(e => e.mode === 'builder').length; const nudgeEligible = builderSessions >= 3 && totalSignals >= 5; const resources = p.resources_shown || []; @@ -529,6 +614,7 @@ case "$CMD" in --check-mismatch) do_check_mismatch ;; --migrate) do_migrate ;; --log-session) do_log_session "$@" ;; + --reconcile) do_reconcile ;; --help|-h) sed -n '1,/^set -euo/p' "$0" | sed 's|^# \?||' ;; *) echo "gstack-developer-profile: unknown subcommand '$CMD'" >&2 diff --git a/test/gstack-developer-profile.test.ts b/test/gstack-developer-profile.test.ts index 1af3eef18..0550dfc31 100644 --- a/test/gstack-developer-profile.test.ts +++ b/test/gstack-developer-profile.test.ts @@ -698,3 +698,111 @@ describe('gstack-developer-profile resources entries do not inflate count/tier/n }); }); + +// ----------------------------------------------------------------------- +// --reconcile: backfill tenure from office-hours timeline history (#2657) +// ----------------------------------------------------------------------- + +describe('gstack-developer-profile --reconcile', () => { + const completed = (ts: string, extra: Record = {}) => + JSON.stringify({ skill: 'office-hours', event: 'completed', ts, ...extra }); + + function writeTimeline(slug: string, lines: string[]) { + const dir = path.join(tmpHome, 'projects', slug); + fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync(path.join(dir, 'timeline.jsonl'), lines.join('\n') + '\n'); + } + + function logSession(entry: Record) { + expect(runDev('--log-session', JSON.stringify({ mode: 'builder', signal_count: 0, signals: [], ...entry })).status).toBe(0); + } + + const readKey = (out: string, key: string) => out.match(new RegExp(`^${key}: (.*)$`, 'm'))?.[1]; + + test('backfills one row per office-hours completed run and counts it for tenure', () => { + writeTimeline('alpha', [ + completed('2026-09-01T10:00:00Z'), + JSON.stringify({ skill: 'office-hours', event: 'started', ts: '2026-09-02T09:00:00Z' }), + completed('2026-09-02T10:00:00Z'), + JSON.stringify({ skill: 'review', event: 'completed', ts: '2026-09-03T10:00:00Z' }), + ]); + writeTimeline('beta', [completed('2026-09-04T10:00:00Z')]); + const r = runDev('--reconcile'); + expect(r.status).toBe(0); + expect(r.stdout).toContain('RECONCILE: 3 backfilled from 3 office-hours run(s)'); + const sessions = readProfile().sessions as Array>; + expect(sessions).toHaveLength(3); + expect(sessions[2]).toEqual({ + date: '2026-09-04T10:00:00Z', mode: 'unknown', project_slug: 'beta', + signal_count: 0, signals: [], backfilled: true, + }); + const read = runDev('--read').stdout; + expect(readKey(read, 'SESSION_COUNT')).toBe('3'); + expect(readKey(read, 'TIER')).toBe('welcome_back'); + }); + + test('a second run adds nothing and leaves the profile file untouched', () => { + writeTimeline('alpha', [completed('2026-09-01T10:00:00Z'), completed('2026-09-05T10:00:00Z')]); + expect(runDev('--reconcile').stdout).toContain('RECONCILE: 2 backfilled'); + const file = path.join(tmpHome, 'developer-profile.json'); + const before = fs.readFileSync(file, 'utf-8'); + const old = new Date('2026-01-01T00:00:00Z'); + fs.utimesSync(file, old, old); + expect(runDev('--reconcile').stdout).toContain('RECONCILE: 0 backfilled from 2 office-hours run(s)'); + expect(fs.readFileSync(file, 'utf-8')).toBe(before); + expect(fs.statSync(file).mtimeMs).toBe(old.getTime()); + }); + + test('a logged session minutes before the run across an hour boundary covers it, per project', () => { + logSession({ date: '2026-09-01T10:55:00Z', ts: '2026-09-01T10:55:00Z', project_slug: 'alpha' }); + writeTimeline('alpha', [completed('2026-09-01T11:20:00Z')]); + writeTimeline('beta', [completed('2026-09-01T11:20:00Z')]); + expect(runDev('--reconcile').stdout).toContain('RECONCILE: 1 backfilled from 2 office-hours run(s)'); + const backfilled = (readProfile().sessions as Array>).filter(s => s.backfilled); + expect(backfilled.map(s => s.project_slug)).toEqual(['beta']); + }); + + test('a long session logged hours before completion is not double-counted; garbage durations are not trusted', () => { + logSession({ date: '2026-09-01T09:00:00Z', ts: '2026-09-01T09:00:00Z', project_slug: 'alpha' }); + logSession({ date: '2026-09-02T09:00:00Z', ts: '2026-09-02T09:00:00Z', project_slug: 'alpha' }); + writeTimeline('alpha', [ + completed('2026-09-01T12:00:00Z', { duration_s: '11000' }), + completed('2026-09-02T19:00:00Z', { duration_s: '40000' }), + ]); + expect(runDev('--reconcile').stdout).toContain('RECONCILE: 1 backfilled from 2 office-hours run(s)'); + const backfilled = (readProfile().sessions as Array>).filter(s => s.backfilled); + expect(backfilled.map(s => s.date)).toEqual(['2026-09-02T19:00:00Z']); + }); + + test('malformed timeline lines are skipped', () => { + writeTimeline('alpha', ['{not json', 'null', completed('not-a-date'), completed('2026-09-01T10:00:00Z')]); + const r = runDev('--reconcile'); + expect(r.status).toBe(0); + expect(r.stdout).toContain('RECONCILE: 1 backfilled from 1 office-hours run(s)'); + }); + + test('backfilled rows never surface as LAST_*, DESIGN_* or CROSS_PROJECT', () => { + logSession({ + date: '2026-09-01T10:00:00Z', ts: '2026-09-01T10:00:00Z', project_slug: 'alpha', + assignment: 'Talk to five users', design_doc: 'docs/alpha-design.md', + }); + writeTimeline('beta', [completed('2026-09-03T10:00:00Z'), completed('2026-09-04T10:00:00Z')]); + expect(runDev('--reconcile').stdout).toContain('RECONCILE: 2 backfilled'); + const read = runDev('--read').stdout; + expect(readKey(read, 'SESSION_COUNT')).toBe('3'); + expect(readKey(read, 'LAST_PROJECT')).toBe('alpha'); + expect(readKey(read, 'LAST_ASSIGNMENT')).toBe('Talk to five users'); + expect(readKey(read, 'LAST_DESIGN_TITLE')).toBe('docs/alpha-design.md'); + expect(readKey(read, 'DESIGN_COUNT')).toBe('1'); + expect(readKey(read, 'CROSS_PROJECT')).toBe('false'); + }); + + test('a backfill-only profile reaches welcome_back with empty LAST_*', () => { + writeTimeline('alpha', [completed('2026-09-01T10:00:00Z')]); + runDev('--reconcile'); + const read = runDev('--read').stdout; + expect(readKey(read, 'TIER')).toBe('welcome_back'); + expect(readKey(read, 'LAST_PROJECT')).toBe(''); + expect(readKey(read, 'LAST_ASSIGNMENT')).toBe(''); + }); +});