JuliusBrussee/caveman · error

proposalrun: detail contains trailing JSON

Error message

proposalrun: detail contains trailing JSON

What it means

CanonicalDetail requires the detail to contain exactly one JSON value. After decoding the first value it decodes again expecting io.EOF; if a second value decodes successfully it throws this error. Trailing JSON (e.g. two concatenated objects) would make canonicalization and hashing ambiguous, so it is rejected.

Solutions

  1. Inspect the detail bytes and split/reject records that contain more than one JSON value
  2. Fix the producer to write exactly one JSON document per detail field
  3. If NDJSON is intended, parse per-line and process each record separately rather than passing the whole blob
  4. Pre-validate with a single-value JSON parser before hashing

Example fix

// before
detail := []byte(`{"run":1}{"extra":2}`) // CanonicalDetail -> trailing JSON
// after
detail := []byte(`{"run":1,"extra":2}`) // merge into a single JSON object
Defensive patterns

Strategy: validation

Validate before calling

dec := json.NewDecoder(bytes.NewReader(detail))
if err := dec.Decode(&struct{}{}); err != nil { return err }
if dec.More() { return errors.New("detail holds more than one JSON value") }

Try / catch

if _, err := CanonicalDetail(detail); err != nil {
    if strings.Contains(err.Error(), "trailing JSON") {
        // split concatenated documents and process each individually
    }
    return err
}

Prevention

When it happens

Trigger: Calling CanonicalDetail/RowHash with detail bytes like '{"a":1}{"b":2}' or '[1][2]' — a valid first JSON value followed by another complete JSON value.

Common situations: Two JSON payloads concatenated by a buggy writer or stream joiner; log-style NDJSON accidentally stored in a single detail field; a producer appending a second record to an existing detail.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


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

Appendix: source

Thrown at shared/platform/proposalrun/proposalrun.go:69

// hash identically, while adjacent arbitrary-precision integers remain
// distinct. The bounds mirror PostgreSQL's numeric-backed jsonb limits and
// reject values that could not be persisted there before allocating output.
// The decoder enforces the JSON number grammar (including rejecting NaN/Inf),
// and a second decode below rejects any trailing value or non-whitespace data.
func CanonicalDetail(detail json.RawMessage) ([]byte, error) {
	if len(detail) == 0 {
		return []byte("{}"), nil
	}
	var v any
	dec := json.NewDecoder(bytes.NewReader(detail))
	dec.UseNumber()
	if err := dec.Decode(&v); err != nil {
		return nil, fmt.Errorf("proposalrun: detail is not valid JSON: %w", err)
	}
	var trailing any
	if err := dec.Decode(&trailing); err != io.EOF {
		if err == nil {
			return nil, fmt.Errorf("proposalrun: detail contains trailing JSON")
		}
		return nil, fmt.Errorf("proposalrun: detail contains trailing data: %w", err)
	}
	v, err := canonicalizeJSONValue(v)
	if err != nil {
		return nil, fmt.Errorf("proposalrun: canonicalize detail: %w", err)
	}
	out, err := json.Marshal(v)
	if err != nil {
		return nil, fmt.Errorf("proposalrun: marshal canonical detail: %w", err)
	}
	return out, nil
}

// PostgreSQL jsonb stores JSON numbers using its numeric type. These are the
// documented numeric bounds (131072 digits before and 16383 after the decimal
// point); rejecting at the canonicalization boundary keeps write and read-back
// hashing deterministic and avoids unbounded strings from hostile exponents.

View on GitHub (pinned to 3ee70a1026)