Skip to content

Expose Metrics counters as accessors - #1659

Open
TastyPi wants to merge 1 commit into
sinclairzx81:mainfrom
TastyPi:metrics-survive-frozen-modules
Open

Expose Metrics counters as accessors#1659
TastyPi wants to merge 1 commit into
sinclairzx81:mainfrom
TastyPi:metrics-survive-frozen-modules

Conversation

@TastyPi

@TastyPi TastyPi commented Jul 28, 2026

Copy link
Copy Markdown

Closes #1658

Metrics increments a counter for every Assign, Create, Clone, Discard and Update call. The counters are data properties, so the increment throws TypeError once Metrics is frozen — and since every Type.* constructor routes through at least one of those five, schema construction fails outright rather than merely losing its metrics. The issue has a self-contained reproduction.

This keeps the counters as module-scope variables and exposes them through accessors. A freeze cannot disable an accessor, so the increments keep working.

TMetrics is unchanged, and Object.keys(Metrics) and { ...Metrics } produce identical output before and after, so the counters stay readable, enumerable and spreadable.

Tests

Two tests in test/typebox/runtime/system/memory.ts: that the counters increment, and that they still increment after Object.freeze(Memory.Metrics).

They assert the behaviour rather than the mechanism, so they do not pin the implementation to accessors specifically.

Verification

  • deno task lint — clean, 685 files
  • deno task test — 35,188 passing, 0 failing
  • deno task build — clean
  • packed that build and ran the issue's reproduction against it: prints Metrics frozen? true and then does not throw, where typebox@1.3.8 throws at create.mjs:25

Note

Happy to drop this in favour of a Settings flag (Settings.Set({ metrics: false })) if you would rather metrics were opt-out than freeze-tolerant — the tradeoff is that a flag has to be set before the first schema is constructed, which a library depending on TypeBox cannot guarantee.


🤖 Drafted with Claude Code

Metrics increments a counter for each Assign, Create, Clone, Discard and
Update call. The counters were data properties, so the increment threw a
TypeError once Metrics was frozen, which failed every schema construction
in hosts that deep-freeze a shared module graph.

Keeping the counters in module scope and exposing them as accessors leaves
the increments working under a freeze. TMetrics is unchanged, and the
counters remain readable, enumerable and spreadable.

Co-Authored-By: Claude <noreply@anthropic.com>
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.

Schema construction fails when Metrics is frozen

1 participant