Skip to content

fix(services): gate synthetic auth tokens as an explicit mock - #18

Merged
Harikeshav-R merged 2 commits into
feat/policies-and-loadgenfrom
fix/gate-synthetic-auth-tokens
Sep 11, 2026
Merged

Harikeshav-R merged 2 commits into
feat/policies-and-loadgenfrom
fix/gate-synthetic-auth-tokens

Conversation

@rsanghvi21

Copy link
Copy Markdown
Collaborator

Build-Plan Step Implemented

  • Step: A1.2 (fix on an already-completed service; resolves an AGENTS.md §8 stop-and-ask surfaced during code review).

Checkpoint Execution & Output

$ uv run pytest tests/unit/services/test_auth_service.py -v
11 passed in 2.44s

$ 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
196 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 -- check_mocks.py confirms the new marker matches its docs/MOCKS.md row.

ADRs Relied On

  • None directly; this resolves an AGENTS.md §2.3/§7 mocking-discipline gap,
    not a decision.

Mock Registry Disclosures

Documentation Updates

  • docs/02-architecture.md §2.5: updated the auth-service row to describe
    the current seeded+synthetic in-memory design and link the tracking issue,
    replacing the stale "in-memory cache over Postgres" description that implied
    the Postgres fallback already existed.

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)
  • Checkpoint command run and output matches, OR marked unverified with a reason
  • Docs updated where this change made them untrue
  • No new # MOCKED: marker without a row in docs/MOCKS.md
  • Conventional commit(s), conventional branch, PR opened, never pushed to main

Context

A /code-review pass against PR #6 flagged auth_service's /validate
endpoint: it accepts any Bearer valid-*/Bearer token-* token as valid
with no real credential store, on the always-reachable path -- an unmarked
mock (AGENTS.md §2.3 "never fake an integration silently"). Investigation
also found a related bug: the seeded admin token could be evicted from the
FIFO TOKEN_CACHE under sustained traffic and then silently re-derived with
a downgraded scope via the same synthetic-pattern branch.

Per AGENTS.md §8, this was raised as a blocking decision rather than resolved
unilaterally: keep the synthetic-token convenience (gated + marked) or build
real Postgres-backed validation. The user chose to gate and mark it
rather than build the real store now. This PR implements that choice:
AUTH_SERVICE_ALLOW_SYNTHETIC_TOKENS gates the branch (on by default outside
prod), the branch is marked # MOCKED: with a real tracking issue
(#16, opened as part of this work since none existed),
and the admin-token-downgrade bug is fixed by giving seeded tokens a
separate, non-evictable path that's checked before the synthetic branch can
ever see them.

Depends on #8 (fix(scripts): scan services/ for # MOCKED: markers in check_mocks) for the new marker to actually be enforced by make check --
that commit is cherry-picked onto this branch so both can be reviewed and
merged independently; once #8 merges to feat/policies-and-loadgen, this
PR's diff will narrow to just this PR's changes.

Base branch is feat/policies-and-loadgen (PR #6, still open), since
services/ only exists on that branch's stack.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CzBeBUsZmE38K1kWeLMhNh

rsanghvi21 and others added 2 commits September 11, 2026 07:36
check_mocks.py only scanned src/ for # MOCKED: markers, so a marker
placed anywhere under services/ was invisible to the make check
mock-parity gate required by AGENTS.md §7.3. Add a second scan of
services/, re-keying its markers with the services/ prefix so they
match the dotted module names used in docs/MOCKS.md (e.g.
services.auth_service.app).

Also fixes a latent Windows bug uncovered while adding this: rel_path
was built with str() instead of as_posix(), so backslash-separated
paths never matched the forward-slash dotted module names produced by
_module_matches_file. This was invisible until now because
docs/MOCKS.md has been empty, but would have silently broken parity
checking for any nested src/ marker on Windows.

Discovered while investigating a code-review finding for PR #6; this
fix unblocks registering the auth_service synthetic-token mock in a
follow-up PR.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CzBeBUsZmE38K1kWeLMhNh
services/auth_service/app.py accepted any Bearer valid-*/token-*
token as valid on the always-reachable /validate path, with no real
credential store behind it -- an unmarked, unregistered mock
(AGENTS.md section 2.3, section 7). Compounding this, TOKEN_CACHE's
FIFO eviction could evict the seeded admin token
("Bearer valid-token", scope: admin) under sustained synthetic
traffic; revalidating it afterward fell through to the
synthetic-pattern branch (it also matches "Bearer valid-"), silently
re-caching it as scope: standard with no error.

Fix both together:
- Carve the two seeded tokens into SEEDED_TOKENS, a separate,
  non-evictable dict checked before TOKEN_CACHE and before the
  synthetic-pattern branch. A seeded token can no longer be evicted
  or re-derived with a different scope.
- Gate the synthetic-pattern branch behind
  AUTH_SERVICE_ALLOW_SYNTHETIC_TOKENS, defaulting to enabled outside
  prod (mirroring how is_fault_injection_enabled gates on
  UNDERSTUDY_ROLE=prod) so it can be forced off, or explicitly
  re-enabled in prod for the eval harness.
- Mark the remaining synthetic-acceptance branch # MOCKED: and
  register it in docs/MOCKS.md (module services.auth_service.app,
  tracked in #16) -- now enforced by
  scripts/check_mocks.py per the earlier services/ scan fix.

Implements build-plan step A1.2 (fix on an already-completed
service), resolving an AGENTS.md section 8 stop-and-ask flagged
during code review.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CzBeBUsZmE38K1kWeLMhNh
@Harikeshav-R
Harikeshav-R merged commit 35e4ee2 into feat/policies-and-loadgen Sep 11, 2026
1 check passed
@Harikeshav-R
Harikeshav-R deleted the fix/gate-synthetic-auth-tokens 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