Skip to content

Context length: the champion chooses CONTEXT_DAYS, from backtests to the live miner - #91

Merged
Thykof merged 13 commits into
mainfrom
feat/context-days
Nov 9, 2026
Merged

Thykof merged 13 commits into
mainfrom
feat/context-days

Conversation

@Thykof

@Thykof Thykof commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Stacked on #90, which is stacked on #89: merge those first.

Why

Every champion got the 7 days of minute candles before the prompt: in its backtests, in the verdict and live. That length was a harness constant that nobody had measured. A model that wants a month of history for its volatility level, or three days for crypto-1h, could not ask for it. An agent could edit predict.py's own constant, but the verdict and the miner still passed 7 days, so its backtests measured a model that would never be served.

With this PR the champion chooses its context length the way it chooses any other parameter: by measuring it. It sets one value, and every consumer reads that same value.

What

The value. CONTEXT_DAYS is a module-level int in the champion's modeling.py, from 3 to 30. A module without it gets 7, so every archived champion keeps its context. All consumers read it through one helper, generate_predictions.context_days_of. Any other value raises everywhere. The check is type(v) is int, so True and 14.0 fail too. One value serves every competition.

Consumer What changes
agent/predict.py (scaffold) Reads the value at import, so an invalid value fails the first backtest. Prints it on each summary line, and one warning line when a context starts with NaN. Its own CONTEXT_MINUTES is gone.
Verdict generator Loads the module before the prices, and writes context_days and context_head_nan_minutes (the most minutes one of the day's contexts runs before its first real close) into each day's index JSON.
run_verdict Checks that every (competition, asset) run reports the same value. Writes a top-level context_days in verdict.json, and context_head_nan_minutes for each asset whose contexts start with NaN, which it also prints. An index written before these fields reads as 7 and 0.
Interface probe Loads the module once for both formats, hands it the context the verdict would (context_slice over CONTEXT_DAYS), and fails the check on an invalid value, with the usual hand-back. BUDGET.md reads ok (24h + 1h), CONTEXT_DAYS 14, one call 2.1 s (24h) and 0.3 s (1h).
Unpack contract gate Builds a synthetic context of CONTEXT_DAYS + 1 days (8 for every archived champion, as before). PROVENANCE.md records the value.
ChampionMiner context_days class attribute, 7 for a copied miner.py that does not set it. The generated miner sets it from modeling.py. Logged at startup.
serve_request Passes the length to build_context, which now takes context_days.

One slice offline. predict.py used to take the last N rows, which started a minute or two early. It, the generator and the interface probe now share context_slice, from t − CONTEXT_DAYS to context_end: CONTEXT_DAYS × 1440 − 2 rows for 24h and − 1 for 1h, ending before the bar labelled start_time. Serving already started its grid at t − CONTEXT_DAYS.

History shorter than the context.

  • The store holds no partition before a market's first trade, and a snapshot or a host store can also start after it. load_minute_prices now makes the days before an asset's first partition NaN on every minute, which is how a live context holds them. A missing partition after the first one still raises.
  • predict.py's coverage check exempts only that head: a missing day on or after the asset's first partition, or on or after the first prompt's day, still stops the run before any generation. Its message points at DATA.md's first and last day per asset.
  • A store that starts late shortens every context without failing, so the head is made visible: the generator records it per day, run_verdict prints and records it per asset over the window's own day files, and it warns when an asset's first partition in the data root is after the day its first context begins. The warning names the cache directory to delete once those days are ingested: cached day files keep the contexts they were generated with.
  • DATA.md, rule 7 and predict.py's docstring say a missing file is before the market's first trade, or before the snapshot's first day.

Live store and requests.

  • The warm-up and the background refresh call refresh_recent(days=CONTEXT_DAYS + 1), which covers today − (CONTEXT_DAYS + 1) through today: the context's date partitions and the day before them. WARMUP_DAYS is gone.
  • warm_up now goes through refresh_recent, so it stops at each venue's retention as the refresh already did. Before, a cold 30-day warm-up would have asked Hyperliquid for about 27 days it no longer serves, on every restart.
  • On a cold volume, after each asset's warm-up the miner refreshes the newest partitions (today − 1 and today) of the assets already served (never one still warming up), so they do not age while each Binance asset fetches a month of partitions.
  • The oldest day a venue holds only in part (Hyperliquid keeps its last 5000 minutes) is stored unsettled, for an archive ingest to complete, and is not fetched again once stored. It used to be stored as settled, and nothing ever completed it. Binance, with no retention, is unchanged. The trade-off: a day stored unsettled while the venue still held it, which then falls past the retention edge (a pod down for about 3 days), is no longer fetched from the venue either; an archive ingest completes it.
  • Each day partition is written atomically (a temporary file in the same directory, replaced into place), so a write cut short leaves the previous partition whole. write_atomic gives the file mode 0644, as a plain write did; mkstemp alone left it owner-only.
  • At startup, the miner logs the share of each asset's context that holds a real close.
  • ContextCache keeps the newest 32 built contexts, so validators sending the same prompt share one. Its key is (asset, time_length, start_time), the fresh tail's newest bar (None when the tail is empty) and the number of store refreshes marked before the build; ChampionMiner marks one after each refresh pass. A refresh or a newer tail bar landing between two validators therefore reaches the second, and validators within one snapshot still share one build. The format is in the key because a crypto-24h prompt and a crypto-1h prompt for the same asset can share a start_time, and only the 1h one carries the fresh tail. simulate() receives a copy, so a champion that mutates its input cannot change the context the next validator gets.

Text. Constitution rule 7 now describes CONTEXT_DAYS: its bounds, its default, who reads it, and the NaN days before a first trade or the snapshot's first day. The lessons that said 7 days and "contexts rougher than the snapshot" are updated to match. Both starter modeling.py files set CONTEXT_DAYS = 7.

Not in this PR.

  • verdict/generate.py and verdict/generate_cli.py are untouched. Nothing calls them, and they will be removed in a separate PR. nomination.load_simulate stays because generate_cli.py imports it.
  • There is no --context-days override in run_verdict, because it would need its own cache key.
  • A champion whose CONTEXT_DAYS is invalid at verdict time fails every generation run, and run_verdict still writes a verdict.json with score and context_days null; the reason is only in the console log. predict.py and the interface probe refuse such a value long before nomination.
  • Offline and live still differ on a context with no real close at all (a prompt on a listing day, before the first trade): offline, simulate() receives it; live, build_context raises "no usable closes" and the request fails. Both starter modeling.py files raise on such a context.
  • The warm-up order is unchanged: HYPE still warms up after the Binance assets.
  • generate_predictions.CONTEXT_MINUTES is removed. A downstream script that imports it (autoquant's scripts/measure_issuance_lead.py) moves to the champion's own length when its synth-lib pin moves past this branch.

Formatting. black 26 reformats the templates in unpack_champion.py and two helpers in test_champion_serving.py. The lint check runs on every changed file, so these files must pass it whole.

Tests

  • A champion with CONTEXT_DAYS = 14 gets 14 days from the probe, predict.py, the verdict's generator, serve_request, and the generated miner's forward_miner. The contract gate gives it 15 days.
  • A champion without the constant gets 7 from each of them, and 8 from the gate.
  • 2, 31, True and 14.0 raise in the helper, predict.py, the generator (before it reads any price), the probe (ok False, with a message naming the bounds), the contract gate and the generated miner.
  • The probe imports the champion once, and its context has D × 1440 − 2 rows for 24h and D × 1440 − 1 for 1h.
  • An asset whose first partition falls inside the context gets a NaN head and a full-length context. A missing day after the first partition raises in the loader and in predict.py's coverage check, and so does a prompt on a day before the first partition.
  • The generator records a NaN head of 0 for a covered context, and of 6 days for a 25-day context over a store that starts 6 days late. run_verdict reads the per-asset maximum over the window's own day files (another window's files in the shared cache are ignored, an index without the field reads as 0) and records it in verdict.json. It flags a store whose first partition is after the day the first context begins, and not one that covers it. predict.py prints its warning on a context that reaches before the snapshot.
  • On a venue keeping 5,000 minutes, a 30-day warm-up fetches back to today − 3. With no retention it fetches back to today − 31, and it logs the share of real closes.
  • During the warm-up, each ready asset's today partition is refreshed after every later asset's warm-up, never the asset still warming up, and a failing store does not stop the others.
  • On a venue keeping 5,000 minutes, the partly held edge day is stored unsettled and fetched once over two refreshes; with no retention the same day is settled. A write cut short leaves the stored partition whole and no temporary file behind, and a written partition has mode 0644.
  • ContextCache returns the same object for the same key, builds once for three identical requests, builds again for the other format, and evicts the oldest key. A store refresh or a newer tail bar between two requests for the same prompt reaches the second, a repeated tail does not rebuild, and each refresh pass marks the cache.
  • run_verdict reads one value across the runs, raises when runs disagree, reads an index without the field as 7, and returns None when no index exists.
  • The tests that pinned CONTEXT_MINUTES, WARMUP_DAYS, the probe's ok (24h + 1h), the probe's context length, check_snapshot_coverage's arguments and the generated miner's import are updated.

Commands:

  • uv run pytest -q: 577 passed, 1 skipped. The skip is the unpacked-champions gate, which has no champions/ directory to parametrize over.
  • uvx black@26.5.1 --check and uvx --with flake8-pyproject==1.2.3 flake8@7.1.2 on the 22 changed Python files, as CI runs them: both clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@Thykof
Thykof added this pull request to stack #92 November 9, 2026 10:48
@Thykof
Thykof force-pushed the feat/context-days branch from cf17d55 to 29dc9af Compare November 9, 2026 10:50
Base automatically changed from feat/antigravity-cli to main November 9, 2026 10:52
Thykof and others added 13 commits November 9, 2026 11:52
…he verdict

A champion sets CONTEXT_DAYS, a plain int from 3 to 30, at module level in
its modeling.py; a module without it gets 7. generate_predictions.py reads it
through one helper, context_days_of, which raises on any other value (the
check is type(v) is int, so True and 14.0 fail too).

predict.py reads it at import, so an invalid value fails the first backtest,
and prints it on each summary line. The verdict's generator loads the module
before the prices, writes context_days into each day's index, and run_verdict
checks that every (competition, asset) run reports the same value and writes
it as a top-level context_days in verdict.json.

Both offline contexts are one slice, context_slice: from CONTEXT_DAYS before
the prompt to context_end (predict.py took the last N rows, which started a
minute or two early). The days before an asset's first partition are NaN on
every minute, as a live context holds them; a missing partition after the
first still raises, and predict.py's coverage check no longer counts the
former as missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
…med per format

The probe builds its synthetic context at the champion's CONTEXT_DAYS and
fails the check, with the usual hand-back, on a value outside 3 to 30. On
success the BUDGET.md note reads "ok (24h + 1h), CONTEXT_DAYS 14, one call
2.1 s (24h) and 0.3 s (1h)": the value as the working tree sets it, and the
wall time of one call of each format at that length.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
…n, one context per prompt

ChampionMiner gains context_days, 7 by default for a miner.py that does not
set it; the generated miner sets it from modeling.py, and the miner logs it at
startup. Each request builds a context of that length, and the warm-up and the
background refresh cover CONTEXT_DAYS + 1 date partitions. The warm-up now goes
through refresh_recent, so it stops at the venue's retention as the refresh
does instead of asking Hyperliquid for days it no longer serves, and it logs
the share of each asset's context that holds a real close.

Built contexts are kept per (asset, time_length, start_time), the newest 32,
so validators sending the same prompt share one. The format is in the key
because a crypto-24h and a crypto-1h prompt for the same asset can share a
start_time. simulate() gets a copy of the cached frame.

The unpack contract gate runs on a synthetic context of CONTEXT_DAYS + 1 days
and PROVENANCE.md records the value. black 26 reformats unpack_champion.py's
templates and test_champion_serving.py, as the lint check on changed files
requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
…rter models

Rule 7 says the context spans CONTEXT_DAYS, a plain int from 3 to 30 at
module level in modeling.py (7 without it), which predict.py, the interface
check, the scoring and the live miner all read, and that whole days before an
asset's first trade are NaN. The lessons that said 7 days and "contexts
rougher than the snapshot" follow. Both starter modeling.py files set
CONTEXT_DAYS = 7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
check_snapshot_coverage takes the first prompt's time. A missing day is a
hole when it is on or after the asset's first partition, or on or after
the first prompt's day, so a window whose prompts start before the
snapshot's first day stops with the coverage error again, before any
generation, instead of handing those prompts an empty context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
…about

A context that reaches back before an asset's first partition starts with
NaN, and the store's first day is not always the market's first trade:
a snapshot or a host store that starts late shortens every context the
same way. That is now visible.

- Each day index records context_head_nan_minutes, the most minutes one of
  the day's contexts runs before its first real close
  (generate_predictions.nan_head_minutes).
- run_verdict prints, per asset, how long its contexts start with NaN,
  and verdict.json records it as context_head_nan_minutes (assets whose
  contexts all start on a real close are left out).
- run_verdict warns when an asset's first partition in the data root is
  after the day its first context begins, and names the cache directory
  to delete once the missing days are ingested: the day files keep the
  contexts they were generated with.
- predict.py prints one warning line when a context starts with NaN.
- DATA.md, the constitution and predict.py's docstring say a missing file
  is before the market's first trade, or before the snapshot's first day.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
…t shape

probe_all_shapes loads the champion's module once and hands it to
probe_simulate for both formats, so a module that does heavy work at
import pays for it once inside the probe's timeout.

The synthetic context spans [start - CONTEXT_DAYS, start] and goes through
context_slice, so the champion gets the shape predict.py and the verdict
hand it: CONTEXT_DAYS * 1440 - 2 rows for 24h and - 1 for 1h, ending
before the bar labelled start_time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
…oves on

ContextCache kept the context the first validator's request built, so a
store refresh, or a fresh tail holding a newer bar, that landed before a
later validator asked the same prompt never reached it. The key now holds
the tail's newest bar (None when the tail is empty), and the cache adds the
number of store refreshes marked before the build: ChampionMiner marks one
after each refresh pass. Validators asking a prompt within one snapshot of
the store and the tail still share one build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
On a cold volume the background refresh started only once every asset had
warmed up, and a long context makes each Binance asset's warm-up fetch a
month of partitions: the assets served first aged meanwhile. After each
asset's warm-up, the warm-up thread now refreshes today's partition of
the assets already ready (refresh_recent(days=1) on a snapshot of the
ready set, each in its own try), never one still warming up, and marks
the refresh for the cached contexts like the background pass does. The
refresh test's stdlib imports form one group.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
…once

Hyperliquid serves its last 5000 minutes, so the oldest day a refresh asks
it for can start before that edge and come back partial. ingest_day stored
it as settled, so neither the miner nor a later archive ingest, which skips
settled partitions, ever completed it. A past day that starts before the
client's retention_minutes is now stored with is_final False, and once a
partition for it exists it is not fetched again (unless force_refresh), so
the per-minute refresh does not ask for it every pass. The stored > fresh
guard is unchanged, and a client without retention (Binance) is too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
ingest_day writes the partition through write_atomic, a temporary file in
the same directory replaced into place, so a write cut short (a killed pod
mid-refresh) leaves the previous partition whole instead of a truncated
file that later reads take for the day.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
refresh_recent(days=D + 1) covers today-(D + 1) through today, the
context's D + 1 date partitions and the day before them. The warm_up and
_refresh_once docstrings and the refresh test's name said D + 1
partitions. warm_up's docstring also says the oldest day Hyperliquid holds
in part is stored unsettled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
…e is

mkstemp creates its file owner-only, so every atomically written partition
was 0600. It is now 0644.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016kmUsyYSEMPGiwbSuvR9hh
@Thykof
Thykof force-pushed the feat/context-days branch from 29dc9af to d1a92d2 Compare November 9, 2026 10:52
@Thykof
Thykof merged commit e2132de into main Nov 9, 2026
4 checks passed
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