Skip to content

refactor(services): migrate magic numbers to Settings - #20

Closed
rsanghvi21 wants to merge 2 commits into
refactor/shared-service-bootstrapfrom
refactor/migrate-magic-numbers-to-settings
Closed

rsanghvi21 wants to merge 2 commits into
refactor/shared-service-bootstrapfrom
refactor/migrate-magic-numbers-to-settings

Conversation

@rsanghvi21

Copy link
Copy Markdown
Collaborator

Build-Plan Step Implemented

  • Step: A1.1/A1.2/A1.3/A1.8 (migration across already-completed services).

Checkpoint Execution & Output

$ uv run pytest tests/unit/services --ignore=tests/unit/services/test_edge_gateway.py -k "not test_run_loadgen_with_owned_client_and_zero_rps" -v
99 passed, 1 deselected

$ uv run pytest tests/unit --ignore=tests/unit/services/test_edge_gateway.py -k "not test_run_loadgen_with_owned_client_and_zero_rps" --cov=understudy --cov-branch --cov-fail-under=100
208 passed, 1 deselected, 100.00% coverage

$ uv run ruff check . && uv run ruff format --check . && uv run mypy --strict src tests && uv run lint-imports && uv run python scripts/check_mocks.py
All green.

Excluded above: two pre-existing Windows-local-only items (real httpx.AsyncClient()
construction crashes on this dev machine; not present on CI, ubuntu-latest) --
test_edge_gateway.py and test_run_loadgen_with_owned_client_and_zero_rps.

ADRs Relied On

  • None.

Mock Registry Disclosures

  • None.

Documentation Updates

Definition of Done

  • Implementation complete for the named build-plan step
  • Unit tests written; 100% line and branch coverage of the added lines
  • make check green (see checkpoint output; two pre-existing Windows-local-only items excluded, unrelated)
  • Checkpoint command run and output matches, OR marked unverified with a reason
  • Docs updated where this change made them untrue (none required, already done in feat(services): add services-scoped layered Settings #19)
  • No new # MOCKED: marker without a row in docs/MOCKS.md
  • Conventional commit(s), conventional branch, PR opened, never pushed to main

Context

Completes the fix started in #19 (feat(services): add services-scoped layered Settings): that PR built the ServicesSettings infrastructure but migrated no call
sites, staying one logical change per AGENTS.md §4. This PR is the mechanical
migration -- every magic number/ad hoc os.getenv identified in the original
/code-review finding now reads from get_services_settings() (or, for loadgen,
the uncached load_services_settings(), since its config is evaluated per
CLI-invocation/per-construction rather than once at module import).

Base branch is refactor/shared-service-bootstrap (PR #15), since this PR's
worker/app.py and other service files build on that PR's create_service_app/
db_pool_lifespan refactor (which itself is stacked on #13's worker_loop fix) --
rebasing onto feat/policies-and-loadgen directly would conflict. feat/services-settings
(#19)'s commit is cherry-picked onto this branch so the Settings infrastructure is
available; once #19 and #15 merge, this PR's diff will narrow to just this PR's
changes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CzBeBUsZmE38K1kWeLMhNh

rsanghvi21 and others added 2 commits September 11, 2026 07:54
Every tunable across services/ was read ad hoc via os.getenv(...)
with a hardcoded string default, scattered per-module -- no
services/ equivalent of src/understudy/common/config.py's layered
pydantic Settings pattern existed, violating AGENTS.md §5.10 ("no
magic numbers in code... thresholds, weights, timeouts and ceilings
live in config/*.yaml and are named in docs/02-architecture.md").

Add services/_common/settings.py mirroring config.py's shape (layered
load, frozen pydantic BaseModel per section, cached singleton with a
reset hook for tests) as an independent module -- services/ and
src/understudy/ share no imports, and src/understudy/common must stay
psycopg/fastapi-free, so it cannot host these tunables either.

One deliberate divergence: fields keep the exact env var name they
already used before this module existed (e.g.
AUTH_TOKEN_CACHE_MAX_SIZE, FAULT_INJECTION_SEED), rather than a new
UNDERSTUDY_<SECTION>__<FIELD> scheme -- those names are already wired
into deploy/prod/*.yaml's env/configMapKeyRef entries and loadgen's
CLI flag fallbacks, and renaming them would silently break
already-deployed configuration for no benefit.

Adds config/services.yaml (defaults) and config/services.local.yaml
to .gitignore (mirroring config/local.yaml) for local overrides.
Documents every field in docs/02-architecture.md §2.5.1.

This is infrastructure only: no call site reads from Settings yet
(follow-up PR). Implements build-plan step A1.8.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CzBeBUsZmE38K1kWeLMhNh
Replaces every ad hoc os.getenv(...)-with-hardcoded-default and bare
numeric literal identified across services/ with a read from
get_services_settings()/load_services_settings() (added in the prior
PR): edge_gateway's HTTP_TIMEOUT_SECONDS, auth_service's
TOKEN_CACHE_MAX_SIZE, worker's POLL_INTERVAL_SECONDS and its
previously-unparameterized `LIMIT 50` job-list query (now bound via
%s, matching the file's existing parameterized-query style), all of
loadgen's LoadgenConfig defaults and its httpx.Limits/p99 SLA
threshold, db.py's connection-pool defaults, faults.py's injection
seed default and POOL_EXHAUSTION getconn timeout, and metrics.py's
histogram buckets.

loadgen reads via the fresh (uncached) load_services_settings()
rather than the cached singleton, since both LoadgenConfig's defaults
and main.py's argparse defaults are evaluated per CLI invocation /
per-test-construction and must reflect an env var set right before
that call, not whatever was cached first in the process. Every other
service reads the cached get_services_settings() once at
module-import time, matching how these values were already evaluated
once via os.getenv(...) at import time before this change.

Implements build-plan step A1.1/A1.2/A1.3/A1.8 (migration on
already-completed services).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CzBeBUsZmE38K1kWeLMhNh
Harikeshav-R added a commit that referenced this pull request Sep 11, 2026
@Harikeshav-R

Copy link
Copy Markdown
Owner

Closed as integrated into main via PR #6.

@Harikeshav-R
Harikeshav-R deleted the refactor/migrate-magic-numbers-to-settings branch September 11, 2026 19:49
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.

2 participants