Skip to content

refactor(json): centralize serialization through the JSON wrapper #616

Closed as not planned

Description

@Sinity

Problem

Polylogue has a canonical typed JSON layer in polylogue/lib/json.py, but many production and devtools paths still bypass it with direct stdlib json calls or direct orjson imports.

Evidence from current master:

  • polylogue/lib/json.py defines JSONValue, JSONDocument, dumps, dumps_bytes, loads, and validation/coercion helpers backed by orjson with stdlib fallback.
  • rg "json\.(loads|dumps|load|dump)\(" polylogue devtools finds many direct call sites.
  • Examples of generic bypasses:
    • devtools JSON CLI output repeatedly uses stdlib json.dump / json.dumps.
    • proof hash and evidence paths repeatedly configure sort_keys, separators, and ensure_ascii locally.
    • polylogue/browser_capture/server.py reads request JSON through stdlib json.loads while using dumps_bytes for responses.
    • polylogue/browser_capture/receiver.py imports orjson directly for pretty sorted output.

Some direct stdlib JSON use may be legitimate for compatibility-sensitive formats. The current code does not mark those exceptions, so generic machine-output and artifact code keeps reinventing formatting/parsing policy.

Impact

The repository already pays for the orjson dependency and typed JSON contracts, but serialization behavior remains scattered. This adds boilerplate, makes stable hash/pretty-output behavior harder to reason about, and weakens the value of the central JSON abstraction.

Acceptance criteria

  • Define a policy for when code should use polylogue.lib.json versus direct stdlib json.
  • Add compact helper functions for common forms, for example:
    • pretty JSON text/bytes
    • canonical hash bytes
    • JSON file read/write with typed validation
    • JSON CLI output to stdout
  • Migrate generic production/devtools call sites to the wrapper.
  • Leave explicit comments or local helper names for compatibility-sensitive direct stdlib JSON uses.
  • Add a lint/check or focused test that prevents new generic direct JSON bypasses outside the allowlist.
  • Run focused JSON/proof/devtools tests and devtools verify --quick.

Ref #578

Activity

  1. Sinity commented on May 1, 2026

    @Sinity
    OwnerAuthor

    Additional scope from the compactness audit: centralizing JSON serialization should not mean keeping every recursive JSON guard as a permanent mini-framework.

    polylogue/lib/json.py has two different behaviors today:

    • strict helpers such as require_json_value() and require_json_document() fail when a producer violates the JSON contract;
    • forgiving helpers such as json_document() and json_document_list() silently coerce invalid values to {} or drop invalid list items.

    The strict serialization wrapper is still useful because the project needs canonical dumping, Decimal handling, and non-finite JSON rejection. But at typed boundaries such as manifests, schema packages, proof attributes, provider capability payloads, and browser-capture payloads, Pydantic models or TypeAdapter validation should replace silent {} coercion where strictness matters.

    Acceptance should include an audit of current json_document() / json_document_list() call sites and a rule: parser-shape probing may use forgiving coercion, but repo-control manifests and machine-output payload construction should fail loudly on invalid JSON-compatible data.

    Ref #629
    Ref #578

  2. Sinity commented on May 1, 2026

    @Sinity
    OwnerAuthor

    Superseded by #614. I consolidated this finding into the composite issue so the work can be handled as one coherent slice instead of as a tiny standalone ticket. The original evidence remains available in this issue history.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions