diff --git a/.gitignore b/.gitignore index b291817f..3b1b759e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,8 @@ __MACOSX/ .AppleDouble .LSOverride -Icon[ ] +Icon[ +] # Thumbnails ._* @@ -331,3 +332,5 @@ balatrobot_oldish.lua balatro_oldish.sh balatro.sh +.launch-config-examples +reports \ No newline at end of file diff --git a/TRAINING.md b/TRAINING.md new file mode 100644 index 00000000..88a09920 --- /dev/null +++ b/TRAINING.md @@ -0,0 +1,662 @@ +# Training experiments + +## Named sandbox image + +Game sandboxes use `modal.Image.from_name("balatrobot")` in the selected Modal +environment. Publish the runtime once, and republish when its build recipe changes: + +```sh +./training/dojo.sh python batro_modal_rl.py --publish-image +./training/dojo.sh python batro_modal_rl.py --smoke-env +``` + +The build recipe stays in `batro_modal_rl.py`; no separate image file is needed. +Episode startup resolves `balatrobot:latest` without rebuilding the runtime. +Private game assets still come from the `balatro-rl-assets` volume. +Already deployed training code keeps its previous image-loading behavior; new +training deployments use the named image. + +## Define experiments in Python + +[`experiments.py`](experiments.py) is the experiment catalog. Each definition +builds a Dojo `TrainConfig` directly in code. Add variants with +`dataclasses.replace(BASE, ...)`; JSON configuration files are unnecessary. + +```sh +# List experiments, then preview locally (no remote calls or files written). +./training/dojo.sh python experiments.py +./training/dojo.sh python experiments.py thinking_8k_200 + +# Validate datasets and exact checkpoint without allocating GPUs. +./training/dojo.sh python experiments.py thinking_8k_200 --validate + +# Submit and register cloud monitoring. Reusing a trial prevents duplicates. +./training/dojo.sh python experiments.py thinking_8k_200 --trial baseline --launch +``` + +The catalog includes `proof` (1 update), `smoke` (10), `reward_to_go_200`, and +`thinking_8k_200` (200 updates with 8,192 thinking tokens). All start from +`perfect-adhesive-da14ace5a831/iter_0000399` with a fresh optimizer. These names +describe configurations, not evidence that training will succeed. + +Python callers can use the same definitions: + +```python +from experiments import EXPERIMENTS, launch + +config = EXPERIMENTS["thinking_8k_200"].build() # actual Dojo TrainConfig +run = launch("thinking_8k_200", trial="baseline") +``` + +Dojo stores the resolved training configuration with the run. The local launcher +automatically writes only a submission receipt under +`reports/experiments///receipt.json`. This records the run ID +and blocks accidental resubmission, including after an interrupted launch. +Never delete a receipt to retry an uncertain submission; inspect the remote run +first. Choose a new trial name for an intentional new experiment. + +The old `.launch-config-examples/` exports were removed. Historical launch +receipts are retained under `reports/legacy-launches/` for diagnosis; generated +evidence and receipts are not experiment inputs. `balatro_compare.py` remains +available as a lower-level builder/CLI and no longer writes `.config.json` files. + +## Modal Dojo (October 19) + +Replacement run: `overcast-groove-11909ffa4c8f`, 200 updates from +`perfect-adhesive-da14ace5a831/iter_0000399`, fresh optimizer, 8K thinking. +The former `naive-rotation-0d3fe4fb61a0` app was stopped and its zero remaining +containers verified before submission. The Dojo dashboard is +`https://modal-labs-joy-dev--dojo-dashboard-fastapi-app.modal.run`. +Launch receipt and migration evidence are in +`reports/launch-dojo-200step-20261019.json` and `reports/dojo-migration-20261019.json`. + +The active launch and monitoring code now uses `modal_dojo`; the Training Gym +checkout is no longer a dependency. Bootstrap the isolated Python 3.12 project: + +```sh +uv run --no-project --python 3.12 training/setup_dojo.py +uv sync --project training --group test +``` + +For an existing Dojo source checkout, pass `--source ../modal-dojo` to the +bootstrap command. It clones pinned revision `0bd6789d1be43c1eb10928d3d69be55de2d2533e` +into `training/.dojo` and applies `training/dojo-checkpoint-recovery.patch`. +The patch preserves strict shard validation, committed-save verification, +temporary Gloo-group cleanup, and correct optimizer/RNG restoration on retry. +It also supports a project-local configuration file and preserves the dashboard's +Modal environment. It does not edit the source checkout or global Dojo settings. + +Use `training/dojo.sh` for all training commands. It selects the pinned package, +Python 3.12, profile `modal-labs`, environment `joy-dev`, and private configuration +`training/.dojo-config.toml`. Explicit environment overrides take precedence. +The Balatro model, rewards, thinking handoff, topology, and checkpoint-volume +name remain unchanged. The new dashboard uses `modal-dojo-metadata`; historical +Training Gym records stay in their original volume. + +```sh +# Validate configuration, dataset rows, and exact checkpoint 399 without GPUs. +./training/dojo.sh python experiments.py thinking_8k_200 --validate + +# Submit once; wait for the receipt to say "submitted". +./training/dojo.sh python experiments.py thinking_8k_200 --trial baseline --launch + +# Deploy the watchdog when its code changes (launch registers runs automatically). +./training/dojo.sh modal deploy training/cloud_watchdog.py + +./training/dojo.sh modal-dojo run get RUN_ID --verbose +./training/dojo.sh python training/monitor_run.py RUN_ID --learning +``` + +The Dojo watchdog uses its own `balatro-dojo-watchdog` app/registry and metadata +volume, so it cannot confuse an old Training Gym run with a Dojo run. Dashboard +CLI reads still require the dashboard's existing authentication; failed reads +are not evidence of a training failure. + +Migration checks: 237 local harness tests, 35 checkpoint/recovery tests, 3 real +Dojo API checks, 42 viewer tests, and 75 pinned-trainer CPU tests passed. Native +CPU validation also reproduced the old process-group cleanup error and passed +three corrected saves on each of two ranks. These are compatibility checks, +not evidence of improved learning. + +## Thinking-budget handoff (October 12) + +At the 8,192-token thinking limit, the harness now inserts `` and +continues generation with a separate 96-token final-answer allowance. The +inserted token remains in the exact inference/training context, with zero +loss mask and no action advantage. Naturally sampled closing tokens retain +normal training credit. Reaching the thinking budget alone is no longer an +invalid action; malformed or length-limited final answers still receive the +existing penalties and execute no game action. + +Context reservations include up to 8,289 physical response tokens: 8,192 +sampled thinking tokens, one optional control token, and 96 sampled answer +tokens. Sampled-token percentiles exclude the inserted control token. +`thinking_forced_close_count/fraction` track handoffs, and episode metrics +separate `balatro/control_tokens` from feedback tokens. + +Validation and limitations: `reports/thinking-handoff-fix-20261012.md`. +No replacement training run was launched for this fix. + +## Thinking relaunch (October 5) + +`warped-median-9f188a4959eb` uses the same 200-update, 8,192-token thinking +recipe from `perfect-adhesive-da14ace5a831/iter_0000399` with a fresh optimizer. +Thinking runs now checkpoint every five updates and allow ten bounded whole-job +recoveries. The previous thinking run hit the 24-hour Modal invocation limit; +recovery still loads this run's latest complete checkpoint and keeps the same +200-update target. + +The parent run's metadata was unavailable while checkpoint 399 remained intact. +The comparison launcher can now locate its deterministic checkpoint volume and +validate the exact iteration directly; missing or invalid weights still fail +preflight. Checkpoint 399's shard manifest matched its predecessor. + +The dashboard endpoint was unavailable. It was redeployed with its Modal +environment preserved, and an authenticated empty metric request returned HTTP +200 before submission. The cloud watchdog was redeployed as well. Launch +checks: 74 passed, one native-image-only skip. Evidence and source hashes: +`reports/thinking-8k-provenance-20261005.json`. + +## Optional 8K thinking (October 1) + +`balatro_compare.py --thinking-budget 8192` enables the model's thinking chat +template for initial snapshots and every feedback turn. Each decision has up +to 8,192 sampled thinking tokens, including a naturally sampled closing `` token, then +up to 96 tokens for the final JSON action. The serving request pauses at the +sampled closing token and resumes with those exact IDs in its prefix. As of +October 12, exhausting the thinking budget without closing the block inserts +a masked control token and continues to the final answer (see above). + +Both reasoning and final-answer tokens retain their sampled log probabilities +and action advantage. Strict single-action validation applies to the final +answer only. Context reservations include up to 8,289 physical response tokens; +thinking runs use a 16,384-token segment target within the same 49,152-token +hard context limit. Initial checkpoint, optimizer reset, reward, topology, +held-out seeds, and 200-update horizon remain the comparison recipe's defaults. + +Diagnostics report total sampled `action_tokens_*`, plus separate +`thinking_tokens_*` and `final_answer_tokens_*` percentiles and +`thinking_budget_hit_fraction`. Watchdog length-limit and malformed-output +checks still apply to invalid responses. Long reasoning alone is not treated as +repeated-action output. + +## Single-action output contract (September 30) + +The harness now validates the entire generated response: exactly one JSON +object with `method` and `params`, followed by natural termination. Whitespace +and one terminal Qwen end token are allowed. Extra objects, prose, duplicate +keys, malformed parameters, and output-limit finishes reject the response. +Custom stop strings/tokens and `ignore_eos` are rejected at preflight so they +cannot hide trailing output. Generation remains unconstrained by a grammar. + +A rejected response executes no game action. It receives a -2 task-reward +penalty and corrective feedback against the unchanged game state. Three +consecutive rejections end the episode; every sampled attempt counts toward +the 250-decision cap. Original sampled token IDs and log probabilities remain +in training, including all rejected output. The existing invalid-action +advantage ceiling of -5 applies to its entire token span, even if a later retry +wins the game. + +Episode metadata records exact token lengths for every sampled action. +Diagnostics deduplicate context segments and report `action_tokens_mean`, +`action_tokens_p50/p90/p95/p99`, and format-invalid, multiple-object, +length-limit, and normal-stop counts/fractions under `rollout/` and +`eval/{dataset}/`. These evaluation metrics do not depend on the training loss +mask. Historical episodes without the metadata do not get fabricated zeros. + +The deployed watchdog stops immediately when at least half of a batch's +responses violate format, contain multiple objects, or hit the output limit. +It also stops after three consecutive batches with multiple-object rates at +least 5%, length-limit rates at least 25%, or format-invalid rates at least 10%. +Each qualifying batch must contain at least 32 actions. These checks operate +independently of reward and do not require a completed baseline evaluation. + +Validation: 260 local tests passed (8 dependency-related skips), and 54 tests +passed in the pinned Slime image on CPU. Replay rejected all 3,883 responses +from collapsed rollout 179 of `crispy-refresh-69af089563f9`, even assuming a +normal finish. That run remains stopped. No replacement run or GPU validation +has been launched for this change; these checks establish contract behavior, +not improved learning. Evidence: +`reports/single-action-contract-fix-20260930.md`. + +## Ten-update smoke completed (September 23) + +`inventive-groove-c49694376007` completed all ten updates from +`perfect-adhesive-da14ace5a831/iter_0000399`, saved a complete +`iter_0000009`, and finished final evaluation on its first attempt. The recipe +requested 64 H100s with TP4/PP2/CP4; the inspected head node received eight +H200s. Model, sampling correction, reward, context, and parallelism match the +one-update proof. No production code changed for this test. + +On the same 32 greedy held-out seeds, mean reward improved **67.6030 → +88.4506 (+30.8%)**, mean ante **3.1875 → 3.65625**, and invalid actions +**54/2,460 → 27/2,869 turns**. Reward increased on 22 seeds, decreased on +eight, and was unchanged on two. Both evaluations had **0/32 wins** and a +best ante of six. The ten training updates kept trainer/rollout probability +disagreement between **0.00281 and 0.00444**, with gradient norms between +**1.37 and 1.95**. Audits cover every generated action in all ten batches; +none showed the previous one-card collapse. + +The 250-decision cap truncated two viable training games in batch six +(zero-based): one at ante seven and one at ante eight, each with only one +invalid action. Neither counts as a win. This is a demonstrated evaluation +limitation to address before interpreting the win rate or promoting a longer +run. Ten stable updates do not establish stability over 200 updates; the +historical collapse occurred later. + +The run took about two hours including startup, checkpointing, and evaluation. +Its Modal app is stopped, with zero active GPU containers verified. +Evidence: `reports/unconstrained-smoke-20260923.json`, per-batch trace audits, +384 episode summaries, optimizer metrics, and checkpoint validation. + +## Original 27B trainer restored (September 22) + +The JEV experiment has been removed. `batro_modal_rl.py` and +`balatro_compare.py` remain the active Qwen3.8-27B training path, using the +existing game reward, checkpoint recovery and trajectory viewer. + +The grammar correction below is retained. Launch preflight and episode entry +now reject sampling settings this harness cannot reproduce in training: +grammar constraints, top-p/top-k/min-p truncation, logit bias/processors and +non-default repetition/presence/frequency penalties. Training explicitly uses +temperature 1, top-p 1 and top-k -1; greedy held-out evaluation remains allowed. +Invalid settings fail before checkpoint pinning, receipt creation or GPU +submission. Missing/nonfinite serving log probabilities fail rather than +silently entering a training sample. + +The 64-H100 proof `spatial-hot-dog-47bd4700e214` completed one optimizer +update from parent checkpoint 399. Trainer/serving log-probability disagreement +was 0.00433 and gradient norm was 1.951; checkpoint `iter_0000000` passes +distributed shard validation. Its Ray control-plane connection failed after +the save, exhausting all five deduplicated RPC retries before final evaluation. +These results validate one corrected update, not long-term learning stability. + +Proof and smoke configs now allow the same two whole-job retries as full runs. +The checkpoint image patch also evaluates restored final weights when the +last checkpoint leaves no optimizer updates to run. Previously that recovery +path could exit without the interrupted final evaluation. Tests cover explicit +evaluation-only runs, final-checkpoint recovery, and normal mid-run recovery. + +The interrupted 32-seed final evaluation completed separately as +`knurled-kasseri-e6a606c5b791`, loading proof checkpoint 0 with zero additional +training updates. Its evaluation-only allocation is one node of 8 H100s, +TP4/PP2/CP1 (serving TP2), with the same 49,152-token context and seeds. +The earlier 64-GPU evaluation `covering-backpack-f4fc25b5d62f` was stopped +while queued for capacity. Mean held-out reward was **70.5175 before** and +**70.4757 after** the update. Both evaluations averaged ante 3.1875, reached +ante 6, and had 0/32 wins. This is a correctness proof, not evidence of learning +improvement. All five apps are stopped with no active GPU containers. + +Evidence: `reports/unconstrained-gpu-proof-20260922.json`, the training trace +audit, per-game evaluation logs, checkpoint validation, and final app states. +Regression checks: 134 passed, 5 dependency-related skips. + +`build_checkpoint_evaluation` keeps a positive scheduler horizon with an empty +training range (`start_rollout_id=num_rollout=1`); Slime's nominal +`num_rollout=0` evaluation mode otherwise trips Megatron's scheduler assertion. +Comparison recipes explicitly package `balatro_compare.py` so imported +recovery launchers can deserialize the held-out dataset on workers. + +## Generation/training probability correction (September 21) + +`arid-gate-422819ed43e1` was stopped after 60 updates for sustained policy +collapse. Fixed-seed reward rose from 64.61 to 79.46 at update 20, then fell to +60.02, 43.41, 4.50, and -0.18 at updates 30/40/50/60. Every game in the last +eight training batches ended at ante 1. All 124 generated play actions in +batch 59 selected one card. Checkpoints 9 through 59 saved; this was a policy +failure, not a repeat of the checkpoint RPC crash. + +The pinned SGLang image applies the regex vocabulary mask before computing +returned log probabilities. The Megatron loss does not replay that mask. +The harness therefore sampled a constrained policy while optimizing an +unconstrained one. Trainer/rollout log-probability disagreement grew from +about 0.003 to 28.43. PPO used recomputed trainer probabilities for its old +policy; its clipping metric stayed zero and did not expose this discrepancy. + +Generation now omits the regex and rejects inherited regex/JSON-schema/EBNF +constraints. Prompts, parsing, action validation, repair, and reward penalties +remain in effect. Reintroducing constrained decoding requires matching mask +replay in the trainer, not just changing the old-logprob flag. Local regression +checks pass; the September 22 proof and September 23 smoke results above +provide GPU validation and matched baseline evaluations. The exact contribution of +this bug versus other optimization choices is not yet established by ablation. + +Evidence: `reports/arid-gate-collapse-diagnosis-20260921.json`, saved traces, +and the exact pinned implementation in `reports/pinned-probability-source/`. +Do not resume the collapsed checkpoint. Checkpoint 19 is the best saved +held-out result; the original parent 399 is also available for a clean comparison. + +## Pipeline communication correction (September 17) + +The pinned Slime image drops the `group` argument from all four `P2POp` +constructors in Megatron's `_batched_p2p_ops`. Payload transfers therefore use +the default WORLD group, while shape exchanges use the intended pipeline +subgroup. `nearest-dialogue-25c4f89a4658` timed out in this payload path on +WORLD during its first reference forward pass, before any optimizer update. + +`training/patch_pipeline_group.py` restores the subgroup argument, matching +upstream Megatron. Both Balatro credit modes install it at image build; the +patch rejects unfamiliar source and is safe to apply again. It does not +change the topology, checkpoint, context limits, reward, or optimizer settings. + +`tests/test_pipeline_group.py` exercises the pinned image's transfer function. +The bounded four-GPU regression in `training/pipeline_group_modal.py` checks +independent pipeline pairs and repeated Slime process-group reloads without +loading a model or playing games. This isolates communication correctness; +it does not measure training progress or establish that every cause of the +original 64-GPU hang has been eliminated. + +## Context and action-credit correction (September 16) + +The recipe now explicitly sets both `max_position_embeddings` and +`sglang_context_length` to **49,152**, with an **8,192** working-history target. +The previous run inherited `max_position_embeddings=4096`, silently forcing +much more frequent history resets. Local launch validation requires explicit +limits; workers reject a trainer/serving limit below the requested hard budget +before acquiring a game. The 49k hard allowance permits large snapshots; +normal histories still reset around 8k. + +Two experiment modes keep the context change separable from credit assignment: + +- `--credit-assignment episode` (default): corrected context, existing game-level GRPO. +- `--credit-assignment reward_to_go`: corrected context plus per-action returns. + +Reward-to-go records the reward accumulated **before** every generated action. +Each action's remaining return is the final reward minus that prior total, +including terminal bonuses and penalties. A leave-one-game-out baseline uses +the other games on the same seed at the same decision number; ended peers +have zero remaining return. The scale is the standard deviation of complete +game rewards with a floor of one reward unit. This is a variance-reduction +baseline, not a comparison between identical game states or a score oracle. + +The new mode does not sum overlapping dashboard checkpoint events. It retains +the current reward components and shared-game loss denominator. Advantages +cover only the original generated action tokens; state-feedback tokens remain +masked out. An earlier success therefore cannot directly reward a later bad +move. Earlier moves still receive credit for future outcomes, so this alone +does not guarantee that a missed flush or a poor purchase gets penalized. + +`training/balatro_credit.py` installs the native custom-advantage hook and a +small transport patch for the pinned Slime image: training metadata must survive +data-parallel partitioning and be excluded from numeric metric aggregation. +Unsupported source, missing metadata, token gaps/overlaps and mismatched +context-parallel slices fail explicitly instead of falling back to scalar GRPO. + +CPU-only preflight, from the Training Gym Python 3.12 environment: + +```sh +uv run python /Users/joyliu/balatrobot/batro_modal_rl.py --num-rollout 1 +uv run python /Users/joyliu/balatrobot/batro_modal_rl.py --num-rollout 1 \ + --credit-assignment reward_to_go +``` + +Neither command launches GPUs. Existing runs keep their deployed code. For a +training experiment, use fresh run IDs and the same initial weights and seeds: +prove one update, then compare roughly ten updates per mode with matched +held-out evaluations. Inspect effective context limits, actual token advantages, +win rate, final ante, and scoring decisions before promoting a longer run. +Preserving an old optimizer is not a requirement for a changed estimator. + +CPU integration tests can use Slime sources extracted from the pinned image: + +```sh +BALATRO_SLIME_SOURCE=/path/to/extracted/slime uv run --with torch \ + pytest tests/test_action_credit.py tests/test_context_management.py -q +``` + +Those tests execute the pinned converter, DP transport, native advantage hook, +CP slicing and per-game reducer with one and four context-parallel ranks, using +CPU tensors. They establish signal plumbing and gradient weighting, not model +improvement or a successful distributed GPU update. + +## Broad-layover recovery fixes + +The local Training Gym checkout now validates every shard and required byte +range referenced by a checkpoint's `.metadata`, both in public checkpoint +listing and automatic Slime recovery. An invalid latest checkpoint triggers +a search for an older valid checkpoint. The launcher pins that exact actor +iteration and keeps reference-model loading independent of it. + +Checkpoint saving coordinates volume commits over Gloo. After all node +commits, the coordinator reloads the committed view, validates its shards, +and publishes `training_gym_complete.json`. Legacy checkpoints remain usable +when their metadata and byte ranges validate; the marker alone is never +treated as proof of completeness. + +This Balatro recipe sets `NCCL_RAS_ENABLE=0` to avoid the RAS peer-update +subsystem in broad-layover's native crash stack. This is a scoped workaround, +not a claim that the underlying NCCL library defect was repaired. NVIDIA +documents the switch in its [NCCL environment reference](https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/env.html#nccl-ras-enable). + +`broad-layover` checkpoint 449 has 16 missing shards and another 16 truncated +shards. It cannot be repaired by retrying or by creating a completion marker. +The validator rejects it without modifying its data. + +Decision snapshots now include `hand_groups`: visible matching ranks, suit +groups, and example standard straights using current indices. Hidden and +Stone cards do not contribute ranks. These are recognition aids, not score +predictions or action replacements. Joker-specific scoring and hand rules +still matter. Two recorded avoidable losses are permanent fixtures in +`tests/fixtures/broad_layover_decisions.json`. + +The new code needs a fresh run/image to take effect. CPU tests and validation +of stored checkpoints do not establish improved model performance or prove +multi-node save/reload. Compare fixed seeds and the recorded decision +fixtures in a short experiment before extending training. + +## Bounded game context + +The harness now starts a fresh conversation from current game state when the +active conversation would exceed `balatro_segment_tokens` (default **8,192**). +The same game continues. Each conversation is a separate training sample; +all samples share the game's `rollout_id` and final reward. The recipe uses +Slime's per-game loss reduction (`calculate_per_token_loss=False`). + +Historical recipes such as perfect-adhesive used per-token weighting (`True`) +and must override it to `False` when paired with this segmented harness. +`_launch_training` validates this requirement before creating a receipt, +pinning a checkpoint, or allocating GPUs. The worker repeats the same check. + +`balatro_context_len` remains **49,152**. Actual request limits also account +for serving capacity, training padding and the next action. An essential +snapshot that cannot fit reports `state_budget_exceeded`; it is never silently +truncated. A snapshot larger than the 8k working target can use the available +hard-limit space. + +Before the next launch, run `python batro_modal_rl.py --prepare-assets` in the +Training Gym Python 3.12 environment to publish the updated Lua snapshot +fields. Older assets are rejected as `state_schema_mismatch`. The harness +checks trainer support before starting a game. The pinned Training Gym image +was checked on CPU for shared-game loss reduction across unequal segments. + +`balatro/context_tokens` now means **peak active conversation size**. +`balatro/episode_archive_tokens` measures accumulated initial-prompt/action/ +feedback tokens, excluding repeated reset snapshots; `segment_context_tokens` +measures each training sequence. Game-level metrics are emitted once on the +first segment. Reward events include segment-local token offsets and mappings +for checkpoints spanning several segments. + +Each segment has its own snapshot and trajectory messages for dashboard replay. +The first sample also carries `balatro_episode_archive` for full training/debug +serialization. Training Gym's generic dashboard metadata size cap can omit that +large archive; use the individual segments there. Raw training `tokens` and +`response` contain only accepted context; trajectory messages also record the +final state update when it was too large to include in that context. + +CPU regression checks: + +```sh +uv run pytest tests/test_context_management.py tests/test_training_launch.py \ + tests/test_joker_scoring_state.py tests/test_pack_legality.py -q +``` + +## Launching and resuming + +Run `batro_modal_rl.py` through `./training/dojo.sh python` to use the pinned +Modal Dojo dependencies in **Python 3.12**, matching the remote image. Keep `colocate=True` in the recipe. + +Validate a one-rollout checkpoint continuation without allocating GPUs: + +```sh +python batro_modal_rl.py --resume-run nullary-bezier-022caf585c3e \ + --resume-iteration 199 --num-rollout 201 --save-interval 1 +``` + +Slime resumes at checkpoint iteration + 1; `--num-rollout` is an exclusive +upper bound, not the number of additional rollouts. From checkpoint 199, +target 200 does no work, 201 performs one rollout, and 400 performs 200. + +To submit, add `--launch --launch-record /absolute/path/to/new-launch.json`. +Keep the local command alive until it prints `Modal Dojo run id` and the +receipt says `submitted`, with both a Modal app ID and function-call ID. +After that, Modal Dojo's detached app outlives the local process. + +Do not use an auto-restarting supervisor for the submission command. Reuse +the same receipt path if an existing invocation is restarted: its exclusive +creation prevents duplicate submissions. A receipt left at `submitting` +means the outcome is uncertain, not that retrying is safe. Check the launch +log and Modal Dojo run before deliberately choosing a new receipt path. + +A dashboard cancellation reason of `stale_pre_active_no_modal_app` means +the run record never acquired a persisted Modal app ID before the stale-run +cleanup timeout. It is not evidence of a GPU initialization failure. Look +at the **local submission log**; remote training may never have been spawned. + +CPU-only regression checks: + +```sh +uv run pytest tests/test_training_launch.py -q +``` + +### Checkpoint RPC recovery + +For the current Balatro training workflow, the user has authorized launching +replacement runs, continuing to monitor them, diagnosing failures, and trying +repairs (2026-09-21). Keep supervising each run through a terminal result; +reassess held-out performance as well as infrastructure health. A background +log collector alone does not provide agent diagnosis or repair. + +The persistent launcher package is `training/.dojo`, created by +`training/setup_dojo.py` with the pinned revision and recovery patch described +above. The source `../modal-dojo` checkout stays untouched. Balatro owns its sandbox lifecycle helpers and pins +the existing H100/TP4/PP2 topology explicitly because current main no longer +provides the branch-only environment helpers or the old model preset defaults. +FlashAttention is pinned explicitly: main's default unfused backend does not +support our CP=4 setup. Local launch validation rejects that incompatible pair. +The patch preserves main's optional `common.pt` support and lazy Modal imports. +It also clears the parent's +fresh-finetune/RNG flags when retrying from the run's own checkpoint, so +optimizer and RNG restoration are not accidentally suppressed. The 200-step +comparison configuration allows two whole-job retries as a fallback; each +retry retains the original 200-step horizon. + +The Balatro training image installs `training/patch_checkpoint_rpc.py` and +`training/checkpoint_rpc.py`. Checkpoint saves now retry an unavailable Ray +worker RPC up to five times, with delays of 2, 4, 8, 16, and 30 seconds. The +whole save has a 30-minute deadline. + +Each save has one operation ID shared across all workers and retries. A worker +records completion only after its original `save_model` returns, including +checkpoint commit and model offload. If its reply is lost, it acknowledges the +same operation without repeating the save or its distributed collectives. A +request lost before execution can run on retry. Successful peers are not +resubmitted. Workers remain single-threaded with in-place restarts disabled, +so their completion records have the same lifetime as their model state. + +This recovery applies to `ActorUnavailableError` at the RPC boundary. Save +exceptions, confirmed worker death, exhausted retries, and deadline expiry +remain failures requiring whole-group recovery. It does not change the +launcher's whole-job retry policy or restart existing runs. + +Run the local regression tests: + +```sh +uv run --group test pytest tests/test_checkpoint_rpc.py tests/test_pipeline_group.py tests/test_training_launch.py -q +``` + +The integration check runs two real Ray workers with CPU Gloo collectives in +the pinned Slime image. It injects lost requests/replies at the client boundary, +verifies each save executes once, verifies the next collective still works, +and checks that application errors, worker death, and exhausted retries fail. +Run from the Python 3.12 Modal Dojo checkout used for launches: + +```sh +./training/dojo.sh modal run training/checkpoint_rpc_modal.py \ + --out /Users/joyliu/balatrobot/reports/checkpoint-rpc-integration.json +``` + +### Strong invalid-action penalties + +The default `reward_to_go` mode assigns rejected model outputs an advantage +of **−5 or more negative**, applied after game-reward normalization. Large +reward variance and successful harness repairs cannot shrink or reverse that +penalty. More negative task credit is retained. Valid-action credit, displayed +game scores, and the exclusion of positive repair rewards are unchanged. + +Both launchers accept `--invalid-action-weight` (default `5`), recorded as +`balatro_invalid_action_weight` in the recipe. This is a magnitude in training +advantage units, not raw game-score points. `normalize_advantages=True` is +rejected at launch and inside the trainer because recentering could weaken or +reverse the penalty. Explicit `--credit-assignment episode` (or comparison +`--mode episode`) selects the legacy control without action-level penalties. + +The pinned-trainer CPU integration tests verify token transport, CP=1/4 +reduction, and a gradient that lowers rejected-output probability. A replay of +179 recorded invalid decisions changed their median penalty magnitude from +0.058 to 5.0. This verifies the training signal; improved gameplay still needs +a new controlled training test. + +## Cloud monitoring (September 25) + +`training/cloud_watchdog.py` is deployed as `balatro-training-watchdog` in +`joy-dev`. Its scheduled CPU function runs every two minutes and reads Training +Gym's metadata volume directly, so laptop sleep does not interrupt monitoring. +Only runs explicitly registered in the `balatro-training-watchdog` Modal Dict +are monitored; each registration pins the exact training app ID. Snapshots and +stop decisions are also persisted in `balatro-training-watchdog-evidence`. + +Automatic stop rules are tested in `tests/test_watchdog_policy.py`: + +- Nonfinite loss, gradient norm, probability-gap metric, or held-out reward. +- Two consecutive held-out evaluations more than 20% below baseline. +- After at least 50 updates, the latest three held-out evaluations are at or + below baseline. + +Read failures never trigger a stop. The watchdog records game-level training +reward, ante, wins, and invalid-action rate without counting context segments +as separate games. It does not change the training recipe or relaunch jobs. +A continue decision is not evidence of learning. Check the Dict's `stop_reason` +when interpreting a terminated run: the training driver's status can report +failure after an intentional watchdog stop. The watchdog does not overwrite +the driver's metadata record. + +The active registration and deployment verification for each run are in its +`reports/-submission.json` and cloud-watchdog evidence files. +# Action contract and reporting correction (2026-09-28) + +The September 25 audit found 51 engine-rejected consumable actions advertised +as legal. `_consumable_available` now shares the joker-capacity gate between +the prompt and Python validator for Judgement, Soul, and Wraith, and retains +Ankh's existing-joker requirement. Recorded failures are regression fixtures; +selling a joker must restore availability. Shop and hand hints also exclude +eternal jokers from sale. The engine remains the final authority on legality. + +Context protocol 2 frames continued observations as user turns through the +actual tokenizer chat template, with the same assistant prefix as fresh +snapshots. A truncated assistant answer gets a masked end-of-turn marker. +Generated action IDs/log probabilities are preserved exactly; feedback and +template tokens have zero loss mask. This fixes the format inconsistency; +reduced malformed-output rates still require validation on new trajectories. + +`training/balatro_diagnostics.py` reports game-deduplicated reward and ante +percentiles (p10/p25/p50/p75/p90/p95), wins, and invalid-action rates during +training and evaluation. `rollout/raw_reward` is the game mean. The trainer's +old segment average is explicitly named `rollout/segment_weighted_raw_reward`. +Sampling/training gap tail fractions above .01, .05, and .1 supplement the mean. +PPO ratio metrics are named `pre_update_ppo_kl` and `pre_update_pg_clipfrac`: +they do not measure policy movement after the optimizer update. + +These changes retain the stronger invalid-action floor, original reward +function, reward-to-go baseline, and exact unmasked sampling distribution. +State-conditioned credit and post-update policy-drift measurement remain +separate experiments rather than unvalidated algorithm changes in this run. diff --git a/balatro_modal.py b/balatro_modal.py new file mode 100644 index 00000000..7c129d14 --- /dev/null +++ b/balatro_modal.py @@ -0,0 +1,530 @@ +"""Package Balatro + BalatroBot into a Modal image. + +The Balatro game itself is *not* redistributable, so it is never baked into a +public layer: the ``.love`` archive is read from your local Steam install at +build time and copied into your own private Modal image. + +The image runs the game headless on Linux via the native LOVE launcher +(``love `` with ``liblovely.so`` LD_PRELOADed), which is the +combination BalatroBot's ``NativeLauncher`` expects. + +Usage:: + + modal run balatro_modal.py # build + smoke test + modal run balatro_modal.py::demo_run # start a run, print gamestate + +From training-gym (or any other Modal app):: + + from balatro_modal import balatro_image, BalatroEnv +""" + +from __future__ import annotations + +import os +from pathlib import Path + +import modal + +# --- Pinned dependency versions ------------------------------------------- + +LOVE_VERSION = "11.5" # the LOVE version Balatro is built against +LOVELY_VERSION = "0.9.0" # >= 0.8.0 required by BalatroBot +SMODS_VERSION = "1.0.0-beta-1814a" # >= 1.0.0-beta-1221a required by BalatroBot + +# --- Paths inside the image ----------------------------------------------- + +GAME_DIR = "/opt/balatro/game" # unpacked Balatro.love +MODS_DIR = "/opt/balatro/Mods" # smods + balatrobot +LOVELY_SO = "/usr/local/lib/liblovely.so" +LOVE_APPIMAGE_DIR = "/opt/love" # extracted upstream AppImage +# Lovely derives its Mods directory from the *name* of the running executable +# (`/root/.local/share//Mods`). +LOVE_BIN = "/opt/balatro/bin/love" +LOVE_BIN_NAME = "love" +LOVE_WRAPPER = "/usr/local/bin/balatro-love" # love + on-demand Xvfb +LOGS_DIR = "/tmp/balatrobot-logs" +# LOVE derives its save directory from the game's identity, which Balatro's +# conf.lua leaves unset - it relies on being a fused build. `conf.lua` is +# patched below so the location is the same one a real install uses. +SAVE_IDENTITY = "Balatro" +SAVE_DIR = f"/root/.local/share/love/{SAVE_IDENTITY}" + +REPO = Path(__file__).parent + +# --- Locating the local Balatro.love -------------------------------------- + +# `Balatro.love` is a plain zip of Lua + assets and is platform independent, so +# the macOS copy runs fine in the Linux image. +LOVE_ARCHIVE_CANDIDATES = ( + Path.home() + / "Library/Application Support/Steam/steamapps/common/Balatro" + / "Balatro.app/Contents/Resources/Balatro.love", + Path.home() / ".local/share/Steam/steamapps/common/Balatro/Balatro.love", + Path.home() / ".steam/steam/steamapps/common/Balatro/Balatro.love", +) + + +def find_balatro_love() -> Path: + """Locate the local Balatro.love archive. + + Override with ``BALATRO_LOVE_ARCHIVE=/path/to/Balatro.love``. + """ + if override := os.environ.get("BALATRO_LOVE_ARCHIVE"): + path = Path(override).expanduser() + if not path.is_file(): + raise FileNotFoundError(f"BALATRO_LOVE_ARCHIVE does not exist: {path}") + return path + + for candidate in LOVE_ARCHIVE_CANDIDATES: + if candidate.is_file(): + return candidate + + searched = "\n ".join(str(c) for c in LOVE_ARCHIVE_CANDIDATES) + raise FileNotFoundError( + "Could not find Balatro.love. Install Balatro via Steam, or set " + f"BALATRO_LOVE_ARCHIVE.\nSearched:\n {searched}" + ) + + +SAVE_DIR_CANDIDATES = ( + Path.home() / "Library/Application Support/Balatro", # macOS + Path.home() / ".local/share/love/Balatro", # Linux +) + + +def find_balatro_save() -> Path: + """Locate the local Balatro save directory. + + Balatro crashes on a profile that has never been through its first-run + setup (`game.lua`: `G.SETTINGS.tutorial_progress` is nil at the main menu), + so the container starts from the same `settings.jkr` and profile a real + install has. Override with ``BALATRO_SAVE_DIR``. + """ + if override := os.environ.get("BALATRO_SAVE_DIR"): + path = Path(override).expanduser() + else: + path = next( + (c for c in SAVE_DIR_CANDIDATES if (c / "settings.jkr").is_file()), + None, + ) + + if path is None or not (path / "settings.jkr").is_file(): + searched = "\n ".join(str(c) for c in SAVE_DIR_CANDIDATES) + raise FileNotFoundError( + "No Balatro save data found. Launch Balatro once so it creates a " + f"profile, or set BALATRO_SAVE_DIR.\nSearched:\n {searched}" + ) + return path + + +# --- Image ---------------------------------------------------------------- + +# Lovely resolves the Mods directory from LOVE's save/config directories, which +# vary with the game's identity; symlink every candidate at the one real dir. +_MOD_DIR_LINKS = ( + f"/root/.local/share/{LOVE_BIN_NAME}/Mods", # what Lovely actually uses + "/root/.config/love/Mods", + f"{GAME_DIR}/Mods", +) + +# LOVE needs a GL context even when BalatroBot hides the window, so bring up a +# throwaway X server (software rendering) on first launch. Xvfb itself must not +# inherit LD_PRELOAD=liblovely.so - Lovely only belongs in the game process. +_LOVE_WRAPPER_SCRIPT = f"""#!/bin/sh +set -e + +# Lovely belongs in the game process only - not in this shell, Xvfb, or any +# helper it spawns. +LOVELY_PRELOAD="$LD_PRELOAD" +unset LD_PRELOAD + +DISPLAY_NUM="${{BALATRO_DISPLAY:-99}}" +if ! xdpyinfo -display ":$DISPLAY_NUM" >/dev/null 2>&1; then + Xvfb ":$DISPLAY_NUM" -screen 0 1280x720x24 -nolisten tcp >/tmp/xvfb.log 2>&1 & + i=0 + while [ "$i" -lt 100 ]; do + xdpyinfo -display ":$DISPLAY_NUM" >/dev/null 2>&1 && break + i=$((i + 1)) + sleep 0.1 + done +fi + +export DISPLAY=":$DISPLAY_NUM" +export LD_LIBRARY_PATH="{LOVE_APPIMAGE_DIR}/lib:${{LD_LIBRARY_PATH:-}}" +export LD_PRELOAD="$LOVELY_PRELOAD" +exec {LOVE_BIN} "$@" +""" + +_base_image = ( + # Debian trixie ships LOVE 11.5, the version Balatro is built against. + modal.Image.from_registry("debian:trixie-slim", add_python="3.12") + # `debian:*-slim` drops man pages via a dpkg path-exclude, but LOVE's + # postinst registers `love-11.5.6.gz` with update-alternatives and fails + # when it is missing. Keep man pages for packages installed here. + .run_commands( + "sed -i '/path-exclude.*man/d' /etc/dpkg/dpkg.cfg.d/* 2>/dev/null || true", + "rm -f /etc/dpkg/dpkg.cfg.d/docker", + ) + .apt_install( + "love", # not used directly - pulls in LOVE's runtime dependencies + "xvfb", # virtual display + "x11-utils", # xdpyinfo, used to wait for Xvfb + "libgl1-mesa-dri", # llvmpipe software GL + "ca-certificates", + "curl", + "git", + "unzip", + "procps", + ) + # Upstream LOVE build. Debian's `love` statically links PhysFS without + # exporting its symbols, and Steamodded's nativefs resolves PHYSFS_* through + # LuaJIT's FFI at runtime - it only works against the upstream build. + .run_commands( + "curl -fsSL -o /tmp/love.AppImage https://github.com/love2d/love/releases" + f"/download/{LOVE_VERSION}/love-{LOVE_VERSION}-x86_64.AppImage", + "chmod +x /tmp/love.AppImage", + "cd /tmp && ./love.AppImage --appimage-extract >/dev/null", + f"mv /tmp/squashfs-root {LOVE_APPIMAGE_DIR}", + "rm -f /tmp/love.AppImage", + ) + # Lovely Injector + .run_commands( + "curl -fsSL -o /tmp/lovely.tar.gz " + "https://github.com/ethangreen-dev/lovely-injector/releases/download/" + f"v{LOVELY_VERSION}/lovely-x86_64-unknown-linux-gnu.tar.gz", + "tar xzf /tmp/lovely.tar.gz -C /tmp", + f"install -m 755 /tmp/liblovely.so {LOVELY_SO}", + "rm -rf /tmp/lovely.tar.gz /tmp/liblovely.so", + ) + # Steamodded + Mods directory layout + .run_commands( + f"mkdir -p {MODS_DIR} {GAME_DIR} /opt/balatro/bin " + f"/root/.config/love /root/.local/share/{LOVE_BIN_NAME}", + # The AppImage's own layout varies by release; pin a stable path to it. + f"ln -sfn $(find {LOVE_APPIMAGE_DIR} -type f -name love -perm -u+x | head -1)" + f" {LOVE_BIN}", + "git clone --depth 1 --branch " + f"{SMODS_VERSION} https://github.com/Steamodded/smods.git {MODS_DIR}/smods", + *(f"ln -sfn {MODS_DIR} {link}" for link in _MOD_DIR_LINKS), + ) +) + + +def _add_local_content(image: modal.Image) -> modal.Image: + """Layers built from files on this machine: the game and this repo. + + Guarded by `modal.is_local()` below - containers re-import this module with + the image already built, and neither the Steam install nor the repo is + present there. + """ + return ( + image + # The game itself, from the local Steam install. NativeLauncher runs + # `love `, so the archive is expanded rather than passed as .love. + .add_local_file(find_balatro_love(), "/opt/balatro/Balatro.love", copy=True) + .run_commands( + f"unzip -q /opt/balatro/Balatro.love -d {GAME_DIR}", + # A packaged Balatro binary is "fused", which implicitly gives it + # the Balatro save identity. Running the portable .love directory + # does not, so set the same identity explicitly or settings.jkr is + # never found and the main menu crashes on tutorial_progress. + f"sed -i \"/t.title = 'Balatro'/a\\\\\\tt.identity = '{SAVE_IDENTITY}'\" " + f"{GAME_DIR}/conf.lua", + "rm /opt/balatro/Balatro.love", + ) + # A real install's settings + profile, so the game boots past its + # first-run tutorial state. + .add_local_file( + find_balatro_save() / "settings.jkr", f"{SAVE_DIR}/settings.jkr", copy=True + ) + .add_local_file( + find_balatro_save() / "1/profile.jkr", + f"{SAVE_DIR}/1/profile.jkr", + copy=True, + ) + .add_local_file( + find_balatro_save() / "1/meta.jkr", f"{SAVE_DIR}/1/meta.jkr", copy=True + ) + # BalatroBot: the Lua mod... + .add_local_file( + REPO / "balatrobot.json", + f"{MODS_DIR}/balatrobot/balatrobot.json", + copy=True, + ) + .add_local_file( + REPO / "balatrobot.lua", + f"{MODS_DIR}/balatrobot/balatrobot.lua", + copy=True, + ) + .add_local_dir(REPO / "src/lua", f"{MODS_DIR}/balatrobot/src/lua", copy=True) + # ...and the Python package that launches and drives the game + .add_local_dir(REPO / "src/balatrobot", "/opt/balatrobot/balatrobot", copy=True) + ) + + +balatro_image = _base_image +if modal.is_local(): + balatro_image = _add_local_content(balatro_image) + +balatro_image = ( + balatro_image.pip_install("httpx", "typer") + # LOVE launcher wrapper + .run_commands( + f"cat > {LOVE_WRAPPER} <<'BALATRO_LOVE_EOF'\n{_LOVE_WRAPPER_SCRIPT}\nBALATRO_LOVE_EOF", + f"chmod +x {LOVE_WRAPPER}", + ) + .env( + { + "PYTHONPATH": "/opt/balatrobot", + "BALATROBOT_PLATFORM": "native", + "BALATROBOT_BALATRO_PATH": GAME_DIR, + "BALATROBOT_LOVE_PATH": LOVE_WRAPPER, + "BALATROBOT_LOVELY_PATH": LOVELY_SO, + "BALATROBOT_LOGS_PATH": LOGS_DIR, + "BALATROBOT_HEADLESS": "1", + "BALATROBOT_FAST": "1", + "BALATROBOT_AUDIO": "0", + "BALATROBOT_NO_SHADERS": "1", + "LIBGL_ALWAYS_SOFTWARE": "1", + "SDL_AUDIODRIVER": "dummy", + # LOVE plays audio through OpenAL Soft, which ignores + # SDL_AUDIODRIVER and otherwise fails on a container's absent ALSA. + "ALSOFT_DRIVERS": "null", + } + ) +) + + +app = modal.App("balatro-env", image=balatro_image) + + +# --- Environment ---------------------------------------------------------- + + +@app.cls(image=balatro_image, timeout=60 * 60, scaledown_window=60) +class BalatroEnv: + """One long-lived Balatro process, driven over BalatroBot's JSON-RPC API. + + This is the unit a rollout worker holds: `call()` maps 1:1 onto the API's + endpoints (`start`, `play`, `discard`, `gamestate`, ...). + """ + + @modal.enter() + def start_game(self) -> None: + import asyncio + + from balatrobot.manager import BalatroInstance + + self._loop = asyncio.new_event_loop() + self._instance = BalatroInstance() + self._loop.run_until_complete(self._instance.start()) + + @modal.exit() + def stop_game(self) -> None: + self._loop.run_until_complete(self._instance.stop()) + self._loop.close() + + @modal.method() + def call(self, method: str, params: dict | None = None) -> dict: + """Call a BalatroBot endpoint and return its JSON-RPC `result`.""" + import httpx + + payload = { + "jsonrpc": "2.0", + "method": method, + "params": params or {}, + "id": 1, + } + response = httpx.post( + f"http://127.0.0.1:{self._instance.port}", json=payload, timeout=60.0 + ) + data = response.json() + if "error" in data: + raise RuntimeError(f"{method} failed: {data['error']}") + return data["result"] + + @modal.method() + def game_log(self, tail: int = 40) -> str: + """Tail the game's log file - the first place to look when a call fails.""" + log_path = self._instance.log_path + if log_path is None or not log_path.exists(): + return "(no log file)" + return "\n".join(log_path.read_text().splitlines()[-tail:]) + + +# --- Smoke tests ---------------------------------------------------------- + + +@app.function(image=balatro_image, timeout=600) +def check_image() -> dict[str, str]: + """Verify the image layout without launching the game.""" + import subprocess + + def sh(cmd: str) -> str: + return subprocess.run( + cmd, shell=True, capture_output=True, text=True + ).stdout.strip() + + return { + "love": sh(f"LD_LIBRARY_PATH={LOVE_APPIMAGE_DIR}/lib {LOVE_BIN} --version"), + "lovely": sh(f"ls -la {LOVELY_SO}"), + "game_files": sh(f"ls {GAME_DIR} | head -5 | tr '\\n' ' '"), + "mods": sh(f"ls {MODS_DIR} | tr '\\n' ' '"), + "mod_links": sh(f"ls -ld {' '.join(_MOD_DIR_LINKS)} | tr '\\n' ' '"), + "love_bin": sh( + f"ls -la {LOVE_BIN} && " + f"LD_LIBRARY_PATH={LOVE_APPIMAGE_DIR}/lib ldd {LOVE_BIN} | grep liblove" + ), + "balatrobot": sh("python -c 'import balatrobot; print(balatrobot.__file__)'"), + } + + +@app.function(image=balatro_image, timeout=900) +def smoke_test() -> str: + """Boot the game, hit the API, and report what the mod loaded.""" + import asyncio + + import httpx + + from balatrobot.manager import BalatroInstance + + def read_log(instance) -> str: + log = instance.log_path + return log.read_text() if log and log.exists() else "(no log file)" + + async def main() -> str: + instance = BalatroInstance() + try: + await instance.start() + except Exception as exc: # noqa: BLE001 - report, don't re-raise + return f"FAILED to start: {exc!r}\n\n--- game log ---\n{read_log(instance)[-6000:]}" + + try: + async with httpx.AsyncClient(timeout=30.0) as client: + response = await client.post( + f"http://127.0.0.1:{instance.port}", + json={"jsonrpc": "2.0", "method": "health", "id": 1}, + ) + health = str(response.json()) + except Exception as exc: # noqa: BLE001 + health = f"health call failed: {exc!r}" + finally: + log_text = read_log(instance) + await instance.stop() + + interesting = [ + line + for line in log_text.splitlines() + if any( + marker in line + for marker in ("BalatroBot", "Steamodded v", "Lovely", "ERROR", "Error") + ) + ] + return "health: {}\n{}".format(health, "\n".join(interesting[-25:])) + + return asyncio.run(main()) + + +@app.function(image=balatro_image, timeout=1800) +def demo_run(steps: int = 60) -> dict: + """Drive a scripted run for `steps` actions and report where it ended up. + + Not a policy - it always plays the first five cards in hand - but it + exercises every phase transition a rollout has to survive: blind select, + hands, cash out, shop, booster packs. + """ + import asyncio + + import httpx + + from balatrobot.manager import BalatroInstance + + # Phases the game moves through on its own (animations); just wait them out. + TRANSIENT = {"HAND_PLAYED", "DRAW_TO_HAND", "NEW_ROUND", "GAME_OVER"} + + async def main() -> dict: + instance = BalatroInstance() + await instance.start() + url = f"http://127.0.0.1:{instance.port}" + history: list[str] = [] + errors: list[str] = [] + + try: + async with httpx.AsyncClient(timeout=120.0) as client: + + async def call(method: str, params: dict | None = None) -> dict: + response = await client.post( + url, + json={ + "jsonrpc": "2.0", + "method": method, + "params": params or {}, + "id": 1, + }, + ) + data = response.json() + if "error" in data: + raise RuntimeError(f"{method}: {data['error']}") + return data["result"] + + state = await call( + "start", {"deck": "RED", "stake": "WHITE", "seed": "BALATRO"} + ) + + for _ in range(steps): + phase = state.get("state") + + try: + if phase == "BLIND_SELECT": + state = await call("select") + elif phase == "SELECTING_HAND": + hand = state.get("hand") or [] + if not hand: + break + cards = list(range(min(5, len(hand)))) + state = await call("play", {"cards": cards}) + elif phase == "ROUND_EVAL": + state = await call("cash_out") + elif phase == "SHOP": + state = await call("next_round") + elif phase == "TAROT_PACK" or phase.endswith("_PACK"): + state = await call("pack", {"skip": True}) + elif phase in TRANSIENT: + await asyncio.sleep(0.25) + state = await call("gamestate") + continue + else: + errors.append(f"unhandled phase {phase}") + break + except RuntimeError as exc: + errors.append(str(exc)) + break + + history.append(phase) + if state.get("won") or state.get("state") == "GAME_OVER": + break + finally: + await instance.stop() + + summary = { + "final_state": state.get("state"), + "ante": state.get("ante_num"), + "round": state.get("round_num"), + "money": state.get("money"), + "actions": len(history), + "phases_visited": sorted(set(history)), + "errors": errors, + } + print(summary) + return summary + + return asyncio.run(main()) + + +@app.local_entrypoint() +def main() -> None: + print("--- image layout ---") + for key, value in check_image.remote().items(): + print(f"{key}: {value}") + print("\n--- boot + health ---") + print(smoke_test.remote()) diff --git a/batro_modal_rl.py b/batro_modal_rl.py new file mode 100644 index 00000000..18476562 --- /dev/null +++ b/batro_modal_rl.py @@ -0,0 +1,4054 @@ +"""Train a Balatro policy with Modal Dojo and one Modal Sandbox per game. + +The macOS ``Balatro.love`` archive is platform independent, but the executable +is not. This file uploads the private game assets to a Modal Volume and runs +them with the official Linux LOVE 11.5 build. Each rollout gets its own +Sandbox, so concurrent games never share a Balatro process or save directory. + +Safe workflow (from the Modal Dojo Python 3.12 environment):: + + python batro_modal_rl.py --prepare-assets + python batro_modal_rl.py --publish-image + python batro_modal_rl.py --smoke-env + python batro_modal_rl.py --launch + +The preparation, image publishing, and smoke commands use CPU containers only. Training is never launched +unless ``--launch`` is passed explicitly. +""" + +from __future__ import annotations + +import argparse +import asyncio +import json +import math +import os +import re +import sys +import tempfile +import time +from copy import deepcopy +from dataclasses import dataclass, field +from io import BytesIO +from pathlib import Path, PurePosixPath +from typing import Any + +import modal +from modal_dojo import ( + DatasetConfig, + Qwen3_8_27B, + Qwen3_8_27B_Recipe as BaseQwen3_8_27B_Recipe, + TrainConfig, +) +from pydantic import ConfigDict +from pydantic.dataclasses import dataclass as recipe_dataclass + +try: + from modal_dojo import DashboardComponent +except ImportError: + # Dashboard components were added after the core Modal Dojo launch API. + # Keep the training entrypoint usable with compatible older clients. + DashboardComponent = None + +from modal_dojo.common.checkpoint import Checkpoint, _list_checkpoints +from modal_dojo.common.models.base import ToolCall +from modal_dojo.common.run import TrainingRun, set_checkpoint_location + +# --------------------------------------------------------------------------- +# Private assets: local Mac -> private Modal Volume +# --------------------------------------------------------------------------- + +LOVE_VERSION = "11.5" +LOVELY_VERSION = "0.9.0" +SMODS_VERSION = "1.0.0-beta-1814a" + +ASSET_VOLUME_NAME = "balatro-rl-assets" +ASSET_MOUNT = "/opt/balatro-assets" +ASSET_APP_NAME = "balatro-rl-assets" +SANDBOX_APP_NAME = "balatro-rl-sandboxes" +SANDBOX_IMAGE_NAME = "balatrobot" + +REPO = Path(__file__).resolve().parent +# The Balatro-specific viewer is shipped with this repository. Keep this +# path relative to the module rather than the caller's working directory so +# ``modal run`` and ``python`` resolve the same local source file. +BALATRO_TRAJECTORY_VIEWER_PATH = REPO / "dashboard" / "TrajectoryViewer.svelte" + +LOVE_ARCHIVE_CANDIDATES = ( + Path.home() + / "Library/Application Support/Steam/steamapps/common/Balatro" + / "Balatro.app/Contents/Resources/Balatro.love", + Path.home() / ".local/share/Steam/steamapps/common/Balatro/Balatro.love", + Path.home() / ".steam/steam/steamapps/common/Balatro/Balatro.love", +) +SAVE_DIR_CANDIDATES = ( + Path.home() / "Library/Application Support/Balatro", + Path.home() / ".local/share/love/Balatro", +) + + +def _find_balatro_love() -> Path: + if override := os.environ.get("BALATRO_LOVE_ARCHIVE"): + path = Path(override).expanduser() + if path.is_file(): + return path + raise FileNotFoundError(f"BALATRO_LOVE_ARCHIVE does not exist: {path}") + for path in LOVE_ARCHIVE_CANDIDATES: + if path.is_file(): + return path + raise FileNotFoundError( + "Could not find Balatro.love. Install Balatro with Steam or set " + "BALATRO_LOVE_ARCHIVE." + ) + + +def _find_balatro_save() -> Path: + if override := os.environ.get("BALATRO_SAVE_DIR"): + path = Path(override).expanduser() + else: + path = next( + (p for p in SAVE_DIR_CANDIDATES if (p / "settings.jkr").is_file()), + None, + ) + if path is None or not (path / "settings.jkr").is_file(): + raise FileNotFoundError( + "No Balatro settings.jkr found. Launch Balatro once or set " + "BALATRO_SAVE_DIR." + ) + for relative in ("1/profile.jkr", "1/meta.jkr"): + if not (path / relative).is_file(): + raise FileNotFoundError(f"Balatro save is missing {relative}: {path}") + return path + + +assets_app = modal.App(ASSET_APP_NAME) +asset_volume = modal.Volume.from_name(ASSET_VOLUME_NAME, create_if_missing=True) +asset_seed_image = modal.Image.debian_slim(python_version="3.12") + + +@assets_app.function( + image=asset_seed_image, + volumes={"/assets": asset_volume}, + timeout=15 * 60, + serialized=True, + include_source=False, +) +def prepare_assets(release: str, upload_id: str) -> dict[str, Any]: + """Publish a content-addressed Balatro release to the private Volume.""" + import shutil + import uuid + import zipfile + + releases = Path("/assets/releases") + destination = releases / release + releases.mkdir(parents=True, exist_ok=True) + reused = destination.exists() + + if not reused: + seed = Path("/assets/uploads") / upload_id + if not upload_id or not seed.is_dir(): + raise FileNotFoundError(f"Asset upload is missing: {seed}") + staging = Path(f"/assets/.staging-{release}-{uuid.uuid4().hex[:8]}") + game = staging / "game" + game.mkdir(parents=True) + with zipfile.ZipFile(seed / "Balatro.love") as archive: + archive.extractall(game) + + # A packaged Balatro executable is fused and implicitly uses the save + # identity "Balatro". The portable .love directory needs it explicitly. + conf_path = game / "conf.lua" + conf = conf_path.read_text() + if "t.identity" not in conf: + conf = conf.replace( + "t.title = 'Balatro'", + "t.title = 'Balatro'\n\tt.identity = 'Balatro'", + 1, + ) + conf_path.write_text(conf) + + shutil.copytree(seed / "mod", staging / "mods/balatrobot") + shutil.copytree(seed / "python", staging / "python") + shutil.copytree(seed / "save", staging / "save") + (staging / "manifest.json").write_text( + json.dumps( + { + "release": release, + "love": LOVE_VERSION, + "lovely": LOVELY_VERSION, + "smods": SMODS_VERSION, + }, + indent=2, + ) + ) + staging.rename(destination) + + if upload_id: + shutil.rmtree(Path("/assets/uploads") / upload_id, ignore_errors=True) + current_tmp = Path(f"/assets/.CURRENT-{os.getpid()}") + current_tmp.write_text(release + "\n") + current_tmp.replace("/assets/CURRENT") + asset_volume.commit() + return { + "release": release, + "volume": ASSET_VOLUME_NAME, + "path": str(destination), + "reused": reused, + } + + +# --------------------------------------------------------------------------- +# Linux game image and one-sandbox-per-episode environment +# --------------------------------------------------------------------------- + +_LOVE_WRAPPER = """#!/bin/sh +set -eu +LOVELY_PRELOAD="${LD_PRELOAD:-}" +unset LD_PRELOAD +DISPLAY_NUM="${BALATRO_DISPLAY:-99}" +if ! xdpyinfo -display ":$DISPLAY_NUM" >/dev/null 2>&1; then + Xvfb ":$DISPLAY_NUM" -screen 0 1280x720x24 -nolisten tcp >/tmp/xvfb.log 2>&1 & + i=0 + while [ "$i" -lt 100 ]; do + xdpyinfo -display ":$DISPLAY_NUM" >/dev/null 2>&1 && break + i=$((i + 1)) + sleep 0.1 + done +fi +export DISPLAY=":$DISPLAY_NUM" +export LD_LIBRARY_PATH="/opt/love/lib:${LD_LIBRARY_PATH:-}" +export LD_PRELOAD="$LOVELY_PRELOAD" +exec /opt/balatro/bin/love "$@" +""" + +_SANDBOX_ENTRYPOINT = f"""#!/bin/sh +set -eu +if [ ! -s {ASSET_MOUNT}/CURRENT ]; then + echo "Balatro assets are missing. Run: python batro_modal_rl.py --prepare-assets" >&2 + exit 2 +fi +RELEASE="$(tr -d '\\r\\n' < {ASSET_MOUNT}/CURRENT)" +ROOT="{ASSET_MOUNT}/releases/$RELEASE" +test -d "$ROOT/game" +test -d "$ROOT/mods/balatrobot" +test -d "$ROOT/python/balatrobot" + +ln -sfn "$ROOT/game" /opt/balatro/game +ln -sfn "$ROOT/mods/balatrobot" /opt/balatro/Mods/balatrobot + +# Saves are per-sandbox writable copies. Concurrent episodes must not mutate a +# shared profile in the Volume. +rm -rf /root/.local/share/love/Balatro +mkdir -p /root/.local/share/love/Balatro +cp -a "$ROOT/save/." /root/.local/share/love/Balatro/ + +export PYTHONPATH="$ROOT/python" +exec python -m balatrobot serve +""" + + +def _balatro_runtime_image() -> modal.Image: + """Build recipe used only when explicitly publishing the named runtime.""" + return ( + modal.Image.from_registry("debian:trixie-slim", add_python="3.12") + .run_commands( + "sed -i '/path-exclude.*man/d' /etc/dpkg/dpkg.cfg.d/* 2>/dev/null || true", + "rm -f /etc/dpkg/dpkg.cfg.d/docker", + ) + .apt_install( + "love", # pulls the AppImage's shared runtime dependencies + "xvfb", + "x11-utils", + "libgl1-mesa-dri", + "ca-certificates", + "curl", + "git", + "unzip", + "procps", + ) + .run_commands( + "curl -fsSL -o /tmp/love.AppImage " + "https://github.com/love2d/love/releases/download/" + f"{LOVE_VERSION}/love-{LOVE_VERSION}-x86_64.AppImage", + "chmod +x /tmp/love.AppImage", + "cd /tmp && ./love.AppImage --appimage-extract >/dev/null", + "mv /tmp/squashfs-root /opt/love", + "rm -f /tmp/love.AppImage", + "curl -fsSL -o /tmp/lovely.tar.gz " + "https://github.com/ethangreen-dev/lovely-injector/releases/download/" + f"v{LOVELY_VERSION}/lovely-x86_64-unknown-linux-gnu.tar.gz", + "tar xzf /tmp/lovely.tar.gz -C /tmp", + "install -m 755 /tmp/liblovely.so /usr/local/lib/liblovely.so", + "rm -f /tmp/lovely.tar.gz /tmp/liblovely.so", + "mkdir -p /opt/balatro/bin /opt/balatro/Mods " + "/root/.local/share/love /root/.config/love", + "ln -sfn /opt/love/bin/love /opt/balatro/bin/love", + "git clone --depth 1 --branch " + f"{SMODS_VERSION} https://github.com/Steamodded/smods.git " + "/opt/balatro/Mods/smods", + "ln -sfn /opt/balatro/Mods /root/.local/share/love/Mods", + "ln -sfn /opt/balatro/Mods /root/.config/love/Mods", + "cat > /usr/local/bin/balatro-love <<'LOVE_EOF'\n" + + _LOVE_WRAPPER + + "LOVE_EOF", + "chmod +x /usr/local/bin/balatro-love", + "cat > /usr/local/bin/balatro-sandbox-entrypoint <<'ENTRY_EOF'\n" + + _SANDBOX_ENTRYPOINT + + "ENTRY_EOF", + "chmod +x /usr/local/bin/balatro-sandbox-entrypoint", + ) + .pip_install("httpx", "typer") + .env( + { + "BALATROBOT_PLATFORM": "native", + "BALATROBOT_BALATRO_PATH": "/opt/balatro/game", + "BALATROBOT_LOVE_PATH": "/usr/local/bin/balatro-love", + "BALATROBOT_LOVELY_PATH": "/usr/local/lib/liblovely.so", + "BALATROBOT_LOGS_PATH": "/tmp/balatrobot-logs", + "BALATROBOT_HEADLESS": "1", + # Render a single Xvfb frame on RPC calls. BalatroBot disables + # headless internally when this is set, allowing screenshots + # without continuously rendering the game. + "BALATROBOT_RENDER_ON_API": "1", + "BALATROBOT_FAST": "1", + "BALATROBOT_AUDIO": "0", + "BALATROBOT_NO_SHADERS": "1", + "LIBGL_ALWAYS_SOFTWARE": "1", + "SDL_AUDIODRIVER": "dummy", + "ALSOFT_DRIVERS": "null", + } + ) + ) + + +_RPC_SNIPPET = r""" +import json, sys, urllib.request +method, params = sys.argv[1], json.loads(sys.argv[2]) +payload = json.dumps({"jsonrpc":"2.0","id":1,"method":method,"params":params}).encode() +request = urllib.request.Request( + "http://127.0.0.1:12346", + data=payload, + headers={"Content-Type":"application/json"}, +) +with urllib.request.urlopen(request, timeout=8) as response: + data = json.loads(response.read()) +print("__BALATRO_RPC__" + json.dumps(data, separators=(",", ":"))) +""".strip() + +DECISION_STATES = frozenset( + {"BLIND_SELECT", "SELECTING_HAND", "SHOP", "SMODS_BOOSTER_OPENED"} +) +TERMINAL_STATES = frozenset({"GAME_OVER"}) + + +@dataclass +class Observation: + """Balatro tool feedback, independent of unpublished Modal Dojo APIs.""" + + text: str = "" + is_error: bool = False + metadata: dict[str, Any] = field(default_factory=dict) + + +@dataclass +class StepResult: + observation: Observation + done: bool = False + info: dict[str, Any] = field(default_factory=dict) + + +class SandboxEnvironment: + def __init__(self, sandbox: Any) -> None: + self.sandbox = sandbox + + def close(self) -> None: + try: + self.sandbox.terminate() + except Exception: + pass + + def __enter__(self): + return self + + def __exit__(self, *exc): + self.close() + + +class SandboxEnvironmentPool: + """Keep the sandbox lifecycle local to the Balatro harness.""" + + def __init__(self) -> None: + self._app = None + + def __getstate__(self) -> dict: + return {**self.__dict__, "_app": None} + + def app_handle(self): + if self._app is None: + self._app = modal.App.lookup(self.app_name, create_if_missing=True) + return self._app + + def create_sandbox(self, *, image=None, command=("sleep", "infinity"), **kwargs): + return modal.Sandbox._experimental_create( + *command, + image=image if image is not None else self.build_image(), + app=self.app_handle(), + **kwargs, + ) + + def release(self, env): + env.close() + + +class BalatroSandboxEnvironment(SandboxEnvironment): + """One isolated Balatro process and JSON-RPC server.""" + + def __init__(self, sandbox: Any, state: dict[str, Any]) -> None: + super().__init__(sandbox) + self.state = state + + def rpc(self, method: str, params: dict[str, Any] | None = None) -> dict[str, Any]: + process = self.sandbox.exec( + "python", + "-c", + _RPC_SNIPPET, + method, + json.dumps(params or {}, separators=(",", ":")), + timeout=15, + ) + process.wait() + output = process.stdout.read() + for line in reversed(output.splitlines()): + if line.startswith("__BALATRO_RPC__"): + payload = json.loads(line.removeprefix("__BALATRO_RPC__")) + if "error" in payload: + raise RuntimeError(f"{method} failed: {payload['error']}") + return payload["result"] + error = process.stderr.read() + raise RuntimeError( + f"{method} RPC failed (rc={process.returncode}): {(error or output)[-1200:]}" + ) + + @staticmethod + def _terminal(state: dict[str, Any]) -> bool: + return bool(state.get("won")) or state.get("state") in TERMINAL_STATES + + @staticmethod + def _stable(state: dict[str, Any]) -> bool: + """Return whether a raw gamestate is safe to use for an action.""" + # The defaults preserve compatibility with an older BalatroBot asset + # release that predates the readiness fields. + return bool(state.get("state_complete", True)) and not bool( + state.get("controller_locked", False) + ) + + def wait_for_stable_state(self, expected: str) -> dict[str, Any]: + """Wait for a particular non-decision state to finish transitioning.""" + current = self.rpc("gamestate") + for _ in range(120): + if current.get("state") == expected and self._stable(current): + self.state = current + return current + time.sleep(0.1) + current = self.rpc("gamestate") + raise TimeoutError( + f"Balatro did not reach stable state {expected!r}: " + f"{current.get('state')!r}" + ) + + def settle(self, state: dict[str, Any] | None = None) -> dict[str, Any]: + """Advance automatic phases until the next model decision or terminal.""" + current = state or self.rpc("gamestate") + for _ in range(120): + name = current.get("state", "") + # A gamestate response can briefly expose the next decision state + # while Balatro is still finishing its UI/event transition. Do + # not hand that frame to the model or use it to issue an action. + # The readiness fields are ignored when talking to an older mod so + # local smoke tests and mixed deployments remain compatible. + stable = self._stable(current) + if self._terminal(current) or ( + name in DECISION_STATES and stable + ): + self.state = current + return current + if name == "ROUND_EVAL": + current = self.rpc("cash_out") + else: + time.sleep(0.1) + current = self.rpc("gamestate") + raise TimeoutError( + f"Balatro did not reach a decision state: {current.get('state')}" + ) + + def start(self, seed: str) -> dict[str, Any]: + self.rpc("menu") + state = self.rpc("start", {"deck": "RED", "stake": "WHITE", "seed": seed}) + return self.settle(state) + + def screenshot_data_uri(self) -> str: + """Capture the current game frame for Modal Dojo's rollout viewer.""" + path = f"/tmp/balatro-rollout-{time.time_ns()}.png" + self.rpc("screenshot", {"path": path}) + process = self.sandbox.exec("base64", "-w", "0", path, timeout=15) + process.wait() + encoded = process.stdout.read().strip() + if process.returncode != 0 or not encoded: + error = process.stderr.read().strip() + raise RuntimeError(f"screenshot encoding failed: {error}") + return f"data:image/png;base64,{encoded}" + + def step(self, action: ToolCall) -> StepResult: + try: + # Refresh immediately before every action. This closes the gap + # between the model's previous observation and the live game, and + # also waits out automatic transitions after a blind/round action. + self.settle() + action_response = self.rpc(action.name, action.arguments) + # A successful play can return through ROUND_EVAL, after which + # settle() immediately calls cash_out and receives SHOP. Preserve + # its commit record across that automatic transition. + committed_play = action_response.get("play_commit") + state = self.settle(action_response) + if isinstance(committed_play, dict) and "play_commit" not in state: + state["play_commit"] = committed_play + return StepResult( + observation=Observation( + text=_compact_state(state), metadata={"state": state} + ), + done=self._terminal(state), + info={"state_name": state.get("state", "")}, + ) + except Exception as exc: # action errors are learning signal, not harness death + # An action can race a state transition even after the preflight + # refresh (for example when the server observes a new frame just + # after accepting the request). Return the freshest recoverable + # state so the episode loop can choose a fallback for the actual + # phase rather than blindly retrying a stale select/skip action. + error_text = str(exc) + recovered = self.state + try: + recovered = self.settle() + except Exception as refresh_exc: + error_text += f"; state refresh failed: {refresh_exc}" + return StepResult( + observation=Observation( + text=error_text, + is_error=True, + metadata={"state": recovered, "error": error_text}, + ), + done=False, + info={ + "exception": repr(exc), + "state_refreshed": recovered is not self.state, + }, + ) + + +class BalatroEnvironmentPool(SandboxEnvironmentPool): + @property + def app_name(self) -> str: + return SANDBOX_APP_NAME + + def build_image(self) -> modal.Image: + # Episode startup resolves the published runtime without triggering builds. + return modal.Image.from_name(SANDBOX_IMAGE_NAME) + + def _diagnostics(self, sandbox: Any) -> str: + parts: list[str] = [] + try: + process = sandbox.exec( + "sh", + "-lc", + "find /tmp/balatrobot-logs -type f -name '*.log' -print " + "-exec tail -80 {} \\; 2>/dev/null", + timeout=15, + ) + process.wait() + parts.append(process.stdout.read() + process.stderr.read()) + except Exception as exc: + parts.append(f"diagnostic exec unavailable: {exc!r}") + for name in ("stdout", "stderr"): + try: + parts.append(f"--- sandbox {name} ---\n{getattr(sandbox, name).read()}") + except Exception as exc: + parts.append(f"{name} unavailable: {exc!r}") + return "\n".join(parts)[-8000:] + + def acquire(self, seed: str) -> BalatroSandboxEnvironment: + volume = modal.Volume.from_name(ASSET_VOLUME_NAME, create_if_missing=False) + sandbox = self.create_sandbox( + image=self.build_image(), + command=("/usr/local/bin/balatro-sandbox-entrypoint",), + volumes={ASSET_MOUNT: volume}, + timeout=60 * 60, + idle_timeout=10 * 60, + cpu=2.0, + memory=4096, + ) + env = BalatroSandboxEnvironment(sandbox, {}) + deadline = time.monotonic() + 90 + last_error = "not ready" + while time.monotonic() < deadline: + try: + env.rpc("health") + return env + except Exception as exc: + last_error = repr(exc) + if sandbox.poll() is not None: + break + time.sleep(0.5) + diagnostics = self._diagnostics(sandbox) + env.close() + raise RuntimeError( + f"Balatro sandbox did not become healthy: {last_error}\n{diagnostics}" + ) + + +_ENV_POOL: BalatroEnvironmentPool | None = None + + +def _get_env_pool() -> BalatroEnvironmentPool: + global _ENV_POOL + if _ENV_POOL is None: + _ENV_POOL = BalatroEnvironmentPool() + return _ENV_POOL + + +# --------------------------------------------------------------------------- +# Agent prompt, action parsing, and episode reward +# --------------------------------------------------------------------------- + +# Base for first-clear ante rewards: ante k pays k * this value. Re-clearing +# an ante after a lowering voucher pays no additional ante checkpoint bonus. +ANTE_CHECKPOINT_BONUS = 8.0 +FINAL_ANTE = 8 +# A complete action budget is evidence that the policy failed to finish the +# game, not an environment fault. Keep the cost large enough that a loop of +# otherwise legal no-op actions cannot outrank actual blind progress. +DECISION_CAP_PENALTY = -10.0 + +_ALLOWED_METHODS = { + "SELECTING_HAND": { + "play", + "discard", + "sell", + "use", + }, + "SHOP": {"buy", "reroll", "next_round", "sell", "use"}, + "SMODS_BOOSTER_OPENED": {"pack", "sell"}, + "BLIND_SELECT": {"select", "skip"}, +} +BALATRO_SYSTEM_PROMPT = ( + "Play Balatro through the provided JSON action interface. Choose exactly " + "one legal API action and return only minified JSON: " + '{"method":"name","params":{...}}. Card and item indices are 0-based; ' + "every index in a list must be distinct. " + "End your response immediately after that one object. Extra objects, prose, " + "or hitting the output limit reject the entire response without executing it. " + '"buy", "sell", "use", and "pack" take scalar item indices, e.g. ' + '{"method":"buy","params":{"pack":0}}; only cards/targets take lists. State uses ' + "hand_stats=[level,total_played,played_this_round]. After the first " + "turn, balatro_state contains only fields changed since the previous " + "turn; absent fields are unchanged and null means removed. Each new " + "conversation starts with a complete snapshot replacing all old state. " + "Card instance IDs identify visible cards; actions still use area indices. Poker-hand " + "scoring: the state field hand_values maps each hand to " + "[level, base_chips, base_mult]. A played hand's rough score is " + "(base_chips + scored card chips + bonuses) × (base_mult + bonuses); " + "jokers, enhancements, debuffs, and boss effects can change it. " + "At level 1 the common bases are High Card 5/1, Pair 10/2, Two Pair " + "20/2, Three of a Kind 30/3, Straight 30/4, Flush 35/4, Full House " + "40/4, Four of a Kind 60/7, and Straight Flush 100/8; Five of a Kind, " + "Flush House, and Flush Five are stronger special hands. The exact " + "current values in hand_values are authoritative. " + "hand_groups gives visible matching ranks, suit groups, and example " + "standard straights using CURRENT action indices. These are recognition " + "aids, not score estimates: account for debuffs, enhancements, jokers, " + "and boss rules. Before play, compare the scoring cards with the other " + "available combinations and the chips still needed. Include all matching " + "cards needed for the intended hand. Consider discarding to improve the " + "hand while discards remain. Re-evaluate indices after every draw. " + "Choose hands that clear the blind target, not just the highest raw " + "rank. At BLIND_SELECT, choose select to enter the blind or skip to take " + "the displayed tag (Boss blinds cannot be skipped). After a successful " + "skip, the next state includes action_result with skipped, tag, tag_effect, " + "and next blind details; action_result is one-turn transition metadata, " + "not a replacement for the persistent game state." +) +_INDEX = r"[0-9]+" +_INDEX_LIST = rf"\[{_INDEX}(,{_INDEX})*\]" +_INDEX_LIST_5 = rf"\[{_INDEX}(,{_INDEX}){{0,4}}\]" +_ACTION_REGEX_BY_STATE = { + "BLIND_SELECT": r'\{"method":"(select|skip)","params":\{\}\}', + "SELECTING_HAND": ( + rf'\{{"method":"(play|discard)","params":\{{"cards":{_INDEX_LIST_5}\}}\}}' + rf'|\{{"method":"sell","params":\{{"(joker|consumable)":{_INDEX}\}}\}}' + rf'|\{{"method":"use","params":\{{"consumable":{_INDEX}' + rf'(,"cards":{_INDEX_LIST_5})?\}}\}}' + ), + "SHOP": ( + rf'\{{"method":"buy","params":\{{"(card|voucher|pack)":{_INDEX}\}}\}}' + r'|\{"method":"(reroll|next_round)","params":\{\}\}' + rf'|\{{"method":"sell","params":\{{"(joker|consumable)":{_INDEX}\}}\}}' + rf'|\{{"method":"use","params":\{{"consumable":{_INDEX}' + rf'(,"cards":{_INDEX_LIST_5})?\}}\}}' + ), + "SMODS_BOOSTER_OPENED": ( + r'\{"method":"pack","params":\{"skip":true\}\}' + rf'|\{{"method":"sell","params":\{{"(joker|consumable)":{_INDEX}\}}\}}' + rf'|\{{"method":"pack","params":\{{"card":{_INDEX}' + rf'(,"targets":{_INDEX_LIST_5})?\}}\}}' + ), +} + + +def _action_regex(state_name: str) -> str: + return _ACTION_REGEX_BY_STATE.get( + state_name, + r'\{"method":"(play|discard|buy|reroll|next_round|sell|use|pack)",' + r'"params":\{[^{}\n]*\}\}', + ) + + +def _compact_state(state: dict[str, Any], limit: int | None = None) -> str: + """Serialize decision state without deleting facts to meet a character limit. + + The context manager budgets actual tokens and starts a new conversation + when needed. ``limit`` remains accepted for older callers only. + """ + del limit + + def card_view(card: Any, *, playing_card: bool = False, owned: bool = False) -> Any: + if not isinstance(card, dict): + return card + value = card.get("value") if isinstance(card.get("value"), dict) else {} + modifier = card.get("modifier") + if isinstance(modifier, dict): + modifier = {key: value for key, value in modifier.items() if value} + else: + modifier = None + card_state = card.get("state") + if isinstance(card_state, dict) and card_state.get("hidden"): + # No instance/key/effect: otherwise a shuffled face-down card can + # be identified by joining its ID to an earlier visible snapshot. + return {"hidden": True} + debuffed = bool( + card.get("debuff") + or (isinstance(card_state, dict) and card_state.get("debuff")) + ) + key = card.get("key") or card.get("label") + if playing_card: + # Most playing cards become a single token-like key such as S_A. + counters = card.get("counters") or {} + instance = card.get("id") + if not modifier and not debuffed and not counters and not instance: + return key + view = {"id": key, "mod": modifier or None} + if instance: + view["instance"] = instance + if counters: + view["counters"] = counters + if debuffed: + view["debuff"] = True + return {name: item for name, item in view.items() if item is not None} + + view = {"id": key, "effect": card.get("effect") or value.get("effect")} + if card.get("id"): + view["instance"] = card["id"] + if debuffed: + view["debuff"] = True + if card.get("counters"): + view["counters"] = card["counters"] + # Already-visible round targets (Idol/Ancient/Mail/Castle), not RNG + # seeds. Keep these structured so replay scoring never guesses them. + if isinstance(card.get("scoring_state"), dict): + view["scoring_state"] = card["scoring_state"] + if modifier: + view["mod"] = modifier + cost = card.get("cost") + buy = cost.get("buy") if isinstance(cost, dict) else card.get("buy") + sell = cost.get("sell") if isinstance(cost, dict) else card.get("sell") + if owned and sell is not None: + view["sell"] = sell + elif not owned and buy is not None: + view["cost"] = buy + return {name: item for name, item in view.items() if item is not None} + + def area_cards( + area: Any, *, playing_cards: bool = False, owned: bool = False + ) -> list[Any]: + if not isinstance(area, dict): + return [] + cards = area.get("cards") if isinstance(area.get("cards"), list) else [] + return [ + card_view(card, playing_card=playing_cards, owned=owned) for card in cards + ] + + def slots(area: Any, default_limit: int) -> str: + if not isinstance(area, dict): + return f"0/{default_limit}" + cards = area.get("cards") if isinstance(area.get("cards"), list) else [] + count = int(area.get("count", len(cards)) or 0) + area_limit = int(area.get("limit", default_limit) or default_limit) + return f"{count}/{area_limit}" + + state_name = str(state.get("state", "")) + phase = { + "SELECTING_HAND": "hand", + "SHOP": "shop", + "SMODS_BOOSTER_OPENED": "pack", + }.get(state_name, state_name) + compact: dict[str, Any] = { + "phase": phase, + "ante": state.get("ante_num"), + "round": state.get("round_num"), + "money": state.get("money"), + "deck_type": state.get("deck"), + "stake": state.get("stake"), + } + for key in ("run_state", "blind_state", "tags", "pack_choices"): + if key in state: + compact[key] = state[key] + + round_info = state.get("round") if isinstance(state.get("round"), dict) else {} + blinds = state.get("blinds") if isinstance(state.get("blinds"), dict) else {} + current_blind = next( + ( + blind + for blind in blinds.values() + if isinstance(blind, dict) and blind.get("status") == "CURRENT" + ), + None, + ) + if current_blind: + compact["blind"] = { + "type": str(current_blind.get("type", "")).lower(), + "score": [round_info.get("chips"), current_blind.get("score")], + "hands": round_info.get("hands_left"), + "discards": round_info.get("discards_left"), + "effect": current_blind.get("effect") or None, + } + + # Blind selection is a model decision. Preserve every blind's status, + # score, and tag so the agent can choose between selecting and skipping. + # This is also useful during hand/shop phases when planning around the next + # blind's effect or tag. + blind_views = { + name: { + "status": blind.get("status"), + "type": str(blind.get("type", "")).lower(), + "name": blind.get("name"), + "score": blind.get("score"), + "effect": blind.get("effect") or None, + "tag": blind.get("tag_name") or None, + "tag_effect": blind.get("tag_effect") or None, + } + for name, blind in blinds.items() + if isinstance(blind, dict) + } + if blind_views: + compact["blinds"] = blind_views + + boss = blinds.get("boss") + if isinstance(boss, dict) and boss.get("status") != "DEFEATED": + compact["boss"] = [ + item + for item in (boss.get("name"), boss.get("score"), boss.get("effect")) + if item not in (None, "") + ] + + jokers = state.get("jokers") if isinstance(state.get("jokers"), dict) else {} + consumables = ( + state.get("consumables") if isinstance(state.get("consumables"), dict) else {} + ) + compact["jokers"] = { + "slots": slots(jokers, 5), + "items": area_cards(jokers, owned=True), + } + compact["consumables"] = { + "slots": slots(consumables, 2), + "items": area_cards(consumables, owned=True), + } + if isinstance(state.get("joker_inventory"), dict): + compact["joker_inventory"] = sorted( + area_cards(state["joker_inventory"], owned=True), + key=lambda item: json.dumps(item, separators=(",", ":"), sort_keys=True), + ) + + # Vouchers move out of the shop once purchased. Keep their owned state in + # the compact trajectory so the agent and debugger can distinguish an + # active voucher from the current shop offers. + used_vouchers = state.get("used_vouchers") + if isinstance(used_vouchers, dict) and used_vouchers: + compact["owned_vouchers"] = [ + {"id": key, "effect": value} + for key, value in sorted(used_vouchers.items()) + ] + + # Expose the known deck composition, not the engine's draw order. The + # composition is what a player can use to plan a build; preserving order + # would leak future draws and would also make every delta unnecessarily + # large. Sort the compact views so draws do not reshuffle this field. + deck_area = state.get("cards") if isinstance(state.get("cards"), dict) else {} + deck = area_cards(deck_area, playing_cards=True) + if deck: + compact["deck"] = sorted( + deck, + key=lambda card: json.dumps( + card, separators=(",", ":"), ensure_ascii=False + ), + ) + discard = area_cards(state.get("discard", {}), playing_cards=True) + if discard: + compact["discard"] = sorted( + discard, + key=lambda card: json.dumps( + card, separators=(",", ":"), ensure_ascii=False + ), + ) + + # Complete owned inventory is independent of draws/discards. It has no + # location/order/instance mapping, so it cannot identify face-down cards. + if isinstance(state.get("owned_cards"), dict): + inventory: dict[str, int] = {} + for card in area_cards(state["owned_cards"], playing_cards=True): + if isinstance(card, dict): + card = {key: value for key, value in card.items() if key != "instance"} + encoded = json.dumps(card, separators=(",", ":"), ensure_ascii=False) + inventory[encoded] = inventory.get(encoded, 0) + 1 + compact["owned_deck"] = [ + [json.loads(card), count] for card, count in sorted(inventory.items()) + ] + + if state_name == "SELECTING_HAND": + deck_cards = deck_area or state.get("deck_cards", {}) + compact["deck_left"] = ( + deck_cards.get("count") if isinstance(deck_cards, dict) else None + ) + compact["hand"] = area_cards(state.get("hand", {}), playing_cards=True) + # Surface relationships already visible in the hand. This addresses + # traces that repeatedly selected positional templates while omitting + # a third King or the other Eight. It neither chooses an action nor + # scores a hand, and never uses hidden card identities or draw order. + ranks: dict[str, list[int]] = {} + suits: dict[str, list[int]] = {} + rank_order = "23456789TJQKA" + for index, card in enumerate(compact["hand"]): + if isinstance(card, dict): + if card.get("hidden") or "STONE" in str(card.get("mod", {})).upper(): + continue + key = card.get("id") + else: + key = card + if not isinstance(key, str) or len(key) != 3 or key[1] != "_": + continue + suit, rank = key[0], key[2] + if suit not in "SHDC" or rank not in rank_order: + continue + ranks.setdefault(rank, []).append(index) + suits.setdefault(suit, []).append(index) + straights = [] + for window in [rank_order[start:start + 5] for start in range(8, -1, -1)] + ["A2345"]: + if all(rank in ranks for rank in window): + straights.append([ranks[rank][0] for rank in window]) + compact["hand_groups"] = { + "same_rank": {rank: indices for rank, indices in ranks.items() if len(indices) >= 2}, + "same_suit": {suit: indices for suit, indices in suits.items() if len(indices) >= 4}, + "straight_examples": straights, + } + elif state_name == "SHOP": + compact["reroll_cost"] = round_info.get("reroll_cost") + for key in ("shop", "vouchers", "packs"): + cards = area_cards(state.get(key, {})) + if cards: + compact[key] = cards + elif state_name == "SMODS_BOOSTER_OPENED": + compact["pack"] = area_cards(state.get("pack", {})) + hand = area_cards(state.get("hand", {}), playing_cards=True) + if hand: + compact["hand"] = hand + + hands = state.get("hands") + if not isinstance(hands, dict): + hands = ( + state.get("poker_hand_changes") + if isinstance(state.get("poker_hand_changes"), dict) + else {} + ) + hand_stats = { + name: [ + int(info.get("level") or 1), + int(info.get("played") or 0), + int(info.get("played_this_round") or 0), + ] + for name, info in hands.items() + if isinstance(info, dict) + and ( + int(info.get("level") or 1) != 1 + or int(info.get("played") or 0) > 0 + or int(info.get("played_this_round") or 0) > 0 + ) + } + if hand_stats: + compact["hand_stats"] = hand_stats + hand_values = { + name: [ + int(info.get("level") or 1), + int(info.get("chips") or 0), + int(info.get("mult") or 0), + ] + for name, info in hands.items() + if isinstance(info, dict) + } + if hand_values: + compact["hand_values"] = hand_values + + if state.get("won"): + compact["won"] = True + for key in ("play_commit",): + if isinstance(state.get(key), dict): + compact[key] = state[key] + + def clean(value: Any) -> Any: + if isinstance(value, dict): + return { + key: cleaned + for key, item in value.items() + if (cleaned := clean(item)) not in (None, {}, []) + } + if isinstance(value, list): + return [clean(item) for item in value] + return value + + compact = clean(compact) + + return json.dumps(compact, separators=(",", ":"), ensure_ascii=False) + + +def _area_items(compact: dict[str, Any], name: str) -> list[Any]: + area = compact.get(name) + if isinstance(area, dict) and isinstance(area.get("items"), list): + return area["items"] + value = compact.get(name) + return value if isinstance(value, list) else [] + + +def _slot_usage(compact: dict[str, Any], name: str) -> tuple[int, int]: + area = compact.get(name) + slots = area.get("slots", "0/0") if isinstance(area, dict) else "0/0" + try: + used, limit = str(slots).split("/", 1) + return int(used), int(limit) + except (TypeError, ValueError): + return 0, 0 + + +def _sellable_indices(compact: dict[str, Any], name: str) -> list[int]: + return [ + index for index, item in enumerate(_area_items(compact, name)) + if isinstance(item, dict) and not (item.get("mod") or {}).get("eternal") + ] + + +def _purchasable_indices( + compact: dict[str, Any], kind: str, *, enforce_slots: bool = False +) -> list[int]: + money = float(compact.get("money") or 0) + jokers_used, jokers_limit = _slot_usage(compact, "jokers") + consumables_used, consumables_limit = _slot_usage(compact, "consumables") + allowed: list[int] = [] + for index, item in enumerate(_area_items(compact, kind)): + if not isinstance(item, dict) or float(item.get("cost") or 0) > money: + continue + card_id = str(item.get("id") or "") + if enforce_slots and card_id.startswith("j_") and jokers_used >= jokers_limit: + continue + if ( + enforce_slots + and card_id.startswith("c_") + and consumables_used >= consumables_limit + ): + continue + allowed.append(index) + return allowed + + +_CONSUMABLE_TARGET_BOUNDS: dict[str, tuple[int, int]] = { + # Tarot cards. Balatro permits choosing fewer than the displayed maximum + # for multi-card enhancements/conversions, except Death (exactly two). + "c_magician": (1, 2), + "c_empress": (1, 2), + "c_heirophant": (1, 2), + "c_lovers": (1, 1), + "c_chariot": (1, 1), + "c_justice": (1, 1), + "c_strength": (1, 2), + "c_hanged_man": (1, 2), + "c_death": (2, 2), + "c_devil": (1, 1), + "c_tower": (1, 1), + "c_star": (1, 3), + "c_moon": (1, 3), + "c_sun": (1, 3), + "c_world": (1, 3), + # Spectral cards that highlight a playing card. + "c_talisman": (1, 1), + "c_aura": (1, 1), + "c_deja_vu": (1, 1), + "c_trance": (1, 1), + "c_medium": (1, 1), + "c_cryptid": (1, 1), +} +_CONSUMABLE_REQUIRES_JOKER = frozenset({"c_ankh"}) + + +def _consumable_target_bounds(item: Any) -> tuple[int, int] | None: + if not isinstance(item, dict): + return None + return _CONSUMABLE_TARGET_BOUNDS.get(str(item.get("id") or "")) + + +def _consumable_requires_joker(item: Any) -> bool: + return ( + isinstance(item, dict) + and str(item.get("id") or "") in _CONSUMABLE_REQUIRES_JOKER + ) + + +def _consumable_available(item: Any, compact: dict[str, Any]) -> bool: + """Shared visible-state gate for advertised and accepted consumable uses. + + Creating a Joker is different from requiring an existing Joker (Ankh). + Apply the same capacity rule to owned consumables and immediate pack uses. + Target-card bounds are checked separately because they depend on the action. + """ + if not isinstance(item, dict): + return True + jokers = _area_items(compact, "jokers") + if _consumable_requires_joker(item) and not jokers: + return False + if item.get("id") in {"c_judgement", "c_soul", "c_wraith"}: + used, limit = _slot_usage(compact, "jokers") + return used < limit + return True + + +def _target_action_help( + *, method: str, item_name: str, index: int, item: Any, hand_size: int +) -> str | None: + bounds = _consumable_target_bounds(item) + if bounds is None: + return f"{method} {item_name}={index} (no targets)" + if hand_size <= 0: + return None + low, high = bounds + count = str(low) if low == high else f"{low}-{high}" + key = "cards" if method == "use" else "targets" + return ( + f"{method} {item_name}={index} with {key}={count} distinct " + f"hand indices in 0..{hand_size - 1}" + ) + + +def _action_help(state: dict[str, Any]) -> str: + """Describe only actions that are legal under the current visible state.""" + compact = json.loads(_compact_state(state)) + state_name = str(state.get("state", "")) + jokers = _area_items(compact, "jokers") + consumables = _area_items(compact, "consumables") + + if state_name == "BLIND_SELECT": + blinds = ( + compact.get("blinds") if isinstance(compact.get("blinds"), dict) else {} + ) + selected = next( + ( + (name, blind) + for name, blind in blinds.items() + if isinstance(blind, dict) + and str(blind.get("status") or "").upper() == "SELECT" + ), + None, + ) + if selected is None: + return "select" + name, blind = selected + label = blind.get("name") or name + score = blind.get("score") + actions = [f"select (currently selected: {label}, score {score})"] + if str(blind.get("type") or "").upper() != "BOSS": + tag = blind.get("tag") or "no tag" + tag_effect = blind.get("tag_effect") + tag_text = f"{tag} ({tag_effect})" if tag_effect else tag + actions.append( + f"skip (currently selected: {label}, tag: {tag_text})" + ) + return "; ".join(actions) + + if state_name == "SELECTING_HAND": + hand = compact.get("hand") if isinstance(compact.get("hand"), list) else [] + high = len(hand) - 1 + blind = compact.get("blind") if isinstance(compact.get("blind"), dict) else {} + play_count = ( + "exactly 5" + if "must play 5 cards" in str(blind.get("effect") or "").lower() + else "1-5" + ) + actions = [ + f"play cards={play_count} distinct indices in 0..{high}", + ] + discards = int(blind.get("discards") or 0) + if discards > 0: + actions.append( + f"discard cards=1-5 distinct indices in 0..{high} ({discards} left)" + ) + if sellable := _sellable_indices(compact, "jokers"): + actions.append(f"sell joker=one of {sellable}") + if consumables: + actions.append(f"sell consumable=0..{len(consumables) - 1}") + actions.extend( + help_text + for index, item in enumerate(consumables) + if _consumable_available(item, compact) + and ( + help_text := _target_action_help( + method="use", + item_name="consumable", + index=index, + item=item, + hand_size=len(hand), + ) + ) + ) + return "; ".join(actions) + + if state_name == "SHOP": + actions = ["next_round"] + for kind, indices in ( + ("card", _purchasable_indices(compact, "shop", enforce_slots=True)), + ("voucher", _purchasable_indices(compact, "vouchers")), + ("pack", _purchasable_indices(compact, "packs")), + ): + if indices: + actions.append(f"buy {kind}=one of {indices}") + reroll_cost = float(compact.get("reroll_cost") or 0) + if reroll_cost > 0 and float(compact.get("money") or 0) >= reroll_cost: + actions.append(f"reroll (cost {reroll_cost:g})") + if sellable := _sellable_indices(compact, "jokers"): + actions.append(f"sell joker=one of {sellable}") + if consumables: + actions.append(f"sell consumable=0..{len(consumables) - 1}") + actions.extend( + help_text + for index, item in enumerate(consumables) + if _consumable_available(item, compact) + and _consumable_target_bounds(item) is None + and ( + help_text := _target_action_help( + method="use", + item_name="consumable", + index=index, + item=item, + hand_size=0, + ) + ) + ) + return "; ".join(actions) + + if state_name == "SMODS_BOOSTER_OPENED": + pack = compact.get("pack") if isinstance(compact.get("pack"), list) else [] + jokers_used, jokers_limit = _slot_usage(compact, "jokers") + selectable = [ + index + for index, item in enumerate(pack) + if not ( + isinstance(item, dict) + and str(item.get("id") or "").startswith("j_") + and jokers_used >= jokers_limit + ) + and _consumable_available(item, compact) + ] + actions = ["pack skip=true"] + for kind, name in (("joker", "jokers"), ("consumable", "consumables")): + sellable = _sellable_indices(compact, name) + if sellable: + actions.append( + f"sell {kind}=one of {sellable} " + "(keeps pack open; can free room before choosing a card)" + ) + hand = compact.get("hand") if isinstance(compact.get("hand"), list) else [] + actions.extend( + help_text + for index in selectable + if ( + help_text := _target_action_help( + method="pack", + item_name="card", + index=index, + item=pack[index], + hand_size=len(hand), + ) + ) + ) + return "; ".join(actions) + + return "none" + + +def _turn_prompt(state: dict[str, Any], last_result: dict[str, Any] | None = None) -> str: + return ( + "You are playing Balatro. Choose exactly one legal API action. Card and " + "item indices are 0-based. Optimize for winning ante 8. Return only " + 'minified JSON: {"method":"name","params":{...}} with no markdown or ' + "reasoning. Every index in a list must be distinct. Obey the exact " + "Legal line; omitted actions are illegal. State uses slots=used/limit and " + "hand_stats=[level,total_played,played_this_round] and hand_values=" + "[level,base_chips,base_mult]. After this first " + "turn, carries only the fields that CHANGED since the " + "previous turn; anything absent is unchanged, null means removed.\n" + "At BLIND_SELECT, blinds contains the selected blind and its tag; use " + "select to enter it or skip to take the tag (Boss blinds cannot be " + "skipped). After skip, action_result explicitly reports the skipped " + "blind, tag/tag_effect, and next blind; action_result is one-turn " + "transition metadata.\n" + "The deck field is the sorted composition of cards remaining in the " + "draw pile (not draw order); discard is the known discarded pile. Use " + "these to plan hand selection and future deck/joker synergies.\n" + "owned_deck lists [card,count] for the whole owned deck, without draw " + "order. instance IDs are not action indices. hidden means unknown; " + "do not infer a hidden card's identity from its position.\n" + "joker_inventory, when present, is the known unordered set of owned " + "jokers; it does not identify their face-down slots.\n" + + ("Previous result (old indices): " + json.dumps(last_result, separators=(",", ":")) + "\n" if last_result else "") + + "This is a complete current snapshot; replace all prior state.\n" + f"Legal actions now: {_action_help(state)}\n" + f"Gamestate:\n{_compact_state(state)}" + ) + + +def _action_is_locally_legal(action: ToolCall, state: dict[str, Any]) -> bool: + compact = json.loads(_compact_state(state)) + method, params = action.name, action.arguments + state_name = str(state.get("state", "")) + + def valid_indices(value: Any, size: int, *, complete: bool = False) -> bool: + if not isinstance(value, list) or not value: + return False + if any(type(index) is not int or not 0 <= index < size for index in value): + return False + return len(value) == len(set(value)) and (not complete or sorted(value) == list(range(size))) + + hand = compact.get("hand") if isinstance(compact.get("hand"), list) else [] + jokers = _area_items(compact, "jokers") + consumables = _area_items(compact, "consumables") + blind = compact.get("blind") if isinstance(compact.get("blind"), dict) else {} + compact_blinds = ( + compact.get("blinds") if isinstance(compact.get("blinds"), dict) else {} + ) + + if method in {"select", "skip"}: + if state_name != "BLIND_SELECT" or params: + return False + selected = next( + ( + blind + for blind in compact_blinds.values() + if isinstance(blind, dict) + and str(blind.get("status") or "").upper() == "SELECT" + ), + None, + ) + if selected is None: + return False + return method == "select" or str(selected.get("type") or "").upper() != "BOSS" + + def valid_targets(key: str, item: Any) -> bool: + if not _consumable_available(item, compact): + return False + bounds = _consumable_target_bounds(item) + if bounds is None: + return key not in params + values = params.get(key) + return ( + valid_indices(values, len(hand)) and bounds[0] <= len(values) <= bounds[1] + ) + + if method in {"play", "discard"}: + if set(params) != {"cards"}: + return False + cards = params.get("cards") + if not valid_indices(cards, len(hand)) or len(cards) > 5: + return False + counter = "hands" if method == "play" else "discards" + if int(blind.get(counter) or 0) <= 0: + return False + effect = str(blind.get("effect") or "").lower() + return ( + not (method == "play" and "must play 5 cards" in effect) or len(cards) == 5 + ) + if method == "sell": + if state_name not in {"SHOP", "SELECTING_HAND", "SMODS_BOOSTER_OPENED"}: + return False + if set(params) not in ({"joker"}, {"consumable"}): + return False + kind = "joker" if "joker" in params else "consumable" + index = params[kind] + return type(index) is int and index in _sellable_indices( + compact, "jokers" if kind == "joker" else "consumables" + ) + if method == "use": + if set(params) - {"consumable", "cards"}: + return False + index = params.get("consumable") + if type(index) is not int or not 0 <= index < len(consumables): + return False + if _consumable_target_bounds(consumables[index]) is not None: + if str(state.get("state", "")) != "SELECTING_HAND": + return False + return valid_targets("cards", consumables[index]) + if method == "buy": + if len(params) != 1: + return False + for kind, area, enforce_slots in ( + ("card", "shop", True), + ("voucher", "vouchers", False), + ("pack", "packs", False), + ): + if kind in params: + return ( + isinstance(params[kind], int) + and not isinstance(params[kind], bool) + and params[kind] + in _purchasable_indices(compact, area, enforce_slots=enforce_slots) + ) + return False + if method == "reroll": + if params: + return False + cost = float(compact.get("reroll_cost") or 0) + return cost > 0 and float(compact.get("money") or 0) >= cost + if method == "next_round": + return state_name == "SHOP" and not params + if method == "pack": + if params.get("skip") is True: + return set(params) == {"skip"} + if set(params) - {"card", "targets"}: + return False + index = params.get("card") + pack = compact.get("pack") if isinstance(compact.get("pack"), list) else [] + if type(index) is not int or not 0 <= index < len(pack): + return False + item = pack[index] + jokers_used, jokers_limit = _slot_usage(compact, "jokers") + if ( + isinstance(item, dict) + and str(item.get("id") or "").startswith("j_") + and jokers_used >= jokers_limit + ): + return False + if ( + isinstance(item, dict) + and str(item.get("id") or "") == "c_ankh" + and jokers_used == 0 + ): + return False + return valid_targets("targets", item) + return False + + +def _unique_json_keys(pairs: list[tuple[str, Any]]) -> dict[str, Any]: + result = {} + for key, value in pairs: + if key in result: + raise ValueError("Duplicate JSON key") + result[key] = value + return result + + +def _reject_json_constant(value: str) -> Any: + raise ValueError(f"Non-JSON constant: {value}") + + +def _single_action_object(text: str | None) -> dict[str, Any] | None: + """Consume the entire response, allowing only whitespace and one terminal EOS. + + Qwen's decoded text may include its sampled end-of-turn token. Removing + that suffix for validation never changes the sampled training IDs/logprobs. + """ + if not text: + return None + candidate = text.strip().removesuffix("<|im_end|>").strip() + try: + value = json.loads(candidate, object_pairs_hook=_unique_json_keys, + parse_constant=_reject_json_constant) + except (ValueError, RecursionError): + return None + if (not isinstance(value, dict) or set(value) != {"method", "params"} + or not isinstance(value["method"], str) or not isinstance(value["params"], dict)): + return None + return value + + +def _action_object_count(text: str) -> int: + """Diagnostic only: count complete action objects, never select one to run.""" + count = 0 + consumed = 0 + for match in re.finditer(r"\{", text): + if match.start() < consumed: + continue + try: + value, end = json.JSONDecoder().raw_decode(text[match.start():]) + except (ValueError, RecursionError): + continue + consumed = match.start() + end + count += isinstance(value, dict) and "method" in value + return count + + +def _action_output_error(text: str, finish: str) -> str: + if finish == "length": + return "length_limit" + if finish != "stop": + return "abnormal_finish" + if _single_action_object(text) is None: + return "invalid_format" + return "" + + +def _decode_action(text: str | None, state: dict[str, Any]) -> ToolCall | None: + """Accept one whole JSON action, without repairing or ignoring any suffix.""" + value = _single_action_object(text) + if value is None or value["method"] not in _ALLOWED_METHODS.get(str(state.get("state", "")), set()): + return None + return ToolCall(name=value["method"], arguments=value["params"]) + + +def _parse_action(text: str | None, state: dict[str, Any]) -> ToolCall | None: + action = _decode_action(text, state) + if action is None: + return None + params = action.arguments + for key in ("cards", "targets"): + indices = params.get(key) + if indices is not None and ( + not isinstance(indices, list) + or not 1 <= len(indices) <= 5 + or any(not isinstance(index, int) for index in indices) + ): + return None + if _action_is_locally_legal(action, state): + return action + return None + + +def _repair_action(text: str | None, state: dict[str, Any]) -> ToolCall | None: + """Repair common index mistakes without hiding genuinely unknown output. + + Qwen occasionally emits duplicate card indices or an index just outside the + visible hand. Preserve the requested method, deduplicate/filter indices, + and only use the emergency fallback when no meaningful action can be + recovered. The episode still marks the repaired turn invalid for reward. + """ + action = _decode_action(text, state) + if action is None or action.name not in {"play", "discard"}: + return None + compact = json.loads(_compact_state(state)) + hand = compact.get("hand") if isinstance(compact.get("hand"), list) else [] + if not hand: + return None + values = action.arguments.get("cards") + if not isinstance(values, list): + return None + cleaned: list[int] = [] + for value in values: + if isinstance(value, int) and 0 <= value < len(hand) and value not in cleaned: + cleaned.append(value) + if not cleaned: + cleaned = [0] + cleaned = cleaned[:5] + blind = compact.get("blind") if isinstance(compact.get("blind"), dict) else {} + effect = str(blind.get("effect") or "").lower() + if action.name == "play" and "must play 5 cards" in effect: + cleaned.extend(index for index in range(len(hand)) if index not in cleaned) + cleaned = cleaned[:5] + repaired = ToolCall(action.name, {**action.arguments, "cards": cleaned}) + return repaired if _action_is_locally_legal(repaired, state) else None + + +def _fallback_action(state: dict[str, Any]) -> ToolCall: + name = state.get("state") + if name == "BLIND_SELECT": + return ToolCall("select", {}) + if name == "SELECTING_HAND": + return ToolCall("play", {"cards": [0]}) + if name == "SHOP": + return ToolCall("next_round", {}) + if name == "SMODS_BOOSTER_OPENED": + return ToolCall("pack", {"skip": True}) + raise RuntimeError(f"No fallback action for state {name!r}") + + +def _episode_reward_parts( + state: dict[str, Any], + *, + invalid_actions: int, + turns: int, + capped: bool, + terminal_reason: str = "", + partial_rewards: dict[str, float] | None = None, +) -> dict[str, float]: + """Return the complete-game reward and its components. + + ``partial_rewards`` contains transition-level shaping accumulated while the + game was played. The trainer still consumes one scalar per full-game + sample, so this is accumulated rather than emitted as separate samples. + Keeping the fallback calculation preserves the helper's useful standalone + behavior for callers that only have a terminal state. + """ + del capped + + if partial_rewards is not None: + parts = { + "progress": float(partial_rewards.get("progress", 0.0)), + "blind_clear_bonus": float(partial_rewards.get("blind_clear_bonus", 0.0)), + "ante_checkpoint_bonus": float( + partial_rewards.get("ante_checkpoint_bonus", 0.0) + ), + "legal_action_bonus": float(partial_rewards.get("legal_action_bonus", 0.0)), + "invalid_penalty": float(partial_rewards.get("invalid_penalty", 0.0)), + } + parts["win_bonus"] = 100.0 if state.get("won") else 0.0 + parts["cap_penalty"] = ( + DECISION_CAP_PENALTY if terminal_reason == "decision_cap" else 0.0 + ) + parts["total"] = sum(parts.values()) + parts["partial_total"] = parts["total"] - parts["win_bonus"] + return parts + + # Terminal-only fallback for tests and older callers. + blinds_cleared, fraction = _progress_value(state) + progress = float(blinds_cleared + fraction) + blind_clear_bonus = float(3.0 * blinds_cleared) + ante = max(1, int(state.get("ante_num") or 1)) + ante_checkpoints = max(0, ante - 1) + if state.get("won"): + ante_checkpoints = max(ante_checkpoints, FINAL_ANTE) + ante_checkpoint_bonus = _ante_clear_reward(0, ante_checkpoints) + # Formatting a legal JSON action is a prerequisite, not game progress. + # Rewarding it made repeatable no-op actions (notably rearrange) a policy + # objective. Keep the metric field for dashboard compatibility at zero. + legal_action_bonus = 0.0 + win_bonus = 100.0 if state.get("won") else 0.0 + invalid_penalty = float(-2.0 * invalid_actions) + # Context exhaustion may be a harness/token-budget limitation. Exhausting + # the explicit decision budget is a policy failure and must not pay. + cap_penalty = ( + DECISION_CAP_PENALTY if terminal_reason == "decision_cap" else 0.0 + ) + return { + "progress": progress, + "blind_clear_bonus": blind_clear_bonus, + "ante_checkpoint_bonus": ante_checkpoint_bonus, + "legal_action_bonus": legal_action_bonus, + "win_bonus": win_bonus, + "invalid_penalty": invalid_penalty, + "cap_penalty": cap_penalty, + "partial_total": ( + progress + + blind_clear_bonus + + ante_checkpoint_bonus + + legal_action_bonus + + invalid_penalty + ), + "total": ( + progress + + blind_clear_bonus + + ante_checkpoint_bonus + + legal_action_bonus + + win_bonus + + invalid_penalty + + cap_penalty + ), + } + + +def _progress_value(state: dict[str, Any]) -> tuple[int, float]: + """Return rewardable blind progress and current-blind score fraction. + + ``SKIPPED`` does advance the game's blind selector, but it must not count + as progress for reward. Treating it as a clear paid the same +4 shaping + as defeating a blind and trained the policy to skip Small and Big blinds + before immediately losing to the Boss. ``DEFEATED`` is the only status + that represents score-earned game progress. + """ + ante = max(1, int(state.get("ante_num") or 1)) + round_info = state.get("round") if isinstance(state.get("round"), dict) else {} + chips = float(round_info.get("chips") or 0) + blinds = state.get("blinds") if isinstance(state.get("blinds"), dict) else {} + status_values = [ + blind.get("status") + for name in ("small", "big", "boss") + for blind in [blinds.get(name)] + if isinstance(blind, dict) + ] + completed_statuses = {"DEFEATED"} + if status_values: + completed = sum(status in completed_statuses for status in status_values) + else: + # Preserve compatibility with older/partial game-state fixtures. + completed = max(0, int(state.get("round_num") or 0) - 1) + blinds_completed = (ante - 1) * 3 + completed + current_blind = next( + ( + float(blind.get("score") or 0) + for blind in blinds.values() + if isinstance(blind, dict) and blind.get("status") == "CURRENT" + ), + 0.0, + ) + fraction = min(1.0, chips / current_blind) if current_blind > 0 else 0.0 + return blinds_completed, fraction + + +def _ante_clear_reward(previous_highest: int, highest: int) -> float: + """Pay N * ante once per newly cleared ante, not per selector crossing.""" + return float(ANTE_CHECKPOINT_BONUS * sum(range(previous_highest + 1, highest + 1))) + + +def _transition_reward_parts( + previous_state: dict[str, Any], + state: dict[str, Any], + *, + valid_action: bool, + invalid_events: int = 0, + highest_rewarded_ante: int = 0, +) -> dict[str, float]: + """Shape reward from one environment transition. + + Progress is potential-based within the currently played blind. A clear is + recognized from the state transition itself rather than reconstructed from + ``ante_num``: the latter cannot distinguish previously skipped blinds from + defeated ones after the game advances to a new ante. + """ + _, previous_fraction = _progress_value(previous_state) + _, fraction = _progress_value(state) + previous_phase = str(previous_state.get("state") or "") + phase = str(state.get("state") or "") + blind_defeated = previous_phase == "SELECTING_HAND" and phase == "SHOP" + if blind_defeated: + # The completed blind has potential 1.0; do not double-pay score + # fraction that was already credited on earlier plays. + progress_delta = float(max(0.0, 1.0 - previous_fraction)) + elif previous_phase == "SELECTING_HAND" and phase == "SELECTING_HAND": + progress_delta = float(max(0.0, fraction - previous_fraction)) + else: + # BLIND_SELECT transitions include skips and tags. They are real game + # actions but create no score-earned progress. + progress_delta = 0.0 + blind_clear_delta = 3.0 if blind_defeated else 0.0 + previous_ante = max(1, int(previous_state.get("ante_num") or 1)) + ante = max(1, int(state.get("ante_num") or 1)) + # The caller carries this high-water mark across the whole episode. A + # Hieroglyph/Petroglyph purchase must not make an old ante rewardable again. + previous_highest = max(highest_rewarded_ante, previous_ante - 1) + highest = max(previous_highest, ante - 1) + ante_checkpoints = highest - previous_highest + ante_checkpoint_bonus = _ante_clear_reward(previous_highest, highest) + # Legal syntax alone is not a rewardable transition. + legal_bonus = 0.0 + invalid_penalty = -2.0 * max(0, int(invalid_events)) + return { + "progress": progress_delta, + "blind_clear_bonus": blind_clear_delta, + "ante_checkpoint_bonus": ante_checkpoint_bonus, + "ante_checkpoints": ante_checkpoints, + "highest_rewarded_ante": highest, + "blind_clear_count": int(blind_defeated), + "legal_action_bonus": legal_bonus, + "invalid_penalty": invalid_penalty, + "total": ( + progress_delta + + blind_clear_delta + + ante_checkpoint_bonus + + legal_bonus + + invalid_penalty + ), + } + + +def _episode_reward( + state: dict[str, Any], + *, + invalid_actions: int, + turns: int, + capped: bool, + terminal_reason: str = "", +) -> float: + return _episode_reward_parts( + state, + invalid_actions=invalid_actions, + turns=turns, + capped=capped, + terminal_reason=terminal_reason, + )["total"] + + +_TOKENIZERS: dict[str, Any] = {} + + +def _tokenizer(checkpoint: str) -> Any: + if checkpoint not in _TOKENIZERS: + from transformers import AutoTokenizer + + _TOKENIZERS[checkpoint] = AutoTokenizer.from_pretrained( + checkpoint, trust_remote_code=True + ) + return _TOKENIZERS[checkpoint] + + +def _thinking_budget(args: Any) -> int: + budget = getattr(args, "balatro_thinking_budget", 0) + if type(budget) is not int or budget not in (0, 8192): + raise ValueError("balatro_thinking_budget must be 0 or 8192") + return budget + + +def _thinking_action_text(text: str, enabled: bool) -> str | None: + """Only the final answer is an action; reasoning is retained for training.""" + if not enabled: + return text + if text.count("") != 1 or "" in text: + return None + reasoning, answer = text.split("") + if "<|im_end|>" in reasoning or "<|im_start|>" in text: + return None + return answer + + +def _balatro_system_prompt(thinking: bool = False) -> str: + if not thinking: + return BALATRO_SYSTEM_PROMPT + return BALATRO_SYSTEM_PROMPT.replace( + "one legal API action and return only minified JSON:", + "one legal API action. Think within the open thinking block for at most " + "8192 tokens. End thinking with ; if the budget is reached, the " + "harness closes the block. Then return only minified JSON:", + ).replace("Extra objects, prose,", "Extra objects or prose in the final answer,").replace( + "or hitting the output limit", "or hitting the final-answer output limit", + ) + + +def _chat_prompt(tokenizer: Any, user_message: str, *, thinking: bool = False) -> str: + if thinking: + user_message = user_message.replace("with no markdown or reasoning.", "with no markdown or reasoning in the final answer.") + messages = [ + { + "role": "system", + "content": _balatro_system_prompt(thinking), + }, + {"role": "user", "content": user_message}, + ] + try: + return tokenizer.apply_chat_template( + messages, + tokenize=False, + add_generation_prompt=True, + enable_thinking=thinking, + ) + except TypeError: + if thinking: + raise ValueError("Tokenizer must support enable_thinking for thinking runs") + return tokenizer.apply_chat_template( + messages, tokenize=False, add_generation_prompt=True + ) + + +def _feedback_chat_turn(tokenizer: Any, feedback: str, *, assistant_ended: bool, thinking: bool = False) -> str: + """Frame every next decision like the fresh-snapshot assistant turn. + + Only append template/control tokens; never retokenize sampled action IDs. + A length-limited answer needs a masked end-of-turn marker before feedback. + """ + messages = [{"role": "user", "content": feedback}] + try: + turn = tokenizer.apply_chat_template( + messages, tokenize=False, add_generation_prompt=True, enable_thinking=thinking, + ) + except TypeError: + if thinking: + raise ValueError("Tokenizer must support enable_thinking for thinking runs") + turn = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) + complete = _chat_prompt(tokenizer, feedback, thinking=thinking) + if thinking and not complete.endswith(turn): + # Qwen's thinking template adds a default reasoning-effort system + # message to a user-only render. Keep its user/assistant suffix and + # verify it against the full explicit-system render below. This model + # uses ChatML; unsupported framing still fails rather than guessing. + user_start = turn.find("<|im_start|>user\n") + if user_start >= 0: + turn = turn[user_start:] + if not turn or not complete.endswith(turn) or not tokenizer.eos_token: + raise ValueError("Tokenizer cannot frame a Balatro feedback turn consistently") + return ("" if assistant_ended else tokenizer.eos_token) + "\n" + turn + + +def _validate_balatro_sampling_params(params: dict[str, Any], *, evaluation: bool = False) -> None: + """Require the same unmasked distribution the trainer optimizes.""" + if not isinstance(params, dict): + raise ValueError("sampling_params must be a dictionary") + for constraint in ("regex", "json_schema", "ebnf"): + if params.get(constraint) is not None: + raise ValueError( + f"Balatro training cannot use {constraint}: the trainer does not replay grammar masks" + ) + for name in ("ignore_eos", "stop", "stop_token_ids", "stop_regex"): + if params.get(name): + raise ValueError(f"Balatro training cannot use {name}: actions must end naturally, without suppressing EOS or clipping trailing output") + # This harness does not record/replay top-p masks or logit processors. + # Greedy held-out evaluation is allowed; those samples never train. + defaults = { + "top_p": 1.0, "top_k": -1, "min_p": 0.0, + "presence_penalty": 0.0, "frequency_penalty": 0.0, + "repetition_penalty": 1.0, + } + if not evaluation: + defaults["temperature"] = 1.0 + for name, expected in defaults.items(): + if params.get(name, expected) != expected: + raise ValueError( + f"Balatro training requires {name}={expected}; sampling and training probabilities must match" + ) + for name in ("logit_bias", "custom_logit_processor"): + if params.get(name): + raise ValueError(f"Balatro training cannot use {name}: the trainer does not replay logit processors") + + +def _validate_balatro_sampling_config(recipe: Any) -> None: + """Reject incompatible sampling flags before checkpoint or launch side effects.""" + extra = getattr(recipe, "extra_config", None) or {} + budget = extra.get("balatro_thinking_budget", 0) + if type(budget) is not int or budget not in (0, 8192): + raise ValueError("balatro_thinking_budget must be 0 or 8192") + if budget: + template = json.loads(getattr(recipe, "apply_chat_template_kwargs", "{}") or "{}") + if template.get("enable_thinking") is not True: + raise ValueError("Thinking runs require enable_thinking=True") + for name in ("rollout_max_response_len", "eval_max_response_len"): + if getattr(recipe, name, 0) != budget + 96: + raise ValueError(f"Thinking runs require {name}={budget + 96}") + params = { + name: extra.get(f"rollout_{name}", getattr(recipe, f"rollout_{name}", default)) + for name, default in (("temperature", 1.0), ("top_p", 1.0), ("top_k", -1)) + } + _validate_balatro_sampling_params(params) + request = extra.get("sglang_request_params", getattr(recipe, "sglang_request_params", None)) or {} + if not isinstance(request, dict): + raise ValueError("sglang_request_params must be a dictionary") + _validate_balatro_sampling_params(request) + if "sampling_params" in request: + _validate_balatro_sampling_params(request["sampling_params"]) + + +async def _generate_action( + args: Any, + sampling_params: dict[str, Any], + prompt_ids: list[int], + state_name: str, +) -> tuple[list[int], str | None, str, list[float], list[int]]: + budget = _thinking_budget(args) + if not budget: + ids, text, finish, probabilities = await _sample_action_tokens(args, sampling_params, prompt_ids, 96) + return ids, text, finish, probabilities, [1] * len(ids) + tokenizer = _tokenizer(args.hf_checkpoint) + closing = tokenizer("", add_special_tokens=False)["input_ids"] + if len(closing) != 1 or closing[0] == tokenizer.unk_token_id: + raise ValueError("Thinking requires a dedicated token") + # A sampled close ends thinking early. At the budget, a masked control + # token ends thinking instead; the final answer gets its own allowance. + ids, text, finish, probabilities = await _sample_action_tokens( + args, sampling_params, prompt_ids, budget, stop_token=closing[0], + ) + mask = [1] * len(ids) + if text is None: + return ids, text, finish, probabilities, mask + text = tokenizer.decode(ids, skip_special_tokens=False) + budget_hit = finish == "length" and len(ids) == budget + if budget_hit and closing[0] not in ids and tokenizer.eos_token_id not in ids: + ids += closing + probabilities += [0.0] # Placeholder only: excluded by loss mask. + mask += [0] + text = tokenizer.decode(ids, skip_special_tokens=False) + if not ids or ids[-1] != closing[0] or not (finish == "stop" or budget_hit): + return ids, text, finish, probabilities, mask + answer_ids, answer, answer_finish, answer_probs = await _sample_action_tokens( + args, sampling_params, prompt_ids + ids, 96, + ) + if answer is None: + # Keep the already sampled reasoning for negative action credit. + return ids, text, answer_finish, probabilities, mask + ids += answer_ids + return (ids, tokenizer.decode(ids, skip_special_tokens=False), answer_finish, + probabilities + answer_probs, mask + [1] * len(answer_ids)) + + +async def _sample_action_tokens( + args: Any, sampling_params: dict[str, Any], prompt_ids: list[int], + limit: int, *, stop_token: int | None = None, +) -> tuple[list[int], str | None, str, list[float]]: + from slime.utils.http_utils import post + + params = deepcopy(sampling_params) + # Training temperature is checked at episode entry. This shared serving + # helper also handles greedy evaluation, which is never used for training. + _validate_balatro_sampling_params(params, evaluation=True) + context_limit, _ = _balatro_context_limits(args) + params["max_new_tokens"] = min( + int(params.get("max_new_tokens", limit)), limit, context_limit - 128 - len(prompt_ids) + ) + if params["max_new_tokens"] <= 0: + return [], None, "context", [] + if stop_token is not None: + # Internal phase boundary only. The final action still must end at EOS; + # user-provided stop strings/tokens remain forbidden by validation. + params["stop_token_ids"] = [stop_token] + params["skip_special_tokens"] = False + # The trainer computes unmasked token probabilities. SGLang applies a + # grammar mask BEFORE returning logprobs, so constrained samples are from + # a different policy even at temperature=1/top_p=1. Keep generation aligned + # with training; the prompt and action validator still enforce the API. + output = await post( + f"http://{args.sglang_router_ip}:{args.sglang_router_port}/generate", + { + "input_ids": prompt_ids, + "sampling_params": params, + "return_logprob": True, + }, + ) + finish = output.get("meta_info", {}).get("finish_reason", {}).get("type", "") + if finish == "abort": + return [], None, finish, [] + + response_ids = [ + item[1] for item in output.get("meta_info", {}).get("output_token_logprobs", []) + ] + log_probs = [ + float(item[0]) for item in output.get("meta_info", {}).get("output_token_logprobs", []) + ] + if len(response_ids) > params["max_new_tokens"]: + raise RuntimeError("Serving returned more tokens than requested") + if not response_ids or any(not math.isfinite(value) for value in log_probs): + raise RuntimeError("Serving returned missing or nonfinite token log probabilities") + return response_ids, output.get("text", ""), finish, log_probs + + +def _balatro_context_limits(args: Any) -> tuple[int, int]: + """Account for serving capacity and the pinned trainer's CP/TP padding.""" + cp = max(1, int(getattr(args, "context_parallel_size", 1))) + tp = max(1, int(getattr(args, "tensor_model_parallel_size", 1))) + padding = cp * tp * 128 + limits = [int(getattr(args, "balatro_context_len", 49152))] + for name in ("sglang_context_length", "max_position_embeddings"): + if value := getattr(args, name, None): + limits.append(int(value)) + if value := getattr(args, "max_tokens_per_gpu", None): + limits.append(int(value) * cp) + hard = min(limits) // padding * padding + requested = int(getattr(args, "balatro_context_len", 49152)) // padding * padding + if hard < requested: + raise ValueError( + f"Balatro requested {requested} context tokens but the trainer/serving " + f"limits permit only {hard}. Set max_position_embeddings and " + "sglang_context_length explicitly; do not silently clamp the game history." + ) + target = min(int(getattr(args, "balatro_segment_tokens", 8192)), hard) + if min(target, hard) <= 224: + raise ValueError("Balatro context budgets must leave room for a snapshot and action") + return hard, target + + +def _validate_balatro_context_config(recipe: Any) -> tuple[int, int]: + """Resolve explicit recipe flags before any launch side effects.""" + from types import SimpleNamespace + + extra = getattr(recipe, "extra_config", None) or {} + values = {} + for name in ( + "balatro_context_len", "balatro_segment_tokens", "max_position_embeddings", + "sglang_context_length", "max_tokens_per_gpu", "context_parallel_size", + "tensor_model_parallel_size", + ): + value = extra.get(name, getattr(recipe, name, None)) + if value is not None: + values[name] = value + for name in ("max_position_embeddings", "sglang_context_length"): + if not values.get(name): + raise ValueError(f"Balatro requires explicit {name}; inherited defaults can clamp context to 4096") + backend = extra.get("attention_backend", getattr(recipe, "attention_backend", None)) + if int(values.get("context_parallel_size", 1)) > 1 and backend == "unfused": + raise ValueError("Unfused attention does not support context parallelism; use flash for Balatro CP>1") + return _balatro_context_limits(SimpleNamespace(**values)) + + +def _invalid_action_weight(args: Any) -> float: + """Penalty magnitude in advantage units, independent of game-score spread.""" + import math + + extra = getattr(args, "extra_config", None) or {} + value = extra.get("balatro_invalid_action_weight", getattr(args, "balatro_invalid_action_weight", 5.0)) + try: + weight = float(value) + except (TypeError, ValueError): + raise ValueError("balatro_invalid_action_weight must be finite and positive") from None + if isinstance(value, bool) or not math.isfinite(weight) or weight <= 0: + raise ValueError("balatro_invalid_action_weight must be finite and positive") + return weight + + +def _validate_balatro_loss_config(args: Any) -> None: + """Check loss weighting locally, before launching GPUs, and on workers.""" + extra = getattr(args, "extra_config", None) or {} + per_token = extra.get( + "calculate_per_token_loss", getattr(args, "calculate_per_token_loss", True) + ) + if per_token is not False: + raise ValueError("Segmented Balatro requires calculate_per_token_loss=False (per-game mean)") + mode = extra.get("balatro_credit_assignment", getattr(args, "balatro_credit_assignment", "episode")) + if mode not in {"episode", "reward_to_go"}: + raise ValueError(f"Unknown Balatro credit assignment: {mode}") + if mode == "reward_to_go": + _invalid_action_weight(args) + if extra.get("normalize_advantages", getattr(args, "normalize_advantages", False)): + raise ValueError("Action credit requires normalize_advantages=False to preserve invalid-action penalties") + path = extra.get("custom_advantage_function_path", getattr(args, "custom_advantage_function_path", None)) + if path != "balatro_credit.compute_advantages": + raise ValueError("reward_to_go requires the Balatro token-advantage hook") + if extra.get("kl_coef", getattr(args, "kl_coef", 0)) != 0: + raise ValueError("reward_to_go requires kl_coef=0; use the separate KL loss") + + +def _validate_balatro_trainer(args: Any, sample_type: Any) -> None: + """Fail before acquiring a game if a trainer cannot weight sibling samples.""" + import inspect + + _validate_balatro_loss_config(args) + if "rollout_id" not in getattr(sample_type, "__dataclass_fields__", {}): + raise RuntimeError("This Slime Sample lacks shared rollout_id support; use the pinned Modal Dojo image") + root = Path(inspect.getfile(sample_type)).resolve().parents[1] + if getattr(args, "balatro_credit_assignment", "episode") == "reward_to_go": + if "train_metadata" not in getattr(sample_type, "__dataclass_fields__", {}): + raise RuntimeError("This Slime Sample lacks train_metadata for action credit") + if "BALATRO_TRAIN_METADATA" not in (root / "ray/rollout.py").read_text(): + raise RuntimeError("Trainer is missing the Balatro reward-to-go metadata transport patch") + for relative, required in ( + ("ray/rollout.py", "rollout_mask_sums"), + ("backends/megatron_utils/loss.py", 'batch["rollout_mask_sums"]'), + ("backends/megatron_utils/cp_utils.py", "sample_denoms"), + ): + if required not in (root / relative).read_text(): + raise RuntimeError(f"Slime lacks per-game segment reduction in {relative}") + + +class _StateBudgetExceeded(RuntimeError): + pass + + +class _SnapshotUnavailable(RuntimeError): + pass + + +class _BalatroContext: + """Bounded inference prefixes and their exact, separately trained sequences.""" + + def __init__(self, tokenizer: Any, hard_limit: int, target: int, *, thinking_budget: int = 0): + self.tokenizer = tokenizer + self.hard_limit = hard_limit + self.target = target + self.thinking = bool(thinking_budget) + self.max_action_tokens = thinking_budget + 96 + int(self.thinking) + self.reserve = self.max_action_tokens + 128 + self.segments: list[dict[str, Any]] = [] + self.current: dict[str, Any] | None = None + self.needs_reset = True + self.peak_tokens = 0 + self.snapshot_tokens = 0 + + def begin(self, prompt: str, user_message: str, turn: int) -> None: + ids = self.tokenizer(prompt, add_special_tokens=False)["input_ids"] + if len(ids) + self.reserve > self.hard_limit: + raise _StateBudgetExceeded( + f"Current snapshot needs {len(ids)} tokens; input allowance is {self.hard_limit - self.reserve}" + ) + self.finish() + self.current = { + "prompt": prompt, "prompt_ids": ids, "response_ids": [], + "response": "", "loss_mask": [], "log_probs": [], "actions": [], + "turn_start": turn, + "messages": [ + {"role": "system", "content": _balatro_system_prompt(self.thinking)}, + {"role": "user", "content": user_message}, + ], + } + self.snapshot_tokens += len(ids) + self.needs_reset = False + self.peak_tokens = max(self.peak_tokens, len(ids)) + + def input_ids(self) -> list[int]: + segment = self.current + if segment is None or self.needs_reset: + raise RuntimeError("A fresh snapshot is required before generation") + ids = segment["prompt_ids"] + segment["response_ids"] + if len(ids) + self.reserve > self.hard_limit: + raise RuntimeError("Context manager produced an oversized request") + return ids + + def add_action(self, ids: list[int], text: str, log_probs: list[float], *, turn: int, archive_start: int, + loss_mask: list[int] | None = None) -> None: + segment = self.current + if not ids or len(ids) > self.max_action_tokens or len(ids) != len(log_probs): + raise RuntimeError("Missing or misaligned generated action tokens/log probabilities") + if segment is None or len(self.input_ids()) + len(ids) > self.hard_limit: + raise RuntimeError("Invalid action context") + mask = [1] * len(ids) if loss_mask is None else loss_mask + if len(mask) != len(ids) or any(type(m) is not int or m not in (0, 1) for m in mask) or not any(mask): + raise RuntimeError("Invalid generated action loss mask") + start = len(segment["response_ids"]) + spans = [] + for offset, enabled in enumerate(mask): + if enabled: + if offset == 0 or not mask[offset - 1]: + spans.append([start + offset, start + offset + 1]) + else: + spans[-1][1] = start + offset + 1 + segment["actions"].append({ + "turn": turn, "token_start": start, "token_end": start + len(ids), + "episode_token_start": archive_start, "episode_token_end": archive_start + len(ids), + "policy_token_spans": spans, "sampled_tokens": sum(mask), + "forced_thinking_close": bool(self.thinking and 0 in mask), + }) + segment["response_ids"].extend(ids) + segment["loss_mask"].extend(mask) + segment["log_probs"].extend(log_probs) + segment["response"] += text + segment["messages"].append({"role": "assistant", "content": text}) + self.peak_tokens = max(self.peak_tokens, len(segment["prompt_ids"]) + len(segment["response_ids"])) + + def add_feedback(self, feedback: str, ids: list[int]) -> None: + segment = self.current + if segment is None: + raise RuntimeError("Feedback without an action") + framed = _feedback_chat_turn( + self.tokenizer, feedback, + assistant_ended=segment["response_ids"][-1] == self.tokenizer.eos_token_id, + thinking=self.thinking, + ) + ids = self.tokenizer(framed, add_special_tokens=False)["input_ids"] + size = len(segment["prompt_ids"]) + len(segment["response_ids"]) + len(ids) + segment["messages"].append({"role": "tool", "content": feedback.strip()}) + if size + self.reserve > self.target or len(segment["messages"]) >= 126: + # Keep this result in the episode archive. The next request gets + # its resulting state as a full snapshot, not a detached delta. + self.needs_reset = True + return + segment["response_ids"].extend(ids) + segment["loss_mask"].extend([0] * len(ids)) + segment["log_probs"].extend([0.0] * len(ids)) + segment["response"] += framed + self.peak_tokens = max(self.peak_tokens, size) + + def finish(self) -> None: + if self.current is not None and self.current["actions"]: + self.segments.append(self.current) + self.current = None + + +def _segment_reward_events(events: list[dict[str, Any]], segments: list[dict[str, Any]]) -> list[list[dict[str, Any]]]: + """Attach a checkpoint once, at its last action, with explicit span mappings.""" + result: list[list[dict[str, Any]]] = [[] for _ in segments] + for event in events: + start, end = event.get("token_start", 0), event["token_end"] + spans = [] + for index, segment in enumerate(segments): + actions = [a for a in segment["actions"] if a["episode_token_start"] < end and a["episode_token_end"] > start] + if actions: + spans.append({"segment_id": index, "token_start": actions[0]["token_start"], "token_end": actions[-1]["token_end"]}) + if spans: + final = spans[-1] + result[final["segment_id"]].append({ + **event, "episode_token_start": start, "episode_token_end": end, + "token_start": final["token_start"], "token_end": final["token_end"], + "segment_spans": spans, + }) + return result + + +def _action_payload(action: ToolCall) -> dict[str, Any]: + return {"method": action.name, "params": action.arguments} + + +# Sentinel: a subtree that is identical to the previous turn's. +_UNCHANGED = object() + + +def _state_delta(previous: Any, current: Any) -> Any: + """Recursive diff of two compact states. + + Within one bounded conversation, reconstruct state from its initial full + snapshot and these deltas. Arrays replace atomically, null deletes a key. + """ + if not isinstance(previous, dict) or not isinstance(current, dict): + return current if current != previous else _UNCHANGED + + delta: dict[str, Any] = {} + for key, value in current.items(): + if key not in previous: + delta[key] = value + continue + nested = _state_delta(previous[key], value) + if nested is not _UNCHANGED: + delta[key] = nested + # Keys the game dropped this turn (a joker sold, a pack closed). + for key in previous: + if key not in current: + delta[key] = None + return delta or _UNCHANGED + + +# Cheap scalars always echoed, so the model never has to scan back to orient. +_DELTA_ANCHORS = ("phase", "ante", "round", "money") + + +def _delta_payload(previous: dict[str, Any] | None, current: dict[str, Any]) -> Any: + """Always retain deletion markers; an unmarked full state is not a delta.""" + if previous is None: + return current + + delta = _state_delta(previous, current) + delta = {} if delta is _UNCHANGED else delta + for anchor in _DELTA_ANCHORS: + if anchor in current: + delta.setdefault(anchor, current[anchor]) + + return delta + + +def _selected_blind(compact: dict[str, Any] | None) -> tuple[str, dict[str, Any]] | None: + """Return the blind currently offered at BLIND_SELECT. + + Compact states keep every blind in ``blinds`` so a skipped blind and the + newly offered blind can be explained without consulting the raw engine + state. Keep this helper deliberately tolerant of partial state deltas. + """ + if not isinstance(compact, dict): + return None + blinds = compact.get("blinds") + if not isinstance(blinds, dict): + return None + for name, blind in blinds.items(): + if isinstance(blind, dict) and str(blind.get("status") or "").upper() == "SELECT": + return str(name), blind + return None + + +def _skip_transition( + previous: dict[str, Any] | None, current: dict[str, Any] +) -> dict[str, Any] | None: + """Summarize what a valid ``skip`` action changed for the model/viewer.""" + before = _selected_blind(previous) + after = _selected_blind(current) + if before is None: + return None + before_name, before_blind = before + next_name, next_blind = after if after is not None else (None, None) + result: dict[str, Any] = { + "method": "skip", + "skipped": { + "id": before_name, + "name": before_blind.get("name") or before_name, + "type": before_blind.get("type"), + }, + } + tag = before_blind.get("tag") + tag_effect = before_blind.get("tag_effect") + if tag: + result["tag"] = tag + if tag_effect: + result["tag_effect"] = tag_effect + if next_blind is not None: + result["next"] = { + "id": next_name, + "name": next_blind.get("name") or next_name, + "type": next_blind.get("type"), + "score": next_blind.get("score"), + "effect": next_blind.get("effect"), + } + return result + + +def _environment_feedback( + state: dict[str, Any], + *, + previous_state_name: str, + previous_compact: dict[str, Any] | None, + executed: ToolCall | None, + valid: bool, + error: str = "", +) -> tuple[str, dict[str, Any]]: + compact_state = json.loads(_compact_state(state)) + view = _delta_payload(previous_compact, compact_state) + if valid and not error: + payload = view + # A skip is otherwise easy to miss in a sparse delta: the old blind + # becomes SKIPPED and the next one becomes SELECT. Include a compact, + # explicit explanation so both the model and trajectory viewers know + # which tag was earned and what blind is now available. + if executed is not None and executed.name == "skip" and isinstance(payload, dict): + if transition := _skip_transition(previous_compact, compact_state): + payload = {**payload, "action_result": transition} + else: + payload = { + **({"fallback": _action_payload(executed)} if executed is not None else {"executed": None}), + "error": error or "invalid_model_action", + "state": view, + } + feedback = ( + "\n" + + json.dumps(payload, separators=(",", ":"), ensure_ascii=False) + + "" + ) + feedback += f"\nLegal: {_action_help(state)}" + return feedback + "\nReturn one JSON action.\n", compact_state + + +def _log_balatro_event(kind: str, payload: dict[str, Any]) -> None: + print( + f"{kind} {json.dumps(payload, separators=(',', ':'), ensure_ascii=False)}", + flush=True, + ) + + +async def generate_balatro_episode( + args: Any, + sample: Any, + sampling_params: dict[str, Any], + evaluation: bool = False, +) -> Any: + """Play one continuous game; return bounded samples with one shared reward.""" + from slime.utils.types import Sample + + _validate_balatro_sampling_params(sampling_params, evaluation=evaluation) + thinking_budget = _thinking_budget(args) + if not evaluation: + _validate_balatro_trainer(args, Sample) + source_metadata = dict(getattr(sample, "metadata", None) or {}) + seed = str(source_metadata.get("balatro_seed") or sample.prompt) + # Bound all sampled decisions, including rejected responses and retries. + max_decisions = int(getattr(args, "balatro_max_decisions", 250)) + context_limit, segment_target = _balatro_context_limits(args) + capture_screenshot = bool(getattr(args, "balatro_capture_screenshot", True)) + rollout_id = time.time_ns() & ((1 << 63) - 1) + env: BalatroSandboxEnvironment | None = None + startup_error = "" + state: dict[str, Any] = {} + episode_started = time.monotonic() + episode_trace: list[dict[str, Any]] = [] + + def span(name: str, started: float, **attributes: Any) -> None: + episode_trace.append( + { + "name": name, + "start": started - episode_started, + "end": time.monotonic() - episode_started, + "attributes": attributes, + } + ) + + # A transient sandbox startup failure should not kill a whole GPU job. + for attempt in range(3): + acquire_started = time.monotonic() + try: + env = await asyncio.to_thread(_get_env_pool().acquire, seed) + span( + "balatro.sandbox.acquire", acquire_started, attempt=attempt + 1, ok=True + ) + # Health can pass just before the game process exits. Starting the + # seeded game is part of startup, and is safe to retry on a fresh + # sandbox because no policy action has been requested yet. + game_start = time.monotonic() + state = await asyncio.to_thread(env.start, seed) + span("balatro.game.start", game_start, attempt=attempt + 1, + state=state.get("state", "")) + break + except Exception as exc: + startup_error = str(exc)[-1200:] + span( + "balatro.sandbox.acquire", + acquire_started, + attempt=attempt + 1, + ok=False, + ) + _log_balatro_event( + "BALATRO_SANDBOX_ERROR", + {"seed": seed, "attempt": attempt + 1, "error": str(exc)[-1200:]}, + ) + if env is not None: + try: + await asyncio.to_thread(env.close) + except Exception as close_exc: + _log_balatro_event("BALATRO_SANDBOX_CLOSE_ERROR", { + "seed": seed, "error": str(close_exc)[-1200:], + }) + env = None + if attempt < 2: + await asyncio.sleep(2 * (attempt + 1)) + if env is None: + if evaluation: + raise RuntimeError(f"Balatro evaluation startup failed for {seed} after 3 attempts: {startup_error}") + sample.status = Sample.Status.ABORTED + sample.metadata = {**source_metadata, "exit_status": "sandbox_startup_error", "error": startup_error} + return sample + + tokenizer = _tokenizer(args.hf_checkpoint) + context = _BalatroContext(tokenizer, context_limit, segment_target, thinking_budget=thinking_budget) + initial_prompt = "" + initial_prompt_ids: list[int] = [] + response_token_ids: list[int] = [] + loss_mask: list[int] = [] + trajectory_text = "" + trajectory_messages: list[dict[str, str]] = [ + { + "role": "system", + "content": _balatro_system_prompt(bool(thinking_budget)), + } + ] + invalid_actions = 0 + invalid_turns = 0 + fallback_actions = 0 + repaired_actions = 0 + api_errors = 0 + turns_taken = 0 + consecutive_rejections = 0 + executed_actions = 0 + output_format_errors = 0 + multiple_action_responses = 0 + length_limit_responses = 0 + normal_stop_responses = 0 + # Previous turn's compact state, so feedback can be sent as a delta. + previous_compact: dict[str, Any] | None = None + last_result: dict[str, Any] | None = None + + def append_tool_feedback(feedback: str) -> bool: + """Archive every transition; only retain fitting feedback in active context.""" + nonlocal trajectory_text, last_result + trajectory_messages.append({"role": "tool", "content": feedback.strip()}) + trajectory_text += feedback + feedback_ids = tokenizer(feedback, add_special_tokens=False)["input_ids"] + response_token_ids.extend(feedback_ids) + loss_mask.extend([0] * len(feedback_ids)) + context.add_feedback(feedback, feedback_ids) + # The last executed action and any recovery error remain available + # even when the feedback itself is replaced by a fresh snapshot. + payload = json.loads(feedback.split("", 1)[1].split("", 1)[0]) + last_result = {"turn": turns_taken, "executed": _action_payload(executed) if executed is not None else None} + for key in ("error", "fallback", "action_result"): + if key in payload: + last_result[key] = payload[key] + return True + + capped = False + aborted = False + episode_error = "" + terminal_reason = "game_over" + screenshot = "" + partial_reward_parts = { + "progress": 0.0, + "blind_clear_bonus": 0.0, + "ante_checkpoint_bonus": 0.0, + "legal_action_bonus": 0.0, + "invalid_penalty": 0.0, + } + ante_checkpoints = 0 + highest_rewarded_ante = 0 + partial_reward_steps = 0 + # Keep checkpoint-window rewards separate from the episode accumulators. + # A blind-clear event credits everything earned since the previous blind + # clear (all card moves included); an ante-clear event credits everything + # earned across that ante, including skipped blinds. These windows are + # observability/credit-assignment records; ``sample.reward`` remains the + # single additive game-level training signal. + partial_reward_trace: list[dict[str, Any]] = [] + blind_reward_pending = dict(partial_reward_parts) + ante_reward_pending = dict(partial_reward_parts) + blind_window_turn: int | None = None + ante_window_turn: int | None = None + blind_window_token: int | None = None + ante_window_token: int | None = None + + def _add_pending(target: dict[str, float], values: dict[str, float]) -> None: + for key in target: + target[key] += float(values.get(key, 0.0)) + + def _pending_total(values: dict[str, float]) -> float: + return float(sum(values.values())) + + def _reset_pending(values: dict[str, float]) -> None: + for key in values: + values[key] = 0.0 + + def _append_checkpoint_event( + *, + label: str, + state_name: str, + turn: int, + token_start: int | None, + token_end: int, + values: dict[str, float], + ) -> None: + if not any(abs(value) > 1e-12 for value in values.values()): + return + event: dict[str, Any] = { + "turn": turn, + "state": state_name, + "label": label, + "reward": _pending_total(values), + # This is the actual game-level cumulative total. It is preferable + # to summing event rewards because an ante window intentionally + # overlaps its blind-clear windows. + "cumulative_reward": sum(partial_reward_parts.values()), + "progress": values["progress"], + "blind_clear_bonus": values["blind_clear_bonus"], + "ante_checkpoint_bonus": values["ante_checkpoint_bonus"], + "legal_action_bonus": values["legal_action_bonus"], + "invalid_penalty": values["invalid_penalty"], + "total": _pending_total(values), + } + if token_start is not None: + event["token_start"] = token_start + event["token_end"] = token_end + partial_reward_trace.append(event) + + previous_reward_state: dict[str, Any] | None = None + try: + previous_reward_state = state + + if state.get("snapshot_version") != 1: + raise _SnapshotUnavailable("Game assets lack snapshot_version=1; prepare updated assets before training") + + first_turn = _turn_prompt(state) + trajectory_messages.append({"role": "user", "content": first_turn}) + initial_prompt = _chat_prompt(tokenizer, first_turn, thinking=bool(thinking_budget)) + initial_prompt_ids = tokenizer(initial_prompt, add_special_tokens=False)[ + "input_ids" + ] + context.begin(initial_prompt, first_turn, 1) + previous_compact = json.loads(_compact_state(state)) + + for turn in range(max_decisions): + if env._terminal(state): + terminal_reason = "won" if state.get("won") else "game_over" + break + if context.needs_reset: + first_turn = _turn_prompt(state, last_result) + context.begin(_chat_prompt(tokenizer, first_turn, thinking=bool(thinking_budget)), first_turn, turn + 1) + previous_compact = json.loads(_compact_state(state)) + span("balatro.context.reset", time.monotonic(), turn=turn + 1, segment_id=len(context.segments)) + + state_name = str(state.get("state", "")) + # Reward events are aligned to the response/loss-mask sequence, + # which excludes the initial prompt. The transition reward is + # emitted after this action and can therefore be attached to the + # action tokens that caused it. + action_token_start = len(response_token_ids) + generate_started = time.monotonic() + generated_ids, response, finish, generated_log_probs, generated_mask = await _generate_action( + args, + sampling_params, + context.input_ids(), + state_name, + ) + span( + "balatro.model.generate", + generate_started, + turn=turn, + state=state_name, + tokens=len(generated_ids), + finish=finish, + ) + if response is None: + aborted = finish == "abort" + capped = finish == "context" + terminal_reason = "model_abort" if aborted else "context_cap" + break + + turns_taken = turn + 1 + context.add_action( + generated_ids, response, generated_log_probs, + turn=turn + 1, archive_start=action_token_start, + loss_mask=generated_mask, + ) + # Store the reward already earned BEFORE this action. Differencing + # against the final total yields a causal return-to-go, including + # invalid-action penalties and terminal bonuses exactly once. + context.current["actions"][-1]["reward_before"] = sum(partial_reward_parts.values()) + # Record interventions separately from the displayed game reward. + # Tokens belong to the requested action, never the harness repair. + action_credit = context.current["actions"][-1] + action_credit["invalid_events"] = 0 + + response_token_ids.extend(generated_ids) + action_token_end = len(response_token_ids) + loss_mask.extend(generated_mask) + trajectory_text += response + trajectory_messages.append({"role": "assistant", "content": response}) + + parse_started = time.monotonic() + action_text = _thinking_action_text(response, bool(thinking_budget)) + output_error = _action_output_error(action_text or "", finish) + output_format_errors += _single_action_object(action_text) is None + multiple_action_responses += _action_object_count(action_text or "") > 1 + length_limit_responses += finish == "length" + normal_stop_responses += finish == "stop" + action = _parse_action(action_text, state) if not output_error else None + if thinking_budget: + closing = tokenizer("", add_special_tokens=False)["input_ids"][0] + reasoning_tokens = generated_ids.index(closing) + 1 if closing in generated_ids else len(generated_ids) + action_credit["thinking_tokens"] = sum(generated_mask[:reasoning_tokens]) + action_credit["final_answer_tokens"] = len(generated_ids) - reasoning_tokens + action_credit["finish_reason"] = finish + action_credit["output_error"] = output_error or ("illegal_action" if action is None else "") + invalid = action is None + repaired_turn = False + span("balatro.action.parse", parse_started, turn=turn, valid=not invalid) + if invalid: + # Do not execute a prefix, repair, or fallback on behalf of a + # rejected response. Retain the full sampled span for negative + # credit and retry the unchanged game with explicit feedback. + consecutive_rejections += 1 + invalid_actions += 1 + invalid_turns += 1 + action_credit["invalid_events"] += 1 + partial_reward_parts["invalid_penalty"] -= 2.0 + partial_reward_steps += 1 + blind_reward_pending["invalid_penalty"] -= 2.0 + ante_reward_pending["invalid_penalty"] -= 2.0 + if blind_window_turn is None: + blind_window_turn, blind_window_token = turn + 1, action_token_start + if ante_window_turn is None: + ante_window_turn, ante_window_token = turn + 1, action_token_start + executed = None + error = (f"Rejected response: {action_credit['output_error']}. No action was executed. " + "Return exactly one complete legal JSON object, no prose or extra objects, " + "then end your response.") + _log_balatro_event("BALATRO_OUTPUT_REJECTED", { + "seed": seed, "episode_id": rollout_id, "turn": turn, + "reason": action_credit["output_error"], "finish": finish, + "tokens": len(generated_ids), "response": response[:500], + "consecutive_rejections": consecutive_rejections, + }) + feedback, previous_compact = _environment_feedback( + state, previous_state_name=state_name, previous_compact=previous_compact, + executed=None, valid=False, error=error, + ) + append_tool_feedback(feedback) + if consecutive_rejections >= 3: + terminal_reason = "invalid_action_limit" + break + continue + consecutive_rejections = 0 + executed_actions += 1 + + executed = action + rpc_started = time.monotonic() + result = await asyncio.to_thread(env.step, action) + api_error = "" + if result.observation.is_error: + action_credit["invalid_events"] += 1 + invalid_actions += 1 + api_errors += 1 + api_error = result.observation.text[:500] + # ``step`` refreshes the environment after an RPC error. Use + # that state for recovery; ``state`` is the model's previous + # observation and may already be stale. + refreshed_state = result.observation.metadata.get("state") + if isinstance(refreshed_state, dict) and refreshed_state: + state = refreshed_state + try: + fallback = _fallback_action(state) + except Exception as fallback_exc: + episode_error = f"{api_error}; no fallback: {fallback_exc}" + terminal_reason = ( + "game_over" if env._terminal(state) else "rpc_error" + ) + partial_reward_parts["invalid_penalty"] -= 2.0 * ( + int(invalid) + 1 + ) + feedback, previous_compact = _environment_feedback( + state, + previous_state_name=state_name, + previous_compact=previous_compact, + executed=action, + valid=False, + error=episode_error, + ) + append_tool_feedback(feedback) + span( + f"balatro.rpc.{action.name}", + rpc_started, + turn=turn, + valid=False, + fallback=False, + refreshed_state=state.get("state", ""), + ) + break + fallback_actions += 1 + executed = fallback + _log_balatro_event( + "BALATRO_ACTION_ERROR", + { + "seed": seed, + "episode_id": rollout_id, + "turn": turn, + "state": state_name, + "state_after_error": state.get("state", ""), + "error": api_error, + "requested": _action_payload(action), + "fallback": _action_payload(fallback), + }, + ) + result = await asyncio.to_thread(env.step, fallback) + if result.observation.is_error: + api_errors += 1 + episode_error = result.observation.text[:500] + terminal_reason = "rpc_error" + refreshed_state = result.observation.metadata.get("state") + if isinstance(refreshed_state, dict) and refreshed_state: + state = refreshed_state + # Preserve the failed turn in the trajectory. Previously + # this branch broke before adding a tool message, leaving + # viewers with an unpaired final action that looked like a + # spontaneous game over. + feedback, previous_compact = _environment_feedback( + state, + previous_state_name=state_name, + previous_compact=previous_compact, + executed=executed, + valid=False, + error=episode_error, + ) + append_tool_feedback(feedback) + partial_reward_parts["invalid_penalty"] -= 2.0 * (int(invalid) + 1) + span( + f"balatro.rpc.{action.name}", + rpc_started, + turn=turn, + valid=False, + fallback=True, + refreshed_state=state.get("state", ""), + ) + break + span( + f"balatro.rpc.{executed.name}", + rpc_started, + turn=turn, + valid=not result.observation.is_error, + fallback=bool(api_error), + repaired=repaired_turn, + ) + state = result.observation.metadata["state"] + + # Accumulate shaping at the transition where it occurs. The + # trainer still receives one reward for this complete game, but + # the breakdown now reflects the sequence of partial outcomes. + if previous_reward_state is not None: + transition_rewards = _transition_reward_parts( + previous_reward_state, + state, + valid_action=not invalid and not api_error, + invalid_events=int(invalid) + int(bool(api_error)), + highest_rewarded_ante=highest_rewarded_ante, + ) + for key in partial_reward_parts: + partial_reward_parts[key] += transition_rewards[key] + ante_checkpoints += int(transition_rewards["ante_checkpoints"]) + highest_rewarded_ante = int(transition_rewards["highest_rewarded_ante"]) + partial_reward_steps += 1 + _add_pending(blind_reward_pending, transition_rewards) + _add_pending(ante_reward_pending, transition_rewards) + if blind_window_turn is None: + blind_window_turn = turn + 1 + blind_window_token = action_token_start + if ante_window_turn is None: + ante_window_turn = turn + 1 + ante_window_token = action_token_start + + blind_clear_count = int(transition_rewards["blind_clear_count"]) + ante_clear_count = int(transition_rewards["ante_checkpoints"]) + if ante_clear_count: + # An ante event intentionally spans all of its blinds, + # including any that were skipped in one transition. + _append_checkpoint_event( + label="ANTE_CLEAR", + state_name=state_name, + turn=turn + 1, + token_start=ante_window_token, + token_end=action_token_end, + values=ante_reward_pending, + ) + _reset_pending(ante_reward_pending) + _reset_pending(blind_reward_pending) + ante_window_turn = None + blind_window_turn = None + ante_window_token = None + blind_window_token = None + elif blind_clear_count: + _append_checkpoint_event( + label="BLIND_CLEAR", + state_name=state_name, + turn=turn + 1, + token_start=blind_window_token, + token_end=action_token_end, + values=blind_reward_pending, + ) + _reset_pending(blind_reward_pending) + blind_window_turn = None + blind_window_token = None + previous_reward_state = state + + feedback, previous_compact = _environment_feedback( + state, + previous_state_name=state_name, + previous_compact=previous_compact, + executed=executed, + valid=not invalid and not api_error, + error=api_error, + ) + if not append_tool_feedback(feedback): + break + + if result.done: + terminal_reason = "won" if state.get("won") else "game_over" + break + else: + capped = True + terminal_reason = "decision_cap" + except _SnapshotUnavailable as exc: + episode_error = str(exc) + terminal_reason = "state_schema_mismatch" + except _StateBudgetExceeded as exc: + episode_error = str(exc) + terminal_reason = "state_budget_exceeded" + capped = True + except Exception as exc: + episode_error = str(exc)[-1200:] + terminal_reason = "episode_error" + invalid_actions += 3 + partial_reward_parts["invalid_penalty"] -= 6.0 + _log_balatro_event( + "BALATRO_EPISODE_ERROR", + {"seed": seed, "episode_id": rollout_id, "error": episode_error}, + ) + finally: + if capture_screenshot and env is not None: + screenshot_started = time.monotonic() + try: + screenshot = await asyncio.to_thread(env.screenshot_data_uri) + span("balatro.screenshot", screenshot_started, ok=True) + except Exception as exc: + span("balatro.screenshot", screenshot_started, ok=False) + _log_balatro_event( + "BALATRO_SCREENSHOT_ERROR", + {"seed": seed, "episode_id": rollout_id, "error": str(exc)}, + ) + await asyncio.to_thread(env.close) + + context.finish() + if not context.segments: + sample.status = Sample.Status.ABORTED + sample.metadata = {**source_metadata, "exit_status": terminal_reason, "error": episode_error} + return sample + + # A win can occur on the ante-8 boss before the game increments + # ``ante_num``. Credit that final checkpoint exactly once, while avoiding + # double-counting if the environment has already advanced the ante. + final_ante = max(1, int(state.get("ante_num") or 1)) + expected_checkpoints = min(FINAL_ANTE, final_ante) + if state.get("won") and highest_rewarded_ante < expected_checkpoints: + missing_checkpoints = expected_checkpoints - highest_rewarded_ante + checkpoint_reward = _ante_clear_reward(highest_rewarded_ante, expected_checkpoints) + highest_rewarded_ante = expected_checkpoints + partial_reward_parts["ante_checkpoint_bonus"] += checkpoint_reward + ante_checkpoints += missing_checkpoints + ante_reward_pending["ante_checkpoint_bonus"] += checkpoint_reward + _append_checkpoint_event( + label="WON", + state_name="WON", + turn=turns_taken, + token_start=ante_window_token, + token_end=len(response_token_ids), + values=ante_reward_pending, + ) + _reset_pending(ante_reward_pending) + _reset_pending(blind_reward_pending) + elif any(abs(value) > 1e-12 for value in blind_reward_pending.values()): + # Preserve the final blind's card-move credit when the game ends or + # hits a context/decision cap before a clear event. + _append_checkpoint_event( + label="GAME_END", + state_name=str(state.get("state", "")), + turn=turns_taken, + token_start=blind_window_token, + token_end=len(response_token_ids), + values=blind_reward_pending, + ) + + reward_parts = _episode_reward_parts( + state, + invalid_actions=invalid_actions, + turns=turns_taken, + capped=capped, + terminal_reason=terminal_reason, + partial_rewards=partial_reward_parts, + ) + reward = reward_parts["total"] + policy_reward = _finalize_action_credit(context.segments, reward) + won = bool(state.get("won")) + final_ante = int(state.get("ante_num") or 0) + final_round = int(state.get("round_num") or 0) + context_tokens = context.peak_tokens + context_utilization = context_tokens / max(1, context_limit) + tokens_per_turn = len(response_token_ids) / max(1, turns_taken) + duration = time.monotonic() - episode_started + span( + "balatro.episode", + episode_started, + turns=turns_taken, + won=won, + capped=capped, + ) + + metadata = dict(getattr(sample, "metadata", None) or {}) + metadata["balatro"] = { + "seed_group": seed, + "episode_id": rollout_id, + "turns": turns_taken, + "output_contract_version": 1, + "thinking_budget": thinking_budget, + "forced_thinking_closures": sum(a["forced_thinking_close"] for segment in context.segments for a in segment["actions"]), + "thinking_token_lengths": [a.get("thinking_tokens", 0) + for segment in context.segments for a in segment["actions"]], + "final_answer_token_lengths": [a.get("final_answer_tokens", a["token_end"] - a["token_start"]) + for segment in context.segments for a in segment["actions"]], + "executed_actions": executed_actions, + "action_token_lengths": [a["sampled_tokens"] + for segment in context.segments for a in segment["actions"]], + "output_format_errors": output_format_errors, + "multiple_action_responses": multiple_action_responses, + "length_limit_responses": length_limit_responses, + "normal_stop_responses": normal_stop_responses, + "terminal_reason": terminal_reason, + "episode_reward": reward, + "partial_reward": reward_parts["partial_total"], + "partial_reward_steps": partial_reward_steps, + "reward_checkpoint_events": len(partial_reward_trace), + "ante_checkpoints": ante_checkpoints, + "partial_reward_trace": partial_reward_trace, + "highest_rewarded_ante": highest_rewarded_ante, + "final_ante": final_ante, + "final_round": final_round, + "won": won, + "capped": capped, + "invalid_turns": invalid_turns, + "invalid_actions": invalid_actions, + "fallback_actions": fallback_actions, + "repaired_actions": repaired_actions, + "api_errors": api_errors, + "context_tokens": context_tokens, + "context_limit": context_limit, + "context_protocol": 2, + "segment_target": segment_target, + "segment_count": len(context.segments), + "context_rebases": len(context.segments) - 1, + "episode_archive_tokens": len(initial_prompt_ids) + len(response_token_ids), + "context_utilization": context_utilization, + "tokens_per_turn": tokens_per_turn, + "duration_s": round(duration, 3), + "error": episode_error, + } + episode_metrics = { + "balatro/reward_total": reward, + "balatro/policy_reward_total": policy_reward, + "balatro/intervention_reward_excluded": reward - policy_reward, + "balatro/reward_progress": reward_parts["progress"], + "balatro/reward_blind_clear": reward_parts["blind_clear_bonus"], + "balatro/reward_ante_checkpoint": reward_parts["ante_checkpoint_bonus"], + "balatro/reward_legal_action": reward_parts["legal_action_bonus"], + "balatro/reward_win_bonus": reward_parts["win_bonus"], + "balatro/reward_invalid_penalty": reward_parts["invalid_penalty"], + "balatro/reward_cap_penalty": reward_parts["cap_penalty"], + "balatro/reward_partial": reward_parts["partial_total"], + "balatro/partial_reward_steps": partial_reward_steps, + "balatro/reward_checkpoint_events": len(partial_reward_trace), + "balatro/ante_checkpoints": ante_checkpoints, + "balatro/final_ante": final_ante, + "balatro/final_round": final_round, + "balatro/turns": turns_taken, + "balatro/invalid_actions": invalid_actions, + "balatro/fallback_actions": fallback_actions, + "balatro/repaired_actions": repaired_actions, + "balatro/api_errors": api_errors, + "balatro/context_tokens": context_tokens, + "balatro/segment_count": len(context.segments), + "balatro/context_rebases": len(context.segments) - 1, + "balatro/episode_archive_tokens": len(initial_prompt_ids) + len(response_token_ids), + "balatro/action_tokens": sum(loss_mask), + "balatro/control_tokens": metadata["balatro"]["forced_thinking_closures"], + "balatro/feedback_tokens": len(loss_mask) - sum(loss_mask) - metadata["balatro"]["forced_thinking_closures"], + "balatro/state_budget_exceeded": int(terminal_reason == "state_budget_exceeded"), + "balatro/context_utilization": context_utilization, + "balatro/tokens_per_turn": tokens_per_turn, + "balatro/won": int(won), + } + # Modal Dojo renders numeric top-level sample metadata as per-rollout + # time-series charts. Keep the nested metrics object for the sample detail + # view, and promote the same values so every Balatro metric is chartable. + metadata["metrics"] = episode_metrics + metadata.update(episode_metrics) + metadata["exit_status"] = terminal_reason + if screenshot: + metadata["image"] = screenshot + # Keep the source compatible with framework Sample implementations that do + # not allow arbitrary attributes. Modal Dojo promotes these reserved + # metadata keys into its first-class rollout fields during extraction. + metadata["reward_granularity"] = "checkpoint" + mapped_events = _segment_reward_events(partial_reward_trace, context.segments) + metadata["balatro"].pop("partial_reward_trace", None) + samples = [] + for index, segment in enumerate(context.segments): + part = deepcopy(sample) + part.prompt = segment["prompt"] + part.tokens = segment["prompt_ids"] + segment["response_ids"] + part.response_length = len(segment["response_ids"]) + part.response = segment["response"] + part.loss_mask = segment["loss_mask"] + part.rollout_log_probs = segment["log_probs"] + part.status = Sample.Status.ABORTED if aborted else Sample.Status.COMPLETED + part.reward = reward + part.rollout_id = rollout_id + part.metadata = { + **metadata, "segment_id": index, "segment_count": len(context.segments), + "context_protocol": 2, "segment_turn_start": segment["turn_start"], + "segment_turn_end": segment["actions"][-1]["turn"], + "segment_context_tokens": len(part.tokens), + "trajectory_messages": segment["messages"], "reward_events": mapped_events[index], + "credit_assignment": getattr(args, "balatro_credit_assignment", "episode"), + "policy_episode_reward": policy_reward, + "action_credit": [ + {"turn": action["turn"], "token_start": action["token_start"], + "token_end": action["token_end"], + "policy_token_spans": action["policy_token_spans"], + "task_return_to_go": reward - action["reward_before"], + "return_to_go": action["policy_return_to_go"], + "intervention_penalty": action["intervention_penalty"], + "finish_reason": action["finish_reason"], "output_error": action["output_error"]} + for action in segment["actions"] + ], + } + # Keep one full episode archive on the original training sample. Each + # dashboard segment also has its own complete snapshot + local moves. + if index == 0: + part.metadata["balatro_episode_archive"] = { + "prompt": initial_prompt, "response": trajectory_text, + "messages": trajectory_messages, "reward_events": partial_reward_trace, + } + part.trace = list(getattr(sample, "trace", None) or []) + episode_trace + else: + part.trace = [] + # Episode-level metrics are emitted once, not once per segment. + part.metadata.pop("metrics", None) + for key in episode_metrics: + part.metadata.pop(key, None) + try: + part.reward_granularity = "checkpoint" + part.reward_events = mapped_events[index] + except (AttributeError, TypeError, ValueError): + pass + samples.append(part) + + _log_balatro_event( + "BALATRO_EPISODE", + { + "seed": seed, + "episode_id": rollout_id, + "reward": reward, + "ante": final_ante, + "round": final_round, + "turns": turns_taken, + "won": won, + "capped": capped, + "invalid_actions": invalid_actions, + "terminal_reason": terminal_reason, + "duration_s": round(duration, 3), + }, + ) + return samples + + +def _sample_reward(sample: Any) -> float: + reward = getattr(sample, "reward", None) + return float(reward) if reward is not None else 0.0 + + +async def passthrough_reward(args: Any, samples: Any, **kwargs: Any) -> Any: + del args, kwargs + if isinstance(samples, (list, tuple)): + return [_sample_reward(sample) for sample in samples] + return _sample_reward(samples) + + +async def generate_balatro_comparison(args: Any, sample: Any, sampling_params: dict[str, Any], evaluation: bool = False): + """Train on exact segments, but report one complete game per held-out eval.""" + result = await generate_balatro_episode(args, sample, sampling_params, evaluation=evaluation) + if not evaluation: + return result + parts = result if isinstance(result, list) else [result] + if not parts or any( + getattr(part, "reward", None) is None + or not math.isfinite(float(part.reward)) + or getattr(getattr(part, "status", None), "value", None) == "aborted" + for part in parts + ): + seed = (getattr(sample, "metadata", None) or {}).get("balatro_seed", sample.prompt) + raise RuntimeError(f"Balatro evaluation returned an incomplete/unscored episode for {seed}") + if not isinstance(result, list): + return result + part = result[0] + archive = part.metadata["balatro_episode_archive"] + part.prompt, part.response = archive["prompt"], archive["response"] + tokenizer = _tokenizer(args.hf_checkpoint) + prompt_ids = tokenizer(part.prompt, add_special_tokens=False)["input_ids"] + response_ids = tokenizer(part.response, add_special_tokens=False)["input_ids"] + # Evaluation transcripts are never submitted to the optimizer. Retokenizing + # the archive is safe here and avoids weighting eval games by segment count. + part.tokens = prompt_ids + response_ids + part.response_length = len(response_ids) + part.loss_mask = [0] * len(response_ids) + part.rollout_log_probs = None + part.metadata["trajectory_messages"] = archive["messages"] + part.metadata["reward_events"] = archive["reward_events"] + part.metadata["evaluation_full_episode"] = True + return part + + +def normalize_episode_rewards( + args: Any, samples: list[Any] +) -> tuple[list[float], list[float]]: + """Normalize episode metrics and optionally attach actual token advantages.""" + from collections import defaultdict + + raw = [_sample_reward(sample) for sample in samples] + seed_groups: dict[str, list[int]] = defaultdict(list) + for index, sample in enumerate(samples): + metadata = getattr(sample, "metadata", None) or {} + seed = metadata.get("balatro", {}).get( + "seed_group", str(getattr(sample, "group_index", 0)) + ) + seed_groups[str(seed)].append(index) + + advantages = [0.0] * len(samples) + for indices in seed_groups.values(): + games: dict[int, list[int]] = defaultdict(list) + for index in indices: + games[int(getattr(samples[index], "rollout_id", index))].append(index) + game_ids = list(games) + scores = [sum(raw[i] for i in games[g]) / len(games[g]) for g in game_ids] + mean = sum(scores) / len(scores) + std = math.sqrt(sum((score - mean) ** 2 for score in scores) / len(scores)) + game_advantage = { + game_id: (score - mean) / (std + 1e-6) + for game_id, score in zip(game_ids, scores) + } + for game_id, game_indices in games.items(): + for index in game_indices: + advantages[index] = game_advantage[game_id] + if getattr(args, "balatro_credit_assignment", "episode") == "reward_to_go": + _assign_action_advantages(samples, seed_groups, invalid_weight=_invalid_action_weight(args)) + return raw, advantages + + +def _finalize_action_credit(segments: list[dict[str, Any]], reward: float) -> float: + """Remove intervention rewards from policy returns, preserving task scores. + + The next action's reward_before includes exactly the preceding transition. + The final delta includes terminal bonuses once. For a rejected action, + credit only its existing invalid-action penalty, never a repair's progress + or win bonus. Earlier valid actions retain subsequent model-earned rewards. + """ + actions = [action for segment in segments for action in segment["actions"]] + remaining = 0.0 + for index in range(len(actions) - 1, -1, -1): + action = actions[index] + after = actions[index + 1]["reward_before"] if index + 1 < len(actions) else reward + delta = float(after - action["reward_before"]) + penalty = -2.0 * action["invalid_events"] + # Retain any larger negative terminal penalty, but never positive + # progress produced by the harness executing a different action. + policy_delta = min(delta, penalty) if penalty < 0 else delta + remaining += policy_delta + action["intervention_penalty"] = min(policy_delta, penalty) if penalty < 0 else 0.0 + action["policy_return_to_go"] = remaining + return remaining + + +def _assign_action_advantages( + samples: list[Any], seed_groups: dict[str, list[int]], *, invalid_weight: float = 5.0, +) -> None: + """Same-seed, leave-one-game-out baselines at each decision number. + + Ended peers have zero remaining return. Scale once per seed using complete + policy returns (floor 1 reward unit), never per context segment. Task scores + remain unchanged; policy returns exclude progress from harness interventions. + Rejected actions have a negative floor applied AFTER reward scaling. Neither + a successful repaired game nor a large reward spread can weaken it. Preserve + larger negative task credit rather than capping the cost of a bad action. + """ + for indices in seed_groups.values(): + games: dict[int, dict[int, float]] = {} + totals: dict[int, float] = {} + for index in indices: + sample = samples[index] + game = int(sample.rollout_id) + actions = (sample.metadata or {}).get("action_credit") + if not actions: + raise ValueError("reward_to_go requires recorded action_credit for every sample") + policy_reward = float(sample.metadata["policy_episode_reward"]) + if game in totals and not math.isclose(totals[game], policy_reward): + raise ValueError("Segments of one game have inconsistent total rewards") + totals[game] = policy_reward + if not math.isfinite(totals[game]): + raise ValueError("Nonfinite game reward") + turns = games.setdefault(game, {}) + for action in actions: + turn, value = int(action["turn"]), float(action["return_to_go"]) + penalty = float(action["intervention_penalty"]) + if not math.isfinite(penalty) or penalty > 0: + raise ValueError("Invalid intervention penalty") + if turn in turns or turn < 1 or not math.isfinite(value): + raise ValueError("Invalid or duplicate action return-to-go") + turns[turn] = value + if len(games) < 2: + raise ValueError("reward_to_go needs at least two complete games per seed") + for turns in games.values(): + if sorted(turns) != list(range(1, len(turns) + 1)): + raise ValueError("reward_to_go requires complete, contiguous game decisions") + mean = sum(totals.values()) / len(totals) + scale = max(1.0, math.sqrt(sum((x - mean) ** 2 for x in totals.values()) / len(totals))) + for index in indices: + sample = samples[index] + game = int(sample.rollout_id) + spans = [] + for action in sample.metadata["action_credit"]: + turn = int(action["turn"]) + baseline = sum(turns.get(turn, 0.0) for peer, turns in games.items() if peer != game) / (len(games) - 1) + penalty = float(action["intervention_penalty"]) + advantage = (float(action["return_to_go"]) - baseline) / scale + if penalty < 0: + advantage = min(advantage, penalty / scale, -invalid_weight) + for start, end in action.get("policy_token_spans", [[action["token_start"], action["token_end"]]]): + if not action["token_start"] <= start < end <= action["token_end"]: + raise ValueError("Policy token span is outside its action") + spans.append({"start": start, "end": end, "advantage": advantage}) + sample.train_metadata = { + **(getattr(sample, "train_metadata", None) or {}), + "balatro_action_advantages": spans, + } + + +# --------------------------------------------------------------------------- +# Modal Dojo dataset and active run configuration +# --------------------------------------------------------------------------- + + +@recipe_dataclass(config=ConfigDict(extra="forbid", arbitrary_types_allowed=True)) +class Qwen3_8_27B_Recipe(BaseQwen3_8_27B_Recipe): + """Explicit Balatro topology on the current upstream Qwen preset. + + Retain the recipe name because Modal Dojo derives the shared checkpoint + volume name from it. Main's preset now leaves these pipeline fields to the + consumer and uses a different GPU/ref-load default. + """ + + gpu_type: str = "H100" + pipeline_model_parallel_size: int = 2 + conversion_tensor_model_parallel_size: int = 4 + conversion_pipeline_model_parallel_size: int = 2 + decoder_last_pipeline_num_layers: int | None = 30 + ref_load: str = "/checkpoints/Qwen3.8-27B_torch_dist_tp4pp2" + # Main's CP=1 preset uses unfused attention, which cannot run with our CP=4. + # Preserve the FlashAttention backend validated by the prior Balatro runs. + attention_backend: str = "flash" + + +class BalatroSeedsDataset(DatasetConfig): + num_seeds = 256 + + def input_key(self) -> str: + return "messages" + + def label_key(self) -> str: + return "label" + + def apply_chat_template(self) -> bool: + return True + + def _rows(self, count: int) -> list[dict[str, Any]]: + rows = [] + for index in range(count): + seed = f"BALATRO-RL-{index:08d}" + rows.append( + { + "messages": [{"role": "user", "content": seed}], + "label": "", + "metadata": {"balatro_seed": seed}, + } + ) + return rows + + @staticmethod + def _write(rows: list[dict[str, Any]], path: str) -> None: + os.makedirs(os.path.dirname(path), exist_ok=True) + with open(path, "w") as output: + for row in rows: + output.write(json.dumps(row) + "\n") + + def rows(self): + return self._rows(int(self.num_seeds)) + + +def build_train_config( + num_rollout: int | None = None, + load: str | None = None, + no_load_optim: bool = False, + credit_assignment: str = "reward_to_go", + invalid_action_weight: float = 5.0, +) -> TrainConfig: + if num_rollout is not None and num_rollout < 1: + raise ValueError(f"num_rollout must be >= 1, got {num_rollout}") + if credit_assignment not in {"episode", "reward_to_go"}: + raise ValueError(f"Unknown credit assignment: {credit_assignment}") + return TrainConfig( + model=Qwen3_8_27B(), + dataset=BalatroSeedsDataset(), + recipe=Qwen3_8_27B_Recipe( + num_rollout=200 if num_rollout is None else num_rollout, + rollout_batch_size=8, + n_samples_per_prompt=4, + global_batch_size=32, + # Rollout engines share the requested 8×8 actor cluster. A colocated recipe + # must not set ``rollout_num_gpus``: that field describes a + # separate rollout pool and only applies to disaggregated runs. + colocate=True, + actor_num_nodes=8, + actor_num_gpus_per_node=8, + rollout_num_gpus_per_engine=2, + tensor_model_parallel_size=4, + sequence_parallel=True, + pipeline_model_parallel_size=2, + context_parallel_size=4, + # Slime's per-game denominator spans all samples sharing rollout_id. + calculate_per_token_loss=False, + rollout_max_response_len=96, + rollout_temperature=1.0, + rollout_top_p=1.0, + max_tokens_per_gpu=12288, + sglang_speculative_algorithm=None, + sglang_speculative_num_steps=None, + sglang_speculative_eagle_topk=None, + sglang_speculative_num_draft_tokens=None, + save_interval=50, + load=load or "", + no_load_optim=no_load_optim, + trace_sample_limit=8, + # Long-horizon tool use is especially prone to collapsing into a + # single brittle action line. Keep a light anchor to ref_load and + # a small exploration incentive rather than relying on optimizer + # weight decay as the only regularizer. + use_kl_loss=True, + kl_loss_type="low_var_kl", + kl_loss_coef=0.001, + kl_coef=0.0, + entropy_coef=0.001, + weight_decay=0.01, + custom_generate_function=generate_balatro_episode, + custom_rm_function=passthrough_reward, + custom_reward_post_process_function=normalize_episode_rewards, + custom_rollout_log_function="balatro_diagnostics.log_rollout", + custom_eval_rollout_log_function="balatro_diagnostics.log_eval", + apply_chat_template_kwargs='{"enable_thinking": false}', + environment={ + "PYTHONPATH": "/root/Megatron-LM/", + "CUDA_DEVICE_MAX_CONNECTIONS": "1", + "NCCL_NVLS_ENABLE": "1", + # broad-layover crashed in NCCL's RAS peer-update thread during + # save. Disable that diagnostic subsystem for this recipe. + "NCCL_RAS_ENABLE": "0", + "MODAL_DOJO_TRAJECTORY_SAMPLE_LIMIT": "64", + "MODAL_DOJO_IMAGE_SAMPLE_LIMIT": "0", + }, + extra_config={ + "balatro_max_decisions": 250, + "balatro_context_len": 49152, + "balatro_segment_tokens": 8192, + "balatro_capture_screenshot": False, + "max_position_embeddings": 49152, + "sglang_context_length": 49152, + "balatro_credit_assignment": credit_assignment, + "balatro_invalid_action_weight": invalid_action_weight, + "normalize_advantages": False, + "rollout_top_k": -1, + **({"custom_advantage_function_path": "balatro_credit.compute_advantages"} + if credit_assignment == "reward_to_go" else {}), + }, + patch_files=[ + str(REPO / "training/patch_pipeline_group.py"), + str(REPO / "training/patch_checkpoint_rpc.py"), + str(REPO / "training/checkpoint_rpc.py"), + str(REPO / "training/balatro_diagnostics.py"), + ] + ( + [str(REPO / "training/balatro_credit.py")] + if credit_assignment == "reward_to_go" else [] + ), + image_run_commands=[ + "python /tmp/patch_pipeline_group.py", + "python /tmp/patch_checkpoint_rpc.py", + "python /tmp/balatro_diagnostics.py", + ] + ( + ["python /tmp/balatro_credit.py"] + if credit_assignment == "reward_to_go" else [] + ), + train_function_kwargs={"ephemeral_disk": 1_048_576}, + ), + ) + + +def _local_asset_release() -> tuple[ + str, list[tuple[Path, str]], list[tuple[Path, str]] +]: + """Return the content hash plus file/directory upload manifest.""" + import hashlib + + save = _find_balatro_save() + files = [ + (_find_balatro_love(), "Balatro.love"), + (REPO / "balatrobot.json", "mod/balatrobot.json"), + (REPO / "balatrobot.lua", "mod/balatrobot.lua"), + (save / "settings.jkr", "save/settings.jkr"), + (save / "1/profile.jkr", "save/1/profile.jkr"), + (save / "1/meta.jkr", "save/1/meta.jkr"), + ] + directories = [ + (REPO / "src/lua", "mod/src/lua"), + (REPO / "src/balatrobot", "python/balatrobot"), + ] + + digest = hashlib.sha256() + + def update(path: Path, logical_path: str) -> None: + digest.update(logical_path.encode()) + with path.open("rb") as source: + while chunk := source.read(1024 * 1024): + digest.update(chunk) + + for path, remote in files: + update(path, remote) + for directory, remote in directories: + for path in sorted(p for p in directory.rglob("*") if p.is_file()): + update(path, f"{remote}/{path.relative_to(directory)}") + return digest.hexdigest()[:16], files, directories + + +def _prepare_assets_from_cli() -> dict[str, Any]: + import uuid + + release, files, directories = _local_asset_release() + try: + reused = bool(asset_volume.listdir(f"/releases/{release}")) + except Exception: + reused = False + + upload_id = "" + with modal.enable_output(): + if not reused: + upload_id = f"{release}-{uuid.uuid4().hex[:10]}" + root = f"/uploads/{upload_id}" + with asset_volume.batch_upload() as batch: + for path, remote in files: + batch.put_file(str(path), f"{root}/{remote}") + for path, remote in directories: + batch.put_directory(str(path), f"{root}/{remote}") + with assets_app.run(): + return prepare_assets.remote(release, upload_id) + + +def _smoke_environment(seed: str = "BALATRO-RL-SMOKE") -> dict[str, Any]: + pool = BalatroEnvironmentPool() + env = pool.acquire(seed) + try: + state = env.start(seed) + initial = { + "state": state.get("state"), + "ante": state.get("ante_num"), + "round": state.get("round_num"), + "hand_size": len(state.get("hand") or []), + } + skip_result = env.step(ToolCall("skip", {})) + skip_state = skip_result.observation.metadata.get("state", {}) + skip_big_result = env.step(ToolCall("skip", {})) + skip_big_state = skip_big_result.observation.metadata.get("state", {}) + result = env.step(ToolCall("select", {})) + selected_state = result.observation.metadata.get("state", {}) + commit_result = env.step(_fallback_action(selected_state)) + next_state = commit_result.observation.metadata.get("state", {}) + return { + "initial": initial, + "skip_action": { + "error": skip_result.observation.is_error, + "state": skip_state.get("state"), + "selected_blinds": { + name: blind.get("status") + for name, blind in (skip_state.get("blinds") or {}).items() + if isinstance(blind, dict) + }, + }, + "skip_big_action": { + "error": skip_big_result.observation.is_error, + "state": skip_big_state.get("state"), + "selected_blinds": { + name: blind.get("status") + for name, blind in (skip_big_state.get("blinds") or {}).items() + if isinstance(blind, dict) + }, + }, + "one_action": { + "error": commit_result.observation.is_error, + "state": next_state.get("state"), + "ante": next_state.get("ante_num"), + "round": next_state.get("round_num"), + }, + } + finally: + pool.release(env) + + +def _checkpoint_iteration(checkpoint: Checkpoint) -> int: + match = re.fullmatch(r"iter_(\d+)", checkpoint.name) + if match is None: + raise RuntimeError( + f"Cannot determine iteration from checkpoint {checkpoint.name!r}" + ) + return int(match.group(1)) + + +def _remaining_rollouts(checkpoint_iteration: int, target: int) -> int: + # Slime resumes at the next rollout ID and uses an exclusive upper bound. + start = checkpoint_iteration + 1 + if target <= start: + raise ValueError( + f"Checkpoint {checkpoint_iteration} resumes at rollout {start}, so " + f"--num-rollout {target} would perform no training. " + f"Use at least --num-rollout {start + 1} for one new rollout." + ) + return target - start + + +def _launch_training(config: TrainConfig, record_path: Path | None = None): + """Submit once; retain a receipt even if submission is interrupted. + + Reuse the same record path when a supervisor reruns this command. An + existing receipt blocks submission, including an uncertain/partial one: + automatically retrying those can create duplicate GPU jobs. + """ + # Reject incompatible historical recipes before pinning checkpoints, + # creating a submission receipt, or allocating remote resources. + _validate_balatro_loss_config(config.recipe) + _validate_balatro_context_config(config.recipe) + _validate_balatro_sampling_config(config.recipe) + record = None + if record_path is not None: + try: + record = record_path.open("x", encoding="utf-8") + except FileExistsError: + raise RuntimeError( + f"Launch record already exists: {record_path}. No run submitted. " + "Inspect its run ID/logs before using a new record for a new run." + ) from None + try: + if record is not None: + json.dump({"state": "submitting", "pid": os.getpid()}, record) + record.flush() + os.fsync(record.fileno()) + checkpoint = getattr(config, "checkpoint", None) + if checkpoint is not None: + # Current Modal Dojo ignores the legacy ``config.checkpoint`` + # attribute. Explicit --load preserves optimizer/RNG and rollout + # numbering; resume_from_checkpoint instead starts a weights-only + # seed run by default. Never silently launch from the base model. + config.recipe.load = str(PurePosixPath(checkpoint.path).parent) + _pin_resume_checkpoint(checkpoint) + # Modal Dojo's public launcher starts a detached Modal app. Keep this + # process alive until it returns the persisted function-call handle. + run = config.launch() + if record is not None: + record.seek(0) + json.dump({ + "state": "submitted", + "training_run_id": run.training_run_id, + "modal_app_id": run.modal_app_id, + "function_call_id": run.function_call_id, + }, record) + record.truncate() + record.flush() + os.fsync(record.fileno()) + return run + finally: + if record is not None: + record.close() + + +def _checkpoint_manifest(checkpoint: Checkpoint) -> dict[str, int]: + volume = modal.Volume.from_name( + checkpoint.checkpoints_volume_name, + create_if_missing=False, + ) + manifest: dict[str, int] = {} + for entry in volume.iterdir(checkpoint.path_relative_to_volume, recursive=False): + path = str(getattr(entry, "path", "")) + size = getattr(entry, "size", None) + if path and isinstance(size, int): + manifest[path.rsplit("/", 1)[-1]] = size + return manifest + + +def _checkpoint_manifest_health( + candidate: dict[str, int], + reference: dict[str, int] | None, +) -> tuple[bool, str]: + required = {".metadata", "metadata.json"} + missing = sorted(required - candidate.keys()) + if missing: + return False, f"missing {', '.join(missing)}" + + candidate_shards = { + name: size for name, size in candidate.items() if name.endswith(".distcp") + } + if not candidate_shards: + return False, "contains no .distcp shards" + if any(size <= 0 for size in candidate_shards.values()): + return False, "contains an empty .distcp shard" + + if reference is None: + return True, "has checkpoint metadata and non-empty shards" + + reference_shards = { + name: size for name, size in reference.items() if name.endswith(".distcp") + } + if candidate_shards.keys() != reference_shards.keys(): + return False, "distributed shard set differs from the previous checkpoint" + + truncated = [ + name + for name, size in candidate_shards.items() + if size < 0.9 * reference_shards[name] + ] + if truncated: + return False, ( + f"{len(truncated)} distributed shards are more than 10% smaller " + "than the previous checkpoint" + ) + return True, "shard manifest matches the previous checkpoint" + + +def _pin_resume_checkpoint(checkpoint: Checkpoint) -> None: + """Point Megatron's run-level tracker at the validated checkpoint. + + Modal Dojo mounts the parent run directory and passes that directory to + ``--load``. Megatron then follows ``latest_checkpointed_iteration.txt``; + selecting a Checkpoint object alone does not override that tracker. + """ + iteration = _checkpoint_iteration(checkpoint) + run_dir = PurePosixPath(checkpoint.path_relative_to_volume).parent + marker_path = str(run_dir / "latest_checkpointed_iteration.txt") + volume = modal.Volume.from_name( + checkpoint.checkpoints_volume_name, + create_if_missing=False, + ) + marker = f"{iteration}\n".encode() + with volume.batch_upload(force=True) as batch: + batch.put_file(BytesIO(marker), marker_path) + written = b"".join(volume.read_file(marker_path)) + if written.strip() != str(iteration).encode(): + raise RuntimeError( + f"Failed to pin Megatron checkpoint tracker {marker_path!r} " + f"to iteration {iteration}" + ) + print(f"Pinned Megatron checkpoint tracker to iteration {iteration}.") + + +def _resume_result(config: TrainConfig, training_run_id: str) -> TrainingRun: + """Load a run and supply legacy checkpoint metadata when it is absent. + + Older Modal Dojo launchers wrote the checkpoint directory to the legacy + train-result store but not to ``TrainingRun.metadata``. The current API + discovers checkpoints exclusively from that metadata, so reconstruct the + deterministic Slime location in memory for those older runs. Do not save + this compatibility value back to the record. + """ + run = TrainingRun.from_id(training_run_id) + if run.checkpoints(): + return run + recipe_name = type(config.recipe).__name__.lstrip("_").lower() + set_checkpoint_location( + run, + checkpoint_dir=f"/checkpoints/{training_run_id}", + checkpoints_volume_name=f"slime-{recipe_name}-checkpoints", + checkpoints_mount_path="/checkpoints", + ) + return run + + +def _select_resume_checkpoint( + result: TrainingRun, + requested_iteration: int | None, +) -> Checkpoint: + # Use the persisted run's public helper in the ordinary case. A legacy run + # may have had its location reconstructed in memory by ``_resume_result``; + # calling ``TrainingRun.checkpoints()`` would reload and discard that local + # compatibility metadata, so enumerate the same location directly there. + location = getattr(result, "metadata", {}).get("checkpoint_location") + if isinstance(location, dict): + checkpoints = _list_checkpoints( + str(location["checkpoint_dir"]), + str(location["checkpoints_volume_name"]), + str(location.get("checkpoints_mount_path") or "/checkpoints"), + training_run_id=result.training_run_id, + app_name=getattr(result, "app_name", ""), + ) + else: + checkpoints = result.checkpoints() + checkpoints = sorted(checkpoints, key=_checkpoint_iteration) + if not checkpoints: + raise RuntimeError(f"No checkpoints found for run {result.training_run_id!r}") + + # Only inspect candidates we actually consider and their predecessor. + # Eagerly listing every historical checkpoint can hit VolumeListFiles limits + # before submission, particularly when validating and then launching. + manifests: dict[str, dict[str, int]] = {} + + def manifest(checkpoint: Checkpoint) -> dict[str, int]: + if checkpoint.name not in manifests: + manifests[checkpoint.name] = _checkpoint_manifest(checkpoint) + return manifests[checkpoint.name] + + candidates = checkpoints + if requested_iteration is not None: + candidates = [ + checkpoint + for checkpoint in checkpoints + if _checkpoint_iteration(checkpoint) == requested_iteration + ] + if not candidates: + available = ", ".join(str(_checkpoint_iteration(c)) for c in checkpoints) + raise RuntimeError( + f"Checkpoint iteration {requested_iteration} was not found; " + f"available iterations: {available}" + ) + + for checkpoint in reversed(candidates): + checkpoint_index = checkpoints.index(checkpoint) + reference = ( + manifest(checkpoints[checkpoint_index - 1]) + if checkpoint_index > 0 + else None + ) + healthy, reason = _checkpoint_manifest_health( + manifest(checkpoint), reference + ) + if healthy: + print(f"Checkpoint {checkpoint.name} accepted: {reason}.") + return checkpoint + print(f"Checkpoint {checkpoint.name} rejected: {reason}.") + + raise RuntimeError( + f"No structurally complete checkpoint found for run {result.training_run_id!r}" + ) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--credit-assignment", choices=("episode", "reward_to_go"), default="reward_to_go", + help="Action credit with strong invalid penalties; episode is the legacy comparison control.", + ) + parser.add_argument("--invalid-action-weight", type=float, default=5.0, + help="Minimum negative advantage magnitude for rejected actions in reward_to_go mode") + parser.add_argument("--prepare-assets", action="store_true") + parser.add_argument("--smoke-env", action="store_true") + parser.add_argument("--publish-image", action="store_true", + help="Build and publish the named balatrobot sandbox image (CPU only).") + parser.add_argument( + "--load", + default=None, + help=( + "Checkpoint directory to resume from, e.g. /checkpoints/. " + "Omit to train from the base model." + ), + ) + parser.add_argument( + "--no-load-optim", + action="store_true", + help=( + "Skip optimizer state when resuming (--load). Required to resume " + "with a different --num-rollout than the checkpoint was written with." + ), + ) + parser.add_argument( + "--num-rollout", + type=int, + default=None, + help=( + "Override the active TrainConfig's total target iteration count. When resuming, " + "this exclusive upper bound must exceed checkpoint iteration + 1." + ), + ) + parser.add_argument( + "--resume-run", + default=None, + metavar="TRAINING_RUN_ID", + help="Resume from the latest structurally complete Megatron checkpoint.", + ) + parser.add_argument( + "--resume-iteration", + type=int, + default=None, + help="Require this exact checkpoint iteration when resuming.", + ) + parser.add_argument( + "--save-interval", + type=int, + default=None, + help="Override checkpoint frequency; the final iteration is always saved.", + ) + parser.add_argument( + "--no-fault-tolerance", + action="store_true", + help="Fail once instead of repeatedly resuming from a newly damaged checkpoint.", + ) + parser.add_argument( + "--launch-record", + type=Path, + default=None, + help="One-shot submission receipt; an existing file prevents duplicate launches.", + ) + parser.add_argument( + "--launch", + action="store_true", + help="Actually launch the H100 Modal Dojo run.", + ) + args = parser.parse_args() + if args.launch and sys.version_info[:2] != (3, 12): + parser.error("Launch with Python 3.12 to match the Modal Dojo image.") + if args.resume_iteration is not None and not args.resume_run: + parser.error("--resume-iteration requires --resume-run") + if args.resume_run and args.load: + parser.error("Use either --resume-run or --load, not both") + + if args.prepare_assets: + print(json.dumps(_prepare_assets_from_cli(), indent=2)) + if args.publish_image: + with modal.enable_output(): + image = _balatro_runtime_image() + image.build(modal.App.lookup(SANDBOX_APP_NAME, create_if_missing=True)) + image.publish(SANDBOX_IMAGE_NAME) + print(f"Published {SANDBOX_IMAGE_NAME}:latest -> {image.object_id}", flush=True) + if not args.smoke_env and not args.launch: + return + if args.smoke_env: + with modal.enable_output(): + smoke_result = _smoke_environment() + print(json.dumps(smoke_result, indent=2)) + + config = build_train_config( + num_rollout=args.num_rollout, + load=args.load, + no_load_optim=args.no_load_optim, + credit_assignment=args.credit_assignment, + invalid_action_weight=args.invalid_action_weight, + ) + _validate_balatro_loss_config(config.recipe) + context_limit, segment_target = _validate_balatro_context_config(config.recipe) + _validate_balatro_sampling_config(config.recipe) + print(f"Context: hard={context_limit}, segment={segment_target}; credit={args.credit_assignment}.") + if args.save_interval is not None: + if args.save_interval < 1: + raise ValueError("--save-interval must be at least 1") + config.recipe.save_interval = args.save_interval + if args.no_fault_tolerance: + config.recipe.use_fault_tolerance = False + if args.resume_run: + result = _resume_result(config, args.resume_run) + checkpoint = _select_resume_checkpoint(result, args.resume_iteration) + checkpoint_iteration = _checkpoint_iteration(checkpoint) + target_iteration = int(config.recipe.num_rollout) + remaining = _remaining_rollouts(checkpoint_iteration, target_iteration) + config.checkpoint = checkpoint + # The checkpoint's scheduler was built for its original rollout + # horizon. Continuing to a larger target must keep the new schedule + # while still restoring model, optimizer, and RNG state. + resume_extra_config = dict(config.recipe.extra_config or {}) + resume_extra_config["override_opt_param_scheduler"] = True + config.recipe.extra_config = resume_extra_config + print( + f"Resuming {args.resume_run} from {checkpoint.name}; " + f"target iterations={target_iteration}; new rollouts={remaining}." + ) + if not args.launch: + with tempfile.TemporaryDirectory() as directory: + path = str(Path(directory) / "balatro-seeds.jsonl") + config.dataset.write(path) + config.dataset.validate_written(path) + rollouts = int(config.recipe.num_rollout) + print( + "TrainConfig validated; no GPUs launched. " + f"rollouts={rollouts}, max_decisions=" + f"{config.recipe.extra_config['balatro_max_decisions']}." + ) + if not args.prepare_assets and not args.resume_run: + print("First run: python batro_modal_rl.py --prepare-assets") + launch_cmd = "python batro_modal_rl.py" + if args.credit_assignment != "episode": + launch_cmd += f" --credit-assignment {args.credit_assignment}" + if args.num_rollout is not None: + launch_cmd += f" --num-rollout {args.num_rollout}" + if args.resume_run: + launch_cmd += f" --resume-run {args.resume_run}" + if args.resume_iteration is not None: + launch_cmd += f" --resume-iteration {args.resume_iteration}" + if args.save_interval is not None: + launch_cmd += f" --save-interval {args.save_interval}" + if args.no_fault_tolerance: + launch_cmd += " --no-fault-tolerance" + print(f"Launch explicitly: {launch_cmd} --launch") + return + + run = _launch_training(config, args.launch_record) + viewer_path = BALATRO_TRAJECTORY_VIEWER_PATH + + if DashboardComponent is not None and hasattr(run, "add_dashboard_component"): + run.add_dashboard_component( + name="balatro", + component_type=DashboardComponent.TRAJECTORY_VIEWER, + from_path=viewer_path, + ) + print(f"Modal Dojo run id: {run.training_run_id}") + + +if __name__ == "__main__": + main() diff --git a/dashboard/README.md b/dashboard/README.md new file mode 100644 index 00000000..25bd4c52 --- /dev/null +++ b/dashboard/README.md @@ -0,0 +1,111 @@ +# Balatro dashboard component + +`TrajectoryViewer.svelte` is the Balatro-specific trajectory viewer shipped +with this repository. `batro_modal_rl.py` resolves it relative to its own +file and attaches it to each launched run through Training Gym's +`training-gym-dashboard-overlay` volume. + +Training Gym also ships a framework-default viewer at +`training-gym/dashboards/frontend/src/components/TrajectoryViewer.svelte`. +That file is the fallback used by the dashboard when no Balatro component is +attached (and can be replaced globally with `training-gym setup`). + +The Balatro viewer follows the same Svelte props contract as the default: +`sample`, `samples`, `trajectory`, `rewardEvents`, `rollout`, and `run`. + +The board shows chips scored, the current blind's target, and chips still +needed. Targets are per blind, not per ante; selection starts at zero and +shop/pack screens never reuse the previous blind's score. The initial +selection is available as the `S` frame. Blind selection lists each target, +skip tag, and its recorded effect; skip outcomes distinguish the tag taken +from the next blind's offer. Missing recorded effects are explicitly marked. + +Run compiler and rendered-output regression tests with +`node --test dashboard/TrajectoryViewer.test.mjs`. This uses Svelte installed +in `../training-gym/dashboards/frontend`; set `TRAINING_GYM_FRONTEND` to +override that location. + +## Card rendering + +Playing cards and all 150 standard jokers use the sprite atlases and layering +approach from EFHIII's Balatro Calculator, pinned to commit +`55322d59197ba08c16874a54b59ac0d443c8ab32`. Enhancement backgrounds, rank/suit +art, legendary-joker overlays, edition textures, seals and debuff crosses are +composed separately. Polychrome uses the calculator's color blend; Negative +uses an inverted-art treatment. Unknown/modded cards retain a text fallback. +Modifier badges remain visible, including stickers and remaining perishable +rounds. Hover is optional, not required to identify a modifier. + +The four PNG atlases are embedded once as CSS variables in the single-file +component, so viewing a trajectory makes no external image requests. See +[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) for attribution and license. +## UI-only calculator and joker information + +The viewer now embeds the pinned upstream `balatro-sim.js` scoring logic, +wrapped to isolate mutable hand-value tables between calculations. The only +change inside the upstream implementation is a Svelte performance-warning +annotation; no scoring rules are rewritten. Its result is an **estimate**, +not an engine RPC or a claim of full Balatro accuracy. + +Click 1–5 cards in play order, then **Calculate**. The compact HUD displays +hand type, Chips × Mult, and the upstream worst/best range for random effects. +Recorded play frames calculate automatically. Their actual `play_commit.score` +is shown separately and remains visible even when calculator inputs are missing. +Discard frames highlight the removed pre-action cards in red, independently +of calculator selection. The discard-pile button opens its card inventory. +Historical plays use pre-action cards, jokers, and hand values. Joker order +stays as recorded; this UI does not optimize, commit a play, call a model, +or change harness rewards. Calculations never mutate trajectory state. + +Counters not present in the compact trace must be entered explicitly in the +same units as the upstream calculator (for example Hologram's *Cards Added*, +not its Mult). Known money/discard counters are populated from the trace. +Unsupported cards/jokers block calculation; missing persistent card bonuses +and unmodeled boss/deck effects remain limitations. Flint, Eye, Plasma Deck, +and Observatory settings are available for inspection/editing. Missing hand +levels/history default to level 1 / zero, as in the upstream calculator. + +The Lua gamestate RPC exports `card.scoring_state.target` for The Idol, +Ancient Joker, Mail-In Rebate and Castle, plus Castle's current chip tally. +The compact harness preserves this in each joker's snapshot and sparse deltas. +These are already-selected, player-visible targets—not RNG seeds or future +draws. The calculator converts rank/suit to its internal Card/Suit ID, so no +manual ID is needed. For older traces, exact English target descriptions can +be recovered from recorded effect text. Unknown targets remain unknown; +current live targets are never substituted into historical frames. +Structured capture requires newly built sandboxes; existing running training +workers are not hot-patched by a dashboard upload. + +Joker sprites have hover/focus/tap tooltips with the recorded effect, edition +bonuses, stickers, and sell value. Escape dismisses a focused tooltip. + +## Compact game layout + +The layout follows Balatro's game board: score/status HUD on the left, jokers +and consumables across the top, phase-specific content in the center, hand +and deck inspection below. It uses the embedded m6x11plus pixel font, +blue/red Chips × Mult blocks, chunky controls, and a muted felt table. +Container queries adapt to the dashboard panel's width, not just the window. + +The replay navigation stays expanded above the board, with ante/round/phase +tabs and individual turn buttons for the selected group. Previous/next buttons +can cross group boundaries. There is no continuous frame scrubber. +**Run info**, **Calculator**, and **Trace & rewards** open an inspector below +the board. Deck/discard contents, vouchers, joker counters, hand levels, +calculator settings, raw pre/post-action states and reward events remain +available without keeping every panel expanded. Rewards are explicitly +episode-wide rather than falsely aligned to the selected frame. + +Run Info sections start collapsed, including the empty owned-voucher message. +Expanding inventory sections shows card racks with modifiers and click-to-open +details; raw fields remain under **Recorded data**. Poker hand levels and +history use a compact table, with missing values shown as dashes. The board's +deck/discard buttons still expand the corresponding section directly. + +Shop cards, prices, vouchers, booster offers and reroll cost are visible in +shop frames; pack frames show their choices. Item details retain all recorded +fields. Non-playing, non-joker cards currently use labeled symbolic tiles. +The top inventory and HUD always show the resulting state; historical play +inputs below are explicitly labeled pre-action. Discards left is distinct +from the discard pile size. Unknown counters display a dash, not a guessed +value. No training settings, harness behavior or rewards change with this UI. diff --git a/dashboard/THIRD_PARTY_NOTICES.md b/dashboard/THIRD_PARTY_NOTICES.md new file mode 100644 index 00000000..0d2caf43 --- /dev/null +++ b/dashboard/THIRD_PARTY_NOTICES.md @@ -0,0 +1,42 @@ +# Balatro Calculator rendering assets + +The embedded `m6x11plus.ttf` font is by Daniel Linssen, distributed via the +same pinned calculator repository. Its author's page permits use with +attribution: https://managore.itch.io/m6x11 . The viewer footer includes this +credit. The calculator's MIT notice below does not replace the font credit. + +Card atlas coordinates, sprite sheets, scoring logic, value-input labels, +and the layered rendering approach +are adapted from [EFHIII/balatro-calculator](https://github.com/EFHIII/balatro-calculator) +at commit `55322d59197ba08c16874a54b59ac0d443c8ab32`. + +Included atlases: `8BitDeck.png`, `Jokers.png`, `Enhancers.png`, `Editions.png`. +They are embedded into `TrajectoryViewer.svelte` because Training Gym uploads +a single-file component. `balatro-sim.js` is embedded with its scoring logic +unchanged, plus a Svelte compiler annotation. No external image or code +requests are made when calculating. Original Balatro artwork remains the property of its +respective rights holders; the upstream project's license is reproduced below. + +``` +MIT License + +Copyright (c) 2024 Saffron Haas + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` diff --git a/dashboard/TrajectoryViewer.svelte b/dashboard/TrajectoryViewer.svelte new file mode 100644 index 00000000..06fc8be9 --- /dev/null +++ b/dashboard/TrajectoryViewer.svelte @@ -0,0 +1,3037 @@ + + +{#snippet finish(card)} + {#if modifierValue(card, "edition") && modifierValue(card, "edition") !== "base"} + + {/if} +{/snippet} + +{#snippet badges(card)} + {@const modifiers = modifierBadges(card)} + {#if modifiers.length} +
+ {#each modifiers as badge} + {badge.label} + {/each} +
+ {/if} +{/snippet} + +{#snippet spriteArt(card, joker = false)} + {@const visual = spriteVisual(card, joker)} + {#if visual} + + {/if} +{/snippet} + +{#snippet jokerRack(items)} + +
+ {#each items as joker, index} + {@const tooltipId = `${viewerId}-joker-${index}`} +
+ + {@render badges(joker)} + +
+ {:else}
No jokers in this frame.
{/each} +
+{/snippet} + +{#snippet inventoryCard(item, index, kind = "item")} + {@const isJoker = String(typeof item === "string" ? item : item.id || item.key || "").startsWith("j_")} + {@const title = cardInfo(item).suit ? cardTitle(item) : itemName(item)} +
+ + {#if spriteVisual(item, isJoker)}{@render spriteArt(item, isJoker)}{:else}{kind === "pack" ? "✦" : kind === "voucher" ? "◆" : "☾"}{/if} + {title} + #{index}{item.cost != null ? " · $" + item.cost : ""}{kind === "owned" ? " · owned" : ""} + {@render badges(item)} + +
{title}

{item.effect || "Effect not recorded in this trajectory."}

{#if item.sell != null}Sell value: {"$" + item.sell}{/if}
Recorded data
{JSON.stringify(item, null, 2)}
+
+{/snippet} + +
`--atlas-${name}:url('${data}')`).join(";")}> + +
+
BALATROTRAJECTORY REPLAY · READ ONLY
+
{states.length} recorded moves · {groups.length} phase groups{#if replay.finalAnte != null} · Final ante {replay.finalAnte}{/if}
+ {latest.phase || run?.status || "RUN"} +
+ {#if activeGroup} + {#if replay.segments < replay.expectedSegments}

Partial replay: {replay.segments} of {replay.expectedSegments} game segments available.

{/if} + {#if replay.missingResults}

{replay.missingResults} move result{replay.missingResults === 1 ? " was" : "s were"} not saved. Showing recorded states only.

{/if} + + + +
+ + + {activeGroup.frames.length} turns + {#each activeGroup.frames as frame, index} + + {/each} + + +
+
+ +
+
+
{@render jokerRack(jokers)}
+
+ +
{#each consumables as item, index}{@render inventoryCard(item, index, "consumable")}{:else}No consumables{/each}
+
+
+
+ {#if activeAction} +
+ {activeAction.method.replaceAll("_", " ")} + {#if actionCards.length}cards [{actionCards.join(", ")}]{/if} + {#if actionTarget != null}{actionTargetName} {actionTarget}{#if actionConsumable} · {itemName(actionConsumable)}{/if}{/if} + {#if activeAction.method === "skip"}skip blind · take its tag{/if} + {#if activeAction.method === "next_round"}leave shop · choose next blind{/if} +
+ {#if skipResult} +
+
TAG TAKEN · IN-GAME SKIP REWARD{skipResult.tag || "Tag not recorded"}
+
+
SKIPPED{blindLabel(skipResult.skipped)}
+
EFFECT / WHEN IT APPLIES{skipResult.tag_effect || "Effect not recorded in this trajectory."}
+ {#if skipResult.next}
NEXT BLIND{blindLabel(skipResult.next)}{#if skipResult.next.score != null} · {chips(skipResult.next.score)} chips required{/if}
{/if} +
+
+ {/if} + {/if} + + {#if blindChoices.length} + +
+ {#each blindChoices as blind} + {@const status = String(blind.status || "UNKNOWN").toUpperCase()} + {@const boss = String(blind.type || blind.id).toUpperCase() === "BOSS"} +
+
{blindLabel(blind)}{status === "SELECT" ? "AVAILABLE NOW" : status}
+

Clear target: {chips(blind.score)} chips

+ {#if blind.effect}

{blind.effect}

{/if} + {#if boss} +
Cannot skip the Boss Blind

No skip reward.

+ {:else} +
{status === "SKIPPED" ? "TAG TAKEN" : status === "DEFEATED" ? "SKIP OFFER · NOT TAKEN" : "SKIP TO RECEIVE"}{blind.tag || "Tag not recorded"}

{blind.tag_effect || "Effect not recorded in this trajectory."}

+ {/if} +
+ {/each} +
+

Skipping takes the tag instead of playing that blind. The description above states when its benefit applies; this is an in-game reward, not the training reward.

+ {/if} + + + {#if phase === "SHOP"} +

SHOP

Reroll {latest.reroll_cost != null ? "$" + latest.reroll_cost : "cost not recorded"}
+
+
{#each shopItems as item, index}{@render inventoryCard(item, index, "shop")}{:else}

No cards recorded.

{/each}
+
{#each vouchers as item, index}{@render inventoryCard(item, index, "voucher")}{/each}{#each packOffers as item, index}{@render inventoryCard(item, index, "pack")}{/each}
+
+ {:else if phase === "PACK" || phase === "SMODS_BOOSTER_OPENED"} +

BOOSTER PACK

{packChoices.length} recorded choices
+
{#each packChoices as item, index}{@render inventoryCard(item, index, "choice")}{:else}

No pack choices recorded.

{/each}
+ {:else if phase === "GAME_OVER"} +

GAME OVER

Recorded terminal state · Ante {latest.ante ?? "—"}, round {latest.round ?? "—"}

+ {:else if !blindChoices.length && !activeAction} +
BALATRO{progress ? "Score the blind to advance" : "Recorded game state"}
+ {/if} +
+
+ + {#if selectedIndices.length} +
{activeAction.method} selected cards [{selectedIndices.join(", ")}]
+ {/if} + {#if handForAction.length} +
+ {#each handForAction as card, index (`${activeFrame?.index}-${index}-${cardTitle(card)}`)} + {@const info = cardInfo(card)} + {@const isSelected = calcSelected.includes(index)} + {@const wasDiscarded = activeAction?.method === "discard" && selectedIndices.includes(index)} + {@const hasArt = spriteVisual(card) != null} +
0 ? !isSelected : activeAction?.method === "discard" && !wasDiscarded}> + + {modifierValue(card, "enhancement") === "stone" ? "Stone" : `${info.rank}${info.suit}`} + {@render badges(card)} +
+ {/each} +
+ {:else}
No hand data at this turn.
{/if} + + + + {#if actionUsesInputHand}

Historical action input. HUD and top inventory show the resulting state; calculation uses the pre-action cards and jokers.

{/if} +
+
+ UI only · never plays a hand + + +
+
+
+
+ +
{latestReward ? "Latest episode reward · " + (latestReward.label || "checkpoint") + " · " + Number(latestReward.reward || 0).toFixed(3) : "Frame " + (framePosition + 1) + " / " + flatFrames.length}Art & scoring: EFHIII · Font: Daniel Linssen
+ {:else}
No structured Balatro state found.
{/if} +
+ + diff --git a/dashboard/TrajectoryViewer.test.mjs b/dashboard/TrajectoryViewer.test.mjs new file mode 100644 index 00000000..2a95fc20 --- /dev/null +++ b/dashboard/TrajectoryViewer.test.mjs @@ -0,0 +1,429 @@ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { readFileSync } from "node:fs"; +import { createRequire } from "node:module"; +import { resolve } from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import test from "node:test"; + +// Use the dashboard's installed Svelte, without a second package/install in +// the game repository. Override for checkouts with a different directory name. +const frontend = process.env.MODAL_DOJO_FRONTEND || fileURLToPath(new URL("../../modal-dojo/dashboards/frontend", import.meta.url)); +const require = createRequire(resolve(frontend, "package.json")); +const { compile } = require("svelte/compiler"); +const { render } = await import(pathToFileURL(require.resolve("svelte/server"))); +const source = readFileSync(new URL("./TrajectoryViewer.svelte", import.meta.url), "utf8"); +const compiled = compile(source, { generate: "server", filename: "TrajectoryViewer.svelte" }); +const code = compiled.js.code.replace(/from '(svelte[^']*)'/g, (_, id) => `from '${pathToFileURL(require.resolve(id))}'`); +const { default: Viewer } = await import(`data:text/javascript;base64,${Buffer.from(code).toString("base64")}`); +const helpers = new Function(`${source.slice(source.indexOf(" // Sprite coordinates"), source.indexOf(" let replay ="))}; return { gameReplay, chipProgress, chips, statesFrom, actionsFrom, spriteVisual, modifierBadges, JOKER_SPRITES, CARD_ATLASES, calculateHand, calculatorJokers, calculatorCard, upstreamCalculator };`)(); + +test("game replay joins ordered segments and recovers boundary results from full snapshots", () => { + const first = { metadata: { balatro: { episode_id: 42, final_ante: 5 }, segment_id: 0, segment_count: 2, segment_turn_start: 1 }, + prompt: 'Gamestate: {"ante":1,"phase":"hand","shop":["stale"],"hand":["S_A"]}', + response: '{"method":"select","params":{}}{"round":1}{"method":"play","params":{"cards":[0]}}' }; + const second = { metadata: { balatro: { episode_id: 42, final_ante: 5 }, segment_id: 1, segment_count: 2, segment_turn_start: 3 }, + prompt: 'Gamestate: {"ante":5,"phase":"hand","hand":["H_K"]}', + response: '{"method":"play","params":{"cards":[0]}}{"phase":"GAME_OVER"}' }; + const unrelated = { ...second, metadata: { ...second.metadata, balatro: { episode_id: 99 } } }; + const replay = helpers.gameReplay(first, [second, unrelated, first, second]); + assert.deepEqual(replay.states.map(s => s.ante), [1, 5, 5]); + assert.deepEqual(replay.actions.map(a => a.method), ["select", "play", "play"]); + assert.deepEqual(replay.turns, [1, 2, 3]); + assert.deepEqual(replay.inputs[1].hand, ["S_A"]); + assert.deepEqual(replay.inputs[2].hand, ["H_K"]); + assert.equal(replay.states[1].shop, undefined, "full snapshot must discard stale fields"); + assert.equal(replay.missingResults, 0); + assert.equal(replay.segments, 2); + const html = render(Viewer, { props: { sample: first, samples: [second, first] } }).body; + for (const label of ["Final ante 5", "ante 5", "Last recorded frame", "3 recorded moves"]) assert.ok(html.includes(label), label); + assert.deepEqual(helpers.gameReplay(second, [first, second]).states, replay.states); +}); + +test("missing segments and unsaved final results are disclosed without inventing states", () => { + const sample = { metadata: { balatro: { episode_id: 42, final_ante: 5 }, segment_id: 0, segment_count: 3, segment_turn_start: 1 }, + prompt: 'Gamestate: {"ante":1}', + response: '{"method":"select","params":{}}{"phase":"hand"}{"method":"play","params":{"cards":[0]}}' }; + const later = { ...sample, metadata: { ...sample.metadata, segment_id: 2, segment_turn_start: 10 }, prompt: 'Gamestate: {"ante":5}' }; + const replay = helpers.gameReplay(sample, [later, sample]); + assert.deepEqual(replay.turns, [1, 10]); + assert.equal(replay.missingResults, 2); + const html = render(Viewer, { props: { sample, samples: [sample] } }).body; + assert.ok(html.includes("Partial replay: 1 of 3 game segments available.")); + assert.ok(html.includes("1 move result was not saved.")); +}); + +test("recorded ante-five game replays all 15 segments through move 136", () => { + // Reduced state fields from independent-slab rollout 0, episode + // 1789759001521903355. Its final action (137) has no saved result. + const parts = JSON.parse(readFileSync(new URL("./fixtures/ante-five-segments.json", import.meta.url), "utf8")); + const replay = helpers.gameReplay(parts[0], [...parts].reverse()); + assert.equal(replay.initial.ante, 1); + assert.equal(replay.states.length, 136); + assert.deepEqual(replay.turns, Array.from({ length: 136 }, (_, i) => i + 1)); + assert.equal(replay.states.at(-1).ante, 5); + assert.deepEqual([...new Set(replay.states.map(s => s.ante))], [1, 2, 3, 4, 5]); + assert.equal(replay.missingResults, 1); + assert.equal(replay.segments, 15); + for (let i = 1; i < parts.length; i++) { + const boundary = parts[i].metadata.segment_turn_start - 2; + const snapshot = JSON.parse(parts[i].prompt.slice('Gamestate: '.length)); + assert.deepEqual(replay.states[boundary], snapshot); + assert.deepEqual(replay.inputs[boundary + 1], snapshot); + } + const html = render(Viewer, { props: { sample: parts[0], samples: parts } }).body; + for (const ante of [1, 2, 3, 4, 5]) assert.ok(html.includes(`ante ${ante}`)); + assert.ok(html.includes("136 recorded moves")); + assert.ok(html.includes("Final ante 5")); + assert.ok(!html.includes("Partial replay")); +}); + +const blinds = { + small: { name: "Small Blind", type: "small", status: "SELECT", score: 300, tag: "Investment Tag", tag_effect: "After defeating the Boss Blind, gain $25." }, + big: { name: "Big Blind", type: "big", status: "UPCOMING", score: 450, tag: "Coupon Tag", tag_effect: "Initial cards and booster packs in the next shop are free." }, + boss: { name: "The Hook", type: "boss", status: "UPCOMING", score: 600, effect: "Discards 2 random cards per hand played." }, +}; +const initial = { phase: "BLIND_SELECT", ante: 1, round: 0, blinds }; +const stateMessage = (state) => ({ role: "tool", content: `${JSON.stringify(state)}` }); +const htmlFor = (state, action = null) => render(Viewer, { props: { + trajectory: [...(action ? [{ role: "assistant", content: JSON.stringify(action) }] : []), stateMessage(state)], +} }).body.replace(/data:image\/png;base64,[A-Za-z0-9+/=]+/g, "data:image/png;base64,[embedded]"); + +test("both Svelte targets compile without warnings", () => { + assert.deepEqual(compiled.warnings, []); + assert.deepEqual(compile(source, { generate: "client" }).warnings, []); +}); + +test("initial blind selection is visible before the first action", () => { + const html = render(Viewer, { props: { trajectory: [{ role: "user", content: `Gamestate: ${JSON.stringify(initial)}` }] } }).body; + for (const label of ["Initial state", "AVAILABLE NOW", "300 chips", "450 chips", "600 chips", "Investment Tag", "gain $25", "Coupon Tag", "Cannot skip the Boss Blind"]) + assert.ok(html.includes(label), label); + assert.ok(!html.includes("No structured Balatro state")); +}); + +test("hand displays explicit scored, required and remaining chips", () => { + const html = htmlFor({ phase: "hand", blind: { type: "big", score: [1200, 2400] } }); + for (const label of ["CHIPS SCORED", "CHIPS REQUIRED TO CLEAR", "CHIPS STILL NEEDED", "1,200", "2,400"]) + assert.ok(html.includes(label), label); +}); + +test("selection resets displayed score instead of inheriting the last hand", () => { + assert.deepEqual(helpers.chipProgress({ ...initial, blind: { score: [999, 300] } }), { + current: 0, target: 300, label: "Small Blind", pending: true, + }); +}); + +test("shop and pack never display stale active-blind chips", () => { + for (const phase of ["shop", "pack"]) { + assert.equal(helpers.chipProgress({ phase, blind: { score: [999, 300] } }), null); + const html = htmlFor({ phase, blind: { score: [999, 300] } }); + assert.ok(html.includes("No active blind score")); + assert.ok(!html.includes("CHIPS SCORED")); + } +}); + +test("zero and missing scores stay distinct; overshoot needs zero more chips", () => { + assert.equal(helpers.chips(0), "0"); + for (const value of [null, undefined, "", "not recorded", Infinity]) assert.equal(helpers.chips(value), "—"); + const html = htmlFor({ phase: "hand", blind: { score: [650, 600] } }); + assert.match(html, /CHIPS STILL NEEDED<\/small>\s*]*>0<\/strong>/); +}); + +test("missing tag effects are acknowledged, not invented", () => { + const html = htmlFor({ ...initial, blinds: { small: { ...blinds.small, tag_effect: null } } }); + assert.ok(html.includes("Effect not recorded in this trajectory.")); +}); + +test("skip outcome shows the earned tag separately from the next offer", () => { + const html = htmlFor({ ...initial, + blinds: { ...blinds, small: { ...blinds.small, status: "SKIPPED" }, big: { ...blinds.big, status: "SELECT" } }, + action_result: { method: "skip", skipped: { name: "Small Blind" }, tag: blinds.small.tag, tag_effect: blinds.small.tag_effect, next: blinds.big }, + }, { method: "skip", params: {} }); + for (const label of ["TAG TAKEN · IN-GAME SKIP REWARD", "Investment Tag", "gain $25", "Coupon Tag", "450 chips required"]) + assert.ok(html.includes(label), label); +}); + +test("a failed skip does not claim a tag was earned", () => { + const html = htmlFor(initial, { method: "skip", params: {} }); + assert.ok(!html.includes("TAG TAKEN · IN-GAME SKIP REWARD")); +}); + +test("initial snapshot seeds sparse deltas without shifting actions", () => { + const messages = [ + { role: "assistant", content: '{"method":"select","params":{}}' }, + stateMessage({ phase: "hand", blind: { type: "small", score: [0, 300] } }), + stateMessage({ blind: null, phase: "shop" }), + ]; + const states = helpers.statesFrom(messages, initial); + assert.equal(states.length, 2); + assert.equal(states[0].ante, 1); + assert.equal(states[0].blinds.small.tag, "Investment Tag"); + assert.equal(states[1].blind, undefined); + assert.deepEqual(helpers.actionsFrom(messages), [{ method: "select", params: {} }]); +}); + +test("all 52 playing cards map to the correct rank and suit cells", () => { + for (const [y, suit] of [..."HCDS"].entries()) { + for (const [x, rank] of ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"].entries()) { + const visual = helpers.spriteVisual(`${suit}_${rank}`); + assert.equal(visual.layers.length, 2); + assert.ok(visual.layers[1].style.includes(`background-position:${x / 12 * 100}% ${y / 3 * 100}%`)); + } + } +}); + +test("all 150 standard jokers have bounded coordinates and real artwork", () => { + assert.equal(Object.keys(helpers.JOKER_SPRITES).length, 150); + for (const [key, position] of Object.entries(helpers.JOKER_SPRITES)) { + assert.ok(position.x >= 0 && position.x < 10 && position.y >= 0 && position.y < 16, key); + const visual = helpers.spriteVisual({ id: key }, true); + assert.ok(visual.layers[0].style.includes("--atlas-Jokers"), key); + assert.ok(visual.mask.includes("mask-image"), key); + } + assert.deepEqual(helpers.JOKER_SPRITES.j_drivers_license, { x: 0, y: 7 }); + assert.equal(helpers.spriteVisual({ id: "j_hologram" }, true).layers[1].kind, "soul"); + assert.equal(helpers.spriteVisual({ id: "j_modded_unknown" }, true), null); +}); + +test("enhancements, editions and seals layer independently", () => { + for (const enhancement of ["BONUS", "MULT", "WILD", "LUCKY", "GLASS", "STEEL", "GOLD", "STONE"]) { + const visual = helpers.spriteVisual({ id: "S_A", mod: { enhancement, edition: "POLYCHROME", seal: "RED" } }); + assert.equal(visual.layers.some(l => l.kind === "face"), enhancement !== "STONE"); + assert.ok(visual.layers.some(l => l.kind === "edition" && l.style.includes("--atlas-Editions"))); + assert.ok(visual.layers.some(l => l.kind === "seal" && l.style.includes("--atlas-Enhancers"))); + } +}); + +test("editions and modifiers are visible on cards and jokers without hovering", () => { + for (const edition of ["FOIL", "HOLO", "POLYCHROME", "NEGATIVE"]) { + const html = htmlFor({ phase: "hand", hand: [{ id: "H_K", mod: { edition, enhancement: "STEEL", seal: "RED" } }], jokers: [{ id: "j_blueprint", effect: "Copies ability", mod: { edition, eternal: true, rental: true, perishable: 2 } }] }); + assert.ok(html.includes(`data-edition="${edition.toLowerCase()}"`)); + assert.ok(html.includes('class="sprite-art')); + assert.ok(html.includes('data-kind="edition"')); + for (const label of ["Steel", "Red Seal", "Eternal", "Rental", "Perishable · 2 rounds", "Blueprint"]) assert.ok(html.includes(label), label); + assert.ok(!html.includes("Object.keys")); + } +}); + +test("unknown cards keep text fallback and debuffed cards show the cross overlay", () => { + assert.equal(helpers.spriteVisual({ id: "unknown", mod: { edition: "FOIL" } }), null); + assert.ok(htmlFor({ phase: "hand", hand: [{ id: "unknown", mod: { edition: "FOIL" } }] }).includes("Foil")); + const visual = helpers.spriteVisual({ id: "S_A", debuff: true }); + assert.equal(visual.layers.at(-1).kind, "debuff"); +}); + +test("sprite sheets are embedded PNGs of the expected dimensions, within upload limit", () => { + const dimensions = { "8BitDeck": [923, 380], Jokers: [710, 1520], Enhancers: [497, 475], Editions: [355, 94] }; + for (const [name, data] of Object.entries(helpers.CARD_ATLASES)) { + const png = Buffer.from(data.split(",")[1], "base64"); + assert.equal(png.subarray(1, 4).toString(), "PNG"); + assert.deepEqual([png.readUInt32BE(16), png.readUInt32BE(20)], dimensions[name]); + } + assert.ok(Buffer.byteLength(source) < 2 * 1024 * 1024); + assert.ok(source.includes("prefers-reduced-motion: reduce")); +}); + +const scoreOf = result => result.low.score[0] * 10 ** result.low.score[1]; +test("embedded engine matches the pinned upstream code apart from the compiler annotation", () => { + const engine = source.split(" function upstreamCalculator() {\n")[1].split("\n return { Hand, handChips };")[0] + .replace("// svelte-ignore perf_avoid_nested_class (Each calculation needs isolated mutable hand-value tables.)\n", "") + "\n"; + assert.equal(createHash("sha256").update(engine).digest("hex"), "27ada8d35202a698ebd30b803e13dd1c765222f2e36ba54e806b5d69f69302a8"); +}); +test("upstream calculator scores a high card, pair and foil joker", () => { + assert.equal(scoreOf(helpers.calculateHand({ hand: ["S_A"] }, [0])), 16); + assert.equal(scoreOf(helpers.calculateHand({ hand: ["H_K", "D_K"] }, [0, 1])), 60); + const result = helpers.calculateHand({ hand: ["S_A"], jokers: [{ id: "j_joker", mod: { edition: "FOIL" } }] }, [0]); + assert.equal(scoreOf(result), 330); +}); + +test("upstream calculator applies Shortcut and Four Fingers", () => { + const shortcut = helpers.calculateHand({ hand: ["S_2", "H_4", "D_6", "C_8", "S_T"], jokers: [{ id: "j_shortcut" }] }, [0, 1, 2, 3, 4]); + assert.equal(shortcut.low.hand, "Straight"); + assert.equal(scoreOf(shortcut), 240); + const four = helpers.calculateHand({ hand: ["S_2", "H_3", "D_4", "C_5"], jokers: [{ id: "j_four_fingers" }] }, [0, 1, 2, 3]); + assert.equal(four.low.hand, "Straight"); + assert.equal(scoreOf(four), 176); +}); + +test("calculator scores held Steel and exposes random outcome ranges", () => { + assert.equal(scoreOf(helpers.calculateHand({ hand: ["S_A", { id: "H_K", mod: { enhancement: "STEEL" } }] }, [0])), 24); + const result = helpers.calculateHand({ hand: ["S_A"], jokers: [{ id: "j_misprint" }] }, [0]); + assert.equal(scoreOf(result), 16); + assert.equal(result.high.score[0] * 10 ** result.high.score[1], 384); + assert.equal(result.range, true); +}); + +test("calculator refuses unknown counters instead of silently assuming zero", () => { + const state = { hand: ["S_A"], jokers: [{ id: "j_hologram", effect: "Currently X2 Mult" }] }; + assert.match(helpers.calculateHand(state, [0]).error, /Hologram.*Cards Added/); + assert.equal(scoreOf(helpers.calculateHand(state, [0], { 0: 4 })), 32); + assert.match(helpers.calculateHand({ hand: ["S_A"], jokers: [{ id: "j_modded_unknown" }] }, [0]).error, /unsupported joker/); +}); + +test("calculator auto-loads known counters and recorded hand values without mutating state", () => { + const state = { hand: ["S_A"], money: 10, jokers: [{ id: "j_bull", sell: 3 }], hand_values: { "High Card": [2, 25, 3] } }; + const before = JSON.stringify(state); + assert.equal(helpers.calculatorJokers(state)[0].value, 10); + assert.equal(scoreOf(helpers.calculateHand(state, [0])), (25 + 11 + 20) * 3); + assert.equal(JSON.stringify(state), before); + assert.equal(scoreOf(helpers.calculateHand({ hand: ["S_A"] }, [0])), 16, "custom base values must not leak into later calculations"); +}); + +test("calculator preserves selected play order and validates unsupported input", () => { + const state = { hand: ["H_2", "D_K"], jokers: [{ id: "j_hanging_chad" }, { id: "j_splash" }] }; + const a = helpers.calculateHand(state, [0, 1]), b = helpers.calculateHand(state, [1, 0]); + assert.notEqual(scoreOf(a), scoreOf(b)); + for (const cards of [[], [0, 0], [9], [0, 1, 2, 3, 4, 5]]) assert.ok(helpers.calculateHand(state, cards).error); + assert.match(helpers.calculateHand({ hand: [{ id: "S_A", mod: { edition: "MODDED" } }] }, [0]).error, /unsupported modifier/); +}); + +test("joker info is an accessible hover/focus tooltip and calculation is UI-only", () => { + const html = htmlFor({ phase: "hand", hand: ["S_A"], jokers: [{ id: "j_joker", effect: "+4 Mult", sell: 2 }] }); + for (const label of ['role="tooltip"', 'aria-describedby=', "+4 Mult", "Sell value: $2", "EFHIII scoring engine", "UI estimate, not a live-game prediction", 'aria-pressed="false"']) assert.ok(html.includes(label), label); + assert.ok(source.includes(".joker-slot:hover .joker-tooltip, .joker-slot:focus-within .joker-tooltip")); + assert.ok(source.includes("#3a5055")); + assert.ok(!source.includes("--felt:")); +}); + +test("compact game layout keeps replay and inspectors without expanding them", () => { + const html = htmlFor({ phase: "hand", ante: 3, round: 7, money: 29, hand: ["S_A"], blind: { score: [42, 2000], hands: 3, discards: 2 } }); + for (const label of ['aria-label="Recorded game status"', 'aria-label="Ante, round, and blind"', 'aria-label="Turns in selected blind"', 'aria-label="Previous frame"', 'aria-label="Next frame"', "Hands left", "Discards left", "$29", "Run info", "Trace & rewards", "UI CALCULATOR · ESTIMATE"]) assert.ok(html.includes(label), label); + assert.match(html, /class="inspector-panel[^\"]*" hidden/); + assert.ok(!html.includes('type="range"')); + assert.ok(html.indexOf('class="blind-strip') < html.indexOf('class="game-layout')); + assert.ok(html.indexOf('class="turn-strip') < html.indexOf('class="game-layout')); + assert.ok(html.includes("ante 3") && html.includes("round 7")); + assert.ok(html.indexOf('class="game-hud') < html.indexOf('class="game-table')); + assert.ok(html.indexOf('class="top-racks') < html.indexOf('class="hand-zone')); + assert.ok(source.includes("@container (max-width: 480px)")); +}); + +test("shop renders prices, offers, vouchers and boosters without an active score", () => { + const html = htmlFor({ phase: "shop", money: 15, reroll_cost: 5, + shop: [{ id: "j_joker", effect: "+4 Mult", cost: 2, mod: { edition: "foil" } }], + vouchers: [{ id: "v_overstock_norm", effect: "+1 card slot in shop", cost: 10 }], + packs: [{ id: "p_arcana_normal_1", name: "Arcana Pack", effect: "Choose 1 of 3 Tarot cards", cost: 4 }], + owned_vouchers: [{ id: "v_hone", effect: "Edition odds doubled" }], blind: { score: [999, 300] }, + }); + for (const label of ["CARDS FOR SALE", "Reroll $5", "VOUCHERS & BOOSTER PACKS", "Arcana Pack", "Choose 1 of 3 Tarot cards", "$4", "$10", "Foil", "Edition odds doubled", "No active blind score"]) assert.ok(html.includes(label), label); + assert.ok(!html.includes("CHIPS SCORED")); +}); + +test("booster choices retain effects and modifiers", () => { + const html = htmlFor({ phase: "pack", pack: [{ id: "S_A", mod: { enhancement: "glass", edition: "polychrome", seal: "red" } }, { id: "c_death", effect: "Convert left card into right card" }] }); + for (const label of ["BOOSTER PACK", "2 recorded choices", "Glass", "Polychrome", "Red Seal", "Convert left card into right card"]) assert.ok(html.includes(label), label); +}); + +test("discards left never masquerade as discard pile size", () => { + const state = { phase: "hand", hand: ["S_A"], blind: { score: [0, 300], hands: 4, discards: 3 }, deck_left: 49 }; + assert.ok(htmlFor(state).includes("Discard pile: —")); + const html = htmlFor({ ...state, discard: ["H_2", "C_3"] }); + assert.match(html, /Discards left<\/small>]*>3<\/strong>/); + assert.ok(html.includes("Discard pile: 2")); + assert.ok(html.includes("DECK 49")); +}); + +test("complete trace and inventory stay inspectable, including fields without dedicated widgets", () => { + const html = htmlFor({ phase: "hand", hand: ["S_A"], deck: ["H_2"], custom_future_field: { unusual: "retained information" }, hand_stats: { Pair: [3, 6, 1] }, owned_vouchers: [{ id: "v_hone", effect: "Edition odds doubled" }] }); + for (const label of ["retained information", "Complete frame state", "Action & input state", "Reward events · entire episode, not frame-aligned", "Deck composition · 1 recorded cards", "Poker hand levels & history", "Edition odds doubled"]) assert.ok(html.includes(label), label); +}); + +test("empty compact inventory does not render slot metadata as a fake card", () => { + const html = htmlFor({ phase: "hand", hand: ["S_A"], jokers: { slots: "0/5" }, consumables: { slots: "0/2" } }); + assert.ok(html.includes("No jokers in this frame.")); + assert.ok(html.includes("No consumables")); + assert.ok(!html.includes("Info: Slots")); + assert.ok(!html.includes("0/0/5")); +}); + +test("inventory sections default collapsed and render cards instead of JSON walls", () => { + const html = htmlFor({ phase: "hand", jokers: [{ id: "j_joker", mod: { edition: "foil" } }], consumables: [{ id: "c_fool", effect: "Create last Tarot" }], deck: ["H_2"], discard: ["C_3"], hand_values: { Pair: [3, 40, 4] }, hand_stats: { Pair: [3, 6, 0], Flush: [1, 0, 0] } }); + for (const label of ["Owned vouchers · 0", "Jokers · 1", "Consumables · 1", "Deck composition · 1 recorded cards", "Discard pile · 1", "Poker hand levels & history · current state"]) + assert.ok(new RegExp(`]*>]*>${label}`).test(html), label); + assert.ok(!/]*\bopen\b[^>]*>]*>(Owned vouchers|Jokers|Consumables|Deck composition|Discard pile|Poker hand levels)/.test(html)); + for (const kind of ["joker", "consumable", "deck", "discard"]) assert.ok(html.includes(`data-kind="${kind}"`), kind); + for (const label of ["No owned vouchers recorded.", "Create last Tarot", "2♥", "3♣", "Foil", "Played total", "This round", "Recorded data"]) assert.ok(html.includes(label), label); + const rows = html.match(/([\s\S]*?)<\/tbody>/)[1].replace(//g, "").replace(/<[^>]*>/g, " ").replace(/\s+/g, " "); + assert.ok(rows.includes("Pair 3 40 4 6 0"), rows); + assert.ok(rows.includes("Flush 1 — — 0 0"), rows); +}); + +// Exercise the same reactive replay state at a selected frame via SSR. This +// changes only initial navigation state in the test build, not viewer logic. +const replayTestComponents = new Map(); +async function replayHtml(before, action, after, groupIndex = 0, frameIndex = 1) { + const key = `${groupIndex}:${frameIndex}`; + if (!replayTestComponents.has(key)) { + const fixtureSource = source.replace('let activeGroupIndex = $state(0);', `let activeGroupIndex = $state(${groupIndex});`).replace('let activeFrameIndex = $state(0);', `let activeFrameIndex = $state(${frameIndex});`); + const built = compile(fixtureSource, { generate: "server" }).js.code.replace(/from '(svelte[^']*)'/g, (_, id) => `from '${pathToFileURL(require.resolve(id))}'`); + const { default: component } = await import(`data:text/javascript;base64,${Buffer.from(built).toString("base64")}`); + replayTestComponents.set(key, component); + } + return render(replayTestComponents.get(key), { props: { trajectory: [{ role: "user", content: `Gamestate: ${JSON.stringify(before)}` }, { role: "assistant", content: JSON.stringify(action) }, stateMessage(after)] } }).body.replace(/data:image\/png;base64,[A-Za-z0-9+/=]+/g, "[embedded]"); +} + +test("discard replay highlights removed input cards, not their replacements", async () => { + const html = await replayHtml({ phase: "hand", ante: 1, round: 1, blind: { type: "small", score: [0,300] }, hand: ["S_A", "H_2", "C_3"] }, { method: "discard", params: { cards: [1,2] } }, { hand: ["S_A", "D_K", "D_Q"], discard: ["H_2", "C_3"] }); + assert.equal((html.match(/class="playing-card[^\"]*recorded-discard/g) || []).length, 2); + assert.ok(html.includes("Discarded this turn. Select 2♥")); + assert.ok(html.includes("DISCARD INPUT")); + assert.ok(html.includes('aria-label="Inspect discard pile"')); + assert.ok(source.includes('open={inventoryFocus === "discard"}')); + assert.ok(!html.includes("ACTUAL PLAY · RECORDED")); +}); + +test("play frames auto-calculate and show recorded score independently", async () => { + const html = await replayHtml({ phase: "hand", ante: 1, round: 1, blind: { type: "small", score: [0,300] }, hand: ["S_A", "H_K"], jokers: { slots: "0/5" } }, { method: "play", params: { cards: [0] } }, { blind: { score: [16,300] }, hand: ["C_2", "H_K"], play_commit: { score: 16, hand: "High Card", cards: [0] } }); + for (const label of ["ACTUAL PLAY · RECORDED", "16 chips", "UI CALCULATOR · ESTIMATE", "High Card"]) assert.ok(html.includes(label), label); + assert.match(html, /class="hud-calculation[\s\S]*?]*>16<\/strong>/); + assert.ok(!html.includes("Select cards to calculate")); +}); + +test("winning play auto-calculates pre-shop inputs and preserves actual random outcome", async () => { + const html = await replayHtml({ phase: "hand", ante: 1, round: 1, blind: { type: "small", score: [280,300] }, hand: ["S_A"], jokers: [{ id: "j_misprint" }] }, { method: "play", params: { cards: [0] } }, { phase: "shop", blind: null, hand: null, play_commit: { score: 128, hand: "High Card", cards: [0] } }, 1, 0); + assert.ok(html.includes("128 chips")); + assert.ok(html.includes("16 – 384")); + assert.ok(html.includes("PLAY INPUT · PRE-SHOP STATE")); + assert.ok(!html.includes("CHIPS SCORED")); +}); + +test("missing calculator inputs never hide the actual recorded play score", async () => { + const html = await replayHtml({ phase: "hand", ante: 1, round: 1, blind: { type: "small", score: [0,300] }, hand: ["S_A"], jokers: [{ id: "j_hologram" }] }, { method: "play", params: { cards: [0] } }, { blind: { score: [32,300] }, play_commit: { score: 32, hand: "High Card", cards: [0] } }); + assert.ok(html.includes("32 chips")); + assert.ok(html.includes("Estimate needs inputs")); +}); + +test("Idol targets auto-populate all 52 upstream Card IDs from structured snapshots", () => { + for (const [rankIndex, rank] of ["2","3","4","5","6","7","8","9","T","J","Q","K","A"].entries()) { + for (const [suitIndex, suit] of [..."HCDS"].entries()) { + const joker = { id: "j_idol", scoring_state: { target: { rank, suit } } }; + assert.equal(helpers.calculatorJokers({ jokers: [joker] })[0].value, rankIndex * 4 + suitIndex); + const plain = scoreOf(helpers.calculateHand({ hand: [suit + "_" + rank] }, [0])); + assert.equal(scoreOf(helpers.calculateHand({ hand: [suit + "_" + rank], jokers: [joker] }, [0])), plain * 2); + } + } +}); + +test("old trace Idol descriptions supply actual round targets without guessing", () => { + const joker = { id: "j_idol", effect: "Each played 3 of Hearts gives X2 Mult when scored Card changes every round" }; + assert.equal(helpers.calculatorJokers({ jokers: [joker] })[0].value, 4); + assert.equal(scoreOf(helpers.calculateHand({ hand: ["H_3"], jokers: [joker] }, [0])), 16); + assert.equal(scoreOf(helpers.calculateHand({ hand: ["C_3"], jokers: [joker] }, [0])), 8); + const unknown = { id: "j_idol", effect: "Each played [rank] of [suit] gives X2 Mult" }; + assert.match(helpers.calculateHand({ hand: ["H_3"], jokers: [unknown] }, [0]).error, /Enter missing calculator inputs/); +}); + +test("structured targets override text and refresh across sparse round deltas", () => { + const first = { jokers: { items: [{ id: "j_idol", effect: "Each played 3 of Hearts gives X2 Mult", scoring_state: { target: { rank: "K", suit: "S" } } }] } }; + const states = helpers.statesFrom([stateMessage(first), stateMessage({ jokers: { items: [{ id: "j_idol", scoring_state: { target: { rank: "2", suit: "H" } } }] } })]); + assert.equal(helpers.calculatorJokers(states[0])[0].value, 47); + assert.equal(helpers.calculatorJokers(states[1])[0].value, 0); +}); + +test("Ancient suit and Castle accumulated chips populate their calculator inputs", () => { + const ancient = { id: "j_ancient", scoring_state: { target: { suit: "C" } } }; + assert.equal(helpers.calculatorJokers({ jokers: [ancient] })[0].value, 1); + assert.equal(scoreOf(helpers.calculateHand({ hand: ["C_A"], jokers: [ancient] }, [0])), 24); + const castle = { id: "j_castle", scoring_state: { target: { suit: "D" }, chips: 27, chip_gain: 3 } }; + assert.equal(scoreOf(helpers.calculateHand({ hand: ["C_A"], jokers: [castle] }, [0])), 43); +}); diff --git a/hi.py b/hi.py new file mode 100644 index 00000000..76e3173a --- /dev/null +++ b/hi.py @@ -0,0 +1,12 @@ +import itertools + +def build(adapters): + units = iter(()) + for lora_name, tensors in adapters: + prefixed = (f"{lora_name}:{t}" for t in tensors) + units = itertools.chain(units, prefixed) + return list(units) # consumption happens here, after the loop + +print(build([("A", ["w1", "w2"]), ("B", ["w1", "w2"])])) +# prints ['B:w1', 'B:w2', 'B:w1', 'B:w2'] +# even though we want ['A:w1', 'A:w2', 'B:w1', 'B:w2'] diff --git a/reports/broad-layover-2026-09-15.md b/reports/broad-layover-2026-09-15.md new file mode 100644 index 00000000..6627d26d --- /dev/null +++ b/reports/broad-layover-2026-09-15.md @@ -0,0 +1,149 @@ +# Broad-layover trace audit — 2026-09-15 + +Run: `broad-layover-3a0ce678e7da`, resumed from +`perfect-adhesive-da14ace5a831`, 8 colocated nodes, 16 seed groups × 4 samples +(64 episodes/update). No training settings or rewards were changed for this audit. + +## Status and learning trend + +At the final status check the job was running, reporting weight sync at +416/600, with rollout 415 reward 46.47. The numbering resumes at 400; this +does not mean this continuation has already performed 416 new updates. + +For the first 14 completed batches (400–413): + +| Window | Episodes | Mean reward | Mean final ante | +|---|---:|---:|---:| +| 400–406 | 448 | 51.70 | 3.27 | +| 407–413 | 448 | 50.52 | 3.18 | +| Parent run 390–399 | 320 | 51.32 | 3.30 | + +The continuation is approximately flat, not demonstrating a clear increase +in depth. Across 400–413 there were no wins in 896 episodes; the highest +recorded final ante was 7. These are training batches with different seeds, +not a controlled fixed-seed evaluation. A single low batch is not sufficient +evidence of parameter/entropy collapse. + +## What happened in the traces + +I reconstructed sparse states (including nested fallback/error results), +read prompts and model responses, and audited all 192 episodes in batches +400, 412 and 413. The dashboard CLI rejected its stored authentication; +the reads used the authenticated Training Gym SDK and persisted rollout data. + +| Rollout | Mean reward | Game over | Context cap | Ended at ante 1 | +|---|---:|---:|---:|---:| +| 400 | 52.23 | 55 | 9 | 12 | +| 412 | 55.67 | 59 | 5 | 11 | +| 413 | 36.45 | 63 | 1 | 29 | + +The latest audited dip is primarily more early game overs, not more context +cutoffs or terminal RPC errors. No audited episode hit the 250-decision cap. +This does not prove every game over is free of harness influence. + +### 1. Narrow hand selection and missed joker synergies + +Of 3,914 executed plays, 2,799 (71.5%) were Pair or Two Pair. Only four were +Straights and two were Flushes. The model made no `sell` or blind `skip` +actions across these batches, and rerolled a shop only three times. +This describes a narrow strategy, not a proof that each such action was bad. + +There were 131 plays of Pair/High Card while a raw five-rank straight existed +in hand, across 91 episodes. This is an opportunity flag, not an exact-regret +metric: debuffs, enhancements and joker synergies can favor a lower hand type. + +One unambiguous example is rollout 413, sample index 0, action 16: + +- Ante 1 Big Blind; 256/450 chips, one hand and one discard remaining. +- Hand indices: `0 H_A, 1 D_K, 2 S_Q, 3 H_J, 4 D_J, 5 H_T, 6 S_8, 7 C_2`. +- Jokers: Superposition and Devious Joker (+100 Chips for a Straight). +- Played `[4,3,5,6,2]`: a Pair, 60 chips; lost at 316/450. +- `[0,1,2,3,5]` is A-K-Q-J-10. From the recorded, unmodified inputs, + `(30 + 51 + 100) × 4 = 724` chips, comfortably enough to clear. + This arithmetic is not a new game-engine replay, but there is no random + scoring modifier in the recorded state. Superposition also benefits from + this exact straight. Sample 2 repeats essentially the same mistake. + +In sample 3, action 24, A-K-Q-J-10 was again available; the policy instead +played Q-J-10-9-5 as High Card, scoring 15 and losing to The Hook. +The relevant card/joker information was present in the model's context. + +### 2. The context budget cuts off deeper attempts + +15/192 episodes (7.8%) hit the 49,152-token context limit, typically after +102–135 decisions, well before the 250-decision limit. Examples include +rollout 400 sample 7 ending alive in the ante-6 shop and rollout 412 sample +38 ending at ante 5 with 17,242/22,000 chips and a hand remaining. + +These traces do not establish that the capped games would have won, but +the model is denied the opportunity to continue. Raising only the decision +budget cannot fix this. Context caps retain earned progress and receive no +cap penalty in the current reward implementation; decision caps get -10. + +### 3. Confirmed pack legality gap: Judgement at full joker capacity + +Rollout 413, sample 39: + +- Action 61: skipped a Buffoon pack while at 5/5 jokers. +- Action 62: bought an Arcana pack containing The Moon, Judgement and Wheel + of Fortune. +- Action 63: `pack {card:1}` used Judgement at 5/5. +- The next raw state contains **six actual joker entries**, slots `6/5`, + adding ordinary Bloodstone with no Negative edition. This is not just a + dashboard counting artifact. + +The pack endpoint checks capacity for a directly selected Joker, but invokes +`G.FUNCS.use_card` for Tarot/Spectral selections without the +`can_use_consumeable()` check used by the normal `use` endpoint. The compact +pack legal-action filter also lacks a Judgement-at-capacity exclusion. +This permits a game-rule violation and should be corrected separately. +No pack behavior was changed during this diagnosis. + +### 4. Repeated ante checkpoint bonuses after ante-lowering vouchers + +Rollout 400, sample 8 went 1→2→3, bought a voucher at action 48 that changed +ante 3→2, then cleared 2→3 again and later 3→4. It therefore recorded four +ante checkpoint bonuses despite finishing at ante 4. + +The ante regression itself is legitimate voucher behavior. The reward +function awards +8 on every positive ante delta and does not distinguish +first attainment from re-clearing an already reached ante. This explains +some reward/final-ante disagreement and creates an objective loophole if the +goal is uniquely advancing toward ante 8. It is separate from UI labeling. +No reward changes were made. + +### 5. Missing machine-readable joker targets + +Rollout 412 already records Idol targets in effect text, e.g. “3 of Hearts” +and “10 of Diamonds.” The prior calculator adapter did not read them. +The viewer now parses exact recorded targets for historical traces; future +RPC snapshots include structured visible round targets for The Idol, Ancient +Joker, Mail-In Rebate and Castle. It does not save/expose private RNG state, +roll RNG during observation, or substitute today's target into older frames. + +The traces also contain unresolved boss-description placeholders such as +`#1# in #2# cards get drawn face down`; structured numerical effect data would +avoid that information loss. This was noted, not repaired in this patch. + +## Interpretation and next steps + +The evidence supports a mixture of a narrow, sometimes plainly incorrect +policy; context-limited long episodes; and remaining legality/reward gaps. +It does not support explaining everything as flaky execution or diagnosing +an optimizer collapse from the reward dip alone. + +The next experiments should first close the Judgement pack legality gap, +decide whether ante bonuses mean first attainment or repeated clears, and +address history growth. Then compare fixed seeds and hand-level decisions. +Larger batches alone do not teach the missing scoring/synergy reasoning. + +## Changes completed for the accompanying UI requests + +- Restored expanded ante/round/phase navigation and per-group turn buttons. +- Restored discarded-card highlighting and a direct discard-pile inspector. +- Automatically calculate recorded play frames; show actual recorded chips + separately from estimates, including when estimate inputs are missing. +- Recover saved Idol targets and add structured target capture for newly + built sandboxes. Existing training workers were not hot-patched. +- 38 viewer tests and 18 CPU serializer/launcher tests passed; the dashboard + bundle compiled. Browser visual verification was unavailable. diff --git a/reports/broad-layover-final-evidence-2026-09-15.json b/reports/broad-layover-final-evidence-2026-09-15.json new file mode 100644 index 00000000..a414e72b --- /dev/null +++ b/reports/broad-layover-final-evidence-2026-09-15.json @@ -0,0 +1,6690 @@ +{ + "run": { + "training_run_id": "broad-layover-3a0ce678e7da", + "modal_app_id": "ap-3vCPrQh34oPL9P5dsmtUSa", + "modal_app_url": "https://modal.com/id/ap-3vCPrQh34oPL9P5dsmtUSa", + "framework": "slime", + "config": { + "model": { + "model_name": "Qwen/Qwen3.8-27B" + }, + "recipe": { + "sequence_parallel": true, + "rollout_max_response_len": 96, + "rollout_temperature": 1.0, + "save_interval": 50, + "gpu_type": "H100", + "colocate": true, + "tensor_model_parallel_size": 4, + "rollout_num_gpus_per_engine": 2, + "num_rollout": 600, + "start_rollout_id": null, + "rollout_batch_size": 16, + "actor_num_nodes": 8, + "actor_num_gpus_per_node": 8, + "rollout_num_gpus": null, + "use_critic": false, + "critic_num_nodes": null, + "critic_num_gpus_per_node": null, + "advantage_estimator": "grpo", + "n_samples_per_prompt": 4, + "eps_clip": 0.2, + "eps_clip_high": null, + "use_kl_loss": true, + "kl_loss_type": "low_var_kl", + "kl_loss_coef": 0.001, + "kl_coef": 0.0, + "entropy_coef": 0.001, + "calculate_per_token_loss": true, + "ref_load": "/checkpoints/Qwen3.8-27B_torch_dist_tp4pp2", + "over_sampling_batch_size": null, + "dynamic_sampling_filter_path": null, + "balance_data": true, + "rollout_shuffle": true, + "rollout_top_p": 1.0, + "rollout_stop_token_ids": null, + "sglang_mem_fraction_static": 0.75, + "use_fault_tolerance": true, + "rollout_health_check_interval": 30, + "rollout_health_check_timeout": 30, + "rollout_health_check_first_wait": 300, + "global_batch_size": 64, + "lr": 1e-06, + "lr_decay_style": "constant", + "weight_decay": 0.01, + "adam_beta1": 0.9, + "adam_beta2": 0.98, + "optimizer": "adam", + "attention_dropout": 0.0, + "hidden_dropout": 0.0, + "attention_softmax_in_fp32": true, + "accumulate_allreduce_grads_in_fp32": true, + "use_distributed_optimizer": false, + "recompute_granularity": "full", + "recompute_method": "uniform", + "recompute_num_layers": 1, + "use_dynamic_batch_size": true, + "max_tokens_per_gpu": 12288, + "qkv_format": "thd", + "eval_interval": null, + "n_samples_per_eval_prompt": 4, + "eval_max_response_len": 16384, + "eval_top_p": 1.0, + "eval_config": null, + "save": "/checkpoints", + "load": "/checkpoints/perfect-adhesive-da14ace5a831", + "no_save_optim": false, + "no_load_optim": false, + "megatron_to_hf_mode": "", + "freeze_params_name_list": null, + "update_weight_mode": "full", + "update_weight_transport": "nccl", + "update_weight_encoding": "indices", + "update_weight_disk_dir": "", + "rm_type": "deepscaler", + "sglang_enable_dp_attention": false, + "sglang_dp_size": null, + "sglang_ep_size": null, + "sglang_enable_dp_lm_head": false, + "sglang_disable_custom_all_reduce": false, + "sglang_cuda_graph_bs": null, + "sglang_cuda_graph_backend_prefill": "disabled", + "sglang_max_running_requests": null, + "sglang_tool_call_parser": null, + "sglang_reasoning_parser": null, + "sglang_config": null, + "apply_chat_template_kwargs": "{\"enable_thinking\": false}", + "train_env_vars": null, + "multimodal_keys": null, + "hf_checkpoint": "Qwen/Qwen3.8-27B", + "pipeline_model_parallel_size": 2, + "decoder_last_pipeline_num_layers": 30, + "context_parallel_size": 4, + "expert_model_parallel_size": 1, + "expert_tensor_parallel_size": 1, + "sglang_speculative_algorithm": null, + "sglang_speculative_num_steps": null, + "sglang_speculative_eagle_topk": null, + "sglang_speculative_num_draft_tokens": null, + "sglang_mamba_scheduler_strategy": "extra_buffer", + "optimizer_cpu_offload": true, + "overlap_cpu_optimizer_d2h_h2d": true, + "use_precision_aware_optimizer": true, + "attention_backend": "flash", + "prompt_data": "/data/2d661a1f-cff4-40fa-9756-a148c8cbbb7f-383b42b9053132de.jsonl", + "eval_prompt_data": null, + "input_key": "messages", + "label_key": "label", + "apply_chat_template": true, + "custom_config_path": { + "balatro_max_decisions": 250, + "balatro_context_len": 49152, + "balatro_capture_screenshot": false, + "custom_generate_function_path": "notebook_custom_generate_ndnib50f.generate_balatro_episode", + "override_opt_param_scheduler": true, + "custom_rm_path": "notebook_custom_rm_v7fr2std.passthrough_reward", + "custom_reward_post_process_path": "notebook_custom_reward_post_process_vvgqtrfs.normalize_episode_rewards" + }, + "custom_rollout_log_function_path": "modal_training_gym.frameworks.slime.phase_reporting.log_rollout_data", + "custom_eval_rollout_log_function_path": "modal_training_gym.frameworks.slime.phase_reporting.log_eval_rollout_data", + "custom_megatron_before_log_prob_hook_path": "modal_training_gym.frameworks.slime.phase_reporting.before_log_prob_hook", + "custom_megatron_before_train_step_hook_path": "modal_training_gym.frameworks.slime.phase_reporting.before_train_step_hook" + }, + "metrics": {}, + "dataset": { + "hf_repo": "", + "name": "BalatroSeedsDataset" + }, + "eval_dataset": null, + "lr": 1e-06, + "global_batch_size": 64 + }, + "dataset_id": "", + "deployment_id": "", + "status": "failed", + "framework_status": "initialize_rollouts", + "created_at": 1789435612, + "started_at": 1789435612, + "ended_at": 1789460930, + "completed_at": 1789460930, + "updated_at": 1789460931, + "duration_seconds": 25318, + "step_times": null, + "substep_times": null, + "error_message": "Job entrypoint command failed with exit code 1, last available logs (truncated to 20,000 chars):\n File \"/usr/lib/python3.12/contextlib.py\", line 137, in __enter__\n return next(self.gen)\n ^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.12/dist-packages/torch/distributed/checkpoint/filesystem.py\", line 520, in create_stream\n with path.open(mode) as stream:\n ^^^^^^^^^^^^^^^\n File \"/usr/lib/python3.12/pathlib.py\", line 1015, in open\n return io.open(self, mode, buffering, encoding, errors, newline)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nFileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'\n (training_run_id=broad-layover-3a0ce678e7da)", + "metadata": { + "framework_progress": { + "phase": "initialize_rollouts", + "updated_at": 1789460382, + "current": 450, + "total": 600, + "unit": "step", + "rollout_id": 449, + "step_id": 449 + }, + "dashboard_components": { + "balatro": { + "name": "balatro", + "type": "trajectory_viewer", + "sha256": "3f05ebe30f20172fb596b4b9dcb8a4a74a4919354599b017ad3cca65e5a0898b", + "filename": "TrajectoryViewer.svelte", + "size": 1067084, + "path": "components/trajectory_viewer/3f05ebe30f20172fb596b4b9dcb8a4a74a4919354599b017ad3cca65e5a0898b/balatro.svelte", + "contract": "training-gym-dashboard-component/v1", + "training_run_id": "broad-layover-3a0ce678e7da" + } + }, + "checkpoint_location": { + "checkpoint_dir": "/checkpoints/broad-layover-3a0ce678e7da", + "checkpoints_volume_name": "slime-qwen3_8_27b_recipe-checkpoints", + "checkpoints_mount_path": "/checkpoints" + }, + "attempt_count": 4, + "last_attempt_started_at": 1789460314, + "last_attempt_status": "failed", + "attempt_starts": [ + 1789435732, + 1789459044, + 1789459634, + 1789460314 + ], + "latest_rollout": { + "rollout_id": 449, + "mean": 46.390621725063134, + "total": 64, + "created_at": 1789458580 + }, + "last_attempt_ended_at": 1789460930, + "resumed_from_checkpoint": true, + "resume_checkpoint_path": "/checkpoints/broad-layover-3a0ce678e7da/iter_0000449", + "resume_checkpoint_name": "iter_0000449", + "resume_from_iteration": 449 + }, + "function_call_id": "fc-01M2HAV5N51FQR76MTYN7KX1KP", + "app_name": "", + "source_model": null, + "metrics": {}, + "group_id": null + }, + "windows": [ + { + "steps": [ + 400, + 409 + ], + "episodes": 640, + "reward": 51.89420874408144, + "ante": 3.2625, + "wins": 0.0 + }, + { + "steps": [ + 410, + 419 + ], + "episodes": 640, + "reward": 48.308131432094385, + "ante": 3.109375, + "wins": 0.0 + }, + { + "steps": [ + 420, + 429 + ], + "episodes": 640, + "reward": 49.42259042475537, + "ante": 3.165625, + "wins": 0.0 + }, + { + "steps": [ + 430, + 439 + ], + "episodes": 640, + "reward": 50.61567625256471, + "ante": 3.175, + "wins": 0.0 + }, + { + "steps": [ + 440, + 449 + ], + "episodes": 640, + "reward": 39.13122086005141, + "ante": 2.6671875, + "wins": 0.0 + } + ], + "sampled_trajectory_audits": { + "400": { + "rollout": 400, + "episodes": 64, + "reward": 52.22539668560606, + "endings": { + "game_over": 55, + "context_cap": 9 + }, + "antes": { + "4": 26, + "2": 12, + "3": 3, + "5": 10, + "6": 1, + "1": 12 + }, + "actions": { + "select": 600, + "play": 1314, + "discard": 1204, + "buy": 793, + "pack": 392, + "next_round": 536, + "use": 209, + "reroll": 2 + }, + "played_hands": { + "Full House": 184, + "Two Pair": 660, + "Three of a Kind": 218, + "Pair": 208, + "High Card": 25, + "Straight": 1, + "Five of a Kind": 1, + "Four of a Kind": 15, + "Flush": 2 + }, + "pair_or_high_with_raw_straight_available": 30, + "affected_samples": 21, + "total_plays": 1314, + "plays_including_debuffed_card": 126, + "losses_with_discards_left": 21, + "caps": [ + { + "sample": 7, + "ante": 6, + "turns": 131, + "reward": 98.0, + "tokens": 48561, + "phase": "shop", + "remaining": null + }, + { + "sample": 8, + "ante": 4, + "turns": 120, + "reward": 88.0, + "tokens": 48458, + "phase": "shop", + "remaining": null + }, + { + "sample": 10, + "ante": 4, + "turns": 113, + "reward": 84.0, + "tokens": 48874, + "phase": "shop", + "remaining": null + }, + { + "sample": 25, + "ante": 4, + "turns": 123, + "reward": 88.0, + "tokens": 48992, + "phase": "pack", + "remaining": null + }, + { + "sample": 36, + "ante": 5, + "turns": 122, + "reward": 88.0, + "tokens": 48797, + "phase": "hand", + "remaining": { + "type": "boss", + "score": [ + 0, + 22000 + ], + "hands": 4, + "discards": 4, + "effect": "All face cards are drawn face down" + } + }, + { + "sample": 37, + "ante": 5, + "turns": 109, + "reward": 88.19772727272728, + "tokens": 48894, + "phase": "hand", + "remaining": { + "type": "boss", + "score": [ + 4350, + 22000 + ], + "hands": 4, + "discards": 1, + "effect": "All face cards are drawn face down" + } + }, + { + "sample": 38, + "ante": 5, + "turns": 113, + "reward": 88.44136363636363, + "tokens": 48699, + "phase": "hand", + "remaining": { + "type": "boss", + "score": [ + 9710, + 22000 + ], + "hands": 2, + "discards": 2, + "effect": "All face cards are drawn face down" + } + }, + { + "sample": 45, + "ante": 5, + "turns": 103, + "reward": 76.93945454545455, + "tokens": 48781, + "phase": "hand", + "remaining": { + "type": "small", + "score": [ + 10334, + 11000 + ], + "hands": 1, + "discards": 1 + } + }, + { + "sample": 47, + "ante": 5, + "turns": 102, + "reward": 80.0, + "tokens": 48782, + "phase": "hand", + "remaining": { + "type": "big", + "score": [ + 0, + 16500 + ], + "hands": 4, + "discards": 2 + } + } + ], + "groups": { + "3200": [ + { + "index": 0, + "reward": 64.91, + "ante": 4 + }, + { + "index": 1, + "reward": 68.68, + "ante": 4 + }, + { + "index": 2, + "reward": 68.57, + "ante": 4 + }, + { + "index": 3, + "reward": 68.47, + "ante": 4 + } + ], + "3201": [ + { + "index": 4, + "reward": 28.53, + "ante": 2 + }, + { + "index": 5, + "reward": 46.78, + "ante": 3 + }, + { + "index": 6, + "reward": 78.51, + "ante": 5 + }, + { + "index": 7, + "reward": 98.0, + "ante": 6 + } + ], + "3202": [ + { + "index": 8, + "reward": 88.0, + "ante": 4 + }, + { + "index": 9, + "reward": 48.5, + "ante": 2 + }, + { + "index": 10, + "reward": 84.0, + "ante": 4 + }, + { + "index": 11, + "reward": 84.87, + "ante": 4 + } + ], + "3203": [ + { + "index": 12, + "reward": 28.63, + "ante": 2 + }, + { + "index": 13, + "reward": 28.47, + "ante": 2 + }, + { + "index": 14, + "reward": 28.56, + "ante": 2 + }, + { + "index": 15, + "reward": 28.57, + "ante": 2 + } + ], + "3204": [ + { + "index": 16, + "reward": 8.55, + "ante": 1 + }, + { + "index": 17, + "reward": 8.55, + "ante": 1 + }, + { + "index": 18, + "reward": 8.27, + "ante": 1 + }, + { + "index": 19, + "reward": 8.55, + "ante": 1 + } + ], + "3205": [ + { + "index": 20, + "reward": 68.47, + "ante": 4 + }, + { + "index": 21, + "reward": 28.56, + "ante": 2 + }, + { + "index": 22, + "reward": 68.64, + "ante": 4 + }, + { + "index": 23, + "reward": 60.82, + "ante": 4 + } + ], + "3206": [ + { + "index": 24, + "reward": 28.96, + "ante": 2 + }, + { + "index": 25, + "reward": 88.0, + "ante": 4 + }, + { + "index": 26, + "reward": 28.95, + "ante": 2 + }, + { + "index": 27, + "reward": 28.96, + "ante": 2 + } + ], + "3207": [ + { + "index": 28, + "reward": 68.51, + "ante": 4 + }, + { + "index": 29, + "reward": 68.52, + "ante": 4 + }, + { + "index": 30, + "reward": 24.31, + "ante": 2 + }, + { + "index": 31, + "reward": 28.77, + "ante": 2 + } + ], + "3208": [ + { + "index": 32, + "reward": 0.62, + "ante": 1 + }, + { + "index": 33, + "reward": 0.61, + "ante": 1 + }, + { + "index": 34, + "reward": 68.36, + "ante": 4 + }, + { + "index": 35, + "reward": 68.33, + "ante": 4 + } + ], + "3209": [ + { + "index": 36, + "reward": 88.0, + "ante": 5 + }, + { + "index": 37, + "reward": 88.2, + "ante": 5 + }, + { + "index": 38, + "reward": 88.44, + "ante": 5 + }, + { + "index": 39, + "reward": 68.73, + "ante": 4 + } + ], + "3210": [ + { + "index": 40, + "reward": 8.64, + "ante": 1 + }, + { + "index": 41, + "reward": 8.8, + "ante": 1 + }, + { + "index": 42, + "reward": 64.56, + "ante": 4 + }, + { + "index": 43, + "reward": 8.64, + "ante": 1 + } + ], + "3211": [ + { + "index": 44, + "reward": 46.41, + "ante": 3 + }, + { + "index": 45, + "reward": 76.94, + "ante": 5 + }, + { + "index": 46, + "reward": 58.69, + "ante": 4 + }, + { + "index": 47, + "reward": 80.0, + "ante": 5 + } + ], + "3212": [ + { + "index": 48, + "reward": 0.31, + "ante": 1 + }, + { + "index": 49, + "reward": 64.48, + "ante": 4 + }, + { + "index": 50, + "reward": 64.45, + "ante": 4 + }, + { + "index": 51, + "reward": 64.52, + "ante": 4 + } + ], + "3213": [ + { + "index": 52, + "reward": 68.59, + "ante": 4 + }, + { + "index": 53, + "reward": 8.64, + "ante": 1 + }, + { + "index": 54, + "reward": 8.64, + "ante": 1 + }, + { + "index": 55, + "reward": 64.67, + "ante": 4 + } + ], + "3214": [ + { + "index": 56, + "reward": 88.85, + "ante": 5 + }, + { + "index": 57, + "reward": 86.64, + "ante": 5 + }, + { + "index": 58, + "reward": 88.63, + "ante": 5 + }, + { + "index": 59, + "reward": 88.77, + "ante": 5 + } + ], + "3215": [ + { + "index": 60, + "reward": 68.5, + "ante": 4 + }, + { + "index": 61, + "reward": 48.76, + "ante": 3 + }, + { + "index": 62, + "reward": 68.18, + "ante": 4 + }, + { + "index": 63, + "reward": 68.41, + "ante": 4 + } + ] + }, + "opportunity_examples": [ + { + "sample": 7, + "turn": 84, + "ante": 4, + "blind": { + "type": "big", + "score": [ + 0, + 7500 + ], + "hands": 4, + "discards": 2 + }, + "hand": [ + "S_A", + "H_A", + { + "id": "D_K", + "mod": { + "enhancement": "MULT" + } + }, + "H_Q", + "C_J", + "H_T", + "D_T", + "S_3" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 2, + 6, + 5, + 4, + 3 + ] + } + }, + "commit": { + "hand": "Pair", + "cards": [ + 2, + 6, + 5, + 4, + 3 + ], + "score": 1980 + }, + "straight": [ + 5, + 4, + 3, + 2, + 0 + ], + "jokers": [ + { + "id": "j_hanging_chad", + "effect": "Retrigger first played card used in scoring 2 additional times", + "sell": 2 + }, + { + "id": "j_mad", + "effect": "+10 Mult if played hand contains a Two Pair", + "mod": { + "enhancement": "TYPE MULT" + }, + "sell": 2 + }, + { + "id": "j_supernova", + "effect": "Adds the number of times poker hand has been played this run to Mult", + "mod": { + "enhancement": "HAND PLAYED MULT" + }, + "sell": 2 + }, + { + "id": "j_castle", + "effect": "This Joker gains +3 Chips per discarded Spade card, suit changes every round (Currently +15 Chips)", + "mod": { + "edition": "HOLO" + }, + "sell": 4 + }, + { + "id": "j_odd_todd", + "effect": "Played cards with odd rank give +31 Chips when scored (A, 9, 7, 5, 3)", + "mod": { + "enhancement": "ODD BUFF" + }, + "sell": 2 + } + ], + "boss": [ + "The Head", + 10000, + "All Heart cards are debuffed" + ] + }, + { + "sample": 11, + "turn": 103, + "ante": 4, + "blind": { + "type": "small", + "score": [ + 3216, + 5000 + ], + "hands": 2, + "discards": 0 + }, + "hand": [ + "C_A", + "S_K", + "D_K", + "C_Q", + "D_J", + { + "id": "H_T", + "mod": { + "edition": "POLYCHROME" + } + }, + "S_5", + "S_2" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 2, + 1, + 6, + 7, + 5 + ] + } + }, + "commit": { + "hand": "Pair", + "cards": [ + 2, + 1, + 6, + 7, + 5 + ], + "score": 1008 + }, + "straight": [ + 5, + 4, + 3, + 1, + 0 + ], + "jokers": [ + { + "id": "j_misprint", + "effect": " +", + "mod": { + "enhancement": "RANDOM MULT" + }, + "sell": 2 + }, + { + "id": "j_cartomancer", + "effect": "Create a Tarot card when Blind is selected (Must have room)", + "mod": { + "edition": "FOIL", + "enhancement": "TAROT BUFF" + }, + "sell": 4 + }, + { + "id": "j_golden", + "effect": "Earn $4 at end of round", + "mod": { + "enhancement": "BONUS DOLLARS" + }, + "sell": 3 + }, + { + "id": "j_bull", + "effect": "+2 Chips for each $1 you have (Currently +4 Chips)", + "sell": 3 + }, + { + "id": "j_delayed_grat", + "effect": "Earn $2 per discard if no discards are used by end of the round", + "mod": { + "enhancement": "DISCARD DOLLARS" + }, + "sell": 2 + } + ], + "boss": [ + "The Manacle", + 10000, + "-1 Hand Size" + ] + }, + { + "sample": 11, + "turn": 104, + "ante": 4, + "blind": { + "type": "small", + "score": [ + 4224, + 5000 + ], + "hands": 1, + "discards": 0 + }, + "hand": [ + "S_A", + "C_A", + "H_K", + "C_Q", + "D_J", + "S_T", + "H_T", + "H_4" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 2, + 3, + 6, + 5, + 7 + ] + } + }, + "commit": { + "hand": "Pair", + "cards": [ + 2, + 3, + 6, + 5, + 7 + ], + "score": 1008 + }, + "straight": [ + 5, + 4, + 3, + 2, + 0 + ], + "jokers": [ + { + "id": "j_misprint", + "effect": " +", + "mod": { + "enhancement": "RANDOM MULT" + }, + "sell": 2 + }, + { + "id": "j_cartomancer", + "effect": "Create a Tarot card when Blind is selected (Must have room)", + "mod": { + "edition": "FOIL", + "enhancement": "TAROT BUFF" + }, + "sell": 4 + }, + { + "id": "j_golden", + "effect": "Earn $4 at end of round", + "mod": { + "enhancement": "BONUS DOLLARS" + }, + "sell": 3 + }, + { + "id": "j_bull", + "effect": "+2 Chips for each $1 you have (Currently +4 Chips)", + "sell": 3 + }, + { + "id": "j_delayed_grat", + "effect": "Earn $2 per discard if no discards are used by end of the round", + "mod": { + "enhancement": "DISCARD DOLLARS" + }, + "sell": 2 + } + ], + "boss": [ + "The Manacle", + 10000, + "-1 Hand Size" + ] + } + ], + "examples": [ + { + "sample": 0, + "reward": 64.90520000000001, + "ending": "game_over", + "ante": 4, + "last_before": { + "phase": "hand", + "ante": 4, + "round": 11, + "money": 0, + "blind": { + "type": "big", + "score": [ + 6789, + 7500 + ], + "hands": 1, + "discards": 0 + }, + "boss": [ + "The Wall", + 20000, + "Extra large blind" + ], + "hand": [ + "D_A", + "H_K", + "C_Q", + "S_9", + "S_8", + "C_8", + "H_7", + "D_6", + "D_4" + ], + "jokers": { + "slots": "5/5", + "items": [ + { + "id": "j_photograph", + "effect": "First played face card gives X2 Mult when scored", + "sell": 2 + }, + { + "id": "j_misprint", + "effect": " +", + "mod": { + "enhancement": "RANDOM MULT" + }, + "sell": 2 + }, + { + "id": "j_wrathful_joker", + "effect": "Played cards with Spade suit give +3 Mult when scored", + "mod": { + "enhancement": "SUIT MULT" + }, + "sell": 2 + }, + { + "id": "j_lusty_joker", + "effect": "Played cards with Heart suit give +3 Mult when scored", + "mod": { + "enhancement": "SUIT MULT" + }, + "sell": 2 + }, + { + "id": "j_riff_raff", + "effect": "When Blind is selected, create 2 Common Jokers (Must have room)", + "sell": 3 + } + ] + }, + "consumables": { + "slots": "0/2" + }, + "hand_values": { + "Four of a Kind": [ + 2, + 90, + 10 + ], + "Full House": [ + 1, + 40, + 4 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 2, + 60, + 7 + ], + "Three of a Kind": [ + 2, + 50, + 5 + ], + "Two Pair": [ + 2, + 40, + 3 + ], + "Pair": [ + 1, + 10, + 2 + ], + "High Card": [ + 1, + 5, + 1 + ], + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ], + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ] + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 5, + 4, + 3, + 2, + 1 + ] + } + }, + "last_after": { + "phase": "GAME_OVER", + "blind": { + "type": "big", + "score": [ + 7309, + 7500 + ], + "hands": 0, + "discards": 0 + }, + "play_commit": { + "score": 520, + "hand": "Pair", + "cards": [ + 5, + 4, + 3, + 2, + 1 + ] + } + } + }, + { + "sample": 7, + "reward": 98.0, + "ending": "context_cap", + "ante": 6, + "last_before": { + "phase": "hand", + "ante": 5, + "round": 15, + "money": 1, + "blind": { + "type": "boss", + "score": [ + 17756, + 22000 + ], + "hands": 1, + "discards": 1, + "effect": "All face cards are debuffed" + }, + "boss": [ + "The Plant", + 22000, + "All face cards are debuffed" + ], + "hand": [ + "H_A", + "D_A", + { + "id": "C_Q", + "debuff": true + }, + "H_9", + "C_9", + { + "id": "H_8", + "mod": { + "seal": "GOLD" + } + }, + "C_7", + "C_3" + ], + "jokers": { + "slots": "5/5", + "items": [ + { + "id": "j_hanging_chad", + "effect": "Retrigger first played card used in scoring 2 additional times", + "sell": 2 + }, + { + "id": "j_mad", + "effect": "+10 Mult if played hand contains a Two Pair", + "mod": { + "enhancement": "TYPE MULT" + }, + "sell": 2 + }, + { + "id": "j_supernova", + "effect": "Adds the number of times poker hand has been played this run to Mult", + "mod": { + "enhancement": "HAND PLAYED MULT" + }, + "sell": 2 + }, + { + "id": "j_castle", + "effect": "This Joker gains +3 Chips per discarded Heart card, suit changes every round (Currently +48 Chips)", + "mod": { + "edition": "HOLO" + }, + "sell": 4 + }, + { + "id": "j_odd_todd", + "effect": "Played cards with odd rank give +31 Chips when scored (A, 9, 7, 5, 3)", + "mod": { + "enhancement": "ODD BUFF" + }, + "sell": 2 + } + ] + }, + "consumables": { + "slots": "0/2" + }, + "hand_values": { + "Flush House": [ + 1, + 140, + 14 + ], + "Flush Five": [ + 1, + 160, + 16 + ], + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 1, + 40, + 4 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 2, + 60, + 7 + ], + "Three of a Kind": [ + 1, + 30, + 3 + ], + "Two Pair": [ + 3, + 60, + 4 + ], + "Pair": [ + 5, + 70, + 6 + ], + "High Card": [ + 1, + 5, + 1 + ] + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 2, + 3, + 4, + 1, + 0 + ] + } + }, + "last_after": { + "phase": "shop", + "blind": null, + "play_commit": { + "hand": "Two Pair", + "cards": [ + 2, + 3, + 4, + 1, + 0 + ], + "score": 14596 + } + } + } + ], + "error_count": 10 + }, + "413": { + "rollout": 413, + "episodes": 64, + "reward": 36.44929845722854, + "endings": { + "game_over": 63, + "context_cap": 1 + }, + "antes": { + "1": 29, + "4": 19, + "3": 7, + "5": 7, + "2": 2 + }, + "actions": { + "select": 440, + "play": 1094, + "discard": 655, + "buy": 568, + "pack": 238, + "next_round": 376, + "use": 151, + "reroll": 1 + }, + "played_hands": { + "Pair": 280, + "Full House": 148, + "Two Pair": 512, + "Three of a Kind": 118, + "High Card": 30, + "Four of a Kind": 5, + "Straight": 1 + }, + "pair_or_high_with_raw_straight_available": 55, + "affected_samples": 38, + "total_plays": 1094, + "plays_including_debuffed_card": 97, + "losses_with_discards_left": 42, + "caps": [ + { + "sample": 14, + "ante": 5, + "turns": 117, + "reward": 86.0, + "tokens": 48508, + "phase": "hand", + "remaining": { + "type": "boss", + "score": [ + 0, + 22000 + ], + "hands": 4, + "discards": 1, + "effect": "#1# in #2# cards get drawn face down" + } + } + ], + "groups": { + "3408": [ + { + "index": 0, + "reward": 4.57, + "ante": 1 + }, + { + "index": 1, + "reward": 4.48, + "ante": 1 + }, + { + "index": 2, + "reward": 4.57, + "ante": 1 + }, + { + "index": 3, + "reward": 8.45, + "ante": 1 + } + ], + "3409": [ + { + "index": 4, + "reward": 8.66, + "ante": 1 + }, + { + "index": 5, + "reward": 0.8, + "ante": 1 + }, + { + "index": 6, + "reward": 0.8, + "ante": 1 + }, + { + "index": 7, + "reward": 8.34, + "ante": 1 + } + ], + "3410": [ + { + "index": 8, + "reward": 66.7, + "ante": 4 + }, + { + "index": 9, + "reward": 48.74, + "ante": 3 + }, + { + "index": 10, + "reward": 60.51, + "ante": 4 + }, + { + "index": 11, + "reward": 64.64, + "ante": 4 + } + ], + "3411": [ + { + "index": 12, + "reward": 82.33, + "ante": 5 + }, + { + "index": 13, + "reward": 48.1, + "ante": 3 + }, + { + "index": 14, + "reward": 86.0, + "ante": 5 + }, + { + "index": 15, + "reward": 0.52, + "ante": 1 + } + ], + "3412": [ + { + "index": 16, + "reward": 64.92, + "ante": 4 + }, + { + "index": 17, + "reward": 66.0, + "ante": 4 + }, + { + "index": 18, + "reward": 68.0, + "ante": 4 + }, + { + "index": 19, + "reward": 68.0, + "ante": 4 + } + ], + "3413": [ + { + "index": 20, + "reward": 4.62, + "ante": 1 + }, + { + "index": 21, + "reward": 4.5, + "ante": 1 + }, + { + "index": 22, + "reward": 4.62, + "ante": 1 + }, + { + "index": 23, + "reward": 4.76, + "ante": 1 + } + ], + "3414": [ + { + "index": 24, + "reward": 0.66, + "ante": 1 + }, + { + "index": 25, + "reward": 0.66, + "ante": 1 + }, + { + "index": 26, + "reward": 48.38, + "ante": 3 + }, + { + "index": 27, + "reward": 0.66, + "ante": 1 + } + ], + "3415": [ + { + "index": 28, + "reward": 68.59, + "ante": 4 + }, + { + "index": 29, + "reward": 64.67, + "ante": 4 + }, + { + "index": 30, + "reward": 64.82, + "ante": 4 + }, + { + "index": 31, + "reward": 64.77, + "ante": 4 + } + ], + "3416": [ + { + "index": 32, + "reward": 4.5, + "ante": 1 + }, + { + "index": 33, + "reward": 4.5, + "ante": 1 + }, + { + "index": 34, + "reward": 4.39, + "ante": 1 + }, + { + "index": 35, + "reward": 24.68, + "ante": 2 + } + ], + "3417": [ + { + "index": 36, + "reward": 88.38, + "ante": 5 + }, + { + "index": 37, + "reward": 48.85, + "ante": 3 + }, + { + "index": 38, + "reward": 88.46, + "ante": 5 + }, + { + "index": 39, + "reward": 88.67, + "ante": 5 + } + ], + "3418": [ + { + "index": 40, + "reward": 0.61, + "ante": 1 + }, + { + "index": 41, + "reward": 0.61, + "ante": 1 + }, + { + "index": 42, + "reward": 64.83, + "ante": 4 + }, + { + "index": 43, + "reward": 0.61, + "ante": 1 + } + ], + "3419": [ + { + "index": 44, + "reward": 0.77, + "ante": 1 + }, + { + "index": 45, + "reward": 64.95, + "ante": 4 + }, + { + "index": 46, + "reward": 64.45, + "ante": 4 + }, + { + "index": 47, + "reward": 28.41, + "ante": 2 + } + ], + "3420": [ + { + "index": 48, + "reward": 68.36, + "ante": 4 + }, + { + "index": 49, + "reward": 64.68, + "ante": 4 + }, + { + "index": 50, + "reward": 60.63, + "ante": 4 + }, + { + "index": 51, + "reward": 62.36, + "ante": 4 + } + ], + "3421": [ + { + "index": 52, + "reward": 66.59, + "ante": 4 + }, + { + "index": 53, + "reward": 48.25, + "ante": 3 + }, + { + "index": 54, + "reward": 80.71, + "ante": 5 + }, + { + "index": 55, + "reward": 82.24, + "ante": 5 + } + ], + "3422": [ + { + "index": 56, + "reward": 8.7, + "ante": 1 + }, + { + "index": 57, + "reward": 48.33, + "ante": 3 + }, + { + "index": 58, + "reward": 44.69, + "ante": 3 + }, + { + "index": 59, + "reward": 0.61, + "ante": 1 + } + ], + "3423": [ + { + "index": 60, + "reward": 4.52, + "ante": 1 + }, + { + "index": 61, + "reward": 4.52, + "ante": 1 + }, + { + "index": 62, + "reward": 4.67, + "ante": 1 + }, + { + "index": 63, + "reward": 8.35, + "ante": 1 + } + ] + }, + "opportunity_examples": [ + { + "sample": 0, + "turn": 16, + "ante": 1, + "blind": { + "type": "big", + "score": [ + 256, + 450 + ], + "hands": 1, + "discards": 1 + }, + "hand": [ + "H_A", + "D_K", + "S_Q", + "H_J", + "D_J", + "H_T", + "S_8", + "C_2" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 4, + 3, + 5, + 6, + 2 + ] + } + }, + "commit": { + "score": 60, + "hand": "Pair", + "cards": [ + 4, + 3, + 5, + 6, + 2 + ] + }, + "straight": [ + 5, + 3, + 2, + 1, + 0 + ], + "jokers": [ + { + "id": "j_superposition", + "effect": "Create a Tarot card if poker hand contains an Ace and a Straight (Must have room)", + "sell": 2 + }, + { + "id": "j_devious", + "effect": "+100 Chips if played hand contains a Straight", + "sell": 2 + } + ], + "boss": [ + "The Hook", + 600, + "Discards 2 random cards per hand played" + ] + }, + { + "sample": 2, + "turn": 16, + "ante": 1, + "blind": { + "type": "big", + "score": [ + 256, + 450 + ], + "hands": 1, + "discards": 1 + }, + "hand": [ + "H_A", + "D_K", + "S_Q", + "H_J", + "D_J", + "H_T", + "S_8", + "C_2" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 4, + 3, + 6, + 5, + 2 + ] + } + }, + "commit": { + "hand": "Pair", + "cards": [ + 4, + 3, + 6, + 5, + 2 + ], + "score": 60 + }, + "straight": [ + 5, + 3, + 2, + 1, + 0 + ], + "jokers": [ + { + "id": "j_superposition", + "effect": "Create a Tarot card if poker hand contains an Ace and a Straight (Must have room)", + "sell": 2 + }, + { + "id": "j_devious", + "effect": "+100 Chips if played hand contains a Straight", + "sell": 2 + } + ], + "boss": [ + "The Hook", + 600, + "Discards 2 random cards per hand played" + ] + }, + { + "sample": 3, + "turn": 24, + "ante": 1, + "blind": { + "type": "boss", + "score": [ + 270, + 600 + ], + "hands": 1, + "discards": 0, + "effect": "Discards 2 random cards per hand played" + }, + "hand": [ + "S_A", + "D_K", + "H_Q", + "S_J", + "S_T", + "C_9", + "C_5", + "D_2" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 6, + 5, + 4, + 3, + 2 + ] + } + }, + "commit": { + "score": 15, + "hand": "High Card", + "cards": [ + 6, + 5, + 4, + 3, + 2 + ] + }, + "straight": [ + 4, + 3, + 2, + 1, + 0 + ], + "jokers": [ + { + "id": "j_superposition", + "effect": "Create a Tarot card if poker hand contains an Ace and a Straight (Must have room)", + "sell": 2 + }, + { + "id": "j_wrathful_joker", + "effect": "Played cards with Spade suit give +3 Mult when scored", + "mod": { + "enhancement": "SUIT MULT" + }, + "sell": 2 + } + ], + "boss": [ + "The Hook", + 600, + "Discards 2 random cards per hand played" + ] + } + ], + "examples": [ + { + "sample": 0, + "reward": 4.568888888888889, + "ending": "game_over", + "ante": 1, + "last_before": { + "phase": "hand", + "ante": 1, + "round": 2, + "money": 1, + "blind": { + "type": "big", + "score": [ + 256, + 450 + ], + "hands": 1, + "discards": 1 + }, + "boss": [ + "The Hook", + 600, + "Discards 2 random cards per hand played" + ], + "hand": [ + "H_A", + "D_K", + "S_Q", + "H_J", + "D_J", + "H_T", + "S_8", + "C_2" + ], + "jokers": { + "slots": "2/5", + "items": [ + { + "id": "j_superposition", + "effect": "Create a Tarot card if poker hand contains an Ace and a Straight (Must have room)", + "sell": 2 + }, + { + "id": "j_devious", + "effect": "+100 Chips if played hand contains a Straight", + "sell": 2 + } + ] + }, + "consumables": { + "slots": "0/2" + }, + "hand_values": { + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ], + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 1, + 40, + 4 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 1, + 30, + 3 + ], + "Two Pair": [ + 1, + 20, + 2 + ], + "Pair": [ + 1, + 10, + 2 + ], + "High Card": [ + 1, + 5, + 1 + ] + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 4, + 3, + 5, + 6, + 2 + ] + } + }, + "last_after": { + "phase": "GAME_OVER", + "blind": { + "type": "big", + "score": [ + 316, + 450 + ], + "hands": 0, + "discards": 1 + }, + "play_commit": { + "score": 60, + "hand": "Pair", + "cards": [ + 4, + 3, + 5, + 6, + 2 + ] + } + } + }, + { + "sample": 39, + "reward": 88.66927272727273, + "ending": "game_over", + "ante": 5, + "last_before": { + "phase": "hand", + "ante": 5, + "round": 15, + "money": 2, + "blind": { + "type": "boss", + "score": [ + 14724, + 22000 + ], + "hands": 1, + "discards": 0, + "effect": "All Heart cards are debuffed" + }, + "boss": [ + "The Head", + 22000, + "All Heart cards are debuffed" + ], + "hand": [ + "C_A", + "C_K", + "C_J", + "C_T", + { + "id": "H_8", + "debuff": true + }, + "S_6", + { + "id": "H_6", + "debuff": true + }, + { + "id": "H_4", + "debuff": true + } + ], + "jokers": { + "slots": "6/5", + "items": [ + { + "id": "j_shoot_the_moon", + "effect": "Each Queen held in hand gives +13 Mult", + "mod": { + "edition": "HOLO" + }, + "sell": 4 + }, + { + "id": "j_scary_face", + "effect": "Played face cards give +30 Chips when scored", + "mod": { + "enhancement": "SCARY FACES" + }, + "sell": 2 + }, + { + "id": "j_mad", + "effect": "+10 Mult if played hand contains a Two Pair", + "mod": { + "enhancement": "TYPE MULT" + }, + "sell": 2 + }, + { + "id": "j_hanging_chad", + "effect": "Retrigger first played card used in scoring 2 additional times", + "sell": 2 + }, + { + "id": "j_ice_cream", + "effect": "+5 Chips -5 Chips for every hand played", + "sell": 2 + }, + { + "id": "j_bloodstone", + "effect": "1 in 2 chance for played cards with Heart suit to give X1.5 Mult when scored", + "sell": 3 + } + ] + }, + "consumables": { + "slots": "2/2", + "items": [ + { + "id": "c_justice", + "effect": "Enhances 1 selected card into a Glass Card", + "mod": { + "enhancement": "ENHANCE" + }, + "sell": 1 + }, + { + "id": "c_magician", + "effect": "Enhances 2 selected cards to Lucky Cards", + "mod": { + "enhancement": "ENHANCE" + }, + "sell": 1 + } + ] + }, + "hand_values": { + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ], + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 2, + 90, + 10 + ], + "Full House": [ + 2, + 65, + 6 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 2, + 50, + 5 + ], + "Two Pair": [ + 2, + 40, + 3 + ], + "Pair": [ + 1, + 10, + 2 + ], + "High Card": [ + 1, + 5, + 1 + ] + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 5, + 4, + 6, + 3, + 2 + ] + } + }, + "last_after": { + "phase": "GAME_OVER", + "blind": { + "type": "boss", + "score": [ + 15120, + 22000 + ], + "hands": 0, + "discards": 0, + "effect": "All Heart cards are debuffed" + }, + "play_commit": { + "hand": "Pair", + "cards": [ + 5, + 4, + 6, + 3, + 2 + ], + "score": 396 + } + } + } + ], + "error_count": 7 + }, + "425": { + "rollout": 425, + "episodes": 64, + "reward": 45.764259150094695, + "endings": { + "game_over": 64 + }, + "antes": { + "4": 24, + "3": 9, + "1": 14, + "2": 11, + "5": 5, + "6": 1 + }, + "actions": { + "select": 530, + "discard": 784, + "play": 1384, + "buy": 664, + "pack": 282, + "next_round": 466, + "use": 122 + }, + "played_hands": { + "Pair": 398, + "Two Pair": 640, + "Full House": 134, + "Three of a Kind": 153, + "High Card": 35, + "Four of a Kind": 19, + "Straight": 5 + }, + "pair_or_high_with_raw_straight_available": 55, + "affected_samples": 37, + "total_plays": 1384, + "plays_including_debuffed_card": 163, + "losses_with_discards_left": 47, + "caps": [], + "groups": { + "3600": [ + { + "index": 0, + "reward": 64.36, + "ante": 4 + }, + { + "index": 1, + "reward": 64.2, + "ante": 4 + }, + { + "index": 2, + "reward": 64.29, + "ante": 4 + }, + { + "index": 3, + "reward": 64.21, + "ante": 4 + } + ], + "3601": [ + { + "index": 4, + "reward": 68.44, + "ante": 4 + }, + { + "index": 5, + "reward": 48.0, + "ante": 3 + }, + { + "index": 6, + "reward": 44.18, + "ante": 3 + }, + { + "index": 7, + "reward": 60.68, + "ante": 4 + } + ], + "3602": [ + { + "index": 8, + "reward": 4.58, + "ante": 1 + }, + { + "index": 9, + "reward": 4.58, + "ante": 1 + }, + { + "index": 10, + "reward": 4.58, + "ante": 1 + }, + { + "index": 11, + "reward": 4.58, + "ante": 1 + } + ], + "3603": [ + { + "index": 12, + "reward": 28.0, + "ante": 2 + }, + { + "index": 13, + "reward": 60.75, + "ante": 4 + }, + { + "index": 14, + "reward": 4.5, + "ante": 1 + }, + { + "index": 15, + "reward": 28.0, + "ante": 2 + } + ], + "3604": [ + { + "index": 16, + "reward": 60.85, + "ante": 4 + }, + { + "index": 17, + "reward": 20.54, + "ante": 2 + }, + { + "index": 18, + "reward": 64.62, + "ante": 4 + }, + { + "index": 19, + "reward": 20.6, + "ante": 2 + } + ], + "3605": [ + { + "index": 20, + "reward": 44.36, + "ante": 3 + }, + { + "index": 21, + "reward": 64.61, + "ante": 4 + }, + { + "index": 22, + "reward": 64.87, + "ante": 4 + }, + { + "index": 23, + "reward": 60.89, + "ante": 4 + } + ], + "3606": [ + { + "index": 24, + "reward": 68.49, + "ante": 4 + }, + { + "index": 25, + "reward": 68.6, + "ante": 4 + }, + { + "index": 26, + "reward": 68.66, + "ante": 4 + }, + { + "index": 27, + "reward": 44.68, + "ante": 3 + } + ], + "3607": [ + { + "index": 28, + "reward": 44.63, + "ante": 3 + }, + { + "index": 29, + "reward": 24.52, + "ante": 2 + }, + { + "index": 30, + "reward": 40.77, + "ante": 3 + }, + { + "index": 31, + "reward": 44.9, + "ante": 3 + } + ], + "3608": [ + { + "index": 32, + "reward": 82.53, + "ante": 4 + }, + { + "index": 33, + "reward": 88.32, + "ante": 4 + }, + { + "index": 34, + "reward": 68.51, + "ante": 4 + }, + { + "index": 35, + "reward": 88.2, + "ante": 4 + } + ], + "3609": [ + { + "index": 36, + "reward": 28.52, + "ante": 2 + }, + { + "index": 37, + "reward": 28.32, + "ante": 2 + }, + { + "index": 38, + "reward": 64.59, + "ante": 4 + }, + { + "index": 39, + "reward": 28.36, + "ante": 2 + } + ], + "3610": [ + { + "index": 40, + "reward": 28.27, + "ante": 2 + }, + { + "index": 41, + "reward": 28.17, + "ante": 2 + }, + { + "index": 42, + "reward": 66.23, + "ante": 4 + }, + { + "index": 43, + "reward": 28.29, + "ante": 2 + } + ], + "3611": [ + { + "index": 44, + "reward": 8.32, + "ante": 1 + }, + { + "index": 45, + "reward": 4.55, + "ante": 1 + }, + { + "index": 46, + "reward": 4.55, + "ante": 1 + }, + { + "index": 47, + "reward": 4.55, + "ante": 1 + } + ], + "3612": [ + { + "index": 48, + "reward": 100.76, + "ante": 5 + }, + { + "index": 49, + "reward": 66.73, + "ante": 4 + }, + { + "index": 50, + "reward": 68.62, + "ante": 4 + }, + { + "index": 51, + "reward": 98.69, + "ante": 5 + } + ], + "3613": [ + { + "index": 52, + "reward": 104.95, + "ante": 6 + }, + { + "index": 53, + "reward": 88.27, + "ante": 5 + }, + { + "index": 54, + "reward": 80.54, + "ante": 5 + }, + { + "index": 55, + "reward": 82.62, + "ante": 5 + } + ], + "3614": [ + { + "index": 56, + "reward": 48.45, + "ante": 3 + }, + { + "index": 57, + "reward": 0.48, + "ante": 1 + }, + { + "index": 58, + "reward": 0.72, + "ante": 1 + }, + { + "index": 59, + "reward": 40.5, + "ante": 3 + } + ], + "3615": [ + { + "index": 60, + "reward": 4.17, + "ante": 1 + }, + { + "index": 61, + "reward": 4.13, + "ante": 1 + }, + { + "index": 62, + "reward": 60.3, + "ante": 4 + }, + { + "index": 63, + "reward": 4.17, + "ante": 1 + } + ] + }, + "opportunity_examples": [ + { + "sample": 1, + "turn": 24, + "ante": 1, + "blind": { + "type": "boss", + "score": [ + 466, + 600 + ], + "hands": 2, + "discards": 1, + "effect": "All Club cards are debuffed" + }, + "hand": [ + "H_A", + { + "id": "C_K", + "debuff": true + }, + { + "id": "C_Q", + "debuff": true + }, + "H_J", + { + "id": "C_J", + "debuff": true + }, + "S_T", + "D_7", + "S_4" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 4, + 3, + 2, + 1, + 5 + ] + } + }, + "commit": { + "hand": "Pair", + "cards": [ + 4, + 3, + 2, + 1, + 5 + ], + "score": 140 + }, + "straight": [ + 5, + 3, + 2, + 1, + 0 + ], + "jokers": [ + { + "id": "j_sly", + "effect": "+50 Chips if played hand contains a Pair", + "sell": 1 + } + ], + "boss": [ + "The Club", + 600, + "All Club cards are debuffed" + ] + }, + { + "sample": 2, + "turn": 36, + "ante": 2, + "blind": { + "type": "big", + "score": [ + 0, + 1200 + ], + "hands": 4, + "discards": 1 + }, + "hand": [ + "S_K", + "H_Q", + "S_J", + "S_T", + "H_T", + "D_9", + "S_5", + "H_4" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 4, + 3, + 5, + 6, + 7 + ] + } + }, + "commit": { + "hand": "Pair", + "cards": [ + 4, + 3, + 5, + 6, + 7 + ], + "score": 1360 + }, + "straight": [ + 5, + 3, + 2, + 1, + 0 + ], + "jokers": [ + { + "id": "j_sly", + "effect": "+50 Chips if played hand contains a Pair", + "sell": 1 + }, + { + "id": "j_zany", + "effect": "+12 Mult if played hand contains a Three of a Kind", + "mod": { + "enhancement": "TYPE MULT" + }, + "sell": 2 + }, + { + "id": "j_gros_michel", + "effect": "+15 Mult 1 in 6 chance this card is destroyed at end of round", + "sell": 2 + }, + { + "id": "j_hiker", + "effect": "Every played card permanently gains +5 Chips when scored", + "sell": 2 + } + ], + "boss": [ + "The Wheel", + 1600, + "#1# in #2# cards get drawn face down" + ] + }, + { + "sample": 4, + "turn": 63, + "ante": 3, + "blind": { + "type": "big", + "score": [ + 1068, + 3000 + ], + "hands": 5, + "discards": 0 + }, + "hand": [ + "C_A", + "H_K", + "S_Q", + "H_J", + "H_T", + "C_7", + "C_5", + "D_3" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 2, + 3, + 1, + 0, + 6 + ] + } + }, + "commit": { + "hand": "High Card", + "score": 384, + "cards": [ + 2, + 3, + 1, + 0, + 6 + ] + }, + "straight": [ + 4, + 3, + 2, + 1, + 0 + ], + "jokers": [ + { + "id": "j_fibonacci", + "effect": "Each played Ace, 2, 3, 5, or 8 gives +8 Mult when scored", + "mod": { + "enhancement": "CARD MULT" + }, + "sell": 4 + }, + { + "id": "j_walkie_talkie", + "effect": "Each played 10 or 4 gives +10 Chips and +4 Mult when scored", + "sell": 2 + }, + { + "id": "j_burglar", + "effect": "When Blind is selected, gain +3 Hands and lose all discards", + "sell": 3 + }, + { + "id": "j_luchador", + "effect": "Sell this card to disable the current Boss Blind ", + "sell": 2 + }, + { + "id": "j_mystic_summit", + "effect": "+15 Mult when 0 discards remaining", + "mod": { + "enhancement": "NO DISCARD MULT" + }, + "sell": 2 + } + ], + "boss": [ + "The Needle", + 2000, + "Play only 1 hand" + ] + } + ], + "examples": [ + { + "sample": 0, + "reward": 64.3572, + "ending": "game_over", + "ante": 4, + "last_before": { + "phase": "hand", + "ante": 4, + "round": 11, + "money": 2, + "blind": { + "type": "big", + "score": [ + 2679, + 7500 + ], + "hands": 1, + "discards": 3 + }, + "boss": [ + "The Arm", + 10000, + "Decrease level of played poker hand" + ], + "hand": [ + "D_A", + "S_J", + "H_J", + "C_J", + "S_8", + "C_8", + "H_3", + "D_2" + ], + "jokers": { + "slots": "5/5", + "items": [ + { + "id": "j_sly", + "effect": "+50 Chips if played hand contains a Pair", + "sell": 1 + }, + { + "id": "j_hiker", + "effect": "Every played card permanently gains +5 Chips when scored", + "sell": 2 + }, + { + "id": "j_crafty", + "effect": "+80 Chips if played hand contains a Flush", + "sell": 2 + }, + { + "id": "j_clever", + "effect": "+80 Chips if played hand contains a Two Pair", + "sell": 2 + }, + { + "id": "j_splash", + "effect": "Every played card counts in scoring", + "sell": 1 + } + ] + }, + "consumables": { + "slots": "2/2", + "items": [ + { + "id": "c_sun", + "effect": "Converts up to 3 selected cards to Hearts", + "mod": { + "enhancement": "SUIT CONVERSION" + }, + "sell": 1 + }, + { + "id": "c_justice", + "effect": "Enhances 1 selected card into a Glass Card", + "mod": { + "enhancement": "ENHANCE" + }, + "sell": 1 + } + ] + }, + "hand_values": { + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 2, + 65, + 6 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 1, + 30, + 3 + ], + "Two Pair": [ + 2, + 40, + 3 + ], + "Pair": [ + 2, + 25, + 3 + ], + "High Card": [ + 1, + 5, + 1 + ], + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ], + "Five of a Kind": [ + 1, + 120, + 12 + ] + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 3, + 2, + 1, + 5, + 4 + ] + } + }, + "last_after": { + "phase": "GAME_OVER", + "blind": { + "type": "big", + "score": [ + 4155, + 7500 + ], + "hands": 0, + "discards": 3 + }, + "play_commit": { + "cards": [ + 3, + 2, + 1, + 5, + 4 + ], + "hand": "Full House", + "score": 1476 + } + } + }, + { + "sample": 52, + "reward": 104.9491, + "ending": "game_over", + "ante": 6, + "last_before": { + "phase": "hand", + "ante": 6, + "round": 17, + "money": 3, + "blind": { + "type": "big", + "score": [ + 28473, + 30000 + ], + "hands": 1, + "discards": 2 + }, + "boss": [ + "The Ox", + 40000, + "Playing a #1# sets money to $0" + ], + "hand": [ + "H_K", + "D_K", + "H_J", + "C_T", + "D_T", + "D_9", + "C_5", + "D_3" + ], + "jokers": { + "slots": "4/5", + "items": [ + { + "id": "j_madness", + "effect": "When Small Blind or Big Blind is selected, gain X0.5 Mult and destroy a random Joker (Currently X5.5 Mult)", + "sell": 3 + }, + { + "id": "j_swashbuckler", + "effect": "Adds the sell value of all other owned Jokers to Mult (Currently +8 Mult)", + "mod": { + "enhancement": "SET MULT" + }, + "sell": 2 + }, + { + "id": "j_clever", + "effect": "+80 Chips if played hand contains a Two Pair", + "sell": 2 + }, + { + "id": "j_rocket", + "effect": "Earn $1 at end of round Payout increases by $2 when Boss Blind is defeated", + "sell": 3 + } + ] + }, + "consumables": { + "slots": "2/3", + "items": [ + { + "id": "c_sun", + "effect": "Converts up to 3 selected cards to Hearts", + "mod": { + "enhancement": "SUIT CONVERSION" + }, + "sell": 1 + }, + { + "id": "c_chariot", + "effect": "Enhances 1 selected card into a Steel Card", + "mod": { + "enhancement": "ENHANCE" + }, + "sell": 1 + } + ] + }, + "hand_values": { + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 2, + 65, + 6 + ], + "Flush": [ + 2, + 50, + 6 + ], + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 2, + 50, + 5 + ], + "High Card": [ + 1, + 5, + 1 + ], + "Pair": [ + 1, + 10, + 2 + ], + "Two Pair": [ + 5, + 100, + 6 + ], + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ] + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 2, + 3, + 4, + 5, + 6 + ] + } + }, + "last_after": { + "phase": "GAME_OVER", + "blind": { + "type": "big", + "score": [ + 29043, + 30000 + ], + "hands": 0, + "discards": 2 + }, + "play_commit": { + "hand": "Pair", + "cards": [ + 2, + 3, + 4, + 5, + 6 + ], + "score": 570 + } + } + } + ], + "error_count": 7 + }, + "440": { + "rollout": 440, + "episodes": 64, + "reward": 26.462248784722224, + "endings": { + "game_over": 64 + }, + "antes": { + "2": 18, + "1": 26, + "3": 13, + "4": 7 + }, + "actions": { + "select": 339, + "play": 956, + "discard": 278, + "buy": 310, + "next_round": 275, + "pack": 84, + "use": 44 + }, + "played_hands": { + "Pair": 423, + "Three of a Kind": 71, + "High Card": 46, + "Two Pair": 344, + "Full House": 68, + "Straight": 2, + "Four of a Kind": 2 + }, + "pair_or_high_with_raw_straight_available": 59, + "affected_samples": 45, + "total_plays": 956, + "plays_including_debuffed_card": 48, + "losses_with_discards_left": 58, + "caps": [], + "groups": { + "3840": [ + { + "index": 0, + "reward": 28.67, + "ante": 2 + }, + { + "index": 1, + "reward": 0.69, + "ante": 1 + }, + { + "index": 2, + "reward": 0.52, + "ante": 1 + }, + { + "index": 3, + "reward": 20.67, + "ante": 2 + } + ], + "3841": [ + { + "index": 4, + "reward": 0.71, + "ante": 1 + }, + { + "index": 5, + "reward": 28.57, + "ante": 2 + }, + { + "index": 6, + "reward": 28.66, + "ante": 2 + }, + { + "index": 7, + "reward": 28.66, + "ante": 2 + } + ], + "3842": [ + { + "index": 8, + "reward": 46.17, + "ante": 3 + }, + { + "index": 9, + "reward": 48.17, + "ante": 3 + }, + { + "index": 10, + "reward": 48.17, + "ante": 3 + }, + { + "index": 11, + "reward": 40.4, + "ante": 3 + } + ], + "3843": [ + { + "index": 12, + "reward": 8.81, + "ante": 1 + }, + { + "index": 13, + "reward": 20.31, + "ante": 2 + }, + { + "index": 14, + "reward": 8.56, + "ante": 1 + }, + { + "index": 15, + "reward": 8.56, + "ante": 1 + } + ], + "3844": [ + { + "index": 16, + "reward": 40.68, + "ante": 3 + }, + { + "index": 17, + "reward": 64.22, + "ante": 4 + }, + { + "index": 18, + "reward": 44.68, + "ante": 3 + }, + { + "index": 19, + "reward": 8.57, + "ante": 1 + } + ], + "3845": [ + { + "index": 20, + "reward": 60.24, + "ante": 3 + }, + { + "index": 21, + "reward": 0.48, + "ante": 1 + }, + { + "index": 22, + "reward": 0.76, + "ante": 1 + }, + { + "index": 23, + "reward": 64.34, + "ante": 3 + } + ], + "3846": [ + { + "index": 24, + "reward": 0.67, + "ante": 1 + }, + { + "index": 25, + "reward": 0.67, + "ante": 1 + }, + { + "index": 26, + "reward": 0.67, + "ante": 1 + }, + { + "index": 27, + "reward": 0.67, + "ante": 1 + } + ], + "3847": [ + { + "index": 28, + "reward": 4.69, + "ante": 1 + }, + { + "index": 29, + "reward": 0.52, + "ante": 1 + }, + { + "index": 30, + "reward": 68.66, + "ante": 4 + }, + { + "index": 31, + "reward": 4.69, + "ante": 1 + } + ], + "3848": [ + { + "index": 32, + "reward": 44.49, + "ante": 3 + }, + { + "index": 33, + "reward": 64.37, + "ante": 4 + }, + { + "index": 34, + "reward": 44.49, + "ante": 3 + }, + { + "index": 35, + "reward": 42.83, + "ante": 3 + } + ], + "3849": [ + { + "index": 36, + "reward": 20.72, + "ante": 2 + }, + { + "index": 37, + "reward": 4.43, + "ante": 1 + }, + { + "index": 38, + "reward": 4.52, + "ante": 1 + }, + { + "index": 39, + "reward": 4.52, + "ante": 1 + } + ], + "3850": [ + { + "index": 40, + "reward": 0.72, + "ante": 1 + }, + { + "index": 41, + "reward": 28.49, + "ante": 2 + }, + { + "index": 42, + "reward": 0.61, + "ante": 1 + }, + { + "index": 43, + "reward": 62.4, + "ante": 4 + } + ], + "3851": [ + { + "index": 44, + "reward": 28.58, + "ante": 2 + }, + { + "index": 45, + "reward": 26.61, + "ante": 2 + }, + { + "index": 46, + "reward": 28.77, + "ante": 2 + }, + { + "index": 47, + "reward": 26.61, + "ante": 2 + } + ], + "3852": [ + { + "index": 48, + "reward": 0.67, + "ante": 1 + }, + { + "index": 49, + "reward": 4.74, + "ante": 1 + }, + { + "index": 50, + "reward": 0.51, + "ante": 1 + }, + { + "index": 51, + "reward": 4.74, + "ante": 1 + } + ], + "3853": [ + { + "index": 52, + "reward": 20.65, + "ante": 2 + }, + { + "index": 53, + "reward": 20.73, + "ante": 2 + }, + { + "index": 54, + "reward": 20.73, + "ante": 2 + }, + { + "index": 55, + "reward": 20.65, + "ante": 2 + } + ], + "3854": [ + { + "index": 56, + "reward": 84.68, + "ante": 4 + }, + { + "index": 57, + "reward": 88.36, + "ante": 4 + }, + { + "index": 58, + "reward": 68.45, + "ante": 3 + }, + { + "index": 59, + "reward": 88.28, + "ante": 4 + } + ], + "3855": [ + { + "index": 60, + "reward": 48.23, + "ante": 3 + }, + { + "index": 61, + "reward": 0.52, + "ante": 1 + }, + { + "index": 62, + "reward": 28.6, + "ante": 2 + }, + { + "index": 63, + "reward": 28.39, + "ante": 2 + } + ] + }, + "opportunity_examples": [ + { + "sample": 0, + "turn": 11, + "ante": 1, + "blind": { + "type": "big", + "score": [ + 0, + 450 + ], + "hands": 4, + "discards": 4 + }, + "hand": [ + "S_Q", + "C_Q", + "H_J", + "D_T", + "H_9", + "D_8", + "S_5", + "H_4" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 2, + 3, + 5, + 6, + 7 + ] + } + }, + "commit": { + "score": 285, + "cards": [ + 2, + 3, + 5, + 6, + 7 + ], + "hand": "High Card" + }, + "straight": [ + 5, + 4, + 3, + 2, + 0 + ], + "jokers": [ + { + "id": "j_raised_fist", + "effect": "Adds double the rank of lowest ranked card held in hand to Mult", + "mod": { + "enhancement": "SOCIALIZED MULT" + }, + "sell": 2 + } + ], + "boss": [ + "The Hook", + 600, + "Discards 2 random cards per hand played" + ] + }, + { + "sample": 0, + "turn": 30, + "ante": 2, + "blind": { + "type": "big", + "score": [ + 0, + 1200 + ], + "hands": 4, + "discards": 4 + }, + "hand": [ + "S_A", + "D_A", + "C_T", + "D_9", + "C_8", + "D_8", + "S_7", + "S_6" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 5, + 4, + 2, + 1, + 3 + ] + } + }, + "commit": { + "score": 364, + "cards": [ + 5, + 4, + 2, + 1, + 3 + ], + "hand": "Pair" + }, + "straight": [ + 7, + 6, + 4, + 3, + 2 + ], + "jokers": [ + { + "id": "j_raised_fist", + "effect": "Adds double the rank of lowest ranked card held in hand to Mult", + "mod": { + "enhancement": "SOCIALIZED MULT" + }, + "sell": 2 + }, + { + "id": "j_shoot_the_moon", + "effect": "Each Queen held in hand gives +13 Mult", + "sell": 2 + } + ], + "boss": [ + "The Psychic", + 1600, + "Must play 5 cards" + ] + }, + { + "sample": 0, + "turn": 41, + "ante": 2, + "blind": { + "type": "boss", + "score": [ + 820, + 1600 + ], + "hands": 2, + "discards": 3, + "effect": "Must play 5 cards" + }, + "hand": [ + "H_A", + "D_A", + "D_K", + "H_Q", + "H_J", + "S_T", + "C_8", + "S_3" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 2, + 3, + 1, + 0, + 5 + ] + } + }, + "commit": { + "score": 256, + "cards": [ + 2, + 3, + 1, + 0, + 5 + ], + "hand": "Pair" + }, + "straight": [ + 5, + 4, + 3, + 2, + 0 + ], + "jokers": [ + { + "id": "j_raised_fist", + "effect": "Adds double the rank of lowest ranked card held in hand to Mult", + "mod": { + "enhancement": "SOCIALIZED MULT" + }, + "sell": 2 + }, + { + "id": "j_shoot_the_moon", + "effect": "Each Queen held in hand gives +13 Mult", + "sell": 2 + } + ], + "boss": [ + "The Psychic", + 1600, + "Must play 5 cards" + ] + } + ], + "examples": [ + { + "sample": 0, + "reward": 28.6725, + "ending": "game_over", + "ante": 2, + "last_before": { + "phase": "hand", + "ante": 2, + "round": 6, + "money": 3, + "blind": { + "type": "boss", + "score": [ + 1076, + 1600 + ], + "hands": 1, + "discards": 3, + "effect": "Must play 5 cards" + }, + "boss": [ + "The Psychic", + 1600, + "Must play 5 cards" + ], + "hand": [ + "S_A", + "S_J", + "H_J", + "C_8", + "S_7", + "C_5", + "D_5", + "S_3" + ], + "jokers": { + "slots": "2/5", + "items": [ + { + "id": "j_raised_fist", + "effect": "Adds double the rank of lowest ranked card held in hand to Mult", + "mod": { + "enhancement": "SOCIALIZED MULT" + }, + "sell": 2 + }, + { + "id": "j_shoot_the_moon", + "effect": "Each Queen held in hand gives +13 Mult", + "sell": 2 + } + ] + }, + "consumables": { + "slots": "2/2", + "items": [ + { + "id": "c_chariot", + "effect": "Enhances 1 selected card into a Steel Card", + "mod": { + "enhancement": "ENHANCE" + }, + "sell": 1 + }, + { + "id": "c_lovers", + "effect": "Enhances 1 selected card into a Wild Card", + "mod": { + "enhancement": "ENHANCE" + }, + "sell": 1 + } + ] + }, + "hand_values": { + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 1, + 40, + 4 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 1, + 30, + 3 + ], + "Two Pair": [ + 1, + 20, + 2 + ], + "Pair": [ + 1, + 10, + 2 + ], + "High Card": [ + 1, + 5, + 1 + ], + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ] + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 6, + 5, + 2, + 1, + 3 + ] + } + }, + "last_after": { + "phase": "GAME_OVER", + "blind": { + "type": "boss", + "score": [ + 1476, + 1600 + ], + "hands": 0, + "discards": 3, + "effect": "Must play 5 cards" + }, + "play_commit": { + "score": 400, + "cards": [ + 6, + 5, + 2, + 1, + 3 + ], + "hand": "Two Pair" + } + } + }, + { + "sample": 57, + "reward": 88.3648, + "ending": "game_over", + "ante": 4, + "last_before": { + "phase": "hand", + "ante": 4, + "round": 15, + "money": 50, + "blind": { + "type": "boss", + "score": [ + 7296, + 20000 + ], + "hands": 1, + "discards": 4, + "effect": "Extra large blind" + }, + "boss": [ + "The Wall", + 20000, + "Extra large blind" + ], + "hand": [ + "S_K", + "C_K", + "C_Q", + "H_J", + "C_J", + "H_7", + "D_4", + "C_3" + ], + "jokers": { + "slots": "5/5", + "items": [ + { + "id": "j_mad", + "effect": "+10 Mult if played hand contains a Two Pair", + "mod": { + "enhancement": "TYPE MULT" + }, + "sell": 1 + }, + { + "id": "j_arrowhead", + "effect": "Played cards with Spade suit give +50 Chips when scored", + "sell": 2 + }, + { + "id": "j_banner", + "effect": "+30 Chips for each remaining discard", + "mod": { + "enhancement": "DISCARD CHIPS" + }, + "sell": 2 + }, + { + "id": "j_golden", + "effect": "Earn $4 at end of round", + "mod": { + "enhancement": "BONUS DOLLARS" + }, + "sell": 2 + }, + { + "id": "j_pareidolia", + "effect": "All cards are considered face cards", + "mod": { + "edition": "HOLO", + "enhancement": "ALL FACE CARDS" + }, + "sell": 3 + } + ] + }, + "consumables": { + "slots": "1/2", + "items": [ + { + "id": "c_venus", + "effect": "(lvl.1) Level up Three of a Kind +2 Mult and +20 chips", + "mod": { + "enhancement": "HAND UPGRADE" + }, + "sell": 1 + } + ] + }, + "hand_values": { + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 1, + 30, + 3 + ], + "Two Pair": [ + 3, + 60, + 4 + ], + "Pair": [ + 5, + 70, + 6 + ], + "High Card": [ + 1, + 5, + 1 + ], + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ], + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 2, + 65, + 6 + ], + "Flush": [ + 1, + 35, + 4 + ] + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 4, + 3, + 1, + 2, + 5 + ] + } + }, + "last_after": { + "phase": "GAME_OVER", + "blind": { + "type": "boss", + "score": [ + 10656, + 20000 + ], + "hands": 0, + "discards": 4, + "effect": "Extra large blind" + }, + "play_commit": { + "score": 3360, + "hand": "Pair", + "cards": [ + 4, + 3, + 1, + 2, + 5 + ] + } + } + } + ], + "error_count": 5 + }, + "449": { + "rollout": 449, + "episodes": 64, + "reward": 46.390621725063134, + "endings": { + "game_over": 62, + "context_cap": 2 + }, + "antes": { + "4": 19, + "5": 7, + "1": 14, + "6": 2, + "3": 12, + "2": 10 + }, + "actions": { + "select": 539, + "play": 1308, + "discard": 539, + "buy": 602, + "pack": 248, + "next_round": 475, + "use": 134, + "reroll": 1 + }, + "played_hands": { + "Pair": 503, + "Two Pair": 533, + "Three of a Kind": 129, + "Full House": 92, + "High Card": 39, + "Four of a Kind": 8, + "Straight": 4 + }, + "pair_or_high_with_raw_straight_available": 74, + "affected_samples": 41, + "total_plays": 1308, + "plays_including_debuffed_card": 121, + "losses_with_discards_left": 53, + "caps": [ + { + "sample": 8, + "ante": 6, + "turns": 125, + "reward": 104.79385, + "tokens": 48937, + "phase": "hand", + "remaining": { + "type": "boss", + "score": [ + 31754, + 40000 + ], + "hands": 1, + "discards": 3, + "effect": "No repeat hand types this round" + } + }, + { + "sample": 9, + "ante": 6, + "turns": 132, + "reward": 128.247125, + "tokens": 48456, + "phase": "hand", + "remaining": { + "type": "boss", + "score": [ + 9885, + 40000 + ], + "hands": 3, + "discards": 4, + "effect": "All Heart cards are debuffed" + } + } + ], + "groups": { + "3984": [ + { + "index": 0, + "reward": 82.52, + "ante": 4 + }, + { + "index": 1, + "reward": 60.69, + "ante": 4 + }, + { + "index": 2, + "reward": 84.55, + "ante": 4 + }, + { + "index": 3, + "reward": 60.6, + "ante": 4 + } + ], + "3985": [ + { + "index": 4, + "reward": 64.43, + "ante": 4 + }, + { + "index": 5, + "reward": 84.38, + "ante": 5 + }, + { + "index": 6, + "reward": 64.25, + "ante": 4 + }, + { + "index": 7, + "reward": 0.78, + "ante": 1 + } + ], + "3986": [ + { + "index": 8, + "reward": 104.79, + "ante": 6 + }, + { + "index": 9, + "reward": 128.25, + "ante": 6 + }, + { + "index": 10, + "reward": 0.91, + "ante": 1 + }, + { + "index": 11, + "reward": 0.91, + "ante": 1 + } + ], + "3987": [ + { + "index": 12, + "reward": 68.45, + "ante": 4 + }, + { + "index": 13, + "reward": 88.17, + "ante": 5 + }, + { + "index": 14, + "reward": 88.58, + "ante": 5 + }, + { + "index": 15, + "reward": 0.49, + "ante": 1 + } + ], + "3988": [ + { + "index": 16, + "reward": 84.65, + "ante": 5 + }, + { + "index": 17, + "reward": 0.72, + "ante": 1 + }, + { + "index": 18, + "reward": 88.51, + "ante": 5 + }, + { + "index": 19, + "reward": 48.56, + "ante": 3 + } + ], + "3989": [ + { + "index": 20, + "reward": 26.39, + "ante": 2 + }, + { + "index": 21, + "reward": 40.12, + "ante": 3 + }, + { + "index": 22, + "reward": 28.2, + "ante": 2 + }, + { + "index": 23, + "reward": 40.18, + "ante": 3 + } + ], + "3990": [ + { + "index": 24, + "reward": 40.62, + "ante": 3 + }, + { + "index": 25, + "reward": 20.68, + "ante": 2 + }, + { + "index": 26, + "reward": 48.62, + "ante": 3 + }, + { + "index": 27, + "reward": 20.63, + "ante": 2 + } + ], + "3991": [ + { + "index": 28, + "reward": 64.51, + "ante": 4 + }, + { + "index": 29, + "reward": 66.3, + "ante": 4 + }, + { + "index": 30, + "reward": 68.34, + "ante": 4 + }, + { + "index": 31, + "reward": 80.55, + "ante": 5 + } + ], + "3992": [ + { + "index": 32, + "reward": 4.46, + "ante": 1 + }, + { + "index": 33, + "reward": 48.45, + "ante": 3 + }, + { + "index": 34, + "reward": 24.46, + "ante": 2 + }, + { + "index": 35, + "reward": 46.6, + "ante": 3 + } + ], + "3993": [ + { + "index": 36, + "reward": 48.22, + "ante": 3 + }, + { + "index": 37, + "reward": 48.22, + "ante": 3 + }, + { + "index": 38, + "reward": 48.29, + "ante": 3 + }, + { + "index": 39, + "reward": 46.32, + "ante": 3 + } + ], + "3994": [ + { + "index": 40, + "reward": 8.36, + "ante": 1 + }, + { + "index": 41, + "reward": 8.36, + "ante": 1 + }, + { + "index": 42, + "reward": 8.36, + "ante": 1 + }, + { + "index": 43, + "reward": 8.36, + "ante": 1 + } + ], + "3995": [ + { + "index": 44, + "reward": 68.41, + "ante": 4 + }, + { + "index": 45, + "reward": 68.35, + "ante": 4 + }, + { + "index": 46, + "reward": 68.44, + "ante": 4 + }, + { + "index": 47, + "reward": 68.43, + "ante": 4 + } + ], + "3996": [ + { + "index": 48, + "reward": 20.49, + "ante": 2 + }, + { + "index": 49, + "reward": 64.56, + "ante": 4 + }, + { + "index": 50, + "reward": 64.88, + "ante": 4 + }, + { + "index": 51, + "reward": 68.88, + "ante": 4 + } + ], + "3997": [ + { + "index": 52, + "reward": 0.79, + "ante": 1 + }, + { + "index": 53, + "reward": 4.65, + "ante": 1 + }, + { + "index": 54, + "reward": 4.65, + "ante": 1 + }, + { + "index": 55, + "reward": 60.77, + "ante": 4 + } + ], + "3998": [ + { + "index": 56, + "reward": 80.27, + "ante": 5 + }, + { + "index": 57, + "reward": 68.71, + "ante": 4 + }, + { + "index": 58, + "reward": 44.21, + "ante": 3 + }, + { + "index": 59, + "reward": 0.55, + "ante": 1 + } + ], + "3999": [ + { + "index": 60, + "reward": 28.18, + "ante": 2 + }, + { + "index": 61, + "reward": 28.18, + "ante": 2 + }, + { + "index": 62, + "reward": 28.39, + "ante": 2 + }, + { + "index": 63, + "reward": 28.39, + "ante": 2 + } + ] + }, + "opportunity_examples": [ + { + "sample": 0, + "turn": 81, + "ante": 4, + "blind": { + "type": "big", + "score": [ + 0, + 7500 + ], + "hands": 3, + "discards": 5 + }, + "hand": [ + "S_K", + "H_K", + "H_T", + "H_9", + "S_6", + "S_5", + "D_4", + "H_3", + "S_2" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 5, + 4, + 1, + 0, + 6 + ] + } + }, + "commit": { + "cards": [ + 5, + 4, + 1, + 0, + 6 + ], + "score": 1260, + "hand": "Pair" + }, + "straight": [ + 8, + 7, + 6, + 5, + 4 + ], + "jokers": [ + { + "id": "j_abstract", + "effect": "+3 Mult for each Joker card (Currently +15 Mult)", + "mod": { + "enhancement": "JOKER MULT" + }, + "sell": 2 + }, + { + "id": "j_8_ball", + "effect": "1 in 4 chance for each played 8 to create a Tarot card when scored (Must have room)", + "mod": { + "edition": "HOLO", + "enhancement": "SPAWN TAROT" + }, + "sell": 4 + }, + { + "id": "j_riff_raff", + "effect": "When Blind is selected, create 2 Common Jokers (Must have room)", + "sell": 3 + }, + { + "id": "j_juggler", + "effect": "+1 hand size", + "mod": { + "enhancement": "HAND SIZE" + }, + "sell": 2 + }, + { + "id": "j_business", + "effect": "Played face cards have a 1 in 2 chance to give $2 when scored", + "mod": { + "enhancement": "FACE DOLLAR CHANCE" + }, + "sell": 2 + } + ], + "boss": [ + "The Eye", + 10000, + "No repeat hand types this round" + ] + }, + { + "sample": 2, + "turn": 83, + "ante": 4, + "blind": { + "type": "big", + "score": [ + 0, + 7500 + ], + "hands": 3, + "discards": 5 + }, + "hand": [ + "S_K", + "H_K", + "H_T", + "H_9", + "S_6", + "S_5", + "D_4", + "H_3", + "S_2" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 3, + 4, + 1, + 0, + 5 + ] + } + }, + "commit": { + "cards": [ + 3, + 4, + 1, + 0, + 5 + ], + "score": 2160, + "hand": "Pair" + }, + "straight": [ + 8, + 7, + 6, + 5, + 4 + ], + "jokers": [ + { + "id": "j_abstract", + "effect": "+3 Mult for each Joker card (Currently +15 Mult)", + "mod": { + "enhancement": "JOKER MULT" + }, + "sell": 2 + }, + { + "id": "j_8_ball", + "effect": "1 in 4 chance for each played 8 to create a Tarot card when scored (Must have room)", + "mod": { + "edition": "HOLO", + "enhancement": "SPAWN TAROT" + }, + "sell": 4 + }, + { + "id": "j_riff_raff", + "effect": "When Blind is selected, create 2 Common Jokers (Must have room)", + "sell": 3 + }, + { + "id": "j_juggler", + "effect": "+1 hand size", + "mod": { + "enhancement": "HAND SIZE" + }, + "sell": 2 + }, + { + "id": "j_supernova", + "effect": "Adds the number of times poker hand has been played this run to Mult", + "mod": { + "enhancement": "HAND PLAYED MULT" + }, + "sell": 2 + } + ], + "boss": [ + "The Eye", + 10000, + "No repeat hand types this round" + ] + }, + { + "sample": 3, + "turn": 51, + "ante": 3, + "blind": { + "type": "boss", + "score": [ + 3672, + 4000 + ], + "hands": 1, + "discards": 4, + "effect": "Decrease level of played poker hand" + }, + "hand": [ + "H_A", + "H_T", + "D_9", + "S_8", + "H_8", + "C_7", + "S_5", + "S_4", + "D_3", + "D_2" + ], + "action": { + "method": "play", + "params": { + "cards": [ + 4, + 3, + 5, + 6, + 7 + ] + } + }, + "commit": { + "hand": "Pair", + "score": 1312, + "cards": [ + 4, + 3, + 5, + 6, + 7 + ] + }, + "straight": [ + 0, + 9, + 8, + 7, + 6 + ], + "jokers": [ + { + "id": "j_abstract", + "effect": "+3 Mult for each Joker card (Currently +15 Mult)", + "mod": { + "enhancement": "JOKER MULT" + }, + "sell": 2 + }, + { + "id": "j_8_ball", + "effect": "1 in 4 chance for each played 8 to create a Tarot card when scored (Must have room)", + "mod": { + "enhancement": "SPAWN TAROT", + "edition": "HOLO" + }, + "sell": 4 + }, + { + "id": "j_popcorn", + "effect": "+4 Mult -4 Mult per round played", + "sell": 2 + }, + { + "id": "j_gluttenous_joker", + "effect": "Played cards with Club suit give +3 Mult when scored", + "mod": { + "enhancement": "SUIT MULT" + }, + "sell": 2 + }, + { + "id": "j_troubadour", + "effect": "+2 hand size, -1 hand each round", + "mod": { + "enhancement": "HAND SIZE, PLAYS" + }, + "sell": 3 + } + ], + "boss": [ + "The Arm", + 4000, + "Decrease level of played poker hand" + ] + } + ], + "examples": [ + { + "sample": 0, + "reward": 82.52373333333333, + "ending": "game_over", + "ante": 4, + "last_before": { + "phase": "hand", + "ante": 4, + "round": 14, + "money": 9, + "blind": { + "type": "big", + "score": [ + 3928, + 7500 + ], + "hands": 1, + "discards": 5 + }, + "boss": [ + "The Eye", + 10000, + "No repeat hand types this round" + ], + "hand": [ + "D_A", + "S_Q", + "H_Q", + "D_T", + "D_5", + "H_3", + "D_3", + "S_2", + "H_2" + ], + "jokers": { + "slots": "5/5", + "items": [ + { + "id": "j_abstract", + "effect": "+3 Mult for each Joker card (Currently +15 Mult)", + "mod": { + "enhancement": "JOKER MULT" + }, + "sell": 2 + }, + { + "id": "j_8_ball", + "effect": "1 in 4 chance for each played 8 to create a Tarot card when scored (Must have room)", + "mod": { + "edition": "HOLO", + "enhancement": "SPAWN TAROT" + }, + "sell": 4 + }, + { + "id": "j_riff_raff", + "effect": "When Blind is selected, create 2 Common Jokers (Must have room)", + "sell": 3 + }, + { + "id": "j_juggler", + "effect": "+1 hand size", + "mod": { + "enhancement": "HAND SIZE" + }, + "sell": 2 + }, + { + "id": "j_business", + "effect": "Played face cards have a 1 in 2 chance to give $2 when scored", + "mod": { + "enhancement": "FACE DOLLAR CHANCE" + }, + "sell": 2 + } + ] + }, + "consumables": { + "slots": "2/2", + "items": [ + { + "id": "c_devil", + "effect": "Enhances 1 selected card into a Gold Card", + "mod": { + "enhancement": "ENHANCE" + }, + "sell": 1 + }, + { + "id": "c_death", + "effect": "Select 2 cards, convert the left card into the right card (Drag to rearrange)", + "mod": { + "enhancement": "CARD CONVERSION" + }, + "sell": 1 + } + ] + }, + "hand_values": { + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 1, + 40, + 4 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 3, + 70, + 7 + ], + "Two Pair": [ + 3, + 60, + 4 + ], + "Pair": [ + 2, + 25, + 3 + ], + "High Card": [ + 1, + 5, + 1 + ], + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ] + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 6, + 5, + 7, + 3, + 2 + ] + } + }, + "last_after": { + "phase": "GAME_OVER", + "blind": { + "type": "big", + "score": [ + 4796, + 7500 + ], + "hands": 0, + "discards": 5 + }, + "play_commit": { + "cards": [ + 6, + 5, + 7, + 3, + 2 + ], + "score": 868, + "hand": "Pair" + } + } + }, + { + "sample": 9, + "reward": 128.247125, + "ending": "context_cap", + "ante": 6, + "last_before": { + "phase": "hand", + "ante": 6, + "round": 21, + "money": 36, + "blind": { + "type": "boss", + "score": [ + 2325, + 40000 + ], + "hands": 4, + "discards": 4, + "effect": "All Heart cards are debuffed" + }, + "boss": [ + "The Head", + 40000, + "All Heart cards are debuffed" + ], + "hand": [ + "S_A", + { + "id": "H_A", + "debuff": true + }, + "C_A", + "C_K", + "D_K", + "S_J", + { + "id": "H_J", + "debuff": true + }, + "D_2" + ], + "jokers": { + "slots": "5/5", + "items": [ + { + "id": "j_raised_fist", + "effect": "Adds double the rank of lowest ranked card held in hand to Mult", + "mod": { + "enhancement": "SOCIALIZED MULT", + "edition": "HOLO" + }, + "sell": 4 + }, + { + "id": "j_mad", + "effect": "+10 Mult if played hand contains a Two Pair", + "mod": { + "enhancement": "TYPE MULT" + }, + "sell": 2 + }, + { + "id": "j_loyalty_card", + "effect": "X4 Mult every 6 hands played 1 remaining", + "mod": { + "enhancement": "1 IN 10 MULT", + "edition": "FOIL" + }, + "sell": 3 + }, + { + "id": "j_wrathful_joker", + "effect": "Played cards with Spade suit give +3 Mult when scored", + "mod": { + "enhancement": "SUIT MULT" + }, + "sell": 2 + }, + { + "id": "j_abstract", + "effect": "+3 Mult for each Joker card (Currently +15 Mult)", + "mod": { + "enhancement": "JOKER MULT" + }, + "sell": 2 + } + ] + }, + "consumables": { + "slots": "3/3", + "items": [ + { + "id": "c_justice", + "effect": "Enhances 1 selected card into a Glass Card", + "mod": { + "enhancement": "ENHANCE" + }, + "sell": 1 + }, + { + "id": "c_devil", + "effect": "Enhances 1 selected card into a Gold Card", + "mod": { + "enhancement": "ENHANCE" + }, + "sell": 1 + }, + { + "id": "c_magician", + "effect": "Enhances 2 selected cards to Lucky Cards", + "mod": { + "enhancement": "ENHANCE" + }, + "sell": 1 + } + ] + }, + "hand_values": { + "Straight Flush": [ + 2, + 140, + 12 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 5, + 140, + 12 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 3, + 70, + 7 + ], + "Two Pair": [ + 2, + 40, + 3 + ], + "Pair": [ + 3, + 40, + 4 + ], + "High Card": [ + 2, + 15, + 2 + ], + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ], + "Five of a Kind": [ + 1, + 120, + 12 + ] + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 4, + 3, + 6, + 5, + 7 + ] + } + }, + "last_after": { + "phase": "hand", + "blind": { + "type": "boss", + "score": [ + 9885, + 40000 + ], + "hands": 3, + "discards": 4, + "effect": "All Heart cards are debuffed" + }, + "play_commit": { + "hand": "Two Pair", + "score": 7560, + "cards": [ + 4, + 3, + 6, + 5, + 7 + ] + } + } + } + ], + "error_count": 9 + } + }, + "optimizer_metrics_steps_425_449": [ + { + "train/loss": -0.21641286656580772, + "train/pg_loss": -0.21676085531407555, + "train/entropy_loss": 0.026850530799451603, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.37483927906728187, + "train/grad_norm": 1.556913919042798, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 425 + }, + { + "train/loss": -0.25290070317928504, + "train/pg_loss": -0.2532545805474411, + "train/entropy_loss": 0.026693489603644052, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.3805470976793488, + "train/grad_norm": 1.4719941854310257, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 426 + }, + { + "train/loss": -0.09779988420306737, + "train/pg_loss": -0.09818589899198055, + "train/entropy_loss": 0.024846588711212557, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.410858464691377, + "train/grad_norm": 1.3478951505017798, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 427 + }, + { + "train/loss": -0.2005713855381584, + "train/pg_loss": -0.2009324411389266, + "train/entropy_loss": 0.023568182585306324, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.3846095476680247, + "train/grad_norm": 1.132750147714902, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 428 + }, + { + "train/loss": -0.2632190098633299, + "train/pg_loss": -0.2635800620884814, + "train/entropy_loss": 0.02376489410876715, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.38480287276042074, + "train/grad_norm": 1.3758376777683539, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 429 + }, + { + "train/loss": -0.189753767478996, + "train/pg_loss": -0.19009753564223683, + "train/entropy_loss": 0.023164339633575354, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.3669177902889512, + "train/grad_norm": 1.211287269530679, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 430 + }, + { + "train/loss": -0.17889164902430044, + "train/pg_loss": -0.1792404731222386, + "train/entropy_loss": 0.022999692217415317, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.37180209867452135, + "train/grad_norm": 1.5390977419773382, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 431 + }, + { + "train/loss": -0.1417064817028738, + "train/pg_loss": -0.14206255764107117, + "train/entropy_loss": 0.023387909109281776, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.37945472969895705, + "train/grad_norm": 1.2984481370400687, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 432 + }, + { + "train/loss": -0.1428134509899688, + "train/pg_loss": -0.14316571841617512, + "train/entropy_loss": 0.02459065635967693, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.37683275921716697, + "train/grad_norm": 1.6206478378858158, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 433 + }, + { + "train/loss": -0.19920421873860295, + "train/pg_loss": -0.19956531583554182, + "train/entropy_loss": 0.026840993880344464, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.3879405631519123, + "train/grad_norm": 1.4545304259694263, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 434 + }, + { + "train/loss": -0.17250839789225073, + "train/pg_loss": -0.17285668573581572, + "train/entropy_loss": 0.025975256667529232, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.3742617272316552, + "train/grad_norm": 1.4612392990986611, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 435 + }, + { + "train/loss": -0.2046804181032993, + "train/pg_loss": -0.20501417527025975, + "train/entropy_loss": 0.029466365412637613, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.36323230457475675, + "train/grad_norm": 1.7085359344677757, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 436 + }, + { + "train/loss": -0.2769795887647989, + "train/pg_loss": -0.27733529720043276, + "train/entropy_loss": 0.03131602225719658, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.38705226983676994, + "train/grad_norm": 1.6940604653809797, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 437 + }, + { + "train/loss": -0.21127858486526344, + "train/pg_loss": -0.2116431410062651, + "train/entropy_loss": 0.03200130891719563, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.3965430174099619, + "train/grad_norm": 1.8807736829843311, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 438 + }, + { + "train/loss": -0.19637107101496615, + "train/pg_loss": -0.19673552752702222, + "train/entropy_loss": 0.035695656077399336, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.40016055706268133, + "train/grad_norm": 1.5856371064225763, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 439 + }, + { + "train/loss": -0.33900399780766743, + "train/pg_loss": -0.33935344290505953, + "train/entropy_loss": 0.037306210830156526, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.3867309839986664, + "train/grad_norm": 2.1157921278513134, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 440 + }, + { + "train/loss": -0.2976195042988649, + "train/pg_loss": -0.29795051069450096, + "train/entropy_loss": 0.03832477462661858, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.36929096723855737, + "train/grad_norm": 1.7121164101123858, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 441 + }, + { + "train/loss": -0.31404900958893645, + "train/pg_loss": -0.3143895102929619, + "train/entropy_loss": 0.04131917257305629, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.3818241269947389, + "train/grad_norm": 1.5922104374334893, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 442 + }, + { + "train/loss": -0.23800103892900312, + "train/pg_loss": -0.23833649810099633, + "train/entropy_loss": 0.042505249016730466, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.37794794852658375, + "train/grad_norm": 1.7267299242963767, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 443 + }, + { + "train/loss": -0.27410861749795423, + "train/pg_loss": -0.27445419467147675, + "train/entropy_loss": 0.04275795377148075, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.3883337044438457, + "train/grad_norm": 1.6521907943020282, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 444 + }, + { + "train/loss": -0.23894212135518872, + "train/pg_loss": -0.23928473406332756, + "train/entropy_loss": 0.04330627421638343, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.3859352525052868, + "train/grad_norm": 1.5906632394447295, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 445 + }, + { + "train/loss": -0.2720324010924771, + "train/pg_loss": -0.27236221069470984, + "train/entropy_loss": 0.044016578088856485, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.37381386402494216, + "train/grad_norm": 1.641423276425294, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 446 + }, + { + "train/loss": -0.42918934163696715, + "train/pg_loss": -0.4295173803532614, + "train/entropy_loss": 0.04166895825888049, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.36969821814968573, + "train/grad_norm": 1.4649065741736327, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 447 + }, + { + "train/loss": -0.2181644219873482, + "train/pg_loss": -0.21850880750722468, + "train/entropy_loss": 0.04093448836954278, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.38532232643547665, + "train/grad_norm": 1.3982791917585196, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 448 + }, + { + "train/loss": -0.31235448297694496, + "train/pg_loss": -0.31269035147088176, + "train/entropy_loss": 0.03963022837887993, + "train/pg_clipfrac": 0.0, + "train/ppo_kl": 0.0, + "train/kl_loss": 0.37553762350915715, + "train/grad_norm": 1.3689244163552206, + "train/lr-pg_0": 1e-06, + "train/lr-pg_1": 1e-06, + "train/global_batch_size": 64, + "train/step": 449 + } + ], + "failure_log_excerpts": [ + "2026-09-15 03:51:53-04:00 (MegatronTrainRayActor pid=15329) [2026-09-15 07:51:52.360146] saving checkpoint at iteration 449 to /checkpoints/broad-layover-3a0ce678e7da in torch_dist format", + "2026-09-15 03:52:47-04:00 (MegatronTrainRayActor pid=15329) [2026-09-15 07:52:45.419580] successfully saved checkpoint from iteration 449 to /checkpoints/broad-layover-3a0ce678e7da [ t 1/4, p 1/2 ]", + "2026-09-15 03:54:04-04:00 (MegatronTrainRayActor pid=15797) File \"ras/peers.cc\", line 549, in rasMsgHandlePeersUpdate(rasMsg*, rasSocket*)", + "2026-09-15 03:54:05-04:00 (MegatronTrainRayActor pid=4714, ip=10.100.0.3) File \"ras/peers.cc\", line 549, in rasMsgHandlePeersUpdate(rasMsg*, rasSocket*)", + "2026-09-15 03:54:14-04:00 Ray job raysubmit_cMPrxHRXqyvPZ24E finished with status: FAILED: Job entrypoint command failed with exit code 1, last available logs (truncated to 20,000 chars):", + "2026-09-15 03:54:14-04:00 (MegatronTrainRayActor pid=4714, ip=10.100.0.3) File \"ras/peers.cc\", line 549, in rasMsgHandlePeersUpdate(rasMsg*, rasSocket*)", + "2026-09-15 03:54:21-04:00 (MegatronTrainRayActor pid=4714, ip=10.100.0.3) File \"ras/peers.cc\", line 549, in rasMsgHandlePeersUpdate(rasMsg*, rasSocket*)", + "2026-09-15 03:57:24-04:00 WARNING: training run broad-layover-3a0ce678e7da is retrying after preemption or interruption (attempt 2).", + "2026-09-15 04:06:10-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__32_1.distcp'", + "2026-09-15 04:06:10-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__33_1.distcp'", + "2026-09-15 04:06:10-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__34_1.distcp'", + "2026-09-15 04:06:11-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__35_1.distcp'", + "2026-09-15 04:06:16-04:00 Ray job raysubmit_LadGCU76J5BKm2jj finished with status: FAILED: Job entrypoint command failed with exit code 1, last available logs (truncated to 20,000 chars):", + "2026-09-15 04:06:16-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__35_1.distcp'", + "2026-09-15 04:06:21-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__35_1.distcp'", + "2026-09-15 04:07:14-04:00 WARNING: training run broad-layover-3a0ce678e7da is retrying after preemption or interruption (attempt 3).", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__52_1.distcp'", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__52_1.distcp'", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__52_1.distcp'", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__48_1.distcp'", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__48_1.distcp'", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__48_1.distcp'", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__48_1.distcp'", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:17:28-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:17:29-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:17:32-04:00 Ray job raysubmit_K5cNJyjjLwfUnixe finished with status: FAILED: Job entrypoint command failed with exit code 1, last available logs (truncated to 20,000 chars):", + "2026-09-15 04:17:32-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:17:38-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:18:34-04:00 WARNING: training run broad-layover-3a0ce678e7da is retrying after preemption or interruption (attempt 4).", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__52_1.distcp'", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__52_1.distcp'", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__52_1.distcp'", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__48_1.distcp'", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__48_1.distcp'", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__48_1.distcp'", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__48_1.distcp'", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 OSError: [Errno 22] Invalid argument", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:28:44-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:28:45-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:28:50-04:00 Ray job raysubmit_HSqKgAqbi6tykF1S finished with status: FAILED: Job entrypoint command failed with exit code 1, last available logs (truncated to 20,000 chars):", + "2026-09-15 04:28:50-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'", + "2026-09-15 04:28:55-04:00 FileNotFoundError: [Errno 2] No such file or directory: '/checkpoints/broad-layover-3a0ce678e7da/iter_0000449/__44_1.distcp'" + ], + "checkpoint_metadata_expected_shards": 128, + "checkpoint_present_shards": 112, + "missing_shards": [ + "__40_1.distcp", + "__41_1.distcp", + "__42_1.distcp", + "__43_1.distcp", + "__44_1.distcp", + "__45_1.distcp", + "__46_1.distcp", + "__47_1.distcp", + "__48_1.distcp", + "__49_1.distcp", + "__50_1.distcp", + "__51_1.distcp", + "__52_1.distcp", + "__53_1.distcp", + "__54_1.distcp", + "__55_1.distcp" + ], + "avoidable_loss_examples": [ + { + "batch": 440, + "sample_position": 4, + "sample_index": 15364, + "group_index": 3841, + "seed": "BALATRO-RL-00000255", + "action_number": 7, + "before": { + "phase": "hand", + "ante": 1, + "round": 1, + "money": 4, + "blinds": { + "small": { + "status": "CURRENT", + "type": "small", + "name": "Small Blind", + "score": 300, + "tag": "Economy Tag", + "tag_effect": "Doubles your money (Max of $40)" + }, + "big": { + "status": "UPCOMING", + "type": "big", + "name": "Big Blind", + "score": 450, + "tag": "Economy Tag", + "tag_effect": "Doubles your money (Max of $40)" + }, + "boss": { + "status": "UPCOMING", + "type": "boss", + "name": "The Manacle", + "score": 600, + "effect": "-1 Hand Size" + } + }, + "boss": [ + "The Manacle", + 600, + "-1 Hand Size" + ], + "jokers": { + "slots": "0/5" + }, + "consumables": { + "slots": "0/2" + }, + "deck": [ + "C_2", + "C_8", + "C_9", + "C_A", + "C_Q", + "D_2", + "D_3", + "D_5", + "D_A", + "D_T", + "H_3", + "H_5", + "H_6", + "H_J", + "S_2", + "S_5", + "S_7", + "S_A", + "S_J", + "S_K", + "S_T" + ], + "blind": { + "type": "small", + "score": [ + 212, + 300 + ], + "hands": 1, + "discards": 2 + }, + "deck_left": 21, + "hand": [ + "H_A", + "H_K", + "C_K", + "D_K", + "C_J", + "S_9", + "S_3", + "H_2" + ], + "hand_values": { + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 1, + 40, + 4 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 1, + 30, + 3 + ], + "Two Pair": [ + 1, + 20, + 2 + ], + "Pair": [ + 1, + 10, + 2 + ], + "High Card": [ + 1, + 5, + 1 + ], + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ] + }, + "discard": [ + "C_3", + "C_4", + "C_5", + "C_6", + "C_7", + "C_T", + "D_4", + "D_6", + "D_7", + "D_8", + "D_9", + "D_J", + "D_Q", + "H_4", + "H_7", + "H_8", + "H_9", + "H_Q", + "H_T", + "S_4", + "S_6", + "S_8", + "S_Q" + ], + "hand_stats": { + "Two Pair": [ + 1, + 1, + 1 + ], + "Pair": [ + 1, + 2, + 2 + ] + }, + "play_commit": { + "score": 44, + "hand": "Pair", + "cards": [ + 6, + 7, + 5, + 4, + 3 + ] + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 4, + 3, + 2, + 6, + 5 + ] + } + }, + "after": { + "phase": "GAME_OVER", + "ante": 1, + "round": 1, + "money": 4, + "blinds": { + "small": { + "status": "CURRENT", + "type": "small", + "name": "Small Blind", + "score": 300, + "tag": "Economy Tag", + "tag_effect": "Doubles your money (Max of $40)" + }, + "big": { + "status": "UPCOMING", + "type": "big", + "name": "Big Blind", + "score": 450, + "tag": "Economy Tag", + "tag_effect": "Doubles your money (Max of $40)" + }, + "boss": { + "status": "UPCOMING", + "type": "boss", + "name": "The Manacle", + "score": 600, + "effect": "-1 Hand Size" + } + }, + "boss": [ + "The Manacle", + 600, + "-1 Hand Size" + ], + "jokers": { + "slots": "0/5" + }, + "consumables": { + "slots": "0/2" + }, + "deck": [ + "C_2", + "C_8", + "C_9", + "C_A", + "C_Q", + "D_2", + "D_3", + "D_5", + "D_A", + "D_T", + "H_3", + "H_5", + "H_6", + "H_J", + "S_2", + "S_5", + "S_7", + "S_A", + "S_J", + "S_K", + "S_T" + ], + "blind": { + "type": "small", + "score": [ + 272, + 300 + ], + "hands": 0, + "discards": 2 + }, + "discard": [ + "C_3", + "C_4", + "C_5", + "C_6", + "C_7", + "C_J", + "C_K", + "C_T", + "D_4", + "D_6", + "D_7", + "D_8", + "D_9", + "D_J", + "D_K", + "D_Q", + "H_4", + "H_7", + "H_8", + "H_9", + "H_Q", + "H_T", + "S_3", + "S_4", + "S_6", + "S_8", + "S_9", + "S_Q" + ], + "play_commit": { + "score": 60, + "hand": "Pair", + "cards": [ + 4, + 3, + 2, + 6, + 5 + ] + } + } + }, + { + "batch": 449, + "sample_position": 7, + "sample_index": 15943, + "group_index": 3985, + "seed": "BALATRO-RL-00000155", + "action_number": 8, + "before": { + "phase": "hand", + "ante": 1, + "round": 1, + "money": 4, + "blinds": { + "small": { + "status": "CURRENT", + "type": "small", + "name": "Small Blind", + "score": 300, + "tag": "Polychrome Tag", + "tag_effect": "Next base edition shop Joker is free and becomes Polychrome" + }, + "big": { + "status": "UPCOMING", + "type": "big", + "name": "Big Blind", + "score": 450, + "tag": "Uncommon Tag", + "tag_effect": "Shop has a free Uncommon Joker" + }, + "boss": { + "status": "UPCOMING", + "type": "boss", + "name": "The Pillar", + "score": 600, + "effect": "Cards played previously this Ante are debuffed" + } + }, + "boss": [ + "The Pillar", + 600, + "Cards played previously this Ante are debuffed" + ], + "jokers": { + "slots": "0/5" + }, + "consumables": { + "slots": "0/2" + }, + "deck": [ + "C_3", + "C_4", + "C_8", + "C_9", + "C_A", + "C_Q", + "C_T", + "D_9", + "D_A", + "D_J", + "H_2", + "H_9", + "H_Q", + "S_2", + "S_7", + "S_8", + "S_K" + ], + "blind": { + "type": "small", + "score": [ + 235, + 300 + ], + "hands": 1, + "discards": 1 + }, + "deck_left": 17, + "hand": [ + "S_A", + "S_T", + "H_8", + "D_8", + "C_6", + "C_5", + "S_3", + "D_3" + ], + "hand_values": { + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ], + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 1, + 40, + 4 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 1, + 30, + 3 + ], + "Two Pair": [ + 1, + 20, + 2 + ], + "Pair": [ + 1, + 10, + 2 + ], + "High Card": [ + 1, + 5, + 1 + ] + }, + "discard": [ + "C_2", + "C_7", + "C_J", + "C_K", + "D_2", + "D_4", + "D_5", + "D_6", + "D_7", + "D_K", + "D_Q", + "D_T", + "H_3", + "H_4", + "H_5", + "H_6", + "H_7", + "H_A", + "H_J", + "H_K", + "H_T", + "S_4", + "S_5", + "S_6", + "S_9", + "S_J", + "S_Q" + ], + "hand_stats": { + "Two Pair": [ + 1, + 2, + 2 + ], + "High Card": [ + 1, + 1, + 1 + ] + }, + "play_commit": { + "score": 120, + "cards": [ + 4, + 5, + 3, + 2, + 1 + ], + "hand": "Two Pair" + } + }, + "action": { + "method": "play", + "params": { + "cards": [ + 6, + 7, + 5, + 4, + 3 + ] + } + }, + "after": { + "phase": "GAME_OVER", + "ante": 1, + "round": 1, + "money": 4, + "blinds": { + "small": { + "status": "CURRENT", + "type": "small", + "name": "Small Blind", + "score": 300, + "tag": "Polychrome Tag", + "tag_effect": "Next base edition shop Joker is free and becomes Polychrome" + }, + "big": { + "status": "UPCOMING", + "type": "big", + "name": "Big Blind", + "score": 450, + "tag": "Uncommon Tag", + "tag_effect": "Shop has a free Uncommon Joker" + }, + "boss": { + "status": "UPCOMING", + "type": "boss", + "name": "The Pillar", + "score": 600, + "effect": "Cards played previously this Ante are debuffed" + } + }, + "boss": [ + "The Pillar", + 600, + "Cards played previously this Ante are debuffed" + ], + "jokers": { + "slots": "0/5" + }, + "consumables": { + "slots": "0/2" + }, + "deck": [ + "C_3", + "C_4", + "C_8", + "C_9", + "C_A", + "C_Q", + "C_T", + "D_9", + "D_A", + "D_J", + "H_2", + "H_9", + "H_Q", + "S_2", + "S_7", + "S_8", + "S_K" + ], + "blind": { + "type": "small", + "score": [ + 267, + 300 + ], + "hands": 0, + "discards": 1 + }, + "discard": [ + "C_2", + "C_5", + "C_6", + "C_7", + "C_J", + "C_K", + "D_2", + "D_3", + "D_4", + "D_5", + "D_6", + "D_7", + "D_8", + "D_K", + "D_Q", + "D_T", + "H_3", + "H_4", + "H_5", + "H_6", + "H_7", + "H_A", + "H_J", + "H_K", + "H_T", + "S_3", + "S_4", + "S_5", + "S_6", + "S_9", + "S_J", + "S_Q" + ], + "play_commit": { + "score": 32, + "cards": [ + 6, + 7, + 5, + 4, + 3 + ], + "hand": "Pair" + } + } + } + ] +} \ No newline at end of file diff --git a/reports/broad-layover-final-failure-audit-2026-09-15.md b/reports/broad-layover-final-failure-audit-2026-09-15.md new file mode 100644 index 00000000..e34475af --- /dev/null +++ b/reports/broad-layover-final-failure-audit-2026-09-15.md @@ -0,0 +1,111 @@ +# Broad-layover: final failure and learning audit + +Run: [broad-layover-3a0ce678e7da](https://modal-labs-joy-dev--training-gym-dashboard-fastapi-app.modal.run/training/broad-layover-3a0ce678e7da?tab=summary). Checked September 15, 2026. + +## Conclusion + +Two problems occurred independently: + +1. **The first attempt crashed in NCCL during checkpoint saving. Three automatic retries then loaded the same incomplete checkpoint and failed before another training update.** +2. **The policy already performed poorly and regressed before that crash.** It increasingly played weak combinations, left usable discards, and missed straightforward winning hands. The training setup supplies whole-game relative reward, offering weak guidance about which individual decisions were wrong. + +The crash is established by logs and checkpoint contents. The mechanism behind poor learning is an evidence-based diagnosis, not a controlled causal experiment. + +## Evidence inspected + +- Persisted run configuration, status, attempt history, and all 50 rollout summaries (400–449; 3,200 games). +- Full prompts and responses from batches 400, 413, 425, 440, and 449: 320 episodes, with sparse state reconstruction and 6,056 executed plays. +- Modal logs covering optimizer steps 425–449 and all four failure endings. +- Committed checkpoint directory and `.metadata` references. Metadata was inspected with `pickletools`, without executing the pickle. +- Local checkpoint validation and reward code, including the earlier committed harness for comparison. The currently edited harness has changes that this historical run did not use. + +The dashboard and normal CLI returned authentication errors. Reads used the authenticated Training Gym SDK and Modal CLI. No training configuration, code, running jobs, or remote data was changed. + +## 1. Why it failed four times + +All times below are EDT on September 15. + +| Attempt | Failure time | Evidence | +|---|---|---| +| 1 | 03:54 | Native segfault in `ras/peers.cc:549`, `rasMsgHandlePeersUpdate`, inside the NCCL RAS thread. Ray reports `ActorDiedError` while waiting for `actor_model.save_model(449)`. | +| 2 | 04:06 | Auto-resumes checkpoint 449; loading fails with missing `__32_1.distcp` through `__35_1.distcp`. | +| 3 | 04:17 | Same checkpoint; missing shards including `__44_1.distcp`, `__48_1.distcp`, `__52_1.distcp`, plus invalid-argument reads. | +| 4 | 04:28 | Same load failure. Persisted final error names `__44_1.distcp`. | + +Step 449's optimizer update completed at 03:51:41. Saving began at 03:51:52, and Megatron printed “successfully saved” at 03:52:45. The outer save operation had not returned when actors segfaulted. That message therefore did not establish a durable, reloadable checkpoint. + +The committed checkpoint now has **112 of 128 referenced `.distcp` files**. Every `__40_1.distcp` through `__55_1.distcp` is missing. Different missing files in the earlier retry logs indicate that its visible contents changed; they do not change the finding that the checkpoint was incomplete. We did not validate byte ranges in the remaining files, so there may also be truncated shards. + +### Why the retries did not recover + +`is_complete_torch_dist_checkpoint()` in Training Gym's `common/torch_dist_checkpoint.py` only checks for `.metadata`, `common.pt`, and **any** `.distcp` file. I passed the actual directory listing into that function: it returned `True` despite the 16 missing referenced files. The launcher uses this check when selecting a resume checkpoint. + +Thus automatic recovery accepted an invalid checkpoint and repeated the same deterministic failure three times. The exact underlying NCCL defect and the precise timing of failed volume persistence are not established by these logs. The native stack identifies the first crash; there is no need to infer an OOM from Ray's generic list of possible causes. + +## 2. Learning did not improve before the crash + +This continuation resumed from parent `perfect-adhesive-da14ace5a831` at 400. It completed **50 new updates**, not 450. Retries added none. + +| Batches | Games | Mean reward | Mean final ante | Wins | +|---|---:|---:|---:|---:| +| 400–409 | 640 | 51.89 | 3.26 | 0 | +| 410–419 | 640 | 48.31 | 3.11 | 0 | +| 420–429 | 640 | 49.42 | 3.17 | 0 | +| 430–439 | 640 | 50.62 | 3.18 | 0 | +| 440–449 | 640 | 39.13 | 2.67 | 0 | + +Last-window reward was **24.6% below** the first window. These are changing training seeds, not a fixed-seed evaluation, so the table alone cannot measure causal checkpoint regression. The decision errors below directly establish poor play. + +## 3. What the model actually did + +| Batch | Pair / Two Pair share | Discards per play | Game overs with discards left | Context cutoffs | +|---|---:|---:|---:|---:| +| 400 | 66.1% | 0.92 | 21 / 55 | 9 / 64 | +| 413 | 72.4% | 0.60 | 42 / 63 | 1 / 64 | +| 425 | 75.0% | 0.57 | 47 / 64 | 0 / 64 | +| 440 | 80.2% | 0.29 | 58 / 64 | 0 / 64 | +| 449 | 79.2% | 0.41 | 53 / 62 | 2 / 64 | + +These counts are behavior indicators: a Pair can be optimal with suitable jokers, and an unused discard does not by itself prove a recoverable loss. + +### Directly avoidable losses + +**Batch 440, sample position 4 (sample_index 15364), action 7:** Ante 1 Small Blind, 212/300 chips, one hand and two discards left, no jokers. The hand was: + +`0 H_A, 1 H_K, 2 C_K, 3 D_K, 4 C_J, 5 S_9, 6 S_3, 7 H_2`. + +The model played `[4,3,2,6,5]`: only two Kings, scoring 60 and losing at 272/300. Playing `[1,2,3]` gives Three of a Kind, `(30 + 30) × 3 = 180`, and clears the blind. The state included both the cards and current scoring bases. No boss was active in this Small Blind. + +**Batch 449, sample position 7 (sample_index 15943), action 8:** Ante 1 Small Blind, 235/300 chips, one hand and one discard left, no jokers. The hand contained two Eights and two Threes. The model played `[6,7,5,4,3]`, a Pair of Threes worth 32, and lost. Selecting both pairs `[2,3,6,7]` scores `(20 + 8 + 8 + 3 + 3) × 2 = 84`, enough to clear. + +These alternatives are deterministic arithmetic from recorded unmodified state, not new engine replays. + +### Evidence of reliance on familiar index patterns + +The selected set `{3,4,5,6,7}` rose from 94/1,314 plays (7.2%) in batch 400 to 341/1,308 (26.1%) in batch 449. The losing action in the second example uses exactly that set despite omitting the other Eight. This is consistent with learned positional templates that fail to condition sufficiently on actual cards; frequency alone does not prove the internal mechanism. + +Across all five sampled batches, the policy never sold a joker or skipped a blind and rerolled only four times. This limits adaptation of its early purchases. The 6,056 plays included just 13 Straights and two Flushes. There were 273 Pair/High Card plays with a raw straight available; these are opportunity flags, not 273 proven mistakes. + +## 4. Why this training setup plausibly reinforces the problem + +**Whole-game credit assignment is weak.** The persisted recipe uses GRPO, four games per seed, and `normalize_episode_rewards`. The matching local reward implementation normalizes final game scores within a seed group and produces one relative advantage per game. The checkpoint reward events shown in the dashboard do not by themselves supply hand-level advantages. A relatively better losing game can reinforce poor plays embedded in that game. Better joker luck and many intervening actions obscure which card choice deserved credit. + +**Useful strategic exploration is narrow.** Four samples per seed yielded distinct transcripts, and 77/80 sampled seed groups had nonzero reward variance. Therefore this is not a blanket zero-reward-variance failure. But many trajectories differ in card-index permutations while repeating the same limited approach. Batch size 64 does not ensure the groups contain examples of the missing strategies. + +**The model must select actions without written reasoning.** The run used `enable_thinking=false`, a 96-token generation limit per action, and instructions to return only JSON. It receives scoring rules, but has no demonstrated hand-scoring decision step. This is a plausible constraint on learning the missing combinations, not proof that enabling reasoning would solve it. + +**Context limits censor some deeper games.** The run used a single growing conversation capped at 49,152 tokens. Twelve of the 320 sampled games reached that cap. However, batch 440's entire sample ended in game over with zero context caps, so truncation cannot explain the late reward dip. The newer segmented-context code in the workspace was not used by this run. + +**There is no evidence here of gradients stopping or exploding.** Steps 425–449 log finite gradient norms of 1.13–2.12 and learning rate `1e-6`. The logged entropy metric rises between the ends of that window, so a simple entropy-collapse claim is unsupported. Zero PPO KL and clip fraction in these logs do not, by themselves, establish that weights were unchanged. + +The earlier audit also identified repeated ante bonuses after ante-lowering vouchers and a Judgement pack-capacity gap. Those can distort reward or legality, but do not explain the direct hand-selection failures or the four infrastructure failures. + +## Recommended order of follow-up + +1. **Fix checkpoint durability and resume validation:** verify every metadata-referenced shard and its required byte range, finish commits from every node, then publish a completion marker. Reject incomplete checkpoints and fall back to a verified earlier checkpoint. More retries cannot repair missing files. +2. **Investigate the native NCCL RAS crash** with the recorded native stack and exact deployed library version. The first failure happened during distributed save, not game rollout. +3. **Measure card-selection competence on fixed states**, including the two guaranteed-clear examples above. Compare available checkpoints on the same seeds, scoring win rate, ante, avoidable losses, discard use, and joker replacement. +4. **Test one learning intervention at a time:** verified scoring demonstrations or a short hand-selection curriculum are justified by these traces. Separately test action-level credit assignment. Preserve the actual win metric and validate the intended objective before changing rewards. +5. **Use bounded context and close known harness gaps** before another long continuation. Verify save/reload as part of a short proof run. + +No fix or relaunch was performed during this diagnosis. Structured evidence is in [broad-layover-final-evidence-2026-09-15.json](broad-layover-final-evidence-2026-09-15.json). diff --git a/src/lua/core/dispatcher.lua b/src/lua/core/dispatcher.lua index c6642a15..f72ea7b0 100644 --- a/src/lua/core/dispatcher.lua +++ b/src/lua/core/dispatcher.lua @@ -119,7 +119,7 @@ end ---@param message string ---@param error_code string -function BB_DISPATCHER.send_error(message, error_code) +function BB_DISPATCHER.send_error(message, error_code, details) if not BB_DISPATCHER.Server then sendDebugMessage("Cannot send error - Server not initialized", "BB.DISPATCHER") return @@ -127,6 +127,7 @@ function BB_DISPATCHER.send_error(message, error_code) BB_DISPATCHER.Server.send_response({ message = message, name = error_code, + details = details, }) end @@ -188,7 +189,14 @@ function BB_DISPATCHER.dispatch(request) break end end - if not state_valid then + -- MENU is a special case: Balatro normally leaves STATE_COMPLETE false + -- there even when the main-menu UI is fully usable. Gameplay states must + -- still be complete and unlocked before accepting an action. + local state_stable = current_state == (G and G.STATES and G.STATES.MENU) + or (G ~= nil + and G.STATE_COMPLETE == true + and (not G.CONTROLLER or G.CONTROLLER.locked ~= true)) + if not state_valid or not state_stable then local state_names = {} for _, state in ipairs(endpoint.requires_state) do table.insert(state_names, get_state_name(state)) @@ -200,7 +208,13 @@ function BB_DISPATCHER.dispatch(request) ) BB_DISPATCHER.send_error( "Method '" .. request.method .. "' requires one of these states: " .. table.concat(state_names, ", "), - BB_ERROR_NAMES.INVALID_STATE + BB_ERROR_NAMES.INVALID_STATE, + { + current_state = current_state_name, + state_complete = G ~= nil and G.STATE_COMPLETE == true, + controller_locked = G ~= nil and G.CONTROLLER ~= nil and G.CONTROLLER.locked == true, + reason = state_valid and "state_transition_in_progress" or "wrong_state", + } ) return end diff --git a/src/lua/core/server.lua b/src/lua/core/server.lua index cbdeb292..8fc849e7 100644 --- a/src/lua/core/server.lua +++ b/src/lua/core/server.lua @@ -397,7 +397,12 @@ function BB_SERVER.send_response(response) error = { code = error_code, message = response.message, - data = { name = error_name }, + data = { + name = error_name, + -- Endpoints may attach diagnostic context without changing the + -- stable error-name contract used by existing clients. + details = response.details, + }, }, id = BB_SERVER.current_request_id, } diff --git a/src/lua/endpoints/pack.lua b/src/lua/endpoints/pack.lua index d60efa80..839c2af6 100644 --- a/src/lua/endpoints/pack.lua +++ b/src/lua/endpoints/pack.lua @@ -2,6 +2,54 @@ ---@type BB_LOGGER local BB_LOGGER = assert(SMODS.load_file("src/lua/utils/logger.lua"))() +local socket = require("socket") + +-- In fast/headless mode the pack object can be removed on a different frame +-- from the state transition. The destination state and readiness flags are +-- therefore the authoritative completion signal; pack removal is useful +-- corroborating evidence but must not be required for the response. +local function state_stable() + return G.STATE_COMPLETE == true + and (not G.CONTROLLER or G.CONTROLLER.locked ~= true) +end + +local function pack_closed() + return not G.pack_cards + or G.pack_cards.REMOVED + or G.STATE ~= G.STATES.SMODS_BOOSTER_OPENED +end + +local function shop_ready() + return G.STATE == G.STATES.SHOP and state_stable() +end + +-- Packs opened from a Blind tag return to blind selection, not the shop. In +-- fast/headless mode that transition can retain the controller lock longer +-- than the pack UI itself; return it to the Python harness, whose settle() +-- loop owns the readiness wait, instead of misreporting a successful skip as +-- an RPC failure. +local function pack_destination_ready() + return shop_ready() or G.STATE == G.STATES.BLIND_SELECT +end + +local function send_timeout(send_response, operation, started_at) + if socket.gettime() - started_at <= 6 then + return false + end + sendDebugMessage( + string.format("pack() %s timed out in state %s", operation, tostring(G.STATE)), + "BB.ENDPOINTS" + ) + send_response({ + message = string.format( + "Pack %s did not reach a stable destination state (current state: %s)", + operation, + tostring(G.STATE) + ), + name = BB_ERROR_NAMES.INTERNAL_ERROR, + }) + return true +end -- ========================================================================== -- Pack Select Endpoint Params @@ -217,6 +265,28 @@ return { end end + -- Pack consumables are used immediately. Calling use_card directly + -- bypasses the UI's legality gate (e.g. Judgement at 5/5 joker slots). + -- Ask the game's own rules after targets have been highlighted, just + -- as the owned-consumable endpoint does. Do not gate playing cards or + -- Jokers with a consumable-only predicate. + if card.ability and card.ability.consumeable then + if not card:can_use_consumeable() then + send_response({ + message = "Consumable '" .. (card.ability.name or card_key or "Unknown") .. "' cannot be used at this time", + name = BB_ERROR_NAMES.NOT_ALLOWED, + }) + return true + end + if card:check_use() then + send_response({ + message = "Cannot use consumable: insufficient space", + name = BB_ERROR_NAMES.NOT_ALLOWED, + }) + return true + end + end + -- Log what we're selecting local card_name = card.ability and card.ability.name or "Unknown" local card_set = card.ability and card.ability.set or card.set or "card" @@ -260,11 +330,12 @@ return { return true end else - -- Pack closes - wait for return to shop - local pack_closed = not G.pack_cards or G.pack_cards.REMOVED - local back_to_shop = G.STATE == G.STATES.SHOP + -- A purchased pack returns to shop; a tag-granted pack returns + -- to blind selection. + local pack_closed_now = pack_closed() + local at_destination = pack_destination_ready() - if pack_closed and back_to_shop then + if pack_closed_now and at_destination then sendDebugMessage("Return pack() after selection", "BB.ENDPOINTS") send_response(BB_GAMESTATE.get_gamestate()) return true @@ -279,24 +350,29 @@ return { -- Handle skip if args.skip then + local started_at = socket.gettime() local pack_count = G.pack_cards.config and G.pack_cards.config.card_count or 0 sendDebugMessage(string.format("Pack: skipping (%d cards remaining)", pack_count), "BB.ENDPOINTS") G.FUNCS.skip_booster({}) - -- Wait for pack to close and return to shop + -- Wait for the destination. Tag-granted packs return to blind select. G.E_MANAGER:add_event(Event({ trigger = "condition", blocking = false, func = function() - local pack_closed = not G.pack_cards or G.pack_cards.REMOVED - local back_to_shop = G.STATE == G.STATES.SHOP + local pack_closed_now = pack_closed() + local at_destination = pack_destination_ready() - if pack_closed and back_to_shop then + if pack_closed_now and at_destination then sendDebugMessage("Return pack() after skip", "BB.ENDPOINTS") send_response(BB_GAMESTATE.get_gamestate()) return true end + if send_timeout(send_response, "skip", started_at) then + return true + end + return false end, })) diff --git a/src/lua/endpoints/play.lua b/src/lua/endpoints/play.lua index 1b9f0a98..2f58d885 100644 --- a/src/lua/endpoints/play.lua +++ b/src/lua/endpoints/play.lua @@ -75,6 +75,17 @@ return { local card_str = BB_LOGGER.format_playing_cards(G.hand.cards, args.cards) sendDebugMessage(string.format("Playing %d cards: %s", #args.cards, card_str), "BB.ENDPOINTS") + local chips_before = G.GAME.chips or 0 + local function send_play_state() + local state_data = BB_GAMESTATE.get_gamestate() + state_data.play_commit = { + cards = args.cards, + score = math.max(0, (G.GAME.chips or chips_before) - chips_before), + hand = G.GAME.last_hand_played, + } + send_response(state_data) + end + ---@diagnostic disable-next-line: undefined-field local play_button = UIBox:get_UIE_by_ID("play_button", G.buttons.UIRoot) assert(play_button ~= nil, "play() play button not found") @@ -87,7 +98,7 @@ return { -- because when G.STATE becomes GAME_OVER, the game sets G.SETTINGS.paused = true, -- which stops all event processing. This callback is set so that love.update -- (which runs even when paused) can detect GAME_OVER immediately. - BB_GAMESTATE.on_game_over = send_response + BB_GAMESTATE.on_game_over = send_play_state G.E_MANAGER:add_event(Event({ trigger = "condition", @@ -124,8 +135,7 @@ return { -- Game is won if G.GAME.won then sendDebugMessage("Return play() - won", "BB.ENDPOINTS") - local state_data = BB_GAMESTATE.get_gamestate() - send_response(state_data) + send_play_state() return true end @@ -144,17 +154,15 @@ return { -- Both first and last scoring rows must be present if has_blind1 and has_cash_out_button then - local state_data = BB_GAMESTATE.get_gamestate() sendDebugMessage("Return play() - cash out", "BB.ENDPOINTS") - send_response(state_data) + send_play_state() return true end end if draw_to_hand and hand_played and G.buttons and G.STATE == G.STATES.SELECTING_HAND then sendDebugMessage("Return play() - same round", "BB.ENDPOINTS") - local state_data = BB_GAMESTATE.get_gamestate() - send_response(state_data) + send_play_state() return true end diff --git a/src/lua/endpoints/sell.lua b/src/lua/endpoints/sell.lua index 5d112222..888fd51a 100644 --- a/src/lua/endpoints/sell.lua +++ b/src/lua/endpoints/sell.lua @@ -32,7 +32,7 @@ return { }, }, - requires_state = { G.STATES.SELECTING_HAND, G.STATES.SHOP }, + requires_state = { G.STATES.SELECTING_HAND, G.STATES.SHOP, G.STATES.SMODS_BOOSTER_OPENED }, ---@param args Request.Endpoint.Sell.Params ---@param send_response fun(response: Response.Endpoint) @@ -95,6 +95,17 @@ return { local card = source_array[pos] + -- Use the game's own gate: in particular Eternal jokers cannot be sold. + if not card:can_sell_card() then + send_response({ + message = "This card cannot be sold at this time", + name = BB_ERROR_NAMES.NOT_ALLOWED, + }) + return + end + local pack_before = G.STATE == G.STATES.SMODS_BOOSTER_OPENED and G.pack_cards or nil + local pack_choices_before = G.GAME.pack_choices + -- Track initial state for completion verification local area = sell_type == "joker" and G.jokers or G.consumeables local initial_count = area.config.card_count @@ -144,7 +155,17 @@ return { local state_stable = G.STATE_COMPLETE == true -- 5. Still in valid state - local valid_state = (G.STATE == G.STATES.SHOP or G.STATE == G.STATES.SELECTING_HAND) + local valid_state + if pack_before then + -- Selling frees inventory capacity; it must not select a pack card, + -- consume a choice, close the pack, or return to the shop. + valid_state = G.STATE == G.STATES.SMODS_BOOSTER_OPENED + and G.pack_cards == pack_before + and not G.pack_cards.REMOVED + and G.GAME.pack_choices == pack_choices_before + else + valid_state = G.STATE == G.STATES.SHOP or G.STATE == G.STATES.SELECTING_HAND + end -- All conditions must be met if count_decreased and money_increased and card_gone and state_stable and valid_state then diff --git a/src/lua/utils/gamestate.lua b/src/lua/utils/gamestate.lua index a7cc2b97..41620d1b 100644 --- a/src/lua/utils/gamestate.lua +++ b/src/lua/utils/gamestate.lua @@ -320,6 +320,75 @@ end -- Card Extractor -- ========================================================================== +-- These are the targets already visible on the joker this round, not RNG +-- seeds or future outcomes. Never invoke a reset/random function while reading. +local function extract_joker_scoring_state(card) + local key = card.config and (card.config.center_key or (card.config.center and card.config.center.key)) + local target_keys = { + j_idol = "idol_card", + j_ancient = "ancient_card", + j_mail = "mail_card", + j_castle = "castle_card", + } + local target_key = target_keys[key] + local current_round = G and G.GAME and G.GAME.current_round + local target = target_key and current_round and current_round[target_key] + if type(target) ~= "table" then return nil end + local visible_target = {} + if target.suit then visible_target.suit = convert_suit_to_enum(target.suit) end + if target.rank then visible_target.rank = convert_rank_to_enum(tostring(target.rank)) end + if type(target.id) == "number" then visible_target.rank_id = target.id end + local result = { target = visible_target } + if key == "j_castle" and card.ability and type(card.ability.extra) == "table" then + result.chips = card.ability.extra.chips + result.chip_gain = card.ability.extra.chip_mod + end + return result +end + +-- Copy only documented, observable mechanics. Never serialize the engine's +-- whole ability/game tables (they can contain hidden state and references). +local function copy_fields(source, keys) + local result = {} + for _, key in ipairs(keys) do + local value = source and source[key] + if type(value) == "number" or type(value) == "boolean" or type(value) == "string" then + result[key] = value + end + end + return result +end + +local function extract_counters(card) + local ability = card.ability or {} + local result = copy_fields(ability, { + "perma_bonus", "played_this_ante", "forced_selection", "perma_debuff", + "mult", "x_mult", "bonus", "h_mult", "h_x_mult", "h_dollars", "p_dollars", + "t_chips", "t_mult", "extra_value", "loyalty_remaining", "burnt_hand", + "caino_xmult", "yorick_discards", "invis_rounds", "hands_played_at_create", + "to_do_poker_hand", "steel_tally", "stone_tally", "driver_tally", "nine_tally", + "perish_tally", "perishable", + }) + -- Playing-card base values already have an enhancement description. Only + -- permanent bonuses and per-card history need an extra persistent record. + if card.config and card.config.card then + local playing = copy_fields(ability, {"perma_bonus", "played_this_ante", "forced_selection", "perma_debuff"}) + if playing.perma_bonus == 0 then playing.perma_bonus = nil end + return playing + end + if type(ability.extra) == "number" then + result.extra = ability.extra + elseif type(ability.extra) == "table" then + result.extra = copy_fields(ability.extra, { + "Xmult", "xmult", "chips", "chip_mod", "mult", "dollars", "odds", + "every", "discards", "discard_sub", "hand_add", "h_mod", "h_plays", + "h_size", "increase", "size", "min", "max", "faces", "suit", + "s_mult", "poker_hand", "d_remaining", "destroy", + }) + end + return result +end + ---Extracts a complete Card object from a game card ---@param card table The game card object ---@return Card card The Card object @@ -366,6 +435,8 @@ local function extract_card(card) modifier = extract_card_modifier(card), state = extract_card_state(card), cost = extract_card_cost(card), + scoring_state = extract_joker_scoring_state(card), + counters = extract_counters(card), } end @@ -728,7 +799,14 @@ function gamestate.get_gamestate() end local state_data = { + snapshot_version = 1, state = get_state_name(G.STATE), + -- These are transport/readiness details for the Python harness. They are + -- deliberately not part of the compact model prompt, but let the client + -- distinguish a stable decision screen from a frame in the middle of a + -- Balatro transition. + state_complete = G.STATE_COMPLETE == true, + controller_locked = G.CONTROLLER and G.CONTROLLER.locked == true or false, } -- Basic game info @@ -737,6 +815,34 @@ function gamestate.get_gamestate() state_data.ante_num = (G.GAME.round_resets and G.GAME.round_resets.ante) or 0 state_data.money = G.GAME.dollars or 0 state_data.won = G.GAME.won + state_data.run_state = copy_fields(G.GAME, { + "last_tarot_planet", "last_hand_played", "hands_played", "unused_discards", + "skips", "bankrupt_at", "interest_cap", "interest_amount", "inflation", + "discount_percent", "ecto_minus", "starting_deck_size", + }) + state_data.run_state.round = copy_fields(G.GAME.current_round, { + "hands_played", "discards_used", "most_played_poker_hand", "free_rerolls", + "reroll_cost_increase", "jokers_purchased", + }) + state_data.run_state.consumable_usage = copy_fields(G.GAME.consumeable_usage_total, {"tarot", "planet", "spectral"}) + state_data.run_state.probabilities = copy_fields(G.GAME.probabilities, {"normal"}) + state_data.blind_state = copy_fields(G.GAME.blind, {"disabled", "only_hand"}) + -- The Eye remembers which hand types have been played this blind. + state_data.blind_state.played_hands = {} + if G.GAME.blind and G.GAME.blind.hands then + for name, played in pairs(G.GAME.blind.hands) do + if type(played) == "boolean" then state_data.blind_state.played_hands[name] = played end + end + end + state_data.tags = {} + for _, tag in ipairs(G.GAME.tags or {}) do + local info = get_tag_info(tag.key) + state_data.tags[#state_data.tags + 1] = { + key = tag.key, name = info.name, effect = info.effect, triggered = tag.triggered == true, + ability = copy_fields(tag.ability, {"orbital_hand", "blind_type"}), + } + end + state_data.pack_choices = G.GAME.pack_choices -- Deck (optional) if G.GAME.selected_back and G.GAME.selected_back.effect and G.GAME.selected_back.effect.center then @@ -782,12 +888,51 @@ function gamestate.get_gamestate() -- Always available areas state_data.jokers = extract_area(G.jokers) state_data.consumables = extract_area(G.consumeables) -- Note: typo in game code + -- Owned joker identities are known even when Amber Acorn hides their new + -- order. Preserve that multiset without revealing which face-down slot is + -- which. Shop/pack cards do not receive this treatment. + if G.jokers then + local hidden = false + for _, card in ipairs(G.jokers.cards or {}) do + if card.facing == "back" then hidden = true end + end + if hidden then + state_data.joker_inventory = {count = #G.jokers.cards, limit = #G.jokers.cards, cards = {}} + for _, card in ipairs(G.jokers.cards) do + local view = extract_card(card) + view.id = 0 + view.state = {} + state_data.joker_inventory.cards[#state_data.joker_inventory.cards + 1] = view + end + table.sort(state_data.joker_inventory.cards, function(a, b) return a.key < b.key end) + end + end + + -- G.playing_cards contains the full owned deck across all areas. Export a + -- sorted composition without per-instance identities or area positions. + -- These are known deck contents, not the identity of any face-down slot. + state_data.owned_cards = {count = #(G.playing_cards or {}), limit = #(G.playing_cards or {}), cards = {}} + for _, card in ipairs(G.playing_cards or {}) do + local view = extract_card(card) + view.id = 0 -- anonymous composition, not an instance ID + view.state = {} + view.counters = copy_fields(card.ability, {"perma_bonus", "played_this_ante"}) + if view.counters.perma_bonus == 0 then view.counters.perma_bonus = nil end + state_data.owned_cards.cards[#state_data.owned_cards.cards + 1] = view + end + table.sort(state_data.owned_cards.cards, function(a, b) return a.key < b.key end) -- Cards remaining in deck if G.deck then state_data.cards = extract_area(G.deck) end + -- Cards already discarded this round/run. The compact Python prompt sorts + -- this composition, so it does not leak the hidden draw order. + if G.discard then + state_data.discard = extract_area(G.discard) + end + -- Hand (count is 0 during not playing phase) if G.hand then state_data.hand = extract_area(G.hand) @@ -814,6 +959,20 @@ function gamestate.get_gamestate() return state_data end +---Whether the game is ready to accept an action in the requested state. +---@param expected_state integer? +---@return boolean +function gamestate.is_stable(expected_state) + if not G then + return false + end + if expected_state and G.STATE ~= expected_state then + return false + end + return G.STATE_COMPLETE == true + and (not G.CONTROLLER or G.CONTROLLER.locked ~= true) +end + -- ========================================================================== -- GAME_OVER Callback Support -- ========================================================================== diff --git a/src/lua/utils/openrpc.json b/src/lua/utils/openrpc.json index eaea0856..508780e4 100644 --- a/src/lua/utils/openrpc.json +++ b/src/lua/utils/openrpc.json @@ -577,7 +577,7 @@ { "name": "sell", "summary": "Sell a joker or consumable", - "description": "Sell a joker or consumable from player inventory. Must provide exactly one of: joker or consumable.", + "description": "Sell a joker or consumable from player inventory in the shop, while selecting a hand, or inside an open booster pack. A pack remains open with its choices unchanged after selling. Must provide exactly one of: joker or consumable. Eternal cards cannot be sold.", "tags": [ { "$ref": "#/components/tags/shop" @@ -954,6 +954,10 @@ "$ref": "#/components/schemas/Area", "description": "Cards remaining in deck (available during run)" }, + "discard": { + "$ref": "#/components/schemas/Area", + "description": "Cards already discarded (available during run)" + }, "pack": { "$ref": "#/components/schemas/Area", "description": "Currently open pack (available during pack opening phase)" @@ -969,6 +973,37 @@ "packs": { "$ref": "#/components/schemas/Area", "description": "Booster packs area (available during shop phase)" + }, + "snapshot_version": { + "type": "integer", + "description": "Version of the state fields required by context resets; currently 1." + }, + "owned_cards": { + "$ref": "#/components/schemas/Area", + "description": "Full owned deck composition across areas. Cards have anonymous id=0; array order conveys no draw order." + }, + "run_state": { + "type": "object", + "description": "Allowlisted observable run and round counters, prior hand/consumable, and economy values." + }, + "blind_state": { + "type": "object", + "description": "Current blind disabled status and hand-type restrictions/history." + }, + "tags": { + "type": "array", + "items": { + "type": "object" + }, + "description": "Pending/active tags, their effects and visible targets." + }, + "pack_choices": { + "type": "integer", + "description": "Number of selections remaining in the current pack." + }, + "joker_inventory": { + "$ref": "#/components/schemas/Area", + "description": "Known unordered owned-joker composition when slots are face-down; anonymous IDs reveal no slot mapping." } }, "required": [ @@ -1152,6 +1187,26 @@ }, "cost": { "$ref": "#/components/schemas/CardCost" + }, + "scoring_state": { + "type": "object", + "description": "Already-visible round target for Idol, Ancient Joker, Mail-In Rebate or Castle; never RNG seeds or future outcomes.", + "properties": { + "target": { + "type": "object", + "properties": { + "rank": { "$ref": "#/components/schemas/Rank" }, + "suit": { "$ref": "#/components/schemas/Suit" }, + "rank_id": { "type": "integer" } + } + }, + "chips": { "type": "number" }, + "chip_gain": { "type": "number" } + } + }, + "counters": { + "type": "object", + "description": "Allowlisted visible persistent card/joker values, including perma_bonus, played_this_ante and accumulated bonuses. No RNG state." } }, "required": [ diff --git a/src/lua/utils/types.lua b/src/lua/utils/types.lua index 53f43b13..8622f940 100644 --- a/src/lua/utils/types.lua +++ b/src/lua/utils/types.lua @@ -9,10 +9,19 @@ -- ========================================================================== ---@class GameState +---@field snapshot_version integer? Context-reset state schema version +---@field owned_cards Area? Full owned composition; anonymous IDs, no draw order +---@field joker_inventory Area? Known unordered joker composition when slots are hidden +---@field run_state table? Observable run/round counters and previous action facts +---@field blind_state table? Current blind restrictions and hand history +---@field tags table[]? Pending/active tags with visible effects and targets +---@field pack_choices integer? Selections remaining in the open pack ---@field deck Deck? Current selected deck ---@field stake Stake? Current selected stake ---@field seed string? Seed used for the run ---@field state State Current game state +---@field state_complete boolean? Whether the current state has finished loading +---@field controller_locked boolean? Whether Balatro is currently blocking input ---@field round_num integer Current round number ---@field ante_num integer Current ante number ---@field money integer Current money amount @@ -24,6 +33,7 @@ ---@field consumables Area? Consumables area ---@field hand Area? Hand area (available during playing phase) ---@field cards Area? Cards remaining in deck (available during run) +---@field discard Area? Cards already discarded (available during run) ---@field pack Area? Currently open pack (available during opeing pack phase) ---@field shop Area? Shop area (available during shop phase) ---@field vouchers Area? Vouchers area (available during shop phase) @@ -71,6 +81,13 @@ ---@field modifier Card.Modifier Modifier information (seals, editions, enhancements) ---@field state Card.State Current state information (debuff, hidden, highlighted) ---@field cost Card.Cost Cost information (buy/sell prices) +---@field scoring_state Card.ScoringState? Visible round-specific joker target/counters (never the RNG state) +---@field counters table? Visible persistent bonuses and per-card mechanic counters + +---@class Card.ScoringState +---@field target {rank: string?, suit: string?, rank_id: integer?} The current round's already-selected target +---@field chips number? Current Castle chips +---@field chip_gain number? Castle chips gained per matching discarded card ---@class Card.Value ---@field suit Card.Value.Suit? Suit (Hearts, Diamonds, Clubs, Spades) - only for playing cards diff --git a/tests/fixtures/broad_layover_decisions.json b/tests/fixtures/broad_layover_decisions.json new file mode 100644 index 00000000..aa19593d --- /dev/null +++ b/tests/fixtures/broad_layover_decisions.json @@ -0,0 +1,209 @@ +[ + { + "batch": 440, + "sample_index": 15364, + "hand": [ + "H_A", + "H_K", + "C_K", + "D_K", + "C_J", + "S_9", + "S_3", + "H_2" + ], + "blind": { + "type": "small", + "score": [ + 212, + 300 + ], + "hands": 1, + "discards": 2 + }, + "hand_values": { + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 1, + 40, + 4 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 1, + 30, + 3 + ], + "Two Pair": [ + 1, + 20, + 2 + ], + "Pair": [ + 1, + 10, + 2 + ], + "High Card": [ + 1, + 5, + 1 + ], + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ] + }, + "bad_action": { + "method": "play", + "params": { + "cards": [ + 4, + 3, + 2, + 6, + 5 + ] + } + }, + "winning_cards": [ + 1, + 2, + 3 + ], + "winning_score": 180 + }, + { + "batch": 449, + "sample_index": 15943, + "hand": [ + "S_A", + "S_T", + "H_8", + "D_8", + "C_6", + "C_5", + "S_3", + "D_3" + ], + "blind": { + "type": "small", + "score": [ + 235, + 300 + ], + "hands": 1, + "discards": 1 + }, + "hand_values": { + "Flush Five": [ + 1, + 160, + 16 + ], + "Flush House": [ + 1, + 140, + 14 + ], + "Five of a Kind": [ + 1, + 120, + 12 + ], + "Straight Flush": [ + 1, + 100, + 8 + ], + "Four of a Kind": [ + 1, + 60, + 7 + ], + "Full House": [ + 1, + 40, + 4 + ], + "Flush": [ + 1, + 35, + 4 + ], + "Straight": [ + 1, + 30, + 4 + ], + "Three of a Kind": [ + 1, + 30, + 3 + ], + "Two Pair": [ + 1, + 20, + 2 + ], + "Pair": [ + 1, + 10, + 2 + ], + "High Card": [ + 1, + 5, + 1 + ] + }, + "bad_action": { + "method": "play", + "params": { + "cards": [ + 6, + 7, + 5, + 4, + 3 + ] + } + }, + "winning_cards": [ + 2, + 3, + 6, + 7 + ], + "winning_score": 84 + } +] diff --git a/tests/test_context_management.py b/tests/test_context_management.py new file mode 100644 index 00000000..c624ad7d --- /dev/null +++ b/tests/test_context_management.py @@ -0,0 +1,774 @@ +"""CPU regressions for resets, exact training prefixes and snapshot fidelity.""" + +import ast +import asyncio +import json +import math +import os +import re +import shutil +import subprocess +import sys +import time +from copy import deepcopy +from dataclasses import dataclass, field +from enum import Enum +from pathlib import Path +from types import ModuleType, SimpleNamespace +from typing import Any + +import pytest + +ROOT = Path(__file__).resolve().parents[1] + + +class Tokenizer: + """Deterministic character tokens; different action IDs test prefix fidelity.""" + + eos_token = "<|im_end|>" + eos_token_id = 0 + unk_token_id = -1 + + def __call__(self, text, **kwargs): + if text == "": + return {"input_ids": [99000]} + return {"input_ids": list(text.encode())} + + def apply_chat_template(self, messages, **kwargs): + result = "".join("<|im_start|>" + m["role"] + "\n" + m["content"] + "<|im_end|>\n" for m in messages) + suffix = "<|im_start|>assistant\n\n" if kwargs.get("enable_thinking") else "<|im_start|>assistant\n\n\n\n\n" + return result + (suffix if kwargs.get("add_generation_prompt") else "") + + +@dataclass +class Action: + name: str + arguments: dict + + +@dataclass +class Sample: + class Status(Enum): + COMPLETED = "completed" + ABORTED = "aborted" + + prompt: str = "TEST-SEED" + metadata: dict = field(default_factory=dict) + rollout_id: int | None = None + index: int = 7 + group_index: int = 1 + status: Any = None + + +@pytest.fixture +def harness(monkeypatch): + names = { + "_compact_state", "_area_items", "_slot_usage", "_purchasable_indices", "_sellable_indices", + "_consumable_target_bounds", "_consumable_requires_joker", "_consumable_available", "_target_action_help", + "_action_help", "_turn_prompt", "_chat_prompt", "_feedback_chat_turn", "_action_payload", + "_state_delta", "_delta_payload", "_selected_blind", "_skip_transition", + "_environment_feedback", "_balatro_context_limits", "_validate_balatro_trainer", + "_validate_balatro_loss_config", "_invalid_action_weight", + "_BalatroContext", "_StateBudgetExceeded", "_SnapshotUnavailable", + "_segment_reward_events", "generate_balatro_episode", "_progress_value", + "_transition_reward_parts", "_ante_clear_reward", "_episode_reward_parts", + "normalize_episode_rewards", "_sample_reward", "_generate_action", + "_validate_balatro_sampling_params", "_validate_balatro_sampling_config", + "_validate_balatro_context_config", "_assign_action_advantages", "_finalize_action_credit", + "generate_balatro_comparison", "_unique_json_keys", "_reject_json_constant", + "_single_action_object", "_action_object_count", "_action_output_error", + "_decode_action", "_parse_action", "_action_is_locally_legal", + "_thinking_budget", "_thinking_action_text", "_balatro_system_prompt", "_sample_action_tokens", + } + constants = { + "BALATRO_SYSTEM_PROMPT", "_DELTA_ANCHORS", "_UNCHANGED", "_ALLOWED_METHODS", + "ANTE_CHECKPOINT_BONUS", "FINAL_ANTE", "DECISION_CAP_PENALTY", + "_CONSUMABLE_TARGET_BOUNDS", "_CONSUMABLE_REQUIRES_JOKER", + } + tree = ast.parse((ROOT / "batro_modal_rl.py").read_text()) + selected = [ast.ImportFrom(module="__future__", names=[ast.alias(name="annotations")], level=0)] + for node in tree.body: + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)) and node.name in names: + selected.append(node) + elif isinstance(node, ast.Assign) and any(isinstance(t, ast.Name) and t.id in constants for t in node.targets): + selected.append(node) + elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name) and node.target.id in constants: + selected.append(node) + ns = dict(Any=Any, json=json, math=math, re=re, time=time, asyncio=asyncio, + Path=Path, deepcopy=deepcopy, ToolCall=Action) + exec(compile(ast.fix_missing_locations(ast.Module(body=selected, type_ignores=[])), "harness", "exec"), ns) + for name in ("slime", "slime.utils", "slime.utils.types"): + monkeypatch.setitem(sys.modules, name, ModuleType(name)) + sys.modules["slime.utils.types"].Sample = Sample + ns["_log_balatro_event"] = lambda *a, **k: None + ns["_tokenizer"] = lambda checkpoint: Tokenizer() + return ns + + +def game_state(turn=0, *, done=False): + card = {"id": 19, "key": "S_A", "modifier": {"seal": "GOLD"}, "counters": {"perma_bonus": 30}} + return { + "snapshot_version": 1, "state": "GAME_OVER" if done else "SELECTING_HAND", + "ante_num": 1, "round_num": 1, "money": turn, "won": done, + "round": {"chips": turn * 10, "hands_left": 4, "discards_left": 3}, + "blinds": {"small": {"type": "SMALL", "status": "CURRENT", "score": 1000}}, + "hand": {"cards": [card]}, "cards": {"count": 1, "cards": [{"key": "C_K"}]}, + "owned_cards": {"cards": [card, {"key": "C_K"}]}, + "jokers": {"cards": [{"id": 70, "key": "j_green_joker", "value": {"effect": "gain Mult"}, "counters": {"mult": turn}}]}, + "used_vouchers": {"v_blank": ""}, + "hands": {"Pair": {"level": 3, "chips": 40, "mult": 4, "played": turn}}, + "tags": [{"key": "tag_double"}], "run_state": {"last_tarot_planet": "c_devil"}, + } + + +def run_episode(harness, *, decisions=30, target=7000, hard=12288, final_extra="", fail_initial=False, forced_error=False, phases=False, missing_schema=False, responses=None, sampled_tokens=2, thinking_budget=0, generate_override=None): + seen = [] + env = SimpleNamespace(closed=False, steps=0) + env._terminal = lambda state: state.get("state") == "GAME_OVER" + initial = game_state() + if missing_schema: + initial.pop("snapshot_version") + if fail_initial: + initial["jokers"]["cards"][0]["value"]["effect"] = "X" * hard + env.start = lambda seed: deepcopy(initial) + + def step(action): + env.steps += 1 + state = game_state(env.steps, done=env.steps == decisions) + if phases and env.steps < decisions: + state["state"] = ["SELECTING_HAND", "SHOP", "SMODS_BOOSTER_OPENED", "BLIND_SELECT"][env.steps % 4] + if state["state"] == "BLIND_SELECT": + state["blinds"]["small"]["status"] = "SELECT" + if env.steps == decisions: + state["jokers"]["cards"][0]["value"]["effect"] += final_extra + if forced_error: + return SimpleNamespace(observation=SimpleNamespace(metadata={"state": state}, is_error=True, text="bad action")) + return SimpleNamespace(observation=SimpleNamespace(metadata={"state": state}, is_error=False), done=env.steps == decisions) + + env.step = step + env.close = lambda: setattr(env, "closed", True) + harness["_get_env_pool"] = lambda: SimpleNamespace(acquire=lambda seed: env) + harness["_fallback_action"] = lambda state: Action("play", {"cards": [0]}) + + async def generate(args, params, ids, phase): + # IDs intentionally differ from encoding the response text. Training + # must use the original IDs, not re-tokenize the pretty transcript. + action_ids = [100000 + len(seen)] * (sampled_tokens - 1) + [99999] + if thinking_budget: + action_ids[-3] = 99000 + seen.append((list(ids), action_ids)) + defaults = { + "SELECTING_HAND": {"method": "play", "params": {"cards": [0]}}, + "SHOP": {"method": "next_round", "params": {}}, + "SMODS_BOOSTER_OPENED": {"method": "pack", "params": {"skip": True}}, + "BLIND_SELECT": {"method": "select", "params": {}}, + } + if responses: + text, finish = responses[min(len(seen)-1, len(responses)-1)] + else: + text, finish = json.dumps(defaults[phase], separators=(",", ":")), "stop" + return action_ids, text, finish, [-0.2] * (sampled_tokens - 1) + [-0.3], [1] * sampled_tokens + + harness["_generate_action"] = generate_override or generate + args = SimpleNamespace(balatro_max_decisions=decisions + 2, balatro_context_len=hard, + balatro_segment_tokens=target, balatro_capture_screenshot=False, + hf_checkpoint="unused", balatro_thinking_budget=thinking_budget) + samples = asyncio.run(harness["generate_balatro_episode"](args, Sample(), {}, evaluation=True)) + return samples, seen, env + + +def test_rebases_keep_game_running_and_train_every_action_with_its_exact_prefix(harness): + samples, seen, env = run_episode(harness) + assert env.closed and env.steps == 30 + assert 1 < len(samples) < 30 + assert {s.rollout_id for s in samples} == {samples[0].rollout_id} + assert {s.reward for s in samples} == {samples[0].reward} + assert {s.metadata["exit_status"] for s in samples} == {"won"} + reconstructed = [] + for index, sample in enumerate(samples): + assert sample.metadata["segment_id"] == index + assert len(sample.tokens) + 128 <= 12288 + assert len(sample.loss_mask) == sample.response_length == len(sample.rollout_log_probs) + assert len(sample.metadata["trajectory_messages"]) <= 128 + offset = len(sample.tokens) - sample.response_length + for token in range(sample.response_length): + if sample.loss_mask[token] and (token == 0 or not sample.loss_mask[token - 1]): + reconstructed.append((sample.tokens[:offset + token], sample.tokens[offset + token:offset + token + 2])) + # Snapshots stand alone and retain changes made before a reset. + assert '"perma_bonus":30' in sample.prompt and '"seal":"GOLD"' in sample.prompt + assert '"owned_vouchers"' in sample.prompt and '"last_tarot_planet":"c_devil"' in sample.prompt + assert reconstructed == seen + assert sum(sum(s.loss_mask) for s in samples) == 60 + assert sum("balatro/won" in s.metadata for s in samples) == 1 + assert sum("balatro_episode_archive" in s.metadata for s in samples) == 1 + events = [e for s in samples for e in s.metadata["reward_events"]] + assert len(events) == 1 and events[0]["label"] == "WON" + assert len(events[0]["segment_spans"]) > 1 + + +def test_oversized_terminal_feedback_does_not_turn_a_win_into_context_cap(harness): + samples, seen, env = run_episode(harness, decisions=2, final_extra="X" * 20000) + assert len(seen) == 2 and env.closed + assert all(s.metadata["exit_status"] == "won" for s in samples) + assert all(len(s.tokens) < 12288 for s in samples) + assert "X" * 20000 in samples[0].metadata["balatro_episode_archive"]["response"] + + +def test_oversized_initial_snapshot_never_calls_model_and_closes_game(harness): + sample, seen, env = run_episode(harness, fail_initial=True) + assert not seen and env.closed and env.steps == 0 + assert sample.status == Sample.Status.ABORTED + assert sample.metadata["exit_status"] == "state_budget_exceeded" + + +def test_old_assets_are_rejected_before_generating_with_incomplete_snapshots(harness): + sample, seen, env = run_episode(harness, missing_schema=True) + assert not seen and env.closed + assert sample.metadata["exit_status"] == "state_schema_mismatch" + + +def test_full_length_game_survives_history_larger_than_the_old_context_cap(harness): + samples, seen, env = run_episode(harness, decisions=250, phases=True) + assert len(seen) == env.steps == 250 and env.closed + assert samples[0].metadata["balatro/episode_archive_tokens"] > 49152 + assert samples[0].metadata["balatro/context_tokens"] < 12288 + assert all(s.metadata["exit_status"] == "won" for s in samples) + assert sum(sum(s.loss_mask) for s in samples) == 500 + assert all(len(prefix) + 224 <= 12288 for prefix, _ in seen) + + +def test_rpc_recovery_error_is_archived_without_dropping_generated_action(harness): + samples, seen, env = run_episode(harness, forced_error=True) + assert len(seen) == 1 and env.closed + assert samples[0].metadata["exit_status"] == "rpc_error" + assert sum(samples[0].loss_mask) == 2 + assert "bad action" in samples[0].metadata["balatro_episode_archive"]["response"] + + +def test_oversized_snapshot_can_exceed_soft_target_but_not_hard_cap(harness): + context = harness["_BalatroContext"](Tokenizer(), 2048, 512) + context.begin("X" * 1000, "snapshot", 1) + assert len(context.input_ids()) == 1000 + context.add_action([1], "a", [-0.1], turn=1, archive_start=0) + context.add_feedback("f", [2]) + assert context.needs_reset + with pytest.raises(RuntimeError, match="fresh snapshot"): + context.input_ids() + + +@pytest.mark.parametrize("ended", [False, True]) +def test_continuations_use_fresh_assistant_framing_without_retokenizing_actions(harness, ended): + tokenizer = Tokenizer() + context = harness["_BalatroContext"](tokenizer, 12288, 10000) + context.begin(harness["_chat_prompt"](tokenizer, "snapshot"), "snapshot", 1) + action_ids = [123456, tokenizer.eos_token_id] if ended else [123456, 654321] + context.add_action(action_ids, "sampled action", [-.2, -.1], turn=1, archive_start=0) + exact_prefix = context.input_ids().copy() + context.add_feedback("new observation", [99]) + framed = harness["_feedback_chat_turn"](tokenizer, "new observation", assistant_ended=ended) + suffix = tokenizer(framed)["input_ids"] + assert context.input_ids() == exact_prefix + suffix + assert context.current["loss_mask"] == [1, 1] + [0] * len(suffix) + assert context.current["log_probs"] == [-.2, -.1] + [0.] * len(suffix) + assert framed.endswith("<|im_start|>assistant\n\n\n\n\n") + assert framed.startswith(("" if ended else tokenizer.eos_token) + "\n<|im_start|>user\n") + assert "system" not in framed + + +def test_budget_counts_output_margin_and_training_padding(harness): + args = SimpleNamespace(balatro_context_len=49152, balatro_segment_tokens=8192, + sglang_context_length=49000, tensor_model_parallel_size=4, + context_parallel_size=4, max_tokens_per_gpu=12288) + with pytest.raises(ValueError, match="permit only 47104"): + harness["_balatro_context_limits"](args) + args.sglang_context_length = 49152 + args.max_position_embeddings = 49152 + assert harness["_balatro_context_limits"](args) == (49152, 8192) + context = harness["_BalatroContext"](Tokenizer(), 512, 512) + context.begin("a" * (512 - 224), "snapshot", 1) + with pytest.raises(harness["_StateBudgetExceeded"]): + context.begin("a" * (512 - 223), "snapshot", 1) + + +def test_inherited_4k_position_limit_fails_before_game_acquisition(harness): + args = SimpleNamespace(balatro_context_len=49152, balatro_segment_tokens=8192, + sglang_context_length=49152, max_position_embeddings=4096, + tensor_model_parallel_size=4, context_parallel_size=4, + max_tokens_per_gpu=12288) + with pytest.raises(ValueError, match="permit only 4096"): + harness["_balatro_context_limits"](args) + + +def test_launch_requires_explicit_serving_and_trainer_context(harness): + recipe = SimpleNamespace(context_parallel_size=4, tensor_model_parallel_size=4, + max_tokens_per_gpu=12288, extra_config={}) + with pytest.raises(ValueError, match="explicit max_position_embeddings"): + harness["_validate_balatro_context_config"](recipe) + recipe.extra_config = {"max_position_embeddings": 49152, "sglang_context_length": 49152} + assert harness["_validate_balatro_context_config"](recipe) == (49152, 8192) + recipe.extra_config["sglang_context_length"] = 4096 + with pytest.raises(ValueError, match="permit only 4096"): + harness["_validate_balatro_context_config"](recipe) + + +def test_context_parallel_launch_rejects_unfused_attention(harness): + recipe = SimpleNamespace(context_parallel_size=4, tensor_model_parallel_size=4, + max_tokens_per_gpu=12288, attention_backend="unfused", + extra_config={"max_position_embeddings": 49152, + "sglang_context_length": 49152}) + validate = harness["_validate_balatro_context_config"] + with pytest.raises(ValueError, match="Unfused attention does not support"): + validate(recipe) + recipe.attention_backend = "flash" + assert validate(recipe) == (49152, 8192) + recipe.extra_config["attention_backend"] = "unfused" + with pytest.raises(ValueError, match="Unfused attention does not support"): + validate(recipe) + + +def test_action_returns_include_terminal_bonus_and_exclude_past_rewards(harness): + samples, seen, _ = run_episode(harness, decisions=30) + actions = [a for s in samples for a in s.metadata["action_credit"]] + assert len(actions) == len(seen) == 30 + assert actions[0]["return_to_go"] == pytest.approx(samples[0].reward) + # The fixture earns 0.01 progress per move before the terminal win. + assert actions[1]["return_to_go"] == pytest.approx(samples[0].reward - .01) + assert actions[-1]["return_to_go"] > 0 # includes terminal/win bonus + assert all(a["token_end"] - a["token_start"] == 2 for a in actions) + + +def test_comparison_eval_reports_one_game_but_training_keeps_exact_segments(harness): + segments, _, _ = run_episode(harness) + async def generate(*args, **kwargs): + return deepcopy(segments) + harness["generate_balatro_episode"] = generate + args = SimpleNamespace(hf_checkpoint="unused") + train = asyncio.run(harness["generate_balatro_comparison"](args, Sample(), {}, evaluation=False)) + assert len(train) == len(segments) + assert train[0].tokens == segments[0].tokens + evaluated = asyncio.run(harness["generate_balatro_comparison"](args, Sample(), {}, evaluation=True)) + assert not isinstance(evaluated, list) + assert evaluated.reward == segments[0].reward + assert evaluated.response == segments[0].metadata["balatro_episode_archive"]["response"] + assert len(evaluated.loss_mask) == evaluated.response_length + assert sum(evaluated.loss_mask) == 0 + + +def test_startup_connection_failure_retries_before_any_policy_action(harness, monkeypatch): + run_episode(harness, decisions=2) # install the normal game/tokenizer fixtures + healthy = harness["_get_env_pool"]().acquire("unused") + healthy.steps = 0 + healthy.closed = False + failed = SimpleNamespace(closed=False) + failed.start = lambda seed: (_ for _ in ()).throw(ConnectionError("Connection refused")) + failed.close = lambda: setattr(failed, "closed", True) + acquired = [] + def acquire(seed): + acquired.append(seed) + return failed if len(acquired) == 1 else healthy + harness["_get_env_pool"] = lambda: SimpleNamespace(acquire=acquire) + async def no_sleep(seconds): + pass + monkeypatch.setattr(asyncio, "sleep", no_sleep) + args = SimpleNamespace(hf_checkpoint="unused", balatro_capture_screenshot=False) + result = asyncio.run(harness["generate_balatro_comparison"](args, Sample(prompt="RETRY-SEED"), {}, evaluation=True)) + assert acquired == ["RETRY-SEED", "RETRY-SEED"] + assert failed.closed and healthy.closed + assert healthy.steps == 2 + assert math.isfinite(result.reward) + + +def test_persistent_startup_failure_reports_seed_and_closes_every_sandbox(harness, monkeypatch): + environments = [] + def acquire(seed): + env = SimpleNamespace(closed=False) + env.start = lambda seed: (_ for _ in ()).throw(ConnectionError("Connection refused")) + env.close = lambda: setattr(env, "closed", True) + environments.append(env) + return env + harness["_get_env_pool"] = lambda: SimpleNamespace(acquire=acquire) + async def no_sleep(seconds): + pass + monkeypatch.setattr(asyncio, "sleep", no_sleep) + args = SimpleNamespace(hf_checkpoint="unused", balatro_capture_screenshot=False) + with pytest.raises(RuntimeError, match="RETRY-SEED after 3 attempts: Connection refused"): + asyncio.run(harness["generate_balatro_comparison"](args, Sample(prompt="RETRY-SEED"), {}, evaluation=True)) + assert len(environments) == 3 + assert all(env.closed for env in environments) + + +@pytest.mark.parametrize("reward", [None, float("nan"), float("inf")]) +def test_eval_rejects_unscored_results_before_trainer_aggregation(harness, reward): + async def generate(*args, **kwargs): + return SimpleNamespace(reward=reward, status=Sample.Status.ABORTED) + harness["generate_balatro_episode"] = generate + with pytest.raises(RuntimeError, match="incomplete/unscored episode for TEST-SEED"): + asyncio.run(harness["generate_balatro_comparison"](SimpleNamespace(), Sample(), {}, evaluation=True)) + + +def credit_sample(game, reward, returns, *, turn_start=1): + return SimpleNamespace(rollout_id=game, reward=reward, train_metadata=None, + metadata={"balatro": {"seed_group": "same"}, "policy_episode_reward": reward, "action_credit": [ + {"turn": turn_start + i, "token_start": 3*i, + "token_end": 3*i + 2, "return_to_go": value, "intervention_penalty": 0.0} + for i, value in enumerate(returns) + ]}) + + +def test_later_bad_decision_does_not_inherit_earlier_success(harness): + # A earned 9 then lost 1; B earned 0 then 2. Whole-game GRPO prefers A + # on both moves. Return-to-go prefers B on the second move. + samples = [credit_sample(1, 8, [8, -1]), credit_sample(2, 2, [2, 2])] + args = SimpleNamespace(balatro_credit_assignment="reward_to_go") + raw, episode_adv = harness["normalize_episode_rewards"](args, samples) + assert raw == [8, 2] and episode_adv[0] > 0 + assert [a["advantage"] for a in samples[0].train_metadata["balatro_action_advantages"]] == [2, -1] + assert [a["advantage"] for a in samples[1].train_metadata["balatro_action_advantages"]] == [-2, 1] + + +def test_intervention_progress_is_excluded_but_later_model_rewards_remain(harness): + # Model earns 10, a repair earns 14 minus its 2-point penalty, then the + # model earns a 100-point terminal bonus. The task score remains 122. + actions = [{"reward_before": 0, "invalid_events": 0}, + {"reward_before": 10, "invalid_events": 1}, + {"reward_before": 22, "invalid_events": 0}] + total = harness["_finalize_action_credit"]([{"actions": actions[:2]}, {"actions": actions[2:]}], 122) + assert total == 108 + assert [a["policy_return_to_go"] for a in actions] == [108, 98, 100] + assert [a["intervention_penalty"] for a in actions] == [0, -2, 0] + + +def test_rejected_actions_never_execute_repairs_or_fallbacks(harness): + harness["_parse_action"] = lambda text, state: None + harness["_repair_action"] = lambda *a: pytest.fail("Rejected output must not be repaired") + samples, seen, env = run_episode(harness, decisions=2) + assert env.closed and env.steps == 0 and len(seen) == 3 + assert samples[0].reward == -6 + assert samples[0].metadata["balatro"]["terminal_reason"] == "invalid_action_limit" + actions = [a for s in samples for a in s.metadata["action_credit"]] + assert [a["return_to_go"] for a in actions] == [-6, -4, -2] + assert [a["intervention_penalty"] for a in actions] == [-2, -2, -2] + assert sum(sum(s.loss_mask) for s in samples) == 6 + + +def test_rejected_action_stays_negative_even_when_peer_returns_are_worse(harness): + samples = [credit_sample(1, 108, [108, 98, 100]), + credit_sample(2, -100, [-100, -100, -100])] + samples[0].metadata["action_credit"][1]["intervention_penalty"] = -2 + harness["normalize_episode_rewards"](SimpleNamespace(balatro_credit_assignment="reward_to_go"), samples) + spans = samples[0].train_metadata["balatro_action_advantages"] + assert spans[0]["advantage"] > 0 and spans[2]["advantage"] > 0 + assert spans[1]["advantage"] == -5.0 + + +@pytest.mark.parametrize("spread", [0, 1, 100, 1_000_000]) +def test_invalid_penalty_does_not_shrink_with_reward_spread(harness, spread): + samples = [credit_sample(1, spread, [spread]), credit_sample(2, -spread, [-spread])] + samples[0].metadata["action_credit"][0]["intervention_penalty"] = -2 + raw, _ = harness["normalize_episode_rewards"](SimpleNamespace(balatro_credit_assignment="reward_to_go"), samples) + assert raw == [spread, -spread] + assert samples[0].train_metadata["balatro_action_advantages"][0]["advantage"] == -5 + + +def test_invalid_penalty_preserves_larger_negative_future_credit(harness): + samples = [credit_sample(1, 100, [100, -50]), credit_sample(2, 100, [100, 50])] + samples[0].metadata["action_credit"][1]["intervention_penalty"] = -2 + harness["normalize_episode_rewards"](SimpleNamespace(balatro_credit_assignment="reward_to_go"), samples) + assert samples[0].train_metadata["balatro_action_advantages"][1]["advantage"] == -100 + + +def test_invalid_penalty_weight_is_configurable_without_changing_valid_credit(harness): + samples = [credit_sample(1, 8, [8, -1]), credit_sample(2, 2, [2, 2])] + samples[0].metadata["action_credit"][1]["intervention_penalty"] = -2 + args = SimpleNamespace(balatro_credit_assignment="reward_to_go", balatro_invalid_action_weight=10) + harness["normalize_episode_rewards"](args, samples) + assert [a["advantage"] for a in samples[0].train_metadata["balatro_action_advantages"]] == [2, -10] + assert [a["advantage"] for a in samples[1].train_metadata["balatro_action_advantages"]] == [-2, 1] + + +@pytest.mark.parametrize("weight", [0, -1, float("nan"), float("inf"), None, True, "bad"]) +def test_invalid_penalty_configuration_fails_closed(harness, weight): + with pytest.raises(ValueError, match="finite and positive"): + harness["_invalid_action_weight"](SimpleNamespace(balatro_invalid_action_weight=weight)) + + +def test_advantage_recentering_rejected_before_launch(harness): + args = SimpleNamespace(calculate_per_token_loss=False, extra_config={ + "balatro_credit_assignment": "reward_to_go", + "custom_advantage_function_path": "balatro_credit.compute_advantages", + "normalize_advantages": True, + }) + with pytest.raises(ValueError, match="normalize_advantages=False"): + harness["_validate_balatro_loss_config"](args) + + +@pytest.mark.parametrize("penalty", [1, float("nan"), float("inf")]) +def test_intervention_penalties_fail_closed(harness, penalty): + samples = [credit_sample(1, 8, [8]), credit_sample(2, 2, [2])] + samples[0].metadata["action_credit"][0]["intervention_penalty"] = penalty + with pytest.raises(ValueError, match="intervention penalty"): + harness["normalize_episode_rewards"](SimpleNamespace(balatro_credit_assignment="reward_to_go"), samples) + + +def test_action_credit_is_invariant_to_segmentation_and_sample_order(harness): + args = SimpleNamespace(balatro_credit_assignment="reward_to_go") + whole = [credit_sample(1, 8, [8, 4, -1]), credit_sample(2, 2, [2, 2])] + split = [credit_sample(2, 2, [2], turn_start=2), credit_sample(1, 8, [4, -1], turn_start=2), + credit_sample(1, 8, [8]), credit_sample(2, 2, [2])] + for samples in (whole, split): + for sample in samples: + for action in sample.metadata["action_credit"]: + if sample.rollout_id == 1 and action["turn"] == 3: + action["intervention_penalty"] = -2 + harness["normalize_episode_rewards"](args, samples) + def by_turn(samples): + return {(s.rollout_id, a["turn"]): v["advantage"] for s in samples + for a, v in zip(s.metadata["action_credit"], s.train_metadata["balatro_action_advantages"])} + assert by_turn(split) == by_turn(whole) + + +@pytest.mark.parametrize("case", ["missing", "duplicate", "gap", "nonfinite", "singleton"]) +def test_invalid_action_credit_fails_closed(harness, case): + samples = [credit_sample(1, 8, [8, -1]), credit_sample(2, 2, [2, 2])] + if case == "missing": + samples[0].metadata.pop("action_credit") + if case == "duplicate": + samples.append(deepcopy(samples[0])) + if case == "gap": + samples[0].metadata["action_credit"][1]["turn"] = 3 + if case == "nonfinite": + samples[0].metadata["action_credit"][0]["return_to_go"] = math.nan + if case == "singleton": + samples = samples[:1] + with pytest.raises(ValueError): + harness["normalize_episode_rewards"](SimpleNamespace(balatro_credit_assignment="reward_to_go"), samples) + + +def test_generation_sends_bounded_ids_and_retains_serving_log_probabilities(harness, monkeypatch): + http = ModuleType("slime.utils.http_utils") + requests = [] + + async def post(url, payload): + requests.append(payload) + return {"text": "action", "meta_info": { + "finish_reason": {"type": "stop"}, + "output_token_logprobs": [[-0.25, 101, None], [-0.5, 102, None]], + }} + + http.post = post + monkeypatch.setitem(sys.modules, "slime.utils.http_utils", http) + args = SimpleNamespace(balatro_context_len=512, balatro_segment_tokens=512, + sglang_router_ip="unused", sglang_router_port=1234) + output = asyncio.run(harness["_generate_action"](args, {"max_new_tokens": 999}, [1] * 288, "hand")) + assert output == ([101, 102], "action", "stop", [-0.25, -0.5], [1, 1]) + assert requests[0]["sampling_params"]["max_new_tokens"] == 96 + assert not any(requests[0]["sampling_params"].get(key) is not None + for key in ("regex", "json_schema", "ebnf")) + assert len(requests[0]["input_ids"]) + 96 + 128 == 512 + assert asyncio.run(harness["_generate_action"](args, {}, [1] * 512, "hand"))[1] is None + assert len(requests) == 1 + + +@pytest.mark.parametrize("constraint", ["regex", "json_schema", "ebnf"]) +def test_generation_rejects_constraints_the_trainer_cannot_replay(harness, monkeypatch, constraint): + http = ModuleType("slime.utils.http_utils") + async def post(*args, **kwargs): + pytest.fail("A probability-mismatched rollout must not be submitted") + http.post = post + monkeypatch.setitem(sys.modules, "slime.utils.http_utils", http) + args = SimpleNamespace(balatro_context_len=512, balatro_segment_tokens=512) + with pytest.raises(ValueError, match="trainer does not replay grammar masks"): + asyncio.run(harness["_generate_action"](args, {constraint: "constraint"}, [1], "hand")) + + +def test_trainer_refuses_segment_weighting_that_counts_long_games_more(harness): + with pytest.raises(ValueError, match="per-game mean"): + harness["_validate_balatro_trainer"](SimpleNamespace(calculate_per_token_loss=True), Sample) + + +@pytest.mark.parametrize("params", [ + {"temperature": 0.0}, {"temperature": 0.7}, {"temperature": float("nan")}, + {"top_p": 0.9}, {"top_k": 50}, {"min_p": 0.1}, + {"presence_penalty": 1.0}, {"frequency_penalty": 1.0}, + {"repetition_penalty": 1.1}, {"logit_bias": {"123": 10}}, + {"custom_logit_processor": "processor"}, {"regex": "json-only"}, +]) +def test_mismatched_training_sampling_rejected_before_game_start(harness, params): + # Empty args would fail later trainer/context validation; this must reject + # the distribution mismatch first, before acquiring any environment. + with pytest.raises(ValueError, match="Balatro training"): + asyncio.run(harness["generate_balatro_episode"](SimpleNamespace(), Sample(), params)) + + +def test_greedy_evaluation_is_allowed_but_grammar_still_rejected(harness): + validate = harness["_validate_balatro_sampling_params"] + validate({"temperature": 0.0, "top_p": 1.0, "top_k": -1}, evaluation=True) + with pytest.raises(ValueError, match="grammar masks"): + validate({"temperature": 0.0, "regex": "json-only"}, evaluation=True) + + +@pytest.mark.parametrize("pairs", [[], [[float("nan"), 101, None]], [[float("inf"), 101, None]]]) +def test_missing_or_nonfinite_serving_probabilities_fail(harness, monkeypatch, pairs): + http = ModuleType("slime.utils.http_utils") + async def post(*args, **kwargs): + return {"text": "action", "meta_info": { + "finish_reason": {"type": "stop"}, "output_token_logprobs": pairs, + }} + http.post = post + monkeypatch.setitem(sys.modules, "slime.utils.http_utils", http) + args = SimpleNamespace(balatro_context_len=512, balatro_segment_tokens=512, + sglang_router_ip="unused", sglang_router_port=1234) + with pytest.raises(RuntimeError, match="token log probabilities"): + asyncio.run(harness["_generate_action"](args, {}, [1], "hand")) + + +def test_checkpoint_crossing_reset_has_one_reward_and_local_token_offsets(harness): + segments = [ + {"actions": [{"token_start": 0, "token_end": 2, "episode_token_start": 0, "episode_token_end": 2}]}, + {"actions": [{"token_start": 0, "token_end": 3, "episode_token_start": 30, "episode_token_end": 33}]}, + ] + result = harness["_segment_reward_events"]([{"token_start": 0, "token_end": 40, "reward": 8}], segments) + assert result[0] == [] and len(result[1]) == 1 + assert result[1][0]["token_start"] == 0 and result[1][0]["token_end"] == 3 + assert result[1][0]["episode_token_end"] == 40 + assert result[1][0]["segment_spans"] == [ + {"segment_id": 0, "token_start": 0, "token_end": 2}, + {"segment_id": 1, "token_start": 0, "token_end": 3}, + ] + + +def test_snapshot_keeps_inventory_and_zero_counters_in_every_phase(harness): + state = game_state(0) + state["state"] = "SHOP" + state["jokers"]["cards"][0]["state"] = {"debuff": True} + before = deepcopy(state) + compact = json.loads(harness["_compact_state"](state, limit=1)) + assert compact["owned_vouchers"] == [{"id": "v_blank", "effect": ""}] + assert compact["jokers"]["items"][0]["counters"]["mult"] == 0 + assert compact["jokers"]["items"][0]["debuff"] is True + assert compact["hand_values"]["Pair"] == [3, 40, 4] + assert "owned_deck" in compact and "deck" in compact and "truncated" not in compact + assert state == before + + +def test_hidden_card_cannot_be_identified_by_key_instance_or_effect(harness): + state = game_state() + state["hand"]["cards"][0]["state"] = {"hidden": True} + compact = json.loads(harness["_compact_state"](state)) + assert compact["hand"] == [{"hidden": True}] + + +def test_hidden_jokers_keep_known_composition_without_slot_mapping(harness): + state = game_state() + visible = deepcopy(state["jokers"]["cards"][0]) + state["jokers"]["cards"][0]["state"] = {"hidden": True} + state["joker_inventory"] = {"cards": [{**visible, "id": 0}]} + compact = json.loads(harness["_compact_state"](state)) + assert compact["jokers"]["items"] == [{"hidden": True}] + assert compact["joker_inventory"][0]["id"] == "j_green_joker" + assert "instance" not in compact["joker_inventory"][0] + + +def test_delta_never_silently_keeps_stale_shop_fields(harness): + previous = {"phase": "shop", "shop": [{"id": "j_joker"}], "packs": [1], "reroll_cost": 5, "money": 4} + current = {"phase": "BLIND_SELECT", "money": 4} + delta = harness["_delta_payload"](previous, current) + assert delta["shop"] is None and delta["packs"] is None and delta["reroll_cost"] is None + + +def test_segment_count_does_not_change_game_advantage(harness): + def sample(game, reward): + return SimpleNamespace(rollout_id=game, reward=reward, metadata={"balatro": {"seed_group": "same"}}) + original = [sample(1, 10), sample(2, 20)] + split = [sample(1, 10)] * 3 + [sample(2, 20)] * 7 + _, expected = harness["normalize_episode_rewards"](None, original) + _, actual = harness["normalize_episode_rewards"](None, split) + assert actual == [expected[0]] * 3 + [expected[1]] * 7 + + +def test_real_lua_snapshot_preserves_public_mechanics_without_mutation(): + lua = shutil.which("luajit") + if not lua: + pytest.skip("LuaJIT required") + script = r''' +local gs = dofile("src/lua/utils/gamestate.lua") +function pseudorandom() error("must not advance RNG") end +local function card(id, key, ability) + return {sort_id=id, config={card_key=key,card={suit="Spades",value="Ace"}}, + ability=ability, seal="Gold", generate_UIBox_ability_table=function() return {main={}} end} +end +local a=card(10,"S_A",{perma_bonus=30,played_this_ante=true,forced_selection=true,private_seed=999}) +local b=card(11,"S_A",{perma_bonus=0}) +G={STATE=1,STATES={SELECTING_HAND=1},playing_cards={a,b}, + hand={cards={a},config={card_count=1,card_limit=8}}, + deck={cards={b},config={card_count=1,card_limit=52}}, + GAME={current_round={hands_played=2,discards_used=1},last_tarot_planet="c_devil", + used_vouchers={v_blank=true},tags={{key="tag_double",ability={orbital_hand="Pair",private_seed=123}}}, + blind={only_hand="Pair",hands={Pair=true,Flush=false}},pseudorandom={seed="SECRET"}}} +local first=gs.get_gamestate() +assert(first.snapshot_version==1) +assert(first.cards.count==1 and first.owned_cards.count==2) +assert(first.hand.cards[1].id==10 and first.hand.cards[1].counters.perma_bonus==30) +assert(first.hand.cards[1].counters.played_this_ante and first.hand.cards[1].counters.forced_selection) +assert(first.hand.cards[1].counters.private_seed==nil) +assert(first.owned_cards.cards[1].id==0 and first.owned_cards.cards[1].state.hidden==nil) +assert(first.run_state.last_tarot_planet=="c_devil" and first.run_state.round.hands_played==2) +assert(first.blind_state.only_hand=="Pair" and first.blind_state.played_hands.Pair) +assert(first.tags[1].key=="tag_double" and first.tags[1].ability.private_seed==nil) +assert(first.used_vouchers.v_blank=="") +a.ability.perma_bonus=60 +assert(first.hand.cards[1].counters.perma_bonus==30) +assert(gs.get_gamestate().hand.cards[1].counters.perma_bonus==60) +assert(a.sort_id==10 and a.seal=="Gold") +local joker={sort_id=99,config={center_key="j_green_joker"},ability={set="Joker",mult=7}, + facing="back",generate_UIBox_ability_table=function() return {main={}} end} +G.jokers={cards={joker},config={card_count=1,card_limit=5}} +local hidden=gs.get_gamestate() +assert(hidden.jokers.cards[1].state.hidden) +assert(hidden.joker_inventory.cards[1].id==0 and hidden.joker_inventory.cards[1].key=="j_green_joker") +assert(hidden.joker_inventory.cards[1].state.hidden==nil) +print("ok") +''' + result = subprocess.run([lua, "-"], input=script, cwd=ROOT, text=True, capture_output=True, check=True) + assert result.stdout.strip() == "ok" + + +@pytest.mark.parametrize("cp_size", [1, 4]) +def test_pinned_trainer_reducer_weights_unequal_segments_across_microbatches(harness, monkeypatch, cp_size): + """Optional integration with sources read from the actual pinned image.""" + source_dir = os.environ.get("BALATRO_SLIME_SOURCE") + if not source_dir: + pytest.skip("Set BALATRO_SLIME_SOURCE to the pinned image's extracted slime source") + torch = pytest.importorskip("torch") + path = Path(source_dir) / "backends/megatron_utils/cp_utils.py" + tree = ast.parse(path.read_text()) + selected = [n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name in {"get_sum_of_sample_mean", "get_logits_and_tokens_offset_with_cp"}] + mpu = SimpleNamespace(get_context_parallel_world_size=lambda: cp_size) + from collections.abc import Callable + ns = {"torch": torch, "mpu": mpu, "Callable": Callable} + exec(compile(ast.Module(body=selected, type_ignores=[]), str(path), "exec"), ns) + reduce = ns["get_sum_of_sample_mean"] + import inspect + monkeypatch.setattr(inspect, "getfile", lambda cls: str(Path(source_dir) / "utils/types.py")) + harness["_validate_balatro_trainer"](SimpleNamespace(calculate_per_token_loss=False), Sample) + + def contribution(values, denominator): + x = torch.tensor(values) + length = len(values) + total = length + 4 + result = torch.tensor(0.) + for rank in range(cp_size): + mpu.get_context_parallel_rank = lambda: rank + local = x + if cp_size > 1: + *_, offsets = ns["get_logits_and_tokens_offset_with_cp"](total, length) + local = torch.cat([x[lo - 4:hi - 4] for lo, hi in offsets]) + result = result + reduce([total], [length], [torch.ones(length)], [torch.tensor(float(denominator))], False)(local) + return result + + # Two games: A losses [1,3,5,7], B losses [10,20]. Split A into + # unequal segments in different microbatches, keeping its full denominator. + a_first = contribution([1.], 4) + a_later = contribution([3., 5., 7.], 4) + b = contribution([10., 20.], 2) + assert ((a_first + a_later + b) / 2).item() == pytest.approx((4 + 15) / 2) diff --git a/tests/test_hand_recognition.py b/tests/test_hand_recognition.py new file mode 100644 index 00000000..9362bef6 --- /dev/null +++ b/tests/test_hand_recognition.py @@ -0,0 +1,60 @@ +"""Regressions from actual losses; helpers must use only visible card faces.""" + +import ast +import json +from pathlib import Path +from typing import Any + +import pytest + +ROOT = Path(__file__).resolve().parents[1] + + +@pytest.fixture +def compact(): + tree = ast.parse((ROOT / "batro_modal_rl.py").read_text()) + node = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == "_compact_state") + ns = {"json": json, "Any": Any} + exec(compile(ast.Module(body=[node], type_ignores=[]), "compact", "exec"), ns) + def render(cards): + return json.loads(ns["_compact_state"]({ + "state": "SELECTING_HAND", "hand": {"cards": cards}, + "round": {}, "blinds": {}, + })) + return render + + +@pytest.mark.parametrize("case", json.loads((ROOT / "tests/fixtures/broad_layover_decisions.json").read_text())) +def test_recorded_winning_combination_is_explicit(compact, case): + state = compact([{"key": key} for key in case["hand"]]) + groups = state["hand_groups"]["same_rank"] + assert sorted(i for indices in groups.values() for i in indices) == case["winning_cards"] + assert case["winning_score"] >= case["blind"]["score"][1] - case["blind"]["score"][0] + + +def test_groups_reindex_when_hand_changes(compact): + before = compact([{"key": k} for k in ["H_A", "H_K", "C_K", "D_K"]]) + after = compact([{"key": k} for k in ["D_K", "H_A", "C_K", "H_K"]]) + assert before["hand_groups"]["same_rank"]["K"] == [1, 2, 3] + assert after["hand_groups"]["same_rank"]["K"] == [0, 2, 3] + + +def test_hidden_cards_and_stone_cards_do_not_supply_ranks(compact): + state = compact([ + {"key": "H_K"}, + {"key": "C_K", "state": {"hidden": True}}, + {"key": "D_K", "modifier": {"enhancement": "STONE"}}, + ]) + assert not state.get("hand_groups", {}).get("same_rank") + + +def test_straight_examples_and_suit_groups_use_current_visible_indices(compact): + state = compact([{"key": k} for k in ["H_A", "H_K", "S_Q", "H_J", "D_J", "H_T", "S_8", "C_2"]]) + assert state["hand_groups"]["straight_examples"] == [[5, 3, 2, 1, 0]] + assert state["hand_groups"]["same_suit"] == {"H": [0, 1, 3, 5]} + + +def test_wheel_and_duplicate_ranks(compact): + state = compact([{"key": k} for k in ["S_A", "H_2", "C_3", "D_4", "S_5", "C_5"]]) + assert state["hand_groups"]["straight_examples"] == [[0, 1, 2, 3, 4]] + assert state["hand_groups"]["same_rank"] == {"5": [4, 5]} diff --git a/tests/test_joker_scoring_state.py b/tests/test_joker_scoring_state.py new file mode 100644 index 00000000..3e1b34dd --- /dev/null +++ b/tests/test_joker_scoring_state.py @@ -0,0 +1,75 @@ +"""CPU-only checks of visible joker targets from Lua RPC through compact traces.""" + +import ast +import json +import shutil +import subprocess +from pathlib import Path +from typing import Any + +import pytest + +ROOT = Path(__file__).resolve().parents[1] + + +def test_rpc_reads_targets_without_advancing_randomness(): + lua = shutil.which("luajit") + if not lua: + pytest.skip("LuaJIT is needed for the RPC serializer test") + script = r''' +local gs = dofile("src/lua/utils/gamestate.lua") +function pseudorandom() error("A state read must not roll RNG") end +function pseudoseed() error("A state read must not roll RNG") end +function card(key, extra) + return {config={center_key=key}, ability={set="Joker",extra=extra}, + generate_UIBox_ability_table=function() return {main={}} end} +end +G = {STATE=1,STATES={SELECTING_HAND=1},GAME={current_round={ + idol_card={rank="King",suit="Hearts",id=13,private_seed="not exported"}, + ancient_card={suit="Clubs"},mail_card={rank="Ace",id=14}, + castle_card={suit="Diamonds"},unrelated_rng=999}}, + jokers={config={card_count=5,card_limit=5},cards={ + card("j_idol"),card("j_ancient"),card("j_mail"), + card("j_castle",{chips=27,chip_mod=3}),card("j_joker")}}} +local first=gs.get_gamestate().jokers.cards +assert(first[1].scoring_state.target.rank=="K") +assert(first[1].scoring_state.target.rank_id==13) +assert(first[1].scoring_state.target.suit=="H") +assert(first[1].scoring_state.target.private_seed==nil) +assert(first[2].scoring_state.target.suit=="C") +assert(first[3].scoring_state.target.rank=="A") +assert(first[4].scoring_state.target.suit=="D") +assert(first[4].scoring_state.chips==27 and first[4].scoring_state.chip_gain==3) +assert(first[5].scoring_state==nil) +G.GAME.current_round.idol_card={rank="10",suit="Spades",id=10} +local second=gs.get_gamestate().jokers.cards +assert(second[1].scoring_state.target.rank=="T") +assert(second[1].scoring_state.target.suit=="S") +assert(first[1].scoring_state.target.rank=="K", "snapshot must not alias live tables") +G.GAME.current_round=nil +assert(gs.get_gamestate().jokers.cards[1].scoring_state==nil) +print("ok") +''' + result = subprocess.run([lua, "-"], input=script, text=True, cwd=ROOT, capture_output=True, check=True) + assert result.stdout.strip() == "ok" + + +@pytest.mark.parametrize("limit", [6000, 1]) +def test_compact_trace_preserves_targets_even_when_deck_is_omitted(limit): + tree = ast.parse((ROOT / "batro_modal_rl.py").read_text()) + node = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == "_compact_state") + namespace = {"Any": Any, "json": json} + exec(compile(ast.Module(body=[node], type_ignores=[]), "batro_modal_rl.py", "exec"), namespace) + target = {"target": {"rank": "K", "rank_id": 13, "suit": "H"}} + state = {"state": "SELECTING_HAND", "jokers": {"count": 1, "limit": 5, "cards": [{"key": "j_idol", "value": {"effect": "X2 Mult on the current target"}, "scoring_state": target}]}} + before = json.dumps(state) + compact = json.loads(namespace["_compact_state"](state, limit)) + assert compact["jokers"]["items"][0]["scoring_state"] == target + assert json.dumps(state) == before + + +def test_rpc_schema_describes_optional_scoring_state(): + schema = json.loads((ROOT / "src/lua/utils/openrpc.json").read_text()) + card = schema["components"]["schemas"]["Card"] + assert "scoring_state" in card["properties"] + assert "scoring_state" not in card["required"] diff --git a/tests/test_pack_legality.py b/tests/test_pack_legality.py new file mode 100644 index 00000000..19557454 --- /dev/null +++ b/tests/test_pack_legality.py @@ -0,0 +1,95 @@ +"""CPU-only regression of the real pack endpoint's game-level legality gate.""" + +import shutil +import subprocess +import zipfile +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[1] + + +@pytest.mark.parametrize("real_game_rules", [False, True]) +def test_pack_checks_consumable_legality_before_using_card(real_game_rules): + lua = shutil.which("luajit") + if not lua: + pytest.skip("LuaJIT is needed for the endpoint regression") + rules = "" + if real_game_rules: + archive = Path.home() / "Library/Application Support/Steam/steamapps/common/Balatro/Balatro.app/Contents/Resources/Balatro.love" + if not archive.exists(): + pytest.skip("Optional check against the installed game's own legality predicate") + with zipfile.ZipFile(archive) as game: + source = game.read("card.lua").decode() + # Exercise the installed game's actual rule without copying proprietary + # game code into the repository. UI/animation dependencies stay stubbed. + predicate = source.split("function Card:can_use_consumeable", 1)[1].split("function Card:check_use", 1)[0] + rules = "Card = {}\nfunction Card:can_use_consumeable" + predicate + script = rules + r''' +package.preload.socket = function() return {gettime=function() return 0 end} end +SMODS = {load_file=function() return function() + return {format_playing_cards=function() return "targets" end} +end end} +function sendDebugMessage() end +function Event(event) return event end +BB_ERROR_NAMES = {NOT_ALLOWED="NOT_ALLOWED", BAD_REQUEST="BAD_REQUEST"} +BB_GAMESTATE = {get_gamestate=function() return {ok=true} end} +G = {STATES={SMODS_BOOSTER_OPENED=1,SHOP=2,BLIND_SELECT=3}} +local endpoint = dofile("src/lua/endpoints/pack.lua") +local function run(set, count, usable, space_blocked, targets) + local used, checked, responses, events = 0, 0, {}, {} + local card = {ability={set=set,name="Judgement",consumeable=set~="Joker" and set~="Default" and {}}, + config={center={key="c_judgement"}}, + can_use_consumeable=function(self) + checked=checked+1 + if targets then assert(#G.hand.highlighted == #targets) end + if usable~=nil then return usable end + if Card then return Card.can_use_consumeable(self) end + return G.jokers.config.card_count < G.jokers.config.card_limit + end, + check_use=function() return space_blocked end} + G.STATE=1; G.STATE_COMPLETE=true; G.GAME={pack_choices=1} + G.P_CENTERS={c_judgement={config=targets and {max_highlighted=1} or {}}} + G.CONTROLLER={locked=false} + G.jokers={cards={},config={card_count=count,card_limit=5}} + for i=1,count do G.jokers.cards[i]={} end + G.pack_cards={cards={card},config={card_count=1}} + G.deck={config={card_count=1}} + G.hand={cards={{T={x=1}}},highlighted={},config={card_limit=1},T={x=1}, + add_to_highlighted=function(self,c) table.insert(self.highlighted,c) end, + remove_from_highlighted=function(self,c) table.remove(self.highlighted) end} + G.E_MANAGER={add_event=function(self,e) table.insert(events,e) end} + G.FUNCS={use_card=function() + used=used+1; G.jokers.config.card_count=count+1 + G.STATE=2; G.pack_cards=nil + end} + endpoint.execute({card=0,targets=targets},function(r) table.insert(responses,r) end) + for _=1,3 do + local pending=events; events={} + for _,e in ipairs(pending) do if not e.func() then table.insert(events,e) end end + end + assert(#responses==1, "must respond exactly once") + return used,checked,responses[1] +end +local used,checked,response=run("Tarot",5) +assert(used==0 and checked==1 and response.name=="NOT_ALLOWED") +assert(G.jokers.config.card_count==5 and G.GAME.pack_choices==1 and G.pack_cards~=nil) +used,checked,response=run("Tarot",4) +assert(used==1 and checked==1 and response.ok) +-- Spectral restrictions and space checks must not be bypassed either. +used,checked,response=run("Spectral",4,false) +assert(used==0 and response.name=="NOT_ALLOWED") +used,checked,response=run("Planet",4,true,true) +assert(used==0 and response.name=="NOT_ALLOWED") +-- Non-joker-generating consumables stay legal at full joker capacity. +used,checked,response=run("Tarot",5,true,false,{0}) +assert(used==1 and checked==1 and response.ok) +for _,set in ipairs({"Joker","Default"}) do + used,checked,response=run(set,4) + assert(used==1 and checked==0 and response.ok) +end +print("ok") +''' + result = subprocess.run([lua, "-"], input=script, text=True, cwd=ROOT, capture_output=True, check=True) + assert result.stdout.strip() == "ok" diff --git a/tests/test_pack_selling.py b/tests/test_pack_selling.py new file mode 100644 index 00000000..bb797600 --- /dev/null +++ b/tests/test_pack_selling.py @@ -0,0 +1,189 @@ +"""Pack replacement regressions: grammar, legal hints, and Lua sale/selection.""" + +import ast +import copy +import json +import re +import shutil +import subprocess +from pathlib import Path +from types import SimpleNamespace +from typing import Any + +import pytest + +ROOT = Path(__file__).resolve().parents[1] + + +@pytest.fixture +def harness(): + tree = ast.parse((ROOT / "batro_modal_rl.py").read_text()) + functions = { + "_compact_state", "_area_items", "_slot_usage", "_sellable_indices", + "_purchasable_indices", "_consumable_target_bounds", "_consumable_requires_joker", "_consumable_available", + "_target_action_help", "_action_help", "_action_is_locally_legal", "_action_regex", + } + constants = {"_INDEX", "_INDEX_LIST", "_INDEX_LIST_5", "_ALLOWED_METHODS", "_ACTION_REGEX_BY_STATE", + "_CONSUMABLE_TARGET_BOUNDS", "_CONSUMABLE_REQUIRES_JOKER"} + nodes = [node for node in tree.body if ( + isinstance(node, ast.FunctionDef) and node.name in functions + or isinstance(node, ast.Assign) and any(isinstance(t, ast.Name) and t.id in constants for t in node.targets) + )] + ns = {"Any": Any, "json": json, "ToolCall": SimpleNamespace, "_CONSUMABLE_TARGET_BOUNDS": {}, "_CONSUMABLE_REQUIRES_JOKER": set()} + exec(compile(ast.Module(body=nodes, type_ignores=[]), "batro_modal_rl.py", "exec"), ns) + return ns + + +def pack_state(): + return { + "state": "SMODS_BOOSTER_OPENED", + "jokers": {"count": 5, "limit": 5, "cards": [ + {"key": "j_joker", "cost": {"sell": 1}, "modifier": {"eternal": i == 0}} + for i in range(5) + ]}, + "consumables": {"count": 1, "limit": 2, "cards": [{"key": "c_fool", "cost": {"sell": 1}}]}, + "pack": {"count": 1, "cards": [{"key": "j_blueprint"}]}, + } + + +def test_pack_exposes_sale_then_unlocks_selection(harness): + state = pack_state() + sell = SimpleNamespace(name="sell", arguments={"joker": 1}) + pick = SimpleNamespace(name="pack", arguments={"card": 0}) + assert "sell" in harness["_ALLOWED_METHODS"][state["state"]] + for kind in ("joker", "consumable"): + assert re.fullmatch(harness["_action_regex"](state["state"]), f'{{"method":"sell","params":{{"{kind}":1}}}}') + help_text = harness["_action_help"](state) + assert "sell joker=one of [1, 2, 3, 4]" in help_text + assert "sell consumable=one of [0]" in help_text + assert "keeps pack open" in help_text + assert "pack card=" not in help_text + assert harness["_action_is_locally_legal"](sell, state) + assert not harness["_action_is_locally_legal"](pick, state) + after = copy.deepcopy(state) + after["jokers"]["cards"].pop(1) + after["jokers"]["count"] = 4 + assert "pack card=0" in harness["_action_help"](after) + assert harness["_action_is_locally_legal"](pick, after) + assert after["pack"] == state["pack"] + + +@pytest.mark.parametrize("index", [0, -1, 5, True, "1"]) +def test_pack_sale_rejects_eternal_or_invalid_index(harness, index): + assert not harness["_action_is_locally_legal"]( + SimpleNamespace(name="sell", arguments={"joker": index}), pack_state(), + ) + + +@pytest.mark.parametrize("phase", ["SHOP", "SELECTING_HAND", "BLIND_SELECT"]) +def test_existing_sale_phases(harness, phase): + state = {**pack_state(), "state": phase} + assert harness["_action_is_locally_legal"]( + SimpleNamespace(name="sell", arguments={"joker": 1}), state, + ) is (phase != "BLIND_SELECT") + + +@pytest.mark.parametrize("case", json.loads((ROOT / "tests/fixtures/consumable_capacity_failures.json").read_text())) +def test_recorded_engine_rejections_are_never_advertised_or_accepted(harness, case): + state = case["state"] + action = SimpleNamespace(name=case["action"]["method"], arguments=case["action"]["params"]) + parameter = "consumable" if action.name == "use" else "card" + hint = f"{action.name} {parameter}={action.arguments[parameter]}" + assert hint not in harness["_action_help"](state) + assert not harness["_action_is_locally_legal"](action, state) + # Selling a joker restores both the hint and the actual legal action. + after = copy.deepcopy(state) + after["jokers"]["cards"].pop() + after["jokers"]["count"] -= 1 + assert hint in harness["_action_help"](after) + assert harness["_action_is_locally_legal"](action, after) + + +@pytest.mark.parametrize("phase", ["SHOP", "SELECTING_HAND"]) +def test_owned_consumable_gate_preserves_legal_uses_and_eternal_sales(harness, phase): + state = {**pack_state(), "state": phase, + "round": {"hands_left": 4, "discards_left": 3}, + "hand": {"cards": [{"key": "S_A"}]}, + "consumables": {"cards": [{"key": "c_wraith"}, {"key": "c_mercury"}]}} + hint = harness["_action_help"](state) + assert "use consumable=0" not in hint + assert "use consumable=1" in hint + assert "sell joker=one of [1, 2, 3, 4]" in hint + assert harness["_action_is_locally_legal"](SimpleNamespace(name="use", arguments={"consumable": 1}), state) + assert not harness["_action_is_locally_legal"](SimpleNamespace(name="use", arguments={"consumable": True}), state) + + +def test_lua_sale_preserves_pack_and_allows_replacement(): + lua = shutil.which("luajit") + if not lua: + pytest.skip("LuaJIT required") + script = r''' +package.preload.socket=function() return {gettime=function() return 0 end} end +SMODS={load_file=function() return function() return {} end end} +function sendDebugMessage() end +function Event(e) return e end +BB_ERROR_NAMES={NOT_ALLOWED="NOT_ALLOWED",BAD_REQUEST="BAD_REQUEST"} +G={STATES={SMODS_BOOSTER_OPENED=1,SHOP=2,SELECTING_HAND=3,BLIND_SELECT=4}, + STATE=1,STATE_COMPLETE=true,GAME={dollars=5,pack_choices=2},P_CENTERS={}} +local function card(id,eternal) + return {sort_id=id,sell_cost=2,ability={name="Joker",set="Joker",eternal=eternal}, + can_sell_card=function(self) return not self.ability.eternal end} +end +G.jokers={cards={card(1,true),card(2),card(3),card(4),card(5)},config={card_count=5,card_limit=5}} +G.consumeables={cards={card(6)},config={card_count=1,card_limit=2}} +G.pack_cards={cards={card(7),card(8)},config={card_count=2}} +local original_pack=G.pack_cards +local events={} +G.E_MANAGER={add_event=function(self,e) table.insert(events,e) end} +G.FUNCS={sell_card=function(e) + for _,area in ipairs({G.jokers,G.consumeables}) do + for i,c in ipairs(area.cards) do + if c==e.config.ref_table then + table.remove(area.cards,i);area.config.card_count=#area.cards + G.GAME.dollars=G.GAME.dollars+c.sell_cost + return + end + end + end +end,use_card=function(e) + assert(#G.jokers.cards 0 + assert recipe.load == "/checkpoints/proof" + assert recipe.extra_config["ckpt_step"] == 0 + + +@pytest.fixture +def launcher(): + source = Path(__file__).resolve().parents[1] / "batro_modal_rl.py" + tree = ast.parse(source.read_text()) + selected = { + "_remaining_rollouts", "_launch_training", "_select_resume_checkpoint", + "_checkpoint_iteration", "_validate_balatro_loss_config", "_invalid_action_weight", + "_validate_balatro_sampling_params", "_validate_balatro_sampling_config", + } + module = ast.Module(body=[ + node for node in tree.body + if isinstance(node, ast.FunctionDef) and node.name in selected + ], type_ignores=[]) + pin = Mock() + manifests = Mock(return_value={"shard": 100}) + checkpoints = Mock() + context = Mock(return_value=(49152, 8192)) + namespace = { + "TrainConfig": object, "TrainingRun": object, "Checkpoint": object, "Any": object, + "Path": Path, "PurePosixPath": PurePosixPath, "json": json, "os": os, "re": re, + "_pin_resume_checkpoint": pin, + "_checkpoint_manifest": manifests, + "_checkpoint_manifest_health": Mock(return_value=(True, "complete")), + "_validate_balatro_context_config": context, + } + exec(compile(module, str(source), "exec"), namespace) + return SimpleNamespace( + remaining=namespace["_remaining_rollouts"], + launch=namespace["_launch_training"], pin=pin, + select=namespace["_select_resume_checkpoint"], + checkpoints=checkpoints, manifests=manifests, + context=context, + ) + + +@pytest.mark.parametrize("target", [1, 199, 200]) +def test_empty_resume_rejected(launcher, target): + with pytest.raises(ValueError, match="at least --num-rollout 201"): + launcher.remaining(199, target) + + +@pytest.mark.parametrize("target,expected", [(201, 1), (400, 200)]) +def test_resume_count(launcher, target, expected): + assert launcher.remaining(199, target) == expected + + +def test_checkpoint_zero_is_also_exclusive(launcher): + with pytest.raises(ValueError): + launcher.remaining(0, 1) + assert launcher.remaining(0, 2) == 1 + + +def test_receipt_prevents_duplicate_submission(launcher, tmp_path): + receipt = tmp_path / "launch.json" + run = SimpleNamespace(training_run_id="run", modal_app_id="app", function_call_id="fc") + checkpoint = SimpleNamespace(path="/checkpoints/parent/iter_0000399") + recipe = SimpleNamespace(load="", no_load_optim=False, start_rollout_id=None, + calculate_per_token_loss=False) + def submit(): + assert recipe.load == "/checkpoints/parent" + assert recipe.no_load_optim is False + assert recipe.start_rollout_id is None + return run + config = SimpleNamespace(checkpoint=checkpoint, recipe=recipe, launch=Mock(side_effect=submit)) + assert launcher.launch(config, receipt) is run + assert json.loads(receipt.read_text()) == { + "state": "submitted", "training_run_id": "run", + "modal_app_id": "app", "function_call_id": "fc", + } + with pytest.raises(RuntimeError, match="No run submitted"): + launcher.launch(config, receipt) + config.launch.assert_called_once() + launcher.pin.assert_called_once_with(checkpoint) + + +@pytest.mark.parametrize("error", [RuntimeError("upload failed"), KeyboardInterrupt()]) +def test_interrupted_submission_is_not_retried(launcher, tmp_path, error): + receipt = tmp_path / "launch.json" + config = SimpleNamespace(checkpoint=None, launch=Mock(side_effect=error), + recipe=SimpleNamespace(calculate_per_token_loss=False)) + with pytest.raises(type(error)): + launcher.launch(config, receipt) + assert json.loads(receipt.read_text())["state"] == "submitting" + with pytest.raises(RuntimeError, match="No run submitted"): + launcher.launch(config, receipt) + config.launch.assert_called_once() + launcher.pin.assert_not_called() + + +def test_partial_receipt_is_safe(launcher, tmp_path): + receipt = tmp_path / "launch.json" + receipt.touch() + config = SimpleNamespace(checkpoint=None, launch=Mock(), + recipe=SimpleNamespace(calculate_per_token_loss=False)) + with pytest.raises(RuntimeError, match="No run submitted"): + launcher.launch(config, receipt) + config.launch.assert_not_called() + + +def test_context_clamp_rejected_before_submission(launcher, tmp_path): + launcher.context.side_effect = ValueError("context clamp") + receipt = tmp_path / "launch.json" + config = SimpleNamespace(recipe=SimpleNamespace(calculate_per_token_loss=False), + checkpoint=object(), launch=Mock()) + with pytest.raises(ValueError, match="context clamp"): + launcher.launch(config, receipt) + assert not receipt.exists() + launcher.pin.assert_not_called() + config.launch.assert_not_called() + + +@pytest.mark.parametrize("overrides", [ + {"rollout_temperature": 0.7}, + {"rollout_top_p": 0.9}, + {"extra_config": {"rollout_top_k": 50}}, + {"extra_config": {"rollout_temperature": 0.0}}, + {"sglang_request_params": {"regex": "json-only"}}, + {"sglang_request_params": {"sampling_params": {"json_schema": {}}}}, + {"extra_config": {"sglang_request_params": {"ebnf": "grammar"}}}, + {"sglang_request_params": {"sampling_params": None}}, +]) +def test_sampling_mismatch_fails_before_launch_side_effects(launcher, tmp_path, overrides): + receipt = tmp_path / "launch.json" + config = SimpleNamespace( + recipe=SimpleNamespace(calculate_per_token_loss=False, **overrides), + checkpoint=object(), launch=Mock(), + ) + with pytest.raises(ValueError): + launcher.launch(config, receipt) + assert not receipt.exists() + launcher.pin.assert_not_called() + config.launch.assert_not_called() + + +@pytest.mark.parametrize("recipe", [ + SimpleNamespace(calculate_per_token_loss=True), + SimpleNamespace(), + SimpleNamespace(calculate_per_token_loss=None), + SimpleNamespace(calculate_per_token_loss=False, + extra_config={"calculate_per_token_loss": True}), +]) +def test_incompatible_loss_fails_before_any_launch_side_effect(launcher, tmp_path, recipe): + receipt = tmp_path / "launch.json" + config = SimpleNamespace(recipe=recipe, checkpoint=object(), launch=Mock()) + with pytest.raises(ValueError, match="calculate_per_token_loss=False"): + launcher.launch(config, receipt) + assert not receipt.exists() + config.launch.assert_not_called() + launcher.pin.assert_not_called() + + +@pytest.mark.parametrize("requested", [None, 199]) +def test_checkpoint_selection_only_reads_two_manifests(launcher, requested): + checkpoints = [SimpleNamespace(name=f"iter_{i:07d}") for i in range(200)] + launcher.checkpoints.return_value = checkpoints + result = SimpleNamespace(training_run_id="run", checkpoints=launcher.checkpoints) + assert launcher.select(result, requested) is checkpoints[-1] + assert launcher.manifests.call_count == 2 + + +def test_missing_checkpoint_does_not_read_manifests(launcher): + launcher.checkpoints.return_value = [SimpleNamespace(name="iter_0000199")] + with pytest.raises(RuntimeError, match="was not found"): + launcher.select( + SimpleNamespace(training_run_id="run", checkpoints=launcher.checkpoints), + 200, + ) + launcher.manifests.assert_not_called() + + +@pytest.fixture +def reward_helpers(): + source = Path(__file__).resolve().parents[1] / "batro_modal_rl.py" + tree = ast.parse(source.read_text()) + selected = {"_progress_value", "_transition_reward_parts", "_ante_clear_reward", "_episode_reward_parts"} + module = ast.Module(body=[ + node for node in tree.body + if isinstance(node, ast.FunctionDef) and node.name in selected + ], type_ignores=[]) + namespace = {"Any": object} + for node in tree.body: + if isinstance(node, ast.Assign) and len(node.targets) == 1 and isinstance(node.targets[0], ast.Name): + if node.targets[0].id in {"ANTE_CHECKPOINT_BONUS", "FINAL_ANTE", "DECISION_CAP_PENALTY"}: + namespace[node.targets[0].id] = ast.literal_eval(node.value) + exec(compile(module, str(source), "exec"), namespace) + return SimpleNamespace( + progress=namespace["_progress_value"], + transition=namespace["_transition_reward_parts"], + ante_reward=namespace["_ante_clear_reward"], + episode=namespace["_episode_reward_parts"], + ) + + +def test_skipped_blind_is_not_rewardable_progress(reward_helpers): + before = { + "ante_num": 1, + "state": "BLIND_SELECT", + "blinds": {"small": {"status": "SELECT"}}, + "round": {"chips": 0}, + } + skipped = { + "ante_num": 1, + "state": "BLIND_SELECT", + "blinds": { + "small": {"status": "SKIPPED"}, + "big": {"status": "SELECT"}, + }, + "round": {"chips": 0}, + } + defeated = { + "ante_num": 1, + "state": "SHOP", + "blinds": { + "small": {"status": "DEFEATED"}, + "big": {"status": "SELECT"}, + }, + "round": {"chips": 0}, + } + assert reward_helpers.progress(skipped) == (0, 0.0) + assert reward_helpers.transition(before, skipped, valid_action=True)["total"] == 0.0 + assert reward_helpers.transition(before, defeated, valid_action=True)["total"] == 0.0 + + playing = {**before, "state": "SELECTING_HAND", "round": {"chips": 120}} + assert reward_helpers.transition(playing, defeated, valid_action=True)["total"] == 4.0 + + # Advancing after a boss clear must not reconstruct two previously skipped + # blinds from the new ante number. + after_boss = {**defeated, "ante_num": 2} + parts = reward_helpers.transition(playing, after_boss, valid_action=True) + assert parts["progress"] == 1.0 + assert parts["blind_clear_bonus"] == 3.0 + assert parts["ante_checkpoint_bonus"] == 8.0 + + +@pytest.mark.parametrize("ante", range(1, 9)) +def test_ante_reward_scales_by_cleared_ante(reward_helpers, ante): + parts = reward_helpers.transition( + {"ante_num": ante}, {"ante_num": ante + 1}, + valid_action=True, highest_rewarded_ante=ante - 1, + ) + assert parts["ante_checkpoint_bonus"] == 8.0 * ante + assert parts["ante_checkpoints"] == 1 + assert parts["highest_rewarded_ante"] == ante + + +def test_vouchers_cannot_repeat_ante_rewards(reward_helpers): + highest = 0 + rewards, checkpoints = [], 0 + antes = [1, 2, 3, 2, 3, 2, 3, 4] + for before, after in zip(antes, antes[1:]): + parts = reward_helpers.transition( + {"ante_num": before}, {"ante_num": after}, + valid_action=True, highest_rewarded_ante=highest, + ) + highest = parts["highest_rewarded_ante"] + rewards.append(parts["ante_checkpoint_bonus"]) + checkpoints += parts["ante_checkpoints"] + assert rewards == [8, 16, 0, 0, 0, 0, 24] + assert highest == checkpoints == 3 + + +def test_win_top_up_uses_weighted_missing_antes_only(reward_helpers): + # Before the ante-8 boss: seven unique clears. A win may or may not + # advance the selector to ante 9; either path must total 8*(1+...+8). + assert reward_helpers.ante_reward(0, 7) == 224 + assert reward_helpers.ante_reward(7, 8) == 64 + assert reward_helpers.ante_reward(8, 8) == 0 + for ante in (8, 9): + parts = reward_helpers.episode( + {"ante_num": ante, "won": True}, invalid_actions=0, turns=1, capped=False, + ) + assert parts["ante_checkpoint_bonus"] == 288 + + +def test_multiple_new_antes_sum_their_individual_rewards(reward_helpers): + parts = reward_helpers.transition( + {"ante_num": 2}, {"ante_num": 5}, valid_action=True, highest_rewarded_ante=1, + ) + assert parts["ante_checkpoint_bonus"] == 8 * (2 + 3 + 4) + assert parts["ante_checkpoints"] == 3 diff --git a/training/.gitignore b/training/.gitignore new file mode 100644 index 00000000..9d789ec6 --- /dev/null +++ b/training/.gitignore @@ -0,0 +1,4 @@ +.dojo/ +.venv/ +__pycache__/ +.dojo-config.toml diff --git a/training/.python-version b/training/.python-version new file mode 100644 index 00000000..e4fba218 --- /dev/null +++ b/training/.python-version @@ -0,0 +1 @@ +3.12 diff --git a/training/action_credit_modal.py b/training/action_credit_modal.py new file mode 100644 index 00000000..0f8f2e03 --- /dev/null +++ b/training/action_credit_modal.py @@ -0,0 +1,62 @@ +"""Validate token-credit transport against the exact trainer image on CPU.""" + +import json +from pathlib import Path + +import modal +from modal_dojo.frameworks.slime.launcher import SLIME_IMAGE + +ROOT = Path(__file__).resolve().parents[1] +app = modal.App("balatro-action-credit-regression") +image = ( + modal.Image.from_registry(SLIME_IMAGE) + .entrypoint([]) + .run_commands("python -m pip install pytest modal==1.6.1") + .add_local_python_source("modal_dojo") + .add_local_file(ROOT / "training/balatro_credit.py", "/tmp/credit-validation/training/balatro_credit.py") + .add_local_file(ROOT / "tests/__init__.py", "/tmp/credit-validation/tests/__init__.py") + .add_local_file(ROOT / "tests/test_action_credit.py", "/tmp/credit-validation/tests/test_action_credit.py") + .add_local_file(ROOT / "training/balatro_diagnostics.py", "/tmp/credit-validation/training/balatro_diagnostics.py") + .add_local_file(ROOT / "tests/test_balatro_diagnostics.py", "/tmp/credit-validation/tests/test_balatro_diagnostics.py") + .add_local_file(ROOT / "batro_modal_rl.py", "/tmp/credit-validation/batro_modal_rl.py") + .add_local_file(ROOT / "tests/test_context_management.py", "/tmp/credit-validation/tests/test_context_management.py") + .add_local_file(ROOT / "tests/test_single_action_output.py", "/tmp/credit-validation/tests/test_single_action_output.py") + .add_local_file(ROOT / "tests/test_thinking_output.py", "/tmp/credit-validation/tests/test_thinking_output.py") + .add_local_file(ROOT / "training/checkpoint_group_integration.py", "/tmp/credit-validation/training/checkpoint_group_integration.py") + .add_local_file(ROOT / "tests/fixtures/repeated_action_outputs.json", "/tmp/credit-validation/tests/fixtures/repeated_action_outputs.json") +) + + +@app.function(image=image, cpu=4, memory=8192, timeout=240, retries=0, serialized=True, + volumes={"/hf-cache": modal.Volume.from_name("huggingface-cache")}) +def check(): + import os + import subprocess + + process = subprocess.run( + ["python", "-m", "pytest", "tests/test_action_credit.py", "tests/test_balatro_diagnostics.py", "tests/test_single_action_output.py", "tests/test_thinking_output.py", "-q", "--tb=short"], + cwd="/tmp/credit-validation", + env={**os.environ, "BALATRO_SLIME_SOURCE": "/root/slime/slime", + "OMP_NUM_THREADS": "1", "OPENBLAS_NUM_THREADS": "1", "BALATRO_THINKING_NATIVE": "1", + "HF_HOME": "/hf-cache", "HF_HUB_CACHE": "/hf-cache/hub"}, + capture_output=True, text=True, timeout=210, + ) + checkpoint = subprocess.run( + ["python", "/tmp/credit-validation/training/checkpoint_group_integration.py"], + env={**os.environ, "MODAL_DOJO_SLIME_SOURCE": "/root/slime/slime", + "OMP_NUM_THREADS": "1", "OPENBLAS_NUM_THREADS": "1"}, + capture_output=True, text=True, timeout=90, + ) + return {"image": SLIME_IMAGE, "returncode": process.returncode or checkpoint.returncode, + "stdout": process.stdout, "stderr": process.stderr, + "checkpoint_collectives": {"returncode": checkpoint.returncode, + "stdout": checkpoint.stdout, "stderr": checkpoint.stderr}, + "data_logging_source": Path("/root/slime/slime/backends/megatron_utils/data.py").read_text() if process.returncode else None} + + +@app.local_entrypoint() +def main(out: str): + result = check.remote() + Path(out).write_text(json.dumps(result, indent=2)) + print(json.dumps(result), flush=True) + assert result["returncode"] == 0, "Action-credit trainer integration failed" diff --git a/training/balatro_credit.py b/training/balatro_credit.py new file mode 100644 index 00000000..ce29e89d --- /dev/null +++ b/training/balatro_credit.py @@ -0,0 +1,94 @@ +"""Token-aligned Balatro returns for the pinned Slime trainer. + +Executed at image build to install this module and transport train_metadata. +No GPU or game dependencies are needed to validate the spans or install it. +""" + +import math +from pathlib import Path + + +def expand_advantages(spans, loss_mask): + """Reject gaps/overlaps: exactly the generated action tokens receive credit.""" + values = [0.0] * len(loss_mask) + covered = [False] * len(loss_mask) + for span in spans: + start, end, value = span["start"], span["end"], float(span["advantage"]) + if not (isinstance(start, int) and isinstance(end, int) + and 0 <= start < end <= len(values) and math.isfinite(value)): + raise ValueError("Invalid Balatro advantage span") + for index in range(start, end): + if covered[index] or not loss_mask[index]: + raise ValueError("Overlapping advantage span or credit on environment feedback") + covered[index] = True + values[index] = value + if covered != [bool(x) for x in loss_mask]: + raise ValueError("Balatro advantage spans do not cover every action token") + return values + + +def compute_advantages(args, rollout_data): + """Slime's native custom-advantage hook; preserves CP slicing and loss masks.""" + if args.kl_coef != 0: + raise ValueError("Balatro reward-to-go expects kl_coef=0; use the separate KL loss") + if getattr(args, "normalize_advantages", False): + raise ValueError("Action credit requires normalize_advantages=False to preserve invalid-action penalties") + import torch + from slime.backends.megatron_utils.cp_utils import slice_log_prob_with_cp + metadata = rollout_data.get("metadata") + if metadata is None or len(metadata) != len(rollout_data["response_lengths"]): + raise ValueError("Missing Balatro action advantages in the training batch") + advantages = [] + for item, mask, length, total, kl in zip( + metadata, rollout_data["loss_masks"], rollout_data["response_lengths"], + rollout_data["total_lengths"], rollout_data["kl"], strict=True, + ): + if not item or "balatro_action_advantages" not in item or len(mask) != length: + raise ValueError("Missing or misaligned Balatro action advantages") + values = expand_advantages(item["balatro_action_advantages"], mask.tolist()) + tensor = torch.tensor(values, dtype=torch.float32, device=kl.device) + local = slice_log_prob_with_cp(tensor, total, length) + if local.shape != kl.shape: + raise ValueError("Balatro advantages and trainer context-parallel chunks differ") + advantages.append(local) + rollout_data["advantages"] = advantages + rollout_data["returns"] = [value.clone() for value in advantages] + + +def install(root=Path("/root/slime")): + """Fail closed on a changed trainer; native hook must exist before patching.""" + root = Path(root) + loss = (root / "slime/backends/megatron_utils/loss.py").read_text() + if "custom_advantage_function_path" not in loss: + raise RuntimeError("Pinned trainer lacks the custom advantage hook") + patches = [ + (root / "slime/ray/rollout.py", + ' rollout_data = {"partition": partition}\n for key in [\n', + ' rollout_data = {"partition": partition}\n for key in [\n' + ' "metadata", # BALATRO_TRAIN_METADATA\n', + "BALATRO_TRAIN_METADATA"), + (root / "slime/backends/megatron_utils/data.py", + ' for key, val in rollout_data.items():\n if key in [\n', + ' for key, val in rollout_data.items():\n if key in [\n' + ' "metadata", # BALATRO_METADATA_NOT_A_METRIC\n', + "BALATRO_METADATA_NOT_A_METRIC"), + ] + updates = [] + for path, before, after, marker in patches: + source = path.read_text() + if marker in source: + continue + if source.count(before) != 1: + raise RuntimeError(f"Unsupported trainer source: {path}") + updated = source.replace(before, after, 1) + compile(updated, str(path), "exec") + updates.append((path, updated)) + for path, updated in updates: + path.write_text(updated) + destination = root / "balatro_credit.py" + if destination.resolve() != Path(__file__).resolve(): + destination.write_text(Path(__file__).read_text()) + + +if __name__ == "__main__": + install() diff --git a/training/balatro_diagnostics.py b/training/balatro_diagnostics.py new file mode 100644 index 00000000..32ed208c --- /dev/null +++ b/training/balatro_diagnostics.py @@ -0,0 +1,159 @@ +"""Game-level reporting and explicitly named probability diagnostics.""" + +import ast +import math +from pathlib import Path + + +def game_metrics(samples): + games = {} + for sample in samples: + metadata = sample.get("metadata", {}) if isinstance(sample, dict) else sample.metadata + game = metadata["balatro"] + games[game["episode_id"]] = game + if not games: + raise ValueError("Cannot report an empty Balatro batch") + games = list(games.values()) + reward = sorted(float(g["episode_reward"]) for g in games) + ante = sorted(float(g["final_ante"]) for g in games) + if not all(math.isfinite(x) for x in reward + ante): + raise ValueError("Nonfinite Balatro game metric") + def percentile(values, q): + position = (len(values)-1) * q / 100 + low, high = math.floor(position), math.ceil(position) + return values[low] + (values[high]-values[low]) * (position-low) + turns = sum(g["turns"] for g in games) + invalid = sum(g["invalid_actions"] for g in games) + result = {"game_count": len(games), "game_reward_mean": sum(reward)/len(games), + "game_ante_mean": sum(ante)/len(games), "game_win_rate": sum(bool(g["won"]) for g in games)/len(games), + "game_invalid_fraction": invalid/max(1, turns), "game_invalid_actions": invalid, + "game_turns": turns} + for name, values in (("game_reward", reward), ("game_ante", ante)): + result.update({f"{name}_p{q}": percentile(values, q) for q in (10, 25, 50, 75, 90, 95)}) + # These lengths count sampled action tokens, including rejected output, + # once per game. They do not depend on segment count or evaluation masks. + if any(g.get("output_contract_version") == 1 for g in games): + if not all(g.get("output_contract_version") == 1 for g in games): + raise ValueError("Mixed output-contract versions in one batch") + lengths = sorted(n for g in games for n in g["action_token_lengths"]) + if (len(lengths) != turns or not lengths + or any(type(n) is not int or not 1 <= n <= 96 + g.get("thinking_budget", 0) + for g in games for n in g["action_token_lengths"])): + raise ValueError("Missing or invalid sampled action lengths") + result.update(action_count=turns, action_tokens_total=sum(lengths), + action_tokens_mean=sum(lengths)/turns, action_tokens_max=max(lengths)) + result.update({f"action_tokens_p{q}": percentile(lengths, q) for q in (50, 90, 95, 99)}) + if any(g.get("thinking_budget", 0) for g in games): + for g in games: + if (g.get("thinking_budget") != 8192 + or len(g.get("thinking_token_lengths", [])) != g["turns"] + or len(g.get("final_answer_token_lengths", [])) != g["turns"]): + raise ValueError("Missing or mixed thinking token counters") + for thinking, answer, total in zip(g["thinking_token_lengths"], g["final_answer_token_lengths"], g["action_token_lengths"]): + if (type(thinking) is not int or type(answer) is not int + or not 0 <= thinking <= 8192 or not 0 <= answer <= 96 + or thinking + answer != total): + raise ValueError("Invalid thinking/final-answer token accounting") + for field, label in (("thinking_token_lengths", "thinking_tokens"), + ("final_answer_token_lengths", "final_answer_tokens")): + values = sorted(n for g in games for n in g[field]) + result[f"{label}_mean"] = sum(values) / turns + result[f"{label}_max"] = max(values) + result.update({f"{label}_p{q}": percentile(values, q) for q in (50, 90, 95, 99)}) + result["thinking_budget_hit_fraction"] = sum(n == 8192 for g in games for n in g["thinking_token_lengths"]) / turns + if all("forced_thinking_closures" in g for g in games): + counts = [g["forced_thinking_closures"] for g in games] + if any(type(n) is not int or not 0 <= n <= g["turns"] for n, g in zip(counts, games)): + raise ValueError("Invalid forced thinking closure count") + result["thinking_forced_close_count"] = sum(counts) + result["thinking_forced_close_fraction"] = sum(counts) / turns + for field, label in (("output_format_errors", "format_invalid"), + ("multiple_action_responses", "multiple_objects"), + ("length_limit_responses", "length_limit"), + ("normal_stop_responses", "normal_stop")): + counts = [g[field] for g in games] + if any(type(n) is not int or not 0 <= n <= g["turns"] for n, g in zip(counts, games)): + raise ValueError(f"Invalid action counter: {field}") + result[f"action_{label}_count"] = sum(counts) + result[f"action_{label}_fraction"] = sum(counts)/turns + return result + + +def log_rollout(rollout_id, args, samples, extra_metrics, rollout_time): + from slime.ray.rollout import compute_metrics_from_samples, compute_perf_metrics_from_samples, compute_rollout_step + from slime.utils import logging_utils + data = dict(extra_metrics or {}) + data.update({f"rollout/segments/{k}": v for k, v in compute_metrics_from_samples(args, samples).items()}) + data.update({f"perf/{k}": v for k, v in compute_perf_metrics_from_samples(args, samples, rollout_time).items()}) + metrics = game_metrics(samples) + data.update({f"rollout/{k}": v for k, v in metrics.items()}) + data["rollout/raw_reward"] = metrics["game_reward_mean"] + data["rollout/step"] = compute_rollout_step(args, rollout_id) + logging_utils.log(args, data, step_key="rollout/step") + return True + + +def log_eval(rollout_id, args, data, extra_metrics): + from slime.ray.rollout import compute_rollout_step + from slime.utils import logging_utils + metrics = {} + for name, item in data.items(): + if item.get("samples"): + metrics.update({f"eval/{name}/{k}": v for k, v in game_metrics(item["samples"]).items()}) + metrics["eval/step"] = compute_rollout_step(args, rollout_id) + logging_utils.log(args, metrics, step_key="eval/step") + return False # Retain the native eval/eval mean used by the watchdog. + + +def install(root=Path("/root/slime")): + root = Path(root) + updates = [] + data_path = root / "slime/backends/megatron_utils/data.py" + source = data_path.read_text() + if "BALATRO_GAME_METRIC_NAMES" not in source: + calls = [n for n in ast.walk(ast.parse(source)) if isinstance(n, (ast.Expr, ast.Assign)) + and isinstance(n.value, ast.Call) and isinstance(n.value.func, ast.Name) + and n.value.func.id == "gather_log_data" and n.value.args + and isinstance(n.value.args[0], ast.Constant) and n.value.args[0].value == "rollout"] + if len(calls) != 1: + raise RuntimeError(f"Unsupported trainer source for Balatro diagnostics: {data_path}") + call = calls[0] + indent = " " * call.col_offset + addition = (f'{indent}# BALATRO_GAME_METRIC_NAMES: these values average context segments.\n' + f'{indent}if "raw_reward" in log_dict:\n' + f'{indent} log_dict["segment_weighted_raw_reward"] = log_dict.pop("raw_reward")\n') + lines = source.splitlines(keepends=True) + lines.insert(call.lineno - 1, addition) + updated = "".join(lines) + compile(updated, str(data_path), "exec") + updates.append((data_path, updated)) + patches = [ + (root / "slime/backends/megatron_utils/loss.py", + ' reported_loss["train_rollout_logprob_abs_diff"] = train_rollout_logprob_abs_diff.clone().detach()', + ' reported_loss["train_rollout_logprob_abs_diff"] = train_rollout_logprob_abs_diff.clone().detach()\n' + ' # BALATRO_PROBABILITY_TAILS: game-normalized token fractions, not averaged quantiles.\n' + ' gap = (log_probs_to_compare - rollout_log_probs).abs()\n' + ' for threshold, label in ((0.01, "0_01"), (0.05, "0_05"), (0.1, "0_1")):\n' + ' reported_loss[f"sampling_gap_gt_{label}_fraction"] = sum_of_sample_mean((gap > threshold).float()).detach()\n' + ' reported_loss["pre_update_ppo_kl"] = reported_loss.pop("ppo_kl")\n' + ' reported_loss["pre_update_pg_clipfrac"] = reported_loss.pop("pg_clipfrac")', + "BALATRO_PROBABILITY_TAILS"), + ] + for path, before, after, marker in patches: + source = path.read_text() + if marker in source: + continue + if source.count(before) != 1: + raise RuntimeError(f"Unsupported trainer source for Balatro diagnostics: {path}") + updated = source.replace(before, after, 1) + compile(updated, str(path), "exec") + updates.append((path, updated)) + for path, source in updates: + path.write_text(source) + destination = root / "balatro_diagnostics.py" + if destination.resolve() != Path(__file__).resolve(): + destination.write_text(Path(__file__).read_text()) + + +if __name__ == "__main__": + install() diff --git a/training/checkpoint_group_integration.py b/training/checkpoint_group_integration.py new file mode 100644 index 00000000..a8d347d2 --- /dev/null +++ b/training/checkpoint_group_integration.py @@ -0,0 +1,67 @@ +"""CPU regression against trainer source; set MODAL_DOJO_SLIME_SOURCE.""" + +import importlib.util +import os +import sys +import tempfile +from datetime import timedelta +from pathlib import Path +from types import ModuleType +import torch.distributed as dist +import torch.multiprocessing as mp + + +def worker(rank, rendezvous): + from modal_dojo.common import torch_dist_checkpoint as ckpt + + for name in ["slime", "slime.utils"]: + sys.modules[name] = ModuleType(name) + memory = ModuleType("slime.utils.memory_utils") + memory.available_memory = lambda: {"free_GB": 100} + memory.clear_memory = lambda: None + memory.print_memory = lambda *a: {} + sys.modules[memory.__name__] = memory + for name in ["distributed_utils", "reloadable_process_group"]: + full = "slime.utils." + name + spec = importlib.util.spec_from_file_location( + full, + str( + Path(os.environ["MODAL_DOJO_SLIME_SOURCE"]) / "utils" / (name + ".py") + ), + ) + module = importlib.util.module_from_spec(spec) + sys.modules[full] = module + spec.loader.exec_module(module) + reloadable = sys.modules["slime.utils.reloadable_process_group"] + dist.init_process_group( + "gloo", init_method="file://" + rendezvous, rank=rank, world_size=2 + ) + reloadable.register_default_process_group(timedelta(seconds=30)) + reloadable.monkey_patch_torch_dist() + # Reproduce the exact failure from the previous implementation. + proxy = dist.new_group(backend="gloo") + try: + dist.destroy_process_group(proxy) + except ValueError as error: + assert "Invalid process group" in str(error) + else: + raise AssertionError("Expected old cleanup to fail") + dist.destroy_process_group(proxy.group) + reloadable.ReloadableProcessGroup.GROUPS[os.getpid()].remove(proxy) + os.environ["LOCAL_RANK"] = "0" + os.environ["MODAL_DOJO_CHECKPOINTS_VOLUME_NAME"] = "test" + ckpt._commit_volume = lambda name: None + ckpt._validate_committed_checkpoint = lambda *args: None + for _ in range(3): + ckpt.commit_latest_checkpoint_volume("/test") + assert not reloadable.ReloadableProcessGroup.GROUPS[os.getpid()] + dist.barrier() + dist.destroy_process_group() + print( + f"rank {rank}: old error reproduced; three corrected saves passed", flush=True + ) + + +if __name__ == "__main__": + with tempfile.TemporaryDirectory() as root: + mp.spawn(worker, args=(str(Path(root) / "rendezvous"),), nprocs=2, join=True) diff --git a/training/checkpoint_rpc.py b/training/checkpoint_rpc.py new file mode 100644 index 00000000..57926a16 --- /dev/null +++ b/training/checkpoint_rpc.py @@ -0,0 +1,127 @@ +"""Retry lost checkpoint RPCs without repeating completed distributed saves. + +Training actors must remain single-threaded and must not restart in place: the +completion records live alongside their model/optimizer state. A dead actor or +an application error still requires recovery of the whole training group. +""" + +import logging +import math +import time +import uuid + +logger = logging.getLogger(__name__) + + +def save_once(actor, operation_id, rollout_id, force_sync=False): + """Actor-side acknowledgement cache; one collective per operation ID.""" + records = actor.__dict__.setdefault("_checkpoint_rpc_records", {}) + arguments = (rollout_id, force_sync) + if operation_id in records: + saved_arguments, state, value = records[operation_id] + if saved_arguments != arguments: + raise ValueError("Checkpoint operation ID reused with different arguments") + if state != "complete": + raise RuntimeError( + f"Checkpoint operation is {state}; group recovery required" + ) + return value + records[operation_id] = (arguments, "incomplete", None) + try: + value = actor.save_model(rollout_id, force_sync=force_sync) + except BaseException: + records[operation_id] = (arguments, "failed", None) + raise + records[operation_id] = (arguments, "complete", value) + return value + + +def save_group( + actors, + rollout_id, + force_sync=False, + *, + max_retries=5, + retry_delay=2.0, + timeout=1800.0, +): + """Resubmit only unavailable RPCs, retaining the same operation ID. + + Consume whichever worker finishes first. Waiting on workers sequentially + could hide an unavailable worker behind peers blocked in its collective. + Never retry a RayTaskError, worker death, or a mere get timeout: those do + not establish a recoverable lost acknowledgement. + """ + import ray + + if ( + not isinstance(max_retries, int) + or max_retries < 0 + or not math.isfinite(retry_delay) + or retry_delay < 0 + or not math.isfinite(timeout) + or timeout <= 0 + ): + raise ValueError("Checkpoint retry bounds must be finite and nonnegative") + operation_id = uuid.uuid4().hex + deadline = time.monotonic() + timeout + pending = {} + scheduled = {} + retries = [0] * len(actors) + results = [None] * len(actors) + + def submit(index): + # Explicitly disable Ray's own resubmission. Only this coordinator may + # retry, and the worker must not restart with an empty completion cache. + ref = ( + actors[index] + ._checkpoint_save_once.options(max_task_retries=0) + .remote(operation_id, rollout_id, force_sync=force_sync) + ) + pending[ref] = index + + for index in range(len(actors)): + submit(index) + while pending or scheduled: + now = time.monotonic() + if now >= deadline: + raise TimeoutError( + f"Checkpoint {rollout_id} did not complete on all workers within {timeout}s" + ) + for index, due in list(scheduled.items()): + if due <= now: + del scheduled[index] + submit(index) + wait_seconds = min(1.0, deadline - now) + if scheduled: + wait_seconds = min(wait_seconds, max(0.0, min(scheduled.values()) - now)) + if not pending: + time.sleep(wait_seconds) + continue + ready, _ = ray.wait(list(pending), num_returns=1, timeout=wait_seconds) + for ref in ready: + index = pending.pop(ref) + try: + results[index] = ray.get(ref) + except ray.exceptions.RayTaskError: + # Ray may make this also inherit the exception raised inside + # the method. An application-raised ActorUnavailableError is + # not evidence that this save's request/reply was lost. + raise + except ray.exceptions.ActorUnavailableError: + if retries[index] >= max_retries: + raise + delay = min(retry_delay * 2 ** retries[index], 30.0) + retries[index] += 1 + scheduled[index] = time.monotonic() + delay + logger.warning( + "Checkpoint %s worker %s RPC unavailable; retry %s/%s in %.1fs " + "with operation %s (completed saves are acknowledged without rerunning)", + rollout_id, + index, + retries[index], + max_retries, + delay, + operation_id, + ) + return results diff --git a/training/checkpoint_rpc_integration.py b/training/checkpoint_rpc_integration.py new file mode 100644 index 00000000..2c7d87f6 --- /dev/null +++ b/training/checkpoint_rpc_integration.py @@ -0,0 +1,226 @@ +"""CPU fault injection using the pinned image's Ray and Gloo collectives.""" + +import ast +import inspect +import json +import os +import socket +import tempfile +import time +from pathlib import Path + +import ray +import torch +import torch.distributed as dist +from slime.utils.checkpoint_rpc import save_group + + +def extracted_method(path, class_name, method_name): + tree = ast.parse(Path(path).read_text()) + cls = next( + n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == class_name + ) + method = next( + n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == method_name + ) + namespace = {"ray": ray} + exec( + compile( + ast.fix_missing_locations(ast.Module(body=[method], type_ignores=[])), + path, + "exec", + ), + namespace, + ) + return namespace[method_name] + + +class Worker: + def __init__(self, rank, rendezvous, root): + from datetime import timedelta + + torch.set_num_threads(1) + dist.init_process_group( + "gloo", + init_method=rendezvous, + rank=rank, + world_size=2, + timeout=timedelta(seconds=30), + ) + self.rank, self.root, self.count = rank, root, 0 + + def save_model(self, rollout_id, force_sync=False): + self.count += 1 + if rollout_id == -1: + raise ValueError("injected disk failure") + if rollout_id == -2: + os._exit(1) + dist.barrier() + time.sleep(0.2) + torch.save( + {"step": rollout_id, "count": self.count}, + f"{self.root}/rank-{self.rank}.pt", + ) + dist.barrier() + + def counts(self): + return self.count + + +Worker._checkpoint_save_once = extracted_method( + "/root/slime/slime/backends/megatron_utils/actor.py", + "MegatronTrainRayActor", + "_checkpoint_save_once", +) + + +class Group: + def __init__(self, actors): + self._actor_handlers = actors + + def _release_train_enabled(self): + return False + + +Group.save_model = extracted_method( + "/root/slime/slime/ray/actor_group.py", "RayTrainGroup", "save_model" +) + + +def main(): + ray.init( + num_cpus=4, + object_store_memory=128 * 1024 * 1024, + include_dashboard=False, + log_to_driver=False, + ) + real_get = ray.get + results = [] + # The exception is injected at the driver transport boundary, not raised + # inside the worker method (which would instead be a RayTaskError). + print( + "ActorUnavailableError", + inspect.signature(ray.exceptions.ActorUnavailableError), + flush=True, + ) + for mode in ( + "before", + "during", + "after", + "application_error", + "worker_death", + "exhausted", + ): + with socket.socket() as sock: + sock.bind(("127.0.0.1", 0)) + port = sock.getsockname()[1] + with tempfile.TemporaryDirectory() as root: + cls = ray.remote( + num_cpus=1, max_restarts=0, max_concurrency=1, max_task_retries=0 + )(Worker) + actors = [cls.remote(i, f"tcp://127.0.0.1:{port}", root) for i in range(2)] + real_get([a.counts.remote() for a in actors], timeout=40) + injected = {} + submissions = [0, 0] + + class Method: + def __init__(self, index): + self.index = index + + def options(self, **kwargs): + self.kwargs = kwargs + return self + + def remote(self, *args, **kwargs): + index = self.index + submissions[index] += 1 + fail = index == 1 and ( + submissions[index] == 1 or mode == "exhausted" + ) + if fail and mode in ("before", "exhausted"): + ref = ray.put(None) + else: + ref = ( + actors[index] + ._checkpoint_save_once.options(**self.kwargs) + .remote(*args, **kwargs) + ) + if fail and mode == "during": + # Lose the response while the original is still in flight. + ref = ray.put(None) + if fail and mode in ("before", "during", "after", "exhausted"): + injected[ref] = True + return ref + + class Proxy: + def __init__(self, index): + self._checkpoint_save_once = Method(index) + + def transport_get(ref, *args, **kwargs): + value = real_get(ref, *args, **kwargs) + if ref in injected: + del injected[ref] + raise ray.exceptions.ActorUnavailableError( + "injected RPC connection reset", b"\x01" * 16 + ) + return value + + ray.get = transport_get + expected_error = None + try: + proxies = [Proxy(i) for i in range(2)] + if mode == "application_error": + expected_error = ray.exceptions.RayTaskError + save_group(proxies, -1, retry_delay=0.01, timeout=10) + elif mode == "worker_death": + expected_error = ray.exceptions.RayActorError + save_group(proxies, -2, retry_delay=0.01, timeout=20) + elif mode == "exhausted": + expected_error = ray.exceptions.ActorUnavailableError + save_group(proxies, 19, max_retries=2, retry_delay=0.01, timeout=10) + else: + # Exercise the patched Slime group method, including its + # unchanged post-save work, with the default retry policy. + Group(proxies).save_model(19, force_sync=True) + assert expected_error is None, "Expected failure was swallowed" + except Exception as exc: + if expected_error is None or not isinstance(exc, expected_error): + raise + finally: + ray.get = real_get + result = {"mode": mode, "submissions": submissions} + if expected_error is None: + counts = real_get([a.counts.remote() for a in actors], timeout=10) + assert counts == [1, 1], counts + assert submissions == [1, 2], submissions + for rank in range(2): + assert torch.load(f"{root}/rank-{rank}.pt", weights_only=True) == { + "step": 19, + "count": 1, + } + # Both actors must still communicate after recovery. + Group(actors).save_model(29, force_sync=True) + assert real_get([a.counts.remote() for a in actors]) == [2, 2] + result["save_counts_after_recovery"] = counts + result["next_collective_passed"] = True + else: + result["surfaced"] = expected_error.__name__ + if mode == "application_error": + assert submissions == [1, 1] + if mode == "exhausted": + assert submissions == [1, 3] + for actor in actors: + ray.kill(actor) + results.append(result) + print(json.dumps(result), flush=True) + ray.shutdown() + print( + json.dumps( + {"passed": len(results), "ray_version": ray.__version__, "results": results} + ), + flush=True, + ) + + +if __name__ == "__main__": + main() diff --git a/training/checkpoint_rpc_modal.py b/training/checkpoint_rpc_modal.py new file mode 100644 index 00000000..a325d18a --- /dev/null +++ b/training/checkpoint_rpc_modal.py @@ -0,0 +1,50 @@ +"""Run checkpoint RPC fault injection on CPU in the exact Slime image.""" + +import json +from pathlib import Path + +import modal +from modal_dojo.frameworks.slime.launcher import SLIME_IMAGE + +ROOT = Path(__file__).resolve().parent +app = modal.App("balatro-checkpoint-rpc-regression") +image = modal.Image.from_registry(SLIME_IMAGE).entrypoint([]) +for name in ( + "checkpoint_rpc.py", + "patch_checkpoint_rpc.py", + "checkpoint_rpc_integration.py", +): + image = image.add_local_file(ROOT / name, f"/tmp/{name}", copy=True) +image = image.run_commands("python /tmp/patch_checkpoint_rpc.py") + + +@app.function(image=image, cpu=4, memory=8192, timeout=240, retries=0, serialized=True) +def check(): + import os + import subprocess + + process = subprocess.run( + ["python", "/tmp/checkpoint_rpc_integration.py"], + env={ + **os.environ, + "PYTHONPATH": "/root/slime:/root/Megatron-LM:/tmp", + "OMP_NUM_THREADS": "1", + "OPENBLAS_NUM_THREADS": "1", + }, + capture_output=True, + text=True, + timeout=210, + ) + return { + "returncode": process.returncode, + "stdout": process.stdout, + "stderr": process.stderr, + } + + +@app.local_entrypoint() +def main(out: str): + result = check.remote() + Path(out).write_text(json.dumps(result, indent=2)) + print(json.dumps(result), flush=True) + assert result["returncode"] == 0, "Checkpoint RPC fault injection failed" diff --git a/training/cloud_watchdog.py b/training/cloud_watchdog.py new file mode 100644 index 00000000..5a6d4605 --- /dev/null +++ b/training/cloud_watchdog.py @@ -0,0 +1,101 @@ +"""Scheduled CPU monitoring and bounded early stopping, independent of a laptop.""" + +from pathlib import Path + +import modal + +app = modal.App("balatro-dojo-watchdog") +metadata = modal.Volume.from_name("modal-dojo-metadata", environment_name="joy-dev") +evidence = modal.Volume.from_name("balatro-dojo-watchdog-evidence", create_if_missing=True, environment_name="joy-dev") +image = (modal.Image.debian_slim(python_version="3.12") + .add_local_file(Path(__file__).with_name("watchdog_policy.py"), "/root/watchdog_policy.py")) + + +@app.function(image=image, volumes={"/metadata": metadata, "/evidence": evidence}, + schedule=modal.Period(minutes=2), timeout=90, cpu=.25, memory=1024, + max_containers=1, serialized=True) +async def tick(): + import asyncio + import json + import time + from pathlib import Path + from modal.client import _Client + from modal_proto import api_pb2 + from watchdog_policy import assess, merge_chunks, summarize_games + + registry = modal.Dict.from_name("balatro-dojo-watchdog", create_if_missing=True, environment_name="joy-dev") + metadata = modal.Volume.from_name("modal-dojo-metadata", environment_name="joy-dev") + evidence = modal.Volume.from_name("balatro-dojo-watchdog-evidence", environment_name="joy-dev") + + await metadata.reload.aio() + client = await _Client.from_env() + summaries = [] + async for run_id, registration in registry.items.aio(): + if not registration.get("active"): + continue + snapshot = {"run": run_id, "checked_at": time.time()} + try: + record = json.loads(Path(f"/metadata/training-runs/{run_id}.json").read_text()) + app_id = registration["app_id"] + # A registration can stop only the exact app belonging to this run. + if record.get("training_run_id") != run_id or record.get("modal_app_id") != app_id: + raise ValueError("Registered run/app identity does not match metadata") + lifecycle = await asyncio.wait_for(client.stub.AppGetLifecycle( + api_pb2.AppGetLifecycleRequest(app_id=app_id)), timeout=15) + stopped = lifecycle.lifecycle.app_state == api_pb2.APP_STATE_STOPPED + files = sorted(Path(f"/metadata/metric-series/{run_id}").glob("*.json")) + table = merge_chunks([json.loads(path.read_text()) for path in files]) + assessment = assess(table) + snapshot.update(assessment) + snapshot.update(status=record.get("status"), progress=record.get("metadata", {}).get("framework_progress"), + app_stopped=stopped, attempt_count=record.get("metadata", {}).get("attempt_count")) + latest = record.get("metadata", {}).get("latest_rollout") or {} + if latest.get("rollout_id") is not None: + try: + rollout_path = Path(f"/metadata/training-rollouts/{run_id}__{int(latest['rollout_id']):08d}.json") + snapshot["latest_training_games"] = summarize_games(json.loads(rollout_path.read_text())) + except Exception as exc: + snapshot["game_read_error"] = f"{type(exc).__name__}: {exc}" + if stopped: + registration["active"] = False + elif assessment["decision"] == "stop": + # Persist the decision before stopping anything. Training metadata + # is read-only here; do not race the launcher's run-record writer. + registration["stop_reason"] = assessment["reason"] + registration.setdefault("stop_requested_at", time.time()) + registration["latest"] = snapshot + await registry.put.aio(run_id, registration) + await asyncio.wait_for(client.stub.AppStop( + api_pb2.AppStopRequest(app_id=app_id, source=api_pb2.APP_STOP_SOURCE_CLI)), timeout=15) + snapshot["stop_rpc_succeeded"] = True + registration["latest"] = snapshot + except Exception as exc: + # Failed reads cannot establish learning failure or authorize a stop. + snapshot["read_error"] = f"{type(exc).__name__}: {exc}" + registration["latest"] = snapshot + await registry.put.aio(run_id, registration) + path = Path("/evidence") / run_id + path.mkdir(exist_ok=True) + (path / "latest.json").write_text(json.dumps(snapshot, indent=2)) + with (path / "history.jsonl").open("a") as output: + output.write(json.dumps(snapshot) + "\n") + print(json.dumps(snapshot), flush=True) + summaries.append(snapshot) + await evidence.commit.aio() + return summaries + + +@app.function(image=image, timeout=45, cpu=.25, serialized=True) +async def verify_stop_api(stopped_app_id: str): + """Check cloud stop permissions using an already stopped, explicit test app.""" + import asyncio + from modal.client import _Client + from modal_proto import api_pb2 + client = await _Client.from_env() + result = await asyncio.wait_for(client.stub.AppGetLifecycle( + api_pb2.AppGetLifecycleRequest(app_id=stopped_app_id)), timeout=15) + if result.lifecycle.app_state != api_pb2.APP_STATE_STOPPED: + raise ValueError("The stop-permission probe requires an already stopped app") + await asyncio.wait_for(client.stub.AppStop( + api_pb2.AppStopRequest(app_id=stopped_app_id, source=api_pb2.APP_STOP_SOURCE_CLI)), timeout=15) + return {"stopped_app_id": stopped_app_id, "cloud_stop_rpc_verified": True} diff --git a/training/dojo-checkpoint-recovery.patch b/training/dojo-checkpoint-recovery.patch new file mode 100644 index 00000000..087d41cc --- /dev/null +++ b/training/dojo-checkpoint-recovery.patch @@ -0,0 +1,956 @@ +diff --git a/modal_dojo/_dashboard.py b/modal_dojo/_dashboard.py +index 6308710c..a97e4045 100644 +--- a/modal_dojo/_dashboard.py ++++ b/modal_dojo/_dashboard.py +@@ -212,6 +212,8 @@ def _build_image() -> modal.Image: + DASHBOARD_VERSION_ENV_KEY: current_dashboard_version(), + TIMING_DEBUG_ENV: os.environ.get(TIMING_DEBUG_ENV, ""), + DASHBOARD_PREVIEW_ENV_KEY: "true" if IS_PREVIEW else "", ++ **({"MODAL_ENVIRONMENT": os.environ["MODAL_ENVIRONMENT"]} ++ if os.environ.get("MODAL_ENVIRONMENT") else {}), + } + ) + ) +diff --git a/modal_dojo/common/checkpoint.py b/modal_dojo/common/checkpoint.py +index ab998405..4745799c 100644 +--- a/modal_dojo/common/checkpoint.py ++++ b/modal_dojo/common/checkpoint.py +@@ -136,18 +136,26 @@ def _list_checkpoints( + continue + child_rel = f"{rel}/{name}" if rel else name + try: +- child_names = { +- _entry_name(child) ++ child_sizes = { ++ _entry_name(child): getattr(child, "size", 0) + for child in volume.iterdir(child_rel, recursive=False) ++ if not _is_dir_entry(child) + } + except (FileNotFoundError, NotFoundError): +- child_names = set() ++ child_sizes = {} + iteration = parse_torch_dist_iteration(name) + if ( + tracker_iteration is None + or iteration is None + or iteration > tracker_iteration +- or not is_complete_torch_dist_checkpoint(child_names) ++ ): ++ continue ++ try: ++ metadata = b"".join(volume.read_file(f"{child_rel}/.metadata")) ++ except (FileNotFoundError, NotFoundError): ++ continue ++ if not is_complete_torch_dist_checkpoint( ++ child_sizes.keys(), metadata=metadata, file_sizes=child_sizes + ): + continue + checkpoints.append( +diff --git a/modal_dojo/common/config.py b/modal_dojo/common/config.py +index 95a9bcf1..6154f39f 100644 +--- a/modal_dojo/common/config.py ++++ b/modal_dojo/common/config.py +@@ -16,8 +16,10 @@ from urllib.error import HTTPError, URLError + from urllib.request import Request, urlopen + + +-CONFIG_PATH = Path.home() / ".modal-dojo.toml" +-LEGACY_CONFIG_PATH = Path.home() / ".training-gym.toml" ++# Allow a consumer to keep dashboard/auth settings scoped to its project. ++# The default retains the existing home-directory migration behavior. ++CONFIG_PATH = Path(os.environ.get("MODAL_DOJO_CONFIG_PATH") or Path.home() / ".modal-dojo.toml").expanduser().resolve() ++LEGACY_CONFIG_PATH = CONFIG_PATH.parent / ".training-gym.toml" + MODAL_CONFIG_PATH = Path( + os.environ.get("MODAL_CONFIG_PATH") or os.path.expanduser("~/.modal.toml") + ) +diff --git a/modal_dojo/common/launcher_helpers.py b/modal_dojo/common/launcher_helpers.py +index c97da897..937bb4c3 100644 +--- a/modal_dojo/common/launcher_helpers.py ++++ b/modal_dojo/common/launcher_helpers.py +@@ -45,6 +45,7 @@ from modal_dojo.common.run import ( + from modal_dojo.common.checkpoint import require_within_volume_mount + from modal_dojo.common.launcher_utils import ( + drop_materialized_config_key, ++ pin_resume_iteration, + serialize_recipe_params, + ) + from modal_dojo.common.metrics import MetricConfig, metric_metadata +@@ -705,6 +706,7 @@ def resumed_recipe(recipe: Any, save_root: str, checkpoint: dict[str, Any] | Non + "WARNING: no_save_optim=True — enabling no_load_optim for resume." + ) + recipe.no_load_optim = recipe.no_save_optim ++ pin_resume_iteration(recipe, iteration) + yield + finally: + for field, value in original.items(): +diff --git a/modal_dojo/common/launcher_utils.py b/modal_dojo/common/launcher_utils.py +index 6fa6aa24..c630e612 100644 +--- a/modal_dojo/common/launcher_utils.py ++++ b/modal_dojo/common/launcher_utils.py +@@ -572,6 +572,38 @@ def drop_materialized_config_key(cfg: Any, key: str) -> None: + ) + + ++def pin_resume_iteration(cfg: Any, iteration: int | None) -> None: ++ """Load the validated iteration, even when the run tracker points elsewhere. ++ ++ Give the reference model its own default tracker selection: otherwise ++ Megatron can apply the actor's ckpt_step to the reference checkpoint too. ++ """ ++ if iteration is None: ++ return ++ import tempfile ++ import yaml ++ ++ source = getattr(cfg, "extra_config", None) ++ if isinstance(source, str): ++ with open(source) as f: ++ data = yaml.safe_load(f) or {} ++ else: ++ data = dict(source or {}) ++ data["ckpt_step"] = iteration ++ data.setdefault("ref_ckpt_step", 0) ++ data["exit_on_missing_checkpoint"] = True ++ # This is recovery from the run's own checkpoint, not a fresh fine-tune ++ # from its parent. Megatron's finetune flag otherwise suppresses optimizer ++ # and RNG restoration even after the launcher clears no_load_optim. ++ data["finetune"] = False ++ data["no_load_rng"] = bool(data.get("no_save_rng", False)) ++ with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: ++ yaml.safe_dump(data, f) ++ object.__setattr__(cfg, "extra_config", f.name) ++ object.__setattr__(cfg, "_materialized_config_keys", tuple(data)) ++ object.__setattr__(cfg, "_materialized_config", dict(data)) ++ ++ + def build_train_cmd( + cfg: Any, + root: str, +diff --git a/modal_dojo/common/megatron_patches/patch_checkpoint_commit.py b/modal_dojo/common/megatron_patches/patch_checkpoint_commit.py +index 1369b9ad..14759005 100644 +--- a/modal_dojo/common/megatron_patches/patch_checkpoint_commit.py ++++ b/modal_dojo/common/megatron_patches/patch_checkpoint_commit.py +@@ -15,13 +15,14 @@ def save_checkpoint(*args, **kwargs): + + result = _modal_dojo_save_checkpoint(*args, **kwargs) + if not getattr(get_args(), "async_save", False): +- commit_latest_checkpoint_volume() ++ commit_latest_checkpoint_volume(get_args().save) + return result + """ + _FINALIZE_WRAP = """ + + # PATCHED_MODAL_DOJO_ASYNC_FINALIZE_COMMIT + def maybe_finalize_async_save(*args, **kwargs): ++ from megatron.training import get_args + from modal_dojo.common.torch_dist_checkpoint import ( + commit_latest_checkpoint_volume, + ) +@@ -29,7 +30,7 @@ def maybe_finalize_async_save(*args, **kwargs): + had_pending = not is_empty_async_queue() + result = _modal_dojo_maybe_finalize_async_save(*args, **kwargs) + if had_pending: +- commit_latest_checkpoint_volume() ++ commit_latest_checkpoint_volume(get_args().save) + return result + """ + +@@ -40,6 +41,21 @@ def _wrap(path: Path, original: str, renamed: str, marker: str, wrap: str) -> No + return + source = path.read_text() + if marker in source: ++ # Upgrade an image that already contains the old wrapper. ++ source = source.replace( ++ "commit_latest_checkpoint_volume()", ++ "commit_latest_checkpoint_volume(get_args().save)", ++ ) ++ if ( ++ original == "maybe_finalize_async_save" ++ and " from megatron.training import get_args" ++ not in source.split(marker, 1)[1] ++ ): ++ source = source.replace( ++ "def maybe_finalize_async_save(*args, **kwargs):", ++ "def maybe_finalize_async_save(*args, **kwargs):\n from megatron.training import get_args", ++ ) ++ path.write_text(source) + print(f"{path.name} already has {marker}") + return + if f"def {original}(" not in source: +diff --git a/modal_dojo/common/run.py b/modal_dojo/common/run.py +index 190891bd..ef05942d 100644 +--- a/modal_dojo/common/run.py ++++ b/modal_dojo/common/run.py +@@ -29,6 +29,7 @@ from modal_dojo.common.models import ModelConfig + from modal_dojo.common.status import FrameworkStatus, resolve_framework_status + from modal_dojo.common.torch_dist_checkpoint import ( + TORCH_DIST_TRACKER_NAME, ++ is_complete_torch_dist_checkpoint_dir, + parse_torch_dist_iteration, + parse_torch_dist_tracker, + ) +@@ -767,7 +768,7 @@ def torch_dist_resume_checkpoint( + if not os.path.isdir(save_path): + return None + +- is_complete = is_complete or os.path.isdir ++ is_complete = is_complete or is_complete_torch_dist_checkpoint_dir + tracker_path = os.path.join(save_path, TORCH_DIST_TRACKER_NAME) + if os.path.isfile(tracker_path): + try: +@@ -777,16 +778,14 @@ def torch_dist_resume_checkpoint( + tracker = "" + if tracker.strip() == "release": + path = os.path.join(save_path, "release") +- return ( +- _resume_checkpoint(path, "release", None) if is_complete(path) else None +- ) ++ if is_complete(path): ++ return _resume_checkpoint(path, "release", None) + iteration = parse_torch_dist_tracker(tracker) + if iteration is not None: + name = f"iter_{iteration:07d}" + path = os.path.join(save_path, name) +- return ( +- _resume_checkpoint(path, name, iteration) if is_complete(path) else None +- ) ++ if is_complete(path): ++ return _resume_checkpoint(path, name, iteration) + + try: + candidates: list[tuple[int, str, str]] = [] +diff --git a/modal_dojo/common/torch_dist_checkpoint.py b/modal_dojo/common/torch_dist_checkpoint.py +index 6c30fb11..3acb99af 100644 +--- a/modal_dojo/common/torch_dist_checkpoint.py ++++ b/modal_dojo/common/torch_dist_checkpoint.py +@@ -1,28 +1,166 @@ + from __future__ import annotations + + import os +-from collections.abc import Collection +-from pathlib import Path ++import io ++import pickle ++import json ++from collections.abc import Collection, Mapping ++from pathlib import Path, PurePosixPath + from typing import Any, Callable + + TORCH_DIST_TRACKER_NAME = "latest_checkpointed_iteration.txt" + + +-def is_complete_torch_dist_checkpoint(names: Collection[str]) -> bool: +- """``.metadata`` is written last, so it separates a finished save from a crashed +- one. ``common.pt`` is not required: newer megatron-core folds the common state +- into the torch_dist metadata and writes no such file.""" +- return ".metadata" in names and any(name.endswith(".distcp") for name in names) ++class _MetadataRecord: ++ """Inert stand-in for checkpoint metadata; never imports or executes torch.""" ++ ++ def __new__(cls, *args, **kwargs): ++ return object.__new__(cls) ++ ++ def __init__(self, *args, **kwargs): ++ pass ++ ++ def __setstate__(self, state): ++ if isinstance(state, dict): ++ self.__dict__.update(state) ++ ++ ++class _MetadataReader(pickle.Unpickler): ++ def find_class(self, module, name): ++ allowed = { ++ "torch.distributed.checkpoint.metadata": { ++ "Metadata", ++ "MetadataIndex", ++ "StorageMeta", ++ "TensorStorageMetadata", ++ "BytesStorageMetadata", ++ "TensorProperties", ++ "ChunkStorageMetadata", ++ "_MEM_FORMAT_ENCODING", ++ }, ++ "torch.distributed.checkpoint.filesystem": {"_StorageInfo"}, ++ "torch.serialization": {"_get_layout"}, ++ "torch": { ++ "Size", ++ "bfloat16", ++ "float16", ++ "float32", ++ "float64", ++ "bool", ++ "uint8", ++ "int8", ++ "int16", ++ "int32", ++ "int64", ++ "complex64", ++ "complex128", ++ "float8_e4m3fn", ++ "float8_e5m2", ++ "float8_e4m3fnuz", ++ "float8_e5m2fnuz", ++ }, ++ } ++ if name not in allowed.get(module, set()): ++ raise pickle.UnpicklingError( ++ f"Unsupported checkpoint metadata type: {module}.{name}" ++ ) ++ return _MetadataRecord ++ ++ ++def checkpoint_shard_sizes(metadata: bytes) -> dict[str, int]: ++ """Minimum file sizes required by the DCP storage offsets, without torch.""" ++ record = _MetadataReader(io.BytesIO(metadata)).load() ++ storage = getattr(record, "storage_data", None) ++ if not isinstance(storage, dict) or not storage: ++ raise ValueError("checkpoint metadata has no storage entries") ++ required: dict[str, int] = {} ++ for entry in storage.values(): ++ name = getattr(entry, "relative_path", None) ++ offset = getattr(entry, "offset", None) ++ length = getattr(entry, "length", None) ++ if ( ++ not isinstance(name, str) ++ or not name.endswith(".distcp") ++ or PurePosixPath(name).is_absolute() ++ or ".." in PurePosixPath(name).parts ++ or "\\" in name ++ or type(offset) is not int ++ or offset < 0 ++ or type(length) is not int ++ or length <= 0 ++ ): ++ raise ValueError("invalid checkpoint storage entry") ++ required[name] = max(required.get(name, 0), offset + length) ++ return required ++ ++ ++def torch_dist_checkpoint_health( ++ metadata: bytes, ++ file_sizes: Mapping[str, int], ++) -> tuple[bool, str]: ++ """Validate every referenced shard and byte range, including legacy saves.""" ++ if file_sizes.get(".metadata", 0) <= 0: ++ return False, "missing or empty checkpoint metadata" ++ try: ++ required = checkpoint_shard_sizes(metadata) ++ except ( ++ ValueError, ++ TypeError, ++ AttributeError, ++ EOFError, ++ pickle.UnpicklingError, ++ ) as exc: ++ return False, f"unreadable checkpoint metadata: {exc}" ++ missing = sorted(required.keys() - file_sizes.keys()) ++ if missing: ++ return False, f"missing referenced shards: {', '.join(missing)}" ++ truncated = sorted( ++ name for name, size in required.items() if file_sizes[name] < size ++ ) ++ if truncated: ++ return False, f"truncated referenced shards: {', '.join(truncated)}" ++ return True, f"validated {len(required)} referenced shards and byte ranges" ++ ++ ++def is_complete_torch_dist_checkpoint( ++ names: Collection[str], ++ *, ++ metadata: bytes | None = None, ++ file_sizes: Mapping[str, int] | None = None, ++) -> bool: ++ # A directory listing alone cannot establish completeness. ++ # Preserve main's support for newer Megatron saves with embedded common ++ # state: common.pt is optional, but every referenced shard must exist. ++ return bool( ++ ".metadata" in names ++ and metadata is not None ++ and file_sizes is not None ++ and torch_dist_checkpoint_health(metadata, file_sizes)[0] ++ ) + + + def is_complete_torch_dist_checkpoint_dir( + checkpoint_dir: str | os.PathLike[str], + ) -> bool: + try: +- names = {entry.name for entry in Path(checkpoint_dir).iterdir()} +- except OSError: ++ root = Path(checkpoint_dir) ++ metadata = (root / ".metadata").read_bytes() ++ required = checkpoint_shard_sizes(metadata) ++ sizes = {} ++ for name in [".metadata", "common.pt", *required]: ++ path = root / name ++ if path.resolve().is_relative_to(root.resolve()) and path.is_file(): ++ sizes[name] = path.stat().st_size ++ return torch_dist_checkpoint_health(metadata, sizes)[0] ++ except ( ++ OSError, ++ ValueError, ++ TypeError, ++ AttributeError, ++ EOFError, ++ pickle.UnpicklingError, ++ ): + return False +- return is_complete_torch_dist_checkpoint(names) + + + def parse_torch_dist_iteration(name: str) -> int | None: +@@ -102,6 +240,9 @@ def commit_checkpoint_volume_across_ranks( + + + class _TorchDistributed: ++ def __init__(self, group=None): ++ self.group = group ++ + @property + def is_coordinator(self) -> bool: + import torch.distributed as dist +@@ -114,17 +255,71 @@ class _TorchDistributed: + if not dist.is_initialized(): + return [error] + gathered: list[str | None] = [None] * dist.get_world_size() +- dist.all_gather_object(gathered, error) ++ dist.all_gather_object(gathered, error, group=self.group) + return gathered + + +-def commit_latest_checkpoint_volume() -> None: ++def _validate_committed_checkpoint(volume_name: str, save_path: str) -> None: ++ from modal import Volume ++ ++ volume = Volume.from_name(volume_name, create_if_missing=False) ++ volume.reload() ++ root = Path(save_path) ++ tracker = (root / TORCH_DIST_TRACKER_NAME).read_text().strip() ++ iteration = parse_torch_dist_tracker(tracker) ++ if tracker == "release": ++ checkpoint = root / "release" ++ elif iteration is not None: ++ checkpoint = root / f"iter_{iteration:07d}" ++ else: ++ raise RuntimeError("Checkpoint tracker is missing a valid saved iteration") ++ if not is_complete_torch_dist_checkpoint_dir(checkpoint): ++ raise RuntimeError(f"Committed checkpoint is incomplete: {checkpoint}") ++ # Publish only after all nodes committed and the committed view was checked. ++ marker = checkpoint / "modal_dojo_complete.json" ++ temporary = marker.with_suffix(".tmp") ++ temporary.write_text( ++ json.dumps( ++ { ++ "version": 1, ++ "required_shard_sizes": checkpoint_shard_sizes( ++ (checkpoint / ".metadata").read_bytes() ++ ), ++ } ++ ) ++ ) ++ temporary.replace(marker) ++ volume.commit() ++ ++ ++def commit_latest_checkpoint_volume(save_path: str | None = None) -> None: + import torch.distributed as dist ++ from torch.distributed.distributed_c10d import new_group + + volume_name = os.environ.get("MODAL_DOJO_CHECKPOINTS_VOLUME_NAME") +- barrier = dist.barrier if dist.is_initialized() else lambda: None +- commit_checkpoint_volume_across_ranks( +- volume_name, +- _TorchDistributed(), +- barrier, +- ) ++ if not volume_name: ++ return ++ # Filesystem coordination is CPU work. Avoid NCCL object collectives and ++ # their device selection/communicator churn around save and offload. ++ # Slime wraps dist.new_group in ReloadableProcessGroup after registering ++ # WORLD. Its collective wrappers unwrap that proxy, but PyTorch's destroy ++ # does not. This short-lived group must also stay out of Slime's registry ++ # so offload cannot recreate it after we have destroyed it. ++ group = new_group(backend="gloo") if dist.is_initialized() else None ++ wrapper = _TorchDistributed(group) ++ barrier = (lambda: dist.barrier(group=group)) if group is not None else lambda: None ++ try: ++ commit_checkpoint_volume_across_ranks(volume_name, wrapper, barrier) ++ error = None ++ if save_path and wrapper.is_coordinator: ++ try: ++ _validate_committed_checkpoint(volume_name, save_path) ++ except Exception as exc: ++ error = _error_text(exc) ++ _raise_gathered_errors( ++ wrapper, error, "Checkpoint committed-view validation failed" ++ ) ++ barrier() ++ finally: ++ if group is not None: ++ dist.destroy_process_group(group) +diff --git a/tests/test_checkpoint.py b/tests/test_checkpoint.py +index fc205e26..794ffc87 100644 +--- a/tests/test_checkpoint.py ++++ b/tests/test_checkpoint.py +@@ -3,6 +3,7 @@ from dataclasses import dataclass + + import modal + import pytest ++from tests.checkpoint_fixtures import metadata_bytes + + from modal_dojo.common import checkpoint as checkpoint_mod + from modal_dojo.common.checkpoint import ( +@@ -204,6 +205,7 @@ class _DirEntry: + path: str + mtime: float = 1.0 + is_directory: bool = False ++ size: int = 4096 + + def is_dir(self) -> bool: + return self.is_directory +@@ -224,6 +226,10 @@ class _ListingVolume: + return list(self.tree.get(key, [])) + + def read_file(self, path: str): ++ if path.endswith("/.metadata") and any( ++ entry.path == path for entry in self.tree.get(path.rsplit("/", 1)[0], []) ++ ): ++ return [metadata_bytes()] + if path not in self.files: + raise FileNotFoundError(path) + return [self.files[path]] +diff --git a/tests/checkpoint_fixtures.py b/tests/checkpoint_fixtures.py +new file mode 100644 +index 00000000..1954cfc3 +--- /dev/null ++++ b/tests/checkpoint_fixtures.py +@@ -0,0 +1,46 @@ ++"""Small DCP metadata fixtures, without installing torch or loading tensors.""" ++ ++import pickle ++import sys ++from types import ModuleType ++from unittest.mock import patch ++ ++ ++def metadata_bytes(entries=None): ++ if entries is None: ++ entries = [("shard.distcp", 0, 32)] ++ modules = { ++ name: ModuleType(name) ++ for name in ("torch", "torch.distributed", "torch.distributed.checkpoint") ++ } ++ classes = {} ++ for module, name in [ ++ ("torch.distributed.checkpoint.metadata", "Metadata"), ++ ("torch.distributed.checkpoint.filesystem", "_StorageInfo"), ++ ]: ++ modules[module] = ModuleType(module) ++ cls = type(name, (), {"__module__": module}) ++ setattr(modules[module], name, cls) ++ classes[name] = cls ++ record = classes["Metadata"]() ++ record.storage_data = {} ++ for index, (name, offset, length) in enumerate(entries): ++ item = classes["_StorageInfo"]() ++ item.relative_path, item.offset, item.length = name, offset, length ++ record.storage_data[index] = item ++ with patch.dict(sys.modules, modules): ++ return pickle.dumps(record, protocol=4) ++ ++ ++def write_checkpoint(root, entries=None): ++ root.mkdir(parents=True, exist_ok=True) ++ entries = entries if entries is not None else [("shard.distcp", 0, 32)] ++ (root / ".metadata").write_bytes(metadata_bytes(entries)) ++ (root / "common.pt").write_bytes(b"common") ++ for name, offset, length in entries: ++ path = root / name ++ path.parent.mkdir(parents=True, exist_ok=True) ++ if not path.exists() or path.stat().st_size < offset + length: ++ with path.open("wb") as stream: ++ stream.truncate(offset + length) ++ return root +diff --git a/tests/checkpoint_group_integration.py b/tests/checkpoint_group_integration.py +new file mode 100644 +index 00000000..a8d347d2 +--- /dev/null ++++ b/tests/checkpoint_group_integration.py +@@ -0,0 +1,67 @@ ++"""CPU regression against trainer source; set MODAL_DOJO_SLIME_SOURCE.""" ++ ++import importlib.util ++import os ++import sys ++import tempfile ++from datetime import timedelta ++from pathlib import Path ++from types import ModuleType ++import torch.distributed as dist ++import torch.multiprocessing as mp ++ ++ ++def worker(rank, rendezvous): ++ from modal_dojo.common import torch_dist_checkpoint as ckpt ++ ++ for name in ["slime", "slime.utils"]: ++ sys.modules[name] = ModuleType(name) ++ memory = ModuleType("slime.utils.memory_utils") ++ memory.available_memory = lambda: {"free_GB": 100} ++ memory.clear_memory = lambda: None ++ memory.print_memory = lambda *a: {} ++ sys.modules[memory.__name__] = memory ++ for name in ["distributed_utils", "reloadable_process_group"]: ++ full = "slime.utils." + name ++ spec = importlib.util.spec_from_file_location( ++ full, ++ str( ++ Path(os.environ["MODAL_DOJO_SLIME_SOURCE"]) / "utils" / (name + ".py") ++ ), ++ ) ++ module = importlib.util.module_from_spec(spec) ++ sys.modules[full] = module ++ spec.loader.exec_module(module) ++ reloadable = sys.modules["slime.utils.reloadable_process_group"] ++ dist.init_process_group( ++ "gloo", init_method="file://" + rendezvous, rank=rank, world_size=2 ++ ) ++ reloadable.register_default_process_group(timedelta(seconds=30)) ++ reloadable.monkey_patch_torch_dist() ++ # Reproduce the exact failure from the previous implementation. ++ proxy = dist.new_group(backend="gloo") ++ try: ++ dist.destroy_process_group(proxy) ++ except ValueError as error: ++ assert "Invalid process group" in str(error) ++ else: ++ raise AssertionError("Expected old cleanup to fail") ++ dist.destroy_process_group(proxy.group) ++ reloadable.ReloadableProcessGroup.GROUPS[os.getpid()].remove(proxy) ++ os.environ["LOCAL_RANK"] = "0" ++ os.environ["MODAL_DOJO_CHECKPOINTS_VOLUME_NAME"] = "test" ++ ckpt._commit_volume = lambda name: None ++ ckpt._validate_committed_checkpoint = lambda *args: None ++ for _ in range(3): ++ ckpt.commit_latest_checkpoint_volume("/test") ++ assert not reloadable.ReloadableProcessGroup.GROUPS[os.getpid()] ++ dist.barrier() ++ dist.destroy_process_group() ++ print( ++ f"rank {rank}: old error reproduced; three corrected saves passed", flush=True ++ ) ++ ++ ++if __name__ == "__main__": ++ with tempfile.TemporaryDirectory() as root: ++ mp.spawn(worker, args=(str(Path(root) / "rendezvous"),), nprocs=2, join=True) +diff --git a/tests/test_torch_dist_checkpoint.py b/tests/test_torch_dist_checkpoint.py +new file mode 100644 +index 00000000..05e9d8a6 +--- /dev/null ++++ b/tests/test_torch_dist_checkpoint.py +@@ -0,0 +1,265 @@ ++import pickle ++import sys ++from pathlib import Path ++from types import SimpleNamespace ++ ++import pytest ++import yaml ++ ++from modal_dojo.common import torch_dist_checkpoint as ckpt ++from modal_dojo.common.launcher_utils import pin_resume_iteration ++from modal_dojo.common.run import torch_dist_resume_checkpoint ++from tests.checkpoint_fixtures import metadata_bytes, write_checkpoint ++ ++ ++def test_all_referenced_files_and_offsets_are_required(tmp_path): ++ root = write_checkpoint( ++ tmp_path / "iter_0000449", ++ [ ++ ("__0_0.distcp", 0, 8), ++ ("__1_0.distcp", 0, 16), ++ ("__1_0.distcp", 16, 32), ++ ], ++ ) ++ assert ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ (root / "__1_0.distcp").write_bytes(b"x" * 47) ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ (root / "__1_0.distcp").unlink() ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ ++ ++def test_broad_layover_missing_shard_pattern(tmp_path): ++ root = write_checkpoint( ++ tmp_path, ++ [(f"__{rank}_{part}.distcp", 0, 8) for rank in range(64) for part in range(2)], ++ ) ++ for rank in range(40, 56): ++ (root / f"__{rank}_1.distcp").unlink() ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ assert not ckpt.is_complete_torch_dist_checkpoint([p.name for p in root.iterdir()]) ++ ++ ++@pytest.mark.parametrize( ++ "entry", ++ [ ++ ("../outside.distcp", 0, 1), ++ ("/outside.distcp", 0, 1), ++ ("shard.distcp", -1, 1), ++ ("shard.distcp", 0, 0), ++ ], ++) ++def test_invalid_storage_entries_are_rejected(entry): ++ with pytest.raises(ValueError): ++ ckpt.checkpoint_shard_sizes(metadata_bytes([entry])) ++ ++ ++def test_metadata_does_not_execute_pickle_globals(tmp_path): ++ class Payload: ++ def __reduce__(self): ++ return (eval, ("1+1",)) ++ ++ with pytest.raises(pickle.UnpicklingError, match="Unsupported"): ++ ckpt.checkpoint_shard_sizes(pickle.dumps(Payload())) ++ root = write_checkpoint(tmp_path) ++ (root / ".metadata").write_bytes(b"broken") ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ ++ ++def test_resume_falls_back_when_tracker_checkpoint_is_truncated(tmp_path): ++ write_checkpoint(tmp_path / "iter_0000399") ++ bad = write_checkpoint(tmp_path / "iter_0000449") ++ (bad / "shard.distcp").write_bytes(b"short") ++ (tmp_path / ckpt.TORCH_DIST_TRACKER_NAME).write_text("449\n") ++ result = torch_dist_resume_checkpoint( ++ str(tmp_path), is_complete=ckpt.is_complete_torch_dist_checkpoint_dir ++ ) ++ assert result["resume_from_iteration"] == 399 ++ (tmp_path / "iter_0000399" / ".metadata").unlink() ++ assert ( ++ torch_dist_resume_checkpoint( ++ str(tmp_path), is_complete=ckpt.is_complete_torch_dist_checkpoint_dir ++ ) ++ is None ++ ) ++ ++ ++@pytest.mark.parametrize("reference", [None, 17]) ++def test_resume_pins_actor_without_reusing_actor_step_for_reference( ++ tmp_path, reference ++): ++ source = tmp_path / "config.yaml" ++ data = {"ckpt_step": 449, "qkv_format": "thd"} ++ if reference is not None: ++ data["ref_ckpt_step"] = reference ++ source.write_text(yaml.safe_dump(data)) ++ cfg = SimpleNamespace(extra_config=str(source)) ++ pin_resume_iteration(cfg, 399) ++ actual = yaml.safe_load(Path(cfg.extra_config).read_text()) ++ assert actual == { ++ **data, ++ "ckpt_step": 399, ++ "ref_ckpt_step": reference or 0, ++ "exit_on_missing_checkpoint": True, ++ "finetune": False, ++ "no_load_rng": False, ++ } ++ assert yaml.safe_load(source.read_text()) == data ++ Path(cfg.extra_config).unlink() ++ ++ ++@pytest.mark.parametrize("no_save_rng", [False, True]) ++def test_retry_restores_training_state_after_fresh_parent_load(no_save_rng): ++ source = { ++ "ckpt_step": 399, ++ "ref_ckpt_step": 0, ++ "finetune": True, ++ "no_load_rng": True, ++ "no_save_rng": no_save_rng, ++ } ++ cfg = SimpleNamespace(extra_config=source) ++ pin_resume_iteration(cfg, 19) ++ actual = yaml.safe_load(Path(cfg.extra_config).read_text()) ++ assert actual["ckpt_step"] == 19 ++ assert actual["ref_ckpt_step"] == 0 ++ assert actual["finetune"] is False ++ assert actual["no_load_rng"] is no_save_rng ++ assert source["finetune"] is True ++ assert source["ckpt_step"] == 399 ++ Path(cfg.extra_config).unlink() ++ ++ ++def test_completion_marker_requires_durable_complete_view(tmp_path, monkeypatch): ++ root = write_checkpoint(tmp_path / "iter_0000449") ++ (tmp_path / ckpt.TORCH_DIST_TRACKER_NAME).write_text("449\n") ++ calls = [] ++ volume = SimpleNamespace( ++ reload=lambda: calls.append("reload"), commit=lambda: calls.append("commit") ++ ) ++ from modal import Volume ++ ++ monkeypatch.setattr(Volume, "from_name", lambda *a, **k: volume) ++ (root / "shard.distcp").unlink() ++ with pytest.raises(RuntimeError, match="incomplete"): ++ ckpt._validate_committed_checkpoint("volume", str(tmp_path)) ++ assert not (root / "modal_dojo_complete.json").exists() ++ assert calls == ["reload"] ++ (root / "shard.distcp").write_bytes(b"x" * 32) ++ ckpt._validate_committed_checkpoint("volume", str(tmp_path)) ++ assert (root / "modal_dojo_complete.json").exists() ++ assert calls == ["reload", "reload", "commit"] ++ ++ ++@pytest.mark.parametrize("coordinator", [False, True]) ++def test_each_node_commits_before_coordinator(coordinator, monkeypatch): ++ events = [] ++ monkeypatch.setenv("LOCAL_RANK", "0") ++ monkeypatch.setattr(ckpt, "_commit_volume", lambda v: events.append("commit")) ++ ++ def gather(error): ++ events.append("gather") ++ return [error] ++ ++ wrapper = SimpleNamespace(is_coordinator=coordinator, all_gather_object=gather) ++ ckpt.commit_checkpoint_volume_across_ranks( ++ "volume", wrapper, lambda: events.append("barrier") ++ ) ++ assert events == ( ++ ["gather", "barrier", "commit", "gather", "barrier"] ++ if coordinator ++ else ["commit", "gather", "barrier", "gather", "barrier"] ++ ) ++ ++ ++def test_failed_shard_commit_stops_before_coordinator_publish(monkeypatch): ++ wrapper = SimpleNamespace( ++ is_coordinator=True, all_gather_object=lambda error: [None, "disk failed"] ++ ) ++ monkeypatch.setattr( ++ ckpt, "_commit_volume", lambda v: pytest.fail("must not publish") ++ ) ++ with pytest.raises(RuntimeError, match="shard commit failed"): ++ ckpt.commit_checkpoint_volume_across_ranks("volume", wrapper, lambda: None) ++ ++ ++def test_save_coordination_uses_gloo_and_cleans_up(monkeypatch): ++ events = [] ++ group = object() ++ ++ def new_group(*, backend): ++ assert backend == "gloo" ++ events.append("gloo") ++ return group ++ ++ def gather(output, value, **kwargs): ++ assert kwargs["group"] is group ++ output[:] = [value] ++ ++ def barrier(**kwargs): ++ assert kwargs["group"] is group ++ ++ dist = SimpleNamespace( ++ is_initialized=lambda: True, ++ get_rank=lambda: 0, ++ get_world_size=lambda: 1, ++ new_group=lambda **kwargs: pytest.fail("must bypass Slime's group wrapper"), ++ all_gather_object=gather, ++ barrier=barrier, ++ destroy_process_group=lambda g: events.append("destroy"), ++ ) ++ monkeypatch.setitem(sys.modules, "torch", SimpleNamespace(distributed=dist)) ++ monkeypatch.setitem(sys.modules, "torch.distributed", dist) ++ monkeypatch.setitem( ++ sys.modules, ++ "torch.distributed.distributed_c10d", ++ SimpleNamespace(new_group=new_group), ++ ) ++ monkeypatch.setenv("LOCAL_RANK", "0") ++ monkeypatch.setenv("MODAL_DOJO_CHECKPOINTS_VOLUME_NAME", "volume") ++ monkeypatch.setattr(ckpt, "_commit_volume", lambda v: events.append("commit")) ++ monkeypatch.setattr( ++ ckpt, "_validate_committed_checkpoint", lambda *a: events.append("validate") ++ ) ++ ckpt.commit_latest_checkpoint_volume("/checkpoints/run") ++ assert events == ["gloo", "commit", "validate", "destroy"] ++ ++ ++def test_save_patch_upgrades_old_wrappers_and_is_idempotent(tmp_path, monkeypatch): ++ from modal_dojo.common.megatron_patches import ( ++ patch_checkpoint_commit as patcher, ++ ) ++ ++ saving = tmp_path / "checkpointing.py" ++ finalize = tmp_path / "async_utils.py" ++ saving.write_text("def save_checkpoint(*args, **kwargs):\n return 7\n") ++ finalize.write_text( ++ "def maybe_finalize_async_save(*args, **kwargs):\n return 8\n" ++ ) ++ monkeypatch.setattr(patcher, "_CHECKPOINTING", saving) ++ monkeypatch.setattr(patcher, "_ASYNC_UTILS", finalize) ++ patcher.main() ++ for path in (saving, finalize): ++ path.write_text( ++ path.read_text().replace( ++ "commit_latest_checkpoint_volume(get_args().save)", ++ "commit_latest_checkpoint_volume()", ++ ) ++ ) ++ finalize.write_text( ++ finalize.read_text().replace(" from megatron.training import get_args\n", "") ++ ) ++ patcher.main() ++ first = [path.read_text() for path in (saving, finalize)] ++ patcher.main() ++ assert first == [path.read_text() for path in (saving, finalize)] ++ for source in first: ++ compile(source, "patched", "exec") ++ assert "commit_latest_checkpoint_volume(get_args().save)" in source ++ assert "from megatron.training import get_args" in source ++ ++ ++def test_checkpoint_without_separate_common_state_remains_supported(tmp_path): ++ root = write_checkpoint(tmp_path) ++ (root / "common.pt").unlink() ++ assert ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ (root / "shard.distcp").write_bytes(b"short") ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) +diff --git a/tests/test_balatro_resume_recipe.py b/tests/test_balatro_resume_recipe.py +new file mode 100644 +index 00000000..4878b6e3 +--- /dev/null ++++ b/tests/test_balatro_resume_recipe.py +@@ -0,0 +1,31 @@ ++from pathlib import Path ++from types import SimpleNamespace ++ ++import yaml ++ ++from modal_dojo.common.launcher_helpers import resumed_recipe ++ ++ ++def test_recovery_loads_own_checkpoint_and_restores_optimizer(tmp_path): ++ config = tmp_path / "recipe.yaml" ++ config.write_text(yaml.safe_dump({ ++ "ckpt_step": 399, "ref_ckpt_step": 0, "finetune": True, ++ "no_load_rng": True, "start_rollout_id": 0, ++ })) ++ recipe = SimpleNamespace( ++ save="/run", load="/parent", ref_load="/base", start_rollout_id=0, ++ no_load_optim=True, no_save_optim=False, num_rollout=200, ++ extra_config=str(config), effective_num_epoch=lambda: None, ++ _ESCAPE_HATCH_FIELD="extra_config", _materialized_config_keys=("start_rollout_id",), ++ ) ++ with resumed_recipe(recipe, "/run", { ++ "resume_from_iteration": 4, "resume_checkpoint_path": "/run/iter_0000004", ++ }): ++ assert recipe.load == "/run" and not recipe.no_load_optim ++ assert recipe.start_rollout_id is None ++ data = yaml.safe_load(Path(recipe.extra_config).read_text()) ++ assert data["ckpt_step"] == 4 and data["ref_ckpt_step"] == 0 ++ assert data["finetune"] is False and data["no_load_rng"] is False ++ assert "start_rollout_id" not in data ++ assert recipe._materialized_config == data ++ assert recipe.load == "/parent" and recipe.no_load_optim diff --git a/training/dojo.sh b/training/dojo.sh new file mode 100755 index 00000000..96723bab --- /dev/null +++ b/training/dojo.sh @@ -0,0 +1,9 @@ +#!/bin/sh +# Use Balatro's pinned Dojo package and project-local dashboard settings. +set -eu +training_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +export MODAL_PROFILE="${MODAL_PROFILE:-modal-labs}" +export MODAL_ENVIRONMENT="${MODAL_ENVIRONMENT:-joy-dev}" +export MODAL_DOJO_CONFIG_PATH="${MODAL_DOJO_CONFIG_PATH:-$training_dir/.dojo-config.toml}" +unset VIRTUAL_ENV +exec uv run --project "$training_dir" "$@" diff --git a/training/evaluation_snapshot.py b/training/evaluation_snapshot.py new file mode 100644 index 00000000..7b720355 --- /dev/null +++ b/training/evaluation_snapshot.py @@ -0,0 +1,79 @@ +"""Extract held-out episode results and paired-seed comparisons from a Ray log.""" + +import argparse +import json +import math +import statistics +import subprocess +from pathlib import Path + + +def summarize(episodes, size=32): + ordered = sorted({e["episode_id"]: e for e in episodes}.values(), key=lambda e: e["episode_id"]) + batches = [] + baseline = None + for offset in range(0, len(ordered), size): + batch = ordered[offset:offset + size] + if len(batch) != size: + break + by_seed = {e["seed"]: e for e in batch} + if len(by_seed) != size: + raise ValueError("Incomplete or overlapping evaluation batches; inspect episode log") + baseline = baseline or by_seed + if by_seed.keys() != baseline.keys(): + raise ValueError("Evaluation seed sets differ") + delta = [by_seed[seed]["reward"] - baseline[seed]["reward"] for seed in baseline] + batches.append({ + "batch_index": len(batches), "games": size, + "mean_reward": statistics.mean(e["reward"] for e in batch), + "mean_ante": statistics.mean(e["ante"] for e in batch), + "wins": sum(e["won"] for e in batch), + "capped": sum(e["capped"] for e in batch), + "invalid_actions": sum(e["invalid_actions"] for e in batch), + "turns": sum(e["turns"] for e in batch), + "paired_reward_delta": statistics.mean(delta), + "paired_delta_standard_error": statistics.stdev(delta) / math.sqrt(size), + "seeds_better": sum(value > 0 for value in delta), + "seeds_worse": sum(value < 0 for value in delta), + }) + return {"completed_batches": batches, "partial_batch_games": len(ordered) % size} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("run_id") + parser.add_argument("--head", required=True) + parser.add_argument("--driver-log", required=True) + args = parser.parse_args() + remote = f''' +import json, time +from pathlib import Path +episodes = {{}} +for line in Path({args.driver_log!r}).read_text().splitlines(): + marker = "BALATRO_EPISODE " + if marker not in line: + continue + try: + event, _ = json.JSONDecoder().raw_decode(line.split(marker, 1)[1]) + except ValueError: + continue + if event.get("seed", "").startswith("BALATRO-EVAL-"): + episodes[event["episode_id"]] = event +print(json.dumps(list(episodes.values())), flush=True) +time.sleep(.3) +''' + process = subprocess.run( + ["modal", "container", "exec", args.head, "--", "python", "-c", remote], + text=True, capture_output=True, timeout=45, check=True, + ) + episodes = json.loads(process.stdout) + root = Path(__file__).resolve().parents[1] / "reports" + (root / f"{args.run_id}-eval-episodes.json").write_text(json.dumps(episodes, indent=2)) + result = summarize(episodes) + result["note"] = "Batch indices follow this Ray attempt's episode order; map to recorded eval steps before comparing across retries." + (root / f"{args.run_id}-eval-summary.json").write_text(json.dumps(result, indent=2)) + print(json.dumps(result), flush=True) + + +if __name__ == "__main__": + main() diff --git a/training/gym-checkpoint-recovery.patch b/training/gym-checkpoint-recovery.patch new file mode 100644 index 00000000..bc7b31b2 --- /dev/null +++ b/training/gym-checkpoint-recovery.patch @@ -0,0 +1,875 @@ +diff --git a/modal_training_gym/common/checkpoint.py b/modal_training_gym/common/checkpoint.py +index bbcd2eb5..2fc9608f 100644 +--- a/modal_training_gym/common/checkpoint.py ++++ b/modal_training_gym/common/checkpoint.py +@@ -138,18 +138,26 @@ def _list_checkpoints( + continue + child_rel = f"{rel}/{name}" if rel else name + try: +- child_names = { +- _entry_name(child) ++ child_sizes = { ++ _entry_name(child): getattr(child, "size", 0) + for child in volume.iterdir(child_rel, recursive=False) ++ if not _is_dir_entry(child) + } + except (FileNotFoundError, NotFoundError): +- child_names = set() ++ child_sizes = {} + iteration = parse_torch_dist_iteration(name) + if ( + tracker_iteration is None + or iteration is None + or iteration > tracker_iteration +- or not is_complete_torch_dist_checkpoint(child_names) ++ ): ++ continue ++ try: ++ metadata = b"".join(volume.read_file(f"{child_rel}/.metadata")) ++ except (FileNotFoundError, NotFoundError): ++ continue ++ if not is_complete_torch_dist_checkpoint( ++ child_sizes.keys(), metadata=metadata, file_sizes=child_sizes + ): + continue + checkpoints.append( +diff --git a/modal_training_gym/common/launcher_utils.py b/modal_training_gym/common/launcher_utils.py +index 05b8419d..378de156 100644 +--- a/modal_training_gym/common/launcher_utils.py ++++ b/modal_training_gym/common/launcher_utils.py +@@ -513,6 +513,37 @@ def drop_materialized_config_key(cfg: Any, key: str) -> None: + ) + + ++def pin_resume_iteration(cfg: Any, iteration: int | None) -> None: ++ """Load the validated iteration, even when the run tracker points elsewhere. ++ ++ Give the reference model its own default tracker selection: otherwise ++ Megatron can apply the actor's ckpt_step to the reference checkpoint too. ++ """ ++ if iteration is None: ++ return ++ import tempfile ++ import yaml ++ ++ source = getattr(cfg, "extra_config", None) ++ if isinstance(source, str): ++ with open(source) as f: ++ data = yaml.safe_load(f) or {} ++ else: ++ data = dict(source or {}) ++ data["ckpt_step"] = iteration ++ data.setdefault("ref_ckpt_step", 0) ++ data["exit_on_missing_checkpoint"] = True ++ # This is recovery from the run's own checkpoint, not a fresh fine-tune ++ # from its parent. Megatron's finetune flag otherwise suppresses optimizer ++ # and RNG restoration even after the launcher clears no_load_optim. ++ data["finetune"] = False ++ data["no_load_rng"] = bool(data.get("no_save_rng", False)) ++ with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: ++ yaml.safe_dump(data, f) ++ object.__setattr__(cfg, "extra_config", f.name) ++ object.__setattr__(cfg, "_materialized_config_keys", tuple(data)) ++ ++ + def build_train_cmd( + cfg: Any, + root: str, +diff --git a/modal_training_gym/common/megatron_patches/patch_checkpoint_commit.py b/modal_training_gym/common/megatron_patches/patch_checkpoint_commit.py +index f7f0dd74..1154a4b4 100644 +--- a/modal_training_gym/common/megatron_patches/patch_checkpoint_commit.py ++++ b/modal_training_gym/common/megatron_patches/patch_checkpoint_commit.py +@@ -15,13 +15,14 @@ def save_checkpoint(*args, **kwargs): + + result = _training_gym_save_checkpoint(*args, **kwargs) + if not getattr(get_args(), "async_save", False): +- commit_latest_checkpoint_volume() ++ commit_latest_checkpoint_volume(get_args().save) + return result + """ + _FINALIZE_WRAP = """ + + # PATCHED_TRAINING_GYM_ASYNC_FINALIZE_COMMIT + def maybe_finalize_async_save(*args, **kwargs): ++ from megatron.training import get_args + from modal_training_gym.common.torch_dist_checkpoint import ( + commit_latest_checkpoint_volume, + ) +@@ -29,7 +30,7 @@ def maybe_finalize_async_save(*args, **kwargs): + had_pending = not is_empty_async_queue() + result = _training_gym_maybe_finalize_async_save(*args, **kwargs) + if had_pending: +- commit_latest_checkpoint_volume() ++ commit_latest_checkpoint_volume(get_args().save) + return result + """ + +@@ -40,6 +41,21 @@ def _wrap(path: Path, original: str, renamed: str, marker: str, wrap: str) -> No + return + source = path.read_text() + if marker in source: ++ # Upgrade an image that already contains the old wrapper. ++ source = source.replace( ++ "commit_latest_checkpoint_volume()", ++ "commit_latest_checkpoint_volume(get_args().save)", ++ ) ++ if ( ++ original == "maybe_finalize_async_save" ++ and " from megatron.training import get_args" ++ not in source.split(marker, 1)[1] ++ ): ++ source = source.replace( ++ "def maybe_finalize_async_save(*args, **kwargs):", ++ "def maybe_finalize_async_save(*args, **kwargs):\n from megatron.training import get_args", ++ ) ++ path.write_text(source) + print(f"{path.name} already has {marker}") + return + if f"def {original}(" not in source: +diff --git a/modal_training_gym/common/run.py b/modal_training_gym/common/run.py +index 3c7d950b..876e8664 100644 +--- a/modal_training_gym/common/run.py ++++ b/modal_training_gym/common/run.py +@@ -29,6 +29,7 @@ from modal_training_gym.common.models import ModelConfig + from modal_training_gym.common.status import FrameworkStatus, resolve_framework_status + from modal_training_gym.common.torch_dist_checkpoint import ( + TORCH_DIST_TRACKER_NAME, ++ is_complete_torch_dist_checkpoint_dir, + parse_torch_dist_iteration, + parse_torch_dist_tracker, + ) +@@ -761,7 +762,7 @@ def torch_dist_resume_checkpoint( + if not os.path.isdir(save_path): + return None + +- is_complete = is_complete or os.path.isdir ++ is_complete = is_complete or is_complete_torch_dist_checkpoint_dir + tracker_path = os.path.join(save_path, TORCH_DIST_TRACKER_NAME) + if os.path.isfile(tracker_path): + try: +@@ -771,16 +772,14 @@ def torch_dist_resume_checkpoint( + tracker = "" + if tracker.strip() == "release": + path = os.path.join(save_path, "release") +- return ( +- _resume_checkpoint(path, "release", None) if is_complete(path) else None +- ) ++ if is_complete(path): ++ return _resume_checkpoint(path, "release", None) + iteration = parse_torch_dist_tracker(tracker) + if iteration is not None: + name = f"iter_{iteration:07d}" + path = os.path.join(save_path, name) +- return ( +- _resume_checkpoint(path, name, iteration) if is_complete(path) else None +- ) ++ if is_complete(path): ++ return _resume_checkpoint(path, name, iteration) + + try: + candidates: list[tuple[int, str, str]] = [] +diff --git a/modal_training_gym/common/torch_dist_checkpoint.py b/modal_training_gym/common/torch_dist_checkpoint.py +index 2010e77b..85725c19 100644 +--- a/modal_training_gym/common/torch_dist_checkpoint.py ++++ b/modal_training_gym/common/torch_dist_checkpoint.py +@@ -1,8 +1,11 @@ + from __future__ import annotations + + import os +-from collections.abc import Collection +-from pathlib import Path ++import io ++import pickle ++import json ++from collections.abc import Collection, Mapping ++from pathlib import Path, PurePosixPath + from typing import Any, Callable + + from modal import Volume +@@ -10,11 +13,130 @@ from modal import Volume + TORCH_DIST_TRACKER_NAME = "latest_checkpointed_iteration.txt" + + +-def is_complete_torch_dist_checkpoint(names: Collection[str]) -> bool: +- return ( ++class _MetadataRecord: ++ """Inert stand-in for checkpoint metadata; never imports or executes torch.""" ++ ++ def __new__(cls, *args, **kwargs): ++ return object.__new__(cls) ++ ++ def __init__(self, *args, **kwargs): ++ pass ++ ++ def __setstate__(self, state): ++ if isinstance(state, dict): ++ self.__dict__.update(state) ++ ++ ++class _MetadataReader(pickle.Unpickler): ++ def find_class(self, module, name): ++ allowed = { ++ "torch.distributed.checkpoint.metadata": { ++ "Metadata", ++ "MetadataIndex", ++ "StorageMeta", ++ "TensorStorageMetadata", ++ "BytesStorageMetadata", ++ "TensorProperties", ++ "ChunkStorageMetadata", ++ "_MEM_FORMAT_ENCODING", ++ }, ++ "torch.distributed.checkpoint.filesystem": {"_StorageInfo"}, ++ "torch.serialization": {"_get_layout"}, ++ "torch": { ++ "Size", ++ "bfloat16", ++ "float16", ++ "float32", ++ "float64", ++ "bool", ++ "uint8", ++ "int8", ++ "int16", ++ "int32", ++ "int64", ++ "complex64", ++ "complex128", ++ "float8_e4m3fn", ++ "float8_e5m2", ++ "float8_e4m3fnuz", ++ "float8_e5m2fnuz", ++ }, ++ } ++ if name not in allowed.get(module, set()): ++ raise pickle.UnpicklingError( ++ f"Unsupported checkpoint metadata type: {module}.{name}" ++ ) ++ return _MetadataRecord ++ ++ ++def checkpoint_shard_sizes(metadata: bytes) -> dict[str, int]: ++ """Minimum file sizes required by the DCP storage offsets, without torch.""" ++ record = _MetadataReader(io.BytesIO(metadata)).load() ++ storage = getattr(record, "storage_data", None) ++ if not isinstance(storage, dict) or not storage: ++ raise ValueError("checkpoint metadata has no storage entries") ++ required: dict[str, int] = {} ++ for entry in storage.values(): ++ name = getattr(entry, "relative_path", None) ++ offset = getattr(entry, "offset", None) ++ length = getattr(entry, "length", None) ++ if ( ++ not isinstance(name, str) ++ or not name.endswith(".distcp") ++ or PurePosixPath(name).is_absolute() ++ or ".." in PurePosixPath(name).parts ++ or "\\" in name ++ or type(offset) is not int ++ or offset < 0 ++ or type(length) is not int ++ or length <= 0 ++ ): ++ raise ValueError("invalid checkpoint storage entry") ++ required[name] = max(required.get(name, 0), offset + length) ++ return required ++ ++ ++def torch_dist_checkpoint_health( ++ metadata: bytes, ++ file_sizes: Mapping[str, int], ++) -> tuple[bool, str]: ++ """Validate every referenced shard and byte range, including legacy saves.""" ++ if any(file_sizes.get(name, 0) <= 0 for name in (".metadata", "common.pt")): ++ return False, "missing or empty checkpoint metadata/common state" ++ try: ++ required = checkpoint_shard_sizes(metadata) ++ except ( ++ ValueError, ++ TypeError, ++ AttributeError, ++ EOFError, ++ pickle.UnpicklingError, ++ ) as exc: ++ return False, f"unreadable checkpoint metadata: {exc}" ++ missing = sorted(required.keys() - file_sizes.keys()) ++ if missing: ++ return False, f"missing referenced shards: {', '.join(missing)}" ++ truncated = sorted( ++ name for name, size in required.items() if file_sizes[name] < size ++ ) ++ if truncated: ++ return False, f"truncated referenced shards: {', '.join(truncated)}" ++ return True, f"validated {len(required)} referenced shards and byte ranges" ++ ++ ++def is_complete_torch_dist_checkpoint( ++ names: Collection[str], ++ *, ++ metadata: bytes | None = None, ++ file_sizes: Mapping[str, int] | None = None, ++) -> bool: ++ # A directory listing alone cannot establish completeness. ++ return bool( + ".metadata" in names + and "common.pt" in names +- and any(name.endswith(".distcp") for name in names) ++ and metadata is not None ++ and file_sizes is not None ++ and torch_dist_checkpoint_health(metadata, file_sizes)[0] + ) + + +@@ -22,10 +144,24 @@ def is_complete_torch_dist_checkpoint_dir( + checkpoint_dir: str | os.PathLike[str], + ) -> bool: + try: +- names = {entry.name for entry in Path(checkpoint_dir).iterdir()} +- except OSError: ++ root = Path(checkpoint_dir) ++ metadata = (root / ".metadata").read_bytes() ++ required = checkpoint_shard_sizes(metadata) ++ sizes = {} ++ for name in [".metadata", "common.pt", *required]: ++ path = root / name ++ if path.resolve().is_relative_to(root.resolve()) and path.is_file(): ++ sizes[name] = path.stat().st_size ++ return torch_dist_checkpoint_health(metadata, sizes)[0] ++ except ( ++ OSError, ++ ValueError, ++ TypeError, ++ AttributeError, ++ EOFError, ++ pickle.UnpicklingError, ++ ): + return False +- return is_complete_torch_dist_checkpoint(names) + + + def parse_torch_dist_iteration(name: str) -> int | None: +@@ -103,6 +239,9 @@ def commit_checkpoint_volume_across_ranks( + + + class _TorchDistributed: ++ def __init__(self, group=None): ++ self.group = group ++ + @property + def is_coordinator(self) -> bool: + import torch.distributed as dist +@@ -115,17 +254,69 @@ class _TorchDistributed: + if not dist.is_initialized(): + return [error] + gathered: list[str | None] = [None] * dist.get_world_size() +- dist.all_gather_object(gathered, error) ++ dist.all_gather_object(gathered, error, group=self.group) + return gathered + + +-def commit_latest_checkpoint_volume() -> None: ++def _validate_committed_checkpoint(volume_name: str, save_path: str) -> None: ++ volume = Volume.from_name(volume_name, create_if_missing=False) ++ volume.reload() ++ root = Path(save_path) ++ tracker = (root / TORCH_DIST_TRACKER_NAME).read_text().strip() ++ iteration = parse_torch_dist_tracker(tracker) ++ if tracker == "release": ++ checkpoint = root / "release" ++ elif iteration is not None: ++ checkpoint = root / f"iter_{iteration:07d}" ++ else: ++ raise RuntimeError("Checkpoint tracker is missing a valid saved iteration") ++ if not is_complete_torch_dist_checkpoint_dir(checkpoint): ++ raise RuntimeError(f"Committed checkpoint is incomplete: {checkpoint}") ++ # Publish only after all nodes committed and the committed view was checked. ++ marker = checkpoint / "training_gym_complete.json" ++ temporary = marker.with_suffix(".tmp") ++ temporary.write_text( ++ json.dumps( ++ { ++ "version": 1, ++ "required_shard_sizes": checkpoint_shard_sizes( ++ (checkpoint / ".metadata").read_bytes() ++ ), ++ } ++ ) ++ ) ++ temporary.replace(marker) ++ volume.commit() ++ ++ ++def commit_latest_checkpoint_volume(save_path: str | None = None) -> None: + import torch.distributed as dist ++ from torch.distributed.distributed_c10d import new_group + + volume_name = os.environ.get("TRAINING_GYM_CHECKPOINTS_VOLUME_NAME") +- barrier = dist.barrier if dist.is_initialized() else lambda: None +- commit_checkpoint_volume_across_ranks( +- volume_name, +- _TorchDistributed(), +- barrier, +- ) ++ if not volume_name: ++ return ++ # Filesystem coordination is CPU work. Avoid NCCL object collectives and ++ # their device selection/communicator churn around save and offload. ++ # Slime wraps dist.new_group in ReloadableProcessGroup after registering ++ # WORLD. Its collective wrappers unwrap that proxy, but PyTorch's destroy ++ # does not. This short-lived group must also stay out of Slime's registry ++ # so offload cannot recreate it after we have destroyed it. ++ group = new_group(backend="gloo") if dist.is_initialized() else None ++ wrapper = _TorchDistributed(group) ++ barrier = (lambda: dist.barrier(group=group)) if group is not None else lambda: None ++ try: ++ commit_checkpoint_volume_across_ranks(volume_name, wrapper, barrier) ++ error = None ++ if save_path and wrapper.is_coordinator: ++ try: ++ _validate_committed_checkpoint(volume_name, save_path) ++ except Exception as exc: ++ error = _error_text(exc) ++ _raise_gathered_errors( ++ wrapper, error, "Checkpoint committed-view validation failed" ++ ) ++ barrier() ++ finally: ++ if group is not None: ++ dist.destroy_process_group(group) +diff --git a/modal_training_gym/frameworks/slime/launcher.py b/modal_training_gym/frameworks/slime/launcher.py +index e6fbb3e1..395725b8 100644 +--- a/modal_training_gym/frameworks/slime/launcher.py ++++ b/modal_training_gym/frameworks/slime/launcher.py +@@ -66,6 +66,7 @@ from modal_training_gym.common.launcher_helpers import ( + from modal_training_gym.common.train_result import save_train_result_blob + from modal_training_gym.common.launcher_utils import ( + drop_materialized_config_key, ++ pin_resume_iteration, + serialize_recipe_params, + timing_debug_env, + ) +@@ -1151,6 +1152,7 @@ def build_slime_app( + # even for runs launched with an explicit start_rollout_id. + object.__setattr__(slime, "start_rollout_id", None) + drop_materialized_config_key(slime, "start_rollout_id") ++ pin_resume_iteration(slime, resume_from_iteration) + # This run's saves include Adam only when no_save_optim is false. + # TrainConfig.resume_from_checkpoint forces no_load_optim for the source seed; + # that flag is not a property of later saves in this directory. +diff --git a/tests/test_checkpoint.py b/tests/test_checkpoint.py +index 16cb766a..cfdd2782 100644 +--- a/tests/test_checkpoint.py ++++ b/tests/test_checkpoint.py +@@ -3,6 +3,7 @@ from dataclasses import dataclass + + import modal + import pytest ++from tests.checkpoint_fixtures import metadata_bytes + + from modal_training_gym.common import checkpoint as checkpoint_mod + from modal_training_gym.common.checkpoint import ( +@@ -204,6 +205,7 @@ class _DirEntry: + path: str + mtime: float = 1.0 + is_directory: bool = False ++ size: int = 4096 + + def is_dir(self) -> bool: + return self.is_directory +@@ -224,6 +226,10 @@ class _ListingVolume: + return list(self.tree.get(key, [])) + + def read_file(self, path: str): ++ if path.endswith("/.metadata") and any( ++ entry.path == path for entry in self.tree.get(path.rsplit("/", 1)[0], []) ++ ): ++ return [metadata_bytes()] + if path not in self.files: + raise FileNotFoundError(path) + return [self.files[path]] +diff --git a/tests/checkpoint_fixtures.py b/tests/checkpoint_fixtures.py +new file mode 100644 +index 00000000..1954cfc3 +--- /dev/null ++++ b/tests/checkpoint_fixtures.py +@@ -0,0 +1,46 @@ ++"""Small DCP metadata fixtures, without installing torch or loading tensors.""" ++ ++import pickle ++import sys ++from types import ModuleType ++from unittest.mock import patch ++ ++ ++def metadata_bytes(entries=None): ++ if entries is None: ++ entries = [("shard.distcp", 0, 32)] ++ modules = { ++ name: ModuleType(name) ++ for name in ("torch", "torch.distributed", "torch.distributed.checkpoint") ++ } ++ classes = {} ++ for module, name in [ ++ ("torch.distributed.checkpoint.metadata", "Metadata"), ++ ("torch.distributed.checkpoint.filesystem", "_StorageInfo"), ++ ]: ++ modules[module] = ModuleType(module) ++ cls = type(name, (), {"__module__": module}) ++ setattr(modules[module], name, cls) ++ classes[name] = cls ++ record = classes["Metadata"]() ++ record.storage_data = {} ++ for index, (name, offset, length) in enumerate(entries): ++ item = classes["_StorageInfo"]() ++ item.relative_path, item.offset, item.length = name, offset, length ++ record.storage_data[index] = item ++ with patch.dict(sys.modules, modules): ++ return pickle.dumps(record, protocol=4) ++ ++ ++def write_checkpoint(root, entries=None): ++ root.mkdir(parents=True, exist_ok=True) ++ entries = entries if entries is not None else [("shard.distcp", 0, 32)] ++ (root / ".metadata").write_bytes(metadata_bytes(entries)) ++ (root / "common.pt").write_bytes(b"common") ++ for name, offset, length in entries: ++ path = root / name ++ path.parent.mkdir(parents=True, exist_ok=True) ++ if not path.exists() or path.stat().st_size < offset + length: ++ with path.open("wb") as stream: ++ stream.truncate(offset + length) ++ return root +diff --git a/tests/checkpoint_group_integration.py b/tests/checkpoint_group_integration.py +new file mode 100644 +index 00000000..46914918 +--- /dev/null ++++ b/tests/checkpoint_group_integration.py +@@ -0,0 +1,67 @@ ++"""CPU regression against trainer source; set TRAINING_GYM_SLIME_SOURCE.""" ++ ++import importlib.util ++import os ++import sys ++import tempfile ++from datetime import timedelta ++from pathlib import Path ++from types import ModuleType ++import torch.distributed as dist ++import torch.multiprocessing as mp ++ ++ ++def worker(rank, rendezvous): ++ from modal_training_gym.common import torch_dist_checkpoint as ckpt ++ ++ for name in ["slime", "slime.utils"]: ++ sys.modules[name] = ModuleType(name) ++ memory = ModuleType("slime.utils.memory_utils") ++ memory.available_memory = lambda: {"free_GB": 100} ++ memory.clear_memory = lambda: None ++ memory.print_memory = lambda *a: {} ++ sys.modules[memory.__name__] = memory ++ for name in ["distributed_utils", "reloadable_process_group"]: ++ full = "slime.utils." + name ++ spec = importlib.util.spec_from_file_location( ++ full, ++ str( ++ Path(os.environ["TRAINING_GYM_SLIME_SOURCE"]) / "utils" / (name + ".py") ++ ), ++ ) ++ module = importlib.util.module_from_spec(spec) ++ sys.modules[full] = module ++ spec.loader.exec_module(module) ++ reloadable = sys.modules["slime.utils.reloadable_process_group"] ++ dist.init_process_group( ++ "gloo", init_method="file://" + rendezvous, rank=rank, world_size=2 ++ ) ++ reloadable.register_default_process_group(timedelta(seconds=30)) ++ reloadable.monkey_patch_torch_dist() ++ # Reproduce the exact failure from the previous implementation. ++ proxy = dist.new_group(backend="gloo") ++ try: ++ dist.destroy_process_group(proxy) ++ except ValueError as error: ++ assert "Invalid process group" in str(error) ++ else: ++ raise AssertionError("Expected old cleanup to fail") ++ dist.destroy_process_group(proxy.group) ++ reloadable.ReloadableProcessGroup.GROUPS[os.getpid()].remove(proxy) ++ os.environ["LOCAL_RANK"] = "0" ++ os.environ["TRAINING_GYM_CHECKPOINTS_VOLUME_NAME"] = "test" ++ ckpt._commit_volume = lambda name: None ++ ckpt._validate_committed_checkpoint = lambda *args: None ++ for _ in range(3): ++ ckpt.commit_latest_checkpoint_volume("/test") ++ assert not reloadable.ReloadableProcessGroup.GROUPS[os.getpid()] ++ dist.barrier() ++ dist.destroy_process_group() ++ print( ++ f"rank {rank}: old error reproduced; three corrected saves passed", flush=True ++ ) ++ ++ ++if __name__ == "__main__": ++ with tempfile.TemporaryDirectory() as root: ++ mp.spawn(worker, args=(str(Path(root) / "rendezvous"),), nprocs=2, join=True) +diff --git a/tests/test_torch_dist_checkpoint.py b/tests/test_torch_dist_checkpoint.py +new file mode 100644 +index 00000000..25979c80 +--- /dev/null ++++ b/tests/test_torch_dist_checkpoint.py +@@ -0,0 +1,255 @@ ++import pickle ++import sys ++from pathlib import Path ++from types import SimpleNamespace ++ ++import pytest ++import yaml ++ ++from modal_training_gym.common import torch_dist_checkpoint as ckpt ++from modal_training_gym.common.launcher_utils import pin_resume_iteration ++from modal_training_gym.common.run import torch_dist_resume_checkpoint ++from tests.checkpoint_fixtures import metadata_bytes, write_checkpoint ++ ++ ++def test_all_referenced_files_and_offsets_are_required(tmp_path): ++ root = write_checkpoint( ++ tmp_path / "iter_0000449", ++ [ ++ ("__0_0.distcp", 0, 8), ++ ("__1_0.distcp", 0, 16), ++ ("__1_0.distcp", 16, 32), ++ ], ++ ) ++ assert ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ (root / "__1_0.distcp").write_bytes(b"x" * 47) ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ (root / "__1_0.distcp").unlink() ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ ++ ++def test_broad_layover_missing_shard_pattern(tmp_path): ++ root = write_checkpoint( ++ tmp_path, ++ [(f"__{rank}_{part}.distcp", 0, 8) for rank in range(64) for part in range(2)], ++ ) ++ for rank in range(40, 56): ++ (root / f"__{rank}_1.distcp").unlink() ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ assert not ckpt.is_complete_torch_dist_checkpoint([p.name for p in root.iterdir()]) ++ ++ ++@pytest.mark.parametrize( ++ "entry", ++ [ ++ ("../outside.distcp", 0, 1), ++ ("/outside.distcp", 0, 1), ++ ("shard.distcp", -1, 1), ++ ("shard.distcp", 0, 0), ++ ], ++) ++def test_invalid_storage_entries_are_rejected(entry): ++ with pytest.raises(ValueError): ++ ckpt.checkpoint_shard_sizes(metadata_bytes([entry])) ++ ++ ++def test_metadata_does_not_execute_pickle_globals(tmp_path): ++ class Payload: ++ def __reduce__(self): ++ return (eval, ("1+1",)) ++ ++ with pytest.raises(pickle.UnpicklingError, match="Unsupported"): ++ ckpt.checkpoint_shard_sizes(pickle.dumps(Payload())) ++ root = write_checkpoint(tmp_path) ++ (root / ".metadata").write_bytes(b"broken") ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ ++ ++def test_resume_falls_back_when_tracker_checkpoint_is_truncated(tmp_path): ++ write_checkpoint(tmp_path / "iter_0000399") ++ bad = write_checkpoint(tmp_path / "iter_0000449") ++ (bad / "shard.distcp").write_bytes(b"short") ++ (tmp_path / ckpt.TORCH_DIST_TRACKER_NAME).write_text("449\n") ++ result = torch_dist_resume_checkpoint( ++ str(tmp_path), is_complete=ckpt.is_complete_torch_dist_checkpoint_dir ++ ) ++ assert result["resume_from_iteration"] == 399 ++ (tmp_path / "iter_0000399" / "common.pt").unlink() ++ assert ( ++ torch_dist_resume_checkpoint( ++ str(tmp_path), is_complete=ckpt.is_complete_torch_dist_checkpoint_dir ++ ) ++ is None ++ ) ++ ++ ++@pytest.mark.parametrize("reference", [None, 17]) ++def test_resume_pins_actor_without_reusing_actor_step_for_reference( ++ tmp_path, reference ++): ++ source = tmp_path / "config.yaml" ++ data = {"ckpt_step": 449, "qkv_format": "thd"} ++ if reference is not None: ++ data["ref_ckpt_step"] = reference ++ source.write_text(yaml.safe_dump(data)) ++ cfg = SimpleNamespace(extra_config=str(source)) ++ pin_resume_iteration(cfg, 399) ++ actual = yaml.safe_load(Path(cfg.extra_config).read_text()) ++ assert actual == { ++ **data, ++ "ckpt_step": 399, ++ "ref_ckpt_step": reference or 0, ++ "exit_on_missing_checkpoint": True, ++ "finetune": False, ++ "no_load_rng": False, ++ } ++ assert yaml.safe_load(source.read_text()) == data ++ Path(cfg.extra_config).unlink() ++ ++ ++@pytest.mark.parametrize("no_save_rng", [False, True]) ++def test_retry_restores_training_state_after_fresh_parent_load(no_save_rng): ++ source = { ++ "ckpt_step": 399, ++ "ref_ckpt_step": 0, ++ "finetune": True, ++ "no_load_rng": True, ++ "no_save_rng": no_save_rng, ++ } ++ cfg = SimpleNamespace(extra_config=source) ++ pin_resume_iteration(cfg, 19) ++ actual = yaml.safe_load(Path(cfg.extra_config).read_text()) ++ assert actual["ckpt_step"] == 19 ++ assert actual["ref_ckpt_step"] == 0 ++ assert actual["finetune"] is False ++ assert actual["no_load_rng"] is no_save_rng ++ assert source["finetune"] is True ++ assert source["ckpt_step"] == 399 ++ Path(cfg.extra_config).unlink() ++ ++ ++def test_completion_marker_requires_durable_complete_view(tmp_path, monkeypatch): ++ root = write_checkpoint(tmp_path / "iter_0000449") ++ (tmp_path / ckpt.TORCH_DIST_TRACKER_NAME).write_text("449\n") ++ calls = [] ++ volume = SimpleNamespace( ++ reload=lambda: calls.append("reload"), commit=lambda: calls.append("commit") ++ ) ++ monkeypatch.setattr(ckpt.Volume, "from_name", lambda *a, **k: volume) ++ (root / "shard.distcp").unlink() ++ with pytest.raises(RuntimeError, match="incomplete"): ++ ckpt._validate_committed_checkpoint("volume", str(tmp_path)) ++ assert not (root / "training_gym_complete.json").exists() ++ assert calls == ["reload"] ++ (root / "shard.distcp").write_bytes(b"x" * 32) ++ ckpt._validate_committed_checkpoint("volume", str(tmp_path)) ++ assert (root / "training_gym_complete.json").exists() ++ assert calls == ["reload", "reload", "commit"] ++ ++ ++@pytest.mark.parametrize("coordinator", [False, True]) ++def test_each_node_commits_before_coordinator(coordinator, monkeypatch): ++ events = [] ++ monkeypatch.setenv("LOCAL_RANK", "0") ++ monkeypatch.setattr(ckpt, "_commit_volume", lambda v: events.append("commit")) ++ ++ def gather(error): ++ events.append("gather") ++ return [error] ++ ++ wrapper = SimpleNamespace(is_coordinator=coordinator, all_gather_object=gather) ++ ckpt.commit_checkpoint_volume_across_ranks( ++ "volume", wrapper, lambda: events.append("barrier") ++ ) ++ assert events == ( ++ ["gather", "barrier", "commit", "gather", "barrier"] ++ if coordinator ++ else ["commit", "gather", "barrier", "gather", "barrier"] ++ ) ++ ++ ++def test_failed_shard_commit_stops_before_coordinator_publish(monkeypatch): ++ wrapper = SimpleNamespace( ++ is_coordinator=True, all_gather_object=lambda error: [None, "disk failed"] ++ ) ++ monkeypatch.setattr( ++ ckpt, "_commit_volume", lambda v: pytest.fail("must not publish") ++ ) ++ with pytest.raises(RuntimeError, match="shard commit failed"): ++ ckpt.commit_checkpoint_volume_across_ranks("volume", wrapper, lambda: None) ++ ++ ++def test_save_coordination_uses_gloo_and_cleans_up(monkeypatch): ++ events = [] ++ group = object() ++ ++ def new_group(*, backend): ++ assert backend == "gloo" ++ events.append("gloo") ++ return group ++ ++ def gather(output, value, **kwargs): ++ assert kwargs["group"] is group ++ output[:] = [value] ++ ++ def barrier(**kwargs): ++ assert kwargs["group"] is group ++ ++ dist = SimpleNamespace( ++ is_initialized=lambda: True, ++ get_rank=lambda: 0, ++ get_world_size=lambda: 1, ++ new_group=lambda **kwargs: pytest.fail("must bypass Slime's group wrapper"), ++ all_gather_object=gather, ++ barrier=barrier, ++ destroy_process_group=lambda g: events.append("destroy"), ++ ) ++ monkeypatch.setitem(sys.modules, "torch", SimpleNamespace(distributed=dist)) ++ monkeypatch.setitem(sys.modules, "torch.distributed", dist) ++ monkeypatch.setitem( ++ sys.modules, ++ "torch.distributed.distributed_c10d", ++ SimpleNamespace(new_group=new_group), ++ ) ++ monkeypatch.setenv("LOCAL_RANK", "0") ++ monkeypatch.setenv("TRAINING_GYM_CHECKPOINTS_VOLUME_NAME", "volume") ++ monkeypatch.setattr(ckpt, "_commit_volume", lambda v: events.append("commit")) ++ monkeypatch.setattr( ++ ckpt, "_validate_committed_checkpoint", lambda *a: events.append("validate") ++ ) ++ ckpt.commit_latest_checkpoint_volume("/checkpoints/run") ++ assert events == ["gloo", "commit", "validate", "destroy"] ++ ++ ++def test_save_patch_upgrades_old_wrappers_and_is_idempotent(tmp_path, monkeypatch): ++ from modal_training_gym.common.megatron_patches import ( ++ patch_checkpoint_commit as patcher, ++ ) ++ ++ saving = tmp_path / "checkpointing.py" ++ finalize = tmp_path / "async_utils.py" ++ saving.write_text("def save_checkpoint(*args, **kwargs):\n return 7\n") ++ finalize.write_text( ++ "def maybe_finalize_async_save(*args, **kwargs):\n return 8\n" ++ ) ++ monkeypatch.setattr(patcher, "_CHECKPOINTING", saving) ++ monkeypatch.setattr(patcher, "_ASYNC_UTILS", finalize) ++ patcher.main() ++ for path in (saving, finalize): ++ path.write_text( ++ path.read_text().replace( ++ "commit_latest_checkpoint_volume(get_args().save)", ++ "commit_latest_checkpoint_volume()", ++ ) ++ ) ++ finalize.write_text( ++ finalize.read_text().replace(" from megatron.training import get_args\n", "") ++ ) ++ patcher.main() ++ first = [path.read_text() for path in (saving, finalize)] ++ patcher.main() ++ assert first == [path.read_text() for path in (saving, finalize)] ++ for source in first: ++ compile(source, "patched", "exec") ++ assert "commit_latest_checkpoint_volume(get_args().save)" in source ++ assert "from megatron.training import get_args" in source diff --git a/training/gym-main-checkpoint-recovery.patch b/training/gym-main-checkpoint-recovery.patch new file mode 100644 index 00000000..315b245f --- /dev/null +++ b/training/gym-main-checkpoint-recovery.patch @@ -0,0 +1,888 @@ +diff --git a/modal_training_gym/common/checkpoint.py b/modal_training_gym/common/checkpoint.py +index bbcd2eb5..2fc9608f 100644 +--- a/modal_training_gym/common/checkpoint.py ++++ b/modal_training_gym/common/checkpoint.py +@@ -138,18 +138,26 @@ def _list_checkpoints( + continue + child_rel = f"{rel}/{name}" if rel else name + try: +- child_names = { +- _entry_name(child) ++ child_sizes = { ++ _entry_name(child): getattr(child, "size", 0) + for child in volume.iterdir(child_rel, recursive=False) ++ if not _is_dir_entry(child) + } + except (FileNotFoundError, NotFoundError): +- child_names = set() ++ child_sizes = {} + iteration = parse_torch_dist_iteration(name) + if ( + tracker_iteration is None + or iteration is None + or iteration > tracker_iteration +- or not is_complete_torch_dist_checkpoint(child_names) ++ ): ++ continue ++ try: ++ metadata = b"".join(volume.read_file(f"{child_rel}/.metadata")) ++ except (FileNotFoundError, NotFoundError): ++ continue ++ if not is_complete_torch_dist_checkpoint( ++ child_sizes.keys(), metadata=metadata, file_sizes=child_sizes + ): + continue + checkpoints.append( +diff --git a/modal_training_gym/common/launcher_utils.py b/modal_training_gym/common/launcher_utils.py +index 79f056bf..6e19b345 100644 +--- a/modal_training_gym/common/launcher_utils.py ++++ b/modal_training_gym/common/launcher_utils.py +@@ -526,6 +526,37 @@ def drop_materialized_config_key(cfg: Any, key: str) -> None: + ) + + ++def pin_resume_iteration(cfg: Any, iteration: int | None) -> None: ++ """Load the validated iteration, even when the run tracker points elsewhere. ++ ++ Give the reference model its own default tracker selection: otherwise ++ Megatron can apply the actor's ckpt_step to the reference checkpoint too. ++ """ ++ if iteration is None: ++ return ++ import tempfile ++ import yaml ++ ++ source = getattr(cfg, "extra_config", None) ++ if isinstance(source, str): ++ with open(source) as f: ++ data = yaml.safe_load(f) or {} ++ else: ++ data = dict(source or {}) ++ data["ckpt_step"] = iteration ++ data.setdefault("ref_ckpt_step", 0) ++ data["exit_on_missing_checkpoint"] = True ++ # This is recovery from the run's own checkpoint, not a fresh fine-tune ++ # from its parent. Megatron's finetune flag otherwise suppresses optimizer ++ # and RNG restoration even after the launcher clears no_load_optim. ++ data["finetune"] = False ++ data["no_load_rng"] = bool(data.get("no_save_rng", False)) ++ with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: ++ yaml.safe_dump(data, f) ++ object.__setattr__(cfg, "extra_config", f.name) ++ object.__setattr__(cfg, "_materialized_config_keys", tuple(data)) ++ ++ + def build_train_cmd( + cfg: Any, + root: str, +diff --git a/modal_training_gym/common/megatron_patches/patch_checkpoint_commit.py b/modal_training_gym/common/megatron_patches/patch_checkpoint_commit.py +index f7f0dd74..1154a4b4 100644 +--- a/modal_training_gym/common/megatron_patches/patch_checkpoint_commit.py ++++ b/modal_training_gym/common/megatron_patches/patch_checkpoint_commit.py +@@ -15,13 +15,14 @@ def save_checkpoint(*args, **kwargs): + + result = _training_gym_save_checkpoint(*args, **kwargs) + if not getattr(get_args(), "async_save", False): +- commit_latest_checkpoint_volume() ++ commit_latest_checkpoint_volume(get_args().save) + return result + """ + _FINALIZE_WRAP = """ + + # PATCHED_TRAINING_GYM_ASYNC_FINALIZE_COMMIT + def maybe_finalize_async_save(*args, **kwargs): ++ from megatron.training import get_args + from modal_training_gym.common.torch_dist_checkpoint import ( + commit_latest_checkpoint_volume, + ) +@@ -29,7 +30,7 @@ def maybe_finalize_async_save(*args, **kwargs): + had_pending = not is_empty_async_queue() + result = _training_gym_maybe_finalize_async_save(*args, **kwargs) + if had_pending: +- commit_latest_checkpoint_volume() ++ commit_latest_checkpoint_volume(get_args().save) + return result + """ + +@@ -40,6 +41,21 @@ def _wrap(path: Path, original: str, renamed: str, marker: str, wrap: str) -> No + return + source = path.read_text() + if marker in source: ++ # Upgrade an image that already contains the old wrapper. ++ source = source.replace( ++ "commit_latest_checkpoint_volume()", ++ "commit_latest_checkpoint_volume(get_args().save)", ++ ) ++ if ( ++ original == "maybe_finalize_async_save" ++ and " from megatron.training import get_args" ++ not in source.split(marker, 1)[1] ++ ): ++ source = source.replace( ++ "def maybe_finalize_async_save(*args, **kwargs):", ++ "def maybe_finalize_async_save(*args, **kwargs):\n from megatron.training import get_args", ++ ) ++ path.write_text(source) + print(f"{path.name} already has {marker}") + return + if f"def {original}(" not in source: +diff --git a/modal_training_gym/common/run.py b/modal_training_gym/common/run.py +index 3c7d950b..876e8664 100644 +--- a/modal_training_gym/common/run.py ++++ b/modal_training_gym/common/run.py +@@ -29,6 +29,7 @@ from modal_training_gym.common.models import ModelConfig + from modal_training_gym.common.status import FrameworkStatus, resolve_framework_status + from modal_training_gym.common.torch_dist_checkpoint import ( + TORCH_DIST_TRACKER_NAME, ++ is_complete_torch_dist_checkpoint_dir, + parse_torch_dist_iteration, + parse_torch_dist_tracker, + ) +@@ -761,7 +762,7 @@ def torch_dist_resume_checkpoint( + if not os.path.isdir(save_path): + return None + +- is_complete = is_complete or os.path.isdir ++ is_complete = is_complete or is_complete_torch_dist_checkpoint_dir + tracker_path = os.path.join(save_path, TORCH_DIST_TRACKER_NAME) + if os.path.isfile(tracker_path): + try: +@@ -771,16 +772,14 @@ def torch_dist_resume_checkpoint( + tracker = "" + if tracker.strip() == "release": + path = os.path.join(save_path, "release") +- return ( +- _resume_checkpoint(path, "release", None) if is_complete(path) else None +- ) ++ if is_complete(path): ++ return _resume_checkpoint(path, "release", None) + iteration = parse_torch_dist_tracker(tracker) + if iteration is not None: + name = f"iter_{iteration:07d}" + path = os.path.join(save_path, name) +- return ( +- _resume_checkpoint(path, name, iteration) if is_complete(path) else None +- ) ++ if is_complete(path): ++ return _resume_checkpoint(path, name, iteration) + + try: + candidates: list[tuple[int, str, str]] = [] +diff --git a/modal_training_gym/common/torch_dist_checkpoint.py b/modal_training_gym/common/torch_dist_checkpoint.py +index 6fff2726..f12c8f6d 100644 +--- a/modal_training_gym/common/torch_dist_checkpoint.py ++++ b/modal_training_gym/common/torch_dist_checkpoint.py +@@ -1,28 +1,166 @@ + from __future__ import annotations + + import os +-from collections.abc import Collection +-from pathlib import Path ++import io ++import pickle ++import json ++from collections.abc import Collection, Mapping ++from pathlib import Path, PurePosixPath + from typing import Any, Callable + + TORCH_DIST_TRACKER_NAME = "latest_checkpointed_iteration.txt" + + +-def is_complete_torch_dist_checkpoint(names: Collection[str]) -> bool: +- """``.metadata`` is written last, so it separates a finished save from a crashed +- one. ``common.pt`` is not required: newer megatron-core folds the common state +- into the torch_dist metadata and writes no such file.""" +- return ".metadata" in names and any(name.endswith(".distcp") for name in names) ++class _MetadataRecord: ++ """Inert stand-in for checkpoint metadata; never imports or executes torch.""" ++ ++ def __new__(cls, *args, **kwargs): ++ return object.__new__(cls) ++ ++ def __init__(self, *args, **kwargs): ++ pass ++ ++ def __setstate__(self, state): ++ if isinstance(state, dict): ++ self.__dict__.update(state) ++ ++ ++class _MetadataReader(pickle.Unpickler): ++ def find_class(self, module, name): ++ allowed = { ++ "torch.distributed.checkpoint.metadata": { ++ "Metadata", ++ "MetadataIndex", ++ "StorageMeta", ++ "TensorStorageMetadata", ++ "BytesStorageMetadata", ++ "TensorProperties", ++ "ChunkStorageMetadata", ++ "_MEM_FORMAT_ENCODING", ++ }, ++ "torch.distributed.checkpoint.filesystem": {"_StorageInfo"}, ++ "torch.serialization": {"_get_layout"}, ++ "torch": { ++ "Size", ++ "bfloat16", ++ "float16", ++ "float32", ++ "float64", ++ "bool", ++ "uint8", ++ "int8", ++ "int16", ++ "int32", ++ "int64", ++ "complex64", ++ "complex128", ++ "float8_e4m3fn", ++ "float8_e5m2", ++ "float8_e4m3fnuz", ++ "float8_e5m2fnuz", ++ }, ++ } ++ if name not in allowed.get(module, set()): ++ raise pickle.UnpicklingError( ++ f"Unsupported checkpoint metadata type: {module}.{name}" ++ ) ++ return _MetadataRecord ++ ++ ++def checkpoint_shard_sizes(metadata: bytes) -> dict[str, int]: ++ """Minimum file sizes required by the DCP storage offsets, without torch.""" ++ record = _MetadataReader(io.BytesIO(metadata)).load() ++ storage = getattr(record, "storage_data", None) ++ if not isinstance(storage, dict) or not storage: ++ raise ValueError("checkpoint metadata has no storage entries") ++ required: dict[str, int] = {} ++ for entry in storage.values(): ++ name = getattr(entry, "relative_path", None) ++ offset = getattr(entry, "offset", None) ++ length = getattr(entry, "length", None) ++ if ( ++ not isinstance(name, str) ++ or not name.endswith(".distcp") ++ or PurePosixPath(name).is_absolute() ++ or ".." in PurePosixPath(name).parts ++ or "\\" in name ++ or type(offset) is not int ++ or offset < 0 ++ or type(length) is not int ++ or length <= 0 ++ ): ++ raise ValueError("invalid checkpoint storage entry") ++ required[name] = max(required.get(name, 0), offset + length) ++ return required ++ ++ ++def torch_dist_checkpoint_health( ++ metadata: bytes, ++ file_sizes: Mapping[str, int], ++) -> tuple[bool, str]: ++ """Validate every referenced shard and byte range, including legacy saves.""" ++ if file_sizes.get(".metadata", 0) <= 0: ++ return False, "missing or empty checkpoint metadata" ++ try: ++ required = checkpoint_shard_sizes(metadata) ++ except ( ++ ValueError, ++ TypeError, ++ AttributeError, ++ EOFError, ++ pickle.UnpicklingError, ++ ) as exc: ++ return False, f"unreadable checkpoint metadata: {exc}" ++ missing = sorted(required.keys() - file_sizes.keys()) ++ if missing: ++ return False, f"missing referenced shards: {', '.join(missing)}" ++ truncated = sorted( ++ name for name, size in required.items() if file_sizes[name] < size ++ ) ++ if truncated: ++ return False, f"truncated referenced shards: {', '.join(truncated)}" ++ return True, f"validated {len(required)} referenced shards and byte ranges" ++ ++ ++def is_complete_torch_dist_checkpoint( ++ names: Collection[str], ++ *, ++ metadata: bytes | None = None, ++ file_sizes: Mapping[str, int] | None = None, ++) -> bool: ++ # A directory listing alone cannot establish completeness. ++ # Preserve main's support for newer Megatron saves with embedded common ++ # state: common.pt is optional, but every referenced shard must exist. ++ return bool( ++ ".metadata" in names ++ and metadata is not None ++ and file_sizes is not None ++ and torch_dist_checkpoint_health(metadata, file_sizes)[0] ++ ) + + + def is_complete_torch_dist_checkpoint_dir( + checkpoint_dir: str | os.PathLike[str], + ) -> bool: + try: +- names = {entry.name for entry in Path(checkpoint_dir).iterdir()} +- except OSError: ++ root = Path(checkpoint_dir) ++ metadata = (root / ".metadata").read_bytes() ++ required = checkpoint_shard_sizes(metadata) ++ sizes = {} ++ for name in [".metadata", "common.pt", *required]: ++ path = root / name ++ if path.resolve().is_relative_to(root.resolve()) and path.is_file(): ++ sizes[name] = path.stat().st_size ++ return torch_dist_checkpoint_health(metadata, sizes)[0] ++ except ( ++ OSError, ++ ValueError, ++ TypeError, ++ AttributeError, ++ EOFError, ++ pickle.UnpicklingError, ++ ): + return False +- return is_complete_torch_dist_checkpoint(names) + + + def parse_torch_dist_iteration(name: str) -> int | None: +@@ -102,6 +240,9 @@ def commit_checkpoint_volume_across_ranks( + + + class _TorchDistributed: ++ def __init__(self, group=None): ++ self.group = group ++ + @property + def is_coordinator(self) -> bool: + import torch.distributed as dist +@@ -114,17 +255,71 @@ class _TorchDistributed: + if not dist.is_initialized(): + return [error] + gathered: list[str | None] = [None] * dist.get_world_size() +- dist.all_gather_object(gathered, error) ++ dist.all_gather_object(gathered, error, group=self.group) + return gathered + + +-def commit_latest_checkpoint_volume() -> None: ++def _validate_committed_checkpoint(volume_name: str, save_path: str) -> None: ++ from modal import Volume ++ ++ volume = Volume.from_name(volume_name, create_if_missing=False) ++ volume.reload() ++ root = Path(save_path) ++ tracker = (root / TORCH_DIST_TRACKER_NAME).read_text().strip() ++ iteration = parse_torch_dist_tracker(tracker) ++ if tracker == "release": ++ checkpoint = root / "release" ++ elif iteration is not None: ++ checkpoint = root / f"iter_{iteration:07d}" ++ else: ++ raise RuntimeError("Checkpoint tracker is missing a valid saved iteration") ++ if not is_complete_torch_dist_checkpoint_dir(checkpoint): ++ raise RuntimeError(f"Committed checkpoint is incomplete: {checkpoint}") ++ # Publish only after all nodes committed and the committed view was checked. ++ marker = checkpoint / "training_gym_complete.json" ++ temporary = marker.with_suffix(".tmp") ++ temporary.write_text( ++ json.dumps( ++ { ++ "version": 1, ++ "required_shard_sizes": checkpoint_shard_sizes( ++ (checkpoint / ".metadata").read_bytes() ++ ), ++ } ++ ) ++ ) ++ temporary.replace(marker) ++ volume.commit() ++ ++ ++def commit_latest_checkpoint_volume(save_path: str | None = None) -> None: + import torch.distributed as dist ++ from torch.distributed.distributed_c10d import new_group + + volume_name = os.environ.get("TRAINING_GYM_CHECKPOINTS_VOLUME_NAME") +- barrier = dist.barrier if dist.is_initialized() else lambda: None +- commit_checkpoint_volume_across_ranks( +- volume_name, +- _TorchDistributed(), +- barrier, +- ) ++ if not volume_name: ++ return ++ # Filesystem coordination is CPU work. Avoid NCCL object collectives and ++ # their device selection/communicator churn around save and offload. ++ # Slime wraps dist.new_group in ReloadableProcessGroup after registering ++ # WORLD. Its collective wrappers unwrap that proxy, but PyTorch's destroy ++ # does not. This short-lived group must also stay out of Slime's registry ++ # so offload cannot recreate it after we have destroyed it. ++ group = new_group(backend="gloo") if dist.is_initialized() else None ++ wrapper = _TorchDistributed(group) ++ barrier = (lambda: dist.barrier(group=group)) if group is not None else lambda: None ++ try: ++ commit_checkpoint_volume_across_ranks(volume_name, wrapper, barrier) ++ error = None ++ if save_path and wrapper.is_coordinator: ++ try: ++ _validate_committed_checkpoint(volume_name, save_path) ++ except Exception as exc: ++ error = _error_text(exc) ++ _raise_gathered_errors( ++ wrapper, error, "Checkpoint committed-view validation failed" ++ ) ++ barrier() ++ finally: ++ if group is not None: ++ dist.destroy_process_group(group) +diff --git a/modal_training_gym/frameworks/slime/launcher.py b/modal_training_gym/frameworks/slime/launcher.py +index 55c385d2..c93dad46 100644 +--- a/modal_training_gym/frameworks/slime/launcher.py ++++ b/modal_training_gym/frameworks/slime/launcher.py +@@ -67,6 +67,7 @@ from modal_training_gym.common.launcher_helpers import ( + from modal_training_gym.common.train_result import save_train_result_blob + from modal_training_gym.common.launcher_utils import ( + drop_materialized_config_key, ++ pin_resume_iteration, + serialize_recipe_params, + timing_debug_env, + ) +@@ -1159,6 +1160,7 @@ def build_slime_app( + # even for runs launched with an explicit start_rollout_id. + object.__setattr__(slime, "start_rollout_id", None) + drop_materialized_config_key(slime, "start_rollout_id") ++ pin_resume_iteration(slime, resume_from_iteration) + # This run's saves include Adam only when no_save_optim is false. + # TrainConfig.resume_from_checkpoint forces no_load_optim for the source seed; + # that flag is not a property of later saves in this directory. +diff --git a/tests/test_checkpoint.py b/tests/test_checkpoint.py +index 16cb766a..cfdd2782 100644 +--- a/tests/test_checkpoint.py ++++ b/tests/test_checkpoint.py +@@ -3,6 +3,7 @@ from dataclasses import dataclass + + import modal + import pytest ++from tests.checkpoint_fixtures import metadata_bytes + + from modal_training_gym.common import checkpoint as checkpoint_mod + from modal_training_gym.common.checkpoint import ( +@@ -204,6 +205,7 @@ class _DirEntry: + path: str + mtime: float = 1.0 + is_directory: bool = False ++ size: int = 4096 + + def is_dir(self) -> bool: + return self.is_directory +@@ -224,6 +226,10 @@ class _ListingVolume: + return list(self.tree.get(key, [])) + + def read_file(self, path: str): ++ if path.endswith("/.metadata") and any( ++ entry.path == path for entry in self.tree.get(path.rsplit("/", 1)[0], []) ++ ): ++ return [metadata_bytes()] + if path not in self.files: + raise FileNotFoundError(path) + return [self.files[path]] +diff --git a/tests/checkpoint_fixtures.py b/tests/checkpoint_fixtures.py +new file mode 100644 +index 00000000..1954cfc3 +--- /dev/null ++++ b/tests/checkpoint_fixtures.py +@@ -0,0 +1,46 @@ ++"""Small DCP metadata fixtures, without installing torch or loading tensors.""" ++ ++import pickle ++import sys ++from types import ModuleType ++from unittest.mock import patch ++ ++ ++def metadata_bytes(entries=None): ++ if entries is None: ++ entries = [("shard.distcp", 0, 32)] ++ modules = { ++ name: ModuleType(name) ++ for name in ("torch", "torch.distributed", "torch.distributed.checkpoint") ++ } ++ classes = {} ++ for module, name in [ ++ ("torch.distributed.checkpoint.metadata", "Metadata"), ++ ("torch.distributed.checkpoint.filesystem", "_StorageInfo"), ++ ]: ++ modules[module] = ModuleType(module) ++ cls = type(name, (), {"__module__": module}) ++ setattr(modules[module], name, cls) ++ classes[name] = cls ++ record = classes["Metadata"]() ++ record.storage_data = {} ++ for index, (name, offset, length) in enumerate(entries): ++ item = classes["_StorageInfo"]() ++ item.relative_path, item.offset, item.length = name, offset, length ++ record.storage_data[index] = item ++ with patch.dict(sys.modules, modules): ++ return pickle.dumps(record, protocol=4) ++ ++ ++def write_checkpoint(root, entries=None): ++ root.mkdir(parents=True, exist_ok=True) ++ entries = entries if entries is not None else [("shard.distcp", 0, 32)] ++ (root / ".metadata").write_bytes(metadata_bytes(entries)) ++ (root / "common.pt").write_bytes(b"common") ++ for name, offset, length in entries: ++ path = root / name ++ path.parent.mkdir(parents=True, exist_ok=True) ++ if not path.exists() or path.stat().st_size < offset + length: ++ with path.open("wb") as stream: ++ stream.truncate(offset + length) ++ return root +diff --git a/tests/checkpoint_group_integration.py b/tests/checkpoint_group_integration.py +new file mode 100644 +index 00000000..46914918 +--- /dev/null ++++ b/tests/checkpoint_group_integration.py +@@ -0,0 +1,67 @@ ++"""CPU regression against trainer source; set TRAINING_GYM_SLIME_SOURCE.""" ++ ++import importlib.util ++import os ++import sys ++import tempfile ++from datetime import timedelta ++from pathlib import Path ++from types import ModuleType ++import torch.distributed as dist ++import torch.multiprocessing as mp ++ ++ ++def worker(rank, rendezvous): ++ from modal_training_gym.common import torch_dist_checkpoint as ckpt ++ ++ for name in ["slime", "slime.utils"]: ++ sys.modules[name] = ModuleType(name) ++ memory = ModuleType("slime.utils.memory_utils") ++ memory.available_memory = lambda: {"free_GB": 100} ++ memory.clear_memory = lambda: None ++ memory.print_memory = lambda *a: {} ++ sys.modules[memory.__name__] = memory ++ for name in ["distributed_utils", "reloadable_process_group"]: ++ full = "slime.utils." + name ++ spec = importlib.util.spec_from_file_location( ++ full, ++ str( ++ Path(os.environ["TRAINING_GYM_SLIME_SOURCE"]) / "utils" / (name + ".py") ++ ), ++ ) ++ module = importlib.util.module_from_spec(spec) ++ sys.modules[full] = module ++ spec.loader.exec_module(module) ++ reloadable = sys.modules["slime.utils.reloadable_process_group"] ++ dist.init_process_group( ++ "gloo", init_method="file://" + rendezvous, rank=rank, world_size=2 ++ ) ++ reloadable.register_default_process_group(timedelta(seconds=30)) ++ reloadable.monkey_patch_torch_dist() ++ # Reproduce the exact failure from the previous implementation. ++ proxy = dist.new_group(backend="gloo") ++ try: ++ dist.destroy_process_group(proxy) ++ except ValueError as error: ++ assert "Invalid process group" in str(error) ++ else: ++ raise AssertionError("Expected old cleanup to fail") ++ dist.destroy_process_group(proxy.group) ++ reloadable.ReloadableProcessGroup.GROUPS[os.getpid()].remove(proxy) ++ os.environ["LOCAL_RANK"] = "0" ++ os.environ["TRAINING_GYM_CHECKPOINTS_VOLUME_NAME"] = "test" ++ ckpt._commit_volume = lambda name: None ++ ckpt._validate_committed_checkpoint = lambda *args: None ++ for _ in range(3): ++ ckpt.commit_latest_checkpoint_volume("/test") ++ assert not reloadable.ReloadableProcessGroup.GROUPS[os.getpid()] ++ dist.barrier() ++ dist.destroy_process_group() ++ print( ++ f"rank {rank}: old error reproduced; three corrected saves passed", flush=True ++ ) ++ ++ ++if __name__ == "__main__": ++ with tempfile.TemporaryDirectory() as root: ++ mp.spawn(worker, args=(str(Path(root) / "rendezvous"),), nprocs=2, join=True) +diff --git a/tests/test_torch_dist_checkpoint.py b/tests/test_torch_dist_checkpoint.py +new file mode 100644 +index 00000000..7101116c +--- /dev/null ++++ b/tests/test_torch_dist_checkpoint.py +@@ -0,0 +1,265 @@ ++import pickle ++import sys ++from pathlib import Path ++from types import SimpleNamespace ++ ++import pytest ++import yaml ++ ++from modal_training_gym.common import torch_dist_checkpoint as ckpt ++from modal_training_gym.common.launcher_utils import pin_resume_iteration ++from modal_training_gym.common.run import torch_dist_resume_checkpoint ++from tests.checkpoint_fixtures import metadata_bytes, write_checkpoint ++ ++ ++def test_all_referenced_files_and_offsets_are_required(tmp_path): ++ root = write_checkpoint( ++ tmp_path / "iter_0000449", ++ [ ++ ("__0_0.distcp", 0, 8), ++ ("__1_0.distcp", 0, 16), ++ ("__1_0.distcp", 16, 32), ++ ], ++ ) ++ assert ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ (root / "__1_0.distcp").write_bytes(b"x" * 47) ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ (root / "__1_0.distcp").unlink() ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ ++ ++def test_broad_layover_missing_shard_pattern(tmp_path): ++ root = write_checkpoint( ++ tmp_path, ++ [(f"__{rank}_{part}.distcp", 0, 8) for rank in range(64) for part in range(2)], ++ ) ++ for rank in range(40, 56): ++ (root / f"__{rank}_1.distcp").unlink() ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ assert not ckpt.is_complete_torch_dist_checkpoint([p.name for p in root.iterdir()]) ++ ++ ++@pytest.mark.parametrize( ++ "entry", ++ [ ++ ("../outside.distcp", 0, 1), ++ ("/outside.distcp", 0, 1), ++ ("shard.distcp", -1, 1), ++ ("shard.distcp", 0, 0), ++ ], ++) ++def test_invalid_storage_entries_are_rejected(entry): ++ with pytest.raises(ValueError): ++ ckpt.checkpoint_shard_sizes(metadata_bytes([entry])) ++ ++ ++def test_metadata_does_not_execute_pickle_globals(tmp_path): ++ class Payload: ++ def __reduce__(self): ++ return (eval, ("1+1",)) ++ ++ with pytest.raises(pickle.UnpicklingError, match="Unsupported"): ++ ckpt.checkpoint_shard_sizes(pickle.dumps(Payload())) ++ root = write_checkpoint(tmp_path) ++ (root / ".metadata").write_bytes(b"broken") ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ ++ ++def test_resume_falls_back_when_tracker_checkpoint_is_truncated(tmp_path): ++ write_checkpoint(tmp_path / "iter_0000399") ++ bad = write_checkpoint(tmp_path / "iter_0000449") ++ (bad / "shard.distcp").write_bytes(b"short") ++ (tmp_path / ckpt.TORCH_DIST_TRACKER_NAME).write_text("449\n") ++ result = torch_dist_resume_checkpoint( ++ str(tmp_path), is_complete=ckpt.is_complete_torch_dist_checkpoint_dir ++ ) ++ assert result["resume_from_iteration"] == 399 ++ (tmp_path / "iter_0000399" / ".metadata").unlink() ++ assert ( ++ torch_dist_resume_checkpoint( ++ str(tmp_path), is_complete=ckpt.is_complete_torch_dist_checkpoint_dir ++ ) ++ is None ++ ) ++ ++ ++@pytest.mark.parametrize("reference", [None, 17]) ++def test_resume_pins_actor_without_reusing_actor_step_for_reference( ++ tmp_path, reference ++): ++ source = tmp_path / "config.yaml" ++ data = {"ckpt_step": 449, "qkv_format": "thd"} ++ if reference is not None: ++ data["ref_ckpt_step"] = reference ++ source.write_text(yaml.safe_dump(data)) ++ cfg = SimpleNamespace(extra_config=str(source)) ++ pin_resume_iteration(cfg, 399) ++ actual = yaml.safe_load(Path(cfg.extra_config).read_text()) ++ assert actual == { ++ **data, ++ "ckpt_step": 399, ++ "ref_ckpt_step": reference or 0, ++ "exit_on_missing_checkpoint": True, ++ "finetune": False, ++ "no_load_rng": False, ++ } ++ assert yaml.safe_load(source.read_text()) == data ++ Path(cfg.extra_config).unlink() ++ ++ ++@pytest.mark.parametrize("no_save_rng", [False, True]) ++def test_retry_restores_training_state_after_fresh_parent_load(no_save_rng): ++ source = { ++ "ckpt_step": 399, ++ "ref_ckpt_step": 0, ++ "finetune": True, ++ "no_load_rng": True, ++ "no_save_rng": no_save_rng, ++ } ++ cfg = SimpleNamespace(extra_config=source) ++ pin_resume_iteration(cfg, 19) ++ actual = yaml.safe_load(Path(cfg.extra_config).read_text()) ++ assert actual["ckpt_step"] == 19 ++ assert actual["ref_ckpt_step"] == 0 ++ assert actual["finetune"] is False ++ assert actual["no_load_rng"] is no_save_rng ++ assert source["finetune"] is True ++ assert source["ckpt_step"] == 399 ++ Path(cfg.extra_config).unlink() ++ ++ ++def test_completion_marker_requires_durable_complete_view(tmp_path, monkeypatch): ++ root = write_checkpoint(tmp_path / "iter_0000449") ++ (tmp_path / ckpt.TORCH_DIST_TRACKER_NAME).write_text("449\n") ++ calls = [] ++ volume = SimpleNamespace( ++ reload=lambda: calls.append("reload"), commit=lambda: calls.append("commit") ++ ) ++ from modal import Volume ++ ++ monkeypatch.setattr(Volume, "from_name", lambda *a, **k: volume) ++ (root / "shard.distcp").unlink() ++ with pytest.raises(RuntimeError, match="incomplete"): ++ ckpt._validate_committed_checkpoint("volume", str(tmp_path)) ++ assert not (root / "training_gym_complete.json").exists() ++ assert calls == ["reload"] ++ (root / "shard.distcp").write_bytes(b"x" * 32) ++ ckpt._validate_committed_checkpoint("volume", str(tmp_path)) ++ assert (root / "training_gym_complete.json").exists() ++ assert calls == ["reload", "reload", "commit"] ++ ++ ++@pytest.mark.parametrize("coordinator", [False, True]) ++def test_each_node_commits_before_coordinator(coordinator, monkeypatch): ++ events = [] ++ monkeypatch.setenv("LOCAL_RANK", "0") ++ monkeypatch.setattr(ckpt, "_commit_volume", lambda v: events.append("commit")) ++ ++ def gather(error): ++ events.append("gather") ++ return [error] ++ ++ wrapper = SimpleNamespace(is_coordinator=coordinator, all_gather_object=gather) ++ ckpt.commit_checkpoint_volume_across_ranks( ++ "volume", wrapper, lambda: events.append("barrier") ++ ) ++ assert events == ( ++ ["gather", "barrier", "commit", "gather", "barrier"] ++ if coordinator ++ else ["commit", "gather", "barrier", "gather", "barrier"] ++ ) ++ ++ ++def test_failed_shard_commit_stops_before_coordinator_publish(monkeypatch): ++ wrapper = SimpleNamespace( ++ is_coordinator=True, all_gather_object=lambda error: [None, "disk failed"] ++ ) ++ monkeypatch.setattr( ++ ckpt, "_commit_volume", lambda v: pytest.fail("must not publish") ++ ) ++ with pytest.raises(RuntimeError, match="shard commit failed"): ++ ckpt.commit_checkpoint_volume_across_ranks("volume", wrapper, lambda: None) ++ ++ ++def test_save_coordination_uses_gloo_and_cleans_up(monkeypatch): ++ events = [] ++ group = object() ++ ++ def new_group(*, backend): ++ assert backend == "gloo" ++ events.append("gloo") ++ return group ++ ++ def gather(output, value, **kwargs): ++ assert kwargs["group"] is group ++ output[:] = [value] ++ ++ def barrier(**kwargs): ++ assert kwargs["group"] is group ++ ++ dist = SimpleNamespace( ++ is_initialized=lambda: True, ++ get_rank=lambda: 0, ++ get_world_size=lambda: 1, ++ new_group=lambda **kwargs: pytest.fail("must bypass Slime's group wrapper"), ++ all_gather_object=gather, ++ barrier=barrier, ++ destroy_process_group=lambda g: events.append("destroy"), ++ ) ++ monkeypatch.setitem(sys.modules, "torch", SimpleNamespace(distributed=dist)) ++ monkeypatch.setitem(sys.modules, "torch.distributed", dist) ++ monkeypatch.setitem( ++ sys.modules, ++ "torch.distributed.distributed_c10d", ++ SimpleNamespace(new_group=new_group), ++ ) ++ monkeypatch.setenv("LOCAL_RANK", "0") ++ monkeypatch.setenv("TRAINING_GYM_CHECKPOINTS_VOLUME_NAME", "volume") ++ monkeypatch.setattr(ckpt, "_commit_volume", lambda v: events.append("commit")) ++ monkeypatch.setattr( ++ ckpt, "_validate_committed_checkpoint", lambda *a: events.append("validate") ++ ) ++ ckpt.commit_latest_checkpoint_volume("/checkpoints/run") ++ assert events == ["gloo", "commit", "validate", "destroy"] ++ ++ ++def test_save_patch_upgrades_old_wrappers_and_is_idempotent(tmp_path, monkeypatch): ++ from modal_training_gym.common.megatron_patches import ( ++ patch_checkpoint_commit as patcher, ++ ) ++ ++ saving = tmp_path / "checkpointing.py" ++ finalize = tmp_path / "async_utils.py" ++ saving.write_text("def save_checkpoint(*args, **kwargs):\n return 7\n") ++ finalize.write_text( ++ "def maybe_finalize_async_save(*args, **kwargs):\n return 8\n" ++ ) ++ monkeypatch.setattr(patcher, "_CHECKPOINTING", saving) ++ monkeypatch.setattr(patcher, "_ASYNC_UTILS", finalize) ++ patcher.main() ++ for path in (saving, finalize): ++ path.write_text( ++ path.read_text().replace( ++ "commit_latest_checkpoint_volume(get_args().save)", ++ "commit_latest_checkpoint_volume()", ++ ) ++ ) ++ finalize.write_text( ++ finalize.read_text().replace(" from megatron.training import get_args\n", "") ++ ) ++ patcher.main() ++ first = [path.read_text() for path in (saving, finalize)] ++ patcher.main() ++ assert first == [path.read_text() for path in (saving, finalize)] ++ for source in first: ++ compile(source, "patched", "exec") ++ assert "commit_latest_checkpoint_volume(get_args().save)" in source ++ assert "from megatron.training import get_args" in source ++ ++ ++def test_checkpoint_without_separate_common_state_remains_supported(tmp_path): ++ root = write_checkpoint(tmp_path) ++ (root / "common.pt").unlink() ++ assert ckpt.is_complete_torch_dist_checkpoint_dir(root) ++ (root / "shard.distcp").write_bytes(b"short") ++ assert not ckpt.is_complete_torch_dist_checkpoint_dir(root) diff --git a/training/learning_snapshot.py b/training/learning_snapshot.py new file mode 100644 index 00000000..6fe27898 --- /dev/null +++ b/training/learning_snapshot.py @@ -0,0 +1,85 @@ +"""Read-only learning evidence for the supervising agent, not an efficacy verdict.""" + +import json +import math +import statistics +import time + +from modal_dojo.common.metric_series import RunMetrics, metric_series_store +from modal_dojo.utils.metadata import MetadataStore, vol_get, vol_list + + +def capture(run, root): + metrics = RunMetrics() + for chunk in vol_list(metric_series_store(run.training_run_id)): + metrics.load_chunk(chunk) + rows = [metrics.table[key] for key in sorted(metrics.table)] + train = {int(row["train/step"]): row for row in rows if "train/step" in row} + evaluations = {int(row["eval/step"]): row["eval/eval"] + for row in rows if "eval/step" in row and "eval/eval" in row} + selected = sorted(train) + alerts = [] + for step, row in train.items(): + for key in ("train/loss", "train/grad_norm", "train/train_rollout_logprob_abs_diff"): + if key in row and not math.isfinite(row[key]): + alerts.append(f"Nonfinite {key} at update {step + 1}") + latest_train = train[selected[-1]] if selected else {} + if latest_train.get("train/train_rollout_logprob_abs_diff", 0) > .05: + alerts.append("Sampling/training log-probability gap exceeds 0.05; inspect") + if selected: + initial = train[selected[0]].get("train/entropy_loss", 0) + current = latest_train.get("train/entropy_loss", 0) + if initial > 0 and current < .3 * initial: + alerts.append("Token entropy fell over 70%; inspect action diversity and evaluation") + eval_history = [{"eval_step": step, "reward": evaluations[step]} for step in sorted(evaluations)] + if len(eval_history) >= 3: + baseline = eval_history[0]["reward"] + if baseline > 0 and all(item["reward"] < .8 * baseline for item in eval_history[-2:]): + alerts.append("Two held-out evaluations are over 20% below baseline; inspect paired games") + latest = (run.metadata or {}).get("latest_rollout") or {} + rollout_id = latest.get("rollout_id") + summary = None + if rollout_id is not None: + path = root / f"{run.training_run_id}-rollout-{rollout_id}.json" + if path.exists(): + rollout = json.loads(path.read_text()) + else: + rollout = vol_get(MetadataStore.TRAINING_ROLLOUTS, + f"{run.training_run_id}__{int(rollout_id):08d}") + if rollout: + path.write_text(json.dumps(rollout)) + games = {} + if rollout: + for sample in rollout.get("samples", []): + metadata = sample.get("metadata") or {} + game = metadata.get("balatro") or {} + if "episode_id" in game: + games[game["episode_id"]] = (game, metadata) + if games: + values = list(games.values()) + turns = sum(game["turns"] for game, _ in values) + invalid = sum(game["invalid_actions"] for game, _ in values) + summary = { + "rollout_id": rollout_id, "observed_training_games": len(values), + "mean_reward": statistics.mean(game["episode_reward"] for game, _ in values), + "mean_ante": statistics.mean(game["final_ante"] for game, _ in values), + "wins": sum(bool(game["won"]) for game, _ in values), + "invalid_actions": invalid, "turns": turns, + "invalid_fraction": invalid / max(1, turns), + "mean_policy_reward": statistics.mean(m["policy_episode_reward"] for _, m in values), + "mean_intervention_reward_excluded": statistics.mean( + game["episode_reward"] - m["policy_episode_reward"] for game, m in values), + } + result = { + "checked_at": time.time(), "run": run.training_run_id, + "completed_updates": max(selected) + 1 if selected else 0, + "eval_history": eval_history, + "latest_train": {key: value for key, value in latest_train.items() if key.startswith("train/")}, + "latest_training_games": summary, "alerts": alerts, + "interpretation": "Held-out means require paired-game and trajectory review; training reward alone does not establish learning.", + } + (root / f"{run.training_run_id}-metrics.json").write_text(json.dumps(metrics.table, indent=2)) + (root / f"{run.training_run_id}-learning-latest.json").write_text(json.dumps(result, indent=2)) + with (root / f"{run.training_run_id}-learning-monitor.jsonl").open("a") as file: + file.write(json.dumps(result) + "\n") + return result diff --git a/training/manual_harness_player.py b/training/manual_harness_player.py new file mode 100644 index 00000000..93808f6d --- /dev/null +++ b/training/manual_harness_player.py @@ -0,0 +1,70 @@ +"""Manual inference bridge for exact Balatro rollout loop, CPU game only.""" +import asyncio, json, sys, hashlib, time +from dataclasses import dataclass, field +from enum import Enum +from pathlib import Path +from types import ModuleType, SimpleNamespace +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) +import batro_modal_rl as h +@dataclass +class Sample: + class Status(Enum): + COMPLETED = 'completed' + ABORTED = 'aborted' + prompt: str = '' + metadata: dict = field(default_factory=dict) + trace: list = field(default_factory=list) + index: int = 0 + group_index: int = 0 + rollout_id: int | None = None +for name in ('slime', 'slime.utils', 'slime.utils.types'): + sys.modules[name] = ModuleType(name) +sys.modules['slime.utils.types'].Sample = Sample +seed = sys.argv[1] +out = ROOT / 'reports/manual-harness-20260916' / seed +out.mkdir(parents=True, exist_ok=False) +args = SimpleNamespace(hf_checkpoint='Qwen/Qwen3.8-27B', balatro_max_decisions=250, balatro_context_len=49152, max_position_embeddings=49152, sglang_context_length=49152, balatro_segment_tokens=8192, balatro_capture_screenshot=True) +pool = h._get_env_pool() +original_acquire = pool.acquire +env_holder = [] +def acquire(s): + env = original_acquire(s) + env_holder.append(env) + proc = env.sandbox.exec('cat', '/opt/balatro-assets/CURRENT'); proc.wait() + release = proc.stdout.read().strip() + manifest = {'seed': seed, 'release': release, 'sandbox_id': env.sandbox.object_id, 'started_at': time.time(), 'tokenizer': args.hf_checkpoint, 'source_sha256': {p: hashlib.sha256((ROOT/p).read_bytes()).hexdigest() for p in ('batro_modal_rl.py','balatrobot.lua','training/manual_harness_player.py')}, 'replacement': 'manual inference and Sample result carrier; no model endpoint or GPU'} + (out/'manifest.json').write_text(json.dumps(manifest, indent=2)) + print('SANDBOX '+json.dumps(manifest), flush=True) + return env +pool.acquire = acquire +turn = 0 +async def manual_action(args, sampling_params, input_ids, state_name): + global turn + turn += 1 + tokenizer = h._tokenizer(args.hf_checkpoint) + full_context = tokenizer.decode(input_ids) + visible = h._turn_prompt(env_holder[0].state) + request = {'turn': turn, 'state_name': state_name, 'visible': visible, 'input_tokens':len(input_ids)} + (out/f'{turn:03}-prompt.txt').write_text(full_context) + (out/f'{turn:03}-request.json').write_text(json.dumps(request, indent=2)) + (out/'pending.json').write_text(json.dumps(request, indent=2)) + print(f'WAITING turn={turn} state={state_name} path={out}/pending.json', flush=True) + action_path = out/f'{turn:03}-action.json' + while not action_path.exists(): + await asyncio.sleep(.2) + payload = json.loads(action_path.read_text()) + action = payload['action'] + response = json.dumps(action, separators=(',',':')) + ids = tokenizer(response, add_special_tokens=False)['input_ids'] + if len(ids)>96: + raise ValueError('Manual action exceeds training response token budget') + print('ACTION '+response, flush=True) + return ids, response, 'stop', [0.0]*len(ids) +h._generate_action = manual_action +async def main(): + samples = await h.generate_balatro_episode(args, Sample(prompt=seed,metadata={'balatro_seed':seed}), {}, evaluation=True) + if not isinstance(samples,list): samples=[samples] + (out/'result.json').write_text(json.dumps([vars(s) for s in samples], default=lambda x:x.value if isinstance(x,Enum) else str(x))) + print('RESULT '+json.dumps(samples[0].metadata.get('balatro',samples[0].metadata)),flush=True) +asyncio.run(main()) diff --git a/training/manual_harness_step.py b/training/manual_harness_step.py new file mode 100644 index 00000000..c307adbf --- /dev/null +++ b/training/manual_harness_step.py @@ -0,0 +1,21 @@ +import json,sys,time +from pathlib import Path +folder=Path(__file__).resolve().parents[1]/'reports/manual-harness-20260916'/sys.argv[1] +pending=folder/'pending.json' +if len(sys.argv)>2: + old=json.loads(pending.read_text())['turn'] + action=json.loads(sys.argv[2]); note=sys.argv[3] if len(sys.argv)>3 else '' + target=folder/f'{old:03}-action.json' + assert not target.exists() + target.write_text(json.dumps({'action':action,'reason':note},indent=2)) + for _ in range(300): + if (folder/'result.json').exists(): + r=json.loads((folder/'result.json').read_text())[0] + print(json.dumps(r['metadata'].get('balatro',r['metadata']),indent=2));sys.exit() + try: + if json.loads(pending.read_text())['turn']>old: break + except json.JSONDecodeError: pass + time.sleep(.1) +p=json.loads(pending.read_text());print('TURN',p['turn'],'TOKENS',p['input_tokens']) +text=p['visible']; print(text.split('Legal actions now: ',1)[1].split('Gamestate:',1)[0]); state=json.loads(text.split('Gamestate:\n',1)[1]); state.pop('deck',None); state.pop('owned_deck',None); state.pop('run_state',None); state.pop('discard',None); state.pop('hand_stats',None); state.pop('deck_type',None); state.pop('stake',None); state.pop('owned_vouchers',None); state.pop('blind_state',None); +lastfile=folder/'last-display.json'; oldstate=json.loads(lastfile.read_text()) if lastfile.exists() else {}; lastfile.write_text(json.dumps(state)); displayed={k:v for k,v in state.items() if k in ('phase','ante','round','money','blind','hand','hand_groups','consumables','shop','pack','packs','play_commit') or oldstate.get(k)!=v}; print(json.dumps(displayed,separators=(',',':'))) diff --git a/training/monitor_run.py b/training/monitor_run.py new file mode 100644 index 00000000..e16b2aea --- /dev/null +++ b/training/monitor_run.py @@ -0,0 +1,156 @@ +"""Persist live run evidence, including failures followed by launcher retries. + +This collector does not diagnose or edit jobs; its output is for the supervising +agent. A FAILED attempt is not terminal while the Modal app is still active. +""" + +import argparse +import json +import os +import signal +import subprocess +import sys +import time +from pathlib import Path + +from modal_dojo.common.run import TrainingRun + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("run_id") + parser.add_argument("--interval", type=float, default=30) + parser.add_argument("--learning", action="store_true", help="Also persist metrics and game-level learning evidence") + parser.add_argument("--eval-container", help="Ray head container for held-out episode evidence") + parser.add_argument("--eval-driver-log", help="Ray driver log containing held-out episode events") + parser.add_argument("--once", action="store_true", help=argparse.SUPPRESS) + args = parser.parse_args() + if bool(args.eval_container) != bool(args.eval_driver_log): + parser.error("Provide both --eval-container and --eval-driver-log") + if not args.once: + # A disconnected SDK request can hang indefinitely after the laptop + # sleeps. Bound each poll and give the next one a fresh client/channel. + while True: + process = subprocess.Popen( + [sys.executable, __file__, *sys.argv[1:], "--once"], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, + start_new_session=True, + ) + try: + output, _ = process.communicate(timeout=55) + print(output, end="", flush=True) + if any(line.startswith("TERMINAL ") for line in output.splitlines()): + return + except subprocess.TimeoutExpired: + os.killpg(process.pid, signal.SIGTERM) + output, _ = process.communicate(timeout=5) + print(output, end="", flush=True) + print("MONITOR_READ_TIMEOUT: restarting observer; training unchanged", flush=True) + time.sleep(args.interval) + root = Path(__file__).resolve().parents[1] / "reports" + previous = None + last_print = 0.0 + last_eval_count = 0 + summary_path = root / f"{args.run_id}-eval-summary.json" + if summary_path.exists(): + last_eval_count = len(json.loads(summary_path.read_text()).get("completed_batches", [])) + while True: + try: + run = TrainingRun.from_id(args.run_id) + metadata = run.metadata + if args.learning: + try: + from learning_snapshot import capture + evidence = capture(run, root) + for alert in evidence["alerts"]: + print("LEARNING_REVIEW", alert, flush=True) + if args.eval_container and len(evidence["eval_history"]) > last_eval_count: + process = subprocess.run( + [sys.executable, str(Path(__file__).with_name("evaluation_snapshot.py")), + args.run_id, "--head", args.eval_container, + "--driver-log", args.eval_driver_log], + capture_output=True, text=True, timeout=55, check=True, + ) + print("HELD_OUT_EVIDENCE", process.stdout.strip(), flush=True) + last_eval_count = len(evidence["eval_history"]) + except Exception as exc: + print("LEARNING_READ_ERROR", type(exc).__name__, str(exc)[-500:], flush=True) + progress = metadata.get("framework_progress") or {} + now = time.time() + row = { + "checked_at": now, + "run": run.training_run_id, + "app": run.modal_app_id, + "function_call_id": run.function_call_id, + "status": str(run.status), + "phase": str(run.framework_status), + "progress": progress, + "latest": metadata.get("latest_rollout"), + "attempt_count": metadata.get("attempt_count"), + "error": (run.error_message or "")[-5000:], + } + terminal = str(run.status).split(".")[-1] in { + "FAILED", + "COMPLETED", + "CANCELLED", + "STOPPED", + } + if terminal and run.modal_app_id: + output = subprocess.check_output( + [sys.executable, "-m", "modal", "app", "list", "--json"], + text=True, + timeout=45, + ) + apps = json.loads(output) + app = next( + ( + a + for a in apps + if a.get("app_id", a.get("App ID")) == run.modal_app_id + ), + None, + ) + state = app.get("state", app.get("State")) if app else "not_listed" + row["modal_app_state"] = state + terminal = state in ("stopped", "not_listed") + row["terminal"] = terminal + with (root / f"{args.run_id}-monitor.jsonl").open("a") as file: + file.write(json.dumps(row, default=str) + "\n") + signature = json.dumps( + {k: v for k, v in row.items() if k != "checked_at"}, default=str + ) + if signature != previous or now - last_print >= 120: + print(json.dumps(row, default=str), flush=True) + previous, last_print = signature, now + if terminal: + (root / f"{args.run_id}-metadata.json").write_text( + json.dumps(metadata, indent=2, default=str) + ) + (root / f"{args.run_id}-error.txt").write_text(run.error_message or "") + if run.modal_app_id: + with (root / f"{args.run_id}-final.log").open("w") as file: + subprocess.run( + [ + sys.executable, + "-m", "modal", + "app", + "logs", + run.modal_app_id, + "--tail", + "5000", + ], + stdout=file, + stderr=subprocess.STDOUT, + timeout=45, + ) + print("TERMINAL", args.run_id, str(run.status), flush=True) + return + except Exception as exc: + print("MONITOR_READ_ERROR", type(exc).__name__, str(exc)[-500:], flush=True) + if args.once: + return + time.sleep(args.interval) + + +if __name__ == "__main__": + main() diff --git a/training/patch_checkpoint_rpc.py b/training/patch_checkpoint_rpc.py new file mode 100644 index 00000000..b8f6463f --- /dev/null +++ b/training/patch_checkpoint_rpc.py @@ -0,0 +1,162 @@ +"""Install checkpoint RPC recovery in the pinned Slime training image.""" + +import ast +from pathlib import Path + +WORKER_METHOD = """ + def _checkpoint_save_once(self, operation_id, rollout_id, force_sync=False): + from slime.utils.checkpoint_rpc import save_once + return save_once(self, operation_id, rollout_id, force_sync=force_sync) +""" + + +def patch_group(source): + tree = ast.parse(source) + groups = [ + n + for n in tree.body + if isinstance(n, ast.ClassDef) and n.name == "RayTrainGroup" + ] + if len(groups) != 1: + raise RuntimeError("Unsupported Slime source: expected RayTrainGroup") + group = groups[0] + methods = [ + n + for n in group.body + if isinstance(n, ast.FunctionDef) and n.name == "save_model" + ] + if len(methods) != 1: + raise RuntimeError("Unsupported Slime source: expected save_model") + method = methods[0] + if "from slime.utils.checkpoint_rpc import save_group" in ast.get_source_segment( + source, method + ): + return source + calls = [ + n + for n in ast.walk(method) + if isinstance(n, ast.Call) + and isinstance(n.func, ast.Attribute) + and isinstance(n.func.value, ast.Name) + and n.func.value.id == "ray" + and n.func.attr == "get" + ] + expected = ast.parse( + "ray.get([actor.save_model.remote(rollout_id, force_sync=force_sync) for actor in self._actor_handlers])", + mode="eval", + ).body + if len(calls) != 1 or ast.dump(calls[0]) != ast.dump(expected): + raise RuntimeError("Unsupported Slime checkpoint dispatch") + # Enforce actor-local cache lifetime and serialization, rather than relying + # on Ray defaults that might change with another patch or image version. + options = [ + n + for n in ast.walk(group) + if isinstance(n, ast.Assign) + and any(isinstance(t, ast.Name) and t.id == "actor_options" for t in n.targets) + ] + if len(options) != 1 or not isinstance(options[0].value, ast.Dict): + raise RuntimeError("Unsupported Slime actor options") + option = options[0].value + if any( + isinstance(k, ast.Constant) + and k.value in {"max_restarts", "max_concurrency", "max_task_retries"} + for k in option.keys + ): + raise RuntimeError("Unexpected Slime actor retry/concurrency options") + # Refuse additional runtime overrides, which would invalidate deduplication. + for n in ast.walk(group): + if isinstance(n, ast.keyword) and n.arg in { + "max_restarts", + "max_concurrency", + "max_task_retries", + }: + raise RuntimeError("Unexpected Slime actor retry/concurrency override") + lines = source.splitlines(keepends=True) + call = calls[0] + if call.lineno != call.end_lineno: + raise RuntimeError("Unsupported multiline Slime checkpoint dispatch") + i = call.lineno - 1 + lines[i] = ( + lines[i][: call.col_offset] + + "save_group(self._actor_handlers, rollout_id, force_sync=force_sync)" + + lines[i][call.end_col_offset :] + ) + lines.insert(i, " from slime.utils.checkpoint_rpc import save_group\n") + i = option.lineno + lines.insert( + i, + ' "max_restarts": 0, "max_concurrency": 1, "max_task_retries": 0,\n', + ) + updated = "".join(lines) + compile(updated, "actor_group.py", "exec") + return updated + + +def patch_worker(source): + tree = ast.parse(source) + actors = [ + n + for n in tree.body + if isinstance(n, ast.ClassDef) and n.name == "MegatronTrainRayActor" + ] + if len(actors) != 1: + raise RuntimeError("Unsupported Slime source: expected MegatronTrainRayActor") + actor = actors[0] + if any( + isinstance(n, ast.FunctionDef) and n.name == "_checkpoint_save_once" + for n in actor.body + ): + if WORKER_METHOD.strip() not in source: + raise RuntimeError("Unexpected existing checkpoint wrapper") + return source + if not any( + isinstance(n, ast.FunctionDef) and n.name == "save_model" for n in actor.body + ): + raise RuntimeError("Unsupported Slime source: no worker save_model") + lines = source.splitlines(keepends=True) + lines.insert(actor.end_lineno, "\n" + WORKER_METHOD) + updated = "".join(lines) + compile(updated, "actor.py", "exec") + return updated + + +def patch_train(source): + """Evaluate restored final weights when no optimizer updates remain.""" + original = ( + " if args.num_rollout == 0 and args.eval_interval is not None:\n" + " ray.get(rollout_manager.eval.remote(rollout_id=0))" + ) + replacement = ( + " # A completed final save can lose its reply before final evaluation.\n" + " if (args.num_rollout == 0 or args.start_rollout_id == args.num_rollout) and args.eval_interval is not None:\n" + " ray.get(rollout_manager.eval.remote(rollout_id=max(0, args.start_rollout_id - 1)))" + ) + if replacement in source: + return source + if source.count(original) != 1: + raise RuntimeError("Unsupported Slime final-checkpoint evaluation dispatch") + updated = source.replace(original, replacement) + compile(updated, "train.py", "exec") + return updated + + +def install(root=Path("/root/slime")): + root = Path(root) + group = root / "slime/ray/actor_group.py" + actor = root / "slime/backends/megatron_utils/actor.py" + train = root / "train.py" + updated_group = patch_group(group.read_text()) + updated_actor = patch_worker(actor.read_text()) + updated_train = patch_train(train.read_text()) + runtime = Path(__file__).with_name("checkpoint_rpc.py").read_text() + compile(runtime, "checkpoint_rpc.py", "exec") + (root / "slime/utils/checkpoint_rpc.py").write_text(runtime) + group.write_text(updated_group) + actor.write_text(updated_actor) + train.write_text(updated_train) + print("Installed bounded, deduplicated checkpoint RPC retries", flush=True) + + +if __name__ == "__main__": + install() diff --git a/training/patch_pipeline_group.py b/training/patch_pipeline_group.py new file mode 100644 index 00000000..ffcda7e4 --- /dev/null +++ b/training/patch_pipeline_group.py @@ -0,0 +1,68 @@ +"""Restore Megatron's pipeline group argument in the pinned Slime image. + +The image drops the fourth P2POp argument in _batched_p2p_ops, so payloads +use WORLD while shapes use the pipeline subgroup. Keep independent pipelines +on their own communicators, as in upstream Megatron. +""" + +import ast +from pathlib import Path + + +def patch_source(source: str) -> str: + tree = ast.parse(source) + functions = [ + node + for node in tree.body + if isinstance(node, ast.FunctionDef) and node.name == "_batched_p2p_ops" + ] + if len(functions) != 1: + raise RuntimeError("Unsupported Megatron source: expected _batched_p2p_ops") + calls = [ + node + for node in ast.walk(functions[0]) + if isinstance(node, ast.Call) + and isinstance(node.func, ast.Attribute) + and node.func.attr == "P2POp" + ] + if len(calls) != 4: + raise RuntimeError("Unsupported Megatron source: expected four pipeline P2POps") + missing = [] + for call in calls: + if call.keywords or len(call.args) not in (3, 4): + raise RuntimeError("Unsupported pipeline P2POp signature") + if len(call.args) == 4: + if not isinstance(call.args[3], ast.Name) or call.args[3].id != "group": + raise RuntimeError("Unexpected pipeline P2POp group") + else: + missing.append(call) + if not missing: + return source + if len(missing) != 4: + raise RuntimeError("Partially patched pipeline source") + lines = source.splitlines(keepends=True) + # Insert at the end of the peer argument, retaining all original formatting. + for call in sorted( + missing, key=lambda node: (node.lineno, node.col_offset), reverse=True + ): + peer = call.args[2] + index, column = peer.end_lineno - 1, peer.end_col_offset + lines[index] = lines[index][:column] + ", group" + lines[index][column:] + updated = "".join(lines) + compile(updated, "p2p_communication.py", "exec") + return updated + + +def install(root=Path("/root/Megatron-LM")): + path = Path(root) / "megatron/core/pipeline_parallel/p2p_communication.py" + source = path.read_text() + updated = patch_source(source) + if updated != source: + path.write_text(updated) + print( + "Validated pipeline P2POps: all four use the pipeline process group", flush=True + ) + + +if __name__ == "__main__": + install() diff --git a/training/pipeline_group_integration.py b/training/pipeline_group_integration.py new file mode 100644 index 00000000..9dc715f2 --- /dev/null +++ b/training/pipeline_group_integration.py @@ -0,0 +1,122 @@ +"""Four-GPU regression in the pinned image; no model or game is loaded. + +Run through pipeline_group_modal.py. An independent pipeline pair must work +while the other pair waits on CPU, including after Slime offload/reload. +""" + +import ast +import faulthandler +import sys +import tempfile +from datetime import timedelta +from pathlib import Path + +import torch +import torch.distributed as dist +import torch.multiprocessing as mp + + +def worker(rank, rendezvous, patched): + faulthandler.dump_traceback_later(60, repeat=False) + from patch_pipeline_group import patch_source + from slime.utils import reloadable_process_group as reloadable + from slime.utils.distributed_utils import get_gloo_group, init_gloo_group + + torch.cuda.set_device(rank) + dist.init_process_group( + "nccl", + init_method="file://" + rendezvous, + rank=rank, + world_size=4, + timeout=timedelta(seconds=20), + ) + init_gloo_group() + reloadable.register_default_process_group(timedelta(seconds=20)) + reloadable.monkey_patch_torch_dist() + groups = [dist.new_group(ranks=ranks, backend="nccl") for ranks in ([0, 1], [2, 3])] + group = groups[rank // 2] + source = Path( + "/root/Megatron-LM/megatron/core/pipeline_parallel/p2p_communication.py" + ).read_text() + if patched: + source = patch_source(source) + tree = ast.parse(source) + fn = next( + node + for node in tree.body + if isinstance(node, ast.FunctionDef) and node.name == "_batched_p2p_ops" + ) + module = ast.Module( + body=[ + ast.ImportFrom( + module="__future__", names=[ast.alias(name="annotations")], level=0 + ), + fn, + ], + type_ignores=[], + ) + namespace = {"torch": torch} + exec( + compile(ast.fix_missing_locations(module), "p2p_communication.py", "exec"), + namespace, + ) + transfer = namespace["_batched_p2p_ops"] + print(f"READY rank={rank} patched={patched}", flush=True) + transfers = 0 + for generation in range(3): + # Only one pair initializes/uses its communicator at a time. With the + # image defect, the first pair tries to initialize all of WORLD and hangs. + for pair in (0, 1): + if rank // 2 == pair: + for step in range(12): + size = (step + 1) * 1024 + sender = rank % 2 == step % 2 + value = generation * 100 + step + tensor = torch.full( + (size,), float(value) if sender else -1.0, device="cuda" + ) + kwargs = { + "tensor_" + name: None + for name in ("send_prev", "recv_prev", "send_next", "recv_next") + } + direction = "next" if rank % 2 == 0 else "prev" + kwargs["tensor_" + ("send_" if sender else "recv_") + direction] = ( + tensor + ) + if step == 0: + print( + f"TRANSFER rank={rank} generation={generation} pair={pair} patched={patched}", + flush=True, + ) + reqs = transfer( + group=group, + prev_pipeline_rank=rank ^ 1, + next_pipeline_rank=rank ^ 1, + **kwargs, + ) + for req in reqs: + req.wait() + torch.cuda.synchronize() + assert torch.all(tensor == value).item() + transfers += 1 + dist.barrier(group=get_gloo_group()) + if generation < 2: + reloadable.destroy_process_groups() + reloadable.reload_process_groups() + print( + f"PASS rank={rank} transfers={transfers} reloads=2 patched={patched}", + flush=True, + ) + faulthandler.cancel_dump_traceback_later() + dist.barrier(group=get_gloo_group()) + dist.destroy_process_group() + + +if __name__ == "__main__": + with tempfile.TemporaryDirectory() as root: + mp.spawn( + worker, + args=(str(Path(root) / "rendezvous"), "--patched" in sys.argv), + nprocs=4, + join=True, + ) diff --git a/training/pipeline_group_modal.py b/training/pipeline_group_modal.py new file mode 100644 index 00000000..d7c966a3 --- /dev/null +++ b/training/pipeline_group_modal.py @@ -0,0 +1,82 @@ +"""Bounded GPU communication reproduction and regression, without training.""" + +import json +from pathlib import Path + +import modal +from modal_dojo.frameworks.slime.launcher import SLIME_IMAGE + +ROOT = Path(__file__).resolve().parent +app = modal.App("balatro-pipeline-group-regression") +image = ( + modal.Image.from_registry(SLIME_IMAGE) + .entrypoint([]) + .add_local_file( + ROOT / "pipeline_group_integration.py", "/tmp/pipeline_group_integration.py" + ) + .add_local_file(ROOT / "patch_pipeline_group.py", "/tmp/patch_pipeline_group.py") +) + + +@app.function(image=image, gpu="H100:4", timeout=360, retries=0, serialized=True) +def check(): + import os + import signal + import subprocess + + env = { + **os.environ, + "PYTHONPATH": "/root/slime:/root/Megatron-LM:/tmp", + "NCCL_RAS_ENABLE": "0", + "TORCH_NCCL_HEARTBEAT_TIMEOUT_SEC": "45", + } + results = [] + for patched in (False, True): + command = ["python", "/tmp/pipeline_group_integration.py"] + ( + ["--patched"] if patched else [] + ) + proc = subprocess.Popen( + command, + env=env, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + start_new_session=True, + ) + try: + stdout, stderr = proc.communicate(timeout=140) + result = { + "patched": patched, + "returncode": proc.returncode, + "stdout": stdout, + "stderr": stderr, + } + except subprocess.TimeoutExpired: + os.killpg(proc.pid, signal.SIGKILL) + stdout, stderr = proc.communicate() + result = { + "patched": patched, + "returncode": "timeout", + "stdout": stdout, + "stderr": stderr, + } + results.append(result) + print(json.dumps(result), flush=True) + return results + + +@app.local_entrypoint() +def main(out: str): + results = check.remote() + Path(out).write_text(json.dumps(results, indent=2)) + assert results[0]["returncode"] != 0, ( + "Expected unpatched image to reproduce the failure" + ) + assert results[0]["stdout"].count("READY rank=") == 4, ( + "Unpatched test failed before all workers initialized" + ) + assert "_batched_p2p_ops" in results[0]["stderr"], ( + "Unpatched test did not establish a pipeline-transfer stall" + ) + assert results[1]["returncode"] == 0, "Corrected pipeline test failed" + assert results[1]["stdout"].count("PASS rank=") == 4 diff --git a/training/pyproject.toml b/training/pyproject.toml new file mode 100644 index 00000000..c568aa55 --- /dev/null +++ b/training/pyproject.toml @@ -0,0 +1,14 @@ +[project] +name = "balatro-training" +version = "0.1.0" +requires-python = ">=3.12,<3.13" +dependencies = ["modal-dojo", "pyyaml>=6"] + +[tool.uv] +package = false + +[tool.uv.sources] +modal-dojo = { path = ".dojo", editable = true } + +[dependency-groups] +test = ["pytest>=9", "pytest-asyncio>=1.3", "pytest-xdist[psutil]>=3.8"] diff --git a/training/register_watchdog.py b/training/register_watchdog.py new file mode 100644 index 00000000..8f5ec179 --- /dev/null +++ b/training/register_watchdog.py @@ -0,0 +1,50 @@ +"""Register a submitted Dojo run with Balatro's deployed cloud watchdog.""" + +import argparse +import hashlib +import json +import os +import time +from pathlib import Path + +import modal + + +def register(run_id, app_id, *, updates=200, thinking_budget=8192): + environment = os.environ.get("MODAL_ENVIRONMENT", "joy-dev") + if environment != "joy-dev": + raise ValueError("Deploy a watchdog for the selected environment before registering this run") + from modal_dojo.common.run import TrainingRun + run = TrainingRun.from_id(run_id) + if run.modal_app_id != app_id: + raise ValueError("Run/app identity mismatch; watchdog registration refused") + registration = { + "active": True, "app_id": app_id, "registered_at": time.time(), + "policy_version": 3, "requested_updates": updates, + "thinking_budget": thinking_budget, + "policy_sha256": hashlib.sha256(Path(__file__).with_name("watchdog_policy.py").read_bytes()).hexdigest(), + } + registry = modal.Dict.from_name("balatro-dojo-watchdog", create_if_missing=True, environment_name=environment) + registry.put(run_id, registration) + checks = modal.Function.from_name("balatro-dojo-watchdog", "tick", environment_name=environment).remote() + return {"registration": registration, "first_check": [x for x in checks if x.get("run") == run_id]} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("receipt", type=Path) + parser.add_argument("--updates", type=int, default=200) + parser.add_argument("--thinking-budget", type=int, default=8192) + args = parser.parse_args() + receipt = json.loads(args.receipt.read_text()) + if receipt.get("state") != "submitted": + raise ValueError("Expected a submitted launch receipt") + result = register(receipt["training_run_id"], receipt["modal_app_id"], + updates=args.updates, thinking_budget=args.thinking_budget) + path = args.receipt.with_suffix(".watchdog.json") + path.write_text(json.dumps(result, indent=2)) + print(json.dumps(result, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/training/setup_dojo.py b/training/setup_dojo.py new file mode 100644 index 00000000..2908828a --- /dev/null +++ b/training/setup_dojo.py @@ -0,0 +1,34 @@ +"""Prepare the pinned Dojo source and Balatro checkpoint recovery fixes.""" + +import argparse +import subprocess +from pathlib import Path + +REVISION = "0bd6789d1be43c1eb10928d3d69be55de2d2533e" +ROOT = Path(__file__).resolve().parent + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--source", default="https://github.com/modal-projects/modal-dojo.git", + help="Git URL or existing local Dojo repository to clone") + args = parser.parse_args() + target = ROOT / ".dojo" + patch = ROOT / "dojo-checkpoint-recovery.patch" + if not target.exists(): + subprocess.run(["git", "clone", "--no-checkout", "--no-hardlinks", args.source, str(target)], check=True) + subprocess.run(["git", "checkout", "--detach", REVISION], cwd=target, check=True) + revision = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=target, text=True).strip() + if revision != REVISION: + raise RuntimeError(f"Expected Dojo {REVISION}; found {revision}. Existing checkout was not changed.") + applied = subprocess.run(["git", "apply", "--reverse", "--check", str(patch)], + cwd=target, capture_output=True).returncode == 0 + if not applied: + subprocess.run(["git", "apply", "--check", str(patch)], cwd=target, check=True) + subprocess.run(["git", "apply", str(patch)], cwd=target, check=True) + print(f"Dojo ready: {target} ({REVISION}, checkpoint recovery patch applied)") + print("Install: uv sync --project training") + + +if __name__ == "__main__": + main() diff --git a/training/uv.lock b/training/uv.lock new file mode 100644 index 00000000..f7e461a0 --- /dev/null +++ b/training/uv.lock @@ -0,0 +1,1248 @@ +version = 1 +revision = 3 +requires-python = "==3.12.*" +resolution-markers = [ + "sys_platform == 'win32'", + "sys_platform == 'emscripten'", + "sys_platform != 'emscripten' and sys_platform != 'win32'", +] + +[[package]] +name = "aiohappyeyeballs" +version = "2.7.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/ce/f4/eec0465c2f67b2664688d0240b3212d5196fd89e741df67ddb81f8d35658/aiohappyeyeballs-2.7.1.tar.gz", hash = "sha256:065665c041c42a5938ed220bdcd7230f22527fbec085e1853d2402c8a3615d9d", size = 24757, upload-time = "2026-07-01T17:11:55.501Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/71/43/1947f06babed6b3f1d7f38b0c767f52df66bfb2bc10b468c4a7de9eceff2/aiohappyeyeballs-2.7.1-py3-none-any.whl", hash = "sha256:9243213661e29250eb41368e5daa826fc017156c3b8a11440826b2e3ed376472", size = 15038, upload-time = "2026-07-01T17:11:54.055Z" }, +] + +[[package]] +name = "aiohttp" +version = "3.14.5" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "aiohappyeyeballs" }, + { name = "aiosignal" }, + { name = "attrs" }, + { name = "frozenlist" }, + { name = "multidict" }, + { name = "propcache" }, + { name = "typing-extensions" }, + { name = "yarl" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/6c/4c/bdccd81e9ee225b69c60e7766c9a5b05364f118f4d383713b89a682d772d/aiohttp-3.14.5.tar.gz", hash = "sha256:5558a7f5a05af9ecf744af91e5baefc436f93c9333e656c27ec253f9a6bbe178", size = 8078052, upload-time = "2026-10-11T01:05:12.408Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d5/94/6ba86efddcb616c811b40e6a0dfdd862738f647e4e3860a961075d5e9ed8/aiohttp-3.14.5-cp312-cp312-macosx_10_13_universal2.whl", hash = "sha256:df37b620684e19b5e25724412518ccafc3b1a49cdac706fdbd2f983fad943450", size = 817499, upload-time = "2026-10-11T01:00:18.901Z" }, + { url = "https://files.pythonhosted.org/packages/5e/e1/7bca6d84dabd228aa8eb4b7f9feac2586aaa9be5505d7d65bf287a615c43/aiohttp-3.14.5-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:ef60869969180ec2464f1349aff07138ae35ca2200f0946cb3552e49e8f301a8", size = 549467, upload-time = "2026-10-11T01:00:20.854Z" }, + { url = "https://files.pythonhosted.org/packages/64/91/11b89f45ca486252dd67dd5f3231fec04bf5518da39a95cb3997619f17fb/aiohttp-3.14.5-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:d079c0a0135c36e7beb6f1c88087c8f108dc5891cdd0b5eafa778421bda70ed2", size = 548067, upload-time = "2026-10-11T01:00:22.659Z" }, + { url = "https://files.pythonhosted.org/packages/22/ff/c6615806c14aab34f82b9424ccde8ce6e417315fd57ce1c5b4d4747888e1/aiohttp-3.14.5-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:abfda5cb094a829f7bc25216a32f7db2e85cc65bd59910f8e7b40b3d9b224764", size = 1896245, upload-time = "2026-10-11T01:00:24.638Z" }, + { url = "https://files.pythonhosted.org/packages/39/b2/25a8c971ae6a92c8394d77e42422d7f38989e05cf41a5ceb92d73d67ab7e/aiohttp-3.14.5-cp312-cp312-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:9cc882cf8619109583c906b4d4a85d6a111a98afa34b7a450d1e08118d016820", size = 1872180, upload-time = "2026-10-11T01:00:26.838Z" }, + { url = "https://files.pythonhosted.org/packages/2f/d5/99f93ea36cc5205e47c1e5a803e087f2ad21b5430b5db2e942cb6e988a37/aiohttp-3.14.5-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:7457580535e019e1247ea35d6a02bf081ad30c26d0cbc210c93f6c3ab67a0835", size = 1937261, upload-time = "2026-10-11T01:00:28.74Z" }, + { url = "https://files.pythonhosted.org/packages/40/a6/9ac9c9e6695040bd73d2584a1b59a9388f6433c76d5294a7bf591e21ffe5/aiohttp-3.14.5-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:c5ed596aedb9c42afd3fe0aae3117725378ac73d2cc5ddc735056fbdb96c5d02", size = 2064994, upload-time = "2026-10-11T01:00:30.623Z" }, + { url = "https://files.pythonhosted.org/packages/da/e4/aa172eb534b7f02f1f8ff1c3213347eaf3cf218db91a727c6863c22f1035/aiohttp-3.14.5-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:20f085697d7e911f1f73c43ed03fafbed1e7121797e2eb5428efa80398060584", size = 1919630, upload-time = "2026-10-11T01:00:32.548Z" }, + { url = "https://files.pythonhosted.org/packages/06/7d/4eedafc5bababa8932636c141e346806966eade12c0b7e5946d43bf8218b/aiohttp-3.14.5-cp312-cp312-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:74b0a9c8270f9b0a11410e124ff8d4f18bfc1f1837440ec84da5ae7b50927b5d", size = 1710016, upload-time = "2026-10-11T01:00:34.471Z" }, + { url = "https://files.pythonhosted.org/packages/b2/94/eee018537ba19da0ceb2ac79cab83faed4ac49568e08376e2799043f2538/aiohttp-3.14.5-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:19e2ba471507c34f8252402ab50f5ab512398b9ea8c8f1cb26beb3f75793ba30", size = 1858112, upload-time = "2026-10-11T01:00:36.581Z" }, + { url = "https://files.pythonhosted.org/packages/68/76/354653a306547238f3427283905972d796ba7c292ba9977abec9f2b6f260/aiohttp-3.14.5-cp312-cp312-musllinux_1_2_armv7l.whl", hash = "sha256:d418ce2af40c6bb685b3f663e9e8de27cb0a22431d8e88a167348d7f01878073", size = 1896371, upload-time = "2026-10-11T01:00:38.478Z" }, + { url = "https://files.pythonhosted.org/packages/f2/ec/63e8c7136b570e356345ad3174e3820fdc973ea10712cb6c649bf875755a/aiohttp-3.14.5-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:70cb4008ac2ed1e0ca9e824deb4b53d3aa0d939109698ebf1e723a84337bd794", size = 1905836, upload-time = "2026-10-11T01:00:40.527Z" }, + { url = "https://files.pythonhosted.org/packages/57/d8/11365bda144b127928cd42533d0eff78a55613c9e28f81941bd6630ea887/aiohttp-3.14.5-cp312-cp312-musllinux_1_2_riscv64.whl", hash = "sha256:a23fe35d776bc03cb495938b9594450d047e3bc08c5255315a82323e9cb7d2dd", size = 1696534, upload-time = "2026-10-11T01:00:42.686Z" }, + { url = "https://files.pythonhosted.org/packages/2f/3d/82df0461b18e00b2998f205c03e0d3010222478c43640aceb8e03dcd7e8f/aiohttp-3.14.5-cp312-cp312-musllinux_1_2_s390x.whl", hash = "sha256:3e0eb43bed3c6801a6cee315195377789e90b2a72c2277a475b578535312488d", size = 1933970, upload-time = "2026-10-11T01:00:44.71Z" }, + { url = "https://files.pythonhosted.org/packages/03/ad/6ddfe0aacd931c17b53533336d97e9d11a98b96d6ae815a9da0b19f82ccf/aiohttp-3.14.5-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:3be7dd397d64ca3e1869626fa9318aaebb54b7bf93bc72d7a205448d83e4f748", size = 1899501, upload-time = "2026-10-11T01:00:46.629Z" }, + { url = "https://files.pythonhosted.org/packages/9e/8e/189bdd9ae4793059bb09f6dc880f211a6c6c7859cebcbf33912dbfab7dd7/aiohttp-3.14.5-cp312-cp312-win32.whl", hash = "sha256:eb324e2009fb54db30a071dad7caf6998ee2879c4704007efb244514dad1fec1", size = 480370, upload-time = "2026-10-11T01:00:48.468Z" }, + { url = "https://files.pythonhosted.org/packages/ae/ce/1f08114679d49655b30a6e0a29858375c94b82c1de1a0bd0a20c2fee8b02/aiohttp-3.14.5-cp312-cp312-win_amd64.whl", hash = "sha256:2cc38a4f2b516bef1714e690df87a0e043faf1a7693c82d860091684453d5111", size = 506141, upload-time = "2026-10-11T01:00:50.272Z" }, + { url = "https://files.pythonhosted.org/packages/79/d4/c7b4f60b16a1b7e43249fa9031ae05e7e8c341ba4b7d1866f914dafeaa0e/aiohttp-3.14.5-cp312-cp312-win_arm64.whl", hash = "sha256:a63afd1f757de949028387e65a7127b61ad0f775432dbb0e62816ae619fe69ac", size = 488108, upload-time = "2026-10-11T01:00:52.321Z" }, + { url = "https://files.pythonhosted.org/packages/68/30/173960c42b05a6c59f7558e4b12a4b0d9ba376cf6aa9bde7f9e08a30ca8d/aiohttp-3.14.5-py3-none-any.whl", hash = "sha256:efc21a454892828368b11c2c780de0ff8bc991f73f6b99c6b66e56205470929b", size = 279517, upload-time = "2026-10-11T01:05:08.523Z" }, +] + +[[package]] +name = "aiosignal" +version = "1.4.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "frozenlist" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/61/62/06741b579156360248d1ec624842ad0edf697050bbaf7c3e46394e106ad1/aiosignal-1.4.0.tar.gz", hash = "sha256:f47eecd9468083c2029cc99945502cb7708b082c232f9aca65da147157b251c7", size = 25007, upload-time = "2025-07-03T22:54:43.528Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/fb/76/641ae371508676492379f16e2fa48f4e2c11741bd63c48be4b12a6b09cba/aiosignal-1.4.0-py3-none-any.whl", hash = "sha256:053243f8b92b990551949e63930a839ff0cf0b0ebbe0597b0f3fb19e1a0fe82e", size = 7490, upload-time = "2025-07-03T22:54:42.156Z" }, +] + +[[package]] +name = "annotated-doc" +version = "0.0.5" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/5a/8e/38aa427ed5402449e226975b649c5dc73ccadfefeb95e6aecb8f8ea4b6b6/annotated_doc-0.0.5.tar.gz", hash = "sha256:c7e58ce09192557605d8bbd92836d7e1d520ac9580096042c0bfd197efacf1bb", size = 10758, upload-time = "2026-07-28T13:50:58.129Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/3e/30/e900b21425a860e195f32e37657aa1f7c7f2b1bfb26f03ca209b90933c06/annotated_doc-0.0.5-py3-none-any.whl", hash = "sha256:117bac03a25ede5df5440e855b32d556049ca169ead221505badf432fed4b101", size = 5302, upload-time = "2026-07-28T13:50:57.239Z" }, +] + +[[package]] +name = "annotated-types" +version = "0.8.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/5f/56/a8120250d128bed162cd73c76d45f6ef9991f3e068f62a8ee060afa3104a/annotated_types-0.8.0.tar.gz", hash = "sha256:13b2beaad985e05e2d6407ee4c4f35590b11f8d693a258a561055cac8f64cab7", size = 15893, upload-time = "2026-07-23T20:16:13.995Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/99/91/8acff4f5e50511b911bbccb72b8628a49c68ce14148cd9f6431094859a90/annotated_types-0.8.0-py3-none-any.whl", hash = "sha256:f072f4d804ea359e4eaf198b1af7a8b0943881a87f31bb764f8bf219bb9419e0", size = 13427, upload-time = "2026-07-23T20:16:12.938Z" }, +] + +[[package]] +name = "anyio" +version = "4.15.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "idna" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/a9/d2/f4d173e22df740bc37b1db102b386ba719b66e95b0f0d751f556b387e6d2/anyio-4.15.1.tar.gz", hash = "sha256:9f28306018cbd6d329e64a36d58256edff76dd996fe423bc957326e578b82a94", size = 276966, upload-time = "2026-09-05T10:42:39.44Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/12/b8/4bd346e22b28902df4d651910f5242c28d84e4a5c2435ca5c3f797ed7e2e/anyio-4.15.1-py3-none-any.whl", hash = "sha256:6152fdbbf9a77fdec97731721bebf7c4c44f7c29b424b0065826173efc7ed101", size = 132079, upload-time = "2026-09-05T10:42:37.923Z" }, +] + +[[package]] +name = "attrs" +version = "26.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/9a/8e/82a0fe20a541c03148528be8cac2408564a6c9a0cc7e9171802bc1d26985/attrs-26.1.0.tar.gz", hash = "sha256:d03ceb89cb322a8fd706d4fb91940737b6642aa36998fe130a9bc96c985eff32", size = 952055, upload-time = "2026-03-19T14:22:25.026Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/64/b4/17d4b0b2a2dc85a6df63d1157e028ed19f90d4cd97c36717afef2bc2f395/attrs-26.1.0-py3-none-any.whl", hash = "sha256:c647aa4a12dfbad9333ca4e71fe62ddc36f4e63b2d260a37a8b83d2f043ac309", size = 67548, upload-time = "2026-03-19T14:22:23.645Z" }, +] + +[[package]] +name = "balatro-training" +version = "0.1.0" +source = { virtual = "." } +dependencies = [ + { name = "modal-dojo" }, + { name = "pyyaml" }, +] + +[package.dev-dependencies] +test = [ + { name = "pytest" }, + { name = "pytest-asyncio" }, + { name = "pytest-xdist", extra = ["psutil"] }, +] + +[package.metadata] +requires-dist = [ + { name = "modal-dojo", editable = ".dojo" }, + { name = "pyyaml", specifier = ">=6" }, +] + +[package.metadata.requires-dev] +test = [ + { name = "pytest", specifier = ">=9" }, + { name = "pytest-asyncio", specifier = ">=1.3" }, + { name = "pytest-xdist", extras = ["psutil"], specifier = ">=3.8" }, +] + +[[package]] +name = "cbor2" +version = "6.1.5" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/39/34/d443914ea562a985ccb357682e17b7190d5d58eff797c741379be47a8f31/cbor2-6.1.5.tar.gz", hash = "sha256:6eb06160c42315ac0c4ded461c7d84d92fa18c69d13d17fc1dfc1fae96580c95", size = 94232, upload-time = "2026-10-01T18:09:33.621Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/a0/d6/8278f1abd5b6b5bcfc94158226a737b62fa0e50ba1d8d0b77f42edbf74f8/cbor2-6.1.5-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:0c1565bcd74a389b581e292592ccab0ed9c46286c6e986256820bc68c9ad7e8c", size = 407737, upload-time = "2026-10-01T18:08:14.982Z" }, + { url = "https://files.pythonhosted.org/packages/fa/1b/a58d72ecbe15273e4e4842ac2149361e2bc0ad75fcab117c06da3c31782f/cbor2-6.1.5-cp312-cp312-manylinux_2_28_aarch64.whl", hash = "sha256:f8f85a49db66df77546d278de4d249772a4557d715df07ba8ae155cfa6a7fb31", size = 451924, upload-time = "2026-10-01T18:08:16.618Z" }, + { url = "https://files.pythonhosted.org/packages/72/28/72c76aee7aa74e5dc53b79505dc6c168805d20c8e75166143076c5b61906/cbor2-6.1.5-cp312-cp312-manylinux_2_28_x86_64.whl", hash = "sha256:b70d7c47ea84d456034d2be02e89d92eef7044cfcedf6f05058e21d4452f0fef", size = 463316, upload-time = "2026-10-01T18:08:18.293Z" }, + { url = "https://files.pythonhosted.org/packages/0b/a4/d81e9351c9ad37da4d999edcd05c6542a24e8899bb0ee8f91990e9e52981/cbor2-6.1.5-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:694f75fdcdb8c6b9a71ab77f789f56be1deab20bbdbf948d5ff53cd7c2543dfc", size = 519564, upload-time = "2026-10-01T18:08:20.123Z" }, + { url = "https://files.pythonhosted.org/packages/af/c7/f7da3d0d46022a1c802074e13966863972d68f29cf07301cce2c8e98febc/cbor2-6.1.5-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:09eeb76177758a0fdf1627a9428b384756872b048c6c0d7d158106b29b207d2c", size = 530974, upload-time = "2026-10-01T18:08:21.83Z" }, + { url = "https://files.pythonhosted.org/packages/5f/e3/74fddce015b171ee087a6e0185a233f3d29c7fda80cfa3041c796a67d100/cbor2-6.1.5-cp312-cp312-win32.whl", hash = "sha256:789ef813f416d353aecd5c8824860ee4be94e0f1179a385eb2beccfbeb615e4f", size = 281010, upload-time = "2026-10-01T18:08:23.614Z" }, + { url = "https://files.pythonhosted.org/packages/5e/f5/ecc8d6a9ff9322405b23a4d3226504e7d7a44424e0d831a02b49bac8e605/cbor2-6.1.5-cp312-cp312-win_amd64.whl", hash = "sha256:9677ce1c3c0cb1fa5a4f721a127fc2cc06e8efc43ee8e5f94e292186d6b51953", size = 304308, upload-time = "2026-10-01T18:08:25.077Z" }, + { url = "https://files.pythonhosted.org/packages/a8/90/23b702147b0858dbbc8a3136f288248118bb32f2785cc35c470a3b3f5571/cbor2-6.1.5-cp312-cp312-win_arm64.whl", hash = "sha256:b73d982e35a60e602a200feb2a9d272e850efdc9ff767b0f4887bdbc16d23e52", size = 293958, upload-time = "2026-10-01T18:08:26.493Z" }, +] + +[[package]] +name = "certifi" +version = "2026.10.18" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/7e/eb/a2aa1ac3ea30dd3f30ca57f40232138b6b0040719316ecb85daf61167771/certifi-2026.10.18.tar.gz", hash = "sha256:b9995d97b64d0431a5af837d8ca8cd6238104d1f319f04bd93ea43124d6c861e", size = 135227, upload-time = "2026-10-18T16:21:07.655Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/08/ba/95dd616042484f7c83a06974388b37b837732740ee70bc5dc715b5fcd55a/certifi-2026.10.18-py3-none-any.whl", hash = "sha256:73191c47aaa79da394e8fee2dbc2ec827432cab56311122c9e47abf79e390f62", size = 134093, upload-time = "2026-10-18T16:21:06.276Z" }, +] + +[[package]] +name = "click" +version = "8.5.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/c7/0e/7fa0ef50764b67090eca4114772a2abf8b6148198475e54c660b97caeee6/click-8.5.0.tar.gz", hash = "sha256:ba0d2089de75ea0310e2dde03160e6ca10009947fb95a182f9b54021bb272e34", size = 382235, upload-time = "2026-08-26T13:33:14.56Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/58/50/6c0d534c5f134586a8e1ba4e330569e32f057e33372ae556463212fb4cd3/click-8.5.0-py3-none-any.whl", hash = "sha256:255bc9599cf7748b4b1a446ccc735421bd08a2ae529a8b88597d3de5664ee360", size = 125251, upload-time = "2026-08-26T13:33:12.928Z" }, +] + +[[package]] +name = "cloudpickle" +version = "3.1.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/27/fb/576f067976d320f5f0114a8d9fa1215425441bb35627b1993e5afd8111e5/cloudpickle-3.1.2.tar.gz", hash = "sha256:7fda9eb655c9c230dab534f1983763de5835249750e85fbcef43aaa30a9a2414", size = 22330, upload-time = "2025-11-03T09:25:26.604Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/88/39/799be3f2f0f38cc727ee3b4f1445fe6d5e4133064ec2e4115069418a5bb6/cloudpickle-3.1.2-py3-none-any.whl", hash = "sha256:9acb47f6afd73f60dc1df93bb801b472f05ff42fa6c84167d25cb206be1fbf4a", size = 22228, upload-time = "2025-11-03T09:25:25.534Z" }, +] + +[[package]] +name = "colorama" +version = "0.4.6" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d8/53/6f443c9a4a8358a93a6792e2acffb9d9d5cb0a5cfd8802644b7b1c9a02e4/colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", size = 27697, upload-time = "2022-10-25T02:36:22.414Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" }, +] + +[[package]] +name = "datasets" +version = "5.1.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "dill" }, + { name = "filelock" }, + { name = "fsspec", extra = ["http"] }, + { name = "huggingface-hub" }, + { name = "multiprocess" }, + { name = "numpy" }, + { name = "packaging" }, + { name = "pandas" }, + { name = "pyarrow" }, + { name = "pyyaml" }, + { name = "tqdm" }, + { name = "xxhash" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/17/e6/b5d8c77f13cc6269660b0f2691e8875777b9d94972e281b7b2c25f25bd9d/datasets-5.1.0.tar.gz", hash = "sha256:1561347daa8841b51351ef2ad1647b4ee4af406852c0f7bd1df587ac1ca7888e", size = 687306, upload-time = "2026-10-05T15:32:32.053Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/24/cc/7f624629846c709ef1c68aa61bfb8d0ae2638cbe265104a1c2f7a630949e/datasets-5.1.0-py3-none-any.whl", hash = "sha256:86fd08b280485aab82894d2c81842c4151d93b575efea0054315104cb7bc3cec", size = 604432, upload-time = "2026-10-05T15:32:29.892Z" }, +] + +[[package]] +name = "dill" +version = "0.4.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/81/e1/56027a71e31b02ddc53c7d65b01e68edf64dea2932122fe7746a516f75d5/dill-0.4.1.tar.gz", hash = "sha256:423092df4182177d4d8ba8290c8a5b640c66ab35ec7da59ccfa00f6fa3eea5fa", size = 187315, upload-time = "2026-01-19T02:36:56.85Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/1e/77/dc8c558f7593132cf8fefec57c4f60c83b16941c574ac5f619abb3ae7933/dill-0.4.1-py3-none-any.whl", hash = "sha256:1e1ce33e978ae97fcfcff5638477032b801c46c7c65cf717f95fbc2248f79a9d", size = 120019, upload-time = "2026-01-19T02:36:55.663Z" }, +] + +[[package]] +name = "execnet" +version = "2.1.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/bf/89/780e11f9588d9e7128a3f87788354c7946a9cbb1401ad38a48c4db9a4f07/execnet-2.1.2.tar.gz", hash = "sha256:63d83bfdd9a23e35b9c6a3261412324f964c2ec8dcd8d3c6916ee9373e0befcd", size = 166622, upload-time = "2025-11-12T09:56:37.75Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/ab/84/02fc1827e8cdded4aa65baef11296a9bbe595c474f0d6d758af082d849fd/execnet-2.1.2-py3-none-any.whl", hash = "sha256:67fba928dd5a544b783f6056f449e5e3931a5c378b128bc18501f7ea79e296ec", size = 40708, upload-time = "2025-11-12T09:56:36.333Z" }, +] + +[[package]] +name = "fastapi" +version = "0.149.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "annotated-doc" }, + { name = "opentelemetry-api" }, + { name = "pydantic" }, + { name = "starlette" }, + { name = "typing-extensions" }, + { name = "typing-inspection" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b9/37/6e7ec4dbe12d9721902066ddb83dd9c6e7adb1cee81e094502c43024e4d1/fastapi-0.149.0.tar.gz", hash = "sha256:aba5779358d0c5233759ddcddd84fe7269d71f775fd754b653b608f770558a41", size = 483369, upload-time = "2026-10-19T17:22:29.495Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/20/6c/78c324bd6725f69ebbac46e0d6a9a59744e108cececac4c1c8500c567512/fastapi-0.149.0-py3-none-any.whl", hash = "sha256:fc4a5cf7e14f607a48eb1ab98147e9140943e88ccd0c8791d72c0a89b1159a35", size = 145182, upload-time = "2026-10-19T17:22:27.753Z" }, +] + +[[package]] +name = "filelock" +version = "4.2.4" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d8/28/3893324bcadf8f89b1616637468d766af3f625008c0735bf704fdfa86617/filelock-4.2.4.tar.gz", hash = "sha256:c6d8663535e1650ee430507b4ffbac7b464dd64b7647ea0112a9f406499d163d", size = 652444, upload-time = "2026-10-19T01:57:51.001Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e0/1b/5e74289a1878e666bb4b65fe5805a04cafcac2aa5f519ef1ead67ef91c56/filelock-4.2.4-py3-none-any.whl", hash = "sha256:301241438c57936ef93f4243a94f2559022268bfcf9b375a9a72eb0f8db8358e", size = 162475, upload-time = "2026-10-19T01:57:49.419Z" }, +] + +[[package]] +name = "fire" +version = "0.7.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "termcolor" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/c0/00/f8d10588d2019d6d6452653def1ee807353b21983db48550318424b5ff18/fire-0.7.1.tar.gz", hash = "sha256:3b208f05c736de98fb343310d090dcc4d8c78b2a89ea4f32b837c586270a9cbf", size = 88720, upload-time = "2025-08-16T20:20:24.175Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e5/4c/93d0f85318da65923e4b91c1c2ff03d8a458cbefebe3bc612a6693c7906d/fire-0.7.1-py3-none-any.whl", hash = "sha256:e43fd8a5033a9001e7e2973bab96070694b9f12f2e0ecf96d4683971b5ab1882", size = 115945, upload-time = "2025-08-16T20:20:22.87Z" }, +] + +[[package]] +name = "frozenlist" +version = "1.8.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/2d/f5/c831fac6cc817d26fd54c7eaccd04ef7e0288806943f7cc5bbf69f3ac1f0/frozenlist-1.8.0.tar.gz", hash = "sha256:3ede829ed8d842f6cd48fc7081d7a41001a56f1f38603f9d49bf3020d59a31ad", size = 45875, upload-time = "2025-10-06T05:38:17.865Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/69/29/948b9aa87e75820a38650af445d2ef2b6b8a6fab1a23b6bb9e4ef0be2d59/frozenlist-1.8.0-cp312-cp312-macosx_10_13_universal2.whl", hash = "sha256:78f7b9e5d6f2fdb88cdde9440dc147259b62b9d3b019924def9f6478be254ac1", size = 87782, upload-time = "2025-10-06T05:36:06.649Z" }, + { url = "https://files.pythonhosted.org/packages/64/80/4f6e318ee2a7c0750ed724fa33a4bdf1eacdc5a39a7a24e818a773cd91af/frozenlist-1.8.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:229bf37d2e4acdaf808fd3f06e854a4a7a3661e871b10dc1f8f1896a3b05f18b", size = 50594, upload-time = "2025-10-06T05:36:07.69Z" }, + { url = "https://files.pythonhosted.org/packages/2b/94/5c8a2b50a496b11dd519f4a24cb5496cf125681dd99e94c604ccdea9419a/frozenlist-1.8.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:f833670942247a14eafbb675458b4e61c82e002a148f49e68257b79296e865c4", size = 50448, upload-time = "2025-10-06T05:36:08.78Z" }, + { url = "https://files.pythonhosted.org/packages/6a/bd/d91c5e39f490a49df14320f4e8c80161cfcce09f1e2cde1edd16a551abb3/frozenlist-1.8.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:494a5952b1c597ba44e0e78113a7266e656b9794eec897b19ead706bd7074383", size = 242411, upload-time = "2025-10-06T05:36:09.801Z" }, + { url = "https://files.pythonhosted.org/packages/8f/83/f61505a05109ef3293dfb1ff594d13d64a2324ac3482be2cedc2be818256/frozenlist-1.8.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:96f423a119f4777a4a056b66ce11527366a8bb92f54e541ade21f2374433f6d4", size = 243014, upload-time = "2025-10-06T05:36:11.394Z" }, + { url = "https://files.pythonhosted.org/packages/d8/cb/cb6c7b0f7d4023ddda30cf56b8b17494eb3a79e3fda666bf735f63118b35/frozenlist-1.8.0-cp312-cp312-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:3462dd9475af2025c31cc61be6652dfa25cbfb56cbbf52f4ccfe029f38decaf8", size = 234909, upload-time = "2025-10-06T05:36:12.598Z" }, + { url = "https://files.pythonhosted.org/packages/31/c5/cd7a1f3b8b34af009fb17d4123c5a778b44ae2804e3ad6b86204255f9ec5/frozenlist-1.8.0-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:c4c800524c9cd9bac5166cd6f55285957fcfc907db323e193f2afcd4d9abd69b", size = 250049, upload-time = "2025-10-06T05:36:14.065Z" }, + { url = "https://files.pythonhosted.org/packages/c0/01/2f95d3b416c584a1e7f0e1d6d31998c4a795f7544069ee2e0962a4b60740/frozenlist-1.8.0-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:d6a5df73acd3399d893dafc71663ad22534b5aa4f94e8a2fabfe856c3c1b6a52", size = 256485, upload-time = "2025-10-06T05:36:15.39Z" }, + { url = "https://files.pythonhosted.org/packages/ce/03/024bf7720b3abaebcff6d0793d73c154237b85bdf67b7ed55e5e9596dc9a/frozenlist-1.8.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:405e8fe955c2280ce66428b3ca55e12b3c4e9c336fb2103a4937e891c69a4a29", size = 237619, upload-time = "2025-10-06T05:36:16.558Z" }, + { url = "https://files.pythonhosted.org/packages/69/fa/f8abdfe7d76b731f5d8bd217827cf6764d4f1d9763407e42717b4bed50a0/frozenlist-1.8.0-cp312-cp312-musllinux_1_2_armv7l.whl", hash = "sha256:908bd3f6439f2fef9e85031b59fd4f1297af54415fb60e4254a95f75b3cab3f3", size = 250320, upload-time = "2025-10-06T05:36:17.821Z" }, + { url = "https://files.pythonhosted.org/packages/f5/3c/b051329f718b463b22613e269ad72138cc256c540f78a6de89452803a47d/frozenlist-1.8.0-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:294e487f9ec720bd8ffcebc99d575f7eff3568a08a253d1ee1a0378754b74143", size = 246820, upload-time = "2025-10-06T05:36:19.046Z" }, + { url = "https://files.pythonhosted.org/packages/0f/ae/58282e8f98e444b3f4dd42448ff36fa38bef29e40d40f330b22e7108f565/frozenlist-1.8.0-cp312-cp312-musllinux_1_2_s390x.whl", hash = "sha256:74c51543498289c0c43656701be6b077f4b265868fa7f8a8859c197006efb608", size = 250518, upload-time = "2025-10-06T05:36:20.763Z" }, + { url = "https://files.pythonhosted.org/packages/8f/96/007e5944694d66123183845a106547a15944fbbb7154788cbf7272789536/frozenlist-1.8.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:776f352e8329135506a1d6bf16ac3f87bc25b28e765949282dcc627af36123aa", size = 239096, upload-time = "2025-10-06T05:36:22.129Z" }, + { url = "https://files.pythonhosted.org/packages/66/bb/852b9d6db2fa40be96f29c0d1205c306288f0684df8fd26ca1951d461a56/frozenlist-1.8.0-cp312-cp312-win32.whl", hash = "sha256:433403ae80709741ce34038da08511d4a77062aa924baf411ef73d1146e74faf", size = 39985, upload-time = "2025-10-06T05:36:23.661Z" }, + { url = "https://files.pythonhosted.org/packages/b8/af/38e51a553dd66eb064cdf193841f16f077585d4d28394c2fa6235cb41765/frozenlist-1.8.0-cp312-cp312-win_amd64.whl", hash = "sha256:34187385b08f866104f0c0617404c8eb08165ab1272e884abc89c112e9c00746", size = 44591, upload-time = "2025-10-06T05:36:24.958Z" }, + { url = "https://files.pythonhosted.org/packages/a7/06/1dc65480ab147339fecc70797e9c2f69d9cea9cf38934ce08df070fdb9cb/frozenlist-1.8.0-cp312-cp312-win_arm64.whl", hash = "sha256:fe3c58d2f5db5fbd18c2987cba06d51b0529f52bc3a6cdc33d3f4eab725104bd", size = 40102, upload-time = "2025-10-06T05:36:26.333Z" }, + { url = "https://files.pythonhosted.org/packages/9a/9a/e35b4a917281c0b8419d4207f4334c8e8c5dbf4f3f5f9ada73958d937dcc/frozenlist-1.8.0-py3-none-any.whl", hash = "sha256:0c18a16eab41e82c295618a77502e17b195883241c563b00f0aa5106fc4eaa0d", size = 13409, upload-time = "2025-10-06T05:38:16.721Z" }, +] + +[[package]] +name = "fsspec" +version = "2026.7.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/00/78/f34251dadb8f3921264a1d9b8946f5e542014ee2614b285261b4e40e6775/fsspec-2026.7.0.tar.gz", hash = "sha256:c803c40f4cf860b49dea58ee3e1c33cb9c790520e233537e1340049f89b82a88", size = 317040, upload-time = "2026-07-28T16:34:51.052Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/fd/3c/6a2bf344106328fd04963664a60b9bb6496fc25df8e962fcdc1367285fb9/fsspec-2026.7.0-py3-none-any.whl", hash = "sha256:b57ddbafedfaef7018c1ecab32aa200a9d7ca26b77965f64e48b70061249d279", size = 206583, upload-time = "2026-07-28T16:34:49.538Z" }, +] + +[package.optional-dependencies] +http = [ + { name = "aiohttp" }, +] + +[[package]] +name = "grpclib" +version = "0.4.9" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "h2" }, + { name = "multidict" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/5b/28/5a2c299ec82a876a252c5919aa895a6f1d1d35c96417c5ce4a4660dc3a80/grpclib-0.4.9.tar.gz", hash = "sha256:cc589c330fa81004c6400a52a566407574498cb5b055fa927013361e21466c46", size = 84798, upload-time = "2025-12-14T22:23:14.349Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/5c/90/b0cbbd9efcc82816c58f31a34963071aa19fb792a212a5d9caf8e0fc3097/grpclib-0.4.9-py3-none-any.whl", hash = "sha256:7762ec1c8ed94dfad597475152dd35cbd11aecaaca2f243e29702435ca24cf0e", size = 77063, upload-time = "2025-12-14T22:23:13.224Z" }, +] + +[[package]] +name = "h11" +version = "0.16.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/01/ee/02a2c011bdab74c6fb3c75474d40b3052059d95df7e73351460c8588d963/h11-0.16.0.tar.gz", hash = "sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1", size = 101250, upload-time = "2025-04-24T03:35:25.427Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/04/4b/29cac41a4d98d144bf5f6d33995617b185d14b22401f75ca86f384e87ff1/h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86", size = 37515, upload-time = "2025-04-24T03:35:24.344Z" }, +] + +[[package]] +name = "h2" +version = "4.4.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "hpack" }, + { name = "hyperframe" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/e7/85/7c366e69d84c17bb778fe41419e1fbcce3033d5b7ce29bbffff0a98b859f/h2-4.4.1.tar.gz", hash = "sha256:4e866ffb1a869ae14dd9b5e6beb5c24a13da0495ad72b65925ded182521c1516", size = 2157281, upload-time = "2026-08-03T11:45:09.509Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7e/22/e85faf23bd72a92d1921e37d674ca56eb298a3c8be31fdecef0ff2b3aaac/h2-4.4.1-py3-none-any.whl", hash = "sha256:0e25f1462b23c9cb82d9eb02e28bc706dac2a68cb457c6a0d74d63c8a2a5d0e6", size = 62636, upload-time = "2026-08-03T11:44:59.164Z" }, +] + +[[package]] +name = "hf-xet" +version = "1.7.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/9e/27/06d899ea7bd721d272f84aac98bdb238de98af4cc767a69056d967d68c71/hf_xet-1.7.0.tar.gz", hash = "sha256:d406ec79053c0871817f700c2ac8c36ba0d87f9c34b7458b0f0063bb218b0466", size = 985689, upload-time = "2026-10-06T20:18:43.89Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/9c/0b/b03be21ffaada749ba0d3197d8aefbf1aa698bac149580421c15239b299e/hf_xet-1.7.0-cp38-abi3-macosx_10_12_x86_64.whl", hash = "sha256:e3e88a7a75d7d95cbee1f37dc31341d6201124cf21c6c4b1dfab8ccba9b09e0f", size = 3796096, upload-time = "2026-10-06T20:18:28.43Z" }, + { url = "https://files.pythonhosted.org/packages/c3/47/a26ebdce7056a61e931f228439bc0ab08cbec239d1690f965e5e637cba79/hf_xet-1.7.0-cp38-abi3-macosx_11_0_arm64.whl", hash = "sha256:59fba37039233c7fcbe196817d6cdcf1b40dfb17b410f229d85b0cf0a1848da4", size = 3560352, upload-time = "2026-10-06T20:18:30.365Z" }, + { url = "https://files.pythonhosted.org/packages/a3/4c/2bf3b66c215d409655f28de1622393dde04c9461280d48c7924bb3b2decd/hf_xet-1.7.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:2814a6e999d13464c4d679b788cc5d784eb5a4edfc638a31f10e9a11ab531ef8", size = 4212180, upload-time = "2026-10-06T20:18:32.292Z" }, + { url = "https://files.pythonhosted.org/packages/49/0c/a2f703a5a78267556e89e03316fa0805c86b72b50829bc67665746e8ebf0/hf_xet-1.7.0-cp38-abi3-manylinux_2_28_aarch64.whl", hash = "sha256:fcfd6c22418e57dd5b3aea649e813b2e2cfb2aebf317b210d90f1fe4b3018b52", size = 3990011, upload-time = "2026-10-06T20:18:34.21Z" }, + { url = "https://files.pythonhosted.org/packages/a4/77/e52e4201b1cbf571530a61cc57f70182045a39a230089ee5f1df182a4de2/hf_xet-1.7.0-cp38-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:80f79dae613ce9e0ea1fd1ae15616ca9ac74aed4c770aabc199c4f03ebecc863", size = 4190628, upload-time = "2026-10-06T20:18:36.062Z" }, + { url = "https://files.pythonhosted.org/packages/6c/dc/03a21b89f118664a0926ff25b0f8e44a519bf22724a6a8fc7a9abbc188b6/hf_xet-1.7.0-cp38-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:0a9e802f33bf50c851abe45fc5380e61f959e2d369647d6742b79ad9d6c27cab", size = 4418814, upload-time = "2026-10-06T20:18:37.888Z" }, + { url = "https://files.pythonhosted.org/packages/4d/59/b35106dfa71b6eef605dc88bd038fe99c7f86fb132a15b60d0bf2f235b2c/hf_xet-1.7.0-cp38-abi3-win_amd64.whl", hash = "sha256:2b7bb5727889b0f2436dbaaad8fc4c3e66b8240d992716989e0c086b4278b1bc", size = 3822644, upload-time = "2026-10-06T20:18:40.052Z" }, + { url = "https://files.pythonhosted.org/packages/48/cd/072313585f74fe9d441e2eb5e0a4703c30586cd709810ea369675f61b74e/hf_xet-1.7.0-cp38-abi3-win_arm64.whl", hash = "sha256:acc3851cf2576a8fb2ae926da863f4efabe21303cf292e9a44332802ab0dcc6a", size = 3662436, upload-time = "2026-10-06T20:18:42.205Z" }, +] + +[[package]] +name = "hpack" +version = "4.2.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/26/5b/fcabf6028144a8723726318b07a32c2f3314acdff6265743cf08a344b18e/hpack-4.2.0.tar.gz", hash = "sha256:0895cfa3b5531fc65fe439c05eb65144f123bf7a394fcaa56aa423548d8e45c0", size = 51300, upload-time = "2026-06-23T18:34:46.667Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/71/b4/4a9fcfb2aef6ba44d9073ecd301443aa00b3dac95de5619f2a7de7ec8a91/hpack-4.2.0-py3-none-any.whl", hash = "sha256:858ac0b02280fa582b5080d68db0899c62a80375e0e5413a74970c5e518b6986", size = 34246, upload-time = "2026-06-23T18:34:45.472Z" }, +] + +[[package]] +name = "httpcore" +version = "1.0.9" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "certifi" }, + { name = "h11" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/06/94/82699a10bca87a5556c9c59b5963f2d039dbd239f25bc2a63907a05a14cb/httpcore-1.0.9.tar.gz", hash = "sha256:6e34463af53fd2ab5d807f399a9b45ea31c3dfa2276f15a2c3f00afff6e176e8", size = 85484, upload-time = "2025-04-24T22:06:22.219Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7e/f5/f66802a942d491edb555dd61e3a9961140fd64c90bce1eafd741609d334d/httpcore-1.0.9-py3-none-any.whl", hash = "sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55", size = 78784, upload-time = "2025-04-24T22:06:20.566Z" }, +] + +[[package]] +name = "httpcore2" +version = "2.15.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "h11", marker = "sys_platform != 'emscripten'" }, + { name = "truststore", marker = "sys_platform != 'emscripten'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/c2/a9/11bc39967bb9dfaab0981ab4d49f519818acbee662a7e76d06813319c838/httpcore2-2.15.1.tar.gz", hash = "sha256:a94b6b3e6260e1a3f035915408d3dcd7dc8e844c2b544b642e227cf98a747af0", size = 69859, upload-time = "2026-10-19T20:14:23.824Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/0d/9b/e2b556e26fbc67716ab1dfc18832eb6fe196d88363f7094cb1da3d7c0d3e/httpcore2-2.15.1-py3-none-any.whl", hash = "sha256:730f603058c260a3288378217b464713350c23e3b8fe8bba753701054032d98a", size = 85055, upload-time = "2026-10-19T20:14:21.363Z" }, +] + +[[package]] +name = "httpx" +version = "0.28.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio" }, + { name = "certifi" }, + { name = "httpcore" }, + { name = "idna" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b1/df/48c586a5fe32a0f01324ee087459e112ebb7224f646c0b5023f5e79e9956/httpx-0.28.1.tar.gz", hash = "sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc", size = 141406, upload-time = "2024-12-06T15:37:23.222Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/2a/39/e50c7c3a983047577ee07d2a9e53faf5a69493943ec3f6a384bdc792deb2/httpx-0.28.1-py3-none-any.whl", hash = "sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad", size = 73517, upload-time = "2024-12-06T15:37:21.509Z" }, +] + +[[package]] +name = "httpx2" +version = "2.15.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio", marker = "sys_platform != 'emscripten'" }, + { name = "httpcore2", marker = "sys_platform != 'emscripten'" }, + { name = "httpx2-jsfetch", marker = "sys_platform == 'emscripten'" }, + { name = "idna" }, + { name = "truststore", marker = "sys_platform != 'emscripten'" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/7f/85/07978f4afd168d6b408fa75c1dc13e3010653a81e182aa1b6d89ab94d9c6/httpx2-2.15.1.tar.gz", hash = "sha256:ff75485308fbe441e75b1218546e4e78d884df41e1a9741c1f79335ed8fcfef7", size = 101673, upload-time = "2026-10-19T20:14:24.587Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7a/bf/56068f774620330c8330e4b4915fae889a5b2d4b52667b3b7c8f66c4a9ad/httpx2-2.15.1-py3-none-any.whl", hash = "sha256:75841cd83d16b417f7d6da8f2089ce8120806efdbf96af37d46c5129c49015be", size = 96411, upload-time = "2026-10-19T20:14:22.789Z" }, +] + +[[package]] +name = "httpx2-jsfetch" +version = "1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/cd/c4/0e5636363151a2a1795e0a77617168b9ca438e1748ec05fc9b5687f93d64/httpx2_jsfetch-1.0.tar.gz", hash = "sha256:70a0e3eabfef7cce5ad9c629f7d01ca05e418f586646f4ddf14782e4c1454c60", size = 6872, upload-time = "2026-08-07T00:13:07.492Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/9b/43/832f631d32e4f1211caa2ba368317739fe71f0b8530e4c9d15dc454bac2a/httpx2_jsfetch-1.0-py3-none-any.whl", hash = "sha256:cb916b707601e69a07721aabc8f3f6659be3a6893bc1ff5c6f9e02241df2da32", size = 6382, upload-time = "2026-08-07T00:13:06.567Z" }, +] + +[[package]] +name = "huggingface-hub" +version = "2.2.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "click" }, + { name = "filelock" }, + { name = "fsspec" }, + { name = "hf-xet", marker = "platform_machine == 'AMD64' or platform_machine == 'ARM64' or platform_machine == 'aarch64' or platform_machine == 'amd64' or platform_machine == 'arm64' or platform_machine == 'x86_64'" }, + { name = "httpx2" }, + { name = "packaging" }, + { name = "pyyaml" }, + { name = "tqdm" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/12/47/6858d63643e66fb4f6585c3cfd4029c0b2bc1ae21688cee9b3335f20a10d/huggingface_hub-2.2.0.tar.gz", hash = "sha256:5d1b47537394e4215cb858aa12fd493d0f7ef7f58990f5dcd24bc173107b2871", size = 1041026, upload-time = "2026-10-08T15:30:59.971Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/eb/b0/0f7b430fd100b3a3b037fdbb314878200241082e607b3383c63d91a13a72/huggingface_hub-2.2.0-py3-none-any.whl", hash = "sha256:1667f145dc56dc210d60966069397df9ecfca9607a5d43db88b308c89dae56b3", size = 839884, upload-time = "2026-10-08T15:30:57.914Z" }, +] + +[[package]] +name = "hyperframe" +version = "6.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/02/e7/94f8232d4a74cc99514c13a9f995811485a6903d48e5d952771ef6322e30/hyperframe-6.1.0.tar.gz", hash = "sha256:f630908a00854a7adeabd6382b43923a4c4cd4b821fcb527e6ab9e15382a3b08", size = 26566, upload-time = "2025-01-22T21:41:49.302Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/48/30/47d0bf6072f7252e6521f3447ccfa40b421b6824517f82854703d0f5a98b/hyperframe-6.1.0-py3-none-any.whl", hash = "sha256:b03380493a519fce58ea5af42e4a42317bf9bd425596f7a0835ffce80f1a42e5", size = 13007, upload-time = "2025-01-22T21:41:47.295Z" }, +] + +[[package]] +name = "idna" +version = "3.20" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f5/08/8eea9d4b8302028f3abb2c0813953f7aec26d33b7a8960ed760e65ff29fa/idna-3.20.tar.gz", hash = "sha256:a7db850025b95ded1eae8a46181a1a6c56c92c96f0e2b005d9ff8dc0210cab44", size = 216463, upload-time = "2026-09-17T14:11:04.752Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/58/a2/bb081bab032533a855d44de1d56f8e8426114ff1ba5d1f07a438a0a654f8/idna-3.20-py3-none-any.whl", hash = "sha256:ab7ae7122974553370f0bdb919e1a960b2cd1bc1ef0276416d896db81c14582c", size = 69583, upload-time = "2026-09-17T14:11:03.168Z" }, +] + +[[package]] +name = "iniconfig" +version = "2.3.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/01/e1/2069291243c926a2ff1cd706c7f3eeb9b62144bf60f77c9fb9ff2fb26bd3/iniconfig-2.3.1.tar.gz", hash = "sha256:67f4b9c50da0dedf52af349e7749a80a9057a5031199791b906c3bb3ae878960", size = 21209, upload-time = "2026-10-06T22:48:38.076Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/56/43/4ca9e49d27a1fcf6bece6f6aec0ea46bb9112489b93d4b688fb415457bdb/iniconfig-2.3.1-py3-none-any.whl", hash = "sha256:9121e2c1fdb355232495be3194c8dfe87ccc2d5dee45947b78e68f499790d7a7", size = 7552, upload-time = "2026-10-06T22:48:36.959Z" }, +] + +[[package]] +name = "markdown-it-py" +version = "4.2.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "mdurl" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/06/ff/7841249c247aa650a76b9ee4bbaeae59370dc8bfd2f6c01f3630c35eb134/markdown_it_py-4.2.0.tar.gz", hash = "sha256:04a21681d6fbb623de53f6f364d352309d4094dd4194040a10fd51833e418d49", size = 82454, upload-time = "2026-05-07T12:08:28.36Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/b3/81/4da04ced5a082363ecfa159c010d200ecbd959ae410c10c0264a38cac0f5/markdown_it_py-4.2.0-py3-none-any.whl", hash = "sha256:9f7ebbcd14fe59494226453aed97c1070d83f8d24b6fc3a3bcf9a38092641c4a", size = 91687, upload-time = "2026-05-07T12:08:27.182Z" }, +] + +[[package]] +name = "mdurl" +version = "0.1.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d6/54/cfe61301667036ec958cb99bd3efefba235e65cdeb9c84d24a8293ba1d90/mdurl-0.1.2.tar.gz", hash = "sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba", size = 8729, upload-time = "2022-08-14T12:40:10.846Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/b3/38/89ba8ad64ae25be8de66a6d463314cf1eb366222074cfda9ee839c56a4b4/mdurl-0.1.2-py3-none-any.whl", hash = "sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8", size = 9979, upload-time = "2022-08-14T12:40:09.779Z" }, +] + +[[package]] +name = "modal" +version = "1.6.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "aiohttp" }, + { name = "cbor2" }, + { name = "certifi" }, + { name = "click" }, + { name = "grpclib" }, + { name = "protobuf" }, + { name = "rich" }, + { name = "synchronicity" }, + { name = "toml" }, + { name = "types-certifi" }, + { name = "types-toml" }, + { name = "typing-extensions" }, + { name = "watchfiles" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/65/ba/2b36899ea5633bf101e6ba6f7e95a4da3e4b1f57f52bd0ba8f4cfd12e808/modal-1.6.1.tar.gz", hash = "sha256:ff17768f67a65595aa7882e893e6cb78e2e001b3653aa52cfe98d4051d8d9e9e", size = 942670, upload-time = "2026-10-03T16:05:05.428Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d2/3c/6b7d9a15dab9af2833ce674bbc72b5654d4099d5b088f1cc1eda3f8249ca/modal-1.6.1-py3-none-any.whl", hash = "sha256:f408ef88563003a83e491a6be09d4221eaa6e038fa4faa2ad8b15b25afe949a4", size = 1063454, upload-time = "2026-10-03T16:05:02.554Z" }, +] + +[[package]] +name = "modal-dojo" +version = "0.1.0" +source = { editable = ".dojo" } +dependencies = [ + { name = "click" }, + { name = "cloudpickle" }, + { name = "datasets" }, + { name = "fastapi" }, + { name = "httpx" }, + { name = "modal" }, + { name = "pydantic" }, + { name = "randomname" }, + { name = "rich" }, + { name = "transformers" }, +] + +[package.metadata] +requires-dist = [ + { name = "click", specifier = ">=8.2,<9" }, + { name = "cloudpickle" }, + { name = "datasets" }, + { name = "fastapi" }, + { name = "httpx" }, + { name = "modal", specifier = "==1.6.1" }, + { name = "pydantic" }, + { name = "randomname" }, + { name = "rich" }, + { name = "transformers", specifier = ">=5.12.1" }, +] + +[package.metadata.requires-dev] +ci = [{ name = "pygithub", specifier = ">=2.9.1" }] +dev = [ + { name = "ipykernel", specifier = ">=6.29.0" }, + { name = "pre-commit", specifier = ">=4.5.1" }, + { name = "pytest", specifier = ">=9.0.3" }, + { name = "pyyaml", specifier = ">=6.0.3" }, + { name = "ruff", specifier = ">=0.15.17" }, + { name = "uv", specifier = ">=0.11.0" }, +] +docs = [ + { name = "mkdocs-gen-files", specifier = ">=0.6.1" }, + { name = "mkdocs-material", specifier = ">=9.7.6" }, + { name = "properdocs", specifier = ">=1.6.7" }, + { name = "pymdown-extensions", specifier = ">=10.21.2" }, +] + +[[package]] +name = "multidict" +version = "7.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f9/79/84ddb5ba16c4eb2c69c71db76ae3c579fe546e511f7170c7e27eedbab7c1/multidict-7.1.0.tar.gz", hash = "sha256:61a4e5d81b8d4e4ad61964b230129e7a2b914793d96289029078fc9009f074ec", size = 362341, upload-time = "2026-10-09T20:31:38.279Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/03/e1/215e7df354e2907f3af9d910c8136653cfec94796012a776359bc802a326/multidict-7.1.0-cp312-cp312-macosx_10_13_universal2.whl", hash = "sha256:ccfb950359a80de0fcd2030ad60ac1b1a861462de3e2ef746697c9256659af21", size = 165426, upload-time = "2026-10-09T13:59:50.564Z" }, + { url = "https://files.pythonhosted.org/packages/a4/ec/461ba588b308ada2cd16907d6ea425ca4d417596b88c12814ad9a0bb7325/multidict-7.1.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:939d8cd2d8c35e3956f6bc858390b6ccb611e6152b4920d64ab5e98f3fcf39e4", size = 95943, upload-time = "2026-10-09T13:59:52.381Z" }, + { url = "https://files.pythonhosted.org/packages/c9/84/31444ef07ec13a33c42c2772d986129e137e69c4beb89ca64f2138988145/multidict-7.1.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:f8e95c95039eab6a2dad8c83c38ab87fc5431d28849e0c8a7e2a4e70ba38710d", size = 98294, upload-time = "2026-10-09T13:59:54.066Z" }, + { url = "https://files.pythonhosted.org/packages/dc/10/aca13806d73d88b5b01e35e828e23a346cb1abad710a49dff0507702efc9/multidict-7.1.0-cp312-cp312-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:05d12b4bac53abe0c65f3163af2b45894e2e1c0cc55493ac784d52a350047d88", size = 453301, upload-time = "2026-10-09T13:59:55.397Z" }, + { url = "https://files.pythonhosted.org/packages/96/8c/382d771bfb3a9282d98332d0e1f27fd1f8b4ae0e7175bd7e008ebf934900/multidict-7.1.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:0e79ed92b1dece6bb57e9b46effd74d7a5d3d00187c85466d880ed184239a698", size = 478570, upload-time = "2026-10-09T13:59:56.899Z" }, + { url = "https://files.pythonhosted.org/packages/52/9c/e81b0c92449da3a1950a575c520d957d7be618777d170717a71e00558d7f/multidict-7.1.0-cp312-cp312-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:fab380fcff8b3555eb2bd04304fa4330909a771a9a9b0dc07666cfc23148a711", size = 443699, upload-time = "2026-10-09T13:59:58.465Z" }, + { url = "https://files.pythonhosted.org/packages/63/72/f8f5fee6960d1b580a7b046c7c5abccf67aa26fa5647927a91c9c835c2f8/multidict-7.1.0-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:4b5c41e44da74383c924cc5d75ef0a268f301d69305b3c42bd17af685d55e412", size = 497332, upload-time = "2026-10-09T14:08:01.88Z" }, + { url = "https://files.pythonhosted.org/packages/e2/92/a25d7db3ca451b588e743e067ee80f2b475edf6467a56908454faf6a714c/multidict-7.1.0-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:3dbaa7f7c2f0ca8578895fc61fb8c8e50ebb405dad8982f92f4343285c7a3fda", size = 512457, upload-time = "2026-10-09T14:08:25.465Z" }, + { url = "https://files.pythonhosted.org/packages/7a/f3/374c0ab122bb98b1a62a8742b1e3f59563e4d609942005a4041861a0df67/multidict-7.1.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:fed6b7705d49dd07e5e0dd5f5c873fc44047e92d714299b13245b5fecac49d01", size = 483000, upload-time = "2026-10-09T14:08:27.132Z" }, + { url = "https://files.pythonhosted.org/packages/46/1f/01c8522859771dc3cee840d5852233fe84c91aa982a1cd8ad594306d25cc/multidict-7.1.0-cp312-cp312-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:53daa47dd176db64bb35170e3d5d0ae2388c060121201883696278f055a0e70c", size = 430746, upload-time = "2026-10-09T14:08:28.715Z" }, + { url = "https://files.pythonhosted.org/packages/ab/f3/af90affc8cca6b4ee59b2ec354a20839005e829d14d155451009810ef1ce/multidict-7.1.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:542429c796430de924d03b68a6173bb6d79d5c4967d4e9a18de3e501cad55593", size = 466859, upload-time = "2026-10-09T20:27:24.139Z" }, + { url = "https://files.pythonhosted.org/packages/f8/97/1b6762f37f6331449e164af9d5500f0abb0f23670e81ba543441e0a6be1d/multidict-7.1.0-cp312-cp312-musllinux_1_2_armv7l.whl", hash = "sha256:c44ca6d3cdf4cfcbcd4f928fdcbe87af5fd7319f6ad4169617b7fd6b4527c33c", size = 441952, upload-time = "2026-10-09T20:27:26.559Z" }, + { url = "https://files.pythonhosted.org/packages/bf/d2/4908177fbf22438799c04ba11a2853a99c69d028fccefe61f19e68caba0c/multidict-7.1.0-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:eb0228c809b2e7eb47921876050af0bc4214b351bad8d8112f70b6ed4288763c", size = 452810, upload-time = "2026-10-09T20:27:28.347Z" }, + { url = "https://files.pythonhosted.org/packages/8d/05/5031f44f680ec54fc182c4d71c9ab7c07216983946aa64ce6fdd56a52692/multidict-7.1.0-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:6ad60de1f4c702448fc8f1449f05e810f6b7957c08a5b3950c8a792dfb13b50a", size = 483934, upload-time = "2026-10-09T20:27:30.14Z" }, + { url = "https://files.pythonhosted.org/packages/da/3b/9b21d107dbe96fa7e6966ff9e5e10b9ac0d2d1c2cf1633098e4c700f865e/multidict-7.1.0-cp312-cp312-musllinux_1_2_riscv64.whl", hash = "sha256:f79def86aee67b5ba01b2565f1610f262bf88ae53c379f93e5fa29c50fe793be", size = 427042, upload-time = "2026-10-09T20:27:32.433Z" }, + { url = "https://files.pythonhosted.org/packages/53/ce/5b01b1041580072866b30e39c6380bff269e72fb5ca41a7f2fb828ede943/multidict-7.1.0-cp312-cp312-musllinux_1_2_s390x.whl", hash = "sha256:248dabb89b5aa90b2f7e43e045f048f7e5392ec77b6446d80853ba7117d7bbdf", size = 492543, upload-time = "2026-10-09T20:27:34.459Z" }, + { url = "https://files.pythonhosted.org/packages/1e/6f/6508a23fcc7b1122e4f18409d7900ffeb3cd020cd080fe98aba3eabf9486/multidict-7.1.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:0747a83e7ae617793181a4763ee8b84863cec5c0bbbde70c4394e4c0276c36de", size = 475796, upload-time = "2026-10-09T20:27:36.289Z" }, + { url = "https://files.pythonhosted.org/packages/8f/b6/ebb6433f4aa55ac1fea4bf11e860e99611640d07cc92d21cb3f35608de22/multidict-7.1.0-cp312-cp312-win32.whl", hash = "sha256:1df055e51fe7491120cc84f3362bd43db186be78d0e4c476acad45e435af9ffb", size = 81250, upload-time = "2026-10-09T20:27:38.229Z" }, + { url = "https://files.pythonhosted.org/packages/61/0a/11240e5e7d2e986e288f4a6090f569ad00225b7c2c513d4096b36643f9ab/multidict-7.1.0-cp312-cp312-win_amd64.whl", hash = "sha256:10202ba98cfb3f7eb60da7ca87a2c458a69b7d0d6e4d4388cd6773ebbce89085", size = 93817, upload-time = "2026-10-09T20:27:39.739Z" }, + { url = "https://files.pythonhosted.org/packages/e9/9b/a04ffd76db7cca2a60d347e839315e1ada63017c6c343fa90a23a5a55433/multidict-7.1.0-cp312-cp312-win_arm64.whl", hash = "sha256:0aa1ba3ff7cdda05a1242490612976b2ae1c90fc6200903ef8f53815dcb35c5d", size = 92312, upload-time = "2026-10-09T20:27:41.285Z" }, + { url = "https://files.pythonhosted.org/packages/d0/86/a3de309c5e28ee85b314d0e3ba0e0dea6fd361c313322a05e67be4656e1e/multidict-7.1.0-py3-none-any.whl", hash = "sha256:d9ef29cfd98e17085b4f91bba8fa1570bec6787d5c52ce653ed33a58785585d0", size = 28087, upload-time = "2026-10-09T20:31:35.945Z" }, +] + +[[package]] +name = "multiprocess" +version = "0.70.19" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "dill" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/a2/f2/e783ac7f2aeeed14e9e12801f22529cc7e6b7ab80928d6dcce4e9f00922d/multiprocess-0.70.19.tar.gz", hash = "sha256:952021e0e6c55a4a9fe4cd787895b86e239a40e76802a789d6305398d3975897", size = 2079989, upload-time = "2026-01-19T06:47:39.744Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e3/45/8004d1e6b9185c1a444d6b55ac5682acf9d98035e54386d967366035a03a/multiprocess-0.70.19-py310-none-any.whl", hash = "sha256:97404393419dcb2a8385910864eedf47a3cadf82c66345b44f036420eb0b5d87", size = 134948, upload-time = "2026-01-19T06:47:32.325Z" }, + { url = "https://files.pythonhosted.org/packages/86/c2/dec9722dc3474c164a0b6bcd9a7ed7da542c98af8cabce05374abab35edd/multiprocess-0.70.19-py311-none-any.whl", hash = "sha256:928851ae7973aea4ce0eaf330bbdafb2e01398a91518d5c8818802845564f45c", size = 144457, upload-time = "2026-01-19T06:47:33.711Z" }, + { url = "https://files.pythonhosted.org/packages/71/70/38998b950a97ea279e6bd657575d22d1a2047256caf707d9a10fbce4f065/multiprocess-0.70.19-py312-none-any.whl", hash = "sha256:3a56c0e85dd5025161bac5ce138dcac1e49174c7d8e74596537e729fd5c53c28", size = 150281, upload-time = "2026-01-19T06:47:35.037Z" }, + { url = "https://files.pythonhosted.org/packages/7e/82/69e539c4c2027f1e1697e09aaa2449243085a0edf81ae2c6341e84d769b6/multiprocess-0.70.19-py39-none-any.whl", hash = "sha256:0d4b4397ed669d371c81dcd1ef33fd384a44d6c3de1bd0ca7ac06d837720d3c5", size = 133477, upload-time = "2026-01-19T06:47:38.619Z" }, +] + +[[package]] +name = "numpy" +version = "2.5.4" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/95/b0/c7453d0b6e2073c3264468b106ee1563750cecc910965e67357e3698c83e/numpy-2.5.4.tar.gz", hash = "sha256:9a94cf751c9ad8ebaa835bcd3d40dacf8534ad086b88c38029b65123c7999d2a", size = 20866315, upload-time = "2026-10-10T20:05:31.422Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d0/97/ba2074e92b7befea137e77ea8471e768bbd87c339b7e8c9f5a931949f977/numpy-2.5.4-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:c6342f54c67093cae5c0227eb0eb772fdb79f2a2c37a6eb278b9909ee06aa356", size = 17001609, upload-time = "2026-10-10T20:02:40.843Z" }, + { url = "https://files.pythonhosted.org/packages/ff/a9/bac826765e971d8e16e2064e9ac7525fd69b40ac17c905033a7f5442023f/numpy-2.5.4-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:b11e8fda06a7d69f15ebf542660b74466c2e51094800c1fb794f47ad4faeef17", size = 12015718, upload-time = "2026-10-10T20:02:43.45Z" }, + { url = "https://files.pythonhosted.org/packages/31/2f/5ea3570fcb8ccd0882bea99436a513b2c85dad8f774a2057849130a8fb99/numpy-2.5.4-cp312-cp312-macosx_14_0_arm64.whl", hash = "sha256:9cb18a327b49c5c337f972b03682f6a49855525faaf3c0d3e9c96cd0fd8880a8", size = 5451717, upload-time = "2026-10-10T20:02:46.169Z" }, + { url = "https://files.pythonhosted.org/packages/34/f2/b4fc1bafca03868220b5eaf729d2f21ebd7d7b151c0f9e144fe212bbca35/numpy-2.5.4-cp312-cp312-macosx_14_0_x86_64.whl", hash = "sha256:aec3fc4b32ff82421274f5d205c559c51c840c8df66a78efd7f3612dd005a26a", size = 6789926, upload-time = "2026-10-10T20:02:48.139Z" }, + { url = "https://files.pythonhosted.org/packages/dc/96/8319e2457ae4333c62c815c7006b869a4f60985c1e01024c2f8c6c040fe5/numpy-2.5.4-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:fe4d21ab149f15e4e6043dfb0de87e6e5f34ac176cde83060e9802981fca2ac2", size = 15695312, upload-time = "2026-10-10T20:02:50.115Z" }, + { url = "https://files.pythonhosted.org/packages/43/a3/c799c62e19c337e6d3770b08e475887fb30ce8477d3c09efca6b2f0228a6/numpy-2.5.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:fbde6962867ee75b48b0ee29b2b9372ec5d617799dbaf38e82dc0596f2f7738a", size = 16727283, upload-time = "2026-10-10T20:02:53.186Z" }, + { url = "https://files.pythonhosted.org/packages/39/6b/3604e53fb00314d0dc1b94ec9125a1484f649c0a17480b1f0f0c7a9d6250/numpy-2.5.4-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:381a7a3d2e65e64c0ec302795ab9dc12bb1e73f150904699c153716177eebdaf", size = 17047890, upload-time = "2026-10-10T20:02:56.038Z" }, + { url = "https://files.pythonhosted.org/packages/4a/7a/e8b58a5289a0d464c52885de47c35a935cdd70c03a4c3ab94a5126416dd0/numpy-2.5.4-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:b89d0aaae2fe498c648f4c4795c084db535af5bd98ef942b2a3681fb74ce8645", size = 18485839, upload-time = "2026-10-10T20:02:59.018Z" }, + { url = "https://files.pythonhosted.org/packages/6f/c9/47094f597015009f310b8c900def59065ef1ff5a6fe7b51fc65ec58ec2c6/numpy-2.5.4-cp312-cp312-win32.whl", hash = "sha256:9968ab7e49b93ac6e1c3b2239732183152c9150f16308d30b66a372cffe3483c", size = 6138936, upload-time = "2026-10-10T20:03:01.626Z" }, + { url = "https://files.pythonhosted.org/packages/12/33/fefe62073dc8acfd0f2b9ed7c003af2f50aa61555e113e6db02b8f79f145/numpy-2.5.4-cp312-cp312-win_amd64.whl", hash = "sha256:a7b1b6353e36a7e50de2973a38d705c88ee93adcf120673cee7f45a4a3fa223a", size = 12573091, upload-time = "2026-10-10T20:03:04.349Z" }, + { url = "https://files.pythonhosted.org/packages/1a/07/161270b0c2eec56e4c905f6d6d22e1b836887b2cb189d3f5820aa588e9dd/numpy-2.5.4-cp312-cp312-win_arm64.whl", hash = "sha256:aa1cce2ff3f8d953de38b76bf44602caeb69f101430208f64a10067f7cb4b1d3", size = 10521630, upload-time = "2026-10-10T20:03:06.767Z" }, +] + +[[package]] +name = "opentelemetry-api" +version = "1.45.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/2e/02/6e0ae9cc61bd3169d401077b507b3ebc344745171e1051ab430be012dcd9/opentelemetry_api-1.45.1.tar.gz", hash = "sha256:aa38ed19bcc084ba42782a73255b3582283eced7ad6dddbd6695189e69adfb75", size = 72804, upload-time = "2026-10-06T17:32:58.133Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/1e/41/f7dcf80b81ee8e71c1a2b59f14208bc723edbd89ed027a73b175abf6348e/opentelemetry_api-1.45.1-py3-none-any.whl", hash = "sha256:b31553efa588ae44bc306f863c785c5333a9ecc091248c6ee68b4b6c87fdedfb", size = 60256, upload-time = "2026-10-06T17:32:33.506Z" }, +] + +[[package]] +name = "packaging" +version = "26.3" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/7d/fa/3944b40b07da9ce895c0e6303a5ab7d53da063554f534556b134a54d6093/packaging-26.3.tar.gz", hash = "sha256:94edc256424af38762eb31306eed28beb9f0efc50a8837492c9d6fd6004aed79", size = 313412, upload-time = "2026-08-04T18:15:28.737Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/63/34/ba1c580383c9eada3711951fef0795c80b829a078d72188184bcab9dd527/packaging-26.3-py3-none-any.whl", hash = "sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c", size = 129956, upload-time = "2026-08-04T18:15:27.159Z" }, +] + +[[package]] +name = "pandas" +version = "3.0.6" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "numpy" }, + { name = "python-dateutil" }, + { name = "tzdata", marker = "sys_platform == 'emscripten' or sys_platform == 'win32'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/e2/17/d7b106e05bfa642e8694451e7d3d759c6a241c5386a5d962e4f66c047e06/pandas-3.0.6.tar.gz", hash = "sha256:66b07ef7315a31bfe1089cd3d71a7de781c9dca986762d0b4fe7c0ef17465d10", size = 4667686, upload-time = "2026-09-17T23:23:18.345Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/77/4c/597d588c055d4373cff19cbbc32d4dd046c7be8fadee957585b5ba9e5b24/pandas-3.0.6-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:7dac2d65e9087e8e7b5a45fe15c4920911a221df061ab629943ce016489145c7", size = 10397411, upload-time = "2026-09-17T23:20:49.465Z" }, + { url = "https://files.pythonhosted.org/packages/18/8f/48907c7c707b61a8e5018c32e1a3f70623209bfb59020a2f6196159d3aa7/pandas-3.0.6-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:9dab635a549e58a053c7b0fa054dc0bd7be22f0ed9a720f4a85d5fb993276172", size = 10051279, upload-time = "2026-09-17T23:20:52.409Z" }, + { url = "https://files.pythonhosted.org/packages/67/fa/613d867c3d9554a61bafdec6f79565c8a3e73235feb52cc4a72ad2e0fa6a/pandas-3.0.6-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:e3dccb584123b399c07562ac4d62543e90ede49ddf8ce3c13ffc64cbe828c281", size = 10303429, upload-time = "2026-09-17T23:20:55.597Z" }, + { url = "https://files.pythonhosted.org/packages/cb/67/0c0f18e38d7f2d2af8c24b3315bc4046e73bbdd4a5540405506671ad0c0d/pandas-3.0.6-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:0704044b676496b8350e023b09f174a26772456c974a2b11c36bebb558c9490d", size = 10788193, upload-time = "2026-09-17T23:20:58.617Z" }, + { url = "https://files.pythonhosted.org/packages/39/53/1b57f3162501fe36687e4870e1b918a6458ca7386b173af75663ace95857/pandas-3.0.6-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:e7c1905ef02c3d6d43d9dbd5b6ccb4da4870a0b0c821bbc103fbdb6f3ad2707b", size = 11323501, upload-time = "2026-09-17T23:21:01.911Z" }, + { url = "https://files.pythonhosted.org/packages/f2/d2/b1182e8d39100369d7f13f4a125a3fb6b096fef112c46d0566c25781ff68/pandas-3.0.6-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:569e114072b24fc4970c12e2b4bab252671668a40b324318903380cab0254c0c", size = 11838173, upload-time = "2026-09-17T23:21:04.85Z" }, + { url = "https://files.pythonhosted.org/packages/c7/33/5b717af24d2f27995e51e0875a269dddd373045216e34cf62e3aa764eaa1/pandas-3.0.6-cp312-cp312-pyemscripten_2024_0_wasm32.whl", hash = "sha256:2a8fc94be2ee5f1d86f97aacd8cc566f81680b6498e76f3007421bb5d98151bf", size = 7118499, upload-time = "2026-09-17T23:21:07.661Z" }, + { url = "https://files.pythonhosted.org/packages/bd/2a/14b3b17cd75cef4a1ee1af4234eb41cc1afe4103b98c2d80e8916abfd42b/pandas-3.0.6-cp312-cp312-win_amd64.whl", hash = "sha256:3ef908d28590b3f42d7070e7ad8f9b34b442b260b7f3c1afb57e0040c58cdb1b", size = 9661439, upload-time = "2026-09-17T23:21:10.959Z" }, + { url = "https://files.pythonhosted.org/packages/3b/11/3d580a604a1e35d69f6676847bd12db7d14344bc677b717f4413e79c5d0d/pandas-3.0.6-cp312-cp312-win_arm64.whl", hash = "sha256:f4e7c52eb108d752e7592268108fd3e98efd76d83a3125cdd06c621c2e44359b", size = 8959806, upload-time = "2026-09-17T23:21:13.851Z" }, +] + +[[package]] +name = "pluggy" +version = "1.7.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/bf/db/7fc19e6f2dc92a966727031389fc2e08b558f0f25eb7403c1119ad4713cd/pluggy-1.7.0.tar.gz", hash = "sha256:d1eaa46ebb595891b860ab086b4d09c8588af65ebd4361b8e8f4bb8920b90ba8", size = 123304, upload-time = "2026-10-15T09:50:58.343Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/40/9e/2b38731e0fc536806f16490e1a12d7f0dc2a1235aa8cc07bcc75416a7daa/pluggy-1.7.0-py3-none-any.whl", hash = "sha256:7dd7b0d8832ba3cb632c306926ded123429211b83641b35dc5c41ad2d34f9bec", size = 27082, upload-time = "2026-10-15T09:50:56.808Z" }, +] + +[[package]] +name = "propcache" +version = "0.5.4" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/b3/9a/9fbf4e4ec0c2d7f1c32519fff782ef467859b8faa9fbc5331a96f6395d43/propcache-0.5.4.tar.gz", hash = "sha256:ff6b113f50bc066a698db5d944d2c6dc7507168dd3341e255a8892fd0715a558", size = 61545, upload-time = "2026-09-16T00:17:14.386Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/71/cd/348d58f142aebc4873345c6b31087629182ca6e0f2b3caeaa528cf882eba/propcache-0.5.4-cp312-cp312-macosx_10_13_universal2.whl", hash = "sha256:b28f41fa3b8c6900457f858ec5b03998f3a6d535fbc1bb2edec5961ea05ec429", size = 87285, upload-time = "2026-09-16T00:14:29.362Z" }, + { url = "https://files.pythonhosted.org/packages/df/f4/f3ffaee281b276da854ac1d7a6a506d26cbc62ea2e623756f1d0a4a1ba1a/propcache-0.5.4-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:dcbf346a318a5e30063f547630b02bb787ce2f45b6368d5da143660b6a3835d8", size = 50984, upload-time = "2026-09-16T00:14:30.473Z" }, + { url = "https://files.pythonhosted.org/packages/25/88/1d7df7201750b37765ef2b23bc1c526c028dadde80afa0f57a118fc01182/propcache-0.5.4-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:87a3caecf8095e48dc72f84bfa42e23a848cf410cc9cc13031fba4869b706a21", size = 52460, upload-time = "2026-09-16T00:14:31.692Z" }, + { url = "https://files.pythonhosted.org/packages/83/4f/48865bd02a16ee5236bc46166b2946f37b93e07b0eae355dac0be0b216ca/propcache-0.5.4-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:60a64cbccaa11b7760ce705a14ada17ba459e7ca9f23ba587eb013821032d7ef", size = 251768, upload-time = "2026-09-16T00:14:32.908Z" }, + { url = "https://files.pythonhosted.org/packages/b0/19/3742a5eed62317b03b4002ee865dc9fd720308bdd0da1f29a5786c630311/propcache-0.5.4-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:a74bfa37147cc08fb29df10bd9c16f40fa7f860cd3a6d2fff853323a94f6e17f", size = 257723, upload-time = "2026-09-16T00:14:34.267Z" }, + { url = "https://files.pythonhosted.org/packages/cb/d5/ee6350fb0be9122bb6c67082a876d34b90d980d100c106af4b81023e04f4/propcache-0.5.4-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:a4d7a54719b67338a305dca2ce6aafe366817df94ddfd4b5514374356f5ca546", size = 265597, upload-time = "2026-09-16T00:14:35.56Z" }, + { url = "https://files.pythonhosted.org/packages/85/9f/83a07b6ec0e043c050cfdd35fb0cf1b7897b91d554d6eea293740309afe7/propcache-0.5.4-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:2814ecd8e818f487bee4b0f921bc4d1c176cc5fc71ac0f072d0fa67eda4ac14b", size = 250424, upload-time = "2026-09-16T00:14:36.894Z" }, + { url = "https://files.pythonhosted.org/packages/33/2c/a763a8251f50fba042af0fb1f02bfec4b31381e40aff760db2be7b2e1f84/propcache-0.5.4-cp312-cp312-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:6af4693716bfb03f1752ef1b30faa593db2c01d5272e9b8564a1549452a979ab", size = 216748, upload-time = "2026-09-16T00:14:38.369Z" }, + { url = "https://files.pythonhosted.org/packages/6a/e2/4d11bea8fd6a777149c6c20645f873952eab5de3a2497aa11648ec9ab6ab/propcache-0.5.4-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:4fbc1a15dc8cd1689508758d626b372b1f09d28d9577667feaf9e6bfcd8efcbc", size = 246533, upload-time = "2026-09-16T00:14:39.82Z" }, + { url = "https://files.pythonhosted.org/packages/9f/36/6683597de4907e70c717e3588c541202c66086a72ff3db58be49de66e72c/propcache-0.5.4-cp312-cp312-musllinux_1_2_armv7l.whl", hash = "sha256:cdee8205a44d0be91bbac4c41b95d86641b72dfc7aef1279400e4fda3f26a937", size = 238173, upload-time = "2026-09-16T00:14:41.259Z" }, + { url = "https://files.pythonhosted.org/packages/85/84/cb08d79f1762daafeb2b030c470cd0c725c97b8ad67412457c6f35c53e9d/propcache-0.5.4-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:9a2a8a50a93dee0268a860a07fa3b4bd968f8ce4dbd794957da772f395368526", size = 251128, upload-time = "2026-09-16T00:14:42.652Z" }, + { url = "https://files.pythonhosted.org/packages/c2/0d/41b848036db6621370c1f2e5471a7da8149c730f8552a5257567721f4576/propcache-0.5.4-cp312-cp312-musllinux_1_2_riscv64.whl", hash = "sha256:7ffafcbfc7b549ab940047e505c831eabac5e67de53e1bc174adbc5285c55944", size = 214821, upload-time = "2026-09-16T00:14:44.112Z" }, + { url = "https://files.pythonhosted.org/packages/f1/b7/adfae4bf9c63bccf12e2d9690a175c6579047a6eec3b5a6a5f51428c15e2/propcache-0.5.4-cp312-cp312-musllinux_1_2_s390x.whl", hash = "sha256:d1f5a500bfcbb2c0ab85e98a0dcd70f5899d34efe365a0187700369a79603031", size = 254793, upload-time = "2026-09-16T00:14:45.429Z" }, + { url = "https://files.pythonhosted.org/packages/51/6f/eeca9647245d5f92e87d53e5f14335bb42fce1a7e6842c8045b364eded8b/propcache-0.5.4-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:8a235f73d6e020855dc29dff012d920c02ee0feab8d73a24185a7569f4be1161", size = 247134, upload-time = "2026-09-16T00:14:46.976Z" }, + { url = "https://files.pythonhosted.org/packages/5d/a9/424e38838793d37160b4379c702f61c74c598fc6cd17204adbe3c554f7a8/propcache-0.5.4-cp312-cp312-win32.whl", hash = "sha256:b3083bfe87f95c756e610bd8025f26cbd1cd4aaa03a422f2d65efb7a97cd53d8", size = 43073, upload-time = "2026-09-16T00:14:48.338Z" }, + { url = "https://files.pythonhosted.org/packages/58/7b/6e8ef26f6d510a7916064fec68d55fcbfbdf7eb01e377480d66a122152d8/propcache-0.5.4-cp312-cp312-win_amd64.whl", hash = "sha256:98914de2c4d7f0f9f4a8c6ea4bf05841f4175796941e3ef7d47eb718f22311fb", size = 46190, upload-time = "2026-09-16T00:14:49.99Z" }, + { url = "https://files.pythonhosted.org/packages/08/b9/72028c5b56ced97f456de6aefa79435ca64d7f77af78ea8cf3c76fc5195f/propcache-0.5.4-cp312-cp312-win_arm64.whl", hash = "sha256:8876b39961e33d912afe3c1bee18ee564fdad0206f873cc15d522756b7f50737", size = 43075, upload-time = "2026-09-16T00:14:51.155Z" }, + { url = "https://files.pythonhosted.org/packages/f5/cd/785c64ed382f3f04201870267b02783f63b4678c2acfddc177a3ebcc2727/propcache-0.5.4-py3-none-any.whl", hash = "sha256:62c60aec739ed00124573cce1178138fd690c7676352d67a37328c1cf51d7468", size = 16338, upload-time = "2026-09-16T00:17:13.106Z" }, +] + +[[package]] +name = "protobuf" +version = "7.36.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d9/89/5b8517baa72f84a67b8a307ba953c91057af618bf40bf676f3c03551f8f0/protobuf-7.36.2.tar.gz", hash = "sha256:497d0463ff3316681da6c0b9e8d06cb465d61abce00b613ab42226175644d1bb", size = 512737, upload-time = "2026-09-17T20:07:59.326Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/32/72/98342feb672507c8f3a69e34b4fa8961f608edba5c1a48a6f47156d92cb5/protobuf-7.36.2-cp310-abi3-macosx_10_9_universal2.whl", hash = "sha256:cbc70b17ee27e28894c7fee8bb04be1abead49e936bc70eb60052531eee2079e", size = 456039, upload-time = "2026-09-17T20:07:51.542Z" }, + { url = "https://files.pythonhosted.org/packages/b6/ea/91fdf7c2b8bbd49cde056f00a9df6773532987e1c00fe2830b895af95c7e/protobuf-7.36.2-cp310-abi3-manylinux2014_aarch64.whl", hash = "sha256:e11e1f0180583a2af89db6a2ecd9e8dc40aa6d2988ca175bfd0e6d12ea72d74e", size = 344219, upload-time = "2026-09-17T20:07:52.914Z" }, + { url = "https://files.pythonhosted.org/packages/17/ab/5fd5f8ece73fad885c5a09aa849b32d70472f954ba3a92d3bb5974ea953b/protobuf-7.36.2-cp310-abi3-manylinux2014_s390x.whl", hash = "sha256:f4fee11ec330d238b34a05c9b675f693c20415d1c5bd7d5320cc2f8a798eb9cf", size = 357223, upload-time = "2026-09-17T20:07:53.985Z" }, + { url = "https://files.pythonhosted.org/packages/db/f3/3996583dd2906297a637af12114deddf7658af6e683fedb83be061983fb5/protobuf-7.36.2-cp310-abi3-manylinux2014_x86_64.whl", hash = "sha256:89f23aa53c24553a2416fd4fd1ec06f74fa42b14b546d8883128813f775bbfd2", size = 343223, upload-time = "2026-09-17T20:07:54.931Z" }, + { url = "https://files.pythonhosted.org/packages/fc/1b/dcc64f358fcb51811b58ae40b3d28f820725f116d86487cc20bd4b130701/protobuf-7.36.2-cp310-abi3-win32.whl", hash = "sha256:912c1221170e16c08d1f086762f563dd61ff83c18b5fa6652952dfaded66f728", size = 442998, upload-time = "2026-09-17T20:07:55.826Z" }, + { url = "https://files.pythonhosted.org/packages/8a/55/b77bda4e5e5f5971fb51b07663694690e9afdb9402136c16a522bd621cad/protobuf-7.36.2-cp310-abi3-win_amd64.whl", hash = "sha256:a300819d441e078a5608c0d3c709796bb548136058fda017ae51d425b44fd353", size = 456514, upload-time = "2026-09-17T20:07:57.188Z" }, + { url = "https://files.pythonhosted.org/packages/e4/04/d52c7016b04b6c5108f26691f9d33ec82a9b65d041f1a9c771137693d618/protobuf-7.36.2-py3-none-any.whl", hash = "sha256:bdb3a345d48db958e6ce1f18e508beb0cc981d64f24088427549c866cd039f1e", size = 179806, upload-time = "2026-09-17T20:07:58.211Z" }, +] + +[[package]] +name = "psutil" +version = "7.2.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/aa/c6/d1ddf4abb55e93cebc4f2ed8b5d6dbad109ecb8d63748dd2b20ab5e57ebe/psutil-7.2.2.tar.gz", hash = "sha256:0746f5f8d406af344fd547f1c8daa5f5c33dbc293bb8d6a16d80b4bb88f59372", size = 493740, upload-time = "2026-01-28T18:14:54.428Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e7/36/5ee6e05c9bd427237b11b3937ad82bb8ad2752d72c6969314590dd0c2f6e/psutil-7.2.2-cp36-abi3-macosx_10_9_x86_64.whl", hash = "sha256:ed0cace939114f62738d808fdcecd4c869222507e266e574799e9c0faa17d486", size = 129090, upload-time = "2026-01-28T18:15:22.168Z" }, + { url = "https://files.pythonhosted.org/packages/80/c4/f5af4c1ca8c1eeb2e92ccca14ce8effdeec651d5ab6053c589b074eda6e1/psutil-7.2.2-cp36-abi3-macosx_11_0_arm64.whl", hash = "sha256:1a7b04c10f32cc88ab39cbf606e117fd74721c831c98a27dc04578deb0c16979", size = 129859, upload-time = "2026-01-28T18:15:23.795Z" }, + { url = "https://files.pythonhosted.org/packages/b5/70/5d8df3b09e25bce090399cf48e452d25c935ab72dad19406c77f4e828045/psutil-7.2.2-cp36-abi3-manylinux2010_x86_64.manylinux_2_12_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:076a2d2f923fd4821644f5ba89f059523da90dc9014e85f8e45a5774ca5bc6f9", size = 155560, upload-time = "2026-01-28T18:15:25.976Z" }, + { url = "https://files.pythonhosted.org/packages/63/65/37648c0c158dc222aba51c089eb3bdfa238e621674dc42d48706e639204f/psutil-7.2.2-cp36-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:b0726cecd84f9474419d67252add4ac0cd9811b04d61123054b9fb6f57df6e9e", size = 156997, upload-time = "2026-01-28T18:15:27.794Z" }, + { url = "https://files.pythonhosted.org/packages/8e/13/125093eadae863ce03c6ffdbae9929430d116a246ef69866dad94da3bfbc/psutil-7.2.2-cp36-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:fd04ef36b4a6d599bbdb225dd1d3f51e00105f6d48a28f006da7f9822f2606d8", size = 148972, upload-time = "2026-01-28T18:15:29.342Z" }, + { url = "https://files.pythonhosted.org/packages/04/78/0acd37ca84ce3ddffaa92ef0f571e073faa6d8ff1f0559ab1272188ea2be/psutil-7.2.2-cp36-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:b58fabe35e80b264a4e3bb23e6b96f9e45a3df7fb7eed419ac0e5947c61e47cc", size = 148266, upload-time = "2026-01-28T18:15:31.597Z" }, + { url = "https://files.pythonhosted.org/packages/b4/90/e2159492b5426be0c1fef7acba807a03511f97c5f86b3caeda6ad92351a7/psutil-7.2.2-cp37-abi3-win_amd64.whl", hash = "sha256:eb7e81434c8d223ec4a219b5fc1c47d0417b12be7ea866e24fb5ad6e84b3d988", size = 137737, upload-time = "2026-01-28T18:15:33.849Z" }, + { url = "https://files.pythonhosted.org/packages/8c/c7/7bb2e321574b10df20cbde462a94e2b71d05f9bbda251ef27d104668306a/psutil-7.2.2-cp37-abi3-win_arm64.whl", hash = "sha256:8c233660f575a5a89e6d4cb65d9f938126312bca76d8fe087b947b3a1aaac9ee", size = 134617, upload-time = "2026-01-28T18:15:36.514Z" }, +] + +[[package]] +name = "pyarrow" +version = "26.0.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/ec/34/17c34cb38e5d940e38f0f0d9fdfa0e8a506676409ea9b85aff7e3079f831/pyarrow-26.0.0.tar.gz", hash = "sha256:0cccd36e00ea3afeb52ded61f2721ce71f604853d70c45365c58324eb773d6ae", size = 1239433, upload-time = "2026-10-09T08:26:25.315Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/b3/60/6793778f2617cce469383dac0ba08c4f2401cf342df0c7b9ca53939d9b46/pyarrow-26.0.0-cp312-cp312-macosx_12_0_arm64.whl", hash = "sha256:90ddaf7c625307ad52f31a9b25c34fe5e4897c7529ee3481135822b2b6842ff1", size = 36333953, upload-time = "2026-10-09T08:14:00.387Z" }, + { url = "https://files.pythonhosted.org/packages/db/81/f944cc63ce8a753e5fbff25de6d1d475ebd7fffdf9cf98c65130294fc896/pyarrow-26.0.0-cp312-cp312-macosx_12_0_x86_64.whl", hash = "sha256:ee341973f78a0b46e073d065e88e75026a9c584051e97f98a0d05d96c6bac7dd", size = 38688456, upload-time = "2026-10-09T08:14:04.344Z" }, + { url = "https://files.pythonhosted.org/packages/f5/2d/7e5c722fa5d5d9f3b75e62fe11694b34217664d4f05ac88031197166b277/pyarrow-26.0.0-cp312-cp312-manylinux_2_28_aarch64.whl", hash = "sha256:01c863a18bd9c8412453dd0d92de6d0ee7b2b3d6fb079d9734a4b2a3c8bd4453", size = 50867603, upload-time = "2026-10-09T08:14:09.115Z" }, + { url = "https://files.pythonhosted.org/packages/88/e4/9cd356d906e71bd79b0c3fc5c9a54e01a0020dcf14c152ccfbcb503c7298/pyarrow-26.0.0-cp312-cp312-manylinux_2_28_x86_64.whl", hash = "sha256:6a628922ba20705fa964ca73e4ef959c2fb2f14b9bbec5589a6a1e68e6257c85", size = 53931932, upload-time = "2026-10-09T08:14:24.051Z" }, + { url = "https://files.pythonhosted.org/packages/bb/e4/5bae3133b7fe04c24907a20f3bc1fba388cbbde659199e7b76445982047a/pyarrow-26.0.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:954d971b363b16ee41f89389a4053315dc71265f2ce5c2468eb0a910b1166268", size = 54444720, upload-time = "2026-10-09T08:14:31.214Z" }, + { url = "https://files.pythonhosted.org/packages/ba/b4/ee422493bb6dafdbef776cfe2c2a73106a1063a79bf4e78d1e5f51176885/pyarrow-26.0.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:5d5768d03426abe6526d5274adefa00abf00a7f81118c46e98b5a46390f5549e", size = 57388949, upload-time = "2026-10-09T08:14:38.964Z" }, + { url = "https://files.pythonhosted.org/packages/54/3c/1783aab1dac28e175dcf26dfc7123725efc474caecaed91e8a34cb89cad0/pyarrow-26.0.0-cp312-cp312-win_amd64.whl", hash = "sha256:cc903e1069e9dd5e9dcf780324c0112e27e051e422ecfaff574fb33ed65d9160", size = 28567581, upload-time = "2026-10-09T08:14:44.279Z" }, +] + +[[package]] +name = "pydantic" +version = "2.14.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "annotated-types" }, + { name = "pydantic-core" }, + { name = "typing-extensions" }, + { name = "typing-inspection" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/7c/0b/8e10b2e693af8ec54346a14caf36221334775975a747c9ceaa3f8371d96d/pydantic-2.14.1.tar.gz", hash = "sha256:94f478203dd03404682a1ada216965651dd74b1d2d5ffd62e00e0837caab5c26", size = 900362, upload-time = "2026-10-11T18:37:55.396Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/35/ea/a56b9fe5066f3537b7882f77e9c5dfb26d8c2eefdaed9b5fc73d57b4dc22/pydantic-2.14.1-py3-none-any.whl", hash = "sha256:9195d967ec791692a04438115466764fb8b9a27b31f14a760437694f40d6b454", size = 488106, upload-time = "2026-10-11T18:37:53.437Z" }, +] + +[[package]] +name = "pydantic-core" +version = "2.50.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/a6/24/af4ec4be49fbc810f35b0bdcacc3d433b56bbfa469fd3bfd4ae116cd9bf1/pydantic_core-2.50.1.tar.gz", hash = "sha256:e50d7b94baac6c7d09927fa5ca5800a0c7ee5015c7fcff65beb3a1931b5a6e09", size = 513515, upload-time = "2026-10-11T18:35:44.82Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/ee/94/101b49a6c63f99586755690696eae14532772587d9e39e93f7b8d28a49da/pydantic_core-2.50.1-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:704075d10b74f2f3c6e15407c696d88701df35fc8953f434a431add0d0074db0", size = 2222428, upload-time = "2026-10-11T18:32:20.228Z" }, + { url = "https://files.pythonhosted.org/packages/db/a3/f8b1b39349c33037c73d7a1b794c9c29d8f07d5bd2b74399c1375a529cc6/pydantic_core-2.50.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:e8e1d6ce820aa23317e8209a86bd65a540973c12dc7552b48a4f6c8e9926815e", size = 2025779, upload-time = "2026-10-11T18:32:21.723Z" }, + { url = "https://files.pythonhosted.org/packages/d2/2d/862a7d115305cc621358e51ba13f296e0ffdf4fbd984be9f63bc7862730b/pydantic_core-2.50.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:c18db21573bd2c6489f9a544b7499f0df2853958c568e5e783536ee1f690af41", size = 2048429, upload-time = "2026-10-11T18:32:23.3Z" }, + { url = "https://files.pythonhosted.org/packages/c0/7d/fed95d18bf2b5abe2d5ab2bbec4184bfc22b6805994a33ccdae8edffd25a/pydantic_core-2.50.1-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:cb57f304525a5e3c13333b772bf9a473f36326e9c821b2e8e1b2fd36f80ae2c3", size = 2173274, upload-time = "2026-10-11T18:32:24.697Z" }, + { url = "https://files.pythonhosted.org/packages/93/1e/c6f22ceac65dc3d291ff79bea0eb74cbd1de8b128f01fee48af03bcea1e1/pydantic_core-2.50.1-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:a27c09d86600f1bf2fe3f37e1ae697faf3143931c09322cd799da94deee923b5", size = 2360403, upload-time = "2026-10-11T18:32:26.281Z" }, + { url = "https://files.pythonhosted.org/packages/2c/97/190b5b6bfe4c72f265feb657913eb30749193927a5ad7b83cfa7866a25a2/pydantic_core-2.50.1-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:46b3301d3b5c886f77de7546e47274a5842c622ea2020b8c6524c6b66913b4a6", size = 2439799, upload-time = "2026-10-11T18:32:27.844Z" }, + { url = "https://files.pythonhosted.org/packages/90/24/48cd98388e4ac2c08d0800af36d15413db15e20387f1038123a2c86cb2f6/pydantic_core-2.50.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:93ba4e9d8210d941c200431a56b2c0400b131865947903937ed3ec5404307d2e", size = 2160157, upload-time = "2026-10-11T18:32:29.553Z" }, + { url = "https://files.pythonhosted.org/packages/00/86/d909133d0b9979ed14c20f110f21124a0e346caf071bd66ac5af3009062b/pydantic_core-2.50.1-cp312-cp312-manylinux_2_31_riscv64.whl", hash = "sha256:e5faeaee74a57d32b3ab3aebad2e348f06d3ba946fc5d28c1728455f00a3d13a", size = 2229523, upload-time = "2026-10-11T18:32:31.027Z" }, + { url = "https://files.pythonhosted.org/packages/90/31/4b43f08131b2098e73a852fd932063385f3103b33839fd9d1abdb89e673d/pydantic_core-2.50.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:a3cda0e538208e5d722bbf3698b24f19c0a7d05bc8d5f8a7f9b121ea7fa243d9", size = 2242700, upload-time = "2026-10-11T18:32:32.622Z" }, + { url = "https://files.pythonhosted.org/packages/88/77/de39658569dc7a4c293d110a89629f61145e7538f00d1e4280c28debefd3/pydantic_core-2.50.1-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:57f51b31ff826e2859120cf4737c5a758a48d96f3e97da40ccee1796d58078ff", size = 2278452, upload-time = "2026-10-11T18:32:34.165Z" }, + { url = "https://files.pythonhosted.org/packages/46/bd/1eeb9e4e791e81210ef9096f58080b32ba15b5b13c108d50aaa3f8ce9f73/pydantic_core-2.50.1-cp312-cp312-musllinux_1_1_armv7l.whl", hash = "sha256:8daa7ee75245d43ad7d747e5c9ecc1b1d06552f72b14887e9276f787d57375f4", size = 2450098, upload-time = "2026-10-11T18:32:35.547Z" }, + { url = "https://files.pythonhosted.org/packages/1c/e6/b2accfdff17e5a88d6358536702146de00f9339adaa4d5bbe1ee93cb4832/pydantic_core-2.50.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:acbf31f37c53a5ac0c34706c80b4f5107ba20b05fdd3816124bf236ef0c57dd2", size = 2476839, upload-time = "2026-10-11T18:32:37.119Z" }, + { url = "https://files.pythonhosted.org/packages/3b/7e/c8a83eb5d2cd42c2e13507150fb777b1edea5a018adc9cbf2858a1d08285/pydantic_core-2.50.1-cp312-cp312-win32.whl", hash = "sha256:45b11cac094aa25725581d9304eee93c9028516b9ea80dd9e175e13a5a2c840e", size = 2101425, upload-time = "2026-10-11T18:32:38.593Z" }, + { url = "https://files.pythonhosted.org/packages/c5/17/8bbd530b8659e9d963f5b16f6cb8e159f4cd174b4d6304e48969ab12dba8/pydantic_core-2.50.1-cp312-cp312-win_amd64.whl", hash = "sha256:132529c83901437ff642f585216831bf5fd7a91df66829907e155192ead62498", size = 2172522, upload-time = "2026-10-11T18:32:40.112Z" }, + { url = "https://files.pythonhosted.org/packages/af/9f/a79d690b127e4e3cff143d031538ab9aa715f6b2fbeecbd07cd6f2bf9fb7/pydantic_core-2.50.1-cp312-cp312-win_arm64.whl", hash = "sha256:4e834f6a8e4ff772dcc34f58ef5504147a3ea5b0f4eeb13b0f8eb2ca75ac57f1", size = 2161366, upload-time = "2026-10-11T18:32:41.606Z" }, + { url = "https://files.pythonhosted.org/packages/a5/09/1bcf160f3cd6333e2a76982fae5963e5032de0bcaaba38464223dc266bfe/pydantic_core-2.50.1-graalpy312-graalpy250_312_native-macosx_10_12_x86_64.whl", hash = "sha256:76e2e83fa6ec8cdc972d438dafc2522b3a47bee4ec0ae668b29cfb1977ab5242", size = 2217070, upload-time = "2026-10-11T18:35:15.688Z" }, + { url = "https://files.pythonhosted.org/packages/3d/81/f02de95eec7794dfc7a5bc69a834f0e834b31b3009014ce6de903855f775/pydantic_core-2.50.1-graalpy312-graalpy250_312_native-macosx_11_0_arm64.whl", hash = "sha256:a51eee75939cf811ac09b278745a6cee7dc873ccfbc8b9af3cc88fe4b7ce25b5", size = 2039027, upload-time = "2026-10-11T18:35:18.07Z" }, + { url = "https://files.pythonhosted.org/packages/4d/16/227746ed3771d9bf2304568b55134849ece34e13b38b1d76de763015c77f/pydantic_core-2.50.1-graalpy312-graalpy250_312_native-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ae28183297fb0d2b8dc46a1f01d51f5e45825fc5afe76a835a6cb7fb34821295", size = 2109009, upload-time = "2026-10-11T18:35:20.218Z" }, + { url = "https://files.pythonhosted.org/packages/e5/b9/7664d1592a0e5a74903f357cd5b28ceb7336f3473256c5a4e8c8432f1881/pydantic_core-2.50.1-graalpy312-graalpy250_312_native-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:88e492e8b9d0312e7dc13667c30222abf284dc3b79b5302b3607b41a5784ce61", size = 2269883, upload-time = "2026-10-11T18:35:22.48Z" }, +] + +[[package]] +name = "pygments" +version = "2.21.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/49/2e/ced460408999b33da6b31b0021b0f37d329e202d4169aeb164493778f25b/pygments-2.21.0.tar.gz", hash = "sha256:610ca751c9bc2492b38eb9a38a7fbc93edbbb2d7182edaf34e66ae493dee5c8c", size = 5005329, upload-time = "2026-08-17T08:02:48.824Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/71/46/17f022dd3e953bf20a04a028a21ec746d942f8d2af30fa0f124fa0e6a684/pygments-2.21.0-py3-none-any.whl", hash = "sha256:2363c69b61c4a97c838da3b130dcd6468f4848992b21a82f2a63ec34377137d9", size = 1250147, upload-time = "2026-08-17T08:02:44.912Z" }, +] + +[[package]] +name = "pytest" +version = "9.1.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "colorama", marker = "sys_platform == 'win32'" }, + { name = "iniconfig" }, + { name = "packaging" }, + { name = "pluggy" }, + { name = "pygments" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/e4/47/b9efed96c114afcfa3c9d3fe98a76a1d14c74a9e266d397cf6eb64be5e01/pytest-9.1.1.tar.gz", hash = "sha256:1088fbde8f2b49d95a549a195707afa7a76a3ce9bcadc26b6d71f0ffda5fe313", size = 1636369, upload-time = "2026-06-19T10:58:32.857Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/24/25/1de2678b631f5a49215c6c96fff41ba892b0a34df68d6d80292b1b48aa7f/pytest-9.1.1-py3-none-any.whl", hash = "sha256:37a86b45efb9a47a61a36449063e8e18d0cab3161329fc099eb21783169c4f0c", size = 386536, upload-time = "2026-06-19T10:58:31.347Z" }, +] + +[[package]] +name = "pytest-asyncio" +version = "1.4.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "pytest" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/43/7c/d36d04db312ecf4298932ef77e6e4a9e8ad017906e24e34f0b0c361a2473/pytest_asyncio-1.4.0.tar.gz", hash = "sha256:c6c0d2259945122819f171a32ecea2c349ead889ee28176caaf492143424be42", size = 58514, upload-time = "2026-05-26T09:56:04.083Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/03/e2/08a497ef684b88559c9cc5f4ad53a37e7b99e727094a86d6ea32536d5d3c/pytest_asyncio-1.4.0-py3-none-any.whl", hash = "sha256:933ca923a23075a87fb7070c0ec272a6848489824d887c85c812670932835aa1", size = 16930, upload-time = "2026-05-26T09:56:02.576Z" }, +] + +[[package]] +name = "pytest-xdist" +version = "3.8.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "execnet" }, + { name = "pytest" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/78/b4/439b179d1ff526791eb921115fca8e44e596a13efeda518b9d845a619450/pytest_xdist-3.8.0.tar.gz", hash = "sha256:7e578125ec9bc6050861aa93f2d59f1d8d085595d6551c2c90b6f4fad8d3a9f1", size = 88069, upload-time = "2025-07-01T13:30:59.346Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/ca/31/d4e37e9e550c2b92a9cbc2e4d0b7420a27224968580b5a447f420847c975/pytest_xdist-3.8.0-py3-none-any.whl", hash = "sha256:202ca578cfeb7370784a8c33d6d05bc6e13b4f25b5053c30a152269fd10f0b88", size = 46396, upload-time = "2025-07-01T13:30:56.632Z" }, +] + +[package.optional-dependencies] +psutil = [ + { name = "psutil" }, +] + +[[package]] +name = "python-dateutil" +version = "2.9.0.post0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "six" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/66/c0/0c8b6ad9f17a802ee498c46e004a0eb49bc148f2fd230864601a86dcf6db/python-dateutil-2.9.0.post0.tar.gz", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 342432, upload-time = "2024-03-01T18:36:20.211Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/ec/57/56b9bcc3c9c6a792fcbaf139543cee77261f3651ca9da0c93f5c1221264b/python_dateutil-2.9.0.post0-py2.py3-none-any.whl", hash = "sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427", size = 229892, upload-time = "2024-03-01T18:36:18.57Z" }, +] + +[[package]] +name = "pyyaml" +version = "6.0.3" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/05/8e/961c0007c59b8dd7729d542c61a4d537767a59645b82a0b521206e1e25c2/pyyaml-6.0.3.tar.gz", hash = "sha256:d76623373421df22fb4cf8817020cbb7ef15c725b9d5e45f17e189bfc384190f", size = 130960, upload-time = "2025-09-25T21:33:16.546Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d1/33/422b98d2195232ca1826284a76852ad5a86fe23e31b009c9886b2d0fb8b2/pyyaml-6.0.3-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:7f047e29dcae44602496db43be01ad42fc6f1cc0d8cd6c83d342306c32270196", size = 182063, upload-time = "2025-09-25T21:32:11.445Z" }, + { url = "https://files.pythonhosted.org/packages/89/a0/6cf41a19a1f2f3feab0e9c0b74134aa2ce6849093d5517a0c550fe37a648/pyyaml-6.0.3-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:fc09d0aa354569bc501d4e787133afc08552722d3ab34836a80547331bb5d4a0", size = 173973, upload-time = "2025-09-25T21:32:12.492Z" }, + { url = "https://files.pythonhosted.org/packages/ed/23/7a778b6bd0b9a8039df8b1b1d80e2e2ad78aa04171592c8a5c43a56a6af4/pyyaml-6.0.3-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:9149cad251584d5fb4981be1ecde53a1ca46c891a79788c0df828d2f166bda28", size = 775116, upload-time = "2025-09-25T21:32:13.652Z" }, + { url = "https://files.pythonhosted.org/packages/65/30/d7353c338e12baef4ecc1b09e877c1970bd3382789c159b4f89d6a70dc09/pyyaml-6.0.3-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:5fdec68f91a0c6739b380c83b951e2c72ac0197ace422360e6d5a959d8d97b2c", size = 844011, upload-time = "2025-09-25T21:32:15.21Z" }, + { url = "https://files.pythonhosted.org/packages/8b/9d/b3589d3877982d4f2329302ef98a8026e7f4443c765c46cfecc8858c6b4b/pyyaml-6.0.3-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ba1cc08a7ccde2d2ec775841541641e4548226580ab850948cbfda66a1befcdc", size = 807870, upload-time = "2025-09-25T21:32:16.431Z" }, + { url = "https://files.pythonhosted.org/packages/05/c0/b3be26a015601b822b97d9149ff8cb5ead58c66f981e04fedf4e762f4bd4/pyyaml-6.0.3-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:8dc52c23056b9ddd46818a57b78404882310fb473d63f17b07d5c40421e47f8e", size = 761089, upload-time = "2025-09-25T21:32:17.56Z" }, + { url = "https://files.pythonhosted.org/packages/be/8e/98435a21d1d4b46590d5459a22d88128103f8da4c2d4cb8f14f2a96504e1/pyyaml-6.0.3-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:41715c910c881bc081f1e8872880d3c650acf13dfa8214bad49ed4cede7c34ea", size = 790181, upload-time = "2025-09-25T21:32:18.834Z" }, + { url = "https://files.pythonhosted.org/packages/74/93/7baea19427dcfbe1e5a372d81473250b379f04b1bd3c4c5ff825e2327202/pyyaml-6.0.3-cp312-cp312-win32.whl", hash = "sha256:96b533f0e99f6579b3d4d4995707cf36df9100d67e0c8303a0c55b27b5f99bc5", size = 137658, upload-time = "2025-09-25T21:32:20.209Z" }, + { url = "https://files.pythonhosted.org/packages/86/bf/899e81e4cce32febab4fb42bb97dcdf66bc135272882d1987881a4b519e9/pyyaml-6.0.3-cp312-cp312-win_amd64.whl", hash = "sha256:5fcd34e47f6e0b794d17de1b4ff496c00986e1c83f7ab2fb8fcfe9616ff7477b", size = 154003, upload-time = "2025-09-25T21:32:21.167Z" }, + { url = "https://files.pythonhosted.org/packages/1a/08/67bd04656199bbb51dbed1439b7f27601dfb576fb864099c7ef0c3e55531/pyyaml-6.0.3-cp312-cp312-win_arm64.whl", hash = "sha256:64386e5e707d03a7e172c0701abfb7e10f0fb753ee1d773128192742712a98fd", size = 140344, upload-time = "2025-09-25T21:32:22.617Z" }, +] + +[[package]] +name = "randomname" +version = "0.2.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "fire" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/e8/c2/525e9e9b458c3ca493d9bd0871f3ed9b51446d26fe82d462494de188f848/randomname-0.2.1.tar.gz", hash = "sha256:b79b98302ba4479164b0a4f87995b7bebbd1d91012aeda483341e3e58ace520e", size = 64242, upload-time = "2023-01-29T02:42:26.469Z" } + +[[package]] +name = "regex" +version = "2026.9.29" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/fc/f2/af1da9d3ceed77bfcdce40427d49ba0be94e4fe84245e3bfef68c10e75b6/regex-2026.9.29.tar.gz", hash = "sha256:8b5fcc4771732191b2b7d1dd68d8f0353f47f8d90b6150f6dce58bf1112442cb", size = 419199, upload-time = "2026-09-29T00:49:58.298Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/84/48/3fdcde9a0baa84d7d25571223265d6e434e114763b438601d54a8028bf3e/regex-2026.9.29-cp312-cp312-macosx_10_13_universal2.whl", hash = "sha256:dc79d36d0618752265f0d575915bdc5c5130ecb9c9f6b3bcefeae32e4bdfafcf", size = 497903, upload-time = "2026-09-29T00:46:38.938Z" }, + { url = "https://files.pythonhosted.org/packages/2e/1c/4ee3e97c76f53940488dfe7a7e18705e78daac8cd7fb161d246b9e328449/regex-2026.9.29-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:3a21a9509d0ee88e7a70e1ad228cd2f0e0fd1e187458db132e8a8d18c97daf9d", size = 296416, upload-time = "2026-09-29T00:46:40.406Z" }, + { url = "https://files.pythonhosted.org/packages/37/14/f3f0ba083d2094392d5eabf56db5ea6ba469fd6e927afd187042054ea68a/regex-2026.9.29-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:f57dc6b8fef170f105d2cf5cdce254f47b137d7755086cf7050f47e16582abba", size = 293633, upload-time = "2026-09-29T00:46:41.959Z" }, + { url = "https://files.pythonhosted.org/packages/c9/72/67e7a8ce17f1aea49df215564048efb49cc8c2b31a0e0fc30f36838f8516/regex-2026.9.29-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:f93bc1c3486ef3747e07c9d7c1d0a147b8fbaab975f80e348aed6f71309dfaca", size = 805885, upload-time = "2026-09-29T00:46:43.373Z" }, + { url = "https://files.pythonhosted.org/packages/f6/78/25436bcfd4d2260b4b4090094d55d7ab53ec8a1ab4865a0b8bcb33c7d5c0/regex-2026.9.29-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:9e1d3a4cb7993b708f0ada8d0c84590efd853f169e7147d2202c9da503180242", size = 878344, upload-time = "2026-09-29T00:46:45.328Z" }, + { url = "https://files.pythonhosted.org/packages/97/e6/a09ec3a23ae41d6179880e67f0aace9284b2d95f2d7b326eff203f8eec5e/regex-2026.9.29-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:dabee8f4935e731fb46b2a3091bdda0d3d94b3bbfb907d2b4f12eefce4009619", size = 919181, upload-time = "2026-09-29T00:46:47.041Z" }, + { url = "https://files.pythonhosted.org/packages/26/83/d2fbd2e4e3afb1167daa825187d196f313cbaa1a4768f311fb041bb0e3d2/regex-2026.9.29-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:39ab5894d971f9ac68baa6eca5c50387db579cfcacf36ae8df3feceb1815e6d0", size = 807783, upload-time = "2026-09-29T00:46:48.894Z" }, + { url = "https://files.pythonhosted.org/packages/46/0b/eb429a7016610d44fc89a597163f8c9127505f0d7dc724dc9effbb6a3ac0/regex-2026.9.29-cp312-cp312-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:c1a9a6651197fbed6f0212591418b9def774fc3f8324f78d1bf0e6a63e5f8aa1", size = 783465, upload-time = "2026-09-29T00:46:50.64Z" }, + { url = "https://files.pythonhosted.org/packages/1b/07/58a3c0153c7476898430f6a7cf3d9062a1d17fbea4f43399ecaf411c7b4c/regex-2026.9.29-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:87fb80cbe3557e27e7b28b995c2b2eedf689b8886f941ab93e0e288f0976518a", size = 793519, upload-time = "2026-09-29T00:46:52.396Z" }, + { url = "https://files.pythonhosted.org/packages/2a/e8/161b94d39164520e21a7befe0245569bf7fda4c7cf1fc4e2df2b5def49da/regex-2026.9.29-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:3c5c2ef13797466aa64170cbb66ad98a32351dd4127694cea7199f80f213750d", size = 869293, upload-time = "2026-09-29T00:46:54.128Z" }, + { url = "https://files.pythonhosted.org/packages/8f/07/3b02ed829aa2decdc1955d222bd1e2f99d1c8bb4873bbb9a66b2f0a36bff/regex-2026.9.29-cp312-cp312-musllinux_1_2_riscv64.whl", hash = "sha256:59b49507f47479e299a9e1bc41b5cb83a7afda0540625f1dbae886615978acbf", size = 770239, upload-time = "2026-09-29T00:46:56.106Z" }, + { url = "https://files.pythonhosted.org/packages/42/5b/ba61f6fe062eb8562e742367d177bb75370434138ef6c9d2a27114f8d613/regex-2026.9.29-cp312-cp312-musllinux_1_2_s390x.whl", hash = "sha256:0dd8af32e9f7b56b7f95cc1fd79b23054c3bdc172392ae560acc24d57b7ffe71", size = 861973, upload-time = "2026-09-29T00:46:57.665Z" }, + { url = "https://files.pythonhosted.org/packages/cc/27/767259b20e8a842948990f5e99138d6c077248fd42f8b5468b1d9ca4b814/regex-2026.9.29-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:db5e82ba15c142425b8406690032df89e39cca4a2e8afbbb9a3d84edc2373ac3", size = 796470, upload-time = "2026-09-29T00:46:59.236Z" }, + { url = "https://files.pythonhosted.org/packages/a0/05/2566c4ba849b68a8ab81a6bf428fa79d20aae7ddee83979103c0381df254/regex-2026.9.29-cp312-cp312-win32.whl", hash = "sha256:d0c3082bf79bcd6a614d55916590ad4b8f93200e10b97f463ea5d9d07c9b5f23", size = 269327, upload-time = "2026-09-29T00:47:01.135Z" }, + { url = "https://files.pythonhosted.org/packages/93/19/489bc8db91196381c935752df01ba3f607140daece33b78d88573f028e64/regex-2026.9.29-cp312-cp312-win_amd64.whl", hash = "sha256:fdd88ed5e20b1bcdd234421e454962c971aa44b653bdb7f1ea9ef683e90fb649", size = 280334, upload-time = "2026-09-29T00:47:04.436Z" }, + { url = "https://files.pythonhosted.org/packages/0b/47/fb88ba779d0e5e7d4b0ec1aceeb13845948a2cb876bd572a2d1dfdba090b/regex-2026.9.29-cp312-cp312-win_arm64.whl", hash = "sha256:4fe97894d1b306c919b4e50def1e6f6c522f4d03a7283811f4d108f1ce5d3ac2", size = 279614, upload-time = "2026-09-29T00:47:06.541Z" }, +] + +[[package]] +name = "rich" +version = "15.0.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "markdown-it-py" }, + { name = "pygments" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/c0/8f/0722ca900cc807c13a6a0c696dacf35430f72e0ec571c4275d2371fca3e9/rich-15.0.0.tar.gz", hash = "sha256:edd07a4824c6b40189fb7ac9bc4c52536e9780fbbfbddf6f1e2502c31b068c36", size = 230680, upload-time = "2026-04-12T08:24:00.75Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/82/3b/64d4899d73f91ba49a8c18a8ff3f0ea8f1c1d75481760df8c68ef5235bf5/rich-15.0.0-py3-none-any.whl", hash = "sha256:33bd4ef74232fb73fe9279a257718407f169c09b78a87ad3d296f548e27de0bb", size = 310654, upload-time = "2026-04-12T08:24:02.83Z" }, +] + +[[package]] +name = "safetensors" +version = "0.8.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/45/06/f955dbbb1859e3bd23c8ac6141af5106e7ad5fedec4a3a6e3d60f94b7001/safetensors-0.8.0.tar.gz", hash = "sha256:fabaf3e0f18a6618d9b36560682562157f77c2b71fcffc7b432be2baed9d753d", size = 325846, upload-time = "2026-06-09T07:52:25.563Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/39/a0/f718cda65b05407d228f97602cf60dca269c979867aa5beb25410de26cd3/safetensors-0.8.0-cp310-abi3-macosx_10_12_x86_64.whl", hash = "sha256:c554f85858e05226d3c2828e32395e677434685d6d94594a41643361c5e837f0", size = 473568, upload-time = "2026-06-09T07:52:18.829Z" }, + { url = "https://files.pythonhosted.org/packages/f5/b1/fa7c600e7dceae12e9606c7578cbc9ff1e1ed55844883ee5c92205e86226/safetensors-0.8.0-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:c80201d22cbf405b80647a60ada77bba06c8fba2da2743ba1e89cdcc39a81f25", size = 484562, upload-time = "2026-06-09T07:52:17.518Z" }, + { url = "https://files.pythonhosted.org/packages/09/7d/65a7de0af421317bb36a067241e4235fff194eed60b961ed6d3f59a3fc60/safetensors-0.8.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:7a46e5ff292c356d6991e60942ba7f79817682d3a2cef0702136448cb9c4d235", size = 502844, upload-time = "2026-06-09T07:52:07.624Z" }, + { url = "https://files.pythonhosted.org/packages/91/4f/3175c9d75634e0e0dda0082794193521035edd7c70a6f212bf33ca06ddf4/safetensors-0.8.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:4124502b78f03534117c848f87a39b8f31e577b15eff423bf8bfb95f2a8c30d0", size = 511823, upload-time = "2026-06-09T07:52:09.565Z" }, + { url = "https://files.pythonhosted.org/packages/20/87/846c289e7aa2299eff406335717cf43ce8777194ece8aad75772e0411615/safetensors-0.8.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:7bc0a787ba8a35be368ee3574edfa2b1ad389eebd0a72e482ae275490e3f6c98", size = 633461, upload-time = "2026-06-09T07:52:11.128Z" }, + { url = "https://files.pythonhosted.org/packages/76/22/8d64d9df2c45d5ded401df889d0ad90882804ca172d79ec4f0df8f727fe0/safetensors-0.8.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:040070828e36dc8e122178bbbd5830ff9e97920affb84cbe0f46442497bed358", size = 545148, upload-time = "2026-06-09T07:52:13.603Z" }, + { url = "https://files.pythonhosted.org/packages/28/50/f203ff3a3ddfe19308efc83c5a3a29ed02bf786732ec35e68bf9162f3365/safetensors-0.8.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:fd6f3f93c9a0a7cc2788ee63fb763353d4bd2e89b0751bc78fcf7dda00bea774", size = 516040, upload-time = "2026-06-09T07:52:16.29Z" }, + { url = "https://files.pythonhosted.org/packages/46/fb/cdaed17ceb2948784fd9c36b6fd3e951b608547cea81a48e8ee6f8cfdfcb/safetensors-0.8.0-cp310-abi3-manylinux_2_31_riscv64.whl", hash = "sha256:fcdd41ec4628fee5799f807c73c353629130fbd942aa23d83c623dd6c9d52d78", size = 513832, upload-time = "2026-06-09T07:52:12.37Z" }, + { url = "https://files.pythonhosted.org/packages/0d/49/1e15de264dcc3b77943d2d0c56a95809956883b1c2d6d585c792523f180b/safetensors-0.8.0-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:8e9f537aa183a38ace122d27303dcd986b26bd2a7591f9181d7f0c396f4677ca", size = 559930, upload-time = "2026-06-09T07:52:14.743Z" }, + { url = "https://files.pythonhosted.org/packages/2a/43/bf38443278eab4b1be1fce2931e2b012ad9cb7df52ada751d0aab8f7659a/safetensors-0.8.0-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:87eec7ffed2b809f05a398a8becb7d013f19f7837cd15d9748580d6cf30dbaf4", size = 678670, upload-time = "2026-06-09T07:52:20.032Z" }, + { url = "https://files.pythonhosted.org/packages/72/e3/68cd3fa5b48488e84add63e04cb12f3bc28ae4638c06d4508c6e88823d0e/safetensors-0.8.0-cp310-abi3-musllinux_1_2_armv7l.whl", hash = "sha256:4a95ae2b05d7726d751da4ebf626a2ca782b706e101bd894c95bc2450b1cffcc", size = 786679, upload-time = "2026-06-09T07:52:21.322Z" }, + { url = "https://files.pythonhosted.org/packages/29/4b/1c19c509d56e01f4fbb3d0a2e597450f6cc04d1d56cf52defb0a62dfd715/safetensors-0.8.0-cp310-abi3-musllinux_1_2_i686.whl", hash = "sha256:3ae091f16662658bdc019a4ff6cb4c085bb7d725eb5978b183ffd265863b6d2d", size = 765683, upload-time = "2026-06-09T07:52:22.594Z" }, + { url = "https://files.pythonhosted.org/packages/27/43/41c1621732edd934d868a00d1b891584c892a7b62a9aab82ea5a0a5623ee/safetensors-0.8.0-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:8e080062fcde23be189565e1c3305d16751a218ecf9412c8601e64204eb6f846", size = 722361, upload-time = "2026-06-09T07:52:23.924Z" }, + { url = "https://files.pythonhosted.org/packages/8e/3f/73ccf82579412b4a71c4ca673f10b5f1f888d7cf5af7fe24f27d30307be4/safetensors-0.8.0-cp310-abi3-win32.whl", hash = "sha256:2ddf52eac562eda224f99acfa7889d02968c1fd59a5b011ae7d8137c37e9c02d", size = 342401, upload-time = "2026-06-09T07:52:28.895Z" }, + { url = "https://files.pythonhosted.org/packages/1b/6d/3fba214c1e5e0f69991677ec3bc17023f0421776975e1de0c682dca475e2/safetensors-0.8.0-cp310-abi3-win_amd64.whl", hash = "sha256:096ec1a98435df7beb08853bb5aa9081a84f23d0adc67ed1a0a10550f608373f", size = 355540, upload-time = "2026-06-09T07:52:27.832Z" }, + { url = "https://files.pythonhosted.org/packages/8d/fc/7eedc3510d97878876e32774eebbeb61c43f148a96e915c84229a3e967aa/safetensors-0.8.0-cp310-abi3-win_arm64.whl", hash = "sha256:f7838e5135a406ad3e02efdcb8cf2e5397d368b0154537c4fec682dbc544d452", size = 340500, upload-time = "2026-06-09T07:52:26.745Z" }, +] + +[[package]] +name = "shellingham" +version = "1.5.4" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/58/15/8b3609fd3830ef7b27b655beb4b4e9c62313a4e8da8c676e142cc210d58e/shellingham-1.5.4.tar.gz", hash = "sha256:8dbca0739d487e5bd35ab3ca4b36e11c4078f3a234bfce294b0a0291363404de", size = 10310, upload-time = "2023-10-24T04:13:40.426Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e0/f9/0595336914c5619e5f28a1fb793285925a8cd4b432c9da0a987836c7f822/shellingham-1.5.4-py2.py3-none-any.whl", hash = "sha256:7ecfff8f2fd72616f7481040475a65b2bf8af90a56c89140852d1120324e8686", size = 9755, upload-time = "2023-10-24T04:13:38.866Z" }, +] + +[[package]] +name = "six" +version = "1.17.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/94/e7/b2c673351809dca68a0e064b6af791aa332cf192da575fd474ed7d6f16a2/six-1.17.0.tar.gz", hash = "sha256:ff70335d468e7eb6ec65b95b99d3a2836546063f63acc5171de367e834932a81", size = 34031, upload-time = "2024-12-04T17:35:28.174Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/b7/ce/149a00dd41f10bc29e5921b496af8b574d8413afcd5e30dfa0ed46c2cc5e/six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274", size = 11050, upload-time = "2024-12-04T17:35:26.475Z" }, +] + +[[package]] +name = "starlette" +version = "1.9.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/ad/eb/4e197bf26b023abafc5e9043ec8710d7de0fcb032d0969045236d72aa0b1/starlette-1.9.0.tar.gz", hash = "sha256:3d931de246ed870e35ce605974440816ef895e592f255d1396cfdf191d4c4318", size = 2732466, upload-time = "2026-10-18T15:50:33.189Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/f3/93/181df62699d9b5aca1d87e5cfda131dac87e3cdaebf668662f7206ca3470/starlette-1.9.0-py3-none-any.whl", hash = "sha256:fd3f159936e882d06c1108f84688f7cda2cf85fc6a592a50edbd175e882c2ddc", size = 80335, upload-time = "2026-10-18T15:50:31.526Z" }, +] + +[[package]] +name = "synchronicity" +version = "0.12.6" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/ee/5f/9f6f7df5919f0d085013b96b9cb99b622b974fe722a04c126c10497bba8b/synchronicity-0.12.6.tar.gz", hash = "sha256:ac971eadb64c95938816b8d6125d6e28473982f3256789a35bdbe02025e1ce17", size = 62425, upload-time = "2026-10-02T15:41:43.739Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/4e/9c/8408129632f2cbd70018cff4a7eb78cc105b141de233b699feeda15d44c9/synchronicity-0.12.6-py3-none-any.whl", hash = "sha256:bc2bab6dde31f6bd9389912cef573b7fa7a5cb4a120ec786275e3284f06473ae", size = 41971, upload-time = "2026-10-02T15:41:42.672Z" }, +] + +[[package]] +name = "termcolor" +version = "3.3.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/46/79/cf31d7a93a8fdc6aa0fbb665be84426a8c5a557d9240b6239e9e11e35fc5/termcolor-3.3.0.tar.gz", hash = "sha256:348871ca648ec6a9a983a13ab626c0acce02f515b9e1983332b17af7979521c5", size = 14434, upload-time = "2025-12-29T12:55:21.882Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/33/d1/8bb87d21e9aeb323cc03034f5eaf2c8f69841e40e4853c2627edf8111ed3/termcolor-3.3.0-py3-none-any.whl", hash = "sha256:cf642efadaf0a8ebbbf4bc7a31cec2f9b5f21a9f726f4ccbb08192c9c26f43a5", size = 7734, upload-time = "2025-12-29T12:55:20.718Z" }, +] + +[[package]] +name = "tokenizers" +version = "0.23.3" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "huggingface-hub" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/e0/7c/2cabb2174e772636683008f2c5621949b645da7d303c596589e84516a184/tokenizers-0.23.3.tar.gz", hash = "sha256:cded33237c77caeef62944d32aa9a7ef42bdce2b3497e18d137e072a8c4be438", size = 385286, upload-time = "2026-10-09T10:16:55.759Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/aa/2e/4ce5b9716f26e526eff6b0502ebed4ea8d7161f03b3c77617c9f25528e97/tokenizers-0.23.3-cp310-abi3-macosx_10_12_x86_64.whl", hash = "sha256:9d2b5c97daf61688c2ad1803ca851800feaba50fb68d5821779e9ea5880d968c", size = 3148800, upload-time = "2026-10-09T10:00:51.457Z" }, + { url = "https://files.pythonhosted.org/packages/b2/72/01e49f032bb346e5aaf06c10c74fe8aeec847173adbadd66eb7c53054bf2/tokenizers-0.23.3-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:68649e97d5b43c44c031d8d848874a6eecae8f8fe40ea989aa777a5a83aca716", size = 3101381, upload-time = "2026-10-09T10:00:54.063Z" }, + { url = "https://files.pythonhosted.org/packages/15/fc/ae987741829b1cd547668c4c94be732ae3eefd1d74344e64c3d2ca714acd/tokenizers-0.23.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ec82e80e65a862275b97c3d90b7a523df8d9519ee48aeb4e9625b2cc909274e0", size = 3519944, upload-time = "2026-10-09T10:00:55.885Z" }, + { url = "https://files.pythonhosted.org/packages/1c/da/cc8f6c030afaf05fbddc608158fbb761dca46913cbeba6b112e59fc82e2a/tokenizers-0.23.3-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:c64a0713180ff16829d4e7f39a658b77ea11443af4e1aa46523692943c9b1414", size = 3397695, upload-time = "2026-10-09T10:00:57.444Z" }, + { url = "https://files.pythonhosted.org/packages/ec/f1/256f78d1365fa2cd3ea6db716883d74667c8cbb6a21f15fa5b89a773cdc2/tokenizers-0.23.3-cp310-abi3-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:ddedfd4b3b4be6be24ff6ca645c4a37fddfd305f6f3e354c54cf10b715c48215", size = 3753125, upload-time = "2026-10-09T10:01:00.165Z" }, + { url = "https://files.pythonhosted.org/packages/60/93/eee007ac2fcbf4ecfce7fbc354826cf3611f56bdb886f3e91b1f7dd06b8f/tokenizers-0.23.3-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:2a89614730d7b80940a5d2ed9320e1ec8add5a745c6151d8d05071b7215505b6", size = 4018598, upload-time = "2026-10-09T10:01:02.05Z" }, + { url = "https://files.pythonhosted.org/packages/bf/f9/0c96c4739461fce9d8d865b416728081bf6230022d7163bd6244f35f4b31/tokenizers-0.23.3-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:e88646b8580c5ad7f4361477f1298e9cc01771a1ee9aecfe32c47b8ff614cc38", size = 3602442, upload-time = "2026-10-09T10:01:03.77Z" }, + { url = "https://files.pythonhosted.org/packages/3a/40/6706b82693715581457c6d5423eaa7faae576bb0526c5738a57085eb4449/tokenizers-0.23.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:376851d22bcf9d650a5c3090bb83e6cf9e895fbf0595369fa4cd43c1f69b5f87", size = 3396193, upload-time = "2026-10-09T10:01:05.48Z" }, + { url = "https://files.pythonhosted.org/packages/fe/0c/85946de40e25b7364b8f1bcf56def129069acd5bb364b7c86a32919e1a23/tokenizers-0.23.3-cp310-abi3-manylinux_2_31_riscv64.whl", hash = "sha256:bf501c40b72d2d5c8623620210430e9cac1ce47a46e45b34107b70a1557d46b0", size = 3553483, upload-time = "2026-10-09T10:01:07.387Z" }, + { url = "https://files.pythonhosted.org/packages/f1/6b/8d615d92cad1d511ca5ab188d1c7c167f0b3d295cc0d96207f9f82d486d8/tokenizers-0.23.3-cp310-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:114e2b55ed177179d59f4ab98200a4471e11e78f9e4b5a922d146740f96fcf52", size = 9972248, upload-time = "2026-10-09T10:01:09.437Z" }, + { url = "https://files.pythonhosted.org/packages/c9/7d/a922e37ddd58d1b463bbc2ad08120c8f59c60b814cd353519a116b24f8ba/tokenizers-0.23.3-cp310-abi3-musllinux_1_2_armv7l.whl", hash = "sha256:d3407fb7b9c4d75dd68850ffd7180bc0a5d2dbaf0762d888e612f31fec3f9c6b", size = 9802957, upload-time = "2026-10-09T10:01:11.869Z" }, + { url = "https://files.pythonhosted.org/packages/4b/06/5d3f506a86ae0699a0e4ea05c05978f9aee169ef2c1d844e68c971cf8194/tokenizers-0.23.3-cp310-abi3-musllinux_1_2_i686.whl", hash = "sha256:84513ef0aeb8bf8f4ea11a2e8a7ac163ec5288aa115e649a59b470ac5c3107df", size = 10145487, upload-time = "2026-10-09T10:01:14.268Z" }, + { url = "https://files.pythonhosted.org/packages/26/e5/065625317690ea3548d834dad81f48ea1fd32e4964610e658e195d7fe28e/tokenizers-0.23.3-cp310-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:e05ab7baf7f47b406a95fea6f3b0a484b2ddcd9e1d14b68844c457eb755085a3", size = 10266026, upload-time = "2026-10-09T10:16:33.054Z" }, + { url = "https://files.pythonhosted.org/packages/77/4e/babede85d0d19f5e3deeef0063e01848141329934d3d77c31b5cab5ac2b4/tokenizers-0.23.3-cp310-abi3-win32.whl", hash = "sha256:1ebf28794e7e4954e20a7f70fbea410b2d1f0418f7dbbca97ca384fcfef38c25", size = 2588086, upload-time = "2026-10-09T10:16:35.686Z" }, + { url = "https://files.pythonhosted.org/packages/d1/6c/24f074c9a0efb98e61b20aafe6b2641922d5db24e447d5d6daffd9e17555/tokenizers-0.23.3-cp310-abi3-win_amd64.whl", hash = "sha256:1f0823bb00c5fdc98e487354d54dd55a03848d61a1a0bf29a68c77f24f3b26c3", size = 2872101, upload-time = "2026-10-09T10:16:37.533Z" }, + { url = "https://files.pythonhosted.org/packages/53/77/a476b6f73a661c11d113a342d2326b91506cf2285f0995d1212a6bb2022d/tokenizers-0.23.3-cp310-abi3-win_arm64.whl", hash = "sha256:7e48734d2de9260d86f03ab056d2cfeeff3869f61dbd49aaa15a2793b5f3458b", size = 2742580, upload-time = "2026-10-09T10:16:39.244Z" }, +] + +[[package]] +name = "toml" +version = "0.10.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/be/ba/1f744cdc819428fc6b5084ec34d9b30660f6f9daaf70eead706e3203ec3c/toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f", size = 22253, upload-time = "2020-11-01T01:40:22.204Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/44/6f/7120676b6d73228c96e17f1f794d8ab046fc910d781c8d151120c3f1569e/toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b", size = 16588, upload-time = "2020-11-01T01:40:20.672Z" }, +] + +[[package]] +name = "tqdm" +version = "4.70.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "colorama", marker = "sys_platform == 'win32'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/0d/ea/b2a5bd54b28a324dae8211928b2d730b6547500342c7e6c6dea08bd0a485/tqdm-4.70.1.tar.gz", hash = "sha256:cefd0eca11b2a37a3aee776544d4f4ae913f02688135b5556b8788dfa474afc4", size = 171846, upload-time = "2026-09-11T07:25:16.601Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/a7/03/921a3d3c75785aca9ebfbfcabfbc3a1be12e2ab5265deb026d55a5a3f83e/tqdm-4.70.1-py3-none-any.whl", hash = "sha256:c293e525e6fef9c20e8728fd4612df02a0aa31bb5fe91ecd93e123b1b7bffa73", size = 80199, upload-time = "2026-09-11T07:25:14.599Z" }, +] + +[[package]] +name = "transformers" +version = "5.20.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "huggingface-hub" }, + { name = "numpy" }, + { name = "packaging" }, + { name = "pyyaml" }, + { name = "regex" }, + { name = "safetensors" }, + { name = "tokenizers" }, + { name = "tqdm" }, + { name = "typer" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/8c/4e/3cd6f08cc84ee612d40f3cdb1649475e7c997f2d5cbd95ade8780708af62/transformers-5.20.2.tar.gz", hash = "sha256:080871e061546d720be57f5846fce426f46b44d61574907cc7404c2cba49d180", size = 10225379, upload-time = "2026-10-19T17:46:03.092Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/96/b6/724faefed06913dd3e42e0476fe2c42448675c42f687a12bf810aa098005/transformers-5.20.2-py3-none-any.whl", hash = "sha256:7ae75dbc1fb3922aff1b6c97b9e5479d6cf26fd86a8866bfb71324190479f807", size = 12844921, upload-time = "2026-10-19T17:45:59.293Z" }, +] + +[[package]] +name = "truststore" +version = "0.10.5" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/ee/9f/c5201d42a484c061e528825fc8e2d565f5abd50a4ced6fb7d29c4ec99b2b/truststore-0.10.5.tar.gz", hash = "sha256:30d36967ccaded5cbb38d602c433f53600036c79d502f4533a49b60a03bbefcd", size = 28091, upload-time = "2026-10-12T22:27:31.808Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/51/e9/3a7820be2bb0fe53b6bc9c3be26d3d1158004e4c3ab953aa6840b955b1e9/truststore-0.10.5-py3-none-any.whl", hash = "sha256:9aaaedaefaf06d8b206278cf8b5012bc897f485a874503501e12d776df78951c", size = 19017, upload-time = "2026-10-12T22:27:30.377Z" }, +] + +[[package]] +name = "typer" +version = "0.27.3" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "annotated-doc" }, + { name = "colorama", marker = "sys_platform == 'win32'" }, + { name = "rich" }, + { name = "shellingham" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/03/51/d33db42cc72ffd8c30777547b42d01f0cbf9d95a770457698d0174b3ed71/typer-0.27.3.tar.gz", hash = "sha256:d0396f770a560ab1b0a8504e13b5f254b728cedb05c61cf0359e944e50ce8901", size = 205303, upload-time = "2026-10-06T17:24:16.61Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/07/ea/2e31b67051e91a133189e9c000c222502ddc6969856416de0d095de4c0b0/typer-0.27.3-py3-none-any.whl", hash = "sha256:e50022f28b82a86313e54501317a1db64bf8f8d036ff8cfe5ca7e47675454aff", size = 123312, upload-time = "2026-10-06T17:24:15.054Z" }, +] + +[[package]] +name = "types-certifi" +version = "2021.10.8.3" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/52/68/943c3aeaf14624712a0357c4a67814dba5cea36d194f5c764dad7959a00c/types-certifi-2021.10.8.3.tar.gz", hash = "sha256:72cf7798d165bc0b76e1c10dd1ea3097c7063c42c21d664523b928e88b554a4f", size = 2095, upload-time = "2022-06-09T15:19:05.244Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/b5/63/2463d89481e811f007b0e1cd0a91e52e141b47f9de724d20db7b861dcfec/types_certifi-2021.10.8.3-py3-none-any.whl", hash = "sha256:b2d1e325e69f71f7c78e5943d410e650b4707bb0ef32e4ddf3da37f54176e88a", size = 2136, upload-time = "2022-06-09T15:19:03.127Z" }, +] + +[[package]] +name = "types-toml" +version = "0.10.8.20260518" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/4b/11/6ece999e91f2ccb848ab4420f3f4816e78ac0541f739e6864affdaaa5737/types_toml-0.10.8.20260518.tar.gz", hash = "sha256:80e10facd24fdeda9d5c672187d72be3ac284843788d67f5aae59e3e016db6fe", size = 9419, upload-time = "2026-05-18T06:02:16.719Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/91/25/489751806bf5c95e4007f8e17409199c54d31e49ffbea07c5729b1286c8e/types_toml-0.10.8.20260518-py3-none-any.whl", hash = "sha256:0e564ab05f6fde62a315b3b5a9b6624fda569399795d30a37e64705a70459303", size = 9669, upload-time = "2026-05-18T06:02:15.86Z" }, +] + +[[package]] +name = "typing-extensions" +version = "4.16.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f6/cc/6253133b5bb138fc3306cebfbda2c520f545d36b5be2c7255cc528bb45d6/typing_extensions-4.16.0.tar.gz", hash = "sha256:dc983d19a509c94dba722ee6abd33940f7c05a89e243c47e907eb4db6f1a43e5", size = 113555, upload-time = "2026-07-02T08:40:05.92Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/49/d3/b8441a820a491ddfc024b0b0cf0393375b75ea13866d9c66727e54c2fc80/typing_extensions-4.16.0-py3-none-any.whl", hash = "sha256:481caa481374e813c1b176ada14e97f1f67a4539ce9cfeb3f350d78d6370c2e8", size = 45571, upload-time = "2026-07-02T08:40:04.659Z" }, +] + +[[package]] +name = "typing-inspection" +version = "0.4.4" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/a3/26/b09b8010994eccc3c09092e6b34058f36a460eea2d4c3e8b910c695975a0/typing_inspection-0.4.4.tar.gz", hash = "sha256:547274fa6b0a561ccf549cc9524b999a578e737d015d8709d021f9d0d13bea47", size = 76928, upload-time = "2026-08-12T12:37:25.997Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/67/81/4add07e5172b7ac40d8ed5ff580409a7801a4fe26d529bdd915401dabfbe/typing_inspection-0.4.4-py3-none-any.whl", hash = "sha256:65b8397ba37ccbce054456aaccddfc91e6e3083c92824df348d96ca832f3f147", size = 14750, upload-time = "2026-08-12T12:37:24.648Z" }, +] + +[[package]] +name = "tzdata" +version = "2026.5" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d9/68/f1b440335057bfce71b6e50a9d09445aa2ecbd08359a337976627b8409e7/tzdata-2026.5.tar.gz", hash = "sha256:8cc73c0a0bfca7dbfa59235d60b2eff82231dee33f53d206db1acd9173cfc0a7", size = 200404, upload-time = "2026-10-03T09:23:14.143Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/94/21/1e5995a1c920cce14e4bffae20c665ec10e7ed03ab25e006cd741092b718/tzdata-2026.5-py2.py3-none-any.whl", hash = "sha256:b683bd1b6659ddcd810ff02ad09ba821d4bf1065072805063eb35c49617905ac", size = 347996, upload-time = "2026-10-03T09:23:12.535Z" }, +] + +[[package]] +name = "watchfiles" +version = "1.3.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b3/68/e6aa0b77d217b31f8f486ec0cdfe5e00e6e38dc0be657e7d85819b9faf0a/watchfiles-1.3.0.tar.gz", hash = "sha256:99aee4a07847c06820765fd7b1b49ceac4f3f711ccb7d104655a33231de1c207", size = 108119, upload-time = "2026-09-21T09:08:55.664Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/68/fa/c0b840d5d8bafe640925408ce11095948c8945bb61be1e67d0ab629b872a/watchfiles-1.3.0-cp310-abi3-macosx_10_12_x86_64.whl", hash = "sha256:000b9688fc8133037a8b075c8ebf98f32844ff8964dda61e85c1db547dafc441", size = 399703, upload-time = "2026-09-21T09:08:13.518Z" }, + { url = "https://files.pythonhosted.org/packages/c7/8a/894799b485fe9473ad10422a0d9668e53fb78e3a2b5cc6061159572844ad/watchfiles-1.3.0-cp310-abi3-macosx_11_0_arm64.whl", hash = "sha256:bbc1198edfdc90fda0600f825aa94150f428dfcbf8138746f55998e0e660d64c", size = 397647, upload-time = "2026-09-21T09:08:15.16Z" }, + { url = "https://files.pythonhosted.org/packages/5a/61/f277cd5ead05f5d37b98b41fbdd0c943ee0c04d49738f62e902cb291cf43/watchfiles-1.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4af9c464d410e8c44b58ddf1cbec7c3a02c1cb2c6c80df9c74659ba2fbda0ab5", size = 454994, upload-time = "2026-09-21T09:08:16.587Z" }, + { url = "https://files.pythonhosted.org/packages/46/66/ea4c01382975e53b1ac1b0a0ea1b353416b8127fb6e04f5c1c313119aab6/watchfiles-1.3.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:eb94e40b9a0db19636b5e3fed0a9803452ecc7381df13b385272abb779ee83ca", size = 460105, upload-time = "2026-09-21T09:08:17.961Z" }, + { url = "https://files.pythonhosted.org/packages/31/c3/1a501b597817096c9831c255f6270c124ce0e2faeb74124be24dcc93b86d/watchfiles-1.3.0-cp310-abi3-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:7398930a2b76b9dc67a4e6b8bfc7baf30946892a86f417a3920c07bdd6febc30", size = 493761, upload-time = "2026-09-21T09:08:19.327Z" }, + { url = "https://files.pythonhosted.org/packages/5a/16/77352d05e152c7786a016e9f1decdb4507e8aa5bcf3343c72142a0413d14/watchfiles-1.3.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:025b108f2d5cc2799cb941f79380df3e8b22bfb589c218e801767fe78d598c95", size = 577500, upload-time = "2026-09-21T09:08:20.723Z" }, + { url = "https://files.pythonhosted.org/packages/95/fe/81001c55b24466fe9911135e50269fd3a29f43e446627a92adfab54553b6/watchfiles-1.3.0-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:be9ef3cd403d756a304a0a08a112163c63da7ec72c8c9d083c98818291ff8f29", size = 469209, upload-time = "2026-09-21T09:08:22.346Z" }, + { url = "https://files.pythonhosted.org/packages/8a/cf/e3ad894ed5909a7c44b0707e89bc25d19a3d78b3a7aec846119443b6ae56/watchfiles-1.3.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b5768b49e426fd5b550b012c866db347cdf15c398ef98dc557b6e6b72fa74cd1", size = 458353, upload-time = "2026-09-21T09:08:23.7Z" }, + { url = "https://files.pythonhosted.org/packages/7f/f5/2f106829e6e2bc91ad74614730f6df484202b9a4d20d927dbf2f08d37d44/watchfiles-1.3.0-cp310-abi3-manylinux_2_31_riscv64.whl", hash = "sha256:d36c72fcdc08f143d87316a4c4c3b064e340da2c7383d39803950740ed418935", size = 468707, upload-time = "2026-09-21T09:08:25.167Z" }, + { url = "https://files.pythonhosted.org/packages/15/37/2741ab776b978669c1f3bba1e573f1a184f727ab8b4d3a6d9c61090800ed/watchfiles-1.3.0-cp310-abi3-musllinux_1_1_aarch64.whl", hash = "sha256:f0c9865240e065a2247f4b5248528ae3d01e0e8ae071462c250c884dc16e3ee3", size = 632510, upload-time = "2026-09-21T09:08:26.532Z" }, + { url = "https://files.pythonhosted.org/packages/48/64/db4a4684275fe9b696ee5118c194705c1423b3d42181c3f8ee70194d5c73/watchfiles-1.3.0-cp310-abi3-musllinux_1_1_x86_64.whl", hash = "sha256:380f513d26cc2e598266b88d66456e07d67ef4be8f0f435a1154d9c44b43d509", size = 661723, upload-time = "2026-09-21T09:08:28.004Z" }, + { url = "https://files.pythonhosted.org/packages/52/80/95ddb24f6a2f595c031c24432ab7b8aa7d6d1ada0c6d63a552166e06e8d1/watchfiles-1.3.0-cp310-abi3-win32.whl", hash = "sha256:509d9f74d2bec5c1f4cd868ddcfe0c9601f7ea53066dfcb19d8ede742c4de661", size = 277761, upload-time = "2026-09-21T09:08:29.335Z" }, + { url = "https://files.pythonhosted.org/packages/48/a1/1c4a1b3c8030cd03206232612be8a6e8799093490c220b0fcb935d02b75a/watchfiles-1.3.0-cp310-abi3-win_amd64.whl", hash = "sha256:1acabde19b67e673274e89a04d613b7fb1f122e2c4b8ec9a581e59c149264b61", size = 290643, upload-time = "2026-09-21T09:08:30.45Z" }, + { url = "https://files.pythonhosted.org/packages/82/e0/d2db577c632d26d08078c4390dd870b8555c4e558df25d22270f0166c71f/watchfiles-1.3.0-cp310-abi3-win_arm64.whl", hash = "sha256:d978cc1dd7ba44f5590d7de74f7a9c8be7262d9e469c2c0efb8bbfc2574321dc", size = 285510, upload-time = "2026-09-21T09:08:31.561Z" }, +] + +[[package]] +name = "xxhash" +version = "4.0.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/39/e6/ed6c428055ffd3f9299db3ec8671fd78b70c24173c040b8faafb6348ebbb/xxhash-4.0.2.tar.gz", hash = "sha256:af3d1b3cfa5280e84d8968e9f30c9eca75781db4915eb6d6acb0dd4fc93e0f47", size = 101549, upload-time = "2026-10-19T06:00:08.459Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/58/32/7f74a5661a3029102178950b24f9b1baa259429f97a5d8ef855935472f58/xxhash-4.0.2-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:cb66d346593cb6851498b9ed554582aede153e32c9835edb76995746e563ac6b", size = 38428, upload-time = "2026-10-19T05:59:15.002Z" }, + { url = "https://files.pythonhosted.org/packages/bf/79/0ad1d456bbfb30761f6fc174dca610dcd46ef1280a708ce1bdb5de8b5368/xxhash-4.0.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:aeda53dfb6bcae94adb1f5df4246fe13b9c4202f531dd9d39e0d55b36ba8db0e", size = 36174, upload-time = "2026-10-19T05:58:05.938Z" }, + { url = "https://files.pythonhosted.org/packages/30/fd/ea481dcc75bdacdb4d4b1039007bc60a0c0d7cd999d28e3e19b61099a6fb/xxhash-4.0.2-cp312-cp312-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:e956b1a4e8fd8ea6fdb823670bf6ee3eade44c5ba3bbca06aa67805e4815a9e6", size = 253093, upload-time = "2026-10-19T06:01:20.933Z" }, + { url = "https://files.pythonhosted.org/packages/19/9c/39619aa3a1d569ff5fd8b5ea34600451310a63bf23d158f74a4fbc9fdcd3/xxhash-4.0.2-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:1fce8df7958f0828bd78d2ecfd9b11510abff9edc0e28fa7f7ca0ddcee2f0a8b", size = 276506, upload-time = "2026-10-19T05:59:15.76Z" }, + { url = "https://files.pythonhosted.org/packages/9e/01/d9e445ac597f8b6acd6aefdfa21230919e9aa7f14bee3ee0d634c3f3bcb7/xxhash-4.0.2-cp312-cp312-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:1ab636ddc4f257bb2713c078d3f6112f194e92d8a32442233ea957fdddb51de6", size = 297700, upload-time = "2026-10-19T05:58:05.221Z" }, + { url = "https://files.pythonhosted.org/packages/62/14/e7ceb1cdb9a643ebf6ca6d8b3a63b9bb43dbee8b362d0c6f9b3dd8318dec/xxhash-4.0.2-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:c9f9c25738cbab7ca51763f724db95bc7f50f3174793389fee7f97e154bd9c83", size = 280234, upload-time = "2026-10-19T05:59:55.319Z" }, + { url = "https://files.pythonhosted.org/packages/be/16/9c30c6d7caafb654632a5fdfb45dfe4133254749dfaccef88bcdd033efe1/xxhash-4.0.2-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:60f30791faec3e96c8ed5921045231f7d3355649af47809e5a75c8a17dba8b6b", size = 511017, upload-time = "2026-10-19T05:58:25.71Z" }, + { url = "https://files.pythonhosted.org/packages/b2/c6/3a85fc0b821ed2dfcf6fb7cfdf07e0b261ada40374583a50abb3480f2863/xxhash-4.0.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:7c7c95dba490be3b4e5a2fa5595b4d4f088fe7b8726e5998b8c1d83b1f9eb3e4", size = 261797, upload-time = "2026-10-19T05:59:17.727Z" }, + { url = "https://files.pythonhosted.org/packages/bc/75/dce80778193b5d099ba01bc03725bf8095280e5b6ec434fdcc62124f7ecc/xxhash-4.0.2-cp312-cp312-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:f53479d8e13f93e1e3ef0e933a1901cbb4d8745c61c3876ff5b7c544e40f13ed", size = 339860, upload-time = "2026-10-19T05:58:07.481Z" }, + { url = "https://files.pythonhosted.org/packages/55/8a/5fc8b33217e25a37f8dfad398f602d404781535759f7d9578871349bb476/xxhash-4.0.2-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:d42a7b02f204d5133d38d173766ff47a5da46fcf2c3876718b819f1f5500a55f", size = 272850, upload-time = "2026-10-19T06:01:22.258Z" }, + { url = "https://files.pythonhosted.org/packages/47/f9/ddf4f2bbc143886edd7c66e73c0adc6be9fb9f138a12c77bea2665be744c/xxhash-4.0.2-cp312-cp312-musllinux_1_2_armv7l.whl", hash = "sha256:6049b09b27d9226dfe0a8b0dee2b867063c8df9a7ef64748bf48110d8fc896c2", size = 301421, upload-time = "2026-10-19T05:59:18.262Z" }, + { url = "https://files.pythonhosted.org/packages/ac/ad/862e0c20ca8cbbe624e5c168ca0fccd0ab6b1e83d13dd9b4aa8b3e575264/xxhash-4.0.2-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:97903f3df852ca5840a26cc27beef15f9f93b0071e2aa550a03d8ee5c65e7765", size = 260139, upload-time = "2026-10-19T05:58:06.923Z" }, + { url = "https://files.pythonhosted.org/packages/f4/19/4c5a3b6698504159b7908662dc61b1f3ebed25135235a023a088a5483e85/xxhash-4.0.2-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:873ee53a7d8f020ddfcd3bc101707ae46fcd1eb702f809ff29a765d0f88b654f", size = 278211, upload-time = "2026-10-19T05:59:57.7Z" }, + { url = "https://files.pythonhosted.org/packages/86/92/732f5f9810132d6f9afb19160980a48d1fa51caa724352a4f170bf960a4a/xxhash-4.0.2-cp312-cp312-musllinux_1_2_riscv64.whl", hash = "sha256:e24e9093771e8a27089176f202991199ebedad8f4791a6e2abf88acd5c69bd9d", size = 330247, upload-time = "2026-10-19T05:58:29.31Z" }, + { url = "https://files.pythonhosted.org/packages/f6/e2/137dbd91bd93281f45bf53e4319874a09692bc07380755913e4dd938db49/xxhash-4.0.2-cp312-cp312-musllinux_1_2_s390x.whl", hash = "sha256:c06c5fd89304f2c274d5129bd3a555b78f7a8af5ea16edd5b1360010f3359bd4", size = 478537, upload-time = "2026-10-19T05:59:20.059Z" }, + { url = "https://files.pythonhosted.org/packages/7f/9b/41b99dfc87602358fc4dbfa1517b7f72b5a9232bce1aa32d4208acaeb160/xxhash-4.0.2-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:c37fcacb7f5b8590c822bbf3bc41690e40a7581d8c4bcea7b15148f0743e716d", size = 258155, upload-time = "2026-10-19T05:58:09.407Z" }, + { url = "https://files.pythonhosted.org/packages/d0/b3/2bfc3c9121a522f6a5abb8f57800345c23a159822b64c7ea69debdf4846d/xxhash-4.0.2-cp312-cp312-pyemscripten_2024_0_wasm32.whl", hash = "sha256:79cfd336c2ecaceef1061e7a7b3da079eab5920ba1352a28be690e857f67f2ea", size = 20749, upload-time = "2026-10-19T06:01:23.513Z" }, + { url = "https://files.pythonhosted.org/packages/cc/25/85d482d35d3f778d25719fc704146e8c984955d44735cfbdf955db240c23/xxhash-4.0.2-cp312-cp312-win32.whl", hash = "sha256:515a5f4405a29bc447bcee2ee13455dfc67ce41bf8f1da5258bc75bbdfb6dee6", size = 34657, upload-time = "2026-10-19T05:59:20.564Z" }, + { url = "https://files.pythonhosted.org/packages/11/63/1beef4ee02cad4b8f97d707c6632e80f35e935c6b880960985e038f42aba/xxhash-4.0.2-cp312-cp312-win_amd64.whl", hash = "sha256:043dcb74ba30a068a2821c136b61262db67f1e1d174ff4dbe83a3e24996861e7", size = 37059, upload-time = "2026-10-19T05:58:08.545Z" }, + { url = "https://files.pythonhosted.org/packages/3d/4d/075f92b562ac13469edd51acb676ac4dbfa3d85a03f842e4a026e29a5ae1/xxhash-4.0.2-cp312-cp312-win_arm64.whl", hash = "sha256:202c18875cf93d652c506d0e7c8a50847713f963a8aa9c06ce5117bdd14ba771", size = 33219, upload-time = "2026-10-19T06:00:00.128Z" }, + { url = "https://files.pythonhosted.org/packages/4e/ab/f5e56048d06c1a42834978a4c17a004a202b092e2f72a632e68868fb9f52/xxhash-4.0.2-graalpy312-graalpy250_312_native-macosx_10_13_x86_64.whl", hash = "sha256:fe5a0d96cb339d383d3f720ffe94d8a7a51ccc8abf4d348a3ee058b5625f948e", size = 31816, upload-time = "2026-10-19T06:00:30.318Z" }, + { url = "https://files.pythonhosted.org/packages/17/b1/2351ddc0b45f983f5ea1d8b91024c932ce939a271b08d7f3bc8a54b3a81b/xxhash-4.0.2-graalpy312-graalpy250_312_native-macosx_11_0_arm64.whl", hash = "sha256:86fdc2ae36f5d11446896c4456883b3740be2ec3ff56f700a7dc7aeb635e236f", size = 34431, upload-time = "2026-10-19T05:59:16.424Z" }, + { url = "https://files.pythonhosted.org/packages/7d/9f/dc3d43fa9ae82435a5fae9647925e40ca2e6198f279a044f6119542cbf33/xxhash-4.0.2-graalpy312-graalpy250_312_native-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:792e7c12ac0d300908a99f644e583f4091c3a5a0035ab7b1b76aadc908480381", size = 38470, upload-time = "2026-10-19T06:00:01.225Z" }, + { url = "https://files.pythonhosted.org/packages/8b/52/45d2a2ac310da915e1ef06cc3a823d904d49c7049fdd830c46b8f5c038bc/xxhash-4.0.2-graalpy312-graalpy250_312_native-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:e359ff2f38be3c856a129c1c5c4cbd70e219188456c1bf266fd4f94e2baae278", size = 38187, upload-time = "2026-10-19T05:58:54.013Z" }, + { url = "https://files.pythonhosted.org/packages/84/77/a8779629eac4fd450dfd0f5a507941419dddb1a2b088288b790c497102bf/xxhash-4.0.2-graalpy312-graalpy250_312_native-win_amd64.whl", hash = "sha256:6e736b549811153e3b54779708725c68e105a6e6860f9f30fcba896a5f28ef0c", size = 37109, upload-time = "2026-10-19T06:01:51.414Z" }, + { url = "https://files.pythonhosted.org/packages/2c/80/6608694dd7817963db9d9c611143d94888c3e7420d8901c962acd7fdfc1c/xxhash-4.0.2-pp312-pypy312_pp80-macosx_10_15_x86_64.whl", hash = "sha256:c9f3129e086297db6b24b83db59a0dbfdd8f325ffc7dcc184fc573473b13293f", size = 36460, upload-time = "2026-10-19T06:00:34.489Z" }, + { url = "https://files.pythonhosted.org/packages/a3/d7/a1acbd775c01f720bbe6368af480e26e3b40525a7eaf17810c0ce5e9e13f/xxhash-4.0.2-pp312-pypy312_pp80-macosx_11_0_arm64.whl", hash = "sha256:2acdb1cabc41140e97d4adba7422dbeb7fabef9caace27d56f8de662d505fdfa", size = 33690, upload-time = "2026-10-19T05:59:23.349Z" }, + { url = "https://files.pythonhosted.org/packages/9b/13/15190b981f474117adefe78cd30acceb24e6f83352069402d3d9c24cd53b/xxhash-4.0.2-pp312-pypy312_pp80-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:2076eee9f06fcb611754f98b9c272046e4a8a3fcfde353575dd8e6e2f33e80ed", size = 47950, upload-time = "2026-10-19T06:00:07.951Z" }, + { url = "https://files.pythonhosted.org/packages/b9/ff/28666972b4a78dca99a537cbd42abe50dfdeca4d6388984c841cfe1f933f/xxhash-4.0.2-pp312-pypy312_pp80-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:7b244e3e1d6eadb5061ce777632a822ed75c2fb6174c65882f5829f31b46e8c1", size = 42584, upload-time = "2026-10-19T05:59:00.064Z" }, + { url = "https://files.pythonhosted.org/packages/0a/30/0b46bb908376d75733ca70cd4124f74abeea0f50732749dafa848f42f385/xxhash-4.0.2-pp312-pypy312_pp80-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:63522739ce94e4e73c1605aa60a52b3863e77e835f6292cc8a5e7e7b626ff00c", size = 39472, upload-time = "2026-10-19T06:01:55.015Z" }, + { url = "https://files.pythonhosted.org/packages/4c/23/6c0aa47774513c1237f7ea198daf36f2d2609f1e233249a9ea97d5b55c38/xxhash-4.0.2-pp312-pypy312_pp80-win_amd64.whl", hash = "sha256:0b96b2694c97084c5e2bff8a6a3dc3c2fb3b9a07178286e8d69869efe76eeb50", size = 84214, upload-time = "2026-10-19T06:00:06.386Z" }, +] + +[[package]] +name = "yarl" +version = "1.25.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "idna" }, + { name = "multidict" }, + { name = "propcache" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/75/16/e8be8e2fb175bbf41a0680381a319f1199fae256588241a2ac8677eafb49/yarl-1.25.1.tar.gz", hash = "sha256:03dd38de09bc213e9a8b29761eec33ee1d5318dac0e49d8af36e4d27830e23a7", size = 246245, upload-time = "2026-09-15T19:35:02.264Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/75/b3/cd32ac66ae622b854c2df0ac52106dda220d361b65a64fde7d5b3684aa3f/yarl-1.25.1-cp312-cp312-macosx_10_13_universal2.whl", hash = "sha256:94d7aa6debf92a1dd14cb5280b083a764169a13cfb23a452111160274ed989f4", size = 144798, upload-time = "2026-09-15T19:31:01.821Z" }, + { url = "https://files.pythonhosted.org/packages/61/fb/a2c52a8007c2051ba74662afb112ecf3d00346af4c25e33df9d80fd14fb8/yarl-1.25.1-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:83d4a37e4b95da4d8bda930d6d35b75b4cdadbacbb4980cae290ea3100b5d51d", size = 104583, upload-time = "2026-09-15T19:31:04.05Z" }, + { url = "https://files.pythonhosted.org/packages/be/dd/ee38aec8e09fdf957e50d4085453fbe202f56c6c3b4cf07b81cdb4f09ee9/yarl-1.25.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:e029648f9c951db30e98a7d7ec90835db88ec4b32820efe2a9bdc2287e032eb6", size = 104325, upload-time = "2026-09-15T19:31:06.338Z" }, + { url = "https://files.pythonhosted.org/packages/1e/b3/058dbfb1857b484c9cf9cc135659f50b85ce66e03c99e44dc2f7b6161f55/yarl-1.25.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:4d781294bb815ecb5ea57ff6bbf8038e0a31a95fdf3e1788f66e0dc100d64b58", size = 115358, upload-time = "2026-09-15T19:31:08.593Z" }, + { url = "https://files.pythonhosted.org/packages/db/39/29693446cf0cf6b15a0e2f75a5d40f93c56819b05b0622196f45e95b5cc0/yarl-1.25.1-cp312-cp312-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:e12c538e00e7c1b286a07061046b90e8124e6a9793efae2c70db6a4aad07faad", size = 107658, upload-time = "2026-09-15T19:31:10.802Z" }, + { url = "https://files.pythonhosted.org/packages/86/b3/3c4dd7e1af43b931fba95e0a722737f2ea94a6d199c802585282831d7abd/yarl-1.25.1-cp312-cp312-manylinux2014_ppc64le.manylinux_2_17_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:7e4de3ac4adbad3d0bc7c6f4360a7dbff5de2f15e3b723be3198074e17fd9c40", size = 122660, upload-time = "2026-09-15T19:31:12.84Z" }, + { url = "https://files.pythonhosted.org/packages/bd/b5/1b60dbc3cfc9c5712b15148c206748f2bc93953ffdbe25ea75b63dfc89c9/yarl-1.25.1-cp312-cp312-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:419f392a1da624877975709e3864dfe833af6cc7671b39318086d456e288380c", size = 126506, upload-time = "2026-09-15T19:31:15.088Z" }, + { url = "https://files.pythonhosted.org/packages/bc/7b/ca212cbe170ac8b96e45317ecbcf9c3c3ecf0cdec98d5b088a9c4088929b/yarl-1.25.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c6f117789d22dce188e5754e8bc65b7e6ebf8cb73963b9fa761f672a5883769d", size = 117050, upload-time = "2026-09-15T19:31:17.241Z" }, + { url = "https://files.pythonhosted.org/packages/cb/c3/72b4938cdbe619ad71ac156182faef4908846b84dc3ca4dbb4c4e6f84014/yarl-1.25.1-cp312-cp312-manylinux_2_31_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:80e47012e730da131c9f059c80936783f9659aae22dc31c03c0595590d11ed54", size = 114174, upload-time = "2026-09-15T19:31:19.294Z" }, + { url = "https://files.pythonhosted.org/packages/e8/43/268717870f9ba0cc9701a95181587f6dc8c5f387aab4aeecc83158f38a79/yarl-1.25.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:e80f557716fd765439577131e526b8942ffc2c07bdbc5e39fa62f660ba1e963f", size = 114944, upload-time = "2026-09-15T19:31:21.414Z" }, + { url = "https://files.pythonhosted.org/packages/da/84/baa5bf504d51fe062c4bcaf62936da97fffb43285978d0b39984824231fd/yarl-1.25.1-cp312-cp312-musllinux_1_2_armv7l.whl", hash = "sha256:f61964f235a43738bfac50da46fc4254943a7eea3051aeb0b6fc7c992c29fadc", size = 108263, upload-time = "2026-09-15T19:31:23.388Z" }, + { url = "https://files.pythonhosted.org/packages/a4/28/779a2ed9e0152a601a27039bed9aead3f0b79797a67e2c44bfa444622dd8/yarl-1.25.1-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:e546fe1d4a93ebc2910f0d768baff19faa09843ab3f2036a67ed6e69fae4419d", size = 122184, upload-time = "2026-09-15T19:31:25.343Z" }, + { url = "https://files.pythonhosted.org/packages/f8/1f/118e9e5b8f07694d63fd3222e801d7782270003f1a222aa798df3f8d5933/yarl-1.25.1-cp312-cp312-musllinux_1_2_riscv64.whl", hash = "sha256:cce0727fd5ac04d372fa9bbfde9febc2bcf209aadfcf0468e45dec72719895d1", size = 114001, upload-time = "2026-09-15T19:31:27.465Z" }, + { url = "https://files.pythonhosted.org/packages/0f/ae/a4cf1cf372313734b17996d4007f9f73596e7a178b9485802e5494ecf484/yarl-1.25.1-cp312-cp312-musllinux_1_2_s390x.whl", hash = "sha256:af4ea5b37403ef4e30f3927eaed540db942bde01d8d3ff083527c0704d1c9c68", size = 120565, upload-time = "2026-09-15T19:31:29.47Z" }, + { url = "https://files.pythonhosted.org/packages/05/79/ad94f93ca731bc9e44d321833ab96b82a4f9f5f63cf773f81a4aeea5ecc1/yarl-1.25.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:68782fdb4027b8d1eee25ec35e9a6db05e863b899eb0310b3a33b6c3fef55707", size = 117060, upload-time = "2026-09-15T19:31:31.367Z" }, + { url = "https://files.pythonhosted.org/packages/bb/cc/51a7b4abf4ac593b8e7eb3794b28e5a35ae26eed8bc04787628d215af82f/yarl-1.25.1-cp312-cp312-win_amd64.whl", hash = "sha256:7d575b54cb3863ef9bc290ea4b009999d55dc237326131e4853cf33e888fee03", size = 102593, upload-time = "2026-09-15T19:31:33.329Z" }, + { url = "https://files.pythonhosted.org/packages/9d/21/0941a6b93a58b59a1ec75e5333bf06929b671309c43c0cd201c172d9c39f/yarl-1.25.1-cp312-cp312-win_arm64.whl", hash = "sha256:bc3ac7bf569f6b64dad04dd7808c7872dae8a97df657856eac05e9b7e3614a85", size = 97697, upload-time = "2026-09-15T19:31:35.855Z" }, + { url = "https://files.pythonhosted.org/packages/54/22/318c7980066769c6bcd9221ed2248294f5698811da099013098c670565ed/yarl-1.25.1-py3-none-any.whl", hash = "sha256:681c758b0490f9e96b78e5fa8e8dc6e648e9185bb6eaebe73183c33ea0c445f3", size = 63617, upload-time = "2026-09-15T19:34:59.616Z" }, +] diff --git a/training/watchdog_policy.py b/training/watchdog_policy.py new file mode 100644 index 00000000..dce37d24 --- /dev/null +++ b/training/watchdog_policy.py @@ -0,0 +1,87 @@ +"""Deterministic stop rules for a registered Balatro training run.""" + +import math +import statistics + + +def summarize_games(rollout): + """Count each game once even when its trajectory spans multiple samples.""" + games = {} + for sample in rollout.get("samples", []): + game = (sample.get("metadata") or {}).get("balatro") or {} + if "episode_id" in game: + games[game["episode_id"]] = game + if not games: + return None + values = list(games.values()) + turns = sum(game["turns"] for game in values) + invalid = sum(game["invalid_actions"] for game in values) + return {"rollout_id": rollout.get("rollout_id"), "games": len(values), + "mean_reward": statistics.mean(game["episode_reward"] for game in values), + "mean_ante": statistics.mean(game["final_ante"] for game in values), + "wins": sum(bool(game["won"]) for game in values), + "turns": turns, "invalid_actions": invalid, + "invalid_fraction": invalid / max(1, turns)} + + +def merge_chunks(chunks): + """Match Modal Dojo's last-writer-wins scalar metric mirror.""" + table, written = {}, {} + for chunk in chunks: + for key, metrics in chunk.get("steps", {}).items(): + step = int(key) + row, stamps = table.setdefault(step, {}), written.setdefault(step, {}) + incoming = chunk.get("written", {}).get(key, {}) + for name, value in metrics.items(): + at = incoming.get(name, 0) + if name not in row or at > stamps.get(name, -1): + row[name], stamps[name] = value, at + return table + + +def assess(table): + train, evaluations, outputs = {}, {}, {} + for step in sorted(table): + row = table[step] + if "train/step" in row: + train[int(row["train/step"])] = row + if "eval/step" in row and "eval/eval" in row: + evaluations[int(row["eval/step"])] = row["eval/eval"] + if "rollout/step" in row and "rollout/action_count" in row: + outputs.setdefault(int(row["rollout/step"]), {}).update(row) + completed = max(train) + 1 if train else 0 + history = [{"eval_step": step, "reward": evaluations[step]} for step in sorted(evaluations)] + result = {"completed_updates": completed, "evaluations": history, + "decision": "continue", "reason": None} + for step, row in train.items(): + for name in ("train/loss", "train/grad_norm", "train/train_rollout_logprob_abs_diff"): + if name in row and not math.isfinite(row[name]): + return {**result, "decision": "stop", "reason": f"Nonfinite {name} at update {step + 1}"} + if any(not math.isfinite(item["reward"]) for item in history): + return {**result, "decision": "stop", "reason": "Nonfinite held-out reward"} + # Output collapse is actionable even when reward rises or no eval exists. + # Missing legacy metrics do not count as either healthy or failed batches. + if outputs: + latest = max(outputs) + recent = [outputs[k] for k in range(latest-2, latest+1) if k in outputs] + result["latest_output_metrics"] = {k: v for k, v in outputs[latest].items() + if k.startswith(("rollout/action_", "rollout/thinking_", "rollout/final_answer_"))} + for name, threshold in (("multiple_objects", .05), ("length_limit", .25), ("format_invalid", .10)): + key = f"rollout/action_{name}_fraction" + last = outputs[latest] + if last.get("rollout/action_count", 0) >= 32 and last.get(key, 0) >= .5: + return {**result, "decision": "stop", "reason": f"Output collapse: {name} in at least half of responses at rollout {latest}"} + if len(recent) == 3 and all(r.get("rollout/action_count", 0) >= 32 + and r.get(key, -1) >= threshold for r in recent): + return {**result, "decision": "stop", "reason": f"Sustained output failure: {name} >= {threshold:.0%} for three rollouts"} + # A missing baseline is an observation failure, not evidence of regression. + if 0 not in evaluations: + return {**result, "reason": "Awaiting baseline evaluation"} + baseline = evaluations[0] + post = [item for item in history if item["eval_step"] > 0] + if baseline > 0 and len(post) >= 2 and all(item["reward"] < .8 * baseline for item in post[-2:]): + return {**result, "decision": "stop", "reason": "Two consecutive held-out evaluations more than 20% below baseline"} + if (completed >= 50 and len(post) >= 3 and post[-1]["eval_step"] >= 49 + and all(item["reward"] <= baseline for item in post[-3:])): + return {**result, "decision": "stop", "reason": "At least 50 updates with the latest three held-out evaluations at or below baseline"} + return result