Skip to content

Typed TOML config with env override layer for PACE_* switches - #57

Merged
IRmsk merged 6 commits into
IRmsk:mainfrom
AptS-1547:toml-config
Nov 26, 2026
Merged

IRmsk merged 6 commits into
IRmsk:mainfrom
AptS-1547:toml-config

Conversation

@AptS-1547

@AptS-1547 AptS-1547 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes #52.

Summary

Replaces the scattered std::env::var("PACE_*") reads (25 call sites) and the hand-written ENTRIES documentation table with a typed config layer in a new leaf crate, crates/pace-forge-config:

  • Precedence: PACE_* env > config.toml > built-in defaults. File discovery: --config <path> / PACE_CONFIG > ./config.toml > ~/.pace-forge/config.toml. Egress proxy keeps its extra top layer (web-saved setting first), unchanged.
  • One registry. All 21 switches are defined exactly once — key, env name, parser, default, effect. The same table drives TOML parsing, env parsing and pace-forge config, so the documented list can no longer drift from what actually gets read (issue Proposal: typed TOML config with env override layer for PACE_* switches #53 documents seven runtime-read vars missing from the old table, with file:line evidence).
  • Fail-fast. Invalid values, unknown TOML keys (typo protection) and half-specified fence anchors are collected and reported together; startup exits 2. No more parse().ok().unwrap_or(default) silent fallbacks.
  • Process snapshot. main.rs loads once before dispatching; everything else reads config::global() (an Arc<Config>). Tests inject via Guard::replace, which serializes internally and restores on drop — no more set_var in tests.
  • pace-forge config keeps its table and now shows, per switch, the current value and which layer it came from (env / config.toml / default). Secrets stay masked. Web startup keeps the non-default line.

Behavior changes (all declared in #53)

For every documented input value, behavior is byte-identical to before — per-key parsing was aligned against the old code (which keys trim, which keep raw text, run_mode accepts any integer, enum vocabularies match raw text, tz/bulge ranges unchanged). The deltas:

  1. Invalid values now abort startup instead of silently falling back to defaults. That is the point of the refactor.
  2. PACE_IGNORE_DAILY_LIMIT=0 now disables the bypass. The old presence check (is_ok()) enabled it for any value, including 0.
  3. Bool switches accept a unified, case-insensitive 0/1/true/false/yes/no vocabulary. The old parsers were inconsistent per key (fence_full accepted only "1", fixed_strict also accepted "TRUE"); every previously documented value behaves as before.
  4. PACE_DUMP_POLICY="" no longer acts as a directory name.

Also, key_sources_fall_back_past_the_posix_defaults now skips on hosts with no machine-identity source (macOS has neither /etc/machine-id nor /etc/hostname and COMPUTERNAME is unset) — it kept cargo test red on dev machines, unrelated to this refactor.

Verification

  • cargo test: 525 lib tests + integration tests green (includes a new boundary/vocabulary matrix in the config crate: numeric range edges, inf/nan/whitespace digits, full enum vocabularies, legacy PACE_FORCE_NORMAL_POINT mapping, precedence, anchor pairing, guard restore, secret masking; and main-crate mapping-equivalence tests for fixed_point / pt_scheme / run_area_id / strict / anchor / tz / hw headers).
  • cargo clippy --all-targets: 0 warnings. cargo fmt --check clean.
  • End-to-end smoke on the real binary: --config source labels, env-over-toml override, and exit code 2 with a collected error list for invalid env values, unknown TOML keys, and missing --config files.
  • Not verified against the real server: no live submission runs were made for this refactor; protocol behavior is untouched by design (payload construction reads the same resolved values as before).

… exists

macOS has neither /etc/machine-id nor /etc/hostname and COMPUTERNAME is
unset, so the fallback chain legitimately ends at "unknown" and the
assertion premise never holds; cargo test stayed red on dev machines.
…g table

All 21 PACE_* switches (7 of which were read at runtime but missing from
the old src/config.rs table: PACE_EXTRA_CA, PACE_IGNORE_DAILY_LIMIT,
PACE_PREFER_STADIUM, PACE_CENTER_LAT, PACE_CENTER_LNG, PACE_FIXED_POINT,
PACE_FORCE_NORMAL_POINT) are defined exactly once - key, env name,
parser, default, effect. The same table drives config.toml parsing, env
parsing and the `pace-forge config` listing, so the documented list can
no longer drift from what actually gets read.

Precedence is env > config.toml > built-in defaults. Loading is
fail-fast: unknown keys, out-of-vocabulary values and half-specified
fence anchors are collected and reported together before anything runs.
A process-wide read-only snapshot (store/global, Guard for tests)
serves the values. Shared deps move to workspace = true.
…alls

main.rs parses --config, loads the config and stores the snapshot before
dispatching; the 25 scattered std::env::var("PACE_*") call sites across
api/web/track now read config::global(). Parsing semantics are aligned
with the old code per key (which keys trim, which keep raw text,
run_mode accepts any integer, enum vocabularies match raw text, tz and
bulge ranges are unchanged), so valid inputs behave exactly as before.

Deliberate behavior changes, all declared in IRmsk#53 and CHANGELOG:
- invalid values abort startup with a config error instead of silently
  falling back to defaults (the point of the refactor)
- PACE_IGNORE_DAILY_LIMIT=0 now disables the bypass; the old presence
  check enabled it for any value including 0
- bool switches accept a unified 0/1/true/false/yes/no vocabulary
  (case-insensitive); every documented value behaves as before
- PACE_DUMP_POLICY="" no longer acts as a directory name

The crosscheck CLI test now locks the fail-fast contract for invalid
PACE_TZ_OFFSET_SEC values.
config.example.toml lists every key with comments; README gains a config
section covering search order, precedence and the per-key source labels
in `pace-forge config`; CONTRIBUTING points contributors at the registry
as the single place to add a switch; CHANGELOG records the migration
notes and the two behavior fixes (IGNORE_DAILY_LIMIT=0 semantics, the
machine-identity test on source-less hosts).
The 21-row switch table duplicated config.example.toml line for line
(defaults, effects, env names) and nobody enjoys reading a wall of rows
in a terminal. The reference belongs to the docs: config.example.toml
(all keys, commented) plus the README config section. The one piece of
live information the table had - which switches are currently
non-default and which layer they come from - stays in the web startup
log line.

With the table gone, the registry no longer carries the display-only
default/effect strings either; entries keep only what code needs (key,
env name, parser). The unknown-key error now points at
config.example.toml alone.
lib.rs had grown past 1400 lines holding seven unrelated concerns plus
the test matrix. Now: value (parsed values and sources), model (typed
config structs and key_values), parser (per-key parsers), registry
(entry table), load (file discovery, TOML/env layers, cross validation),
runtime (process snapshot and startup summary), tests (the
equivalence/boundary matrix). The public API is unchanged - lib.rs only
declares modules and re-exports, so pace_forge::config::* paths stay
exactly as they are.
@AptS-1547

Copy link
Copy Markdown
Contributor Author

Two follow-up commits pushed to this branch:

1. pace-forge config subcommand removed (a6e5686)

On reflection, the 21-row switch table this PR added to pace-forge config was redundant with config.example.toml line for line (defaults, effects, env names) and unpleasant to read in a terminal. The key reference now lives only in the docs:

  • config.example.toml — all keys, commented, the single reference
  • README config section — search order, precedence, fail-fast contract
  • the web startup log line — which switches are currently non-default and from which layer (the one piece of live information the table had)

The registry also stopped carrying display-only default/effect strings, so entries now hold only what code needs (key, env name, parser). Unknown-key errors point at config.example.toml.

2. crate split into modules (96e2866)

pace-forge-config/src/lib.rs had grown past 1400 lines. Now split by responsibility: value / model / parser / registry / load / runtime / tests. Public API unchanged — lib.rs only declares modules and re-exports.

Both verified: 525 lib tests + 21 config-crate tests + integration green, clippy 0 warnings, fmt clean. Smoke-checked on the real binary: pace-forge config now exits 2 as an unknown command, unknown TOML keys error with a pointer to config.example.toml, and --config with a missing file still aborts with the collected error list.

@IRmsk
IRmsk merged commit ac4f4cb into IRmsk:main Nov 26, 2026
IRmsk added a commit that referenced this pull request Nov 26, 2026
#58)

Phase 1 of the proposal, the near-zero-cost part:

- [workspace.lints.rust]: unsafe_code = forbid, unsafe_op_in_unsafe_fn = deny.
  The crate has no unsafe left after #57 removed the env writes in tests.
- [workspace.lints.clippy]: allow_attributes and
  allow_attributes_without_reason denied, so suppressions are written as
  #[expect(.., reason = ..)] and complain once they go stale;
  undocumented_unsafe_blocks and multiple_unsafe_ops_per_block denied.
- The config crate now inherits the workspace lints instead of its own copy.
- The five remaining #[allow]s:
  - two dead_code ones covered items that only tests use, so they are
    #[cfg(test)] now and the suppression is gone;
  - two too_many_arguments became #[expect] with a reason;
  - one (detail_payload_rrid) was stale, the function no longer has too
    many arguments, so it is simply removed.

missing_docs, pedantic and the cast denies are deliberately not enabled
yet: CI is -D warnings and they need their cleanup in the same change.
@IRmsk

IRmsk commented Nov 26, 2026

Copy link
Copy Markdown
Owner

合了,这个 PR 我核得最细:全门、release 构建、cargo-deny,还实际跑了写错值(退出码 2,报错说得很清楚)、未知键、不带参数启动。都没问题。

有一件事想直说:后加的那个 commit 把 pace-forge config 子命令删了,和 PR 描述里写的「保留」不一样。你的理由我认同(表和 config.example.toml 一行行重复),但现在只用 CLI 的人想看「哪些开关是非默认的」就没地方看了,web 启动那行日志覆盖不到他们。

我先合了,没改你的代码。你要是愿意,我想请你补一个很薄的 config 命令:不带参数就只列非默认项和来源那一列,不再印全表。不愿意也没事,我自己来。

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.

Proposal: typed TOML config with env override layer for PACE_* switches

2 participants