Skip to content

JSON reload turns every authored integer into a float64, and the codecs disagree #711

Closed

Description

@David-Noble-at-work

Blocks: #709

JSON has one number type, so unmarshalling into an any slot cannot say whether 420 was written as an
integer or a float. LoadGraph (pkg/op/graph.go:265) uses plain json.Unmarshal, so every number in a
reloaded graph comes back as float64
— an authored 0o644 reaches an fs.FileMode parameter as a float.

Today that is invisible, because op.Convert truncates it back. #709 removes the truncation, and this
surfaces immediately:

file.copy: param mode: float64 value is neither assignable nor convertible to fs.FileMode

The codecs already disagree

gopkg.in/yaml.v3 decodes an integer as int. JSON decodes it as float64. The same graph reloads to
different in-memory Go values depending on which codec wrote it
— which is a single-codec problem
independent of #709.

Why float64 is not merely inelegant

float64 cannot represent every int64 exactly. A sufficiently large integer in a saved graph is already
silently corrupted on reload; truncation just makes the corruption look like a clean number.

The rule

Convert from the value we serialize to the type of the field it fills.

Preserve the literal, and let the target type decide how to read it. json.Decoder.UseNumber() yields
json.Number, which keeps the text; Convert gains a step that parses it against the field's type — an
fs.FileMode wants an integer, a timeout wants a float, and the text answers either without a lossy
float64 in between. Range is enforced by ParseInt against the target's bit size.

The precedent is already in the tree, deliberate and commented — pkg/result/jq_filter.go:119:

decoder.UseNumber()
// Numbers come out as int64 (UseNumber + Int64 conversion).

The part that makes this its own issue

A prototype of exactly that produced:

op.LoadGraph: checksum mismatch:
  document   "sha256:8e5bf1a16451fbcf4881a2fc2ec4a7d87856d0a0ad82907a1e324e08662043b4"
  recomputed "sha256:39ca2d67f48706afb358729954e597f342b93a9f7a40fd5999d79f17b525d447"

Changing the decode changes the in-memory representation, which changes the recomputed checksum — so this
reaches graph identity, not just deserialization. That needs deciding on its own terms, against the existing
rulings about what may and may not vary across formats, rather than as a side effect of a conversion fix.

Sequencing

This lands before #709. #709's category rule is red without it, because reload legitimately produces
floats today. The prototype has been backed out of the #709 branch, leaving that branch purely about the
conversion rule and red until this lands.

Acceptance

  • A reloaded graph's integer parameters are integers, whichever codec wrote it
  • 0o644 reaches an fs.FileMode without passing through float64
  • A value too large for its field is an error, not a truncation
  • Graph identity is decided deliberately: either the checksum is unchanged, or the change is ruled on
  • A test pins the JSON/YAML asymmetry closed

Plan and design documents

Kind Document
Plan docs/plans/number-fidelity.md

Activity

  1. David-Noble-at-work commented on Aug 27, 2026

    @David-Noble-at-work
    ContributorAuthor

    Scope narrowed: parameters with a declared type only.

    A parameter declared any is excluded, and not for effort — because no load-side fix can reach it. json.Marshal(float64(42)) emits 42, so a float in an any slot loses its type at save, before anything reloads. UseNumber preserves the literal faithfully; the literal simply no longer says "float". yaml.v3 does the same, so it is not JSON-specific.

    Ruled: for an any slot the document stores the type — with no declared type at either end, the document is the only place the information can live. Filed as #712. It changes the document format and does not block this plan; any-typed slots keep today's behavior until #712 is decided.

    That boundary is what makes this plan's rule work: a declared type says how to read the number, and the preserved literal answers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions