Skip to content

feat(kernel): encode and view bounded structured object trees - #2756

Merged
jbeda merged 3 commits into
mainfrom
impl/kernel-map-values
Nov 10, 2026
Merged

jbeda merged 3 commits into
mainfrom
impl/kernel-map-values

Conversation

@jbeda

@jbeda jbeda commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

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]any bridge would blur those contracts and risk losing integer precision through floating-point conversion. This PR adds a closed value.Value tree and ordinary mutable value.Map authoring 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.MapView adapts entries lazily, so selecting one child does not clone siblings or materialize a second map. dtogen uses 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 RequestedCall bytes 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. Value and Map borrow 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

  • The closed tree supports null, booleans, UTF-8 text, exact integers in [-2^63, 2^64-1], arrays, and string-keyed objects. Floats, byte strings, and CID links are outside this primitive.
  • Authoring remains ordinary mutable DTO data. Borrowing requires stable backing; admission validates and detaches it before publishing immutable navigation. Repeated shared subtrees have per-occurrence value semantics.
  • Fixed local depth 128 protects the primitive independently of enclosing wire depth and operational codec limits. JSON encoding rejects excessive depth, cycles, invalid representations, and invalid native UTF-8 with errors and nil direct output. Leaf/empty containers at depth 128 remain valid.
  • Nil/empty maps and zero MapView encode as {}; explicitly nil arrays encode as []. Zero Value/View is invalid, distinct from null. Integers remain exact, and object keys sort lexically.
  • JSON input applies explicit resource limits before and during retained construction. Debug marshaling has no operational byte/node/breadth admission policy; it retains the fixed depth bound.
  • Automatic DTO-map views are limited to supported scalars and pointer-valued context-free DTOs. By-value wrappers and context-bearing children still require handwritten navigation to preserve ownership and identity scope.

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 String methods, a Go source-type change explicitly approved for these new, unmerged experimental APIs. ParseJSONObject intentionally 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 against 771462b2e7831eb2ecd8283de189999588f1ac72.

Passed locally:

cd engine && go test -race ./kernel/value ./kernel/views ./kernel/internal/valuefixture

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 --check also 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.

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]>
@jbeda

jbeda commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Design summary

This 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. value.Value represents that data while preserving exact integers and explicit null, array, and object semantics. A map[string]any bridge would weaken those guarantees. Typed persistence DTOs remain responsible for record structure; canonical CBOR remains the content-identity representation.

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 confidence

Production 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.

jbeda and others added 2 commits November 9, 2026 15:23
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]>
@jbeda
jbeda enabled auto-merge (squash) November 9, 2026 23:46
@jbeda
jbeda merged commit 7e791b1 into main Nov 10, 2026
46 checks passed
@jbeda
jbeda deleted the impl/kernel-map-values branch November 10, 2026 00:04
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Nov 18, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant