.md file to compare - side-by-side diff against distill
distill
description: "Triggers on prompt mention of 'distill'."
What it does for you
Turns your daily notes into a tidy record of what's true and where it came from.
What it produces
A recent result, so you can see the kind of work it returns.
loading…
How to get it
These run inside the Snappy workspace. Want this working in your business? I set skills like this up with you, in one focused week.
For developers how this skill is built, graded, and how it runs
at a glance- the short version
what's inside - the parts that make up a skill 3/4 present
A skill is just a few plain-text files. Only the main one is required. The rest are optional, added as the work needs them. This is what the skill is made of; how it runs is just below.
state/skills/distill/SKILL.md
present
state/lib/distill.ts
present
state/bin/distill/
not present
state/skills/distill/AGENTS.md
present
how it's graded - what counts as a good run 4 criteria · 3 deterministic · 1 judge
Each row is one thing a good run has to get right. deterministic means a quick check decides, pass or fail. judge means the AI reads the result and rates it. Grading each piece on its own (instead of one overall score) shows exactly where a run fell short, so the fix is obvious.
how it runs - the shared frame every skill uses 5/5 present
Every skill runs the same way. One part does the work, a separate part checks it, and a short loader hands the AI exactly what it needs for the job. Anything this skill doesn't use shows a one-line note saying why, on purpose, not by accident.
state/log/evals.ndjson - Every observation MUST carry at least one source path. A claim without sources[] is banned — the UI refuses to render it. That is the whole point of the skill.
- NEVER write observations.md by hand or from a skill other than distill. It is overwritten on every run; hand-edits vanish. Journal entries are the right surface for hand-edits.
- NEVER invent frequencies like "last 4 Fridays" unless the heuristic that computed it actually walked the daily files and the Friday count is sources[].length.
- Heuristic window default = 14 days. Widening is fine, narrowing below 7 is almost always wrong (too much noise, not enough signal).
- A sidecar that calls distill() MUST read the result back via parseObservations() as its audit step. Actor ≠ auditor.
what it has learned - fixes written back in over time sample
When a run hits something this skill didn't handle, the fix gets written back into the skill so it doesn't happen again. FIXED means it was corrected on the spot. LOGGED means it's queued for a bigger rewrite. Either way, the skill gets a little better and never makes the same mistake twice.
- Loading feedback rows…
how the work flows- who makes it, who checks it
import { distill, parseObservations } from "../lib/distill.ts"
SKILL.md- the skill, written out in plain English
Backed by: state/lib/distill.ts
distill
The curated side of the through-line layer. Raw daily notes (state/memory/YYYY-MM-DD.md) → state/observations.md.
Scope (2026-04-23 cut): memory is human-scale only. Journal lines are the input; every observation sources at least one daily-note path. Frictions (state/log/frictions.ndjson) and breakage trends (state/log/evals.ndjson) are automation fuel for pid-regen and the other lints that consume them - surfacing them as "memory" was double-shouting work the PID loop already drains, and turned the Memory screen into log noise.
Every observation carries its sources. The UI ASK lane reads this file; an ASK with no source path does not render. If it's in observations.md, you can click through to the daily-note line that produced it.
Steps
- Read
state/memory/*.mdwithin the window (default 14 days). - Run the journal-verb heuristic: count entries matching
rejected|approved|edited|decided|surprised|corrected. ≥2 hits ⇒ an observation with the matching daily-note files as sources. - Read the prior snapshot (latest file in
state/observations/) and tag each current observation:new(id absent from prior),drifted(id present, claim text changed - usually the count went up),stable(unchanged),resolved(was present, now gone). - Sort by status (new → drifted → stable → resolved), then confidence. Write
state/observations.mdandstate/observations/<today>.md(the next run's prior). - Log one eval row (
skill: "distill") tostate/log/evals.ndjson: shape check = the written file parses back viaparseObservations()withobservations.length >= 0and every claim has ≥1 source.
Eval
Actor: the heuristic pass that writes the file. Auditor: re-read via parseObservations(); fail if any observation has zero sources or empty claim. That's the actor≠auditor split.
Known pitfalls
- If you add a new heuristic, it MUST populate
sources[]fromstate/memory/. A claim without a daily-note source is banned - the whole design is to make ASKS unfakeable. - Do not re-introduce friction/breakage clusters here. Those are pid-regen's input, not memory. They were removed 2026-04-23 because Robert pointed out the Memory screen had become a dashboard of automation counts instead of human decisions. If you think clustering frictions here would be useful, write a different skill (
breakage-report,friction-trends) - don't smuggle them back into memory. - Don't hand-edit
observations.md. It's overwritten every run. Hand-edits go in the daily notes. - The window default is 14 days. Widen with
--days 30for pattern drift questions.
Rubric
criteria:
- name: output_file_exists
kind: deterministic
check: "A file named 'state/observations.md' exists after skill execution."
- name: parseable_and_sourced
kind: deterministic
check: "Parsing 'state/observations.md' via 'parseObservations()' results in an array where every observation has 'observations.length >= 0' and 'claim.sources.length >= 1'."
- name: eval_log_entry
kind: deterministic
check: "A new entry with 'skill: \"distill\"' exists in 'state/log/evals.ndjson' after skill execution, with a valid shape check result."
- name: observation_relevance
kind: judge
check: "Observations recorded in 'state/observations.md' are relevant to human-scale memory and do not re-introduce friction/breakage clusters."AGENTS.md- what the AI loads when this skill comes up
distill - loader
Per-turn rules for the distill skill. Full reference: state/skills/distill/SKILL.md. Do not skip these.
Critical Rules
- Every observation MUST carry at least one source path. A claim without
sources[]is banned - the UI refuses to render it. That is the whole point of the skill. - NEVER write observations.md by hand or from a skill other than distill. It is overwritten on every run; hand-edits vanish. Journal entries are the right surface for hand-edits.
- NEVER invent frequencies like "last 4 Fridays" unless the heuristic that computed it actually walked the daily files and the Friday count is
sources[].length. - Heuristic window default = 14 days. Widening is fine, narrowing below 7 is almost always wrong (too much noise, not enough signal).
- A sidecar that calls
distill()MUST read the result back viaparseObservations()as its audit step. Actor ≠ auditor.
Commands
| ui dashboard | state/skills/distill/resources/ui.openui | |invoke: import { distill, parseObservations } from "../lib/distill.ts" |cli: npx tsx state/lib/distill.ts (default 14d) · npx tsx state/lib/distill.ts --days 30 |output: state/observations.md |eval log: state/log/evals.ndjson (skill: "distill")
OpenUI Resource
- Skill-owned OpenUI Lang resource:
state/skills/distill/resources/ui.openui. Read it before rendering or editing this skill's generated component surface. - Treat this resource as a first-class artifact of the skill, not a generic chat response. Improve it when the skill's user-facing output needs to become richer.
- System resources compose OpenUI primitives and inherit SnappyChat tokens. Use
ui_contract: brandedin SKILL.md only for deliberate platform or client visuals.
Known Pitfalls
- Adding a heuristic without
sources[]: the parser will reject it in audit. Always thread the provenance through. - Calling distill on an empty memory/: observations.md will correctly say "no observations - journal is thin." That is not a bug. Use
journalto seed. - The friction cluster heuristic is area-based - area strings must match exactly. Adjacent-but-different areas don't fold together.
Self-Test
An agent reading this should correctly:
- [ ] Confirm every observation in observations.md has at least one source path before logging eval?
- [ ] Use
parseObservations()as the auditor, not re-inspect the observations it just computed? - [ ] Understand that hand-edits to observations.md are lost on next run?
Self-report
If this loader fell short, append a line:
echo "[$(date -u +%FT%TZ)] distill: <what was missing>" >> state/log/loader-feedback.log
<!-- kernel-ok: historical attribution for the PID-loader pattern. -->
Self-correcting loader (PID feedback)
This is the rule that makes loaders learn. Ported from snappy-kernel, where it demonstrably worked: the snappy-dom-cartographer skill mapped Skool admin UI once, every later ship-agent inherited the full map for free.
Before you finish, do two things:
1. Fix gaps (P - proportional). MANDATORY. If this loader didn't cover your case - if you had to read another file, run an undocumented command, work around a wrong selector, or discover a quirk - you MUST attempt an Edit to this AGENTS.md before you log.
- Default action: edit this file. Surgical. Add the missing line, table
row, or code example where it belongs. No restructuring.
- Only
LOGGEDis allowed when: the fix needs >10 lines, spans multiple
files, or requires a structural rewrite. In that case the state/regen/drain.sh queue picks it up asynchronously.
- "I didn't have time" / "it's minor" / "the next agent will figure it out"
are NOT valid reasons. Minor gaps compound - that's the whole point of this loop.
- Concrete example of a valid P-fix: discover
agent-browser screenshot
takes path positionally not as --output <path>. One line in a table. Edit it in. Do not just log it.
- The goal: the next agent never has to leave the loader.
2. Log the result.
echo "[$(date -u +%FT%TZ)] distill: <what was missing or fixed> [FIXED|LOGGED]" >> state/log/loader-feedback.log
FIXED= you patched this loader inline (P-fix).LOGGED= too large for inline; the PostToolUse enqueue + Stop-hook drain
will rewrite the loader from scratch on next session-end.
Do not skip this. Every agent run must leave the system better than it found it. The loader is the setpoint; you are the sensor; the gap is the error signal; closing the gap is the correction.
api.ts- the code it can call
#!/usr/bin/env npx tsx
/**
* state/lib/distill.ts — scan state/memory/ journal,
* write state/observations.md with claims that carry their source.
*
* The curated side of the OpenClaw-style memory split:
* memory/YYYY-MM-DD.md = raw daily (written by journal)
* observations.md = distilled claims (written here)
*
* Every observation carries:
* - id — stable slug so the UI can key on it
* - claim — one-sentence statement
* - confidence — low|medium|high (heuristic)
* - sources[] — file paths (+ optional line) the claim derives from
* - generated_at — ISO timestamp
*
* Usage:
* npx tsx state/lib/distill.ts # scan last 14 days
* npx tsx state/lib/distill.ts --days 30 # widen window
*
* SCOPE (2026-04-23 cut): memory is human-scale only. We distill the journal.
* Breakage rows + low-score eval rows are automation fuel for `pid-regen` +
* other lints; they are NOT memory. Surfacing them here was double-shouting
* work the PID loop already drains. If you want to know what broke, read
* `state/log/breakage-report.ndjson` and the recent eval rows; if you want
* to know what the operator thought, read the journal / this file.
*/
import { readFileSync, writeFileSync, existsSync, mkdirSync, readdirSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const HERE = dirname(fileURLToPath(import.meta.url));
const REPO = resolve(HERE, "..", "..");
const MEMORY_DIR = resolve(REPO, "state", "memory");
const OUT = resolve(REPO, "state", "observations.md");
const SNAPSHOT_DIR = resolve(REPO, "state", "observations");
export type ObservationStatus = "new" | "drifted" | "stable" | "resolved";
export interface Observation {
id: string;
claim: string;
confidence: "low" | "medium" | "high";
sources: string[];
generated_at: string;
kind: "journal-pattern";
status?: ObservationStatus;
prior_claim?: string;
}
interface JournalRow {
date: string;
ts: string;
text: string;
source: string;
}
const VERB_PATTERNS: Array<{ verb: string; re: RegExp }> = [
{ verb: "rejected", re: /\brejected\b/i },
{ verb: "approved", re: /\bapproved\b/i },
{ verb: "edited", re: /\bedited\b/i },
{ verb: "decided", re: /\bdecided|decision\b/i },
{ verb: "surprised",re: /\b(surprised|unexpected|surprise)\b/i },
{ verb: "corrected",re: /\bcorrected|correction\b/i },
];
function slugify(s: string): string {
return s.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "").slice(0, 40);
}
function daysBack(date: Date, n: number): Date {
const d = new Date(date);
d.setUTCDate(d.getUTCDate() - n);
return d;
}
function isoDate(d: Date): string {
return d.toISOString().slice(0, 10);
}
function readJournalWindow(windowDays: number): JournalRow[] {
if (!existsSync(MEMORY_DIR)) return [];
const today = new Date();
const from = daysBack(today, windowDays - 1);
const rows: JournalRow[] = [];
const files = readdirSync(MEMORY_DIR)
.filter(f => /^\d{4}-\d{2}-\d{2}\.md$/.test(f))
.sort();
for (const f of files) {
const date = f.slice(0, 10);
const d = new Date(date + "T00:00:00Z");
if (d < from) continue;
const body = readFileSync(resolve(MEMORY_DIR, f), "utf8");
const lines = body.split(/\r?\n/);
for (const line of lines) {
const m = line.match(/^- `([^`]+)` (.+)$/);
if (!m) continue;
rows.push({ date, ts: m[1], text: m[2], source: `state/memory/${f}` });
}
}
return rows;
}
function groupJournalByVerb(rows: JournalRow[]): Observation[] {
const out: Observation[] = [];
for (const { verb, re } of VERB_PATTERNS) {
const hits = rows.filter(r => re.test(r.text));
if (hits.length < 2) continue;
const sources = [...new Set(hits.map(h => h.source))];
out.push({
id: `journal-verb-${verb}`,
kind: "journal-pattern",
claim: `${hits.length} journal entries in window use "${verb}" — recurring theme.`,
confidence: hits.length >= 4 ? "high" : hits.length >= 3 ? "medium" : "low",
sources,
generated_at: new Date().toISOString(),
});
}
return out;
}
function render(obs: Observation[], windowDays: number): string {
const lines: string[] = [];
lines.push(`# observations.md`);
lines.push("");
lines.push(`Generated: ${new Date().toISOString()}`);
lines.push(`Window: last ${windowDays} days`);
lines.push(`Count: ${obs.length}`);
const byStatus = obs.reduce<Record<string, number>>((m, o) => { const s = o.status || "new"; m[s] = (m[s] || 0) + 1; return m; }, {});
lines.push(`Status: new=${byStatus.new||0} drifted=${byStatus.drifted||0} stable=${byStatus.stable||0} resolved=${byStatus.resolved||0}`);
lines.push("");
lines.push(`> Curated claims with provenance. Written by \`state/lib/distill.ts\`.`);
lines.push(`> Raw journal: \`state/memory/YYYY-MM-DD.md\`. Do not hand-edit this file — rerun distill.`);
lines.push(`> Snapshots: \`state/observations/YYYY-MM-DD.md\` (one per run, source of the delta).`);
lines.push("");
if (obs.length === 0) {
lines.push(`_No observations — journal is thin or the window is too tight. Use the \`journal\` skill to log rejections, decisions, surprises._`);
lines.push("");
return lines.join("\n");
}
for (const o of obs) {
lines.push(`## ${o.id}`);
lines.push("");
lines.push(`- **claim:** ${o.claim}`);
lines.push(`- **kind:** ${o.kind}`);
lines.push(`- **confidence:** ${o.confidence}`);
lines.push(`- **status:** ${o.status || "new"}`);
if (o.prior_claim && o.status === "drifted") {
lines.push(`- **prior_claim:** ${o.prior_claim}`);
}
lines.push(`- **generated_at:** ${o.generated_at}`);
lines.push(`- **sources:**`);
for (const s of o.sources) lines.push(` - \`${s}\``);
lines.push("");
}
return lines.join("\n");
}
function readPriorSnapshot(): Map<string, Observation> {
const map = new Map<string, Observation>();
if (!existsSync(SNAPSHOT_DIR)) return map;
const files = readdirSync(SNAPSHOT_DIR)
.filter(f => /^\d{4}-\d{2}-\d{2}\.md$/.test(f))
.sort();
if (files.length === 0) return map;
const latest = resolve(SNAPSHOT_DIR, files[files.length - 1]);
const body = readFileSync(latest, "utf8");
const blocks = body.split(/^## /m).slice(1);
for (const block of blocks) {
const [header, ...rest] = block.split(/\r?\n/);
const id = header.trim();
const body = rest.join("\n");
const claim = (body.match(/- \*\*claim:\*\* (.+)/)?.[1] ?? "").trim();
const confidence = (body.match(/- \*\*confidence:\*\* (low|medium|high)/)?.[1] ?? "low") as "low"|"medium"|"high";
const kind = (body.match(/- \*\*kind:\*\* ([\w-]+)/)?.[1] ?? "journal-pattern") as Observation["kind"];
map.set(id, { id, claim, confidence, kind, sources: [], generated_at: "" });
}
return map;
}
function annotateStatus(current: Observation[], prior: Map<string, Observation>): Observation[] {
const out: Observation[] = [];
const currentIds = new Set(current.map(o => o.id));
for (const o of current) {
const p = prior.get(o.id);
if (!p) {
out.push({ ...o, status: "new" });
} else if (p.claim !== o.claim) {
out.push({ ...o, status: "drifted", prior_claim: p.claim });
} else {
out.push({ ...o, status: "stable" });
}
}
// carry forward disappeared as "resolved" so the operator sees them fade
for (const [id, p] of prior.entries()) {
if (currentIds.has(id)) continue;
out.push({
id,
claim: p.claim,
confidence: p.confidence,
kind: p.kind,
sources: [],
generated_at: new Date().toISOString(),
status: "resolved",
prior_claim: p.claim,
});
}
return out;
}
function writeSnapshot(obs: Observation[], windowDays: number): string {
if (!existsSync(SNAPSHOT_DIR)) mkdirSync(SNAPSHOT_DIR, { recursive: true });
const date = new Date().toISOString().slice(0, 10);
const path = resolve(SNAPSHOT_DIR, `${date}.md`);
writeFileSync(path, render(obs, windowDays), "utf8");
return path;
}
export function distill(windowDays = 14): { observations: Observation[]; path: string; snapshot: string } {
const journalRows = readJournalWindow(windowDays);
const fresh: Observation[] = [
...groupJournalByVerb(journalRows),
];
const prior = readPriorSnapshot();
const annotated = annotateStatus(fresh, prior);
// Sort: new + drifted first (most interesting), then stable, then resolved. Within each, confidence descending.
const statusRank: Record<string, number> = { new: 0, drifted: 1, stable: 2, resolved: 3 };
const confRank: Record<string, number> = { high: 0, medium: 1, low: 2 };
annotated.sort((a, b) => {
const s = statusRank[a.status || "new"] - statusRank[b.status || "new"];
if (s !== 0) return s;
return confRank[a.confidence] - confRank[b.confidence];
});
if (!existsSync(dirname(OUT))) mkdirSync(dirname(OUT), { recursive: true });
writeFileSync(OUT, render(annotated, windowDays), "utf8");
const snapshot = writeSnapshot(annotated, windowDays);
return { observations: annotated, path: OUT, snapshot };
}
/**
* Parse observations.md back into structured Observation records.
* The UI endpoint uses this so the fake-ASK risk is gone: what the UI shows
* is what the file says.
*/
export function parseObservations(): Observation[] {
if (!existsSync(OUT)) return [];
const body = readFileSync(OUT, "utf8");
const blocks = body.split(/^## /m).slice(1);
const out: Observation[] = [];
for (const block of blocks) {
const [header, ...rest] = block.split(/\r?\n/);
const id = header.trim();
const body = rest.join("\n");
const claim = (body.match(/- \*\*claim:\*\* (.+)/)?.[1] ?? "").trim();
const kindRaw = (body.match(/- \*\*kind:\*\* (.+)/)?.[1] ?? "journal-pattern").trim();
const confidence = (body.match(/- \*\*confidence:\*\* (low|medium|high)/)?.[1] ?? "low") as "low"|"medium"|"high";
const generated_at = (body.match(/- \*\*generated_at:\*\* (.+)/)?.[1] ?? "").trim();
const statusRaw = (body.match(/- \*\*status:\*\* (new|drifted|stable|resolved)/)?.[1] ?? "") as ObservationStatus | "";
const prior_claim = (body.match(/- \*\*prior_claim:\*\* (.+)/)?.[1] ?? "").trim();
const sources: string[] = [];
const srcBlock = body.split(/- \*\*sources:\*\*/)[1] ?? "";
for (const line of srcBlock.split(/\r?\n/)) {
const m = line.match(/^\s*-\s*`([^`]+)`\s*$/);
if (m) sources.push(m[1]);
}
const kind = "journal-pattern" as Observation["kind"];
void kindRaw;
const rec: Observation = { id, claim, kind, confidence, sources, generated_at };
if (statusRaw) rec.status = statusRaw;
if (prior_claim) rec.prior_claim = prior_claim;
out.push(rec);
}
return out;
}
if (import.meta.url === `file://${process.argv[1]}`) {
const idx = process.argv.indexOf("--days");
const windowDays = idx >= 0 ? Math.max(1, parseInt(process.argv[idx + 1], 10) || 14) : 14;
const { observations, path } = distill(windowDays);
process.stdout.write(`ok: ${path}\ncount: ${observations.length}\nwindow_days: ${windowDays}\n`);
// eval row: shape check. Actor = the distill() run that wrote the file.
// Auditor = re-read + parse, confirm every observation has ≥1 source.
const { append, newRunId } = await import("./log.ts");
const reread = parseObservations();
const sourceless = reread.filter((o) => !o.sources || o.sources.length === 0);
const run_id = newRunId();
append("evals", {
skill: "distill",
run_id,
mode: "shape",
score: sourceless.length === 0 ? 1 : 0,
count: reread.length,
sourceless: sourceless.length,
window_days: windowDays,
actor_session_id: `distill-cli-${run_id}`,
auditor_session_id: `distill-shape-${Date.now()}`,
writer_id: "state/lib/distill.ts",
notes: sourceless.length === 0 ? "shape-ok" : `${sourceless.length} observations missing sources`,
});
}
scripts- helper scripts it can run
prose-only skill - 1 inline code block live in SKILL.md above (no state/bin/ sidecar yet).
how we check it- the checks, plus the last 10 runs
| timestamp | verb | score | primary_issue | artifact |
|---|---|---|---|---|
| 2026-04-27 05:53Z | - | 1.00 | - | - |
| 2026-04-25 04:11Z | - | 1.00 | - | - |
| 2026-04-24 06:03Z | - | 1.00 | - | - |
| 2026-04-27 05:53Z | - | 1.00 | - | - |
| 2026-04-25 04:11Z | - | 1.00 | - | - |
| 2026-04-24 06:03Z | - | 1.00 | - | - |
| 2026-04-27 05:53Z | - | 1.00 | - | - |
| 2026-04-25 04:11Z | - | 1.00 | - | - |
| 2026-04-24 06:03Z | - | 1.00 | - | - |
| 2026-04-25 04:11Z | - | 1.00 | - | - |