mirror of
https://github.com/lukaszraczylo/claude-adam.git
synced 2026-06-05 22:49:28 +00:00
chore(v0.3.3): analyst observability, A/B measurement, journal hygiene
Storage/window/exclusion split (#7): ISO-week journal rotation with safety fuse replaces size-based rotation (fixes silent under-counting when clusters straddle boundaries). Per-signal sliding windows via adam-window.mjs guard against stale signal accumulation. Legacy YYYY-MM-DD-<ts>.jsonl files remain readable. Error fingerprint normalization (#3): adam-observe.mjs extracts canonical error codes (ENOENT, ECONNREFUSED, etc.) and normalizes paths/timestamps/hex before hashing. 'Connection refused' and 'ECONNREFUSED' now cluster identically. Correction corpus expansion (#1): strong tokens (stop, wrong, undo, try again, different approach, etc.) fire on any occurrence. Weak tokens (no, actually, wait) require negation/contrast co-occurrence within 8 tokens. Kills the 'actually, I think...' false positive. Analyst observability (#6): mandatory clustering trace block; adam-explain.mjs parses to summary/full/json. Cluster decisions now surface rejection reasons (threshold, contradiction, window). Persisted to ~/.claude/adam/last-trace.txt. Dead_end nudge proposal type (#2): single-session auto-apply gate (>=3 dead_end events). Action appends to active-nudges.json, surfaced via adam-nudge.mjs at next SessionStart. Lower blast than skill_edit. Per-(skill, fingerprint) cooldown (#4): adam-cooldown.mjs replaces coarse per-skill check. proposal_fingerprint = djb2(skill_slug + cluster_id + normalized_diff_body). Legacy applied/rejected records gate via 'legacy' fingerprint fallback through resolveSkill helper (handles target_skill, skill, or target: <path>). task_completed scoring integration (#8): adam-score.mjs computes per-session urgency dampener (3 task_completed -> 0.5) and reinforcement candidates (skills cited in >=3 clean completions). New 'reinforcement' proposal type appends to reinforcements.jsonl on apply (no code/memory mutation). A/B effectiveness measurement (#5): every auto-applied edit appends to ab-tracking.jsonl. adam-ab-measure.mjs computes 7d pre/post signal-count delta per entry (improved / neutral / regressed / no_baseline / pending). Analyst surfaces regressions at top of /reflect output. Upgrade UX overhaul (#9): adam-upgrade.mjs implements --list/--diff/--accept /--accept-all. SessionStart nudge prints pending-merge warning when .adam-new files exist (latency ~20ms via fixed shortlist). install.sh emits unmissable final-message hint after creating any .adam-new file. Simplify pass: adam-utils.mjs deduplicates readJsonlSafe / listJsonlFiles / parseFrontmatter across 8 scripts. Net -46 LOC. Test coverage: 30 -> 87 tests. Every new feature has feature-validating assertions (false-case coverage included). T77 statically verifies install.sh references every adam-*.mjs source script (would have caught the missing adam-utils inclusion that review #2 surfaced).
This commit is contained in:
@@ -4,6 +4,8 @@ Self-improvement layer for [Claude Code](https://claude.com/claude-code) that ob
|
||||
|
||||
## What's new
|
||||
|
||||
- **v0.3.3** — analyst observability, A/B measurement, journal hygiene. Storage/window/exclusion split: ISO-week journal rotation with safety fuse (replaces size-based, fixes silent under-counting); per-signal sliding windows via new `adam-window.mjs` (`dead_end` 7d, `correction` 30d, reinforcement signals 60d). Error fingerprint normalization — `ECONNREFUSED` and `"Connection refused"` cluster identically. Correction corpus expanded (`wait`, `hold on`, `try again`, `different approach`); weak tokens (`no`, `actually`, `wait`) require negation co-occurrence within 8 tokens to fire — kills the `"actually, I think..."` false positive. Mandatory clustering trace + new `adam-explain.mjs --mode summary|full|json`. New `nudge` proposal type (single-session auto-apply, low blast) for repeated `dead_end`. Per-(skill, fingerprint) cooldown via `adam-cooldown.mjs` (replaces coarse per-skill gate). `task_completed` scoring: urgency dampener + reinforcement candidates. A/B effectiveness measurement on auto-applied edits (`adam-ab-measure.mjs`, 7d pre/post window). Upgrade UX overhaul: `adam-upgrade.mjs --list/--diff/--accept` + SessionStart pending-merge warning. Shared helper module `adam-utils.mjs` deduplicates journal-reading and frontmatter parsing across scripts. 87 tests (up from 30).
|
||||
- **v0.3.2** — `task_completed` signal: post-task skill capture for downstream reinforcement scoring (consumed in v0.3.3).
|
||||
- **v0.3.1** — code review pass: bug fixes (`errorFingerprint` no longer false-positives on `is_error: false`, archive script handles same-millisecond duplicates correctly, `tool_window` now clears on session change, nudge filters proposal filenames by pattern), prose conciseness cuts, hardened `install.sh` with curl one-liner + settings.json merge, `adam-uninstall.sh`, isolated test harness (no longer pollutes live `~/.claude/adam/` state).
|
||||
- **v0.3.0** — causal diagnosis: every proposal carries a `# Diagnosis` block (Trigger/Action/Mismatch/Outcome with verbatim transcript quote) before drafting, plus optional `contradiction_flag` heuristic that vetoes auto-apply on obviously-conflicting `skill_edit` additions.
|
||||
- **v0.2.1** — win signals (`correction_free_streak`, `clean_recovery`) feed `skill_edit` auto-apply under a strict gate (≤30 LOC, ≤2× byte cap, 7d cooldown, 30d blacklist on rejection).
|
||||
|
||||
Executable
+190
@@ -0,0 +1,190 @@
|
||||
#!/usr/bin/env node
|
||||
// adam-ab-measure.mjs — A/B effectiveness measurement on auto-applied edits.
|
||||
//
|
||||
// Reads ~/.claude/adam/ab-tracking.jsonl (one line per auto-apply event,
|
||||
// written by adam-self-improvement/SKILL.md), then for each entry old enough
|
||||
// (>= --min-age-days; default 7) compares signal counts in the 7-day window
|
||||
// BEFORE applied_at against the 7-day window AFTER applied_at across the
|
||||
// full journal corpus (active + rotated). Surfaces regressions so /reflect
|
||||
// can flag proposals that made things worse.
|
||||
//
|
||||
// CLI:
|
||||
// adam-ab-measure.mjs [--home <path>] [--format json|table] [--min-age-days N]
|
||||
//
|
||||
// Output (default `table`): aligned columns sorted regressed-first.
|
||||
// Output (`json`): array of deltas.
|
||||
// Empty / missing tracking file → empty output, exit 0.
|
||||
// Exit 1 only on I/O failure.
|
||||
|
||||
import { join } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { readJsonlSafe, listJsonlFiles } from "./adam-utils.mjs";
|
||||
|
||||
const DAY_MS = 86400000;
|
||||
export const DEFAULT_PRE_WINDOW_DAYS = 7;
|
||||
export const DEFAULT_MIN_AGE_DAYS = 7;
|
||||
|
||||
const REGRESSED_PCT = 25;
|
||||
const IMPROVED_PCT = -25;
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = { home: null, format: "table", minAgeDays: DEFAULT_MIN_AGE_DAYS, help: false };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--home" && i + 1 < argv.length) args.home = argv[++i];
|
||||
else if (a === "--format" && i + 1 < argv.length) args.format = argv[++i];
|
||||
else if (a === "--min-age-days" && i + 1 < argv.length) {
|
||||
const n = Number(argv[++i]);
|
||||
if (!Number.isNaN(n) && n >= 0) args.minAgeDays = n;
|
||||
}
|
||||
else if (a === "--help" || a === "-h") args.help = true;
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
function loadJournalAll(claudeHome) {
|
||||
const adamRoot = join(claudeHome, "adam");
|
||||
const sources = [join(adamRoot, "journal.jsonl"), ...listJsonlFiles(join(adamRoot, "journal"))];
|
||||
const all = [];
|
||||
for (const p of sources) for (const e of readJsonlSafe(p)) all.push(e);
|
||||
return all;
|
||||
}
|
||||
|
||||
function tsMs(e) {
|
||||
if (!e || typeof e.ts !== "string") return NaN;
|
||||
return Date.parse(e.ts);
|
||||
}
|
||||
|
||||
// computeDeltas: pure function — entries = ab-tracking objects, journal = list
|
||||
// of journal entries (any source). opts.now is unix ms; opts.minAgeDays is the
|
||||
// floor for non-pending.
|
||||
export function computeDeltas(entries, journal, opts = {}) {
|
||||
const now = typeof opts.now === "number" ? opts.now : Date.now();
|
||||
const minAgeDays = typeof opts.minAgeDays === "number" ? opts.minAgeDays : DEFAULT_MIN_AGE_DAYS;
|
||||
const out = [];
|
||||
for (const e of entries || []) {
|
||||
if (!e || typeof e !== "object") continue;
|
||||
const appliedAt = Number(e.applied_at);
|
||||
if (!appliedAt || Number.isNaN(appliedAt)) continue;
|
||||
const ageDays = (now - appliedAt) / DAY_MS;
|
||||
// Symmetric window: same span applied to pre AND post sides. JSONL schema
|
||||
// field stays `pre_window_days` for backward compat with existing
|
||||
// ab-tracking.jsonl entries — local name reflects symmetry.
|
||||
const windowDays = typeof e.pre_window_days === "number" ? e.pre_window_days : DEFAULT_PRE_WINDOW_DAYS;
|
||||
const signals = Array.isArray(e.originating_signals)
|
||||
? e.originating_signals.map((s) => (s && typeof s === "object" ? s.type : null)).filter(Boolean)
|
||||
: [];
|
||||
const sigSet = new Set(signals);
|
||||
|
||||
const base = {
|
||||
proposal_id: e.proposal_id || "",
|
||||
proposal_type: e.proposal_type || "",
|
||||
target_skill: e.target_skill || "",
|
||||
applied_at: appliedAt,
|
||||
applied_at_iso: new Date(appliedAt).toISOString(),
|
||||
signal_types: [...sigSet],
|
||||
};
|
||||
|
||||
if (ageDays < minAgeDays) {
|
||||
out.push({ ...base, pre_count: null, post_count: null, delta_pct: null, status: "pending" });
|
||||
continue;
|
||||
}
|
||||
|
||||
const preStart = appliedAt - windowDays * DAY_MS;
|
||||
const postEnd = appliedAt + windowDays * DAY_MS;
|
||||
let preCount = 0;
|
||||
let postCount = 0;
|
||||
for (const je of journal || []) {
|
||||
if (!je || typeof je !== "object") continue;
|
||||
if (!sigSet.has(je.type)) continue;
|
||||
const t = tsMs(je);
|
||||
if (Number.isNaN(t)) continue;
|
||||
if (t >= preStart && t < appliedAt) preCount++;
|
||||
else if (t >= appliedAt && t < postEnd) postCount++;
|
||||
}
|
||||
|
||||
let status;
|
||||
let deltaPct;
|
||||
if (preCount === 0) {
|
||||
status = "no_baseline";
|
||||
deltaPct = null;
|
||||
} else {
|
||||
deltaPct = ((postCount - preCount) / preCount) * 100;
|
||||
// Round to 2 dp for stable comparison + presentation.
|
||||
deltaPct = Math.round(deltaPct * 100) / 100;
|
||||
if (deltaPct <= IMPROVED_PCT) status = "improved";
|
||||
else if (deltaPct >= REGRESSED_PCT) status = "regressed";
|
||||
else status = "neutral";
|
||||
}
|
||||
out.push({ ...base, pre_count: preCount, post_count: postCount, delta_pct: deltaPct, status });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
const STATUS_ORDER = { regressed: 0, neutral: 1, no_baseline: 2, improved: 3, pending: 4 };
|
||||
|
||||
function sortForTable(deltas) {
|
||||
return [...deltas].sort((a, b) => {
|
||||
const sa = STATUS_ORDER[a.status] ?? 99;
|
||||
const sb = STATUS_ORDER[b.status] ?? 99;
|
||||
if (sa !== sb) return sa - sb;
|
||||
return a.applied_at - b.applied_at;
|
||||
});
|
||||
}
|
||||
|
||||
function padRight(s, n) { s = String(s); return s.length >= n ? s : s + " ".repeat(n - s.length); }
|
||||
|
||||
export function formatTable(deltas) {
|
||||
if (!deltas || !deltas.length) return "";
|
||||
const rows = sortForTable(deltas);
|
||||
const headers = ["proposal_id", "target", "type", "applied_at(iso)", "pre/post", "delta%", "status"];
|
||||
const data = rows.map((d) => [
|
||||
d.proposal_id || "-",
|
||||
d.target_skill || "-",
|
||||
d.proposal_type || "-",
|
||||
d.applied_at_iso || "-",
|
||||
d.pre_count == null ? "-" : `${d.pre_count}/${d.post_count}`,
|
||||
d.delta_pct == null ? "-" : `${d.delta_pct.toFixed(2)}`,
|
||||
d.status || "-",
|
||||
]);
|
||||
const widths = headers.map((h, i) => Math.max(h.length, ...data.map((r) => String(r[i]).length)));
|
||||
const lines = [];
|
||||
lines.push(headers.map((h, i) => padRight(h, widths[i])).join(" | "));
|
||||
lines.push(widths.map((w) => "-".repeat(w)).join("-+-"));
|
||||
for (const r of data) lines.push(r.map((c, i) => padRight(c, widths[i])).join(" | "));
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
export function formatJson(deltas) {
|
||||
return JSON.stringify(deltas || []);
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
if (args.help) {
|
||||
process.stdout.write("usage: adam-ab-measure.mjs [--home <path>] [--format json|table] [--min-age-days N]\n");
|
||||
process.exit(0);
|
||||
}
|
||||
const claudeHome = args.home || join(homedir(), ".claude");
|
||||
const trackingPath = join(claudeHome, "adam", "ab-tracking.jsonl");
|
||||
try {
|
||||
const entries = readJsonlSafe(trackingPath);
|
||||
if (!entries.length) {
|
||||
if (args.format === "json") process.stdout.write("[]\n");
|
||||
// table mode prints nothing on empty input — exit 0.
|
||||
process.exit(0);
|
||||
}
|
||||
const journal = loadJournalAll(claudeHome);
|
||||
const deltas = computeDeltas(entries, journal, { minAgeDays: args.minAgeDays });
|
||||
const out = args.format === "json" ? formatJson(deltas) : formatTable(deltas);
|
||||
if (out) process.stdout.write(out + "\n");
|
||||
process.exit(0);
|
||||
} catch (e) {
|
||||
process.stderr.write(`adam-ab-measure error: ${e.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
main();
|
||||
}
|
||||
Executable
+91
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env node
|
||||
// adam-apply-reinforcement.mjs — apply-path for `reinforcement` proposals.
|
||||
//
|
||||
// Reads a proposal markdown file, validates the apply gate
|
||||
// (confidence >= 4 AND blast_radius == "low" AND type == "reinforcement"),
|
||||
// and on success appends one JSON line to ~/.claude/adam/reinforcements.jsonl
|
||||
// of shape `{ts, skill_slug, count, source_session}`.
|
||||
//
|
||||
// CLI: adam-apply-reinforcement.mjs <proposal-path> [--home <path>]
|
||||
// Output: JSON one-liner on stdout: {"status":"applied"|"gated", "reason":"..."}
|
||||
// Exit: 0 on apply, 0 on gated, 1 on I/O or parse error.
|
||||
//
|
||||
// SKILL.md invokes this in the auto-apply path when the proposal type is
|
||||
// `reinforcement`. No code/memory/skill modifications.
|
||||
|
||||
import { readFileSync, appendFileSync, existsSync, mkdirSync } from "node:fs";
|
||||
import { join, dirname } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { parseFrontmatter } from "./adam-utils.mjs";
|
||||
|
||||
// Re-exported for backward compat — callers historically imported it from here.
|
||||
export { parseFrontmatter };
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = { home: null, path: null, help: false };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--home" && i + 1 < argv.length) args.home = argv[++i];
|
||||
else if (a === "--help" || a === "-h") args.help = true;
|
||||
else if (!args.path && !a.startsWith("--")) args.path = a;
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
export function checkGate(fm) {
|
||||
if ((fm.type || "") !== "reinforcement") {
|
||||
return { ok: false, reason: `type != reinforcement (got: ${fm.type || "<none>"})` };
|
||||
}
|
||||
const conf = Number(fm.confidence);
|
||||
if (Number.isNaN(conf) || conf < 4) {
|
||||
return { ok: false, reason: `confidence < 4 (got: ${fm.confidence ?? "<none>"})` };
|
||||
}
|
||||
if ((fm.blast_radius || "").toLowerCase() !== "low") {
|
||||
return { ok: false, reason: `blast_radius != low (got: ${fm.blast_radius || "<none>"})` };
|
||||
}
|
||||
if (!fm.skill_slug) {
|
||||
return { ok: false, reason: "skill_slug missing in frontmatter" };
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
export function buildEntry(fm, now = Date.now()) {
|
||||
return {
|
||||
ts: now,
|
||||
skill_slug: String(fm.skill_slug),
|
||||
count: Number(fm.count) || 0,
|
||||
source_session: fm.source_session || "",
|
||||
};
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
if (args.help || !args.path) {
|
||||
process.stdout.write("usage: adam-apply-reinforcement.mjs <proposal-path> [--home <path>]\n");
|
||||
process.exit(args.help ? 0 : 1);
|
||||
}
|
||||
const claudeHome = args.home || join(homedir(), ".claude");
|
||||
const outPath = join(claudeHome, "adam", "reinforcements.jsonl");
|
||||
try {
|
||||
const content = readFileSync(args.path, "utf8");
|
||||
const fm = parseFrontmatter(content);
|
||||
const gate = checkGate(fm);
|
||||
if (!gate.ok) {
|
||||
process.stdout.write(JSON.stringify({ status: "gated", reason: gate.reason }) + "\n");
|
||||
process.exit(0);
|
||||
}
|
||||
const entry = buildEntry(fm);
|
||||
const dir = dirname(outPath);
|
||||
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
||||
appendFileSync(outPath, JSON.stringify(entry) + "\n");
|
||||
process.stdout.write(JSON.stringify({ status: "applied", path: outPath }) + "\n");
|
||||
process.exit(0);
|
||||
} catch (e) {
|
||||
process.stderr.write(`adam-apply-reinforcement error: ${e.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
main();
|
||||
}
|
||||
@@ -8,49 +8,12 @@
|
||||
import { readFileSync, writeFileSync, appendFileSync, mkdirSync, existsSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { parseFrontmatter } from "./adam-utils.mjs";
|
||||
|
||||
const ROOT = join(homedir(), ".claude", "adam");
|
||||
const JOURNAL = join(ROOT, "journal.jsonl");
|
||||
const JOURNAL_DIR = join(ROOT, "journal");
|
||||
|
||||
function parseFrontmatter(content) {
|
||||
const m = content.match(/^---\n([\s\S]*?)\n---/);
|
||||
if (!m) return {};
|
||||
const fm = {};
|
||||
const lines = m[1].split("\n");
|
||||
let i = 0;
|
||||
while (i < lines.length) {
|
||||
const line = lines[i];
|
||||
const idx = line.indexOf(":");
|
||||
if (idx === -1) { i++; continue; }
|
||||
const key = line.slice(0, idx).trim();
|
||||
const value = line.slice(idx + 1).trim();
|
||||
if (key === "source_entries") {
|
||||
const arr = [];
|
||||
if (value.startsWith("[") && value.endsWith("]")) {
|
||||
const inner = value.slice(1, -1)
|
||||
.split(",")
|
||||
.map(s => s.trim().replace(/^['"]|['"]$/g, ""));
|
||||
arr.push(...inner.filter(Boolean));
|
||||
fm[key] = arr;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
i++;
|
||||
while (i < lines.length && /^\s*-\s+/.test(lines[i])) {
|
||||
const item = lines[i].replace(/^\s*-\s+/, "").trim().replace(/^['"]|['"]$/g, "");
|
||||
if (item) arr.push(item);
|
||||
i++;
|
||||
}
|
||||
fm[key] = arr;
|
||||
continue;
|
||||
}
|
||||
fm[key] = value;
|
||||
i++;
|
||||
}
|
||||
return fm;
|
||||
}
|
||||
|
||||
function main() {
|
||||
const proposalPath = process.argv[2];
|
||||
if (!proposalPath) {
|
||||
|
||||
Executable
+191
@@ -0,0 +1,191 @@
|
||||
#!/usr/bin/env node
|
||||
// adam-cooldown.mjs — per-(skill, proposal_fingerprint) cooldown / blacklist
|
||||
// gate. Replaces the previous coarse per-skill cooldown.
|
||||
//
|
||||
// CLI:
|
||||
// adam-cooldown.mjs --skill <slug> --fingerprint <hash> [--home <path>]
|
||||
//
|
||||
// Output: JSON one-liner with shape
|
||||
// { "status": "cool"|"cooldown"|"blacklisted",
|
||||
// "reason": "<human-readable reason>",
|
||||
// "blocked_by": { "file": "<basename>", "days_remaining": <int> } | null }
|
||||
//
|
||||
// Rules:
|
||||
// - applied/*.md with target_skill == <skill> AND
|
||||
// (proposal_fingerprint == <fingerprint> OR missing/legacy)
|
||||
// within 7 days of `applied_at` → "cooldown"
|
||||
// - rejected/*.md with same skill match AND
|
||||
// auto_apply_blacklist: true within 30 days of applied_at → "blacklisted"
|
||||
// - else "cool"
|
||||
//
|
||||
// Backward compat: proposals without `proposal_fingerprint` field are treated
|
||||
// as fingerprint == "legacy" so historical applied/rejected records still
|
||||
// produce coarse-grained gating until they age out of their windows.
|
||||
|
||||
import { readFileSync, readdirSync, existsSync, statSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { parseFrontmatter } from "./adam-utils.mjs";
|
||||
|
||||
export const COOLDOWN_DAYS = 7;
|
||||
export const BLACKLIST_DAYS = 30;
|
||||
const DAY_MS = 86400000;
|
||||
export const LEGACY_FINGERPRINT = "legacy";
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = { home: null, skill: null, fingerprint: null, help: false };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--home" && i + 1 < argv.length) args.home = argv[++i];
|
||||
else if (a === "--skill" && i + 1 < argv.length) args.skill = argv[++i];
|
||||
else if (a === "--fingerprint" && i + 1 < argv.length) args.fingerprint = argv[++i];
|
||||
else if (a === "--help" || a === "-h") args.help = true;
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
// Pull applied_at as epoch ms. Accept ms-number, ISO string, or fall back to
|
||||
// the file's mtime so we never crash on legacy records.
|
||||
function frontmatterTimestampMs(fm, filePath) {
|
||||
const raw = fm.applied_at;
|
||||
if (raw) {
|
||||
const asNum = Number(raw);
|
||||
if (!Number.isNaN(asNum) && asNum > 0) return asNum;
|
||||
const asIso = Date.parse(raw);
|
||||
if (!Number.isNaN(asIso)) return asIso;
|
||||
}
|
||||
try { return statSync(filePath).mtimeMs; } catch { return 0; }
|
||||
}
|
||||
|
||||
function fingerprintMatches(recordFp, queryFp) {
|
||||
// Missing / empty field on legacy records → coarse fallback: any fingerprint
|
||||
// query matches (so the historical applied/rejected record still gates).
|
||||
if (!recordFp || recordFp === LEGACY_FINGERPRINT) return true;
|
||||
return recordFp === queryFp;
|
||||
}
|
||||
// Resolve a frontmatter record to its skill slug. Modern records use
|
||||
// `target_skill`; legacy v0.2.x records used `target` with a full path
|
||||
// (e.g. `skills/foo/SKILL.md`). Falls back through both before giving up.
|
||||
function resolveSkill(fm) {
|
||||
if (fm.target_skill) return fm.target_skill;
|
||||
if (fm.skill) return fm.skill;
|
||||
if (fm.target) {
|
||||
const base = fm.target.split("/").filter(Boolean);
|
||||
// skills/<slug>/SKILL.md → <slug>; <slug>.md → <slug>; else last segment
|
||||
if (base.length >= 2 && base[base.length - 1] === "SKILL.md") {
|
||||
return base[base.length - 2];
|
||||
}
|
||||
return base[base.length - 1].replace(/\.md$/, "");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function scanDir(dir, predicate) {
|
||||
if (!existsSync(dir)) return [];
|
||||
let names;
|
||||
try { names = readdirSync(dir); } catch { return []; }
|
||||
const out = [];
|
||||
for (const name of names) {
|
||||
if (!name.endsWith(".md")) continue;
|
||||
const p = join(dir, name);
|
||||
let content;
|
||||
try { content = readFileSync(p, "utf8"); } catch { continue; }
|
||||
const fm = parseFrontmatter(content);
|
||||
const hit = predicate(fm, p, name);
|
||||
if (hit) out.push(hit);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function checkCooldown(home, skill, fingerprint, now = Date.now()) {
|
||||
const adamRoot = join(home, "adam");
|
||||
const appliedDir = join(adamRoot, "applied");
|
||||
const rejectedDir = join(adamRoot, "rejected");
|
||||
|
||||
// Applied → cooldown
|
||||
const appliedHits = scanDir(appliedDir, (fm, p, name) => {
|
||||
if (resolveSkill(fm) !== skill) return null;
|
||||
if (!fingerprintMatches(fm.proposal_fingerprint, fingerprint)) return null;
|
||||
const tsMs = frontmatterTimestampMs(fm, p);
|
||||
if (!tsMs) return null;
|
||||
const ageDays = (now - tsMs) / DAY_MS;
|
||||
if (ageDays > COOLDOWN_DAYS) return null;
|
||||
return { name, daysRemaining: Math.max(0, Math.ceil(COOLDOWN_DAYS - ageDays)) };
|
||||
});
|
||||
|
||||
// Rejected → blacklisted (requires auto_apply_blacklist: true)
|
||||
const blacklistHits = scanDir(rejectedDir, (fm, p, name) => {
|
||||
if (resolveSkill(fm) !== skill) return null;
|
||||
if (!fingerprintMatches(fm.proposal_fingerprint, fingerprint)) return null;
|
||||
const flag = (fm.auto_apply_blacklist || "").toLowerCase();
|
||||
if (flag !== "true") return null;
|
||||
const tsMs = frontmatterTimestampMs(fm, p);
|
||||
if (!tsMs) return null;
|
||||
const ageDays = (now - tsMs) / DAY_MS;
|
||||
if (ageDays > BLACKLIST_DAYS) return null;
|
||||
return { name, daysRemaining: Math.max(0, Math.ceil(BLACKLIST_DAYS - ageDays)) };
|
||||
});
|
||||
|
||||
if (blacklistHits.length) {
|
||||
const h = blacklistHits[0];
|
||||
return {
|
||||
status: "blacklisted",
|
||||
reason: `auto_apply_blacklist active on rejected/${h.name}`,
|
||||
blocked_by: { file: h.name, days_remaining: h.daysRemaining },
|
||||
};
|
||||
}
|
||||
if (appliedHits.length) {
|
||||
const h = appliedHits[0];
|
||||
return {
|
||||
status: "cooldown",
|
||||
reason: `applied within ${COOLDOWN_DAYS}d (applied/${h.name})`,
|
||||
blocked_by: { file: h.name, days_remaining: h.daysRemaining },
|
||||
};
|
||||
}
|
||||
return { status: "cool", reason: "no recent applied/rejected match", blocked_by: null };
|
||||
}
|
||||
|
||||
// djb2 hash returned as base36 — deterministic, no deps.
|
||||
function djb2(s) {
|
||||
let h = 5381;
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
h = (((h << 5) + h) ^ s.charCodeAt(i)) >>> 0; // xor variant, force u32
|
||||
}
|
||||
return h.toString(36);
|
||||
}
|
||||
|
||||
export function computeProposalFingerprint(proposal) {
|
||||
if (!proposal || typeof proposal !== "object") return LEGACY_FINGERPRINT;
|
||||
const skill = proposal.skill_slug || proposal.target_skill || proposal.skill || "";
|
||||
const cluster = proposal.signal_cluster_id || proposal.cluster_id || "";
|
||||
const diff = String(proposal.diff_body || proposal.proposed_change || "")
|
||||
.replace(/\s+/g, " ")
|
||||
.replace(/\n+$/g, "")
|
||||
.trim();
|
||||
return djb2(`${skill}\n${cluster}\n${diff}`);
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
if (args.help) {
|
||||
process.stdout.write("usage: adam-cooldown.mjs --skill <slug> --fingerprint <hash> [--home <path>]\n");
|
||||
process.exit(0);
|
||||
}
|
||||
if (!args.skill || !args.fingerprint) {
|
||||
process.stderr.write("adam-cooldown: --skill and --fingerprint required\n");
|
||||
process.exit(1);
|
||||
}
|
||||
const home = args.home || join(homedir(), ".claude");
|
||||
try {
|
||||
const result = checkCooldown(home, args.skill, args.fingerprint);
|
||||
process.stdout.write(JSON.stringify(result) + "\n");
|
||||
process.exit(0);
|
||||
} catch (e) {
|
||||
process.stderr.write(`adam-cooldown error: ${e.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
main();
|
||||
}
|
||||
Executable
+238
@@ -0,0 +1,238 @@
|
||||
#!/usr/bin/env node
|
||||
// adam-explain.mjs — render the analyst's clustering trace in summary / full / json modes.
|
||||
//
|
||||
// The adam agent ALWAYS emits a fenced ```trace block after its proposals. The
|
||||
// adam-self-improvement skill persists the most recent trace to
|
||||
// ~/.claude/adam/last-trace.txt. This tool parses and presents it.
|
||||
//
|
||||
// Trace line grammar (per agents/adam.md "Clustering trace (always emit)"):
|
||||
// <cluster_id> | signal=<type> count=<N> sessions=<M> | gates: threshold=<pass|fail:<reason>>, cross_session=<pass|fail>, window=<in:<N>/out:<M>>, contradiction=<none|vetoed:[[memory-name]]> | decision: <proposal_emitted:<type>|skipped:<reason>>
|
||||
// Trailing summary line:
|
||||
// SUMMARY: considered=<N> emitted=<M> skipped=<N-M> reasons={threshold:X, contradiction:Y, window:Z, other:W}
|
||||
//
|
||||
// Usage: adam-explain.mjs [--input <path>] [--mode summary|full|json] [--home <path>]
|
||||
|
||||
import { readFileSync, existsSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = { input: null, mode: "summary", home: null };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--input" && i + 1 < argv.length) args.input = argv[++i];
|
||||
else if (a === "--mode" && i + 1 < argv.length) args.mode = argv[++i];
|
||||
else if (a === "--home" && i + 1 < argv.length) args.home = argv[++i];
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
// Extract the inside of a ```trace ... ``` fenced block, or return the input
|
||||
// verbatim if no fence is present (tolerant mode).
|
||||
function extractFenced(text) {
|
||||
const m = text.match(/```trace\s*\n([\s\S]*?)\n```/);
|
||||
return m ? m[1] : text;
|
||||
}
|
||||
|
||||
// Parse a single cluster line. Returns null when no recognisable structure.
|
||||
function parseClusterLine(line) {
|
||||
// Three pipe-separated chunks: id | signal=… count=… sessions=… | gates: … | decision: …
|
||||
const parts = line.split("|").map((s) => s.trim());
|
||||
if (parts.length < 4) return null;
|
||||
const id = parts[0];
|
||||
if (!id) return null;
|
||||
const sigChunk = parts[1];
|
||||
const gatesChunk = parts[2];
|
||||
const decisionChunk = parts.slice(3).join("|").trim();
|
||||
|
||||
const sigM = sigChunk.match(/signal=(\S+)\s+count=(\d+)\s+sessions=(\d+)/);
|
||||
if (!sigM) return null;
|
||||
const signal = sigM[1];
|
||||
const count = Number(sigM[2]);
|
||||
const sessions = Number(sigM[3]);
|
||||
|
||||
if (!gatesChunk.startsWith("gates:")) return null;
|
||||
const gatesBody = gatesChunk.slice("gates:".length).trim();
|
||||
const gates = {};
|
||||
// gates body is a comma-separated list of key=value with possible commas inside values
|
||||
// we accept simple key=token,key=token,… — split on commas not inside [[ ]]
|
||||
const tokens = [];
|
||||
let depth = 0;
|
||||
let buf = "";
|
||||
for (const ch of gatesBody) {
|
||||
if (ch === "[") depth++;
|
||||
else if (ch === "]") depth--;
|
||||
if (ch === "," && depth === 0) { tokens.push(buf.trim()); buf = ""; }
|
||||
else buf += ch;
|
||||
}
|
||||
if (buf.trim()) tokens.push(buf.trim());
|
||||
for (const t of tokens) {
|
||||
const idx = t.indexOf("=");
|
||||
if (idx === -1) continue;
|
||||
gates[t.slice(0, idx).trim()] = t.slice(idx + 1).trim();
|
||||
}
|
||||
|
||||
if (!decisionChunk.startsWith("decision:")) return null;
|
||||
const decision = decisionChunk.slice("decision:".length).trim();
|
||||
|
||||
return { id, signal, count, sessions, gates, decision };
|
||||
}
|
||||
|
||||
function parseSummaryLine(line) {
|
||||
// SUMMARY: considered=N emitted=M skipped=K [regressions=R] reasons={...}
|
||||
// `regressions` is optional (added in #5 — A/B measurement). Pre-existing
|
||||
// traces lacking the token still parse.
|
||||
const m = line.match(
|
||||
/^SUMMARY:\s*considered=(\d+)\s+emitted=(\d+)\s+skipped=(\d+)(?:\s+regressions=(\d+))?\s+reasons=\{([^}]*)\}\s*$/
|
||||
);
|
||||
if (!m) return null;
|
||||
const reasons = {};
|
||||
for (const piece of m[5].split(",")) {
|
||||
const kv = piece.trim();
|
||||
if (!kv) continue;
|
||||
const idx = kv.indexOf(":");
|
||||
if (idx === -1) continue;
|
||||
reasons[kv.slice(0, idx).trim()] = Number(kv.slice(idx + 1).trim()) || 0;
|
||||
}
|
||||
return {
|
||||
considered: Number(m[1]),
|
||||
emitted: Number(m[2]),
|
||||
skipped: Number(m[3]),
|
||||
regressions: m[4] != null ? Number(m[4]) : 0,
|
||||
reasons,
|
||||
};
|
||||
}
|
||||
|
||||
export function parseTrace(text) {
|
||||
const body = extractFenced(text || "");
|
||||
const lines = body.split("\n").map((l) => l.replace(/\s+$/, "")).filter((l) => l.trim().length);
|
||||
const clusters = [];
|
||||
let summary = null;
|
||||
const warnings = [];
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("SUMMARY:")) {
|
||||
const s = parseSummaryLine(line);
|
||||
if (s) summary = s;
|
||||
else warnings.push(`malformed summary line: ${line}`);
|
||||
continue;
|
||||
}
|
||||
const c = parseClusterLine(line);
|
||||
if (c) clusters.push(c);
|
||||
else warnings.push(`malformed cluster line: ${line}`);
|
||||
}
|
||||
// Synthesize a summary from clusters when none provided AND clusters parsed.
|
||||
if (!summary && clusters.length) {
|
||||
const reasons = { threshold: 0, contradiction: 0, window: 0, other: 0 };
|
||||
let emitted = 0;
|
||||
for (const c of clusters) {
|
||||
if (c.decision.startsWith("proposal_emitted")) emitted++;
|
||||
else {
|
||||
const r = (c.decision.match(/^skipped:(\S+)/) || [])[1] || "other";
|
||||
reasons[r in reasons ? r : "other"]++;
|
||||
}
|
||||
}
|
||||
summary = {
|
||||
considered: clusters.length,
|
||||
emitted,
|
||||
skipped: clusters.length - emitted,
|
||||
reasons,
|
||||
};
|
||||
}
|
||||
return { clusters, summary, warnings };
|
||||
}
|
||||
|
||||
function countByDecision(clusters) {
|
||||
const counts = {};
|
||||
for (const c of clusters) {
|
||||
const key = c.decision.startsWith("proposal_emitted")
|
||||
? "proposal_emitted"
|
||||
: (c.decision.match(/^skipped:(\S+)/)?.[1] || "other");
|
||||
counts[key] = (counts[key] || 0) + 1;
|
||||
}
|
||||
return counts;
|
||||
}
|
||||
|
||||
export function formatSummary(parsed) {
|
||||
const s = parsed.summary;
|
||||
if (!s) return "no trace data";
|
||||
const reasonStr = Object.entries(s.reasons)
|
||||
.map(([k, v]) => `${k}:${v}`)
|
||||
.join(", ");
|
||||
const counts = countByDecision(parsed.clusters);
|
||||
const breakdown = Object.entries(counts)
|
||||
.map(([k, v]) => `${k}=${v}`)
|
||||
.join(" ");
|
||||
const head = `considered=${s.considered} emitted=${s.emitted} skipped=${s.skipped} reasons={${reasonStr}}`;
|
||||
return breakdown ? `${head}\nclusters by decision: ${breakdown}` : head;
|
||||
}
|
||||
|
||||
export function formatFull(parsed) {
|
||||
const lines = [];
|
||||
for (const c of parsed.clusters) {
|
||||
const gatesStr = Object.entries(c.gates).map(([k, v]) => `${k}=${v}`).join(", ");
|
||||
lines.push(`${c.id} | signal=${c.signal} count=${c.count} sessions=${c.sessions} | gates: ${gatesStr} | decision: ${c.decision}`);
|
||||
}
|
||||
if (parsed.summary) {
|
||||
const s = parsed.summary;
|
||||
const reasonStr = Object.entries(s.reasons).map(([k, v]) => `${k}:${v}`).join(", ");
|
||||
lines.push(`SUMMARY: considered=${s.considered} emitted=${s.emitted} skipped=${s.skipped} regressions=${s.regressions ?? 0} reasons={${reasonStr}}`);
|
||||
}
|
||||
// Histogram footer: only count actual rejection reasons from clusters.
|
||||
const hist = {};
|
||||
for (const c of parsed.clusters) {
|
||||
const m = c.decision.match(/^skipped:(\S+)/);
|
||||
if (m) hist[m[1]] = (hist[m[1]] || 0) + 1;
|
||||
}
|
||||
const histStr = Object.entries(hist).map(([k, v]) => `${k} ${v}`).join(", ");
|
||||
lines.push(`Rejection reasons: ${histStr || "none"}`);
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
export function formatJson(parsed) {
|
||||
return JSON.stringify({
|
||||
clusters: parsed.clusters,
|
||||
summary: parsed.summary,
|
||||
}, null, 2);
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
const claudeHome = args.home || join(homedir(), ".claude");
|
||||
const defaultInput = join(claudeHome, "adam", "last-trace.txt");
|
||||
const inputPath = args.input || defaultInput;
|
||||
|
||||
let raw;
|
||||
try {
|
||||
if (!existsSync(inputPath)) {
|
||||
process.stderr.write(`adam-explain: input not found: ${inputPath}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
raw = readFileSync(inputPath, "utf8");
|
||||
} catch (e) {
|
||||
process.stderr.write(`adam-explain: read failed: ${e.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const parsed = parseTrace(raw);
|
||||
if (!parsed.clusters.length && !parsed.summary) {
|
||||
process.stderr.write(`adam-explain: no parseable trace lines in ${inputPath}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
for (const w of parsed.warnings) {
|
||||
process.stderr.write(`adam-explain: warn: ${w}\n`);
|
||||
}
|
||||
|
||||
const mode = args.mode || "summary";
|
||||
let out;
|
||||
if (mode === "full") out = formatFull(parsed);
|
||||
else if (mode === "json") out = formatJson(parsed);
|
||||
else out = formatSummary(parsed);
|
||||
process.stdout.write(out + "\n");
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
try { main(); } catch (e) {
|
||||
process.stderr.write(`adam-explain error: ${e.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
Executable
+97
@@ -0,0 +1,97 @@
|
||||
#!/usr/bin/env node
|
||||
// adam-nudge-eligibility.mjs — checks whether the current session has accrued
|
||||
// enough `dead_end` entries to warrant emitting a cross-session nudge.
|
||||
//
|
||||
// CLI: adam-nudge-eligibility.mjs [--home <path>] [--session <id>]
|
||||
// --home defaults to $HOME/.claude
|
||||
// --session if absent, reads state.json `session_id` field
|
||||
//
|
||||
// Output (stdout): JSON one-liner
|
||||
// eligible: {"eligible": true, "session_id": "...", "dead_end_count": N, "last_ts": "..."}
|
||||
// not: {"eligible": false, "session_id": "...", "dead_end_count": N, "last_ts": "..."|null}
|
||||
// Exit codes:
|
||||
// 0 — read succeeded (eligible OR not)
|
||||
// 1 — read failure / unable to resolve session
|
||||
//
|
||||
// Threshold: ≥3 dead_end entries within a single session_id across the active
|
||||
// journal + all rotated journal/*.jsonl files. Threshold matches the
|
||||
// "dead-end checkpoint" feedback rule.
|
||||
|
||||
import { readFileSync, existsSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { readJsonlSafe, listJsonlFiles } from "./adam-utils.mjs";
|
||||
|
||||
export const DEAD_END_THRESHOLD = 3;
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = { home: null, session: null, help: false };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--home" && i + 1 < argv.length) args.home = argv[++i];
|
||||
else if (a === "--session" && i + 1 < argv.length) args.session = argv[++i];
|
||||
else if (a === "--help" || a === "-h") args.help = true;
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
function resolveSession(home, fallback) {
|
||||
if (fallback) return fallback;
|
||||
const statePath = join(home, "adam", "state.json");
|
||||
if (!existsSync(statePath)) return null;
|
||||
try {
|
||||
const st = JSON.parse(readFileSync(statePath, "utf8"));
|
||||
return st && typeof st.session_id === "string" ? st.session_id : null;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
export function checkEligibility(home, sessionId) {
|
||||
const adamRoot = join(home, "adam");
|
||||
const sources = [
|
||||
join(adamRoot, "journal.jsonl"),
|
||||
...listJsonlFiles(join(adamRoot, "journal")),
|
||||
];
|
||||
let count = 0;
|
||||
let lastTs = null;
|
||||
for (const p of sources) {
|
||||
for (const e of readJsonlSafe(p)) {
|
||||
if (!e || e.type !== "dead_end") continue;
|
||||
if (e.session !== sessionId && e.session_id !== sessionId) continue;
|
||||
count++;
|
||||
const ts = typeof e.ts === "string" ? e.ts : null;
|
||||
if (ts && (!lastTs || ts > lastTs)) lastTs = ts;
|
||||
}
|
||||
}
|
||||
return {
|
||||
eligible: count >= DEAD_END_THRESHOLD,
|
||||
session_id: sessionId,
|
||||
dead_end_count: count,
|
||||
last_ts: lastTs,
|
||||
};
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
if (args.help) {
|
||||
process.stdout.write("usage: adam-nudge-eligibility.mjs [--home <path>] [--session <id>]\n");
|
||||
process.exit(0);
|
||||
}
|
||||
const home = args.home || join(homedir(), ".claude");
|
||||
const sessionId = resolveSession(home, args.session);
|
||||
if (!sessionId) {
|
||||
process.stderr.write("adam-nudge-eligibility: no session_id (pass --session or seed state.json)\n");
|
||||
process.exit(1);
|
||||
}
|
||||
try {
|
||||
const result = checkEligibility(home, sessionId);
|
||||
process.stdout.write(JSON.stringify(result) + "\n");
|
||||
process.exit(0);
|
||||
} catch (e) {
|
||||
process.stderr.write(`adam-nudge-eligibility error: ${e.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
main();
|
||||
}
|
||||
Executable
+169
@@ -0,0 +1,169 @@
|
||||
#!/usr/bin/env node
|
||||
// adam-score.mjs — computes per-session urgency dampeners + reinforcement
|
||||
// candidates from `task_completed` signals.
|
||||
//
|
||||
// Effects:
|
||||
// 1. Dampener:
|
||||
// task_completed_count >= 3 → 0.5
|
||||
// task_completed_count >= 1 → 0.75
|
||||
// else → 1.0
|
||||
// Analyst multiplies a cluster's urgency by the dampener of the session
|
||||
// it originated from.
|
||||
// 2. Reinforcement candidates: per skill, count of clean task_completed
|
||||
// events citing it (via `active_skills` payload). Skills with count >= 3
|
||||
// are surfaced as reinforcement proposal candidates (low blast,
|
||||
// confidence ≥ 4 required for auto-apply, same gate as memory).
|
||||
//
|
||||
// CLI:
|
||||
// adam-score.mjs [--home <path>] [--input <jsonl-path>]
|
||||
//
|
||||
// --input defaults to: stdout of adam-window.mjs (preferred) — if missing,
|
||||
// falls back to the raw active journal.
|
||||
//
|
||||
// Output: JSON object
|
||||
// {
|
||||
// "sessions": [
|
||||
// {"session_id": "...", "negative_count": N, "task_completed_count": M, "dampener": 1.0}
|
||||
// ],
|
||||
// "reinforcement_candidates": [
|
||||
// {"skill_slug": "tdd-loop", "count": 3, "recent_ts": "..."}
|
||||
// ]
|
||||
// }
|
||||
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { readJsonlSafe, listJsonlFiles } from "./adam-utils.mjs";
|
||||
|
||||
export const NEGATIVE_SIGNAL_TYPES = new Set([
|
||||
"correction",
|
||||
"tool_error_loop",
|
||||
"dead_end",
|
||||
"edit_churn",
|
||||
"retry_loop",
|
||||
"build_loop",
|
||||
"weak_agent",
|
||||
]);
|
||||
|
||||
export const REINFORCEMENT_THRESHOLD = 3;
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = { home: null, input: null, help: false };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--home" && i + 1 < argv.length) args.home = argv[++i];
|
||||
else if (a === "--input" && i + 1 < argv.length) args.input = argv[++i];
|
||||
else if (a === "--help" || a === "-h") args.help = true;
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
function readAllStdin() {
|
||||
try { return readFileSync(0, "utf8"); } catch { return ""; }
|
||||
}
|
||||
|
||||
function entriesFromText(text) {
|
||||
const out = [];
|
||||
for (const line of (text || "").split("\n")) {
|
||||
if (!line) continue;
|
||||
try { out.push(JSON.parse(line)); } catch { /* skip */ }
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function computeDampener(taskCompletedCount) {
|
||||
if (taskCompletedCount >= 3) return 0.5;
|
||||
if (taskCompletedCount >= 1) return 0.75;
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
export function computeSessionScores(entries) {
|
||||
const bySession = new Map();
|
||||
for (const e of entries || []) {
|
||||
if (!e || typeof e !== "object") continue;
|
||||
const sid = e.session || e.session_id || "";
|
||||
if (!sid) continue;
|
||||
if (!bySession.has(sid)) {
|
||||
bySession.set(sid, { session_id: sid, negative_count: 0, task_completed_count: 0 });
|
||||
}
|
||||
const slot = bySession.get(sid);
|
||||
if (e.type === "task_completed") slot.task_completed_count++;
|
||||
else if (NEGATIVE_SIGNAL_TYPES.has(e.type)) slot.negative_count++;
|
||||
}
|
||||
const out = [];
|
||||
for (const slot of bySession.values()) {
|
||||
out.push({
|
||||
...slot,
|
||||
dampener: computeDampener(slot.task_completed_count),
|
||||
});
|
||||
}
|
||||
// Stable ordering by session_id for deterministic output.
|
||||
out.sort((a, b) => (a.session_id < b.session_id ? -1 : a.session_id > b.session_id ? 1 : 0));
|
||||
return out;
|
||||
}
|
||||
|
||||
export function computeReinforcementCandidates(entries) {
|
||||
const counts = new Map();
|
||||
for (const e of entries || []) {
|
||||
if (!e || e.type !== "task_completed") continue;
|
||||
const skills = Array.isArray(e.active_skills) ? e.active_skills : [];
|
||||
for (const slug of skills) {
|
||||
if (!slug || typeof slug !== "string") continue;
|
||||
if (!counts.has(slug)) counts.set(slug, { count: 0, recent_ts: null });
|
||||
const slot = counts.get(slug);
|
||||
slot.count++;
|
||||
const ts = typeof e.ts === "string" ? e.ts : null;
|
||||
if (ts && (!slot.recent_ts || ts > slot.recent_ts)) slot.recent_ts = ts;
|
||||
}
|
||||
}
|
||||
const out = [];
|
||||
for (const [slug, { count, recent_ts }] of counts.entries()) {
|
||||
if (count < REINFORCEMENT_THRESHOLD) continue;
|
||||
out.push({ skill_slug: slug, count, recent_ts });
|
||||
}
|
||||
out.sort((a, b) => b.count - a.count || (a.skill_slug < b.skill_slug ? -1 : 1));
|
||||
return out;
|
||||
}
|
||||
|
||||
function gatherInputEntries(args) {
|
||||
if (args.input) return readJsonlSafe(args.input);
|
||||
// Honor piped stdin only when it is non-empty AND not a TTY.
|
||||
if (!process.stdin.isTTY) {
|
||||
const piped = readAllStdin();
|
||||
if (piped && piped.trim()) return entriesFromText(piped);
|
||||
}
|
||||
// Default fallback: active journal + rotated files.
|
||||
const home = args.home || join(homedir(), ".claude");
|
||||
const adamRoot = join(home, "adam");
|
||||
const sources = [
|
||||
join(adamRoot, "journal.jsonl"),
|
||||
...listJsonlFiles(join(adamRoot, "journal")),
|
||||
];
|
||||
const all = [];
|
||||
for (const p of sources) {
|
||||
for (const e of readJsonlSafe(p)) all.push(e);
|
||||
}
|
||||
return all;
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
if (args.help) {
|
||||
process.stdout.write("usage: adam-score.mjs [--home <path>] [--input <jsonl-path>]\n");
|
||||
process.exit(0);
|
||||
}
|
||||
try {
|
||||
const entries = gatherInputEntries(args);
|
||||
const sessions = computeSessionScores(entries);
|
||||
const reinforcement_candidates = computeReinforcementCandidates(entries);
|
||||
process.stdout.write(JSON.stringify({ sessions, reinforcement_candidates }) + "\n");
|
||||
process.exit(0);
|
||||
} catch (e) {
|
||||
process.stderr.write(`adam-score error: ${e.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
main();
|
||||
}
|
||||
Executable
+251
@@ -0,0 +1,251 @@
|
||||
#!/usr/bin/env node
|
||||
// adam-upgrade.mjs — review/accept pending `.adam-new` files from install.sh.
|
||||
//
|
||||
// install.sh writes <file>.adam-new next to user-modified ADAM files instead
|
||||
// of clobbering. This tool surfaces those pending merges and lets users
|
||||
// review the diff + accept atomically.
|
||||
//
|
||||
// CLI:
|
||||
// adam-upgrade.mjs --list [--home <path>]
|
||||
// adam-upgrade.mjs --diff [<path>] [--home <path>]
|
||||
// adam-upgrade.mjs --accept <path> [--home <path>]
|
||||
// adam-upgrade.mjs --accept-all [--home <path>]
|
||||
// adam-upgrade.mjs --help
|
||||
|
||||
import {
|
||||
readdirSync,
|
||||
statSync,
|
||||
existsSync,
|
||||
renameSync,
|
||||
readFileSync,
|
||||
unlinkSync,
|
||||
} from "node:fs";
|
||||
import { join, dirname, basename } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
const EXCLUDE_DIRS = new Set([
|
||||
".git",
|
||||
"node_modules",
|
||||
"journal",
|
||||
"trash",
|
||||
"proposals",
|
||||
"applied",
|
||||
"rejected",
|
||||
]);
|
||||
|
||||
// Walk a directory tree, collecting paths to files ending in `.adam-new`.
|
||||
// Excludes the dirs above defensively (no point in surfacing journal entries).
|
||||
export function findPending(home) {
|
||||
const root = home;
|
||||
const out = [];
|
||||
function walk(dir) {
|
||||
let entries;
|
||||
try {
|
||||
entries = readdirSync(dir, { withFileTypes: true });
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
for (const e of entries) {
|
||||
const full = join(dir, e.name);
|
||||
if (e.isDirectory()) {
|
||||
if (EXCLUDE_DIRS.has(e.name)) continue;
|
||||
walk(full);
|
||||
} else if (e.isFile() && e.name.endsWith(".adam-new")) {
|
||||
out.push(full);
|
||||
}
|
||||
}
|
||||
}
|
||||
walk(root);
|
||||
return out.sort();
|
||||
}
|
||||
|
||||
function fileSize(p) {
|
||||
try { return statSync(p).size; } catch { return 0; }
|
||||
}
|
||||
|
||||
function fileAgeDays(p) {
|
||||
try {
|
||||
const mtime = statSync(p).mtimeMs;
|
||||
return Math.floor((Date.now() - mtime) / 86400000);
|
||||
} catch { return 0; }
|
||||
}
|
||||
|
||||
// Produce a unified diff between two files. Prefer the system `diff -u` binary
|
||||
// (universally available, accurate). On systems without `diff`, fall back to a
|
||||
// naive line-by-line diff prefixed with MISSING:/NEW: so the tool still works.
|
||||
export function diffPaths(orig, neu) {
|
||||
const r = spawnSync("diff", ["-u", orig, neu], { encoding: "utf8" });
|
||||
if (r.error || r.status === null || r.status === 2) {
|
||||
// diff binary missing or fatal error — naive fallback
|
||||
let a = [], b = [];
|
||||
try { a = readFileSync(orig, "utf8").split("\n"); } catch {}
|
||||
try { b = readFileSync(neu, "utf8").split("\n"); } catch {}
|
||||
const max = Math.max(a.length, b.length);
|
||||
const lines = [];
|
||||
for (let i = 0; i < max; i++) {
|
||||
const la = a[i], lb = b[i];
|
||||
if (la === lb) continue;
|
||||
if (la !== undefined) lines.push(`MISSING: ${la}`);
|
||||
if (lb !== undefined) lines.push(`NEW: ${lb}`);
|
||||
}
|
||||
return lines.join("\n");
|
||||
}
|
||||
return r.stdout || "";
|
||||
}
|
||||
|
||||
// Atomic swap: rename orig → orig.adam-prev, rename neu → orig. Overwrites any
|
||||
// prior .adam-prev backup (safe: a previous accept already promoted it).
|
||||
export function acceptOne(orig, neu) {
|
||||
if (!existsSync(neu)) {
|
||||
throw new Error(`missing pending file: ${neu}`);
|
||||
}
|
||||
const prev = `${orig}.adam-prev`;
|
||||
if (existsSync(orig)) {
|
||||
if (existsSync(prev)) {
|
||||
try { unlinkSync(prev); } catch {}
|
||||
}
|
||||
renameSync(orig, prev);
|
||||
}
|
||||
renameSync(neu, orig);
|
||||
return { orig, prev };
|
||||
}
|
||||
|
||||
export function acceptAll(home) {
|
||||
const pending = findPending(home);
|
||||
const results = [];
|
||||
for (const neu of pending) {
|
||||
const orig = neu.replace(/\.adam-new$/, "");
|
||||
try {
|
||||
const r = acceptOne(orig, neu);
|
||||
results.push({ ok: true, ...r });
|
||||
} catch (err) {
|
||||
results.push({ ok: false, orig, error: String(err && err.message || err) });
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = { cmd: null, target: null, home: null };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--list") args.cmd = "list";
|
||||
else if (a === "--diff") args.cmd = "diff";
|
||||
else if (a === "--accept") args.cmd = "accept";
|
||||
else if (a === "--accept-all") args.cmd = "accept-all";
|
||||
else if (a === "--help" || a === "-h") args.cmd = "help";
|
||||
else if (a === "--home" && i + 1 < argv.length) args.home = argv[++i];
|
||||
else if (!a.startsWith("--") && args.target == null) args.target = a;
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
function usage() {
|
||||
process.stdout.write(
|
||||
"adam-upgrade — review pending `.adam-new` files from install.sh\n" +
|
||||
"\n" +
|
||||
"Usage:\n" +
|
||||
" adam-upgrade.mjs --list [--home <path>]\n" +
|
||||
" adam-upgrade.mjs --diff [<path>] [--home <path>]\n" +
|
||||
" adam-upgrade.mjs --accept <path> [--home <path>]\n" +
|
||||
" adam-upgrade.mjs --accept-all [--home <path>]\n" +
|
||||
" adam-upgrade.mjs --help\n"
|
||||
);
|
||||
}
|
||||
|
||||
function resolveHome(args) {
|
||||
if (args.home) return args.home;
|
||||
return join(process.env.HOME || homedir(), ".claude");
|
||||
}
|
||||
|
||||
function cmdList(args) {
|
||||
const home = resolveHome(args);
|
||||
const pending = findPending(home);
|
||||
for (const neu of pending) {
|
||||
const orig = neu.replace(/\.adam-new$/, "");
|
||||
const origSize = fileSize(orig);
|
||||
const newSize = fileSize(neu);
|
||||
const age = fileAgeDays(neu);
|
||||
process.stdout.write(`${neu} (orig: ${origSize}, new: ${newSize}, age: ${age}d)\n`);
|
||||
}
|
||||
process.stderr.write(`${pending.length} pending\n`);
|
||||
return 0;
|
||||
}
|
||||
|
||||
function cmdDiff(args) {
|
||||
const home = resolveHome(args);
|
||||
let targets;
|
||||
if (args.target) {
|
||||
// Allow either passing the orig path or the .adam-new path.
|
||||
const t = args.target;
|
||||
const orig = t.endsWith(".adam-new") ? t.replace(/\.adam-new$/, "") : t;
|
||||
targets = [orig];
|
||||
} else {
|
||||
targets = findPending(home).map((n) => n.replace(/\.adam-new$/, ""));
|
||||
}
|
||||
for (const orig of targets) {
|
||||
const neu = `${orig}.adam-new`;
|
||||
process.stdout.write(`=== ${orig} ===\n`);
|
||||
if (!existsSync(neu)) {
|
||||
process.stderr.write(`no pending: ${neu}\n`);
|
||||
continue;
|
||||
}
|
||||
process.stdout.write(diffPaths(orig, neu));
|
||||
process.stdout.write("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function cmdAccept(args) {
|
||||
if (!args.target) {
|
||||
process.stderr.write("error: --accept requires a <path>\n");
|
||||
return 1;
|
||||
}
|
||||
const t = args.target;
|
||||
const orig = t.endsWith(".adam-new") ? t.replace(/\.adam-new$/, "") : t;
|
||||
const neu = `${orig}.adam-new`;
|
||||
try {
|
||||
const r = acceptOne(orig, neu);
|
||||
process.stdout.write(`accepted: ${r.orig} (backup: ${r.prev})\n`);
|
||||
return 0;
|
||||
} catch (err) {
|
||||
process.stderr.write(`error: ${err.message}\n`);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
function cmdAcceptAll(args) {
|
||||
const home = resolveHome(args);
|
||||
const results = acceptAll(home);
|
||||
for (const r of results) {
|
||||
if (r.ok) {
|
||||
process.stdout.write(`accepted: ${r.orig} (backup: ${r.prev})\n`);
|
||||
} else {
|
||||
process.stderr.write(`error: ${r.orig}: ${r.error}\n`);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
if (!args.cmd || args.cmd === "help") { usage(); return 0; }
|
||||
if (args.cmd === "list") return cmdList(args);
|
||||
if (args.cmd === "diff") return cmdDiff(args);
|
||||
if (args.cmd === "accept") return cmdAccept(args);
|
||||
if (args.cmd === "accept-all") return cmdAcceptAll(args);
|
||||
usage();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Only run main() when invoked as a script (not when imported for tests).
|
||||
const invokedAsScript = (() => {
|
||||
try {
|
||||
const argv1 = process.argv[1] || "";
|
||||
return argv1.endsWith("adam-upgrade.mjs");
|
||||
} catch { return true; }
|
||||
})();
|
||||
if (invokedAsScript) {
|
||||
process.exit(main());
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// adam-utils.mjs — shared helpers used across adam-* scripts.
|
||||
//
|
||||
// Pure library: no shebang, not a CLI. Imported by adam-window.mjs,
|
||||
// adam-score.mjs, adam-ab-measure.mjs, adam-nudge-eligibility.mjs (jsonl
|
||||
// helpers) and adam-apply-reinforcement.mjs, adam-archive.mjs,
|
||||
// adam-cooldown.mjs (parseFrontmatter).
|
||||
//
|
||||
// All helpers swallow read/parse failures by design — callers expect to keep
|
||||
// going on a corrupt line/file rather than abort the whole pipeline.
|
||||
|
||||
import { readFileSync, readdirSync, existsSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
// listJsonlFiles: list *.jsonl files in `dir`. Missing dir or read failure
|
||||
// returns []. Filenames are joined with `dir` so callers can read directly.
|
||||
export function listJsonlFiles(dir) {
|
||||
if (!existsSync(dir)) return [];
|
||||
try {
|
||||
return readdirSync(dir)
|
||||
.filter((n) => n.endsWith(".jsonl"))
|
||||
.map((n) => join(dir, n));
|
||||
} catch { return []; }
|
||||
}
|
||||
|
||||
// readJsonlSafe: read a .jsonl file and return an array of parsed objects.
|
||||
// Missing file, unreadable file, or any malformed line are silently skipped.
|
||||
export function readJsonlSafe(path) {
|
||||
if (!existsSync(path)) return [];
|
||||
let buf;
|
||||
try { buf = readFileSync(path, "utf8"); } catch { return []; }
|
||||
const out = [];
|
||||
for (const line of buf.split("\n")) {
|
||||
if (!line) continue;
|
||||
try { out.push(JSON.parse(line)); } catch { /* skip malformed */ }
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// parseFrontmatter: parse a markdown YAML-ish frontmatter block into a flat
|
||||
// object. Supports:
|
||||
// - inline scalars key: value
|
||||
// - inline arrays key: [a, b, c]
|
||||
// - block-form arrays key:\n - a\n - b
|
||||
// Quotes around scalar values are stripped. Comment-only lines (`# ...`) and
|
||||
// keys with empty inline values that are NOT followed by a block array are
|
||||
// skipped (preserves prior cooldown.mjs behavior). Missing frontmatter → {}.
|
||||
export function parseFrontmatter(content) {
|
||||
const m = content.match(/^---\n([\s\S]*?)\n---/);
|
||||
if (!m) return {};
|
||||
const out = {};
|
||||
const lines = m[1].split("\n");
|
||||
let i = 0;
|
||||
while (i < lines.length) {
|
||||
const line = lines[i];
|
||||
const idx = line.indexOf(":");
|
||||
if (idx === -1) { i++; continue; }
|
||||
const key = line.slice(0, idx).trim();
|
||||
if (!key || key.startsWith("#")) { i++; continue; }
|
||||
const rawValue = line.slice(idx + 1).trim();
|
||||
if (rawValue.startsWith("[") && rawValue.endsWith("]")) {
|
||||
const inner = rawValue.slice(1, -1)
|
||||
.split(",")
|
||||
.map((s) => s.trim().replace(/^['"]|['"]$/g, ""))
|
||||
.filter(Boolean);
|
||||
out[key] = inner;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (!rawValue) {
|
||||
// Possible block-form array: look ahead for ` - item` lines.
|
||||
const arr = [];
|
||||
let j = i + 1;
|
||||
while (j < lines.length && /^\s*-\s+/.test(lines[j])) {
|
||||
const item = lines[j].replace(/^\s*-\s+/, "").trim().replace(/^['"]|['"]$/g, "");
|
||||
if (item) arr.push(item);
|
||||
j++;
|
||||
}
|
||||
if (arr.length) {
|
||||
out[key] = arr;
|
||||
i = j;
|
||||
continue;
|
||||
}
|
||||
// Empty value, no block follow-up: skip (cooldown/apply-reinforcement
|
||||
// expectation — empty scalars are noise).
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
out[key] = rawValue.replace(/^['"]|['"]$/g, "");
|
||||
i++;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Executable
+174
@@ -0,0 +1,174 @@
|
||||
#!/usr/bin/env node
|
||||
// adam-window.mjs — per-signal sliding-window filter over the ADAM journal.
|
||||
//
|
||||
// Reads all journal sources (active journal.jsonl + rotated journal/*.jsonl,
|
||||
// including both new YYYY-Www.jsonl format and legacy YYYY-MM-DD-<ts>.jsonl
|
||||
// size-rotated files), applies a per-signal-type age cutoff based on each
|
||||
// entry's `ts` field, and emits the filtered JSONL stream to stdout.
|
||||
//
|
||||
// Exclusion: entries whose `ts` appears in any applied/*.md or rejected/*.md
|
||||
// proposal frontmatter `source_entries` array are dropped (same semantics the
|
||||
// adam agent previously enforced manually). Keeps actioned signals out of the
|
||||
// next /reflect even if they're inside the analysis window.
|
||||
//
|
||||
// Usage: adam-window.mjs [--home <path>] default: $HOME/.claude
|
||||
// Output: filtered JSONL on stdout. One-line summary on stderr.
|
||||
|
||||
import { readFileSync, readdirSync, existsSync, statSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { readJsonlSafe, listJsonlFiles } from "./adam-utils.mjs";
|
||||
|
||||
// Per-signal sliding window in days. Source of truth — referenced by agents/adam.md.
|
||||
export const SIGNAL_WINDOWS_DAYS = {
|
||||
dead_end: 7,
|
||||
correction: 30,
|
||||
tool_error_loop: 30,
|
||||
edit_churn: 14,
|
||||
retry_loop: 14,
|
||||
build_loop: 30,
|
||||
weak_agent: 30,
|
||||
subagent_dispatch_pattern: 30,
|
||||
correction_free_streak: 60,
|
||||
clean_recovery: 60,
|
||||
task_completed: 60,
|
||||
};
|
||||
|
||||
// Fallback window for unknown / future signal types.
|
||||
export const DEFAULT_WINDOW_DAYS = 30;
|
||||
|
||||
const DAY_MS = 86400000;
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = { home: null };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
if (argv[i] === "--home" && i + 1 < argv.length) {
|
||||
args.home = argv[++i];
|
||||
}
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
// Crude single-pass frontmatter source_entries extractor. Mirrors adam-archive.mjs
|
||||
// parsing: handles both YAML block form and inline-array form. Only pulls the
|
||||
// `source_entries` key — we don't need anything else for exclusion.
|
||||
function extractSourceEntries(content) {
|
||||
const m = content.match(/^---\n([\s\S]*?)\n---/);
|
||||
if (!m) return [];
|
||||
const lines = m[1].split("\n");
|
||||
const out = [];
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
const idx = line.indexOf(":");
|
||||
if (idx === -1) continue;
|
||||
const key = line.slice(0, idx).trim();
|
||||
if (key !== "source_entries") continue;
|
||||
const value = line.slice(idx + 1).trim();
|
||||
if (value.startsWith("[") && value.endsWith("]")) {
|
||||
const inner = value.slice(1, -1).split(",")
|
||||
.map((s) => s.trim().replace(/^['"]|['"]$/g, ""))
|
||||
.filter(Boolean);
|
||||
out.push(...inner);
|
||||
continue;
|
||||
}
|
||||
let j = i + 1;
|
||||
while (j < lines.length && /^\s*-\s+/.test(lines[j])) {
|
||||
const item = lines[j].replace(/^\s*-\s+/, "").trim().replace(/^['"]|['"]$/g, "");
|
||||
if (item) out.push(item);
|
||||
j++;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function buildExclusionSet(...dirs) {
|
||||
const set = new Set();
|
||||
for (const dir of dirs) {
|
||||
if (!existsSync(dir)) continue;
|
||||
let names;
|
||||
try { names = readdirSync(dir); } catch { continue; }
|
||||
for (const name of names) {
|
||||
if (!name.endsWith(".md")) continue;
|
||||
const p = join(dir, name);
|
||||
try {
|
||||
const content = readFileSync(p, "utf8");
|
||||
for (const ts of extractSourceEntries(content)) set.add(ts);
|
||||
} catch { /* skip */ }
|
||||
}
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
function windowDaysFor(type) {
|
||||
if (Object.prototype.hasOwnProperty.call(SIGNAL_WINDOWS_DAYS, type)) {
|
||||
return SIGNAL_WINDOWS_DAYS[type];
|
||||
}
|
||||
return DEFAULT_WINDOW_DAYS;
|
||||
}
|
||||
|
||||
export function filterEntries(entries, exclusionSet, now = new Date()) {
|
||||
const nowMs = now.getTime();
|
||||
const dropped = { stale: {}, excluded: 0, no_ts: 0 };
|
||||
const kept = [];
|
||||
for (const e of entries) {
|
||||
if (!e || typeof e !== "object") continue;
|
||||
if (!e.ts || typeof e.ts !== "string") {
|
||||
dropped.no_ts++;
|
||||
continue;
|
||||
}
|
||||
if (exclusionSet.has(e.ts)) {
|
||||
dropped.excluded++;
|
||||
continue;
|
||||
}
|
||||
const type = e.type || "unknown";
|
||||
const days = windowDaysFor(type);
|
||||
const tsMs = Date.parse(e.ts);
|
||||
if (Number.isNaN(tsMs)) {
|
||||
dropped.no_ts++;
|
||||
continue;
|
||||
}
|
||||
if (nowMs - tsMs > days * DAY_MS) {
|
||||
dropped.stale[type] = (dropped.stale[type] || 0) + 1;
|
||||
continue;
|
||||
}
|
||||
kept.push(e);
|
||||
}
|
||||
return { kept, dropped };
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
const claudeHome = args.home || join(homedir(), ".claude");
|
||||
const adamRoot = join(claudeHome, "adam");
|
||||
const activeJournal = join(adamRoot, "journal.jsonl");
|
||||
const journalDir = join(adamRoot, "journal");
|
||||
const appliedDir = join(adamRoot, "applied");
|
||||
const rejectedDir = join(adamRoot, "rejected");
|
||||
|
||||
const sources = [activeJournal, ...listJsonlFiles(journalDir)];
|
||||
const all = [];
|
||||
for (const p of sources) {
|
||||
for (const e of readJsonlSafe(p)) all.push(e);
|
||||
}
|
||||
|
||||
const exclusion = buildExclusionSet(appliedDir, rejectedDir);
|
||||
const { kept, dropped } = filterEntries(all, exclusion);
|
||||
|
||||
// Stable output: sort by ts ascending so downstream clustering sees chronological order.
|
||||
kept.sort((a, b) => (a.ts < b.ts ? -1 : a.ts > b.ts ? 1 : 0));
|
||||
|
||||
const out = kept.map((e) => JSON.stringify(e)).join("\n");
|
||||
if (out) process.stdout.write(out + "\n");
|
||||
|
||||
const staleParts = Object.entries(dropped.stale).map(([t, n]) => `${t}=${n}`).join(",") || "none";
|
||||
process.stderr.write(
|
||||
`windowed: ${all.length} in, ${kept.length} out (stale: ${staleParts}; excluded: ${dropped.excluded}; no_ts: ${dropped.no_ts})\n`
|
||||
);
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
try { main(); } catch (e) {
|
||||
process.stderr.write(`adam-window error: ${e.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
Regular → Executable
+988
-6
File diff suppressed because it is too large
Load Diff
+153
-2
@@ -20,7 +20,30 @@ You MUST obey these on every proposal:
|
||||
|
||||
## Inputs
|
||||
|
||||
Paths arrive via the dispatch prompt — see `~/.claude/skills/adam-self-improvement/SKILL.md` §1.
|
||||
Paths arrive via the dispatch prompt — see `~/.claude/skills/adam-self-improvement/SKILL.md` §2.
|
||||
|
||||
## Analysis window
|
||||
|
||||
The journal you receive is **pre-filtered** by `~/.claude/adam/scripts/adam-window.mjs` before this agent runs. You do NOT apply window math yourself — every entry in the input stream is already within its signal type's freshness window. The same script also drops entries whose `ts` already appears in `applied/*.md` or `rejected/*.md` frontmatter `source_entries`, so the manual excluded-timestamps computation in the Process section below becomes a no-op when the pre-filter is healthy (still keep the logic — it's the fallback if the pre-filter is bypassed).
|
||||
|
||||
Per-signal windows (single source of truth: `SIGNAL_WINDOWS_DAYS` in `~/.claude/adam/scripts/adam-window.mjs`):
|
||||
|
||||
| signal | window | rationale |
|
||||
|---|---|---|
|
||||
| `dead_end` | 7 d | autonomy friction — fix-or-forget fast |
|
||||
| `correction` | 30 d | user phrasing patterns drift slowly |
|
||||
| `tool_error_loop` | 30 d | error fingerprints stable across days |
|
||||
| `edit_churn` | 14 d | per-file churn is task-local |
|
||||
| `retry_loop` | 14 d | tool-arg retries are task-local |
|
||||
| `build_loop` | 30 d | build/test failure patterns |
|
||||
| `weak_agent` | 30 d | subagent quality signal |
|
||||
| `subagent_dispatch_pattern` | 30 d | dispatch routing pattern |
|
||||
| `correction_free_streak` | 60 d | wins accumulate slowly |
|
||||
| `clean_recovery` | 60 d | wins accumulate slowly |
|
||||
| `task_completed` | 60 d | recipe wins accumulate slowly |
|
||||
| (unknown / new types) | 30 d | `DEFAULT_WINDOW_DAYS` fallback |
|
||||
|
||||
Cross-session evidence gate: "≥3 occurrences across ≥2 sessions" is now scoped — it means **≥3 occurrences across ≥2 sessions WITHIN the signal's analysis window**. Entries that fall outside the window are not visible to clustering or scoring at all.
|
||||
|
||||
## Signal types
|
||||
|
||||
@@ -231,6 +254,55 @@ A `skill_edit` proposal sets `auto_apply_eligible: true` ONLY when ALL hold:
|
||||
|
||||
If any of (3)–(9) fails: still emit the proposal, but `auto_apply_eligible: false` — main thread queues for review.
|
||||
|
||||
## Per-(skill, fingerprint) cooldown
|
||||
|
||||
The cooldown gate is keyed on **(target_skill, proposal_fingerprint)** — not on target_skill alone. A rejected/applied proposal for skill `X` with fingerprint `A` does NOT block future proposals for skill `X` with fingerprint `B`.
|
||||
|
||||
`proposal_fingerprint` is computed deterministically as `djb2(skill_slug + "\n" + signal_cluster_id + "\n" + normalized_diff_body)` returned as base36, where:
|
||||
|
||||
- `skill_slug` — target skill basename (or proposed slug for `skill_new`)
|
||||
- `signal_cluster_id` — the cluster id you assigned in the clustering trace (e.g. `c1`, `tool_error_loop-ECONNREFUSED:5432`)
|
||||
- `normalized_diff_body` — proposal's `# Proposed change` section with all whitespace collapsed to single spaces and trailing newlines stripped
|
||||
|
||||
Both apply-time and analyst-time checks invoke `adam-cooldown.mjs --skill <slug> --fingerprint <hash>`. The script returns one of `{"status":"cool"}`, `{"status":"cooldown",...}`, or `{"status":"blacklisted",...}`. Auto-apply requires `cool`.
|
||||
|
||||
Backward compat: proposals from before this rubric version (no `proposal_fingerprint` field) are treated as `fingerprint = "legacy"`. The cooldown script matches legacy applied/rejected records against any query fingerprint for the same skill — i.e. coarse-grained gating until those records age out of their windows (7d / 30d).
|
||||
|
||||
## Scoring: task_completed dampener
|
||||
|
||||
Before scoring each cluster's confidence, multiply the cluster's urgency score by the `dampener` value reported by `adam-score.mjs` for the session the cluster originated from:
|
||||
|
||||
- `task_completed_count >= 3` in that session → dampener `0.5`
|
||||
- `task_completed_count >= 1` in that session → dampener `0.75`
|
||||
- otherwise → dampener `1.0`
|
||||
|
||||
Rationale: sessions that successfully closed several multi-tool tasks alongside the friction signal are noisier proposal sources than sessions that produced only friction. The dampener does not zero out signals; it down-weights urgency so cross-session friction beats single-session friction-with-recoveries.
|
||||
|
||||
The skill (`adam-self-improvement/SKILL.md` §1) runs `adam-score.mjs` immediately after `adam-window.mjs` and passes both outputs into the analyst's dispatch prompt.
|
||||
|
||||
## A/B effectiveness
|
||||
|
||||
Every auto-applied edit (`skill_edit`, `skill_new`, `memory`, `nudge`, `reinforcement`) gets a one-line tracking entry written to `~/.claude/adam/ab-tracking.jsonl` by `adam-self-improvement/SKILL.md` immediately after the proposal is moved to `applied/`. Schema:
|
||||
|
||||
```json
|
||||
{"applied_at":<ms>,"proposal_id":"<id>","proposal_type":"...","target_skill":"<slug>","proposal_fingerprint":"<hash>","originating_signals":[{"type":"<signal>","count":<N>,"session_ids":[...]}],"pre_window_days":7}
|
||||
```
|
||||
|
||||
After ≥7 days, `~/.claude/adam/scripts/adam-ab-measure.mjs` reads each entry and compares signal counts in the 7-day window BEFORE `applied_at` against the 7-day window AFTER (raw journal counts — does NOT use `adam-window.mjs` filtering). Status assignment:
|
||||
|
||||
- `delta_pct = (post - pre) / pre * 100`
|
||||
- `pre == 0` → `no_baseline` (cold start, no measurement possible)
|
||||
- `delta_pct <= -25` → `improved`
|
||||
- `-25 < delta_pct < 25` → `neutral`
|
||||
- `delta_pct >= 25` → `regressed`
|
||||
- entry younger than 7 days → `pending`
|
||||
|
||||
The `/reflect` skill runs `adam-ab-measure.mjs --format json` before dispatching this agent, filters to `status == "regressed"`, and passes the list as `ab_regressions` (each object has `proposal_id`, `target_skill`, `proposal_type`, `delta_pct`, `pre_count`, `post_count`).
|
||||
|
||||
**When `ab_regressions` is non-empty, you MUST emit a `## Regressions` section at the TOP of your output (above the proposals listing).** One bullet per regressed proposal listing `proposal_id`, `target_skill`, `delta_pct`, plus the short suggestion `consider revert via /reflect --revert <proposal_id>` (the revert mechanism itself is out of scope for this release — the message stands as a hint).
|
||||
|
||||
The clustering trace summary (see §"Clustering trace") adds an extra `regressions=<N>` key alongside `considered/emitted/skipped`. When no `ab_regressions` arrive (or list is empty), emit `regressions=0`.
|
||||
|
||||
## Confidence rubric (deterministic — do NOT vibe)
|
||||
|
||||
Sum:
|
||||
@@ -257,12 +329,40 @@ Sum:
|
||||
| `memory` | `~/.claude/projects/-Users-nvm/memory/*.md` | low | yes if conf≥4 AND cross_session |
|
||||
| `skill_new` | new dir under `~/.claude/skills/` | low | yes if conf≥4 AND cross_session |
|
||||
| `skill_edit` | existing skill file | medium | yes if win-evidence + LOC + cooldown gates all pass (see "Win-driven skill_edit eligibility") |
|
||||
| `nudge` | append to `~/.claude/adam/active-nudges.json` | low | yes when `dead_end_count ≥ 3` in a single session (single-session evidence sufficient; skips cross-session gate). Does NOT modify skills/memories/CLAUDE.md — only seeds a SessionStart reminder for a future session. |
|
||||
| `reinforcement` | append entry to `~/.claude/adam/reinforcements.jsonl` | low | yes if conf≥4 AND blast_radius=low (same gate as memory). Applies via `adam-apply-reinforcement.mjs`; appends one JSONL entry, no code/memory/skill changes. |
|
||||
| `agent_new` | new file under `~/.claude/agents/` | medium | no |
|
||||
| `agent_edit` | existing agent file | medium | no |
|
||||
| `claude_md_edit` | `~/.claude/CLAUDE.md` | high | no |
|
||||
| `hook_new` / `hook_edit` | `settings.json` hooks | high | no |
|
||||
| `deletion` | any skill/agent (soft delete) | high | no |
|
||||
|
||||
### `nudge` proposals
|
||||
|
||||
A `nudge` proposal does NOT modify any persistent rubric/skill/memory artifact. Its sole side-effect is to append an entry to `~/.claude/adam/active-nudges.json` so the next SessionStart hook surfaces a one-line reminder to the user in a *different* session.
|
||||
|
||||
Trigger: `adam-nudge-eligibility.mjs --session <id>` returns `eligible: true` (i.e. ≥3 `dead_end` entries inside a single session). Distinguished from `skill_edit` precisely because there is no learning artifact to mutate — the action surfaces a checkpoint reminder, not a behavior change.
|
||||
|
||||
`active-nudges.json` entry shape (created by the skill at apply time):
|
||||
|
||||
```json
|
||||
{
|
||||
"kind": "dead_end_reminder",
|
||||
"message": "adam: previous session hit 3 dead_ends — consider a checkpoint before continuing.",
|
||||
"created_at": <ms>,
|
||||
"expires_at_ts": <ms now + 7 days>,
|
||||
"max_displays": 3,
|
||||
"displays_used": 0,
|
||||
"source_session": "<originating session_id>"
|
||||
}
|
||||
```
|
||||
|
||||
### `reinforcement` proposals
|
||||
|
||||
A `reinforcement` proposal is logged when `adam-score.mjs` reports `count >= 3` clean `task_completed` events citing the same `active_skills[0]` slug. Frontmatter MUST include `skill_slug`, `count`, `source_session`, `confidence`, `blast_radius: low`. Apply gate (`confidence >= 4 AND blast_radius == low`) is identical to the `memory` gate — when both hold, the skill invokes `~/.claude/adam/scripts/adam-apply-reinforcement.mjs <proposal-path>` which appends one JSON line to `~/.claude/adam/reinforcements.jsonl` of shape `{ts, skill_slug, count, source_session}`. No code/memory/skill modifications either side of the gate — reinforcements are a positive-only ledger, separate from `ab-tracking.jsonl` (A/B intentionally does NOT measure positive signals to avoid skewing regression detection).
|
||||
|
||||
Note that `task_completed` alone — without an adjacent negative signal cluster — is NOT a proposal source. It is a urgency *modifier* (see "Scoring: task_completed dampener") and a reinforcement input only.
|
||||
|
||||
## Special handling
|
||||
|
||||
### CLAUDE.md edits
|
||||
@@ -289,7 +389,7 @@ Filename: `proposals_dir/YYYY-MM-DD-NNN-<type>-<slug>.md` (NNN is daily counter
|
||||
```markdown
|
||||
---
|
||||
id: YYYY-MM-DD-NNN
|
||||
type: skill_new | memory | skill_edit | agent_new | agent_edit | claude_md_edit | hook_new | hook_edit | deletion
|
||||
type: skill_new | memory | skill_edit | nudge | reinforcement | agent_new | agent_edit | claude_md_edit | hook_new | hook_edit | deletion
|
||||
target: <absolute path — for skill_new, the will-be path: ~/.claude/skills/<slug>/SKILL.md>
|
||||
confidence: <int>
|
||||
blast_radius: low | medium | high
|
||||
@@ -301,6 +401,12 @@ source_entries:
|
||||
- "<journal entry ts that fed this cluster>"
|
||||
- "<another ts>"
|
||||
- "..."
|
||||
# skill_edit / skill_new — required for cooldown gate (see "Per-(skill, fingerprint) cooldown" below)
|
||||
proposal_fingerprint: "<djb2_base36 hash — computed via computeProposalFingerprint() in adam-cooldown.mjs>"
|
||||
target_skill: "<slug — populated for skill_edit (basename of target dir) and skill_new (proposed slug)>"
|
||||
# A/B effectiveness — required on every proposal; consumed at apply time to seed ab-tracking.jsonl
|
||||
originating_signals:
|
||||
- {type: "<signal_type>", count: <N>, session_ids: ["<sid>", "..."]}
|
||||
# skill_edit only — required when auto_apply_eligible: true
|
||||
win_evidence: "<ts of triggering clean_recovery or correction_free_streak entry>"
|
||||
bytes_before: <int>
|
||||
@@ -351,3 +457,48 @@ Print a single JSON line to stdout:
|
||||
- Do not delete files. Deletion proposals describe a soft-move; the main thread executes it.
|
||||
- Do not write outside `proposals_dir/` and `state_path`.
|
||||
- Do not invent trigger phrases for `skill_new` — every trigger must come from observed user input.
|
||||
|
||||
## Clustering trace (always emit)
|
||||
|
||||
After your proposals are written and BEFORE the final punch-list JSON line, you MUST emit a fenced code block tagged ` ```trace ` containing one line per cluster considered during this pass. This is mandatory regardless of whether any proposals were emitted, and regardless of any flags. The skill controls whether to SHOW this block to the user; you always produce it.
|
||||
|
||||
Line format (one cluster per line, all four pipe-separated chunks required):
|
||||
|
||||
```
|
||||
<cluster_id> | signal=<type> count=<N> sessions=<M> | gates: threshold=<pass|fail:<reason>>, cross_session=<pass|fail>, window=<in:<N>/out:<M>>, contradiction=<none|vetoed:[[memory-name]]> | decision: <proposal_emitted:<type>|skipped:<reason>>
|
||||
```
|
||||
|
||||
Field semantics:
|
||||
|
||||
- `cluster_id` — short stable identifier you assign per cluster this pass (e.g. `c1`, `c2`, …, or `<signal>-<short-key>`). Used by humans + adam-explain.mjs.
|
||||
- `signal=<type>` — the journal signal type (e.g. `correction`, `dead_end`).
|
||||
- `count=<N>` — number of journal entries that fell into this cluster.
|
||||
- `sessions=<M>` — distinct session ids contributing.
|
||||
- `gates:` — four sub-fields, all required:
|
||||
- `threshold=pass` if the cluster met the "≥3 across ≥2 sessions" (or single-session struggle) rubric gate, else `fail:<short reason>` (e.g. `fail:only_1_session`, `fail:count_below_3`).
|
||||
- `cross_session=pass|fail` — boolean restatement matching the `cross_session_evidence` rubric field.
|
||||
- `window=in:<N>/out:<M>` — entries that survived per-signal sliding window vs entries dropped as stale. Pre-filter from `adam-window.mjs` makes `out` usually 0; record what you observed.
|
||||
- `contradiction=none` for non-skill_edit clusters; for `skill_edit` set `vetoed:[[<memory-or-skill-name>]]` when the contradiction heuristic flagged a conflict, else `none`.
|
||||
- `decision:` — one of:
|
||||
- `proposal_emitted:<type>` (e.g. `proposal_emitted:memory`, `proposal_emitted:skill_new`).
|
||||
- `skipped:<reason>` where reason is a single token from `{threshold, contradiction, window, rejected-similar, type-bias, deletion-criteria, claude-md-scope, overlap, other}`.
|
||||
|
||||
After the cluster lines, emit exactly one summary line (this trailing line is REQUIRED — adam-explain.mjs falls back to synthesising it from the cluster lines if you omit it, but you should always write it):
|
||||
|
||||
```
|
||||
SUMMARY: considered=<N> emitted=<M> skipped=<N-M> regressions=<R> reasons={threshold:X, contradiction:Y, window:Z, other:W}
|
||||
```
|
||||
|
||||
`reasons` keys: the same skip-reason tokens used in `decision:`; values are counts; include all four canonical keys (`threshold`, `contradiction`, `window`, `other`) even when zero — `other` is the catch-all for any reason not in the first three. `regressions=<R>` is the count of entries with `status == "regressed"` in the `ab_regressions` input (0 when empty/absent — see §"A/B effectiveness").
|
||||
|
||||
Worked example (4 clusters, 2 emitted, 2 skipped):
|
||||
|
||||
```trace
|
||||
c1 | signal=correction count=5 sessions=3 | gates: threshold=pass, cross_session=pass, window=in:5/out:0, contradiction=none | decision: proposal_emitted:memory
|
||||
c2 | signal=dead_end count=1 sessions=1 | gates: threshold=pass, cross_session=fail, window=in:1/out:0, contradiction=none | decision: proposal_emitted:skill_new
|
||||
c3 | signal=retry_loop count=2 sessions=1 | gates: threshold=fail:count_below_3, cross_session=fail, window=in:2/out:0, contradiction=none | decision: skipped:threshold
|
||||
c4 | signal=tool_error_loop count=4 sessions=2 | gates: threshold=pass, cross_session=pass, window=in:4/out:6, contradiction=none | decision: skipped:window
|
||||
SUMMARY: considered=4 emitted=2 skipped=2 regressions=0 reasons={threshold:1, contradiction:0, window:1, other:0}
|
||||
```
|
||||
|
||||
Clusters that were filtered out entirely BEFORE clustering (e.g. excluded by `applied/*.md` `source_entries`) do not appear here — only clusters that the agent actually considered as candidates. Note: the trace lives entirely in your final assistant message, alongside the punch-list JSON; nothing else writes to disk on the agent side.
|
||||
|
||||
+123
-8
@@ -1,16 +1,131 @@
|
||||
#!/usr/bin/env node
|
||||
import { readdirSync } from "node:fs";
|
||||
// adam-nudge.mjs — SessionStart hook. Prints two kinds of reminders:
|
||||
// 1. Pending proposals (≥3 queued in adam/proposals/).
|
||||
// 2. Cross-session nudges (entries in adam/active-nudges.json whose
|
||||
// source_session differs from the current session and that haven't
|
||||
// expired or exhausted their max_displays).
|
||||
import { readdirSync, readFileSync, writeFileSync, existsSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
|
||||
const PROPOSALS = join(homedir(), ".claude", "adam", "proposals");
|
||||
const HOME = process.env.HOME || homedir();
|
||||
const CLAUDE_ROOT = join(HOME, ".claude");
|
||||
const ADAM_ROOT = join(CLAUDE_ROOT, "adam");
|
||||
const PROPOSALS = join(ADAM_ROOT, "proposals");
|
||||
const NUDGES_FILE = join(ADAM_ROOT, "active-nudges.json");
|
||||
const STATE_FILE = join(ADAM_ROOT, "state.json");
|
||||
const THRESHOLD = 3;
|
||||
|
||||
try {
|
||||
const PROPOSAL_RE = /^\d{4}-\d{2}-\d{2}-\d{3}-/;
|
||||
const files = readdirSync(PROPOSALS).filter(f => PROPOSAL_RE.test(f) && f.endsWith(".md"));
|
||||
if (files.length >= THRESHOLD) {
|
||||
process.stdout.write(`adam: ${files.length} proposals queued. Run /reflect to review.\n`);
|
||||
// Known installable paths (mirrors install.sh copy_file list). Checking a
|
||||
// fixed shortlist keeps SessionStart latency under control vs full FS walk.
|
||||
const PENDING_CHECK_PATHS = [
|
||||
"hooks/adam-observe.mjs",
|
||||
"hooks/adam-nudge.mjs",
|
||||
"agents/adam.md",
|
||||
"skills/adam-self-improvement/SKILL.md",
|
||||
"commands/reflect.md",
|
||||
"adam/scripts/adam-archive.mjs",
|
||||
"adam/scripts/adam-upgrade.mjs",
|
||||
"adam/scripts/adam-window.mjs",
|
||||
"adam/scripts/adam-explain.mjs",
|
||||
"adam/scripts/adam-nudge-eligibility.mjs",
|
||||
"adam/scripts/adam-cooldown.mjs",
|
||||
"adam/scripts/adam-score.mjs",
|
||||
"adam/scripts/adam-ab-measure.mjs",
|
||||
"adam/scripts/adam-apply-reinforcement.mjs",
|
||||
"adam/tests/run-tests.sh",
|
||||
];
|
||||
|
||||
function readJson(path, fallback) {
|
||||
if (!existsSync(path)) return fallback;
|
||||
try { return JSON.parse(readFileSync(path, "utf8")); } catch { return fallback; }
|
||||
}
|
||||
|
||||
function readSessionInput() {
|
||||
// SessionStart payload arrives on stdin; capture session_id if present.
|
||||
// We don't block on stdin — best-effort, non-blocking.
|
||||
try {
|
||||
const buf = readFileSync(0, "utf8");
|
||||
if (!buf) return null;
|
||||
const parsed = JSON.parse(buf);
|
||||
return parsed && typeof parsed.session_id === "string" ? parsed.session_id : null;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
function emitProposalReminder() {
|
||||
try {
|
||||
const PROPOSAL_RE = /^\d{4}-\d{2}-\d{2}-\d{3}-/;
|
||||
const files = readdirSync(PROPOSALS).filter((f) => PROPOSAL_RE.test(f) && f.endsWith(".md"));
|
||||
if (files.length >= THRESHOLD) {
|
||||
process.stdout.write(`adam: ${files.length} proposals queued. Run /reflect to review.\n`);
|
||||
}
|
||||
} catch { /* proposals dir absent → silent */ }
|
||||
}
|
||||
|
||||
function emitActiveNudges(currentSession) {
|
||||
if (!existsSync(NUDGES_FILE)) return;
|
||||
const raw = readJson(NUDGES_FILE, null);
|
||||
if (!Array.isArray(raw)) return;
|
||||
const now = Date.now();
|
||||
const kept = [];
|
||||
let mutated = false;
|
||||
for (const entry of raw) {
|
||||
if (!entry || typeof entry !== "object") { mutated = true; continue; }
|
||||
const expires = Number(entry.expires_at_ts || 0);
|
||||
if (!expires || expires <= now) { mutated = true; continue; }
|
||||
const sourceSession = entry.source_session || "";
|
||||
const max = Number(entry.max_displays || 0);
|
||||
const used = Number(entry.displays_used || 0);
|
||||
if (max > 0 && used >= max) { mutated = true; continue; }
|
||||
// Cross-session gate: only print when current session differs.
|
||||
if (sourceSession && currentSession && sourceSession === currentSession) {
|
||||
kept.push(entry);
|
||||
continue;
|
||||
}
|
||||
if (typeof entry.message === "string" && entry.message) {
|
||||
process.stdout.write(entry.message + "\n");
|
||||
const nextUsed = used + 1;
|
||||
mutated = true;
|
||||
if (max > 0 && nextUsed >= max) continue; // drop after exhaustion
|
||||
kept.push({ ...entry, displays_used: nextUsed });
|
||||
} else {
|
||||
kept.push(entry);
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
if (mutated) {
|
||||
try { writeFileSync(NUDGES_FILE, JSON.stringify(kept, null, 2)); } catch { /* swallow */ }
|
||||
}
|
||||
}
|
||||
|
||||
function emitPendingUpgrades() {
|
||||
// Cheap: stat a fixed shortlist of `.adam-new` candidates. Non-fatal.
|
||||
try {
|
||||
let count = 0;
|
||||
for (const rel of PENDING_CHECK_PATHS) {
|
||||
const p = join(CLAUDE_ROOT, `${rel}.adam-new`);
|
||||
try {
|
||||
if (existsSync(p)) count++;
|
||||
} catch { /* per-path swallow */ }
|
||||
}
|
||||
if (count > 0) {
|
||||
process.stdout.write(
|
||||
`[adam] ${count} pending upgrade(s). Review: node ~/.claude/adam/scripts/adam-upgrade.mjs --list\n`
|
||||
);
|
||||
}
|
||||
} catch { /* never break SessionStart */ }
|
||||
}
|
||||
|
||||
function main() {
|
||||
const stdinSession = readSessionInput();
|
||||
const stateSession = (() => {
|
||||
const st = readJson(STATE_FILE, null);
|
||||
return st && typeof st.session_id === "string" ? st.session_id : null;
|
||||
})();
|
||||
const currentSession = stdinSession || stateSession || "";
|
||||
emitProposalReminder();
|
||||
emitActiveNudges(currentSession);
|
||||
emitPendingUpgrades();
|
||||
}
|
||||
|
||||
try { main(); } catch { /* never block SessionStart */ }
|
||||
process.exit(0);
|
||||
|
||||
+174
-17
@@ -14,8 +14,76 @@ const JOURNAL = join(ROOT, "journal.jsonl");
|
||||
const STATE = join(ROOT, "state.json");
|
||||
const USAGE = join(ROOT, "usage.json");
|
||||
const JOURNAL_DIR = join(ROOT, "journal");
|
||||
// Safety fuse only — primary rotation is weekly (ISO Monday 00:00 UTC).
|
||||
// If active journal exceeds this even mid-week, force-rotate to avoid runaway growth.
|
||||
// Override via $ADAM_MAX_JOURNAL_BYTES (used by tests).
|
||||
const MAX_JOURNAL_BYTES = Number(process.env.ADAM_MAX_JOURNAL_BYTES) || 50 * 1024 * 1024;
|
||||
|
||||
const CORRECTION_RE = /\b(no|stop|don't|don\'t|wrong|actually|nope|undo|revert)\b/i;
|
||||
// Strong-correction tokens: any single occurrence in a prompt is a correction.
|
||||
// Weak tokens (no/actually/wait) require co-occurrence with a negation/contrast
|
||||
// token within an 8-token window — see isCorrection() below.
|
||||
const CORRECTION_RE = /\b(stop|don't|don\'t|wrong|nope|undo|revert|incorrect|nevermind|never\s+mind|disregard|redo)\b|that's\s+wrong|hold\s+on|wait\s+wait|try\s+again|different\s+approach|that's\s+not\s+what\s+i\s+meant|not\s+what\s+i\s+wanted|start\s+over|go\s+back/i;
|
||||
const WEAK_CORRECTION_TOKENS = new Set(["no", "actually", "wait"]);
|
||||
const NEGATION_RE = /^(not|wrong|but|isn't|isn\'t|didn't|didn\'t|aren't|aren\'t|won't|won\'t|shouldn't|shouldn\'t|don't|don\'t|nope|bad|broken|fail|fails|failed|failing)$/i;
|
||||
const WEAK_WINDOW = 8;
|
||||
|
||||
function isCorrection(text) {
|
||||
if (!text || typeof text !== "string") return false;
|
||||
if (CORRECTION_RE.test(text)) return true;
|
||||
// Weak-token path: token must co-occur with a negation/contrast within WEAK_WINDOW tokens.
|
||||
const tokens = text.toLowerCase().split(/\s+/).map(t => t.replace(/^[^\w']+|[^\w']+$/g, "")).filter(Boolean);
|
||||
for (let i = 0; i < tokens.length; i++) {
|
||||
if (!WEAK_CORRECTION_TOKENS.has(tokens[i])) continue;
|
||||
const lo = Math.max(0, i - WEAK_WINDOW);
|
||||
const hi = Math.min(tokens.length - 1, i + WEAK_WINDOW);
|
||||
for (let j = lo; j <= hi; j++) {
|
||||
if (j === i) continue;
|
||||
if (NEGATION_RE.test(tokens[j])) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Canonical error codes. Surface text → code mapping below.
|
||||
const ERROR_CODES = new Set([
|
||||
"ENOENT", "ECONNREFUSED", "ETIMEDOUT", "EACCES", "EPERM", "EADDRINUSE",
|
||||
"ENOTFOUND", "EISDIR", "ENOTDIR", "EEXIST", "EMFILE", "EPIPE", "ECONNRESET"
|
||||
]);
|
||||
const ERROR_CODE_RE = /\b(ENOENT|ECONNREFUSED|ETIMEDOUT|EACCES|EPERM|EADDRINUSE|ENOTFOUND|EISDIR|ENOTDIR|EEXIST|EMFILE|EPIPE|ECONNRESET)\b/;
|
||||
// Phrase → code mapping. First match wins; order matters.
|
||||
const ERROR_PHRASE_MAP = [
|
||||
[/no such file or directory/i, "ENOENT"],
|
||||
[/connection refused/i, "ECONNREFUSED"],
|
||||
[/permission denied/i, "EACCES"],
|
||||
[/address already in use/i, "EADDRINUSE"],
|
||||
[/connection reset/i, "ECONNRESET"],
|
||||
[/operation timed out/i, "ETIMEDOUT"],
|
||||
[/name resolution|getaddrinfo/i, "ENOTFOUND"],
|
||||
];
|
||||
|
||||
function normalizeErrorText(text) {
|
||||
if (!text || typeof text !== "string") return "";
|
||||
let s = text;
|
||||
// ISO timestamps first (contain digits we'd otherwise strip individually).
|
||||
s = s.replace(/\d{4}-\d{2}-\d{2}T[\d:.Z+-]+/g, " ");
|
||||
// Windows paths.
|
||||
s = s.replace(/[A-Z]:\\[^\s]+/g, " ");
|
||||
// Absolute POSIX paths.
|
||||
s = s.replace(/\/[^\s:]+/g, " ");
|
||||
// Hex addresses.
|
||||
s = s.replace(/0x[0-9a-f]+/gi, " ");
|
||||
// Unix epoch (seconds or ms): 10-13 digit runs.
|
||||
s = s.replace(/\b\d{10,13}\b/g, " ");
|
||||
// Line/col refs.
|
||||
s = s.replace(/:\d+(?::\d+)?/g, " ");
|
||||
// UUIDs.
|
||||
s = s.replace(/\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/gi, " ");
|
||||
// Large integers (>6 digits) that survived above.
|
||||
s = s.replace(/\b\d{7,}\b/g, " ");
|
||||
// Lowercase + collapse whitespace.
|
||||
s = s.toLowerCase().replace(/\s+/g, " ").trim();
|
||||
return s.slice(0, 80);
|
||||
}
|
||||
const ERROR_RE = /\b(error|failed|exception|traceback|denied|cannot|unable to|not found|undefined|nullpointer|typeerror|syntaxerror|panic|fatal|enoent|econnrefused|etimedout|eaccess|segfault|crashed|uncaught)\b/i;
|
||||
const BUILD_RE = /\b(build|compile|make|gradle|cargo|tsc|webpack|vite|rollup|pytest|jest|mocha|vitest|go\s+test|npm\s+test|yarn\s+test|npm\s+run\s+build|yarn\s+build|ctest|ninja|bazel)\b/i;
|
||||
const EDIT_TOOLS = new Set(["Edit", "Write", "MultiEdit", "NotebookEdit"]);
|
||||
@@ -44,14 +112,69 @@ function safeWrite(path, obj) {
|
||||
try { writeFileSync(path, JSON.stringify(obj)); } catch {}
|
||||
}
|
||||
|
||||
function rotateIfLarge(path, max) {
|
||||
// ISO-8601 week: returns { year, week } for a Date (UTC).
|
||||
// Week 1 = the week containing the first Thursday of the year (Monday-based weeks).
|
||||
function isoWeek(date) {
|
||||
const d = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
|
||||
// Shift to Thursday in current week (ISO week-numbering year tracks the Thursday).
|
||||
const day = d.getUTCDay() || 7; // 1..7, Mon=1..Sun=7
|
||||
d.setUTCDate(d.getUTCDate() + 4 - day);
|
||||
const isoYear = d.getUTCFullYear();
|
||||
const yearStart = new Date(Date.UTC(isoYear, 0, 1));
|
||||
const week = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
|
||||
return { year: isoYear, week };
|
||||
}
|
||||
|
||||
function isoWeekTag(date) {
|
||||
const { year, week } = isoWeek(date);
|
||||
return `${year}-W${String(week).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
function firstEntryTs(path) {
|
||||
try {
|
||||
if (existsSync(path) && statSync(path).size > max) {
|
||||
mkdirSync(JOURNAL_DIR, { recursive: true });
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
const dest = join(JOURNAL_DIR, `${today}-${Date.now()}.jsonl`);
|
||||
renameSync(path, dest);
|
||||
const buf = readFileSync(path, "utf8");
|
||||
const nl = buf.indexOf("\n");
|
||||
const firstLine = nl === -1 ? buf : buf.slice(0, nl);
|
||||
if (!firstLine.trim()) return null;
|
||||
const obj = JSON.parse(firstLine);
|
||||
return obj && typeof obj.ts === "string" ? obj.ts : null;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
// Weekly ISO rotation + size safety fuse.
|
||||
// - If active journal's first entry is in a different ISO week than now, rotate to
|
||||
// journal/<that-entry's-iso-week>.jsonl and start fresh.
|
||||
// - If active journal exceeds MAX_JOURNAL_BYTES, force-rotate even mid-week
|
||||
// using the current ISO week tag (suffixed with timestamp to avoid clobber).
|
||||
function rotateIfNeeded(path) {
|
||||
try {
|
||||
if (!existsSync(path)) return;
|
||||
const size = statSync(path).size;
|
||||
if (size === 0) return;
|
||||
const now = new Date();
|
||||
const currentTag = isoWeekTag(now);
|
||||
const firstTs = firstEntryTs(path);
|
||||
let rotate = false;
|
||||
let destTag = null;
|
||||
if (firstTs) {
|
||||
const firstTag = isoWeekTag(new Date(firstTs));
|
||||
if (firstTag !== currentTag) {
|
||||
rotate = true;
|
||||
destTag = firstTag;
|
||||
}
|
||||
}
|
||||
if (!rotate && size > MAX_JOURNAL_BYTES) {
|
||||
rotate = true;
|
||||
destTag = `${currentTag}-${Date.now()}`; // safety-fuse: keep mid-week rotations unique
|
||||
}
|
||||
if (!rotate) return;
|
||||
mkdirSync(JOURNAL_DIR, { recursive: true });
|
||||
let dest = join(JOURNAL_DIR, `${destTag}.jsonl`);
|
||||
if (existsSync(dest)) {
|
||||
// Append-merge collision (rare: two mid-week safety-fuse rotations in same ms).
|
||||
dest = join(JOURNAL_DIR, `${destTag}-${Date.now()}.jsonl`);
|
||||
}
|
||||
renameSync(path, dest);
|
||||
} catch {}
|
||||
}
|
||||
|
||||
@@ -65,7 +188,7 @@ function readStdin() {
|
||||
}
|
||||
|
||||
function appendJournal(entry) {
|
||||
rotateIfLarge(JOURNAL, STATE_MAX_BYTES * 5);
|
||||
rotateIfNeeded(JOURNAL);
|
||||
try {
|
||||
appendFileSync(JOURNAL, JSON.stringify(entry) + "\n");
|
||||
} catch {}
|
||||
@@ -107,15 +230,34 @@ function errorFingerprint(toolResponse) {
|
||||
}
|
||||
if (!text) return null;
|
||||
text = text.slice(0, 4000);
|
||||
// ERROR_RE fallback covers tools that omit `is_error` entirely (text-only
|
||||
// responses, third-party tools). Explicit `is_error: false` is honored as-is
|
||||
// — the regex is NOT used to second-guess a tool that already declared success.
|
||||
const isError = toolResponse.is_error === true ||
|
||||
(toolResponse.is_error === undefined && ERROR_RE.test(text));
|
||||
if (!isError) return null;
|
||||
const m = text.match(ERROR_RE);
|
||||
const idx = m && typeof m.index === "number" ? m.index : 0;
|
||||
const start = Math.max(0, idx - 20);
|
||||
const slice = text.slice(start, start + 80).toLowerCase().replace(/\s+/g, " ").trim();
|
||||
if (!slice) return null;
|
||||
return djb2(slice);
|
||||
|
||||
// 1. Try canonical code (literal token first, then phrase mapping).
|
||||
let code = null;
|
||||
const codeMatch = text.match(ERROR_CODE_RE);
|
||||
if (codeMatch && ERROR_CODES.has(codeMatch[1])) {
|
||||
code = codeMatch[1];
|
||||
} else {
|
||||
for (const [re, mapped] of ERROR_PHRASE_MAP) {
|
||||
if (re.test(text)) { code = mapped; break; }
|
||||
}
|
||||
}
|
||||
|
||||
// 2. When canonical code matched, the bucket key IS the code — residual
|
||||
// surface text (ports, hostnames, syscall names) varies across instances
|
||||
// of the same root cause, so we hash a fixed sentinel for stability.
|
||||
// When no code matched, normalize residual and hash it for the raw bucket.
|
||||
if (code) {
|
||||
return `${code}:${djb2(code)}`;
|
||||
}
|
||||
const normalized = normalizeErrorText(text);
|
||||
if (!normalized) return null;
|
||||
return `raw:${djb2(normalized)}`;
|
||||
}
|
||||
|
||||
function resetFrictionCounters(state) {
|
||||
@@ -163,6 +305,10 @@ function main() {
|
||||
const input = readStdin();
|
||||
if (!input || typeof input !== "object") return;
|
||||
|
||||
// Weekly rotation check at hook entry — ensures the active journal rolls over
|
||||
// even if this invocation appends nothing.
|
||||
rotateIfNeeded(JOURNAL);
|
||||
|
||||
const event = input.hook_event_name;
|
||||
const session = input.session_id || "unknown";
|
||||
const cwd = input.cwd || process.cwd();
|
||||
@@ -177,7 +323,7 @@ function main() {
|
||||
|
||||
if (event === "UserPromptSubmit") {
|
||||
const prompt = (input.prompt || "").slice(0, 200);
|
||||
if (CORRECTION_RE.test(prompt)) {
|
||||
if (isCorrection(prompt)) {
|
||||
const last = state.tool_window[state.tool_window.length - 1] || {};
|
||||
appendJournal({
|
||||
ts, session, cwd, type: "correction",
|
||||
@@ -348,5 +494,16 @@ function main() {
|
||||
safeWrite(STATE, state);
|
||||
}
|
||||
|
||||
try { main(); } catch {}
|
||||
process.exit(0);
|
||||
// Run main only when executed as a script, not when imported for tests.
|
||||
// import.meta.url comparison is the standard ESM idiom.
|
||||
const isMain = (() => {
|
||||
try {
|
||||
return import.meta.url === `file://${process.argv[1]}`;
|
||||
} catch { return true; }
|
||||
})();
|
||||
if (isMain) {
|
||||
try { main(); } catch {}
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
export { errorFingerprint, normalizeErrorText, isCorrection };
|
||||
|
||||
+20
@@ -123,6 +123,15 @@ copy_file "$SRC/skills/adam-self-improvement/SKILL.md" "$DEST/skil
|
||||
copy_file "$SRC/commands/reflect.md" "$DEST/commands/reflect.md"
|
||||
# Adam internals
|
||||
copy_file "$SRC/adam/scripts/adam-archive.mjs" "$DEST/adam/scripts/adam-archive.mjs"
|
||||
copy_file "$SRC/adam/scripts/adam-upgrade.mjs" "$DEST/adam/scripts/adam-upgrade.mjs"
|
||||
# v0.3.3 helper scripts — invoked from SKILL.md / hooks / analyst flow
|
||||
for _adam_script in adam-utils adam-window adam-explain adam-nudge-eligibility adam-cooldown \
|
||||
adam-score adam-ab-measure adam-apply-reinforcement; do
|
||||
copy_file "$SRC/adam/scripts/${_adam_script}.mjs" \
|
||||
"$DEST/adam/scripts/${_adam_script}.mjs"
|
||||
run "chmod +x \"$DEST/adam/scripts/${_adam_script}.mjs\""
|
||||
done
|
||||
run "chmod +x \"$DEST/adam/scripts/adam-upgrade.mjs\""
|
||||
copy_file "$SRC/adam/tests/run-tests.sh" "$DEST/adam/tests/run-tests.sh"
|
||||
copy_file "$SRC/adam/tests/fixtures/seed-corrections.jsonl" "$DEST/adam/tests/fixtures/seed-corrections.jsonl"
|
||||
|
||||
@@ -201,6 +210,7 @@ log " agents/adam.md"
|
||||
log " skills/adam-self-improvement/SKILL.md"
|
||||
log " commands/reflect.md"
|
||||
log " adam/scripts/adam-archive.mjs"
|
||||
log " adam/scripts/adam-upgrade.mjs"
|
||||
log " adam/tests/run-tests.sh"
|
||||
log ""
|
||||
log "preserved (if existed):"
|
||||
@@ -214,3 +224,13 @@ log ""
|
||||
log "ADAM is dormant until you run /reflect."
|
||||
log "journal: $DEST/adam/journal.jsonl"
|
||||
log "proposals: $DEST/adam/proposals/"
|
||||
|
||||
# --------------------------------------------------------------------- pending merges
|
||||
# If this upgrade left any `.adam-new` files behind, make the trap unmissable.
|
||||
PENDING_COUNT=$(find "$DEST" \( -name .git -o -name node_modules -o -path "*/adam/journal" -o -path "*/adam/trash" -o -path "*/adam/proposals" -o -path "*/adam/applied" -o -path "*/adam/rejected" \) -prune -o -type f -name '*.adam-new' -print 2>/dev/null | wc -l | tr -d ' ')
|
||||
if [ "${PENDING_COUNT:-0}" -gt 0 ]; then
|
||||
log ""
|
||||
warn "${PENDING_COUNT} file(s) need merge review."
|
||||
warn " Review: node ~/.claude/adam/scripts/adam-upgrade.mjs --list"
|
||||
warn " Accept: node ~/.claude/adam/scripts/adam-upgrade.mjs --accept <path>"
|
||||
fi
|
||||
|
||||
@@ -8,12 +8,64 @@ description: Use when the user types /reflect, asks "what has adam learned", ask
|
||||
## When to invoke
|
||||
|
||||
- User types `/reflect`
|
||||
- User types `/reflect --explain` (same flow, but the analyst's clustering trace is shown to the user — see §2b below)
|
||||
- User asks: "what has adam learned", "any proposals", "review the queue"
|
||||
- SessionStart nudge said proposals are pending and user wants to act on it
|
||||
|
||||
## Protocol
|
||||
|
||||
### 1. Dispatch the analyst
|
||||
### 0. Parse flags
|
||||
|
||||
Check the slash-command argument string for the literal token `--explain`. Set `explain=true` when present; otherwise `explain=false`. Unknown flags: print one-line warning, continue with `explain=false`. This single flag is the only argument `/reflect` currently accepts.
|
||||
|
||||
### 1. Pre-filter the journal (window + exclusion) + score
|
||||
|
||||
Before dispatching the analyst, run the windowed-journal filter:
|
||||
|
||||
```bash
|
||||
node ~/.claude/adam/scripts/adam-window.mjs --home ~/.claude > /tmp/adam-windowed-journal.jsonl 2> /tmp/adam-windowed-journal.log
|
||||
```
|
||||
|
||||
The script reads the active journal plus all rotated journal files (new
|
||||
`journal/YYYY-Www.jsonl` weekly format AND legacy
|
||||
`journal/YYYY-MM-DD-<ts>.jsonl` size-rotated format are both supported), applies
|
||||
per-signal-type sliding windows (see `SIGNAL_WINDOWS_DAYS` in
|
||||
`adam-window.mjs`), and drops entries already actioned via
|
||||
`applied/*.md` / `rejected/*.md` frontmatter `source_entries`.
|
||||
|
||||
If `adam-window.mjs` exits non-zero: log the stderr file to the user, fall
|
||||
through to passing the raw `~/.claude/adam/journal.jsonl` path to the agent
|
||||
(graceful degradation — the agent's manual excluded-timestamps logic still
|
||||
filters actioned entries; only the freshness window is lost).
|
||||
|
||||
Then run the scoring pre-step on the same windowed journal:
|
||||
|
||||
```bash
|
||||
node ~/.claude/adam/scripts/adam-score.mjs --input /tmp/adam-windowed-journal.jsonl > /tmp/adam-scores.json 2> /tmp/adam-scores.log
|
||||
```
|
||||
|
||||
This produces a per-session `dampener` (0.5 / 0.75 / 1.0 based on
|
||||
`task_completed_count`) and a `reinforcement_candidates` list (skills cited by
|
||||
≥3 clean `task_completed` events). The analyst uses both — see
|
||||
`agents/adam.md` §"Scoring: task_completed dampener". If the score step fails,
|
||||
log stderr to the user and pass an empty `{"sessions":[],"reinforcement_candidates":[]}`
|
||||
to the analyst (dampener defaults to 1.0).
|
||||
|
||||
Finally, run the A/B measurement pre-step on any previously auto-applied
|
||||
proposals (see §3 ab-tracking write):
|
||||
|
||||
```bash
|
||||
node ~/.claude/adam/scripts/adam-ab-measure.mjs --home ~/.claude --format json > /tmp/adam-ab-regressions.json 2> /tmp/adam-ab-regressions.log
|
||||
```
|
||||
|
||||
The JSON output is an array of A/B delta objects (`pre_count`, `post_count`,
|
||||
`delta_pct`, `status` ∈ {`improved`,`neutral`,`regressed`,`no_baseline`,`pending`}).
|
||||
Filter to `status == "regressed"` before passing to the analyst as
|
||||
`ab_regressions`. The analyst is required (see `agents/adam.md` §"A/B
|
||||
effectiveness") to surface a `## Regressions` section at the top of its output
|
||||
when this list is non-empty. If the script fails: log stderr, pass `[]`.
|
||||
|
||||
### 2. Dispatch the analyst
|
||||
|
||||
Use the Agent tool with `subagent_type: "adam"` and prompt:
|
||||
|
||||
@@ -21,7 +73,10 @@ Use the Agent tool with `subagent_type: "adam"` and prompt:
|
||||
Run a single analysis pass.
|
||||
|
||||
Inputs:
|
||||
- journal_path: ~/.claude/adam/journal.jsonl
|
||||
- windowed_journal_path: /tmp/adam-windowed-journal.jsonl # pre-filtered by adam-window.mjs
|
||||
- scores_path: /tmp/adam-scores.json # per-session dampeners + reinforcement candidates
|
||||
- ab_regressions_path: /tmp/adam-ab-regressions.json # A/B deltas for prior auto-applied proposals
|
||||
- journal_path: ~/.claude/adam/journal.jsonl # raw — fallback only
|
||||
- state_path: ~/.claude/adam/state.json
|
||||
- usage_path: ~/.claude/adam/usage.json
|
||||
- proposals_dir: ~/.claude/adam/proposals/
|
||||
@@ -30,12 +85,34 @@ Inputs:
|
||||
- transcripts_root: ~/.claude/projects/
|
||||
- skills_root: ~/.claude/skills/
|
||||
|
||||
The windowed_journal is already filtered by per-signal age (see
|
||||
SIGNAL_WINDOWS_DAYS in adam-window.mjs) AND by actioned-exclusion. Read it as
|
||||
your primary input — do not re-apply window math. Fall back to journal_path
|
||||
only if windowed_journal_path is missing or empty.
|
||||
|
||||
Follow your system prompt exactly. Emit a single JSON punch list as your final message.
|
||||
```
|
||||
|
||||
Wait for return.
|
||||
|
||||
### 2. Auto-apply high-confidence items
|
||||
### 2b. Persist and render the clustering trace
|
||||
|
||||
The analyst's final message always contains a fenced ` ```trace ` block (per `agents/adam.md` §"Clustering trace (always emit)") immediately before its punch-list JSON line.
|
||||
|
||||
1. Extract the trace block. If it is missing, print a one-line warning to the user (`adam: trace block missing from agent output — proceeding without observability`) and continue; do not block on this.
|
||||
2. ALWAYS write the trace verbatim (without the surrounding fences) to `~/.claude/adam/last-trace.txt` (overwrite each run). This persists for retrospection via `node ~/.claude/adam/scripts/adam-explain.mjs`.
|
||||
3. Extract the `SUMMARY:` line from the trace. ALWAYS display it as a one-line status to the user BEFORE the proposals are listed, e.g. `clustering: <SUMMARY line>`. This single-line status is shown in both `--explain` and default modes.
|
||||
4. If `explain=true` (from §0): ALSO render the full trace block back to the user as a fenced code block (` ```text ` … ` ``` `) under a header `Clustering trace:`. If `explain=false`: SUPPRESS the cluster-line body from the user-visible output (the SUMMARY line is already shown in step 3).
|
||||
|
||||
The user can re-render any past trace at any time via:
|
||||
|
||||
```bash
|
||||
node ~/.claude/adam/scripts/adam-explain.mjs --mode summary # SUMMARY + per-decision counts
|
||||
node ~/.claude/adam/scripts/adam-explain.mjs --mode full # verbatim trace + rejection histogram
|
||||
node ~/.claude/adam/scripts/adam-explain.mjs --mode json # machine-readable
|
||||
```
|
||||
|
||||
### 3. Auto-apply high-confidence items
|
||||
|
||||
For each id in `high_confidence`:
|
||||
- Read the proposal file from `~/.claude/adam/proposals/<id>-*.md`.
|
||||
@@ -43,6 +120,14 @@ For each id in `high_confidence`:
|
||||
- Apply the change:
|
||||
- **For `skill_new`**: `mkdir -p ~/.claude/skills/<slug>/`, then `Write` the proposal's `# Proposed change` body to `~/.claude/skills/<slug>/SKILL.md`. After write, print: "skill `<slug>` written to `~/.claude/skills/<slug>/SKILL.md` — activates immediately — Claude Code v2.1.0+ auto-hot-reloads user-level skills, no restart needed."
|
||||
- **For `memory`**: `Write` the proposal's `# Proposed change` body (which MUST include the auto-memory frontmatter — see "Memory drafting protocol" in `agents/adam.md`) to the path in `target`. Then update `MEMORY.md` index with a one-line pointer.
|
||||
- **For `nudge`**: low-blast auto-apply path. Single-session evidence is sufficient — skip the cross-session gate. Append a new entry to `~/.claude/adam/active-nudges.json` (create the file with `[]` if absent) with shape `{kind, message, created_at: <now_ms>, expires_at_ts: <now_ms + 7*86400000>, max_displays: 3, displays_used: 0, source_session: <session_id from proposal>}`. Do NOT modify any skill, memory, agent, or CLAUDE.md. Tell user: "nudge queued — surfaces on next SessionStart in a different session (expires in 7 days)."
|
||||
- **For `reinforcement`**: gated by `confidence >= 4 AND blast_radius == low` (same as memory). Apply by invoking the helper:
|
||||
|
||||
```bash
|
||||
node ~/.claude/adam/scripts/adam-apply-reinforcement.mjs ~/.claude/adam/proposals/<id>-*.md --home ~/.claude
|
||||
```
|
||||
|
||||
The helper reads the proposal frontmatter (`skill_slug`, `count`, `source_session`) and appends one JSON line to `~/.claude/adam/reinforcements.jsonl`. No code/memory/skill modifications. Output: `{"status":"applied"|"gated", ...}` — on `gated` leave proposal in `proposals/` (helper failed its own re-check), on `applied` continue to the archive step. Tell user: "reinforcement logged for `<skill_slug>` (count=<N>) — appended to reinforcements.jsonl."
|
||||
- **For `skill_edit`**: enforce the apply-time gate before writing.
|
||||
1. Verify proposal frontmatter has `auto_apply_eligible: true`. If not, abort and queue for review.
|
||||
2. Read `target` SKILL.md, capture `current_bytes` from a fresh stat — do NOT trust frontmatter `bytes_before`.
|
||||
@@ -51,18 +136,33 @@ For each id in `high_confidence`:
|
||||
- Zero `-` lines on existing SKILL.md content (additions only).
|
||||
- Total `+` lines ≤ 30.
|
||||
If any check fails, print one-line refusal reason, leave proposal in `proposals/`, continue.
|
||||
4. Cooldown re-check: scan `applied/` frontmatter for `target` matching this and `last_auto_edit` newer than 7 days ago. Refuse if found.
|
||||
5. Blacklist re-check: scan `rejected/` frontmatter for `target` matching this and `auto_apply_blacklist: true` newer than 30 days ago. Refuse if found.
|
||||
4. Cooldown re-check: run `node ~/.claude/adam/scripts/adam-cooldown.mjs --skill <target_skill> --fingerprint <proposal_fingerprint>` (both fields come from proposal frontmatter; missing fingerprint → "legacy"). Refuse if the script returns `status: cooldown` OR `status: blacklisted`. This per-(skill, fingerprint) gate replaces the previous coarse per-skill scan — proposals for the same skill with a different fingerprint are NOT blocked by an older entry.
|
||||
5. (covered by step 4 — blacklisted status is returned by `adam-cooldown.mjs` when `auto_apply_blacklist: true` is found in `rejected/` within 30 days for the same (skill, fingerprint))
|
||||
6. Apply via `Edit` tool (append the new section per the diff). Never use `Write` on existing SKILL.md.
|
||||
7. Re-stat target. If new size exceeds `2 * current_bytes` (captured in step 2), revert via `Edit` (remove the just-appended section) and refuse — print refusal reason.
|
||||
8. Add `last_auto_edit: <iso8601 utc now>` to the proposal frontmatter before moving it.
|
||||
9. Tell user: "skill `<slug>` extended (added <N> lines) — auto-applied via win-evidence gate."
|
||||
- Move proposal to `~/.claude/adam/applied/<UTC-ts>-<id>.md`.
|
||||
- **A/B tracking append**: as a separate atomic step right after the move, append one JSON line to `~/.claude/adam/ab-tracking.jsonl` (create with empty contents if absent). Read fields from the proposal's frontmatter (`proposal_fingerprint`, `originating_signals` — both populated per `agents/adam.md`; `originating_signals` is a list of `{type, count, session_ids}` objects). Schema:
|
||||
|
||||
```json
|
||||
{
|
||||
"applied_at": <unix_ms now>,
|
||||
"proposal_id": "<id>",
|
||||
"proposal_type": "skill_edit|skill_new|memory|nudge|reinforcement",
|
||||
"target_skill": "<slug or target basename>",
|
||||
"proposal_fingerprint": "<hash>",
|
||||
"originating_signals": [{"type":"<signal>","count":<N>,"session_ids":[...]}],
|
||||
"pre_window_days": 7
|
||||
}
|
||||
```
|
||||
|
||||
This entry is consumed by `adam-ab-measure.mjs` on subsequent `/reflect` runs to compute pre/post signal-count deltas. See `agents/adam.md` §"A/B effectiveness". If the append fails (disk-full etc.) log a warning but do NOT abort the apply path — A/B is observability, not a gate.
|
||||
- **Archive consumed journal entries**: `node ~/.claude/adam/scripts/adam-archive.mjs ~/.claude/adam/applied/<UTC-ts>-<id>.md` — moves entries listed in proposal's `source_entries` from `journal.jsonl` to `journal/actioned-<id>.jsonl` so subsequent `/reflect` runs do not re-cluster them.
|
||||
|
||||
Print: `auto-applied N proposals: [ids]`.
|
||||
|
||||
### 3. Walk the queue
|
||||
### 4. Walk the queue
|
||||
|
||||
For each id in `queued`:
|
||||
|
||||
@@ -78,13 +178,13 @@ c. On **approve**:
|
||||
- Move proposal to `~/.claude/adam/applied/<ts>-<id>.md`.
|
||||
- Archive: `node ~/.claude/adam/scripts/adam-archive.mjs ~/.claude/adam/applied/<ts>-<id>.md`.
|
||||
d. On **reject**: ask for reason in one line. Append `# Reason\n<reason>` to proposal body. If the proposal `type` is `skill_edit`, ALSO add `auto_apply_blacklist: true` to its frontmatter (so future reflects skip auto-apply on this target for 30 days). Move to `~/.claude/adam/rejected/<id>.md`. Archive: `node ~/.claude/adam/scripts/adam-archive.mjs ~/.claude/adam/rejected/<id>.md`.
|
||||
e. On **edit**: ask the user for the change, edit the proposal in place, then loop back to step 3a for that same id.
|
||||
e. On **edit**: ask the user for the change, edit the proposal in place, then loop back to step 4a for that same id.
|
||||
|
||||
### 4. Handle failures
|
||||
### 5. Handle failures
|
||||
|
||||
If apply fails (file write error, target missing): leave proposal in `proposals/`, append `# Apply error\n<error>` to its body. Tell the user. Do not move it.
|
||||
|
||||
### 5. Summary
|
||||
### 6. Summary
|
||||
|
||||
End with one block:
|
||||
|
||||
@@ -108,7 +208,7 @@ Before writing any proposal:
|
||||
- For `claude_md_edit`: confirm 3+ distinct cwds in the `# Why` section.
|
||||
- For `deletion`: confirm both criteria (a) and (b) from the agent's special handling are documented in the proposal.
|
||||
- For `skill_new`: confirm the slug doesn't collide with any existing skill in `~/.claude/skills/`. If it does, refuse and ask user to rename.
|
||||
- For `skill_edit`: confirm the diff is append-only (no `-` lines that remove existing content) and that target SKILL.md exists. When auto-applying, ALSO re-verify the eligibility gate steps in §2 (cooldown, blacklist, byte cap) before any `Edit` call — never trust frontmatter alone.
|
||||
- For `skill_edit`: confirm the diff is append-only (no `-` lines that remove existing content) and that target SKILL.md exists. When auto-applying, ALSO re-verify the eligibility gate steps in §3 (cooldown, blacklist, byte cap) before any `Edit` call — never trust frontmatter alone.
|
||||
- For `skill_edit` with `auto_apply_eligible: true`: confirm `contradiction_flag` is absent or null in frontmatter. Refuse auto-apply if `contradiction_flag` is set with any non-empty value (treat the agent's flag as a hard veto on auto-apply; user can still manually approve in walk-the-queue if they disagree with the heuristic).
|
||||
- For `memory`: confirm `# Proposed change` body starts with `---` frontmatter containing required fields `name`, `description`, `type`, `originSessionId`. Refuse if frontmatter missing — agent must redraft per the Memory drafting protocol.
|
||||
- Confirm `source_entries` is present in proposal frontmatter as a non-empty list (used for archive). Warn (do not refuse) if missing — legacy proposals from before v0.2.0 won't have it.
|
||||
|
||||
Reference in New Issue
Block a user