JuliusBrussee/caveman · error

encode before evidence

Error message

encode before evidence: %w

What it means

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.

Solutions

  1. Ensure sink.Evidence only holds JSON-serializable values
  2. Sanitize or re-marshal evidence into map[string]any via a JSON round-trip before calling
  3. Inspect the wrapped %w error to identify the offending key/value

Example fix

// before
sink.Evidence["conn"] = someChan
// after
safe, _ := json.Marshal(someEvidence)
var clean map[string]any
json.Unmarshal(safe, &clean)
sink.Evidence = clean
Defensive patterns

Strategy: validation

Validate before calling

if _, err := json.Marshal(sink.Evidence); err != nil { return fmt.Errorf("evidence not serializable: %w", err) }

Try / catch

var jerr *json.UnsupportedTypeError; if errors.As(err, &jerr) { log.Warnf("dropping unserializable evidence value of type %v", jerr.Value) }

Prevention

When it happens

Trigger: sink.Evidence (or beforeSource) containing a value json.Marshal cannot encode — channels, funcs, cyclic maps, or NaN-bearing numbers.

Common situations: Programmatic callers constructing an AppliedFix-capable sink with hand-built Evidence instead of report-derived data; a corrupted in-memory sink.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/5e92cdd4bdc08e18. Report an issue: GitHub.

Appendix: source

Thrown at proxy/internal/store/learn_outcomes.go:93

	}
	if fixKind == "" {
		return AppliedFixRecord{}, fmt.Errorf("sink %q has no measurable fix kind; pass --fix-kind explicitly", sinkID)
	}
	if len(fixKind) > 128 {
		return AppliedFixRecord{}, fmt.Errorf("fix kind exceeds 128 bytes")
	}
	if len(note) > 4096 {
		return AppliedFixRecord{}, fmt.Errorf("note exceeds 4096 bytes")
	}
	appliedAt := appliedAtTime.Format(time.RFC3339)
	beforeEvidence := make(map[string]any, len(sink.Evidence)+1)
	for key, value := range sink.Evidence {
		beforeEvidence[key] = value
	}
	beforeEvidence["before_source"] = beforeSource
	evidence, err := json.Marshal(beforeEvidence)
	if err != nil {
		return AppliedFixRecord{}, fmt.Errorf("encode before evidence: %w", err)
	}
	// Fingerprint the edited artifact as it stands NOW — which at
	// `caveman learn applied` time is immediately after the approved edit. This
	// is the provenance anchor: a later scan re-hashes the same path and can
	// state whether the change we proposed is still the change that is there.
	target := fingerprintFixTarget(*sink)
	targetJSON := ""
	if target != nil {
		if encoded, encodeErr := json.Marshal(target); encodeErr == nil {
			targetJSON = string(encoded)
		}
	}
	result, err := s.db.Exec(`INSERT INTO applied_fixes
		(sink_id, practice_id, fix_kind, applied_at, before_tokens_per_turn, before_evidence_json, note, target_json)
		VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, sink.SinkID, sink.PracticeID, fixKind, appliedAt, sink.TokensPerTurn, string(evidence), note, targetJSON)
	if err != nil {
		return AppliedFixRecord{}, fmt.Errorf("record applied fix: %w", err)
	}

View on GitHub (pinned to 3ee70a1026)