{"record":{"id":"5e92cdd4bdc08e18","repo":"JuliusBrussee/caveman","slug":"encode-before-evidence-w","errorCode":null,"errorMessage":"encode before evidence: %w","messagePattern":"encode before evidence: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/store/learn_outcomes.go","lineNumber":93,"sourceCode":"\t}\n\tif fixKind == \"\" {\n\t\treturn AppliedFixRecord{}, fmt.Errorf(\"sink %q has no measurable fix kind; pass --fix-kind explicitly\", sinkID)\n\t}\n\tif len(fixKind) > 128 {\n\t\treturn AppliedFixRecord{}, fmt.Errorf(\"fix kind exceeds 128 bytes\")\n\t}\n\tif len(note) > 4096 {\n\t\treturn AppliedFixRecord{}, fmt.Errorf(\"note exceeds 4096 bytes\")\n\t}\n\tappliedAt := appliedAtTime.Format(time.RFC3339)\n\tbeforeEvidence := make(map[string]any, len(sink.Evidence)+1)\n\tfor key, value := range sink.Evidence {\n\t\tbeforeEvidence[key] = value\n\t}\n\tbeforeEvidence[\"before_source\"] = beforeSource\n\tevidence, err := json.Marshal(beforeEvidence)\n\tif err != nil {\n\t\treturn AppliedFixRecord{}, fmt.Errorf(\"encode before evidence: %w\", err)\n\t}\n\t// Fingerprint the edited artifact as it stands NOW — which at\n\t// `caveman learn applied` time is immediately after the approved edit. This\n\t// is the provenance anchor: a later scan re-hashes the same path and can\n\t// state whether the change we proposed is still the change that is there.\n\ttarget := fingerprintFixTarget(*sink)\n\ttargetJSON := \"\"\n\tif target != nil {\n\t\tif encoded, encodeErr := json.Marshal(target); encodeErr == nil {\n\t\t\ttargetJSON = string(encoded)\n\t\t}\n\t}\n\tresult, err := s.db.Exec(`INSERT INTO applied_fixes\n\t\t(sink_id, practice_id, fix_kind, applied_at, before_tokens_per_turn, before_evidence_json, note, target_json)\n\t\tVALUES (?, ?, ?, ?, ?, ?, ?, ?)`, sink.SinkID, sink.PracticeID, fixKind, appliedAt, sink.TokensPerTurn, string(evidence), note, targetJSON)\n\tif err != nil {\n\t\treturn AppliedFixRecord{}, fmt.Errorf(\"record applied fix: %w\", err)\n\t}","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/proxy/internal/store/learn_outcomes.go#L75-L111","documentation":"Before recording the fix, RecordAppliedFix builds a before-evidence map from sink.Evidence plus before_source and serializes it with json.Marshal. If marshaling fails (e.g. a value contains an unsupported type such as a channel, func, or cyclic structure) the error is wrapped as \"encode before evidence\". In practice this almost never happens because evidence comes from JSON scan reports.","triggerScenarios":"sink.Evidence (or beforeSource) containing a value json.Marshal cannot encode — channels, funcs, cyclic maps, or NaN-bearing numbers.","commonSituations":"Programmatic callers constructing an AppliedFix-capable sink with hand-built Evidence instead of report-derived data; a corrupted in-memory sink.","solutions":["Ensure sink.Evidence only holds JSON-serializable values","Sanitize or re-marshal evidence into map[string]any via a JSON round-trip before calling","Inspect the wrapped %w error to identify the offending key/value"],"exampleFix":"// before\nsink.Evidence[\"conn\"] = someChan\n// after\nsafe, _ := json.Marshal(someEvidence)\nvar clean map[string]any\njson.Unmarshal(safe, &clean)\nsink.Evidence = clean","handlingStrategy":"validation","validationCode":"if _, err := json.Marshal(sink.Evidence); err != nil { return fmt.Errorf(\"evidence not serializable: %w\", err) }","typeGuard":null,"tryCatchPattern":"var jerr *json.UnsupportedTypeError; if errors.As(err, &jerr) { log.Warnf(\"dropping unserializable evidence value of type %v\", jerr.Value) }","preventionTips":["Only populate Evidence from JSON-derived data","JSON round-trip any hand-built maps before passing them in","Never store channels, funcs, or cyclic structures in Evidence"],"tags":["json","serialization","learn"],"backgroundTag":"json-marshal-failed","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}