|
| 1 | +"""V7-H06b: pipeline.status RPC + run_registry round-trip. |
| 2 | +
|
| 3 | +Verifies that pipeline.status reflects backend reality for pause / |
| 4 | +resume / abort / running / finished, so the UI can confirm a state |
| 5 | +transition before flipping its own indicators. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import threading |
| 11 | +import time |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from cppmega_v4.jsonrpc.dispatcher import ( |
| 16 | + _pipeline_abort, _pipeline_pause, _pipeline_resume, _pipeline_status, |
| 17 | +) |
| 18 | +from cppmega_v4.jsonrpc.schema import ( |
| 19 | + PipelineAbortParams, PipelineStatusParams, VerifyParams, |
| 20 | +) |
| 21 | +from cppmega_v4.runner import Pipeline, run_pipeline |
| 22 | +from cppmega_v4.runtime import job_control as jc |
| 23 | +from cppmega_v4.runtime import run_registry as rr |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(autouse=True) |
| 27 | +def _reset(): |
| 28 | + jc.reset() |
| 29 | + rr.reset() |
| 30 | + yield |
| 31 | + jc.reset() |
| 32 | + rr.reset() |
| 33 | + |
| 34 | + |
| 35 | +def _spec() -> VerifyParams: |
| 36 | + return VerifyParams.model_validate({ |
| 37 | + "graph": { |
| 38 | + "nodes": [ |
| 39 | + {"id": "attn", "kind": "attention", |
| 40 | + "params": {"num_heads": 4, "head_dim": 64}}, |
| 41 | + {"id": "mlp", "kind": "mlp", "params": {}}, |
| 42 | + ], |
| 43 | + "edges": [{"src": "attn", "dst": "mlp"}], |
| 44 | + }, |
| 45 | + "dim_env": {"B": 1, "S": 8, "H": 128, |
| 46 | + "nh": 2, "nkv": 1, "head_dim": 64}, |
| 47 | + "loss": {"kind": "cross_entropy", "head_outputs": ["mlp"]}, |
| 48 | + "optim": {"kind": "adamw", |
| 49 | + "groups": [{"matcher": "all", "lr": 1e-3, |
| 50 | + "weight_decay": 0.01, |
| 51 | + "betas": [0.9, 0.95]}]}, |
| 52 | + }) |
| 53 | + |
| 54 | + |
| 55 | +def test_v7_h06b_status_unknown_for_never_seen_run(): |
| 56 | + r = _pipeline_status(PipelineStatusParams(run_id="ghost")) |
| 57 | + assert r.run_id == "ghost" |
| 58 | + assert r.known is False |
| 59 | + assert r.running is False |
| 60 | + |
| 61 | + |
| 62 | +def test_v7_h06b_status_reports_paused_after_pause_rpc(): |
| 63 | + rr.register("run-X") |
| 64 | + _pipeline_pause(PipelineAbortParams(run_id="run-X")) |
| 65 | + r = _pipeline_status(PipelineStatusParams(run_id="run-X")) |
| 66 | + assert r.known is True |
| 67 | + assert r.paused is True |
| 68 | + assert r.running is True |
| 69 | + |
| 70 | + |
| 71 | +def test_v7_h06b_status_clears_paused_after_resume_rpc(): |
| 72 | + rr.register("run-Y") |
| 73 | + _pipeline_pause(PipelineAbortParams(run_id="run-Y")) |
| 74 | + assert _pipeline_status( |
| 75 | + PipelineStatusParams(run_id="run-Y")).paused is True |
| 76 | + _pipeline_resume(PipelineAbortParams(run_id="run-Y")) |
| 77 | + r = _pipeline_status(PipelineStatusParams(run_id="run-Y")) |
| 78 | + assert r.paused is False |
| 79 | + |
| 80 | + |
| 81 | +def test_v7_h06b_status_marks_aborted_after_abort_rpc(): |
| 82 | + rr.register("run-Z") |
| 83 | + _pipeline_abort(PipelineAbortParams(run_id="run-Z")) |
| 84 | + r = _pipeline_status(PipelineStatusParams(run_id="run-Z")) |
| 85 | + assert r.aborted is True |
| 86 | + |
| 87 | + |
| 88 | +def test_v7_h06b_status_reports_running_during_train_then_finished(): |
| 89 | + """Pause the train upfront so the train loop is *guaranteed* to be |
| 90 | + in wait_while_paused when the poller runs. Verify running=True |
| 91 | + + paused=True. Then resume, wait for train to finish, verify |
| 92 | + running=False.""" |
| 93 | + # Pause first so step 0 blocks in wait_while_paused. |
| 94 | + jc.pause("run-T") |
| 95 | + captured: dict = {} |
| 96 | + |
| 97 | + def _resumer(): |
| 98 | + time.sleep(0.15) |
| 99 | + r = _pipeline_status(PipelineStatusParams(run_id="run-T")) |
| 100 | + captured["mid"] = {"known": r.known, "running": r.running, |
| 101 | + "paused": r.paused} |
| 102 | + jc.resume("run-T") |
| 103 | + |
| 104 | + threading.Thread(target=_resumer, daemon=True).start() |
| 105 | + rep = run_pipeline(_spec(), Pipeline.from_dict({ |
| 106 | + "stages": ["parse", "verify_build_spec", "build_model", "train"], |
| 107 | + "stage_options": {"train": { |
| 108 | + "num_steps": 2, "run_id": "run-T", "abort_token": "run-T", |
| 109 | + }}, |
| 110 | + })) |
| 111 | + tr = next(s for s in rep.stages if s.name == "train") |
| 112 | + assert tr.status == "ok" |
| 113 | + |
| 114 | + # Mid-train snapshot taken while paused: running=True + paused=True. |
| 115 | + assert captured["mid"] == {"known": True, "running": True, |
| 116 | + "paused": True}, captured |
| 117 | + # After train returns: running=False, paused already cleared. |
| 118 | + final = _pipeline_status(PipelineStatusParams(run_id="run-T")) |
| 119 | + assert final.known is True |
| 120 | + assert final.running is False |
| 121 | + assert final.paused is False |
| 122 | + assert final.last_step >= 0 |
| 123 | + |
| 124 | + |
| 125 | +def test_v7_h06b_status_last_step_and_loss_updates_during_train(): |
| 126 | + rep = run_pipeline(_spec(), Pipeline.from_dict({ |
| 127 | + "stages": ["parse", "verify_build_spec", "build_model", "train"], |
| 128 | + "stage_options": {"train": { |
| 129 | + "num_steps": 3, "run_id": "run-S", "abort_token": "run-S", |
| 130 | + }}, |
| 131 | + })) |
| 132 | + tr = next(s for s in rep.stages if s.name == "train") |
| 133 | + assert tr.status == "ok" |
| 134 | + final = _pipeline_status(PipelineStatusParams(run_id="run-S")) |
| 135 | + # 3-step train, last index = 2. |
| 136 | + assert final.last_step == 2 |
| 137 | + assert final.last_loss is not None |
| 138 | + assert final.last_loss > 0 |
0 commit comments