diff --git a/.gitignore b/.gitignore index 078d109..fedf543 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,11 @@ docker/.container.cfg # used to configure VS Code settings !.vscode +# Local credentials and environment configuration (never version secrets) +.env +.env.* +!.env.example + # Outputs **/output/* **/outputs/* diff --git a/AGENT.md b/AGENT.md index 015d967..855feed 100644 --- a/AGENT.md +++ b/AGENT.md @@ -3,9 +3,5 @@ 项目计划参考'/home/cen/Documents/Platform'下的文档 第一阶段先考虑四足狗的腿 可以调用子agent加速开发 - -当前首要任务:Go2 FL受载站立,见 docs/loaded_stand_training.md 和 docs/loaded_stand_acceptance.md。 -首轮2048更新已完成,1024候选holdout/full通过,120秒耐久足端漂移30.78mm>30mm,原始验收失败。 -用户随后明确同意此微小偏差特例放行,已完成 go2_fl_loaded_stand-1.0.0 带豁免冻结发布,见 docs/loaded_stand_release_1_0_0.md;仅当前候选/证据的foot_drift豁免,不是全局阈值改变。 -保留固定候选和原始失败证据;不得以已查看holdout重选权重、缩短耐久或豁免其它门槛。新一轮训练需用户新授权。 -旧固定悬空Reach 1.0.1保持归档不变;不以站立控制基线、短训练或奖励上涨替代新策略验收。 \ No newline at end of file +遇到用户的问题时指出当前证据不足以回答什么 +训练时默认采用5080显卡,不要使用cpu \ No newline at end of file diff --git a/README.md b/README.md index da5f756..599545b 100644 --- a/README.md +++ b/README.md @@ -1,104 +1,27 @@ # SkillFactory v0.0.2 -仓库:[chenlin/SkillFactory](https://gitea.robotquan.com/chenlin/SkillFactory)。 +仓库:[chenlin/SkillFactory](https://gitea.robotquan.com/chenlin/SkillFactory)。真实机器人部件资产导入、仿真控制与技能训练;当前先做Go2 FL单腿,不声明整机或实机能力。 -真实单腿资产导入、固定悬空/可运动承载底座仿真与策略训练。资产/旧接口覆盖 **Go2 FL / Go1 FL**;新承载站立试验仅限Go2 FL,共享 PPO 仍仅为短更新原型。第二阶段新增 **Go2 FL 固定悬空足端定点到达**的固定数据 IQL 训练、恢复、独立评估与冻结包链路;发布验收结果见下文,不声明 Go1/整机/接触迁移。 +## 当前版本进度 -## 当前首要任务:受载站立策略训练 +- 原46D参考+残差PPO已完成256更新,冻结u256名义9秒开发验证128/128通过,P0回放通过。 +- 严格P1连续周期双方均在首周期水平根漂移超限;P2虽完成20行,但10组命令初态全部失配,不能支持策略优越性。 +- RootXY-v2实际完成81次PPO更新;u64功能128/128通过,XY RMS约改善2.8%,但126/128仍超30mm门槛,**没有合格新候选**。约44分钟因监控文件竞态中止,修复目前仅CPU验证。 +- `accepted_skill=false`;无连续稳定、独立鲁棒性、硬件或sim2real结论。当前运行授权已消费,不自动重跑。 -主线已切换到 `SkillFactory-LoadedLegStand-v0` 的独立PPO训练。使用真实六维测力、可运动3kg底座和地面足端接触;保留横向/转动导向,竖直辅助为0。**旧Reach冻结权重不用于新任务。** +## 从这里继续 -- [训练预登记、预算与选模规则](docs/loaded_stand_training.md) -- 配置:`configs/training/go2_fl_loaded_stand_ppo_v1.json` -- 进度:`logs/loaded_stand/train_v1/progress.json`;检查点:该目录的 `checkpoints/` -- **首轮已完成**:256环境、2048次更新、16,777,216条转移,耗时25.18分钟。validation-only选中1024更新,平均高度误差0.9663mm;随后holdout均值0.921mm、13类全幅通过,但原始120秒耐久足端漂移30.78mm超过30mm。**用户批准本次固定偏差后,已带豁免冻结发布1.0.0**;不是原始门槛全部通过。 -- 候选:`logs/loaded_stand/train_v1/checkpoints/best_validation.pt`;[原始验收报告](docs/loaded_stand_acceptance.md);[用户豁免与1.0.0发布报告](docs/loaded_stand_release_1_0_0.md)。归档:`releases/go2_fl_loaded_stand-1.0.0.tar.gz`。原始失败证据保留,不以已查看holdout重选权重。 +- **[文档入口](docs/README.md)** +- [当前进度](docs/current_status.md) / [下一阶段](docs/next_stage.md) +- [接口和CPU测试](docs/contact_cycle_runtime.md) +- [RootXY配方](docs/contact_cycle_rootxy_v2_plan.md) / [启动合同](docs/contact_cycle_rootxy_v2_launch.md) +- [冻结身份与证据路径](docs/evidence_index.md) +- [统一软硬件描述v2](docs/software_hardware_spec.md):Schema及静态校验已实现,通用运行adapter尚未实现。 -## 已完成归档:Reach离线预训练与冻结包 +训练默认RTX5080。新Kit运行必须新预算、新配置绑定、新授权及唯一监督器;不得复用失败目录/旧grant、改写账本或终止桌面等外来进程。准备通过不是物理资格,源码提交也不是技能验收。 -- 范围与预注册门槛:[`docs/stage2_plan.md`](docs/stage2_plan.md);复现/推理:[`docs/stage2_usage.md`](docs/stage2_usage.md)。 -- 最新验收报告:[`docs/stage2_release_1_0_1.md`](docs/stage2_release_1_0_1.md);正式训练配置:`configs/training/go2_fl_offline_v1_attempt2.json`。 -- 发布包:[`releases/go2_fl_reach-1.0.1.tar.gz`](releases/go2_fl_reach-1.0.1.tar.gz);只读目录:`releases/go2_fl_reach-1.0.1/`。完整边界6面/12边/8角物理复验通过(均值2.308mm),统一12项验收全部通过,权重未变。独立SkillBundle v1/leg-token-v1,不是通用SoftwareSpec v2 adapter。 -- 新复验证据:`logs/stage2/v1_0_1/`;原训练/恢复点:`logs/stage2/v1_attempt2/`;失败首轮:`logs/stage2/v1/`。1.0.0及[历史报告](docs/stage2_acceptance.md)原样保留,其旧`accepted`不代表新门槛通过,见[修复记录](docs/stage2_reliability_fix.md)。 +资产配置在`configs/legs/`,接触周期配置在`configs/contact_cycle/`。依赖本机IsaacLab/IsaacSim、Unitree资源及未随本次提交交付的日志/权重/生成资产;跨机器需另带实际产物并验证身份。`.env`与本机授权不提交。 -## 新任务:连接底座的受载站立(实验v0) +## 历史发布边界 -- `SkillFactory-LoadedLegStand-v0`:3kg可运动虚拟底座、刚性安装六维测力、足端接地;横向/转动弹簧阻尼导向,**竖直导向力为0**,没有世界固定根。 -- 13种无扰动/六轴正负载荷场景×14秒真实物理验证通过;最坏高度误差8.01mm。关闭驱动后全部环境向下塌落约35.46cm,排除了竖直悬挂承重。 -- **本节数据属于反馈控制基线和训练环境;独立RL首轮已另行训练并获有限漂移豁免发布,但不是原始门槛全通过或无辅助单足/整机平衡。** 新观测含6D安装口wrench,与Reach冻结接口不兼容。 -- [模型、六维接口、实测报告与启动命令](docs/loaded_stand.md);配置:`configs/standing/go2_fl_support_v0.json`;证据:`logs/loaded_stand/v0/`。旧Reach资产和1.0.1包不变。 - -## 统一软硬件描述规范 - -唯一规范入口:[`docs/software_hardware_spec.md`](docs/software_hardware_spec.md)。最新机器格式为 PartSpec / SoftwareSpec **v2**,示例位于 `configs/parts/` 与 `configs/software/`;旧版规范及 Schema 已移除。发布版本 `v0.0.2` 与描述格式版本 `v2` 独立。 - -```bash -# Python 3.12,需已有 jsonschema;无需启动 Kit -python3 scripts/validate_descriptions.py \ - --hardware configs/parts/*.json --software configs/software/*.json -``` - -通用 v2 adapter 尚未实现;下述单腿仿真继续使用保留的 `configs/legs/` 运行输入。设计模板、静态校验通过均不等于已支持对应部位的仿真、训练或实机部署。 - -## 运行环境 - -本机已验证:Isaac Sim 6.0.1、Isaac Lab 3.0.0(core 6.1.17)、Python 3.12、Torch 2.10 CUDA、Kit `PhysxCfg`。不修改外部 Lab/Unitree,不安装新运行时。 - -```bash -# 仓库根目录;避免旧 conda/venv 抢占解释器 -unset CONDA_PREFIX VIRTUAL_ENV -export PYTHONPATH="$PWD/source/SkillFactory${PYTHONPATH:+:$PYTHONPATH}" -export PYTHONUNBUFFERED=1 -LAB=/home/cen/IsaacLab/isaaclab.sh -``` - -路径可按机器调整。**不要在仓库根执行 `uv sync`**:根 `pyproject.toml` 的依赖源仍是 Lab 模板配置;使用已有 Lab 解释器。需要安装项目时用该解释器 `-m pip install -e source/SkillFactory`。Kit 必须在任务配置/动作类导入之前启动;以下仿真入口已处理。 - -## 通用单腿:导入 → 验证 → 短训练 - -```bash -for leg in go2_fl go1_fl; do - "$LAB" -p scripts/assets/extract_leg.py --spec "configs/legs/$leg.json" - "$LAB" -p scripts/assets/convert_leg.py --bundle "assets/generated/legs/$leg" -done - -"$LAB" -p scripts/leg_worker.py --bundle assets/generated/legs/go2_fl \ - --num_envs 64 --steps 120 --output logs/leg_interface/verify - -"$LAB" -p scripts/train_leg_skill.py \ - --bundles assets/generated/legs/go2_fl assets/generated/legs/go1_fl \ - --num_envs 16 --horizon 32 --updates 1 --output logs/leg_interface/train_smoke -``` - -新腿通过 JSON 声明源路径、安装切口、辅助支链、受控关节及足端,不按 Go2 名称硬编码。`extract_leg.py --source-root` 可覆盖示例机器路径。重提取后必须重新转换;分层 USD、网格、许可证与 manifest 整体保留。 - -GUI:给验证命令加 `--num_envs 1 --viz kit`;默认无 GUI。接触验证加 `--contact-action 0,0,1`,该动作只适用于示例 Go2/Go1 的声明顺序,不是任意腿的通用压地指令。 - -## 保留的第一周基准 - -```bash -"$LAB" -p scripts/assets/extract_go2_leg.py -"$LAB" -p scripts/assets/convert_go2_leg.py -"$LAB" -p scripts/anchor_smoke.py --num_envs 1 --mode contact --steps 600 --viz kit -# 完整验收约 11 分钟;开发时用 --suite contract -"$LAB" -p scripts/verify_anchor.py --suite full --num_envs 64 \ - --duration 600 --reset_count 1334 --output logs/week1_anchor/acceptance -``` - -## 测试与接口 - -```bash -"$LAB" -p scripts/list_envs.py --show_presets -PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 "$LAB" -p -m pytest tests -q -# 可选:额外启动旧任务 B=1/16/64 Kit 集成测试 -PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 ANCHOR_RUNTIME_TESTS=1 "$LAB" -p -m pytest tests/test_anchor_contract.py -q -"$LAB" -p -m ruff check source/SkillFactory scripts tests -``` - -- [`docs/software_hardware_spec.md`](docs/software_hardware_spec.md):统一软硬件描述规范 v2;支持腿/臂/手/夹爪/躯干/头部/底座/传感器的结构与语义端口,示例在 `configs/parts/`。v2 adapter 尚未实现,当前可运行的仍是单腿 v1。 -- [`docs/leg_interface.md`](docs/leg_interface.md):现有单腿兼容运行链路、验收阈值与证据;不作为另一份描述规范。 -- `SkillFactory-LegSkill-v0`:结构化 token 与专用 PPO 原型入口;不能直接交给仅接受平坦观测的旧 RSL-RL trainer。 -- `SkillFactory-SingleLegAnchor-Go2-v0`:保留 `[B,19]` 验收接口。 -- `Template-Skillfactory-v0`:原 Cartpole 与 `scripts/rsl_rl/` 入口保留。 - -生成资产、训练日志、checkpoint 不入库;换机器需重建复验,冻结发布包独立保留。旧 `train_leg_skill.py` 每轮重启 Kit,仍仅适合短接口测试;正式离线链路使用常驻收集器与独立无仿真梯度训练。后续 latent 学习、留出形态评估和整机协调仍未实现;固定支座结果不证明整机迁移。 +Reach1.0.1及受载站立1.0.0保持不变。后者原120秒30.78mm漂移超过30mm,仅该候选获[专属豁免](docs/loaded_stand_release_1_0_0.md),不能推广为本任务豁免。旧32D失败、全部失败证据及已扣预算保留。过程文档已[归档](docs/README.md#历史资料),不作为当前运行指令。 diff --git a/archives/documentation_before_rootxy_handoff.tar.gz b/archives/documentation_before_rootxy_handoff.tar.gz new file mode 100644 index 0000000..32f4bdb Binary files /dev/null and b/archives/documentation_before_rootxy_handoff.tar.gz differ diff --git a/archives/documentation_before_simplification_20261014.tar.gz b/archives/documentation_before_simplification_20261014.tar.gz new file mode 100644 index 0000000..01b8ea9 Binary files /dev/null and b/archives/documentation_before_simplification_20261014.tar.gz differ diff --git a/archives/documentation_cleanup_20261014.json b/archives/documentation_cleanup_20261014.json new file mode 100644 index 0000000..521ae10 --- /dev/null +++ b/archives/documentation_cleanup_20261014.json @@ -0,0 +1,76 @@ +{ + "purpose": "User-authorized docs simplification; byte-exact historical archive before deletion. No evidence, releases, weights or execution inputs removed.", + "archive": "archives/documentation_before_simplification_20261014.tar.gz", + "archive_sha256": "ed5a9e94d090ad68a724e5d7122723706b4d8366005126c18d09884a42b59e24", + "original_sha256": { + "docs/contact_cycle_design.md": "d403741142c8f9bdc0be936ba2337119f31d3b36191a8c0a693921cdf2dbc414", + "docs/contact_cycle_diagnostic_training.md": "d5e3439063c0b7e8a312ccb732193ecf568489515a3c258704c98847a774ca9b", + "docs/contact_cycle_initialization_repair.md": "7010f4b7cd6d6d2be4731252db0f9164c05ba1d9b0c93bafe5690631ed512c81", + "docs/contact_cycle_offline_diagnosis.md": "f26f214515b870ee3f24d0f7651784e7781149b42908689b099c07e4297b905e", + "docs/contact_cycle_repair.md": "e94936ded6e252c43ffece600497525b59bd7c1c4d1a1ac56d7c1566d3bb4d5f", + "docs/contact_cycle_report.md": "5f97f50c7b9e4dce764a02d45b92df21bb3bd4592aad50544a0dce3ac3ca5a1f", + "docs/contact_cycle_sim_residual.md": "3350a3f6be8f040ffceaf23a99d4960d4f1234703586c9c8c508f73576634843", + "docs/contact_cycle_sim_stress.md": "acf56ad088fdbf5be4791899b64db1297765e0d0656a91bfeb79cfbfb2fd4e3b", + "docs/contact_cycle_sim_stress_result.md": "781cfbc2043a1165186e0b94719b89f5817b786a06c495d0a40d046ba154b1e0", + "docs/contact_cycle_sim_training_plan.md": "c585658c16370e8efb6085ab538d0a9fb5bba61be4a26b57bb9e1936aa0d324f", + "docs/contact_cycle_sim_training_result.md": "ba3899d7296ec6188533103813516669abec56ae898be6e961fcc866fb416e57", + "docs/contact_cycle_simulation_functional.md": "adb1fcf56f2e136feabc9eb6a0f272e6ecc74b47a2918944c2ebd4f04734b8e2", + "docs/contact_cycle_training.md": "7228d20d6218c988d6f11b39cbd13057dd7ed652e42c990deb5835df082650c8", + "docs/contact_cycle_training_result.md": "ee696f89a1aa769e97af1021a74c2996b38fbff07b4ba375356be551cfa9108d", + "docs/leg_interface.md": "2222811c5e8ce9c34819d6a9868a4d97d502037f6759c0a7fbcbb772f8c7ac67", + "docs/loaded_stand.md": "4298f0259063f027ca6bd5953c7940429eccdfc2fbec15400c128a5f88732654", + "docs/loaded_stand_acceptance.md": "35b247b813de55bce5cecec4acba87421b0db0cc109dcc316bad07416f5ad960", + "docs/loaded_stand_plan.md": "43f024678e427cdd8755e545fd74709f95243b717721406366d5d9206d855a4a", + "docs/loaded_stand_release_1_0_0.md": "04fb0b59ddc64ee9484e555bbf59cd71fb72b4673fcdd5178f609594aff1e498", + "docs/loaded_stand_training.md": "32daf0c38758fcec7f4d01752cf6cbb722123a3b74e4b924f014f68bb0ec3fa6", + "docs/schemas/part_spec.v2.schema.json": "12bdc21249a0d2f56523bd09a9f634a8376b18fdf42c50b15187d1dc6ce20c04", + "docs/schemas/software_spec.v2.schema.json": "34b7fc1c20291ad2cdc5f96a9c33acf713dd1fb5317d395cc41ef0f576d5431a", + "docs/software_hardware_spec.md": "73d391f29238c55261ffbaaf4d039752fd5aac740348fa3d275e0e2860a6526b", + "docs/stage2_acceptance.md": "0dc7ed9f6cbc16fe796b4389c81e4a3e1f90f182c3118e248f788a7099e07d9a", + "docs/stage2_attempt2.md": "13ad57085f2db1516087f2c683bc85e4ca4f06254ab798bd0a88eb69f836ea01", + "docs/stage2_plan.md": "6a10df5d9f4ed4bc4d26f7cb11ed775f66f4e3b392bb8f5e1a0188219ec8bf40", + "docs/stage2_release_1_0_1.md": "b6c214d7e7c2f2c62dae969d0614ac4163ec3d000c2754106a7588ec20a53eaf", + "docs/stage2_reliability_fix.md": "dbf08ec07893a05d08dcffdaf399cf442e45f1e006f007000852de7178ea328e", + "docs/stage2_usage.md": "b86e21f23dab0cc9f18ea296aa046f86f519c7776757bd8927c845bbd0a26e63", + "README.md": "a97ac512c07e68c6c60a75656e576ae00e897a6779d6bcf592c7f30594c2f46b" + }, + "removed_from_active_docs": [ + "docs/contact_cycle_design.md", + "docs/contact_cycle_diagnostic_training.md", + "docs/contact_cycle_initialization_repair.md", + "docs/contact_cycle_offline_diagnosis.md", + "docs/contact_cycle_repair.md", + "docs/contact_cycle_report.md", + "docs/contact_cycle_sim_residual.md", + "docs/contact_cycle_sim_stress_result.md", + "docs/contact_cycle_simulation_functional.md", + "docs/contact_cycle_training.md", + "docs/contact_cycle_training_result.md", + "docs/leg_interface.md", + "docs/loaded_stand.md", + "docs/loaded_stand_acceptance.md", + "docs/loaded_stand_plan.md", + "docs/loaded_stand_training.md", + "docs/stage2_acceptance.md", + "docs/stage2_attempt2.md", + "docs/stage2_plan.md", + "docs/stage2_release_1_0_1.md", + "docs/stage2_reliability_fix.md", + "docs/stage2_usage.md" + ], + "retained_original_docs": [ + "contact_cycle_sim_stress.md", + "contact_cycle_sim_training_plan.md", + "contact_cycle_sim_training_result.md", + "loaded_stand_release_1_0_0.md", + "software_hardware_spec.md" + ], + "must_remain_byte_exact": [ + "docs/contact_cycle_sim_training_plan.md", + "docs/contact_cycle_sim_stress.md", + "docs/loaded_stand_release_1_0_0.md", + "docs/contact_cycle_sim_training_result.md", + "docs/schemas/part_spec.v2.schema.json", + "docs/schemas/software_spec.v2.schema.json" + ] +} diff --git a/archives/documentation_rootxy_handoff.json b/archives/documentation_rootxy_handoff.json new file mode 100644 index 0000000..6822d91 --- /dev/null +++ b/archives/documentation_rootxy_handoff.json @@ -0,0 +1,32 @@ +{ + "scope": "Documentation only; historical text is NOT current execution authority", + "archive": "archives/documentation_before_rootxy_handoff.tar.gz", + "archive_sha256": "0f73d03bf2e8ff0efdf8b37dbd97613197d442370547dd0168982ef07c14b892", + "files": { + "README.md": "22f22b03e5a410e6cfa98e9d99f06bb197f661b0b77465a99ff31ee917457f95", + "docs/README.md": "7ac704c468f5801be274299402918ec2ecdf2c3827a85b71a7300ca08662c9aa", + "docs/contact_cycle_evaluation_namespace_binding.md": "a78ad4fedd63262bc6a51c1e1d46eed191e09dab4466088750699b7e5ec2ef98", + "docs/contact_cycle_evidence_encoding_v2.md": "e010a21b0a483c828e843b51af04ee534bc59b71533ebd9f33aca8c304a0af42", + "docs/contact_cycle_native_monitor_v3.md": "cf02afee2794478d1fc47b6939c64f012b55c577b4cf1a9edbf1cf2993486a99", + "docs/contact_cycle_p1_initialization_handoff.md": "664436c5073f2b292d4ac7575351752315694b0248d771077f9669c9693ab972", + "docs/contact_cycle_p2_extended_profile.md": "9245a4094bda4b972e9db305b61b71cc1da09d1f0eb058abbcf528dce6f1d463", + "docs/contact_cycle_p2_pairing_repair.md": "2ec134adf371bd2db29840786c5f6fa47d6875a9aa6b69b34bd9f17c97661bd0", + "docs/contact_cycle_p2_second_attempt.md": "fd35ee8340677bec9b8e13d694c2b0b21551c5d0d0e67ac4885397809ac32a12", + "docs/contact_cycle_rootxy_v2_launch.md": "24210520cda5346e39da1efd837f41d01525680b7eaba1e44c2c1324dee5a833", + "docs/contact_cycle_rootxy_v2_plan.md": "9ccaf1427de4189261d718e839173a67e4e389bdb25145838eb98ef2d2bd8e62", + "docs/contact_cycle_runtime.md": "369ad770c02b105fb2d7f5cdfe89205f508af5111cbfd1757ecf4d8998614c59", + "docs/contact_cycle_sim_stress.md": "acf56ad088fdbf5be4791899b64db1297765e0d0656a91bfeb79cfbfb2fd4e3b", + "docs/contact_cycle_sim_training_plan.md": "c585658c16370e8efb6085ab538d0a9fb5bba61be4a26b57bb9e1936aa0d324f", + "docs/contact_cycle_sim_training_result.md": "ba3899d7296ec6188533103813516669abec56ae898be6e961fcc866fb416e57", + "docs/current_status.md": "38f5f2ba28196580fa7c245a4c8928ebbc3c0d6bf6ee825701ff437fbca1ecb6", + "docs/loaded_stand_release_1_0_0.md": "04fb0b59ddc64ee9484e555bbf59cd71fb72b4673fcdd5178f609594aff1e498", + "docs/next_stage.md": "b12edd4d24ef11e313c8361c02c4d73e29d3e941d3452d987f3473d2ea34dfce", + "docs/schemas/part_spec.v2.schema.json": "12bdc21249a0d2f56523bd09a9f634a8376b18fdf42c50b15187d1dc6ce20c04", + "docs/schemas/software_spec.v2.schema.json": "34b7fc1c20291ad2cdc5f96a9c33acf713dd1fb5317d395cc41ef0f576d5431a", + "docs/software_hardware_spec.md": "3d0cb28cffe62fc4d6e35aa111c39b764d87392f82bae4cedaa67ad6cebcad4a" + }, + "protected_unchanged": [ + "docs/contact_cycle_sim_training_plan.md", + "docs/contact_cycle_sim_stress.md" + ] +} diff --git a/configs/contact_cycle/acceptance_v1.json b/configs/contact_cycle/acceptance_v1.json new file mode 100644 index 0000000..affcf2b --- /dev/null +++ b/configs/contact_cycle/acceptance_v1.json @@ -0,0 +1,50 @@ +{ + "schema_version": 1, + "status": "proposed_not_authorized_for_training", + "contract": "go2-fl-contact-cycle-v1", + "gates": { + "stance_contact_fraction_min": 0.98, + "stance_load_mae_weight_fraction_max": 0.25, + "height_error_m_max": 0.06, + "swing_rms_m_max": 0.025, + "swing_clearance_m_min": 0.02, + "swing_contact_fraction_max": 0.01, + "stance_slip_m_max": 0.03, + "landing_peak_weight_fraction_max": 3.0, + "recovery_s_max": 2.5, + "joint_limit_excess_rad_max": 0.01, + "normalized_effort_velocity_max": 1.00001, + "unilateral_residual_N_max": 0.5, + "friction_residual_N_max": 2.0, + "friction_cone_mu_upper": 0.8 + }, + "proposed_training": { + "authorization": null, + "seeds": [91101, 91102, 91103], + "num_envs": 128, + "horizon": 32, + "updates_per_seed": 2048, + "max_total_transitions": 25165824, + "max_total_wall_s": 10800, + "max_artifact_bytes": 4294967296, + "resume_counts_toward_budget": true, + "selection": "Per seed, every 256 updates on validation only: minimum failure_fraction*10 + swing_rms_m/0.025 + load_mae_weight_fraction/0.25 + slip_m/0.03. Missing/terminated samples receive worst score. Tie: earliest update. Lock all three candidates before holdout.", + "curriculum": [ + {"updates": [1, 512], "distribution": "nominal cycle without disturbance; 25% stand-only regression"}, + {"updates": [513, 1024], "distribution": "continuous force directions at half amplitude; 25% nominal regression"}, + {"updates": [1025, 2048], "distribution": "combined force/couple and phase/contact triggers; 25% earlier-stage regression"} + ], + "implementation_status": "trainer/DR sampler not wired; environment preflight only" + }, + "independent_test": { + "status": "not_materialized_not_executed", + "seed_commitment": [95101, 95102, 95103], + "episodes_per_family_per_seed": 100, + "families": ["nominal_cycle", "stand_120s_regression", "unseen_direction_cones", "unseen_application_points", "unseen_trigger_delays", "simultaneous_force_couple_switch", "sensor_actuator_corners"], + "direction_partition": "hold out azimuth [0.35,0.55] and [-2.55,-2.35] rad at all elevations from train/validation", + "point_partition": "hold out local radius [0.09,0.12] m; train/validation <=0.08 m", + "timing_partition": "hold out edge delays [0.12,0.18] s; train/validation <=0.08 s", + "gate": "Each family AND each training seed: success >=0.95, Wilson 95% lower bound >=0.90, all hardware/nonfinite gates zero violations; report per-phase metrics and failures. No aggregate reward gate.", + "rule": "Generate only after candidate identities/config/source/threshold hashes are locked. Tests cannot tune parameters or select another checkpoint. Any test-driven revision retires this set to regression and requires a fresh pre-registration." + } +} diff --git a/configs/contact_cycle/bounded_diagnostic_p2_extended_v1.json b/configs/contact_cycle/bounded_diagnostic_p2_extended_v1.json new file mode 100644 index 0000000..ef7f80a --- /dev/null +++ b/configs/contact_cycle/bounded_diagnostic_p2_extended_v1.json @@ -0,0 +1,398 @@ +{ + "format": "skillfactory.contact-cycle-evaluation-protocol.v2", + "stage": "P2", + "status": "prospective_development_not_acceptance", + "execution_authorized": false, + "requires_parent_physics_approval": true, + "requires_P1_evidence_review": true, + "accepted_skill": false, + "optimizer_steps": 0, + "num_envs": 1, + "frozen_model": { + "candidate": "logs/contact_cycle/sim_residual_training_v1/prepared01/candidate_u0256.pt", + "candidate_sha256": "c088bb3aecaca751867118858626fd508f7a3983303802d7524353f80802cf99", + "features": "contact-cycle-sim-truth-residual-46-v1", + "learned_policy": "deterministic_mean_residual_plus_unchanged_analytic_reference", + "analytic_reference": "zero_residual_plus_unchanged_analytic_reference", + "verification": "unchanged_verify_bundle_plus_separate_new_evaluation_binding", + "selection_or_tuning": false + }, + "controllers": [ + "learned_policy", + "analytic_reference" + ], + "seeds": [ + 94501, + 94502 + ], + "trial_order": "condition_then_seed_then_controller", + "trial_count": 20, + "cycles_per_trial": 1, + "anchor_sequence": [ + "A", + "B" + ], + "conditions": [ + { + "id": "nominal", + "scenario_id": 0, + "swing_displacement_m": [ + 0.025, + 0.02, + 0.0 + ], + "event_indices": [] + }, + { + "id": "oblique_sustained", + "scenario_id": 1, + "swing_displacement_m": [ + 0.025, + 0.02, + 0.0 + ], + "event_indices": [ + 0 + ] + }, + { + "id": "eccentric_liftoff", + "scenario_id": 2, + "swing_displacement_m": [ + 0.025, + 0.02, + 0.0 + ], + "event_indices": [ + 1 + ] + }, + { + "id": "landing_combination", + "scenario_id": 3, + "swing_displacement_m": [ + 0.025, + 0.02, + 0.0 + ], + "event_indices": [ + 2, + 3 + ] + }, + { + "id": "target_only", + "scenario_id": 0, + "swing_displacement_m": [ + 0.03, + 0.025, + 0.0 + ], + "event_indices": [] + } + ], + "events": [ + { + "scenario": 1, + "body": "virtual_payload", + "frame": "world", + "point_frame": "body", + "point_m": [ + 0.03, + -0.02, + 0.06 + ], + "force_N": [ + 3.0, + -4.0, + -2.0 + ], + "couple_Nm": [ + 0.2, + 0.1, + -0.1 + ], + "trigger": "time", + "phase": 0, + "start_s": 1.0, + "duration_s": 6.5, + "ramp_s": 0.5 + }, + { + "scenario": 2, + "body": "FL_thigh", + "frame": "body", + "point_frame": "body", + "point_m": [ + 0.02, + 0.01, + -0.06 + ], + "force_N": [ + -2.0, + 1.0, + 2.0 + ], + "couple_Nm": [ + 0.1, + -0.1, + 0.2 + ], + "trigger": "liftoff", + "phase": 2, + "start_s": 0.02, + "duration_s": 0.2, + "ramp_s": 0.1 + }, + { + "scenario": 3, + "body": "virtual_payload", + "frame": "mount", + "point_frame": "mount", + "point_m": [ + -0.03, + 0.04, + 0.04 + ], + "force_N": [ + 2.0, + 2.0, + -3.0 + ], + "couple_Nm": [ + -0.2, + 0.3, + 0.1 + ], + "trigger": "phase", + "phase": 4, + "start_s": 0.1, + "duration_s": 0.6, + "ramp_s": 0.15 + }, + { + "scenario": 3, + "body": "FL_calf", + "frame": "world", + "point_frame": "body", + "point_m": [ + 0.01, + 0.0, + -0.08 + ], + "force_N": [ + -1.0, + 1.5, + 0.5 + ], + "couple_Nm": [ + 0.0, + -0.1, + 0.1 + ], + "trigger": "touchdown", + "phase": 4, + "start_s": 0.0, + "duration_s": 0.3, + "ramp_s": 0.1 + } + ], + "clock": { + "physics_dt_s": 0.002, + "control_ticks": 5, + "phase_durations_s": [ + 3.0, + 1.0, + 0.6, + 1.4, + 0.8, + 2.2 + ], + "phase_ticks": [ + 1500, + 500, + 300, + 700, + 400, + 1100 + ], + "cycle_ticks": 4500, + "evidence_phase_counts": [ + 1499, + 500, + 300, + 700, + 400, + 1101 + ], + "evidence_intervals": "right_closed_initial_tick_separate", + "phase_boundary": "next_phase_at_exact_internal_boundary" + }, + "command": { + "anchor_frame": "world_fixed_from_initial_ground_foot_target", + "root_target": "fixed_initial_commanded_mounting_port", + "measured_landing_reanchor": false, + "clearance_m": 0.05, + "swing_arch_m": 0.01, + "actor_target_coordinates": "world_axis_target_minus_commanded_port_origin", + "ik_target_coordinates": "inverse_rotate_actual_root_quaternion_target_minus_actual_root_position", + "noise_or_domain_randomization": false, + "reference_controller_reward_residual_changes": false + }, + "thresholds": { + "functional_source": "configs/contact_cycle/simulation_functional_v1.json#gates", + "functional": { + "minimum_complete_cycles": 1, + "support_final_window_s": 0.5, + "reload_final_window_s": 0.2, + "contact_force_on_N": 2.0, + "support_reload_contact_fraction_min": 0.8, + "support_reload_load_fraction_min": 0.5, + "unload_final_window_s": 0.1, + "unload_remaining_support_fraction_max": 0.8, + "swing_gap_m_min": 0.01, + "swing_air_fraction_min": 0.8, + "swing_contiguous_air_s_min": 0.1, + "swing_xy_progress_fraction_min": 0.5, + "landing_position_error_m_max": 0.05, + "root_height_error_m_max": 0.1, + "normal_force_negative_tolerance_N": 0.5, + "root_tilt_rad_max": 0.6, + "ground_penetration_m_max": 0.002, + "auxiliary_vertical_N_max": 140.0, + "auxiliary_lever_arm_m_max": 0.06, + "guide_vertical_N_max": 1e-06, + "normalized_effort_velocity_max": 1.00001, + "joint_limit_excess_rad_max": 0.01 + }, + "functional_application": "each_complete_cycle_with_signed_requested_xy_progress", + "excluded_nominal_auditor_checks": [ + "nominal_only", + "no_external_disturbance" + ], + "replacement_condition_check": "exact_bound_trial_scenario_and_event_program", + "original_runtime_source": "source/SkillFactory/SkillFactory/legs/contact_cycle/task.py#phase_failures", + "original_runtime": { + "lift_progress_strictly_greater": 0.9, + "lift_gap_m_min": 0.02, + "lift_normal_N_max": 2.0, + "swing_gap_m_min": 0.01, + "reload_progress_strictly_greater": 0.8, + "reload_load_fraction_min": 0.5, + "root_height_below_nominal_m_max": 0.1, + "root_tilt_rad_max": 0.6, + "guide_vertical_N_exact": 0.0, + "nonfinite_allowed": false, + "premature_separation_allowed": false + }, + "contact_source": "configs/contact_cycle/go2_fl_v1.json; contact_cycle/mechanics.py", + "contact": { + "force_on_N": 2.0, + "force_off_N": 0.5, + "dwell_ticks": 5, + "separation_on_m": 0.001, + "separation_off_m": 0.0005, + "initial_acquisition_deadline_tick": 100, + "initial_acquisition_load_fraction_min": 0.5, + "initial_acquisition_gap_m_max": 0.0005, + "initial_acquisition_rearmed_at_seam": false + }, + "P1_margins_source": "parent_approved_prospective_development_v1_not_P0_pass_claim", + "P1_height_value_precedent": "configs/contact_cycle/acceptance_v1.json#gates.height_error_m_max", + "P1_margins": { + "root_horizontal_error_m_max": 0.03, + "root_absolute_z_error_m_max": 0.06, + "seam_to_initial_root_horizontal_drift_m_max": 0.02, + "reload_mean_3d_error_m_max": 0.02, + "reload_instantaneous_3d_error_m_max": 0.03, + "stance_slip_per_cycle_m_max": 0.03, + "stance_slip_per_run_m_max": 0.06, + "repeat_B_reload_mean_xy_drift_m_max": 0.015, + "unload_remaining_support_fraction_max": 0.7, + "premature_separation_allowed": false, + "lift_gap_m_min": 0.022, + "lift_normal_N_max": 2.0, + "lift_deadline_local_tick": 2260, + "lift_dwell_ticks": 5, + "lift_onset_phase": 2, + "reload_final_dwell_ticks": 50, + "reload_gap_m_max": 0.0005, + "reload_load_fraction_min": 0.8, + "reload_load_fraction_max": 1.2, + "reload_contact_required": true, + "reload_separation_allowed": false + }, + "P1_margins_role": "diagnostic_only", + "historical_swing_clearance_diagnostic_m": 0.02, + "event_delivery": { + "fired_and_completed_required": true, + "area": "duration_s_minus_ramp_s", + "area_tolerance_s": 0.002 + }, + "event_delivery_source": "source/SkillFactory/SkillFactory/legs/contact_cycle/mechanics.py#delivered_events" + }, + "resources": { + "proposal_only": true, + "max_launches": 3, + "max_sim_step_ticks_including_initialization": 120000, + "max_rollout_ticks": 117000, + "max_initialization_reset_ticks": 3000, + "max_control_steps": 23400, + "aggregate_same_boot_window_s": 14400, + "max_artifact_bytes": 1073741824, + "cleanup_reserve_s": 120, + "P1_max_launches": 2, + "P1_launch_wall_cap_s": 600, + "P1_rollout_ticks_per_launch": 13500, + "P2_max_launches": 1, + "P2_launch_wall_cap_s": 10800, + "P2_rollout_ticks_per_launch": 90000, + "overlapping_jobs": false, + "retries_or_resume": false, + "count_hook_before_construction": true + }, + "handoff": { + "seam_order": [ + "physics_and_latches", + "safety_and_endpoint_snapshot", + "cycle_gate", + "command_metadata_only", + "single_reference_refresh", + "next_observation" + ], + "carry_state": [ + "physical_pose_velocity", + "previous_action_processed_target_residual", + "actuator_sensor_delay", + "IK_warm_start_action_refresh_count", + "contact_latches_timers", + "separation_onset_armed_premature", + "initial_acquisition", + "global_substeps", + "RNG", + "one_shot_event_scheduler" + ], + "reset_or_pose_write_within_trial": false, + "completion": "protocol_complete_without_terminal_reset", + "seam_admission_failure": "stop_at_endpoint_no_extension_or_wait", + "initial_reset_per_trial": true, + "P1_separate_fresh_launch_per_controller": true, + "P2_reset_only_between_trials": true, + "failure_check": "after_each_scene_tick_before_another_sim_step", + "failure_reporting": "first_tick_all_causes_passed_prefix_only_remaining_gates_not_evaluated", + "P2_functional_failure": "retain_row_continue_matrix_if_safe", + "fatal_failures": [ + "hardware", + "nonfinite", + "identity", + "illegal_reset_or_pose_write", + "storage_or_recording", + "process", + "budget_expired" + ], + "pairing": "same_seed_initial_state_targets_trigger_rule_not_equal_wall_time_force_samples", + "P2_all_rows_required": true, + "P2_new_seeds_are": "development_repeatability_not_independent_robustness" + }, + "resource_profile": "p2-wall10800-aggregate14400-v1" +} diff --git a/configs/contact_cycle/bounded_diagnostic_p2_v1.json b/configs/contact_cycle/bounded_diagnostic_p2_v1.json new file mode 100644 index 0000000..ad30dab --- /dev/null +++ b/configs/contact_cycle/bounded_diagnostic_p2_v1.json @@ -0,0 +1,397 @@ +{ + "format": "skillfactory.contact-cycle-evaluation-protocol.v1", + "stage": "P2", + "status": "prospective_development_not_acceptance", + "execution_authorized": false, + "requires_parent_physics_approval": true, + "requires_P1_evidence_review": true, + "accepted_skill": false, + "optimizer_steps": 0, + "num_envs": 1, + "frozen_model": { + "candidate": "logs/contact_cycle/sim_residual_training_v1/prepared01/candidate_u0256.pt", + "candidate_sha256": "c088bb3aecaca751867118858626fd508f7a3983303802d7524353f80802cf99", + "features": "contact-cycle-sim-truth-residual-46-v1", + "learned_policy": "deterministic_mean_residual_plus_unchanged_analytic_reference", + "analytic_reference": "zero_residual_plus_unchanged_analytic_reference", + "verification": "unchanged_verify_bundle_plus_separate_new_evaluation_binding", + "selection_or_tuning": false + }, + "controllers": [ + "learned_policy", + "analytic_reference" + ], + "seeds": [ + 94501, + 94502 + ], + "trial_order": "condition_then_seed_then_controller", + "trial_count": 20, + "cycles_per_trial": 1, + "anchor_sequence": [ + "A", + "B" + ], + "conditions": [ + { + "id": "nominal", + "scenario_id": 0, + "swing_displacement_m": [ + 0.025, + 0.02, + 0.0 + ], + "event_indices": [] + }, + { + "id": "oblique_sustained", + "scenario_id": 1, + "swing_displacement_m": [ + 0.025, + 0.02, + 0.0 + ], + "event_indices": [ + 0 + ] + }, + { + "id": "eccentric_liftoff", + "scenario_id": 2, + "swing_displacement_m": [ + 0.025, + 0.02, + 0.0 + ], + "event_indices": [ + 1 + ] + }, + { + "id": "landing_combination", + "scenario_id": 3, + "swing_displacement_m": [ + 0.025, + 0.02, + 0.0 + ], + "event_indices": [ + 2, + 3 + ] + }, + { + "id": "target_only", + "scenario_id": 0, + "swing_displacement_m": [ + 0.03, + 0.025, + 0.0 + ], + "event_indices": [] + } + ], + "events": [ + { + "scenario": 1, + "body": "virtual_payload", + "frame": "world", + "point_frame": "body", + "point_m": [ + 0.03, + -0.02, + 0.06 + ], + "force_N": [ + 3.0, + -4.0, + -2.0 + ], + "couple_Nm": [ + 0.2, + 0.1, + -0.1 + ], + "trigger": "time", + "phase": 0, + "start_s": 1.0, + "duration_s": 6.5, + "ramp_s": 0.5 + }, + { + "scenario": 2, + "body": "FL_thigh", + "frame": "body", + "point_frame": "body", + "point_m": [ + 0.02, + 0.01, + -0.06 + ], + "force_N": [ + -2.0, + 1.0, + 2.0 + ], + "couple_Nm": [ + 0.1, + -0.1, + 0.2 + ], + "trigger": "liftoff", + "phase": 2, + "start_s": 0.02, + "duration_s": 0.2, + "ramp_s": 0.1 + }, + { + "scenario": 3, + "body": "virtual_payload", + "frame": "mount", + "point_frame": "mount", + "point_m": [ + -0.03, + 0.04, + 0.04 + ], + "force_N": [ + 2.0, + 2.0, + -3.0 + ], + "couple_Nm": [ + -0.2, + 0.3, + 0.1 + ], + "trigger": "phase", + "phase": 4, + "start_s": 0.1, + "duration_s": 0.6, + "ramp_s": 0.15 + }, + { + "scenario": 3, + "body": "FL_calf", + "frame": "world", + "point_frame": "body", + "point_m": [ + 0.01, + 0.0, + -0.08 + ], + "force_N": [ + -1.0, + 1.5, + 0.5 + ], + "couple_Nm": [ + 0.0, + -0.1, + 0.1 + ], + "trigger": "touchdown", + "phase": 4, + "start_s": 0.0, + "duration_s": 0.3, + "ramp_s": 0.1 + } + ], + "clock": { + "physics_dt_s": 0.002, + "control_ticks": 5, + "phase_durations_s": [ + 3.0, + 1.0, + 0.6, + 1.4, + 0.8, + 2.2 + ], + "phase_ticks": [ + 1500, + 500, + 300, + 700, + 400, + 1100 + ], + "cycle_ticks": 4500, + "evidence_phase_counts": [ + 1499, + 500, + 300, + 700, + 400, + 1101 + ], + "evidence_intervals": "right_closed_initial_tick_separate", + "phase_boundary": "next_phase_at_exact_internal_boundary" + }, + "command": { + "anchor_frame": "world_fixed_from_initial_ground_foot_target", + "root_target": "fixed_initial_commanded_mounting_port", + "measured_landing_reanchor": false, + "clearance_m": 0.05, + "swing_arch_m": 0.01, + "actor_target_coordinates": "world_axis_target_minus_commanded_port_origin", + "ik_target_coordinates": "inverse_rotate_actual_root_quaternion_target_minus_actual_root_position", + "noise_or_domain_randomization": false, + "reference_controller_reward_residual_changes": false + }, + "thresholds": { + "functional_source": "configs/contact_cycle/simulation_functional_v1.json#gates", + "functional": { + "minimum_complete_cycles": 1, + "support_final_window_s": 0.5, + "reload_final_window_s": 0.2, + "contact_force_on_N": 2.0, + "support_reload_contact_fraction_min": 0.8, + "support_reload_load_fraction_min": 0.5, + "unload_final_window_s": 0.1, + "unload_remaining_support_fraction_max": 0.8, + "swing_gap_m_min": 0.01, + "swing_air_fraction_min": 0.8, + "swing_contiguous_air_s_min": 0.1, + "swing_xy_progress_fraction_min": 0.5, + "landing_position_error_m_max": 0.05, + "root_height_error_m_max": 0.1, + "normal_force_negative_tolerance_N": 0.5, + "root_tilt_rad_max": 0.6, + "ground_penetration_m_max": 0.002, + "auxiliary_vertical_N_max": 140.0, + "auxiliary_lever_arm_m_max": 0.06, + "guide_vertical_N_max": 1e-06, + "normalized_effort_velocity_max": 1.00001, + "joint_limit_excess_rad_max": 0.01 + }, + "functional_application": "each_complete_cycle_with_signed_requested_xy_progress", + "excluded_nominal_auditor_checks": [ + "nominal_only", + "no_external_disturbance" + ], + "replacement_condition_check": "exact_bound_trial_scenario_and_event_program", + "original_runtime_source": "source/SkillFactory/SkillFactory/legs/contact_cycle/task.py#phase_failures", + "original_runtime": { + "lift_progress_strictly_greater": 0.9, + "lift_gap_m_min": 0.02, + "lift_normal_N_max": 2.0, + "swing_gap_m_min": 0.01, + "reload_progress_strictly_greater": 0.8, + "reload_load_fraction_min": 0.5, + "root_height_below_nominal_m_max": 0.1, + "root_tilt_rad_max": 0.6, + "guide_vertical_N_exact": 0.0, + "nonfinite_allowed": false, + "premature_separation_allowed": false + }, + "contact_source": "configs/contact_cycle/go2_fl_v1.json; contact_cycle/mechanics.py", + "contact": { + "force_on_N": 2.0, + "force_off_N": 0.5, + "dwell_ticks": 5, + "separation_on_m": 0.001, + "separation_off_m": 0.0005, + "initial_acquisition_deadline_tick": 100, + "initial_acquisition_load_fraction_min": 0.5, + "initial_acquisition_gap_m_max": 0.0005, + "initial_acquisition_rearmed_at_seam": false + }, + "P1_margins_source": "parent_approved_prospective_development_v1_not_P0_pass_claim", + "P1_height_value_precedent": "configs/contact_cycle/acceptance_v1.json#gates.height_error_m_max", + "P1_margins": { + "root_horizontal_error_m_max": 0.03, + "root_absolute_z_error_m_max": 0.06, + "seam_to_initial_root_horizontal_drift_m_max": 0.02, + "reload_mean_3d_error_m_max": 0.02, + "reload_instantaneous_3d_error_m_max": 0.03, + "stance_slip_per_cycle_m_max": 0.03, + "stance_slip_per_run_m_max": 0.06, + "repeat_B_reload_mean_xy_drift_m_max": 0.015, + "unload_remaining_support_fraction_max": 0.7, + "premature_separation_allowed": false, + "lift_gap_m_min": 0.022, + "lift_normal_N_max": 2.0, + "lift_deadline_local_tick": 2260, + "lift_dwell_ticks": 5, + "lift_onset_phase": 2, + "reload_final_dwell_ticks": 50, + "reload_gap_m_max": 0.0005, + "reload_load_fraction_min": 0.8, + "reload_load_fraction_max": 1.2, + "reload_contact_required": true, + "reload_separation_allowed": false + }, + "P1_margins_role": "diagnostic_only", + "historical_swing_clearance_diagnostic_m": 0.02, + "event_delivery": { + "fired_and_completed_required": true, + "area": "duration_s_minus_ramp_s", + "area_tolerance_s": 0.002 + }, + "event_delivery_source": "source/SkillFactory/SkillFactory/legs/contact_cycle/mechanics.py#delivered_events" + }, + "resources": { + "proposal_only": true, + "max_launches": 3, + "max_sim_step_ticks_including_initialization": 120000, + "max_rollout_ticks": 117000, + "max_initialization_reset_ticks": 3000, + "max_control_steps": 23400, + "aggregate_same_boot_window_s": 3600, + "max_artifact_bytes": 1073741824, + "cleanup_reserve_s": 120, + "P1_max_launches": 2, + "P1_launch_wall_cap_s": 600, + "P1_rollout_ticks_per_launch": 13500, + "P2_max_launches": 1, + "P2_launch_wall_cap_s": 2100, + "P2_rollout_ticks_per_launch": 90000, + "overlapping_jobs": false, + "retries_or_resume": false, + "count_hook_before_construction": true + }, + "handoff": { + "seam_order": [ + "physics_and_latches", + "safety_and_endpoint_snapshot", + "cycle_gate", + "command_metadata_only", + "single_reference_refresh", + "next_observation" + ], + "carry_state": [ + "physical_pose_velocity", + "previous_action_processed_target_residual", + "actuator_sensor_delay", + "IK_warm_start_action_refresh_count", + "contact_latches_timers", + "separation_onset_armed_premature", + "initial_acquisition", + "global_substeps", + "RNG", + "one_shot_event_scheduler" + ], + "reset_or_pose_write_within_trial": false, + "completion": "protocol_complete_without_terminal_reset", + "seam_admission_failure": "stop_at_endpoint_no_extension_or_wait", + "initial_reset_per_trial": true, + "P1_separate_fresh_launch_per_controller": true, + "P2_reset_only_between_trials": true, + "failure_check": "after_each_scene_tick_before_another_sim_step", + "failure_reporting": "first_tick_all_causes_passed_prefix_only_remaining_gates_not_evaluated", + "P2_functional_failure": "retain_row_continue_matrix_if_safe", + "fatal_failures": [ + "hardware", + "nonfinite", + "identity", + "illegal_reset_or_pose_write", + "storage_or_recording", + "process", + "budget_expired" + ], + "pairing": "same_seed_initial_state_targets_trigger_rule_not_equal_wall_time_force_samples", + "P2_all_rows_required": true, + "P2_new_seeds_are": "development_repeatability_not_independent_robustness" + } +} diff --git a/configs/contact_cycle/continuous_eval_p1_v1.json b/configs/contact_cycle/continuous_eval_p1_v1.json new file mode 100644 index 0000000..245da3e --- /dev/null +++ b/configs/contact_cycle/continuous_eval_p1_v1.json @@ -0,0 +1,246 @@ +{ + "format": "skillfactory.contact-cycle-evaluation-protocol.v1", + "stage": "P1", + "status": "prospective_development_not_acceptance", + "execution_authorized": false, + "requires_parent_physics_approval": true, + "requires_P1_evidence_review": false, + "accepted_skill": false, + "optimizer_steps": 0, + "num_envs": 1, + "frozen_model": { + "candidate": "logs/contact_cycle/sim_residual_training_v1/prepared01/candidate_u0256.pt", + "candidate_sha256": "c088bb3aecaca751867118858626fd508f7a3983303802d7524353f80802cf99", + "features": "contact-cycle-sim-truth-residual-46-v1", + "learned_policy": "deterministic_mean_residual_plus_unchanged_analytic_reference", + "analytic_reference": "zero_residual_plus_unchanged_analytic_reference", + "verification": "unchanged_verify_bundle_plus_separate_new_evaluation_binding", + "selection_or_tuning": false + }, + "controllers": [ + "learned_policy", + "analytic_reference" + ], + "seeds": [ + 93501 + ], + "trial_order": "condition_then_seed_then_controller", + "trial_count": 2, + "cycles_per_trial": 3, + "anchor_sequence": [ + "A", + "B", + "A", + "B" + ], + "conditions": [ + { + "id": "nominal", + "scenario_id": 0, + "swing_displacement_m": [ + 0.025, + 0.02, + 0.0 + ], + "event_indices": [] + } + ], + "events": [], + "clock": { + "physics_dt_s": 0.002, + "control_ticks": 5, + "phase_durations_s": [ + 3.0, + 1.0, + 0.6, + 1.4, + 0.8, + 2.2 + ], + "phase_ticks": [ + 1500, + 500, + 300, + 700, + 400, + 1100 + ], + "cycle_ticks": 4500, + "evidence_phase_counts": [ + 1499, + 500, + 300, + 700, + 400, + 1101 + ], + "evidence_intervals": "right_closed_initial_tick_separate", + "phase_boundary": "next_phase_at_exact_internal_boundary" + }, + "command": { + "anchor_frame": "world_fixed_from_initial_ground_foot_target", + "root_target": "fixed_initial_commanded_mounting_port", + "measured_landing_reanchor": false, + "clearance_m": 0.05, + "swing_arch_m": 0.01, + "actor_target_coordinates": "world_axis_target_minus_commanded_port_origin", + "ik_target_coordinates": "inverse_rotate_actual_root_quaternion_target_minus_actual_root_position", + "noise_or_domain_randomization": false, + "reference_controller_reward_residual_changes": false + }, + "thresholds": { + "functional_source": "configs/contact_cycle/simulation_functional_v1.json#gates", + "functional": { + "minimum_complete_cycles": 1, + "support_final_window_s": 0.5, + "reload_final_window_s": 0.2, + "contact_force_on_N": 2.0, + "support_reload_contact_fraction_min": 0.8, + "support_reload_load_fraction_min": 0.5, + "unload_final_window_s": 0.1, + "unload_remaining_support_fraction_max": 0.8, + "swing_gap_m_min": 0.01, + "swing_air_fraction_min": 0.8, + "swing_contiguous_air_s_min": 0.1, + "swing_xy_progress_fraction_min": 0.5, + "landing_position_error_m_max": 0.05, + "root_height_error_m_max": 0.1, + "normal_force_negative_tolerance_N": 0.5, + "root_tilt_rad_max": 0.6, + "ground_penetration_m_max": 0.002, + "auxiliary_vertical_N_max": 140.0, + "auxiliary_lever_arm_m_max": 0.06, + "guide_vertical_N_max": 1e-06, + "normalized_effort_velocity_max": 1.00001, + "joint_limit_excess_rad_max": 0.01 + }, + "functional_application": "each_complete_cycle_with_signed_requested_xy_progress", + "excluded_nominal_auditor_checks": [ + "nominal_only", + "no_external_disturbance" + ], + "replacement_condition_check": "exact_bound_trial_scenario_and_event_program", + "original_runtime_source": "source/SkillFactory/SkillFactory/legs/contact_cycle/task.py#phase_failures", + "original_runtime": { + "lift_progress_strictly_greater": 0.9, + "lift_gap_m_min": 0.02, + "lift_normal_N_max": 2.0, + "swing_gap_m_min": 0.01, + "reload_progress_strictly_greater": 0.8, + "reload_load_fraction_min": 0.5, + "root_height_below_nominal_m_max": 0.1, + "root_tilt_rad_max": 0.6, + "guide_vertical_N_exact": 0.0, + "nonfinite_allowed": false, + "premature_separation_allowed": false + }, + "contact_source": "configs/contact_cycle/go2_fl_v1.json; contact_cycle/mechanics.py", + "contact": { + "force_on_N": 2.0, + "force_off_N": 0.5, + "dwell_ticks": 5, + "separation_on_m": 0.001, + "separation_off_m": 0.0005, + "initial_acquisition_deadline_tick": 100, + "initial_acquisition_load_fraction_min": 0.5, + "initial_acquisition_gap_m_max": 0.0005, + "initial_acquisition_rearmed_at_seam": false + }, + "P1_margins_source": "parent_approved_prospective_development_v1_not_P0_pass_claim", + "P1_height_value_precedent": "configs/contact_cycle/acceptance_v1.json#gates.height_error_m_max", + "P1_margins": { + "root_horizontal_error_m_max": 0.03, + "root_absolute_z_error_m_max": 0.06, + "seam_to_initial_root_horizontal_drift_m_max": 0.02, + "reload_mean_3d_error_m_max": 0.02, + "reload_instantaneous_3d_error_m_max": 0.03, + "stance_slip_per_cycle_m_max": 0.03, + "stance_slip_per_run_m_max": 0.06, + "repeat_B_reload_mean_xy_drift_m_max": 0.015, + "unload_remaining_support_fraction_max": 0.7, + "premature_separation_allowed": false, + "lift_gap_m_min": 0.022, + "lift_normal_N_max": 2.0, + "lift_deadline_local_tick": 2260, + "lift_dwell_ticks": 5, + "lift_onset_phase": 2, + "reload_final_dwell_ticks": 50, + "reload_gap_m_max": 0.0005, + "reload_load_fraction_min": 0.8, + "reload_load_fraction_max": 1.2, + "reload_contact_required": true, + "reload_separation_allowed": false + }, + "P1_margins_role": "gate", + "historical_swing_clearance_diagnostic_m": 0.02, + "event_delivery": { + "fired_and_completed_required": true, + "area": "duration_s_minus_ramp_s", + "area_tolerance_s": 0.002 + }, + "event_delivery_source": "source/SkillFactory/SkillFactory/legs/contact_cycle/mechanics.py#delivered_events" + }, + "resources": { + "proposal_only": true, + "max_launches": 3, + "max_sim_step_ticks_including_initialization": 120000, + "max_rollout_ticks": 117000, + "max_initialization_reset_ticks": 3000, + "max_control_steps": 23400, + "aggregate_same_boot_window_s": 3600, + "max_artifact_bytes": 1073741824, + "cleanup_reserve_s": 120, + "P1_max_launches": 2, + "P1_launch_wall_cap_s": 600, + "P1_rollout_ticks_per_launch": 13500, + "P2_max_launches": 1, + "P2_launch_wall_cap_s": 2100, + "P2_rollout_ticks_per_launch": 90000, + "overlapping_jobs": false, + "retries_or_resume": false, + "count_hook_before_construction": true + }, + "handoff": { + "seam_order": [ + "physics_and_latches", + "safety_and_endpoint_snapshot", + "cycle_gate", + "command_metadata_only", + "single_reference_refresh", + "next_observation" + ], + "carry_state": [ + "physical_pose_velocity", + "previous_action_processed_target_residual", + "actuator_sensor_delay", + "IK_warm_start_action_refresh_count", + "contact_latches_timers", + "separation_onset_armed_premature", + "initial_acquisition", + "global_substeps", + "RNG", + "one_shot_event_scheduler" + ], + "reset_or_pose_write_within_trial": false, + "completion": "protocol_complete_without_terminal_reset", + "seam_admission_failure": "stop_at_endpoint_no_extension_or_wait", + "initial_reset_per_trial": true, + "P1_separate_fresh_launch_per_controller": true, + "P2_reset_only_between_trials": true, + "failure_check": "after_each_scene_tick_before_another_sim_step", + "failure_reporting": "first_tick_all_causes_passed_prefix_only_remaining_gates_not_evaluated", + "P2_functional_failure": "retain_row_continue_matrix_if_safe", + "fatal_failures": [ + "hardware", + "nonfinite", + "identity", + "illegal_reset_or_pose_write", + "storage_or_recording", + "process", + "budget_expired" + ], + "pairing": "same_seed_initial_state_targets_trigger_rule_not_equal_wall_time_force_samples", + "P2_all_rows_required": true, + "P2_new_seeds_are": "development_repeatability_not_independent_robustness" + } +} diff --git a/configs/contact_cycle/diagnostic_proposal_v1.json b/configs/contact_cycle/diagnostic_proposal_v1.json new file mode 100644 index 0000000..272db57 --- /dev/null +++ b/configs/contact_cycle/diagnostic_proposal_v1.json @@ -0,0 +1,55 @@ +{ + "schema_version": 1, + "status": "diagnostic_proposal_requires_separate_manifest_bound_grant", + "contract": "go2-fl-contact-cycle-v1", + "gates": { + "stance_contact_fraction_min": 0.98, + "stance_load_mae_weight_fraction_max": 0.25, + "height_error_m_max": 0.06, + "swing_rms_m_max": 0.025, + "swing_clearance_m_min": 0.02, + "swing_contact_fraction_max": 0.01, + "stance_slip_m_max": 0.03, + "landing_peak_weight_fraction_max": 3.0, + "recovery_s_max": 2.5, + "joint_limit_excess_rad_max": 0.01, + "normalized_effort_velocity_max": 1.00001, + "unilateral_residual_N_max": 0.5, + "friction_residual_N_max": 2.0, + "friction_cone_mu_upper": 0.8 + }, + "proposed_training": { + "authorization": null, + "seeds": [91101], + "num_envs": 128, + "horizon": 32, + "updates_per_seed": 512, + "max_total_transitions": 2097152, + "max_total_wall_s": 1800, + "max_artifact_bytes": 1073741824, + "resume_counts_toward_budget": true, + "selection": "Diagnostic only: validate at updates256,512 with existing seed93101; failures=16, successes=sum clipped normalized rms/load/slip, mean over all128 rows; earliest tie. No independent acceptance eligibility.", + "curriculum": [ + {"updates": [1, 512], "distribution": "existing stage0: 75% nominal cycle without disturbance,25% stand-only regression; validation retains all4 development scenarios"} + ], + "stop_rule": "Stop at512 updates or any existing safety/identity/budget/runtime failure. No retries,resume,warm start,extra checkpoints/evaluations,or automatic promotion to a three-seed study.", + "implementation_status": "diagnostic schema uses the same task/PPO/sampler; new initialization and diagnostics already physics-regression tested" + }, + "diagnostic_interpretation": { + "not_skill_acceptance": true, + "questions": [ + "At updates256 and512, how many scenario0 baseline validation first episodes complete all6 phases and gates?", + "In final128 training updates, what is control-sample occupancy for each phase and each failure-cause count?", + "How often do contact acquisition timeout,premature separation,lift deadline and low height occur?", + "What do retained first4 biased validation traces show for actual initial state,actor input/action and latches?", + "Are PPO values/advantages finite and does observed runtime/artifact cost fit this pilot cap?" + ], + "progress_screen": "Report baseline success fractions at BOTH checkpoints. If final baseline success is0 or final128 training updates never reach swing,land or reload, record failure of this minimal progression screen; otherwise record limited nominal progress only. Neither outcome permits automatic additional training or acceptance.", + "limitations": "One reused development seed,not statistical replication. Stand-only training shares the phase clock,so phase occupancy alone does not prove flight; require full-cycle baseline validation and actual gap/contact traces. Disturbance validation is out-of-stage and cannot justify changing gates or training longer. No advantage/reward increase is an acceptance criterion." + }, + "independent_test": { + "status": "not_materialized_not_executed", + "seed_commitment": [95101, 95102, 95103], + "rule": "No independent evaluation or release in this diagnostic pilot. Original acceptance proposal and reserved domains remain unchanged." + } +} diff --git a/configs/contact_cycle/go2_fl_v1.json b/configs/contact_cycle/go2_fl_v1.json new file mode 100644 index 0000000..1a6fdae --- /dev/null +++ b/configs/contact_cycle/go2_fl_v1.json @@ -0,0 +1,22 @@ +{ + "schema_version": 1, + "contract": "go2-fl-contact-cycle-v1", + "support_config": "configs/standing/go2_fl_support_v0.json", + "phase_durations_s": [3.0, 1.0, 0.6, 1.4, 0.8, 2.2], + "swing_displacement_m": [0.025, 0.02, 0.0], + "clearance_m": 0.05, + "initialization": {"foot_clearance_m": 0.0, "contact_deadline_s": 0.2, "min_load_fraction": 0.5, "dwell_s": 0.01}, + "contact_transition": {"separation_on_m": 0.001, "separation_off_m": 0.0005, "dwell_s": 0.01, "unload_tracking_scale_m": 0.005}, + "support": {"kp_N_m": 1800.0, "kd_Ns_m": 120.0, "max_N": 140.0}, + "base_motion": {"amplitude_m": [0.0, 0.0, 0.0], "frequency_Hz": 0.2}, + "sensor": {"joint_noise_std_rad": 0.0, "velocity_noise_std_rad_s": 0.0, "wrench_noise_std": 0.0, "delay_steps": 0}, + "actuator": {"gain": 1.0, "bias_rad": [0.0, 0.0, 0.0], "delay_steps": 0}, + "limits": {"force_N": 10.0, "couple_Nm": 1.0, "point_radius_m": 0.12, "force_rate_N_s": 100.0, "couple_rate_Nm_s": 10.0}, + "scenarios": ["baseline", "oblique_sustained", "eccentric_liftoff", "landing_combination"], + "events": [ + {"scenario": 1, "body": "virtual_payload", "frame": "world", "point_frame": "body", "point_m": [0.03, -0.02, 0.06], "force_N": [3.0, -4.0, -2.0], "couple_Nm": [0.2, 0.1, -0.1], "trigger": "time", "phase": 0, "start_s": 1.0, "duration_s": 6.5, "ramp_s": 0.5}, + {"scenario": 2, "body": "FL_thigh", "frame": "body", "point_frame": "body", "point_m": [0.02, 0.01, -0.06], "force_N": [-2.0, 1.0, 2.0], "couple_Nm": [0.1, -0.1, 0.2], "trigger": "liftoff", "phase": 2, "start_s": 0.02, "duration_s": 0.2, "ramp_s": 0.1}, + {"scenario": 3, "body": "virtual_payload", "frame": "mount", "point_frame": "mount", "point_m": [-0.03, 0.04, 0.04], "force_N": [2.0, 2.0, -3.0], "couple_Nm": [-0.2, 0.3, 0.1], "trigger": "phase", "phase": 4, "start_s": 0.1, "duration_s": 0.6, "ramp_s": 0.15}, + {"scenario": 3, "body": "FL_calf", "frame": "world", "point_frame": "body", "point_m": [0.01, 0.0, -0.08], "force_N": [-1.0, 1.5, 0.5], "couple_Nm": [0.0, -0.1, 0.1], "trigger": "touchdown", "phase": 4, "start_s": 0.0, "duration_s": 0.3, "ramp_s": 0.1} + ] +} diff --git a/configs/contact_cycle/sim_training_proposal_v1.json b/configs/contact_cycle/sim_training_proposal_v1.json new file mode 100644 index 0000000..8f2aecc --- /dev/null +++ b/configs/contact_cycle/sim_training_proposal_v1.json @@ -0,0 +1,17 @@ +{ + "format": "skillfactory.contact-cycle-sim-residual-proposal.v1", + "contract": "go2-fl-contact-cycle-sim-residual-v1", + "parent_plan": "docs/contact_cycle_sim_training_plan.md", + "scope": "fresh_nominal_only_pilot_no_external_DR_holdout", + "actual_training_launches": 1, + "resume": false, + "warm_start": false, + "accepted_skill": false, + "baseline": "u0_untrained_zero_residual_network_requires_128_of_128_functional_passes", + "selection": "u64_u128_u192_u256_lexicographic_failure_fraction_then_all_row_mean_cost_earliest_tie", + "row_cost": "failed=16; success=min(height_rms/.06,2)+min(swing_rms/.025,2)+min(landing_error/.05,2)", + "physics": "unchanged_reference_full_phase_residual_limit_0.05rad_500Hz_safety", + "validation": "same128_nominal_envs_seed93501_first_episode_only_full9s", + "post_result_changes": "separate_version_no_second_optimization_authorized", + "supervisor_timeout_s": 3480 +} diff --git a/configs/contact_cycle/sim_training_rootxy_proposal_v2.json b/configs/contact_cycle/sim_training_rootxy_proposal_v2.json new file mode 100644 index 0000000..6c8617b --- /dev/null +++ b/configs/contact_cycle/sim_training_rootxy_proposal_v2.json @@ -0,0 +1,20 @@ +{ + "format": "skillfactory.contact-cycle-sim-rootxy-proposal.v2", + "contract": "go2-fl-contact-cycle-sim-rootxy-v2", + "actual_training_launches": 1, + "resume": false, + "accepted_skill": false, + "initialization": "actor_log_std_exact_normalization_only_fresh_critic_Adam_RNG", + "baselines": [ + "analytic_zero", + "frozen_u256" + ], + "warmstart_u0": "CPU_exact_actor_equivalence_not_physical_repeat_or_full_state_equivalence", + "validation_reservation": "128*900*7_no_refund_actual_two_baselines_plus_four_development", + "selection": "all128_original_functional_and_full4500ticks_XY_le_0.03_then_mean_full_RMS_tracking_earliest", + "termination": "original_safety_only_no_early_30mm_termination", + "scope": "independent_nominal_9s_single_cycle_not_continuous_learning", + "launch_barrier": "P2_review_then_supervised_adapter_review_then_fresh_parent_manifest_hash_grant", + "native_storage": "aggregate_new_owned_1GiB_MONITOR_and_stop_not_hard_quota", + "downstream": "holdout_P1_P2_require_separate_grants_not_remaining_pilot_budget" +} diff --git a/configs/contact_cycle/sim_training_rootxy_v2.json b/configs/contact_cycle/sim_training_rootxy_v2.json new file mode 100644 index 0000000..802de6e --- /dev/null +++ b/configs/contact_cycle/sim_training_rootxy_v2.json @@ -0,0 +1,47 @@ +{ + "format": "skillfactory.contact-cycle-sim-rootxy-training.v2", + "contract": "go2-fl-contact-cycle-sim-rootxy-v2", + "task_id": "SkillFactory-Go2FL-ContactCycleSimRootXY-v2", + "features": "contact-cycle-sim-truth-residual-46-v1", + "proposal": "configs/contact_cycle/sim_training_rootxy_proposal_v2.json", + "task_config": "configs/contact_cycle/simulation_residual_rootxy_v2.json", + "functional_config": "configs/contact_cycle/simulation_functional_v1.json", + "safety_config": "configs/contact_cycle/acceptance_v1.json", + "seeds": [ + 92511 + ], + "validation_seed": 93511, + "num_envs": 128, + "horizon": 32, + "updates_per_seed": 256, + "validation_updates": [ + 64, + 128, + 192, + 256 + ], + "max_total_transitions": 1048576, + "max_total_wall_s": 7200, + "max_artifact_bytes": 1073741824, + "algorithm": { + "hidden": 128, + "initial_std": 0.1, + "learning_rate": 0.0003, + "adam_eps": 1e-05, + "gamma": 0.99, + "gae_lambda": 0.95, + "clip_ratio": 0.2, + "epochs": 4, + "minibatch_size": 1024, + "value_coefficient": 0.5, + "max_gradient_norm": 1.0, + "target_kl": 0.02, + "failure_penalty": 10.0 + }, + "holdout_seed": 95511, + "cleanup_reserve_s": 120, + "max_validation_transitions": 806400, + "source_checkpoint": "logs/contact_cycle/sim_residual_training_v1/prepared01/candidate_u0256.pt", + "source_sha256": "c088bb3aecaca751867118858626fd508f7a3983303802d7524353f80802cf99", + "source_manifest_sha256": "a70a226f176104414d2da295f08cce230f6b91ed3e2b78490c78f7ffce95ab01" +} diff --git a/configs/contact_cycle/sim_training_v1.json b/configs/contact_cycle/sim_training_v1.json new file mode 100644 index 0000000..810aca6 --- /dev/null +++ b/configs/contact_cycle/sim_training_v1.json @@ -0,0 +1,42 @@ +{ + "format": "skillfactory.contact-cycle-sim-residual-training.v1", + "contract": "go2-fl-contact-cycle-sim-residual-v1", + "task_id": "SkillFactory-Go2FL-ContactCycleSim-v1", + "features": "contact-cycle-sim-truth-residual-46-v1", + "proposal": "configs/contact_cycle/sim_training_proposal_v1.json", + "task_config": "configs/contact_cycle/simulation_residual_v1.json", + "functional_config": "configs/contact_cycle/simulation_functional_v1.json", + "safety_config": "configs/contact_cycle/acceptance_v1.json", + "seeds": [ + 91501 + ], + "validation_seed": 93501, + "num_envs": 128, + "horizon": 32, + "updates_per_seed": 256, + "validation_updates": [ + 0, + 64, + 128, + 192, + 256 + ], + "max_total_transitions": 1048576, + "max_total_wall_s": 3600, + "max_artifact_bytes": 1073741824, + "algorithm": { + "hidden": 128, + "initial_std": 0.1, + "learning_rate": 0.0003, + "adam_eps": 1e-05, + "gamma": 0.99, + "gae_lambda": 0.95, + "clip_ratio": 0.2, + "epochs": 4, + "minibatch_size": 1024, + "value_coefficient": 0.5, + "max_gradient_norm": 1.0, + "target_kl": 0.02, + "failure_penalty": 10.0 + } +} diff --git a/configs/contact_cycle/simulation_functional_v1.json b/configs/contact_cycle/simulation_functional_v1.json new file mode 100644 index 0000000..b147438 --- /dev/null +++ b/configs/contact_cycle/simulation_functional_v1.json @@ -0,0 +1,36 @@ +{ + "format": "skillfactory.contact-cycle-simulation-functional.v1", + "status": "simulation_functional_development_tuned_not_independent", + "scope": "One nominal 9s cycle of floating-root FL leg with declared active auxiliary fixture. Not full quadruped balance, learned-policy certification, robustness or sim2real.", + "physics_dt_s": 0.002, + "phase_durations_s": [3.0, 1.0, 0.6, 1.4, 0.8, 2.2], + "nominal_scenario_id": 0, + "gates": { + "minimum_complete_cycles": 1, + "support_final_window_s": 0.5, + "reload_final_window_s": 0.2, + "contact_force_on_N": 2.0, + "support_reload_contact_fraction_min": 0.8, + "support_reload_load_fraction_min": 0.5, + "unload_final_window_s": 0.1, + "unload_remaining_support_fraction_max": 0.8, + "swing_gap_m_min": 0.01, + "swing_air_fraction_min": 0.8, + "swing_contiguous_air_s_min": 0.1, + "swing_xy_progress_fraction_min": 0.5, + "landing_position_error_m_max": 0.05, + "root_height_error_m_max": 0.1, + "normal_force_negative_tolerance_N": 0.5, + "root_tilt_rad_max": 0.6, + "ground_penetration_m_max": 0.002, + "auxiliary_vertical_N_max": 140.0, + "auxiliary_lever_arm_m_max": 0.06, + "guide_vertical_N_max": 0.000001, + "normalized_effort_velocity_max": 1.00001, + "joint_limit_excess_rad_max": 0.01 + }, + "deferred": ["random disturbances", "cross-seed success confidence", "120s endurance", "sensor and actuator domain randomization", "hardware calibration", "sim2real"], + "separation_of_claims": "Report sim_functional_passed separately for analytic_reference and learned_policy. Keep historical strict results unchanged; accepted_skill remains false in this development checker. No implicit training grant.", + "development_note": "Initial prototype required unload<=0.5W; retrospective reference traces showed about0.676W with actual flight/landing. Function-first revision explicitly requires at least20% measured reduction relative to preceding support, not near-zero load tracking. Prototype failure/config are retained; this is development tuning, never independent acceptance.", + "runtime_note": "This file is an offline functional checker contract, not a change to the frozen task or its termination code. Partial historical episodes cannot be extrapolated past their actual stop. Any future relaxed runtime requires a new explicit protocol." +} diff --git a/configs/contact_cycle/simulation_residual_rootxy_v2.json b/configs/contact_cycle/simulation_residual_rootxy_v2.json new file mode 100644 index 0000000..d152a70 --- /dev/null +++ b/configs/contact_cycle/simulation_residual_rootxy_v2.json @@ -0,0 +1,14 @@ +{ + "format": "skillfactory.contact-cycle-sim-rootxy.v2", + "task_id": "SkillFactory-Go2FL-ContactCycleSimRootXY-v2", + "features": "contact-cycle-sim-truth-residual-46-v1", + "base_config": "configs/contact_cycle/simulation_residual_v1.json", + "base_sha256": "a2352c197bd6e13a2a729792657b26da120fd2d295d00f205eca8154aed82628", + "overrides": { + "reward.position_scale_m": [ + 0.03, + 0.03, + 0.04 + ] + } +} diff --git a/configs/contact_cycle/simulation_residual_v1.json b/configs/contact_cycle/simulation_residual_v1.json new file mode 100644 index 0000000..9ec9ee9 --- /dev/null +++ b/configs/contact_cycle/simulation_residual_v1.json @@ -0,0 +1,10 @@ +{ + "format": "skillfactory.contact-cycle-sim-residual.v1", + "task_id": "SkillFactory-Go2FL-ContactCycleSim-v1", + "cycle_config": "configs/contact_cycle/go2_fl_v1.json", + "features": "contact-cycle-sim-truth-residual-46-v1", + "residual_limit_rad": 0.05, + "reference": {"ik_iterations": 3, "ik_damping": 0.00001, "ik_step_limit_rad": 0.08, "jacobian_epsilon_rad": 0.0005}, + "reward": {"position_scale_m": [0.05, 0.05, 0.04], "velocity_scale_m_s": 0.5, "cost_cap": 10.0, "position_weight": 1.0, "velocity_weight": 0.1, "residual_weight": 0.05}, + "scope": "Nominal-only simulation development; privileged IK/load-compensation reference plus bounded joint residual. No sensor/actuator DR, no stand-only mixture, no hardware or learned-skill claim. Existing physical failure termination stays enabled." +} diff --git a/configs/contact_cycle/simulation_stress_v1.json b/configs/contact_cycle/simulation_stress_v1.json new file mode 100644 index 0000000..f1f2fac --- /dev/null +++ b/configs/contact_cycle/simulation_stress_v1.json @@ -0,0 +1,38 @@ +{ + "format": "skillfactory.contact-cycle-scripted-residual-stress.v1", + "task": "SkillFactory-Go2FL-ContactCycleSim-v1", + "controller_kind": "analytic_reference_with_scripted_residual", + "num_envs": 8, + "controls_per_case": 900, + "control_dt_s": 0.01, + "physics_dt_s": 0.002, + "environment_seed": 90311, + "residual_limit_rad": 0.05, + "frequency_Hz": 0.75, + "joint_phase_rad": [0.0, 2.0943951023931953, 4.1887902047863905], + "environment_phase_increment_rad": 0.7853981633974483, + "gated_phases": [2, 3], + "gaussian_normalized_std": 0.1, + "functional_config": "configs/contact_cycle/simulation_functional_v1.json", + "nominal_only": true, + "external_disturbance": false, + "optimizer_steps": 0, + "omitted_cases": [], + "signal_definition": "sin(2*pi*0.75*k*0.01 + joint_phase[j] + env*pi/4); constant sign = +1 iff env bit j is set, else -1; Gaussian = tanh(0.1*z), independent CPU torch.randn float32 from declared seed, precomputed [900,8,3]. No finite-sample recentering. Gating uses command phase at control start, only lift/swing (2,3). Requests held for five physics ticks; requested rad divided by .05; absolute target projection may shrink effective rad.", + "episode_rule": "Only first episode per environment; retain through first control-boundary stop, separately record exact 500Hz first failure onset. Later autoresets are excluded, not stitched. Always execute 900 controls per case for remaining environments.", + "trace_rule": "All eight initial snapshots and summary rows; first four unsuccessful first-episode traces plus first complete successful representative (or first complete unsuccessful if needed); compact cloned storage; full first-episode reward/action histories for all eight.", + "cases": [ + {"id": "baseline_before", "kind": "zero", "amplitude_rad": 0.0}, + {"id": "sine_all_005", "kind": "sine", "amplitude_rad": 0.005}, + {"id": "sine_all_010", "kind": "sine", "amplitude_rad": 0.01}, + {"id": "sine_all_025", "kind": "sine", "amplitude_rad": 0.025}, + {"id": "sine_all_050", "kind": "sine", "amplitude_rad": 0.05}, + {"id": "constant_signs_025", "kind": "constant", "amplitude_rad": 0.025}, + {"id": "constant_signs_050", "kind": "constant", "amplitude_rad": 0.05}, + {"id": "gaussian_seed_90321", "kind": "gaussian", "seed": 90321}, + {"id": "gaussian_seed_90322", "kind": "gaussian", "seed": 90322}, + {"id": "sine_lift_swing_025", "kind": "gated_sine", "amplitude_rad": 0.025}, + {"id": "sine_lift_swing_050", "kind": "gated_sine", "amplitude_rad": 0.05}, + {"id": "baseline_after", "kind": "zero", "amplitude_rad": 0.0} + ] +} diff --git a/configs/contact_cycle/training_diagnostic_v1.json b/configs/contact_cycle/training_diagnostic_v1.json new file mode 100644 index 0000000..8e25322 --- /dev/null +++ b/configs/contact_cycle/training_diagnostic_v1.json @@ -0,0 +1,30 @@ +{ + "format": "skillfactory.contact-cycle-diagnostic-training.v1", + "contract": "go2-fl-contact-cycle-v1", + "proposal": "configs/contact_cycle/diagnostic_proposal_v1.json", + "task_config": "configs/contact_cycle/go2_fl_v1.json", + "seeds": [91101], + "validation_seed": 93101, + "num_envs": 128, + "horizon": 32, + "updates_per_seed": 512, + "checkpoint_interval": 256, + "validation_interval": 256, + "max_total_transitions": 2097152, + "max_total_wall_s": 1800, + "max_artifact_bytes": 1073741824, + "algorithm": { + "hidden": 128, + "initial_std": 0.15, + "learning_rate": 0.0003, + "gamma": 0.99, + "gae_lambda": 0.95, + "clip_ratio": 0.2, + "epochs": 4, + "minibatch_size": 1024, + "value_coefficient": 0.5, + "max_gradient_norm": 1.0, + "target_kl": 0.02, + "failure_penalty": 10.0 + } +} diff --git a/configs/contact_cycle/training_v1.json b/configs/contact_cycle/training_v1.json new file mode 100644 index 0000000..32b5178 --- /dev/null +++ b/configs/contact_cycle/training_v1.json @@ -0,0 +1,30 @@ +{ + "format": "skillfactory.contact-cycle-training.v1", + "contract": "go2-fl-contact-cycle-v1", + "proposal": "configs/contact_cycle/acceptance_v1.json", + "task_config": "configs/contact_cycle/go2_fl_v1.json", + "seeds": [91101, 91102, 91103], + "validation_seed": 93101, + "num_envs": 128, + "horizon": 32, + "updates_per_seed": 2048, + "checkpoint_interval": 256, + "validation_interval": 256, + "max_total_transitions": 25165824, + "max_total_wall_s": 10800, + "max_artifact_bytes": 4294967296, + "algorithm": { + "hidden": 128, + "initial_std": 0.15, + "learning_rate": 0.0003, + "gamma": 0.99, + "gae_lambda": 0.95, + "clip_ratio": 0.2, + "epochs": 4, + "minibatch_size": 1024, + "value_coefficient": 0.5, + "max_gradient_norm": 1.0, + "target_kl": 0.02, + "failure_penalty": 10.0 + } +} diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..0d27c99 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,33 @@ +# 文档入口 + +主线:Go2 FL仿真参考+残差,当前RootXY-v2已完成81次更新但中断,**没有合格新候选,accepted_skill=false**。本次文档/源码提交不是新的运行授权。 + +## 下一阶段必读 + +1. [当前进度](current_status.md):唯一当前状态摘要;物理结果、失配、部分训练与预算分开记录。 +2. [下一阶段](next_stage.md):监控资格验证→新绑定/新授权→有界试训→后续能力验证。 +3. [接口与CPU测试](contact_cycle_runtime.md):46D、动作/物理边界、实现入口和测试命令。 +4. [RootXY固定配方](contact_cycle_rootxy_v2_plan.md) / [受监督启动合同](contact_cycle_rootxy_v2_launch.md):预算、baseline/开发门槛、GPU共享及最终候选发布。 +5. [评估/溯源/证据/监控](contact_cycle_native_monitor_v3.md):合并P1/P2、初态修复、v5绑定、XZ与native扫描合同。 +6. [证据索引](evidence_index.md):冻结身份、外部报告、失败账本和不可选取检查点。 + +## 按需保留 + +- [统一软硬件规范v2](software_hardware_spec.md)及`schemas/`:静态描述/校验,通用运行adapter尚未实现。 +- [受载站立发布边界](loaded_stand_release_1_0_0.md):候选专属漂移豁免,禁止移用于新任务。 +- [原v1训练计划](contact_cycle_sim_training_plan.md)与[脚本残差压力测试计划](contact_cycle_sim_stress.md):**保留原路径/原字节**,冻结manifest/验证入口绑定它们。属于可复核输入,不是当前计划或授权;不为精简而破坏旧证据。 + +## 历史资料 + +过程性修复/第N次尝试及过期“下一步”已移出docs活动文档。归档仅用于溯源,其中的命令、预算、时钟与授权均不能直接执行。12份活动Markdown(含2份冻结输入)+2份Schema;不再按失败次数新增文档。 + +- 本次整理前全部docs及根README:[归档](../archives/documentation_before_rootxy_handoff.tar.gz)、[逐文件SHA256清单](../archives/documentation_rootxy_handoff.json)。 +- 更早阶段:[原归档](../archives/documentation_before_simplification_20261014.tar.gz)、[原清单](../archives/documentation_cleanup_20261014.json)。 + +只读查看,不解压覆盖当前工作区: + +```sh +tar -xOf archives/documentation_before_rootxy_handoff.tar.gz docs/contact_cycle_p2_pairing_repair.md +``` + +没有删除/移动原日志、权重、失败轨迹、授权、账本、快照或发布包。本机`logs/`与`/home/cen/sf-runs/`不随本次提交交付;缺少这些实际产物的clone不能独立复现冻结加载/物理资格。Git提交是源码版本,不是新的已验收技能发布。 diff --git a/docs/contact_cycle_native_monitor_v3.md b/docs/contact_cycle_native_monitor_v3.md new file mode 100644 index 0000000..e5523c7 --- /dev/null +++ b/docs/contact_cycle_native_monitor_v3.md @@ -0,0 +1,52 @@ +# 评估、溯源与存储监控合同 + +保留原文件名便于代码引用;评估native策略是v4,不是文件名中的v3。RootXY使用自己的v2策略/grant,不能混用评估授权。本文整合原namespace、codec、P1初始化和P2修复说明;历史过程已归档。 + +## P1/P2评估边界 + +- 入口`evaluate_cycle_sim_residual.py`、`prepare_cycle_sim_binding.py`、`audit_cycle_sim_evaluation.py`;协议/审计/绑定/预算在`legs/contact_cycle_sim/evaluation_*.py`。 +- P1固定世界锚点3×9秒A→B、B→A、A→B;周期间无pose reset、autoreset或测量足位置重锚。rootXY30mm/rootZ60mm等原安全/接触/接缝门槛不变。已运行双方都在首周期失败。 +- 扩展P2协议`configs/contact_cycle/bounded_diagnostic_p2_extended_v1.json`,profile `p2-wall10800-aggregate14400-v1`;20有序行,5条件×2seed×学习/参考,seed94501/94502。10800s槽/14400s aggregate,槽内120s清理,1GiB输出,91,000共享调用预留(90,000rollout+1,000init)。P1严格漂移界在P2仅诊断,不冒充连续验收。 +- 仅支持有限根p2-01/02,均已消费;p2-03拒绝。adoption只读绑定P1失败及前次P2预算/结果/grant/reservation,不退款、不重置历史时钟。新campaign必须先受审支持,不直接换目录绕过门槛。 +- 初态修复:保存首个构造时的origin/base_origin独立clone,后续行在唯一物理reset/IK刷新前恢复命令;不改物理pose、不加step、不影响P1 seam。每个reference行推理前比较前一learned行全部记录的initial_pair_state,缺失/不等致命停止。 +- 离线失配返回`invalid_initial_pair`、`both_functional_passed=false`、差值`not_evaluated`。旧P2数据不可被重写成有效对照。事件必须实际投递才能讨论受扰结果。 + +## 有限溯源绑定v5 + +`skillfactory.contact-cycle-evaluation-binding.v5`与`development-finite-runtime-provenance-v5`:冻结包严格verify_bundle;实际cfg/callable/class/source及有限Isaac、Kit、USD、Torch/NumPy/Warp和二进制身份校验。不是整个解释器/动态链接/JIT/供应链/PhysX状态的证明。 + +- REQUIRED_RUNTIME、ISAAC_PINS、RUNTIME_LAYOUT_PINS以源文件精确枚举为准,缺失/交换/未绑定来源失败;不递归搜第三方module元数据来扩大或豁免合同。 +- 使用实际加载pxr包装与native spec/loader,Tf所属omni.usd.libs与PhysX schema owner分别验证;不得猜相邻目录或虚构fileless hash。 +- 只接受安装中明确记录的IsaacLab `_isaac_sim`→isaacsim→isaacsim-6.0.1链接;项目路径canonical。旁观模块只标observational-not-verified。 +- `BindingMismatchError.binding_diagnostic`提供expected/actual hash、JSON Pointer差异、至多64项/32KiB及省略计数;不转储进程环境。诊断不是绕过。 +- 正常fast_shutdown前同对象cfg检查/持久化;监督器等待退出后检查文件和归属清理。准备提案不授权物理运行;非零退出、缺证据仍失败。 +- bootstrap用精确注册协议路径取stage,不能导入私有CPU规划别名。固定lazy PhysX函数/有限预导入修复不允许扩大身份白名单。 + +## 无损证据容器 + +schema仍`skillfactory.contact-cycle-evaluation-evidence.v1`,容器`jsonl-xz-finalized-chunks.v2`,文件`trace.v2.jsonl.xz`;兼容读取旧gzip,不改字段/顺序/500Hz采样。 + +- 独立finalized XZ流每块≤64记录且≤8MiB raw;每记录≤1MiB,preset0/256KiB dictionary。重要initial/seam/step-result/故障/terminal同步flush。 +- 每条/每次实际写入都保留预算检查;pending预留`3*raw+65536`,不得用缓存大小跳过原检查。最终trace fsync/metrics/directory fsync/close后才可确认停止。 +- flush不等于fsync;kill可丢未提交块或截断写入。可读前缀不是完整成功。 +- reader逐块限制64KiB输入/解码、1MiB记录、8MiB/member、XZ64记录/member与8MiB decoder memory;限制总解码、记录数及deadline。完整读至trailer/EOF并验证terminal,拒绝损坏尾巴和跨XZ流记录。 +- CPU保存行字节/审计一致性通过;实际p2-02全部20条也已离线重审。压缩性能不是物理通过证据。 + +## Native监控范围和不可证明项 + +精确列表由runner helpers及NATIVE_GAPS生成并绑定policy,不手写近似:owned `C/native`、Isaac安装`data`、`kit/data`、`kit/logs`和共享`kit/cache`;显式用户cache为`.cache/ov/shaders`、`texturecache`、`Kit/107.3`。C另扫描且排除已计native。RootXY重定向由自身`redirects(root,mode)`生成。 + +只对C、`/tmp`、HOME、安装根做free-space/mount检查,不递归浏览其无关子树。不得恢复对浏览器缓存、泛HOME/tmp/.nv等的扫描。portable token目的地是源支持候选,不能称全部运行时解析已证实。 + +- descriptor-relative遍历不跟随symlink;owned/strict拒绝权限错误。只有稳定非本用户所有的shared目录、实际EACCES/EPERM及元数据/access复查均满足,才可记录覆盖缺口。 +- 目录/祖先替换、未知I/O、mount变化、持续扫描故障致命;native failure保留partial事实,总量未知为null。异步扫描不能阻塞唯一截止时钟/清理线程。 +- 原生写入是采样monitor-and-stop,**native_hard_cap=false、coverage_complete=false**。未列writer、deleted-open文件、隐藏mount、native分配及无法访问范围不能伪造零增长。 +- 逻辑字节、native正增长、文件系统free floor分别报告,不能重复加总。新策略不得延长运行预算或豁免安全门槛。 + +## 已修复的原子发布竞态(下一阶段必须验证) + +1. `evaluation_budget.artifact_size`对列出后消失的文件最多4次完整重扫,单lstat取mode/size;不返回部分和、不吞权限错误。RootXY继承ledger整体guard仅对FileNotFound做同样有限重试,保留每次时钟/预算检查。 +2. native `os.access=False`没有errno,不能直接解释成权限拒绝。对已stat的regular文件,以同目录fd复查:仅确认ENOENT才记录disappeared并保留末次size作保守计数;仍存在的不可读文件/其他错误继续失败,不变成permission gap。 +3. rxy-04在第2项修复前停止;其静态文件重哈希已在修复前完成。最后修复只有CPU280passed/1skip,没有重新物理运行或获得新授权。 + +测试重点:`test_contact_cycle_sim_native_monitor.py`、`evaluation_runner.py`、`evaluation_binding.py`、`preparation_supervisor.py`、`evidence_codec.py`、`worker_bootstrap.py`、`p2_extended_profile.py`(均以`test_contact_cycle_sim_`为前缀)。 diff --git a/docs/contact_cycle_rootxy_v2_launch.md b/docs/contact_cycle_rootxy_v2_launch.md new file mode 100644 index 0000000..28e5c51 --- /dev/null +++ b/docs/contact_cycle_rootxy_v2_launch.md @@ -0,0 +1,57 @@ +# RootXY-v2:受监督启动与终态合同 + +入口`scripts/train_cycle_sim_rootxy_v2.py`;stdlib监督器`scripts/rootxy_launch_v2.py`。已有真实配置准备和部分PPO运行,但**最新native修复尚未端到端资格验证**;历史绑定/授权不能用于当前源码。[当前进度](current_status.md)不是新运行许可。 + +## 准备与授权顺序 + +1. CPU `--check-only`只读验证,`--plan`输出preregistration JSON;不推理、不构造环境、不签发许可。 +2. 父级审核manifest、来源/冻结包、短路径新输出根、GPU身份、存储策略及明确预算;生成独立prepare grant。 +3. PREP总600s,480s工作+120s清理:安装零物理调用钩子,构造真实128env配置而非环境,有限固定Isaac/PhysX预导入,精确规范化,生成绑定提案。无reset/inference/optimizer。 +4. 只有监督器独立等待正常退出、清理并重哈希之后的`preparation.json`有效;worker provisional不能直接授权RUN。 +5. 父级另作P2证据/试训可行性决定并签发RUN grant。P2失配不改称通过;仅可明确批准名义单周期pilot,保留功能baseline硬门槛。 +6. RUN限额按[配方](contact_cycle_rootxy_v2_plan.md);一次、无恢复/重试。任何源码/配置/绑定变更须新PREP,不复用旧claim或grant。 + +模板仅说明参数,**不是可直接执行的新许可**。监督器使用绝对入口和`/usr/bin/python3.12`,不能直接启动Kit worker或替换为通用`supervise_sim.py`: + +```sh +/usr/bin/python3.12 /ABS/PROJECT/scripts/train_cycle_sim_rootxy_v2.py --prepare \ + --output /NEW/ROOT --manifest /PARENT/preregistration.json \ + --trusted-manifest-sha256 EXACT_SHA256 \ + --storage-policy /PARENT/prepare-policy.json --trusted-storage-policy-sha256 EXACT_SHA256 \ + --authorization /PARENT/prepare-grant.json --trusted-authorization-sha256 EXACT_SHA256 + +/usr/bin/python3.12 /ABS/PROJECT/scripts/train_cycle_sim_rootxy_v2.py --run \ + --output /SAME/PREPARED/ROOT --manifest /PARENT/preregistration.json \ + --trusted-manifest-sha256 EXACT_SHA256 \ + --storage-policy /PARENT/run-policy.json --trusted-storage-policy-sha256 EXACT_SHA256 \ + --authorization /PARENT/run-grant.json --trusted-authorization-sha256 EXACT_SHA256 \ + --p2-decision /PARENT/pilot-feasibility-decision.json +``` + +## 精确身份与时钟 + +- profile=`rootxy-v2-7200s-1GiB-73460calls`,grant=`skillfactory.rootxy-parent-grant.v2`。精确字段校验以`admission`/grant校验代码为准;包括root、mode、manifest/policy/launch identity、boot、expiry、parent_review、GPU,以及RUN必须有的preparation/binding/P2 decision哈希。PREP后三项为null。 +- 入场grant有效窗口≤3600s,同boot;这是入场期限,**不是**运行总时长。新策略`skillfactory.rootxy-native-policy.v2`寿命7200..10800s,须覆盖运行及规定120s额外策略余量和grant入场期限。 +- 外部trusted SHA256针对精确文件字节,不是重新序列化JSON;`launch_identity`按适配器canonical JSON规则取hash。不得自读自批、猜测digest或让worker自行批准。 +- `skillfactory.rootxy-p2-decision.v2`包含task/root/manifest/preparation/binding、approved、parent_review和非空`p2_evidence`路径→哈希。对失败证据的可行性批准不是P2物理通过。 +- 保持短canonical输出路径,避免长TMPDIR/NVRTC问题;不使用symlink冒充根或复用旧输出。 + +## GPU桌面共享 + +用户已批准保留桌面,而不是GPU独占或硬件分区。每次新grant必须重新记录实际身份,不照抄旧PID: + +- admission无计算客户端,graphics恰好匹配有限父级基线(排序唯一、最多64个);NVML库路径/哈希和GPU0 UUID固定,CUDA可见性由监督器强制。 +- desktop身份包含PID、birth、UID、argv哈希、exe文件路径/元数据/哈希;运行中查birth/文件身份,入场/终态也重哈希。 +- root Xorg的实际`/proc/PID/exe`不可读时,仅UID0、精确`/usr/lib/xorg/Xorg` argv0与comm Xorg允许显式公共元数据范围;**actual-executable-unverified**,不声称真实exe已验证。 +- 新图形、外来计算、desktop取得计算身份、身份变化均停止。批准desktop可退出,不属于清理归属,不得终止它或其他外来进程。最终只允许剩余批准desktop,无任务/外来GPU工作。 + +## 运行绑定、安全与终态 + +- 唯一spawner/subreaper通过认证继承FD交接;进程清理使用birth identity/pidfd,独立wait,失败也保持有界清理。禁止手动直接运行worker、fake模块或放宽导入白名单。 +- 配置只在构造前按精确类/计数规范化:Evaluation=1环境、RootXY=128环境。有限依赖预导入不代替真实同对象绑定。 +- 检查构造前、实际环境构造后且动作前、训练源码检查点、env.close后/app.close前。Kit快速关闭前持久化证据;退出后监督器只重哈希静态文件,不能宣称仍验证原cfg对象。 +- 真实SimulationContext.step前后记录attempted/completed及初始化/rollout分类;500Hz scene-update安全检查包括reset,缺失/重复覆盖在下一物理步或工作边界拒绝。构造期尚无task audit的调用另计。 +- 1GiB含PREP/快照/native/tmp/stdout和声明的外部正增长;保留1MiB stdout和16MiB发布/清理余量。路径、mount、free floor≥2GiB、interval≤1s、覆盖缺口必须精确声明。不是native硬quota。 +- 仅完整正常退出、归属/GPU清理、最终身份/账本/物理安全、两baseline+四开发功能、XY资格都通过后,监督器最后提交candidate lock。缺少worker最终报告、仅静态文件PASS、只有checkpoint均不够。 + +扫描实现、XZ完整性与评估初态规则见[评估和监控](contact_cycle_native_monitor_v3.md)。CPU用例见`test_contact_cycle_sim_rootxy_launch_v2.py`、`test_contact_cycle_sim_rootxy_v2.py`及native/evaluation runner测试;独立复审缺口必须如实披露。 diff --git a/docs/contact_cycle_rootxy_v2_plan.md b/docs/contact_cycle_rootxy_v2_plan.md new file mode 100644 index 0000000..d5271da --- /dev/null +++ b/docs/contact_cycle_rootxy_v2_plan.md @@ -0,0 +1,52 @@ +# RootXY-v2:固定训练配方与候选判据 + +这是当前实现合同,不是新运行授权。两次RUN均已停止;第二次完成81更新但没有合格候选,见[当前进度](current_status.md)。改动本文会改变新manifest身份;旧快照/失败证据不修改。 + +## 范围与唯一任务变化 + +- 独立任务`SkillFactory-Go2FL-ContactCycleSimRootXY-v2`,不是旧v1或32D策略的别名。 +- 沿用46D特征、固定归一化、参考/载荷补偿、9秒独立回合、原辅助夹具和物理失败规则;所有阶段残差±0.05rad,关节投影后有效权限可能更小。 +- 唯一奖励变化:position scales由`[.05,.05,.04]`改为`[.03,.03,.04]m`。权重1/.1/.05、XYZ共享cap10、velocity scale .5和其余成本不变。 +- 不增加30mm训练termination;普通原任务失败仍可作为PPO学习信号。身份、有限性、关节/effort/guide安全、计数/诊断、时间/存储/归属异常致命停止。 +- 仍是名义单周期漂移减少试验,不学习连续周期或外扰恢复。 + +## 初始化与对照 + +冻结来源见[证据索引](evidence_index.md)。仅导入u256的actor、log_std、精确normalization;critic/Adam/Python/NumPy/Torch RNG全新,不继承旧budget、update、best或optimizer状态。 + +- train92511,development93511;95511是拟用holdout,未在本轮调用,未来须再次检查使用记录。 +- CPU在绑定的45×46观察上检查warmstart动作/mean/std精确相等,不冒充第三次物理baseline或完整PhysX状态相等。 +- 物理baseline:analytic_zero与frozen_u256各128环境、同dev seed,检查初始观察/命令配对。两者都须原功能128/128,才允许优化。 +- baseline的30mm超限只作诊断,不改写功能结果;开发候选的30mm判据则是硬门槛。 + +## 预算、开发验证与选模 + +| 项目 | 固定限制 | +|---|---| +| 优化 | 128环境×32控制步×256更新=1,048,576环境转移;每rollout先预扣,不退还中断预留 | +| 验证预留 | 128×900×7=806,400环境转移;实际计划2基线+4开发,共6次,未用预留不退 | +| 共享物理调用 | rollout预留72,460+初始化/reset上限1,000=73,460;实际前/后钩子计数,不用转移数伪造 | +| RUN时间 | 同boot CLOCK_BOOTTIME 7200s,从监督器入场起含启动/输出/关闭;7080s工作截止、120s清理 | +| 存储 | 1GiB新owned+列明外部native正增长;monitor-and-stop,不是硬quota;不覆盖旧根 | +| 启动 | 一次,无重试、恢复、延长或退款;RTX5080 cuda:0 | + +u64/128/192/256各完成128条首次9秒开发回合,原功能128/128,否则停止;不以autoreset后回合补齐。 + +候选资格:全部128条完整4500物理tick、每tick rootXY≤0.03m、无违例。超限锁存至回合结束,恢复不能抹除;部分前缀RMS不得参选。合格者依次按全回合平均XY RMS、原tracking综合代价、最早update排序。无合格者可形成“完整试验但无候选”,不选最好的失败权重。 + +## 实现与输出 + +路径前缀`source/SkillFactory/SkillFactory/`: + +| 文件 | 职责 | +|---|---| +| `legs/contact_cycle_sim/rootxy_training_v2.py` | 配方/manifest、weights-only导入、独立debit ledger、eval-only checkpoint | +| `rootxy_runner_v2.py`(同目录) | baseline/rollout/PPO/开发顺序,强制RuntimeHooks、失败后撤销临时候选 | +| `rootxy_diagnostics_v2.py` / `rootxy_selection_v2.py` | 逐tick XY/饱和/权限统计、无副作用资格与排序 | +| `rootxy_binding_v2.py` / `rootxy_runtime_v2.py` | 真实配置/来源绑定、物理计数、安全覆盖 | +| `tasks/manager_based/contact_cycle_sim/rootxy_cfg_v2.py` | additive配置,原类/计数精确合同,不改v1 | +| `rootxy_recording_v2.py`(同目录) | 4500×128紧凑功能列、全部验证行、每次最多4条代表轨迹 | + +`result.json`与`run-worker-result.json`仅临时结果;任意后续异常均使保留checkpoint不可选取。只有独立监督器在正常退出、清理、最终身份/计数/安全/资格全部通过后可写`locked_candidate.json`。checkpoint没有恢复训练所需optimizer状态。 + +真实PREP/RUN授权、GPU共享与证据关闭次序见[启动契约](contact_cycle_rootxy_v2_launch.md);CPU测试方法见[运行接口](contact_cycle_runtime.md)。所有P1/P2/holdout下游运行另行授权。 diff --git a/docs/contact_cycle_runtime.md b/docs/contact_cycle_runtime.md new file mode 100644 index 0000000..a74f66d --- /dev/null +++ b/docs/contact_cycle_runtime.md @@ -0,0 +1,82 @@ +# 接口与运行约束:Go2 FL参考+残差 + +## 当前契约 + +- 任务:`SkillFactory-Go2FL-ContactCycleSim-v1`。 +- 特征:`contact-cycle-sim-truth-residual-46-v1`;46D、float32、有限值,不能载入旧32D策略。 +- 名义9秒周期:承重3s、卸载1s、抬腿0.6s、摆动1.4s、落地0.8s、重新承重2.2s。 +- 500Hz物理/失败检查,100Hz控制。默认位移`[0.025,0.02,0]`m,净空0.05m+0.01m拱高。 +- 浮动根、3kg合成载荷/传感载体、真实接触/六维测力;当前有效系统质量约5.388kg,W指有效系统重量,不仅是3kg载荷重量。保留有界横向/转动辅助,竖直guide力必须为0。不是固定根,也不是无辅助平衡。 +- 当前使用仿真真值,名义任务不接受旧课程采样器、stand-only、扰动或传感/执行器随机化的静默混入。 + +## 观察与动作 + +| 观察索引 | 含义 | +|---|---| +| 0–31 | 原传感/命令32D,包括上一步完整归一化关节动作 | +| 32–34 | root相对命令挂载点的位置误差,世界系m | +| 35–37 | root相对命令的线速度误差,世界系m/s | +| 38 / 39 | 真实地面法向力Fn(N)/真实足端gap(m) | +| 40–42 | 下一控制步的归一化参考动作 | +| 43–45 | 上一控制步夹紧后的请求残差,不是投影后的实际残差 | + +参考由足端目标、阻尼IK和Jacobian载荷补偿生成,仅在reset或控制边界推进,不能因读取观察而推进。3维残差请求先夹到±1,每关节对应±0.05rad;合成目标再投影到原关节范围,投影只能缩小实际残差。 + +必须分别记录`reference_used_normalized`、`reference_next_normalized`、`residual_request_normalized`、`effective_residual_rad`和实际关节目标。PPO概率针对采样残差latent,不针对合成后的绝对关节动作。随机策略与确定性均值策略要明确区分。 + +checkpoint为`skillfactory.contact-cycle-sim-residual-ppo-checkpoint.v1`。原参考/零初始化actor成功不算学习;本轮已训练u256身份见[当前进度](current_status.md)。原task objective之外保留root位置/速度和残差成本,具体尺度以绑定配置为准,不能为了新实验原地改旧配置。 + +## 不可绕过的门禁 + +- 接触获取:200ms内达到至少0.5W并持续10ms、gap≤0.5mm;失败必须终止。 +- 离地检测:on=1mm、off=0.5mm、dwell=10ms;保持真实起始阶段和首次失败来源。 +- timeout bootstrap用reset前的终端观察;真实失败bootstrap为0;禁止跨reset串接回合。 +- 有限性、关节/力矩/速度约束、辅助力边界及物理失败检查保持启用。非有限动作必须在污染动作历史前拒绝。 +- 使用SI单位;项目对外四元数约定xyzw,与后端不同约定要显式转换。力/力矩坐标系与作用点必须明确,统一经唯一COM外力合成入口,不能重复叠加r×F或把辅助力冒充足端接触力。 +- 功能门槛是开发阶段形成的判据,不是独立验收;不得删失败、放宽门槛或用autoreset后的轨迹补成功。 + +## 实现入口 + +| 用途 | 路径(仓库根目录相对路径) | +|---|---| +| 参考、模型、PPO、预算协议 | `source/SkillFactory/SkillFactory/legs/contact_cycle_sim/` | +| 仿真环境、动作、记录 | `source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/` | +| 任务/功能/训练配置 | `configs/contact_cycle/simulation_residual_v1.json`、`simulation_functional_v1.json`、`sim_training_v1.json` | +| 训练器(不是回放器) | `scripts/train_cycle_sim_residual.py` | +| 非优化接口/物理探针 | `scripts/verify_cycle_sim_residual.py`、`verify_cycle_sim_learning_runtime.py` | +| 功能审计 / Kit监督 | `scripts/audit_contact_cycle_functional.py` / `scripts/supervise_sim.py` | + +旧`train_contact_cycle.py`仍属于32D路径;不要用于46D候选。已实现冻结回放`scripts/replay_cycle_sim_residual.py`,P1/P2准备与评估为`prepare_cycle_sim_binding.py`/`evaluate_cycle_sim_residual.py`。RootXY独立入口`train_cycle_sim_rootxy_v2.py`,同46D观察但独立task/checkpoint身份;仅XY奖励尺度变化,详见[配方](contact_cycle_rootxy_v2_plan.md)。新任务不得借用旧grant或用不可选取u64替换冻结u256。 + +## 安全运行 + +仓库根目录下执行。只读复核已有训练,不启动Kit: + +```sh +python3 logs/contact_cycle/sim_residual_training_v1/verify_delivery.py +``` + +本机CPU测试解释器:`/home/cen/miniconda3/envs/linker_sim_isaac/bin/python`(Python3.11/Torch2.7);不要用缺Torch的系统Python跑学习测试。用以下文件映射跳过顶层Kit任务注册,**仅供CPU测试,不可用于运行绑定**: + +```sh +CUDA_VISIBLE_DEVICES='' PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 \ + /home/cen/miniconda3/envs/linker_sim_isaac/bin/python - <<'PY' +import importlib.util, sys +from pathlib import Path +root = Path.cwd() +spec = importlib.util.spec_from_file_location( + 'SkillFactory', root / 'source/SkillFactory/SkillFactory/__init__.py', + submodule_search_locations=[str(root / 'source/SkillFactory/SkillFactory')]) +sys.modules['SkillFactory'] = importlib.util.module_from_spec(spec) +sys.path.insert(0, str(root / 'scripts')) +import pytest +targets = [str(p) for p in sorted((root / 'tests').glob('test_contact_cycle*.py'))] +raise SystemExit(pytest.main(['-q', *targets])) +PY +``` + +上面只跑接触周期主线。全仓旧发布/ABI测试要求Python3.12+Torch2.10.0;任务注册另需pxr/Isaac运行依赖,不应在此3.11/Torch2.7环境期待全套通过,也不能放宽冻结包版本检查来凑通过。不少CPU回归读取本机冻结快照/安装文件;缺少时失败或显式skip不等于新机器获得资格。Isaac运行使用固定`/home/cen/IsaacLab`、`/home/cen/isaacsim-6.0.1`及RTX5080,不把CPU测试版本当实际Kit依赖版本。 + +默认测试中跳过的PhysX包装不算已执行物理验证。不要运行根目录`pytest`,它可能收集`logs/`中的归档测试;不要在根目录执行`uv sync`或修改外部Isaac Lab/Unitree资源。 + +当前RootXY和P1/P2必须使用各自认证受监督入口,不得用通用`supervise_sim.py`替代授权/绑定/归属检查。监督器使用`/usr/bin/python3.12`,真实worker在认证FD交接后加载AppLauncher。不得覆盖原证据、杀无关进程或把工具attention当作卡死。采用同一boot绝对`CLOCK_BOOTTIME`截止、rollout预扣、成功后提交;中断不退款,旧grant/目录不复用。配置准备不等于批准运行。详情见[启动合同](contact_cycle_rootxy_v2_launch.md)和[评估监控](contact_cycle_native_monitor_v3.md)。 diff --git a/docs/contact_cycle_sim_stress.md b/docs/contact_cycle_sim_stress.md new file mode 100644 index 0000000..1bfcc23 --- /dev/null +++ b/docs/contact_cycle_sim_stress.md @@ -0,0 +1,85 @@ +# Nominal scripted-residual operating-envelope test + +This additive non-learning harness runs `SkillFactory-Go2FL-ContactCycleSim-v1` +with the existing analytic reference **plus scripted residual**, never a policy model. +It changes no controller, reward, termination, functional threshold or asset. + +## Preregistered development matrix + +`configs/contact_cycle/simulation_stress_v1.json` declares all 12 ordered cases: +zero baseline before/after; all-phase sine amplitudes 0.005/0.01/0.025/0.05 rad; +constant signed 8-combination residuals 0.025/0.05 rad; tanh-Gaussian normalized +latent std 0.1, development seeds 90321/90322; lift/swing-only sine 0.025/0.05 rad. +Eight environments run 900 controls each, 100Hz control / 500Hz physics, nominal +scenario only, no external disturbance or actuator delay/noise. The environment +seed is 90311. There are no omitted cases and no parameter search. + +Sine frequency is 0.75Hz; joint offsets are 0, 2π/3, 4π/3; environment offset is +eπ/4. Time is control index ×0.01s. Constant signs are the three bits of the +environment index (unset=-1, set=+1). Gaussian requests are independent CPU +float32 `tanh(0.1*randn([900,8,3]))`, not finite-sample mean-centered. Thus their +population mean is zero and typical actual requested std is about 0.005rad. +Gating uses the command phase **at control start** and holds the request across +five physics ticks. Only phases 2/3 (lift/swing) enable gated requests. All-phase +and gated tests have different domains: gated success cannot establish safe +all-phase exploration. Amplitudes are requested joint radians; the unchanged +absolute-target projection may shrink effective residuals near bounds. + +## Evidence and honest failure accounting + +Only each environment's FIRST episode is evaluated by the frozen +`audit_contact_cycle_functional.evaluate` and `simulation_functional_v1.json`. +Failed environments can autoreset while remaining environments finish, but +later episodes are excluded from all functional rows and saved histories. +Physics-rate failure onset (tick, clock, phase, overlapping causes and state) +is distinct from the control-boundary termination/reset clock. All six phase +sample counts and every functional gate actually evaluated are retained; the +frozen checker returns early on incomplete cycles, so missing later gates are +not passes. Original physical failures remain enabled. + +Each case saves eight initial snapshots, eight summary rows, first-episode +reward/action histories, the first four unsuccessful traces, and a representative +complete trace when available. Tensor slices are cloned before serialization, +not aliased views of a full matrix storage. Trace includes requested/effective +residuals, used/next reference, actual target, real contact and fixture/external +wrenches, failure flags and actor metadata (unavailable, since no actor). +PhysicalSafetyAudit runs every physics tick, including excluded later episodes; +its unchanged physical safety guard can abort the matrix. + +Top-level `harness_verified`, `baseline_passed`, stress case counts and stress +episode counts are separate. `passed` is only the supervisor-compatible harness +and baseline gate, explicitly **not** an overall stress functional pass. +Stress failures are results, never a reason to weaken termination or retry for +favorable behavior. No learned, robustness, recovery, sim2real, hardware, +release, or publication claim follows from this nominal development matrix. + +## Bounded execution + +The fresh maximum-two-hour contract at +`logs/contact_cycle/autonomous_sim_resume_v1/window.json` has SHA256 +`511d3f98a2c9ec7b78b1cb7f8f72e28ef06de6dcf96ecf2493ac7399d1eefbc8`. +It is NOT a renewed 12-hour grant. Same-boot CLOCK_BOOTTIME deadline and 120s +cleanup margin are binding; wall UTC is diagnostic. The launcher fails closed +on boot change, expiry, process conflict or artifact cap (1GiB total window, +conservatively including prior window bookkeeping, with 8MiB log/report reserve). +It serializes owned launches with a lock, checks the deadline before Kit and +every control, checks disk at least once per second and before evidence writes, +and limits the supervisor timeout to min(2400s, remaining−130s). + +```bash +python3 scripts/verify_cycle_sim_stress.py --supervise \ + --output logs/contact_cycle/autonomous_sim_resume_v1/phase01_nominal_residual/attempt01 +``` + +The stdlib outer launcher removes CONDA_PREFIX/VIRTUAL_ENV and sets +PYTHONPATH to `$PWD/source/SkillFactory`. The sole Kit invocation is via +`scripts/supervise_sim.py` and `/home/cen/IsaacLab/isaaclab.sh -p`. +It writes an immutable preregistration containing the exact plan and source, +asset and protected input hashes **before launch**, checks hashes each case +and after shutdown, and retains the supervisor report. All attempt paths must +be fresh; failed attempts are preserved. Stop when this matrix is complete. + +Results are authoritative in the fresh attempt directory: per-case `result.json`, +`matrix_progress.json`, final `result.json`, `supervisor.json`, and +`integrity_after.json`. Default CPU tests skip Kit and do not count as physics +evidence; the real matrix itself supplies PhysX evidence. diff --git a/docs/contact_cycle_sim_training_plan.md b/docs/contact_cycle_sim_training_plan.md new file mode 100644 index 0000000..f00e303 --- /dev/null +++ b/docs/contact_cycle_sim_training_plan.md @@ -0,0 +1,41 @@ +# 46D残差PPO首轮试训计划(实施/运行前计划) + +用户新授权:“开始训练,然后根据结果再修改内容”。此前所有训练额度已消费,本轮是新协议,不复用旧grant或账本。连续多周期测试暂缓。 + +## 固定试训范围 + +- 独立任务 `SkillFactory-Go2FL-ContactCycleSim-v1`;feature `contact-cycle-sim-truth-residual-46-v1`。 +- 全新seed **91501**,128环境×32步×256更新=**1,048,576训练转移**。 +- 训练及验证绝对CLOCK_BOOTTIME窗口 **3600s**,含启动/停机时间,同boot有效;监督器留清理余量。 +- 全部产物 **≤1GiB**;一次实际训练launch,无自动重试、恢复、warm start或追加优化。 +- 现有参考控制、±0.05rad全阶段残差、46D观察、奖励和功能门槛保持不变。只有名义周期,没有stand-only、外扰、DR或holdout。 +- 新网络默认2×128 Tanh、初始latent std=.1、actor末层为零。零初始化能跑通是参考能力,不是学习成果。 +- PPO保持现有generic math:Adam lr3e-4/eps1e-5,gamma=.99,GAE=.95,clip=.2,4epochs,minibatch1024,value coefficient=.5,maxgrad1,targetKL=.02,failure penalty10;reward除control dt。 + +## 接线和前置验证(不做机器人优化) + +新增46D rollout/bootstrap/checkpoint层;不改旧32D runner或模型。 + +1. `contact_cycle_sim/ppo.py`:46D feature采集,true failure bootstrap0,timeout仅使用终止前46D观察;GAE跨任何reset截断;收集phase/causes诊断。 +2. 新训练协议与runner:准备不等于批准;新的manifest绑定任务/配置/源码/资产/指标代码,使用独立checkpoint标识。 +3. 复用不变的通用PPO数学与BOOTTIME预算机制,训练rollout前预扣、checkpoint后提交、不退还中断预留。CLI本轮不开放resume。 +4. CPU合成测试和短实物理API检查(不更新参数)覆盖真实timeout/true-failure、终止观察、log-prob重算、参数未变、动作边界、缺诊断拒绝优化。 +5. 独立审查、前置门禁通过后,由父代理准备快照并将本次用户授权绑定到最终manifest;此前不得创建实际训练grant或调用机器人optimizer。 + +## 开发验证与候选固定 + +使用相同128环境验证,seed **93501**;并非独立鲁棒性样本。 + +- u0(未训练基线)、u64/u128/u192/u256各执行完整9秒确定性评估。 +- u0必须128/128满足既有仿真功能判据,否则训练在第一条优化rollout前停止。u0明确未训练且不得成为训练候选。 +- 每个环境只评估第一次回合,不能拼接失败后的自动重置;保存所有行、前4条失败(若无失败则前4条成功)轨迹/初始状态和参数/参考/实际残差统计。 +- 用旧功能审计器检查网络输出轨迹时,其`learned_policy`参数仅代表actor metadata要求;外层结果必须将u0标为未训练零残差网络,训练后标为解析参考+学习残差。 +- 训练候选只在u64/u128/u192/u256中选择:先最小失败比例,再最小以下所有环境均值成本,精确并列取较早更新。 + - 失败行固定16;成功行 `min(height_RMS/.06,2)+min(swing_RMS/.025,2)+min(landing_error/.05,2)`。 +- 报告每个检查点与u0的完整性、位置误差、高度误差、残差幅度及回报,不以回报上涨替代功能。 +- 记录actor、critic、log_std相对初始参数的变化;明确功能是否本已由参考控制提供、学习是否实际改善或退化。 +- `accepted_skill=false`;不创建发布、独立验收或硬件/sim2real结论。 + +## 根据结果修改 + +先完成上述冻结试训,再依据真实失败/退化证据决定是否需要阶段限幅、奖励或优化配置修改;修改另立版本、保留本次结果。**此授权不自动包含修改后的第二轮优化**。 diff --git a/docs/current_status.md b/docs/current_status.md new file mode 100644 index 0000000..9fcbe56 --- /dev/null +++ b/docs/current_status.md @@ -0,0 +1,55 @@ +# 当前进度:RootXY-v2完成81次PPO更新,尚无合格新候选 + +当前对象是Go2 FL仿真单腿,浮动根、有界XY/转动辅助、竖直辅助为0;使用仿真真值。**accepted_skill=false**。单周期功能通过不等于连续稳定、扰动鲁棒、整机控制或实机能力。 + +## 已确认的里程碑 + +| 阶段 | 已观察结果 | 不能据此宣称 | +|---|---|---| +| 原46D残差PPO | 256更新、1,048,576训练转移;u0/64/128/192/256各128/128名义9秒功能通过,按预设规则冻结u256 | 从零学会任务、独立验收;零残差参考原本可完成周期 | +| P0冻结回放 | u256首次9秒通过,900控制/4500采样tick;高度/摆动/落点19.385/7.492/9.967mm | 初始化额外tick已完整计数、连续周期通过 | +| P1连续周期 | 学习/参考分别在3.658/3.540s越过30mm水平根漂移门槛;均在首周期卸载阶段停止 | 已完成任何连续周期或接缝验证 | +| P2扰动矩阵 | p2-02退出0、20行执行、75,863实际共享物理调用、清理通过;完整XZ离线审计复现 | 有效对照:全部10组命令初态失配,原始8/10对5/10不能说明策略优势 | +| RootXY第一次RUN | rxy-03参考基线中因文件扫描竞态停止,0更新 | 已完成基线或最终物理计数 | +| RootXY第二次RUN | rxy-04实际完成81更新,u64开发验证128/128功能/安全通过;约43.80分钟后因另一扫描竞态停止 | 完整训练成功或存在合格候选 | + +P2跨行状态捕获前一行末态足位置,导致anchors/q_ik不同。已恢复首个构造命令并在参考推理前检查配对;失配时致命停止且禁止发布比较差值。**修复只有CPU资格,尚无修复后的P2物理验证**。两次P2运行授权均耗尽,现代码不支持p2-03自动重试。 + +## 最近训练结果:rxy-04 + +任务`SkillFactory-Go2FL-ContactCycleSimRootXY-v2`,128环境、train92511/dev93511。正式优化前参考与冻结u256均通过原128/128功能基线。u64结果如下,单位mm,统计为128环境指标的均值: + +| 验证对象 | 功能通过 | 全程XY>30mm | XY RMS | 每环境最大XY的均值 | +|---|---:|---:|---:|---:| +| 解析零残差 | 128/128 | 126/128 | 25.423 | 79.663 | +| 冻结u256 | 128/128 | 126/128 | 16.250 | 68.146 | +| 新u64 | 128/128 | 126/128 | 15.796 | 67.196 | + +u64平均XY RMS约改善2.8%,但超限数量未减少,`candidate_key(rows,64)=None`。这只是已查看的开发seed结果,不是独立holdout或显著性结论。u64仅留存诊断,不可发布、不可恢复训练。 + +- 81次完成更新对应331,776环境转移;第82次rollout已预扣但未提交,优化账本335,872转移,**不退还**。 +- 最近故障:native扫描先stat临时文件,原子重命名后`os.access=False`被误判为权限失败;不是物理失败。监督器停止worker,清理正常。 +- 最后物理快照26,468完成调用/26,467安全样本,写于scene-update之前;不是最终计数,也不能据此认定缺安全样本。worker最终报告缺失,最终实际总计未知。 +- 父级确认4个owned PID消失、无计算/任务图形残留、原7个桌面图形客户端存活;修改源码前623个绑定文件重哈希通过,仅证明静态文件,不替代缺失的worker最终同对象配置检查。 +- 输出167,275,915字节,监控native正增长85,068,025字节;监控不是硬配额。 + +## 修复状态与未完成项 + +1. 配置准备现使用精确RootXY128环境规范化和有限固定PhysX预导入;rxy-03/04配置准备均成功,早期rxy-01/02失败保留。 +2. 输出大小扫描以单次lstat读取元数据;失踪条目最多4次完整重扫,持续变化/权限错误仍停止。继承ledger整体guard也仅对FileNotFound有限重试,不改旧冻结训练器。 +3. native扫描对access=False只在同目录fd复查确认ENOENT时记录消失并保留末次字节作保守计数;不可读且仍存在的文件/其他错误继续失败。此最后修复仅CPU验证,**尚未完成端到端训练资格验证**。 +4. 最近修复专项280passed/1skip;rxy-04启动前组合790passed/1skip。CPU测试、配置准备、物理功能、候选验收须分开;独立适配器复审曾因服务限额中断,不声称已获完整独立批准。 +5. 当前所有旧准备绑定均不能授权修改后的源码。既有运行授权已消费;本次文档整理/源码提交不授权新Kit运行或训练。 + +## 预算与历史边界 + +- 原ContactCycle历史账本31,850,496环境转移保持不变;RootXY账本另列,不伪造合并后的实际执行数。 +- P1/P2历史预留254,500共享物理调用,不是实际调用总数。 +- 两次RootXY RUN各保留完整上限1,048,576优化+806,400验证环境转移、73,460共享调用;累计授权预留2,097,152+1,612,800转移、146,920共享调用。首RUN完成0更新,第二RUN完成81;预留与实测严格分开。 +- 原u256仍是冻结开发基线。旧32D失败、Reach1.0.1、受载站立1.0.0及候选专属漂移豁免均不改写。站立豁免不得用于本任务。 + +## 本次源码整理验证 + +接触周期全套+PartSpec契约+已有可视化/CLI改动:**1403passed、6skipped**(CPU,113.30s)。全仓首次试跑为1544passed/9skipped/28failed:本次README版本标题已修正;6项旧lifecycle fixture补齐新增constructor-export元数据边界后通过;其余21项涉及当前3.11/Torch2.7环境不满足旧冻结发布的3.12/Torch2.10约束,或缺少pxr任务注册依赖,未放宽生产检查。不能称全仓/Kit验证全绿。 + +归档逐文件hash和冻结计划原字节另行检查;本次不启动物理或训练。产物身份和失败报告见[证据索引](evidence_index.md);后续按[下一阶段](next_stage.md)推进。 diff --git a/docs/evidence_index.md b/docs/evidence_index.md new file mode 100644 index 0000000..4fac75a --- /dev/null +++ b/docs/evidence_index.md @@ -0,0 +1,45 @@ +# 下一阶段证据索引 + +相对路径以仓库为根;`/home/cen/sf-runs/`是本机外部证据,**不随此源码提交**。原始轨迹、权重、授权、账本不移动/覆写;历史grant不是新运行许可。跨机器使用须另行携带并校验所需资产、快照、权重和报告。 + +## 必须保护的身份 + +| 对象 | 路径 / SHA256 | +|---|---| +| 冻结v1候选 | `logs/contact_cycle/sim_residual_training_v1/prepared01/candidate_u0256.pt` | +| 候选SHA256 | `c088bb3aecaca751867118858626fd508f7a3983303802d7524353f80802cf99` | +| 原训练manifest SHA256 | `a70a226f176104414d2da295f08cce230f6b91ed3e2b78490c78f7ffce95ab01` | +| RootXY u64(不可选取) | `/home/cen/sf-runs/rxy-04/checkpoint_u0064.pt` | +| u64 SHA256 | `960eacdba588fe27fc1751be3be5ac575d10e2ef742d2708d35a304209372aa8` | + +原v1训练seed91501/dev93501,256更新;128/128名义开发功能通过。u256相对u0高度RMS27.325→19.623mm、摆动13.680→7.494mm、落点13.705→9.951mm;候选不是独立验收技能。原执行子代理训练后曾`fetch failed`,父级只读复核结果,不掩盖执行链路异常。 + +## 首先阅读的证据 + +| 证据根 | 入口与用途 | +|---|---| +| `logs/contact_cycle/sim_residual_training_v1/` | `parent_final_verification.json`、`verify_delivery.py`、`prepared01/`;原训练/冻结快照 | +| `logs/contact_cycle/frozen_replay_p0_01/` | `run/result.json`、`supervisor.json`;名义单周期复现 | +| `/home/cen/sf-runs/p1-03/` | `FINAL_P1_ROW0.md`、`FINAL_P1_ROW1.md`、`row1-paired-analysis.json`;双方严格漂移失败 | +| `/home/cen/sf-runs/p2-01/` | `FINAL_P2_ATTEMPT.md`;bootstrap失败、20行not_run、计数缺失、91,000预留保留 | +| `/home/cen/sf-runs/p2-02/` | `FINAL_P2_MATRIX.md`、`parent-offline-audit.json`、`audit_retained_matrix.py`;20条完整XZ复核、全部10组失配 | +| `/home/cen/sf-runs/rxy-authority-03/` | `FINAL_PILOT_ATTEMPT.md`;首次RUN扫描竞态、0更新、终态缺失 | +| `/home/cen/sf-runs/rxy-authority-04/` | `FINAL_PILOT_ATTEMPT.md`、`parent-postexit.json`、`validation-summary.json`、`parent-progress-check.json`;81更新与u64非候选结论 | +| `/home/cen/sf-runs/rxy-04/` | `progress.jsonl`、`budget.json`、`run-supervisor.json`、`physics-counts.json`、三个`validation_*.json`;原始部分训练证据 | + +rxy-authority目录包含本机授权生成脚本,仅可参考结构,不复制旧身份/时钟/PID作为新授权。rxy-01/02配置失败、rxy-03/04配置成功均保留在对应根中。RootXY失败后的counter快照不是最终实际总数;缺失报告保持缺失。 + +## 旧发布和更早失败 + +- Reach:`releases/go2_fl_reach-1.0.1/`。 +- 受载站立:`releases/go2_fl_loaded_stand-1.0.0/`;[候选专属豁免说明](loaded_stand_release_1_0_0.md)。原120秒30.78mm>30mm仍是原始失败,不能推广豁免。 +- 旧32D/更早评估失败:`logs/contact_cycle/`及`/home/cen/sf-runs/diag-cpu-01/FINAL_DIAGNOSIS.md`。不删除失败权重或历史账本。 +- 文档原文与旧细节见[历史资料](README.md#历史资料);归档不构成可执行计划。 + +只读检查原训练产物(需要本机实际证据文件): + +```sh +python3 logs/contact_cycle/sim_residual_training_v1/verify_delivery.py +``` + +任何新源码/文档改动都可能影响manifest;新准备必须重建,不能凭Git提交或历史文件重哈希自动获得运行资格。 diff --git a/docs/leg_interface.md b/docs/leg_interface.md deleted file mode 100644 index 36520c9..0000000 --- a/docs/leg_interface.md +++ /dev/null @@ -1,114 +0,0 @@ -# 单腿运行与验收 - -软硬件描述规范的唯一入口为 [统一软硬件描述规范 v2](software_hardware_spec.md)。本文只记录当前单腿兼容运行链路的实现、验收证据与限制,不是独立规范,也不代表通用 v2 adapter 已实现。 - -**新增受载站立任务另见[loaded_stand.md](loaded_stand.md)**:可运动底座、六维测力及接地承载,采用新观测契约;本页下述固定支座token和1kg合成anchor描述仅适用于旧链路,不能直接用于移动根。 - -## 1. 实现范围 - -```text -LegSpec JSON → URDF 子树/资源/许可 → USD 与映射 -→ 固定支座环境 → 形态/状态 tokens → 共享 actor/critic → PPO -``` - -- 已实现真实 Go2 FL、Go1 FL 导入、CUDA 批量仿真、indexed reset、目标条件共享 PPO 短更新。 -- 支持独立 bounded revolute 树、显式辅助 fixed 支链、一个足端、sphere/box 间隙探针;mimic、prismatic/continuous、闭链、柔性结构和 mesh 足底近似明确不支持。 -- 第二阶段新增仅绑定 Go2 FL 的离线 IQL Reach 冻结链路,使用本页既有 token/PD 接口;见 [使用说明](stage2_usage.md) 与 [验收报告](stage2_acceptance.md)。 -- **未实现** ModSkill 式学习 latent、跨部件协调、通用高层调用及真实留出形态泛化。人工 goal、URMA 内部 action intent、保存 checkpoint 都不等于已学得通用可复用 Skill。 -- Go2/Go1 都是 3 DoF;生成的 1/2/4 DoF 夹具验证接口,不代替真实不同 DoF 或迁移实验。 - -## 2. 资产与物理契约 - -公共入口:`SkillFactory.legs.LegSpec` / `JointSpec`、`legs.urdf.extract_leg`、`legs.spec.load_bundle`。示例 `configs/legs/{go2_fl,go1_fl}.json`。 - -| 声明 | 含义 | -| --- | --- | -| source_root / urdf / package_name / license_file / license_id | 来源、资源解析及许可 | -| mount_joint / auxiliary_root_joints | 切口与保留支链;辅助 roots 必须 fixed 且共用源 parent | -| joints | 受控关节的**声明顺序**、q_default、action_scale、Kp/Kd | -| foot_body / foot_collision_index | 独立传感 body 与用于 gap 的碰撞体 | -| soft_limit_factor / physics_dt / decimation | 软限位与物理/控制时间步 | - -提取按树而非名称前缀;全部 moving joints 必须受控,保留足端/rotor/固定碰撞辅助。合成 `anchor_base` 无碰撞、质量 1 kg、惯性对角 0.001 kg·m²;固定安装座,不冻结 hip。anchor frame 为安装关节零位 frame,源 parent 到 anchor 的变换保存在 `manifest.mount`,供未来装配使用。 - -输出:`leg.urdf`、asset manifest v2、完整资源、`LICENSE`、分层 USD、`conversion.json`。加载校验 URDF/资源/许可 hash、执行顺序、转换 manifest hash、独立 body/joint 映射、固定支座及层依赖;拒绝逃逸路径、输出覆盖源目录及越界 symlink。改描述/重提取后必须重转换,资产目录整体迁移。 - -真实 Unitree 资产保留 BSD-3-Clause 许可,不修改原文件。有源惯性保留完整 tensor 并检验正定;Go2 `FL_calflower/FL_calflower1` 缺少惯性,PhysX 实测估算质量约 0.0247086/0.0226430 kg,不能冒充厂家数据。质量尺度为源已知惯性质量和,排除 anchor 和未知估算质量;有效导入 body masses 随运行报告保存。 - -默认显式 Ideal PD,示例 Kp=25、Kd=0.5,导入器 drive 关闭;限矩/限速取 URDF,目标为 `q_default + action_scale * clamp(action,-1,1)`,再按软范围裁剪。Go2 默认 `[0,0.8,-1.5]` rad、scale=0.25;限矩 `[23.7,23.7,45.43]` N·m、限速 `[30.1,30.1,15.7]` rad/s。物理 0.002 s / 控制 0.01 s。 - -新任务用本地 `legs/resources/ground.usda`(z=0、静/动摩擦 0.8/0.6、恢复系数 0);旧基准地面保持原配置。足端显式启用 contact reporter;运行入口验证 ground collider 和传感器唯一映射。 - -## 3. 通用任务 `SkillFactory-LegSkill-v0` - -**Kit 启动后**构造 `LegEnvCfg(bundle_dir=..., max_joints=8)` 并 `gym.make(...)`。默认悬空高度为保守足端路径长度 +0.1 m;ReachCommand 对实际动作范围内姿态做 FK,生成运动学可达的足端 link 位置目标。reward 为归一化 tracking、effort 与 action-rate 惩罚,不保证目标动态可行或已学到步态。 - -`obs['policy']` 字典:浮点 CUDA float32、mask bool;anchor 坐标、米/弧度/SI;四元数 **xyzw**、世界 z 向上。完整字段顺序/归一化由 `legs.tokens.FEATURE_SCHEMA` 定义,并存入 checkpoint。 - -| 字段 | 形状 | -| --- | --- | -| joint_description / joint_observation / joint_mask | `[B,Jmax,25]` / `[B,Jmax,3]` / `[B,Jmax]` | -| effector_description / effector_observation / effector_mask | `[B,1,16]` / `[B,1,11]` / `[B,1]` | -| context / goal | `[B,10]` / `[B,3]` | - -- joint description 含 nominal 轴/位置、范围/控制规格、source child mass、COM 惯性及偏移;COM/inertia 转到 anchor nominal frame,**不做 fixed-subtree 复合惯性聚合**。 -- joint observation:`(q-q_nominal)/scale`、`dq/limit`、最后已施加动作(ActionManager.action)。 -- effector:link 原点位置/线速度、ground-filtered **法向**力、接触标志(>1 N)、选定碰撞体 gap。不是切向摩擦力、实测电机扭矩或六维 wrench;其他碰撞体的全脚掌最小 gap 未实现。 -- context 保留重力、dt、地面相对高度、长度/已知质量、限矩对重力力矩比、绝对限速、缺失惯性标志,避免归一化擦掉物理差异。 -- 当前必需传感器,无噪声/DR;未来随机化物理属性必须同步描述,不能沿用静态缓存。 - -`mdp.state(env).snapshot()` 返回新 token 视图;跨 step/reset 保存须 clone。`state(env).actions([B,Jmax])` 去 padding 后再调用 `env.step([B,N])`;**Gym action space 是实际 N**。虚拟槽不驱动,也不参与路由/log-prob/entropy/梯度。 - -`reset(env_ids=...)` 支持全量、索引、空集合 no-op;拒绝负/越界/重复/非整数索引及空 reset reseed。复位清理目标、动作/历史、episode;不重建 stage、不推进物理时间,不改变未选实例。非法原生动作在 ActionManager 改历史前拒绝。timeout 使用 reset 前的 terminal tokens bootstrap,真 termination 不 bootstrap。 - -## 4. 训练与失败语义 - -`LegActorCritic`:静态描述 MLP 的逐 latent 特征 softmax 路由动态状态、共享核心、共享逐关节高斯 decoder。不是大型 Transformer 或完整 ModSkill 复现。 - -不同拓扑按独立同构 worker 采样,只在 PPO 层合并,不能认为 `replicate_physics=True` 支持任意拓扑混装。各资产等量采样、共用采样权重/optimizer,必须同 physics/control dt;device 显式传给 worker。保存未裁剪动作的 log-prob,实际执行再裁剪。更新前检验概率一致性;GAE/旧 rollout 梯度分离,拒绝空/非有限输入。 - -本节原 `train_leg_skill.py` 仍每轮重启 Kit、CPU 文件交换,**仅适合短测试,不适合直接长训或吞吐结论**。其 checkpoint 仍标 `trained_skill=false`。新正式路径 `stage2_sim.py` / `train_offline_leg.py` 常驻采集后固定数据离线更新,与旧 PPO 分离,发布有独立证据门槛。 - -运行报告先写 `passed=false/status=running`,环境成功关闭后才提交成功;JSON 原子替换并拒绝 NaN/Inf。关闭异常/子进程失败/critical log 均保留非零退出码,检查 error/fatal/contact buffer overflow,不能只看进程退出 0。优化按字段读取观测,不缓存跨 reset 状态;FK 常量与 box 投影复用/向量化。 - -## 5. 保留基准 `SkillFactory-SingleLegAnchor-Go2-v0` - -接口不变:CUDA float32 actions `[B,3]`,policy `[B,19]`: - -| 切片 | 字段 | -| --- | --- | -| 0:3 / 3:6 | q-q_default [rad] / dq [rad/s] | -| 6:9 / 9:12 | foot link 位置 [m] / 线速度 [m/s],anchor frame | -| 12:15 / 15:16 | ground 法向力 [N] / contact float 0/1 | -| 16:19 | ActionManager.prev_action(旧接口历史语义,区别于新任务) | - -旧默认支座高度 0.344598 m、足球 r=0.022 m / local offset=(-0.002,0,0);默认 timeout 10 s,验收禁用。旧空 reset 在验证调用端做 no-op;不要假设底层普通 ManagerBasedRLEnv 支持空索引。诊断 `single_leg_anchor.mdp.observations.diagnostics` 包含 q/dq/目标、限幅前后命令 torque、gap、anchor pose、`[B,5,1,3]` 历史;torque 非实测、`reaction_wrench=None/valid=False`。 - -## 6. 验收证据与限制 - -**第一周历史完整验收**:`logs/week1_anchor/acceptance/result.json`,`passed=true/milestone_complete=true`;原 Go2 URDF SHA256 `7d19fe48e2e689ee1a032ab99f2a4a8b671d87e73de48d3e65811682a5b48b9e`,源提交 `daadf41ee9afce8f90fdc09a98506012691fa122`。 - -| 项目 | 实测 | 保留阈值 | -| --- | --- | --- | -| B=1/16/64 接口 | `[B,19]` CUDA、顺序/有限性通过 | shape/order/finite 匹配 | -| 悬空 60 s + 2 s warmup,0.1 rad / 0.5 Hz | RMS `[0.043313,0.017097,0.009654]` rad,根漂移/转角 0 | RMS≤0.10,根≤1e-4 m/1e-3 rad,硬限位容差0.01 rad、torque不越界 | -| 20 次抬脚/压地 | 离地 0 N;最小稳定平均 Fz 31.3321 N | gap≥1 cm/力<0.1 N,压地>1 N | -| 接触响应/穿透 | 延迟0控制周期、最大1.40e-6 m | ≤1周期 / ≤5 mm | -| 独立1 kg小球标定 | 9.810026 N | m·g误差≤20% | -| 耐久 | 64×600 s、60,000控制/300,000物理步 | 完整时长、无非有限/意外reset/限位异常 | -| reset隔离 | 1,334操作=1,001非空+333no-op;误差0、prim数2384不变 | ≥1000真reset,q/dq≤1e-5、未选≤1e-6、历史清零、不重建 | -| 资源 | device used 3,814,981,632 B前后不变 | warmup后增长≤5% | - -历史墙钟 576.748 s(约6,658环境控制步/s)、reset均值/最大1.785/4.620 ms;没有墙钟实时门槛。显存包含 Warp/PhysX,不能只报 Torch 分配。固定支座承担载荷,足端力**不等于腿重量**。 - -**本次代码复验**放在 `logs/code_review/`,不覆盖历史验收: -- 全部定向测试 **43 passed**(含旧任务 B=1/16/64 Kit 集成),全仓库 Ruff / `git diff --check` 通过。 -- `go2_64`、`go1_contact_64`、`fixture2_16`:原生/token动作、快照一致性、任意轴/box、索引/空reset与接触复验通过。 -- `train_mixed_smoke`:三组各16环境、32步、1次共享PPO,概率与checkpoint一致性通过;`trained_skill=false`。 -- `anchor_debug`:缩短的旧full套件通过,明确 `milestone_complete=false`;最终运行日志无error/fatal/buffer overflow。 - -缩短时长的通过不等于重做600 s验收;生成夹具或一次 PPO 更新不证明真实泛化/收敛。 - -生成缓存与证据不入库,迁移机器需重建复验。后续顺序:常驻采样与训练/留出评估 → reference/goal-to-latent 学习 → 冻结低层与高层复用 → 单独评估移动基座/整机协调。 - -研究参考:[URMA](https://arxiv.org/html/2409.06366v2)、[ModSkill](https://arxiv.org/html/2502.14140v1)。本项目是固定支座单腿工程改编,不是原论文全身模仿或零样本迁移复现。 diff --git a/docs/loaded_stand.md b/docs/loaded_stand.md deleted file mode 100644 index 4be358c..0000000 --- a/docs/loaded_stand.md +++ /dev/null @@ -1,123 +0,0 @@ -# Go2 FL:连接可运动底座的承载站立试验 - -## 状态与范围 - -已实现独立任务 **`SkillFactory-LoadedLegStand-v0`**,并完成真实PhysX物理验证。预登记范围见[计划](loaded_stand_plan.md),配置为`configs/standing/go2_fl_support_v0.json`。 - -**本页记录的是环境、六维测力和反馈控制基线,不是新训练/冻结策略的验收。** 用户已将该任务设为当前首要任务,独立PPO首轮训练已完成,原始120秒耐久足端漂移超限;用户随后批准该固定偏差,已带豁免冻结发布,见[训练结果](loaded_stand_training.md)、[原始验收报告](loaded_stand_acceptance.md)及[1.0.0发布报告](loaded_stand_release_1_0_0.md)。 旧Reach 1.0.1、训练源码身份及原固定悬空资产保持不变;新场景没有世界固定根约束,也没有加载旧Reach策略来冒充站立。 - -## 机械模型 - -```text -世界参考 ── XY/转动弹簧阻尼导向(z向力严格为0) - │ - 可运动虚拟底座:3 kg - │ - 刚性安装关节 + 六维测力 - │ - 传感器载体:0.05 kg - │ - Go2 FL - │ - 足端—地面接触 -``` - -- 根是floating articulation;固定的是**底座与腿之间的安装连接**,不是底座与世界之间的连接。保留所有固定关节,不合并测力边界。 -- 导向模拟连接后的侧向/姿态约束:横向刚度250N/m、阻尼20N·s/m;转动100N·m/rad、5N·m·s/rad。导向有限幅,竖直刚度/阻尼/力上限均为0,无重力补偿或竖直悬挂。 -- 这是**有导向约束的单腿承重**,不代表完整四足躯干或无辅助单足平衡。`--free-base`可关闭导向做失败探索,未声明该模式稳定。 -- 虚拟底座是合成参数,不是厂家躯干模型。原腿真实惯性不改;两条源装饰支链没有惯性,仍采用导入器推定值并披露。运行时实际总质量为**5.388351kg**,不是仅凭已知源质量估算支撑力。 -- 平地材料沿用项目地面:静摩擦0.8、动摩擦0.6、恢复系数0;没有验证随机地形/摩擦/传感噪声。 - -## 六维传感与接口 - -使用Isaac Lab `JointWrenchSensor`读取PhysX内部固定关节反力,选取child=`anchor_base`的link行,**不是actuator effort,也不是外力指令副本**。 - -- `mount_wrench`顺序`[Fx,Fy,Fz,Tx,Ty,Tz]`,N/N·m;原点为安装关节,坐标为child joint frame;方向为底座施加在腿上的力/矩。没有重力去皮,包含惯性与接触传递影响。 -- 当前安装关节两侧原点重合、child局部旋转为单位旋转。已在实际受力情况下核对符号/力矩原点,不能不经复验推广到任意偏移安装frame。 -- 导向力、扰动力都在世界坐标表达,作用于底座安装原点,分别记录;它们不冒充传感值。 -- 新契约是`go2-fl-loaded-stand-v0`,不是`leg-token-v1`,也不是通用SoftwareSpec v2 adapter。 - -`obs["policy"]`各字段为float32、仿真device上的批量tensor: - -| 字段 | 形状 | 含义 | -|---|---|---| -| `joint_state` | `[B,3,3]` | q[rad]、dq[rad/s]、上一已执行normalized action | -| `base_state` | `[B,13]` | 相对env原点的位置、**xyzw**四元数(本机Isaac Lab版本)、世界线速度/角速度 | -| `mount_wrench` | `[B,6]` | 安装口实测约束反力/矩,N/N·m | -| `foot_state` | `[B,10]` | 根系足端球心位置、旋转根系相对速度、世界地面法向力3维、gap[m] | -| `guide_wrench` | `[B,6]` | 上一物理子步实际施加的导向wrench,非传感器读数 | -| `goal` | `[B,1]` | 名义站立底座高度[m] | - -移动根相对速度包含`omega × r`项;足端接触传感器只提供法向力,不声称摩擦wrench。动作是`[B,3]`,按hip/thigh/calf顺序,裁剪到[-1,1]后转换成`q_nominal + 0.5*action`并裁剪软限位。保留原PD、硬件限矩/限速。奖励包含承重接触、高度、倾角、effort和动作变化;默认有timeout、invalid和fall终止。 - -## 本轮真实物理证据 - -最终正试验:`logs/loaded_stand/v0/standing_clean/`;无动力负对照:`logs/loaded_stand/v0/disabled_clean/`。后者的`passed=false`是预期站立失败,没有重标为成功。汇总见`logs/loaded_stand/v0/summary.json`;正试验16项物理/接口检查、进程退出和日志检查均通过,不能只看单个物理报告的passed位。 - -- **13场景×14秒**:无扰动,加世界±Fx/±Fy/±Fz/±Tx/±Ty/±Tz;幅值5/5/10N、1/1/1N·m。3–6秒与8–11秒施加梯形脉冲,渐入/渐出各0.5秒。 -- PGS、500Hz物理、100Hz控制;每个试验7,000批物理快照,13环境合计91,000环境物理步。前2秒settle,试验内无自动复位。 -- 初始化前2秒内最大下沉**54.06mm**;下面8.01mm高度结果仅指settle之后,没有隐藏为全程高度误差。若实际装配不允许这种启动下沉,需要另设初始化/控制验收。 -- 基线是根据底座位置/速度误差、名义FK Jacobian进行积分修正的关节位置控制器。六维传感器已提供给任务观测并独立验证,但本基线没有使用学习模型或声称学会力反馈策略。 - -| 指标(13环境最坏值) | 实测 | 预登记门槛 | -|---|---:|---:| -| settle后高度误差 | **8.011mm** | ≤60mm | -| settle后倾角 | **3.292°** | ≤15° | -| 足端中心水平漂移 | **1.562mm** | ≤30mm | -| 承重接触比例 | **100%** | ≥98% | -| 平均竖直力平衡相对误差 | **0.00513%** | ≤10% | -| normalized applied torque峰值 | **0.24634** | ≤1.00001 | -| normalized joint velocity峰值 | **0.18830** | ≤1.00001 | -| 硬限位超出 / 导向z力 | **0rad / 0N** | ≤0.01rad / 0N | -| 安装口准静态力误差 | **0.04027N** | ≤1.5N | -| 安装口准静态力矩误差 | **0.002964N·m** | ≤0.3N·m | - -准静态检查独立计算底座重力、COM力矩和实际施加外力,分别比较无扰动[2,3]s、两个载荷平台[4.5,5.5]/[9.5,10.5]s的测量均值。不以这个检查声称动态传感器精度或实机标定。足端中心漂移只是接触位置代理,不是直接摩擦滑移测量。 - -负对照在**全部13环境**出现约**0.3546m有符号向下位移**,且关节applied torque始终为0,承重接触/高度/力平衡等失败。底座最终可能由自身碰地承载,这不算站立。 - -`physics_trace.pt`保存每个500Hz子步的底座状态、关节状态/力矩、足端法向力、实际导向/扰动和安装口世界wrench;`trace.pt`另存100Hz控制观测/动作。indexed/empty reset、3控制步timeout的pre-reset terminal observation和复位后传感清零、非法动作原子拒绝均实际验证。reset probe的15个额外物理步不混入7,000步试验统计。 - -## 失败记录与工程检查 - -- 首次转换未显式给深层foot开启ContactReportAPI,运行失败;已修复转换器,失败资产保存在`build_initial_asset/`,旧asset没有被改。 -- 本资产在原TGS配置下,安装口反力不满足独立静力平衡(约64N误差),不能采信。GPU与CPU调试结果保留`debug_02/03/cpu`。改用本任务独立的PGS配置后通过;不是对整个TGS后端作通用结论,也没有修改旧Reach配置或降低门槛。 -- 最终日志审计发现继承的`enable_external_forces_every_iteration=True`是TGS专用,不受PGS支持。已显式关闭并重新执行`standing_clean/disabled_clean`;正试验无critical日志。监督器也新增“即使子进程退出0,只要日志出现Error/Fatal仍拒绝通过”的回归。较早`standing_final/disabled_final`保留,不作为最终干净日志验收。 -- 独立只读审查指出倒塌判据应为有符号、逐环境判定,以及应保存500Hz而非仅100Hz历史;两项已修复并重新执行最终正/负物理试验,复审无遗留发现。 -- 独立读取500Hz历史重新计算高度/倾角/向下位移、正负载荷峰值和三个测力平衡窗口;发现并纠正了说明/重算器中的四元数顺序假设。本机Lab采用xyzw;仿真实现始终使用同版本Math API,物理结果不变,失败重算记录也保留。 -- pytest **207 passed, 3 skipped**,Ruff及`git diff --check`通过。skip为原有可选Kit集成项;本轮真实Kit试验独立执行,不以fixture替代。 -- 使用`supervise_sim.py`监督进程组,只清理自己收养的Kit/Hub/telemetry子进程,最终均`cleanup.clean=true`。不声称Kit原生退出没有helper残留。 - -- 本地新产物约105MiB;单GPU串行执行,未开展训练。最终日志/独立历史重算发现问题后补做复验,开发验证超过原45分钟估计(约50余分钟),不声称满足原时间预算。 - -## 复现 - -在仓库根目录,用新输出路径保留所有历史证据: - -```bash -unset CONDA_PREFIX VIRTUAL_ENV -export PYTHONPATH="$PWD/source/SkillFactory" PYTHONUNBUFFERED=1 -LAB=/home/cen/IsaacLab/isaaclab.sh - -# 仅在新机器/不存在派生资产时执行;拒绝覆盖已有目录 -python3 scripts/supervise_sim.py --log logs/loaded_repro/build.log \ - --report logs/loaded_repro/build_process.json -- \ - "$LAB" -p scripts/assets/build_loaded_leg.py --headless - -# 正试验 -python3 scripts/supervise_sim.py --log logs/loaded_repro/stand.log \ - --report logs/loaded_repro/stand_process.json -- \ - "$LAB" -p scripts/verify_loaded_leg.py --num_envs 13 --duration 14 \ - --output logs/loaded_repro/stand --headless - -# 负对照:预期非零退出;必须检查negative_control_collapsed和逐环境向下位移, -# 不能把任何异常退出都当作“符合预期的倒塌”。 -python3 scripts/supervise_sim.py --log logs/loaded_repro/disabled.log \ - --report logs/loaded_repro/disabled_process.json -- \ - "$LAB" -p scripts/verify_loaded_leg.py --num_envs 13 --duration 14 \ - --controller disabled --output logs/loaded_repro/disabled --headless -``` - -去掉`--headless`可观察GUI。少于13环境或短于14秒只能调试,完整协议门槛会失败;单环境演示可加`--scenario-offset 1`观察横向扰动。修改载荷质量/几何时需更换配置中的`support_asset`到新目录并重新生成,不能让旧USD与新质量配置混用。 - -下一阶段需要单独定义正式RL训练、载荷/刚度分布、留出扰动和新冻结包验收。当前结果不覆盖大冲击、随机底座运动、其他腿、整机平衡、承重行走或实机安全。 diff --git a/docs/loaded_stand_acceptance.md b/docs/loaded_stand_acceptance.md deleted file mode 100644 index d038b95..0000000 --- a/docs/loaded_stand_acceptance.md +++ /dev/null @@ -1,52 +0,0 @@ -# Go2 FL受载站立:1024候选独立验收预登记 - -**后续状态:用户已明确批准本次固定漂移偏差,现已带豁免冻结发布1.0.0,见[发布报告](loaded_stand_release_1_0_0.md)。以下原始门槛、实测失败和首次拒绝结论仍按发生时的事实保留,不回填成功。** - -用户授权独立留出测试、完整物理验收和冻结发布。仅测试已选择的1024更新,不重训、不使用holdout重新选模或改变门槛。候选checkpoint SHA256为`f2197339dea3622ed92b82fdddff3400f2a82f3ca4d4fd3be6c72b45f1a77704`。 - -## 固定协议(首次留出运行前登记) - -- 新发布名预留`go2_fl_loaded_stand-1.0.0`,不是Reach后继版本。旧训练源码、配置、资产、权重及旧发布保持不变。先提取纯actor、固定normalization、绑定task/support hash生成candidate manifest;所有物理证据绑定整个candidate身份。actor纯PyTorch直接3动作,无teacher、残差、critic或采样噪声。 -- **holdout**:seed83000,256环境×14秒;13场景平衡覆盖后独立打乱,幅值[0.6,1.0]、时移[-0.25,0.25]。同一输入的独立零动作hold对照;settle2秒后逐500Hz高度均值比hold改善≥30%。所有环境仍须满足原高度/倾角/foot drift/contact/vertical balance及全时段安全阈值。禁用自动reset、完整记录失败状态;出现fall不会被重置后的好状态替代。 -- **full**:13环境×14秒,原默认13类单轴±全幅、零时移、seed71000;必须通过既有16项环境物理门槛,额外记录高度均值/每环境结果及全时段跌倒。原测力三个准静态窗口[2,3)、[4.5,5.5)、[9.5,10.5)秒,force≤1.5N、torque≤0.3Nm,不进行偏置拟合。 -- **stability**:26环境×120秒(每场景2环境),seed85000;每14秒重复完整载荷协议,不reset,2秒只在开头settle一次;整个后续118秒沿用高度≤0.06m、倾角≤15°、接触≥98%、足端水平漂移≤0.03m、平均竖直平衡误差≤10%。所有物理子步有限、torque/velocity≤1.00001、硬限位超出≤0.01rad、guide Fz严格0,无跌倒/异常reset。复用每周期准静态窗口校验测力。GPU进程显存尾部10秒峰值相比2–12秒窗口峰值增长≤5%,外部nvidia-smi采样并计入真实物理时间;不以空尾部报告0增长。 -- **exploratory**:60环境×14秒,seed84000;所有两轴组合15对×4符号、各轴全幅,单独披露,非训练分布、非发布适用域。不可用于选模。探索失败保留,不将其重标通过,也不把单轴域的通过泛化为组合域能力。 -- 全部模式保留500Hz原始物理trace和稀疏实际观测/动作;CPU独立重算所有物理门槛和原始载荷覆盖;不能仅信报告passed。hold基线可能不通过技能门槛,必须如实保存退出非零和失败门槛,不阻止以相同输入评估其均值对照。 -- 适用物理域不变:PGS、floating root、3kg合成底座、有效总质量约5.388351kg、XY/转动导向,竖直辅助0;不是自由单足平衡、四足、跨形态或实机安全认证。foot drift仅球心滑移代理、contact仅法向力。源缺失惯性/导入推定值继续披露。 - -## 工程与冻结 - -- 新接口`go2-fl-loaded-stand-v0`,45维固定SI输入,xyzw四元数;与旧leg-token-v1及通用v2 adapter不兼容。 -- 对candidate及最终冻结目录/归档解包执行真正隔离CPU解释器package-only加载(复用已有Python3.12/Torch2.10 CPU venv,不安装运行时),B1/16/64/256各100次确定性调用、输入/权重/buffer不变、无grad/训练/仿真依赖;错误shape/dtype/device/finite/绑定和损坏文件拒绝。 -- 实际物理观测的CPU/CUDA输出最大绝对差≤1e-5。检查点actor提取与训练策略确定性输出等价、critic/optimizer/RNG不进推理包。 -- 发布门禁fail-closed:完整训练/validation-only选模、候选身份、独立加载、holdout和对照、full、stability及原始trace重算、资源/工程检查、干净进程日志全部通过才原子发布;没有足够证据不得写accepted。探索结果必须存在并如实披露,但不是单轴域门禁。 -- 最终版本目录独占、禁止覆盖;最终manifest新增证据时明确绑定已测试candidate manifest与不变运行时核心,不偷换测试身份;暂存最终包通过独立加载后才原子rename,再验证正式目录和归档。SHA256是完整性校验,不是数字签名。 -- 资源护栏:本轮实际仿真/加载累计上限30分钟、额外产物2GiB(不含开发时间);不自动开展第二候选或调参搜索。发现工程错误保留失败证据,仅对同一候选修复验证工具并记录新执行身份;任务/验收门槛不变。 - -证据根目录:`logs/loaded_stand/acceptance_v1/`。以上是首次留出运行前的预登记,门槛未变。 - -## 执行结果:耐久门槛失败,拒绝发布 - -最终证据在`logs/loaded_stand/acceptance_v1/v2/`。独立审查修正了每周期测力窗口必须分别取误差的问题,并补全包内混合坐标系说明;原candidate及第一批测试完整保留。说明修正改变manifest,因此同一权重、同一输入协议重新执行full/holdout/hold;这是同一留出集的身份复验,不是新的独立测试集,也没有以测试结果选模。 - -固定checkpoint SHA未变;actor SHA256始终为`1384b5aa780cded1f4ed21b617ff8c883a5f82826c4cb4c9415b75f315b1e664`。修正说明后的candidate manifest为`7cb478a3b17aaf307d2d6e3c2b49cc25aa9af1ee9d118dd731b52f618c27e628`,不是已发布版本。 - -| 测试 | 结果 | settle后平均/最大高度误差 | 最大足端漂移 | -|---|---|---|---| -| 独立holdout,256×14秒 | 通过 | 0.921 / 10.693mm | 4.442mm | -| 同输入零动作hold | 高度不通过,保留失败 | 66.377 / 97.808mm | 3.313mm | -| 原13类全幅,13×14秒 | 全部19门槛通过 | 1.097 / 11.397mm | 4.637mm | -| 连续耐久,26×120秒 | **foot_drift失败** | 1.703 / 20.279mm | **30.783mm >30mm** | -| 两轴组合探索,60×14秒 | 本次探索通过,不泛化适用域 | 1.673 / 19.595mm | 7.915mm | - -- holdout相对hold平均高度误差改善**98.6126%**。各试验最低承重接触比例均100%、无跌倒/自动reset;guide Fz严格0、物理/测力有限。 -- 耐久环境6/19都是**负向Fz 10N**场景;漂移分别30.620924/30.782583mm,首次越界在115.828/115.760秒,峰值在119.054/119.044秒。参考点固定在2秒,不按载荷周期重置。 -- 父级独立CPU读取全部500Hz trace并重算;另用逐点Python `math.hypot`独立重算两个失败环境,峰值与报告差<1e-12m。不是报告舍入或末段漏采,不能以“仅超0.78mm”为由放行。 -- 耐久其余门槛通过:最大倾角0.057942rad,torque/velocity比例0.265703/0.555917,竖直平均平衡误差≤1.06e-6;26个独立准静态窗口各500样本,最大测力残差0.029747N/0.001857Nm;2–12秒与110–120秒显存峰值增长0%。 -- candidate在真正隔离CPU解释器完成B1/16/64/256各100次确定性调用/坏输入和损坏拒绝;真实物理观测CPU/CUDA最大动作差2.3842e-7。没有声称最终包/归档已经独立验证,因为没有生成正式包。 -- 五个最终试验全部完成和清理;hold与stability保持原失败/非零退出,唯一critical记录是launcher对预期失败的退出报告,没有后端Error。探索是在必需耐久失败后单独完成的已登记信息性试验,不用于放行。 -- 全部物理尝试(含旧说明身份复验)累计**502.72秒**;原始证据约**1.72GB**,符合30分钟/2GiB护栏。旧Reach发布全部checksum通过,原训练源码与候选权重不变。 - -`v2/trace_audit.json`的passed仅表示证据读回一致,其`accepted_skill=false/publication_blocked=true`;`v2/publication_gate.json`实际非零拒绝必需试验失败。未调用正式发布操作、未创建`releases/go2_fl_loaded_stand-1.0.0/`。发布器另补了暂存/最终加载/归档加载/提交后报告失败的隔离回滚,以及锁内不可覆盖检查;这些纯工程测试不代替真实成功发布。 - -**下一步需用户授权新一轮训练**,针对负向竖直载荷下长期足端漂移改进训练/长回合覆盖。不得用已查看的holdout重选本轮其它checkpoint、缩短耐久时长、重置漂移参考点或放宽30mm门槛。新一轮应独立预登记,并保留本轮失败。 diff --git a/docs/loaded_stand_plan.md b/docs/loaded_stand_plan.md deleted file mode 100644 index fcd4644..0000000 --- a/docs/loaded_stand_plan.md +++ /dev/null @@ -1,32 +0,0 @@ -# 单腿连接底座承载站立:v0实施/验收计划 - -## 范围(实现前登记) - -Go2 FL单腿,地面接触,可运动的3kg虚拟底座→保留的固定测力关节→0.05kg合成传感器载体→原腿。世界根固定约束必须不存在,`is_fixed_base=false`。真实腿源惯性不改;源中两条装饰支链惯性缺失,保留导入器推定值并报告实际质量,不冒充厂家惯性。 - -默认采用弹簧阻尼导向,横向250N/m、20N·s/m,转动100N·m/rad、5N·m·s/rad;世界z向刚度/阻尼/外加导向力均为0。它模拟其他装配结构的侧向/姿态约束,不是完整四足躯干,也不是无辅助单足平衡。脚必须承担竖直重量;无动力负对照应倒塌。可将导向关闭做自由底座实验,但不预先声称稳定。 - -挂载传感器取PhysX JointWrenchSensor的内部固定关节反力,单位N/N·m、顺序Fx,Fy,Fz,Tx,Ty,Tz,原点为mount joint、坐标为child joint frame。不是将施加的扰动力复制成传感读数,不做隐式重力去皮。独立静力平衡确认符号和力矩原点。世界坐标下的导向wrench与扰动指令独立记录。 - -新任务/观测契约为`go2-fl-loaded-stand-v0`,不兼容Reach冻结权重。先交付物理环境、传感器、奖励/失败条件、站立基线与实测报告;**本轮不是新的RL训练/冻结发布**。旧任务、旧资产、已发布1.0.0/1.0.1及训练绑定源码不改。 - -## 预定试验与门槛 - -配置见`configs/standing/go2_fl_support_v0.json`。500Hz物理/100Hz控制,14秒episode,前2秒settle。13类场景为无扰动和世界坐标±Fx/±Fy/±Fz/±Tx/±Ty/±Tz,幅值5/5/10N、1/1/1N·m,作用点为虚拟底座安装原点。3–6秒及8–11秒施加梯形脉冲,0.5秒渐入/渐出。完整检查至少13环境,允许少量环境作调试但不算全类别验证。 - -- 检查完全浮动root、固定sensor joint保留、足端地面接触,传感器不是零值占位。 -- 每物理子步:有限状态、硬关节越界≤0.01rad,归一化torque/velocity≤1.00001;保存实际导向、扰动和六轴测量。 -- settle后每环境:高度误差≤0.06m、倾角≤15°、足端中心水平漂移≤0.03m、有效承重接触比例≥98%。足端中心漂移是接触位置代理指标,不等于摩擦滑移测量。 -- 竖直合力统计:平均足端法向力与实际总质量*g减实际施加竖直外力之差≤10%;导向z力严格为0。传感器静力力误差≤1.5N、力矩误差≤0.3N·m(使用同原点/同坐标)。 -- 负对照:关闭关节驱动力,底座应明显下落/跌倒;不能靠底座弹簧把腿悬挂起来。 -- 只读审计→纯数学/配置测试→小规模真实物理调试→完整13类试验及负对照→回归。失败和调参过程保留,不将调试当holdout或正式学习验收。 -- 单GPU;本轮最多45分钟开发/物理验证资源窗口,不开展大规模训练。到预算则如实报告未完成项。 - -需要正式训练时另定数据分布、质量/刚度变化范围、未知扰动评估和冻结接口;不能仅以奖励上涨或本轮基线通过宣称训练完成。 - -## 实施补记(不改上述门槛) - -- 实际传感读数在该资产的TGS配置下未通过独立平衡检查,改用本任务独立PGS配置;失败日志保留,不以修改符号或放宽误差阈值掩盖问题。 -- 准静态传感检查增加至3个窗口:无载荷平台[2,3]s和受载平台[4.5,5.5]/[9.5,10.5]s,仍使用原力/矩容差。 -- 按独立审查补全500Hz历史,并将负对照判据明确为全部环境向下位移≥0.10m且电机力矩为0;增加真实timeout终端状态/复位与非法动作拒绝检查。 -- 实施结果与边界见[实测报告](loaded_stand.md);这不是新RL策略发布。 diff --git a/docs/loaded_stand_release_1_0_0.md b/docs/loaded_stand_release_1_0_0.md index 0568325..4894140 100644 --- a/docs/loaded_stand_release_1_0_0.md +++ b/docs/loaded_stand_release_1_0_0.md @@ -48,4 +48,27 @@ action = policy.act(observation) # float32[B,3],100Hz q_target = policy.position_targets(observation) # rad,驱动仍需限矩/限速 ``` +### GUI演示与扰动力箭头 + +在本机桌面终端、仓库根目录运行(沿用已有派生资产): + +```bash +unset CONDA_PREFIX VIRTUAL_ENV +export PYTHONPATH="$PWD/source/SkillFactory" PYTHONUNBUFFERED=1 +OUT="logs/loaded_stand/view_$(date +%Y%m%d_%H%M%S)" +python3 scripts/supervise_sim.py \ + --log "$OUT.log" --report "${OUT}_process.json" -- \ + /home/cen/IsaacLab/isaaclab.sh -p scripts/evaluate_standing.py \ + --package releases/go2_fl_loaded_stand-1.0.0 \ + --manifest 27e5e56908a0a8585a13913f27663b0dd3cdb7270614a0a05355334de4093e98 \ + --mode full --controller policy --device cuda:0 --viz kit \ + --force-arrows --force-arrow-scale 0.03 --output "$OUT" +``` + +- `--force-arrows`是可选的纯显示开关,默认关闭。橙色箭头尾部位于底座实际施力点,方向为世界坐标系的**实际已施加扰动力**,长度=`|F|×0.03 m/N`(5N→15cm、10N→30cm)。它不是合力、导向力或安装口传感器反力。 +- 无力时隐藏;纯力矩场景也没有力箭头,不能据此判断没有扰动。箭头使用本地程序化圆柱/圆锥,无碰撞/刚体,无需下载箭头资产。 +- `full`为13个固定场景×14秒,3–6和8–11秒有载荷。默认镜头靠近无扰动的`env_0`;可在Stage选中`/World/envs/env_6/Robot`后按F聚焦向下加载场景。 +- 要显示预登记的随机场景/幅值/时移,改成`--mode holdout`(256环境、固定随机种子);它不是每帧无限制随机换向。`--mode stability`则为26环境×120秒重复载荷。 +- 这是重新仿真,不训练,完成后关闭窗口。显示可能改变运行耗时/图形内存,结果写入独立目录;不替换原始发布证据,也不将已有豁免自动用于新试验。 + 完整混合坐标系/单位见包内README;已批准的漂移偏差见`DEVIATIONS.md`。适用域仍是Go2 FL、3kg合成底座、PGS、有XY/转动导向且竖直辅助为0的仿真单腿,不是无辅助平衡、四足或实机安全认证。 diff --git a/docs/loaded_stand_training.md b/docs/loaded_stand_training.md deleted file mode 100644 index 6947b9d..0000000 --- a/docs/loaded_stand_training.md +++ /dev/null @@ -1,73 +0,0 @@ -# 当前首要任务:Go2 FL受载站立策略训练 - -用户已要求将受载站立设为主线并开始训练。旧Reach1.0.1仅维护归档/完整性,不再作为当前开发重点。任务物理契约及既有基线见[loaded_stand.md](loaded_stand.md)。 - -## 首轮结果:训练完成,随后耐久验收失败 - -2026-10-12首轮完整结束:**2,048次更新、16,777,216条在线转移**,含启动/清理共**1,510.50秒(25.18分钟)**;单GPU、无恢复重启,run约12.6MB,满足本轮90分钟/2GiB预算。KL早停按预登记执行,实际14,979次optimizer minibatch、30,676,992条梯度抽样,不把未执行的epoch计入训练量。 - -固定validation(256环境×14秒,seed82000)结果: - -| 指标 | 选中1024更新 | 零动作hold基线 | -|---|---:|---:| -| settle后平均高度误差 | **0.9663mm** | 66.4361mm | -| 最大selection高度误差 | 11.4227mm | 99.4814mm | -| 最大倾角 | 0.039608rad | 0.052887rad | -| 跌倒比例 | 0 | 0 | -| 最低环境承重接触比例 | 100% | 100% | - -按validation-only规则选中**1024更新**,平均高度误差改善**98.55%**;最终2048更新误差2.5967mm,并未默认选最终权重。8次validation与hold使用完全一致的输入;hold是零动作控制对照,**不是holdout**。现有控制基线8.01mm是另一个协议下的最大误差,不能与此处均值直接对比。 - -- 候选:`logs/loaded_stand/train_v1/checkpoints/best_validation.pt`。 -- 编号副本:`checkpoints/update_001024.pt`;已读取并核实model和optimizer逐tensor一致。两文件因保存时机/RNG状态等元数据不同,不要求整个文件hash相同。 -- 候选SHA256:`f2197339dea3622ed92b82fdddff3400f2a82f3ca4d4fd3be6c72b45f1a77704`;后续验收须绑定此候选,不以holdout重新选模。 -- 训练期间共3次跌倒终止;所有8次validation未跌倒。全程物理/传感有限,关节硬限位超出0、导向Fz为0,所有12种有符号载荷均实际出现。normalized torque峰值0.666229、velocity峰值1.000000477,在1.00001容差内,**不声称速度有裕量**。 -- 最终累计390,680批物理快照,其中训练327,680批;其余是hold/validation,不混入在线训练转移量。进程退出0、critical日志为空、自有helper清理完成。 -- 父级完成检查读取了4份checkpoint、检查185个tensor,确认真实参数变化、固定归一化buffer不变,并做了CPU合成输入确定性调用。**这只是检查点完整性/数值检查,不是包-only加载或物理验收。** -- 原绑定源码与旧Reach发布校验均未变。完整证据:`result.json`、`selection.json`、`validation/`、`completion_verification.json`、`completion_monitor.md`,以及外部`train_v1_process.json`。 - -**后续独立验收已执行**:holdout与原13类全幅通过,候选包独立CPU加载通过;但26环境×120秒耐久的两个负Fz场景足端漂移30.62/30.78mm超过30mm门槛,**拒绝冻结发布**。详见[验收报告](loaded_stand_acceptance.md)。该次原始验收的`accepted_skill=false`保持不变,不重选权重、不自动重训。**之后用户明确批准这一次漂移偏差,已另行带豁免冻结发布1.0.0**,见[发布报告](loaded_stand_release_1_0_0.md);不能把该决定回写成原始物理门槛通过。 - -## 首轮预登记(在正式梯度训练前固定) - -- 新策略从随机初始化开始,**在线PPO**,直接输出3个关节位置目标的归一化动作;不加载Reach权重、不把解析站立控制器作为策略输出或隐藏残差。 -- 输入新任务全部45维观测,包括真实安装口六维wrench;固定物理量缩放、base z减目标高度,无训练中滚动统计。两层128维actor/critic,tanh-squashed Gaussian;PPO概率比使用pre-tanh latent(同一变换的Jacobian抵消)。不声称URMA共享形态或通用v2 adapter。 -- 256环境,32控制步rollout,2048次PPO更新:**16,777,216条在线转移**。每更新4 epochs、minibatch2048;KL超过0.02时停止该更新后续minibatch,不改变转移预算。其余数值锁定`configs/training/go2_fl_loaded_stand_ppo_v1.json`。 -- 沿用已校准的PGS、3kg载荷、XY/转动导向及零竖直辅助。每次episode随机选择13种有符号轴向场景,幅值为配置上限的[0.5,1.0]、脉冲时移[-0.5,0.5]秒;不增加未验证的质量/刚度/摩擦随机化。 -- 环境奖励除以control dt恢复逐步尺度;真正终止额外-5。GAE对timeout使用**复位前terminal observation** bootstrap,对真正跌倒置零,episode边界切断递归;不得用复位后状态偷接回报。 -- 每128更新保存完整model/optimizer/CPU+CUDA RNG/更新计数/累计时间/配置与资产及源码hash;保存原子替换。中断恢复保留learner状态但重置物理episode,**不声称PhysX逐位恢复**。`STOP`文件请求在rollout/update边界保存并退出。 -- 每256更新进行独立确定性validation(seed82000,全部256环境,14秒;幅值[0.6,1.0]、时移[-0.25,0.25])。以`平均高度误差 + 0.10m×跌倒比例`最小选模,失败后的剩余步按0.10m误差计,避免早结束获得虚假低误差。相同validation输入重复比较,validation不回传梯度;训练RNG在验证前后恢复。 -- 启动前记录固定零动作hold基线;validation仅负责选模,**不是发布验收**。最终选择锁定后才允许读取seed83000 holdout:独立扰动采样并加入两轴组合探索(超出本轮训练分布,单独报告),不得以holdout重选权重或调整门槛。 -- 后续候选必须在默认13类全幅协议下逐物理子步通过既有站立/安全/测力门槛,并完成新接口独立加载/耐久等冻结验收;至少比hold基线的平均高度误差改善30%。训练完成、奖励提高或validation最优都不等于技能已发布。 - -## 本轮预算及失败处理 - -单GPU,正式首轮最多90分钟wall time、最多2GiB新run产物;到预算保存并标记paused,不将未完成2048更新标成完整训练。监督进程额外留5分钟启动/清理宽限。保留所有预检和失败尝试,不启动无限调参/自动多轮训练。纯数学/恢复单测与短Kit预检只作工程准备,不计入正式训练量。 - -日志和权重位于`logs/loaded_stand/train_v1/`,预检在独立`logs/loaded_stand/train_preflight/`。`progress.json`是当前进度,`metrics.jsonl`记录逐更新训练/validation;checkpoint不是可直接发布的技能包。训练后还需独立物理测试,不改变既有冻结包。 - -## 启动记录与操作 - -2026-10-12首轮已启动并完成,当前没有继续运行的训练进程。启动门禁证据在`train_preflight/`:225 passed/3 skipped、Ruff通过;`run_02`在第2更新真实退出,再通过新Kit进程恢复至第4更新并完成validation。4个预检更新各发生156次正常timeout,覆盖terminal bootstrap和reset;固定下一批数据的learner/optimizer/RNG精确恢复另有纯Torch回归。预检只有工程意义,不计入正式1678万转移。独立审查的验证评分问题已修复、测试并复审通过。 - -```bash -# 正式首轮已完成;原目录保留,不要覆盖或在原run上追加训练。 -# 新的独立复现应替换下面所有train_v1输出名。 -unset CONDA_PREFIX VIRTUAL_ENV -export PYTHONPATH="$PWD/source/SkillFactory" PYTHONUNBUFFERED=1 -export CUBLAS_WORKSPACE_CONFIG=:4096:8 -python3 scripts/supervise_sim.py \ - --log logs/loaded_stand/train_v1.log \ - --report logs/loaded_stand/train_v1_process.json --timeout 5700 -- \ - /home/cen/IsaacLab/isaaclab.sh -p scripts/train_loaded_stand.py \ - --config configs/training/go2_fl_loaded_stand_ppo_v1.json \ - --output logs/loaded_stand/train_v1 \ - --console-log logs/loaded_stand/train_v1.log --headless - -# 请求安全暂停;不会直接杀死其他Kit/桌面进程 -# touch logs/loaded_stand/train_v1/STOP -``` - -`train_v1.launch.json`保存启动命令/监督PID。监督报告仅在退出及清理后最终落盘;运行中不能把`passed=false/status=running`当作失败。结束后同时检查训练`result.json`、`train_v1_process.json`及日志,不能以进度文件或子进程退出0代替完整判断。 - -恢复时先确认原进程已退出、保存并移走STOP文件;读取`latest_checkpoint.json`,使用同一config/output和`--resume <实际checkpoint路径>`,为新监督log/report选择未占用文件名。不要改绑定源码/配置后强行恢复;从checkpoint重启会重置物理episode。程序没有自动重启/自动换参数/自动发布。 diff --git a/docs/next_stage.md b/docs/next_stage.md new file mode 100644 index 0000000..ad70ed7 --- /dev/null +++ b/docs/next_stage.md @@ -0,0 +1,36 @@ +# 下一阶段:完成监控资格验证,再决定新的有界RootXY试训 + +这是待办,不是运行授权。当前没有合格RootXY候选,旧授权不可复用;不恢复u64、不追加旧账本、不扩大残差或放宽30mm门槛。 + +## 1. 优先解决基础设施可靠性 + +- 对两个输出扫描器与继承ledger统一做原子发布并发压力回归,覆盖`.pending`创建/替换/消失、权限拒绝、symlink/目录替换、持续churn、磁盘/截止时间/清理故障。 +- 保留完整扫描/保守计数,不把真实权限错误吞掉;确认新修复不会低估输出或阻塞监督器。若做真实native路径采样,先明确只读范围与时间,不遍历无关缓存。 +- 专项CPU通过后复核监督器的归属、500Hz安全覆盖、初始化/部分控制计数、最终结果和候选提交路径。独立复审若仍不可用,应明确披露,不用测试代替批准。 +- 完成标准:可复现并捕获两次旧竞态,修复在并发压力下不误报;真实拒绝/预算/取消仍停止,且不会发布候选。 + +## 2. 经另行授权后进行一次新试训 + +1. 复核[固定配方](contact_cycle_rootxy_v2_plan.md)及P1/P2局限,形成诚实的名义单周期可行性决定;P2失配绝不写成通过。 +2. 新隔离短路径、新manifest/来源快照、新desktop身份基线和存储策略。保留所有旧失败、grant、claim和完整扣款。 +3. 按[启动契约](contact_cycle_rootxy_v2_launch.md)单独授权配置PREP:600s含120s清理、零环境/物理/优化;父级验证真实结果与静态文件后,才可另发RUN授权。 +4. 如继续原配方:RTX5080、128环境、最多256更新、7200s含120s清理、1GiB,一次启动,无重试/恢复。旧u256只导入actor/log_std/normalization,fresh critic/Adam/RNG。 +5. 参考和冻结策略各128/128原功能基线必须在优化前通过;开发u64/128/192/256同样128/128,否则停止。不能以return提高替代物理功能。 +6. 任何缺失最终证据、非零退出或清理/身份失败均使检查点不可选取;完整训练也允许“无候选”。只有监督器可在全部终态门槛通过后发布lock。 + +当前dev93511已查看;95511仅是拟用holdout,未来锁定候选前须重新检查是否被使用。不得把重复dev试验当独立泛化证据。新训练预算不是下游P1/P2/holdout预算。 + +## 3. 根据漂移证据决定是否改变方案 + +u64仅将XY RMS从16.250降到15.796mm,而最大漂移均值仍67.196mm、126/128超限。先检查卸载阶段逐tick根漂移、接触载荷、参考饱和及实际残差权限;不能仅凭critic loss或少量RMS收益扩训。 + +若奖励、参考、观测或辅助结构需修改,另立版本、预登记对照和门槛,重新做受影响资格验证;不原地修改冻结v1或RootXY失败证据。 + +## 4. 训练后再补能力证据 + +- 仅合格且完整发布的候选进入新的P0回放和严格P1连续周期;周期间不能reset、瞬移、reanchor或拼接回合。 +- P2修复尚未物理验证;现有p2-01/02根均已消费。新矩阵需先审查有限新campaign支持,不能直接创建p2-03绕过限制。 +- 配对初态一致、事件实际投递、完整轨迹/终态/离线审计分别验证。未投递的扰动不能用来描述受扰后的失败。 +- 整机、无辅助平衡、硬件、sim2real和通用v2 adapter暂不扩展。 + +交付保持精简:更新[current_status.md](current_status.md)和[evidence_index.md](evidence_index.md),详细故障/哈希放独立证据目录,不再追加同级“第N次修复”文档。 diff --git a/docs/software_hardware_spec.md b/docs/software_hardware_spec.md index d8b8a7a..b91cd09 100644 --- a/docs/software_hardware_spec.md +++ b/docs/software_hardware_spec.md @@ -6,7 +6,7 @@ **已落地:** PartSpec v2 / SoftwareSpec v2、JSON Schema、跨文件语义校验、不同部位的设计示例、现有 Go2 单腿描述迁移与回归测试。仓库只保留本规范及两份 v2 Schema,不再并列维护旧版规范。 -**尚未落地:** v2 通用仿真/训练 adapter、描述驱动 launcher、实机驱动、v2 通用冻结发布、跨部件协调训练。第二阶段已有绑定旧 leg-token-v1 的独立 Go2 Reach SkillBundle 流程,见 [验收报告](stage2_acceptance.md),不改变本规范 v2 的运行支持边界。设计模板不是厂家资产;静态契约匹配不证明机械结构可制造、物理模型准确或策略已训练。当前可运行的是[既有单腿任务](leg_interface.md)及专用的[受载站立实验任务](loaded_stand.md)。后者使用独立`go2-fl-loaded-stand-v0`观测契约和六维安装口传感器,仍不是v2通用adapter;已完成独立PPO首轮训练和物理测试,并在用户明确批准本次耐久漂移偏差后[带豁免冻结发布1.0.0](loaded_stand_release_1_0_0.md);原始30mm漂移门槛并非通过,不代表v2通用能力或实机安全。这些文档仅记录具体实现和运行验收,不是另一份统一软硬件描述规范。 +**尚未落地:** v2 通用仿真/训练 adapter、描述驱动 launcher、实机驱动、v2 通用冻结发布、跨部件协调训练。设计模板不是厂家资产;静态契约匹配不证明机械结构可制造、物理模型准确或策略已训练。当前专用单腿链路的进度见[当前状态](current_status.md),参考+学习残差的46D契约见[运行接口](contact_cycle_runtime.md),两者均不代表v2通用adapter已实现。旧Reach绑定leg-token-v1;受载站立使用独立观测及六维测力,已[带候选专属漂移豁免发布1.0.0](loaded_stand_release_1_0_0.md),原始30mm门槛并非通过。旧实现/验收详述已归入[历史资料](README.md#历史资料),不作为另一份统一规范。 ## 2. 分层模型 diff --git a/docs/stage2_acceptance.md b/docs/stage2_acceptance.md deleted file mode 100644 index e764f67..0000000 --- a/docs/stage2_acceptance.md +++ /dev/null @@ -1,83 +0,0 @@ -# 第二阶段历史验收报告 — Go2 FL Reach 1.0.0 - -**最新交付为[1.0.1完整边界复验与发布报告](stage2_release_1_0_1.md):12项检查全部通过,已实际发布。** 下文只记录1.0.0,不将新结果追写入旧版证据。 - -**对1.0.0的复核结论:训练、恢复、选模和冻结推理闭环保留有效;历史报告不足以证明修正后的完整发布验收。** 后续发现并修复了工程/恢复门槛遗漏、边界覆盖不全、失败复验残留成功报告三项问题,见 [修复记录](stage2_reliability_fix.md)。旧边界实际仅覆盖2/6个面、4/12条边、8/8个角。 - -以下是2026-10-12的**历史验收记录**。原机器报告`logs/stage2/v1_attempt2/acceptance.json`及1.0.0包内`accepted`保持原样以便追溯,**不代表已通过修正后的门槛**。旧版“全部完成”结论据上述复核限定。不是通用部件策略或整机迁移完成声明。 - -## 1. 交付 - -- 冻结包:`releases/go2_fl_reach-1.0.0/`;归档:`releases/go2_fl_reach-1.0.0.tar.gz`;完整性入口:`releases/SHA256SUMS`。 -- 正式配置:`configs/training/go2_fl_offline_v1_attempt2.json`。 -- 代码:`legs/offline.py`、`legs/frozen_policy.py`;`scripts/{stage2_sim,train_offline_leg,export_leg_bundle,verify_frozen_leg,verify_stage2_delivery}.py`。 -- 测试:`tests/test_offline_leg.py`;使用/复现:[stage2_usage.md](stage2_usage.md);预注册:[stage2_plan.md](stage2_plan.md)。 -- 第二轮全量数据、训练恢复点、日志和评估:`logs/stage2/v1_attempt2/`。第一轮失败产物完整保留在 `logs/stage2/v1/`,没有覆盖或冒充通过。 - -## 2. 任务与方法 - -真实 Go2 FL 三关节、固定悬空支座,500 Hz PhysX / 100 Hz position-PD 控制。目标为 anchor-frame **足端 link** xyz/L,由 `q_nominal + 0.25*a` 的 FK 生成,`a∈[-0.75,0.75]^3`。每个独立目标从名义姿态复位,2秒运行,最后0.5秒判定。成功要求该窗口**每步**误差≤15mm;均值/P95以episode末段平均误差为统计单位。 - -IQL:固定数据、双Q TD、expectile V、优势加权确定性actor。策略只接收现有token投影的15个状态/目标数;不接收目标关节角。采集器可使用目标关节角、PD反馈、积分补偿与噪声,**发布策略不包含采集器、IK或积分控制器**。仅actor及固定训练集mean/std用于推理。 - -## 3. 正式训练、恢复与选模 - -- 20 shards ×256环境×200步 = **1,024,000条**固定转移,5,120个训练目标;训练种子42,采集种子12000–12019。 -- 完成 **30,000次**更新,batch512(15,360,000次转移抽样),没有以短更新/reward上升作为验收。 -- 第5,000步真实退出并从完整模型/target/optimizer/CPU与CUDA RNG恢复,继续到30,000步。两个独立进程从同一真实5k检查点恢复至5001,全部learner/optimizer/RNG逐tensor一致;另有连续更新vs保存恢复的单元回归。 -- 独立validation种子21000–21001,512目标;10k/20k/30k平均误差分别 **2.621/2.211/2.222mm**,全部过门槛。按预定最小平均误差规则选择 **20,000步**,不是默认选最终权重。 -- 首轮完整30k训练因行为数据稳态误差过大而未通过(10k候选均值10.45mm、成功83.0%)。第二轮修改记录见 [stage2_attempt2.md](stage2_attempt2.md)。阈值未降低,test种子未用于调参。 - -## 4. 独立效果验收 - -| 项目 | 实测 | 预注册门槛 | -|---|---:|---:| -| 未见目标,seed31000–31003 | 1024 episodes | 1024 | -| 末段平均误差 | **2.184mm** | ≤8mm | -| episode均值P95 | **4.532mm** | ≤15mm | -| 成功率(窗口最大误差≤15mm) | **100%**,Wilson95% CI [99.626%,100%] | ≥95% | -| 零动作同目标基线均值 | 53.019mm | 对照,非学习目标 | -| 相对基线误差下降 | **95.881%** | ≥60% | -| 旧边界目标,seed41000(面/边覆盖不全) | 256 episodes | 256 | -| 边界均值 / P95 | **2.709 / 4.719mm** | ≤12 / ≤20mm | -| 边界成功率 | 100%(补充指标) | 不以此替换边界均值/P95 | - -测试和边界报告逐episode保留均值及最大误差。验证/测试是独立进程,不复用训练环境状态。包审查发现需要绑定**整个manifest**而不只是权重,因此相同已选权重/目标/门槛又完整重跑一次,仅补全证据身份;此前结果保留 `pre_manifest_evaluation/`,没有在看过测试后调参或重新选模。 - -## 5. 运行稳定性与控制约束 - -最终耐久:**64环境×120秒**、12,000控制步/768,000环境控制步,另200步warmup;每个物理子步取样,共61,000批物理快照。每2秒换目标,不复位,验证持续运行安全而非连续轨迹跟踪精度。 - -- 非有限状态、意外termination/reset:**0**。 -- 全套最终测试/边界/耐久:关节硬限位超出量 **0 rad**,支座pose漂移 **0**。 -- 耐久峰值归一化applied torque **0.663709**(阈值1.00001)。 -- 耐久峰值归一化速度 **1.000000834**(阈值1.00001):触及速度上限,只有数值容差范围内超量,**不宣称存在速度安全裕量**。 -- warmup后总设备显存(含PhysX/Warp)初始/峰值均 **3,544,711,168 bytes**;增长 **0%**(阈值≤5%),包括最后一次采样。 -- 最终collect、validation、test、boundary、stability日志无 `[Error]/[Fatal]/buffer overflow`;环境关闭成功才提交passed。 - -这里的torque是仿真控制器applied torque,不是实测电机力矩;无实机安全、watchdog、承重、足底摩擦wrench或接触控制能力声明。 - -## 6. 冻结与独立加载 - -独立版本域:`skillfactory.skillbundle.v1` / `go2-fl-reach-leg-token-v1` / 1.0.0;**不冒充 SoftwareSpec v2通用adapter**。包内含actor、归一化buffer、完整token schema、静态token/物理绑定、关节顺序/PD/限位、版本依赖、示例输入、来源许可、选模/评估证据和每文件SHA256。 - -- 必需推理依赖:Python3.12 + Torch2.10.0及pip传递依赖;不需要NumPy、Isaac/Kit、项目导入、资产路径、数据集或训练checkpoint。 -- 全新CPU venv(Python3.12.3/Torch2.10.0+cpu),将包复制到独立临时工作目录,清除PYTHONPATH/Isaac变量,`python -I`启动;B=1/16/64/256,共400次重复调用通过。 -- eval + requires_grad=false + inference_mode;参数/所有buffer和调用者输入调用前后逐tensor不变。无optimizer,无统计更新路径,无内部递归状态。 -- 错hardware hash/part、字段/shape/dtype、有效槽NaN/Inf、mask/context、损坏权重均拒绝;padding NaN不影响输出,padding动作始终0。 -- CPU/CUDA相同输入最大动作差 **1.49e-7**(阈值1e-6),`device="cuda"`正确归一为cuda:0;GPU参数/buffer不变。 -- candidate全包manifest hash同时进入物理评估和独立进程报告;最终包再次独立加载,最终manifest hash单独核对。新增“同权重、不同包不能复用证据”回归。 -- 已存在发布目录拒绝覆盖;后续任何权重/统计/接口/元数据改动应发新版本。SHA256防意外损坏,不是数字签名;只加载可信来源代码。 - -最终权重 SHA256:`cdfe459887e7629924df7e6a79fd2900eff184e5b25bc2a26bb585378f671c4a` - -最终manifest SHA256:`3106158d2ab214b09360528fb3c994a713435f05f801a8bde5115ebd16079655` - -## 7. 工程测试、资源与边界 - -- 全仓单测 **132 passed, 3 skipped**;skip为保留的可选旧Kit集成测试,不声称重做历史600秒验收;15个外部Lab弃用警告。Ruff与`git diff --check`通过。 -- 两次独立只读代码审查共4项发现均修复并验证:耐久尾部内存采样、CUDA设备别名、全包证据身份、文档日志捕获。关键两项有失败回归。 -- RTX5080 16GiB单卡;第二轮采集46.85秒、梯度训练134.28秒、最终耐久143.58秒(均不以时长替代质量门槛)。从初始审计到交付约30分钟,低于90分钟预算;两个正式训练尝试,全部本地产物含独立CPU依赖环境约1.2GiB,低于10GB。 -- 模拟环境沿用本机Isaac Sim6.0.1路径 / Lab3.0配置,Lab core包6.1.17、Torch2.10.0+cu128;Python与源码hash见`provenance.json`。基线git `8019d6f`,交付工作区有未提交变更;训练源hash、配置hash、数据hash、资产hash及检查点hash均留存,不把dirty源码伪装成已提交版本。 -- 外部运行时限制:主仿真进程全部结束后,发现validation_030000遗留一个无GPU context的`carb.tasking6`孤立helper(约1.50GB CPU RSS),已按本次任务PID单独SIGTERM清理并确认退出,记录在`kit_helper_cleanup.json`。这不影响已完成的物理评估或纯Torch冻结推理,但**不宣称Kit批量启动/退出不存在资源残留**;长批次部署仍应使用进程组监督。只读发布目录再次独立加载验证也通过,报告与发布前逐字节一致。 -- 不覆盖:Go1/不同DoF泛化、ModSkill latent、接触承重/步态、移动基座、整机协调、噪声/动力学随机化、真实硬件。固定悬空结果不能外推到这些范围。 diff --git a/docs/stage2_attempt2.md b/docs/stage2_attempt2.md deleted file mode 100644 index cf784a2..0000000 --- a/docs/stage2_attempt2.md +++ /dev/null @@ -1,7 +0,0 @@ -# 第二次正式尝试(独立测试前登记) - -第一轮完整训练:1,024,000 转移/30,000更新;三个预定validation候选全部失败,不发布。平均误差10.45/11.09/11.90mm;成功率83.0/76.2/72.9%。固定数据中行为策略各噪声组末段均值13.1–14.8mm,说明原行为控制器不足以提供高质量稳态数据。独立test、boundary、stability尚未运行,测试种子未用于任何调参。 - -使用原定最多两次训练尝试中的第二次:`configs/training/go2_fl_offline_v1_attempt2.json`。范围、样本量、30k更新、IQL超参数、验证选模规则、全部验收门槛、独立test种子**不变**。仅行为收集器增加积分项:I += 8*dt*(q_goal-q)/scale,限幅±0.6,补偿静态重力偏差;噪声档改为[0,.03,.1,.25],收集seed12000,重新完整采集并封存数据。积分只属于数据收集器,不进入发布策略,也不在训练后用解析控制器替换学习策略。 - -失败第一轮完整数据、checkpoint、日志保留在logs/stage2/v1/;其训练源快照在training_source/(冻结loader后修复了cuda设备规范化,旧checkpoint如需严格恢复须用原快照)。第二轮位置logs/stage2/v1_attempt2/。不能因第一次未过而放宽门槛;第二轮仍不达标则报告未完成。 diff --git a/docs/stage2_plan.md b/docs/stage2_plan.md deleted file mode 100644 index 7ae862e..0000000 --- a/docs/stage2_plan.md +++ /dev/null @@ -1,27 +0,0 @@ -# 第二阶段实施与预注册验收 v1 - -本次第二阶段按用户定义:局部策略离线强化学习预训练与冻结交付,不把 Platform 宏观方案的阶段编号混为一谈。 - -## 审计 - -基线 `8019d6f`;开始时 README、train_leg_skill.py 有未提交修改,另有 AGENT.md/test_train_leg_skill.py,均保留。历史日志只有共享 PPO smoke,trained_skill=false。原采样每更新重启 Kit,缺少 optimizer/RNG 恢复、真正离线数据、选模/留出测试及独立冻结发布。v2 描述协议明确尚无通用 adapter,不擅自将旧 token 伪装成 v2。 - -## 范围与算法(训练前固定) - -唯一正式任务为真实 Go2 FL,固定支座悬空、无外载、无接触、无观测噪声,100 Hz 控制/500 Hz 物理。goal 是现有 anchor-frame 足端 link xyz/L,不提供目标关节角给策略。可达目标通过 FK(q_nominal + 0.25*a) 采样,a∈[-0.75,0.75]^3;每目标 2 秒,名义姿态复位,后 0.5 秒判定。不声称 latent 技能库、Go1 泛化、未见形态、步态、承重或真实硬件可用。 - -收集 20×256×200=1,024,000 条固定转移。行为控制器使用目标关节角 + 位置/速度反馈并混合四档噪声;这是有模型数据收集器,不是发布策略。数据生成后封存 hash;IQL 双 Q、expectile V、优势加权确定性 actor 只读取数据。actor 使用旧 token 中 joint_observation、foot position、goal 的 15 个数,静态绑定 Go2;15 维训练集归一化均值/标准差进入权重,推理不更新。数据末步保留 reset 前 next-state;2 秒为人工截断,继续任务 bootstrap,不把复位观测作为下一状态。 - -## 预注册阈值、选模及预算 - -机器配置唯一入口:`configs/training/go2_fl_offline_v1.json`。RTX 5080 单卡;1,024,000 转移,30,000 更新,batch512,最多两次训练尝试,总墙钟预算90分钟/磁盘10GB(不含已有仿真运行时)。预算是上限,不是终止质量门槛。数据20个独立 shard 可恢复,训练每5000步原子保存模型/target/optimizer/RNG;收到 SIGTERM 在当前更新完成后保存。sim 物理状态不声称 bitwise 可恢复;收集按 shard 确定种子重做,梯度训练验证 bitwise resume。 - -用独立 validation 种子21000的512个目标,比较10k/20k/30k权重,在达到所有 nominal gates 的候选中选末段平均误差最小者;无达标候选则不发布。选定后固定,仅一次独立 test:seed31000 的1024目标 + seed41000 的256边界目标(关节动作空间边/面/角)。报告以 episode 最后50步平均距离为统计单元,成功为该 episode 末段**最大**距离≤15mm;报告置信区间及零动作基线。 - -Nominal:平均≤8mm、episode均值P95≤15mm、成功率≥95%、相对同目标零动作基线误差降低≥60%。Boundary:平均≤12mm、P95≤20mm,同时硬安全约束通过。所有物理步控制观测:finite、无意外 termination、力矩≤厂家限值(1e-5相对容差)、速度≤厂家限值(1e-5)、关节位置不越硬限位(0.01rad容差)、支座pose漂移≤1e-4。另64实例×120s持续换目标耐久,warmup后设备显存增长≤5%,全部约束零违规。控制周期内5个物理子步的峰值需采样,不仅检查末步。 - -## 冻结与交付门槛 - -独立命名 SkillBundle v1(不是 SoftwareSpec v2),只声明现有 leg-token-v1 兼容。包内包含 actor/normalization、接口schema、静态token/物理绑定、资产来源/许可、版本依赖、评估证据、SHA256 manifest与不依赖项目/Kit的推理模块。依赖仅 Python3.12/Torch2.10(CPU 为必须验证平台);禁止覆盖已存在版本。推理 eval + inference_mode + requires_grad=false;无 optimizer、无训练/统计更新路径。参数和buffer调用前后hash一致。包复制到临时独立工作目录,清 PYTHONPATH、隔离进程仅从包加载,覆盖B=1/16/64、错误输入/错绑定/篡改拒绝及稳定推理;不需要训练数据/检查点/资产原路径。 - -代码/配置/测试/简洁文档入库工作区,完整运行产物位于 logs/stage2/,发布包位于 releases/。只有训练足额完成、独立评估门槛全部达标、独立加载及测试通过才可声明完成;否则保留可恢复产物与缺口报告。 diff --git a/docs/stage2_release_1_0_1.md b/docs/stage2_release_1_0_1.md deleted file mode 100644 index ff814c7..0000000 --- a/docs/stage2_release_1_0_1.md +++ /dev/null @@ -1,77 +0,0 @@ -# Go2 FL Reach 1.0.1 — 完整边界复验与发布 - -**2026-10-12:已完成真实物理复验并发布。** `logs/stage2/v1_0_1/acceptance.json`中12项检查全部通过,`milestone_complete=true`。该结论仅限固定悬空Go2 FL足端定点到达,不是接触承重、整机、跨形态或实机能力声明。 - -## 交付 - -- 只读冻结包:[`releases/go2_fl_reach-1.0.1/`](../releases/go2_fl_reach-1.0.1/) -- 确定性归档:[`go2_fl_reach-1.0.1.tar.gz`](../releases/go2_fl_reach-1.0.1.tar.gz),201,947 bytes -- 外部验收:[`go2_fl_reach-1.0.1.acceptance.json`](../releases/go2_fl_reach-1.0.1.acceptance.json) -- 最终包独立加载:[`go2_fl_reach-1.0.1.independent-load.json`](../releases/go2_fl_reach-1.0.1.independent-load.json) -- 校验入口:[`releases/SHA256SUMS`](../releases/SHA256SUMS) -- 运行证据、源码快照、进程监督记录:`logs/stage2/v1_0_1/` - -1.0.0包、归档及原有校验条目原样保留,作为覆盖不全的历史发布;新结果没有覆盖旧报告。 - -## 不变项与版本管理 - -复用第二轮的1,024,000条固定数据、完整30,000次训练及原validation-only选择的**20,000步actor**。没有新增正式训练尝试,没有根据边界或测试结果调参、降低阈值或重新选模。发布前再次核对原validation选择规则,结果仍为20k。 - -`actor.pt`、`selection.json`、`binding.json`、`frozen_policy.py`、`config.json`、`example_tokens.pt`与原candidate逐字节相同;沿用的训练/validation证据也按文件hash核对。actor哈希: - -`cdfe459887e7629924df7e6a79fd2900eff184e5b25bc2a26bb585378f671c4a` - -训练配置`configs/training/go2_fl_offline_v1_attempt2.json`完全未改。新增`--release-version`和包内`release.json`独立声明发布版本并绑定训练配置hash;`config.json.release_version=1.0.0`是历史训练配置中的默认值,**实际发布版本以manifest/release.json一致的1.0.1为准**。错误版本格式、配置身份不匹配或finalize时修改被测candidate版本会拒绝。 - -## 真实仿真结果 - -同一候选包manifest下重新执行boundary、test、stability,使用Isaac/PhysX真实环境而非单元测试fixture;每个模式独立启动headless Kit。由于版本元数据改变了包身份,三个模式均重跑,未替换旧报告的hash字段。 - -| 复验 | 样本及覆盖 | 均值 | episode均值P95 | 成功率 | -|---|---|---:|---:|---:| -| 完整边界,seed41000 | 256目标,6个面/12条边/8个角 | **2.308mm** | **3.286mm** | **100%** | -| 名义测试,seed31000–31003 | 1024目标 | **2.184mm** | **4.532mm** | **100%** | - -- 边界每种轴/符号类别9或10个目标,保存送入FK的实际动作并重新计算覆盖率;是全部26种边界**类别**覆盖,不是穷尽连续边界。 -- 边界门槛仍为均值≤12mm、P95≤20mm及原物理安全门槛;名义门槛仍为≤8mm/≤15mm、成功≥95%、相对零动作基线改善≥60%。成功要求episode最后50个控制步全部≤15mm。 -- 名义结果与旧权重评估相同,相对零动作基线误差下降95.881%。这次复用相同holdout种子,**不声称新增未见测试集**;边界采样协议改变,不能将新旧边界均值差当作训练收益。 -- 边界峰值归一化torque/velocity为0.406347/0.786603;所有本次物理评估硬限位超出、支座漂移、invalid states均为0,无意外termination/reset。 - -## 耐久与运行时 - -- 64环境×120秒,12,000控制步、768,000环境控制步;另200步warmup,合计61,000批物理子步快照。目标每2秒更换、不中途复位。 -- 显存初始3,525,246,976 bytes,峰值3,554,869,248 bytes,增长**0.8403%≤5%**,包含尾部采样。 -- 峰值normalized torque=0.663709、velocity=1.000000834≤1.00001。速度触及上限,**不宣称限速安全裕量**;torque是仿真applied torque,不是实测电机力矩。 -- 三个模式主进程退出及日志扫描均通过。监督器只收养、终止并回收本任务遗留的Hub/telemetry helper,三个`*_process.json`均`cleanup.clean=true`;不是对Kit原生退出无残留的保证。 -- 边界/名义/耐久含启动与清理墙钟约21.66/36.05/182.33秒。仍为单GPU运行,没有并行Kit任务。 - -## 恢复、工程与冻结加载 - -- 两个新的独立GPU进程从真实5k检查点恢复到5001,**112个tensor**及非tensor结构、optimizer、CPU/CUDA RNG逐项一致。只用于恢复复验,不将这些probe作为训练完成或重新选模证据。 -- 全仓pytest **172 passed, 3 skipped**,Ruff、`git diff --check`通过。3个skip仍是历史可选Kit集成测试,不声称重做旧600秒验收。新增发布版本与训练身份解耦的回归。 -- candidate、暂存最终包、只读正式包均通过独立CPU解释器`python -I`包-only加载;B=1/16/64/256的确定性调用、输入/参数/buffer不变、错误绑定与损坏拒绝通过。无需Isaac、训练文件、项目导入或资产路径。 -- 新CPU/CUDA parity最大动作差**4.1723e-7<1e-6**;400次GPU重复调用确定性,参数/buffer不变,`cuda`别名解析为cuda:0。 -- 工程/恢复/物理/独立加载等语义门槛通过后,暂存最终包再次独立加载、统一总验收通过,才原子发布。正式目录随后设为只读并再次独立加载、总验收。 - -## 完整性与复核 - -最终manifest SHA256: -`b685088e675fc624744a239644ce59766efab6bbb8f4790b4783c16f0ed100fc` - -归档 SHA256: -`90168f81e33abbe1f5ac49a71cc9447b61dea6db9bbb886878d15134695c9dc7` - -```bash -unset CONDA_PREFIX VIRTUAL_ENV -export PYTHONPATH="$PWD/source/SkillFactory" -/home/cen/IsaacLab/isaaclab.sh -p scripts/verify_stage2_delivery.py \ - --config configs/training/go2_fl_offline_v1_attempt2.json \ - --run logs/stage2/v1_0_1 --release releases/go2_fl_reach-1.0.1 \ - --output logs/stage2/recheck_1_0_1/acceptance.json -python3 scripts/verify_frozen_leg.py --package releases/go2_fl_reach-1.0.1 \ - --python logs/stage2/inference_venv/bin/python \ - --output logs/stage2/recheck_1_0_1/standalone.json -(cd releases && sha256sum -c SHA256SUMS) -``` - -归档和SHA256用于完整性而非数字签名;只运行可信包代码。工作区仍为带有既有修改的dirty状态,未创建git提交,来源与hash见新run的`provenance.json`。推理延迟测量/性能优化没有包含在本次任务中。 diff --git a/docs/stage2_reliability_fix.md b/docs/stage2_reliability_fix.md deleted file mode 100644 index 29f2c2b..0000000 --- a/docs/stage2_reliability_fix.md +++ /dev/null @@ -1,42 +0,0 @@ -# 第二阶段发布可靠性修复 - -## 状态 - -**后续已完成[1.0.1完整物理复验及发布](stage2_release_1_0_1.md),沿用原权重且未重训。** 以下保留修复阶段的历史记录:当时三项问题已修复并通过失败路径回归,但尚未物理复验/发布;1.0.0文件与原校验条目始终未改。新版本的证据单独位于`logs/stage2/v1_0_1/`,SHA256SUMS仅追加新版本条目。 - -## 修复 - -1. **P1 发布门槛遗漏** - - 新增`scripts/stage2_acceptance.py`,发布器和总验收器共用数据、训练、选模、恢复、工程、物理、独立加载检查;不再各维护一套不同门槛。 - - 工程报告及完整learner/optimizer/RNG恢复报告必须存在、解析成功且通过。检查工程日志hash,重算episode统计,校验物理时长/样本数、边界覆盖及整个被测包的manifest身份。 - - 先验证全部语义门槛,再在同文件系统的临时目录构建最终包;使用必需的`--python`独立验证**最终manifest字节**,再复用总验收逻辑检查,最后原子rename公开。失败不留下公开accepted目录;同版本禁止覆盖。 - - 新版总验收亦先写running,缺失/损坏证据会覆盖旧成功报告为failed;可用`--output`保存历史报告。 -2. **P2 边界覆盖不全** - - 新增`scripts/stage2_boundary.py`:枚举三轴的全部26种非零边界签名,6个面内点类别、12条边内点类别、8个角。 - - 循环分配每个轴/符号组合,非固定坐标严格位于内部,固定seed可复现;256环境下每种组合9或10个目标。少于26环境直接拒绝,不静默缩小覆盖范围。 - - 从实际送往FK的动作记录`boundary_actions`和`boundary_coverage`,不是只记录预期采样计划。评估和发布都要求覆盖完整;旧协议证据拒绝通过。 - - 这里的“完整”指全部边界**类别**均被采样,不声称穷尽连续边界上的所有点。 -3. **P2 失败复验残留旧成功报告** - - `verify_frozen_leg.py`启动立即原子写`passed=false,status=running`和新run_id。 - - 复制失败、解释器缺失、子进程非零、畸形/否定输出、可捕获中断等写failed并重新抛错,保留子进程returncode/stdout/stderr。突然kill保留的running同样不能通过门槛。 - - 只有校验成功、输入包身份未变化、临时目录清理完成才写completed。隔离子进程仍只依赖stdlib/Torch,不需要新验收模块。 - -## 修复阶段验证(发布复验之前) - -- 全仓pytest:**167 passed, 3 skipped**(新增35项,历史可选Kit集成仍skip);Ruff、`git diff --check`通过。 -- 回归包含:工程/恢复分别失败及同时失败、缺失/畸形JSON;旧边界证据、缺日志、物理步数不足、不同manifest复用相同权重;暂存复制/加载失败不发布;全部门槛满足后的原子发布及拒绝覆盖;3种batch ×3个seed的全部轴/符号覆盖;旧成功被失败复验覆盖、成功路径及包变化拒绝。 -- 发布事务单元测试中的物理数据是显式合成fixture,最终独立加载调用在该单元测试中mock;**这些不是新物理验收**。 -- 另用真实独立CPU解释器验证原只读1.0.0包:通过。再在临时副本注入权重损坏,对预先填入成功的报告复验:实际子进程失败、CLI退出1,报告变为`passed=false,status=failed`。 -- `releases/SHA256SUMS`全部匹配,训练源码身份绑定的三个文件未改动。 - -证据:`logs/stage2/reliability_fix/{pytest.log,ruff.log,readonly_standalone.json,failed_recheck.json,historical_delivery_recheck.json}`。 - -## 历史产物的处理与原待办 - -新版检查对旧1.0.0报告预期返回非零: - -- 训练、恢复、选模、数据、名义测试、耐久、包完整性仍通过已有证据核对。 -- **边界不通过**:旧报告实际为2/6个面、4/12条边、8/8个角,没有新协议的完整采样证据。 -- 历史两份独立加载报告没有新版`status=completed`生命周期字段,不能直接复用;本次真实CPU复验结果单独保留,未篡改历史报告。 - -上述原待办已在1.0.1完成:不改变权重/阈值、不利用测试选模,重新执行完整边界、名义测试、耐久、恢复一致性、工程及独立加载,原子发布新版本。不要修改旧1.0.0内容或仅手工补字段让旧证据“通过”。推理延迟测量和基于瓶颈的性能优化尚未进行。 diff --git a/docs/stage2_usage.md b/docs/stage2_usage.md deleted file mode 100644 index 91d6e20..0000000 --- a/docs/stage2_usage.md +++ /dev/null @@ -1,100 +0,0 @@ -# 第二阶段:离线单腿 Reach 与冻结调用 - -范围、预算、预注册门槛:[stage2_plan.md](stage2_plan.md)。第二次尝试理由:[stage2_attempt2.md](stage2_attempt2.md)。与通用描述规范的关系:继续复用现有 leg-token-v1,不声称已实现 SoftwareSpec v2 通用 adapter。 - -## 重现训练 - -先按项目 README 构建 `assets/generated/legs/go2_fl`。Isaac Sim 6.0.1 / Lab 3.0.0、Python3.12、Torch2.10 CUDA;收集和评估需要 Kit,梯度训练不需要 Kit。 - -```bash -unset CONDA_PREFIX VIRTUAL_ENV -export PYTHONPATH="$PWD/source/SkillFactory" -export PYTHONUNBUFFERED=1 -LAB=/home/cen/IsaacLab/isaaclab.sh -CFG=configs/training/go2_fl_offline_v1_attempt2.json -RUN=logs/stage2/reproduction # 使用新目录;不要覆盖交付证据 -VERSION=1.0.2 # 重现示例:选择尚未占用的版本;当前已交付1.0.1 -mkdir -p "$RUN" -set -o pipefail - -"$LAB" -p scripts/stage2_sim.py --config "$CFG" --mode collect --output "$RUN/data" --device cuda:0 2>&1 | tee "$RUN/collect.log" -"$LAB" -p scripts/train_offline_leg.py --config "$CFG" --data "$RUN/data" --output "$RUN/train" 2>&1 | tee "$RUN/train.log" -``` - -数据收集20个确定性种子shard;中断后重跑收集命令只重新生成未完成shard。固定数据封存后训练绝不调用仿真器。训练每5000更新保存actor/Q/V/target/optimizer/RNG;SIGINT/SIGTERM在当前更新后原子落盘。磁盘/硬崩溃可退回最近一次完整checkpoint。恢复必须使用相同config、dataset及训练源hash: - -```bash -"$LAB" -p scripts/train_offline_leg.py --config "$CFG" --data "$RUN/data" \ - --output "$RUN/train" --resume "$RUN/train/latest.pt" -``` - -`--stop-after 5000` 是中断演练,不是训练完成。完整必须达到30000更新且训练result.training_complete=true;它仍不等于任务验收通过。所有JSON报告以passed=false开始,shutdown失败不得标成功。 - -## 选模、独立评估与发布 - -**发布可靠性修订:** 历史1.0.0的边界报告只有2/6个面、4/12条边、8/8个角,不能作为新门槛的完整覆盖证据。修订后的采样按26种轴/符号组合循环,每批至少26环境,并保存实际动作及覆盖计数。原训练/权重未更改;完整边界、名义测试、耐久等已重新执行并发布1.0.1,见[发布报告](stage2_release_1_0_1.md)。旧版问题见[修复记录](stage2_reliability_fix.md)。 - -```bash -for STEP in 010000 020000 030000; do - "$LAB" -p scripts/stage2_sim.py --config "$CFG" --mode validation \ - --weights "$RUN/train/step_$STEP.pt" --output "$RUN/validation_$STEP" --device cuda:0 2>&1 | tee "$RUN/validation_$STEP.log" - # 某候选未达标退出非零,可记录后继续其它预定候选;运行异常必须检查日志。 -done -"$LAB" -p scripts/export_leg_bundle.py --config "$CFG" --run "$RUN" --output "$RUN/candidate" \ - --release-version "$VERSION" -for MODE in test boundary stability; do - "$LAB" -p scripts/stage2_sim.py --config "$CFG" --mode "$MODE" \ - --package "$RUN/candidate" --output "$RUN/$MODE" --device cuda:0 2>&1 | tee "$RUN/$MODE.log" -done -``` - -导出先检查完整训练与三份validation证据,并仅在通过门槛者中按平均误差选模。未达标不导出。candidate已经冻结,但不是accepted release。test种子不能用于调参或选模;任一独立门槛不通过,停止发布。每步检查退出码、result与日志的`[Error]/[Fatal]/buffer overflow`,不能只看奖励。 - -独立CPU验证(在普通Python新venv中,不用Kit): - -```bash -python3 -m venv /tmp/skill-inference -/tmp/skill-inference/bin/python -m pip install torch==2.10.0 --index-url https://download.pytorch.org/whl/cpu -python3 scripts/verify_frozen_leg.py --package "$RUN/candidate" \ - --python /tmp/skill-inference/bin/python --output "$RUN/standalone.json" -# 发布前必须已有本次真实工程测试、恢复一致性证据,不能复制历史passed标志: -# $RUN/engineering_checks.json、$RUN/resume_exact.json(字段要求见下文)。 -# candidate的release.json/manifest版本已固定;不能在finalize时改版本或复用旧包测试hash。 -"$LAB" -p scripts/export_leg_bundle.py --config "$CFG" --run "$RUN" --candidate "$RUN/candidate" \ - --python /tmp/skill-inference/bin/python --output "releases/go2_fl_reach-$VERSION" -python3 scripts/verify_frozen_leg.py --package "releases/go2_fl_reach-$VERSION" \ - --python /tmp/skill-inference/bin/python --output "$RUN/release_standalone.json" -``` - -`--release-version`只在candidate导出时定义发布元数据,不改变已hash锁定的训练配置。包内`release.json`绑定训练配置hash并与manifest版本一致;旧配置的`release_version`保留为历史默认值,不能通过修改它绕过训练身份锁定。省略参数时保持旧配置默认版本。仅换版本也改变candidate manifest,因此所有包级物理/独立加载证据必须对应新包。 - -发布器与交付核验器共享`stage2_acceptance.py`:数据/训练/选模、工程测试、恢复一致性、物理指标及覆盖、独立加载全部通过后,才在同文件系统临时目录构建最终包,并用`--python`再次独立验证最终manifest对应的字节;通过后原子rename为公开accepted目录。中途失败会清理临时包,不会遗留公开accepted目录。 - -工程报告必须`passed=true`、`git_diff_check=true`、`review_blockers_remaining=0`,并有与实际`pytest.log`/`ruff.log`匹配的`pytest_log_sha256`/`ruff_log_sha256`。恢复报告必须`passed=true`且`full_learner_optimizer_rng_exact=true`,均为强制门槛,缺失/损坏/失败均拒绝发布。 - -验证工具启动即原子写`passed=false,status=running`;异常(复制、解释器/子进程失败、畸形输出、中断)原子写`passed=false,status=failed`并非零退出,不复用旧成功报告。成功状态为`completed`,附独立run_id。突然kill留下的running也不能通过发布门槛。 - -验证工具把**仅包和验证脚本**复制到全新临时工作目录,清掉Python/Isaac环境变量,以`python -I`启动;检查B=1/16/64/256,400次确定性调用、参数/buffer/输入不变、错输入/绑定/篡改拒绝。后5槽始终0。模拟评估与独立加载都绑定**整个candidate manifest hash**,不能只凭同一actor复用不同包的证据。发布目录拒绝覆盖;后续任何修改必须新版本。 - -重新核验最新1.0.1时使用新报告路径保留交付证据,应全部通过。若改为历史1.0.0与`v1_attempt2`运行目录,则因旧边界覆盖及旧加载报告生命周期字段缺失而返回非零;不要篡改历史报告。全部版本文件SHA256均应匹配: - -```bash -"$LAB" -p scripts/verify_stage2_delivery.py \ - --config configs/training/go2_fl_offline_v1_attempt2.json \ - --run logs/stage2/v1_0_1 --release releases/go2_fl_reach-1.0.1 \ - --output logs/stage2/recheck_1_0_1/acceptance.json -(cd releases && sha256sum -c SHA256SUMS) -``` - -发布器和最终核验器都会读取`resume_exact.json`与`engineering_checks.json`;历史交付已包含真实恢复/测试证据,但不能据此替代新代码的复验。新训练不能复用这些结果,须重做恢复一致性、pytest、Ruff并记录自己的报告。 - -实际推理示例见包内README;输入实时token,`act`返回归一化PD目标偏移,`position_targets`返回软限位后的声明顺序rad目标。接收方必须提供自己的资产manifest hash,不应盲信包内示例。无物理仿真依赖,无训练临时文件/optimizer;Torch可发出缺少可选NumPy的警告,但推理不使用NumPy。 - -## 测试与证据 - -```bash -PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 "$LAB" -p -m pytest tests -q -"$LAB" -p -m ruff check source/SkillFactory scripts tests -``` - -`tests/test_offline_leg.py`覆盖IQL更新、连续vs恢复完全一致、非有限loss、冻结状态、padding、hash拒绝及耐久尾部内存增长回归。`tests/test_stage2_release.py`覆盖强制门槛失败/缺失/损坏、原子发布及最终包加载失败、全部26种边界组合覆盖、旧成功报告被失败复验覆盖等路径。测试之外保留真实大数据训练恢复和完整独立仿真评估证据。历史第一周仿真集成三项默认skip,不声称此次重做旧600秒验收。 diff --git a/scripts/adopt_cycle_sim_p1_evidence.py b/scripts/adopt_cycle_sim_p1_evidence.py new file mode 100644 index 0000000..9f62935 --- /dev/null +++ b/scripts/adopt_cycle_sim_p1_evidence.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +"""CPU-only, explicitly reviewed external P1 adoption; no grant, reservation or Kit. + +Writes only an allowlisted P2 root's p1-adoption.json, exclusively and read-only. +The parent must independently review its digest before any extended P2 grant. +""" +import argparse +import importlib.util +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--parent-review', required=True) + parser.add_argument('--trusted-source-budget-sha256', required=True) + parser.add_argument('--artifact-root', type=Path, default=Path('/home/cen/sf-runs/p2-01'), + choices=[Path('/home/cen/sf-runs/p2-01'), Path('/home/cen/sf-runs/p2-02')]) + args = parser.parse_args(argv) + spec = importlib.util.spec_from_file_location('_adoption_runner', ROOT / 'scripts/evaluate_cycle_sim_residual.py') + runner = importlib.util.module_from_spec(spec) + spec.loader.exec_module(runner) + adoption = runner.cpu_module('evaluation_adoption') + print(adoption.create_adoption(parent_review=args.parent_review, + trusted_source_budget_sha256=args.trusted_source_budget_sha256, + target_root=args.artifact_root)) + return 0 + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/scripts/audit_contact_cycle.py b/scripts/audit_contact_cycle.py new file mode 100644 index 0000000..78b638c --- /dev/null +++ b/scripts/audit_contact_cycle.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +"""Offline CPU trace readback. Audit consistency is NOT skill acceptance.""" + +import argparse +import hashlib +import json +from pathlib import Path + +import torch +from runtime_common import write_json +from SkillFactory.legs.contact_cycle.metrics import summarize + + +def audit(run): + run = Path(run) + report = json.loads((run/"result.json").read_text()) + cfg = json.loads((run/"config.json").read_text()) + locked = json.loads((run/"invocation.json").read_text())["thresholds"]["gates"] + trace = torch.load(run/"physics_trace.pt", map_location="cpu", weights_only=True) + rows = summarize(trace, report["physics_dt"], locked, cfg["scenarios"]) + samples = round(sum(cfg["phase_durations_s"])/report["physics_dt"]) + gates = { + "not_skill_acceptance": report["accepted_skill"] is False and report["trained_skill"] is False, + "same_rows_from_raw_trace": rows == report["rows"], + "summary_status_consistent": ( + report["passed"] == all(report["gates"].values()) + and report["gates"]["all_scenario_metrics"] == all(r["success"] for r in rows) + and report["status"] == ("completed" if report["passed"] else "failed") + ), + "complete_duration": len(trace["time"]) == samples == report["physics_steps"], + "monotonic_physics_clock": bool(torch.allclose(trace["time"][1:]-trace["time"][:-1], + torch.full_like(trace["time"][1:], report["physics_dt"]), + atol=1e-6, rtol=0)), + } + identity = json.loads((run/"identity.json").read_text()) + snapshot_files = list((run/"source_snapshot").rglob("*")) + hashes = {hashlib.sha256(p.read_bytes()).hexdigest() for p in snapshot_files if p.is_file()} + gates["bound_source_snapshots"] = all(digest in hashes for digest in identity.values()) + # Independent scalar loop for the slip integral (does not call summarize's implementation). + slip = [] + for e in range(trace["phase"].shape[1]): + total = 0.0 + for k in range(len(trace["time"])): + phase, progress = int(trace["phase"][k, e]), float(trace["progress"][k, e]) + stance = (phase == 0 and float(trace["time"][k, e]) >= 2) or (phase == 5 and progress >= 0.8) + if stance and float(trace["normal"][k, e, 2]) > 2: + total += float(trace["slip_speed"][k, e]) * report["physics_dt"] + slip.append(total) + gates["independent_slip_integral"] = all( + not r["complete"] or not r["finite"] or abs(slip[i]-r["stance_slip_m"]) <= 1e-5 + for i, r in enumerate(rows)) + failures = [] + for row in rows: + if not row["success"]: + e = row["env"] + indices = torch.nonzero(trace["would_fail"][:, e]).flatten() + failures.append({"env": e, "scenario": row["scenario"], "reasons": row["failure_reasons"], + "first_task_failure_s": float(trace["time"][indices[0], e]) if len(indices) else None, + "peak_height_error_s": float(trace["time"][trace["height_error"][:, e].argmax(), e])}) + return {"passed": all(gates.values()), "accepted_skill": False, "audit_only": True, + "original_physical_passed": report["passed"], "gates": gates, "failure_cases": failures, + "trace_sha256": hashlib.sha256((run/"physics_trace.pt").read_bytes()).hexdigest(), + "report_sha256": hashlib.sha256((run/"result.json").read_bytes()).hexdigest()} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--run", type=Path, required=True) + parser.add_argument("--output", type=Path, required=True) + args = parser.parse_args() + if args.output.exists(): + parser.error("Refusing to overwrite audit evidence") + result = audit(args.run) + write_json(args.output, result) + raise SystemExit(0 if result["passed"] else 2) + + +if __name__ == "__main__": + main() diff --git a/scripts/audit_contact_cycle_functional.py b/scripts/audit_contact_cycle_functional.py new file mode 100644 index 0000000..26eece1 --- /dev/null +++ b/scripts/audit_contact_cycle_functional.py @@ -0,0 +1,158 @@ +#!/usr/bin/env python3 +"""Offline, nominal simulation-function check. Never runs Kit, a policy or an optimizer.""" +import argparse +import hashlib +import json +import math +from pathlib import Path + +import torch + +ROOT = Path(__file__).resolve().parents[1] +DEFAULT_CONFIG = ROOT / 'configs/contact_cycle/simulation_functional_v1.json' + + +def longest_run(mask): + longest = current = 0 + for value in mask.tolist(): + current = current + 1 if value else 0 + longest = max(longest, current) + return longest + + +def evaluate(trace, cfg, controller_kind): + """One actual first episode, [T,1,...]. Early stops/resets cannot be repaired by relabeling.""" + if controller_kind not in ('analytic_reference', 'learned_policy'): + raise ValueError('Declare the controller evidence type') + if (cfg['format'] != 'skillfactory.contact-cycle-simulation-functional.v1' + or len(cfg['phase_durations_s']) != 6 + or any(type(x) not in (int, float) or not math.isfinite(x) or x <= 0 + for x in [cfg['physics_dt_s'], *cfg['phase_durations_s'], *cfg['gates'].values()]) + or type(cfg['gates']['minimum_complete_cycles']) is not int): + raise ValueError('Invalid functional contract') + required = {'time', 'phase', 'scenario', 'weight', 'normal', 'foot', 'target', 'gap', + 'signed_height_error', 'tilt', 'external', 'guide', 'auxiliary', 'joint_excess', + 'limit_ratio', 'contact_acquired', 'actor_metadata_available', 'control_action_available'} + out = {'controller_kind': controller_kind, 'sim_functional_passed': False, + 'accepted_skill': False, 'gates': {}, 'metrics': {}} + if not required <= trace.keys(): + out['missing_fields'] = sorted(required - trace.keys()) + return out + n = len(trace['time']) + if not n or any(v.shape[:2] != (n, 1) for v in trace.values()): + out['error'] = 'Require nonempty single-episode [T,1,...] arrays' + return out + gates, metrics = out['gates'], out['metrics'] + gates['finite'] = all(bool(torch.isfinite(v).all()) for v in trace.values()) + if not gates['finite']: + return out + d = {k: v[:, 0] for k, v in trace.items()} + t, dt, g = d['time'], cfg['physics_dt_s'], cfg['gates'] + duration = sum(cfg['phase_durations_s']) + expected = torch.arange(1, round(duration / dt) + 1, dtype=t.dtype, device=t.device) * dt + gates['complete_clock_without_reset'] = n == len(expected) and bool(torch.allclose(t, expected, atol=1e-5, rtol=0)) + gates['nominal_only'] = bool((d['scenario'] == cfg['nominal_scenario_id']).all()) + gates['no_external_disturbance'] = bool((d['external'].abs() <= 1e-6).all()) + gates['actions_recorded'] = bool(d['control_action_available'].all()) + gates['controller_metadata'] = (bool(d['actor_metadata_available'].all()) if controller_kind == 'learned_policy' + else not bool(d['actor_metadata_available'].any())) + if not gates['complete_clock_without_reset']: + metrics['actual_end_s'] = float(t[-1]) + return out + ends = t.new_tensor(cfg['phase_durations_s']).cumsum(0) + phase = torch.bucketize(t.contiguous(), ends[:-1], right=True) + gates['six_command_phases'] = bool(torch.equal(d['phase'], phase)) and set(phase.tolist()) == set(range(6)) + if not gates['six_command_phases']: + return out + normal, weight = d['normal'][:, 2], d['weight'] + gates['positive_weight'] = bool((weight > 0).all()) + if not gates['positive_weight']: + return out + windows = {'support': (phase == 0) & (t >= ends[0] - g['support_final_window_s']), + 'unload': (phase == 1) & (t >= ends[1] - g['unload_final_window_s']), + 'reload': (phase == 5) & (t >= duration - g['reload_final_window_s'])} + for name, mask in windows.items(): + contact = float((normal[mask] > g['contact_force_on_N']).float().mean()) + load = float((normal[mask] / weight[mask]).mean()) + metrics[name + '_contact_fraction'] = contact + metrics[name + '_load_fraction'] = load + if name != 'unload': + gates[name + '_ground_support'] = (contact >= g['support_reload_contact_fraction_min'] + and load >= g['support_reload_load_fraction_min']) + else: + metrics['unload_remaining_support_fraction'] = load / max(metrics['support_load_fraction'], 1e-6) + gates['unloaded'] = (metrics['unload_remaining_support_fraction'] + <= g['unload_remaining_support_fraction_max']) + air = (d['gap'] >= g['swing_gap_m_min']) & (normal <= g['contact_force_on_N']) + swing = phase == 3 + metrics['swing_air_fraction'] = float(air[swing].float().mean()) + metrics['swing_contiguous_air_s'] = longest_run(air[swing]) * dt + gates['real_flight'] = (metrics['swing_air_fraction'] >= g['swing_air_fraction_min'] + and metrics['swing_contiguous_air_s'] >= g['swing_contiguous_air_s_min']) + pre, post = (phase == 2).nonzero()[-1, 0], swing.nonzero()[-1, 0] + requested = d['target'][post, :2] - d['target'][pre, :2] + actual = d['foot'][post, :2] - d['foot'][pre, :2] + gates['three_dimensional_command'] = bool((requested.abs() > .001).all()) + progress = actual * requested.sign() / requested.abs().clamp_min(1e-6) + metrics['swing_xy_progress_fraction'] = progress.tolist() + gates['xy_swing_motion'] = bool((progress >= g['swing_xy_progress_fraction_min']).all()) + landing_error = (d['foot'][windows['reload']] - d['target'][windows['reload']]).norm(dim=-1) + metrics['landing_error_m'] = float(landing_error.mean()) + gates['landing_position'] = metrics['landing_error_m'] <= g['landing_position_error_m_max'] + gates['contact_acquired'] = bool(d['contact_acquired'].any()) + gates['no_gross_collapse'] = (float(d['signed_height_error'].abs().max()) <= g['root_height_error_m_max'] + and float(d['tilt'].max()) <= g['root_tilt_rad_max']) + gates['unilateral_contact'] = float(normal.min()) >= -g['normal_force_negative_tolerance_N'] + gates['no_gross_penetration'] = float(d['gap'].min()) >= -g['ground_penetration_m_max'] + gates['actuator_joint_bounds'] = (float(d['limit_ratio'].max()) <= g['normalized_effort_velocity_max'] + and float(d['joint_excess'].max()) <= g['joint_limit_excess_rad_max']) + auxiliary = d['auxiliary'] + # A point-applied vertical force can have COM-reduced x/y moments, but no free couple. + # The declared payload COM offset has norm .06m; do not blanket-zero valid r x F. + moment_bound = auxiliary[..., 2].clamp_min(0) * g['auxiliary_lever_arm_m_max'] + 1e-6 + gates['declared_fixture'] = (float(d['guide'][..., 2].abs().max()) <= g['guide_vertical_N_max'] + and float(auxiliary[..., :2].abs().max()) <= 1e-6 + and float(auxiliary[..., 2].min()) >= -1e-6 + and float(auxiliary[..., 2].sum(-1).max()) <= g['auxiliary_vertical_N_max'] + and float(auxiliary[..., 5].abs().max()) <= 1e-6 + and bool((auxiliary[..., 3:].norm(dim=-1) <= moment_bound).all()) + and float(auxiliary[phase == 0].abs().max()) <= 1e-6) + out['sim_functional_passed'] = all(gates.values()) + return out + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--trace', type=Path, required=True) + parser.add_argument('--config', type=Path, default=DEFAULT_CONFIG) + parser.add_argument('--controller-kind', choices=['analytic_reference', 'learned_policy'], required=True) + parser.add_argument('--output', type=Path, required=True) + args = parser.parse_args() + if args.output.exists(): + raise FileExistsError('Never overwrite an earlier audit') + cfg = json.loads(args.config.read_text()) + raw = torch.load(args.trace, map_location='cpu', weights_only=True) + if 'time' in raw: + traces = {str(e): {k: v[:, e:e+1] for k, v in raw.items()} + for e in range(raw['time'].shape[1])} + else: + traces = raw # saved validation subsets keyed by environment + rows = {e: evaluate(d, cfg, args.controller_kind) for e, d in traces.items()} + passed = sum(r['sim_functional_passed'] for r in rows.values()) + result = {'format': cfg['format'], 'controller_kind': args.controller_kind, + 'sim_functional_passed': passed >= cfg['gates']['minimum_complete_cycles'], + 'trace_path': str(args.trace), 'trace_sha256': hashlib.sha256(args.trace.read_bytes()).hexdigest(), + 'config_sha256': hashlib.sha256(args.config.read_bytes()).hexdigest(), + 'audit_source_sha256': hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), 'rows': rows, + 'functional_cycles_passed': passed, + 'accepted_skill': False, 'new_simulation_steps': 0, 'new_optimizer_steps': 0, + 'note': 'Retrospective development replay. Non-nominal rows are outside this scope; ' + 'no learned claim from analytic control.'} + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text(json.dumps(result, indent=2, allow_nan=False) + '\n') + print(json.dumps({k: v for k, v in result.items() if k != 'rows'}, indent=2)) + raise SystemExit(0 if result['sim_functional_passed'] else 1) + + +if __name__ == '__main__': + main() diff --git a/scripts/audit_cycle_sim_evaluation.py b/scripts/audit_cycle_sim_evaluation.py new file mode 100644 index 0000000..9dcf207 --- /dev/null +++ b/scripts/audit_cycle_sim_evaluation.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python3 +"""CPU-only JSONL evidence re-audit; no policy, physics, registration or optimizer.""" +import argparse +import importlib.util +import json +import lzma +import sys +import time +import types +import zlib +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +def cpu_modules(): + # Private offline namespace, NOT SkillFactory and never used for frozen identity verification. + directory = ROOT / 'source/SkillFactory/SkillFactory/legs/contact_cycle_sim' + name = '_contact_cycle_offline_cpu' + package = types.ModuleType(name) + package.__path__ = [str(directory)] + sys.modules[name] = package + modules = [] + for leaf in ('evaluation_protocol', 'evaluation_audit'): + spec = importlib.util.spec_from_file_location(name + '.' + leaf, directory / (leaf + '.py')) + module = importlib.util.module_from_spec(spec) + sys.modules[spec.name] = module + spec.loader.exec_module(module) + modules.append(module) + return modules + + +def trace_lines(path, *, compressed, max_bytes, max_record_bytes, deadline, max_decoded_bytes=None): + """Incremental legacy gzip / v2 XZ / raw JSONL, never whole-stream decompression. + + XZ decoder memory is capped, including dictionaries advertised in headers. + Input, output, individual records and per-member output are independently + bounded; deadlines are checked even while draining buffered decoder output. + """ + _, limits = cpu_modules() + chunk_bytes = 64 * 1024 + max_member_bytes = limits.MAX_CHUNK_RAW_BYTES + max_decoded_bytes = max_bytes * 64 if max_decoded_bytes is None else max_decoded_bytes + xz = compressed and path.name.endswith('.xz') + read_bytes = decoded_bytes = member_bytes = member_records = 0 + line = bytearray() + + def check(): + if time.monotonic() > deadline or path.stat().st_size > max_bytes or read_bytes > max_bytes: + raise ValueError('budget_audit_resource_or_record_limit') + + decoder = None + with path.open('rb') as source: + while True: + check() + data = source.read(min(chunk_bytes, max_bytes - read_bytes + 1)) + read_bytes += len(data) + check() # Includes EOF and time spent reading compressed headers/tails. + if not data: + if decoder is not None: + raise EOFError('truncated compressed member') + if line: + raise ValueError('budget_audit_resource_or_record_limit') + return + # XZ may retain input internally. Drain it with bounded output before + # reading more source, including when the source is already at EOF. + while data or (xz and decoder is not None and not decoder.needs_input): + check() + member_done = False + if compressed: + if decoder is None: + decoder = (lzma.LZMADecompressor(format=lzma.FORMAT_XZ, + memlimit=limits.XZ_DECODER_MEMORY_BYTES) + if xz else zlib.decompressobj(wbits=31)) + member_bytes = member_records = 0 + decoded = decoder.decompress(data, chunk_bytes) + member_done = decoder.eof + if xz: + remaining = decoder.unused_data if member_done else b'' + else: + remaining = decoder.unused_data if member_done else decoder.unconsumed_tail + if not decoded and len(remaining) == len(data): + raise ValueError('gzip decoder made no progress') + data = remaining + else: + decoded, data = data, b'' + check() + decoded_bytes += len(decoded) + member_bytes += len(decoded) + if decoded_bytes > max_decoded_bytes or compressed and member_bytes > max_member_bytes: + raise ValueError('budget_audit_decoded_limit') + # At most one bounded record plus a 64KiB decoded block is held. + for part in decoded.splitlines(keepends=True): + line.extend(part) + if len(line) > max_record_bytes: + raise ValueError('budget_audit_resource_or_record_limit') + if line.endswith(b'\n'): + member_records += 1 + if xz and member_records > limits.MAX_CHUNK_RECORDS: + raise ValueError('budget_audit_chunk_record_limit') + check() + yield bytes(line) + line.clear() + if member_done: + if xz and line: + raise ValueError('incomplete record at compressed member boundary') + decoder = None + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--protocol', type=Path, required=True) + parser.add_argument('--trial-index', type=int, required=True) + parser.add_argument('--trace', type=Path, required=True) + parser.add_argument('--output', type=Path, required=True) + parser.add_argument('--max-trace-bytes', type=int, required=True) + parser.add_argument('--wall-seconds', type=float, required=True) + args = parser.parse_args() + protocol_module, audit_module = cpu_modules() + protocol = protocol_module.load_protocol(args.protocol) + if not 0 < args.max_trace_bytes <= protocol.document['resources']['max_artifact_bytes']: + parser.error('Require bounded positive trace budget within protocol cap') + if not 0 < args.wall_seconds <= 3600: + parser.error('Require bounded positive CPU audit wall budget') + audit = audit_module.TrialAudit(protocol, args.trial_index) + deadline = time.monotonic() + args.wall_seconds + count = 0 + try: + if args.trace.stat().st_size > args.max_trace_bytes: + raise ValueError('budget_audit_compressed_bytes') + for line in trace_lines(args.trace, compressed=args.trace.name.endswith(('.jsonl.gz', '.jsonl.xz')), + max_bytes=args.max_trace_bytes, max_record_bytes=audit_module.MAX_RECORD_BYTES, + deadline=deadline): + count += 1 + if count > 2 * audit.trial.total_ticks + 16: + raise ValueError('budget_audit_resource_or_record_limit') + audit.consume(json.loads(line)) + if audit.terminal is None: + audit.fail(('recording_missing_terminal',), kind='terminal') + except (OSError, EOFError, zlib.error, lzma.LZMAError, ValueError, KeyError, TypeError) as exc: + audit.fail((f'recording_offline:{exc}',), kind='offline') + report = audit.report() + report.update(terminal=audit.terminal, source_trace=str(args.trace), + new_simulation_steps=0, new_optimizer_steps=0) + with args.output.open('x') as output: + json.dump(report, output, indent=2, allow_nan=False) + output.write('\n') + print(json.dumps({'functional_passed': report['functional_passed'], 'first_failure': report['first_failure']})) + return 0 if report['functional_passed'] else 1 + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/scripts/evaluate_cycle_sim_residual.py b/scripts/evaluate_cycle_sim_residual.py new file mode 100644 index 0000000..70a9c4f --- /dev/null +++ b/scripts/evaluate_cycle_sim_residual.py @@ -0,0 +1,1947 @@ +#!/usr/bin/env python3 +"""Prospective P1/P2 evaluator. CPU --check-only; Kit --prepare-binding; gated --run. + +No mode grants its own permission. --run is a Linux supervisor, not a direct Kit +invocation. It consumes a fresh independently trusted parent grant and binding. +Never use an old replay grant. See logs/contact_cycle/evaluation_runner_v1/README.md +and docs/contact_cycle_native_monitor_v3.md (superseding native coverage policy). +""" +from __future__ import annotations + +import argparse +import ctypes +import dataclasses +import enum +import errno +import functools +import hashlib +import importlib +import importlib.util +import inspect +import json +import math +import os +import shlex +import signal +import socket +import stat +import struct +import subprocess +import sys +import time +from contextlib import ExitStack, suppress +from dataclasses import asdict +from pathlib import Path, PosixPath, WindowsPath +from types import GetSetDescriptorType, MappingProxyType, MemberDescriptorType, ModuleType, SimpleNamespace + +ROOT = Path(__file__).resolve().parents[1] +SCRIPT = 'scripts/evaluate_cycle_sim_residual.py' +PACKAGE = 'source/SkillFactory/SkillFactory/' +TASK = 'SkillFactory.tasks.manager_based.contact_cycle_sim.' +LEG = 'SkillFactory.legs.contact_cycle_sim.' +PROTOCOLS = ('configs/contact_cycle/continuous_eval_p1_v1.json', + 'configs/contact_cycle/bounded_diagnostic_p2_v1.json', + 'configs/contact_cycle/bounded_diagnostic_p2_extended_v1.json') +FUTURE_SOURCES = { + 'runner': [SCRIPT, PACKAGE + 'legs/contact_cycle_sim/evaluation_budget.py'], + 'instrumentation': [PACKAGE + 'tasks/manager_based/contact_cycle_sim/' + name + '.py' + for name in ('evaluation_instrumentation', 'evaluation_rng_scope')], +} +REAL_TEST = 'tests/test_contact_cycle_sim_replay.py::test_actual_frozen_candidate_cpu_only_when_artifacts_present' +LAUNCHER = '/home/cen/IsaacLab/isaaclab.sh' +OPTIONAL_REPLICATOR = 'omni.replicator.core' + +# Exact constructor-late imports evidenced in p1_first_failure_diagnosis.md. +# Full source identities are checked before import and retained in the binding. +CONSTRUCTOR_IMPORT_PINS = { + 'isaaclab.cloner.cloner_cfg': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/cloner/cloner_cfg.py', + 'f48e268609e8e5e91e30b8e6d364cc76579aeb497d69f963cd45975152a282ca'), + 'isaaclab.cloner.cloner_strategies': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/cloner/cloner_strategies.py', + 'ea5943bb5e17df09b792dcf7de436f7481daed8768dc2d8573c6eb45464481d8'), + 'isaaclab.sim.schemas.schemas_actuators': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/sim/schemas/schemas_actuators.py', + '3defa9a6ff41d39e797c6d4a1dfee031913fdaf64ff7f99305a844f4ddf47f77'), + 'isaaclab.sim.spawners.wrappers': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/sim/spawners/wrappers/__init__.py', + '431dfebf1f14464b5479fe618720d913fdefb8e1d613a0c54f45dcddb671cce5'), + 'isaaclab.sim.spawners.wrappers.wrappers_cfg': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/sim/spawners/wrappers/wrappers_cfg.py', + 'ce073f03584f9cab400735fce1af92b261b0e9217592c7ce79c1b8f541399574'), + 'isaaclab.sim.views.usd_frame_view': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/sim/views/usd_frame_view.py', + '6c186fe2991a608323f12f40fb67516dd629079eabe4f6e525c198e0452a59d9'), + 'isaaclab.utils.warp.fabric': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/utils/warp/fabric.py', + '3756853121fbd0fb88b01f07c521927e9cdc1ccd96ceb40b6a603bef29e56c94'), + 'isaaclab_physx.assets.surface_gripper': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/assets/surface_gripper/__init__.py', + '42992defc933ea096bac4666c574bfd32ace19fb3886f6e41b5d5498e06da2ef'), + 'isaaclab_physx.assets.surface_gripper.surface_gripper_cfg': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/assets/surface_gripper/surface_gripper_cfg.py', + 'c71846dad68db328c6faa9ac7dfe37488502e9477f56de0961ab3648f273b14a'), + 'isaaclab_physx.cloner': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/cloner/__init__.py', + '13e3966a049c8a6feeee9a8018162ddce6667f9cbdc0d7350644b2176a191def'), + 'isaaclab_physx.cloner.physx_replicate': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/cloner/physx_replicate.py', + 'a0e7eb7f47e45ce27351002a0ef6c137ddb3c1c4a20a5efcbbd389c3ff5a426c'), + 'isaaclab_physx.sensors': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sensors/__init__.py', + '52dc0bed9f1cc96b938b9a6aae4f2f48076006fce0663c2e8ad017488f8098a4'), + 'isaaclab_physx.sensors.contact_sensor': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sensors/contact_sensor/__init__.py', + '2f81a5de0c1a2a264fc18832fbfe33e3cb49a41a7a246b0de4471437dcbf3dfd'), + 'isaaclab_physx.sensors.contact_sensor.contact_sensor': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sensors/contact_sensor/contact_sensor.py', + '09507db17049bcadd6a48f5992bc20f79b019356c13f1ba3ef1527b2e4188b2c'), + 'isaaclab_physx.sensors.contact_sensor.contact_sensor_data': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sensors/contact_sensor/contact_sensor_data.py', + '711c461be5c696a7a99f0a8fd2a010cd9a5a47e1b8bcfdf493e5ac8af8b65e3d'), + 'isaaclab_physx.sensors.contact_sensor.kernels': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sensors/contact_sensor/kernels.py', + 'e3e9d678336a704a9ba58fe7d31fe8a3a577f66f357c921a2c312c2edb8fa6e3'), + 'isaaclab_physx.sensors.joint_wrench': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sensors/joint_wrench/__init__.py', + '308cb956e92e065c05b66ff0ec0c139dc3bc35380a09d58191af13acf5c14f36'), + 'isaaclab_physx.sensors.joint_wrench.joint_wrench_sensor': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sensors/joint_wrench/joint_wrench_sensor.py', + 'eff63d6f6e8c9958651280889c1797c9cbf2a14fb1b35a1f4e39cb208399ee5f'), + 'isaaclab_physx.sensors.joint_wrench.joint_wrench_sensor_data': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sensors/joint_wrench/joint_wrench_sensor_data.py', + '32b168096dc384587e2962b8e09caccba1d76bb8e40459073b262843b6511937'), + 'isaaclab_physx.sensors.joint_wrench.kernels': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sensors/joint_wrench/kernels.py', + 'a71e33ae91ab7bef9d585d7ca8137028a3dfe5b169dabec4b9b763f8497d25a0'), + 'isaaclab_physx.sensors.kernels': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sensors/kernels.py', + '297f79677d58d7c5ed38cffc7ba19480bc3236a331a64700a54d57f34a5b8b56'), + 'isaaclab_physx.sim.views': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sim/views/__init__.py', + 'ac57ce5aafa903fe42868a410461b9db8fc8a150ed458f9b8bfe80772a94f97c'), + 'isaaclab_physx.sim.views.fabric_frame_view': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/sim/views/fabric_frame_view.py', + '7d9d199be7dc8b9087c60f9f89567fb0463fd0269d20b0aa0e20ae7f8ac1d329'), +} + + +# One reviewed installed shell/CLI layout only; the bridge changes subprocess.run +# to exec, not the inherited socket authentication or the runtime import tree. +LAUNCH_PINS = { + '/home/cen/IsaacLab/isaaclab.sh': + 'c3a6f3111012aa39c5f568012da1ef2cde52a80cbc2277bc59ce295895d7bc68', + '/home/cen/IsaacLab/source/isaaclab/isaaclab/cli/__init__.py': + '8886104cee16eb41c8c7dccbe2955cf8e4c72677f49423bf586f4eb4615a96af', + '/home/cen/IsaacLab/source/isaaclab/isaaclab/cli/commands/envs.py': + '92002ebc7301f559cd17dcbf13a749006c1353aabf9f1247e57a9438a85acecd', + '/home/cen/IsaacLab/source/isaaclab/isaaclab/cli/commands/format.py': + 'bc575ef81dff01a4c8a7b11496515ac34fa9bbdf14613978e84fe37449cd64cc', + '/home/cen/IsaacLab/source/isaaclab/isaaclab/cli/commands/install.py': + 'b285da1f19beada43c1a4ad9aa7e3b432a0042eb0074c7c89ccc7aa0d3c46b31', + '/home/cen/IsaacLab/source/isaaclab/isaaclab/cli/commands/misc.py': + 'b608ffecbae98c810024dc361852325d09197a9f9a355b270bd60c410a459747', + '/home/cen/IsaacLab/source/isaaclab/isaaclab/cli/utils.py': + '9608423230d159234e740a8000fd8206a4c980311e6517f97d785eb58f68fc97', + '/home/cen/IsaacLab/source/isaaclab/isaaclab/__init__.py': + '24e50c4e57b9be1c55aa949f1b8dd416d95e6002e12941abe33a7a6eb16f697b', + '/home/cen/isaacsim-6.0.1/python.sh': + '37f7e3717f1fce1800d0b15b55fcd754c909978bd2455b9d6de3d22d77a92bc3', + '/home/cen/isaacsim-6.0.1/setup_python_env.sh': + '0f73e6dac65efa1a28f994939f4734859d2c39eb236c658bfad9fab70c8882e6', + '/home/cen/isaacsim-6.0.1/kit/python/bin/python3': + 'f3300e753c44afd7860b3f26ccf7c3d98c323b4fc90e7ce2fccae2cd49efd09c', + '/home/cen/isaacsim-6.0.1/kit/libcarb.so': + 'aef0445bb75bd3da04acfabf6d067fb9e40b5c3d17f07dd5369bd63c75489c4f', + '/usr/bin/python3.12': + 'e50d468e8b0adfb05733f5b87b3cff34829c4a8c1aea50c865aa8bdfe4bb150f', + '/usr/bin/bash': + 'bc5945feb8bd26203ebfafea5ce1878bb2e32cb8fb50ab7ae395cfb1e1aaaef1', +} + +RESOLVED_PYTHON = '/home/cen/isaacsim-6.0.1/kit/python/bin/python3' +EXEC_BRIDGE = """import os, sys +from pathlib import Path +from isaaclab.cli.utils import extract_python_exe +resolved = extract_python_exe() +if Path(resolved).resolve() != Path('/home/cen/isaacsim-6.0.1/python.sh'): + raise RuntimeError('identity_unreviewed_resolved_interpreter') +os.execv(resolved, [resolved, *sys.argv[1:]]) +""" + + +def launch_identity(): + b = cpu_module('evaluation_budget') + for path, expected in LAUNCH_PINS.items(): + if b.digest(path) != expected: + raise RuntimeError('identity_launch_source_changed: ' + path) + # This layout has no optional setup_conda_env.sh or docker startup helper. + sim = Path('/home/cen/IsaacLab/_isaac_sim') + if (sim.resolve() != Path('/home/cen/isaacsim-6.0.1') + or (sim / 'setup_conda_env.sh').exists() or (sim / 'vulkan_check.sh').exists() + or any(Path('/home/cen/IsaacLab', name).exists() for name in ('env_isaaclab', '.venv'))): + raise RuntimeError('identity_unreviewed_launch_layout') + return {'format': 'skillfactory.fd-exec-launch.v1', 'sources': LAUNCH_PINS, + 'bridge_sha256': hashlib.sha256(EXEC_BRIDGE.encode()).hexdigest(), + 'runner_sha256': b.digest(ROOT / SCRIPT), 'interpreter': RESOLVED_PYTHON} + + +def launch_command(arguments, environment): + launch_identity() + if any(environment.get(k) for k in ('VIRTUAL_ENV', 'CONDA_PREFIX', 'PYTHONEXE', 'PYTHONHOME', 'args')): + raise RuntimeError('identity_launch_requires_reviewed_no_virtualenv_layout') + text = Path(LAUNCHER).read_text() + text = text.replace('export ISAACLAB_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"', + 'export ISAACLAB_PATH=/home/cen/IsaacLab') + old = 'exec "$python_exe" -c "from isaaclab.cli import cli; cli()" "$@"' + if text.count(old) != 1: + raise RuntimeError('identity_launch_exec_boundary_changed') + text = text.replace(old, 'exec "$python_exe" -c ' + shlex.quote(EXEC_BRIDGE) + ' "$@"') + return ['/usr/bin/bash', '-c', text, LAUNCHER, *arguments] + + +def cpu_module(name): + """Load pure stdlib source without importing Isaac registration before Kit. + + Budget has its real file-backed module identity (no fake parent packages). + CPU-only protocol planning uses a private identity, never a binding snapshot. + """ + if name == 'evaluation_adoption': + cpu_module('evaluation_budget') + full = LEG + name if name in ('evaluation_budget', 'evaluation_adoption') else '_evaluation_cpu_' + name + if full not in sys.modules: + path = ROOT / PACKAGE / 'legs/contact_cycle_sim' / (name + '.py') + spec = importlib.util.spec_from_file_location(full, path) + module = importlib.util.module_from_spec(spec) + sys.modules[full] = module + spec.loader.exec_module(module) + return sys.modules[full] + + +def protect_artifact_path(path): + path = Path(path) + allowed = ROOT / 'logs/contact_cycle/evaluation_runs_v1' + if (not path.is_absolute() or path != path.resolve() + or (path.is_relative_to(ROOT) and not path.is_relative_to(allowed))): + raise ValueError('Use new evaluation_runs_v1 campaign paths or canonical paths outside repository; ' + 'historic evidence/source/release trees are protected') + + +def read_trusted(path, expected): + payload = Path(path).read_bytes() + if (len(expected or '') != 64 or any(c not in '0123456789abcdef' for c in expected) + or hashlib.sha256(payload).hexdigest() != expected): + raise ValueError('identity_independently_trusted_digest_required: ' + str(path)) + return payload + + +def resource_profile(protocol_path): + if protocol_path not in PROTOCOLS: + raise ValueError('Unknown fixed protocol path') + protocol = cpu_module('evaluation_protocol').load_protocol(ROOT / protocol_path) + return protocol.document.get('resource_profile') + + +def protocol_stage(protocol_path): + """Fixed-path stage lookup, safe before real runtime binding. + + Never load a private CPU planning module into the Kit worker. Full protocol + validation remains with the supervisor and the real runtime module. + """ + if protocol_path not in PROTOCOLS: + raise ValueError('Unknown fixed protocol path') + return 'P1' if protocol_path == PROTOCOLS[0] else 'P2' + + +def grant_check(grant, args, b): + extended = resource_profile(args.protocol) == b.EXTENDED_PROFILE + fields = {'format', 'campaign_id', 'launch_index', 'stage', 'trial_indices', 'manifest_sha256', + 'protocol_sha256', 'inventory_sha256', 'artifact_root', 'boot_id', + 'not_after_boottime', 'parent_review', 'p1_evidence_sha256', 'storage_policy_sha256', + 'storage_scope', 'launch_identity_sha256'} + if extended: + fields = (fields - {'p1_evidence_sha256'}) | {'resource_profile', 'p1_adoption_sha256'} + if not isinstance(grant, dict) or set(grant) != fields: + raise ValueError('Explicit fresh parent grant schema required') + index = grant['launch_index'] + if type(index) is not int or index not in ((0,) if extended else (0, 1, 2)): + raise ValueError('Parent grant launch slot') + expected_stage = 'P2' if extended or index == 2 else 'P1' + rows = list(range(20)) if expected_stage == 'P2' else [index] + if (grant['format'] != (b.EXTENDED_GRANT_FORMAT if extended else 'skillfactory.evaluation-parent-grant.v2') + or not isinstance(grant['campaign_id'], str) or not grant['campaign_id'] + or not isinstance(grant['parent_review'], str) or not grant['parent_review'].strip() + or grant['stage'] != expected_stage or grant['trial_indices'] != rows + or grant['manifest_sha256'] != args.trusted_manifest_sha256 + or grant['protocol_sha256'] != b.digest(ROOT / args.protocol) + or grant['inventory_sha256'] != b.digest(args.inventory) + or grant['storage_policy_sha256'] != args.trusted_storage_policy_sha256 + or grant['storage_scope'] != b.STORAGE_SCOPE + or grant['launch_identity_sha256'] != hashlib.sha256(b.encode(launch_identity())).hexdigest() + or grant['artifact_root'] != str(args.artifact_root) + or grant['boot_id'] != b.boot_id() + or args.protocol != PROTOCOLS[2 if extended else index == 2] + or type(grant['not_after_boottime']) not in (int, float) + or not b.boottime() < grant['not_after_boottime'] <= b.boottime() + 3600 + or (not extended and index < 2 and grant['p1_evidence_sha256'] != []) + or (not extended and index == 2 and (not isinstance(grant['p1_evidence_sha256'], list) + or len(grant['p1_evidence_sha256']) != 2))): + raise ValueError('Parent grant identity/order/expiry/review mismatch') + if extended: + if grant['resource_profile'] != b.EXTENDED_PROFILE or args.artifact_root not in (b.EXTENDED_ROOT, b.SECOND_EXTENDED_ROOT): + raise ValueError('Parent grant fixed extended profile/root mismatch') + adoption = cpu_module('evaluation_adoption').verify_adoption(args.artifact_root, grant['p1_adoption_sha256']) + if adoption['source_boot_id'] != b.boot_id(): + raise ValueError('Parent grant adoption same-boot mismatch') + read_trusted(args.manifest, args.trusted_manifest_sha256) + return grant + + +def mount_identity(path): + """Read-only mount facts, NOT evidence that a quota or confinement is effective.""" + matches = [] + for line in Path('/proc/self/mountinfo').read_text().splitlines(): + fields = line.split() + mount = Path(fields[4].replace('\\040', ' ')) + if path == mount or mount in path.parents: + matches.append((len(mount.parts), line)) + if not matches: + raise RuntimeError('storage_mount_identity_unavailable') + return max(matches)[1] + + +STORAGE_APPROVAL = 'logs/contact_cycle/evaluation_runner_recovery_v1/USER_STORAGE_SCOPE_AMENDMENT.md' +STORAGE_APPROVAL_SHA256 = 'e4fac0d36799c9beb67af7687a390a6b5e8390bde1d4b840a6e26c309578c8db' +NATIVE_GAPS = ('sampled logical sizes, not a quota; writes can overshoot between checks', + 'unlisted/global cache writers, deleted-open files, symlink targets and native allocations ' + 'have unknown bytes/growth; only free-space floors on listed filesystems cover external writes', + 'shared native cache changes include unrelated jobs; growth can cause conservative stops', + 'stable non-runtime-owned inaccessible shared subtrees are explicit permission gaps; ' + 'their bytes and growth are unknown; coverage is incomplete', + 'generic HOME/.cache and /tmp are not recursively scanned; unlisted or hidden mounts are uncovered', + 'installed data and kit/data/cache/logs are conservative portable-token candidates; ' + 'actual token destinations require runtime confirmation; missing candidates are not created') +SIM_ROOT = Path('/home/cen/isaacsim-6.0.1') + + +def native_paths(root): + # SimulationApp --portable + kit-core.json data/log writers; token resolution + # remains unqualified. Never substitute a generic home/cache/tmp tree. + return sorted({str(root / 'native'), *shared_native_paths(), + *[str(SIM_ROOT / name) for name in ('data', 'kit/data', 'kit/logs')]}) + + +def shared_native_paths(): + # Installed clear_caches.sh names these three ov caches explicitly. + return sorted({*[str(Path.home() / '.cache/ov' / name) + for name in ('shaders', 'texturecache', 'Kit/107.3')], str(SIM_ROOT / 'kit/cache')}) + + +def free_space_paths(root): + # Other native roots also observe their own filesystem during their scan. + return sorted({str(root), '/tmp', str(Path.home()), str(SIM_ROOT)}) + + +def native_redirects(native, *, preparation=False): + redirects = {'HOME': str(native / 'home'), 'XDG_CACHE_HOME': str(native / 'cache'), + 'TMPDIR': str(native / 'tmp')} + if preparation: + redirects.update(XDG_CONFIG_HOME=str(native / 'config'), XDG_DATA_HOME=str(native / 'data')) + return redirects + + +def task_owned_cache_paths(root): + # Same mapping used to construct the actual subprocess environments. + native = root / 'native' + return sorted([*native_redirects(native / 'preparation-support', preparation=True).values(), + *[path for index in range(3) for path in native_redirects(native / f'launch-{index}').values()]]) + + +def storage_policy_check(args, b): + if args.storage_policy is None or args.trusted_storage_policy_sha256 is None: + raise RuntimeError('storage_bound_policy_unavailable; no launch permitted') + extended = resource_profile(getattr(args, 'protocol', PROTOCOLS[0])) == b.EXTENDED_PROFILE + policy = json.loads(read_trusted(args.storage_policy, args.trusted_storage_policy_sha256)) + read_trusted(ROOT / STORAGE_APPROVAL, STORAGE_APPROVAL_SHA256) + fields = {'format', 'artifact_root', 'limit_bytes', 'scope', 'approval_sha256', + 'native_paths', 'check_interval_seconds', 'max_native_growth_bytes', 'min_free_bytes', + 'coverage_gaps', 'native_hard_cap', 'boot_id', 'mount_identity', 'not_after_boottime', + 'shared_native_paths', 'task_owned_cache_paths', 'free_space_paths', 'free_space_mount_identities'} + root = args.artifact_root + protect_artifact_path(root) + if extended: + fields |= {'resource_profile', 'issued_boottime'} + if (root not in (b.EXTENDED_ROOT, b.SECOND_EXTENDED_ROOT) or policy.get('resource_profile') != b.EXTENDED_PROFILE + or type(policy.get('issued_boottime')) not in (int, float) + or not 0 <= policy['issued_boottime'] <= b.boottime() + or type(policy.get('not_after_boottime')) not in (int, float) + or not policy['issued_boottime'] < policy['not_after_boottime'] + <= policy['issued_boottime'] + b.EXTENDED_TOTAL_SECONDS): + raise RuntimeError('storage_extended_profile_lifetime_mismatch') + if (set(policy) != fields or policy['format'] != ( + b.EXTENDED_POLICY_FORMAT if extended else 'skillfactory.output-storage-policy.v4') + or not root.is_dir() or policy['artifact_root'] != str(root) + or type(policy['limit_bytes']) is not int + or not b.HEADROOM_BYTES + b.STDOUT_ALLOWANCE < policy['limit_bytes'] <= b.CAP_BYTES + or policy['scope'] != b.STORAGE_SCOPE or policy['approval_sha256'] != STORAGE_APPROVAL_SHA256 + or policy['native_paths'] != native_paths(root) or policy['native_hard_cap'] is not False + or policy['coverage_gaps'] != list(NATIVE_GAPS) + or policy['shared_native_paths'] != shared_native_paths() + or policy['task_owned_cache_paths'] != task_owned_cache_paths(root) + or policy['free_space_paths'] != free_space_paths(root) + or policy['free_space_mount_identities'] != {p: mount_identity(Path(p)) for p in free_space_paths(root)} + or any(Path(p).is_relative_to(root) for p in shared_native_paths()) + or type(policy['check_interval_seconds']) not in (int, float) + or not 0 < policy['check_interval_seconds'] <= 1 + or type(policy['max_native_growth_bytes']) is not int + or not 0 < policy['max_native_growth_bytes'] <= b.CAP_BYTES + or type(policy['min_free_bytes']) is not int or policy['min_free_bytes'] < 2 * b.CAP_BYTES + or policy['boot_id'] != b.boot_id() or policy['mount_identity'] != mount_identity(root) + or type(policy['not_after_boottime']) not in (int, float) + or not b.boottime() < policy['not_after_boottime'] <= b.boottime() + (14400 if extended else 3600)): + raise RuntimeError('storage_policy_identity_or_conditions_mismatch') + return policy + + +class NativeScanError(RuntimeError): + """Retain partial scan facts even when baseline fails before monitor creation.""" + def __init__(self, report): + self.report = report + super().__init__('storage_native_scan_failed: ' + json.dumps(report, sort_keys=True)) + + +def native_metadata(info): + return {key: getattr(info, 'st_' + key) for key in ('uid', 'gid', 'mode', 'dev', 'ino', 'ctime_ns')} + + +def native_directory_identity(info): + # Creating entries changes ctime during normal writes; it is not a new directory. + return (info.st_dev, info.st_ino, info.st_uid, info.st_gid, info.st_mode) + + +def native_directory_names(fd, *, recursive): + if not recursive: + return [] + with os.scandir(fd) as entries: + return [entry.name for entry in entries] + + +def native_size(path, *, shared=False, strict_paths=(), exclude_paths=(), required=False, recursive=True): + """Descriptor-relative traversal; never follow even a raced ancestor symlink. + + Only an actual denied directory operation plus stable non-owned metadata and + an effective-credential access denial permits a shared coverage gap. A gap's + contents are not inspected. recursive=False observes only root/ancestor + filesystem facts, never directory entries. Missing ordinary entries are sampled churn, not + permission exclusions; disappearance during denial qualification is fatal. + """ + path = Path(path) + strict = tuple(Path(p) for p in strict_paths) + excluded = set(map(Path, exclude_paths)) + result = {'logical_bytes': 0, 'permission_gaps': [], 'skipped_count': 0, + 'disappeared_paths': [], 'symlink_paths': [], 'free_space': None} + + def fail(current, operation, error, info=None): + result['failure'] = {'path': str(current), 'operation': operation, + 'errno': error.errno, 'error': str(error), + 'metadata': native_metadata(info) if info is not None else None} + raise NativeScanError(result) from error + + def denied(parent, name, current, info, operation, error): + # An inaccessible ancestor of a strict path would also hide owned writes. + protected = any(current.is_relative_to(p) or p.is_relative_to(current) for p in strict) + if (not shared or protected or error.errno not in (errno.EACCES, errno.EPERM) + or info.st_uid in (os.getuid(), os.geteuid()) or not stat.S_ISDIR(info.st_mode)): + fail(current, operation, error, info) + try: + after = os.stat(name, dir_fd=parent, follow_symlinks=False) + accessible = os.access(name, os.R_OK | os.X_OK, dir_fd=parent, + effective_ids=True, follow_symlinks=False) + final = os.stat(name, dir_fd=parent, follow_symlinks=False) + except OSError as metadata_error: + result['denied_operation'] = {'path': str(current), 'errno': error.errno, + 'metadata': native_metadata(info)} + fail(current, 'permission_metadata_recheck', metadata_error, info) + if (native_metadata(info) != native_metadata(after) + or native_metadata(info) != native_metadata(final) or accessible): + fail(current, 'permission_identity_or_access_changed', error, final) + result['permission_gaps'].append({'path': str(current), 'errno': error.errno, + 'operation': operation, **native_metadata(final)}) + result['skipped_count'] += 1 + + def observe_free(fd, current): + try: + usage = os.fstatvfs(fd) + result['free_space'] = {'observed_path': str(current), + 'free_bytes': usage.f_bavail * usage.f_frsize, + 'device': os.fstat(fd).st_dev, 'mount_identity': mount_identity(current)} + except OSError as error: + fail(current, 'free_space', error) + + def visit(parent, name, current, *, ancestor=False): + if current in excluded: + return + try: + info = os.stat(name, dir_fd=parent, follow_symlinks=False) + except FileNotFoundError as error: + if required and (ancestor or current == path): + fail(current, 'required_directory', error) + result['disappeared_paths'].append(str(current)) + return + except OSError as error: + fail(current, 'lstat', error) + if stat.S_ISLNK(info.st_mode): + # Explicit monitored roots/ancestors and owned trees cannot be aliases. + if ancestor or current == path or any(current.is_relative_to(p) or p.is_relative_to(current) + for p in strict): + fail(current, 'symlink', OSError(errno.ELOOP, 'monitored path is a symlink'), info) + result['symlink_paths'].append(str(current)) + return + if ((ancestor or current == path or any(p.is_relative_to(current) for p in strict)) + and not stat.S_ISDIR(info.st_mode)): + fail(current, 'directory', OSError(errno.ENOTDIR, 'not a directory'), info) + if stat.S_ISREG(info.st_mode): + if not os.access(name, os.R_OK, dir_fd=parent, effective_ids=True, follow_symlinks=False): + # access() returns False for ENOENT as well as permission denial. + # Atomic .pending publication may race the earlier lstat. Only a + # descriptor-relative ENOENT recheck is ordinary sampled churn; + # retain its last observed bytes conservatively, never a gap. + try: + os.stat(name, dir_fd=parent, follow_symlinks=False) + except FileNotFoundError: + result['disappeared_paths'].append(str(current)) + result['logical_bytes'] += info.st_size + return + except OSError as error: + fail(current, 'file_access_recheck', error, info) + fail(current, 'file_access', PermissionError(None, 'file unreadable; access() exposes no errno'), info) + result['logical_bytes'] += info.st_size + return + if not stat.S_ISDIR(info.st_mode): + return + fd = None + operation = 'open' + try: + try: + fd = os.open(name, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=parent) + operation = 'fstat' + if native_directory_identity(os.fstat(fd)) != native_directory_identity(info): + fail(current, 'open_identity_changed', OSError(errno.ESTALE, 'directory changed'), info) + if ancestor: + walk_to_root(fd, current) + else: + if current == path: + observe_free(fd, current) + # Scandir errors belong to this directory, not one of its children. + operation = 'scandir' + names = native_directory_names(fd, recursive=recursive) + operation = 'children' + for child in names: + visit(fd, child, current / child) + operation = 'traversal_lstat' + after = os.stat(name, dir_fd=parent, follow_symlinks=False) + if native_directory_identity(after) != native_directory_identity(info): + fail(current, 'traversal_identity_changed', OSError(errno.ESTALE, 'directory changed'), after) + except OSError as error: + if operation in ('open', 'scandir') and error.errno in (errno.EACCES, errno.EPERM) and not ancestor: + denied(parent, name, current, info, operation, error) + else: + fail(current, operation, error, info) + finally: + if fd is not None: + os.close(fd) + + def walk_to_root(fd, current): + observe_free(fd, current) + relative = path.relative_to(current) + if not relative.parts: + raise ValueError('native scan root must not be /') + child = current / relative.parts[0] + visit(fd, relative.parts[0], child, ancestor=child != path) + + try: + with ExitStack() as stack: + fd = os.open('/', os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW) + stack.callback(os.close, fd) + walk_to_root(fd, Path('/')) + except OSError as error: + fail(path, 'ancestor', error) + return result + + +class NativeMonitor: + def __init__(self, policy, b): + self.policy, self.b = policy, b + self.baseline = self.latest = {} + self.baseline_scans = self.latest_scans = {} + self.baseline_free_space = self.latest_free_space = {} + self.gap_history = [] + self.growth = 0 + self.next_check = 0 + self.latest_scan_completed = False + try: + self.check(force=True) + except Exception as error: + if not isinstance(error, NativeScanError): + error.report = self.report() + raise + + def check(self, *, force=False): + now = self.b.boottime() + if not force and now < self.next_check: + return + self.next_check = now + self.policy['check_interval_seconds'] + self.latest_scan_completed = False + root = Path(self.policy['artifact_root']) + if mount_identity(root) != self.policy['mount_identity']: + raise RuntimeError('storage_policy_mount_identity_changed') + paths = self.policy['native_paths'] + strict = [str(root), *self.policy['task_owned_cache_paths'], + *[p for p in paths if p not in self.policy['shared_native_paths']]] + scans = {} + self.latest_free_space = {} + checking_free_space = True + try: + for path in self.policy['free_space_paths']: + if mount_identity(Path(path)) != self.policy['free_space_mount_identities'][path]: + raise RuntimeError('storage_policy_free_space_mount_identity_changed: ' + path) + observation = native_size(path, required=True, recursive=False) + self.latest_free_space[path] = observation['free_space'] + if observation['free_space']['mount_identity'] != self.policy['free_space_mount_identities'][path]: + raise RuntimeError('storage_policy_free_space_mount_identity_changed: ' + path) + if observation['free_space']['free_bytes'] < self.policy['min_free_bytes']: + raise RuntimeError('storage_native_free_space_stop') + checking_free_space = False + # Disjoint scans: output accessibility is NOT native growth/cap accounting. + # The unchanged pre-write aggregate cap still excludes only root/native. + for path in [str(root), *paths]: + exclusions = [p for p in [str(root), *paths] if p != path and Path(p).is_relative_to(path)] + scans[path] = native_size(path, shared=path in self.policy['shared_native_paths'], + strict_paths=strict, exclude_paths=exclusions, required=path == str(root)) + except NativeScanError as error: + if checking_free_space: + self.latest_free_space[path] = error.report['free_space'] + else: + scans[path] = error.report + self.latest_scans = scans + self.latest = {p: scans[p]['logical_bytes'] for p in paths if p in scans} + error.report = {**self.report(), 'failed_scan': error.report} + raise + previous_gaps = {gap['path']: gap for scan in self.latest_scans.values() + for gap in scan['permission_gaps']} + current_gaps = {gap['path']: gap for scan in scans.values() for gap in scan['permission_gaps']} + self.gap_history.append({'boottime': now, 'new': [current_gaps[p] for p in sorted(current_gaps.keys() + - previous_gaps.keys())], + 'no_longer_observed': [previous_gaps[p] for p in sorted(previous_gaps.keys() + - current_gaps.keys())], + 'changed': [{'before': previous_gaps[p], 'after': current_gaps[p]} + for p in sorted(previous_gaps.keys() & current_gaps.keys()) + if previous_gaps[p] != current_gaps[p]]}) + self.latest_scan_completed = True + self.latest_scans = scans + self.latest = {p: scans[p]['logical_bytes'] for p in paths} + if not self.baseline_scans: + self.baseline_free_space = dict(self.latest_free_space) + self.baseline_scans = scans + self.baseline = dict(self.latest) + self.growth = sum(max(0, size - self.baseline[p]) for p, size in self.latest.items()) + if self.gap_history[-1]['changed']: + raise RuntimeError('storage_native_permission_gap_identity_changed') + if any(scan['free_space']['free_bytes'] < self.policy['min_free_bytes'] for scan in scans.values()): + raise RuntimeError('storage_native_free_space_stop') + if self.growth >= self.policy['max_native_growth_bytes']: + raise RuntimeError('storage_native_growth_stop') + + def report(self): + return {'scope': self.b.STORAGE_SCOPE, 'native_hard_cap': False, + 'coverage_complete': False, 'runtime_uid': os.geteuid(), + 'latest_scan_completed': self.latest_scan_completed, + 'baseline_skipped_count': sum(s['skipped_count'] for s in self.baseline_scans.values()), + 'latest_skipped_count': sum(s['skipped_count'] for s in self.latest_scans.values()), + 'baseline_logical_bytes': self.baseline, 'latest_logical_bytes': self.latest, + 'observed_positive_growth_bytes': self.growth if self.latest_scan_completed else None, + 'baseline_scans': self.baseline_scans, 'latest_scans': self.latest_scans, + 'baseline_free_space': self.baseline_free_space, 'latest_free_space': self.latest_free_space, + 'permission_gap_history': list(self.gap_history), 'policy': self.policy} + + +class ForwardingEvidence: + """Stable runtime callbacks, installed before constructor; targets fail closed.""" + recorder = None + observer = None + + def __call__(self, event, state): + if self.recorder is None: + raise RuntimeError('recording_forwarder_uninstalled') + return self.recorder(event, state) + + def seam(self, event, state): + if self.recorder is None: + raise RuntimeError('recording_forwarder_uninstalled') + return self.recorder.seam_callback(event, state) + + def supplemental(self, state): + if self.observer is None: + raise RuntimeError('recording_observer_uninstalled') + return self.observer.supplemental(state) + + +def bounded_traceback(error): + """Retain the innermost 32 frames, without locals, source lines or values.""" + frames = [] + omitted = 0 + tb = error.__traceback__ + while tb is not None: + code = tb.tb_frame.f_code + frames.append({'file': code.co_filename[-512:], 'line': tb.tb_lineno, + 'function': code.co_name[:128]}) + if len(frames) > 32: + del frames[0] + omitted += 1 + tb = tb.tb_next + return {'frames': frames, 'omitted_outer_frames': omitted} + + +def external_failure(phase, error): + prefix = {'inference': 'inference', 'identity': 'identity', 'recording': 'recording', + 'finalize': 'recording_persistence'}.get(phase, 'infrastructure') + if isinstance(error, (KeyboardInterrupt, InterruptedError)): + prefix = 'process_signal' + context = {'phase': phase, 'type': type(error).__name__, 'message': str(error), + 'traceback': bounded_traceback(error)} + diagnostic = getattr(error, 'binding_diagnostic', None) + if diagnostic is not None: + context['binding_diagnostic'] = diagnostic + return {'causes': [prefix + '_external_failure'], 'fatal': True, 'context': context} + + +def execute_lifecycle(api, cfg, protocol, indices, output, budget, verify): + """One constructor, stable forwards, actual step results, durable P2 transitions. + + api is an explicit CPU-test seam; CLI always obtains it from real_runtime. + There is no plugin/provider flag or production verifier injection path. + """ + rows, failures = [], [] + continuation_allowed = True + env = hooks = recorder = None + forward = ForwardingEvidence() + current = indices[0] + phase = 'identity' + counts = None + try: + verify(current) + budget.check() + with api.hooks(before_step=budget.before_step) as hooks: + try: + for position, current in enumerate(indices): + cfg.seed = protocol.trial(current).seed # sole declared transition + phase = 'identity' + verify(current) # same used cfg, never normalize here + budget.check() + phase = 'recording' + recorder = api.recorder(protocol, current, output / f'trial-{current:02d}', + budget_check=budget.recording, supplemental=forward.supplemental) + forward.recorder = recorder + stop = failure = None + acknowledged = False + report = None + try: + phase = 'initialization' + if position == 0: + env = api.environment(cfg=cfg, protocol=protocol, trial_index=current, + runtime_hooks=hooks, evidence_callback=forward, + seam_callback=forward.seam) + phase = 'identity' + verify(current) # immediate constructor boundary, before reset/action + phase = 'initialization' + # Enter only after construction, before any published reset snapshot. + observers = ExitStack() + try: + forward.observer = observers.enter_context(api.instrumentation( + env, backend_rng=api.backend_rng, generators={})) + except BaseException: + observers.close() + raise + observation, _ = env.reset() + else: + observation, _ = env.begin_trial(protocol, current) + stop = env.stop_result + if protocol.document['stage'] == 'P2' and current % 2: + phase = 'identity' + initial = recorder.audit.report().get('initial_pair_state') + if (not rows or rows[-1]['trial']['index'] != current - 1 + or not isinstance(initial, dict) or not initial + or rows[-1].get('initial_pair_state') != initial): + raise RuntimeError('identity_pair_initial_state_mismatch') + tick = 0 + while stop is None: + budget.check() + phase = 'inference' + x = observation['policy'] # raw 46D evidence, not normalized model features + action, mean, std = api.infer(x, protocol.trial(current).controller_kind) + phase = 'recording' + causes = recorder.before_control(tick, x, action, mean=mean, std=std) + if causes: + raise RuntimeError('Rejected control evidence: ' + repr(causes)) + phase = 'step' + actual = env.step(action) + stop = actual.stop + tick += actual.ticks_advanced + phase = 'recording' + causes = recorder.after_step(actual) + if causes: + raise RuntimeError('Rejected actual step evidence: ' + repr(causes)) + observation = actual.observation + except BaseException as error: + failure = external_failure(phase, error) + failures.append(failure) + if env is not None: + stop = env.stop_result # never manufacture StopResult/actions/ticks + finally: + def acknowledge(exact): + nonlocal acknowledged + env.acknowledge_finalized(exact) + acknowledged = True + + try: + phase = 'finalize' + report = recorder.finalize(stop=stop, external_failure=failure, + acknowledge=acknowledge if failure is None else None) + except BaseException as error: + failures.append(external_failure('finalize', error)) + report = recorder.audit.report() + report.update(status='persistence_failed', functional_passed=False) + # Preserve partial stream; close it without claiming durability. + try: + recorder.stream.close() + except BaseException as close_error: + failures.append(external_failure('finalize', close_error)) + rows.append(report) + if failure is not None or failures or stop is None or stop.fatal or not acknowledged: + continuation_allowed = False + break + recorder = None + finally: + phase = 'cleanup' + try: + if 'observers' in locals(): + observers.close() + finally: + if env is not None: + env.close() # hooks still installed, cleanup steps cannot evade counting + except BaseException as error: + failures.append(external_failure(phase, error)) + finally: + if hooks is not None: + counts = {name: getattr(hooks, name) for name in ( + 'attempted_steps', 'completed_steps', 'initialization_steps', 'rollout_steps')} + try: + verify(current) # used actual cfg, after close/guard unwind, including construction failure + except BaseException as error: + failures.append(external_failure('identity', error)) + matrix = api.matrix(protocol, rows) + matrix.update(failures=failures, observed_counts=counts, selected_rows=indices, last_trial_index=current, + equality_scope='recorded_application_controller_and_enumerated_observable_rng_only', + complete_backend_state_equality=False, deterministic_replay=False, + continuation_eligible=continuation_allowed and not failures and all( + row.get('terminal', {}).get('runtime_stop') is not None + and not row['terminal']['runtime_stop']['fatal'] for row in rows) + and len(rows) == len(indices), accepted_skill=False, optimizer_steps=0) + return matrix + + +def preimport_replicator(): + """Only the intended optional package may be absent; broken dependencies fail.""" + missing = None + try: + importlib.import_module(OPTIONAL_REPLICATOR) + except ModuleNotFoundError as error: + if error.name not in ('omni.replicator', OPTIONAL_REPLICATOR): + raise + missing = error.name + return {'module': OPTIONAL_REPLICATOR, 'available': missing is None, + 'missing_module': missing, 'sampling_scope': 'no configured sampling', + 'scope_requires_nominal_config_qualification': True, + 'complete_rng_state_claim': False} + + +def check_replicator_inventory(inventory): + """Availability is bound by exact reviewed module membership, not a grant.""" + if (OPTIONAL_REPLICATOR in sys.modules) != (OPTIONAL_REPLICATOR in inventory['modules']): + raise RuntimeError('identity_optional_replicator_availability_changed') + + +def stored_config_fields(value): + """Binding's dataclass/ClassVar/instance/slot field set, without properties.""" + missing = object() + + def stored(name): + item = inspect.getattr_static(value, name, missing) + if item is missing: + raise ValueError('Unstored dataclass config field: ' + name) + if type(item) is MemberDescriptorType: + return item.__get__(value, type(value)) + if inspect.getattr_static(type(item), '__get__', None) is not None: + # Functions/classes are legitimate stored values, not evaluated descriptors. + if not (inspect.isfunction(item) or inspect.isclass(item) or inspect.isbuiltin(item)): + raise ValueError('Unsupported config field descriptor: ' + name) + return item + + fields = {name: stored(name) for name in inspect.getattr_static(type(value), '__dataclass_fields__')} + dictionary = next((vars(cls)['__dict__'] for cls in type(value).__mro__ if '__dict__' in vars(cls)), None) + if type(dictionary) is GetSetDescriptorType: + fields.update(dictionary.__get__(value, type(value))) + elif dictionary is not None: + raise ValueError('Unsupported config instance dictionary descriptor') + for cls in type(value).__mro__: + slots = vars(cls).get('__slots__', ()) + for slot in (slots,) if type(slots) is str else slots: + if slot not in ('__dict__', '__weakref__') and inspect.getattr_static(value, slot, missing) is not missing: + with suppress(AttributeError): # uninitialized slot, as in binding._config_fields + fields[slot] = stored(slot) + return fields + + +def config_references(config, binding): + """Collect exact ResolvableStrings, rejecting invalid graphs before imports.""" + references, active = [], set() + + def visit(value): + if binding._resolvable(value): + reference = str(value) + if reference.count(':') != 1: + raise ValueError('Unsupported ResolvableString reference') + module, attribute = reference.split(':') + if not attribute.isidentifier() or not all(p.isidentifier() for p in module.split('.')): + raise ValueError('Unsupported ResolvableString reference') + references.append(value) + return + if value is None or type(value) in (str, bool, int, Path, PosixPath, WindowsPath): + return + if type(value) is float: + if not math.isfinite(value): + raise ValueError('Nonfinite effective config') + return + if inspect.isclass(value) or inspect.isfunction(value) or inspect.isbuiltin(value): + return + if id(value) in active: + raise ValueError('Cyclic effective config') + active.add(id(value)) + try: + if isinstance(value, enum.Enum): + children = (value.value,) + elif type(value) in (dict, MappingProxyType): + if any(type(k) is not str for k in value): + raise ValueError('Effective config mapping keys must be strings') + children = value.values() + elif type(value) in (tuple, list): + children = value + elif type(value) is slice: + children = (value.start, value.stop, value.step) + elif type(value) is functools.partial: + children = (value.func, value.args, value.keywords) + elif dataclasses.is_dataclass(type(value)): + children = stored_config_fields(value).values() + else: + raise ValueError('Unsupported effective config type: ' + type(value).__qualname__) + for child in children: + visit(child) + finally: + active.remove(id(value)) + + visit(config) + return references + + +def config_module_path(name, binding): + """Only exact file-backed references in the existing bound source namespaces. + + No find_spec parent imports, prefix wildcard imports, or external inventory + inference. Loaded parent search paths must also be the installed directories. + """ + roots = {'isaaclab': binding.ISAAC_ROOT / 'source/isaaclab/isaaclab', + 'isaaclab_physx': binding.ISAAC_ROOT / 'source/isaaclab_physx/isaaclab_physx', + 'SkillFactory': ROOT / binding.PACKAGE} + parts = name.split('.') + if parts[0] not in roots: + raise ValueError('Config reference requires explicit external inventory: ' + name) + directory = roots[parts[0]] + for index in range(len(parts)): + if index: + directory = directory / parts[index] + package = directory / '__init__.py' + path = package if package.is_file() else directory.with_suffix('.py') + if not path.is_file(): + raise ModuleNotFoundError('Missing installed config module: ' + name, name=name) + path = binding._file(path) + parent_name = '.'.join(parts[:index + 1]) + module = sys.modules.get(parent_name) + if module is not None: + if binding._module_file(parent_name) != path: + raise ValueError('Config reference installed source mismatch: ' + parent_name) + if path == package and list(vars(module).get('__path__', ())) != [str(directory)]: + raise ValueError('Config reference package search path mismatch: ' + parent_name) + if index == 0 and module is None: + raise ValueError('Config reference requires already imported installed root: ' + parent_name) + if index < len(parts) - 1 and path != package: + raise ValueError('Config reference parent is not an installed package: ' + parent_name) + return path + + +def preimport_config_references(config, binding): + """Explicit runner import phase; never calls references or changes their caches. + + The unchanged encoder/import inventory binds these modules and their source / + direct dependency closure later. This report is discovery, not a trust grant. + """ + references = config_references(config, binding) + names = sorted({str(value).split(':')[0] for value in references}) + paths = {name: config_module_path(name, binding) for name in names} + report = {'references': sorted({str(value) for value in references}), 'modules': {}} + for name, expected in paths.items(): + importlib.import_module(name) # absence and transitive failures deliberately propagate + path = binding._module_file(name) + if path != expected or config_module_path(name, binding) != expected: + raise ValueError('Config reference imported source mismatch: ' + name) + report['modules'][name] = {'path': str(path), 'sha256': binding.sha256(path.read_bytes())} + for value in references: + binding._reference_target(value, set()) # static named-symbol/cache validation only + return report + + +def preforward_video_recorder(cfg, env_module): + """Mirror pinned ManagerBasedRLEnv.__init__ forwarding before config binding.""" + env_module.verify_installed_stepping(Path('/home/cen/IsaacLab/source/isaaclab/isaaclab/envs')) + # EvaluationContactCycleEnv calls super().__init__(cfg), i.e. render_mode=None. + if cfg.video_recorder is not None: + cfg.video_recorder.env_render_mode = None + cfg.video_recorder.eye = tuple(float(x) for x in cfg.viewer.eye) + cfg.video_recorder.lookat = tuple(float(x) for x in cfg.viewer.lookat) + + +def preimport_constructor_modules(binding): + """Eagerly resolve the finite reviewed constructor set; never hide new imports. + + Transitive imports remain subject to the unchanged full binding inventory and + immediate postconstructor/final equality checks, not an import ignorelist. + """ + report = {} + for name, (filename, expected_hash) in CONSTRUCTOR_IMPORT_PINS.items(): + expected = Path(filename) + if (config_module_path(name, binding) != expected + or binding.sha256(expected.read_bytes()) != expected_hash): + raise ValueError('Constructor import source mismatch: ' + name) + importlib.import_module(name) + if binding._module_file(name) != expected: + raise ValueError('Constructor imported source mismatch: ' + name) + report[name] = {'path': filename, 'sha256': binding.sha256(expected.read_bytes())} + if report[name]['sha256'] != expected_hash: + raise ValueError('Constructor import source changed: ' + name) + return report + + +# Same-named eager submodule imports bypass this package's lazy function export. +PHYSX_EXPORT_SOURCES = { + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/cloner/__init__.pyi': + '811bf1b50a42f0c854f13a8c26a9cf1a00e00120132c633c38b58375908f8dc6', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/lazy_loader/__init__.py': + '9017e7e780ffe7742b8cd25882c22c93acb9ac353f3372b8dcc76acf832acf29', +} + + +def physx_constructor_export(binding, *, resolve=False, expected=None): + """Resolve once before binding; later checks never repair namespace drift.""" + package_name = 'isaaclab_physx.cloner' + module_name = package_name + '.physx_replicate' + sources = dict(PHYSX_EXPORT_SOURCES) + for name in (package_name, module_name): + filename, digest = CONSTRUCTOR_IMPORT_PINS[name] + if binding._module_file(name) != Path(filename): + raise ValueError('identity_physx_export_module_path: ' + name) + sources[filename] = digest + for filename, digest in sources.items(): + if binding.sha256(Path(filename).read_bytes()) != digest: + raise ValueError('identity_physx_export_source: ' + filename) + package, module = sys.modules[package_name], sys.modules[module_name] + target = vars(module).get('physx_replicate') + if (not inspect.isfunction(target) or target.__module__ != module_name + or target.__qualname__ != 'physx_replicate'): + raise ValueError('identity_physx_export_function') + binding._source_code(target, Path(CONSTRUCTOR_IMPORT_PINS[module_name][0])) + if expected is not None and target is not expected: + raise ValueError('identity_physx_export_changed_callable') + if resolve: + resolver = vars(package).get('__getattr__') + loader_path = Path(next(p for p in PHYSX_EXPORT_SOURCES if '/lazy_loader/' in p)) + if not inspect.isfunction(resolver) or resolver.__module__ != 'lazy_loader': + raise ValueError('identity_physx_export_resolver') + binding._source_code(resolver, loader_path) + if resolver('physx_replicate') is not target: + raise ValueError('identity_physx_export_resolution') + if vars(package).get('physx_replicate') is not target: + raise ValueError('identity_physx_export_namespace') + return target, {'package': package_name, 'symbol': 'physx_replicate', + 'module': module_name, 'qualname': target.__qualname__, 'sources': sources} + + +def verify_constructor_export(runtime): + _, metadata = physx_constructor_export(runtime.binding, expected=runtime.constructor_export) + if metadata != runtime.generator_inventory['constructor_export']: + raise ValueError('identity_physx_export_metadata') + + +def real_runtime(args, output): + """Actual imported runtime; no environment/sim.step here, including preparation.""" + os.chdir(ROOT) + from isaaclab.app import AppLauncher + + app_module = sys.modules[AppLauncher.__module__] + if not str(Path(app_module.__file__).resolve()).startswith('/home/cen/IsaacLab/source/'): + raise RuntimeError('identity_require_actual_/home/cen/IsaacLab_imports') + app = AppLauncher(headless=True, device=args.device, fast_shutdown=True, + kit_args=f'--/log/file={Path(args.native_output) / "kit.log"}').app + try: + inventory = (None if getattr(args, 'discover_inventory', False) + else json.loads(Path(args.inventory).read_bytes())) + # Execution always uses reviewed eager imports; discovery is preparation-only. + for name in (() if inventory is None else inventory['modules']): + importlib.import_module(name) + modules = {name: importlib.import_module(TASK + name) for name in ( + 'evaluation_cfg', 'evaluation_env', 'evaluation_recording', + 'evaluation_instrumentation', 'evaluation_rng_scope')} + for name in ('evaluation_protocol', 'evaluation_audit', 'evaluation_binding', + 'evaluation_budget', 'evaluation_adoption', 'replay'): + modules[name] = importlib.import_module(LEG + name) + ins = modules['evaluation_instrumentation'] + for name in ins.BACKEND_SOURCE_INVENTORY: + importlib.import_module(name) + # ManagerBasedEnv.seed treats Replicator as optional. Do not enable an + # extension, mask transitive defects, or claim its hidden RNG is observed. + replicator = preimport_replicator() + print(json.dumps({'optional_replicator': replicator}), flush=True) + if inventory is not None: + check_replicator_inventory(inventory) + # Numerical lazy paths also belong in the reviewed inventory. + import pytest + import torch + + if torch.device(args.device).type == 'cuda': + torch.cuda.init() + torch.cuda.get_rng_state(torch.device(args.device)) + ins.verify_backend_sources() + replay = modules['replay'] + if not (ROOT / replay.CANDIDATE).is_file(): + raise RuntimeError('identity_real_artifact_test_cannot_skip_missing_candidate') + # Required unchanged CPU test in THIS file-backed, correctly imported process. + if pytest.main(['-q', '-p', 'no:cacheprovider', REAL_TEST]) != 0: + raise RuntimeError('identity_real_artifact_CPU_test_failed') + cfg = modules['evaluation_cfg'].EvaluationContactCycleEnvCfg() + protocol = modules['evaluation_protocol'].load_protocol(ROOT / args.protocol) + cfg.sim.device = args.device + cfg.seed = protocol.trial(args.trial_index).seed + binding = modules['evaluation_binding'] + for name in binding.REQUIRED_RUNTIME: + importlib.import_module(name) + preforward_video_recorder(cfg, modules['evaluation_env']) + constructor_imports = preimport_constructor_modules(binding) + constructor_export, export_metadata = physx_constructor_export(binding, resolve=True) + config_imports = preimport_config_references(cfg, binding) + print(json.dumps({'config_reference_imports': config_imports}), flush=True) + binding.normalize_config(cfg) # exactly once, actual config used later + scope = modules['evaluation_rng_scope'] + inventory_report = scope.qualify_config(cfg) + inventory_report['optional_replicator'] = replicator + inventory_report['config_reference_imports'] = config_imports + inventory_report['constructor_imports'] = constructor_imports + inventory_report['constructor_export'] = export_metadata + physx_constructor_export(binding, expected=constructor_export) + ins.qualify_backend_rng(scope.backend_rng(None)) + arguments = dict(root=ROOT, protocol_path=args.protocol, effective_config=cfg, + future_sources=FUTURE_SOURCES, declared_overrides={'seed': 'protocol.trial.seed'}, + dependency_inventory=inventory, entrypoints=(SCRIPT,)) + api = SimpleNamespace( + environment=modules['evaluation_env'].EvaluationContactCycleEnv, + hooks=modules['evaluation_env'].RuntimeHooks.isaac, + recorder=modules['evaluation_recording'].EvaluationRecorder, + instrumentation=ins.EvaluationInstrumentation, backend_rng=scope.backend_rng, + matrix=modules['evaluation_audit'].matrix_report, + ) + return SimpleNamespace(app=app, cfg=cfg, protocol=protocol, binding=binding, + arguments=arguments, api=api, replay=replay, torch=torch, + generator_inventory=inventory_report, constructor_export=constructor_export) + except BaseException: + import traceback + + traceback.print_exc() + app.close(exit_code=1) # supported fast shutdown must preserve failure + raise + + +def runtime_metadata(runtime): + torch = runtime.torch + device = torch.device(runtime.cfg.sim.device) + driver = Path('/proc/driver/nvidia/version') + return {'torch': str(torch.__version__), 'numpy': str(importlib.import_module('numpy').__version__), + 'warp': str(importlib.import_module('warp').__version__), 'device': str(device), + 'device_name': torch.cuda.get_device_name(device) if device.type == 'cuda' else 'CPU', + 'torch_cuda': torch.version.cuda, + 'driver': driver.read_text() if driver.exists() else 'unavailable', + 'generator_inventory': runtime.generator_inventory, + 'evidence_scope': importlib.import_module(TASK + 'evaluation_rng_scope').EVIDENCE_SCOPE, + 'unobserved_internals': importlib.import_module(TASK + 'evaluation_rng_scope').UNOBSERVED_INTERNALS} + + +class InventoryPreparationError(ValueError): + """Unreviewed diagnostic only; partial identities cannot become a manifest.""" + + def __init__(self, diagnostics): + self.report = {'status': 'FAIL', 'inventory_status': 'unreviewed_diagnostic', + 'execution_authorized': False, 'environment_constructed': False, + 'sim_step_calls': 0, 'invalid_entries': diagnostics, + 'note': 'Invalid entries were not hashed or accepted; discovery is incomplete.'} + super().__init__('Inventory preparation FAIL: ' + '; '.join( + row['entry'] + ': ' + row['error'] for row in diagnostics)) + + +def proposed_native_layouts(roots): + """Independent known layouts only for roots with validated file identities.""" + layouts = {} + if 'torch' in roots: + layouts['torch'] = (roots['torch'] / 'lib',) + if 'numpy' in roots: + layouts['numpy'] = (roots['numpy'] / ('_core' if (roots['numpy'] / '_core').is_dir() else 'core'), + roots['numpy'] / 'random', roots['numpy'] / 'linalg', roots['numpy'] / 'fft', + roots['numpy'].parent / 'numpy.libs') + if 'warp' in roots: + layouts['warp'] = (roots['warp'] / 'bin',) + return layouts + + +def proposed_inventory(binding, cfg): + """Collect independent incompatibilities, never approve a partial inventory. + + Inspect only valid imported identities. An invalid provider is not traversed; + other independent source/module/native entries can still be diagnosed. + """ + diagnostics = [] + + def invalid(stage, entry, error): + diagnostics.append({'stage': stage, 'entry': entry, 'error': str(error), + 'module_observation': binding.module_diagnostic(entry)}) + + _, names = binding._sources(ROOT, FUTURE_SOURCES, (SCRIPT,)) + roles = set(names) + binding._encode(cfg, names, set()) + source_records = binding._imports(names, ROOT, diagnostics=diagnostics) + sources = set(source_records) + numerical = binding.NUMERICAL_ROOTS + required = binding.required_dependencies(sources, roles) + modules = {} + for name in sorted(required): + try: + # Required modules must already be imported by the actual runtime. + # Never interpret incidental __module__ metadata as an import name. + modules[name] = binding._dependency_module_identity(name) + except (ValueError, OSError, ImportError) as error: + invalid('dependency-module', name, error) + roots = {name: Path(modules[name]['path']).parent for name in numerical if name in modules} + layouts = proposed_native_layouts(roots) + artifacts = {} + + def add(path, owner, vendor=False): + try: + path = binding._file(path) + label = str(path) + if label in artifacts: + raise ValueError('Duplicate proposed native artifact: ' + label) + artifacts[label] = {'module': owner, 'path': label, 'sha256': binding.sha256(path.read_bytes()), + 'purpose': 'Installed numerical native implementation consumed by ' + owner} + if vendor: + artifacts[label]['vendor'] = 'NVIDIA' + except (ValueError, OSError) as error: + invalid('native-artifact', str(path), error) + + for owner, directories in layouts.items(): + for directory in directories: + paths = sorted(directory.glob('*.so*')) + if not paths: + invalid('native-layout', str(directory), 'Unsupported/missing numerical native layout') + for path in paths: + add(path, owner) + if 'torch' in roots and (roots['torch'] / 'lib/libtorch_cuda.so').is_file(): + for package in binding.NVIDIA_CUDA_PACKAGES: + directory = roots['torch'].parent / 'nvidia' / package / 'lib' + paths = sorted(directory.glob('*.so*')) + if package in ('cuda_runtime', 'cublas', 'cudnn') and not paths: + invalid('native-layout', str(directory), 'Unsupported/missing NVIDIA CUDA native layout') + for path in paths: + add(path, 'torch', vendor=True) + if diagnostics: + raise InventoryPreparationError(diagnostics) + proposal = {'scope': binding.INVENTORY_SCOPE, 'modules': modules, 'artifacts': artifacts} + try: + return binding._dependency_inventory(proposal, sources, roles) + except (ValueError, OSError, ImportError) as error: + invalid('inventory-validation', 'proposal', error) + raise InventoryPreparationError(diagnostics) from error + + +def bound_filesystem_inputs(manifest): + """Extract immutable file identities, not runtime objects, for external rehash.""" + document = json.loads(manifest) + files, links = {}, {} + + def visit(value): + if isinstance(value, dict): + if {'path', 'target', 'device', 'inode', 'target_sha256'} <= value.keys(): + links[value['path']] = dict(value) + if isinstance(value.get('path'), str) and isinstance(value.get('sha256'), str): + path, digest = value['path'], value['sha256'] + if path in files and files[path] != digest: + raise ValueError('identity_conflicting_file_hash: ' + path) + files[path] = digest + for item in value.values(): + visit(item) + elif isinstance(value, list): + for item in value: + visit(item) + + visit(document) + files.update(document.get('runtime_layout_pins', {})) + # Frozen bundle verification remains the unchanged child's verify_bundle. + # Independently inventory every immutable prepared01 file for postexit equality. + frozen = ROOT / 'logs/contact_cycle/sim_residual_training_v1/prepared01' + for path in frozen.rglob('*'): + if path.is_file(): + files[str(path)] = hashlib.sha256(path.read_bytes()).hexdigest() + return {'files': files, 'links': links, 'frozen_root': str(frozen), + 'scope': 'immutable-files-only; no postexit same-object cfg claim'} + + +def verify_filesystem_inputs(evidence): + for filename, expected in evidence.get('links', {}).items(): + path = Path(filename) + stat = path.lstat() + target = str(path.readlink()) + if (target != expected['target'] or stat.st_dev != expected['device'] + or stat.st_ino != expected['inode'] + or hashlib.sha256(target.encode()).hexdigest() != expected['target_sha256']): + raise ValueError('identity_postexit_link_changed: ' + filename) + for filename, expected in evidence['files'].items(): + path = Path(filename) + if (not path.is_absolute() or path != path.resolve(strict=True) or path.is_symlink() + or hashlib.sha256(path.read_bytes()).hexdigest() != expected): + raise ValueError('identity_postexit_file_changed: ' + filename) + frozen = Path(evidence['frozen_root']) + expected = {p for p in evidence['files'] if Path(p).is_relative_to(frozen)} + if expected != {str(p) for p in frozen.rglob('*') if p.is_file()}: + raise ValueError('identity_postexit_frozen_file_set_changed') + return {'status': 'PASS', 'files_rehashed': len(evidence['files']), 'scope': evidence['scope']} + + +def prepare(args): + discovery = getattr(args, 'discover_inventory', False) + if (args.output is None or args.artifact_root is None + or (args.inventory is None and not discovery) or (args.inventory is not None and discovery)): + raise ValueError('Preparation requires root/output and exactly one of reviewed inventory or discovery') + protect_artifact_path(args.output) + b = cpu_module('evaluation_budget') + protect_artifact_path(args.artifact_root) + if not args.output.is_relative_to(args.artifact_root) or args.output == args.artifact_root: + raise ValueError('Preparation outputs must belong to aggregate artifact root') + b.durable_directory(args.output) + native = args.artifact_root / 'native' + if native != native.resolve(): + raise RuntimeError('storage_native_redirect_symlink') + if not native.exists(): + b.durable_directory(native) + native = native / args.output.name + b.durable_directory(native) + args.native_output = str(native) + + def write(path, data): + if (b.artifact_size(args.artifact_root, exclude_native=True) + len(data) + > b.CAP_BYTES - b.HEADROOM_BYTES - b.STDOUT_ALLOWANCE): + raise RuntimeError('storage_aggregate_cap') + b.durable_write(path, data) + + runtime = None + completed = False + started = b.boottime() + try: + runtime = real_runtime(args, args.output) + if discovery: + try: + inventory = proposed_inventory(runtime.binding, runtime.cfg) + except InventoryPreparationError as error: + error.report['module_observations'] = { + name: runtime.binding.module_diagnostic(name) for name, module in tuple(sys.modules.items()) + if isinstance(module, ModuleType)} + write(args.output / 'inventory.diagnostic.json', b.encode(error.report)) + raise + write(args.output / 'inventory.proposed.json', b.encode(inventory)) + runtime.arguments['dependency_inventory'] = inventory + manifest = runtime.binding.prepare_binding(trial_index=args.trial_index, **runtime.arguments) + write(args.output / 'binding.json', manifest) + write(args.output / 'incidental-observations.json', b.encode( + runtime.binding.incidental_observations( + set(json.loads(manifest)['imports']) | set(runtime.arguments['dependency_inventory']['modules'])))) + metadata = runtime_metadata(runtime) + verify_constructor_export(runtime) + runtime.binding.verify_binding(manifest, trusted_manifest_sha256=hashlib.sha256(manifest).hexdigest(), + trial_index=args.trial_index, **runtime.arguments) + filesystem = bound_filesystem_inputs(manifest) + verify_filesystem_inputs(filesystem) + write(args.output / 'preclose-filesystem.json', b.encode(filesystem)) + write(args.output / 'preparation.json', b.encode({ + 'storage_scope': b.STORAGE_SCOPE, 'native_hard_cap': False, 'launch_identity': launch_identity(), + 'manifest_sha256_for_independent_review': hashlib.sha256(manifest).hexdigest(), + 'execution_authorized': False, 'environment_constructed': False, 'sim_step_calls': 0, + 'inventory_status': 'unreviewed_proposal' if discovery else 'caller_supplied', + 'binding_format': runtime.binding.FORMAT, 'evidence_scope': runtime.binding.PROVENANCE_SCOPE, + 'limitations': runtime.binding.PROVENANCE_LIMITATIONS, 'accepted_skill': False, + 'prepare_seconds': b.boottime() - started, 'runtime': metadata, + 'identity_order': ('actual cfg and inputs verified; evidence durable; then app.close; ' + 'parent postexit files/cleanup'), + 'same_object_cfg_scope': 'preclose only; no claim after interpreter destruction', + 'preclose_identity_verified': True, 'shutdown': 'supported-default-fast', + 'final_pass_owner': 'external supervisor after exit', + 'note': 'Preparation timing is not an actual backend rollout/storage qualification.'})) + completed = True + except BaseException as error: + write(args.output / 'preparation-failure.json', b.encode({ + 'status': 'FAIL', 'error': repr(error), 'failure': external_failure('preparation', error)})) + import traceback + + traceback.print_exc() + raise + finally: + if runtime is not None: + runtime.app.close(exit_code=0 if completed else 1) + return 0 + + +def ancestor(parent, child): + for _ in range(32): + fields = Path(f'/proc/{child}/stat').read_text().rsplit(') ', 1)[1].split() + child = int(fields[1]) + if child == parent: + return True + if child <= 1: + return False + return False + + +def worker_payload(fd): + """Single-use inherited socket; only a live matching supervisor may hand off.""" + with socket.socket(fileno=fd) as channel: + channel.settimeout(15) + pid, uid, _ = struct.unpack('3i', channel.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED, 12)) + command = Path(f'/proc/{pid}/cmdline').read_bytes().split(b'\0') + if (Path(f'/proc/{pid}/exe').resolve() != Path('/usr/bin/python3.12') + or not ancestor(pid, os.getpid())) or uid != os.getuid() or b'--run' not in command or not any( + item.decode(errors='replace').endswith(SCRIPT) for item in command): + raise RuntimeError('identity_inherited_parent_supervisor_required') + channel.sendall(str(os.getpid()).encode() + b'\n') + with channel.makefile('rb') as stream: + data = stream.read(1 << 20) + if len(data) >= 1 << 20: + raise RuntimeError('identity_oversized_handoff') + return json.loads(data) + + +def run_worker(payload): + b = cpu_module('evaluation_budget') + if payload.get('launch_identity') != launch_identity(): + raise RuntimeError('identity_worker_launch_changed') + args = SimpleNamespace(**payload['arguments']) + for name in ('artifact_root', 'inventory', 'manifest'): + setattr(args, name, Path(getattr(args, name))) + reservation = payload['reservation'] + index = reservation['index'] + output = args.artifact_root / f'launch-{index}' + args.trial_index = reservation['grant']['trial_indices'][0] + budget = b.LaunchBudget(args.artifact_root, reservation) + ledger = json.loads((args.artifact_root / 'budget.json').read_bytes()) + if ledger['launches'][-1] != reservation or reservation['status'] != 'reserved': + raise RuntimeError('identity_stale_reservation') + budget.write(output / 'worker-claim.json', {'pid': os.getpid(), 'reservation': reservation}) + handlers = {s: signal.signal(s, lambda *_: setattr(budget, 'interrupted', True)) + for s in (signal.SIGTERM, signal.SIGINT)} + runtime = None + result = {'accepted_skill': False, 'optimizer_steps': 0, 'continuation_eligible': False, + 'storage_scope': b.STORAGE_SCOPE, + 'observed_counts': None, 'failures': [], + 'rows': [{'trial_index': i, 'status': 'not_run'} + for i in range(20 if protocol_stage(args.protocol) == 'P2' else 2)]} + verify = None + try: + budget.check() + manifest = read_trusted(args.manifest, args.trusted_manifest_sha256) + if b.digest(args.inventory) != reservation['grant']['inventory_sha256']: + raise RuntimeError('identity_inventory_changed') + runtime = real_runtime(args, output) + + def verify(row): + budget.check(cleanup=True) + verify_constructor_export(runtime) + check_replicator_inventory(runtime.arguments['dependency_inventory']) + return runtime.binding.verify_binding( + manifest, trusted_manifest_sha256=args.trusted_manifest_sha256, + trial_index=row, **runtime.arguments) + + verify(args.trial_index) + frozen, _ = runtime.replay.verify_bundle(ROOT) + policy = runtime.replay.load_frozen_policy(ROOT / runtime.replay.CANDIDATE, frozen) + policy = policy.to(runtime.cfg.sim.device) + + def infer(x, kind): + if kind not in ('learned_policy', 'analytic_reference'): + raise ValueError('identity_unknown_controller') + mode = 'learned' if kind == 'learned_policy' else 'reference' + return runtime.replay.inference_action(x, mode, policy if mode == 'learned' else None) + + runtime.api.infer = infer + budget.write(output / 'runtime.json', runtime_metadata(runtime)) + result = execute_lifecycle(runtime.api, runtime.cfg, runtime.protocol, + reservation['grant']['trial_indices'], output, budget, verify) + except BaseException as error: + result['failures'].append(external_failure('initialization', error)) + result['continuation_eligible'] = False + finally: + if runtime is not None and verify is not None: + try: + verify(result.get('last_trial_index', args.trial_index)) + filesystem = bound_filesystem_inputs(manifest) + verify_filesystem_inputs(filesystem) + budget.write(output / 'preclose-filesystem.json', filesystem) + result['preclose_identity_verified'] = True + except BaseException as error: + result['failures'].append(external_failure('identity', error)) + result['continuation_eligible'] = False + result['same_object_cfg_scope'] = 'after env.close, before app.close only' + result['final_pass_owner'] = 'external supervisor after exit' + result['storage_scope'] = b.STORAGE_SCOPE + result['native_hard_cap'] = False + persisted = False + try: + budget.write(output / 'result.json', result) + persisted = True + finally: + for sig, handler in handlers.items(): + signal.signal(sig, handler) + if runtime is not None: + runtime.app.close(exit_code=0 if persisted and result['continuation_eligible'] else 1) + return 0 if result['continuation_eligible'] else 1 + + +def process_table(): + table = {} + for path in Path('/proc').glob('[0-9]*/stat'): + try: + fields = path.read_text().rsplit(') ', 1)[1].split() + table[int(path.parent.name)] = (int(fields[19]), int(fields[1]), int(fields[2]), fields[0]) + except (FileNotFoundError, ProcessLookupError): + pass + return table + + +class OwnedProcesses: + """Dedicated sole-spawner supervisor only; not general service containment. + + Pre-existing child trees are refused. Track birth identities, not names/PIDs; + orphan attribution relies on this process spawning no other jobs after start. + """ + def __init__(self): + table = process_table() + self.supervisor = (os.getpid(), table[os.getpid()][0]) + if any(row[1] == os.getpid() for row in table.values()): + raise RuntimeError('process_dedicated_supervisor_has_preexisting_children') + self.preexisting = {(pid, row[0]) for pid, row in table.items()} + self.known = set() + self.root = None + self.unknown = False + + def attach(self, child): + row = process_table().get(child.pid) + if row is None: + raise RuntimeError('process_spawn_identity_unavailable') + self.root = (child.pid, row[0]) + self.known.add(self.root) + + def discover(self): + table = process_table() + if table.get(self.supervisor[0], (None,))[0] != self.supervisor[1]: + self.unknown = True + return {} + # Retain exclusions for observed descendants of unrelated baseline + # processes as well, even if their parent later exits/adopts here. + changed = True + while changed: + changed = False + for pid, row in table.items(): + identity = (pid, row[0]) + parent = table.get(row[1]) + if (identity not in self.preexisting and identity not in self.known and parent is not None + and (row[1], parent[0]) != self.supervisor + and (row[1], parent[0]) in self.preexisting): + self.preexisting.add(identity) + changed = True + if any(row[1] == self.supervisor[0] and (pid, row[0]) in self.preexisting + for pid, row in table.items()): + self.unknown = True # unexpected unrelated adoption violates dedicated ownership + active = {pid: row for pid, row in table.items() if (pid, row[0]) in self.known} + changed = True + while changed: + changed = False + for pid, row in table.items(): + identity = (pid, row[0]) + if pid in active or identity in self.preexisting: + continue + # Direct children after attach are adopted owned orphans under + # the enforced dedicated, sole-spawner architecture. + if (self.root is not None or self.unknown) and (row[1] == self.supervisor[0] or row[1] in active): + self.known.add(identity) + active[pid] = row + changed = True + return active + + def signal(self, pid, row, sig): + # pidfd pins the process during signal delivery; recheck starttime after + # opening to close the discovery -> open PID-reuse window. + with suppress(ProcessLookupError, FileNotFoundError): + fd = os.pidfd_open(pid) + try: + current = process_table().get(pid) + if current is not None and current[0] == row[0]: + signal.pidfd_send_signal(fd, sig) + finally: + os.close(fd) + + def reap(self, child): + child.poll() + for pid, row in self.discover().items(): + if pid != child.pid and row[1] == self.supervisor[0] and row[3] == 'Z': + # An unreaped child cannot reuse its PID. Never reap unrelated jobs. + with suppress(ChildProcessError, ProcessLookupError): + os.waitpid(pid, os.WNOHANG) + + +def cleanup_group(child, deadline, b, owned, pump=lambda: None): + sent = set() + term_until = min(deadline - 10, b.boottime() + 110) + while b.boottime() < deadline: + sig = signal.SIGTERM if b.boottime() < term_until else signal.SIGKILL + for pid, row in owned.discover().items(): + key = (pid, row[0], sig) + if row[3] != 'Z' and key not in sent: + owned.signal(pid, row, sig) + sent.add(key) + owned.reap(child) + pump() # keep draining even when handshake failed or descendants hold pipe + if not owned.discover(): + return not owned.unknown + time.sleep(.05) + owned.reap(child) + pump() + return not owned.discover() and not owned.unknown + + +class StdoutCapture: + """Nonblocking capture from spawn through cleanup; overflow drains/discards. + + Each of three launches has a fixed 1 MiB allowance withheld from all other + writers. This avoids a check/write race with the worker's pending trace bytes. + The 16 MiB cleanup reserve is separate and never consumed by stdout. + """ + def __init__(self, pipe, path, b): + self.pipe, self.b = pipe, b + self.log = Path(path).open('xb', buffering=0) # noqa: SIM115 - owned until cleanup close + try: + os.set_blocking(pipe.fileno(), False) + except BaseException: + try: + self.log.close() + finally: + pipe.close() + raise + self.written = self.discarded = 0 + self.eof = False + self.error = None + + def pump(self): + # Bounded work per turn, even for an unlimited producer. + for _ in range(16): + try: + block = os.read(self.pipe.fileno(), 65536) + except BlockingIOError: + break + if not block: + self.eof = True + break + retained = block[:max(0, self.b.STDOUT_BYTES - self.written)] + try: + if retained and self.error is None: + if self.log.write(retained) != len(retained): + raise OSError('storage_short_stdout_write') + self.written += len(retained) + except OSError as error: + self.error = error + self.discarded += len(block) - (len(retained) if self.error is None else 0) + return self.discarded + + def check(self): + self.pump() + if self.error is not None: + raise self.error + if self.discarded: + raise RuntimeError('storage_stdout_capture_overflow') + + def close(self): + try: + self.pump() + os.fsync(self.log.fileno()) + finally: + self.log.close() + self.pipe.close() + + +def exchange_worker(child, channel, payload, budget, capture, owned, monitor, b): + deadline = min(b.boottime() + 30, budget.reservation['deadline_boottime'] - b.CLEANUP_SECONDS) + channel.setblocking(False) + hello = b'' + while b'\n' not in hello: + capture.check() + budget.check() + monitor.check() + owned.discover() + if b.boottime() >= deadline: + raise RuntimeError('identity_worker_handshake_timeout') + try: + block = channel.recv(64 - len(hello)) + if not block: + raise RuntimeError('identity_worker_handshake_eof') + hello += block + except BlockingIOError: + if child.poll() is not None: + capture.pump() + raise RuntimeError('identity_worker_exited_without_handshake') + time.sleep(.01) + if len(hello) >= 64: + raise RuntimeError('identity_worker_handshake_oversized') + worker_pid = int(hello.strip()) + if (os.getpgid(worker_pid) != child.pid or worker_pid not in owned.discover() + or not ancestor(os.getpid(), worker_pid)): + raise RuntimeError('identity_foreign_worker_handshake') + channel.settimeout(max(.01, deadline - b.boottime())) + channel.sendall(b.encode(payload)) + channel.shutdown(socket.SHUT_WR) + channel.close() + + +def supervise(args): + b = cpu_module('evaluation_budget') + policy = storage_policy_check(args, b) + if not hasattr(os, 'pidfd_open') or not hasattr(signal, 'pidfd_send_signal'): + raise RuntimeError('process_identity_safe_pidfd_supervisor_required') + grant = grant_check(json.loads(read_trusted(args.grant, args.trusted_grant_sha256)), args, b) + profile = resource_profile(args.protocol) + slots, _, _ = b.profile_limits(profile) + if policy['not_after_boottime'] < max(grant['not_after_boottime'], + b.boottime() + slots[grant['launch_index']][2] + + (b.CLEANUP_SECONDS if profile is not None else 0)): + raise RuntimeError('storage_policy_expires_before_grant') + owned = OwnedProcesses() # refuse pre-existing jobs before any reservation + monitor = NativeMonitor(policy, b) + environment = dict(os.environ, PYTEST_DISABLE_PLUGIN_AUTOLOAD='1', PYTHONDONTWRITEBYTECODE='1') + launch_command([], environment) # qualify installed launch path before debit + ledger = b.BudgetLedger(args.artifact_root, grant['campaign_id'], cap_bytes=policy['limit_bytes'], + resource_profile=profile) + child = reservation = channels = capture = None + clean = eligible = False + counts = None + errors = [] + handlers = {} + monitor_failed = False + + def cleanup_pump(): + nonlocal monitor_failed + if capture is not None: + capture.pump() + try: + monitor.check() + except Exception as error: + if not monitor_failed: + errors.append(external_failure('supervisor', error)) + monitor_failed = True # keep signaling/reaping even after native stop + + try: + reservation = ledger.reserve(grant, enforcement_deadline=policy['not_after_boottime']) + output = args.artifact_root / f'launch-{reservation["index"]}' + budget = b.LaunchBudget(args.artifact_root, reservation) + b.durable_directory(output) + handlers = {s: signal.signal(s, lambda *_: setattr(budget, 'interrupted', True)) + for s in (signal.SIGTERM, signal.SIGINT)} + budget.write(output / 'reservation.json', reservation) + if profile is not None: + budget.write(output / 'adopted-p1-evidence.json', cpu_module('evaluation_adoption').verify_adoption( + args.artifact_root, grant['p1_adoption_sha256'])) + budget.write(output / 'storage-policy.json', monitor.report()) + budget.write(output / 'launch-identity.json', launch_identity()) + budget.write(output / 'supervisor-identity.json', { + 'pid': owned.supervisor[0], 'starttime': owned.supervisor[1], + 'dedicated_sole_spawner': True, 'preexisting_children': []}) + for name, path in (('manifest.json', args.manifest), ('inventory.json', args.inventory), + ('grant.json', args.grant)): + data = Path(path).read_bytes() + budget.check(pending=len(data)) + b.durable_write(output / name, data) + if ctypes.CDLL(None, use_errno=True).prctl(36, 1, 0, 0, 0) != 0: + raise RuntimeError('process_subreaper_setup_failed') + channels = socket.socketpair() + parent, child_channel = channels + child_channel.set_inheritable(True) + native = args.artifact_root / 'native' + if native != native.resolve(): + raise RuntimeError('storage_native_redirect_symlink') + if not native.exists(): + b.durable_directory(native) + native = native / f'launch-{reservation["index"]}' + b.durable_directory(native) + redirects = native_redirects(native) + for directory in redirects.values(): + b.durable_directory(Path(directory)) + environment.update(redirects) + command = launch_command([str(ROOT / SCRIPT), '--_worker-fd', str(child_channel.fileno())], environment) + budget.check() + child = subprocess.Popen(command, cwd=ROOT, env=environment, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, start_new_session=True, + pass_fds=(child_channel.fileno(),)) + try: + owned.attach(child) # first operation after spawn; capture setup can fail + except BaseException: + # Sole-spawner discovery must still collect children if birth lookup + # failed; unknown ownership can never be reported as clean. + owned.unknown = True + raise + child_channel.close() + capture = StdoutCapture(child.stdout, output / 'stdout.log', b) + arguments = {'protocol': args.protocol, 'device': args.device, + 'artifact_root': str(args.artifact_root), 'native_output': str(native), + 'inventory': str(output / 'inventory.json'), 'manifest': str(output / 'manifest.json'), + 'trusted_manifest_sha256': args.trusted_manifest_sha256} + exchange_worker(child, parent, {'arguments': arguments, 'reservation': reservation, + 'launch_identity': launch_identity()}, + budget, capture, owned, monitor, b) + while not capture.eof or child.poll() is None: + capture.check() + budget.check() + monitor.check() + owned.discover() + time.sleep(.01) + child.wait(timeout=max(.1, reservation['deadline_boottime'] - b.boottime() - 120)) + if (output / 'result.json').is_file(): + result = json.loads((output / 'result.json').read_bytes()) + counts = result['observed_counts'] + postexit = verify_filesystem_inputs(json.loads((output / 'preclose-filesystem.json').read_bytes())) + budget.write(output / 'postexit-filesystem.json', postexit) + eligible = (child.returncode == 0 and result['continuation_eligible'] + and result.get('preclose_identity_verified') is True) + except BaseException as error: + errors.append(external_failure('supervisor', error)) + finally: + try: + try: + if channels: + for channel in channels: + channel.close() + if child is not None and capture is None and child.stdout is not None: + child.stdout.close() # constructor/attach failure left no capture owner + finally: + if child is not None: + clean = cleanup_group(child, reservation['deadline_boottime'], b, owned, cleanup_pump) + if capture is not None: + capture.close() + if capture.discarded or capture.error: + eligible = False + errors.append(external_failure('recording', RuntimeError('storage_stdout_capture_incomplete'))) + if reservation is not None: + if profile is not None: + try: + adoption = cpu_module('evaluation_adoption').verify_adoption( + args.artifact_root, grant['p1_adoption_sha256']) + budget.write(output / 'postexit-adoption.json', { + 'p1_adoption_sha256': grant['p1_adoption_sha256'], 'status': 'PASS', + 'rows': [{'trial': row['trial'], 'first_failure': row['first_failure'], + 'functional_passed': False, 'accepted_skill': False} + for row in adoption['rows']], + 'historical_reserved_calls': adoption['historical_reserved_calls'], 'local_reserved_calls': 91000, + 'cumulative_reserved_calls': adoption['historical_reserved_calls'] + 91000}) + except BaseException as error: + eligible = False + errors.append(external_failure('identity', error)) + final_size = b.artifact_size(args.artifact_root, exclude_native=True) + eligible = eligible and not errors and final_size < policy['limit_bytes'] - b.HEADROOM_BYTES + budget.write(output / 'supervisor.json', { + 'cleanup_clean': clean, 'errors': errors, 'returncode': None if child is None else child.returncode, + 'artifact_bytes_before_finalization': final_size, 'continuation_eligible': eligible and clean, + 'storage': monitor.report(), 'stdout_discarded_bytes': capture.discarded if capture else 0, + 'stdout_eof_observed': capture.eof if capture else False, + 'owned_process_identities': sorted(owned.known), 'ownership_unknown': owned.unknown, + 'reserved_physical_calls': reservation['reserved_physical_calls'], 'observed_counts': counts}) + ledger.finish(counts=counts, clean=clean, eligible=eligible) + finally: + ledger.close() + for sig, handler in handlers.items(): + signal.signal(sig, handler) + return 0 if eligible and clean else 1 + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + modes = parser.add_mutually_exclusive_group(required=True) + modes.add_argument('--check-only', action='store_true') + modes.add_argument('--prepare-binding', action='store_true') + modes.add_argument('--run', action='store_true') + modes.add_argument('--_worker-fd', type=int, help=argparse.SUPPRESS) + parser.add_argument('--protocol', choices=PROTOCOLS, default=PROTOCOLS[0]) + parser.add_argument('--trial-index', type=int, default=0) + parser.add_argument('--device', default='cuda:0') + parser.add_argument('--inventory', type=Path) + parser.add_argument('--discover-inventory', action='store_true', + help='Preparation only: propose actual imported inventory; never grants execution trust') + parser.add_argument('--output', type=Path) + parser.add_argument('--artifact-root', type=Path) + parser.add_argument('--manifest', type=Path) + parser.add_argument('--trusted-manifest-sha256') + parser.add_argument('--grant', type=Path) + parser.add_argument('--trusted-grant-sha256') + parser.add_argument('--storage-policy', type=Path) + parser.add_argument('--trusted-storage-policy-sha256') + args = parser.parse_args(argv) + if args.discover_inventory and (not args.prepare_binding or args.inventory is not None): + parser.error('--discover-inventory requires preparation only, without --inventory') + if args._worker_fd is not None: + return run_worker(worker_payload(args._worker_fd)) + if args.check_only: + protocol = cpu_module('evaluation_protocol').load_protocol(ROOT / args.protocol) + protocol.trial(args.trial_index) + print(json.dumps({'scope': 'CPU protocol/source planning only; no binding, authorization or physical pass', + 'stage': protocol.document['stage'], 'rows': [asdict(t) for t in protocol.trials], + 'future_sources': FUTURE_SOURCES, + 'resource_profile': resource_profile(args.protocol), + 'reserved_physical_calls': [slot[0] for slot in + cpu_module('evaluation_budget').profile_limits(resource_profile(args.protocol))[0]], + 'actual_backend_imports_and_performance': 'unqualified'})) + return 0 + if args.prepare_binding: + return prepare(args) + if any(getattr(args, name) is None for name in ('inventory', 'artifact_root', 'manifest', + 'trusted_manifest_sha256', 'grant', 'trusted_grant_sha256')): + parser.error('--run requires reviewed inventory, artifact root, manifest+trusted digest, ' + 'fresh grant+trusted digest') + return supervise(args) + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/scripts/evaluate_standing.py b/scripts/evaluate_standing.py index c7df1e9..ef46f91 100644 --- a/scripts/evaluate_standing.py +++ b/scripts/evaluate_standing.py @@ -3,6 +3,7 @@ import argparse import json +import math import os import shutil import subprocess @@ -74,6 +75,14 @@ def evaluate(env, args, policy, inputs): original_loads, original_update = s.apply_loads, env.scene.update frames = 0 shots, actions, memory = [], [], [] + arrows = None + if args.force_arrows: + from standing_force_visualizer import DisturbanceForceArrows + + arrows = DisturbanceForceArrows(args.force_arrow_scale) + print( + f"Orange arrows: actual world disturbance force, {args.force_arrow_scale:g} m/N; no torque/guide/reaction" + ) def apply_loads(): s.last_guide[:] = s.guidance() @@ -87,6 +96,8 @@ def evaluate(env, args, policy, inputs): body_ids=s.payload_ids, is_global=True, ) + if arrows is not None and frames % env.cfg.decimation == 0: + arrows.update(s) # display only, after applying this control step's actual load def observe(dt): nonlocal frames @@ -135,6 +146,8 @@ def evaluate(env, args, policy, inputs): print(f"{args.mode} {i + 1}/{steps}", flush=True) finally: s.apply_loads, env.scene.update = original_loads, original_update + if arrows is not None: + arrows.close() cpu = {k: v[:frames].cpu() for k, v in trace.items()} torch.save(cpu, args.output / "physics_trace.pt") if shots: @@ -176,6 +189,11 @@ def evaluate(env, args, policy, inputs): passed=all(result["gates"].values()), mode=args.mode, controller=args.controller, + visualization={ + "force_arrows": args.force_arrows, + "force_arrow_scale_m_per_N": args.force_arrow_scale, + "source": "actual applied disturbance force in world frame; excludes torque, guide, reaction", + }, input_hash=meta["input_hash"], metadata=meta, accepted_skill=False, @@ -194,8 +212,16 @@ def main(): parser.add_argument("--mode", choices=("holdout", "full", "stability", "exploratory"), required=True) parser.add_argument("--controller", choices=("policy", "hold"), default="policy") parser.add_argument("--output", type=Path, required=True) + parser.add_argument( + "--force-arrows", action="store_true", help="Draw actual disturbance force in Kit (visual only)" + ) + parser.add_argument( + "--force-arrow-scale", type=float, default=0.03, help="Arrow length per newton, m/N (default: 0.03)" + ) add_launcher_args(parser) args = parser.parse_args() + if not math.isfinite(args.force_arrow_scale) or args.force_arrow_scale <= 0: + parser.error("--force-arrow-scale must be finite and positive") args.output.mkdir(parents=True, exist_ok=False) write_json(args.output / "result.json", {"passed": False, "status": "running", "accepted_skill": False}) app = AppLauncher(args).app @@ -221,6 +247,8 @@ def main(): "scripts/runtime_common.py", "scripts/supervise_sim.py", ] + if args.force_arrows: + source_paths.append("scripts/standing_force_visualizer.py") training_identity = json.loads((args.package / "binding.json").read_text())["training_identity"] for path, digest in training_identity["sources"].items(): if sha256(path) != digest: diff --git a/scripts/prepare_cycle_sim_binding.py b/scripts/prepare_cycle_sim_binding.py new file mode 100644 index 0000000..f1c2444 --- /dev/null +++ b/scripts/prepare_cycle_sim_binding.py @@ -0,0 +1,310 @@ +#!/usr/bin/env python3 +"""Dedicated ONE preparation-only supervisor; never an execution grant or retry. + +Native scans run on a daemon thread, never on the BOOTTIME deadline owner. +The main thread alone spawns, discovers, signals and reaps the Kit process tree. +""" +from __future__ import annotations + +import argparse +import ctypes +import importlib.util +import json +import os +import signal +import subprocess +import threading +import time +from pathlib import Path +from types import SimpleNamespace + +ROOT = Path(__file__).resolve().parents[1] +SPEC = importlib.util.spec_from_file_location('preparation_runner', ROOT / 'scripts/evaluate_cycle_sim_residual.py') +r = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(r) +b = r.cpu_module('evaluation_budget') +TOTAL_SECONDS = 600 +PREPARE_SECONDS = 480 +CANDIDATE = ROOT / 'logs/contact_cycle/sim_residual_training_v1/prepared01/candidate_u0256.pt' + + +class AsyncNativeMonitor: + """A blocked native scan cannot block the sole deadline/cleanup owner.""" + def __init__(self, policy): + self.state = (None, None, None) + self.stop = threading.Event() + self.policy = policy + self.thread = threading.Thread(target=self._scan, daemon=True, name='native-monitor') + self.thread.start() + + def _scan(self): + monitor = None + try: + monitor = r.NativeMonitor(self.policy, b) + self.state = (b.boottime(), monitor.report(), None) + while not self.stop.wait(self.policy['check_interval_seconds']): + monitor.check(force=True) + self.state = (b.boottime(), monitor.report(), None) + except BaseException as error: + completed, report, _ = self.state + report = getattr(error, 'report', monitor.report() if monitor is not None else report) + self.state = (completed, report, repr(error)) + + def check(self): + completed, report, error = self.state + if error: + raise RuntimeError('preparation_native_monitor: ' + error) + if completed is not None and b.boottime() - completed > 2: + raise RuntimeError('preparation_native_scan_stale_over_2_seconds') + return report + + +def write(root, name, value, *, final=False): + payload = b.encode(value) + reserve = 0 if final else b.HEADROOM_BYTES + b.STDOUT_ALLOWANCE + if b.artifact_size(root, exclude_native=True) + len(payload) > b.CAP_BYTES - reserve: + raise RuntimeError('preparation_aggregate_output_cap') + b.durable_write(root / name, payload) + + +def without_core_dumps(command): + """Child-only soft/hard RLIMIT_CORE=0; neither a crash fix nor global policy. + + Use an exec shell rather than unsafe preexec_fn in this threaded supervisor. + Piped OS crash handlers may ignore RLIMIT_CORE; old evidence is untouched. + """ + return ['/usr/bin/bash', '-c', + 'ulimit -S -c 0 && ulimit -H -c 0 || exit 125; exec "$@"', + 'preparation-no-core', *command] + + +def postexit_identity(root, frozen_before, protocol=r.PROTOCOLS[0]): + """External owner: require durable child evidence, independently rehash files.""" + output = root / preparation_name(protocol) + manifest = (output / 'binding.json').read_bytes() + report = json.loads((output / 'preparation.json').read_bytes()) + if (report.get('preclose_identity_verified') is not True + or report.get('manifest_sha256_for_independent_review') != r.hashlib.sha256(manifest).hexdigest() + or (output / 'preparation-failure.json').exists()): + raise ValueError('preparation_missing_or_failed_preclose_identity') + evidence = json.loads((output / 'preclose-filesystem.json').read_bytes()) + independent = r.bound_filesystem_inputs(manifest) + independent['files'].update(frozen_before) + if evidence != independent: + raise ValueError('preparation_postexit_bound_inputs_changed') + return r.verify_filesystem_inputs(evidence) + + +def collect_postexit_identity(root, frozen_before, child, errors, deadline, protocol=r.PROTOCOLS[0]): + if child is None: + return None + result = [] + + def rehash(): + try: + result.append((postexit_identity(root, frozen_before, protocol), None)) + except BaseException as error: + result.append((None, repr(error))) + + # File I/O must not take deadline ownership away from the sole supervisor. + thread = threading.Thread(target=rehash, daemon=True, name='postexit-identity') + thread.start() + while thread.is_alive(): + remaining = deadline - b.boottime() + if remaining <= 0: + errors.append('preparation_postexit_identity_deadline') + return None + thread.join(min(.05, remaining)) + report, error = result[0] + if error: + errors.append(error) + return report + + +def preparation_name(protocol): + return 'prep-' + r.protocol_stage(protocol).lower() + + +def supervise(root, policy_path, trusted_policy_digest, protocol=r.PROTOCOLS[0]): + name = preparation_name(protocol) + if r.resource_profile(protocol) is not None: + b.extended_historical_calls(root) # same finite root allowlist as reservation + started = b.boottime() + deadline = started + TOTAL_SECONDS + if not hasattr(os, 'pidfd_open') or not hasattr(signal, 'pidfd_send_signal'): + raise RuntimeError('preparation_pidfd_required') + r.protect_artifact_path(root) + if any((root / name).exists() for name in ('prep-p1', 'prep-p2', 'native/prep-p1', 'native/prep-p2', 'preparation-start.json', 'budget.json')): + raise RuntimeError('preparation_requires_fresh_campaign_no_retry') + policy = r.storage_policy_check(SimpleNamespace( + artifact_root=root, protocol=protocol, storage_policy=policy_path, + trusted_storage_policy_sha256=trusted_policy_digest), b) + if policy['not_after_boottime'] < deadline: + raise RuntimeError('preparation_policy_expiry_before_cleanup') + owned = r.OwnedProcesses() + if ctypes.CDLL(None, use_errno=True).prctl(36, 1, 0, 0, 0) != 0: + raise RuntimeError('preparation_subreaper_setup_failed') + before = b.digest(CANDIDATE) + frozen_before = {str(p): b.digest(p) for p in CANDIDATE.parent.rglob('*') if p.is_file()} + write(root, 'preparation-start.json', { + 'started_boottime': started, 'deadline_boottime': deadline, + 'prepare_cutoff_boottime': started + PREPARE_SECONDS, + 'supervisor_pid_starttime': owned.supervisor, 'dedicated_sole_spawner': True, + 'candidate_before_sha256': before, 'kit_only_attempt': True, + 'actual_run_reservations_consumed': 0, 'execution_authorized': False, + 'storage_policy_sha256': trusted_policy_digest, 'policy_role': 'preparation-monitor-only', + 'supervisor_sha256': b.digest(Path(__file__)), 'runner_sha256': b.digest(ROOT / r.SCRIPT), + 'independent_native_scan_deadline_owner': True, + 'stdout_limit_bytes': b.STDOUT_BYTES, 'cleanup_reserve_bytes': b.HEADROOM_BYTES, + 'stdout_allowance_bytes': b.STDOUT_ALLOWANCE, + 'child_rlimit_core': {'soft': 0, 'hard': 0, 'scope': 'launched child and descendants only'}, + }) + monitor = AsyncNativeMonitor(policy) + child = capture = None + command = None + clean = False + errors = [] + interrupted = threading.Event() + handlers = {sig: signal.signal(sig, lambda *_: interrupted.set()) for sig in (signal.SIGTERM, signal.SIGINT)} + native_report = None + launch_started = launch_ended = None + + def check(): + nonlocal native_report + if interrupted.is_set(): + raise RuntimeError('preparation_interrupted') + if b.boottime() >= started + PREPARE_SECONDS: + raise RuntimeError('preparation_480s_deadline') + native_report = monitor.check() + if b.artifact_size(root, exclude_native=True) > b.CAP_BYTES - b.HEADROOM_BYTES - b.STDOUT_ALLOWANCE: + raise RuntimeError('preparation_aggregate_output_cap') + + def cleanup_pump(): + nonlocal native_report + if capture is not None: + try: + capture.pump() + except Exception as error: + text = repr(error) + if text not in errors: + errors.append(text) + try: + native_report = monitor.check() + except Exception as error: + text = repr(error) + if text not in errors: + errors.append(text) + + try: + while native_report is None: + check() + time.sleep(.05) + write(root, 'native-baseline.json', native_report) + native = root / 'native' + if not native.exists(): + b.durable_directory(native) + support = native / 'preparation-support' + b.durable_directory(support) + redirects = r.native_redirects(support, preparation=True) + for directory in redirects.values(): + b.durable_directory(Path(directory)) + environment = dict(os.environ, PYTHONDONTWRITEBYTECODE='1', PYTEST_DISABLE_PLUGIN_AUTOLOAD='1', + **redirects) + for key in ('CONDA_PREFIX', 'VIRTUAL_ENV', 'PYTHONEXE', 'PYTHONHOME', 'args'): + environment.pop(key, None) + environment['PYTHONPATH'] = ':'.join([ + str(ROOT / 'source/SkillFactory'), + *['/home/cen/IsaacLab/source/' + name for name in + ('isaaclab', 'isaaclab_physx', 'isaaclab_assets', 'isaaclab_tasks')]]) + arguments = [str(ROOT / r.SCRIPT), '--prepare-binding', '--discover-inventory', + '--protocol', protocol, '--trial-index', '0', '--device', 'cuda:0', + '--artifact-root', str(root), '--output', str(root / name)] + command = without_core_dumps(r.launch_command(arguments, environment)) + write(root, 'preparation-command.json', {'argv': command, 'cwd': str(ROOT), + 'environment_overrides': {k: environment[k] for k in ('HOME', 'TMPDIR', 'XDG_CACHE_HOME', + 'XDG_CONFIG_HOME', 'XDG_DATA_HOME', 'PYTHONPATH', 'PYTHONDONTWRITEBYTECODE', + 'PYTEST_DISABLE_PLUGIN_AUTOLOAD')}, 'launch_identity': r.launch_identity()}) + check() + launch_started = b.boottime() + child = subprocess.Popen(command, cwd=ROOT, env=environment, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, start_new_session=True) + try: + owned.attach(child) + except BaseException: + owned.unknown = True + raise + capture = r.StdoutCapture(child.stdout, root / 'preparation.stdout.log', b) + while not capture.eof or child.poll() is None: + capture.check() + check() + owned.discover() + time.sleep(.02) + launch_ended = b.boottime() + if child.returncode != 0: + raise RuntimeError('preparation_child_exit_' + str(child.returncode)) + except BaseException as error: + errors.append(repr(error)) + finally: + try: + if child is not None: + try: + if capture is None and child.stdout is not None: + child.stdout.close() + finally: + clean = r.cleanup_group(child, deadline, b, owned, cleanup_pump) + else: + clean = not owned.discover() and not owned.unknown + if capture is not None: + capture.close() + if capture.discarded or capture.error: + errors.append('preparation_stdout_incomplete') + native_report = monitor.check() + except BaseException as error: + errors.append(repr(error)) + finally: + monitor.stop.set() # never join a blocked filesystem scanner + for sig, handler in handlers.items(): + signal.signal(sig, handler) + postexit = collect_postexit_identity(root, frozen_before, child, errors, deadline, protocol) + after = b.digest(CANDIDATE) + if after != before: + errors.append('candidate_changed') + remaining = owned.discover() + report = { + 'execution_authorized': False, 'kit_only_launch_started': child is not None, + 'actual_run_reservations_consumed': 0, 'command_record': 'preparation-command.json' if command else None, + 'started_boottime': started, 'finished_boottime': b.boottime(), 'deadline_boottime': deadline, + 'launch_started_boottime': launch_started, 'launch_ended_boottime': launch_ended, + 'returncode': child.returncode if child is not None else None, + 'postexit_identity': postexit, + 'identity_ownership': 'child actual cfg preclose; parent immutable files and process cleanup postexit', + 'cleanup_clean': clean, 'ownership_unknown': owned.unknown, + 'owned_remaining': remaining, 'owned_identities': sorted(owned.known), + 'stdout_bytes': capture.written if capture else 0, 'stdout_discarded': capture.discarded if capture else 0, + 'stdout_eof': capture.eof if capture else False, 'errors': errors, + 'monitor': monitor.state[1], 'monitor_last_completed_boottime': monitor.state[0], + 'monitor_thread_still_alive': monitor.thread.is_alive(), + 'candidate_before_sha256': before, 'candidate_after_sha256': after, + 'protocol': protocol, 'preparation_directory': name, + 'outcomes': {filename: (root / name / filename).is_file() + for filename in ('inventory.proposed.json', 'binding.json', 'preparation.json')}, + 'artifact_bytes_before_report': b.artifact_size(root, exclude_native=True), + 'scope': b.STORAGE_SCOPE, 'native_hard_cap': False, + 'inventory_and_manifest_trust': 'unreviewed proposals only; no execution identity approved', + } + write(root, 'preparation-supervisor.json', report, final=True) + return 0 if child is not None and clean and not remaining and not errors else 1 + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--protocol', choices=r.PROTOCOLS, default=r.PROTOCOLS[0]) + parser.add_argument('--artifact-root', type=Path, required=True) + parser.add_argument('--storage-policy', type=Path, required=True) + parser.add_argument('--trusted-storage-policy-sha256', required=True) + args = parser.parse_args(argv) + return supervise(args.artifact_root, args.storage_policy, args.trusted_storage_policy_sha256, args.protocol) + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/scripts/profile_cycle_sim_evidence.py b/scripts/profile_cycle_sim_evidence.py new file mode 100644 index 0000000..9c57f5f --- /dev/null +++ b/scripts/profile_cycle_sim_evidence.py @@ -0,0 +1,197 @@ +#!/usr/bin/env python3 +"""CPU-only saved-record component replay; no simulator, inference or training. + +Private namespace packages bypass task registration, for offline measurement only, +never for identity verification. Originals are read-only; outputs are exclusive. +""" +import argparse +import cProfile +import dataclasses +import gzip +import hashlib +import importlib.util +import json +import lzma +import pstats +import sys +import time +import types +from collections import defaultdict +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +def modules(): + base = ROOT / 'source/SkillFactory/SkillFactory' + for relative in ('', '.legs', '.legs.contact_cycle', '.legs.contact_cycle_sim'): + name = 'SkillFactory' + relative + package = types.ModuleType(name) + package.__path__ = [str(base / relative.lstrip('.').replace('.', '/'))] + sys.modules[name] = package + from SkillFactory.legs.contact_cycle_sim import evaluation_audit, evaluation_budget, evaluation_protocol + path = base / 'tasks/manager_based/contact_cycle_sim/evaluation_recording.py' + spec = importlib.util.spec_from_file_location('_offline_recording', path) + recording = importlib.util.module_from_spec(spec) + spec.loader.exec_module(recording) + return evaluation_protocol, evaluation_audit, evaluation_budget, recording + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--row0', type=Path, required=True) + parser.add_argument('--row1', type=Path, required=True) + parser.add_argument('--budget-root', type=Path, required=True) + parser.add_argument('--output', type=Path, required=True) + parser.add_argument('--replay-directory', type=Path) + args = parser.parse_args() + protocol_module, audit_module, budget_module, recording = modules() + protocol = protocol_module.load_protocol(ROOT / 'configs/contact_cycle/continuous_eval_p1_v1.json') + result = {'scope': 'CPU saved-record replay only; not end-to-end or GPU throughput', + 'python': sys.version, 'rows': [], 'budget_root': str(args.budget_root)} + deadline = time.monotonic() + 1800 + if args.replay_directory is not None: + args.replay_directory.mkdir(exist_ok=False) + for index, path in enumerate((args.row0, args.row1)): + audit = audit_module.TrialAudit(protocol, index) + seconds = defaultdict(float) + counts = defaultdict(int) + compressed = defaultdict(int) + chunks = [] + chunk_size = 0 + digest = hashlib.sha256() + profiler = cProfile.Profile() + + def timed(name, fn, *values, **kwargs): + start = time.perf_counter() + value = fn(*values, **kwargs) + seconds[name] += time.perf_counter() - start + return value + + def flush(): + nonlocal chunks, chunk_size + if not chunks: + return + raw = b''.join(chunks) + for name, codec in [('gzip_chunk_level1', lambda b: gzip.compress(b, compresslevel=1, mtime=0)), + ('xz_chunk_preset0', lambda b: lzma.compress(b, preset=0))]: + data = timed(name, codec, raw) + compressed[name] += len(data) + assert (gzip.decompress(data) if name.startswith('gzip') else lzma.decompress(data)) == raw + counts['chunks'] += 1 + chunks, chunk_size = [], 0 + + start = time.perf_counter() + with gzip.open(path, 'rb') as source: + while True: + raw = timed('gzip_readline', source.readline, audit_module.MAX_RECORD_BYTES + 1) + if not raw: + break + if len(raw) > audit_module.MAX_RECORD_BYTES or time.monotonic() >= deadline: + raise RuntimeError('offline benchmark bound') + record = timed('json_loads', json.loads, raw) + counts[record['kind']] += 1 + safe = timed('json_safe', recording._json_safe, record) + encoded = timed('json_dumps_encode', lambda: (json.dumps(safe, allow_nan=False, + separators=(',', ':')) + '\n').encode()) + assert encoded == raw # Complete serialized fields and order, not just parsed equality. + digest.update(encoded) + compressed['gzip_record_level1'] += len(timed('gzip_record_level1', + gzip.compress, encoded, compresslevel=1, mtime=0)) + timed('plain_copy', audit_module.plain, record) + timed('recorder_copied', recording.copied, record) + causes = timed('audit_consume', audit.consume, record) + # Profile a separate prefix audit below; never alter this audit's ordering. + if len(chunks) == 64 or chunk_size + len(raw) > 8 * 1024 * 1024: + flush() + chunks.append(raw) + chunk_size += len(raw) + if causes or record['kind'] in ('initial', 'seam', 'step_result', 'terminal', 'capture_fault'): + flush() + # Real read-only aggregate walk, same frequency as original recording callback. + timed('artifact_size_scan', budget_module.artifact_size, args.budget_root, exclude_native=True) + flush() + elapsed = time.perf_counter() - start + assert audit.terminal is not None + report = audit.report() + original = json.loads((path.parent / 'metrics.json').read_text()) + assert all(original[k] == value for k, value in report.items()), 'saved audit mismatch' + profile_audit = audit_module.TrialAudit(protocol, index) + with gzip.open(path, 'rb') as source: + for _ in range(161): + record = json.loads(source.readline()) + profiler.runcall(profile_audit.consume, record) + stats = pstats.Stats(profiler) + top = sorted(stats.stats.items(), key=lambda item: item[1][3], reverse=True)[:20] + result['rows'].append({'path': str(path), 'original_bytes': path.stat().st_size, + 'records': dict(counts), 'component_seconds': dict(seconds), 'measured_loop_seconds': elapsed, + 'compressed_bytes': dict(compressed), 'serialized_sha256': digest.hexdigest(), + 'saved_metrics_audit_equal': True, + 'bytes_per_tick': {k: v / counts['tick'] for k, v in compressed.items()}, + 'projected_90000_tick_bytes': {k: 90000 * v / counts['tick'] for k, v in compressed.items()}, + 'audit_ticks_per_second': counts['tick'] / seconds['audit_consume'], + 'audit_profile_prefix_161_records': [{'function': str(key), 'primitive_calls': value[0], + 'calls': value[1], 'self_seconds': value[2], 'cumulative_seconds': value[3]} for key, value in top]}) + if args.replay_directory is not None: + checks = defaultdict(int) + scan_seconds = 0. + + def budget(stage, pending, written): + nonlocal scan_seconds + started = time.perf_counter() + size = (budget_module.artifact_size(args.budget_root, exclude_native=True) + + budget_module.artifact_size(args.replay_directory)) + scan_seconds += time.perf_counter() - started + checks[stage] += 1 + if size + pending > 1024 ** 3 or time.monotonic() > deadline: + raise RuntimeError('offline replay budget') + + started = time.perf_counter() + recorder = recording.EvaluationRecorder(protocol, index, args.replay_directory / f'row{index}', + budget_check=budget, supplemental=lambda _: {}) + with gzip.open(path, 'rb') as source: + for line in source: + record = json.loads(line) + if record['kind'] == 'terminal': + stop_type = dataclasses.make_dataclass('SavedStop', [(k, object) for k in record['runtime_stop']]) + acknowledged = [] + live = recorder.finalize(stop=stop_type(**record['runtime_stop']), + external_failure=record['external_failure'], acknowledge=acknowledged.append) + assert len(acknowledged) == 1 + else: + recorder._record(record) + replay_seconds = time.perf_counter() - started + assert not recorder.faults + assert all(live[k] == value for k, value in report.items()) + # Production bounded reader, complete traversal including trailers/EOF. + spec = importlib.util.spec_from_file_location('_offline_reader', ROOT / 'scripts/audit_cycle_sim_evaluation.py') + reader = importlib.util.module_from_spec(spec) + spec.loader.exec_module(reader) + trace = recorder.directory / audit_module.TRACE_FILENAME + replay_audit = audit_module.TrialAudit(protocol, index) + actual_digest = hashlib.sha256() + decoded_records = 0 + for line in reader.trace_lines(trace, compressed=True, max_bytes=128 * 1024 * 1024, + max_record_bytes=audit_module.MAX_RECORD_BYTES, deadline=deadline): + actual_digest.update(line) + decoded_records += 1 + replay_audit.consume(json.loads(line)) + assert actual_digest.hexdigest() == digest.hexdigest() + assert decoded_records == sum(v for k, v in counts.items() if k != 'chunks') + assert replay_audit.report() == report + result['rows'][-1]['production_replay'] = { + 'trace': str(trace), 'trace_bytes': trace.stat().st_size, + 'bytes_per_tick': trace.stat().st_size / counts['tick'], + 'cpu_seconds_including_read_json_budget_fsync': replay_seconds, + 'cpu_ticks_per_second': counts['tick'] / replay_seconds, + 'budget_checks': dict(checks), 'budget_scan_seconds': scan_seconds, + 'decoded_bytes_sha256_equal': True, 'decoded_records': decoded_records, + 'audit_report_equal': True, 'saved_metrics_equal': True} + with args.output.open('x') as output: + json.dump(result, output, indent=2) + output.write('\n') + print(json.dumps(result, indent=2)) + + +if __name__ == '__main__': + main() diff --git a/scripts/replay_cycle_sim_residual.py b/scripts/replay_cycle_sim_residual.py new file mode 100644 index 0000000..b2d81b8 --- /dev/null +++ b/scripts/replay_cycle_sim_residual.py @@ -0,0 +1,258 @@ +#!/usr/bin/env python3 +"""Frozen u256 replay, never training. --check-only performs CPU preflight without Kit. + +--replay is one nominal first episode (900 controls / 4500 physics ticks per env), +not continuous cycling or acceptance. Physics execution requires separate approval. +Use supervise_sim.py with a fresh sibling log/report and <=360s outer timeout. +""" +import argparse +import json +import os +import signal +import sys +import time +import traceback +from pathlib import Path + +from runtime_common import write_json + +ROOT = Path(__file__).resolve().parents[1] + + +def record_episodes(recorder, output, mode, functional, nominal, boundaries): + """Persist full physics-rate first episodes even when execution/auditing fails.""" + import torch + from audit_contact_cycle_functional import evaluate + from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES + + torch.save(recorder.initial_state, output / "initial_state.pt") + if not recorder.frames: + write_json(output / "episodes.json", {"rows": [], "error": "No physics samples captured"}) + return [] + trace = {k: torch.stack([f[k] for f in recorder.frames]) for k in recorder.frames[0]} + # SimCycleState.policy() exposes these signed simulation-truth errors in 46D. + trace["root_position_error_m"] = trace["policy_snapshot"][..., 32:35].clone() + trace["root_velocity_error_m_s"] = trace["policy_snapshot"][..., 35:38].clone() + episodes = {str(e): {k: v[:stop, e:e+1] for k, v in trace.items()} + for e, stop in enumerate(recorder.stops)} + torch.save(episodes, output / "first_episodes.pt") + write_json(output / "boundaries.json", boundaries) + rows = [] + for e, episode in episodes.items(): + d = {k: v[:, 0] for k, v in episode.items()} + flags = d["failure_flags"] + hits = flags.any(-1).nonzero().flatten() + causes = [name for j, name in enumerate(FAILURE_NAMES) if bool(flags[:, j].any())] + # The immutable auditor has two metadata interfaces, not three controller claims. + audit_kind = "learned_policy" if mode == "learned" else "analytic_reference" + audit = evaluate(episode, functional, audit_kind) + boundary = boundaries[int(e)] + actual = d["processed_target"] - nominal.cpu() - .5 * d["reference_used_normalized"] + invariants = { + "finite": all(bool(torch.isfinite(v).all()) for v in episode.values()), + "actor46": d["actor_input"].shape[-1] == 46, + "request_bound": bool((d["requested_action"].abs() <= 1).all()), + "reference_bound": bool((d["reference_used_normalized"].abs() <= 1).all()), + "actual_residual_bound": bool((actual.abs() <= .05 + 1e-6).all()), + "actual_residual_logged": bool(torch.allclose(actual, d["effective_residual_rad"], atol=1e-6, rtol=0)), + } + if mode == "learned": + invariants["deterministic_action"] = bool(torch.allclose( + d["requested_action"], d["actor_mean"].tanh(), atol=1e-6, rtol=0)) + elif mode == "reference": + invariants["zero_residual"] = not bool(d["requested_action"].any() or actual.abs().gt(1e-6).any()) + success = bool(audit["sim_functional_passed"] and all(invariants.values()) + and boundary["reason"] == "timeout" and not recorder.early[int(e)] + and not causes and not d["would_fail"].any()) + swing = d["phase"] == 3 + rows.append({"env": int(e), "success": success, "functional_audit": audit, + "audit_controller_kind_is_metadata_only": True, "invariants": invariants, + "first_episode_samples": len(d["time"]), "termination": boundary, + "terminal_causes": causes, + "first_failure_time_s": float(d["time"][int(hits[0])]) if len(hits) else None, + "phase_samples": {str(p): int((d["phase"] == p).sum()) for p in range(6)}, + "height_rms_m": float(d["signed_height_error"].square().mean().sqrt()) + if invariants["finite"] else None, + "swing_rms_m": float((d["foot"][swing] - d["target"][swing]).square().sum(-1).mean().sqrt()) + if invariants["finite"] and swing.any() else None, + "landing_error_m": audit["metrics"].get("landing_error_m")}) + write_json(output / "episodes.json", {"rows": rows, "controller_mode": mode, "accepted_skill": False}) + return rows + + +def run_episode(env, args, policy, functional, guard): + import torch + from SkillFactory.legs.contact_cycle_sim.model import features + from SkillFactory.legs.contact_cycle_sim.replay import inference_action + from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state + from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder + + s = state(env) + if (s.robot.is_fixed_base or env.cfg.sim.physics.solver_type != 0 + or env.cfg.sim.physics.enable_external_forces_every_iteration + or env.cfg.terminations.failed is None or env.physics_dt != .002 or env.step_dt != .01 + or env.cfg.episode_length_s != 9): + raise RuntimeError("Require frozen floating-root PGS/500Hz physics/100Hz control/9s failure-enabled task") + x = features(env.reset()[0]["policy"]) + if bool(s.scenarios.any() or s.stand_only.any()): + raise RuntimeError("Frozen replay must contain nominal complete cycles only") + boundaries = [{"reason": "incomplete", "control_step": None} for _ in range(env.num_envs)] + recorder = SimCycleRecorder(env) + rewards = [] + rows = [] + try: + with recorder, torch.no_grad(): + for step in range(900): + guard() + action, mean, std = inference_action(x, args.controller, policy, args.script_action) + recorder.before_control(x if policy is not None else None, action, mean, std) + obs, reward, terminated, truncated, _ = env.step(action) + rewards.append(reward.detach().cpu().clone()) + recorder.end_control(terminated, truncated, step == 899) + for e in (terminated | truncated).nonzero().flatten().cpu().tolist(): + if boundaries[e]["control_step"] is None: + boundaries[e] = {"reason": "physical_failure" if bool(terminated[e]) else "timeout", + "control_step": step + 1} + if not torch.isfinite(reward).all(): + raise RuntimeError("Nonfinite replay reward") + guard() + # Stop the entire batch on any early first boundary; never splice a reset episode. + if bool(terminated.any()) or (step != 899 and bool(truncated.any())): + for boundary in boundaries: + if boundary["control_step"] is None: + boundary.update(reason="batch_stopped_after_peer_boundary", control_step=step + 1) + break + if bool((terminated | truncated).all()): + break + x = features(obs["policy"]) + except BaseException as error: + for boundary in boundaries: + if boundary["control_step"] is None: + boundary.update(reason="exception", error=f"{type(error).__name__}: {error}") + raise + finally: + if rewards: + torch.save(torch.stack(rewards), args.output / "control_rewards.pt") + rows = record_episodes(recorder, args.output, args.controller, functional, s.nominal, boundaries) + return {"passed": len(rows) == env.num_envs and all(row["success"] for row in rows), + "rows": rows, "control_steps": len(rewards), "physics_samples": len(recorder.frames)} + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + execution = parser.add_mutually_exclusive_group(required=True) + execution.add_argument("--check-only", action="store_true") + execution.add_argument("--replay", action="store_true") + parser.add_argument("--controller", choices=("learned", "reference", "script"), default="learned") + parser.add_argument("--checkpoint", type=Path) + parser.add_argument("--script-action", nargs=3, type=float, metavar=("HIP", "THIGH", "CALF")) + parser.add_argument("--output", type=Path, required=True) + parser.add_argument("--num-envs", type=int, choices=range(1, 9), default=1) + parser.add_argument("--device", default="cuda:0") + parser.add_argument("--headless", action="store_true") + args = parser.parse_args(argv) + from SkillFactory.legs.contact_cycle_sim.replay import ( + CANDIDATE, + controller_identity, + create_output, + load_frozen_policy, + verify_bundle, + ) + + checkpoint = (args.checkpoint or ROOT / CANDIDATE).resolve() + args.output = create_output(ROOT, args.output, checkpoint) + result = {"passed": False, "accepted_skill": False, "optimizer_steps": 0, + "controller_mode": args.controller, "initialization_entered": False, "physics_executed": False, + "checkpoint_requested": str(checkpoint)} + app = env = safety = None + code = 1 + started = time.monotonic() + stopped = False + old_handlers = {} + + def stop(*_): + nonlocal stopped + stopped = True + + def guard(): + if stopped or time.monotonic() - started >= 300: + raise InterruptedError("Replay interrupted or 300s wall bound exceeded; no retry") + if safety is not None: + safety.check() + + try: + result.update(controller_identity(args.controller, args.script_action)) + manifest, binding = verify_bundle(ROOT, checkpoint) + write_json(args.output / "identity.json", {**binding, **result}) + policy = load_frozen_policy(checkpoint, manifest) if args.controller == "learned" else None + result["preflight_passed"] = True + if args.check_only: + result.update(passed=True, scope="CPU identity/inference loading only; no physical success claim") + code = 0 + else: + for sig in (signal.SIGTERM, signal.SIGINT): + old_handlers[sig] = signal.signal(sig, stop) + guard() + # Existing configs/assets resolve relative to the repository, not the caller's cwd. + os.chdir(ROOT) + from isaaclab.app import AppLauncher + + app = AppLauncher(headless=args.headless, device=args.device).app + guard() + import torch + from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit + from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg + from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv + + cfg = manifest["training"] + environment_cfg = SimContactCycleEnvCfg(residual_config=str(ROOT / cfg["task_config"])) + environment_cfg.scene.num_envs = args.num_envs + environment_cfg.sim.device = args.device + environment_cfg.seed = cfg["validation_seed"] + # Construction resets/warms up PhysX; a constructor failure cannot prove zero physics. + result.update(initialization_entered=True, physics_executed=None, status="initializing") + write_json(args.output / "result.json", result) + env = SimContactCycleEnv(cfg=environment_cfg) + result["physics_executed"] = True + if policy is not None: + policy = policy.to(env.device) + result["runtime"] = {"torch": str(torch.__version__), "device": str(env.device), + "seed": cfg["validation_seed"], "num_envs": env.num_envs, + "physics_dt": env.physics_dt, "control_dt": env.step_dt, + "max_control_steps": 900, "max_wall_s": 300, + "note": "Nominal development replay; numerical differences may occur"} + functional = json.loads((ROOT / cfg["functional_config"]).read_text()) + thresholds = json.loads((ROOT / cfg["safety_config"]).read_text())["gates"] + with PhysicalSafetyAudit(env, thresholds) as safety: + result.update(run_episode(env, args, policy, functional, guard)) + guard() + code = 0 if result["passed"] else 2 + _, final_binding = verify_bundle(ROOT, checkpoint) + if final_binding != binding: + raise RuntimeError("Replay inputs changed during execution") + except BaseException: + result.update(passed=False, error=traceback.format_exc()) + traceback.print_exc() + code = 1 + finally: + if safety is not None: + result["physics_safety"] = safety.report() + if env is not None: + try: + env.close() + except BaseException: + result.update(passed=False, shutdown_error=traceback.format_exc()) + code = 1 + result.update(status="completed" if code == 0 else "failed", wall_seconds=time.monotonic() - started) + try: + write_json(args.output / "result.json", result) + finally: + for sig, handler in old_handlers.items(): + signal.signal(sig, handler) + if app is not None: + app.close(exit_code=code) + return code + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/rootxy_launch_v2.py b/scripts/rootxy_launch_v2.py new file mode 100644 index 0000000..6e34c5e --- /dev/null +++ b/scripts/rootxy_launch_v2.py @@ -0,0 +1,871 @@ +"""Dedicated RootXY supervisor adapter. Importing this module never starts Kit. + +Ownership/native monitoring/launch transport reuse reviewed evaluator helpers; +no evaluation grant, protocol, ledger, checkpoint or candidate authority is reused. +""" +import argparse +import ctypes +from contextlib import contextmanager +import fcntl +import hashlib +import importlib +import json +import math +import os +import signal +import socket +import struct +import subprocess +import sys +import time +from pathlib import Path +from types import SimpleNamespace + +import evaluate_cycle_sim_residual as owner + +ROOT = owner.ROOT +SCRIPT = 'scripts/train_cycle_sim_rootxy_v2.py' +PROFILE = 'rootxy-v2-7200s-1GiB-73460calls' +TASK_ID = 'SkillFactory-Go2FL-ContactCycleSimRootXY-v2' +PREREG_FORMAT = 'skillfactory.contact-cycle-sim-rootxy-preregistration.v2' +GRANT_FORMAT = 'skillfactory.rootxy-parent-grant.v2' +POLICY_FORMAT = 'skillfactory.rootxy-native-policy.v2' +PREPARATION_FORMAT = 'skillfactory.rootxy-config-preparation.v2' +STORAGE_SCOPE = 'rootxy-new-owned.native-monitored-stop.v2' +DECISION_FORMAT = 'skillfactory.rootxy-p2-decision.v2' +CAP = 1 << 30 +HEADROOM = 16 << 20 +STDOUT = 1 << 20 +NATIVE_LIFETIME = 10800 +b = owner.cpu_module('evaluation_budget') + + +def canonical(value): + return b.encode(value) + + +def sha(value): + return hashlib.sha256(canonical(value)).hexdigest() + + +def read(path, expected=None): + path = Path(path) + if not path.is_file() or path.is_symlink() or path.stat().st_size > 32 << 20: + raise ValueError('Missing/unsafe/oversized RootXY input: ' + str(path)) + data = path.read_bytes() + if expected is not None: + owner.read_trusted(path, expected) + return json.loads(data) + + +def checked_root(path): + root = Path(path) + if (not root.is_absolute() or root != root.resolve() + or root.is_relative_to(ROOT) and not root.is_relative_to(ROOT / 'logs/contact_cycle/rootxy_v2') + or root.is_relative_to(ROOT / 'releases') + or root.is_relative_to(ROOT / 'logs/contact_cycle/sim_residual_training_v1')): + raise ValueError('Canonical new isolated RootXY root required') + return root + + +def verify_sources(manifest): + if (manifest.get('format') != PREREG_FORMAT or manifest.get('task_id') != TASK_ID + or manifest.get('launch_integration_complete') is not True + or manifest.get('accepted_skill') is not False): + raise ValueError('New RootXY preregistration required') + for relative, expected in manifest['sources'].items(): + path = ROOT / relative + if (Path(relative).is_absolute() or '..' in Path(relative).parts or path != path.resolve() + or b.digest(path) != expected): + raise ValueError('RootXY source drift: ' + relative) + cfg = manifest['training'] + fixed = {'seeds': [92511], 'validation_seed': 93511, 'num_envs': 128, + 'max_total_wall_s': 7200, 'cleanup_reserve_s': 120, 'max_artifact_bytes': CAP, + 'max_validation_transitions': 806400, 'validation_updates': [64, 128, 192, 256]} + if any(cfg.get(k) != v for k, v in fixed.items()): + raise ValueError('RootXY resource contract changed') + + +def launch_identity(): + app_path = Path('/home/cen/IsaacLab/source/isaaclab/isaaclab/app/app_launcher.py') + return {'app_launcher': {'path': str(app_path), 'sha256': b.digest(app_path)}, 'format': 'skillfactory.rootxy-launch-identity.v2', + 'transport': owner.launch_identity(), 'profile': PROFILE, 'task_id': TASK_ID, + 'entrypoint': {'path': str(ROOT / SCRIPT), 'sha256': b.digest(ROOT / SCRIPT)}, + 'adapter': {'path': str(Path(__file__).resolve()), 'sha256': b.digest(__file__)}} + + +def redirects(root, mode): + return owner.native_redirects(root / 'native' / mode, preparation=True) + + +def policy_check(args, now): + policy = read(args.storage_policy, args.trusted_storage_policy_sha256) + root = args.output + fields = {'format', 'profile', 'artifact_root', 'limit_bytes', 'native_paths', 'shared_native_paths', + 'task_owned_cache_paths', 'free_space_paths', 'free_space_mount_identities', 'mount_identity', + 'check_interval_seconds', 'max_native_growth_bytes', 'min_free_bytes', 'coverage_gaps', + 'native_hard_cap', 'boot_id', 'issued_boottime', 'not_after_boottime', 'lifetime_seconds'} + duration = policy.get('lifetime_seconds') + if (set(policy) != fields or policy['format'] != POLICY_FORMAT or policy['profile'] != PROFILE + or policy['artifact_root'] != str(root) or policy['limit_bytes'] != CAP + or policy['native_paths'] != owner.native_paths(root) + or policy['shared_native_paths'] != owner.shared_native_paths() + or policy['task_owned_cache_paths'] != sorted([*redirects(root, 'prepare').values(), + *redirects(root, 'run').values()]) + or policy['free_space_paths'] != owner.free_space_paths(root) + or policy['free_space_mount_identities'] != { + p: owner.mount_identity(Path(p)) for p in owner.free_space_paths(root)} + or policy['mount_identity'] != owner.mount_identity(root) + or policy['coverage_gaps'] != list(owner.NATIVE_GAPS) or policy['native_hard_cap'] is not False + or policy['boot_id'] != b.boot_id() or type(duration) is not int or not 7200 <= duration <= 10800 + or type(policy['issued_boottime']) not in (int, float) + or not 0 <= policy['issued_boottime'] <= now + or policy['not_after_boottime'] != policy['issued_boottime'] + duration + or type(policy['check_interval_seconds']) not in (int, float) + or not 0 < policy['check_interval_seconds'] <= 1 + or type(policy['max_native_growth_bytes']) is not int + or not 0 < policy['max_native_growth_bytes'] <= CAP + or type(policy['min_free_bytes']) is not int or policy['min_free_bytes'] < 2 * CAP): + raise PermissionError('Exact RootXY native MONITOR policy required (not quota)') + return policy + + +def admission(args, now): + args.output = checked_root(args.output) + mode = 'prepare' if args.prepare else 'run' + required = ('manifest', 'trusted_manifest_sha256', 'authorization', 'trusted_authorization_sha256', + 'storage_policy', 'trusted_storage_policy_sha256') + if any(getattr(args, k, None) is None for k in required): + raise PermissionError('Fresh exact RootXY manifest/native-policy/parent grant required') + manifest = read(args.manifest, args.trusted_manifest_sha256) + verify_sources(manifest) + grant = read(args.authorization, args.trusted_authorization_sha256) + fields = {'format', 'profile', 'task_id', 'mode', 'artifact_root', 'manifest_sha256', 'storage_policy_sha256', + 'launch_identity_sha256', 'boot_id', 'not_after_boottime', 'parent_review', + 'preparation_sha256', 'binding_sha256', 'p2_decision_sha256', 'gpu_identity'} + if (set(grant) != fields or grant['format'] != GRANT_FORMAT or grant['profile'] != PROFILE + or grant['task_id'] != TASK_ID or grant['mode'] != mode or grant['artifact_root'] != str(args.output) + or grant['manifest_sha256'] != args.trusted_manifest_sha256 + or grant['storage_policy_sha256'] != args.trusted_storage_policy_sha256 + or grant['launch_identity_sha256'] != sha(launch_identity()) + or grant['boot_id'] != b.boot_id() or type(grant['not_after_boottime']) not in (int, float) + or not now < grant['not_after_boottime'] <= now + 3600 + or type(grant['parent_review']) is not str or not grant['parent_review'].strip()): + raise PermissionError('RootXY parent grant identity/expiry mismatch') + if mode == 'prepare': + if any(grant[k] is not None for k in ('preparation_sha256', 'binding_sha256', 'p2_decision_sha256')): + raise PermissionError('PREP has pending P2/config identity only, not runtime approval') + if args.output.exists(): + raise FileExistsError('PREP requires a NEW root; no retry') + else: + for field in ('preparation_sha256', 'binding_sha256', 'p2_decision_sha256'): + value = grant[field] + if (type(value) is not str or len(value) != 64 + or any(c not in '0123456789abcdef' for c in value)): + raise PermissionError('RUN requires an exact independently approved ' + field) + if any((args.output / name).exists() for name in ( + 'run-claim.lock', 'launch_claim.json', 'budget.json', 'budget.lock', 'locked_candidate.json', + 'run-worker-result.json', 'run-supervisor.json')): + raise FileExistsError('Fresh-only RootXY RUN; no prior trial or candidate artifacts') + preparation = read(args.output / 'preparation.json', grant['preparation_sha256']) + binding = read(args.output / 'training-binding.json', grant['binding_sha256']) + decision = read(args.p2_decision, grant['p2_decision_sha256']) + if (preparation.get('format') != PREPARATION_FORMAT or preparation.get('status') != 'prepared_config_only' + or preparation.get('manifest_sha256') != args.trusted_manifest_sha256 + or preparation.get('binding_sha256') != grant['binding_sha256'] + or preparation.get('artifact_root') != str(args.output) + or preparation.get('physics_calls') != 0 or preparation.get('runtime_qualified') is not False + or binding.get('format') != 'skillfactory.rootxy-training-binding.v2' + or binding.get('artifact_root') != str(args.output) or binding.get('profile') != PROFILE): + raise PermissionError('Exact config-only preparation/binding required') + prep_supervisor = read(args.output / 'prepare-supervisor.json', preparation['supervisor_sha256']) + read(args.output / 'prepare-worker-result.json', preparation['worker_result_sha256']) + if (preparation.get('postexit_verified') is not True + or prep_supervisor.get('success') is not True or prep_supervisor.get('cleanup_clean') is not True): + raise PermissionError('PREP needs independent clean waited-exit completion') + if (set(decision) != {'format', 'task_id', 'artifact_root', 'manifest_sha256', 'preparation_sha256', + 'binding_sha256', 'approved', 'parent_review', 'p2_evidence'} + or decision['format'] != DECISION_FORMAT or decision['task_id'] != TASK_ID + or decision['artifact_root'] != str(args.output) or decision['approved'] is not True + or decision['manifest_sha256'] != args.trusted_manifest_sha256 + or decision['preparation_sha256'] != grant['preparation_sha256'] + or decision['binding_sha256'] != grant['binding_sha256'] + or not isinstance(decision['parent_review'], str) or not decision['parent_review'].strip() + or not isinstance(decision['p2_evidence'], dict) or not decision['p2_evidence']): + raise PermissionError('Separate parent-approved P2 evidence/feasibility decision required') + for path, digest in decision['p2_evidence'].items(): + owner.read_trusted(path, digest) + for relative, digest in manifest['sources'].items(): + if b.digest(args.output / 'source_snapshot' / relative) != digest: + raise ValueError('RootXY preparation snapshot drift') + policy = policy_check(args, now) + total = 600 if mode == 'prepare' else 7200 + if policy['not_after_boottime'] < max(grant['not_after_boottime'], now + total + 120): + raise PermissionError('Native policy must cover admission, full lifetime and headroom') + return manifest, grant, policy + + +class GuardBudget: + """RootXY cap/deadline facade for reviewed ownership and stdout transport.""" + def __init__(self, root, start, mode, monitor=None): + self.root, self.start, self.mode, self.monitor = Path(root), start, mode, monitor + self.boot = b.boot_id() + self.deadline = start + (600 if mode == 'prepare' else 7200) + self.reservation = {'deadline_boottime': self.deadline} + self.interrupted = False + self.external_growth = 0 + + def check(self, pending=0, *, cleanup=False): + if (self.interrupted and not cleanup or b.boot_id() != self.boot + or not self.start <= b.boottime() < self.deadline - (0 if cleanup else 120)): + raise RuntimeError('RootXY cancelled/deadline/boot mismatch') + if self.monitor is not None: + self.monitor.check() + self.external_growth = sum(max(0, size - self.monitor.baseline[path]) + for path, size in self.monitor.latest.items() + if not Path(path).is_relative_to(self.root)) + limit = CAP - (0 if cleanup else HEADROOM + STDOUT) + if b.artifact_size(self.root) + self.external_growth + pending > limit: + raise RuntimeError('RootXY aggregate new-owned/native storage cutoff') + + def write(self, path, value, *, cleanup=False): + path = Path(path) + if path.parent != path.parent.resolve() or not path.is_relative_to(self.root) or path.is_symlink(): + raise ValueError('Unsafe RootXY publication path') + data = canonical(value) + if len(data) > 16 << 20: + raise RuntimeError('Bounded RootXY publication exceeded') + self.check(len(data), cleanup=cleanup) + temporary = path.with_name(path.name + '.pending') + b.durable_write(temporary, data) + os.replace(temporary, path) + b.sync_directory(path.parent) + + +def desktop_identity(pid, *, hash_executable=True): + """Pin a pre-existing graphics client, never infer permission from its name.""" + if type(pid) is not int or pid <= 0: + raise ValueError('Invalid desktop PID') + proc = Path('/proc') / str(pid) + birth = int((proc / 'stat').read_text().rsplit(') ', 1)[1].split()[19]) + uid = proc.stat().st_uid + command = (proc / 'cmdline').read_bytes() + scope = 'proc-exe' + try: + executable = (proc / 'exe').resolve(strict=True) + except PermissionError: + # Ubuntu's root-owned display server hides /proc/PID/exe from this user. + # Explicit, narrowly scoped public-metadata identity, NOT executable proof. + # Still requires a parent-pinned PID/birth/UID/argv and graphics-only NVML. + if (uid != 0 or command.split(b'\0')[0] != b'/usr/lib/xorg/Xorg' + or (proc / 'comm').read_text().strip() != 'Xorg'): + raise + executable = Path('/usr/lib/xorg/Xorg').resolve(strict=True) + scope = 'root-Xorg-public-metadata; actual-executable-unverified' + info = executable.stat() + result = {'pid': pid, 'starttime': birth, 'uid': uid, + 'command_sha256': hashlib.sha256(command).hexdigest(), + 'executable_identity_scope': scope, 'executable': str(executable), + 'device': info.st_dev, 'inode': info.st_ino, 'size': info.st_size, + 'mtime_ns': info.st_mtime_ns} + if hash_executable: + result['sha256'] = b.digest(executable) + if (birth != int((proc / 'stat').read_text().rsplit(') ', 1)[1].split()[19]) + or command != (proc / 'cmdline').read_bytes() or uid != proc.stat().st_uid + or scope == 'proc-exe' and executable != (proc / 'exe').resolve(strict=True)): + raise RuntimeError('Desktop process changed during identity capture') + return result + + +class GPUOwnership: + """Monitor owned GPU work plus explicitly approved existing desktop graphics. + + Unknown/new graphics, foreign compute (even an approved desktop PID), changed + process identity or inaccessible NVML fail closed. Desktop clients are NEVER + cleanup-owned. This is shared display monitoring, not GPU partitioning. + """ + class Process(ctypes.Structure): + _fields_ = [('pid', ctypes.c_uint), ('usedGpuMemory', ctypes.c_ulonglong), + ('gpuInstanceId', ctypes.c_uint), ('computeInstanceId', ctypes.c_uint)] + + def __init__(self, identity): + if (not isinstance(identity, dict) or set(identity) != {'library', 'sha256', 'uuid', 'index', 'desktop_clients'} + or identity['index'] != 0 or type(identity['index']) is not int): + raise ValueError('Exact GPU0 NVML identity required') + path = Path(identity['library']) + if not path.is_absolute() or path != path.resolve() or b.digest(path) != identity['sha256']: + raise ValueError('NVML library identity mismatch') + self.identity = identity + self.lib = ctypes.CDLL(str(path)) + self.call('nvmlInit_v2') + self.handle = ctypes.c_void_p() + self.call('nvmlDeviceGetHandleByIndex_v2', ctypes.c_uint(0), ctypes.byref(self.handle)) + uuid = ctypes.create_string_buffer(96) + self.call('nvmlDeviceGetUUID', self.handle, uuid, ctypes.c_uint(len(uuid))) + if uuid.value.decode() != identity['uuid']: + raise RuntimeError('GPU UUID mismatch') + clients = identity['desktop_clients'] + if (not isinstance(clients, list) or len(clients) > 64 + or any(not isinstance(x, dict) or type(x.get('pid')) is not int for x in clients) + or [x['pid'] for x in clients] != sorted({x['pid'] for x in clients})): + raise ValueError('Exact finite desktop client identities required') + self.desktop = {x['pid']: x for x in clients} + actual = self.clients() + if actual['compute'] or actual['graphics'] != set(self.desktop): + raise RuntimeError('GPU admission compute/approved-desktop mismatch') + for pid, expected in self.desktop.items(): + if desktop_identity(pid) != expected: + raise RuntimeError('GPU desktop admission identity mismatch') + + def call(self, name, *args): + code = getattr(self.lib, name)(*args) + if code != 0: + raise RuntimeError('NVML query failed: ' + name + ':' + str(code)) + + def clients(self): + result = {} + for kind, name in (('compute', 'nvmlDeviceGetComputeRunningProcesses_v2'), + ('graphics', 'nvmlDeviceGetGraphicsRunningProcesses_v2')): + count = ctypes.c_uint(1024) + processes = (self.Process * 1024)() + self.call(name, self.handle, ctypes.byref(count), processes) + if count.value > 1024: + raise RuntimeError('GPU ownership list overflow') + result[kind] = {processes[i].pid for i in range(count.value)} + return result + + def pids(self): + clients = self.clients() + return clients['compute'] | clients['graphics'] + + def check(self, owned, *, final=False): + if b.digest(self.identity['library']) != self.identity['sha256']: + raise RuntimeError('NVML drift') + clients = self.clients() + active = set(owned.discover()) + graphics = clients['graphics'] + desktop = graphics & set(self.desktop) + for pid in desktop: + expected = self.desktop[pid] + if not final: + expected = {k: v for k, v in expected.items() if k != 'sha256'} + if desktop_identity(pid, hash_executable=final) != expected: + raise RuntimeError('GPU desktop process identity changed') + if (clients['compute'] - active or graphics - active - desktop + or clients['compute'] & set(self.desktop) + or final and (clients['compute'] or graphics - desktop)): + raise RuntimeError('GPU ownership lost or residual task/foreign GPU client') + return {'uuid': self.identity['uuid'], 'compute_pids': sorted(clients['compute']), + 'approved_desktop_graphics_pids': sorted(desktop), + 'task_graphics_pids': sorted(graphics - desktop), + 'clean': not clients['compute'] and not (graphics - desktop), + 'scope': 'owned-compute-and-graphics.plus-pinned-desktop-graphics-only'} + + +def worker_payload(fd): + """RootXY entrypoint-specific authenticated single-use inherited FD.""" + with socket.socket(fileno=fd) as channel: + channel.settimeout(15) + pid, uid, _ = struct.unpack('3i', channel.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED, 12)) + command = Path(f'/proc/{pid}/cmdline').read_bytes().split(b'\0') + entry = command[1:] # only harmless interpreter flags may precede the actual entry script + while entry and entry[0] in (b'-u', b'-B', b'-s', b'-E', b'-I'): + entry = entry[1:] + script_matches = bool(entry and Path(entry[0].decode()).is_absolute() + and Path(entry[0].decode()) == ROOT / SCRIPT) + if (not script_matches or Path(f'/proc/{pid}/exe').resolve() != Path('/usr/bin/python3.12') + or not owner.ancestor(pid, os.getpid()) or uid != os.getuid() + or not (b'--run' in entry or b'--prepare' in entry)): + raise PermissionError('Authenticated RootXY supervisor required') + channel.sendall(str(os.getpid()).encode() + b'\n') + with channel.makefile('rb') as stream: + data = stream.read(1 << 20) + if len(data) >= 1 << 20: + raise ValueError('Oversized RootXY handoff') + payload = json.loads(data) + if payload['launch_identity'] != launch_identity(): + raise ValueError('RootXY launch drift') + return payload + + +def validate_completion(root, result, manifest, start): + data = read(root / 'budget.json') + expected = {'reserved_transitions': 1048576, 'reservations': 256, 'pending': None, + 'updates': {'92511': 256}, 'validation_reserved_transitions': 806400, + 'validation_attempted_control_calls': 5400, 'validation_completed_control_calls': 5400, + 'validation_evaluations_started': ['analytic_zero', 'frozen_u256', 'u0064', 'u0128', + 'u0192', 'u0256']} + if any(data.get(k) != v or result.get('budget', {}).get(k) != v for k, v in expected.items()): + raise ValueError('Incomplete optimization/validation debit evidence') + if (data['clock']['start_s'] != start or data['clock']['deadline_s'] != start + 7200 + or data['clock']['boot_id'] != b.boot_id()): + raise ValueError('Worker changed supervisor lifetime') + physics = result['physics'] + attempted, completed, init, rollout, startup = [physics[k] for k in ( + 'attempted_shared_calls', 'completed_shared_calls', 'initialization_attempted_shared_calls', + 'rollout_attempted_shared_calls', 'startup_completed_without_task_audit')] + if (any(type(x) is not int for x in (attempted, completed, init, rollout, startup)) + or not 0 <= startup <= init <= 1000 or rollout != 67960 + or attempted != completed or completed != init + rollout or completed > 73460): + raise ValueError('Actual shared physics completion/cap evidence mismatch') + safety = physics['safety'] + gates = read(ROOT / manifest['training']['safety_config'])['gates'] + values = [safety[k] for k in ('max_effort_velocity_ratio', 'max_joint_excess_rad', 'max_guide_vertical_N')] + if (safety['physics_samples'] != completed - startup or safety['finite'] is not True + or any(type(v) not in (int, float) or not math.isfinite(v) or v < 0 for v in values) + or values[0] > gates['normalized_effort_velocity_max'] + or values[1] > gates['joint_limit_excess_rad_max'] or values[2] != 0): + raise ValueError('Missing/failing global physical safety evidence') + for label in ('analytic_zero', 'frozen_u256'): + rows = read(root / ('validation_' + label + '.json'))['rows'] + if (len(rows) != 128 or [r.get('env') for r in rows] != list(range(128)) + or any(r.get('success') is not True or r.get('functional_audit', {}).get('sim_functional_passed') + is not True for r in rows)): + raise ValueError('Missing original baseline functional128/128 evidence') + + +def commit_candidate(root, result, manifest, budget): + """Independent validation and sole-authority last atomic commit; no worker lock.""" + # Import CPU protocol only after worker exit/cleanup; never initialize CUDA. + selection_path = ROOT / owner.PACKAGE / 'legs/contact_cycle_sim/rootxy_selection_v2.py' + spec = importlib.util.spec_from_file_location('_rootxy_selection_cpu', selection_path) + selection = importlib.util.module_from_spec(spec) + spec.loader.exec_module(selection) + if (result.get('status') != 'completed_provisional' or result.get('training_complete') is not True + or result.get('accepted_skill') is not False or result.get('checkpoints_selectable') is not False + or result.get('postclose_identity_verified') is not True): + raise ValueError('No successful provisional worker result') + ranked = [] + for update in (64, 128, 192, 256): + document = read(root / f'validation_u{update:04d}.json') + rows = document['rows'] + # Recheck original functional audit, not just worker's candidate label. + if any(r.get('functional_audit', {}).get('sim_functional_passed') is not True for r in rows): + raise ValueError('Missing original functional128/128 evidence') + key = selection.candidate_key(rows, update) + if key is not None: + ranked.append((key, update)) + candidate = result.get('candidate') + if not ranked: + if candidate is not None or result.get('outcome') != 'no_candidate': + raise ValueError('Ineligible worker candidate') + return None + key, update = min(ranked) + if (not isinstance(candidate, dict) or set(candidate) != {'path', 'sha256', 'update', 'selection_key'} + or candidate['update'] != update or candidate['path'] != f'checkpoint_u{update:04d}.pt' + or candidate['selection_key'] != list(key) or b.digest(root / candidate['path']) != candidate['sha256']): + raise ValueError('Candidate selection/hash mismatch') + lock = {'format': 'skillfactory.rootxy-supervisor-candidate.v2', 'candidate': candidate, + 'manifest_file_sha256': b.digest(root / 'manifest.json'), 'accepted_skill': False, + 'authority': 'waited_exit_postexit_identity_clean_owned_GPU_cleanup_original_eligibility', + 'downstream_requires_separate_grant': True} + budget.write(root / 'locked_candidate.json', lock, cleanup=True) + return lock + + +def supervise(args): + start = b.boottime() # includes admission/startup; never reset in worker + manifest, grant, policy = admission(args, start) + mode = 'prepare' if args.prepare else 'run' + if not hasattr(os, 'pidfd_open') or not hasattr(signal, 'pidfd_send_signal'): + raise RuntimeError('pidfd supervisor required') + owned = owner.OwnedProcesses() + gpu = GPUOwnership(grant['gpu_identity']) + environment = dict(os.environ, PYTHONDONTWRITEBYTECODE='1', PYTEST_DISABLE_PLUGIN_AUTOLOAD='1', + CUDA_VISIBLE_DEVICES=grant['gpu_identity']['uuid']) + owner.launch_command([], environment) + if mode == 'prepare': + b.durable_directory(args.output) + lock = (args.output / (mode + '-claim.lock')).open('xb') + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + budget = GuardBudget(args.output, start, mode) + monitor = child = capture = channels = None + errors, handlers = [], {} + clean = waited = False + result = None + filesystem = None + postexit = None + + def cleanup_pump(): + if capture is not None: + capture.pump() + if monitor is not None: + try: + monitor.check() + except BaseException as error: + if not any(e.startswith('native_cleanup:') for e in errors): + errors.append('native_cleanup:' + str(error)[-2048:]) + + try: + monitor = owner.NativeMonitor(policy, SimpleNamespace(boottime=b.boottime, STORAGE_SCOPE=STORAGE_SCOPE)) + budget.monitor = monitor + handlers = {s: signal.signal(s, lambda *_: setattr(budget, 'interrupted', True)) + for s in (signal.SIGTERM, signal.SIGINT)} + budget.write(args.output / (mode + '-reservation.json'), { + 'profile': PROFILE, 'mode': mode, 'start_boottime': start, 'deadline_boottime': budget.deadline, + 'boot_id': budget.boot, 'grant': grant, + 'reserved_optimization_environment_transitions': 0 if mode == 'prepare' else 1048576, + 'reserved_validation_environment_transitions': 0 if mode == 'prepare' else 806400, + 'reserved_rollout_shared_calls': 0 if mode == 'prepare' else 72460, + 'initialization_shared_calls_cap': 0 if mode == 'prepare' else 1000, + 'no_refund': True, 'accepted_skill': False}) + budget.write(args.output / (mode + '-manifest.json'), manifest) + if mode == 'prepare': + for relative, digest in manifest['sources'].items(): + destination = args.output / 'source_snapshot' / relative + destination.parent.mkdir(parents=True, exist_ok=True) + data = (ROOT / relative).read_bytes() + budget.check(len(data)) + b.durable_write(destination, data) + original_manifest = Path(args.manifest).read_bytes() + budget.check(len(original_manifest)) + b.durable_write(args.output / 'manifest.json', original_manifest) + filesystem = owner.bound_filesystem_inputs(original_manifest) + else: + filesystem = owner.bound_filesystem_inputs((args.output / 'training-binding.json').read_bytes()) + owner.verify_filesystem_inputs(filesystem) + budget.write(args.output / (mode + '-grant.json'), grant) + if ctypes.CDLL(None, use_errno=True).prctl(36, 1, 0, 0, 0) != 0: + raise RuntimeError('Subreaper setup failed') + native = args.output / 'native' + if native != native.resolve(): + raise ValueError('Native redirect symlink forbidden') + native.mkdir(exist_ok=True) + for directory in redirects(args.output, mode).values(): + Path(directory).mkdir(parents=True, exist_ok=False) + environment.update(redirects(args.output, mode)) + channels = socket.socketpair() + parent, child_channel = channels + child_channel.set_inheritable(True) + budget.check() + child = subprocess.Popen(owner.launch_command( + [str(ROOT / SCRIPT), '--_worker-fd', str(child_channel.fileno())], environment), + cwd=ROOT, env=environment, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + start_new_session=True, pass_fds=(child_channel.fileno(),)) + try: + owned.attach(child) + except BaseException: + owned.unknown = True + raise + child_channel.close() + capture = owner.StdoutCapture(child.stdout, args.output / (mode + '-stdout.log'), b) + budget.write(args.output / 'monitor-heartbeat.json', { + 'boot_id': budget.boot, 'boottime': b.boottime(), 'external_growth': budget.external_growth}) + owner.exchange_worker(child, parent, { + 'mode': mode, 'output': str(args.output), 'start': start, 'boot_id': budget.boot, + 'manifest_sha256': args.trusted_manifest_sha256, 'grant': grant, + 'launch_identity': launch_identity()}, budget, capture, owned, monitor, b) + next_heartbeat = 0 + while child.poll() is None or not capture.eof: + capture.check() + budget.check() + gpu.check(owned) + if b.boottime() >= next_heartbeat: + budget.write(args.output / 'monitor-heartbeat.json', { + 'boot_id': budget.boot, 'boottime': b.boottime(), 'external_growth': budget.external_growth}) + next_heartbeat = b.boottime() + .5 + time.sleep(.01) + child.wait(timeout=max(.01, budget.deadline - b.boottime())) + waited = True + result = read(args.output / (mode + '-worker-result.json')) + if child.returncode != 0: + raise RuntimeError('Worker nonzero exit') + except BaseException as error: + errors.append(str(error)[-8192:]) + finally: + try: + if channels: + for channel in channels: + channel.close() + if child is not None: + if capture is None and child.stdout is not None: + child.stdout.close() + try: + clean = owner.cleanup_group(child, budget.deadline, b, owned, cleanup_pump) + child.wait(timeout=max(.01, budget.deadline - b.boottime())) + waited = True + except BaseException as error: + errors.append('cleanup:' + str(error)[-2048:]) + if capture is not None: + try: + capture.close() + if capture.discarded or capture.error or not capture.eof: + raise RuntimeError('Incomplete/overflowed stdout') + except BaseException as error: + errors.append(str(error)[-2048:]) + try: + if not clean or not waited or child is None or child.returncode != 0 or owned.unknown: + raise RuntimeError('No clean independently waited worker exit') + gpu.check(owned, final=True) + verify_sources(manifest) + if filesystem is None: + raise RuntimeError('Missing prelaunch filesystem identity') + postexit = owner.verify_filesystem_inputs(filesystem) + binding_files = owner.bound_filesystem_inputs((args.output / 'training-binding.json').read_bytes()) + owner.verify_filesystem_inputs(binding_files) + if result is None or result.get('postclose_identity_verified') is not True: + raise RuntimeError('Missing postclose same-object verification') + if mode == 'prepare': + if result.get('status') != 'prepared_config_only' or result.get('physics_calls') != 0: + raise RuntimeError('PREP incomplete/config-only violation') + else: + if result.get('status') != 'completed_provisional' or result.get('training_complete') is not True: + raise RuntimeError('Training incomplete') + validate_completion(args.output, result, manifest, start) + budget.check(cleanup=True) + monitor.check(force=True) + except BaseException as error: + errors.append(str(error)[-4096:]) + summary = {'success': not errors, 'mode': mode, 'cleanup_clean': clean, 'waited_exit': waited, + 'returncode': None if child is None else child.returncode, 'errors': errors, + 'accepted_skill': False, 'postexit': postexit, 'owned': sorted(owned.known), + 'storage': None if monitor is None else monitor.report()} + budget.write(args.output / (mode + '-supervisor.json'), summary, cleanup=True) + if not errors and mode == 'prepare': + prepared = read(args.output / 'config-preparation-provisional.json') + prepared.update(supervisor_sha256=b.digest(args.output / 'prepare-supervisor.json'), + worker_result_sha256=b.digest(args.output / 'prepare-worker-result.json'), + postexit_verified=True) + try: + budget.write(args.output / 'preparation.json', prepared, cleanup=True) + except BaseException: + (args.output / 'preparation.json').unlink(missing_ok=True) + raise + if not errors and mode == 'run': + # Last operation capable of committing success. Failure leaves no candidate. + try: + commit_candidate(args.output, result, manifest, budget) + except BaseException as error: + (args.output / 'locked_candidate.json').unlink(missing_ok=True) + summary.update(success=False, errors=[*errors, 'candidate_commit:' + str(error)[-2048:]]) + budget.write(args.output / (mode + '-supervisor.json'), summary, cleanup=True) + raise + finally: + for sig, handler in handlers.items(): + signal.signal(sig, handler) + lock.close() + return 0 if not errors else 1 + + +def preimport_binding_runtime(binding): + """Finite binding-required imports, before config/inventory; no optional skips.""" + for name in dict.fromkeys((*binding.REQUIRED_RUNTIME, *binding.ISAAC_PINS)): + importlib.import_module(name) + + +class RuntimeAPI: + """Actual Kit recipe; callable only from authenticated worker handoff.""" + def __init__(self, payload, budget, guard): + self.payload, self.budget, self.guard = payload, budget, guard + self.output = Path(payload['output']) + self.mode = payload['mode'] + self.launcher = self.env = self.cfg = self.ledger = None + self.started = False + self.physics_hooks = None + self.manifest = read(self.output / 'manifest.json', payload['manifest_sha256']) + self.expected = None + + def start(self): + from isaaclab.app import AppLauncher + app_source = self.payload['launch_identity']['app_launcher'] + if (AppLauncher.__module__ != 'isaaclab.app.app_launcher' + or Path(sys.modules[AppLauncher.__module__].__file__).resolve() != Path(app_source['path']) + or b.digest(app_source['path']) != app_source['sha256']): + raise ValueError('Actual installed AppLauncher identity mismatch') + # Retain launcher even if initialization fails after creating its app. + self.launcher = AppLauncher.__new__(AppLauncher) + AppLauncher.__init__(self.launcher, headless=True, device='cuda:0', fast_shutdown=True, + kit_args=f'--/log/file={self.output / "native" / self.mode / "kit.log"}') + self.started = True + for name in ('rootxy_training_v2', 'rootxy_runner_v2', 'rootxy_binding_v2', 'rootxy_runtime_v2'): + setattr(self, name, importlib.import_module(owner.LEG + name)) + self.p = self.rootxy_training_v2 + self.binding = self.rootxy_binding_v2 + eb = importlib.import_module(owner.LEG + 'evaluation_binding') + self.eb = eb + for name in self.binding.ROLES: + if name != '__main__': + importlib.import_module(name) + # Do not depend on an evaluation recorder to trigger lazy backends. + preimport_binding_runtime(eb) + # Match the finite installed constructor preimports, never a whole interpreter scan. + owner.preimport_replicator() + owner.preimport_constructor_modules(eb) + self.constructor_export, self.constructor_metadata = owner.physx_constructor_export(eb, resolve=True) + self.cfg_class = importlib.import_module(owner.TASK + 'rootxy_cfg_v2').RootXYContactCycleEnvCfg + self.environment_class = importlib.import_module(owner.TASK + 'env').SimContactCycleEnv + + def configure(self): + self.cfg = self.cfg_class() + self.cfg.scene.num_envs = 128 + self.cfg.seed = 92511 + self.cfg.sim.device = 'cuda:0' + # This helper only verifies installed stepping and explicitly forwards + # fixed constructor video config fields; no evaluation environment created. + owner.preforward_video_recorder(self.cfg, importlib.import_module(owner.TASK + 'evaluation_env')) + owner.preimport_config_references(self.cfg, self.eb) + self.binding.normalize_config(self.cfg) # exact128-env PREconstruction only + import torch + torch.cuda.init() + torch.cuda.get_rng_state(torch.device('cuda:0')) + if self.mode == 'prepare': + self.inventory = self.binding.propose_inventory(ROOT, self.cfg, owner.proposed_native_layouts) + self.expected = self.binding.snapshot(ROOT, self.output, self.cfg, self.inventory, self.manifest) + else: + self.expected = read(self.output / 'training-binding.json', self.payload['grant']['binding_sha256']) + self.inventory = self.expected['dependency_inventory'] + if self.mode == 'run': + outer = self + class Ledger(self.p.RootXYBudgetLedger): + def guard(self, reserve_bytes=0): + outer.guard() + outer.budget.check(reserve_bytes) + return super().guard(reserve_bytes=reserve_bytes) + self.ledger = Ledger(self.output, self.manifest, launch_start_s=self.payload['start']) + self.ledger.__enter__() + + @contextmanager + def physics(self): + from isaaclab.sim import SimulationContext + with self.rootxy_runtime_v2.RootXYPhysicsHooks( + SimulationContext, self.environment_class, self.guard, + lambda value: self.budget.write(self.output / 'physics-counts.json', value), + preparation=self.mode == 'prepare') as hooks: + # Config construction/normalization itself is under zero-physics PREP hooks. + self.configure() + yield hooks + + def verify(self): + self.budget.check(cleanup=True) + verify_sources(self.manifest) + if self.cfg is None or self.expected is None: + raise RuntimeError('Startup did not establish actual config identity') + target, metadata = owner.physx_constructor_export(self.eb, expected=self.constructor_export) + if metadata != self.constructor_metadata: + raise ValueError('Constructor export drift') + self.binding.verify(self.expected, root=ROOT, output=self.output, cfg=self.cfg, + inventory=self.inventory, manifest=self.manifest) + if self.env is not None and getattr(self.env, 'cfg', None) is not self.cfg: + raise ValueError('Constructor substituted actual config object') + + def prepare(self): + self.budget.write(self.output / 'training-binding.json', self.expected) + result = {'format': PREPARATION_FORMAT, 'status': 'prepared_config_only', + 'manifest_sha256': self.payload['manifest_sha256'], + 'binding_sha256': b.digest(self.output / 'training-binding.json'), + 'artifact_root': str(self.output), 'physics_calls': 0, 'num_envs': 128, + 'runtime_qualified': False, 'p2_decision': 'pending', 'accepted_skill': False, + 'recipe': {'device': 'cuda:0', 'train_seed': 92511, 'development_seed': 93511, + 'environment_constructed': False, 'inference_executed': False, + 'profile': PROFILE, 'hooks': self.binding.HOOKS}, + 'ready_gaps': ['parent review of actual config-only binding', + 'separate approved P2 decision and fresh run grant', + 'immediate postconstructor actual-used identity and reset safety qualification']} + self.budget.write(self.output / 'config-preparation-provisional.json', result) + return result + + def construct(self): + self.guard() + self.env = self.environment_class.__new__(self.environment_class) + self.environment_class.__init__(self.env, cfg=self.cfg) + + def check_actual(self): + importlib.import_module(owner.TASK + 'rootxy_cfg_v2').verify_actual_config(self.env) + if self.env._physics_handles_decimation: + raise RuntimeError('Require scalar 500Hz physics stepping') + + def safety(self): + from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit + gates = read(ROOT / self.p.PILOT['safety_config'])['gates'] + return PhysicalSafetyAudit(self.env, gates) + + def learn(self): + import torch + from audit_contact_cycle_functional import evaluate + trace_path = ROOT / self.p.replay.CANDIDATE + trace_path = trace_path.parent / 'validation_u0256_trace.pt' + if b.digest(trace_path) != self.manifest['parity_trace']['sha256']: + raise ValueError('Saved CPU parity trace drift') + trace = torch.load(trace_path, map_location='cpu', weights_only=True) + observations = next(iter(trace.values()))['actor_input'][::100, 0].contiguous() + del trace + + def guard(): + self.guard() + self.physics_hooks.safety_boundary() + + def before_control(kind): + if kind not in ('optimization', 'validation'): + raise ValueError('Unknown control budget kind') + guard() + self.physics_hooks.mode = 'rollout' + + def after_control(kind): + self.physics_hooks.mode = 'initialization' + guard() + + hooks = self.rootxy_runner_v2.RuntimeHooks( + admit=lambda manifest: {'manifest_sha256': self.p.digest(manifest), 'p2_review_approved': True, + 'sole_cleanup_owner': True, 'native_monitor_active': True}, + guard=guard, verify_identity=self.verify, before_control=before_control, after_control=after_control, + progress=lambda value: guard(), finish=lambda value: guard()) + return self.rootxy_runner_v2.execute(self.env, ROOT, self.output, self.manifest, self.ledger, + observations, hooks, evaluate) + + def close_env(self): + if self.env is not None: + self.env.close() + + def close_app(self, code): + error = None + try: + if self.ledger is not None: + self.ledger.__exit__() + except BaseException as caught: + error = caught + app = None if self.launcher is None else getattr(self.launcher, '_app', None) + if app is not None: + # Fast shutdown may exit instead of returning: never pass zero after + # another teardown failure and rely on later Python unwinding. + app.close(exit_code=1 if error is not None else code) + if error is not None: + raise error + + +def run_worker(fd): + payload = worker_payload(fd) # no unauthenticated dictionary entrypoint + if payload['mode'] not in ('prepare', 'run') or payload['boot_id'] != b.boot_id(): + raise PermissionError('Invalid authenticated RootXY handoff') + root = checked_root(payload['output']) + reservation = read(root / (payload['mode'] + '-reservation.json')) + if (reservation['grant'] != payload['grant'] or reservation['start_boottime'] != payload['start'] + or reservation['boot_id'] != payload['boot_id']): + raise PermissionError('RootXY reservation mismatch') + budget = GuardBudget(root, payload['start'], payload['mode']) + + def guard(): + heartbeat = read(root / 'monitor-heartbeat.json') + if (heartbeat['boot_id'] != b.boot_id() + or not 0 <= b.boottime() - heartbeat['boottime'] <= 5): + raise RuntimeError('Native monitor heartbeat missing/stale') + budget.external_growth = heartbeat['external_growth'] + budget.check() + + handlers = {s: signal.signal(s, lambda *_: setattr(budget, 'interrupted', True)) + for s in (signal.SIGTERM, signal.SIGINT)} + # Pure hook module is loaded under a private identity before Kit, not a fake + # SkillFactory package. Actual runtime imports after AppLauncher use real paths. + spec = importlib.util.spec_from_file_location('_rootxy_lifecycle_cpu', ROOT / owner.PACKAGE / + 'legs/contact_cycle_sim/rootxy_runtime_v2.py') + runtime = importlib.util.module_from_spec(spec) + spec.loader.exec_module(runtime) + api = RuntimeAPI(payload, budget, guard) + verify_sources(api.manifest) + try: + runtime.worker_lifecycle(api, prepare=payload['mode'] == 'prepare', guard=guard, verify=api.verify, + publish=lambda value: budget.write( + root / (payload['mode'] + '-worker-result.json'), value, cleanup=True)) + return 0 + finally: + for sig, handler in handlers.items(): + signal.signal(sig, handler) diff --git a/scripts/standing_force_visualizer.py b/scripts/standing_force_visualizer.py new file mode 100644 index 0000000..b61d097 --- /dev/null +++ b/scripts/standing_force_visualizer.py @@ -0,0 +1,119 @@ +"""Visual-only world-frame disturbance arrows; no physics or network assets.""" + +import math + +import torch + + +def arrow_geometry(origins, forces, metres_per_newton=0.03): + """Return cylinder/cone instances along +Z, rotated using xyzw quaternions. + + The tail is the application point and the tip is origin + scale * force. + Zero forces are omitted (including pure torque disturbances). Inputs are + read-only. Prototype cylinders/cones have unit radius and unit height. + """ + if not math.isfinite(metres_per_newton) or metres_per_newton <= 0: + raise ValueError("Arrow scale must be finite and positive (m/N)") + if ( + origins.ndim != 2 + or origins.shape[1] != 3 + or forces.shape != origins.shape + or origins.device != forces.device + or origins.dtype != forces.dtype + or not origins.is_floating_point() + or not torch.isfinite(origins).all() + or not torch.isfinite(forces).all() + ): + raise ValueError("Expected finite matching floating world positions/forces [B,3]") + magnitude = torch.linalg.vector_norm(forces, dim=-1) + if not torch.isfinite(magnitude).all(): + raise ValueError("Force magnitude overflow") + active = magnitude > 1e-6 + origin, force, magnitude = origins[active], forces[active], magnitude[active] + direction = force / magnitude[:, None] + length = magnitude * metres_per_newton + if not torch.isfinite(length).all(): + raise ValueError("Arrow length overflow") + # atan2 avoids the cancellation of the [cross(z,d), 1+dot(z,d)] + # construction near -Z; exact -Z uses a 180-degree turn around X. + horizontal = torch.linalg.vector_norm(direction[:, :2], dim=-1) + axis = torch.stack((-direction[:, 1], direction[:, 0], torch.zeros_like(horizontal)), dim=-1) + axis /= horizontal.clamp_min(torch.finfo(forces.dtype).tiny)[:, None] + axis[horizontal == 0] = axis.new_tensor([1.0, 0.0, 0.0]) + half_angle = torch.atan2(horizontal, direction[:, 2]) * 0.5 + orientation = torch.cat((axis * half_angle.sin()[:, None], half_angle.cos()[:, None]), dim=-1) + head_length = torch.minimum(0.3 * length, length.new_tensor(0.05)) + shaft_length = length - head_length + shaft_radius = torch.minimum(0.04 * length, length.new_tensor(0.004)) + head_radius = torch.minimum(0.12 * length, length.new_tensor(0.012)) + shaft_center = origin + direction * (shaft_length / 2)[:, None] + head_center = origin + direction * (length - head_length / 2)[:, None] + count = len(origin) + return { + "translations": torch.cat((shaft_center, head_center)), + "orientations": torch.cat((orientation, orientation)), + "scales": torch.cat( + ( + torch.stack((shaft_radius, shaft_radius, shaft_length), dim=-1), + torch.stack((head_radius, head_radius, head_length), dim=-1), + ) + ), + "marker_indices": torch.cat( + ( + torch.zeros(count, dtype=torch.int32, device=origins.device), + torch.ones(count, dtype=torch.int32, device=origins.device), + ) + ), + } + + +def _make_markers(): + # Lazy Kit imports: pure geometry tests and non-visual runs need no renderer. + import isaaclab.sim as sim_utils + from isaaclab.markers import VisualizationMarkers, VisualizationMarkersCfg + + return VisualizationMarkers( + VisualizationMarkersCfg( + prim_path="/Visuals/AppliedDisturbanceForce", + markers={ + "shaft": sim_utils.CylinderCfg( + radius=1.0, + height=1.0, + axis="Z", + visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(1.0, 0.15, 0.02)), + collision_props=None, + rigid_props=None, + ), + "head": sim_utils.ConeCfg( + radius=1.0, + height=1.0, + axis="Z", + visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(1.0, 0.15, 0.02)), + collision_props=None, + rigid_props=None, + ), + }, + ) + ) + + +class DisturbanceForceArrows: + def __init__(self, metres_per_newton=0.03): + if not math.isfinite(metres_per_newton) or metres_per_newton <= 0: + raise ValueError("Arrow scale must be finite and positive (m/N)") + self.metres_per_newton = metres_per_newton + self.markers = _make_markers() + self.markers.set_visibility(False) + + @torch.no_grad() + def update(self, state): + # Actual last-applied disturbance ONLY, not guidance, total load, sensor + # reaction, or the next requested/random wrench. root()[0] is world XYZ. + geometry = arrow_geometry(state.root()[0], state.last_disturbance[:, :3], self.metres_per_newton) + visible = len(geometry["translations"]) > 0 + self.markers.set_visibility(visible) + if visible: + self.markers.visualize(**geometry) + + def close(self): + self.markers.set_visibility(False) diff --git a/scripts/train_contact_cycle.py b/scripts/train_contact_cycle.py new file mode 100644 index 0000000..675ebda --- /dev/null +++ b/scripts/train_contact_cycle.py @@ -0,0 +1,303 @@ +#!/usr/bin/env python3 +"""Explicitly authorized contact-cycle PPO. --prepare does not launch Kit or train. + +Independent testing is intentionally not an option. A continuation request or a +previous standing authorization is NOT a grant for this runner. +""" + +import argparse +import hashlib +import json +import math +import shutil +import signal +import sys +import traceback +from pathlib import Path + +from runtime_common import critical_log_lines, finish_run, write_json + +ROOT = Path(__file__).resolve().parents[1] + + +def execute(env, cfg, args, manifest, ledger, safety): + import torch + from SkillFactory.legs.contact_cycle.curriculum import EpisodeSampler + from SkillFactory.legs.contact_cycle.ppo import ( + CycleActorCritic, + collect_rollout, + features, + ppo_step, + restore_checkpoint, + restore_rng, + rng_state, + rollout_diagnostics, + save_checkpoint, + selection_score, + ) + from SkillFactory.legs.contact_cycle.training import artifact_bytes, save_trace_subset, source_identity + from SkillFactory.tasks.manager_based.contact_cycle.mdp import state + from SkillFactory.tasks.manager_based.contact_cycle.recording import CycleRecorder + + torch.set_num_threads(4) + s = state(env) + if (s.robot.is_fixed_base or env.cfg.sim.physics.solver_type != 0 + or env.cfg.sim.physics.enable_external_forces_every_iteration): + raise RuntimeError("Require validated floating-root PGS/external-force physics settings") + stop = {"requested": False} + signal.signal(signal.SIGTERM, lambda *_: stop.update(requested=True)) + signal.signal(signal.SIGINT, lambda *_: stop.update(requested=True)) + runtime = {"torch": str(torch.__version__), "device": str(env.device), "physics_dt": env.physics_dt, + "control_dt": env.step_dt, "num_envs": env.num_envs} + identity = {"manifest": manifest, "runtime": runtime} + runtime_path = args.output/"runtime_identity.json" + if runtime_path.exists() and json.loads(runtime_path.read_text()) != runtime: + raise RuntimeError("Runtime identity changed across resume") + ledger.write_json(runtime_path, runtime) + thresholds = json.loads((ROOT/cfg["proposal"]).read_text())["gates"] + algo = {**cfg["algorithm"], "horizon": cfg["horizon"]} + + def guard(): + ledger.guard() + safety.check() + if stop["requested"]: + raise InterruptedError("Operator requested stop; resume from last atomic learner checkpoint") + + def check_source(): + guard() + if source_identity(ROOT, cfg, args.config) != manifest["sources"]: + raise RuntimeError("Training inputs changed; refusing continuation/checkpoint") + if args.console_log is not None and critical_log_lines(args.console_log): + raise RuntimeError("Critical runtime log; refusing successful completion") + + def save(path, model, optimizer, seed_identity, update, sampler, best): + guard() + remaining = cfg["max_artifact_bytes"] - artifact_bytes(args.output) - 1024*1024 + save_checkpoint(path, model, optimizer, seed_identity, update, sampler, best, ledger.data, + max_bytes=remaining) + ledger.guard() + + candidates = [] + for seed in cfg["seeds"]: + guard() + seed_dir = args.output/f"seed_{seed}" + seed_dir.mkdir(exist_ok=True) + seed_identity = {**identity, "seed": seed} + torch.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + model = CycleActorCritic(cfg["algorithm"]["hidden"], cfg["algorithm"]["initial_std"]).to(env.device) + optimizer = torch.optim.Adam(model.parameters(), lr=cfg["algorithm"]["learning_rate"], eps=1e-5) + sampler = EpisodeSampler(s.cc, env.num_envs, seed) + latest = seed_dir/"latest.pt" + update, best = 0, None + if latest.exists(): + checkpoint = restore_checkpoint(latest, model, optimizer, seed_identity, sampler) + update, best = checkpoint["update"], checkpoint["best"] + if ledger.data["pending"] is not None and ledger.data["pending"]["seed"] != seed: + if update != cfg["updates_per_seed"]: + raise RuntimeError("Unexpected unfinished earlier seed") + else: + ledger.reconcile_checkpoint(seed, update) + s.set_sampler(sampler) + x = features(env.reset()[0]["policy"]) + if not latest.exists(): + save(latest, model, optimizer, seed_identity, 0, sampler, None) + + def log(row): + ledger.append_json(seed_dir/"metrics.jsonl", row) + + @torch.no_grad() + def validation(): + check_source() + saved_rng = rng_state() + validation_sampler = EpisodeSampler(s.cc, env.num_envs, cfg["validation_seed"], split="validation") + s.set_sampler(validation_sampler) + observation = env.reset()[0]["policy"] + inputs = {"scenario": s.scenarios.cpu().tolist(), "commands": s.schedule.commands.cpu().tolist(), + "points": s.sampled_points.cpu().tolist(), "delays": s.schedule.delays.cpu().tolist()} + if set(inputs["scenario"]) != set(range(4)): + raise RuntimeError("Validation family coverage missing") + steps = round(sum(s.cc["phase_durations_s"])/env.step_dt) + try: + with CycleRecorder(env) as recorder: + for step in range(steps): + guard() + action, mean, _, _ = model.act(features(observation), deterministic=True) + recorder.before_control(observation, action, mean, + model.log_std.clamp(math.log(0.02), math.log(0.5)).exp()) + obs, _, terminated, truncated, _ = env.step(action) + recorder.end_control(terminated, truncated, step == steps - 1) + observation = obs["policy"] + rows, trace = recorder.summarize(thresholds) + score = selection_score(rows) + row = {"kind": "validation_only", "seed": seed, "validation_seed": cfg["validation_seed"], + "update": update, "reservation": ledger.data["reservations"], "score": score, + "failure_fraction": sum(not r["success"] for r in rows)/len(rows), + "inputs": inputs, "rows": rows, "accepted_skill": False} + label = f"validation_u{update:04d}_r{ledger.data['reservations']:06d}" + ledger.write_json(seed_dir/f"{label}.json", row) + # Keep bounded real failure traces: first four failed first episodes (or first four successes). + selected = ([r["env"] for r in rows if not r["success"]] or list(range(env.num_envs)))[:4] + save_trace_subset(seed_dir/f"{label}_trace.pt", trace, selected, recorder.stops, + lambda size: ledger.guard(reserve_bytes=size + 1024*1024)) + save_trace_subset(seed_dir/f"{label}_initial.pt", + {k: v[None] for k, v in recorder.initial_state.items()}, selected, + [1]*env.num_envs, lambda size: ledger.guard(reserve_bytes=size + 1024*1024)) + ledger.guard() + return row + finally: + s.set_sampler(sampler) + restore_rng(saved_rng) + + while update < cfg["updates_per_seed"]: + check_source() + sampler.set_update(update + 1) + ledger.reserve_rollout(seed, update + 1) + data, x, failures, timeouts = collect_rollout(env, model, x, algo, guard) + guard() + diagnostic_row = rollout_diagnostics(data, algo) + if diagnostic_row["missing_diagnostic_samples"]: + raise RuntimeError("Missing control-clock diagnostics; refusing optimizer update") + learning = ppo_step(model, optimizer, data, algo) + update += 1 + if update % cfg["validation_interval"] == 0: + validation_row = validation() + log(validation_row) + if best is None or validation_row["score"] < best["score"]: + relative = f"candidate_u{update:04d}_r{ledger.data['reservations']:06d}.pt" + best = {"update": update, "score": validation_row["score"], "path": relative, + "failure_fraction": validation_row["failure_fraction"]} + save(seed_dir/relative, model, optimizer, seed_identity, update, sampler, best) + best["sha256"] = hashlib.sha256((seed_dir/relative).read_bytes()).hexdigest() + x = features(env.reset()[0]["policy"]) + # Checkpoint BEFORE durable progress commit; ledger reconciles the crash window on resume. + check_source() + save(latest, model, optimizer, seed_identity, update, sampler, best) + ledger.commit_update(seed, update) + log({"kind": "training", "update": update, "seed": seed, "learner": learning, + "terminated": failures, "timeouts": timeouts, "curriculum_stage": sampler.stage, + "episode_counts": sampler.episodes.tolist(), + "diagnostics": diagnostic_row, + "reserved_transitions": ledger.data["reserved_transitions"]}) + if best is None or hashlib.sha256((seed_dir/best["path"]).read_bytes()).hexdigest() != best["sha256"]: + raise RuntimeError("Missing/changed fixed candidate") + candidates.append({"seed": seed, **best, "path": str((seed_dir/best["path"]).relative_to(args.output))}) + check_source() + scope = ("diagnostic_pilot" if cfg["format"] == "skillfactory.contact-cycle-diagnostic-training.v1" + else "three_seed_study") + locked = {"candidates": candidates, "training_scope": scope, + "accepted_skill": False, "independent_test_executed": False, + "validation_screen_passed": all(c["failure_fraction"] <= 0.05 for c in candidates), + "rule": "Never reselect these candidates using independent test outcomes"} + path = args.output/"locked_candidates.json" + if path.exists() and json.loads(path.read_text()) != locked: + raise RuntimeError("Cannot overwrite a previous candidate lock") + ledger.write_json(path, locked) + return {"passed": True, "training_complete": True, "training_scope": scope, + "accepted_skill": False, "locked": locked, + "budget": ledger.data} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + mode = parser.add_mutually_exclusive_group(required=True) + mode.add_argument("--prepare", action="store_true") + mode.add_argument("--train", action="store_true") + parser.add_argument("--config", type=Path, default=ROOT/"configs/contact_cycle/training_v1.json") + parser.add_argument("--output", type=Path, required=True) + parser.add_argument("--authorization", type=Path) + parser.add_argument("--resume", action="store_true") + parser.add_argument("--console-log", type=Path) + parser.add_argument("--headless", action="store_true") + parser.add_argument("--device", default="cuda:0") + args = parser.parse_args() + frozen = (ROOT/"releases").resolve() + if args.output.resolve() == frozen or frozen in args.output.resolve().parents: + parser.error("Preparation/training must not write into frozen releases") + from SkillFactory.legs.contact_cycle.training import ( + BudgetLedger, + digest, + make_manifest, + require_authorization, + ) + + manifest = make_manifest(ROOT, args.config) + cfg = manifest["training"] + if args.prepare: + if args.resume or args.authorization is not None: + parser.error("Preparation neither resumes nor authorizes training") + snapshot_size = sum((ROOT/p).stat().st_size for p in manifest["sources"]) + if snapshot_size + len(json.dumps(manifest).encode()) + 1024*1024 > cfg["max_artifact_bytes"]: + raise ValueError("Preparation snapshots exceed total artifact budget") + args.output.mkdir(parents=True, exist_ok=False) + write_json(args.output/"manifest.json", manifest) + for relative in manifest["sources"]: + target = args.output/"source_snapshot"/relative + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(ROOT/relative, target) + write_json(args.output/"preparation.json", {"prepared": True, "authorized": False, "trained": False, + "manifest_sha256": digest(manifest)}) + print(f"Prepared only; no Kit/optimizer/independent test: {args.output}", flush=True) + return + # Fail BEFORE importing/launching Isaac/Kit and BEFORE starting the budget clock. + grant = require_authorization(args.authorization, manifest) + if not (args.output/"manifest.json").is_file() or json.loads((args.output/"manifest.json").read_text()) != manifest: + raise ValueError("Use an immutable prepared directory matching the current sources/config") + if (args.output/"budget.json").exists() != args.resume: + raise ValueError("Existing experiment requires --resume; new experiment must not use it") + if not args.headless: + raise ValueError("Initial authorized run is headless only") + if args.console_log is None or not args.console_log.resolve().is_relative_to(args.output.resolve()): + raise ValueError("Supervised console log must be recorded inside the shared artifact budget directory") + app = env = safety = None + result = {"passed": False, "training_complete": False, "accepted_skill": False, + "training_scope": ("diagnostic_pilot" + if cfg["format"] == "skillfactory.contact-cycle-diagnostic-training.v1" + else "three_seed_study")} + code = 1 + with BudgetLedger(args.output, manifest) as ledger: + authorization_record = args.output/"authorization_record.json" + if authorization_record.exists() and json.loads(authorization_record.read_text()) != grant: + raise ValueError("Cannot replace the experiment's authorization record") + ledger.write_json(authorization_record, grant) + previous_result = args.output/"result.json" + if previous_result.exists(): + history = args.output/"attempt_history" + history.mkdir(exist_ok=True) + number = len(list(history.glob("result_*.json"))) + 1 + ledger.write_json(history/f"result_{number:04d}.json", json.loads(previous_result.read_text())) + try: + from isaaclab.app import AppLauncher + + app = AppLauncher(headless=True, device=args.device).app + from SkillFactory.tasks.manager_based.contact_cycle.cfg import ContactCycleEnvCfg + from SkillFactory.tasks.manager_based.contact_cycle.env import ContactCycleEnv + + environment_cfg = ContactCycleEnvCfg(cycle_config=str(ROOT/cfg["task_config"])) + environment_cfg.scene.num_envs = cfg["num_envs"] + environment_cfg.sim.device = args.device + environment_cfg.seed = cfg["seeds"][0] + env = ContactCycleEnv(cfg=environment_cfg) + from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit + + thresholds = json.loads((ROOT/cfg["proposal"]).read_text())["gates"] + with PhysicalSafetyAudit(env, thresholds) as safety: + result = execute(env, cfg, args, manifest, ledger, safety) + code = 0 + except BaseException: + result["error"] = traceback.format_exc() + traceback.print_exc() + finally: + result["budget"] = ledger.data + if safety is not None: + result["physics_safety"] = safety.report() + if app is not None: + finish_run(app, env, args.output/"result.json", result, code) + else: + write_json(args.output/"result.json", result) + if code: + sys.exit(code) + + +if __name__ == "__main__": + main() diff --git a/scripts/train_cycle_sim_residual.py b/scripts/train_cycle_sim_residual.py new file mode 100644 index 0000000..bc17c12 --- /dev/null +++ b/scripts/train_cycle_sim_residual.py @@ -0,0 +1,291 @@ +#!/usr/bin/env python3 +"""Fresh 46D residual PPO pilot. --prepare neither launches Kit nor authorizes learning. + +Only a parent-bound final manifest grant permits --train. No resume/warm-start CLI. +The sole actual training launch must be supervised with a <=3480s outer timeout. +""" + +import argparse +import hashlib +import json +import math +import signal +import sys +import traceback +from pathlib import Path + +from runtime_common import critical_log_lines + +ROOT = Path(__file__).resolve().parents[1] + + +def execute(env, cfg, args, manifest, ledger, guard): + import torch + from audit_contact_cycle_functional import evaluate + from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic + from SkillFactory.legs.contact_cycle_sim.ppo import ( + features, + save_checkpoint, + selection_key, + ) + from SkillFactory.legs.contact_cycle_sim.training import ( + MODEL_CONFIG, + artifact_bytes, + checkpoint_then_commit, + choose_candidate, + isolated_validation_rng, + optimize_reserved_rollout, + parameter_deltas, + save_trace_subset, + source_identity, + validation_rows, + verify_snapshot, + ) + from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state + from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder + + torch.set_num_threads(4) + s = state(env) + if (s.robot.is_fixed_base or env.cfg.sim.physics.solver_type != 0 + or env.cfg.sim.physics.enable_external_forces_every_iteration + or env.cfg.terminations.failed is None or env.physics_dt != .002 or env.step_dt != .01 + or env.num_envs != 128 or env.cfg.episode_length_s != 9): + raise RuntimeError("Require unchanged floating-root PGS, active failures, 500Hz physics and 9s nominal task") + runtime = {"torch": str(torch.__version__), "device": str(env.device), "physics_dt": env.physics_dt, + "control_dt": env.step_dt, "num_envs": env.num_envs} + identity = {"manifest": manifest, "runtime": runtime, "seed": cfg["seeds"][0], "model_config": MODEL_CONFIG} + ledger.write_json(args.output / "runtime_identity.json", identity) + functional = json.loads((ROOT / cfg["functional_config"]).read_text()) + algo = {**cfg["algorithm"], "horizon": cfg["horizon"]} + seed = cfg["seeds"][0] + torch.manual_seed(seed) + torch.cuda.manual_seed_all(seed) + model = SimResidualActorCritic(**MODEL_CONFIG).to(env.device) + initial = {k: v.detach().cpu().clone() for k, v in model.named_parameters()} + optimizer = torch.optim.Adam(model.parameters(), lr=algo["learning_rate"], eps=algo["adam_eps"]) + best = None + + def check_source(): + guard() + if source_identity(ROOT, cfg, args.config) != manifest["sources"]: + raise RuntimeError("Bound sources/assets/config changed; refusing optimization or publication") + verify_snapshot(args.output, manifest) + if critical_log_lines(args.console_log): + raise RuntimeError("Critical simulator console log") + + def save(path, update): + check_source() + remaining = cfg["max_artifact_bytes"] - artifact_bytes(args.output) - 1048576 + size = save_checkpoint(path, model, optimizer, identity, update, best, ledger.data, + MODEL_CONFIG, max_bytes=remaining) + ledger.guard() + return size + + @torch.no_grad() + def validation(update): + check_source() + with isolated_validation_rng(env, cfg["validation_seed"], seed): + observation = features(env.reset()[0]["policy"]) + if bool(s.scenarios.any() or s.stand_only.any()): + raise RuntimeError("Validation must contain only nominal complete cycles") + returns = torch.zeros(env.num_envs, device=env.device) + live = torch.ones(env.num_envs, dtype=torch.bool, device=env.device) + with SimCycleRecorder(env) as recorder: + for step in range(900): + guard() + action, mean, _, _ = model.act(observation, deterministic=True) + recorder.before_control(observation, action, mean, + model.log_std.clamp(math.log(.02), math.log(.5)).exp()) + obs, reward, terminated, truncated, _ = env.step(action) + if not torch.isfinite(reward).all(): + raise RuntimeError("Nonfinite validation reward") + returns += reward * live + live &= ~(terminated | truncated) + recorder.end_control(terminated, truncated, step == 899) + observation = features(obs["policy"]) + trace = {k: torch.stack([frame[k] for frame in recorder.frames]) for k in recorder.frames[0]} + # Recorded buffers are copies; release the original per-frame storage before serialization. + recorder.frames.clear() + rows = validation_rows(trace, recorder.stops, recorder.early, s.nominal, functional, evaluate) + for row, total in zip(rows, returns.cpu().tolist()): + row["episode_return"] = total + result = {"kind": ("untrained_zero_residual_network" if update == 0 + else "analytic_reference_plus_learned_residual"), + "trained": update > 0, "update": update, "seed": seed, + "validation_seed": cfg["validation_seed"], "rows": rows, + "selection_key": list(selection_key(rows)), "parameter_deltas": parameter_deltas(model, initial), + "accepted_skill": False, "independent_test_executed": False, + "audit_controller_kind": "learned_policy_metadata_interface_only"} + label = f"validation_u{update:04d}" + ledger.write_json(args.output / f"{label}.json", result) + selected = ([r["env"] for r in rows if not r["success"]] or list(range(env.num_envs)))[:4] + def reserve(size): + ledger.guard(reserve_bytes=size + 1048576) + save_trace_subset(args.output / f"{label}_trace.pt", trace, selected, recorder.stops, reserve) + save_trace_subset(args.output / f"{label}_initial.pt", + {k: v[None] for k, v in recorder.initial_state.items()}, selected, + [1] * env.num_envs, reserve) + check_source() + return result + + save(args.output / "untrained_u0000.pt", 0) + baseline = validation(0) + if not all(r["success"] for r in baseline["rows"]) or len(baseline["rows"]) != 128: + raise RuntimeError("u0 did not pass 128/128 functional cycles; stop before the first training rollout") + comparisons = [] + x = features(env.reset()[0]["policy"]) + for update in range(1, cfg["updates_per_seed"] + 1): + x, failures, timeouts, learning = optimize_reserved_rollout( + env, model, optimizer, x, algo, seed, update, ledger, guard, check_source) + if update in cfg["validation_updates"]: + row = validation(update) + if choose_candidate(best, update, row["rows"]): + name = f"candidate_u{update:04d}.pt" + best = {"update": update, "selection_key": row["selection_key"], "path": name} + save(args.output / name, update) + best["sha256"] = hashlib.sha256((args.output / name).read_bytes()).hexdigest() + comparisons.append({"update": update, "selection_key": row["selection_key"], + "parameter_deltas": row["parameter_deltas"], + "mean_height_rms_m": sum(r["height_rms_m"] for r in row["rows"]) / 128, + "mean_return": sum(r["episode_return"] for r in row["rows"]) / 128, + "mean_actual_residual_rms_rad": sum( + r["actual_residual_rms_rad"] for r in row["rows"]) / 128}) + x = features(env.reset()[0]["policy"]) + # Pending reservation remains charged on ANY failure; no automatic recovery. + latest_bytes = checkpoint_then_commit( + ledger, seed, update, lambda: save(args.output / "latest.pt", update)) + ledger.append_json(args.output / "metrics.jsonl", {"update": update, "learner": learning, + "failures": failures, "timeouts": timeouts, "latest_checkpoint_bytes": latest_bytes, + "reserved_transitions": ledger.data["reserved_transitions"]}) + check_source() + if (best is None or best["update"] == 0 + or hashlib.sha256((args.output / best["path"]).read_bytes()).hexdigest() != best["sha256"]): + raise RuntimeError("Missing or changed trained candidate") + locked = {"candidate": best, "accepted_skill": False, "independent_test_executed": False, + "training_scope": "nominal_simulation_residual_pilot", + "rule": "failure fraction then all-row cost; earliest tie; never reselect from later outcomes"} + ledger.write_json(args.output / "locked_candidate.json", locked) + ledger.write_json(args.output / "baseline_comparison.json", { + "baseline_kind": baseline["kind"], "baseline_selection_key": baseline["selection_key"], + "baseline_mean_height_rms_m": sum(r["height_rms_m"] for r in baseline["rows"]) / 128, + "baseline_mean_return": sum(r["episode_return"] for r in baseline["rows"]) / 128, + "baseline_mean_actual_residual_rms_rad": sum(r["actual_residual_rms_rad"] for r in baseline["rows"]) / 128, + "updated_models": comparisons, "selection_changed_by_comparison": False, + "note": "Reference already passed at u0. Nonzero parameter deltas alone do not demonstrate improvement.", + "accepted_skill": False}) + return {"passed": True, "training_complete": True, "accepted_skill": False, "locked": locked} + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + mode = parser.add_mutually_exclusive_group(required=True) + mode.add_argument("--prepare", action="store_true") + mode.add_argument("--train", action="store_true") + parser.add_argument("--config", type=Path, default=ROOT / "configs/contact_cycle/sim_training_v1.json") + parser.add_argument("--output", type=Path, required=True) + parser.add_argument("--authorization", type=Path) + parser.add_argument("--console-log", type=Path) + parser.add_argument("--supervisor-report", type=Path) + parser.add_argument("--headless", action="store_true") + parser.add_argument("--device", default="cuda:0") + args = parser.parse_args(argv) + from SkillFactory.legs.contact_cycle_sim.training import ( + FreshBudgetLedger, + assert_no_conflicting_processes, + make_manifest, + prepare, + require_authorization, + validate_prepared, + writable_output, + ) + + args.output = writable_output(ROOT, args.output) + if args.prepare: + if args.authorization is not None or args.console_log is not None or args.supervisor_report is not None: + parser.error("Preparation is not authorization or a supervised launch") + prepare(ROOT, args.config, args.output) + print(f"Prepared only; no Kit, budget or optimizer: {args.output}", flush=True) + return + # Every gate here runs BEFORE importing Kit and BEFORE the ledger clock starts. + manifest = make_manifest(ROOT, args.config) + grant = require_authorization(args.authorization, manifest) + validate_prepared(ROOT, args.output, manifest) + if not args.headless: + raise ValueError("Pilot must be headless and externally supervised") + if (args.console_log is None or args.console_log.resolve() != args.output / "console.log" + or args.supervisor_report is None or args.supervisor_report.resolve() != args.output / "supervisor.json" + or not args.console_log.is_file() or not args.supervisor_report.is_file()): + raise ValueError("Require live supervisor console.log and supervisor.json inside artifact budget") + supervisor = json.loads(args.supervisor_report.read_text()) + if supervisor.get("status") != "running" or supervisor.get("passed") is not False: + raise ValueError("Require a running external supervisor") + assert_no_conflicting_processes() + cfg = manifest["training"] + stop = {"requested": False} + for sig in (signal.SIGTERM, signal.SIGINT): + signal.signal(sig, lambda *_: stop.update(requested=True)) + app = env = safety = None + result = {"passed": False, "training_complete": False, "accepted_skill": False} + code = 1 + with FreshBudgetLedger(args.output, manifest) as ledger: + def guard(): + ledger.guard() + if stop["requested"]: + raise InterruptedError("Cooperative stop; pending work remains charged; no retry/resume") + if safety is not None: + safety.check() + + ledger.write_json(args.output / "authorization_record.json", grant) + try: + guard() + from isaaclab.app import AppLauncher + + app = AppLauncher(headless=True, device=args.device).app + guard() + from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit + from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg + from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv + + environment_cfg = SimContactCycleEnvCfg(residual_config=str(ROOT / cfg["task_config"])) + environment_cfg.scene.num_envs = cfg["num_envs"] + environment_cfg.sim.device = args.device + environment_cfg.seed = cfg["seeds"][0] + env = SimContactCycleEnv(cfg=environment_cfg) + thresholds = json.loads((ROOT / cfg["safety_config"]).read_text())["gates"] + with PhysicalSafetyAudit(env, thresholds) as safety: + result = execute(env, cfg, args, manifest, ledger, guard) + guard() + code = 0 + except BaseException: + result.update(passed=False, error=traceback.format_exc()) + traceback.print_exc() + finally: + if env is not None: + try: + env.close() + except BaseException: + result.update(passed=False, shutdown_error=traceback.format_exc()) + code = 1 + result["budget"] = ledger.data + if safety is not None: + result["physics_safety"] = safety.report() + try: + guard() + if critical_log_lines(args.console_log): + raise RuntimeError("Critical console log during shutdown") + except BaseException: + result.update(passed=False, final_guard_error=traceback.format_exc()) + code = 1 + result["status"] = "completed" if code == 0 else "failed" + # Never bypass the artifact/deadline guard to publish a success on failure. + try: + ledger.write_json(args.output / "result.json", result) + finally: + if app is not None: + app.close(exit_code=code) + if code: + sys.exit(code) + + +if __name__ == "__main__": + main() diff --git a/scripts/train_cycle_sim_rootxy_v2.py b/scripts/train_cycle_sim_rootxy_v2.py new file mode 100644 index 0000000..5a51bd5 --- /dev/null +++ b/scripts/train_cycle_sim_rootxy_v2.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +"""RootXY-v2 dedicated supervised adapter; no mode authorizes itself. + +--check-only/--plan are read-only CPU operations. --prepare is separately granted +Kit config-only preparation (zero environments/physics). --run requires exact +preparation plus new parent grant and approved P2 decision. Neither runs by default. +""" +import argparse +import importlib +import importlib.util +import json +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +def cpu_protocol(): + # Real file-backed private CPU namespace, not fake SkillFactory registration. + # Never reuse this namespace for runtime binding or simulator construction. + name = "_rootxy_cpu_legs" + if name not in sys.modules: + spec = importlib.util.spec_from_file_location( + name, ROOT / "source/SkillFactory/SkillFactory/legs/__init__.py", + submodule_search_locations=[str(ROOT / "source/SkillFactory/SkillFactory/legs")]) + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + spec.loader.exec_module(module) + return importlib.import_module(name + ".contact_cycle_sim.rootxy_training_v2") + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + mode = parser.add_mutually_exclusive_group(required=True) + mode.add_argument("--check-only", action="store_true") + mode.add_argument("--plan", action="store_true") + mode.add_argument("--_worker-fd", type=int, help=argparse.SUPPRESS) + mode.add_argument("--prepare", action="store_true") + mode.add_argument("--run", action="store_true") + parser.add_argument("--config", type=Path, default=ROOT / "configs/contact_cycle/sim_training_rootxy_v2.json") + parser.add_argument("--output", type=Path) + parser.add_argument("--authorization", type=Path) + parser.add_argument("--trusted-authorization-sha256") + parser.add_argument("--manifest", type=Path) + parser.add_argument("--trusted-manifest-sha256") + parser.add_argument("--storage-policy", type=Path) + parser.add_argument("--trusted-storage-policy-sha256") + parser.add_argument("--p2-decision", type=Path) + args = parser.parse_args(argv) + if args._worker_fd is not None: + import rootxy_launch_v2 as adapter + return adapter.run_worker(args._worker_fd) + if args.run or args.prepare: + if args.output is None or any(getattr(args, k) is None for k in ( + 'authorization', 'trusted_authorization_sha256', 'manifest', 'trusted_manifest_sha256', + 'storage_policy', 'trusted_storage_policy_sha256')) or args.run and args.p2_decision is None: + raise PermissionError('RootXY-v2 admission INCOMPLETE: exact new manifest/preparation/native policy/' + 'parent grant and RUN P2 decision required; no launch authorized') + if args.config.resolve() != ROOT / 'configs/contact_cycle/sim_training_rootxy_v2.json': + raise ValueError('Fixed RootXY config required') + import rootxy_launch_v2 as adapter + return adapter.supervise(args) + if args.authorization is not None: + parser.error("CPU checks/preparation never accept launch authorization") + if args.output is not None: + parser.error('CPU check/plan writes no output') + protocol = cpu_protocol() + manifest = protocol.make_manifest(ROOT, args.config) + if args.plan: + print(json.dumps(manifest, sort_keys=True, separators=(',', ':'), allow_nan=False)) + else: + print(json.dumps({'checked': True, 'prepared': False, 'authorized': False, + 'launch_integration_complete': True, 'runtime_qualified': False, + 'manifest_sha256': protocol.digest(manifest), + 'task_id': protocol.TASK_ID, 'accepted_skill': False})) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/train_leg_skill.py b/scripts/train_leg_skill.py index a996c3e..b79c2db 100644 --- a/scripts/train_leg_skill.py +++ b/scripts/train_leg_skill.py @@ -17,12 +17,7 @@ from pathlib import Path from runtime_common import critical_log_lines, write_json -def main(): - import torch - from SkillFactory.legs.policy import LegActorCritic, load_policy, ppo_update, save_policy - from SkillFactory.legs.spec import load_bundle - from SkillFactory.legs.tokens import FEATURE_SCHEMA - +def parse_args(argv=None): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--bundles", type=Path, nargs="+", required=True) parser.add_argument("--num_envs", type=int, default=16, help="Per-asset environment count") @@ -32,14 +27,60 @@ def main(): parser.add_argument("--episode-length", type=float, default=0.2) parser.add_argument("--seed", type=int, default=42) parser.add_argument("--device", default="cuda:0") + parser.add_argument( + "--viz", + "--visualizer", + choices=("none", "kit"), + help="Worker visualization (omitted: worker default). Kit windows restart for each asset/update.", + ) parser.add_argument("--output", type=Path, default=Path("logs/leg_interface/train_smoke")) - args = parser.parse_args() + args = parser.parse_args(argv) if ( min(args.num_envs, args.max_joints, args.horizon, args.updates) < 1 or not math.isfinite(args.episode_length) or args.episode_length <= 0 ): parser.error("Counts and episode length must be positive") + return args + + +def worker_command(args, bundle_path, weights, directory, seed): + command = [ + sys.executable, + str(Path(__file__).with_name("leg_worker.py")), + "--bundle", + str(bundle_path.resolve()), + "--mode", + "rollout", + "--weights", + str(weights.resolve()), + "--num_envs", + str(args.num_envs), + "--max-joints", + str(args.max_joints), + "--horizon", + str(args.horizon), + "--episode-length", + str(args.episode_length), + "--seed", + str(seed), + "--device", + args.device, + "--output", + str(directory.resolve()), + ] + if args.viz is not None: + command.extend(["--viz", args.viz]) + return command + + +def main(): + args = parse_args() + import torch + from SkillFactory.legs.policy import LegActorCritic, load_policy, ppo_update, save_policy + from SkillFactory.legs.spec import load_bundle + from SkillFactory.legs.tokens import FEATURE_SCHEMA + args.output.mkdir(parents=True, exist_ok=True) report = { "passed": False, @@ -73,30 +114,7 @@ def main(): directory = args.output / f"update_{update:04d}" / part_id directory.mkdir(parents=True, exist_ok=True) log_path = directory / "worker.log" - command = [ - sys.executable, - str(Path(__file__).with_name("leg_worker.py")), - "--bundle", - str(bundle_path.resolve()), - "--mode", - "rollout", - "--weights", - str(weights.resolve()), - "--num_envs", - str(args.num_envs), - "--max-joints", - str(args.max_joints), - "--horizon", - str(args.horizon), - "--episode-length", - str(args.episode_length), - "--seed", - str(args.seed + index + update * 100), - "--device", - args.device, - "--output", - str(directory.resolve()), - ] + command = worker_command(args, bundle_path, weights, directory, args.seed + index + update * 100) with log_path.open("w") as log: child = subprocess.run(command, stdout=log, stderr=subprocess.STDOUT) critical = critical_log_lines(log_path) diff --git a/scripts/verify_contact_cycle.py b/scripts/verify_contact_cycle.py new file mode 100644 index 0000000..911589c --- /dev/null +++ b/scripts/verify_contact_cycle.py @@ -0,0 +1,262 @@ +#!/usr/bin/env python3 +"""Development-only physical preflight. No optimizer, checkpoint selection or holdout access.""" + +import argparse +import hashlib +import json +import shutil +import sys +import traceback +from pathlib import Path + +from runtime_common import finish_run, write_json + + +def evaluate(env, args, thresholds): + import torch + from SkillFactory.legs.contact_cycle.mechanics import inverse_rotate + from SkillFactory.legs.contact_cycle.metrics import summarize + from SkillFactory.legs.tokens import LegKinematics + from SkillFactory.tasks.manager_based.contact_cycle.mdp import failed, state + + s = state(env) + fk = LegKinematics(s.bundle, env.device) + nominal = s.nominal.expand(env.num_envs, -1) + q_ik = nominal.clone() + limits = torch.tensor([[j["limits"]["lower"], j["limits"]["upper"]] for j in s.bundle["joints"]], device=env.device) + speeds = torch.tensor([j["limits"]["velocity"] for j in s.bundle["joints"]], device=env.device) + stiffness = torch.tensor([j["kp"] for j in s.bundle["spec"]["joints"]], device=env.device) + trace = {} + + def foot(q): + transform = fk.forward(q)[s.bundle["foot"]["body"]] + return transform[:, :3, 3] + (transform[:, :3, :3] @ s.offset[:, :, None]).squeeze(-1) + + def jacobian(q): + columns = [] + for j in range(3): + delta = torch.zeros_like(q) + delta[:, j] = 0.0005 + columns.append((foot(q+delta)-foot(q-delta))/0.001) + return torch.stack(columns, -1) + + original = env.scene.update + + def observe(dt): + original(dt) + if dt <= 0: + return + phase, progress, target, desired, _ = s.reference() + pos = s.root()[0] + excess = torch.maximum(limits[:, 0]-s.q(), s.q()-limits[:, 1]).clamp(min=0).amax(-1) + ratio = torch.maximum((s.robot.data.applied_torque.torch[:, s.joints]/s.effort).abs().amax(-1), + (s.robot.data.joint_vel.torch[:, s.joints]/speeds).abs().amax(-1)) + mass = s.robot.data.body_mass.torch + gravity = torch.zeros(env.num_envs, s.robot.num_bodies, 3, device=env.device) + gravity[..., 2] = -9.81 * mass + com = s.robot.data.body_com_pos_w.torch + payload_force = (s.external + s.guide + s.auxiliary)[:, s.payload, :3] + gravity[:, s.payload] + payload_torque = (s.external + s.guide + s.auxiliary)[:, s.payload, 3:] + expected_mount = torch.cat((payload_force, payload_torque + torch.cross( + com[:, s.payload] - s.robot.data.body_link_pos_w.torch[:, s.anchor], payload_force, dim=-1)), -1) + values = { + "time": s.time, "phase": phase, "progress": progress, "scenario": s.scenarios, + "foot": s.foot_center(), "target": target, "desired_force": desired, "weight": s.weight, + "normal": s.contact(), "friction": s.friction(), "gap": s.foot_center()[:, 2]-s.bundle["foot"]["radius"], + "slip_speed": s.slip_velocity(), "height_error": (pos[:, 2]-s.port_reference()[0][:, 2]).abs(), + "joint_excess": excess, "limit_ratio": ratio, "joint_pos": s.q(), + "joint_vel": s.robot.data.joint_vel.torch[:, s.joints], + "applied_torque": s.robot.data.applied_torque.torch[:, s.joints], + "base_state": torch.cat(s.root(), -1), "mount_wrench": s.mount_wrench(), + "external": s.external, "guide": s.guide, "auxiliary": s.auxiliary, + "contact_latch": s.latch.contact, "event_fired": s.schedule.fired, + "event_envelope": s.schedule.envelope, "event_integral": s.schedule.integral, + "event_completed": s.schedule.completed, "would_fail": failed(env), "tilt": s.tilt(), + "linear_momentum": (mass[..., None] * s.robot.data.body_com_lin_vel_w.torch).sum(1), + "total_external_force": (s.external[..., :3] + s.guide[..., :3] + s.auxiliary[..., :3] + gravity).sum(1) + + s.contact() + s.friction(), + "mount_world": s.mount_world(), "expected_static_mount_world": expected_mount, + } + for k, v in values.items(): + trace.setdefault(k, []).append(v.detach().cpu().clone()) + + env.scene.update = observe + steps = round(sum(s.cc["phase_durations_s"]) / env.step_dt) + observations, actions = [], [] + try: + for _ in range(steps): + # Analytic engineering controller with privileged root pose/FK. Not a deployable learned policy. + _, _, target, fn, _ = s.reference() + pos, quat, _, _ = s.root() + local = inverse_rotate(quat, target-pos) + for _ in range(3): + j = jacobian(q_ik) + dq = torch.linalg.solve(j @ j.transpose(-1, -2) + torch.eye(3, device=env.device)*1e-5, + (local-foot(q_ik))[:, :, None]) + q_ik += (j.transpose(-1, -2) @ dq).squeeze(-1).clamp(-0.08, 0.08) + q_ik.clamp_(min=nominal-0.5, max=nominal+0.5) + f = torch.zeros_like(local) + f[:, 2] = fn + torque = -(jacobian(q_ik).transpose(-1, -2) @ inverse_rotate(quat, f)[:, :, None]).squeeze(-1) + command = q_ik + torque / stiffness + action = ((command-nominal)/0.5).clamp(-1, 1) + if args.controller == "disabled": + action.zero_() + obs, _, terminated, truncated, _ = env.step(action.float()) + if terminated.any() or truncated.any(): + raise RuntimeError("Physical validation must retain failures without automatic reset") + observations.append(obs["policy"].cpu().clone()) + actions.append(action.cpu().clone()) + finally: + env.scene.update = original + data = {k: torch.stack(v) for k, v in trace.items()} + torch.save(data, args.output / "physics_trace.pt") + torch.save({"policy": torch.stack(observations) if observations else [], + "action": torch.stack(actions) if actions else []}, args.output / "control_trace.pt") + rows = summarize(data, env.physics_dt, thresholds, s.cc["scenarios"]) + momentum_residual = ((data["linear_momentum"][1:] - data["linear_momentum"][:-1]) / env.physics_dt + - data["total_external_force"][1:]) + momentum_rms = momentum_residual.square().mean(0).sqrt().norm(dim=-1) + static = (data["time"][:, 0] >= 2.0) & (data["time"][:, 0] < 2.8) + sensor_residual = (data["mount_world"][static].mean(0) + - data["expected_static_mount_world"][static].mean(0)).abs() + expected_events = torch.tensor([[i == e["scenario"] for e in s.cc["events"]] + for i in s.scenarios.tolist()], device="cpu") + # Reset probes are outside the physics trial. Live nonzero wrench + selective reset tests persistence. + before = {"root": s.robot.data.root_state_w.torch.clone(), "q": s.q().clone(), + "time": s.time.clone(), "fired": s.schedule.fired.clone(), "sensor": s.sensed.clone()} + ids = torch.arange(0, env.num_envs, 2, device=env.device) + other = torch.arange(1, env.num_envs, 2, device=env.device) + composer = s.robot.permanent_wrench_composer + seeded = torch.arange(1, env.num_envs + 1, device=env.device).float()[:, None, None] + seeded = seeded.expand(-1, s.robot.num_bodies, 3).contiguous() + composer.set_forces_and_torques_index(forces=seeded, torques=seeded*0.1, positions=None, is_global=True) + before_force, before_torque = composer.out_force_b.torch.clone(), composer.out_torque_b.torch.clone() + env.reset(env_ids=ids) + composer_reset = bool((composer.out_force_b.torch[ids] == 0).all() + and (composer.out_torque_b.torch[ids] == 0).all() + and torch.equal(before_force[other], composer.out_force_b.torch[other]) + and torch.equal(before_torque[other], composer.out_torque_b.torch[other])) + isolated = all(torch.equal(a[other], b[other]) for a, b in ( + (before["root"], s.robot.data.root_state_w.torch), (before["q"], s.q()), + (before["time"], s.time), (before["fired"], s.schedule.fired), (before["sensor"], s.sensed))) + cleared = bool((s.external[ids] == 0).all() and (s.auxiliary[ids] == 0).all() + and (s.time[ids] == 0).all() and not s.schedule.fired[ids].any() + and (s.action_delay.data[:, ids] == 0).all() and not s.latch.contact[ids].any() + and (s.schedule.integral[ids] == 0).all() + and (s.sensor_delay.data[:, ids] == s.raw_sensors()[ids]).all() + and (s.sensed[ids] == s.raw_sensors()[ids]).all()) + snapshot = s.policy().clone() + env.reset(env_ids=[]) + empty = torch.equal(snapshot, s.policy()) + env.cfg.episode_length_s = 3 * env.step_dt + env.reset() + for _ in range(3): + obs, _, terminated, truncated, info = env.step(torch.zeros(env.num_envs, 3, device=env.device)) + terminal = bool(truncated.all() and not terminated.any() and info["terminal_observation"] is not None + and info["terminal_observation"]["policy"].shape == (env.num_envs, 32) + and (s.time == 0).all() and not s.schedule.fired.any()) + before = s.time.clone() + rejected = False + try: + env.step(torch.full((env.num_envs, 3), float("nan"), device=env.device)) + except ValueError: + rejected = torch.equal(before, s.time) + expected_integral = torch.tensor([e["duration_s"] - e["ramp_s"] for e in s.cc["events"]]) + delivered = (data["event_integral"][-1] - expected_events * expected_integral).abs() <= 0.002 + gates = {"floating_root": not s.robot.is_fixed_base, "finite_trace": all(r["finite"] for r in rows), + "full_phases": all(r["complete"] for r in rows), "indexed_reset": isolated and cleared, + "empty_reset": empty, "composer_reset": composer_reset, + "timeout_terminal": terminal, "bad_action_rejected": rejected, + "guide_vertical_zero": bool((data["guide"][..., 2] == 0).all()), + "all_scenario_metrics": all(r["success"] for r in rows), + "event_coverage": torch.equal(data["event_fired"][-1], expected_events) + and torch.equal(data["event_completed"][-1], expected_events) and bool(delivered.all()), + "scenario_coverage": set(data["scenario"][0].tolist()) == set(range(len(s.cc["scenarios"]))), + "linear_momentum_balance": bool((momentum_rms <= 5.0).all()), + "static_mount_force": bool((sensor_residual[:, :3] <= 1.5).all()), + "static_mount_torque": bool((sensor_residual[:, 3:] <= 0.3).all())} + return {"passed": all(gates.values()), "accepted_skill": False, "trained_skill": False, + "kind": "development_physics_preflight", "controller": args.controller, + "controller_uses_privileged_state": True, "seed": args.seed, + "gates": gates, "rows": rows, "physics_dt": env.physics_dt, "control_dt": env.step_dt, + "physics_steps": len(data["time"]), "support_enabled": env.cfg.auxiliary_enabled, + "effective_mass_kg": dict(zip(s.robot.body_names, s.robot.data.body_mass.torch[0].tolist())), + "event_triggered_per_env": data["event_fired"][-1].tolist(), + "event_integral_s_per_env": data["event_integral"][-1].tolist(), + "event_completed_per_env": data["event_completed"][-1].tolist(), + "linear_momentum_balance_rms_N_per_env": momentum_rms.tolist(), + "static_mount_residual_N_Nm_per_env": sensor_residual.tolist(), + "note": "Preflight is not independent acceptance, multi-seed training, or hardware generalization."} + + +def main(): + from isaaclab.app import AppLauncher + + from isaaclab_tasks.utils import add_launcher_args, launch_simulation + + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--config", type=Path, default=Path("configs/contact_cycle/go2_fl_v1.json")) + parser.add_argument("--thresholds", type=Path, default=Path("configs/contact_cycle/acceptance_v1.json")) + parser.add_argument("--output", type=Path, required=True) + parser.add_argument("--num_envs", type=int, default=4) + parser.add_argument("--seed", type=int, default=90101) + parser.add_argument("--no-auxiliary", action="store_true") + parser.add_argument("--controller", choices=("ik", "disabled"), default="ik") + add_launcher_args(parser) + args = parser.parse_args() + if args.num_envs < 4: + parser.error("At least four environments required for development scenarios") + args.output.mkdir(parents=True, exist_ok=False) + root = Path(__file__).resolve().parents[1] + from SkillFactory.legs.contact_cycle.config import load_config, require_scenario_coverage + + require_scenario_coverage(load_config(args.config), args.num_envs) + sources = [Path(__file__), args.config, args.thresholds, root/"scripts/runtime_common.py", + root/"scripts/supervise_sim.py", root/"configs/standing/go2_fl_support_v0.json", + root/"assets/generated/support/go2_fl_3kg_v0/support.json", + root/"assets/generated/legs/go2_fl/manifest.json"] + sources += list((root/"source/SkillFactory/SkillFactory").rglob("*.py")) + identity = {str(p): hashlib.sha256(p.read_bytes()).hexdigest() for p in sources} + for p in sources: + relative = (p.resolve().relative_to(root) if p.resolve().is_relative_to(root) + else Path("external")/hashlib.sha256(p.read_bytes()).hexdigest()/p.name) + target = args.output/"source_snapshot"/relative + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(p, target) + write_json(args.output/"identity.json", identity) + write_json(args.output/"invocation.json", {"argv": sys.argv, "thresholds": json.loads(args.thresholds.read_text())}) + write_json(args.output/"result.json", {"passed": False, "status": "running", "accepted_skill": False}) + app = AppLauncher(args).app + env, result, code = None, {"passed": False, "accepted_skill": False}, 1 + try: + import gymnasium as gym + import SkillFactory.tasks # noqa: F401 + from SkillFactory.tasks.manager_based.contact_cycle.cfg import ContactCycleEnvCfg + + cfg = ContactCycleEnvCfg(cycle_config=str(args.config)) + cfg.scene.num_envs = args.num_envs + cfg.seed = args.seed + cfg.auxiliary_enabled = not args.no_auxiliary + cfg.episode_length_s += 1 + cfg.terminations.failed = None # Full failed trajectory retained for audit, never censored by reset. + if args.controller == "disabled": + cfg.scene.robot.actuators["leg"].stiffness = 0 + cfg.scene.robot.actuators["leg"].damping = 0 + with launch_simulation(cfg, args): + env = gym.make("SkillFactory-Go2FL-ContactCycle-v1", cfg=cfg).unwrapped + env.reset(seed=args.seed) + result = evaluate(env, args, json.loads(args.thresholds.read_text())["gates"]) + write_json(args.output/"config.json", cfg.cycle_parameters) + if any(hashlib.sha256(Path(p).read_bytes()).hexdigest() != digest for p, digest in identity.items()): + raise RuntimeError("Bound source/config changed during physical trial") + code = 0 if result["passed"] else 2 + except BaseException: + result.update(passed=False, error=traceback.format_exc()) + print(result["error"], file=sys.stderr) + finally: + finish_run(app, env, args.output/"result.json", result, code) + + +if __name__ == "__main__": + main() diff --git a/scripts/verify_cycle_learning_runtime.py b/scripts/verify_cycle_learning_runtime.py new file mode 100644 index 0000000..4a3e300 --- /dev/null +++ b/scripts/verify_cycle_learning_runtime.py @@ -0,0 +1,298 @@ +#!/usr/bin/env python3 +"""Non-learning PhysX integration: sampled loads, first-episode recorder, 32D timeout rollout. + +Uses a privileged analytic controller and an UNTRAINED network for API checks. +No optimizer, learner checkpoint selection, training grant or independent test. +""" + +import argparse +import hashlib +import json +import traceback +from pathlib import Path + +from runtime_common import finish_run, write_json + + +def run(env, output): + import torch + import warp as wp + from SkillFactory.legs.contact_cycle.curriculum import EpisodeSampler, assert_sample_domain + from SkillFactory.legs.contact_cycle.mechanics import inverse_rotate + from SkillFactory.legs.contact_cycle.ppo import CycleActorCritic, collect_rollout, features + from SkillFactory.legs.tokens import LegKinematics + from SkillFactory.tasks.manager_based.contact_cycle.mdp import state + from SkillFactory.tasks.manager_based.contact_cycle.recording import CycleRecorder + + s = state(env) + sampler = EpisodeSampler(s.cc, env.num_envs, 90211, update=1500) + s.set_sampler(sampler) + env.reset() + sample = {"scenario": s.scenarios.cpu().clone(), "stand": s.stand_only.cpu().clone(), + "stage": s.sampled_stage.cpu().clone(), "commands": s.schedule.commands.cpu().clone(), + "points": s.sampled_points.cpu().clone(), "delays": s.schedule.delays.cpu().clone()} + assert_sample_domain(s.cc, sample) + torch.save(sample, output/"episode_inputs.pt") + fk = LegKinematics(s.bundle, env.device) + nominal = s.nominal.expand(env.num_envs, -1) + q_ik = nominal.clone() + stiffness = torch.tensor([j["kp"] for j in s.bundle["spec"]["joints"]], device=env.device) + + def foot(q): + transform = fk.forward(q)[s.bundle["foot"]["body"]] + return transform[:, :3, 3] + (transform[:, :3, :3] @ s.offset[:, :, None]).squeeze(-1) + + def jacobian(q): + columns = [] + for j in range(3): + delta = torch.zeros_like(q) + delta[:, j] = 0.0005 + columns.append((foot(q+delta)-foot(q-delta))/0.001) + return torch.stack(columns, -1) + + def analytic_action(early_env=None): + nonlocal q_ik + _, _, target, fn, _ = s.reference() + if early_env is not None and s.time[early_env] >= 3.8: + target[early_env, 2] += 0.03 # physical negative control, not edited sensor data + pos, quat, _, _ = s.root() + local = inverse_rotate(quat, target-pos) + for _ in range(3): + j = jacobian(q_ik) + dq = torch.linalg.solve(j @ j.transpose(-1, -2) + torch.eye(3, device=env.device)*1e-5, + (local-foot(q_ik))[:, :, None]) + q_ik += (j.transpose(-1, -2) @ dq).squeeze(-1).clamp(-0.08, 0.08) + q_ik.clamp_(min=nominal-0.5, max=nominal+0.5) + force = torch.zeros_like(local) + force[:, 2] = fn + torque = -(jacobian(q_ik).transpose(-1, -2) @ inverse_rotate(quat, force)[:, :, None]).squeeze(-1) + return ((q_ik + torque/stiffness - nominal)/0.5).clamp(-1, 1).float() + + steps = round(sum(s.cc["phase_durations_s"])/env.step_dt) + with CycleRecorder(env) as recorder: + initial_snapshot = recorder.initial_state + for k in range(steps): + action = analytic_action() + recorder.before_control(None, action) + _, _, terminated, truncated, _ = env.step(action) + torch.testing.assert_close(recorder.frames[-1]["requested_action"], action.cpu(), rtol=0, atol=0) + recorder.end_control(terminated, truncated, k == steps-1) + if terminated.any() or truncated.any(): + raise RuntimeError("Development sampled trace must not silently auto-reset") + thresholds = json.loads(Path("configs/contact_cycle/acceptance_v1.json").read_text())["gates"] + rows, trace = recorder.summarize(thresholds) + torch.save(trace, output/"sampled_physics_trace.pt") + torch.save(initial_snapshot, output/"sampled_initial_state.pt") + initialization_checks = { + "configured_on_plane": env.cfg.scene.robot.init_state.pos[2] == env.cfg.anchor_height, + "measured_reset_gap": bool((initial_snapshot["gap"].abs() <= 1e-5).all()), + "all_acquired_load": all(r["contact_acquired"] for r in rows), + "no_initialization_failure": not bool(trace["acquisition_failed"].any()), + "initial_snapshot_separate": bool((initial_snapshot["time"] == 0).all()) and len(trace["time"]) == 4500, + } + expected = torch.tensor([[e["scenario"] == int(sc) for e in s.cc["events"]] for sc in sample["scenario"]]) + event_completed = torch.equal(s.schedule.completed.cpu(), expected) + integral = torch.tensor([e["duration_s"]-e["ramp_s"] for e in s.cc["events"]]) + delivered = bool(((s.schedule.integral.cpu() - expected*integral).abs() <= 0.002).all()) + # Selective reset must not resample another environment, its RNG counters or backend load. + old_commands, old_points = s.schedule.commands.clone(), s.sampled_points.clone() + old_counts = sampler.episodes.clone() + composer = s.robot.permanent_wrench_composer + old_force = composer.out_force_b.torch.clone() + buffer_names = ("global_force_w", "global_force_at_com_w", "global_torque_w", "local_force_b", "local_torque_b") + before_buffers = {name: wp.to_torch(getattr(composer, name)).clone() for name in buffer_names} + ids = torch.tensor([1, 3], dtype=torch.int32, device=env.device) + others = torch.tensor([i for i in range(env.num_envs) if i not in (1, 3)], device=env.device) + env.reset(env_ids=ids) + reset_checks = { + "other_commands_unchanged": torch.equal(old_commands[others], s.schedule.commands[others]), + "other_points_unchanged": torch.equal(old_points[others], s.sampled_points[others]), + "other_counters_unchanged": torch.equal(old_counts[others.cpu()], sampler.episodes[others.cpu()]), + "selected_counters_advance_once": bool((sampler.episodes[ids.cpu()] == old_counts[ids.cpu()] + 1).all()), + "selected_points_resampled": not torch.equal(old_points[ids], s.sampled_points[ids]), + } + for name in buffer_names: + after = wp.to_torch(getattr(composer, name)) + reset_checks[f"{name}_isolated"] = torch.equal(before_buffers[name][others], after[others]) + reset_checks[f"{name}_selected_zero"] = bool((after[ids] == 0).all()) + # Do not compare stale cached body-frame output to recomposition at the newest body pose. + # The world/local INPUT buffers are the persistent applied-command source of truth. + cache_difference = float((old_force[others] - composer.out_force_b.torch[others]).abs().max()) + isolated = all(reset_checks.values()) + # Zero normalized actions must not inherit a 3mm airborne reset and fail the 10ms monitor. + s.set_sampler(None) + env.reset() + zero_failed = False + with CycleRecorder(env) as zero_recorder: + for _ in range(25): + action = torch.zeros(env.num_envs, 3, device=env.device) + zero_recorder.before_control(None, action) + _, _, terminated, truncated, _ = env.step(action) + zero_failed |= bool(terminated.any() or truncated.any()) + initialization_checks["zero_action_startup"] = not zero_failed and bool(s.acquisition.acquired.all()) + torch.save({k: torch.stack([f[k] for f in zero_recorder.frames]) for k in zero_recorder.frames[0]}, + output/"zero_action_startup.pt") + # Invalid physical reset probe: lift only env0's root 30mm, with a separately declared 20ms deadline. + # This shortened diagnostic is not a production-policy episode or a relaxed acceptance gate. + from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES + + env.reset() + pose = s.robot.data.root_state_w.torch[:1, :7].clone() + pose[:, 2] += .03 + s.robot.write_root_pose_to_sim(pose, env_ids=torch.tensor([0], device=env.device)) + original_deadline = s.acquisition.deadline + s.acquisition.deadline = .02 + try: + with CycleRecorder(env) as invalid_recorder: + for _ in range(2): + action = torch.zeros(env.num_envs, 3, device=env.device) + invalid_recorder.before_control(None, action) + _, _, terminated, _, info = env.step(action) + flags = info["cycle_step"]["failure_flags"][0] + initialization_checks["invalid_start_times_out"] = bool( + terminated[0] and flags[FAILURE_NAMES.index("contact_acquisition_timeout")]) + initialization_checks["invalid_start_not_fake_departure"] = not bool( + flags[FAILURE_NAMES.index("premature_separation")]) + initialization_checks["autoreset_clears_acquisition"] = not bool(s.acquisition.failed[0]) and s.time[0] == 0 + torch.save({k: torch.stack([f[k] for f in invalid_recorder.frames]) for k in invalid_recorder.frames[0]}, + output/"invalid_start_negative.pt") + finally: + s.acquisition.deadline = original_deadline + initialization_checks = {k: bool(v) for k, v in initialization_checks.items()} + # Separate physical negative control: lift env0 early while unloading. + # Keep actual termination enabled: capture before autoreset, then require that reset clears only env0. + s.set_sampler(None) + env.reset() + s.scenarios[:] = 2 + q_ik = nominal.clone() + with CycleRecorder(env) as early_recorder: + for k in range(round(4.3/env.step_dt)): + action = analytic_action(early_env=0) + early_recorder.before_control(None, action) + _, _, terminated, truncated, _ = env.step(action) + early_recorder.end_control(terminated, truncated, False) + if terminated.any() or truncated.any(): + break + early_trace = {key: torch.stack([f[key] for f in early_recorder.frames]) for key in early_recorder.frames[0]} + early_checks = { + "premature_detected_at_physics_rate": bool(early_trace["premature_separation"][:, 0].any()), + "actual_termination": bool(terminated[0]) and not bool(terminated[1:].any() or truncated.any()), + "no_synthetic_delayed_pulse": not bool(early_trace["event_fired"][:, 0].any()), + "other_envs_not_premature": not bool(early_trace["premature_separation"][:, 1:].any()), + "autoreset_clears_failure": not bool(s.separation.premature[0]) and s.time[0] == 0, + "other_episode_clocks_not_reset": bool((s.time[1:] > 3.8).all()), + "terminal_snapshot": (env.extras.get("terminal_ids") is not None + and env.extras["terminal_ids"].cpu().tolist() == [0]), + } + early_checks = {key: bool(value) for key, value in early_checks.items()} + early_time = float(early_trace["time"][-1, 0]) + torch.save({key: value[:, :2].clone() for key, value in early_trace.items()}, output/"early_lift_negative_trace.pt") + # API test only: force a short TIMEOUT to check terminal snapshots/32D GAE inputs. + # This shortened episode is NOT included in task metrics or counted as skill acceptance. + s.set_sampler(None) + env.cfg.episode_length_s = 2*env.step_dt + x = features(env.reset()[0]["policy"]) + torch.manual_seed(90212) + model = CycleActorCritic(hidden=16).to(env.device) + before = {k: v.clone() for k, v in model.state_dict().items()} + # Isolate timeout/bootstrap API from random untrained early-lift failures. ONLY this 0.02s API fixture + # disables the failed term; the full 9s positive run and early-lift negative above use the real term. + from isaaclab.managers import TerminationTermCfg + + original_failed = env.termination_manager.get_term_cfg("failed") + env.termination_manager.set_term_cfg("failed", TerminationTermCfg( + func=lambda e: torch.zeros(e.num_envs, dtype=torch.bool, device=e.device))) + try: + data, _, failures, timeouts = collect_rollout(env, model, x, {"horizon": 4, "failure_penalty": 10.}) + finally: + env.termination_manager.set_term_cfg("failed", original_failed) + canonical_terminal = env.extras["terminal_ids"].dtype == torch.int64 + no_learning = all(torch.equal(before[k], v) for k, v in model.state_dict().items()) + x = features(env.reset()[0]["policy"]) + with CycleRecorder(env) as actor_recorder: + with torch.no_grad(): + action, mean, _, _ = model.act(x, deterministic=True) + actor_recorder.before_control(x, action, mean, model.log_std.exp()) + env.step(action) + actor_frame = actor_recorder.frames[0] + actor_logging = (bool(actor_frame["actor_metadata_available"].all()) + and torch.equal(actor_frame["actor_input"], x.cpu()) + and torch.equal(actor_frame["requested_action"], action.cpu()) + and torch.equal(actor_frame["actor_mean"], mean.cpu())) + torch.save({"initial": actor_recorder.initial_state, "first_tick": actor_frame}, output/"actor_diagnostic_api.pt") + gates = {"domain_audit": True, "scenario_coverage": set(sample["scenario"].tolist()) == {0, 1, 2, 3}, + "full_sampled_duration": len(trace["time"]) == round(9/env.physics_dt), + "all_sampled_analytic_metrics": all(r["success"] for r in rows), + "event_delivery": event_completed and delivered, "indexed_sampler_reset": isolated, + "initialization_contract": all(initialization_checks.values()), + "early_lift_negative_control": all(early_checks.values()), + "timeout_rollout": failures == 0 and timeouts == 2*env.num_envs and bool(data["done"].any()), + "canonical_terminal_ids": canonical_terminal, + "actor_diagnostic_logging": actor_logging, + "terminal_metadata_not_stale": (env.extras["terminal_ids"] is None + and env.extras["terminal_failure_flags"] is None), + "analytic_actions_logged": (bool(trace["control_action_available"].all()) + and not bool(trace["actor_metadata_available"].any())), + "rollout_diagnostic_coverage": bool((data["phase"] >= 0).all()), + "finite_rollout": all(torch.isfinite(v).all().item() for v in data.values()), + "no_parameter_update": no_learning} + torch.save(data, output/"untrained_timeout_api_rollout.pt") + return {"passed": all(gates.values()), "gates": gates, "rows": rows, "accepted_skill": False, + "trained_skill": False, "optimizer_steps": 0, "independent_test_executed": False, + "sampled_development_seed": 90211, "timeout_api_test_episode_s": env.cfg.episode_length_s, + "timeout_api_failed_term_disabled": True, + "timeout_api_failures": failures, "timeout_api_timeouts": timeouts, + "reset_checks": reset_checks, "cached_vs_recomposed_body_force_difference_N": cache_difference, + "initialization_checks": initialization_checks, + "invalid_start_probe": {"root_raise_m": .03, "deadline_s": .02, "production_deadline_s": original_deadline}, + "early_lift_checks": early_checks, "early_lift_probe_end_s": early_time, + "early_lift_max_unload_gap_m": float(early_trace["gap"][:, 0][early_trace["phase"][:, 0] == 1].max()), + "note": "Analytic development, early-lift negative control and short timeout API. Not skill acceptance."} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--output", type=Path, required=True) + from isaaclab.app import AppLauncher + + AppLauncher.add_app_launcher_args(parser) + args = parser.parse_args() + args.output.mkdir(parents=True, exist_ok=False) + root = Path(__file__).resolve().parents[1] + files = list((root/"source/SkillFactory").rglob("*.py")) + [Path(__file__)] + files += list((root/"configs/contact_cycle").glob("*.json")) + identity = {str(p): hashlib.sha256(p.read_bytes()).hexdigest() for p in files} + write_json(args.output/"identity.json", identity) + launcher = AppLauncher(args) + env = None + result = {"passed": False, "accepted_skill": False, "trained_skill": False} + code = 1 + try: + from SkillFactory.tasks.manager_based.contact_cycle.cfg import ContactCycleEnvCfg + from SkillFactory.tasks.manager_based.contact_cycle.env import ContactCycleEnv + + cfg = ContactCycleEnvCfg() + cfg.scene.num_envs = 16 + cfg.sim.device = args.device + cfg.seed = 90211 + cfg.episode_length_s = sum(cfg.cycle_parameters["phase_durations_s"]) + 1 + env = ContactCycleEnv(cfg=cfg) + from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit + + thresholds = json.loads((root/"configs/contact_cycle/acceptance_v1.json").read_text())["gates"] + with PhysicalSafetyAudit(env, thresholds) as safety: + result = run(env, args.output) + safety.check() + result["physics_safety"] = safety.report() + if any(hashlib.sha256(Path(p).read_bytes()).hexdigest() != h for p, h in identity.items()): + raise RuntimeError("Source changed during runtime test") + code = 0 if result["passed"] else 2 + except BaseException: + result["error"] = traceback.format_exc() + traceback.print_exc() + finally: + finish_run(launcher.app, env, args.output/"result.json", result, code) + + +if __name__ == "__main__": + main() diff --git a/scripts/verify_cycle_sim_learning_runtime.py b/scripts/verify_cycle_sim_learning_runtime.py new file mode 100644 index 0000000..9ea428a --- /dev/null +++ b/scripts/verify_cycle_sim_learning_runtime.py @@ -0,0 +1,223 @@ +#!/usr/bin/env python3 +"""Nonoptimizing 46D learner API probe: 8 envs, two 32-control short fixtures. + +No optimizer, grant, training ledger, checkpoint load or functional-skill claim. +Production failure term and 200ms acquisition deadline remain enabled. +""" + +import argparse +import hashlib +import json +import signal +import traceback +from pathlib import Path + +from runtime_common import finish_run, write_json + +ROOT = Path(__file__).resolve().parents[1] + + +def model_hash(model): + h = hashlib.sha256() + for name, value in sorted(model.state_dict().items()): + h.update(name.encode()) + h.update(value.detach().cpu().contiguous().numpy().tobytes()) + return h.hexdigest() + + +def run(env, output, guard): + import torch + from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES + from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic, features + from SkillFactory.legs.contact_cycle_sim.ppo import bootstrap_values, collect_rollout, rollout_diagnostics + from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state + from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder + + torch.set_num_threads(4) + torch.manual_seed(91502) + model = SimResidualActorCritic().to(env.device) + before = model_hash(model) + s = state(env) + if (s.robot.is_fixed_base or env.cfg.terminations.failed is None or s.acquisition.deadline != .2 + or env.num_envs != 8 or env.cfg.episode_length_s != .3): + raise RuntimeError("Require declared bounded fixture with original active failure/deadline contract") + gates = {} + fixtures = {} + total_controls = 0 + for name in ("timeout", "true_failure"): + x = features(env.reset()[0]["policy"]) + if name == "true_failure": + pose = s.robot.data.root_state_w.torch[:1, :7].clone() + pose[:, 2] += .5 + s.robot.write_root_pose_to_sim(pose, env_ids=torch.tensor([0], device=env.device)) + x = features(env.observation_manager.compute(update_history=False)["policy"]) + events, expected_values, terminals = [], [], [] + current = x.clone() + with SimCycleRecorder(env) as recorder: + class ObservedEnv: + num_envs = env.num_envs + device = env.device + step_dt = env.step_dt + + def step(self, action): + nonlocal current, total_controls + with torch.no_grad(): + dist, _ = model.distribution_value(current) + recorder.before_control(current, action, dist.mean, dist.stddev) + obs, reward, failed, timeout, info = env.step(action) + total_controls += 1 + recorder.end_control(failed, timeout, False) + next_x = features(obs["policy"]) + values = bootstrap_values(model, next_x, failed, timeout, info) + expected_values.append(values.detach().clone()) + event = {"control": total_controls, "failed": failed.cpu().tolist(), + "timeout": timeout.cpu().tolist(), + "failure_flags": info["cycle_step"]["failure_flags"].cpu().tolist(), + "times_after_reset_s": s.time.cpu().tolist()} + if (failed | timeout).any(): + ids = info["terminal_ids"] + terminal = features(info["terminal_observation"]["policy"]) + pre = recorder.frames[-1]["policy_snapshot"][ids.cpu()] + event["terminal_ids"] = ids.cpu().tolist() + event["terminal46"] = terminal.shape == (len(ids), 46) + event["terminal_matches_prereset"] = bool(torch.allclose( + terminal.cpu(), pre, atol=1e-6, rtol=0)) + event["terminal_differs_from_reset"] = bool((terminal != next_x[ids]).any(-1).all()) + event["failure_bootstrap_zero"] = bool((values[failed] == 0).all()) + timeout_only = timeout & ~failed + selected = timeout_only[ids] + with torch.no_grad(): + target = model.value(terminal[selected]) if selected.any() else values[:0] + event["timeout_bootstrap_matches"] = bool(torch.allclose( + values[ids[selected]], target, atol=1e-6, rtol=0)) + terminals.append({"ids": ids.cpu().clone(), "pre_reset": terminal.cpu().clone(), + "post_reset": next_x[ids].cpu().clone(), + "bootstrap": values[ids].cpu().clone()}) + events.append(event) + current = next_x.clone() + guard() + return obs, reward, failed, timeout, info + + data, _, failures, timeouts = collect_rollout( + ObservedEnv(), model, x, {"horizon": 32, "failure_penalty": 10.}, guard) + trace = {k: torch.stack([f[k] for f in recorder.frames]) for k in recorder.frames[0]} + actual = trace["processed_target"] - s.nominal.cpu() - .5 * trace["reference_used_normalized"] + with torch.no_grad(): + logp, _ = model.evaluate(data["features"].flatten(0, 1), data["latent"].flatten(0, 1)) + logp_error = float((logp.reshape_as(data["log_prob"]) - data["log_prob"]).abs().max()) + terminal_events = [e for e in events if "terminal_ids" in e] + checks = { + "finite_rollout": all(bool(torch.isfinite(v).all()) for v in data.values()), + "features46": data["features"].shape == (32, 8, 46), + "phase_cause_finite": data["failure_flags"].shape == (32, 8, 8) + and bool(((data["phase"] >= 0) & (data["phase"] <= 5)).all()), + "logprob_recompute": logp_error < 2e-4, + "bootstrap_matches": bool(torch.allclose( + data["next_value"], torch.stack(expected_values), atol=1e-6, rtol=0)), + "terminal_events_present": bool(terminal_events), + "terminal_contract": all(all(e[k] for k in ( + "terminal46", "terminal_matches_prereset", "terminal_differs_from_reset", + "failure_bootstrap_zero", "timeout_bootstrap_matches")) for e in terminal_events), + "actual_delta_matches": bool(torch.allclose(actual, trace["effective_residual_rad"], atol=1e-6, rtol=0)), + "actual_delta_bound": float(actual.abs().max()) <= .050001, + "template_bound": bool(((trace["processed_target"] - s.nominal.cpu()).abs() <= .500001).all()), + "actor_metadata46": trace["actor_input"].shape[-1] == 46 and bool(trace["actor_metadata_available"].all()), + "failure_term_still_enabled": env.cfg.terminations.failed is not None and s.acquisition.deadline == .2, + } + if name == "timeout": + checks["genuine_timeouts_only"] = failures == 0 and timeouts == 8 + else: + failure_events = [e for e in events if any(e["failed"])] + checks["genuine_env0_failure"] = (failures == 1 and len(failure_events) == 1 + and failure_events[0]["failed"][0]) + checks["other_envs_unaffected"] = not any(any(e["failed"][1:]) for e in events) + checks["production_acquisition_failure"] = bool(failure_events) and failure_events[0]["failure_flags"][0][ + FAILURE_NAMES.index("contact_acquisition_timeout")] + checks["other_clocks_not_reset_on_failure"] = bool(failure_events) and all( + t >= .19 for t in failure_events[0]["times_after_reset_s"][1:]) + checks["failure_row_zero_bootstrap"] = bool((data["next_value"][data["failure_flags"].any(-1)] == 0).all()) + gates[name] = all(checks.values()) + diagnostics = rollout_diagnostics(data, {"gamma": .99, "gae_lambda": .95}) + torch.save({"data": {k: v.cpu().clone() for k, v in data.items()}, "terminal_events": terminals}, + output / f"{name}_api.pt") + # Entire fixture is bounded to 160 physics frames x8, not a complete-cycle claim. + torch.save(trace, output / f"{name}_trace.pt") + torch.save(recorder.initial_state, output / f"{name}_initial.pt") + fixtures[name] = {"checks": checks, "failures": failures, "timeouts": timeouts, + "logprob_max_abs_error": logp_error, "max_actual_residual_rad": float(actual.abs().max()), + "events": events, "diagnostics": diagnostics} + after = model_hash(model) + gates["parameters_unchanged"] = before == after + return {"passed": all(gates.values()), "gates": gates, "fixtures": fixtures, + "model_sha256_before": before, "model_sha256_after": after, + "model_seed": 91502, "environment_seed": 93502, + "optimizer_steps": 0, "training_transitions": 0, "nonoptimizing_api_transitions": total_controls * 8, + "physics_steps_per_env": total_controls * env.cfg.decimation, + "aggregate_physics_transitions": total_controls * env.cfg.decimation * 8, + "fixture_episode_s": .3, "failure_root_raise_m": .5, "acquisition_deadline_s": .2, + "failure_term_disabled": False, "accepted_skill": False, "trained_residual_policy": False, + "functional_claim": False} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--output", type=Path, required=True) + parser.add_argument("--device", default="cuda:0") + parser.add_argument("--headless", action="store_true", required=True) + args = parser.parse_args() + from SkillFactory.legs.contact_cycle_sim.training import ( + CONFIG_PATH, + assert_no_conflicting_processes, + make_manifest, + writable_output, + ) + + args.output = writable_output(ROOT, args.output) + assert_no_conflicting_processes() + args.output.mkdir(parents=True, exist_ok=False) + manifest = make_manifest(ROOT, ROOT / CONFIG_PATH) + write_json(args.output / "identity.json", manifest) + stop = {"requested": False} + for sig in (signal.SIGTERM, signal.SIGINT): + signal.signal(sig, lambda *_: stop.update(requested=True)) + from isaaclab.app import AppLauncher + + app = AppLauncher(headless=True, device=args.device).app + env = safety = None + result = {"passed": False, "accepted_skill": False, "optimizer_steps": 0, "training_transitions": 0} + code = 1 + try: + from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit + from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg + from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv + + cfg = SimContactCycleEnvCfg() + cfg.scene.num_envs = 8 + cfg.sim.device = args.device + cfg.seed = 93502 + cfg.episode_length_s = .3 # API only; production acquisition deadline and failure term UNCHANGED + env = SimContactCycleEnv(cfg=cfg) + thresholds = json.loads((ROOT / "configs/contact_cycle/acceptance_v1.json").read_text())["gates"] + with PhysicalSafetyAudit(env, thresholds) as safety: + def guard(): + if stop["requested"]: + raise InterruptedError("Probe interrupted") + safety.check() + + result = run(env, args.output, guard) + guard() + result["physics_safety"] = safety.report() + if manifest != make_manifest(ROOT, ROOT / CONFIG_PATH): + raise RuntimeError("Identity changed during API probe") + code = 0 if result["passed"] else 2 + except BaseException: + result.update(passed=False, error=traceback.format_exc()) + traceback.print_exc() + finally: + if safety is not None: + result["physics_safety"] = safety.report() + finish_run(app, env, args.output / "result.json", result, code) + + +if __name__ == "__main__": + main() diff --git a/scripts/verify_cycle_sim_residual.py b/scripts/verify_cycle_sim_residual.py new file mode 100644 index 0000000..128be58 --- /dev/null +++ b/scripts/verify_cycle_sim_residual.py @@ -0,0 +1,196 @@ +#!/usr/bin/env python3 +"""Supervised non-learning physics verification of the simulation reference/residual task.""" +import argparse +import hashlib +import json +import traceback +from pathlib import Path + +from runtime_common import finish_run, write_json + +ROOT = Path(__file__).resolve().parents[1] + + +def run(env, output): + import torch + from audit_contact_cycle_functional import evaluate + from SkillFactory.legs.contact_cycle_sim.config import FEATURE_VERSION, pose_cost + from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic, features + from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state + from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder + + s = state(env) + if s.robot.is_fixed_base or env.cfg.terminations.failed is None: + raise RuntimeError('Require floating root and active physical failure termination') + x = features(env.reset()[0]['policy']) + zero = torch.zeros(env.num_envs, 3, device=env.device) + gates = {'policy46': x.shape == (env.num_envs, 46), 'nominal_only': not bool(s.scenarios.any())} + try: + s.set_sampler(object()) + except ValueError: + gates['old_sampler_rejected'] = True + else: + gates['old_sampler_rejected'] = False + rewards = [] + with SimCycleRecorder(env) as recorder: + initial = recorder.initial_state + for k in range(900): + recorder.before_control(None, zero) + observation, reward, terminated, truncated, _ = env.step(zero) + recorder.end_control(terminated, truncated, k == 899) + if terminated.any() or (k != 899 and truncated.any()): + raise RuntimeError(f'Zero-residual reference failed at control step{k}; no repaired autoresets') + features(observation['policy']) + rewards.append(reward.detach().cpu()) + trace = {key: torch.stack([frame[key] for frame in recorder.frames]) for key in recorder.frames[0]} + torch.save(trace, output / 'zero_residual_trace.pt') + torch.save(initial, output / 'zero_residual_initial.pt') + torch.save(torch.stack(rewards), output / 'control_rewards.pt') + functional = json.loads((ROOT / 'configs/contact_cycle/simulation_functional_v1.json').read_text()) + rows = [evaluate({k: v[:, e:e+1] for k, v in trace.items()}, functional, 'analytic_reference') + for e in range(env.num_envs)] + gates['full_functional_cycle'] = all(row['sim_functional_passed'] for row in rows) + gates['zero_means_reference'] = bool((trace['effective_residual_rad'] == 0).all()) + gates['no_hidden_disturbance'] = not bool(trace['external'].any()) + # The complete9s trajectory was captured before the normal timeout reset, not after. + gates['terminal46_at_timeout'] = env.extras['terminal_observation']['policy'].shape == (env.num_envs, 46) + gates['all_timeouts_no_failures'] = bool(truncated.all()) and not bool(terminated.any()) + + env.reset() + stored = s.controller.q_ik.clone(), s.controller.refreshes.clone(), s.controller.action.clone() + for _ in range(5): + env.observation_manager.compute(update_history=False) + gates['observation_reads_do_not_advance_reference'] = all( + torch.equal(a, b) for a, b in zip(stored, [s.controller.q_ik, s.controller.refreshes, s.controller.action])) + before = s.substeps.clone() + try: + env.step(torch.full_like(zero, float('nan'))) + except ValueError: + gates['invalid_action_no_physics'] = torch.equal(before, s.substeps) + else: + gates['invalid_action_no_physics'] = False + + gates['outer_rejection_history_finite'] = bool(torch.isfinite(env.action_manager.action).all() + and torch.isfinite(env.action_manager.prev_action).all()) + # Low-level manager entry bypasses the outer env.step input guard and stores input before terms. + try: + env.action_manager.process_action(torch.full_like(zero, float('nan'))) + except ValueError: + gates['manager_rejection_rolls_back'] = bool(torch.isfinite(env.action_manager.action).all() + and torch.equal(env.action_manager.action, + env.action_manager.get_term('joint_position').raw_actions)) + else: + gates['manager_rejection_rolls_back'] = False + gates['reward_after_rejected_input_finite'] = True + # Deliberately beyond normalized actor bounds; projection must still keep <=.05rad residual. + request = torch.tensor([100., -100., 100.], device=env.device).expand_as(zero) + with SimCycleRecorder(env) as boundary: + boundary_terminated = 0 + for _ in range(20): + boundary.before_control(None, request) + _, reward, terminated, _, _ = env.step(request) + gates['reward_after_rejected_input_finite'] &= bool(torch.isfinite(reward).all() + and torch.isfinite(env.action_manager.prev_action).all()) + boundary_terminated += int(terminated.sum()) + bound_trace = {k: torch.stack([f[k] for f in boundary.frames]) for k in boundary.frames[0]} + torch.save(bound_trace, output / 'residual_boundary_trace.pt') + actual_delta = (bound_trace['processed_target'] - s.nominal.cpu() + - .5 * bound_trace['reference_used_normalized']) + maximum = float(actual_delta.abs().max()) + gates['actual_target_residual_bound'] = maximum <= s.residual_cfg['residual_limit_rad'] + 1e-6 + gates['delta_log_matches_applied_target'] = bool(torch.allclose( + actual_delta, bound_trace['effective_residual_rad'], rtol=0, atol=1e-6)) + + others = torch.arange(1, env.num_envs, device=env.device) + buffers = [s.controller.q_ik, s.controller.action, s.controller.refreshes, s.residual_request, + s.reference_used, s.effective_residual_rad, s.substeps] + copies = [b[others].clone() for b in buffers] + env.reset(env_ids=torch.tensor([0], device=env.device, dtype=torch.int32)) + gates['indexed_reset_isolated'] = all(torch.equal(old, b[others]) for old, b in zip(copies, buffers)) + gates['indexed_residual_cleared'] = not bool(s.residual_request[0].any() or s.reference_used[0].any() + or s.effective_residual_rad[0].any()) + + # A fresh untrained46D network checks the interface; no old checkpoint/optimizer is used. + model = SimResidualActorCritic().to(env.device) + x = features(env.reset()[0]['policy']) + with torch.no_grad(): + mean_action = model.act(x, deterministic=True)[0] + gates['untrained_actor_is_zero_residual'] = not bool(mean_action.any()) + gates['sim_truth_features_match'] = bool(torch.allclose(x[:, 32:35], s.errors()[0]) + and torch.allclose(x[:, 35:38], s.errors()[1]) + and torch.allclose(x[:, 38], s.contact()[:, 2])) + for _ in range(2): + env.step(zero) + gates['reference_control_clock'] = bool((s.controller.refreshes == 3).all()) # reset + two controls + c = s.residual_cfg['reward'] + example_cost = pose_cost(torch.tensor([[0., 0., -.0996]]), torch.zeros(1, 3), c)[0].item() + gates['sinking_not_high_reward_counterexample'] = .9255 - c['position_weight'] * example_cost < 0 + from SkillFactory.tasks.manager_based.contact_cycle import mdp as old_mdp + from SkillFactory.tasks.manager_based.contact_cycle.cfg import ContactCycleEnvCfg + from SkillFactory.tasks.manager_based.loaded_leg.cfg import LoadedLegEnvCfg + + old, standing = ContactCycleEnvCfg(), LoadedLegEnvCfg() + gates['historical_configs_unmodified'] = ( + old.actions.joint_position.class_type is old_mdp.CycleAction + and not hasattr(old.rewards, 'root_position') and old.scene is not env.cfg.scene + and standing.scene.robot.init_state.pos[2] + == standing.anchor_height + standing.support_parameters['initial_foot_clearance_m']) + return {'passed': all(gates.values()), 'gates': gates, 'rows': rows, 'accepted_skill': False, + 'sim_functional_passed': gates['full_functional_cycle'], 'controller_kind': 'analytic_reference', + 'trained_residual_policy': False, 'optimizer_steps': 0, 'feature_version': FEATURE_VERSION, + 'max_actual_residual_rad': maximum, 'boundary_terminated': boundary_terminated, + 'zero_residual_cycle_s': 9, 'untrained_network_api_only': True, + 'task_reward_minus_height_cost_at_minus99_6mm': .9255 - c['position_weight'] * example_cost, + 'note': 'Nominal reference/residual interface verification, not evidence of learned-policy success.'} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--output', type=Path, required=True) + from isaaclab.app import AppLauncher + + AppLauncher.add_app_launcher_args(parser) + args = parser.parse_args() + frozen = (ROOT / 'releases').resolve() + if args.output.resolve() == frozen or frozen in args.output.resolve().parents: + parser.error('Verification must not write into frozen releases') + args.output.mkdir(parents=True, exist_ok=False) + from SkillFactory.legs.contact_cycle.training import load_training_config, source_identity + + old_cfg = ROOT / 'configs/contact_cycle/training_v1.json' + identity = source_identity(ROOT, load_training_config(old_cfg, ROOT), old_cfg) + extras = [Path(__file__), ROOT / 'scripts/audit_contact_cycle_functional.py', + *list((ROOT / 'configs/contact_cycle').glob('*.json'))] + identity.update({str(p.resolve().relative_to(ROOT)): hashlib.sha256(p.read_bytes()).hexdigest() for p in extras}) + write_json(args.output / 'identity.json', identity) + app = AppLauncher(args).app + env = None + result = {'passed': False, 'accepted_skill': False, 'optimizer_steps': 0} + code = 1 + try: + from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit + from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg + from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv + + cfg = SimContactCycleEnvCfg() + cfg.scene.num_envs = 8 + cfg.sim.device = args.device + cfg.seed = 90311 + env = SimContactCycleEnv(cfg=cfg) + thresholds = json.loads((ROOT / 'configs/contact_cycle/acceptance_v1.json').read_text())['gates'] + with PhysicalSafetyAudit(env, thresholds) as safety: + result = run(env, args.output) + safety.check() + result['physics_safety'] = safety.report() + if any(hashlib.sha256((ROOT / f).read_bytes()).hexdigest() != h for f, h in identity.items()): + raise RuntimeError('Source changed during test') + code = 0 if result['passed'] else 2 + except BaseException: + result['error'] = traceback.format_exc() + traceback.print_exc() + finally: + finish_run(app, env, args.output / 'result.json', result, code) + + +if __name__ == '__main__': + main() diff --git a/scripts/verify_cycle_sim_stress.py b/scripts/verify_cycle_sim_stress.py new file mode 100644 index 0000000..2a1946b --- /dev/null +++ b/scripts/verify_cycle_sim_stress.py @@ -0,0 +1,343 @@ +#!/usr/bin/env python3 +"""One supervised, bounded, non-learning nominal residual matrix. No model construction.""" +import argparse +import fcntl +import hashlib +import io +import json +import os +import subprocess +import sys +import time +import traceback +from pathlib import Path + +from runtime_common import finish_run, write_json + +ROOT = Path(__file__).resolve().parents[1] +WINDOW_ROOT = ROOT / 'logs/contact_cycle/autonomous_sim_resume_v1' +WINDOW = WINDOW_ROOT / 'window.json' +WINDOW_SHA256 = '511d3f98a2c9ec7b78b1cb7f8f72e28ef06de6dcf96ecf2493ac7399d1eefbc8' +OUTPUT_ROOT = WINDOW_ROOT / 'phase01_nominal_residual' +PLAN = ROOT / 'configs/contact_cycle/simulation_stress_v1.json' + + +def sha256(path): + return hashlib.sha256(Path(path).read_bytes()).hexdigest() + + +class WindowGuard: + """Same-boot absolute deadline; wall UTC never authorizes a launch or control.""" + + def __init__(self): + if sha256(WINDOW) != WINDOW_SHA256: + raise RuntimeError('Window contract hash changed') + self.window = json.loads(WINDOW.read_text()) + self.last_disk_check = -float('inf') + self.bytes = 0 + + def check(self, reserve=0, force_disk=False): + now = time.clock_gettime(time.CLOCK_BOOTTIME) + boot = Path('/proc/sys/kernel/random/boot_id').read_text().strip() + remaining = self.window['deadline_boottime'] - now + if boot != self.window['boot_id'] or remaining <= self.window['cleanup_margin_s']: + raise RuntimeError('Boot/deadline guard tripped; no additional physics authorized') + if force_disk or reserve or now - self.last_disk_check >= 1: + self.bytes = sum(p.stat().st_size for p in WINDOW_ROOT.rglob('*') if p.is_file()) + self.last_disk_check = now + # Reserve space for supervisor logs and final shutdown/error reports. + if self.bytes + reserve >= self.window['max_new_artifact_bytes'] - 8 * 1024**2: + raise RuntimeError('Artifact guard tripped') + return remaining + + +def verify_hashes(identity): + changed = [name for name, digest in identity.items() if sha256(ROOT / name) != digest] + if changed: + raise RuntimeError(f'Protected/registered inputs changed: {changed}') + + +def capture_identity(): + protected = json.loads((WINDOW_ROOT / 'protected_inputs_before.json').read_text()) + verify_hashes(protected) + files = {ROOT / name for name in protected} + files.update(p for p in (ROOT / 'source/SkillFactory').rglob('*') + if p.is_file() and '__pycache__' not in p.parts and p.suffix != '.pyc') + support = json.loads((ROOT / 'configs/standing/go2_fl_support_v0.json').read_text()) + for key in ('source_bundle', 'support_asset'): + files.update(p for p in (ROOT / support[key]).rglob('*') if p.is_file()) + files.update([Path(__file__), PLAN, WINDOW, WINDOW_ROOT / 'protected_inputs_before.json', + ROOT / 'scripts/runtime_common.py', ROOT / 'scripts/supervise_sim.py', + ROOT / 'tests/test_contact_cycle_sim_stress.py', ROOT / 'docs/contact_cycle_sim_stress.md']) + return {str(p.relative_to(ROOT)): sha256(p) for p in sorted(files)} + + +def assert_no_kit(): + for p in Path('/proc').glob('[0-9]*/cmdline'): + try: + args = p.read_bytes().split(b'\0') + except (FileNotFoundError, PermissionError, ProcessLookupError): + continue + # Match executable/launcher tokens, not incidental substrings such as polkit. + if args and (Path(os.fsdecode(args[0])).name in ('kit', 'isaac-sim', 'isaac-sim.sh') + or any(a.endswith(b'/isaaclab.sh') for a in args)): + raise RuntimeError(f'Unrelated simulator process conflict: {p.parent.name}') + + +def supervise(output): + guard = WindowGuard() + guard.check(force_disk=True) + OUTPUT_ROOT.mkdir(parents=True, exist_ok=True) + with (OUTPUT_ROOT / 'writer.lock').open('a') as lock: + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + assert_no_kit() + output.mkdir(parents=True, exist_ok=False) + identity = capture_identity() + registration = {'window_sha256': WINDOW_SHA256, 'registered_boottime': time.clock_gettime(time.CLOCK_BOOTTIME), + 'plan_sha256': sha256(PLAN), 'plan': json.loads(PLAN.read_text()), 'identity': identity, + 'omissions': [], 'optimizer_steps': 0, 'policy_models_constructed': 0} + write_json(output / 'preregistration.json', registration) + remaining = guard.check(force_disk=True) # immediately before the sole Kit launch + timeout = min(2400, int(remaining - guard.window['cleanup_margin_s'] - 10)) + if timeout <= 0: + raise RuntimeError('No safe launch time remains') + command = [sys.executable, str(ROOT / 'scripts/supervise_sim.py'), + '--log', str(output / 'simulator.log'), '--report', str(output / 'supervisor.json'), + '--timeout', str(timeout), '--', '/home/cen/IsaacLab/isaaclab.sh', '-p', + str(Path(__file__).resolve()), '--output', str(output), '--headless'] + write_json(output / 'launch.json', {'command': command, 'remaining_s': remaining, 'timeout_s': timeout, + 'preregistration_sha256': sha256(output / 'preregistration.json')}) + environment = dict(os.environ, PYTHONPATH=str(ROOT / 'source/SkillFactory'), PYTHONUNBUFFERED='1') + environment.pop('CONDA_PREFIX', None) + environment.pop('VIRTUAL_ENV', None) + completed = subprocess.run(command, env=environment, check=False) + verify_hashes(identity) + if sha256(output / 'preregistration.json') != json.loads((output / 'launch.json').read_text())[ + 'preregistration_sha256']: + raise RuntimeError('Preregistration changed during the run') + write_json(output / 'integrity_after.json', {'unchanged': True, 'files_checked': len(identity), + 'remaining_s': guard.check(force_disk=True), + 'window_artifact_bytes': guard.bytes}) + return completed.returncode + + +def claim_execution(output): + """Consume an attempt once, before Kit; never overwrite completed/partial evidence.""" + if (output / 'result.json').exists() or any(path.is_dir() for path in output.iterdir()): + raise FileExistsError('Attempt already contains runtime evidence; use a fresh output') + report = output / 'supervisor.json' + if report.exists(): + try: + supervisor = json.loads(report.read_text()) + except (ValueError, UnicodeError) as error: + raise FileExistsError('Existing supervisor evidence is malformed; preserve attempt') from error + # The unchanged supervisor writes precisely this initial report BEFORE spawning us. + initial_running = (isinstance(supervisor, dict) and set(supervisor) == {'passed', 'status', 'command'} + and supervisor['passed'] is False and supervisor['status'] == 'running' + and isinstance(supervisor['command'], list) and bool(supervisor['command']) + and all(isinstance(arg, str) for arg in supervisor['command'])) + if not initial_running: + raise FileExistsError('Existing supervisor report is not a fresh running report') + # O_EXCL also prevents simultaneous internal invocations or retries after startup failure. + with (output / 'execution_started.json').open('x') as stream: + json.dump({'pid': os.getpid(), 'started_boottime': time.clock_gettime(time.CLOCK_BOOTTIME)}, stream) + + +def save_tensor(path, value, guard): + import torch + + stream = io.BytesIO() + torch.save(value, stream) + guard.check(reserve=stream.tell()) + with path.open('xb') as out: + out.write(stream.getbuffer()) + + +def summarize_episode(episode, e, recorder, history, plan, state, functional): + import torch + from audit_contact_cycle_functional import evaluate + from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES + from SkillFactory.legs.contact_cycle_sim.stress import first_failure + + row = evaluate(episode, functional, 'analytic_reference') + row.update(env=e, controller_kind='analytic_reference_with_scripted_residual', + first_episode_samples=len(episode['time']), + phase_sample_counts={str(p): int((episode['phase'] == p).sum()) for p in range(6)}, + first_failure=first_failure(episode, FAILURE_NAMES), + control_boundary_stop_s=float(episode['time'][-1, 0]) if recorder.stops[e] is not None else None, + early_stop=bool(recorder.early[e])) + stop = (recorder.stops[e] or len(recorder.frames)) // 5 + h = {k: v[:stop, e:e+1].clone().contiguous() for k, v in history.items()} + row['terminated'] = bool(h['terminated'].any()) + row['truncated'] = bool(h['truncated'].any()) + row['termination_physics_tick'] = len(episode['time']) if row['terminated'] or row['truncated'] else None + row['termination_causes'] = ([name for j, name in enumerate(FAILURE_NAMES) + if episode['failure_flags'][-1, 0, j]] if row['terminated'] else + ['timeout'] if row['truncated'] else []) + target = episode['processed_target'] + nominal = state.nominal.cpu() + scale = state.env.cfg.support_parameters['action_scale_rad'] + delta = target - nominal - scale * episode['reference_used_normalized'] + row['max_actual_residual_rad'] = float(delta.abs().max()) + row['max_requested_residual_rad'] = float(episode['residual_request_normalized'].abs().max()) * .05 + row['max_absolute_target_offset_rad'] = float((target - nominal).abs().max()) + row['checks'] = { + 'actual_delta_bounded': row['max_actual_residual_rad'] <= plan['residual_limit_rad'] + 1e-6, + 'applied_target_original_bounds': row['max_absolute_target_offset_rad'] <= scale + 1e-6, + 'effective_delta_matches_target': bool(torch.allclose(delta, episode['effective_residual_rad'], + rtol=0, atol=1e-6)), + 'finite_reward_action_history': all(bool(torch.isfinite(v).all()) for v in h.values()), + 'finite_physics_trace': all(bool(torch.isfinite(v).all()) for v in episode.values()), + 'nominal_no_external_disturbance': not bool(episode['scenario'].any() or episode['external'].any()), + 'no_actor_metadata': not bool(episode['actor_metadata_available'].any()), + 'termination_has_physics_cause': not row['terminated'] or row['first_failure'] is not None, + } + row['success'] = (row['sim_functional_passed'] and not row['early_stop'] + and not row['terminated'] and all(row['checks'].values())) + return row, h + + +def run_case(env, case, plan, output, guard): + import torch + from SkillFactory.legs.contact_cycle_sim.stress import gate_residual, residual_signals + from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit + from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state + from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder + + guard.check(force_disk=True) + output.mkdir(exist_ok=False) + env.reset(seed=plan['environment_seed']) + s = state(env) + if s.robot.is_fixed_base or env.cfg.terminations.failed is None or bool(s.scenarios.any()): + raise RuntimeError('Require floating root, original failure termination and nominal scenarios') + requests = residual_signals(plan, case).to(env.device) + histories = {k: [] for k in ('reward', 'request', 'action', 'previous_action', 'terminated', 'truncated')} + thresholds = json.loads((ROOT / 'configs/contact_cycle/acceptance_v1.json').read_text())['gates'] + with PhysicalSafetyAudit(env, thresholds) as safety, SimCycleRecorder(env) as recorder: + save_tensor(output / 'initial.pt', recorder.initial_state, guard) + for k in range(plan['controls_per_case']): + guard.check() + request = requests[k] + if case['kind'] == 'gated_sine': + request = gate_residual(request, s.reference()[0], plan['gated_phases']) + recorder.before_control(None, request) + _, reward, terminated, truncated, _ = env.step(request) + recorder.end_control(terminated, truncated, k == plan['controls_per_case'] - 1) + for name, value in zip(histories, (reward, request, env.action_manager.action, + env.action_manager.prev_action, terminated, truncated)): + histories[name].append(value.detach().cpu().clone()) + safety.check() + if (k + 1) % 100 == 0: + print(f"{case['id']}: {k + 1}/900 controls", flush=True) + history = {k: torch.stack(v) for k, v in histories.items()} + trace = {k: torch.stack([f[k] for f in recorder.frames]) for k in recorder.frames[0]} + functional = json.loads((ROOT / plan['functional_config']).read_text()) + rows, first_histories = [], {} + for e, stop in enumerate(recorder.stops): + episode = {k: v[:stop, e:e+1] for k, v in trace.items()} + row, h = summarize_episode(episode, e, recorder, history, plan, s, functional) + rows.append(row) + first_histories[str(e)] = h + selected = [r['env'] for r in rows if not r['success']][:4] + complete = [r['env'] for r in rows if r['first_episode_samples'] == 4500 and not r['early_stop']] + successful = [r['env'] for r in rows if r['success']] + representative = (successful or complete)[:1] + selected = sorted(set(selected + representative)) + subset = {str(e): {k: v[:recorder.stops[e], e:e+1].clone().contiguous() for k, v in trace.items()} + for e in selected} + save_tensor(output / 'first_episode_traces.pt', subset, guard) + save_tensor(output / 'first_episode_control_histories.pt', first_histories, guard) + result = {'case': case, 'rows': rows, 'success_count': sum(r['success'] for r in rows), + 'environment_count': env.num_envs, 'saved_trace_environments': selected, + 'physics_safety': safety.report(), + 'harness_verified': all(all(r['checks'].values()) for r in rows), + 'force_provenance': {'normal': 'PhysX foot_contact net normal force, world frame', + 'external': 'declared event wrench buffers; nominal all zero', + 'guide': 'declared auxiliary fixture, no vertical guide force', + 'auxiliary': 'declared phase-dependent vertical support with r cross F moments'}, + 'remaining_window_s': guard.check(force_disk=True)} + write_json(output / 'result.json', result) + print(f"CASE_RESULT {case['id']}: {result['success_count']}/8", flush=True) + return result + + +def run_matrix(env, output, guard, registration): + results = [] + plan = registration['plan'] + for case in plan['cases']: + verify_hashes(registration['identity']) + result = run_case(env, case, plan, output / case['id'], guard) + results.append(result) + write_json(output / 'matrix_progress.json', { + 'completed_cases': [{'id': r['case']['id'], 'success_count': r['success_count']} for r in results], + 'planned_case_count': len(plan['cases']), 'remaining_window_s': guard.check()}) + baseline = all(r['success_count'] == 8 for r in results if r['case']['kind'] == 'zero') + verified = all(r['harness_verified'] for r in results) + stress = [r for r in results if r['case']['kind'] != 'zero'] + return {'passed': verified and baseline, 'passed_means': 'harness verified AND both baselines; NOT stress success', + 'harness_verified': verified, 'baseline_passed': baseline, + 'case_count': len(results), 'environment_episodes': 8 * len(results), + 'stress_case_count': len(stress), 'stress_full_success_cases': sum(r['success_count'] == 8 for r in stress), + 'stress_success_episodes': sum(r['success_count'] for r in stress), + 'case_counts': {r['case']['id']: r['success_count'] for r in results}, + 'controller_kind': 'analytic_reference_with_scripted_residual', 'accepted_skill': False, + 'trained_residual_policy': False, 'optimizer_steps': 0, 'policy_models_constructed': 0, + 'remaining_window_s': guard.check(force_disk=True), + 'limitations': ['Nominal development-only deterministic environment; no external disturbances.', + 'Gated lift/swing domain is not the all-phase domain.', + 'No learned-policy, robustness, recovery, hardware or release claim.', + 'First failure onset is 500Hz; actual environment termination/reset is control-rate.', + 'First four failures plus representative complete traces; all eight rows/histories.']} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--output', type=Path, required=True) + parser.add_argument('--supervise', action='store_true') + known, _ = parser.parse_known_args() + output = known.output.resolve() + if output.parent != OUTPUT_ROOT: + parser.error(f'Output must be a fresh attempt directory directly inside {OUTPUT_ROOT}') + if known.supervise: + raise SystemExit(supervise(output)) + guard = WindowGuard() + guard.check(force_disk=True) + registration = json.loads((output / 'preregistration.json').read_text()) + launch = json.loads((output / 'launch.json').read_text()) + if (sha256(output / 'preregistration.json') != launch['preregistration_sha256'] + or sha256(PLAN) != registration['plan_sha256']): + raise RuntimeError('Preregistration changed before launch') + verify_hashes(registration['identity']) + guard.check(reserve=1024) + claim_execution(output) + from isaaclab.app import AppLauncher + + AppLauncher.add_app_launcher_args(parser) + args = parser.parse_args() + guard.check() # last pre-Kit authorization check + app = AppLauncher(args).app + env = None + result = {'passed': False, 'harness_verified': False, 'baseline_passed': False, 'optimizer_steps': 0} + code = 1 + try: + from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg + from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv + + cfg = SimContactCycleEnvCfg() + cfg.scene.num_envs = registration['plan']['num_envs'] + cfg.sim.device = args.device + cfg.seed = registration['plan']['environment_seed'] + guard.check() + env = SimContactCycleEnv(cfg=cfg) + result = run_matrix(env, output, guard, registration) + verify_hashes(registration['identity']) + code = 0 if result['passed'] else 2 + except BaseException: + result.update(passed=False, harness_verified=False, error=traceback.format_exc()) + traceback.print_exc() + finally: + finish_run(app, env, output / 'result.json', result, code) + + +if __name__ == '__main__': + main() diff --git a/scripts/verify_wrench_physics.py b/scripts/verify_wrench_physics.py new file mode 100644 index 0000000..a119f38 --- /dev/null +++ b/scripts/verify_wrench_physics.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +"""Independent real PhysX impulse oracle: native point load vs COM-reduced load. + +Three identical isotropic bodies: correct reduction, native off-center force, +and deliberately double-counted lever arm (negative control). No training. +""" + +import argparse +import hashlib +import math +import traceback +from pathlib import Path + +from runtime_common import finish_run, write_json + + +def run(args): + import torch + from isaaclab_physx.physics import PhysxCfg + from SkillFactory.legs.contact_cycle.mechanics import reduce_to_com, rotate + + import isaaclab.sim as sim_utils + from isaaclab.assets import RigidObject, RigidObjectCfg + from isaaclab.sim import SimulationCfg, build_simulation_context + + dt, mass, radius = 0.002, 2.0, 0.1 + cfg = SimulationCfg(dt=dt, device=args.device, gravity=(0, 0, 0), + physics=PhysxCfg(solver_type=0, enable_external_forces_every_iteration=False)) + with build_simulation_context(sim_cfg=cfg) as sim: + sim._app_control_on_stop_handle = None + for i in range(3): + sim_utils.create_prim(f"/World/Fixture_{i}", "Xform", translation=(2*i, 0, 0)) + body = RigidObject(RigidObjectCfg( + prim_path="/World/Fixture_.*/Sphere", + spawn=sim_utils.SphereCfg(radius=radius, + rigid_props=sim_utils.RigidBodyPropertiesCfg(linear_damping=0, angular_damping=0), + mass_props=sim_utils.MassPropertiesCfg(mass=mass)), + )) + sim.reset() + pos = torch.tensor([[2*i, 0., 0.] for i in range(3)], device=args.device) + quat = torch.tensor([[0.2, -0.3, 0.4, math.sqrt(0.71)]], device=args.device).expand(3, -1) + body.write_root_pose_to_sim(torch.cat((pos, quat), -1)) + body.write_root_velocity_to_sim(torch.zeros(3, 6, device=args.device)) + body.update(0) + local_f = torch.tensor([[1., -2, 3]], device=args.device).expand(3, -1) + local_tau = torch.tensor([[0.02, -0.03, 0.04]], device=args.device).expand(3, -1) + lever = rotate(quat, torch.tensor([[0.04, 0.03, -0.02]], device=args.device).expand(3, -1)) + point = pos + lever + reduced = reduce_to_com(local_f, local_tau, quat, point, pos) + force = reduced[:, :3] + torques = reduced[:, 3:].clone() + positions = pos.clone() + # native point oracle, and intentional double r x F negative control + torques[1] = rotate(quat, local_tau)[1] + positions[1:] = point[1:] + body.permanent_wrench_composer.set_forces_and_torques_index( + forces=force[:, None], torques=torques[:, None], positions=positions[:, None], is_global=True) + body.write_data_to_sim() + sim.step() + body.update(dt) + velocity = body.data.root_com_vel_w.torch.clone() + inertia = 0.4 * mass * radius**2 + expected = torch.cat((force * dt/mass, reduced[:, 3:] * dt/inertia), -1) + native_difference = (velocity[0]-velocity[1]).abs().max().item() + oracle_error = (velocity[:2]-expected[:2]).abs().max().item() + negative_separation = (velocity[2]-expected[2]).abs().max().item() + body.permanent_wrench_composer.reset() + body.write_data_to_sim() + sim.step() + body.update(dt) + cleared_delta = (body.data.root_com_vel_w.torch-velocity).abs().max().item() + gates = {"native_point_equivalence": native_difference <= 2e-5, + "newton_euler_impulse": oracle_error <= 2e-5, + "double_lever_negative_control_detected": negative_separation >= 0.001, + "clear_stops_acceleration": cleared_delta <= 2e-5} + return {"passed": all(gates.values()), "accepted_skill": False, "gates": gates, + "native_point_max_velocity_difference": native_difference, + "analytical_max_velocity_error": oracle_error, + "double_lever_error": negative_separation, "clear_max_velocity_change": cleared_delta, + "measured_twist_world": velocity.tolist(), "expected_twist_world": expected.tolist(), + "mass_kg": mass, "inertia_kg_m2": inertia, "dt_s": dt, + "note": "Rigid-body force/torque API oracle only; not a Go2 tracking or sim-to-real certificate."} + + +def main(): + from isaaclab.app import AppLauncher + + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--output", type=Path, required=True) + AppLauncher.add_app_launcher_args(parser) + args = parser.parse_args() + args.output.mkdir(parents=True, exist_ok=False) + root = Path(__file__).resolve().parents[1] + files = [Path(__file__), root/"source/SkillFactory/SkillFactory/legs/contact_cycle/mechanics.py", + root/"scripts/runtime_common.py", root/"scripts/supervise_sim.py"] + write_json(args.output/"identity.json", {str(p): hashlib.sha256(p.read_bytes()).hexdigest() for p in files}) + write_json(args.output/"result.json", {"passed": False, "status": "running"}) + app = AppLauncher(args).app + result, code = {"passed": False}, 1 + try: + result = run(args) + code = 0 if result["passed"] else 2 + except BaseException: + result.update(error=traceback.format_exc()) + print(result["error"]) + finally: + finish_run(app, None, args.output/"result.json", result, code) + + +if __name__ == "__main__": + main() diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle/__init__.py b/source/SkillFactory/SkillFactory/legs/contact_cycle/__init__.py new file mode 100644 index 0000000..59c387c --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle/__init__.py @@ -0,0 +1,4 @@ +"""Go2 FL contact-cycle v1: simulator-independent contracts and test oracles.""" + +CONTRACT = "go2-fl-contact-cycle-v1" +PHASES = ("support", "unload", "lift", "swing", "land", "reload") diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle/config.py b/source/SkillFactory/SkillFactory/legs/contact_cycle/config.py new file mode 100644 index 0000000..a948980 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle/config.py @@ -0,0 +1,141 @@ +"""Fail-closed configuration for the deliberately bounded v1 fixture domain.""" + +import json +import math +from pathlib import Path + +from . import CONTRACT + + +def _keys(value, expected): + if not isinstance(value, dict) or set(value) != set(expected.split()): + raise ValueError(f"Expected exactly: {expected}") + + +def _number(value, low, high): + if type(value) not in (int, float) or not math.isfinite(value) or not low <= value <= high: + raise ValueError(f"Number outside [{low}, {high}]: {value}") + + +def _vector(value, size, bound): + if not isinstance(value, list) or len(value) != size: + raise ValueError("Invalid vector size") + for x in value: + _number(x, -bound, bound) + + +def _unique_pairs(pairs): + result = {} + for key, value in pairs: + if key in result: + raise ValueError(f"Duplicate key: {key}") + result[key] = value + return result + + +def load_config(path): + cfg = json.loads(Path(path).read_text(), object_pairs_hook=_unique_pairs) + validate_config(cfg) + return cfg + + +def require_scenario_coverage(c, count): + if type(count) is not int or count < len(c["scenarios"]): + raise ValueError("num_envs must cover every configured development scenario") + + +def validate_config(c): + _keys(c, "schema_version contract support_config phase_durations_s swing_displacement_m clearance_m " + "support base_motion sensor actuator limits scenarios events contact_transition initialization") + if type(c["schema_version"]) is not int or c["schema_version"] != 1 or c["contract"] != CONTRACT: + raise ValueError("Unsupported cycle contract") + if c["support_config"] != "configs/standing/go2_fl_support_v0.json": + raise ValueError("v1 binds the existing Go2 FL 3kg asset; rebuild/revalidate to change it") + _vector(c["phase_durations_s"], 6, 10) + for x in c["phase_durations_s"]: + _number(x, 0.2, 10) + _vector(c["swing_displacement_m"], 3, 0.03) + if c["swing_displacement_m"][2] != 0: + raise ValueError("v1 lands on the same flat plane; uneven terrain is not implemented") + _number(c["clearance_m"], 0.02, 0.06) + transition = c["contact_transition"] + _keys(transition, "separation_on_m separation_off_m dwell_s unload_tracking_scale_m") + for key in ("separation_on_m", "separation_off_m", "unload_tracking_scale_m"): + _number(transition[key], 1e-5, 0.01) + if transition["separation_off_m"] >= transition["separation_on_m"]: + raise ValueError("Separation requires ordered geometric hysteresis") + _number(transition["dwell_s"], 0.002, 0.05) + init = c["initialization"] + _keys(init, "foot_clearance_m contact_deadline_s min_load_fraction dwell_s") + _number(init["foot_clearance_m"], 0, 0) # cycle only: do not inherit standing's airborne reset + _number(init["contact_deadline_s"], 0.01, min(0.2, c["phase_durations_s"][0])) + _number(init["dwell_s"], 0.002, init["contact_deadline_s"]) + _number(init["min_load_fraction"], 0.5, 1.0) + _keys(c["support"], "kp_N_m kd_Ns_m max_N") + for key, hi in (("kp_N_m", 3000), ("kd_Ns_m", 200), ("max_N", 150)): + _number(c["support"][key], 0, hi) + _keys(c["base_motion"], "amplitude_m frequency_Hz") + _vector(c["base_motion"]["amplitude_m"], 3, 0.01) + _number(c["base_motion"]["frequency_Hz"], 0.05, 0.5) + _keys(c["sensor"], "joint_noise_std_rad velocity_noise_std_rad_s wrench_noise_std delay_steps") + for key, hi in (("joint_noise_std_rad", 0.005), ("velocity_noise_std_rad_s", 0.05), ("wrench_noise_std", 0.1)): + _number(c["sensor"][key], 0, hi) + _keys(c["actuator"], "gain bias_rad delay_steps") + _number(c["actuator"]["gain"], 0.95, 1.05) + _vector(c["actuator"]["bias_rad"], 3, 0.01) + for group in ("actuator", "sensor"): + n = c[group]["delay_steps"] + if type(n) is not int or not 0 <= n <= 2: + raise ValueError("delay_steps must be integer 0..2") + _keys(c["limits"], "force_N couple_Nm point_radius_m force_rate_N_s couple_rate_Nm_s") + for key, hi in (("force_N", 10), ("couple_Nm", 1), ("point_radius_m", 0.12), + ("force_rate_N_s", 100), ("couple_rate_Nm_s", 10)): + _number(c["limits"][key], 1e-6, hi) + if (not isinstance(c["scenarios"], list) or not c["scenarios"] + or any(not isinstance(x, str) or not x for x in c["scenarios"]) + or len(set(c["scenarios"])) != len(c["scenarios"])): + raise ValueError("Unique scenarios required") + if c["scenarios"][0] != "baseline": + raise ValueError("Scenario 0 must remain no-disturbance regression") + if not isinstance(c["events"], list): + raise ValueError("events must be a list") + for e in c["events"]: + _keys(e, "scenario body frame point_frame point_m force_N couple_Nm trigger phase start_s duration_s ramp_s") + if type(e["scenario"]) is not int or not 1 <= e["scenario"] < len(c["scenarios"]): + raise ValueError("Invalid event scenario (baseline cannot be disturbed)") + if e["body"] not in ("virtual_payload", "FL_hip", "FL_thigh", "FL_calf", "FL_foot"): + raise ValueError("Unsupported application body") + if e["frame"] not in ("body", "mount", "world") or e["point_frame"] not in ("body", "mount", "world"): + raise ValueError("Unknown frame") + if e["trigger"] not in ("time", "phase", "liftoff", "touchdown"): + raise ValueError("Unknown trigger") + if type(e["phase"]) is not int or not 0 <= e["phase"] < 6: + raise ValueError("Unknown phase") + for key in ("start_s", "duration_s", "ramp_s"): + _number(e[key], 0 if key == "start_s" else 0.01, 60) + if e["duration_s"] < 2 * e["ramp_s"]: + raise ValueError("Pulse must include both ramps") + # Contact triggers may happen anywhere in their declared phase. Reserve room for the entire pulse. + durations = c["phase_durations_s"] + trigger_latest = (0 if e["trigger"] == "time" else + sum(durations[:e["phase"] + (e["trigger"] in ("liftoff", "touchdown"))])) + if trigger_latest + e["start_s"] + e["duration_s"] >= sum(durations): + raise ValueError("Event cannot reliably finish before episode deadline") + for field, limit, rate in (("force_N", "force_N", "force_rate_N_s"), + ("couple_Nm", "couple_Nm", "couple_rate_Nm_s"), + ("point_m", "point_radius_m", None)): + _vector(e[field], 3, 1e3) + norm = math.sqrt(sum(v * v for v in e[field])) + # world points are env-origin-relative, so still bounded to the local fixture workspace. + bound = 0.65 if field == "point_m" and e["point_frame"] == "world" else c["limits"][limit] + if norm > bound + 1e-9 or (rate and norm / e["ramp_s"] > c["limits"][rate] + 1e-9): + raise ValueError(f"Event amplitude/rate outside domain: {field}") + # Conservative aggregate bound, even if pulses do not overlap. No implicit clipping. + for scenario in range(1, len(c["scenarios"])): + for field, rate in (("force_N", "force_rate_N_s"), ("couple_Nm", "couple_rate_Nm_s")): + events = [e for e in c["events"] if e["scenario"] == scenario] + if sum(math.sqrt(sum(x*x for x in e[field])) for e in events) > c["limits"][field] + 1e-9: + raise ValueError("Combined event budget exceeded") + if sum(math.sqrt(sum(x*x for x in e[field])) / e["ramp_s"] for e in events) > c["limits"][rate] + 1e-9: + raise ValueError("Combined event rate budget exceeded") + return c diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle/curriculum.py b/source/SkillFactory/SkillFactory/legs/contact_cycle/curriculum.py new file mode 100644 index 0000000..1e56516 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle/curriculum.py @@ -0,0 +1,146 @@ +"""Episode-local train/validation sampling. No independent-test generator is provided. + +All ranges are engineering assumptions, not calibrated Go2 uncertainties. +Directions/partitions are in each event's DECLARED command frame, not world space. +Reset sampling is keyed by seed, environment index, episode counter and stage; +reset order/batch grouping cannot change another environment's future samples. +""" + +import hashlib +import math + +import torch + +from .config import validate_config +from .mechanics import canonical_ids + +DIRECTION_EXCLUSIONS = ((0.35, 0.55), (-2.55, -2.35)) +POINT_RADIUS_MAX = 0.08 +EDGE_DELAY_MAX = 0.08 +SAMPLER_VERSION = "cycle-episode-sampler-v1" + + +def curriculum_stage(update): + if type(update) is not int or not 1 <= update <= 2048: + raise ValueError("Curriculum update must be in [1, 2048]") + return 0 if update <= 512 else 1 if update <= 1024 else 2 + + +def excluded_direction(v): + azimuth = torch.atan2(v[..., 1], v[..., 0]) + return sum(((azimuth >= lo) & (azimuth <= hi)).int() for lo, hi in DIRECTION_EXCLUSIONS).bool() + + +def direction(generator): + # Conditional isotropic sphere sampling. Deliberately not uniform azimuth/elevation. + for _ in range(1024): + v = torch.randn(3, generator=generator) + norm = v.norm() + if norm > 1e-8 and not excluded_direction(v): + return v / norm + raise RuntimeError("Direction rejection sampling exhausted") + + +class EpisodeSampler: + def __init__(self, config, count, seed, split="train", update=1): + validate_config(config) + if split not in ("train", "validation"): + raise ValueError("Independent testing is deliberately unavailable to the trainer") + if type(count) is not int or count < 1 or type(seed) is not int or seed < 0: + raise ValueError("Invalid batch/seed") + if len(config["scenarios"]) != 4 or {e["scenario"] for e in config["events"]} != {1, 2, 3}: + raise ValueError("Sampler v1 requires nominal plus three development scenario families") + if any(e["point_frame"] == "world" for e in config["events"]): + raise ValueError("Radius partition applies only to body/mount local points") + self.config, self.count, self.seed, self.split = config, count, seed, split + self.episodes = torch.zeros(count, dtype=torch.long) + self.set_update(update) + + def set_update(self, update): + self.stage = curriculum_stage(update) + + def state_dict(self): + return {"version": SAMPLER_VERSION, "seed": self.seed, "split": self.split, + "count": self.count, "stage": self.stage, "episodes": self.episodes.clone()} + + def load_state_dict(self, data): + for key in ("version", "seed", "split", "count"): + if data[key] != self.state_dict()[key]: + raise ValueError("Sampler identity mismatch") + if (data["episodes"].shape != (self.count,) or data["episodes"].dtype != torch.long + or (data["episodes"] < 0).any() or data["stage"] not in (0, 1, 2)): + raise ValueError("Invalid sampler state") + self.episodes.copy_(data["episodes"].cpu()) + self.stage = data["stage"] + + def sample(self, ids): + ids = canonical_ids(torch.as_tensor(ids, device="cpu"), self.count) + events, limits = self.config["events"], self.config["limits"] + n, k = len(ids), len(events) + out = {"scenario": torch.zeros(n, dtype=torch.long), "stand": torch.zeros(n, dtype=torch.bool), + "commands": torch.zeros(n, k, 6), "points": torch.zeros(n, k, 3), + "delays": torch.zeros(n, k), "stage": torch.zeros(n, dtype=torch.long)} + for row, env_id in enumerate(ids.tolist()): + stage = self.stage if self.split == "train" else 2 + key = f"{SAMPLER_VERSION}:{self.seed}:{self.split}:{env_id}:{int(self.episodes[env_id])}:{stage}" + seed = int.from_bytes(hashlib.sha256(key.encode()).digest()[:8], "little") % (2**63 - 1) + generator = torch.Generator().manual_seed(seed) + regression = bool(torch.rand((), generator=generator) < 0.25) + if stage == 0: + out["stand"][row] = regression + elif regression: + # Half of final-stage regression revisits the preceding disturbance stage. + stage = 0 if stage == 1 or torch.rand((), generator=generator) < 0.5 else 1 + out["stage"][row] = stage + scenario = 0 if stage == 0 else int(torch.randint(1, 4, (), generator=generator)) + out["scenario"][row] = scenario + for j, event in enumerate(events): + siblings = sum(e["scenario"] == event["scenario"] for e in events) + # Conservative sums enforce both magnitude AND declared-frame rate budgets, + # even if all same-scenario pulses overlap. Frame rotation rates are separate. + caps = [min(limits["force_N"], limits["force_rate_N_s"] * event["ramp_s"]) / siblings, + min(limits["couple_Nm"], limits["couple_rate_Nm_s"] * event["ramp_s"]) / siblings] + for axis, cap in enumerate(caps): + gain = 0.0 if stage == 0 or (stage == 1 and axis == 1) else 0.5 if stage == 1 else 1.0 + out["commands"][row, j, axis*3:(axis+1)*3] = ( + direction(generator) * cap * gain * torch.rand((), generator=generator)) + # Uniform volume within the allowed local ball; not held-out outer shell. + radius = min(POINT_RADIUS_MAX, limits["point_radius_m"]) * float( + torch.rand((), generator=generator)) ** (1/3) + point = torch.randn(3, generator=generator) + out["points"][row, j] = point / point.norm().clamp(min=1e-8) * radius + out["delays"][row, j] = (EDGE_DELAY_MAX * torch.rand((), generator=generator) + if event["trigger"] in ("liftoff", "touchdown") else event["start_s"]) + self.episodes[env_id] += 1 + return out + + +def assert_sample_domain(config, batch): + """Independent batched domain audit; can run on recorded episode samples.""" + if not all(torch.isfinite(v).all() for v in batch.values()): + raise ValueError("Nonfinite sampled command") + commands, points = batch["commands"], batch["points"] + if (points.norm(dim=-1) > POINT_RADIUS_MAX + 1e-6).any(): + raise ValueError("Reserved point shell entered") + for axis in (0, 3): + v = commands[..., axis:axis+3] + if (excluded_direction(v) & (v.norm(dim=-1) > 1e-8)).any(): + raise ValueError("Reserved direction cone entered") + for scenario in range(1, 4): + indices = [j for j, e in enumerate(config["events"]) if e["scenario"] == scenario] + ramps = torch.tensor([config["events"][j]["ramp_s"] for j in indices], device=commands.device) + for axis, magnitude, rate in ((0, "force_N", "force_rate_N_s"), (3, "couple_Nm", "couple_rate_Nm_s")): + norms = commands[:, indices, axis:axis+3].norm(dim=-1) + if (norms.sum(-1) > config["limits"][magnitude] + 1e-5).any(): + raise ValueError("Aggregate magnitude budget exceeded") + if ((norms/ramps).sum(-1) > config["limits"][rate] + 1e-4).any(): + raise ValueError("Aggregate rate budget exceeded") + for j, e in enumerate(config["events"]): + delay = batch["delays"][:, j] + if e["trigger"] in ("liftoff", "touchdown"): + if ((delay < 0) | (delay > EDGE_DELAY_MAX + 1e-6)).any(): + raise ValueError("Reserved edge-delay band entered") + elif not torch.allclose(delay, torch.full_like(delay, e["start_s"]), atol=1e-6, rtol=0): + raise ValueError("Unexpected time/phase start modification") + if not math.isfinite(float(commands.norm())): + raise ValueError("Invalid command norm") diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle/mechanics.py b/source/SkillFactory/SkillFactory/legs/contact_cycle/mechanics.py new file mode 100644 index 0000000..ec7c93a --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle/mechanics.py @@ -0,0 +1,198 @@ +"""SI, right-handed, xyzw quaternion mechanics; no Isaac imports. + +External disturbances are NOT contact-force commands. A free couple is specified +at the force application point; reduction to COM adds r x F exactly once. +""" + +import torch + + +def canonical_ids(ids, count): + """Isaac emits int32; public reset and learner boundaries accept either integer width.""" + if (not isinstance(ids, torch.Tensor) or ids.ndim != 1 or ids.dtype not in (torch.int32, torch.int64) + or (ids < 0).any() or (ids >= count).any() or ids.unique().numel() != len(ids)): + raise ValueError("Require unique in-range int32/int64 reset indices") + return ids.long() + + +def rotate(q, v): + """Rotate vectors by unit xyzw q (caller owns pose validity).""" + u = q[..., :3] + t = 2 * torch.cross(u, v, dim=-1) + return v + q[..., 3:] * t + torch.cross(u, t, dim=-1) + + +def inverse_rotate(q, v): + return rotate(torch.cat((-q[..., :3], q[..., 3:]), -1), v) + + +def reduce_to_com(force, couple, frame_quat, point_world, com_world): + f = rotate(frame_quat, force) + return torch.cat((f, rotate(frame_quat, couple) + torch.cross(point_world - com_world, f, dim=-1)), -1) + + +def cone_violation(force, normal, mu): + """Return unilateral and Coulomb residuals; never project measured physics.""" + fn = (force * normal).sum(-1) + ft = (force - fn[..., None] * normal).norm(dim=-1) + return (-fn).clamp(min=0), (ft - mu * fn.clamp(min=0)).clamp(min=0) + + +def uniform_directions(count, generator, device="cpu"): + """Continuous isotropic directions, not uniform Euler angles or axis labels.""" + x = torch.randn(count, 3, generator=generator, device=device) + return x / x.norm(dim=-1, keepdim=True).clamp(min=1e-12) + + +def delivered_events(events, scenario, fired, completed, integral, tolerance_s=0.002): + """Per-env delivery gate: scheduling bits alone cannot certify a disturbance.""" + expected = scenario[:, None] == scenario.new_tensor([e["scenario"] for e in events]) + area = integral.new_tensor([e["duration_s"] - e["ramp_s"] for e in events]) + return ((fired == expected).all(-1) & (completed == expected).all(-1) + & ((integral - expected * area).abs() <= tolerance_s).all(-1)) + + +class ContactLatch: + """Force hysteresis + dwell. Read once per physics step, not per observation.""" + + def __init__(self, count, device, on=2.0, off=0.5, dwell=0.01): + self.on, self.off, self.dwell = on, off, dwell + self.contact = torch.zeros(count, device=device, dtype=torch.bool) + self.timer = torch.zeros(count, device=device) + + def reset(self, ids): + self.contact[ids] = False + self.timer[ids] = 0 + + def update(self, normal_force, dt): + candidate = torch.where(self.contact, normal_force > self.off, normal_force >= self.on) + changed = candidate != self.contact + self.timer = torch.where(changed, self.timer + dt, torch.zeros_like(self.timer)) + commit = changed & (self.timer >= self.dwell - 1e-7) + touchdown = commit & candidate + liftoff = commit & ~candidate + self.contact = torch.where(commit, candidate, self.contact) + self.timer[commit] = 0 + return liftoff, touchdown + + +class ContactAcquisition: + """Bounded load acquisition INSIDE support time; never restarts/pauses the phase clock. + + Failure is latched at the deadline and cannot be repaired by a later landing. + Separate from contact loss: a never-contacted foot must not escape termination. + """ + + def __init__(self, count, device, deadline, dwell, min_load_fraction, max_gap): + self.deadline, self.dwell = deadline, dwell + self.min_load_fraction, self.max_gap = min_load_fraction, max_gap + self.acquired = torch.zeros(count, device=device, dtype=torch.bool) + self.failed = torch.zeros_like(self.acquired) + self.timer = torch.zeros(count, device=device) + + def reset(self, ids): + self.acquired[ids] = self.failed[ids] = False + self.timer[ids] = 0 + + def update(self, time, gap, normal_force, weight, dt): + valid = (gap <= self.max_gap) & (normal_force >= self.min_load_fraction * weight) + pending = ~self.acquired & ~self.failed + self.timer = torch.where(pending & valid, self.timer + dt, torch.zeros_like(self.timer)) + # Accept a completed dwell at the deadline, but never after it. + self.acquired |= pending & (self.timer >= self.dwell - 1e-7) & (time <= self.deadline + 1e-7) + self.failed |= ~self.acquired & (time >= self.deadline - 1e-7) + + +class SeparationLatch: + """Geometric liftoff is NOT a loss of normal load at an unloaded, touching foot. + + Ground contact arms liftoff; gap hysteresis/dwell confirms physical separation. + Remember onset phase so dwelling across unload->lift cannot launder early lift. + Premature separation is sticky until indexed reset, including sub-control blips. + """ + + def __init__(self, count, device, on, off, dwell): + self.on, self.off, self.dwell = on, off, dwell + self.separated = torch.zeros(count, device=device, dtype=torch.bool) + self.armed = torch.zeros_like(self.separated) + self.premature = torch.zeros_like(self.separated) + self.timer = torch.zeros(count, device=device) + self.onset_phase = torch.full((count,), -1, device=device, dtype=torch.long) + self.edge_phase = torch.full_like(self.onset_phase, -1) + + def reset(self, ids): + self.separated[ids] = self.armed[ids] = self.premature[ids] = False + self.timer[ids] = 0 + self.onset_phase[ids] = self.edge_phase[ids] = -1 + + def update(self, gap, loaded_contact, phase, dt): + self.armed |= loaded_contact & (gap <= self.off) + candidate = torch.where(self.separated, gap > self.off, gap >= self.on) + changed = candidate != self.separated + begin = changed & (self.timer == 0) + self.onset_phase = torch.where(begin, phase, self.onset_phase) + self.timer = torch.where(changed, self.timer + dt, torch.zeros_like(self.timer)) + commit = changed & (self.timer >= self.dwell - 1e-7) + rising = commit & candidate + liftoff = rising & self.armed + self.edge_phase = torch.where(liftoff, self.onset_phase, -1) + # "Departure" requires a preceding ground contact. Never-contacted flight is + # independently bounded by ContactAcquisition, not silently permitted forever. + self.premature |= liftoff & (self.onset_phase < 2) + self.separated = torch.where(commit, candidate, self.separated) + self.timer[commit] = 0 + return liftoff + + +class WrenchSchedule: + """One-shot per episode triggers, finite ramped pulses, selective reset. + + start_s is time after reset for time triggers, otherwise delay after first + phase entry/contact edge. A missed trigger does not silently become a pulse. + duration includes ramp-in AND ramp-out; ending is exactly zero. + """ + + def __init__(self, events, count, device): + self.events = events + self.fired = torch.zeros(count, len(events), dtype=torch.bool, device=device) + self.start = torch.zeros(count, len(events), device=device) + self.envelope = torch.zeros_like(self.start) + self.integral = torch.zeros_like(self.start) + self.completed = torch.zeros_like(self.fired) + # Optional episode-local training values; None preserves the frozen preflight semantics. + self.commands = None + self.delays = None + + def reset(self, ids): + self.fired[ids] = False + self.start[ids] = 0 + self.envelope[ids] = self.integral[ids] = 0 + self.completed[ids] = False + + def step(self, time, phase, liftoff, touchdown, scenario, dt=0.0, liftoff_phase=None): + values = time.new_zeros(len(time), len(self.events), 6) + for j, event in enumerate(self.events): + trigger = event["trigger"] + delay = (torch.full_like(time, event["start_s"]) if self.delays is None else self.delays[:, j]) + condition = { + "time": time >= delay, + "phase": phase == event["phase"], + "liftoff": liftoff & (phase == event["phase"]) & ( + (phase if liftoff_phase is None else liftoff_phase) == event["phase"]), + "touchdown": touchdown & (phase == event["phase"]), + }[trigger] + fire = condition & ~self.fired[:, j] & (scenario == event["scenario"]) + self.start[fire, j] = ( + delay[fire] if trigger == "time" else time[fire] + delay[fire] + ) + self.fired[:, j] |= fire + age = time - self.start[:, j] + envelope = torch.minimum(age / event["ramp_s"], (event["duration_s"] - age) / event["ramp_s"]).clamp(0, 1) + envelope *= self.fired[:, j] + self.envelope[:, j] = envelope + self.integral[:, j] += envelope * dt + self.completed[:, j] |= self.fired[:, j] & (age >= event["duration_s"]) + vector = (time.new_tensor(event["force_N"] + event["couple_Nm"]) + if self.commands is None else self.commands[:, j]) + values[:, j] = envelope[:, None] * vector + return values diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle/metrics.py b/source/SkillFactory/SkillFactory/legs/contact_cycle/metrics.py new file mode 100644 index 0000000..37804a4 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle/metrics.py @@ -0,0 +1,112 @@ +"""CPU-recomputable phase/episode metrics. Missing phases fail closed.""" + +import math + +import torch + +from .mechanics import cone_violation + + +def summarize(trace, dt, thresholds, scenario_names): + """All inputs [physics_steps, env, ...]; preserve failed samples, never reset.""" + t = trace + rows = [] + for env in range(t["phase"].shape[1]): + phase, progress = t["phase"][:, env].long(), t["progress"][:, env] + stance = ((phase == 0) & (t["time"][:, env] >= 2)) | ((phase == 5) & (progress >= 0.8)) + swing, land = phase == 3, phase == 4 + fn = t["normal"][:, env, 2] + weight = float(t["weight"][0, env]) + error = (t["foot"][:, env] - t["target"][:, env]).norm(dim=-1) + joint_excess = t["joint_excess"][:, env] + normal = torch.zeros_like(t["normal"][:, env]) + normal[:, 2] = 1 + unilateral, friction = cone_violation(t["normal"][:, env] + t["friction"][:, env], normal, + thresholds["friction_cone_mu_upper"]) + complete = (all((phase == i).any().item() for i in range(6)) + and bool(stance.any() and swing.any() and land.any())) + finite = all(torch.isfinite(v[:, env]).all().item() for v in t.values()) + # Missing coverage is represented by null + failed gate, not NaN or an invented zero. + row = {"env": env, "scenario": scenario_names[int(t["scenario"][0, env])], + "complete": complete, "finite": finite} + if not complete or not finite: + row.update(success=False, failure_reasons=["missing_phase_or_nonfinite"]) + rows.append(row) + continue + contact = fn > 0.5 * weight + reload = phase == 5 + landed = torch.nonzero((phase >= 4) & (fn > 2)).flatten() + recovery = None + if len(landed): + start = int(landed[0]) + stable = contact & (t["height_error"][:, env] < 0.02) & (t["slip_speed"][:, env] < 0.03) & reload + dwell = 0 + for k in range(start, len(stable)): + dwell = dwell + 1 if stable[k] else 0 + if dwell * dt >= 0.2 - 1e-7: + recovery = (k - start + 1) * dt + break + row.update( + stance_contact_fraction=float(contact[stance].float().mean()), + stance_load_mae_weight_fraction=float((fn[stance]-t["desired_force"][:, env][stance]).abs().mean()/weight), + height_error_m_max=float(t["height_error"][:, env][t["time"][:, env] >= 2].max()), + swing_rms_m=float(error[swing].square().mean().sqrt()), + swing_clearance_m_min=float(t["gap"][:, env][swing].min()), + swing_contact_fraction=float((fn[swing] > 2).float().mean()), + stance_slip_m=float((t["slip_speed"][:, env] * (stance & (fn > 2))).sum() * dt), + landing_peak_N=float(fn[land | reload].max()), + landing_impulse_Ns=float(fn[land].sum() * dt), + recovery_s=recovery, + joint_limit_excess_rad_max=float(joint_excess.max()), + joint_limit_violation_steps=int((joint_excess > thresholds["joint_limit_excess_rad_max"]).sum()), + would_terminate_steps=int(t["would_fail"][:, env].sum()), + max_tilt_rad=float(t["tilt"][:, env].max()), + normalized_effort_velocity_max=float(t["limit_ratio"][:, env].max()), + unilateral_residual_N_max=float(unilateral.max()), + friction_residual_N_max=float(friction.max()), + auxiliary_peak_N=float(t["auxiliary"][:, env, :, :3].norm(dim=-1).sum(-1).max()), + auxiliary_vertical_impulse_Ns=float(t["auxiliary"][:, env, :, 2].sum() * dt), + guide_force_peak_N=float(t["guide"][:, env, :, :3].norm(dim=-1).sum(-1).max()), + external_force_peak_N=float(t["external"][:, env, :, :3].norm(dim=-1).sum(-1).max()), + external_couple_com_peak_Nm=float(t["external"][:, env, :, 3:].norm(dim=-1).sum(-1).max()), + ) + row["phase_metrics"] = { + str(i): {"samples": int((phase == i).sum()), + "tracking_rms_m": float(error[phase == i].square().mean().sqrt()), + "normal_force_mean_N": float(fn[phase == i].mean()), + "normal_force_peak_N": float(fn[phase == i].max()), + "gap_min_m": float(t["gap"][:, env][phase == i].min())} + for i in range(6) + } + lift_deadline = (phase == 2) & (progress > 0.9) + checks = { + "task_termination": row["would_terminate_steps"] == 0, + "tilt": row["max_tilt_rad"] <= 0.6, + "lift_deadline": bool(lift_deadline.any()) and bool( + ((t["gap"][:, env][lift_deadline] >= 0.02) & (fn[lift_deadline] <= 2)).all()), + "stance_contact": row["stance_contact_fraction"] >= thresholds["stance_contact_fraction_min"], + "load": row["stance_load_mae_weight_fraction"] <= thresholds["stance_load_mae_weight_fraction_max"], + "height": row["height_error_m_max"] <= thresholds["height_error_m_max"], + "swing_tracking": row["swing_rms_m"] <= thresholds["swing_rms_m_max"], + "clearance": row["swing_clearance_m_min"] >= thresholds["swing_clearance_m_min"], + "airborne": row["swing_contact_fraction"] <= thresholds["swing_contact_fraction_max"], + "slip": row["stance_slip_m"] <= thresholds["stance_slip_m_max"], + "impact": row["landing_peak_N"] <= thresholds["landing_peak_weight_fraction_max"] * weight, + "recovery": recovery is not None and recovery <= thresholds["recovery_s_max"], + "joint_limits": row["joint_limit_excess_rad_max"] <= thresholds["joint_limit_excess_rad_max"], + "actuation": row["normalized_effort_velocity_max"] <= thresholds["normalized_effort_velocity_max"], + "unilateral": row["unilateral_residual_N_max"] <= thresholds["unilateral_residual_N_max"], + "friction": row["friction_residual_N_max"] <= thresholds["friction_residual_N_max"], + } + row.update(success=all(checks.values()), failure_reasons=[k for k, v in checks.items() if not v], gates=checks) + rows.append(row) + return rows + + +def wilson(successes, count): + if count <= 0 or not 0 <= successes <= count: + raise ValueError("Invalid trial count") + p, z = successes/count, 1.959963984540054 + center = (p + z*z/(2*count))/(1+z*z/count) + half = z * math.sqrt(p*(1-p)/count + z*z/(4*count*count))/(1+z*z/count) + return [center-half, center+half] diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle/ppo.py b/source/SkillFactory/SkillFactory/legs/contact_cycle/ppo.py new file mode 100644 index 0000000..7d715d6 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle/ppo.py @@ -0,0 +1,211 @@ +"""New 32D symmetric PPO contract; never loads standing/Reach checkpoints. + +The frozen standing module supplies only generic PPO math and RNG helpers. +No optimizer is invoked merely by importing or preparing a run. +""" + +import io +import math +import os +import tempfile +from pathlib import Path + +import torch +from torch import nn +from torch.distributions import Normal + +from ..standing_ppo import ppo_step, restore_rng, rng_state # noqa: F401 +from .mechanics import canonical_ids +from .task import FAILURE_NAMES + +CONTRACT = "go2-fl-contact-cycle-v1" +FEATURE_VERSION = "contact-cycle-32-fixed-si-v1" +CHECKPOINT_FORMAT = "skillfactory.contact-cycle-ppo-checkpoint.v1" + + +def features(policy): + if (not isinstance(policy, torch.Tensor) or policy.ndim != 2 or policy.shape[1] != 32 + or len(policy) < 1 or policy.dtype != torch.float32 or not torch.isfinite(policy).all()): + raise ValueError("Require finite float32 [B,32] policy sensors/commands, not privileged observations") + return policy + + +class CycleActorCritic(nn.Module): + def __init__(self, hidden=128, initial_std=0.15): + super().__init__() + if type(hidden) is not int or hidden < 8 or not math.isfinite(initial_std) or not 0.02 <= initial_std <= 0.5: + raise ValueError("Invalid network configuration") + self.model_config = {"hidden": hidden, "initial_std": initial_std} + offset, scale = torch.zeros(32), torch.ones(32) + offset[:3] = torch.tensor([0., 0.8, -1.5]) + scale[:3], scale[3:6] = 0.5, 10.0 + scale[6:12] = torch.tensor([50., 50., 50., 5., 5., 5.]) + scale[15:18], scale[28:31], scale[31] = 5.0, 0.4, 50.0 + self.register_buffer("offset", offset) + self.register_buffer("scale", scale) + self.actor = nn.Sequential(nn.Linear(32, hidden), nn.Tanh(), nn.Linear(hidden, hidden), nn.Tanh(), + nn.Linear(hidden, 3)) + self.critic = nn.Sequential(nn.Linear(32, hidden), nn.Tanh(), nn.Linear(hidden, hidden), nn.Tanh(), + nn.Linear(hidden, 1)) + for layer in self.modules(): + if isinstance(layer, nn.Linear): + nn.init.orthogonal_(layer.weight, math.sqrt(2)) + nn.init.zeros_(layer.bias) + nn.init.orthogonal_(self.actor[-1].weight, 0.01) + nn.init.orthogonal_(self.critic[-1].weight, 1.0) + self.log_std = nn.Parameter(torch.full((3,), math.log(initial_std))) + self.float() + + def distribution_value(self, x): + x = ((features(x) - self.offset) / self.scale).clamp(-10, 10) + return (Normal(self.actor(x), self.log_std.clamp(math.log(0.02), math.log(0.5)).exp(), validate_args=False), + self.critic(x).squeeze(-1)) + + def act(self, x, deterministic=False): + dist, value = self.distribution_value(x) + latent = dist.mean if deterministic else dist.sample() + return latent.tanh(), latent, dist.log_prob(latent).sum(-1), value + + def evaluate(self, x, latent): + dist, value = self.distribution_value(x) + return dist.log_prob(latent).sum(-1), value + + def value(self, x): + return self.distribution_value(x)[1] + + +@torch.no_grad() +def bootstrap_values(model, next_features, terminated, truncated, info): + value = model.value(features(next_features)) + timeout = truncated & ~terminated + if timeout.any(): + ids, terminal = info.get("terminal_ids"), info.get("terminal_observation") + if ids is None or terminal is None: + raise RuntimeError("Missing/invalid pre-reset timeout observations") + try: + ids = canonical_ids(ids, len(value)) + except ValueError as error: + raise RuntimeError("Invalid terminal indices") from error + if not torch.isin(timeout.nonzero().flatten(), ids).all(): + raise RuntimeError("Missing pre-reset timeout observations") + terminal_x = features(terminal["policy"]) + if len(terminal_x) != len(ids): + raise RuntimeError("Terminal observation/index mismatch") + selected = timeout[ids] + value[ids[selected]] = model.value(terminal_x[selected]) + value[terminated] = 0 + return value + + +@torch.no_grad() +def collect_rollout(env, model, x, config, guard=lambda: None): + data = {key: torch.empty(config["horizon"], env.num_envs, *shape, device=env.device, dtype=dtype) + for key, shape, dtype in (("features", (32,), torch.float32), ("latent", (3,), torch.float32), + ("log_prob", (), torch.float32), ("value", (), torch.float32), ("reward", (), torch.float32), + ("next_value", (), torch.float32), ("done", (), torch.bool), + ("phase", (), torch.long), ("failure_flags", (len(FAILURE_NAMES),), torch.bool))} + failures = timeouts = 0 + for step in range(config["horizon"]): + guard() + action, latent, logp, value = model.act(x) + obs, reward, terminated, truncated, info = env.step(action) + nx = features(obs["policy"]) + nv = bootstrap_values(model, nx, terminated, truncated, info) + reward = reward / env.step_dt - config["failure_penalty"] * terminated.float() + if not torch.isfinite(reward).all() or not torch.isfinite(nv).all(): + raise RuntimeError("Nonfinite rollout rewards/values") + for key, item in (("features", x), ("latent", latent), ("log_prob", logp), ("value", value), + ("reward", reward), ("next_value", nv), ("done", terminated | truncated)): + data[key][step].copy_(item) + diagnostic = info.get("cycle_step") + data["phase"][step].fill_(-1) + data["failure_flags"][step].zero_() + if diagnostic is not None: + data["phase"][step].copy_(diagnostic["phase"]) + data["failure_flags"][step].copy_(diagnostic["failure_flags"]) + failures += int(terminated.sum()) + timeouts += int(truncated.sum()) + x = nx + return data, x, failures, timeouts + + +def rollout_diagnostics(data, cfg): + """Aggregate already-collected samples; no policy inference or optimizer call.""" + from ..policy import compute_gae + + adv, returns = compute_gae(data["reward"], data["value"], data["next_value"], data["done"], + cfg["gamma"], cfg["gae_lambda"]) + + def stats(x): + return {"min": float(x.min()), "mean": float(x.mean()), "max": float(x.max()), + "std": float(x.std(unbiased=False))} + + phases = data["phase"].flatten() + return {"phase_control_samples": torch.bincount(phases[phases >= 0], minlength=6).cpu().tolist(), + "missing_diagnostic_samples": int((phases < 0).sum()), + "terminal_causes": dict(zip(FAILURE_NAMES, data["failure_flags"].sum((0, 1)).cpu().tolist())), + "reward": stats(data["reward"]), "advantage_before_normalization": stats(adv), + "return_target": stats(returns)} + + +def selection_score(rows): + """Validation only. Failed/missing samples cannot benefit from early termination. + + Each failure receives fixed worst cost 16 (=10+2+2+2); each successful + normalized term is clipped to [0,2]. All candidates/thresholds remain locked + before any future independent testing. + """ + if not rows: + raise ValueError("Empty validation is not a candidate") + scores = [] + for row in rows: + if not row.get("success", False): + scores.append(16.) + continue + values = [row["swing_rms_m"]/0.025, row["stance_load_mae_weight_fraction"]/0.25, + row["stance_slip_m"]/0.03] + if not all(math.isfinite(v) and v >= 0 for v in values): + raise ValueError("Nonfinite/negative validation metric") + scores.append(sum(min(v, 2.) for v in values)) + return sum(scores)/len(scores) + + +def save_checkpoint(path, model, optimizer, identity, update, sampler, best, budget, max_bytes=None): + path = Path(path) + data = {"format": CHECKPOINT_FORMAT, "contract": CONTRACT, "features": FEATURE_VERSION, + "model_config": model.model_config, "model": model.state_dict(), "optimizer": optimizer.state_dict(), + "identity": identity, "update": update, "sampler": sampler.state_dict(), "best": best, + "rng": rng_state(), "budget_snapshot": budget, "accepted_skill": False, + "resume_semantics": "Learner/RNG/curriculum counters restored; physics episodes restart, not exact replay"} + buffer = io.BytesIO() + torch.save(data, buffer) + payload = buffer.getvalue() + if max_bytes is not None and len(payload) > max_bytes: + raise RuntimeError("Insufficient artifact budget for atomic checkpoint temporary file") + path.parent.mkdir(parents=True, exist_ok=True) + temporary = None + try: + with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as stream: + temporary = Path(stream.name) + stream.write(payload) + stream.flush() + os.fsync(stream.fileno()) + os.replace(temporary, path) + finally: + if temporary is not None: + temporary.unlink(missing_ok=True) + return len(payload) + + +def restore_checkpoint(path, model, optimizer, identity, sampler): + data = torch.load(path, map_location=next(model.parameters()).device, weights_only=True) + if (data["format"] != CHECKPOINT_FORMAT or data["contract"] != CONTRACT or data["features"] != FEATURE_VERSION + or data["identity"] != identity or data["model_config"] != model.model_config + or type(data["update"]) is not int or data["update"] < 0 + or any(not torch.isfinite(v).all() for v in data["model"].values())): + raise ValueError("Cycle checkpoint/config/source mismatch") + sampler.load_state_dict(data["sampler"]) + model.load_state_dict(data["model"]) + optimizer.load_state_dict(data["optimizer"]) + restore_rng(data["rng"]) + return data diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle/task.py b/source/SkillFactory/SkillFactory/legs/contact_cycle/task.py new file mode 100644 index 0000000..86a6990 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle/task.py @@ -0,0 +1,78 @@ +"""Explicit timed commands; phase-conditioned objective, no all-phase contact bonus.""" + +import torch + + +def smooth(x): + x = x.clamp(0, 1) + return x.square() * (3 - 2 * x) + + +def command(time, cfg, origin, weight): + durations = time.new_tensor(cfg["phase_durations_s"]) + ends = durations.cumsum(0) + phase = torch.bucketize(time.contiguous(), ends[:-1], right=True) + starts = torch.cat((time.new_zeros(1), ends[:-1])) + progress = ((time - starts[phase]) / durations[phase]).clamp(0, 1) + u = smooth(progress) + target = origin.clone() + delta = time.new_tensor(cfg["swing_displacement_m"]) + xy_progress = torch.where(phase < 3, 0.0, torch.where(phase == 3, u, 1.0)) + target += xy_progress[:, None] * delta + height = torch.where(phase == 2, u, torch.where(phase == 3, 1.0, torch.where(phase == 4, 1-u, 0.0))) + target[:, 2] += cfg["clearance_m"] * height + # A genuine three-dimensional swing curve, zero derivative at endpoints. + target[:, 2] += torch.where(phase == 3, 0.01 * torch.sin(torch.pi * progress).square(), 0.0) + load_fraction = torch.where(phase == 0, 1.0, torch.where(phase == 1, 1-u, torch.where(phase == 5, u, 0.0))) + return phase, progress, target, weight * load_fraction, 1-load_fraction + + +FAILURE_NAMES = ("nonfinite", "contact_acquisition_timeout", "premature_separation", "lift_deadline", + "swing_clearance", "reload_deadline", "low_height", "tilt") + + +def phase_failures(phase, progress, gap, normal_force, weight): + return { + "lift_deadline": (phase == 2) & (progress > 0.9) & ((gap < 0.02) | (normal_force > 2)), + "swing_clearance": (phase == 3) & (gap < 0.01), + "reload_deadline": (phase == 5) & (progress > 0.8) & (normal_force < 0.5 * weight), + } + + +def phase_objective(phase, progress, error, gap, normal_force, desired_force, slip_speed, impact, weight, + unload_tracking_scale_m=0.005): + """Dimensionless shaped reward and deadline violations; not acceptance metrics. + + No early-contact bonus in lift/swing. No air-time bonus in support/reload. + Measured ground force is privileged reward input, NEVER an actor observation. + """ + airborne = (phase == 2) | (phase == 3) + stance = (phase == 0) | (phase == 5) + load_score = torch.exp(-((normal_force - desired_force) / (0.25 * weight)).square()) + track = torch.exp(-(error / 0.025).square()) + reward = torch.where(airborne, track - (normal_force > 2).float(), load_score) + # Unload means shed load while holding the ground target, NOT lift early to attain Fn=0. + unload_track = torch.exp(-(error / unload_tracking_scale_m).square()) + reward = torch.where(phase == 1, load_score + unload_track - 1, reward) + reward = torch.where(phase == 4, track - (impact / (3 * weight)).square(), reward) + reward -= stance.float() * (slip_speed / 0.05).square().clamp(max=4) + reward -= (phase == 3).float() * (gap < 0.02).float() + # End-of-phase deadlines prevent persistent contact AND indefinite flight. + violation = torch.stack(list(phase_failures(phase, progress, gap, normal_force, weight).values()), -1).any(-1) + return reward, violation + + +class DelayLine: + """Control-clock history with selective reset; repeated reads never advance it.""" + + def __init__(self, count, width, delay, device): + self.delay = delay + self.data = torch.zeros(delay + 1, count, width, device=device) + + def reset(self, ids, value=0.0): + self.data[:, ids] = value + + def push(self, value): + self.data = torch.roll(self.data, -1, 0) + self.data[-1] = value + return self.data[0].clone() diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle/training.py b/source/SkillFactory/SkillFactory/legs/contact_cycle/training.py new file mode 100644 index 0000000..dbaa41d --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle/training.py @@ -0,0 +1,304 @@ +"""Pre-registration, explicit authorization and fail-closed multi-seed budget ledger. + +This file has no simulator imports. Preparation is not permission to optimize. +The budget is an absolute CLOCK_BOOTTIME window (includes suspend and same-boot +downtime), not adjustable UTC. Reboots and legacy ledgers fail closed. Reserved +interrupted rollouts are not refunded; changing this code does not authorize migration. +""" + +import fcntl +import hashlib +import io +import json +import math +import os +import tempfile +import time +from pathlib import Path + +from .config import load_config +from .ppo import CONTRACT, FEATURE_VERSION + + +class BudgetExceeded(RuntimeError): + pass + + +def digest(value): + encoded = json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False).encode() + return hashlib.sha256(encoded).hexdigest() + + +def atomic_json(path, value): + path = Path(path) + path.parent.mkdir(parents=True, exist_ok=True) + temporary = None + try: + with tempfile.NamedTemporaryFile(mode="w", dir=path.parent, delete=False) as stream: + temporary = Path(stream.name) + json.dump(value, stream, indent=2, allow_nan=False) + stream.write("\n") + stream.flush() + os.fsync(stream.fileno()) + os.replace(temporary, path) + finally: + if temporary is not None: + temporary.unlink(missing_ok=True) + + +def load_training_config(path, root): + cfg = json.loads(Path(path).read_text()) + keys = {"format", "contract", "proposal", "task_config", "seeds", "validation_seed", "num_envs", "horizon", + "updates_per_seed", "checkpoint_interval", "validation_interval", "max_total_transitions", + "max_total_wall_s", "max_artifact_bytes", "algorithm"} + formats = {"skillfactory.contact-cycle-training.v1", "skillfactory.contact-cycle-diagnostic-training.v1"} + if set(cfg) != keys or cfg["format"] not in formats or cfg["contract"] != CONTRACT: + raise ValueError("Unknown training config/contract") + diagnostic = cfg["format"] == "skillfactory.contact-cycle-diagnostic-training.v1" + for key in keys - {"format", "contract", "proposal", "task_config", "seeds", "algorithm"}: + if type(cfg[key]) is not int or cfg[key] <= 0: + raise ValueError(f"Invalid positive integer: {key}") + proposal = json.loads((Path(root)/cfg["proposal"]).read_text()) + plan = proposal["proposed_training"] + for key in ("seeds", "num_envs", "horizon", "updates_per_seed", "max_total_transitions", + "max_total_wall_s", "max_artifact_bytes"): + if cfg[key] != plan[key]: + raise ValueError(f"Budget/seed scope changed from proposal: {key}") + seed_count = 1 if diagnostic else 3 + reserved_seeds = proposal["independent_test"]["seed_commitment"] + if (not isinstance(cfg["seeds"], list) or len(cfg["seeds"]) != seed_count + or len(set(cfg["seeds"])) != seed_count or any(type(s) is not int or s < 0 for s in cfg["seeds"]) + or set(cfg["seeds"]) & set(reserved_seeds) + or cfg["validation_seed"] in cfg["seeds"] + reserved_seeds): + raise ValueError("Train/validation/independent seed overlap or count") + if cfg["updates_per_seed"] != (512 if diagnostic else 2048) or cfg["num_envs"] < 4: + raise ValueError("Unsupported curriculum/batch") + if diagnostic and (cfg["num_envs"] != 128 or cfg["horizon"] != 32 + or cfg["max_total_wall_s"] > 1800 or cfg["max_artifact_bytes"] > 1073741824): + raise ValueError("Diagnostic pilot exceeds fixed scope") + if any(cfg[k] != 256 for k in ("checkpoint_interval", "validation_interval")): + raise ValueError("Selection schedule is locked to every 256 updates") + expected = len(cfg["seeds"]) * cfg["updates_per_seed"] * cfg["horizon"] * cfg["num_envs"] + if cfg["max_total_transitions"] != expected: + raise ValueError("Inconsistent total transition budget") + a = cfg["algorithm"] + expected_keys = {"hidden", "initial_std", "learning_rate", "gamma", "gae_lambda", "clip_ratio", "epochs", + "minibatch_size", "value_coefficient", "max_gradient_norm", "target_kl", "failure_penalty"} + if set(a) != expected_keys: + raise ValueError("Invalid PPO keys") + for key, value in a.items(): + if type(value) not in (int, float) or not math.isfinite(value) or value <= 0: + raise ValueError(f"Invalid algorithm value: {key}") + if any(type(a[k]) is not int for k in ("hidden", "epochs", "minibatch_size")): + raise ValueError("Integer PPO parameters required") + if (a["hidden"] < 8 or not 0.02 <= a["initial_std"] <= 0.5 or not 0 < a["gamma"] <= 1 + or not 0 < a["gae_lambda"] <= 1 or not 0 < a["clip_ratio"] < 1 + or cfg["num_envs"]*cfg["horizon"] % a["minibatch_size"]): + raise ValueError("Invalid PPO discounts/std/minibatches") + load_config(Path(root)/cfg["task_config"]) + return cfg + + +def source_identity(root, cfg, config_path): + root = Path(root).resolve() + task = load_config(root/cfg["task_config"]) + support = json.loads((root/task["support_config"]).read_text()) + files = {root/cfg["task_config"], root/cfg["proposal"], Path(config_path).resolve(), root/task["support_config"]} + files.update((root/"source/SkillFactory").rglob("*.py")) + files.add(root/"source/SkillFactory/SkillFactory/legs/resources/ground.usda") + for name in ("train_contact_cycle.py", "runtime_common.py", "supervise_sim.py"): + files.add(root/"scripts"/name) + for folder in (support["support_asset"], support["source_bundle"]): + files.update(p for p in (root/folder).rglob("*") if p.is_file()) + if any(not p.resolve().is_relative_to(root) for p in files): + raise ValueError("Training identity inputs must be inside repository") + return {str(p.resolve().relative_to(root)): hashlib.sha256(p.read_bytes()).hexdigest() for p in sorted(files)} + + +def make_manifest(root, config_path): + cfg = load_training_config(config_path, root) + diagnostic = cfg["format"] == "skillfactory.contact-cycle-diagnostic-training.v1" + return {"format": "skillfactory.contact-cycle-preregistration.v1", "contract": CONTRACT, + "features": FEATURE_VERSION, "training": cfg, "sources": source_identity(root, cfg, config_path), + "sampler": "cycle-episode-sampler-v1", "critic": "symmetric_policy32_no_privileged_input", + "selection": "validation every 256; failures=16, successes=sum clipped [0,2] normalized rms/load/slip; " + "mean over every environment; tie earliest; " + + ("one diagnostic candidate only; no independent acceptance eligibility" if diagnostic + else "lock all three before independent test"), + "resume": "learner/RNG/counters restored; reset physics; no refund of interrupted reserved rollouts", + "wall_budget": "CLOCK_BOOTTIME absolute deadline including suspend/downtime/validation; " + "same boot only; no automatic legacy-ledger migration", + "contact_transition": "geometric liftoff with onset-phase provenance; premature separation terminates", + "independent_test": "not_materialized_not_executed", "accepted_skill": False} + + +def require_authorization(path, manifest): + if path is None: + raise PermissionError("New training requires explicit user authorization bound to this prepared manifest") + grant = json.loads(Path(path).read_text()) + required = {"format", "approved", "manifest_sha256", "operator", "authorization_reference"} + if (set(grant) != required or grant["format"] != "skillfactory.contact-cycle-authorization.v1" + or grant["approved"] is not True or grant["manifest_sha256"] != digest(manifest) + or not isinstance(grant["operator"], str) or not grant["operator"].strip() + or not isinstance(grant["authorization_reference"], str) or not grant["authorization_reference"].strip()): + raise PermissionError("Invalid/stale authorization; do not synthesize approval from a configuration file") + return grant + + +def save_trace_subset(path, trace, selected, stops, reserve): + """Detach backing storage, measure exact serialization bytes, then atomically write.""" + import torch + + path = Path(path) + if path.exists(): + raise FileExistsError("Never overwrite validation evidence") + evidence = {str(e): {k: v[:stops[e], e:e+1].clone().contiguous() for k, v in trace.items()} for e in selected} + stream = io.BytesIO() + torch.save(evidence, stream) + payload = stream.getvalue() + reserve(len(payload)) # must fail before any temporary/partial artifact is created + path.parent.mkdir(parents=True, exist_ok=True) + temporary = None + try: + with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as out: + temporary = Path(out.name) + out.write(payload) + out.flush() + os.fsync(out.fileno()) + os.replace(temporary, path) + finally: + if temporary is not None: + temporary.unlink(missing_ok=True) + return len(payload) + + +def artifact_bytes(root): + return sum(p.stat().st_size for p in Path(root).rglob("*") if p.is_file()) + + +class BudgetLedger: + """One exclusive writer for ALL training seeds and resumptions in this experiment.""" + + FORMAT = "skillfactory.contact-cycle-budget.v2" + + def __init__(self, root, manifest, now=time.time, *, boot_now=None, boot_id=None): + self.root, self.manifest, self.now = Path(root), manifest, now + self.boot_now = boot_now or (lambda: time.clock_gettime(time.CLOCK_BOOTTIME)) + self.boot_id = boot_id if boot_id is not None else Path("/proc/sys/kernel/random/boot_id").read_text().strip() + if not isinstance(self.boot_id, str) or not self.boot_id: + raise ValueError("A Linux boot identity is required; never fall back to UTC") + self.path = self.root/"budget.json" + self.cfg = manifest["training"] + self.lock = None + + def clocks(self): + wall, boot = self.now(), self.boot_now() + if not math.isfinite(wall) or not math.isfinite(boot) or boot < 0: + raise BudgetExceeded(f"Nonfinite/invalid clock: wall={wall!r}, boottime={boot!r}") + return wall, boot + + def __enter__(self): + self.root.mkdir(parents=True, exist_ok=True) + self.lock = (self.root/"budget.lock").open("a+") + try: + fcntl.flock(self.lock.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) + now, boot = self.clocks() + if self.path.exists(): + self.data = json.loads(self.path.read_text()) + if self.data.get("format") != self.FORMAT: + raise ValueError("Legacy/unknown ledger: explicit newly authorized recovery protocol required") + if self.data["manifest_sha256"] != digest(self.manifest): + raise ValueError("Experiment identity changed") + clock = self.data["clock"] + if clock["boot_id"] != self.boot_id: + raise BudgetExceeded("Boot identity changed; cannot prove downtime, refusing automatic resume") + start, last, deadline = (clock[k] for k in ("start_s", "last_seen_s", "deadline_s")) + if (not all(math.isfinite(v) for v in (start, last, deadline)) or not 0 <= start <= last < deadline + or deadline != start + self.cfg["max_total_wall_s"]): + raise ValueError("Invalid immutable boot-clock deadline") + else: + self.data = {"format": self.FORMAT, "manifest_sha256": digest(self.manifest), + "started_at": now, "last_seen_at": now, + "clock": {"boot_id": self.boot_id, "start_s": boot, "last_seen_s": boot, + "deadline_s": boot + self.cfg["max_total_wall_s"], + "wall_rollbacks": 0, "last_wall_rollback": None}, + "reserved_transitions": 0, "reservations": 0, "pending": None, + "updates": {str(s): 0 for s in self.cfg["seeds"]}} + atomic_json(self.path, self.data) + self.guard() + return self + except BaseException: + self.lock.close() + raise + + def __exit__(self, *_): + if self.lock is not None: + self.lock.close() + + def guard(self, reserve_bytes=0): + now, boot = self.clocks() + clock = self.data["clock"] + if boot < clock["last_seen_s"]: + raise BudgetExceeded(f"Boot clock rollback: observed={boot!r}, last={clock['last_seen_s']!r}, wall={now!r}") + if boot >= clock["deadline_s"]: + raise BudgetExceeded(f"Global wall-time budget exhausted: boottime={boot!r}, " + f"deadline={clock['deadline_s']!r}, wall={now!r}") + if artifact_bytes(self.root) + reserve_bytes > self.cfg["max_artifact_bytes"]: + raise BudgetExceeded("Global artifact budget exhausted") + if now < self.data["last_seen_at"]: + clock["wall_rollbacks"] += 1 + clock["last_wall_rollback"] = {"previous": self.data["last_seen_at"], "observed": now, "boottime": boot} + clock["last_seen_s"] = boot + self.data["last_seen_at"] = now # diagnostic only; never changes the boot-clock deadline + + def write_json(self, path, value): + payload = json.dumps(value, indent=2, allow_nan=False) + "\n" + self.guard(reserve_bytes=len(payload.encode()) + 65536) + atomic_json(path, value) + + def append_json(self, path, value): + payload = json.dumps(value, allow_nan=False) + "\n" + self.guard(reserve_bytes=len(payload.encode()) + 65536) + with Path(path).open("a") as stream: + stream.write(payload) + stream.flush() + os.fsync(stream.fileno()) + + def reconcile_checkpoint(self, seed, update): + """Reconcile the checkpoint-before-ledger-commit crash window. Never refund work.""" + self.guard() + previous = self.data["updates"][str(seed)] + pending = self.data["pending"] + if update not in (previous, previous + 1): + raise ValueError("Only latest checkpoint recovery is allowed") + if update == previous + 1 and pending != {"seed": seed, "update": update}: + raise ValueError("Unreserved checkpoint progress") + if pending is not None and pending["seed"] != seed: + raise ValueError("A different seed has interrupted work; recover it first") + self.data["updates"][str(seed)] = update + self.data["pending"] = None + atomic_json(self.path, self.data) + + def reserve_rollout(self, seed, update): + self.guard() + if self.data["pending"] is not None: + raise ValueError("Uncommitted reservation must be reconciled before continuing") + if (seed not in self.cfg["seeds"] or type(update) is not int or not 1 <= update <= self.cfg["updates_per_seed"] + or update != self.data["updates"][str(seed)] + 1): + raise ValueError("Unexpected seed/update sequence") + cost = self.cfg["num_envs"] * self.cfg["horizon"] + if self.data["reserved_transitions"] + cost > self.cfg["max_total_transitions"]: + raise BudgetExceeded("Global transition budget exhausted") + self.data["reserved_transitions"] += cost + self.data["reservations"] += 1 + self.data["pending"] = {"seed": seed, "update": update} + atomic_json(self.path, self.data) # durable BEFORE any environment transitions + + def commit_update(self, seed, update): + self.guard() + if (update != self.data["updates"][str(seed)] + 1 + or self.data["pending"] != {"seed": seed, "update": update}): + raise ValueError("Nonsequential/unreserved completed update") + self.data["updates"][str(seed)] = update + self.data["pending"] = None + atomic_json(self.path, self.data) diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/__init__.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/__init__.py new file mode 100644 index 0000000..4168320 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/__init__.py @@ -0,0 +1 @@ +"""Simulation-truth reference/residual protocol; incompatible with historical policy32.""" diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/config.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/config.py new file mode 100644 index 0000000..f95f231 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/config.py @@ -0,0 +1,49 @@ +"""Explicit simulation-only contract. Loading a config never authorizes optimization.""" +import json +import math +from pathlib import Path + +import torch + +FEATURE_VERSION = 'contact-cycle-sim-truth-residual-46-v1' +TASK_ID = 'SkillFactory-Go2FL-ContactCycleSim-v1' + + +def load_config(path): + c = json.loads(Path(path).read_text()) + keys = {'format', 'task_id', 'cycle_config', 'features', 'residual_limit_rad', 'reference', 'reward', 'scope'} + if (set(c) != keys + or c['format'] != 'skillfactory.contact-cycle-sim-residual.v1' + or c['task_id'] != TASK_ID or c['features'] != FEATURE_VERSION): + raise ValueError('Invalid simulation-residual identity') + ref, r = c['reference'], c['reward'] + if (set(ref) != {'ik_iterations', 'ik_damping', 'ik_step_limit_rad', 'jacobian_epsilon_rad'} + or set(r) != {'position_scale_m', 'velocity_scale_m_s', 'cost_cap', 'position_weight', + 'velocity_weight', 'residual_weight'} or len(r['position_scale_m']) != 3): + raise ValueError('Invalid reference/reward parameters') + vals = [c['residual_limit_rad'], *ref.values(), *r['position_scale_m'], + *[v for k, v in r.items() if k != 'position_scale_m']] + if any(type(x) not in (int, float) or not math.isfinite(x) or x <= 0 for x in vals): + raise ValueError('Require positive finite simulation parameters') + if (not 0 < c['residual_limit_rad'] <= .1 or type(ref['ik_iterations']) is not int + or not 1 <= ref['ik_iterations'] <= 10 or ref['ik_step_limit_rad'] > .1 + or ref['jacobian_epsilon_rad'] > .01): + raise ValueError('Reference/residual bounds exceeded') + return c + + +def combine(reference_action, residual, limit_rad, action_scale_rad): + if (residual.ndim != 2 or residual.shape != reference_action.shape or residual.shape[-1] != 3 + or residual.dtype != torch.float32 or reference_action.dtype != torch.float32 + or not torch.isfinite(residual).all() or not torch.isfinite(reference_action).all() + or (reference_action.abs() > 1).any()): + raise ValueError('Require finite float32 residuals matching [B,3] reference') + request = residual.clamp(-1, 1) + full = (reference_action + request * (limit_rad / action_scale_rad)).clamp(-1, 1) + return full, request, (full - reference_action) * action_scale_rad + + +def pose_cost(position_error, velocity_error, reward_cfg): + position = (position_error / position_error.new_tensor(reward_cfg['position_scale_m'])).square().sum(-1) + velocity = (velocity_error / reward_cfg['velocity_scale_m_s']).square().sum(-1) + return position.clamp(max=reward_cfg['cost_cap']), velocity.clamp(max=reward_cfg['cost_cap']) diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_adoption.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_adoption.py new file mode 100644 index 0000000..ec4f1a1 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_adoption.py @@ -0,0 +1,261 @@ +"""Exact external P1 evidence adoption, never a launch or a physical-pass claim. + +Historical source/config hashes are evidence of the old execution, not a claim +that today's edited evaluator equals those sources. Source artifacts are opened +read-only; the independently reviewed adoption digest is the trust boundary. +""" +from __future__ import annotations + +import hashlib +import importlib.util +import json +import time +from pathlib import Path + +from .evaluation_budget import ( + CAP_BYTES, EXTENDED_PROFILE, FORMAT as BUDGET_FORMAT, artifact_size, digest, + durable_write, encode, sync_directory, SECOND_EXTENDED_ROOT, +) + +FORMAT = 'skillfactory.external-p1-adoption.v1' +SOURCE_ROOT = Path('/home/cen/sf-runs/p1-03') +TARGET_ROOT = Path('/home/cen/sf-runs/p2-01') +SOURCE_CAMPAIGN = 'short-p1-03' +CANDIDATE_SHA256 = 'c088bb3aecaca751867118858626fd508f7a3983303802d7524353f80802cf99' +P1_PROTOCOL = 'configs/contact_cycle/continuous_eval_p1_v1.json' +ROOT = Path(__file__).resolve().parents[5] + + +def _require(condition, name): + if not condition: + raise ValueError('adoption_' + name) + + +def _json(path): + _require(path == path.resolve(strict=True) and path.is_file(), 'canonical_regular_file') + return json.loads(path.read_bytes()) + + +def _hash_value(value): + return hashlib.sha256(encode(value)).hexdigest() + + +def source_snapshot(*, check_trace=False): + """Parse both closed rows, retain failures, bind all original launch bytes.""" + root = SOURCE_ROOT + _require(root == root.resolve(strict=True), 'source_root') + ledger = _json(root / 'budget.json') + _require(ledger['format'] == BUDGET_FORMAT and ledger['campaign_id'] == SOURCE_CAMPAIGN + and len(ledger['launches']) == 2, 'original_two_closed_rows_required') + preflight = _json(root / 'row1-preflight.json') + _require(preflight['cumulative_prior_consumed_physical_calls'] == 58000 + and preflight['budget_before']['launches'] == ledger['launches'][:1] + and preflight['budget_before']['first_run_boottime'] == ledger['first_run_boottime'], + 'historical_no_refund_accounting') + files = {name: digest(root / name) for name in ( + 'budget.json', 'row1-preflight.json', 'FINAL_P1_ROW0.md', 'FINAL_P1_ROW1.md', 'PAIRED_P1_SUMMARY.md')} + summaries = [] + for index, (controller, tick) in enumerate((('learned_policy', 1829), ('analytic_reference', 1770))): + launch = root / f'launch-{index}' + artifact_size(launch) # refuse linked/nonregular evidence, including extra files + for path in sorted(launch.rglob('*')): + if path.is_file(): + files[str(path.relative_to(root))] = digest(path) + row = ledger['launches'][index] + grant = _json(launch / 'grant.json') + reservation = _json(launch / 'reservation.json') + result = _json(launch / 'result.json') + metrics = _json(launch / f'trial-{index:02d}/metrics.json') + supervisor = _json(launch / 'supervisor.json') + preclose = _json(launch / 'preclose-filesystem.json') + postexit = _json(launch / 'postexit-filesystem.json') + manifest = _json(launch / 'manifest.json') + _require(row['index'] == index and row['status'] == 'closed' + and row['cleanup_clean'] is True and row['continuation_eligible'] is True + and row['reserved_physical_calls'] == 14500 and row['rollout_cap'] == 13500 + and row['initialization_cap'] == 1000 and row['grant'] == grant + and grant['stage'] == 'P1' and grant['trial_indices'] == [index] + and grant['launch_index'] == index and grant['campaign_id'] == SOURCE_CAMPAIGN + and grant['artifact_root'] == str(root) and grant['boot_id'] == ledger['boot_id'], + 'closed_row_identity') + _require(reservation == {k: v for k, v in row.items() + if k not in ('cleanup_clean', 'ended_boottime')} + | {'status': 'reserved', 'continuation_eligible': False, 'observed_counts': None}, + 'reservation_changed') + trial = metrics['trial'] + _require(trial == manifest['protocol']['trials'][index] + and trial['index'] == index and trial['seed'] == 93501 + and trial['controller_kind'] == controller + and trial['trial_id'] == f'P1/nominal/93501/{controller}' + and manifest['trial_seeds'] == {'0': 93501, '1': 93501}, 'trial_stage_controller_seed') + _require(result['accepted_skill'] is False and result['optimizer_steps'] == 0 + and result['selected_rows'] == [index] and len(result['rows']) == 2 + and result['rows'][index] == metrics and result['rows'][1-index]['status'] == 'not_run' + and result['failures'] == [] and result['preclose_identity_verified'] is True + and result['continuation_eligible'] is True, 'partial_or_forged_result') + _require(metrics['accepted_skill'] is False and metrics['functional_passed'] is False + and metrics['protocol_complete'] is False and metrics['completed_clock_cycles'] == 0 + and metrics['passed_prefix_cycles'] == 0 and metrics['observed_physics_ticks'] == tick + and metrics['all_causes'] == ['P1_root_horizontal'] + and metrics['first_failure'] == {'global_tick': tick, 'time_s': tick * .002, + 'cycle_index': 0, 'local_tick': tick, 'kind': 'tick', + 'causes': ['P1_root_horizontal'], 'phase': 1}, 'original_failure_required') + counts = {'attempted_steps': tick, 'completed_steps': tick, 'initialization_steps': 0, + 'rollout_steps': tick} + _require(row['observed_counts'] == result['observed_counts'] == supervisor['observed_counts'] == counts + and supervisor['cleanup_clean'] is True and supervisor['continuation_eligible'] is True + and supervisor['ownership_unknown'] is False and supervisor['returncode'] == 0 + and supervisor['errors'] == [] and supervisor['stdout_discarded_bytes'] == 0 + and supervisor['stdout_eof_observed'] is True + and postexit['status'] == 'PASS' and postexit['files_rehashed'] == len(preclose['files']), + 'actual_cleanup_and_identity_required') + for name, key in (('manifest.json', 'manifest_sha256'), ('inventory.json', 'inventory_sha256'), + ('launch-identity.json', 'launch_identity_sha256')): + _require(digest(launch / name) == grant[key], 'grant_artifact_hash') + protocol = ROOT / P1_PROTOCOL + _require(manifest['protocol']['path'] == str(protocol) + and manifest['protocol']['sha256'] == grant['protocol_sha256'] == digest(protocol), + 'original_protocol_hash') + frozen = manifest['frozen_policy'] + _require(frozen['candidate_sha256'] == CANDIDATE_SHA256 + and preclose['files'][frozen['candidate_path']] == CANDIDATE_SHA256, + 'frozen_candidate_hash') + # Preserve every frozen/snapshot file, not current mutable evaluator sources. + frozen_files = {p: sha for p, sha in preclose['files'].items() + if Path(p).is_relative_to(preclose['frozen_root'])} + _require(bool(frozen_files), 'missing_frozen_files') + for path, sha in frozen_files.items(): + _require(digest(path) == sha, 'frozen_files_changed') + trace = launch / f'trial-{index:02d}/trace.v1.jsonl.gz' + _require(str(trace.relative_to(root)) in files, 'missing_trace') + if check_trace: + _check_trace(trace, index, tick) + summaries.append({'trial': trial, 'first_failure': metrics['first_failure'], + 'functional_passed': False, 'accepted_skill': False, + 'observed_counts': counts, 'cleanup_clean': True, + 'historical_closed_row': row, 'frozen_policy': frozen, + 'frozen_files': frozen_files, + 'effective_config_sha256': _hash_value(manifest['effective_config']), + 'historical_sources': manifest['sources'], + 'protocol': manifest['protocol']}) + _require(all(digest(root / name) == sha for name, sha in files.items()), 'source_changed_during_snapshot') + return {'source_root': str(root), 'source_campaign_id': SOURCE_CAMPAIGN, + 'source_boot_id': ledger['boot_id'], 'source_first_run_boottime': ledger['first_run_boottime'], + 'source_files_sha256': files, 'rows': summaries, + 'historical_reserved_calls_before_reference': 58000, + 'historical_reserved_calls': 72500, 'local_reserved_calls_at_adoption': 0, + 'prospective_local_reserved_calls': 91000, 'cumulative_reserved_calls_if_launched': 163500} + + +def _check_trace(path, index, tick): + spec = importlib.util.spec_from_file_location('_adoption_trace_reader', ROOT / 'scripts/audit_cycle_sim_evaluation.py') + reader = importlib.util.module_from_spec(spec) + spec.loader.exec_module(reader) + terminal = None + physics = 0 + initial = False + deadline = time.monotonic() + 120 + for number, line in enumerate(reader.trace_lines(path, compressed=True, max_bytes=CAP_BYTES, + max_record_bytes=1 << 20, max_decoded_bytes=CAP_BYTES, deadline=deadline)): + _require(number < 2 * 13500 + 16 and terminal is None, 'trace_partial_or_trailing') + record = json.loads(line) + if number == 0: + _require(record['kind'] == 'initial', 'trace_missing_initial') + if record['kind'] == 'initial': + _require(not initial and number == 0 and record['event']['clock']['global_tick'] == 0, + 'trace_duplicate_initial') + initial = True + event = record.get('event') + if event is not None: + _require(event['trial_index'] == index, 'trace_cross_trial') + if record['kind'] == 'tick': + physics += 1 + _require(event['clock']['global_tick'] == physics, 'trace_noncontiguous_ticks') + if record['kind'] == 'terminal': + terminal = record + _require(terminal is not None, 'trace_missing_terminal') + stop = terminal['runtime_stop'] + _require(stop['trial_index'] == index and stop['reason'] == 'failure' and stop['fatal'] is False + and stop['physics_ticks'] == tick and stop['clock']['global_tick'] == tick + and stop['causes'] == ['P1_root_horizontal'] and terminal['recording_faults'] == [] + and terminal['external_failure'] is None and physics == tick, 'trace_failure_mismatch') + + +# Exact failed attempt reviewed by the parent; no dynamic predecessor discovery. +PRIOR_P2_ROOT = Path('/home/cen/sf-runs/p2-01') +PRIOR_P2_FILES = { + 'budget.json': 'a0da00090f18bf28a0b2814aa3f130ef484176f220669621b458b0c3d7b409d7', + 'launch-0/result.json': '462a6588aaeefe8634ea494800314a1249ddc97c57b938d131e437ecc07923bd', + 'launch-0/supervisor.json': '5f0d62785679c7709f6d9d7250e5b2d09eae74915a3b020d804451623f4ab778', + 'launch-0/grant.json': 'c2d894a7b9233a6311e6d1234b2cf5e11a8a5586ad001fc40255025edeb8c781', + 'launch-0/reservation.json': 'b0f78f729b7de734ba453d00235550f82b2c6aa76bfc59419d80862dfdf7d27c', +} + + +def prior_p2_snapshot(source_boot_id): + """Retain the consumed failure, not a successful trial or measured-zero claim.""" + for name, sha in PRIOR_P2_FILES.items(): + _json(PRIOR_P2_ROOT / name) # canonical regular files only + _require(digest(PRIOR_P2_ROOT / name) == sha, 'prior_P2_changed') + ledger = _json(PRIOR_P2_ROOT / 'budget.json') + result = _json(PRIOR_P2_ROOT / 'launch-0/result.json') + supervisor = _json(PRIOR_P2_ROOT / 'launch-0/supervisor.json') + _require(ledger['boot_id'] == source_boot_id and len(ledger['launches']) == 1 + and ledger['launches'][0]['status'] == 'closed' + and ledger['local_reserved_calls'] == 91000 + and ledger['cumulative_reserved_calls'] == 163500 + and ledger['historical_reserved_calls'] == 72500, 'prior_P2_debit') + _require(supervisor['cleanup_clean'] is True and supervisor['ownership_unknown'] is False + and supervisor['returncode'] == 1 and result['continuation_eligible'] is False + and result['accepted_skill'] is False and result['observed_counts'] is None + and result['rows'] == [{'status': 'not_run', 'trial_index': i} for i in range(20)], + 'prior_P2_failure') + return {'root': str(PRIOR_P2_ROOT), 'files_sha256': dict(PRIOR_P2_FILES), + 'reserved_calls': 91000, 'observed_counts': None, 'rows': result['rows'], + 'failures': result['failures'], 'cleanup_clean': True, 'accepted_skill': False} + + +def snapshot_for_target(target, *, check_trace=False): + _require(target in (TARGET_ROOT, SECOND_EXTENDED_ROOT), 'fixed_target') + snapshot = source_snapshot(check_trace=check_trace) + if target == SECOND_EXTENDED_ROOT: + snapshot.update(prior_failed_P2=prior_p2_snapshot(snapshot['source_boot_id']), + historical_reserved_calls=163500, + cumulative_reserved_calls_if_launched=254500) + return snapshot + + +def create_adoption(*, parent_review, trusted_source_budget_sha256, target_root=None): + """Explicit parent review gate; writes only a new read-only, untrusted proposal.""" + _require(isinstance(parent_review, str) and bool(parent_review.strip()), 'explicit_parent_review_required') + _require(digest(SOURCE_ROOT / 'budget.json') == trusted_source_budget_sha256, + 'reviewed_source_budget_digest') + target = TARGET_ROOT if target_root is None else Path(target_root) + snapshot = snapshot_for_target(target, check_trace=True) + document = {'format': FORMAT, 'resource_profile': EXTENDED_PROFILE, 'target_root': str(target), + 'parent_review': parent_review, 'execution_authorized': False, 'accepted_skill': False, + 'P1_requirement': 'reviewed_interface_and_failure_evidence_not_physical_pass', **snapshot} + # No local ledger, launch tree, reservation or clock is created by adoption. + path = target / 'p1-adoption.json' + _require(target == target.resolve(strict=True), 'target_root') + durable_write(path, encode(document)) + path.chmod(0o444) + sync_directory(target) + return digest(path) + + +def verify_adoption(root, trusted_sha256): + path = Path(root) / 'p1-adoption.json' + _require(Path(root) in (TARGET_ROOT, SECOND_EXTENDED_ROOT) and path.stat().st_mode & 0o222 == 0, + 'read_only_fixed_target') + _require(isinstance(trusted_sha256, str) and len(trusted_sha256) == 64 + and digest(path) == trusted_sha256, 'independently_reviewed_digest') + document = _json(path) + expected = {'format': FORMAT, 'resource_profile': EXTENDED_PROFILE, 'target_root': str(root), + 'parent_review': document.get('parent_review'), 'execution_authorized': False, + 'accepted_skill': False, + 'P1_requirement': 'reviewed_interface_and_failure_evidence_not_physical_pass', + **snapshot_for_target(Path(root))} + _require(isinstance(document.get('parent_review'), str) and bool(document['parent_review'].strip()) + and document == expected, 'stale_mutated_or_forged_evidence') + return document diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_audit.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_audit.py new file mode 100644 index 0000000..243a1bb --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_audit.py @@ -0,0 +1,940 @@ +"""Streaming CPU evidence audit. Clocks are not passed cycles; no simulator imports.""" +from __future__ import annotations + +import copy +import hashlib +import json +import math +from dataclasses import asdict +from typing import TypedDict + +from .evaluation_protocol import CYCLE_TICKS, PHASE_END_TICKS, PHYSICS_DT_S, Clock + +FORMAT = 'skillfactory.contact-cycle-evaluation-evidence.v1' +TRACE_ENCODING = 'jsonl-xz-finalized-chunks.v2' +TRACE_FILENAME = 'trace.v2.jsonl.xz' +MAX_RECORD_BYTES = 1024 * 1024 +MAX_CHUNK_RECORDS = 64 +MAX_CHUNK_RAW_BYTES = 8 * 1024 * 1024 +XZ_PRESET = 0 # liblzma preset 0: 256 KiB dictionary; independent streams. +XZ_DECODER_MEMORY_BYTES = 8 * 1024 * 1024 +DT = PHYSICS_DT_S +NOT_EVALUATED = 'not_evaluated' +SCHEDULE_ROUNDOFF_S = 2e-6 + + +class AppliedEvent(TypedDict): + event_index: int + body_index: int + envelope: float + force_world: list[float] + couple_world: list[float] + point_world: list[float] + com_world: list[float] + frame_quat_xyzw: list[float] + point_frame_quat_xyzw: list[float] + point_frame_origin_world: list[float] + + +class AppliedEvidence(TypedDict): + """Read-only pre-step capture, from the SAME unique writer invocation. + + global_tick identifies the ensuing interval, i.e. pre-step substeps + 1. + total_com_wrench is the actual setter buffer, not an independently recomposed + command. events describe external contributions only; guide/auxiliary are + checked separately against the captured state buffers. + """ + format: str # skillfactory.contact-cycle-applied-wrench.v1 + trial_index: int + global_tick: int + events: list[AppliedEvent] + total_com_wrench: list[list[float]] + + +def cross(a, b): + return [a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]] + + +def rotate(q, v): + t = [2*x for x in cross(q[:3], v)] + return [a + q[3]*b + c for a, b, c in zip(v, t, cross(q[:3], t))] + + +def close(a, b, tolerance=1e-5): + return len(a) == len(b) and all(abs(x-y) <= tolerance for x, y in zip(a, b)) + + +def plain(value): + """Independent JSON-compatible copy, also accepting protocol immutable mappings.""" + # Exact immutable JSON scalars need no deepcopy; subclasses retain the old path. + if value is None or type(value) in (str, bool, int, float): + return value + if hasattr(value, 'items'): + return {k: plain(v) for k, v in value.items()} + if isinstance(value, (tuple, list)): + return [plain(v) for v in value] + return copy.deepcopy(value) + + +def fingerprint(value): + return hashlib.sha256(json.dumps(value, sort_keys=True, allow_nan=False, + separators=(',', ':')).encode()).hexdigest() + + +def finite(value): + if isinstance(value, dict): + return all(finite(v) for v in value.values()) + if isinstance(value, (list, tuple)): + return all(finite(v) for v in value) + return not isinstance(value, float) or math.isfinite(value) + + +def norm(v): + return math.sqrt(sum(x * x for x in v)) + + +def sub(a, b): + return [x - y for x, y in zip(a, b, strict=True)] + + +def mean(values): + return sum(values) / len(values) + + +def longest(values): + best = current = 0 + for value in values: + current = current + 1 if value else 0 + best = max(best, current) + return best + + +def vector(value, size): + return (isinstance(value, list) and len(value) == size + and all(type(x) in (int, float) and math.isfinite(x) for x in value)) + + +# Every field is mandatory. Nonfinite records are retained in the raw stream, never repaired. +SCALARS = ('weight', 'gap', 'tilt', 'slip_speed', 'limit_ratio', 'joint_excess', 'time', + 'contact_timer', 'separation_timer', 'acquisition_timer', 'anchor_height') +BOOLS = ('contact', 'separated', 'premature', 'acquired', 'acquisition_failed', 'armed', + 'load_off', 'liftoff', 'touchdown') +VECTORS = {'foot': 3, 'normal': 3, 'friction': 3, 'root': 13, 'q': 3, 'qdot': 3, 'torque': 3, + 'processed_target': 3, 'executed': 3, 'residual_request': 3, 'effective_residual_rad': 3, + 'ik_target': 3, 'q_ik': 3, 'reference_action': 3, 'sensed': 18, 'policy': 46, + 'manager_action': 3, 'manager_previous_action': 3, 'env_origin': 3} +STATE_FIELDS = (*SCALARS, *BOOLS, *VECTORS, 'onset_phase', 'edge_phase', 'refreshes', 'substeps', + 'external', 'guide', 'auxiliary', 'events', 'sensor_delay', 'action_delay', + 'rng', 'actuator_state', 'reward_components', 'counters', 'scenario', 'anchors', + 'event_program', 'body_names', 'applied') + + +def validate_state(s): + if not isinstance(s, dict): + raise ValueError('state_not_object') + missing = set(STATE_FIELDS) - s.keys() + if missing: + raise ValueError('missing_state:' + ','.join(sorted(missing))) + if not finite(s): + raise ValueError('nonfinite_state') + for k in SCALARS: + if type(s[k]) not in (int, float): + raise ValueError('scalar:' + k) + for k in BOOLS: + if type(s[k]) is not bool: + raise ValueError('boolean:' + k) + for k, n in VECTORS.items(): + if not vector(s[k], n): + raise ValueError('vector:' + k) + for k in ('onset_phase', 'edge_phase', 'refreshes', 'substeps', 'scenario'): + if type(s[k]) is not int: + raise ValueError('integer:' + k) + if (s['weight'] <= 0 or s['slip_speed'] < 0 or s['limit_ratio'] < 0 or s['joint_excess'] < 0 + or abs(norm(s['root'][3:7]) - 1) > 1e-5): + raise ValueError('invalid_physical_scalar_or_quaternion') + if not s['body_names'] or len(set(s['body_names'])) != len(s['body_names']): + raise ValueError('body_names') + for k in ('external', 'guide', 'auxiliary'): + if len(s[k]) != len(s['body_names']) or not all(vector(v, 6) for v in s[k]): + raise ValueError('wrench:' + k) + for k, width in (('sensor_delay', 18), ('action_delay', 3)): + if not s[k] or not all(vector(v, width) for v in s[k]): + raise ValueError('delay:' + k) + if (set(s['anchors']) != {'anchor_a_world', 'root_origin_world', 'initial_root_world'} + or not all(vector(v, 3) for v in s['anchors'].values())): + raise ValueError('anchors') + for k in ('reset_calls', 'pose_write_calls', 'composer_buffer_replacements', 'initialization_steps', + 'rollout_steps', 'attempted_steps', 'completed_steps'): + if type(s['counters'][k]) is not int or s['counters'][k] < 0: + raise ValueError('counter:' + k) + if not isinstance(s['rng'], dict) or not s['rng'] or not isinstance(s['actuator_state'], dict): + raise ValueError('rng_or_actuator_state') + if set(s['reward_components']) != {'task', 'effort', 'action_rate', 'root_position', 'root_velocity', 'residual'}: + raise ValueError('reward_components') + if len(s['events']) != len(s['event_program']): + raise ValueError('event_count') + for e in s['events']: + if (set(e) != {'fired', 'completed', 'start', 'envelope', 'integral'} + or type(e['fired']) is not bool or type(e['completed']) is not bool + or any(type(e[k]) not in (int, float) for k in ('start', 'envelope', 'integral'))): + raise ValueError('event_shape') + + +def carry_state(s): + """Everything that a metadata transition + one IK refresh must leave bit-identical.""" + allowed = {'ik_target', 'q_ik', 'reference_action', 'refreshes', 'policy', 'reward_components'} + return {k: v for k, v in s.items() if k not in allowed} + + +class TrialAudit: + """Consumes initial/control/tick/seam/next_reference records in runtime order. + + Gates become determinate at the *last included sample*, not a later control + boundary. After the first failure only terminal evidence is admissible. + """ + def __init__(self, protocol, trial_index): + self.protocol, self.trial = protocol, protocol.trial(trial_index) + thresholds = protocol.document['thresholds'] + self.f, self.m, self.r = (thresholds[k] for k in ('functional', 'P1_margins', 'original_runtime')) + self.initial = self.last = self.reference = self.control = None + self.tick = 0 + self.first_failure = None + self.cycles = [] + self.frames = [] + self.total_slip = 0. + self.admitted = [] + self.seams = [] + self.needs_reference = False + self.gates = {} + self.margins = {} + self.metrics = {} + self._cycle_start() + self.event_integrals = None + self.event_previous = None + self.delivery = [] + self.recovery = {'status': NOT_EVALUATED, 'time_s': None, 'definition': + 'first 0.1s after final pulse; root xy<=.03,z<=.06,tilt<=.3; original phase bounds'} + self.recovery_run = 0 + self.terminal = None + self.step_results = [] + self.all_causes = [] + + def _cycle_start(self): + self.gates = {k: NOT_EVALUATED for k in ( + 'support_ground_support', 'unloaded', 'real_flight', 'three_dimensional_command', 'xy_swing_motion', + 'reload_ground_support', 'landing_position', 'contact_acquired', 'complete_clock', + 'runtime_safety', 'condition_identity', 'event_delivery')} + self.margins = {k: NOT_EVALUATED for k in ( + 'root_horizontal', 'root_z', 'seam_root_drift', 'reload_mean_position', 'reload_position', + 'cycle_slip', 'run_slip', 'repeat_B_drift', 'unload_ratio', 'lift_dwell', 'reload_dwell')} + self.metrics = {} + + def fail(self, causes, *, kind='tick', tick=None): + causes = list(dict.fromkeys(causes)) + if not causes: + return () + self.all_causes = list(dict.fromkeys(self.all_causes + causes)) + tick = self.tick if tick is None else tick + if self.first_failure is None: + clock = self.protocol.evidence_clock(self.trial.index, tick) if tick else Clock(0, 0, 0) + self.first_failure = {'global_tick': tick, 'time_s': tick * DT, 'cycle_index': clock.cycle_index, + 'local_tick': clock.local_tick, 'kind': kind, 'causes': causes} + if self.last is not None: + self.first_failure['phase'] = self.last.get('event', {}).get('command', {}).get('phase') + elif self.first_failure['global_tick'] == tick: + self.first_failure['causes'] = list(dict.fromkeys(self.first_failure['causes'] + causes)) + return tuple(causes) + + def consume(self, record): + """Return ALL current causes; malformed evidence is a fatal recording cause.""" + if not isinstance(record, dict): + return self.fail(('recording_invalid:record_not_object',), kind='unknown') + try: + return self._consume(record) + except (KeyError, TypeError, ValueError, IndexError, OverflowError) as exc: + event = record.get('event') + event = event if isinstance(event, dict) else {} + clock = event.get('clock') + clock = clock if isinstance(clock, dict) else {} + g = clock.get('global_tick', self.tick) + if type(g) is not int or not 0 <= g <= self.trial.total_ticks: + g = self.tick + if record.get('kind') == 'tick' and g == self.tick + 1: + self.tick = g + self.last = plain(record) + stop = record.get('runtime_stop', record.get('stop')) + runtime_causes = stop.get('causes', []) if isinstance(stop, dict) else event.get('causes', []) + if not isinstance(runtime_causes, list) or any(not isinstance(c, str) for c in runtime_causes): + runtime_causes = [] + return self.fail((*runtime_causes, f'recording_invalid:{exc}'), + kind=record.get('kind', 'unknown'), tick=g) + + def _stop_causes(self, stop): + if not isinstance(stop, dict): + raise ValueError('terminal_stop_shape') + integer_fields = ('trial_index', 'physics_ticks', 'completed_controls', 'partial_control_ticks', + 'completed_clock_cycles', 'all_sim_steps_attempted', 'all_sim_steps_completed', + 'initialization_steps', 'rollout_steps', 'reset_calls', 'pose_write_calls', + 'composer_buffer_replacements') + if (set(stop) != {*integer_fields, 'reason', 'clock', 'causes', 'fatal'} + or any(type(stop[k]) is not int or stop[k] < 0 for k in integer_fields) + or type(stop['fatal']) is not bool or not isinstance(stop['causes'], list) + or any(not isinstance(c, str) or not c for c in stop['causes'])): + raise ValueError('terminal_stop_shape') + g = stop['physics_ticks'] + clock = self.protocol.evidence_clock(self.trial.index, g) if g else Clock(0, 0, 0) + if (stop['trial_index'] != self.trial.index or g != self.tick or stop['clock'] != asdict(clock) + or stop['completed_controls'] != g // 5 or stop['partial_control_ticks'] != g % 5 + or stop['completed_clock_cycles'] != g // CYCLE_TICKS): + raise ValueError('terminal_clock') + if (stop['reason'] not in ('failure', 'protocol_complete') + or (stop['reason'] == 'failure') != bool(stop['causes']) + or stop['reason'] == 'protocol_complete' and g != self.trial.total_ticks): + raise ValueError('terminal_reason') + causes = list(stop['causes']) + fatal = any(c.startswith(('nonfinite', 'hardware', 'identity', 'illegal_', 'storage', 'recording', + 'budget', 'process', 'infrastructure', 'guide_', 'auxiliary_')) for c in causes) + if stop['fatal'] != fatal: + causes.append('recording_terminal_fatal_classification') + if self.last is not None: + counters = self.last['state']['counters'] + names = {'all_sim_steps_attempted': 'attempted_steps', 'all_sim_steps_completed': 'completed_steps', + **{k: k for k in integer_fields[7:]}} + if any(stop[k] != counters[v] for k, v in names.items()): + causes.append('recording_terminal_counter_mismatch') + return causes + + def _step_result(self, record): + n = record['ticks_advanced'] + prior = sum(s['ticks_advanced'] for s in self.step_results) + if (set(record) != {'kind', 'ticks_advanced', 'observed_tick', 'reward', 'has_next_observation', 'stop'} + or type(n) is not int or not 0 <= n <= 5 or type(record['observed_tick']) is not int + or record['observed_tick'] != self.tick or prior + n != self.tick + or type(record['has_next_observation']) is not bool + or n < 5 and record['reward'] is not None + or record['reward'] is not None and not vector(record['reward'], 1)): + raise ValueError('partial_step_result') + if self.step_results and self.step_results[-1]['stop'] is not None: + raise ValueError('step_after_stop') + if record['stop'] is None: + if n != 5 or not record['has_next_observation'] or record['reward'] is None or self.first_failure: + raise ValueError('continuing_step_result') + causes = () + else: + if record['has_next_observation']: + raise ValueError('stopped_next_observation') + causes = self._stop_causes(record['stop']) + self.step_results.append(plain(record)) + return self.fail(causes, kind='step_result') + + def _terminal(self, record): + if set(record) != {'kind', 'runtime_stop', 'external_failure', 'recording_faults'}: + raise ValueError('terminal_fields') + self.terminal = plain(record) # Retain even an invalid terminal for offline diagnosis. + stop, external, faults = record['runtime_stop'], record['external_failure'], record['recording_faults'] + if not isinstance(faults, list) or any(not isinstance(c, str) or not c for c in faults): + raise ValueError('terminal_faults') + causes = list(faults) + if stop is None and external is None: + raise ValueError('missing_terminal_reason') + if external is not None: + if (not isinstance(external, dict) or set(external) != {'causes', 'fatal', 'context'} + or external['fatal'] is not True or not isinstance(external['context'], dict) + or not isinstance(external['causes'], list) or not external['causes'] + or any(not isinstance(c, str) or not c for c in external['causes'])): + raise ValueError('runner_terminal') + causes.extend(external['causes']) + if stop is not None: + causes.extend(self._stop_causes(stop)) + if self.step_results and self.step_results[-1]['stop'] != stop: + causes.append('recording_terminal_step_stop_mismatch') + if sum(s['ticks_advanced'] for s in self.step_results) != self.tick: + causes.append('recording_missing_step_results') + return self.fail(causes, kind='terminal') + + def _control(self, record): + if (set(record) != {'kind', 'origin_tick', 'observation', 'action', 'mean', 'std'} + or type(record['origin_tick']) is not int + or self.initial is None or self.needs_reference or self.control is not None + or record['origin_tick'] != self.tick): + raise ValueError('control_order') + if not vector(record['observation'], 46) or not vector(record['action'], 3): + raise ValueError('actor_shape') + learned = self.trial.controller_kind == 'learned_policy' + if learned: + if (not vector(record['mean'], 3) or not vector(record['std'], 3) + or any(x <= 0 for x in record['std'])): + raise ValueError('actor_metadata') + if any(abs(a - math.tanh(m)) > 1e-6 for a, m in zip(record['action'], record['mean'])): + raise ValueError('deterministic_actor_action') + elif record['mean'] is not None or record['std'] is not None or any(record['action']): + raise ValueError('reference_actor_metadata') + if record['observation'] != self.reference['state']['policy']: + raise ValueError('actor_observation_provenance') + self.control = plain(record) + return () + + def _reference_event(self, record, kind, s, e, g): + if kind == 'seam': + if (g != self.tick or not g or g % CYCLE_TICKS or g == self.trial.total_ticks + or g in self.admitted or len(self.cycles) != g // CYCLE_TICKS + or not self.cycles[-1]['passed'] or self.last != {**record, 'kind': 'tick'}): + raise ValueError('seam_not_completed_passing_or_state_mutation') + self.admitted.append(g) + self.seams.append({'tick': g, 'pre': fingerprint(s), 'carry_pre': fingerprint(carry_state(s)), + 'post': None, 'carry_post': None}) + return () + if kind == 'next_reference': + if not self.needs_reference or g != self.tick or self.last is None: + raise ValueError('duplicate_or_skipped_reference') + before = self.last['state'] + if carry_state(before) != carry_state(s) or s['refreshes'] != before['refreshes'] + 1: + raise ValueError('reference_refresh_mutated_carry_state') + if g % CYCLE_TICKS == 0: + self.seams[-1].update(post=fingerprint(s), carry_post=fingerprint(carry_state(s))) + self.reference = plain(record) + self.needs_reference = False + return self.fail(e['causes'], kind=kind) + if kind == 'initial': + if e['reference_used_clock'] is not None: + raise ValueError('initial_held_reference') + if any(event != {'fired': False, 'completed': False, 'start': 0., 'envelope': 0., 'integral': 0.} + for event in s['events']): + raise ValueError('initial_event_schedule') + if any(abs(x) > 1e-6 for v in s['external'] for x in v): + raise ValueError('initial_external_wrench') + self.last = plain(record) + self.event_previous = plain(record) + return self.fail(e['causes'], kind=kind) + return None + + def _event_shape(self, record, kind): + s, e = record['state'], record['event'] + if set(record) != {'kind', 'state', 'event'} or set(e) != { + 'kind', 'trial_index', 'clock', 'command', 'held_control_index', 'action_origin_tick', + 'reference_used_clock', 'reference_used', 'reference_next_clock', 'reference_next', 'causes' + }: + raise ValueError('event_fields') + if (not isinstance(e['causes'], list) or any(not isinstance(c, str) or not c for c in e['causes']) + or not vector(e['reference_used'], 3) or not vector(e['reference_next'], 3) + or type(e['trial_index']) is not int): + raise ValueError('event_types') + for name in ('clock', 'reference_next_clock', 'reference_used_clock'): + value = e[name] + if value is None and name == 'reference_used_clock': + continue + if (not isinstance(value, dict) or set(value) != {'global_tick', 'cycle_index', 'local_tick'} + or any(type(x) is not int for x in value.values())): + raise ValueError('clock_types') + validate_state(s) + if not finite(e) or e['trial_index'] != self.trial.index or e['kind'] != ('tick' if kind == 'seam' else kind): + raise ValueError('event_identity') + if e['clock'] != asdict(Clock(**e['clock'])): + raise ValueError('clock_shape') + return s, e + + def _held_action(self, s, e, g): + if self.needs_reference or g != self.tick + 1 or self.control is None: + raise ValueError('duplicate_skipped_tick_or_missing_control') + if (e['held_control_index'] != (g - 1) // 5 or e['action_origin_tick'] != 5 * ((g - 1) // 5) + or e['action_origin_tick'] != self.control['origin_tick'] + or e['reference_used_clock'] != self.reference['event']['clock'] + or e['reference_used'] != self.reference['event']['reference_next'] + or e['reference_next_clock'] != self.reference['event']['clock'] + or e['reference_next'] != e['reference_used'] + or s['refreshes'] != self.reference['state']['refreshes']): + raise ValueError('held_action_provenance') + if any(abs(a - b) > 1e-6 for a, b in zip(s['residual_request'], self.control['action'])): + raise ValueError('requested_residual_provenance') + for ref, req, actual, delta in zip(e['reference_used'], s['residual_request'], s['executed'], + s['effective_residual_rad']): + full = max(-1., min(1., ref + max(-1., min(1., req)) * .1)) + if abs(actual - full) > 1e-6 or abs(delta - (full - ref) * .5) > 1e-6: + raise ValueError('residual_composition') + if s['q_ik'] != self.reference['state']['q_ik']: + raise ValueError('IK_changed_between_refreshes') + if (s['counters']['rollout_steps'] - self.initial['state']['counters']['rollout_steps'] != g + or s['counters']['completed_steps'] - self.initial['state']['counters']['completed_steps'] != g): + raise ValueError('unobserved_physics_steps') + + def _consume(self, record): + kind = record['kind'] + if self.terminal is not None: + raise ValueError('recording_after_terminal') + if kind == 'step_result': + return self._step_result(record) + if kind == 'terminal': + return self._terminal(record) + if kind == 'capture_fault': + causes = self.fail(record['causes'], kind=kind, tick=record['event']['clock']['global_tick']) + if self.first_failure['global_tick'] == record['event']['clock']['global_tick']: + self.first_failure['phase'] = record['event'].get('command', {}).get('phase') + return causes + if self.first_failure: + return self.fail(('recording_after_failure',), kind=kind) + if kind == 'control': + return self._control(record) + if kind not in ('initial', 'tick', 'next_reference', 'seam'): + raise ValueError('record_kind') + s, e = self._event_shape(record, kind) + g = e['clock']['global_tick'] + if type(g) is not int or s['substeps'] != g or abs(s['time'] - g * DT) > 1e-5: + raise ValueError('physical_clock') + if kind == 'initial': + if self.initial is not None or g != 0: + raise ValueError('duplicate_initial') + clock = self.protocol.next_command_clock(self.trial.index, 0) + self.initial = plain(record) + self.reference = plain(record) + else: + if self.initial is None: + raise ValueError('missing_initial') + clock = (self.protocol.evidence_clock(self.trial.index, g) if kind in ('tick', 'seam') else + self.protocol.next_command_clock(self.trial.index, g, seam_admitted=g in self.admitted)) + if (s['anchors'] != self.initial['state']['anchors'] + or s['env_origin'] != self.initial['state']['env_origin']): + raise ValueError('recentered_anchors') + for k in ('reset_calls', 'pose_write_calls', 'initialization_steps'): + if s['counters'][k] != self.initial['state']['counters'][k]: + raise ValueError('illegal_state_reset:' + k) + if clock is None or e['clock'] != asdict(clock): + raise ValueError('command_clock') + command = self.protocol.command(self.trial.index, clock, + anchor_a_world=s['anchors']['anchor_a_world'], + root_origin_world=s['anchors']['root_origin_world'], weight_N=s['weight']) + if e['command'] != plain(asdict(command)) or s['weight'] != self.initial['state']['weight']: + raise ValueError('command_or_weight') + local = rotate([-x for x in s['root'][3:6]] + [s['root'][6]], + sub(e['command']['target_world_m'], s['root'][:3])) + if not close(local, s['ik_target']): + raise ValueError('IK_world_to_actual_body_coordinates') + if s['scenario'] != self.trial.scenario_id: + raise ValueError('condition_scenario') + program = s['event_program'] + selected = [i for i, p in enumerate(program) if p['scenario'] == s['scenario']] + if selected != list(self.trial.event_indices): + raise ValueError('condition_event_indices') + if self.protocol.document['events'] and program != plain(self.protocol.document['events']): + raise ValueError('condition_event_program') + if program != self.initial['state']['event_program']: + raise ValueError('changed_event_program') + if kind in ('initial', 'next_reference'): + if e['held_control_index'] is not None or e['action_origin_tick'] is not None: + raise ValueError('reference_labeled_as_action') + if e['reference_next_clock'] != e['clock'] or e['reference_next'] != s['reference_action']: + raise ValueError('reference_next_provenance') + handled = self._reference_event(record, kind, s, e, g) + if handled is not None: + return handled + self._held_action(s, e, g) + self.tick = g + self.last = plain(record) + self.frames.append({'state': {k: s[k] for k in ('foot', 'normal', 'gap', 'root', 'weight', + 'onset_phase', 'contact', 'separated')}, 'command': e['command']}) + causes = list(e['causes']) + self._instant(s, command, clock) + causes += self._windows(clock) + causes += self._events(s, clock) + if g % 5 == 0: + self.control = None + self.needs_reference = g != self.trial.total_ticks + result = self.fail(causes) + if clock.local_tick == CYCLE_TICKS: + self.gates['complete_clock'] = len(self.frames) == CYCLE_TICKS + self.gates['runtime_safety'] = not bool(result) + self.gates['condition_identity'] = True + self._finish_cycle(clock, result) + if self.first_failure: + result = tuple(self.first_failure['causes']) + return result + + def _gate(self, name, passed, causes, *, margin=False): + (self.margins if margin else self.gates)[name] = bool(passed) + if not passed and (not margin or self.protocol.document['stage'] == 'P1'): + causes.append(('P1_' if margin else '') + name) + + def _instant(self, s, c, clock): + f, m, r = self.f, self.m, self.r + p, t, fn, w, gap = c.phase, clock.local_tick, s['normal'][2], s['weight'], s['gap'] + error = sub(s['root'][:3], c.root_target_world_m) + causes = [] + checks = { + 'contact_acquisition_timeout': s['acquisition_failed'] or (self.tick >= 100 and not s['acquired']), + 'premature_separation': s['premature'], + 'lift_deadline': p == 2 and c.progress > r['lift_progress_strictly_greater'] and ( + gap < r['lift_gap_m_min'] or fn > r['lift_normal_N_max']), + 'swing_clearance': p == 3 and gap < r['swing_gap_m_min'], + 'reload_deadline': p == 5 and c.progress > r['reload_progress_strictly_greater'] + and fn < r['reload_load_fraction_min'] * w, + 'low_height': s['root'][2] < s['anchor_height'] - r['root_height_below_nominal_m_max'], + 'root_height_error': abs(error[2]) > f['root_height_error_m_max'], + 'tilt': s['tilt'] > f['root_tilt_rad_max'], + 'negative_normal': fn < -f['normal_force_negative_tolerance_N'], + 'ground_penetration': gap < -f['ground_penetration_m_max'], + 'hardware_effort_velocity': s['limit_ratio'] > f['normalized_effort_velocity_max'], + 'hardware_joint_limit': s['joint_excess'] > f['joint_limit_excess_rad_max'], + 'guide_vertical': any(v[2] != r['guide_vertical_N_exact'] for v in s['guide']), + 'auxiliary_force': sum(v[2] for v in s['auxiliary']) > f['auxiliary_vertical_N_max'], + 'auxiliary_geometry': any(abs(v[0]) > 1e-6 or abs(v[1]) > 1e-6 or v[2] < -1e-6 + or abs(v[5]) > 1e-6 or norm(v[3:]) > f['auxiliary_lever_arm_m_max'] * max(v[2], 0) + 1e-6 + for v in s['auxiliary']), + 'auxiliary_support_phase': p == 0 and any(abs(x) > 1e-6 for v in s['auxiliary'] for x in v), + } + causes.extend(k for k, hit in checks.items() if hit) + if self.tick >= 100: + self.gates['contact_acquired'] = s['acquired'] + slip = s['slip_speed'] * DT if p in (0, 5) else 0. + self.total_slip += slip + self.metrics['stance_slip_m'] = self.metrics.get('stance_slip_m', 0.) + slip + for name, value in (('root_horizontal_error_m', norm(error[:2])), ('root_absolute_z_error_m', abs(error[2])), + ('tilt_rad', s['tilt']), ('peak_impact_over_weight', fn / w), + ('root_displacement_from_initial_m', norm(sub(s['root'][:3], + s['anchors']['initial_root_world'])))): + self.metrics['max_' + name] = max(value, self.metrics.get('max_' + name, 0.)) + margins = {'root_horizontal': norm(error[:2]) <= m['root_horizontal_error_m_max'], + 'root_z': abs(error[2]) <= m['root_absolute_z_error_m_max'], + 'cycle_slip': self.metrics['stance_slip_m'] <= m['stance_slip_per_cycle_m_max'], + 'run_slip': self.total_slip <= m['stance_slip_per_run_m_max']} + if t >= 4400: + margins['reload_position'] = (norm(sub(s['foot'], c.target_world_m)) + <= m['reload_instantaneous_3d_error_m_max']) + if t == CYCLE_TICKS: + drift = norm(sub(s['root'][:2], s['anchors']['initial_root_world'][:2])) + self.metrics['seam_root_horizontal_drift_m'] = drift + margins['seam_root_drift'] = drift <= m['seam_to_initial_root_horizontal_drift_m_max'] + for name, passed in margins.items(): + self._gate(name, passed and self.margins[name] is not False, causes, margin=True) + if not s['separated'] and s['separation_timer'] > 0 and gap >= .001: + self.metrics.setdefault('liftoff_onset_time_s', clock.local_time_s - s['separation_timer'] + DT) + if s['liftoff']: + self.metrics.setdefault('liftoff_onset_time_s', None) + self.metrics.setdefault('liftoff_edge_local_tick', t) + self.metrics.setdefault('liftoff_onset_phase', s['edge_phase']) + return causes + + def _windows(self, clock): + t, f, m = clock.local_tick, self.f, self.m + causes = [] + states = None + if t in (1499, 1999, m['lift_deadline_local_tick'], 2999, CYCLE_TICKS): + states = [x['state'] for x in self.frames] + for name, end, start in (('support', 1499, 1250), ('unload', 1999, 1950), ('reload', 4500, 4400)): + if t != end: + continue + window = states[start - 1:end] + contact = mean([s['normal'][2] > f['contact_force_on_N'] for s in window]) + load = mean([s['normal'][2] / s['weight'] for s in window]) + self.metrics[name + '_contact_fraction'] = contact + self.metrics[name + '_load_fraction'] = load + if name != 'unload': + self._gate(name + '_ground_support', contact >= f['support_reload_contact_fraction_min'] + and load >= f['support_reload_load_fraction_min'], causes) + else: + ratio = load / max(self.metrics['support_load_fraction'], 1e-6) + self.metrics['unload_remaining_support_fraction'] = ratio + self._gate('unloaded', ratio <= f['unload_remaining_support_fraction_max'], causes) + self._gate('unload_ratio', ratio <= m['unload_remaining_support_fraction_max'], causes, margin=True) + self.metrics['unload_min_separation_margin_m'] = min(.001 - s['gap'] for s in states[1499:1999]) + if t == m['lift_deadline_local_tick']: + lift = states[1999:t] + valid = [s['gap'] >= m['lift_gap_m_min'] and s['normal'][2] <= m['lift_normal_N_max'] + and s['onset_phase'] == m['lift_onset_phase'] for s in lift] + dwell = longest(valid) + self.metrics['lift_longest_dwell_ticks_by_deadline'] = dwell + self._gate('lift_dwell', dwell >= m['lift_dwell_ticks'], causes, margin=True) + first = next((i for i in range(m['lift_dwell_ticks'] - 1, len(valid)) + if all(valid[i - m['lift_dwell_ticks'] + 1:i + 1])), None) + self.metrics['lift_dwell_completed_local_tick'] = None if first is None else first + 2000 + self.metrics['lift_deadline_reserve_s'] = None if first is None else (2270 - first - 2000) * DT + if t == 2999: + swing = states[2299:2999] + air = [s['gap'] >= f['swing_gap_m_min'] and s['normal'][2] <= f['contact_force_on_N'] for s in swing] + self.metrics['swing_air_fraction'] = mean(air) + self.metrics['swing_contiguous_air_s'] = longest(air) * DT + self._gate('real_flight', mean(air) >= f['swing_air_fraction_min'] + and longest(air) * DT >= f['swing_contiguous_air_s_min'], causes) + requested = sub(self.frames[2998]['command']['target_world_m'][:2], + self.frames[2298]['command']['target_world_m'][:2]) + actual = sub(states[2998]['foot'][:2], states[2298]['foot'][:2]) + progress = [a * (1 if r >= 0 else -1) / max(abs(r), 1e-6) for a, r in zip(actual, requested)] + self.metrics['swing_xy_progress_fraction'] = progress + self._gate('three_dimensional_command', all(abs(x) > .001 for x in requested), causes) + self._gate('xy_swing_motion', all(x >= f['swing_xy_progress_fraction_min'] for x in progress), causes) + self.metrics['swing_rms_m'] = math.sqrt(mean([norm(sub(x['state']['foot'], + x['command']['target_world_m'])) ** 2 for x in self.frames[2299:2999]])) + self.metrics['swing_min_gap_minus_10mm_m'] = min(s['gap'] for s in swing) - .01 + self.metrics['swing_min_gap_minus_historical_20mm_m'] = min(s['gap'] for s in swing) - .02 + if t == CYCLE_TICKS: + window = self.frames[4399:4500] + errors = [norm(sub(x['state']['foot'], x['command']['target_world_m'])) for x in window] + self.metrics['landing_error_m'] = mean(errors) + self.metrics['landing_max_error_m'] = max(errors) + foot_mean = [mean([x['state']['foot'][j] for x in window]) for j in range(3)] + self.metrics['reload_mean_foot_world_m'] = foot_mean + self.metrics['reload_signed_anchor_error_m'] = sub(foot_mean, window[-1]['command']['target_world_m']) + self._gate('landing_position', mean(errors) <= f['landing_position_error_m_max'], causes) + self._gate('reload_mean_position', mean(errors) <= m['reload_mean_3d_error_m_max'], causes, margin=True) + valid = [s['gap'] <= m['reload_gap_m_max'] and + m['reload_load_fraction_min'] <= s['normal'][2] / s['weight'] <= m['reload_load_fraction_max'] + and s['contact'] and not s['separated'] for s in states] + tail = 0 + for ok in reversed(valid): + if not ok: + break + tail += 1 + self.metrics['reload_final_dwell_ticks'] = tail + self._gate('reload_dwell', tail >= m['reload_final_dwell_ticks'], causes, margin=True) + if clock.cycle_index == 2: + drift = norm(sub(foot_mean[:2], self.cycles[0]['metrics']['reload_mean_foot_world_m'][:2])) + self.metrics['repeat_B_reload_mean_xy_drift_m'] = drift + self._gate('repeat_B_drift', drift <= m['repeat_B_reload_mean_xy_drift_m_max'], causes, margin=True) + return causes + + def _applied(self, s, clock): + if not self.trial.event_indices: + if any(abs(x) > 1e-6 for v in s['external'] for x in v): + raise ValueError('unexpected_external_wrench') + return {} + applied = s['applied'] + if (not isinstance(applied, dict) or set(applied) != {'format', 'trial_index', 'global_tick', + 'events', 'total_com_wrench'} + or applied['format'] != 'skillfactory.contact-cycle-applied-wrench.v1' + or type(applied['trial_index']) is not int or type(applied['global_tick']) is not int + or applied['trial_index'] != self.trial.index or applied['global_tick'] != clock.global_tick): + raise ValueError('missing_or_stale_prephysics_applied_evidence') + if [e['event_index'] for e in applied['events']] != list(self.trial.event_indices): + raise ValueError('applied_event_identity') + external = [[0.] * 6 for _ in s['body_names']] + actual = {} + for e in applied['events']: + i, body = e['event_index'], e['body_index'] + program = s['event_program'][i] + if (set(e) != set(AppliedEvent.__annotations__) or type(i) is not int or type(body) is not int + or type(e['envelope']) not in (int, float) + or not 0 <= body < len(external) or s['body_names'][body] != program['body'] + or any(not vector(e[k], 3) for k in ('force_world', 'couple_world', 'point_world', + 'com_world', 'point_frame_origin_world')) + or any(not vector(e[k], 4) or abs(norm(e[k]) - 1) > 1e-5 + for k in ('frame_quat_xyzw', 'point_frame_quat_xyzw')) + or abs(e['envelope'] - s['events'][i]['envelope']) > 1e-6): + raise ValueError('applied_event_shape_or_frame') + if program['frame'] == 'world' and not close(e['frame_quat_xyzw'], [0, 0, 0, 1]): + raise ValueError('applied_world_frame') + if program['point_frame'] == 'world' and ( + not close(e['point_frame_quat_xyzw'], [0, 0, 0, 1]) + or not close(e['point_frame_origin_world'], s['env_origin'])): + raise ValueError('applied_world_point_frame') + if program['frame'] == program['point_frame'] and not close( + e['frame_quat_xyzw'], e['point_frame_quat_xyzw']): + raise ValueError('applied_inconsistent_same_frame') + for field, command in (('force_world', 'force_N'), ('couple_world', 'couple_Nm')): + expected = [x * e['envelope'] for x in rotate(e['frame_quat_xyzw'], program[command])] + if not close(e[field], expected): + raise ValueError('applied_frame_transform:' + field) + point = [a+b for a, b in zip(e['point_frame_origin_world'], + rotate(e['point_frame_quat_xyzw'], program['point_m']))] + if not close(point, e['point_world']): + raise ValueError('applied_point_transform') + moment = cross(sub(e['point_world'], e['com_world']), e['force_world']) + wrench = e['force_world'] + [a+b for a, b in zip(e['couple_world'], moment)] + external[body] = [a+b for a, b in zip(external[body], wrench)] + actual[i] = e + total = applied['total_com_wrench'] + if len(total) != len(external) or not all(vector(v, 6) for v in total): + raise ValueError('applied_total_shape') + for i, v in enumerate(external): + if not close(v, s['external'][i]): + raise ValueError('applied_external_COM_moment') + expected = [a+b+c for a, b, c in zip(v, s['guide'][i], s['auxiliary'][i])] + if not close(expected, total[i]): + raise ValueError('applied_total_COM_wrench') + return actual + + def _schedule_causes(self, e, old, program, previous, expected, index): + """Check the frozen scheduler against the prior, pre-step trigger state.""" + t = previous['state']['time'] + phase = previous['event']['command']['phase'] + state = previous['state'] + trigger = program['trigger'] + condition = { + 'time': t >= program['start_s'], + 'phase': phase == program['phase'], + 'liftoff': state['liftoff'] and phase == program['phase'] and state['edge_phase'] == program['phase'], + 'touchdown': state['touchdown'] and phase == program['phase'], + }[trigger] + fire = expected and condition and not old['fired'] + fired = old['fired'] or fire + start = (program['start_s'] if trigger == 'time' else t + program['start_s']) if fire else old['start'] + age = t - start + envelope = max(0., min(1., age / program['ramp_s'], + (program['duration_s'] - age) / program['ramp_s'])) if fired else 0. + completed = old['completed'] or fired and age >= program['duration_s'] + # Float32 schedule clocks may straddle the exact end by a few ulps. + completion_ok = e['completed'] == completed or fired and abs(age - program['duration_s']) <= SCHEDULE_ROUNDOFF_S + if (e['fired'] != fired or abs(e['start'] - start) > 2e-6 + or abs(e['envelope'] - envelope) > 1e-5 or not completion_ok): + return ['recording_event_trigger_or_envelope:' + str(index)] + return [] + + def _events(self, s, clock): + causes = [] + try: + actual = self._applied(s, clock) + except (KeyError, TypeError, ValueError, IndexError) as exc: + causes.append('recording_applied:' + str(exc)) + actual = {} + if self.event_integrals is None: + self.event_integrals = [{'force_impulse_Ns': [0.] * 3, 'couple_impulse_Nms': [0.] * 3, + 'peak_force_N': 0., 'peak_couple_Nm': 0.} for _ in s['events']] + delivery = [] + previous = self.event_previous['state'] if self.event_previous else self.initial['state'] + for i, (e, program) in enumerate(zip(s['events'], s['event_program'])): + expected = i in self.trial.event_indices + old = previous['events'][i] + causes += self._schedule_causes(e, old, program, self.event_previous or self.initial, expected, i) + if (e['integral'] < old['integral'] or e['fired'] < old['fired'] + or e['completed'] < old['completed'] + or abs(e['integral'] - old['integral'] - e['envelope'] * DT) > 1e-6 + or not 0 <= e['envelope'] <= 1 + or old['fired'] and e['start'] != old['start']): + causes.append('recording_event_history:' + str(i)) + integral = self.event_integrals[i] + if i in actual: + for key, field in (('force_impulse_Ns', 'force_world'), ('couple_impulse_Nms', 'couple_world')): + integral[key] = [a + b * DT for a, b in zip(integral[key], actual[i][field])] + integral['peak_force_N'] = max(integral['peak_force_N'], norm(actual[i]['force_world'])) + integral['peak_couple_Nm'] = max(integral['peak_couple_Nm'], norm(actual[i]['couple_world'])) + area = program['duration_s'] - program['ramp_s'] if expected else 0. + tolerance = self.protocol.document['thresholds']['event_delivery']['area_tolerance_s'] + ok = (e['fired'] == expected and e['completed'] == expected + and abs(e['integral'] - area) <= tolerance) + # Fired schedules use the recorded pre-step clock/start, not a nominal + # phase deadline. A false completion gets only bounded float32 slack. + if e['fired']: + age = previous['time'] - e['start'] + due = e['completed'] or age > program['duration_s'] + SCHEDULE_ROUNDOFF_S + elif program['trigger'] == 'time': + due = previous['time'] >= program['start_s'] + program['duration_s'] + else: + # This tick consumed the final eligible pre-step invocation. + # An edge first observed at the boundary belongs to the next phase. + due = clock.local_tick >= PHASE_END_TICKS[program['phase']] + determinate = due or not expected or clock.global_tick == self.trial.total_ticks + if determinate and not ok: + causes.append('event_delivery:' + str(i)) + if not expected and e['envelope']: + causes.append('identity_unexpected_event:' + str(i)) + delivery.append({'index': i, 'expected': expected, 'fired': e['fired'], 'completed': e['completed'], + 'missed': expected and not e['fired'], 'expected_area_s': area, + 'actual_area_s': e['integral'], 'area_error_s': e['integral'] - area, + 'absolute_start_s': e['start'] if e['fired'] else None, + 'trigger_delay_s': program['start_s'], + 'actual_applied_evidence_available': i in actual or not expected, + 'status': ok if determinate else NOT_EVALUATED, **plain(integral)}) + if not self.trial.event_indices and any(abs(x) > 1e-6 for v in s['external'] for x in v): + causes.append('identity_unexpected_external_wrench') + self.delivery = delivery + self.event_previous = plain(self.last) + if clock.local_tick == CYCLE_TICKS: + self.gates['event_delivery'] = all(e['status'] is True for e in delivery) + if self.trial.event_indices and all(s['events'][i]['completed'] for i in self.trial.event_indices): + c = self.last['event']['command'] + error = sub(s['root'][:3], c['root_target_world_m']) + p = c['phase'] + appropriate = (s['gap'] >= .01 if p == 3 else + s['gap'] >= .02 and s['normal'][2] <= 2 if p == 2 and c['progress'] > .9 else + s['gap'] <= .0005 and s['normal'][2] >= .5 * s['weight'] if p in (0, 5) else + not s['premature']) + good = norm(error[:2]) <= .03 and abs(error[2]) <= .06 and s['tilt'] <= .3 and appropriate + self.recovery_run = self.recovery_run + 1 if good else 0 + if self.recovery_run >= 50 and self.recovery['status'] != 'recovered': + completion = max(s['events'][i]['start'] + s['event_program'][i]['duration_s'] + for i in self.trial.event_indices) + start = (clock.global_tick - 49) * DT + self.recovery.update(status='recovered', time_s=start - completion, absolute_start_s=start) + elif self.recovery['status'] != 'recovered': + self.recovery['status'] = 'censored_not_recovered' + return causes + + def _cycle_report(self, index, passed=False): + metrics = plain(self.metrics) + if self.frames: + metrics['height_rms_m'] = math.sqrt(mean([ + (x['state']['root'][2] - x['command']['root_target_world_m'][2]) ** 2 for x in self.frames])) + metrics['phase_counts'] = [sum(x['command']['phase'] == p for x in self.frames) for p in range(6)] + metrics['phase_min_gap_m'] = [min((x['state']['gap'] for x in self.frames if x['command']['phase'] == p), + default=None) for p in range(6)] + metrics['phase_max_normal_N'] = [max((x['state']['normal'][2] for x in self.frames + if x['command']['phase'] == p), default=None) for p in range(6)] + return {'cycle_index': index, 'samples': len(self.frames), 'passed': passed, + 'gates': plain(self.gates), 'P1_margins': plain(self.margins), + 'P1_margins_role': self.protocol.document['thresholds']['P1_margins_role'], 'metrics': metrics} + + def _finish_cycle(self, clock, causes): + applicable = [v for k, v in self.margins.items() if k != 'repeat_B_drift' or clock.cycle_index == 2] + passed = (not causes and all(v is True for v in self.gates.values()) + and (self.protocol.document['stage'] == 'P2' or all(v is True for v in applicable))) + self.cycles.append(self._cycle_report(clock.cycle_index, passed)) + if not passed and not self.first_failure: + self.fail(('recording_incomplete_cycle_gates',)) + self.frames = [] + self._cycle_start() + + def report(self): + cycles = plain(self.cycles) + if self.frames: + cycles.append(self._cycle_report(len(cycles))) + for i in range(len(cycles), len(self.trial.anchor_sequence) - 1): + cycles.append({'cycle_index': i, 'samples': 0, 'passed': False, 'status': NOT_EVALUATED}) + prefix = 0 + for c in cycles: + if not c['passed']: + break + prefix += 1 + return {'format': FORMAT, 'trial': plain(asdict(self.trial)), 'observed_physics_ticks': self.tick, + 'completed_clock_cycles': self.tick // CYCLE_TICKS, 'passed_prefix_cycles': prefix, + 'requested_cycles': len(cycles), 'cycles': cycles, 'first_failure': plain(self.first_failure), + 'protocol_complete': self.tick == self.trial.total_ticks, + 'all_causes': plain(self.all_causes), + 'functional_passed': self.tick == self.trial.total_ticks and prefix == len(cycles) + and self.first_failure is None, + 'total_stance_slip_m': self.total_slip, 'delivery': plain(self.delivery), + 'recovery_diagnostic': plain(self.recovery), 'seams': plain(self.seams), + 'initial_pair_state': (None if self.initial is None else + {k: plain(self.initial['state'][k]) for k in ( + 'root', 'q', 'qdot', 'foot', 'anchors', 'sensor_delay', 'action_delay', + 'q_ik', 'sensed', 'actuator_state', 'rng')}), + 'accepted_skill': False, 'optimizer_steps': 0} + + +def matrix_report(protocol, rows): + """All preregistered rows, including not-run; pair equality is evidence, not assumed.""" + indexed = {} + for row in rows: + index = row['trial']['index'] + if index in indexed or row['trial'] != plain(asdict(protocol.trial(index))): + raise ValueError('Duplicate or foreign trial row') + indexed[index] = row + result = [indexed.get(t.index, {'trial': plain(asdict(t)), 'status': 'not_run', + 'functional_passed': False, 'accepted_skill': False}) for t in protocol.trials] + pairs = [] + for a, b in zip(result[::2], result[1::2]): + pair = {'pair_id': a['trial']['pair_id'], 'status': NOT_EVALUATED} + if isinstance(a.get('initial_pair_state'), dict) and isinstance(b.get('initial_pair_state'), dict): + pair['initial_exactly_equal'] = a['initial_pair_state'] == b['initial_pair_state'] + if not pair['initial_exactly_equal']: + pair.update(status='invalid_initial_pair', both_functional_passed=False, + learned_minus_reference=NOT_EVALUATED) + pairs.append(pair) + continue + pair['status'] = 'evaluated' + pair['both_functional_passed'] = a['functional_passed'] and b['functional_passed'] + pair['failure_onsets'] = [x['first_failure'] for x in (a, b)] + pair['learned_minus_reference'] = {} + for metric in ('swing_rms_m', 'landing_error_m', 'max_root_horizontal_error_m'): + left = a['cycles'][0].get('metrics', {}).get(metric) + right = b['cycles'][0].get('metrics', {}).get(metric) + pair['learned_minus_reference'][metric] = (left - right if left is not None and right is not None + else NOT_EVALUATED) + pair['recovery_diagnostics'] = [x['recovery_diagnostic'] for x in (a, b)] + pairs.append(pair) + return {'rows': result, 'pairs': pairs, 'accepted_skill': False, 'optimizer_steps': 0} diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_binding.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_binding.py new file mode 100644 index 0000000..f202233 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_binding.py @@ -0,0 +1,1375 @@ +"""Prospective evaluation identity, not execution permission or frozen-policy loading. + +No Isaac imports are performed here. The authorized launcher must import the real +file-backed modules or validated source-root PEP 420 packages first. Preparation returns immutable canonical JSON bytes; +verification requires a separately trusted digest of those exact bytes. +""" +from __future__ import annotations + +import dataclasses +import enum +import functools +import hashlib +import inspect +import json +import math +import sys +from collections.abc import Mapping +from importlib._bootstrap_external import _NamespacePath +from importlib.machinery import ExtensionFileLoader, ModuleSpec, NamespaceLoader, PathFinder +from pathlib import Path, PosixPath, WindowsPath +from types import CodeType, MappingProxyType, ModuleType +from typing import Any + +from .evaluation_protocol import load_protocol + +FORMAT = "skillfactory.contact-cycle-evaluation-binding.v5" +EVALUATION_ID = "contact-cycle-independent-p1-p2-v1" +PACKAGE = "source/SkillFactory/SkillFactory/" +FIXED_SOURCES = ( + PACKAGE + "legs/contact_cycle_sim/evaluation_binding.py", + PACKAGE + "legs/contact_cycle_sim/evaluation_protocol.py", + PACKAGE + "legs/contact_cycle_sim/evaluation_audit.py", + PACKAGE + "tasks/manager_based/contact_cycle_sim/evaluation_cfg.py", + PACKAGE + "tasks/manager_based/contact_cycle_sim/evaluation_env.py", + PACKAGE + "tasks/manager_based/contact_cycle_sim/evaluation_mdp.py", + PACKAGE + "tasks/manager_based/contact_cycle_sim/evaluation_recording.py", + "scripts/audit_cycle_sim_evaluation.py", + "configs/contact_cycle/continuous_eval_p1_v1.json", + "configs/contact_cycle/bounded_diagnostic_p2_v1.json", + "configs/contact_cycle/bounded_diagnostic_p2_extended_v1.json", + PACKAGE + "legs/contact_cycle_sim/evaluation_adoption.py", +) +# Exact additive files hashed without importing or activating training code. +SOURCE_ONLY = ( + "scripts/prepare_cycle_sim_binding.py", + "scripts/adopt_cycle_sim_p1_evidence.py", + PACKAGE + "legs/contact_cycle_sim/rootxy_diagnostics_v2.py", + PACKAGE + "legs/contact_cycle_sim/rootxy_runner_v2.py", + PACKAGE + "legs/contact_cycle_sim/rootxy_training_v2.py", + PACKAGE + "tasks/manager_based/contact_cycle_sim/rootxy_cfg_v2.py", + PACKAGE + "tasks/manager_based/contact_cycle_sim/rootxy_recording_v2.py", + "scripts/train_cycle_sim_rootxy_v2.py", + "configs/contact_cycle/sim_training_rootxy_proposal_v2.json", + "configs/contact_cycle/sim_training_rootxy_v2.json", + "configs/contact_cycle/simulation_residual_rootxy_v2.json", +) +FIXED_SOURCES += SOURCE_ONLY +ISAAC_ROOT = Path("/home/cen/IsaacLab") +# The first three pins are the unchanged runtime's pins. The last two are the +# actual PhysX implementation (not isaaclab's backend-dispatching factory). +ISAAC_PINS = MappingProxyType({ + "isaaclab.sim.utils.prims": ( + "source/isaaclab/isaaclab/sim/utils/prims.py", + "b93e7f5cd0cf78dde4abc8c3e3a1607e006c91e0056f84bb16c8e7048de27771"), + "isaaclab.utils.string": ( + "source/isaaclab/isaaclab/utils/string.py", + "8943b90128542370fbecdfe4de1561722ff85901dc699b6051b50c1b7e43266f"), + "isaaclab.utils.configclass": ( + "source/isaaclab/isaaclab/utils/configclass.py", + "c0856c132267eecedd790698a67728ac799a835d9044ebe5d9608f099146653e"), + "isaaclab.scene.interactive_scene": ( + "source/isaaclab/isaaclab/scene/interactive_scene.py", + "caf1c0646707d6c5a3c28a81020cab741987d96e74f3e43290f0584e221afe40"), + "isaaclab.scene.interactive_scene_cfg": ( + "source/isaaclab/isaaclab/scene/interactive_scene_cfg.py", + "b518a9781d40824933710b6e2a1408a3d1313c3510edff7cc2510f769b37f5e6"), + "isaaclab.envs.manager_based_env": ( + "source/isaaclab/isaaclab/envs/manager_based_env.py", + "2159b887235fd664684be917c34890e9054e51848855148229216d4f2ec85ad8"), + "isaaclab.envs.manager_based_rl_env": ( + "source/isaaclab/isaaclab/envs/manager_based_rl_env.py", + "a963cfcdb4cd60955cd022ed709d27c99a6935685b69180bb29d74f8983f6566"), + "isaaclab.sensors.joint_wrench.joint_wrench_sensor_cfg": ( + "source/isaaclab/isaaclab/sensors/joint_wrench/joint_wrench_sensor_cfg.py", + "4b6b711e873e59fc7f3c6790e3dac3f5bd67289bf33a84ebbb4833a4a796b2da"), + "isaaclab.sensors.sensor_base_cfg": ( + "source/isaaclab/isaaclab/sensors/sensor_base_cfg.py", + "63a03c348249c3a789a48edefc29236a9d19b48c344ff3cf0dcc1cec382a0296"), + "isaaclab.utils.wrench_composer": ( + "source/isaaclab/isaaclab/utils/wrench_composer.py", + "8fb3ab4e59db121165db52daf61e0de0e73653f0c154a143f6a828c198082c06"), + "isaaclab_physx.assets.articulation.articulation": ( + "source/isaaclab_physx/isaaclab_physx/assets/articulation/articulation.py", + "47d96553282b389493e69f691b8dcbfeb98997a5a80b88c2571c60ba8b58cb6d"), + "isaaclab_physx.assets.articulation.articulation_data": ( + "source/isaaclab_physx/isaaclab_physx/assets/articulation/articulation_data.py", + "43a31992353ad3a500024d0ec908d5b253ada7a0aac9a1cd2261466e10bda4ff"), +}) +SEED_OVERRIDE = MappingProxyType({"seed": "protocol.trial.seed"}) + + +def sha256(payload: bytes) -> str: + return hashlib.sha256(payload).hexdigest() + + +def canonical_json(value: Any) -> bytes: + return json.dumps(value, sort_keys=True, separators=(",", ":"), + ensure_ascii=True, allow_nan=False).encode("utf-8") + + +def _file(path: Path) -> Path: + """Require exact absolute spelling: no '..', symlinks, or resolved aliases.""" + path = Path(path) + if (not path.is_absolute() or path != path.resolve(strict=True) + or any(p.is_symlink() for p in (path, *path.parents)) or not path.is_file()): + raise ValueError(f"Noncanonical or missing source path: {path}") + return path + + +def _relative(root: Path, relative: str) -> Path: + if (type(relative) is not str or not relative or Path(relative).is_absolute() + or relative != Path(relative).as_posix() or ".." in Path(relative).parts): + raise ValueError("Explicit canonical relative source path required") + return _file(root / relative) + + +# Only the inspected installation links, including their lstat identity, may +# spell runtime files differently. Project/config/symbol _file stays canonical. +SIM_ROOT = Path("/home/cen/isaacsim-6.0.1") +RUNTIME_LINKS = ( + ("/home/cen/IsaacLab/_isaac_sim", "/home/cen/isaacsim", 66310, 9699804), + ("/home/cen/isaacsim", "/home/cen/isaacsim-6.0.1", 66310, 8949820), +) +FAST_IMPORTER = "omni.ext._impl.fast_importer" +FAST_IMPORTER_RELATIVE = "kit/kernel/py/omni/ext/_impl/fast_importer.py" +FAST_IMPORTER_SHA256 = "cdaa2d6299abc5df78bfcce2da5421c8c59426725e0ee0b1cf608a6a19eee7cd" +PROVENANCE_SCOPE = "development-finite-runtime-provenance-v5" +PROVENANCE_LIMITATIONS = ( + "unverified-dynamic-internals: incidental metadata is observational, not verified", + "unobserved dynamic-provider internals", + "not complete transitive runtime attestation or a security sandbox", + "native solver, loaded linker resolution, drivers and JIT internals unobserved", +) + + +# Finite execution dependencies selected by source role, NOT arbitrary object metadata. +REQUIRED_RUNTIME = MappingProxyType({ + 'isaaclab_physx.physics.physx_manager': ("physics-stepping-provider", + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/physics/physx_manager.py', + '79852394dafa48aeb5e9cdb39072dde27c9ab35f6b587a17ce931318ab44fd4a'), + 'isaaclab.physics.physics_manager': ("physics-stepping-provider", + '/home/cen/IsaacLab/source/isaaclab/isaaclab/physics/physics_manager.py', + '771493538eb25a700583266197bd559959a2ad017870af2e8354bc7876d0ee50'), + 'omni.timeline': ("physics-stepping-provider", + '/home/cen/isaacsim-6.0.1/extscache/omni.timeline-1.1.0+f9bf0dda.lx64.r.cp312/omni/timeline/__init__.py', + '4657e8a3cc5f1e8214a131f21f2f065e526de2d421b126ea144d69c3f51838a0'), + 'omni.usd': ("physics-stepping-provider", + '/home/cen/isaacsim-6.0.1/extscache/omni.usd-1.16.0+f9bf0dda.lx64.r.cp312/omni/usd/__init__.py', + '0f1c91bb2d686d84d296f7c2b66a9de82c4c35af2108f86e65b5097a5244c668'), + 'omni.physics.tensors.api': ("physics-stepping-provider", + '/home/cen/isaacsim-6.0.1/extscache/omni.physics.tensors-110.1.13+110.1.2.lx64.r.cp312.u7f4/omni/physics/tensors/api.py', + 'b913fdb1a3c0cf04b62aa01d6ceaf7d96cfe577a510d0e02b71485018661c903'), + 'omni.physics.tensors.bindings._physicsTensors': ("physics-stepping-provider", + '/home/cen/isaacsim-6.0.1/extscache/omni.physics.tensors-110.1.13+110.1.2.lx64.r.cp312.u7f4/omni/physics/tensors/bindings/_physicsTensors.cpython-312-x86_64-linux-gnu.so', + 'cf6a18bfbfcb22764e4c1e205f42c48c815ebd0c84f03c8045e480dcd896a15a'), + 'omni.physx.bindings._physx': ("physics-stepping-provider", + '/home/cen/isaacsim-6.0.1/extscache/omni.physx-110.1.13+110.1.2.lx64.r.cp312.u7f4/omni/physx/bindings/_physx.cpython-312-x86_64-linux-gnu.so', + 'ccb0ddafa214d6ecf93903632a6c4b73e9e0d02cddfa1ca56e1dd2b65a5a8d6f'), + 'torch': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/torch/__init__.py', + '51c90fe34a7cf869517d1bab4cd114498790e169ec668f1a154c35ec64117a5e'), + 'torch._C': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/torch/_C.cpython-312-x86_64-linux-gnu.so', + '90fc84350d2de15ca167734eb63b20b3a1a4ea2721d498b66e3e65174c0cff9f'), + 'torch.random': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/torch/random.py', + '628b865c4dfa6b742c38b3fefead2cc7251f945d575c27ed052951c569702f41'), + 'torch.cuda': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/torch/cuda/__init__.py', + '0161199fa03888a8744550727d1026a82f140c8dd31b265a26b41115f7dc857d'), + 'numpy': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/numpy/__init__.py', + '09295a80660f17925ae23765ce8cbd7ff7ceae968d5f2f89349f1cb74c0b9e11'), + 'numpy.random': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/numpy/random/__init__.py', + '585ce7b73b5454d6a25c2a50967f2dc322fc1d214d4bb5c0589949b105e06ea9'), + 'numpy.random.mtrand': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/numpy/random/mtrand.cpython-312-x86_64-linux-gnu.so', + '23d0689770f3b1145b4e2a87a4d4641c8a5d33ca52b6b9fca3fc6ff01843a638'), + 'numpy._core._multiarray_umath': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/numpy/_core/_multiarray_umath.cpython-312-x86_64-linux-gnu.so', + '3aac21341706a466577d7c7caab9c5013e98ffb62cf98ae3bc46a3e1004f3ce0'), + 'warp': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/warp/__init__.py', + '4a272a0c07b087b8f43710ebb061f02b1bc002a1bf4f701c336528c64a6ba2f7'), + 'warp._src.context': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/warp/_src/context.py', + '40948811fbc2e99773883f762cc0eb939718b11d26676739e4332b13337d5ee2'), + 'warp._src.types': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/warp/_src/types.py', + '36e97b368411190363409387a665896f1b21160ef12283e3e5883b9f01391d2e'), + 'warp._src.builtins': ('numerical-core', + '/home/cen/isaacsim-6.0.1/kit/python/lib/python3.12/site-packages/warp/_src/builtins.py', + '11b38c6bbcf15df706cc45247a9480c7e587b0f2d513111348ad91093cb08251'), + 'carb': ('kit-provider', + '/home/cen/isaacsim-6.0.1/kit/kernel/py/carb/__init__.py', + 'ff2cc445b795a340d68bfe04b0d1a43520e562beba14ae60cc6c8d6848f55d3c'), + 'omni.kit.app': ('kit-provider', + '/home/cen/isaacsim-6.0.1/kit/kernel/py/omni/kit/app/__init__.py', + '47418defe37bb7bf2feb882a65506aead28ad1cec074da45da5ab02dcb80876e'), + 'omni.physics.tensors': ('kit-provider', + '/home/cen/isaacsim-6.0.1/extscache/omni.physics.tensors-110.1.13+110.1.2.lx64.r.cp312.u7f4/omni/physics/tensors/__init__.py', + '377af3ebe0786768209c0582f5032db37d712e1a45c6f58f00e5b5284158afd4'), + 'omni.physx': ('kit-provider', + '/home/cen/isaacsim-6.0.1/extscache/omni.physx-110.1.13+110.1.2.lx64.r.cp312.u7f4/omni/physx/__init__.py', + 'bd4a23138dff6d2ae9e2c703c616641e89ed7fa83c096aa49b8c6550c85dc88e'), + 'pxr.Tf': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.libs-1.0.3+f9bf0dda.lx64.r.cp312/pxr/Tf/__init__.py', + '483b9f300080181276c407210be09bba07e2bc43a65ec4a489df05d846226902'), + 'pxr.Gf': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.libs-1.0.3+f9bf0dda.lx64.r.cp312/pxr/Gf/__init__.py', + '01dd802e1595abbabe0dd412e659594634438c2a677a69192081a81b03fc29bf'), + 'pxr.Sdf': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.libs-1.0.3+f9bf0dda.lx64.r.cp312/pxr/Sdf/__init__.py', + '01ba7cf4c245b4288ea3c0367491b6cdb01446b3931f95b5eff800566dc5921c'), + 'pxr.Usd': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.libs-1.0.3+f9bf0dda.lx64.r.cp312/pxr/Usd/__init__.py', + '296d06c2e8bcadecfd1e622b19a3354b5af5b951fa2881e89974fcc0d9a02747'), + 'pxr.UsdGeom': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.libs-1.0.3+f9bf0dda.lx64.r.cp312/pxr/UsdGeom/__init__.py', + '296d06c2e8bcadecfd1e622b19a3354b5af5b951fa2881e89974fcc0d9a02747'), + 'pxr.UsdLux': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.libs-1.0.3+f9bf0dda.lx64.r.cp312/pxr/UsdLux/__init__.py', + '00b372a13b06de79b865f8e7ed489a434873cb1d6d517d5b355ebb5a8d7f201d'), + 'pxr.UsdPhysics': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.libs-1.0.3+f9bf0dda.lx64.r.cp312/pxr/UsdPhysics/__init__.py', + 'edec069a2e138c7dbf8972092b904e1b94cc57f1390637cfac110a1095a4ef23'), + 'pxr.UsdShade': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.libs-1.0.3+f9bf0dda.lx64.r.cp312/pxr/UsdShade/__init__.py', + '296d06c2e8bcadecfd1e622b19a3354b5af5b951fa2881e89974fcc0d9a02747'), + 'pxr.UsdUtils': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.libs-1.0.3+f9bf0dda.lx64.r.cp312/pxr/UsdUtils/__init__.py', + '4346706cdcf53b9ce9193cd8a22340d340b538cec23f85fef987b162bbdcb85d'), + 'pxr.Vt': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.libs-1.0.3+f9bf0dda.lx64.r.cp312/pxr/Vt/__init__.py', + '37cd73fd76692b7d5faff0d6f7a379f6145f26727223b66683c5266442a4cdee'), + 'pxr.PhysxSchema': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.schema.physx-110.1.13+110.1.2.lx64.r.cp312.u7f4/pxr/PhysxSchema/__init__.py', + '95ba057f6b88ff3343fe3afa101fb65038b0a803606d2bb9fcea0327a352d4f2'), + 'pxr.PhysicsSchemaTools': ('usd-wrapper', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.schema.physx-110.1.13+110.1.2.lx64.r.cp312.u7f4/pxr/PhysicsSchemaTools/__init__.py', + '016484b41e4ac8b975c77e5306d5b271bb9812a5dbcb6456733fce2ca5f4c53e'), + 'isaaclab.sim.simulation_context': ('stepping-importer', + '/home/cen/IsaacLab/source/isaaclab/isaaclab/sim/simulation_context.py', + '819536a201b525774b4bc04d6bd9a9e3fc11397961b4881e9083eb4502ee958a'), + 'omni.ext._impl.fast_importer': ('stepping-importer', + '/home/cen/isaacsim-6.0.1/kit/kernel/py/omni/ext/_impl/fast_importer.py', + 'cdaa2d6299abc5df78bfcce2da5421c8c59426725e0ee0b1cf608a6a19eee7cd'), + 'simulation_app.simulation_app': ('stepping-importer', + '/home/cen/isaacsim-6.0.1/exts/isaacsim.simulation_app/isaacsim/simulation_app/simulation_app.py', + '6af5372bb0cdda6e665a30cd285ff724b3e5bd012dbdd4df458e0726173fc903'), +}) +RUNTIME_LAYOUT_PINS = MappingProxyType({ + '/home/cen/IsaacLab/source/isaaclab_physx/config/extension.toml': + '817d3d8a29ef534e0d1dd1e23a88aa1d741e7b4c76e2a2bbc43b7b56d30ac888', + '/home/cen/isaacsim-6.0.1/extscache/omni.timeline-1.1.0+f9bf0dda.lx64.r.cp312/config/extension.toml': + '3a6849c330068a5372a41e6b1abc7992a15e14d1d2582720862e92ce4fb767c3', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd-1.16.0+f9bf0dda.lx64.r.cp312/config/extension.toml': + 'b65a4cb57313c2dd95baa09b8b83ae6e64168a2de902cc3bec7de58ac89e346c', + + '/home/cen/isaacsim-6.0.1/extscache/omni.physics.tensors-110.1.13+110.1.2.lx64.r.cp312.u7f4/config/extension.toml': + '1ac0ebd2fde3c3e722cba2614d638034d0b48f2d6cb3f0bb0e840d2dc0a8843d', + '/home/cen/isaacsim-6.0.1/extscache/omni.physx-110.1.13+110.1.2.lx64.r.cp312.u7f4/config/extension.toml': + '36fecddd676f1e5de19157684113afe94a4a90b8fbedd59827c164abaf7b2f9e', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.libs-1.0.3+f9bf0dda.lx64.r.cp312/config/extension.toml': + '673847c415b733148786b1e7bc6425ff928c763ed31bdc00eed34eaf1b250e85', + '/home/cen/isaacsim-6.0.1/extscache/omni.usd.schema.physx-110.1.13+110.1.2.lx64.r.cp312.u7f4/config/extension.toml': + 'b62b5a5f94f8aad1eb902f37ef09b430d9ee7bdbbefdc2c143126f2ee92ce349', + '/home/cen/IsaacLab/source/isaaclab/config/extension.toml': + 'e7b4ce684455538f815efd930913bdd54d38b2c5ba80e038a2b51390da00eb07', + '/home/cen/isaacsim-6.0.1/exts/isaacsim.simulation_app/config/extension.toml': + '9d8dcac4ea340d5510d70ccf4023fed762e461f98236e9d0735c07f91316eb8f', +}) + + +def _runtime_path(path: Path, *, directory: bool = False, content_hash: bool = True) -> dict: + raw = str(path) + if not path.is_absolute() or ".." in path.parts: + raise ValueError(f"Noncanonical runtime path: {path}") + links = [] + allowed = {p: (target, device, inode) for p, target, device, inode in RUNTIME_LINKS} + # Walk again after resolving each link so the second installed alias is + # recorded too. Reject any other link, including one below the physical root. + current = path + while True: + link = next((p for p in reversed((current, *current.parents)) if p.is_symlink()), None) + if link is None: + break + target = str(link.readlink()) + stat = link.lstat() + if allowed.get(str(link)) != (target, stat.st_dev, stat.st_ino): + raise ValueError(f"Unbound runtime symlink: {link}") + links.append({"path": str(link), "target": target, "device": stat.st_dev, + "inode": stat.st_ino, "target_sha256": sha256(target.encode())}) + current = Path(target) / current.relative_to(link) + resolved = path.resolve(strict=True) + if current != resolved or (links and not resolved.is_relative_to(SIM_ROOT)): + raise ValueError(f"Foreign runtime alias tree: {path}") + if not (resolved.is_dir() if directory else resolved.is_file()): + raise ValueError(f"Missing runtime {'directory' if directory else 'file'}: {path}") + record = {"raw_path": raw, "path": str(resolved), "links": links} + if not directory and content_hash: + record["sha256"] = sha256(resolved.read_bytes()) + return record + + +def _object_type(value: Any) -> str: + cls = type(value) + return cls.__module__ + "." + cls.__qualname__ + + +def module_diagnostic(name: str) -> dict: + """Read raw dictionaries only; never import, resolve a provider or recalc paths.""" + module = sys.modules.get(name) + data = vars(module) if isinstance(module, ModuleType) else {} + spec = data.get("__spec__") + fields = vars(spec) if isinstance(spec, ModuleSpec) else {} + loader = data.get("__loader__") + loader_fields = {key: inspect.getattr_static(loader, key, None) for key in ("name", "path")} + locations = data.get("__path__") + path_fields = vars(locations) if type(locations) is _NamespacePath else {} + finder = path_fields.get("_path_finder") + + def scalar(value): + return value if value is None or type(value) in (str, bool, int, float) else {"type": _object_type(value)} + + def raw_locations(value): + seen = set() + while type(value) is _NamespacePath and id(value) not in seen: + seen.add(id(value)) + value = vars(value).get("_path") + return [scalar(p) for p in value] if type(value) in (list, tuple) else scalar(value) + + return {"module_type": _object_type(module), "name": scalar(data.get("__name__")), + "file": scalar(data.get("__file__")), "package": scalar(data.get("__package__")), + "spec_type": _object_type(spec), "spec_name": scalar(fields.get("name")), + "spec_origin": scalar(fields.get("origin")), "loader_type": _object_type(loader), + "spec_loader_type": _object_type(fields.get("loader")), + "spec_loader_name": scalar(inspect.getattr_static(fields.get("loader"), "name", None)), + "spec_loader_path": scalar(inspect.getattr_static(fields.get("loader"), "path", None)), + "loader_name": scalar(loader_fields.get("name")), "loader_path": scalar(loader_fields.get("path")), + "search_locations": raw_locations(locations), + "spec_search_locations": raw_locations(fields.get("submodule_search_locations")), + "search_locations_type": _object_type(locations), + "path_finder_module": finder.__module__ if inspect.ismethod(finder) or inspect.isfunction(finder) else None, + "path_finder_qualname": ( + finder.__qualname__ if inspect.ismethod(finder) or inspect.isfunction(finder) else None)} + + +def _fast_importer_identity(finder: Any) -> dict: + owner = sys.modules.get(FAST_IMPORTER) + cls = vars(owner).get("FastFinder") if isinstance(owner, ModuleType) else None + if (cls is None or cls not in sys.meta_path or finder != cls.find_spec + or not inspect.ismethod(finder) or finder.__self__ is not cls): + raise ValueError("Unbound namespace importer callback") + path = _module_file(FAST_IMPORTER) + if path != SIM_ROOT / FAST_IMPORTER_RELATIVE or sha256(path.read_bytes()) != FAST_IMPORTER_SHA256: + raise ValueError("Pinned Kit FastFinder source mismatch") + if finder.__func__.__globals__ is not vars(owner): + raise ValueError("Replaced Kit FastFinder globals") + _source_code(finder.__func__, path) + return {"module": FAST_IMPORTER, "callable": "FastFinder.find_spec", + "observation": module_diagnostic(FAST_IMPORTER), + "source": _runtime_path(Path(vars(owner)["__file__"]))} + + +def _namespace_importer(locations: Any) -> dict: + finder = getattr(locations, "_path_finder", None) + if finder == PathFinder._get_spec: + return {"kind": "cpython-pathfinder"} + return {"kind": "kit-fastfinder", **_fast_importer_identity(finder)} + + +def _pxr_native_wrapper(name: str, module: ModuleType, path: Path) -> dict: + """Bind one wrapper/native pair to its own pinned extension owner.""" + parts = name.split(".") + if len(parts) != 2 or parts[0] != "pxr" or not parts[1].isidentifier(): + raise ValueError(f"Imported module spec/path mismatch: {name}") + package = path.parent.parent + manifest = package.parent / "config/extension.toml" + if (not package.is_relative_to(SIM_ROOT) or package.name != "pxr" + or str(manifest) not in RUNTIME_LAYOUT_PINS + or sha256(_file(manifest).read_bytes()) != RUNTIME_LAYOUT_PINS[str(manifest)] + or path != package / parts[1] / "__init__.py"): + raise ValueError(f"Foreign pxr wrapper: {name}") + native_name = name + "._" + parts[1][0].lower() + parts[1][1:] + native = sys.modules.get(native_name) + spec = vars(module).get("__spec__") + if (not isinstance(native, ModuleType) or native is module + or spec is not vars(native).get("__spec__") + or vars(module).get("__loader__") is not vars(native).get("__loader__")): + raise ValueError(f"Unbound pxr native wrapper: {name}") + native_path = _module_file(native_name) + if (native_path.parent != path.parent or ".so" not in native_path.suffixes + or type(spec.loader) is not ExtensionFileLoader or spec.loader.name != native_name + or spec.loader is not vars(native).get("__loader__") + or Path(_runtime_path(Path(spec.loader.path), content_hash=False)["path"]) != native_path): + raise ValueError(f"Foreign pxr native binary: {name}") + return {"kind": "pxr-native-wrapper", "native_module": native_name, + "native": _runtime_path(Path(vars(native)["__file__"])), + "wrapper_source": _runtime_path(Path(vars(module)["__file__"])), + "extension_manifest": _runtime_path(manifest)} + + +def _pxr_wrapper(name: str, module: ModuleType, path: Path) -> dict: + """Tf.PrepareModule copies native spec/loader, but Tf can have another owner.""" + record = _pxr_native_wrapper(name, module, path) + tf = sys.modules.get("pxr.Tf") + if (not isinstance(tf, ModuleType) or vars(tf).get("__name__") != "pxr.Tf" + or not isinstance(vars(tf).get("__file__"), str)): + raise ValueError("Missing or aliased imported module: pxr.Tf") + raw = vars(tf)["__file__"] + if raw != str(Path(raw)): + raise ValueError("Noncanonical imported __file__ spelling: pxr.Tf") + source = _runtime_path(Path(raw)) + _, expected_path, expected_hash = REQUIRED_RUNTIME["pxr.Tf"] + if source["path"] != expected_path or source["sha256"] != expected_hash: + raise ValueError("Required runtime pin/path mismatch: pxr.Tf") + # Do not call _module_file(Tf) here: Tf itself uses PreparePythonModule, + # so its copied native spec must be checked without recursive Tf resolution. + tf_wrapper = _pxr_native_wrapper("pxr.Tf", tf, Path(source["path"])) + record["prepare_module_source"] = source + record["prepare_module_identity"] = { + "module": "pxr.Tf", "observation": module_diagnostic("pxr.Tf"), **tf_wrapper} + return record + + +def _dependency_module_identity(name: str) -> dict: + module = sys.modules.get(name) + if not isinstance(module, ModuleType) or vars(module).get("__name__") != name: + raise ValueError(f"Missing or aliased imported module: {name}") + if not vars(module).get("__file__"): + if name in REQUIRED_RUNTIME: + raise ValueError(f"Required runtime has no implementation file: {name}") + return _provider_namespace_identity(name, module) + path = _module_file(name) + if name in REQUIRED_RUNTIME: + _, expected_path, expected_hash = REQUIRED_RUNTIME[name] + if str(path) != expected_path or sha256(path.read_bytes()) != expected_hash: + raise ValueError(f"Required runtime pin/path mismatch: {name}") + raw = vars(module)["__file__"] + spec = vars(module).get("__spec__") + # Keep canonical ordinary file records small; adapted installed modules carry + # raw __file__, resolved origin, loader and link-chain evidence explicitly. + if (raw == str(path) and (spec is None or spec.origin == raw) + and not path.is_relative_to(SIM_ROOT)): + return {"path": str(path), "sha256": sha256(path.read_bytes())} + record = {"kind": "installed-file", **_runtime_path(Path(raw)), + "spec_name": spec.name, "spec_origin": spec.origin, + "loader_type": _object_type(spec.loader)} + if spec.name != name: + record["wrapper"] = _pxr_wrapper(name, module, path) + else: + record["origin"] = _runtime_path(Path(spec.origin)) + return record + + +def _provider_namespace_identity(name: str, module: ModuleType) -> dict: + # Root/child namespace containers only, never an exemption for providers. + # Every container independently proves importer, location and child evidence. + parts = name.split(".") + if parts[0] not in {"omni", "usdrt"} or not all(p.isidentifier() for p in parts): + raise ValueError(f"Unreviewed fileless provider: {name}") + spec, loader, locations = (vars(module).get(k) for k in ("__spec__", "__loader__", "__path__")) + if (type(spec) is not ModuleSpec or spec.name != name or spec.origin is not None + or spec.has_location or type(loader) is not NamespaceLoader or spec.loader is not loader + or spec.submodule_search_locations is not locations or loader._path is not locations + or type(locations) is not _NamespacePath or getattr(locations, "_name", None) != name + or vars(module).get("__package__") != name): + raise ValueError(f"Unbound provider namespace spec: {name}") + importer = _namespace_importer(locations) + if any(type(p) is not str or p != str(Path(p)) for p in locations): + raise ValueError(f"Noncanonical provider search spelling: {name}") + paths = [_runtime_path(Path(p), directory=True) for p in locations] + if not paths or any(not Path(p["path"]).is_relative_to(SIM_ROOT) + or Path(p["path"]).parts[-len(parts):] != tuple(parts) for p in paths): + raise ValueError(f"Foreign provider namespace: {name}") + children, manifests, kernel = {}, {}, {} + for child, value in tuple(sys.modules.items()): + if child.startswith(name + ".") and isinstance(value, ModuleType) and vars(value).get("__file__"): + path = _module_file(child) + if not any(path.is_relative_to(p["path"]) for p in paths): + raise ValueError(f"Provider child outside search locations: {child}") + children[child] = _dependency_module_identity(child) + manifest = next((p / "config/extension.toml" for p in path.parents + if p.is_relative_to(SIM_ROOT) and (p / "config/extension.toml").is_file()), None) + if manifest is not None: + manifests[str(manifest)] = _runtime_path(manifest) + elif path.is_relative_to(SIM_ROOT / "kit/kernel/py"): + kernel = _runtime_path(SIM_ROOT / "kit/kernel/config/kit-core.json") + else: + raise ValueError(f"Provider child without extension manifest: {child}") + if not children or not (manifests or kernel): + raise ValueError(f"Provider has no reachable source/native extension evidence: {name}") + return {"kind": "kit-provider-namespace", "observation": module_diagnostic(name), + "importer": importer, "search_locations": paths, "imported_child_files": children, + "extension_manifests": manifests, "kernel_configuration": kernel, + "implementation_identity": "unobserved dynamic-provider internals"} + + +def _module_file(name: str) -> Path: + module = sys.modules.get(name) + if module is None or getattr(module, "__name__", None) != name: + raise ValueError(f"Missing or aliased imported module: {name}") + filename = getattr(module, "__file__", None) + if not isinstance(filename, str) or not filename: + raise ValueError(f"Imported module has no real __file__: {name}") + if filename != str(Path(filename)): + raise ValueError(f"Noncanonical imported __file__ spelling: {name}") + path = Path(_runtime_path(Path(filename), content_hash=False)["path"]) + spec = getattr(module, "__spec__", None) + if name != "__main__": + if spec is not None and spec.name != name and name.startswith("pxr."): + _pxr_wrapper(name, module, path) + elif (spec is None or spec.name != name or not isinstance(spec.origin, str) + or spec.origin != str(Path(spec.origin)) or not Path(spec.origin).is_file() + or Path(_runtime_path(Path(spec.origin), content_hash=False)["path"]) != path): + raise ValueError(f"Imported module spec/path mismatch: {name}") + return path + + +def _namespace_identity(name: str, root: Path) -> dict: + """Recognize only import-machinery namespaces in explicitly allowed source roots. + + This binds search locations, not every file in the tree. _imports attaches + the separately validated, actually imported descendant file identities. + External Kit provider namespaces use a separate, explicitly limited variant. + """ + module = sys.modules.get(name) + if type(module) is not ModuleType or vars(module).get("__name__") != name: + raise ValueError(f"Missing or synthetic namespace module: {name}") + spec = vars(module).get("__spec__") + loader = vars(module).get("__loader__") + locations = vars(module).get("__path__") + if (type(spec) is not ModuleSpec or spec.name != name or spec.origin is not None + or spec.has_location or type(loader) is not NamespaceLoader + or spec.loader is not loader or spec.submodule_search_locations is not locations + or getattr(loader, "_path", None) is not locations + or vars(module).get("__file__") is not None + or vars(module).get("__package__") != name): + raise ValueError(f"Invalid namespace spec/loader/path: {name}") + parts = name.split(".") + allowed = {"SkillFactory": root / PACKAGE, + "isaaclab": ISAAC_ROOT / "source/isaaclab/isaaclab", + "isaaclab_physx": ISAAC_ROOT / "source/isaaclab_physx/isaaclab_physx"} + if parts[0] not in allowed or not all(p.isidentifier() for p in parts): + raise ValueError(f"Namespace has no approved source root: {name}") + expected = allowed[parts[0]].joinpath(*parts[1:]) + if (not expected.is_absolute() or not expected.is_dir() + or expected != expected.resolve(strict=True) + or any(p.is_symlink() for p in (expected, *expected.parents))): + raise ValueError(f"Noncanonical namespace source directory: {name}") + # Rediscover without executing code, against the canonical parent rather + # than a caller-controlled sys.path or already replaced package __path__. + if "." in name: + parent = sys.modules.get(name.rpartition(".")[0]) + if (not isinstance(parent, ModuleType) + or list(vars(parent).get("__path__", ())) != [str(expected.parent)]): + raise ValueError(f"Replaced namespace parent search locations: {name}") + discovered = PathFinder.find_spec(name, [str(expected.parent)]) + if (discovered is None or discovered.name != name or discovered.origin is not None + or discovered.loader is not None or discovered.has_location + or type(locations) is not type(discovered.submodule_search_locations) + or getattr(locations, "_name", None) != name + or not list(locations) or set(locations) != {str(expected)} + or list(discovered.submodule_search_locations) != [str(expected)]): + raise ValueError(f"Replaced or broken namespace search locations: {name}") + importer = _namespace_importer(locations) + extra = ({"importer": importer, "raw_search_locations": list(locations)} + if importer["kind"] == "kit-fastfinder" else {}) + return {**extra, "kind": "pep420-namespace", "spec_name": spec.name, "origin": None, + "loader": "importlib.machinery.NamespaceLoader", + "search_locations": sorted(set(locations)), "imported_child_files": {}} + + +def _symbol(value: Any, modules: set[str]) -> dict: + name, qualname = getattr(value, "__module__", None), getattr(value, "__qualname__", None) + if not name or not qualname or "<" in qualname: + raise ValueError("Config class/callable must be an importable named symbol") + owner = sys.modules.get(name) + target = owner + for part in qualname.split("."): + target = inspect.getattr_static(target, part, None) + if target is not value: + raise ValueError(f"Config symbol is not the actual imported object: {name}.{qualname}") + if inspect.isfunction(value) and value.__closure__: + return _clone_symbol(value, modules, {"module": name, "qualname": qualname}) + if name != "builtins": + path = _module_file(name) + source = inspect.getsourcefile(value) + if source is None or _file(Path(source)) != path: + raise ValueError(f"Config symbol source mismatch: {name}.{qualname}") + modules.add(name) + return {"module": name, "qualname": qualname} + + +def _source_code(function: Any, path: Path) -> dict: + """Compare code with its file without importing or executing that file.""" + code = function.__code__ + pending = [compile(path.read_bytes(), str(path), "exec", dont_inherit=True)] + while pending: + candidate = pending.pop() + if candidate == code: + return {"path": str(path), "qualname": code.co_qualname, "firstlineno": code.co_firstlineno} + pending.extend(c for c in candidate.co_consts if isinstance(c, CodeType)) + raise ValueError("Callable code does not match bound source") + + +def _clone_symbol(value: Any, modules: set[str], public: dict) -> dict: + name = "isaaclab.sim.utils.prims" + relative, digest = ISAAC_PINS[name] + path = _module_file(name) + if path != ISAAC_ROOT / relative or sha256(path.read_bytes()) != digest: + raise ValueError("Pinned clone decorator source mismatch") + clone = _imported_symbol(name, "clone") + if not inspect.isfunction(clone) or clone.__closure__: + raise ValueError("Unsupported clone decorator") + decorator = _symbol(clone, modules) + _source_code(clone, path) + expected = [c for c in clone.__code__.co_consts + if isinstance(c, CodeType) and c.co_name == "wrapper"] + wrapped = vars(value).get("__wrapped__") + if (len(expected) != 1 or value.__code__ is not expected[0] + or value.__globals__ is not clone.__globals__ + or value.__code__.co_freevars != ("func",) or len(value.__closure__) != 1 + or not inspect.isfunction(wrapped) or wrapped.__closure__ + or "__wrapped__" in vars(wrapped) + or value.__closure__[0].cell_contents is not wrapped + or (wrapped.__module__, wrapped.__qualname__) != (value.__module__, value.__qualname__) + or wrapped.__code__.co_qualname != wrapped.__qualname__): + raise ValueError("Unsupported/changed clone wrapper closure") + target_path = _module_file(wrapped.__module__) + if inspect.getsourcefile(wrapped) != str(target_path): + raise ValueError("Clone underlying callable source mismatch") + modules.add(wrapped.__module__) + return {**public, "clone_wrapper": {"decorator": decorator, + "code": _source_code(value, path), "freevars": ["func"], + "wrapped": {"module": wrapped.__module__, "qualname": wrapped.__qualname__, + "code": _source_code(wrapped, target_path)}}} + + +def _imported_symbol(module: str, name: str) -> Any: + owner = sys.modules.get(module) + value = vars(owner).get(name) if owner is not None else None + if value is None: + raise ValueError(f"Required already imported symbol: {module}.{name}") + return value + + +def _resolvable(value: Any) -> bool: + owner = sys.modules.get("isaaclab.utils.string") + return owner is not None and type(value) is vars(owner).get("ResolvableString") + + +def _reference_target(value: Any, modules: set[str]) -> Any: + # The pinned string_to_callable uses rsplit(':', 1) and one getattr, not a + # dotted-attribute walker. Static lookup avoids module __getattr__/properties. + reference = str(value) + if reference.count(":") != 1: + raise ValueError("Unsupported ResolvableString reference") + module, attribute = reference.split(":") + if not attribute.isidentifier() or not all(p.isidentifier() for p in module.split(".")): + raise ValueError("Unsupported ResolvableString reference") + target = _imported_symbol(module, attribute) + if not (inspect.isclass(target) or inspect.isfunction(target) or inspect.isbuiltin(target)): + raise ValueError("ResolvableString target must be a named class/function") + _symbol(target, modules) + modules.add(module) + _module_file(module) + if object.__getattribute__(value, "_resolve_error") is not None: + raise ValueError("ResolvableString cached resolution error") + cached = object.__getattribute__(value, "_resolved_callable") + if cached is not None and cached is not target: + raise ValueError("ResolvableString cached callable conflicts with reference") + return target + + +def _config_fields(value: Any) -> dict: + fields = {} + for name in value.__dataclass_fields__: + if not hasattr(value, name): + raise ValueError(f"Unstored dataclass config field: {name}") + fields[name] = getattr(value, name) + fields.update(vars(value) if hasattr(value, "__dict__") else {}) + for cls in type(value).__mro__: + slots = cls.__dict__.get("__slots__", ()) + for slot in (slots,) if isinstance(slots, str) else slots: + if slot not in ("__dict__", "__weakref__") and hasattr(value, slot): + fields[slot] = getattr(value, slot) + return fields + + +def _encode(value: Any, modules: set[str], active: set[int]) -> Any: + if isinstance(value, enum.Enum): + cls = type(value) + if value.name is None or cls.__members__.get(value.name) is not value: + raise ValueError("Unnamed/combined enum config member") + return {"enum": _symbol(cls, modules), "member": value.name, + "value": _encode(value.value, modules, active)} + if _resolvable(value): + target = _reference_target(value, modules) + return {"resolvable_string": _symbol(type(value), modules), "reference": str(value), + "target": _encode(target, modules, active), + "cached_callable": _encode(object.__getattribute__(value, "_resolved_callable"), modules, active), + "cached_error": None} + if value is None or type(value) in (str, bool, int): + return value + if type(value) is float: + if not math.isfinite(value): + raise ValueError("Nonfinite effective config") + return {"float": value.hex()} + if inspect.isclass(value) or inspect.isfunction(value) or inspect.isbuiltin(value): + return {"symbol": _symbol(value, modules)} + identity = id(value) + if identity in active: + raise ValueError("Cyclic effective config") + active.add(identity) + try: + def encode(item): + return _encode(item, modules, active) + + if type(value) in (dict, MappingProxyType): + if any(type(k) is not str for k in value): + raise ValueError("Effective config mapping keys must be strings") + return {"mapping": {k: encode(v) for k, v in value.items()}} + if type(value) in (tuple, list): + return {type(value).__name__: [encode(v) for v in value]} + if type(value) is slice: + return {"slice": [encode(value.start), encode(value.stop), encode(value.step)]} + if type(value) is functools.partial: + return {"partial": encode(value.func), "args": encode(value.args), + "keywords": encode(value.keywords)} + if type(value) in (Path, PosixPath, WindowsPath): + return {"path": str(value)} + if dataclasses.is_dataclass(value): + # fields() excludes ClassVars, but Isaac USD schema configs use + # mutable ClassVars to control physics-property serialization. + return {"dataclass": _symbol(type(value), modules), "fields": encode(_config_fields(value))} + raise ValueError(f"Unsupported effective config type: {type(value).__module__}.{type(value).__qualname__}") + finally: + active.remove(identity) + + +def canonical_config(config: Any) -> bytes: + """Lossless typed encoding; unsupported objects/callables fail, never repr().""" + return canonical_json(_encode(config, set(), set())) + + +# This is intentionally the bound task's homogeneous scene, not a general scene +# serializer. The pinned backend plans every env-scoped single spawner at env_0 +# before constructing entities, regardless of replicate_physics. +NORMALIZATION_SOURCES = ( + "isaaclab.utils.string", "isaaclab.utils.configclass", + "isaaclab.scene.interactive_scene", "isaaclab.scene.interactive_scene_cfg", + "isaaclab.sensors.joint_wrench.joint_wrench_sensor_cfg", "isaaclab.sensors.sensor_base_cfg", +) +SCENE_TYPES = { + "mount_wrench": ("isaaclab.sensors.joint_wrench.joint_wrench_sensor_cfg", "JointWrenchSensorCfg"), + "ground": ("isaaclab.assets.asset_base_cfg", "AssetBaseCfg"), + "light": ("isaaclab.assets.asset_base_cfg", "AssetBaseCfg"), + "robot": ("isaaclab.assets.articulation.articulation_cfg", "ArticulationCfg"), + "foot_contact": ("isaaclab.sensors.contact_sensor.contact_sensor_cfg", "ContactSensorCfg"), +} +SPAWNER_TYPES = { + "ground": ("isaaclab.sim.spawners.from_files.from_files_cfg", "UsdFileCfg"), + "robot": ("isaaclab.sim.spawners.from_files.from_files_cfg", "UsdFileCfg"), + "light": ("isaaclab.sim.spawners.lights.lights_cfg", "DomeLightCfg"), +} + + +def _normalization_updates(config: Any) -> list[tuple[Any, str, Any]]: + # Evaluation's original exact single-env contract is unchanged. + return _typed_normalization_updates(config, + 'SkillFactory.tasks.manager_based.contact_cycle_sim.evaluation_cfg', + 'EvaluationContactCycleEnvCfg', 1) + + +def _typed_normalization_updates(config: Any, module: str, class_name: str, + num_envs: int) -> list[tuple[Any, str, Any]]: + """Finite scene mechanics shared by separately bound, exact-type adapters.""" + if (module, class_name, num_envs) not in ( + ('SkillFactory.tasks.manager_based.contact_cycle_sim.evaluation_cfg', 'EvaluationContactCycleEnvCfg', 1), + ('SkillFactory.tasks.manager_based.contact_cycle_sim.rootxy_cfg_v2', 'RootXYContactCycleEnvCfg', 128), + ): + raise ValueError('Unsupported normalization contract') + for name in NORMALIZATION_SOURCES: + relative, digest = ISAAC_PINS[name] + path = _module_file(name) + if path != ISAAC_ROOT / relative or sha256(path.read_bytes()) != digest: + raise ValueError(f"Installed normalization source mismatch: {name}") + cls = _imported_symbol(module, class_name) + if type(config) is not cls: + raise ValueError('Normalizer requires actual ' + class_name) + scene = config.scene + base = _imported_symbol("isaaclab.scene.interactive_scene_cfg", "InteractiveSceneCfg") + if not isinstance(scene, base) or not dataclasses.is_dataclass(scene): + raise ValueError("Unsupported scene config type") + if (type(scene.num_envs) is not int or scene.num_envs != num_envs + or type(scene.replicate_physics) is not bool or scene.clone_in_fabric is not False): + raise ValueError('Only single-env, non-fabric scene normalization is supported' if num_envs == 1 + else 'Only exact128-env, non-fabric RootXY normalization is supported') + entities = {k: v for k, v in vars(scene).items() if k not in base.__dataclass_fields__} + if set(entities) != set(SCENE_TYPES): + raise ValueError("Unsupported scene entities") + updates = [] + + def update(obj, field, desired): + current = getattr(obj, field) + if current != desired: + updates.append((obj, field, desired)) + + regex = "/World/envs/env_.*" + + def path(value): + if type(value) is not str: + raise ValueError("Scene paths must be plain strings") + try: + result = value.format(ENV_REGEX_NS=regex) + except (KeyError, IndexError, ValueError) as error: + raise ValueError("Unsupported scene path template") from error + if (not result.startswith("/World/") or "{" in result or "}" in result + or ".." in result.split("/") or "//" in result): + raise ValueError("Unsupported scene namespace/path") + if "/World/envs" in result and not result.startswith(regex + "/"): + raise ValueError("Unsupported environment namespace") + return result + + for name, expected in SCENE_TYPES.items(): + entity = entities[name] + if type(entity) is not _imported_symbol(*expected): + raise ValueError(f"Unsupported scene entity type: {name}") + prim = path(entity.prim_path) + if name in ("ground", "light") and prim != f"/World/{name}": + raise ValueError(f"Unsupported global entity path: {name}") + if name == "robot" and prim != regex + "/Robot": + raise ValueError("Unsupported robot prim_path") + if name == "foot_contact" and not prim.startswith(regex + "/Robot/"): + raise ValueError("Unsupported contact sensor prim_path") + if name == "mount_wrench" and ( + prim != regex + "/Robot" or type(entity.update_period) is not float + or entity.update_period != 0.0 or entity.debug_vis is not False + or entity.convention != "incoming_joint_frame" + or not (type(entity.class_type) is str or _resolvable(entity.class_type)) + or str(entity.class_type) != ( + "isaaclab.sensors.joint_wrench.joint_wrench_sensor:JointWrenchSensor") + or set(_config_fields(entity)) != { + "class_type", "prim_path", "update_period", "debug_vis", "convention"}): + raise ValueError("Unsupported inherited mount_wrench config") + update(entity, "prim_path", prim) + if name in SPAWNER_TYPES: + spawn = entity.spawn + if type(spawn) is not _imported_symbol(*SPAWNER_TYPES[name]): + raise ValueError(f"Unsupported single spawner: {name}") + desired = prim.replace(regex, "/World/envs/env_0") + if spawn.spawn_path not in (None, desired): + raise ValueError("Conflicting preconstruction spawn_path") + update(spawn, "spawn_path", desired) + elif name == "foot_contact": + for field in ("filter_prim_paths_expr", "sensor_shape_prim_expr", "filter_shape_prim_expr"): + if hasattr(entity, field): + values = getattr(entity, field) + if field != "filter_prim_paths_expr" and not values: + continue # same conditional as pinned ContactSensor branch + if type(values) is not list: + raise ValueError(f"Unsupported contact path list: {field}") + update(entity, field, [path(p) for p in values]) + + updates.extend(_reference_updates(config)) + canonical_config(config) # reject unsupported values before applying changes + return updates + + +def _reference_updates(config: Any) -> list[tuple[Any, str, Any]]: + updates = [] + # Walk only types accepted by the encoder; never getattr on arbitrary objects + # or invoke a ResolvableString. Error caches and conflicting caches fail closed. + seen = set() + + def references(value): + if id(value) in seen: + return + seen.add(id(value)) + if _resolvable(value): + target = _reference_target(value, set()) + if object.__getattribute__(value, "_resolved_callable") is None: + updates.append((value, "_resolved_callable", target)) + elif not inspect.isclass(value) and dataclasses.is_dataclass(value): + for child in _config_fields(value).values(): + references(child) + elif type(value) in (dict, MappingProxyType): + for child in value.values(): + references(child) + elif type(value) in (tuple, list): + for child in value: + references(child) + elif type(value) is functools.partial: + references(value.func) + references(value.args) + references(value.keywords) + + references(config) + return updates + + +def normalize_config(config: Any) -> None: + """PREconstruction only: normalize the actual config in place, idempotently. + + No imports, callable execution, environment, or physics. Run once before + preparation/construction; never use this to repair a postconstruction mismatch. + Verification only checks the fixed point and never calls this mutator. + """ + for obj, field, value in _normalization_updates(config): + setattr(obj, field, value) + + +def _sources(root: Path, future_sources: Mapping[str, tuple[str, ...]], + entrypoints: tuple[str, ...]) -> tuple[dict, set[str]]: + if not isinstance(future_sources, Mapping) or set(future_sources) != {"runner", "instrumentation"}: + raise ValueError("Required explicit runner AND instrumentation source lists") + roles = {"evaluation": FIXED_SOURCES} + roles.update(future_sources) + all_paths = [] + for role, paths in roles.items(): + if type(paths) not in (list, tuple) or not paths or any(type(p) is not str for p in paths): + raise ValueError(f"Missing explicit source paths for {role}") + if role in ("runner", "instrumentation") and not any(p.endswith(".py") for p in paths): + raise ValueError(f"Required executable Python source for {role}") + all_paths.extend(paths) + if len(set(all_paths)) != len(all_paths): + raise ValueError("Duplicate/ambiguous source roles") + if (type(entrypoints) is not tuple or len(set(entrypoints)) != len(entrypoints) + or any(p not in all_paths or not p.endswith(".py") for p in entrypoints)): + raise ValueError("Entrypoints must be explicit unique bound Python paths") + imports, result = set(), {} + for role, paths in roles.items(): + result[role] = [] + for relative in paths: + path = _relative(root, relative) + record = {"path": str(path), "sha256": sha256(path.read_bytes())} + if relative in SOURCE_ONLY: + record["source_only_not_runtime"] = True + elif path.suffix == ".py": + if relative.startswith(PACKAGE): + name = relative.removeprefix("source/SkillFactory/").removesuffix(".py").replace("/", ".") + name = name.removesuffix(".__init__") + if _module_file(name) != path: + raise ValueError(f"Imported evaluation source mismatch: {name}") + imports.add(name) + record["module"] = name + elif relative == "scripts/audit_cycle_sim_evaluation.py" or relative in entrypoints: + record["entrypoint"] = True + if relative in entrypoints: + # The running CLI is __main__, not a forged package module. + if _module_file("__main__") != path: + raise ValueError("Actual __main__ entrypoint mismatch") + imports.add("__main__") + else: + matches = [n for n, m in tuple(sys.modules.items()) + if getattr(m, "__file__", None) == str(path)] + if len(matches) != 1 or _module_file(matches[0]) != path: + raise ValueError(f"Required source not uniquely imported: {relative}") + imports.add(matches[0]) + record["module"] = matches[0] + result[role].append(record) + return result, imports + + +def _imports(names: set[str], root: Path, *, diagnostics: list | None = None) -> dict: + # Include real imported package dependencies, not a caller-selected directory + # of lookalike source. Also catches late imports at post-cleanup verification. + names.update(n for n in tuple(sys.modules) + if n.split(".")[0] in ("SkillFactory", "isaaclab", "isaaclab_physx")) + names.update(ISAAC_PINS) + result, seen, namespaces = {}, {}, {} + + def invalid(name, error): + if diagnostics is None: + raise error + diagnostics.append({"stage": "source-import", "entry": name, "error": str(error), + "module_observation": module_diagnostic(name)}) + + for name in sorted(names): + try: + module = sys.modules.get(name) + if (name not in ISAAC_PINS and isinstance(module, ModuleType) + and vars(module).get("__file__") is None): + result[name] = _namespace_identity(name, root) + namespaces[name] = module + continue + path = _module_file(name) + if name in ISAAC_PINS: + relative, expected = ISAAC_PINS[name] + if path != ISAAC_ROOT / relative or sha256(path.read_bytes()) != expected: + raise ValueError(f"Installed Isaac pin/path mismatch: {name}") + if name.split(".")[0] in ("isaaclab", "isaaclab_physx") and not path.is_relative_to(ISAAC_ROOT): + raise ValueError(f"Alternative Isaac tree forbidden: {name}") + if name.split(".")[0] == "SkillFactory" and not path.is_relative_to(root / PACKAGE): + raise ValueError(f"Alternative task tree forbidden: {name}") + if path in seen: + raise ValueError(f"Imported-path alias: {seen[path]} / {name}") + seen[path] = name + result[name] = _dependency_module_identity(name) + except (ValueError, OSError, ImportError, KeyError) as error: + invalid(name, error) + # Aliases outside the selected namespaces must not hide substituted imports. + for name, module in tuple(sys.modules.items()): + filename = getattr(module, "__file__", None) + if isinstance(filename, str) and Path(filename).resolve() in seen and name not in result: + if (name == "__mp_main__" and module is sys.modules.get("__main__") + and seen[Path(filename).resolve()] == "__main__"): + result["__main__"]["standard_aliases"] = { + name: {"kind": "standard-multiprocessing-alias", "same_module_object": True}} + else: + invalid(name, ValueError(f"Unbound imported-path alias: {name}")) + if name not in namespaces and any(module is m for m in namespaces.values()): + invalid(name, ValueError(f"Unbound namespace alias: {name}")) + elif isinstance(module, ModuleType) and name not in namespaces: + locations = vars(module).get("__path__") + for namespace in namespaces: + ns_locations = vars(namespaces[namespace])["__path__"] + if (type(locations) in (list, tuple, type(ns_locations)) + and set(locations) & set(result[namespace]["search_locations"])): + invalid(name, ValueError(f"Unbound namespace search-path alias: {name}")) + for name in namespaces: + children = {} + for child, record in result.items(): + if child.startswith(name + ".") and "path" in record: + if not any(Path(record["path"]).is_relative_to(location) + for location in result[name]["search_locations"]): + invalid(child, ValueError(f"Namespace child outside search locations: {child}")) + else: + children[child] = record + result[name]["imported_child_files"] = children + return result + + +NUMERICAL_ROOTS = frozenset({"torch", "numpy", "warp"}) +INVENTORY_SCOPE = "development-finite-source-role-provenance-v3" +# Installed torch/__init__.py CUDA preload inventory; module owner denotes the +# consuming implementation, not NVIDIA library authorship. +NVIDIA_CUDA_PACKAGES = ( + "cublas", "cudnn", "cuda_nvrtc", "cuda_runtime", "cuda_cupti", "cufft", "curand", + "nvjitlink", "cusparse", "cusparselt", "cusolver", "nccl", "nvtx", "cufile", +) + + +def required_dependencies(source_names: set[str], role_names: set[str]) -> set[str]: + """Exact declared roles plus actual config references; no recursive metadata walk.""" + required = set(REQUIRED_RUNTIME) + required.update(n for n in source_names - role_names if n.split(".")[0] not in ( + sys.stdlib_module_names | {"SkillFactory", "isaaclab", "isaaclab_physx"})) + if "omni.replicator.core" in sys.modules: + required.add("omni.replicator.core") # ManagerBasedEnv.seed's optional provider + return required + + +def incidental_observations(required: set[str]) -> dict: + """Nonblocking raw evidence, never a verified dependency or import instruction.""" + result = {} + for name, module in tuple(sys.modules.items()): + if name in required or not isinstance(module, ModuleType): + continue + row = {"status": "observational-not-verified", "raw": module_diagnostic(name), + "limitation": "unverified-dynamic-internals"} + filename = vars(module).get("__file__") + if isinstance(filename, str): + try: + row["observed_file"] = _runtime_path(Path(filename)) + for owner, (_, owner_path, _) in REQUIRED_RUNTIME.items(): + if (name.startswith(owner + '.') and row['observed_file']['path'] == owner_path + and '.so' in Path(owner_path).suffixes): + row['observed_binary_owner'] = owner + row['owner_scope'] = 'shared native file; not an independently verified pseudo-module' + except (ValueError, OSError) as error: + row["observation_error"] = str(error) + result[name] = row + return result + + +def _dependency_inventory(inventory: Mapping, source_names: set[str], role_names: set[str]) -> dict: + """Check caller-reviewed expected identities, not a self-selected hash scan. + + Native files are selected by exact installed layout below; every one must + already have an explicit reviewed entry. This does not attest loaded linker + resolution, system/CUDA drivers, JIT caches, or a complete distribution. + """ + if (not isinstance(inventory, Mapping) or set(inventory) != {"scope", "modules", "artifacts"} + or inventory["scope"] != INVENTORY_SCOPE + or not isinstance(inventory["modules"], Mapping) or not inventory["modules"] + or not isinstance(inventory["artifacts"], Mapping) or not inventory["artifacts"]): + raise ValueError("Required explicit reviewed dependency inventory (modules AND artifacts)") + for filename, expected in RUNTIME_LAYOUT_PINS.items(): + if sha256(_file(Path(filename)).read_bytes()) != expected: + raise ValueError(f"Pinned runtime layout mismatch: {filename}") + modules, artifacts = inventory["modules"], inventory["artifacts"] + required = required_dependencies(source_names, role_names) + # Only explicitly bound source roles: incidental third-party internals are + # not traversed, but substituting a required import at its use site is fatal. + for name in source_names | role_names: + module = sys.modules.get(name) + if isinstance(module, ModuleType): + for value in vars(module).values(): + if isinstance(value, ModuleType) and vars(value).get('__name__') in required: + if sys.modules.get(vars(value)['__name__']) is not value: + raise ValueError('Substituted imported dependency object: ' + vars(value)['__name__']) + if not required <= set(modules): + raise ValueError(f"Missing reviewed dependency modules: {sorted(required - set(modules))}") + def record(path, expected): + if (not isinstance(expected, Mapping) or set(expected) != {"path", "sha256"} + or type(expected["path"]) is not str or expected["path"] != str(path) + or expected["sha256"] != sha256(path.read_bytes())): + raise ValueError(f"Reviewed dependency path/hash mismatch: {path}") + return dict(expected) + + if set(modules) != required: + raise ValueError("Dependency inventory exceeds declared finite source roles") + result_modules = {} + for name, expected in sorted(modules.items()): + if type(name) is not str: + raise ValueError("Dependency module names must be strings") + actual = _dependency_module_identity(name) + if actual != expected: + raise ValueError(f"Reviewed dependency path/hash/provenance mismatch: {name}") + result_modules[name] = actual + artifact_paths = set() + result_artifacts = {} + for label, expected in sorted(artifacts.items()): + if (type(label) is not str or not label or not isinstance(expected, Mapping) + or set(expected) not in ({"module", "path", "sha256", "purpose"}, + {"module", "path", "sha256", "purpose", "vendor"}) + or ("vendor" in expected and expected["vendor"] != "NVIDIA") + or expected["module"] not in modules + or type(expected["purpose"]) is not str or not expected["purpose"] + or type(expected["path"]) is not str): + raise ValueError("Artifact requires explicit owner module, path, hash and purpose") + path = _file(Path(expected["path"])) + record(path, {k: expected[k] for k in ("path", "sha256")}) + if path in artifact_paths: + raise ValueError("Duplicate dependency artifact path") + artifact_paths.add(path) + result_artifacts[label] = dict(expected) + + # Source-proven installed Linux layouts. Refuse other layouts rather than + # mistaking __init__.py for a numerical implementation. Extra provider, + # compiled-kernel, library and evidence-scope files can always be explicit. + roots = {n: _module_file(n).parent for n in NUMERICAL_ROOTS} + native = {} + layouts = { + "torch": (roots["torch"] / "lib",), + "numpy": (roots["numpy"] / ("_core" if (roots["numpy"] / "_core").is_dir() else "core"), + roots["numpy"] / "random", roots["numpy"] / "linalg", + roots["numpy"] / "fft", roots["numpy"].parent / "numpy.libs"), + "warp": (roots["warp"] / "bin",), + } + for owner, directories in layouts.items(): + for directory in directories: + paths = list(directory.glob("*.so*")) + if not paths: + raise ValueError(f"Unsupported/missing numerical native layout: {directory}") + for path in paths: + native[_file(path)] = owner + vendor_paths = set() + if (roots["torch"] / "lib/libtorch_cuda.so").is_file(): + vendor_root = roots["torch"].parent / "nvidia" + for package in NVIDIA_CUDA_PACKAGES: + directory = vendor_root / package / "lib" + paths = list(directory.glob("*.so*")) + if package in ("cuda_runtime", "cublas", "cudnn") and not paths: + raise ValueError(f"Unsupported/missing NVIDIA CUDA native layout: {directory}") + for path in paths: + canonical = _file(path) + native[canonical] = "torch" + vendor_paths.add(canonical) + for path, owner in native.items(): + if not any(a["path"] == str(path) and a["module"] == owner + and (path not in vendor_paths or a.get("vendor") == "NVIDIA") + for a in artifacts.values()): + raise ValueError(f"Missing reviewed numerical native artifact: {path}") + return {"scope": INVENTORY_SCOPE, "modules": result_modules, "artifacts": result_artifacts} + + +def _snapshot(root: Path, *, protocol_path: str, effective_config: Any, trial_index: int, + future_sources: Mapping[str, tuple[str, ...]], declared_overrides: Mapping[str, str], + entrypoints: tuple[str, ...], quaternion_convention: str, dependency_inventory: Mapping) -> dict: + root = Path(root) + if not root.is_absolute() or root != root.resolve(strict=True): + raise ValueError("Canonical absolute project root required") + if quaternion_convention != "xyzw": + raise ValueError("Only pinned PhysX xyzw is supported; no implicit quaternion adapter") + if dict(declared_overrides) != dict(SEED_OVERRIDE): + raise ValueError("Only explicit protocol.trial.seed override is permitted") + if protocol_path not in ( + "configs/contact_cycle/continuous_eval_p1_v1.json", + "configs/contact_cycle/bounded_diagnostic_p2_v1.json", + "configs/contact_cycle/bounded_diagnostic_p2_extended_v1.json"): + raise ValueError("Protocol must be one of the bound preregistrations") + protocol_file = _relative(root, protocol_path) + protocol = load_protocol(protocol_file) + trial = protocol.trial(trial_index) + cfg_module = sys.modules.get("SkillFactory.tasks.manager_based.contact_cycle_sim.evaluation_cfg") + cfg_class = getattr(cfg_module, "EvaluationContactCycleEnvCfg", None) + if (cfg_class is None or type(effective_config) is not cfg_class + or not dataclasses.is_dataclass(effective_config) + or type(getattr(effective_config, "seed", None)) is not int + or effective_config.seed != trial.seed): + raise ValueError("Effective config must have selected protocol trial seed") + if _normalization_updates(effective_config): + raise ValueError("Effective config requires explicit preconstruction normalize_config") + from . import replay + + # No injected verifier, new exceptions, policy loading, or optimizer state. + frozen_manifest, frozen = replay.verify_bundle(root) + sources, names = _sources(root, future_sources, entrypoints) + role_names = set(names) + encoded = _encode(effective_config, names, set()) + encoded["fields"]["mapping"]["seed"] = {"override": "protocol.trial.seed"} + imports = _imports(names, root) + dependencies = _dependency_inventory(dependency_inventory, set(imports), role_names) + return { + "format": FORMAT, "evaluation_id": EVALUATION_ID, + "evidence_scope": PROVENANCE_SCOPE, "limitations": PROVENANCE_LIMITATIONS, + "execution_authorized": False, "accepted_skill": False, "optimizer_steps": 0, + "root": str(root), "sources": sources, "imports": imports, + "dependency_inventory": dependencies, + "required_runtime_roles": dict(REQUIRED_RUNTIME), + "runtime_layout_pins": dict(RUNTIME_LAYOUT_PINS), + "quaternion_convention": quaternion_convention, + "protocol": {"path": str(protocol_file), "sha256": sha256(protocol_file.read_bytes()), + "canonical_sha256": sha256(canonical_json(_plain(protocol.document))), + "trials": [dataclasses.asdict(t) for t in protocol.trials]}, + "effective_config": encoded, "declared_overrides": dict(SEED_OVERRIDE), + "trial_seeds": {str(t.index): t.seed for t in protocol.trials}, + "frozen_policy": {"task_id": frozen_manifest["task_id"], + "contract": frozen_manifest["contract"], + "features": frozen_manifest["features"], **frozen}, + } + + +def _plain(value: Any) -> Any: + if isinstance(value, Mapping): + return {k: _plain(v) for k, v in value.items()} + if isinstance(value, (tuple, list)): + return [_plain(v) for v in value] + return value + + +def prepare_binding(root: Path, *, protocol_path: str, effective_config: Any, trial_index: int, + future_sources: Mapping[str, tuple[str, ...]], declared_overrides: Mapping[str, str], + dependency_inventory: Mapping, + entrypoints: tuple[str, ...] = (), quaternion_convention: str = "xyzw") -> bytes: + """Snapshot imported sources/config, NOT approval; all future files must exist. + + Normalize the actual config before construction and set cfg.seed to the + selected trial seed. Supply an explicit reviewed dependency inventory. Other + fields are bound exactly. No files are written and no environment is created. + The separately reviewed digest must never be obtained by replay preparation. + """ + arguments = dict(protocol_path=protocol_path, effective_config=effective_config, + trial_index=trial_index, future_sources=future_sources, + declared_overrides=declared_overrides, entrypoints=entrypoints, + quaternion_convention=quaternion_convention, dependency_inventory=dependency_inventory) + # Detect ordinary mutation during collection, including caller-owned config + # and source inputs. This is not an adversarial process/memory sandbox. + first = canonical_json(_snapshot(root, **arguments)) + if canonical_json(_snapshot(root, **arguments)) != first: + raise ValueError("Binding inputs changed during snapshot") + return first + + +MISMATCH_MAX_DIFFERENCES = 64 +MISMATCH_MAX_BYTES = 32768 + + +def binding_difference(expected: bytes, actual: bytes) -> dict: + """Bounded canonical JSON comparison; paths are RFC 6901 JSON Pointers. + + Only binding documents are inspected, never environment variables or object + reprs. Large values/paths retain exact byte hashes instead of truncated text. + Full document hashes and omitted counts keep truncation explicit. + """ + report = { + "format": "skillfactory.evaluation-binding-difference.v1", + "path_format": "RFC6901", "expected_sha256": sha256(expected), + "actual_sha256": sha256(actual), "differences": [], + "difference_count": 0, "omitted_difference_count": 0, + "max_differences": MISMATCH_MAX_DIFFERENCES, "max_bytes": MISMATCH_MAX_BYTES, + } + missing = object() + + def summary(value): + if value is missing: + return {"present": False} + payload = canonical_json(value) + result = {"present": True, "canonical_bytes": len(payload), "sha256": sha256(payload)} + if len(payload) <= 256: + result["value"] = value + return result + + def visit(left, right, path): + if type(left) not in (dict, list) and type(left) is type(right) and left == right: + return + if type(left) is dict and type(right) is dict: + for key in sorted(left.keys() | right.keys()): + visit(left.get(key, missing), right.get(key, missing), + path + "/" + key.replace("~", "~0").replace("/", "~1")) + return + if type(left) is list and type(right) is list: + for index in range(max(len(left), len(right))): + visit(left[index] if index < len(left) else missing, + right[index] if index < len(right) else missing, path + "/" + str(index)) + return + report["difference_count"] += 1 + if len(report["differences"]) >= MISMATCH_MAX_DIFFERENCES: + report["omitted_difference_count"] += 1 + return + entry = {"expected": summary(left), "actual": summary(right)} + if len(canonical_json(path)) <= 1024: + entry["path"] = path + else: + entry["path_sha256"] = sha256(path.encode("utf-8")) + entry["path_utf8_bytes"] = len(path.encode("utf-8")) + # Reserve space for counters growing as the remaining tree is visited. + if len(canonical_json(report)) + len(canonical_json(entry)) + 128 > MISMATCH_MAX_BYTES: + report["omitted_difference_count"] += 1 + else: + report["differences"].append(entry) + + visit(json.loads(expected), json.loads(actual), "") + return report + + +class BindingMismatchError(ValueError): + """Fatal identity error with bounded evidence safe for durable failure records.""" + def __init__(self, expected: bytes, actual: bytes): + super().__init__("Evaluation binding mismatch") + self.binding_diagnostic = binding_difference(expected, actual) + + +def verify_binding(manifest: bytes, *, trusted_manifest_sha256: str, root: Path, + protocol_path: str, effective_config: Any, trial_index: int, + future_sources: Mapping[str, tuple[str, ...]], declared_overrides: Mapping[str, str], + dependency_inventory: Mapping, + entrypoints: tuple[str, ...] = (), quaternion_convention: str = "xyzw") -> str: + """Verify before construction, each trial transition, and after cleanup. + + The digest must come from independent preparation/review, not this replay. + Use unchanged replay.load_frozen_policy only after this succeeds. Returning a + digest is identity evidence, never a physics/training authorization. + """ + if type(manifest) is not bytes: + raise ValueError("Manifest must be immutable exact JSON bytes") + if (type(trusted_manifest_sha256) is not str or len(trusted_manifest_sha256) != 64 + or any(c not in "0123456789abcdef" for c in trusted_manifest_sha256) + or sha256(manifest) != trusted_manifest_sha256): + raise ValueError("Separately trusted manifest SHA256 mismatch") + document = json.loads(manifest) + if canonical_json(document) != manifest: + raise ValueError("Manifest is not exact canonical JSON") + actual = prepare_binding(root, protocol_path=protocol_path, effective_config=effective_config, + trial_index=trial_index, future_sources=future_sources, + declared_overrides=declared_overrides, entrypoints=entrypoints, + quaternion_convention=quaternion_convention, dependency_inventory=dependency_inventory) + if actual != manifest: + raise BindingMismatchError(manifest, actual) + return trusted_manifest_sha256 diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_budget.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_budget.py new file mode 100644 index 0000000..1090c23 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_budget.py @@ -0,0 +1,288 @@ +"""Linux same-boot, single-writer evaluation reservations. No simulator imports. + +Reservations are debits, NOT observations of executed physics. No refund, resume, +retry, transfer or new boot. The parent-reviewed grant identifies this campaign. +""" +from __future__ import annotations + +import fcntl +import hashlib +import json +import os +import stat +import time +from pathlib import Path + +CAP_BYTES = 1 << 30 +HEADROOM_BYTES = 16 << 20 +STDOUT_BYTES = 1 << 20 +STDOUT_ALLOWANCE = 3 * STDOUT_BYTES +STORAGE_SCOPE = "validation-outputs-only.native-monitored-stop.v1" +CLEANUP_SECONDS = 120 +TOTAL_SECONDS = 3600 +SLOTS = ((14500, 13500, 600), (14500, 13500, 600), (91000, 90000, 2100)) +FORMAT = 'skillfactory.evaluation-budget.v2' +EXTENDED_PROFILE = 'p2-wall10800-aggregate14400-v1' +EXTENDED_FORMAT = 'skillfactory.evaluation-budget.p2-extended.v1' +EXTENDED_GRANT_FORMAT = 'skillfactory.evaluation-parent-grant.p2-extended.v1' +EXTENDED_POLICY_FORMAT = 'skillfactory.output-storage-policy.p2-extended.v1' +EXTENDED_TOTAL_SECONDS = 14400 +EXTENDED_SLOTS = ((91000, 90000, 10800),) +EXTENDED_ROOT = Path('/home/cen/sf-runs/p2-01') +SECOND_EXTENDED_ROOT = Path('/home/cen/sf-runs/p2-02') + + +def extended_historical_calls(root): + """Finite campaigns only; second campaign retains the failed 91000 debit.""" + if Path(root) == EXTENDED_ROOT: + return 72500 + if Path(root) == SECOND_EXTENDED_ROOT: + return 163500 + raise ValueError('budget_extended_fixed_root_required') + + +def profile_limits(profile): + if profile is None: + return SLOTS, TOTAL_SECONDS, FORMAT + if profile == EXTENDED_PROFILE: + return EXTENDED_SLOTS, EXTENDED_TOTAL_SECONDS, EXTENDED_FORMAT + raise ValueError('budget_unknown_resource_profile') + + +def boottime(): + return time.clock_gettime(time.CLOCK_BOOTTIME) + + +def boot_id(): + return Path('/proc/sys/kernel/random/boot_id').read_text().strip() + + +def digest(path): + h = hashlib.sha256() + with Path(path).open('rb') as stream: + for block in iter(lambda: stream.read(1 << 20), b''): + h.update(block) + return h.hexdigest() + + +def sync_directory(path): + fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY) + try: + os.fsync(fd) + finally: + os.close(fd) + + +def durable_directory(path): + path = Path(path) + path.mkdir() # ancestors must already be durable; never recursively invent them + sync_directory(path) + sync_directory(path.parent) + + +def encode(value): + return (json.dumps(value, sort_keys=True, separators=(',', ':'), allow_nan=False) + '\n').encode() + + +def durable_write(path, data): + path = Path(path) + with path.open('xb', buffering=0) as stream: + if stream.write(data) != len(data): + raise OSError('Short durable write') + os.fsync(stream.fileno()) + sync_directory(path.parent) + + +def artifact_size(root, *, exclude_native=False): + root = Path(root) + if root != root.resolve(strict=True) or not root.is_dir(): + raise RuntimeError('storage_artifact_root_moved_or_linked') + def walk_error(error): + raise error # never silently skip inaccessible owned directories + + # Atomic writer publication can rename a listed .pending file before lstat. + # Restart the WHOLE scan (not skip the entry or return a partial total), since + # the destination may already have been counted at its previous smaller size. + # Persistent churn fails closed; this remains sampled accounting, not a quota. + for attempt in range(4): + total = 0 + try: + for directory, dirs, files in os.walk(root, followlinks=False, onerror=walk_error): + if exclude_native and Path(directory) == root and "native" in dirs: + dirs.remove("native") + for name in dirs + files: + p = Path(directory) / name + info = p.lstat() # one no-follow metadata read, no second stat race + mode = info.st_mode + if stat.S_ISLNK(mode) or not (stat.S_ISREG(mode) or stat.S_ISDIR(mode)): + raise RuntimeError('storage_nonregular_artifact: ' + str(p)) + if stat.S_ISREG(mode): + total += info.st_size + if root != root.resolve(strict=True) or not root.is_dir(): + raise RuntimeError('storage_artifact_root_moved_or_linked') + return total + except FileNotFoundError: + if attempt == 3: + raise RuntimeError('storage_artifact_scan_unstable_after_4_attempts') + raise AssertionError('unreachable bounded artifact scan') + + +def tree_digest(root): + """Bind every retained P1 child/log/cleanup artifact for the P2 review gate.""" + artifact_size(root) + entries = {str(p.relative_to(root)): digest(p) for p in sorted(Path(root).rglob('*')) if p.is_file()} + return hashlib.sha256(encode(entries)).hexdigest() + + +class BudgetLedger: + def __init__(self, root, campaign_id, *, clock=boottime, boot=boot_id, cap_bytes=CAP_BYTES, + resource_profile=None): + self.root = Path(root) + self.resource_profile = resource_profile + self.slots, self.total_seconds, self.format = profile_limits(resource_profile) + historical = extended_historical_calls(self.root) if resource_profile is not None else None + if type(cap_bytes) is not int or not HEADROOM_BYTES < cap_bytes <= CAP_BYTES: + raise ValueError('Require reviewed artifact cap with cleanup headroom') + self.cap_bytes = cap_bytes + if not self.root.is_absolute() or self.root != self.root.resolve(): + raise ValueError('Canonical absolute artifact root required') + if not self.root.exists(): + durable_directory(self.root) + self.clock, self.boot = clock, boot + self.lock = (self.root / 'budget.lock').open('a+b') + try: + fcntl.flock(self.lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + self.path = self.root / 'budget.json' + if self.path.exists(): + self.data = json.loads(self.path.read_bytes()) + if (self.data['format'] != self.format + or self.data.get('resource_profile') != resource_profile or self.data['campaign_id'] != campaign_id + or self.data['boot_id'] != boot() or self.data['artifact_cap_bytes'] != cap_bytes + or self.data.get('storage_scope') != STORAGE_SCOPE): + raise RuntimeError('budget_campaign_or_boot_mismatch') + else: + if list(self.root.glob('launch-*')): + raise RuntimeError('budget_missing_ledger_no_recovery') + self.data = {'format': self.format, 'campaign_id': campaign_id, 'boot_id': boot(), + 'first_run_boottime': None, 'launches': [], 'artifact_cap_bytes': cap_bytes, + 'storage_scope': STORAGE_SCOPE} + if resource_profile is not None: + self.data.update(resource_profile=resource_profile, + historical_reserved_calls=historical, local_reserved_calls=0, + cumulative_reserved_calls=historical, p1_adoption_sha256=None) + self._save() + except BaseException: + self.lock.close() + raise + + def _save(self): + temporary = self.root / 'budget.pending' + payload = encode(self.data) + if artifact_size(self.root, exclude_native=True) + len(payload) > self.cap_bytes: + raise RuntimeError('storage_ledger_headroom_exhausted') + durable_write(temporary, payload) + os.replace(temporary, self.path) + sync_directory(self.root) + + def reserve(self, grant, *, enforcement_deadline=float('inf')): + rows = self.data['launches'] + index = len(rows) + if index >= len(self.slots) or grant['launch_index'] != index: + raise RuntimeError('budget_no_retry_resume_or_reused_grant') + if rows and rows[-1]['status'] != 'closed': + raise RuntimeError('budget_interrupted_or_overlapping_campaign') + if rows and not rows[-1]['continuation_eligible']: + raise RuntimeError('budget_previous_fatal_or_unclean') + if self.resource_profile is not None: + if (grant.get('format') != EXTENDED_GRANT_FORMAT + or grant.get('resource_profile') != self.resource_profile + or grant.get('stage') != 'P2' or grant.get('trial_indices') != list(range(20))): + raise RuntimeError('budget_grant_resource_profile_mismatch') + from .evaluation_adoption import verify_adoption + adoption = verify_adoption(self.root, grant['p1_adoption_sha256']) + if adoption['source_boot_id'] != self.boot(): + raise RuntimeError('budget_adoption_boot_mismatch') + if (adoption['historical_reserved_calls'] != extended_historical_calls(self.root) + or self.data['historical_reserved_calls'] != adoption['historical_reserved_calls'] + or self.data['local_reserved_calls'] != 0 + or self.data['cumulative_reserved_calls'] != adoption['historical_reserved_calls']): + raise RuntimeError('budget_historical_debits_mismatch') + elif grant.get('resource_profile') is not None or grant.get('format') == EXTENDED_GRANT_FORMAT: + raise RuntimeError('budget_grant_resource_profile_mismatch') + now = self.clock() + first = self.data['first_run_boottime'] + if first is None: + first = now + if self.boot() != self.data['boot_id'] or now < first or now >= first + self.total_seconds - CLEANUP_SECONDS: + raise RuntimeError('budget_aggregate_deadline_or_boot') + if now >= grant['not_after_boottime']: + raise RuntimeError('budget_expired_parent_grant') + if artifact_size(self.root, exclude_native=True) >= self.cap_bytes - HEADROOM_BYTES: + raise RuntimeError('budget_artifact_headroom') + if index == 2: + expected = [tree_digest(self.root / f'launch-{i}') for i in (0, 1)] + if grant['p1_evidence_sha256'] != expected: + raise RuntimeError('identity_P2_review_does_not_bind_retained_P1_evidence') + ticks, rollout, seconds = self.slots[index] + row = {'index': index, 'status': 'reserved', 'grant': grant, + 'reserved_physical_calls': ticks, 'rollout_cap': rollout, 'initialization_cap': 1000, + 'started_boottime': now, + 'deadline_boottime': min(now + seconds, first + self.total_seconds, enforcement_deadline), + 'continuation_eligible': False, 'observed_counts': None, 'artifact_cap_bytes': self.cap_bytes} + if self.resource_profile is not None: + row['resource_profile'] = self.resource_profile + self.data.update(p1_adoption_sha256=grant['p1_adoption_sha256'], + local_reserved_calls=91000, + cumulative_reserved_calls=self.data['historical_reserved_calls'] + 91000) + self.data['first_run_boottime'] = first + rows.append(row) + self._save() # irrevocable BEFORE Popen, including failed Popen + return row + + def finish(self, *, counts, clean, eligible): + row = self.data['launches'][-1] + if row['status'] != 'reserved': + raise RuntimeError('budget_reservation_already_closed') + row.update(status='closed', observed_counts=counts, cleanup_clean=bool(clean), + continuation_eligible=bool(clean and eligible), ended_boottime=self.clock()) + self._save() + + def close(self): + self.lock.close() + + +class LaunchBudget: + def __init__(self, root, reservation, *, clock=boottime, boot=boot_id): + self.root, self.reservation = Path(root), reservation + self.clock, self.boot = clock, boot + self.interrupted = False + + def check(self, *, pending=0, cleanup=False): + r = self.reservation + limit = r['deadline_boottime'] - (0 if cleanup else CLEANUP_SECONDS) + if self.boot() != r['grant']['boot_id'] or self.clock() >= limit: + raise RuntimeError('budget_time_or_boot') + if self.interrupted and not cleanup: + raise InterruptedError('process_signal') + cap = min(CAP_BYTES, r.get('artifact_cap_bytes', CAP_BYTES)) + ceiling = cap - STDOUT_ALLOWANCE - (0 if cleanup else HEADROOM_BYTES) + if artifact_size(self.root, exclude_native=True) + pending > ceiling: + raise RuntimeError('storage_aggregate_cap') + + def before_step(self, hooks): + self.check() + r = self.reservation + # Attempt counter is incremented by RuntimeHooks before this callback. + # A rejected attempt is not relabeled as a completed physical call. + if (hooks.attempted_steps > r['reserved_physical_calls'] + or (hooks.mode == 'rollout' and hooks.rollout_steps >= r['rollout_cap']) + or (hooks.mode != 'rollout' and hooks.initialization_steps >= r['initialization_cap'])): + raise RuntimeError('budget_physical_calls') + + def recording(self, stage, pending_bytes, written_bytes): + self.check(pending=pending_bytes, cleanup=stage == 'finalize') + + def write(self, path, value): + data = encode(value) + self.check(pending=len(data), cleanup=True) + durable_write(path, data) diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_protocol.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_protocol.py new file mode 100644 index 0000000..011014c --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_protocol.py @@ -0,0 +1,433 @@ +"""Pure, preregistered P1/P2 development protocol; never an execution grant. + +Integer clocks are authoritative. Evidence is right-closed per cycle; a seam's +next command is available only with explicit admission by the future evaluator. +No simulator, tensor, controller, RNG, or measured-landing state is accessed. +""" + +from __future__ import annotations + +import json +import math +from bisect import bisect_right +from collections.abc import Mapping, Sequence +from dataclasses import dataclass +from pathlib import Path +from types import MappingProxyType +from typing import Any + +PHYSICS_DT_S = 0.002 +CONTROL_TICKS = 5 +PHASE_TICKS = (1500, 500, 300, 700, 400, 1100) +PHASE_END_TICKS = (1500, 2000, 2300, 3000, 3400, 4500) +CYCLE_TICKS = 4500 +EVIDENCE_PHASE_COUNTS = (1499, 500, 300, 700, 400, 1101) +EXTENDED_PROFILE = "p2-wall10800-aggregate14400-v1" +Vec3 = tuple[float, float, float] + +_FUNCTIONAL_GATES = { + "minimum_complete_cycles": 1, + "support_final_window_s": 0.5, + "reload_final_window_s": 0.2, + "contact_force_on_N": 2.0, + "support_reload_contact_fraction_min": 0.8, + "support_reload_load_fraction_min": 0.5, + "unload_final_window_s": 0.1, + "unload_remaining_support_fraction_max": 0.8, + "swing_gap_m_min": 0.01, + "swing_air_fraction_min": 0.8, + "swing_contiguous_air_s_min": 0.1, + "swing_xy_progress_fraction_min": 0.5, + "landing_position_error_m_max": 0.05, + "root_height_error_m_max": 0.1, + "normal_force_negative_tolerance_N": 0.5, + "root_tilt_rad_max": 0.6, + "ground_penetration_m_max": 0.002, + "auxiliary_vertical_N_max": 140.0, + "auxiliary_lever_arm_m_max": 0.06, + "guide_vertical_N_max": 0.000001, + "normalized_effort_velocity_max": 1.00001, + "joint_limit_excess_rad_max": 0.01, +} + +_P1_MARGINS = { + "root_horizontal_error_m_max": 0.030, + "root_absolute_z_error_m_max": 0.060, + "seam_to_initial_root_horizontal_drift_m_max": 0.020, + "reload_mean_3d_error_m_max": 0.020, + "reload_instantaneous_3d_error_m_max": 0.030, + "stance_slip_per_cycle_m_max": 0.030, + "stance_slip_per_run_m_max": 0.060, + "repeat_B_reload_mean_xy_drift_m_max": 0.015, + "unload_remaining_support_fraction_max": 0.70, + "premature_separation_allowed": False, + "lift_gap_m_min": 0.022, + "lift_normal_N_max": 2.0, + "lift_deadline_local_tick": 2260, + "lift_dwell_ticks": 5, + "lift_onset_phase": 2, + "reload_final_dwell_ticks": 50, + "reload_gap_m_max": 0.0005, + "reload_load_fraction_min": 0.8, + "reload_load_fraction_max": 1.2, + "reload_contact_required": True, + "reload_separation_allowed": False, +} + +_EVENTS = [ + {"scenario": 1, "body": "virtual_payload", "frame": "world", "point_frame": "body", + "point_m": [0.03, -0.02, 0.06], "force_N": [3.0, -4.0, -2.0], "couple_Nm": [0.2, 0.1, -0.1], + "trigger": "time", "phase": 0, "start_s": 1.0, "duration_s": 6.5, "ramp_s": 0.5}, + {"scenario": 2, "body": "FL_thigh", "frame": "body", "point_frame": "body", + "point_m": [0.02, 0.01, -0.06], "force_N": [-2.0, 1.0, 2.0], "couple_Nm": [0.1, -0.1, 0.2], + "trigger": "liftoff", "phase": 2, "start_s": 0.02, "duration_s": 0.2, "ramp_s": 0.1}, + {"scenario": 3, "body": "virtual_payload", "frame": "mount", "point_frame": "mount", + "point_m": [-0.03, 0.04, 0.04], "force_N": [2.0, 2.0, -3.0], "couple_Nm": [-0.2, 0.3, 0.1], + "trigger": "phase", "phase": 4, "start_s": 0.1, "duration_s": 0.6, "ramp_s": 0.15}, + {"scenario": 3, "body": "FL_calf", "frame": "world", "point_frame": "body", + "point_m": [0.01, 0.0, -0.08], "force_N": [-1.0, 1.5, 0.5], "couple_Nm": [0.0, -0.1, 0.1], + "trigger": "touchdown", "phase": 4, "start_s": 0.0, "duration_s": 0.3, "ramp_s": 0.1}, +] + + +def _approved_document(stage: str, resource_profile: str | None = None) -> dict[str, Any]: + """Exact preregistered schemas/values; only the named P2 wall profile is additive.""" + if stage not in ("P1", "P2"): + raise ValueError("Unknown evaluation stage") + conditions = [ + {"id": name, "scenario_id": scenario, "swing_displacement_m": delta, + "event_indices": indices} + for name, scenario, delta, indices in ( + ("nominal", 0, [0.025, 0.020, 0.0], []), + ("oblique_sustained", 1, [0.025, 0.020, 0.0], [0]), + ("eccentric_liftoff", 2, [0.025, 0.020, 0.0], [1]), + ("landing_combination", 3, [0.025, 0.020, 0.0], [2, 3]), + ("target_only", 0, [0.030, 0.025, 0.0], []), + ) + ] + document = { + "format": "skillfactory.contact-cycle-evaluation-protocol.v1", + "stage": stage, + "status": "prospective_development_not_acceptance", + "execution_authorized": False, + "requires_parent_physics_approval": True, + "requires_P1_evidence_review": stage == "P2", + "accepted_skill": False, + "optimizer_steps": 0, + "num_envs": 1, + "frozen_model": { + "candidate": "logs/contact_cycle/sim_residual_training_v1/prepared01/candidate_u0256.pt", + "candidate_sha256": "c088bb3aecaca751867118858626fd508f7a3983303802d7524353f80802cf99", + "features": "contact-cycle-sim-truth-residual-46-v1", + "learned_policy": "deterministic_mean_residual_plus_unchanged_analytic_reference", + "analytic_reference": "zero_residual_plus_unchanged_analytic_reference", + "verification": "unchanged_verify_bundle_plus_separate_new_evaluation_binding", + "selection_or_tuning": False, + }, + "controllers": ["learned_policy", "analytic_reference"], + "seeds": [93501] if stage == "P1" else [94501, 94502], + "trial_order": "condition_then_seed_then_controller", + "trial_count": 2 if stage == "P1" else 20, + "cycles_per_trial": 3 if stage == "P1" else 1, + "anchor_sequence": ["A", "B", "A", "B"] if stage == "P1" else ["A", "B"], + "conditions": conditions[:1] if stage == "P1" else conditions, + "events": [] if stage == "P1" else _EVENTS, + "clock": { + "physics_dt_s": PHYSICS_DT_S, "control_ticks": CONTROL_TICKS, + "phase_durations_s": [3.0, 1.0, 0.6, 1.4, 0.8, 2.2], + "phase_ticks": list(PHASE_TICKS), "cycle_ticks": CYCLE_TICKS, + "evidence_phase_counts": list(EVIDENCE_PHASE_COUNTS), + "evidence_intervals": "right_closed_initial_tick_separate", + "phase_boundary": "next_phase_at_exact_internal_boundary", + }, + "command": { + "anchor_frame": "world_fixed_from_initial_ground_foot_target", + "root_target": "fixed_initial_commanded_mounting_port", + "measured_landing_reanchor": False, + "clearance_m": 0.05, "swing_arch_m": 0.01, + "actor_target_coordinates": "world_axis_target_minus_commanded_port_origin", + "ik_target_coordinates": "inverse_rotate_actual_root_quaternion_target_minus_actual_root_position", + "noise_or_domain_randomization": False, + "reference_controller_reward_residual_changes": False, + }, + "thresholds": { + "functional_source": "configs/contact_cycle/simulation_functional_v1.json#gates", + "functional": _FUNCTIONAL_GATES, + "functional_application": "each_complete_cycle_with_signed_requested_xy_progress", + "excluded_nominal_auditor_checks": ["nominal_only", "no_external_disturbance"], + "replacement_condition_check": "exact_bound_trial_scenario_and_event_program", + "original_runtime_source": "source/SkillFactory/SkillFactory/legs/contact_cycle/task.py#phase_failures", + "original_runtime": { + "lift_progress_strictly_greater": 0.9, "lift_gap_m_min": 0.02, + "lift_normal_N_max": 2.0, "swing_gap_m_min": 0.01, + "reload_progress_strictly_greater": 0.8, "reload_load_fraction_min": 0.5, + "root_height_below_nominal_m_max": 0.1, "root_tilt_rad_max": 0.6, + "guide_vertical_N_exact": 0.0, + "nonfinite_allowed": False, "premature_separation_allowed": False, + }, + "contact_source": "configs/contact_cycle/go2_fl_v1.json; contact_cycle/mechanics.py", + "contact": { + "force_on_N": 2.0, "force_off_N": 0.5, "dwell_ticks": 5, + "separation_on_m": 0.001, "separation_off_m": 0.0005, + "initial_acquisition_deadline_tick": 100, + "initial_acquisition_load_fraction_min": 0.5, + "initial_acquisition_gap_m_max": 0.0005, + "initial_acquisition_rearmed_at_seam": False, + }, + "P1_margins_source": "parent_approved_prospective_development_v1_not_P0_pass_claim", + "P1_height_value_precedent": "configs/contact_cycle/acceptance_v1.json#gates.height_error_m_max", + "P1_margins": _P1_MARGINS, + "P1_margins_role": "gate" if stage == "P1" else "diagnostic_only", + "historical_swing_clearance_diagnostic_m": 0.02, + "event_delivery": {"fired_and_completed_required": True, "area": "duration_s_minus_ramp_s", + "area_tolerance_s": 0.002}, + "event_delivery_source": ( + "source/SkillFactory/SkillFactory/legs/contact_cycle/mechanics.py#delivered_events" + ), + }, + "resources": { + "proposal_only": True, "max_launches": 3, "max_sim_step_ticks_including_initialization": 120000, + "max_rollout_ticks": 117000, "max_initialization_reset_ticks": 3000, + "max_control_steps": 23400, "aggregate_same_boot_window_s": 3600, + "max_artifact_bytes": 1073741824, "cleanup_reserve_s": 120, + "P1_max_launches": 2, "P1_launch_wall_cap_s": 600, + "P1_rollout_ticks_per_launch": 13500, + "P2_max_launches": 1, "P2_launch_wall_cap_s": 2100, + "P2_rollout_ticks_per_launch": 90000, + "overlapping_jobs": False, "retries_or_resume": False, + "count_hook_before_construction": True, + }, + "handoff": { + "seam_order": ["physics_and_latches", "safety_and_endpoint_snapshot", "cycle_gate", + "command_metadata_only", "single_reference_refresh", "next_observation"], + "carry_state": ["physical_pose_velocity", "previous_action_processed_target_residual", + "actuator_sensor_delay", "IK_warm_start_action_refresh_count", "contact_latches_timers", + "separation_onset_armed_premature", "initial_acquisition", "global_substeps", "RNG", + "one_shot_event_scheduler"], + "reset_or_pose_write_within_trial": False, + "completion": "protocol_complete_without_terminal_reset", + "seam_admission_failure": "stop_at_endpoint_no_extension_or_wait", + "initial_reset_per_trial": True, + "P1_separate_fresh_launch_per_controller": True, + "P2_reset_only_between_trials": True, + "failure_check": "after_each_scene_tick_before_another_sim_step", + "failure_reporting": "first_tick_all_causes_passed_prefix_only_remaining_gates_not_evaluated", + "P2_functional_failure": "retain_row_continue_matrix_if_safe", + "fatal_failures": ["hardware", "nonfinite", "identity", "illegal_reset_or_pose_write", + "storage_or_recording", "process", "budget_expired"], + "pairing": "same_seed_initial_state_targets_trigger_rule_not_equal_wall_time_force_samples", + "P2_all_rows_required": True, + "P2_new_seeds_are": "development_repeatability_not_independent_robustness", + }, + } + + if resource_profile is not None: + if resource_profile != EXTENDED_PROFILE or stage != "P2": + raise ValueError("Unknown or incompatible resource profile") + document["format"] = "skillfactory.contact-cycle-evaluation-protocol.v2" + document["resource_profile"] = EXTENDED_PROFILE + document["resources"]["P2_launch_wall_cap_s"] = 10800 + document["resources"]["aggregate_same_boot_window_s"] = 14400 + return document + + +def _match(actual: Any, expected: Any, path: str = "protocol") -> None: + # Exact numeric types reject bool-as-int, coercion, and nonfinite values. + if type(actual) is not type(expected): + raise ValueError(f"{path}: invalid type") + if isinstance(expected, dict): + if actual.keys() != expected.keys(): + raise ValueError(f"{path}: missing or unknown keys") + for key in expected: + _match(actual[key], expected[key], f"{path}.{key}") + elif isinstance(expected, list): + if len(actual) != len(expected): + raise ValueError(f"{path}: invalid length") + for index, value in enumerate(expected): + _match(actual[index], value, f"{path}[{index}]") + elif actual != expected: + raise ValueError(f"{path}: differs from preregistered value") + + +def _freeze(value: Any) -> Any: + if isinstance(value, dict): + return MappingProxyType({key: _freeze(item) for key, item in value.items()}) + if isinstance(value, list): + return tuple(_freeze(item) for item in value) + return value + + +def _integer(value: int, low: int, high: int, name: str) -> None: + if type(value) is not int or not low <= value <= high: + raise ValueError(f"{name}: require integer in [{low}, {high}]") + + +def _vector(value: Sequence[float], name: str) -> Vec3: + if (not isinstance(value, (list, tuple)) or len(value) != 3 + or any(type(x) not in (int, float) or not math.isfinite(x) for x in value)): + raise ValueError(f"{name}: require three finite real coordinates") + return tuple(float(x) for x in value) + + +@dataclass(frozen=True) +class Trial: + """Ordered, paired descriptor. Event indices address Protocol.document['events'].""" + + index: int + trial_id: str + pair_id: str + condition_id: str + seed: int + controller_kind: str + scenario_id: int + swing_displacement_m: Vec3 + event_indices: tuple[int, ...] + anchor_sequence: tuple[str, ...] + + @property + def total_ticks(self) -> int: + return (len(self.anchor_sequence) - 1) * CYCLE_TICKS + + +@dataclass(frozen=True) +class Clock: + global_tick: int + cycle_index: int + local_tick: int + + @property + def global_time_s(self) -> float: + return self.global_tick * PHYSICS_DT_S + + @property + def local_time_s(self) -> float: + return self.local_tick * PHYSICS_DT_S + + +@dataclass(frozen=True) +class Command: + phase: int + progress: float + start_anchor: str + end_anchor: str + target_world_m: Vec3 + target_velocity_world_m_s: Vec3 + desired_normal_N: float + auxiliary_fraction: float + root_target_world_m: Vec3 + root_velocity_world_m_s: Vec3 + actor_target_displacement_world_m: Vec3 + + +@dataclass(frozen=True, init=False) +class Protocol: + """Validated immutable handoff; methods compute metadata, never admit physics.""" + + document: Mapping[str, Any] + trials: tuple[Trial, ...] + + def __init__(self, document: dict[str, Any]): + if type(document) is not dict or document.get("stage") not in ("P1", "P2"): + raise ValueError("Require P1/P2 protocol document") + _match(document, _approved_document(document["stage"], document.get("resource_profile"))) + frozen = _freeze(document) + rows = [] + for condition in frozen["conditions"]: + for seed in frozen["seeds"]: + pair = f"{frozen['stage']}/{condition['id']}/{seed}" + for controller in frozen["controllers"]: + rows.append(Trial(len(rows), f"{pair}/{controller}", pair, condition["id"], seed, controller, + condition["scenario_id"], condition["swing_displacement_m"], + condition["event_indices"], frozen["anchor_sequence"])) + object.__setattr__(self, "document", frozen) + object.__setattr__(self, "trials", tuple(rows)) + + def trial(self, index: int) -> Trial: + _integer(index, 0, len(self.trials) - 1, "trial index") + return self.trials[index] + + def evidence_clock(self, trial_index: int, global_tick: int) -> Clock: + """Ticks 1..4500 belong to cycle 0; tick 0 is initial state, not evidence.""" + trial = self.trial(trial_index) + _integer(global_tick, 1, trial.total_ticks, "evidence global tick") + cycle = (global_tick - 1) // CYCLE_TICKS + return Clock(global_tick, cycle, global_tick - cycle * CYCLE_TICKS) + + def next_command_clock(self, trial_index: int, global_tick: int, *, seam_admitted: bool = False) -> Clock | None: + """Control boundary only. Snapshot/evaluate old endpoint BEFORE requesting this. + + ``seam_admitted`` is a caller assertion, not a gate result computed here. + At final completion return None: never invent a fourth cycle or reset. + """ + trial = self.trial(trial_index) + _integer(global_tick, 0, trial.total_ticks, "command global tick") + if type(seam_admitted) is not bool or global_tick % CONTROL_TICKS: + raise ValueError("Require a control boundary and boolean seam admission") + if global_tick == trial.total_ticks: + return None + if global_tick and global_tick % CYCLE_TICKS == 0 and not seam_admitted: + raise ValueError("Preceding endpoint must pass before advancing command metadata") + return Clock(global_tick, global_tick // CYCLE_TICKS, global_tick % CYCLE_TICKS) + + def command(self, trial_index: int, clock: Clock, *, anchor_a_world: Sequence[float], + root_origin_world: Sequence[float], weight_N: float) -> Command: + """Fixed A/B targets, not measured landings. SI world coordinates. + + Clock may be an evidence endpoint or a separately admitted next command. + Root origin is the initial commanded mounting-port origin, not measured root. + """ + trial = self.trial(trial_index) + if type(clock) is not Clock: + raise ValueError("Require Clock metadata") + _integer(clock.cycle_index, 0, len(trial.anchor_sequence) - 2, "cycle index") + _integer(clock.local_tick, 0, CYCLE_TICKS, "local tick") + _integer(clock.global_tick, 0, trial.total_ticks, "global tick") + if clock.global_tick != clock.cycle_index * CYCLE_TICKS + clock.local_tick: + raise ValueError("Inconsistent global/local clock") + a, root = _vector(anchor_a_world, "anchor A"), _vector(root_origin_world, "root origin") + if type(weight_N) not in (float, int) or not math.isfinite(weight_N) or weight_N <= 0: + raise ValueError("Require positive finite weight_N") + start, end = trial.anchor_sequence[clock.cycle_index:clock.cycle_index + 2] + b = tuple(x + d for x, d in zip(a, trial.swing_displacement_m)) + origin, finish = (a, b) if start == "A" else (b, a) + delta = tuple(y - x for x, y in zip(origin, finish)) + phase = bisect_right(PHASE_END_TICKS[:-1], clock.local_tick) + phase_start = 0 if phase == 0 else PHASE_END_TICKS[phase - 1] + progress = (clock.local_tick - phase_start) / PHASE_TICKS[phase] + u = progress * progress * (3 - 2 * progress) + du = 6 * progress * (1 - progress) / (PHASE_TICKS[phase] * PHYSICS_DT_S) + travel = 0.0 if phase < 3 else u if phase == 3 else 1.0 + height = u if phase == 2 else 1.0 if phase == 3 else 1 - u if phase == 4 else 0.0 + dz = 0.05 * (du if phase == 2 else -du if phase == 4 else 0.0) + arch = 0.0 + if phase == 3: + arch = 0.01 * math.sin(math.pi * progress) ** 2 + dz += 0.01 * math.pi * math.sin(2 * math.pi * progress) / (PHASE_TICKS[phase] * PHYSICS_DT_S) + target = tuple(origin[j] + travel * delta[j] + (0.05 * height + arch if j == 2 else 0.0) + for j in range(3)) + velocity = tuple((du * delta[j] if phase == 3 else 0.0) + (dz if j == 2 else 0.0) for j in range(3)) + load = 1.0 if phase == 0 else 1 - u if phase == 1 else u if phase == 5 else 0.0 + values = (*b, *target, *velocity, *(x - y for x, y in zip(target, root)), weight_N * load) + if not all(math.isfinite(x) for x in values): + raise ValueError("Nonfinite derived command") + return Command(phase, progress, start, end, target, velocity, weight_N * load, 1 - load, + root, (0.0, 0.0, 0.0), tuple(x - y for x, y in zip(target, root))) + + +def validate_protocol(document: dict[str, Any]) -> Protocol: + """Validate exact preregistered v1 keys/types/values; return an immutable copy.""" + return Protocol(document) + + +def _unique_keys(pairs: list[tuple[str, Any]]) -> dict[str, Any]: + result = {} + for key, value in pairs: + if key in result: + raise ValueError(f"Duplicate JSON key: {key}") + result[key] = value + return result + + +def load_protocol(path: str | Path) -> Protocol: + """Read JSON without duplicate-key or nonfinite-value escape hatches.""" + return validate_protocol(json.loads(Path(path).read_text(), object_pairs_hook=_unique_keys)) diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/model.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/model.py new file mode 100644 index 0000000..24fff11 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/model.py @@ -0,0 +1,61 @@ +"""Untrained 46D residual network API. No trainer/checkpoint migration in this module.""" +import math + +import torch +from torch import nn +from torch.distributions import Normal + +from .config import FEATURE_VERSION + + +def features(x): + if (not isinstance(x, torch.Tensor) or x.ndim != 2 or x.shape[1] != 46 or not len(x) + or x.dtype != torch.float32 or not torch.isfinite(x).all()): + raise ValueError('Require finite float32 [B,46] simulation-truth residual features') + return x + + +class SimResidualActorCritic(nn.Module): + feature_version = FEATURE_VERSION + + def __init__(self, hidden=128, initial_std=.1): + super().__init__() + if type(hidden) is not int or hidden < 8 or not .02 <= initial_std <= .5: + raise ValueError('Invalid residual network parameters') + offset, scale = torch.zeros(46), torch.ones(46) + offset[:3] = torch.tensor([0., .8, -1.5]) + scale[:3], scale[3:6] = .5, 10. + scale[6:12] = torch.tensor([50., 50., 50., 5., 5., 5.]) + scale[15:18], scale[28:31], scale[31] = 5., .4, 50. + scale[32:35], scale[35:38], scale[38], scale[39] = .1, 1., 50., .05 + self.register_buffer('offset', offset) + self.register_buffer('scale', scale) + self.actor = nn.Sequential(nn.Linear(46, hidden), nn.Tanh(), nn.Linear(hidden, hidden), nn.Tanh(), + nn.Linear(hidden, 3)) + self.critic = nn.Sequential(nn.Linear(46, hidden), nn.Tanh(), nn.Linear(hidden, hidden), nn.Tanh(), + nn.Linear(hidden, 1)) + for layer in self.modules(): + if isinstance(layer, nn.Linear): + nn.init.orthogonal_(layer.weight, math.sqrt(2)) + nn.init.zeros_(layer.bias) + nn.init.zeros_(self.actor[-1].weight) # deterministic initial policy is EXACTLY zero residual + nn.init.orthogonal_(self.critic[-1].weight, 1.) + self.log_std = nn.Parameter(torch.full((3,), math.log(initial_std))) + self.float() + + def distribution_value(self, x): + normalized = ((features(x) - self.offset) / self.scale).clamp(-10, 10) + return Normal(self.actor(normalized), self.log_std.clamp(math.log(.02), math.log(.5)).exp()), \ + self.critic(normalized).squeeze(-1) + + def act(self, x, deterministic=False): + dist, value = self.distribution_value(x) + latent = dist.mean if deterministic else dist.sample() + return latent.tanh(), latent, dist.log_prob(latent).sum(-1), value + + def evaluate(self, x, latent): + dist, value = self.distribution_value(x) + return dist.log_prob(latent).sum(-1), value + + def value(self, x): + return self.distribution_value(x)[1] diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/ppo.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/ppo.py new file mode 100644 index 0000000..5ec7820 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/ppo.py @@ -0,0 +1,342 @@ +"""46D residual learner boundary; no training launch or legacy checkpoint migration. + +PPO math is unchanged. Callers must collect with this module (diagnostics fail +closed), record rollout_diagnostics before ppo_step, and own authorization/budgets. +""" + +import copy +import io +import math +import os +import tempfile +from pathlib import Path + +import torch + +from ..contact_cycle.mechanics import canonical_ids +from ..contact_cycle.ppo import rollout_diagnostics # noqa: F401 +from ..contact_cycle.task import FAILURE_NAMES +from ..standing_ppo import ppo_step, restore_rng, rng_state # noqa: F401 +from .config import FEATURE_VERSION +from .model import SimResidualActorCritic, features + +CONTRACT = "go2-fl-contact-cycle-sim-residual-v1" +CHECKPOINT_FORMAT = "skillfactory.contact-cycle-sim-residual-ppo-checkpoint.v1" + + +def _tensor(x, shape, dtype, device, name): + if (not isinstance(x, torch.Tensor) or x.shape != shape or x.dtype != dtype + or x.device != device or not torch.isfinite(x).all()): + raise ValueError(f"Invalid {name}: require finite {dtype} {shape} on {device}") + return x + + +@torch.no_grad() +def bootstrap_values(model, next_features, terminated, truncated, info): + """Failures are zero; only pre-reset terminal policy can bootstrap timeouts.""" + x = features(next_features) + shape = (len(x),) + _tensor(terminated, shape, torch.bool, x.device, "terminated") + _tensor(truncated, shape, torch.bool, x.device, "truncated") + if not isinstance(info, dict): + raise ValueError("Invalid step info") + value = torch.zeros(len(x), device=x.device, dtype=torch.float32) + continuing = ~(terminated | truncated) + if continuing.any(): + v = model.value(x[continuing]) + value[continuing] = _tensor(v, (int(continuing.sum()),), torch.float32, x.device, "next value") + timeout = truncated & ~terminated + if timeout.any(): + ids, terminal = info.get("terminal_ids"), info.get("terminal_observation") + if ids is None or not isinstance(terminal, dict) or "policy" not in terminal: + raise RuntimeError("Missing pre-reset timeout observations") + try: + ids = canonical_ids(ids, len(x)).to(x.device) + except ValueError as error: + raise RuntimeError("Invalid terminal indices") from error + if (not torch.isin(timeout.nonzero().flatten(), ids).all() + or not (terminated | truncated)[ids].all()): + raise RuntimeError("Invalid terminal coverage: missing timeout or nonterminal index") + terminal_x = features(terminal["policy"]) + if len(terminal_x) != len(ids) or terminal_x.device != x.device: + raise RuntimeError("Terminal observation/index shape or device mismatch") + selected = timeout[ids] + v = model.value(terminal_x[selected]) + value[ids[selected]] = _tensor(v, (int(selected.sum()),), torch.float32, x.device, "terminal value") + return value + + +@torch.no_grad() +def collect_rollout(env, model, x, config, guard=lambda: None): + """Copy pre-step tensors before an environment may reuse observation buffers. + + Counts distinguish true failures (including simultaneous timeout flags) from + timeout-only resets. Every reset ends GAE, even when its value bootstraps. + """ + horizon, penalty = config["horizon"], config["failure_penalty"] + if (type(horizon) is not int or horizon < 1 or type(env.num_envs) is not int or env.num_envs < 1 + or type(penalty) not in (int, float) or not math.isfinite(penalty) or penalty < 0 + or not math.isfinite(env.step_dt) or env.step_dt <= 0): + raise ValueError("Invalid rollout dimensions/reward scaling") + x = features(x).clone() + device, batch = x.device, env.num_envs + # Tensor.device canonicalizes CUDA's default index, unlike torch.device('cuda'). + if len(x) != batch or torch.empty(0, device=env.device).device != device: + raise ValueError("Environment/feature batch or device mismatch") + data = {key: torch.empty(horizon, batch, *shape, device=device, dtype=dtype) + for key, shape, dtype in ( + ("features", (46,), torch.float32), ("latent", (3,), torch.float32), + ("log_prob", (), torch.float32), ("value", (), torch.float32), + ("reward", (), torch.float32), ("next_value", (), torch.float32), + ("done", (), torch.bool), ("phase", (), torch.long), + ("failure_flags", (len(FAILURE_NAMES),), torch.bool))} + failures = timeouts = 0 + for step in range(horizon): + guard() + action, latent, logp, value = model.act(x) + _tensor(action, (batch, 3), torch.float32, device, "action") + if (action.abs() > 1).any(): + raise ValueError("Residual action outside [-1,1]") + for key, item, shape in (("features", x, (batch, 46)), ("latent", latent, (batch, 3)), + ("log_prob", logp, (batch,)), ("value", value, (batch,))): + data[key][step].copy_(_tensor(item, shape, torch.float32, device, key)) + obs, reward, terminated, truncated, info = env.step(action) + nx = features(obs["policy"]) + _tensor(nx, (batch, 46), torch.float32, device, "next features") + nv = bootstrap_values(model, nx, terminated, truncated, info) + _tensor(reward, (batch,), torch.float32, device, "reward") + reward = reward / env.step_dt - penalty * terminated.float() + _tensor(reward, (batch,), torch.float32, device, "scaled reward") + diagnostic = info.get("cycle_step") + if not isinstance(diagnostic, dict) or not {"phase", "failure_flags"} <= diagnostic.keys(): + raise RuntimeError("Missing cycle_step diagnostics; rollout cannot be optimized") + phase = diagnostic["phase"] + if not isinstance(phase, torch.Tensor) or phase.dtype not in (torch.int32, torch.int64): + raise ValueError("Invalid cycle_step phase type") + _tensor(phase, (batch,), phase.dtype, device, "phase") + if ((phase < 0) | (phase > 5)).any(): + raise ValueError("Invalid cycle_step phase range") + flags = _tensor(diagnostic["failure_flags"], (batch, len(FAILURE_NAMES)), torch.bool, + device, "failure_flags") + for key, item in (("reward", reward), ("next_value", nv), ("done", terminated | truncated), + ("phase", phase), ("failure_flags", flags)): + data[key][step].copy_(item) + failures += int(terminated.sum()) + timeouts += int((truncated & ~terminated).sum()) + x = nx.clone() + return data, x, failures, timeouts + + +def selection_key(rows): + """Preregistered lexicographic (failure fraction, all-row mean cost).""" + if not rows: + raise ValueError("Empty validation is not a candidate") + failures, scores = 0, [] + for row in rows: + if not isinstance(row, dict) or type(row.get("success", False)) is not bool: + raise ValueError("Invalid validation success flag") + if not row.get("success", False): + failures += 1 + scores.append(16.) + continue + values = [row.get(key) for key in ("height_rms_m", "swing_rms_m", "landing_error_m")] + if any(type(v) not in (int, float) or not math.isfinite(v) or v < 0 for v in values): + raise ValueError("Missing/nonfinite/negative successful validation metric") + scores.append(sum(min(v / scale, 2.) for v, scale in zip(values, (.06, .025, .05)))) + return failures / len(rows), sum(scores) / len(rows) + + +def _finite_tree(value): + if isinstance(value, torch.Tensor): + if not torch.isfinite(value).all(): + raise ValueError("Nonfinite checkpoint tensor") + elif isinstance(value, dict): + for key, item in value.items(): + if type(key) not in (str, int): + raise ValueError("Invalid checkpoint mapping key") + _finite_tree(item) + elif isinstance(value, (list, tuple)): + for item in value: + _finite_tree(item) + elif type(value) is float: + if not math.isfinite(value): + raise ValueError("Nonfinite checkpoint scalar") + elif value is not None and type(value) not in (str, int, bool): + raise ValueError("Unsupported checkpoint value type") + + +def _model_contract(model, model_config): + if (type(model) is not SimResidualActorCritic or model.feature_version != FEATURE_VERSION + or not isinstance(model_config, dict) or set(model_config) != {"hidden", "initial_std"} + or type(model_config["hidden"]) is not int or model_config["hidden"] < 8 + or type(model_config["initial_std"]) not in (int, float) + or not math.isfinite(model_config["initial_std"]) or not .02 <= model_config["initial_std"] <= .5): + raise ValueError("Residual model/config/type mismatch") + # Construction only checks topology and fixed normalization, not trained weights. + # initial_std is explicit provenance: the unchanged model stores no constructor config. + with torch.random.fork_rng(devices=[]): + expected = SimResidualActorCritic(**model_config) + _model_state(model.state_dict(), expected.state_dict()) + for name in ("actor", "critic"): + actual_layers, expected_layers = list(getattr(model, name)), list(getattr(expected, name)) + if len(actual_layers) != len(expected_layers) or any( + type(a) is not type(b) for a, b in zip(actual_layers, expected_layers)): + raise ValueError("Residual model topology mismatch") + + +def _model_state(state, expected): + if not isinstance(state, dict) or set(state) != set(expected): + raise ValueError("Residual model state keys mismatch") + for key, reference in expected.items(): + value = state[key] + if (not isinstance(value, torch.Tensor) or value.shape != reference.shape + or value.dtype != reference.dtype or not torch.isfinite(value).all()): + raise ValueError(f"Residual model state shape/type/finite mismatch: {key}") + if key in ("offset", "scale") and not torch.equal(value.cpu(), reference.cpu()): + raise ValueError("Residual fixed normalization mismatch") + + +def _optimizer_type(optimizer): + return f"{type(optimizer).__module__}.{type(optimizer).__qualname__}" + + +def _same_types(value, reference): + if type(value) is not type(reference): + return False + if isinstance(reference, (tuple, list)): + return len(value) == len(reference) and all(_same_types(a, b) for a, b in zip(value, reference)) + if isinstance(reference, torch.Tensor): + return value.shape == reference.shape and value.dtype == reference.dtype + return True + + +def _optimizer_state(optimizer, state): + if not isinstance(state, dict) or set(state) != {"state", "param_groups"}: + raise ValueError("Invalid optimizer state") + groups = state["param_groups"] + if not isinstance(groups, list) or len(groups) != len(optimizer.param_groups): + raise ValueError("Optimizer parameter groups mismatch") + parameters, amsgrad = {}, {} + for saved, current in zip(groups, optimizer.param_groups): + if not isinstance(saved, dict) or set(saved) != set(current): + raise ValueError("Optimizer parameter group fields mismatch") + if any(not _same_types(saved[key], current[key]) for key in current if key != "params"): + raise ValueError("Optimizer parameter group value type mismatch") + ids = saved["params"] + if not isinstance(ids, list) or len(ids) != len(current["params"]): + raise ValueError("Optimizer parameter count mismatch") + for ident, parameter in zip(ids, current["params"]): + if type(ident) is not int or ident in parameters: + raise ValueError("Invalid optimizer parameter indices") + parameters[ident] = parameter + amsgrad[ident] = saved.get("amsgrad", False) + if not isinstance(state["state"], dict) or not state["state"].keys() <= parameters.keys(): + raise ValueError("Invalid optimizer state indices") + _finite_tree(state) + for ident, slots in state["state"].items(): + if not isinstance(slots, dict): + raise ValueError("Invalid optimizer slots") + if isinstance(optimizer, torch.optim.Adam): + expected_slots = {"step", "exp_avg", "exp_avg_sq"} + if amsgrad[ident]: + expected_slots.add("max_exp_avg_sq") + if set(slots) != expected_slots or any(not isinstance(v, torch.Tensor) for v in slots.values()): + raise ValueError("Invalid Adam optimizer slots/types") + for key, value in slots.items(): + if isinstance(value, torch.Tensor): + parameter = parameters[ident] + if key == "step": + if (value.shape != () or not value.is_floating_point() + or value.item() < 0 or value.item() != int(value.item())): + raise ValueError("Invalid optimizer step") + elif value.shape != parameter.shape or value.dtype != parameter.dtype: + raise ValueError("Optimizer tensor shape/type mismatch") + # Load into a detached copy so framework-level rejection cannot partially mutate the learner. + copy.deepcopy(optimizer).load_state_dict(copy.deepcopy(state)) + + +def _rng_contract(state): + if not isinstance(state, dict) or set(state) != {"cpu", "cuda"} or not isinstance(state["cuda"], list): + raise ValueError("Invalid RNG state") + if state["cuda"] and len(state["cuda"]) != torch.cuda.device_count(): + raise ValueError("RNG CUDA device count mismatch") + for device, value in [("cpu", state["cpu"])] + [(f"cuda:{i}", v) for i, v in enumerate(state["cuda"])]: + if not isinstance(value, torch.Tensor) or value.dtype != torch.uint8 or value.ndim != 1: + raise ValueError("Invalid RNG tensor") + try: + torch.Generator(device=device).set_state(value.cpu()) + except RuntimeError as error: + raise ValueError("Invalid RNG state bytes") from error + + +def _checkpoint_contract(data, model, optimizer, identity, model_config): + required = {"format", "contract", "features", "model_config", "model", "optimizer", "optimizer_type", + "identity", "update", "best", "rng", "budget_snapshot", "accepted_skill"} + if (not isinstance(data, dict) or set(data) != required or data["format"] != CHECKPOINT_FORMAT + or data["contract"] != CONTRACT or data["features"] != FEATURE_VERSION + or not isinstance(identity, dict) or data["identity"] != identity + or data["model_config"] != model_config or data["optimizer_type"] != _optimizer_type(optimizer) + or type(data["update"]) is not int or data["update"] < 0 or data["accepted_skill"] is not False + or not isinstance(data["budget_snapshot"], dict)): + raise ValueError("Residual checkpoint/config/source/type mismatch") + _finite_tree(data) + _model_contract(model, model_config) + # Validate the saved config's types as well (bool/int equality is not a type check). + if any(type(data["model_config"][k]) is not type(model_config[k]) for k in model_config): + raise ValueError("Residual checkpoint model_config type mismatch") + _model_state(data["model"], model.state_dict()) + model_parameters = list(model.parameters()) + optimizer_parameters = [p for group in optimizer.param_groups for p in group["params"]] + if len(optimizer_parameters) != len(model_parameters) or {id(p) for p in optimizer_parameters} != { + id(p) for p in model_parameters}: + raise ValueError("Optimizer is not bound to the residual model") + _optimizer_state(optimizer, data["optimizer"]) + _rng_contract(data["rng"]) + + +def save_checkpoint(path, model, optimizer, identity, update, best, budget, model_config, max_bytes=None): + """Atomically save the distinct learner format; return exact serialized byte count. + + max_bytes is available capacity for the entire temporary file, not net growth + after replacement. It must be supplied by the caller's artifact ledger. + """ + _model_contract(model, model_config) + data = {"format": CHECKPOINT_FORMAT, "contract": CONTRACT, "features": FEATURE_VERSION, + "model_config": copy.deepcopy(model_config), "model": model.state_dict(), + "optimizer": optimizer.state_dict(), "optimizer_type": _optimizer_type(optimizer), + "identity": identity, "update": update, "best": best, "rng": rng_state(), + "budget_snapshot": budget, "accepted_skill": False} + _checkpoint_contract(data, model, optimizer, identity, model_config) + buffer = io.BytesIO() + torch.save(data, buffer) + payload = buffer.getvalue() + if max_bytes is not None: + if type(max_bytes) is not int or max_bytes < 0: + raise ValueError("Invalid temporary checkpoint byte budget") + if len(payload) > max_bytes: + raise RuntimeError("Insufficient artifact budget for atomic checkpoint temporary file") + path = Path(path) + path.parent.mkdir(parents=True, exist_ok=True) + temporary = None + try: + with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as stream: + temporary = Path(stream.name) + stream.write(payload) + stream.flush() + os.fsync(stream.fileno()) + os.replace(temporary, path) + finally: + if temporary is not None: + temporary.unlink(missing_ok=True) + return len(payload) + + +def restore_checkpoint(path, model, optimizer, identity, model_config): + """Validate before mutating; synthetic/future authorized resume, not pilot CLI.""" + _model_contract(model, model_config) + data = torch.load(path, map_location="cpu", weights_only=True) + _checkpoint_contract(data, model, optimizer, identity, model_config) + model.load_state_dict(data["model"]) + optimizer.load_state_dict(data["optimizer"]) + restore_rng(data["rng"]) + return data diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/reference.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/reference.py new file mode 100644 index 0000000..17855ea --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/reference.py @@ -0,0 +1,64 @@ +"""Privileged simulation reference, factored from the validated analytic verifier. + +Only explicit control-clock refresh/reset advances IK state. Observation reads never do. +""" +import torch + +from SkillFactory.legs.contact_cycle.mechanics import inverse_rotate +from SkillFactory.legs.tokens import LegKinematics + + +class ReferenceController: + def __init__(self, state, config): + self.s, self.cfg = state, config + self.fk = LegKinematics(state.bundle, state.env.device) + self.nominal = state.nominal.expand(state.env.num_envs, -1) + self.q_ik = self.nominal.clone() + self.action = torch.zeros_like(self.q_ik) + self.refreshes = torch.zeros(state.env.num_envs, dtype=torch.long, device=state.env.device) + self.kp = self.q_ik.new_tensor([j['kp'] for j in state.bundle['spec']['joints']]) + + def foot(self, q): + transform = self.fk.forward(q)[self.s.bundle['foot']['body']] + offset = q.new_tensor(self.s.bundle['foot']['xyz']) + return transform[:, :3, 3] + (transform[:, :3, :3] @ offset[:, None]).squeeze(-1) + + def jacobian(self, q): + eps = self.cfg['jacobian_epsilon_rad'] + columns = [] + for j in range(3): + delta = torch.zeros_like(q) + delta[:, j] = eps + columns.append((self.foot(q + delta) - self.foot(q - delta)) / (2 * eps)) + return torch.stack(columns, -1) + + def reset(self, ids): + self.q_ik[ids] = self.nominal[ids] + self.action[ids] = 0 + self.refreshes[ids] = 0 + self.refresh(ids) + + def refresh(self, ids): + if not len(ids): + return + s = self.s + _, _, target, normal, _ = s.reference() + pos, quat, _, _ = s.root() + q = self.q_ik[ids].clone() + local = inverse_rotate(quat[ids], target[ids] - pos[ids]) + nominal = self.nominal[ids] + for _ in range(self.cfg['ik_iterations']): + j = self.jacobian(q) + rhs = (local - self.foot(q))[:, :, None] + regularizer = torch.eye(3, device=q.device) * self.cfg['ik_damping'] + dq = j.transpose(-1, -2) @ torch.linalg.solve(j @ j.transpose(-1, -2) + regularizer, rhs) + q += dq.squeeze(-1).clamp(-self.cfg['ik_step_limit_rad'], self.cfg['ik_step_limit_rad']) + q.clamp_(min=nominal - .5, max=nominal + .5) + force = torch.zeros_like(local) + force[:, 2] = normal[ids] + torque = -(self.jacobian(q).transpose(-1, -2) + @ inverse_rotate(quat[ids], force)[:, :, None]).squeeze(-1) + scale = s.env.cfg.support_parameters['action_scale_rad'] + self.q_ik[ids] = q + self.action[ids] = ((q + torque / self.kp - nominal) / scale).clamp(-1, 1) + self.refreshes[ids] += 1 diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/replay.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/replay.py new file mode 100644 index 0000000..2e60591 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/replay.py @@ -0,0 +1,174 @@ +"""Frozen u256 inference boundary; never restore optimizer, RNG or training state.""" +import hashlib +import io +import json +import math +import sys +from pathlib import Path + +import torch + +from .config import FEATURE_VERSION, TASK_ID, load_config +from .model import SimResidualActorCritic, features + +CANDIDATE = "logs/contact_cycle/sim_residual_training_v1/prepared01/candidate_u0256.pt" +CANDIDATE_SHA256 = "c088bb3aecaca751867118858626fd508f7a3983303802d7524353f80802cf99" +MANIFEST_SHA256 = "a70a226f176104414d2da295f08cce230f6b91ed3e2b78490c78f7ffce95ab01" +MODEL_CONFIG = {"hidden": 128, "initial_std": 0.1} +CONTRACT = "go2-fl-contact-cycle-sim-residual-v1" +CHECKPOINT_FORMAT = "skillfactory.contact-cycle-sim-residual-ppo-checkpoint.v1" +REPLAY_SOURCES = ( + "scripts/replay_cycle_sim_residual.py", + "source/SkillFactory/SkillFactory/legs/contact_cycle_sim/replay.py", +) +CONTROLLERS = { + "learned": "analytic_reference_plus_learned_residual", + "reference": "analytic_reference_zero_residual", + "script": "analytic_reference_plus_scripted_residual", +} + + +def sha256(payload): + return hashlib.sha256(payload).hexdigest() + + +def canonical(value): + return json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False) + + +def _bound_file(base, relative): + path = base / relative + if (Path(relative).is_absolute() or ".." in Path(relative).parts + or not path.resolve().is_relative_to(base.resolve()) + or any(p.is_symlink() for p in (path, *path.parents) if p != base.parent)): + raise ValueError(f"Unsafe bound source path: {relative}") + return path + + +def verify_bundle(root, checkpoint=None): + """Check all immutable snapshot bytes and live inputs, not a newly generated manifest. + + AGENT.md is the sole parent-approved live-only exception: instructions are not + runtime code. Its immutable snapshot MUST still match, and drift is reported. + New replay files are separately bound; no training source is rewritten. + """ + root = Path(root).resolve() + checkpoint = Path(checkpoint) if checkpoint is not None else root / CANDIDATE + manifest = json.loads((checkpoint.parent / "manifest.json").read_text()) + if sha256(canonical(manifest).encode()) != MANIFEST_SHA256: + raise ValueError("Frozen training manifest SHA256 mismatch") + if (manifest["contract"] != CONTRACT or manifest["task_id"] != TASK_ID + or manifest["features"] != FEATURE_VERSION + or canonical(manifest["model_config"]) != canonical(MODEL_CONFIG)): + raise ValueError("Frozen manifest 46D/config identity mismatch") + exceptions = [] + for relative, expected in manifest["sources"].items(): + snapshot = _bound_file(checkpoint.parent / "source_snapshot", relative) + if sha256(snapshot.read_bytes()) != expected: + raise ValueError(f"Training source snapshot mismatch: {relative}") + live = _bound_file(root, relative) + actual = sha256(live.read_bytes()) + if actual != expected: + if relative != "AGENT.md": + raise ValueError(f"Live bound source/config mismatch: {relative}") + exceptions.append({"path": relative, "snapshot_sha256": expected, "live_sha256": actual, + "reason": "Parent-approved non-runtime instruction drift only; snapshot unchanged"}) + if relative.startswith("source/SkillFactory/SkillFactory/") and relative.endswith(".py"): + name = relative.removeprefix("source/SkillFactory/").removesuffix(".py").replace("/", ".") + name = name.removesuffix(".__init__") + loaded = sys.modules.get(name) + if loaded is not None and Path(getattr(loaded, "__file__", "")).resolve() != live.resolve(): + raise ValueError(f"Imported runtime source is outside verified tree: {name}") + cfg = manifest["training"] + task = load_config(root / cfg["task_config"]) + if task["residual_limit_rad"] != .05: + raise ValueError("Frozen residual config mismatch") + # Check before torch deserialization, including reference/script invocations. + if sha256(checkpoint.read_bytes()) != CANDIDATE_SHA256: + raise ValueError("Frozen candidate SHA256 mismatch") + return manifest, {"candidate_path": str(checkpoint.resolve()), "candidate_sha256": CANDIDATE_SHA256, + "manifest_sha256": MANIFEST_SHA256, "snapshot_files_verified": len(manifest["sources"]), + "live_source_exceptions": exceptions, + "replay_sources": {p: sha256((root / p).read_bytes()) for p in REPLAY_SOURCES}} + + +def load_frozen_policy(checkpoint, manifest): + """Deserialize the exact verified bytes on CPU; optimizer/RNG payload is ignored.""" + payload = Path(checkpoint).read_bytes() + if sha256(payload) != CANDIDATE_SHA256: + raise ValueError("Frozen candidate SHA256 mismatch") + data = torch.load(io.BytesIO(payload), map_location="cpu", weights_only=True) + identity = data.get("identity", {}) if isinstance(data, dict) else {} + if (not isinstance(data, dict) or not isinstance(identity, dict) or data.get("format") != CHECKPOINT_FORMAT + or data.get("contract") != CONTRACT or data.get("features") != FEATURE_VERSION + or type(data.get("update")) is not int or data["update"] != 256 + or data.get("accepted_skill") is not False + or canonical(data.get("model_config")) != canonical(MODEL_CONFIG) + or canonical(identity.get("manifest")) != canonical(manifest) + or sha256(canonical(manifest).encode()) != MANIFEST_SHA256 + or canonical(identity.get("model_config")) != canonical(MODEL_CONFIG) + or type(identity.get("seed")) is not int or identity["seed"] != 91501): + raise ValueError("Frozen checkpoint 46D/config/source identity mismatch (no legacy migration)") + with torch.random.fork_rng(devices=[]): + policy = SimResidualActorCritic(**MODEL_CONFIG) + expected, saved = policy.state_dict(), data.get("model") + if not isinstance(saved, dict) or set(saved) != set(expected): + raise ValueError("Frozen model state keys mismatch") + for name, reference in expected.items(): + value = saved[name] + if (not isinstance(value, torch.Tensor) or value.shape != reference.shape + or value.dtype != reference.dtype or not torch.isfinite(value).all()): + raise ValueError(f"Frozen model state shape/dtype/finite mismatch: {name}") + if name in ("offset", "scale") and not torch.equal(value, reference): + raise ValueError("Frozen 46D normalization mismatch") + policy.load_state_dict(saved, strict=True) + return policy.eval().requires_grad_(False) + + +def controller_identity(mode, script_action=None): + if mode not in CONTROLLERS: + raise ValueError("Explicit learned/reference/script controller required") + if mode == "script": + if (script_action is None or len(script_action) != 3 + or any(type(v) not in (float, int) or not -1 <= v <= 1 for v in script_action)): + raise ValueError("Script requires three finite normalized residuals in [-1,1]") + elif script_action is not None: + raise ValueError("Script action is only valid for script controller") + return {"controller_mode": mode, "controller_kind": CONTROLLERS[mode], + "trained_residual_policy": mode == "learned", "script_action_normalized": script_action, + "feature_version": FEATURE_VERSION, "task_id": TASK_ID, + "optimizer_steps": 0, "accepted_skill": False, "independent_test_executed": False} + + +@torch.inference_mode() +def inference_action(observation, mode, policy=None, script_action=None): + controller_identity(mode, script_action) + x = features(observation) + if mode == "learned": + if policy is None: + raise ValueError("Missing learned policy; reference fallback is forbidden") + normalized = ((x - policy.offset) / policy.scale).clamp(-10, 10) + mean = policy.actor(normalized) + std = policy.log_std.clamp(math.log(.02), math.log(.5)).exp() + action = mean.tanh() + if not all(bool(torch.isfinite(v).all()) for v in (action, mean, std)): + raise ValueError("Nonfinite frozen policy output") + return action, mean, std + if policy is not None: + raise ValueError("Non-learned controller cannot receive learned policy") + action = torch.zeros(len(x), 3, dtype=torch.float32, device=x.device) + if mode == "script": + action[:] = action.new_tensor(script_action) + return action, None, None + + +def create_output(root, output, checkpoint): + output = Path(output).resolve() + # No replay artifact may be placed inside any historical training or release tree. + protected = (Path(root).resolve() / "releases", + Path(root).resolve() / "logs/contact_cycle/sim_residual_training_v1", + Path(checkpoint).resolve().parent) + if any(output.is_relative_to(p) for p in protected): + raise ValueError("Replay output must be independent of frozen/historical artifacts") + output.mkdir(parents=True, exist_ok=False) + return output diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_binding_v2.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_binding_v2.py new file mode 100644 index 0000000..035b639 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_binding_v2.py @@ -0,0 +1,136 @@ +"""Finite RootXY training provenance, not an evaluation/P1/P2 binding. + +Uses the reviewed lossless config and installed import provenance mechanisms. +No interpreter-wide attestation: only explicit source roles/config references and +installed namespaces are bound. Incidental third-party imports are not traversed. +""" +from pathlib import Path + +FORMAT = 'skillfactory.rootxy-training-binding.v2' +PROFILE = 'rootxy-v2-7200s-1GiB-73460calls' +SCRIPT = 'scripts/train_cycle_sim_rootxy_v2.py' +ROLES = ( + 'SkillFactory.legs.contact_cycle_sim.rootxy_training_v2', + 'SkillFactory.legs.contact_cycle_sim.rootxy_runner_v2', + 'SkillFactory.legs.contact_cycle_sim.rootxy_binding_v2', + 'SkillFactory.legs.contact_cycle_sim.rootxy_runtime_v2', + 'SkillFactory.legs.contact_cycle_sim.rootxy_selection_v2', + 'SkillFactory.legs.contact_cycle_sim.training', + 'SkillFactory.legs.contact_cycle_sim.ppo', + 'SkillFactory.legs.contact_cycle_sim.model', + 'SkillFactory.legs.contact_cycle_sim.replay', + 'SkillFactory.tasks.manager_based.contact_cycle_sim.rootxy_cfg_v2', + 'SkillFactory.tasks.manager_based.contact_cycle_sim.rootxy_recording_v2', + 'SkillFactory.tasks.manager_based.contact_cycle_sim.env', + 'SkillFactory.tasks.manager_based.contact_cycle_sim.mdp', + 'SkillFactory.tasks.manager_based.contact_cycle.recording', + 'audit_contact_cycle_functional', 'rootxy_launch_v2', 'evaluate_cycle_sim_residual', '__main__', +) +HOOKS = {'physics': 'RootXYPhysicsHooks.class_step_before_after', + 'safety': 'PhysicalSafetyAudit.scene_update_500Hz_including_resets', + 'learner': 'rootxy_runner_v2.RuntimeHooks', + 'cleanup': 'OwnedProcesses.pidfd_subreaper_waited_exit', + 'candidate': 'supervisor_only_after_postexit_and_cleanup'} + + +def sources(root, binding): + names = set(ROLES) + result = {} + for name in sorted(names): + path = binding._module_file(name) + if name.startswith('SkillFactory.'): + expected = root / 'source/SkillFactory' / (name.replace('.', '/') + '.py') + else: + expected = root / (SCRIPT if name == '__main__' else 'scripts/' + name + '.py') + if path != expected: + raise ValueError('RootXY source-role path mismatch: ' + name) + result[name] = {'path': str(path), 'sha256': binding.sha256(path.read_bytes())} + return result, names + + +def components(root, cfg): + from . import evaluation_binding as binding + records, names = sources(Path(root), binding) + roles = set(names) + encoded = binding._encode(cfg, names, set()) + imports = binding._imports(names, Path(root)) + return binding, records, roles, encoded, imports + + +def propose_inventory(root, cfg, native_layouts): + """Preparation proposal only; later grant binds the exact unchanged bytes.""" + b, _, roles, _, imports = components(root, cfg) + required = b.required_dependencies(set(imports), roles) + modules = {name: b._dependency_module_identity(name) for name in sorted(required)} + roots = {name: Path(modules[name]['path']).parent for name in b.NUMERICAL_ROOTS} + artifacts = {} + + def add(path, owner, vendor=False): + path = b._file(path) + label = str(path) + if label in artifacts: + raise ValueError('Duplicate native artifact: ' + label) + artifacts[label] = {'module': owner, 'path': label, 'sha256': b.sha256(path.read_bytes()), + 'purpose': 'Installed numerical implementation consumed by ' + owner} + if vendor: + artifacts[label]['vendor'] = 'NVIDIA' + + for owner, directories in native_layouts(roots).items(): + for directory in directories: + paths = sorted(directory.glob('*.so*')) + if not paths: + raise ValueError('Missing native layout: ' + str(directory)) + for path in paths: + add(path, owner) + if (roots['torch'] / 'lib/libtorch_cuda.so').is_file(): + for package in b.NVIDIA_CUDA_PACKAGES: + for path in sorted((roots['torch'].parent / 'nvidia' / package / 'lib').glob('*.so*')): + add(path, 'torch', True) + inventory = {'scope': b.INVENTORY_SCOPE, 'modules': modules, 'artifacts': artifacts} + return b._dependency_inventory(inventory, set(imports), roles) + + +def normalization_updates(cfg): + from . import evaluation_binding as binding + return binding._typed_normalization_updates(cfg, + 'SkillFactory.tasks.manager_based.contact_cycle_sim.rootxy_cfg_v2', + 'RootXYContactCycleEnvCfg', 128) + + +def normalize_config(cfg): + """Exact128-env RootXY PREconstruction normalization; never runtime repair.""" + for obj, field, value in normalization_updates(cfg): + setattr(obj, field, value) + + +def snapshot(root, output, cfg, inventory, manifest): + from . import rootxy_training_v2 as p + from SkillFactory.tasks.manager_based.contact_cycle_sim.rootxy_cfg_v2 import RootXYContactCycleEnvCfg + b, records, roles, encoded, imports = components(root, cfg) + if (type(cfg) is not RootXYContactCycleEnvCfg or type(cfg.seed) is not int + or cfg.seed not in (92511, 93511) or cfg.scene.num_envs != 128 + or cfg.task_identity != p.TASK_ID or normalization_updates(cfg)): + raise ValueError('RootXY actual config/seed/normalization mismatch') + if not p.exact(manifest, p.make_manifest(root, Path(root) / p.CONFIG_PATH)): + raise ValueError('RootXY preregistration changed') + # The ONLY temporary override, validated above; never normalize other drift. + encoded['fields']['mapping']['seed'] = {'override': '92511_train_or_93511_development_only'} + _, frozen = p.replay.verify_bundle(root) + return {'format': FORMAT, 'profile': PROFILE, 'root': str(Path(root).resolve()), + 'artifact_root': str(Path(output).resolve()), 'task_id': p.TASK_ID, + 'hooks': HOOKS, 'preregistration_sha256': p.digest(manifest), + 'sources': records, 'imports': imports, 'effective_config': encoded, + 'dependency_inventory': b._dependency_inventory(inventory, set(imports), roles), + 'runtime_layout_pins': dict(b.RUNTIME_LAYOUT_PINS), + 'required_runtime_roles': dict(b.REQUIRED_RUNTIME), + 'scope': b.PROVENANCE_SCOPE, 'limitations': b.PROVENANCE_LIMITATIONS, + 'frozen_weights_only_source': frozen, 'accepted_skill': False, + 'execution_authorized': False} + + +def verify(expected, **arguments): + from . import rootxy_training_v2 as p + actual = snapshot(**arguments) + if not p.exact(expected, actual): + raise ValueError('RootXY training binding drift (no postconstruction repair)') + return actual diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_diagnostics_v2.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_diagnostics_v2.py new file mode 100644 index 0000000..6d05484 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_diagnostics_v2.py @@ -0,0 +1,89 @@ +"""Bounded, streaming first-episode XY diagnostics at PHYSICS frequency. + +No 30mm termination: breach is latched even after recovery. Partial intervals +never become full-episode RMS evidence. No physics-frequency full-state log. +""" +import torch + + +class RootXYMetrics: + def __init__(self, num_envs, device="cpu"): + self.num_envs = num_envs + self.device = torch.device(device) + self.count = torch.zeros(num_envs, dtype=torch.long, device=device) + self.live = torch.ones(num_envs, dtype=torch.bool, device=device) + self.clock_ok = self.live.clone() + self.breached = ~self.live + self.safety_ok = self.live.clone() + self.first_breach = torch.full((num_envs,), -1, dtype=torch.long, device=device) + self.maximum = torch.zeros(num_envs, dtype=torch.float64, device=device) + self.sum_square = self.maximum.clone() + self.uncapped_xy_sum = self.maximum.clone() + self.cap_count = self.count.clone() + self.reference_saturated = self.count.clone() + self.authority_shortfall = self.maximum.clone() + self.phase_count = torch.zeros(num_envs, 6, dtype=torch.long, device=device) + self.phase_square = torch.zeros(num_envs, 6, dtype=torch.float64, device=device) + + def tick(self, *, time, position_error, phase, reference, request, effective_residual, safety_ok): + n = self.num_envs + for name, value, shape in (("time", time, (n,)), ("position_error", position_error, (n, 3)), + ("phase", phase, (n,)), ("reference", reference, (n, 3)), + ("request", request, (n, 3)), ("effective_residual", effective_residual, (n, 3)), + ("safety_ok", safety_ok, (n,))): + if (not isinstance(value, torch.Tensor) or value.shape != shape or value.device != self.device + or not torch.isfinite(value).all()): + raise ValueError("Invalid physics-tick " + name) + if (phase.dtype != torch.long or safety_ok.dtype != torch.bool or (phase < 0).any() or (phase > 5).any() + or (reference.abs() > 1).any() or (request.abs() > 1).any() + or (effective_residual.abs() > .05 + 1e-6).any()): + raise ValueError("Invalid physics phase/action/safety contract") + live = self.live + if (self.count[live] >= 4500).any(): + raise ValueError("Extra first-episode physics ticks") + self.count += live.long() + self.clock_ok &= ~live | ((time.double() - self.count * .002).abs() <= 1e-5) + square = position_error[:, :2].double().square().sum(-1) + xy = square.sqrt() + hit = live & (xy > .03) + self.first_breach[hit & ~self.breached] = self.count[hit & ~self.breached] + self.breached |= hit + self.maximum = torch.maximum(self.maximum, torch.where(live, xy, 0.)) + self.sum_square += square * live + uncapped_xy = square / .03**2 + self.uncapped_xy_sum += uncapped_xy * live + self.cap_count += (live & (uncapped_xy + (position_error[:, 2].double() / .04).square() >= 10)).long() + self.reference_saturated += (live & (reference.abs() >= 1).any(-1)).long() + shortfall = (request.double() * .05 - effective_residual.double()).abs().amax(-1) + self.authority_shortfall = torch.maximum(self.authority_shortfall, torch.where(live, shortfall, 0.)) + self.safety_ok &= ~live | safety_ok + ids = live.nonzero().flatten() + self.phase_count[ids, phase[ids]] += 1 + self.phase_square[ids, phase[ids]] += square[ids] + + def end_control(self, terminated, truncated): + for value in (terminated, truncated): + if value.shape != self.live.shape or value.dtype != torch.bool or value.device != self.device: + raise ValueError("Invalid first-episode terminal flags") + self.live &= ~(terminated | truncated) + + def rows(self): + if (self.count == 0).any(): + raise ValueError("Missing physics-tick diagnostics") + rows = [] + for e in range(self.num_envs): + count = int(self.count[e]) + rows.append({"env": e, "physics_samples": count, + "complete_clock": bool(count == 4500 and self.clock_ok[e] and not self.live[e]), + "rootxy_rms_m": float((self.sum_square[e] / count).sqrt()), + "rms_scope": "full_episode" if count == 4500 else "observed_prefix_only", + "rootxy_max_m": float(self.maximum[e]), "rootxy_breached": bool(self.breached[e]), + "first_rootxy_breach_tick": int(self.first_breach[e]) if self.breached[e] else None, + "uncapped_xy_cost_mean": float(self.uncapped_xy_sum[e] / count), + "position_cap_fraction": float(self.cap_count[e] / count), + "reference_saturated_fraction": float(self.reference_saturated[e] / count), + "effective_authority_shortfall_max_rad": float(self.authority_shortfall[e]), + "phase_physics_samples": self.phase_count[e].cpu().tolist(), + "phase_xy_square_sum_m2": self.phase_square[e].cpu().tolist(), + "safety_ok": bool(self.safety_ok[e])}) + return rows diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_runner_v2.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_runner_v2.py new file mode 100644 index 0000000..6019e28 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_runner_v2.py @@ -0,0 +1,224 @@ +"""Reusable learner body, NOT a launcher or authorization adapter. + +The future sole-owner supervisor supplies authenticated admission, native monitor, +BOOTTIME/physical-call debit instrumentation, installed/full actual-cfg identity, +cooperative cancellation and bounded cleanup. Nothing here spawns a subprocess. +""" +import hashlib +import json +import traceback +from dataclasses import dataclass +from pathlib import Path +from typing import Callable + +import torch + +from . import rootxy_training_v2 as protocol +from .model import features +from .replay import inference_action +from .training import isolated_validation_rng, optimize_reserved_rollout, save_trace_subset + + +@dataclass(frozen=True) +class RuntimeHooks: + # All hooks are mandatory. The adapter must instrument actual sim.step calls + # separately: a control call is NOT a physics tick or 128 physical calls. + admit: Callable[[dict], dict] + guard: Callable[[], None] + verify_identity: Callable[[], None] + before_control: Callable[[str], None] + after_control: Callable[[str], None] + progress: Callable[[dict], None] + finish: Callable[[dict], None] # notification only; owner closes env/app, waits, verifies cleanup + + +class _ControlledEnv: + def __init__(self, env, hooks, guard): + self.env, self.hooks, self.guard = env, hooks, guard + self.kind = 'optimization' + + def __getattr__(self, name): + return getattr(self.env, name) + + def step(self, action): + self.guard() + self.hooks.before_control(self.kind) # durable adapter debit before attempted call + result = self.env.step(action) + self.hooks.after_control(self.kind) + self.guard() + return result + + +def execute(env, root, output, manifest, ledger, observations, hooks, evaluate): + """Future admitted worker API. This function is not reached by the staged CLI. + + observations are saved46D CPU parity rows, not a request for simulator inference. + evaluate is the unchanged audit_contact_cycle_functional.evaluate callable. + Outputs: bounded JSON result/progress, six validations, at most five eval-only + checkpoints and four compact trajectories per validation; no rollout snapshots. + """ + root, output = Path(root), Path(output) + cfg = protocol.load_training_config(root / protocol.CONFIG_PATH, root) + if not protocol.exact(manifest, protocol.make_manifest(root, root / protocol.CONFIG_PATH)): + raise ValueError('Stale RootXY-v2 manifest') + # Admission is delegated, not inferred from a training config or old grant. + admission = hooks.admit(manifest) + if (not isinstance(admission, dict) or admission.get('manifest_sha256') != protocol.digest(manifest) + or admission.get('p2_review_approved') is not True + or admission.get('sole_cleanup_owner') is not True + or admission.get('native_monitor_active') is not True): + raise PermissionError('Missing supervised RootXY-v2 admission') + from SkillFactory.tasks.manager_based.contact_cycle_sim.rootxy_cfg_v2 import verify_actual_config + from SkillFactory.tasks.manager_based.contact_cycle_sim.rootxy_recording_v2 import RootXYRecorder + + result = {'format': 'skillfactory.contact-cycle-rootxy-result.v2', 'training_complete': False, + 'accepted_skill': False, 'candidate': None, 'status': 'started', + 'candidate_authority': 'none; supervisor waited-exit/postexit-identity/cleanup lock required', + 'checkpoints_selectable': False} + provisional_candidate = None + + def invalidate(): + result.update(status='failed', candidate=None, training_complete=False, + checkpoints_selectable=False, error=traceback.format_exc()[-16384:]) + result.pop('outcome', None) + + def guard(): + ledger.work_guard() + hooks.guard() # stop request, same-boot deadline, native aggregate storage, ownership, physical safety + + def check_identity(): + guard() + hooks.verify_identity() + verify_actual_config(env) + if protocol.source_identity(root, root / protocol.CONFIG_PATH) != manifest['sources']: + raise RuntimeError('Bound sources changed') + protocol.v1.verify_snapshot(output, manifest) + + controlled = _ControlledEnv(env, hooks, guard) + try: + check_identity() + model, optimizer, frozen, imported = protocol.import_actor_weights_only(root, observations) + if optimizer.state: + raise ValueError('Warm-start optimizer must be empty') + model.to(env.device) + frozen.to(env.device) + # Bind after transfer even if framework device conversion replaces Parameters. + optimizer = torch.optim.Adam(model.parameters(), lr=cfg['algorithm']['learning_rate'], + eps=cfg['algorithm']['adam_eps']) + identity = {'task_id': protocol.TASK_ID, 'contract': protocol.CONTRACT, + 'manifest_sha256': protocol.digest(manifest), 'resolved_task': manifest['resolved_task'], + 'source_sha256': protocol.replay.CANDIDATE_SHA256, 'seed': 92511} + ledger.write_json(output / 'warmstart.json', imported) + functional = json.loads((root / cfg['functional_config']).read_text()) + baseline_pair = None + + def save(update): + check_identity() + path = output / f'checkpoint_u{update:04d}.pt' + remaining = cfg['max_artifact_bytes'] - protocol.artifact_bytes(output) - 1048576 + sha = protocol.save_checkpoint(path, model, identity, update, imported, ledger.data, remaining) + guard() + return {'path': path.name, 'sha256': sha, 'update': update} + + @torch.no_grad() + def validate(label, policy): + nonlocal baseline_pair + check_identity() + ledger.begin_validation(label) + controlled.kind = 'validation' + with isolated_validation_rng(env, cfg['validation_seed'], cfg['seeds'][0]): + observation = features(env.reset()[0]['policy']) + with RootXYRecorder(env, functional, policy is not None) as recorder: + if baseline_pair is None: + baseline_pair = recorder.initial_pair + elif any(not torch.equal(value, baseline_pair[key]) + for key, value in recorder.initial_pair.items()): + raise RuntimeError('Validation initial observable pairing mismatch (not full-state proof)') + returns = torch.zeros(env.num_envs, device=env.device) + for step in range(900): + ledger.before_validation_control() + action, mean, std = inference_action(observation, 'learned' if policy is not None + else 'reference', policy) + recorder.before_control(observation, action, mean, std) + live = recorder.metrics.live.clone() + obs, reward, terminated, truncated, _ = controlled.step(action) + ledger.after_validation_control() + if not torch.isfinite(reward).all(): + raise RuntimeError('Nonfinite validation reward') + returns += reward * live + recorder.end_control(terminated, truncated, step == 899) + observation = features(obs['policy']) + rows = recorder.rows(evaluate) + for row, total in zip(rows, returns.cpu().tolist()): + row['episode_return'] = total + ledger.write_json(output / f'validation_{label}.json', { + 'label': label, 'validation_seed': 93511, 'rows': rows, 'accepted_skill': False}) + selected = ([r['env'] for r in rows if not r['success'] or r['rootxy_breached']] + or list(range(env.num_envs)))[:4] + save_trace_subset(output / f'validation_{label}_selected.pt', recorder.columns, selected, + recorder.stops, lambda size: ledger.guard(reserve_bytes=size + 1048576)) + if len(rows) != 128 or not all(r['success'] for r in rows): + raise RuntimeError('Original functional128/128 regression: stop, no retry') + check_identity() + controlled.kind = 'optimization' + return rows + + save(0) + validate('analytic_zero', None) + validate('frozen_u256', frozen) + ledger.write_json(output / 'warmstart_u0_equivalence.json', { + 'physical_evaluation': False, 'equivalent_actor': 'frozen_u256', 'evidence': imported, + 'note': 'Exact CPU actor equivalence only, not physical repeat/full-state equivalence'}) + best_key = None + algo = {**cfg['algorithm'], 'horizon': cfg['horizon']} + x = features(env.reset()[0]['policy']) + for update in range(1, 257): + x, failures, timeouts, learning = optimize_reserved_rollout( + controlled, model, optimizer, x, algo, 92511, update, ledger, guard, check_identity) + if update in cfg['validation_updates']: + rows = validate(f'u{update:04d}', model) + key = protocol.candidate_key(rows, update) + checkpoint = save(update) + if key is not None and (best_key is None or key < best_key): + best_key = key + provisional_candidate = {**checkpoint, 'selection_key': list(key)} + x = features(env.reset()[0]['policy']) + # No resume/latest is offered. Pending interrupted work remains fully debited. + ledger.commit_update(92511, update) + progress = {'format': 'skillfactory.contact-cycle-rootxy-progress.v2', 'update': update, + 'optimization_reserved_environment_transitions': ledger.data['reserved_transitions'], + 'validation_reserved_environment_transitions': 806400, + 'failures': failures, 'timeouts': timeouts, 'learner': learning, 'accepted_skill': False} + ledger.append_json(output / 'progress.jsonl', progress) + hooks.progress(progress) + check_identity() + if provisional_candidate is not None: + candidate = provisional_candidate + if hashlib.sha256((output / candidate['path']).read_bytes()).hexdigest() != candidate['sha256']: + raise ValueError('Selected checkpoint changed') + result.update(training_complete=True, status='completed_provisional', candidate=provisional_candidate, + outcome='provisional_candidate' if provisional_candidate is not None else 'no_candidate') + except BaseException: + invalidate() + raise + finally: + # No worker lock is authoritative, even on success. Retained checkpoints + # are forensic/nonselectable until the external owner commits its own lock. + result['budget'] = ledger.data + try: + try: + ledger.write_json(output / 'result.json', result) + except BaseException: + invalidate() + # Failure publication is not a learner retry. Persistent storage + # failure still propagates; the owner must treat missing evidence as fatal. + ledger.write_json(output / 'result.json', result) + raise + finally: + try: + hooks.finish(result) + except BaseException: + invalidate() + ledger.write_json(output / 'result.json', result) + raise + return result diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_runtime_v2.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_runtime_v2.py new file mode 100644 index 0000000..2ce49db --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_runtime_v2.py @@ -0,0 +1,191 @@ +"""RootXY-only lifetime hooks; original environment and safety source stay intact.""" +from contextlib import ExitStack, contextmanager +from functools import wraps + + +class RootXYPhysicsHooks: + """Instrument actual shared sim.step calls, including constructor and reset. + + Reservation is 72,460 rollout + 1,000 initialization calls, never a refund. + Attempted calls are charged even when sim.step throws. Postconstruction every + completed call MUST have one original PhysicalSafetyAudit scene-update sample + before another step/work boundary. Unsupported reset coverage is fatal. + """ + def __init__(self, simulation_class, environment_class, guard, publish, *, preparation=False): + self.simulation_class, self.environment_class = simulation_class, environment_class + self.guard, self.publish, self.preparation = guard, publish, preparation + self.attempted = self.completed = self.initialization = self.rollout = 0 + self.mode = 'initialization' + self.audit = None + self.audit_start = 0 + self.patches = [] + self.active = False + + def report(self): + return {'attempted_shared_calls': self.attempted, 'completed_shared_calls': self.completed, + 'initialization_attempted_shared_calls': self.initialization, + 'rollout_attempted_shared_calls': self.rollout, + 'initialization_cap': 1000, 'rollout_reserved_shared_calls': 72460, + 'total_cap': 73460, 'reservation_unused_is_not_refunded': True, + 'startup_completed_without_task_audit': self.audit_start, + 'safety': None if self.audit is None else self.audit.report()} + + def safety_boundary(self): + if self.audit is not None: + if self.audit.samples != self.completed - self.audit_start: + raise RuntimeError('RootXY missing/duplicate 500Hz safety sample, including reset') + self.audit.check() + + def attach_audit(self, audit): + if self.audit is not None: + raise RuntimeError('Duplicate physics audit') + self.audit, self.audit_start = audit, self.completed + if audit.samples != 0: + raise RuntimeError('Audit must begin before first training/reset action') + + def patch(self, owner, name, new): + self.patches.append((owner, name, getattr(owner, name), name in vars(owner))) + setattr(owner, name, new) + + @contextmanager + def scope(self, mode): + old = self.mode + self.mode = mode + try: + yield + finally: + self.mode = old + + def __enter__(self): + if self.active: + raise RuntimeError('Duplicate physics hook owner') + self.active = True + original = self.simulation_class.step + + @wraps(original) + def step(sim, *args, **kwargs): + self.guard() + self.safety_boundary() + if self.preparation: + raise PermissionError('PREP is config-only: sim.step forbidden') + if self.mode not in ('initialization', 'rollout'): + raise RuntimeError('Unowned physics call') + count, cap = ((self.initialization, 1000) if self.mode == 'initialization' + else (self.rollout, 72460)) + if count >= cap or self.attempted >= 73460: + raise RuntimeError('RootXY shared physics call cap') + self.attempted += 1 + if self.mode == 'initialization': + self.initialization += 1 + else: + self.rollout += 1 + # Bounded atomic publication BEFORE an actual call, including partial failure. + self.publish(self.report()) + result = original(sim, *args, **kwargs) + self.completed += 1 + self.publish(self.report()) + self.guard() + return result + + self.patch(self.simulation_class, 'step', step) + for name in ('reset', '_reset_idx'): + old = getattr(self.environment_class, name) + + @wraps(old) + def reset(*args, _old=old, **kwargs): + self.guard() + self.safety_boundary() + with self.scope('initialization'): + result = _old(*args, **kwargs) + self.safety_boundary() + self.guard() + return result + + self.patch(self.environment_class, name, reset) + return self + + def __exit__(self, *_): + for owner, name, old, own in reversed(self.patches): + if own: + setattr(owner, name, old) + else: + delattr(owner, name) + self.patches.clear() + self.active = False + + +def worker_lifecycle(api, *, prepare, guard, verify, publish): + """One owner from partial startup through env.close and app.close. + + api.start must publish app ownership as soon as acquired (including a partially + constructed launcher). No result can authorize a candidate. A missing close, + verification, or final publication is a failed worker even after learner success. + """ + result = {'status': 'failed', 'accepted_skill': False, 'training_complete': False, + 'candidate': None, 'postclose_identity_verified': False} + error = None + stack = ExitStack() + try: + guard() + api.start() + guard() + physics = stack.enter_context(api.physics()) + api.physics_hooks = physics + verify() + if prepare: + result = api.prepare() + if physics.attempted: + raise RuntimeError('PREP observed physics') + else: + api.construct() # owner retains partially initialized object for close + verify() # immediately postconstructor, BEFORE reset/actions + api.check_actual() + with api.safety() as audit: + physics.attach_audit(audit) + result = api.learn() + physics.safety_boundary() + except BaseException as caught: + error = caught + finally: + try: + if api.physics_hooks is not None: + api.physics_hooks.mode = 'closed' + api.close_env() + except BaseException as caught: + error = error or caught + try: + if api.started: + verify() # actual same-object cfg after env.close, before app.close + result['postclose_identity_verified'] = True + except BaseException as caught: + error = error or caught + try: + if api.physics_hooks is not None: + result['physics'] = api.physics_hooks.report() + except BaseException as caught: + error = error or caught + if error is not None: + result.update(status='failed', training_complete=False, candidate=None, + checkpoints_selectable=False, error=str(error)[-8192:]) + try: + publish(result) + except BaseException as caught: + error = error or caught + finally: + try: + api.close_app(0 if error is None else 1) + except BaseException as caught: + error = error or caught + finally: + try: + stack.close() # closed-mode sim.step guard covers app teardown too + except BaseException as caught: + error = error or caught + if error is not None: + # app.close can itself fail after earlier provisional publication. + result.update(status='failed', training_complete=False, candidate=None, + checkpoints_selectable=False, error=str(error)[-8192:]) + publish(result) + if error is not None: + raise error + return result diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_selection_v2.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_selection_v2.py new file mode 100644 index 0000000..6e8a031 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_selection_v2.py @@ -0,0 +1,36 @@ +"""Shared stdlib candidate gates for learner and independent supervisor.""" +import math + + +def tracking_cost(rows): + # Exactly the successful-row branch of the original PPO selection composite. + scores = [] + for row in rows: + values = [row.get(key) for key in ("height_rms_m", "swing_rms_m", "landing_error_m")] + if any(type(v) not in (int, float) or not math.isfinite(v) or v < 0 for v in values): + raise ValueError("Missing/nonfinite/negative successful validation metric") + scores.append(sum(min(v / scale, 2.) for v, scale in zip(values, (.06, .025, .05)))) + return sum(scores) / len(rows) + + +def candidate_key(rows, update): + if type(update) is not int or update not in (64, 128, 192, 256): + raise ValueError("Only u64/u128/u192/u256 are selectable") + if len(rows) != 128 or [r.get("env") for r in rows] != list(range(128)): + raise ValueError("Require all128 unique ordered validation rows") + if any(r.get("success") is not True for r in rows): + raise RuntimeError("Original functional regression: stop trial") + for row in rows: + for key in ("rootxy_rms_m", "rootxy_max_m"): + value = row.get(key) + if type(value) not in (float, int) or not math.isfinite(value) or value < 0: + raise ValueError("Missing/nonfinite XY diagnostics") + if (type(row.get("physics_samples")) is not int or row["physics_samples"] != 4500 + or row.get("complete_clock") is not True + or type(row.get("rootxy_breached")) is not bool or row.get("safety_ok") is not True): + raise ValueError("Missing/partial physics-tick evidence or safety violation") + if row["rootxy_rms_m"] > row["rootxy_max_m"] + 1e-12: + raise ValueError("Inconsistent RMS/max") + if any(r["rootxy_breached"] or r["rootxy_max_m"] > .03 for r in rows): + return None + return sum(r["rootxy_rms_m"] for r in rows) / 128, tracking_cost(rows), update diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_training_v2.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_training_v2.py new file mode 100644 index 0000000..ef50817 --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_training_v2.py @@ -0,0 +1,346 @@ +"""Additive RootXY-v2 protocol; no simulator imports and no launch authorization. + +The additive supervised adapter owns launch admission; CPU identity is not a grant. +V1 source/checkpoint identities and restore behavior remain untouched. +""" +import copy +import hashlib +import io +import json +import math +import os +import random +import shutil +import tempfile +from pathlib import Path + +import torch + +from . import replay, training as v1 +from .config import FEATURE_VERSION, load_config +from .model import SimResidualActorCritic, features +from .ppo import _finite_tree, _model_state +from .rootxy_selection_v2 import candidate_key +from ..contact_cycle.training import BudgetExceeded, atomic_json, digest +from .evaluation_budget import artifact_size as artifact_bytes + +TASK_ID = "SkillFactory-Go2FL-ContactCycleSimRootXY-v2" +CONTRACT = "go2-fl-contact-cycle-sim-rootxy-v2" +CONFIG_PATH = "configs/contact_cycle/sim_training_rootxy_v2.json" +TASK_PATH = "configs/contact_cycle/simulation_residual_rootxy_v2.json" +PROPOSAL_PATH = "configs/contact_cycle/sim_training_rootxy_proposal_v2.json" +PLAN_PATH = "docs/contact_cycle_rootxy_v2_plan.md" +BASE_PATH = "configs/contact_cycle/simulation_residual_v1.json" +BASE_SHA256 = "a2352c197bd6e13a2a729792657b26da120fd2d295d00f205eca8154aed82628" +MODEL_CONFIG = dict(v1.MODEL_CONFIG) +CHECKPOINT_FORMAT = "skillfactory.contact-cycle-sim-rootxy-checkpoint.v2" +TASK = { + "format": "skillfactory.contact-cycle-sim-rootxy.v2", "task_id": TASK_ID, + "features": FEATURE_VERSION, "base_config": BASE_PATH, "base_sha256": BASE_SHA256, + "overrides": {"reward.position_scale_m": [.03, .03, .04]}, +} +PILOT = { + **copy.deepcopy(v1.PILOT), "format": "skillfactory.contact-cycle-sim-rootxy-training.v2", + "contract": CONTRACT, "task_id": TASK_ID, "proposal": PROPOSAL_PATH, "task_config": TASK_PATH, + "seeds": [92511], "validation_seed": 93511, "holdout_seed": 95511, + "validation_updates": [64, 128, 192, 256], "max_total_wall_s": 7200, + "cleanup_reserve_s": 120, "max_validation_transitions": 806400, + "source_checkpoint": replay.CANDIDATE, "source_sha256": replay.CANDIDATE_SHA256, + "source_manifest_sha256": replay.MANIFEST_SHA256, +} +PROPOSAL = { + "format": "skillfactory.contact-cycle-sim-rootxy-proposal.v2", "contract": CONTRACT, + "actual_training_launches": 1, "resume": False, "accepted_skill": False, + "initialization": "actor_log_std_exact_normalization_only_fresh_critic_Adam_RNG", + "baselines": ["analytic_zero", "frozen_u256"], + "warmstart_u0": "CPU_exact_actor_equivalence_not_physical_repeat_or_full_state_equivalence", + "validation_reservation": "128*900*7_no_refund_actual_two_baselines_plus_four_development", + "selection": "all128_original_functional_and_full4500ticks_XY_le_0.03_then_mean_full_RMS_tracking_earliest", + "termination": "original_safety_only_no_early_30mm_termination", + "scope": "independent_nominal_9s_single_cycle_not_continuous_learning", + "launch_barrier": "P2_review_then_supervised_adapter_review_then_fresh_parent_manifest_hash_grant", + "native_storage": "aggregate_new_owned_1GiB_MONITOR_and_stop_not_hard_quota", + "downstream": "holdout_P1_P2_require_separate_grants_not_remaining_pilot_budget", +} + + +def exact(actual, expected): + return json.dumps(actual, sort_keys=True, allow_nan=False) == json.dumps(expected, sort_keys=True, allow_nan=False) + + +def load_task_config(path, root): + if not exact(json.loads(Path(path).read_text()), TASK): + raise ValueError("RootXY-v2 identity/override whitelist mismatch") + base = Path(root) / BASE_PATH + if hashlib.sha256(base.read_bytes()).hexdigest() != BASE_SHA256: + raise ValueError("Frozen base config changed") + resolved = load_config(base) + resolved.update(format=TASK["format"], task_id=TASK_ID, + scope=PROPOSAL["scope"]) + resolved["reward"]["position_scale_m"] = [.03, .03, .04] + return resolved + + +def load_training_config(path, root): + cfg = json.loads(Path(path).read_text()) + if not exact(cfg, PILOT): + raise ValueError("RootXY-v2 fixed pilot config mismatch") + if not exact(json.loads((Path(root) / PROPOSAL_PATH).read_text()), PROPOSAL): + raise ValueError("RootXY-v2 fixed proposal mismatch") + load_task_config(Path(root) / TASK_PATH, root) + return cfg + + +def source_identity(root, config_path): + # Reuse the frozen dependency inventory, then explicitly bind every additive seam. + root = Path(root).resolve() + sources = v1.source_identity(root, v1.PILOT, root / v1.CONFIG_PATH) + paths = [Path(config_path).resolve(), root / TASK_PATH, root / PROPOSAL_PATH, root / PLAN_PATH, + root / "scripts/train_cycle_sim_rootxy_v2.py", root / "scripts/rootxy_launch_v2.py", + root / "scripts/evaluate_cycle_sim_residual.py", + root / "docs/contact_cycle_rootxy_v2_launch.md", + root / "tests/test_contact_cycle_sim_rootxy_launch_v2.py", + root / "tests/test_contact_cycle_sim_rootxy_v2.py"] + for path in paths: + if not path.is_relative_to(root) or path.is_symlink(): + raise ValueError("Unsafe RootXY-v2 identity input") + sources[str(path.relative_to(root))] = hashlib.sha256(path.read_bytes()).hexdigest() + return dict(sorted(sources.items())) + + +def make_manifest(root, config_path): + cfg = load_training_config(config_path, root) + _, source = replay.verify_bundle(root) + parity_path = Path(root) / Path(replay.CANDIDATE).parent / 'validation_u0256_trace.pt' + return {"format": "skillfactory.contact-cycle-sim-rootxy-preregistration.v2", + "contract": CONTRACT, "task_id": TASK_ID, "features": FEATURE_VERSION, + "training": cfg, "proposal": PROPOSAL, "model_config": MODEL_CONFIG, + "resolved_task": load_task_config(Path(root) / TASK_PATH, root), + "sources": source_identity(root, config_path), "warmstart_source": source, + "parity_trace": {"path": str(parity_path.resolve()), + "sha256": hashlib.sha256(parity_path.read_bytes()).hexdigest()}, + "launch_integration_complete": True, "runtime_qualified": False, "accepted_skill": False, "resume": False} + + +def writable_output(root, output): + output = Path(output).resolve() + protected = [Path(root).resolve() / "releases", (Path(root) / replay.CANDIDATE).resolve().parent.parent] + if (any(output.is_relative_to(path) for path in protected) + or output.is_relative_to(Path(root).resolve()) + and not output.is_relative_to(Path(root).resolve() / "logs/contact_cycle/rootxy_v2")): + raise ValueError("Use new rootxy_v2 outputs or external directory; frozen/historical/source paths are immutable") + return output + + +def prepare(root, config_path, output): + """Legacy CPU snapshot helper only; NOT actual Kit config preparation/admission. + + The dedicated CLI uses rootxy_launch_v2 and its distinct preparation schema. + This snapshot alone is never accepted by the supervisor. + """ + output = writable_output(root, output) + manifest = make_manifest(root, config_path) + size = sum((Path(root) / p).stat().st_size for p in manifest["sources"]) + if size + len(json.dumps(manifest).encode()) + 1048576 > PILOT["max_artifact_bytes"]: + raise BudgetExceeded("Preparation exceeds aggregate output cap") + output.mkdir(parents=True, exist_ok=False) + atomic_json(output / "manifest.json", manifest) + for relative in manifest["sources"]: + target = output / "source_snapshot" / relative + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(Path(root) / relative, target) + atomic_json(output / "preparation.json", {"prepared": True, "authorized": False, "trained": False, + "manifest_sha256": digest(manifest)}) + return manifest + + +def require_launch_admission(*_): + raise PermissionError("RootXY-v2 supervised launch integration INCOMPLETE: P2 review, reviewed admission/native " + "monitor/sole-owner cleanup adapter and fresh parent manifest-hash grant required") + + +def import_actor_weights_only(root, observations): + """CPU-only exact saved-observation parity; never restore optimizer, critic or RNG. + + This intentionally initializes NEW Python/NumPy/Torch streams with seed92511. + Model construction consumes only that new stream. Frozen loading preserves it. + """ + import numpy as np + + x = features(observations) + if x.device.type != "cpu": + raise ValueError("Warm-start parity is CPU-only") + manifest, source = replay.verify_bundle(root) + frozen = replay.load_frozen_policy(Path(root) / replay.CANDIDATE, manifest) + seed = PILOT["seeds"][0] + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + model = SimResidualActorCritic(**MODEL_CONFIG) + initial_critic = {k: v.clone() for k, v in model.critic.state_dict().items()} + saved, target = frozen.state_dict(), model.state_dict() + _model_state(saved, target) + imported = sorted(k for k in saved if k.startswith("actor.") or k in ("log_std", "offset", "scale")) + target.update({k: saved[k].clone() for k in imported}) + model.load_state_dict(target) + with torch.no_grad(): + old, new = replay.inference_action(x, "learned", frozen), replay.inference_action(x, "learned", model) + if not all(torch.equal(a, b) for a, b in zip(old, new)): + raise ValueError("Exact CPU warm-start actor/action/std parity failed") + if not all(torch.equal(v, initial_critic[k]) for k, v in model.critic.state_dict().items()): + raise ValueError("Warm-start changed fresh critic") + optimizer = torch.optim.Adam(model.parameters(), lr=PILOT["algorithm"]["learning_rate"], + eps=PILOT["algorithm"]["adam_eps"]) + evidence = {"source": source, "imported_keys": imported, + "excluded_model_keys": sorted(set(saved) - set(imported)), + "excluded_state": ["optimizer", "rng", "critic", "best", "budget_snapshot", "update"], + "parity": "exact_CPU_action_mean_std", "observations": len(x), + "observations_sha256": hashlib.sha256(x.contiguous().numpy().tobytes()).hexdigest(), + "fresh_seed": seed, "physical_repeat": False, "full_state_equivalence": False} + return model, optimizer, frozen, evidence + + + + +class RootXYBudgetLedger(v1.FreshBudgetLedger): + """Fresh debit plus separately reserved validation. Adapter supplies BOOTTIME start. + + Instantiate BEFORE Kit startup. Native monitoring and physical-call accounting + remain the supervisor adapter's responsibility; this is not a hard disk quota. + """ + FORMAT = "skillfactory.contact-cycle-rootxy-budget.v2" + + def __init__(self, *args, launch_start_s, **kwargs): + super().__init__(*args, **kwargs) + if type(launch_start_s) not in (float, int) or not math.isfinite(launch_start_s) or launch_start_s < 0: + raise ValueError("Require supervisor BOOTTIME start") + self.launch_start_s = launch_start_s + + def __enter__(self): + super().__enter__() + try: + clock = self.data["clock"] + if self.launch_start_s > clock["start_s"]: + raise ValueError("Supervisor start is in the future") + clock.update(start_s=self.launch_start_s, deadline_s=self.launch_start_s + 7200) + self.data.update(validation_reserved_transitions=806400, validation_evaluations_started=[], + validation_completed_control_calls=0, validation_attempted_control_calls=0) + self.guard() + atomic_json(self.path, self.data) + return self + except BaseException: + super().__exit__() + raise + + def guard(self, reserve_bytes=0): + # Preserve every inherited clock/debit/cap check. Its legacy size scan can + # race the supervisor's atomic heartbeat rename. Retry the entire guard, + # boundedly; never accept a partial scan or modify the frozen v1 helper. + for attempt in range(4): + try: + return super().guard(reserve_bytes=reserve_bytes) + except FileNotFoundError: + if attempt == 3: + raise BudgetExceeded('RootXY legacy artifact scan unstable after4attempts') + + def work_guard(self): + self.guard() + if self.boot_now() >= self.data["clock"]["deadline_s"] - 120: + raise BudgetExceeded("Cleanup reserve reached; no new work") + + def begin_validation(self, label): + self.work_guard() + labels = ["analytic_zero", "frozen_u256", "u0064", "u0128", "u0192", "u0256"] + used = self.data["validation_evaluations_started"] + if len(used) >= len(labels) or label != labels[len(used)]: + raise ValueError("No duplicate/reordered/retry validation") + used.append(label) + self._evaluation_calls = 0 + self.write_json(self.path, self.data) + + def before_validation_control(self): + self.work_guard() + if not hasattr(self, "_evaluation_calls") or self._evaluation_calls >= 900: + raise BudgetExceeded("Validation control-call cap") + self._evaluation_calls += 1 + self.data["validation_attempted_control_calls"] += 1 + + def after_validation_control(self): + self.data["validation_completed_control_calls"] += 1 + + +def _checkpoint_identity(identity, imported): + keys = {"task_id", "contract", "manifest_sha256", "resolved_task", "source_sha256", "seed"} + if (not isinstance(identity, dict) or set(identity) != keys or identity["task_id"] != TASK_ID + or identity["contract"] != CONTRACT or identity["source_sha256"] != replay.CANDIDATE_SHA256 + or type(identity["seed"]) is not int or identity["seed"] != 92511 + or not isinstance(identity["manifest_sha256"], str) or len(identity["manifest_sha256"]) != 64 + or any(c not in "0123456789abcdef" for c in identity["manifest_sha256"]) + or identity["resolved_task"].get("task_id") != TASK_ID + or identity["resolved_task"].get("reward", {}).get("position_scale_m") != [.03, .03, .04] + or imported.get("source", {}).get("candidate_sha256") != replay.CANDIDATE_SHA256 + or imported.get("source", {}).get("manifest_sha256") != replay.MANIFEST_SHA256 + or imported.get("parity") != "exact_CPU_action_mean_std" or imported.get("fresh_seed") != 92511): + raise ValueError("RootXY-v2 checkpoint provenance mismatch") + + +def save_checkpoint(path, model, identity, update, import_evidence, budget, max_bytes): + """New eval-only artifact; no resume payload. Exact temporary capacity reserved.""" + if (identity.get("task_id") != TASK_ID or identity.get("contract") != CONTRACT + or type(update) is not int or update not in [0, *PILOT["validation_updates"]]): + raise ValueError("RootXY-v2 checkpoint identity/update mismatch") + _checkpoint_identity(identity, import_evidence) + with torch.random.fork_rng(devices=[]): + expected = SimResidualActorCritic(**MODEL_CONFIG) + _model_state(model.state_dict(), expected.state_dict()) + data = {"format": CHECKPOINT_FORMAT, "task_id": TASK_ID, "contract": CONTRACT, + "features": FEATURE_VERSION, "model_config": MODEL_CONFIG, + "model": {k: v.detach().cpu() for k, v in model.state_dict().items()}, + "identity": identity, "update": update, "import_evidence": import_evidence, + "budget_snapshot": copy.deepcopy(budget), "accepted_skill": False, "resume": False} + _finite_tree(data) + stream = io.BytesIO() + torch.save(data, stream) + payload = stream.getvalue() + if type(max_bytes) is not int or max_bytes < len(payload): + raise BudgetExceeded("Atomic checkpoint temporary capacity exceeded") + path = Path(path) + if path.exists(): + raise FileExistsError("Never overwrite checkpoint evidence") + temporary = None + try: + with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as out: + temporary = Path(out.name) + out.write(payload) + out.flush() + os.fsync(out.fileno()) + os.replace(temporary, path) + finally: + if temporary is not None: + temporary.unlink(missing_ok=True) + return hashlib.sha256(payload).hexdigest() + + +def load_evaluation_policy(path, expected_sha256, identity): + """New v2-only loader: does not masquerade as frozen v1 or restore learner state.""" + payload = Path(path).read_bytes() + if hashlib.sha256(payload).hexdigest() != expected_sha256: + raise ValueError("RootXY-v2 checkpoint digest mismatch") + data = torch.load(io.BytesIO(payload), map_location="cpu", weights_only=True) + required = {"format", "task_id", "contract", "features", "model_config", "model", "identity", + "update", "import_evidence", "budget_snapshot", "accepted_skill", "resume"} + if (not isinstance(data, dict) or set(data) != required + or data.get("format") != CHECKPOINT_FORMAT or data.get("task_id") != TASK_ID + or data.get("contract") != CONTRACT or data.get("features") != FEATURE_VERSION + or not exact(data.get("identity"), identity) or identity.get("task_id") != TASK_ID + or not exact(data.get("model_config"), MODEL_CONFIG) + or data.get("accepted_skill") is not False or data.get("resume") is not False + or type(data.get("update")) is not int or data["update"] not in [0, *PILOT["validation_updates"]]): + raise ValueError("RootXY-v2 eval identity mismatch") + _finite_tree(data) + _checkpoint_identity(identity, data["import_evidence"]) + with torch.random.fork_rng(devices=[]): + model = SimResidualActorCritic(**MODEL_CONFIG) + _model_state(data["model"], model.state_dict()) + model.load_state_dict(data["model"]) + return model.eval().requires_grad_(False), data diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/stress.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/stress.py new file mode 100644 index 0000000..bc09bde --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/stress.py @@ -0,0 +1,53 @@ +"""Deterministic scripted requests, not a policy or a controller modification.""" +import math + +import torch + + +def residual_signals(plan, case): + """Return CPU float32 normalized requests [control, environment, joint].""" + shape = (plan['controls_per_case'], plan['num_envs'], 3) + kind = case['kind'] + if kind == 'gaussian': + generator = torch.Generator(device='cpu').manual_seed(case['seed']) + return (torch.randn(shape, generator=generator) * plan['gaussian_normalized_std']).tanh() + amplitude = case['amplitude_rad'] + if not math.isfinite(amplitude) or not 0 <= amplitude <= plan['residual_limit_rad']: + raise ValueError('Amplitude exceeds the original residual contract') + scale = amplitude / plan['residual_limit_rad'] + if kind == 'zero': + return torch.zeros(shape) + if kind == 'constant': + signs = [[1 if e & (1 << j) else -1 for j in range(3)] for e in range(shape[1])] + return (torch.tensor(signs, dtype=torch.float32) * scale).expand(shape).clone() + if kind in ('sine', 'gated_sine'): + t = torch.arange(shape[0], dtype=torch.float64) * plan['control_dt_s'] + phase = (torch.tensor(plan['joint_phase_rad'], dtype=torch.float64)[None, :] + + torch.arange(shape[1], dtype=torch.float64)[:, None] + * plan['environment_phase_increment_rad']) + return (scale * torch.sin(2 * math.pi * plan['frequency_Hz'] * t[:, None, None] + phase)).float() + raise ValueError(f'Unknown scripted signal: {kind}') + + +def gate_residual(request, phase, enabled_phases): + """Gate at the control-start command phase; never infer phase from observed contact.""" + enabled = torch.zeros_like(phase, dtype=torch.bool) + for value in enabled_phases: + enabled |= phase == value + return torch.where(enabled[:, None], request, torch.zeros_like(request)) + + +def first_failure(trace, failure_names): + """Find physics-rate onset separately from the later control-boundary reset.""" + flags = trace['failure_flags'][:, 0] + hits = flags.any(-1).nonzero().flatten() + if not len(hits): + return None + tick = int(hits[0]) + return {'physics_tick': tick + 1, 'time_s': float(trace['time'][tick, 0]), + 'phase': int(trace['phase'][tick, 0]), + 'causes': [name for j, name in enumerate(failure_names) if flags[tick, j]], + 'gap_m': float(trace['gap'][tick, 0]), + 'normal_N': float(trace['normal'][tick, 0, 2]), + 'signed_height_error_m': float(trace['signed_height_error'][tick, 0]), + 'tilt_rad': float(trace['tilt'][tick, 0])} diff --git a/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/training.py b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/training.py new file mode 100644 index 0000000..b0963cd --- /dev/null +++ b/source/SkillFactory/SkillFactory/legs/contact_cycle_sim/training.py @@ -0,0 +1,348 @@ +"""Fresh-only 46D pilot protocol. Preparation is never authorization. + +Generic BOOTTIME accounting is reused, not the old task manifest or sampler. +This module and the CLI reject resume, legacy grants and mutable preparation. +""" + +import hashlib +import json +import os +import random +import shutil +from contextlib import contextmanager, suppress +from pathlib import Path + +from ..contact_cycle.training import ( + BudgetExceeded, + BudgetLedger, + artifact_bytes, + atomic_json, + digest, +) +from ..contact_cycle.training import ( + save_trace_subset as save_trace_subset, +) +from .config import FEATURE_VERSION, TASK_ID, load_config +from .ppo import CONTRACT, collect_rollout, ppo_step, rollout_diagnostics, selection_key + +CONFIG_PATH = "configs/contact_cycle/sim_training_v1.json" +PROPOSAL_PATH = "configs/contact_cycle/sim_training_proposal_v1.json" +PLAN_PATH = "docs/contact_cycle_sim_training_plan.md" +AUTHORIZATION_FORMAT = "skillfactory.contact-cycle-sim-residual-authorization.v1" +VALIDATION_UPDATES = (0, 64, 128, 192, 256) +MODEL_CONFIG = {"hidden": 128, "initial_std": 0.1} +PILOT = { + "format": "skillfactory.contact-cycle-sim-residual-training.v1", + "contract": CONTRACT, + "task_id": TASK_ID, + "features": FEATURE_VERSION, + "proposal": PROPOSAL_PATH, + "task_config": "configs/contact_cycle/simulation_residual_v1.json", + "functional_config": "configs/contact_cycle/simulation_functional_v1.json", + "safety_config": "configs/contact_cycle/acceptance_v1.json", + "seeds": [91501], + "validation_seed": 93501, + "num_envs": 128, + "horizon": 32, + "updates_per_seed": 256, + "validation_updates": list(VALIDATION_UPDATES), + "max_total_transitions": 1048576, + "max_total_wall_s": 3600, + "max_artifact_bytes": 1073741824, + "algorithm": { + **MODEL_CONFIG, "learning_rate": 0.0003, "adam_eps": 0.00001, + "gamma": 0.99, "gae_lambda": 0.95, "clip_ratio": 0.2, "epochs": 4, + "minibatch_size": 1024, "value_coefficient": 0.5, + "max_gradient_norm": 1.0, "target_kl": 0.02, "failure_penalty": 10.0, + }, +} +PROPOSAL = { + "format": "skillfactory.contact-cycle-sim-residual-proposal.v1", + "contract": CONTRACT, + "parent_plan": PLAN_PATH, + "scope": "fresh_nominal_only_pilot_no_external_DR_holdout", + "actual_training_launches": 1, + "resume": False, + "warm_start": False, + "accepted_skill": False, + "baseline": "u0_untrained_zero_residual_network_requires_128_of_128_functional_passes", + "selection": "u64_u128_u192_u256_lexicographic_failure_fraction_then_all_row_mean_cost_earliest_tie", + "row_cost": "failed=16; success=min(height_rms/.06,2)+min(swing_rms/.025,2)+min(landing_error/.05,2)", + "physics": "unchanged_reference_full_phase_residual_limit_0.05rad_500Hz_safety", + "validation": "same128_nominal_envs_seed93501_first_episode_only_full9s", + "post_result_changes": "separate_version_no_second_optimization_authorized", + "supervisor_timeout_s": 3480, +} + + +def _exact(value, expected): + # Python's True == 1 and NaN handling must not weaken the fixed protocol. + return json.dumps(value, sort_keys=True, allow_nan=False) == json.dumps(expected, sort_keys=True, allow_nan=False) + + +def load_training_config(path, root): + cfg = json.loads(Path(path).read_text()) + if not _exact(cfg, PILOT): + raise ValueError("Unsupported residual pilot config: seed/budget/schema/algorithm are fixed") + proposal = json.loads((Path(root) / cfg["proposal"]).read_text()) + if not _exact(proposal, PROPOSAL): + raise ValueError("Unsupported residual pilot proposal") + task = load_config(Path(root) / cfg["task_config"]) + if task["residual_limit_rad"] != .05: + raise ValueError("Full-phase residual contract must remain +/-0.05rad") + return cfg + + +def source_identity(root, cfg, config_path): + root = Path(root).resolve() + task = load_config(root / cfg["task_config"]) + cycle = json.loads((root / task["cycle_config"]).read_text()) + support = json.loads((root / cycle["support_config"]).read_text()) + files = {Path(config_path).resolve(), root / cfg["proposal"], root / cfg["task_config"], + root / cfg["functional_config"], root / cfg["safety_config"], root / PLAN_PATH, + root / task["cycle_config"], root / cycle["support_config"], root / "AGENT.md", + root / "source/SkillFactory/SkillFactory/legs/resources/ground.usda"} + files.update((root / "source/SkillFactory").rglob("*.py")) + for name in ("train_cycle_sim_residual.py", "verify_cycle_sim_learning_runtime.py", + "audit_contact_cycle_functional.py", "runtime_common.py", "supervise_sim.py"): + files.add(root / "scripts" / name) + for name in ("test_contact_cycle_sim_training.py", "test_contact_cycle_sim_learning_physics.py", + "test_contact_cycle_sim_learning.py"): + files.add(root / "tests" / name) + for folder in (support["support_asset"], support["source_bundle"]): + files.update(p for p in (root / folder).rglob("*") if p.is_file()) + if any(not p.resolve().is_relative_to(root) for p in files): + raise ValueError("Identity input escaped repository") + return {str(p.relative_to(root)): hashlib.sha256(p.read_bytes()).hexdigest() for p in sorted(files)} + + +def make_manifest(root, config_path): + cfg = load_training_config(config_path, root) + return {"format": "skillfactory.contact-cycle-sim-residual-preregistration.v1", + "contract": CONTRACT, "task_id": TASK_ID, "features": FEATURE_VERSION, + "training": cfg, "proposal": PROPOSAL, "model_config": MODEL_CONFIG, + "sources": source_identity(root, cfg, config_path), + "critic": "symmetric_sim_truth_policy46", "sampler": None, + "resume": False, "accepted_skill": False, + "wall_budget": "same_boot_CLOCK_BOOTTIME_includes_startup_validation_suspend_shutdown"} + + +def require_authorization(path, manifest): + if path is None: + raise PermissionError("Explicit parent-bound final manifest grant required before Kit or budget") + grant = json.loads(Path(path).read_text()) + keys = {"format", "approved", "manifest_sha256", "operator", "authorization_reference"} + if (set(grant) != keys or grant["format"] != AUTHORIZATION_FORMAT or grant["approved"] is not True + or grant["manifest_sha256"] != digest(manifest) + or any(not isinstance(grant[k], str) or not grant[k].strip() + for k in ("operator", "authorization_reference"))): + raise PermissionError("Invalid/stale/legacy training grant") + return grant + + +def writable_output(root, output): + output = Path(output).resolve() + if output.is_relative_to((Path(root) / "releases").resolve()): + raise ValueError("Frozen releases are not writable pilot outputs") + return output + + +def prepare(root, config_path, output): + output = writable_output(root, output) + manifest = make_manifest(root, config_path) + size = sum((Path(root) / p).stat().st_size for p in manifest["sources"]) + if size + len(json.dumps(manifest).encode()) + 1048576 > manifest["training"]["max_artifact_bytes"]: + raise BudgetExceeded("Preparation snapshot exceeds total artifact cap") + output.mkdir(parents=True, exist_ok=False) + atomic_json(output / "manifest.json", manifest) + for relative in manifest["sources"]: + target = output / "source_snapshot" / relative + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(Path(root) / relative, target) + atomic_json(output / "preparation.json", {"prepared": True, "authorized": False, "trained": False, + "manifest_sha256": digest(manifest)}) + return manifest + + +def validate_prepared(root, output, manifest): + output = writable_output(root, output) + if json.loads((output / "manifest.json").read_text()) != manifest: + raise ValueError("Prepared manifest is stale or mismatched") + expected = {"prepared": True, "authorized": False, "trained": False, "manifest_sha256": digest(manifest)} + if json.loads((output / "preparation.json").read_text()) != expected: + raise ValueError("Invalid preparation metadata") + if any((output / name).exists() for name in ("budget.json", "budget.lock", "launch_claim.json")): + raise FileExistsError("Fresh-only pilot refuses existing budget/launch: no retry or resume") + verify_snapshot(output, manifest) + allowed = {"manifest.json", "preparation.json", "source_snapshot", "console.log", "supervisor.json"} + if any(p.name not in allowed for p in output.iterdir()): + raise ValueError("Prepared output contains undeclared prior artifacts") + if artifact_bytes(output) + 1048576 > manifest["training"]["max_artifact_bytes"]: + raise BudgetExceeded("Prepared artifacts exceed cap") + + +def verify_snapshot(output, manifest): + for relative, sha in manifest["sources"].items(): + path = Path(output) / "source_snapshot" / relative + if path.is_symlink() or hashlib.sha256(path.read_bytes()).hexdigest() != sha: + raise ValueError("Prepared source snapshot changed") + + +@contextmanager +def isolated_validation_rng(env, validation_seed, training_seed): + """Environment seeding changes Python/NumPy too; restore all learner-side streams.""" + import numpy as np + + from .ppo import restore_rng, rng_state + + saved = rng_state(), random.getstate(), np.random.get_state() + try: + env.seed(validation_seed) + yield + finally: + try: + env.seed(training_seed) + env.reset() # physics is intentionally reset, never spliced into a training rollout + finally: + restore_rng(saved[0]) + random.setstate(saved[1]) + np.random.set_state(saved[2]) + + +def assert_no_conflicting_processes(proc=Path("/proc")): + conflicts = [] + for entry in proc.glob("[0-9]*"): + if int(entry.name) == os.getpid(): + continue + try: + argv = entry.joinpath("cmdline").read_bytes().split(b"\0") + executable = Path(argv[0].decode(errors="replace")).name.lower() + # Match executables and script argv, not shell/supervisor quoted command text. + is_kit = executable in ("kit", "isaac-sim", "isaac-sim.sh") + suffix = executable.removeprefix("python") + is_python = executable == "python" or (executable.startswith("python") + and suffix.replace(".", "").isdigit()) + # Only the Python entry script counts; supervisor command arguments are not workers. + entry_args = [a for a in argv[1:] if a not in (b"-u", b"-B", b"-s", b"-E", b"-I")] + first_arg = entry_args[0] if entry_args else b"" + script = Path(first_arg.decode(errors="replace")).name if first_arg.endswith(b".py") else None + is_worker = is_python and script in { + "train_cycle_sim_residual.py", "train_contact_cycle.py", "train_standing.py", + "verify_cycle_sim_learning_runtime.py", "verify_cycle_sim_residual.py", + "verify_cycle_sim_stress.py", "verify_cycle_learning_runtime.py", "verify_contact_cycle.py"} + loaded_kit = False + if is_python: + with suppress(OSError): + loaded_kit = "libomni.kit.app" in entry.joinpath("maps").read_text() + if is_kit or is_worker or loaded_kit: + conflicts.append(int(entry.name)) + except (OSError, IndexError): + continue + if conflicts: + raise RuntimeError(f"Conflicting simulator/training processes: {conflicts}") + + +class FreshBudgetLedger(BudgetLedger): + """No reconciliation/retry path; an exclusive claim also closes concurrent invocation races.""" + + def __enter__(self): + if self.path.exists() or (self.root / "budget.lock").exists(): + raise FileExistsError("Existing ledger is forbidden for this fresh-only pilot") + with (self.root / "launch_claim.json").open("x") as stream: + json.dump({"manifest_sha256": digest(self.manifest), "pid": os.getpid()}, stream) + stream.flush() + os.fsync(stream.fileno()) + return super().__enter__() + + def reconcile_checkpoint(self, *_): + raise PermissionError("No resume or interrupted reservation refund in this pilot") + + +def optimize_reserved_rollout(env, model, optimizer, x, algo, seed, update, ledger, guard, check_source): + """The sole optimizer entry: reserve, collect, verify AND persist diagnostics first.""" + check_source() + ledger.reserve_rollout(seed, update) + data, next_x, failures, timeouts = collect_rollout(env, model, x, algo, guard) + diagnostic = rollout_diagnostics(data, algo) + if diagnostic["missing_diagnostic_samples"]: + raise RuntimeError("Missing phase/cause diagnostics; no optimization") + ledger.append_json(ledger.root / "rollout_diagnostics.jsonl", {"update": update, **diagnostic}) + check_source() + learning = ppo_step(model, optimizer, data, algo) + guard() + return next_x, failures, timeouts, learning + + +def checkpoint_then_commit(ledger, seed, update, save_latest): + size = save_latest() # atomic latest checkpoint must succeed BEFORE durable commit + ledger.commit_update(seed, update) + return size + + +def choose_candidate(best, update, rows): + if update not in VALIDATION_UPDATES[1:]: + raise ValueError("Only u64/u128/u192/u256 are trained candidates; never u0") + key = selection_key(rows) + return best is None or key < tuple(best["selection_key"]) + + +def parameter_deltas(model, initial): + import torch + + result = {} + for group in ("actor", "critic", "log_std"): + delta = torch.cat([(v.detach().cpu() - initial[k]).reshape(-1) for k, v in model.named_parameters() + if k == group or k.startswith(group + ".")]) + if not torch.isfinite(delta).all(): + raise ValueError("Nonfinite parameter delta") + result[group] = {"l2": float(delta.norm()), "max_abs": float(delta.abs().max()), + "changed_parameters": int((delta != 0).sum())} + return result + + +def validation_rows(trace, stops, early, nominal, functional, evaluate): + """Audit each *first* episode; learned_policy is only the frozen metadata interface.""" + import torch + + if not trace or any(s is None or type(s) is not int or s <= 0 for s in stops): + raise RuntimeError("Missing first-episode terminal boundary") + if any(not torch.isfinite(v).all() for v in trace.values()): + raise RuntimeError("Nonfinite validation histories") + rows = [] + for e, stop in enumerate(stops): + episode = {k: v[:stop, e:e+1] for k, v in trace.items()} + d = {k: v[:, 0] for k, v in episode.items()} + actual = d["processed_target"] - nominal.cpu() - .5 * d["reference_used_normalized"] + invariants = { + "actor46": d["actor_input"].shape == (stop, 46), + "metadata": bool(d["actor_metadata_available"].all() and d["control_action_available"].all()), + "deterministic_action": bool(torch.allclose( + d["requested_action"], d["actor_mean"].tanh(), atol=1e-6, rtol=0)), + "request_bound": bool((d["requested_action"].abs() <= 1).all()), + "reference_bound": bool((d["reference_used_normalized"].abs() <= 1).all()), + "template_bound": bool(((d["processed_target"] - nominal.cpu()).abs() <= .5 + 1e-6).all()), + "actual_residual_bound": bool((actual.abs() <= .05 + 1e-6).all()), + "actual_residual_logged": bool(torch.allclose(actual, d["effective_residual_rad"], atol=1e-6, rtol=0)), + "nominal_only": bool((d["scenario"] == 0).all() and not d["external"].any()), + } + if not all(invariants.values()): + raise RuntimeError(f"Required validation invariant failed env{e}: {invariants}") + audited = evaluate(episode, functional, "learned_policy") + swing = d["phase"] == 3 + height = float(d["signed_height_error"].square().mean().sqrt()) + swing_rms = (float((d["foot"][swing] - d["target"][swing]).square().sum(-1).mean().sqrt()) + if swing.any() else None) + rows.append({"env": e, "success": bool(audited["sim_functional_passed"] and not early[e] + and not d["would_fail"].any()), + "first_episode_samples": stop, "functional_audit": audited, "invariants": invariants, + "height_rms_m": height, "swing_rms_m": swing_rms, + "landing_error_m": audited["metrics"].get("landing_error_m"), + "reference_rms_rad": float((.5 * d["reference_used_normalized"]).square().mean().sqrt()), + "reference_max_rad": float((.5 * d["reference_used_normalized"]).abs().max()), + "actual_residual_rms_rad": float(actual.square().mean().sqrt()), + "actual_residual_max_rad": float(actual.abs().max()), + "deterministic_request_rms": float(d["requested_action"].square().mean().sqrt()), + "template_max_rad": float((d["processed_target"] - nominal.cpu()).abs().max()), + "failure_flags": d["failure_flags"].any(0).tolist()}) + selection_key(rows) # successful rows require all three finite metrics + return rows diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/__init__.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/__init__.py new file mode 100644 index 0000000..c3a93fa --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/__init__.py @@ -0,0 +1,10 @@ +"""Experimental fixture-supported contact cycle, not a frozen skill.""" + +import gymnasium as gym + +gym.register( + id="SkillFactory-Go2FL-ContactCycle-v1", + entry_point=f"{__name__}.env:ContactCycleEnv", + disable_env_checker=True, + kwargs={"env_cfg_entry_point": f"{__name__}.cfg:ContactCycleEnvCfg"}, +) diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/cfg.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/cfg.py new file mode 100644 index 0000000..535fbc1 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/cfg.py @@ -0,0 +1,70 @@ +"""New task/config identity. Old standing physics, rewards and releases are untouched.""" + +from isaaclab.envs import mdp as base_mdp +from isaaclab.managers import ObservationGroupCfg, ObservationTermCfg, RewardTermCfg, TerminationTermCfg +from isaaclab.utils.configclass import configclass + +from SkillFactory.legs.contact_cycle.config import load_config +from SkillFactory.tasks.manager_based.loaded_leg.cfg import LoadedLegEnvCfg +from SkillFactory.tasks.manager_based.single_leg_anchor.mdp.actions import BoundedJointPositionActionCfg + +from . import mdp + + +@configclass +class ObservationsCfg: + @configclass + class Policy(ObservationGroupCfg): + state = ObservationTermCfg(func=mdp.observation) + + def __post_init__(self): + self.enable_corruption = False + self.concatenate_terms = True + + @configclass + class Privileged(ObservationGroupCfg): + truth = ObservationTermCfg(func=mdp.observation, params={"privileged": True}) + + def __post_init__(self): + self.enable_corruption = False + self.concatenate_terms = True + + policy = Policy() + privileged = Privileged() + + +@configclass +class RewardsCfg: + task = RewardTermCfg(func=mdp.reward, weight=1.0) + effort = RewardTermCfg(func=mdp.effort, weight=-0.01) + action_rate = RewardTermCfg(func=mdp.action_rate, weight=-0.005) + + +@configclass +class TerminationsCfg: + time_out = TerminationTermCfg(func=base_mdp.time_out, time_out=True) + failed = TerminationTermCfg(func=mdp.failed) + + +@configclass +class ContactCycleEnvCfg(LoadedLegEnvCfg): + cycle_config: str = "configs/contact_cycle/go2_fl_v1.json" + cycle_parameters: dict | None = None + auxiliary_enabled: bool = True + observations = ObservationsCfg() + rewards = RewardsCfg() + terminations = TerminationsCfg() + + def __post_init__(self): + self.cycle_parameters = load_config(self.cycle_config) + self.support_config = self.cycle_parameters["support_config"] + super().__post_init__() + # Override only this task's spawn. Historical standing keeps its 3mm clearance. + self.scene.robot.init_state.pos = ( + 0.0, 0.0, self.anchor_height + self.cycle_parameters["initialization"]["foot_clearance_m"]) + self.scene.foot_contact.track_friction_forces = True + self.actions.joint_position = BoundedJointPositionActionCfg( + class_type=mdp.CycleAction, asset_name="robot", joint_names=self.leg_bundle["joint_order"], + scale=self.support_parameters["action_scale_rad"], preserve_order=True, use_default_offset=True + ) + self.episode_length_s = sum(self.cycle_parameters["phase_durations_s"]) diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/env.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/env.py new file mode 100644 index 0000000..4e99a39 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/env.py @@ -0,0 +1,48 @@ +"""Control/physics clock separation and isolated reset, including terminal observations.""" + +import torch + +from isaaclab.envs import ManagerBasedRLEnv + +from SkillFactory.legs.contact_cycle.mechanics import canonical_ids +from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES +from SkillFactory.tasks.manager_based.loaded_leg.env import LoadedLegEnv + +from .mdp import failure_flags, state + + +class ContactCycleEnv(LoadedLegEnv): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + original_update = self.scene.update + + def update(dt): + original_update(dt) + if dt > 0: + state(self).after_physics(dt) + + self.scene.update = update + + def step(self, action): + self.extras["terminal_failure_flags"] = None + phase = state(self).reference()[0].clone() + self._step_failures = torch.zeros(self.num_envs, len(FAILURE_NAMES), dtype=torch.bool, device=self.device) + result = super().step(action) + self.extras["cycle_step"] = {"phase": phase, "failure_flags": self._step_failures.clone()} + return result + + def _reset_idx(self, env_ids): + env_ids = canonical_ids(env_ids, self.num_envs) + if getattr(self, "_standing_step", False): + self._step_failures[env_ids] = failure_flags(self)[env_ids] + self.extras["terminal_failure_names"] = FAILURE_NAMES + self.extras["terminal_failure_flags"] = self._step_failures[env_ids].clone() + self.extras["terminal_ids"] = env_ids.clone() + self.extras["terminal_observation"] = { + "policy": state(self).policy()[env_ids].clone(), + "privileged": state(self).privileged()[env_ids].clone(), + } + # Deliberately bypass LoadedLegEnv's old 45D terminal/reset implementation. + ManagerBasedRLEnv._reset_idx(self, env_ids) + if hasattr(self, "_cycle_state"): + self._cycle_state.reset(env_ids) diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/mdp.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/mdp.py new file mode 100644 index 0000000..6bd8419 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/mdp.py @@ -0,0 +1,284 @@ +"""Fixture-supported FL task. All external loads have exactly one composer owner.""" + +import torch + +from isaaclab.utils.math import axis_angle_from_quat + +from SkillFactory.legs.contact_cycle.mechanics import ( + ContactAcquisition, + ContactLatch, + SeparationLatch, + WrenchSchedule, + inverse_rotate, + reduce_to_com, + rotate, +) +from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES, DelayLine, command, phase_failures, phase_objective +from SkillFactory.tasks.manager_based.loaded_leg.mdp import LoadedState +from SkillFactory.tasks.manager_based.single_leg_anchor.mdp.actions import BoundedJointPositionAction + + +class CycleState(LoadedState): + def __init__(self, env): + super().__init__(env) + self.cc = env.cfg.cycle_parameters + b, device = env.num_envs, env.device + self.time = torch.zeros(b, device=device) + self.scenarios = torch.arange(b, device=device) % len(self.cc["scenarios"]) + self.schedule = WrenchSchedule(self.cc["events"], b, device) + self.latch = ContactLatch(b, device) + transition = self.cc["contact_transition"] + self.separation = SeparationLatch(b, device, transition["separation_on_m"], + transition["separation_off_m"], transition["dwell_s"]) + init = self.cc["initialization"] + self.acquisition = ContactAcquisition(b, device, init["contact_deadline_s"], init["dwell_s"], + init["min_load_fraction"], transition["separation_off_m"]) + self.load_off = torch.zeros(b, device=device, dtype=torch.bool) + self.edge_off = torch.zeros(b, device=device, dtype=torch.bool) + self.edge_on = torch.zeros_like(self.edge_off) + self.weight = self.robot.data.body_mass.torch.sum(-1) * 9.81 + self.origin = self.foot_center().clone() + self.origin[:, 2] = self.bundle["foot"]["radius"] + self.base_origin = env.scene.env_origins.clone() + self.base_origin[:, 2] += env.cfg.anchor_height + self.all_bodies = torch.arange(self.robot.num_bodies, device=device) + self.external = torch.zeros(b, self.robot.num_bodies, 6, device=device) + self.guide = torch.zeros_like(self.external) + self.auxiliary = torch.zeros_like(self.external) + self.sensor_delay = DelayLine(b, 18, self.cc["sensor"]["delay_steps"], device) + self.action_delay = DelayLine(b, 3, self.cc["actuator"]["delay_steps"], device) + self.sensed = torch.zeros(b, 18, device=device) + self.executed = torch.zeros(b, 3, device=device) + self.substeps = torch.zeros(b, dtype=torch.long, device=device) + self.impact = torch.zeros(b, device=device) + self.sampler = None + self.stand_only = torch.zeros(b, dtype=torch.bool, device=device) + self.sampled_points = None + self.sampled_stage = torch.zeros(b, dtype=torch.long, device=device) + self.reset(torch.arange(b, device=device)) + + def reset(self, ids): + self.time[ids] = 0 + self.substeps[ids] = 0 + self.schedule.reset(ids) + if self.sampler is not None and len(ids): + from SkillFactory.legs.contact_cycle.curriculum import assert_sample_domain + + sample = self.sampler.sample(ids.cpu()) + assert_sample_domain(self.cc, sample) + for target, key in ((self.scenarios, "scenario"), (self.stand_only, "stand"), + (self.sampled_stage, "stage"), (self.schedule.commands, "commands"), + (self.sampled_points, "points"), (self.schedule.delays, "delays")): + target[ids] = sample[key].to(self.env.device) + self.latch.reset(ids) + self.separation.reset(ids) + self.acquisition.reset(ids) + self.edge_on[ids] = self.edge_off[ids] = self.load_off[ids] = False + self.impact[ids] = 0 + self.executed[ids] = 0 + self.external[ids] = self.guide[ids] = self.auxiliary[ids] = 0 + self.action_delay.reset(ids) + # Reset all sensor history to the new measurement, not the previous episode. + sensed = self.raw_sensors()[ids] + self.sensor_delay.reset(ids, sensed) + self.sensed[ids] = sensed + self.robot.permanent_wrench_composer.reset(env_ids=ids) + + def set_sampler(self, sampler): + """Install distribution only at an explicit reset boundary. Never changes old defaults.""" + if sampler is not None and (sampler.count != self.env.num_envs or sampler.config != self.cc): + raise ValueError("Sampler/environment identity mismatch") + self.sampler = sampler + self.stand_only.zero_() + self.scenarios[:] = torch.arange(self.env.num_envs, device=self.env.device) % len(self.cc["scenarios"]) + if sampler is None: + self.schedule.commands = self.schedule.delays = self.sampled_points = None + else: + shape = (self.env.num_envs, len(self.cc["events"])) + self.schedule.commands = torch.zeros(*shape, 6, device=self.env.device) + self.schedule.delays = torch.zeros(*shape, device=self.env.device) + self.sampled_points = torch.zeros(*shape, 3, device=self.env.device) + + def reference(self): + phase, progress, target, force, fraction = command(self.time, self.cc, self.origin, self.weight) + if self.sampler is not None: + phase = torch.where(self.stand_only, 0, phase) + progress = torch.where(self.stand_only, self.time / sum(self.cc["phase_durations_s"]), progress) + target = torch.where(self.stand_only[:, None], self.origin, target) + force = torch.where(self.stand_only, self.weight, force) + fraction = torch.where(self.stand_only, 0., fraction) + return phase, progress, target, force, fraction + + def port_reference(self): + motion = self.cc["base_motion"] + w = 2 * torch.pi * motion["frequency_Hz"] + amp = self.time.new_tensor(motion["amplitude_m"]) + # zero initial position and velocity, bounded displacement by configured amplitude + pos = self.base_origin + 0.5 * (1 - torch.cos(w * self.time[:, None])) * amp + vel = 0.5 * w * torch.sin(w * self.time[:, None]) * amp + return pos, vel + + def apply_loads(self): + phase, _, _, _, fraction = self.reference() + values = self.schedule.step(self.time, phase, self.edge_off, self.edge_on, self.scenarios, + self.env.physics_dt, liftoff_phase=self.separation.edge_phase) + self.external.zero_() + self.guide.zero_() + self.auxiliary.zero_() + d = self.robot.data + com = d.body_com_pos_w.torch + pos, quat, vel, omega = self.root() + identity = torch.zeros_like(quat) + identity[:, 3] = 1 + for j, event in enumerate(self.cc["events"]): + body = self.robot.body_names.index(event["body"]) + bpos, bquat = d.body_link_pos_w.torch[:, body], d.body_link_quat_w.torch[:, body] + frames = {"world": (self.env.scene.env_origins, identity), + "mount": (d.body_link_pos_w.torch[:, self.anchor], d.body_link_quat_w.torch[:, self.anchor]), + "body": (bpos, bquat)} + point_origin, point_quat = frames[event["point_frame"]] + local_point = (self.time.new_tensor(event["point_m"]).expand_as(pos) + if self.sampled_points is None else self.sampled_points[:, j]) + point = point_origin + rotate(point_quat, local_point) + self.external[:, body] += reduce_to_com(values[:, j, :3], values[:, j, 3:], + frames[event["frame"]][1], point, com[:, body]) + target, target_vel = self.port_reference() + disp = torch.cat((pos - target, axis_angle_from_quat(quat)), -1) + velocity = torch.cat((vel - target_vel, omega), -1) + raw_guide = (-self.stiffness * disp - self.damping * velocity).clamp(-self.wrench_limit, self.wrench_limit) + if not self.env.cfg.guide_enabled: + raw_guide.zero_() + self.guide[:, self.payload] = reduce_to_com( + raw_guide[:, :3], raw_guide[:, 3:], identity, pos, com[:, self.payload] + ) + # Explicit equivalent other-leg support: gravity share + finite impedance at mounting port. + # Exactly zero in full support; full system weight in flight. This is an active fixture, not free balance. + support = self.cc["support"] + fz = fraction * (self.weight + support["kp_N_m"] * (target[:, 2] - pos[:, 2]) + + support["kd_Ns_m"] * (target_vel[:, 2] - vel[:, 2])) + f = torch.zeros_like(pos) + f[:, 2] = fz.clamp(0, support["max_N"]) if self.env.cfg.auxiliary_enabled else 0 + self.auxiliary[:, self.payload] = reduce_to_com(f, torch.zeros_like(f), identity, pos, com[:, self.payload]) + # COM-reduced loads; positions=None prevents the backend from adding r x F a second time. + # ONE set call across ALL bodies: the installed composer resets the whole selected env on set. + total = self.external + self.guide + self.auxiliary + self.robot.permanent_wrench_composer.set_forces_and_torques_index( + forces=total[..., :3], torques=total[..., 3:], body_ids=self.all_bodies, positions=None, is_global=True + ) + + def point_velocity(self): + d = self.robot.data + return d.body_link_lin_vel_w.torch[:, self.foot] + torch.cross( + d.body_link_ang_vel_w.torch[:, self.foot], + rotate(d.body_link_quat_w.torch[:, self.foot], self.offset), dim=-1) + + def slip_velocity(self): + # Actual velocity of the spherical surface point at z-down, not sphere-center drift. + r = torch.zeros_like(self.point_velocity()) + r[:, 2] = -self.bundle["foot"]["radius"] + surface = self.point_velocity() + torch.cross( + self.robot.data.body_link_ang_vel_w.torch[:, self.foot], r, dim=-1 + ) + return surface[:, :2].norm(dim=-1) + + def friction(self): + return self.env.scene["foot_contact"].data.friction_forces_w.torch[:, 0].sum(1) + + def raw_sensors(self): + _, quat, _, omega = self.root() + gravity = torch.zeros_like(omega) + gravity[:, 2] = -1 + return torch.cat((self.q(), self.robot.data.joint_vel.torch[:, self.joints], self.mount_wrench(), + inverse_rotate(quat, gravity), inverse_rotate(quat, omega)), -1) + + def after_physics(self, dt): + self.substeps += 1 + self.time[:] = self.substeps * dt + force = self.contact()[:, 2] + self.load_off, self.edge_on = self.latch.update(force, dt) + phase = self.reference()[0] + gap = self.foot_center()[:, 2] - self.bundle["foot"]["radius"] + self.acquisition.update(self.time, gap, force, self.weight, dt) + self.edge_off = self.separation.update(gap, self.latch.contact, phase, dt) + self.impact = torch.where(phase == 4, torch.maximum(self.impact, force), torch.zeros_like(force)) + if (self.substeps % self.env.cfg.decimation == 0).all(): + x = self.raw_sensors() + noise = self.cc["sensor"] + scale = x.new_tensor([noise["joint_noise_std_rad"]]*3 + [noise["velocity_noise_std_rad_s"]]*3 + + [noise["wrench_noise_std"]]*6 + [0.0]*6) + self.sensed = self.sensor_delay.push(x + torch.randn_like(x) * scale) + + def policy(self): + phase, progress, target, force, _ = self.reference() + # Desired position expressed in commanded (not ground-truth actual) port frame. + cmd = torch.cat((torch.nn.functional.one_hot(phase, 6).float(), progress[:, None], + target - self.port_reference()[0], force[:, None]), -1) + return torch.cat((self.sensed, self.executed, cmd), -1) # 18 + 3 + 11 = 32 + + def privileged(self): + return torch.cat((self.root()[0] - self.env.scene.env_origins, self.foot_center(), + self.contact(), self.friction(), self.external.flatten(1), + self.guide.flatten(1), self.auxiliary.flatten(1)), -1) + + def objective(self): + phase, progress, target, force, _ = self.reference() + center = self.foot_center() + reward, violation = phase_objective( + phase, progress, (center-target).norm(dim=-1), center[:, 2] - self.bundle["foot"]["radius"], + self.contact()[:, 2], force, self.slip_velocity(), self.impact, self.weight, + self.cc["contact_transition"]["unload_tracking_scale_m"]) + return reward, violation | self.separation.premature | self.acquisition.failed + + +def state(env): + if not hasattr(env, "_cycle_state"): + env._cycle_state = CycleState(env) + return env._cycle_state + + +def observation(env, privileged=False): + return state(env).privileged() if privileged else state(env).policy() + + +class CycleAction(BoundedJointPositionAction): + def process_actions(self, actions): + s = state(self._env) + c = s.cc["actuator"] + effective = s.action_delay.push(actions.clamp(-1, 1)) * c["gain"] + effective += actions.new_tensor(c["bias_rad"]) / self._env.cfg.support_parameters["action_scale_rad"] + super().process_actions(effective) + s.executed[:] = self.raw_actions + + def apply_actions(self): + state(self._env).apply_loads() + super().apply_actions() + + +def reward(env): + return state(env).objective()[0] + + +def failure_flags(env): + """Independent, overlapping causes in a stable order, evaluated BEFORE reset.""" + s = state(env) + phase, progress, _, _, _ = s.reference() + reasons = phase_failures(phase, progress, s.foot_center()[:, 2] - s.bundle["foot"]["radius"], + s.contact()[:, 2], s.weight) + reasons.update( + nonfinite=~torch.isfinite(torch.cat((s.raw_sensors(), s.root()[0], s.contact()), -1)).all(-1), + contact_acquisition_timeout=s.acquisition.failed, premature_separation=s.separation.premature, + low_height=s.root()[0][:, 2] < env.cfg.anchor_height - 0.1, tilt=s.tilt() > 0.6) + return torch.stack([reasons[name] for name in FAILURE_NAMES], -1) + + +def failed(env): + return failure_flags(env).any(-1) + + +def effort(env): + s = state(env) + return (s.robot.data.applied_torque.torch[:, s.joints] / s.effort).square().mean(-1) + + +def action_rate(env): + return (env.action_manager.action - env.action_manager.prev_action).square().mean(-1) diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/recording.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/recording.py new file mode 100644 index 0000000..831f8ad --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle/recording.py @@ -0,0 +1,184 @@ +"""Physics-rate validation capture; evaluate the first episode, never repaired autoresets.""" + +import math + +import torch + +from SkillFactory.legs.contact_cycle.mechanics import delivered_events +from SkillFactory.legs.contact_cycle.metrics import summarize +from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES + +from .mdp import effort, failed, failure_flags, state + + +class PhysicalSafetyAudit: + """Accumulate at every physics tick, check at control boundaries (also across resets).""" + + def __init__(self, env, thresholds): + self.env, self.thresholds, self.s = env, thresholds, state(env) + self.maximum = torch.zeros(3, device=env.device) + self.finite = torch.tensor(True, device=env.device) + self.samples = 0 + self.lower = torch.tensor([j["limits"]["lower"] for j in self.s.bundle["joints"]], device=env.device) + self.upper = torch.tensor([j["limits"]["upper"] for j in self.s.bundle["joints"]], device=env.device) + self.speed = torch.tensor([j["limits"]["velocity"] for j in self.s.bundle["joints"]], device=env.device) + + def __enter__(self): + self.previous = self.env.scene.update + + def update(dt): + self.previous(dt) + if dt > 0: + s = self.s + ratio = torch.maximum((s.robot.data.applied_torque.torch[:, s.joints]/s.effort).abs().amax(), + (s.robot.data.joint_vel.torch[:, s.joints]/self.speed).abs().amax()) + excess = torch.maximum(self.lower - s.q(), s.q() - self.upper).clamp(min=0).amax() + values = torch.stack((ratio, excess, s.guide[..., 2].abs().amax())) + self.maximum = torch.maximum(self.maximum, values) + self.finite &= (torch.isfinite(values).all() & torch.isfinite(s.raw_sensors()).all() + & torch.isfinite(s.robot.data.root_state_w.torch).all()) + self.samples += 1 + + self.env.scene.update = update + return self + + def __exit__(self, *_): + self.env.scene.update = self.previous + + def report(self): + values = self.maximum.cpu().tolist() + return {"physics_samples": self.samples, "finite": bool(self.finite), + "max_effort_velocity_ratio": values[0] if math.isfinite(values[0]) else None, + "max_joint_excess_rad": values[1] if math.isfinite(values[1]) else None, + "max_guide_vertical_N": values[2] if math.isfinite(values[2]) else None} + + def check(self): + report = self.report() + if (not report["finite"] or report["max_effort_velocity_ratio"] is None + or report["max_effort_velocity_ratio"] > self.thresholds["normalized_effort_velocity_max"] + or report["max_joint_excess_rad"] > self.thresholds["joint_limit_excess_rad_max"] + or report["max_guide_vertical_N"] != 0): + raise RuntimeError(f"Physical safety gate failed: {report}") + + +class CycleRecorder: + def __init__(self, env): + self.env, self.state = env, state(env) + self.frames = [] + self.stops = [None] * env.num_envs + self.early = torch.zeros(env.num_envs, dtype=torch.bool) + s = self.state + self.lower = torch.tensor([j["limits"]["lower"] for j in s.bundle["joints"]], device=env.device) + self.upper = torch.tensor([j["limits"]["upper"] for j in s.bundle["joints"]], device=env.device) + self.speed = torch.tensor([j["limits"]["velocity"] for j in s.bundle["joints"]], device=env.device) + self.previous = None + self.actor_input = torch.zeros(env.num_envs, 32) + self.actor_mean = torch.zeros(env.num_envs, 3) + self.actor_std = torch.zeros(env.num_envs, 3) + self.requested_action = torch.zeros(env.num_envs, 3) + self.actor_available = torch.zeros(env.num_envs, dtype=torch.bool) + self.action_available = torch.zeros(env.num_envs, dtype=torch.bool) + + def before_control(self, observation, action, mean=None, std=None): + if observation is not None: + self.actor_input = observation.detach().cpu().clone() + self.requested_action = action.detach().cpu().clone() + self.action_available.fill_(True) + self.actor_available.fill_(observation is not None and mean is not None and std is not None) + if mean is not None and std is not None: + self.actor_mean = mean.detach().cpu().clone() + self.actor_std = std.detach().cpu().expand(self.env.num_envs, 3).clone() + + def __enter__(self): + self.initial_state = self.snapshot() # t=0 separately; not an extra physics tick + self.previous = self.env.scene.update + + def update(dt): + self.previous(dt) + if dt > 0: + self.record() + + self.env.scene.update = update + return self + + def __exit__(self, *_): + self.env.scene.update = self.previous + + def snapshot(self): + s = self.state + phase, progress, target, force, _ = s.reference() + ratio = torch.maximum((s.robot.data.applied_torque.torch[:, s.joints]/s.effort).abs().amax(-1), + (s.robot.data.joint_vel.torch[:, s.joints]/self.speed).abs().amax(-1)) + values = {"phase": phase, "progress": progress, "time": s.time, "scenario": s.scenarios, + "weight": s.weight, "normal": s.contact(), "friction": s.friction(), "foot": s.foot_center(), + "target": target, "gap": s.foot_center()[:, 2] - s.bundle["foot"]["radius"], + "desired_force": force, + "joint_excess": torch.maximum(self.lower - s.q(), s.q() - self.upper).clamp(min=0).amax(-1), + "limit_ratio": ratio, "slip_speed": s.slip_velocity(), + "height_error": (s.root()[0][:, 2] - s.port_reference()[0][:, 2]).abs(), + "external": s.external, "guide": s.guide, "auxiliary": s.auxiliary, + "would_fail": failed(self.env), "failure_flags": failure_flags(self.env), "tilt": s.tilt(), + "root_state": torch.cat(s.root(), -1), + "signed_height_error": s.root()[0][:, 2] - s.port_reference()[0][:, 2], + "joint_pos": s.q(), "joint_vel": s.robot.data.joint_vel.torch[:, s.joints], + "applied_torque": s.robot.data.applied_torque.torch[:, s.joints], + "processed_target": self.env.action_manager.get_term("joint_position").processed_actions, + "effective_action": s.executed, "policy_snapshot": s.policy(), + "actor_input": self.actor_input, "actor_mean": self.actor_mean, "actor_std": self.actor_std, + "requested_action": self.requested_action, "actor_metadata_available": self.actor_available, + "control_action_available": self.action_available, + "task_reward_unweighted": s.objective()[0], "effort_cost_unweighted": effort(self.env), + "contact_acquired": s.acquisition.acquired, "acquisition_failed": s.acquisition.failed, + "acquisition_timer": s.acquisition.timer, "contact_armed": s.separation.armed, + "separation_timer": s.separation.timer, + "load_off": s.load_off, "liftoff": s.edge_off, "liftoff_phase": s.separation.edge_phase, + "separated": s.separation.separated, "premature_separation": s.separation.premature, + "event_fired": s.schedule.fired, "event_completed": s.schedule.completed, + "event_integral": s.schedule.integral} + return {k: v.detach().cpu().clone() for k, v in values.items()} + + def record(self): + self.frames.append(self.snapshot()) + + def end_control(self, terminated, truncated, final_step): + done = (terminated | truncated).nonzero().flatten().cpu().tolist() + for e in done: + if self.stops[e] is None: + self.stops[e] = len(self.frames) + self.early[e] = bool(terminated[e]) or not final_step + + def summarize(self, thresholds): + if not self.frames: + raise RuntimeError("No physics samples captured") + trace = {k: torch.stack([f[k] for f in self.frames]) for k in self.frames[0]} + rows = [] + for e, stop in enumerate(self.stops): + episode = {k: v[:stop, e:e+1] for k, v in trace.items()} + row = summarize(episode, self.env.physics_dt, thresholds, self.state.cc["scenarios"])[0] + row["env"] = e + row["first_episode_samples"] = len(episode["time"]) + flags = episode["failure_flags"][:, 0] + row["terminal_causes"] = [name for j, name in enumerate(FAILURE_NAMES) if flags[:, j].any()] + hits = flags.any(-1).nonzero().flatten() + row["first_failure_time_s"] = float(episode["time"][int(hits[0]), 0]) if len(hits) else None + row["contact_acquired"] = bool(episode["contact_acquired"][:, 0].any()) + delivered = bool(delivered_events( + self.state.cc["events"], episode["scenario"][0], episode["event_fired"][-1], + episode["event_completed"][-1], episode["event_integral"][-1])[0]) + row.setdefault("gates", {})["disturbance_delivery"] = delivered + if not delivered: + row["success"] = False + row["failure_reasons"].append("disturbance_not_delivered") + if episode["acquisition_failed"].any(): + row["success"] = False + row["failure_reasons"].append("contact_acquisition_timeout") + if episode["premature_separation"].any(): + row["success"] = False + row["failure_reasons"].append("premature_separation") + complete_clock = (len(episode["time"]) == round(sum(self.state.cc["phase_durations_s"])/self.env.physics_dt) + and float(episode["time"][-1, 0]) >= sum(self.state.cc["phase_durations_s"]) - 1e-5) + if self.early[e] or not complete_clock: + row["success"] = False + row["failure_reasons"].append("terminated_or_incomplete_first_episode") + rows.append(row) + return rows, trace diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/__init__.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/__init__.py new file mode 100644 index 0000000..c719ada --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/__init__.py @@ -0,0 +1,9 @@ +"""Nominal simulation-truth residual task, NOT a hardware or learned-skill release.""" +import gymnasium as gym + +gym.register( + id='SkillFactory-Go2FL-ContactCycleSim-v1', + entry_point=f'{__name__}.env:SimContactCycleEnv', + disable_env_checker=True, + kwargs={'env_cfg_entry_point': f'{__name__}.cfg:SimContactCycleEnvCfg'}, +) diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/cfg.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/cfg.py new file mode 100644 index 0000000..04cca11 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/cfg.py @@ -0,0 +1,61 @@ +"""Separate nominal simulation-only config. No historical task parameters are mutated.""" +from isaaclab.managers import ObservationGroupCfg, ObservationTermCfg, RewardTermCfg +from isaaclab.utils.configclass import configclass + +from SkillFactory.legs.contact_cycle_sim.config import load_config +from SkillFactory.tasks.manager_based.contact_cycle.cfg import ContactCycleEnvCfg +from SkillFactory.tasks.manager_based.single_leg_anchor.mdp.actions import BoundedJointPositionActionCfg + +from . import mdp + + +@configclass +class ObservationsCfg: + @configclass + class Policy(ObservationGroupCfg): + state = ObservationTermCfg(func=mdp.observation) + + def __post_init__(self): + self.enable_corruption = False + self.concatenate_terms = True + + @configclass + class Privileged(ObservationGroupCfg): + truth = ObservationTermCfg(func=mdp.observation, params={'privileged': True}) + + policy = Policy() + privileged = Privileged() + + +@configclass +class RewardsCfg: + task = RewardTermCfg(func=mdp.base.reward, weight=1.) + effort = RewardTermCfg(func=mdp.base.effort, weight=-.01) + action_rate = RewardTermCfg(func=mdp.base.action_rate, weight=-.005) + root_position = RewardTermCfg(func=mdp.root_position_cost, weight=-1.) + root_velocity = RewardTermCfg(func=mdp.root_velocity_cost, weight=-.1) + residual = RewardTermCfg(func=mdp.residual_cost, weight=-.05) + + +@configclass +class SimContactCycleEnvCfg(ContactCycleEnvCfg): + residual_config: str = 'configs/contact_cycle/simulation_residual_v1.json' + residual_parameters: dict | None = None + observations = ObservationsCfg() + rewards = RewardsCfg() + + def __post_init__(self): + self.residual_parameters = c = load_config(self.residual_config) + self.cycle_config = c['cycle_config'] + super().__post_init__() + cycle = self.cycle_parameters + if (any(cycle['sensor'].values()) or cycle['actuator']['gain'] != 1 + or any(cycle['actuator']['bias_rad']) or cycle['actuator']['delay_steps'] != 0 + or any(cycle['base_motion']['amplitude_m']) or self.support_parameters['action_scale_rad'] != .5): + raise ValueError('Sim residual v1 requires declared noiseless, undelayed nominal fixture and .5rad scale') + self.actions.joint_position = BoundedJointPositionActionCfg( + class_type=mdp.SimResidualAction, asset_name='robot', joint_names=self.leg_bundle['joint_order'], + scale=.5, preserve_order=True, use_default_offset=True) + self.rewards.root_position.weight = -c['reward']['position_weight'] + self.rewards.root_velocity.weight = -c['reward']['velocity_weight'] + self.rewards.residual.weight = -c['reward']['residual_weight'] diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/env.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/env.py new file mode 100644 index 0000000..6f323ad --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/env.py @@ -0,0 +1,10 @@ +"""Reuse physical clock, safety terminations and PRE-reset capture via virtual policy().""" +from SkillFactory.tasks.manager_based.contact_cycle.env import ContactCycleEnv + +from .mdp import state + + +class SimContactCycleEnv(ContactCycleEnv): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + state(self) # fail closed on a wrong state contract diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_cfg.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_cfg.py new file mode 100644 index 0000000..b0bf631 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_cfg.py @@ -0,0 +1,16 @@ +"""Distinct, directly instantiated evaluation config; frozen config is unchanged.""" +from isaaclab.utils.configclass import configclass + +from .cfg import SimContactCycleEnvCfg +from .evaluation_mdp import EvaluationResidualAction + + +@configclass +class EvaluationContactCycleEnvCfg(SimContactCycleEnvCfg): + def __post_init__(self): + super().__post_init__() + self.scene.num_envs = 1 + self.actions.joint_position.class_type = EvaluationResidualAction + # Keep frozen reward/observation/termination definitions. The evaluation + # runtime checks their failure floors per physics tick, not manager timeout. + # Cycle count comes from Protocol, never by extending a training episode. diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_env.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_env.py new file mode 100644 index 0000000..bc8bca2 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_env.py @@ -0,0 +1,498 @@ +"""Evaluation-only no-autoreset runtime. Not a Gym training environment. + +Callbacks are synchronous/read-only and must copy tensor evidence before returning. +The installed RL step has no reusable single-substep helper. Only its scalar +physics-loop branch is reproduced; manager autoreset is never entered. +""" +from collections.abc import Callable +from contextlib import contextmanager +from dataclasses import dataclass, replace +from functools import wraps +from hashlib import sha256 +from pathlib import Path + +import torch + +from isaaclab.envs import ManagerBasedRLEnv + +from SkillFactory.legs.contact_cycle_sim.evaluation_protocol import ( + CONTROL_TICKS, + CYCLE_TICKS, + PHYSICS_DT_S, + Clock, + Command, + Protocol, +) + +from .evaluation_mdp import EvaluationCycleState, state, vector_tuple + +# Expose exact installed source dependencies for the separate evaluation binding. +INSTALLED_ISAAC_SOURCES = { + 'manager_based_rl_env.py': 'a963cfcdb4cd60955cd022ed709d27c99a6935685b69180bb29d74f8983f6566', + 'manager_based_env.py': '2159b887235fd664684be917c34890e9054e51848855148229216d4f2ec85ad8', +} +INSTALLED_COMPOSER_SHA256 = '8fb3ab4e59db121165db52daf61e0de0e73653f0c154a143f6a828c198082c06' + + +def verify_installed_stepping(envs_directory): + for name, expected in INSTALLED_ISAAC_SOURCES.items(): + if sha256((Path(envs_directory) / name).read_bytes()).hexdigest() != expected: + raise RuntimeError(f'Installed Isaac stepping dependency changed: {name}') + + +class RuntimeViolation(RuntimeError): + pass + + +class RuntimeHooks: + """Class hooks MUST surround construction through close (single writer only). + + Counts attempted and completed sim.step calls, including constructor/reset. + before_step is the future aggregate budget/deadline owner's hook; exceptions + fail closed. Guard lists are public binding evidence, not a security sandbox. + """ + def __init__(self, simulation_class, guarded_methods, *, before_step=None): + self.simulation_class = simulation_class + self.guarded_methods = tuple(guarded_methods) + self.before_step = before_step + self.active = False + self.mode = 'closed' + self.attempted_steps = self.completed_steps = 0 + self.initialization_steps = self.rollout_steps = 0 + self.reset_calls = self.pose_write_calls = self.composer_buffer_replacements = 0 + self._load_owner = self._replacement_owner = None + self.violations = [] + self._patches = [] + self._guarded = set() + + @classmethod + def isaac(cls, *, before_step=None): + """Call after AppLauncher/import setup but BEFORE environment construction.""" + from isaaclab_physx.assets.articulation import Articulation + + from isaaclab.scene import InteractiveScene + from isaaclab.sim import SimulationContext + + guards = [(SimulationContext, 'reset'), (InteractiveScene, 'reset'), (InteractiveScene, 'reset_to')] + # Backend class, not the articulation factory: also covers legacy aliases. + guards += [(Articulation, name) for name in dir(Articulation) + if name == 'reset' or (name.startswith('write_') and '_to_sim' in name + and name != 'write_data_to_sim')] + return cls(SimulationContext, guards, before_step=before_step) + + def _patch(self, owner, name, replacement): + own = name in vars(owner) + old = getattr(owner, name) + self._patches.append((owner, name, old, own)) + setattr(owner, name, replacement) + + def require_initialization(self, operation): + if not self.active or self.mode not in ('construction', 'initialization'): + self.violations.append(operation) + raise RuntimeViolation(f'Illegal reset/pose write: {operation}') + + def guard_object(self, obj, names): + for name in names: + key = (id(obj), name) + if key in self._guarded: + continue + self._guarded.add(key) + old = getattr(obj, name) + + @wraps(old) + def guarded(*args, _old=old, _name=name, **kwargs): + self.require_initialization(_name) + if 'reset' in _name: + self.reset_calls += 1 + else: + self.pose_write_calls += 1 + return _old(*args, **kwargs) + + self._patch(obj, name, guarded) + + def guard_composer(self, composer): + """Setter-internal input-buffer replacement is not a trial/state reset.""" + key = (id(composer), 'reset') + if key in self._guarded: + return + self._guarded.add(key) + reset = composer.reset + setter = composer.set_forces_and_torques_index + + @wraps(reset) + def guarded_reset(*args, **kwargs): + if self._replacement_owner is composer: + self.composer_buffer_replacements += 1 + else: + self.require_initialization('composer.reset') + self.reset_calls += 1 + return reset(*args, **kwargs) + + @wraps(setter) + def guarded_setter(*args, **kwargs): + if self._load_owner is not composer or self._replacement_owner is not None: + self.violations.append('unowned composer setter') + raise RuntimeViolation('Only the bound physics action may replace wrench buffers') + self._replacement_owner = composer + try: + return setter(*args, **kwargs) + finally: + self._replacement_owner = None + + self._patch(composer, 'reset', guarded_reset) + self._patch(composer, 'set_forces_and_torques_index', guarded_setter) + + @contextmanager + def wrench_update(self, composer): + if not self.active or self.mode != 'locked' or self._load_owner is not None: + raise RuntimeViolation('Invalid unique-writer scope') + self._load_owner = composer + try: + yield + finally: + self._load_owner = None + + def __enter__(self): + if self.active: + raise RuntimeError('Runtime hooks already installed') + self.active, self.mode = True, 'construction' + old = self.simulation_class.step + + @wraps(old) + def step(sim, *args, **kwargs): + if self.mode not in ('construction', 'initialization', 'rollout'): + self.violations.append('sim.step outside runtime') + raise RuntimeViolation('Unowned sim.step') + self.attempted_steps += 1 + if self.before_step is not None: + self.before_step(self) + result = old(sim, *args, **kwargs) + self.completed_steps += 1 + if self.mode == 'rollout': + self.rollout_steps += 1 + else: + self.initialization_steps += 1 + return result + + self._patch(self.simulation_class, 'step', step) + for owner, name in self.guarded_methods: + self.guard_object(owner, (name,)) + return self + + @contextmanager + def scope(self, mode): + if not self.active or self.mode != 'locked' or mode not in ('initialization', 'rollout'): + raise RuntimeViolation('Invalid runtime hook scope') + self.mode = mode + try: + yield + finally: + self.mode = 'locked' + + def __exit__(self, *_): + for owner, name, old, own in reversed(self._patches): + if own: + setattr(owner, name, old) + else: + delattr(owner, name) + self._patches.clear() + self._guarded.clear() + self.active, self.mode = False, 'closed' + + +@dataclass(frozen=True) +class RuntimeEvent: + kind: str # initial, tick, next_reference + trial_index: int + clock: Clock + command: Command + held_control_index: int | None + action_origin_tick: int | None + reference_used_clock: Clock | None + reference_used: tuple[float, ...] + reference_next_clock: Clock + reference_next: tuple[float, ...] + causes: tuple[str, ...] = () + + +@dataclass(frozen=True) +class SeamDecision: + admitted: bool + causes: tuple[str, ...] = () + + +@dataclass(frozen=True) +class StopResult: + reason: str # failure or protocol_complete, never a gate-pass assertion + trial_index: int + clock: Clock + causes: tuple[str, ...] + fatal: bool + physics_ticks: int + completed_controls: int + partial_control_ticks: int + completed_clock_cycles: int + all_sim_steps_attempted: int + all_sim_steps_completed: int + initialization_steps: int + rollout_steps: int + reset_calls: int + pose_write_calls: int + composer_buffer_replacements: int + + +@dataclass(frozen=True) +class StepResult: + observation: object | None + reward: object | None + ticks_advanced: int + stop: StopResult | None + + +EvidenceCallback = Callable[[RuntimeEvent, EvaluationCycleState], tuple[str, ...]] +SeamCallback = Callable[[RuntimeEvent, EvaluationCycleState], SeamDecision] + + +def _causes(value): + if not isinstance(value, tuple) or any(not isinstance(v, str) or not v for v in value): + raise TypeError('Callback must return a tuple of nonempty cause strings') + return value + + +def _fatal(causes): + return any(c.startswith(('nonfinite', 'hardware', 'identity', 'illegal_', 'storage', 'recording', + 'budget', 'process', 'infrastructure', 'guide_', 'auxiliary_')) for c in causes) + + +class EvaluationContactCycleEnv(ManagerBasedRLEnv): + """Direct construction only; no registration or inherited autoreset step. + + Required evidence callback: initial snapshot, then every post-latch tick + (including the old endpoint), then next_reference after admission/refresh. + Its returned causes join ALL runtime causes. Missing/raising callbacks stop. + Intermediate endpoint seam callback must return explicit SeamDecision(True). + No callback means no admission. Final endpoint has no next reference/reset. + """ + def __init__(self, cfg, *, protocol: Protocol, trial_index: int, runtime_hooks: RuntimeHooks, + evidence_callback: EvidenceCallback, seam_callback: SeamCallback | None = None, **kwargs): + if not runtime_hooks.active or runtime_hooks.mode != 'construction': + raise RuntimeError('Install RuntimeHooks before environment construction') + if not callable(evidence_callback): + raise ValueError('Evidence callback required before construction') + protocol.trial(trial_index) + if protocol.document['stage'] == 'P2' and trial_index != 0: + raise ValueError('P2 must start with preregistered row zero') + self.evaluation_protocol, self.evaluation_trial_index = protocol, trial_index + self.runtime_hooks = runtime_hooks + self.evidence_callback, self.seam_callback = evidence_callback, seam_callback + self.stop_result = None + self._started = self._initial_reset = self._finalized = False + self._consumed_rows = set() + cfg.seed = protocol.trial(trial_index).seed + if cfg.scene.num_envs != 1 or cfg.decimation != CONTROL_TICKS or cfg.sim.dt != PHYSICS_DT_S: + raise ValueError('Evaluation requires one environment and 500Hz/100Hz clocks') + try: + super().__init__(cfg, **kwargs) + if self._physics_handles_decimation or self.recorder_manager.active_terms: + raise ValueError('Require scalar physics ticks and external evidence callbacks, not manager recorders') + if 'interval' in self.event_manager.available_modes: + raise ValueError('Evaluation disallows unbound interval events') + initial_state = state(self) + # Preserve the constructor command, NOT a post-reset measured foot. + # Later P2 states are constructed at the previous row's terminal pose. + self._canonical_command_origins = (initial_state.origin.clone(), + initial_state.base_origin.clone()) + self._guard_trial_objects() + except Exception: + self.runtime_hooks.violations.append('infrastructure_constructor') + raise + finally: + self.runtime_hooks.mode = 'locked' + + def _guard_trial_objects(self): + s = state(self) + self.runtime_hooks.guard_composer(s.robot.permanent_wrench_composer) + objects = [s.controller, s.schedule, s.latch, s.separation, s.acquisition, + s.sensor_delay, s.action_delay] + objects += [getattr(self, name) for name in ( + 'action_manager', 'observation_manager', 'reward_manager', 'termination_manager', + 'command_manager', 'event_manager', 'curriculum_manager')] + objects += list(self.scene.sensors.values()) + list(s.robot.actuators.values()) + for obj in objects: + self.runtime_hooks.guard_object(obj, ('reset',)) + + def _event(self, kind, causes=()): + s = state(self) + g = s.command_clock.global_tick + held = (g - 1) // CONTROL_TICKS if kind == 'tick' else None + return RuntimeEvent(kind, self.evaluation_trial_index, s.command_clock, s.protocol_command(), held, + None if held is None else held * CONTROL_TICKS, s.reference_used_clock, + vector_tuple(s.reference_used), s.reference_clock, vector_tuple(s.controller.action), + tuple(causes)) + + def _evidence(self, event): + try: + return _causes(self.evidence_callback(event, state(self))) + except Exception as exc: + return (f'recording_callback:{type(exc).__name__}:{exc}',) + + def _stop(self, causes=()): + causes = tuple(dict.fromkeys((*causes, *( + ('illegal_reset_or_pose_write',) if self.runtime_hooks.violations else ())))) + if self.stop_result is not None: + combined = tuple(dict.fromkeys((*self.stop_result.causes, *causes))) + if combined != self.stop_result.causes: + self.stop_result = replace(self.stop_result, reason='failure', causes=combined, + fatal=_fatal(combined)) + return self.stop_result + s = state(self) + h = self.runtime_hooks + trial_index = self.evaluation_trial_index + g = int(s.substeps[0]) if s.trial.index == trial_index else 0 + clock = (s.protocol.evidence_clock(trial_index, g) if g else + s.protocol.next_command_clock(trial_index, 0)) + self.stop_result = StopResult('failure' if causes else 'protocol_complete', trial_index, clock, + causes, _fatal(causes), g, g // CONTROL_TICKS, g % CONTROL_TICKS, + g // CYCLE_TICKS, h.attempted_steps, h.completed_steps, + h.initialization_steps, h.rollout_steps, h.reset_calls, h.pose_write_calls, + h.composer_buffer_replacements) + return self.stop_result + + def reset(self, seed=None, env_ids=None, options=None): + if self._started or self._initial_reset or env_ids is not None or options is not None: + self.runtime_hooks.violations.append('env.reset') + if hasattr(self, '_cycle_state'): + self._stop(('illegal_reset_or_pose_write',)) + raise RuntimeViolation('Only one full initial reset per trial is permitted') + trial = self.evaluation_protocol.trial(self.evaluation_trial_index) + if seed is not None and seed != trial.seed: + raise ValueError('Reset seed must match bound trial') + self._initial_reset = True + self._started = True # failed initialization is consumed, never retried + self._consumed_rows.add(trial.index) + try: + with self.runtime_hooks.scope('initialization'): + # Base reset performs reset events + forward; its provisional observation + # is not published. Final capture/refresh follows forward before inference. + ManagerBasedRLEnv.reset(self, seed=trial.seed) + s = state(self) + s.initialize_trial() + causes = s.safety_causes() + causes = (*causes, *self._evidence(self._event('initial', causes))) + if causes or self.runtime_hooks.violations: + self._stop(causes) + return None, {'evaluation_stop': self.stop_result} + self.obs_buf = self.observation_manager.compute(update_history=False) + return self.obs_buf, self.extras + except Exception as exc: + self._stop((f'infrastructure_initialization:{type(exc).__name__}:{exc}',)) + raise + finally: + self._initial_reset = False + + def _reset_idx(self, env_ids): + if not self._initial_reset: + self.runtime_hooks.violations.append('env._reset_idx') + raise RuntimeViolation('No automatic or partial reset allowed') + self.runtime_hooks.require_initialization('env._reset_idx') + ManagerBasedRLEnv._reset_idx(self, env_ids) + + def reset_to(self, *args, **kwargs): + self.runtime_hooks.violations.append('env.reset_to') + raise RuntimeViolation('Evaluation never accepts reset_to') + + def acknowledge_finalized(self, result: StopResult): + if result is not self.stop_result or result is None: + raise ValueError('Acknowledge the exact stopped row after preserving evidence') + self._finalized = True + + def begin_trial(self, protocol: Protocol, trial_index: int): + if (protocol is not self.evaluation_protocol or protocol.document['stage'] != 'P2' + or self.stop_result is None or self.stop_result.fatal or not self._finalized + or self.runtime_hooks.violations or trial_index != self.evaluation_trial_index + 1 + or trial_index in self._consumed_rows): + raise RuntimeViolation('No active/fatal/retried/skipped/unfinalized or P1 between-row reset') + protocol.trial(trial_index) + self.evaluation_trial_index = trial_index + self.stop_result = None + self._started = self._finalized = False + try: + with self.runtime_hooks.scope('initialization'): + # New row only. Never used at a continuous-cycle seam. + self._cycle_state = EvaluationCycleState(self) + # Restore command inputs before the physical reset refreshes IK. + # Never called at a continuous-cycle seam; no physical pose write. + self._cycle_state.origin.copy_(self._canonical_command_origins[0]) + self._cycle_state.base_origin.copy_(self._canonical_command_origins[1]) + self._guard_trial_objects() + return self.reset() + except Exception as exc: + self._started = True + self._consumed_rows.add(trial_index) + self._stop((f'infrastructure_begin_trial:{type(exc).__name__}:{exc}',)) + raise + + def step(self, action): + if not self._started or self.stop_result is not None: + raise RuntimeViolation('Initialize trial once; never step a stopped trial') + if (not isinstance(action, torch.Tensor) or action.shape != (1, 3) or action.dtype != torch.float32 + or action.device != torch.device(self.device) or not torch.isfinite(action).all()): + return StepResult(None, None, 0, self._stop(('nonfinite_or_invalid_action',))) + s = state(self) + if s.trial.controller_kind == 'analytic_reference' and bool(action.any()): + return StepResult(None, None, 0, self._stop(('identity_nonzero_reference_residual',))) + start = int(s.substeps[0]) + reward = None + try: + if self.runtime_hooks.violations: + return StepResult(None, None, 0, self._stop()) + self.action_manager.process_action(action) + for _ in range(CONTROL_TICKS): + self._sim_step_counter += 1 + with self.runtime_hooks.wrench_update(s.robot.permanent_wrench_composer): + self.action_manager.apply_action() + self.scene.write_data_to_sim() + with self.runtime_hooks.scope('rollout'): + self.sim.step(render=False) + self.scene.update(dt=self.physics_dt) + s.after_physics(self.physics_dt) + causes = s.safety_causes() + event = self._event('tick', causes) + causes = (*causes, *self._evidence(event)) + if causes or self.runtime_hooks.violations: + return StepResult(None, None, int(s.substeps[0]) - start, self._stop(causes)) + # Safety/snapshot always precedes rendering or another physical step. + if self._sim_step_counter % self.cfg.sim.render_interval == 0 and self.sim.is_rendering: + self.sim.render(skip_app_pumping=not self.render_enabled) + self.episode_length_buf += 1 + self.common_step_counter += 1 + reward = self.reward_manager.compute(dt=self.step_dt) + g = int(s.substeps[0]) + if g == s.trial.total_ticks: + return StepResult(None, reward, CONTROL_TICKS, self._stop()) + admitted = False + if g % CYCLE_TICKS == 0: + decision = None if self.seam_callback is None else self.seam_callback(event, s) + if not isinstance(decision, SeamDecision) or type(decision.admitted) is not bool: + return StepResult(None, reward, CONTROL_TICKS, self._stop(('seam_not_admitted',))) + causes = _causes(decision.causes) + if not decision.admitted or causes or self.runtime_hooks.violations: + return StepResult(None, reward, CONTROL_TICKS, + self._stop(causes or ('seam_not_admitted',))) + admitted = True + clock = s.protocol.next_command_clock(s.trial.index, g, seam_admitted=admitted) + self.command_manager.compute(dt=self.step_dt) + s.advance_reference(clock) + # Do not re-label the old physical sample with the new seam phase. + # Only freshly computed controller values are new evidence here. + reference_finite = bool(torch.isfinite(s.controller.q_ik).all() + and torch.isfinite(s.controller.action).all()) + causes = () if reference_finite else ('nonfinite_reference',) + causes = (*causes, *self._evidence(self._event('next_reference', causes))) + if causes or self.runtime_hooks.violations: + return StepResult(None, reward, CONTROL_TICKS, self._stop(causes)) + self.obs_buf = self.observation_manager.compute(update_history=True) + return StepResult(self.obs_buf, reward, CONTROL_TICKS, None) + except Exception as exc: + return StepResult(None, reward, int(s.substeps[0]) - start, + self._stop((f'infrastructure_step:{type(exc).__name__}:{exc}',))) diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_instrumentation.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_instrumentation.py new file mode 100644 index 0000000..200a49d --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_instrumentation.py @@ -0,0 +1,421 @@ +"""Read-only observers of the frozen schedule return and existing unique writer. + +Install after RuntimeHooks/environment construction, before initial reset; unwind +before RuntimeHooks. No simulator imports at module import. Identity verification +and qualification of the mandatory backend RNG inventory precede construction. +""" +from __future__ import annotations + +import copy +import random +import sys +from contextlib import ExitStack +from functools import wraps +from types import MappingProxyType + +import numpy as np +import torch + +from SkillFactory.legs.contact_cycle.mechanics import rotate + + +class InstrumentationError(RuntimeError): + """Incomplete, unsupported or misaligned observable evidence.""" + + +# Exact classes only: subclasses may contain unobserved recurrent/delay state. +ACTUATOR_MODULE = 'isaaclab.actuators.actuator_pd' +ACTUATOR_FIELDS = frozenset([ + '_num_envs', '_device', '_joint_names', '_joint_indices', 'joint_property_resolution_table', + 'velocity_limit_sim', 'effort_limit_sim', 'stiffness', 'damping', 'armature', 'friction', + 'dynamic_friction', 'viscous_friction', 'velocity_limit', 'effort_limit', 'computed_effort', 'applied_effort', +]) +DELAY_FIELDS = frozenset(['_history_length', '_min_time_lag', '_max_time_lag', '_time_lags', '_circular_buffer']) +CIRCULAR_FIELDS = frozenset(['_batch_size', '_device', '_ALL_INDICES', '_max_len', '_num_pushes', '_buffer']) +DELAY_NAMES = ('positions_delay_buffer', 'velocities_delay_buffer', 'efforts_delay_buffer') +SCHEDULE_FIELDS = ('fired', 'start', 'envelope', 'integral', 'completed') + + +def _tensor(value): + if isinstance(value, torch.Tensor): + return value + # Pinned Warp arrays (not ProxyArray output properties that trigger composition). + import warp as wp + if not isinstance(value, wp.array): + raise InstrumentationError('Unsupported backend buffer') + return wp.to_torch(value) + + +def _copy(value): + """Owned JSON-compatible values; no returned tensor shares live storage.""" + if isinstance(value, torch.Tensor): + return value.detach().cpu().clone().tolist() + if isinstance(value, np.ndarray): + return value.copy().tolist() + if isinstance(value, np.generic): + return value.item() + if value is None or type(value) in (str, bool, int, float): + return value + if isinstance(value, slice): + return {'slice': [_copy(value.start), _copy(value.stop), _copy(value.step)]} + if isinstance(value, (tuple, list)): + return [_copy(v) for v in value] + if isinstance(value, dict) and all(type(k) is str for k in value): + return {k: _copy(v) for k, v in value.items()} + raise InstrumentationError(f'Unsupported snapshot value: {type(value)}') + + +def _exact_class(obj, module, name): + loaded = sys.modules.get(module) + if loaded is None or type(obj) is not getattr(loaded, name, None): + raise InstrumentationError(f'Unsupported hidden state: require exact {module}.{name}') + + +def _fields(obj, expected, *, ignored=()): + if set(vars(obj)) - set(ignored) != set(expected): + raise InstrumentationError(f'Unsupported hidden state layout: {type(obj).__name__}') + return {name: getattr(obj, name) for name in sorted(expected)} + + +def actuator_snapshot(robot): + """Pinned observable Lab actuator memory, including full unrolled delay storage. + + Implicit means no recurrent Python controller memory, NOT no PhysX solver + state. Solver internals are not represented as an empty actuator dictionary. + """ + if getattr(robot, '_has_newton_actuators', False) or getattr(robot, 'newton_actuator_adapter', None) is not None: + raise InstrumentationError('Unsupported hidden Newton actuator state') + if not robot.actuators: + raise InstrumentationError('No bound actuator inventory') + groups = {} + for name, actuator in robot.actuators.items(): + kind = type(actuator).__name__ + if kind not in ('ImplicitActuator', 'IdealPDActuator', 'DelayedPDActuator'): + raise InstrumentationError('Unsupported hidden actuator state: ' + kind) + _exact_class(actuator, ACTUATOR_MODULE, kind) + delayed = kind == 'DelayedPDActuator' + fields = ACTUATOR_FIELDS | (set(DELAY_NAMES) | {'_ALL_INDICES'} if delayed else set()) + data = _fields(actuator, fields, ignored=('cfg', 'reset')) + for key in DELAY_NAMES if delayed else (): + delay = data[key] + _exact_class(delay, 'isaaclab.utils.buffers.delay_buffer', 'DelayBuffer') + delay_data = _fields(delay, DELAY_FIELDS) + circular = delay_data['_circular_buffer'] + _exact_class(circular, 'isaaclab.utils.buffers.circular_buffer', 'CircularBuffer') + delay_data['_circular_buffer'] = _fields(circular, CIRCULAR_FIELDS) + data[key] = delay_data + groups[name] = {'class': ACTUATOR_MODULE + '.' + kind, + 'memory': 'delayed' if delayed else 'stateless_controller', + 'implicit_solver': kind == 'ImplicitActuator', 'buffers': _copy(data)} + targets = {key: _copy(_tensor(getattr(robot, key))) for key in + ('_joint_pos_target_sim', '_joint_vel_target_sim', '_joint_effort_target_sim')} + targets.update({key: _copy(getattr(robot.data, key).torch) for key in + ('joint_pos_target', 'joint_vel_target', 'joint_effort_target')}) + return {'groups': groups, 'targets': targets} + + +def qualify_backend_rng(snapshot): + """Validate coverage structure, never certify a provider's completeness claim. + + Sources/evidence and the bounded scope must be independently bound/reviewed. + Native internals are explicitly unobserved, never constant pseudo-state. + """ + snapshot = _copy(snapshot) + if (not isinstance(snapshot, dict) + or set(snapshot) != {'coverage', 'evidence_scope', 'unobserved_internals'} + or not isinstance(snapshot['coverage'], dict) or not snapshot['coverage']): + raise InstrumentationError('Explicit backend RNG coverage required') + scope = snapshot['evidence_scope'] + if (not isinstance(scope, dict) or set(scope) != {'format', 'equality', 'sources', 'claims'} + or scope['format'] != 'skillfactory.observable-state-scope.v1' + or scope['equality'] != 'recorded_application_controller_and_enumerated_observable_rng_only' + or not isinstance(scope['claims'], dict) + or set(scope['claims']) != {'complete_backend_state_equality', 'deterministic_replay'} + or any(value is not False for value in scope['claims'].values()) + or not isinstance(scope['sources'], list) or not scope['sources'] + or any(type(p) is not str or not p for p in scope['sources'])): + raise InstrumentationError('Bound/versioned observable evidence scope required') + internals = snapshot['unobserved_internals'] + if (not isinstance(internals, dict) or set(internals) != { + 'physx_solver_contact_caches', 'internal_stochastic_state_if_any', 'numerical_scheduling_effects'} + or any(value != 'unobserved' for value in internals.values())): + raise InstrumentationError('Native internals must be explicitly unobserved') + for name, entry in snapshot['coverage'].items(): + if (not name or not isinstance(entry, dict) + or set(entry) != {'status', 'sources', 'evidence', 'state'} + or not isinstance(entry['sources'], list) or not entry['sources'] + or any(type(p) is not str or not p for p in entry['sources']) + or type(entry['evidence']) is not str or not entry['evidence']): + raise InstrumentationError('Invalid backend RNG source coverage') + if entry['status'] == 'observed': + if entry['state'] is None or entry['state'] == {} or entry['state'] == []: + raise InstrumentationError('Missing observable backend RNG state') + elif entry['status'] == 'not_applicable': + if entry['state'] is not None: + raise InstrumentationError('Not-applicable RNG must not invent state') + else: + raise InstrumentationError('Unavailable/unqualified backend RNG coverage: ' + name) + return snapshot + + +def rng_snapshot(state, backend_rng, generators): + extra = {} + for name, generator in generators.items(): + if type(generator) is random.Random: + value = generator.getstate() + elif type(generator) is np.random.Generator: + value = generator.bit_generator.state + elif type(generator) is np.random.RandomState or type(generator) is torch.Generator: + value = generator.get_state() + else: + raise InstrumentationError('Unsupported RNG generator: ' + name) + extra[name] = _copy(value) + return {'python': _copy(random.getstate()), 'numpy': _copy(np.random.get_state()), + 'generators': extra, 'backend': qualify_backend_rng(backend_rng(state))} + + +class EvaluationInstrumentation: + """One environment, one context; supplemental(state) matches recorder schema. + + Repeated reads retain the *same completed interval*, including next_reference + and seam fingerprints. Only runtime tick records integrate delivery. A new + P2 state is attached at its initial supplemental call (before inference). + """ + def __init__(self, env, *, backend_rng, generators): + if not callable(backend_rng) or not isinstance(generators, dict) or any( + type(k) is not str or not k for k in generators): + raise InstrumentationError('Explicit backend RNG provider and named generator inventory required') + self.env, self.backend_rng = env, backend_rng + self.generators = dict(generators) + self._stack = self._schedule_stack = None + self._state = self._pending = self._capture = self._schedule_state = None + self._last_tick = 0 + self._fault = False + + @staticmethod + def _patch(stack, obj, name, wrapper): + own, old = name in vars(obj), getattr(obj, name) + setattr(obj, name, wrapper) + stack.callback(lambda: setattr(obj, name, old) if own else delattr(obj, name)) + + def __enter__(self): + h = self.env.runtime_hooks + if self._stack is not None or not h.active or h.mode != 'locked': + raise InstrumentationError('Install inside active RuntimeHooks after construction') + self._stack = ExitStack() + try: + s = self.env._cycle_state + self._composer = s.robot.permanent_wrench_composer + if (id(self._composer), 'reset') not in h._guarded: + raise InstrumentationError('Runtime composer guard must be installed first') + old = self._composer.set_forces_and_torques_index + if hasattr(old, '_evaluation_instrumentation_owner'): + raise InstrumentationError('Nested unique-writer instrumentation is not supported') + self._attach(s) + + @wraps(old) + def setter(*args, **kwargs): + try: + evidence = self._before_setter(args, kwargs) + result = old(*args, **kwargs) # preserve guard's replacement permission scope + self._check_buffers(kwargs) + self._capture, self._pending = evidence, None + self._last_tick = evidence['global_tick'] + return result + except BaseException: + self._fault = True + self._capture = self._pending = None + raise + + setter._evaluation_instrumentation_owner = self + self._patch(self._stack, self._composer, 'set_forces_and_torques_index', setter) + return self + except BaseException: + self.__exit__(None, None, None) + raise + + def __exit__(self, *exc): + try: + if self._schedule_stack is not None: + self._schedule_stack.close() + finally: + if self._stack is not None: + self._stack.close() + self._stack = self._schedule_stack = None + self._pending = self._capture = None + + def _attach(self, s): + if int(s.substeps[0]) != 0 or s.robot.permanent_wrench_composer is not self._composer: + raise InstrumentationError('Attach only new initial state on the bound composer') + if self._pending is not None or (self._state is not None and s.trial.index != self._state.trial.index + 1): + raise InstrumentationError('Invalid P2 state transition') + if self._schedule_stack is not None: + self._schedule_stack.close() + self._state, self._capture, self._schedule_state = s, None, None + self._last_tick = 0 + self._schedule_stack = ExitStack() + old = s.schedule.step + + @wraps(old) + def scheduled(*args, **kwargs): + try: + tick = int(s.substeps[0]) + 1 + if (self._fault or s is not self.env._cycle_state or self._pending is not None + or tick != self._last_tick + 1 + or self.env.runtime_hooks._load_owner is not self._composer): + raise InstrumentationError('Unexpected/duplicate schedule invocation') + if s.schedule.commands is not None or s.schedule.delays is not None or s.sampled_points is not None: + raise InstrumentationError('Unbound schedule overrides') + values = old(*args, **kwargs) # the ONLY scheduler invocation + self._pending = (tick, values.detach().clone(), self._poses(s)) + self._schedule_state = {k: _copy(getattr(s.schedule, k)) for k in SCHEDULE_FIELDS} + return values + except BaseException: + self._fault = True + self._pending = self._capture = None + raise + + self._patch(self._schedule_stack, s.schedule, 'step', scheduled) + + @staticmethod + def _poses(s): + d = s.robot.data + return tuple(t.detach().clone() for t in (d.body_link_pos_w.torch, + d.body_link_quat_w.torch, d.body_com_pos_w.torch, s.env.scene.env_origins)) + + def _before_setter(self, args, kw): + s = self._state + if (self._fault or self._pending is None or s is not self.env._cycle_state + or self.env.runtime_hooks._load_owner is not self._composer): + raise InstrumentationError('Missing matching schedule result') + tick, values, poses = self._pending + if tick != int(s.substeps[0]) + 1 or any(not torch.equal(a, b) for a, b in zip(poses, self._poses(s))): + raise InstrumentationError('Pre-step pose/tick changed between schedule and setter') + if (args or set(kw) != {'forces', 'torques', 'body_ids', 'positions', 'is_global'} + or kw['positions'] is not None or kw['is_global'] is not True + or _copy(kw['body_ids']) != list(range(len(s.robot.body_names)))): + raise InstrumentationError('Require exact frozen all-body world COM setter') + total = torch.cat((kw['forces'], kw['torques']), -1).detach().clone() + if total.shape != s.external.shape or not torch.equal(total, s.external + s.guide + s.auxiliary): + raise InstrumentationError('Actual setter arguments differ from composed buffers') + bpos, bquat, com, origin = poses + identity = bquat.new_tensor([[0., 0., 0., 1.]]) + external = torch.zeros_like(total) + events = [] + selected = tuple(i for i, e in enumerate(s.cc['events']) if e['scenario'] == s.trial.scenario_id) + if selected != s.trial.event_indices or int(s.scenarios[0]) != s.trial.scenario_id: + raise InstrumentationError('Event/trial identity mismatch') + for j, e in enumerate(s.cc['events']): + body = s.robot.body_names.index(e['body']) + frames = {'world': (origin, identity), 'mount': (bpos[:, s.anchor], bquat[:, s.anchor]), + 'body': (bpos[:, body], bquat[:, body])} + point_origin, point_quat = frames[e['point_frame']] + quat = frames[e['frame']][1] + point = point_origin + rotate(point_quat, values.new_tensor([e['point_m']])) + force, couple = rotate(quat, values[:, j, :3]), rotate(quat, values[:, j, 3:]) + external[:, body] += torch.cat((force, couple + torch.cross(point - com[:, body], force, dim=-1)), -1) + if j in selected: + events.append({'event_index': j, 'body_index': body, + 'envelope': self._schedule_state['envelope'][0][j], + **{k: _copy(v[0]) for k, v in { + 'force_world': force, 'couple_world': couple, 'point_world': point, + 'com_world': com[:, body], 'frame_quat_xyzw': quat, + 'point_frame_quat_xyzw': point_quat, + 'point_frame_origin_world': point_origin}.items()}}) + elif bool(values[:, j].any()): + raise InstrumentationError('Unselected event produced a wrench') + if not torch.equal(external, s.external): + raise InstrumentationError('Observed schedule/frames disagree with external COM buffer') + return {'format': 'skillfactory.contact-cycle-applied-wrench.v1', 'trial_index': s.trial.index, + 'global_tick': tick, 'events': events, 'total_com_wrench': _copy(total[0])} + + def _check_buffers(self, kw): + # Do not read out_force_b/out_torque_b: their accessors mutate dirty composition. + c = self._composer + for name, expected in (('global_force_at_com_w', kw['forces']), ('global_torque_w', kw['torques'])): + if not torch.equal(_tensor(getattr(c, name)), expected): + raise InstrumentationError('Composer input buffer differs from actual submission: ' + name) + for name in ('global_force_w', 'local_force_b', 'local_torque_b'): + if bool(_tensor(getattr(c, name)).any()): + raise InstrumentationError('Unexpected positional/global or local composer contribution') + + def supplemental(self, s): + if self._stack is None or self._fault or s is not self.env._cycle_state: + raise InstrumentationError('Inactive/faulted instrumentation or foreign state') + if s is not self._state: + self._attach(s) # begin_trial replaces state; first callback is initial at tick zero + tick = int(s.substeps[0]) + if self._pending is not None: + raise InstrumentationError('Schedule result without completed unique setter') + if tick == 0: + if self._capture is not None: + raise InstrumentationError('Future application at initial snapshot') + applied = None + else: + if (self._capture is None + or (self._capture['trial_index'], self._capture['global_tick']) != (s.trial.index, tick)): + raise InstrumentationError('Missing/stale/future applied evidence') + if self._schedule_state != {k: _copy(getattr(s.schedule, k)) for k in SCHEDULE_FIELDS}: + raise InstrumentationError('Scheduler changed after pre-step capture') + applied = copy.deepcopy(self._capture) if s.trial.event_indices else None + actuators = actuator_snapshot(s.robot) + # Frozen project delay lines are separate from backend actuator delays. + from SkillFactory.legs.contact_cycle.task import DelayLine + for name in ('sensor_delay', 'action_delay'): + line = getattr(s, name) + if type(line) is not DelayLine: + raise InstrumentationError('Unsupported project delay history') + actuators[name] = _copy(_fields(line, {'delay', 'data'}, ignored=('reset',))) + return {'actuator_state': actuators, 'rng': rng_snapshot(s, self.backend_rng, self.generators), + 'applied': applied} + + +# Bind these actual imported modules (not merely matching bytes in another tree). +BACKEND_SOURCE_INVENTORY = MappingProxyType({ + 'isaaclab_physx.assets.articulation.articulation': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/assets/articulation/articulation.py', + '47d96553282b389493e69f691b8dcbfeb98997a5a80b88c2571c60ba8b58cb6d'), + 'isaaclab_physx.assets.articulation.articulation_data': ( + '/home/cen/IsaacLab/source/isaaclab_physx/isaaclab_physx/assets/articulation/articulation_data.py', + '43a31992353ad3a500024d0ec908d5b253ada7a0aac9a1cd2261466e10bda4ff'), + 'isaaclab.actuators.actuator_base': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/actuators/actuator_base.py', + 'de715f048558dfeb9917ec48d7a110e6942eef6f242e9bab379281e8398beedb'), + 'isaaclab.actuators.actuator_pd': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/actuators/actuator_pd.py', + '105f0341f972102f6bd5b0c544bc7c0510662241389a9decd24406bb2371d27e'), + 'isaaclab.utils.buffers.delay_buffer': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/utils/buffers/delay_buffer.py', + '648ce0cf011798a1ab4a8980bafbbeba16ef8cf02affbc404c67ff312ac02da2'), + 'isaaclab.utils.buffers.circular_buffer': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/utils/buffers/circular_buffer.py', + 'f1443d6eaa8b7bbfcfea6d70249143b9bf5a3deda16a2b2cfa00ea01a5af4155'), + 'isaaclab.utils.wrench_composer': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/utils/wrench_composer.py', + '8fb3ab4e59db121165db52daf61e0de0e73653f0c154a143f6a828c198082c06'), + 'isaaclab.utils.warp.kernels': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/utils/warp/kernels.py', + '9991d03fe46fa964b7e9531d5fba8e1b44a0701926d3c900f1e1ac3cf84ad906'), + 'isaaclab.utils.warp.utils': ( + '/home/cen/IsaacLab/source/isaaclab/isaaclab/utils/warp/utils.py', + 'dfffeb6d683061a0e169e4e28f6f05137d6b2dfdeb8953b0878b6dc1b45705de'), +}) + + +def verify_backend_sources(): + """Read-only imported-source precheck; NOT a replacement for verify_binding. + + Eagerly import these dependencies in the authorized runtime setup first. The + binding owner also inventories Torch/numpy/Warp and the backend RNG provider, + plus its explicitly declared state sources, before environment construction. + """ + from hashlib import sha256 + from pathlib import Path + + for name, (path, digest) in BACKEND_SOURCE_INVENTORY.items(): + module = sys.modules.get(name) + file = getattr(module, '__file__', None) + spec = getattr(module, '__spec__', None) + if (file != path or getattr(spec, 'origin', None) != path + or str(Path(path).resolve()) != path or sha256(Path(path).read_bytes()).hexdigest() != digest): + raise InstrumentationError('Actual imported backend identity mismatch: ' + name) diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_mdp.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_mdp.py new file mode 100644 index 0000000..9331ffe --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_mdp.py @@ -0,0 +1,191 @@ +"""Protocol commands with frozen observation, residual, IK and wrench math. + +No refresh in after_physics: the environment admits the endpoint before advancing +command metadata and refreshing the carried controller exactly once. +""" +from dataclasses import dataclass + +import torch + +from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES +from SkillFactory.legs.contact_cycle_sim.evaluation_protocol import CONTROL_TICKS, PHYSICS_DT_S, Protocol +from SkillFactory.tasks.manager_based.contact_cycle import mdp as base +from SkillFactory.tasks.manager_based.single_leg_anchor.mdp.actions import BoundedJointPositionAction + +from .mdp import SimCycleState, SimResidualAction + + +@dataclass(frozen=True) +class CapturedAnchors: + anchor_a_world: tuple[float, float, float] + root_origin_world: tuple[float, float, float] + initial_root_world: tuple[float, float, float] + + +def vector_tuple(value): + return tuple(value.detach().cpu().reshape(-1).tolist()) + + +class EvaluationCycleState(SimCycleState): + def __init__(self, env): + self.protocol: Protocol = env.evaluation_protocol + self.trial = self.protocol.trial(env.evaluation_trial_index) + self.command_clock = self.protocol.next_command_clock(self.trial.index, 0) + self.reference_clock = self.command_clock + self.reference_used_clock = None + self._anchors = None + super().__init__(env) + self._install_descriptor() + + @property + def anchors(self): + return self._anchors + + def _install_descriptor(self): + # Select existing events, never introduce a second force writer or sampler. + expected = self.protocol.document['events'] + actual = self.cc['events'] + if expected and tuple(actual) != tuple(dict(e) for e in expected): + # JSON lists versus immutable protocol tuples are normalized explicitly. + for a, b in zip(actual, expected, strict=True): + if a.keys() != b.keys() or any( + (tuple(a[k]) if isinstance(a[k], list) else a[k]) != b[k] for k in a + ): + raise ValueError('Bound event program differs from frozen wrench schedule') + indices = tuple(i for i, e in enumerate(actual) if e['scenario'] == self.trial.scenario_id) + if indices != self.trial.event_indices: + raise ValueError('Bound scenario/event indices mismatch') + self.scenarios.fill_(self.trial.scenario_id) + self.stand_only.zero_() + if self.sampler is not None or self.schedule.commands is not None or self.schedule.delays is not None: + raise ValueError('Evaluation disallows sampler/event overrides') + + def reset(self, ids): + self.env.runtime_hooks.require_initialization('state.reset') + super().reset(ids) + self._install_descriptor() # inherited nominal reset must not win + + def set_sampler(self, sampler): + raise RuntimeError('Evaluation descriptor is immutable; no sampler installation') + + def initialize_trial(self): + """Called after the single physical reset/forward, before public inference.""" + if self._anchors is not None: + raise RuntimeError('Anchors already captured') + # Frozen CycleState retains the constructor's ground target across reset. + # The forwarded measured foot is sensor evidence, not a new command anchor. + a = self.origin[0].clone() + self._anchors = CapturedAnchors(vector_tuple(a), vector_tuple(self.base_origin[0]), + vector_tuple(self.root()[0][0])) + ids = torch.arange(self.env.num_envs, device=self.env.device) + # Refresh reset sensor histories from the forwarded initial pose. + self.reset(ids) + self.reference_clock = self.command_clock + + def protocol_command(self): + anchors = self.anchors + return self.protocol.command( + self.trial.index, self.command_clock, + anchor_a_world=vector_tuple(self.origin[0]) if anchors is None else anchors.anchor_a_world, + root_origin_world=vector_tuple(self.base_origin[0]) if anchors is None else anchors.root_origin_world, + weight_N=float(self.weight[0])) + + def reference(self): + c = self.protocol_command() + return (torch.tensor([c.phase], device=self.env.device, dtype=torch.long), + self.time.new_tensor([c.progress]), self.time.new_tensor([c.target_world_m]), + self.time.new_tensor([c.desired_normal_N]), self.time.new_tensor([c.auxiliary_fraction])) + + def port_reference(self): + c = self.protocol_command() + return (self.time.new_tensor([c.root_target_world_m]), + self.time.new_tensor([c.root_velocity_world_m_s])) + + def after_physics(self, dt): + if dt != PHYSICS_DT_S: + raise ValueError('Evaluation requires individual 0.002s physics ticks') + tick = int(self.substeps[0]) + 1 + self.command_clock = self.protocol.evidence_clock(self.trial.index, tick) + # Deliberately bypass SimCycleState.after_physics's automatic IK refresh. + base.CycleState.after_physics(self, dt) + + def advance_reference(self, clock): + if clock.global_tick % CONTROL_TICKS: + raise ValueError('Reference refresh requires control boundary') + self.command_clock = clock + self.controller.refresh(torch.arange(self.env.num_envs, device=self.env.device)) + self.reference_clock = clock + + def safety_causes(self): + """Instantaneous floors only; window/dwell/delivery gates belong to callback owner.""" + flags = base.failure_flags(self.env)[0].tolist() + causes = [name for name, hit in zip(FAILURE_NAMES, flags) if hit] + f = self.protocol.document['thresholds']['functional'] + d = self.robot.data + lower = self.time.new_tensor([j['limits']['lower'] for j in self.bundle['joints']]) + upper = self.time.new_tensor([j['limits']['upper'] for j in self.bundle['joints']]) + speed = self.time.new_tensor([j['limits']['velocity'] for j in self.bundle['joints']]) + torque, velocity = d.applied_torque.torch[:, self.joints], d.joint_vel.torch[:, self.joints] + root = self.root()[0] + foot = self.foot_center() + gap = foot[:, 2] - self.bundle['foot']['radius'] + error = root - self.port_reference()[0] + auxiliary_vertical = self.auxiliary[..., 2] + auxiliary_moment = self.auxiliary[..., 3:].norm(dim=-1) + finite = torch.cat((d.root_state_w.torch.flatten(), self.q().flatten(), velocity.flatten(), + torque.flatten(), self.external.flatten(), self.guide.flatten(), + self.auxiliary.flatten(), self.sensed.flatten(), foot.flatten(), + self.controller.q_ik.flatten(), self.controller.action.flatten())) + checks = { + 'nonfinite': not bool(torch.isfinite(finite).all()), + 'hardware_effort_velocity': bool(torch.maximum((torque / self.effort).abs().amax(), + (velocity / speed).abs().amax()) + > f['normalized_effort_velocity_max']), + 'hardware_joint_limit': bool(torch.maximum(lower - self.q(), self.q() - upper).amax() + > f['joint_limit_excess_rad_max']), + 'guide_vertical': bool(self.guide[..., 2].abs().amax() != 0), + 'root_height_error': bool(error[:, 2].abs().amax() > f['root_height_error_m_max']), + 'ground_penetration': bool(gap.min() < -f['ground_penetration_m_max']), + 'negative_normal': bool(self.contact()[:, 2].min() < -f['normal_force_negative_tolerance_N']), + 'auxiliary_force': bool(auxiliary_vertical.sum(-1).amax() > f['auxiliary_vertical_N_max']), + # Preserve the original functional auditor's point-applied fixture checks/tolerance. + 'auxiliary_geometry': bool( + (auxiliary_moment > f['auxiliary_lever_arm_m_max'] * auxiliary_vertical.clamp_min(0) + 1e-6).any() + or self.auxiliary[..., :2].abs().amax() > 1e-6 + or auxiliary_vertical.amin() < -1e-6 or self.auxiliary[..., 5].abs().amax() > 1e-6), + 'auxiliary_support_phase': bool(self.protocol_command().phase == 0 + and self.auxiliary.abs().amax() > 1e-6), + } + if self.protocol.document['stage'] == 'P1': + m = self.protocol.document['thresholds']['P1_margins'] + checks['P1_root_horizontal'] = bool(error[:, :2].norm(dim=-1).amax() > m['root_horizontal_error_m_max']) + checks['P1_root_z'] = bool(error[:, 2].abs().amax() > m['root_absolute_z_error_m_max']) + local = self.command_clock.local_tick + if local >= 4400: + checks['P1_reload_position'] = bool((foot - self.reference()[2]).norm(dim=-1).amax() + > m['reload_instantaneous_3d_error_m_max']) + if local == 4500: + initial = root.new_tensor(self.anchors.initial_root_world) + checks['P1_seam_root_drift'] = bool((root - initial)[:, :2].norm(dim=-1).amax() + > m['seam_to_initial_root_horizontal_drift_m_max']) + return tuple(dict.fromkeys(causes + [name for name, hit in checks.items() if hit])) + + +def state(env): + if not hasattr(env, '_cycle_state'): + env._cycle_state = EvaluationCycleState(env) + if not isinstance(env._cycle_state, EvaluationCycleState): + raise RuntimeError('Wrong evaluation state contract') + return env._cycle_state + + +class EvaluationResidualAction(SimResidualAction): + def __init__(self, cfg, env): + # Install the new state before any frozen lazy-state helper can run. + BoundedJointPositionAction.__init__(self, cfg, env) + state(env) + + def process_actions(self, actions): + super().process_actions(actions) + s = state(self._env) + s.reference_used_clock = s.reference_clock diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_recording.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_recording.py new file mode 100644 index 0000000..c49a2bd --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_recording.py @@ -0,0 +1,314 @@ +"""Read-only synchronous evidence callbacks. No reset/refresh/scene-update wrappers. + +The future runner supplies actor records, resource checks, stop results and bound +actuator/RNG snapshots. No default unlimited disk/time budget is provided. +""" +from __future__ import annotations + +import json +import lzma +import math +import os +from dataclasses import asdict +from pathlib import Path + +import torch + +from SkillFactory.legs.contact_cycle.mechanics import inverse_rotate +from SkillFactory.legs.contact_cycle_sim.config import pose_cost +from SkillFactory.legs.contact_cycle_sim.evaluation_audit import ( + MAX_CHUNK_RAW_BYTES, + MAX_CHUNK_RECORDS, + MAX_RECORD_BYTES, + TRACE_ENCODING, + TRACE_FILENAME, + XZ_PRESET, + TrialAudit, + plain, +) + + +def copied(value): + """Synchronously detach/copy tensors before callback return (no live aliases).""" + if value is None or type(value) in (str, bool, int, float): + return value + if isinstance(value, torch.Tensor): + return value.detach().cpu().clone().tolist() + if isinstance(value, dict): + return {k: copied(v) for k, v in value.items()} + if isinstance(value, (tuple, list)): + return [copied(v) for v in value] + return plain(value) + + +def _json_safe(value): + # Preserve offending nonfinite values explicitly; never emit invalid JSON or turn them into zeros. + if isinstance(value, float) and not math.isfinite(value): + return {'nonfinite': repr(value)} + if isinstance(value, dict): + return {k: _json_safe(v) for k, v in value.items()} + if isinstance(value, list): + return [_json_safe(v) for v in value] + return value + + +def _row(value): + return copied(value[0]) + + +def snapshot(s, supplemental): + """Copy existing state only. supplemental is a required read-only owner callback. + + It supplies {actuator_state: dict, rng: dict, applied: AppliedEvidence | None}; + applied MUST come from pre-physics unique-writer observation, never post-step + reconstruction (see evaluation_audit.AppliedEvidence). Actuator internals and non-Torch + RNGs are backend-dependent and must be separately bound by the runner. Empty + actuator dict explicitly means no hidden actuator memory (requires binding). + """ + d, env = s.robot.data, s.env + root = s.root() + q, vel, torque = s.q(), d.joint_vel.torch[:, s.joints], d.applied_torque.torch[:, s.joints] + lower = q.new_tensor([j['limits']['lower'] for j in s.bundle['joints']]) + upper = q.new_tensor([j['limits']['upper'] for j in s.bundle['joints']]) + speed = q.new_tensor([j['limits']['velocity'] for j in s.bundle['joints']]) + foot, contact = s.foot_center(), s.contact() + target = s.reference()[2] + events = [] + for j, event in enumerate(s.cc['events']): + events.append({'fired': bool(s.schedule.fired[0, j]), 'completed': bool(s.schedule.completed[0, j]), + 'start': float(s.schedule.start[0, j]), 'envelope': float(s.schedule.envelope[0, j]), + 'integral': float(s.schedule.integral[0, j])}) + h = env.runtime_hooks + root_position, root_velocity = pose_cost(*s.errors(), s.residual_cfg['reward']) + extra = copied(supplemental(s)) + if set(extra) != {'actuator_state', 'rng', 'applied'}: + raise ValueError('Supplemental evidence requires actuator_state, rng and applied only') + # torch RNGs are copied in addition to owner-supplied Python/numpy/backend RNG state. + extra['rng']['torch_cpu'] = copied(torch.get_rng_state()) + if q.is_cuda: + extra['rng']['torch_cuda'] = copied(torch.cuda.get_rng_state(q.device)) + result = { + 'time': float(s.time[0]), 'substeps': int(s.substeps[0]), 'scenario': int(s.scenarios[0]), + 'weight': float(s.weight[0]), 'anchor_height': float(env.cfg.anchor_height), + 'anchors': plain(asdict(s.anchors)), 'env_origin': _row(env.scene.env_origins), + 'body_names': list(s.robot.body_names), 'event_program': plain(s.cc['events']), + 'foot': _row(foot), 'normal': _row(contact), 'friction': _row(s.friction()), + 'gap': float(foot[0, 2] - s.bundle['foot']['radius']), 'tilt': float(s.tilt()[0]), + 'slip_speed': float(s.slip_velocity()[0]), 'root': _row(torch.cat(root, -1)), + 'q': _row(q), 'qdot': _row(vel), 'torque': _row(torque), + 'limit_ratio': float(torch.maximum((torque / s.effort).abs().max(), (vel / speed).abs().max())), + 'joint_excess': float(torch.maximum(lower - q, q - upper).clamp_min(0).max()), + 'external': _row(s.external), 'guide': _row(s.guide), 'auxiliary': _row(s.auxiliary), + 'processed_target': _row(env.action_manager.get_term('joint_position').processed_actions), + 'executed': _row(s.executed), 'residual_request': _row(s.residual_request), + 'effective_residual_rad': _row(s.effective_residual_rad), 'sensed': _row(s.sensed), + 'policy': _row(s.policy()), 'manager_action': _row(env.action_manager.action), + 'manager_previous_action': _row(env.action_manager.prev_action), + 'ik_target': _row(inverse_rotate(root[1], target - root[0])), + 'q_ik': _row(s.controller.q_ik), 'reference_action': _row(s.controller.action), + 'refreshes': int(s.controller.refreshes[0]), + 'sensor_delay': copied(s.sensor_delay.data[:, 0]), 'action_delay': copied(s.action_delay.data[:, 0]), + 'contact': bool(s.latch.contact[0]), 'contact_timer': float(s.latch.timer[0]), + 'separated': bool(s.separation.separated[0]), 'premature': bool(s.separation.premature[0]), + 'armed': bool(s.separation.armed[0]), 'separation_timer': float(s.separation.timer[0]), + 'onset_phase': int(s.separation.onset_phase[0]), 'edge_phase': int(s.separation.edge_phase[0]), + 'acquired': bool(s.acquisition.acquired[0]), 'acquisition_failed': bool(s.acquisition.failed[0]), + 'acquisition_timer': float(s.acquisition.timer[0]), 'load_off': bool(s.load_off[0]), + 'liftoff': bool(s.edge_off[0]), 'touchdown': bool(s.edge_on[0]), 'events': events, + 'counters': {k: getattr(h, k) for k in ('reset_calls', 'pose_write_calls', 'composer_buffer_replacements', + 'initialization_steps', 'rollout_steps', 'attempted_steps', 'completed_steps')}, + 'reward_components': {'task': float(s.objective()[0][0]), + 'effort': float((torque / s.effort).square().mean()), + 'action_rate': float((env.action_manager.action - env.action_manager.prev_action) + .square().mean()), + 'root_position': float(root_position[0]), 'root_velocity': float(root_velocity[0]), + 'residual': float(s.residual_request.square().mean())}, **extra} + return result + + +class EvaluationRecorder: + """One trial, exclusive directory; lossless JSONL in bounded finalized XZ chunks. + + budget_check(stage, pending_bytes, written_bytes) MUST raise on aggregate + disk/time expiry. Stages: create, record, finalize. Every record checks a + conservative reservation for pending raw memory plus estimated encoded bytes; + every actual write separately checks exact bytes including headers/trailers. + The runner MUST account all other artifacts across every trial/launch. + Pending raw data is bounded by 64 records AND 8 MiB, independently of the + unchanged 1 MiB record limit. Compression uses the bounded preset-0 dictionary. + Initial/seam/step_result/terminal/capture_fault and any audit failure flush + synchronously. Forced kill can lose the uncommitted chunk (or tear a write); + only finalize fsyncs and acknowledges. A readable prefix is NOT a complete trace. + Hook calls cannot grant physics. Recording faults are sticky and fatal. + The caller MUST supply an existing, durably established parent directory and + keep that path stable through finalization. Only the exclusive trial directory + is created here; finalization fsyncs it and its parent before acknowledgment. + """ + def __init__(self, protocol, trial_index, directory, *, budget_check, supplemental): + if not callable(budget_check) or not callable(supplemental): + raise ValueError('Explicit resource and supplemental-state hooks required') + self.audit = TrialAudit(protocol, trial_index) + self.directory = Path(directory) + self.budget_check, self.supplemental = budget_check, supplemental + self.written_bytes = 0 + self.pending: list[bytes] = [] + self.pending_raw_bytes = 0 + self.faults = [] + self.finalized = False + self.budget_check('create', 0, 0) + self.directory.mkdir(exist_ok=False) + self.stream = (self.directory / TRACE_FILENAME).open('xb', buffering=0) + + def _flush(self, *, stage='record'): + if not self.pending: + return + data = lzma.compress(b''.join(self.pending), format=lzma.FORMAT_XZ, preset=XZ_PRESET) + self.budget_check(stage, len(data), self.written_bytes) + written = self.stream.write(data) + self.written_bytes += written + if written != len(data): + raise OSError('Short evidence write') + self.pending.clear() + self.pending_raw_bytes = 0 + + def _write(self, record, *, stage='record', flush=False): + raw = (json.dumps(_json_safe(record), allow_nan=False, separators=(',', ':')) + '\n').encode() + if len(raw) > MAX_RECORD_BYTES: + raise ValueError('Decoded evidence record exceeds bounded reader limit') + if (len(self.pending) >= MAX_CHUNK_RECORDS + or self.pending_raw_bytes + len(raw) > MAX_CHUNK_RAW_BYTES): + self._flush(stage=stage) + self.pending.append(raw) + self.pending_raw_bytes += len(raw) + # Reserve raw memory plus a deliberately conservative encoded-size estimate. + # Actual disk admission still uses exact finalized bytes immediately pre-write. + # Retain pending evidence if admission fails, for handled cleanup/finalization. + self.budget_check(stage, 3 * self.pending_raw_bytes + 65536, self.written_bytes) + if (flush or record['kind'] in ('initial', 'seam', 'step_result', 'terminal', 'capture_fault') + or len(self.pending) >= MAX_CHUNK_RECORDS or self.pending_raw_bytes >= MAX_CHUNK_RAW_BYTES): + self._flush(stage=stage) + + def _fault(self, exc): + cause = f'recording_fault:{type(exc).__name__}:{exc}' + self.faults.append(cause) + self.audit.fail((cause,), kind='recording') + return (cause,) + + def _record(self, record): + if self.finalized or self.faults: + return self._fault(RuntimeError('Recorder finalized or faulted')) + # Evaluate AND write: malformed/nonfinite first-offending evidence is not dropped. + causes = self.audit.consume(record) + try: + self._write(record, flush=bool(causes)) + except Exception as exc: + causes = (*causes, *self._fault(exc)) + return tuple(dict.fromkeys(causes)) + + def before_control(self, origin_tick, observation, action, *, mean=None, std=None): + """Runner calls before env.step; nonempty return means DO NOT submit an action. + + Learned metadata: raw 46D observation, pre-tanh mean, std and tanh(mean) + action, all [1,N]. Reference: mean/std=None and zero action. No inference here. + """ + try: + return self._record({'kind': 'control', 'origin_tick': origin_tick, 'observation': _row(observation), + 'action': _row(action), 'mean': None if mean is None else _row(mean), + 'std': None if std is None else _row(std.expand(1, 3))}) + except Exception as exc: + return self._fault(exc) + + def __call__(self, event, state): + try: + record = {'kind': event.kind, 'event': plain(asdict(event)), + 'state': snapshot(state, self.supplemental)} + return self._record(record) + except Exception as exc: + cause = f'recording_fault:{type(exc).__name__}:{exc}' + self.faults.append(cause) + # Locate a failed accessor at the actual event, without inventing its state/tick sample. + record = {'kind': 'capture_fault', 'event': plain(asdict(event)), 'causes': [*event.causes, cause]} + causes = self.audit.consume(record) + try: + self._write(record) + except Exception as write_exc: + causes += self._fault(write_exc) + return causes + + def seam_callback(self, event, state): + # Lazy import: the metric/audit and offline reader have no Isaac dependency. + from .evaluation_env import SeamDecision + + try: + causes = self._record({'kind': 'seam', 'event': plain(asdict(event)), + 'state': snapshot(state, self.supplemental)}) + except Exception as exc: + causes = self._fault(exc) + return SeamDecision(not bool(causes), causes) + + def after_step(self, result): + """Persist actual StepResult, including partial controls; never invent a reward.""" + try: + record = {'kind': 'step_result', 'ticks_advanced': result.ticks_advanced, + 'observed_tick': self.audit.tick, 'reward': copied(result.reward), + 'has_next_observation': result.observation is not None, + 'stop': None if result.stop is None else plain(asdict(result.stop))} + if self.finalized: + raise RuntimeError('Recorder finalized') + causes = self.audit.consume(record) + self._write(record) + runtime_causes = () if result.stop is None else result.stop.causes + return tuple(c for c in causes if c not in runtime_causes) + except Exception as exc: + return self._fault(exc) + + def finalize(self, *, stop=None, external_failure=None, acknowledge=None): + """Durably persist trace, terminal, metrics; only then acknowledge EXACT stop. + + external_failure is a runner-owned {causes:[nonempty strings], fatal:True, + context:dict} for inference/identity/I/O/budget exceptions outside callbacks. + It is NOT a synthetic StopResult/tick. Such rows are NEVER acknowledged: + abort launch, including when an earlier runtime stop was nonfatal. + On ANY write/fsync/budget error raises, leaves partial files, never acknowledges. + """ + if self.finalized: + raise RuntimeError('Already finalized') + if stop is None and external_failure is None: + raise ValueError('Runtime stop or explicit runner terminal required') + terminal = {'kind': 'terminal', 'runtime_stop': None if stop is None else plain(asdict(stop)), + 'external_failure': plain(external_failure), 'recording_faults': list(self.faults)} + self.audit.consume(terminal) + report = self.audit.report() + report['terminal'] = terminal + report['trace_encoding'] = TRACE_ENCODING + try: + self._write(terminal, stage='finalize') + self.stream.flush() + os.fsync(self.stream.fileno()) + encoded = (json.dumps(_json_safe(report), indent=2, allow_nan=False) + '\n').encode() + self.budget_check('finalize', len(encoded), self.written_bytes) + with (self.directory / 'metrics.json').open('xb') as output: + if output.write(encoded) != len(encoded): + raise OSError('Short metrics write') + output.flush() + os.fsync(output.fileno()) + self.written_bytes += len(encoded) + # Directory entries as well as data must be durable before lifecycle admission. + for directory in (self.directory, self.directory.parent): + fd = os.open(directory, os.O_RDONLY | os.O_DIRECTORY) + try: + os.fsync(fd) + finally: + os.close(fd) + self.stream.close() + except Exception as exc: + self._fault(exc) + raise + self.finalized = True + # Audit recording faults also block nonfatal runtime lifecycle continuation. + recording_failure = any(c.startswith(('recording', 'identity', 'nonfinite', 'storage', 'budget')) + for c in report['all_causes']) + if acknowledge is not None and stop is not None and external_failure is None and not self.faults \ + and not recording_failure: + acknowledge(stop) + return report diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_rng_scope.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_rng_scope.py new file mode 100644 index 0000000..0d390a9 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_rng_scope.py @@ -0,0 +1,93 @@ +"""Source-qualified observable RNG inventory, NOT complete PhysX state capture. + +No sampling, restoration, solver getter or inference of hidden native state. +The runner binds this provider and its scope under the instrumentation source role. +""" +from copy import deepcopy + +PREFIX = 'source/SkillFactory/SkillFactory/' +SCOPE_SOURCE = PREFIX + 'tasks/manager_based/contact_cycle_sim/evaluation_rng_scope.py' +SOURCES = [SCOPE_SOURCE, + PREFIX + 'tasks/manager_based/contact_cycle_sim/evaluation_instrumentation.py', + PREFIX + 'tasks/manager_based/contact_cycle_sim/evaluation_recording.py', + PREFIX + 'tasks/manager_based/contact_cycle/mdp.py', + PREFIX + 'tasks/manager_based/contact_cycle_sim/cfg.py', + PREFIX + 'tasks/manager_based/contact_cycle_sim/evaluation_env.py', + '/home/cen/IsaacLab/source/isaaclab/isaaclab/actuators/actuator_pd.py'] +EVIDENCE_SCOPE = { + 'format': 'skillfactory.observable-state-scope.v1', + 'equality': 'recorded_application_controller_and_enumerated_observable_rng_only', + 'sources': SOURCES, + 'claims': {'complete_backend_state_equality': False, 'deterministic_replay': False}, +} +UNOBSERVED_INTERNALS = dict.fromkeys(( + 'physx_solver_contact_caches', 'internal_stochastic_state_if_any', + 'numerical_scheduling_effects'), 'unobserved') +# Source inventory, not a search of arbitrary native memory. Unexpected task or +# actuator layouts fail qualification; instrumentation separately checks fields. +GENERATOR_INVENTORY = { + 'python_global': 'observed by instrumentation.random.getstate', + 'numpy_global': 'observed by instrumentation.numpy.random.get_state', + 'torch_cpu_global': 'observed by recorder.torch.get_rng_state', + 'torch_current_cuda_device': 'observed by recorder when state.device is CUDA', + 'additional_generators': [], + 'task_sampling': 'CycleState.after_physics calls torch.randn_like even with zero noise scale', + 'warp_sampling': 'no configured task sampling or project-owned Warp random-state buffers', + 'replicator_sampling': 'no configured task Replicator randomization', + 'actuator': 'exact IdealPDActuator; no additional generator in active task path', +} + + +def qualify_config(cfg): + """Require the source-reviewed nominal task, not a generic no-RNG assertion.""" + from isaaclab.actuators.actuator_pd import IdealPDActuator + from isaaclab.actuators.actuator_pd_cfg import IdealPDActuatorCfg + from isaaclab.utils.string import ResolvableString + + def qualified(actuator): + if type(actuator) is not IdealPDActuatorCfg: + return False + target = actuator.class_type + return target is IdealPDActuator or ( + type(target) is ResolvableString + and str(target) == 'isaaclab.actuators.actuator_pd:IdealPDActuator' + and target._resolved_callable is IdealPDActuator and target._resolve_error is None) + + groups = cfg.scene.robot.actuators + if not groups or not all(qualified(a) for a in groups.values()): + raise ValueError('RNG inventory requires exact IdealPD task configuration') + if (cfg.scene.num_envs != 1 or cfg.load_randomization is not None + or cfg.observations.policy.enable_corruption + or any(cfg.cycle_parameters['sensor'].values())): + raise ValueError('RNG inventory does not cover changed task randomization') + return deepcopy(GENERATOR_INVENTORY) + + +def backend_rng(state): + """Observe actual Torch state; label only source-qualified unused samplers N/A. + + Python/numpy globals and named generators are copied by instrumentation. + Torch is also copied by the recorder; this read does not advance either RNG. + Native PhysX internal stochastic state, if any, is explicitly NOT covered. + """ + import torch + + device = torch.device('cpu' if state is None else state.env.device) + actual = {'cpu': torch.get_rng_state().tolist()} + if device.type == 'cuda': + actual['cuda'] = torch.cuda.get_rng_state(device).tolist() + coverage = { + 'torch_task_global': { + 'status': 'observed', 'sources': SOURCES, + 'evidence': 'Actual global RNG; randn_like executes even when noise scale is zero.', + 'state': actual, + }, + } + for name in ('warp_sampling', 'replicator_sampling'): + coverage[name] = { + 'status': 'not_applicable', 'sources': SOURCES, + 'evidence': GENERATOR_INVENTORY[name] + '; not a claim about native internals.', + 'state': None, + } + return {'coverage': coverage, 'evidence_scope': deepcopy(EVIDENCE_SCOPE), + 'unobserved_internals': deepcopy(UNOBSERVED_INTERNALS)} diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/mdp.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/mdp.py new file mode 100644 index 0000000..dacd7f4 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/mdp.py @@ -0,0 +1,99 @@ +"""Simulation truth is deliberately available to reference AND residual actor.""" +import torch + +from SkillFactory.legs.contact_cycle_sim.config import combine, pose_cost +from SkillFactory.legs.contact_cycle_sim.reference import ReferenceController +from SkillFactory.tasks.manager_based.contact_cycle import mdp as base + + +class SimCycleState(base.CycleState): + def __init__(self, env): + super().__init__(env) + self.residual_cfg = env.cfg.residual_parameters + self.residual_request = torch.zeros_like(self.executed) + self.reference_used = torch.zeros_like(self.executed) + self.effective_residual_rad = torch.zeros_like(self.executed) + self.controller = ReferenceController(self, self.residual_cfg['reference']) + self.controller.reset(torch.arange(env.num_envs, device=env.device)) + + def reset(self, ids): + super().reset(ids) + self.scenarios[ids] = 0 + self.stand_only[ids] = False + if hasattr(self, 'controller'): + self.residual_request[ids] = 0 + self.reference_used[ids] = 0 + self.effective_residual_rad[ids] = 0 + self.controller.reset(ids) + + def set_sampler(self, sampler): + if sampler is not None: + raise ValueError('Simulation residual v1 is nominal-cycle only; old training sampler is incompatible') + super().set_sampler(None) + self.scenarios.zero_() + + def after_physics(self, dt): + super().after_physics(dt) + ids = (self.substeps % self.env.cfg.decimation == 0).nonzero().flatten() + self.controller.refresh(ids) + + def errors(self): + target, target_velocity = self.port_reference() + pos, _, velocity, _ = self.root() + return pos - target, velocity - target_velocity + + def policy(self): + error, velocity_error = self.errors() + gap = self.foot_center()[:, 2] - self.bundle['foot']['radius'] + # 32 + root error3 + linear velocity3 + Fn1 + gap1 + NEXT reference3 + PREVIOUS residual3. + # Read-only: repeated observations do not solve/advance the reference controller. + return torch.cat((super().policy(), error, velocity_error, self.contact()[:, 2:3], gap[:, None], + self.controller.action, self.residual_request), -1) + + +def state(env): + if not hasattr(env, '_cycle_state'): + env._cycle_state = SimCycleState(env) + if not isinstance(env._cycle_state, SimCycleState): + raise RuntimeError('Wrong state contract; do not silently reuse historical policy32 state') + return env._cycle_state + + +def observation(env, privileged=False): + s = state(env) + return s.privileged() if privileged else s.policy() + + +class SimResidualAction(base.CycleAction): + def __init__(self, cfg, env): + super().__init__(cfg, env) + state(env) # install the distinct state BEFORE old wrappers can create policy32 state + + def process_actions(self, actions): + s = state(self._env) + try: + full, request, delta = combine(s.controller.action, actions, s.residual_cfg['residual_limit_rad'], + self._env.cfg.support_parameters['action_scale_rad']) + except ValueError: + # Direct ActionManager callers bypass LoadedLegEnv.step's outer validation. + # Manager stores input first; restore history before rejecting composition. + self._env.action_manager.action[:] = self.raw_actions + raise + super().process_actions(full) + s.reference_used.copy_(s.controller.action) + s.residual_request.copy_(request) + s.effective_residual_rad.copy_(delta) + + +def root_position_cost(env): + s = state(env) + return pose_cost(*s.errors(), s.residual_cfg['reward'])[0] + + +def root_velocity_cost(env): + s = state(env) + return pose_cost(*s.errors(), s.residual_cfg['reward'])[1] + + +def residual_cost(env): + return state(env).residual_request.square().mean(-1) diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/recording.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/recording.py new file mode 100644 index 0000000..45a9bb4 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/recording.py @@ -0,0 +1,26 @@ +"""Residual requests are NOT absolute joint actions. Log both with explicit units.""" +import torch + +from SkillFactory.tasks.manager_based.contact_cycle.recording import CycleRecorder + +from .mdp import residual_cost, root_position_cost, root_velocity_cost + + +class SimCycleRecorder(CycleRecorder): + def __init__(self, env): + super().__init__(env) + self.actor_input = torch.zeros(env.num_envs, 46) + + def snapshot(self): + values = super().snapshot() + s = self.state + extra = {'residual_request_normalized': s.residual_request, + 'reference_used_normalized': s.reference_used, + 'reference_next_normalized': s.controller.action, + 'effective_residual_rad': s.effective_residual_rad, + 'reference_ik_rad': s.controller.q_ik, + 'root_position_cost_unweighted': root_position_cost(self.env), + 'root_velocity_cost_unweighted': root_velocity_cost(self.env), + 'residual_cost_unweighted': residual_cost(self.env)} + values.update({k: v.detach().cpu().clone() for k, v in extra.items()}) + return values diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/rootxy_cfg_v2.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/rootxy_cfg_v2.py new file mode 100644 index 0000000..cfdd0f2 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/rootxy_cfg_v2.py @@ -0,0 +1,69 @@ +"""Direct-construction RootXY-v2 config. No registry/v1 source mutation.""" +from pathlib import Path + +from isaaclab.utils.configclass import configclass + +from SkillFactory.legs.contact_cycle_sim.rootxy_training_v2 import ( + BASE_PATH, TASK_ID, TASK_PATH, exact, load_task_config, +) +from .cfg import SimContactCycleEnvCfg +from . import mdp + +ROOT = Path(__file__).resolve().parents[6] + + +@configclass +class RootXYContactCycleEnvCfg(SimContactCycleEnvCfg): + rootxy_config: str = str(ROOT / TASK_PATH) + task_identity: str = TASK_ID + residual_config: str = str(ROOT / BASE_PATH) + + def __post_init__(self): + resolved = load_task_config(self.rootxy_config, ROOT) + if Path(self.residual_config).resolve() != ROOT / BASE_PATH or self.task_identity != TASK_ID: + raise ValueError("RootXY-v2 must construct from the pinned v1 base") + super().__post_init__() # unchanged controller/fixture/46D/actions/termination + self.residual_parameters = resolved # honest v2 identity, single whitelisted reward override + + +def verify_actual_config(env): + """Task seam checks, supplementary to the future supervisor full cfg binding. + + Call before first reset/action and after close. Never substitutes for installed + source/namespace/constructor normalization identity checks in the adapter. + """ + cfg = env.cfg + resolved = load_task_config(ROOT / TASK_PATH, ROOT) + if (type(cfg) is not RootXYContactCycleEnvCfg or cfg.task_identity != TASK_ID + or not exact(cfg.residual_parameters, resolved) + or cfg.actions.joint_position.class_type is not mdp.SimResidualAction + or cfg.rewards.root_position.func is not mdp.root_position_cost + or cfg.rewards.root_position.weight != -1. + or cfg.rewards.root_velocity.weight != -.1 or cfg.rewards.residual.weight != -.05 + or cfg.rewards.task.weight != 1. or cfg.rewards.effort.weight != -.01 + or cfg.rewards.action_rate.weight != -.005 + or cfg.terminations.failed is None or cfg.episode_length_s != 9 + or env.num_envs != 128 or env.physics_dt != .002 or env.step_dt != .01 + or cfg.sim.physics.solver_type != 0 or cfg.sim.physics.enable_external_forces_every_iteration + or mdp.state(env).robot.is_fixed_base): + raise ValueError("Actual-used RootXY-v2 task seam mismatch") + if not exact(mdp.state(env).residual_cfg, resolved): + raise ValueError("Actual-used residual state identity mismatch") + return {"task_id": TASK_ID, "resolved_task": resolved, "full_adapter_binding_required": True} + + +def construct_environment(device="cuda:0"): + """For the future admitted worker only; never imported by the CPU CLI.""" + from .env import SimContactCycleEnv + + cfg = RootXYContactCycleEnvCfg() + cfg.scene.num_envs = 128 + cfg.sim.device = device + cfg.seed = 92511 + env = SimContactCycleEnv(cfg=cfg) + try: + verify_actual_config(env) + except BaseException: + env.close() + raise + return env diff --git a/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/rootxy_recording_v2.py b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/rootxy_recording_v2.py new file mode 100644 index 0000000..86d8637 --- /dev/null +++ b/source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/rootxy_recording_v2.py @@ -0,0 +1,127 @@ +"""Compact functional columns plus streaming XY/authority metrics, never full snapshots. + +4500x128 bounded columns are retained in RAM for the unchanged functional auditor. +Only four selected trajectories are publishable; learner rollouts are not traced. +""" +import torch + +from SkillFactory.legs.contact_cycle_sim.rootxy_diagnostics_v2 import RootXYMetrics +from .mdp import state, base + + +class RootXYRecorder: + def __init__(self, env, functional, learned): + self.env, self.s, self.functional, self.learned = env, state(env), functional, learned + self.metrics = RootXYMetrics(env.num_envs, env.device) + self.columns = {} + self.ticks = 0 + self.stops = [None] * env.num_envs + self.early = [False] * env.num_envs + self.action_available = False + self.height_square = torch.zeros(env.num_envs, device=env.device, dtype=torch.float64) + self.swing_square = self.height_square.clone() + self.swing_count = torch.zeros(env.num_envs, device=env.device, dtype=torch.long) + self.failure = torch.zeros(env.num_envs, device=env.device, dtype=torch.bool) + s = self.s + self.lower = torch.tensor([j['limits']['lower'] for j in s.bundle['joints']], device=env.device) + self.upper = torch.tensor([j['limits']['upper'] for j in s.bundle['joints']], device=env.device) + self.speed = torch.tensor([j['limits']['velocity'] for j in s.bundle['joints']], device=env.device) + self.initial_pair = None + + def before_control(self, observation, action, mean, std): + if (observation.shape != (self.env.num_envs, 46) or action.shape != (self.env.num_envs, 3) + or not torch.isfinite(observation).all() or not torch.isfinite(action).all() + or (action.abs() > 1).any()): + raise ValueError('Invalid validation actor input/action') + if self.learned: + if (mean is None or std is None or not torch.isfinite(mean).all() + or not torch.isfinite(std).all() or not torch.equal(action, mean.tanh())): + raise ValueError('Missing/nonfinite deterministic learned metadata') + elif mean is not None or std is not None or action.any(): + raise ValueError('Analytic baseline must have zero residual and no actor metadata') + self.action_available = True + + def __enter__(self): + s = self.s + self.initial_pair = {k: v.detach().cpu().clone() for k, v in { + 'policy': s.policy(), 'root': torch.cat(s.root(), -1), 'joint_pos': s.q(), + 'target_root': s.port_reference()[0], 'target_foot': s.reference()[2], + 'reference': s.controller.action, 'normal': s.contact(), 'foot': s.foot_center(), + }.items()} + self.previous = self.env.scene.update + + def update(dt): + self.previous(dt) + if dt > 0: + if dt != .002: + raise ValueError('Unexpected diagnostic physics dt') + self.record() + + self.env.scene.update = update + return self + + def __exit__(self, *_): + self.env.scene.update = self.previous + + def record(self): + if self.ticks >= 4500 or not self.action_available: + raise ValueError('Extra tick or missing control metadata') + s, g = self.s, self.functional['gates'] + phase, _, target, _, _ = s.reference() + error, _ = s.errors() + ratio = torch.maximum((s.robot.data.applied_torque.torch[:, s.joints] / s.effort).abs().amax(-1), + (s.robot.data.joint_vel.torch[:, s.joints] / self.speed).abs().amax(-1)) + excess = torch.maximum(self.lower - s.q(), s.q() - self.upper).clamp(min=0).amax(-1) + actual = (self.env.action_manager.get_term('joint_position').processed_actions + - s.nominal - .5 * s.reference_used) + if (not torch.allclose(actual, s.effective_residual_rad, atol=1e-6, rtol=0) + or (actual.abs() > .05 + 1e-6).any()): + raise ValueError('Effective residual authority invariant failed') + safe = ((ratio <= g['normalized_effort_velocity_max']) & (excess <= g['joint_limit_excess_rad_max']) + & (s.guide[..., 2].abs().reshape(self.env.num_envs, -1).amax(-1) == 0)) + self.metrics.tick(time=s.time, position_error=error, phase=phase, reference=s.reference_used, + request=s.residual_request, effective_residual=actual, safety_ok=safe) + live = self.metrics.live + self.height_square += error[:, 2].double().square() * live + swing = live & (phase == 3) + self.swing_square += (s.foot_center() - target).double().square().sum(-1) * swing + self.swing_count += swing.long() + self.failure |= live & base.failed(self.env) + values = {'time': s.time, 'phase': phase, 'scenario': s.scenarios, 'weight': s.weight, + 'normal': s.contact(), 'foot': s.foot_center(), 'target': target, + 'gap': s.foot_center()[:, 2] - s.bundle['foot']['radius'], + 'signed_height_error': error[:, 2], 'tilt': s.tilt(), 'external': s.external, + 'guide': s.guide, 'auxiliary': s.auxiliary, 'joint_excess': excess, 'limit_ratio': ratio, + 'contact_acquired': s.acquisition.acquired, + 'actor_metadata_available': torch.full_like(live, self.learned), + 'control_action_available': torch.full_like(live, self.action_available), + 'root_position_error': error, 'reference_used_normalized': s.reference_used, + 'effective_residual_rad': actual} + for name, value in values.items(): + if not torch.isfinite(value).all(): + raise ValueError('Nonfinite validation column: ' + name) + if name not in self.columns: + self.columns[name] = torch.empty((4500, *value.shape), dtype=value.dtype, device='cpu') + self.columns[name][self.ticks].copy_(value.detach().cpu()) + self.ticks += 1 + + def end_control(self, terminated, truncated, final_step): + for e in (terminated | truncated).nonzero().flatten().cpu().tolist(): + if self.stops[e] is None: + self.stops[e] = self.ticks + self.early[e] = bool(terminated[e]) or not final_step + self.metrics.end_control(terminated, truncated) + + def rows(self, evaluate): + result = self.metrics.rows() + for e, row in enumerate(result): + stop = self.stops[e] + if stop is None or stop != row['physics_samples']: + raise ValueError('Missing/partial first-episode terminal capture') + episode = {k: v[:stop, e:e+1] for k, v in self.columns.items()} + audit = evaluate(episode, self.functional, 'learned_policy' if self.learned else 'analytic_reference') + row.update(success=bool(audit['sim_functional_passed'] and not self.early[e] and not self.failure[e]), + functional_audit=audit, height_rms_m=float((self.height_square[e] / stop).sqrt()), + swing_rms_m=float((self.swing_square[e] / self.swing_count[e]).sqrt()) + if self.swing_count[e] else None, landing_error_m=audit['metrics'].get('landing_error_m')) + return result diff --git a/tests/test_contact_cycle.py b/tests/test_contact_cycle.py new file mode 100644 index 0000000..744d3f6 --- /dev/null +++ b/tests/test_contact_cycle.py @@ -0,0 +1,216 @@ +"""CPU contracts, not physical evidence. Kit runs are separately supervised and archived.""" + +import copy +import json +from pathlib import Path + +import pytest +import torch +from SkillFactory.legs.contact_cycle.config import load_config, require_scenario_coverage, validate_config +from SkillFactory.legs.contact_cycle.mechanics import ( + ContactLatch, + WrenchSchedule, + cone_violation, + reduce_to_com, + rotate, + uniform_directions, +) +from SkillFactory.legs.contact_cycle.metrics import wilson +from SkillFactory.legs.contact_cycle.task import DelayLine, command, phase_objective + +ROOT = Path(__file__).resolve().parents[1] +CONFIG = ROOT / "configs/contact_cycle/go2_fl_v1.json" + + +def test_config_and_unchanged_standing_vertical_support(): + c = load_config(CONFIG) + assert c["contract"] == "go2-fl-contact-cycle-v1" + assert json.loads((ROOT/c["support_config"]).read_text())["guide_stiffness"][2] == 0 + plan = json.loads((ROOT/"configs/contact_cycle/acceptance_v1.json").read_text()) + assert plan["proposed_training"]["authorization"] is None + + +@pytest.mark.parametrize("key,value", [ + ("clearance_m", float("nan")), ("clearance_m", True), ("unknown", 1), + ("phase_durations_s", [1]*5), ("swing_displacement_m", [0, 0, 0.01]), + ("schema_version", 2), ("base_motion", {"amplitude_m": [0, 0, 1], "frequency_Hz": 0.2}), +]) +def test_invalid_config(key, value): + c = load_config(CONFIG) + c[key] = value + with pytest.raises(ValueError): + validate_config(c) + + +@pytest.mark.parametrize("key,value", [ + ("ramp_s", 0), ("duration_s", 0.5), ("frame", "camera"), ("body", "trunk"), + ("force_N", [8, 8, 8]), ("couple_Nm", [1, 1, 1]), ("point_m", [0.2, 0, 0]), + ("trigger", "invalid"), ("phase", True), ("scenario", 0), ("start_s", 60.0), +]) +def test_invalid_events(key, value): + c = load_config(CONFIG) + c["events"][0][key] = value + with pytest.raises(ValueError): + validate_config(c) + + +def test_duplicate_json_rejected(tmp_path): + p = tmp_path/"bad.json" + p.write_text('{"schema_version": 1, "schema_version": 1}') + with pytest.raises(ValueError, match="Duplicate"): + load_config(p) + + +def test_wrench_rotates_force_and_couple_shifts_once(): + q = torch.tensor([[0, 0, 2**-0.5, 2**-0.5]]) + f, tau = torch.tensor([[2., 0, 0]]), torch.tensor([[0., 1, 0]]) + point, com = torch.tensor([[1., 0, 0]]), torch.zeros(1, 3) + w = reduce_to_com(f, tau, q, point, com) + torch.testing.assert_close(w, torch.tensor([[0., 2, 0, -1, 0, 2]]), atol=1e-6, rtol=1e-6) + # Rotation + translation preserves mechanical power when twisting about corresponding origins. + omega, v_com = torch.tensor([[0.1, -0.2, 0.3]]), torch.tensor([[0.2, 0.3, 0.1]]) + v_point = v_com + torch.cross(omega, point-com, dim=-1) + p1 = (rotate(q, f)*v_point).sum() + (rotate(q, tau)*omega).sum() + p2 = (w[:, :3]*v_com).sum() + (w[:, 3:]*omega).sum() + torch.testing.assert_close(p1, p2) + + +def test_superposition_and_zero_force_pure_couple(): + q = torch.tensor([[0., 0, 0, 1]]) + com, point = torch.zeros(1, 3), torch.ones(1, 3) + f, t = torch.tensor([[1., 2, 3]]), torch.tensor([[3., 1, 2]]) + a = reduce_to_com(f, t, q, point, com) + b = reduce_to_com(-f, -t, q, point, com) + assert (a+b == 0).all() + torch.testing.assert_close(reduce_to_com(com, t, q, point, com)[:, 3:], t) + + +def test_continuous_directions_not_axis_grid(): + d = uniform_directions(10000, torch.Generator().manual_seed(13)) + torch.testing.assert_close(d.norm(dim=-1), torch.ones(10000)) + assert (d.mean(0).abs() < 0.02).all() + assert ((d > 0).any(0) & (d < 0).any(0)).all() + assert (d.abs() > 0.001).all(-1).float().mean() > 0.99 + + +def test_unilateral_and_friction_are_not_external_force_constraints(): + f = torch.tensor([[3., 4, 10], [0., 0, -3], [9., 0, 10]]) + n = torch.tensor([[0., 0, 1.]]).expand_as(f) + u, friction = cone_violation(f, n, 0.6) + torch.testing.assert_close(u, torch.tensor([0., 3, 0])) + torch.testing.assert_close(friction, torch.tensor([0., 0, 3])) + + +def test_schedule_lifecycle_reset_no_refire_and_rate(): + e = copy.deepcopy(load_config(CONFIG)["events"][0]) + e.update(start_s=0.1, duration_s=0.4, ramp_s=0.1) + bank = WrenchSchedule([e], 2, "cpu") + phase, edge, scenario = torch.zeros(2).long(), torch.zeros(2).bool(), torch.ones(2).long() + out = [] + for k in range(71): + out.append(bank.step(torch.full((2,), k*0.01), phase, edge, edge, scenario)) + out = torch.stack(out)[:, 0, 0] + assert (out[:11] == 0).all() and (out[50:] == 0).all() + torch.testing.assert_close(out[20], torch.tensor(e["force_N"]+e["couple_Nm"])) + assert ((out[1:, :3]-out[:-1, :3]).norm(dim=-1)/0.01 <= 54).all() + bank.reset(torch.tensor([0])) + assert not bank.fired[0].any() and bank.fired[1].all() + torch.testing.assert_close(bank.start[1], torch.tensor([0.1])) + + +def test_contact_event_phase_filter_delay_and_one_shot(): + e = copy.deepcopy(load_config(CONFIG)["events"][-1]) + e.update(start_s=0.1) + b = WrenchSchedule([e], 1, "cpu") + yes, no = torch.tensor([True]), torch.tensor([False]) + sc = torch.tensor([3]) + assert not b.step(torch.tensor([0.]), torch.tensor([0]), no, yes, sc).any() + assert not b.fired.any() # initial settling contact cannot consume landing event + assert not b.step(torch.tensor([6.]), torch.tensor([4]), no, yes, sc).any() + assert b.fired.all() + assert b.step(torch.tensor([6.2]), torch.tensor([4]), no, no, sc).abs().sum() > 0 + assert not b.step(torch.tensor([7.]), torch.tensor([4]), no, yes, sc).any() + + +def test_contact_hysteresis_and_partial_reset(): + c = ContactLatch(2, "cpu", dwell=0.01) + for _ in range(4): + off, on = c.update(torch.tensor([3., 3]), 0.002) + assert not on.any() + _, on = c.update(torch.tensor([3., 3]), 0.002) + assert on.all() + for _ in range(20): + off, on = c.update(torch.tensor([1., 1]), 0.002) + assert not off.any() and not on.any() + c.reset(torch.tensor([0])) + assert c.contact.tolist() == [False, True] + for _ in range(5): + off, _ = c.update(torch.tensor([0., 0]), 0.002) + assert off.tolist() == [False, True] + + +def test_task_continuity_clearance_load_and_phase_deadlines(): + cfg = load_config(CONFIG) + origin, weight = torch.zeros(1, 3), torch.tensor([50.]) + time = 0 + for duration in cfg["phase_durations_s"][:-1]: + time += duration + a = command(torch.tensor([time-1e-5]), cfg, origin, weight) + b = command(torch.tensor([time+1e-5]), cfg, origin, weight) + assert (a[2]-b[2]).abs().max() < 1e-5 + assert (a[3]-b[3]).abs().max() < 0.001 + assert (a[4]-b[4]).abs().max() < 0.001 + p, _, target, force, fraction = command(torch.tensor([5.2]), cfg, origin, weight) + assert p == 3 and target[0, 2] >= 0.05 and force == 0 and fraction == 1 + p, _, _, force, fraction = command(torch.tensor([10.]), cfg, origin, weight) + assert p == 5 and force == 50 and fraction == 0 + + +def test_reward_cannot_hide_in_air_or_stay_on_ground(): + phase, progress = torch.tensor([3, 3, 5, 5]), torch.ones(4) + reward, fail = phase_objective(phase, progress, torch.zeros(4), torch.tensor([0., 0.05, 0.05, 0.]), + torch.tensor([50., 0, 0, 50]), torch.tensor([0., 0, 50, 50]), + torch.zeros(4), torch.zeros(4), torch.full((4,), 50.)) + assert reward[1] > reward[0] and reward[3] > reward[2] + assert fail.tolist() == [True, False, True, False] + + +def test_delay_line_latency_and_reset_isolation(): + d = DelayLine(2, 1, 2, "cpu") + assert not d.push(torch.ones(2, 1)).any() + assert not d.push(2*torch.ones(2, 1)).any() + assert (d.push(3*torch.ones(2, 1)) == 1).all() + old = d.data[:, 1].clone() + d.reset(torch.tensor([0]), torch.tensor([[7.]])) + assert (d.data[:, 0] == 7).all() and torch.equal(old, d.data[:, 1]) + + +def test_more_configured_scenarios_cannot_be_silently_omitted(): + c = load_config(CONFIG) + c["scenarios"].append("fifth") + validate_config(c) + with pytest.raises(ValueError, match="every configured"): + require_scenario_coverage(c, 4) + require_scenario_coverage(c, 5) + + +def test_event_fired_is_not_delivered_or_completed(): + e = copy.deepcopy(load_config(CONFIG)["events"][0]) + e.update(trigger="phase", phase=0, start_s=0.2, duration_s=0.4, ramp_s=0.1) + b = WrenchSchedule([e], 1, "cpu") + phase, sc, edge = torch.tensor([0]), torch.tensor([1]), torch.tensor([False]) + b.step(torch.tensor([0.]), phase, edge, edge, sc, 0.01) + assert b.fired.all() and not b.integral.any() and not b.completed.any() + for k in range(1, 70): + b.step(torch.tensor([k*0.01]), phase, edge, edge, sc, 0.01) + assert b.completed.all() + torch.testing.assert_close(b.integral, torch.tensor([[0.3]])) + b.reset(torch.tensor([0])) + assert not b.integral.any() and not b.completed.any() + + +def test_wilson_and_no_claim_from_one_trial(): + assert wilson(1, 1)[0] < 0.21 + assert wilson(96, 100)[0] > 0.90 + with pytest.raises(ValueError): + wilson(0, 0) diff --git a/tests/test_contact_cycle_diagnostic.py b/tests/test_contact_cycle_diagnostic.py new file mode 100644 index 0000000..69f3f9f --- /dev/null +++ b/tests/test_contact_cycle_diagnostic.py @@ -0,0 +1,60 @@ +"""Diagnostic scope validation; never starts Kit or robot optimization.""" + +import copy +import json +from pathlib import Path + +import pytest +from SkillFactory.legs.contact_cycle.training import load_training_config, make_manifest, require_authorization + +ROOT = Path(__file__).resolve().parents[1] +CONFIG = ROOT/'configs/contact_cycle/training_diagnostic_v1.json' + + +def test_diagnostic_scope_gates_and_algorithm_are_not_relaxed(): + pilot = load_training_config(CONFIG, ROOT) + full = load_training_config(ROOT/'configs/contact_cycle/training_v1.json', ROOT) + proposal = json.loads((ROOT/pilot['proposal']).read_text()) + original = json.loads((ROOT/full['proposal']).read_text()) + assert proposal['gates'] == original['gates'] + assert pilot['algorithm'] == full['algorithm'] + assert pilot['task_config'] == full['task_config'] + assert pilot['seeds'] == [91101] + assert pilot['max_total_transitions'] == 512*128*32 == 2097152 + assert pilot['max_total_wall_s'] == 1800 + assert pilot['max_artifact_bytes'] == 1024**3 + manifest = make_manifest(ROOT, CONFIG) + assert 'diagnostic candidate' in manifest['selection'] + assert manifest['accepted_skill'] is False + with pytest.raises(PermissionError): + require_authorization(None, manifest) + + +@pytest.mark.parametrize('key,value', [ + ('seeds', [91101, 91102, 91103]), ('seeds', [95101]), ('updates_per_seed', 1024), + ('num_envs', 256), ('horizon', 64), ('max_total_wall_s', 1801), + ('max_artifact_bytes', 1073741825), ('max_total_transitions', 2097153), + ('validation_interval', 128), ('validation_seed', 95101), +]) +def test_pilot_rejects_scope_expansion_even_with_matching_proposal(tmp_path, key, value): + cfg = copy.deepcopy(json.loads(CONFIG.read_text())) + proposal = json.loads((ROOT/cfg['proposal']).read_text()) + cfg[key] = value + if key in proposal['proposed_training']: + proposal['proposed_training'][key] = value + proposal_path = tmp_path/'proposal.json' + proposal_path.write_text(json.dumps(proposal)) + cfg['proposal'] = str(proposal_path) + path = tmp_path/'config.json' + path.write_text(json.dumps(cfg)) + with pytest.raises(ValueError): + load_training_config(path, ROOT) + + +def test_full_study_does_not_silently_become_a_pilot(tmp_path): + cfg = json.loads(CONFIG.read_text()) + cfg['format'] = 'skillfactory.contact-cycle-training.v1' + path = tmp_path/'bad_full.json' + path.write_text(json.dumps(cfg)) + with pytest.raises(ValueError): + load_training_config(path, ROOT) diff --git a/tests/test_contact_cycle_functional.py b/tests/test_contact_cycle_functional.py new file mode 100644 index 0000000..ef5afe9 --- /dev/null +++ b/tests/test_contact_cycle_functional.py @@ -0,0 +1,85 @@ +"""Synthetic offline checker tests, not simulation/controller acceptance.""" +import importlib.util +import json +from pathlib import Path + +import pytest +import torch + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location('functional_audit', ROOT / 'scripts/audit_contact_cycle_functional.py') +audit = importlib.util.module_from_spec(spec) +spec.loader.exec_module(audit) +CFG = json.loads((ROOT / 'configs/contact_cycle/simulation_functional_v1.json').read_text()) + + +def example(): + t = torch.arange(1, 4501) * .002 + phase = torch.bucketize(t, torch.tensor([3., 4., 4.6, 6., 6.8]), right=True) + z = torch.zeros(4500) + foot = torch.stack(((t - 4.6).div(1.4).clamp(0, 1) * .025, + (t - 4.6).div(1.4).clamp(0, 1) * .02, + torch.where((phase == 2) | (phase == 3), .04, 0.) + .02), -1) + normal = torch.stack((z, z, torch.where((phase == 0) | (phase == 5), 50., 0.)), -1) + d = {'time': t, 'phase': phase, 'scenario': phase * 0, 'weight': z + 50, + 'normal': normal, 'foot': foot, 'target': foot.clone(), 'gap': foot[:, 2] - .02, + 'signed_height_error': z, 'tilt': z, 'external': torch.zeros(4500, 2, 6), + 'guide': torch.zeros(4500, 2, 6), 'auxiliary': torch.zeros(4500, 2, 6), + 'joint_excess': z, 'limit_ratio': z, 'contact_acquired': z.bool().logical_not(), + 'actor_metadata_available': z.bool(), 'control_action_available': z.bool().logical_not()} + return {k: v[:, None].clone() for k, v in d.items()} + + +def test_complete_nominal_example_is_functional_not_learned_acceptance(): + result = audit.evaluate(example(), CFG, 'analytic_reference') + assert result['sim_functional_passed'] and result['accepted_skill'] is False + + +@pytest.mark.parametrize('fault', ['partial', 'reset', 'airless', 'static_xy', 'no_reload', + 'collapse', 'nonfinite', 'hidden_guide', 'wrong_controller', 'disturbance', + 'no_unload', 'aux_horizontal', 'aux_cancellation', 'aux_free_couple', + 'negative_contact']) +def test_functional_gate_rejects_nonfunctional_or_out_of_scope_trace(fault): + d = example() + kind = 'analytic_reference' + if fault == 'partial': + d = {k: v[:330] for k, v in d.items()} + elif fault == 'reset': + d['time'][3000:] -= 6 + elif fault == 'airless': + d['gap'].zero_() + elif fault == 'static_xy': + d['foot'][:, :, :2].zero_() + elif fault == 'no_reload': + d['normal'][d['phase'] == 5] = 0 + elif fault == 'no_unload': + d['normal'][:, :, 2][d['phase'] == 1] = 50 + elif fault == 'collapse': + d['signed_height_error'][-1] = -.101 + elif fault == 'nonfinite': + d['normal'][0, 0, 2] = float('nan') + elif fault == 'hidden_guide': + d['guide'][:, :, 0, 2] = 50 + elif fault == 'wrong_controller': + kind = 'learned_policy' + elif fault == 'disturbance': + d['external'][:, :, 0, 0] = 5 + elif fault == 'aux_horizontal': + d['auxiliary'][:, :, 0, 0] = 1000 + elif fault == 'aux_cancellation': + mask = d['phase'][:, 0] == 3 + d['auxiliary'][mask, 0, 0, 2] = 1000 + d['auxiliary'][mask, 0, 1, 2] = -1000 + elif fault == 'aux_free_couple': + d['auxiliary'][:, :, 0, 3] = 1000 + elif fault == 'negative_contact': + d['normal'][:, :, 2][d['phase'] == 3] = -5 + assert not audit.evaluate(d, CFG, kind)['sim_functional_passed'] + + +def test_vertical_fixture_allows_bounded_com_reduction_moment(): + d = example() + mask = d['phase'][:, 0] == 3 + d['auxiliary'][mask, 0, 0, 2] = 50 + d['auxiliary'][mask, 0, 0, 4] = -1 # r=(.02,0,0), F=(0,0,50) -> r x F=(0,-1,0) + assert audit.evaluate(d, CFG, 'analytic_reference')['sim_functional_passed'] diff --git a/tests/test_contact_cycle_initialization.py b/tests/test_contact_cycle_initialization.py new file mode 100644 index 0000000..29dffcc --- /dev/null +++ b/tests/test_contact_cycle_initialization.py @@ -0,0 +1,100 @@ +"""Initialization contract and diagnostic regression; no robot optimizer or Kit launch.""" + +from pathlib import Path + +import pytest +import torch +from SkillFactory.legs.contact_cycle.config import load_config, validate_config +from SkillFactory.legs.contact_cycle.mechanics import ContactAcquisition, SeparationLatch +from SkillFactory.legs.contact_cycle.ppo import rollout_diagnostics +from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES, phase_failures + +ROOT = Path(__file__).resolve().parents[1] + + +def acquisition(count=1): + return ContactAcquisition(count, 'cpu', deadline=.05, dwell=.01, min_load_fraction=.5, max_gap=.0005) + + +def test_initial_uncontacted_flight_has_bounded_distinct_failure_not_premature(): + a = acquisition() + sep = SeparationLatch(1, 'cpu', .001, .0005, .01) + zero, weight, phase = torch.zeros(1), torch.tensor([50.]), torch.tensor([0]) + for k in range(1, 26): + t, gap = torch.tensor([k*.002]), torch.tensor([.003]) + a.update(t, gap, zero, weight, .002) + edge = sep.update(gap, torch.tensor([False]), phase, .002) + assert not edge.any() and not sep.premature.any() and not a.acquired.any() + assert bool(a.failed[0]) == (k == 25) + # Late contact cannot rewrite an initialization failure as a success. + for k in range(26, 40): + a.update(torch.tensor([k*.002]), zero, weight, weight, .002) + assert a.failed.all() and not a.acquired.any() + + +def test_on_plane_load_acquires_and_true_unload_departure_still_fails(): + a, sep = acquisition(), SeparationLatch(1, 'cpu', .001, .0005, .01) + weight, zero = torch.tensor([50.]), torch.zeros(1) + for k in range(1, 6): + a.update(torch.tensor([k*.002]), zero, weight, weight, .002) + sep.update(zero, torch.tensor([True]), torch.tensor([0]), .002) + assert a.acquired.all() and not a.failed.any() + for _ in range(5): + edge = sep.update(torch.tensor([.002]), torch.tensor([False]), torch.tensor([1]), .002) + assert edge.all() and sep.premature.all() + a.update(torch.tensor([3.5]), torch.tensor([.002]), zero, weight, .002) + assert not a.failed.any() # acquisition is one-shot; departure has its own cause + + +def test_acquisition_partial_reset_and_contiguous_dwell(): + a = acquisition(2) + w, z = torch.full((2,), 50.), torch.zeros(2) + for k in range(1, 5): + a.update(torch.full((2,), k*.002), z, w, w, .002) + a.update(torch.full((2,), .01), z, torch.tensor([0., 50.]), w, .002) + assert a.acquired.tolist() == [False, True] + a.update(torch.full((2,), .05), z, torch.zeros(2), w, .002) + assert a.failed.tolist() == [True, False] + a.reset(torch.tensor([1])) + assert a.failed.tolist() == [True, False] and not a.acquired.any() + a.reset(torch.tensor([0])) + assert not a.failed.any() + + +def test_load_dwell_completed_at_deadline_is_not_rejected(): + a = acquisition() + for k in range(21, 26): + a.update(torch.tensor([k*.002]), torch.zeros(1), torch.tensor([50.]), torch.tensor([50.]), .002) + assert a.acquired.all() and not a.failed.any() + + +@pytest.mark.parametrize('key,value', [('foot_clearance_m', .003), ('contact_deadline_s', .5), + ('dwell_s', .3), ('min_load_fraction', .1)]) +def test_initialization_config_rejects_airborne_or_unbounded_start(key, value): + c = load_config(ROOT/'configs/contact_cycle/go2_fl_v1.json') + c['initialization'][key] = value + with pytest.raises(ValueError): + validate_config(c) + + +def test_separate_phase_failures_preserve_thresholds(): + r = phase_failures(torch.tensor([2, 3, 5]), torch.ones(3), torch.tensor([0., 0., .05]), + torch.tensor([0., 0., 0.]), torch.full((3,), 50.)) + assert r['lift_deadline'].tolist() == [True, False, False] + assert r['swing_clearance'].tolist() == [False, True, False] + assert r['reload_deadline'].tolist() == [False, False, True] + + +def test_diagnostic_aggregation_does_not_confuse_missing_samples_or_change_rollout(): + flags = torch.zeros(2, 2, len(FAILURE_NAMES), dtype=torch.bool) + flags[1, 0, FAILURE_NAMES.index('contact_acquisition_timeout')] = True + data = {'phase': torch.tensor([[0, -1], [1, 2]]), 'failure_flags': flags, + 'reward': torch.ones(2, 2), 'value': torch.zeros(2, 2), 'next_value': torch.zeros(2, 2), + 'done': torch.tensor([[False, False], [True, False]])} + copy = {k:v.clone() for k,v in data.items()} + r = rollout_diagnostics(data, {'gamma': .99, 'gae_lambda': .95}) + assert r['phase_control_samples'] == [1, 1, 1, 0, 0, 0] + assert r['missing_diagnostic_samples'] == 1 + assert r['terminal_causes']['contact_acquisition_timeout'] == 1 + assert r['terminal_causes']['premature_separation'] == 0 + assert all(torch.equal(copy[k], data[k]) for k in data) diff --git a/tests/test_contact_cycle_learning.py b/tests/test_contact_cycle_learning.py new file mode 100644 index 0000000..7982623 --- /dev/null +++ b/tests/test_contact_cycle_learning.py @@ -0,0 +1,264 @@ +"""CPU/synthetic learner tests only; none performs robot training or holdout evaluation.""" + +import copy +import json +from pathlib import Path + +import pytest +import torch +from SkillFactory.legs.contact_cycle.config import load_config +from SkillFactory.legs.contact_cycle.curriculum import EpisodeSampler, assert_sample_domain, curriculum_stage +from SkillFactory.legs.contact_cycle.ppo import ( + CycleActorCritic, + bootstrap_values, + collect_rollout, + features, + ppo_step, + restore_checkpoint, + save_checkpoint, + selection_score, +) +from SkillFactory.legs.contact_cycle.training import ( + BudgetExceeded, + BudgetLedger, + digest, + load_training_config, + require_authorization, + save_trace_subset, +) + +ROOT = Path(__file__).resolve().parents[1] +TASK = ROOT/"configs/contact_cycle/go2_fl_v1.json" +TRAINING = ROOT/"configs/contact_cycle/training_v1.json" + + +@pytest.mark.parametrize("update,stage", [(1, 0), (512, 0), (513, 1), (1024, 1), (1025, 2), (2048, 2)]) +def test_stage_boundaries(update, stage): + assert curriculum_stage(update) == stage + + +def test_sampler_no_holdout_and_reset_order_independence(): + cfg = load_config(TASK) + with pytest.raises(ValueError, match="Independent"): + EpisodeSampler(cfg, 4, 1, "holdout") + a, b = EpisodeSampler(cfg, 4, 17, update=1500), EpisodeSampler(cfg, 4, 17, update=1500) + first = a.sample(torch.tensor([0, 2])) + second = b.sample(torch.tensor([2, 0])) + for key in first: + torch.testing.assert_close(first[key], second[key].flip(0)) + a.sample(torch.tensor([1])) + resumed = EpisodeSampler(cfg, 4, 17) + resumed.load_state_dict(a.state_dict()) + x, y = a.sample(torch.tensor([0])), resumed.sample(torch.tensor([0])) + for key in x: + torch.testing.assert_close(x[key], y[key]) + assert b.episodes.tolist() == [1, 0, 1, 0] + assert a.sample(torch.tensor([], dtype=torch.long))["commands"].shape == (0, 4, 6) + with pytest.raises(ValueError): + a.sample(torch.tensor([0, 0])) + + +def test_curriculum_distribution_budgets_exclusions_and_regression(): + cfg = load_config(TASK) + for update in (1, 600, 1500): + sample = EpisodeSampler(cfg, 512, 91101, update=update).sample(torch.arange(512)) + assert_sample_domain(cfg, sample) + if update == 1: + assert not sample["commands"].any() + assert 0.15 < sample["stand"].float().mean() < 0.35 + if update == 600: + assert not sample["commands"][..., 3:].any() + if update == 1500: + assert set(sample["scenario"].tolist()) == {0, 1, 2, 3} + assert set(sample["stage"].tolist()) == {0, 1, 2} + assert sample["commands"][..., 3:].abs().max() > 0 + for fault in ("commands", "points", "delays"): + bad = copy.deepcopy(sample) + bad[fault][0] = 100 + with pytest.raises(ValueError): + assert_sample_domain(cfg, bad) + + +def test_validation_distribution_independent_of_training_update(): + cfg = load_config(TASK) + a = EpisodeSampler(cfg, 8, 93101, "validation", update=1).sample(torch.arange(8)) + b = EpisodeSampler(cfg, 8, 93101, "validation", update=1500).sample(torch.arange(8)) + for key in a: + torch.testing.assert_close(a[key], b[key]) + assert not a["stand"].any() + + +@pytest.mark.parametrize("bad", [torch.zeros(2, 45), torch.zeros(2, 32, dtype=torch.float64), + torch.full((2, 32), float("nan")), {"privileged": torch.zeros(2, 32)}]) +def test_feature_contract_rejects_other_interfaces(bad): + with pytest.raises(ValueError): + features(bad) + + +class ValueModel: + def value(self, x): + return x[:, 0].clone() + + +@pytest.mark.parametrize("dtype", [torch.int32, torch.int64]) +def test_timeout_bootstraps_terminal_not_reset_and_failure_zero(dtype): + x = torch.ones(3, 32) + terminal = torch.zeros(2, 32) + terminal[:, 0] = torch.tensor([30., 20.]) + terminated, truncated = torch.tensor([True, False, False]), torch.tensor([True, True, True]) + info = {"terminal_ids": torch.tensor([2, 1], dtype=dtype), "terminal_observation": {"policy": terminal}} + v = bootstrap_values(ValueModel(), x, terminated, truncated, info) + torch.testing.assert_close(v, torch.tensor([0., 20., 30.])) + with pytest.raises(RuntimeError): + bootstrap_values(ValueModel(), x, terminated, truncated, {}) + + +class FakeEnv: + num_envs, device, step_dt = 2, "cpu", 0.01 + + def step(self, action): + assert action.shape == (2, 3) and (action.abs() <= 1).all() + # Both episodes timeout; deliberately distinct reset/terminal states. + return ({"policy": torch.zeros(2, 32), "privileged": torch.full((2, 71), float("nan"))}, + torch.ones(2)*0.01, torch.zeros(2, dtype=torch.bool), torch.ones(2, dtype=torch.bool), + {"terminal_ids": torch.tensor([0, 1]), "terminal_observation": {"policy": torch.ones(2, 32)}}) + + +def test_synthetic_ppo_and_atomic_restore(tmp_path): + torch.set_num_threads(1) + torch.manual_seed(4) + model = CycleActorCritic(hidden=16) + cfg = {**json.loads(TRAINING.read_text())["algorithm"], "horizon": 4, "minibatch_size": 4} + optimizer = torch.optim.Adam(model.parameters(), lr=cfg["learning_rate"]) + data, x, failures, timeouts = collect_rollout(FakeEnv(), model, torch.zeros(2, 32), cfg) + assert x.shape == (2, 32) and failures == 0 and timeouts == 8 + assert data["done"].all() and torch.isfinite(data["features"]).all() + result = ppo_step(model, optimizer, data, cfg) + assert result["sampling_log_prob_error"] <= 2e-4 + sampler = EpisodeSampler(load_config(TASK), 2, 11) + sampler.sample(torch.arange(2)) + path = tmp_path/"model.pt" + save_checkpoint(path, model, optimizer, {"test": "synthetic"}, 1, sampler, None, {}) + expected = model.act(x, deterministic=True)[0].detach().clone() + with torch.no_grad(): + model.actor[-1].bias.add_(1) + restore_checkpoint(path, model, optimizer, {"test": "synthetic"}, sampler) + torch.testing.assert_close(model.act(x, deterministic=True)[0], expected) + with pytest.raises(ValueError, match="mismatch"): + restore_checkpoint(path, model, optimizer, {"test": "different"}, sampler) + with pytest.raises(RuntimeError, match="budget"): + save_checkpoint(tmp_path/"too_big.pt", model, optimizer, {}, 1, sampler, None, {}, max_bytes=1) + assert not (tmp_path/"too_big.pt").exists() + + +def test_selection_failure_cannot_profit_from_missing_metrics(): + good = {"success": True, "swing_rms_m": 0.025, "stance_load_mae_weight_fraction": 0.25, "stance_slip_m": 0.03} + assert selection_score([good]) == 3 + assert selection_score([{"success": False}]) == 16 + assert selection_score([good, {"success": False}]) == 9.5 + with pytest.raises(ValueError): + selection_score([]) + with pytest.raises(ValueError): + selection_score([{**good, "swing_rms_m": float("nan")}]) + + +def test_configuration_and_explicit_authorization(tmp_path): + cfg = load_training_config(TRAINING, ROOT) + manifest = {"training": cfg} + with pytest.raises(PermissionError): + require_authorization(None, manifest) + grant = {"format": "skillfactory.contact-cycle-authorization.v1", "approved": True, + "manifest_sha256": digest(manifest), "operator": "synthetic_test_only", + "authorization_reference": "unit-test fixture, not robot training approval"} + path = tmp_path/"grant.json" + path.write_text(json.dumps(grant)) + require_authorization(path, manifest) + with pytest.raises(PermissionError): + require_authorization(path, {"training": {}}) + bad = copy.deepcopy(cfg) + bad["horizon"] = True + path.write_text(json.dumps(bad)) + with pytest.raises(ValueError): + load_training_config(path, ROOT) + + +def small_manifest(): + return {"training": {"seeds": [1, 2, 3], "num_envs": 2, "horizon": 4, "updates_per_seed": 2, + "max_total_transitions": 16, "max_total_wall_s": 100, "max_artifact_bytes": 100000}} + + +def test_global_budget_crash_charge_resume_and_lock(tmp_path): + now = [100.] + manifest = small_manifest() + with BudgetLedger(tmp_path, manifest, lambda: now[0], boot_now=lambda: now[0], boot_id="test-boot") as ledger: + ledger.reserve_rollout(1, 1) + with pytest.raises(ValueError): + ledger.reserve_rollout(1, 1) + with pytest.raises(BlockingIOError), BudgetLedger( + tmp_path, manifest, lambda: now[0], boot_now=lambda: now[0], boot_id="test-boot"): + pass + with BudgetLedger(tmp_path, manifest, lambda: now[0], boot_now=lambda: now[0], boot_id="test-boot") as resumed: + resumed.reconcile_checkpoint(1, 0) # interrupted rollout lost, no refund + resumed.reserve_rollout(1, 1) + resumed.commit_update(1, 1) + assert resumed.data["reserved_transitions"] == 16 + with pytest.raises(BudgetExceeded, match="transition"): + resumed.reserve_rollout(2, 1) + now[0] = 201 + with pytest.raises(BudgetExceeded, match="wall"): + resumed.guard() + + +def test_validation_rejects_missing_or_partial_disturbance_delivery(): + from SkillFactory.legs.contact_cycle.mechanics import delivered_events + + events = load_config(TASK)["events"] + scenario = torch.tensor([0, 1]) + mask = scenario[:, None] == torch.tensor([e["scenario"] for e in events]) + zero = torch.zeros_like(mask, dtype=torch.float32) + assert delivered_events(events, scenario, mask, mask, zero).tolist() == [True, False] + areas = torch.tensor([e["duration_s"] - e["ramp_s"] for e in events]) + assert delivered_events(events, scenario, mask, mask, mask*areas).all() + assert delivered_events(events, scenario, mask, torch.zeros_like(mask), mask*areas).tolist() == [True, False] + + +def test_engine_int32_reset_ids_and_invalid_float(): + from SkillFactory.legs.contact_cycle.mechanics import canonical_ids + + ids = canonical_ids(torch.tensor([0, 2], dtype=torch.int32), 4) + assert ids.dtype == torch.int64 and ids.tolist() == [0, 2] + sampler = EpisodeSampler(load_config(TASK), 4, 1) + sampler.sample(torch.tensor([0, 2], dtype=torch.int32)) + assert sampler.episodes.tolist() == [1, 0, 1, 0] + with pytest.raises(ValueError): + canonical_ids(torch.tensor([0., 2.]), 4) + + +def test_trace_storage_is_bounded_and_budget_failure_leaves_no_file(tmp_path): + trace = {"foot": torch.arange(100*128*3, dtype=torch.float32).reshape(100, 128, 3)} + sizes = [] + path = tmp_path/"subset.pt" + size = save_trace_subset(path, trace, [1], [2]*128, sizes.append) + assert size == path.stat().st_size == sizes[0] and size < 8192 + saved = torch.load(path, weights_only=True)["1"]["foot"] + assert saved.untyped_storage().nbytes() == saved.numel()*saved.element_size() == 24 + torch.testing.assert_close(saved, trace["foot"][:2, 1:2]) + + def refuse(size): + raise BudgetExceeded(f"synthetic capacity less than {size}") + + with pytest.raises(BudgetExceeded): + save_trace_subset(tmp_path/"insufficient.pt", trace, [1], [2]*128, refuse) + assert not (tmp_path/"insufficient.pt").exists() + assert list(tmp_path.iterdir()) == [path] + + +def test_checkpoint_commit_crash_window_and_disk_guard(tmp_path): + with BudgetLedger(tmp_path, small_manifest(), lambda: 100.) as ledger: + ledger.reserve_rollout(1, 1) + ledger.reconcile_checkpoint(1, 1) # optimizer/checkpoint completed before commit + assert ledger.data["updates"]["1"] == 1 and ledger.data["pending"] is None + with pytest.raises(ValueError): + ledger.commit_update(1, 2) + with pytest.raises(BudgetExceeded, match="artifact"): + ledger.guard(reserve_bytes=100001) diff --git a/tests/test_contact_cycle_metrics.py b/tests/test_contact_cycle_metrics.py new file mode 100644 index 0000000..399ff29 --- /dev/null +++ b/tests/test_contact_cycle_metrics.py @@ -0,0 +1,69 @@ +"""Synthetic negative coverage for report logic; not physical validation.""" + +import json +from pathlib import Path + +import pytest +import torch +from SkillFactory.legs.contact_cycle.metrics import summarize + + +@pytest.fixture +def fixture_trace(): + # Deliberately idealized input to test gate branches, not a dynamic simulator. + count = 600 + phase = torch.arange(6).repeat_interleave(100)[:, None] + progress = torch.linspace(0, 1, 100).repeat(6)[:, None] + time = (torch.arange(count)*0.02)[:, None] + force = torch.zeros(count, 1, 3) + force[:, :, 2] = torch.where((phase == 0) | (phase >= 4), 50., 0.) + trace = { + "phase": phase, "progress": progress, "time": time, "scenario": torch.zeros(count, 1), + "weight": torch.full((count, 1), 50.), "normal": force, + "friction": torch.zeros_like(force), "foot": torch.zeros_like(force), "target": torch.zeros_like(force), + "gap": torch.full((count, 1), 0.05), "desired_force": force[..., 2], + "joint_excess": torch.zeros(count, 1), "limit_ratio": torch.zeros(count, 1), + "slip_speed": torch.zeros(count, 1), "height_error": torch.zeros(count, 1), + "external": torch.zeros(count, 1, 1, 6), "guide": torch.zeros(count, 1, 1, 6), + "auxiliary": torch.zeros(count, 1, 1, 6), "would_fail": torch.zeros(count, 1).bool(), + "tilt": torch.zeros(count, 1), + } + path = Path(__file__).resolve().parents[1]/"configs/contact_cycle/acceptance_v1.json" + thresholds = json.loads(path.read_text())["gates"] + return trace, thresholds + + +def report(fixture): + trace, thresholds = fixture + return summarize(trace, 0.02, thresholds, ["baseline"])[0] + + +def test_complete_fixture_passes_logic_only(fixture_trace): + assert report(fixture_trace)["success"] + + +@pytest.mark.parametrize("fault,reason", [ + ("tilt", "tilt"), ("lift", "lift_deadline"), ("terminate", "task_termination"), + ("hover", "stance_contact"), ("drag", "slip"), ("nan", "missing_phase_or_nonfinite"), + ("missing", "missing_phase_or_nonfinite"), ("impact", "impact"), +]) +def test_negative_metrics_cannot_pass(fixture_trace, fault, reason): + t, _ = fixture_trace + if fault == "tilt": + t["tilt"][300] = 0.7 + elif fault == "lift": + t["gap"][290:300] = 0 + elif fault == "terminate": + t["would_fail"][100] = True + elif fault == "hover": + t["normal"][500:] = 0 + elif fault == "drag": + t["slip_speed"][500:] = 0.1 + elif fault == "nan": + t["normal"][400] = float("nan") + elif fault == "missing": + t["phase"][300:400] = 4 + elif fault == "impact": + t["normal"][405, 0, 2] = 200 + row = report(fixture_trace) + assert not row["success"] and reason in row["failure_reasons"] diff --git a/tests/test_contact_cycle_physics.py b/tests/test_contact_cycle_physics.py new file mode 100644 index 0000000..46c2a8f --- /dev/null +++ b/tests/test_contact_cycle_physics.py @@ -0,0 +1,34 @@ +"""Opt-in true PhysX integration; skips are NOT evidence of physical success.""" + +import json +import os +import subprocess +import sys +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[1] +pytestmark = pytest.mark.skipif(os.environ.get("CYCLE_RUNTIME_TESTS") != "1", reason="requires supervised Isaac PhysX") + + +@pytest.mark.parametrize("script", [ + "verify_wrench_physics.py", "verify_contact_cycle.py", "verify_cycle_learning_runtime.py", +]) +def test_real_physics_contract(script, tmp_path): + env = os.environ.copy() + env.pop("CONDA_PREFIX", None) + env.pop("VIRTUAL_ENV", None) + env["PYTHONPATH"] = str(ROOT/"source/SkillFactory") + lab = env.get("ISAACLAB_LAUNCHER", "/home/cen/IsaacLab/isaaclab.sh") + process = subprocess.run([ + sys.executable, str(ROOT/"scripts/supervise_sim.py"), + "--log", str(tmp_path/"kit.log"), "--report", str(tmp_path/"process.json"), "--timeout", "240", "--", + lab, "-p", str(ROOT/"scripts"/script), "--output", str(tmp_path/"trial"), "--headless", + ], cwd=ROOT, env=env, capture_output=True, text=True, timeout=270) + assert process.returncode == 0, process.stdout[-3000:] + process.stderr[-3000:] + result = json.loads((tmp_path/"trial/result.json").read_text()) + supervision = json.loads((tmp_path/"process.json").read_text()) + assert result["passed"] and not result["accepted_skill"] + assert supervision["passed"] and supervision["cleanup"]["clean"] + assert not supervision["critical_log_lines"] diff --git a/tests/test_contact_cycle_repair.py b/tests/test_contact_cycle_repair.py new file mode 100644 index 0000000..8aaa107 --- /dev/null +++ b/tests/test_contact_cycle_repair.py @@ -0,0 +1,172 @@ +"""Repair regression tests: fake clocks/physics signals; never robot optimization.""" + +import copy +import json +from pathlib import Path + +import pytest +import torch +from SkillFactory.legs.contact_cycle.config import load_config, validate_config +from SkillFactory.legs.contact_cycle.mechanics import ContactLatch, SeparationLatch, WrenchSchedule +from SkillFactory.legs.contact_cycle.task import phase_objective +from SkillFactory.legs.contact_cycle.training import BudgetExceeded, BudgetLedger + +ROOT = Path(__file__).resolve().parents[1] + + +def manifest(): + return {"training": {"seeds": [1, 2, 3], "num_envs": 2, "horizon": 4, "updates_per_seed": 2048, + "max_total_transitions": 100, "max_total_wall_s": 100, "max_artifact_bytes": 100000}} + + +class Clocks: + wall, boot, boot_id = 1000., 100., "synthetic-boot" + + def ledger(self, path): + return BudgetLedger(path, manifest(), lambda: self.wall, boot_now=lambda: self.boot, boot_id=self.boot_id) + + +def test_wall_corrections_do_not_change_deadline_or_refund_and_resume_counts_downtime(tmp_path): + c = Clocks() + with c.ledger(tmp_path) as b: + b.reserve_rollout(1, 1) + c.wall, c.boot = 900., 110. # UTC backwards, elapsed time forwards + b.guard() + assert b.data["clock"]["wall_rollbacks"] == 1 + assert b.data["clock"]["last_wall_rollback"] == {"previous": 1000., "observed": 900., "boottime": 110.} + c.wall, c.boot = 1e9, 120. # forward wall step also cannot spend/extend boot-clock time + b.commit_update(1, 1) + assert b.data["clock"]["deadline_s"] == 200. + assert b.data["reserved_transitions"] == 8 + c.wall, c.boot = 850., 190. # downtime (and suspend on Linux) is included + with c.ledger(tmp_path) as resumed: + assert resumed.data["clock"]["deadline_s"] == 200. + assert resumed.data["clock"]["wall_rollbacks"] == 2 + assert resumed.data["updates"]["1"] == 1 + c.boot = 200. + with pytest.raises(BudgetExceeded, match="wall-time.*boottime=200.*deadline=200"): + resumed.guard() + with pytest.raises(BudgetExceeded, match="wall-time"): + with c.ledger(tmp_path): + pass + + +@pytest.mark.parametrize("fault", ["reboot", "legacy", "deadline", "boot_rollback", "boot_nan", "wall_nan"]) +def test_clock_faults_fail_closed_without_mutating_disk(tmp_path, fault): + c = Clocks() + with c.ledger(tmp_path) as b: + b.reserve_rollout(1, 1) + p = tmp_path/"budget.json" + value = json.loads(p.read_text()) + if fault == "legacy": + del value["format"] + elif fault == "deadline": + value["clock"]["deadline_s"] += 1 + elif fault == "reboot": + c.boot_id = "different-boot" + elif fault == "boot_rollback": + c.boot = 99. + elif fault == "boot_nan": + c.boot = float("nan") + else: + c.wall = float("nan") + p.write_text(json.dumps(value)) + before = p.read_bytes() + with pytest.raises((ValueError, BudgetExceeded)): + with c.ledger(tmp_path): + pytest.fail("Invalid clock/ledger accepted") + assert p.read_bytes() == before + + +def test_live_boot_rollback_includes_observed_and_previous_values(tmp_path): + c = Clocks() + with c.ledger(tmp_path) as b: + c.boot = 99. + with pytest.raises(BudgetExceeded, match="observed=99.0, last=100.0, wall=1000.0"): + b.guard() + + +def latch(count=1): + return SeparationLatch(count, "cpu", on=0.001, off=0.0005, dwell=0.01) + + +def test_zero_load_is_not_liftoff_then_geometric_lift_delivers_full_pulse(): + sep, load = latch(), ContactLatch(1, "cpu") + config = load_config(ROOT/"configs/contact_cycle/go2_fl_v1.json") + e = copy.deepcopy(config["events"][1]) + schedule = WrenchSchedule([e], 1, "cpu") + edges, load_edges = [], [] + for tick in range(601): + t = 3.8 + tick*0.002 + phase = torch.tensor([1 if t < 4 else 2 if t < 4.6 else 3]) + fn = torch.tensor([50. if t < 3.9 else 0.]) + off, on = load.update(fn, 0.002) + # Remains on the plane after Fn drops below threshold; separates only during lift. + gap = torch.tensor([0. if t < 4.05 else 0.002]) + edge = sep.update(gap, load.contact, phase, 0.002) + if off.item(): + load_edges.append(t) + if edge.item(): + edges.append(t) + schedule.step(torch.tensor([t]), phase, edge, on, torch.tensor([2]), 0.002, sep.edge_phase) + assert len(load_edges) == len(edges) == 1 and load_edges[0] < 4 < edges[0] + assert not sep.premature.any() + assert schedule.fired.all() and schedule.completed.all() + torch.testing.assert_close(schedule.integral, torch.tensor([[e["duration_s"]-e["ramp_s"]]]), atol=0.002, rtol=0) + + +def test_early_separation_straddling_phase_boundary_cannot_be_laundered_into_event(): + sep = latch(2) + yes, no = torch.ones(2, dtype=torch.bool), torch.zeros(2, dtype=torch.bool) + sep.update(torch.zeros(2), yes, torch.ones(2, dtype=torch.long), 0.002) + for k in range(5): + phase = torch.tensor([1 if k == 0 else 2, 2]) + edge = sep.update(torch.full((2,), 0.002), no, phase, 0.002) + assert edge.all() and sep.edge_phase.tolist() == [1, 2] + assert sep.premature.tolist() == [True, False] + cfg = load_config(ROOT/"configs/contact_cycle/go2_fl_v1.json") + b = WrenchSchedule([cfg["events"][1]], 2, "cpu") + b.step(torch.full((2,), 4.006), phase, edge, no, torch.full((2,), 2), 0.002, sep.edge_phase) + assert b.fired[:, 0].tolist() == [False, True] + for _ in range(10): + sep.update(torch.zeros(2), yes, phase, 0.002) + assert sep.premature.tolist() == [True, False] # grounding again cannot hide failure + sep.reset(torch.tensor([1])) + assert sep.premature.tolist() == [True, False] + sep.reset(torch.tensor([0])) + assert not sep.premature.any() and not sep.armed.any() and (sep.edge_phase == -1).all() + + +def test_gap_hysteresis_blip_and_unarmed_initial_flight(): + s = latch() + yes, no, phase = torch.tensor([True]), torch.tensor([False]), torch.tensor([2]) + for _ in range(5): + assert not s.update(torch.tensor([0.003]), no, phase, 0.002).any() + s.reset(torch.tensor([0])) + s.update(torch.zeros(1), yes, phase, 0.002) + for _ in range(4): + assert not s.update(torch.tensor([0.002]), no, phase, 0.002).any() + s.update(torch.zeros(1), yes, phase, 0.002) # sub-dwell excursion does not commit + assert not s.separated.any() + for _ in range(5): + edge = s.update(torch.tensor([0.002]), no, phase, 0.002) + assert edge.all() + for _ in range(20): + assert not s.update(torch.tensor([0.00075]), no, phase, 0.002).any() + assert s.separated.all() + + +def test_unload_reward_prefers_zero_load_on_plane_not_early_flight(): + zero, phase = torch.zeros(2), torch.ones(2, dtype=torch.long) + reward, _ = phase_objective(phase, torch.full((2,), 0.98), torch.tensor([0., 0.005]), + torch.tensor([0., 0.005]), zero, zero, zero, zero, torch.full((2,), 50.)) + assert reward[0] == 1 and reward[1] < 0.5 + + +@pytest.mark.parametrize("key,value", [("separation_off_m", 0.001), ("separation_on_m", float("nan")), + ("dwell_s", 0), ("unload_tracking_scale_m", True)]) +def test_transition_config_rejected(key, value): + c = load_config(ROOT/"configs/contact_cycle/go2_fl_v1.json") + c["contact_transition"][key] = value + with pytest.raises(ValueError): + validate_config(c) diff --git a/tests/test_contact_cycle_sim_config_preimports.py b/tests/test_contact_cycle_sim_config_preimports.py new file mode 100644 index 0000000..7f3ab44 --- /dev/null +++ b/tests/test_contact_cycle_sim_config_preimports.py @@ -0,0 +1,357 @@ +"""CPU-only runner import phase: actual pinned string/configclass source, no Kit. + +File-backed isolated test packages are fixtures only, never runtime substitutes. +The configclass dictionary conversion helpers are disabled as in binding tests. +""" +import ast +import dataclasses +import enum +import functools +import inspect +import sys +from pathlib import Path +from types import MappingProxyType, SimpleNamespace +from typing import ClassVar + +import pytest +from test_contact_cycle_sim_evaluation_binding import binding, load +from test_contact_cycle_sim_evaluation_runner import cli + + +@pytest.fixture +def sources(tmp_path, monkeypatch): + installed = binding.ISAAC_ROOT + root = tmp_path / 'source/isaaclab/isaaclab' + for name, path in [('isaaclab', root), ('isaaclab.utils', root / 'utils')]: + path.mkdir(parents=True, exist_ok=True) + (path / '__init__.py').write_text('') + load(name, path / '__init__.py', monkeypatch) + for leaf in ('string', 'configclass'): + name = 'isaaclab.utils.' + leaf + relative, digest = binding.ISAAC_PINS[name] + text = (installed / relative).read_text() + assert binding.sha256(text.encode()) == digest + if leaf == 'configclass': + text = text.replace('from .dict import class_to_dict, update_class_from_dict', + 'class_to_dict = update_class_from_dict = None') + path = root / 'utils' / (leaf + '.py') + path.write_text(text) + load(name, path, monkeypatch) + monkeypatch.setattr(binding, 'ISAAC_ROOT', tmp_path) + return SimpleNamespace(root=root, installed=installed, + rs=sys.modules['isaaclab.utils.string'].ResolvableString, + configclass=sys.modules['isaaclab.utils.configclass'].configclass) + + +def test_preimport_config_references_real_configclass_nested_no_cache_mutation(sources, monkeypatch): + @sources.configclass + class Nested: + references = {'nested': ['isaaclab.utils.string:to_snake_case', + ('isaaclab.utils.string:to_camel_case',)]} + + @dataclasses.dataclass(slots=True) + class Slotted: + nested: object + schema: ClassVar = sources.rs('isaaclab.utils.string:string_to_slice') + + config = Slotted(Nested()) + assert cli.stored_config_fields(config) == binding._config_fields(config) + refs = cli.config_references(config, binding) + before = [object.__getattribute__(r, '_resolved_callable') for r in refs] + imported = [] + original = cli.importlib.import_module + + def eager(name): + imported.append(name) + return original(name) + + monkeypatch.setattr(cli.importlib, 'import_module', eager) + report = cli.preimport_config_references(config, binding) + assert imported == ['isaaclab.utils.string'] + assert len(report['references']) == 3 + path = sources.root / 'utils/string.py' + assert report['modules'] == {'isaaclab.utils.string': { + 'path': str(path), 'sha256': binding.sha256(path.read_bytes())}} + assert [object.__getattribute__(r, '_resolved_callable') for r in refs] == before == [None] * 3 + assert all(object.__getattribute__(r, '_resolve_error') is None for r in refs) + # The unchanged normalizer owns cache writes, not the eager import phase. + for obj, field, value in binding._reference_updates(config): + setattr(obj, field, value) + assert all(object.__getattribute__(r, '_resolved_callable') is not None for r in refs) + names = set() + binding._encode(refs, names, set()) + assert 'isaaclab.utils.string' in names # enters the unchanged source/dependency closure + + +def test_config_references_actual_installed_material_cfg_strings(sources): + path = sources.installed / 'source/isaaclab/isaaclab/sim/spawners/materials/physics_materials_cfg.py' + tree = ast.parse(path.read_text()) + reference = next(n.value for n in ast.walk(tree) if isinstance(n, ast.Constant) + and n.value == '{DIR}.physics_materials:spawn_rigid_body_material') + cls = type('MaterialCfg', (), {'__module__': 'isaaclab.sim.spawners.materials.physics_materials_cfg', + '__annotations__': {'func': str}, 'func': reference}) + material = sources.configclass(cls)() + assert cli.config_references({'scene': [material]}, binding) == [material.func] + assert type(material.func) is sources.rs + assert str(material.func) == 'isaaclab.sim.spawners.materials.physics_materials:spawn_rigid_body_material' + assert object.__getattribute__(material.func, '_resolved_callable') is None + + +def test_config_references_all_encoder_containers_and_plain_strings(sources): + ref = sources.rs('isaaclab.utils.string:to_snake_case') + + class Mode(enum.Enum): + VALUE = ref + + value = MappingProxyType({'partial': functools.partial(str, ref, value=ref), + 'slice': slice(ref), 'enum': Mode.VALUE, + 'plain': 'outside.module:never_import', 'path': Path('/unused')}) + assert len(cli.config_references(value, binding)) == 4 + + +@pytest.mark.parametrize('bad', ['no_colon', 'x:y:z', ':name', 'a..b:name', 'a/b:name', + 'a.b:with.dot', 'a.b:bad()', 'lambda x: x']) +def test_preimport_config_references_malformed_before_any_import(sources, monkeypatch, bad): + monkeypatch.setattr(cli.importlib, 'import_module', lambda _: pytest.fail('must not import')) + with pytest.raises(ValueError, match='Unsupported ResolvableString'): + cli.preimport_config_references([sources.rs('isaaclab.utils.string:to_snake_case'), sources.rs(bad)], binding) + + +@pytest.mark.parametrize('kind', ['cycle', 'unsupported', 'mapping_key', 'nonfinite', 'subclass']) +def test_preimport_config_references_invalid_graph_before_any_import(sources, monkeypatch, kind): + value = [sources.rs('isaaclab.utils.string:to_snake_case')] + if kind == 'cycle': + value.append(value) + elif kind == 'unsupported': + value.append(object()) + elif kind == 'mapping_key': + value.append({1: 'bad'}) + elif kind == 'nonfinite': + value.append(float('nan')) + else: + class FakeString(str): + pass + value.append(FakeString('isaaclab.utils.string:to_snake_case')) + monkeypatch.setattr(cli.importlib, 'import_module', lambda _: pytest.fail('must not import')) + with pytest.raises(ValueError): + cli.preimport_config_references(value, binding) + + +def test_preimport_config_references_rejects_property_without_evaluation(sources): + @dataclasses.dataclass + class Config: + field: object = None + + cfg = Config() + del cfg.field + Config.field = property(lambda _: pytest.fail('property must not run')) + with pytest.raises(ValueError, match='descriptor'): + cli.preimport_config_references(cfg, binding) + + +@pytest.mark.parametrize('reference', ['os:system', 'isaaclab_evil.module:call', 'numpy:array']) +def test_preimport_config_references_external_requires_inventory(sources, monkeypatch, reference): + monkeypatch.setattr(cli.importlib, 'import_module', lambda _: pytest.fail('must not import')) + with pytest.raises(ValueError, match='explicit external inventory'): + cli.preimport_config_references(sources.rs(reference), binding) + + +def test_preimport_config_references_absent_module(sources): + with pytest.raises(ModuleNotFoundError, match='Missing installed config module') as caught: + cli.preimport_config_references(sources.rs('isaaclab.utils.absent:run'), binding) + assert caught.value.name == 'isaaclab.utils.absent' + + +@pytest.mark.parametrize('error', [ModuleNotFoundError('transitive', name='broken_dependency'), + ImportError('native failure')]) +def test_preimport_config_references_import_failure_surfaces_unchanged(sources, monkeypatch, error): + def broken(_): + raise error + monkeypatch.setattr(cli.importlib, 'import_module', broken) + ref = sources.rs('isaaclab.utils.string:to_snake_case') + with pytest.raises(type(error)) as caught: + cli.preimport_config_references(ref, binding) + assert caught.value is error + assert object.__getattribute__(ref, '_resolved_callable') is None + assert object.__getattribute__(ref, '_resolve_error') is None + + +def test_preimport_config_references_imports_not_previously_loaded_module(sources, monkeypatch): + # A real source-backed CPU fixture whose function must never execute. + path = sources.root / 'utils/new_target.py' + path.write_text('from isaaclab.utils.string import to_snake_case\n' + 'def spawn():\n raise AssertionError("must not execute")\n') + monkeypatch.delitem(sys.modules, 'isaaclab.utils.new_target', raising=False) + try: + ref = sources.rs('isaaclab.utils.new_target:spawn') + report = cli.preimport_config_references(ref, binding) + assert report['modules']['isaaclab.utils.new_target']['path'] == str(path) + module = sys.modules['isaaclab.utils.new_target'] + assert inspect.isfunction(module.spawn) + assert module.to_snake_case is sys.modules['isaaclab.utils.string'].to_snake_case + assert object.__getattribute__(ref, '_resolved_callable') is None + finally: + sys.modules.pop('isaaclab.utils.new_target', None) + + +@pytest.mark.parametrize('mutation', ['path', 'package_search', 'cached_error', 'cached_conflict', 'not_callable']) +def test_preimport_config_references_identity_and_cache_fail_closed(sources, monkeypatch, mutation): + ref = sources.rs('isaaclab.utils.string:to_snake_case') + module = sys.modules['isaaclab.utils.string'] + if mutation == 'path': + monkeypatch.setattr(module, '__file__', '/not/the/installed/source.py') + elif mutation == 'package_search': + monkeypatch.setattr(sys.modules['isaaclab.utils'], '__path__', ['/outside']) + elif mutation == 'cached_error': + ref._resolve_error = ValueError('prior failure') + elif mutation == 'cached_conflict': + ref._resolved_callable = str + else: + monkeypatch.setattr(module, 'to_snake_case', object()) + with pytest.raises((ValueError, FileNotFoundError)): + cli.preimport_config_references(ref, binding) + + +def test_real_runtime_preimports_before_unchanged_normalizer(): + source = inspect.getsource(cli.real_runtime) + assert source.index('preimport_config_references(cfg, binding)') < source.index('binding.normalize_config(cfg)') + assert source.count('binding.normalize_config(cfg)') == 1 + + +def test_stored_config_fields_inherited_slots_extra_fields_and_properties(sources): + @dataclasses.dataclass + class Parent: + field: object + + @property + def unrelated(self): + pytest.fail('unrelated property must not run') + + class Child(Parent): + __slots__ = ('extra', 'uninitialized', '__private') + + cfg = Child(sources.rs('isaaclab.utils.string:to_snake_case')) + cfg.extra = sources.rs('isaaclab.utils.string:to_camel_case') + cfg.dynamic = {'nested': cfg.field} + assert cli.stored_config_fields(cfg) == binding._config_fields(cfg) + assert len(cli.config_references(cfg, binding)) == 3 + + +def test_preimport_config_references_real_transitive_failure_no_cache_write(sources, monkeypatch): + path = sources.root / 'utils/broken_target.py' + path.write_text('import missing_config_transitive_dependency\n') + monkeypatch.delitem(sys.modules, 'isaaclab.utils.broken_target', raising=False) + ref = sources.rs('isaaclab.utils.broken_target:spawn') + with pytest.raises(ModuleNotFoundError) as caught: + cli.preimport_config_references(ref, binding) + assert caught.value.name == 'missing_config_transitive_dependency' + assert object.__getattribute__(ref, '_resolved_callable') is None + assert object.__getattribute__(ref, '_resolve_error') is None + assert 'isaaclab.utils.broken_target' not in sys.modules + + +def test_preimport_config_references_explicit_dependency_role(sources, tmp_path, monkeypatch): + from test_contact_cycle_sim_evaluation_binding import numerical_inventory + + for name in tuple(sys.modules): + if name.split('.')[0] in binding.NUMERICAL_ROOTS: + monkeypatch.delitem(sys.modules, name) + numerical_inventory(tmp_path, monkeypatch) + dependency_path = tmp_path / 'config_dependency.py' + dependency_path.write_text('def helper():\n raise AssertionError("must not execute")\n') + dependency = load('_config_reference_dependency', dependency_path, monkeypatch) + monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', {**binding.REQUIRED_RUNTIME, + dependency.__name__: ('config-callable-provider', str(dependency_path), + binding.sha256(dependency_path.read_bytes()))}) + path = sources.root / 'utils/target.py' + path.write_text('from _config_reference_dependency import helper\n' + 'def spawn():\n raise AssertionError("must not execute")\n') + load('isaaclab.utils.target', path, monkeypatch) + ref = sources.rs('isaaclab.utils.target:spawn') + report = cli.preimport_config_references(ref, binding) + monkeypatch.setattr(binding, '_sources', lambda *_: ({}, set())) + # Only isolate unrelated installed-pin source scanning; retain real encoder, + # explicit dependency roles and the inventory validator. + captured = set() + + def imported(names, _, *, diagnostics=None): + captured.update(names) + return {name: {'path': str(binding._module_file(name))} for name in names} + + monkeypatch.setattr(binding, '_imports', imported) + proposal = cli.proposed_inventory(binding, ref) + assert set(report['modules']) <= captured + assert proposal['modules'][dependency.__name__] == { + 'path': str(dependency_path), 'sha256': binding.sha256(dependency_path.read_bytes())} + assert binding._dependency_inventory(proposal, captured, set()) == proposal + + +@pytest.mark.parametrize('eye,lookat', [((1.2, 1.2, 1), (0, 0, .1672991751785088)), + ((-2, 3, 4), (.2, .3, .4))]) +def test_preforward_video_recorder_matches_pinned_constructor_idempotently(eye, lookat): + path = binding.ISAAC_ROOT / 'source/isaaclab/isaaclab/envs/manager_based_rl_env.py' + assert binding.sha256(path.read_bytes()) == 'a963cfcdb4cd60955cd022ed709d27c99a6935685b69180bb29d74f8983f6566' + tree = ast.parse(path.read_text()) + constructor = next(n for n in ast.walk(tree) if isinstance(n, ast.FunctionDef) and n.name == '__init__') + forwarding = next(n for n in constructor.body if isinstance(n, ast.If) + and ast.unparse(n.test) == 'cfg.video_recorder is not None') + cfg = SimpleNamespace(viewer=SimpleNamespace(eye=eye, lookat=lookat), + video_recorder=SimpleNamespace(env_render_mode='rgb_array', eye=(7.5,) * 3, + lookat=(0.,) * 3)) + checked = [] + cli.preforward_video_recorder(cfg, SimpleNamespace(verify_installed_stepping=lambda p: checked.append(p))) + assert checked == [path.parent] + before = vars(cfg.video_recorder).copy() + assert before == {'env_render_mode': None, 'eye': tuple(map(float, eye)), 'lookat': tuple(map(float, lookat))} + # Execute only the installed forwarding AST, never instantiate Isaac/Kit. + exec(compile(ast.Module(body=[forwarding], type_ignores=[]), str(path), 'exec'), + {'cfg': cfg, 'render_mode': None}) + assert vars(cfg.video_recorder) == before + cli.preforward_video_recorder(cfg, SimpleNamespace(verify_installed_stepping=lambda _: None)) + assert vars(cfg.video_recorder) == before + cfg.viewer.eye = (9., 8., 7.) + # No after-the-fact normalization: real binding detects changed recorder config. + exec(compile(ast.Module(body=[forwarding], type_ignores=[]), str(path), 'exec'), + {'cfg': cfg, 'render_mode': None}) + assert vars(cfg.video_recorder) != before + + +def test_preimport_constructor_modules_exact_diagnosed_sources(monkeypatch): + import json + + path = Path('/home/cen/sf-runs/p1-01/row0-binding-diagnostics.json') + if not path.exists(): + pytest.skip('P1 diagnostic not installed') + differences = json.loads(path.read_bytes())['distinct_diagnostic']['differences'] + expected = {d['path'].removeprefix('/imports/'): d['actual']['value'] for d in differences + if d['path'].startswith('/imports/')} + assert len(expected) == 23 + assert {k: (v['path'], v['sha256']) for k, v in expected.items()} == cli.CONSTRUCTOR_IMPORT_PINS + imported = [] + monkeypatch.setattr(cli.importlib, 'import_module', lambda name: imported.append(name)) + monkeypatch.setattr(binding, '_module_file', lambda name: Path(expected[name]['path'])) + monkeypatch.setattr(cli, 'config_module_path', lambda name, _: Path(expected[name]['path'])) + assert cli.preimport_constructor_modules(binding) == expected + assert imported == list(expected) + + +@pytest.mark.parametrize('drift', ['hash', 'resolved_path', 'imported_path', 'missing', 'transitive_failure']) +def test_preimport_constructor_modules_fail_closed(monkeypatch, tmp_path, drift): + path = tmp_path / 'reviewed.py' + path.write_text('reviewed = True\n') + name = 'isaaclab.reviewed' + monkeypatch.setattr(cli, 'CONSTRUCTOR_IMPORT_PINS', {name: (str(path), binding.sha256(path.read_bytes()))}) + monkeypatch.setattr(cli, 'config_module_path', lambda *_: path if drift != 'resolved_path' else tmp_path) + monkeypatch.setattr(binding, '_module_file', lambda _: path if drift != 'imported_path' else tmp_path) + + def eager(_): + if drift == 'transitive_failure': + raise ModuleNotFoundError('unexpected lazy dependency') + + monkeypatch.setattr(cli.importlib, 'import_module', eager) + if drift == 'hash': + path.write_text('changed = True\n') + if drift == 'missing': + path.unlink() + with pytest.raises((ValueError, FileNotFoundError, ModuleNotFoundError)): + cli.preimport_constructor_modules(binding) diff --git a/tests/test_contact_cycle_sim_constructor_export.py b/tests/test_contact_cycle_sim_constructor_export.py new file mode 100644 index 0000000..5d62431 --- /dev/null +++ b/tests/test_contact_cycle_sim_constructor_export.py @@ -0,0 +1,130 @@ +"""CPU-only same-name lazy export collision; installed definitions, no Kit. + +Native imports are omitted from a file-backed PhysX definition-only fixture. +The function is never invoked. Configclass and lazy-loader logic are real. +""" +import ast +import importlib +import json +import sys +from pathlib import Path +from types import ModuleType, SimpleNamespace + +import pytest +from test_contact_cycle_sim_config_preimports import sources # noqa: F401 - pytest fixture +from test_contact_cycle_sim_evaluation_binding import binding, load +from test_contact_cycle_sim_evaluation_runner import cli + + +@pytest.fixture +def export_sources(sources, tmp_path, monkeypatch): # noqa: F811 + loader = Path(next(p for p in cli.PHYSX_EXPORT_SOURCES if '/lazy_loader/' in p)) + assert binding.sha256(loader.read_bytes()) == cli.PHYSX_EXPORT_SOURCES[str(loader)] + load('lazy_loader', loader, monkeypatch) + load('isaaclab.utils.module', sources.installed / 'source/isaaclab/isaaclab/utils/module.py', monkeypatch) + root = tmp_path / 'isaaclab_physx' + root.mkdir() + (root / '__init__.py').write_text('') + load('isaaclab_physx', root / '__init__.py', monkeypatch) + package_name = 'isaaclab_physx.cloner' + module_name = package_name + '.physx_replicate' + package = root / 'cloner' + package.mkdir() + pins = {} + for name, leaf in ((package_name, '__init__.py'), (module_name, 'physx_replicate.py')): + path, digest = cli.CONSTRUCTOR_IMPORT_PINS[name] + assert binding.sha256(Path(path).read_bytes()) == digest + text = Path(path).read_text() + if name == module_name: + tree = ast.parse(text) + tree.body = [n for n in tree.body if isinstance(n, ast.FunctionDef) + or isinstance(n, ast.ImportFrom) and n.module == '__future__'] + text = ast.unparse(tree) + target = package / leaf + target.write_text(text) + pins[name] = (str(target), binding.sha256(target.read_bytes())) + stub = package / '__init__.pyi' + original_stub = Path(cli.CONSTRUCTOR_IMPORT_PINS[package_name][0]).with_suffix('.pyi') + assert binding.sha256(original_stub.read_bytes()) == cli.PHYSX_EXPORT_SOURCES[str(original_stub)] + stub.write_bytes(original_stub.read_bytes()) + monkeypatch.setattr(cli, 'CONSTRUCTOR_IMPORT_PINS', pins) + monkeypatch.setattr(cli, 'PHYSX_EXPORT_SOURCES', { + str(stub): binding.sha256(stub.read_bytes()), str(loader): binding.sha256(loader.read_bytes())}) + # Real importlib publishes the same-name submodule onto the fixture package. + monkeypatch.delitem(sys.modules, module_name, raising=False) + pkg = load(package_name, package / '__init__.py', monkeypatch) + yield SimpleNamespace(package=pkg, module_name=module_name, stub=stub) + sys.modules.pop(module_name, None) + + +@pytest.mark.parametrize('order', ['cold', 'eager']) +def test_physx_constructor_export_real_lazy_loader_and_clonecfg(sources, export_sources, order): # noqa: F811 + fixture = export_sources + if order == 'cold': + # Natural constructor import resolves the declared callable lazily. + from isaaclab_physx.cloner import physx_replicate + assert callable(physx_replicate) + module = importlib.import_module(fixture.module_name) + if order == 'eager': + assert isinstance(fixture.package.physx_replicate, ModuleType) + # Actual installed CloneCfg class, with inert random strategy dependency. + path = '/home/cen/IsaacLab/source/isaaclab/isaaclab/cloner/cloner_cfg.py' + digest = 'f48e268609e8e5e91e30b8e6d364cc76579aeb497d69f963cd45975152a282ca' + assert binding.sha256(Path(path).read_bytes()) == digest + tree = ast.parse(Path(path).read_text()) + tree.body = [n for n in tree.body if not isinstance(n, ast.ImportFrom) + or n.module == '__future__'] + namespace = {'configclass': sources.configclass, 'random': lambda: None, '__name__': __name__} + exec(compile(tree, path, 'exec'), namespace) + clone_cfg = namespace['CloneCfg'] + if order == 'eager': + with pytest.raises(TypeError, match="cannot pickle 'module' object") as caught: + clone_cfg(physics_clone_fn=fixture.package.physx_replicate) + frames = cli.bounded_traceback(caught.value)['frames'] + assert any(f['function'] == '_custom_post_init' and f['line'] == 496 for f in frames) + assert frames[-1]['function'] == 'deepcopy' + target, metadata = cli.physx_constructor_export(binding, resolve=True) + assert target is module.physx_replicate is fixture.package.physx_replicate + assert clone_cfg(physics_clone_fn=target).physics_clone_fn is target + # Repeating the eager inventory import cannot overwrite an already-loaded submodule. + assert importlib.import_module(fixture.module_name) is module + runtime = SimpleNamespace(binding=binding, constructor_export=target, + generator_inventory={'constructor_export': metadata}) + cli.verify_constructor_export(runtime) + assert json.loads(json.dumps(metadata)) == metadata + fixture.package.physx_replicate = module + with pytest.raises(ValueError, match='namespace'): + cli.verify_constructor_export(runtime) # verification never repairs an overwrite + assert fixture.package.physx_replicate is module + + +@pytest.mark.parametrize('drift', ['hash', 'module', 'symbol', 'callable', 'late_callable', 'resolver', 'metadata']) +def test_physx_constructor_export_fail_closed(export_sources, monkeypatch, drift): + fixture = export_sources + module = importlib.import_module(fixture.module_name) + target, metadata = cli.physx_constructor_export(binding, resolve=True) + runtime = SimpleNamespace(binding=binding, constructor_export=target, + generator_inventory={'constructor_export': metadata}) + if drift == 'hash': + fixture.stub.write_text('changed export declaration\n') + elif drift == 'module': + monkeypatch.setattr(module, '__file__', '/wrong/source.py') + elif drift == 'symbol': + module.physx_replicate = module + elif drift == 'callable': + module.physx_replicate = lambda: None + elif drift == 'late_callable': + # Same code/source, new callable identity: must not be accepted after binding. + import types + changed = types.FunctionType(target.__code__, target.__globals__, target.__name__) + changed.__qualname__ = target.__qualname__ + module.physx_replicate = fixture.package.physx_replicate = changed + elif drift == 'resolver': + fixture.package.__getattr__ = lambda _: target + else: + runtime.generator_inventory = {'constructor_export': {}} + with pytest.raises((ValueError, OSError)): + if drift == 'resolver': + cli.physx_constructor_export(binding, resolve=True) + else: + cli.verify_constructor_export(runtime) diff --git a/tests/test_contact_cycle_sim_evaluation_audit.py b/tests/test_contact_cycle_sim_evaluation_audit.py new file mode 100644 index 0000000..782b17e --- /dev/null +++ b/tests/test_contact_cycle_sim_evaluation_audit.py @@ -0,0 +1,1001 @@ +"""CPU synthetic evidence only: no simulator, actor, checkpoint or optimizer execution.""" +import copy +import gzip +import importlib.util +import json +import lzma +import math +import sys +from dataclasses import asdict, dataclass, field +from pathlib import Path +from types import SimpleNamespace + +import pytest +import torch +from SkillFactory.legs.contact_cycle_sim.evaluation_audit import ( + BOOLS, + SCALARS, + TRACE_FILENAME, + VECTORS, + TrialAudit, + matrix_report, + plain, + sub, +) +from SkillFactory.legs.contact_cycle_sim.evaluation_protocol import load_protocol + +ROOT = Path(__file__).resolve().parents[1] + + +def protocol(stage='P1'): + return load_protocol(ROOT / 'configs/contact_cycle' / ( + 'continuous_eval_p1_v1.json' if stage == 'P1' else 'bounded_diagnostic_p2_v1.json')) + + +def state(p, trial, clock, refreshes=1): + anchors = {'anchor_a_world': [0., 0., .022], 'root_origin_world': [0., 0., .3], + 'initial_root_world': [0., 0., .3]} + c = p.command(trial, clock, anchor_a_world=anchors['anchor_a_world'], + root_origin_world=anchors['root_origin_world'], weight_N=40.) + g = clock.global_tick + s = {k: 0. for k in SCALARS} + s.update({k: False for k in BOOLS}) + s.update({k: [0.] * n for k, n in VECTORS.items()}) + s.update(weight=40., time=g * .002, substeps=g, anchor_height=.3, anchors=anchors, + body_names=['virtual_payload', 'FL_thigh', 'FL_calf'], foot=list(c.target_world_m), + normal=[0., 0., 40. if c.phase in (0, 5) else 16. if c.phase == 1 else 0.], + root=[0., 0., .3, 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], + gap=c.target_world_m[2] - .022, q_ik=[refreshes * .001] * 3, refreshes=refreshes, + contact=c.phase in (0, 1, 5), acquired=g >= 5, armed=g >= 5, + separated=c.phase in (2, 3), onset_phase=2 if c.phase in (2, 3) else -1, edge_phase=-1, + ik_target=sub(c.target_world_m, [0., 0., .3]), scenario=p.trial(trial).scenario_id, + event_program=plain(p.document['events']), external=[[0.] * 6 for _ in range(3)], + guide=[[0.] * 6 for _ in range(3)], auxiliary=[[0.] * 6 for _ in range(3)], + sensor_delay=[[0.] * 18], action_delay=[[0.] * 3], rng={'test_rng': [1, 2, 3]}, + actuator_state={}, applied=None, + counters={'reset_calls': 3, 'pose_write_calls': 1, 'composer_buffer_replacements': g, + 'initialization_steps': 2, 'rollout_steps': g, + 'attempted_steps': g + 2, 'completed_steps': g + 2}, + reward_components={k: 0. for k in ('task', 'effort', 'action_rate', 'root_position', + 'root_velocity', 'residual')}) + s['events'] = [{'fired': False, 'completed': False, 'start': 0., 'envelope': 0., 'integral': 0.} + for _ in s['event_program']] + return s, plain(asdict(c)) + + +def records(p, trial=0, end=None): + total = p.trial(trial).total_ticks if end is None else end + clock = p.next_command_clock(trial, 0) + s, command = state(p, trial, clock) + refclock = plain(asdict(clock)) + event = dict(kind='initial', trial_index=trial, clock=refclock, command=command, + held_control_index=None, action_origin_tick=None, reference_used_clock=None, + reference_used=[0.] * 3, reference_next_clock=refclock, reference_next=[0.] * 3, causes=[]) + yield {'kind': 'initial', 'event': event, 'state': s} + refreshes = 1 + for g in range(1, total + 1): + if (g - 1) % 5 == 0: + learned = p.trial(trial).controller_kind == 'learned_policy' + yield {'kind': 'control', 'origin_tick': g - 1, 'observation': [0.] * 46, 'action': [0.] * 3, + 'mean': [0.] * 3 if learned else None, 'std': [.1] * 3 if learned else None} + clock = p.evidence_clock(trial, g) + s, command = state(p, trial, clock, refreshes) + event = dict(kind='tick', trial_index=trial, clock=plain(asdict(clock)), command=command, + held_control_index=(g-1)//5, action_origin_tick=5*((g-1)//5), reference_used_clock=refclock, + reference_used=[0.] * 3, reference_next_clock=refclock, reference_next=[0.] * 3, causes=[]) + record = {'kind': 'tick', 'event': event, 'state': s} + yield record + if g % 5 == 0 and g < p.trial(trial).total_ticks: + if g % 4500 == 0: + yield {**copy.deepcopy(record), 'kind': 'seam'} + clock = p.next_command_clock(trial, g, seam_admitted=True) + _, command = state(p, trial, clock) + refreshes += 1 + refclock = plain(asdict(clock)) + s = copy.deepcopy(s) + s.update(refreshes=refreshes, q_ik=[refreshes * .001] * 3, + ik_target=sub(command['target_world_m'], s['root'][:3])) + yield {'kind': 'next_reference', 'state': s, 'event': { + **event, 'kind': 'next_reference', 'clock': refclock, 'command': command, + 'held_control_index': None, 'action_origin_tick': None, 'reference_next_clock': refclock}} + + +def run(p=None, trial=0, end=None, mutate=None): + p = protocol() if p is None else p + audit = TrialAudit(p, trial) + for record in records(p, trial, end): + if mutate: + mutate(record) + causes = audit.consume(record) + if causes: + break + return audit + + +def test_complete_three_cycles_and_clock_prefix(): + audit = run() + report = audit.report() + assert report['first_failure'] is None + assert report['functional_passed'] and report['passed_prefix_cycles'] == 3 + assert report['completed_clock_cycles'] == 3 + assert all(c['metrics']['phase_counts'] == [1499, 500, 300, 700, 400, 1101] for c in report['cycles']) + assert all(s['carry_pre'] == s['carry_post'] and s['post'] is not None for s in report['seams']) + assert not report['accepted_skill'] and report['optimizer_steps'] == 0 + + +def test_incomplete_tail_not_passed_and_later_gates_not_evaluated(): + report = run(end=4512).report() + assert not report['functional_passed'] and report['passed_prefix_cycles'] == 1 + assert report['completed_clock_cycles'] == 1 + assert report['cycles'][1]['gates']['landing_position'] == 'not_evaluated' + assert report['cycles'][2]['status'] == 'not_evaluated' + + +@pytest.mark.parametrize('field,value', [('root', [1.] * 13), ('q_ik', [0.] * 3), + ('sensor_delay', [[1.] * 18]), ('contact', False), + ('rng', {'changed': [4]}), ('action_delay', [[1.] * 3])]) +def test_seam_fingerprint_rejects_teleport_and_memory_reset(field, value): + def mutate(r): + if r['kind'] == 'seam': + r['state'][field] = value + audit = run(mutate=mutate) + assert audit.first_failure['global_tick'] == 4500 + assert any('recording' in c for c in audit.first_failure['causes']) + + +@pytest.mark.parametrize('kind', ['duplicate', 'skip', 'time_wrap', 'reset_counter']) +def test_clock_and_reset_faults(kind): + p = protocol() + audit = TrialAudit(p, 0) + stream = iter(records(p, end=3)) + for _ in range(3): + r = next(stream) + assert not audit.consume(r) + r = copy.deepcopy(r) if kind == 'duplicate' else next(stream) + if kind == 'skip': + r['event']['clock']['global_tick'] = 3 + elif kind == 'time_wrap': + r['state']['time'] = 0 + elif kind == 'reset_counter': + r['state']['counters']['reset_calls'] += 1 + assert audit.consume(r) + assert audit.first_failure + + +@pytest.mark.parametrize('field,value', [('normal', [0., 0., float('nan')]), ('tilt', float('inf')), + ('foot', [0., 0.]), ('q', None)]) +def test_nonfinite_and_malformed_first_offending_tick(field, value): + def mutate(r): + if r['kind'] == 'tick' and r['event']['clock']['global_tick'] == 2: + r['state'][field] = value + audit = run(end=3, mutate=mutate) + assert audit.first_failure['global_tick'] == 2 + assert not audit.report()['functional_passed'] + + +@pytest.mark.parametrize('value,passed', [(.03, True), (.03000001, False)]) +def test_exact_root_threshold(value, passed): + def mutate(r): + if r['kind'] == 'tick': + r['state']['root'][0] = value + r['state']['ik_target'][0] -= value + audit = run(end=1, mutate=mutate) + assert (audit.first_failure is None) == passed + + +def test_overlapping_failures_preserved(): + def mutate(r): + if r['kind'] == 'tick': + r['state'].update(tilt=.7, joint_excess=.02, limit_ratio=2.) + audit = run(end=1, mutate=mutate) + assert {'tilt', 'hardware_joint_limit', 'hardware_effort_velocity'} <= set(audit.first_failure['causes']) + + +@pytest.mark.parametrize('valid_ticks,passed', [(4, False), (5, True)]) +def test_lift_dwell_exact_deadline(valid_ticks, passed): + def mutate(r): + if r['kind'] == 'tick' and r['event']['command']['phase'] == 2: + t = r['event']['clock']['local_tick'] + r['state']['gap'] = .022 if t > 2260 - valid_ticks else .021 + audit = run(end=2260, mutate=mutate) + assert (audit.first_failure is None) == passed + assert audit.margins['lift_dwell'] == passed + if not passed: + assert audit.first_failure['global_tick'] == 2260 + + +def test_lift_onset_unload_not_laundered(): + def mutate(r): + if r['kind'] == 'tick' and r['event']['command']['phase'] == 2: + r['state']['onset_phase'] = 1 + audit = run(end=2260, mutate=mutate) + assert 'P1_lift_dwell' in audit.first_failure['causes'] + + +@pytest.mark.parametrize('bad_tick,passed', [(4450, True), (4451, False)]) +def test_final_reload_dwell_boundary(bad_tick, passed): + def mutate(r): + if r['kind'] == 'tick' and r['event']['clock']['global_tick'] == bad_tick: + r['state']['normal'][2] = 31.9 + audit = run(end=4500, mutate=mutate) + assert (audit.first_failure is None) == passed + assert audit.cycles[0]['P1_margins']['reload_dwell'] == passed + + +def test_support_and_unload_stop_at_window_last_sample(): + def mutate(r): + if r['kind'] == 'tick' and r['event']['command']['phase'] == 1: + r['state']['normal'][2] = 29. + audit = run(end=2100, mutate=mutate) + assert audit.first_failure['global_tick'] == 1999 + assert audit.gates['unloaded'] is True + assert audit.margins['unload_ratio'] is False + assert audit.gates['real_flight'] == 'not_evaluated' + + +def test_P2_stricter_margins_are_diagnostic_not_suppressed_functional_failures(): + def mutate(r): + if r['kind'] in ('initial', 'tick', 'next_reference'): + r['state']['root'][0] = .04 + r['state']['ik_target'][0] = r['event']['command']['target_world_m'][0] - .04 + report = run(protocol('P2'), mutate=mutate).report() + assert report['functional_passed'] + assert report['cycles'][0]['P1_margins']['root_horizontal'] is False + assert report['cycles'][0]['P1_margins_role'] == 'diagnostic_only' + + +def applied_zero(r, p, trial): + """Pre-step capture with zero envelope, used before the event should start.""" + s = r['state'] + events = [] + for i in p.trial(trial).event_indices: + program = s['event_program'][i] + events.append(dict(event_index=i, body_index=s['body_names'].index(program['body']), envelope=0., + force_world=[0.] * 3, couple_world=[0.] * 3, point_world=program['point_m'], + com_world=[0.] * 3, frame_quat_xyzw=[0., 0., 0., 1.], + point_frame_quat_xyzw=[0., 0., 0., 1.], point_frame_origin_world=[0.] * 3)) + s['applied'] = dict(format='skillfactory.contact-cycle-applied-wrench.v1', trial_index=trial, + global_tick=r['event']['clock']['global_tick'], events=events, + total_com_wrench=[[0.] * 6 for _ in s['body_names']]) + + +@pytest.mark.parametrize('fault', ['missing', 'stale', 'frame', 'moment']) +def test_required_prephysics_applied_evidence(fault): + p, trial = protocol('P2'), 4 + def mutate(r): + if r['kind'] == 'tick': + applied_zero(r, p, trial) + a = r['state']['applied'] + if fault == 'missing': + r['state']['applied'] = None + elif fault == 'stale': + a['global_tick'] = 0 + elif fault == 'frame': + a['events'][0]['frame_quat_xyzw'] = [0., 0., 1., 0.] + else: + a['total_com_wrench'][0][3] = .01 + audit = run(p, trial, end=1, mutate=mutate) + assert any('recording_applied' in c for c in audit.first_failure['causes']) + + +def test_undelivered_time_event_fails_at_first_delivery_deadline(): + p, trial = protocol('P2'), 4 + # Use a direct event-window probe so original functional windows are irrelevant. + audit = TrialAudit(p, trial) + initial = next(records(p, trial, end=0)) + assert not audit.consume(initial) + audit.event_previous['state']['time'] = 7.5 + r = next(x for x in records(p, trial, end=1) if x['kind'] == 'tick') + clock = p.evidence_clock(trial, 3751) + r['event']['clock'] = plain(asdict(clock)) + applied_zero(r, p, trial) + audit.last = r + causes = audit._events(r['state'], clock) + assert 'event_delivery:0' in causes + assert audit.delivery[0]['expected'] and audit.delivery[0]['missed'] + assert audit.delivery[0]['expected_area_s'] == 6. + + +def test_matrix_includes_all_not_run_rows_and_does_not_assume_pairing(): + result = matrix_report(protocol('P2'), []) + assert len(result['rows']) == 20 and len(result['pairs']) == 10 + assert all(r['status'] == 'not_run' for r in result['rows']) + assert all(p['status'] == 'not_evaluated' for p in result['pairs']) + + +@pytest.fixture +def recording(): + path = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_recording.py' + spec = importlib.util.spec_from_file_location('_cpu_evaluation_recording', path) + module = importlib.util.module_from_spec(spec) + sys.modules[spec.name] = module + spec.loader.exec_module(module) + return module + + +@dataclass +class Stop: + trial_index: int = 0 + physics_ticks: int = 2 + causes: tuple = ('swing_clearance',) + reason: str = 'failure' + fatal: bool = False + clock: dict = field(default_factory=lambda: {'global_tick': 2, 'cycle_index': 0, 'local_tick': 2}) + completed_controls: int = 0 + partial_control_ticks: int = 2 + completed_clock_cycles: int = 0 + all_sim_steps_attempted: int = 4 + all_sim_steps_completed: int = 4 + initialization_steps: int = 2 + rollout_steps: int = 2 + reset_calls: int = 3 + pose_write_calls: int = 1 + composer_buffer_replacements: int = 2 + + +def recorder_prefix(recording, tmp_path): + budgets = [] + recorder = recording.EvaluationRecorder(protocol(), 0, tmp_path / 'evidence', + budget_check=lambda *args: budgets.append(args), supplemental=lambda s: {}) + for r in records(protocol(), end=2): + assert not recorder._record(r) + stop = Stop() + assert not recorder.after_step(SimpleNamespace(ticks_advanced=2, observation=None, reward=None, stop=stop)) + return recorder, stop, budgets + + +def test_partial_control_durable_persistence_then_exact_ack(recording, tmp_path): + recorder, stop, budgets = recorder_prefix(recording, tmp_path) + acknowledgements = [] + def acknowledge(value): + assert (recorder.directory / 'metrics.json').is_file() + acknowledgements.append(value) + report = recorder.finalize(stop=stop, acknowledge=acknowledge) + assert acknowledgements == [stop] + assert report['first_failure']['global_tick'] == 2 + decoded = lzma.decompress((recorder.directory / TRACE_FILENAME).read_bytes()).decode() + raw = [json.loads(x) for x in decoded.splitlines()] + assert len([r for r in raw if r['kind'] == 'tick']) == 2 + assert raw[-2]['ticks_advanced'] == 2 and raw[-2]['reward'] is None + assert raw[-1]['kind'] == 'terminal' and not report['functional_passed'] + assert any(b[0] == 'finalize' for b in budgets) + + +def test_io_error_never_acknowledges(recording, tmp_path, monkeypatch): + recorder, stop, _ = recorder_prefix(recording, tmp_path) + acknowledgements = [] + def fail(_): + raise OSError('disk fault') + monkeypatch.setattr(recording.os, 'fsync', fail) + with pytest.raises(OSError, match='disk fault'): + recorder.finalize(stop=stop, acknowledge=acknowledgements.append) + assert not acknowledgements and recorder.faults + assert (recorder.directory / TRACE_FILENAME).exists() + recorder.stream.close() + + +def test_external_failure_terminal_no_fake_tick_and_no_ack(recording, tmp_path): + recorder, stop, _ = recorder_prefix(recording, tmp_path) + acknowledgements = [] + report = recorder.finalize(stop=stop, external_failure={ + 'causes': ['identity_changed'], 'fatal': True, 'context': {'stage': 'after_step'}}, + acknowledge=acknowledgements.append) + assert not acknowledgements + assert report['observed_physics_ticks'] == 2 + assert 'identity_changed' in report['first_failure']['causes'] + + +def test_nonfinite_raw_and_budget_fault_are_retained(recording, tmp_path): + recorder = recording.EvaluationRecorder(protocol(), 0, tmp_path / 'evidence', + budget_check=lambda *args: None, supplemental=lambda s: {}) + for r in records(protocol(), end=1): + if r['kind'] == 'tick': + r['state']['tilt'] = math.nan + causes = recorder._record(r) + assert causes + recorder.finalize(external_failure={'causes': ['recording_nonfinite'], 'fatal': True, 'context': {}}) + assert '"nonfinite":"nan"' in lzma.decompress((recorder.directory / TRACE_FILENAME).read_bytes()).decode() + + +def test_tensor_copy_is_independent(recording): + tensor = torch.ones(1, 3) + result = recording.copied({'x': tensor}) + tensor.zero_() + assert result == {'x': [[1., 1., 1.]]} + + +def test_incomplete_endpoint_gates_return_immediate_stop(): + p = protocol() + audit = TrialAudit(p, 0) + for record in records(p, end=4500): + if record['kind'] == 'tick' and record['event']['clock']['global_tick'] == 4500: + audit.gates['real_flight'] = 'not_evaluated' + causes = audit.consume(record) + assert 'recording_incomplete_cycle_gates' in causes + break + assert not audit.consume(record) + assert audit.first_failure['global_tick'] == 4500 + assert not audit.report()['cycles'][0]['passed'] + + +def test_no_event_P1_requires_zero_wrench_not_fabricated_event_capture(): + assert run(end=1).first_failure is None + def mutate(r): + if r['kind'] == 'tick': + r['state']['external'][0][0] = .01 + audit = run(end=1, mutate=mutate) + assert 'identity_unexpected_external_wrench' in audit.first_failure['causes'] + + +@pytest.mark.parametrize('fault', ['trial', 'point_frame', 'free_couple', 'moment_arm']) +def test_nonzero_applied_evidence_checks_frame_and_COM_moment(fault): + from SkillFactory.legs.contact_cycle_sim.evaluation_audit import cross, rotate + p, trial = protocol('P2'), 8 + audit = TrialAudit(p, trial) + r = next(x for x in records(p, trial, end=1) if x['kind'] == 'tick') + applied_zero(r, p, trial) + s = r['state'] + e = s['applied']['events'][0] + program = s['event_program'][1] + q = [0., 0., math.sqrt(.5), math.sqrt(.5)] + e.update(envelope=.5, frame_quat_xyzw=q, point_frame_quat_xyzw=q, + point_frame_origin_world=[.2, .3, .4], com_world=[.1, .2, .3]) + s['events'][1]['envelope'] = .5 + e['point_world'] = [a+b for a, b in zip(e['point_frame_origin_world'], rotate(q, program['point_m']))] + e['force_world'] = [v*.5 for v in rotate(q, program['force_N'])] + e['couple_world'] = [v*.5 for v in rotate(q, program['couple_Nm'])] + wrench = e['force_world'] + [a+b for a, b in zip(e['couple_world'], + cross(sub(e['point_world'], e['com_world']), e['force_world']))] + s['external'][e['body_index']] = wrench + s['applied']['total_com_wrench'][e['body_index']] = wrench.copy() + clock = p.evidence_clock(trial, 1) + assert audit._applied(s, clock)[1] == e + if fault == 'trial': + s['applied']['trial_index'] = 4 + elif fault == 'point_frame': + e['point_frame_quat_xyzw'] = [0., 0., 0., 1.] + elif fault == 'free_couple': + e['couple_world'][0] += .01 + else: + e['com_world'][0] += .01 + with pytest.raises(ValueError, match='applied'): + audit._applied(s, clock) + + +def test_trigger_provenance_rejects_fabricated_early_fire(): + p, trial = protocol('P2'), 4 + def mutate(r): + if r['kind'] == 'tick': + applied_zero(r, p, trial) + r['state']['events'][0]['fired'] = True + audit = run(p, trial, end=1, mutate=mutate) + assert 'recording_event_trigger_or_envelope:0' in audit.first_failure['causes'] + + +def frozen_schedule_mutator(p, trial, *, edge_tick=None, inspect=None): + from SkillFactory.legs.contact_cycle.mechanics import WrenchSchedule + from SkillFactory.legs.contact_cycle_sim.evaluation_audit import cross + schedule = WrenchSchedule(plain(p.document['events']), 1, 'cpu') + def mutate(r): + if r['kind'] != 'tick': + return + s = r['state'] + g = r['event']['clock']['global_tick'] + clock = p.next_command_clock(trial, 0) if g == 1 else p.evidence_clock(trial, g-1) + command = p.command(trial, clock, anchor_a_world=s['anchors']['anchor_a_world'], + root_origin_world=s['anchors']['root_origin_world'], weight_N=40.) + # Match the actual frozen runtime: integer substeps multiplied by dt + # in float32, NOT a Python double product subsequently cast to float32. + time = torch.tensor([g-1]) * .002 + s['time'] = float((torch.tensor([g]) * .002)[0]) + s['liftoff'] = g == edge_tick and s['scenario'] == 2 + s['touchdown'] = g == edge_tick and s['scenario'] == 3 + s['edge_phase'] = 2 if s['liftoff'] else -1 + schedule.step(time, torch.tensor([command.phase]), + torch.tensor([g-1 == edge_tick and s['scenario'] == 2]), + torch.tensor([g-1 == edge_tick and s['scenario'] == 3]), + torch.tensor([s['scenario']]), .002) + if inspect: + inspect(g, time, schedule) + for i, event in enumerate(s['events']): + event.update(fired=bool(schedule.fired[0, i]), completed=bool(schedule.completed[0, i]), + start=float(schedule.start[0, i]), envelope=float(schedule.envelope[0, i]), + integral=float(schedule.integral[0, i])) + applied_zero(r, p, trial) + for e in s['applied']['events']: + i, body = e['event_index'], e['body_index'] + e['envelope'] = s['events'][i]['envelope'] + program = s['event_program'][i] + e['force_world'] = [v*e['envelope'] for v in program['force_N']] + e['couple_world'] = [v*e['envelope'] for v in program['couple_Nm']] + wrench = e['force_world'] + [a+b for a, b in zip(e['couple_world'], + cross(sub(e['point_world'], e['com_world']), e['force_world']))] + s['external'][body] = wrench + s['applied']['total_com_wrench'][body] = wrench.copy() + return mutate + + +def test_frozen_time_schedule_complete_delivery_and_impulses(): + p, trial = protocol('P2'), 4 + report = run(p, trial, mutate=frozen_schedule_mutator(p, trial)).report() + assert report['first_failure'] is None + assert report['functional_passed'] and report['protocol_complete'] + event = report['delivery'][0] + assert event['status'] is True and event['actual_applied_evidence_available'] + assert event['force_impulse_Ns'] == pytest.approx([18., -24., -12.], abs=1e-4) + assert event['couple_impulse_Nms'] == pytest.approx([1.2, .6, -.6], abs=1e-5) + + +@pytest.mark.parametrize('fault', ['partial_reward', 'next_observation', 'duplicate_step', 'terminal_counters']) +def test_partial_and_terminal_records_fail_closed(recording, tmp_path, fault): + recorder, stop, _ = recorder_prefix(recording, tmp_path) + if fault == 'terminal_counters': + stop.all_sim_steps_completed += 1 + else: + bad = copy.deepcopy(recorder.audit.step_results[-1]) + if fault == 'partial_reward': + bad['reward'] = [1.] + elif fault == 'next_observation': + bad['has_next_observation'] = True + assert recorder.audit.consume(bad) + acknowledgements = [] + report = recorder.finalize(stop=stop, acknowledge=acknowledgements.append) + assert not acknowledgements + assert any(c.startswith('recording') for c in report['all_causes']) + + +def test_recording_fault_after_earlier_failure_is_not_hidden(recording, tmp_path): + recorder, stop, _ = recorder_prefix(recording, tmp_path) + recorder.audit.fail(('recording_late_fault',), kind='terminal', tick=3) + acknowledgements = [] + report = recorder.finalize(stop=stop, acknowledge=acknowledgements.append) + assert report['first_failure']['global_tick'] == 2 + assert 'recording_late_fault' in report['all_causes'] + assert not acknowledgements + + +def test_capture_fault_reports_actual_tick_without_fabricating_sample(recording, tmp_path, monkeypatch): + recorder = recording.EvaluationRecorder(protocol(), 0, tmp_path / 'evidence', + budget_check=lambda *args: None, supplemental=lambda s: {}) + for record in records(protocol(), end=1): + assert not recorder._record(record) + @dataclass + class Event: + kind: str = 'tick' + clock: dict = field(default_factory=lambda: {'global_tick': 2, 'cycle_index': 0, 'local_tick': 2}) + causes: tuple = ('hardware_joint_limit',) + def broken(*args): + raise ValueError('accessor failed') + monkeypatch.setattr(recording, 'snapshot', broken) + assert recorder(Event(), None) + report = recorder.finalize(external_failure={'causes': ['recording_capture'], 'fatal': True, 'context': {}}) + assert report['first_failure']['global_tick'] == 2 + assert report['observed_physics_ticks'] == 1 + assert 'hardware_joint_limit' in report['first_failure']['causes'] + assert 'capture_fault' in lzma.decompress((recorder.directory / TRACE_FILENAME).read_bytes()).decode() + + +def test_offline_reaudit_matches_live_partial_report(recording, tmp_path): + import subprocess + recorder, stop, _ = recorder_prefix(recording, tmp_path) + live = recorder.finalize(stop=stop) + output = tmp_path / 'offline.json' + result = subprocess.run([sys.executable, str(ROOT / 'scripts/audit_cycle_sim_evaluation.py'), + '--protocol', str(ROOT / 'configs/contact_cycle/continuous_eval_p1_v1.json'), '--trial-index', '0', + '--trace', str(recorder.directory / TRACE_FILENAME), '--output', str(output), + '--max-trace-bytes', '1048576', '--wall-seconds', '20'], capture_output=True, text=True) + assert result.returncode == 1, result.stderr + offline = json.loads(output.read_text()) + for key in ('first_failure', 'all_causes', 'cycles', 'functional_passed', 'protocol_complete', 'terminal'): + assert offline[key] == live[key] + assert offline['new_simulation_steps'] == offline['new_optimizer_steps'] == 0 + + +def test_matrix_mismatched_pair_never_publishes_controller_delta(): + p = protocol('P2') + rows = [{'trial': plain(asdict(p.trial(i))), 'initial_pair_state': {'anchor': i}, + 'functional_passed': True} for i in (0, 1)] + pair = matrix_report(p, rows)['pairs'][0] + assert pair['status'] == 'invalid_initial_pair' + assert pair['initial_exactly_equal'] is False + assert pair['both_functional_passed'] is False + assert pair['learned_minus_reference'] == 'not_evaluated' + + +def test_matrix_absent_pair_snapshots_are_not_equal_evidence(): + p = protocol('P2') + rows = [{'trial': plain(asdict(p.trial(i))), 'initial_pair_state': None} for i in (0, 1)] + report = matrix_report(p, rows) + assert report['pairs'][0]['status'] == 'not_evaluated' + + +def test_xz_lossless_members_and_exact_inclusive_byte_accounting(recording, tmp_path): + recorder = recording.EvaluationRecorder(protocol(), 0, tmp_path / 'evidence', + budget_check=lambda *args: None, supplemental=lambda s: {}) + original = list(records(protocol(), end=2)) + for record in original: + assert not recorder._record(record) + assert (recorder.directory / TRACE_FILENAME).stat().st_size == recorder.written_bytes + # The initial is a checkpoint; control/ticks remain bounded and uncommitted. + assert len(recorder.pending) == len(original) - 1 + assert [json.loads(x) for x in lzma.decompress( + (recorder.directory / TRACE_FILENAME).read_bytes()).splitlines()] == original[:1] + recorder._flush() + assert not recorder.pending and recorder.pending_raw_bytes == 0 + decoded = lzma.decompress((recorder.directory / TRACE_FILENAME).read_bytes()).splitlines() + assert [json.loads(line) for line in decoded] == original + recorder.stream.close() + + +@pytest.mark.parametrize('stage', ['record', 'terminal_member', 'metrics', 'close', 'compression']) +def test_xz_storage_failures_preserve_complete_prefix_never_ack(recording, tmp_path, monkeypatch, stage): + recorder, stop, _ = recorder_prefix(recording, tmp_path) + path = recorder.directory / TRACE_FILENAME + prefix = path.read_bytes() + before = [json.loads(line) for line in lzma.decompress(prefix).splitlines()] + finalize_calls = 0 + def budget(where, pending, written): + nonlocal finalize_calls + assert written == path.stat().st_size + if where == 'finalize': + finalize_calls += 1 + fail = ((stage == 'record' and where == 'record') + or (stage == 'terminal_member' and finalize_calls == 2) + or (stage == 'metrics' and finalize_calls == 3)) + if fail: + # Deny by one byte: the pending count must INCLUDE XZ header/check/footer. + assert pending > 18 + raise OSError('aggregate byte budget includes complete compressed member') + recorder.budget_check = budget + if stage == 'compression': + def broken_compression(*args, **kwargs): + raise OSError('compression fault') + monkeypatch.setattr(recording.lzma, 'compress', broken_compression) + if stage == 'close': + stream = recorder.stream + class CloseFault: + def __getattr__(self, key): + return getattr(stream, key) + def close(self): + raise OSError('close fault') + recorder.stream = CloseFault() + acknowledgements = [] + if stage == 'record': + assert recorder.after_step(SimpleNamespace(ticks_advanced=0, observation=None, reward=None, stop=stop)) + recorder.finalize(stop=stop, acknowledge=acknowledgements.append) + else: + with pytest.raises(OSError): + recorder.finalize(stop=stop, acknowledge=acknowledgements.append) + assert not acknowledgements and recorder.faults + decoded = [json.loads(line) for line in lzma.decompress(path.read_bytes()).splitlines()] + assert decoded[:len(before)] == before + if stage == 'close': + stream.close() + else: + recorder.stream.close() + + +@pytest.mark.parametrize('fault', ['truncated', 'corrupt', 'oversized', 'missing_terminal']) +def test_offline_xz_rejects_damaged_or_incomplete_tail(recording, tmp_path, fault): + import subprocess + recorder, stop, _ = recorder_prefix(recording, tmp_path) + trace = recorder.directory / TRACE_FILENAME + if fault == 'missing_terminal': + recorder.stream.close() + else: + recorder.finalize(stop=stop) + data = trace.read_bytes() + if fault == 'truncated': + trace.write_bytes(data[:-4]) + elif fault == 'corrupt': + trace.write_bytes(data[:-8] + bytes([data[-8] ^ 1]) + data[-7:]) + elif fault == 'oversized': + trace.write_bytes(data + lzma.compress(b'x' * (1024 * 1024 + 1) + b'\n')) + output = tmp_path / 'offline.json' + result = subprocess.run([sys.executable, str(ROOT / 'scripts/audit_cycle_sim_evaluation.py'), + '--protocol', str(ROOT / 'configs/contact_cycle/continuous_eval_p1_v1.json'), '--trial-index', '0', + '--trace', str(trace), '--output', str(output), '--max-trace-bytes', '1048576', + '--wall-seconds', '20'], capture_output=True, text=True) + assert result.returncode == 1, result.stderr + report = json.loads(output.read_text()) + assert not report['functional_passed'] + assert any(c.startswith(('recording_offline', 'recording_missing_terminal')) for c in report['all_causes']) + assert report['observed_physics_ticks'] == 2 + + +@pytest.mark.parametrize('value', [None, [], 'not a state']) +def test_malformed_state_container_is_a_recorded_failure(value): + def mutate(r): + if r['kind'] == 'tick': + r['state'] = value + audit = run(end=1, mutate=mutate) + assert 'recording_invalid:state_not_object' in audit.first_failure['causes'] + assert audit.first_failure['global_tick'] == 1 + + +def test_protocol_complete_is_not_a_passing_final_endpoint(): + def mutate(r): + if r['kind'] == 'tick' and r['event']['clock']['global_tick'] == 13500: + r['state']['tilt'] = .7 + report = run(mutate=mutate).report() + assert report['protocol_complete'] and report['completed_clock_cycles'] == 3 + assert report['passed_prefix_cycles'] == 2 and not report['functional_passed'] + assert report['first_failure']['global_tick'] == 13500 + + +def test_xz_short_write_retains_readable_complete_record_prefix(recording, tmp_path): + recorder, stop, _ = recorder_prefix(recording, tmp_path) + path = recorder.directory / TRACE_FILENAME + prefix = path.read_bytes() + stream = recorder.stream + class ShortWrite: + def __getattr__(self, key): + return getattr(stream, key) + def write(self, data): + return stream.write(data[:len(data)//2]) + recorder.stream = ShortWrite() + acknowledgements = [] + with pytest.raises(OSError, match='Short evidence write'): + recorder.finalize(stop=stop, acknowledge=acknowledgements.append) + assert not acknowledgements + assert path.read_bytes().startswith(prefix) + expected = lzma.decompress(prefix).splitlines() + with lzma.open(path, 'rb') as reader: + assert [reader.readline().rstrip(b'\n') for _ in expected] == expected + with pytest.raises(EOFError): + reader.read() + stream.close() + + +def test_frozen_phase_schedule_float32_completion_uses_actual_start(): + p, trial = protocol('P2'), 12 + seen = [] + def inspect(g, time, schedule): + if g == 3351: + start = float(schedule.start[0, 2]) + seen.append((start, float(time[0]), float(time[0] - schedule.start[0, 2]))) + assert not schedule.completed[0, 2] + audit = run(p, trial, mutate=frozen_schedule_mutator(p, trial, edge_tick=3399, inspect=inspect)) + assert seen == [pytest.approx((6.1000003815, 6.7000002861, .5999999046), abs=1e-10)] + assert audit.first_failure is None + assert audit.report()['functional_passed'] + assert all(audit.delivery[i]['status'] is True for i in (2, 3)) + + +@pytest.mark.parametrize('fault', ['late_completion', 'area']) +def test_phase_schedule_completion_slack_does_not_relax_delivery(fault): + p, trial = protocol('P2'), 12 + schedule = frozen_schedule_mutator(p, trial) + def mutate(r): + schedule(r) + if r['kind'] == 'tick' and r['event']['clock']['global_tick'] == 3352: + event = r['state']['events'][2] + if fault == 'late_completion': + event['completed'] = False + else: + event['integral'] -= .003 + audit = run(p, trial, mutate=mutate) + assert audit.first_failure['global_tick'] == 3352 + assert 'event_delivery:2' in audit.first_failure['causes'] + + +@pytest.mark.parametrize('trial,event,closure', [(12, 3, 3400), (8, 1, 2300)]) +@pytest.mark.parametrize('edge_offset', [-1, 0, 1, None]) +def test_edge_trigger_phase_closure_respects_last_pre_step(trial, event, closure, edge_offset): + p = protocol('P2') + edge_tick = None if edge_offset is None else closure + edge_offset + audit = run(p, trial, mutate=frozen_schedule_mutator(p, trial, edge_tick=edge_tick)) + if edge_offset == -1: + assert audit.first_failure is None + assert audit.delivery[event]['status'] is True + assert audit.delivery[event]['absolute_start_s'] >= (closure - 1) * .002 + else: + # Includes boundary/reload-first landing (or swing-first liftoff), + # which cannot be consumed by a scheduler still in the eligible phase. + assert audit.first_failure['global_tick'] == closure + assert audit.first_failure['causes'] == ['event_delivery:' + str(event)] + assert audit.delivery[event]['missed'] + + +def test_delayed_liftoff_is_not_a_missing_trigger_at_phase_closure(): + p, trial = protocol('P2'), 8 + audit = run(p, trial, end=2300, mutate=frozen_schedule_mutator(p, trial, edge_tick=2299)) + assert audit.first_failure is None + event = audit.delivery[1] + assert event['fired'] and not event['completed'] and not event['missed'] + assert event['absolute_start_s'] > 4.6 + assert event['status'] == 'not_evaluated' + + +def test_recorder_requires_existing_parent_without_creating_ancestors(recording, tmp_path): + directory = tmp_path / 'missing' / 'nested' / 'trial' + with pytest.raises(FileNotFoundError): + recording.EvaluationRecorder(protocol(), 0, directory, + budget_check=lambda *args: None, supplemental=lambda s: {}) + assert not (tmp_path / 'missing').exists() + + +@pytest.mark.parametrize('fail_at', [None, 3, 4]) +def test_recorder_directory_and_parent_fsync_before_ack(recording, tmp_path, monkeypatch, fail_at): + import os + import stat + recorder, stop, _ = recorder_prefix(recording, tmp_path) + fsync, calls, acknowledgements = os.fsync, [], [] + def sync(fd): + calls.append((os.fstat(fd).st_ino, stat.S_ISDIR(os.fstat(fd).st_mode))) + assert not acknowledgements + if len(calls) == fail_at: + raise OSError('directory fsync failed') + fsync(fd) + monkeypatch.setattr(recording.os, 'fsync', sync) + if fail_at: + with pytest.raises(OSError, match='directory fsync failed'): + recorder.finalize(stop=stop, acknowledge=acknowledgements.append) + assert not acknowledgements and not recorder.finalized and recorder.faults + recorder.stream.close() + else: + recorder.finalize(stop=stop, acknowledge=acknowledgements.append) + assert acknowledgements == [stop] and recorder.stream.closed + assert calls[2] == (recorder.directory.stat().st_ino, True) + if fail_at != 3: + assert calls[3] == (recorder.directory.parent.stat().st_ino, True) + + +@pytest.fixture +def offline_reader(): + spec = importlib.util.spec_from_file_location('_cpu_offline_reader', ROOT / 'scripts/audit_cycle_sim_evaluation.py') + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def gzip_processing_tail(kind): + if kind == 'empty_members': + return gzip.compress(b'') * 10000 + if kind == 'oversized_header': + member = gzip.compress(b'') + return member[:3] + b'\x08' + member[4:10] + b'n' * (1024 * 1024) + b'\0' + member[10:] + if kind == 'empty_blocks': + # Valid deflate stream that consumes compressed bytes without output. + return gzip.compress(b'')[:10] + b'\x00\x00\x00\xff\xff' * 200000 + b'\x03\x00' + b'\0' * 8 + raise AssertionError(kind) + + +@pytest.mark.parametrize('tail', ['empty_members', 'oversized_header', 'empty_blocks']) +def test_offline_gzip_budget_during_no_output_processing(offline_reader, tmp_path, monkeypatch, tail): + path = tmp_path / 'trace.v1.jsonl.gz' + terminal = b'{"kind":"terminal"}\n' + path.write_bytes(gzip.compress(terminal) + gzip_processing_tail(tail)) + now = 0 + def monotonic(): + nonlocal now + now += 1 + return now + monkeypatch.setattr(offline_reader.time, 'monotonic', monotonic) + lines = offline_reader.trace_lines(path, compressed=True, max_bytes=2**21, + max_record_bytes=2**20, deadline=12) + assert next(lines) == terminal + with pytest.raises(ValueError, match='budget_audit'): + list(lines) + assert now == 13 # Rejection inside processing, not merely after decoded output. + + +@pytest.mark.parametrize('compressed', [False, True]) +def test_offline_budget_checked_at_eof(offline_reader, tmp_path, monkeypatch, compressed): + path = tmp_path / 'trace' + raw = b'{"kind":"terminal"}\n' + path.write_bytes(gzip.compress(raw) if compressed else raw) + monkeypatch.setattr(offline_reader.time, 'monotonic', lambda: 0) + lines = offline_reader.trace_lines(path, compressed=compressed, max_bytes=4096, + max_record_bytes=4096, deadline=1) + assert next(lines) == raw + monkeypatch.setattr(offline_reader.time, 'monotonic', lambda: 2) + with pytest.raises(ValueError, match='budget_audit'): + next(lines) + + +def test_offline_gzip_expansion_record_limit(offline_reader, tmp_path): + path = tmp_path / 'trace.v1.jsonl.gz' + path.write_bytes(gzip.compress(b'x' * (2**20 + 1))) + with pytest.raises(ValueError, match='budget_audit'): + list(offline_reader.trace_lines(path, compressed=True, max_bytes=2**20, + max_record_bytes=2**20, deadline=math.inf)) + + +def test_offline_gzip_decoder_no_progress_rejected(offline_reader, tmp_path, monkeypatch): + path = tmp_path / 'trace.v1.jsonl.gz' + path.write_bytes(gzip.compress(b'')) + class StalledDecoder: + eof = False + def decompress(self, data, limit): + self.unconsumed_tail = data + return b'' + monkeypatch.setattr(offline_reader.zlib, 'decompressobj', lambda **kwargs: StalledDecoder()) + with pytest.raises(ValueError, match='no progress'): + list(offline_reader.trace_lines(path, compressed=True, max_bytes=4096, + max_record_bytes=4096, deadline=math.inf)) + + +@pytest.mark.parametrize('tail', ['empty_members', 'oversized_header', 'empty_blocks']) +def test_offline_cli_budget_failure_after_terminal_is_reported( + recording, offline_reader, tmp_path, monkeypatch, tail): + recorder, stop, _ = recorder_prefix(recording, tmp_path) + recorder.finalize(stop=stop) + trace = tmp_path / 'legacy.jsonl.gz' + trace.write_bytes(gzip.compress(lzma.decompress((recorder.directory / TRACE_FILENAME).read_bytes()))) + with trace.open('ab') as output: + output.write(gzip_processing_tail(tail)) + protocol_module, audit_module = offline_reader.cpu_modules() + audit = audit_module.TrialAudit(protocol_module.load_protocol( + ROOT / 'configs/contact_cycle/continuous_eval_p1_v1.json'), 0) + monkeypatch.setattr(audit_module, 'TrialAudit', lambda *args: audit) + monkeypatch.setattr(offline_reader, 'cpu_modules', lambda: (protocol_module, audit_module)) + now = 0 + def monotonic(): + nonlocal now + if audit.terminal is not None: + now += 1 + return now + monkeypatch.setattr(offline_reader.time, 'monotonic', monotonic) + output = tmp_path / 'report.json' + monkeypatch.setattr(sys, 'argv', ['audit', '--protocol', + str(ROOT / 'configs/contact_cycle/continuous_eval_p1_v1.json'), '--trial-index', '0', + '--trace', str(trace), '--output', str(output), '--max-trace-bytes', str(2**21), '--wall-seconds', '4']) + assert offline_reader.main() == 1 + report = json.loads(output.read_text()) + assert report['terminal'] is not None and not report['functional_passed'] + assert 'recording_offline:budget_audit_resource_or_record_limit' in report['all_causes'] + assert now == 5 + + +@pytest.mark.parametrize('codec', ['gzip', 'xz']) +def test_offline_passing_terminal_cannot_hide_empty_member_budget_overrun(offline_reader, tmp_path, monkeypatch, codec): + p = protocol('P2') + trace = tmp_path / ('trace.v1.jsonl.gz' if codec == 'gzip' else TRACE_FILENAME) + encode = (lambda raw: gzip.compress(raw, compresslevel=1)) if codec == 'gzip' else ( + lambda raw: lzma.compress(raw, preset=0)) + stop = plain(asdict(Stop(physics_ticks=4500, causes=(), reason='protocol_complete', + clock=plain(asdict(p.evidence_clock(0, 4500))), completed_controls=900, + partial_control_ticks=0, completed_clock_cycles=1, all_sim_steps_attempted=4502, + all_sim_steps_completed=4502, rollout_steps=4500, composer_buffer_replacements=4500))) + with trace.open('wb') as output: + def write(record): + output.write(encode((json.dumps(record) + '\n').encode())) + for record in records(p): + write(record) + if record['kind'] == 'tick' and record['state']['substeps'] % 5 == 0: + g = record['state']['substeps'] + write({'kind': 'step_result', 'ticks_advanced': 5, 'observed_tick': g, + 'reward': [0.], 'has_next_observation': g != 4500, 'stop': stop if g == 4500 else None}) + write({'kind': 'terminal', 'runtime_stop': stop, 'external_failure': None, 'recording_faults': []}) + output.write(encode(b'') * 10000) + protocol_module, audit_module = offline_reader.cpu_modules() + audit = audit_module.TrialAudit(p, 0) + monkeypatch.setattr(audit_module, 'TrialAudit', lambda *args: audit) + monkeypatch.setattr(offline_reader, 'cpu_modules', lambda: (protocol_module, audit_module)) + now = 0 + def monotonic(): + nonlocal now + if audit.terminal is not None: + assert audit.report()['functional_passed'] # Passing until budget expiry is recorded. + now += 1 + return now + monkeypatch.setattr(offline_reader.time, 'monotonic', monotonic) + output = tmp_path / 'report.json' + monkeypatch.setattr(sys, 'argv', ['audit', '--protocol', + str(ROOT / 'configs/contact_cycle/bounded_diagnostic_p2_v1.json'), '--trial-index', '0', + '--trace', str(trace), '--output', str(output), '--max-trace-bytes', str(2**26), '--wall-seconds', '4']) + assert offline_reader.main() == 1 + report = json.loads(output.read_text()) + assert not report['functional_passed'] + assert report['first_failure']['causes'] == ['recording_offline:budget_audit_resource_or_record_limit'] + + +def test_offline_gzip_bounded_chunks_are_lossless(offline_reader, tmp_path): + path = tmp_path / 'trace.v1.jsonl.gz' + lines = [b'a' * (2**20 - 1) + b'\n', b'b\n', b'c' * (2**17) + b'\n'] + # Deliberately span record/member boundaries, including empty members. + raw = b''.join(lines) + path.write_bytes(gzip.compress(raw[:300000]) + gzip.compress(b'') + gzip.compress(raw[300000:])) + assert list(offline_reader.trace_lines(path, compressed=True, max_bytes=2**20, + max_record_bytes=2**20, deadline=math.inf)) == lines diff --git a/tests/test_contact_cycle_sim_evaluation_binding.py b/tests/test_contact_cycle_sim_evaluation_binding.py new file mode 100644 index 0000000..dc01d3c --- /dev/null +++ b/tests/test_contact_cycle_sim_evaluation_binding.py @@ -0,0 +1,1154 @@ +"""CPU file-backed synthetic identities; never imports Isaac or loads a policy.""" +import ast +import copy +import importlib.util +import json +import sys +from pathlib import Path +from types import SimpleNamespace + +import pytest + +ROOT = Path(__file__).resolve().parents[1] +SOURCES = ROOT / "source/SkillFactory/SkillFactory/legs/contact_cycle_sim" + + +def load(name, path, monkeypatch=None): + spec = importlib.util.spec_from_file_location(name, path) + module = importlib.util.module_from_spec(spec) + if monkeypatch: + monkeypatch.setitem(sys.modules, name, module) + else: + sys.modules[name] = module + spec.loader.exec_module(module) + return module + + +# A real file-backed private test package, not a forged SkillFactory.__file__. +PACKAGE = "_evaluation_binding_cpu" +load(PACKAGE, SOURCES / "__init__.py") +binding = load(PACKAGE + ".evaluation_binding", SOURCES / "evaluation_binding.py") +replay = load(PACKAGE + ".replay", SOURCES / "replay.py") + +CFG_NAME = "SkillFactory.tasks.manager_based.contact_cycle_sim.evaluation_cfg" +CFG = '''from dataclasses import dataclass, field +from typing import ClassVar +from isaaclab.scene.interactive_scene_cfg import InteractiveSceneCfg +from isaaclab.assets.asset_base_cfg import AssetBaseCfg +from isaaclab.assets.articulation.articulation_cfg import ArticulationCfg +from isaaclab.sensors.contact_sensor.contact_sensor_cfg import ContactSensorCfg +from isaaclab.sensors.joint_wrench.joint_wrench_sensor_cfg import JointWrenchSensorCfg +from isaaclab.sim.spawners.from_files.from_files_cfg import UsdFileCfg +from isaaclab.sim.spawners.lights.lights_cfg import DomeLightCfg + +@dataclass +class LegSceneCfg(InteractiveSceneCfg): + ground: AssetBaseCfg = field(default_factory=lambda: AssetBaseCfg('/World/ground', UsdFileCfg())) + light: AssetBaseCfg = field(default_factory=lambda: AssetBaseCfg('/World/light', DomeLightCfg())) + robot: ArticulationCfg = field(default_factory=lambda: ArticulationCfg('{ENV_REGEX_NS}/Robot', UsdFileCfg())) + foot_contact: ContactSensorCfg = field(default_factory=lambda: ContactSensorCfg('{ENV_REGEX_NS}/Robot/foot')) + +@dataclass +class LoadedSceneCfg(LegSceneCfg): + mount_wrench: JointWrenchSensorCfg = field(default_factory=JointWrenchSensorCfg) + +def reward(): + return 1 + +@dataclass +class EvaluationContactCycleEnvCfg: + _usd_field_exceptions: ClassVar[dict] = {"physics": "original"} + seed: int = 93501 + sim: dict = field(default_factory=lambda: {"dt": .002, "physx": {"solver": 4}}) + scene: LoadedSceneCfg = field(default_factory=LoadedSceneCfg) + actions: dict = field(default_factory=lambda: {"scale": .5}) + rewards: dict = field(default_factory=lambda: {"task": reward}) + terminations: dict = field(default_factory=lambda: {"timeout": 9.}) +''' + + +SCENE_BASE = """from dataclasses import dataclass +@dataclass +class InteractiveSceneCfg: + num_envs: int = 1 + env_spacing: float = 2. + lazy_sensor_update: bool = False + replicate_physics: bool = True + filter_collisions: bool = True + clone_in_fabric: bool = False +""" +ASSET = """from dataclasses import dataclass +@dataclass +class AssetBaseCfg: + prim_path: str + spawn: object = None + collision_group: int = 0 +""" +SPAWN = """from dataclasses import dataclass +@dataclass +class UsdFileCfg: + spawn_path: str | None = None +""" +WRENCH = """from dataclasses import dataclass +@dataclass +class JointWrenchSensorCfg: + class_type: str = 'isaaclab.sensors.joint_wrench.joint_wrench_sensor:JointWrenchSensor' + prim_path: str = '{ENV_REGEX_NS}/Robot' + update_period: float = 0.0 + debug_vis: bool = False + convention: str = 'incoming_joint_frame' +""" +SCENE_MODULES = { + "isaaclab.assets.asset_base_cfg": ASSET, + "isaaclab.assets.articulation.articulation_cfg": + ASSET.replace("AssetBaseCfg", "ArticulationCfg"), + "isaaclab.sensors.contact_sensor.contact_sensor_cfg": """from dataclasses import dataclass, field +@dataclass +class ContactSensorCfg: + prim_path: str + filter_prim_paths_expr: list = field(default_factory=lambda: ['/World/ground/GroundPlane/CollisionPlane']) + sensor_shape_prim_expr: list = field(default_factory=list) + filter_shape_prim_expr: list | None = None + collision_group: int = 0 +""", + "isaaclab.sim.spawners.from_files.from_files_cfg": SPAWN, + "isaaclab.sim.spawners.lights.lights_cfg": SPAWN.replace("UsdFileCfg", "DomeLightCfg"), +} + + +def numerical_inventory(tmp_path, monkeypatch): + inventory = {"scope": binding.INVENTORY_SCOPE, "modules": {}, "artifacts": {}} + for name in ("torch", "torch._C", "torch.random", "numpy", "numpy.random", "numpy.random.mtrand", + "numpy.core._multiarray_umath", "warp", "warp._src.context"): + path = tmp_path / "site" / name.replace(".", "/") / "__init__.py" + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("# synthetic external module\n") + load(name, path, monkeypatch) + inventory["modules"][name] = {"path": str(path), "sha256": binding.sha256(path.read_bytes())} + for owner, directories in {"torch": ("torch/lib",), "numpy": ("numpy/core", "numpy/random", "numpy/linalg", + "numpy/fft", "numpy.libs"), "warp": ("warp/bin",)}.items(): + for directory in directories: + path = tmp_path / "site" / directory / "synthetic.so" + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(b"not-an-executable-shared-library") + inventory["artifacts"][directory] = {"module": owner, "path": str(path), + "sha256": binding.sha256(path.read_bytes()), + "purpose": "synthetic test native dependency"} + monkeypatch.setattr(binding, "REQUIRED_RUNTIME", {n: ("synthetic-core", r["path"], r["sha256"]) + for n, r in inventory["modules"].items()}) + monkeypatch.setattr(binding, "RUNTIME_LAYOUT_PINS", {}) + return inventory + + +@pytest.fixture +def fixture(tmp_path, monkeypatch): + # Isolate synthetic file-backed modules from optional registration bootstraps + # used by the other CPU suites. Restore every entry after this test. + for name in tuple(sys.modules): + if name.split(".")[0] in ("SkillFactory", "isaaclab", "isaaclab_physx", "torch", "numpy", "warp"): + monkeypatch.delitem(sys.modules, name) + root = tmp_path / "project" + root.mkdir() + for relative in binding.FIXED_SOURCES: + path = root / relative + path.parent.mkdir(parents=True, exist_ok=True) + if relative.endswith(".json"): + path.write_bytes((ROOT / relative).read_bytes()) + else: + path.write_text(CFG if relative.endswith("evaluation_cfg.py") else "# synthetic fixture\n") + if relative.startswith(binding.PACKAGE) and relative.endswith(".py"): + name = relative.removeprefix("source/SkillFactory/").removesuffix(".py").replace("/", ".") + if name != CFG_NAME: + load(name, path, monkeypatch) + runner = root / "runner.py" + runner.write_text("def run():\n return None\n") + instrumentation = root / "instrumentation.py" + instrumentation.write_text("def capture():\n return None\n") + load("synthetic_runner", runner, monkeypatch) + load("synthetic_instrumentation", instrumentation, monkeypatch) + isaac = tmp_path / "IsaacLab" + pins = {} + for name, (relative, _) in binding.ISAAC_PINS.items(): + path = isaac / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(SCENE_BASE if name == "isaaclab.scene.interactive_scene_cfg" + else WRENCH if name == "isaaclab.sensors.joint_wrench.joint_wrench_sensor_cfg" + else "# synthetic pinned dependency\n") + pins[name] = (relative, binding.sha256(path.read_bytes())) + load(name, path, monkeypatch) + monkeypatch.setattr(binding, "ISAAC_ROOT", isaac) + monkeypatch.setattr(binding, "ISAAC_PINS", pins) + for name, text in SCENE_MODULES.items(): + path = isaac / ("source/isaaclab/" + name.replace(".", "/") + ".py") + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(text) + load(name, path, monkeypatch) + load(CFG_NAME, root / binding.PACKAGE / "tasks/manager_based/contact_cycle_sim/evaluation_cfg.py", monkeypatch) + inventory = numerical_inventory(tmp_path, monkeypatch) + frozen_calls = [] + + def verify(root): + frozen_calls.append(root) + return ({"task_id": "frozen-task", "contract": "frozen-contract", "features": "46D"}, + {"candidate_path": str(root / "candidate.pt"), "candidate_sha256": "a" * 64, + "manifest_sha256": "b" * 64, "live_source_exceptions": []}) + + monkeypatch.setattr(replay, "verify_bundle", verify) + cfg = sys.modules[CFG_NAME].EvaluationContactCycleEnvCfg() + binding.normalize_config(cfg) + args = dict(dependency_inventory=inventory, root=root, + protocol_path="configs/contact_cycle/continuous_eval_p1_v1.json", + effective_config=cfg, trial_index=0, + future_sources={"runner": ("runner.py",), "instrumentation": ("instrumentation.py",)}, + declared_overrides={"seed": "protocol.trial.seed"}) + return SimpleNamespace(args=args, cfg=cfg, root=root, calls=frozen_calls, monkeypatch=monkeypatch) + + +def prepare(fixture): + return binding.prepare_binding(**fixture.args) + + +def verify(fixture, manifest, **changes): + args = {**fixture.args, **changes} + return binding.verify_binding(manifest, trusted_manifest_sha256=binding.sha256(manifest), **args) + + +def test_exact_canonical_identity_separate_task_policy_and_no_grant(fixture): + manifest = prepare(fixture) + assert type(manifest) is bytes + document = json.loads(manifest) + assert binding.canonical_json(document) == manifest + assert verify(fixture, manifest) == binding.sha256(manifest) + assert len(fixture.calls) == 4 # unchanged frozen delegation on both snapshots + assert document["evaluation_id"] != document["frozen_policy"]["task_id"] + assert document["execution_authorized"] is document["accepted_skill"] is False + assert document["optimizer_steps"] == 0 + assert document["quaternion_convention"] == "xyzw" + assert document["protocol"]["sha256"] == binding.sha256( + (fixture.root / fixture.args["protocol_path"]).read_bytes()) + assert document["trial_seeds"] == {"0": 93501, "1": 93501} + assert document["effective_config"]["fields"]["mapping"]["seed"] == {"override": "protocol.trial.seed"} + assert document["sources"]["runner"][0]["module"] == "synthetic_runner" + + +@pytest.mark.parametrize("field", ["sim", "scene", "actions", "rewards", "terminations"]) +def test_every_effective_config_field_is_bound(fixture, field): + manifest = prepare(fixture) + if field == "scene": + fixture.cfg.scene.env_spacing += 1. + else: + getattr(fixture.cfg, field)["unexpected"] = True + with pytest.raises(ValueError, match="binding mismatch"): + verify(fixture, manifest) + + +def test_mutable_classvar_physics_config_is_bound(fixture): + manifest = prepare(fixture) + type(fixture.cfg)._usd_field_exceptions["physics"] = "changed" + with pytest.raises(ValueError, match="binding mismatch"): + verify(fixture, manifest) + + +def test_private_dynamic_config_fields_not_silently_ignored(fixture): + manifest = prepare(fixture) + fixture.cfg._hidden = {"actuator_delay": 2} + with pytest.raises(ValueError, match="binding mismatch"): + verify(fixture, manifest) + + +@pytest.mark.parametrize("value", [object(), {1: "bad"}, {1, 2}, float("nan"), float("inf"), lambda: 1]) +def test_unsupported_serialization_fails_closed(fixture, value): + fixture.cfg.actions["bad"] = value + with pytest.raises(ValueError): + prepare(fixture) + + +def test_cyclic_serialization_fails_closed(fixture): + fixture.cfg.actions["cycle"] = fixture.cfg + with pytest.raises(ValueError, match="Cyclic"): + prepare(fixture) + + +def test_canonical_config_is_typed_stable_and_contains_callable_identity(fixture): + encoded = binding.canonical_config(fixture.cfg) + assert encoded == binding.canonical_config(copy.deepcopy(fixture.cfg)) + assert b'"qualname":"reward"' in encoded + assert b"0x" in encoded # exact finite float hex, not an object address + for left, right in [(1, True), (1, 1.), ([1], (1,)), (0., -0.)]: + assert binding.canonical_config(left) != binding.canonical_config(right) + + +@pytest.mark.parametrize("role", ["runner", "instrumentation"]) +def test_required_future_roles_and_missing_files(fixture, role): + fixture.args["future_sources"].pop(role) + with pytest.raises(ValueError, match="runner AND instrumentation"): + prepare(fixture) + + +def test_duplicate_roles_cannot_hide_missing_instrumentation(fixture): + fixture.args["future_sources"]["instrumentation"] = ("runner.py",) + with pytest.raises(ValueError, match="Duplicate"): + prepare(fixture) + + +@pytest.mark.parametrize("relative", binding.FIXED_SOURCES + ("runner.py", "instrumentation.py")) +def test_missing_or_changed_full_source_list(fixture, relative): + manifest = prepare(fixture) + path = fixture.root / relative + path.write_bytes(path.read_bytes() + b"\n") + with pytest.raises(ValueError, match="binding mismatch"): + verify(fixture, manifest) + path.unlink() + with pytest.raises((ValueError, FileNotFoundError)): + prepare(fixture) + + +def test_source_import_must_match_actual_file_not_lookalike_directory(fixture): + module = sys.modules[CFG_NAME] + other = fixture.root / "lookalike.py" + other.write_bytes(Path(module.__file__).read_bytes()) + fixture.monkeypatch.setattr(module, "__file__", str(other)) + fixture.monkeypatch.setattr(module.__spec__, "origin", str(other)) + with pytest.raises(ValueError, match="(?:Imported evaluation source|Config symbol source) mismatch"): + prepare(fixture) + + +@pytest.mark.parametrize("name", tuple(binding.ISAAC_PINS)) +def test_swapped_isaac_tree_composer_and_articulation_backend(fixture, name): + module = sys.modules[name] + swapped = fixture.root / "opt/IsaacLab" / Path(module.__file__).name + swapped.parent.mkdir(parents=True, exist_ok=True) + swapped.write_bytes(Path(module.__file__).read_bytes()) + fixture.monkeypatch.setattr(module, "__file__", str(swapped)) + fixture.monkeypatch.setattr(module.__spec__, "origin", str(swapped)) + with pytest.raises(ValueError, match="(?:Isaac pin/path|normalization source) mismatch"): + prepare(fixture) + + +@pytest.mark.parametrize("name", tuple(binding.ISAAC_PINS)) +def test_isaac_pin_hash_changes_rejected_even_during_preparation(fixture, name): + Path(sys.modules[name].__file__).write_text("# tampered\n") + with pytest.raises(ValueError, match="(?:Isaac pin/path|normalization source) mismatch"): + prepare(fixture) + + +def test_wxyz_is_never_implicitly_adapted(fixture): + fixture.args["quaternion_convention"] = "wxyz" + with pytest.raises(ValueError, match="xyzw"): + prepare(fixture) + + +@pytest.mark.parametrize("kind", ["symlink", "relative", "spec", "namespace", "alias", "dot_alias"]) +def test_imported_path_alias_and_namespace_identity_fail_closed(fixture, kind): + module = sys.modules["synthetic_runner"] + if kind == "symlink": + alias = fixture.root / "runner_alias.py" + alias.symlink_to(module.__file__) + fixture.monkeypatch.setattr(module, "__file__", str(alias)) + elif kind == "relative": + fixture.monkeypatch.setattr(module, "__file__", "runner.py") + elif kind == "dot_alias": + fixture.monkeypatch.setattr(module, "__file__", str(fixture.root) + "/./runner.py") + elif kind == "spec": + fixture.monkeypatch.setattr(module.__spec__, "origin", str(fixture.root / "wrong.py")) + elif kind == "namespace": + fixture.monkeypatch.delattr(module, "__file__") + else: + fixture.monkeypatch.setitem(sys.modules, "surprise_alias", sys.modules[CFG_NAME]) + with pytest.raises(ValueError): + prepare(fixture) + + +def test_entrypoint_requires_real_main_and_explicit_path(fixture): + fixture.args["entrypoints"] = ("runner.py",) + with pytest.raises(ValueError): + prepare(fixture) + fixture.monkeypatch.delitem(sys.modules, "synthetic_runner") + load("__main__", fixture.root / "runner.py", fixture.monkeypatch) + manifest = prepare(fixture) + assert json.loads(manifest)["sources"]["runner"][0]["entrypoint"] is True + verify(fixture, manifest) + + +def test_manifest_requires_independent_exact_digest_and_immutable_bytes(fixture): + manifest = prepare(fixture) + with pytest.raises(TypeError, match="trusted_manifest_sha256"): + binding.verify_binding(manifest, **fixture.args) + with pytest.raises(ValueError, match="trusted manifest"): + binding.verify_binding(manifest, trusted_manifest_sha256="0" * 64, **fixture.args) + for mutable in (bytearray(manifest), json.loads(manifest)): + with pytest.raises(ValueError, match="immutable"): + binding.verify_binding(mutable, trusted_manifest_sha256="0" * 64, **fixture.args) + + +@pytest.mark.parametrize("mutation", [ + lambda x: x + b"\n", lambda x: x.replace(b'"format":', b'"format":"duplicate","format":')]) +def test_noncanonical_json_with_matching_hash_is_rejected(fixture, mutation): + manifest = mutation(prepare(fixture)) + with pytest.raises(ValueError, match="canonical JSON"): + verify(fixture, manifest) + + +def test_tampered_manifest_with_old_trusted_digest_rejected(fixture): + manifest = prepare(fixture) + tampered = manifest.replace(b"93501", b"93502") + with pytest.raises(ValueError, match="trusted manifest"): + binding.verify_binding(tampered, trusted_manifest_sha256=binding.sha256(manifest), **fixture.args) + + +def test_input_mutation_during_snapshot_rejected(fixture): + original = replay.verify_bundle + + def mutate(root): + fixture.cfg.actions["counter"] = len(fixture.calls) + return original(root) + + fixture.monkeypatch.setattr(replay, "verify_bundle", mutate) + with pytest.raises(ValueError, match="changed during snapshot"): + prepare(fixture) + + +def test_frozen_delegate_failure_propagates_at_prepare_and_verify(fixture): + manifest = prepare(fixture) + + def reject(root): + raise ValueError("Frozen candidate SHA256 mismatch") + + fixture.monkeypatch.setattr(replay, "verify_bundle", reject) + with pytest.raises(ValueError, match="Frozen candidate"): + prepare(fixture) + with pytest.raises(ValueError, match="Frozen candidate"): + verify(fixture, manifest) + + +@pytest.mark.parametrize("overrides", [{}, {"seed": "any"}, {"seed": "protocol.trial.seed", "sim.dt": .1}]) +def test_only_explicit_declared_seed_override(fixture, overrides): + fixture.args["declared_overrides"] = overrides + with pytest.raises(ValueError, match="override"): + prepare(fixture) + + +@pytest.mark.parametrize("protocol_path", ["configs/contact_cycle/bounded_diagnostic_p2_v1.json", + "configs/contact_cycle/bounded_diagnostic_p2_extended_v1.json"]) +def test_all_preregistered_p2_seed_rows_without_rewriting_manifest(fixture, protocol_path): + fixture.args["protocol_path"] = protocol_path + protocol = binding.load_protocol(fixture.root / fixture.args["protocol_path"]) + fixture.cfg.seed = protocol.trial(0).seed + manifest = prepare(fixture) + for trial in protocol.trials: + fixture.cfg.seed = trial.seed + verify(fixture, manifest, trial_index=trial.index) + fixture.cfg.seed += 1 + with pytest.raises(ValueError, match="trial seed"): + verify(fixture, manifest) + + +def test_late_imports_fail_closed_until_eager_imported_before_preparation(fixture): + manifest = prepare(fixture) + path = fixture.root / binding.PACKAGE / "late.py" + path.write_text("# late dependency\n") + load("SkillFactory.late", path, fixture.monkeypatch) + with pytest.raises(ValueError, match="binding mismatch"): + verify(fixture, manifest) + + +def test_real_installed_pins_match_files_and_runtime_literals_without_import(): + # Inspection only: no importing Isaac, namespace spoofing or real bundle pass. + runtime = ROOT / binding.PACKAGE / "tasks/manager_based/contact_cycle_sim/evaluation_env.py" + text = runtime.read_text() + for name, (relative, digest) in binding.ISAAC_PINS.items(): + assert binding.sha256((binding.ISAAC_ROOT / relative).read_bytes()) == digest + if name.startswith("isaaclab.") and name not in (*binding.NORMALIZATION_SOURCES, "isaaclab.sim.utils.prims"): + assert digest in text + data = binding.ISAAC_ROOT / binding.ISAAC_PINS[ + "isaaclab_physx.assets.articulation.articulation_data"][0] + assert "orientation is provided in (x, y, z, w) format" in data.read_text() + + +def test_unchanged_frozen_verifier_checks_synthetic_candidate_bytes(fixture): + # Restore the real function from its unchanged source, with synthetic pins + # only in this private test module. Never deserialize any candidate. + with fixture.monkeypatch.context() as temporary: + temporary.setitem(sys.modules, "torch", replay.torch) + actual = load(PACKAGE + ".synthetic_replay", SOURCES / "replay.py", fixture.monkeypatch) + candidate = fixture.root / actual.CANDIDATE + candidate.parent.mkdir(parents=True) + candidate.write_bytes(b"synthetic-not-a-checkpoint") + config_relative = "configs/contact_cycle/simulation_residual_v1.json" + path = fixture.root / config_relative + path.write_bytes((ROOT / config_relative).read_bytes()) + source = "fixture_source.txt" + (fixture.root / source).write_text("frozen source") + snapshot = candidate.parent / "source_snapshot" / source + snapshot.parent.mkdir() + snapshot.write_text("frozen source") + manifest = {"contract": actual.CONTRACT, "task_id": actual.TASK_ID, + "features": actual.FEATURE_VERSION, "model_config": actual.MODEL_CONFIG, + "sources": {source: actual.sha256(snapshot.read_bytes())}, + "training": {"task_config": config_relative}} + (candidate.parent / "manifest.json").write_text(json.dumps(manifest)) + for relative in actual.REPLAY_SOURCES: + path = fixture.root / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("# synthetic frozen replay file\n") + fixture.monkeypatch.setattr(actual, "MANIFEST_SHA256", actual.sha256(actual.canonical(manifest).encode())) + fixture.monkeypatch.setattr(actual, "CANDIDATE_SHA256", actual.sha256(candidate.read_bytes())) + fixture.monkeypatch.setattr(replay, "verify_bundle", actual.verify_bundle) + prepared = prepare(fixture) + candidate.write_bytes(b"tampered candidate") + with pytest.raises(ValueError, match="Frozen candidate SHA256 mismatch"): + verify(fixture, prepared) + candidate.write_bytes(b"synthetic-not-a-checkpoint") + snapshot.write_text("tampered immutable snapshot") + with pytest.raises(ValueError, match="snapshot mismatch"): + verify(fixture, prepared) + + +def test_future_roles_cannot_be_satisfied_by_nonexecutable_placeholder(fixture): + path = fixture.root / "pending.txt" + path.write_text("not implemented") + fixture.args["future_sources"]["instrumentation"] = ("pending.txt",) + with pytest.raises(ValueError, match="executable Python"): + prepare(fixture) + + +def test_undeclared_callable_substitution_is_rejected(fixture): + manifest = prepare(fixture) + fixture.cfg.rewards["task"] = sys.modules["synthetic_runner"].run + with pytest.raises(ValueError, match="binding mismatch"): + verify(fixture, manifest) + + +def test_symbol_must_resolve_to_actual_imported_object(fixture): + original = fixture.cfg.rewards["task"] + fixture.monkeypatch.setattr(sys.modules[CFG_NAME], "reward", lambda: 2) + assert fixture.cfg.rewards["task"] is original + with pytest.raises(ValueError, match="actual imported object"): + prepare(fixture) + + +def test_class_identity_cannot_be_replaced_by_equal_field_dictionary(fixture): + fixture.args["effective_config"] = vars(fixture.cfg) + with pytest.raises(ValueError, match="Effective config"): + prepare(fixture) + + +def test_source_list_symlink_and_parent_alias_rejected(fixture): + path = fixture.root / "instrumentation.py" + renamed = fixture.root / "real_instrumentation.py" + path.rename(renamed) + path.symlink_to(renamed) + with pytest.raises(ValueError, match="Noncanonical"): + prepare(fixture) + fixture.args["future_sources"]["instrumentation"] = ("../instrumentation.py",) + with pytest.raises(ValueError, match="canonical relative"): + prepare(fixture) + + +def test_caller_owned_role_lists_do_not_mutate_prepared_bytes(fixture): + fixture.args["future_sources"]["runner"] = ["runner.py"] + manifest = prepare(fixture) + expected = bytes(manifest) + fixture.args["future_sources"]["runner"].clear() + assert manifest == expected + with pytest.raises(ValueError, match="Missing explicit"): + verify(fixture, manifest) + + +@pytest.fixture +def source_realistic_config(fixture): + """Pinned string/configclass code, without importing Isaac registration/Kit. + + Only the unrelated to_dict/from_dict import is removed from configclass; + these helpers are deliberately unavailable, and binding must never use them. + """ + for name in ("isaaclab.utils.string", "isaaclab.utils.configclass"): + relative, _ = binding.ISAAC_PINS[name] + text = (Path('/home/cen/IsaacLab') / relative).read_text() + if name.endswith('configclass'): + text = text.replace('from .dict import class_to_dict, update_class_from_dict', + 'class_to_dict = update_class_from_dict = None') + path = Path(sys.modules[name].__file__) + path.write_text(text) + load(name, path, fixture.monkeypatch) + binding.ISAAC_PINS[name] = (relative, binding.sha256(path.read_bytes())) + recorder = Path('/home/cen/IsaacLab/source/isaaclab/isaaclab/managers/recorder_manager.py') + enum_node = next(n for n in ast.parse(recorder.read_text()).body + if isinstance(n, ast.ClassDef) and n.name == 'DatasetExportMode') + path = fixture.root / 'source_realistic_config.py' + path.write_text('import enum\nfrom isaaclab.utils.configclass import configclass\n' + + ast.unparse(enum_node) + '\n' + ''' +@configclass +class RecorderManagerBaseCfg: + dataset_export_mode = DatasetExportMode.EXPORT_ALL + +@configclass +class WrappedConfig: + recorder = RecorderManagerBaseCfg() + ui_window_class_type = 'synthetic_runner:run' + spawn_func = 'synthetic_instrumentation:capture' +''') + module = load('source_realistic_config', path, fixture.monkeypatch) + fixture.args['dependency_inventory']['modules'][module.__name__] = { + 'path': str(path), 'sha256': binding.sha256(path.read_bytes())} + cfg = module.WrappedConfig() + fixture.cfg.actions['source_realistic'] = cfg + return module, cfg + + +def test_actual_configclass_enum_and_resolvable_string_layout(fixture, source_realistic_config): + module, cfg = source_realistic_config + rs = sys.modules['isaaclab.utils.string'].ResolvableString + assert type(cfg.ui_window_class_type) is rs + assert rs.__slots__ == ('_resolved_callable', '_resolve_error') + assert copy.deepcopy(cfg.ui_window_class_type) is cfg.ui_window_class_type + encoded = json.loads(binding.canonical_config(cfg))['fields']['mapping'] + wrapped = encoded['ui_window_class_type'] + assert wrapped['reference'] == 'synthetic_runner:run' + assert wrapped['cached_callable'] is None + assert wrapped['target']['symbol'] == {'module': 'synthetic_runner', 'qualname': 'run'} + mode = encoded['recorder']['fields']['mapping']['dataset_export_mode'] + assert mode['enum'] == {'module': module.__name__, 'qualname': 'DatasetExportMode'} + assert mode['member'] == 'EXPORT_ALL' + assert binding.canonical_config(module.DatasetExportMode.EXPORT_ALL) != binding.canonical_config( + int(module.DatasetExportMode.EXPORT_ALL)) + with pytest.raises(ValueError, match='preconstruction'): + prepare(fixture) + binding.normalize_config(fixture.cfg) + manifest = prepare(fixture) + assert cfg.ui_window_class_type._resolved_callable is sys.modules['synthetic_runner'].run + # The real resolver would return exactly this cached object without import/call. + assert cfg.ui_window_class_type._resolve() is sys.modules['synthetic_runner'].run + binding.normalize_config(fixture.cfg) + assert prepare(fixture) == manifest + verify(fixture, manifest) + cfg.ui_window_class_type._resolved_callable = None + with pytest.raises(ValueError, match='preconstruction'): + verify(fixture, manifest) + assert cfg.ui_window_class_type._resolved_callable is None # verification did not repair it + + +@pytest.mark.parametrize('mutation', ['target', 'error', 'missing', 'object', 'plain_string']) +def test_resolvable_reference_cache_fail_closed(fixture, source_realistic_config, mutation): + _, cfg = source_realistic_config + binding.normalize_config(fixture.cfg) + manifest = prepare(fixture) + if mutation == 'target': + cfg.spawn_func._resolved_callable = sys.modules['synthetic_runner'].run + elif mutation == 'error': + cfg.spawn_func._resolve_error = ValueError('cached failure') + elif mutation == 'missing': + fixture.monkeypatch.delattr(sys.modules['synthetic_instrumentation'], 'capture') + elif mutation == 'object': + fixture.monkeypatch.setattr(sys.modules['synthetic_instrumentation'], 'capture', object()) + else: + cfg.spawn_func = str(cfg.spawn_func) + with pytest.raises(ValueError): + verify(fixture, manifest) + + +def test_arbitrary_string_subclass_not_accepted_as_resolvable(fixture): + class Lookalike(str): + pass + fixture.cfg.actions['bad'] = Lookalike('synthetic_runner:run') + with pytest.raises(ValueError, match='Unsupported effective config type'): + prepare(fixture) + + +@pytest.mark.parametrize('field,value', [ + ('clone_in_fabric', True), ('num_envs', 2), ('replicate_physics', 1), +]) +def test_normalizer_rejects_unsupported_clone_settings(fixture, field, value): + setattr(fixture.cfg.scene, field, value) + with pytest.raises(ValueError, match='single-env'): + binding.normalize_config(fixture.cfg) + + +@pytest.mark.parametrize('mutation', ['namespace', 'template', 'multi', 'extra', 'terrain', 'spawn']) +def test_normalizer_rejects_unreviewed_scene_semantics(fixture, mutation): + scene = fixture.cfg.scene + if mutation == 'namespace': + scene.robot.prim_path = '/Other/envs/env_.*/Robot' + elif mutation == 'template': + scene.robot.prim_path = '{OTHER_NS}/Robot' + elif mutation == 'multi': + scene.robot.spawn = SimpleNamespace(spawn_paths=[]) + elif mutation == 'extra': + scene.extra = scene.ground + elif mutation == 'terrain': + scene.ground = SimpleNamespace(prim_path='/World/ground', num_envs=1) + else: + scene.robot.spawn.spawn_path = '/World/envs/env_8/Robot' + with pytest.raises(ValueError): + binding.normalize_config(fixture.cfg) + + +def test_rootxy_exact128_normalizer_does_not_broaden_evaluation_contract(fixture): + name = 'SkillFactory.tasks.manager_based.contact_cycle_sim.rootxy_cfg_v2' + path = fixture.root / 'rootxy_config_double.py' + path.write_text('from ' + CFG_NAME + ' import EvaluationContactCycleEnvCfg\n' + 'class RootXYContactCycleEnvCfg(EvaluationContactCycleEnvCfg):\n pass\n') + module = load(name, path, fixture.monkeypatch) + cfg = module.RootXYContactCycleEnvCfg() + cfg.scene.num_envs = 128 + get = lambda: binding._typed_normalization_updates(cfg, name, 'RootXYContactCycleEnvCfg', 128) + for obj, field, value in get(): + setattr(obj, field, value) + assert get() == [] + assert cfg.scene.robot.prim_path == '/World/envs/env_.*/Robot' + assert cfg.scene.robot.spawn.spawn_path == '/World/envs/env_0/Robot' + with pytest.raises(ValueError, match='actual Evaluation'): + binding.normalize_config(cfg) + fixture.cfg.scene.num_envs = 128 + with pytest.raises(ValueError, match='single-env'): + binding.normalize_config(fixture.cfg) + for count in (1, 127, 129): + cfg.scene.num_envs = count + with pytest.raises(ValueError, match='exact128'): + get() + with pytest.raises(ValueError, match='Unsupported normalization contract'): + binding._typed_normalization_updates(cfg, name, 'RootXYContactCycleEnvCfg', 129) + + +def scene_backend_methods(): + """Extract unmodified method bodies; no constructor, Kit or physics import.""" + source = Path('/home/cen/IsaacLab/source/isaaclab/isaaclab/scene/interactive_scene.py') + scene = next(n for n in ast.parse(source.read_text()).body + if isinstance(n, ast.ClassDef) and n.name == 'InteractiveScene') + methods = [n for n in scene.body if isinstance(n, ast.FunctionDef) + and n.name in ('_build_clone_plan_from_cfg', '_add_entities_from_cfg', 'env_ns', 'env_regex_ns')] + # Only remove the local optional SurfaceGripperCfg import; the unrelated type + # is supplied as a nonmatching sentinel. All normalization statements execute. + for method in methods: + method.body = [n for n in method.body if not isinstance(n, ast.ImportFrom)] + future = ast.parse('from __future__ import annotations').body + return compile(ast.fix_missing_locations(ast.Module(body=future + methods, type_ignores=[])), str(source), 'exec') + + +@pytest.mark.parametrize('replicate', [True, False]) +def test_normalization_parity_with_pinned_scene_methods(fixture, replicate): + scene = fixture.cfg.scene + scene.replicate_physics = replicate + scene.robot.prim_path = '{ENV_REGEX_NS}/Robot' + scene.foot_contact.prim_path = '{ENV_REGEX_NS}/Robot/foot' + scene.foot_contact.filter_prim_paths_expr += ['{ENV_REGEX_NS}/Robot/other'] + scene.foot_contact.sensor_shape_prim_expr = ['{ENV_REGEX_NS}/Robot/foot/shape'] + scene.foot_contact.filter_shape_prim_expr = ['{ENV_REGEX_NS}/Robot/other/shape'] + for name in binding.SPAWNER_TYPES: + getattr(scene, name).spawn.spawn_path = None + scene.mount_wrench.prim_path = '{ENV_REGEX_NS}/Robot' + original = copy.deepcopy(scene) + binding.normalize_config(fixture.cfg) + normalized = binding.canonical_config(scene) + binding.normalize_config(fixture.cfg) + assert binding.canonical_config(scene) == normalized + unrelated = type('Unrelated', (), {}) + types = {name: binding._imported_symbol(*symbol) for name, symbol in binding.SCENE_TYPES.items()} + globals_ = { + 'InteractiveSceneCfg': binding._imported_symbol('isaaclab.scene.interactive_scene_cfg', 'InteractiveSceneCfg'), + 'SensorBaseCfg': (types['foot_contact'], types['mount_wrench']), 'ContactSensorCfg': types['foot_contact'], + 'AssetBaseCfg': types['ground'], 'ArticulationCfg': types['robot'], + 'sim_utils': SimpleNamespace(MultiAssetSpawnerCfg=unrelated, MultiUsdFileCfg=unrelated), + 'torch': SimpleNamespace(ones=lambda *a, **k: 'homogeneous-mask', bool=bool), + 'cloner': SimpleNamespace(ClonePlan=lambda **kw: kw), + 'FrameView': lambda *a, **kw: None, + } + for name in ('RigidObjectCollectionCfg', 'TerrainImporterCfg', 'DeformableObjectCfg', 'RigidObjectCfg', + 'SurfaceGripperCfg', 'FrameTransformerCfg', 'VisuoTactileSensorCfg'): + globals_[name] = unrelated + exec(scene_backend_methods(), globals_) + plans = [] + for candidate in (original, scene): + # Only constructors/spawn side effects are replaced; paths and clone-plan + # branch predicates are the installed source's actual statements. + for name in binding.SCENE_TYPES: + entity = getattr(candidate, name) + entity._test_class_type = getattr(entity, "class_type", None) + entity.class_type = lambda cfg: None + if name in ('ground', 'light'): + entity.init_state = SimpleNamespace(pos=(0, 0, 0), rot=(0, 0, 0, 1)) + entity.spawn.func = lambda *a, **kw: None + backend = SimpleNamespace(cfg=candidate, env_ns='/World/envs', env_regex_ns='/World/envs/env_.*', + env_fmt='/World/envs/env_{}', num_envs=1, device='cpu', stage=None, + _articulations={}, _sensors={}, _extras={}) + plans.append(globals_['_build_clone_plan_from_cfg'](backend)) + globals_['_add_entities_from_cfg'](backend) + for name in binding.SCENE_TYPES: + entity = getattr(candidate, name) + previous = entity._test_class_type + del entity._test_class_type + if previous is None: + del entity.class_type + else: + entity.class_type = previous + if name in ('ground', 'light'): + del entity.init_state + del entity.spawn.func + assert binding.canonical_config(candidate) == normalized + assert plans[0] == plans[1] == {'sources': ('/World/envs/env_0',), + 'destinations': ('/World/envs/env_{}',), 'clone_mask': 'homogeneous-mask'} + assert scene.replicate_physics is replicate + assert globals_['env_ns'].fget(None) == '/World/envs' + assert globals_['env_regex_ns'].fget(SimpleNamespace(env_ns='/World/envs')) == '/World/envs/env_.*' + + +def test_postconstruction_mutation_rejected_without_repair(fixture): + manifest = prepare(fixture) + fixture.cfg.scene.foot_contact.filter_prim_paths_expr.append('/World/changed') + with pytest.raises(ValueError, match='binding mismatch'): + verify(fixture, manifest) + fixture.cfg.scene.robot.prim_path = '{ENV_REGEX_NS}/Robot' + with pytest.raises(ValueError, match='preconstruction'): + verify(fixture, manifest) + assert fixture.cfg.scene.robot.prim_path == '{ENV_REGEX_NS}/Robot' + + +@pytest.mark.parametrize('missing', ['argument', 'empty', 'modules', 'artifacts', 'native', 'core']) +def test_explicit_reviewed_dependency_inventory_required(fixture, missing): + inventory = fixture.args['dependency_inventory'] + if missing == 'argument': + del fixture.args['dependency_inventory'] + with pytest.raises(TypeError, match='dependency_inventory'): + prepare(fixture) + return + if missing == 'empty': + fixture.args['dependency_inventory'] = {} + elif missing in ('modules', 'artifacts'): + inventory[missing] = {} + elif missing == 'native': + inventory['artifacts'].pop('torch/lib') + else: + inventory['modules'].pop('numpy.core._multiarray_umath') + with pytest.raises(ValueError, match='inventory|Missing reviewed'): + prepare(fixture) + + +@pytest.mark.parametrize('mutation', ['file', 'path', 'spec', 'missing', 'artifact']) +def test_external_numerical_substitutions_rejected(fixture, mutation): + manifest = prepare(fixture) + module = sys.modules['torch._C'] + if mutation == 'file': + Path(module.__file__).write_text('# replaced extension implementation') + elif mutation == 'path': + path = fixture.root / 'substituted.py' + path.write_bytes(Path(module.__file__).read_bytes()) + fixture.monkeypatch.setattr(module, '__file__', str(path)) + fixture.monkeypatch.setattr(module.__spec__, 'origin', str(path)) + elif mutation == 'spec': + fixture.monkeypatch.setattr(module.__spec__, 'origin', '/other/tree') + elif mutation == 'missing': + fixture.monkeypatch.delitem(sys.modules, 'torch._C') + elif mutation == 'artifact': + Path(fixture.args['dependency_inventory']['artifacts']['warp/bin']['path']).write_bytes(b'replaced') + else: + path = fixture.root / 'late_numerical.py' + path.write_text('# late implementation') + load('torch.late', path, fixture.monkeypatch) + with pytest.raises(ValueError): + verify(fixture, manifest) + + +def test_provider_outside_project_namespaces_and_future_scope_artifact(fixture): + path = fixture.root / 'external_provider.py' + path.write_text('def read_scope():\n return None\n') + provider = load('external_provider', path, fixture.monkeypatch) + fixture.monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', {**binding.REQUIRED_RUNTIME, + 'external_provider': ('test-provider', str(path), binding.sha256(path.read_bytes()))}) + fixture.monkeypatch.setattr(sys.modules['synthetic_instrumentation'], 'provider', provider, raising=False) + with pytest.raises(ValueError, match='Missing reviewed dependency modules'): + prepare(fixture) + inventory = fixture.args['dependency_inventory'] + inventory['modules']['external_provider'] = {'path': str(path), 'sha256': binding.sha256(path.read_bytes())} + scope = fixture.root / 'reviewed_scope.json' + scope.write_text('{"scope":"test-only"}') + inventory['artifacts']['provider_scope'] = {'module': 'external_provider', 'path': str(scope), + 'sha256': binding.sha256(scope.read_bytes()), + 'purpose': 'explicit provider evidence-scope document'} + manifest = prepare(fixture) + assert 'external_provider' in json.loads(manifest)['dependency_inventory']['modules'] + scope.write_text('{"scope":"substituted"}') + with pytest.raises(ValueError, match='path/hash mismatch'): + verify(fixture, manifest) + + +def test_from_provider_import_dependency_missing(fixture): + path = fixture.root / 'external_provider.py' + path.write_text('def read_state():\n return None\n') + provider = load('external_provider', path, fixture.monkeypatch) + fixture.monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', {**binding.REQUIRED_RUNTIME, + 'external_provider': ('test-provider', str(path), binding.sha256(path.read_bytes()))}) + fixture.monkeypatch.setattr(sys.modules['synthetic_instrumentation'], 'read_state', provider.read_state, + raising=False) + with pytest.raises(ValueError, match='external_provider'): + prepare(fixture) + + +def test_installed_numerical_native_layouts_read_only(): + site = Path('/home/cen/miniconda3/envs/linker_sim_isaac/lib/python3.11/site-packages') + assert (site / 'torch/lib/libtorch_cpu.so').is_file() + assert (site / 'torch/lib/libc10.so').is_file() + assert list((site / 'numpy/core').glob('_multiarray_umath*.so')) + assert list((site / 'numpy/random').glob('mtrand*.so')) + assert list((site / 'numpy.libs').glob('*openblas*.so')) + assert (site / 'warp/bin/warp.so').is_file() + assert (site / 'warp/bin/warp-clang.so').is_file() + context = (site / 'warp/_src/context.py').read_text() + assert 'warp_lib = os.path.join(bin_path, "warp.so")' in context + assert 'self.core = self.load_dll(warp_lib)' in context + + +@pytest.fixture +def actual_clone_targets(fixture, source_realistic_config): + def extracted(name, source, symbol, prefix): + tree = ast.parse(Path(source).read_text()) + node = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == symbol) + path = binding.ISAAC_ROOT / ('source/isaaclab/' + name.replace('.', '/') + '.py') + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text('from __future__ import annotations\n' + prefix + '\n' + ast.unparse(node) + '\n') + return load(name, path, fixture.monkeypatch) + + name = 'isaaclab.sim.utils.prims' + relative, _ = binding.ISAAC_PINS[name] + decorator = extracted(name, Path('/home/cen/IsaacLab') / relative, 'clone', 'import functools') + binding.ISAAC_PINS[name] = (relative, binding.sha256(Path(decorator.__file__).read_bytes())) + modules = [] + for name, symbol in (('isaaclab.sim.spawners.from_files.from_files', 'spawn_from_usd'), + ('isaaclab.sim.spawners.lights.lights', 'spawn_light')): + source = Path('/home/cen/IsaacLab/source/isaaclab') / (name.replace('.', '/') + '.py') + module = extracted(name, source, symbol, 'from isaaclab.sim.utils.prims import clone') + modules.append((module, symbol)) + _, cfg = source_realistic_config + rs = sys.modules['isaaclab.utils.string'].ResolvableString + cfg.spawn_func = rs(modules[0][0].__name__ + ':' + modules[0][1]) + cfg.ui_window_class_type = rs(modules[1][0].__name__ + ':' + modules[1][1]) + return cfg, modules, decorator + + +def test_real_clone_decorated_spawner_reference_identity(fixture, actual_clone_targets): + cfg, modules, _ = actual_clone_targets + binding.normalize_config(fixture.cfg) + manifest = prepare(fixture) + encoded = json.loads(binding.canonical_config(cfg.spawn_func)) + target = encoded['target']['symbol'] + assert target['qualname'] == 'spawn_from_usd' + assert target['clone_wrapper']['decorator']['qualname'] == 'clone' + assert target['clone_wrapper']['freevars'] == ['func'] + assert target['clone_wrapper']['wrapped']['qualname'] == 'spawn_from_usd' + for module, symbol in modules: + wrapper = getattr(module, symbol) + assert wrapper.__closure__[0].cell_contents is wrapper.__wrapped__ + assert wrapper.__code__.co_freevars == ('func',) + verify(fixture, manifest) + + +@pytest.mark.parametrize('mutation', ['forged', 'cell', 'underlying_code', 'chain', 'multiple_cells', 'wrapped']) +def test_clone_wrapper_layout_rejects_forgery(fixture, actual_clone_targets, mutation): + cfg, modules, decorator = actual_clone_targets + module, symbol = modules[0] + wrapper = getattr(module, symbol) + target = wrapper.__wrapped__ + if mutation == 'forged': + import functools + @functools.wraps(wrapper) + def forged(*args, **kwargs): + return wrapper(*args, **kwargs) + fixture.monkeypatch.setattr(module, symbol, forged) + elif mutation == 'cell': + wrapper.__closure__[0].cell_contents = getattr(modules[1][0], modules[1][1]).__wrapped__ + elif mutation == 'underlying_code': + target.__code__ = sys.modules['synthetic_runner'].run.__code__ + elif mutation == 'chain': + fixture.monkeypatch.setattr(module, symbol, decorator.clone(wrapper)) + elif mutation == 'multiple_cells': + import functools + other = 1 + @functools.wraps(wrapper) + def two_cells(*args, **kwargs): + return wrapper(*args, **kwargs), other + fixture.monkeypatch.setattr(module, symbol, two_cells) + else: + wrapper.__wrapped__ = getattr(modules[1][0], modules[1][1]).__wrapped__ + with pytest.raises(ValueError): + binding.normalize_config(fixture.cfg) + assert cfg.spawn_func._resolved_callable is None + + +def test_provider_module_object_substitution_rejected(fixture): + path = fixture.root / 'external_provider.py' + path.write_text('def read_state():\n return None\n') + provider = load('external_provider', path, fixture.monkeypatch) + fixture.monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', {**binding.REQUIRED_RUNTIME, + 'external_provider': ('test-provider', str(path), binding.sha256(path.read_bytes()))}) + fixture.args['dependency_inventory']['modules']['external_provider'] = { + 'path': str(path), 'sha256': binding.sha256(path.read_bytes())} + from types import ModuleType + substituted = ModuleType('external_provider') + assert substituted is not provider + fixture.monkeypatch.setattr(sys.modules['synthetic_instrumentation'], 'provider', substituted, raising=False) + with pytest.raises(ValueError, match='Substituted imported dependency'): + prepare(fixture) + + +def cuda_inventory(fixture): + inventory = fixture.args['dependency_inventory'] + site = Path(inventory['modules']['torch']['path']).parent.parent + cuda = site / 'torch/lib/libtorch_cuda.so' + cuda.write_bytes(b'synthetic-torch-cuda') + inventory['artifacts']['torch_cuda'] = {'module': 'torch', 'path': str(cuda), + 'sha256': binding.sha256(cuda.read_bytes()), + 'purpose': 'Torch CUDA implementation'} + for package in ('cuda_runtime', 'cublas', 'cudnn', 'nvjitlink'): + path = site / 'nvidia' / package / 'lib' / f'lib{package}.so.12' + path.parent.mkdir(parents=True) + path.write_bytes(b'synthetic-nvidia-library') + inventory['artifacts'][package] = {'module': 'torch', 'vendor': 'NVIDIA', 'path': str(path), + 'sha256': binding.sha256(path.read_bytes()), + 'purpose': 'NVIDIA CUDA dependency consumed by Torch'} + return inventory + + +def test_explicit_vendor_cuda_inventory_and_cpu_only_layout(fixture): + cpu = prepare(fixture) # no libtorch_cuda.so, no vendor dirs required + inventory = cuda_inventory(fixture) + manifest = prepare(fixture) + assert manifest != cpu + verify(fixture, manifest) + assert json.loads(manifest)['dependency_inventory']['artifacts']['cublas']['vendor'] == 'NVIDIA' + Path(inventory['artifacts']['nvjitlink']['path']).write_bytes(b'substituted vendor implementation') + with pytest.raises(ValueError, match='path/hash mismatch'): + verify(fixture, manifest) + + +@pytest.mark.parametrize('mutation', ['entry', 'vendor_label', 'directory', 'symlink']) +def test_missing_or_aliased_vendor_cuda_artifacts_rejected(fixture, mutation): + inventory = cuda_inventory(fixture) + artifact = inventory['artifacts']['cublas'] + path = Path(artifact['path']) + if mutation == 'entry': + del inventory['artifacts']['cublas'] + elif mutation == 'vendor_label': + del artifact['vendor'] + elif mutation == 'directory': + path.unlink() + del inventory['artifacts']['cublas'] + else: + target = path.with_name('actual_library.so') + path.rename(target) + path.symlink_to(target) + with pytest.raises(ValueError, match='native artifact|CUDA native layout|Noncanonical'): + prepare(fixture) + + +def test_loaded_scene_inherited_mount_wrench_actual_config_source(fixture, source_realistic_config): + """Execute only pinned config definitions and frozen LoadedSceneCfg class AST. + + No sensor constructor, Isaac package initialization or backend is executed. + """ + for name in ('isaaclab.sensors.sensor_base_cfg', + 'isaaclab.sensors.joint_wrench.joint_wrench_sensor_cfg'): + relative, _ = binding.ISAAC_PINS[name] + path = Path(sys.modules[name].__file__) + path.write_bytes((Path('/home/cen/IsaacLab') / relative).read_bytes()) + load(name, path, fixture.monkeypatch) + binding.ISAAC_PINS[name] = (relative, binding.sha256(path.read_bytes())) + name = 'isaaclab.sensors.joint_wrench.joint_wrench_sensor' + path = fixture.root / 'sensor_factory_double.py' + path.write_text('class JointWrenchSensor:\n pass\n') + load(name, path, fixture.monkeypatch) + source = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/loaded_leg/cfg.py' + node = next(n for n in ast.parse(source.read_text()).body + if isinstance(n, ast.ClassDef) and n.name == 'LoadedSceneCfg') + path = fixture.root / 'actual_loaded_scene.py' + path.write_text('from isaaclab.utils.configclass import configclass\n' + 'from isaaclab.sensors.joint_wrench.joint_wrench_sensor_cfg import JointWrenchSensorCfg\n' + f'from {CFG_NAME} import LegSceneCfg\n' + ast.unparse(node) + '\n') + actual = load('actual_loaded_scene', path, fixture.monkeypatch) + fixture.cfg.scene = actual.LoadedSceneCfg() + sensor = fixture.cfg.scene.mount_wrench + assert set(vars(sensor)) == {'class_type', 'prim_path', 'update_period', 'debug_vis', 'convention'} + assert sensor.prim_path == '{ENV_REGEX_NS}/Robot' + binding.normalize_config(fixture.cfg) + assert sensor.prim_path == '/World/envs/env_.*/Robot' + assert sensor.update_period == 0.0 and sensor.convention == 'incoming_joint_frame' + assert sensor.class_type._resolved_callable is sys.modules[name].JointWrenchSensor + before = binding.canonical_config(fixture.cfg) + binding.normalize_config(fixture.cfg) + assert binding.canonical_config(fixture.cfg) == before + + +@pytest.mark.parametrize('field,value', [('prim_path', '{ENV_REGEX_NS}/Robot/other'), + ('update_period', .1), ('debug_vis', True), + ('convention', 'world'), ('class_type', 'other:Sensor')]) +def test_mount_wrench_only_exact_inherited_sensor_supported(fixture, field, value): + setattr(fixture.cfg.scene.mount_wrench, field, value) + with pytest.raises(ValueError, match='mount_wrench'): + binding.normalize_config(fixture.cfg) + + +def test_binding_mismatch_reports_exact_actual_config_without_mutating_it(fixture): + manifest = prepare(fixture) + fixture.cfg.sim['dt'] = .004 + with pytest.raises(binding.BindingMismatchError) as caught: + verify(fixture, manifest) + report = caught.value.binding_diagnostic + assert report['expected_sha256'] == binding.sha256(manifest) + assert report['actual_sha256'] == binding.sha256(prepare(fixture)) + assert report['difference_count'] == 1 and report['omitted_difference_count'] == 0 + difference = report['differences'][0] + assert difference['path'] == '/effective_config/fields/mapping/sim/mapping/dt/float' + assert difference['expected']['value'] == (.002).hex() + assert difference['actual']['value'] == (.004).hex() + assert fixture.cfg.sim['dt'] == .004 + + +def test_binding_mismatch_reports_import_identity_and_missing_typed_values(): + expected = {'imports': {'a/b~c': {'sha256': 'a' * 64}}, 'values': [True, None, 9]} + actual = {'imports': {'a/b~c': {'sha256': 'b' * 64}}, 'values': [1, None]} + report = binding.binding_difference(binding.canonical_json(expected), binding.canonical_json(actual)) + differences = {d['path']: d for d in report['differences']} + assert differences['/imports/a~1b~0c/sha256']['actual']['value'] == 'b' * 64 + assert differences['/values/0']['expected']['value'] is True + assert differences['/values/0']['actual']['value'] == 1 + assert differences['/values/2']['actual'] == {'present': False} + assert report['difference_count'] == 3 + + +def test_binding_mismatch_diagnostic_is_bounded_and_hashes_large_values_and_paths(): + expected = {str(i): 'a' * 10000 for i in range(200)} + actual = {str(i): 'b' * 10000 for i in range(200)} + report = binding.binding_difference(binding.canonical_json(expected), binding.canonical_json(actual)) + assert len(binding.canonical_json(report)) <= binding.MISMATCH_MAX_BYTES + assert report['difference_count'] == 200 + assert len(report['differences']) == binding.MISMATCH_MAX_DIFFERENCES + assert report['omitted_difference_count'] == 200 - binding.MISMATCH_MAX_DIFFERENCES + assert all('value' not in d['actual'] for d in report['differences']) + assert report['differences'][0]['actual']['sha256'] == binding.sha256(binding.canonical_json('b' * 10000)) + path = '/' + 'x' * 2000 + report = binding.binding_difference(binding.canonical_json({path[1:]: 1}), + binding.canonical_json({path[1:]: 2})) + assert report['differences'][0]['path_sha256'] == binding.sha256(path.encode()) + + +def test_verify_binding_reports_new_canonical_import_identity(fixture): + manifest = prepare(fixture) + name = 'isaaclab.constructor_only' + path = binding.ISAAC_ROOT / 'source/isaaclab/isaaclab/constructor_only.py' + path.write_text('# synthetic constructor-only import\n') + load(name, path, fixture.monkeypatch) + with pytest.raises(binding.BindingMismatchError) as caught: + verify(fixture, manifest) + report = caught.value.binding_diagnostic + difference = next(d for d in report['differences'] if d.get('path') == '/imports/' + name) + assert difference['expected'] == {'present': False} + record = json.loads(prepare(fixture))['imports'][name] + assert difference['actual']['sha256'] == binding.sha256(binding.canonical_json(record)) + + +def test_binding_mismatch_byte_limit_is_enforced_before_record_limit(): + expected = {'x' * 900 + str(i): 'a' * 200 for i in range(64)} + actual = {'x' * 900 + str(i): 'b' * 200 for i in range(64)} + report = binding.binding_difference(binding.canonical_json(expected), binding.canonical_json(actual)) + assert len(binding.canonical_json(report)) <= binding.MISMATCH_MAX_BYTES + assert 0 < len(report['differences']) < binding.MISMATCH_MAX_DIFFERENCES + assert report['difference_count'] == 64 + assert report['omitted_difference_count'] == 64 - len(report['differences']) diff --git a/tests/test_contact_cycle_sim_evaluation_finite_scope.py b/tests/test_contact_cycle_sim_evaluation_finite_scope.py new file mode 100644 index 0000000..9f36118 --- /dev/null +++ b/tests/test_contact_cycle_sim_evaluation_finite_scope.py @@ -0,0 +1,174 @@ +"""Finite v5 development scope and shutdown ownership; CPU only, never Kit.""" +import json +import sys +from pathlib import Path +from types import ModuleType +from types import SimpleNamespace as NS + +import pytest +from test_contact_cycle_sim_evaluation_binding import binding, fixture, load, prepare, verify # noqa: F401 +from test_contact_cycle_sim_evaluation_runner import cli +from test_contact_cycle_sim_preparation_supervisor import prep + + +def test_exact_critical_role_list_and_pins_read_only(): + assert set(binding.REQUIRED_RUNTIME) == { + 'torch', 'torch._C', 'torch.random', 'torch.cuda', 'numpy', 'numpy.random', + 'numpy.random.mtrand', 'numpy._core._multiarray_umath', 'warp', 'warp._src.context', + 'warp._src.types', 'warp._src.builtins', 'carb', 'omni.kit.app', 'omni.physics.tensors', + 'omni.physx', 'pxr.Tf', 'pxr.Gf', 'pxr.Sdf', 'pxr.Usd', 'pxr.UsdGeom', 'pxr.UsdLux', + 'pxr.UsdPhysics', 'pxr.UsdShade', 'pxr.UsdUtils', 'pxr.Vt', 'pxr.PhysxSchema', + 'pxr.PhysicsSchemaTools', 'isaaclab.sim.simulation_context', 'omni.ext._impl.fast_importer', + 'simulation_app.simulation_app', 'isaaclab_physx.physics.physx_manager', + 'isaaclab.physics.physics_manager', 'omni.timeline', 'omni.usd', 'omni.physics.tensors.api', + 'omni.physics.tensors.bindings._physicsTensors', 'omni.physx.bindings._physx', + } + for role, filename, digest in binding.REQUIRED_RUNTIME.values(): + assert role and binding.sha256(Path(filename).read_bytes()) == digest + for filename, digest in binding.RUNTIME_LAYOUT_PINS.items(): + assert binding.sha256(Path(filename).read_bytes()) == digest + + +def test_unrelated_scipy_metadata_never_execution_dependency(fixture): # noqa: F811 + manifest = prepare(fixture) + module = ModuleType('scipy.incidental') + def function(): + return None + function.__module__ = 'View.MemoryView' + module.cython_function = function + module.native_container = ModuleType('_unimportable_native_container') + fixture.monkeypatch.setitem(sys.modules, module.__name__, module) + fixture.monkeypatch.setattr(sys.modules['torch'], 'unrelatedSciPy', module, raising=False) + verify(fixture, manifest) + required = binding.required_dependencies(set(), set()) + assert not {'View.MemoryView', '_unimportable_native_container', module.__name__} & required + observations = binding.incidental_observations(set(sys.modules) - {module.__name__}) + row = observations[module.__name__] + assert row['status'] == 'observational-not-verified' + assert row['limitation'] == 'unverified-dynamic-internals' + assert 'sha256' not in row and 'observed_file' not in row + + +@pytest.mark.parametrize('mutation', ['missing', 'foreign', 'swapped', 'fileless']) +def test_required_core_module_identity_fails(fixture, mutation): # noqa: F811 + manifest = prepare(fixture) + name = 'torch._C' + if mutation == 'missing': + fixture.monkeypatch.delitem(sys.modules, name) + elif mutation == 'fileless': + fixture.monkeypatch.setitem(sys.modules, name, ModuleType(name)) + else: + path = fixture.root / 'foreign_core.py' + path.write_text('# not the declared core\n') + replacement = load(name, path, fixture.monkeypatch) + if mutation == 'swapped': + replacement.__name__ = 'not_torch_core' + with pytest.raises(ValueError): + verify(fixture, manifest) + + +def preparation_fixture(tmp_path, monkeypatch): + project = tmp_path / 'project' + frozen = project / 'logs/contact_cycle/sim_residual_training_v1/prepared01' + frozen.mkdir(parents=True) + (frozen / 'candidate.pt').write_bytes(b'CPU-only not a policy') + source = project / 'required.py' + source.write_text('VALUE = 1\n') + monkeypatch.setattr(cli, 'ROOT', project) + monkeypatch.setattr(cli, 'launch_identity', lambda: {'scope': 'CPU fixture'}) + monkeypatch.setattr(prep.r, 'ROOT', project) + manifest = json.dumps({'imports': {}, 'sources': [{'path': str(source), + 'sha256': binding.sha256(source.read_bytes())}]}).encode() + cfg = NS(value=1) + checks = [] + + def check(*_, **arguments): + assert arguments['effective_config'] is cfg + checks.append(cfg.value) + if cfg.value != 1: + raise ValueError('preclose config mutation') + + runtime = NS(cfg=cfg, binding=NS(FORMAT='CPU-v5', PROVENANCE_SCOPE='development', + PROVENANCE_LIMITATIONS=(), prepare_binding=lambda **_: manifest, + verify_binding=check, incidental_observations=lambda _: {}), + arguments={'effective_config': cfg, 'dependency_inventory': {'modules': {}}}, + app=NS(close=lambda **_: None)) + # Lifecycle fixture, not an installed PhysX export qualification. Exercise + # the real metadata comparison with a synthetic resolver at that boundary; + # actual export identity/tamper coverage lives in constructor_export tests. + runtime.constructor_export = {'scope': 'CPU synthetic constructor export'} + runtime.generator_inventory = {'constructor_export': dict(runtime.constructor_export)} + def constructor_export(bound, *, expected): + assert bound is runtime.binding and expected is runtime.constructor_export + return None, dict(expected) + monkeypatch.setattr(cli, 'physx_constructor_export', constructor_export) + monkeypatch.setattr(cli, 'real_runtime', lambda *_: runtime) + monkeypatch.setattr(cli, 'runtime_metadata', lambda _: {}) + root = tmp_path / 'campaign' + root.mkdir() + args = NS(output=root / 'prep-p1', inventory=root / 'inventory', trial_index=0, artifact_root=root) + baseline = {str(p): binding.sha256(p.read_bytes()) for p in frozen.iterdir()} + return runtime, args, source, baseline, checks + + +def test_fast_shutdown_interpreter_exit_after_durable_evidence(tmp_path, monkeypatch): + runtime, args, _, baseline, checks = preparation_fixture(tmp_path, monkeypatch) + + def close(*, exit_code): + assert exit_code == 0 and checks == [1] + assert json.loads((args.output / 'preparation.json').read_bytes())['preclose_identity_verified'] + assert (args.output / 'preclose-filesystem.json').is_file() + raise SystemExit(0) # No child code after this point may be required. + + runtime.app.close = close + with pytest.raises(SystemExit) as stopped: + cli.prepare(args) + assert stopped.value.code == 0 and checks == [1] + assert prep.postexit_identity(args.artifact_root, baseline)['status'] == 'PASS' + assert checks == [1] # external owner never claims a destroyed cfg object check + + +def test_preclose_config_mutation_persists_failure_and_nonzero(tmp_path, monkeypatch): + runtime, args, _, baseline, _ = preparation_fixture(tmp_path, monkeypatch) + monkeypatch.setattr(cli, 'runtime_metadata', lambda _: setattr(runtime.cfg, 'value', 2) or {}) + codes = [] + runtime.app.close = lambda *, exit_code: codes.append(exit_code) + with pytest.raises(ValueError, match='config mutation'): + cli.prepare(args) + assert codes == [1] + assert (args.output / 'preparation-failure.json').is_file() + with pytest.raises((ValueError, FileNotFoundError)): + prep.postexit_identity(args.artifact_root, baseline) + + +@pytest.mark.parametrize('mutation', ['source', 'frozen', 'missing-evidence', 'nonzero']) +def test_parent_postexit_identity_cannot_turn_tamper_or_failure_into_pass(tmp_path, monkeypatch, mutation): + _, args, source, baseline, _ = preparation_fixture(tmp_path, monkeypatch) + assert cli.prepare(args) == 0 + if mutation == 'source': + source.write_text('VALUE = 2\n') + elif mutation == 'frozen': + Path(next(iter(baseline))).write_bytes(b'changed') + elif mutation == 'missing-evidence': + (args.output / 'preparation.json').unlink() + errors = ['preparation_child_exit_139'] if mutation == 'nonzero' else [] + postexit = prep.collect_postexit_identity( + args.artifact_root, baseline, NS(returncode=139), errors, prep.b.boottime() + 5) + if mutation == 'nonzero': + assert postexit['status'] == 'PASS' and errors == ['preparation_child_exit_139'] + else: + assert postexit is None and errors + + +def test_incidental_native_pseudo_module_observes_owner_not_fake_module_hash(tmp_path, monkeypatch): + binary = tmp_path / '_core.so' + binary.write_bytes(b'CPU-only non-executable binary fixture') + monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', { + 'native._core': ('test-core', str(binary), binding.sha256(binary.read_bytes()))}) + module = ModuleType('native._core.pseudo') + module.__file__ = str(binary) + monkeypatch.setitem(sys.modules, module.__name__, module) + row = binding.incidental_observations(set(sys.modules) - {module.__name__})[module.__name__] + assert row['observed_binary_owner'] == 'native._core' + assert row['observed_file']['sha256'] == binding.sha256(binary.read_bytes()) + assert row['status'] == 'observational-not-verified' and 'sha256' not in row diff --git a/tests/test_contact_cycle_sim_evaluation_instrumentation.py b/tests/test_contact_cycle_sim_evaluation_instrumentation.py new file mode 100644 index 0000000..d291dc3 --- /dev/null +++ b/tests/test_contact_cycle_sim_evaluation_instrumentation.py @@ -0,0 +1,486 @@ +"""CPU only: frozen load math/scheduler, runtime guard stack, pinned source layouts. + +AST-extracted backend Python classes never import Kit or start AppLauncher. These +are not actual imported-backend identity evidence. +""" +import ast +import copy +import importlib.util +import logging +import random +import sys +import types +from abc import ABC, abstractmethod +from contextlib import contextmanager +from hashlib import sha256 +from pathlib import Path +from types import SimpleNamespace as NS + +import numpy as np +import pytest +import torch +from SkillFactory.legs.contact_cycle_sim.evaluation_audit import TrialAudit, carry_state, fingerprint +from test_contact_cycle_sim_evaluation_runtime import protocol, runtime # noqa: F401 + +ROOT = Path(__file__).resolve().parents[1] +PATH = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_instrumentation.py' +spec = importlib.util.spec_from_file_location('cpu_evaluation_instrumentation', PATH) +ins = importlib.util.module_from_spec(spec) +spec.loader.exec_module(ins) + + +def source_classes(monkeypatch, module, names, **dependencies): + path = ins.BACKEND_SOURCE_INVENTORY[module][0] + tree = ast.parse(Path(path).read_text()) + nodes = [n for n in tree.body if isinstance(n, ast.ClassDef) and n.name in names] + code = 'from __future__ import annotations\n' + '\n'.join(ast.unparse(n) for n in nodes) + obj = types.ModuleType(module) + obj.__dict__.update(torch=torch, logger=logging.getLogger(__name__), **dependencies) + exec(compile(code, path, 'exec'), obj.__dict__) + monkeypatch.setitem(sys.modules, module, obj) + return obj + + +@pytest.fixture +def backend(monkeypatch): + circular = source_classes(monkeypatch, 'isaaclab.utils.buffers.circular_buffer', ['CircularBuffer']) + delay = source_classes(monkeypatch, 'isaaclab.utils.buffers.delay_buffer', ['DelayBuffer'], + CircularBuffer=circular.CircularBuffer) + base = source_classes(monkeypatch, 'isaaclab.actuators.actuator_base', ['ActuatorBase'], + ABC=ABC, abstractmethod=abstractmethod) + pd = source_classes(monkeypatch, ins.ACTUATOR_MODULE, + ['ImplicitActuator', 'IdealPDActuator', 'DelayedPDActuator'], + ActuatorBase=base.ActuatorBase, DelayBuffer=delay.DelayBuffer) + + def actuator(kind='ImplicitActuator'): + cfg = NS(**{k: 1.0 for k in ('stiffness', 'damping', 'armature', 'friction', + 'dynamic_friction', 'viscous_friction', 'velocity_limit', 'velocity_limit_sim', + 'effort_limit', 'effort_limit_sim')}, max_delay=3, min_delay=1) + return getattr(pd, kind)(cfg, ['a', 'b', 'c'], slice(None), 1, 'cpu', + effort_limit=10., velocity_limit=10.) + return NS(actuator=actuator, pd=pd) + + +def scoped(value): + import copy + + from SkillFactory.tasks.manager_based.contact_cycle_sim.evaluation_rng_scope import ( + EVIDENCE_SCOPE, + UNOBSERVED_INTERNALS, + ) + return {**value, 'evidence_scope': copy.deepcopy(EVIDENCE_SCOPE), + 'unobserved_internals': copy.deepcopy(UNOBSERVED_INTERNALS)} + + +def backend_rng(_state): + return scoped({'coverage': {'cpu_mock': {'status': 'observed', 'sources': [__file__], + 'evidence': 'Test-owned explicit deterministic RNG buffer, not a PhysX claim', + 'state': [7, 8, 9]}}}) + + +@pytest.fixture +def setup(runtime, backend, monkeypatch): # noqa: F811 - imported pytest fixture + monkeypatch.setattr(runtime.base.CycleState, 'apply_loads', runtime.real_apply_loads) + composer_class = type(runtime.Robot().permanent_wrench_composer) + old_setter = composer_class.set_forces_and_torques_index + + def setter(c, **kw): + result = old_setter(c, **kw) + for name in ('global_force_w', 'local_force_b', 'local_torque_b'): + setattr(c, name, torch.zeros_like(kw['forces'])) + c.global_force_at_com_w = kw['forces'].clone() + c.global_torque_w = kw['torques'].clone() + if getattr(c, 'corrupt', False): + c.local_force_b += 1 + return result + monkeypatch.setattr(composer_class, 'set_forces_and_torques_index', setter) + + @contextmanager + def opened(stage='P2', trial=4, callback=None): + hooks = runtime.env.RuntimeHooks(runtime.Sim, [(runtime.Robot, 'reset'), + (runtime.Robot, 'write_root_pose_to_sim'), (runtime.Scene, 'reset')]) + with hooks: + env = runtime.env.EvaluationContactCycleEnv(runtime.config(), protocol=protocol(stage), + trial_index=0, runtime_hooks=hooks, evidence_callback=lambda *a: ()) + # CPU fixture selects a row without running preceding physical trials. + if trial: + env.evaluation_trial_index = trial + with hooks.scope('initialization'): + env._cycle_state = runtime.mdp.EvaluationCycleState(env) + env._guard_trial_objects() + robot = env._cycle_state.robot + robot.actuators = {'joints': backend.actuator()} + for key in ('_joint_pos_target_sim', '_joint_vel_target_sim', '_joint_effort_target_sim'): + setattr(robot, key, torch.zeros(1, 3)) + for key in ('joint_pos_target', 'joint_vel_target', 'joint_effort_target'): + setattr(robot.data, key, NS(torch=torch.zeros(1, 3))) + evidence = [] + with ins.EvaluationInstrumentation(env, backend_rng=backend_rng, generators={}) as observer: + def record(event, s): + evidence.append((event.kind, observer.supplemental(s))) + return () if callback is None else callback(event, s) + env.evidence_callback = record + env._cycle_state.safety_causes = lambda: () + env.reset() + yield NS(env=env, s=env._cycle_state, hooks=hooks, observer=observer, evidence=evidence) + return opened + + +def apply_one(f): + with f.hooks.wrench_update(f.s.robot.permanent_wrench_composer): + f.s.apply_loads() + # Advance only CPU evidence clock, never a simulator step. + f.s.substeps += 1 + f.s.time[:] = f.s.substeps * .002 + return f.observer.supplemental(f.s) + + +def test_unique_submission_actual_frozen_world_frames_and_com_arm(setup): + with setup() as f: + s, c = f.s, f.s.robot.permanent_wrench_composer + # Distinct environment-origin/world coordinates and nontrivial xyzw poses. + s.env.scene.env_origins[:] = torch.tensor([[11., -3., 2.]]) + s.robot.data.body_link_pos_w.torch[:] = torch.tensor([[[12., 4., 3.]]]) + s.robot.data.body_com_pos_w.torch[:] = torch.tensor([[[12.2, 4.1, 3.4]]]) + s.robot.data.body_link_quat_w.torch = torch.tensor([[[0., 0., 2**-.5, 2**-.5]]]).repeat(1, 4, 1) + # Real event 0 is world-force/body-point; use actual scheduler time without re-execution. + s.time[:] = s.cc['events'][0]['start_s'] + .1 + before = c.set_calls + out = apply_one(f) + applied = out['applied'] + e = applied['events'][0] + assert applied['trial_index'] == 4 and applied['global_tick'] == 1 + assert e['event_index'] == 0 and e['envelope'] > 0 + assert c.set_calls == before + 1 + assert f.hooks.composer_buffer_replacements == 1 + assert applied['total_com_wrench'] == torch.cat((c.last['forces'], c.last['torques']), -1)[0].tolist() + force, couple = torch.tensor(e['force_world']), torch.tensor(e['couple_world']) + torque = couple + torch.cross(torch.tensor(e['point_world']) - torch.tensor(e['com_world']), force, dim=-1) + torch.testing.assert_close(s.external[0, e['body_index'], 3:], torque) + assert e['point_frame_origin_world'] == [12., 4., 3.] + assert e['frame_quat_xyzw'] == [0., 0., 0., 1.] + # Validate the exact frozen auditor schema/arithmetic with no fabricated setter. + audit = TrialAudit(s.protocol, s.trial.index) + state = {'applied': applied, 'external': s.external[0].tolist(), 'guide': s.guide[0].tolist(), + 'auxiliary': s.auxiliary[0].tolist(), 'body_names': s.robot.body_names, + 'event_program': s.cc['events'], 'env_origin': s.env.scene.env_origins[0].tolist(), + 'events': [{'envelope': float(s.schedule.envelope[0, j])} for j in range(len(s.cc['events']))]} + assert list(audit._applied(state, s.protocol.evidence_clock(4, 1))) == [0] + # Returned mutable JSON containers are owned copies, never live tensor aliases. + out['applied']['events'][0]['point_world'][0] = -999 + out['actuator_state']['groups']['joints']['buffers']['applied_effort'][0][0] = -999 + assert f.observer.supplemental(s)['applied']['events'][0]['point_world'][0] != -999 + assert s.robot.actuators['joints'].applied_effort[0, 0] == 0 + + +@pytest.mark.parametrize('trial', [4, 8, 12]) +def test_selected_events_every_zero_envelope_tick_and_next_reference(setup, trial): + with setup(trial=trial) as f: + assert f.evidence[0][1]['applied'] is None + before = f.env.sim.calls + result = f.env.step(torch.zeros(1, 3)) + assert result.stop is None + ticks = [v for k, v in f.evidence if k == 'tick'] + assert len(ticks) == 5 and f.env.sim.calls == before + 5 + for i, value in enumerate(ticks, 1): + assert value['applied']['global_tick'] == i + assert [e['event_index'] for e in value['applied']['events']] == list(f.s.trial.event_indices) + assert all(e['envelope'] == 0 for e in value['applied']['events']) + assert f.evidence[-1][0] == 'next_reference' + assert f.evidence[-1][1] == ticks[-1] + assert f.s.robot.permanent_wrench_composer.set_calls == 5 + assert f.hooks.composer_buffer_replacements == 5 + + +@pytest.mark.parametrize('stage,trial', [('P1', 0), ('P2', 0), ('P2', 16)]) +def test_no_event_rows_never_fabricate_application(setup, stage, trial): + with setup(stage, trial) as f: + assert f.observer.supplemental(f.s)['applied'] is None + out = apply_one(f) + assert out['applied'] is None + assert not f.s.external.any() + assert not f.s.schedule.fired.any() + + +@pytest.mark.parametrize('frame', ['world', 'mount', 'body']) +@pytest.mark.parametrize('point_frame', ['world', 'mount', 'body']) +def test_frame_matrix_uses_frozen_load_math(setup, frame, point_frame): + with setup() as f: + # Test-local descriptor variation exercises all frozen supported frame combinations. + e = f.s.cc['events'][0] + e.update(frame=frame, point_frame=point_frame) + f.s.env.scene.env_origins[:] = torch.tensor([[5., 7., 1.]]) + f.s.robot.data.body_link_pos_w.torch[:] = torch.arange(12).reshape(1, 4, 3) + f.s.robot.data.body_link_quat_w.torch = torch.tensor([[[0., 0., 1., 0.]]]).repeat(1, 4, 1) + f.s.time[:] = e['start_s'] + e['ramp_s'] + out = apply_one(f)['applied']['events'][0] + assert out['frame_quat_xyzw'] == ([0., 0., 0., 1.] if frame == 'world' else [0., 0., 1., 0.]) + if point_frame == 'world': + assert out['point_frame_origin_world'] == [5., 7., 1.] + expected = (torch.tensor(e['point_m']) + torch.tensor([5., 7., 1.])).tolist() + assert out['point_world'] == pytest.approx(expected) + + +def test_repeated_supplemental_seam_fingerprint_and_rng_unchanged(setup): + with setup() as f: + f.s.time[:] = f.s.cc['events'][0]['start_s'] + .1 + before = apply_one(f) + schedule = copy.deepcopy(f.observer._schedule_state) + c = f.s.robot.permanent_wrench_composer + torch_rng = torch.get_rng_state().clone() + for _ in range(3): + after = f.observer.supplemental(f.s) + assert fingerprint(carry_state(before)) == fingerprint(carry_state(after)) + assert torch.equal(torch_rng, torch.get_rng_state()) + assert f.observer._schedule_state == schedule + assert c.set_calls == 1 + # Exercise actual auditor event integration once and its reference-only dispatch. + audit = TrialAudit(f.s.protocol, f.s.trial.index) + audit.initial = {'state': {'time': 1.1, 'liftoff': False, 'touchdown': False, 'edge_phase': -1, + 'events': [dict(fired=False, completed=False, start=0., envelope=0., integral=0.) + for _ in f.s.cc['events']]}, 'event': {'command': {'phase': 0}}} + state = {**before, 'refreshes': 1, 'external': f.s.external[0].tolist(), + 'guide': f.s.guide[0].tolist(), 'auxiliary': f.s.auxiliary[0].tolist(), + 'body_names': f.s.robot.body_names, 'event_program': f.s.cc['events'], + 'env_origin': f.s.env.scene.env_origins[0].tolist(), + 'events': [{k: schedule[k][0][j] for k in ins.SCHEDULE_FIELDS} + for j in range(len(f.s.cc['events']))]} + audit.last = {'state': state} + assert not audit._events(state, f.s.protocol.evidence_clock(f.s.trial.index, 1)) + impulse = copy.deepcopy(audit.event_integrals) + force = before['applied']['events'][0]['force_world'] + assert impulse[0]['force_impulse_Ns'] == pytest.approx([x * .002 for x in force]) + audit.tick, audit.needs_reference = 5, True + next_state = {**state, 'refreshes': 2} + assert audit._reference_event({'state': next_state}, 'next_reference', next_state, {'causes': []}, 5) == () + assert audit.event_integrals == impulse + # The public consumer returns immediately after this dispatch, before _events. + audit_path = ROOT / 'source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_audit.py' + tree = ast.parse(audit_path.read_text()) + consumer = next(n for n in ast.walk(tree) if isinstance(n, ast.FunctionDef) and n.name == '_consume') + text = ast.unparse(consumer) + assert text.index('return handled') < text.index('self._events(') + + +@pytest.mark.parametrize('failure', ['setter', 'buffer', 'duplicate_schedule', 'duplicate_setter', 'pose']) +def test_exception_restores_guard_stack_and_permissions(setup, failure): + with setup() as f: + c, h, s = f.s.robot.permanent_wrench_composer, f.hooks, f.s + wrapper = c.set_forces_and_torques_index + schedule_wrapper = s.schedule.step + if failure == 'setter': + c.fail = True + elif failure == 'buffer': + c.corrupt = True + with pytest.raises((RuntimeError, ins.InstrumentationError)), h.wrench_update(c): + if failure in ('setter', 'buffer'): + s.apply_loads() + elif failure == 'duplicate_setter': + s.apply_loads() + c.set_forces_and_torques_index(**c.last) + else: + args = (s.time, s.reference()[0], s.edge_off, s.edge_on, s.scenarios, .002) + s.schedule.step(*args) + if failure == 'duplicate_schedule': + s.schedule.step(*args) + else: + s.robot.data.body_com_pos_w.torch += 1 + c.set_forces_and_torques_index() + assert h._load_owner is h._replacement_owner is None + assert c.set_forces_and_torques_index is wrapper and s.schedule.step is schedule_wrapper + with pytest.raises(ins.InstrumentationError): + f.observer.supplemental(s) + # Instrumentation unwound first, runtime hooks restored original instance ownership. + assert 'set_forces_and_torques_index' not in vars(c) + assert 'step' not in vars(s.schedule) + + +@pytest.mark.parametrize('change', ['past', 'future', 'scheduler']) +def test_stale_future_or_changed_schedule_rejected(setup, change): + with setup() as f: + apply_one(f) + if change == 'past': + f.s.substeps += 1 + elif change == 'future': + f.s.substeps.zero_() + else: + f.s.schedule.start += 1 + with pytest.raises(ins.InstrumentationError): + f.observer.supplemental(f.s) + + +def test_p2_begin_trial_replaces_schedule_clears_capture_without_extra_reset(setup): + with setup(trial=0) as f: + assert f.env.step(torch.zeros(1, 3)).stop is None + old_state = f.s + old_schedule = old_state.schedule + f.env._stop(('functional_mock_failure',)) + f.env.acknowledge_finalized(f.env.stop_result) + before = f.s.robot.permanent_wrench_composer.set_calls + f.env.begin_trial(f.s.protocol, 1) + assert f.env._cycle_state is not old_state + assert 'step' not in vars(old_schedule) + assert f.observer._state is f.env._cycle_state + assert f.observer._capture is None + assert f.evidence[-1][0] == 'initial' and f.evidence[-1][1]['applied'] is None + assert f.env._cycle_state.robot.permanent_wrench_composer.set_calls == before + assert f.hooks._load_owner is f.hooks._replacement_owner is None + + +@pytest.mark.parametrize('kind', ['ImplicitActuator', 'IdealPDActuator', 'DelayedPDActuator']) +def test_pinned_source_actuator_delay_layout_complete(setup, backend, kind): + with setup() as f: + a = backend.actuator(kind) + f.s.robot.actuators = {'joints': a} + if kind == 'DelayedPDActuator': + for name in ins.DELAY_NAMES: + delay = getattr(a, name) + delay.set_time_lag(2) + for i in range(6): + delay.compute(torch.full((1, 3), float(i))) + snapshot = f.observer.supplemental(f.s)['actuator_state']['groups']['joints'] + assert snapshot['implicit_solver'] == (kind == 'ImplicitActuator') + if kind == 'DelayedPDActuator': + d = snapshot['buffers']['positions_delay_buffer'] + assert d['_time_lags'] == [2] + assert d['_circular_buffer']['_num_pushes'] == [6] + assert [row[0][0] for row in d['_circular_buffer']['_buffer']] == [2., 3., 4., 5.] + else: + assert snapshot['memory'] == 'stateless_controller' + + +@pytest.mark.parametrize('unsupported', ['extra', 'subclass', 'newton', 'empty', 'delay_extra']) +def test_unsupported_hidden_actuator_state_fails_closed(setup, backend, unsupported): + with setup() as f: + robot = f.s.robot + if unsupported == 'extra': + robot.actuators['joints'].secret_history = torch.zeros(1) + elif unsupported == 'subclass': + robot.actuators['joints'].__class__ = type('ImplicitActuator', (type(robot.actuators['joints']),), {}) + elif unsupported == 'newton': + robot._has_newton_actuators = True + elif unsupported == 'empty': + robot.actuators = {} + else: + robot.actuators['joints'] = backend.actuator('DelayedPDActuator') + robot.actuators['joints'].positions_delay_buffer._circular_buffer.secret = 1 + with pytest.raises(ins.InstrumentationError): + f.observer.supplemental(f.s) + + +def test_rng_generator_inventory_no_draw_and_owned_copies(): + generators = {'python': random.Random(4), 'numpy': np.random.default_rng(5), + 'legacy': np.random.RandomState(6), 'torch': torch.Generator().manual_seed(7)} + before = ins.rng_snapshot(None, backend_rng, generators) + after = ins.rng_snapshot(None, backend_rng, generators) + assert before == after + after['generators']['torch'][0] = -1 + assert ins.rng_snapshot(None, backend_rng, generators) == before + with pytest.raises(ins.InstrumentationError): + ins.rng_snapshot(None, backend_rng, {'unbound': object()}) + + +@pytest.mark.parametrize('value', [{}, {'coverage': {}}, {'coverage': {'x': {}}}, + {'coverage': {'x': {'status': 'unavailable', 'sources': ['physx'], 'evidence': 'No getter', 'state': None}}}]) +def test_missing_invalid_unavailable_backend_rng_is_a_blocker(value): + with pytest.raises(ins.InstrumentationError): + ins.qualify_backend_rng(value) + + +def test_explicit_not_applicable_is_not_fabricated_rng_state(): + value = scoped({'coverage': {'x': {'status': 'not_applicable', 'sources': [__file__], + 'evidence': 'CPU mock has no solver RNG; not a production qualification', 'state': None}}}) + assert ins.qualify_backend_rng(value) == value + value['coverage']['x']['state'] = [0] + with pytest.raises(ins.InstrumentationError): + ins.qualify_backend_rng(value) + with pytest.raises(ins.InstrumentationError): + ins.EvaluationInstrumentation(None, backend_rng=None, generators={}) + + +def test_backend_source_inventory_hashes_and_real_import_required(): + for path, digest in ins.BACKEND_SOURCE_INVENTORY.values(): + assert sha256(Path(path).read_bytes()).hexdigest() == digest + with pytest.raises(ins.InstrumentationError, match='identity'): + ins.verify_backend_sources() + data = Path(ins.BACKEND_SOURCE_INVENTORY['isaaclab_physx.assets.articulation.articulation_data'][0]).read_text() + assert '(x, y, z, w)' in data + kernels = Path(ins.BACKEND_SOURCE_INVENTORY['isaaclab.utils.warp.kernels'][0]).read_text() + assert 'global_force_at_com_w[ei, bi] = forces[tid_env, tid_body]' in kernels + assert 'local_force_b[ei, bi] = forces[tid_env, tid_body]' in kernels + + +def test_no_call_before_initial_read_and_missing_setter_rejected(setup): + with setup() as f: + initial = f.observer.supplemental(f.s) + assert initial == f.evidence[0][1] + assert f.s.robot.permanent_wrench_composer.set_calls == 0 + with f.hooks.wrench_update(f.s.robot.permanent_wrench_composer): + f.s.schedule.step(f.s.time, f.s.reference()[0], f.s.edge_off, f.s.edge_on, f.s.scenarios, .002) + with pytest.raises(ins.InstrumentationError, match='without completed'): + f.observer.supplemental(f.s) + + +def test_nested_observer_rejected_without_disturbing_original(setup): + with setup() as f: + setter = f.s.robot.permanent_wrench_composer.set_forces_and_torques_index + scheduled = f.s.schedule.step + with pytest.raises(ins.InstrumentationError, match='Nested'), ins.EvaluationInstrumentation( + f.env, backend_rng=backend_rng, generators={}): + pytest.fail('nested observer admitted') + assert f.s.robot.permanent_wrench_composer.set_forces_and_torques_index is setter + assert f.s.schedule.step is scheduled + apply_one(f) + + +def test_exit_restores_existing_guard_before_runtime_unwinds(setup): + with setup() as f: + c = f.s.robot.permanent_wrench_composer + guarded = c.set_forces_and_torques_index.__wrapped__ + f.observer.__exit__(None, None, None) + assert c.set_forces_and_torques_index is guarded + assert 'step' not in vars(f.s.schedule) + with pytest.raises(RuntimeError, match='Only the bound physics action'): + c.set_forces_and_torques_index() + assert f.hooks._load_owner is f.hooks._replacement_owner is None + + +def test_actual_total_includes_nonzero_guide_auxiliary_and_event(setup): + with setup() as f: + s = f.s + s.command_clock = s.protocol.evidence_clock(s.trial.index, 2400) + s.time[:] = 4.8 + s.stiffness[:] = torch.tensor([0., 0., 0., 1., 1., 1.]) + s.damping[:] = 1. + # Frozen root getter mock has zero velocity, so give it a known physical velocity. + root = s.root() + s.root = lambda: (root[0], root[1], torch.tensor([[.1, .2, 0.]]), root[3]) + applied = apply_one(f)['applied'] + assert s.guide.any() and s.auxiliary.any() and s.external.any() + expected = (s.guide + s.auxiliary + s.external)[0] + torch.testing.assert_close(torch.tensor(applied['total_com_wrench']), expected) + + +@pytest.mark.parametrize('field', ['evidence_scope', 'unobserved_internals']) +def test_rng_scope_is_mandatory_and_copied(field): + value = backend_rng(None) + copied = ins.qualify_backend_rng(value) + value[field].clear() + assert copied[field] + with pytest.raises(ins.InstrumentationError): + ins.qualify_backend_rng(value) + + +@pytest.mark.parametrize('replacement', ['not_applicable', 'observed', None, [0]]) +def test_native_unknowns_cannot_be_not_applicable_or_pseudo_state(replacement): + value = backend_rng(None) + value['unobserved_internals']['internal_stochastic_state_if_any'] = replacement + with pytest.raises(ins.InstrumentationError): + ins.qualify_backend_rng(value) + + +def test_complete_backend_or_replay_claim_rejected(): + value = backend_rng(None) + value['evidence_scope']['claims']['deterministic_replay'] = True + with pytest.raises(ins.InstrumentationError): + ins.qualify_backend_rng(value) diff --git a/tests/test_contact_cycle_sim_evaluation_namespaces.py b/tests/test_contact_cycle_sim_evaluation_namespaces.py new file mode 100644 index 0000000..eb5bb6a --- /dev/null +++ b/tests/test_contact_cycle_sim_evaluation_namespaces.py @@ -0,0 +1,162 @@ +"""Real import-machinery PEP 420 fixtures; no Kit imports or installed writes.""" +import importlib +import importlib.machinery +import importlib.util +import sys +from pathlib import Path +from types import ModuleType + +import pytest +from test_contact_cycle_sim_evaluation_binding import binding, load + + +@pytest.fixture +def namespace(tmp_path, monkeypatch): + for name in tuple(sys.modules): + if name.split('.')[0] in ('isaaclab', 'isaaclab_physx', 'SkillFactory'): + monkeypatch.delitem(sys.modules, name) + isaac = tmp_path / 'IsaacLab' + package = isaac / 'source/isaaclab/isaaclab' + directory = package / 'ui' + directory.mkdir(parents=True) + (package / '__init__.py').write_text('') + (directory / 'child.py').write_text('IDENTITY = 1\n') + monkeypatch.setattr(binding, 'ISAAC_ROOT', isaac) + monkeypatch.setattr(binding, 'ISAAC_PINS', {}) + monkeypatch.syspath_prepend(str(package.parent)) + # Register deletions before real imports so monkeypatch restores sys.modules. + for name in ('isaaclab', 'isaaclab.ui', 'isaaclab.ui.child'): + monkeypatch.setitem(sys.modules, name, None) + del sys.modules[name] + module = importlib.import_module('isaaclab.ui') + importlib.import_module('isaaclab.ui.child') + return module, directory + + +def test_namespace_binds_real_loader_locations_and_imported_children(namespace, tmp_path): + module, directory = namespace + assert type(module.__loader__) is importlib.machinery.NamespaceLoader + assert module.__file__ is None + records = binding._imports(set(), tmp_path) + record = records['isaaclab.ui'] + assert record == { + 'kind': 'pep420-namespace', 'spec_name': 'isaaclab.ui', 'origin': None, + 'loader': 'importlib.machinery.NamespaceLoader', 'search_locations': [str(directory)], + 'imported_child_files': {'isaaclab.ui.child': records['isaaclab.ui.child']}} + assert records['isaaclab.ui.child']['sha256'] == binding.sha256((directory / 'child.py').read_bytes()) + (directory / 'unimported.py').write_text('not bound\n') + assert binding._imports(set(), tmp_path) == records + (directory / 'child.py').write_text('IDENTITY = 2\n') + assert binding._imports(set(), tmp_path) != records + assert binding.FORMAT.endswith('.v5') + # Frozen file-only helper remains strict (including callers in symbol checks). + with pytest.raises(ValueError, match='no real __file__'): + binding._module_file('isaaclab.ui') + + +@pytest.mark.parametrize('change', [ + 'name', 'origin', 'loader', 'spec_loader', 'spec_path', 'list_path', + 'foreign_path', 'empty_path', 'file', 'package', 'regular_package', 'missing_dir', + 'symlink', 'synthetic', 'forged_spec', 'alias', 'path_alias', 'child_elsewhere', + 'parent_path', 'path_name', 'path_finder', +]) +def test_namespace_rejects_broken_swapped_and_forged_identity(namespace, tmp_path, monkeypatch, change): + module, directory = namespace + if change == 'name': + module.__spec__.name = 'other' + elif change == 'origin': + module.__spec__.origin = 'built-in' + elif change == 'loader': + module.__loader__ = object() + elif change == 'spec_loader': + module.__spec__.loader = object() + elif change == 'spec_path': + module.__spec__.submodule_search_locations = [str(directory)] + elif change == 'list_path': + module.__path__ = module.__spec__.submodule_search_locations = [str(directory)] + module.__loader__._path = module.__path__ + elif change in ('foreign_path', 'empty_path'): + module.__path__._path[:] = [] if change == 'empty_path' else [str(tmp_path)] + elif change == 'file': + module.__file__ = '' + elif change == 'package': + module.__package__ = 'other' + elif change == 'regular_package': + (directory / '__init__.py').write_text('') + elif change == 'missing_dir': + directory.rename(directory.with_name('moved')) + elif change == 'symlink': + moved = directory.with_name('moved') + directory.rename(moved) + directory.symlink_to(moved, target_is_directory=True) + elif change in ('synthetic', 'forged_spec'): + forged = ModuleType('isaaclab.ui') + if change == 'forged_spec': + loader = importlib.machinery.NamespaceLoader('isaaclab.ui', [str(directory)], None) + forged.__loader__ = loader + forged.__spec__ = importlib.machinery.ModuleSpec('isaaclab.ui', loader, is_package=True) + forged.__path__ = forged.__spec__.submodule_search_locations + monkeypatch.setitem(sys.modules, 'isaaclab.ui', forged) + elif change == 'alias': + monkeypatch.setitem(sys.modules, 'foreign_alias', module) + elif change == 'path_alias': + forged = ModuleType('foreign_alias') + forged.__path__ = [str(directory)] + monkeypatch.setitem(sys.modules, 'foreign_alias', forged) + elif change == 'parent_path': + sys.modules['isaaclab'].__path__ = [str(tmp_path)] + elif change == 'path_name': + module.__path__._name = 'other' + elif change == 'path_finder': + module.__path__._path_finder = lambda *_: None + elif change == 'child_elsewhere': + path = directory.parent / 'elsewhere.py' + path.write_text('') + load('isaaclab.ui.child', path, monkeypatch) + with pytest.raises((ValueError, OSError)): + binding._imports(set(), tmp_path) + + +def test_namespace_unapproved_root_and_fileless_provider_rejected(namespace, tmp_path, monkeypatch): + module, _ = namespace + monkeypatch.setitem(sys.modules, 'dynamic_provider', ModuleType('dynamic_provider')) + with pytest.raises(ValueError, match='spec/loader'): + binding._imports({'dynamic_provider'}, tmp_path) + # A genuine namespace loader is insufficient without an approved source root. + outside = tmp_path / 'foreign_ns' + outside.mkdir() + spec = importlib.machinery.PathFinder.find_spec('foreign_ns', [str(tmp_path)]) + foreign = importlib.util.module_from_spec(spec) + monkeypatch.setitem(sys.modules, 'foreign_ns', foreign) + with pytest.raises(ValueError, match='no approved source root'): + binding._imports({'foreign_ns'}, tmp_path) + assert module.__file__ is None + + +def test_import_diagnostics_aggregate_without_hashing_invalid_modules(namespace, tmp_path, monkeypatch): + for name in ('isaaclab.bad_one', 'isaaclab.bad_two'): + monkeypatch.setitem(sys.modules, name, ModuleType(name)) + errors = [] + records = binding._imports(set(), tmp_path, diagnostics=errors) + assert {row['entry'] for row in errors} == {'isaaclab.bad_one', 'isaaclab.bad_two'} + assert not ({'isaaclab.bad_one', 'isaaclab.bad_two'} & records.keys()) + assert 'isaaclab.ui' in records + + +def test_actual_installed_isaaclab_ui_read_only(monkeypatch): + directory = binding.ISAAC_ROOT / 'source/isaaclab/isaaclab/ui' + if not directory.is_dir(): + pytest.skip('Pinned installation not present') + assert not (directory / '__init__.py').exists() + before = {str(p): binding.sha256(p.read_bytes()) for p in directory.rglob('*.py')} + # PathFinder and namespace exec_module execute no installed package code. + parent_spec = importlib.util.spec_from_file_location('isaaclab', directory.parent / '__init__.py') + monkeypatch.setitem(sys.modules, 'isaaclab', importlib.util.module_from_spec(parent_spec)) + spec = importlib.machinery.PathFinder.find_spec('isaaclab.ui', [str(directory.parent)]) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + monkeypatch.setitem(sys.modules, 'isaaclab.ui', module) + record = binding._namespace_identity('isaaclab.ui', Path.cwd()) + assert record['search_locations'] == [str(directory)] + assert record['imported_child_files'] == {} # not an entire-tree binding claim + assert before == {str(p): binding.sha256(p.read_bytes()) for p in directory.rglob('*.py')} diff --git a/tests/test_contact_cycle_sim_evaluation_protocol.py b/tests/test_contact_cycle_sim_evaluation_protocol.py new file mode 100644 index 0000000..a825cdb --- /dev/null +++ b/tests/test_contact_cycle_sim_evaluation_protocol.py @@ -0,0 +1,302 @@ +"""CPU-only protocol tests: these are not simulator or development-gate evidence.""" + +import copy +import dataclasses +import json +import math +import subprocess +import sys +from collections import Counter +from pathlib import Path + +import pytest +import torch +from SkillFactory.legs.contact_cycle.mechanics import inverse_rotate +from SkillFactory.legs.contact_cycle.task import command as frozen_command +from SkillFactory.legs.contact_cycle_sim.evaluation_protocol import ( + CYCLE_TICKS, + EVIDENCE_PHASE_COUNTS, + PHYSICS_DT_S, + Clock, + load_protocol, + validate_protocol, +) + +ROOT = Path(__file__).resolve().parents[1] +PATHS = {"P1": ROOT / "configs/contact_cycle/continuous_eval_p1_v1.json", + "P2": ROOT / "configs/contact_cycle/bounded_diagnostic_p2_v1.json"} +ORIGINAL = json.loads((ROOT / "configs/contact_cycle/go2_fl_v1.json").read_text()) +A = (0.12, -0.07, 0.022) +PORT = (0.02, 0.03, 0.30) + + +def sample(protocol, clock, trial=0, **kwargs): + return protocol.command(trial, clock, anchor_a_world=A, root_origin_world=PORT, weight_N=40.0, **kwargs) + + +@pytest.mark.parametrize("stage,rows,ticks", [("P1", 2, 13500), ("P2", 20, 4500)]) +def test_evaluation_protocol_schema_and_immutable_trials(stage, rows, ticks): + raw = json.loads(PATHS[stage].read_text()) + protocol = validate_protocol(raw) + assert len(protocol.trials) == rows + assert all(row.total_ticks == ticks for row in protocol.trials) + assert protocol.document["execution_authorized"] is False + assert protocol.document["accepted_skill"] is False + assert protocol.document["optimizer_steps"] == 0 + raw["clock"]["cycle_ticks"] = 7 + assert protocol.document["clock"]["cycle_ticks"] == 4500 + with pytest.raises(TypeError): + protocol.document["clock"]["cycle_ticks"] = 7 + with pytest.raises(dataclasses.FrozenInstanceError): + protocol.trials[0].seed = 0 + with pytest.raises(dataclasses.FrozenInstanceError): + protocol.trials = () + + +def test_evaluation_protocol_exact_preregistered_order_and_pairing(): + p1, p2 = load_protocol(PATHS["P1"]), load_protocol(PATHS["P2"]) + controllers = ("learned_policy", "analytic_reference") + conditions = ("nominal", "oblique_sustained", "eccentric_liftoff", "landing_combination", "target_only") + assert [(r.seed, r.controller_kind) for r in p1.trials] == [(93501, c) for c in controllers] + assert [(r.condition_id, r.seed, r.controller_kind) for r in p2.trials] == [ + (condition, seed, controller) + for condition in conditions for seed in (94501, 94502) for controller in controllers + ] + assert len({r.trial_id for r in p2.trials}) == 20 + for learned, reference in zip(p2.trials[::2], p2.trials[1::2]): + assert learned.pair_id == reference.pair_id + assert dataclasses.replace(learned, index=reference.index, trial_id=reference.trial_id, + controller_kind=reference.controller_kind) == reference + assert [p2.trials[i].event_indices for i in (0, 4, 8, 12, 16)] == [(), (0,), (1,), (2, 3), ()] + assert p2.trials[16].swing_displacement_m == (0.030, 0.025, 0.0) + assert p1.document["events"] == () + assert json.loads(json.dumps(ORIGINAL["events"])) == json.loads(PATHS["P2"].read_text())["events"] + + +def test_evaluation_protocol_frozen_threshold_provenance_and_budget(): + p1, p2 = load_protocol(PATHS["P1"]), load_protocol(PATHS["P2"]) + functional = json.loads((ROOT / "configs/contact_cycle/simulation_functional_v1.json").read_text()) + assert p1.document["thresholds"]["functional"] == functional["gates"] + assert p2.document["thresholds"]["P1_margins_role"] == "diagnostic_only" + assert p1.document["thresholds"]["P1_margins_role"] == "gate" + assert p1.document["thresholds"]["P1_margins"] == p2.document["thresholds"]["P1_margins"] + assert p2.document["requires_P1_evidence_review"] is True + limits = p1.document["resources"] + assert limits == p2.document["resources"] + assert sum(r.total_ticks for r in (*p1.trials, *p2.trials)) == limits["max_rollout_ticks"] == 117000 + assert limits["max_rollout_ticks"] + limits["max_initialization_reset_ticks"] == 120000 + assert limits["max_sim_step_ticks_including_initialization"] == 120000 + assert limits["max_launches"] == 3 + assert limits["aggregate_same_boot_window_s"] == 3600 + assert limits["max_artifact_bytes"] == 2**30 + assert limits["cleanup_reserve_s"] == 120 + assert limits["max_control_steps"] == 117000 // 5 + + +def _leaves(value, prefix=()): + if isinstance(value, dict): + for key, item in value.items(): + yield from _leaves(item, (*prefix, key)) + elif isinstance(value, list): + for index, item in enumerate(value): + yield from _leaves(item, (*prefix, index)) + else: + yield prefix, value + + +@pytest.mark.parametrize("stage", ["P1", "P2"]) +def test_evaluation_protocol_every_leaf_is_locked_and_nonfinite_fails_closed(stage): + raw = json.loads(PATHS[stage].read_text()) + for path, value in _leaves(raw): + bad_values = [None] + if type(value) in (int, float): + bad_values += [float("nan"), float("inf"), -float("inf"), True, -1, value + 0.001] + for bad in bad_values: + changed = copy.deepcopy(raw) + parent = changed + for key in path[:-1]: + parent = parent[key] + parent[path[-1]] = bad + with pytest.raises(ValueError): + validate_protocol(changed) + + +@pytest.mark.parametrize("bad", [None, [], {}, {"stage": "P3"}, {"stage": []}]) +def test_evaluation_protocol_invalid_root(bad): + with pytest.raises(ValueError): + validate_protocol(bad) + + +def test_evaluation_protocol_unknown_missing_and_reordered_values_fail(tmp_path): + for mutate in ( + lambda d: d.update(unknown=True), lambda d: d.pop("resources"), + lambda d: d["resources"].update(unknown=1), lambda d: d["seeds"].reverse(), + lambda d: d["conditions"].pop(), lambda d: d["clock"]["phase_ticks"].append(1), + lambda d: d["events"][0].update(force_N=[30.0, -4.0, -2.0]), + ): + raw = json.loads(PATHS["P2"].read_text()) + mutate(raw) + with pytest.raises(ValueError): + validate_protocol(raw) + path = tmp_path / "duplicate.json" + path.write_text('{"stage": "P1", "stage": "P2"}') + with pytest.raises(ValueError, match="Duplicate"): + load_protocol(path) + path.write_text(PATHS["P1"].read_text().replace('"physics_dt_s": 0.002', '"physics_dt_s": NaN')) + with pytest.raises(ValueError): + load_protocol(path) + + +@pytest.mark.parametrize("dtype", [torch.float32, torch.float64]) +def test_evaluation_protocol_first_cycle_matches_frozen_command_every_tick(dtype): + protocol = load_protocol(PATHS["P1"]) + time = torch.arange(4501, dtype=dtype) * PHYSICS_DT_S + origin = torch.tensor(A, dtype=dtype).expand(4501, -1) + expected = frozen_command(time, ORIGINAL, origin, torch.full_like(time, 40.0)) + actual = [sample(protocol, Clock(t, 0, t)) for t in range(4501)] + assert torch.equal(torch.tensor([c.phase for c in actual]), expected[0]) + for field, reference in zip(("progress", "target_world_m", "desired_normal_N", "auxiliary_fraction"), expected[1:]): + torch.testing.assert_close(torch.tensor([getattr(c, field) for c in actual], dtype=dtype), reference, + rtol=1e-5, atol=3e-5 if dtype == torch.float32 else 1e-12) + + +def test_evaluation_protocol_exact_phase_counts_right_closed_endpoints(): + protocol = load_protocol(PATHS["P1"]) + counts = [Counter() for _ in range(3)] + for tick in range(1, 13501): + clock = protocol.evidence_clock(0, tick) + assert clock.global_tick == clock.cycle_index * CYCLE_TICKS + clock.local_tick + counts[clock.cycle_index][sample(protocol, clock).phase] += 1 + assert [tuple(c[p] for p in range(6)) for c in counts] == [EVIDENCE_PHASE_COUNTS] * 3 + for tick, cycle, local in ((4499, 0, 4499), (4500, 0, 4500), (4501, 1, 1), + (9000, 1, 4500), (9001, 2, 1), (13500, 2, 4500)): + assert protocol.evidence_clock(0, tick) == Clock(tick, cycle, local) + for tick, phase in ((1499, 0), (1500, 1), (1999, 1), (2000, 2), (2299, 2), (2300, 3), + (2999, 3), (3000, 4), (3399, 4), (3400, 5), (4500, 5)): + c = sample(protocol, Clock(tick, 0, tick)) + assert c.phase == phase + assert protocol.evidence_clock(0, 4500).local_time_s == 9.0 + assert protocol.evidence_clock(0, 13500).global_time_s == 27.0 + + +@pytest.mark.parametrize("tick", [4500, 9000]) +def test_evaluation_protocol_seam_requires_admission_and_preserves_command_continuity(tick): + protocol = load_protocol(PATHS["P1"]) + old = sample(protocol, protocol.evidence_clock(0, tick)) + with pytest.raises(ValueError, match="endpoint must pass"): + protocol.next_command_clock(0, tick) + clock = protocol.next_command_clock(0, tick, seam_admitted=True) + new = sample(protocol, clock) + assert old.phase == 5 and old.progress == 1.0 + assert new.phase == 0 and new.progress == 0.0 + assert old.end_anchor == new.start_anchor + assert old.target_world_m == new.target_world_m + assert old.desired_normal_N == new.desired_normal_N == 40.0 + assert old.auxiliary_fraction == new.auxiliary_fraction == 0.0 + assert old.target_velocity_world_m_s == new.target_velocity_world_m_s == (0.0, 0.0, 0.0) + assert old.root_target_world_m == new.root_target_world_m == PORT + assert old.root_velocity_world_m_s == new.root_velocity_world_m_s == (0.0, 0.0, 0.0) + assert protocol.evidence_clock(0, tick + 1).local_tick == 1 + + +def test_evaluation_protocol_fixed_anchors_reverse_and_repeat_without_state_or_rng_writes(): + protocol = load_protocol(PATHS["P1"]) + rng = torch.get_rng_state().clone() + anchors, root = list(A), list(PORT) + args = {"anchor_a_world": anchors, "root_origin_world": root, "weight_N": 40.0} + endpoints = [protocol.command(0, protocol.evidence_clock(0, t), **args) for t in (4500, 9000, 13500)] + assert endpoints[0].target_world_m == endpoints[2].target_world_m + assert endpoints[1].target_world_m == A + assert [c.start_anchor + c.end_anchor for c in endpoints] == ["AB", "BA", "AB"] + assert anchors == list(A) and root == list(PORT) + assert torch.equal(rng, torch.get_rng_state()) + assert protocol.next_command_clock(0, 0) == Clock(0, 0, 0) + assert protocol.next_command_clock(0, 13500) is None + assert protocol.next_command_clock(1, 13500) is None + assert load_protocol(PATHS["P2"]).next_command_clock(0, 4500) is None + # Mid-swing reverses direction; it does not accumulate a third positive displacement. + first = sample(protocol, Clock(2650, 0, 2650)) + reverse = sample(protocol, Clock(7150, 1, 2650)) + assert reverse.target_world_m == pytest.approx(first.target_world_m) + assert reverse.target_velocity_world_m_s[:2] == pytest.approx( + tuple(-v for v in first.target_velocity_world_m_s[:2])) + + +def test_evaluation_protocol_target_only_is_smooth_and_predeclared(): + protocol = load_protocol(PATHS["P2"]) + start = sample(protocol, Clock(0, 0, 0), trial=16) + endpoint = sample(protocol, Clock(4500, 0, 4500), trial=16) + assert start.target_world_m == A + assert endpoint.target_world_m == pytest.approx(tuple(x + d for x, d in zip(A, (0.030, 0.025, 0.0)))) + assert start.target_velocity_world_m_s == endpoint.target_velocity_world_m_s == (0.0, 0.0, 0.0) + for tick in (2100, 2600, 3200): + before, center, after = [sample(protocol, Clock(t, 0, t), trial=16) for t in (tick - 1, tick, tick + 1)] + derivative = tuple((y - x) / (2 * PHYSICS_DT_S) for x, y in zip(before.target_world_m, after.target_world_m)) + assert center.target_velocity_world_m_s == pytest.approx(derivative, abs=2e-6) + + +def test_evaluation_protocol_world_axis_actor_displacement_is_not_actual_body_ik(): + protocol = load_protocol(PATHS["P1"]) + cmd = sample(protocol, Clock(2600, 0, 2600)) + target = torch.tensor([cmd.target_world_m], dtype=torch.float64) + commanded_port = torch.tensor([PORT], dtype=torch.float64) + actual_root = commanded_port + torch.tensor([[0.01, -0.02, 0.03]], dtype=torch.float64) + q = torch.tensor([[0.0, 0.0, math.sqrt(0.5), math.sqrt(0.5)]], dtype=torch.float64) + actor = torch.tensor([cmd.actor_target_displacement_world_m], dtype=torch.float64) + ik = inverse_rotate(q, target - actual_root) + torch.testing.assert_close(actor, target - commanded_port) + assert not torch.allclose(actor, ik) + torch.testing.assert_close(ik[:, 0], (target - actual_root)[:, 1]) + torch.testing.assert_close(ik[:, 1], -(target - actual_root)[:, 0]) + + +@pytest.mark.parametrize("bad", [-1, 0, 13501, True, 1.0, float("nan"), float("inf")]) +def test_evaluation_protocol_bad_evidence_ticks(bad): + with pytest.raises(ValueError): + load_protocol(PATHS["P1"]).evidence_clock(0, bad) + + +@pytest.mark.parametrize("bad", [-1, 1, 4499, 13501, True, 5.0, float("nan")]) +def test_evaluation_protocol_bad_control_ticks(bad): + with pytest.raises(ValueError): + load_protocol(PATHS["P1"]).next_command_clock(0, bad) + + +@pytest.mark.parametrize("clock", [Clock(1, 0, 0), Clock(0, -1, 4500), Clock(13500, 3, 0), + Clock(4501, 0, 4501), Clock(True, 0, 1), Clock(0, 0, 0.0), None]) +def test_evaluation_protocol_bad_command_clock(clock): + with pytest.raises(ValueError): + sample(load_protocol(PATHS["P1"]), clock) + + +@pytest.mark.parametrize("key,value", [("weight_N", 0), ("weight_N", True), ("weight_N", float("inf")), + ("weight_N", float("nan")), ("anchor_a_world", [0, 0]), + ("anchor_a_world", [0, float("nan"), 0]), + ("root_origin_world", [0, float("inf"), 0]), + ("root_origin_world", [0, True, 0])]) +def test_evaluation_protocol_bad_command_parameters(key, value): + args = {"weight_N": 40.0, "anchor_a_world": A, "root_origin_world": PORT} + args[key] = value + with pytest.raises(ValueError): + load_protocol(PATHS["P1"]).command(0, Clock(0, 0, 0), **args) + + +@pytest.mark.parametrize("index", [-1, 2, True, 0.0]) +def test_evaluation_protocol_bad_trial_index(index): + with pytest.raises(ValueError): + load_protocol(PATHS["P1"]).trial(index) + + +def test_evaluation_protocol_import_and_load_need_only_stdlib_no_runtime(): + path = ROOT / "source/SkillFactory/SkillFactory/legs/contact_cycle_sim/evaluation_protocol.py" + script = f""" +import importlib.util, sys +spec = importlib.util.spec_from_file_location('pure_protocol', {str(path)!r}) +module = importlib.util.module_from_spec(spec) +sys.modules[spec.name] = module +spec.loader.exec_module(module) +protocol = module.load_protocol({str(PATHS['P1'])!r}) +assert len(protocol.trials) == 2 +assert not any(name.split('.')[0] in ('torch', 'isaaclab', 'isaacsim', 'omni') for name in sys.modules) +""" + subprocess.run([sys.executable, "-I", "-S", "-c", script], check=True) diff --git a/tests/test_contact_cycle_sim_evaluation_provenance.py b/tests/test_contact_cycle_sim_evaluation_provenance.py new file mode 100644 index 0000000..6714fc7 --- /dev/null +++ b/tests/test_contact_cycle_sim_evaluation_provenance.py @@ -0,0 +1,421 @@ +"""CPU provenance fixtures; no Kit launch or installed-tree mutation.""" +import ast +import importlib +import importlib.machinery +import importlib.util +import sys +from pathlib import Path +from types import CodeType, FunctionType, ModuleType, SimpleNamespace + +import pytest +from test_contact_cycle_sim_evaluation_binding import ROOT, binding, load, replay +from test_contact_cycle_sim_evaluation_namespaces import namespace # noqa: F401 + + +@pytest.fixture +def installed_links(tmp_path, monkeypatch): + physical = tmp_path / 'sim-6' + physical.mkdir() + source = physical / 'module.py' + source.write_text('VALUE = 1\n') + intermediate, alias = tmp_path / 'sim', tmp_path / '_sim' + intermediate.symlink_to(physical) + alias.symlink_to(intermediate) + links = tuple((str(p), str(p.readlink()), p.lstat().st_dev, p.lstat().st_ino) + for p in (alias, intermediate)) + monkeypatch.setattr(binding, 'SIM_ROOT', physical) + monkeypatch.setattr(binding, 'RUNTIME_LINKS', links) + return alias, intermediate, source + + +def test_installed_alias_chain_binds_raw_physical_links_and_source(installed_links, monkeypatch): + alias, intermediate, source = installed_links + module = load('_runtime_alias_cpu', alias / source.name, monkeypatch) + module.__spec__.origin = str(source) + record = binding._dependency_module_identity(module.__name__) + assert record['raw_path'] == str(alias / source.name) + assert record['path'] == str(source) + assert record['sha256'] == binding.sha256(source.read_bytes()) + assert [link['path'] for link in record['links']] == [str(alias), str(intermediate)] + assert all(link['target_sha256'] == binding.sha256(link['target'].encode()) for link in record['links']) + assert record['origin']['raw_path'] == str(source) + with pytest.raises(ValueError, match='Noncanonical'): + binding._file(alias / source.name) # project/config helper remains strict + + +@pytest.mark.parametrize('change', ['target', 'inode', 'foreign', 'extra-link', 'dot', 'origin']) +def test_installed_alias_rejects_unbound_paths(installed_links, monkeypatch, tmp_path, change): + alias, intermediate, source = installed_links + module = load('_runtime_alias_cpu', alias / source.name, monkeypatch) + if change == 'target': + alias.unlink() + alias.symlink_to(source.parent) + elif change == 'inode': + monkeypatch.setattr(binding, 'RUNTIME_LINKS', tuple((p, t, d, i + 1) for p, t, d, i in binding.RUNTIME_LINKS)) + elif change == 'foreign': + monkeypatch.setattr(binding, 'SIM_ROOT', tmp_path / 'foreign') + elif change == 'extra-link': + extra = source.parent / 'extra.py' + extra.symlink_to(source) + module.__file__ = str(alias / extra.name) + elif change == 'dot': + module.__file__ = str(alias) + '/./module.py' + else: + other = tmp_path / 'other.py' + other.write_bytes(source.read_bytes()) + module.__spec__.origin = str(other) + with pytest.raises(ValueError): + binding._dependency_module_identity(module.__name__) + + +def test_inspected_installed_aliases_read_only(): + physical = binding.SIM_ROOT / binding.FAST_IMPORTER_RELATIVE + if not physical.exists(): + pytest.skip('inspected installation absent') + record = binding._runtime_path(Path('/home/cen/IsaacLab/_isaac_sim') / binding.FAST_IMPORTER_RELATIVE) + assert record['sha256'] == binding.FAST_IMPORTER_SHA256 + assert len(record['links']) == 2 + assert record['path'] == str(physical) + + +@pytest.fixture +def fast_namespace(namespace, monkeypatch): # noqa: F811 + module, directory = namespace + path = binding.SIM_ROOT / binding.FAST_IMPORTER_RELATIVE + if not path.exists(): + pytest.skip('inspected installation absent') + # Execute only the installed FastFinder method, with CPU-only cache fixtures. + # Its function code is still independently compared against the full source. + spec = importlib.util.spec_from_file_location(binding.FAST_IMPORTER, path) + importer = importlib.util.module_from_spec(spec) + importer._bootstrap = importlib.import_module('_frozen_importlib') + importer._bootstrap_external = importlib.import_module('_frozen_importlib_external') + importer._cache = SimpleNamespace(modules={}, namespaces={module.__name__: [str(directory)]}) + compiled = compile(path.read_bytes(), str(path), 'exec', dont_inherit=True) + cls_code = next(c for c in compiled.co_consts if isinstance(c, CodeType) and c.co_name == 'FastFinder') + method_code = next(c for c in cls_code.co_consts if isinstance(c, CodeType) and c.co_name == 'find_spec') + importer.FastFinder = type('FastFinder', (), { + 'find_spec': classmethod(FunctionType(method_code, vars(importer), argdefs=(None, None)))}) + monkeypatch.setitem(sys.modules, binding.FAST_IMPORTER, importer) + monkeypatch.setattr(sys, 'meta_path', [importer.FastFinder, *sys.meta_path]) + locations = importer.FastFinder.find_spec(module.__name__, [str(directory.parent)]).submodule_search_locations + module.__path__ = module.__spec__.submodule_search_locations = module.__loader__._path = locations + return module, directory, importer + + +def test_actual_pinned_fastfinder_code_and_duplicate_paths(fast_namespace, tmp_path): + module, directory, _ = fast_namespace + record = binding._namespace_identity(module.__name__, tmp_path) + assert record['importer']['kind'] == 'kit-fastfinder' + assert record['importer']['source']['sha256'] == binding.FAST_IMPORTER_SHA256 + assert record['search_locations'] == [str(directory)] + assert record['raw_search_locations'] == [str(directory), str(directory)] + + +@pytest.mark.parametrize('change', ['callback', 'pin', 'meta-path', 'foreign-location', 'source']) +def test_fastfinder_rejects_changed_provenance(fast_namespace, tmp_path, monkeypatch, change): + module, directory, importer = fast_namespace + if change == 'callback': + module.__path__._path_finder = lambda *args: None + elif change == 'pin': + monkeypatch.setattr(binding, 'FAST_IMPORTER_SHA256', '0' * 64) + elif change == 'meta-path': + monkeypatch.setattr(sys, 'meta_path', [v for v in sys.meta_path if v is not importer.FastFinder]) + elif change == 'foreign-location': + module.__path__._path.append(str(tmp_path)) + else: + other = tmp_path / 'fast_importer.py' + other.write_bytes(Path(importer.__file__).read_bytes()) + importer.__file__ = importer.__spec__.origin = str(other) + with pytest.raises(ValueError): + binding._namespace_identity(module.__name__, tmp_path) + + +def test_multiprocessing_alias_requires_real_main_object(tmp_path, monkeypatch): + monkeypatch.setattr(binding, 'ISAAC_PINS', {}) + for name in tuple(sys.modules): + if name.split('.')[0] in ('SkillFactory', 'isaaclab', 'isaaclab_physx'): + monkeypatch.delitem(sys.modules, name) + path = tmp_path / 'entry.py' + path.write_text('VALUE = 1\n') + main = load('__main__', path, monkeypatch) + monkeypatch.setitem(sys.modules, '__mp_main__', main) + record = binding._imports({'__main__'}, tmp_path)['__main__'] + assert record['standard_aliases']['__mp_main__']['same_module_object'] + duplicate = ModuleType('__main__') + duplicate.__file__ = str(path) + monkeypatch.setitem(sys.modules, '__mp_main__', duplicate) + with pytest.raises(ValueError, match='Unbound imported-path alias'): + binding._imports({'__main__'}, tmp_path) + monkeypatch.setitem(sys.modules, '__mp_main__', main) + monkeypatch.setitem(sys.modules, 'arbitrary_alias', main) + with pytest.raises(ValueError, match='Unbound imported-path alias'): + binding._imports({'__main__'}, tmp_path) + + +def make_pxr_wrapper(package, short_name, monkeypatch): + path = package / short_name / '__init__.py' + path.parent.mkdir(parents=True) + path.write_text('# CPU wrapper fixture\n') + name = 'pxr.' + short_name + wrapper = load(name, path, monkeypatch) + native_name = name + '._' + short_name[0].lower() + short_name[1:] + native_path = path.parent / (native_name.rsplit('.', 1)[1] + '.so') + native_path.write_bytes(b'CPU fixture only; never dlopen') + native = ModuleType(native_name) + native.__file__ = str(native_path) + native.__spec__ = importlib.util.spec_from_file_location(native_name, native_path) + native.__loader__ = native.__spec__.loader + monkeypatch.setitem(sys.modules, native_name, native) + wrapper.__spec__, wrapper.__loader__ = native.__spec__, native.__loader__ + return wrapper, native + + +@pytest.fixture(params=['Usd', 'PhysxSchema', 'PhysicsSchemaTools']) +def pxr_wrapper(request, tmp_path, monkeypatch): + monkeypatch.setattr(binding, 'SIM_ROOT', tmp_path) + libs = tmp_path / 'extscache/omni.usd.libs-pinned/pxr' + schema = tmp_path / 'extscache/omni.usd.schema.physx-pinned/pxr' + manifests = {} + for package in (libs, schema): + manifest = package.parent / 'config/extension.toml' + manifest.parent.mkdir(parents=True) + manifest.write_text('# owner: ' + package.parent.name + '\n[[python.module]]\nname = "pxr"\n') + manifests[str(manifest)] = binding.sha256(manifest.read_bytes()) + monkeypatch.setattr(binding, 'RUNTIME_LAYOUT_PINS', manifests) + tf, _ = make_pxr_wrapper(libs, 'Tf', monkeypatch) + wrapper, native = make_pxr_wrapper(libs if request.param == 'Usd' else schema, + request.param, monkeypatch) + monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', { + module.__name__: ('usd-wrapper', module.__file__, binding.sha256(Path(module.__file__).read_bytes())) + for module in (tf, wrapper)}) + return wrapper, native + + +def test_pxr_copied_native_spec_binds_wrapper_and_binary(pxr_wrapper): + wrapper, native = pxr_wrapper + record = binding._dependency_module_identity(wrapper.__name__) + assert record['spec_name'] == native.__name__ + assert record['wrapper']['native']['path'] == native.__file__ + assert record['wrapper']['wrapper_source']['path'] == wrapper.__file__ + tf = sys.modules['pxr.Tf'] + tf_record = record['wrapper']['prepare_module_identity'] + assert tf_record['module'] == 'pxr.Tf' + assert tf_record['observation'] == binding.module_diagnostic('pxr.Tf') + assert tf_record['wrapper_source'] == record['wrapper']['prepare_module_source'] + assert tf_record['wrapper_source']['path'] == tf.__file__ + assert tf_record['native']['path'] == sys.modules['pxr.Tf._tf'].__file__ + assert tf_record['native']['sha256'] == binding.sha256(Path(sys.modules['pxr.Tf._tf'].__file__).read_bytes()) + assert tf_record['extension_manifest']['path'] == str(Path(tf.__file__).parents[2] / 'config/extension.toml') + if wrapper.__name__ != 'pxr.Usd': + assert tf_record['extension_manifest']['path'] != record['wrapper']['extension_manifest']['path'] + assert not (Path(wrapper.__file__).parents[1] / 'Tf').exists() + # Tf's self-use is finite, not recursive or dependent on an invented spec. + assert binding._dependency_module_identity('pxr.Tf')['wrapper']['prepare_module_identity'] == tf_record + + +@pytest.mark.parametrize('change', ['name', 'object', 'loader', 'spec_loader', 'binary', 'wrapper']) +def test_pxr_rejects_foreign_or_replaced_native_identity(pxr_wrapper, monkeypatch, tmp_path, change): + wrapper, native = pxr_wrapper + if change == 'name': + native.__spec__.name = wrapper.__name__ + '._foreign' + elif change == 'object': + wrapper.__spec__ = importlib.util.spec_from_file_location(native.__name__, native.__file__) + elif change == 'loader': + wrapper.__loader__ = object() + elif change == 'spec_loader': + native.__spec__.loader = importlib.machinery.ExtensionFileLoader(native.__name__, native.__file__) + elif change == 'binary': + foreign = tmp_path / '_foreign.so' + foreign.write_bytes(b'foreign') + native.__file__ = native.__spec__.origin = str(foreign) + else: + foreign = tmp_path / '__init__.py' + foreign.write_bytes(Path(wrapper.__file__).read_bytes()) + wrapper.__file__ = str(foreign) + with pytest.raises(ValueError): + binding._dependency_module_identity(wrapper.__name__) + + +@pytest.mark.parametrize('change', [ + 'missing', 'swapped', 'foreign', 'source_missing', 'source_changed', + 'native_missing', 'native_foreign', 'spec_replaced', 'loader_replaced', + 'manifest_missing', 'manifest_changed', 'manifest_unpinned', 'manifest_swapped', 'manifest_foreign', + 'schema_manifest_missing', 'schema_manifest_changed', +]) +def test_pxr_rejects_missing_swapped_foreign_tf_and_manifests(pxr_wrapper, monkeypatch, tmp_path, change): + wrapper, _ = pxr_wrapper + tf, native = sys.modules['pxr.Tf'], sys.modules['pxr.Tf._tf'] + path = Path(tf.__file__) + if change == 'missing': + monkeypatch.delitem(sys.modules, 'pxr.Tf') + elif change == 'swapped': + monkeypatch.setitem(sys.modules, 'pxr.Tf', wrapper) + elif change == 'foreign': + # Identical bytes under even the pinned schema owner cannot replace Tf. + foreign = tmp_path / 'extscache/omni.usd.schema.physx-pinned/pxr/Tf/__init__.py' + foreign.parent.mkdir(parents=True) + foreign.write_bytes(path.read_bytes()) + tf.__file__ = str(foreign) + elif change == 'source_missing': + path.unlink() + elif change == 'source_changed': + path.write_text('# changed Tf\n') + elif change == 'native_missing': + monkeypatch.delitem(sys.modules, native.__name__) + elif change == 'native_foreign': + foreign = tmp_path / '_tf.so' + foreign.write_bytes(Path(native.__file__).read_bytes()) + native.__file__ = native.__spec__.origin = native.__loader__.path = str(foreign) + elif change == 'spec_replaced': + tf.__spec__ = importlib.util.spec_from_file_location(native.__name__, native.__file__) + elif change == 'loader_replaced': + tf.__loader__ = object() + else: + owner = Path(wrapper.__file__) if change.startswith('schema_') else path + manifest = owner.parents[2] / 'config/extension.toml' + if change.endswith('missing'): + manifest.unlink() + elif change.endswith('changed'): + manifest.write_text('# changed owner\n') + elif change.endswith('swapped'): + other = next(Path(p) for p in binding.RUNTIME_LAYOUT_PINS if p != str(manifest)) + manifest.write_bytes(other.read_bytes()) + elif change.endswith('foreign'): + foreign = tmp_path / 'foreign-extension.toml' + foreign.write_bytes(manifest.read_bytes()) + manifest.unlink() + manifest.symlink_to(foreign) + else: + monkeypatch.delitem(binding.RUNTIME_LAYOUT_PINS, str(manifest)) + with pytest.raises((ValueError, FileNotFoundError)): + binding._dependency_module_identity(wrapper.__name__) + + +def test_pxr_tf_binds_actual_raw_alias_not_schema_adjacent_path(pxr_wrapper, monkeypatch, tmp_path): + wrapper, _ = pxr_wrapper + tf = sys.modules['pxr.Tf'] + physical = Path(tf.__file__) + alias = tmp_path / 'usd-libs-alias' + alias.symlink_to(physical.parents[2]) + monkeypatch.setattr(binding, 'RUNTIME_LINKS', ( + (str(alias), str(alias.readlink()), alias.lstat().st_dev, alias.lstat().st_ino),)) + tf.__file__ = str(alias / 'pxr/Tf/__init__.py') + record = binding._dependency_module_identity(wrapper.__name__)['wrapper'] + source = record['prepare_module_source'] + assert source['raw_path'] == tf.__file__ + assert source['path'] == str(physical) + assert source['links'][0]['path'] == str(alias) + assert record['prepare_module_identity']['wrapper_source'] == source + + +def test_installed_tf_prepare_module_copies_native_spec_loader_read_only(): + # Execute only the inspected pure-Python definition, never import pxr/Kit or dlopen. + _, filename, digest = binding.REQUIRED_RUNTIME['pxr.Tf'] + source = Path(filename).read_bytes() + assert binding.sha256(source) == digest + tree = ast.parse(source) + prepare = next(node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name == 'PrepareModule') + definitions = {} + exec(compile(ast.Module(body=[prepare], type_ignores=[]), filename, 'exec'), definitions) + native = ModuleType('pxr.Tf._tf') + native.__file__ = '/not-loaded/_tf.so' + native.__spec__ = importlib.util.spec_from_file_location(native.__name__, native.__file__) + native.__loader__ = native.__spec__.loader + result = {'__name__': 'pxr.Tf', '__file__': filename, '__package__': 'pxr.Tf', '__path__': ['original']} + definitions['PrepareModule'](native, result) + assert result['__spec__'] is native.__spec__ + assert result['__loader__'] is native.__loader__ + assert result['__name__'] == result['__package__'] == 'pxr.Tf' + assert result['__file__'] == filename and result['__path__'] == ['original'] + assert any(isinstance(node, ast.Expr) and isinstance(node.value, ast.Call) + and isinstance(node.value.func, ast.Name) and node.value.func.id == 'PreparePythonModule' + for node in tree.body) + + +@pytest.fixture(params=['omni', 'usdrt', 'omni.metrics.assembler.core.bindings', + 'omni.metrics.assembler.core.scripts', 'omni.kit', 'usdrt.internal.namespace']) +def provider(request, tmp_path, monkeypatch): + name = request.param + for key in tuple(sys.modules): + if key.split('.')[0] in ('omni', 'usdrt'): + monkeypatch.delitem(sys.modules, key) + monkeypatch.setattr(binding, 'SIM_ROOT', tmp_path) + extension = tmp_path / 'exts/provider' + directory = extension.joinpath(*name.split('.')) + directory.mkdir(parents=True) + (extension / 'config').mkdir() + manifest = extension / 'config/extension.toml' + manifest.write_text('[[python.module]]\nname = "' + name + '.child"\n') + (directory / 'child.py').write_text('VALUE = 1\n') + monkeypatch.syspath_prepend(str(extension)) + for child in ['.'.join(name.split('.')[:i]) for i in range(1, len(name.split('.')) + 1)] + [name + '.child']: + monkeypatch.setitem(sys.modules, child, None) + del sys.modules[child] + module = importlib.import_module(name) + importlib.import_module(name + '.child') + return name, module, manifest + + +def test_explicit_provider_records_manifest_children_and_limitation(provider): + name, module, manifest = provider + record = binding._dependency_module_identity(name) + assert record['kind'] == 'kit-provider-namespace' + assert record['implementation_identity'] == 'unobserved dynamic-provider internals' + assert record['imported_child_files'][name + '.child']['sha256'] + assert record['extension_manifests'][str(manifest)]['sha256'] + assert 'sha256' not in record and not module.__file__ + manifest.write_text('# changed manifest\n') + assert record != binding._dependency_module_identity(name) + + +@pytest.mark.parametrize('change', ['loader', 'manifest', 'child', 'foreign', 'forged-path', 'changed-location']) +def test_provider_rejects_unbound_evidence(provider, monkeypatch, tmp_path, change): + name, module, manifest = provider + if change == 'loader': + module.__loader__ = object() + elif change == 'manifest': + manifest.unlink() + elif change == 'child': + monkeypatch.delitem(sys.modules, name + '.child') + elif change == 'forged-path': + module.__path__ = module.__spec__.submodule_search_locations = module.__loader__._path = list(module.__path__) + elif change == 'changed-location': + other = tmp_path / 'foreign' + other.mkdir() + module.__path__._path[:] = [str(other)] + else: + monkeypatch.setattr(binding, 'SIM_ROOT', tmp_path / 'foreign') + with pytest.raises(ValueError): + binding._dependency_module_identity(name) + + +def test_frozen_verifier_real_artifacts_unchanged(): + if not (ROOT / replay.CANDIDATE).is_file(): + pytest.skip('frozen artifacts absent; never synthesize a candidate') + manifest, record = replay.verify_bundle(ROOT) + assert record['candidate_sha256'] == replay.CANDIDATE_SHA256 + assert record['manifest_sha256'] == replay.MANIFEST_SHA256 + assert record['snapshot_files_verified'] == len(manifest['sources']) + + +def test_unknown_provider_is_not_an_allowlist_skip(tmp_path, monkeypatch): + unknown = ModuleType('omni.unknown') + monkeypatch.setitem(sys.modules, unknown.__name__, unknown) + with pytest.raises(ValueError, match='Unbound provider namespace spec'): + binding._dependency_module_identity(unknown.__name__) + foreign = ModuleType('other_provider') + monkeypatch.setitem(sys.modules, foreign.__name__, foreign) + with pytest.raises(ValueError, match='Unreviewed fileless provider'): + binding._dependency_module_identity(foreign.__name__) + + +def test_diagnostic_does_not_recalculate_namespace_path(namespace): # noqa: F811 + module, directory = namespace + module.__path__._path_finder = lambda *args: pytest.fail('must not rediscover') + before = dict(vars(module.__path__)) + report = binding.module_diagnostic(module.__name__) + assert report['search_locations'] == [str(directory)] + assert report['spec_name'] == module.__name__ + assert vars(module.__path__) == before + binding.canonical_json(report) diff --git a/tests/test_contact_cycle_sim_evaluation_runner.py b/tests/test_contact_cycle_sim_evaluation_runner.py new file mode 100644 index 0000000..ba6b327 --- /dev/null +++ b/tests/test_contact_cycle_sim_evaluation_runner.py @@ -0,0 +1,1621 @@ +"""CPU lifecycle doubles plus one explicitly harmless installed FD exec-chain probe. No Kit.""" +import importlib.util +import json +import sys +from pathlib import Path +from types import SimpleNamespace as NS + +import pytest + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location( + 'evaluation_runner_test_cli', ROOT / 'scripts/evaluate_cycle_sim_residual.py') +cli = importlib.util.module_from_spec(spec) +sys.modules[spec.name] = cli +spec.loader.exec_module(cli) +b = cli.cpu_module('evaluation_budget') +protocols = cli.cpu_module('evaluation_protocol') + + +@pytest.fixture(autouse=True) +def no_real_launch(monkeypatch, request): + if request.node.name == 'test_harmless_installed_launch_probe': + return + def forbidden(*args, **kwargs): + raise AssertionError('No real launcher/process calls in runner CPU tests') + monkeypatch.setattr(cli.subprocess, 'Popen', forbidden) + if not hasattr(cli.os, 'pidfd_open'): + monkeypatch.setattr(cli.os, 'pidfd_open', forbidden, raising=False) + + +@pytest.fixture +def fake_clock(): + value = NS(now=100., boot='test-boot') + value.clock = lambda: value.now + value.boot_fn = lambda: value.boot + return value + + +def reservation(index=0, clock=100.): + ticks, rollout, seconds = b.SLOTS[index] + return {'index': index, 'status': 'reserved', 'reserved_physical_calls': ticks, + 'rollout_cap': rollout, 'initialization_cap': 1000, 'started_boottime': clock, + 'deadline_boottime': clock + seconds, 'grant': {'boot_id': 'test-boot'}} + + +def test_budget_conservative_debits_and_no_resume(tmp_path, fake_clock): + ledger = b.BudgetLedger(tmp_path, 'campaign', clock=fake_clock.clock, boot=fake_clock.boot_fn) + grant = {'launch_index': 0, 'not_after_boottime': 1000} + row = ledger.reserve(grant) + assert row['reserved_physical_calls'] == 14500 and row['observed_counts'] is None + with pytest.raises(RuntimeError, match='retry|reused'): + ledger.reserve(grant) + ledger.close() # simulate crash, no completion record + ledger = b.BudgetLedger(tmp_path, 'campaign', clock=fake_clock.clock, boot=fake_clock.boot_fn) + with pytest.raises(RuntimeError, match='interrupted'): + ledger.reserve({**grant, 'launch_index': 1}) + assert ledger.data['launches'][0]['reserved_physical_calls'] == 14500 + ledger.close() + + +def test_no_overlap_boot_change_or_campaign_substitution(tmp_path, fake_clock): + ledger = b.BudgetLedger(tmp_path, 'campaign', clock=fake_clock.clock, boot=fake_clock.boot_fn) + with pytest.raises(BlockingIOError): + b.BudgetLedger(tmp_path, 'campaign', clock=fake_clock.clock, boot=fake_clock.boot_fn) + ledger.close() + fake_clock.boot = 'different-boot' + with pytest.raises(RuntimeError, match='boot_mismatch'): + b.BudgetLedger(tmp_path, 'campaign', clock=fake_clock.clock, boot=fake_clock.boot_fn) + + +def test_three_slots_reviewed_P2_no_refunds_or_fourth(tmp_path, fake_clock): + ledger = b.BudgetLedger(tmp_path, 'campaign', clock=fake_clock.clock, boot=fake_clock.boot_fn) + for i in range(3): + if i < 2: + (tmp_path / f'launch-{i}').mkdir() + (tmp_path / f'launch-{i}' / 'result').write_text(str(i)) + grant = {'launch_index': i, 'not_after_boottime': 4000, + 'p1_evidence_sha256': [b.tree_digest(tmp_path / f'launch-{j}') for j in (0, 1)] if i == 2 else []} + ledger.reserve(grant) + ledger.finish(counts={'completed_steps': 1}, clean=True, eligible=True) + fake_clock.now += 10 + assert sum(r['reserved_physical_calls'] for r in ledger.data['launches']) == 120000 + assert all(r['observed_counts']['completed_steps'] == 1 for r in ledger.data['launches']) + with pytest.raises(RuntimeError): + ledger.reserve({'launch_index': 3}) + ledger.close() + + +def test_cleanup_and_aggregate_boottime_not_prepare_time(tmp_path, fake_clock): + ledger = b.BudgetLedger(tmp_path, 'campaign', clock=fake_clock.clock, boot=fake_clock.boot_fn) + fake_clock.now = 5000 # coding/preparation time is not charged + row = ledger.reserve({'launch_index': 0, 'not_after_boottime': 10000}) + assert ledger.data['first_run_boottime'] == 5000 + row['grant']['boot_id'] = 'test-boot' + budget = b.LaunchBudget(tmp_path, row, clock=fake_clock.clock, boot=fake_clock.boot_fn) + fake_clock.now = 5480 + with pytest.raises(RuntimeError, match='time'): + budget.check() + budget.check(cleanup=True) + ledger.finish(counts=None, clean=True, eligible=True) + fake_clock.now = 8480 + with pytest.raises(RuntimeError, match='aggregate'): + ledger.reserve({'launch_index': 1, 'not_after_boottime': 10000}) + ledger.close() + + +@pytest.mark.parametrize('mode,init,roll,attempt,allowed', [ + ('construction', 999, 0, 1000, True), ('initialization', 1000, 0, 1001, False), + ('rollout', 0, 13499, 13500, True), ('rollout', 0, 13500, 13501, False), + ('rollout', 999, 13499, 14501, False), +]) +def test_all_initialization_reset_and_rollout_caps(tmp_path, fake_clock, mode, init, roll, attempt, allowed): + budget = b.LaunchBudget(tmp_path, reservation(), clock=fake_clock.clock, boot=fake_clock.boot_fn) + h = NS(mode=mode, initialization_steps=init, rollout_steps=roll, attempted_steps=attempt) + if allowed: + budget.before_step(h) + else: + with pytest.raises(RuntimeError, match='physical_calls'): + budget.before_step(h) + + +def test_exact_pending_artifacts_headroom_signal_and_native_scope(tmp_path, fake_clock, monkeypatch): + monkeypatch.setattr(b, 'CAP_BYTES', 1000) + monkeypatch.setattr(b, 'HEADROOM_BYTES', 100) + monkeypatch.setattr(b, 'STDOUT_ALLOWANCE', 0) + (tmp_path / 'stdout.log').write_bytes(b'x' * 800) + budget = b.LaunchBudget(tmp_path, reservation(), clock=fake_clock.clock, boot=fake_clock.boot_fn) + budget.recording('record', 100, 0) + with pytest.raises(RuntimeError, match='storage'): + budget.recording('record', 101, 0) + budget.recording('finalize', 200, 0) + with pytest.raises(RuntimeError, match='storage'): + budget.recording('finalize', 201, 0) + budget.interrupted = True + with pytest.raises(InterruptedError): + budget.check() + budget.check(cleanup=True) + (tmp_path / 'link').symlink_to(tmp_path / 'stdout.log') + with pytest.raises(RuntimeError, match='nonregular'): + budget.check(cleanup=True) + + +def test_artifact_scan_restarts_after_atomic_publication_not_partial_total(tmp_path, monkeypatch): + target = tmp_path / 'physics-counts.json' + pending = tmp_path / 'physics-counts.json.pending' + target.write_bytes(b'x') + pending.write_bytes(b'y' * 700) + scans = [] + original = Path.lstat + def walk(root, **kwargs): + scans.append(True) + yield str(root), [], [target.name, pending.name] if len(scans) == 1 else [target.name] + def lstat(path, *args, **kwargs): + if path == pending: + pending.replace(target) + raise FileNotFoundError('atomic rename between listing and lstat') + return original(path, *args, **kwargs) + monkeypatch.setattr(b.os, 'walk', walk) + monkeypatch.setattr(Path, 'lstat', lstat) + assert b.artifact_size(tmp_path) == 700 + assert len(scans) == 2 + + +def test_artifact_scan_persistent_churn_is_bounded_and_permission_denied_fails(tmp_path, monkeypatch): + scans = [] + def churn(root, **kwargs): + scans.append(True) + yield str(root), [], ['always-disappeared.pending'] + monkeypatch.setattr(b.os, 'walk', churn) + with pytest.raises(RuntimeError, match='unstable_after_4'): + b.artifact_size(tmp_path) + assert len(scans) == 4 + def inaccessible(root, **kwargs): + kwargs['onerror'](PermissionError('unreadable owned directory')) + yield # generator shape; must never yield a partial success + monkeypatch.setattr(b.os, 'walk', inaccessible) + with pytest.raises(PermissionError, match='unreadable'): + b.artifact_size(tmp_path) + + +@pytest.fixture(params=[1, 2]) +def lifecycle(tmp_path, request): # noqa: C901 - scoped CPU doubles keep lifecycle events in one fixture + """Deterministic environment/recorder boundaries with real runner control flow.""" + events = [] + protocol = protocols.load_protocol(ROOT / cli.PROTOCOLS[request.param]) + cfg = NS(seed=protocol.trial(0).seed, mutation=False) + fault = NS(phase=None, row=0, fatal=False, acknowledge=True, mutate=False) + state = NS(env=None, hooks=None, recorders=[]) + + class Hooks: + def __init__(self, before_step): + self.before_step = before_step + self.attempted_steps = self.completed_steps = self.initialization_steps = self.rollout_steps = 0 + state.hooks = self + + def __enter__(self): + events.append('hooks_enter') + return self + + def step(self, mode): + self.mode = mode + self.attempted_steps += 1 + self.before_step(self) + self.completed_steps += 1 + if mode == 'rollout': + self.rollout_steps += 1 + else: + self.initialization_steps += 1 + + def __exit__(self, *_): + events.append('hooks_exit') + + class Env: + def __init__(self, **kwargs): + assert events[-1] == 'recorder_0' + state.env = self + self.cfg = kwargs['cfg'] + self.index = kwargs['trial_index'] + self.evidence = kwargs['evidence_callback'] + self.stop_result = None + self.hooks = kwargs['runtime_hooks'] + self.acked = False + self.hooks.step('construction') + events.append('construct') + if fault.phase == 'constructor_config': + self.cfg.mutation = True + if fault.phase == 'construct': + raise RuntimeError('constructor fault') + + def reset(self): + events.append(f'reset_{self.index}') + self.hooks.step('initialization') + self.evidence('initial', self) + return {'policy': 'raw46'}, {} + + def step(self, action): + assert action == 'actual_action' + events.append(f'step_{self.index}') + self.hooks.step('rollout') + self.evidence('tick', self) + self.stop_result = NS(fatal=fault.fatal, causes=('functional_fail',), trial_index=self.index) + return NS(stop=self.stop_result, ticks_advanced=1, observation=None, reward=None) + + def acknowledge_finalized(self, stop): + assert stop is self.stop_result + assert events[-1] == f'durable_{self.index}' + self.acked = True + events.append(f'ack_{self.index}') + + def begin_trial(self, proto, index): + assert proto is protocol and index == self.index + 1 and self.acked + assert (tmp_path / f'trial-{self.index:02d}' / 'durable.json').exists() + self.index, self.acked, self.stop_result = index, False, None + return self.reset() + + def close(self): + if 'observer_enter' in events: + assert events[-1] == 'observer_exit' + events.append('env_close') + if fault.mutate: + self.cfg.mutation = True + + class Recorder: + def __init__(self, proto, row, path, *, supplemental, budget_check): + self.row, self.supplemental, self.path = row, supplemental, path + self.audit = NS(report=lambda: self.report()) + path.mkdir() + self.stream = (path / 'partial').open('wb') + events.append(f'recorder_{row}') + state.recorders.append(self) + + def __call__(self, event, env): + assert self.supplemental(env) == 'snapshot' + events.append(f'evidence_{event}_{self.row}') + return () + + def seam_callback(self, event, env): + return () + + def before_control(self, tick, x, action, **kwargs): + assert tick == 0 and x == 'raw46' + events.append(f'control_{self.row}') + return ('recording_bad_control',) if fault.phase == 'control' else () + + def after_step(self, actual): + assert actual is not None and actual.ticks_advanced == 1 and actual.reward is None + events.append(f'after_step_{self.row}') + return ('recording_bad_step',) if fault.phase == 'after_step' else () + + def report(self): + return {'trial': json.loads(json.dumps(cli.asdict(protocol.trial(self.row)))), + 'initial_pair_state': {'mock_initial': self.row if fault.phase == 'pair_identity' else 0}, + 'functional_passed': False, 'all_causes': ['functional_fail']} + + def finalize(self, *, stop, external_failure, acknowledge): + events.append(f'finalize_{self.row}') + if fault.phase == 'finalize': + raise OSError('fsync fault') + b.durable_write(self.path / 'durable.json', b.encode({'external': external_failure})) + self.stream.close() + events.append(f'durable_{self.row}') + if acknowledge and fault.acknowledge and stop is not None: + acknowledge(stop) + return {**self.report(), 'terminal': {'runtime_stop': None if stop is None else vars(stop), + 'external_failure': external_failure}} + + class Observer: + def __init__(self, *args, **kwargs): + pass + + def __enter__(self): + events.append('observer_enter') + return self + + def __exit__(self, *_): + events.append('observer_exit') + + def supplemental(self, state): + assert 'observer_enter' in events + return 'snapshot' + + def infer(x, kind): + events.append('inference') + if fault.phase == 'inference': + raise ValueError('bad inference') + if fault.phase == 'signal': + raise InterruptedError('signal') + return 'actual_action', None, None + + def verify(row): + assert cfg.seed == protocol.trial(row).seed + events.append(f'verify_{row}') + if cfg.mutation or fault.phase == 'identity' and row == fault.row: + raise ValueError('actual config changed') + + def matrix(proto, rows): + lookup = {r['trial']['index']: r for r in rows} + return {'rows': [lookup.get(t.index, {'status': 'not_run'}) for t in proto.trials]} + + api = NS(hooks=Hooks, recorder=Recorder, environment=Env, instrumentation=Observer, + backend_rng=lambda _: None, infer=infer, matrix=matrix) + budget = NS(check=lambda **_: None, recording=lambda *_: None, before_step=lambda _: None) + return NS(api=api, cfg=cfg, protocol=protocol, budget=budget, verify=verify, + events=events, fault=fault, state=state, + run=lambda indices: cli.execute_lifecycle(api, cfg, protocol, indices, tmp_path, budget, verify)) + + +def test_pair_mismatch_stops_before_reference_inference_or_step(lifecycle): + lifecycle.fault.phase = 'pair_identity' + report = lifecycle.run(list(range(20))) + assert report['failures'][0]['context']['message'] == 'identity_pair_initial_state_mismatch' + assert 'reset_1' in lifecycle.events and 'step_1' not in lifecycle.events + assert lifecycle.events.count('inference') == 1 + assert report['rows'][2]['status'] == 'not_run' + assert not report['continuation_eligible'] + + +def test_twenty_rows_continue_only_nonfatal_durable_order(lifecycle): + f = lifecycle + result = f.run(list(range(20))) + assert result['continuation_eligible'] and not result['failures'] + assert not result['complete_backend_state_equality'] and not result['deterministic_replay'] + assert result['equality_scope'] == 'recorded_application_controller_and_enumerated_observable_rng_only' + assert result['observed_counts'] == {'attempted_steps': 41, 'completed_steps': 41, + 'initialization_steps': 21, 'rollout_steps': 20} + assert f.events.count('construct') == 1 and f.events.count('observer_enter') == 1 + assert f.events.index('hooks_enter') < f.events.index('construct') < f.events.index('observer_enter') + for row in range(19): + assert f.events.index(f'durable_{row}') < f.events.index(f'ack_{row}') < f.events.index(f'reset_{row+1}') + assert f.events[-4:] == ['observer_exit', 'env_close', 'hooks_exit', 'verify_19'] + assert not any(row['functional_passed'] for row in result['rows']) # functional fail retained + + +@pytest.mark.parametrize('phase', ['construct', 'inference', 'control', 'after_step', 'finalize', 'signal']) +def test_external_failures_no_fake_actions_ticks_or_continuation(lifecycle, phase): + f = lifecycle + f.fault.phase = phase + result = f.run(list(range(20))) + assert not result['continuation_eligible'] and result['failures'] + assert all(row['status'] == 'not_run' for row in result['rows'][1:]) + if phase not in ('after_step', 'finalize'): + assert 'step_0' not in f.events + if phase == 'finalize': + assert 'ack_0' not in f.events and result['rows'][0]['status'] == 'persistence_failed' + if phase == 'signal': + assert result['failures'][0]['causes'] == ['process_signal_external_failure'] + assert f.events[-1] == 'verify_0' + + +@pytest.mark.parametrize('fatal,ack', [(True, True), (False, False)]) +def test_no_transition_after_fatal_or_unacknowledged(lifecycle, fatal, ack): + lifecycle.fault.fatal, lifecycle.fault.acknowledge = fatal, ack + result = lifecycle.run(list(range(20))) + assert 'reset_1' not in lifecycle.events + assert not result['continuation_eligible'] + + +def test_postcleanup_same_cfg_detects_mutation_no_renormalizing(lifecycle): + lifecycle.fault.mutate = True + actual = lifecycle.cfg + result = lifecycle.run([0]) + assert lifecycle.cfg is actual and actual.mutation + assert not result['continuation_eligible'] + assert result['failures'][-1]['causes'] == ['identity_external_failure'] + + +def test_transition_identity_failure_leaves_later_rows_not_run(lifecycle): + lifecycle.fault.phase, lifecycle.fault.row = 'identity', 1 + result = lifecycle.run(list(range(20))) + assert 'reset_1' not in lifecycle.events and not result['continuation_eligible'] + assert result['rows'][1]['status'] == 'not_run' + + +def test_forwarders_never_silently_invent_missing_snapshots(): + forward = cli.ForwardingEvidence() + for function, args in ((forward, (None, None)), (forward.seam, (None, None)), + (forward.supplemental, (None,))): + with pytest.raises(RuntimeError, match='uninstalled'): + function(*args) + + +def test_check_only_is_stdlib_no_runtime_or_launch(monkeypatch, capsys): + monkeypatch.setattr(cli, 'real_runtime', lambda *_: pytest.fail('Kit must not load')) + assert cli.main(['--check-only']) == 0 + result = json.loads(capsys.readouterr().out) + assert len(result['rows']) == 2 + assert cli.SCRIPT in result['future_sources']['runner'] + assert result['actual_backend_imports_and_performance'] == 'unqualified' + + +def test_prepare_only_normalized_binding_never_constructor(tmp_path, monkeypatch): + events = [] + fake = NS(binding=NS(FORMAT='test-v4', PROVENANCE_SCOPE='development', PROVENANCE_LIMITATIONS=(), + prepare_binding=lambda **_: b'{"imports":{}}', + verify_binding=lambda *_, **__: None, incidental_observations=lambda _: {}), + arguments={'dependency_inventory': {'modules': {}}}, + app=NS(close=lambda **_: events.append('app_close'))) + monkeypatch.setattr(cli, 'real_runtime', lambda *_: fake) + monkeypatch.setattr(cli, 'verify_constructor_export', lambda _: None) + monkeypatch.setattr(cli, 'runtime_metadata', lambda _: {}) + args = NS(output=tmp_path / 'prepare', inventory=tmp_path / 'inventory', trial_index=0, artifact_root=tmp_path) + assert cli.prepare(args) == 0 + assert (args.output / 'binding.json').read_bytes() == b'{"imports":{}}' + report = json.loads((args.output / 'preparation.json').read_bytes()) + assert not report['execution_authorized'] and report['sim_step_calls'] == 0 + assert events == ['app_close'] + + +def test_missing_storage_qualification_blocks_before_reservation(tmp_path, monkeypatch): + monkeypatch.setattr(cli, 'real_runtime', lambda *_: pytest.fail('no runtime')) + args = NS(storage_policy=None, trusted_storage_policy_sha256=None, artifact_root=tmp_path) + with pytest.raises(RuntimeError, match='bound_policy_unavailable'): + cli.supervise(args) + assert list(tmp_path.iterdir()) == [] + + +def trusted_inputs(tmp_path): + root = tmp_path / 'campaign' + root.mkdir() + inventory, manifest = tmp_path / 'inventory.json', tmp_path / 'manifest.json' + inventory.write_text('{}') + manifest.write_text('{}') + review = {'format': 'skillfactory.output-storage-policy.v4', 'artifact_root': str(root), + 'limit_bytes': b.CAP_BYTES, 'scope': b.STORAGE_SCOPE, + 'approval_sha256': cli.STORAGE_APPROVAL_SHA256, 'native_paths': cli.native_paths(root), + 'shared_native_paths': cli.shared_native_paths(), + 'task_owned_cache_paths': cli.task_owned_cache_paths(root), + 'free_space_paths': cli.free_space_paths(root), + 'free_space_mount_identities': {p: cli.mount_identity(Path(p)) for p in cli.free_space_paths(root)}, + 'check_interval_seconds': 1, 'max_native_growth_bytes': b.CAP_BYTES, + 'min_free_bytes': 2 * b.CAP_BYTES, 'coverage_gaps': list(cli.NATIVE_GAPS), + 'native_hard_cap': False, 'boot_id': b.boot_id(), + 'mount_identity': cli.mount_identity(root), 'not_after_boottime': b.boottime() + 3500} + review_path = tmp_path / 'storage.json' + b.durable_write(review_path, b.encode(review)) + args = NS(protocol=cli.PROTOCOLS[0], artifact_root=root, inventory=inventory, manifest=manifest, + device='cpu', trusted_manifest_sha256=b.digest(manifest), storage_policy=review_path, + trusted_storage_policy_sha256=b.digest(review_path), grant=tmp_path / 'grant.json') + grant = {'format': 'skillfactory.evaluation-parent-grant.v2', 'campaign_id': 'CPU-test-only', + 'launch_index': 0, 'stage': 'P1', 'trial_indices': [0], + 'manifest_sha256': args.trusted_manifest_sha256, 'protocol_sha256': b.digest(ROOT / args.protocol), + 'inventory_sha256': b.digest(inventory), 'artifact_root': str(root), 'boot_id': b.boot_id(), + 'not_after_boottime': b.boottime() + 1000, 'parent_review': 'CPU fixture not an actual grant', + 'p1_evidence_sha256': [], 'storage_policy_sha256': args.trusted_storage_policy_sha256, + 'storage_scope': b.STORAGE_SCOPE, + 'launch_identity_sha256': cli.hashlib.sha256(b.encode(cli.launch_identity())).hexdigest()} + b.durable_write(args.grant, b.encode(grant)) + args.trusted_grant_sha256 = b.digest(args.grant) + return args, grant, review + + +@pytest.mark.parametrize('field,value', [('native_hard_cap', True), + ('scope', 'monitored_only'), ('boot_id', 'old'), + ('not_after_boottime', 0), ('mount_identity', 'other')]) +def test_storage_policy_only_trusts_bound_facts_not_capacity(tmp_path, field, value): + args, _, review = trusted_inputs(tmp_path) + review[field] = value + args.storage_policy.write_bytes(b.encode(review)) + args.trusted_storage_policy_sha256 = b.digest(args.storage_policy) + with pytest.raises(RuntimeError, match='storage_policy'): + cli.storage_policy_check(args, b) + + +def test_no_self_trusted_manifest_digest(tmp_path): + args, grant, _ = trusted_inputs(tmp_path) + args.trusted_manifest_sha256 = '0' * 64 + with pytest.raises(ValueError): + cli.grant_check(grant, args, b) + + +def test_failed_popen_is_charged_and_not_retryable(tmp_path, monkeypatch): + args, _, _ = trusted_inputs(tmp_path) + calls = [] + monkeypatch.setattr(cli, 'native_size', lambda path, **_kw: native_scan_result(0, path=path)) + + def failed(*a, **kw): + calls.append(a) + raise OSError('CPU mock Popen failure') + + monkeypatch.setattr(cli.subprocess, 'Popen', failed) + monkeypatch.setattr(cli.ctypes, 'CDLL', lambda *_a, **_kw: NS(prctl=lambda *_: 0)) + assert cli.supervise(args) == 1 + ledger = json.loads((args.artifact_root / 'budget.json').read_bytes()) + assert ledger['launches'][0]['reserved_physical_calls'] == 14500 + assert ledger['launches'][0]['observed_counts'] is None + assert not ledger['launches'][0]['continuation_eligible'] + assert len(calls) == 1 and calls[0][0][0] == '/usr/bin/bash' + assert cli.supervise(args) == 1 + assert len(calls) == 1 # same grant cannot relaunch + + +def test_worker_handoff_requires_live_parent_socket(): + left, right = cli.socket.socketpair() + try: + with pytest.raises(RuntimeError, match='parent_supervisor'): + cli.worker_payload(right.detach()) + finally: + left.close() + right.close() + + +def test_private_worker_requires_reservation_and_single_consumption(tmp_path): + (tmp_path / 'launch-0').mkdir() + row = reservation() + b.durable_write(tmp_path / 'budget.json', b.encode({'launches': [row]})) + b.durable_write(tmp_path / 'launch-0/worker-claim.json', b'consumed') + payload = {'launch_identity': cli.launch_identity(), 'reservation': row, + 'arguments': {'artifact_root': str(tmp_path), 'inventory': 'x', + 'manifest': 'y'}} + row['grant']['trial_indices'] = [0] + # Changed reservation is not authorized by immutable ledger bytes. + with pytest.raises(RuntimeError, match='stale_reservation'): + cli.run_worker(payload) + + +def test_single_row_unacknowledged_is_not_eligible(lifecycle): + lifecycle.fault.acknowledge = False + assert not lifecycle.run([0])['continuation_eligible'] + + +def test_supervisor_three_fresh_processes_bound_logs_and_P2_review(tmp_path, monkeypatch): + args, grant, _ = trusted_inputs(tmp_path) + calls, payloads, cleaned = [], [], [] + + class Channel: + def fileno(self): + return 987 + + def set_inheritable(self, _): + pass + + def close(self): + pass + + class Owned: + supervisor = (1, 2) + known = set() + unknown = False + def attach(self, _): + pass + def discover(self): + return {} + + def exchange(child, channel, payload, *_): + payloads.append(payload) + + def popen(command, **kwargs): + index = len(calls) + calls.append((command, kwargs)) + output = args.artifact_root / f'launch-{index}' + b.durable_write(output / 'result.json', b.encode({ + 'observed_counts': {'attempted_steps': 7, 'completed_steps': 7, + 'initialization_steps': 2, 'rollout_steps': 5}, + 'continuation_eligible': True, 'preclose_identity_verified': True})) + b.durable_write(output / 'preclose-filesystem.json', b.encode({ + 'files': {}, 'frozen_root': str(tmp_path / 'no-frozen'), 'scope': 'CPU fixture'})) + stdout = tmp_path / f'mock-pipe-{index}' + stdout.write_bytes(b'CPU mocked child output\n') + return NS(pid=12345, stdout=stdout.open('rb'), returncode=0, wait=lambda **_: None, poll=lambda: 0) + + monkeypatch.setattr(cli.socket, 'socketpair', lambda: (Channel(), Channel())) + monkeypatch.setattr(cli.subprocess, 'Popen', popen) + monkeypatch.setattr(cli, 'exchange_worker', exchange) + monkeypatch.setattr(cli, 'OwnedProcesses', Owned) + monkeypatch.setattr(cli, 'native_size', lambda path, **_kw: native_scan_result(0, path=path)) + monkeypatch.setattr(cli.ctypes, 'CDLL', lambda *_a, **_kw: NS(prctl=lambda *_: 0)) + monkeypatch.setattr(cli.os, 'getpgid', lambda _: 12345) + monkeypatch.setattr(cli, 'cleanup_group', lambda child, *_: cleaned.append(child.pid) or True) + for index in range(3): + args.protocol = cli.PROTOCOLS[index == 2] + grant.update(launch_index=index, stage='P2' if index == 2 else 'P1', + trial_indices=list(range(20)) if index == 2 else [index], + protocol_sha256=b.digest(ROOT / args.protocol)) + if index == 2: + grant['p1_evidence_sha256'] = [b.tree_digest(args.artifact_root / f'launch-{i}') for i in (0, 1)] + args.grant.write_bytes(b.encode(grant)) + args.trusted_grant_sha256 = b.digest(args.grant) + assert cli.supervise(args) == 0 + assert len(calls) == len(payloads) == len(cleaned) == 3 + assert [p['reservation']['grant']['trial_indices'] for p in payloads] == [[0], [1], list(range(20))] + assert all(c[0][0] == '/usr/bin/bash' and c[1]['start_new_session'] for c in calls) + assert all(c[1]['pass_fds'] == (987,) for c in calls) + ledger = json.loads((args.artifact_root / 'budget.json').read_bytes()) + assert sum(row['reserved_physical_calls'] for row in ledger['launches']) == 120000 + assert sum(row['observed_counts']['completed_steps'] for row in ledger['launches']) == 21 + assert (args.artifact_root / 'launch-2/stdout.log').read_bytes() == b'CPU mocked child output\n' + + +def test_worker_single_consumption_even_with_valid_reservation(tmp_path): + row = reservation(clock=b.boottime()) + row['grant'].update(boot_id=b.boot_id(), trial_indices=[0]) + (tmp_path / 'launch-0').mkdir() + b.durable_write(tmp_path / 'budget.json', b.encode({'launches': [row]})) + b.durable_write(tmp_path / 'launch-0/worker-claim.json', b'already consumed') + payload = {'launch_identity': cli.launch_identity(), 'reservation': row, + 'arguments': {'artifact_root': str(tmp_path), 'inventory': 'x', + 'manifest': 'y', 'protocol': cli.PROTOCOLS[0]}} + with pytest.raises(FileExistsError): + cli.run_worker(payload) + + +@pytest.mark.parametrize('protocol_index,rows', [(0, 2), (1, 20), (2, 20)]) +def test_worker_runtime_failure_reports_not_run_and_durable_failure(tmp_path, monkeypatch, protocol_index, rows): + args, grant, _ = trusted_inputs(tmp_path) + args.protocol = cli.PROTOCOLS[protocol_index] + row = reservation(clock=b.boottime()) + row['grant'] = grant + (args.artifact_root / 'launch-0').mkdir() + b.durable_write(args.artifact_root / 'budget.json', b.encode({'launches': [row]})) + payload = {'launch_identity': cli.launch_identity(), 'reservation': row, 'arguments': { + 'artifact_root': str(args.artifact_root), 'inventory': str(args.inventory), + 'manifest': str(args.manifest), 'trusted_manifest_sha256': args.trusted_manifest_sha256, + 'protocol': args.protocol}} + + def fail(*_): + raise RuntimeError('mock unsupported import layout') + + monkeypatch.setattr(cli, 'real_runtime', fail) + assert cli.run_worker(payload) == 1 + result = json.loads((args.artifact_root / 'launch-0/result.json').read_bytes()) + assert result['observed_counts'] is None and not result['continuation_eligible'] + assert len(result['rows']) == rows and all(r['status'] == 'not_run' for r in result['rows']) + assert result['failures'] + + +def test_source_qualified_provider_reads_real_RNG_without_sampling(): + import random + + import numpy as np + import torch + + path = ROOT / cli.PACKAGE / 'tasks/manager_based/contact_cycle_sim/evaluation_rng_scope.py' + spec = importlib.util.spec_from_file_location('test_source_qualified_rng_scope', path) + scope = importlib.util.module_from_spec(spec) + spec.loader.exec_module(scope) + before = torch.get_rng_state().clone(), random.getstate(), np.random.get_state() + snapshot = scope.backend_rng(NS(env=NS(device='cpu'))) + assert snapshot['coverage']['torch_task_global']['state']['cpu'] == before[0].tolist() + assert torch.equal(before[0], torch.get_rng_state()) and random.getstate() == before[1] + after = np.random.get_state() + assert np.array_equal(before[2][1], after[1]) and before[2][2:] == after[2:] + assert scope.GENERATOR_INVENTORY['additional_generators'] == [] + assert 'even with zero' in scope.GENERATOR_INVENTORY['task_sampling'] + assert all(value == 'unobserved' for value in snapshot['unobserved_internals'].values()) + assert not any(snapshot['evidence_scope']['claims'].values()) + + +def test_actual_runtime_hooks_recorder_partial_stop_through_runner(tmp_path, monkeypatch): + """Real evaluation loop/recorder with CPU backend accessors, never Kit imports.""" + import types + + import torch + + if 'SkillFactory' not in sys.modules: + package = types.ModuleType('SkillFactory') + package.__path__ = [str(ROOT / cli.PACKAGE)] + monkeypatch.setitem(sys.modules, 'SkillFactory', package) + from test_contact_cycle_sim_evaluation_runtime import runtime as runtime_fixture + + fixture = runtime_fixture.__wrapped__(monkeypatch) + runtime = next(fixture) + try: + recording = __import__(cli.TASK + 'evaluation_recording', fromlist=['EvaluationRecorder']) + audit = __import__(cli.LEG + 'evaluation_audit', fromlist=['matrix_report']) + # Physical accessors and manager adapter only; actual evidence is copied by + # the real recorder, not manufactured by this test or runner. + monkeypatch.setattr(runtime.base.CycleState, 'friction', lambda _: torch.zeros(1, 3)) + monkeypatch.setattr(runtime.base.CycleState, 'slip_velocity', lambda _: torch.zeros(1)) + closed, constructed = [], [] + + def environment(**kw): + env = runtime.env.EvaluationContactCycleEnv(**kw) + env.action_manager.get_term = lambda _: env.term + env._cycle_state.safety_causes = lambda: ( + ('cpu_functional_stop',) if int(env._cycle_state.substeps[0]) >= 2 else ()) + env.close = lambda: closed.append(True) + constructed.append(env) + return env + + class Observer: + def __init__(self, env, **_): + self.env = env + + def __enter__(self): + assert not self.env._started + return self + + def __exit__(self, *_): + pass + + def supplemental(self, s): + return {'actuator_state': {'cpu_mock': 'explicit test backend, no physical claim'}, + 'rng': {'cpu_mock': [1]}, 'applied': None} + + p = protocols.load_protocol(ROOT / cli.PROTOCOLS[0]) + cfg = runtime.config() + cfg.seed = p.trial(0).seed + seen_cfg = [] + api = NS( + environment=environment, recorder=recording.EvaluationRecorder, + hooks=lambda **kw: runtime.env.RuntimeHooks(runtime.Sim, [(runtime.Robot, 'reset'), + (runtime.Robot, 'write_root_pose_to_sim'), (runtime.Scene, 'reset')], **kw), + instrumentation=Observer, backend_rng=lambda _: None, matrix=audit.matrix_report, + infer=lambda x, _: (torch.zeros(1, 3), torch.zeros(1, 3), torch.ones(3) * .1)) + budget = NS(check=lambda **_: None, recording=lambda *_: None, before_step=lambda _: None) + result = cli.execute_lifecycle(api, cfg, p, [0], tmp_path, budget, + lambda _: seen_cfg.append(cfg)) + assert closed == [True] and all(c is constructed[0].cfg for c in seen_cfg) + assert result['observed_counts']['initialization_steps'] == 2 + assert result['observed_counts']['rollout_steps'] == 2 + assert result['rows'][0]['observed_physics_ticks'] == 2 + assert not result['rows'][0]['functional_passed'] + assert (tmp_path / 'trial-00/metrics.json').exists() + import time + reader_spec = importlib.util.spec_from_file_location( + 'evaluation_lifecycle_trace_reader', ROOT / 'scripts/audit_cycle_sim_evaluation.py') + reader = importlib.util.module_from_spec(reader_spec) + reader_spec.loader.exec_module(reader) + trace = [json.loads(line) for line in reader.trace_lines( + tmp_path / 'trial-00' / recording.TRACE_FILENAME, compressed=True, + max_bytes=8 * 1024 * 1024, max_record_bytes=1024 * 1024, + max_decoded_bytes=8 * 1024 * 1024, deadline=time.monotonic() + 30)] + assert [r['kind'] for r in trace] == ['initial', 'control', 'tick', 'tick', 'step_result', 'terminal'] + assert trace[-2]['reward'] is None and trace[-2]['ticks_advanced'] == 2 + finally: + with pytest.raises(StopIteration): + next(fixture) + + +def test_historic_evidence_paths_are_protected(): + for path in (ROOT, ROOT / 'logs/contact_cycle/frozen_replay_p0_01/new', + ROOT / 'logs/contact_cycle/sim_residual_training_v1/new', + ROOT / 'releases/new'): + with pytest.raises(ValueError, match='historic'): + cli.protect_artifact_path(path) + cli.protect_artifact_path(ROOT / 'logs/contact_cycle/evaluation_runs_v1/future-campaign') + + +def test_provider_accepts_actual_normalized_resolvable_PD_without_calling_it(monkeypatch): + import ast + from types import ModuleType + + source = Path('/home/cen/IsaacLab/source/isaaclab/isaaclab/utils/string.py').read_text() + node = next(n for n in ast.parse(source).body if isinstance(n, ast.ClassDef) and n.name == 'ResolvableString') + namespace = {} + tree = ast.Module(body=[ast.ImportFrom(module='__future__', names=[ast.alias(name='annotations')], level=0), node], + type_ignores=[]) + exec(compile(ast.fix_missing_locations(tree), 'pinned-ResolvableString-CPU-extraction', 'exec'), namespace) + resolver = namespace['ResolvableString'] + + class PD: + def __init__(self): + pytest.fail('Qualification must never construct actuator') + + class Cfg: + pass + + for module, symbol, value in ( + ('isaaclab.actuators.actuator_pd', 'IdealPDActuator', PD), + ('isaaclab.actuators.actuator_pd_cfg', 'IdealPDActuatorCfg', Cfg), + ('isaaclab.utils.string', 'ResolvableString', resolver), + ): + mock = ModuleType(module) + setattr(mock, symbol, value) + monkeypatch.setitem(sys.modules, module, mock) + path = ROOT / cli.PACKAGE / 'tasks/manager_based/contact_cycle_sim/evaluation_rng_scope.py' + spec = importlib.util.spec_from_file_location('provider_qualification_CPU_test', path) + scope = importlib.util.module_from_spec(spec) + spec.loader.exec_module(scope) + actuator = Cfg() + actuator.class_type = resolver('isaaclab.actuators.actuator_pd:IdealPDActuator') + cfg = NS(scene=NS(num_envs=1, robot=NS(actuators={'leg': actuator})), load_randomization=None, + observations=NS(policy=NS(enable_corruption=False)), cycle_parameters={'sensor': {'noise': 0}}) + with pytest.raises(ValueError): + scope.qualify_config(cfg) # empty cache cannot be silently resolved + actuator.class_type._resolved_callable = PD + assert scope.qualify_config(cfg)['additional_generators'] == [] + actuator.class_type._resolve_error = ValueError('cached error') + with pytest.raises(ValueError): + scope.qualify_config(cfg) + + +def test_before_Kit_close_verifies_actual_row_not_mutated_seed(tmp_path, monkeypatch): + args, grant, _ = trusted_inputs(tmp_path) + protocol = protocols.load_protocol(ROOT / cli.PROTOCOLS[1]) + row = reservation(index=2, clock=b.boottime()) + grant.update(boot_id=b.boot_id(), trial_indices=list(range(20)), launch_index=2) + row['grant'] = grant + (args.artifact_root / 'launch-2').mkdir() + b.durable_write(args.artifact_root / 'budget.json', b.encode({'launches': [row]})) + payload = {'launch_identity': cli.launch_identity(), 'reservation': row, 'arguments': { + 'artifact_root': str(args.artifact_root), 'inventory': str(args.inventory), + 'manifest': str(args.manifest), 'trusted_manifest_sha256': args.trusted_manifest_sha256, + 'protocol': cli.PROTOCOLS[1]}} + cfg = NS(seed=protocol.trial(0).seed, sim=NS(device='cpu')) + verified = [] + + def verify(_manifest, *, trial_index, **_): + verified.append(trial_index) + if cfg.seed != protocol.trial(trial_index).seed: + raise ValueError('Unexpected actual cfg seed mutation before app close') + + policy = NS() + policy.to = lambda _: policy + runtime = NS(cfg=cfg, protocol=protocol, api=NS(), arguments={'dependency_inventory': {'modules': {}}}, + binding=NS(verify_binding=verify), + replay=NS(verify_bundle=lambda _: ({}, {}), CANDIDATE='unused', + load_frozen_policy=lambda *_: policy), + app=NS(close=lambda **_: None)) + + def execute(*_): + cfg.seed = protocol.trial(0).seed + return {'continuation_eligible': True, 'failures': [], 'observed_counts': None, 'last_trial_index': 19} + + monkeypatch.setattr(cli, 'real_runtime', lambda *_: runtime) + monkeypatch.setattr(cli, 'verify_constructor_export', lambda _: None) + monkeypatch.setattr(cli, 'runtime_metadata', lambda _: {}) + monkeypatch.setattr(cli, 'execute_lifecycle', execute) + assert cli.run_worker(payload) == 1 + result = json.loads((args.artifact_root / 'launch-2/result.json').read_bytes()) + assert verified == [0, 19] and not result['continuation_eligible'] + assert result['failures'][-1]['causes'] == ['identity_external_failure'] + + +def test_signal_cleanup_uses_reserve_then_kills_only_owned_descendant(monkeypatch): + clock = NS(now=0., alive=True) + signals = [] + + def kill(pid, sig): + assert pid == 123456 + signals.append(sig) + if sig == cli.signal.SIGKILL: + clock.alive = False + + owned = NS(discover=lambda: {123456: (1, 2, 3, 'S')} if clock.alive else {}, + signal=lambda pid, row, sig: kill(pid, sig), reap=lambda _: None, unknown=False) + monkeypatch.setattr(cli.time, 'sleep', lambda dt: setattr(clock, 'now', clock.now + dt)) + assert cli.cleanup_group(NS(pid=123456, poll=lambda: None), 120, NS(boottime=lambda: clock.now), owned) + assert signals == [cli.signal.SIGTERM, cli.signal.SIGKILL] + assert 110 <= clock.now < 120 + + +def test_native_outputs_excluded_from_owned_cap_but_not_owned_traces(tmp_path, fake_clock): + (tmp_path / 'native').mkdir() + (tmp_path / 'native/cache').write_bytes(b'x' * 4096) + (tmp_path / 'trace').write_bytes(b'x' * 123) + assert b.artifact_size(tmp_path, exclude_native=True) == 123 + assert b.artifact_size(tmp_path) == 4219 + budget = b.LaunchBudget(tmp_path, reservation(), clock=fake_clock.clock, boot=fake_clock.boot_fn) + budget.check(pending=b.CAP_BYTES - b.HEADROOM_BYTES - b.STDOUT_ALLOWANCE - 123) + with pytest.raises(RuntimeError, match='storage'): + budget.check(pending=b.CAP_BYTES - b.HEADROOM_BYTES - b.STDOUT_ALLOWANCE - 122) + + +@pytest.mark.parametrize('field,value', [('approval_sha256', '0' * 64), ('check_interval_seconds', 2), + ('coverage_gaps', []), ('native_paths', []), + ('shared_native_paths', []), ('task_owned_cache_paths', []), + ('format', 'skillfactory.output-storage-policy.v2'), + ('format', 'skillfactory.output-storage-policy.v3'), + ('free_space_paths', []), ('free_space_mount_identities', {}), + ('min_free_bytes', 0), ('max_native_growth_bytes', 0)]) +def test_storage_policy_requires_explicit_approved_monitoring(tmp_path, field, value): + args, _, policy = trusted_inputs(tmp_path) + policy[field] = value + args.storage_policy.write_bytes(b.encode(policy)) + args.trusted_storage_policy_sha256 = b.digest(args.storage_policy) + with pytest.raises(RuntimeError, match='storage_policy'): + cli.storage_policy_check(args, b) + + +def native_scan_result(size, free=10 * b.CAP_BYTES, *, path=None): + return {'logical_bytes': size, 'permission_gaps': [], 'skipped_count': 0, + 'free_space': {'free_bytes': free, 'mount_identity': cli.mount_identity(Path(path)) if path else None}, + 'symlink_paths': [], 'disappeared_paths': []} + + +def test_native_monitor_baseline_interval_growth_and_free_space(tmp_path, monkeypatch): + args, _, policy = trusted_inputs(tmp_path) + clock = NS(now=0, size=10, free=10 * b.CAP_BYTES) + def scan(path, **_kw): + result = native_scan_result(clock.size, clock.free) + result['free_space']['mount_identity'] = cli.mount_identity(Path(path)) + return result + monkeypatch.setattr(cli, 'native_size', scan) + monitor = cli.NativeMonitor(policy, NS(boottime=lambda: clock.now, STORAGE_SCOPE=b.STORAGE_SCOPE)) + assert set(monitor.baseline.values()) == {10} + clock.size = b.CAP_BYTES + monitor.check() # not yet next sample + clock.now = 1 + with pytest.raises(RuntimeError, match='growth_stop'): + monitor.check() + clock.size = 10 + clock.free = b.CAP_BYTES + with pytest.raises(RuntimeError, match='free_space_stop'): + monitor.check(force=True) + assert monitor.report()['native_hard_cap'] is False + + +def test_grant_binds_launch_and_output_only_scope(tmp_path): + args, grant, _ = trusted_inputs(tmp_path) + cli.grant_check(grant, args, b) + for field in ('launch_identity_sha256', 'storage_policy_sha256', 'storage_scope'): + with pytest.raises(ValueError, match='mismatch'): + cli.grant_check({**grant, field: 'different'}, args, b) + + +def test_launch_bridge_fails_closed_on_source_or_environment_change(monkeypatch): + monkeypatch.setattr(b, 'digest', lambda _: '0' * 64) + with pytest.raises(RuntimeError, match='launch_source_changed'): + cli.launch_command([], {}) + monkeypatch.undo() + with pytest.raises(RuntimeError, match='no_virtualenv'): + cli.launch_command([], {'CONDA_PREFIX': '/another/python'}) + + +def test_owned_processes_rejects_preexisting_child_tree(monkeypatch): + parent = cli.os.getpid() + monkeypatch.setattr(cli, 'process_table', lambda: {parent: (1, 0, 0, 'S'), 90: (2, parent, 90, 'S')}) + with pytest.raises(RuntimeError, match='preexisting_children'): + cli.OwnedProcesses() + + +@pytest.mark.parametrize('unrelated_adoption', [False, True]) +def test_cleanup_rediscovery_staged_adoption_group_escape_and_unrelated_tree(monkeypatch, unrelated_adoption): + parent = cli.os.getpid() + table = {parent: (1, 0, 0, 'S'), 90: (2, 999, 90, 'S')} + monkeypatch.setattr(cli, 'process_table', lambda: dict(table)) + owned = cli.OwnedProcesses() + table[100] = (10, parent, 100, 'S') + child = NS(pid=100, poll=lambda: None) + owned.attach(child) + clock = NS(now=0) + signaled = [] + + def send(pid, row, sig): + signaled.append((pid, sig)) + assert pid != 90 + if sig == cli.signal.SIGKILL: + del table[pid] + # Each descendant is first observed after its parent is gone; every + # one escaped the original process group before adoption. + if pid < 102: + table[pid + 1] = (row[0] + 1, parent, pid + 1, 'S') + if unrelated_adoption: + table[90] = (2, parent, 90, 'S') # excluded preexisting unrelated identity + + monkeypatch.setattr(owned, 'signal', send) + monkeypatch.setattr(cli.time, 'sleep', lambda dt: setattr(clock, 'now', clock.now + dt)) + assert cli.cleanup_group(child, 12, NS(boottime=lambda: clock.now), owned) is not unrelated_adoption + assert owned.unknown is unrelated_adoption # unexpected adoption is never relabeled as clean + assert [pid for pid, sig in signaled if sig == cli.signal.SIGKILL] == [100, 101, 102] + assert table[90][0] == 2 and clock.now < 12 + + +def test_owned_pidfd_rechecks_identity_after_open(monkeypatch): + parent = cli.os.getpid() + table = {parent: (1, 0, 0, 'S')} + monkeypatch.setattr(cli, 'process_table', lambda: dict(table)) + owned = cli.OwnedProcesses() + table[100] = (10, parent, 100, 'S') + owned.attach(NS(pid=100)) + closed = [] + def opened(_): + table[100] = (999, 555, 555, 'S') # PID reused before pidfd open + return 987 + monkeypatch.setattr(cli.os, 'pidfd_open', opened) + monkeypatch.setattr(cli.os, 'close', closed.append) + monkeypatch.setattr(cli.signal, 'pidfd_send_signal', lambda *_: pytest.fail('must not signal reused PID')) + owned.signal(100, (10, parent, 100, 'S'), cli.signal.SIGKILL) + assert closed == [987] and not owned.discover() + + +def test_cleanup_deadline_reports_remaining_owned_processes(monkeypatch): + clock = NS(now=0) + signals = [] + owned = NS(discover=lambda: {100: (10, 1, 100, 'S')}, + signal=lambda pid, row, sig: signals.append(sig), reap=lambda _: None, unknown=False) + monkeypatch.setattr(cli.time, 'sleep', lambda dt: setattr(clock, 'now', clock.now + dt)) + assert not cli.cleanup_group(NS(pid=100), .2, NS(boottime=lambda: clock.now), owned) + assert signals == [cli.signal.SIGKILL] and .2 <= clock.now < .3 + + +def test_stdout_capture_startup_traceback_and_overflow(tmp_path): + source = tmp_path / 'pipe' + traceback = b'Traceback: startup failed before handshake\n' + source.write_bytes(traceback + b'x' * (b.STDOUT_BYTES + 100)) + capture = cli.StdoutCapture(source.open('rb'), tmp_path / 'stdout.log', b) + capture.pump() + with pytest.raises(RuntimeError, match='capture_overflow'): + capture.check() + capture.close() + log = (tmp_path / 'stdout.log').read_bytes() + assert log.startswith(traceback) and len(log) == b.STDOUT_BYTES + assert capture.discarded == len(traceback) + 100 + + +@pytest.mark.parametrize('exit_code', [1, None]) +def test_handshake_early_exit_or_timeout_keeps_startup_output(tmp_path, monkeypatch, exit_code): + source = tmp_path / 'pipe' + source.write_bytes(b'Traceback: no handshake\n') + capture = cli.StdoutCapture(source.open('rb'), tmp_path / 'stdout.log', b) + clock = NS(now=0) + row = reservation(clock=0) + budget = NS(reservation=row, check=lambda: None) + owned = NS(discover=lambda: {}) + monitor = NS(check=lambda: None) + monkeypatch.setattr(cli.time, 'sleep', lambda dt: setattr(clock, 'now', clock.now + dt)) + left, right = cli.socket.socketpair() + try: + with pytest.raises(RuntimeError, match='exited_without_handshake|handshake_timeout'): + cli.exchange_worker(NS(poll=lambda: exit_code), left, {}, budget, capture, owned, monitor, + NS(boottime=lambda: clock.now, CLEANUP_SECONDS=120)) + finally: + left.close() + right.close() + capture.close() + assert (tmp_path / 'stdout.log').read_bytes() == b'Traceback: no handshake\n' + + +def test_harmless_installed_launch_probe(tmp_path): + """Opt-in ONE <=60s stdlib/CLI exec-chain probe, never runner main/Kit. + + The parent -c harness carries the exact supervisor script/--run identity; + the child calls the real worker_payload authentication but not run_worker. + """ + if cli.os.environ.get('SKILLFACTORY_FD_PROBE') != '1': + pytest.skip('explicit harmless installed shell/FD probe opt-in required') + worker = r''' +import json, os, runpy, sys +from pathlib import Path +m = runpy.run_path(sys.argv[1], run_name='fd_probe_worker') +payload = m['worker_payload'](int(sys.argv[2])) +assert payload['probe'] == 'no-kit' +assert Path(sys.executable).resolve() == Path(m['RESOLVED_PYTHON']).resolve() +assert not any(x.split('.')[0] in ('omni', 'isaacsim', 'torch', 'pxr') for x in sys.modules) +print(json.dumps({'handshake': 'authenticated', 'pid': os.getpid(), + 'executable': str(Path(sys.executable).resolve()), + 'ISAACLAB_PATH': os.environ['ISAACLAB_PATH'], + 'CARB_APP_PATH': os.environ['CARB_APP_PATH'], + 'EXP_PATH': os.environ['EXP_PATH'], + 'RESOURCE_NAME': os.environ['RESOURCE_NAME'], + 'PYTHONPATH': os.environ['PYTHONPATH'], + 'no_application_modules': True}), flush=True) +''' + harness = r''' +import ctypes, json, os, runpy, socket, subprocess, sys, time +from pathlib import Path +from types import SimpleNamespace as NS +m = runpy.run_path(sys.argv[1], run_name='fd_probe_supervisor') +b = m['cpu_module']('evaluation_budget') +owned = m['OwnedProcesses']() +assert ctypes.CDLL(None).prctl(36, 1, 0, 0, 0) == 0 +root = Path(sys.argv[3]) +left, right = socket.socketpair() +env = dict(os.environ, PYTHONDONTWRITEBYTECODE='1', PYTEST_DISABLE_PLUGIN_AUTOLOAD='1') +for key in ('CONDA_PREFIX', 'VIRTUAL_ENV', 'PYTHONEXE', 'PYTHONHOME', 'args'): + env.pop(key, None) +command = m['launch_command'](['-c', sys.argv[4], sys.argv[1], str(right.fileno())], env) +child = subprocess.Popen(command, env=env, cwd=m['ROOT'], stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, start_new_session=True, pass_fds=(right.fileno(),)) +right.close() +capture = m['StdoutCapture'](child.stdout, root / 'probe.stdout.log', b) +owned.attach(child) +started = b.boottime() +budget = b.LaunchBudget(root, {'deadline_boottime': started + 160, + 'grant': {'boot_id': b.boot_id()}}) +clean = False +try: + m['exchange_worker'](child, left, {'probe': 'no-kit'}, budget, capture, owned, NS(check=lambda: None), b) + while not capture.eof or child.poll() is None: + budget.check() + capture.check() + owned.discover() + time.sleep(.01) +finally: + clean = m['cleanup_group'](child, min(started + 50, b.boottime() + 10), b, owned, capture.pump) + capture.close() + left.close() +print(json.dumps({'returncode': child.returncode, 'cleanup_clean': clean, + 'owned_remaining': owned.discover(), 'seconds': b.boottime() - started, + 'stdout_bytes': capture.written, 'stdout_discarded': capture.discarded})) +assert child.returncode == 0 and clean and not capture.discarded +''' + result = cli.subprocess.run(['/usr/bin/python3.12', '-c', harness, str(ROOT / cli.SCRIPT), '--run', + str(tmp_path), worker], capture_output=True, text=True, timeout=60) + log = (tmp_path / 'probe.stdout.log').read_text() + print('HARMLESS PROBE supervisor:', result.stdout.strip()) + print('HARMLESS PROBE child:', log.strip()) + assert result.returncode == 0, result.stderr + result.stdout + log + report = json.loads(result.stdout.strip().splitlines()[-1]) + assert report['cleanup_clean'] and report['owned_remaining'] == {} and report['seconds'] < 60 + child_report = json.loads(log.strip().splitlines()[-1]) + assert child_report['handshake'] == 'authenticated' and child_report['no_application_modules'] + assert '/home/cen/IsaacLab/source/isaaclab' in child_report['PYTHONPATH'] + assert 'Traceback' not in log and report['stdout_bytes'] < 16384 + + +@pytest.mark.parametrize('overflow', [False, True]) +def test_supervise_captures_failed_handshake_through_cleanup(tmp_path, monkeypatch, overflow): + args, _, _ = trusted_inputs(tmp_path) + monkeypatch.setattr(cli, 'native_size', lambda path, **_kw: native_scan_result(0, path=path)) + monkeypatch.setattr(cli.ctypes, 'CDLL', lambda *_a, **_kw: NS(prctl=lambda *_: 0)) + owned = NS(supervisor=(1, 2), known={(100, 10)}, attach=lambda _: None, + discover=lambda: {}, reap=lambda _: None, unknown=False) + monkeypatch.setattr(cli, 'OwnedProcesses', lambda: owned) + source = tmp_path / 'mock-pipe' + trace = b'Traceback: import failed BEFORE hello\n' + source.write_bytes(trace + (b'x' * b.STDOUT_BYTES if overflow else b'')) + def popen(*_, **__): + return NS(pid=100, stdout=source.open('rb'), returncode=1, poll=lambda: 1) + def failed_exchange(child, channel, payload, budget, capture, *_): + if overflow: + capture.check() + capture.check() + raise RuntimeError('identity_worker_handshake_eof') + monkeypatch.setattr(cli.subprocess, 'Popen', popen) + monkeypatch.setattr(cli, 'exchange_worker', failed_exchange) + assert cli.supervise(args) == 1 + output = args.artifact_root / 'launch-0' + log = (output / 'stdout.log').read_bytes() + assert log.startswith(trace) and len(log) <= b.STDOUT_BYTES + result = json.loads((output / 'supervisor.json').read_bytes()) + assert result['cleanup_clean'] and not result['continuation_eligible'] + assert result['storage']['scope'] == b.STORAGE_SCOPE + assert bool(result['stdout_discarded_bytes']) is overflow + ledger = json.loads((args.artifact_root / 'budget.json').read_bytes()) + assert ledger['launches'][0]['reserved_physical_calls'] == 14500 + assert ledger['launches'][0]['status'] == 'closed' + + +def test_unrelated_descendant_exclusion_survives_parent_exit(monkeypatch): + parent = cli.os.getpid() + table = {parent: (1, 0, 0, 'S'), 90: (2, 999, 90, 'S')} + monkeypatch.setattr(cli, 'process_table', lambda: dict(table)) + owned = cli.OwnedProcesses() + table[100] = (10, parent, 100, 'S') + owned.attach(NS(pid=100)) + table[91] = (3, 90, 91, 'S') # unrelated descendant born after baseline + assert set(owned.discover()) == {100} + del table[90] + table[91] = (3, parent, 91, 'S') + assert set(owned.discover()) == {100} and owned.unknown + + +@pytest.mark.parametrize('failure', ['capture', 'attach']) +def test_supervise_setup_failure_terminates_reaps_and_closes_pipes(tmp_path, monkeypatch, failure): + args, _, _ = trusted_inputs(tmp_path) + monkeypatch.setattr(cli, 'native_size', lambda path, **_kw: native_scan_result(0, path=path)) + monkeypatch.setattr(cli.ctypes, 'CDLL', lambda *_a, **_kw: NS(prctl=lambda *_: 0)) + parent = cli.os.getpid() + table = {parent: (1, 0, 0, 'S')} + monkeypatch.setattr(cli, 'process_table', lambda: dict(table)) + owned = cli.OwnedProcesses() + monkeypatch.setattr(cli, 'OwnedProcesses', lambda: owned) + events = [] + pipe = (tmp_path / 'pipe').open('w+b') + child = NS(pid=100, stdout=pipe, returncode=None) + + def poll(): + if table.get(100, (None, None, None, None))[3] == 'Z': + del table[100] + child.returncode = -cli.signal.SIGTERM + events.append('reaped') + return child.returncode + + child.poll = poll + + def popen(*_, **kwargs): + assert kwargs['start_new_session'] + table[100] = (10, parent, 100, 'S') + events.append('spawned') + return child + + attach = owned.attach + + def attaching(process): + assert events == ['spawned'] + events.append('attached') + if failure == 'attach': + raise RuntimeError('injected_attach_failure') + attach(process) + + def capture(*_): + assert owned.root == (100, 10) + events.append('capture') + raise OSError('injected_capture_constructor_failure') + + def terminate(pid, row, sig): + assert pid == 100 and row[0] == 10 and pipe.closed + assert sig == cli.signal.SIGTERM + table[100] = (10, parent, 100, 'Z') + events.append('terminated') + + monkeypatch.setattr(cli.subprocess, 'Popen', popen) + monkeypatch.setattr(owned, 'attach', attaching) + monkeypatch.setattr(owned, 'signal', terminate) + monkeypatch.setattr(cli, 'StdoutCapture', capture) + assert cli.supervise(args) == 1 + assert events == ['spawned', 'attached'] + (['capture'] if failure == 'capture' else []) + [ + 'terminated', 'reaped'] + assert pipe.closed and not owned.discover() + result = json.loads((args.artifact_root / 'launch-0/supervisor.json').read_bytes()) + assert result['cleanup_clean'] is (failure == 'capture') + assert result['ownership_unknown'] is (failure == 'attach') + assert result['returncode'] == -cli.signal.SIGTERM + assert not result['continuation_eligible'] + assert 'injected_' in result['errors'][0]['context']['message'] + + +def test_stdout_capture_configuration_failure_closes_log_and_pipe(tmp_path, monkeypatch): + pipe = (tmp_path / 'pipe').open('w+b') + log = (tmp_path / 'stdout.log').open('xb') + monkeypatch.setattr(cli.Path, 'open', lambda *_a, **_kw: log) + + def fail(*_): + raise OSError('injected_nonblocking_failure') + + monkeypatch.setattr(cli.os, 'set_blocking', fail) + with pytest.raises(OSError, match='injected_nonblocking_failure'): + cli.StdoutCapture(pipe, tmp_path / 'stdout.log', b) + assert log.closed and pipe.closed + + +@pytest.mark.parametrize('arguments', [['--run'], ['--check-only'], ['--_worker-fd', '3'], + ['--prepare-binding', '--inventory', '/supplied']]) +def test_inventory_discovery_cli_restricted_to_preparation(monkeypatch, arguments): + monkeypatch.setattr(cli, 'prepare', lambda *_: pytest.fail('must reject before preparation')) + monkeypatch.setattr(cli, 'supervise', lambda *_: pytest.fail('must reject before execution')) + with pytest.raises(SystemExit) as error: + cli.main([*arguments, '--discover-inventory']) + assert error.value.code == 2 + + +@pytest.mark.parametrize('discovery_failure', [False, True]) +def test_prepare_discovery_proposal_exclusive_and_fail_closed(tmp_path, monkeypatch, discovery_failure): + events = [] + proposal = {'scope': 'test', 'modules': {'test': {}}, 'artifacts': {'native': {}}} + fake = NS(cfg=object(), binding=NS(FORMAT='test-v4', PROVENANCE_SCOPE='development', PROVENANCE_LIMITATIONS=(), + prepare_binding=lambda **_: b'{"imports":{}}', + verify_binding=lambda *_, **__: None, incidental_observations=lambda _: {}), + arguments={'dependency_inventory': {'modules': {}}}, + app=NS(close=lambda **_: events.append('app_close'))) + monkeypatch.setattr(cli, 'real_runtime', lambda *_: fake) + monkeypatch.setattr(cli, 'verify_constructor_export', lambda _: None) + monkeypatch.setattr(cli, 'runtime_metadata', lambda _: {}) + + def discover(*_): + if discovery_failure: + raise ValueError('Missing reviewed dependency modules') + return proposal + + monkeypatch.setattr(cli, 'proposed_inventory', discover) + args = NS(output=tmp_path / 'prepare', inventory=None, trial_index=0, + artifact_root=tmp_path, discover_inventory=True) + if discovery_failure: + with pytest.raises(ValueError, match='Missing reviewed dependency'): + cli.prepare(args) + assert not (args.output / 'binding.json').exists() + else: + assert cli.prepare(args) == 0 + assert json.loads((args.output / 'inventory.proposed.json').read_bytes()) == proposal + report = json.loads((args.output / 'preparation.json').read_bytes()) + assert report['inventory_status'] == 'unreviewed_proposal' and not report['execution_authorized'] + assert events == ['app_close'] + with pytest.raises(FileExistsError): + cli.prepare(args) # never overwrite a prior attempt + assert events == ['app_close'] + + +def test_proposed_inventory_uses_unchanged_binding_coverage_and_rejects_missing_native(tmp_path, monkeypatch): + # Reuse file-backed synthetic numerical fixtures, never actual numerical imports. + from test_contact_cycle_sim_evaluation_binding import binding, numerical_inventory + + for name in tuple(sys.modules): + if name.split('.')[0] in binding.NUMERICAL_ROOTS: + monkeypatch.delitem(sys.modules, name) + numerical_inventory(tmp_path, monkeypatch) + monkeypatch.setattr(binding, '_sources', lambda *_: ({}, set())) + monkeypatch.setattr(binding, '_encode', lambda *_: None) + monkeypatch.setattr(binding, '_imports', lambda *_, **__: {}) + proposal = cli.proposed_inventory(binding, object()) + assert binding._dependency_inventory(proposal, set(), set()) == proposal + module = sys.modules['warp._src.context'] + monkeypatch.delitem(sys.modules, 'warp._src.context') + with pytest.raises(ValueError, match='warp._src.context'): + cli.proposed_inventory(binding, object()) + monkeypatch.setitem(sys.modules, 'warp._src.context', module) + (tmp_path / 'site/warp/bin/synthetic.so').unlink() + with pytest.raises(ValueError, match='missing numerical native layout'): + cli.proposed_inventory(binding, object()) + + +def test_storage_policy_historical_v2_is_not_reinterpreted(tmp_path): + args, _, policy = trusted_inputs(tmp_path) + policy['format'] = 'skillfactory.output-storage-policy.v2' + del policy['shared_native_paths'], policy['task_owned_cache_paths'] + policy['coverage_gaps'] = list(cli.NATIVE_GAPS[:-1]) + args.storage_policy.write_bytes(b.encode(policy)) + args.trusted_storage_policy_sha256 = b.digest(args.storage_policy) + with pytest.raises(RuntimeError, match='storage_policy_identity_or_conditions_mismatch'): + cli.storage_policy_check(args, b) + + +@pytest.mark.parametrize('field', ['native_paths', 'shared_native_paths', 'task_owned_cache_paths', 'free_space_paths']) +@pytest.mark.parametrize('change', ['omission', 'addition', 'alias']) +def test_storage_policy_paths_are_exact_writer_paths(tmp_path, change, field): + args, _, policy = trusted_inputs(tmp_path) + if change == 'omission': + policy[field].pop() + elif change == 'addition': + policy[field].append('/tmp/arbitrary-external-cache') + else: + policy[field][0] += '/../alias' + args.storage_policy.write_bytes(b.encode(policy)) + args.trusted_storage_policy_sha256 = b.digest(args.storage_policy) + with pytest.raises(RuntimeError, match='storage_policy_identity_or_conditions_mismatch'): + cli.storage_policy_check(args, b) + + +def test_storage_policy_installed_data_logs_and_campaign_native_stay_strict(tmp_path): + paths = cli.native_paths(tmp_path) + shared = cli.shared_native_paths() + assert set(shared) < set(paths) + for strict in (str(tmp_path / 'native'), '/home/cen/isaacsim-6.0.1/data', + '/home/cen/isaacsim-6.0.1/kit/data', '/home/cen/isaacsim-6.0.1/kit/logs'): + assert strict in paths and strict not in shared + assert all(Path(p).is_relative_to(tmp_path / 'native') for p in cli.task_owned_cache_paths(tmp_path)) + + +@pytest.mark.parametrize('missing', ['omni.replicator', 'omni.replicator.core']) +def test_preimport_replicator_allows_only_intended_absence(monkeypatch, missing): + def absent(name): + assert name == cli.OPTIONAL_REPLICATOR + raise ModuleNotFoundError('optional absent', name=missing) + + monkeypatch.setattr(cli.importlib, 'import_module', absent) + report = cli.preimport_replicator() + assert report == {'module': 'omni.replicator.core', 'available': False, + 'missing_module': missing, 'sampling_scope': 'no configured sampling', + 'scope_requires_nominal_config_qualification': True, + 'complete_rng_state_claim': False} + + +@pytest.mark.parametrize('error', [ModuleNotFoundError('required parent', name='omni'), + ModuleNotFoundError('transitive', name='unrelated_dependency'), + ModuleNotFoundError('broken internal', name='omni.replicator.broken'), + ModuleNotFoundError('unspecified'), + AttributeError('broken extension'), ImportError('broken native')]) +def test_preimport_replicator_propagates_import_defects(monkeypatch, error): + def broken(_): + raise error + + monkeypatch.setattr(cli.importlib, 'import_module', broken) + with pytest.raises(type(error)) as caught: + cli.preimport_replicator() + assert caught.value is error + + +@pytest.mark.parametrize('available', [False, True]) +def test_replicator_availability_must_match_bound_inventory(monkeypatch, available): + monkeypatch.delitem(sys.modules, cli.OPTIONAL_REPLICATOR, raising=False) + if available: + monkeypatch.setitem(sys.modules, cli.OPTIONAL_REPLICATOR, NS()) + matching = {'modules': {cli.OPTIONAL_REPLICATOR: {}} if available else {}} + cli.check_replicator_inventory(matching) + with pytest.raises(RuntimeError, match='optional_replicator_availability_changed'): + cli.check_replicator_inventory({'modules': {} if available else {cli.OPTIONAL_REPLICATOR: {}}}) + + +def test_present_replicator_source_not_recursive_metadata_in_proposed_inventory(tmp_path, monkeypatch): + from test_contact_cycle_sim_evaluation_binding import binding, load, numerical_inventory + + for name in tuple(sys.modules): + if name.split('.')[0] in binding.NUMERICAL_ROOTS: + monkeypatch.delitem(sys.modules, name) + numerical_inventory(tmp_path, monkeypatch) + dependency_path = tmp_path / 'replicator_dependency.py' + dependency_path.write_text('def seed(value):\n return value\n') + dependency = load('_replicator_dependency_test', dependency_path, monkeypatch) + module_path = tmp_path / 'replicator_core.py' + module_path.write_text('from _replicator_dependency_test import seed\n') + module = load(cli.OPTIONAL_REPLICATOR, module_path, monkeypatch) + monkeypatch.setattr(binding, '_sources', lambda *_: ({}, set())) + monkeypatch.setattr(binding, '_encode', lambda *_: None) + monkeypatch.setattr(binding, '_imports', lambda *_, **__: {}) + report = cli.preimport_replicator() + assert report['available'] and report['missing_module'] is None + assert report['sampling_scope'] == 'no configured sampling' and not report['complete_rng_state_claim'] + proposal = cli.proposed_inventory(binding, object()) + assert dependency.__name__ not in proposal['modules'] + for actual, path in ((module, module_path),): + assert proposal['modules'][actual.__name__] == { + 'path': str(path), 'sha256': binding.sha256(path.read_bytes())} + assert binding._dependency_inventory(proposal, set(), set()) == proposal + cli.check_replicator_inventory(proposal) + + +def test_proposed_inventory_aggregates_independent_invalid_entries(tmp_path, monkeypatch): + from test_contact_cycle_sim_evaluation_binding import binding, load, numerical_inventory + + for name in tuple(sys.modules): + if name.split('.')[0] in binding.NUMERICAL_ROOTS: + monkeypatch.delitem(sys.modules, name) + numerical_inventory(tmp_path, monkeypatch) + source = tmp_path / 'provider.py' + source.write_text('') + provider = load('diagnostic_provider', source, monkeypatch) + for name in ('broken_provider_one', 'broken_provider_two'): + module = cli.ModuleType(name) + monkeypatch.setitem(sys.modules, name, module) + setattr(provider, name, module) + monkeypatch.setattr(binding, '_sources', lambda *_: ({}, set())) + monkeypatch.setattr(binding, '_encode', lambda *_: None) + monkeypatch.setattr(binding, '_imports', lambda *_, **__: { + 'diagnostic_provider': {'path': str(source), 'sha256': binding.sha256(source.read_bytes())}}) + monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', {**binding.REQUIRED_RUNTIME, + **{n: ('test-required', '/missing', '0' * 64) + for n in ('broken_provider_one', 'broken_provider_two')}}) + (tmp_path / 'site/warp/bin/synthetic.so').unlink() + (tmp_path / 'site/torch/lib/synthetic.so').unlink() + monkeypatch.setattr(binding, '_dependency_inventory', lambda *_: pytest.fail('invalid proposal accepted')) + with pytest.raises(cli.InventoryPreparationError) as caught: + cli.proposed_inventory(binding, object()) + errors = caught.value.report['invalid_entries'] + assert {row['entry'] for row in errors} == { + 'broken_provider_one', 'broken_provider_two', str(tmp_path / 'site/warp/bin'), + str(tmp_path / 'site/torch/lib')} + assert all('sha256' not in row for row in errors) + + +def test_prepare_inventory_diagnostic_is_fail_and_never_manifest(tmp_path, monkeypatch): + events = [] + fake = NS(cfg=object(), binding=NS(FORMAT='test-v4', PROVENANCE_SCOPE='development', PROVENANCE_LIMITATIONS=(), + prepare_binding=lambda **_: pytest.fail('must not bind'), + module_diagnostic=lambda name: {'observed': name}), + arguments={}, app=NS(close=lambda **_: events.append('normal_app_close'))) + monkeypatch.setattr(cli, 'real_runtime', lambda *_: fake) + monkeypatch.setattr(cli, 'verify_constructor_export', lambda _: None) + diagnostics = [{'stage': 'dependency-module', 'entry': name, 'error': 'no real file'} + for name in ('one', 'two')] + + def discover(*_): + raise cli.InventoryPreparationError(diagnostics) + + monkeypatch.setattr(cli, 'proposed_inventory', discover) + args = NS(output=tmp_path / 'prepare', inventory=None, trial_index=0, + artifact_root=tmp_path, discover_inventory=True) + with pytest.raises(cli.InventoryPreparationError): + cli.prepare(args) + report = json.loads((args.output / 'inventory.diagnostic.json').read_bytes()) + assert report['status'] == 'FAIL' and report['inventory_status'] == 'unreviewed_diagnostic' + assert not report['execution_authorized'] and report['invalid_entries'] == diagnostics + assert report['module_observations']['sys'] == {'observed': 'sys'} + assert sorted(p.name for p in args.output.iterdir()) == ['inventory.diagnostic.json', 'preparation-failure.json'] + assert events == ['normal_app_close'] + + +def test_constructor_failure_retains_actual_binding_diagnostic_without_preclose(lifecycle, tmp_path): + f = lifecycle + from test_contact_cycle_sim_evaluation_binding import binding + expected = binding.canonical_json({'effective_config': {'mutation': False}}) + actual = binding.canonical_json({'effective_config': {'mutation': True}}) + + def constructor(**kwargs): + assert kwargs['cfg'] is f.cfg + f.cfg.mutation = True + raise RuntimeError('construction failed before environment returned') + + def verify(row): + if f.cfg.mutation: + raise binding.BindingMismatchError(expected, actual) + + f.api.environment = constructor + result = cli.execute_lifecycle(f.api, f.cfg, f.protocol, [0], tmp_path, f.budget, verify) + assert not result['continuation_eligible'] and f.cfg.mutation + assert result['failures'][0]['context']['phase'] == 'initialization' + diagnostic = result['failures'][-1]['context']['binding_diagnostic'] + assert diagnostic['differences'][0]['path'] == '/effective_config/mutation' + assert diagnostic['differences'][0]['actual']['value'] is True + assert not (tmp_path / 'preclose-filesystem.json').exists() + + +def test_worker_persists_binding_diagnostic_before_fast_close_without_preclose(tmp_path, monkeypatch): + args, grant, _ = trusted_inputs(tmp_path) + row = reservation(clock=b.boottime()) + row['grant'] = grant + output = args.artifact_root / 'launch-0' + output.mkdir() + b.durable_write(args.artifact_root / 'budget.json', b.encode({'launches': [row]})) + from test_contact_cycle_sim_evaluation_binding import binding + error = binding.BindingMismatchError(b'{"imports":{"x":1}}', b'{"imports":{"x":2}}') + closed = [] + + def verify(*_a, **_kw): + raise error + + def close(*, exit_code): + saved = json.loads((output / 'result.json').read_bytes()) + assert saved['failures'][-1]['context']['binding_diagnostic'] == error.binding_diagnostic + assert not saved['continuation_eligible'] and exit_code == 1 + assert not (output / 'preclose-filesystem.json').exists() + closed.append(True) + + runtime = NS(binding=NS(verify_binding=verify), arguments={'dependency_inventory': {}}, + app=NS(close=close)) + monkeypatch.setattr(cli, 'real_runtime', lambda *_: runtime) + monkeypatch.setattr(cli, 'verify_constructor_export', lambda _: None) + monkeypatch.setattr(cli, 'check_replicator_inventory', lambda *_: None) + payload = {'launch_identity': cli.launch_identity(), 'reservation': row, 'arguments': { + 'artifact_root': str(args.artifact_root), 'inventory': str(args.inventory), + 'manifest': str(args.manifest), 'trusted_manifest_sha256': args.trusted_manifest_sha256, + 'protocol': args.protocol}} + assert cli.run_worker(payload) == 1 and closed == [True] + + +@pytest.mark.parametrize('drift', ['constructor_config', 'unexpected_module']) +def test_immediate_postconstructor_binding_stops_before_reset_or_inference(lifecycle, drift, tmp_path): + from test_contact_cycle_sim_evaluation_binding import binding + + f = lifecycle + f.fault.phase = drift + diagnostic = {'difference_count': 1, 'differences': [{'path': '/imports/isaaclab.unexpected'}]} + + def verify(row): + f.verify(row) + if drift == 'unexpected_module' and 'construct' in f.events: + error = ValueError('unexpected actual module') + error.binding_diagnostic = diagnostic + raise error + + result = cli.execute_lifecycle(f.api, f.cfg, f.protocol, [0], tmp_path, f.budget, verify) + assert not result['continuation_eligible'] + assert result['failures'][0]['causes'] == ['identity_external_failure'] + assert result['observed_counts']['initialization_steps'] == 1 # constructor only + assert result['observed_counts']['rollout_steps'] == 0 + assert not {'reset_0', 'inference', 'control_0', 'step_0', 'observer_enter'} & set(f.events) + assert f.events[f.events.index('construct') + 1] == 'verify_0' + assert f.events[-3:] == ['env_close', 'hooks_exit', 'verify_0'] + assert 'durable_0' in f.events + if drift == 'unexpected_module': + assert result['failures'][0]['context']['binding_diagnostic'] == diagnostic + assert result['failures'][-1]['context']['binding_diagnostic'] == diagnostic + # The real comparator includes new imports, not just the lifecycle test double. + report = binding.binding_difference(b'{"imports":{}}', b'{"imports":{"isaaclab.unexpected":"new"}}') + assert report['difference_count'] == 1 + + +def test_external_failure_bounded_traceback_excludes_locals_and_source(): + def fail(depth): + secret = 'private-local-value-not-for-artifacts' # noqa: F841 - intentional private local + if depth: + return fail(depth - 1) + raise TypeError('cannot pickle module') + + try: + fail(45) + except TypeError as error: + failure = cli.external_failure('initialization', error) + encoded = json.dumps(failure) + trace = failure['context']['traceback'] + assert failure['fatal'] and failure['causes'] == ['infrastructure_external_failure'] + assert len(trace['frames']) == 32 and trace['omitted_outer_frames'] > 0 + assert trace['frames'][-1]['function'] == 'fail' + assert trace['frames'][-1]['file'] == __file__ + assert all(set(frame) == {'file', 'line', 'function'} for frame in trace['frames']) + assert 'private-local-value' not in encoded and 'raise TypeError' not in encoded + assert cli.external_failure('step', RuntimeError('unraised'))['context']['traceback'] == { + 'frames': [], 'omitted_outer_frames': 0} + + +def test_constructor_failure_traceback_persisted_without_continuation(lifecycle): + lifecycle.fault.phase = 'construct' + result = lifecycle.run([0, 1]) + failure = result['failures'][0] + assert failure['context']['phase'] == 'initialization' and failure['fatal'] + frames = failure['context']['traceback']['frames'] + assert [f['function'] for f in frames] == ['execute_lifecycle', '__init__'] + persisted = json.loads((lifecycle.state.recorders[0].path / 'durable.json').read_bytes()) + assert persisted['external'] == failure + assert not result['continuation_eligible'] and not result['accepted_skill'] + assert 'reset_0' not in lifecycle.events and 'step_0' not in lifecycle.events diff --git a/tests/test_contact_cycle_sim_evaluation_runtime.py b/tests/test_contact_cycle_sim_evaluation_runtime.py new file mode 100644 index 0000000..821b8d1 --- /dev/null +++ b/tests/test_contact_cycle_sim_evaluation_runtime.py @@ -0,0 +1,738 @@ +"""CPU spies of the actual evaluation state/step, never Kit or physics launches. + +Only Isaac imports/backend operations and IK solve are mocked. Frozen latch, +command, action composition, delays, 46D policy and runtime code are exercised. +""" +import importlib +import json +import sys +import types +from pathlib import Path +from types import SimpleNamespace as NS + +import pytest +import torch +from SkillFactory.legs.contact_cycle.mechanics import inverse_rotate, rotate +from SkillFactory.legs.contact_cycle_sim.evaluation_protocol import load_protocol +from SkillFactory.legs.spec import load_bundle + +ROOT = Path(__file__).resolve().parents[1] +TASK = 'SkillFactory.tasks.manager_based.' + + +@pytest.fixture +def runtime(monkeypatch): # noqa: C901 - local spy classes keep Isaac mocks scoped to each test + # Test-local namespace imports only; never passed to frozen verify_bundle. + for relative in ('tasks', 'tasks.manager_based', 'tasks.manager_based.contact_cycle', + 'tasks.manager_based.contact_cycle_sim', 'tasks.manager_based.loaded_leg', + 'tasks.manager_based.single_leg_anchor', 'tasks.manager_based.single_leg_anchor.mdp'): + name = 'SkillFactory.' + relative + module = types.ModuleType(name) + module.__path__ = [str(ROOT / 'source/SkillFactory/SkillFactory' / relative.replace('.', '/'))] + monkeypatch.setitem(sys.modules, name, module) + names = [TASK + p for p in ('loaded_leg.mdp', 'contact_cycle.mdp', 'contact_cycle_sim.mdp', + 'contact_cycle_sim.evaluation_mdp', 'contact_cycle_sim.evaluation_env')] + for name in names: + monkeypatch.delitem(sys.modules, name, raising=False) + + class Sim: + is_rendering = False + + def __init__(self): + self.calls = 0 + + def step(self, render=False): + self.calls += 1 + + def forward(self): + pass + + def reset(self): + pass + + class Composer: + def __init__(self): + self.set_calls = self.reset_calls = 0 + self.fail = False + self.last = None + + def reset(self, **kwargs): + self.reset_calls += 1 + + def set_forces_and_torques_index(self, **kwargs): + self.set_calls += 1 + self.reset(env_ids=None) # installed setter's legitimate internal replacement + self.last = kwargs + if self.fail: + raise RuntimeError('composer setter failure') + + class Robot: + num_bodies = 4 + body_names = ['virtual_payload', 'FL_thigh', 'FL_calf', 'anchor'] + actuators = {} + + def __init__(self): + self.data = NS(body_mass=NS(torch=torch.tensor([[4., 0., 0., 0.]])), + applied_torque=NS(torch=torch.zeros(1, 3)), + joint_vel=NS(torch=torch.zeros(1, 3)), root_state_w=NS(torch=torch.zeros(1, 13))) + self.permanent_wrench_composer = Composer() + self.data.body_com_pos_w = NS(torch=torch.zeros(1, 4, 3)) + self.data.body_link_pos_w = NS(torch=torch.zeros(1, 4, 3)) + self.data.body_link_quat_w = NS(torch=torch.tensor([[[0., 0., 0., 1.]]]).expand(1, 4, 4)) + + def write_root_pose_to_sim(self, *args): + pass + + def reset(self): + pass + + class Scene(dict): + sensors = {} + + def __init__(self): + super().__init__(robot=Robot()) + self.env_origins = torch.zeros(1, 3) + self.updates = 0 + + def update(self, dt): + self.updates += 1 + + def write_data_to_sim(self): + pass + + def reset(self, *args): + self['robot'].reset() + self['robot'].write_root_pose_to_sim(None) + + class BoundedAction: + def __init__(self, cfg, env): + self._env = env + self.raw_actions = torch.zeros(1, 3) + self.processed_actions = torch.zeros(1, 3) + + def process_actions(self, action): + self.raw_actions.copy_(action) + self.processed_actions.copy_(action) + + def apply_actions(self): + pass + + class Manager: + active_terms = () + available_modes = () + + def reset(self, *a, **kw): + return {} + + def compute(self, **kw): + return torch.zeros(1) + + class BaseEnv: + def __init__(self, cfg): + self.cfg, self.device, self.num_envs = cfg, 'cpu', 1 + self.physics_dt, self.step_dt = .002, .01 + self.sim, self.scene = Sim(), Scene() + self.sim.step() # constructor step MUST be counted + self._physics_handles_decimation = False + self._sim_step_counter = self.common_step_counter = 0 + self.episode_length_buf = torch.zeros(1, dtype=torch.long) + self.extras = {} + self.render_enabled = False + for name in ('recorder', 'event', 'reward', 'termination', 'curriculum', 'command'): + setattr(self, name + '_manager', Manager()) + mdp = importlib.import_module(TASK + 'contact_cycle_sim.evaluation_mdp') + self.term = mdp.EvaluationResidualAction(None, self) + self.action_manager = Manager() + self.action_manager.action = torch.zeros(1, 3) + self.action_manager.prev_action = torch.zeros(1, 3) + + def process(action): + self.action_manager.prev_action.copy_(self.action_manager.action) + self.action_manager.action.copy_(action) + self.term.process_actions(action) + + self.action_manager.process_action = process + self.action_manager.apply_action = self.term.apply_actions + self.observation_manager = Manager() + self.observation_manager.compute = lambda **kw: {'policy': self._cycle_state.policy()} + + def reset(self, seed=None): + torch.manual_seed(seed) + self._reset_idx(torch.tensor([0])) + self.sim.step() # mocked reset warmup; not a rollout sample + return {}, {} + + def _reset_idx(self, ids): + self.scene.reset(ids) + self.action_manager.reset(ids) + self.episode_length_buf.zero_() + + def step(self, action): + raise AssertionError('Inherited autoreset step MUST NEVER RUN') + + monkeypatch.setitem(sys.modules, 'isaaclab.envs', NS(ManagerBasedRLEnv=BaseEnv)) + monkeypatch.setitem(sys.modules, 'isaaclab.utils.math', NS( + axis_angle_from_quat=lambda q: q[:, :3], quat_apply=rotate, quat_apply_inverse=inverse_rotate)) + monkeypatch.setitem(sys.modules, TASK + 'single_leg_anchor.mdp.actions', NS( + BoundedJointPositionAction=BoundedAction)) + loaded = importlib.import_module(TASK + 'loaded_leg.mdp') + + def loaded_init(self, env): + self.env, self.bundle, self.robot = env, env.cfg.leg_bundle, env.scene['robot'] + self.nominal = torch.tensor([j['q_default'] for j in self.bundle['spec']['joints']]) + self.joints, self.payload, self.anchor = [0, 1, 2], 0, 3 + self.effort = torch.tensor([j['limits']['effort'] for j in self.bundle['joints']]) + self.stiffness = self.damping = torch.zeros(6) + self.wrench_limit = torch.ones(6) * 100 + + monkeypatch.setattr(loaded.LoadedState, '__init__', loaded_init) + monkeypatch.setattr(loaded.LoadedState, 'foot_center', lambda s: torch.tensor([[.1, -.1, .022]])) + monkeypatch.setattr(loaded.LoadedState, 'root', lambda s: ( + torch.tensor([[0., 0., .3]]), torch.tensor([[0., 0., 0., 1.]]), torch.zeros(1, 3), torch.zeros(1, 3))) + monkeypatch.setattr(loaded.LoadedState, 'contact', lambda s: torch.tensor([[0., 0., 39.24]])) + monkeypatch.setattr(loaded.LoadedState, 'q', lambda s: s.nominal[None].clone()) + monkeypatch.setattr(loaded.LoadedState, 'tilt', lambda s: torch.zeros(1)) + base = importlib.import_module(TASK + 'contact_cycle.mdp') + monkeypatch.setattr(base.CycleState, 'raw_sensors', lambda s: torch.zeros(1, 18)) + real_apply_loads = base.CycleState.apply_loads + monkeypatch.setattr(base.CycleState, 'apply_loads', lambda s: + s.robot.permanent_wrench_composer.set_forces_and_torques_index()) + frozen = importlib.import_module(TASK + 'contact_cycle_sim.mdp') + + class Controller: + def __init__(self, s, cfg): + self.s = s + self.q_ik = torch.ones(1, 3) + self.action = torch.zeros(1, 3) + self.refreshes = torch.zeros(1, dtype=torch.long) + + def reset(self, ids): + self.q_ik[ids] = 1 + self.refreshes[ids] = 0 + self.refresh(ids) + + def refresh(self, ids): + self.q_ik[ids] += .001 + self.refreshes[ids] += 1 + + monkeypatch.setattr(frozen, 'ReferenceController', Controller) + mdp = importlib.import_module(TASK + 'contact_cycle_sim.evaluation_mdp') + env_module = importlib.import_module(TASK + 'contact_cycle_sim.evaluation_env') + def config(): + return NS( + scene=NS(num_envs=1), sim=NS(dt=.002, render_interval=5), decimation=5, anchor_height=.3, + guide_enabled=True, auxiliary_enabled=True, + leg_bundle=load_bundle(ROOT / 'assets/generated/legs/go2_fl'), + cycle_parameters=json.loads((ROOT / 'configs/contact_cycle/go2_fl_v1.json').read_text()), + residual_parameters=json.loads((ROOT / 'configs/contact_cycle/simulation_residual_v1.json').read_text()), + support_parameters={'action_scale_rad': .5}) + + yield NS(env=env_module, mdp=mdp, base=base, frozen=frozen, config=config, Sim=Sim, Robot=Robot, Scene=Scene, + real_apply_loads=real_apply_loads) + for name in names: + sys.modules.pop(name, None) + + +def protocol(stage='P1'): + path = 'continuous_eval_p1_v1.json' if stage == 'P1' else 'bounded_diagnostic_p2_v1.json' + return load_protocol(ROOT / 'configs/contact_cycle' / path) + + +@pytest.fixture +def make_env(runtime): + opened = [] + + def make(stage='P1', trial=0, evidence=None, seam=None, safety=False): + hooks = runtime.env.RuntimeHooks(runtime.Sim, [(runtime.Robot, 'reset'), + (runtime.Robot, 'write_root_pose_to_sim'), (runtime.Scene, 'reset')]) + hooks.__enter__() + opened.append(hooks) + events = [] + + def record(event, s): + events.append(event) + return () if evidence is None else evidence(event, s) + + env = runtime.env.EvaluationContactCycleEnv(runtime.config(), protocol=protocol(stage), trial_index=trial, + runtime_hooks=hooks, evidence_callback=record, seam_callback=seam) + if not safety: + env._cycle_state.safety_causes = lambda: () + env.reset() + return env, hooks, events + + yield make + for hooks in reversed(opened): + hooks.__exit__() + + +def advance(env, controls): + result = None + for _ in range(controls): + result = env.step(torch.zeros(1, 3)) + if result.stop: + break + return result + + +def test_evaluation_runtime_initial_reset_instrumentation_and_pose_guards(make_env, runtime): + env, hooks, events = make_env() + assert hooks.initialization_steps == hooks.completed_steps == 2 + assert hooks.rollout_steps == 0 and hooks.reset_calls > 0 and hooks.pose_write_calls > 0 + assert [e.kind for e in events] == ['initial'] and events[0].clock.global_tick == 0 + assert events[0].held_control_index is None and env._cycle_state.policy().shape == (1, 46) + with pytest.raises(runtime.env.RuntimeViolation): + env.scene['robot'].write_root_pose_to_sim(None) + result = advance(env, 1) + assert result.stop.fatal and result.ticks_advanced == 0 and hooks.rollout_steps == 0 + with pytest.raises(runtime.env.RuntimeViolation): + env.reset() + with pytest.raises(runtime.env.RuntimeViolation): + env._cycle_state.controller.reset(torch.tensor([0])) + with pytest.raises(runtime.env.RuntimeViolation): + env._cycle_state.reset(torch.tensor([0])) + with pytest.raises(runtime.env.RuntimeViolation): + env.sim.step() + + +def test_evaluation_runtime_substep_two_failure_collects_all_causes_no_three(make_env): + def evidence(event, s): + return ('gate_overlap',) if event.kind == 'tick' and event.clock.global_tick == 2 else () + + env, hooks, events = make_env(evidence=evidence) + env._cycle_state.safety_causes = lambda: ('swing_clearance', 'premature_separation') if int( + env._cycle_state.substeps[0]) == 2 else () + result = advance(env, 1) + assert result.ticks_advanced == hooks.rollout_steps == 2 + assert result.stop.causes == ('swing_clearance', 'premature_separation', 'gate_overlap') + assert result.stop.completed_controls == 0 and result.stop.partial_control_ticks == 2 + assert [e.clock.global_tick for e in events if e.kind == 'tick'] == [1, 2] + assert env._cycle_state.controller.refreshes.item() == 1 + + +def test_evaluation_runtime_complete_clock_seam_order_carry_and_no_double_refresh(make_env, runtime): + seam_records = [] + + def seam(event, s): + seam_records.append((event, s.controller.q_ik.clone(), s.sensor_delay.data.clone(), + s.action_delay.data.clone(), torch.get_rng_state().clone(), + s.acquisition.timer.clone(), s.schedule.integral.clone())) + return runtime.env.SeamDecision(True) + + env, hooks, events = make_env(seam=seam) + s = env._cycle_state + anchors = s.anchors + advance(env, 899) + s.raw_sensors = lambda: torch.full((1, 18), .123) + s.schedule.integral[0, 0] = .4 # arbitrary carried scheduler memory, not event evidence + request = torch.tensor([[.4, .2, -.1]]) + first = env.step(request) + assert first.stop is None + assert torch.equal(s.residual_request, request) + assert torch.equal(env.action_manager.action, request) + assert s.action_delay.data.abs().sum() > 0 and s.sensed.abs().sum() > 0 + assert s.acquisition.acquired.item() and s.separation.armed.item() + assert events[-2].kind == 'tick' and events[-2].clock.local_tick == 4500 + assert events[-2].command.phase == 5 and events[-2].command.progress == 1 + assert events[-1].kind == 'next_reference' and events[-1].clock.local_tick == 0 + assert events[-1].command.phase == 0 and events[-1].clock.cycle_index == 1 + assert events[-2].reference_used_clock.global_tick == 4495 + assert events[-1].reference_next_clock.global_tick == 4500 + _, ik, sensor, action, rng, acquisition, integral = seam_records[0] + assert torch.equal(s.controller.q_ik, ik + .001) + assert torch.equal(sensor, s.sensor_delay.data) and torch.equal(action, s.action_delay.data) + assert torch.equal(rng, torch.get_rng_state()) + assert torch.equal(acquisition, s.acquisition.timer) and torch.equal(integral, s.schedule.integral) + assert s.anchors is anchors + snapshots = (s.controller.q_ik.clone(), s.controller.refreshes.clone(), torch.get_rng_state()) + assert torch.equal(s.policy(), s.policy()) + assert torch.equal(snapshots[0], s.controller.q_ik) and torch.equal(snapshots[2], torch.get_rng_state()) + reset_calls = hooks.reset_calls + last = advance(env, 1800) + assert last.stop.reason == 'protocol_complete' and not last.stop.fatal + assert last.stop.physics_ticks == hooks.rollout_steps == 13500 + assert last.stop.completed_controls == 2700 and last.stop.completed_clock_cycles == 3 + assert last.stop.partial_control_ticks == 0 and hooks.reset_calls == reset_calls + assert s.controller.refreshes.item() == 2700 # initial + 2699 next references, no final refresh + assert events[-1].kind == 'tick' and events[-1].clock.local_tick == 4500 + ticks = [e for e in events if e.kind == 'tick'] + assert [e.clock.global_tick for e in ticks] == list(range(1, 13501)) + assert ticks[4500].clock.cycle_index == 1 and ticks[4500].clock.local_tick == 1 + assert ticks[-1].held_control_index == 2699 and ticks[-1].action_origin_tick == 13495 + with pytest.raises(runtime.env.RuntimeViolation): + env.step(torch.zeros(1, 3)) + + +@pytest.mark.parametrize('decision', [None, False, 'denied']) +def test_evaluation_runtime_seam_admission_fails_closed(make_env, runtime, decision): + callback = None if decision is None else lambda e, s: ( + False if decision is False else runtime.env.SeamDecision(False, ('reload_dwell',))) + env, hooks, events = make_env(seam=callback) + result = advance(env, 901) + assert result.stop.physics_ticks == 4500 and hooks.rollout_steps == 4500 + assert result.stop.reason == 'failure' and events[-1].clock.local_tick == 4500 + assert env._cycle_state.controller.refreshes.item() == 900 + assert hooks.composer_buffer_replacements == 4500 # no extra setter/reset at seam + + +def test_evaluation_runtime_callback_failure_stops_even_at_partial_control(make_env): + def evidence(event, s): + if event.kind == 'tick' and event.clock.global_tick == 2: + raise OSError('storage failure') + return () + + env, hooks, _ = make_env(evidence=evidence) + result = advance(env, 1) + assert result.stop.fatal and 'recording_callback' in result.stop.causes[0] + assert hooks.rollout_steps == 2 + + +def test_evaluation_runtime_frozen_coordinate_layout_and_anchor_immutability(make_env): + env, _, _ = make_env() + s = env._cycle_state + anchors = s.anchors + s.origin.add_(100) + s.base_origin.add_(100) + s.command_clock = s.protocol.evidence_clock(0, 2500) + c = s.protocol_command() + policy = s.policy() + assert torch.allclose(policy[0, 28:31], torch.tensor(c.actor_target_displacement_world_m)) + assert tuple(c.root_target_world_m) == anchors.root_origin_world + assert policy.shape == (1, 46) + assert s.controller.refreshes.item() == 1 + with pytest.raises(AttributeError): + s.anchors = anchors + + +def test_evaluation_runtime_original_and_physical_overlapping_safety(make_env): + env, _, _ = make_env(safety=True) + s = env._cycle_state + s.command_clock = s.protocol.evidence_clock(0, 2400) + s.robot.data.applied_torque.torch.fill_(100000) + s.guide[..., 2] = 1 + s.separation.premature[:] = True + causes = s.safety_causes() + assert {'swing_clearance', 'premature_separation', 'hardware_effort_velocity', 'guide_vertical'} <= set(causes) + s.robot.data.root_state_w.torch[0, 0] = float('nan') + assert 'nonfinite' in s.safety_causes() + + +def test_p2_rows_restore_first_constructor_command_not_previous_terminal_foot(make_env, runtime, monkeypatch): + env, hooks, _ = make_env(stage='P2') + original = env._cycle_state.origin.clone() + root = env._cycle_state.base_origin.clone() + for row in range(19): + s = env._cycle_state + s.safety_causes = lambda: ('functional_failure',) + stopped = advance(env, 1).stop + env.acknowledge_finalized(stopped) + # Emulate the preceding physical terminal foot used by the new constructor. + dirty = original + torch.tensor([[.1 + row * .01, -.07, .02]]) + monkeypatch.setattr(runtime.base.LoadedState, 'foot_center', lambda _: dirty.clone()) + s.origin.add_(1.) # aliasing an old state must not change canonical commands + env.begin_trial(env.evaluation_protocol, row + 1) + new = env._cycle_state + assert torch.equal(new.origin, original) + assert torch.equal(new.base_origin, root) + assert new.anchors.anchor_a_world == tuple(original[0].tolist()) + assert not torch.equal(new.origin, dirty) + assert hooks.initialization_steps == 21 # no added physical reset/step + assert hooks.rollout_steps == 19 + + +def test_evaluation_runtime_p2_lifecycle_order_finalization_and_descriptor_install(make_env, runtime): + env, hooks, _ = make_env(stage='P2') + p = env.evaluation_protocol + with pytest.raises(runtime.env.RuntimeViolation): + env.begin_trial(p, 1) + for row in range(20): + s = env._cycle_state + s.safety_causes = lambda: ('functional_failure',) + assert int(s.scenarios[0]) == p.trial(row).scenario_id + assert s.trial.event_indices == p.trial(row).event_indices + assert s.sampler is None and s.schedule.commands is None + with pytest.raises(RuntimeError): + s.set_sampler(None) + result = advance(env, 1) + assert not result.stop.fatal and result.stop.physics_ticks == 1 + if row < 19: + with pytest.raises(runtime.env.RuntimeViolation): + env.begin_trial(p, row + 1) # must preserve evidence first + env.acknowledge_finalized(result.stop) + for wrong in (row, row + 2): + with pytest.raises(runtime.env.RuntimeViolation): + env.begin_trial(p, wrong) + with pytest.raises(runtime.env.RuntimeViolation): + env.begin_trial(protocol('P2'), row + 1) # even equal config is not same bound object + env.begin_trial(p, row + 1) + assert hooks.initialization_steps == 21 # constructor + one reset per row + assert hooks.rollout_steps == 20 and len(env._consumed_rows) == 20 + + +@pytest.mark.parametrize('cause', ['nonfinite', 'hardware_joint_limit', 'identity', 'illegal_reset_or_pose_write', + 'recording', 'storage', 'budget_expired', 'infrastructure']) +def test_evaluation_runtime_fatal_p2_row_cannot_restart(make_env, runtime, cause): + env, _, _ = make_env(stage='P2') + env._cycle_state.safety_causes = lambda: (cause,) + result = advance(env, 1) + env.acknowledge_finalized(result.stop) + with pytest.raises(runtime.env.RuntimeViolation): + env.begin_trial(env.evaluation_protocol, 1) + + +def test_evaluation_runtime_p1_cannot_begin_second_controller_in_launch(make_env, runtime): + env, _, _ = make_env() + env._cycle_state.safety_causes = lambda: ('functional_failure',) + result = advance(env, 1) + env.acknowledge_finalized(result.stop) + with pytest.raises(runtime.env.RuntimeViolation): + env.begin_trial(env.evaluation_protocol, 1) + + +def test_evaluation_runtime_bound_event_uses_existing_scheduler_and_onset_phase(make_env): + env, _, _ = make_env(stage='P2') + # Advance rows explicitly with preserved failure evidence, no ad-hoc descriptor overwrite. + for row in range(8): + env._cycle_state.safety_causes = lambda: ('functional_failure',) + result = advance(env, 1) + env.acknowledge_finalized(result.stop) + env.begin_trial(env.evaluation_protocol, row + 1) + s = env._cycle_state + t, phase = torch.tensor([4.02]), torch.tensor([2]) + edge = torch.tensor([True]) + s.schedule.step(t, phase, edge, ~edge, s.scenarios, .002, liftoff_phase=torch.tensor([1])) + assert not s.schedule.fired.any() # unload-onset is not a valid lift trigger + s.schedule.step(t, phase, edge, ~edge, s.scenarios, .002, liftoff_phase=torch.tensor([2])) + assert s.schedule.fired[0].tolist() == [False, True, False, False] + assert s.schedule.start[0, 1].item() == pytest.approx(4.04) + assert type(s).apply_loads is type(s).__mro__[2].apply_loads # inherited unique composer + + +def test_evaluation_runtime_installed_stepping_source_pin(runtime, tmp_path): + path = Path('/home/cen/IsaacLab/source/isaaclab/isaaclab/envs') + if not path.is_dir(): + pytest.skip('Installed Isaac sources not present') + runtime.env.verify_installed_stepping(path) # reads source only; no Isaac import/launch + for name in runtime.env.INSTALLED_ISAAC_SOURCES: + (tmp_path / name).write_bytes((path / name).read_bytes()) + (tmp_path / 'manager_based_rl_env.py').write_text('tampered') + with pytest.raises(RuntimeError, match='dependency changed'): + runtime.env.verify_installed_stepping(tmp_path) + + +def test_evaluation_runtime_unique_composer_internal_reset_permission_is_exception_safe(make_env, runtime): + env, hooks, _ = make_env() + composer = env._cycle_state.robot.permanent_wrench_composer + resets = hooks.reset_calls + result = advance(env, 1) + assert result.stop is None and hooks.composer_buffer_replacements == 5 + assert hooks.reset_calls == resets + composer.fail = True + result = advance(env, 1) + assert result.stop.fatal and result.ticks_advanced == 0 + assert hooks._load_owner is hooks._replacement_owner is None + with pytest.raises(runtime.env.RuntimeViolation): + composer.reset() + with pytest.raises(runtime.env.RuntimeViolation): + composer.set_forces_and_torques_index() + + +def test_evaluation_runtime_event_force_reaches_only_frozen_unique_composer(make_env, runtime): + env, hooks, _ = make_env(stage='P2') + for row in range(4): + env._cycle_state.safety_causes = lambda: ('functional_failure',) + result = advance(env, 1) + env.acknowledge_finalized(result.stop) + env.begin_trial(env.evaluation_protocol, row + 1) + s = env._cycle_state + s.time[:] = 1.5 + s.command_clock = s.protocol.evidence_clock(s.trial.index, 750) + composer = s.robot.permanent_wrench_composer + before = composer.set_calls + with hooks.wrench_update(composer): + runtime.real_apply_loads(s) + assert composer.set_calls == before + 1 + assert composer.last['is_global'] is True and composer.last['positions'] is None + assert torch.equal(composer.last['forces'][0, 0], torch.tensor([3., -4., -2.])) + assert s.schedule.fired.tolist() == [[True, False, False, False]] + assert s.scenarios.item() == 1 + + +def test_evaluation_runtime_budget_callback_before_any_additional_sim_step(runtime): + def before_step(hooks): + if hooks.attempted_steps > 2: + raise RuntimeError('budget expired') + + with runtime.env.RuntimeHooks(runtime.Sim, [], before_step=before_step) as hooks: + sim = runtime.Sim() + sim.step() + sim.step() + with pytest.raises(RuntimeError, match='budget expired'): + sim.step() + assert hooks.attempted_steps == 3 and hooks.completed_steps == 2 and sim.calls == 2 + + +def test_evaluation_runtime_wrong_state_cannot_be_silently_reused(runtime): + with pytest.raises(RuntimeError, match='Wrong evaluation state'): + runtime.mdp.state(NS(_cycle_state=object())) + + +def test_evaluation_runtime_p2_final_endpoint_then_new_initial_snapshot(make_env): + env, hooks, events = make_env(stage='P2') + original = env._cycle_state + resets = hooks.reset_calls + result = advance(env, 900) + assert result.stop.reason == 'protocol_complete' + assert result.stop.physics_ticks == result.stop.rollout_steps == 4500 + assert result.stop.completed_clock_cycles == 1 and result.stop.initialization_steps == 2 + assert hooks.reset_calls == resets and events[-1].kind == 'tick' + env.acknowledge_finalized(result.stop) + env.begin_trial(env.evaluation_protocol, 1) + assert env._cycle_state is not original + assert env._cycle_state.controller.refreshes.item() == 1 + assert events[-1].kind == 'initial' and events[-1].trial_index == 1 + assert events[-1].clock.global_tick == 0 and hooks.initialization_steps == 3 + invalid = env.step(torch.ones(1, 3)) # bound analytic-reference row cannot accept a residual + assert invalid.stop.fatal and invalid.ticks_advanced == 0 + assert invalid.stop.causes == ('identity_nonzero_reference_residual',) + + +def test_evaluation_runtime_illegal_reset_does_not_erase_earliest_failure(make_env, runtime): + env, _, _ = make_env() + env._cycle_state.safety_causes = lambda: ('swing_clearance',) + first = advance(env, 1).stop + with pytest.raises(runtime.env.RuntimeViolation): + env.reset() + assert env.stop_result.clock == first.clock + assert env.stop_result.causes == ('swing_clearance', 'illegal_reset_or_pose_write') + assert env.stop_result.fatal + + +@pytest.mark.parametrize('component,value', [(0, .01), (2, -.01), (5, .01)]) +def test_evaluation_runtime_original_fixture_direction_and_couple_floors(make_env, component, value): + env, _, _ = make_env(safety=True) + s = env._cycle_state + s.auxiliary[0, 0, component] = value + assert 'auxiliary_geometry' in s.safety_causes() + assert 'auxiliary_support_phase' in s.safety_causes() + + +def test_evaluation_runtime_next_reference_does_not_relabel_endpoint_physics(make_env, runtime): + env, _, events = make_env(seam=lambda e, s: runtime.env.SeamDecision(True)) + s = env._cycle_state + checks = [] + + def safety(): + checks.append(s.command_clock) + return () + + s.safety_causes = safety + advance(env, 900) + assert len(checks) == 4500 + assert checks[-1].local_tick == 4500 and checks[-1].cycle_index == 0 + assert events[-1].kind == 'next_reference' and events[-1].clock.local_tick == 0 + + +def test_initialize_trial_preserves_constructor_command_not_forwarded_foot(runtime, monkeypatch): + """Recorded P0/P1 tick-zero replay through real state/reference/model, CPU only.""" + import gzip + import hashlib + + from SkillFactory.legs.contact_cycle_sim import replay + from SkillFactory.legs.contact_cycle_sim.reference import ReferenceController + + directory = ROOT / 'logs/contact_cycle/frozen_replay_p0_01/run' + trace = Path('/home/cen/sf-runs/p1-01/launch-0/trial-00/trace.v1.jsonl.gz') + if not (directory / 'initial_state.pt').exists() or not trace.exists(): + pytest.skip('Recorded P0/P1 artifacts not present') + for path, expected in ( + (directory / 'initial_state.pt', '2e2b506b9d98d72cf64e32fcabfcc66561a694bb44a203408e95b960f29fa168'), + (directory / 'first_episodes.pt', 'd416095c66245f4c3359e619a04b0b092a2aca551cf0c23ca990b6b592d72149'), + (trace, 'c943489ad035ca94a9f3edb0307113ed67af0d7bfc90543963f0eac699bfc6ee'), + ): + assert hashlib.sha256(path.read_bytes()).hexdigest() == expected + initial = torch.load(directory / 'initial_state.pt', map_location='cpu', weights_only=False) + episode = torch.load(directory / 'first_episodes.pt', map_location='cpu', weights_only=False)['0'] + with gzip.open(trace, 'rt') as stream: + p1 = json.loads(next(stream))['state'] + assert (initial['policy_snapshot'][0] != torch.tensor(p1['policy'])).nonzero().flatten().tolist() == [ + 28, 29, 40, 41, 42] + physical = NS(foot=initial['target'].clone(), root=initial['root_state'].clone()) + loaded = runtime.base.LoadedState + monkeypatch.setattr(loaded, 'foot_center', lambda _: physical.foot.clone()) + monkeypatch.setattr(loaded, 'root', lambda _: ( + physical.root[:, :3], physical.root[:, 3:7], physical.root[:, 7:10], physical.root[:, 10:13])) + monkeypatch.setattr(loaded, 'contact', lambda _: initial['normal'].clone()) + monkeypatch.setattr(runtime.base.CycleState, 'raw_sensors', lambda _: initial['policy_snapshot'][:, :18].clone()) + monkeypatch.setattr(runtime.frozen, 'ReferenceController', ReferenceController) + robot_init = runtime.Robot.__init__ + + def initialize_robot(robot): + robot_init(robot) + robot.data.body_mass.torch[:] = 0 + robot.data.body_mass.torch[:, 0] = initial['weight'] / 9.81 + + monkeypatch.setattr(runtime.Robot, '__init__', initialize_robot) + cfg = runtime.config() + cfg.anchor_height = initial['root_state'][0, 2].item() + with runtime.env.RuntimeHooks(runtime.Sim, []) as hooks: + env = runtime.env.EvaluationContactCycleEnv(cfg, protocol=protocol(), trial_index=0, + runtime_hooks=hooks, evidence_callback=lambda *_: ()) + s = env._cycle_state + assert torch.equal(s.origin, initial['target']) + physical.foot = initial['foot'].clone() # reset/forward differs by 55.56 mm XY + env.reset() + assert s.anchors.anchor_a_world == tuple(initial['target'][0].tolist()) + assert s.anchors.root_origin_world == tuple(initial['root_state'][0, :3].tolist()) + assert s.anchors.initial_root_world == tuple(physical.root[0, :3].tolist()) + torch.testing.assert_close(s.controller.q_ik, initial['reference_ik_rad'], atol=2e-5, rtol=0) + torch.testing.assert_close(s.controller.action, initial['reference_next_normalized'], atol=1.5e-4, rtol=0) + actor = s.policy() + torch.testing.assert_close(actor, initial['policy_snapshot'], atol=1.5e-4, rtol=0) + # All non-IK features match exactly; only CPU/GPU finite-difference IK is approximate. + assert torch.equal(actor[:, :40], initial['policy_snapshot'][:, :40]) + assert torch.equal(actor[:, 43:], initial['policy_snapshot'][:, 43:]) + before = (s.controller.q_ik.clone(), s.controller.refreshes.clone(), torch.get_rng_state()) + assert torch.equal(actor, s.policy()) + assert all(torch.equal(a, b) for a, b in zip(before, ( + s.controller.q_ik, s.controller.refreshes, torch.get_rng_state()), strict=True)) + manifest = json.loads((ROOT / replay.CANDIDATE).with_name('manifest.json').read_bytes()) + model = replay.load_frozen_policy(ROOT / replay.CANDIDATE, manifest) + action = replay.inference_action(actor, 'learned', model)[0] + recorded_action = episode['requested_action'][0] + exact = replay.inference_action(initial['policy_snapshot'], 'learned', model)[0] + torch.testing.assert_close(exact, recorded_action, atol=1e-7, rtol=0) + torch.testing.assert_close(action, recorded_action, atol=3e-5, rtol=0) + env.term.process_actions(action) + processed = s.nominal + .5 * s.executed # bounded actuator's unchanged target formula + torch.testing.assert_close(processed, episode['processed_target'][0], atol=8e-5, rtol=0) + # Phase-zero old/new commands and held/next refresh clocks, without simulated motion. + for tick in (0, 5, 10, 205, 210): + s.time[:] = tick * .002 + s.command_clock = s.protocol.next_command_clock(0, tick) + for old, new in zip(runtime.base.CycleState.reference(s), s.reference(), strict=True): + torch.testing.assert_close(old, new, atol=3e-8, rtol=0) + if tick: + assert s.command_clock.global_tick == tick + assert s.protocol.evidence_clock(0, tick).global_tick == tick + assert s.controller.refreshes.item() == 1 # command/observation reads never refresh + + +def test_initialize_trial_commanded_root_is_not_measured_initial_root(runtime): + s = object.__new__(runtime.mdp.EvaluationCycleState) + s._anchors = None + s.origin = torch.tensor([[.0384319425, .0940605775, .022]]) + s.base_origin = torch.tensor([[0., 0., .3345983624]]) + measured = torch.tensor([[.003, -.004, .335]]) + s.root = lambda: (measured, None, None, None) + s.foot_center = lambda: torch.tensor([[-.0171082187, .0955000445, .0220000464]]) + s.env = NS(num_envs=1, device='cpu') + s.command_clock = protocol().next_command_clock(0, 0) + resets = [] + s.reset = lambda ids: resets.append(ids.tolist()) + s.initialize_trial() + assert s.anchors.root_origin_world == tuple(s.base_origin[0].tolist()) + assert s.anchors.initial_root_world == tuple(measured[0].tolist()) + assert s.anchors.anchor_a_world == tuple(s.origin[0].tolist()) + assert resets == [[0]] and s.reference_clock == s.command_clock + with pytest.raises(RuntimeError, match='already captured'): + s.initialize_trial() diff --git a/tests/test_contact_cycle_sim_evidence_codec.py b/tests/test_contact_cycle_sim_evidence_codec.py new file mode 100644 index 0000000..0c878fa --- /dev/null +++ b/tests/test_contact_cycle_sim_evidence_codec.py @@ -0,0 +1,320 @@ +"""CPU-only v2 codec bounds, crash prefixes, budget admission and copy parity.""" +import copy +import gzip +import json +import lzma +import math +import os +import subprocess +import sys +from pathlib import Path +from types import MappingProxyType + +import pytest +from SkillFactory.legs.contact_cycle_sim.evaluation_audit import ( + MAX_CHUNK_RAW_BYTES, MAX_CHUNK_RECORDS, MAX_RECORD_BYTES, TRACE_FILENAME, + XZ_DECODER_MEMORY_BYTES, plain, +) +import test_contact_cycle_sim_evaluation_audit as audit_tests +from test_contact_cycle_sim_evaluation_audit import ROOT, protocol, records + +# Reuse the source-named CPU-only fixtures without loading task registration. +recording = audit_tests.recording +offline_reader = audit_tests.offline_reader + + +def decoded_members(path): + data = path.read_bytes() + while data: + decoder = lzma.LZMADecompressor(memlimit=XZ_DECODER_MEMORY_BYTES) + raw = decoder.decompress(data) + assert decoder.eof + yield raw + data = decoder.unused_data + + +def make_recorder(recording, tmp_path, budget=lambda *args: None): + return recording.EvaluationRecorder(protocol(), 0, tmp_path / 'trial', + budget_check=budget, supplemental=lambda _: {}) + + +def test_plain_and_copied_exact_immutable_fast_path_parity(recording): + class SubInt(int): + pass + value = {'bool': True, 'int': 123, 'float': -.0, 'none': None, 'str': 'x', + 'subclass': SubInt(42), 'list': [1, {'nested': [2., 3]}], 'tuple': (7, 8)} + def old_plain(v): + if hasattr(v, 'items'): + return {k: old_plain(x) for k, x in v.items()} + if isinstance(v, (list, tuple)): + return [old_plain(x) for x in v] + return copy.deepcopy(v) + for fn in (plain, recording.copied): + result = fn(value) + assert result == old_plain(value) + assert type(result['subclass']) is SubInt + assert math.copysign(1., result['float']) == -1. + result['list'][1]['nested'][0] = 100 + assert value['list'][1]['nested'][0] == 2. + assert math.isnan(fn(float('nan'))) + assert plain(MappingProxyType(value)) == old_plain(value) + + +def test_chunk_record_bound_final_partial_and_exact_order(recording, tmp_path): + recorder = make_recorder(recording, tmp_path) + original = [{'kind': 'payload', 'sequence': i} for i in range(67)] + for record in original: + recorder._write(record) + assert len(recorder.pending) <= MAX_CHUNK_RECORDS + assert len(recorder.pending) == 3 + terminal = {'kind': 'terminal', 'runtime_stop': None, + 'external_failure': {'causes': ['cpu_test'], 'fatal': True, 'context': {}}, 'recording_faults': []} + recorder._write(terminal, stage='finalize') + members = list(decoded_members(recorder.directory / TRACE_FILENAME)) + assert [len(raw.splitlines()) for raw in members] == [64, 4] + assert [json.loads(line) for raw in members for line in raw.splitlines()] == original + [terminal] + assert not recorder.pending and recorder.pending_raw_bytes == 0 + recorder.stream.close() + + +def test_chunk_raw_bound_is_separate_from_unchanged_record_limit(recording, tmp_path): + assert MAX_RECORD_BYTES == 1024 * 1024 + recorder = make_recorder(recording, tmp_path) + overhead = len((json.dumps({'kind': 'payload', 'data': ''}, separators=(',', ':')) + '\n').encode()) + record = {'kind': 'payload', 'data': 'x' * (MAX_RECORD_BYTES - overhead)} + for _ in range(9): + recorder._write(record) + assert recorder.pending_raw_bytes <= MAX_CHUNK_RAW_BYTES + assert recorder.pending_raw_bytes == MAX_RECORD_BYTES + recorder._flush() + assert [len(raw) for raw in decoded_members(recorder.directory / TRACE_FILENAME)] == [8 * MAX_RECORD_BYTES, + MAX_RECORD_BYTES] + record['data'] += 'x' + with pytest.raises(ValueError, match='record exceeds'): + recorder._write(record) + recorder.stream.close() + + +@pytest.mark.parametrize('kind', ['initial', 'seam', 'step_result', 'terminal', 'capture_fault']) +def test_checkpoint_kinds_flush_pending_immediately(recording, tmp_path, kind): + recorder = make_recorder(recording, tmp_path) + recorder._write({'kind': 'payload'}) + assert recorder.written_bytes == 0 + recorder._write({'kind': kind}) + assert not recorder.pending + assert len(list(decoded_members(recorder.directory / TRACE_FILENAME))) == 1 + recorder.stream.close() + + +def test_first_physical_failure_persisted_before_callback_return(recording, tmp_path): + recorder = make_recorder(recording, tmp_path) + original = [] + for record in records(protocol(), end=2): + if record['kind'] == 'tick' and record['state']['substeps'] == 2: + record['state']['root'][0] = .031 + record['state']['ik_target'][0] -= .031 + original.append(copy.deepcopy(record)) + causes = recorder._record(record) + assert 'P1_root_horizontal' in causes + assert not recorder.pending + assert [json.loads(line) for raw in decoded_members(recorder.directory / TRACE_FILENAME) + for line in raw.splitlines()] == original + assert not (recorder.directory / 'metrics.json').exists() + recorder.stream.close() + + +def test_finalize_external_failure_flushes_partial_chunk_without_ack(recording, tmp_path): + recorder = make_recorder(recording, tmp_path) + original = list(records(protocol(), end=2)) + for record in original: + assert not recorder._record(record) + assert recorder.pending + ack = [] + report = recorder.finalize(external_failure={'causes': ['cpu_test'], 'fatal': True, 'context': {}}, + acknowledge=ack.append) + assert not ack and not report['functional_passed'] + raw = [json.loads(line) for member in decoded_members(recorder.directory / TRACE_FILENAME) + for line in member.splitlines()] + assert raw[:-1] == original and raw[-1]['kind'] == 'terminal' + + +@pytest.mark.parametrize('deny', [False, True]) +def test_exact_finalized_member_admission_includes_header_footer(recording, tmp_path, deny): + recorder = make_recorder(recording, tmp_path) + record = {'kind': 'payload', 'data': 'abc' * 2000} + recorder._write(record) + raw = b''.join(recorder.pending) + expected = lzma.compress(raw, preset=0) + calls = [] + def budget(stage, pending, written): + calls.append((stage, pending, written)) + assert pending == len(expected) and written == 0 + if pending > len(expected) - int(deny): + raise OSError('one byte over aggregate cap') + recorder.budget_check = budget + if deny: + with pytest.raises(OSError, match='aggregate'): + recorder._flush() + assert (recorder.directory / TRACE_FILENAME).stat().st_size == 0 + assert b''.join(recorder.pending) == raw + else: + recorder._flush() + assert (recorder.directory / TRACE_FILENAME).read_bytes() == expected + assert len(calls) == 1 + recorder.stream.close() + + +def test_pending_reservation_on_every_record_and_handled_cleanup(recording, tmp_path): + recorder = make_recorder(recording, tmp_path) + calls = [] + def budget(stage, pending, written): + calls.append((stage, pending, written)) + if stage == 'record': + assert pending >= recorder.pending_raw_bytes + raise OSError('aggregate pending reservation denied') + recorder.budget_check = budget + # Admission fails with evidence still available for the cleanup allowance. + assert recorder._record(next(records(protocol(), end=1))) + assert recorder.pending and recorder.written_bytes == 0 + ack = [] + recorder.finalize(external_failure={'causes': ['storage_test'], 'fatal': True, 'context': {}}, + acknowledge=ack.append) + assert not ack + assert len(calls) >= 4 # per-record reservation, final reservation, member, metrics + raw = lzma.decompress((recorder.directory / TRACE_FILENAME).read_bytes()) + assert [json.loads(line)['kind'] for line in raw.splitlines()] == ['initial', 'terminal'] + + +def test_forced_exit_loses_only_uncommitted_chunk_no_terminal(tmp_path, offline_reader): + directory = tmp_path / 'crashed' + code = ''' +import os, sys +from pathlib import Path +sys.path.insert(0, str(Path.cwd() / 'scripts')) +from profile_cycle_sim_evidence import modules +p, a, b, r = modules() +protocol = p.load_protocol(Path('configs/contact_cycle/continuous_eval_p1_v1.json')) +recorder = r.EvaluationRecorder(protocol, 0, Path(sys.argv[1]), budget_check=lambda *a: None, supplemental=lambda _: {}) +recorder._write({'kind': 'initial', 'saved': True}) +recorder._write({'kind': 'tick', 'uncommitted': True}) +os._exit(23) +''' + child = subprocess.run([sys.executable, '-c', code, str(directory)], cwd=ROOT, + env={**os.environ, 'CUDA_VISIBLE_DEVICES': ''}, timeout=30) + assert child.returncode == 23 + lines = list(offline_reader.trace_lines(directory / TRACE_FILENAME, compressed=True, max_bytes=2**20, + max_record_bytes=MAX_RECORD_BYTES, deadline=math.inf)) + assert [json.loads(line) for line in lines] == [{'kind': 'initial', 'saved': True}] + assert not (directory / 'metrics.json').exists() + + +@pytest.mark.parametrize('codec', ['gzip', 'xz']) +def test_reader_old_and_new_exact_record_and_total_decoded_bounds(offline_reader, tmp_path, codec): + path = tmp_path / ('trace.jsonl.gz' if codec == 'gzip' else TRACE_FILENAME) + lines = [b'a' * (MAX_RECORD_BYTES - 1) + b'\n', b'b\n', b'c' * 200000 + b'\n'] + encode = (lambda raw: gzip.compress(raw, mtime=0)) if codec == 'gzip' else (lambda raw: lzma.compress(raw, preset=0)) + path.write_bytes(b''.join(encode(line) for line in lines) if codec == 'gzip' else encode(b''.join(lines))) + kwargs = dict(compressed=True, max_bytes=2**20, max_record_bytes=MAX_RECORD_BYTES, deadline=math.inf) + assert list(offline_reader.trace_lines(path, **kwargs)) == lines + with pytest.raises(ValueError, match='decoded_limit'): + list(offline_reader.trace_lines(path, **kwargs, max_decoded_bytes=sum(map(len, lines)) - 1)) + + +@pytest.mark.parametrize('fault', ['memory', 'records', 'raw_bytes', 'record_bytes', 'split_record', 'truncated', 'corrupt']) +def test_xz_reader_rejects_bounds_and_damage(offline_reader, tmp_path, fault): + raw, preset = b'{"kind":"terminal"}\n', 0 + if fault == 'memory': + preset = 6 # 8MiB dictionary + overhead exceeds the decoder's 8MiB cap. + elif fault == 'records': + raw = b'{}\n' * 65 + elif fault == 'raw_bytes': + raw = (b'x' * (MAX_RECORD_BYTES - 1) + b'\n') * 9 + elif fault == 'record_bytes': + raw = b'x' * MAX_RECORD_BYTES + b'\n' + data = lzma.compress(raw, preset=preset) + if fault == 'split_record': + data = lzma.compress(raw[:5], preset=0) + lzma.compress(raw[5:], preset=0) + elif fault == 'truncated': + data = data[:-1] + elif fault == 'corrupt': + data = data[:-8] + bytes([data[-8] ^ 1]) + data[-7:] + path = tmp_path / TRACE_FILENAME + path.write_bytes(data) + with pytest.raises((ValueError, lzma.LZMAError, EOFError)): + list(offline_reader.trace_lines(path, compressed=True, max_bytes=2**20, + max_record_bytes=MAX_RECORD_BYTES, deadline=math.inf)) + + +def test_xz_deadline_checked_while_draining_output_and_empty_streams(offline_reader, tmp_path, monkeypatch): + path = tmp_path / TRACE_FILENAME + path.write_bytes(lzma.compress(b'a' * (MAX_RECORD_BYTES - 1) + b'\n', preset=0)) + now = 0 + def clock(): + nonlocal now + now += 1 + return now + monkeypatch.setattr(offline_reader.time, 'monotonic', clock) + with pytest.raises(ValueError, match='budget_audit'): + list(offline_reader.trace_lines(path, compressed=True, max_bytes=2**20, + max_record_bytes=MAX_RECORD_BYTES, deadline=10)) + path.write_bytes(lzma.compress(b'', preset=0) * 100) + now = 0 + with pytest.raises(ValueError, match='budget_audit'): + list(offline_reader.trace_lines(path, compressed=True, max_bytes=2**20, + max_record_bytes=MAX_RECORD_BYTES, deadline=10)) + + +def test_chunk_repeated_retained_records_compress_without_loss(recording, tmp_path): + # Saved rows are optional external evidence; the standalone report covers both. + path = Path('/home/cen/sf-runs/p1-03/launch-1/trial-01/trace.v1.jsonl.gz') + if not path.is_file(): + pytest.skip('retained row1 unavailable') + recorder = make_recorder(recording, tmp_path) + with gzip.open(path, 'rb') as source: + original = [source.readline() for _ in range(81)] + for raw in original: + recorder._write(json.loads(raw)) + recorder._flush() + trace = recorder.directory / TRACE_FILENAME + assert lzma.decompress(trace.read_bytes()) == b''.join(original) + legacy_bytes = sum(len(gzip.compress(raw, compresslevel=1, mtime=0)) for raw in original) + assert trace.stat().st_size < legacy_bytes / 4 + recorder.stream.close() + + +@pytest.mark.parametrize('tail', ['missing_terminal', 'truncated_terminal']) +def test_complete_physical_prefix_without_intact_terminal_never_passes( + recording, offline_reader, tmp_path, monkeypatch, tail): + from dataclasses import asdict + + from test_contact_cycle_sim_evaluation_audit import Stop + + p = protocol('P2') + recorder = recording.EvaluationRecorder(p, 0, tmp_path / 'trial', + budget_check=lambda *args: None, supplemental=lambda _: {}) + stop = plain(asdict(Stop(physics_ticks=4500, causes=(), reason='protocol_complete', + clock=plain(asdict(p.evidence_clock(0, 4500))), completed_controls=900, + partial_control_ticks=0, completed_clock_cycles=1, all_sim_steps_attempted=4502, + all_sim_steps_completed=4502, rollout_steps=4500, composer_buffer_replacements=4500))) + for record in records(p): + recorder._write(record) + if record['kind'] == 'tick' and record['state']['substeps'] % 5 == 0: + g = record['state']['substeps'] + recorder._write({'kind': 'step_result', 'ticks_advanced': 5, 'observed_tick': g, + 'reward': [0.], 'has_next_observation': g != 4500, 'stop': stop if g == 4500 else None}) + if tail == 'truncated_terminal': + recorder._write({'kind': 'terminal', 'runtime_stop': stop, 'external_failure': None, 'recording_faults': []}) + recorder.stream.close() + trace = recorder.directory / TRACE_FILENAME + if tail == 'truncated_terminal': + with trace.open('r+b') as stream: + stream.truncate(trace.stat().st_size - 4) + output = tmp_path / 'offline.json' + monkeypatch.setattr(sys, 'argv', ['audit', '--protocol', + str(ROOT / 'configs/contact_cycle/bounded_diagnostic_p2_v1.json'), '--trial-index', '0', + '--trace', str(trace), '--output', str(output), '--max-trace-bytes', str(2**26), '--wall-seconds', '30']) + assert offline_reader.main() == 1 + report = json.loads(output.read_text()) + assert report['observed_physics_ticks'] == 4500 and report['protocol_complete'] + assert not report['functional_passed'] + assert all(c.startswith(('recording_missing_terminal', 'recording_offline:')) for c in report['all_causes']) diff --git a/tests/test_contact_cycle_sim_learning.py b/tests/test_contact_cycle_sim_learning.py new file mode 100644 index 0000000..547e8bc --- /dev/null +++ b/tests/test_contact_cycle_sim_learning.py @@ -0,0 +1,369 @@ +"""Synthetic CPU-only 46D learner API tests; never robot-data optimization or Kit.""" + +import copy + +import pytest +import torch + +from SkillFactory.legs import standing_ppo +from SkillFactory.legs.contact_cycle.ppo import CycleActorCritic +from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES +from SkillFactory.legs.contact_cycle_sim import ppo +from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic, features +from SkillFactory.legs.policy import compute_gae + +MODEL_CONFIG = {"hidden": 16, "initial_std": .1} +CFG = {"horizon": 4, "failure_penalty": 10., "gamma": .99, "gae_lambda": .95, + "epochs": 2, "minibatch_size": 4, "clip_ratio": .2, "value_coefficient": .5, + "max_gradient_norm": 1., "target_kl": .02} + + +@pytest.fixture(autouse=True) +def cpu_synthetic_seed(): + torch.set_num_threads(1) + torch.manual_seed(71) + + +def learner(): + model = SimResidualActorCritic(**MODEL_CONFIG) + return model, torch.optim.Adam(model.parameters(), lr=3e-4, eps=1e-5) + + +class ValueModel: + def value(self, x): + # Return a view deliberately: bootstrap must not modify any input. + return x[:, 0] + + +def terminal_case(dtype=torch.int32): + x = torch.full((4, 46), -100.) # reset observations never supply terminal values + x[3] = 7. + terminal = torch.zeros(3, 46) + terminal[:, 0] = torch.tensor([30., 99., 20.]) + terminated = torch.tensor([True, False, False, False]) + truncated = torch.tensor([True, True, True, False]) + info = {"terminal_ids": torch.tensor([2, 0, 1], dtype=dtype), + "terminal_observation": {"policy": terminal}} + return x, terminated, truncated, info + + +def test_reexports_are_unchanged_generic_math_and_46d_features(): + assert ppo.features is features + assert ppo.ppo_step is standing_ppo.ppo_step + assert ppo.rng_state is standing_ppo.rng_state + assert ppo.restore_rng is standing_ppo.restore_rng + + +@pytest.mark.parametrize("dtype", [torch.int32, torch.int64]) +def test_bootstrap_timeout_prereset_failure_zero_and_no_alias(dtype): + x, terminated, truncated, info = terminal_case(dtype) + before = x.clone() + actual = ppo.bootstrap_values(ValueModel(), x, terminated, truncated, info) + torch.testing.assert_close(actual, torch.tensor([0., 20., 30., 7.])) + torch.testing.assert_close(x, before) + torch.testing.assert_close(info["terminal_observation"]["policy"][:, 0], torch.tensor([30., 99., 20.])) + # True failures need no terminal observations, even when also truncated. + torch.testing.assert_close(ppo.bootstrap_values(ValueModel(), x, torch.ones(4, dtype=torch.bool), + torch.ones(4, dtype=torch.bool), {}), torch.zeros(4)) + + +@pytest.mark.parametrize("fault", ["missing", "missing_policy", "32d", "count", "duplicate", "float_ids", + "negative_id", "out_of_range", "coverage", "nonterminal_id", "nan"]) +def test_bootstrap_rejects_bad_terminal_contract(fault): + x, terminated, truncated, info = terminal_case() + if fault == "missing": + info = {} + elif fault == "missing_policy": + info["terminal_observation"] = {} + elif fault == "32d": + info["terminal_observation"]["policy"] = torch.zeros(3, 32) + elif fault == "count": + info["terminal_observation"]["policy"] = torch.zeros(2, 46) + elif fault == "nan": + info["terminal_observation"]["policy"][0, 0] = float("nan") + else: + info["terminal_ids"] = { + "duplicate": torch.tensor([2, 2, 1]), "float_ids": torch.tensor([2., 0., 1.]), + "negative_id": torch.tensor([2, -1, 1]), "out_of_range": torch.tensor([2, 4, 1]), + "coverage": torch.tensor([2, 0]), "nonterminal_id": torch.tensor([2, 3, 1]), + }[fault] + with pytest.raises((ValueError, RuntimeError)): + ppo.bootstrap_values(ValueModel(), x, terminated, truncated, info) + + +@pytest.mark.parametrize("bad", [torch.zeros(2, 32), torch.zeros(2, 46, dtype=torch.float64), + torch.full((2, 46), float("nan")), torch.zeros(0, 46)]) +def test_features_reject_old_or_malformed_data(bad): + with pytest.raises(ValueError): + ppo.features(bad) + + +def test_bootstrap_shapes_types_and_nonfinite_value(): + x, terminated, truncated, info = terminal_case() + for bad in (terminated.float(), terminated[:, None], terminated[:1]): + with pytest.raises(ValueError): + ppo.bootstrap_values(ValueModel(), x, bad, truncated, info) + + class BadValue: + def value(self, x): + return torch.full((len(x),), float("nan")) + + with pytest.raises(ValueError): + ppo.bootstrap_values(BadValue(), x, terminated, truncated, info) + + +def test_gae_failure_and_timeout_end_recursion_but_timeout_bootstraps(): + x, terminated, truncated, info = terminal_case() + nv = ppo.bootstrap_values(ValueModel(), x, terminated, truncated, info) + rewards = torch.tensor([[1., 1., 1., 1.], [100., 100., 100., 100.]]) + values = torch.zeros_like(rewards) + next_values = torch.stack([nv, torch.zeros(4)]) + done = torch.stack([terminated | truncated, torch.ones(4, dtype=torch.bool)]) + adv, returns = compute_gae(rewards, values, next_values, done, .9, .5) + torch.testing.assert_close(adv[0], torch.tensor([1., 19., 28., 52.3])) + torch.testing.assert_close(returns, adv) + + +class ReusedBufferEnv: + num_envs, device, step_dt = 2, "cpu", .01 + + def __init__(self, fault=None): + self.x = torch.zeros(2, 46) + self.phase = torch.zeros(2, dtype=torch.int32) + self.flags = torch.zeros(2, len(FAILURE_NAMES), dtype=torch.bool) + self.steps, self.fault = 0, fault + + def step(self, action): + assert not torch.is_grad_enabled() + assert action.shape == (2, 3) and (action.abs() <= 1).all() + self.x.fill_(self.steps + 1.) + self.phase.fill_(self.steps % 6) # phase of pre-step state, not next observation + self.flags.zero_() + self.flags[0, 0] = True + terminal = torch.full((2, 46), 10. + self.steps) + self.steps += 1 + info = {"terminal_ids": torch.tensor([0, 1], dtype=torch.int32), + "terminal_observation": {"policy": terminal}, + "cycle_step": {"phase": self.phase, "failure_flags": self.flags}} + reward = torch.full((2,), .01) + if self.fault == "missing": + del info["cycle_step"] + elif self.fault == "phase": + info["cycle_step"]["phase"] = torch.full((2,), -1) + elif self.fault == "flags": + info["cycle_step"]["failure_flags"] = torch.zeros(2, 7, dtype=torch.bool) + elif self.fault == "reward": + reward[0] = float("inf") + elif self.fault == "reward_shape": + reward = reward[:, None] + return {"policy": self.x}, reward, torch.tensor([True, False]), torch.tensor([True, True]), info + + +def test_collect_preserves_prestep_buffers_diagnostics_logp_and_reward(): + model, _ = learner() + env = ReusedBufferEnv() + calls = [] + data, nx, failures, timeouts = ppo.collect_rollout(env, model, env.x, CFG, lambda: calls.append(True)) + assert len(calls) == CFG["horizon"] and failures == timeouts == 4 + assert data["features"].shape == (4, 2, 46) and data["latent"].shape == (4, 2, 3) + for step in range(4): + torch.testing.assert_close(data["features"][step], torch.full((2, 46), float(step))) + assert (data["phase"][step] == step).all() + logp, values = model.evaluate(data["features"].flatten(0, 1), data["latent"].flatten(0, 1)) + torch.testing.assert_close(logp, data["log_prob"].flatten()) + torch.testing.assert_close(values, data["value"].flatten()) + torch.testing.assert_close(data["reward"], torch.tensor([[-9., 1.]]).expand(4, 2)) + assert data["done"].all() and not data["next_value"][:, 0].any() + for step in range(4): + torch.testing.assert_close(data["next_value"][step, 1], model.value(torch.full((1, 46), 10. + step))[0]) + assert all(not v.requires_grad for v in data.values()) + env.x.fill_(100.) + env.phase.fill_(5) + env.flags.zero_() + assert (nx == 4).all() and (data["phase"][0] == 0).all() + assert data["failure_flags"][:, 0, 0].all() + + +@pytest.mark.parametrize("fault", ["missing", "phase", "flags", "reward", "reward_shape"]) +def test_collect_missing_or_malformed_diagnostics_fails_before_optimizer(fault): + model, optimizer = learner() + before = copy.deepcopy(model.state_dict()) + env = ReusedBufferEnv(fault) + with pytest.raises((ValueError, RuntimeError)): + data, *_ = ppo.collect_rollout(env, model, env.x, CFG) + ppo.ppo_step(model, optimizer, data, CFG) + assert not optimizer.state + for key, value in before.items(): + torch.testing.assert_close(value, model.state_dict()[key], rtol=0, atol=0) + + +def test_synthetic_generic_ppo_and_immutable_pre_normalization_diagnostics(): + model, optimizer = learner() + env = ReusedBufferEnv() + data, *_ = ppo.collect_rollout(env, model, env.x, CFG) + before_data = copy.deepcopy(data) + before_model = copy.deepcopy(model.state_dict()) + diagnostics = ppo.rollout_diagnostics(data, CFG) + assert diagnostics["missing_diagnostic_samples"] == 0 + assert diagnostics["phase_control_samples"] == [2, 2, 2, 2, 0, 0] + assert diagnostics["terminal_causes"][FAILURE_NAMES[0]] == 4 + adv, _ = compute_gae(data["reward"], data["value"], data["next_value"], data["done"], .99, .95) + assert diagnostics["advantage_before_normalization"]["mean"] == pytest.approx(float(adv.mean())) + metrics = ppo.ppo_step(model, optimizer, data, CFG) + assert metrics["sampling_log_prob_error"] < 2e-4 and metrics["minibatches"] > 0 + assert any(not torch.equal(v, model.state_dict()[k]) for k, v in before_model.items()) + assert ppo.rollout_diagnostics(data, CFG) == diagnostics + for key in data: + torch.testing.assert_close(data[key], before_data[key], rtol=0, atol=0) + # The generic learner itself rejects stale/corrupted sampling log probabilities. + data["log_prob"].add_(10.) + with pytest.raises(RuntimeError, match="mismatch"): + ppo.ppo_step(model, optimizer, data, CFG) + + +def test_selection_key_lexicographic_all_rows_and_failure_cost(): + good = {"success": True, "height_rms_m": .06, "swing_rms_m": .025, "landing_error_m": .05} + assert ppo.selection_key([good]) == (0., 3.) + assert ppo.selection_key([good, {"success": False}]) == (.5, 9.5) + assert ppo.selection_key([{}]) == (1., 16.) + worst_success = {**good, "height_rms_m": 100., "swing_rms_m": 100., "landing_error_m": 100.} + assert ppo.selection_key([worst_success]) < ppo.selection_key([good, {}]) + for bad in ([], [{"success": True}], [{**good, "height_rms_m": float("nan")}], + [{**good, "landing_error_m": -1.}], [{**good, "success": "false"}]): + with pytest.raises(ValueError): + ppo.selection_key(bad) + + +def saved_learner(tmp_path): + model, optimizer = learner() + env = ReusedBufferEnv() + data, *_ = ppo.collect_rollout(env, model, env.x, CFG) + ppo.ppo_step(model, optimizer, data, CFG) + path = tmp_path / "residual.pt" + identity = {"source_sha256": "synthetic-source", "task": ppo.CONTRACT} + size = ppo.save_checkpoint(path, model, optimizer, identity, 1, {"update": 1, "key": [0., 3.]}, + {"reserved_transitions": 8}, MODEL_CONFIG) + return path, model, optimizer, identity, size + + +def assert_tree_equal(a, b): + if isinstance(a, torch.Tensor): + torch.testing.assert_close(a, b, rtol=0, atol=0) + elif isinstance(a, dict): + assert a.keys() == b.keys() + for key in a: + assert_tree_equal(a[key], b[key]) + elif isinstance(a, (tuple, list)): + assert type(a) is type(b) and len(a) == len(b) + for av, bv in zip(a, b): + assert_tree_equal(av, bv) + else: + assert a == b + + +def test_checkpoint_distinct_atomic_exact_bytes_rng_and_optimizer_roundtrip(tmp_path): + path, model, optimizer, identity, size = saved_learner(tmp_path) + assert size == path.stat().st_size and list(tmp_path.iterdir()) == [path] + payload = torch.load(path, weights_only=True) + assert payload["format"] == ppo.CHECKPOINT_FORMAT + assert payload["contract"] == ppo.CONTRACT and payload["features"] == model.feature_version + assert payload["accepted_skill"] is False and "sampler" not in payload + assert payload["model_config"] == MODEL_CONFIG and payload["budget_snapshot"] == {"reserved_transitions": 8} + expected_random = torch.rand(12) + with torch.no_grad(): + model.actor[-1].bias.add_(1.) + optimizer.param_groups[0]["lr"] = .005 + restored = ppo.restore_checkpoint(path, model, optimizer, identity, MODEL_CONFIG) + assert restored["update"] == 1 + assert_tree_equal(model.state_dict(), payload["model"]) + assert_tree_equal(optimizer.state_dict(), payload["optimizer"]) + torch.testing.assert_close(torch.rand(12), expected_random, rtol=0, atol=0) + original = path.read_bytes() + with pytest.raises(RuntimeError, match="budget"): + ppo.save_checkpoint(path, model, optimizer, identity, 2, None, {}, MODEL_CONFIG, max_bytes=1) + assert path.read_bytes() == original and list(tmp_path.iterdir()) == [path] + + +@pytest.mark.parametrize("fault", ["format", "contract", "features", "identity", "hidden", "config_type", + "update", "accepted", "model_shape", "model_dtype", "model_nan", "scale", + "optimizer_nan", "optimizer_shape", "optimizer_type", "optimizer_lr_type", + "optimizer_slot_type", "optimizer_missing_slot", "optimizer_step", "rng", "budget", "sampler"]) +def test_checkpoint_rejects_before_mutating_any_learner_or_rng(tmp_path, fault): + path, model, optimizer, identity, _ = saved_learner(tmp_path) + payload = torch.load(path, weights_only=True) + if fault in ("format", "contract", "features"): + payload[fault] = "legacy-32D-standing-or-reach" + elif fault == "identity": + payload["identity"]["source_sha256"] = "different-source" + elif fault == "hidden": + payload["model_config"]["hidden"] = 32 + elif fault == "config_type": + payload["model_config"]["hidden"] = 16. + elif fault == "update": + payload["update"] = True + elif fault == "accepted": + payload["accepted_skill"] = True + elif fault == "model_shape": + payload["model"]["actor.0.weight"] = torch.zeros(16, 32) + elif fault == "model_dtype": + payload["model"]["log_std"] = payload["model"]["log_std"].double() + elif fault == "model_nan": + payload["model"]["log_std"][0] = float("nan") + elif fault == "scale": + payload["model"]["scale"][0] = 0. + elif fault == "optimizer_nan": + payload["optimizer"]["param_groups"][0]["lr"] = float("nan") + elif fault == "optimizer_shape": + payload["optimizer"]["state"][0]["exp_avg"] = torch.zeros(32) + elif fault == "optimizer_type": + payload["optimizer_type"] = "SGD" + elif fault == "optimizer_lr_type": + payload["optimizer"]["param_groups"][0]["lr"] = "invalid" + elif fault == "optimizer_slot_type": + payload["optimizer"]["state"][0]["exp_avg"] = 0. + elif fault == "optimizer_missing_slot": + del payload["optimizer"]["state"][0]["exp_avg"] + elif fault == "optimizer_step": + payload["optimizer"]["state"][0]["step"] = torch.tensor(.5) + elif fault == "rng": + payload["rng"]["cpu"] = torch.zeros(2, dtype=torch.uint8) + elif fault == "budget": + payload["budget_snapshot"] = None + elif fault == "sampler": + payload["sampler"] = {} + torch.save(payload, path) + before = copy.deepcopy((model.state_dict(), optimizer.state_dict(), ppo.rng_state())) + with pytest.raises(ValueError): + ppo.restore_checkpoint(path, model, optimizer, identity, MODEL_CONFIG) + assert_tree_equal(before, (model.state_dict(), optimizer.state_dict(), ppo.rng_state())) + + +def test_save_rejects_wrong_model_configuration_type_and_nonfinite_state(tmp_path): + model, optimizer = learner() + old = CycleActorCritic(hidden=16) + cases = [(old, torch.optim.Adam(old.parameters()), MODEL_CONFIG), + (model, optimizer, {"hidden": 32, "initial_std": .1}), + (model, optimizer, {"hidden": 16, "initial_std": True}), + (model, torch.optim.Adam(old.parameters()), MODEL_CONFIG)] + for candidate, opt, cfg in cases: + with pytest.raises(ValueError): + ppo.save_checkpoint(tmp_path / "bad.pt", candidate, opt, {}, 0, None, {}, cfg) + with torch.no_grad(): + model.log_std[0] = float("inf") + with pytest.raises(ValueError): + ppo.save_checkpoint(tmp_path / "bad.pt", model, optimizer, {}, 0, None, {}, MODEL_CONFIG) + assert not list(tmp_path.iterdir()) + + +def test_atomic_replace_failure_keeps_destination_and_removes_temporary(tmp_path, monkeypatch): + path, model, optimizer, identity, _ = saved_learner(tmp_path) + original = path.read_bytes() + + def refuse(source, destination): + assert source.exists() and destination == path + raise OSError("synthetic replace failure") + + monkeypatch.setattr(ppo.os, "replace", refuse) + with pytest.raises(OSError, match="synthetic"): + ppo.save_checkpoint(path, model, optimizer, identity, 2, None, {}, MODEL_CONFIG) + assert path.read_bytes() == original and list(tmp_path.iterdir()) == [path] diff --git a/tests/test_contact_cycle_sim_learning_physics.py b/tests/test_contact_cycle_sim_learning_physics.py new file mode 100644 index 0000000..c610217 --- /dev/null +++ b/tests/test_contact_cycle_sim_learning_physics.py @@ -0,0 +1,42 @@ +"""Opt-in nonoptimizing API preflight. A skipped test is not physical evidence.""" + +import json +import os +import subprocess +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[1] + + +@pytest.mark.skipif(os.environ.get("SIM_RESIDUAL_LEARNING_API_TESTS") != "1", + reason="requires explicit supervised nonlearning PhysX API probe") +def test_contact_cycle_sim_learning_runtime(tmp_path): + env = os.environ.copy() + env.pop("CONDA_PREFIX", None) + env.pop("VIRTUAL_ENV", None) + env["PYTHONPATH"] = str(ROOT / "source/SkillFactory") + env["PYTHONDONTWRITEBYTECODE"] = "1" + process = subprocess.run([ + "/usr/bin/python3", str(ROOT / "scripts/supervise_sim.py"), "--timeout", "240", + "--log", str(tmp_path / "console.log"), "--report", str(tmp_path / "supervisor.json"), "--", + env.get("ISAACLAB_LAUNCHER", "/home/cen/IsaacLab/isaaclab.sh"), "-p", + str(ROOT / "scripts/verify_cycle_sim_learning_runtime.py"), + "--output", str(tmp_path / "probe"), "--headless", + ], cwd=ROOT, env=env, capture_output=True, text=True, timeout=280) + assert process.returncode == 0, process.stdout[-4000:] + process.stderr[-4000:] + report = json.loads((tmp_path / "probe/result.json").read_text()) + supervisor = json.loads((tmp_path / "supervisor.json").read_text()) + assert report["passed"] and supervisor["passed"] and supervisor["cleanup"]["clean"] + assert not supervisor["critical_log_lines"] + assert report["optimizer_steps"] == report["training_transitions"] == 0 + assert report["nonoptimizing_api_transitions"] == 512 + assert report["model_sha256_before"] == report["model_sha256_after"] + assert not report["accepted_skill"] and not report["functional_claim"] + assert not report["failure_term_disabled"] and report["acquisition_deadline_s"] == .2 + for name in ("timeout", "true_failure"): + fixture = report["fixtures"][name] + assert all(fixture["checks"].values()) + assert fixture["logprob_max_abs_error"] < 2e-4 + assert fixture["max_actual_residual_rad"] <= .050001 diff --git a/tests/test_contact_cycle_sim_native_monitor.py b/tests/test_contact_cycle_sim_native_monitor.py new file mode 100644 index 0000000..90943f6 --- /dev/null +++ b/tests/test_contact_cycle_sim_native_monitor.py @@ -0,0 +1,589 @@ +"""CPU-only native coverage tests on synthetic metadata and owned temporary trees.""" +import errno +import importlib.util +import os +import stat +from pathlib import Path +from types import SimpleNamespace as NS + +import pytest + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location('native_monitor_test', ROOT / 'scripts/evaluate_cycle_sim_residual.py') +r = importlib.util.module_from_spec(spec) +spec.loader.exec_module(r) + + +def permission_case(tmp_path, monkeypatch, *, owner=None, code=errno.EACCES, access=False, recheck=None): + """Never chmod, chown or inspect another user's tree; model the OS denial.""" + denied = tmp_path / 'unrelated-private' + denied.mkdir() + (tmp_path / 'visible').write_bytes(b'12345') + real_stat, real_open, real_access = r.os.stat, r.os.open, r.os.access + calls = NS(stats=0, opens=0, access=0) + owner = os.geteuid() + 1 if owner is None else owner + + def metadata(path, *args, **kwargs): + info = real_stat(path, *args, **kwargs) + if path == denied.name and kwargs.get('dir_fd') is not None: + calls.stats += 1 + values = {k: getattr(info, k) for k in dir(info) if k.startswith('st_')} + values.update(st_uid=owner, st_mode=stat.S_IFDIR | 0o700) + if calls.stats > 1 and recheck: + recheck(values, calls.stats) + return NS(**values) + return info + + def opening(path, flags, *args, **kwargs): + assert flags & os.O_NOFOLLOW + if path == denied.name and kwargs.get('dir_fd') is not None: + calls.opens += 1 + raise OSError(code, 'injected directory operation failure', str(denied)) + return real_open(path, flags, *args, **kwargs) + + def accessible(path, *args, **kwargs): + if path == denied.name and kwargs.get('dir_fd') is not None: + assert kwargs['effective_ids'] and not kwargs['follow_symlinks'] + calls.access += 1 + return access + return real_access(path, *args, **kwargs) + + monkeypatch.setattr(r.os, 'stat', metadata) + monkeypatch.setattr(r.os, 'open', opening) + monkeypatch.setattr(r.os, 'access', accessible) + return denied, calls + + +@pytest.mark.parametrize('code', [errno.EACCES, errno.EPERM]) +def test_native_size_shared_unrelated_permission_gap_exact_evidence(tmp_path, monkeypatch, code): + denied, calls = permission_case(tmp_path, monkeypatch, code=code) + result = r.native_size(tmp_path, shared=True) + assert result['logical_bytes'] == 5 and result['skipped_count'] == 1 + assert result['permission_gaps'] == [{ + 'path': str(denied), 'errno': code, 'operation': 'open', + 'uid': os.geteuid() + 1, 'gid': denied.lstat().st_gid, + 'mode': stat.S_IFDIR | 0o700, 'dev': denied.lstat().st_dev, + 'ino': denied.lstat().st_ino, 'ctime_ns': denied.lstat().st_ctime_ns}] + assert calls.stats == 3 and calls.opens == 1 and calls.access == 1 + assert result['free_space']['observed_path'] == str(tmp_path) + assert result['free_space']['free_bytes'] > 0 + + +@pytest.mark.parametrize('protection', ['owned_uid', 'strict_root', 'task_cache', 'strict_descendant', 'not_shared']) +def test_native_size_owned_or_task_owned_paths_never_excluded(tmp_path, monkeypatch, protection): + denied, _ = permission_case(tmp_path, monkeypatch, + owner=os.geteuid() if protection == 'owned_uid' else None) + strict = {'strict_root': [tmp_path], 'task_cache': [denied], + 'strict_descendant': [denied / 'task-cache']}.get(protection, []) + with pytest.raises(r.NativeScanError) as caught: + r.native_size(tmp_path, shared=protection != 'not_shared', strict_paths=strict) + failure = caught.value.report['failure'] + assert failure['path'] == str(denied) and failure['errno'] == errno.EACCES + assert failure['metadata']['mode'] == stat.S_IFDIR | 0o700 + assert caught.value.report['skipped_count'] == 0 + + +@pytest.mark.parametrize('code', [errno.EIO, errno.ESTALE, errno.EMFILE, errno.ELOOP]) +def test_native_size_other_io_errors_fatal(tmp_path, monkeypatch, code): + denied, _ = permission_case(tmp_path, monkeypatch, code=code) + with pytest.raises(r.NativeScanError) as caught: + r.native_size(tmp_path, shared=True) + assert caught.value.report['failure']['path'] == str(denied) + assert caught.value.report['failure']['errno'] == code + assert caught.value.report['skipped_count'] == 0 + + +@pytest.mark.parametrize('change', ['unknown', 'disappeared', 'owner', 'inode', 'mode', 'ctime', 'access']) +def test_native_size_permission_metadata_races_fail_closed(tmp_path, monkeypatch, change): + def recheck(values, count): + if change in ('unknown', 'disappeared'): + code = errno.EACCES if change == 'unknown' else errno.ENOENT + raise OSError(code, 'injected metadata unavailable') + field = {'owner': 'st_uid', 'inode': 'st_ino', 'mode': 'st_mode', 'ctime': 'st_ctime_ns'}.get(change) + if field: + values[field] += 1 + denied, _ = permission_case(tmp_path, monkeypatch, access=change == 'access', recheck=recheck) + with pytest.raises(r.NativeScanError) as caught: + r.native_size(tmp_path, shared=True) + assert caught.value.report['failure']['path'] == str(denied) + assert caught.value.report['skipped_count'] == 0 + + +def test_native_size_unknown_initial_owner_is_fatal(tmp_path, monkeypatch): + denied = tmp_path / 'unknown' + denied.mkdir() + real_stat = r.os.stat + + def unknown(path, *args, **kwargs): + if path == denied.name and kwargs.get('dir_fd') is not None: + raise PermissionError(errno.EACCES, 'metadata unavailable', str(denied)) + return real_stat(path, *args, **kwargs) + + monkeypatch.setattr(r.os, 'stat', unknown) + with pytest.raises(r.NativeScanError) as caught: + r.native_size(tmp_path, shared=True) + assert caught.value.report['failure']['metadata'] is None + assert caught.value.report['failure']['path'] == str(denied) + + +def test_native_size_disappearance_is_recorded_except_required_root(tmp_path, monkeypatch): + (tmp_path / 'vanished').write_text('gone') + real_stat = r.os.stat + + def missing(path, *args, **kwargs): + if path == 'vanished' and kwargs.get('dir_fd') is not None: + raise FileNotFoundError(errno.ENOENT, 'injected churn') + return real_stat(path, *args, **kwargs) + + monkeypatch.setattr(r.os, 'stat', missing) + result = r.native_size(tmp_path) + assert result['logical_bytes'] == 0 + assert result['disappeared_paths'] == [str(tmp_path / 'vanished')] + assert r.native_size(tmp_path / 'missing')['disappeared_paths'] == [str(tmp_path / 'missing')] + with pytest.raises(r.NativeScanError, match='required_directory'): + r.native_size(tmp_path / 'missing', required=True) + + +@pytest.mark.parametrize('where', ['root', 'ancestor', 'owned_child', 'shared_child']) +def test_native_size_never_follows_symlinks(tmp_path, where): + target = tmp_path / 'target' + target.mkdir() + (target / 'data').write_bytes(b'not counted') + scanned = tmp_path / 'scanned' + scanned.mkdir() + link = scanned / 'link' + link.symlink_to(target, target_is_directory=True) + if where == 'shared_child': + result = r.native_size(scanned, shared=True) + assert result['logical_bytes'] == 0 and result['symlink_paths'] == [str(link)] + else: + path = {'root': link, 'ancestor': link / 'nested', 'owned_child': scanned}[where] + with pytest.raises(r.NativeScanError, match='symlink'): + r.native_size(path, strict_paths=[scanned] if where == 'owned_child' else []) + + +def test_native_size_directory_replaced_between_stat_and_open_fails(tmp_path, monkeypatch): + child = tmp_path / 'child' + child.mkdir() + replacement = tmp_path / 'replacement' + replacement.mkdir() + real_open = r.os.open + + def raced(path, flags, *args, **kwargs): + if path == child.name and kwargs.get('dir_fd') is not None: + return real_open(replacement, flags) + return real_open(path, flags, *args, **kwargs) + + monkeypatch.setattr(r.os, 'open', raced) + with pytest.raises(r.NativeScanError, match='open_identity_changed'): + r.native_size(child, shared=True) + + +def local_policy(root, shared): + return {'artifact_root': str(root), 'native_paths': [str(root / 'native'), str(shared)], + 'shared_native_paths': [str(shared)], 'task_owned_cache_paths': [str(root / 'native/cache')], + 'free_space_paths': [str(root), str(shared.parent)], + 'free_space_mount_identities': {str(p): r.mount_identity(p) for p in (root, shared.parent)}, + 'mount_identity': r.mount_identity(root), 'min_free_bytes': 1, + 'check_interval_seconds': 1, 'max_native_growth_bytes': 100} + + +def test_native_monitor_disjoint_baseline_growth_outputs_and_gap_history(tmp_path, monkeypatch): + # Campaign nested in a shared root: no duplicate native/output growth charges. + root = tmp_path / 'campaign' + (root / 'native/cache').mkdir(parents=True) + (root / 'native/cache/data').write_bytes(b'123') + (root / 'report').write_bytes(b'output') + denied, _ = permission_case(tmp_path, monkeypatch) + policy = local_policy(root, tmp_path) + clock = NS(now=10) + budget = NS(boottime=lambda: clock.now, STORAGE_SCOPE='test-output-only') + monitor = r.NativeMonitor(policy, budget) + before = monitor.report() + assert monitor.baseline == {str(root / 'native'): 3, str(tmp_path): 5} + assert before['latest_scans'][str(root)]['logical_bytes'] == 6 + assert before['baseline_scans'] == before['latest_scans'] + assert before['permission_gap_history'][0]['new'][0]['path'] == str(denied) + assert not before['coverage_complete'] and not before['native_hard_cap'] + assert before['runtime_uid'] == os.geteuid() + (root / 'report').write_bytes(b'longer output does not add native growth') + (root / 'native/cache/data').write_bytes(b'1234567') + monitor.check() # interval not reached + assert monitor.latest == monitor.baseline + clock.now = 11 + monitor.check() + report = monitor.report() + assert report['observed_positive_growth_bytes'] == 4 + assert report['baseline_logical_bytes'] == before['baseline_logical_bytes'] + assert report['latest_logical_bytes'][str(root / 'native')] == 7 + assert report['permission_gap_history'][-1]['new'] == [] + assert len(before['permission_gap_history']) == 1 # immutable published snapshot + denied.rmdir() + clock.now = 12 + monitor.check() + assert monitor.report()['permission_gap_history'][-1]['no_longer_observed'][0]['path'] == str(denied) + assert monitor.report()['coverage_complete'] is False + + +def test_native_monitor_new_and_changed_gap_are_not_silent(tmp_path, monkeypatch): + root = tmp_path / 'campaign' + (root / 'native').mkdir(parents=True) + policy = local_policy(root, tmp_path) + monitor = r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test')) + denied, _ = permission_case(tmp_path, monkeypatch) + monitor.check(force=True) + assert monitor.report()['permission_gap_history'][-1]['new'][0]['path'] == str(denied) + # A replacement with the same name but different stable metadata is fatal. + real_stat = r.os.stat + + def changed(path, *args, **kwargs): + info = real_stat(path, *args, **kwargs) + if path == denied.name and kwargs.get('dir_fd') is not None: + info.st_uid += 1 + return info + + monkeypatch.setattr(r.os, 'stat', changed) + with pytest.raises(RuntimeError, match='gap_identity_changed'): + monitor.check(force=True) + assert monitor.report()['permission_gap_history'][-1]['changed'] + + +def test_native_monitor_owned_output_denial_fatal_with_partial_report(tmp_path, monkeypatch): + denied, _ = permission_case(tmp_path, monkeypatch) + policy = local_policy(tmp_path, tmp_path / 'shared') + with pytest.raises(r.NativeScanError) as caught: + r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test')) + report = caught.value.report + assert not report['latest_scan_completed'] + assert not report['coverage_complete'] + assert report['failed_scan']['failure']['path'] == str(denied) + assert report['failed_scan']['free_space']['free_bytes'] > 0 + assert report['baseline_logical_bytes'] == {} + + +@pytest.mark.parametrize('code', [errno.EACCES, errno.EPERM, errno.EIO]) +def test_native_size_scandir_denial_uses_directory_metadata(tmp_path, monkeypatch, code): + real_stat, real_fstat, real_scandir, real_access = os.stat, os.fstat, os.scandir, os.access + identity = tmp_path.stat().st_ino + + def metadata(info): + if info.st_ino != identity: + return info + values = {k: getattr(info, k) for k in dir(info) if k.startswith('st_')} + values['st_uid'] = os.geteuid() + 1 + return NS(**values) + + def scanned(fd): + if real_fstat(fd).st_ino == identity: + raise OSError(code, 'injected scandir failure') + return real_scandir(fd) + + monkeypatch.setattr(r.os, 'stat', lambda *a, **kw: metadata(real_stat(*a, **kw))) + monkeypatch.setattr(r.os, 'fstat', lambda fd: metadata(real_fstat(fd))) + monkeypatch.setattr(r.os, 'scandir', scanned) + monkeypatch.setattr(r.os, 'access', lambda p, *a, **kw: + False if p == tmp_path.name else real_access(p, *a, **kw)) + if code == errno.EIO: + with pytest.raises(r.NativeScanError) as caught: + r.native_size(tmp_path, shared=True) + assert caught.value.report['failure']['errno'] == code + else: + report = r.native_size(tmp_path, shared=True) + assert report['permission_gaps'][0]['path'] == str(tmp_path) + assert report['permission_gaps'][0]['errno'] == code + assert report['logical_bytes'] == 0 + + +@pytest.mark.parametrize('shared', [False, True]) +def test_native_access_false_after_atomic_rename_keeps_bytes_not_permission_gap(tmp_path, monkeypatch, shared): + pending = tmp_path / 'counts.pending' + pending.write_bytes(b'x' * 73) + real_access = os.access + def access(name, *args, **kwargs): + if name == 'counts.pending': + pending.rename(tmp_path / 'counts.json') + return False # actual access(ENOENT) after successful earlier lstat + return real_access(name, *args, **kwargs) + monkeypatch.setattr(r.os, 'access', access) + report = r.native_size(tmp_path, shared=shared, strict_paths=[tmp_path]) + assert report['logical_bytes'] >= 73 # never silently discard observed bytes + assert report['disappeared_paths'] == [str(pending)] + assert report['permission_gaps'] == [] and report['skipped_count'] == 0 + assert 'failure' not in report + + +def test_native_size_unreadable_owned_file_fails(tmp_path, monkeypatch): + (tmp_path / 'owned').write_text('data') + real_access = os.access + monkeypatch.setattr(r.os, 'access', lambda p, *a, **kw: False if p == 'owned' else real_access(p, *a, **kw)) + with pytest.raises(r.NativeScanError) as caught: + r.native_size(tmp_path, shared=True) + assert caught.value.report['failure']['operation'] == 'file_access' + assert caught.value.report['failure']['path'] == str(tmp_path / 'owned') + + +def test_native_size_free_space_error_fails_with_exact_observed_path(tmp_path, monkeypatch): + monkeypatch.setattr(r.os, 'fstatvfs', lambda _: (_ for _ in ()).throw(OSError(errno.EIO, 'injected'))) + with pytest.raises(r.NativeScanError) as caught: + r.native_size(tmp_path, shared=True) + assert caught.value.report['failure']['operation'] == 'free_space' + assert caught.value.report['failure']['path'] == '/' + assert caught.value.report['failure']['errno'] == errno.EIO + + +def test_native_size_symlink_replacement_at_open_never_followed(tmp_path, monkeypatch): + child = tmp_path / 'child' + child.mkdir() + target = tmp_path / 'target' + target.mkdir() + real_open = os.open + + def replace(path, flags, *args, **kwargs): + if path == child.name and kwargs.get('dir_fd') is not None: + child.rmdir() + child.symlink_to(target, target_is_directory=True) + return real_open(path, flags, *args, **kwargs) + + monkeypatch.setattr(r.os, 'open', replace) + with pytest.raises(r.NativeScanError) as caught: + r.native_size(child, shared=True) + assert caught.value.report['failure']['errno'] in (errno.ELOOP, errno.ENOTDIR) + assert caught.value.report['skipped_count'] == 0 + + +def test_native_monitor_previously_unrelated_gap_becoming_owned_is_fatal(tmp_path, monkeypatch): + root = tmp_path / 'campaign' + (root / 'native').mkdir(parents=True) + denied, _ = permission_case(tmp_path, monkeypatch) + monitor = r.NativeMonitor(local_policy(root, tmp_path), NS(boottime=lambda: 0, STORAGE_SCOPE='test')) + real_stat = r.os.stat + + def newly_owned(path, *args, **kwargs): + info = real_stat(path, *args, **kwargs) + if path == denied.name and kwargs.get('dir_fd') is not None: + info.st_uid = os.geteuid() + return info + + monkeypatch.setattr(r.os, 'stat', newly_owned) + with pytest.raises(r.NativeScanError) as caught: + monitor.check(force=True) + report = caught.value.report + assert report['failed_scan']['failure']['metadata']['uid'] == os.geteuid() + assert report['failed_scan']['skipped_count'] == 0 + assert report['observed_positive_growth_bytes'] is None + assert report['baseline_scans'][str(tmp_path)]['skipped_count'] == 1 + + +def test_native_monitor_baseline_threshold_failure_retains_observations(tmp_path): + root = tmp_path / 'campaign' + (root / 'native').mkdir(parents=True) + policy = local_policy(root, tmp_path) + policy['min_free_bytes'] = 1 << 100 + with pytest.raises(RuntimeError, match='free_space_stop') as caught: + r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test')) + assert not caught.value.report['latest_scan_completed'] + assert caught.value.report['latest_free_space'][str(root)]['free_bytes'] > 0 + assert caught.value.report['baseline_logical_bytes'] == {} + + +@pytest.mark.parametrize('change', ['unavailable', 'ownership', 'disappeared']) +def test_native_size_post_traversal_metadata_never_becomes_permission_gap(tmp_path, monkeypatch, change): + real_stat = os.stat + calls = 0 + + def changed(path, *args, **kwargs): + nonlocal calls + info = real_stat(path, *args, **kwargs) + if path == tmp_path.name and kwargs.get('dir_fd') is not None: + calls += 1 + if calls > 1: + if change != 'ownership': + code = errno.EACCES if change == 'unavailable' else errno.ENOENT + raise OSError(code, 'injected post-traversal metadata failure') + values = {k: getattr(info, k) for k in dir(info) if k.startswith('st_')} + values['st_uid'] += 1 + return NS(**values) + return info + + monkeypatch.setattr(r.os, 'stat', changed) + with pytest.raises(r.NativeScanError) as caught: + r.native_size(tmp_path, shared=True) + assert caught.value.report['skipped_count'] == 0 + assert caught.value.report['failure']['path'] == str(tmp_path) + expected = 'traversal_identity_changed' if change == 'ownership' else 'traversal_lstat' + assert caught.value.report['failure']['operation'] == expected + + +def test_native_size_directory_disappearing_at_open_fails(tmp_path, monkeypatch): + permission_case(tmp_path, monkeypatch, code=errno.ENOENT) + with pytest.raises(r.NativeScanError) as caught: + r.native_size(tmp_path, shared=True) + assert caught.value.report['failure']['errno'] == errno.ENOENT + assert caught.value.report['skipped_count'] == 0 + + +@pytest.mark.parametrize('kind', ['file', 'symlink']) +def test_native_size_task_directory_alias_or_nondirectory_is_fatal(tmp_path, kind): + entry = tmp_path / 'entry' + if kind == 'file': + entry.write_text('not a directory') + else: + entry.symlink_to(tmp_path / 'not-followed') + with pytest.raises(r.NativeScanError): + r.native_size(tmp_path, shared=True, strict_paths=[entry / 'task-owned-cache']) + + +def test_native_size_closes_all_descriptors_after_gap_and_failure(tmp_path, monkeypatch): + permission_case(tmp_path, monkeypatch) + real_open, real_close = r.os.open, r.os.close + opened, closed = [], [] + + def opening(*args, **kwargs): + fd = real_open(*args, **kwargs) + opened.append(fd) + return fd + + def closing(fd): + closed.append(fd) + return real_close(fd) + + monkeypatch.setattr(r.os, 'open', opening) + monkeypatch.setattr(r.os, 'close', closing) + r.native_size(tmp_path, shared=True) + with pytest.raises(r.NativeScanError): + r.native_size(tmp_path, shared=False) + assert sorted(opened) == sorted(closed) + + +def v4_fixture(tmp_path, monkeypatch): + home, sim, system_tmp, root = [tmp_path / name for name in ('home', 'sim', 'system-tmp', 'campaign')] + for path in (home, sim, system_tmp, root): + path.mkdir() + monkeypatch.setattr(Path, 'home', classmethod(lambda cls: home)) + monkeypatch.setattr(r, 'SIM_ROOT', sim) + policy = local_policy(root, home) + policy.update(native_paths=r.native_paths(root), shared_native_paths=r.shared_native_paths(), + task_owned_cache_paths=r.task_owned_cache_paths(root), coverage_gaps=list(r.NATIVE_GAPS), + free_space_paths=list(map(str, (root, home, sim, system_tmp))), + free_space_mount_identities={str(p): r.mount_identity(p) for p in (root, home, sim, system_tmp)}) + return policy, home, sim, system_tmp, root + + +def test_native_monitor_v4_never_traverses_chrome_or_system_tmp_but_measures_writers(tmp_path, monkeypatch): + policy, home, sim, system_tmp, root = v4_fixture(tmp_path, monkeypatch) + unrelated = [home / '.cache/google-chrome/Default/Cache', system_tmp / 'private-system-job'] + for path in unrelated: + path.mkdir(parents=True) + (path / 'not-task-evidence').write_bytes(b'x' * 1000) + for path in map(Path, policy['native_paths']): + path.mkdir(parents=True) + (path / 'writer').write_bytes(b'123') + (root / 'report').write_bytes(b'output only') + real_scandir = os.scandir + traversed = [] + + def scanned(fd): + path = Path(os.readlink(f'/proc/self/fd/{fd}')) + traversed.append(path) + assert path not in (home, home / '.cache', system_tmp) + assert not any(path.is_relative_to(p) for p in unrelated) + return real_scandir(fd) + + monkeypatch.setattr(r.os, 'scandir', scanned) + monitor = r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test')) + assert set(monitor.baseline) == set(policy['native_paths']) + assert set(monitor.baseline.values()) == {3} + assert monitor.latest_scans[str(root)]['logical_bytes'] == len(b'output only') + assert set(monitor.report()['latest_free_space']) == set(policy['free_space_paths']) + assert all(value['free_bytes'] > 0 for value in monitor.latest_free_space.values()) + assert all(Path(p) in traversed for p in policy['native_paths']) + (sim / 'kit/data/writer').write_bytes(b'1234567') + for path in unrelated: + (path / 'not-task-evidence').write_bytes(b'x' * 2000) + monitor.check(force=True) + assert monitor.growth == 4 # no output/free-only/unknown-writer byte attribution + assert not monitor.report()['coverage_complete'] + assert any('unlisted/global cache writers' in gap for gap in policy['coverage_gaps']) + assert any('runtime confirmation' in gap for gap in policy['coverage_gaps']) + + +def test_native_monitor_v4_shared_tmp_free_floor_stops_without_traversal(tmp_path, monkeypatch): + policy, _, _, system_tmp, _ = v4_fixture(tmp_path, monkeypatch) + real_usage = os.fstatvfs + + def usage(fd): + if os.fstat(fd).st_ino == system_tmp.stat().st_ino: + return NS(f_bavail=0, f_frsize=4096) + return real_usage(fd) + + monkeypatch.setattr(r.os, 'fstatvfs', usage) + monkeypatch.setattr(r.os, 'scandir', lambda *_: pytest.fail('free floor must stop before recursive work')) + with pytest.raises(RuntimeError, match='free_space_stop') as caught: + r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test')) + report = caught.value.report + assert report['latest_free_space'][str(system_tmp)]['free_bytes'] == 0 + assert report['baseline_logical_bytes'] == {} and report['observed_positive_growth_bytes'] is None + + +def test_native_monitor_v4_missing_candidates_are_absent_not_created(tmp_path, monkeypatch): + policy, _, sim, _, _ = v4_fixture(tmp_path, monkeypatch) + monitor = r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test')) + for name in ('data', 'kit/data', 'kit/logs', 'kit/cache'): + path = sim / name + scan = monitor.latest_scans[str(path)] + assert scan['disappeared_paths'] and scan['logical_bytes'] == 0 + assert scan['free_space']['observed_path'] == str(sim) + assert not path.exists() + + +@pytest.mark.parametrize('change', ['alias', 'race', 'mount']) +def test_native_monitor_v4_free_only_path_identity_failures(tmp_path, monkeypatch, change): + policy, _, _, system_tmp, _ = v4_fixture(tmp_path, monkeypatch) + replacement = tmp_path / 'replacement' + replacement.mkdir() + if change == 'alias': + system_tmp.rmdir() + system_tmp.symlink_to(replacement, target_is_directory=True) + elif change == 'race': + real_open = os.open + + def raced(path, flags, *args, **kwargs): + if path == system_tmp.name and kwargs.get('dir_fd') is not None: + return real_open(replacement, flags) + return real_open(path, flags, *args, **kwargs) + monkeypatch.setattr(r.os, 'open', raced) + else: + policy['free_space_mount_identities'][str(system_tmp)] = 'wrong mount' + with pytest.raises(RuntimeError, match='symlink|open_identity_changed|free_space_mount_identity_changed'): + r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test')) + + +def test_native_monitor_v4_exact_scope_and_actual_environment_redirects(tmp_path): + assert r.shared_native_paths() == sorted([ + str(Path.home() / '.cache/ov' / name) for name in ('shaders', 'texturecache', 'Kit/107.3') + ] + [str(r.SIM_ROOT / 'kit/cache')]) + assert '/tmp' in r.free_space_paths(tmp_path) and '/tmp' not in r.native_paths(tmp_path) + assert str(Path.home() / '.cache') not in r.native_paths(tmp_path) + actual = list(r.native_redirects(tmp_path / 'native/preparation-support', preparation=True).values()) + for index in range(3): + actual.extend(r.native_redirects(tmp_path / f'native/launch-{index}').values()) + assert sorted(actual) == r.task_owned_cache_paths(tmp_path) + assert all(Path(p).is_relative_to(tmp_path / 'native') for p in actual) + + +@pytest.mark.parametrize('relative,shared', [('data', False), ('kit/data', False), ('kit/logs', False), + ('kit/cache', True)]) +def test_native_monitor_v4_installed_candidate_permissions_stay_classified(tmp_path, monkeypatch, relative, shared): + policy, _, sim, _, _ = v4_fixture(tmp_path, monkeypatch) + candidate = sim / relative + candidate.mkdir(parents=True) + denied, _ = permission_case(candidate, monkeypatch) + if shared: + monitor = r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test')) + assert monitor.latest_scans[str(candidate)]['permission_gaps'][0]['path'] == str(denied) + assert monitor.latest[str(candidate)] == 5 + else: + with pytest.raises(r.NativeScanError) as caught: + r.NativeMonitor(policy, NS(boottime=lambda: 0, STORAGE_SCOPE='test')) + assert caught.value.report['failed_scan']['failure']['path'] == str(denied) + assert caught.value.report['failed_scan']['skipped_count'] == 0 diff --git a/tests/test_contact_cycle_sim_p2_extended_profile.py b/tests/test_contact_cycle_sim_p2_extended_profile.py new file mode 100644 index 0000000..08fc39c --- /dev/null +++ b/tests/test_contact_cycle_sim_p2_extended_profile.py @@ -0,0 +1,437 @@ +"""CPU-only fixed-profile/adoption contracts; never launches Kit or creates real grants.""" +import gzip +import importlib.util +import json +from pathlib import Path +from types import SimpleNamespace as NS + +import pytest + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location('p2_extended_runner', ROOT / 'scripts/evaluate_cycle_sim_residual.py') +r = importlib.util.module_from_spec(spec) +spec.loader.exec_module(r) +b = r.cpu_module('evaluation_budget') +a = r.cpu_module('evaluation_adoption') +p = r.cpu_module('evaluation_protocol') + + +def put(path, value): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(b.encode(value)) + + +@pytest.fixture(autouse=True) +def no_launch(monkeypatch): + monkeypatch.setattr(r.subprocess, 'Popen', lambda *_a, **_k: pytest.fail('CPU only')) + monkeypatch.setattr(r.os, 'pidfd_open', lambda *_: pytest.fail('CPU only'), raising=False) + monkeypatch.setattr(r.signal, 'pidfd_send_signal', lambda *_: pytest.fail('CPU only'), raising=False) + + +def test_only_two_wall_values_and_version_metadata_differ(): + old = json.loads((ROOT / r.PROTOCOLS[1]).read_bytes()) + new = json.loads((ROOT / r.PROTOCOLS[2]).read_bytes()) + assert new.pop('resource_profile') == b.EXTENDED_PROFILE + assert new.pop('format') == 'skillfactory.contact-cycle-evaluation-protocol.v2' + new['format'] = old['format'] + assert new['resources'].pop('P2_launch_wall_cap_s') == 10800 + new['resources']['P2_launch_wall_cap_s'] = old['resources']['P2_launch_wall_cap_s'] + assert new['resources'].pop('aggregate_same_boot_window_s') == 14400 + new['resources']['aggregate_same_boot_window_s'] = old['resources']['aggregate_same_boot_window_s'] + assert new == old + assert b.digest(ROOT / r.PROTOCOLS[0]) == '8ad69f1c3a0e1a85b20050088574a50c4d90dc88ec85e14e66826bae1e7823c3' + protocol = p.load_protocol(ROOT / r.PROTOCOLS[2]) + assert [t.index for t in protocol.trials] == list(range(20)) + assert protocol.trials == p.load_protocol(ROOT / r.PROTOCOLS[1]).trials + assert r.protocol_stage(r.PROTOCOLS[2]) == 'P2' + for change in ('wall', 'profile', 'stage', 'gate'): + document = json.loads((ROOT / r.PROTOCOLS[2]).read_bytes()) + if change == 'wall': + document['resources']['P2_launch_wall_cap_s'] += 1 + elif change == 'profile': + document['resource_profile'] += '-arbitrary' + elif change == 'stage': + document['stage'] = 'P1' + else: + document['thresholds']['functional']['root_tilt_rad_max'] += .01 + with pytest.raises(ValueError): + p.Protocol(document) + + +@pytest.fixture +def source(tmp_path, monkeypatch): + source, target = tmp_path / 'p1-03', tmp_path / 'p2-01' + source.mkdir(); target.mkdir() + monkeypatch.setattr(a, 'SOURCE_ROOT', source) + monkeypatch.setattr(a, 'TARGET_ROOT', target) + monkeypatch.setattr(b, 'EXTENDED_ROOT', target) + monkeypatch.setattr(b, 'boot_id', lambda: 'boot') + monkeypatch.setattr(b, 'boottime', lambda: 100.) + frozen = tmp_path / 'frozen/candidate' + put(frozen, {'unchanged': True}) + sha = b.digest(frozen) + monkeypatch.setattr(a, 'CANDIDATE_SHA256', sha) + protocol = p.load_protocol(ROOT / r.PROTOCOLS[0]) + trials = [r.asdict(t) for t in protocol.trials] + rows = [] + for i, tick in enumerate((1829, 1770)): + launch = source / f'launch-{i}' + metrics = {'trial': trials[i], 'accepted_skill': False, 'functional_passed': False, + 'protocol_complete': False, 'completed_clock_cycles': 0, 'passed_prefix_cycles': 0, + 'observed_physics_ticks': tick, 'all_causes': ['P1_root_horizontal'], + 'first_failure': {'global_tick': tick, 'time_s': tick * .002, 'cycle_index': 0, + 'local_tick': tick, 'kind': 'tick', 'causes': ['P1_root_horizontal'], 'phase': 1}} + manifest = {'protocol': {'path': str(ROOT / r.PROTOCOLS[0]), 'sha256': b.digest(ROOT / r.PROTOCOLS[0]), + 'trials': trials}, 'trial_seeds': {'0': 93501, '1': 93501}, + 'frozen_policy': {'candidate_path': str(frozen), 'candidate_sha256': sha}, + 'effective_config': {'seed': 'protocol.trial.seed', 'controller': 'original'}, + 'sources': {'evaluation': [{'path': 'historical-source', 'sha256': 'a' * 64}]}} + put(launch / 'manifest.json', manifest) + put(launch / 'inventory.json', {'original': True}) + put(launch / 'launch-identity.json', {'original': True}) + grant = {'stage': 'P1', 'trial_indices': [i], 'launch_index': i, 'campaign_id': a.SOURCE_CAMPAIGN, + 'artifact_root': str(source), 'boot_id': 'boot', 'protocol_sha256': b.digest(ROOT / r.PROTOCOLS[0]), + **{key: b.digest(launch / name) for key, name in ( + ('manifest_sha256', 'manifest.json'), ('inventory_sha256', 'inventory.json'), + ('launch_identity_sha256', 'launch-identity.json'))}} + put(launch / 'grant.json', grant) + counts = dict(attempted_steps=tick, completed_steps=tick, initialization_steps=0, rollout_steps=tick) + row = dict(index=i, status='closed', cleanup_clean=True, continuation_eligible=True, + reserved_physical_calls=14500, rollout_cap=13500, initialization_cap=1000, + grant=grant, observed_counts=counts, ended_boottime=20 + i) + rows.append(row) + put(launch / 'reservation.json', {k: v for k, v in row.items() if k not in ('cleanup_clean', 'ended_boottime')} + | {'status': 'reserved', 'continuation_eligible': False, 'observed_counts': None}) + result_rows = [{'status': 'not_run'}, {'status': 'not_run'}] + result_rows[i] = metrics + put(launch / 'result.json', {'accepted_skill': False, 'optimizer_steps': 0, 'selected_rows': [i], + 'rows': result_rows, 'failures': [], 'preclose_identity_verified': True, + 'continuation_eligible': True, 'observed_counts': counts}) + put(launch / f'trial-{i:02d}/metrics.json', metrics) + put(launch / 'supervisor.json', dict(observed_counts=counts, cleanup_clean=True, continuation_eligible=True, + ownership_unknown=False, returncode=0, errors=[], stdout_discarded_bytes=0, stdout_eof_observed=True)) + put(launch / 'preclose-filesystem.json', {'files': {str(frozen): sha}, 'frozen_root': str(frozen.parent)}) + put(launch / 'postexit-filesystem.json', {'status': 'PASS', 'files_rehashed': 1}) + with gzip.open(launch / f'trial-{i:02d}/trace.v1.jsonl.gz', 'wb') as stream: + stream.write(b.encode({'kind': 'initial', 'event': {'trial_index': i, 'clock': {'global_tick': 0}}})) + for step in range(1, tick + 1): + stream.write(b.encode({'kind': 'tick', 'event': {'trial_index': i, 'clock': {'global_tick': step}}})) + stream.write(b.encode({'kind': 'terminal', 'runtime_stop': {'trial_index': i, 'reason': 'failure', + 'fatal': False, 'physics_ticks': tick, 'clock': {'global_tick': tick}, 'causes': ['P1_root_horizontal']}, + 'recording_faults': [], 'external_failure': None})) + ledger = {'format': b.FORMAT, 'campaign_id': a.SOURCE_CAMPAIGN, 'boot_id': 'boot', + 'first_run_boottime': 10, 'launches': rows} + put(source / 'budget.json', ledger) + put(source / 'row1-preflight.json', {'cumulative_prior_consumed_physical_calls': 58000, + 'budget_before': {**ledger, 'launches': rows[:1]}}) + for name in ('FINAL_P1_ROW0.md', 'FINAL_P1_ROW1.md', 'PAIRED_P1_SUMMARY.md'): + (source / name).write_text('Both original P1_root_horizontal failures; not a pass.\n') + return NS(root=source, target=target, ledger=ledger) + + +def adopt(source): + return a.create_adoption(parent_review='CPU fixture explicit evidence review, not physical pass', + trusted_source_budget_sha256=b.digest(source.root / 'budget.json')) + + +def test_adoption_is_separate_read_only_no_debit_no_clock(source): + before = b.tree_digest(source.root) + digest = adopt(source) + doc = a.verify_adoption(source.target, digest) + assert b.tree_digest(source.root) == before + assert doc['historical_reserved_calls_before_reference'] == 58000 + assert doc['historical_reserved_calls'] == 72500 and doc['local_reserved_calls_at_adoption'] == 0 + assert doc['cumulative_reserved_calls_if_launched'] == 163500 + assert [row['first_failure']['global_tick'] for row in doc['rows']] == [1829, 1770] + assert all(not row['functional_passed'] and not row['accepted_skill'] for row in doc['rows']) + assert not (source.target / 'budget.json').exists() + assert not list(source.target.glob('launch-*')) + with pytest.raises(FileExistsError): + adopt(source) + with pytest.raises(ValueError, match='parent_review'): + a.create_adoption(parent_review='', trusted_source_budget_sha256=b.digest(source.root / 'budget.json')) + with pytest.raises(ValueError, match='digest'): + a.verify_adoption(source.target, '0' * 64) + + +@pytest.mark.parametrize('mutation', ['failure', 'seed', 'partial', 'cleanup', 'accounting', 'trace', 'missing']) +def test_adoption_rejects_partial_forged_crossseed_unclean_and_mutation(source, mutation): + digest = adopt(source) + path = source.root / 'launch-1/trial-01/metrics.json' + value = json.loads(path.read_bytes()) + if mutation == 'failure': + value['functional_passed'] = True + elif mutation == 'seed': + value['trial']['seed'] += 1 + elif mutation == 'partial': + value['first_failure'] = None + elif mutation == 'cleanup': + path = source.root / 'launch-1/supervisor.json'; value = json.loads(path.read_bytes()) + value['cleanup_clean'] = False + elif mutation == 'accounting': + path = source.root / 'row1-preflight.json'; value = json.loads(path.read_bytes()) + value['cumulative_prior_consumed_physical_calls'] = 43500 + elif mutation == 'trace': + path = source.root / 'launch-1/trial-01/trace.v1.jsonl.gz' + path.write_bytes(path.read_bytes()[:-10]) + else: + path.unlink() + if mutation not in ('trace', 'missing'): + put(path, value) + with pytest.raises((ValueError, FileNotFoundError)): + a.verify_adoption(source.target, digest) + + +def test_one_future_reservation_separate_history_and_cross_profile_rejected(source): + sha = adopt(source) + grant = {'format': b.EXTENDED_GRANT_FORMAT, 'resource_profile': b.EXTENDED_PROFILE, 'stage': 'P2', + 'trial_indices': list(range(20)), 'launch_index': 0, 'not_after_boottime': 500, + 'p1_adoption_sha256': sha, 'boot_id': 'boot'} + kwargs = dict(clock=lambda: 100., boot=lambda: 'boot') + ledger = b.BudgetLedger(source.target, 'new-P2', resource_profile=b.EXTENDED_PROFILE, **kwargs) + assert ledger.data['first_run_boottime'] is None and ledger.data['launches'] == [] + assert ledger.data['cumulative_reserved_calls'] == 72500 + row = ledger.reserve(grant) + assert row['reserved_physical_calls'] == 91000 and row['rollout_cap'] == 90000 + assert row['initialization_cap'] == 1000 and row['deadline_boottime'] == 10900 + assert ledger.data['first_run_boottime'] == 100 and ledger.data['cumulative_reserved_calls'] == 163500 + ledger.finish(counts={'completed_steps': 0}, clean=True, eligible=True) + with pytest.raises(RuntimeError, match='retry'): + ledger.reserve({**grant, 'launch_index': 1}) + ledger.close() + with pytest.raises(RuntimeError, match='mismatch'): + b.BudgetLedger(source.target, 'new-P2', **kwargs) + legacy = source.target / 'legacy'; legacy.mkdir() + ledger = b.BudgetLedger(legacy, 'legacy', **kwargs) + with pytest.raises(RuntimeError, match='profile_mismatch'): + ledger.reserve(grant) + ledger.close() + (source.target / 'budget.json').write_bytes((legacy / 'budget.json').read_bytes()) + with pytest.raises(RuntimeError, match='mismatch'): + b.BudgetLedger(source.target, 'legacy', resource_profile=b.EXTENDED_PROFILE, **kwargs) + + +@pytest.mark.parametrize('mutate_prior', [False, True]) +def test_second_campaign_retains_failed_attempt_debit(source, monkeypatch, mutate_prior): + prior = source.target + documents = { + 'budget.json': {'boot_id': 'boot', 'launches': [{'status': 'closed'}], + 'historical_reserved_calls': 72500, 'local_reserved_calls': 91000, + 'cumulative_reserved_calls': 163500}, + 'launch-0/result.json': {'continuation_eligible': False, 'accepted_skill': False, + 'observed_counts': None, + 'rows': [{'status': 'not_run', 'trial_index': i} for i in range(20)], + 'failures': [{'message': 'retained bootstrap failure'}]}, + 'launch-0/supervisor.json': {'cleanup_clean': True, 'ownership_unknown': False, 'returncode': 1}, + 'launch-0/grant.json': {'consumed': True}, + 'launch-0/reservation.json': {'reserved_physical_calls': 91000}, + } + for name, document in documents.items(): + put(prior / name, document) + monkeypatch.setattr(a, 'PRIOR_P2_ROOT', prior) + pins = {name: b.digest(prior / name) for name in documents} + monkeypatch.setattr(a, 'PRIOR_P2_FILES', pins) + second = prior.parent / 'p2-02'; second.mkdir() + monkeypatch.setattr(a, 'SECOND_EXTENDED_ROOT', second) + monkeypatch.setattr(b, 'SECOND_EXTENDED_ROOT', second) + before = b.tree_digest(prior) + sha = a.create_adoption(parent_review='new CPU proposal, no run authority', + trusted_source_budget_sha256=b.digest(source.root / 'budget.json'), + target_root=second) + assert b.tree_digest(prior) == before + assert not (second / 'budget.json').exists() + doc = a.verify_adoption(second, sha) + assert doc['historical_reserved_calls'] == 163500 + assert doc['cumulative_reserved_calls_if_launched'] == 254500 + assert doc['prior_failed_P2']['observed_counts'] is None + grant = {'format': b.EXTENDED_GRANT_FORMAT, 'resource_profile': b.EXTENDED_PROFILE, + 'stage': 'P2', 'trial_indices': list(range(20)), 'launch_index': 0, + 'not_after_boottime': 500, 'p1_adoption_sha256': sha, 'boot_id': 'boot'} + ledger = b.BudgetLedger(second, 'second', resource_profile=b.EXTENDED_PROFILE, + clock=lambda: 100., boot=lambda: 'boot') + try: + if mutate_prior: + (prior / 'launch-0/result.json').write_text('{}') + with pytest.raises(ValueError, match='prior_P2_changed'): + ledger.reserve(grant) + assert ledger.data['launches'] == [] + assert ledger.data['local_reserved_calls'] == 0 + else: + ledger.reserve(grant) + assert ledger.data['cumulative_reserved_calls'] == 254500 + assert ledger.data['historical_reserved_calls'] == 163500 + ledger.finish(counts=None, clean=True, eligible=False) + with pytest.raises(RuntimeError, match='retry'): + ledger.reserve(grant) + assert b.tree_digest(prior) == before + finally: + ledger.close() + with pytest.raises(ValueError, match='fixed_root'): + b.extended_historical_calls(second.parent / 'p2-03') + + +def test_second_root_preparation_reaches_policy_without_launch(tmp_path, monkeypatch): + spec = importlib.util.spec_from_file_location('second_root_prep_test', ROOT / 'scripts/prepare_cycle_sim_binding.py') + prep = importlib.util.module_from_spec(spec) + spec.loader.exec_module(prep) + second = tmp_path / 'p2-02'; second.mkdir() + monkeypatch.setattr(prep.b, 'SECOND_EXTENDED_ROOT', second) + def boundary(*_): + raise RuntimeError('CPU policy boundary; no Kit launch') + monkeypatch.setattr(prep.r, 'storage_policy_check', boundary) + with pytest.raises(RuntimeError, match='CPU policy boundary'): + prep.supervise(second, tmp_path / 'unused-policy', 'unused', r.PROTOCOLS[2]) + assert list(second.iterdir()) == [] + with pytest.raises(ValueError, match='fixed_root'): + prep.supervise(tmp_path / 'p2-03', tmp_path / 'unused-policy', 'unused', r.PROTOCOLS[2]) + + +def policy_fixture(source, monkeypatch): + monkeypatch.setattr(r, 'read_trusted', lambda path, _: path.read_bytes() if path.name == 'policy.json' else b'') + monkeypatch.setattr(r, 'mount_identity', lambda _: 'mount') + root = source.target + policy = {'format': b.EXTENDED_POLICY_FORMAT, 'artifact_root': str(root), 'limit_bytes': b.CAP_BYTES, + 'scope': b.STORAGE_SCOPE, 'approval_sha256': r.STORAGE_APPROVAL_SHA256, + 'native_paths': r.native_paths(root), 'check_interval_seconds': 1, 'max_native_growth_bytes': b.CAP_BYTES, + 'min_free_bytes': 2 * b.CAP_BYTES, 'coverage_gaps': list(r.NATIVE_GAPS), 'native_hard_cap': False, + 'boot_id': 'boot', 'mount_identity': 'mount', 'not_after_boottime': 14500., 'issued_boottime': 100., + 'resource_profile': b.EXTENDED_PROFILE, 'shared_native_paths': r.shared_native_paths(), + 'task_owned_cache_paths': r.task_owned_cache_paths(root), 'free_space_paths': r.free_space_paths(root), + 'free_space_mount_identities': {p: 'mount' for p in r.free_space_paths(root)}} + args = NS(artifact_root=root, protocol=r.PROTOCOLS[2], storage_policy=root / 'policy.json', + trusted_storage_policy_sha256='0' * 64) + return args, policy + + +def test_extended_policy_expiry_boundary_legacy_unchanged(source, monkeypatch): + args, policy = policy_fixture(source, monkeypatch) + put(args.storage_policy, policy) + assert r.storage_policy_check(args, b) == policy + for change in ({'not_after_boottime': 14500.001}, {'issued_boottime': 101}, {'boot_id': 'other'}, + {'format': 'skillfactory.output-storage-policy.v4'}): + put(args.storage_policy, {**policy, **change}) + with pytest.raises(RuntimeError): + r.storage_policy_check(args, b) + old = {k: v for k, v in policy.items() if k not in ('issued_boottime', 'resource_profile')} + old.update(format='skillfactory.output-storage-policy.v4', not_after_boottime=3700) + args.protocol = r.PROTOCOLS[0] + put(args.storage_policy, old) + assert r.storage_policy_check(args, b) == old + old['not_after_boottime'] += .001 + put(args.storage_policy, old) + with pytest.raises(RuntimeError): + r.storage_policy_check(args, b) + + +def test_grant_admission_stays_short_and_binds_twenty_rows(source, monkeypatch): + adoption = adopt(source) + monkeypatch.setattr(r, 'launch_identity', lambda: {'CPU': True}) + args = NS(protocol=r.PROTOCOLS[2], artifact_root=source.target, manifest=source.target / 'binding.json', + inventory=source.target / 'inventory.json', trusted_storage_policy_sha256='e' * 64) + put(args.manifest, {'CPU': True}); put(args.inventory, {'CPU': True}) + args.trusted_manifest_sha256 = b.digest(args.manifest) + grant = dict(format=b.EXTENDED_GRANT_FORMAT, resource_profile=b.EXTENDED_PROFILE, campaign_id='P2', + launch_index=0, stage='P2', trial_indices=list(range(20)), manifest_sha256=args.trusted_manifest_sha256, + protocol_sha256=b.digest(ROOT / args.protocol), inventory_sha256=b.digest(args.inventory), + artifact_root=str(args.artifact_root), boot_id='boot', not_after_boottime=3700, parent_review='CPU review', + p1_adoption_sha256=adoption, storage_policy_sha256=args.trusted_storage_policy_sha256, + storage_scope=b.STORAGE_SCOPE, launch_identity_sha256=r.hashlib.sha256(b.encode(r.launch_identity())).hexdigest()) + assert r.grant_check(grant, args, b) == grant + for change in ({'not_after_boottime': 3700.001}, {'trial_indices': list(range(19))}, {'launch_index': 2}, + {'stage': 'P1'}, {'resource_profile': 'arbitrary'}, {'p1_adoption_sha256': '0' * 64}): + with pytest.raises(ValueError): + r.grant_check({**grant, **change}, args, b) + + +@pytest.mark.parametrize('mutate_after_exit', [False, True]) +def test_supervisor_rechecks_external_adoption_after_exit_and_always_cleans(source, monkeypatch, mutate_after_exit): + sha = adopt(source) + root = source.target + args = NS(protocol=r.PROTOCOLS[2], artifact_root=root, manifest=root / 'binding.json', + inventory=root / 'inventory.json', grant=root / 'grant.json', device='cpu') + put(args.manifest, {'CPU': True}); put(args.inventory, {'CPU': True}) + args.trusted_manifest_sha256 = b.digest(args.manifest) + grant = {'format': b.EXTENDED_GRANT_FORMAT, 'resource_profile': b.EXTENDED_PROFILE, 'stage': 'P2', + 'trial_indices': list(range(20)), 'launch_index': 0, 'not_after_boottime': 500, + 'p1_adoption_sha256': sha, 'boot_id': 'boot', 'campaign_id': 'CPU-P2'} + put(args.grant, grant); args.trusted_grant_sha256 = b.digest(args.grant) + monkeypatch.setattr(r, 'grant_check', lambda value, *_: value) # exact schema tested separately + monkeypatch.setattr(r, 'storage_policy_check', lambda *_: {'not_after_boottime': 14000, 'limit_bytes': b.CAP_BYTES}) + monkeypatch.setattr(r, 'launch_identity', lambda: {'CPU': True}) + monkeypatch.setattr(r, 'launch_command', lambda *_: ['CPU-no-process']) + monkeypatch.setattr(r, 'NativeMonitor', lambda *_: NS(check=lambda: None, report=lambda: {'CPU': True})) + monkeypatch.setattr(r, 'OwnedProcesses', lambda: NS( + supervisor=(1, 2), known={(3, 4)}, unknown=False, attach=lambda _: None, discover=lambda: {})) + monkeypatch.setattr(r.ctypes, 'CDLL', lambda *_a, **_k: NS(prctl=lambda *_: 0)) + monkeypatch.setattr(r.socket, 'socketpair', lambda: ( + NS(close=lambda: None), NS(close=lambda: None, set_inheritable=lambda _: None, fileno=lambda: 99))) + ledger_class, budget_class = b.BudgetLedger, b.LaunchBudget + monkeypatch.setattr(b, 'BudgetLedger', lambda *args, **kwargs: ledger_class( + *args, clock=lambda: 100., boot=lambda: 'boot', **kwargs)) + monkeypatch.setattr(b, 'LaunchBudget', lambda *args, **kwargs: budget_class( + *args, clock=lambda: 100., boot=lambda: 'boot', **kwargs)) + events = [] + + def spawn(*_a, **_k): + events.append('spawn') + put(root / 'launch-0/result.json', {'observed_counts': {'completed_steps': 3}, + 'continuation_eligible': True, 'preclose_identity_verified': True}) + put(root / 'launch-0/preclose-filesystem.json', {'CPU': True}) + return NS(pid=3, stdout=None, poll=lambda: 0, returncode=0, wait=lambda **_: None) + monkeypatch.setattr(r.subprocess, 'Popen', spawn) + monkeypatch.setattr(r, 'exchange_worker', lambda _child, _channel, payload, *_: + events.append(payload['reservation']['grant']['trial_indices'])) + monkeypatch.setattr(r, 'StdoutCapture', lambda *_: NS( + eof=True, discarded=0, error=None, close=lambda: None, check=lambda: None)) + monkeypatch.setattr(r, 'verify_filesystem_inputs', lambda _: {'status': 'PASS'}) + + def cleanup(*_): + events.append('cleanup') + if mutate_after_exit: + path = source.root / 'launch-0/stdout.log' + path.write_text('changed after exit') + return True + monkeypatch.setattr(r, 'cleanup_group', cleanup) + assert r.supervise(args) == (1 if mutate_after_exit else 0) + assert events == ['spawn', list(range(20)), 'cleanup'] + ledger = json.loads((root / 'budget.json').read_bytes()) + assert len(ledger['launches']) == 1 and ledger['cumulative_reserved_calls'] == 163500 + row = ledger['launches'][0] + assert row['status'] == 'closed' and row['cleanup_clean'] + assert row['continuation_eligible'] is (not mutate_after_exit) + adopted = json.loads((root / 'launch-0/adopted-p1-evidence.json').read_bytes()) + assert [row['first_failure']['global_tick'] for row in adopted['rows']] == [1829, 1770] + assert (root / 'launch-0/postexit-adoption.json').exists() is (not mutate_after_exit) + if mutate_after_exit: + assert json.loads((root / 'launch-0/supervisor.json').read_bytes())['errors'] + + +def test_extended_storage_policy_requires_slot_plus_cleanup_coverage(source, monkeypatch): + monkeypatch.setattr(r, 'storage_policy_check', lambda *_: {'not_after_boottime': 11019.999}) + monkeypatch.setattr(r, 'read_trusted', lambda *_: b.encode({})) + monkeypatch.setattr(r, 'grant_check', lambda *_: {'launch_index': 0, 'not_after_boottime': 200}) + args = NS(protocol=r.PROTOCOLS[2], grant=source.target / 'never', trusted_grant_sha256='0' * 64) + with pytest.raises(RuntimeError, match='storage_policy_expires'): + r.supervise(args) + assert not (source.target / 'budget.json').exists() + + +@pytest.mark.parametrize('mutation', ['missing_initial', 'missing_terminal', 'wrong_terminal_trial', 'tick_gap']) +def test_adoption_construction_rejects_partial_or_cross_trial_trace(source, mutation): + path = source.root / 'launch-1/trial-01/trace.v1.jsonl.gz' + with gzip.open(path, 'rt') as stream: + records = [json.loads(line) for line in stream] + if mutation == 'missing_initial': + records.pop(0) + elif mutation == 'missing_terminal': + records.pop() + elif mutation == 'wrong_terminal_trial': + records[-1]['runtime_stop']['trial_index'] = 0 + else: + records[10]['event']['clock']['global_tick'] += 1 + with gzip.open(path, 'wb') as stream: + for record in records: + stream.write(b.encode(record)) + with pytest.raises(ValueError, match='trace_'): + adopt(source) + assert not (source.target / 'p1-adoption.json').exists() diff --git a/tests/test_contact_cycle_sim_preparation_supervisor.py b/tests/test_contact_cycle_sim_preparation_supervisor.py new file mode 100644 index 0000000..c591a5d --- /dev/null +++ b/tests/test_contact_cycle_sim_preparation_supervisor.py @@ -0,0 +1,191 @@ +"""CPU preparation tests; only a harmless stdlib RLIMIT child, never Kit.""" +import importlib.util +import json +import threading +from pathlib import Path +from types import SimpleNamespace as NS + +import pytest + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location( + 'preparation_supervisor_test', ROOT / 'scripts/prepare_cycle_sim_binding.py') +prep = importlib.util.module_from_spec(spec) +spec.loader.exec_module(prep) + + +def test_native_scan_cannot_block_deadline_owner(monkeypatch): + entered, release = threading.Event(), threading.Event() + + def blocked(*_): + entered.set() + release.wait(5) + raise RuntimeError('injected scan failure') + + monkeypatch.setattr(prep.r, 'NativeMonitor', blocked) + monitor = prep.AsyncNativeMonitor({'check_interval_seconds': .1}) + try: + assert entered.wait(1) + assert monitor.check() is None # immediate, despite scan being blocked + monitor.state = (prep.b.boottime() - 3, {}, None) + with pytest.raises(RuntimeError, match='scan_stale'): + monitor.check() + finally: + monitor.stop.set() + release.set() + monitor.thread.join(1) + with pytest.raises(RuntimeError, match='injected scan failure'): + monitor.check() + + +def test_supervisor_refuses_prior_preparation_without_spawning(tmp_path, monkeypatch): + (tmp_path / 'prep-p1').mkdir() + monkeypatch.setattr(prep.os, 'pidfd_open', lambda *_: None, raising=False) + monkeypatch.setattr(prep.signal, 'pidfd_send_signal', lambda *_: None, raising=False) + monkeypatch.setattr(prep.subprocess, 'Popen', lambda *_a, **_k: pytest.fail('no spawn')) + with pytest.raises(RuntimeError, match='fresh_campaign_no_retry'): + prep.supervise(tmp_path, tmp_path / 'policy', '0' * 64) + + +def test_output_cap_retains_reserves(tmp_path, monkeypatch): + monkeypatch.setattr(prep.b, 'artifact_size', lambda *_a, **_kw: + prep.b.CAP_BYTES - prep.b.HEADROOM_BYTES - prep.b.STDOUT_ALLOWANCE) + with pytest.raises(RuntimeError, match='aggregate_output_cap'): + prep.write(tmp_path, 'report.json', {'extra': 1}) + assert not (tmp_path / 'report.json').exists() + + +def test_blocked_baseline_stops_at_prepare_deadline_without_spawn(tmp_path, monkeypatch): + clock = NS(now=0) + monkeypatch.setattr(prep.b, 'boottime', lambda: clock.now) + monkeypatch.setattr(prep.time, 'sleep', lambda _: setattr(clock, 'now', clock.now + 1)) + monkeypatch.setattr(prep.os, 'pidfd_open', lambda *_: None, raising=False) + monkeypatch.setattr(prep.signal, 'pidfd_send_signal', lambda *_: None, raising=False) + monkeypatch.setattr(prep.r, 'storage_policy_check', lambda *_: {'not_after_boottime': 1000}) + monkeypatch.setattr(prep.r, 'OwnedProcesses', lambda: NS( + supervisor=(1, 2), discover=lambda: {}, unknown=False, known=set())) + monkeypatch.setattr(prep.ctypes, 'CDLL', lambda *_a, **_kw: NS(prctl=lambda *_: 0)) + monkeypatch.setattr(prep.b, 'digest', lambda _: '0' * 64) + stopped = [] + monkeypatch.setattr(prep, 'AsyncNativeMonitor', lambda _: NS( + check=lambda: None, state=(None, None, None), stop=NS(set=lambda: stopped.append(True)), + thread=NS(is_alive=lambda: True))) + monkeypatch.setattr(prep.subprocess, 'Popen', lambda *_a, **_k: pytest.fail('no spawn before baseline')) + assert prep.supervise(tmp_path, tmp_path / 'policy', '0' * 64) == 1 + report = json.loads((tmp_path / 'preparation-supervisor.json').read_bytes()) + assert report['finished_boottime'] == 480 and report['deadline_boottime'] == 600 + assert report['cleanup_clean'] and not report['kit_only_launch_started'] + assert report['errors'] == ["RuntimeError('preparation_480s_deadline')"] + assert stopped == [True] + + +@pytest.mark.parametrize('baseline_failure', [False, True]) +def test_async_native_monitor_retains_structured_failure_report(monkeypatch, baseline_failure): + report = {'coverage_complete': False, 'latest_scan_completed': False, + 'failed_scan': {'logical_bytes': 5, 'skipped_count': 0, + 'failure': {'path': '/synthetic/owned', 'errno': 13, + 'metadata': {'uid': 123, 'mode': 0o40700}}}} + + def failed(*_a, **_kw): + raise prep.r.NativeScanError(report) + + if baseline_failure: + monkeypatch.setattr(prep.r, 'NativeMonitor', failed) + else: + monkeypatch.setattr(prep.r, 'NativeMonitor', lambda *_: NS(report=lambda: {}, check=failed)) + monitor = prep.AsyncNativeMonitor({'check_interval_seconds': .001}) + monitor.thread.join(1) + assert not monitor.thread.is_alive() + assert monitor.state[1] == report + assert (monitor.state[0] is None) == baseline_failure + with pytest.raises(RuntimeError, match='/synthetic/owned'): + monitor.check() + + +@pytest.mark.parametrize('protocol_index', [0, 2]) +def test_preparation_environment_uses_exact_monitored_redirects_without_launch(tmp_path, monkeypatch, protocol_index): + monkeypatch.setattr(prep.b, 'EXTENDED_ROOT', tmp_path) + protocol = prep.r.PROTOCOLS[protocol_index] + monkeypatch.setattr(prep.os, 'pidfd_open', lambda *_: None, raising=False) + monkeypatch.setattr(prep.signal, 'pidfd_send_signal', lambda *_: None, raising=False) + monkeypatch.setattr(prep.r, 'storage_policy_check', lambda *_: {'not_after_boottime': prep.b.boottime() + 1000}) + monkeypatch.setattr(prep.r, 'OwnedProcesses', lambda: NS( + supervisor=(1, 2), discover=lambda: {}, unknown=False, known=set())) + monkeypatch.setattr(prep.ctypes, 'CDLL', lambda *_a, **_kw: NS(prctl=lambda *_: 0)) + monkeypatch.setattr(prep.b, 'digest', lambda _: '0' * 64) + monkeypatch.setattr(prep, 'AsyncNativeMonitor', lambda _: NS( + check=lambda: {}, state=(0, {}, None), stop=NS(set=lambda: None), thread=NS(is_alive=lambda: False))) + observed = [] + + def command(arguments, environment): + assert arguments[arguments.index('--protocol') + 1] == protocol + assert arguments[arguments.index('--output') + 1] == str(tmp_path / prep.preparation_name(protocol)) + expected = prep.r.native_redirects(tmp_path / 'native/preparation-support', preparation=True) + assert {key: environment[key] for key in expected} == expected + assert set(expected.values()) <= set(prep.r.task_owned_cache_paths(tmp_path)) + assert all(Path(path).is_dir() for path in expected.values()) + observed.append(expected) + raise RuntimeError('CPU stop before launcher') + + monkeypatch.setattr(prep.r, 'launch_command', command) + monkeypatch.setattr(prep.subprocess, 'Popen', lambda *_a, **_k: pytest.fail('no Kit launch')) + assert prep.supervise(tmp_path, tmp_path / 'policy', '0' * 64, protocol) == 1 + assert len(observed) == 1 + report = json.loads((tmp_path / 'preparation-supervisor.json').read_text()) + assert not report['kit_only_launch_started'] + assert report['errors'] == ["RuntimeError('CPU stop before launcher')"] + + +def test_without_core_dumps_is_child_only_exec_setup(): + # A harmless stdlib child, not Kit; parent's limits must remain unchanged. + import resource + import subprocess + import sys + + before = resource.getrlimit(resource.RLIMIT_CORE) + command = prep.without_core_dumps([ + sys.executable, '-c', + 'import resource; print(resource.getrlimit(resource.RLIMIT_CORE))']) + result = subprocess.run(command, capture_output=True, text=True, timeout=5, check=True) + assert result.stdout.strip() == '(0, 0)' + assert resource.getrlimit(resource.RLIMIT_CORE) == before + + +@pytest.mark.parametrize('returncode,clean', [(0, True), (139, True), (0, False)]) +def test_supervisor_postexit_and_finally_cleanup_own_final_pass(tmp_path, monkeypatch, returncode, clean): + monkeypatch.setattr(prep.os, 'pidfd_open', lambda *_: None, raising=False) + monkeypatch.setattr(prep.signal, 'pidfd_send_signal', lambda *_: None, raising=False) + monkeypatch.setattr(prep.r, 'storage_policy_check', lambda *_: {'not_after_boottime': prep.b.boottime() + 1000}) + owned = NS(supervisor=(1, 2), discover=lambda: {}, unknown=False, known={(22, 33)}, attach=lambda _: None) + monkeypatch.setattr(prep.r, 'OwnedProcesses', lambda: owned) + monkeypatch.setattr(prep.ctypes, 'CDLL', lambda *_a, **_kw: NS(prctl=lambda *_: 0)) + monkeypatch.setattr(prep.b, 'digest', lambda _: '0' * 64) + monkeypatch.setattr(prep, 'AsyncNativeMonitor', lambda _: NS( + check=lambda: {}, state=(0, {}, None), stop=NS(set=lambda: None), thread=NS(is_alive=lambda: False))) + monkeypatch.setattr(prep.r, 'launch_command', lambda *_: ['CPU-fake-child']) + monkeypatch.setattr(prep.r, 'launch_identity', lambda: {'scope': 'CPU fixture'}) + monkeypatch.setattr(prep.subprocess, 'Popen', lambda *_a, **_k: NS( + returncode=returncode, stdout=NS(close=lambda: None), poll=lambda: returncode)) + monkeypatch.setattr(prep.r, 'StdoutCapture', lambda *_: NS( + eof=True, written=0, discarded=0, error=None, close=lambda: None)) + events = [] + monkeypatch.setattr(prep.r, 'cleanup_group', lambda *_: events.append('cleanup') or clean) + monkeypatch.setattr(prep, 'postexit_identity', lambda *_: events.append('postexit-files') or {'status': 'PASS'}) + assert prep.supervise(tmp_path, tmp_path / 'policy', '0' * 64) == (0 if returncode == 0 and clean else 1) + report = json.loads((tmp_path / 'preparation-supervisor.json').read_bytes()) + assert events == ['cleanup', 'postexit-files'] + assert report['returncode'] == returncode and report['cleanup_clean'] == clean + assert report['owned_remaining'] == {} and not report['ownership_unknown'] + if returncode: + assert 'preparation_child_exit_139' in report['errors'][0] + + +def test_postexit_rehash_cannot_block_total_deadline(tmp_path, monkeypatch): + release = threading.Event() + monkeypatch.setattr(prep, 'postexit_identity', lambda *_: release.wait(3)) + errors = [] + try: + assert prep.collect_postexit_identity(tmp_path, {}, NS(), errors, prep.b.boottime() - 1) is None + assert errors == ['preparation_postexit_identity_deadline'] + finally: + release.set() diff --git a/tests/test_contact_cycle_sim_replay.py b/tests/test_contact_cycle_sim_replay.py new file mode 100644 index 0000000..7644bda --- /dev/null +++ b/tests/test_contact_cycle_sim_replay.py @@ -0,0 +1,438 @@ +"""CPU-only frozen replay interfaces and failure artifacts; no Kit or optimizer.""" +import copy +import importlib.util +import json +import sys +from pathlib import Path +from types import SimpleNamespace + +import pytest +import torch +from SkillFactory.legs.contact_cycle_sim import replay +from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "scripts")) +import replay_cycle_sim_residual as cli + + +@pytest.fixture(autouse=True) +def cpu_only(monkeypatch): + old = torch.get_num_threads() + torch.set_num_threads(1) + + def forbidden(*args, **kwargs): + raise AssertionError("Optimizer construction is forbidden in frozen replay") + + monkeypatch.setattr(torch.optim.Adam, "__init__", forbidden) + yield + torch.set_num_threads(old) + + +def write_json(path, data): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(data)) + + +@pytest.fixture +def bundle(tmp_path, monkeypatch): + """Synthetic trusted root; production CLI never exposes hash override flags.""" + root, prepared = tmp_path / "repo", tmp_path / "frozen" + root.mkdir() + prepared.mkdir() + config_path = "configs/contact_cycle/simulation_residual_v1.json" + files = {"AGENT.md": b"instructions", "source/runtime.py": b"runtime", + config_path: (ROOT / config_path).read_bytes()} + for relative, payload in files.items(): + for base in (root, prepared / "source_snapshot"): + path = base / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(payload) + for relative in replay.REPLAY_SOURCES: + path = root / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("synthetic replay source") + manifest = {"contract": replay.CONTRACT, "task_id": replay.TASK_ID, + "features": replay.FEATURE_VERSION, "model_config": replay.MODEL_CONFIG, + "training": {"task_config": config_path}, + "sources": {p: replay.sha256(v) for p, v in files.items()}} + write_json(prepared / "manifest.json", manifest) + monkeypatch.setattr(replay, "MANIFEST_SHA256", replay.sha256(replay.canonical(manifest).encode())) + policy = SimResidualActorCritic(**replay.MODEL_CONFIG) + with torch.no_grad(): + policy.actor[-1].bias.copy_(torch.tensor([.1, -.2, .3])) + data = {"format": replay.CHECKPOINT_FORMAT, "contract": replay.CONTRACT, + "features": replay.FEATURE_VERSION, "model_config": replay.MODEL_CONFIG, + "update": 256, "accepted_skill": False, "model": policy.state_dict(), + "identity": {"manifest": manifest, "model_config": replay.MODEL_CONFIG, "seed": 91501}, + "optimizer": {"ignored": "must never instantiate or restore"}, + "rng": {"ignored": "must never restore"}} + checkpoint = prepared / "candidate_u0256.pt" + torch.save(data, checkpoint) + monkeypatch.setattr(replay, "CANDIDATE_SHA256", replay.sha256(checkpoint.read_bytes())) + monkeypatch.setattr(cli, "ROOT", root) + return SimpleNamespace(root=root, prepared=prepared, checkpoint=checkpoint, manifest=manifest, data=data) + + +def test_frozen_loading_is_inference_only_and_deterministic(bundle): + before = bundle.checkpoint.read_bytes() + manifest, binding = replay.verify_bundle(bundle.root, bundle.checkpoint) + rng = torch.get_rng_state().clone() + policy = replay.load_frozen_policy(bundle.checkpoint, manifest) + assert torch.equal(rng, torch.get_rng_state()) + assert not policy.training and not any(p.requires_grad for p in policy.parameters()) + x = torch.zeros(2, 46) + action, mean, std = replay.inference_action(x, "learned", policy) + assert torch.equal(action, policy.act(x, deterministic=True)[0]) + assert torch.equal(action, mean.tanh()) and action.any() and std.shape == (3,) + assert not action.requires_grad + assert torch.equal(action, replay.inference_action(x, "learned", policy)[0]) + assert bundle.checkpoint.read_bytes() == before + assert binding["snapshot_files_verified"] == 3 and not binding["live_source_exceptions"] + assert set(binding["replay_sources"]) == set(replay.REPLAY_SOURCES) + + +def test_agent_live_drift_is_the_only_explicit_exception(bundle): + (bundle.root / "AGENT.md").write_text("changed live instructions") + _, binding = replay.verify_bundle(bundle.root, bundle.checkpoint) + exception, = binding["live_source_exceptions"] + assert exception["path"] == "AGENT.md" and exception["reason"] + assert exception["snapshot_sha256"] == bundle.manifest["sources"]["AGENT.md"] + assert exception["live_sha256"] == replay.sha256(b"changed live instructions") + + +@pytest.mark.parametrize("relative", ["AGENT.md", "source/runtime.py", + "configs/contact_cycle/simulation_residual_v1.json"]) +def test_any_snapshot_tampering_fails_including_agent(bundle, relative): + (bundle.prepared / "source_snapshot" / relative).write_text("tampered") + with pytest.raises(ValueError, match="snapshot mismatch"): + replay.verify_bundle(bundle.root, bundle.checkpoint) + + +@pytest.mark.parametrize("relative", ["source/runtime.py", "configs/contact_cycle/simulation_residual_v1.json"]) +def test_live_runtime_and_config_drift_fail_closed(bundle, relative): + (bundle.root / relative).write_text("changed") + with pytest.raises(ValueError, match="Live bound source/config mismatch"): + replay.verify_bundle(bundle.root, bundle.checkpoint) + + +def test_manifest_tampering_rejected(bundle): + write_json(bundle.prepared / "manifest.json", {**bundle.manifest, "features": "policy32"}) + with pytest.raises(ValueError, match="manifest SHA256"): + replay.verify_bundle(bundle.root, bundle.checkpoint) + + +def test_candidate_sha_checked_before_deserialization(bundle, monkeypatch): + bundle.checkpoint.write_bytes(b"not the frozen candidate") + monkeypatch.setattr(torch, "load", lambda *a, **k: pytest.fail("Must verify SHA before deserialization")) + with pytest.raises(ValueError, match="candidate SHA256"): + replay.load_frozen_policy(bundle.checkpoint, bundle.manifest) + with pytest.raises(ValueError, match="candidate SHA256"): + replay.verify_bundle(bundle.root, bundle.checkpoint) + + +@pytest.mark.parametrize("fault", ["legacy", "features32", "update0", "update_bool", "model_config", + "manifest", "seed", "shape32", "dtype", "nan", "normalization", "keys"]) +def test_checkpoint_contract_rejects_incompatible_payload(bundle, monkeypatch, fault): + data = copy.deepcopy(bundle.data) + if fault == "legacy": + data["format"] = "old32" + elif fault == "features32": + data["features"] = "policy32" + elif fault == "update0": + data["update"] = 0 + elif fault == "update_bool": + data["update"] = True + elif fault == "model_config": + data["model_config"]["hidden"] = 32 + elif fault == "manifest": + data["identity"]["manifest"] = {} + elif fault == "seed": + data["identity"]["seed"] = 1 + elif fault == "shape32": + data["model"]["actor.0.weight"] = torch.zeros(128, 32) + elif fault == "dtype": + data["model"]["actor.0.weight"] = data["model"]["actor.0.weight"].double() + elif fault == "nan": + data["model"]["actor.0.bias"][0] = float("nan") + elif fault == "normalization": + data["model"]["scale"][0] += 1 + else: + del data["model"]["log_std"] + torch.save(data, bundle.checkpoint) + monkeypatch.setattr(replay, "CANDIDATE_SHA256", replay.sha256(bundle.checkpoint.read_bytes())) + with pytest.raises(ValueError): + replay.load_frozen_policy(bundle.checkpoint, bundle.manifest) + + +@pytest.mark.parametrize("shape,dtype", [((1, 32), torch.float32), ((0, 46), torch.float32), + ((1, 46), torch.float64), ((46,), torch.float32)]) +def test_invalid_observation_never_migrated(shape, dtype): + with pytest.raises(ValueError, match="46"): + replay.inference_action(torch.zeros(shape, dtype=dtype), "reference") + + +def test_invalid_actions_and_missing_policy_never_fall_back(): + x = torch.zeros(1, 46) + with pytest.raises(ValueError, match="fallback"): + replay.inference_action(x, "learned") + for action in (None, [float("nan"), 0, 0], [float("inf"), 0, 0], [1.01, 0, 0], [0, 0]): + with pytest.raises(ValueError, match="Script requires"): + replay.inference_action(x, "script", script_action=action) + x[0, 0] = float("nan") + with pytest.raises(ValueError): + replay.inference_action(x, "reference") + + +def test_controller_identity_cannot_confuse_script_reference_and_learned(): + identities = [replay.controller_identity("learned"), replay.controller_identity("reference"), + replay.controller_identity("script", [.5, 0, -.5])] + assert len({row["controller_kind"] for row in identities}) == 3 + assert [row["trained_residual_policy"] for row in identities] == [True, False, False] + assert all(row["optimizer_steps"] == 0 and not row["accepted_skill"] for row in identities) + x = torch.zeros(2, 46) + assert not replay.inference_action(x, "reference")[0].any() + assert torch.equal(replay.inference_action(x, "script", script_action=[.5, 0, -.5])[0], + torch.tensor([[.5, 0, -.5]]).expand(2, 3)) + with pytest.raises(ValueError): + replay.inference_action(x, "reference", policy=object()) + with pytest.raises(ValueError): + replay.controller_identity("learned", [0, 0, 0]) + + +@pytest.mark.parametrize("protected", ["releases/run", "logs/contact_cycle/sim_residual_training_v1/new"]) +def test_output_cannot_modify_historical_trees(bundle, protected): + with pytest.raises(ValueError, match="independent"): + replay.create_output(bundle.root, bundle.root / protected, bundle.checkpoint) + assert not (bundle.root / protected).exists() + + +def test_output_exclusive_and_symlink_protected(bundle, tmp_path): + output = replay.create_output(bundle.root, tmp_path / "new", bundle.checkpoint) + (output / "evidence").write_text("preserve") + with pytest.raises(FileExistsError): + replay.create_output(bundle.root, output, bundle.checkpoint) + assert (output / "evidence").read_text() == "preserve" + link = tmp_path / "link" + link.symlink_to(bundle.prepared, target_is_directory=True) + with pytest.raises(ValueError, match="independent"): + replay.create_output(bundle.root, link / "new", bundle.checkpoint) + + +def test_cli_preflight_is_cpu_only_and_preserves_failure(bundle, tmp_path, monkeypatch): + # A fake launcher turns any accidental Kit path into a test failure. + monkeypatch.setitem(sys.modules, "isaaclab.app", SimpleNamespace( + AppLauncher=lambda *a, **k: pytest.fail("CPU preflight imported Kit"))) + output = tmp_path / "check" + args = ["--check-only", "--output", str(output), "--checkpoint", str(bundle.checkpoint)] + assert cli.main(args) == 0 + result = json.loads((output / "result.json").read_text()) + assert result["preflight_passed"] and not result["physics_executed"] + assert result["trained_residual_policy"] and result["optimizer_steps"] == 0 + assert (output / "identity.json").is_file() + bundle.checkpoint.write_bytes(b"bad checkpoint") + failed = tmp_path / "failure" + args[2] = str(failed) + assert cli.main(args) == 1 + result = json.loads((failed / "result.json").read_text()) + assert not result["passed"] and not result["physics_executed"] + assert result["controller_mode"] == "learned" and "SHA256 mismatch" in result["error"] + assert (output / "result.json").is_file() + + +def test_cli_bad_script_recorded_without_launch(bundle, tmp_path): + output = tmp_path / "bad_script" + assert cli.main(["--replay", "--controller", "script", "--checkpoint", str(bundle.checkpoint), + "--output", str(output)]) == 1 + result = json.loads((output / "result.json").read_text()) + assert not result["physics_executed"] and not result["passed"] + + +def synthetic_recorder(): + spec = importlib.util.spec_from_file_location( + "synthetic_functional", ROOT / "tests/test_contact_cycle_functional.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + trace = module.example() + from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES + for name, shape in {"policy_snapshot": (46,), "actor_input": (46,), "requested_action": (3,), + "actor_mean": (3,), "processed_target": (3,), "reference_used_normalized": (3,), + "effective_residual_rad": (3,)}.items(): + trace[name] = torch.zeros(4500, 1, *shape) + trace["failure_flags"] = torch.zeros(4500, 1, len(FAILURE_NAMES), dtype=torch.bool) + trace["would_fail"] = torch.zeros(4500, 1, dtype=torch.bool) + recorder = SimpleNamespace(initial_state={k: v[0] for k, v in trace.items()}, stops=[4500], + early=[False], frames=[{k: v[i] for k, v in trace.items()} for i in range(4500)]) + return recorder, module.CFG + + +@pytest.mark.parametrize("mode", ["reference", "script", "learned"]) +def test_recording_full_six_phases_and_explicit_controller(tmp_path, mode): + recorder, functional = synthetic_recorder() + if mode == "learned": + for frame in recorder.frames: + frame["actor_metadata_available"].fill_(True) + rows = cli.record_episodes(recorder, tmp_path, mode, functional, torch.zeros(3), + [{"reason": "timeout", "control_step": 900}]) + assert rows[0]["success"] and all(rows[0]["phase_samples"].values()) + trace = torch.load(tmp_path / "first_episodes.pt", weights_only=True)["0"] + assert {"normal", "gap", "root_position_error_m", "root_velocity_error_m_s", "requested_action", + "reference_used_normalized", "effective_residual_rad", "failure_flags"} <= trace.keys() + assert len(trace["time"]) == 4500 and rows[0]["terminal_causes"] == [] + assert json.loads((tmp_path / "episodes.json").read_text())["controller_mode"] == mode + + +def test_recording_keeps_failure_and_never_splices_reset_episode(tmp_path): + recorder, functional = synthetic_recorder() + recorder.stops, recorder.early = [12], [True] + recorder.frames[10]["failure_flags"][0, 0] = True + rows = cli.record_episodes(recorder, tmp_path, "reference", functional, torch.zeros(3), + [{"reason": "physical_failure", "control_step": 3}]) + assert not rows[0]["success"] and rows[0]["terminal_causes"] + assert rows[0]["first_failure_time_s"] == pytest.approx(.022) + trace = torch.load(tmp_path / "first_episodes.pt", weights_only=True)["0"] + assert len(trace["time"]) == 12 + + +def test_recording_exception_partial_trace_stays_failed(tmp_path): + recorder, functional = synthetic_recorder() + recorder.stops = [None] + recorder.frames = recorder.frames[:7] + rows = cli.record_episodes(recorder, tmp_path, "reference", functional, torch.zeros(3), + [{"reason": "incomplete", "control_step": None}]) + assert not rows[0]["success"] and rows[0]["first_episode_samples"] == 7 + assert (tmp_path / "first_episodes.pt").is_file() + + +def test_actual_frozen_candidate_cpu_only_when_artifacts_present(): + checkpoint = ROOT / replay.CANDIDATE + if not checkpoint.is_file(): + pytest.skip("Frozen training artifacts must be copied separately; never synthesize a candidate") + manifest, binding = replay.verify_bundle(ROOT, checkpoint) + policy = replay.load_frozen_policy(checkpoint, manifest) + action, _, _ = replay.inference_action(torch.zeros(1, 46), "learned", policy) + assert action.shape == (1, 3) and torch.isfinite(action).all() + assert binding["candidate_sha256"] == "c088bb3aecaca751867118858626fd508f7a3983303802d7524353f80802cf99" + + +def test_snapshot_symlink_rejected_even_with_identical_bytes(bundle, tmp_path): + path = bundle.prepared / "source_snapshot/AGENT.md" + copy_path = tmp_path / "same_bytes" + copy_path.write_bytes(path.read_bytes()) + path.unlink() + path.symlink_to(copy_path) + with pytest.raises(ValueError, match="Unsafe bound source"): + replay.verify_bundle(bundle.root, bundle.checkpoint) + + +def test_cli_startup_failure_is_not_success_or_reference_fallback(bundle, tmp_path, monkeypatch): + def fail_launcher(**kwargs): + raise RuntimeError("synthetic launcher failure, no Kit instantiated") + + monkeypatch.chdir(tmp_path) + monkeypatch.setitem(sys.modules, "isaaclab.app", SimpleNamespace(AppLauncher=fail_launcher)) + output = tmp_path / "startup_failure" + assert cli.main(["--replay", "--output", str(output), "--checkpoint", str(bundle.checkpoint)]) == 1 + result = json.loads((output / "result.json").read_text()) + assert result["preflight_passed"] and not result["passed"] and not result["physics_executed"] + assert result["controller_mode"] == "learned" and "synthetic launcher failure" in result["error"] + + +def test_cli_constructor_failure_records_possible_physics_before_entry(bundle, tmp_path, monkeypatch): + output = tmp_path / "constructor_failure" + closed = [] + monkeypatch.chdir(tmp_path) + manifest = {**bundle.manifest, "training": {**bundle.manifest["training"], "validation_seed": 91502}} + monkeypatch.setattr(replay, "verify_bundle", lambda *args: (manifest, {})) + monkeypatch.setattr(replay, "load_frozen_policy", lambda *args: object()) + monkeypatch.setitem(sys.modules, "isaaclab.app", SimpleNamespace( + AppLauncher=lambda **kwargs: SimpleNamespace(app=SimpleNamespace( + close=lambda **kwargs: closed.append(kwargs))))) + monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle.recording", + SimpleNamespace(PhysicalSafetyAudit=object)) + monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle_sim.cfg", SimpleNamespace( + SimContactCycleEnvCfg=lambda **kwargs: SimpleNamespace(scene=SimpleNamespace(), sim=SimpleNamespace()))) + + def fail_constructor(**kwargs): + # Persisted before entry, including if construction never returns to Python. + entered = json.loads((output / "result.json").read_text()) + assert entered["initialization_entered"] is True + assert entered["physics_executed"] is None and entered["status"] == "initializing" + raise RuntimeError("synthetic constructor failure after possible PhysX warmup") + + monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle_sim.env", + SimpleNamespace(SimContactCycleEnv=fail_constructor)) + assert cli.main(["--replay", "--output", str(output), "--checkpoint", str(bundle.checkpoint)]) == 1 + result = json.loads((output / "result.json").read_text()) + assert result["initialization_entered"] is True and result["physics_executed"] is None + assert result["status"] == "failed" and not result["passed"] + assert result["controller_mode"] == "learned" and "synthetic constructor failure" in result["error"] + assert closed == [{"exit_code": 1}] + + +@pytest.mark.parametrize("fault", ["physical_failure", "step_exception", "guard_exception"]) +def test_run_episode_failure_saves_partial_recording_without_second_episode(tmp_path, monkeypatch, fault): + recorder, functional = synthetic_recorder() + templates = recorder.frames + recorder.frames = [] + recorder.stops, recorder.early = [None], [False] + + class FakeRecorder: + def __init__(self, env): + self.__dict__.update(recorder.__dict__) + + def __enter__(self): + return self + + def __exit__(self, *args): + return False + + def before_control(self, *args): + pass + + def end_control(self, terminated, truncated, final): + if bool((terminated | truncated).any()): + self.stops[0], self.early[0] = len(self.frames), True + + calls = {"step": 0, "reset": 0, "guard": 0} + + def reset(): + calls["reset"] += 1 + return {"policy": torch.zeros(1, 46)}, {} + + def step(action): + calls["step"] += 1 + start = len(recorder.frames) + recorder.frames.extend(templates[start:start + 5]) + if fault == "step_exception" and calls["step"] == 2: + raise RuntimeError("synthetic step failure") + terminated = torch.tensor([fault == "physical_failure" and calls["step"] == 2]) + return {"policy": torch.zeros(1, 46)}, torch.zeros(1), terminated, torch.tensor([False]), {} + + def guard(): + calls["guard"] += 1 + if fault == "guard_exception" and calls["guard"] == 4: + raise RuntimeError("synthetic safety guard failure") + + state = SimpleNamespace(robot=SimpleNamespace(is_fixed_base=False), scenarios=torch.zeros(1), + stand_only=torch.zeros(1, dtype=torch.bool), nominal=torch.zeros(3)) + monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle_sim.mdp", + SimpleNamespace(state=lambda env: state)) + monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle_sim.recording", + SimpleNamespace(SimCycleRecorder=FakeRecorder)) + cfg = SimpleNamespace(sim=SimpleNamespace(physics=SimpleNamespace( + solver_type=0, enable_external_forces_every_iteration=False)), + terminations=SimpleNamespace(failed=object()), episode_length_s=9) + env = SimpleNamespace(cfg=cfg, physics_dt=.002, step_dt=.01, num_envs=1, reset=reset, step=step) + args = SimpleNamespace(controller="reference", script_action=None, output=tmp_path) + if fault == "physical_failure": + result = cli.run_episode(env, args, None, functional, guard) + assert not result["passed"] and result["control_steps"] == 2 + else: + with pytest.raises(RuntimeError, match="synthetic"): + cli.run_episode(env, args, None, functional, guard) + assert calls["reset"] == 1 and calls["step"] == 2 + rows = json.loads((tmp_path / "episodes.json").read_text())["rows"] + assert not rows[0]["success"] and rows[0]["first_episode_samples"] == 10 + assert rows[0]["termination"]["reason"] == ("physical_failure" if fault == "physical_failure" else "exception") + assert (tmp_path / "first_episodes.pt").is_file() diff --git a/tests/test_contact_cycle_sim_residual.py b/tests/test_contact_cycle_sim_residual.py new file mode 100644 index 0000000..efd4ddc --- /dev/null +++ b/tests/test_contact_cycle_sim_residual.py @@ -0,0 +1,94 @@ +"""Pure CPU simulation-residual protocol tests. No robot optimization.""" +import json +from pathlib import Path +from types import SimpleNamespace + +import pytest +import torch +from SkillFactory.legs.contact_cycle_sim.config import FEATURE_VERSION, combine, load_config, pose_cost +from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic, features +from SkillFactory.legs.contact_cycle_sim.reference import ReferenceController +from SkillFactory.legs.spec import load_bundle +from SkillFactory.legs.tokens import LegKinematics + +ROOT = Path(__file__).resolve().parents[1] +CONFIG = ROOT / 'configs/contact_cycle/simulation_residual_v1.json' + + +def test_composition_bounds_zero_identity_and_saturation(): + reference = torch.tensor([[.99, -.99, .2], [.2, -.1, .5]]) + full, requested, delta = combine(reference, torch.zeros_like(reference), .05, .5) + assert torch.equal(full, reference) and not delta.any() and not requested.any() + full, requested, delta = combine(reference, torch.tensor([[100., -100., 100.], [-100., 100., -100.]]), .05, .5) + assert full.abs().max() <= 1 and requested.abs().max() <= 1 + assert delta.abs().max() <= .050001 + assert abs(float(delta[0, 0]) - .005) < 1e-7 # projection can shrink, never enlarge, the requested residual + + +@pytest.mark.parametrize('bad', [torch.zeros(2, 4), torch.zeros(2, 3, dtype=torch.float64), + torch.full((2, 3), float('nan'))]) +def test_invalid_residual_rejected(bad): + with pytest.raises(ValueError): + combine(torch.zeros(2, 3), bad, .05, .5) + + +@pytest.mark.parametrize('reference', [torch.full((2, 3), float('nan')), torch.full((2, 3), 1.1)]) +def test_corrupt_reference_rejected(reference): + with pytest.raises(ValueError): + combine(reference, torch.zeros_like(reference), .05, .5) + + +def test_dense_pose_cost_removes_support_height_blind_spot(): + c = load_config(CONFIG)['reward'] + error = torch.tensor([[0., 0., 0.], [0., 0., -.05], [0., 0., -.0996]]) + cost, velocity = pose_cost(error, torch.zeros_like(error), c) + assert cost[0] == 0 and cost[2] > cost[1] > 0 and not velocity.any() + assert .9255 - c['position_weight'] * cost[2] < 0 + assert pose_cost(torch.full((1, 3), 100.), torch.full((1, 3), 100.), c)[0].item() == c['cost_cap'] + + +def test_sim_network_zero_mean_and_distinct_feature_contract(): + model = SimResidualActorCritic(hidden=16) + x = torch.randn(4, 46) + with torch.no_grad(): + action, latent, logp, value = model.act(x, deterministic=True) + assert not action.any() and not latent.any() and torch.isfinite(logp).all() and torch.isfinite(value).all() + assert model.feature_version == FEATURE_VERSION + with pytest.raises(ValueError): + features(torch.zeros(4, 32)) + + +@pytest.mark.parametrize('field,value', [('residual_limit_rad', .2), ('residual_limit_rad', float('nan')), + ('features', 'contact-cycle-32-fixed-si-v1')]) +def test_config_rejects_old_identity_or_unbounded_residual(tmp_path, field, value): + c = json.loads(CONFIG.read_text()) + c[field] = value + p = tmp_path / 'bad.json' + p.write_text(json.dumps(c)) + with pytest.raises(ValueError): + load_config(p) + + +def test_reference_indexed_reset_and_refresh_are_isolated(): + bundle = load_bundle(ROOT / 'assets/generated/legs/go2_fl') + nominal = torch.tensor([j['q_default'] for j in bundle['spec']['joints']]) + env = SimpleNamespace(num_envs=3, device='cpu', cfg=SimpleNamespace(support_parameters={'action_scale_rad': .5})) + fk = LegKinematics(bundle, 'cpu') + transform = fk.forward(nominal.expand(3, -1))[bundle['foot']['body']] + target = transform[:, :3, 3] + (transform[:, :3, :3] @ torch.tensor(bundle['foot']['xyz'])[:, None]).squeeze(-1) + zero = torch.zeros(3, 3) + quat = torch.tensor([[0., 0., 0., 1.]]).expand(3, -1) + s = SimpleNamespace(bundle=bundle, env=env, nominal=nominal, + root=lambda: (zero, quat, zero, zero), + reference=lambda: (None, None, target, torch.full((3,), 50.), None)) + ref = ReferenceController(s, load_config(CONFIG)['reference']) + ref.reset(torch.arange(3)) + old = ref.q_ik.clone(), ref.action.clone(), ref.refreshes.clone() + target[1, 0] += .01 + ref.refresh(torch.tensor([1])) + assert torch.equal(ref.q_ik[[0, 2]], old[0][[0, 2]]) + assert torch.equal(ref.action[[0, 2]], old[1][[0, 2]]) + assert ref.refreshes.tolist() == [1, 2, 1] + ref.reset(torch.tensor([1])) + assert ref.refreshes.tolist() == [1, 1, 1] + assert torch.isfinite(ref.action).all() and ref.action.abs().max() <= 1 diff --git a/tests/test_contact_cycle_sim_residual_physics.py b/tests/test_contact_cycle_sim_residual_physics.py new file mode 100644 index 0000000..ef37216 --- /dev/null +++ b/tests/test_contact_cycle_sim_residual_physics.py @@ -0,0 +1,32 @@ +"""Explicit non-learning PhysX gate; a skip is not physical evidence.""" +import json +import os +import subprocess +import sys +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[1] + + +@pytest.mark.skipif(os.environ.get('SIM_RESIDUAL_RUNTIME_TESTS') != '1', reason='requires supervised Isaac PhysX') +def test_real_nominal_reference_residual(tmp_path): + env = os.environ.copy() + env.pop('CONDA_PREFIX', None) + env.pop('VIRTUAL_ENV', None) + env['PYTHONPATH'] = str(ROOT / 'source/SkillFactory') + lab = env.get('ISAACLAB_LAUNCHER', '/home/cen/IsaacLab/isaaclab.sh') + process = subprocess.run([ + sys.executable, str(ROOT / 'scripts/supervise_sim.py'), '--timeout', '240', + '--log', str(tmp_path / 'kit.log'), '--report', str(tmp_path / 'process.json'), '--', + lab, '-p', str(ROOT / 'scripts/verify_cycle_sim_residual.py'), + '--output', str(tmp_path / 'trial'), '--headless', + ], cwd=ROOT, env=env, capture_output=True, text=True, timeout=270) + assert process.returncode == 0, process.stdout[-3000:] + process.stderr[-3000:] + result = json.loads((tmp_path / 'trial/result.json').read_text()) + supervisor = json.loads((tmp_path / 'process.json').read_text()) + assert result['passed'] and result['sim_functional_passed'] and not result['accepted_skill'] + assert result['optimizer_steps'] == 0 and not result['trained_residual_policy'] + assert result['max_actual_residual_rad'] <= .050001 + assert supervisor['passed'] and supervisor['cleanup']['clean'] and not supervisor['critical_log_lines'] diff --git a/tests/test_contact_cycle_sim_rootxy_launch_v2.py b/tests/test_contact_cycle_sim_rootxy_launch_v2.py new file mode 100644 index 0000000..80b12cd --- /dev/null +++ b/tests/test_contact_cycle_sim_rootxy_launch_v2.py @@ -0,0 +1,562 @@ +"""CPU-only launch contract/lifecycle mocks. NEVER imports Kit or starts a worker.""" +import contextlib +import importlib.util +import json +import sys +from pathlib import Path +from types import SimpleNamespace + +import pytest + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'scripts')) +import rootxy_launch_v2 as a + +spec = importlib.util.spec_from_file_location('rootxy_runtime_cpu_test', ROOT / + 'source/SkillFactory/SkillFactory/legs/contact_cycle_sim/rootxy_runtime_v2.py') +runtime = importlib.util.module_from_spec(spec) +spec.loader.exec_module(runtime) + + +@pytest.fixture +def desktop_gpu(tmp_path, monkeypatch): + library = tmp_path / 'nvml.so'; library.write_bytes(b'mock') + client = {'pid': 7, 'starttime': 123, 'sha256': 'executable-hash'} + identity = {'library': str(library), 'sha256': a.b.digest(library), 'uuid': 'GPU-test', + 'index': 0, 'desktop_clients': [client]} + current = {'compute': set(), 'graphics': {7}} + monkeypatch.setattr(a.ctypes, 'CDLL', lambda _: object()) + def call(self, name, *args): + if name == 'nvmlDeviceGetUUID': + args[1].value = b'GPU-test' + monkeypatch.setattr(a.GPUOwnership, 'call', call) + monkeypatch.setattr(a.GPUOwnership, 'clients', lambda _: current) + monkeypatch.setattr(a, 'desktop_identity', lambda pid, hash_executable=True: + dict(client) if hash_executable else {k: v for k, v in client.items() if k != 'sha256'}) + return SimpleNamespace(identity=identity, client=client, current=current, + owned=SimpleNamespace(discover=lambda: {99: object()})) + + +@pytest.mark.parametrize('compute,graphics,final,passes', [ + (set(), {7}, False, True), ({99}, {7, 99}, False, True), + (set(), set(), True, True), (set(), {7}, True, True), + ({7}, {7}, False, False), ({8}, {7}, False, False), + (set(), {7, 8}, False, False), ({99}, {7}, True, False), + (set(), {7, 99}, True, False), +]) +def test_gpu_desktop_sharing_never_allows_foreign_compute_or_new_graphics(desktop_gpu, compute, graphics, final, passes): + f = desktop_gpu + gpu = a.GPUOwnership(f.identity) + f.current.update(compute=compute, graphics=graphics) + if passes: + report = gpu.check(f.owned, final=final) + assert report['approved_desktop_graphics_pids'] == sorted(graphics & {7}) + assert report['clean'] == (not compute and not (graphics - {7})) + else: + with pytest.raises(RuntimeError, match='GPU ownership'): + gpu.check(f.owned, final=final) + + +@pytest.mark.parametrize('change', ['foreign_compute', 'new_graphics', 'missing_graphics', 'birth', 'hash', 'duplicate']) +def test_gpu_admission_requires_exact_parent_desktop_baseline(desktop_gpu, change): + f = desktop_gpu + if change == 'foreign_compute': f.current['compute'] = {7} + if change == 'new_graphics': f.current['graphics'].add(8) + if change == 'missing_graphics': f.current['graphics'].clear() + if change in ('birth', 'hash'): + f.identity['desktop_clients'] = [{**f.client, ('starttime' if change == 'birth' else 'sha256'): 'changed'}] + if change == 'duplicate': f.identity['desktop_clients'] *= 2 + with pytest.raises((ValueError, RuntimeError)): + a.GPUOwnership(f.identity) + + +def test_gpu_runtime_pid_reuse_and_final_executable_change_fail(desktop_gpu, monkeypatch): + f = desktop_gpu + gpu = a.GPUOwnership(f.identity) + monkeypatch.setattr(a, 'desktop_identity', lambda *args, **kwargs: {'pid': 7, 'starttime': 999}) + with pytest.raises(RuntimeError, match='identity changed'): + gpu.check(f.owned) + with pytest.raises(RuntimeError, match='identity changed'): + gpu.check(f.owned, final=True) + + +@pytest.mark.parametrize('uid,command,comm,allowed', [ + (0, b'/usr/lib/xorg/Xorg\0vt2\0', 'Xorg', True), + (1000, b'/usr/lib/xorg/Xorg\0', 'Xorg', False), + (0, b'/usr/bin/other\0', 'Xorg', False), + (0, b'/usr/lib/xorg/Xorg\0', 'other', False), +]) +def test_inaccessible_executable_only_explicit_root_xorg_metadata(tmp_path, monkeypatch, uid, command, comm, allowed): + proc = tmp_path / '7'; proc.mkdir() + fields = ['0'] * 20; fields[19] = '123' + (proc / 'stat').write_text('7 (Xorg) ' + ' '.join(fields)) + (proc / 'cmdline').write_bytes(command) + (proc / 'comm').write_text(comm) + old_stat, old_resolve = Path.stat, Path.resolve + monkeypatch.setattr(Path, 'stat', lambda self, *args, **kwargs: + SimpleNamespace(st_uid=uid) if self == proc else old_stat(self, *args, **kwargs)) + def resolve(self, *args, **kwargs): + if self == proc / 'exe': + raise PermissionError('root display server') + return old_resolve(self, *args, **kwargs) + monkeypatch.setattr(Path, 'resolve', resolve) + monkeypatch.setattr(a, 'Path', lambda value: tmp_path if value == '/proc' else Path(value)) + monkeypatch.setattr(a.b, 'digest', lambda _: 'pinned-file-hash') + if allowed: + identity = a.desktop_identity(7) + assert identity['executable_identity_scope'] == 'root-Xorg-public-metadata; actual-executable-unverified' + assert identity['starttime'] == 123 and identity['uid'] == 0 + assert identity['command_sha256'] == a.hashlib.sha256(command).hexdigest() + else: + with pytest.raises(PermissionError): + a.desktop_identity(7) + + +def test_preimport_requires_all_binding_pins_without_evaluation_recorder(monkeypatch): + binding = SimpleNamespace(REQUIRED_RUNTIME={'runtime': None}, + ISAAC_PINS={'runtime': None, 'lazy.physx': None}) + imported = [] + monkeypatch.setattr(a.importlib, 'import_module', lambda name: imported.append(name)) + a.preimport_binding_runtime(binding) + assert imported == ['runtime', 'lazy.physx'] + def fail(name): + if name == 'lazy.physx': + raise ImportError('missing exact backend') + monkeypatch.setattr(a.importlib, 'import_module', fail) + with pytest.raises(ImportError, match='exact backend'): + a.preimport_binding_runtime(binding) + + +def write(path, value): + path.write_bytes(a.canonical(value)) + + +def test_cli_prepare_and_run_require_new_exact_grants_before_import_or_output(tmp_path, monkeypatch): + import train_cycle_sim_rootxy_v2 as cli + monkeypatch.setattr(cli, 'cpu_protocol', lambda: pytest.fail('No CPU/Kit import on denied admission')) + monkeypatch.setattr(a, 'supervise', lambda _: pytest.fail('No supervisor without grant')) + for mode in ('--run', '--prepare'): + with pytest.raises(PermissionError, match='admission INCOMPLETE'): + cli.main([mode, '--output', str(tmp_path / 'new')]) + assert not (tmp_path / 'new').exists() + + +def test_physics_attempted_completed_reset_partial_failure_and_caps(): + events = [] + class Sim: + fail = False + def step(self): + events.append('actual') + if self.fail: + raise RuntimeError('simstepfailure') + class Env: + def reset(self): + return self._reset_idx() + def _reset_idx(self): + return Sim().step() + original_step, original_reset = Sim.step, Env.reset + hooks = runtime.RootXYPhysicsHooks(Sim, Env, lambda: None, lambda x: events.append(dict(x))) + with hooks: + Env().reset() + assert hooks.initialization == hooks.completed == 1 + hooks.mode = 'rollout' + sim = Sim() + sim.fail = True + with pytest.raises(RuntimeError, match='simstepfailure'): + sim.step() + assert hooks.attempted == 2 and hooks.completed == 1 and hooks.rollout == 1 + hooks.rollout = 72460 + with pytest.raises(RuntimeError, match='cap'): + Sim().step() + assert Sim.step is original_step and Env.reset is original_reset + assert events[0]['attempted_shared_calls'] == 1 and events[1] == 'actual' + + +def test_physics_safety_missing_reset_sample_is_fatal_and_prep_zero_calls(): + class Sim: + def step(self): + pass + class Env: + def reset(self): + Sim().step() + _reset_idx = reset + audit = SimpleNamespace(samples=0, check=lambda: None, report=lambda: {}) + hooks = runtime.RootXYPhysicsHooks(Sim, Env, lambda: None, lambda _: None) + with hooks: + hooks.attach_audit(audit) + with pytest.raises(RuntimeError, match='safety sample'): + Env().reset() + assert hooks.attempted == hooks.completed == 1 + with runtime.RootXYPhysicsHooks(Sim, Env, lambda: None, lambda _: None, preparation=True) as hooks: + with pytest.raises(PermissionError, match='PREP'): + Sim().step() + assert hooks.attempted == hooks.completed == 0 + + +@pytest.mark.parametrize('fault', ['start', 'partial_start', 'construct', 'identity', 'learn', + 'close_env', 'postclose_identity', 'publication', 'close_app', None]) +def test_worker_lifecycle_failures_are_provisional_and_owner_closes(fault): + events, published = [], [] + class API: + started = False + physics_hooks = None + def start(self): + events.append('start') + if fault == 'start': + raise RuntimeError(fault) + self.started = True + if fault == 'partial_start': + raise RuntimeError(fault) + @contextlib.contextmanager + def physics(self): + events.append('hooks_enter') + yield SimpleNamespace(attempted=0, attach_audit=lambda _: None, safety_boundary=lambda: None, + report=lambda: {}) + events.append('hooks_exit') + def construct(self): + events.append('construct') + if fault == 'construct': + raise RuntimeError(fault) + def check_actual(self): + events.append('actual') + @contextlib.contextmanager + def safety(self): + yield object() + def learn(self): + events.append('learn') + if fault == 'learn': + raise RuntimeError(fault) + return {'status': 'completed_provisional', 'training_complete': True, + 'candidate': {'path': 'forensic.pt'}, 'accepted_skill': False} + def close_env(self): + events.append('close_env') + if fault == 'close_env': + raise RuntimeError(fault) + def close_app(self, code): + events.append('close_app') + if fault == 'close_app': + raise RuntimeError(fault) + api = API() + def verify(): + events.append('verify') + if fault == 'identity' or fault == 'postclose_identity' and 'close_env' in events: + raise RuntimeError(fault) + def publish(value): + published.append(dict(value)) + if fault == 'publication': + raise OSError(fault) + call = lambda: runtime.worker_lifecycle(api, prepare=False, guard=lambda: None, + verify=verify, publish=publish) + if fault is None: + assert call()['status'] == 'completed_provisional' + assert events.index('construct') < events.index('actual') < events.index('learn') + else: + with pytest.raises((RuntimeError, OSError)): + call() + assert published[-1]['candidate'] is None and not published[-1]['training_complete'] + assert 'close_env' in events and 'close_app' in events + assert events.index('close_env') < events.index('close_app') + if 'hooks_exit' in events: + assert events.index('close_env') < events.index('close_app') < events.index('hooks_exit') + + +def test_guard_deadline_cancellation_storage_and_atomic_write_failure(tmp_path, monkeypatch): + monkeypatch.setattr(a.b, 'boottime', lambda: 10.) + budget = a.GuardBudget(tmp_path, 10., 'run') + budget.write(tmp_path / 'value.json', {'ok': True}) + monkeypatch.setattr(a.b, 'boottime', lambda: 7090.) + with pytest.raises(RuntimeError, match='deadline'): + budget.check() + budget.check(cleanup=True) + monkeypatch.setattr(a.b, 'boottime', lambda: 10.) + budget.interrupted = True + with pytest.raises(RuntimeError, match='cancelled'): + budget.check() + budget.interrupted = False + budget.external_growth = a.CAP + with pytest.raises(RuntimeError, match='storage'): + budget.check() + budget.external_growth = 0 + monkeypatch.setattr(a.os, 'replace', lambda *_: (_ for _ in ()).throw(OSError('atomic failure'))) + with pytest.raises(OSError): + budget.write(tmp_path / 'value.json', {'ok': False}) + assert a.read(tmp_path / 'value.json') == {'ok': True} + assert (tmp_path / 'value.json.pending').exists() # forensic bytes remain charged + + +def test_native_monitor_failure_is_not_swallowed(tmp_path, monkeypatch): + monkeypatch.setattr(a.b, 'boottime', lambda: 10.) + monitor = SimpleNamespace(check=lambda: (_ for _ in ()).throw(RuntimeError('nativefail'))) + with pytest.raises(RuntimeError, match='nativefail'): + a.GuardBudget(tmp_path, 10., 'run', monitor).check() + + +def test_source_hash_drift_is_rejected(tmp_path, monkeypatch): + (tmp_path / 'file').write_text('original') + manifest = {'format': a.PREREG_FORMAT, 'task_id': a.TASK_ID, 'launch_integration_complete': True, + 'accepted_skill': False, 'sources': {'file': a.b.digest(tmp_path / 'file')}, + 'training': {'seeds': [92511], 'validation_seed': 93511, 'num_envs': 128, + 'max_total_wall_s': 7200, 'cleanup_reserve_s': 120, 'max_artifact_bytes': a.CAP, + 'max_validation_transitions': 806400, 'validation_updates': [64, 128, 192, 256]}} + monkeypatch.setattr(a, 'ROOT', tmp_path) + a.verify_sources(manifest) + (tmp_path / 'file').write_text('drift') + with pytest.raises(ValueError, match='source drift'): + a.verify_sources(manifest) + + +@pytest.fixture +def supervisor(tmp_path, monkeypatch): + root = tmp_path / 'run' + args = SimpleNamespace(prepare=True, output=root, manifest=tmp_path / 'manifest.json', + trusted_manifest_sha256='m') + write(args.manifest, {'sources': {}}) + manifest, grant, policy = {'sources': {}}, {'gpu_identity': {'uuid': 'GPU-mock'}}, {} + monkeypatch.setattr(a, 'admission', lambda *_: (manifest, grant, policy)) + monkeypatch.setattr(a, 'verify_sources', lambda *_: None) + monkeypatch.setattr(a.b, 'boottime', lambda: 10.) + monkeypatch.setattr(a.owner, 'launch_command', lambda *_: ['NOT_EXECUTED']) + monkeypatch.setattr(a, 'launch_identity', lambda: {}) + monkeypatch.setattr(a.owner, 'bound_filesystem_inputs', lambda *_: {}) + monkeypatch.setattr(a.owner, 'verify_filesystem_inputs', lambda *_: {}) + monkeypatch.setattr(a.ctypes, 'CDLL', lambda *_ , **__: SimpleNamespace(prctl=lambda *_: 0)) + monkeypatch.setattr(a.signal, 'signal', lambda *_: None) + monkeypatch.setattr(a.signal, 'pidfd_send_signal', lambda *_: None, raising=False) + monkeypatch.setattr(a.os, 'pidfd_open', lambda *_: None, raising=False) + monkeypatch.setattr(a, 'GPUOwnership', lambda _: SimpleNamespace(check=lambda *_ , **__: {})) + owned = SimpleNamespace(unknown=False, known=set(), attach=lambda _: None, discover=lambda: {}) + monkeypatch.setattr(a.owner, 'OwnedProcesses', lambda: owned) + monitor = SimpleNamespace(check=lambda **_: None, latest={}, baseline={}, report=lambda: {}) + monkeypatch.setattr(a.owner, 'NativeMonitor', lambda *_: monitor) + child = SimpleNamespace(pid=12345, stdout=SimpleNamespace(close=lambda: None), returncode=0, + poll=lambda: 0, wait=lambda **_: 0) + monkeypatch.setattr(a.subprocess, 'Popen', lambda *_ , **__: child) + capture = SimpleNamespace(eof=True, discarded=0, error=None, check=lambda: None, + pump=lambda: None, close=lambda: None) + monkeypatch.setattr(a.owner, 'StdoutCapture', lambda *_: capture) + monkeypatch.setattr(a.owner, 'cleanup_group', lambda *_: True) + def exchange(*_): + write(root / 'prepare-worker-result.json', {'status': 'prepared_config_only', 'physics_calls': 0, + 'postclose_identity_verified': True}) + write(root / 'training-binding.json', {}) + write(root / 'config-preparation-provisional.json', {'status': 'prepared_config_only'}) + monkeypatch.setattr(a.owner, 'exchange_worker', exchange) + return args, child, monitor, capture, owned + + +@pytest.mark.parametrize('fault', ['spawn', 'attach', 'handshake', 'workerexit', 'nonzero', + 'nativefail', 'cleanupfailure', 'postexit', None]) +def test_supervisor_independent_exit_native_and_cleanup_gate(supervisor, monkeypatch, fault): + args, child, monitor, capture, owned = supervisor + def fail(*_, **__): + raise RuntimeError(fault) + if fault == 'spawn': + monkeypatch.setattr(a.subprocess, 'Popen', fail) + elif fault == 'attach': + owned.attach = fail + elif fault == 'handshake': + monkeypatch.setattr(a.owner, 'exchange_worker', fail) + elif fault == 'workerexit': + child.wait = fail + elif fault == 'nonzero': + child.returncode = 1 + elif fault == 'nativefail': + monitor.check = fail + elif fault == 'cleanupfailure': + monkeypatch.setattr(a.owner, 'cleanup_group', lambda *_: False) + elif fault == 'postexit': + monkeypatch.setattr(a.owner, 'verify_filesystem_inputs', fail) + if fault == 'nativefail': + with pytest.raises(RuntimeError, match='nativefail'): + a.supervise(args) # cannot publish through a failed monitor + else: + result = a.supervise(args) + assert result == (0 if fault is None else 1) + assert not (args.output / 'locked_candidate.json').exists() + if (args.output / 'prepare-supervisor.json').exists(): + summary = a.read(args.output / 'prepare-supervisor.json') + assert summary['success'] is (fault is None) + + +def eligible_rows(xy=.02): + return [{'env': i, 'success': True, 'physics_samples': 4500, 'complete_clock': True, + 'rootxy_rms_m': xy, 'rootxy_max_m': xy, 'rootxy_breached': False, 'safety_ok': True, + 'height_rms_m': .01, 'swing_rms_m': .01, 'landing_error_m': .01, + 'functional_audit': {'sim_functional_passed': True}} for i in range(128)] + + +def test_candidate_commit_independently_rechecks_eligibility_and_publication(tmp_path, monkeypatch): + monkeypatch.setattr(a.b, 'boottime', lambda: 10.) + budget = a.GuardBudget(tmp_path, 10., 'run') + write(tmp_path / 'manifest.json', {}) + for update in (64, 128, 192, 256): + write(tmp_path / f'validation_u{update:04d}.json', {'rows': eligible_rows()}) + path = tmp_path / 'checkpoint_u0064.pt' + path.write_bytes(b'forensic checkpoint bytes') + key = [.02, .01 / .06 + .01 / .025 + .01 / .05, 64] + # Match the exact shared floating reduction, not a rounded displayed metric. + key[0] = sum(.02 for _ in range(128)) / 128 + key[1] = sum(key[1] for _ in range(128)) / 128 + result = {'status': 'completed_provisional', 'training_complete': True, 'accepted_skill': False, + 'checkpoints_selectable': False, 'postclose_identity_verified': True, + 'candidate': {'path': path.name, 'sha256': a.b.digest(path), 'update': 64, 'selection_key': key}} + lock = a.commit_candidate(tmp_path, result, {}, budget) + assert lock['accepted_skill'] is False + (tmp_path / 'locked_candidate.json').unlink() + rows = eligible_rows() + rows[4]['functional_audit']['sim_functional_passed'] = False + write(tmp_path / 'validation_u0128.json', {'rows': rows}) + with pytest.raises(ValueError, match='functional'): + a.commit_candidate(tmp_path, result, {}, budget) + assert not (tmp_path / 'locked_candidate.json').exists() + write(tmp_path / 'validation_u0128.json', {'rows': eligible_rows()}) + monkeypatch.setattr(budget, 'write', lambda *_ , **__: (_ for _ in ()).throw(OSError('final commit'))) + with pytest.raises(OSError, match='final commit'): + a.commit_candidate(tmp_path, result, {}, budget) + assert path.exists() and not (tmp_path / 'locked_candidate.json').exists() + + +@pytest.mark.parametrize('mutation', [None, 'old_format', 'wrong_task', 'expired', 'long_admission', + 'fake_p2_in_prep', 'extra_field', 'bad_hash', 'run_missing_digest']) +def test_prepare_exact_grant_schema_and_expiry(tmp_path, monkeypatch, mutation): + root = tmp_path / 'new' + manifest = tmp_path / 'manifest.json' + grant_path = tmp_path / 'grant.json' + write(manifest, {}) + monkeypatch.setattr(a, 'verify_sources', lambda _: None) + monkeypatch.setattr(a, 'launch_identity', lambda: {}) + monkeypatch.setattr(a, 'policy_check', lambda *_: {'not_after_boottime': 9000.}) + grant = {'format': a.GRANT_FORMAT, 'profile': a.PROFILE, 'task_id': a.TASK_ID, 'mode': 'prepare', + 'artifact_root': str(root), 'manifest_sha256': a.b.digest(manifest), + 'storage_policy_sha256': 'p', 'launch_identity_sha256': a.sha({}), + 'boot_id': a.b.boot_id(), 'not_after_boottime': 1000., 'parent_review': 'config-only', + 'preparation_sha256': None, 'binding_sha256': None, 'p2_decision_sha256': None, + 'gpu_identity': {}} + if mutation == 'old_format': + grant['format'] = 'skillfactory.evaluation-parent-grant.v2' + elif mutation == 'wrong_task': + grant['task_id'] = 'v1' + elif mutation == 'expired': + grant['not_after_boottime'] = 10. + elif mutation == 'long_admission': + grant['not_after_boottime'] = 3611. + elif mutation == 'fake_p2_in_prep': + grant['p2_decision_sha256'] = '0' * 64 + elif mutation == 'extra_field': + grant['approved'] = True + elif mutation == 'run_missing_digest': + grant['mode'] = 'run' + write(grant_path, grant) + args = SimpleNamespace(prepare=mutation != 'run_missing_digest', output=root, manifest=manifest, + trusted_manifest_sha256=a.b.digest(manifest), authorization=grant_path, + trusted_authorization_sha256='0' * 64 if mutation == 'bad_hash' else a.b.digest(grant_path), + storage_policy=tmp_path / 'policy', trusted_storage_policy_sha256='p') + if mutation is None: + _, actual, _ = a.admission(args, 10.) + assert actual == grant and not root.exists() + else: + with pytest.raises((ValueError, PermissionError)): + a.admission(args, 10.) + assert not root.exists() + + +def test_native_policy_exact_horizon_is_distinct_from_admission_expiry(tmp_path, monkeypatch): + root = tmp_path / 'new' + policy_path = tmp_path / 'policy.json' + monkeypatch.setattr(a.owner, 'mount_identity', lambda _: 'mount') + policy = {'format': a.POLICY_FORMAT, 'profile': a.PROFILE, 'artifact_root': str(root), 'limit_bytes': a.CAP, + 'native_paths': a.owner.native_paths(root), 'shared_native_paths': a.owner.shared_native_paths(), + 'task_owned_cache_paths': sorted([*a.redirects(root, 'prepare').values(), *a.redirects(root, 'run').values()]), + 'free_space_paths': a.owner.free_space_paths(root), + 'free_space_mount_identities': {p: 'mount' for p in a.owner.free_space_paths(root)}, + 'mount_identity': 'mount', 'check_interval_seconds': 1., 'max_native_growth_bytes': a.CAP, + 'min_free_bytes': 2 * a.CAP, 'coverage_gaps': list(a.owner.NATIVE_GAPS), 'native_hard_cap': False, + 'boot_id': a.b.boot_id(), 'issued_boottime': 10., 'not_after_boottime': 10810., 'lifetime_seconds': 10800} + args = SimpleNamespace(output=root, storage_policy=policy_path) + for duration in (10800, 3600, 10801): + policy.update(lifetime_seconds=duration, not_after_boottime=10. + duration) + write(policy_path, policy) + args.trusted_storage_policy_sha256 = a.b.digest(policy_path) + if duration == 10800: + assert a.policy_check(args, 10.) == policy + else: + with pytest.raises(PermissionError): + a.policy_check(args, 10.) + + +def test_fast_app_shutdown_cannot_mask_ledger_teardown_failure(): + api = a.RuntimeAPI.__new__(a.RuntimeAPI) + codes = [] + api.ledger = SimpleNamespace(__exit__=lambda: (_ for _ in ()).throw(OSError('ledger close'))) + api.launcher = SimpleNamespace(_app=SimpleNamespace(close=lambda **kw: codes.append(kw['exit_code']))) + with pytest.raises(OSError, match='ledger close'): + api.close_app(0) + assert codes == [1] + + +def test_supervisor_final_candidate_commit_failure_removes_lock(supervisor, monkeypatch): + args, child, monitor, capture, owned = supervisor + args.prepare = False + args.output.mkdir() + write(args.output / 'manifest.json', {}) + write(args.output / 'training-binding.json', {}) + (args.output / 'checkpoint_u0064.pt').write_bytes(b'forensic') + monkeypatch.setattr(a, 'validate_completion', lambda *_: None) + def exchange(*_): + write(args.output / 'run-worker-result.json', {'status': 'completed_provisional', + 'training_complete': True, 'postclose_identity_verified': True}) + monkeypatch.setattr(a.owner, 'exchange_worker', exchange) + def fail_commit(root, *_): + (root / 'locked_candidate.json').write_bytes(b'failed durable commit') + raise OSError('candidate commit failure') + monkeypatch.setattr(a, 'commit_candidate', fail_commit) + with pytest.raises(OSError, match='candidate commit failure'): + a.supervise(args) + assert not (args.output / 'locked_candidate.json').exists() + assert (args.output / 'checkpoint_u0064.pt').read_bytes() == b'forensic' + assert a.read(args.output / 'run-supervisor.json')['success'] is False + + +def test_worker_fd_rejects_non_supervisor_peer_before_handoff(): + parent, child = a.socket.socketpair() + try: + with pytest.raises(PermissionError, match='Authenticated RootXY supervisor'): + a.run_worker(child.detach()) + finally: + parent.close() + child.close() + + +@pytest.mark.parametrize('fault', [None, 'pending', 'safety_coverage', 'physics_partial', 'startup_cap', 'baseline']) +def test_independent_completion_debits_and_all_tick_safety(tmp_path, monkeypatch, fault): + data = {'reserved_transitions': 1048576, 'reservations': 256, 'pending': None, 'updates': {'92511': 256}, + 'validation_reserved_transitions': 806400, 'validation_attempted_control_calls': 5400, + 'validation_completed_control_calls': 5400, + 'validation_evaluations_started': ['analytic_zero', 'frozen_u256', 'u0064', 'u0128', 'u0192', 'u0256'], + 'clock': {'start_s': 10., 'deadline_s': 7210., 'boot_id': a.b.boot_id()}} + safety = {'physics_samples': 67960, 'finite': True, 'max_effort_velocity_ratio': 0., + 'max_joint_excess_rad': 0., 'max_guide_vertical_N': 0.} + physics = {'attempted_shared_calls': 67962, 'completed_shared_calls': 67962, + 'initialization_attempted_shared_calls': 2, 'rollout_attempted_shared_calls': 67960, + 'startup_completed_without_task_audit': 2, 'safety': safety} + rows = eligible_rows() + if fault == 'pending': + data['pending'] = {'update': 256, 'seed': 92511} + elif fault == 'safety_coverage': + safety['physics_samples'] -= 1 + elif fault == 'physics_partial': + physics['completed_shared_calls'] -= 1 + elif fault == 'startup_cap': + physics['initialization_attempted_shared_calls'] = 1001 + elif fault == 'baseline': + rows[0]['success'] = False + write(tmp_path / 'budget.json', data) + for name in ('analytic_zero', 'frozen_u256'): + write(tmp_path / ('validation_' + name + '.json'), {'rows': rows}) + result = {'budget': data, 'physics': physics} + manifest = {'training': {'safety_config': 'configs/contact_cycle/acceptance_v1.json'}} + if fault is None: + a.validate_completion(tmp_path, result, manifest, 10.) + else: + with pytest.raises(ValueError): + a.validate_completion(tmp_path, result, manifest, 10.) diff --git a/tests/test_contact_cycle_sim_rootxy_v2.py b/tests/test_contact_cycle_sim_rootxy_v2.py new file mode 100644 index 0000000..2fe6b0c --- /dev/null +++ b/tests/test_contact_cycle_sim_rootxy_v2.py @@ -0,0 +1,574 @@ +"""CPU-only RootXY-v2 contracts. Synthetic data except explicitly saved46D parity. + +No Kit, environment, actual pilot optimizer step, preparation or physical grant. +""" +import copy +import hashlib +import importlib.util +import json +import random +import sys +from pathlib import Path + +import pytest +import torch + +from SkillFactory.legs.contact_cycle_sim import rootxy_training_v2 as p +from SkillFactory.legs.contact_cycle_sim.config import combine, load_config, pose_cost +from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic +from SkillFactory.legs.contact_cycle_sim.ppo import restore_checkpoint +from SkillFactory.legs.contact_cycle_sim.rootxy_diagnostics_v2 import RootXYMetrics + +ROOT = Path(__file__).resolve().parents[1] + + +@pytest.fixture(autouse=True) +def threads(): + old = torch.get_num_threads() + torch.set_num_threads(1) + yield + torch.set_num_threads(old) + + +def test_inherited_guard_retries_only_disappearing_files_without_skipping_checks(monkeypatch): + ledger = object.__new__(p.RootXYBudgetLedger) + calls = [] + def guard(self, reserve_bytes=0): + calls.append(reserve_bytes) + if len(calls) < 3: + raise FileNotFoundError('heartbeat atomic rename') + return 'checked' + monkeypatch.setattr(p.v1.FreshBudgetLedger, 'guard', guard) + assert ledger.guard(123) == 'checked' + assert calls == [123, 123, 123] + calls.clear() + def unstable(self, reserve_bytes=0): + calls.append(reserve_bytes) + raise FileNotFoundError('persistent churn') + monkeypatch.setattr(p.v1.FreshBudgetLedger, 'guard', unstable) + with pytest.raises(p.BudgetExceeded, match='unstable'): + ledger.guard(456) + assert calls == [456] * 4 + def denied(self, reserve_bytes=0): + raise PermissionError('not a rename') + monkeypatch.setattr(p.v1.FreshBudgetLedger, 'guard', denied) + with pytest.raises(PermissionError): + ledger.guard() + + +def write(path, value): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(value)) + return path + + +def test_exact_config_reward_and_old_identity_rejection(): + cfg = p.load_training_config(ROOT / p.CONFIG_PATH, ROOT) + task = p.load_task_config(ROOT / p.TASK_PATH, ROOT) + assert cfg == p.PILOT and task['task_id'] == p.TASK_ID + assert cfg['num_envs'] * cfg['horizon'] * cfg['updates_per_seed'] == 1048576 + assert cfg['max_validation_transitions'] == 128 * 900 * 7 == 806400 + assert cfg['max_total_wall_s'] == 7200 and cfg['cleanup_reserve_s'] == 120 + with pytest.raises(ValueError): + load_config(ROOT / p.TASK_PATH) + with pytest.raises(ValueError): + p.v1.load_training_config(ROOT / p.CONFIG_PATH, ROOT) + old = load_config(ROOT / p.BASE_PATH) + expected = copy.deepcopy(old) + expected['reward']['position_scale_m'] = [.03, .03, .04] + for field in ('task_id', 'format', 'scope'): + expected[field] = task[field] + assert task == expected + error = torch.tensor([[.03, 0, .04], [0, 0, 0], [1, 1, 1]], dtype=torch.float32) + new_cost, velocity = pose_cost(error, torch.zeros_like(error), task['reward']) + old_cost, _ = pose_cost(error, torch.zeros_like(error), old['reward']) + assert torch.allclose(new_cost, torch.tensor([2., 0., 10.])) + assert torch.allclose(old_cost, torch.tensor([1.36, 0., 10.])) + assert not velocity.any() + ref = torch.tensor([[1., -.5, -1.]]) + _, _, effective = combine(ref, torch.tensor([[1., 1., -1.]]), .05, .5) + assert torch.allclose(effective, torch.tensor([[0., .05, 0.]]), atol=1e-7) + + +@pytest.mark.parametrize('key,value', [('seeds', [91501]), ('holdout_seed', 93511), ('num_envs', 64), + ('max_validation_transitions', 806401), ('max_total_wall_s', 7201), + ('updates_per_seed', True), ('resume', True)]) +def test_scope_and_old_grants_rejected(tmp_path, key, value): + bad = copy.deepcopy(p.PILOT) + bad[key] = value + with pytest.raises(ValueError): + p.load_training_config(write(tmp_path / 'config.json', bad), ROOT) + with pytest.raises(PermissionError, match='INCOMPLETE'): + p.require_launch_admission({'format': p.v1.AUTHORIZATION_FORMAT, 'approved': True}) + + +def test_override_whitelist_and_base_mutation(tmp_path): + bad = copy.deepcopy(p.TASK) + bad['overrides']['residual_limit_rad'] = .1 + with pytest.raises(ValueError, match='whitelist'): + p.load_task_config(write(tmp_path / 'bad.json', bad), ROOT) + base = tmp_path / p.BASE_PATH + base.parent.mkdir(parents=True) + base.write_bytes((ROOT / p.BASE_PATH).read_bytes() + b' ') + with pytest.raises(ValueError, match='base config'): + p.load_task_config(ROOT / p.TASK_PATH, tmp_path) + + +def test_manifest_explicit_additive_identity_and_immutable_base(): + manifest = p.make_manifest(ROOT, ROOT / p.CONFIG_PATH) + assert manifest['task_id'] == p.TASK_ID and manifest['launch_integration_complete'] is True + assert manifest['runtime_qualified'] is False + assert manifest['sources'][p.BASE_PATH] == p.BASE_SHA256 + for suffix in ('rootxy_training_v2.py', 'rootxy_diagnostics_v2.py', 'rootxy_runner_v2.py', + 'rootxy_cfg_v2.py', 'rootxy_recording_v2.py', 'train_cycle_sim_rootxy_v2.py', + 'test_contact_cycle_sim_rootxy_v2.py'): + assert any(name.endswith(suffix) for name in manifest['sources']) + assert manifest['warmstart_source']['candidate_sha256'] == p.replay.CANDIDATE_SHA256 + + +def test_actual_frozen_saved46d_actor_parity_fresh_critic_Adam_RNG(): + trace = torch.load(ROOT / Path(p.replay.CANDIDATE).parent / 'validation_u0256_trace.pt', + map_location='cpu', weights_only=True) + first = next(iter(trace.values())) + x = first['actor_input'][::100, 0].contiguous() + assert x.shape[1] == 46 and x.shape[0] == 45 + model, optimizer, frozen, evidence = p.import_actor_weights_only(ROOT, x) + assert not optimizer.state + assert evidence['observations'] == 45 and not evidence['physical_repeat'] + assert evidence['source']['candidate_sha256'] == p.replay.CANDIDATE_SHA256 + assert evidence['source']['manifest_sha256'] == p.replay.MANIFEST_SHA256 + rng = torch.get_rng_state().clone() + with torch.random.fork_rng(devices=[]): + torch.manual_seed(92511) + expected = SimResidualActorCritic(**p.MODEL_CONFIG) + assert torch.equal(rng, torch.get_rng_state()) + for k, v in model.critic.state_dict().items(): + assert torch.equal(v, expected.critic.state_dict()[k]) + assert any(not torch.equal(v, frozen.critic.state_dict()[k]) for k, v in model.critic.state_dict().items()) + for name in evidence['imported_keys']: + assert torch.equal(model.state_dict()[name], frozen.state_dict()[name]) + assert random.random() == random.Random(92511).random() + with torch.no_grad(): + assert torch.equal(model.act(x, deterministic=True)[0], frozen.act(x, deterministic=True)[0]) + # Fixed46D feature offsets/scales remain v1, including root error indices32:35. + assert torch.equal(model.offset, frozen.offset) and torch.equal(model.scale, frozen.scale) + assert torch.equal(model.scale[32:35], torch.full((3,), .1)) + + +def tick(metrics, index, xy=.02, z=0., safe=True): + n = metrics.num_envs + metrics.tick(time=torch.full((n,), index * .002), position_error=torch.tensor([[xy, 0., z]]).repeat(n, 1), + phase=torch.full((n,), min(index // 750, 5), dtype=torch.long), + reference=torch.tensor([[1., 0., 0.]]).repeat(n, 1), + request=torch.ones(n, 3), effective_residual=torch.zeros(n, 3), + safety_ok=torch.full((n,), safe)) + + +def test_physics_latches_recovery_cap_authority_and_partial_ticks(): + m = RootXYMetrics(2) + tick(m, 1, .031, .2) + tick(m, 2, .001) + m.end_control(torch.tensor([True, False]), torch.tensor([False, False])) + tick(m, 3, .001) + rows = m.rows() + assert rows[0]['physics_samples'] == 2 and rows[1]['physics_samples'] == 3 + assert all(r['rootxy_breached'] and r['first_rootxy_breach_tick'] == 1 for r in rows) + assert not rows[0]['complete_clock'] and rows[0]['rms_scope'] == 'observed_prefix_only' + assert rows[0]['position_cap_fraction'] == .5 + assert rows[0]['reference_saturated_fraction'] == 1. + assert rows[0]['effective_authority_shortfall_max_rad'] == .05 + assert rows[0]['uncapped_xy_cost_mean'] > 0 + with pytest.raises(ValueError, match='Missing'): + RootXYMetrics(1).rows() + + +def good_rows(): + return [dict(env=e, success=True, physics_samples=4500, complete_clock=True, rootxy_rms_m=.01, + rootxy_max_m=.02, rootxy_breached=False, safety_ok=True, height_rms_m=.02, + swing_rms_m=.01, landing_error_m=.01) for e in range(128)] + + +def test_selection_fullphase_strict_gate_then_RMS_tracking_earliest(): + rows = good_rows() + key = p.candidate_key(rows, 64) + assert key < p.candidate_key(rows, 128) + improved = copy.deepcopy(rows) + for row in improved: + row['rootxy_rms_m'] = .009 + row['height_rms_m'] = .05 + assert p.candidate_key(improved, 256) < key + rows[6]['rootxy_max_m'] = .031 + rows[6]['rootxy_breached'] = True + rows[6]['episode_return'] = 1e9 + assert p.candidate_key(rows, 64) is None + rows[6]['rootxy_max_m'] = .02 # latching still disqualifies after recovery + assert p.candidate_key(rows, 64) is None + rows[6]['success'] = False + with pytest.raises(RuntimeError, match='regression'): + p.candidate_key(rows, 64) + + +@pytest.mark.parametrize('field,value', [('physics_samples', 4499), ('complete_clock', False), + ('rootxy_rms_m', float('nan')), ('safety_ok', False), + ('rootxy_breached', None)]) +def test_selection_missing_partial_safety_fail_closed(field, value): + rows = good_rows() + rows[0][field] = value + with pytest.raises(ValueError): + p.candidate_key(rows, 64) + + +def test_budget_no_retry_separate_validation_and_cleanup_deadline(tmp_path): + clock = [20.] + manifest = {'synthetic_cpu_only': True, 'training': p.PILOT} + def ledger(): + return p.RootXYBudgetLedger(tmp_path, manifest, launch_start_s=10., now=lambda: 100., + boot_now=lambda: clock[0], boot_id='cpu-test') + with ledger() as budget: + assert budget.data['clock']['deadline_s'] == 7210 + assert budget.data['validation_reserved_transitions'] == 806400 + assert budget.data['reserved_transitions'] == 0 + budget.begin_validation('analytic_zero') + budget.before_validation_control() + budget.after_validation_control() + budget.reserve_rollout(92511, 1) + assert budget.data['reserved_transitions'] == 4096 + with pytest.raises(ValueError): + budget.begin_validation('analytic_zero') + with pytest.raises(ValueError): + budget.reserve_rollout(92511, 1) + with pytest.raises(PermissionError): + budget.reconcile_checkpoint(92511, 1) + clock[0] = 7090 + with pytest.raises(p.BudgetExceeded, match='Cleanup'): + budget.work_guard() + budget.guard() # cleanup publication remains inside7200s + with pytest.raises(FileExistsError): + with ledger(): + pass + persisted = json.loads((tmp_path / 'budget.json').read_text()) + assert persisted['reserved_transitions'] == 4096 and persisted['pending']['update'] == 1 + + +def checkpoint_inputs(): + return ({'task_id': p.TASK_ID, 'contract': p.CONTRACT, 'manifest_sha256': 'a' * 64, + 'resolved_task': p.load_task_config(ROOT / p.TASK_PATH, ROOT), + 'source_sha256': p.replay.CANDIDATE_SHA256, 'seed': 92511}, + {'source': {'candidate_sha256': p.replay.CANDIDATE_SHA256, + 'manifest_sha256': p.replay.MANIFEST_SHA256}, + 'fresh_seed': 92511, 'parity': 'exact_CPU_action_mean_std'}) + + +def test_v2_checkpoint_eval_only_atomic_budget_old_restore_rejects(tmp_path): + model = SimResidualActorCritic(**p.MODEL_CONFIG) + identity, imported = checkpoint_inputs() + path = tmp_path / 'v2.pt' + with pytest.raises(p.BudgetExceeded): + p.save_checkpoint(path, model, identity, 64, imported, {}, 1) + assert not list(tmp_path.iterdir()) + sha = p.save_checkpoint(path, model, identity, 64, imported, {}, 1048576) + rng = torch.get_rng_state().clone() + restored, data = p.load_evaluation_policy(path, sha, identity) + assert torch.equal(rng, torch.get_rng_state()) + assert 'optimizer' not in data and 'rng' not in data and data['resume'] is False + assert all(torch.equal(v, restored.state_dict()[k]) for k, v in model.state_dict().items()) + with pytest.raises(ValueError): + restore_checkpoint(path, model, torch.optim.Adam(model.parameters()), identity, p.MODEL_CONFIG) + with pytest.raises(ValueError): + p.replay.load_frozen_policy(path, {}) + with pytest.raises(ValueError): + p.load_evaluation_policy(path, '0' * 64, identity) + with pytest.raises(FileExistsError): + p.save_checkpoint(path, model, identity, 64, imported, {}, 1048576) + bad = dict(identity, task_id=p.replay.TASK_ID) + with pytest.raises(ValueError): + p.load_evaluation_policy(path, sha, bad) + + +def test_cli_run_barrier_precedes_import_output_and_authorization(tmp_path, monkeypatch): + spec = importlib.util.spec_from_file_location('rootxy_cli_test', ROOT / 'scripts/train_cycle_sim_rootxy_v2.py') + cli = importlib.util.module_from_spec(spec) + spec.loader.exec_module(cli) + def forbidden(): + raise AssertionError('run must fail before Torch/protocol imports') + monkeypatch.setattr(cli, 'cpu_protocol', forbidden) + with pytest.raises(PermissionError, match='INCOMPLETE'): + cli.main(['--run', '--output', str(tmp_path / 'unused'), '--authorization', 'old_grant.json']) + assert not list(tmp_path.iterdir()) + with pytest.raises(SystemExit): + cli.main(['--check-only', '--output', str(tmp_path / 'unused')]) + + +def test_v1_frozen_hashes_unchanged(): + manifest, _ = p.replay.verify_bundle(ROOT) + assert p.digest(manifest) == p.replay.MANIFEST_SHA256 + assert hashlib.sha256((ROOT / p.replay.CANDIDATE).read_bytes()).hexdigest() == p.replay.CANDIDATE_SHA256 + + +def test_full_saved_episode_streaming_metrics_match_direct_reduction_and_compact_audit(): + # Retrospective tensor reduction only: no policy call or simulator step. + trace = torch.load(ROOT / Path(p.replay.CANDIDATE).parent / 'validation_u0256_trace.pt', + map_location='cpu', weights_only=True) + episode = next(iter(trace.values())) + metrics = RootXYMetrics(1) + for i in range(4500): + metrics.tick(time=episode['time'][i], position_error=episode['policy_snapshot'][i, :, 32:35], + phase=episode['phase'][i], reference=episode['reference_used_normalized'][i], + request=episode['residual_request_normalized'][i], + effective_residual=episode['effective_residual_rad'][i], + safety_ok=torch.ones(1, dtype=torch.bool)) + metrics.end_control(torch.zeros(1, dtype=torch.bool), torch.ones(1, dtype=torch.bool)) + row = metrics.rows()[0] + xy = episode['policy_snapshot'][:, 0, 32:34].double().square().sum(-1).sqrt() + assert row['complete_clock'] and row['rms_scope'] == 'full_episode' + assert row['rootxy_rms_m'] == pytest.approx(float(xy.square().mean().sqrt()), abs=1e-14) + assert row['rootxy_max_m'] == float(xy.max()) + assert row['rootxy_breached'] == bool((xy > .03).any()) + hits = (xy > .03).nonzero().flatten() + assert row['first_rootxy_breach_tick'] == (int(hits[0]) + 1 if len(hits) else None) + assert sum(row['phase_physics_samples']) == 4500 + with pytest.raises(ValueError, match='Extra'): + metrics.live.fill_(True) + tick(metrics, 4501) + spec = importlib.util.spec_from_file_location('rootxy_original_functional_audit', + ROOT / 'scripts/audit_contact_cycle_functional.py') + audit = importlib.util.module_from_spec(spec) + spec.loader.exec_module(audit) + cfg = json.loads((ROOT / p.PILOT['functional_config']).read_text()) + fields = {'time', 'phase', 'scenario', 'weight', 'normal', 'foot', 'target', 'gap', + 'signed_height_error', 'tilt', 'external', 'guide', 'auxiliary', 'joint_excess', + 'limit_ratio', 'contact_acquired', 'actor_metadata_available', 'control_action_available'} + assert audit.evaluate({k: episode[k] for k in fields}, cfg, 'learned_policy') == audit.evaluate( + episode, cfg, 'learned_policy') + + +def test_missing_tick_and_nonfinite_are_not_full_episode_evidence(): + metrics = RootXYMetrics(1) + tick(metrics, 1) + tick(metrics, 3) # missing tick2; cannot repair by recording a later sample + metrics.end_control(torch.zeros(1, dtype=torch.bool), torch.ones(1, dtype=torch.bool)) + assert not metrics.rows()[0]['complete_clock'] and not bool(metrics.clock_ok[0]) + with pytest.raises(ValueError, match='physics-tick'): + tick(RootXYMetrics(1), 1, float('nan')) + + +def test_control_adapter_guard_debit_and_failure_do_not_count_completed_physics(): + from SkillFactory.legs.contact_cycle_sim.rootxy_runner_v2 import _ControlledEnv, RuntimeHooks + calls = [] + class Env: + def step(self, action): + calls.append('env_step') + if action == 'fail': + raise RuntimeError('synthetic partial physics failure') + return action + hooks = RuntimeHooks(admit=lambda _: {}, guard=lambda: None, verify_identity=lambda: None, + before_control=lambda kind: calls.append('before_' + kind), + after_control=lambda kind: calls.append('after_' + kind), + progress=lambda _: None, finish=lambda _: None) + controlled = _ControlledEnv(Env(), hooks, lambda: calls.append('guard')) + assert controlled.step('ok') == 'ok' + assert calls == ['guard', 'before_optimization', 'env_step', 'after_optimization', 'guard'] + calls.clear() + with pytest.raises(RuntimeError, match='partial'): + controlled.step('fail') + assert calls == ['guard', 'before_optimization', 'env_step'] + + +@pytest.fixture +def execute_fixture(tmp_path, monkeypatch): + """Real execute control flow; synthetic env/recorder and no optimizer/physics.""" + from contextlib import nullcontext + from types import ModuleType, SimpleNamespace as NS + from SkillFactory.legs.contact_cycle_sim import rootxy_runner_v2 as runner + + state = NS(update=0, label=None, fault=None, finished=[], publications=[], selected=[]) + manifest = {'sources': {'CPU': True}, 'resolved_task': {'CPU': True}} + cfg = copy.deepcopy(p.PILOT) + functional = tmp_path / cfg['functional_config'] + write(functional, {'CPU': True}) + output = tmp_path / 'output'; output.mkdir() + monkeypatch.setattr(p, 'load_training_config', lambda *_: cfg) + monkeypatch.setattr(p, 'make_manifest', lambda *_: manifest) + monkeypatch.setattr(p, 'source_identity', lambda *_: manifest['sources']) + monkeypatch.setattr(p.v1, 'verify_snapshot', lambda *_: None) + model = torch.nn.Linear(1, 1) + monkeypatch.setattr(p, 'import_actor_weights_only', lambda *_: ( + model, NS(state={}), torch.nn.Linear(1, 1), {'CPU': True})) + monkeypatch.setattr(runner, 'features', lambda x: x) + monkeypatch.setattr(runner, 'isolated_validation_rng', lambda *_: nullcontext()) + monkeypatch.setattr(runner, 'inference_action', lambda *_: (torch.zeros(128, 3), None, None)) + monkeypatch.setattr(runner, 'save_trace_subset', lambda *_: None) + + def save(path, _model, _identity, update, *_): + path.write_bytes(str(update).encode()) + return hashlib.sha256(path.read_bytes()).hexdigest() + monkeypatch.setattr(p, 'save_checkpoint', save) + + def optimize(_env, _model, _optimizer, x, _algo, _seed, update, *_): + state.update = update + return x, 0, 0, {} + monkeypatch.setattr(runner, 'optimize_reserved_rollout', optimize) + + class Ledger: + data = {'reserved_transitions': 0} + def work_guard(self): + pass + def guard(self, **_): + pass + def write_json(self, path, value): + if path.name == 'result.json' and state.fault == 'publication' and value['training_complete']: + raise OSError('injected final publication') + write(path, value) + state.publications.append((path.name, copy.deepcopy(value))) + def append_json(self, *_): + pass + def begin_validation(self, label): + state.label = label + def before_validation_control(self): + pass + def after_validation_control(self): + pass + def commit_update(self, seed, update): + if state.fault == 'accounting' and update == 128: + raise RuntimeError('injected accounting') + + class Recorder: + def __init__(self, *_): + self.initial_pair = {'CPU': torch.zeros(1)} + self.metrics = NS(live=torch.ones(128, dtype=torch.bool)) + self.columns, self.stops = {}, [] + def __enter__(self): + return self + def __exit__(self, *_): + pass + def before_control(self, *_): + pass + def end_control(self, *_): + pass + def rows(self, _): + rows = good_rows() + if state.fault == 'validation' and state.label == 'u0128': + rows[0]['success'] = False + return rows + + task = 'SkillFactory.tasks.manager_based.contact_cycle_sim.' + for leaf, attributes in ( + ('rootxy_cfg_v2', {'verify_actual_config': lambda _: None}), + ('rootxy_recording_v2', {'RootXYRecorder': Recorder})): + module = ModuleType(task + leaf) + vars(module).update(attributes) + monkeypatch.setitem(sys.modules, task + leaf, module) + observation = {'policy': torch.zeros(128, 46)} + env = NS(device='cpu', num_envs=128, reset=lambda: (observation, {}), + step=lambda _: (observation, torch.zeros(128), torch.zeros(128, dtype=torch.bool), + torch.zeros(128, dtype=torch.bool), {})) + + def identity(): + if state.fault == 'identity' and state.update == 128: + raise RuntimeError('injected identity') + + def finish(result): + state.finished.append(result) # notification object, never an authoritative lock + if state.fault == 'finish': + raise RuntimeError('injected finish hook') + + hooks = runner.RuntimeHooks( + admit=lambda _: dict(manifest_sha256=p.digest(manifest), p2_review_approved=True, + sole_cleanup_owner=True, native_monitor_active=True), + guard=lambda: None, verify_identity=identity, before_control=lambda _: None, + after_control=lambda _: None, progress=lambda _: None, finish=finish) + state.run = lambda: runner.execute(env, tmp_path, output, manifest, Ledger(), [], hooks, lambda *_: {}) + state.output = output + return state + + +@pytest.mark.parametrize('fault', ['validation', 'identity', 'accounting', 'publication', 'finish']) +def test_execute_invalidates_eligible_u64_on_later_failure(execute_fixture, fault): + f = execute_fixture + f.fault = fault + with pytest.raises((RuntimeError, OSError)): + f.run() + assert (f.output / 'checkpoint_u0064.pt').exists() # forensic only + terminal = json.loads((f.output / 'result.json').read_text()) + for result in (terminal, *f.finished): + assert result['status'] == 'failed' + assert result['candidate'] is None and result['training_complete'] is False + assert result['checkpoints_selectable'] is False and 'outcome' not in result + assert not (f.output / 'locked_candidate.json').exists() + assert not list(f.output.glob('*lock*')) + + +def test_execute_success_is_only_provisional_until_external_owner_commits(execute_fixture): + f = execute_fixture + result = f.run() + assert result['training_complete'] and result['candidate']['update'] == 64 + assert result['outcome'] == 'provisional_candidate' and result['status'] == 'completed_provisional' + assert result['checkpoints_selectable'] is False and result['accepted_skill'] is False + assert 'supervisor waited-exit/postexit-identity/cleanup' in result['candidate_authority'] + assert not (f.output / 'locked_candidate.json').exists() + + +def test_actual_rootxy_recorder_restores_callback_and_rejects_missing_metadata(monkeypatch): + """Actual recorder enter/control/exit seams with CPU state; no scene physics.""" + from types import ModuleType, SimpleNamespace as NS + package = ModuleType('_rootxy_recorder_cpu') + package.__path__ = [str(ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim')] + monkeypatch.setitem(sys.modules, package.__name__, package) + z = torch.zeros(1, 3) + state = NS(bundle={'joints': [{'limits': {'lower': -1, 'upper': 1, 'velocity': 1}}] * 3}, + policy=lambda: torch.zeros(1, 46), root=lambda: (z,), q=lambda: z, + port_reference=lambda: (z,), reference=lambda: (None, None, z), + controller=NS(action=z), contact=lambda: torch.zeros(1), foot_center=lambda: z) + mdp = ModuleType(package.__name__ + '.mdp'); mdp.state = lambda _: state; mdp.base = NS() + monkeypatch.setitem(sys.modules, mdp.__name__, mdp) + spec = importlib.util.spec_from_file_location(package.__name__ + '.rootxy_recording_v2', + Path(package.__path__[0]) / 'rootxy_recording_v2.py') + module = importlib.util.module_from_spec(spec); spec.loader.exec_module(module) + calls = [] + callback = lambda dt: calls.append(dt) + env = NS(num_envs=1, device='cpu', scene=NS(update=callback)) + with pytest.raises(ValueError, match='missing control metadata'): + with module.RootXYRecorder(env, {}, False) as recorder: + assert recorder.initial_pair['policy'].shape == (1, 46) + env.scene.update(.002) + assert env.scene.update is callback and calls == [.002] + with module.RootXYRecorder(env, {}, False) as recorder: + recorder.before_control(torch.zeros(1, 46), z, None, None) + assert recorder.action_available + with pytest.raises(ValueError, match='zero residual'): + recorder.before_control(torch.zeros(1, 46), torch.ones(1, 3), None, None) + assert env.scene.update is callback + + +def test_actual_rootxy_config_construction_uses_base_then_only_reviewed_override(monkeypatch): + """Actual additive config code over a minimal CPU base, not installed Isaac cfg qualification.""" + from types import ModuleType, SimpleNamespace as NS + directory = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim' + package = ModuleType('_rootxy_cfg_cpu'); package.__path__ = [str(directory)] + monkeypatch.setitem(sys.modules, package.__name__, package) + calls = [] + + class Base: + def __init__(self): + self.__post_init__() + def __post_init__(self): + calls.append('unchanged_base') + self.residual_parameters = {'base': True} + self.actions = NS(joint_position=NS(class_type=action)) + action = type('OriginalAction', (), {}) + base = ModuleType(package.__name__ + '.cfg'); base.SimContactCycleEnvCfg = Base + mdp = ModuleType(package.__name__ + '.mdp'); mdp.SimResidualAction = action + decorator = ModuleType('isaaclab.utils.configclass'); decorator.configclass = lambda cls: cls + for name, module in ((base.__name__, base), (mdp.__name__, mdp), (decorator.__name__, decorator)): + monkeypatch.setitem(sys.modules, name, module) + spec = importlib.util.spec_from_file_location(package.__name__ + '.rootxy_cfg_v2', directory / 'rootxy_cfg_v2.py') + module = importlib.util.module_from_spec(spec); spec.loader.exec_module(module) + cfg = module.RootXYContactCycleEnvCfg() + assert calls == ['unchanged_base'] + assert cfg.residual_parameters == p.load_task_config(ROOT / p.TASK_PATH, ROOT) + assert cfg.actions.joint_position.class_type is action + assert cfg.task_identity == p.TASK_ID and cfg.residual_config == str(ROOT / p.BASE_PATH) + monkeypatch.setattr(module.RootXYContactCycleEnvCfg, 'task_identity', 'old-or-forged') + with pytest.raises(ValueError, match='pinned v1 base'): + module.RootXYContactCycleEnvCfg() + assert calls == ['unchanged_base'] # rejected before base construction diff --git a/tests/test_contact_cycle_sim_stress.py b/tests/test_contact_cycle_sim_stress.py new file mode 100644 index 0000000..2f1ea41 --- /dev/null +++ b/tests/test_contact_cycle_sim_stress.py @@ -0,0 +1,193 @@ +"""Source-named pure CPU tests for the nominal scripted residual harness.""" +import importlib.util +import json +from pathlib import Path + +import pytest +import torch +from SkillFactory.legs.contact_cycle_sim.stress import first_failure, gate_residual, residual_signals + +ROOT = Path(__file__).resolve().parents[1] +PLAN = json.loads((ROOT / 'configs/contact_cycle/simulation_stress_v1.json').read_text()) + + +@pytest.mark.parametrize('case', PLAN['cases'], ids=lambda c: c['id']) +def test_residual_signals_range_and_reproducibility(case): + a, b = residual_signals(PLAN, case), residual_signals(PLAN, case) + assert a.shape == (900, 8, 3) and a.dtype == torch.float32 + assert torch.equal(a, b) and torch.isfinite(a).all() and a.abs().max() <= 1 + if case['kind'] != 'gaussian': + assert float(a.abs().max()) * .05 <= case['amplitude_rad'] + 1e-8 + + +def test_constant_signs_cover_all_eight_combinations(): + x = residual_signals(PLAN, {'kind': 'constant', 'amplitude_rad': .025}) + assert len(x[0].unique(dim=0)) == 8 + assert set(x.flatten().tolist()) == {-.5, .5} + assert torch.equal(x[0], x[-1]) + + +def test_sine_explicit_phase_frequency_and_gating(): + x = residual_signals(PLAN, {'kind': 'sine', 'amplitude_rad': .025}) + assert x[0, 0, 0] == 0 + assert x[0, 2, 0] == .5 + assert abs(float(x[100, 0, 0]) + .5) < 1e-7 + phase = torch.tensor([0, 1, 2, 3, 4, 5, 2, 3]) + before = x[10].clone() + gated = gate_residual(x[10], phase, PLAN['gated_phases']) + assert not gated[[0, 1, 4, 5]].any() + assert torch.equal(gated[[2, 3, 6, 7]], before[[2, 3, 6, 7]]) + assert torch.equal(x[10], before) + + +def test_gaussian_declared_seed_zero_mean_distribution_not_recentering(): + a = residual_signals(PLAN, {'kind': 'gaussian', 'seed': 90321}) + b = residual_signals(PLAN, {'kind': 'gaussian', 'seed': 90322}) + assert not torch.equal(a, b) + assert abs(float(a.mean())) < .003 + assert .095 < float(a.std()) < .102 + generator = torch.Generator().manual_seed(90321) + assert torch.equal(a, (.1 * torch.randn((900, 8, 3), generator=generator)).tanh()) + + +@pytest.mark.parametrize('amplitude', [-.1, .051, float('nan')]) +def test_invalid_amplitude_rejected(amplitude): + with pytest.raises(ValueError): + residual_signals(PLAN, {'kind': 'sine', 'amplitude_rad': amplitude}) + + +def test_first_failure_reports_physics_onset_not_end_of_control(): + trace = {'failure_flags': torch.zeros(5, 1, 2, dtype=torch.bool), + 'time': torch.arange(1, 6).float()[:, None] * .002, + 'phase': torch.zeros(5, 1, dtype=torch.long), 'gap': torch.zeros(5, 1), + 'normal': torch.zeros(5, 1, 3), 'signed_height_error': torch.zeros(5, 1), + 'tilt': torch.zeros(5, 1)} + assert first_failure(trace, ['a', 'b']) is None + trace['failure_flags'][1, 0, 1] = True + trace['failure_flags'][4, 0, 0] = True + onset = first_failure(trace, ['a', 'b']) + assert onset['physics_tick'] == 2 and onset['time_s'] == pytest.approx(.004) + assert onset['causes'] == ['b'] + + +def load_runner(monkeypatch): + monkeypatch.syspath_prepend(str(ROOT / 'scripts')) + spec = importlib.util.spec_from_file_location( + 'verify_cycle_sim_stress', ROOT / 'scripts/verify_cycle_sim_stress.py') + runner = importlib.util.module_from_spec(spec) + spec.loader.exec_module(runner) + return runner + + +@pytest.mark.parametrize('fault', ['boot', 'expired', 'disk', 'contract']) +def test_window_guard_fails_closed(monkeypatch, tmp_path, fault): + runner = load_runner(monkeypatch) + window = {'boot_id': Path('/proc/sys/kernel/random/boot_id').read_text().strip(), + 'deadline_boottime': 1000, 'cleanup_margin_s': 120, 'max_new_artifact_bytes': 16 * 1024**2} + if fault == 'boot': + window['boot_id'] = 'other-boot' + if fault == 'disk': + window['max_new_artifact_bytes'] = 1 + path = tmp_path / 'window.json' + path.write_text(json.dumps(window)) + monkeypatch.setattr(runner, 'WINDOW', path) + monkeypatch.setattr(runner, 'WINDOW_ROOT', tmp_path) + monkeypatch.setattr(runner, 'WINDOW_SHA256', runner.sha256(path) if fault != 'contract' else 'bad') + monkeypatch.setattr(runner.time, 'clock_gettime', lambda _: 881 if fault == 'expired' else 100) + with pytest.raises(RuntimeError): + runner.WindowGuard().check(force_disk=True) + + +@pytest.mark.parametrize('existing', ['result.json', 'supervisor.json', 'baseline_before']) +def test_execution_claim_preserves_existing_evidence(monkeypatch, tmp_path, existing): + runner = load_runner(monkeypatch) + evidence = tmp_path / existing + if existing == 'baseline_before': + evidence.mkdir() + evidence = evidence / 'initial.pt' + evidence.write_bytes(b'immutable original evidence') + with pytest.raises(FileExistsError): + runner.claim_execution(tmp_path) + assert evidence.read_bytes() == b'immutable original evidence' + assert not (tmp_path / 'execution_started.json').exists() + + +def test_execution_claim_rejects_repeat_without_changing_marker(monkeypatch, tmp_path): + runner = load_runner(monkeypatch) + runner.claim_execution(tmp_path) + marker = tmp_path / 'execution_started.json' + original = marker.read_bytes() + assert json.loads(original)['pid'] > 0 + with pytest.raises(FileExistsError): + runner.claim_execution(tmp_path) + assert marker.read_bytes() == original + + +@pytest.mark.parametrize('report', [ + {'passed': True, 'status': 'completed', 'command': ['python', 'child']}, + {'passed': False, 'status': 'failed', 'command': ['python', 'child']}, + {'passed': False, 'status': 'running', 'command': ['python', 'child'], 'cleanup': {}}, +]) +def test_execution_claim_preserves_noninitial_supervisor_report(monkeypatch, tmp_path, report): + runner = load_runner(monkeypatch) + evidence = tmp_path / 'supervisor.json' + original = json.dumps(report).encode() + evidence.write_bytes(original) + with pytest.raises(FileExistsError): + runner.claim_execution(tmp_path) + assert evidence.read_bytes() == original and not (tmp_path / 'execution_started.json').exists() + + +@pytest.mark.parametrize('completed', [False, True]) +def test_internal_entry_claims_once_before_kit(monkeypatch, tmp_path, completed): + import builtins + import sys + from types import SimpleNamespace + + runner = load_runner(monkeypatch) + output = tmp_path / 'attempt' + output.mkdir() + plan = tmp_path / 'plan.json' + plan.write_text('{}') + registration = output / 'preregistration.json' + registration.write_text(json.dumps({'identity': {}, 'plan_sha256': runner.sha256(plan)})) + (output / 'launch.json').write_text(json.dumps({'preregistration_sha256': runner.sha256(registration)})) + result = output / 'result.json' + if completed: + result.write_bytes(b'original failed or successful result') + supervisor = output / 'supervisor.json' + running = json.dumps({'passed': False, 'status': 'running', 'command': ['python', 'child']}).encode() + supervisor.write_bytes(running) + monkeypatch.setattr(runner, 'OUTPUT_ROOT', tmp_path) + monkeypatch.setattr(runner, 'PLAN', plan) + monkeypatch.setattr(runner, 'WindowGuard', lambda: SimpleNamespace(check=lambda **kwargs: 1000)) + monkeypatch.setattr(sys, 'argv', ['verify_cycle_sim_stress.py', '--output', str(output), '--headless']) + original_import = builtins.__import__ + + def prohibit_kit(name, *args, **kwargs): + if name == 'isaaclab.app': + raise AssertionError('Repeated internal invocation reached Kit import') + return original_import(name, *args, **kwargs) + + monkeypatch.setattr(builtins, '__import__', prohibit_kit) + if completed: + with pytest.raises(FileExistsError): + runner.main() + assert result.read_bytes() == b'original failed or successful result' + else: + # A legitimate first launch reaches the sentinel, never the real Kit import. + with pytest.raises(AssertionError, match='reached Kit import'): + runner.main() + marker = output / 'execution_started.json' + original_marker = marker.read_bytes() + with pytest.raises(FileExistsError): + runner.main() + assert marker.read_bytes() == original_marker and not result.exists() + assert supervisor.read_bytes() == running + + +def test_plan_is_twelve_nominal_cases_with_baseline_bookends(): + assert len(PLAN['cases']) == 12 and not PLAN['omitted_cases'] + assert PLAN['cases'][0]['id'] == 'baseline_before' + assert PLAN['cases'][-1]['id'] == 'baseline_after' + assert PLAN['nominal_only'] and not PLAN['external_disturbance'] and PLAN['optimizer_steps'] == 0 diff --git a/tests/test_contact_cycle_sim_training.py b/tests/test_contact_cycle_sim_training.py new file mode 100644 index 0000000..a7e7447 --- /dev/null +++ b/tests/test_contact_cycle_sim_training.py @@ -0,0 +1,572 @@ +"""CPU/synthetic protocol tests. No saved/physical robot data is optimized here.""" + +import copy +import importlib.util +import json +import sys +from pathlib import Path + +import pytest +import torch +from SkillFactory.legs.contact_cycle_sim import training as protocol +from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic +from SkillFactory.legs.contact_cycle_sim.ppo import restore_checkpoint, save_checkpoint, selection_key + +ROOT = Path(__file__).resolve().parents[1] +CFG = ROOT / protocol.CONFIG_PATH + + +@pytest.fixture(autouse=True) +def threads(): + old = torch.get_num_threads() + torch.set_num_threads(1) + yield + torch.set_num_threads(old) + + +def write(path, value): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(value)) + return path + + +def test_exact_pilot_config_and_proposal(): + cfg = protocol.load_training_config(CFG, ROOT) + assert cfg == protocol.PILOT + assert cfg["num_envs"] * cfg["horizon"] * cfg["updates_per_seed"] == 1048576 + assert cfg["validation_updates"] == [0, 64, 128, 192, 256] + assert protocol.PROPOSAL["actual_training_launches"] == 1 + + +@pytest.mark.parametrize("key,value", [ + ("seeds", [91500]), ("num_envs", 8), ("horizon", 64), ("max_total_transitions", 1048577), + ("max_total_wall_s", 3601), ("max_artifact_bytes", 1073741825), ("validation_seed", 91501), + ("validation_updates", [0, 32, 256]), ("features", "policy32"), ("resume", False), + ("updates_per_seed", True), ("task_id", "SkillFactory-Go2FL-ContactCycle-v0"), +]) +def test_config_rejects_scope_changes(tmp_path, key, value): + cfg = copy.deepcopy(protocol.PILOT) + cfg[key] = value + with pytest.raises(ValueError): + protocol.load_training_config(write(tmp_path / "bad.json", cfg), ROOT) + + +@pytest.mark.parametrize("key,value", [("hidden", 32), ("initial_std", .2), ("adam_eps", 1e-8), + ("gamma", True), ("learning_rate", float("nan"))]) +def test_algorithm_is_fixed(tmp_path, key, value): + cfg = copy.deepcopy(protocol.PILOT) + cfg["algorithm"][key] = value + with pytest.raises(ValueError): + protocol.load_training_config(write(tmp_path / "bad.json", cfg), ROOT) + + +def test_changed_proposal_rejected(tmp_path): + proposal = {**protocol.PROPOSAL, "resume": True} + write(tmp_path / protocol.PROPOSAL_PATH, proposal) + with pytest.raises(ValueError, match="proposal"): + protocol.load_training_config(CFG, tmp_path) + + +def test_manifest_binds_new_seam_math_assets_auditor_and_parent_plan(): + m = protocol.make_manifest(ROOT, CFG) + assert m["features"] == "contact-cycle-sim-truth-residual-46-v1" + assert m["sampler"] is None and m["resume"] is False and m["accepted_skill"] is False + for name in ("source/SkillFactory/SkillFactory/legs/standing_ppo.py", + "source/SkillFactory/SkillFactory/legs/policy.py", + "source/SkillFactory/SkillFactory/legs/contact_cycle_sim/ppo.py", + "source/SkillFactory/SkillFactory/legs/contact_cycle_sim/training.py", + "scripts/train_cycle_sim_residual.py", "scripts/audit_contact_cycle_functional.py", + "scripts/verify_cycle_sim_learning_runtime.py", "scripts/supervise_sim.py", + "docs/contact_cycle_sim_training_plan.md", protocol.CONFIG_PATH, protocol.PROPOSAL_PATH): + assert name in m["sources"] + assert any(p.startswith("assets/generated/support/") for p in m["sources"]) + assert any(p.startswith("assets/generated/legs/") for p in m["sources"]) + + +def synthetic_grant(manifest): + # NOT a grant for any actual pilot: the bound manifest below is a synthetic CPU object. + return {"format": protocol.AUTHORIZATION_FORMAT, "approved": True, + "manifest_sha256": protocol.digest(manifest), "operator": "synthetic_cpu_test", + "authorization_reference": "synthetic_only_never_execute"} + + +@pytest.mark.parametrize("fault", ["missing", "old", "stale", "unapproved", "operator", "extra"]) +def test_grants_fail_closed(tmp_path, fault): + manifest = {"synthetic_cpu_only": True} + grant = synthetic_grant(manifest) + if fault == "old": + grant["format"] = "skillfactory.contact-cycle-authorization.v1" + elif fault == "stale": + grant["manifest_sha256"] = "0" * 64 + elif fault == "unapproved": + grant["approved"] = 1 + elif fault == "operator": + grant["operator"] = " " + elif fault == "extra": + grant["resume"] = True + path = None if fault == "missing" else write(tmp_path / "synthetic.json", grant) + with pytest.raises(PermissionError): + protocol.require_authorization(path, manifest) + + +def test_synthetic_bound_grant_only(tmp_path): + manifest = {"synthetic_cpu_only": True} + grant = synthetic_grant(manifest) + assert protocol.require_authorization(write(tmp_path / "synthetic.json", grant), manifest) == grant + + +def test_frozen_and_symlink_output_rejected(tmp_path): + with pytest.raises(ValueError, match="Frozen"): + protocol.writable_output(ROOT, ROOT / "releases/child") + (tmp_path / "link").symlink_to(ROOT / "releases", target_is_directory=True) + with pytest.raises(ValueError, match="Frozen"): + protocol.writable_output(ROOT, tmp_path / "link/child") + + +@pytest.fixture +def prepared(tmp_path, monkeypatch): + # Small source snapshot fixture; never prepare a live training manifest or grant. + source = tmp_path / "source_root" + source.mkdir() + (source / "input.txt").write_text("synthetic input") + import hashlib + + manifest = {"synthetic_cpu_only": True, "training": copy.deepcopy(protocol.PILOT), + "sources": {"input.txt": hashlib.sha256(b"synthetic input").hexdigest()}} + monkeypatch.setattr(protocol, "make_manifest", lambda *_: manifest) + output = tmp_path / "prepared" + protocol.prepare(source, source / "config", output) + return source, output, manifest + + +def test_preparation_not_training(prepared): + root, out, manifest = prepared + protocol.validate_prepared(root, out, manifest) + assert not (out / "budget.json").exists() + assert not (out / "launch_claim.json").exists() + assert not list(out.glob("*.pt")) + with pytest.raises(FileExistsError): + protocol.prepare(root, root / "config", out) + + +@pytest.mark.parametrize("fault", ["snapshot", "manifest", "budget", "lock", "claim", "old_checkpoint", "cap"]) +def test_prepared_must_be_fresh_immutable_and_bounded(prepared, fault): + root, out, manifest = prepared + if fault == "snapshot": + (out / "source_snapshot/input.txt").write_text("changed") + elif fault == "manifest": + write(out / "manifest.json", {}) + elif fault == "cap": + manifest["training"]["max_artifact_bytes"] = 1 + write(out / "manifest.json", manifest) + write(out / "preparation.json", {"prepared": True, "authorized": False, "trained": False, + "manifest_sha256": protocol.digest(manifest)}) + else: + name = {"budget": "budget.json", "lock": "budget.lock", "claim": "launch_claim.json", + "old_checkpoint": "old32.pt"}[fault] + (out / name).write_text("old") + with pytest.raises((ValueError, FileExistsError, protocol.BudgetExceeded)): + protocol.validate_prepared(root, out, manifest) + + +def small_manifest(): + # Fixed pilot batch/cap, simulated BOOTTIME; no actual sources/grant. + return {"synthetic_cpu_only": True, "training": copy.deepcopy(protocol.PILOT)} + + +def ledger_at(path, clock=None): + clock = clock if clock is not None else [10.] + path.mkdir(exist_ok=True) + return protocol.FreshBudgetLedger(path, small_manifest(), now=lambda: 100., boot_now=lambda: clock[0], + boot_id="synthetic-boot") + + +def test_reservation_is_durable_and_never_refunded(tmp_path): + with ledger_at(tmp_path) as ledger: + ledger.reserve_rollout(91501, 1) + disk = json.loads((tmp_path / "budget.json").read_text()) + assert disk["reserved_transitions"] == 4096 + assert disk["pending"] == {"seed": 91501, "update": 1} + with pytest.raises(PermissionError): + ledger.reconcile_checkpoint(91501, 0) + with pytest.raises(FileExistsError): + with ledger_at(tmp_path): + pass + assert json.loads((tmp_path / "budget.json").read_text())["reserved_transitions"] == 4096 + + +def test_checkpoint_before_commit_and_failure_keeps_pending(tmp_path): + with ledger_at(tmp_path) as ledger: + ledger.reserve_rollout(91501, 1) + def failed_save(): + raise OSError("synthetic full disk") + with pytest.raises(OSError): + protocol.checkpoint_then_commit(ledger, 91501, 1, failed_save) + assert ledger.data["pending"] == {"seed": 91501, "update": 1} + assert ledger.data["updates"]["91501"] == 0 + def save(): + assert ledger.data["updates"]["91501"] == 0 + (tmp_path / "synthetic_latest").write_text("atomic checkpoint proof") + return 23 + assert protocol.checkpoint_then_commit(ledger, 91501, 1, save) == 23 + assert ledger.data["pending"] is None and ledger.data["updates"]["91501"] == 1 + + +def test_boot_deadline_includes_validation_suspend_and_startup(tmp_path): + clock = [10.] + with ledger_at(tmp_path, clock) as ledger: + assert ledger.data["clock"]["deadline_s"] == 3610. + clock[0] = 3610. + with pytest.raises(protocol.BudgetExceeded): + ledger.reserve_rollout(91501, 1) + assert ledger.data["reserved_transitions"] == 0 + + +def test_all_256_reservations_reach_exact_cap(tmp_path): + with ledger_at(tmp_path) as ledger: + for update in range(1, 257): + ledger.reserve_rollout(91501, update) + ledger.commit_update(91501, update) + assert ledger.data["reserved_transitions"] == 1048576 + with pytest.raises(ValueError): + ledger.reserve_rollout(91501, 257) + + +def test_artifact_cap_counts_console_supervisor_and_atomic_temporary(tmp_path): + with ledger_at(tmp_path) as ledger: + (tmp_path / "console.log").write_bytes(b"x" * 11) + (tmp_path / "supervisor.json").write_bytes(b"x" * 17) + used = protocol.artifact_bytes(tmp_path) + ledger.cfg["max_artifact_bytes"] = used + 20 + ledger.guard(reserve_bytes=20) + with pytest.raises(protocol.BudgetExceeded): + ledger.guard(reserve_bytes=21) + + +def success(height=0.): + return {"success": True, "height_rms_m": height, "swing_rms_m": 0., "landing_error_m": 0.} + + +def test_selection_lexicographic_earliest_tie_and_never_u0(): + with pytest.raises(ValueError, match="never u0"): + protocol.choose_candidate(None, 0, [success()]) + best = {"update": 64, "selection_key": selection_key([success(.06)])} + assert not protocol.choose_candidate(best, 128, [success(.06)]) + assert protocol.choose_candidate(best, 128, [success(.03)]) + assert not protocol.choose_candidate(best, 128, [{"success": False}, success()]) + assert protocol.choose_candidate(None, 64, [{"success": False}]) # failed trained candidate still lockable + assert selection_key([success(), {"success": False}]) == (.5, 8.) + + +class SyntheticEnv: + num_envs = 128 + device = "cpu" + step_dt = .01 + + def __init__(self, ledger, malformed=False): + self.ledger, self.malformed, self.steps = ledger, malformed, 0 + + def step(self, action): + assert self.ledger.data["pending"] == {"seed": 91501, "update": 1} + self.steps += 1 + x = torch.zeros(128, 46) + x[:, 32] = self.steps * .001 + info = {} if self.malformed else {"cycle_step": { + "phase": torch.zeros(128, dtype=torch.long), "failure_flags": torch.zeros(128, 8, dtype=torch.bool)}} + return {"policy": x}, -action.square().sum(-1) * .01, torch.zeros(128, dtype=torch.bool), \ + torch.zeros(128, dtype=torch.bool), info + + +def test_real_optimizer_on_fresh_synthetic_data_only_after_diagnostics(tmp_path, monkeypatch): + model = SimResidualActorCritic(hidden=16) + initial = {k: v.detach().clone() for k, v in model.named_parameters()} + optimizer = torch.optim.Adam(model.parameters(), lr=.0003, eps=1e-5) + original = protocol.ppo_step + calls = [] + def checked_step(*args): + assert (tmp_path / "rollout_diagnostics.jsonl").is_file() + record = json.loads((tmp_path / "rollout_diagnostics.jsonl").read_text()) + assert record["missing_diagnostic_samples"] == 0 + assert sum(record["phase_control_samples"]) == 4096 + calls.append("optimizer") + return original(*args) + monkeypatch.setattr(protocol, "ppo_step", checked_step) + algo = {**protocol.PILOT["algorithm"], "horizon": 32} + with ledger_at(tmp_path) as ledger: + env = SyntheticEnv(ledger) + _, failures, timeouts, learning = protocol.optimize_reserved_rollout( + env, model, optimizer, torch.zeros(128, 46), algo, 91501, 1, ledger, ledger.guard, ledger.guard) + assert env.steps == 32 and failures == timeouts == 0 and learning["minibatches"] > 0 + assert calls == ["optimizer"] + assert ledger.data["pending"] is not None # no premature commit + delta = protocol.parameter_deltas(model, initial) + assert delta["actor"]["l2"] > 0 and delta["critic"]["l2"] > 0 and delta["log_std"]["l2"] > 0 + + +def test_missing_diagnostics_never_reach_optimizer(tmp_path, monkeypatch): + model = SimResidualActorCritic(hidden=16) + before = {k: v.clone() for k, v in model.state_dict().items()} + def forbidden(*_): + pytest.fail("optimizer must never be called") + monkeypatch.setattr(protocol, "ppo_step", forbidden) + with ledger_at(tmp_path) as ledger: + with pytest.raises(RuntimeError, match="diagnostics"): + protocol.optimize_reserved_rollout(SyntheticEnv(ledger, True), model, None, torch.zeros(128, 46), + {**protocol.PILOT["algorithm"], "horizon": 32}, 91501, 1, ledger, ledger.guard, ledger.guard) + assert ledger.data["reserved_transitions"] == 4096 and ledger.data["pending"] is not None + assert all(torch.equal(v, before[k]) for k, v in model.state_dict().items()) + + +def test_old32_checkpoint_rejected_without_mutation(tmp_path): + model = SimResidualActorCritic(hidden=16) + optimizer = torch.optim.Adam(model.parameters(), lr=.0003) + path = tmp_path / "synthetic.pt" + config = {"hidden": 16, "initial_std": .1} + save_checkpoint(path, model, optimizer, {"synthetic": True}, 0, None, {}, config) + raw = torch.load(path, weights_only=True) + raw["format"] = "skillfactory.contact-cycle-ppo-checkpoint.v1" + raw["features"] = "policy32" + torch.save(raw, path) + initial = {k: v.clone() for k, v in model.state_dict().items()} + with pytest.raises(ValueError): + restore_checkpoint(path, model, optimizer, {"synthetic": True}, config) + assert all(torch.equal(v, initial[k]) for k, v in model.state_dict().items()) + + +def synthetic_trace(): + n, b = 4, 2 + z = torch.zeros(n, b) + return {"actor_input": torch.zeros(n, b, 46), "actor_metadata_available": torch.ones(n, b, dtype=torch.bool), + "control_action_available": torch.ones(n, b, dtype=torch.bool), "actor_mean": torch.zeros(n, b, 3), + "requested_action": torch.zeros(n, b, 3), "reference_used_normalized": torch.zeros(n, b, 3), + "processed_target": torch.zeros(n, b, 3), "effective_residual_rad": torch.zeros(n, b, 3), + "scenario": torch.zeros(n, b, dtype=torch.long), "external": torch.zeros(n, b, 2, 6), + "phase": torch.full((n, b), 3, dtype=torch.long), "signed_height_error": z, + "foot": torch.zeros(n, b, 3), "target": torch.zeros(n, b, 3), + "would_fail": torch.zeros(n, b, dtype=torch.bool), "failure_flags": torch.zeros(n, b, 8, dtype=torch.bool)} + + +def synthetic_audit(episode, _, kind): + assert kind == "learned_policy" + return {"sim_functional_passed": True, "metrics": {"landing_error_m": 0.}} + + +def test_first_episode_slicing_no_autoreset_stitch_and_failure_cost(): + trace = synthetic_trace() + trace["signed_height_error"][2:, 0] = 100 # autoreset evidence must not improve/contaminate first episode + rows = protocol.validation_rows(trace, [2, 4], [True, False], torch.zeros(3), {}, synthetic_audit) + assert rows[0]["height_rms_m"] == 0 and not rows[0]["success"] + assert rows[0]["first_episode_samples"] == 2 and rows[1]["success"] + assert selection_key(rows) == (.5, 8.) + + +@pytest.mark.parametrize("fault", ["width32", "nan", "missing_metadata", "delta", "template", "reference", + "stochastic", "scenario", "external", "missing_stop"]) +def test_validation_required_invariants_fail_closed(fault): + trace = synthetic_trace() + stops = [4, 4] + if fault == "width32": + trace["actor_input"] = torch.zeros(4, 2, 32) + elif fault == "nan": + trace["actor_input"][0, 0, 0] = float("nan") + elif fault == "missing_metadata": + trace["actor_metadata_available"][0, 0] = False + elif fault == "delta": + trace["effective_residual_rad"][0, 0, 0] = .1 + elif fault == "template": + trace["processed_target"][0, 0, 0] = .6 + elif fault == "reference": + trace["reference_used_normalized"][0, 0, 0] = 1.1 + elif fault == "stochastic": + trace["requested_action"][0, 0, 0] = .1 + elif fault == "scenario": + trace["scenario"][0, 0] = 1 + elif fault == "external": + trace["external"][0, 0, 0, 0] = 1 + else: + stops[0] = None + with pytest.raises(RuntimeError): + protocol.validation_rows(trace, stops, [False, False], torch.zeros(3), {}, synthetic_audit) + + +def test_compact_trace_subset_clones_full_width_and_exact_guard(tmp_path): + trace = {"actor_input": torch.zeros(100, 128, 46)} + reserved = [] + size = protocol.save_trace_subset(tmp_path / "trace.pt", trace, [2], [3] * 128, reserved.append) + assert reserved == [size] and (tmp_path / "trace.pt").stat().st_size == size + loaded = torch.load(tmp_path / "trace.pt", weights_only=True)["2"]["actor_input"] + assert loaded.shape == (3, 1, 46) and loaded.untyped_storage().nbytes() == 3 * 46 * 4 + def reject(_): + raise protocol.BudgetExceeded("no capacity") + with pytest.raises(protocol.BudgetExceeded): + protocol.save_trace_subset(tmp_path / "rejected.pt", trace, [2], [3] * 128, reject) + assert not (tmp_path / "rejected.pt").exists() + + +def test_conflicting_sim_processes_and_ignore_shell_command_text(tmp_path): + for pid, command in ((123, b"/bin/bash\0-c\0kit scripts/train_cycle_sim_residual.py\0"), + (124, b"/usr/bin/python3\0scripts/supervise_sim.py\0--\0/isaaclab.sh\0-p\0" + b"scripts/verify_cycle_sim_learning_runtime.py\0"), + (126, b"/kit/python.sh\0scripts/verify_cycle_sim_learning_runtime.py\0"), + (127, b"/kit/python3\0-c\0from isaaclab.cli import cli; cli()\0-p\0" + b"scripts/verify_cycle_sim_learning_runtime.py\0")): + p = tmp_path / str(pid) + p.mkdir() + (p / "cmdline").write_bytes(command) + protocol.assert_no_conflicting_processes(tmp_path) + p = tmp_path / "125" + p.mkdir() + (p / "cmdline").write_bytes(b"/opt/kit/kit\0--headless\0") + with pytest.raises(RuntimeError, match="125"): + protocol.assert_no_conflicting_processes(tmp_path) + + +@pytest.fixture +def cli(monkeypatch): + monkeypatch.syspath_prepend(str(ROOT / "scripts")) + spec = importlib.util.spec_from_file_location("residual_training_cli", ROOT / "scripts/train_cycle_sim_residual.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + # A pre-Kit rejection must not silently import the simulator. + import builtins + original = builtins.__import__ + def no_kit(name, *args, **kwargs): + if name.startswith(("isaaclab", "omni", "isaacsim")): + pytest.fail("Kit imported before authorization/preparation gates") + return original(name, *args, **kwargs) + monkeypatch.setattr(builtins, "__import__", no_kit) + return module + + +@pytest.mark.parametrize("flag", ["--resume", "--checkpoint", "--warm-start"]) +def test_cli_has_no_resume_checkpoint_or_warmstart(cli, tmp_path, flag): + with pytest.raises(SystemExit) as error: + cli.main(["--train", "--output", str(tmp_path / "no-output"), flag]) + assert error.value.code == 2 and not (tmp_path / "no-output").exists() + + +def test_cli_old_grant_before_kit_budget(cli, tmp_path): + grant = synthetic_grant({"synthetic_cpu_only": True}) + grant["format"] = "skillfactory.contact-cycle-authorization.v1" + path = write(tmp_path / "old_synthetic_grant.json", grant) + out = tmp_path / "no-output" + with pytest.raises(PermissionError): + cli.main(["--train", "--output", str(out), "--authorization", str(path), "--headless"]) + assert not out.exists() + + +def test_cli_frozen_and_wrong_config_before_kit_budget(cli, tmp_path): + with pytest.raises(ValueError, match="Frozen"): + cli.main(["--prepare", "--output", str(ROOT / "releases/new")]) + cfg = copy.deepcopy(protocol.PILOT) + cfg["seeds"] = [1] + path = write(tmp_path / "bad.json", cfg) + with pytest.raises(ValueError): + cli.main(["--train", "--output", str(tmp_path / "no-output"), "--config", str(path)]) + assert not (tmp_path / "no-output").exists() + + +def test_cli_preparation_never_imports_kit(cli, tmp_path, monkeypatch): + calls = [] + monkeypatch.setattr(protocol, "prepare", lambda *args: calls.append(args)) + cli.main(["--prepare", "--output", str(tmp_path / "synthetic")]) + assert len(calls) == 1 + assert "isaaclab.app" not in sys.modules + + +@pytest.mark.parametrize("reset_fails", [False, True]) +def test_validation_rng_restores_all_streams_even_on_reset_error(reset_fails): + import random + + import numpy as np + + calls = [] + class Env: + def seed(self, seed): + calls.append(("seed", seed)) + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + def reset(self): + calls.append(("reset",)) + if reset_fails: + raise RuntimeError("synthetic reset failure") + torch_before, python_before, numpy_before = torch.get_rng_state(), random.getstate(), np.random.get_state() + try: + with protocol.isolated_validation_rng(Env(), 93501, 91501): + torch.rand(5) + np.random.random(5) + random.random() + except RuntimeError: + assert reset_fails + assert calls == [("seed", 93501), ("seed", 91501), ("reset",)] + assert torch.equal(torch_before, torch.get_rng_state()) and python_before == random.getstate() + after = np.random.get_state() + assert numpy_before[0] == after[0] and np.array_equal(numpy_before[1], after[1]) + assert numpy_before[2:] == after[2:] + + +def test_snapshot_is_checked_during_execution_not_only_preparation(prepared): + _, out, manifest = prepared + protocol.verify_snapshot(out, manifest) + (out / "source_snapshot/input.txt").write_text("changed after startup") + with pytest.raises(ValueError, match="snapshot"): + protocol.verify_snapshot(out, manifest) + + +def test_u0_failure_aborts_before_any_reservation_or_optimizer(cli, tmp_path, monkeypatch): + # Synthetic recorder/environment exercise runner ordering, never import the simulator. + from types import SimpleNamespace + + state = SimpleNamespace(robot=SimpleNamespace(is_fixed_base=False), scenarios=torch.zeros(128, dtype=torch.long), + stand_only=torch.zeros(128, dtype=torch.bool), nominal=torch.zeros(3)) + class Env: + cfg = SimpleNamespace(sim=SimpleNamespace(physics=SimpleNamespace( + solver_type=0, enable_external_forces_every_iteration=False)), + terminations=SimpleNamespace(failed=object()), episode_length_s=9) + num_envs, device, physics_dt, step_dt = 128, "cpu", .002, .01 + def seed(self, seed): + torch.manual_seed(seed) + def reset(self): + self.steps = 0 + return {"policy": torch.zeros(128, 46)}, {} + def step(self, _): + self.steps += 1 + return ({"policy": torch.zeros(128, 46)}, torch.zeros(128), torch.zeros(128, dtype=torch.bool), + torch.full((128,), self.steps == 900, dtype=torch.bool), {}) + class Recorder: + def __init__(self, _): + self.frames, self.stops, self.early = [], [None] * 128, [False] * 128 + self.initial_state = {"actor_input": torch.zeros(128, 46)} + def __enter__(self): + return self + def __exit__(self, *_): + pass + def before_control(self, x, *_): + self.frames.append({"actor_input": x.clone()}) + def end_control(self, _, truncated, final): + if final: + assert truncated.all() + self.stops = [len(self.frames)] * 128 + monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle_sim.mdp", + SimpleNamespace(state=lambda _: state)) + monkeypatch.setitem(sys.modules, "SkillFactory.tasks.manager_based.contact_cycle_sim.recording", + SimpleNamespace(SimCycleRecorder=Recorder)) + monkeypatch.setattr(protocol, "source_identity", lambda *_: {}) + monkeypatch.setattr(protocol, "validation_rows", lambda *_: [{"env": e, "success": False} for e in range(128)]) + def forbidden(*_): + pytest.fail("u0 failure must abort before first rollout/optimizer") + monkeypatch.setattr(protocol, "optimize_reserved_rollout", forbidden) + (tmp_path / "console.log").touch() + args = SimpleNamespace(output=tmp_path, config=CFG, console_log=tmp_path / "console.log") + manifest = {**small_manifest(), "sources": {}} + with protocol.FreshBudgetLedger(tmp_path, manifest, now=lambda: 100., boot_now=lambda: 10., + boot_id="synthetic") as ledger: + with pytest.raises(RuntimeError, match="u0 did not pass"): + cli.execute(Env(), protocol.PILOT, args, manifest, ledger, ledger.guard) + assert ledger.data["reserved_transitions"] == 0 and ledger.data["updates"]["91501"] == 0 + baseline = json.loads((tmp_path / "validation_u0000.json").read_text()) + assert baseline["kind"] == "untrained_zero_residual_network" and not baseline["trained"] + assert len(baseline["rows"]) == 128 + assert (tmp_path / "untrained_u0000.pt").exists() and not (tmp_path / "latest.pt").exists() + assert not (tmp_path / "locked_candidate.json").exists() diff --git a/tests/test_contact_cycle_sim_worker_bootstrap.py b/tests/test_contact_cycle_sim_worker_bootstrap.py new file mode 100644 index 0000000..8c4df06 --- /dev/null +++ b/tests/test_contact_cycle_sim_worker_bootstrap.py @@ -0,0 +1,75 @@ +"""Fresh-interpreter worker bootstrap regression. No Kit, policy or physics.""" +import subprocess +import sys +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[1] + + +@pytest.mark.parametrize('protocol_index,expected_rows', [(0, 2), (1, 20), (2, 20)]) +@pytest.mark.parametrize('reproduce_legacy', [False, True]) +def test_worker_bootstrap_has_no_private_protocol_alias( + tmp_path, protocol_index, expected_rows, reproduce_legacy): + # A separate interpreter matters: the larger runner test module intentionally + # imports CPU planning aliases during collection, hiding runtime pollution. + code = r''' +import importlib.util, json, sys +from pathlib import Path +root, output, index, count = Path(sys.argv[1]), Path(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]) +spec = importlib.util.spec_from_file_location('bootstrap_runner', root / 'scripts/evaluate_cycle_sim_residual.py') +r = importlib.util.module_from_spec(spec) +sys.modules[spec.name] = r +spec.loader.exec_module(r) +b = r.cpu_module('evaluation_budget') +assert '_evaluation_cpu_evaluation_protocol' not in sys.modules +assert r.protocol_stage(r.PROTOCOLS[index]) == ('P1' if index == 0 else 'P2') +try: + r.protocol_stage('unregistered-protocol.json') +except ValueError: + pass +else: + raise AssertionError('unknown protocol admitted') +row = {'index': 0, 'status': 'reserved', 'reserved_physical_calls': 91000, + 'rollout_cap': 90000, 'initialization_cap': 1000, + 'started_boottime': b.boottime(), 'deadline_boottime': b.boottime() + 600, + 'grant': {'boot_id': b.boot_id(), 'trial_indices': [0]}} +(output / 'launch-0').mkdir() +(output / 'manifest.json').write_text('{}') +(output / 'inventory.json').write_text('{}') +row['grant']['inventory_sha256'] = b.digest(output / 'inventory.json') +b.durable_write(output / 'budget.json', b.encode({'launches': [row]})) +payload = {'launch_identity': r.launch_identity(), 'reservation': row, 'arguments': { + 'artifact_root': str(output), 'inventory': str(output / 'inventory.json'), + 'manifest': str(output / 'manifest.json'), + 'trusted_manifest_sha256': b.digest(output / 'manifest.json'), 'protocol': r.PROTOCOLS[index]}} +entered = [] +def stop_before_kit(*args): + assert '_evaluation_cpu_evaluation_protocol' not in sys.modules, 'private planning alias leaked' + entered.append(True) + raise RuntimeError('CPU boundary reached without planning imports; no Kit authorized') +r.real_runtime = stop_before_kit +assert r.run_worker(payload) == 1 +result = json.loads((output / 'launch-0/result.json').read_text()) +assert entered == [True], result['failures'] +assert len(result['rows']) == count +assert all(row['status'] == 'not_run' for row in result['rows']) +assert result['observed_counts'] is None and result['optimizer_steps'] == 0 +assert not result['continuation_eligible'] and not result['accepted_skill'] +assert result['failures'][0]['context']['message'].startswith('CPU boundary reached') +assert '_evaluation_cpu_evaluation_protocol' not in sys.modules +assert not any(name.startswith(('isaaclab', 'omni.')) for name in sys.modules) +''' + if reproduce_legacy: + # Reproduce the original defect only in the isolated CPU child. This + # proves the test detects it without rolling back production sources. + code = code.replace("row = {'index': 0", "r.protocol_stage = lambda p: r.cpu_module('evaluation_protocol').load_protocol(root / p).document['stage']\nrow = {'index': 0", 1) + completed = subprocess.run( + [sys.executable, '-c', code, str(ROOT), str(tmp_path), str(protocol_index), str(expected_rows)], + capture_output=True, text=True, timeout=30) + if reproduce_legacy: + assert completed.returncode != 0 + assert 'private planning alias leaked' in completed.stderr + else: + assert completed.returncode == 0, completed.stdout + completed.stderr diff --git a/tests/test_standing_force_visualizer.py b/tests/test_standing_force_visualizer.py new file mode 100644 index 0000000..8012bb3 --- /dev/null +++ b/tests/test_standing_force_visualizer.py @@ -0,0 +1,117 @@ +"""No-Kit geometry and read-only applied-force visualization regressions.""" + +import sys +from pathlib import Path +from types import SimpleNamespace + +import pytest +import torch + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) +import standing_force_visualizer as viz + + +def rotated_z(q): + z = torch.zeros_like(q[:, :3]) + z[:, 2] = 1 + t = 2 * torch.cross(q[:, :3], z, dim=-1) + return z + q[:, 3:] * t + torch.cross(q[:, :3], t, dim=-1) + + +@pytest.mark.parametrize("dtype", [torch.float32, torch.float64]) +def test_arrow_world_tail_tip_and_xyzw_all_directions(dtype): + force = torch.tensor( + [ + [5, 0, 0], + [-5, 0, 0], + [0, 5, 0], + [0, -5, 0], + [0, 0, 10], + [0, 0, -10], + [2, -3, 4], + [0.001, -0.002, -10], + [1e-5, 0, 0], + ], + dtype=dtype, + ) + origin = torch.arange(len(force) * 3, dtype=dtype).reshape(-1, 3) / 10 + before = origin.clone(), force.clone() + g = viz.arrow_geometry(origin, force, 0.03) + n = len(force) + direction = force / force.norm(dim=-1, keepdim=True) + assert g["translations"].shape == (2 * n, 3) + assert torch.allclose(rotated_z(g["orientations"][:n]), direction, atol=2e-7) + assert torch.allclose(g["orientations"].norm(dim=-1), torch.ones(2 * n, dtype=dtype)) + assert torch.allclose(g["translations"][:n] - direction * g["scales"][:n, 2:] / 2, origin, atol=1e-6) + assert torch.allclose(g["translations"][n:] + direction * g["scales"][n:, 2:] / 2, origin + 0.03 * force, atol=1e-6) + assert g["marker_indices"].tolist() == [0] * n + [1] * n + assert torch.equal(origin, before[0]) and torch.equal(force, before[1]) + + +def test_zero_force_and_zero_batch_have_no_visible_geometry(): + for count in (0, 5): + g = viz.arrow_geometry(torch.zeros(count, 3), torch.zeros(count, 3)) + assert g["translations"].shape == (0, 3) + assert g["orientations"].shape == (0, 4) + assert all(torch.isfinite(v).all() for v in g.values()) + + +@pytest.mark.parametrize("scale", [0, -0.01, float("nan"), float("inf")]) +def test_bad_scale_rejected_before_kit_import(scale): + with pytest.raises(ValueError, match="scale"): + viz.DisturbanceForceArrows(scale) + with pytest.raises(ValueError, match="scale"): + viz.arrow_geometry(torch.zeros(1, 3), torch.ones(1, 3), scale) + + +@pytest.mark.parametrize( + "force", [torch.ones(1, 6), torch.ones(2, 3), torch.full((1, 3), float("nan")), torch.ones(1, 3).long()] +) +def test_bad_force_rejected(force): + with pytest.raises(ValueError): + viz.arrow_geometry(torch.zeros(1, 3), force) + + +def test_applied_disturbance_only_and_visibility_lifecycle(monkeypatch): + class Markers: + def __init__(self): + self.visible = True + self.frames = [] + + def set_visibility(self, value): + self.visible = value + + def visualize(self, **kwargs): + self.frames.append(kwargs) + + markers = Markers() + monkeypatch.setattr(viz, "_make_markers", lambda: markers) + arrows = viz.DisturbanceForceArrows() + assert not markers.visible + position = torch.tensor([[12.0, -3.0, 0.4], [-8.0, 4.0, 0.5]]) + # First environment is pure torque: must not be interpreted as a force. + wrench = torch.tensor([[0.0, 0.0, 0.0, 1.0, 2.0, 3.0], [0.0, 0.0, -10.0, 0.0, 0.0, 0.0]]) + state = SimpleNamespace( + root=lambda: (position,), + last_disturbance=wrench, + last_guide=torch.ones(2, 6) * 500, + mount_wrench=lambda: (_ for _ in ()).throw(AssertionError("Not sensor data")), + ) + before = position.clone(), wrench.clone(), state.last_guide.clone(), torch.get_rng_state().clone() + arrows.update(state) + assert markers.visible and len(markers.frames) == 1 + g = markers.frames[0] + assert len(g["translations"]) == 2 # one shaft + one head, not two arrows + assert torch.allclose( + g["translations"][1] + rotated_z(g["orientations"])[1] * g["scales"][1, 2] / 2, + position[1] + torch.tensor([0.0, 0.0, -0.3]), + ) + assert all(torch.equal(a, b) for a, b in zip(before, (position, wrench, state.last_guide, torch.get_rng_state()))) + wrench.zero_() + arrows.update(state) + assert not markers.visible and len(markers.frames) == 1 # no stale arrow + wrench[0, 0] = 5 + arrows.update(state) + assert markers.visible and len(markers.frames) == 2 + arrows.close() + assert not markers.visible diff --git a/tests/test_train_leg_skill.py b/tests/test_train_leg_skill.py new file mode 100644 index 0000000..b706c0f --- /dev/null +++ b/tests/test_train_leg_skill.py @@ -0,0 +1,56 @@ +"""Trainer CLI forwards visualization to workers without importing Kit.""" + +import importlib.util +import sys +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[1] + + +@pytest.fixture +def trainer(monkeypatch): + monkeypatch.syspath_prepend(str(ROOT / "scripts")) + spec = importlib.util.spec_from_file_location("train_leg_skill", ROOT / "scripts/train_leg_skill.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +@pytest.mark.parametrize("viz_args", [[], ["--viz", "kit"], ["--viz", "none"], ["--visualizer", "kit"]]) +def test_worker_visualization_forwarding(trainer, tmp_path, viz_args): + bundles = [tmp_path / "go2_fl", tmp_path / "go1_fl"] + args = trainer.parse_args( + [ + "--bundles", + *map(str, bundles), + "--num_envs", + "1", + "--horizon", + "32", + "--updates", + "1", + *viz_args, + ] + ) + for index, bundle in enumerate(args.bundles): + directory = tmp_path / "update_0000" / bundle.name + command = trainer.worker_command(args, bundle, tmp_path / "sampling_policy.pt", directory, args.seed + index) + assert command[0] == sys.executable + assert Path(command[1]).name == "leg_worker.py" + assert command[command.index("--bundle") + 1] == str(bundle.resolve()) + assert command[command.index("--output") + 1] == str(directory.resolve()) + assert command[command.index("--seed") + 1] == str(args.seed + index) + assert command[command.index("--device") + 1] == args.device + assert command[command.index("--mode") + 1] == "rollout" + if viz_args: + assert command[-2:] == ["--viz", viz_args[-1]] + else: + assert "--viz" not in command + + +def test_invalid_visualization_rejected(trainer): + with pytest.raises(SystemExit) as error: + trainer.parse_args(["--bundles", "asset", "--viz", "invalid"]) + assert error.value.code == 2