JuliusBrussee/caveman · error

cachebench: observation line %d: trailing JSON

Error message

cachebench: observation line %d: trailing JSON

What it means

After decoding one ObservationRecord from a line, a second decode must return io.EOF; anything else means extra JSON follows the object on the same line (e.g. two objects or stray tokens). One line must contain exactly one JSON object.

Source

Thrown at cacheengine/cachebench/observed.go:126

		raw := bytes.TrimSpace(scanner.Bytes())
		if len(raw) == 0 {
			continue
		}
		if len(records) >= limits.MaxRecords {
			return nil, fmt.Errorf("cachebench: observations exceed record limit %d", limits.MaxRecords)
		}
		if !validUniqueJSONObject(raw) {
			return nil, fmt.Errorf("cachebench: observation line %d: duplicate or invalid JSON", line)
		}
		decoder := json.NewDecoder(bytes.NewReader(raw))
		decoder.DisallowUnknownFields()
		var record ObservationRecord
		if err := decoder.Decode(&record); err != nil {
			return nil, fmt.Errorf("cachebench: observation line %d: %w", line, err)
		}
		var trailing any
		if err := decoder.Decode(&trailing); err != io.EOF {
			return nil, fmt.Errorf("cachebench: observation line %d: trailing JSON", line)
		}
		if record.Schema != ObservationSchema {
			return nil, fmt.Errorf("cachebench: observation line %d: schema %q", line, record.Schema)
		}
		if strings.TrimSpace(record.RequestID) == "" || seen[record.RequestID] {
			return nil, fmt.Errorf("cachebench: observation line %d: empty or duplicate request_id", line)
		}
		seen[record.RequestID] = true
		record.Usage = append(json.RawMessage(nil), record.Usage...)
		records = append(records, record)
	}
	if err := scanner.Err(); err != nil {
		return nil, fmt.Errorf("cachebench: read observations: %w", err)
	}
	if len(records) == 0 {
		return nil, errors.New("cachebench: no observation records")
	}
	if err := validateObservationRecords(records); err != nil {

View on GitHub (pinned to 27d5a3981a)

Solutions

  1. Ensure the observation writer emits exactly one JSON object per line with '\n' separators.
  2. Re-split any concatenated file so each object is on its own line.
  3. Check for missing final newlines when files are appended in place.
Defensive patterns

Strategy: validation

Validate before calling

func oneObjectPerLine(line []byte) error {
	dec := json.NewDecoder(bytes.NewReader(line))
	var rec ObservationRecord
	if err := dec.Decode(&rec); err != nil {
		return err
	}
	var trailing any
	if err := dec.Decode(&trailing); err != io.EOF {
		return fmt.Errorf("trailing JSON")
	}
	return nil
}

Prevention

When it happens

Trigger: An observation line like '{...}{...}' or '{...} trailing' — the first object decodes, then the follow-up decode does not yield io.EOF and the line number is reported.

Common situations: Concatenation scripts joining JSON objects without newlines; a writer missing a trailing newline so two records land on one line; shell heredocs collapsing lines.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@27d5a3981a (2026-08-15). Data as JSON: /api/errors/56cce490d13db7ef. Report an issue: GitHub.