mirror of
https://github.com/lukaszraczylo/claude-adam.git
synced 2026-06-27 02:33:54 +00:00
7ddda26bb4
Adds an 11th signal type emitted when a run of work (between two
UserPromptSubmit events) crosses three quality gates:
- >=5 tool calls (TASK_TOOL_MIN)
- >=3 distinct tool kinds (TASK_DIVERSITY_MIN, filters single-tool
sweeps like "wrote 5 files")
- 0 correction signals during the run (filters tasks where the user
pushed back; correction-during-task disqualifies the recipe)
Payload carries tool_count, tool_kinds, active_skills, active_agents
so the agent can cluster by sorted tool-kind tuple and route through
the existing skill-overlap rule (skill_new vs skill_edit).
Importantly: cross_session_evidence is FALSE on first occurrence,
so resulting skill_new proposals always queue for review — they only
auto-apply when the same multi-tool recipe recurs in a second session
(then the existing rubric kicks in). Post-task creation captures novel
patterns while preserving the rule "auto-apply requires cross-session".
Hook adds state fields: task_tool_count, task_tool_kinds, task_corrections.
All reset on UserPromptSubmit boundary and on session change.
Agent gets one new signal-types-table row and one clustering bullet
referencing the existing skill-overlap rule.
3 new tests (30 passed, 0 failed):
- 5 tools + 5 kinds + 0 corrections fires task_completed
- 5 tools + 1 kind (Edit only) does NOT fire (diversity gate)
- 5 tools + 3 kinds + correction-on-closing-prompt does NOT fire
353 lines
14 KiB
JavaScript
Executable File
353 lines
14 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
import { readFileSync, writeFileSync, appendFileSync, existsSync, statSync, renameSync, mkdirSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { homedir } from "node:os";
|
|
|
|
function djb2(str) {
|
|
let h = 5381;
|
|
for (let i = 0; i < str.length; i++) h = ((h << 5) + h) ^ str.charCodeAt(i);
|
|
return (h >>> 0).toString(36);
|
|
}
|
|
|
|
const ROOT = join(homedir(), ".claude", "adam");
|
|
const JOURNAL = join(ROOT, "journal.jsonl");
|
|
const STATE = join(ROOT, "state.json");
|
|
const USAGE = join(ROOT, "usage.json");
|
|
const JOURNAL_DIR = join(ROOT, "journal");
|
|
|
|
const CORRECTION_RE = /\b(no|stop|don't|don\'t|wrong|actually|nope|undo|revert)\b/i;
|
|
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"]);
|
|
const WINDOW_SIZE = 10;
|
|
const RETRY_THRESHOLD = 3;
|
|
const AGENT_RESPAWN_THRESHOLD = 2;
|
|
const ERROR_RING_SIZE = 5;
|
|
const ERROR_LOOP_THRESHOLD = 3;
|
|
const DEAD_END_THRESHOLD = 8;
|
|
const EDIT_CHURN_THRESHOLD = 4;
|
|
const BUILD_LOOP_THRESHOLD = 2;
|
|
const SUBAGENT_DISPATCH_THRESHOLD = 3;
|
|
const CORRECTION_FREE_THRESHOLD = 5;
|
|
const CLEAN_RECOVERY_WINDOW = 3;
|
|
const STRUGGLE_TYPES = new Set(["tool_error_loop", "dead_end", "retry_loop"]);
|
|
const ACTIVE_SKILLS_LOOKBACK = 10;
|
|
const TASK_TOOL_MIN = 5;
|
|
const TASK_DIVERSITY_MIN = 3;
|
|
const STATE_MAX_BYTES = 1_000_000;
|
|
|
|
function safeRead(path, fallback) {
|
|
try { return JSON.parse(readFileSync(path, "utf8")); } catch { return fallback; }
|
|
}
|
|
|
|
function safeWrite(path, obj) {
|
|
try { writeFileSync(path, JSON.stringify(obj)); } catch {}
|
|
}
|
|
|
|
function rotateIfLarge(path, max) {
|
|
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);
|
|
}
|
|
} catch {}
|
|
}
|
|
|
|
function readStdin() {
|
|
if (process.stdin.isTTY) return null;
|
|
let buf = "";
|
|
try {
|
|
buf = readFileSync(0, "utf8");
|
|
} catch {}
|
|
try { return JSON.parse(buf); } catch { return null; }
|
|
}
|
|
|
|
function appendJournal(entry) {
|
|
rotateIfLarge(JOURNAL, STATE_MAX_BYTES * 5);
|
|
try {
|
|
appendFileSync(JOURNAL, JSON.stringify(entry) + "\n");
|
|
} catch {}
|
|
}
|
|
|
|
function bumpUsage(name) {
|
|
const usage = safeRead(USAGE, {});
|
|
usage[name] = (usage[name] || 0) + 1;
|
|
safeWrite(USAGE, usage);
|
|
return usage[name];
|
|
}
|
|
|
|
function readUsage(name) {
|
|
const usage = safeRead(USAGE, {});
|
|
return usage[name] || 0;
|
|
}
|
|
|
|
function pushActivity(state, kind, name, ts) {
|
|
state.activity_ring.push({ kind, name, ts });
|
|
if (state.activity_ring.length > ACTIVE_SKILLS_LOOKBACK) state.activity_ring.shift();
|
|
}
|
|
|
|
function activeNames(state, kind) {
|
|
const seen = new Set();
|
|
for (const e of state.activity_ring) if (e.kind === kind) seen.add(e.name);
|
|
return [...seen];
|
|
}
|
|
|
|
function errorFingerprint(toolResponse) {
|
|
if (!toolResponse) return null;
|
|
let text = "";
|
|
if (typeof toolResponse === "string") text = toolResponse;
|
|
else if (toolResponse.content !== undefined) {
|
|
text = typeof toolResponse.content === "string"
|
|
? toolResponse.content
|
|
: JSON.stringify(toolResponse.content);
|
|
} else {
|
|
try { text = JSON.stringify(toolResponse); } catch { return null; }
|
|
}
|
|
if (!text) return null;
|
|
text = text.slice(0, 4000);
|
|
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);
|
|
}
|
|
|
|
function resetFrictionCounters(state) {
|
|
state.tools_since_user = 0;
|
|
state.dead_end_emitted = false;
|
|
state.last_errors = [];
|
|
state.edit_counts = {};
|
|
state.edit_churn_emitted = {};
|
|
state.build_failure_count = 0;
|
|
state.build_loop_emitted = false;
|
|
}
|
|
|
|
function resetSessionLocal(state) {
|
|
resetFrictionCounters(state);
|
|
state.session_subagents = {};
|
|
state.subagent_dispatch_emitted = {};
|
|
state.correctionFreeCounter = 0;
|
|
state.recoveryWatch = null;
|
|
state.tool_window = [];
|
|
state.task_tool_kinds = {};
|
|
state.task_tool_count = 0;
|
|
state.task_corrections = 0;
|
|
}
|
|
|
|
function ensureStateDefaults(state) {
|
|
if (!Array.isArray(state.tool_window)) state.tool_window = [];
|
|
if (typeof state.tools_since_user !== "number") state.tools_since_user = 0;
|
|
if (typeof state.dead_end_emitted !== "boolean") state.dead_end_emitted = false;
|
|
if (!Array.isArray(state.last_errors)) state.last_errors = [];
|
|
if (!state.edit_counts || typeof state.edit_counts !== "object") state.edit_counts = {};
|
|
if (!state.edit_churn_emitted || typeof state.edit_churn_emitted !== "object") state.edit_churn_emitted = {};
|
|
if (typeof state.build_failure_count !== "number") state.build_failure_count = 0;
|
|
if (typeof state.build_loop_emitted !== "boolean") state.build_loop_emitted = false;
|
|
if (!state.session_subagents || typeof state.session_subagents !== "object") state.session_subagents = {};
|
|
if (!state.subagent_dispatch_emitted || typeof state.subagent_dispatch_emitted !== "object") state.subagent_dispatch_emitted = {};
|
|
if (typeof state.correctionFreeCounter !== "number") state.correctionFreeCounter = 0;
|
|
if (state.recoveryWatch === undefined) state.recoveryWatch = null;
|
|
if (!Array.isArray(state.activity_ring)) state.activity_ring = [];
|
|
if (!state.task_tool_kinds || typeof state.task_tool_kinds !== "object") state.task_tool_kinds = {};
|
|
if (typeof state.task_tool_count !== "number") state.task_tool_count = 0;
|
|
if (typeof state.task_corrections !== "number") state.task_corrections = 0;
|
|
}
|
|
|
|
function main() {
|
|
const input = readStdin();
|
|
if (!input || typeof input !== "object") return;
|
|
|
|
const event = input.hook_event_name;
|
|
const session = input.session_id || "unknown";
|
|
const cwd = input.cwd || process.cwd();
|
|
const ts = new Date().toISOString();
|
|
const state = safeRead(STATE, { cursor: 0, tool_window: [] });
|
|
ensureStateDefaults(state);
|
|
|
|
if (state.session_id && state.session_id !== session) {
|
|
resetSessionLocal(state);
|
|
}
|
|
state.session_id = session;
|
|
|
|
if (event === "UserPromptSubmit") {
|
|
const prompt = (input.prompt || "").slice(0, 200);
|
|
if (CORRECTION_RE.test(prompt)) {
|
|
const last = state.tool_window[state.tool_window.length - 1] || {};
|
|
appendJournal({
|
|
ts, session, cwd, type: "correction",
|
|
phrase: prompt.slice(0, 80),
|
|
prev_tool: last.tool || null,
|
|
prev_file: last.file || null,
|
|
});
|
|
state.correctionFreeCounter = 0;
|
|
state.task_corrections += 1;
|
|
} else {
|
|
state.correctionFreeCounter += 1;
|
|
if (state.correctionFreeCounter >= CORRECTION_FREE_THRESHOLD) {
|
|
appendJournal({
|
|
ts, session, cwd, type: "correction_free_streak",
|
|
streak: state.correctionFreeCounter,
|
|
active_skills: activeNames(state, "skill"),
|
|
active_agents: activeNames(state, "agent"),
|
|
});
|
|
state.correctionFreeCounter = 0;
|
|
}
|
|
}
|
|
// Evaluate prior task (work between previous UserPromptSubmit and this one).
|
|
const taskKinds = Object.keys(state.task_tool_kinds);
|
|
if (state.task_tool_count >= TASK_TOOL_MIN &&
|
|
taskKinds.length >= TASK_DIVERSITY_MIN &&
|
|
state.task_corrections === 0) {
|
|
appendJournal({
|
|
ts, session, cwd, type: "task_completed",
|
|
tool_count: state.task_tool_count,
|
|
tool_kinds: taskKinds,
|
|
active_skills: activeNames(state, "skill"),
|
|
active_agents: activeNames(state, "agent"),
|
|
});
|
|
}
|
|
state.task_tool_kinds = {};
|
|
state.task_tool_count = 0;
|
|
state.task_corrections = 0;
|
|
resetFrictionCounters(state);
|
|
} else if (event === "PreToolUse") {
|
|
const tool = input.tool_name;
|
|
if (tool === "Skill") {
|
|
const name = (input.tool_input && (input.tool_input.skill || input.tool_input.skill_name)) || "unknown";
|
|
bumpUsage(`skill:${name}`);
|
|
pushActivity(state, "skill", name, ts);
|
|
} else if (tool === "Agent") {
|
|
const name = (input.tool_input && (input.tool_input.subagent_type || input.tool_input.agent)) || "unknown";
|
|
bumpUsage(`agent:${name}`);
|
|
pushActivity(state, "agent", name, ts);
|
|
state.session_subagents[name] = (state.session_subagents[name] || 0) + 1;
|
|
const cumulative = readUsage(`agent:${name}`);
|
|
const sessionCount = state.session_subagents[name];
|
|
const total = Math.max(cumulative, sessionCount);
|
|
if (total >= SUBAGENT_DISPATCH_THRESHOLD && !state.subagent_dispatch_emitted[name]) {
|
|
appendJournal({
|
|
ts, session, cwd, type: "subagent_dispatch_pattern",
|
|
subagent_type: name, session_count: sessionCount, cumulative
|
|
});
|
|
state.subagent_dispatch_emitted[name] = true;
|
|
}
|
|
}
|
|
} else if (event === "PostToolUse") {
|
|
const tool = input.tool_name || "unknown";
|
|
const argsHash = djb2(JSON.stringify(input.tool_input || {}));
|
|
const file = (input.tool_input && (input.tool_input.file_path || input.tool_input.path)) || null;
|
|
|
|
let struggleEmittedThisTurn = null;
|
|
const emit = (entry) => {
|
|
if (STRUGGLE_TYPES.has(entry.type)) struggleEmittedThisTurn = entry.type;
|
|
appendJournal(entry);
|
|
};
|
|
|
|
const windowEntry = { tool, argsHash, file };
|
|
if (tool === "Agent") {
|
|
const sub = (input.tool_input && (input.tool_input.subagent_type || input.tool_input.agent)) || "unknown";
|
|
windowEntry.subagent = sub;
|
|
}
|
|
state.tool_window.push(windowEntry);
|
|
if (state.tool_window.length > WINDOW_SIZE) state.tool_window.shift();
|
|
|
|
const sameToolArgs = state.tool_window.filter(e => e.tool === tool && e.argsHash === argsHash).length;
|
|
if (sameToolArgs >= RETRY_THRESHOLD) {
|
|
emit({ ts, session, cwd, type: "retry_loop", tool, count: sameToolArgs });
|
|
}
|
|
|
|
if (tool === "Agent") {
|
|
const subagent = (input.tool_input && (input.tool_input.subagent_type || input.tool_input.agent)) || "unknown";
|
|
const recent = state.tool_window.slice(-5).filter(e => e.tool === "Agent" && e.subagent === subagent).length;
|
|
if (recent >= AGENT_RESPAWN_THRESHOLD) {
|
|
emit({ ts, session, cwd, type: "weak_agent", subagent_type: subagent, count: recent });
|
|
}
|
|
}
|
|
|
|
if (input.tool_response && typeof input.tool_response === "object") {
|
|
bumpUsage("payload:tool_response_seen");
|
|
}
|
|
|
|
const fp = errorFingerprint(input.tool_response);
|
|
if (fp) {
|
|
bumpUsage("payload:tool_response_error_seen");
|
|
state.last_errors.push({ tool, fp });
|
|
if (state.last_errors.length > ERROR_RING_SIZE) state.last_errors.shift();
|
|
const sameError = state.last_errors.filter(e => e.fp === fp).length;
|
|
if (sameError >= ERROR_LOOP_THRESHOLD) {
|
|
emit({ ts, session, cwd, type: "tool_error_loop", tool, count: sameError, fp });
|
|
}
|
|
}
|
|
|
|
if (file && EDIT_TOOLS.has(tool)) {
|
|
state.edit_counts[file] = (state.edit_counts[file] || 0) + 1;
|
|
if (state.edit_counts[file] >= EDIT_CHURN_THRESHOLD && !state.edit_churn_emitted[file]) {
|
|
emit({ ts, session, cwd, type: "edit_churn", file, count: state.edit_counts[file] });
|
|
state.edit_churn_emitted[file] = true;
|
|
}
|
|
const keys = Object.keys(state.edit_counts);
|
|
if (keys.length > 20) {
|
|
const oldest = keys[0];
|
|
delete state.edit_counts[oldest];
|
|
delete state.edit_churn_emitted[oldest];
|
|
}
|
|
}
|
|
|
|
if (tool === "Bash") {
|
|
const cmd = (input.tool_input && input.tool_input.command) || "";
|
|
const isBuildCmd = BUILD_RE.test(cmd);
|
|
const hasError = (input.tool_response && input.tool_response.is_error === true) || fp !== null;
|
|
if (isBuildCmd && hasError) {
|
|
state.build_failure_count += 1;
|
|
if (state.build_failure_count >= BUILD_LOOP_THRESHOLD && !state.build_loop_emitted) {
|
|
emit({ ts, session, cwd, type: "build_loop", count: state.build_failure_count, command: cmd.slice(0, 80) });
|
|
state.build_loop_emitted = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
state.tools_since_user += 1;
|
|
if (state.tools_since_user >= DEAD_END_THRESHOLD && !state.dead_end_emitted) {
|
|
emit({ ts, session, cwd, type: "dead_end", count: state.tools_since_user });
|
|
state.dead_end_emitted = true;
|
|
}
|
|
|
|
state.task_tool_count += 1;
|
|
state.task_tool_kinds[tool] = (state.task_tool_kinds[tool] || 0) + 1;
|
|
|
|
if (struggleEmittedThisTurn) {
|
|
state.recoveryWatch = { recovered_from: struggleEmittedThisTurn, since_ts: ts, clean_count: 0, window_tools: [] };
|
|
} else if (state.recoveryWatch) {
|
|
const turnHadError = fp !== null;
|
|
if (turnHadError) {
|
|
state.recoveryWatch = null;
|
|
} else {
|
|
state.recoveryWatch.clean_count += 1;
|
|
state.recoveryWatch.window_tools.push(tool);
|
|
if (state.recoveryWatch.window_tools.length > CLEAN_RECOVERY_WINDOW) state.recoveryWatch.window_tools.shift();
|
|
if (state.recoveryWatch.clean_count >= CLEAN_RECOVERY_WINDOW) {
|
|
appendJournal({
|
|
ts, session, cwd, type: "clean_recovery",
|
|
recovered_from: state.recoveryWatch.recovered_from,
|
|
recovery_window_tools: state.recoveryWatch.window_tools.slice(),
|
|
active_skills: activeNames(state, "skill"),
|
|
active_agents: activeNames(state, "agent"),
|
|
});
|
|
state.recoveryWatch = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
safeWrite(STATE, state);
|
|
}
|
|
|
|
try { main(); } catch {}
|
|
process.exit(0);
|