{"modulePath":"github.com/memcode-ai/memcode","version":"v0.56.16","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package lessons\n\nPackage lessons is the distilled-failure memory: strategy-level lessons (\"when X breaks, do Y\") extracted from the agent's own failure-and-repair episodes, accumulated as lesson\\_signal events, and promoted to standing plaintext files once they recur — the preference\\_signal promotion rigor applied to failures (≥3 signals, ≥2 sessions, weighted score ≥ 2.0).\n\nMemory is a COST lever here, not a quality lever: a promoted lesson saves the re-derivation turns, so the gate errs toward silence. Lessons surface as DATA (background evidence in the prompt), never as instructions — the documented poisoning path is a hostile input becoming a standing rule, and the recurrence gate plus data-framing is the defense.\n\nFiles are canonical: signals live in each session's events.jsonl (the SQLite events table is a rebuildable index; see the runtime's backfill), and a promoted lesson is a user-editable .memcode/lessons/\\\u003cid\u003e-\\\u003cslug\u003e.md — delete the file to revoke the lesson.\n\n## Functions\n\n```go\nfunc Demote(root string, c Candidate) error\n```\nDemote removes a lesson's file(s) and drops a demotion marker so the same cluster can't silently re-promote next startup on the same old evidence. It re-earns only when a NEW signal lands after the marker (see demotedAndNotReearned).\n\n```go\nfunc Inline(root string) string\n```\nInline renders the top lessons as a compact context block — DATA, not instructions (the poisoning boundary), ranked by weight, hard-capped.\n\n```go\nfunc InlineTop(root string) (string, []string)\n```\nInlineTop is Inline plus the ids of the lessons actually surfaced — the caller records them (context\\_inlined) so the adherence judge only scores rules the model really saw.\n\n```go\nfunc WriteLesson(root string, c Candidate) (string, error)\n```\nWriteLesson persists a promoted lesson as a user-editable markdown file and returns its path. Evidence lines carry provenance: which session AND which commit (repo HEAD at the episode) each contributing episode came from. Promotion clears any demotion marker — the cluster re-earned its place.\n\n## Types\n\n```go\ntype Candidate struct {\n\tID           string // content-derived, stable across rebuilds\n\tTrigger      string // representative (highest-strength signal)\n\tStrategy     string\n\tWeight       float64\n\tSignalCount  int\n\tSessionCount int\n\tEvidence     []Signal\n\n\t// Adherence tallies (from KindAdherence events referencing this ID).\n\tViolations     int // violated verdicts on corrected/rejected sessions\n\tReinforcements int // followed verdicts on accepted sessions\n}\n```\nCandidate is a cluster of similar signals with its evidence tally.\n\n```go\nfunc PendingDemotions(root string, cands []Candidate) []Candidate\n```\nPendingDemotions returns candidates whose lesson file exists but whose adherence violations have crossed the threshold — the agent kept breaking the rule (or the rule kept being wrong) on sessions the human then corrected or rejected. Mirror of prefs.PendingDemotions, keyed on git-outcome evidence instead of user pushback.\n\n```go\nfunc PendingPromotions(root string, cands []Candidate) []Candidate\n```\nPendingPromotions returns candidates past the evidence bar whose lesson file doesn't exist yet. File existence IS the status — no candidate table. A candidate carrying enough violations, or demoted and not re-earned since (no signal newer than its demotion marker), stays out.\n\n```go\nfunc Reduce(ctx context.Context, st store.Store) ([]Candidate, error)\n```\nReduce scans lesson\\_signal events and clusters them into candidates. Deterministic, bounded, no model calls.\n\n```go\ntype Promoted struct {\n\tID       string // stable content-addressed id (the filename prefix)\n\tTrigger  string\n\tStrategy string\n\tWeight   float64\n}\n```\nPromoted is one promoted lesson file, parsed.\n\n```go\nfunc List(root string) []Promoted\n```\nList returns every promoted lesson, strongest first.\n\n```go\ntype Signal struct {\n\tTrigger   string\n\tStrategy  string\n\tStrength  float64\n\tSessionID string\n\tHeadSHA   string // repo HEAD when the episode happened — provenance\n\tTS        time.Time\n}\n```\nSignal is one distilled lesson observation.\n\n","path":"github.com/memcode-ai/memcode/internal/lessons","name":"lessons","synopsis":"Package lessons is the distilled-failure memory: strategy-level lessons (\"when X breaks, do Y\") extracted from the agent's own failure-and-repair episodes, accumulated as lesson_signal events, and promoted to standing plaintext files once they recur — the preference_signal promotion rigor applied to failures (≥3 signals, ≥2 sessions, weighted score ≥ 2.0).","isRedistributable":true}
