feat(clock): read time through the clock seam - #11
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
and the newly-merged hard-gate tests)
CLOCK_MODEdefaults toreal; only the exact stringreplayswitchesmodes, so a typo cannot put production on a simulated 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 simulatedtime records zero
during a replay, so 15s must elapse in real seconds
rules_cacheconfig caching — rules are static across a run, and a simulatedTTL would re-read Redis every simulated minute
The naive/aware trap
datetime.utcnow()returns naive datetimes; signal timestamps,last_updateandentry_dateare naive throughout. An aware-returning clockwould raise
TypeErrorat every comparison. So the shim exposes exactdrop-ins:
clock.utcnow()(naive),clock.now()(aware, for the trade_plannersites that already used aware),
clock.timestamp()(float). Tests cover bothdirections.
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
mainthat nopre-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=replayacross 479 simulated trading days, firing Golden Cross / Full Trend Alignment /
Commodity Breakout and generating trade plans with stop-snapping.
🤖 Generated with Claude Code