Skip to content

cue-gen: generate Go from the design grammar with charly's own CUE-API generator, retiring gengotypes and the hand mirrors #271

Open

Description

@aitrawog

Problem

cue-gen builds Go types with cue exp gengotypes and a regex retag pass (internal/schemaretag). gengotypes cannot express the shapes the design schema is written in:

  • a key-tagged union becomes map[string]any;
  • an enum becomes a bare string, with no constants;
  • a {[#Name]: T} collection becomes an empty struct, so the data is lost;
  • a definition reached through a comprehension becomes an anonymous struct.

That is why about 11k lines in spec/spec mirror CUE by hand: union_types.go, the *_consts.go enums, charly_methods.go, the op strings, and the untyped params_json/result_json payloads. DESIGN.md forbids this (D-SCH-1, D-GEN-1/3/4).

Target (opencharly/opencharly#619)

DESIGN.md §6 now gives every schema shape exactly one Go mapping (C1–C7) and makes the generator charly's own, built on the CUE Go API at the pinned CUE version (D-GEN-7). Each shape has one spelling (D-GEN-8), and the design check enforces it.

  • Vocabulary tables: a typed word plus Valid(); a core or plugin-owned table also gets its constants.
  • Tag tables: a one-of struct with an exactly-one decoder.
  • Method tables: per-role interfaces, typed clients and servers, the request and reply one-ofs, and streams.
  • Derived shapes: generated from the table rows, never read back from an evaluated comprehension.

Feasibility, measured

A throw-away spike on cuelang.org/go v0.18.0-rc.1 read DESIGN.cue-shaped CUE through cue/load and the syntax tree. It emitted a Phase enum with constants and Valid(), a homogeneous Source one-of with Tag(), and KindRole/VerbRole interfaces with a stream parameter for log. The output passed go vet and go build.

Three findings the generator must honour:

  • A hidden table needs a package-qualified selector (cue.Hid(name, inst.ID())).
  • A reference's name does not survive a comprehension, so each derived shape is generated from its rows.
  • A payload named #X<Tag> collides with a generated arm struct of the same name. Homogeneous unions therefore map arms to their tag's value type (no arm structs), and heterogeneous unions generate X<Tag> arm structs.

Scope

  • Extend internal/schemagen along the lines internal/wiregen already follows.
  • Delete schemaretag and the hand mirrors it replaces.
  • Gate on a byte-for-byte regeneration, as TestGenReproducible does today.

This lands after the schema in spec/schema moves to the grammar. It is not part of #619, which changes DESIGN.* and design/ only.

Agent: design-grammar
Assisted-by: Claude Code Claude Opus 5.5 (analysed on a live system)

Activity

  1. aitrawog commented on Nov 9, 2026

    @aitrawog
    ContributorAuthor

    STATUS

    Direction 1 (the in-process generator for params) landed in #299 (head 47738b7067571a475f304f9072a29d1470dc6c01). The cue exp gengotypes shell-out is gone: params are generated by charly's own CUE-API generator over the same offline module resolution schemacompose uses, and an imported definition is emitted through the Go package that owns it.

    This does not close the issue. Still ahead from the scope here: the grammar-table mapping (vocabulary tables with Valid(), tag tables, method tables, derived shapes), the retirement of schemaretag and the hand-written mirrors, and the byte-for-byte gate over that new output. Next update when #299 merges.

    Assisted-by: DeepSeek Harness deepseek-v4.1-flash (fully tested and validated)

  2. aitrawog commented on Nov 9, 2026

    @aitrawog
    ContributorAuthor

    STATUS

    #299 merged (squash 9428017f5b182ccfd3f87efd92a3456b7158a797, tag v0.2026313.1428): Direction 1 landed. Params are now generated in-process by charly's CUE-API generator over the offline compose, and the cue exp gengotypes shell-out is gone.

    This issue stays open for the grammar-table mapping, the derived shapes, and the retirement of schemaretag and the hand mirrors.

    Assisted-by: DeepSeek Harness deepseek-v4.1-flash (fully tested and validated)

  3. aitrawog commented on Nov 17, 2026

    @aitrawog
    ContributorAuthor

    Measured: the pattern-keyed map collapse is silent, and one shape survives

    Found while porting DESIGN.cue's §8 capability surface into schema/capability.cue (the first place in the shipped schema that carries a {[<pattern>]: T} collection at all — grep -rn '\[#[A-Za-z]*\]:' schema/*.cue matched nothing before it).

    Isolated, cue exp gengotypes on this module's pinned CUE (v0.16.1):

    package p
    #Word: =~"^[a-z][a-z0-9_]*$"
    #A: {[#Word]: {count: int}}                 // regex key, non-empty value
    #B: {[#Word]: {}}                            // regex key, empty value
    #C: {[("nvidia" | "amd" | "intel")]: {}}     // literal-disjunction key
    #D: {[string]: int}                          // plain string key
    #E: {foo?: {[#Word]: {}}}
    $ cue exp gengotypes .
    $ cat cue_types_p_gen.go
    type Word string
    
    type A struct {
    }
    
    type B struct {
    }
    
    type C struct {
    }
    
    type D map[string]int64
    
    type E struct {
    	Foo struct {
    	} `json:"foo,omitempty"`
    }

    So: only [string] survives. A regex key (#Word, #Name, #EnvName) and a literal-disjunction key (or(_gpuWord)) both collapse the whole collection to struct{} — silently, with rc 0, no warning, and no difference between "collection of records" and "no data at all".

    Why this blocks the capability surface

    #Need/#Use/#Provide are exactly this shape ({[#EnvName]: {}}, {[or(_gpuWord)]: {}}, {[#Name]: #CountNeed}, …). On branch feat/capability-port the generated spec.Need/spec.Use/spec.Provide therefore carry the 21 type keys correctly (pinned by TestTheNeedUseProvideAreDerivedFromTheTable) but every per-type collection is struct{}:

    type Need struct {
    	Env struct {            // ← `{[#EnvName]: {}}`, the map is gone
    	} `yaml:"env,omitempty" json:"env,omitempty"`
    	...
    	Cpu struct {            // ← `{[or(_cpuWord)]: {count!}}`, `count` is gone
    	} `yaml:"cpu,omitempty" json:"cpu,omitempty"`

    A caller can express that it needs cpu, never what it needs — which is the whole contract. Five consuming units (D-CAP-*/D-HW-* wiring, the candy need:/use: migration, the solver) read these structs, so the port is blocked on this issue rather than worked around: flattening the keys to [string] would make the generator's gap permanent in the contract.

    What the fix has to generate

    Per C5/C7's own rule, a {[<pattern>]: T} is a map whose key type is the pattern's Valid()-checked word (type EnvName string already generates) and whose value type is T — i.e. map[EnvName]struct{…} for env, map[string]CandyNeedEntry-shaped values for the parameter rows. The key's regex must not defeat the map: validation belongs in the key type's Valid(), which is the same place every other vocabulary already puts it.

    The two other findings this port produced (the S3 gate's shape assumptions, and three generic _-names already spent on vendor detail elsewhere) are separate and are being handled on their own tracks.

    Agent: audit-protocol
    Assisted-by: DeepSeek Harness ollama-cloud/deepseek-v4.1-flash (syntax check only)

  4. added a commit that references this issue on Nov 17, 2026
    9254185
  5. added
    category:runtime
    type:design
    on Nov 23, 2026
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

    category:runtimetype:design

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions