| name | accumulate-pattern |
| description | Updates per-developer cost-pattern accumulator in state/learnings.json with the current session's observations. Rolls μ and σ per (skill, session_type, model) axis using a slow exponential update (α = 0.05) so one noisy session doesn't skew learned patterns. Exports a cross-plugin-readable snapshot to shared/learnings.json. Use when: PreCompact hook fires (end-of-session or compaction trigger). Do not use during an active session — runtime invocation mid-session would distort the pattern.
|
| model | sonnet |
| tools | ["Read","Write"] |
accumulate-pattern
Preconditions
- The session has at least 5 ledger rows (otherwise there's nothing useful to accumulate)
state/learnings.json exists or can be initialized (first run)
Inputs
- Session ledger:
plugins/cost-tracker/state/ledger-YYYY-MM.jsonl (current month)
- Current session_id: from
ENCHANTED_ATTRIBUTION.session_id
- Prior learnings:
state/learnings.json (or empty {patterns: {}} on first run)
Steps
- Group current session's rows by attribution key
<plugin>:<skill>:<agent_tier> (or fall back to <plugin>:<agent_tier> if skill is null, e.g. for hook-driven observations).
- For each key, compute session-local stats:
session_mean, session_stdev, session_n.
- Load
state/learnings.json. For each key:
- Increment
n_sessions_accumulated. Update last_updated timestamp.
- Write
state/learnings.json atomically (write-to-tmp + fsync + rename — Emu-A4 pattern).
- Append a cross-plugin snapshot to
shared/learnings.json with origin: "pech" and the full patterns map.
- Emit
pech.learning.pattern.updated event (one per PreCompact, not per key).
Success criterion: atomic write succeeded (no partial file on crash); shared/learnings.json appended (not overwritten — peers own their own sections); exactly one event fired regardless of how many keys updated.
Outputs
state/learnings.json (updated)
shared/learnings.json (appended with Pech's section)
- Event:
pech.learning.pattern.updated
Handoff
budget-watcher/detect-anomaly reads state/learnings.json for the historical prior when current-session observations are < 30.
- Peer plugins (Wixie, Sylph) read
shared/learnings.json#pech for their own cost-aware decisions.
Failure modes
| Code | Scenario | Counter |
|---|
| F09 | Concurrent PreCompact from parallel sessions clobbering learnings.json | Atomic rename is mandatory; last-writer-wins is acceptable for slow-update semantics |
| F11 | Tune α up to 0.2 to "learn faster" — session-specific noise leaks into patterns | α = 0.05 is the contract; faster learning defeats the purpose of cross-session stability |
| F13 | Accumulate orphan rows (missing attribution) into a "misc" bucket | Orphans are never accumulated — they're a bug signal, not a data point |