Skip to content

feat(golang): stashgen stores a UUID field - #1997

Open
coderdan wants to merge 9 commits into
nextfrom
feat/stashgen-uuid
Open

coderdan wants to merge 9 commits into
nextfrom
feat/stashgen-uuid

Conversation

@coderdan

@coderdan coderdan commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

stashgen now generates a UUID field. A Go field of type [16]byte, or of a type whose declaration is an array of 16 bytes (such as github.com/google/uuid.UUID), is sealed as the engine's uuid kind. It can be queried by equality and reads back as the field's own type. A pointer to one is an optional field. This is the Go part of #1821.

Closes #1994. Refs #1821.

Changes

  1. Mapping: a field is a UUID when its type, after following aliases, is one of three things:

    • [16]byte itself (that is, [16]uint8);
    • a defined type whose own declaration is an array of 16 bytes, such as type ID [16]byte, as github.com/google/uuid.UUID is declared;
    • uuid.UUID or vcvalue.UUID, recognised by name.

    go/types does not record what a defined type was declared over, so stashgen/uuid.go reads the declaration from source. Everything else whose underlying type is [16]byte is refused with a message that says why, including type Balance encrypt.Int128 and the same over Uint128. Arrays of another length, and [16]T for a type T defined over byte, are refused as before. No existing field changes kind: a sealed [16]byte was refused until now. Every golden file is byte-identical, and go generate ./... changes no committed file.

  2. Indexes: equality is allowed. match, ore and ope are refused at generation time by the engine, as for other kinds. enginetest.Static agrees. A UUID has a fixed width, so a nil pointer is a sealed null by default, with EqNull. null=sql also works.

  3. Generated code:

    • Declared with EncryptIndex("id", gensupport.UUID, encrypt.Equality).
    • Written as it is; gensupport turns it into the engine's UUID.
    • Read with gensupport.Get[[16]byte] and converted to a named type, or with GetPtr for a pointer.
    • gensupport.convert reads a vcvalue.UUID into a *[16]byte directly.
  4. Docs: encrypt/doc.go, encrypt/README.md, cmd/stashgen/README.md and the CHANGELOG. The CHANGELOG's note that stashgen does not map a UUID is removed. Two statements that were already out of date are also corrected: the README said a sealed field cannot be a pointer, and the package docs listed bytes among the types that take an order index.

Verification

  • New tests:
    • TestGolden/sessions: a named type with equality, plain [16]byte, and two nullable fields, one with a sealed null and one with null=sql. The test also compiles the generated file.
    • Refusals: [8]byte, [16]octet, and match, ore and ope on a UUID. These are checked with both the fake engine and the real one, which agree on five UUID cases.
    • TestGetReadsAUUIDAsSixteenBytes.
    • TestGeneratedCodeStoresQueriesAndReadsAUUID, with the deterministic offline client. It covers store, equality terms, Eq, EqNull, Encrypt, Decrypt back to the named type and to [16]byte with nil kept, and a one-column update. It fails without the convert change.
  • With all five guests built: go build, go vet, gofmt -l and go test ./... pass in every module, with STACK_CONFORMANCE_REQUIRED=1. The only failures are accounts, contacts and documents in stashgen/testdata/cases, which fail on next too: their generated code exists only as .golden files.
  • go.mod and go.sum are unchanged. github.com/google/uuid is not added; the tests use a local [16]byte type.

Review notes

  • A type declared over a UUID type is refused (type MyID uuid.UUID). Accepting it means following the declaration chain, and refusing any chain that reaches the 128-bit integer types. Should it be supported?
  • A UUID type from another package is read from the source file go/packages records. A build with -trimpath could hide that file, and such a type would then be refused with "could not be read". uuid.UUID and vcvalue.UUID are known by name, so they are not affected. This was not tested under -trimpath.
  • [16]MyByte is refused. The element type must be exactly byte, because the write path converts only types whose underlying type is [16]byte.
  • No cross-language check of the UUID equality term's bytes. No fixture compares the bytes from Go against Rust, as value_kinds.json does for other kinds. The tests prove a round trip and correct equality only.
  • Conflicts with stage 3. It touches gensupport and stashgen, which stage 3 (moving vcvalue) also touches. Whichever merges second needs a small import update.

The engine, the Go guest and gensupport can seal a UUID, but stashgen
had no Go type that mapped to the uuid kind, so a Go program could not
store one through generated code. A [16]byte field was refused as a
composite, and the only way to store a UUID was as text or bytes, which
Rust and TypeScript would not read as a UUID.

stashgen now classifies [16]byte, and any type defined over [16]byte
(github.com/google/uuid.UUID, say), as a UUID and declares the field
with gensupport.UUID. No field that generated before changes kind: a
sealed [16]byte was refused until now, and an array of another length
or of a type defined over byte is still a slice. The engine decides the
indexes, as for every kind: equality is accepted, and an order or match
index is refused when the code is generated. A pointer to one stores nil
as a sealed null by default, since a UUID has a fixed width.

Generated code reads the value as [16]byte and converts it to the
field's own type, as it does for a type defined over a scalar, so it
needs no reflection. gensupport's Get reads a vcvalue.UUID as [16]byte
for that; any other value still goes through the array path it took
before.
…r index

The encrypt README said a sealed field cannot be a pointer, so an
optional date had no sealed form. Optional fields declared over a
pointer, with `null=sealed` or `null=sql`, exist now. The package docs
listed bytes among the values ORE and OPE apply to; the engine refuses
an order index on bytes, as on a UUID.
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI; Enforced overrides: Organization
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 546b1180-5f29-4206-8c82-08f91512ad1c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

@changeset-bot

changeset-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 664ad4b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderdan coderdan left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review (/code-review medium) of b692668. Three findings, checked against the code: one important and one nit inline, and one outside this PR's diff, below.

Outside the diff (nit): the engine's SealedNullType refusal (packages/stack-encrypt/src/refusal.rs) and the matching doc comment in dynamic/record.rs list the fixed-width types as "a bool, a number, a date, a timestamp or a decimal". This PR documents a UUID as fixed width and seals a nil UUID by default, so the engine's text should name a UUID too. I will add it in the pull request for #1985, which changes refusal.rs.

I will reply on each thread with what I changed.

Comment thread languages/golang/stashgen/read.go Outdated
g.Kind, g.Elem = KindSlice, &elem
}
case *types.Array:
if uuidArray(u) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A type defined over encrypt.Int128 or encrypt.Uint128 is now stored as a UUID instead of being refused. vitaminc's vcvalue declares both as [16]byte (type Int128 [16]byte). valueKind matches only the named types themselves, so for type Balance encrypt.Int128, classify falls through to the underlying array, and uuidArray returns true. valueKind's own comment says such a type is "classified by its underlying type and refused as a struct or array".

Failure scenario: type Balance encrypt.Int128 with a field tagged encrypt,index=equality was refused before. Now it generates EncryptIndex("amount", gensupport.UUID, ...), and the 128-bit integer is sealed under the uuid type. Rust and TypeScript read it back as a UUID, and adding ore later fails.

Severity: important

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 11c794f. A field is a UUID when its type, after following aliases, is one of three things: [16]byte itself; a defined type whose own declaration is an array of 16 bytes (type ID [16]byte, [16]uint8, or a constant length of 16), as github.com/google/uuid.UUID is declared; or uuid.UUID or vcvalue.UUID, recognised by name. go/types does not record what a defined type was declared over, so stashgen/uuid.go reads the declaration from source, from pkg.Syntax or the file go/packages records for another package. Everything else with an underlying [16]byte is refused with a message that says why: type Balance encrypt.Int128, the same over Uint128 or the vcvalue types, an alias of one, a pointer to one, and a type whose declaration cannot be read. New tests cover each, including a type from another package and the vcvalue integers themselves, which keep their integer kinds.

case *[16]byte:
// A UUID field opens as a vcvalue.UUID. Generated code reads it as
// [16]byte and converts it to the field's own type (uuid.UUID, say).
// Any other value is read as an array, as before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment describes past behaviour instead of the rule: "Any other value is read as an array, as before." The same wording is in TestGetReadsAUUIDAsSixteenBytes's comment. Doc comments state the rule and never narrate history.

Failure scenario: A reader cannot tell what "as before" refers to once this change has merged.

Severity: nit

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 09fb4f1. The doc comment and the test's comment now state the rule: any other value is read as an array of sixteen numbers.

vcvalue declares Int128 and Uint128 as [16]byte, so a type declared over
encrypt.Int128 (type Balance encrypt.Int128) has the same underlying type
as a UUID. stashgen looked only at the underlying type, and so generated
such a field as a UUID. Before UUIDs, it refused the field, and it must
still refuse it: the value is an integer, and sealing it as a UUID would
store the wrong kind.

go/types does not record what a defined type was declared over, so
stashgen now reads the type's declaration from source. A type is a UUID
when its declaration is an array of 16 bytes (type ID [16]byte, as
github.com/google/uuid.UUID is), and for that one type and vcvalue.UUID
also by name. A type of the struct's own package is read from the loaded
syntax; a type of another package from the file its position names. A
type declared over another named type is refused, even over a UUID type,
and so is one whose declaration cannot be read. The refusal names the
type and says what a UUID field can be.
The comments compared the behaviour with an earlier one. They now state
the rule: a value that is not a UUID is read as an array of sixteen
numbers.
@coderdan
coderdan marked this pull request as ready for review November 27, 2026 13:47
@coderdan
coderdan requested a review from a team as a code owner November 27, 2026 13:47

@cipherstash-agent cipherstash-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review attestation

Reviewed 09fb4f1d · posted from Claude Code (scheduled routine) · claude-opus-5-5 · effort medium
Sources: code-review (medium) · claude-opus-5-5; mattpocock-skills:code-review standards and spec agents · model unknown (general-purpose subagents); review-cross-check verify agents · model unknown
Skills: code-review, mattpocock-skills:code-review, company-skills:rust-security (loaded; not applicable, the PR changes no Rust), company-skills:review-cross-check, company-skills:review-post

Findings (6): 1 major, 4 minor, 1 nit — 4 inline, 2 below.

  • languages/golang/stashgen/emit.go:224 (outside diff) nit kindExpr's comment lists the special Go-type-to-kind mappings but not the one this PR adds: [16]byte, or a type declared as one, is UUID.
  • languages/golang/stashgen/read.go:970 nit uuidArray(u) && isUUID(t) repeats the array test isUUID makes at uuid.go:46; harmless.

Checked and dropped: encrypt.KeysetID now classifying as a UUID is correct (it is documented as a UUID); the CHANGELOG code span split across lines renders as one span; TestGeneratedCodeStoresQueriesAndReadsAUUID cannot skip in CI (scripts/go-binding-test.sh fails without the deterministic guest).
Open questions, already raised in the PR's review notes: whether type MyID uuid.UUID should be accepted, and whether the README's pointer-field correction belongs in this PR (it is disclosed and accurate).
Not run here: the guests were not built, so encrypt/uuid_test.go skipped locally and device_stash.go was not regenerated.


Generated by Claude Code

Comment thread languages/golang/stashgen/uuid.go Outdated
Comment on lines +83 to +89
pos := r.pkg.Fset.Position(obj.Pos())
if !pos.IsValid() || pos.Filename == "" {
return nil
}
file, ok := r.declFiles[pos.Filename]
if !ok {
parsed, err := parser.ParseFile(token.NewFileSet(), pos.Filename, nil, parser.SkipObjectResolution)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

major typeSpec finds an imported type's declaration through the file name in its export-data position, which is not always a real path: under GOFLAGS=-trimpath it is module-relative (example.com/app/crm/ids.go), and a //line directive replaces it with the directive's file. Then ParseFile fails and a plain type Ref [16]byte from a sibling package (or gofrs/uuid, or encrypt.KeysetID) is refused; GOFLAGS=-trimpath go test -run TestUUIDTypes ./stashgen/ fails on "a UUID type of another package". Load the imported package by import path (packages.Load with NeedName|NeedFiles, same Dir, cached per path) and search its GoFiles instead of using positions; PositionFor(pos, false) does not help, as the export data already holds the adjusted name.

Source: code-review (medium), mattpocock-skills:code-review spec agent; verified by review-cross-check


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6605929. typeSpec no longer uses the export-data position. It loads a type's own package by import path, with packages.Load(NeedName|NeedFiles) in the same directory and environment as the main load, and searches that package's GoFiles. Each import path is loaded once. A new test, TestUUIDTypesOfAnotherPackageUnderTrimpath, sets GOFLAGS=-trimpath. It checks that crm.Ref from a sibling package is accepted and that crm.Amount, declared over encrypt.Int128, is refused. The test fails on the previous code and passes now, and so does GOFLAGS=-trimpath go test -run TestUUIDTypes ./stashgen/.

Comment thread languages/golang/stashgen/read.go Outdated
if sixteen := sixteenBytes(cf.typ); sixteen != nil {
// [16]byte underneath, and not a UUID: a type declared
// over another type, such as encrypt.Int128.
return fieldErr(typeName, cf.goName, "a UUID field is [16]byte, a type whose declaration is an array of 16 bytes (type ID [16]byte), or github.com/google/uuid.UUID; %s is declared over another type, or its declaration could not be read, so it seals only as part of an opaque struct", r.typeExpr(sixteen))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor One message covers two causes, so a plain type Ref [16]byte whose source could not be read is told it "is declared over another type", which is false. It also lists github.com/google/uuid.UUID but not vcvalue.UUID, which knownUUIDs accepts too. Have isUUID report an unreadable declaration separately and give it its own message (naming -trimpath as a likely cause); the same list is in declaration.go:287 and encrypt/doc.go:128.

Source: mattpocock-skills:code-review standards and spec agents


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6605929. isUUID is built on uuidVerdict, which tells "declared over another type" apart from "declaration could not be read". Each now has its own message. The second names likely causes: a module missing from the module cache, a vendor directory without it, or a type not declared at package level. It does not name -trimpath, which no longer causes it. One uuidTypes constant now gives the list of UUID types, with vcvalue.UUID. declaration.go, encrypt/doc.go, both READMEs and the CHANGELOG say the same.

Comment on lines +18 to +19
// timestamp, vcvalue.Date, vcvalue.Decimal, vcvalue.UUID for a UUID, or a
// vcvalue.Object for a composite — and the struct's type is that type, or in the same family and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor The paragraph's rule ("the struct's type is that type, or in the same family and narrower", and "a time, date, decimal or 128-bit integer is read only into its own type, which Get matches") does not describe the new path: Get[[16]byte] misses a vcvalue.UUID and convert reads it into any *[16]byte. Add a sentence such as "A UUID opens as a vcvalue.UUID and is read into [16]byte, which generated code converts to the field's type", and rewrap line 19 (99 columns).

Source: mattpocock-skills:code-review standards agent


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b050cae. The paragraph is rewrapped, and now says a UUID opens as a vcvalue.UUID and is read into [16]byte, which generated code converts to the field's own type.

Comment on lines +19 to +22
var knownUUIDs = map[[2]string]bool{
{"github.com/google/uuid", "UUID"}: true,
{vcvaluePath, "UUID"}: true,
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor knownUUIDs is a second table of types known by name beside valueKind (read.go:1004-1044), so the next known type means editing two tables. Consider returning KindUUID from valueKind for these two names, which also follows aliases already. Judgement call.

Source: mattpocock-skills:code-review standards agent


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changed. Merging knownUUIDs into valueKind would change behaviour, not just move code. valueKind is the set of types the codec writes by their exact type, and other checks treat those as one value. notDocument calls it, so a uuid.UUID field with encrypt_into=JsonSearch would start being refused as "one value, not a document". classify would also need a special case to set Basic to [16]byte for these names. The comment on knownUUIDs now gives this reasoning. The two nits from the review body are fixed too: kindExpr's comment names the UUID mapping (b050cae), and classify calls isUUID alone, without the repeated array test (6605929).

…ource

stashgen decides whether a [16]byte type is a UUID from its declaration,
and found the declaration of a type in another package through the file
name in the position go/packages records for it. That name is not always
a file on disk: under -trimpath it is a module path, and a //line
directive replaces it. Then a plain type Ref [16]byte from a sibling
package, or gofrs/uuid's UUID, was refused.

stashgen now lists the imported package's Go files with go/packages, by
import path and from the directory the package was loaded from, and
parses them, once per package. The position is no longer used.

The refusal of a [16]byte type that is not a UUID now has two messages:
one for a type declared over another type, and one for a type whose
declaration could not be read, which names the likely causes. Every list
of the types a UUID field takes now names vcvalue.UUID, which stashgen
accepts by name, beside github.com/google/uuid.UUID. The array check
that isUUID already makes is no longer repeated in classify.
The paragraph on convert did not describe how a UUID is read, and the
comment on kindExpr did not list the UUID types among the Go types with
a kind of their own.
The comments this branch added for UUID fields were hard to read. The
comments had sentences over 25 words, asides in brackets, clauses joined
by semicolons, and Go terms with no explanation. A developer who was not
part of this work could not tell from them why a type declared over
another type is not a UUID.

Each comment now says what its function returns and when the result is
nil. The comment on reader.uuidVerdict defines declaration, defined
type, alias, and underlying type. That comment then lists the three
cases in which a type is a UUID. Parallel cases are lists: the types
KindUUID covers, the kinds kindExpr writes, the Go types convert reads,
and the indexes indexApplies accepts.

The comment on the sessions golden case said that its [16]byte field has
an equality index. That field has no index, and the comment now says so.

No code changes. Every changed Go file has the same syntax tree as
before once its comments are removed.
The stashgen README described UUID fields in one paragraph of eight long
sentences, in the middle of the section on wire types. That paragraph is
now a section of its own, "UUID fields". The section lists the UUID
types, then the types that are not a UUID. The section also says:

- why stashgen refuses a type declared over another type
- how stashgen reads a declaration
- which indexes a UUID takes
- what a nil pointer stores

The changelog entry is split into the same lists. The entry defines
kind, declaration, and sealed null where it first uses them.

The encrypt README and the package documentation now state the rule for
a type defined over another type. That rule is stricter for a UUID than
for the other types. The package documentation also says that a pointer
field is optional, and what the field stores for nil. The README's
sentence on pointer fields is now a paragraph of its own.

Two other README sentences that named a UUID are lists now. One says
when null=sealed is the default. The other names the scalar types.
stashgen prints these messages when go generate refuses a sealed field.
Three of them were one long sentence with an aside in brackets, and all
of them said "seals" where the developer wrote encrypt.

The refusal of a type declared over another type now starts with that
type, because the developer did not ask for a UUID. The refusal then
lists the UUID types, and gives the three ways to fix the field. The old
message gave one. Declaring an integer type as [16]byte would store it
as a UUID, so the message says which fix is for which case.

The refusal of a type whose declaration cannot be read is three
sentences: what stashgen could not do, the two causes, and when a
package's source cannot be loaded. The list of UUID types is no longer
in that message. The list does not help a developer whose problem is
missing source.

The refusal of a field that is not one scalar now says "must be". A
sentence of its own lists the scalar types, and the list is the same.
The last sentence says "can be encrypted only inside an opaque struct".

The tests that check these messages check the new words. Five failure
messages of the UUID test used "single out" and "follow". Those
messages now say what the test compares.

No behaviour changes. stashgen refuses the same fields.
@coderdan

Copy link
Copy Markdown
Contributor Author

This replies to the findings in the bodies of reviews 6088855333 and 6089215000. The inline threads have their own replies.

  • The SealedNullType refusal did not name a UUID. Fixed on next in 02b6d46. The refusal (packages/stack-encrypt/src/refusal.rs:953) and the record plan's doc (dynamic/record.rs:1248) now list a UUID as a type of fixed width.
  • kindExpr's comment did not list the UUID types (stashgen/emit.go:224). Fixed in b050cae. The comment now lists [16]byte, a type declared as [16]byte, github.com/google/uuid.UUID, and vcvalue.UUID.
  • uuidArray(u) && isUUID(t) tested the array twice (stashgen/read.go:970). Fixed in 6605929. The branch now calls isUUID alone.

This branch has not been deployed

No deployments
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.

2 participants