Repository navigation
feat(kernel): encode and view bounded structured object trees - #2756
Conversation
Add native structured Value/Map owners, lazy string-key map views, strict JSON conversion, and narrow dtogen composition. Prove bounded admission and detached persistence ownership through generated envelopes and real History storage. Co-authored-by: Mecatl <[email protected]>
Design summaryThis PR establishes structured values and read-only map navigation for future kernel tool arguments, without coupling those contracts to execution code. Why a dedicated value type?Some data has a shape known only at runtime. Why separate authoring from inspection?Callers can construct ordinary mutable values. Admission validates and detaches their backing before publishing read-only views. Borrowed views support inexpensive inspection when the caller can keep backing stable. Lazy map and array navigation avoids copying unrelated children, and generated DTO accessors reuse the same map-view runtime. Why marshal JSON through views?JSON encoding only needs to read data, so it uses the same navigation contract as other readers instead of depending on private mutable storage. Authoring values borrow views without cloning the tree. One shared traversal preserves depth and cycle checks across every descendant, while standard-library escaping and exact integer formatting keep output predictable. Scope and confidenceProduction execution and existing persisted encodings are unchanged. The persistence proof covers real History Append/Read; typed Session Submit/Load belongs with the first structured-value record. Focused value, views, and persistence-fixture race tests passed for the final refactor, including ownership, malformed values, integer boundaries, and depth/cycle behavior. Incremental review found no outstanding issues. Detailed verification and compatibility notes are in the PR description. |
Add all-root JSON parsing and native JSON marshaling for owned values and borrowed views. Use integer-backed kind discriminators with readable diagnostics, preserve bounded traversal, and expand persistence and generator regression coverage. Co-authored-by: Mecatl <[email protected]>
Keep JSON encoding on the read-only navigation contract instead of coupling it to mutable authoring storage. Borrow owners without copying descendants and restore generic MapView delegation while preserving shared depth accounting and output semantics. Co-authored-by: Mecatl <[email protected]>
Why this belongs in the kernel
Future tool arguments need structured data whose shape is determined at runtime, while persistence DTOs need typed ownership and validation. A
map[string]anybridge would blur those contracts and risk losing integer precision through floating-point conversion. This PR adds a closedvalue.Valuetree and ordinary mutablevalue.Mapauthoring storage, with bounded CBOR encoding and read-only views after admission.Map inspection should follow the same ownership model as slices and byte leaves. The reusable
views.MapViewadapts entries lazily, so selecting one child does not clone siblings or materialize a second map.dtogenuses it for supported scalar maps and pointer-valued, context-free DTO maps. Value-specific map views delegate to that same runtime rather than maintaining another implementation of map navigation.This is a prerequisite for #2663, separate from paused #2755. It establishes the data contract before execution code depends on it. Production entry points and opaque
RequestedCallbytes remain unchanged; typed Session Submit/Load coverage belongs with the first real structured-value record.Why JSON encoding reads views
JSON output is an inspection operation. It should need only the read-only navigation contract available to other callers, rather than reaching behind views into private mutable authoring storage.
ValueandMapborrow views in O(1); all four marshalers share private traversal over those views. This avoids cloning an admitted tree merely to inspect it and keeps encoding independent of the owner representation.The shared traversal carries one local depth counter across array and object children. Calling each child's public marshaler would restart that counter, weakening cycle/depth protection and repeatedly serializing subtrees. Lazy accessors avoid a whole-tree conversion; a temporary sorted key slice provides deterministic object output. Standard-library string escaping and decimal integer formatting retain familiar JSON behavior without a serializer framework.
JSON is for interoperability and inspection. Canonical CBOR remains the content-identity representation. Input parsing likewise uses the standard JSON decoder's Unicode replacement behavior instead of maintaining a separate Unicode scanner, while rejecting decoded-key collisions and retaining strict native Value/CBOR UTF-8 validation.
Contracts and deliberate limits
[-2^63, 2^64-1], arrays, and string-keyed objects. Floats, byte strings, and CID links are outside this primitive.MapViewencode as{}; explicitly nil arrays encode as[]. ZeroValue/Viewis invalid, distinct from null. Integers remain exact, and object keys sort lexically.API compatibility
No production RPC/HTTP semantics, wire major/feature, existing record schema/version, dependency pin, or canonical CBOR-byte changes. The latest JSON-over-views refactor preserves the public API and external behavior; no generated declarations or API snapshots change in that commit.
Across the full PR, generic JSON parsing and the four marshalers are additive isolated-kernel APIs. Both public Kind enums use integer backing with readable
Stringmethods, a Go source-type change explicitly approved for these new, unmerged experimental APIs.ParseJSONObjectintentionally adopts standard-library source Unicode replacement; duplicate decoded keys still reject. Required DTO-pointer/null semantics remain unchanged. Earlier API additions have regenerated snapshots; kernel guidance waives changelog bookkeeping.Verification and review
Latest refactor:
c27c447c3e8596d15823da7edf6e117cbee951c8, reviewed against771462b2e7831eb2ecd8283de189999588f1ac72.Passed locally:
This covers all four receiver forms, pointers/map elements/struct fields, exact integers, lexical keys and escaping, depth 128/129 and cycles, invalid native data, independent output buffers, and admitted plus real History Append/Read-loaded views with nonempty siblings. The refactor extends malformed-representation coverage through nested map/array paths after a valid sibling, checking nil output across all four receivers. Formatting and
git diff --checkalso passed.Incremental Spec, Standards, Test adequacy, Security, Architecture, Reuse, and Duplication review found no outstanding findings or reviewer failures. The current commit changes only six files in
engine/kernel/value. Full repository, dtogen, generation, and API-update gates were deliberately not repeated for this implementation-only change.Earlier prerequisite/amendment evidence: at
771462b2e7831eb2ecd8283de189999588f1ac72, focused kernel/history races, dtogen owner/bootstrap/signature/freshness checks, generation/API/architecture checks, standalone engine validation, lint, docs, full test/race, bounded JSON fuzzing, and the offline demo passed. Those results are historical evidence, not fresh certification of the latest refactor. Workspace-race was cancelled and is not claimed as a pass. The storage proof is History Append/Read, not typed Session Submit/Load.Design summary
The design summary explains the ownership boundaries and why JSON encoding reads views.