{"modulePath":"github.com/memcode-ai/memcode","version":"v0.56.16","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package prefs\n\nPackage prefs is the preference-learning reducer. It scans preference\\_signal events from the event log (the source of truth), clusters them by axis + lexical similarity, weighs each cluster with recency decay, tracks contradictions, and materializes the resulting candidates into the preference\\_candidates table atomically. Promotion to a standing plaintext rule (and demotion on contradiction) is a separate, silent step in StartChat (see promote.go) — this package is pure over the event log and makes no LLM calls.\n\n## Functions\n\n```go\nfunc ConfirmPath(root, id string) string\n```\nConfirmPath resolves the plaintext file for a confirmed preference under .memcode/prefs/. The file is written as \\\u003cid\u003e-\\\u003cslug\u003e.md (see writeConfirmedPref), so this globs \\\u003cid\u003e-\\*.md rather than guessing the slug — the previous \\\u003cid\u003e.md form never matched, so demotion deleted a nonexistent path and the binding file survived. Falls back to the legacy \\\u003cid\u003e.md if no slugged file is found.\n\n## Types\n\n```go\ntype Candidate struct {\n\tID            string\n\tAxis          string\n\tText          string\n\tScope         string\n\tWeight        float64\n\tSignalCount   int\n\tSessionCount  int\n\tFirstSeen     time.Time\n\tLastSeen      time.Time\n\tStatus        string\n\tConfirmedPath string // set when Status==\"confirmed\" — the exact plaintext file to revoke\n\tEvidence      []SignalRef\n\n\t// Contradictions is the count of same-axis signals with opposite polarity\n\t// that contradicted this candidate. Used by PendingDemotions to decide whether\n\t// a confirmed pref has been overturned.\n\tContradictions int\n}\n```\nCandidate is a clustered, weighted preference candidate — the reducer's output before promotion/demotion decides its fate. It maps 1:1 onto a store.PreferenceCandidate row.\n\n```go\nfunc PendingDemotions(cands []Candidate, _ store.Store) ([]Candidate, error)\n```\nPendingDemotions returns CONFIRMED candidates whose contradiction signal count (from the event log, via the reducer's Contradictions field) has crossed contradictionThreshold — the user has pushed back enough that the standing preference should be demoted. The caller passes the candidates from Reduce (which carries Contradictions); the store parameter is accepted for the v2 case where demotion needs to re-scan the event log for fresh contradictions since the last Reduce. For v1 the candidates already carry the count.\n\n```go\nfunc PendingPromotions(cands []Candidate) []Candidate\n```\nPendingPromotions returns candidates that have crossed the evidence bar and are ready to be promoted to standing plaintext rules: weight ≥ promotionThreshold, ≥ minSignals signals, ≥ minSessions sessions, and status is still \"candidate\" (not already confirmed or demoted). Sorted by weight descending so the strongest preferences promote first.\n\n```go\nfunc Reduce(ctx context.Context, st store.Store) ([]Candidate, error)\n```\nReduce scans preference\\_signal events, clusters them, weighs them with recency decay, tracks contradictions, and materializes the candidates into the preference\\_candidates table atomically (ClearPreferenceCandidates + a batch of AddPreferenceCandidate in a single RunInTx — same pattern as learn.Run). It returns the candidates so the caller (applyPreferencePromotions) can decide promotions and demotions.\n\n```go\ntype SignalRef struct {\n\tSessionID string    `json:\"session_id\"`\n\tText      string    `json:\"text\"`\n\tStrength  float64   `json:\"strength\"`\n\tTS        time.Time `json:\"ts\"`\n\tHeadSHA   string    `json:\"head_sha,omitempty\"` // repo HEAD when the directive was given — provenance\n}\n```\nSignalRef is a pointer back to the event that contributed to a candidate — so the promotion notice and the introspect tool can show evidence (which sessions, what text). It's the \"why did this become a rule\" trail.\n\n","path":"github.com/memcode-ai/memcode/internal/prefs","name":"prefs","synopsis":"Package prefs is the preference-learning reducer.","isRedistributable":true}
