Skip to content

fix(apidoc): deterministic enum order for fork-extended enums - #414

Merged
ibetitsmike merged 4 commits into
mainfrom
mike/issue-410-apidoc-enum-order
Nov 21, 2026
Merged

ibetitsmike merged 4 commits into
mainfrom
mike/issue-410-apidoc-enum-order

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Closes #410

What and why

make gen produced a different coderd/apidoc/swagger.json, coderd/apidoc/docs.go and generated markdown on every run. swag v1.16.6 collects Go constants per file in Go map iteration order (PackagesDefinitions.ParseTypes iterates pkgDefs.files, a map keyed by *ast.File), so any enum whose constants span more than one file came out in a random order. Three fork-extended enums are affected:

  • codersdk.Experiment (deployment.go + fork_experiments.go)
  • codersdk.ChatContextResourceKind (chats.go + fork_agent_definitions.go)
  • codersdk.ChatWatchEventKind (chats.go + fork_chat_watch.go)

The swaginit wrapper (our own generator, scripts/apidocgen/swaginit) now canonicalizes the generated spec right after swag writes it: upstream (non fork_) constants keep exactly the order swag emitted, fork constants declared in fork_*.go files are appended at the back in deterministic source scan-rank order. No alphabetical sorting. The three parallel arrays (enum, x-enum-varnames, x-enum-descriptions) are permuted in lockstep. swagger.json is edited in place; docs.go is edited inside its embedded docTemplate JSON by exact offset splicing; the markdown is generated from the canonicalized spec by the existing widdershins pipeline.

Scope

  • Fork logic lives in scripts/apidocgen/swaginit/fork_enumorder.go and fork_enumorder_scan.go (the wrapper package is ours to edit).
  • The only hook in upstream-owned code is one call in scripts/apidocgen/swaginit/main.go after gen.Build.
  • Makefile changes one dependency line; scripts/apidocgen/generate.sh changes the go run target from main.go to the package directory (required now that the package has multiple files).
  • Regenerated output committed: coderd/apidoc/swagger.json, coderd/apidoc/docs.go, docs/reference/api/chats.md, docs/reference/api/schemas.md. The Experiment block is unchanged versus base because upstream-first plus fork-last is already what base had; ChatContextResourceKind and ChatWatchEventKind move their fork constants to the back, so the docs example kind values follow the new first element (instruction_file, status_change), matching upstream's examples.

Determinism evidence

After merging origin/main (fa59c8f), three back-to-back make gen runs under flock ~/.fork-go.lock, sha256 each run, all byte-identical:

run1 == run2 == run3

d12130fbd208c0061d7137fbbfe6a4a6174d37a21c2c7b2b083f55ba1d6be7f8  coderd/apidoc/swagger.json
1e0816509a5ea719f2a4f6f2c22216cfc530b08d040bcd6ff02c9ddb912a9b00  coderd/apidoc/docs.go
954ef54b106a6d5b5f7846df0787a88778228b850312cae7a3e12a576c72b713  docs/reference/api/agents.md
1748bdc1a23b8170d1ed8f88e3b12ae7489280515e99ce83c9d9437f1e870827  docs/reference/api/chats.md
db119048a2c15a5c8829fc06d4d31c2b478ff9b89564fb99029f3172f30933a6  docs/reference/api/general.md
dff10d5dc851a701283df7d6fcb8592248980bfe6321b88fc31238b93a509f59  docs/reference/api/schemas.md

All 33 generated files hashed identically across runs 1, 2 and 3.

Test that fails without the fix

TestCanonicalizeRestoresRandomizedEnumOrder (fork_enumorder_scan_test.go) mutates the committed swagger.json and docs.go the way swag's random order does, then requires canonicalizeEnumArrays to restore the committed bytes exactly. With Plan neutered to return nil, the test fails on both files:

--- FAIL: TestCanonicalizeRestoresRandomizedEnumOrder
    --- FAIL: .../swagger.json: first diff at 547224
    --- FAIL: .../docs.go: first diff at 942432

With the fix: go test ./scripts/apidocgen/swaginit/... -count=1 passes; golangci-lint run ./scripts/apidocgen/swaginit/... reports 0 issues; go vet clean.

check-apidoc.sh

.github/fork/check-apidoc.sh jq sort is left in place. It is now redundant for the three fork-extended enums because generation is deterministic, but it also guards forks that compare a committed tree against a fresh run without make gen full context, so it stays as a cheap safety net.

Pre-commit hook

This PR follows the pre-commit hook setup from #407. The white-box test files are named *_internal_test.go per the testpackage linter rule. The worktree opt-out (git config --worktree coder.pre-commit false) was used for the commits on this branch because the full make pre-commit hook runs outside the flock and includes the known main-branch golangci findings. It applies to this worktree only and was set back after the work. Targeted checks run instead, each under flock ~/.fork-go.lock:

  • go test ./scripts/apidocgen/swaginit/... -count=1: ok
  • golangci-lint run ./scripts/apidocgen/swaginit/...: 0 issues
  • gofmt -l scripts/apidocgen/swaginit/: clean
  • 3 make gen determinism runs after the origin/main merge: byte-identical

Not exercised

Runtime behavior of the three enums in a running server was not tested; the change is generation-time only and the enum values themselves are untouched, only their order in docs and example strings.

swag collects constants per file in Go map iteration order, so the
enum arrays for codersdk.Experiment, codersdk.ChatContextResourceKind
and codersdk.ChatWatchEventKind came out in a different order on every
make gen run, changing coderd/apidoc/swagger.json, coderd/apidoc/docs.go
and the generated markdown.

The swaginit wrapper now canonicalizes the generated spec after
generation: upstream (non fork_) constants keep the order upstream
produces, fork constants are appended at the back in source scan-rank
order. swagger.json, docs.go and the markdown are all generated from
the canonicalized spec.

Closes #410
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T16:26:28.643784Z c16d294 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2d00194045

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Makefile Resolved
- Rename canonicalize.go, enumorder.go and enumorder_test.go to
  fork_enumorder_canonicalize.go, fork_enumorder_scan.go and
  fork_enumorder_scan_test.go so every new fork file follows the
  fork_*.go naming rule.
- Depend on $(wildcard scripts/apidocgen/swaginit/*.go) instead of the
  directory so edits to the generator rerun make gen.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 66e2974f7d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

The testpackage linter only exempts package main test files named
*_internal_test.go, so rename both swaginit test files accordingly.
Addresses the P1 from the Codex review on 66e2974.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

P1 (white-box test naming): fixed in c16d294, both test files renamed to *_internal_test.go. Tests and golangci-lint re-run clean under the flock.

P2 (track swaginit sources instead of the directory): already fixed on this branch, the Makefile depends on $(wildcard scripts/apidocgen/swaginit/*.go) since 8433e27.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: c16d294111

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike merged commit e24f7e7 into main Nov 21, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-410-apidoc-enum-order branch November 21, 2026 16:40
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head c16d2941114f7709f446edc643e5de09407ce2c1 as e24f7e7, on main fa59c8fb112cd0049f114acff21b76c4ed1cf446, with the server running that main.

Gates on this exact head:

  1. The head matched the SHA I verified (--match-head-commit).

  2. Every check run passed. fork-ci run 44924284560 passed on this head.

  3. Codex receipt for this head: fix(apidoc): deterministic enum order for fork-extended enums #414 (comment) ("Didn't find any major issues", Reviewed commit c16d294111).

  4. No unresolved review thread. Codex raised 1 P1 (test file naming) and 1 P2 (Makefile source tracking), and both are fixed.

  5. 0 commits behind main.

  6. Test evidence I ran in the PR worktree at this head, under the build lock:

    • go test ./scripts/apidocgen/swaginit/... -count=1: ok.
    • Negative control: with Plan returning nil, TestPlan and TestCanonicalizeRestoresRandomizedEnumOrder fail. The file was restored afterwards.
    • The generated diff against main touches only ChatContextResourceKind and ChatWatchEventKind. Upstream constants keep their upstream order, and the fork constants (subagent, fork_change) come last. Experiment is unchanged.

    The workstream ran 3 back-to-back make gen runs after merging main, and all 33 outputs were byte-identical (swagger.json sha256 d12130fbd208...). It also ran golangci-lint and gofmt on the package. I did not rerun those.

Review notes:

Not exercised: the server at runtime. This is a generation-time change, and enum values are unchanged. Only the order in the docs and the example strings move.

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.

chore: make the fork apidoc gen enum order deterministic

1 participant