8 Commits

Author SHA1 Message Date
lukaszraczylo a48c705c0a feat(adam): smarter signals & clustering
- New signal types in hooks/adam-observe.mjs:
  - silent_drift: 5 consecutive read-only PostToolUse without an action tool
  - error_after_recovery: same error fingerprint returns within 5 events of clean_recovery
- Severity-weighted scoring in adam/scripts/adam-score.mjs:
  - SEVERITY_DIVISORS exported per struggle signal type
  - Per-session severity_sum + severity_by_type added to JSON output
- Skill-attribution clustering in agents/adam.md:
  - Sub-cluster struggle signals on active_skills[0]
  - New struggle-driven skill_edit variant (always queues, never auto-applies)
- Rubric updates:
  - +1 for cluster severity-sum >= 10, additional +1 for >= 32
  - +1 for skill-attributed sub-cluster naming an existing skill
  - silent_drift + error_after_recovery added to struggle signal list
- Window: silent_drift 14d, error_after_recovery 30d
- Tests: 94 passing (78-82 new)

Backward compat: entries without count default to severity 1. Existing
win-driven skill_edit gate untouched. No journal migration.
2026-05-13 19:21:59 +01:00
lukaszraczylo a8883aa8b7 fix(logo): explicit light/dark variants + <picture> for GitHub
The prior logo.svg used currentColor, which resolves to black when the
SVG is loaded via <img> on GitHub — making the logo invisible in dark
mode (the GitHub default for many users).

Fix uses GitHub's supported <picture> + prefers-color-scheme media-
source pattern in README:

- assets/logo-light.svg — explicit GitHub light-theme text color #24292f
- assets/logo-dark.svg  — explicit GitHub dark-theme text color #f0f6fc
- assets/logo.svg       — kept with embedded @media + currentColor for
                          standalone use (markmorph notes, anywhere
                          else the SVG is loaded outside <picture>)

README updates the <img> tag to a <picture> with media-conditioned
source so GitHub's renderer picks the right variant per theme.
2026-05-13 02:07:11 +01:00
lukaszraczylo 7ed2aecdfa docs(logo): swap to swaddled-baby design with hands
Replaces the geometric-A-with-observation-dot with a softer, more
on-theme design: a swaddled-baby silhouette (rounded A-shape bundle),
face nestled inside, and the wrap-band extended past the bundle on
both sides as little hands. Maintains currentColor + zero external
assets; reads cleanly down to favicon size.

Ties the visual identity to the 'Story behind Adam' section: the
project is named after the author's son, and now the logo is too.
2026-05-13 02:02:02 +01:00
lukaszraczylo a30f8b1158 docs: replace ASCII pipeline diagram with mermaid flowchart
GitHub renders mermaid natively. Diagram now shows three subgraphs
(Observation → Analysis → Review + apply) with a nested Pre-processors
subgraph inside Analysis. Includes:

- Dotted edge labeled 'user runs /reflect' marking the observe→analyze
  boundary.
- Diamond gate node for auto-apply decision (conf≥4 · low blast ·
  cooldown cool) with explicit yes/no branches.
- Feedback loop: applied/ entries measure back into adam-ab-measure.mjs
  on subsequent reflects.
- Color-coded classDef for stores (blue), processes (orange), and the
  clustering trace artifact (purple).

ASCII art retired — diagram now legible at any zoom on github.com.
2026-05-13 01:54:38 +01:00
lukaszraczylo d3e4350d71 docs: modernize README + add SVG logo + inspiration story
- New 'Story behind Adam' section at the top: the project is named after
  the author's newborn son, whose observe-act-adjust-observe-again
  learning loop is the methodology ADAM applies to LLM sessions.
- New SVG logo at assets/logo.svg: stylized 'A' with a captured
  observation point inside the apex and a feedback crossbar. Uses
  currentColor + gradient so it adapts to light/dark GitHub themes.
- Centered header block with project tagline + 5 badges (License,
  Version, Tests, Node, Platform).
- New 'Highlights' section: 8 emoji-tagged one-liners covering the
  v0.3.3 design pillars (zero LLM cost observation, A/B measurement,
  sliding windows, observability, etc.).
- New 'How it works' ASCII pipeline diagram: observation -> analysis
  pre-processors -> analyst -> review + apply.
- Signals table now includes per-signal sliding window column.
- Rubric section restructured: gates, modifiers (dampener), and
  skill_edit-specific requirements clearly separated.
- New 'Inspecting the analyst's reasoning' section documenting
  adam-explain.mjs + /reflect --explain.
- Layout updated for v0.3.3 state files (active-nudges.json,
  ab-tracking.jsonl, reinforcements.jsonl, last-trace.txt) and all
  9 new helper scripts under adam/scripts/.
- Test count: 27 -> 87.
- Closing line crediting Adam.
2026-05-13 01:50:59 +01:00
lukaszraczylo 871592a75b Merge branch 'adam-v0.3.3-fixes' (v0.3.3) 2026-05-13 01:02:40 +01:00
lukaszraczylo 012c40b9ab 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).
2026-05-13 01:02:33 +01:00
lukaszraczylo 7ddda26bb4 feat: task_completed signal — post-task skill capture (v0.3.2)
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
2026-05-10 22:34:33 +01:00
21 changed files with 3660 additions and 211 deletions
+256 -126
View File
@@ -1,157 +1,273 @@
<div align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="./assets/logo-dark.svg">
<img src="./assets/logo-light.svg" alt="claude-adam logo" width="128" height="128" />
</picture>
# claude-adam
Self-improvement layer for [Claude Code](https://claude.com/claude-code) that observes friction signals during your sessions and proposes targeted improvements (new skills, memory entries, agent edits) which you can review and apply.
**A self-improvement layer for [Claude Code](https://claude.com/claude-code).**
## What's new
Watches the friction in your coding sessions, clusters the signals via an LLM analyst, and proposes targeted improvements — new skills, memory entries, agent edits — that you review and apply.
- **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).
- **v0.2.0** — actioned-entry archival via `adam-archive.mjs`; `cursor` field deprecated.
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
[![Version](https://img.shields.io/github/v/release/lukaszraczylo/claude-adam?label=version&color=blue)](https://github.com/lukaszraczylo/claude-adam/releases)
[![Tests](https://img.shields.io/badge/tests-87%20passing-brightgreen.svg)](./adam/tests/run-tests.sh)
[![Node](https://img.shields.io/badge/node-22%2B-339933.svg)](https://nodejs.org)
[![Platform](https://img.shields.io/badge/platform-macOS%20%7C%20Linux-lightgrey.svg)]()
## What it does
</div>
A lightweight Node.js hook (`adam-observe.mjs`) runs on `UserPromptSubmit`, `PreToolUse`, and `PostToolUse` events. It detects:
---
| Signal | Trigger |
|---|---|
| `correction` | User prompt contains "no", "stop", "wrong", "actually", etc. after a tool call |
| `retry_loop` | Same tool + same args called 3× in a 10-event window |
| `weak_agent` | Same subagent dispatched 2× in last 5 tool calls |
| `tool_error_loop` | Same error fingerprint appears 3× in a 5-event ring |
| `dead_end` | 8 PostToolUse events without a UserPromptSubmit between them |
| `edit_churn` | Same file edited 4× in a window |
| `build_loop` | 2× build/test/compile commands fail in same session |
| `subagent_dispatch_pattern` | Same subagent dispatched ≥3× cumulatively |
| `correction_free_streak` | 5 clean UserPromptSubmits in a row (no correction phrase) — feeds `skill_edit` reinforcement |
| `clean_recovery` | 3 clean PostToolUse events after a struggle signal — feeds `skill_edit` reinforcement |
## The story behind Adam
Adam is my newborn son.
Watching him over the last few months — the way he observes the world, tries something, watches what happens, adjusts, and tries again — I realised that the most powerful learning loop in nature is also one of the simplest. No grand theory. No instruction manual. Just relentless feedback and pattern recognition, applied to every waking moment.
LLMs can learn the same way. Give them a hook into the real friction of your work — the corrections, the dead-ends, the moments you say *"no, try again"* — and let them propose improvements grounded in **what actually happened**. Not what they assume might help. What you actually struggled with.
**claude-adam** is that loop, wired into Claude Code. It's named after Adam because the methodology is his.
---
## Highlights
- 🔍 **Zero LLM cost at observation time.** Deterministic regex + counter detection in a Node hook. The analyst only runs when you invoke `/reflect`.
- 📡 **11 signal types.** Friction (`correction`, `tool_error_loop`, `dead_end`, `edit_churn`, …) + reinforcement (`task_completed`, `correction_free_streak`, `clean_recovery`) + meta.
- 🛡️ **Tight auto-apply gates.** Confidence ≥ 4, cross-session evidence, contradiction veto, per-(skill, fingerprint) cooldown. Most things queue for your manual review.
- 📊 **A/B effectiveness measurement.** Every auto-applied edit gets a 7-day pre/post signal-count delta. If a proposed fix made things worse, the next `/reflect` says so.
-**Per-signal sliding windows.** Stale friction doesn't accumulate forever. `dead_end` 7d, `correction` 30d, reinforcement signals 60d.
- 🔬 **Observable.** Every clustering decision (passed / threshold-blocked / window-filtered / contradiction-vetoed) emits a trace. `/reflect --explain` shows it.
- 📦 **Pure Node.** Zero npm dependencies. Runs on macOS and Linux (Alpine smoke-tested).
## Quick start
```sh
curl -fsSL https://raw.githubusercontent.com/lukaszraczylo/claude-adam/main/install.sh | bash
```
The installer copies files into `~/.claude/`, offers to merge ADAM's hook entries into `~/.claude/settings.json` (with a diff preview and `[y/N]` confirm), and preserves any local edits via `.adam-new` sidecar files. Pass `--yes` to skip prompts, `--dry-run` to preview.
Then:
```sh
bash ~/.claude/adam/tests/run-tests.sh # expect: 87 passed, 0 failed
# … start a fresh Claude Code session …
/reflect # walks the proposal queue
/reflect --explain # also shows the analyst's clustering trace
```
Pin a release for reproducibility:
```sh
curl -fsSL https://raw.githubusercontent.com/lukaszraczylo/claude-adam/v0.3.3/install.sh \
| VERSION=v0.3.3 bash
```
## How it works
```mermaid
flowchart TB
subgraph OBS["Observation (deterministic, in-hook, zero LLM cost)"]
direction LR
EV["Tool event /<br/>user prompt"] --> OBSERVE["adam-observe.mjs<br/><sub>regex · counters · ring buffers</sub>"]
OBSERVE --> JOURNAL[("journal.jsonl<br/><sub>append-only signal log</sub>")]
end
JOURNAL -. user runs <code>/reflect</code> .-> ANALYSIS
subgraph ANALYSIS["Analysis (LLM, only on demand)"]
direction TB
subgraph PRE["Pre-processors (deterministic)"]
direction LR
W["adam-window.mjs<br/><sub>per-signal sliding window</sub>"]
S["adam-score.mjs<br/><sub>task_completed dampener<br/>+ reinforcement candidates</sub>"]
AB["adam-ab-measure.mjs<br/><sub>7d pre/post deltas<br/>on prior auto-applies</sub>"]
end
AGENT["adam subagent<br/><sub>cluster · score · diagnose</sub>"]
PRE --> AGENT
AGENT --> PROPOSALS[("proposals/")]
AGENT --> TRACE[["clustering trace<br/><sub>adam-explain.mjs renders</sub>"]]
end
PROPOSALS --> REVIEW
subgraph REVIEW["Review + apply"]
direction TB
GATE{"auto-apply<br/>gates pass?<br/><sub>conf≥4 · low blast<br/>· cooldown cool</sub>"}
GATE -->|yes| APPLIED[("applied/<br/>+ ab-tracking.jsonl")]
GATE -->|no| QUEUE["walk-the-queue<br/><sub>approve · reject · edit</sub>"]
QUEUE -->|approve| APPLIED
QUEUE -->|reject| REJECTED[("rejected/")]
end
APPLIED -. measures back into .-> AB
classDef store fill:#e8f4fd,stroke:#5b9bd5,stroke-width:2px,color:#1f3a5f
classDef proc fill:#fff4e6,stroke:#e8a33d,stroke-width:1px,color:#5a3d0f
classDef trace fill:#f0e8fd,stroke:#7e5dc0,stroke-width:1px,color:#2f1e60
class JOURNAL,PROPOSALS,APPLIED,REJECTED store
class EV,OBSERVE,W,S,AB,AGENT,QUEUE proc
class TRACE trace
```
The observation layer is a 350-line Node hook. Pure regex, counters, ring buffers — no LLM in the hot path. Signals append one JSONL line per detection to `~/.claude/adam/journal.jsonl`.
The analysis layer is an LLM subagent invoked by `/reflect`. Before the analyst runs, three deterministic pre-processors filter and enrich the journal: `adam-window.mjs` drops stale entries per per-signal age, `adam-score.mjs` computes per-session urgency dampeners + reinforcement candidates, and `adam-ab-measure.mjs` checks whether previously auto-applied edits actually reduced their originating signal.
The analyst clusters signals, scores them against a deterministic rubric (see below), and emits proposal markdown files to `~/.claude/adam/proposals/`. Each proposal carries a `# Diagnosis` block (Trigger / Action / Mismatch / Outcome with a verbatim transcript quote), a `# Success criterion`, and the source journal-entry timestamps it clustered.
Auto-apply runs only for low-blast types (memory entries, new skills, ephemeral nudges, reinforcement logs) backed by cross-session evidence. Everything else queues for your manual approve / reject / edit walk.
## Signals
| Signal | Trigger | Window* |
|---|---|---|
| `correction` | Strong tokens (`stop`, `wrong`, `undo`, …) OR weak tokens (`no`, `actually`, `wait`) with negation/contrast nearby | 30d |
| `retry_loop` | Same tool + same args called 3× in a 10-event window | 14d |
| `weak_agent` | Same subagent dispatched 2× in last 5 tool calls | 30d |
| `tool_error_loop` | Same error fingerprint 3× in a 5-event ring (fingerprints normalised — `ECONNREFUSED` and `"Connection refused"` cluster) | 30d |
| `dead_end` | 8 PostToolUse events without a UserPromptSubmit between them | 7d |
| `edit_churn` | Same file edited 4× in a window | 14d |
| `build_loop` | 2× build/test/compile commands fail in same session | 30d |
| `subagent_dispatch_pattern` | Same subagent dispatched ≥ 3× cumulatively | 30d |
| `correction_free_streak` | 5 clean UserPromptSubmits in a row — reinforcement input | 60d |
| `clean_recovery` | 3 clean PostToolUse events after a struggle signal — reinforcement input | 60d |
| `task_completed` | 5 tools / 3 kinds / 0 corrections — fed into the urgency dampener + reinforcement candidates | 60d |
\* Per-signal sliding window for `/reflect` analysis. See `SIGNAL_WINDOWS_DAYS` in `adam/scripts/adam-window.mjs`.
Detection is local, regex-based, zero LLM cost. Signals append to `~/.claude/adam/journal.jsonl`.
When you run `/reflect`, the `adam` subagent reads the journal, clusters signals, scores them against a deterministic rubric, and emits proposal files to `~/.claude/adam/proposals/`. Auto-applied proposals only ship for low-blast types (memory, new skills) backed by cross-session evidence; everything else queues for your manual approve/reject/edit walk.
## Auto-apply rubric
## Why
```
Sum:
+2 Signal repeated ≥ 3× across ≥ 2 sessions (within signal's window)
+2 Struggle signal appearing ≥ 1× within a single session (does not stack)
+2 Transcript contains positive endorsement near related action
+1 Multi-axis cluster (≥ 2 distinct struggle types in same session)
-1 Type-bias penalty (≥ 3 rejections, applied:rejected < 1:2)
+1 Blast radius low (memory or new isolated skill)
0 Blast radius medium (new agent, new hook, edit existing skill)
-1 Blast radius high (CLAUDE.md, settings hooks, edit agent, deletion)
+1 Surgical (one file, ≤ 50 LOC for non-skill_new; ≤ 80 LOC for skill_new)
-3 Touches deny-list (settings.json hooks/permissions, CLAUDE.md, deletions)
```
LLM coding sessions reveal repeated friction the moment you stop and look. ADAM looks so you don't have to.
Modifiers applied at scoring time:
- × `dampener` from `adam-score.mjs` (0.5 / 0.75 / 1.0 based on session's `task_completed` count) — sessions that net-succeeded score lower urgency.
`auto_apply_eligible` requires **all** of:
- `confidence ≥ 4`
- `blast_radius == low`
- `type ∈ {memory, skill_new, nudge, reinforcement}` (or `skill_edit` via the win-driven gate)
- `cross_session_evidence == true` (except `nudge`, which is single-session by design)
- `adam-cooldown.mjs` returns `cool` for `(target_skill, proposal_fingerprint)`
- `contradiction_flag` unset
`skill_edit` additionally requires:
- Win-signal evidence (`correction_free_streak` / `clean_recovery` cites target skill)
- Diff is append-only, ≤ 30 LOC, resulting size ≤ 2× original
- No auto-edit to same target in past 7 days (per-fingerprint cooldown)
- No rejection-blacklist on target in past 30 days
- `# Diagnosis` section present + structurally valid
Everything else queues.
## Lifecycle: from signal to permanent improvement
Every proposal records the journal entry timestamps that fed its cluster (`source_entries` in frontmatter). When you apply or reject a proposal, the skill calls `adam-archive.mjs` which moves matching entries from `journal.jsonl` to `journal/actioned-<id>.jsonl`. The result:
- `journal.jsonl` stays bounded by **active** observations only.
- The next `/reflect` reads `applied/` + `rejected/` frontmatter, builds an excluded-timestamps set, and skips any leftover journal entries that were already actioned.
- Rule changes (e.g. lowering a threshold) immediately re-evaluate the remaining active observations — no manual cursor rewind needed.
Auto-applied proposals additionally append to `~/.claude/adam/ab-tracking.jsonl`. The next time `/reflect` runs (and 7+ days have passed), `adam-ab-measure.mjs` computes a pre/post delta of the originating signal count. Status: `improved` / `neutral` / `regressed` / `no_baseline` / `pending`. Regressions surface at the top of the analyst's output so a bad fix doesn't quietly persist.
## Inspecting the analyst's reasoning
Every `/reflect` run also writes the analyst's clustering trace to `~/.claude/adam/last-trace.txt`. The trace records, per cluster: signal type, occurrence count, sessions, which gates passed or failed, and whether the cluster produced a proposal or was skipped (with reason: `threshold` / `cross_session` / `window` / `contradiction` / `other`).
```sh
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
```
Or pass `--explain` to `/reflect` to render the full trace inline.
## What it will not do
- 🚫 No background LLM spend. The analyst runs only when you invoke `/reflect`.
- 🚫 No retroactive transcript mining beyond the journal.
- 🚫 No hard `rm` of any artifact. Deletions are soft (`mv` to `trash/<ts>/`).
- 🚫 No autonomous edits to `CLAUDE.md`, agents, hooks, or `settings.json` — these always queue for review regardless of confidence.
- 🚫 No proposal that matches a previously-rejected idea (≥ 2 token overlap with rejection's `# Why`).
- 🚫 No invented trigger phrases for new skills — every trigger comes from observed user input.
## Layout
```
~/.claude/
├── hooks/
│ ├── adam-observe.mjs # signal collector
│ └── adam-nudge.mjs # SessionStart reminder when ≥3 proposals queued
├── agents/adam.md # analyst subagent (system prompt + rubric)
├── skills/adam-self-improvement/SKILL.md # /reflect protocol
├── commands/reflect.md # /reflect slash command
│ ├── adam-observe.mjs # signal collector (UserPromptSubmit / PreToolUse / PostToolUse)
│ └── adam-nudge.mjs # SessionStart reminder + pending-upgrade warning
├── agents/adam.md # analyst subagent (system prompt + rubric)
├── skills/adam-self-improvement/
│ └── SKILL.md # /reflect protocol
├── commands/reflect.md # /reflect slash command
└── adam/
├── journal.jsonl # append-only signal log (active observations)
├── journal/ # rotated daily logs + actioned-<id>.jsonl per applied/rejected proposal
├── state.json # per-session counters
├── usage.json # skill/agent invocation tallies + payload visibility counters
├── proposals/ # queued, awaiting review
├── applied/ # approved + auto-applied archive
├── rejected/ # rejected (with reason)
├── trash/ # soft-deleted artifacts (recoverable)
├── scripts/ # adam-archive.mjs (called by skill on apply/reject)
── tests/run-tests.sh # 27 verification tests (isolated tmpdir; never touches live state)
├── journal.jsonl # active observations
├── journal/ # rotated weekly (YYYY-Www.jsonl) + actioned-<id>.jsonl
├── state.json # per-session counters
├── usage.json # invocation tallies + visibility metrics
├── active-nudges.json # ephemeral SessionStart reminders (auto-expire)
├── ab-tracking.jsonl # one entry per auto-apply, drives effectiveness measurement
├── reinforcements.jsonl # appended on reinforcement proposal apply
├── last-trace.txt # most recent analyst clustering trace
├── proposals/ # queued, awaiting review
── applied/ # approved + auto-applied archive
├── rejected/ # rejected with reason
├── trash/ # soft-deleted artifacts (recoverable)
├── scripts/
│ ├── adam-utils.mjs # shared journal-reading + frontmatter parsing
│ ├── adam-window.mjs # per-signal sliding-window filter
│ ├── adam-score.mjs # urgency dampener + reinforcement candidates
│ ├── adam-ab-measure.mjs # 7d pre/post delta per auto-applied edit
│ ├── adam-cooldown.mjs # per-(skill, fingerprint) cooldown gate
│ ├── adam-nudge-eligibility.mjs # dead_end session-count check
│ ├── adam-explain.mjs # clustering trace parser/renderer
│ ├── adam-apply-reinforcement.mjs # reinforcement proposal apply
│ ├── adam-upgrade.mjs # .adam-new file UX (list/diff/accept)
│ └── adam-archive.mjs # post-apply journal cleanup
└── tests/run-tests.sh # 87 isolated tests; never touches live state
```
## Install
## What's new
### One-liner (recommended)
```sh
curl -fsSL https://raw.githubusercontent.com/lukaszraczylo/claude-adam/main/install.sh | bash
```
Pin a release for reproducibility:
```sh
curl -fsSL https://raw.githubusercontent.com/lukaszraczylo/claude-adam/v0.3.1/install.sh \
| VERSION=v0.3.1 bash
```
The installer clones the repo to `/tmp`, copies files into `~/.claude/`, and offers to merge ADAM's hook entries into your `~/.claude/settings.json` (with a diff preview and `[y/N]` confirmation — your existing hooks are preserved). Pass `--yes` to skip the prompt; `--dry-run` to preview without writing.
Requires `git`, `curl`, `jq`, and `node` 18+.
### From a clone
```sh
git clone https://github.com/lukaszraczylo/claude-adam
cd claude-adam
./install.sh
```
### Upgrade-safe
These files are **never overwritten** if they already exist:
- `~/.claude/adam/journal.jsonl` — your observation log
- `~/.claude/adam/state.json` — session counters
- `~/.claude/adam/usage.json` — invocation tallies
If you've locally edited any installed file (e.g. `agents/adam.md`), the installer writes the new version to `<file>.adam-new` and warns you instead of clobbering.
After install: run `bash ~/.claude/adam/tests/run-tests.sh` to verify (expect `27 passed, 0 failed`), start a fresh Claude Code session, then run `/reflect`.
- **v0.3.3** — analyst observability, A/B measurement, journal hygiene. ISO-week journal rotation replaces 5MB size-based (fixes silent cluster-straddling under-count); per-signal sliding windows via `adam-window.mjs`; error fingerprint normalisation; correction corpus expanded + weak-token co-occurrence requirement (kills the `"actually, I think..."` false positive); mandatory clustering trace + `adam-explain.mjs`; new `nudge` and `reinforcement` proposal types; per-(skill, fingerprint) cooldown via `adam-cooldown.mjs`; `task_completed` scoring (dampener + reinforcement); A/B effectiveness measurement; upgrade UX overhaul (`adam-upgrade.mjs --list/--diff/--accept`); shared `adam-utils.mjs`. 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` 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.
- **v0.3.0** — causal diagnosis: every proposal carries a `# Diagnosis` block (Trigger/Action/Mismatch/Outcome with verbatim transcript quote), plus `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).
- **v0.2.0** — actioned-entry archival via `adam-archive.mjs`; `cursor` field deprecated.
## Requirements
- Claude Code v2.1.0+ (for auto skill hot-reload; older versions need session restart after `skill_new` proposals are applied)
- Node.js 18+ (for the hook; tested on v22)
- Bash 4+, `git`, `curl`, `jq` (for installer + test harness)
- **Claude Code v2.1.0+** — for auto skill hot-reload (older versions need a session restart after `skill_new` proposals).
- **Node.js 18+** — tested on v22, used by the hook + helper scripts. Zero npm dependencies.
- **Bash 4+**, `git`, `curl`, `jq` for the installer + test harness.
### Platform support
Tested on **macOS** (Darwin / BSD coreutils) and **Linux** (Alpine, glibc + musl). The install / uninstall / test scripts are written to be portable: `stat` uses BSD `-f` with GNU `-c` fallback, `mktemp -d -t prefix.XXXXXX` works on both, no GNU-only flags. CI smoke verified `27 passed, 0 failed` under `alpine:latest`.
## Confidence rubric
```
Sum:
+2 Signal repeated ≥3× across ≥2 sessions
+2 Struggle signal appearing ≥1× within a single session (does not stack)
+2 Transcript contains positive endorsement near related action
+1 Multi-axis cluster (≥2 distinct struggle types in same session)
-1 Type-bias penalty (≥3 rejections, applied:rejected <1:2)
+1 Blast radius low (memory or new isolated skill)
0 Blast radius medium (new agent, new hook, edit existing skill)
-1 Blast radius high (CLAUDE.md, settings hooks, edit agent, deletion)
+1 Surgical (one file, ≤50 LOC for non-skill_new; ≤80 LOC for skill_new)
-3 Touches deny-list (settings.json hooks/permissions, CLAUDE.md, deletions)
auto_apply_eligible requires ALL:
confidence ≥ 4
blast_radius == low
type ∈ {memory, skill_new, skill_edit} # skill_edit also passes the win-driven gate
cross_session_evidence == true (single-session-only proposals always queue)
skill_edit additionally requires (v0.2.1+):
win-signal evidence (correction_free_streak / clean_recovery cites target skill)
diff is append-only, ≤30 LOC, resulting size ≤2× original
no auto-edit to same target in past 7 days (cooldown)
no rejection-blacklist on target in past 30 days
contradiction heuristic does not flag (v0.3.0+)
# Diagnosis section present + structurally valid (v0.3.0+)
```
## Lifecycle: how proposals become permanent
Every proposal records the journal entry timestamps that fed its cluster (`source_entries` in frontmatter). When you apply or reject a proposal, the skill calls `adam/scripts/adam-archive.mjs` which moves matching entries from `journal.jsonl` to `journal/actioned-<id>.jsonl`. Effects:
- The `journal.jsonl` stays bounded by **active** observations only.
- The next `/reflect` reads applied/ + rejected/ frontmatter, builds an excluded-timestamps set, and skips any leftover journal entries that were already actioned.
- Rule changes (e.g. lowering a threshold) immediately re-evaluate the remaining active observations — no manual cursor rewind needed.
## What it will not do
- No background LLM spend. The analyst runs only when you invoke `/reflect`.
- No retroactive transcript mining beyond the journal cursor.
- No hard `rm` of any artifact. Deletions are soft (`mv` to `trash/<ts>/`).
- No autonomous edits to `CLAUDE.md`, agents, hooks, or `settings.json` — these always queue for review regardless of confidence.
- No proposal that matches a previously-rejected idea (≥2 token overlap with rejection's `# Why`).
- No invented trigger phrases for new skills — every trigger comes from observed user input.
Tested on **macOS** (Darwin / BSD coreutils) and **Linux** (Alpine, glibc + musl). The install / uninstall / test scripts are written to be portable: `stat` uses BSD `-f` with GNU `-c` fallback, `mktemp -d -t prefix.XXXXXX` works on both, no GNU-only flags. CI smoke verified under `alpine:latest`.
## Uninstall
@@ -173,6 +289,20 @@ rm -rf ~/.claude/skills/adam-self-improvement
Then remove the four `adam-*` hook entries from `~/.claude/settings.json`.
## Contributing
Issues and PRs welcome — especially additional signal types, transcript-aware diagnosis improvements, and platform fixes. Run the test suite before opening a PR:
```sh
bash ~/.claude/adam/tests/run-tests.sh
```
## License
[MIT](LICENSE) — © 2026 Lukasz Raczylo
---
<div align="center">
<sub>Named after my son Adam, who taught me that observation is the start of every interesting thing.</sub>
</div>
+190
View File
@@ -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();
}
+91
View File
@@ -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();
}
+1 -38
View File
@@ -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) {
+191
View File
@@ -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();
}
+238
View File
@@ -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);
}
}
+97
View File
@@ -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();
}
+202
View File
@@ -0,0 +1,202 @@
#!/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",
"silent_drift",
"error_after_recovery",
]);
export const REINFORCEMENT_THRESHOLD = 3;
// Severity divisor per struggle signal type. Severity = max(1, floor(count / divisor)).
// Entries without `count` default to severity 1. Source of truth — referenced by
// agents/adam.md (Confidence rubric → severity-sum bullets).
export const SEVERITY_DIVISORS = {
dead_end: 8,
edit_churn: 4,
tool_error_loop: 3,
retry_loop: 3,
weak_agent: 2,
build_loop: 1,
};
export function entrySeverity(entry) {
if (!entry || typeof entry !== "object") return 1;
const divisor = SEVERITY_DIVISORS[entry.type];
if (!divisor) return 1;
const count = typeof entry.count === "number" && entry.count > 0 ? entry.count : 1;
return Math.max(1, Math.floor(count / divisor));
}
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,
severity_sum: 0,
severity_by_type: {},
});
}
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 sev = entrySeverity(e);
slot.severity_sum += sev;
slot.severity_by_type[e.type] = (slot.severity_by_type[e.type] || 0) + sev;
}
}
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();
}
+251
View File
@@ -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());
}
+92
View File
@@ -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;
}
+176
View File
@@ -0,0 +1,176 @@
#!/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,
silent_drift: 14,
error_after_recovery: 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
View File
+1129 -6
View File
File diff suppressed because it is too large Load Diff
+187 -5
View File
@@ -20,7 +20,32 @@ 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 |
| `silent_drift` | 14 d | exploration-without-action is task-local |
| `error_after_recovery` | 30 d | recovery-then-same-error patterns persist |
| `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
@@ -36,8 +61,11 @@ The hook emits these `type` values into the journal:
| `edit_churn` | same file edited 4× in window | file basename |
| `build_loop` | 2 build/test/compile commands fail in session | session |
| `subagent_dispatch_pattern` | same subagent dispatched ≥3× cumulatively | subagent_type |
| `silent_drift` | 5 consecutive read-only PostToolUse without an action tool (reset on action or UserPromptSubmit) | `active_skills[0]` |
| `error_after_recovery` | same error fingerprint returns within 5 PostToolUse of a `clean_recovery` | (`recovered_from`, `original_fp`) |
| `correction_free_streak` | 5 clean UserPromptSubmits in a row (no correction phrase) | `active_skills[0]` |
| `clean_recovery` | 3 clean PostToolUse events after a `tool_error_loop`/`dead_end`/`retry_loop` | (`recovered_from`, `active_skills[0]`) |
| `task_completed` | UserPromptSubmit closes a run of ≥5 tool calls with ≥3 distinct tool kinds and 0 corrections | sorted `tool_kinds` tuple |
## Process
@@ -60,9 +88,17 @@ The hook emits these `type` values into the journal:
- `edit_churn`: cluster by file basename pattern (e.g. `*.test.ts`).
- `build_loop`: cluster by `session`.
- `subagent_dispatch_pattern`: cluster by `subagent_type`.
- `silent_drift`: cluster by `active_skills[0]` (empty string when no skill is active).
- `error_after_recovery`: cluster by (`recovered_from`, `original_fp`).
- `correction_free_streak`: cluster by `active_skills[0]`. Treat ≥3 streaks across ≥2 sessions naming the same skill as cross-session evidence.
- `clean_recovery`: cluster by (`recovered_from`, `active_skills[0]`). A win cluster qualifies for `skill_edit` only when the named skill exists in `skills_root`.
5. **Multi-axis correlation**: for each session that produced ≥2 distinct struggle types (`tool_error_loop`, `dead_end`, `weak_agent`, `retry_loop`, `edit_churn`, `build_loop`), tag clusters from that session as `multi_axis: true`. This grants +1 confidence at scoring.
- `task_completed`: cluster by sorted `tool_kinds` tuple (the multi-tool recipe). Single entry qualifies for `skill_new` proposal (drafting protocol applies). Cross-session evidence requires ≥2 entries from distinct sessions with same tuple — without it, proposal queues, never auto-applies. Run the existing skill-overlap rule before drafting: if the recipe matches an existing skill's name/description tokens, route to `skill_edit` instead.
5. **Multi-axis correlation**: for each session that produced ≥2 distinct struggle types (`tool_error_loop`, `dead_end`, `weak_agent`, `retry_loop`, `edit_churn`, `build_loop`, `silent_drift`, `error_after_recovery`), tag clusters from that session as `multi_axis: true`. This grants +1 confidence at scoring.
5b. **Skill-attribution sub-clustering**: after primary clustering (step 4), for every struggle cluster (`tool_error_loop`, `dead_end`, `weak_agent`, `retry_loop`, `edit_churn`, `build_loop`, `silent_drift`, `error_after_recovery`) that contains entries with non-empty `active_skills[0]`:
- Split into per-skill sub-clusters keyed on `active_skills[0]`. Entries with empty `active_skills` stay in the original cluster.
- If a sub-cluster has ≥3 entries AND names a skill that exists in `skills_root`, mark it as a candidate for `skill_edit` (struggle-driven variant; see "Struggle-driven `skill_edit` eligibility"). Otherwise treat the parent cluster normally.
- The umbrella cluster (cross-skill) still emits its usual proposal type (memory, etc.) — sub-clusters do NOT replace it, they supplement it.
6. For each cluster qualifying under the rubric — ≥3 occurrences across ≥2 sessions, OR (for struggle types) ≥1 entry within a single session, OR (for `correction`) ≥3 occurrences across ≥2 cwds:
a. If cluster topic matches a rejected idea via the rejected-ideas fuzzy set (≥2 token overlap with rejection's `# Why`), skip with reason `"rejected-similar"`.
b. Pull ~20 messages of transcript context from `transcripts_root` to enrich. Never read full transcripts.
@@ -229,13 +265,80 @@ 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.
## Struggle-driven `skill_edit` eligibility
Skill-attribution sub-clustering (step 5b) produces struggle-driven `skill_edit` candidates: a sub-cluster of ≥3 struggle entries all naming the same `active_skills[0]` that exists in `skills_root`. These proposals are emitted but **ALWAYS queue**`auto_apply_eligible: false` regardless of confidence. Negative evidence on a skill is a weaker basis for self-modification than positive evidence (the skill may be active during friction caused by something else), so the human reviews every one.
A struggle-driven `skill_edit` proposal MUST:
1. Set `target` to the matched skill's `SKILL.md` path.
2. Cluster severity-sum ≥ 10 (same threshold as the +1 rubric bullet).
3. Sub-cluster names exactly one skill (no ambiguity across distinct `active_skills[0]` values).
4. `# Proposed change` is an append-only diff adding a `## When struggling` section (naive default body: a checkpoint-or-pause rule appropriate to the dominant signal — e.g. `dead_end` → "After 16 PostToolUse events without UserPromptSubmit, emit a one-line checkpoint summary before continuing.").
5. Frontmatter includes `struggle_evidence: "<ts of one source entry naming this skill>"` and `struggle_signals: [<list of signal types in the sub-cluster>]`. The win-driven `win_evidence` field is omitted.
6. Subject to the same Per-(skill, fingerprint) cooldown as win-driven `skill_edit`.
If gate (2) or (3) fails: skip the sub-cluster (the parent cluster still produces its umbrella proposal). The sub-cluster's `source_entries` overlap with the parent's — the apply pipeline handles dedup via the excluded-timestamps set.
## 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:
- Signal repeated ≥3× across ≥2 sessions: **+2**
- Struggle signal (`tool_error_loop`, `dead_end`, `weak_agent`, `retry_loop`, `edit_churn`, `build_loop`) appearing ≥1× within a single session: **+2** *(each struggle entry already represents a hook-side threshold crossing — e.g. 8 tools without a prompt, 3 same-args retries, 4 edits to one file. Treat each entry as one piece of evidence. Does not stack with the cross-session bonus.)*
- Struggle signal (`tool_error_loop`, `dead_end`, `weak_agent`, `retry_loop`, `edit_churn`, `build_loop`, `silent_drift`, `error_after_recovery`) appearing ≥1× within a single session: **+2** *(each struggle entry already represents a hook-side threshold crossing — e.g. 8 tools without a prompt, 3 same-args retries, 4 edits to one file, 5 read-only tools in a row, same-fp error after a recovery. Treat each entry as one piece of evidence. Does not stack with the cross-session bonus.)*
- Transcript contains positive endorsement (`yes`, `exactly`, `do that`, `keep doing`) within 2 messages of related action: **+2**
- Multi-axis cluster (≥2 distinct struggle types in same session): **+1**
- Cluster severity-sum ≥ 10 (severity per entry = `max(1, floor(count / divisor))` using `SEVERITY_DIVISORS` from `adam-score.mjs``dead_end:8, edit_churn:4, tool_error_loop:3, retry_loop:3, weak_agent:2, build_loop:1`; entries without `count` count as 1): **+1**
- Cluster severity-sum ≥ 32: **+1** *(additive — a severity-sum of 32 gets +1 from the previous bullet AND +1 here, total +2.)*
- Skill-attributed sub-cluster (≥3 entries naming the same `active_skills[0]` that exists in `skills_root`): **+1**
- Type-bias penalty from feedback loop (≥3 rejections, applied:rejected ratio <1:2 for this `type`): **-1**
- Diagnosis flags `Mismatch: unclear` (causation could not be reconstructed from transcript context): **-1**
- Blast radius: low **+1**, medium **0**, high **-1** (default per type — see Proposal types table)
@@ -254,13 +357,41 @@ 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") |
| `skill_edit` | existing skill file | medium | yes (win-driven only) if win-evidence + LOC + cooldown gates all pass (see "Win-driven skill_edit eligibility"); struggle-driven variant ALWAYS queues (see "Struggle-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
@@ -287,7 +418,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
@@ -299,6 +430,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>
@@ -349,3 +486,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.
+13
View File
@@ -0,0 +1,13 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 160" width="160" height="160" role="img" aria-label="claude-adam logo">
<title>claude-adam</title>
<desc>A swaddled baby — rounded A-shape bundle with a face inside and small hands extending from the wrap-band. Dark-background variant.</desc>
<g stroke="#f0f6fc">
<path d="M 36 134 Q 30 78 80 28 Q 130 78 124 134 Z" fill="none" stroke-width="9" stroke-linejoin="round"/>
<path d="M 16 100 L 44 100 Q 80 115 116 100 L 144 100" fill="none" stroke-width="6" stroke-linecap="round"/>
<path d="M 75 78 Q 80 82 85 78" fill="none" stroke-width="2.5" stroke-linecap="round"/>
</g>
<g fill="#f0f6fc">
<circle cx="72" cy="64" r="3.2"/>
<circle cx="88" cy="64" r="3.2"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 763 B

+13
View File
@@ -0,0 +1,13 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 160" width="160" height="160" role="img" aria-label="claude-adam logo">
<title>claude-adam</title>
<desc>A swaddled baby — rounded A-shape bundle with a face inside and small hands extending from the wrap-band. Light-background variant.</desc>
<g stroke="#24292f">
<path d="M 36 134 Q 30 78 80 28 Q 130 78 124 134 Z" fill="none" stroke-width="9" stroke-linejoin="round"/>
<path d="M 16 100 L 44 100 Q 80 115 116 100 L 144 100" fill="none" stroke-width="6" stroke-linecap="round"/>
<path d="M 75 78 Q 80 82 85 78" fill="none" stroke-width="2.5" stroke-linecap="round"/>
</g>
<g fill="#24292f">
<circle cx="72" cy="64" r="3.2"/>
<circle cx="88" cy="64" r="3.2"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 764 B

+19
View File
@@ -0,0 +1,19 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 160" width="160" height="160" role="img" aria-label="claude-adam logo">
<title>claude-adam</title>
<desc>A swaddled baby — rounded A-shape bundle with a face inside and small hands extending from the wrap-band. Adapts to light/dark via embedded media query + currentColor fallback.</desc>
<style>
svg { color: #24292f; }
@media (prefers-color-scheme: dark) {
svg { color: #f0f6fc; }
}
</style>
<g stroke="currentColor">
<path d="M 36 134 Q 30 78 80 28 Q 130 78 124 134 Z" fill="none" stroke-width="9" stroke-linejoin="round"/>
<path d="M 16 100 L 44 100 Q 80 115 116 100 L 144 100" fill="none" stroke-width="6" stroke-linecap="round"/>
<path d="M 75 78 Q 80 82 85 78" fill="none" stroke-width="2.5" stroke-linecap="round"/>
</g>
<g fill="currentColor">
<circle cx="72" cy="64" r="3.2"/>
<circle cx="88" cy="64" r="3.2"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 946 B

+123 -8
View File
@@ -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);
+261 -18
View File
@@ -14,11 +14,85 @@ 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"]);
const READ_ONLY_TOOLS = new Set([
"Read", "Grep", "Glob", "ToolSearch", "WebFetch", "WebSearch",
"mcp__filepuff__file_read", "mcp__filepuff__file_search",
"mcp__filepuff__find_definition", "mcp__filepuff__find_references",
"mcp__filepuff__ast_query", "mcp__filepuff__symbol_at", "mcp__filepuff__ping",
]);
const WINDOW_SIZE = 10;
const RETRY_THRESHOLD = 3;
const AGENT_RESPAWN_THRESHOLD = 2;
@@ -30,8 +104,13 @@ const BUILD_LOOP_THRESHOLD = 2;
const SUBAGENT_DISPATCH_THRESHOLD = 3;
const CORRECTION_FREE_THRESHOLD = 5;
const CLEAN_RECOVERY_WINDOW = 3;
const SILENT_DRIFT_THRESHOLD = 5;
const ERROR_AFTER_RECOVERY_WINDOW = 5;
const RECENT_RECOVERIES_MAX = 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) {
@@ -42,14 +121,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 {}
}
@@ -63,7 +197,7 @@ function readStdin() {
}
function appendJournal(entry) {
rotateIfLarge(JOURNAL, STATE_MAX_BYTES * 5);
rotateIfNeeded(JOURNAL);
try {
appendFileSync(JOURNAL, JSON.stringify(entry) + "\n");
} catch {}
@@ -105,15 +239,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) {
@@ -124,6 +277,8 @@ function resetFrictionCounters(state) {
state.edit_churn_emitted = {};
state.build_failure_count = 0;
state.build_loop_emitted = false;
state.silentDriftCounter = 0;
state.silentDriftEmitted = false;
}
function resetSessionLocal(state) {
@@ -132,7 +287,12 @@ function resetSessionLocal(state) {
state.subagent_dispatch_emitted = {};
state.correctionFreeCounter = 0;
state.recoveryWatch = null;
state.recentRecoveries = [];
state.session_post_count = 0;
state.tool_window = [];
state.task_tool_kinds = {};
state.task_tool_count = 0;
state.task_corrections = 0;
}
function ensureStateDefaults(state) {
@@ -149,12 +309,23 @@ function ensureStateDefaults(state) {
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;
if (typeof state.silentDriftCounter !== "number") state.silentDriftCounter = 0;
if (typeof state.silentDriftEmitted !== "boolean") state.silentDriftEmitted = false;
if (!Array.isArray(state.recentRecoveries)) state.recentRecoveries = [];
if (typeof state.session_post_count !== "number") state.session_post_count = 0;
}
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();
@@ -169,7 +340,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",
@@ -178,6 +349,7 @@ function main() {
prev_file: last.file || null,
});
state.correctionFreeCounter = 0;
state.task_corrections += 1;
} else {
state.correctionFreeCounter += 1;
if (state.correctionFreeCounter >= CORRECTION_FREE_THRESHOLD) {
@@ -190,6 +362,22 @@ function main() {
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;
@@ -231,12 +419,24 @@ function main() {
}
state.tool_window.push(windowEntry);
if (state.tool_window.length > WINDOW_SIZE) state.tool_window.shift();
state.session_post_count += 1;
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 (READ_ONLY_TOOLS.has(tool)) {
state.silentDriftCounter += 1;
if (state.silentDriftCounter >= SILENT_DRIFT_THRESHOLD && !state.silentDriftEmitted) {
emit({ ts, session, cwd, type: "silent_drift", read_count: state.silentDriftCounter, last_tool: tool });
state.silentDriftEmitted = true;
}
} else {
state.silentDriftCounter = 0;
state.silentDriftEmitted = false;
}
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;
@@ -252,6 +452,23 @@ function main() {
const fp = errorFingerprint(input.tool_response);
if (fp) {
bumpUsage("payload:tool_response_error_seen");
if (state.recentRecoveries.length) {
const keep = [];
for (const rec of state.recentRecoveries) {
const tools_since = state.session_post_count - rec.emitted_at_count;
if (tools_since > ERROR_AFTER_RECOVERY_WINDOW) continue;
if (Array.isArray(rec.fps) && rec.fps.includes(fp)) {
emit({
ts, session, cwd, type: "error_after_recovery",
recovered_from: rec.recovered_from, original_fp: fp,
tools_since_recovery: tools_since,
});
continue;
}
keep.push(rec);
}
state.recentRecoveries = keep;
}
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;
@@ -293,8 +510,17 @@ function main() {
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: [] };
state.recoveryWatch = {
recovered_from: struggleEmittedThisTurn,
since_ts: ts,
clean_count: 0,
window_tools: [],
watched_fps: state.last_errors.map(e => e.fp),
};
} else if (state.recoveryWatch) {
const turnHadError = fp !== null;
if (turnHadError) {
@@ -311,6 +537,12 @@ function main() {
active_skills: activeNames(state, "skill"),
active_agents: activeNames(state, "agent"),
});
state.recentRecoveries.push({
recovered_from: state.recoveryWatch.recovered_from,
fps: state.recoveryWatch.watched_fps || [],
emitted_at_count: state.session_post_count,
});
if (state.recentRecoveries.length > RECENT_RECOVERIES_MAX) state.recentRecoveries.shift();
state.recoveryWatch = null;
}
}
@@ -320,5 +552,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
View File
@@ -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
+110 -10
View File
@@ -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.