Skip to content

fix: preserve numeric enum constraints during canonicalization - #24

Open
JunkaiWang-TheoPhy wants to merge 1 commit into
MoonshotAI:mainfrom
JunkaiWang-TheoPhy:codex/preserve-numeric-enum-constraints
Open

JunkaiWang-TheoPhy wants to merge 1 commit into
MoonshotAI:mainfrom
JunkaiWang-TheoPhy:codex/preserve-numeric-enum-constraints

Conversation

@JunkaiWang-TheoPhy

Copy link
Copy Markdown

Problem

A tool parameter with type: ["integer", "null"] and enum: [1, null] is valid, but default validation rejects its null member as "not a valid integer". Reversing the type order passes. The same problem affects nullable numbers.

More seriously, Canonical() retries that error without changing the schema and eventually returns {}, dropping all constraints on the tool parameters. A numeric enum containing an unreachable fractional or string value takes the same path instead of preserving its reachable values.

Change

Try every declared type before rejecting an enum member. When no type matches, retain the existing numeric diagnostic but use the existing enum-intersection simplifier, which removes only unreachable enum values. Numeric helper behavior, validation levels, public APIs and dependencies are unchanged.

This follows the existing documented rule in docs/validation-principles.md: enum members excluded by the declared type are pruned during canonicalization while the other constraints survive.

Validation

The same new regression was run with the implementation restored to unmodified main and then with the fix:

go test ./... -run '^TestCanonicalNumericEnumPreservesReachableValues$' -count=1 -v
before: 5 subtests failed, 2 passed (exit 1)
after:  7 subtests passed (exit 0)

The failures identify valid nullable numeric enums being rejected and canonicalization returning {}. The null-first cases are passing controls. The regression also covers pruning fractional integer values and strings while preserving the object and required-field constraints.

Additional checks on Go 1.26.1 / macOS arm64:

  • go test ./...: passed; JSON test events report 279 passing tests/subtests.
  • go test -race ./...: passed.
  • go vet ./...: passed.
  • go build ./...: passed.
  • git diff --check: passed.

The same Go suite also passed on the CI's Go 1.23.0 toolchain. CI-pinned golangci-lint v1.64.8 passed. Building the native library with Go 1.23.0 succeeded, and an actual Python/CGO smoke check validated and canonicalized the nullable integer schema without changing it. The three existing Python regression tests are Linux-specific and were skipped on this macOS host; Linux CGO behavior is left to upstream CI. The locally generated macOS library is not included in the patch.

The leader reviewed the matching/simplification contract and independently reran all seven new regression cases successfully.

AI assistance: investigated, implemented and tested with OpenAI Codex.

The numeric type matcher reports a mismatch as an error, so enum validation
stops before trying a nullable alternative. Its no-op simplifier then makes
Canonical exhaust its retries and return an unconstrained schema.

Try the remaining types, then use the existing enum-intersection rewrite for
an unreachable member while preserving the previous numeric diagnostic.

Constraint: MFJS supports numeric types paired with null and prunes unreachable enum values
Rejected: Change IsTypeMatch globally | its error contract is used by numeric validation and existing diagnostics
Confidence: high
Scope-risk: narrow
Tested: Seven public-API regressions; rollback 5 failed/2 passed, fixed 7 passed; full Go suite, race, vet, build
Not-tested: CI-pinned Go 1.23 and Linux Python/CGO suite

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.

1 participant