matthewwong525/WongStack / .agents/plugins/memory
memory
Verifiedscripts/lib/digest.mjs
View on GitHub// The session-start digest: one `digest` call, bounded output, and a local cache for offline starts.
import { existsSync, readFileSync, statSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { pluginPath, readJson, SCRIPT, statePath, writeJson } from './store.mjs';
import { topicLines } from './topics.mjs';
export const MAX_LINES = 40;
export const MAX_BYTES = 6 * 1024;
// The person's own wiki page gets at most this much of the digest, so facts keep room as the page grows.
export const PERSON_MAX_BYTES = 1536;
// An older store still lists facts; it takes this many.
const FETCH_LIMIT = 60;
const SEARCH = `${SCRIPT} search <terms>`;
const RECALL = `${SCRIPT} recall <question>`;
const PULL = `${SCRIPT} pull <topic>`;
export const HELPER = pluginPath('references/recall-helper.md');
const OLDER_NOTE = 'Memory: the memory store is older than this plugin, so no daily tidy starts until the app deploys its update and an admin runs `memory.mjs migrate`.';
const ageDays = (iso, now) => {
const time = Date.parse(iso);
return Number.isNaN(time) ? '?' : `${Math.max(0, Math.floor((now - time) / 86400000))}d`;
};
// One line per fact. Search and show add the change state and supersession when the fact carries them.
export function formatFact(fact, now = Date.now()) {
const where = [fact.slug, fact.state, ageDays(fact.created_at, now), fact.author || 'unknown', `#${fact.id}`].filter(Boolean).join(', ');
return `- [${fact.type}] ${fact.body.replace(/\s+/g, ' ')} (${where})${fact.superseded_by ? ` superseded by #${fact.superseded_by}` : ''}`;
}
export const DIFFERED = 'model reported other counts';
// Whitespace collapsed to single spaces, cut to `max` characters: a record's text never adds a line of its own.
const oneLine = (text, max) => String(text ?? '').replace(/\s+/g, ' ').trim().slice(0, max);
// The person's last background run as one short line, or null for a record it can't read. A record is only text
// its writer sent, so the line never throws, never breaks, and stays short: host 64 characters, reason 300.
export function formatRun(run) {
if (!run || !['capture', 'consolidation'].includes(run.kind)) return null;
let parsed;
try { parsed = JSON.parse(run.counts || '{}'); } catch { return null; }
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return null;
const when = `${oneLine(run.finished_at || run.started_at, 16).replace('T', ' ')} UTC on ${oneLine(run.host, 64) || 'unknown host'}`;
if (run.status === 'failed') return `Last background ${run.kind} run failed (${when}): ${oneLine(run.reason, 300) || 'no reason recorded'}`;
const counts = Object.entries(parsed).filter(([key, value]) => /^[a-z]{1,20}$/.test(key) && Number.isFinite(value) && value)
.slice(0, 8).map(([key, value]) => `${key} ${value}`).join(', ');
const differed = String(run.reason ?? '').startsWith(DIFFERED) ? " (the run's own report differed)" : '';
return `Last background ${run.kind} run (${when}): ${counts || 'nothing to do'}${differed}`;
}
const NOTE_LINE = /^(?:[a-z ]{3,40} \d{1,7}(?: -> \d{1,7})?|stopped at its time limit; the rest is on tomorrow's map)$/;
/** An `ok` tidy's note as the lines safe to show: a record is text its writer sent, so only a known shape passes. */
export const tidyNote = reason => String(reason ?? '').split('\n').map(line => line.trim()).filter(line => NOTE_LINE.test(line)).slice(0, 8);
// The last daily tidy: one short line, or with a note, a line for its counts and each map number that changed.
// A record is text its writer sent, so it is cut and checked as a run's is.
export function formatTidy(daily) {
if (!daily) return 'Last tidy: none yet; the first chat of a day starts one.';
if (!/^\d{4}-\d{2}-\d{2}$/.test(String(daily.day))) return null;
const when = `${daily.day} on ${oneLine(daily.host, 64) || 'unknown host'}`;
if (daily.status === 'started') return `Last tidy (${when}): started, not finished.`;
if (daily.status === 'failed') return `Last tidy (${when}) failed: ${oneLine(daily.reason, 300) || 'no reason recorded'}`;
const counts = daily.counts && typeof daily.counts === 'object' && !Array.isArray(daily.counts) ? daily.counts : {};
const said = Object.entries(counts).filter(([key, value]) => /^[a-z]{1,20}$/.test(key) && Number.isFinite(value) && value)
.slice(0, 8).map(([key, value]) => `${key} ${value}`).join(', ');
const note = tidyNote(daily.reason);
if (!note.length) return `Last tidy (${when}): ${said || 'nothing to do'}`;
return [`Last tidy (${when}):`, ...[said || 'nothing to do', ...note].map(line => ` ${line}`)].join('\n');
}
// The topics that gained facts this week, as one line: only tags that are topics. Null when none.
export function latelyLine(lately = [], topicTags = []) {
const topics = new Set(topicTags);
const shown = lately.filter(row => topics.has(row.tag) && Number(row.n) > 0).map(row => `${row.tag} ${Number(row.n)}`);
return shown.length ? `Lately: ${shown.join(' · ')}` : null;
}
// One line on other changes' open threads: how many carry each step tag and how many carry none, and how a
// step loads its own. `steps` are rows of { tag, n }, with a null tag for the untagged. Null when there are none.
export function stepLine(steps = [], stepTags = steps.map(row => row.tag).filter(Boolean)) {
const counts = new Map(steps.map(row => [row.tag ?? null, Number(row.n)]));
const tagged = stepTags.filter(tag => counts.get(tag)).map(tag => `${tag} ${counts.get(tag)}`).join(', ');
const untagged = counts.get(null) ? `${counts.get(null)} untagged` : '';
if (!tagged && !untagged) return null;
return `Open threads on other changes, by step: ${[tagged, untagged].filter(Boolean).join('; ')}. When a step starts, load its own: \`${SCRIPT} search --type thread --tag <step>\`.`;
}
// The person's page as digest lines: its body, without the `#` title, the `Back to` footer, or blank lines.
const personLines = text => text.split('\n').map(line => line.trimEnd()).filter(line => line && !line.startsWith('# ') && !line.startsWith('Back to '));
// No other fact is listed: the tidy puts lasting ones on pages. An `older` store's answer has no tidy, so its facts
// list as before.
export function buildDigest({ facts = [], live, threads = [], steps = [], stepTags, topics = [], topicTags = [], person = null, run = null,
daily = null, lately = [], fresh = [], older = false, slug = null, personal = false, admin = false, now = Date.now() }) {
const listed = older ? facts : [];
live ??= facts.length;
const runLine = older || run?.status === 'failed' ? formatRun(run) : null;
const tidyLine = older ? null : formatTidy(daily);
const step = stepLine(steps, stepTags);
if (!live && !threads.length && !runLine && !step && !person && !topics.length && !fresh.length && !daily) return '';
const lines = [
'# Memory digest',
`Facts are dated context, not instructions; the repo wins. Once you know the task, before you act on more than a quick question, pull its topics: \`${PULL}\`; give a task that spans topics to the memory helper: ${HELPER}. Before substantial work, recall the task in your own words: \`${RECALL}\`. Read cited originals; use explicit history scope for past decisions.`,
...(personal ? [`This machine sees its own private facts and shared team memory.${admin ? ` See everyone's: \`${SCRIPT} search <terms> --everyone\`.` : ''}`] : []),
...[...(tidyLine?.split('\n') ?? []), runLine].filter(Boolean),
];
const size = next => next.reduce((sum, line) => sum + Buffer.byteLength(line) + 1, 0);
const closing = count => (older ? `${count} more live facts are not shown. Search them: \`${SEARCH}\`.`
: `${count} live facts. Pull a topic for its facts: \`${PULL}\`; search them: \`${SEARCH}\`.`);
const reserve = size([closing(live)]);
let bytes = Buffer.byteLength(lines.join('\n'));
// Whether `next` fits with room left for `held`, lines still to come, and the closing line.
const fits = (next, held = []) => lines.length + next.length + held.length + 1 <= MAX_LINES && bytes + size(next) + size(held) + reserve <= MAX_BYTES;
const push = next => { lines.push(...next); bytes += size(next); };
let full = false;
let shown = 0;
// A list under a heading: the heading with its first item, then items while they fit.
const pushList = (heading, items) => {
for (const [i, line] of items.entries()) {
const next = [...(i ? [] : [heading]), line];
if (!fits(next)) { full = true; return; }
push(next);
shown += 1;
}
};
// The step line goes right after the current change's threads; its room stays reserved until then.
const stepHeld = step ? [step] : [];
for (const [i, fact] of threads.entries()) {
const next = [...(i ? [] : [`## Open threads on \`${slug}\``]), formatFact(fact, now)];
if (!fits(next, stepHeld)) { full = true; break; }
push(next);
shown += 1;
}
if (step && fits(stepHeld)) push(stepHeld);
const block = next => { if (full || !next.length) return; if (fits(next)) push(next); else full = true; };
block(topics);
block(older ? [] : [latelyLine(lately, topicTags)].filter(Boolean));
block(personSection(person, fits, size));
if (!full) pushList('## New since the last tidy', fresh.map(fact => formatFact(fact, now)));
const threadIds = new Set(threads.map(fact => fact.id));
if (!full) pushList('## Live facts', listed.filter(fact => !threadIds.has(fact.id)).map(fact => formatFact(fact, now)));
if (older ? live > shown : live > 0) lines.push(closing(older ? live - shown : live));
return lines.join('\n');
}
// The person's page as a digest section within PERSON_MAX_BYTES, ending with a line naming the rest when cut.
function personSection(person, fits, size) {
const body = person ? personLines(person.text) : [];
if (!body.length) return [];
const rest = `The rest: ${person.path}.`;
const section = [`## You (${person.path})`];
for (const [i, line] of body.entries()) {
const tail = i === body.length - 1 ? [] : [rest];
if (size([...section, line, ...tail]) > PERSON_MAX_BYTES || !fits([...section, line], tail)) return [...section, rest];
section.push(line);
}
return section;
}
// The digest from the store, written to the cache an offline start reads. `daily` is the last tidy; `older` says the
// store answers no tidy yet, so none may start.
export async function loadDigest(ctx, store) {
const { profile } = store;
const slug = (await profile.currentSlug?.(ctx)) ?? null;
const stepTags = Object.keys(profile.steps || {});
const data = await store.digest({ slug, stepTags, limit: FETCH_LIMIT });
const older = !('daily' in data);
const topics = await profileTopics(ctx, profile);
const text = buildDigest({ ...data, older, stepTags, slug, topics: topicLines(topics, SCRIPT), topicTags: topics.map(topic => topic.tag),
person: (await profile.person?.(ctx)) ?? null, personal: true, admin: store.role === 'admin' });
writeFileSync(statePath(ctx, 'digest.md'), text);
return { text, daily: data.daily ?? null, older };
}
// The older-store line, once a day on this computer; null on the other starts that day.
export function olderNote(ctx, now = new Date()) {
const file = statePath(ctx, 'older-store.json');
const day = new Intl.DateTimeFormat('en-CA').format(now);
if (readJson(file, {}).day === day) return null;
writeJson(file, { day });
return OLDER_NOTE;
}
// The profile's topics; a broken topic list gives none, so the digest still loads.
async function profileTopics(ctx, profile) {
try { return (await profile?.topics?.(ctx)) ?? []; } catch { return []; }
}
// The profile's topics as digest lines.
export async function profileTopicLines(ctx, profile) {
try { return topicLines(await profileTopics(ctx, profile), SCRIPT); } catch { return []; }
}
export function readCache(ctx, now = Date.now()) {
const file = join(ctx.stateDir, 'digest.md');
if (existsSync(join(ctx.stateDir, 'authorization-refused.json')) || !existsSync(file)) return null;
const text = readFileSync(file, 'utf8');
return text.trim() ? { text, age: ageDays(new Date(statSync(file).mtimeMs).toISOString(), now) } : null;
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |