matthewwong525/WongStack / .agents/plugins/memory
memory
Verifiedscripts/before-edit.mjs
View on GitHub#!/usr/bin/env node
// PreToolUse hook on file edits: the first time a session edits a file in a mapped folder, show that area's open
// threads and newest live facts. It never blocks an edit: any failure prints nothing, and it always exits 0.
import { readFileSync } from 'node:fs';
import { parseCli } from './lib/cli.mjs';
import { areasOf, loadAreas } from './lib/areas.mjs';
import { formatFact } from './lib/digest.mjs';
import { isMain, openStore, readJson, repoContext, statePath, writeJson } from './lib/store.mjs';
const BUDGET_MS = 2500;
const LINES = 8;
// The edited files: Claude Code's Edit, Write, and MultiEdit name file_path, and NotebookEdit notebook_path;
// Codex's apply_patch names each file in its patch's headers.
export function editedPaths({ tool_input: tool = {} }) {
const own = [tool.file_path, tool.notebook_path].filter(Boolean);
const patch = typeof tool.command === 'string' ? tool.command : '';
return [...own, ...[...patch.matchAll(/^\*\*\* (?:(?:Add|Update|Delete) File|Move to): (.+)$/gm)].map(([, path]) => path.trim())];
}
// The hook's output for one edit, or '' when it shows nothing.
export async function beforeEdit(input) {
const paths = editedPaths(input);
if (!paths.length) return '';
const ctx = repoContext(input.cwd || process.cwd());
const shownFile = statePath(ctx, 'shown-areas', `${String(input.session_id || 'unknown').replace(/[^\w.-]/g, '_')}.json`);
const shown = readJson(shownFile, []);
const areas = await loadAreas(ctx);
const tags = [...new Set(paths.flatMap(path => areasOf(path, areas, ctx.root)))].filter(tag => !shown.includes(tag));
if (!tags.length) return '';
const store = await openStore(ctx, { timeoutMs: BUDGET_MS });
const [facts] = await store.facts([{ tags, order: 'threads-first', limit: LINES }]);
writeJson(shownFile, [...shown, ...tags]);
if (!facts.length) return '';
const additionalContext = [`Memory for ${tags.join(', ')}:`, ...facts.map(fact => formatFact(fact))].join('\n');
return `${JSON.stringify({ hookSpecificOutput: { hookEventName: 'PreToolUse', additionalContext } })}\n`;
}
if (isMain(import.meta.url)) {
parseCli({ usage: 'usage: before-edit.mjs the PreToolUse hook on file edits; reads the hook\'s JSON on stdin' });
let out = '';
try { out = await beforeEdit(JSON.parse(readFileSync(0, 'utf8') || '{}')); } catch { /* never blocks an edit */ }
// Exit once the output is flushed: an aborted fetch's socket would otherwise hold the hook past its timeout.
process.stdout.write(out, () => process.exit(0));
}
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 |