Repository navigation
Description
Summary
Ship Typhon's DEBUG-only correctness checks in the Release NuGet by converting the user-valuable ones into runtime-gated checks (the profiler-gate pattern), so users can enable a "strict mode" for diagnosis without needing a separate Debug build. Developer-only internal tripwires stay Debug-only.
Design doc (settled): claude/research/Observability/debug-checks-runtime-gating.md
Background
The Release NuGet strips every Debug.Assert / #if DEBUG / [Conditional("DEBUG")] check (~177 across the engine). Shipping a Debug-built assembly via NuGet was rejected — NuGet has no build-configuration axis (layout is keyed on TFM/RID only). Instead, split the checks by who benefits into three tiers.
Three-tier design
Tier 0 — always-on guards (ungated, free). The predicate already runs in Release; only the reaction was stripped, so promoting costs nothing yet closes a silent-failure hole.
ManagedPagedMMF.cs:583/635/658duplicate segment-root → throw (allocator corruption)AccessControl.cs:385/485wrong-thread exclusive release/demote → throw (lock-protocol corruption)SpatialRTree.Query.csDFS depth>256 ×7 → Record (counter + log, latch-safe; no silent incomplete results)
Tier 1 — opt-in strict mode (~50 user-facing checks). New CheckConfig class mirroring TelemetryConfig (static readonly bool → JIT dead-code-eliminated when off). Off by default; enabled via typhon.telemetry.json (Typhon:Checks:Enabled) or the TYPHON__CHECKS__ENABLED env var. A DeclaredAccessActive sub-flag gates the one costly check (SystemAccessValidator.AssertWrite<T>). Covers EntityRef misuse (write-on-readonly, disabled component, wrong-archetype handle), thread-affinity, MVCC Versioned-bypass, spawn/destroy/registration guards, on-reopen store-corruption/schema-drift, PackedDateTime range, etc.
Tier 2 — keep-as-is (~95 dev-facing). Internal invariants stay Debug.Assert/#if DEBUG; Typhon developers use a local clone + Debug build. (~20 more dropped as noise.)
Acceptance criteria
- Tier 0: the 3 corruption swallowers throw a corruption exception via
ThrowHelper; the R-tree overflow ×7 records a counter +[LoggerMessage]warning; all always-on, no gate. -
CheckConfigclass withEnabled+DeclaredAccessActive, loaded once at class init from env /typhon.telemetry.json(config-time, JIT-foldable — no runtime setter). -
Require(throw, lazy message) +Record(latch-safe, never throws) primitives, reusingThrowHelper. -
EntityAccessor._debugOwningThreadIdpromoted out of#if DEBUG(3 sites) so the affinity check compiles always-on. - ~50 user-facing checks converted behind
CheckConfig.Enabled, with lazy messages (no per-call allocation when the check passes). - Zero measurable hot-path cost when strict mode is off (BDN spot-check on the EntityRef read/write path).
- Tests: strict mode on → misuse throws; off → no-op. Tier 0 corruption/record paths covered.
- Research doc promoted to
design/Observability/; user-facing docs updated.
Notes
- Order: land Tier 0 first — it's free and independent of
CheckConfig. - Constraint: any check reachable while holding an OLC latch must
Record, neverthrow(an exception under a latch leaks it → deadlock). - Reuse existing
ThrowHelper+SystemAccessValidator'sInvalidAccessExceptionpattern — no new plumbing.
Activity
nockawa commentedon Jul 2, 2026
Implementation plan (approved 2026-07-02) —
/implement-featureBranch:
feature/422-strict-mode-checks. Scope: whole issue, Tier 0 → Tier 1 (Tier 2 = no work).Verified deviations from the design doc (grounded in current code):
PackedDateTime(3 checks) deferred — it lives inTyphon.Schema.Definition, a leaf assembly Engine depends on (one-way); it cannot referenceCheckConfig(Engine) without a cycle. Kept asDebug.Assert. Net convert set ≈ 47, not 50.CheckConfig) — staticInterlockedcounter + a new minimal[LoggerMessage]partial onSpatialRTree(precedentSpatialMaintainer.cs), emitted latch-safely (never under the OLC latch).test/.../typhon.telemetry.json→Checks:Enabled=true);DeclaredAccessstays off globally (needs a system context) and is proven via dedicated primitive-level tests.Increments:
ManagedPagedMMF+ 2×AccessControlDebug.Fail/Assert(false)→ThrowHelper.ThrowCorruption(existingCorruptionException); 7×SpatialRTree.QueryDFS-overflow → always-on Record (counter +[LoggerMessage]).CheckConfig: newObservability/public/CheckConfig.csmirroringTelemetryConfig(static readonly bool Enabled+DeclaredAccessActive, reusesTelemetryConfig.Configuration);Require/Recordprimitives (lazy interpolated-string message, reuseThrowHelper); wired in theProfilerBootstrap[ModuleInitializer]; gate-shape reflection test.EntityRef,ClusterRef,ComponentValue,EntityId,EcsQuery,PointInTimeAccessor,EntityAccessor(affinity promotion out of#if DEBUG),Transaction(.ECS),SystemAccessValidator+DagScheduler(AssertWritebehindDeclaredAccessActive),PagedMMF/BootstrapDictionary/PagedHashMapBase.design/Observability/; update09-observability.md.Acceptance criteria tracked 1:1 with the issue checklist.
nockawa commentedon Jul 2, 2026
Implemented (2026-07-02) — ready for review + commit
Branch
feature/422-strict-mode-checks. All 8 acceptance criteria met (one scoped deviation on #5, below).ManagedPagedMMF.cs×3 +AccessControl.cs×2 →ThrowHelper.ThrowCorruption;SpatialRTree.Query.cs×7 →SpatialRTreeDiagnostics.RecordDfsStackOverflow(counter +[LoggerMessage]). Covered byTier0GuardsTests(3).CheckConfig(Enabled+DeclaredAccessActive)Observability/public/CheckConfig.cs, reusesTelemetryConfig.Configuration,EnsureInitializedwired inProfilerBootstrapmodule-init. Gate-shape reflection test.Require+Recordprimitives (lazy msg, reuseThrowHelper)CheckMessageHandler) skips arg-eval on the passing path — proven by theProbe()side-effect test inCheckConfigTests.#if DEBUG_debugOwningThreadId→_owningThreadId(field+seed+clear+Transactionseed);AssertThreadAffinityun-[Conditional]'d, inline-guarded.EntityRef12,ClusterRef4,ComponentValue3,EntityId2,EcsQuery5,PointInTimeAccessor8,Transaction(.ECS),SystemAccessValidatorbehindDeclaredAccessActive,PagedMMF/BootstrapDictionary/PagedHashMapBase). Deviation:PackedDateTime(3) deferred — it lives inTyphon.Schema.Definition, a leaf assembly Engine depends on, so it can't referenceCheckConfigwithout a cycle. Kept asDebug.Assert.Requireoff-path ≈ 0.24 ns/call, zero alloc.StrictModeOffPathBenchmarkscommitted. (Note: BDN external run is blocked in this env by an unrelated stray.claude/worktrees/437benchmark project — use the external toolchain, not InProcess.)CheckConfigTests(primitives + canary),StrictModeMisuseTests(write-readonly / destroy-null / cross-thread),Tier0GuardsTests. Strict mode enabled suite-wide in the testtyphon.telemetry.json.research/Observability/…→design/Observability/debug-checks-runtime-gating.md(status Implemented + deviations); strict-mode subsection added tooverview/09-observability.md.Code review: 3-agent parallel review (infra+Tier-0 / ECS conversions / runtime+storage+tests). No critical/high bugs. Addressed all actionable findings:
AssertWrite/Enter/Leave+AssertThreadAffinityfast-slow inline split (removes residual off-path call onEntityRef.Write), single-SpawnEntityMap check → inline-guard (array-index condition can't be DCE'd),Recordhardened withtry/catch,CheckMessageHandlerformat/alignment overloads, config-regression canary test,[NonParallelizable], unused-using + 160-col cleanups.Verification: engine + tests build clean (0 warn/err); the 24 #422 tests pass (0 skipped — strict mode active); full suite green apart from pre-existing parallel-flaky tests (
ExceptionPathLeakTests×3, oneTransactionTestsread) that all pass in isolation.Handoff note: the working tree also carries the unrelated
TraceIdEnricher.csdeletion +Typhon.Engine.csprojedit (packaging feature #427–434) that rode onto this branch frommain— commit those separately.nockawa commentedon Jul 2, 2026
Correction on AC #8 (user-facing docs). My completion note ticked #8 clean, but I'd only updated the
claude/knowledge base (design doc +overview/09-observability.md) — the user-facing DocFxdoc/tree was missed. Now filled:doc/feature-set/Observability/strict-mode-checks.md— full feature doc (template-matched totelemetry-config-gating.md): the two flags + key table, JSON +TYPHON__CHECKS__*precedence table, guarantees (off-by-default, resolve-once, ~0.24 ns zero-cost, what-it-catches, Tier-0 always-on guards), test links.doc/feature-set/Observability/README.md— Public Features table row.Not touched:
doc/in-depth-overview/12-observability.md(the narrative deep-dive chapter) — optional follow-up if the strict-mode section is wanted there too.Fix GitHub Actions script injection in project-sync.yml (#448)
#422 Runtime-gated correctness checks ("strict mode") + benchmark CI …