Spaces:
Paused
Paused
File size: 5,690 Bytes
5e0b58b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | """Restore worker subprocess for Path C (Alpha.5).
This is C2: a fresh Python process that receives only filesystem paths
and scalar arguments. It calls the production restore_full() and
computes digests using the canonical production digest.
No in-memory network/engine/state object crosses the subprocess boundary.
"""
from __future__ import annotations
import argparse
import json
import os
import sys
from pathlib import Path
from typing import Any, cast
def main() -> None:
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
parser = argparse.ArgumentParser()
parser.add_argument("--phase", choices=("c1", "c2"), default="c2")
parser.add_argument("--snapshot")
parser.add_argument("--journal")
parser.add_argument("--checkpoint")
parser.add_argument("--config", required=True)
parser.add_argument("--schedule", required=True)
parser.add_argument("--output", required=True)
parser.add_argument("--end-tick", type=int, required=True)
parser.add_argument(
"--digest-k",
required=True,
help="Path to write digest at K (before continuation)",
)
args = parser.parse_args()
if args.phase == "c1":
from src.homeostasis.engine import HomeostasisEngine
from src.learning.learning_engine import LearningEngine
from src.storage.checkpoint import (
CheckpointNetworkLike,
capture_runtime_checkpoint,
write_runtime_checkpoint,
)
from src.storage.runtime import (
RuntimeNetworkLike,
StorageRuntimeConfig,
StorageSession,
)
from tests._restore_helpers import K, create_network, run_absolute_schedule
config = json.loads(Path(args.config).read_text(encoding="utf-8"))
schedule = json.loads(Path(args.schedule).read_text(encoding="utf-8"))
network = create_network(config)
homeo = HomeostasisEngine(network, config)
homeo.attach()
learn = LearningEngine(network, config)
learn.attach()
run_absolute_schedule(network, schedule, K)
artifact_dir = Path(args.output).parent
runtime = StorageRuntimeConfig(
snapshot_path=artifact_dir / "base.b5d",
journal_path=artifact_dir / "base.b5d.journal",
commit_interval_ticks=1,
)
with StorageSession(cast("RuntimeNetworkLike", network), runtime):
pass
from tests._restore_helpers import capture_learning_state
checkpoint = capture_runtime_checkpoint(
cast("CheckpointNetworkLike", network),
homeostasis_rates=cast(Any, homeo)._rates_hz,
learning_states=capture_learning_state(learn)["states"],
pending_rewards=capture_learning_state(learn)["pending_rewards"],
)
checkpoint_path = artifact_dir / "runtime.json"
write_runtime_checkpoint(checkpoint_path, checkpoint)
manifest = {
"snapshot": str(runtime.snapshot_path),
"journal": str(runtime.journal_path),
"checkpoint": str(checkpoint_path),
"pid": os.getpid(),
}
(artifact_dir / "pid_C1.txt").write_text(str(os.getpid()), encoding="utf-8")
Path(args.output).write_text(json.dumps(manifest, indent=2), encoding="utf-8")
print(f"C1 worker done: pid={os.getpid()}")
return
from src.storage.core_restore import restore_full
# ββ Restore from filesystem artifacts only βββββββββββββββββββββββββββ
bundle = restore_full(
snapshot_path=Path(args.snapshot),
journal_path=Path(args.journal),
checkpoint_path=Path(args.checkpoint),
config=json.loads(Path(args.config).read_text()),
recovered_path=Path(args.output).parent / "recovered.b5d",
create_homeostasis_engine=True,
create_learning_engine=True,
)
# ββ Compute digest at K (before continuing) ββββββββββββββββββββββββββ
from tests._restore_helpers import (
compute_digest,
config_sha256,
make_config,
run_absolute_schedule,
)
config = make_config()
digest_K = compute_digest(
bundle.network,
bundle.homeostasis_engine,
bundle.learning_engine,
config_sha256_str=config_sha256(config),
)
digest_K_path = Path(args.digest_k)
digest_K_path.write_text(
json.dumps({"digest_K": digest_K, "pid": os.getpid()}), encoding="utf-8"
)
# ββ Continue K -> N ββββββββββββββββββββββββββββββββββββββββββββββββββ
schedule = json.loads(Path(args.schedule).read_text())
run_absolute_schedule(bundle.network, schedule, args.end_tick)
# ββ Compute digest at N ββββββββββββββββββββββββββββββββββββββββββββββ
digest_N = compute_digest(
bundle.network,
bundle.homeostasis_engine,
bundle.learning_engine,
config_sha256_str=config_sha256(config),
)
result = {
"digest": digest_N,
"digest_K": digest_K,
"pid": os.getpid(),
"start_tick": 0,
"end_tick": args.end_tick,
"restore_full_used": True,
}
Path(args.output).write_text(json.dumps(result, indent=2), encoding="utf-8")
print(
f"C2 worker done: digest_K={digest_K}, digest_N={digest_N}, pid={os.getpid()}"
)
if __name__ == "__main__":
main()
|