Skip to content

Reduce session immutable-view boilerplate with reusable view support #2747

Closed

Description

@jbeda

Outcome

Reduce the hand-maintained boilerplate required to expose complete, deeply immutable session-record hierarchies, while preserving allocation-free scalar inspection and explicit owned exports.

Parent: #2663. This phase is the next slice after passive model interactions, merged in PR #2746 at 8c1276a7da77eca4ce31f3e7c44023464aeb0994. It precedes passive tool disposition and further record-family expansion. Joe selected session data-object boilerplate reduction as the next focus. Investigation and approach selection remain to be done; no representation, generator, dependency or implementation plan is approved yet.

Problem

The isolated session package maintains mutable authoring values, persistence DTOs, and hand-authored immutable view companions. As records gain nested evidence, usage, recipes, content, and collections, view accessors and ownership tests add substantial repetitive code. Extension-owned record types will amplify this burden.

Merged PR #2746 replaced aggregate-cloning getters with complete hand-authored views for usage/evidence, opaque metadata, request recipes, ending capture evidence, and response-item call data. That is the behavioral baseline for this investigation. Its complete scalar traversal benchmark reports 0 B/op and 0 allocs/op through both Submit and storage-backed Load using indexed records and compiler-visible nested iteration; byte exports are separately measured intentional copies. A zero-allocation traversal that skips nested fields is not proof of complete read coverage, and these direct-path results are not a claim about opaque iterator escape costs.

Preferred direction and flexibility

  • Prefer immutable views directly over persistence DTOs if appropriate public semantics and package boundaries can be preserved. This means read-only access to DTO-backed storage, not exposing mutable DTOs or their aliases.
  • Hand-defined session-facing structs with reusable/generated view companions are also acceptable. Adapt experimental Go contracts where justified instead of reproducing every existing accessor mechanically.
  • Keep explicit semantic adapters where required: persistence-local ordinals need enclosing history/run scope to become public occurrence identities. Do not make storage layout an accidental public identity contract.
  • Evaluate established community tooling and designs first, but direct adoption is not required. A small focused local implementation or licensed adaptation is an acceptable outcome if it fits better than adding a large module dependency; no approach is preselected.

Investigation

Evaluate Tailscale's maintained BSD-3-Clause types/views library, cmd/viewer, related cmd/cloner, and shared util/codegen as concrete reference designs, plus any credible alternatives discovered. Distinguish generic read-only containers from automatic deep struct views; persistent collection libraries alone do not make their elements immutable.

The operator is concerned that these relatively small facilities may not justify depending on the large tailscale.com module. Compare direct adoption with a focused local reimplementation or licensed adaptation. Measure actual tool/runtime transitive dependencies, module download/build cost, and public API coupling separately; importing a package does not link an entire repository into the binary. Preserve required copyright/license notices for any copied or adapted code.

Investigate whether reusable type analysis and emission support can be factored out of existing cborgen machinery and shared by codec, view, and clone generation, analogous to Tailscale's util/codegen. Look for concrete overlap rather than bundling all generators together or building a speculative framework. Keep schema-specific bounded codec behavior separate from generic Go type/code-generation support. Reimplementation, factoring, and a shared helper package are options to evaluate, not implementation decisions.

Sources:

Use a representative vertical slice: response → usage → optional counters → raw provider evidence, plus a collection, a by-value nested DTO, and a scoped occurrence identity. Compare:

  1. DTO-backed generated/reusable views with thin session-specific semantic adapters.
  2. Hand-defined session-facing structs with generated/reusable view companions.
  3. The hand-authored baseline, including whether small immutable public leaf values can eliminate unnecessary mutable-value/view pairs.

Assess actual compatibility rather than assuming drop-in adoption: generated-code package placement, by-value DTO collections, zero-view behavior, optional presence, custom identity reconstruction, cloning signatures, automatic JSON support, runtime dependencies, licensing, maintenance, and public dependency exposure. Measure hand-maintained code reduction separately from generated output size. Check generator failure behavior for unsupported shapes and new fields; do not silently emit unsafe accessors.

Acceptance criteria

  • A reviewed recommendation and bounded implementation plan explain the chosen representation, dependency/tooling trade-offs, and rejected alternatives. If no suitable approach earns its cost, document that result rather than introducing a framework by default.
  • Deep scalar and presence inspection does not clone sibling fields or payload bytes and supports allocation-free indexed/direct traversal. Iterator escape costs are described separately.
  • No mutable pointers, byte slices, collection elements, or nested aliases escape retained immutable backing. Owned byte/aggregate exports are explicit and detached; input ownership is established before retention.
  • Preserve absent versus explicit zero/empty distinctions, canonical persistence bytes, bounded decoding, session-scoped identities, lifecycle/admission semantics, snapshot pinning, and frozen retry behavior. Any proposed stored-data changes require separate explicit review, not incidental regeneration.
  • If adopted, demonstrate the approach replacing existing code and deleting meaningful hand-maintained boilerplate, with ownership and allocation regression coverage over the complete nested slice.
  • Keep extension feasibility explicit: extension authors should be able to define their own types and obtain safe views without teaching a central generator every record kind. Identify open package/registration/ownership questions; do not build an extension registry or redesign lifecycle interpretation here.
  • Record established view-authoring discipline in kernel AGENTS.md, public read/export guarantees in session/doc.go, and precise ownership/zero behavior beside affected declarations. Update generated API snapshots for approved experimental Go contract changes; no production integration is implied.

Boundaries

This is a separate phase, not extra scope for #2746. No persistence-format migration, production wiring, new lifecycle facts, extension registry, or speculative general-purpose framework. The parent issue remains open for passive tools, request reconstruction, accounting, and its other completion criteria.

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