Skip to content

feat(clock): read time through the clock seam - #11

Merged
trogers1052 merged 3 commits into
mainfrom
feat/clock-adoption
Jul 27, 2026
Merged

feat(clock): read time through the clock seam#11
trogers1052 merged 3 commits into
mainfrom
feat/clock-adoption

Conversation

@trogers1052

Copy link
Copy Markdown
Owner

Phase 2b of the e2e-replay workflow, and the first adoption touching the
capital path. The change is where time comes from, never what the logic
does.

Evidence it is behaviour-preserving

  • All 849 pre-existing tests pass untouched (869 total with the clock tests
    and the newly-merged hard-gate tests)
  • CLOCK_MODE defaults to real; only the exact string replay switches
    modes, so a typo cannot put production on a simulated clock
  • The real path never opens a Redis connection for the clock

25 semantic sites moved

Signal debounce, ranking interval, context staleness, earnings staleness,
stale-signal/stale-state eviction, entry and scale-in dates, tier cache TTL,
published event timestamps, and the trade planner's end-of-day / trading-day /
validity-window arithmetic.

7 sites deliberately keep the wall clock

Simulated time would make these wrong, not right:

  • EVALUATION_DURATION — a latency metric; real work measured in simulated
    time records zero
  • the trade planner's Redis circuit-breaker backoff — Redis is a real server
    during a replay, so 15s must elapse in real seconds
  • rules_cache config caching — rules are static across a run, and a simulated
    TTL would re-read Redis every simulated minute

The naive/aware trap

datetime.utcnow() returns naive datetimes; signal timestamps,
last_update and entry_date are naive throughout. An aware-returning clock
would raise TypeError at every comparison. So the shim exposes exact
drop-ins: clock.utcnow() (naive), clock.now() (aware, for the trade_planner
sites that already used aware), clock.timestamp() (float). Tests cover both
directions.

A module-level accessor is used rather than constructor injection because the
reads live in module-level functions and dataclass methods with no constructor
to thread through — a far larger diff across code that sizes positions and sets
stops.

Also

Drops 5 imports this change orphaned (verified against main that no
pre-existing warnings were touched) and removes 61 datetime.utcnow()
deprecation warnings under Python 3.12.

Verified end to end: this branch ran the real spine under CLOCK_MODE=replay
across 479 simulated trading days, firing Golden Cross / Full Trend Alignment /
Commodity Breakout and generating trade plans with stop-snapping.

🤖 Generated with Claude Code

trogers1052 and others added 3 commits July 25, 2026 22:04
Phase 2b of the e2e-replay workflow, and the first adoption that touches
the capital path. The commit changes WHERE time comes from and never WHAT
the logic does: all 849 pre-existing tests pass untouched.

25 semantic sites now read decision_engine.clock:
  - signal debounce and ranking interval (service)
  - market-context staleness (market_context)
  - earnings staleness (checklist)
  - stale-signal and stale-state eviction, entry/scale-in dates
    (state_manager)
  - tier cache TTL (tier_reader)
  - published event timestamps (kafka_producer, ranker, position_tracker)
  - end-of-trading-day, trading-day arithmetic, and trade plan validity
    window (trade_planner)

7 sites deliberately keep the wall clock, because simulated time would
make them wrong rather than right:
  - EVALUATION_DURATION latency metric (simulated time records zero)
  - the trade planner's Redis circuit-breaker backoff (Redis is a real
    server during a replay, so 15s must elapse in real seconds)
  - rules_cache config caching (rules are static across a run, and a
    simulated TTL would re-read Redis every simulated minute)

Naive vs aware is preserved exactly. clock.utcnow() returns naive UTC as a
drop-in for datetime.utcnow(), clock.now() returns aware UTC for the
trade_planner sites. Signal timestamps, last_update and entry_date are
naive throughout, so returning aware datetimes would raise TypeError at
every comparison. Tests cover both directions.

A module-level accessor is used rather than constructor injection because
the reads live in module-level functions and dataclass methods with no
constructor to thread through; threading one everywhere would be a far
larger diff across code that sizes positions and sets stops.

Production is unchanged: CLOCK_MODE defaults to real, only the exact
string "replay" switches modes, and the real path never opens a Redis
connection for the clock. Replay fails at initialize() if the driver has
not published simulated time.

Also drops 5 imports this change orphaned, and removes 61 datetime.utcnow()
deprecation warnings under Python 3.12.

NOTE: requirements.txt carries a TEMPORARY pin at the trading-py-commons
branch feat/clock-module, which is not yet pushed or tagged. This branch
will not pass CI until that release is cut.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The clock module is released, so the temporary branch pin is replaced with
the tag. Full suite (863 tests) passes against the published release.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@trogers1052
trogers1052 merged commit 4876c85 into main Jul 27, 2026
1 check passed
@trogers1052
trogers1052 deleted the feat/clock-adoption branch July 27, 2026 13:33
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.

1 participant