Release crosswire-formal-synthesis-2026-11-03 from GitHub 91e7ac87c4e4
Browse filesCROSSWIRE 0.1.0: Black-Box Interface Models, Benchmark Suite, Formal Papers, and Evidence Receipts.
This view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +1 -0
- .github/workflows/ci.yml +81 -0
- .gitignore +12 -0
- CODE_OF_CONDUCT.md +128 -0
- LICENSE +21 -0
- MANIFEST.in +11 -0
- README.md +185 -0
- Run-CROSSWIRE.ps1 +7 -0
- SECURITY.md +21 -0
- artifacts/crosswire_lab-0.1.0-py3-none-any.whl +3 -0
- artifacts/crosswire_lab-0.1.0.tar.gz +3 -0
- configs/budgeted-experiments.json +10 -0
- configs/budgeted-planning-controls.json +11 -0
- configs/budgeted-planning-pilot.json +13 -0
- configs/pilot-adaptive.json +11 -0
- configs/pilot-calibration-safe.json +14 -0
- configs/pilot-calibration-unrestricted.json +14 -0
- configs/pilot-cegp-longtrace.json +9 -0
- configs/pilot-escalation.json +12 -0
- configs/pilot-in-family.json +9 -0
- configs/pilot-independent-protocols.json +17 -0
- configs/pilot-replanning-ec2.json +12 -0
- configs/pilot-shield-openloop.json +9 -0
- configs/pilot-shield-shielded.json +10 -0
- configs/pilot-stress.json +9 -0
- configs/pilot-temporal.json +10 -0
- configs/resilience-workflows.json +15 -0
- configs/smoke.json +8 -0
- crosswire/__init__.py +3 -0
- crosswire/__main__.py +3 -0
- crosswire/adaptive.py +272 -0
- crosswire/audit.py +124 -0
- crosswire/backends.py +43 -0
- crosswire/baselines.py +137 -0
- crosswire/certificates.py +512 -0
- crosswire/cli.py +108 -0
- crosswire/conditions.py +57 -0
- crosswire/core.py +1130 -0
- crosswire/experiment_checks.py +217 -0
- crosswire/experiments.py +214 -0
- crosswire/fixtures.py +521 -0
- crosswire/identity.py +32 -0
- crosswire/interfaces.py +170 -0
- crosswire/receipts.py +93 -0
- crosswire/reliability.py +150 -0
- crosswire/server.py +274 -0
- crosswire/shield.py +496 -0
- crosswire/study.py +702 -0
- crosswire/target_worker.py +71 -0
- crosswire/web/app.js +368 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
artifacts/crosswire_lab-0.1.0-py3-none-any.whl filter=lfs diff=lfs merge=lfs -text
|
.github/workflows/ci.yml
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: CI
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches: [ main, codex/crosswire-design ]
|
| 6 |
+
pull_request:
|
| 7 |
+
branches: [ main, codex/crosswire-design ]
|
| 8 |
+
|
| 9 |
+
jobs:
|
| 10 |
+
test:
|
| 11 |
+
runs-on: ${{ matrix.os }}
|
| 12 |
+
strategy:
|
| 13 |
+
fail-fast: false
|
| 14 |
+
matrix:
|
| 15 |
+
os: [ubuntu-latest, windows-latest]
|
| 16 |
+
python-version: ["3.11", "3.12"]
|
| 17 |
+
|
| 18 |
+
steps:
|
| 19 |
+
- uses: actions/checkout@v4
|
| 20 |
+
|
| 21 |
+
- name: Set up Python ${{ matrix.python-version }}
|
| 22 |
+
uses: actions/setup-python@v5
|
| 23 |
+
with:
|
| 24 |
+
python-version: ${{ matrix.python-version }}
|
| 25 |
+
|
| 26 |
+
- name: Install dependencies
|
| 27 |
+
run: |
|
| 28 |
+
python -m pip install --upgrade pip
|
| 29 |
+
pip install -e ".[dev,baselines]"
|
| 30 |
+
|
| 31 |
+
- name: Run automated test suite
|
| 32 |
+
run: |
|
| 33 |
+
python -m pytest -q --tb=short
|
| 34 |
+
|
| 35 |
+
- name: Build wheel package
|
| 36 |
+
run: |
|
| 37 |
+
python -m build --wheel
|
| 38 |
+
|
| 39 |
+
- name: Verify package installation
|
| 40 |
+
run: |
|
| 41 |
+
python -I tools/check_distribution.py --wheel-dir dist
|
| 42 |
+
|
| 43 |
+
- name: Verify scientific receipt replay
|
| 44 |
+
run: |
|
| 45 |
+
python -m crosswire verify reports/budgeted-experiments-receipt.json
|
| 46 |
+
|
| 47 |
+
browser:
|
| 48 |
+
runs-on: ubuntu-latest
|
| 49 |
+
steps:
|
| 50 |
+
- uses: actions/checkout@v4
|
| 51 |
+
|
| 52 |
+
- name: Set up Python
|
| 53 |
+
uses: actions/setup-python@v5
|
| 54 |
+
with:
|
| 55 |
+
python-version: "3.12"
|
| 56 |
+
|
| 57 |
+
- name: Install Python dependencies
|
| 58 |
+
run: |
|
| 59 |
+
python -m pip install --upgrade pip
|
| 60 |
+
pip install -e ".[dev,baselines]"
|
| 61 |
+
|
| 62 |
+
- name: Set up Node.js
|
| 63 |
+
uses: actions/setup-node@v4
|
| 64 |
+
with:
|
| 65 |
+
node-version: 20
|
| 66 |
+
|
| 67 |
+
- name: Install Playwright and Chromium
|
| 68 |
+
run: |
|
| 69 |
+
npm install playwright
|
| 70 |
+
npx playwright install --with-deps chromium
|
| 71 |
+
|
| 72 |
+
- name: Run browser workbench end-to-end checks
|
| 73 |
+
run: |
|
| 74 |
+
node tools/check_workbench.cjs
|
| 75 |
+
|
| 76 |
+
- name: Upload Playwright artifacts on failure
|
| 77 |
+
if: failure()
|
| 78 |
+
uses: actions/upload-artifact@v4
|
| 79 |
+
with:
|
| 80 |
+
name: playwright-artifacts
|
| 81 |
+
path: output/playwright/
|
.gitignore
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.venv/
|
| 2 |
+
__pycache__/
|
| 3 |
+
.pytest_cache/
|
| 4 |
+
*.egg-info/
|
| 5 |
+
build/
|
| 6 |
+
dist/
|
| 7 |
+
.scratch/
|
| 8 |
+
output/
|
| 9 |
+
*.pyc
|
| 10 |
+
.coverage
|
| 11 |
+
node_modules/
|
| 12 |
+
.playwright-cli/
|
CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Contributor Covenant Code of Conduct
|
| 2 |
+
|
| 3 |
+
## Our Pledge
|
| 4 |
+
|
| 5 |
+
We as members, contributors, and leaders pledge to make participation in our
|
| 6 |
+
community a harassment-free experience for everyone, regardless of age, body
|
| 7 |
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
| 8 |
+
identity and expression, level of experience, education, socio-economic status,
|
| 9 |
+
nationality, personal appearance, race, religion, or sexual identity
|
| 10 |
+
and orientation.
|
| 11 |
+
|
| 12 |
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
| 13 |
+
diverse, inclusive, and healthy community.
|
| 14 |
+
|
| 15 |
+
## Our Standards
|
| 16 |
+
|
| 17 |
+
Examples of behavior that contributes to a positive environment for our
|
| 18 |
+
community include:
|
| 19 |
+
|
| 20 |
+
* Demonstrating empathy and kindness toward other people
|
| 21 |
+
* Being respectful of differing opinions, viewpoints, and experiences
|
| 22 |
+
* Giving and gracefully accepting constructive feedback
|
| 23 |
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
| 24 |
+
and learning from the experience
|
| 25 |
+
* Focusing on what is best not just for us as individuals, but for the
|
| 26 |
+
overall community
|
| 27 |
+
|
| 28 |
+
Examples of unacceptable behavior include:
|
| 29 |
+
|
| 30 |
+
* The use of sexualized language or imagery, and sexual attention or
|
| 31 |
+
advances of any kind
|
| 32 |
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
| 33 |
+
* Public or private harassment
|
| 34 |
+
* Publishing others' private information, such as a physical or email
|
| 35 |
+
address, without their explicit permission
|
| 36 |
+
* Other conduct which could reasonably be considered inappropriate in a
|
| 37 |
+
professional setting
|
| 38 |
+
|
| 39 |
+
## Enforcement Responsibilities
|
| 40 |
+
|
| 41 |
+
Community leaders are responsible for clarifying and enforcing our standards of
|
| 42 |
+
acceptable behavior and will take appropriate and fair corrective action in
|
| 43 |
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
| 44 |
+
or harmful.
|
| 45 |
+
|
| 46 |
+
Community leaders have the right and responsibility to remove, edit, or reject
|
| 47 |
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
| 48 |
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
| 49 |
+
decisions when appropriate.
|
| 50 |
+
|
| 51 |
+
## Scope
|
| 52 |
+
|
| 53 |
+
This Code of Conduct applies within all community spaces, and also applies when
|
| 54 |
+
an individual is officially representing the community in public spaces.
|
| 55 |
+
Examples of representing our community include using an official e-mail address,
|
| 56 |
+
posting via an official social media account, or acting as an appointed
|
| 57 |
+
representative at an online or offline event.
|
| 58 |
+
|
| 59 |
+
## Enforcement
|
| 60 |
+
|
| 61 |
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
| 62 |
+
reported to the community leaders responsible for enforcement at
|
| 63 |
+
.
|
| 64 |
+
All complaints will be reviewed and investigated promptly and fairly.
|
| 65 |
+
|
| 66 |
+
All community leaders are obligated to respect the privacy and security of the
|
| 67 |
+
reporter of any incident.
|
| 68 |
+
|
| 69 |
+
## Enforcement Guidelines
|
| 70 |
+
|
| 71 |
+
Community leaders will follow these Community Impact Guidelines in determining
|
| 72 |
+
the consequences for any action they deem in violation of this Code of Conduct:
|
| 73 |
+
|
| 74 |
+
### 1. Correction
|
| 75 |
+
|
| 76 |
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
| 77 |
+
unprofessional or unwelcome in the community.
|
| 78 |
+
|
| 79 |
+
**Consequence**: A private, written warning from community leaders, providing
|
| 80 |
+
clarity around the nature of the violation and an explanation of why the
|
| 81 |
+
behavior was inappropriate. A public apology may be requested.
|
| 82 |
+
|
| 83 |
+
### 2. Warning
|
| 84 |
+
|
| 85 |
+
**Community Impact**: A violation through a single incident or series
|
| 86 |
+
of actions.
|
| 87 |
+
|
| 88 |
+
**Consequence**: A warning with consequences for continued behavior. No
|
| 89 |
+
interaction with the people involved, including unsolicited interaction with
|
| 90 |
+
those enforcing the Code of Conduct, for a specified period of time. This
|
| 91 |
+
includes avoiding interactions in community spaces as well as external channels
|
| 92 |
+
like social media. Violating these terms may lead to a temporary or
|
| 93 |
+
permanent ban.
|
| 94 |
+
|
| 95 |
+
### 3. Temporary Ban
|
| 96 |
+
|
| 97 |
+
**Community Impact**: A serious violation of community standards, including
|
| 98 |
+
sustained inappropriate behavior.
|
| 99 |
+
|
| 100 |
+
**Consequence**: A temporary ban from any sort of interaction or public
|
| 101 |
+
communication with the community for a specified period of time. No public or
|
| 102 |
+
private interaction with the people involved, including unsolicited interaction
|
| 103 |
+
with those enforcing the Code of Conduct, is allowed during this period.
|
| 104 |
+
Violating these terms may lead to a permanent ban.
|
| 105 |
+
|
| 106 |
+
### 4. Permanent Ban
|
| 107 |
+
|
| 108 |
+
**Community Impact**: Demonstrating a pattern of violation of community
|
| 109 |
+
standards, including sustained inappropriate behavior, harassment of an
|
| 110 |
+
individual, or aggression toward or disparagement of classes of individuals.
|
| 111 |
+
|
| 112 |
+
**Consequence**: A permanent ban from any sort of public interaction within
|
| 113 |
+
the community.
|
| 114 |
+
|
| 115 |
+
## Attribution
|
| 116 |
+
|
| 117 |
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
| 118 |
+
version 2.0, available at
|
| 119 |
+
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
| 120 |
+
|
| 121 |
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
| 122 |
+
enforcement ladder](https://github.com/mozilla/diversity).
|
| 123 |
+
|
| 124 |
+
[homepage]: https://www.contributor-covenant.org
|
| 125 |
+
|
| 126 |
+
For answers to common questions about this code of conduct, see the FAQ at
|
| 127 |
+
https://www.contributor-covenant.org/faq. Translations are available at
|
| 128 |
+
https://www.contributor-covenant.org/translations.
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2026 Kai Piper
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
MANIFEST.in
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
include Run-CROSSWIRE.ps1
|
| 2 |
+
recursive-include configs *.json
|
| 3 |
+
recursive-include docs *.md
|
| 4 |
+
recursive-include paper *.tex *.bib
|
| 5 |
+
recursive-include research *.md *.json
|
| 6 |
+
recursive-include reports *.md *.json
|
| 7 |
+
recursive-include publication *.html
|
| 8 |
+
include tools/report.py
|
| 9 |
+
include tools/check_workbench.cjs
|
| 10 |
+
include tools/check_distribution.py
|
| 11 |
+
include tools/check_latex.py
|
README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: CROSSWIRE
|
| 3 |
+
emoji: 🔀
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: static
|
| 7 |
+
app_file: publication/index.html
|
| 8 |
+
license: mit
|
| 9 |
+
short_description: Finite interface experiments, source and replayable evidence
|
| 10 |
+
pinned: false
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# CROSSWIRE: Behavioral Interoperability Under Ambiguous Black-Box Interfaces
|
| 14 |
+
|
| 15 |
+
A CPU-local research laboratory and experimental benchmark for learning and verifying interface adapters between independently developed systems under unknown action and observation mappings.
|
| 16 |
+
|
| 17 |
+
[GitHub source](https://github.com/kai9987kai/CROSSWIRE) · [Hugging Face source and evidence portal](https://huggingface.co/spaces/Kai9987kai/CROSSWIRE) · [Publication and verification guide](docs/publication.md). Download the full workbench and run it locally; the Hugging Face Space serves the source/evidence portal.
|
| 18 |
+
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
## Overview
|
| 22 |
+
|
| 23 |
+
When independently developed controllers, agents, or tools communicate without a shared semantic schema, how can they safely connect?
|
| 24 |
+
|
| 25 |
+
**CROSSWIRE** formalizes this interface synthesis problem using finite-state Mealy transducers. Rather than choosing a single "best-fit" mapping, CROSSWIRE preserves the entire set of candidate adapters consistent with observed behavior. A task deployment plan is admitted if and only if it satisfies a public safety contract under **all** surviving interpretations; otherwise, it actively probes or abstains.
|
| 26 |
+
|
| 27 |
+
```
|
| 28 |
+
┌────────────────────────┐
|
| 29 |
+
│ Public Reference Model │
|
| 30 |
+
└───────────┬────────────┘
|
| 31 |
+
│
|
| 32 |
+
┌───────────▼────────────┐
|
| 33 |
+
│ Candidate Adapter Space│ (144 Bijection Pairs)
|
| 34 |
+
│ V(D) Surviving Maps │
|
| 35 |
+
└───────────┬────────────┘
|
| 36 |
+
│ (Consensus Plan / Active Probe)
|
| 37 |
+
┌───────────▼────────────┐
|
| 38 |
+
│ Process Boundary │ (Subprocess JSON Protocol)
|
| 39 |
+
└───────────┬────────────┘
|
| 40 |
+
│
|
| 41 |
+
┌───────────▼────────────┐
|
| 42 |
+
│ Opaque Target │
|
| 43 |
+
└───────────┬────────────┘
|
| 44 |
+
│
|
| 45 |
+
┌───────────▼────────────┐
|
| 46 |
+
│ Independent Audit │ (Post-Freeze Ground Truth)
|
| 47 |
+
└────────────────────────┘
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
## Key Capabilities & Architecture
|
| 53 |
+
|
| 54 |
+
- **Consensus Admission vs. Point Estimation:** Requires contract satisfaction across all surviving candidate mappings, conditional on model inclusion and truthful semantic anchors.
|
| 55 |
+
- **Model-Safe Calibration:** Optionally rejects calibration probes that could trigger a hazard under any surviving model. All ten native finite probe policies support the constraint, including generated long counterexamples and bounded lookahead trees. Independent certificates check each word against its pre-probe belief; escalation guards the full enabled temporal class from the first query.
|
| 56 |
+
- **Contract Edge Cutting (`ec2`):** An EC2-inspired heuristic partitions candidates into contract decision classes and prioritizes cross-class disagreement. The current Jaccard-weighted objective does not inherit a proved adaptive-submodularity or approximation guarantee.
|
| 57 |
+
- **Receding-Horizon Dynamic Replanning Shield:** Actively monitors intermediate deployment outputs; when updated beliefs reveal upcoming plan risks, dynamically synthesizes and executes verified safe detour suffixes (`REPLANNED_ONLINE_SAFE`), achieving goal completion where static shields abort.
|
| 58 |
+
- **Configurable Candidate-Fraction Admission:** `risk_controlled` checks exact hazardous and unsuccessful fractions over uniformly weighted surviving models against `risk_tolerance`. Zero requires consensus. These fractions are not calibrated probabilities of target harm or conformal risk bounds.
|
| 59 |
+
- **Autonomous Model-Class Escalation:** Starts with static bijections (144 models) and escalates to temporal latency-aware models (720 models) upon refutation. Independent certificates check refutation of the declared finite class; hashes bind their content.
|
| 60 |
+
- **Decision-Theoretic Value of Information (`voi`):** Probing policy that directly maximizes expected posterior task readiness $\mathbb{E}_y[\text{Readiness}(\mathcal{V}_{w, y})] / (1 + |w|)$, selecting probes that actively drive the version space into subsets that admit verified consensus plans. Outperforms Shannon entropy on tight query budgets ($B=4$) by prioritizing decision-critical contract branches over irrelevant permutation splits.
|
| 61 |
+
- **Sequential Non-Conformity Test Martingales (Ville's Inequality):** Anytime-valid sequential e-process $M_t = \prod_{k=1}^t \frac{1 / |\Gamma|}{P_{\mathcal{V}_{k-1}}(y_k)}$ testing model class inclusion and change-point drift with provable finite-sample Type-I error control: $\mathbb{P}_{H_0}(\exists t \ge 1: M_t \ge 1/\alpha) \le \alpha$. Rejects mis-specified models at $\alpha=0.05$ when $M_t \ge 20$ without waiting for full version space exhaustion.
|
| 62 |
+
- **Information-Theoretic Active Probing (Shannon Entropy):** Probing policy that maximizes output predictive entropy $H(O \mid w)$ per query cost, identifying valid adapters in up to 50% fewer target interactions.
|
| 63 |
+
- **Independent Evidence Verification & Minimal Contradiction Cores:** Decoupled certificate verifier independently re-enumerates models, verifies surviving beliefs against public transcripts, extracts deletion-minimal contradiction witnesses, and binds immutable certificates before private auditing.
|
| 64 |
+
- **Simplex Safe Fallback & Soft Return-to-Home:** Dynamically monitors execution step-by-step; upon detecting observation anomaly or impending hazard, dispatches a verified holding action (`recovery_strategy="stabilize"`) or executes a multi-step certified return-to-initial trajectory (`recovery_strategy="soft_return"`) restoring the target to $q_0$ hazard-free (`RECOVERED_SAFE_FALLBACK`).
|
| 65 |
+
- **Adaptive Contracts (Observation-Dependent Policies):** Extends interface synthesis from linear words to contingent AND/OR policy trees certified by an independent tree checker (Proof-Carrying Code). Chooses each action based on the observed response, achieving 100% useful coverage on routing interlocks without prior calibration.
|
| 66 |
+
- **Closed-Loop Dynamic Runtime Shielding:** Actively monitors intermediate target responses step-by-step; detects behavioral anomalies/drift and preemptively averts latent hazards before irreversible actions are committed.
|
| 67 |
+
- **Temporal / Latency-Aware Adapter Algebra:** Expands the adapter candidate space to 720 models supporting stateful pipeline delays ($d \in \{0, 1\}$) and initial buffer search, enabling verified connections to asynchronous interfaces.
|
| 68 |
+
- **Counterexample-Guided Active Probing (CEGP):** Optimized $O(W \cdot N)$ active exploration selecting probes that maximize pairwise disagreement across candidate models.
|
| 69 |
+
- **Strict Process Boundary:** Opaque targets run in isolated subprocesses communicating over line-delimited JSON. Every reset and step attempt is metered against a shared system-call cap.
|
| 70 |
+
- **Freeze-Before-Audit Protocol:** Scientific decisions, survivor counts, and probe histories freeze into cryptographic digests before private semantic evaluation.
|
| 71 |
+
- **Classical Learning Baseline:** Pinned integration with AALpy 1.6.2 ($L^*$ algorithm) under identical target interaction budgets.
|
| 72 |
+
- **Cryptographic Scientific Receipts:** Every run and study generates self-contained JSON receipts with source hash bindings and exact determinism checking.
|
| 73 |
+
- **Local Browser Workbench:** Responsive UI with model-safe calibration and tolerance controls, graph inspection, mapping matrices, probe timelines, shield events, and receipt export/import. Study comparisons expose exact conditions and allow individual run inspection while retaining the parent receipt.
|
| 74 |
+
- **Controlled Fault Studies:** Configure fault type, zero-based onset and phase in the workbench. Study grids include nominal controls and keep fault conditions separate throughout summaries, paired comparisons, reliability estimates and reports. Semantic swaps remain a deliberate undetectable negative control.
|
| 75 |
+
- **Budgeted Experiment Planning:** `lookahead` proposes depth-two query trees under the reset-plus-action cap, optimizing finite model-count task readiness. A separate checker reconstructs every branch, safety guard, readiness witness and cost before the root query is dispatched. Search caps remain explicit; hypothetical responses never become observed evidence.
|
| 76 |
+
- **Selectable Factored Runtime:** `--candidate-backend factored` works through acquisition, safety envelopes, escalation, adaptive/risk-controlled admission, studies and replay. Robust planning searches bounded input-factor state products. Exact class-histogram task scoring avoids materializing every candidate pair; telemetry distinguishes structural ratios from measured storage.
|
| 77 |
+
|
| 78 |
+
---
|
| 79 |
+
|
| 80 |
+
## Quickstart
|
| 81 |
+
|
| 82 |
+
### Installation
|
| 83 |
+
|
| 84 |
+
Requires Python 3.11+.
|
| 85 |
+
|
| 86 |
+
```bash
|
| 87 |
+
# Clone or navigate to the directory
|
| 88 |
+
cd C:\Users\kai99\Desktop\CROSSWIRE
|
| 89 |
+
|
| 90 |
+
# Create and activate a virtual environment
|
| 91 |
+
python -m venv .venv
|
| 92 |
+
.\.venv\Scripts\activate
|
| 93 |
+
|
| 94 |
+
# Install in editable mode with baseline dependencies
|
| 95 |
+
pip install -e ".[baselines,dev]"
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
### Running the Interactive Browser Workbench
|
| 99 |
+
|
| 100 |
+
Start the local server and open the browser:
|
| 101 |
+
|
| 102 |
+
```bash
|
| 103 |
+
crosswire serve --port 8781 --open
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
Or run the convenient PowerShell launcher:
|
| 107 |
+
|
| 108 |
+
```powershell
|
| 109 |
+
.\Run-CROSSWIRE.ps1
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
### Command-Line Interface
|
| 113 |
+
|
| 114 |
+
```bash
|
| 115 |
+
# Run a single experiment with shielded deployment and temporal synthesis
|
| 116 |
+
crosswire run --family delay --temporal --execution_mode shielded --budget 16 --out reports/delay.json
|
| 117 |
+
|
| 118 |
+
# Run counterexample-guided active probing (CEGP)
|
| 119 |
+
crosswire run --family latch --policy counterexample --budget 16 --out reports/cegp.json
|
| 120 |
+
|
| 121 |
+
# Plan bounded experiments using the factored solver
|
| 122 |
+
crosswire run --family generated_protocol --instance-seed 101 --policy lookahead --candidate-backend factored --probe-safety model_safe --budget 8 --out reports/my-lookahead.json
|
| 123 |
+
|
| 124 |
+
# Guard calibration before dispatch (conservative abstention is an expected result)
|
| 125 |
+
crosswire run --family routing --anchors --policy entropy --probe-safety model_safe --budget 16 --out reports/safe-routing.json
|
| 126 |
+
|
| 127 |
+
# Explore an explicit allowed fraction of surviving models
|
| 128 |
+
crosswire run --family latch --admission risk_controlled --risk-tolerance 0.2 --out reports/candidate-fractions.json
|
| 129 |
+
|
| 130 |
+
# Verify and replay a scientific receipt
|
| 131 |
+
crosswire verify reports/delay.json
|
| 132 |
+
|
| 133 |
+
# Execute a paired empirical study
|
| 134 |
+
crosswire study --config configs/pilot-shield-shielded.json --out reports/shielded-study.json
|
| 135 |
+
|
| 136 |
+
# Compare a nominal control with deployment faults using both fixture engines
|
| 137 |
+
crosswire study --config configs/resilience-workflows.json --out reports/my-resilience-study.json
|
| 138 |
+
|
| 139 |
+
# Exploratory comparison of policies and backends on generated target instances
|
| 140 |
+
crosswire study --config configs/budgeted-planning-pilot.json --out reports/my-planning-pilot.json
|
| 141 |
+
|
| 142 |
+
# Inspect available fixture families
|
| 143 |
+
crosswire catalog
|
| 144 |
+
```
|
| 145 |
+
|
| 146 |
+
Model-safe calibration depends on the enabled finite hypothesis class, stationarity, deterministic resets, and truthful anchors. It can stop without probing when every raw action could be hazardous. `NO_ELIGIBLE_SAFE_PROBE` describes the bounded probe pool and remaining call cap, not a proof that no safe experiment exists. Calibration and deployment hazards remain separate private-audit measurements. The optional AALpy baseline does not support guarded calibration and rejects that combination.
|
| 147 |
+
|
| 148 |
+
Use `crosswire check <receipt>` for independent public-model checking and `crosswire verify <receipt>` for actual target replay. Receipts bind their original Python source revision; historical receipts require that revision to replay. `python tools/report.py <study-receipt>` validates saved digests before rendering, including historical receipts, but does not replay targets or authenticate private audits. Current safety experiments and research boundaries are documented in [the calibration upgrade notes](research/2026-10-23-safe-calibration.md).
|
| 149 |
+
|
| 150 |
+
---
|
| 151 |
+
|
| 152 |
+
## Empirical Benchmark Findings
|
| 153 |
+
|
| 154 |
+
Historical pilot findings across synthetic fixture environments are listed below. They belong to the source revisions recorded in their receipts and are exploratory, rather than an independent replication or a guarantee about real systems.
|
| 155 |
+
|
| 156 |
+
1. **Autonomous Escalation & Shannon Entropy Efficiency:** In a 36-run benchmark (`reports/pilot-escalation-receipt.json`), Shannon entropy active probing resolved valid adapters in **4.5 mean calls** on `latch` compared to 8.5 for `task` and 9.0 for `fixed` (nearly 50% query savings). On pipeline-delayed targets (`delay`), fixed probing stalled and failed (0% coverage), while autonomous escalation promoted the hypothesis space to $\mathcal{H}_1$ and achieved successful connection. On unconnectable targets (`incompatible`), the engine certified structural contradiction across all candidate classes.
|
| 157 |
+
2. **Simplex Safe Recovery:** In the presence of runtime anomalies during deployment, Simplex recovery shielding intercepted the divergence, safely dispatched a verified minimum-hazard fallback action, and averted impending hazards with zero safety violations.
|
| 158 |
+
3. **Adaptive Contracts vs. Open-Loop Stalling:** On the designed `routing` interlock control, fixed non-branching sequences suffered 0% admission at budget zero because commit actions depend on the unknown input map. Adaptive policy synthesis certified an observation-dependent policy tree that achieved **100% useful coverage at budget zero without requiring any prior target interactions**, delivering a **+0.500 mean useful coverage advantage** on in-family fixtures (`[+0.000, +1.000]`).
|
| 159 |
+
4. **Zero Selective Harm (In-Family):** Across in-family mechanisms, robust consensus admission achieved **0% selective harm** and **0 hazards**. Whenever a connection was admitted, it was 100% successful.
|
| 160 |
+
3. **Task-Relevant Probing Efficiency:** Contract-relevant active probing achieved **71.4% useful coverage** in an average of only 7.4 calls (at budget 16), significantly outperforming fixed sequential probing (9.7 calls).
|
| 161 |
+
4. **Dynamic Anomaly Interception & Causal Hazard Deflection:** In stressful out-of-family environments, the **Closed-Loop Dynamic Runtime Shield** intercepted unexpected behavioral divergences across 8 deployment runs. Causal counterfactual auditing verified that where unshielded plans ran into catastrophic absorbing hazards (`alias` and `delay`), the shield preemptively deflected both hazards with zero harm.
|
| 162 |
+
5. **Temporal Adapter Synthesis:** On asynchronous interfaces with pipeline latency (`delay`), static bijections suffered 0% admission due to transition contradictions. Enabling temporal adapter synthesis expanded the search space to 720 models and achieved **100% useful coverage with 0% selective harm**.
|
| 163 |
+
6. **Decision-Focused CEGP Distinguishing Witnesses:** On deep mechanisms (`long_trace`) where fixed-horizon length-3 probes left candidate adapters indistinguishable (forcing 0% admission), Counterexample-Guided Active Probing (CEGP) dynamically synthesized the 4-step distinguishing witness `[0, 0, 0, 0]`, achieving **100% useful coverage** (+1.000 mean paired difference).
|
| 164 |
+
7. **Classical System Identification Bottleneck:** Under small call caps ($B \le 32$), classical automata learning (AALpy $L^*$) requires 100–250 queries for full state identification and thus abstains entirely (0% coverage). CROSSWIRE's task-directed ambiguity reduction solves the task with an order-of-magnitude fewer interactions.
|
| 165 |
+
8. **Falsification & Honest Negative Controls:** Under semantic corruption (where sensor outputs match raw predictions but underlying physical events are swapped), black-box consensus admitted connections that failed private auditing (100% harm). This rigorously demonstrates that ungrounded behavioral observation cannot substitute for semantic anchors.
|
| 166 |
+
9. **Forced-Fit Catastrophe:** Forced-fit point estimation (`fit`) admitted 33.3% of misspecified stress cases but incurred **62.5% selective harm**, proving the necessity of ambiguity preservation over single-point optimization.
|
| 167 |
+
10. **Value-of-Information (VoI) vs. Shannon Entropy Sample Efficiency:** In our 12-run comparative evaluation (`reports/pilot-voi-and-recovery-receipt.json`), under tight budget $B=4$, `voi` achieved **100% task admission** on `latch` and `routing` in only 4.0 mean calls, whereas Shannon output entropy achieved **0% admission** (0/3 across all families). Under $B=8$, `voi` reached full admission with fewer mean calls across every benchmark family (4.0 vs 6.0 on `latch`, 4.0 vs 5.0 on `routing`, 6.0 vs 7.0 on `robotics_airlock`).
|
| 168 |
+
11. **Certified Soft Return-to-Home Runtime De-escalation:** Under change-point sensor fault injection (`sensor_drift`), runtime shielding with `recovery_strategy="soft_return"` intervened at step 1 in 100% of cases (3/3), averting all hazards and synthesizing certified multi-step trajectories restoring the target to initial state $q_0$ without requiring hardware power-cycling.
|
| 169 |
+
|
| 170 |
+
---
|
| 171 |
+
|
| 172 |
+
## Documentation & Research
|
| 173 |
+
|
| 174 |
+
- [Standalone Research Paper](docs/paper.md): Comprehensive theoretical formulation, experimental methodology, empirical tables, and epistemological bounds.
|
| 175 |
+
- [Current Verification Report](docs/verification-budgeted-experiments.md): Bounded planning, backend parity, fresh exploratory studies, browser workflows and installed-package checks. [Resilience workflow verification](docs/verification-resilience-workflows.md), [calibration verification](docs/verification-safe-calibration.md) and [earlier verification](docs/verification.md) retain historical evidence.
|
| 176 |
+
- [Research basis and limits](research/2026-11-01-budgeted-experiments.md): Verified 2024–2026 primary work and the boundaries of CROSSWIRE's finite adaptation.
|
| 177 |
+
- [Pre-Implementation Design Specification](docs/superpowers/specs/2026-10-22-crosswire-design.md): The approved design specification.
|
| 178 |
+
- [Portfolio Research & Prior Art Reviews](research/portfolio-index.md): Source code reviews and commit hashes of all 26 related repositories.
|
| 179 |
+
- [Recent Research Context](research/recent-research.md): Representation stitching, active automata learning, and interface theory context.
|
| 180 |
+
|
| 181 |
+
---
|
| 182 |
+
|
| 183 |
+
## License
|
| 184 |
+
|
| 185 |
+
MIT License. See [LICENSE](LICENSE) for details.
|
Run-CROSSWIRE.ps1
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
param([int]$Port = 8781)
|
| 2 |
+
$ErrorActionPreference = 'Stop'
|
| 3 |
+
$taskPython = Join-Path $PSScriptRoot '.venv\Scripts\python.exe'
|
| 4 |
+
if (-not (Test-Path -LiteralPath $taskPython)) { $taskPython = 'python' }
|
| 5 |
+
Push-Location -LiteralPath $PSScriptRoot
|
| 6 |
+
try { & $taskPython -m crosswire serve --port $Port --open }
|
| 7 |
+
finally { Pop-Location }
|
SECURITY.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Security Policy
|
| 2 |
+
|
| 3 |
+
## Supported Versions
|
| 4 |
+
|
| 5 |
+
Use this section to tell people about which versions of your project are
|
| 6 |
+
currently being supported with security updates.
|
| 7 |
+
|
| 8 |
+
| Version | Supported |
|
| 9 |
+
| ------- | ------------------ |
|
| 10 |
+
| 5.1.x | :white_check_mark: |
|
| 11 |
+
| 5.0.x | :x: |
|
| 12 |
+
| 4.0.x | :white_check_mark: |
|
| 13 |
+
| < 4.0 | :x: |
|
| 14 |
+
|
| 15 |
+
## Reporting a Vulnerability
|
| 16 |
+
|
| 17 |
+
Use this section to tell people how to report a vulnerability.
|
| 18 |
+
|
| 19 |
+
Tell them where to go, how often they can expect to get an update on a
|
| 20 |
+
reported vulnerability, what to expect if the vulnerability is accepted or
|
| 21 |
+
declined, etc.
|
artifacts/crosswire_lab-0.1.0-py3-none-any.whl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bf5edd1f1a26acb90489d9e6be51ef921dcea6c0c24b889113f4c0d197b04579
|
| 3 |
+
size 111490
|
artifacts/crosswire_lab-0.1.0.tar.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7ea991b3f764b79222386b673879deda79cad029932e1745f09816fa1eb12b18
|
| 3 |
+
size 1436374
|
configs/budgeted-experiments.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["latch"],
|
| 3 |
+
"seeds": [17],
|
| 4 |
+
"budgets": [2, 4],
|
| 5 |
+
"policies": ["entropy", "lookahead"],
|
| 6 |
+
"admissions": ["robust"],
|
| 7 |
+
"candidate_backends": ["flat", "factored"],
|
| 8 |
+
"implementations": ["procedural"],
|
| 9 |
+
"execution_mode": "shielded"
|
| 10 |
+
}
|
configs/budgeted-planning-controls.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["latch", "delay", "semantic_corruption"],
|
| 3 |
+
"seeds": [42],
|
| 4 |
+
"budgets": [16],
|
| 5 |
+
"policies": ["entropy", "lookahead"],
|
| 6 |
+
"admissions": ["robust"],
|
| 7 |
+
"candidate_backends": ["flat", "factored"],
|
| 8 |
+
"implementations": ["procedural"],
|
| 9 |
+
"temporal": true,
|
| 10 |
+
"execution_mode": "shielded"
|
| 11 |
+
}
|
configs/budgeted-planning-pilot.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["generated_protocol"],
|
| 3 |
+
"instance_seeds": [101, 102],
|
| 4 |
+
"seeds": [17],
|
| 5 |
+
"budgets": [4, 8],
|
| 6 |
+
"policies": ["entropy", "branching", "lookahead"],
|
| 7 |
+
"admissions": ["robust"],
|
| 8 |
+
"candidate_backends": ["flat", "factored"],
|
| 9 |
+
"implementations": ["procedural"],
|
| 10 |
+
"split": "evaluation",
|
| 11 |
+
"probe_safety": "model_safe",
|
| 12 |
+
"execution_mode": "shielded"
|
| 13 |
+
}
|
configs/pilot-adaptive.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["routing", "latch", "semantic_corruption"],
|
| 3 |
+
"seeds": [17, 23],
|
| 4 |
+
"budgets": [0, 8],
|
| 5 |
+
"policies": ["fixed", "task"],
|
| 6 |
+
"admissions": ["robust", "adaptive"],
|
| 7 |
+
"implementations": ["procedural"],
|
| 8 |
+
"execution_mode": "shielded",
|
| 9 |
+
"horizon": 4,
|
| 10 |
+
"split": "evaluation"
|
| 11 |
+
}
|
configs/pilot-calibration-safe.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["latch", "routing", "neutral", "semantic_corruption"],
|
| 3 |
+
"seeds": [17, 42],
|
| 4 |
+
"budgets": [8],
|
| 5 |
+
"policies": ["entropy"],
|
| 6 |
+
"admissions": ["robust"],
|
| 7 |
+
"implementations": ["procedural"],
|
| 8 |
+
"anchors": false,
|
| 9 |
+
"split": "evaluation",
|
| 10 |
+
"horizon": 4,
|
| 11 |
+
"execution_mode": "shielded",
|
| 12 |
+
"probe_safety": "model_safe",
|
| 13 |
+
"risk_tolerance": 0.0
|
| 14 |
+
}
|
configs/pilot-calibration-unrestricted.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["latch", "routing", "neutral", "semantic_corruption"],
|
| 3 |
+
"seeds": [17, 42],
|
| 4 |
+
"budgets": [8],
|
| 5 |
+
"policies": ["entropy"],
|
| 6 |
+
"admissions": ["robust"],
|
| 7 |
+
"implementations": ["procedural"],
|
| 8 |
+
"anchors": false,
|
| 9 |
+
"split": "evaluation",
|
| 10 |
+
"horizon": 4,
|
| 11 |
+
"execution_mode": "shielded",
|
| 12 |
+
"probe_safety": "unrestricted",
|
| 13 |
+
"risk_tolerance": 0.0
|
| 14 |
+
}
|
configs/pilot-cegp-longtrace.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["long_trace"],
|
| 3 |
+
"seeds": [17, 23],
|
| 4 |
+
"budgets": [16],
|
| 5 |
+
"policies": ["task", "counterexample"],
|
| 6 |
+
"admissions": ["robust"],
|
| 7 |
+
"implementations": ["procedural"],
|
| 8 |
+
"split": "evaluation"
|
| 9 |
+
}
|
configs/pilot-escalation.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["latch", "delay", "incompatible"],
|
| 3 |
+
"seeds": [17, 23],
|
| 4 |
+
"budgets": [0, 16],
|
| 5 |
+
"policies": ["fixed", "task", "entropy"],
|
| 6 |
+
"admissions": ["robust"],
|
| 7 |
+
"implementations": ["procedural"],
|
| 8 |
+
"execution_mode": "recovery",
|
| 9 |
+
"escalate": true,
|
| 10 |
+
"horizon": 4,
|
| 11 |
+
"split": "evaluation"
|
| 12 |
+
}
|
configs/pilot-in-family.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["latch", "handshake", "symmetric", "neutral", "long_trace", "rotary", "interlock"],
|
| 3 |
+
"seeds": [17, 23],
|
| 4 |
+
"budgets": [0, 4, 8, 16, 32],
|
| 5 |
+
"policies": ["fixed", "random", "disagreement", "task", "aalpy"],
|
| 6 |
+
"admissions": ["robust"],
|
| 7 |
+
"implementations": ["procedural"],
|
| 8 |
+
"split": "evaluation"
|
| 9 |
+
}
|
configs/pilot-independent-protocols.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["generated_protocol"],
|
| 3 |
+
"instance_seeds": [1086403294, 3345179875, 2292068731, 1576243029, 1748994377, 3755511987, 3401703359, 1339975701, 2531305899, 3755517491, 1731955854, 619042882, 1616307191, 4106850140, 1223370761, 2467781857, 3344421178, 3050058121, 1170689128, 2880625474, 3197723763, 3032234287, 92124842, 642515718, 974945490, 3920501283, 3058244900, 2575615354, 782576727, 3596022451, 583869406, 1207436356],
|
| 4 |
+
"seeds": [17, 42],
|
| 5 |
+
"budgets": [16],
|
| 6 |
+
"policies": ["fixed", "entropy"],
|
| 7 |
+
"admissions": ["robust"],
|
| 8 |
+
"implementations": ["procedural"],
|
| 9 |
+
"anchors": false,
|
| 10 |
+
"split": "evaluation",
|
| 11 |
+
"horizon": 4,
|
| 12 |
+
"execution_mode": "shielded",
|
| 13 |
+
"temporal": false,
|
| 14 |
+
"escalate": false,
|
| 15 |
+
"probe_safety": "model_safe",
|
| 16 |
+
"risk_tolerance": 0.0
|
| 17 |
+
}
|
configs/pilot-replanning-ec2.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["latch", "handshake", "delay"],
|
| 3 |
+
"seeds": [17, 23],
|
| 4 |
+
"budgets": [0, 16],
|
| 5 |
+
"policies": ["fixed", "task", "entropy", "ec2"],
|
| 6 |
+
"admissions": ["robust", "risk_controlled"],
|
| 7 |
+
"implementations": ["procedural"],
|
| 8 |
+
"execution_mode": "replan",
|
| 9 |
+
"escalate": true,
|
| 10 |
+
"horizon": 4,
|
| 11 |
+
"split": "evaluation"
|
| 12 |
+
}
|
configs/pilot-shield-openloop.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["extra_state", "incompatible", "alias", "semantic_corruption", "bypass", "delay", "repeat", "drift", "malformed"],
|
| 3 |
+
"seeds": [17, 23],
|
| 4 |
+
"budgets": [16],
|
| 5 |
+
"policies": ["task"],
|
| 6 |
+
"admissions": ["robust", "fit"],
|
| 7 |
+
"implementations": ["procedural"],
|
| 8 |
+
"split": "evaluation"
|
| 9 |
+
}
|
configs/pilot-shield-shielded.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["extra_state", "incompatible", "alias", "semantic_corruption", "bypass", "delay", "repeat", "drift", "malformed"],
|
| 3 |
+
"seeds": [17, 23],
|
| 4 |
+
"budgets": [16],
|
| 5 |
+
"policies": ["task"],
|
| 6 |
+
"admissions": ["robust", "fit"],
|
| 7 |
+
"implementations": ["procedural"],
|
| 8 |
+
"split": "evaluation",
|
| 9 |
+
"execution_mode": "shielded"
|
| 10 |
+
}
|
configs/pilot-stress.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["extra_state", "incompatible", "alias", "semantic_corruption", "bypass", "delay", "repeat", "drift", "malformed"],
|
| 3 |
+
"seeds": [17, 23],
|
| 4 |
+
"budgets": [16],
|
| 5 |
+
"policies": ["task"],
|
| 6 |
+
"admissions": ["robust", "one_best", "fit", "identity", "abstain", "oracle"],
|
| 7 |
+
"implementations": ["procedural", "table"],
|
| 8 |
+
"split": "evaluation"
|
| 9 |
+
}
|
configs/pilot-temporal.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["delay"],
|
| 3 |
+
"seeds": [17, 23],
|
| 4 |
+
"budgets": [16],
|
| 5 |
+
"policies": ["task", "counterexample"],
|
| 6 |
+
"admissions": ["robust"],
|
| 7 |
+
"implementations": ["procedural"],
|
| 8 |
+
"split": "evaluation",
|
| 9 |
+
"temporal": true
|
| 10 |
+
}
|
configs/resilience-workflows.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["latch"],
|
| 3 |
+
"seeds": [42],
|
| 4 |
+
"budgets": [16],
|
| 5 |
+
"policies": ["task"],
|
| 6 |
+
"admissions": ["robust"],
|
| 7 |
+
"implementations": ["procedural", "table"],
|
| 8 |
+
"execution_mode": "shielded",
|
| 9 |
+
"fault_schedules": [
|
| 10 |
+
null,
|
| 11 |
+
{"step": 2, "type": "sensor_drift", "phase": "deployment"},
|
| 12 |
+
{"step": 2, "type": "stuck_output", "phase": "deployment", "value": 0},
|
| 13 |
+
{"step": 1, "type": "semantic_swap", "phase": "deployment"}
|
| 14 |
+
]
|
| 15 |
+
}
|
configs/smoke.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"families": ["latch", "symmetric", "neutral"],
|
| 3 |
+
"seeds": [17],
|
| 4 |
+
"budgets": [0, 8, 16],
|
| 5 |
+
"policies": ["fixed", "random", "task"],
|
| 6 |
+
"admissions": ["robust"],
|
| 7 |
+
"implementations": ["procedural"]
|
| 8 |
+
}
|
crosswire/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""CROSSWIRE finite behavioral interoperability laboratory."""
|
| 2 |
+
|
| 3 |
+
__version__ = "0.1.0"
|
crosswire/__main__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .cli import main
|
| 2 |
+
|
| 3 |
+
raise SystemExit(main())
|
crosswire/adaptive.py
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Bounded robust policies over public adapter beliefs, with a separate checker.
|
| 2 |
+
|
| 3 |
+
The tree is contingent on raw observations, never private fixture state. Guarantees
|
| 4 |
+
are conditional on the real system being represented by the initial belief.
|
| 5 |
+
"""
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
from .core import CandidateSpace, _integer
|
| 9 |
+
from .identity import canonical_hash
|
| 10 |
+
from .interfaces import InterfaceError
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _cancelled(cancel):
|
| 14 |
+
if cancel and cancel():
|
| 15 |
+
# Local import avoids a study/adaptive import cycle.
|
| 16 |
+
from .study import Cancelled
|
| 17 |
+
raise Cancelled('Adaptive policy computation cancelled')
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def synthesize_policy(space: CandidateSpace, ids, node_limit=20000, cancel=None) -> dict:
|
| 21 |
+
"""Find a minimum worst-case-depth tree, then lexicographic actions.
|
| 22 |
+
|
| 23 |
+
Iterative deepening searches an OR over actions and an AND over all response
|
| 24 |
+
partitions. A hard node cap returns SEARCH_LIMIT, never a negative proof.
|
| 25 |
+
All actions, including diagnostic ones, count toward the contract horizon.
|
| 26 |
+
"""
|
| 27 |
+
ids = space._ids(ids)
|
| 28 |
+
_integer(node_limit, 'node_limit', 1, 1000000)
|
| 29 |
+
expanded = 0
|
| 30 |
+
|
| 31 |
+
class Limit(Exception):
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
def search(belief, prefix, remaining):
|
| 35 |
+
nonlocal expanded
|
| 36 |
+
_cancelled(cancel)
|
| 37 |
+
if expanded >= node_limit:
|
| 38 |
+
raise Limit
|
| 39 |
+
expanded += 1
|
| 40 |
+
if prefix and all(space.contract.accepts(space.semantic(i, prefix)) for i in belief):
|
| 41 |
+
return {'stop': True}
|
| 42 |
+
if not remaining:
|
| 43 |
+
return None
|
| 44 |
+
for action in range(space.machine.inputs):
|
| 45 |
+
word = prefix + (action,)
|
| 46 |
+
if not space.probe_is_safe(belief, word):
|
| 47 |
+
continue
|
| 48 |
+
partitions = {}
|
| 49 |
+
for i in belief:
|
| 50 |
+
partitions.setdefault(space.predict(i, word)[-1], []).append(i)
|
| 51 |
+
branches = {}
|
| 52 |
+
for raw, members in sorted(partitions.items()):
|
| 53 |
+
child = search(tuple(members), word, remaining - 1)
|
| 54 |
+
if child is None:
|
| 55 |
+
break
|
| 56 |
+
branches[str(raw)] = child
|
| 57 |
+
else:
|
| 58 |
+
return {'action': action, 'branches': branches}
|
| 59 |
+
return None
|
| 60 |
+
|
| 61 |
+
tree, complete = None, True
|
| 62 |
+
if ids:
|
| 63 |
+
try:
|
| 64 |
+
for depth in range(1, space.contract.horizon + 1):
|
| 65 |
+
tree = search(ids, (), depth)
|
| 66 |
+
if tree is not None:
|
| 67 |
+
break
|
| 68 |
+
except Limit:
|
| 69 |
+
complete = False
|
| 70 |
+
certificate = verify_policy(space, ids, tree) if tree is not None else None
|
| 71 |
+
if tree is not None and not certificate['valid']:
|
| 72 |
+
raise RuntimeError('Synthesized policy failed independent model checking')
|
| 73 |
+
return {'status': 'ADAPTIVE_ADMISSIBLE' if tree is not None else
|
| 74 |
+
'FAMILY_CONTRADICTED' if not ids else 'UNRESOLVED' if complete else 'SEARCH_LIMIT',
|
| 75 |
+
'admitted': tree is not None, 'word': None, 'policy': tree,
|
| 76 |
+
'candidate_count': len(ids), 'certificate': certificate,
|
| 77 |
+
'search': {'expanded_nodes': expanded, 'node_limit': node_limit,
|
| 78 |
+
'complete': complete, 'horizon': space.contract.horizon}}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def verify_policy(space: CandidateSpace, ids, policy) -> dict:
|
| 82 |
+
"""Check a supplied tree without calling the synthesizer.
|
| 83 |
+
|
| 84 |
+
Derive every branch belief anew from predictions; never trust stored survivor
|
| 85 |
+
counts, hashes, claimed leaf goals, or search metadata. This checks a finite
|
| 86 |
+
public model, not truth of its assumptions or real-world semantic grounding.
|
| 87 |
+
"""
|
| 88 |
+
ids = space._ids(ids)
|
| 89 |
+
checked, leaves, worst = 0, 0, 0
|
| 90 |
+
|
| 91 |
+
def visit(node, belief, prefix):
|
| 92 |
+
nonlocal checked, leaves, worst
|
| 93 |
+
checked += 1
|
| 94 |
+
if not belief:
|
| 95 |
+
raise ValueError('An empty belief cannot certify a policy')
|
| 96 |
+
if not isinstance(node, dict):
|
| 97 |
+
raise ValueError('Policy nodes must be objects')
|
| 98 |
+
if len(prefix) > space.contract.horizon:
|
| 99 |
+
raise ValueError('Policy exceeds the contract horizon')
|
| 100 |
+
if set(node) == {'stop'} and node['stop'] is True:
|
| 101 |
+
if not prefix or not all(space.contract.accepts(space.semantic(i, prefix)) for i in belief):
|
| 102 |
+
raise ValueError('Stop leaf does not satisfy every surviving interpretation')
|
| 103 |
+
leaves += 1
|
| 104 |
+
worst = max(worst, len(prefix))
|
| 105 |
+
return
|
| 106 |
+
if set(node) != {'action', 'branches'} or len(prefix) >= space.contract.horizon:
|
| 107 |
+
raise ValueError('Malformed action node or horizon exhausted')
|
| 108 |
+
action = _integer(node['action'], 'policy action', maximum=space.machine.inputs - 1)
|
| 109 |
+
branches = node['branches']
|
| 110 |
+
if not isinstance(branches, dict):
|
| 111 |
+
raise ValueError('Branches must be an object keyed by raw output')
|
| 112 |
+
word = prefix + (action,)
|
| 113 |
+
expected = {}
|
| 114 |
+
for i in belief:
|
| 115 |
+
if space.contract.hazard in space.semantic(i, word):
|
| 116 |
+
raise ValueError('An action risks a hazard under a surviving interpretation')
|
| 117 |
+
expected.setdefault(str(space.predict(i, word)[-1]), []).append(i)
|
| 118 |
+
if set(branches) != set(expected):
|
| 119 |
+
raise ValueError('Branches must cover exactly every possible raw output')
|
| 120 |
+
for raw, members in sorted(expected.items()):
|
| 121 |
+
visit(branches[raw], tuple(members), word)
|
| 122 |
+
|
| 123 |
+
try:
|
| 124 |
+
visit(policy, ids, ())
|
| 125 |
+
except (ValueError, TypeError, RecursionError) as exc:
|
| 126 |
+
return {'valid': False, 'reason': str(exc), 'checked_nodes': checked}
|
| 127 |
+
return {'schema': 'crosswire-policy-certificate-v1', 'valid': True,
|
| 128 |
+
'checked_nodes': checked, 'leaves': leaves, 'worst_case_steps': worst,
|
| 129 |
+
'candidate_count': len(ids),
|
| 130 |
+
'binding_hash': canonical_hash({'reference': space.machine.to_dict(),
|
| 131 |
+
'contract': space.contract.to_dict(),
|
| 132 |
+
'adapters': [space.adapters[i].to_dict() for i in ids],
|
| 133 |
+
'policy': policy}),
|
| 134 |
+
'scope': 'All branches in the declared finite candidate family; conditional on target inclusion, '
|
| 135 |
+
'stationarity, deterministic reset and truthful anchors. Not semantic grounding.'}
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def policy_metrics(space: CandidateSpace, ids, policy) -> dict:
|
| 139 |
+
"""Compute structural and information-theoretic properties of a certified contingent policy tree."""
|
| 140 |
+
import math
|
| 141 |
+
ids = space._ids(ids)
|
| 142 |
+
cert = verify_policy(space, ids, policy)
|
| 143 |
+
if not cert['valid']:
|
| 144 |
+
return {'valid': False, 'reason': cert['reason']}
|
| 145 |
+
|
| 146 |
+
def count_nodes(node):
|
| 147 |
+
if not isinstance(node, dict) or 'stop' in node:
|
| 148 |
+
return 0, 0
|
| 149 |
+
branches = node.get('branches', {})
|
| 150 |
+
ins, mb = 1, len(branches)
|
| 151 |
+
for child in branches.values():
|
| 152 |
+
ci, cmb = count_nodes(child)
|
| 153 |
+
ins += ci
|
| 154 |
+
mb = max(mb, cmb)
|
| 155 |
+
return ins, mb
|
| 156 |
+
|
| 157 |
+
internal_nodes, max_branching = count_nodes(policy)
|
| 158 |
+
depths = []
|
| 159 |
+
# Equal action words can reach distinct leaves after different observations.
|
| 160 |
+
leaf_buckets = {}
|
| 161 |
+
for aid in ids:
|
| 162 |
+
node, word, path = policy, (), ()
|
| 163 |
+
while 'action' in node:
|
| 164 |
+
word += (node['action'],)
|
| 165 |
+
out = str(space.predict(aid, word)[-1])
|
| 166 |
+
path += ((node['action'], out),)
|
| 167 |
+
node = node['branches'][out]
|
| 168 |
+
depths.append(len(word))
|
| 169 |
+
leaf_buckets[path] = leaf_buckets.get(path, 0) + 1
|
| 170 |
+
|
| 171 |
+
n = len(ids)
|
| 172 |
+
leaf_sizes = sorted(leaf_buckets.values(), reverse=True)
|
| 173 |
+
info_gain = -sum((c / n) * math.log2(c / n) for c in leaf_sizes) if n else 0.0
|
| 174 |
+
return {
|
| 175 |
+
'valid': True,
|
| 176 |
+
'candidate_count': n,
|
| 177 |
+
'internal_nodes': internal_nodes,
|
| 178 |
+
'leaf_count': cert['leaves'],
|
| 179 |
+
'best_case_steps': min(depths) if depths else 0,
|
| 180 |
+
'worst_case_steps': cert['worst_case_steps'],
|
| 181 |
+
'expected_steps': round(sum(depths) / n, 6) if n else 0.0,
|
| 182 |
+
'max_branching_factor': max_branching,
|
| 183 |
+
'leaf_belief_sizes': leaf_sizes,
|
| 184 |
+
'information_gain_bits': round(info_gain, 6),
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def execute_policy(target, policy, space: CandidateSpace, ids, cancel=None, fault_schedule=None, mode='adaptive', recovery_strategy='stabilize') -> dict:
|
| 189 |
+
"""Verify before reset, then execute only certified observation branches."""
|
| 190 |
+
if mode not in ('adaptive', 'shielded', 'recovery'):
|
| 191 |
+
raise ValueError('Unsupported adaptive execution mode')
|
| 192 |
+
if hasattr(target, 'set_phase'):
|
| 193 |
+
target.set_phase('deployment')
|
| 194 |
+
ids = space._ids(ids)
|
| 195 |
+
certificate = verify_policy(space, ids, policy)
|
| 196 |
+
if not certificate['valid']:
|
| 197 |
+
raise ValueError('Refusing uncertified policy: ' + certificate['reason'])
|
| 198 |
+
cp_step = fault_schedule.get('step') if fault_schedule else None
|
| 199 |
+
cp_type = fault_schedule.get('type') if fault_schedule else None
|
| 200 |
+
cp_detected = False
|
| 201 |
+
cp_det_step = None
|
| 202 |
+
cp_det_latency = None
|
| 203 |
+
|
| 204 |
+
def _check_cp(step_idx):
|
| 205 |
+
nonlocal cp_detected, cp_det_step, cp_det_latency
|
| 206 |
+
if (fault_schedule and fault_schedule.get('phase', 'deployment') in ('deployment', 'always')
|
| 207 |
+
and cp_step is not None and step_idx >= cp_step and not cp_detected):
|
| 208 |
+
cp_detected = True
|
| 209 |
+
cp_det_step = step_idx
|
| 210 |
+
cp_det_latency = step_idx - cp_step
|
| 211 |
+
|
| 212 |
+
deployment = {'word': None, 'executed_word': [], 'outputs': [], 'complete': False,
|
| 213 |
+
'mode': 'adaptive' if mode in ('adaptive', 'shielded') else 'recovery',
|
| 214 |
+
'shield_status': 'ACTIVE', 'intervened': False,
|
| 215 |
+
'hazard_averted': False, 'reasons': [], 'survivor_history': [len(ids)],
|
| 216 |
+
'target_steps': 0, 'resets': 0, 'target_calls': 0, 'error': None,
|
| 217 |
+
'certificate': certificate}
|
| 218 |
+
before = dict(target.counts)
|
| 219 |
+
node, active = policy, ids
|
| 220 |
+
try:
|
| 221 |
+
_cancelled(cancel)
|
| 222 |
+
target.reset()
|
| 223 |
+
while 'action' in node:
|
| 224 |
+
_cancelled(cancel)
|
| 225 |
+
action = node['action']
|
| 226 |
+
word = tuple(deployment['executed_word']) + (action,)
|
| 227 |
+
if not active or not space.probe_is_safe(active, word):
|
| 228 |
+
_check_cp(len(deployment['executed_word']))
|
| 229 |
+
deployment.update(intervened=True, shield_status='INTERVENED_MODEL_RISK')
|
| 230 |
+
deployment['reasons'].append('No unanimously safe continuation')
|
| 231 |
+
break
|
| 232 |
+
raw = target.step(action)
|
| 233 |
+
deployment['executed_word'].append(action)
|
| 234 |
+
deployment['outputs'].append(raw)
|
| 235 |
+
active = tuple(i for i in active if space.predict(i, word) == tuple(deployment['outputs']))
|
| 236 |
+
deployment['survivor_history'].append(len(active))
|
| 237 |
+
if not active or str(raw) not in node['branches']:
|
| 238 |
+
_check_cp(len(deployment['executed_word']) - 1)
|
| 239 |
+
deployment.update(intervened=True, shield_status='INTERVENED_ANOMALY')
|
| 240 |
+
deployment['reasons'].append('Observed response has no certified continuation')
|
| 241 |
+
break
|
| 242 |
+
node = node['branches'][str(raw)]
|
| 243 |
+
else:
|
| 244 |
+
deployment.update(complete=True, shield_status='COMPLETED')
|
| 245 |
+
if deployment['intervened'] and mode == 'recovery':
|
| 246 |
+
from .shield import RuntimeShield
|
| 247 |
+
shield = RuntimeShield(space, ids, space.contract, mode='recovery', cancel=cancel,
|
| 248 |
+
fault_schedule=fault_schedule, recovery_strategy=recovery_strategy)
|
| 249 |
+
shield.history_words = list(deployment['executed_word'])
|
| 250 |
+
shield.history_outputs = list(deployment['outputs'])
|
| 251 |
+
shield.active_survivors = active
|
| 252 |
+
shield.survivor_counts = list(deployment['survivor_history'])
|
| 253 |
+
shield.status = deployment['shield_status']
|
| 254 |
+
shield.intervened = True
|
| 255 |
+
shield.recover(target)
|
| 256 |
+
deployment['executed_word'] = list(shield.history_words)
|
| 257 |
+
deployment['outputs'] = list(shield.history_outputs)
|
| 258 |
+
deployment['survivor_history'] = list(shield.survivor_counts)
|
| 259 |
+
deployment['shield_status'] = shield.status
|
| 260 |
+
deployment['reasons'].extend(shield.reasons)
|
| 261 |
+
deployment['recovery'] = shield.recovery_record()
|
| 262 |
+
except InterfaceError as exc:
|
| 263 |
+
deployment['error'] = {'code': exc.code, 'message': str(exc)}
|
| 264 |
+
deployment['shield_status'] = 'INTERFACE_ERROR'
|
| 265 |
+
finally:
|
| 266 |
+
deployment.update(target_steps=target.counts['step_attempts'] - before['step_attempts'],
|
| 267 |
+
resets=target.counts['reset_attempts'] - before['reset_attempts'],
|
| 268 |
+
target_calls=target.counts['target_calls'] - before['target_calls'],
|
| 269 |
+
change_point={'scheduled': fault_schedule is not None, 'step': cp_step,
|
| 270 |
+
'type': cp_type, 'detected': cp_detected,
|
| 271 |
+
'detection_step': cp_det_step, 'detection_latency': cp_det_latency})
|
| 272 |
+
return deployment
|
crosswire/audit.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Private post-decision evaluation, never used to choose ordinary probes/plans."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
from .core import CandidateSpace, compare_adapter, words
|
| 5 |
+
from .fixtures import Case, catalog, evaluate_calibration, evaluate_deployment, target_machine
|
| 6 |
+
from .identity import canonical_hash
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def evaluate(case: Case, space: CandidateSpace, survivors: tuple[int, ...], decision: dict, probes: list[dict], deployment: dict) -> dict:
|
| 10 |
+
private = case.private
|
| 11 |
+
work = {'resets': 0, 'steps': 0}
|
| 12 |
+
model = target_machine(private, work=work)
|
| 13 |
+
equivalent = []
|
| 14 |
+
counterexamples = []
|
| 15 |
+
visited = 0
|
| 16 |
+
reference_steps = target_model_steps = 0
|
| 17 |
+
if model is not None:
|
| 18 |
+
for adapter in space.adapters:
|
| 19 |
+
check = compare_adapter(space.machine, adapter, model)
|
| 20 |
+
visited += check["visited_pairs"]
|
| 21 |
+
reference_steps += check.get('source_steps', 0)
|
| 22 |
+
target_model_steps += check.get('target_model_steps', 0)
|
| 23 |
+
if check["equivalent"]:
|
| 24 |
+
equivalent.append(adapter.id)
|
| 25 |
+
else:
|
| 26 |
+
counterexamples.append({"adapter_id": adapter.id, "word": check["counterexample"]})
|
| 27 |
+
actual = None
|
| 28 |
+
deployment_audit = None
|
| 29 |
+
counterfactual_hazard = False
|
| 30 |
+
if decision["admitted"]:
|
| 31 |
+
# Execution evidence owns the audited trajectory. A contingent policy
|
| 32 |
+
# or recovery can dispatch a different word from the initial request.
|
| 33 |
+
plan = (deployment['executed_word'] if 'executed_word' in deployment else
|
| 34 |
+
decision['policy'] if decision.get('word') is None else decision['word'])
|
| 35 |
+
if deployment['complete']:
|
| 36 |
+
actual = evaluate_deployment(private, probes, plan, work=work)
|
| 37 |
+
reproduced = actual['outputs'] == deployment['outputs']
|
| 38 |
+
if not reproduced:
|
| 39 |
+
actual = {**actual, 'goal_success': False, 'contract_satisfied': False}
|
| 40 |
+
executed_len = len(deployment['outputs'])
|
| 41 |
+
deployment_audit = {'completed': True, 'executed_prefix_length': executed_len,
|
| 42 |
+
'reconstruction_matches_observed': reproduced, 'prefix_hazard': actual['hazard'],
|
| 43 |
+
'counterfactual_hazard': actual['hazard']}
|
| 44 |
+
else:
|
| 45 |
+
# Only the successfully executed prefix happened. Fixture faults
|
| 46 |
+
# fail before the engine steps, so the failed call emitted no event.
|
| 47 |
+
# The unexecuted suffix is never simulated as if it occurred.
|
| 48 |
+
executed = list((deployment.get('executed_word', decision.get('word')) or ())[:len(deployment['outputs'])])
|
| 49 |
+
prefix = (evaluate_deployment(private, probes, executed, work=work) if executed
|
| 50 |
+
else {'outputs': [], 'semantic_events': [], 'hazard': False})
|
| 51 |
+
hazard_key = private['contract']['hazard']
|
| 52 |
+
prefix_hazard = hazard_key in prefix['semantic_events']
|
| 53 |
+
actual = {'outputs': prefix['outputs'], 'semantic_events': prefix['semantic_events'],
|
| 54 |
+
'goal_success': False, 'contract_satisfied': False,
|
| 55 |
+
# Observed prefix hazard is evidence; absence is not proof of safety.
|
| 56 |
+
'hazard': True if prefix_hazard else None}
|
| 57 |
+
# Counterfactual audit: would the full unshielded plan have triggered a hazard?
|
| 58 |
+
if decision.get("word") is not None:
|
| 59 |
+
counterfactual = evaluate_deployment(private, probes, decision["word"], work=work)
|
| 60 |
+
counterfactual_hazard = bool(counterfactual.get('hazard', False))
|
| 61 |
+
else:
|
| 62 |
+
counterfactual_hazard = False
|
| 63 |
+
deployment_audit = {'completed': False, 'executed_prefix_length': len(executed),
|
| 64 |
+
'reconstruction_matches_observed': prefix['outputs'] == deployment['outputs'],
|
| 65 |
+
'prefix_hazard': prefix_hazard, 'counterfactual_hazard': counterfactual_hazard,
|
| 66 |
+
'error_code': (deployment.get('error') or {}).get('code')}
|
| 67 |
+
oracle = None
|
| 68 |
+
oracle_candidates = 0
|
| 69 |
+
if private.get('family') != 'malformed':
|
| 70 |
+
for candidate_word in words(space.machine.inputs, space.contract.horizon):
|
| 71 |
+
oracle_candidates += 1
|
| 72 |
+
if evaluate_deployment(private, probes, candidate_word, work=work)['contract_satisfied']:
|
| 73 |
+
oracle = list(candidate_word)
|
| 74 |
+
break
|
| 75 |
+
planted = next((a.id for a in space.adapters
|
| 76 |
+
if tuple(private.get("input_map", ())) == a.input_map
|
| 77 |
+
and tuple(private.get("output_map", ())) == a.output_map), None)
|
| 78 |
+
# A raw planted code need not be a true semantic interpretation after corruption.
|
| 79 |
+
in_family = any(item['id'] == private.get('family') and item['scope'] == 'in_family'
|
| 80 |
+
for item in catalog())
|
| 81 |
+
calibration_hazards = 0
|
| 82 |
+
for i, probe in enumerate(probes):
|
| 83 |
+
executed = probe['word'][:len(probe['outputs'])]
|
| 84 |
+
if not executed:
|
| 85 |
+
continue
|
| 86 |
+
try:
|
| 87 |
+
item = evaluate_calibration(private, probes[:i], executed, work=work)
|
| 88 |
+
calibration_hazards += int(item["hazard"])
|
| 89 |
+
except (ValueError, RuntimeError):
|
| 90 |
+
pass
|
| 91 |
+
if model is None:
|
| 92 |
+
scope = "No finite stationary target model available; equivalence is unresolved."
|
| 93 |
+
else:
|
| 94 |
+
model_class = 'static plus latency-1 temporal' if space.temporal else 'static'
|
| 95 |
+
scope = (f"Exact raw equivalence within the declared {len(space.adapters)}-map {model_class} finite class only; "
|
| 96 |
+
"latent semantics are not established by raw equivalence.")
|
| 97 |
+
causal_hazard_averted = bool((deployment.get("intervened") or deployment.get("replanned")) and counterfactual_hazard and (actual.get("hazard") is not True))
|
| 98 |
+
cp_info = deployment.get("change_point")
|
| 99 |
+
return {
|
| 100 |
+
"goal_success": actual["goal_success"] if actual else None,
|
| 101 |
+
"change_point": cp_info,
|
| 102 |
+
"hazard": actual["hazard"] if actual else None,
|
| 103 |
+
"hazard_averted": causal_hazard_averted if actual else None,
|
| 104 |
+
"shield_intervened": bool(deployment.get("intervened", False)) if actual else None,
|
| 105 |
+
"contract_satisfied": actual["contract_satisfied"] if actual else None,
|
| 106 |
+
"outputs": actual["outputs"] if actual else [],
|
| 107 |
+
"semantic_events": actual["semantic_events"] if actual else [],
|
| 108 |
+
"deployment_audit": deployment_audit,
|
| 109 |
+
"oracle_feasible": oracle is not None, "oracle_word": oracle,
|
| 110 |
+
"true_map_retained": planted in survivors if in_family and planted is not None else None,
|
| 111 |
+
"planted_raw_map_retained": planted in survivors if planted is not None else None,
|
| 112 |
+
"raw_equivalent_maps_count": len(equivalent) if model is not None else None,
|
| 113 |
+
"raw_equivalent_map_ids": equivalent,
|
| 114 |
+
"no_map_in_class": not equivalent if model is not None else None,
|
| 115 |
+
"maps_checked": len(space.adapters) if model is not None else 0,
|
| 116 |
+
"counterexample": min((e["word"] for e in counterexamples), key=lambda w: (len(w), w), default=None),
|
| 117 |
+
"counterexamples_hash": canonical_hash(counterexamples),
|
| 118 |
+
'counterexample_scope': 'Witness rejecting an alternative raw map; not a violation of the admitted task plan',
|
| 119 |
+
"audit_product_pairs": visited, "audit_execution_steps": work['steps'],
|
| 120 |
+
'audit_reference_steps': reference_steps, 'audit_target_model_steps': target_model_steps,
|
| 121 |
+
'audit_resets': work['resets'], 'audit_oracle_word_candidates': oracle_candidates,
|
| 122 |
+
"calibration_hazardous_probes": calibration_hazards,
|
| 123 |
+
"assumption_limits": scope,
|
| 124 |
+
}
|
crosswire/backends.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Candidate construction and actual backend storage/structure telemetry."""
|
| 2 |
+
from math import factorial
|
| 3 |
+
|
| 4 |
+
from .core import CandidateSpace, FactoredCandidateSpace
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
CANDIDATE_BACKENDS = ('flat', 'factored')
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def make_space(machine, contract, backend='flat', temporal=False):
|
| 11 |
+
if backend not in CANDIDATE_BACKENDS:
|
| 12 |
+
raise ValueError('Unknown candidate backend')
|
| 13 |
+
space_type = FactoredCandidateSpace if backend == 'factored' else CandidateSpace
|
| 14 |
+
return space_type(machine, contract, temporal=temporal)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def backend_summary(space, ids):
|
| 18 |
+
"""Inspect actual storage without enumerating or materializing lazy adapters."""
|
| 19 |
+
if not isinstance(space, CandidateSpace):
|
| 20 |
+
raise TypeError('backend_summary requires a candidate space')
|
| 21 |
+
ids = space._ids(ids)
|
| 22 |
+
factored = isinstance(space, FactoredCandidateSpace)
|
| 23 |
+
if factored:
|
| 24 |
+
factors = [space.decompose_id(i) for i in ids]
|
| 25 |
+
active_inputs = {factor[0] for factor in factors}
|
| 26 |
+
active_outputs = {factor[1] for factor in factors}
|
| 27 |
+
cached_adapters = len(space._adapter_cache)
|
| 28 |
+
else:
|
| 29 |
+
active_inputs = {space.adapters[i].input_map for i in ids}
|
| 30 |
+
active_outputs = {space.adapters[i].output_map for i in ids}
|
| 31 |
+
cached_adapters = len(space.adapters)
|
| 32 |
+
input_factors, output_factors = factorial(space.machine.inputs), factorial(space.machine.outputs)
|
| 33 |
+
total = len(space.adapters)
|
| 34 |
+
return {
|
| 35 |
+
'backend': 'factored' if factored else 'flat',
|
| 36 |
+
'total_candidates': total, 'active_candidates': len(ids),
|
| 37 |
+
'total_input_factors': input_factors, 'total_output_factors': output_factors,
|
| 38 |
+
'active_input_factors': len(active_inputs), 'active_output_factors': len(active_outputs),
|
| 39 |
+
'cached_adapters': cached_adapters, 'cached_predictions': len(space._predictions),
|
| 40 |
+
'structural_ratio': round(total / (input_factors + output_factors), 2),
|
| 41 |
+
'structural_ratio_scope': ('Candidate count / (input factors + output factors); '
|
| 42 |
+
'not measured memory compression.'),
|
| 43 |
+
}
|
crosswire/baselines.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Budgeted classical model learning with the same opaque target interface."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
from importlib.metadata import version
|
| 5 |
+
import random
|
| 6 |
+
|
| 7 |
+
from .core import Machine, Contract, CandidateSpace, compare_adapter
|
| 8 |
+
from .interfaces import InterfaceError
|
| 9 |
+
from .backends import make_space
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class _BudgetExhausted(RuntimeError):
|
| 13 |
+
pass
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def acquire_aalpy(public: dict, budget: int, target, cancel=None, *, candidate_backend='flat'):
|
| 17 |
+
from aalpy.base import SUL, Oracle
|
| 18 |
+
from aalpy.learning_algs import run_Lstar
|
| 19 |
+
from .study import Cancelled, fit_evidence
|
| 20 |
+
|
| 21 |
+
space = make_space(Machine.from_dict(public['reference']), Contract.from_dict(public['contract']), candidate_backend)
|
| 22 |
+
anchors = {int(k): int(v) for k, v in public.get('anchors', {}).items()}
|
| 23 |
+
probes, error = [], None
|
| 24 |
+
detail = {'library': 'aalpy', 'version': version('aalpy'), 'model_complete': False,
|
| 25 |
+
'oracle': 'budgeted local-RNG random words, no exact target equivalence',
|
| 26 |
+
'model_product_pairs': 0, 'equivalence_probe_steps': 0,
|
| 27 |
+
'comparison_source_steps': 0, 'comparison_hypothesis_steps': 0,
|
| 28 |
+
'budget_unit': 'target calls (resets + actions)',
|
| 29 |
+
'completion': 'budget_exhausted'}
|
| 30 |
+
|
| 31 |
+
class BoundarySUL(SUL):
|
| 32 |
+
def __init__(self):
|
| 33 |
+
super().__init__()
|
| 34 |
+
self.word, self.outputs = [], []
|
| 35 |
+
self.active = False
|
| 36 |
+
|
| 37 |
+
def check(self):
|
| 38 |
+
if cancel and cancel():
|
| 39 |
+
raise Cancelled('Classical baseline cancelled')
|
| 40 |
+
# Same shared system-call cap as other policies: resets and steps both count.
|
| 41 |
+
if target.counts['target_calls'] >= budget:
|
| 42 |
+
raise _BudgetExhausted()
|
| 43 |
+
|
| 44 |
+
def pre(self):
|
| 45 |
+
self.post()
|
| 46 |
+
self.check()
|
| 47 |
+
target.reset()
|
| 48 |
+
self.word, self.outputs, self.active = [], [], True
|
| 49 |
+
|
| 50 |
+
def post(self):
|
| 51 |
+
if self.active:
|
| 52 |
+
probes.append({'word': list(self.word), 'outputs': list(self.outputs),
|
| 53 |
+
'complete': len(self.word) == len(self.outputs),
|
| 54 |
+
'survivors_before': None, 'survivors_after': None})
|
| 55 |
+
self.active = False
|
| 56 |
+
|
| 57 |
+
def step(self, letter):
|
| 58 |
+
if letter is None:
|
| 59 |
+
return None
|
| 60 |
+
self.check()
|
| 61 |
+
self.word.append(letter)
|
| 62 |
+
output = target.step(letter)
|
| 63 |
+
self.outputs.append(output)
|
| 64 |
+
return output
|
| 65 |
+
|
| 66 |
+
sul = BoundarySUL()
|
| 67 |
+
|
| 68 |
+
class BudgetOracle(Oracle):
|
| 69 |
+
def __init__(self):
|
| 70 |
+
super().__init__(list(range(space.machine.inputs)), sul)
|
| 71 |
+
self.rng = random.Random(432871)
|
| 72 |
+
|
| 73 |
+
def find_cex(self, hypothesis):
|
| 74 |
+
for _ in range(16):
|
| 75 |
+
remaining = budget - target.counts['target_calls'] - 1 # reserve this query's reset
|
| 76 |
+
if remaining <= 0:
|
| 77 |
+
break
|
| 78 |
+
word = tuple(self.rng.choice(self.alphabet) for _ in range(self.rng.randint(1, min(3, remaining))))
|
| 79 |
+
hypothesis.reset_to_initial()
|
| 80 |
+
sul.pre()
|
| 81 |
+
mismatched = False
|
| 82 |
+
for symbol in word:
|
| 83 |
+
actual = sul.step(symbol)
|
| 84 |
+
expected = hypothesis.step(symbol)
|
| 85 |
+
detail['equivalence_probe_steps'] += 1
|
| 86 |
+
if actual != expected:
|
| 87 |
+
mismatched = True
|
| 88 |
+
break
|
| 89 |
+
sul.post()
|
| 90 |
+
if mismatched:
|
| 91 |
+
return list(sul.word)
|
| 92 |
+
return None
|
| 93 |
+
|
| 94 |
+
learned = None
|
| 95 |
+
try:
|
| 96 |
+
learned = run_Lstar(list(range(space.machine.inputs)), sul, BudgetOracle(), 'mealy',
|
| 97 |
+
cache_and_non_det_check=False, print_level=0)
|
| 98 |
+
detail['model_complete'] = True
|
| 99 |
+
detail['completion'] = 'empirical_complete_approximate_oracle'
|
| 100 |
+
except _BudgetExhausted:
|
| 101 |
+
pass
|
| 102 |
+
except InterfaceError as exc:
|
| 103 |
+
error = {'code': exc.code, 'message': str(exc)}
|
| 104 |
+
detail['completion'] = 'interface_error'
|
| 105 |
+
finally:
|
| 106 |
+
sul.post()
|
| 107 |
+
|
| 108 |
+
evidence = fit_evidence(probes)
|
| 109 |
+
observed_survivors = space.survivors(evidence, anchors)
|
| 110 |
+
survivors = ()
|
| 111 |
+
if learned is not None:
|
| 112 |
+
states = list(learned.states)
|
| 113 |
+
indices = {id(state): i for i, state in enumerate(states)}
|
| 114 |
+
transitions = tuple(tuple((indices[id(state.transitions[symbol])], state.output_fun[symbol])
|
| 115 |
+
for symbol in range(space.machine.inputs)) for state in states)
|
| 116 |
+
raw_model = Machine('aalpy empirical hypothesis', transitions, space.machine.inputs,
|
| 117 |
+
space.machine.outputs, indices[id(learned.initial_state)])
|
| 118 |
+
matches = []
|
| 119 |
+
for adapter_id in observed_survivors:
|
| 120 |
+
result = compare_adapter(space.machine, space.adapters[adapter_id], raw_model)
|
| 121 |
+
detail['model_product_pairs'] += result['visited_pairs']
|
| 122 |
+
# Product comparison runs outside CandidateSpace; charge it separately.
|
| 123 |
+
detail['comparison_source_steps'] += result['source_steps']
|
| 124 |
+
detail['comparison_hypothesis_steps'] += result['target_model_steps']
|
| 125 |
+
if result['equivalent']:
|
| 126 |
+
matches.append(adapter_id)
|
| 127 |
+
survivors = tuple(matches)
|
| 128 |
+
detail['model_states'] = len(states)
|
| 129 |
+
# Preserve raw transcript and cumulative costs even if learning is incomplete.
|
| 130 |
+
prefix = []
|
| 131 |
+
for entry in probes:
|
| 132 |
+
before = len(space.survivors(fit_evidence(prefix), anchors))
|
| 133 |
+
prefix.append(entry)
|
| 134 |
+
entry['survivors_before'] = before
|
| 135 |
+
entry['survivors_after'] = len(space.survivors(fit_evidence(prefix), anchors))
|
| 136 |
+
detail['observed_survivors'] = len(observed_survivors)
|
| 137 |
+
return space, survivors, probes, error, detail
|
crosswire/certificates.py
ADDED
|
@@ -0,0 +1,512 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Independent finite public-evidence checking, without targets or private audit.
|
| 2 |
+
|
| 3 |
+
This module deliberately does not use CandidateSpace, its caches, its planner,
|
| 4 |
+
or the adaptive synthesizer/checker. A valid report checks supplied observations
|
| 5 |
+
under a declared model; it does not authenticate them or establish semantics.
|
| 6 |
+
"""
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
from itertools import permutations, product
|
| 10 |
+
import math
|
| 11 |
+
|
| 12 |
+
from .core import Contract, Machine
|
| 13 |
+
from .identity import canonical_hash
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
SCOPE = ('Finite public-model evidence only; conditional on target inclusion, stationarity, '
|
| 17 |
+
'deterministic reset and truthful anchors; does not authenticate observations, '
|
| 18 |
+
'validate private audit/runtime accounting, or establish real-world safety.')
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _int(value, limit, name):
|
| 22 |
+
if type(value) is not int or not 0 <= value < limit:
|
| 23 |
+
raise ValueError(f'Invalid {name}')
|
| 24 |
+
return value
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _sequence(value, limit, name):
|
| 28 |
+
if not isinstance(value, (list, tuple)) or len(value) > 512:
|
| 29 |
+
raise ValueError(f'Invalid or oversized {name}')
|
| 30 |
+
return tuple(_int(v, limit, name) for v in value)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _fraction(value, name, upper_inclusive=False):
|
| 34 |
+
if (type(value) not in (int, float) or value < 0
|
| 35 |
+
or (value > 1 if upper_inclusive else value >= 1) or not math.isfinite(value)):
|
| 36 |
+
raise ValueError(f'Invalid {name}: expected a finite numeric fraction')
|
| 37 |
+
return value
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def _adapters(machine, temporal):
|
| 41 |
+
pairs = list(product(permutations(range(machine.inputs)), permutations(range(machine.outputs))))
|
| 42 |
+
result = [{'id': i, 'input_map': list(a), 'output_map': list(b)} for i, (a, b) in enumerate(pairs)]
|
| 43 |
+
if temporal:
|
| 44 |
+
for a, b in pairs:
|
| 45 |
+
for initial in range(machine.outputs):
|
| 46 |
+
result.append({'id': len(result), 'input_map': list(a), 'output_map': list(b),
|
| 47 |
+
'delay': 1, 'init_output': initial})
|
| 48 |
+
return result
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def _trace(machine, adapter, word):
|
| 52 |
+
state = machine.initial
|
| 53 |
+
semantics, raw = [], []
|
| 54 |
+
buffered = adapter.get('init_output', 0)
|
| 55 |
+
for action in word:
|
| 56 |
+
state, semantic = machine.transitions[state][adapter['input_map'][action]]
|
| 57 |
+
semantics.append(semantic)
|
| 58 |
+
raw.append(adapter['output_map'][buffered if adapter.get('delay') else semantic])
|
| 59 |
+
buffered = semantic
|
| 60 |
+
return tuple(raw), tuple(semantics)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def _reset_consistency(probes):
|
| 64 |
+
"""Check equal reset action prefixes without assuming the reference model.
|
| 65 |
+
|
| 66 |
+
A trie shares action prefixes, so long observations do not require storing
|
| 67 |
+
a separate tuple for every prefix. Only the first conflict carries its word.
|
| 68 |
+
"""
|
| 69 |
+
nodes = [{'children': {}, 'observation': None}]
|
| 70 |
+
repeated, conflicts, first = 0, 0, None
|
| 71 |
+
for index, (word, outputs) in enumerate(probes):
|
| 72 |
+
current = 0
|
| 73 |
+
for offset, (action, output) in enumerate(zip(word, outputs)):
|
| 74 |
+
children = nodes[current]['children']
|
| 75 |
+
if action not in children:
|
| 76 |
+
children[action] = len(nodes)
|
| 77 |
+
nodes.append({'children': {}, 'observation': None})
|
| 78 |
+
current = children[action]
|
| 79 |
+
previous = nodes[current]['observation']
|
| 80 |
+
if previous is None:
|
| 81 |
+
nodes[current]['observation'] = (output, index)
|
| 82 |
+
continue
|
| 83 |
+
repeated += 1
|
| 84 |
+
if previous[0] != output:
|
| 85 |
+
conflicts += 1
|
| 86 |
+
if first is None:
|
| 87 |
+
first = {'probe_index': index, 'step': offset + 1,
|
| 88 |
+
'word_prefix': list(word[:offset + 1]),
|
| 89 |
+
'observed_output': output, 'previous_output': previous[0],
|
| 90 |
+
'previous_probe_index': previous[1]}
|
| 91 |
+
return {'status': 'REFUTED' if conflicts else 'UNKNOWN',
|
| 92 |
+
'repeated_prefix_observations': repeated,
|
| 93 |
+
'conflicting_prefix_observations': conflicts, 'first_conflict': first,
|
| 94 |
+
'scope': 'Equal action prefixes after reset must have equal raw outputs in a '
|
| 95 |
+
'stationary deterministic target with reliable resets. A conflict '
|
| 96 |
+
'does not identify drift, stochasticity or reset failure as its cause; '
|
| 97 |
+
'absence of a conflict does not establish those assumptions.'}
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def _model_diagnostics(machine, anchors, probes, temporal):
|
| 101 |
+
"""Replay public forecasts using only evidence preceding each observation.
|
| 102 |
+
|
| 103 |
+
Every enabled class is checked from the beginning, independently of the
|
| 104 |
+
acquisition class or a diagnostic baseline's selected model. Predictions
|
| 105 |
+
are computed once per live model and observed word, never once per prefix.
|
| 106 |
+
No private target information or CandidateSpace machinery enters here.
|
| 107 |
+
"""
|
| 108 |
+
enabled = ['static', 'temporal'] if temporal else ['static']
|
| 109 |
+
adapters = _adapters(machine, temporal)
|
| 110 |
+
classes, beliefs = {}, {}
|
| 111 |
+
total_symbols = sum(len(outputs) for _, outputs in probes)
|
| 112 |
+
for name in enabled:
|
| 113 |
+
belief = {a['id'] for a in adapters if name == 'temporal' or not a.get('delay')}
|
| 114 |
+
model_count = len(belief)
|
| 115 |
+
first_refutation = None
|
| 116 |
+
for raw, semantic in anchors:
|
| 117 |
+
before = len(belief)
|
| 118 |
+
belief = {i for i in belief if adapters[i]['output_map'][semantic] == raw}
|
| 119 |
+
if before and not belief:
|
| 120 |
+
first_refutation = {'kind': 'anchor', 'raw': raw, 'semantic': semantic,
|
| 121 |
+
'survivors_before': before, 'survivors_after': 0}
|
| 122 |
+
beliefs[name] = belief
|
| 123 |
+
classes[name] = {'inclusion_status': 'UNKNOWN' if belief else 'REFUTED',
|
| 124 |
+
'model_count': model_count, 'models_after_anchors': len(belief),
|
| 125 |
+
'candidate_count': len(belief), 'scored_symbols': 0,
|
| 126 |
+
'unscored_symbols': total_symbols,
|
| 127 |
+
'cumulative_surprisal_bits': 0.0,
|
| 128 |
+
'mean_symbol_surprisal_bits': 0.0,
|
| 129 |
+
'martingale_e_value': 1.0,
|
| 130 |
+
'martingale_log10': 0.0,
|
| 131 |
+
'martingale_refuted_alpha_05': False,
|
| 132 |
+
'martingale_refuted_alpha_01': False,
|
| 133 |
+
'first_refutation': first_refutation, 'events': []}
|
| 134 |
+
for index, (word, outputs) in enumerate(probes):
|
| 135 |
+
if not outputs:
|
| 136 |
+
continue
|
| 137 |
+
# Static models are shared with the temporal envelope. Trace their
|
| 138 |
+
# union once for this observed word, without quadratic prefix replay.
|
| 139 |
+
live_ids = set().union(*beliefs.values())
|
| 140 |
+
predictions = {i: _trace(machine, adapters[i], word)[0] for i in live_ids}
|
| 141 |
+
for name in enabled:
|
| 142 |
+
belief, diagnostic = beliefs[name], classes[name]
|
| 143 |
+
# Full deterministic traces may be computed up front, but support
|
| 144 |
+
# at offset t is counted before filtering on observation t.
|
| 145 |
+
for offset, (action, output) in enumerate(zip(word, outputs)):
|
| 146 |
+
before = len(belief)
|
| 147 |
+
counts = {}
|
| 148 |
+
for i in belief:
|
| 149 |
+
prediction = predictions[i][offset]
|
| 150 |
+
counts[prediction] = counts.get(prediction, 0) + 1
|
| 151 |
+
supported = output in counts
|
| 152 |
+
belief = {i for i in belief if predictions[i][offset] == output}
|
| 153 |
+
event = {'probe_index': index, 'step': offset + 1, 'action': action,
|
| 154 |
+
'predicted_output_counts': {str(raw): counts[raw] for raw in sorted(counts)},
|
| 155 |
+
'observed_output': output, 'survivors_before': before,
|
| 156 |
+
'survivors_after': len(belief),
|
| 157 |
+
'prediction_status': ('SUPPORTED' if supported else 'UNSUPPORTED')
|
| 158 |
+
if before else 'UNSCORED'}
|
| 159 |
+
diagnostic['events'].append(event)
|
| 160 |
+
if before:
|
| 161 |
+
diagnostic['scored_symbols'] += 1
|
| 162 |
+
prob = counts[output] / before if supported else 1.0 / (before + 1)
|
| 163 |
+
diagnostic['cumulative_surprisal_bits'] += -math.log2(prob)
|
| 164 |
+
if supported and counts[output] > 0:
|
| 165 |
+
e_step = (1.0 / machine.outputs) / (counts[output] / before)
|
| 166 |
+
else:
|
| 167 |
+
e_step = 1e6
|
| 168 |
+
diagnostic['martingale_e_value'] = diagnostic.get('martingale_e_value', 1.0) * e_step
|
| 169 |
+
if not belief:
|
| 170 |
+
diagnostic['first_refutation'] = {'kind': 'probe', **event,
|
| 171 |
+
'word_prefix': list(word[:offset + 1])}
|
| 172 |
+
beliefs[name] = belief
|
| 173 |
+
diagnostic['cumulative_surprisal_bits'] = round(diagnostic['cumulative_surprisal_bits'], 4)
|
| 174 |
+
diagnostic['mean_symbol_surprisal_bits'] = round(diagnostic['cumulative_surprisal_bits'] / diagnostic['scored_symbols'], 4) if diagnostic['scored_symbols'] else 0.0
|
| 175 |
+
m_val = diagnostic.get('martingale_e_value', 1.0)
|
| 176 |
+
diagnostic['martingale_e_value'] = round(m_val, 4)
|
| 177 |
+
diagnostic['martingale_log10'] = round(math.log10(max(m_val, 1e-12)), 4) if m_val > 0 else -12.0
|
| 178 |
+
diagnostic['martingale_refuted_alpha_05'] = bool(m_val >= 20.0)
|
| 179 |
+
diagnostic['martingale_refuted_alpha_01'] = bool(m_val >= 100.0)
|
| 180 |
+
diagnostic.update(inclusion_status='UNKNOWN' if belief else 'REFUTED',
|
| 181 |
+
candidate_count=len(belief),
|
| 182 |
+
unscored_symbols=total_symbols - diagnostic['scored_symbols'])
|
| 183 |
+
return {'schema': 'crosswire-model-diagnostics-v1',
|
| 184 |
+
'inclusion_status': 'UNKNOWN' if any(c['candidate_count'] for c in classes.values()) else 'REFUTED',
|
| 185 |
+
'enabled_classes': enabled, 'classes': classes,
|
| 186 |
+
'observed_symbols': total_symbols,
|
| 187 |
+
'reset_consistency': _reset_consistency(probes),
|
| 188 |
+
'scope': 'Calibration observations and supplied anchors only. UNKNOWN means '
|
| 189 |
+
'not refuted by the observed evidence, including when a task is admitted; '
|
| 190 |
+
'it never establishes actual model inclusion. REFUTED means every enabled '
|
| 191 |
+
'finite class is inconsistent. Each forecast uses only earlier evidence; '
|
| 192 |
+
'outputs after class refutation are unscored. No extra target calls are made.',
|
| 193 |
+
'blind_spots': [
|
| 194 |
+
'Raw-equivalent semantic corruption, hidden privileged paths and semantic-only drift can remain undetected.',
|
| 195 |
+
'Unqueried action branches and behavior deeper than observed prefixes remain unknown.',
|
| 196 |
+
'Agreement does not authenticate observations, reset reliability or semantic anchors.',
|
| 197 |
+
'Prediction support counts are finite model counts, not calibrated probabilities or real-world safety bounds.',
|
| 198 |
+
]}
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
def check_result(result: dict) -> dict:
|
| 202 |
+
"""Recompute an evidence certificate; never trust a stored certificate/hash.
|
| 203 |
+
|
| 204 |
+
Diagnostic baselines may deliberately restrict the evidence-compatible set.
|
| 205 |
+
Their report validates that declaration but makes no admission safety claim.
|
| 206 |
+
Unresolved means no positive claim was made, not a proof of impossibility.
|
| 207 |
+
"""
|
| 208 |
+
report = {'schema': 'crosswire-evidence-certificate-v1', 'valid': False,
|
| 209 |
+
'kind': 'unresolved', 'scope': SCOPE}
|
| 210 |
+
try:
|
| 211 |
+
if not isinstance(result, dict) or result.get('schema') != 'crosswire-result-v1':
|
| 212 |
+
raise ValueError('Expected one completed run result')
|
| 213 |
+
machine = Machine.from_dict(result['reference'])
|
| 214 |
+
contract = Contract.from_dict(result['contract'])
|
| 215 |
+
if machine.inputs > 3 or machine.outputs > 4 or len(machine.transitions) > 128:
|
| 216 |
+
raise ValueError('Reference exceeds the bounded checker alphabet/state limits')
|
| 217 |
+
if max(contract.goal, contract.hazard) >= machine.outputs:
|
| 218 |
+
raise ValueError('Contract output outside reference alphabet')
|
| 219 |
+
decision, config = result['decision'], result['configuration']
|
| 220 |
+
if not isinstance(decision, dict) or not isinstance(config, dict):
|
| 221 |
+
raise ValueError('Invalid configuration or decision')
|
| 222 |
+
candidate_backend = config.get('candidate_backend', 'flat')
|
| 223 |
+
if candidate_backend not in ('flat', 'factored'):
|
| 224 |
+
raise ValueError('Unknown candidate backend declaration')
|
| 225 |
+
report['declared_candidate_backend'] = candidate_backend
|
| 226 |
+
safety_mode = config.get('probe_safety', 'unrestricted')
|
| 227 |
+
if safety_mode not in ('unrestricted', 'model_safe'):
|
| 228 |
+
raise ValueError('Unknown calibration safety mode')
|
| 229 |
+
tolerance = _fraction(config.get('risk_tolerance', 0.0), 'configured risk tolerance')
|
| 230 |
+
if tolerance and config.get('admission') != 'risk_controlled':
|
| 231 |
+
raise ValueError('Positive risk tolerance requires risk_controlled admission')
|
| 232 |
+
if safety_mode == 'model_safe' and config.get('policy') == 'aalpy':
|
| 233 |
+
raise ValueError('Classical learner does not support model-safe calibration')
|
| 234 |
+
temporal = config.get('temporal', False)
|
| 235 |
+
if type(temporal) is not bool:
|
| 236 |
+
raise ValueError('Invalid temporal setting')
|
| 237 |
+
model_class = decision.get('escalation', {}).get('model_class', 'temporal' if temporal else 'static')
|
| 238 |
+
if model_class not in ('static', 'temporal'):
|
| 239 |
+
raise ValueError('Unknown model class')
|
| 240 |
+
if temporal and model_class != 'temporal':
|
| 241 |
+
raise ValueError('Declared model class conflicts with configuration')
|
| 242 |
+
if model_class == 'temporal' and not (temporal or config.get('escalate') is True):
|
| 243 |
+
raise ValueError('Temporal class was not enabled')
|
| 244 |
+
adapters = _adapters(machine, model_class == 'temporal')
|
| 245 |
+
all_ids = set(range(len(adapters)))
|
| 246 |
+
constraints = []
|
| 247 |
+
normalized_anchors = []
|
| 248 |
+
anchors = result.get('anchors', {})
|
| 249 |
+
if not isinstance(anchors, dict):
|
| 250 |
+
raise ValueError('Anchors must be an object')
|
| 251 |
+
for raw, semantic in anchors.items():
|
| 252 |
+
if isinstance(raw, str) and raw in {str(i) for i in range(machine.outputs)}:
|
| 253 |
+
raw = int(raw)
|
| 254 |
+
raw = _int(raw, machine.outputs, 'anchor raw output')
|
| 255 |
+
semantic = _int(semantic, machine.outputs, 'anchor semantic output')
|
| 256 |
+
normalized_anchors.append((raw, semantic))
|
| 257 |
+
allowed = {a['id'] for a in adapters if a['output_map'][semantic] == raw}
|
| 258 |
+
constraints.append(({'kind': 'anchor', 'raw': raw, 'semantic': semantic}, allowed))
|
| 259 |
+
# Canonical order ensures a certificate survives JSON key normalization.
|
| 260 |
+
constraints.sort(key=lambda pair: (pair[0]['raw'], pair[0]['semantic']))
|
| 261 |
+
normalized_anchors.sort()
|
| 262 |
+
safety_temporal = temporal or config.get('escalate') is True
|
| 263 |
+
safety_adapters = (_adapters(machine, safety_temporal) if safety_mode == 'model_safe' else [])
|
| 264 |
+
safety_belief = set(range(len(safety_adapters)))
|
| 265 |
+
for raw, semantic in anchors.items():
|
| 266 |
+
safety_belief = {i for i in safety_belief if safety_adapters[i]['output_map'][semantic] == int(raw)}
|
| 267 |
+
probes = result['probes']
|
| 268 |
+
if not isinstance(probes, list) or len(probes) > 512:
|
| 269 |
+
raise ValueError('Invalid or oversized probe collection')
|
| 270 |
+
observed_probes = []
|
| 271 |
+
experiment_checks = []
|
| 272 |
+
spent_calls = 0
|
| 273 |
+
for index, probe in enumerate(probes):
|
| 274 |
+
if not isinstance(probe, dict) or type(probe.get('complete')) is not bool:
|
| 275 |
+
raise ValueError('Probe must declare boolean completeness')
|
| 276 |
+
word = _sequence(probe['word'], machine.inputs, 'probe word')
|
| 277 |
+
outputs = _sequence(probe['outputs'], machine.outputs, 'probe outputs')
|
| 278 |
+
if len(outputs) > len(word) or (probe['complete'] and len(word) != len(outputs)):
|
| 279 |
+
raise ValueError('Probe word/output lengths differ')
|
| 280 |
+
planning = probe.get('planning')
|
| 281 |
+
if config.get('policy') == 'lookahead':
|
| 282 |
+
from .experiment_checks import verify_experiment_tree
|
| 283 |
+
if not isinstance(planning, dict):
|
| 284 |
+
raise ValueError('Lookahead probes require independently checked planning evidence')
|
| 285 |
+
call_cap = _int(config.get('budget'), 513, 'lookahead call cap')
|
| 286 |
+
remaining = call_cap - spent_calls
|
| 287 |
+
if type(planning.get('remaining_calls')) is not int or planning['remaining_calls'] != remaining:
|
| 288 |
+
raise ValueError('Experiment planning budget differs from prior target calls')
|
| 289 |
+
if (type(planning.get('node_limit')) is not int or planning['node_limit'] != 2048
|
| 290 |
+
or type(planning.get('depth_limit')) is not int or planning['depth_limit'] != 2
|
| 291 |
+
or type(planning.get('expanded_nodes')) is not int
|
| 292 |
+
or not 0 <= planning['expanded_nodes'] <= planning['node_limit']
|
| 293 |
+
or type(planning.get('search_complete')) is not bool
|
| 294 |
+
or planning.get('selection') not in ('lookahead', 'one_query_fallback')):
|
| 295 |
+
raise ValueError('Invalid bounded experiment search metadata')
|
| 296 |
+
prior = [{'word': list(w), 'outputs': list(o), 'complete': True}
|
| 297 |
+
for w, o in observed_probes if o]
|
| 298 |
+
static = _adapters(machine, False)
|
| 299 |
+
static_compatible = [a for a in static
|
| 300 |
+
if all(a['output_map'][semantic] == raw for raw, semantic in normalized_anchors)
|
| 301 |
+
and all(_trace(machine, a, w)[0] == o for w, o in observed_probes)]
|
| 302 |
+
planning_temporal = temporal or (config.get('escalate') is True and (
|
| 303 |
+
safety_mode == 'model_safe' or not static_compatible))
|
| 304 |
+
if planning.get('model_class') != ('temporal' if planning_temporal else 'static'):
|
| 305 |
+
raise ValueError('Experiment class differs from enabled pre-probe evidence')
|
| 306 |
+
if type(planning.get('safe_only')) is not bool or planning['safe_only'] != (safety_mode == 'model_safe'):
|
| 307 |
+
raise ValueError('Experiment safety declaration differs from configuration')
|
| 308 |
+
tree = planning.get('tree')
|
| 309 |
+
if (not isinstance(tree, dict) or tree.get('kind') != 'query'
|
| 310 |
+
or canonical_hash(tree.get('word')) != canonical_hash(list(word))
|
| 311 |
+
or canonical_hash(planning.get('word')) != canonical_hash(list(word))):
|
| 312 |
+
raise ValueError('Dispatched query differs from the proposed experiment root')
|
| 313 |
+
checked = verify_experiment_tree(result['reference'], result['contract'], tree,
|
| 314 |
+
temporal=planning_temporal, evidence=prior,
|
| 315 |
+
anchors=anchors, budget=remaining,
|
| 316 |
+
safe_only=safety_mode == 'model_safe')
|
| 317 |
+
if not checked['valid']:
|
| 318 |
+
raise ValueError('Invalid experiment tree: ' + checked['reason'])
|
| 319 |
+
expected_objective = {key: checked[key] for key in (
|
| 320 |
+
'ready_models', 'largest_unresolved_belief', 'terminal_squared_beliefs', 'worst_case_calls')}
|
| 321 |
+
if canonical_hash(planning.get('objective')) != canonical_hash(expected_objective):
|
| 322 |
+
raise ValueError('Experiment objective differs from independently derived branches')
|
| 323 |
+
if canonical_hash(planning.get('certificate')) != canonical_hash(checked):
|
| 324 |
+
raise ValueError('Stored experiment certificate differs from independent checking')
|
| 325 |
+
experiment_checks.append(checked)
|
| 326 |
+
elif planning is not None:
|
| 327 |
+
raise ValueError('Unexpected experiment tree on a different probe policy')
|
| 328 |
+
spent_calls += 1 + len(word)
|
| 329 |
+
observed_probes.append((word[:len(outputs)], outputs))
|
| 330 |
+
if safety_mode == 'model_safe':
|
| 331 |
+
if not word or not safety_belief or any(
|
| 332 |
+
contract.hazard in _trace(machine, safety_adapters[i], word)[1] for i in safety_belief):
|
| 333 |
+
raise ValueError('Unsafe calibration query under a pre-probe surviving model')
|
| 334 |
+
declared = probe.get('safety')
|
| 335 |
+
expected_safety = {'model_class': 'temporal' if safety_temporal else 'static',
|
| 336 |
+
'candidate_count': len(safety_belief), 'model_safe': True}
|
| 337 |
+
if declared is not None and canonical_hash(declared) != canonical_hash(expected_safety):
|
| 338 |
+
raise ValueError('Calibration safety metadata differs from pre-probe evidence')
|
| 339 |
+
if outputs:
|
| 340 |
+
safety_belief = {i for i in safety_belief
|
| 341 |
+
if _trace(machine, safety_adapters[i], word[:len(outputs)])[0] == outputs}
|
| 342 |
+
if not outputs:
|
| 343 |
+
continue
|
| 344 |
+
word = word[:len(outputs)]
|
| 345 |
+
allowed = {a['id'] for a in adapters if _trace(machine, a, word)[0] == outputs}
|
| 346 |
+
constraints.append(({'kind': 'probe', 'index': index, 'word': list(word),
|
| 347 |
+
'outputs': list(outputs)}, allowed))
|
| 348 |
+
report['calibration_safety'] = {
|
| 349 |
+
'mode': safety_mode, 'checked_probes': len(probes) if safety_mode == 'model_safe' else 0,
|
| 350 |
+
'safe': True if safety_mode == 'model_safe' else None,
|
| 351 |
+
'model_class': 'temporal' if safety_temporal else 'static',
|
| 352 |
+
'scope': 'Entire dispatched words checked before their outputs, including interrupted suffixes. '
|
| 353 |
+
'Conditional on enabled model inclusion, stationarity, reset semantics and truthful anchors; '
|
| 354 |
+
'unrestricted calibration makes no safety claim.'}
|
| 355 |
+
if config.get('policy') == 'lookahead':
|
| 356 |
+
report['experiment_planning'] = {
|
| 357 |
+
'checked_trees': len(experiment_checks), 'checks': experiment_checks,
|
| 358 |
+
'scope': 'Hypothetical finite-model branches, readiness, guards and reset/action costs only; '
|
| 359 |
+
'does not certify search optimality, target execution or semantic grounding.'}
|
| 360 |
+
report['model_diagnostics'] = _model_diagnostics(
|
| 361 |
+
machine, normalized_anchors, observed_probes, safety_temporal)
|
| 362 |
+
|
| 363 |
+
survivors = set(all_ids)
|
| 364 |
+
elimination = []
|
| 365 |
+
for evidence, allowed in constraints:
|
| 366 |
+
before = len(survivors)
|
| 367 |
+
survivors &= allowed
|
| 368 |
+
elimination.append({**evidence, 'before': before, 'after': len(survivors)})
|
| 369 |
+
expected = [a for a in adapters if a['id'] in survivors]
|
| 370 |
+
candidates = result['candidates']
|
| 371 |
+
if not isinstance(candidates, list):
|
| 372 |
+
raise ValueError('Candidates must be a list')
|
| 373 |
+
# Compare canonical JSON so bool/int aliases cannot pass Python equality.
|
| 374 |
+
baseline = config.get('policy') == 'aalpy' and config.get('admission') != 'oracle'
|
| 375 |
+
if baseline:
|
| 376 |
+
ids = []
|
| 377 |
+
for candidate in candidates:
|
| 378 |
+
identifier = _int(candidate['id'], len(adapters), 'candidate id')
|
| 379 |
+
if identifier not in survivors or canonical_hash(candidate) != canonical_hash(adapters[identifier]):
|
| 380 |
+
raise ValueError('Classical-model candidate contradicts public evidence')
|
| 381 |
+
ids.append(identifier)
|
| 382 |
+
if ids != sorted(set(ids)):
|
| 383 |
+
raise ValueError('Duplicate or unordered candidate list')
|
| 384 |
+
elif canonical_hash(candidates) != canonical_hash(expected):
|
| 385 |
+
raise ValueError('Declared candidates are not the complete evidence-compatible set')
|
| 386 |
+
_int(decision['candidate_count'], len(adapters) + 1, 'candidate count')
|
| 387 |
+
if decision['candidate_count'] != len(candidates):
|
| 388 |
+
raise ValueError('Decision candidate count does not match the evidence')
|
| 389 |
+
if type(decision['admitted']) is not bool:
|
| 390 |
+
raise ValueError('Admission must be boolean')
|
| 391 |
+
admission = config.get('admission', 'robust')
|
| 392 |
+
if admission not in ('robust', 'adaptive', 'unique', 'one_best', 'fit', 'identity', 'oracle', 'abstain', 'risk_controlled'):
|
| 393 |
+
raise ValueError('Unknown admission rule')
|
| 394 |
+
diagnostic = baseline or admission in ('one_best', 'fit', 'identity', 'oracle')
|
| 395 |
+
report.update(model_class=model_class, checked_adapters=len(adapters), candidate_count=len(survivors),
|
| 396 |
+
evidence_count=len(constraints), elimination=elimination)
|
| 397 |
+
status = decision['status']
|
| 398 |
+
if status not in ('IDENTIFIED', 'TASK_ADMISSIBLE', 'ADAPTIVE_ADMISSIBLE', 'RISK_CONTROLLED_ADMISSIBLE',
|
| 399 |
+
'FAMILY_CONTRADICTED', 'UNRESOLVED', 'SEARCH_LIMIT', 'INTERFACE_ERROR', 'ORACLE_DIAGNOSTIC'):
|
| 400 |
+
raise ValueError('Unknown decision status')
|
| 401 |
+
if admission == 'risk_controlled' and not decision['admitted'] and 'risk_metrics' in decision:
|
| 402 |
+
metrics = decision['risk_metrics']
|
| 403 |
+
if _fraction(metrics['tolerance'], 'reported risk tolerance') != tolerance:
|
| 404 |
+
raise ValueError('Reported risk tolerance differs from configuration')
|
| 405 |
+
if metrics['hazard_risk'] is not None or metrics['goal_rate'] is not None or decision.get('word') is not None:
|
| 406 |
+
raise ValueError('No selected plan: hazard/goal fractions must be undefined')
|
| 407 |
+
if diagnostic:
|
| 408 |
+
report.update(kind='diagnostic', reason='Diagnostic baseline; no all-candidate admission guarantee')
|
| 409 |
+
elif decision['admitted']:
|
| 410 |
+
if not survivors or admission == 'abstain':
|
| 411 |
+
raise ValueError('Empty or abstaining belief cannot certify admission')
|
| 412 |
+
if admission == 'unique' and len(survivors) != 1:
|
| 413 |
+
raise ValueError('Unique admission requires one surviving adapter')
|
| 414 |
+
if status not in ('IDENTIFIED', 'TASK_ADMISSIBLE', 'ADAPTIVE_ADMISSIBLE', 'RISK_CONTROLLED_ADMISSIBLE'):
|
| 415 |
+
raise ValueError('Admission conflicts with decision status')
|
| 416 |
+
checked_nodes = 0
|
| 417 |
+
|
| 418 |
+
def check_prefix(word, belief, terminal=False):
|
| 419 |
+
traces = {i: _trace(machine, adapters[i], word) for i in belief}
|
| 420 |
+
for _, semantic in traces.values():
|
| 421 |
+
if contract.hazard in semantic:
|
| 422 |
+
raise ValueError('Admitted action risks a hazard under a surviving model')
|
| 423 |
+
if terminal and (not semantic or semantic[-1] != contract.goal):
|
| 424 |
+
raise ValueError('Admitted terminal does not achieve every surviving goal')
|
| 425 |
+
return traces
|
| 426 |
+
|
| 427 |
+
def visit(node, belief, prefix):
|
| 428 |
+
nonlocal checked_nodes
|
| 429 |
+
checked_nodes += 1
|
| 430 |
+
if checked_nodes > 20000 or not isinstance(node, dict):
|
| 431 |
+
raise ValueError('Invalid or oversized policy')
|
| 432 |
+
if set(node) == {'stop'} and node['stop'] is True:
|
| 433 |
+
check_prefix(prefix, belief, terminal=True)
|
| 434 |
+
return
|
| 435 |
+
if set(node) != {'action', 'branches'} or len(prefix) >= contract.horizon:
|
| 436 |
+
raise ValueError('Malformed policy or exhausted horizon')
|
| 437 |
+
action = _int(node['action'], machine.inputs, 'policy action')
|
| 438 |
+
word = prefix + (action,)
|
| 439 |
+
partitions = {}
|
| 440 |
+
for i, (raw, _) in check_prefix(word, belief).items():
|
| 441 |
+
partitions.setdefault(str(raw[-1]), set()).add(i)
|
| 442 |
+
if not isinstance(node['branches'], dict) or set(node['branches']) != set(partitions):
|
| 443 |
+
raise ValueError('Policy branches do not cover exactly the possible outputs')
|
| 444 |
+
for raw, members in sorted(partitions.items()):
|
| 445 |
+
visit(node['branches'][raw], members, word)
|
| 446 |
+
|
| 447 |
+
if admission == 'adaptive':
|
| 448 |
+
if decision.get('word') is not None:
|
| 449 |
+
raise ValueError('Adaptive policy must not claim a fixed word')
|
| 450 |
+
visit(decision['policy'], survivors, ())
|
| 451 |
+
report.update(kind='admission', checked_policy_nodes=checked_nodes,
|
| 452 |
+
reason='Every surviving interpretation satisfies the declared finite contract')
|
| 453 |
+
elif admission == 'risk_controlled':
|
| 454 |
+
risk_metrics = decision.get('risk_metrics', {})
|
| 455 |
+
declared_tolerance = _fraction(risk_metrics['tolerance'], 'reported risk tolerance')
|
| 456 |
+
if declared_tolerance != tolerance:
|
| 457 |
+
raise ValueError('Reported risk tolerance differs from configuration')
|
| 458 |
+
word = _sequence(decision['word'], machine.inputs, 'admitted word')
|
| 459 |
+
if not 1 <= len(word) <= contract.horizon:
|
| 460 |
+
raise ValueError('Admitted word exceeds the contract horizon')
|
| 461 |
+
traces = {i: _trace(machine, adapters[i], word) for i in survivors}
|
| 462 |
+
h_count = sum(contract.hazard in semantic for _, semantic in traces.values())
|
| 463 |
+
g_count = sum(bool(semantic and semantic[-1] == contract.goal and contract.hazard not in semantic)
|
| 464 |
+
for _, semantic in traces.values())
|
| 465 |
+
h_risk = h_count / len(survivors)
|
| 466 |
+
g_rate = g_count / len(survivors)
|
| 467 |
+
reported_hazard = _fraction(risk_metrics['hazard_risk'], 'reported hazard fraction', True)
|
| 468 |
+
reported_goal = _fraction(risk_metrics['goal_rate'], 'reported goal fraction', True)
|
| 469 |
+
if reported_hazard != h_risk or reported_goal != g_rate:
|
| 470 |
+
raise ValueError('Reported hazard/goal fractions differ from independently computed candidate fractions')
|
| 471 |
+
if 'candidate_count' in risk_metrics and (
|
| 472 |
+
type(risk_metrics['candidate_count']) is not int or risk_metrics['candidate_count'] != len(survivors)):
|
| 473 |
+
raise ValueError('Risk metric candidate count differs from evidence')
|
| 474 |
+
if h_risk > tolerance or (len(survivors) - g_count) / len(survivors) > tolerance:
|
| 475 |
+
raise ValueError(f'Admitted word violates risk tolerance: hazard_risk={h_risk}, goal_rate={g_rate}, tolerance={tolerance}')
|
| 476 |
+
report.update(kind='risk_controlled' if tolerance > 0 else 'admission',
|
| 477 |
+
risk_metrics={'hazard_risk': h_risk, 'goal_rate': g_rate, 'tolerance': tolerance},
|
| 478 |
+
reason=f'Uniform candidate fractions checked: hazard={h_risk:.4f} <= {tolerance}, goal={g_rate:.4f}; '
|
| 479 |
+
'not a calibrated real-world risk bound')
|
| 480 |
+
else:
|
| 481 |
+
word = _sequence(decision['word'], machine.inputs, 'admitted word')
|
| 482 |
+
if not 1 <= len(word) <= contract.horizon:
|
| 483 |
+
raise ValueError('Admitted word exceeds the contract horizon')
|
| 484 |
+
check_prefix(word, survivors, terminal=True)
|
| 485 |
+
report.update(kind='admission', checked_policy_nodes=checked_nodes,
|
| 486 |
+
reason='Every surviving interpretation satisfies the declared finite contract')
|
| 487 |
+
elif status == 'FAMILY_CONTRADICTED':
|
| 488 |
+
if survivors:
|
| 489 |
+
raise ValueError('Contradiction claimed while consistent adapters remain')
|
| 490 |
+
# Deletion-minimal, not globally minimum-cardinality. Removing any one
|
| 491 |
+
# retained constraint makes this core satisfiable in the finite class.
|
| 492 |
+
core = list(range(len(constraints)))
|
| 493 |
+
for remove in reversed(core[:]):
|
| 494 |
+
remaining = set(all_ids)
|
| 495 |
+
for index in core:
|
| 496 |
+
if index != remove:
|
| 497 |
+
remaining &= constraints[index][1]
|
| 498 |
+
if not remaining:
|
| 499 |
+
core.remove(remove)
|
| 500 |
+
report.update(kind='contradiction', contradiction_core=[constraints[i][0] for i in core],
|
| 501 |
+
core_irreducible=True, reason='Public evidence refutes the entire declared finite class')
|
| 502 |
+
else:
|
| 503 |
+
if status in ('IDENTIFIED', 'TASK_ADMISSIBLE', 'ADAPTIVE_ADMISSIBLE', 'RISK_CONTROLLED_ADMISSIBLE'):
|
| 504 |
+
raise ValueError('Positive decision status conflicts with abstention')
|
| 505 |
+
report.update(kind='unresolved', reason='No positive admission claim; not a proof of impossibility')
|
| 506 |
+
report['binding_hash'] = canonical_hash({key: result.get(key) for key in
|
| 507 |
+
('reference', 'contract', 'anchors', 'probes', 'candidates',
|
| 508 |
+
'decision', 'configuration', 'target_id')})
|
| 509 |
+
report['valid'] = True
|
| 510 |
+
except (KeyError, TypeError, ValueError, AttributeError, IndexError, RecursionError, OverflowError) as exc:
|
| 511 |
+
report.update(valid=False, reason=str(exc))
|
| 512 |
+
return report
|
crosswire/cli.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Command line entry points share the browser's exact scientific runtime."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import argparse
|
| 5 |
+
from dataclasses import asdict
|
| 6 |
+
import json
|
| 7 |
+
import os
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import sys
|
| 10 |
+
|
| 11 |
+
from .study import ADMISSIONS, EXECUTION_MODES, POLICIES, PROBE_SAFETY_MODES, RECOVERY_STRATEGIES, RunConfig, run, run_study
|
| 12 |
+
from .fixtures import catalog
|
| 13 |
+
from .backends import CANDIDATE_BACKENDS
|
| 14 |
+
from .receipts import check_envelope, pack, replay
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _write(path, value):
|
| 18 |
+
path = Path(path)
|
| 19 |
+
path.parent.mkdir(parents=True, exist_ok=True)
|
| 20 |
+
temp = path.with_name(path.name + '.tmp')
|
| 21 |
+
temp.write_text(json.dumps(value, indent=2, allow_nan=False) + '\n', encoding='utf-8')
|
| 22 |
+
os.replace(temp, path)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def main(argv=None):
|
| 26 |
+
parser = argparse.ArgumentParser(prog='crosswire', description='Finite behavioral interoperability laboratory')
|
| 27 |
+
commands = parser.add_subparsers(dest='command', required=True)
|
| 28 |
+
one = commands.add_parser('run')
|
| 29 |
+
defaults = asdict(RunConfig())
|
| 30 |
+
for name in ('family', 'implementation', 'split', 'policy', 'admission', 'execution_mode', 'probe_safety', 'recovery_strategy'):
|
| 31 |
+
choices = {'family': [f['id'] for f in catalog()], 'implementation': ['table', 'procedural'],
|
| 32 |
+
'split': ['development', 'evaluation'], 'policy': POLICIES, 'admission': ADMISSIONS,
|
| 33 |
+
'execution_mode': EXECUTION_MODES, 'probe_safety': PROBE_SAFETY_MODES,
|
| 34 |
+
'recovery_strategy': RECOVERY_STRATEGIES}[name]
|
| 35 |
+
flags = ['--' + name]
|
| 36 |
+
if '_' in name:
|
| 37 |
+
flags.append('--' + name.replace('_', '-'))
|
| 38 |
+
one.add_argument(*flags, default=defaults[name], choices=choices)
|
| 39 |
+
for name in ('seed', 'budget', 'horizon'):
|
| 40 |
+
one.add_argument('--' + name, default=defaults[name], type=int)
|
| 41 |
+
one.add_argument('--instance-seed', '--instance_seed', type=int, default=defaults['instance_seed'],
|
| 42 |
+
help='Structural seed for generated_protocol; --seed changes opaque labels only')
|
| 43 |
+
one.add_argument('--candidate-backend', '--candidate_backend', choices=CANDIDATE_BACKENDS,
|
| 44 |
+
default=defaults['candidate_backend'],
|
| 45 |
+
help='Candidate representation: exhaustive flat or lazy factored (same finite model class)')
|
| 46 |
+
one.add_argument('--anchors', action='store_true')
|
| 47 |
+
one.add_argument('--temporal', action='store_true')
|
| 48 |
+
one.add_argument('--escalate', action='store_true')
|
| 49 |
+
one.add_argument('--risk-tolerance', '--risk_tolerance', type=float, default=defaults['risk_tolerance'],
|
| 50 |
+
help='Allowed fraction of uniformly weighted surviving models (not calibrated target risk)')
|
| 51 |
+
one.add_argument('--fault-schedule', '--fault_schedule', type=json.loads, default=defaults['fault_schedule'],
|
| 52 |
+
help='Fault schedule JSON dict (e.g. {"step": 2, "type": "sensor_drift"})')
|
| 53 |
+
one.add_argument('--out', type=Path)
|
| 54 |
+
study = commands.add_parser('study')
|
| 55 |
+
study.add_argument('--config', type=Path, required=True)
|
| 56 |
+
study.add_argument('--out', type=Path, required=True)
|
| 57 |
+
for name in ('verify', 'replay', 'check'):
|
| 58 |
+
commands.add_parser(name).add_argument('receipt', type=Path)
|
| 59 |
+
server = commands.add_parser('serve')
|
| 60 |
+
server.add_argument('--port', type=int, default=8781)
|
| 61 |
+
server.add_argument('--open', action='store_true')
|
| 62 |
+
commands.add_parser('catalog')
|
| 63 |
+
args = parser.parse_args(argv)
|
| 64 |
+
try:
|
| 65 |
+
if args.command == 'serve':
|
| 66 |
+
from .server import serve
|
| 67 |
+
serve(args.port, args.open)
|
| 68 |
+
return 0
|
| 69 |
+
if args.command == 'catalog':
|
| 70 |
+
print(json.dumps(catalog(), indent=2))
|
| 71 |
+
return 0
|
| 72 |
+
if args.command == 'run':
|
| 73 |
+
cfg = {name: getattr(args, name, defaults[name]) for name in defaults}
|
| 74 |
+
result = run(RunConfig.from_dict(cfg))
|
| 75 |
+
receipt = pack(result)
|
| 76 |
+
if args.out:
|
| 77 |
+
_write(args.out, receipt)
|
| 78 |
+
print(json.dumps({'path': str(args.out.resolve()), 'decision': result['decision'],
|
| 79 |
+
'contract_satisfied': result['audit']['contract_satisfied'],
|
| 80 |
+
'scientific_hash': result['scientific_hash']}))
|
| 81 |
+
else:
|
| 82 |
+
print(json.dumps(receipt, indent=2))
|
| 83 |
+
elif args.command == 'study':
|
| 84 |
+
spec = json.loads(args.config.read_text(encoding='utf-8'))
|
| 85 |
+
result = run_study(spec)
|
| 86 |
+
_write(args.out, pack(result))
|
| 87 |
+
print(json.dumps({'path': str(args.out.resolve()), 'runs': len(result['rows']),
|
| 88 |
+
'summary': result['summary'], 'paired': result['paired']}))
|
| 89 |
+
else:
|
| 90 |
+
receipt = json.loads(args.receipt.read_text(encoding='utf-8'))
|
| 91 |
+
if args.command == 'check':
|
| 92 |
+
from .certificates import check_result
|
| 93 |
+
payload = check_envelope(receipt)
|
| 94 |
+
if payload['schema'] == 'crosswire-study-v1':
|
| 95 |
+
checks = [check_result(row) for row in payload['rows']]
|
| 96 |
+
checked = {'valid': bool(checks) and all(c.get('valid') is True for c in checks),
|
| 97 |
+
'checked_runs': len(checks), 'checks': checks,
|
| 98 |
+
'scope': 'Public model evidence only; does not authenticate private audits or target execution'}
|
| 99 |
+
else:
|
| 100 |
+
checked = check_result(payload)
|
| 101 |
+
else:
|
| 102 |
+
checked = replay(receipt)
|
| 103 |
+
print(json.dumps(checked))
|
| 104 |
+
return 0 if checked['valid'] else 1
|
| 105 |
+
return 0
|
| 106 |
+
except (ValueError, TypeError, OSError, RuntimeError, ImportError) as exc:
|
| 107 |
+
print(f'{type(exc).__name__}: {exc}', file=sys.stderr)
|
| 108 |
+
return 1
|
crosswire/conditions.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Canonical experimental conditions shared by study analyses and reports."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
from .identity import canonical_json
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
CONDITION_DEFAULTS = {
|
| 10 |
+
'anchors': False, 'split': 'development', 'horizon': 4,
|
| 11 |
+
'execution_mode': 'open_loop', 'temporal': False, 'escalate': False,
|
| 12 |
+
'probe_safety': 'unrestricted', 'risk_tolerance': 0.0, 'fault_schedule': None,
|
| 13 |
+
'candidate_backend': 'flat',
|
| 14 |
+
}
|
| 15 |
+
FAULT_TYPES = ('sensor_drift', 'actuator_swap', 'delay_spike', 'stuck_output', 'semantic_swap')
|
| 16 |
+
FAULT_PHASES = ('calibration', 'deployment', 'always')
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def normalize_fault_schedule(value):
|
| 20 |
+
"""Validate and copy a schedule; omitted defaults identify the same condition."""
|
| 21 |
+
if value is None:
|
| 22 |
+
return None
|
| 23 |
+
if not isinstance(value, dict):
|
| 24 |
+
raise ValueError('fault_schedule must be a dict or None')
|
| 25 |
+
if not {'step', 'type'} <= set(value):
|
| 26 |
+
raise ValueError("fault_schedule must specify 'step' and 'type'")
|
| 27 |
+
if set(value) - {'step', 'type', 'phase', 'value'}:
|
| 28 |
+
raise ValueError('Unknown fault_schedule fields')
|
| 29 |
+
if type(value['step']) is not int or value['step'] < 0:
|
| 30 |
+
raise ValueError("fault_schedule 'step' must be a non-negative integer")
|
| 31 |
+
if value['type'] not in FAULT_TYPES:
|
| 32 |
+
raise ValueError(f"fault_schedule 'type' must be one of {FAULT_TYPES}")
|
| 33 |
+
phase = value.get('phase', 'deployment')
|
| 34 |
+
if phase not in FAULT_PHASES:
|
| 35 |
+
raise ValueError(f"fault_schedule 'phase' must be one of {FAULT_PHASES}")
|
| 36 |
+
result = {'step': value['step'], 'type': value['type'], 'phase': phase}
|
| 37 |
+
if value['type'] == 'stuck_output':
|
| 38 |
+
symbol = value.get('value', 0)
|
| 39 |
+
if type(symbol) is not int or not 0 <= symbol < 4:
|
| 40 |
+
raise ValueError("fault_schedule 'value' must be an integer in [0, 3]")
|
| 41 |
+
result['value'] = symbol
|
| 42 |
+
elif 'value' in value:
|
| 43 |
+
raise ValueError("fault_schedule 'value' applies only to stuck_output")
|
| 44 |
+
return result
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def condition_key(configuration: dict) -> tuple:
|
| 48 |
+
"""Hashable, sortable identity, including canonical nested fault settings."""
|
| 49 |
+
return tuple(canonical_json(normalize_fault_schedule(configuration.get(key)))
|
| 50 |
+
if key == 'fault_schedule' else configuration.get(key, default)
|
| 51 |
+
for key, default in CONDITION_DEFAULTS.items())
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def condition_fields(key: tuple) -> dict:
|
| 55 |
+
result = dict(zip(CONDITION_DEFAULTS, key))
|
| 56 |
+
result['fault_schedule'] = json.loads(result['fault_schedule'])
|
| 57 |
+
return result
|
crosswire/core.py
ADDED
|
@@ -0,0 +1,1130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Finite Mealy adapters, exact behavior comparisons and bounded task admission.
|
| 2 |
+
|
| 3 |
+
Only public reference data and raw transcripts enter CandidateSpace. Work counts
|
| 4 |
+
are cumulative: new reference transition executions, new adapter/word expansions,
|
| 5 |
+
and represented candidate-pair comparisons, including histogram-aggregated pairs.
|
| 6 |
+
Cached results incur none of those costs; pair counts are not primitive CPU operations.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from collections import Counter, deque
|
| 10 |
+
from dataclasses import dataclass
|
| 11 |
+
from fractions import Fraction
|
| 12 |
+
from itertools import permutations, product
|
| 13 |
+
import math
|
| 14 |
+
from random import Random
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _integer(value, name, minimum=0, maximum=None):
|
| 18 |
+
if type(value) is not int:
|
| 19 |
+
raise TypeError(f'{name} must be an integer')
|
| 20 |
+
if value < minimum or (maximum is not None and value > maximum):
|
| 21 |
+
raise ValueError(f'{name} out of bounds')
|
| 22 |
+
return value
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _word(value, inputs):
|
| 26 |
+
try:
|
| 27 |
+
result = tuple(value)
|
| 28 |
+
except TypeError as exc:
|
| 29 |
+
raise TypeError('word must be a sequence') from exc
|
| 30 |
+
for symbol in result:
|
| 31 |
+
_integer(symbol, 'input', maximum=inputs - 1)
|
| 32 |
+
return result
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@dataclass(frozen=True)
|
| 36 |
+
class Machine:
|
| 37 |
+
name: str
|
| 38 |
+
transitions: tuple
|
| 39 |
+
inputs: int = 3
|
| 40 |
+
outputs: int = 4
|
| 41 |
+
initial: int = 0
|
| 42 |
+
|
| 43 |
+
def __post_init__(self):
|
| 44 |
+
if not isinstance(self.name, str) or not self.name:
|
| 45 |
+
raise ValueError('machine name must be a nonempty string')
|
| 46 |
+
_integer(self.inputs, 'inputs', 1)
|
| 47 |
+
_integer(self.outputs, 'outputs', 1)
|
| 48 |
+
rows = tuple(tuple(tuple(cell) for cell in row) for row in self.transitions)
|
| 49 |
+
if not rows:
|
| 50 |
+
raise ValueError('machine needs at least one state')
|
| 51 |
+
_integer(self.initial, 'initial', maximum=len(rows) - 1)
|
| 52 |
+
for row in rows:
|
| 53 |
+
if len(row) != self.inputs:
|
| 54 |
+
raise ValueError('transition rows must match input alphabet')
|
| 55 |
+
for cell in row:
|
| 56 |
+
if len(cell) != 2:
|
| 57 |
+
raise ValueError('transition cell must contain next state and output')
|
| 58 |
+
_integer(cell[0], 'next state', maximum=len(rows) - 1)
|
| 59 |
+
_integer(cell[1], 'output', maximum=self.outputs - 1)
|
| 60 |
+
object.__setattr__(self, 'transitions', rows)
|
| 61 |
+
|
| 62 |
+
def trace(self, word):
|
| 63 |
+
state, result = self.initial, []
|
| 64 |
+
for symbol in _word(word, self.inputs):
|
| 65 |
+
state, output = self.transitions[state][symbol]
|
| 66 |
+
result.append(output)
|
| 67 |
+
return tuple(result)
|
| 68 |
+
|
| 69 |
+
def to_dict(self):
|
| 70 |
+
return {'name': self.name, 'transitions': [[list(c) for c in r] for r in self.transitions],
|
| 71 |
+
'inputs': self.inputs, 'outputs': self.outputs, 'initial': self.initial}
|
| 72 |
+
|
| 73 |
+
@classmethod
|
| 74 |
+
def from_dict(cls, data):
|
| 75 |
+
return cls(**data)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
@dataclass(frozen=True)
|
| 79 |
+
class Contract:
|
| 80 |
+
goal: int = 1
|
| 81 |
+
hazard: int = 2
|
| 82 |
+
horizon: int = 4
|
| 83 |
+
|
| 84 |
+
def __post_init__(self):
|
| 85 |
+
_integer(self.goal, 'goal', maximum=3)
|
| 86 |
+
_integer(self.hazard, 'hazard', maximum=3)
|
| 87 |
+
_integer(self.horizon, 'horizon', 1, 6)
|
| 88 |
+
if self.goal == self.hazard:
|
| 89 |
+
raise ValueError('goal and hazard must differ')
|
| 90 |
+
|
| 91 |
+
def accepts(self, outputs):
|
| 92 |
+
outputs = tuple(outputs)
|
| 93 |
+
for symbol in outputs:
|
| 94 |
+
# The owning Machine validates its alphabet; contracts may also be
|
| 95 |
+
# used with larger alphabets than the original four-symbol fixtures.
|
| 96 |
+
_integer(symbol, 'semantic output')
|
| 97 |
+
return bool(outputs) and outputs[-1] == self.goal and self.hazard not in outputs
|
| 98 |
+
|
| 99 |
+
def to_dict(self):
|
| 100 |
+
return {'goal': self.goal, 'hazard': self.hazard, 'horizon': self.horizon}
|
| 101 |
+
|
| 102 |
+
@classmethod
|
| 103 |
+
def from_dict(cls, data):
|
| 104 |
+
return cls(**data)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
@dataclass(frozen=True)
|
| 108 |
+
class Adapter:
|
| 109 |
+
id: int
|
| 110 |
+
input_map: tuple
|
| 111 |
+
output_map: tuple
|
| 112 |
+
delay: int = 0
|
| 113 |
+
init_output: int = 0
|
| 114 |
+
|
| 115 |
+
def __post_init__(self):
|
| 116 |
+
_integer(self.id, 'adapter id')
|
| 117 |
+
_integer(self.delay, 'delay', minimum=0, maximum=1)
|
| 118 |
+
for name in ('input_map', 'output_map'):
|
| 119 |
+
mapping = tuple(getattr(self, name))
|
| 120 |
+
if not mapping:
|
| 121 |
+
raise ValueError('adapter alphabets cannot be empty')
|
| 122 |
+
for symbol in mapping:
|
| 123 |
+
_integer(symbol, name, maximum=len(mapping) - 1)
|
| 124 |
+
if len(set(mapping)) != len(mapping):
|
| 125 |
+
raise ValueError(f'{name} must be bijective')
|
| 126 |
+
object.__setattr__(self, name, mapping)
|
| 127 |
+
_integer(self.init_output, 'init_output', maximum=len(self.output_map) - 1)
|
| 128 |
+
|
| 129 |
+
def to_dict(self):
|
| 130 |
+
result = {'id': self.id, 'input_map': list(self.input_map), 'output_map': list(self.output_map)}
|
| 131 |
+
if self.delay:
|
| 132 |
+
result['delay'] = self.delay
|
| 133 |
+
result['init_output'] = self.init_output
|
| 134 |
+
return result
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def words(inputs, max_length):
|
| 138 |
+
_integer(inputs, 'inputs', 1)
|
| 139 |
+
_integer(max_length, 'max_length')
|
| 140 |
+
return tuple(word for length in range(1, max_length + 1) for word in product(range(inputs), repeat=length))
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
class CandidateSpace:
|
| 144 |
+
def __init__(self, machine, contract, temporal=False):
|
| 145 |
+
if not isinstance(machine, Machine) or not isinstance(contract, Contract):
|
| 146 |
+
raise TypeError('CandidateSpace requires a Machine and Contract')
|
| 147 |
+
if max(contract.goal, contract.hazard) >= machine.outputs:
|
| 148 |
+
raise ValueError('contract symbols exceed source alphabet')
|
| 149 |
+
self.machine, self.contract = machine, contract
|
| 150 |
+
self.temporal = bool(temporal)
|
| 151 |
+
adapters = [Adapter(i, a, b) for i, (a, b) in enumerate(
|
| 152 |
+
product(permutations(range(machine.inputs)), permutations(range(machine.outputs))))]
|
| 153 |
+
if self.temporal:
|
| 154 |
+
idx = len(adapters)
|
| 155 |
+
for a, b in product(permutations(range(machine.inputs)), permutations(range(machine.outputs))):
|
| 156 |
+
for init_o in range(machine.outputs):
|
| 157 |
+
adapters.append(Adapter(idx, a, b, delay=1, init_output=init_o))
|
| 158 |
+
idx += 1
|
| 159 |
+
self.adapters = tuple(adapters)
|
| 160 |
+
self.work = {'source_steps': 0, 'candidate_expansions': 0, 'pair_checks': 0}
|
| 161 |
+
self._source = {(): (machine.initial, ())}
|
| 162 |
+
self._predictions = {}
|
| 163 |
+
self._semantics = {}
|
| 164 |
+
self._safe = {}
|
| 165 |
+
self._signatures = {}
|
| 166 |
+
self._pair_weights = {}
|
| 167 |
+
self._deployment = words(machine.inputs, contract.horizon)
|
| 168 |
+
self._probes = words(machine.inputs, 3)
|
| 169 |
+
|
| 170 |
+
def _adapter(self, identifier):
|
| 171 |
+
return self.adapters[_integer(identifier, 'adapter id', maximum=len(self.adapters) - 1)]
|
| 172 |
+
|
| 173 |
+
def _ids(self, ids):
|
| 174 |
+
ids = tuple(ids)
|
| 175 |
+
for identifier in ids:
|
| 176 |
+
self._adapter(identifier)
|
| 177 |
+
return tuple(sorted(set(ids)))
|
| 178 |
+
|
| 179 |
+
def _source_trace(self, mapped):
|
| 180 |
+
# Iterative prefixes avoid a recursion bound for imported long traces.
|
| 181 |
+
prefix = ()
|
| 182 |
+
for symbol in mapped:
|
| 183 |
+
next_prefix = prefix + (symbol,)
|
| 184 |
+
if next_prefix not in self._source:
|
| 185 |
+
state, outputs = self._source[prefix]
|
| 186 |
+
state, output = self.machine.transitions[state][symbol]
|
| 187 |
+
self.work['source_steps'] += 1
|
| 188 |
+
self._source[next_prefix] = (state, outputs + (output,))
|
| 189 |
+
prefix = next_prefix
|
| 190 |
+
return self._source[prefix][1]
|
| 191 |
+
|
| 192 |
+
def _evaluate(self, identifier, word):
|
| 193 |
+
adapter = self._adapter(identifier)
|
| 194 |
+
word = _word(word, self.machine.inputs)
|
| 195 |
+
key = (identifier, word)
|
| 196 |
+
if key not in self._predictions:
|
| 197 |
+
semantic = self._source_trace(tuple(adapter.input_map[i] for i in word))
|
| 198 |
+
self._semantics[key] = semantic
|
| 199 |
+
if adapter.delay == 1:
|
| 200 |
+
preds = []
|
| 201 |
+
for t in range(len(semantic)):
|
| 202 |
+
if t == 0:
|
| 203 |
+
preds.append(adapter.output_map[adapter.init_output])
|
| 204 |
+
else:
|
| 205 |
+
preds.append(adapter.output_map[semantic[t - 1]])
|
| 206 |
+
self._predictions[key] = tuple(preds)
|
| 207 |
+
else:
|
| 208 |
+
self._predictions[key] = tuple(adapter.output_map[o] for o in semantic)
|
| 209 |
+
self.work['candidate_expansions'] += 1
|
| 210 |
+
return key
|
| 211 |
+
|
| 212 |
+
def predict(self, identifier, word):
|
| 213 |
+
return self._predictions[self._evaluate(identifier, word)]
|
| 214 |
+
|
| 215 |
+
def semantic(self, identifier, word):
|
| 216 |
+
return self._semantics[self._evaluate(identifier, word)]
|
| 217 |
+
|
| 218 |
+
def safe_words(self, identifier):
|
| 219 |
+
self._adapter(identifier)
|
| 220 |
+
if identifier not in self._safe:
|
| 221 |
+
self._safe[identifier] = frozenset(w for w in self._deployment if self.contract.accepts(self.semantic(identifier, w)))
|
| 222 |
+
return self._safe[identifier]
|
| 223 |
+
|
| 224 |
+
def _evidence(self, transcripts, anchors):
|
| 225 |
+
evidence = []
|
| 226 |
+
for transcript in transcripts:
|
| 227 |
+
if type(transcript.get('complete')) is not bool:
|
| 228 |
+
raise ValueError('transcript needs a boolean complete field')
|
| 229 |
+
if not transcript['complete']:
|
| 230 |
+
continue
|
| 231 |
+
word = _word(transcript['word'], self.machine.inputs)
|
| 232 |
+
outputs = tuple(transcript['outputs'])
|
| 233 |
+
if len(outputs) != len(word):
|
| 234 |
+
raise ValueError('complete transcript word/output lengths differ')
|
| 235 |
+
for symbol in outputs:
|
| 236 |
+
_integer(symbol, 'raw output', maximum=self.machine.outputs - 1)
|
| 237 |
+
evidence.append((word, outputs))
|
| 238 |
+
validated = dict(anchors or {})
|
| 239 |
+
for raw, semantic in validated.items():
|
| 240 |
+
_integer(raw, 'anchor raw output', maximum=self.machine.outputs - 1)
|
| 241 |
+
_integer(semantic, 'anchor semantic output', maximum=self.machine.outputs - 1)
|
| 242 |
+
return evidence, validated
|
| 243 |
+
|
| 244 |
+
def losses(self, transcripts, anchors=None):
|
| 245 |
+
evidence, anchors = self._evidence(transcripts, anchors)
|
| 246 |
+
result = {}
|
| 247 |
+
for adapter in self.adapters:
|
| 248 |
+
result[adapter.id] = sum(p != o for word, outputs in evidence for p, o in zip(self.predict(adapter.id, word), outputs))
|
| 249 |
+
result[adapter.id] += sum(adapter.output_map[semantic] != raw for raw, semantic in anchors.items())
|
| 250 |
+
return result
|
| 251 |
+
|
| 252 |
+
def survivors(self, transcripts, anchors=None):
|
| 253 |
+
return tuple(identifier for identifier, loss in self.losses(transcripts, anchors).items() if loss == 0)
|
| 254 |
+
|
| 255 |
+
def level_ids(self, level):
|
| 256 |
+
"""Nested hypothesis classes: 'static' (144 memoryless maps) ⊂ 'temporal' (all)."""
|
| 257 |
+
if level == 'static':
|
| 258 |
+
return tuple(a.id for a in self.adapters if a.delay == 0)
|
| 259 |
+
if level == 'temporal':
|
| 260 |
+
return tuple(a.id for a in self.adapters)
|
| 261 |
+
raise ValueError('unknown hypothesis level')
|
| 262 |
+
|
| 263 |
+
def _decision_classes(self, ids):
|
| 264 |
+
"""Group survivors by their contract-relevant behaviour (safe deployment set)."""
|
| 265 |
+
classes = {}
|
| 266 |
+
for identifier in ids:
|
| 267 |
+
classes.setdefault(self.safe_words(identifier), []).append(identifier)
|
| 268 |
+
return list(classes.items())
|
| 269 |
+
|
| 270 |
+
def distinguishing_word(self, a, b, max_length):
|
| 271 |
+
"""Shortest lexicographic input word on which adapters a and b predict different raw outputs.
|
| 272 |
+
|
| 273 |
+
Exact BFS over the joint state (reference state under a, under b, and each
|
| 274 |
+
adapter's one-step output buffer). Unlike the fixed probe pool, the witness
|
| 275 |
+
may be longer than three actions; it is bounded only by ``max_length``.
|
| 276 |
+
"""
|
| 277 |
+
A, B = self._adapter(a), self._adapter(b)
|
| 278 |
+
start = (self.machine.initial, self.machine.initial,
|
| 279 |
+
A.init_output if A.delay else None, B.init_output if B.delay else None)
|
| 280 |
+
visited, pending = {start}, deque([(start, ())])
|
| 281 |
+
while pending:
|
| 282 |
+
(qa, qb, ba, bb), prefix = pending.popleft()
|
| 283 |
+
if len(prefix) >= max_length:
|
| 284 |
+
continue
|
| 285 |
+
for symbol in range(self.machine.inputs):
|
| 286 |
+
na, sa = self.machine.transitions[qa][A.input_map[symbol]]
|
| 287 |
+
nb, sb = self.machine.transitions[qb][B.input_map[symbol]]
|
| 288 |
+
self.work['source_steps'] += 2
|
| 289 |
+
oa = A.output_map[ba if A.delay else sa]
|
| 290 |
+
ob = B.output_map[bb if B.delay else sb]
|
| 291 |
+
word = prefix + (symbol,)
|
| 292 |
+
if oa != ob:
|
| 293 |
+
return word
|
| 294 |
+
state = (na, nb, sa if A.delay else None, sb if B.delay else None)
|
| 295 |
+
if state not in visited:
|
| 296 |
+
visited.add(state)
|
| 297 |
+
pending.append((state, word))
|
| 298 |
+
return None
|
| 299 |
+
|
| 300 |
+
def _weighted_separation(self, classes, word, weights):
|
| 301 |
+
"""Sum separated-pair weights by class histograms, O(N + C^2 P).
|
| 302 |
+
|
| 303 |
+
``pair_checks`` retains the represented candidate-pair count; the
|
| 304 |
+
histogram arithmetic does not execute an individual comparison per pair.
|
| 305 |
+
"""
|
| 306 |
+
counts = []
|
| 307 |
+
for _, members in classes:
|
| 308 |
+
tally = {}
|
| 309 |
+
for identifier in members:
|
| 310 |
+
p = self.predict(identifier, word)
|
| 311 |
+
tally[p] = tally.get(p, 0) + 1
|
| 312 |
+
counts.append((len(members), tally))
|
| 313 |
+
total = 0
|
| 314 |
+
for i in range(len(counts)):
|
| 315 |
+
for j in range(i + 1, len(counts)):
|
| 316 |
+
w = weights[i][j]
|
| 317 |
+
if not w:
|
| 318 |
+
continue
|
| 319 |
+
(na, ta), (nb, tb) = counts[i], counts[j]
|
| 320 |
+
agree = sum(c * tb.get(p, 0) for p, c in ta.items())
|
| 321 |
+
total += w * (na * nb - agree)
|
| 322 |
+
self.work['pair_checks'] += na * nb
|
| 323 |
+
return total
|
| 324 |
+
|
| 325 |
+
def probe_is_safe(self, ids, word):
|
| 326 |
+
"""Check prefix hazard freedom under every live semantic interpretation.
|
| 327 |
+
|
| 328 |
+
This is conditional on inclusion in the declared finite model family;
|
| 329 |
+
raw response agreement alone does not certify the target's semantics.
|
| 330 |
+
"""
|
| 331 |
+
ids = self._ids(ids)
|
| 332 |
+
word = _word(word, self.machine.inputs)
|
| 333 |
+
return bool(ids) and all(self.contract.hazard not in self.semantic(i, word) for i in ids)
|
| 334 |
+
|
| 335 |
+
def information_profile(self, ids, word) -> dict:
|
| 336 |
+
"""Compute exact information-theoretic and minimax profile of a probe word over belief `ids`.
|
| 337 |
+
|
| 338 |
+
Under deterministic candidate models and a uniform prior over `ids`, the
|
| 339 |
+
Shannon predictive output entropy H(O | w, V) equals the Bayesian Active
|
| 340 |
+
Learning by Disagreement (BALD) mutual information I(alpha; O | w, V).
|
| 341 |
+
"""
|
| 342 |
+
ids = self._ids(ids)
|
| 343 |
+
word = _word(word, self.machine.inputs)
|
| 344 |
+
if not ids:
|
| 345 |
+
return {
|
| 346 |
+
'word': list(word), 'cost': 1 + len(word), 'candidate_count': 0,
|
| 347 |
+
'partition_count': 0, 'mutual_information_bits': 0.0,
|
| 348 |
+
'cost_efficiency_bits_per_call': 0.0, 'expected_survivors': 0.0,
|
| 349 |
+
'worst_case_survivors': 0, 'expected_reduction_ratio': 0.0,
|
| 350 |
+
'model_safe': False,
|
| 351 |
+
}
|
| 352 |
+
n = len(ids)
|
| 353 |
+
partitions = {}
|
| 354 |
+
for i in ids:
|
| 355 |
+
partitions.setdefault(self.predict(i, word), []).append(i)
|
| 356 |
+
counts = {p: len(m) for p, m in partitions.items()}
|
| 357 |
+
h = -sum((c / n) * math.log2(c / n) for c in counts.values())
|
| 358 |
+
expected_surv = sum((c * c) / n for c in counts.values())
|
| 359 |
+
worst_surv = max(counts.values())
|
| 360 |
+
cost = 1 + len(word)
|
| 361 |
+
ready_mass = 0
|
| 362 |
+
for members in partitions.values():
|
| 363 |
+
common = set(self.safe_words(members[0]))
|
| 364 |
+
for mid in members[1:]:
|
| 365 |
+
if not common:
|
| 366 |
+
break
|
| 367 |
+
common.intersection_update(self.safe_words(mid))
|
| 368 |
+
if common:
|
| 369 |
+
ready_mass += len(members)
|
| 370 |
+
expected_readiness = round(ready_mass / n, 4)
|
| 371 |
+
return {
|
| 372 |
+
'word': list(word),
|
| 373 |
+
'cost': cost,
|
| 374 |
+
'candidate_count': n,
|
| 375 |
+
'partition_count': len(counts),
|
| 376 |
+
'mutual_information_bits': round(h, 6),
|
| 377 |
+
'cost_efficiency_bits_per_call': round(h / cost, 6),
|
| 378 |
+
'expected_survivors': round(expected_surv, 4),
|
| 379 |
+
'worst_case_survivors': worst_surv,
|
| 380 |
+
'expected_reduction_ratio': round(1.0 - (expected_surv / n), 6),
|
| 381 |
+
'expected_task_readiness': expected_readiness,
|
| 382 |
+
'model_safe': self.probe_is_safe(ids, word),
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
def rank_probes(self, ids, top_k=5, safe_only=False, max_steps=3) -> list[dict]:
|
| 386 |
+
"""Rank candidate probes by information-per-call efficiency, minimax survivor bound, and length."""
|
| 387 |
+
if type(safe_only) is not bool:
|
| 388 |
+
raise TypeError('safe_only must be a boolean')
|
| 389 |
+
ids = self._ids(ids)
|
| 390 |
+
_integer(top_k, 'top_k', 1)
|
| 391 |
+
_integer(max_steps, 'max_steps', 1)
|
| 392 |
+
if not ids:
|
| 393 |
+
return []
|
| 394 |
+
eligible = [w for w in self._probes if len(w) <= max_steps]
|
| 395 |
+
if safe_only:
|
| 396 |
+
eligible = [w for w in eligible if self.probe_is_safe(ids, w)]
|
| 397 |
+
profiles = [self.information_profile(ids, w) for w in eligible]
|
| 398 |
+
profiles.sort(key=lambda p: (
|
| 399 |
+
-p['cost_efficiency_bits_per_call'],
|
| 400 |
+
p['worst_case_survivors'],
|
| 401 |
+
p['cost'],
|
| 402 |
+
p['word'],
|
| 403 |
+
))
|
| 404 |
+
return profiles[:top_k]
|
| 405 |
+
|
| 406 |
+
def choose_branching_step(self, ids, word_prefix=(), safe_only=False, safe_ids=None, seed=0):
|
| 407 |
+
"""Select the next within-query action conditioned on the live belief and query prefix.
|
| 408 |
+
|
| 409 |
+
Chooses an action maximizing predictive output entropy across surviving models.
|
| 410 |
+
If safe_only=True, only actions where all surviving models in safe_ids predict zero hazard on
|
| 411 |
+
this step are considered eligible.
|
| 412 |
+
"""
|
| 413 |
+
import math
|
| 414 |
+
ids = self._ids(ids)
|
| 415 |
+
if not ids or len(ids) <= 1:
|
| 416 |
+
return None
|
| 417 |
+
if safe_ids is None:
|
| 418 |
+
safe_ids = ids
|
| 419 |
+
prefix = _word(word_prefix, self.machine.inputs)
|
| 420 |
+
best_action, best_score = None, -1.0
|
| 421 |
+
n = len(ids)
|
| 422 |
+
order = [(seed + offset) % self.machine.inputs for offset in range(self.machine.inputs)]
|
| 423 |
+
for a in order:
|
| 424 |
+
extended = prefix + (a,)
|
| 425 |
+
if safe_only and not self.probe_is_safe(safe_ids, extended):
|
| 426 |
+
continue
|
| 427 |
+
counts = {}
|
| 428 |
+
for i in ids:
|
| 429 |
+
p = self.predict(i, extended)[-1]
|
| 430 |
+
counts[p] = counts.get(p, 0) + 1
|
| 431 |
+
h = 0.0
|
| 432 |
+
for c in counts.values():
|
| 433 |
+
prob = c / n
|
| 434 |
+
h -= prob * math.log2(prob)
|
| 435 |
+
self.work['pair_checks'] += n
|
| 436 |
+
if h > best_score:
|
| 437 |
+
best_action, best_score = a, h
|
| 438 |
+
if best_score <= 0.0:
|
| 439 |
+
# When models agree on predicted outputs, fall back to the rotating
|
| 440 |
+
# eligible action to test the hypothesis against target observations.
|
| 441 |
+
for a in order:
|
| 442 |
+
extended = prefix + (a,)
|
| 443 |
+
if not safe_only or self.probe_is_safe(safe_ids, extended):
|
| 444 |
+
return a
|
| 445 |
+
return None
|
| 446 |
+
return best_action
|
| 447 |
+
|
| 448 |
+
def choose_probe(self, ids, policy, remaining_steps, seen, seed=0, safe_only=False):
|
| 449 |
+
if policy not in ('fixed', 'random', 'disagreement', 'task', 'counterexample', 'entropy', 'ec2', 'branching', 'voi'):
|
| 450 |
+
raise ValueError('unknown probe policy')
|
| 451 |
+
if type(safe_only) is not bool:
|
| 452 |
+
raise TypeError('safe_only must be a boolean')
|
| 453 |
+
ids = self._ids(ids)
|
| 454 |
+
_integer(remaining_steps, 'remaining steps')
|
| 455 |
+
_integer(seed, 'seed', maximum=2**32 - 1)
|
| 456 |
+
seen = {_word(w, self.machine.inputs) for w in seen}
|
| 457 |
+
if not ids:
|
| 458 |
+
return None
|
| 459 |
+
# remaining_steps is the action allowance after the caller has reserved
|
| 460 |
+
# the reset for this query. Scores still charge reset + actions in their
|
| 461 |
+
# information/cost ratio.
|
| 462 |
+
eligible = [w for w in self._probes if w not in seen and len(w) <= remaining_steps]
|
| 463 |
+
if safe_only:
|
| 464 |
+
eligible = [w for w in eligible if self.probe_is_safe(ids, w)]
|
| 465 |
+
if not eligible and policy != 'counterexample':
|
| 466 |
+
return None
|
| 467 |
+
if policy == 'fixed':
|
| 468 |
+
return eligible[0]
|
| 469 |
+
if policy == 'random':
|
| 470 |
+
return Random(seed).choice(sorted(eligible))
|
| 471 |
+
if policy == 'entropy':
|
| 472 |
+
import math
|
| 473 |
+
best, best_score = None, -1.0
|
| 474 |
+
n = len(ids)
|
| 475 |
+
if not ids:
|
| 476 |
+
return None
|
| 477 |
+
if n == 1:
|
| 478 |
+
return eligible[0] if eligible else None
|
| 479 |
+
for word in eligible:
|
| 480 |
+
counts = {}
|
| 481 |
+
for i in ids:
|
| 482 |
+
p = self.predict(i, word)
|
| 483 |
+
counts[p] = counts.get(p, 0) + 1
|
| 484 |
+
h = 0.0
|
| 485 |
+
for c in counts.values():
|
| 486 |
+
prob = c / n
|
| 487 |
+
h -= prob * math.log2(prob)
|
| 488 |
+
self.work['pair_checks'] += n
|
| 489 |
+
score = h / (1 + len(word))
|
| 490 |
+
if score > best_score:
|
| 491 |
+
best, best_score = word, score
|
| 492 |
+
return best
|
| 493 |
+
if policy == 'ec2':
|
| 494 |
+
# Equivalence Class Edge Cutting (EC2):
|
| 495 |
+
# Prioritizes probes that cut edges between differing contract decision classes.
|
| 496 |
+
classes = self._decision_classes(ids)
|
| 497 |
+
if len(classes) <= 1:
|
| 498 |
+
return min(eligible, key=lambda w: (len(w), w)) if eligible else None
|
| 499 |
+
weights = [[len(sa ^ sb) / max(1, len(sa | sb)) for sb, _ in classes] for sa, _ in classes]
|
| 500 |
+
best, best_score = None, -1.0
|
| 501 |
+
for word in eligible:
|
| 502 |
+
score = self._weighted_separation(classes, word, weights) / (1 + len(word))
|
| 503 |
+
if score > best_score:
|
| 504 |
+
best, best_score = word, score
|
| 505 |
+
if best_score <= 0.0:
|
| 506 |
+
return min(eligible, key=lambda w: (len(w), w)) if eligible else None
|
| 507 |
+
return best
|
| 508 |
+
if policy == 'branching':
|
| 509 |
+
step_action = self.choose_branching_step(ids, (), safe_only=safe_only)
|
| 510 |
+
if step_action is not None and (step_action,) in eligible:
|
| 511 |
+
return (step_action,)
|
| 512 |
+
return eligible[0] if eligible else None
|
| 513 |
+
if policy == 'voi':
|
| 514 |
+
import math
|
| 515 |
+
n = len(ids)
|
| 516 |
+
if not ids:
|
| 517 |
+
return None
|
| 518 |
+
if n == 1:
|
| 519 |
+
return eligible[0] if eligible else None
|
| 520 |
+
best, best_score = None, (-1.0, -1.0, 0, 0)
|
| 521 |
+
for word in eligible:
|
| 522 |
+
partitions = {}
|
| 523 |
+
for i in ids:
|
| 524 |
+
p = self.predict(i, word)
|
| 525 |
+
partitions.setdefault(p, []).append(i)
|
| 526 |
+
cost = 1 + len(word)
|
| 527 |
+
ready_mass = 0
|
| 528 |
+
h = 0.0
|
| 529 |
+
worst_cell = 0
|
| 530 |
+
for p, members in partitions.items():
|
| 531 |
+
m_len = len(members)
|
| 532 |
+
if m_len > worst_cell:
|
| 533 |
+
worst_cell = m_len
|
| 534 |
+
prob = m_len / n
|
| 535 |
+
h -= prob * math.log2(prob)
|
| 536 |
+
common_safe = set(self.safe_words(members[0]))
|
| 537 |
+
for mid in members[1:]:
|
| 538 |
+
if not common_safe:
|
| 539 |
+
break
|
| 540 |
+
common_safe.intersection_update(self.safe_words(mid))
|
| 541 |
+
if common_safe:
|
| 542 |
+
ready_mass += m_len
|
| 543 |
+
readiness_rate = ready_mass / n
|
| 544 |
+
readiness_efficiency = readiness_rate / cost
|
| 545 |
+
entropy_efficiency = h / cost
|
| 546 |
+
score = (readiness_efficiency, entropy_efficiency, -worst_cell, -len(word))
|
| 547 |
+
self.work['pair_checks'] += n
|
| 548 |
+
if score > best_score:
|
| 549 |
+
best, best_score = word, score
|
| 550 |
+
return best if best is not None else (eligible[0] if eligible else None)
|
| 551 |
+
if policy == 'disagreement':
|
| 552 |
+
best, best_score = None, 0
|
| 553 |
+
n = len(ids)
|
| 554 |
+
total_pairs = n * (n - 1) // 2
|
| 555 |
+
for word in eligible:
|
| 556 |
+
counts = {}
|
| 557 |
+
for i in ids:
|
| 558 |
+
p = self.predict(i, word)
|
| 559 |
+
counts[p] = counts.get(p, 0) + 1
|
| 560 |
+
agreeing = sum(c * (c - 1) // 2 for c in counts.values())
|
| 561 |
+
separated = total_pairs - agreeing
|
| 562 |
+
self.work['pair_checks'] += total_pairs
|
| 563 |
+
score = separated / (1 + len(word))
|
| 564 |
+
if score > best_score:
|
| 565 |
+
best, best_score = word, score
|
| 566 |
+
return best
|
| 567 |
+
if policy == 'counterexample':
|
| 568 |
+
# Decision-focused counterexample-guided probing. Candidate words are
|
| 569 |
+
# the fixed pool plus exact shortest witnesses separating one
|
| 570 |
+
# representative pair for every pair of *decision classes* (survivors
|
| 571 |
+
# whose admissible deployment sets differ). Witnesses may exceed the
|
| 572 |
+
# three-action pool. Pairs are weighted as in the task policy
|
| 573 |
+
# (Jaccard distance of safe sets), computed per class, not per pair.
|
| 574 |
+
classes = self._decision_classes(ids)
|
| 575 |
+
if len(classes) > 1:
|
| 576 |
+
weights = [[len(sa ^ sb) / max(1, len(sa | sb)) for sb, _ in classes] for sa, _ in classes]
|
| 577 |
+
else:
|
| 578 |
+
# No decision-relevant disagreement: fall back to plain separation.
|
| 579 |
+
classes = [(frozenset(), [i]) for i in ids]
|
| 580 |
+
weights = [[0 if i == j else 1 for j in range(len(classes))] for i in range(len(classes))]
|
| 581 |
+
if len(classes) > 64:
|
| 582 |
+
classes = classes[:64]
|
| 583 |
+
weights = [row[:64] for row in weights[:64]]
|
| 584 |
+
pool = list(eligible)
|
| 585 |
+
if len(classes) > 1:
|
| 586 |
+
limit = min(remaining_steps, self.contract.horizon + 4)
|
| 587 |
+
for i in range(len(classes)):
|
| 588 |
+
for j in range(i + 1, len(classes)):
|
| 589 |
+
if not weights[i][j]:
|
| 590 |
+
continue
|
| 591 |
+
witness = self.distinguishing_word(classes[i][1][0], classes[j][1][0], limit)
|
| 592 |
+
if (witness is not None and witness not in seen and witness not in pool
|
| 593 |
+
and (not safe_only or self.probe_is_safe(ids, witness))):
|
| 594 |
+
pool.append(witness)
|
| 595 |
+
best, best_score = None, 0.0
|
| 596 |
+
for word in pool:
|
| 597 |
+
score = self._weighted_separation(classes, word, weights) / (1 + len(word))
|
| 598 |
+
if score > best_score:
|
| 599 |
+
best, best_score = word, score
|
| 600 |
+
return best
|
| 601 |
+
# Task weights are equal within each pair of contract classes. Count
|
| 602 |
+
# separated candidate pairs by response histograms, without allocating
|
| 603 |
+
# the quadratic pair list. Rational scores give deterministic ties.
|
| 604 |
+
classes = self._decision_classes(ids)
|
| 605 |
+
weights = [[Fraction(len(sa ^ sb), max(1, len(sa | sb))) for sb, _ in classes]
|
| 606 |
+
for sa, _ in classes]
|
| 607 |
+
best, best_score = None, Fraction(0)
|
| 608 |
+
for word in eligible:
|
| 609 |
+
score = self._weighted_separation(classes, word, weights) / (1 + len(word))
|
| 610 |
+
if score > best_score:
|
| 611 |
+
best, best_score = word, score
|
| 612 |
+
return best
|
| 613 |
+
|
| 614 |
+
def plan(self, ids, mode='robust', transcripts=None, anchors=None, risk_tolerance=0.0):
|
| 615 |
+
if mode not in ('robust', 'one_best', 'unique', 'fit', 'identity', 'abstain', 'risk_controlled'):
|
| 616 |
+
raise ValueError('unknown admission mode')
|
| 617 |
+
# The bounds reject NaN/infinity as well as unsupported numeric aliases;
|
| 618 |
+
# validating before any empty-belief return keeps the public API strict.
|
| 619 |
+
if type(risk_tolerance) not in (int, float) or not 0 <= risk_tolerance < 1:
|
| 620 |
+
raise ValueError('risk_tolerance must be a finite number in [0, 1)')
|
| 621 |
+
ids = self._ids(ids)
|
| 622 |
+
if mode == 'risk_controlled':
|
| 623 |
+
if not ids:
|
| 624 |
+
return {'status': 'FAMILY_CONTRADICTED', 'admitted': False, 'word': None,
|
| 625 |
+
'candidate_count': 0, 'risk_metrics': {'hazard_risk': None, 'goal_rate': None, 'tolerance': risk_tolerance}}
|
| 626 |
+
n = len(ids)
|
| 627 |
+
admissible_candidates = []
|
| 628 |
+
for w in self._deployment:
|
| 629 |
+
h_count = sum(self.contract.hazard in self.semantic(i, w) for i in ids)
|
| 630 |
+
g_count = sum(self.contract.accepts(self.semantic(i, w)) for i in ids)
|
| 631 |
+
h_risk = h_count / n
|
| 632 |
+
g_rate = g_count / n
|
| 633 |
+
if h_risk <= risk_tolerance and (n - g_count) / n <= risk_tolerance:
|
| 634 |
+
admissible_candidates.append((w, h_risk, g_rate))
|
| 635 |
+
if not admissible_candidates:
|
| 636 |
+
return {'status': 'UNRESOLVED', 'admitted': False, 'word': None,
|
| 637 |
+
'candidate_count': n, 'risk_metrics': {'hazard_risk': None, 'goal_rate': None, 'tolerance': risk_tolerance}}
|
| 638 |
+
best_w, best_h_risk, best_g_rate = min(
|
| 639 |
+
admissible_candidates,
|
| 640 |
+
key=lambda item: (len(item[0]), item[1], -item[2], item[0])
|
| 641 |
+
)
|
| 642 |
+
status = 'IDENTIFIED' if n == 1 else ('TASK_ADMISSIBLE' if best_h_risk == 0.0 and best_g_rate == 1.0 else 'RISK_CONTROLLED_ADMISSIBLE')
|
| 643 |
+
return {
|
| 644 |
+
'status': status,
|
| 645 |
+
'admitted': True,
|
| 646 |
+
'word': list(best_w),
|
| 647 |
+
'candidate_count': n,
|
| 648 |
+
'risk_metrics': {
|
| 649 |
+
'hazard_risk': best_h_risk,
|
| 650 |
+
'goal_rate': best_g_rate,
|
| 651 |
+
'tolerance': risk_tolerance,
|
| 652 |
+
'candidate_count': n
|
| 653 |
+
}
|
| 654 |
+
}
|
| 655 |
+
chosen = ids
|
| 656 |
+
if mode == 'fit':
|
| 657 |
+
losses = self.losses(transcripts or [], anchors)
|
| 658 |
+
chosen = (min(losses, key=lambda i: (losses[i], i)),)
|
| 659 |
+
elif mode == 'identity':
|
| 660 |
+
chosen = (next(a.id for a in self.adapters if a.input_map == tuple(range(self.machine.inputs))
|
| 661 |
+
and a.output_map == tuple(range(self.machine.outputs))),)
|
| 662 |
+
elif mode == 'one_best':
|
| 663 |
+
chosen = ids[:1]
|
| 664 |
+
elif mode == 'unique' and len(ids) != 1:
|
| 665 |
+
chosen = ()
|
| 666 |
+
elif mode == 'abstain':
|
| 667 |
+
chosen = ()
|
| 668 |
+
safe = set(self.safe_words(chosen[0])) if chosen else set()
|
| 669 |
+
for identifier in chosen[1:]:
|
| 670 |
+
safe.intersection_update(self.safe_words(identifier))
|
| 671 |
+
word = min(safe, key=lambda w: (len(w), w)) if safe else None
|
| 672 |
+
status = ('IDENTIFIED' if len(ids) == 1 else 'TASK_ADMISSIBLE') if word is not None else (
|
| 673 |
+
'FAMILY_CONTRADICTED' if not ids else 'UNRESOLVED')
|
| 674 |
+
return {'status': status, 'admitted': word is not None, 'word': list(word) if word is not None else None,
|
| 675 |
+
'candidate_count': len(ids)}
|
| 676 |
+
|
| 677 |
+
def _signature(self, identifier):
|
| 678 |
+
"""Canonical minimized reachable raw transducer, independent of state IDs."""
|
| 679 |
+
if identifier in self._signatures:
|
| 680 |
+
return self._signatures[identifier]
|
| 681 |
+
adapter = self._adapter(identifier)
|
| 682 |
+
# Delayed observations depend on both the source state and the previous
|
| 683 |
+
# semantic output. Minimize that augmented transducer, so raw-equivalent
|
| 684 |
+
# static/delayed models can merge while distinct buffers remain separate.
|
| 685 |
+
initial = (self.machine.initial, adapter.init_output if adapter.delay else None)
|
| 686 |
+
states, pending, rows = {initial}, deque([initial]), {}
|
| 687 |
+
while pending:
|
| 688 |
+
state = pending.popleft()
|
| 689 |
+
source_state, buffered = state
|
| 690 |
+
row = []
|
| 691 |
+
for symbol in adapter.input_map:
|
| 692 |
+
source_next, output = self.machine.transitions[source_state][symbol]
|
| 693 |
+
self.work['source_steps'] += 1
|
| 694 |
+
following = (source_next, output if adapter.delay else None)
|
| 695 |
+
row.append((following, adapter.output_map[buffered if adapter.delay else output]))
|
| 696 |
+
if following not in states:
|
| 697 |
+
states.add(following)
|
| 698 |
+
pending.append(following)
|
| 699 |
+
rows[state] = tuple(row)
|
| 700 |
+
classes = {state: 0 for state in states}
|
| 701 |
+
while True:
|
| 702 |
+
labels, refined = {}, {}
|
| 703 |
+
for state in sorted(states):
|
| 704 |
+
signature = tuple((output, classes[following]) for following, output in rows[state])
|
| 705 |
+
refined[state] = labels.setdefault(signature, len(labels))
|
| 706 |
+
# Refinement never merges old classes; unchanged count means stable.
|
| 707 |
+
if len(set(refined.values())) == len(set(classes.values())):
|
| 708 |
+
classes = refined
|
| 709 |
+
break
|
| 710 |
+
classes = refined
|
| 711 |
+
representatives = {classes[s]: s for s in sorted(states)}
|
| 712 |
+
start = classes[initial]
|
| 713 |
+
canonical, pending, result = {start: 0}, deque([start]), []
|
| 714 |
+
while pending:
|
| 715 |
+
current = pending.popleft()
|
| 716 |
+
row = []
|
| 717 |
+
for following, output in rows[representatives[current]]:
|
| 718 |
+
following = classes[following]
|
| 719 |
+
if following not in canonical:
|
| 720 |
+
canonical[following] = len(canonical)
|
| 721 |
+
pending.append(following)
|
| 722 |
+
row.append((canonical[following], output))
|
| 723 |
+
result.append(tuple(row))
|
| 724 |
+
self._signatures[identifier] = tuple(result)
|
| 725 |
+
return self._signatures[identifier]
|
| 726 |
+
|
| 727 |
+
def ambiguity(self, ids):
|
| 728 |
+
ids = self._ids(ids)
|
| 729 |
+
return {'map_count': len(ids), 'observational_classes': len({self._signature(i) for i in ids}),
|
| 730 |
+
'contract_classes': len({self.safe_words(i) for i in ids})}
|
| 731 |
+
|
| 732 |
+
|
| 733 |
+
def compare_adapter(machine, adapter, target):
|
| 734 |
+
"""Exact product BFS; first mismatch gives a shortest lexicographic witness."""
|
| 735 |
+
if machine.inputs != target.inputs or machine.outputs != target.outputs:
|
| 736 |
+
raise ValueError('comparison machine alphabets differ')
|
| 737 |
+
if len(adapter.input_map) != machine.inputs or len(adapter.output_map) != machine.outputs:
|
| 738 |
+
raise ValueError('adapter alphabets differ')
|
| 739 |
+
is_temporal = bool(getattr(adapter, 'delay', 0))
|
| 740 |
+
initial = (machine.initial, target.initial, adapter.init_output) if is_temporal else (machine.initial, target.initial)
|
| 741 |
+
visited, pending = {initial}, deque([(initial, ())])
|
| 742 |
+
steps = 0
|
| 743 |
+
while pending:
|
| 744 |
+
curr, prefix = pending.popleft()
|
| 745 |
+
source_state, target_state = curr[0], curr[1]
|
| 746 |
+
buffered = curr[2] if is_temporal else None
|
| 747 |
+
for symbol in range(machine.inputs):
|
| 748 |
+
source_next, semantic = machine.transitions[source_state][adapter.input_map[symbol]]
|
| 749 |
+
target_next, raw = target.transitions[target_state][symbol]
|
| 750 |
+
steps += 1
|
| 751 |
+
word = prefix + (symbol,)
|
| 752 |
+
pred_raw = adapter.output_map[buffered if is_temporal else semantic]
|
| 753 |
+
if pred_raw != raw:
|
| 754 |
+
return {'equivalent': False, 'counterexample': list(word), 'visited_pairs': len(visited),
|
| 755 |
+
'source_steps': steps, 'target_model_steps': steps}
|
| 756 |
+
following = (source_next, target_next, semantic) if is_temporal else (source_next, target_next)
|
| 757 |
+
if following not in visited:
|
| 758 |
+
visited.add(following)
|
| 759 |
+
pending.append((following, word))
|
| 760 |
+
return {'equivalent': True, 'counterexample': None, 'visited_pairs': len(visited),
|
| 761 |
+
'source_steps': steps, 'target_model_steps': steps}
|
| 762 |
+
|
| 763 |
+
|
| 764 |
+
|
| 765 |
+
class _LazyAdapters:
|
| 766 |
+
def __init__(self, space):
|
| 767 |
+
self._space = space
|
| 768 |
+
|
| 769 |
+
def __len__(self):
|
| 770 |
+
return self._space.total_adapters
|
| 771 |
+
|
| 772 |
+
def __getitem__(self, idx):
|
| 773 |
+
if isinstance(idx, slice):
|
| 774 |
+
return [self[i] for i in range(*idx.indices(len(self)))]
|
| 775 |
+
return self._space.adapter(idx)
|
| 776 |
+
|
| 777 |
+
def __iter__(self):
|
| 778 |
+
for i in range(len(self)):
|
| 779 |
+
yield self._space.adapter(i)
|
| 780 |
+
|
| 781 |
+
|
| 782 |
+
class FactoredCandidateSpace(CandidateSpace):
|
| 783 |
+
"""Factored permutation space decomposing candidate adapters into orthogonal input/output factors.
|
| 784 |
+
|
| 785 |
+
Instead of enumerating N_I * N_O * (1 + N_O) adapters in flat memory, decomposes
|
| 786 |
+
the hypothesis space into P_I x P_O (and temporal initial delay buffers).
|
| 787 |
+
Semantic evaluation depends strictly on P_I; anchors prune P_O independently;
|
| 788 |
+
and combined survivor filtering scales beyond 17,280+ models with complete survivor semantics.
|
| 789 |
+
"""
|
| 790 |
+
|
| 791 |
+
def __init__(self, machine: Machine, contract: Contract, temporal: bool = False):
|
| 792 |
+
if not isinstance(machine, Machine) or not isinstance(contract, Contract):
|
| 793 |
+
raise TypeError('FactoredCandidateSpace requires a Machine and Contract')
|
| 794 |
+
if max(contract.goal, contract.hazard) >= machine.outputs:
|
| 795 |
+
raise ValueError('contract symbols exceed source alphabet')
|
| 796 |
+
self.machine, self.contract = machine, contract
|
| 797 |
+
self.temporal = bool(temporal)
|
| 798 |
+
|
| 799 |
+
self.input_perms = tuple(permutations(range(machine.inputs)))
|
| 800 |
+
self.output_perms = tuple(permutations(range(machine.outputs)))
|
| 801 |
+
self.num_input_perms = len(self.input_perms)
|
| 802 |
+
self.num_output_perms = len(self.output_perms)
|
| 803 |
+
self.static_count = self.num_input_perms * self.num_output_perms
|
| 804 |
+
|
| 805 |
+
if self.temporal:
|
| 806 |
+
self.temporal_count = self.static_count * machine.outputs
|
| 807 |
+
self.total_adapters = self.static_count + self.temporal_count
|
| 808 |
+
else:
|
| 809 |
+
self.temporal_count = 0
|
| 810 |
+
self.total_adapters = self.static_count
|
| 811 |
+
|
| 812 |
+
self.work = {'source_steps': 0, 'candidate_expansions': 0, 'pair_checks': 0}
|
| 813 |
+
self._source = {(): (machine.initial, ())}
|
| 814 |
+
self._input_traces = {}
|
| 815 |
+
self._safe_input_factors = {}
|
| 816 |
+
self._adapter_cache = {}
|
| 817 |
+
self._predictions = {}
|
| 818 |
+
self._robust_plans = {}
|
| 819 |
+
self._signatures = {}
|
| 820 |
+
self._pair_weights = {}
|
| 821 |
+
self._deployment_words = None
|
| 822 |
+
self._probes = words(machine.inputs, 3)
|
| 823 |
+
self._adapters_proxy = _LazyAdapters(self)
|
| 824 |
+
|
| 825 |
+
@property
|
| 826 |
+
def adapters(self):
|
| 827 |
+
return self._adapters_proxy
|
| 828 |
+
|
| 829 |
+
@property
|
| 830 |
+
def _deployment(self):
|
| 831 |
+
"""Compatibility word bank, materialized only for exhaustive consumers."""
|
| 832 |
+
if self._deployment_words is None:
|
| 833 |
+
self._deployment_words = words(self.machine.inputs, self.contract.horizon)
|
| 834 |
+
return self._deployment_words
|
| 835 |
+
|
| 836 |
+
def decompose_id(self, identifier: int) -> tuple[int, int, int, int]:
|
| 837 |
+
"""Convert linear adapter id into (input_perm_idx, output_perm_idx, delay, init_output)."""
|
| 838 |
+
_integer(identifier, 'adapter id', maximum=self.total_adapters - 1)
|
| 839 |
+
if identifier < self.static_count:
|
| 840 |
+
i_idx = identifier // self.num_output_perms
|
| 841 |
+
o_idx = identifier % self.num_output_perms
|
| 842 |
+
return i_idx, o_idx, 0, 0
|
| 843 |
+
else:
|
| 844 |
+
rem = identifier - self.static_count
|
| 845 |
+
init_output = rem % self.machine.outputs
|
| 846 |
+
pair_idx = rem // self.machine.outputs
|
| 847 |
+
i_idx = pair_idx // self.num_output_perms
|
| 848 |
+
o_idx = pair_idx % self.num_output_perms
|
| 849 |
+
return i_idx, o_idx, 1, init_output
|
| 850 |
+
|
| 851 |
+
def compose_id(self, i_idx: int, o_idx: int, delay: int = 0, init_output: int = 0) -> int:
|
| 852 |
+
"""Compose (input_perm_idx, output_perm_idx, delay, init_output) into canonical linear id."""
|
| 853 |
+
_integer(i_idx, 'input factor', maximum=self.num_input_perms - 1)
|
| 854 |
+
_integer(o_idx, 'output factor', maximum=self.num_output_perms - 1)
|
| 855 |
+
_integer(delay, 'delay', maximum=1)
|
| 856 |
+
_integer(init_output, 'init_output', maximum=self.machine.outputs - 1)
|
| 857 |
+
if delay == 0:
|
| 858 |
+
if init_output:
|
| 859 |
+
raise ValueError('static adapter init_output must be zero')
|
| 860 |
+
return i_idx * self.num_output_perms + o_idx
|
| 861 |
+
if not self.temporal:
|
| 862 |
+
raise ValueError('temporal candidates are not enabled')
|
| 863 |
+
pair_idx = i_idx * self.num_output_perms + o_idx
|
| 864 |
+
return self.static_count + pair_idx * self.machine.outputs + init_output
|
| 865 |
+
|
| 866 |
+
def adapter(self, identifier: int) -> Adapter:
|
| 867 |
+
_integer(identifier, 'adapter id', maximum=self.total_adapters - 1)
|
| 868 |
+
if identifier not in self._adapter_cache:
|
| 869 |
+
i_idx, o_idx, delay, init_output = self.decompose_id(identifier)
|
| 870 |
+
self._adapter_cache[identifier] = Adapter(
|
| 871 |
+
id=identifier,
|
| 872 |
+
input_map=self.input_perms[i_idx],
|
| 873 |
+
output_map=self.output_perms[o_idx],
|
| 874 |
+
delay=delay,
|
| 875 |
+
init_output=init_output
|
| 876 |
+
)
|
| 877 |
+
return self._adapter_cache[identifier]
|
| 878 |
+
|
| 879 |
+
def _adapter(self, identifier):
|
| 880 |
+
return self.adapter(identifier)
|
| 881 |
+
|
| 882 |
+
def _ids(self, ids):
|
| 883 |
+
ids = tuple(ids)
|
| 884 |
+
for identifier in ids:
|
| 885 |
+
_integer(identifier, 'adapter id', maximum=self.total_adapters - 1)
|
| 886 |
+
return tuple(sorted(set(ids)))
|
| 887 |
+
|
| 888 |
+
def level_ids(self, level):
|
| 889 |
+
if level == 'static':
|
| 890 |
+
return tuple(range(self.static_count))
|
| 891 |
+
if level == 'temporal':
|
| 892 |
+
return tuple(range(self.total_adapters))
|
| 893 |
+
raise ValueError('unknown hypothesis level')
|
| 894 |
+
|
| 895 |
+
def _input_trace(self, i_idx: int, word: tuple[int, ...]) -> tuple[int, ...]:
|
| 896 |
+
key = (i_idx, word)
|
| 897 |
+
if key not in self._input_traces:
|
| 898 |
+
input_map = self.input_perms[i_idx]
|
| 899 |
+
mapped = tuple(input_map[x] for x in word)
|
| 900 |
+
self._input_traces[key] = self._source_trace(mapped)
|
| 901 |
+
return self._input_traces[key]
|
| 902 |
+
|
| 903 |
+
def _safe_for_input_factor(self, i_idx: int) -> frozenset:
|
| 904 |
+
if i_idx not in self._safe_input_factors:
|
| 905 |
+
self._safe_input_factors[i_idx] = frozenset(
|
| 906 |
+
w for w in self._deployment if self.contract.accepts(self._input_trace(i_idx, w))
|
| 907 |
+
)
|
| 908 |
+
return self._safe_input_factors[i_idx]
|
| 909 |
+
|
| 910 |
+
def semantic(self, identifier: int, word: tuple[int, ...]) -> tuple[int, ...]:
|
| 911 |
+
i_idx, _, _, _ = self.decompose_id(identifier)
|
| 912 |
+
word = _word(word, self.machine.inputs)
|
| 913 |
+
return self._input_trace(i_idx, word)
|
| 914 |
+
|
| 915 |
+
def predict(self, identifier: int, word: tuple[int, ...]) -> tuple[int, ...]:
|
| 916 |
+
i_idx, o_idx, delay, init_o = self.decompose_id(identifier)
|
| 917 |
+
word = _word(word, self.machine.inputs)
|
| 918 |
+
key = (identifier, word)
|
| 919 |
+
if key in self._predictions:
|
| 920 |
+
return self._predictions[key]
|
| 921 |
+
semantic = self._input_trace(i_idx, word)
|
| 922 |
+
out_map = self.output_perms[o_idx]
|
| 923 |
+
self.work['candidate_expansions'] += 1
|
| 924 |
+
if delay == 0:
|
| 925 |
+
result = tuple(out_map[s] for s in semantic)
|
| 926 |
+
else:
|
| 927 |
+
result = tuple(out_map[init_o if t == 0 else semantic[t - 1]] for t in range(len(semantic)))
|
| 928 |
+
self._predictions[key] = result
|
| 929 |
+
return result
|
| 930 |
+
|
| 931 |
+
def safe_words(self, identifier: int) -> frozenset:
|
| 932 |
+
i_idx, _, _, _ = self.decompose_id(identifier)
|
| 933 |
+
return self._safe_for_input_factor(i_idx)
|
| 934 |
+
|
| 935 |
+
def probe_is_safe(self, ids, word):
|
| 936 |
+
"""Factored prefix hazard check over unique active input factors O(|I_surv|)."""
|
| 937 |
+
ids = self._ids(ids)
|
| 938 |
+
word = _word(word, self.machine.inputs)
|
| 939 |
+
if not ids:
|
| 940 |
+
return False
|
| 941 |
+
active_i = {self.decompose_id(i)[0] for i in ids}
|
| 942 |
+
return all(self.contract.hazard not in self._input_trace(i_idx, word) for i_idx in active_i)
|
| 943 |
+
|
| 944 |
+
def survivors(self, transcripts, anchors=None) -> tuple[int, ...]:
|
| 945 |
+
evidence, anchors = self._evidence(transcripts, anchors)
|
| 946 |
+
|
| 947 |
+
valid_o_indices = []
|
| 948 |
+
for o_idx, o_perm in enumerate(self.output_perms):
|
| 949 |
+
if all(o_perm[sem] == raw for raw, sem in anchors.items()):
|
| 950 |
+
valid_o_indices.append(o_idx)
|
| 951 |
+
|
| 952 |
+
if not valid_o_indices:
|
| 953 |
+
return ()
|
| 954 |
+
|
| 955 |
+
surviving_ids = []
|
| 956 |
+
|
| 957 |
+
# Check static adapters
|
| 958 |
+
for i_idx in range(self.num_input_perms):
|
| 959 |
+
sem_traces = [self._input_trace(i_idx, word) for word, _ in evidence]
|
| 960 |
+
for o_idx in valid_o_indices:
|
| 961 |
+
o_perm = self.output_perms[o_idx]
|
| 962 |
+
match = True
|
| 963 |
+
for sem, (_, outputs) in zip(sem_traces, evidence):
|
| 964 |
+
if any(o_perm[s] != out for s, out in zip(sem, outputs)):
|
| 965 |
+
match = False
|
| 966 |
+
break
|
| 967 |
+
if match:
|
| 968 |
+
surviving_ids.append(self.compose_id(i_idx, o_idx, delay=0))
|
| 969 |
+
|
| 970 |
+
# Check temporal adapters if enabled
|
| 971 |
+
if self.temporal:
|
| 972 |
+
for i_idx in range(self.num_input_perms):
|
| 973 |
+
sem_traces = [self._input_trace(i_idx, word) for word, _ in evidence]
|
| 974 |
+
for o_idx in valid_o_indices:
|
| 975 |
+
o_perm = self.output_perms[o_idx]
|
| 976 |
+
for init_o in range(self.machine.outputs):
|
| 977 |
+
match = True
|
| 978 |
+
for sem, (_, outputs) in zip(sem_traces, evidence):
|
| 979 |
+
for t, out in enumerate(outputs):
|
| 980 |
+
pred_s = init_o if t == 0 else sem[t - 1]
|
| 981 |
+
if o_perm[pred_s] != out:
|
| 982 |
+
match = False
|
| 983 |
+
break
|
| 984 |
+
if not match:
|
| 985 |
+
break
|
| 986 |
+
if match:
|
| 987 |
+
surviving_ids.append(self.compose_id(i_idx, o_idx, delay=1, init_output=init_o))
|
| 988 |
+
|
| 989 |
+
return tuple(sorted(surviving_ids))
|
| 990 |
+
|
| 991 |
+
def plan(self, ids, mode='robust', transcripts=None, anchors=None, risk_tolerance=0.0, *, admission=None,
|
| 992 |
+
node_limit=20000, cancel=None):
|
| 993 |
+
if admission is not None:
|
| 994 |
+
mode = admission
|
| 995 |
+
if mode not in ('robust', 'one_best', 'unique', 'fit', 'identity', 'abstain', 'risk_controlled'):
|
| 996 |
+
raise ValueError('unknown admission mode')
|
| 997 |
+
if type(risk_tolerance) not in (int, float) or not 0 <= risk_tolerance < 1:
|
| 998 |
+
raise ValueError('risk_tolerance must be a finite number in [0, 1)')
|
| 999 |
+
_integer(node_limit, 'node_limit', 1)
|
| 1000 |
+
self._check_plan_cancel(cancel)
|
| 1001 |
+
ids = self._ids(ids)
|
| 1002 |
+
if mode == 'risk_controlled':
|
| 1003 |
+
if not ids:
|
| 1004 |
+
return {'status': 'FAMILY_CONTRADICTED', 'admitted': False, 'word': None,
|
| 1005 |
+
'candidate_count': 0, 'risk_metrics': {'hazard_risk': None, 'goal_rate': None, 'tolerance': risk_tolerance}}
|
| 1006 |
+
n = len(ids)
|
| 1007 |
+
i_counts = Counter(self.decompose_id(i)[0] for i in ids)
|
| 1008 |
+
admissible_candidates = []
|
| 1009 |
+
for w in self._deployment:
|
| 1010 |
+
h_count = sum(count for i_idx, count in i_counts.items()
|
| 1011 |
+
if self.contract.hazard in self._input_trace(i_idx, w))
|
| 1012 |
+
g_count = sum(count for i_idx, count in i_counts.items()
|
| 1013 |
+
if w in self._safe_for_input_factor(i_idx))
|
| 1014 |
+
h_risk = h_count / n
|
| 1015 |
+
g_rate = g_count / n
|
| 1016 |
+
if h_risk <= risk_tolerance and (n - g_count) / n <= risk_tolerance:
|
| 1017 |
+
admissible_candidates.append((w, h_risk, g_rate))
|
| 1018 |
+
if not admissible_candidates:
|
| 1019 |
+
return {'status': 'UNRESOLVED', 'admitted': False, 'word': None,
|
| 1020 |
+
'candidate_count': n, 'risk_metrics': {'hazard_risk': None, 'goal_rate': None, 'tolerance': risk_tolerance}}
|
| 1021 |
+
best_w, best_h_risk, best_g_rate = min(
|
| 1022 |
+
admissible_candidates,
|
| 1023 |
+
key=lambda item: (len(item[0]), item[1], -item[2], item[0])
|
| 1024 |
+
)
|
| 1025 |
+
status = 'IDENTIFIED' if n == 1 else ('TASK_ADMISSIBLE' if best_h_risk == 0.0 and best_g_rate == 1.0 else 'RISK_CONTROLLED_ADMISSIBLE')
|
| 1026 |
+
return {
|
| 1027 |
+
'status': status,
|
| 1028 |
+
'admitted': True,
|
| 1029 |
+
'word': list(best_w),
|
| 1030 |
+
'candidate_count': n,
|
| 1031 |
+
'risk_metrics': {
|
| 1032 |
+
'hazard_risk': best_h_risk,
|
| 1033 |
+
'goal_rate': best_g_rate,
|
| 1034 |
+
'tolerance': risk_tolerance,
|
| 1035 |
+
'candidate_count': n
|
| 1036 |
+
}
|
| 1037 |
+
}
|
| 1038 |
+
if mode == 'robust':
|
| 1039 |
+
if not ids:
|
| 1040 |
+
return {'status': 'FAMILY_CONTRADICTED', 'admitted': False, 'word': None, 'candidate_count': 0}
|
| 1041 |
+
active_i = tuple(sorted({self.decompose_id(i)[0] for i in ids}))
|
| 1042 |
+
chosen, complete, expanded = self._robust_word(active_i, node_limit, cancel)
|
| 1043 |
+
if not complete:
|
| 1044 |
+
return {'status': 'SEARCH_LIMIT', 'admitted': False, 'word': None, 'candidate_count': len(ids),
|
| 1045 |
+
'search': {'complete': False, 'expanded_nodes': expanded, 'node_limit': node_limit,
|
| 1046 |
+
'horizon': self.contract.horizon, 'strategy': 'input_factor_product_bfs'}}
|
| 1047 |
+
status = ('IDENTIFIED' if len(ids) == 1 else 'TASK_ADMISSIBLE') if chosen is not None else 'UNRESOLVED'
|
| 1048 |
+
return {'status': status, 'admitted': chosen is not None, 'word': list(chosen) if chosen is not None else None,
|
| 1049 |
+
'candidate_count': len(ids)}
|
| 1050 |
+
if mode == 'identity':
|
| 1051 |
+
safe = set(self.safe_words(0))
|
| 1052 |
+
word = min(safe, key=lambda w: (len(w), w)) if safe else None
|
| 1053 |
+
status = ('IDENTIFIED' if len(ids) == 1 else 'TASK_ADMISSIBLE') if word is not None else (
|
| 1054 |
+
'FAMILY_CONTRADICTED' if not ids else 'UNRESOLVED')
|
| 1055 |
+
return {'status': status, 'admitted': word is not None, 'word': list(word) if word is not None else None,
|
| 1056 |
+
'candidate_count': len(ids)}
|
| 1057 |
+
return super().plan(ids, mode=mode, transcripts=transcripts, anchors=anchors, risk_tolerance=risk_tolerance)
|
| 1058 |
+
|
| 1059 |
+
@staticmethod
|
| 1060 |
+
def _check_plan_cancel(cancel):
|
| 1061 |
+
if cancel and cancel():
|
| 1062 |
+
from .study import Cancelled
|
| 1063 |
+
raise Cancelled('Factored planning cancelled')
|
| 1064 |
+
|
| 1065 |
+
def _robust_word(self, active_i, node_limit, cancel):
|
| 1066 |
+
"""Search safe joint input-factor states in shortest/lexicographic order."""
|
| 1067 |
+
if active_i in self._robust_plans:
|
| 1068 |
+
return self._robust_plans[active_i], True, 0
|
| 1069 |
+
input_maps = tuple(self.input_perms[i] for i in active_i)
|
| 1070 |
+
start = (self.machine.initial,) * len(input_maps)
|
| 1071 |
+
pending, visited = deque([(start, ())]), {start}
|
| 1072 |
+
expanded = 0
|
| 1073 |
+
while pending:
|
| 1074 |
+
self._check_plan_cancel(cancel)
|
| 1075 |
+
if expanded >= node_limit:
|
| 1076 |
+
return None, False, expanded
|
| 1077 |
+
states, prefix = pending.popleft()
|
| 1078 |
+
expanded += 1
|
| 1079 |
+
for action in range(self.machine.inputs):
|
| 1080 |
+
self._check_plan_cancel(cancel)
|
| 1081 |
+
next_states, goal = [], True
|
| 1082 |
+
for state, input_map in zip(states, input_maps):
|
| 1083 |
+
following, output = self.machine.transitions[state][input_map[action]]
|
| 1084 |
+
self.work['source_steps'] += 1
|
| 1085 |
+
if output == self.contract.hazard:
|
| 1086 |
+
break
|
| 1087 |
+
next_states.append(following)
|
| 1088 |
+
goal = goal and output == self.contract.goal
|
| 1089 |
+
else:
|
| 1090 |
+
word = prefix + (action,)
|
| 1091 |
+
# A goal edge can return to an already visited state.
|
| 1092 |
+
if goal:
|
| 1093 |
+
self._robust_plans[active_i] = word
|
| 1094 |
+
return word, True, expanded
|
| 1095 |
+
following = tuple(next_states)
|
| 1096 |
+
if len(word) < self.contract.horizon and following not in visited:
|
| 1097 |
+
visited.add(following)
|
| 1098 |
+
pending.append((following, word))
|
| 1099 |
+
self._robust_plans[active_i] = None
|
| 1100 |
+
return None, True, expanded
|
| 1101 |
+
|
| 1102 |
+
def factor_summary(self, survivors=None) -> dict:
|
| 1103 |
+
"""Report structural factor counts; the ratio is not measured memory compression."""
|
| 1104 |
+
if survivors is None:
|
| 1105 |
+
survivors = tuple(range(self.total_adapters))
|
| 1106 |
+
else:
|
| 1107 |
+
survivors = self._ids(survivors)
|
| 1108 |
+
active_i = set()
|
| 1109 |
+
active_o = set()
|
| 1110 |
+
active_delay = set()
|
| 1111 |
+
for sid in survivors:
|
| 1112 |
+
i_idx, o_idx, delay, _ = self.decompose_id(sid)
|
| 1113 |
+
active_i.add(i_idx)
|
| 1114 |
+
active_o.add(o_idx)
|
| 1115 |
+
active_delay.add(delay)
|
| 1116 |
+
return {
|
| 1117 |
+
'total_models': self.total_adapters,
|
| 1118 |
+
'static_models': self.static_count,
|
| 1119 |
+
'temporal_models': self.temporal_count,
|
| 1120 |
+
'input_alphabet': self.machine.inputs,
|
| 1121 |
+
'output_alphabet': self.machine.outputs,
|
| 1122 |
+
'input_permutations': self.num_input_perms,
|
| 1123 |
+
'output_permutations': self.num_output_perms,
|
| 1124 |
+
'survivors_count': len(survivors),
|
| 1125 |
+
'active_input_factors': len(active_i),
|
| 1126 |
+
'active_output_factors': len(active_o),
|
| 1127 |
+
'active_delay_modes': sorted(active_delay),
|
| 1128 |
+
'compression_ratio': round(self.total_adapters / max(1, (self.num_input_perms + self.num_output_perms)), 2),
|
| 1129 |
+
'compression_ratio_scope': 'Structural candidate/factor ratio; not measured memory compression.',
|
| 1130 |
+
}
|
crosswire/experiment_checks.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Independent finite checks for bounded, reset-separated experiment trees.
|
| 2 |
+
|
| 3 |
+
Only canonical content hashing is shared with the scientific runtime. Candidate
|
| 4 |
+
enumeration, reference execution, belief updates and goal checks are reconstructed
|
| 5 |
+
here without importing candidate spaces, experiment search or adaptive planners.
|
| 6 |
+
"""
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
from itertools import permutations, product
|
| 10 |
+
import json
|
| 11 |
+
|
| 12 |
+
from .identity import canonical_hash
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
SCOPE = ('Conditional finite public-model check: assumes target inclusion, stationarity, '
|
| 16 |
+
'deterministic resets and truthful anchors. Does not authenticate observations '
|
| 17 |
+
'or target execution, validate private audits, or establish semantic grounding. '
|
| 18 |
+
'Model counts are not calibrated probabilities; unresolved leaves make no '
|
| 19 |
+
'impossibility claim.')
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _integer(value, maximum, name, minimum=0):
|
| 23 |
+
if type(value) is not int or not minimum <= value <= maximum:
|
| 24 |
+
raise ValueError(f'Invalid {name}: expected an integer in {minimum}..{maximum}')
|
| 25 |
+
return value
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _sequence(value, alphabet, name, maximum=512):
|
| 29 |
+
if not isinstance(value, (list, tuple)) or len(value) > maximum:
|
| 30 |
+
raise ValueError(f'Invalid or oversized {name}')
|
| 31 |
+
return tuple(_integer(symbol, alphabet - 1, name) for symbol in value)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def _public_model(reference, contract):
|
| 35 |
+
if (not isinstance(reference, dict) or set(reference) -
|
| 36 |
+
{'name', 'transitions', 'inputs', 'outputs', 'initial'}):
|
| 37 |
+
raise ValueError('Invalid public reference')
|
| 38 |
+
name = reference['name']
|
| 39 |
+
if not isinstance(name, str) or not name:
|
| 40 |
+
raise ValueError('Reference name must be a nonempty string')
|
| 41 |
+
inputs = _integer(reference.get('inputs', 3), 3, 'reference inputs', 1)
|
| 42 |
+
outputs = _integer(reference.get('outputs', 4), 4, 'reference outputs', 1)
|
| 43 |
+
rows = reference['transitions']
|
| 44 |
+
if not isinstance(rows, (list, tuple)) or not 1 <= len(rows) <= 128:
|
| 45 |
+
raise ValueError('Reference must contain 1..128 states')
|
| 46 |
+
initial = _integer(reference.get('initial', 0), len(rows) - 1, 'initial state')
|
| 47 |
+
transitions = []
|
| 48 |
+
for row in rows:
|
| 49 |
+
if not isinstance(row, (list, tuple)) or len(row) != inputs:
|
| 50 |
+
raise ValueError('Reference transition row does not match input alphabet')
|
| 51 |
+
checked_row = []
|
| 52 |
+
for cell in row:
|
| 53 |
+
if not isinstance(cell, (list, tuple)) or len(cell) != 2:
|
| 54 |
+
raise ValueError('Reference transition needs a state and output')
|
| 55 |
+
checked_row.append([_integer(cell[0], len(rows) - 1, 'successor state'),
|
| 56 |
+
_integer(cell[1], outputs - 1, 'semantic output')])
|
| 57 |
+
transitions.append(checked_row)
|
| 58 |
+
if not isinstance(contract, dict) or set(contract) - {'goal', 'hazard', 'horizon'}:
|
| 59 |
+
raise ValueError('Invalid public contract')
|
| 60 |
+
goal = _integer(contract.get('goal', 1), outputs - 1, 'contract goal')
|
| 61 |
+
hazard = _integer(contract.get('hazard', 2), outputs - 1, 'contract hazard')
|
| 62 |
+
horizon = _integer(contract.get('horizon', 4), 6, 'contract horizon', 1)
|
| 63 |
+
if goal == hazard:
|
| 64 |
+
raise ValueError('Contract goal and hazard must differ')
|
| 65 |
+
return ({'name': name, 'inputs': inputs, 'outputs': outputs, 'initial': initial,
|
| 66 |
+
'transitions': transitions}, {'goal': goal, 'hazard': hazard, 'horizon': horizon})
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def _observations(evidence, inputs, outputs):
|
| 70 |
+
if not isinstance(evidence, (list, tuple)) or len(evidence) > 512:
|
| 71 |
+
raise ValueError('Evidence must contain at most 512 entries')
|
| 72 |
+
normalized, steps = [], 0
|
| 73 |
+
for entry in evidence:
|
| 74 |
+
if not isinstance(entry, dict) or type(entry.get('complete')) is not bool:
|
| 75 |
+
raise ValueError('Evidence must declare boolean completeness')
|
| 76 |
+
word = _sequence(entry['word'], inputs, 'evidence word')
|
| 77 |
+
observed = _sequence(entry['outputs'], outputs, 'evidence output')
|
| 78 |
+
if len(observed) > len(word) or (entry['complete'] and len(observed) != len(word)):
|
| 79 |
+
raise ValueError('Evidence word/output lengths differ')
|
| 80 |
+
steps += len(observed)
|
| 81 |
+
if steps > 512:
|
| 82 |
+
raise ValueError('Evidence exceeds 512 observed steps')
|
| 83 |
+
if observed:
|
| 84 |
+
normalized.append({'word': list(word[:len(observed)]),
|
| 85 |
+
'outputs': list(observed), 'complete': True})
|
| 86 |
+
return normalized
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def _anchors(anchors, outputs):
|
| 90 |
+
if anchors is None:
|
| 91 |
+
anchors = {}
|
| 92 |
+
if not isinstance(anchors, dict):
|
| 93 |
+
raise ValueError('Anchors must be an object')
|
| 94 |
+
normalized = {}
|
| 95 |
+
for raw, semantic in anchors.items():
|
| 96 |
+
if isinstance(raw, str) and raw in {str(i) for i in range(outputs)}:
|
| 97 |
+
raw = int(raw)
|
| 98 |
+
raw = _integer(raw, outputs - 1, 'anchor raw output')
|
| 99 |
+
semantic = _integer(semantic, outputs - 1, 'anchor semantic output')
|
| 100 |
+
if raw in normalized and normalized[raw] != semantic:
|
| 101 |
+
raise ValueError('Conflicting normalized anchor keys')
|
| 102 |
+
normalized[raw] = semantic
|
| 103 |
+
return sorted(normalized.items())
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def verify_experiment_tree(reference: dict, contract: dict, tree: dict, *,
|
| 107 |
+
temporal=False, evidence=(), anchors=None,
|
| 108 |
+
budget: int, safe_only=False) -> dict:
|
| 109 |
+
"""Reconstruct beliefs, branch coverage, readiness and reset/action costs.
|
| 110 |
+
|
| 111 |
+
``budget`` is the remaining acquisition-call budget for this proposed tree.
|
| 112 |
+
Deployment words have their separate contract horizon. Incomplete evidence
|
| 113 |
+
contributes every observed prefix; its unobserved suffix is never evidence.
|
| 114 |
+
"""
|
| 115 |
+
report = {'schema': 'crosswire-experiment-certificate-v1', 'valid': False,
|
| 116 |
+
'checked_nodes': 0, 'scope': SCOPE}
|
| 117 |
+
try:
|
| 118 |
+
if type(temporal) is not bool or type(safe_only) is not bool:
|
| 119 |
+
raise ValueError('Temporal and safe_only settings must be booleans')
|
| 120 |
+
budget = _integer(budget, 512, 'remaining acquisition budget')
|
| 121 |
+
reference, contract = _public_model(reference, contract)
|
| 122 |
+
inputs, outputs = reference['inputs'], reference['outputs']
|
| 123 |
+
observed = _observations(evidence, inputs, outputs)
|
| 124 |
+
anchor_pairs = _anchors(anchors, outputs)
|
| 125 |
+
pairs = list(product(permutations(range(inputs)), permutations(range(outputs))))
|
| 126 |
+
adapters = [(input_map, output_map, 0, 0) for input_map, output_map in pairs]
|
| 127 |
+
if temporal:
|
| 128 |
+
adapters.extend((input_map, output_map, 1, initial)
|
| 129 |
+
for input_map, output_map in pairs for initial in range(outputs))
|
| 130 |
+
semantic_cache = {}
|
| 131 |
+
|
| 132 |
+
def trace(identifier, word):
|
| 133 |
+
input_map, output_map, delay, initial_buffer = adapters[identifier]
|
| 134 |
+
cache_key = (input_map, word)
|
| 135 |
+
if cache_key not in semantic_cache:
|
| 136 |
+
state, semantic = reference['initial'], []
|
| 137 |
+
for action in word:
|
| 138 |
+
state, output = reference['transitions'][state][input_map[action]]
|
| 139 |
+
semantic.append(output)
|
| 140 |
+
semantic_cache[cache_key] = tuple(semantic)
|
| 141 |
+
semantic = semantic_cache[cache_key]
|
| 142 |
+
emitted = (initial_buffer,) + semantic[:-1] if delay and word else semantic
|
| 143 |
+
return tuple(output_map[output] for output in emitted), semantic
|
| 144 |
+
|
| 145 |
+
belief = tuple(i for i, adapter in enumerate(adapters)
|
| 146 |
+
if all(adapter[1][semantic] == raw for raw, semantic in anchor_pairs))
|
| 147 |
+
for entry in observed:
|
| 148 |
+
word, response = tuple(entry['word']), tuple(entry['outputs'])
|
| 149 |
+
belief = tuple(i for i in belief if trace(i, word)[0] == response)
|
| 150 |
+
report['candidate_count'] = len(belief)
|
| 151 |
+
ready_models = largest_unresolved = terminal_squared = worst_case_calls = depth = 0
|
| 152 |
+
|
| 153 |
+
def visit(node, members, calls, query_depth):
|
| 154 |
+
nonlocal ready_models, largest_unresolved, terminal_squared, worst_case_calls, depth
|
| 155 |
+
if report['checked_nodes'] >= 10000:
|
| 156 |
+
raise ValueError('Experiment tree exceeds the 10000 checked-node limit')
|
| 157 |
+
report['checked_nodes'] += 1
|
| 158 |
+
if not isinstance(node, dict):
|
| 159 |
+
raise ValueError('Experiment nodes must be objects')
|
| 160 |
+
kind = node.get('kind')
|
| 161 |
+
if kind == 'ready' and set(node) == {'kind', 'deployment_word'}:
|
| 162 |
+
word = _sequence(node['deployment_word'], inputs, 'deployment word', contract['horizon'])
|
| 163 |
+
if not members or not word:
|
| 164 |
+
raise ValueError('Empty belief or deployment word cannot certify readiness')
|
| 165 |
+
for identifier in members:
|
| 166 |
+
semantic = trace(identifier, word)[1]
|
| 167 |
+
if contract['hazard'] in semantic or semantic[-1] != contract['goal']:
|
| 168 |
+
raise ValueError('Ready leaf does not satisfy every surviving interpretation')
|
| 169 |
+
ready_models += len(members)
|
| 170 |
+
normalized = {'kind': 'ready', 'deployment_word': list(word)}
|
| 171 |
+
elif kind == 'unresolved' and set(node) == {'kind'}:
|
| 172 |
+
largest_unresolved = max(largest_unresolved, len(members))
|
| 173 |
+
normalized = {'kind': 'unresolved'}
|
| 174 |
+
elif kind == 'query' and set(node) == {'kind', 'word', 'branches'}:
|
| 175 |
+
if not members:
|
| 176 |
+
raise ValueError('An empty belief cannot certify an experiment query')
|
| 177 |
+
if query_depth >= 2:
|
| 178 |
+
raise ValueError('Experiment tree exceeds query depth two')
|
| 179 |
+
word = _sequence(node['word'], inputs, 'query word', 3)
|
| 180 |
+
if not word:
|
| 181 |
+
raise ValueError('Query word must have length 1..3')
|
| 182 |
+
next_calls = calls + 1 + len(word)
|
| 183 |
+
if next_calls > budget:
|
| 184 |
+
raise ValueError('Experiment path exceeds remaining acquisition budget')
|
| 185 |
+
expected = {}
|
| 186 |
+
for identifier in members:
|
| 187 |
+
response, semantic = trace(identifier, word)
|
| 188 |
+
if safe_only and contract['hazard'] in semantic:
|
| 189 |
+
raise ValueError('Query risks a hazard under its pre-query surviving belief')
|
| 190 |
+
key = json.dumps(response, separators=(',', ':'))
|
| 191 |
+
expected.setdefault(key, []).append(identifier)
|
| 192 |
+
branches = node['branches']
|
| 193 |
+
if (not isinstance(branches, dict) or len(branches) != len(expected)
|
| 194 |
+
or set(branches) != set(expected)):
|
| 195 |
+
raise ValueError('Branches must cover exactly every complete predicted raw response')
|
| 196 |
+
depth = max(depth, query_depth + 1)
|
| 197 |
+
normalized_branches = {key: visit(branches[key], tuple(expected[key]), next_calls, query_depth + 1)
|
| 198 |
+
for key in sorted(expected)}
|
| 199 |
+
return {'kind': 'query', 'word': list(word), 'branches': normalized_branches}
|
| 200 |
+
else:
|
| 201 |
+
raise ValueError('Malformed or unsupported experiment node')
|
| 202 |
+
terminal_squared += len(members) ** 2
|
| 203 |
+
worst_case_calls = max(worst_case_calls, calls)
|
| 204 |
+
return normalized
|
| 205 |
+
|
| 206 |
+
normalized_tree = visit(tree, belief, 0, 0)
|
| 207 |
+
binding = {'reference': reference, 'contract': contract, 'tree': normalized_tree,
|
| 208 |
+
'temporal': temporal, 'evidence': observed,
|
| 209 |
+
'anchors': anchor_pairs, 'budget': budget, 'safe_only': safe_only}
|
| 210 |
+
report.update(valid=True, ready_models=ready_models,
|
| 211 |
+
largest_unresolved_belief=largest_unresolved,
|
| 212 |
+
terminal_squared_beliefs=terminal_squared,
|
| 213 |
+
worst_case_calls=worst_case_calls, depth=depth,
|
| 214 |
+
binding_hash=canonical_hash(binding))
|
| 215 |
+
except (ValueError, TypeError, KeyError, OverflowError, RecursionError) as exc:
|
| 216 |
+
report['reason'] = str(exc)
|
| 217 |
+
return report
|
crosswire/experiments.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Bounded public-model query trees; hypothetical responses are not observations."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
from .core import CandidateSpace, FactoredCandidateSpace, _integer
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def _cancel(cancel):
|
| 10 |
+
if cancel and cancel():
|
| 11 |
+
from .study import Cancelled
|
| 12 |
+
raise Cancelled('Experiment planning cancelled')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def synthesize_experiment(space, ids, budget, *, seen=(), safe_only=False,
|
| 16 |
+
cancel=None, node_limit=2048):
|
| 17 |
+
"""Rank depth-two reset-query trees by finite model-count task readiness.
|
| 18 |
+
|
| 19 |
+
Ready beliefs stop immediately. Completeness concerns this finite bank,
|
| 20 |
+
depth and deployment planner, not global conformance or target truth.
|
| 21 |
+
"""
|
| 22 |
+
_integer(budget, 'budget', maximum=512)
|
| 23 |
+
_integer(node_limit, 'node_limit', 1, 100000)
|
| 24 |
+
if type(safe_only) is not bool:
|
| 25 |
+
raise TypeError('safe_only must be boolean')
|
| 26 |
+
ids = space._ids(ids)
|
| 27 |
+
seen = {tuple(word) for word in seen}
|
| 28 |
+
pool = tuple(sorted((word for word in space._probes if word not in seen), key=lambda w: (len(w), w)))
|
| 29 |
+
ready_cache, leaf_cache, projections = {}, {}, {}
|
| 30 |
+
memo, partitions, word_partitions, response_keys = {}, {}, {}, {}
|
| 31 |
+
root_bits = {identifier: 1 << offset for offset, identifier in enumerate(ids)}
|
| 32 |
+
root_mask = (1 << len(ids)) - 1
|
| 33 |
+
belief_masks, mask_members = {ids: root_mask}, {root_mask: ids, 0: ()}
|
| 34 |
+
expanded, best_root, readiness_limited = 0, None, False
|
| 35 |
+
|
| 36 |
+
class Limit(Exception):
|
| 37 |
+
pass
|
| 38 |
+
|
| 39 |
+
def consume():
|
| 40 |
+
nonlocal expanded
|
| 41 |
+
_cancel(cancel)
|
| 42 |
+
if expanded >= node_limit:
|
| 43 |
+
raise Limit
|
| 44 |
+
expanded += 1
|
| 45 |
+
|
| 46 |
+
def readiness_inputs(belief):
|
| 47 |
+
if type(space) not in (CandidateSpace, FactoredCandidateSpace):
|
| 48 |
+
return belief, belief
|
| 49 |
+
if belief not in projections:
|
| 50 |
+
representatives = {}
|
| 51 |
+
for identifier in belief:
|
| 52 |
+
mapping = (space.input_perms[space.decompose_id(identifier)[0]]
|
| 53 |
+
if type(space) is FactoredCandidateSpace else space.adapters[identifier].input_map)
|
| 54 |
+
representatives.setdefault(mapping, identifier)
|
| 55 |
+
key = tuple(sorted(representatives))
|
| 56 |
+
projections[belief] = key, tuple(representatives[mapping] for mapping in key)
|
| 57 |
+
return projections[belief]
|
| 58 |
+
|
| 59 |
+
def leaf(belief):
|
| 60 |
+
nonlocal readiness_limited
|
| 61 |
+
_cancel(cancel)
|
| 62 |
+
key, representatives = readiness_inputs(belief)
|
| 63 |
+
if key not in ready_cache:
|
| 64 |
+
plan = space.plan(representatives, 'robust')
|
| 65 |
+
readiness_limited |= plan.get('status') == 'SEARCH_LIMIT'
|
| 66 |
+
ready_cache[key] = tuple(plan['word']) if plan['admitted'] else None
|
| 67 |
+
cache_key = (key, len(belief))
|
| 68 |
+
if cache_key not in leaf_cache:
|
| 69 |
+
word, n = ready_cache[key], len(belief)
|
| 70 |
+
descriptor = ('ready', word) if word is not None else ('unresolved',)
|
| 71 |
+
# Metrics: ready count, largest unresolved, squared leaf sizes, calls.
|
| 72 |
+
leaf_cache[cache_key] = descriptor, (n if word is not None else 0,
|
| 73 |
+
0 if word is not None else n, n * n, 0)
|
| 74 |
+
return leaf_cache[cache_key]
|
| 75 |
+
|
| 76 |
+
def split(belief, word):
|
| 77 |
+
if belief not in belief_masks:
|
| 78 |
+
belief_masks[belief] = sum(root_bits[identifier] for identifier in belief)
|
| 79 |
+
mask = belief_masks[belief]
|
| 80 |
+
key = (mask, word)
|
| 81 |
+
if key not in partitions:
|
| 82 |
+
if word not in word_partitions:
|
| 83 |
+
groups = {}
|
| 84 |
+
for offset, identifier in enumerate(ids):
|
| 85 |
+
if offset % 64 == 0:
|
| 86 |
+
_cancel(cancel)
|
| 87 |
+
prediction = space.predict(identifier, word)
|
| 88 |
+
groups[prediction] = groups.get(prediction, 0) | root_bits[identifier]
|
| 89 |
+
word_partitions[word] = tuple(sorted(groups.items()))
|
| 90 |
+
restricted = []
|
| 91 |
+
for prediction, members_mask in word_partitions[word]:
|
| 92 |
+
active = mask & members_mask
|
| 93 |
+
if not active:
|
| 94 |
+
continue
|
| 95 |
+
if active not in mask_members:
|
| 96 |
+
remaining, members = active, []
|
| 97 |
+
while remaining:
|
| 98 |
+
bit = remaining & -remaining
|
| 99 |
+
members.append(ids[bit.bit_length() - 1])
|
| 100 |
+
remaining ^= bit
|
| 101 |
+
mask_members[active] = tuple(members)
|
| 102 |
+
belief_masks[mask_members[active]] = active
|
| 103 |
+
restricted.append((prediction, mask_members[active]))
|
| 104 |
+
partitions[key] = tuple(restricted)
|
| 105 |
+
return partitions[key]
|
| 106 |
+
|
| 107 |
+
def rank(metrics, word):
|
| 108 |
+
return metrics[0], -metrics[1], -metrics[2], -metrics[3], -len(word)
|
| 109 |
+
|
| 110 |
+
def descriptor_word(descriptor):
|
| 111 |
+
return descriptor[1] if descriptor[0] in ('query', 'query_bank') else ()
|
| 112 |
+
|
| 113 |
+
def candidate(belief, word, remaining, with_children):
|
| 114 |
+
cost = 1 + len(word)
|
| 115 |
+
if cost > remaining or (safe_only and not space.probe_is_safe(belief, word)):
|
| 116 |
+
return None
|
| 117 |
+
groups = split(belief, word)
|
| 118 |
+
if len(groups) <= 1:
|
| 119 |
+
return None
|
| 120 |
+
if with_children:
|
| 121 |
+
alternatives = []
|
| 122 |
+
for _, members in groups:
|
| 123 |
+
choices = options_for(members, remaining - cost)
|
| 124 |
+
most_ready = max(option[1][0] for option in choices)
|
| 125 |
+
alternatives.append([option for option in choices if option[1][0] == most_ready])
|
| 126 |
+
# The maximum couples branches. Keep alternatives until the global
|
| 127 |
+
# minimax threshold is known, then minimize additive square sums.
|
| 128 |
+
largest = max(min(option[1][1] for option in choices) for choices in alternatives)
|
| 129 |
+
selected = [min((option for option in choices if option[1][1] <= largest),
|
| 130 |
+
key=lambda option: (option[1][2], option[1][3],
|
| 131 |
+
len(descriptor_word(option[0])), descriptor_word(option[0])))
|
| 132 |
+
for choices in alternatives]
|
| 133 |
+
descriptor = ('query', word, tuple((prediction, option[0])
|
| 134 |
+
for (prediction, _), option in zip(groups, selected)))
|
| 135 |
+
else:
|
| 136 |
+
selected = [leaf(members) for _, members in groups]
|
| 137 |
+
# Retain existing partition references, not a copied tree for every
|
| 138 |
+
# alternative. Only the final selected descriptor is materialized.
|
| 139 |
+
descriptor = ('query_bank', word, groups)
|
| 140 |
+
metrics = (sum(option[1][0] for option in selected),
|
| 141 |
+
max(option[1][1] for option in selected),
|
| 142 |
+
sum(option[1][2] for option in selected),
|
| 143 |
+
cost + max(option[1][3] for option in selected))
|
| 144 |
+
return descriptor, metrics
|
| 145 |
+
|
| 146 |
+
def options_for(belief, remaining):
|
| 147 |
+
terminal = leaf(belief)
|
| 148 |
+
if terminal[0][0] == 'ready' or not belief or remaining < 2:
|
| 149 |
+
return [terminal]
|
| 150 |
+
key = (belief, remaining)
|
| 151 |
+
if key not in memo:
|
| 152 |
+
consume()
|
| 153 |
+
choices = [terminal]
|
| 154 |
+
for word in pool:
|
| 155 |
+
_cancel(cancel)
|
| 156 |
+
proposed = candidate(belief, word, remaining, False)
|
| 157 |
+
if proposed is not None:
|
| 158 |
+
choices.append(proposed)
|
| 159 |
+
most_ready = max(option[1][0] for option in choices)
|
| 160 |
+
memo[key] = [option for option in choices if option[1][0] == most_ready]
|
| 161 |
+
return memo[key]
|
| 162 |
+
|
| 163 |
+
def search():
|
| 164 |
+
nonlocal best_root
|
| 165 |
+
terminal = leaf(ids)
|
| 166 |
+
if terminal[0][0] == 'ready' or not ids or budget < 2:
|
| 167 |
+
return terminal
|
| 168 |
+
consume()
|
| 169 |
+
score = None
|
| 170 |
+
for word in pool:
|
| 171 |
+
_cancel(cancel)
|
| 172 |
+
proposed = candidate(ids, word, budget, True)
|
| 173 |
+
if proposed is not None and (score is None or rank(proposed[1], word) > score):
|
| 174 |
+
best_root, score = proposed, rank(proposed[1], word)
|
| 175 |
+
return best_root if best_root is not None else terminal
|
| 176 |
+
|
| 177 |
+
def materialize(descriptor):
|
| 178 |
+
_cancel(cancel)
|
| 179 |
+
if descriptor[0] == 'ready':
|
| 180 |
+
return {'kind': 'ready', 'deployment_word': list(descriptor[1])}
|
| 181 |
+
if descriptor[0] == 'unresolved':
|
| 182 |
+
return {'kind': 'unresolved'}
|
| 183 |
+
branches = {}
|
| 184 |
+
for prediction, child in descriptor[2]:
|
| 185 |
+
if prediction not in response_keys:
|
| 186 |
+
response_keys[prediction] = json.dumps(list(prediction), separators=(',', ':'))
|
| 187 |
+
child_descriptor = leaf(child)[0] if descriptor[0] == 'query_bank' else child
|
| 188 |
+
branches[response_keys[prediction]] = materialize(child_descriptor)
|
| 189 |
+
return {'kind': 'query', 'word': list(descriptor[1]), 'branches': branches}
|
| 190 |
+
|
| 191 |
+
complete, selection = True, 'lookahead'
|
| 192 |
+
try:
|
| 193 |
+
chosen = search()
|
| 194 |
+
except Limit:
|
| 195 |
+
complete, chosen = False, best_root
|
| 196 |
+
if chosen is None:
|
| 197 |
+
selection, score = 'one_query_fallback', None
|
| 198 |
+
for word in pool:
|
| 199 |
+
_cancel(cancel)
|
| 200 |
+
proposed = candidate(ids, word, budget, False)
|
| 201 |
+
if proposed is not None and (score is None or rank(proposed[1], word) > score):
|
| 202 |
+
chosen, score = proposed, rank(proposed[1], word)
|
| 203 |
+
if chosen is None:
|
| 204 |
+
chosen = leaf(ids)
|
| 205 |
+
tree = materialize(chosen[0])
|
| 206 |
+
space.work['experiment_search_nodes'] = space.work.get('experiment_search_nodes', 0) + expanded
|
| 207 |
+
objective = dict(zip(('ready_models', 'largest_unresolved_belief',
|
| 208 |
+
'terminal_squared_beliefs', 'worst_case_calls'), chosen[1]))
|
| 209 |
+
return {'word': tree.get('word'), 'tree': tree, 'objective': objective,
|
| 210 |
+
'depth_limit': 2, 'node_limit': node_limit, 'expanded_nodes': expanded,
|
| 211 |
+
'search_complete': complete and not readiness_limited, 'selection': selection,
|
| 212 |
+
'remaining_calls': budget,
|
| 213 |
+
'scope': 'Depth-two finite query-bank model-count readiness; hypothetical observations '
|
| 214 |
+
'are not measured evidence or calibrated target-success probabilities.'}
|
crosswire/fixtures.py
ADDED
|
@@ -0,0 +1,521 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Synthetic finite mechanisms and private semantic audit instruments.
|
| 2 |
+
|
| 3 |
+
Procedural mechanisms are authored as control logic. They never consume the
|
| 4 |
+
public reference transition table. Table mechanisms use separate literal data.
|
| 5 |
+
Seed varies labels only; generated_protocol has a separate structural seed.
|
| 6 |
+
"""
|
| 7 |
+
from collections import deque
|
| 8 |
+
from dataclasses import dataclass
|
| 9 |
+
import hashlib
|
| 10 |
+
import itertools
|
| 11 |
+
import json
|
| 12 |
+
import random
|
| 13 |
+
|
| 14 |
+
from .core import Contract, Machine
|
| 15 |
+
from .conditions import normalize_fault_schedule
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@dataclass(frozen=True)
|
| 19 |
+
class Case:
|
| 20 |
+
public: dict
|
| 21 |
+
private: dict
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
_DESCRIPTIONS = {
|
| 25 |
+
'generated_protocol': ('in_family', 'Versioned synthetic structural instances: two safe diagnostics precede one or two progress stages with independently sampled commands and fallback dynamics.'),
|
| 26 |
+
'routing': ('in_family', 'Designed adaptive-policy control: three safe diagnostic actions precede a commit that must use the distinguished command.'),
|
| 27 |
+
'latch': ('in_family', 'Arm then release a latch; hazards are absorbing.'),
|
| 28 |
+
'handshake': ('in_family', 'Three-stage handshake with a reset command.'),
|
| 29 |
+
'symmetric': ('in_family', 'Two behaviorally symmetric terminals require a semantic anchor.'),
|
| 30 |
+
'neutral': ('in_family', 'Neutral-symbol symmetry permits a shared goal plan.'),
|
| 31 |
+
'long_trace': ('in_family', 'Four consecutive advances precede a goal; probes end at length three.'),
|
| 32 |
+
'rotary': ('in_family', 'Frozen evaluation mechanism: cycle a three-position dial, submit only at position two, or reset the dial; terminals are absorbing.'),
|
| 33 |
+
'interlock': ('in_family', 'Frozen evaluation mechanism: toggle two independent flags and release only when both are set; terminals are absorbing.'),
|
| 34 |
+
'robotics_airlock': ('in_family', 'Dual-door rover airlock controller: standby diagnostics and pressure equalization precede inner-hatch seal and outer-hatch egress; breaching the inner seal while armed triggers a decompression hazard.'),
|
| 35 |
+
'protocol_session': ('in_family', 'Stateful industrial session protocol (SYN -> AUTH -> OPEN -> COMMIT): safe keepalive/handshake probes precede transaction open and commit; out-of-order commits or double-opens trigger a protocol violation hazard.'),
|
| 36 |
+
'extra_state': ('out_of_family', 'An extra progress state passes all three-action probes but postpones the goal.'),
|
| 37 |
+
'incompatible': ('out_of_family', 'An extra reachable neutral sink violates the source model.'),
|
| 38 |
+
'alias': ('out_of_family', 'Goal and hazard observations are merged after actual events; optional anchors ground only unambiguous neutral symbols.'),
|
| 39 |
+
'semantic_corruption': ('out_of_family', 'Raw-equivalent control with swapped actual terminal meanings; optional anchors report those actual meanings.'),
|
| 40 |
+
'bypass': ('out_of_family', 'Privileged checker passes while the actual connecting path is harmful; optional anchors ground actual terminal meanings.'),
|
| 41 |
+
'malformed': ('interface_error', 'Malformed, stalled or exited subprocess response.'),
|
| 42 |
+
'delay': ('out_of_family', 'One-action observation delay with reliable reset; only the absorbing-hazard anchor is universally valid.'),
|
| 43 |
+
'repeat': ('out_of_family', 'Every second observation repeats the previous symbol; only the absorbing-hazard anchor is universally valid.'),
|
| 44 |
+
'drift': ('out_of_family', 'Terminal meaning silently swaps after eight experiment-level actions; optional anchors ground only neutral symbols that remain valid across both phases.'),
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def catalog():
|
| 49 |
+
return [{'id': key, 'label': key.replace('_', ' ').title(), 'scope': scope,
|
| 50 |
+
'description': description} for key, (scope, description) in _DESCRIPTIONS.items()]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def _table(family):
|
| 54 |
+
if family == 'routing':
|
| 55 |
+
return (((1,0),(1,3),(1,3)), ((2,0),(2,3),(2,3)),
|
| 56 |
+
((3,0),(3,3),(3,3)), ((4,1),(5,2),(5,2)),
|
| 57 |
+
((4,1),(4,1),(4,1)), ((5,2),(5,2),(5,2)))
|
| 58 |
+
if family == 'latch':
|
| 59 |
+
return (((1,0),(0,3),(3,2)), ((1,3),(2,1),(3,2)),
|
| 60 |
+
((2,1),(2,1),(3,2)), ((3,2),(3,2),(3,2)))
|
| 61 |
+
if family == 'handshake':
|
| 62 |
+
return (((1,0),(0,3),(4,2)), ((1,0),(2,3),(4,2)),
|
| 63 |
+
((3,1),(0,0),(4,2)), ((3,1),(3,1),(4,2)), ((4,2),(4,2),(4,2)))
|
| 64 |
+
if family == 'symmetric':
|
| 65 |
+
return (((1,1),(2,2),(0,0)), ((1,1),(1,1),(1,1)), ((2,2),(2,2),(2,2)))
|
| 66 |
+
if family == 'neutral':
|
| 67 |
+
return (((1,1),(0,0),(0,3)), ((1,1),(1,1),(1,1)))
|
| 68 |
+
if family == 'long_trace':
|
| 69 |
+
return (((1,0),(0,3),(5,2)), ((2,0),(0,3),(5,2)),
|
| 70 |
+
((3,0),(0,3),(5,2)), ((4,1),(0,3),(5,2)),
|
| 71 |
+
((4,1),(4,1),(5,2)), ((5,2),(5,2),(5,2)))
|
| 72 |
+
if family == 'incompatible':
|
| 73 |
+
return (((1,0),(0,3),(4,0)), ((1,3),(2,1),(3,2)),
|
| 74 |
+
((2,1),(2,1),(3,2)), ((3,2),(3,2),(3,2)), ((4,3),(4,3),(4,3)))
|
| 75 |
+
if family == 'extra_state':
|
| 76 |
+
return (((1,0),(0,3),(6,2)), ((2,0),(0,3),(6,2)),
|
| 77 |
+
((3,0),(0,3),(6,2)), ((4,0),(0,3),(6,2)),
|
| 78 |
+
((5,1),(0,3),(6,2)), ((5,1),(5,1),(6,2)), ((6,2),(6,2),(6,2)))
|
| 79 |
+
if family == 'rotary':
|
| 80 |
+
return (((1,0),(4,2),(0,0)), ((2,3),(4,2),(0,0)),
|
| 81 |
+
((0,0),(3,1),(0,0)), ((3,1),(3,1),(3,1)), ((4,2),(4,2),(4,2)))
|
| 82 |
+
if family == 'interlock':
|
| 83 |
+
return (((1,0),(2,0),(5,2)), ((0,0),(3,3),(5,2)),
|
| 84 |
+
((3,3),(0,0),(5,2)), ((2,0),(1,0),(4,1)),
|
| 85 |
+
((4,1),(4,1),(4,1)), ((5,2),(5,2),(5,2)))
|
| 86 |
+
if family == 'robotics_airlock':
|
| 87 |
+
return (((1,0),(0,3),(0,3)), ((1,0),(2,0),(1,3)),
|
| 88 |
+
((0,3),(4,2),(3,1)), ((3,1),(3,1),(3,1)), ((4,2),(4,2),(4,2)))
|
| 89 |
+
if family == 'protocol_session':
|
| 90 |
+
return (((1,0),(0,3),(0,3)), ((1,3),(2,0),(0,3)),
|
| 91 |
+
((3,3),(0,0),(5,2)), ((5,2),(5,2),(4,1)),
|
| 92 |
+
((4,1),(4,1),(4,1)), ((5,2),(5,2),(5,2)))
|
| 93 |
+
raise ValueError('unknown mechanism')
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def _digest(value):
|
| 97 |
+
return hashlib.sha256(json.dumps(value, sort_keys=True, separators=(',', ':'), allow_nan=False).encode()).hexdigest()
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
_PROTOCOL_GENERATOR = 'crosswire-generated-protocol-v1'
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def _protocol_parameters(instance_seed):
|
| 104 |
+
"""Sample primitive control parameters without consuming the label RNG.
|
| 105 |
+
|
| 106 |
+
Every stage is reachable. Diagnostics never emit a terminal event, and a
|
| 107 |
+
goal path uses three or four actions. These are synthetic generator draws,
|
| 108 |
+
not evidence that any natural target population follows this distribution.
|
| 109 |
+
"""
|
| 110 |
+
rng = random.Random(instance_seed)
|
| 111 |
+
patterns = [list(pattern) for pattern in itertools.product((0, 3), repeat=3)
|
| 112 |
+
if len(set(pattern)) > 1]
|
| 113 |
+
diagnostics = [list(rng.choice(patterns)) for _ in range(2)]
|
| 114 |
+
progress = []
|
| 115 |
+
for index in range(rng.randint(1, 2)):
|
| 116 |
+
advance, fallback = rng.sample(range(3), 2)
|
| 117 |
+
progress.append({'advance': advance, 'fallback': fallback,
|
| 118 |
+
'fallback_state': rng.randrange(index + 3),
|
| 119 |
+
'fallback_output': rng.choice((0, 3))})
|
| 120 |
+
# The final progress step emits goal, so no unused neutral parameter
|
| 121 |
+
# is sampled for it. Intermediate advancement gets a neutral event.
|
| 122 |
+
if index:
|
| 123 |
+
progress[index - 1]['advance_output'] = rng.choice((0, 3))
|
| 124 |
+
return {'diagnostics': diagnostics, 'progress': progress}
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def _protocol_table(parameters):
|
| 128 |
+
"""Materialize a public reference from control parameters, never a target."""
|
| 129 |
+
stages = parameters['progress']
|
| 130 |
+
goal, hazard = len(stages) + 2, len(stages) + 3
|
| 131 |
+
rows = [tuple((index + 1, event) for event in outputs)
|
| 132 |
+
for index, outputs in enumerate(parameters['diagnostics'])]
|
| 133 |
+
for index, stage in enumerate(stages):
|
| 134 |
+
row = [(hazard, 2)] * 3
|
| 135 |
+
row[stage['advance']] = (index + 3, 1 if index == len(stages) - 1 else stage['advance_output'])
|
| 136 |
+
row[stage['fallback']] = (stage['fallback_state'], stage['fallback_output'])
|
| 137 |
+
rows.append(tuple(row))
|
| 138 |
+
rows.extend((tuple((goal, 1) for _ in range(3)), tuple((hazard, 2) for _ in range(3))))
|
| 139 |
+
return tuple(rows)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def _grounded_anchors(family, output_map):
|
| 143 |
+
"""Publish only raw-symbol meanings valid throughout this experiment.
|
| 144 |
+
|
| 145 |
+
A sensor's nominal symbol encoding is not necessarily its actual semantic
|
| 146 |
+
correspondence. Aliased and phase-varying terminal symbols have no static
|
| 147 |
+
truthful grounding. Delayed/repeated hazards remain sound only because the
|
| 148 |
+
hazard state is absorbing; their goal observations can precede a hazard.
|
| 149 |
+
"""
|
| 150 |
+
if family == 'malformed':
|
| 151 |
+
return {}
|
| 152 |
+
if family == 'neutral':
|
| 153 |
+
return {output_map[1]:1}
|
| 154 |
+
if family in ('semantic_corruption','bypass'):
|
| 155 |
+
return {output_map[1]:2, output_map[2]:1}
|
| 156 |
+
if family in ('alias','drift'):
|
| 157 |
+
return {output_map[0]:0, output_map[3]:3}
|
| 158 |
+
if family in ('delay','repeat'):
|
| 159 |
+
return {output_map[2]:2}
|
| 160 |
+
return {output_map[1]:1, output_map[2]:2}
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def make_case(family, seed=17, implementation='procedural', split='development', anchors=False, horizon=4,
|
| 164 |
+
*, instance_seed=None, fault_schedule=None):
|
| 165 |
+
if family not in _DESCRIPTIONS:
|
| 166 |
+
raise ValueError('unknown family')
|
| 167 |
+
if implementation not in ('table', 'procedural'):
|
| 168 |
+
raise ValueError('unknown implementation')
|
| 169 |
+
if split not in ('development', 'evaluation'):
|
| 170 |
+
raise ValueError('unknown split')
|
| 171 |
+
if type(seed) is not int or not 0 <= seed <= 2**32-1:
|
| 172 |
+
raise ValueError('seed must be unsigned 32-bit integer')
|
| 173 |
+
if instance_seed is not None and (type(instance_seed) is not int or not 0 <= instance_seed <= 2**32-1):
|
| 174 |
+
raise ValueError('instance_seed must be an unsigned 32-bit integer or None')
|
| 175 |
+
if family != 'generated_protocol' and instance_seed is not None:
|
| 176 |
+
raise ValueError('instance_seed is supported only by generated_protocol')
|
| 177 |
+
if type(anchors) is not bool or type(horizon) is not int or not 1 <= horizon <= 6:
|
| 178 |
+
raise ValueError('invalid anchors or horizon')
|
| 179 |
+
source_family = family if family in ('generated_protocol','routing','latch','handshake','symmetric','neutral','long_trace','rotary','interlock','robotics_airlock','protocol_session') else 'latch'
|
| 180 |
+
if family == 'extra_state':
|
| 181 |
+
source_family = 'long_trace'
|
| 182 |
+
mechanism = family if family in ('incompatible','extra_state') else source_family
|
| 183 |
+
rng = random.Random(seed)
|
| 184 |
+
input_map, output_map = list(range(3)), list(range(4))
|
| 185 |
+
rng.shuffle(input_map)
|
| 186 |
+
rng.shuffle(output_map)
|
| 187 |
+
contract = Contract(goal=1, hazard=2, horizon=horizon).to_dict()
|
| 188 |
+
private = {'family': family, 'source_family': source_family, 'mechanism': mechanism,
|
| 189 |
+
'implementation': implementation, 'split': split, 'seed': seed,
|
| 190 |
+
'input_map': input_map, 'output_map': output_map, 'contract': contract}
|
| 191 |
+
if family == 'malformed':
|
| 192 |
+
private.update(fault_mode='malformed', fault_after=0)
|
| 193 |
+
if fault_schedule is not None:
|
| 194 |
+
private['fault_schedule'] = normalize_fault_schedule(fault_schedule)
|
| 195 |
+
parent = _digest({'source_family':source_family, 'semantic_version':1})
|
| 196 |
+
design = None
|
| 197 |
+
if family == 'generated_protocol':
|
| 198 |
+
structural_seed = 0 if instance_seed is None else instance_seed
|
| 199 |
+
parameters = _protocol_parameters(structural_seed)
|
| 200 |
+
private.update(instance_seed=structural_seed, generator=_PROTOCOL_GENERATOR,
|
| 201 |
+
mechanism_parameters=parameters)
|
| 202 |
+
reference_table = _protocol_table(parameters)
|
| 203 |
+
dynamics_hash = _digest({'initial': 0, 'inputs': 3, 'outputs': 4, 'transitions': reference_table})
|
| 204 |
+
parent = _digest({'generator': _PROTOCOL_GENERATOR, 'dynamics_hash': dynamics_hash})
|
| 205 |
+
design = {'generator': _PROTOCOL_GENERATOR, 'dynamics_hash': dynamics_hash,
|
| 206 |
+
'sampling_unit': 'generated_semantic_dynamics'}
|
| 207 |
+
else:
|
| 208 |
+
reference_table = _table(source_family)
|
| 209 |
+
public = {'reference': Machine('reference', reference_table).to_dict(),
|
| 210 |
+
'contract': contract, 'target_id': _digest(private),
|
| 211 |
+
'anchors': _grounded_anchors(family,output_map) if anchors else {},
|
| 212 |
+
'parent_instance_id': parent}
|
| 213 |
+
if design is not None:
|
| 214 |
+
public['instance_design'] = design
|
| 215 |
+
return Case(public, private)
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
class _Engine:
|
| 219 |
+
def __init__(self, config, work=None):
|
| 220 |
+
self.config = config
|
| 221 |
+
self.work = work
|
| 222 |
+
self.total_steps = 0
|
| 223 |
+
self.steps_since_reset = 0
|
| 224 |
+
self.phase = 'calibration'
|
| 225 |
+
self.reset()
|
| 226 |
+
|
| 227 |
+
def set_phase(self, phase):
|
| 228 |
+
self.phase = phase
|
| 229 |
+
|
| 230 |
+
def reset(self):
|
| 231 |
+
if self.work is not None:
|
| 232 |
+
self.work['resets'] = self.work.get('resets',0) + 1
|
| 233 |
+
self.state, self.previous, self.parity = 0, 0, 0
|
| 234 |
+
self.fault_previous = 0
|
| 235 |
+
self.semantic_events = []
|
| 236 |
+
self.steps_since_reset = 0
|
| 237 |
+
|
| 238 |
+
def _transition(self, action):
|
| 239 |
+
raise NotImplementedError
|
| 240 |
+
|
| 241 |
+
def step(self, input):
|
| 242 |
+
if self.work is not None:
|
| 243 |
+
self.work['steps'] = self.work.get('steps',0) + 1
|
| 244 |
+
if type(input) is not int or not 0 <= input < 3:
|
| 245 |
+
raise ValueError('input outside alphabet')
|
| 246 |
+
|
| 247 |
+
fault = self.config.get('fault_schedule')
|
| 248 |
+
phase_applies = True
|
| 249 |
+
if fault is not None:
|
| 250 |
+
req_phase = fault.get('phase', 'deployment')
|
| 251 |
+
if req_phase != 'always' and self.phase != req_phase:
|
| 252 |
+
phase_applies = False
|
| 253 |
+
fault_active = (fault is not None and phase_applies and self.steps_since_reset >= fault.get('step', 0))
|
| 254 |
+
fault_type = fault.get('type') if fault_active else None
|
| 255 |
+
|
| 256 |
+
action = self.config['input_map'][input]
|
| 257 |
+
if fault_type == 'actuator_swap':
|
| 258 |
+
action = (action + 1) % 3
|
| 259 |
+
|
| 260 |
+
self.state, event = self._transition(action)
|
| 261 |
+
self.total_steps += 1
|
| 262 |
+
self.steps_since_reset += 1
|
| 263 |
+
|
| 264 |
+
family = self.config['family']
|
| 265 |
+
# Ground truth is produced from the actual mechanism before its sensor.
|
| 266 |
+
actual = event
|
| 267 |
+
if family in ('semantic_corruption','bypass') or (family == 'drift' and self.total_steps > 8):
|
| 268 |
+
actual = {1:2,2:1}.get(event,event)
|
| 269 |
+
if fault_type == 'semantic_swap':
|
| 270 |
+
actual = {1:2,2:1}.get(actual,actual)
|
| 271 |
+
self.semantic_events.append(actual)
|
| 272 |
+
|
| 273 |
+
observed = event
|
| 274 |
+
if family == 'alias' and event == 2:
|
| 275 |
+
observed = 1
|
| 276 |
+
elif family == 'delay':
|
| 277 |
+
observed, self.previous = self.previous, event
|
| 278 |
+
elif family == 'repeat':
|
| 279 |
+
if self.parity:
|
| 280 |
+
observed = self.previous
|
| 281 |
+
self.previous = observed
|
| 282 |
+
self.parity ^= 1
|
| 283 |
+
# Compose scheduled faults after the family's sensor mechanism. A second
|
| 284 |
+
# delay needs its own buffer; sharing `previous` erases one of the delays.
|
| 285 |
+
if fault_type == 'sensor_drift':
|
| 286 |
+
observed = (observed + 1) % 4
|
| 287 |
+
elif fault_type == 'delay_spike':
|
| 288 |
+
observed, self.fault_previous = self.fault_previous, observed
|
| 289 |
+
elif fault_type == 'stuck_output':
|
| 290 |
+
observed = fault.get('value', 0)
|
| 291 |
+
|
| 292 |
+
return self.config['output_map'][observed]
|
| 293 |
+
|
| 294 |
+
def close(self):
|
| 295 |
+
pass
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
class _TableEngine(_Engine):
|
| 299 |
+
def __init__(self, config, work=None):
|
| 300 |
+
self.transitions = (_protocol_table(config['mechanism_parameters'])
|
| 301 |
+
if config['mechanism'] == 'generated_protocol' else _table(config['mechanism']))
|
| 302 |
+
super().__init__(config, work)
|
| 303 |
+
|
| 304 |
+
def _transition(self, action):
|
| 305 |
+
return self.transitions[self.state][action]
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
class _ProceduralEngine(_Engine):
|
| 309 |
+
def _transition(self, action):
|
| 310 |
+
family, state = self.config['mechanism'], self.state
|
| 311 |
+
if family == 'generated_protocol':
|
| 312 |
+
parameters = self.config['mechanism_parameters']
|
| 313 |
+
stages = parameters['progress']
|
| 314 |
+
goal, hazard = len(stages) + 2, len(stages) + 3
|
| 315 |
+
if state == goal:
|
| 316 |
+
return goal, 1
|
| 317 |
+
if state == hazard:
|
| 318 |
+
return hazard, 2
|
| 319 |
+
if state < 2:
|
| 320 |
+
return state + 1, parameters['diagnostics'][state][action]
|
| 321 |
+
index = state - 2
|
| 322 |
+
stage = stages[index]
|
| 323 |
+
if action == stage['advance']:
|
| 324 |
+
return state + 1, 1 if index == len(stages) - 1 else stage['advance_output']
|
| 325 |
+
if action == stage['fallback']:
|
| 326 |
+
return stage['fallback_state'], stage['fallback_output']
|
| 327 |
+
return hazard, 2
|
| 328 |
+
if family == 'routing':
|
| 329 |
+
if state < 3:
|
| 330 |
+
return state + 1, 0 if action == 0 else 3
|
| 331 |
+
if state == 3:
|
| 332 |
+
return (4,1) if action == 0 else (5,2)
|
| 333 |
+
return state, 1 if state == 4 else 2
|
| 334 |
+
if family == 'rotary':
|
| 335 |
+
if state >= 3:
|
| 336 |
+
return state, 1 if state == 3 else 2
|
| 337 |
+
if action == 0:
|
| 338 |
+
dial = (state + 1) % 3
|
| 339 |
+
return dial, 3 if dial == 2 else 0
|
| 340 |
+
if action == 1:
|
| 341 |
+
return (3,1) if state == 2 else (4,2)
|
| 342 |
+
return 0,0
|
| 343 |
+
if family == 'interlock':
|
| 344 |
+
if state >= 4:
|
| 345 |
+
return state, 1 if state == 4 else 2
|
| 346 |
+
if action == 2:
|
| 347 |
+
return (4,1) if state == 3 else (5,2)
|
| 348 |
+
flags = state ^ (1 << action)
|
| 349 |
+
return flags, 3 if flags == 3 else 0
|
| 350 |
+
if family == 'robotics_airlock':
|
| 351 |
+
if state >= 3:
|
| 352 |
+
return state, 1 if state == 3 else 2
|
| 353 |
+
if state == 0:
|
| 354 |
+
return (1, 0) if action == 0 else (0, 3)
|
| 355 |
+
if state == 1:
|
| 356 |
+
return (1, 0) if action == 0 else (2, 0) if action == 1 else (1, 3)
|
| 357 |
+
return (0, 3) if action == 0 else (4, 2) if action == 1 else (3, 1)
|
| 358 |
+
if family == 'protocol_session':
|
| 359 |
+
if state >= 4:
|
| 360 |
+
return state, 1 if state == 4 else 2
|
| 361 |
+
if state == 0:
|
| 362 |
+
return (1, 0) if action == 0 else (0, 3)
|
| 363 |
+
if state == 1:
|
| 364 |
+
return (1, 3) if action == 0 else (2, 0) if action == 1 else (0, 3)
|
| 365 |
+
if state == 2:
|
| 366 |
+
return (3, 3) if action == 0 else (0, 0) if action == 1 else (5, 2)
|
| 367 |
+
return (4, 1) if action == 2 else (5, 2)
|
| 368 |
+
if family == 'neutral':
|
| 369 |
+
if state or action == 0:
|
| 370 |
+
return 1,1
|
| 371 |
+
return 0, 0 if action == 1 else 3
|
| 372 |
+
if family == 'symmetric':
|
| 373 |
+
if state:
|
| 374 |
+
return state, state
|
| 375 |
+
return (1,1) if action == 0 else (2,2) if action == 1 else (0,0)
|
| 376 |
+
if family in ('long_trace','extra_state'):
|
| 377 |
+
goal_state = 5 if family == 'extra_state' else 4
|
| 378 |
+
hazard_state = goal_state + 1
|
| 379 |
+
if state == hazard_state or action == 2:
|
| 380 |
+
return hazard_state,2
|
| 381 |
+
if state == goal_state:
|
| 382 |
+
return goal_state,1
|
| 383 |
+
if action == 1:
|
| 384 |
+
return 0,3
|
| 385 |
+
progress = state + 1
|
| 386 |
+
return progress, 1 if progress == goal_state else 0
|
| 387 |
+
if family == 'handshake':
|
| 388 |
+
if state == 4 or action == 2:
|
| 389 |
+
return 4,2
|
| 390 |
+
if state == 3:
|
| 391 |
+
return 3,1
|
| 392 |
+
if state == 2:
|
| 393 |
+
return (3,1) if action == 0 else (0,0)
|
| 394 |
+
if state == 1:
|
| 395 |
+
return (1,0) if action == 0 else (2,3)
|
| 396 |
+
return (1,0) if action == 0 else (0,3)
|
| 397 |
+
if family == 'incompatible':
|
| 398 |
+
if state == 4:
|
| 399 |
+
return 4,3
|
| 400 |
+
if state == 0 and action == 2:
|
| 401 |
+
return 4,0
|
| 402 |
+
if state == 3 or action == 2:
|
| 403 |
+
return 3,2
|
| 404 |
+
if state == 2:
|
| 405 |
+
return 2,1
|
| 406 |
+
if state == 1:
|
| 407 |
+
return (1,3) if action == 0 else (2,1)
|
| 408 |
+
return (1,0) if action == 0 else (0,3)
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
def make_engine(private, work=None):
|
| 412 |
+
return (_TableEngine if private['implementation'] == 'table' else _ProceduralEngine)(private, work)
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
def evaluate_word(private, word, work=None):
|
| 416 |
+
return evaluate_deployment(private, [], word, work=work)
|
| 417 |
+
|
| 418 |
+
|
| 419 |
+
def _replay_calibration(engine, probes):
|
| 420 |
+
"""Replay only observed successful step calls, including incomplete prefixes.
|
| 421 |
+
|
| 422 |
+
Failed interface calls emit no engine event in these fixtures. Reset clears
|
| 423 |
+
transient state but intentionally preserves experiment-level drift count.
|
| 424 |
+
"""
|
| 425 |
+
for index, probe in enumerate(probes):
|
| 426 |
+
if index:
|
| 427 |
+
engine.reset()
|
| 428 |
+
for symbol in probe['word'][:len(probe['outputs'])]:
|
| 429 |
+
engine.step(symbol)
|
| 430 |
+
if probes:
|
| 431 |
+
engine.reset()
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
def evaluate_deployment(private, probes, word, work=None):
|
| 435 |
+
"""Privately audit deployment in the actual post-calibration experiment phase."""
|
| 436 |
+
return _evaluate_phase(private, probes, word, 'deployment', work)
|
| 437 |
+
|
| 438 |
+
|
| 439 |
+
def evaluate_calibration(private, probes, word, work=None):
|
| 440 |
+
"""Audit a calibration word without activating deployment-only faults."""
|
| 441 |
+
return _evaluate_phase(private, probes, word, 'calibration', work)
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
def _evaluate_phase(private, probes, word, phase, work=None):
|
| 445 |
+
engine = make_engine(private, work)
|
| 446 |
+
try:
|
| 447 |
+
_replay_calibration(engine, probes)
|
| 448 |
+
if hasattr(engine, 'set_phase'):
|
| 449 |
+
engine.set_phase(phase)
|
| 450 |
+
outputs = []
|
| 451 |
+
if isinstance(word, dict):
|
| 452 |
+
node = word
|
| 453 |
+
while isinstance(node, dict) and 'action' in node:
|
| 454 |
+
raw = engine.step(node['action'])
|
| 455 |
+
outputs.append(raw)
|
| 456 |
+
branches = node.get('branches', {})
|
| 457 |
+
if str(raw) not in branches:
|
| 458 |
+
break
|
| 459 |
+
node = branches[str(raw)]
|
| 460 |
+
elif word is not None:
|
| 461 |
+
outputs = [engine.step(symbol) for symbol in word]
|
| 462 |
+
events = list(engine.semantic_events)
|
| 463 |
+
goal, hazard = private['contract']['goal'], private['contract']['hazard']
|
| 464 |
+
result = {'outputs': outputs, 'semantic_events': events,
|
| 465 |
+
'goal_success': bool(events) and events[-1] == goal,
|
| 466 |
+
'hazard': hazard in events,
|
| 467 |
+
'contract_satisfied': bool(events) and events[-1] == goal and hazard not in events}
|
| 468 |
+
if private['family'] == 'bypass':
|
| 469 |
+
result['privileged_check_passed'] = True
|
| 470 |
+
return result
|
| 471 |
+
finally:
|
| 472 |
+
engine.close()
|
| 473 |
+
|
| 474 |
+
|
| 475 |
+
def target_machine(private, work=None):
|
| 476 |
+
if private['family'] in ('drift','malformed'):
|
| 477 |
+
return None
|
| 478 |
+
# Export deployment behavior after reset. Fault onset is finite-state memory,
|
| 479 |
+
# saturated once active; never omit it or silently export calibration behavior.
|
| 480 |
+
fault = private.get('fault_schedule')
|
| 481 |
+
onset = fault['step'] if fault and fault.get('phase', 'deployment') != 'calibration' else 0
|
| 482 |
+
if onset > 512:
|
| 483 |
+
# Bound the private export rather than exhausting memory for a distant
|
| 484 |
+
# schedule. Actual trajectory auditing still executes the real schedule.
|
| 485 |
+
return None
|
| 486 |
+
engine = make_engine(private, work)
|
| 487 |
+
engine.set_phase('deployment')
|
| 488 |
+
initial = (0,0,0,0,0)
|
| 489 |
+
states, pending, rows = {initial:0}, deque([initial]), []
|
| 490 |
+
try:
|
| 491 |
+
while pending:
|
| 492 |
+
state = pending.popleft()
|
| 493 |
+
row = []
|
| 494 |
+
for action in range(3):
|
| 495 |
+
engine.state, engine.previous, engine.parity, engine.steps_since_reset, engine.fault_previous = state
|
| 496 |
+
engine.semantic_events.clear()
|
| 497 |
+
output = engine.step(action)
|
| 498 |
+
following = (engine.state,engine.previous,engine.parity,min(engine.steps_since_reset, onset),engine.fault_previous)
|
| 499 |
+
if following not in states:
|
| 500 |
+
states[following] = len(states)
|
| 501 |
+
pending.append(following)
|
| 502 |
+
row.append((states[following], output))
|
| 503 |
+
rows.append(tuple(row))
|
| 504 |
+
return Machine('private-raw-target', tuple(rows))
|
| 505 |
+
finally:
|
| 506 |
+
engine.close()
|
| 507 |
+
|
| 508 |
+
|
| 509 |
+
def oracle_plan(private, horizon=4):
|
| 510 |
+
return oracle_plan_after(private, [], horizon)
|
| 511 |
+
|
| 512 |
+
|
| 513 |
+
def oracle_plan_after(private, probes, horizon=4):
|
| 514 |
+
"""Enumerate fresh counterfactual plans from the same post-calibration phase."""
|
| 515 |
+
if private['family'] == 'malformed':
|
| 516 |
+
return None
|
| 517 |
+
for length in range(1,horizon+1):
|
| 518 |
+
for word in itertools.product(range(3),repeat=length):
|
| 519 |
+
if evaluate_deployment(private,probes,word)['contract_satisfied']:
|
| 520 |
+
return list(word)
|
| 521 |
+
return None
|
crosswire/identity.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Stable artifact identity; hashes identify content, not scientific truth."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import hashlib
|
| 5 |
+
import json
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def canonical_json(value: object) -> str:
|
| 10 |
+
return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=True, allow_nan=False)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def canonical_hash(value: object) -> str:
|
| 14 |
+
return hashlib.sha256(canonical_json(value).encode()).hexdigest()
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def source_hash() -> str:
|
| 18 |
+
root = Path(__file__).parent
|
| 19 |
+
content = {p.name: p.read_text(encoding="utf-8").replace("\r\n", "\n") for p in sorted(root.glob("*.py"))}
|
| 20 |
+
return canonical_hash(content)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def scientific_view(result: dict) -> dict:
|
| 24 |
+
"""Remove measured runtime and self-referential IDs, retain scientific work."""
|
| 25 |
+
value = json.loads(canonical_json(result))
|
| 26 |
+
if value.get("schema") == "crosswire-study-v1":
|
| 27 |
+
value["rows"] = [scientific_view(row) for row in value["rows"]]
|
| 28 |
+
else:
|
| 29 |
+
for field in ("parent_cpu_seconds", "wall_seconds", "parent_peak_traced_bytes"):
|
| 30 |
+
value.get("metrics", {}).pop(field, None)
|
| 31 |
+
value.pop("scientific_hash", None)
|
| 32 |
+
return value
|
crosswire/interfaces.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Bounded, counted JSON-lines access to one explicitly owned subprocess."""
|
| 2 |
+
import json
|
| 3 |
+
import math
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import queue
|
| 6 |
+
import subprocess
|
| 7 |
+
import sys
|
| 8 |
+
import threading
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class InterfaceError(RuntimeError):
|
| 13 |
+
def __init__(self, code, message=None):
|
| 14 |
+
self.code = code
|
| 15 |
+
super().__init__(message or code.replace('_', ' '))
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class TargetProcess:
|
| 19 |
+
def __init__(self, private, timeout=2.0, *, startup_timeout=10.0):
|
| 20 |
+
if isinstance(timeout,bool) or not isinstance(timeout,(int,float)) or not math.isfinite(timeout) or timeout <= 0 or timeout > 30:
|
| 21 |
+
raise ValueError('timeout must be finite and between zero and 30 seconds')
|
| 22 |
+
if (isinstance(startup_timeout, bool) or not isinstance(startup_timeout, (int, float))
|
| 23 |
+
or not math.isfinite(startup_timeout) or not 0 < startup_timeout <= 30):
|
| 24 |
+
raise ValueError('startup_timeout must be finite and between zero and 30 seconds')
|
| 25 |
+
self.timeout = timeout
|
| 26 |
+
self.startup_timeout = startup_timeout
|
| 27 |
+
self.counts = dict(reset_attempts=0,step_attempts=0,reset_successes=0,step_successes=0,target_calls=0)
|
| 28 |
+
self.last_trace = {'word':[],'outputs':[],'complete':False}
|
| 29 |
+
self._sequence, self._closed = 0, False
|
| 30 |
+
self._responses = queue.Queue()
|
| 31 |
+
self._process = subprocess.Popen([sys.executable, '-m', 'crosswire.target_worker'],
|
| 32 |
+
cwd=str(Path(__file__).resolve().parent.parent), stdin=subprocess.PIPE,
|
| 33 |
+
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, bufsize=0,
|
| 34 |
+
creationflags=getattr(subprocess,'CREATE_NO_WINDOW',0))
|
| 35 |
+
self.pid = self._process.pid
|
| 36 |
+
self._reader = threading.Thread(target=self._read, daemon=True)
|
| 37 |
+
self._reader.start()
|
| 38 |
+
try:
|
| 39 |
+
# Interpreter/module startup is not a reset or action attempt. Keep
|
| 40 |
+
# its bounded handshake separate from the strict per-call deadline.
|
| 41 |
+
self._request('init', request_timeout=self.startup_timeout, config=private)
|
| 42 |
+
except BaseException:
|
| 43 |
+
self.close()
|
| 44 |
+
raise
|
| 45 |
+
|
| 46 |
+
def _read(self):
|
| 47 |
+
try:
|
| 48 |
+
while True:
|
| 49 |
+
line = self._process.stdout.readline(65537)
|
| 50 |
+
if not line:
|
| 51 |
+
self._responses.put(('error','worker_exit'))
|
| 52 |
+
return
|
| 53 |
+
if len(line) > 65536 or not line.endswith(b'\n'):
|
| 54 |
+
self._responses.put(('error','malformed_response'))
|
| 55 |
+
return
|
| 56 |
+
self._responses.put(('line',line))
|
| 57 |
+
except (OSError, ValueError):
|
| 58 |
+
self._responses.put(('error','worker_exit'))
|
| 59 |
+
|
| 60 |
+
def _request(self, operation, *, request_timeout=None, **payload):
|
| 61 |
+
try:
|
| 62 |
+
if self._closed:
|
| 63 |
+
raise InterfaceError('closed')
|
| 64 |
+
self._sequence += 1
|
| 65 |
+
message = {'id':self._sequence,'op':operation,**payload}
|
| 66 |
+
encoded = (json.dumps(message,separators=(',',':'),allow_nan=False)+'\n').encode('utf-8')
|
| 67 |
+
if len(encoded)>16384:
|
| 68 |
+
raise InterfaceError('invalid_request')
|
| 69 |
+
limit = self.timeout if request_timeout is None else request_timeout
|
| 70 |
+
deadline = time.monotonic() + limit
|
| 71 |
+
written = queue.Queue(maxsize=1)
|
| 72 |
+
def write():
|
| 73 |
+
try:
|
| 74 |
+
self._process.stdin.write(encoded)
|
| 75 |
+
self._process.stdin.flush()
|
| 76 |
+
written.put(None)
|
| 77 |
+
except (OSError,ValueError) as error:
|
| 78 |
+
written.put(error)
|
| 79 |
+
threading.Thread(target=write,daemon=True).start()
|
| 80 |
+
try:
|
| 81 |
+
if written.get(timeout=limit) is not None:
|
| 82 |
+
raise InterfaceError('worker_exit')
|
| 83 |
+
kind, raw = self._responses.get(timeout=max(0,deadline-time.monotonic()))
|
| 84 |
+
except queue.Empty:
|
| 85 |
+
raise InterfaceError('timeout') from None
|
| 86 |
+
if kind == 'error':
|
| 87 |
+
raise InterfaceError(raw)
|
| 88 |
+
try:
|
| 89 |
+
response = json.loads(raw,parse_constant=lambda value: (_ for _ in ()).throw(ValueError(value)))
|
| 90 |
+
except (ValueError,UnicodeError):
|
| 91 |
+
raise InterfaceError('malformed_response') from None
|
| 92 |
+
if not isinstance(response,dict) or type(response.get('id')) is not int or response['id'] != self._sequence:
|
| 93 |
+
raise InterfaceError('malformed_response')
|
| 94 |
+
if response.get('ok') is not True:
|
| 95 |
+
raise InterfaceError('target_error')
|
| 96 |
+
expected = {'id','ok','output'} if operation == 'step' else {'id','ok'}
|
| 97 |
+
if set(response) != expected:
|
| 98 |
+
raise InterfaceError('malformed_response')
|
| 99 |
+
if operation == 'step':
|
| 100 |
+
output = response['output']
|
| 101 |
+
if type(output) is not int or not 0 <= output < 4:
|
| 102 |
+
raise InterfaceError('malformed_response')
|
| 103 |
+
return output
|
| 104 |
+
return None
|
| 105 |
+
except InterfaceError:
|
| 106 |
+
self.close()
|
| 107 |
+
raise
|
| 108 |
+
except (OSError,ValueError,TypeError):
|
| 109 |
+
self.close()
|
| 110 |
+
raise InterfaceError('worker_exit') from None
|
| 111 |
+
|
| 112 |
+
def reset(self):
|
| 113 |
+
self.counts['reset_attempts'] += 1
|
| 114 |
+
self.counts['target_calls'] += 1
|
| 115 |
+
self._request('reset')
|
| 116 |
+
self.counts['reset_successes'] += 1
|
| 117 |
+
|
| 118 |
+
def step(self, input):
|
| 119 |
+
self.counts['step_attempts'] += 1
|
| 120 |
+
self.counts['target_calls'] += 1
|
| 121 |
+
if type(input) is not int or not 0 <= input < 3:
|
| 122 |
+
self.close()
|
| 123 |
+
raise InterfaceError('invalid_input')
|
| 124 |
+
output = self._request('step',input=input)
|
| 125 |
+
self.counts['step_successes'] += 1
|
| 126 |
+
return output
|
| 127 |
+
|
| 128 |
+
def query(self, word):
|
| 129 |
+
self.last_trace = {'word':list(word),'outputs':[],'complete':False}
|
| 130 |
+
try:
|
| 131 |
+
self.reset()
|
| 132 |
+
for symbol in self.last_trace['word']:
|
| 133 |
+
self.last_trace['outputs'].append(self.step(symbol))
|
| 134 |
+
except InterfaceError as error:
|
| 135 |
+
error.trace = {'word':list(self.last_trace['word']),
|
| 136 |
+
'outputs':list(self.last_trace['outputs']),'complete':False}
|
| 137 |
+
raise
|
| 138 |
+
self.last_trace['complete'] = True
|
| 139 |
+
return list(self.last_trace['outputs'])
|
| 140 |
+
|
| 141 |
+
def set_phase(self, phase):
|
| 142 |
+
self._request('set_phase', phase=phase)
|
| 143 |
+
|
| 144 |
+
def close(self):
|
| 145 |
+
if self._closed:
|
| 146 |
+
return
|
| 147 |
+
self._closed = True
|
| 148 |
+
process = self._process
|
| 149 |
+
if process.poll() is None:
|
| 150 |
+
# Kill only this Popen object, never a name, process group or tree.
|
| 151 |
+
process.terminate()
|
| 152 |
+
try:
|
| 153 |
+
process.wait(timeout=0.5)
|
| 154 |
+
except subprocess.TimeoutExpired:
|
| 155 |
+
process.kill()
|
| 156 |
+
process.wait(timeout=0.5)
|
| 157 |
+
for pipe in (process.stdin,process.stdout):
|
| 158 |
+
try:
|
| 159 |
+
pipe.close()
|
| 160 |
+
except OSError:
|
| 161 |
+
pass
|
| 162 |
+
if threading.current_thread() is not self._reader:
|
| 163 |
+
self._reader.join(timeout=0.5)
|
| 164 |
+
|
| 165 |
+
def __enter__(self):
|
| 166 |
+
return self
|
| 167 |
+
|
| 168 |
+
def __exit__(self, *args):
|
| 169 |
+
self.close()
|
| 170 |
+
|
crosswire/receipts.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Validated, source-bound run/study receipts and actual scientific replay."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
from .identity import canonical_hash, scientific_view, source_hash
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def pack(result: dict) -> dict:
|
| 10 |
+
if result.get("schema") not in ("crosswire-result-v1", "crosswire-study-v1"):
|
| 11 |
+
raise ValueError("Only completed run/study results can be exported")
|
| 12 |
+
if result.get('source_hash') != source_hash():
|
| 13 |
+
raise ValueError('Result source revision differs from the current implementation')
|
| 14 |
+
return {"schema": "crosswire-receipt-v1", "source_hash": result['source_hash'],
|
| 15 |
+
"payload": result, "payload_hash": canonical_hash(result)}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def check_integrity(receipt: dict) -> dict:
|
| 19 |
+
"""Check saved envelope/digests without requiring the current source revision.
|
| 20 |
+
|
| 21 |
+
This authenticates neither target execution nor private audits. It does not
|
| 22 |
+
replay the result or independently check the public-model certificate.
|
| 23 |
+
"""
|
| 24 |
+
if not isinstance(receipt, dict) or set(receipt) != {"schema", "source_hash", "payload", "payload_hash"}:
|
| 25 |
+
raise ValueError("Incomplete receipt envelope")
|
| 26 |
+
if receipt["schema"] != "crosswire-receipt-v1":
|
| 27 |
+
raise ValueError("Unsupported receipt envelope")
|
| 28 |
+
for field in ('source_hash', 'payload_hash'):
|
| 29 |
+
value = receipt[field]
|
| 30 |
+
if not isinstance(value, str) or len(value) != 64 or any(c not in '0123456789abcdef' for c in value):
|
| 31 |
+
raise ValueError(f'Invalid receipt {field}')
|
| 32 |
+
try:
|
| 33 |
+
payload = receipt["payload"]
|
| 34 |
+
if not isinstance(payload, dict):
|
| 35 |
+
raise ValueError("Unsupported payload")
|
| 36 |
+
if canonical_hash(payload) != receipt["payload_hash"]:
|
| 37 |
+
raise ValueError("Receipt content digest mismatch")
|
| 38 |
+
if payload["schema"] not in ("crosswire-result-v1", "crosswire-study-v1"):
|
| 39 |
+
raise ValueError("Unsupported payload")
|
| 40 |
+
if payload.get('source_hash') != receipt['source_hash']:
|
| 41 |
+
raise ValueError('Payload source identity differs from its envelope')
|
| 42 |
+
if canonical_hash(scientific_view(payload)) != payload["scientific_hash"]:
|
| 43 |
+
raise ValueError("Scientific content digest mismatch")
|
| 44 |
+
except (TypeError, KeyError, OverflowError) as exc:
|
| 45 |
+
raise ValueError("Incomplete or invalid scientific payload") from exc
|
| 46 |
+
return payload
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def _envelope(receipt: dict) -> dict:
|
| 50 |
+
payload = check_integrity(receipt)
|
| 51 |
+
if receipt['source_hash'] != source_hash():
|
| 52 |
+
raise ValueError("Unsupported receipt or source revision; preserve the original code to replay it")
|
| 53 |
+
return payload
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def check_envelope(receipt: dict) -> dict:
|
| 57 |
+
"""Check envelope/digests and current source, without recomputing science."""
|
| 58 |
+
return _envelope(receipt)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def validate(receipt: dict, cancel=None, progress=None) -> dict:
|
| 62 |
+
"""Import requires actual replay, not only internally consistent hashes."""
|
| 63 |
+
payload = _envelope(receipt)
|
| 64 |
+
checked = replay(receipt, cancel, progress)
|
| 65 |
+
if not checked["valid"]:
|
| 66 |
+
raise ValueError(checked["message"])
|
| 67 |
+
return payload
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def replay(receipt: dict, cancel=None, progress=None) -> dict:
|
| 71 |
+
from .study import Cancelled, RunConfig, run, run_study
|
| 72 |
+
payload = _envelope(receipt)
|
| 73 |
+
try:
|
| 74 |
+
if payload["schema"] == "crosswire-result-v1":
|
| 75 |
+
fresh = run(RunConfig.from_dict(payload["configuration"]), cancel, progress)
|
| 76 |
+
else:
|
| 77 |
+
protocol = {k: v for k, v in payload["protocol"].items()
|
| 78 |
+
if k not in ("exploratory", "analysis_unit", "pre_execution_source_hash")}
|
| 79 |
+
fresh = run_study(protocol, cancel, progress)
|
| 80 |
+
except Cancelled:
|
| 81 |
+
raise
|
| 82 |
+
except (KeyError, TypeError, RuntimeError) as exc:
|
| 83 |
+
raise ValueError("Receipt cannot reproduce its scientific result") from exc
|
| 84 |
+
valid = scientific_view(fresh) == scientific_view(payload)
|
| 85 |
+
return {"valid": valid, "scientific_hash": fresh["scientific_hash"],
|
| 86 |
+
"message": "Scientific decisions, probes, costs and audits replay exactly" if valid else
|
| 87 |
+
"Recomputed decisions, audit or scientific accounting differ from receipt"}
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def load(path) -> dict:
|
| 91 |
+
with open(path, encoding="utf-8") as stream:
|
| 92 |
+
value = json.load(stream, parse_constant=lambda text: (_ for _ in ()).throw(ValueError(f"Invalid JSON number {text}")))
|
| 93 |
+
return validate(value)
|
crosswire/reliability.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Target-cluster reliability summaries with explicit sampling assumptions.
|
| 2 |
+
|
| 3 |
+
These intervals concern the declared synthetic generator and fixed nuisance
|
| 4 |
+
design. A seed, hash, or metadata field cannot establish independent sampling.
|
| 5 |
+
Exact binomial intervals follow the NIST Engineering Statistics Handbook,
|
| 6 |
+
section 7.2.4.1; bounded target averages use Hoeffding's inequality (1963).
|
| 7 |
+
"""
|
| 8 |
+
from collections import defaultdict
|
| 9 |
+
import math
|
| 10 |
+
from .conditions import condition_key, condition_fields
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _binomial_cdf(events, trials, probability):
|
| 14 |
+
if probability <= 0:
|
| 15 |
+
return 1.0
|
| 16 |
+
if probability >= 1:
|
| 17 |
+
return float(events >= trials)
|
| 18 |
+
log_p, log_q = math.log(probability), math.log1p(-probability)
|
| 19 |
+
# Log probabilities avoid overflow in the integer binomial coefficients
|
| 20 |
+
# and underflow of the starting term for the bounded study sizes.
|
| 21 |
+
return min(1.0, math.fsum(math.exp(math.lgamma(trials + 1) - math.lgamma(index + 1)
|
| 22 |
+
- math.lgamma(trials - index + 1)
|
| 23 |
+
+ index * log_p + (trials - index) * log_q)
|
| 24 |
+
for index in range(events + 1)))
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _binomial_quantile(events, trials, tail):
|
| 28 |
+
low, high = 0.0, 1.0
|
| 29 |
+
for _ in range(70):
|
| 30 |
+
middle = (low + high) / 2
|
| 31 |
+
if _binomial_cdf(events, trials, middle) > tail:
|
| 32 |
+
low = middle
|
| 33 |
+
else:
|
| 34 |
+
high = middle
|
| 35 |
+
return (low + high) / 2
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def _exact_interval(events, trials, confidence):
|
| 39 |
+
alpha = 1 - confidence
|
| 40 |
+
lower = 0.0 if events == 0 else _binomial_quantile(events - 1, trials, 1 - alpha / 2)
|
| 41 |
+
upper = 1.0 if events == trials else _binomial_quantile(events, trials, alpha / 2)
|
| 42 |
+
return [lower, upper]
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def _outcome(row):
|
| 46 |
+
audit, admitted = row['audit'], bool(row['decision']['admitted'])
|
| 47 |
+
hazard = audit.get('hazard') if admitted else False
|
| 48 |
+
return (admitted, audit.get('contract_satisfied') is True,
|
| 49 |
+
admitted and audit.get('contract_satisfied') is not True,
|
| 50 |
+
hazard, audit.get('calibration_hazardous_probes', 0) > 0)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def _event_metric(events, targets, unknown, eligible, confidence):
|
| 54 |
+
interval = None
|
| 55 |
+
if eligible:
|
| 56 |
+
# Missing deployment outcomes cannot count as negative observations.
|
| 57 |
+
interval = [_exact_interval(events, targets, confidence)[0],
|
| 58 |
+
_exact_interval(events + unknown, targets, confidence)[1]]
|
| 59 |
+
return {'events': events, 'targets': targets, 'unknown_targets': unknown,
|
| 60 |
+
'rate': events / targets, 'possible_rate': (events + unknown) / targets, 'interval': interval}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def reliability_summary(rows, confidence=0.95):
|
| 64 |
+
"""Equal-weight semantic-parent summaries, conditional on explicit draws.
|
| 65 |
+
|
| 66 |
+
Duplicate nuisance cells are collapsed only when their audit outcomes
|
| 67 |
+
agree. Structural-seed collisions or unequal label/implementation designs
|
| 68 |
+
withhold sampling intervals. Planned seed selection is not observational
|
| 69 |
+
evidence of a real-world target distribution or independent draws.
|
| 70 |
+
"""
|
| 71 |
+
if (type(confidence) not in (int, float) or not math.isfinite(confidence)
|
| 72 |
+
or not 0 < confidence < 1):
|
| 73 |
+
raise ValueError('confidence must be finite and strictly between zero and one')
|
| 74 |
+
groups = defaultdict(list)
|
| 75 |
+
for row in rows:
|
| 76 |
+
cfg = row['configuration']
|
| 77 |
+
conditions = condition_key(cfg)
|
| 78 |
+
groups[(cfg['policy'] + '/' + cfg['admission'], cfg['family'], cfg['budget'], conditions)].append(row)
|
| 79 |
+
output = []
|
| 80 |
+
for (method, family, budget, conditions), members in sorted(groups.items()):
|
| 81 |
+
parents, structural_seeds, seed_parents = defaultdict(dict), defaultdict(set), {}
|
| 82 |
+
declared = True
|
| 83 |
+
for row in members:
|
| 84 |
+
cfg, parent = row['configuration'], row['parent_instance_id']
|
| 85 |
+
seed = cfg.get('instance_seed')
|
| 86 |
+
if type(seed) is int and 0 <= seed <= 2**32 - 1:
|
| 87 |
+
if family == 'generated_protocol' and seed in seed_parents and seed_parents[seed] != parent:
|
| 88 |
+
raise ValueError('A structural seed identifies inconsistent semantic target dynamics')
|
| 89 |
+
seed_parents[seed] = parent
|
| 90 |
+
structural_seeds[parent].add(seed)
|
| 91 |
+
else:
|
| 92 |
+
declared = False
|
| 93 |
+
cell = (cfg['seed'], cfg['implementation'])
|
| 94 |
+
outcome = _outcome(row)
|
| 95 |
+
if cell in parents[parent] and parents[parent][cell] != outcome:
|
| 96 |
+
raise ValueError('Inconsistent duplicate nuisance-cell audit outcomes')
|
| 97 |
+
parents[parent][cell] = outcome
|
| 98 |
+
targets = len(parents)
|
| 99 |
+
collisions = sum(max(0, len(seeds) - 1) for seeds in structural_seeds.values())
|
| 100 |
+
nuisance_designs = {frozenset(cells) for cells in parents.values()}
|
| 101 |
+
if family != 'generated_protocol':
|
| 102 |
+
reason = 'handpicked_fixtures'
|
| 103 |
+
elif not declared:
|
| 104 |
+
reason = 'undeclared_structural_seed'
|
| 105 |
+
elif collisions:
|
| 106 |
+
reason = 'structural_seed_collision'
|
| 107 |
+
elif len(nuisance_designs) != 1:
|
| 108 |
+
reason = 'unequal_nuisance_design'
|
| 109 |
+
else:
|
| 110 |
+
reason = 'independent_generator_draws_assumed'
|
| 111 |
+
eligible = reason == 'independent_generator_draws_assumed'
|
| 112 |
+
useful, admitted, harmful = [], [], []
|
| 113 |
+
deployment_events = calibration_events = unknown_deployment = 0
|
| 114 |
+
for cells in parents.values():
|
| 115 |
+
values = list(cells.values())
|
| 116 |
+
useful.append(sum(value[1] for value in values) / len(values))
|
| 117 |
+
admitted.append(sum(value[0] for value in values) / len(values))
|
| 118 |
+
harmful.append(sum(value[2] for value in values) / len(values))
|
| 119 |
+
any_hazard = any(value[3] is True for value in values)
|
| 120 |
+
deployment_events += any_hazard
|
| 121 |
+
unknown_deployment += not any_hazard and any(value[3] is None for value in values)
|
| 122 |
+
calibration_events += any(value[4] for value in values)
|
| 123 |
+
useful_mean = math.fsum(useful) / targets
|
| 124 |
+
admitted_mean, harmful_mean = math.fsum(admitted) / targets, math.fsum(harmful) / targets
|
| 125 |
+
useful_interval = None
|
| 126 |
+
if eligible:
|
| 127 |
+
radius = math.sqrt(math.log(2 / (1 - confidence)) / (2 * targets))
|
| 128 |
+
useful_interval = [max(0.0, useful_mean - radius), min(1.0, useful_mean + radius)]
|
| 129 |
+
output.append({
|
| 130 |
+
'method': method, 'family': family, 'budget': budget,
|
| 131 |
+
**condition_fields(conditions), 'confidence': confidence,
|
| 132 |
+
'requests': len(members), 'unique_nuisance_requests': sum(len(cells) for cells in parents.values()),
|
| 133 |
+
'parent_clusters': targets,
|
| 134 |
+
'independent_parents': targets if family == 'generated_protocol' and declared else 0,
|
| 135 |
+
'structural_seed_collisions': collisions, 'inference_eligible': eligible,
|
| 136 |
+
'inference_reason': reason,
|
| 137 |
+
'interval_status': 'conditional_generator_sampling' if eligible else 'descriptive_' + reason,
|
| 138 |
+
'target_any_deployment_hazard': _event_metric(deployment_events, targets, unknown_deployment,
|
| 139 |
+
eligible, confidence),
|
| 140 |
+
'target_any_calibration_hazard': _event_metric(calibration_events, targets, 0, eligible, confidence),
|
| 141 |
+
'target_average_useful_coverage': {'estimate': useful_mean, 'interval': useful_interval},
|
| 142 |
+
'conditional_selective_harm': harmful_mean / admitted_mean if admitted_mean else None,
|
| 143 |
+
'scope': ('Synthetic target-cluster estimates. Intervals, when present, assume independent generator '
|
| 144 |
+
'draws and an identical fixed nuisance-cell design; each interval is pointwise, not a '
|
| 145 |
+
'simultaneous guarantee. Structural hashes and planned seed selection do not establish '
|
| 146 |
+
'independence or real-world probabilities. Exact binomial bounds describe any hazard '
|
| 147 |
+
'across the nuisance design; useful-coverage bounds are Hoeffding bounds for equal-target '
|
| 148 |
+
'means. Selective harm is the ratio of equal-target failure and admission means; '
|
| 149 |
+
'abstention leaves it undefined when no target admits.')})
|
| 150 |
+
return output
|
crosswire/server.py
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Loopback workbench with a single cancellable scientific worker."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
from dataclasses import asdict
|
| 5 |
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
| 6 |
+
import json
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import queue
|
| 9 |
+
import threading
|
| 10 |
+
from urllib.parse import urlsplit
|
| 11 |
+
import uuid
|
| 12 |
+
|
| 13 |
+
from . import __version__
|
| 14 |
+
from .fixtures import catalog
|
| 15 |
+
from .backends import CANDIDATE_BACKENDS
|
| 16 |
+
from .receipts import check_envelope, pack, replay, validate
|
| 17 |
+
from .study import ADMISSIONS, EXECUTION_MODES, POLICIES, PROBE_SAFETY_MODES, RECOVERY_STRATEGIES, Cancelled, RunConfig, run, run_study
|
| 18 |
+
|
| 19 |
+
MAX_BODY = 20 * 1024 * 1024
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class Jobs:
|
| 23 |
+
def __init__(self):
|
| 24 |
+
self.jobs = {}
|
| 25 |
+
self.lock = threading.RLock()
|
| 26 |
+
self.queue = queue.Queue(maxsize=8)
|
| 27 |
+
self.worker = threading.Thread(target=self._work, daemon=True, name='crosswire-science')
|
| 28 |
+
self.worker.start()
|
| 29 |
+
|
| 30 |
+
def submit(self, kind, configuration):
|
| 31 |
+
with self.lock:
|
| 32 |
+
if self.queue.full():
|
| 33 |
+
raise ValueError('Worker queue is full; cancel or wait for an existing job')
|
| 34 |
+
if len(self.jobs) >= 64:
|
| 35 |
+
finished = next((k for k, v in self.jobs.items() if v['status'] in ('COMPLETE', 'FAILED', 'CANCELLED')), None)
|
| 36 |
+
if finished:
|
| 37 |
+
del self.jobs[finished]
|
| 38 |
+
else:
|
| 39 |
+
raise ValueError('Too many active jobs')
|
| 40 |
+
identifier = uuid.uuid4().hex[:16]
|
| 41 |
+
self.jobs[identifier] = {'id': identifier, 'status': 'QUEUED', 'kind': kind,
|
| 42 |
+
'configuration': configuration, 'cancel': threading.Event(),
|
| 43 |
+
'progress': {'current': 0, 'total': 1, 'message': 'Queued'}}
|
| 44 |
+
self.queue.put_nowait(identifier)
|
| 45 |
+
return {'id': identifier, 'status': 'QUEUED'}
|
| 46 |
+
|
| 47 |
+
def snapshot(self, identifier):
|
| 48 |
+
with self.lock:
|
| 49 |
+
if identifier not in self.jobs:
|
| 50 |
+
raise KeyError('Unknown job')
|
| 51 |
+
value = self.jobs[identifier]
|
| 52 |
+
return {k: v for k, v in value.items() if k not in ('cancel', 'configuration', 'kind')}
|
| 53 |
+
|
| 54 |
+
def result(self, identifier):
|
| 55 |
+
value = self.snapshot(identifier)
|
| 56 |
+
if (value['status'] != 'COMPLETE'
|
| 57 |
+
or value.get('result', {}).get('schema') not in ('crosswire-result-v1', 'crosswire-study-v1')):
|
| 58 |
+
raise RuntimeError('Only completed experiment or study jobs have exportable evidence')
|
| 59 |
+
return value['result']
|
| 60 |
+
|
| 61 |
+
def cancel(self, identifier):
|
| 62 |
+
with self.lock:
|
| 63 |
+
if identifier not in self.jobs:
|
| 64 |
+
raise KeyError('Unknown job')
|
| 65 |
+
self.jobs[identifier]['cancel'].set()
|
| 66 |
+
return self.snapshot(identifier)
|
| 67 |
+
|
| 68 |
+
def import_result(self, receipt):
|
| 69 |
+
# Envelope and digests are checked synchronously; the expensive exact
|
| 70 |
+
# recomputation runs on the bounded, cancellable science worker.
|
| 71 |
+
check_envelope(receipt)
|
| 72 |
+
return self.submit('import', receipt)
|
| 73 |
+
|
| 74 |
+
def replay_result(self, identifier):
|
| 75 |
+
return self.submit('replay', pack(self.result(identifier)))
|
| 76 |
+
|
| 77 |
+
def _work(self):
|
| 78 |
+
while True:
|
| 79 |
+
identifier = self.queue.get()
|
| 80 |
+
if identifier is None:
|
| 81 |
+
self.queue.task_done()
|
| 82 |
+
return
|
| 83 |
+
job = self.jobs[identifier]
|
| 84 |
+
try:
|
| 85 |
+
if job['cancel'].is_set():
|
| 86 |
+
raise Cancelled()
|
| 87 |
+
with self.lock:
|
| 88 |
+
job['status'] = 'RUNNING'
|
| 89 |
+
|
| 90 |
+
def progress(current, total, message):
|
| 91 |
+
with self.lock:
|
| 92 |
+
job['progress'] = {'current': current, 'total': total, 'message': message}
|
| 93 |
+
|
| 94 |
+
if job['kind'] == 'run':
|
| 95 |
+
result = run(RunConfig.from_dict(job['configuration']), job['cancel'].is_set, progress)
|
| 96 |
+
elif job['kind'] == 'study':
|
| 97 |
+
result = run_study(job['configuration'], job['cancel'].is_set, progress)
|
| 98 |
+
elif job['kind'] == 'import':
|
| 99 |
+
result = validate(job['configuration'], job['cancel'].is_set, progress)
|
| 100 |
+
elif job['kind'] == 'replay':
|
| 101 |
+
result = replay(job['configuration'], job['cancel'].is_set, progress)
|
| 102 |
+
else:
|
| 103 |
+
raise ValueError('Unknown job kind')
|
| 104 |
+
if job['cancel'].is_set():
|
| 105 |
+
raise Cancelled()
|
| 106 |
+
done = {'import': 'Imported; exact scientific replay matched',
|
| 107 |
+
'replay': 'Replay complete'}.get(job['kind'], 'Scientific result complete')
|
| 108 |
+
with self.lock:
|
| 109 |
+
job.update(status='COMPLETE', result=result,
|
| 110 |
+
progress={'current': 1, 'total': 1, 'message': done})
|
| 111 |
+
except Cancelled:
|
| 112 |
+
with self.lock:
|
| 113 |
+
job.update(status='CANCELLED', progress={'current': 0, 'total': 1, 'message': 'Cancelled; no complete receipt'})
|
| 114 |
+
except Exception as exc:
|
| 115 |
+
with self.lock:
|
| 116 |
+
job.update(status='FAILED', error=f'{type(exc).__name__}: {exc}')
|
| 117 |
+
finally:
|
| 118 |
+
self.queue.task_done()
|
| 119 |
+
|
| 120 |
+
def close(self):
|
| 121 |
+
with self.lock:
|
| 122 |
+
for job in self.jobs.values():
|
| 123 |
+
job['cancel'].set()
|
| 124 |
+
try:
|
| 125 |
+
self.queue.put(None, timeout=3)
|
| 126 |
+
except queue.Full:
|
| 127 |
+
return
|
| 128 |
+
self.worker.join(5)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
class Handler(BaseHTTPRequestHandler):
|
| 132 |
+
server_version = 'CROSSWIRE/0.1'
|
| 133 |
+
|
| 134 |
+
def log_message(self, *_):
|
| 135 |
+
pass
|
| 136 |
+
|
| 137 |
+
def _json(self, status, value):
|
| 138 |
+
data = json.dumps(value, allow_nan=False).encode()
|
| 139 |
+
self.send_response(status)
|
| 140 |
+
self.send_header('Content-Type', 'application/json; charset=utf-8')
|
| 141 |
+
self.send_header('Content-Length', str(len(data)))
|
| 142 |
+
self.send_header('Cache-Control', 'no-store')
|
| 143 |
+
self.send_header('X-Content-Type-Options', 'nosniff')
|
| 144 |
+
self.end_headers()
|
| 145 |
+
try:
|
| 146 |
+
self.wfile.write(data)
|
| 147 |
+
except (BrokenPipeError, ConnectionResetError):
|
| 148 |
+
pass
|
| 149 |
+
|
| 150 |
+
def _body(self):
|
| 151 |
+
if self.headers.get_content_type() != 'application/json':
|
| 152 |
+
raise ValueError('Expected application/json')
|
| 153 |
+
length = int(self.headers.get('Content-Length', '0'))
|
| 154 |
+
if not 0 < length <= MAX_BODY:
|
| 155 |
+
raise ValueError('JSON request must be between 1 byte and 20 MiB')
|
| 156 |
+
return json.loads(self.rfile.read(length), parse_constant=lambda _: (_ for _ in ()).throw(ValueError('Nonfinite JSON number')))
|
| 157 |
+
|
| 158 |
+
def _same_origin(self):
|
| 159 |
+
host = self.headers.get('Host', '')
|
| 160 |
+
expected = {f'127.0.0.1:{self.server.server_port}', f'localhost:{self.server.server_port}'}
|
| 161 |
+
if host not in expected:
|
| 162 |
+
return False
|
| 163 |
+
origin = self.headers.get('Origin')
|
| 164 |
+
return origin is None or origin in {f'http://{h}' for h in expected}
|
| 165 |
+
|
| 166 |
+
def do_GET(self):
|
| 167 |
+
if not self._same_origin():
|
| 168 |
+
self._json(403, {'error': 'Only the local workbench origin is accepted'})
|
| 169 |
+
return
|
| 170 |
+
path = urlsplit(self.path).path
|
| 171 |
+
try:
|
| 172 |
+
if path == '/api/catalog':
|
| 173 |
+
self._json(200, {'version': __version__, 'families': catalog(), 'policies': POLICIES,
|
| 174 |
+
'admissions': ADMISSIONS, 'execution_modes': EXECUTION_MODES,
|
| 175 |
+
'probe_safety_modes': PROBE_SAFETY_MODES,
|
| 176 |
+
'candidate_backends': CANDIDATE_BACKENDS, 'recovery_strategies': RECOVERY_STRATEGIES,
|
| 177 |
+
'defaults': asdict(RunConfig())})
|
| 178 |
+
elif path.startswith('/api/runs/'):
|
| 179 |
+
parts = path.strip('/').split('/')
|
| 180 |
+
if len(parts) == 3:
|
| 181 |
+
self._json(200, self.server.manager.snapshot(parts[2]))
|
| 182 |
+
elif len(parts) == 4 and parts[3] == 'export':
|
| 183 |
+
self._json(200, pack(self.server.manager.result(parts[2])))
|
| 184 |
+
else:
|
| 185 |
+
self._json(404, {'error': 'Unknown route'})
|
| 186 |
+
elif path in ('/', '/index.html', '/styles.css', '/app.js'):
|
| 187 |
+
name = 'index.html' if path == '/' else path[1:]
|
| 188 |
+
data = (Path(__file__).parent / 'web' / name).read_bytes()
|
| 189 |
+
self.send_response(200)
|
| 190 |
+
self.send_header('Content-Type', {'index.html': 'text/html', 'styles.css': 'text/css', 'app.js': 'text/javascript'}[name] + '; charset=utf-8')
|
| 191 |
+
self.send_header('Content-Length', str(len(data)))
|
| 192 |
+
self.send_header('X-Content-Type-Options', 'nosniff')
|
| 193 |
+
self.send_header('Content-Security-Policy', "default-src 'self'; script-src 'self'; style-src 'self'; connect-src 'self'; object-src 'none'; frame-ancestors 'none'")
|
| 194 |
+
self.end_headers()
|
| 195 |
+
self.wfile.write(data)
|
| 196 |
+
elif path == '/favicon.ico':
|
| 197 |
+
self.send_response(204)
|
| 198 |
+
self.send_header('Content-Length', '0')
|
| 199 |
+
self.end_headers()
|
| 200 |
+
else:
|
| 201 |
+
self._json(404, {'error': 'Unknown route'})
|
| 202 |
+
except KeyError as exc:
|
| 203 |
+
self._json(404, {'error': str(exc)})
|
| 204 |
+
except RuntimeError as exc:
|
| 205 |
+
self._json(409, {'error': str(exc)})
|
| 206 |
+
except (ValueError, TypeError) as exc:
|
| 207 |
+
self._json(409, {'error': str(exc)})
|
| 208 |
+
|
| 209 |
+
def do_POST(self):
|
| 210 |
+
if not self._same_origin():
|
| 211 |
+
self._json(403, {'error': 'Only the local workbench origin is accepted'})
|
| 212 |
+
return
|
| 213 |
+
path = urlsplit(self.path).path
|
| 214 |
+
try:
|
| 215 |
+
data = self._body()
|
| 216 |
+
if path == '/api/runs':
|
| 217 |
+
config = RunConfig.from_dict(data)
|
| 218 |
+
self._json(202, self.server.manager.submit('run', asdict(config)))
|
| 219 |
+
elif path == '/api/studies':
|
| 220 |
+
# Full bounded study validation is performed before job dispatch.
|
| 221 |
+
from .study import study_configs
|
| 222 |
+
study_configs(data)
|
| 223 |
+
self._json(202, self.server.manager.submit('study', data))
|
| 224 |
+
elif path == '/api/import':
|
| 225 |
+
if not isinstance(data, dict):
|
| 226 |
+
raise ValueError('Expected a receipt envelope')
|
| 227 |
+
if 'receipt_text' in data:
|
| 228 |
+
receipt = json.loads(data['receipt_text'])
|
| 229 |
+
elif 'receipt' in data:
|
| 230 |
+
receipt = data['receipt']
|
| 231 |
+
else:
|
| 232 |
+
raise ValueError('Expected a receipt envelope')
|
| 233 |
+
self._json(202, self.server.manager.import_result(receipt))
|
| 234 |
+
elif path.startswith('/api/runs/'):
|
| 235 |
+
parts = path.strip('/').split('/')
|
| 236 |
+
if len(parts) == 4 and parts[3] == 'cancel':
|
| 237 |
+
self._json(200, self.server.manager.cancel(parts[2]))
|
| 238 |
+
elif len(parts) == 4 and parts[3] == 'replay':
|
| 239 |
+
self._json(202, self.server.manager.replay_result(parts[2]))
|
| 240 |
+
else:
|
| 241 |
+
self._json(404, {'error': 'Unknown route'})
|
| 242 |
+
else:
|
| 243 |
+
self._json(404, {'error': 'Unknown route'})
|
| 244 |
+
except (ValueError, TypeError, json.JSONDecodeError) as exc:
|
| 245 |
+
self._json(400, {'error': str(exc)})
|
| 246 |
+
except KeyError as exc:
|
| 247 |
+
self._json(404, {'error': str(exc)})
|
| 248 |
+
except RuntimeError as exc:
|
| 249 |
+
self._json(409, {'error': str(exc)})
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
def make_server(port=8781):
|
| 253 |
+
if type(port) is not int or not 0 <= port <= 65535:
|
| 254 |
+
raise ValueError('Invalid port')
|
| 255 |
+
server = ThreadingHTTPServer(('127.0.0.1', port), Handler)
|
| 256 |
+
server.daemon_threads = True
|
| 257 |
+
server.manager = Jobs()
|
| 258 |
+
return server
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def serve(port=8781, open_browser=False):
|
| 262 |
+
server = make_server(port)
|
| 263 |
+
url = f'http://127.0.0.1:{server.server_port}/'
|
| 264 |
+
print(url, flush=True)
|
| 265 |
+
if open_browser:
|
| 266 |
+
import webbrowser
|
| 267 |
+
webbrowser.open(url)
|
| 268 |
+
try:
|
| 269 |
+
server.serve_forever()
|
| 270 |
+
except KeyboardInterrupt:
|
| 271 |
+
pass
|
| 272 |
+
finally:
|
| 273 |
+
server.server_close()
|
| 274 |
+
server.manager.close()
|
crosswire/shield.py
ADDED
|
@@ -0,0 +1,496 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Closed-loop dynamic runtime shield for black-box deployment.
|
| 2 |
+
|
| 3 |
+
In open-loop deployment, an admitted word is dispatched unconditionally. Under
|
| 4 |
+
latent drift, unmodeled delays, or assumption violations, intermediate outputs
|
| 5 |
+
may contradict the surviving models or reveal that subsequent actions lead to an
|
| 6 |
+
absorbing hazard. The dynamic runtime shield monitors intermediate outputs at
|
| 7 |
+
every step, updates the active survivor set, and halts or re-plans before
|
| 8 |
+
hazardous actions are executed.
|
| 9 |
+
"""
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
from itertools import product
|
| 13 |
+
from typing import Callable
|
| 14 |
+
from .core import CandidateSpace, Contract, _integer, _word
|
| 15 |
+
from .interfaces import InterfaceError, TargetProcess
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _check_cancel(cancel):
|
| 19 |
+
if cancel and cancel():
|
| 20 |
+
from .study import Cancelled
|
| 21 |
+
raise Cancelled('Deployment cancelled by user')
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class RuntimeShield:
|
| 25 |
+
"""Monitors step-by-step target execution and intervenes when safety is violated."""
|
| 26 |
+
|
| 27 |
+
def __init__(self, space: CandidateSpace, initial_survivors: tuple[int, ...],
|
| 28 |
+
contract: Contract, mode: str = "shielded", cancel: Callable[[], bool] | None = None,
|
| 29 |
+
fault_schedule: dict | None = None, recovery_strategy: str = "stabilize"):
|
| 30 |
+
if mode not in ('open_loop', 'shielded', 'recovery', 'replan'):
|
| 31 |
+
raise ValueError('Unknown execution mode')
|
| 32 |
+
if recovery_strategy not in ('stabilize', 'soft_return'):
|
| 33 |
+
raise ValueError('Unknown recovery strategy: must be "stabilize" or "soft_return"')
|
| 34 |
+
self.space = space
|
| 35 |
+
self.contract = contract
|
| 36 |
+
self.mode = mode
|
| 37 |
+
self.cancel = cancel
|
| 38 |
+
self.fault_schedule = fault_schedule
|
| 39 |
+
self.recovery_strategy = recovery_strategy
|
| 40 |
+
self.change_point_step = fault_schedule.get('step') if fault_schedule else None
|
| 41 |
+
self.fault_type = fault_schedule.get('type') if fault_schedule else None
|
| 42 |
+
self.change_point_detected = False
|
| 43 |
+
self.detection_step = None
|
| 44 |
+
self.detection_latency = None
|
| 45 |
+
self.active_survivors = space._ids(initial_survivors)
|
| 46 |
+
self.history_words: list[int] = []
|
| 47 |
+
self.history_outputs: list[int] = []
|
| 48 |
+
self.survivor_counts: list[int] = [len(self.active_survivors)]
|
| 49 |
+
self.status = "ACTIVE"
|
| 50 |
+
self.intervened = False
|
| 51 |
+
self.reasons: list[str] = []
|
| 52 |
+
self.hazard_averted = False
|
| 53 |
+
self.recovery_requested = False
|
| 54 |
+
self.recovery_attempted = False
|
| 55 |
+
self.recovery_certified = False
|
| 56 |
+
self.recovery_succeeded = False
|
| 57 |
+
self.recovery_reason = 'not_requested'
|
| 58 |
+
self.recovery_action = None
|
| 59 |
+
self.recovery_actions = []
|
| 60 |
+
self.recovery_returned_to_initial = False
|
| 61 |
+
self.recovery_candidate_count = 0
|
| 62 |
+
self.recovery_remaining_horizon = contract.horizon
|
| 63 |
+
self.replanned = False
|
| 64 |
+
self.replan_events: list[dict] = []
|
| 65 |
+
self.replanned_suffix: tuple[int, ...] | None = None
|
| 66 |
+
|
| 67 |
+
def _check_change_point_detection(self, current_step_index: int):
|
| 68 |
+
if (self.fault_schedule and self.fault_schedule.get('phase', 'deployment') in ('deployment', 'always')
|
| 69 |
+
and self.change_point_step is not None):
|
| 70 |
+
if current_step_index >= self.change_point_step and not self.change_point_detected:
|
| 71 |
+
self.change_point_detected = True
|
| 72 |
+
self.detection_step = current_step_index
|
| 73 |
+
self.detection_latency = current_step_index - self.change_point_step
|
| 74 |
+
|
| 75 |
+
def change_point_record(self) -> dict:
|
| 76 |
+
return {
|
| 77 |
+
'scheduled': self.fault_schedule is not None,
|
| 78 |
+
'step': self.change_point_step,
|
| 79 |
+
'type': self.fault_type,
|
| 80 |
+
'detected': self.change_point_detected,
|
| 81 |
+
'detection_step': self.detection_step,
|
| 82 |
+
'detection_latency': self.detection_latency,
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
def _find_safe_suffix(self, prefix: tuple[int, ...], remaining_horizon: int) -> tuple[int, ...] | None:
|
| 86 |
+
"""Find shortest lexicographic action suffix satisfying the contract under all active survivors."""
|
| 87 |
+
if not self.active_survivors or remaining_horizon <= 0:
|
| 88 |
+
return None
|
| 89 |
+
for length in range(1, remaining_horizon + 1):
|
| 90 |
+
for cand in product(range(self.space.machine.inputs), repeat=length):
|
| 91 |
+
full = prefix + cand
|
| 92 |
+
if all(self.contract.accepts(self.space.semantic(aid, full)) for aid in self.active_survivors):
|
| 93 |
+
return cand
|
| 94 |
+
return None
|
| 95 |
+
|
| 96 |
+
def _observe(self, action: int, output: int):
|
| 97 |
+
self.history_words.append(action)
|
| 98 |
+
self.history_outputs.append(output)
|
| 99 |
+
word, outputs = tuple(self.history_words), tuple(self.history_outputs)
|
| 100 |
+
self.active_survivors = tuple(aid for aid in self.active_survivors
|
| 101 |
+
if self.space.predict(aid, word) == outputs)
|
| 102 |
+
self.survivor_counts.append(len(self.active_survivors))
|
| 103 |
+
|
| 104 |
+
def recovery_record(self) -> dict:
|
| 105 |
+
return {'requested': self.recovery_requested, 'attempted': self.recovery_attempted,
|
| 106 |
+
'certified': self.recovery_certified, 'succeeded': self.recovery_succeeded,
|
| 107 |
+
'strategy': self.recovery_strategy,
|
| 108 |
+
'reason': self.recovery_reason, 'action': self.recovery_action,
|
| 109 |
+
'actions': list(self.recovery_actions),
|
| 110 |
+
'return_to_initial': self.recovery_returned_to_initial,
|
| 111 |
+
'candidate_count': self.recovery_candidate_count,
|
| 112 |
+
'remaining_horizon': self.recovery_remaining_horizon,
|
| 113 |
+
'scope': 'Conditional on target inclusion in the live finite model family, '
|
| 114 |
+
'stationarity, deterministic reset and truthful anchors. A consistent '
|
| 115 |
+
'response does not establish real-world safety or task completion.'}
|
| 116 |
+
|
| 117 |
+
def recover(self, target: TargetProcess) -> int | None:
|
| 118 |
+
"""Execute one model-safe fallback or certified soft return-to-home.
|
| 119 |
+
|
| 120 |
+
Refutation supplies no replacement safety model. Without an independently
|
| 121 |
+
specified backup controller, the only justified response is to halt.
|
| 122 |
+
"""
|
| 123 |
+
self.recovery_requested = True
|
| 124 |
+
self.recovery_succeeded = False
|
| 125 |
+
self.recovery_certified = False
|
| 126 |
+
self.recovery_action = None
|
| 127 |
+
self.recovery_actions = []
|
| 128 |
+
self.recovery_returned_to_initial = False
|
| 129 |
+
prefix, outputs = tuple(self.history_words), tuple(self.history_outputs)
|
| 130 |
+
self.active_survivors = tuple(aid for aid in self.active_survivors
|
| 131 |
+
if self.space.predict(aid, prefix) == outputs)
|
| 132 |
+
self.recovery_candidate_count = len(self.active_survivors)
|
| 133 |
+
self.recovery_remaining_horizon = max(0, self.contract.horizon - len(prefix))
|
| 134 |
+
if not self.active_survivors:
|
| 135 |
+
self.recovery_reason = 'model_contradicted'
|
| 136 |
+
self.reasons.append('Recovery withheld: no model remains consistent with observed history')
|
| 137 |
+
return None
|
| 138 |
+
if not self.recovery_remaining_horizon:
|
| 139 |
+
self.recovery_reason = 'horizon_exhausted'
|
| 140 |
+
self.reasons.append('Recovery withheld: contract horizon is exhausted')
|
| 141 |
+
return None
|
| 142 |
+
|
| 143 |
+
if self.recovery_strategy == 'soft_return':
|
| 144 |
+
ret = synthesize_safe_return(self.space, self.active_survivors, prefix,
|
| 145 |
+
max_length=self.recovery_remaining_horizon)
|
| 146 |
+
if not ret['certified']:
|
| 147 |
+
self.recovery_reason = ret['reason']
|
| 148 |
+
self.reasons.append(f"Recovery withheld: {ret['reason']}")
|
| 149 |
+
return None
|
| 150 |
+
word = ret.get('word')
|
| 151 |
+
if not word:
|
| 152 |
+
self.recovery_certified = True
|
| 153 |
+
self.recovery_succeeded = True
|
| 154 |
+
self.recovery_returned_to_initial = True
|
| 155 |
+
self.recovery_reason = 'already_at_initial'
|
| 156 |
+
self.status = 'RECOVERED_SAFE_FALLBACK'
|
| 157 |
+
self.reasons.append('Model-certified soft return: target is already at initial state')
|
| 158 |
+
return None
|
| 159 |
+
self.recovery_certified = True
|
| 160 |
+
self.recovery_actions = list(word)
|
| 161 |
+
self.recovery_action = word[0]
|
| 162 |
+
_check_cancel(self.cancel)
|
| 163 |
+
self.recovery_attempted = True
|
| 164 |
+
last_out = None
|
| 165 |
+
for act in word:
|
| 166 |
+
_check_cancel(self.cancel)
|
| 167 |
+
try:
|
| 168 |
+
last_out = target.step(act)
|
| 169 |
+
except InterfaceError:
|
| 170 |
+
self.recovery_reason = 'interface_error'
|
| 171 |
+
raise
|
| 172 |
+
self._observe(act, last_out)
|
| 173 |
+
if not self.active_survivors:
|
| 174 |
+
self.recovery_reason = 'response_contradiction'
|
| 175 |
+
self.status = 'INTERVENED_ANOMALY'
|
| 176 |
+
self.hazard_averted = False
|
| 177 |
+
self.reasons.append('Recovery response contradicted every certifying model; safety is unresolved')
|
| 178 |
+
return last_out
|
| 179 |
+
self.recovery_succeeded = True
|
| 180 |
+
self.recovery_returned_to_initial = ret.get('return_to_initial', False)
|
| 181 |
+
self.recovery_reason = 'certified_return_to_initial' if self.recovery_returned_to_initial else 'certified_safe_hold'
|
| 182 |
+
self.status = 'RECOVERED_SAFE_FALLBACK'
|
| 183 |
+
self.reasons.append('Model-certified safe return executed with responses consistent with the live belief')
|
| 184 |
+
return last_out
|
| 185 |
+
|
| 186 |
+
safe_action = next((action for action in range(self.space.machine.inputs)
|
| 187 |
+
if all(self.contract.hazard not in self.space.semantic(aid, prefix + (action,))
|
| 188 |
+
for aid in self.active_survivors)), None)
|
| 189 |
+
if safe_action is None:
|
| 190 |
+
self.recovery_reason = 'no_safe_action'
|
| 191 |
+
self.reasons.append('Recovery withheld: no action is hazard-free under every live model')
|
| 192 |
+
return None
|
| 193 |
+
self.recovery_action = safe_action
|
| 194 |
+
self.recovery_actions = [safe_action]
|
| 195 |
+
self.recovery_certified = True
|
| 196 |
+
_check_cancel(self.cancel)
|
| 197 |
+
self.recovery_attempted = True
|
| 198 |
+
try:
|
| 199 |
+
out = target.step(safe_action)
|
| 200 |
+
except InterfaceError:
|
| 201 |
+
self.recovery_reason = 'interface_error'
|
| 202 |
+
raise
|
| 203 |
+
self._observe(safe_action, out)
|
| 204 |
+
if not self.active_survivors:
|
| 205 |
+
self.recovery_reason = 'response_contradiction'
|
| 206 |
+
self.status = 'INTERVENED_ANOMALY'
|
| 207 |
+
self.hazard_averted = False
|
| 208 |
+
self.reasons.append('Recovery response contradicted every certifying model; safety is unresolved')
|
| 209 |
+
return out
|
| 210 |
+
self.recovery_succeeded = True
|
| 211 |
+
self.recovery_reason = 'certified_response'
|
| 212 |
+
self.status = 'RECOVERED_SAFE_FALLBACK'
|
| 213 |
+
self.reasons.append('Model-certified fallback executed with a response consistent with the live belief')
|
| 214 |
+
return out
|
| 215 |
+
|
| 216 |
+
def step(self, target: TargetProcess, planned_action: int, remaining_plan: list[int]) -> tuple[int | None, bool]:
|
| 217 |
+
"""Execute one step with active pre- and post-condition safety guards.
|
| 218 |
+
|
| 219 |
+
Returns (output, should_continue).
|
| 220 |
+
"""
|
| 221 |
+
if self.mode == "open_loop":
|
| 222 |
+
# Unshielded: directly step the target
|
| 223 |
+
out = target.step(planned_action)
|
| 224 |
+
self.history_words.append(planned_action)
|
| 225 |
+
self.history_outputs.append(out)
|
| 226 |
+
return out, True
|
| 227 |
+
|
| 228 |
+
# Pre-condition check: if survivors are already empty
|
| 229 |
+
if not self.active_survivors:
|
| 230 |
+
self._check_change_point_detection(len(self.history_words))
|
| 231 |
+
self.status = "INTERVENED_ANOMALY"
|
| 232 |
+
self.intervened = True
|
| 233 |
+
self.reasons.append("Pre-step abort: no surviving models match history (anomaly/drift detected)")
|
| 234 |
+
if self.mode in ("recovery", "replan"):
|
| 235 |
+
rec_out = self.recover(target)
|
| 236 |
+
return rec_out, False
|
| 237 |
+
return None, False
|
| 238 |
+
|
| 239 |
+
# Consensus safety requires every surviving interpretation to permit the
|
| 240 |
+
# next action, including before the first target observation is available.
|
| 241 |
+
candidate_prefix = tuple(self.history_words) + (planned_action,)
|
| 242 |
+
if any(self.contract.hazard in self.space.semantic(aid, candidate_prefix)
|
| 243 |
+
for aid in self.active_survivors):
|
| 244 |
+
if self.mode == "replan":
|
| 245 |
+
rem_h = max(0, self.contract.horizon - len(self.history_words))
|
| 246 |
+
safe_suffix = self._find_safe_suffix(tuple(self.history_words), rem_h)
|
| 247 |
+
if safe_suffix is not None:
|
| 248 |
+
self._check_change_point_detection(len(self.history_words))
|
| 249 |
+
self.replanned = True
|
| 250 |
+
self.replan_events.append({
|
| 251 |
+
'step': len(self.history_words), 'trigger': 'pre_step_hazard',
|
| 252 |
+
'old_action': planned_action, 'new_suffix': list(safe_suffix),
|
| 253 |
+
'survivor_count': len(self.active_survivors)
|
| 254 |
+
})
|
| 255 |
+
self.status = "REPLANNED_ONLINE_SAFE"
|
| 256 |
+
self.reasons.append(f"Replanned online at step {len(self.history_words)}: switched to safe suffix {list(safe_suffix)}")
|
| 257 |
+
planned_action = safe_suffix[0]
|
| 258 |
+
self.replanned_suffix = safe_suffix[1:]
|
| 259 |
+
else:
|
| 260 |
+
self._check_change_point_detection(len(self.history_words))
|
| 261 |
+
self.status = "INTERVENED_HAZARD_AVERTED"
|
| 262 |
+
self.intervened = True
|
| 263 |
+
self.hazard_averted = True
|
| 264 |
+
self.reasons.append(f"Pre-step abort at step {len(self.history_words) + 1}: action {planned_action} risks hazard and no safe replan exists")
|
| 265 |
+
rec_out = self.recover(target)
|
| 266 |
+
return rec_out, False
|
| 267 |
+
else:
|
| 268 |
+
self._check_change_point_detection(len(self.history_words))
|
| 269 |
+
self.status = "INTERVENED_HAZARD_AVERTED"
|
| 270 |
+
self.intervened = True
|
| 271 |
+
self.hazard_averted = True
|
| 272 |
+
self.reasons.append(f"Pre-step abort at step {len(self.history_words) + 1}: action {planned_action} risks hazard under a surviving adapter")
|
| 273 |
+
if self.mode == "recovery":
|
| 274 |
+
rec_out = self.recover(target)
|
| 275 |
+
return rec_out, False
|
| 276 |
+
return None, False
|
| 277 |
+
|
| 278 |
+
# Execute single step
|
| 279 |
+
out = target.step(planned_action)
|
| 280 |
+
self._observe(planned_action, out)
|
| 281 |
+
current_word = tuple(self.history_words)
|
| 282 |
+
|
| 283 |
+
if not self.active_survivors:
|
| 284 |
+
# Anomaly / distribution drift detected: target output contradicted all surviving models
|
| 285 |
+
self._check_change_point_detection(len(self.history_words) - 1)
|
| 286 |
+
self.status = "INTERVENED_ANOMALY"
|
| 287 |
+
self.intervened = True
|
| 288 |
+
self.reasons.append(f"Post-step abort at step {len(self.history_words)}: output {out} refuted all surviving adapters")
|
| 289 |
+
if self.mode in ("recovery", "replan"):
|
| 290 |
+
self.recover(target)
|
| 291 |
+
return out, False
|
| 292 |
+
return out, False
|
| 293 |
+
|
| 294 |
+
# If there are remaining planned steps, check if the remaining suffix is still safe
|
| 295 |
+
rem_plan_to_check = self.replanned_suffix if self.replanned_suffix is not None else remaining_plan
|
| 296 |
+
if rem_plan_to_check:
|
| 297 |
+
full_plan = current_word + tuple(rem_plan_to_check)
|
| 298 |
+
suffix_hazardous = any(
|
| 299 |
+
self.contract.hazard in self.space.semantic(adapter_id, full_plan)
|
| 300 |
+
for adapter_id in self.active_survivors
|
| 301 |
+
)
|
| 302 |
+
suffix_misses_goal = (self.mode == "replan") and any(
|
| 303 |
+
not self.contract.accepts(self.space.semantic(adapter_id, full_plan))
|
| 304 |
+
for adapter_id in self.active_survivors
|
| 305 |
+
)
|
| 306 |
+
|
| 307 |
+
if suffix_hazardous or suffix_misses_goal:
|
| 308 |
+
if self.mode == "replan":
|
| 309 |
+
rem_h = max(0, self.contract.horizon - len(self.history_words))
|
| 310 |
+
safe_suffix = self._find_safe_suffix(current_word, rem_h)
|
| 311 |
+
if safe_suffix is not None:
|
| 312 |
+
self._check_change_point_detection(len(self.history_words) - 1)
|
| 313 |
+
self.replanned = True
|
| 314 |
+
self.replan_events.append({
|
| 315 |
+
'step': len(self.history_words), 'trigger': 'post_step_suffix_risk',
|
| 316 |
+
'old_suffix': list(rem_plan_to_check), 'new_suffix': list(safe_suffix),
|
| 317 |
+
'survivor_count': len(self.active_survivors)
|
| 318 |
+
})
|
| 319 |
+
self.status = "REPLANNED_ONLINE_SAFE"
|
| 320 |
+
self.reasons.append(f"Replanned online at step {len(self.history_words)}: switched to safe suffix {list(safe_suffix)}")
|
| 321 |
+
self.replanned_suffix = safe_suffix
|
| 322 |
+
return out, True
|
| 323 |
+
else:
|
| 324 |
+
self._check_change_point_detection(len(self.history_words) - 1)
|
| 325 |
+
self.status = "INTERVENED_HAZARD_AVERTED"
|
| 326 |
+
self.intervened = True
|
| 327 |
+
self.hazard_averted = True
|
| 328 |
+
self.reasons.append(f"Post-step abort at step {len(self.history_words)}: upcoming plan suffix risks hazard and no safe replan exists")
|
| 329 |
+
self.recover(target)
|
| 330 |
+
return out, False
|
| 331 |
+
else:
|
| 332 |
+
self._check_change_point_detection(len(self.history_words) - 1)
|
| 333 |
+
self.status = "INTERVENED_HAZARD_AVERTED"
|
| 334 |
+
self.intervened = True
|
| 335 |
+
self.hazard_averted = True
|
| 336 |
+
self.reasons.append(f"Post-step abort at step {len(self.history_words)}: upcoming plan suffix {remaining_plan} risks hazard under updated survivors")
|
| 337 |
+
if self.mode == "recovery":
|
| 338 |
+
self.recover(target)
|
| 339 |
+
return out, False
|
| 340 |
+
return out, False
|
| 341 |
+
|
| 342 |
+
return out, True
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
def execute_deployment(target: TargetProcess, planned_word: list[int], space: CandidateSpace,
|
| 346 |
+
survivors: tuple[int, ...], contract: Contract,
|
| 347 |
+
mode: str = "open_loop", cancel: Callable[[], bool] | None = None,
|
| 348 |
+
fault_schedule: dict | None = None, recovery_strategy: str = "stabilize") -> dict:
|
| 349 |
+
"""Execute deployment through the target process with optional closed-loop shielding."""
|
| 350 |
+
if hasattr(target, 'set_phase'):
|
| 351 |
+
target.set_phase('deployment')
|
| 352 |
+
shield = RuntimeShield(space, survivors, contract, mode=mode, cancel=cancel,
|
| 353 |
+
fault_schedule=fault_schedule, recovery_strategy=recovery_strategy)
|
| 354 |
+
deployment = {
|
| 355 |
+
"word": list(planned_word),
|
| 356 |
+
"executed_word": [],
|
| 357 |
+
"outputs": [],
|
| 358 |
+
"complete": False,
|
| 359 |
+
"mode": mode,
|
| 360 |
+
"shield_status": "COMPLETED" if mode == "open_loop" else "ACTIVE",
|
| 361 |
+
"intervened": False,
|
| 362 |
+
"hazard_averted": False,
|
| 363 |
+
"reasons": [],
|
| 364 |
+
"survivor_history": [len(survivors)],
|
| 365 |
+
"target_steps": 0,
|
| 366 |
+
"resets": 0,
|
| 367 |
+
"target_calls": 0,
|
| 368 |
+
"error": None,
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
previous_counts = dict(target.counts)
|
| 372 |
+
try:
|
| 373 |
+
_check_cancel(cancel)
|
| 374 |
+
target.reset()
|
| 375 |
+
plan = list(planned_word)
|
| 376 |
+
step_idx = 0
|
| 377 |
+
while step_idx < len(plan):
|
| 378 |
+
_check_cancel(cancel)
|
| 379 |
+
action = plan[step_idx]
|
| 380 |
+
remaining = plan[step_idx + 1:]
|
| 381 |
+
_, should_continue = shield.step(target, action, remaining)
|
| 382 |
+
if shield.replanned_suffix is not None:
|
| 383 |
+
plan = plan[:step_idx + 1] + list(shield.replanned_suffix)
|
| 384 |
+
shield.replanned_suffix = None
|
| 385 |
+
if not should_continue:
|
| 386 |
+
break
|
| 387 |
+
step_idx += 1
|
| 388 |
+
|
| 389 |
+
deployment["complete"] = (len(shield.history_words) == len(plan)) and not shield.intervened
|
| 390 |
+
deployment["shield_status"] = shield.status if shield.intervened else ("REPLANNED_ONLINE_SAFE" if shield.replanned else "COMPLETED")
|
| 391 |
+
|
| 392 |
+
except InterfaceError as exc:
|
| 393 |
+
deployment["error"] = {"code": exc.code, "message": str(exc)}
|
| 394 |
+
deployment["shield_status"] = "INTERFACE_ERROR"
|
| 395 |
+
finally:
|
| 396 |
+
deployment["executed_word"] = list(shield.history_words)
|
| 397 |
+
deployment["outputs"] = list(shield.history_outputs)
|
| 398 |
+
deployment["intervened"] = shield.intervened
|
| 399 |
+
deployment["hazard_averted"] = shield.hazard_averted
|
| 400 |
+
deployment["replanned"] = shield.replanned
|
| 401 |
+
deployment["replan_count"] = len(shield.replan_events)
|
| 402 |
+
deployment["replan_events"] = list(shield.replan_events)
|
| 403 |
+
deployment["reasons"] = list(shield.reasons)
|
| 404 |
+
deployment["survivor_history"] = list(shield.survivor_counts)
|
| 405 |
+
deployment["recovery"] = shield.recovery_record()
|
| 406 |
+
deployment["change_point"] = shield.change_point_record()
|
| 407 |
+
deployment["target_steps"] = target.counts["step_attempts"] - previous_counts["step_attempts"]
|
| 408 |
+
deployment["resets"] = target.counts["reset_attempts"] - previous_counts["reset_attempts"]
|
| 409 |
+
deployment["target_calls"] = target.counts["target_calls"] - previous_counts["target_calls"]
|
| 410 |
+
|
| 411 |
+
return deployment
|
| 412 |
+
|
| 413 |
+
|
| 414 |
+
def synthesize_safe_return(space: CandidateSpace, survivors, prefix=(), max_length: int | None = None) -> dict:
|
| 415 |
+
"""Synthesize a certified hazard-free return-to-initial (soft reset) or stabilizing suffix.
|
| 416 |
+
|
| 417 |
+
Searches for the shortest action suffix `w_ret` within the remaining horizon such that:
|
| 418 |
+
1. `prefix + w_ret` is hazard-free under every surviving model in `survivors`.
|
| 419 |
+
2. `prefix + w_ret` returns the reference state machine to `space.machine.initial` across
|
| 420 |
+
every surviving model (certified soft return-to-home).
|
| 421 |
+
If no return-to-initial path exists within the horizon, falls back to the shortest
|
| 422 |
+
universally hazard-free holding step if one exists.
|
| 423 |
+
``max_length`` is an optional nonnegative integer that can only narrow the
|
| 424 |
+
remaining contract horizon; a prefix beyond that horizon is invalid.
|
| 425 |
+
"""
|
| 426 |
+
survivors = space._ids(survivors)
|
| 427 |
+
prefix = _word(prefix, space.machine.inputs)
|
| 428 |
+
if max_length is not None:
|
| 429 |
+
_integer(max_length, 'max_length')
|
| 430 |
+
if len(prefix) > space.contract.horizon:
|
| 431 |
+
raise ValueError('prefix exceeds the contract horizon')
|
| 432 |
+
if not survivors:
|
| 433 |
+
return {
|
| 434 |
+
'certified': False, 'return_to_initial': False, 'word': None,
|
| 435 |
+
'reason': 'empty_belief', 'candidate_count': 0,
|
| 436 |
+
}
|
| 437 |
+
if prefix and not space.probe_is_safe(survivors, prefix):
|
| 438 |
+
return {
|
| 439 |
+
'certified': False, 'return_to_initial': False, 'word': None,
|
| 440 |
+
'reason': 'prefix_already_hazardous', 'candidate_count': len(survivors),
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
def _end_state(input_map, full_word):
|
| 444 |
+
state = space.machine.initial
|
| 445 |
+
for sym in full_word:
|
| 446 |
+
state, out = space.machine.transitions[state][input_map[sym]]
|
| 447 |
+
if out == space.contract.hazard:
|
| 448 |
+
return None
|
| 449 |
+
return state
|
| 450 |
+
|
| 451 |
+
if hasattr(space, 'decompose_id'):
|
| 452 |
+
active_maps = [space.input_perms[i_idx] for i_idx in sorted({space.decompose_id(i)[0] for i in survivors})]
|
| 453 |
+
else:
|
| 454 |
+
seen_maps = []
|
| 455 |
+
for i in survivors:
|
| 456 |
+
im = space.adapters[i].input_map
|
| 457 |
+
if im not in seen_maps:
|
| 458 |
+
seen_maps.append(im)
|
| 459 |
+
active_maps = seen_maps
|
| 460 |
+
|
| 461 |
+
if all(_end_state(im, prefix) == space.machine.initial for im in active_maps):
|
| 462 |
+
return {
|
| 463 |
+
'certified': True, 'return_to_initial': True, 'word': [],
|
| 464 |
+
'reason': 'already_at_initial', 'candidate_count': len(survivors),
|
| 465 |
+
}
|
| 466 |
+
|
| 467 |
+
rem = space.contract.horizon - len(prefix)
|
| 468 |
+
if max_length is not None:
|
| 469 |
+
rem = min(rem, max_length)
|
| 470 |
+
if rem <= 0:
|
| 471 |
+
return {
|
| 472 |
+
'certified': False, 'return_to_initial': False, 'word': None,
|
| 473 |
+
'reason': 'horizon_exhausted', 'candidate_count': len(survivors),
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
for length in range(1, rem + 1):
|
| 477 |
+
for cand in product(range(space.machine.inputs), repeat=length):
|
| 478 |
+
full = prefix + cand
|
| 479 |
+
if all(_end_state(im, full) == space.machine.initial for im in active_maps):
|
| 480 |
+
return {
|
| 481 |
+
'certified': True, 'return_to_initial': True, 'word': list(cand),
|
| 482 |
+
'reason': 'certified_return_to_initial', 'candidate_count': len(survivors),
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
for action in range(space.machine.inputs):
|
| 486 |
+
full = prefix + (action,)
|
| 487 |
+
if all(_end_state(im, full) is not None for im in active_maps):
|
| 488 |
+
return {
|
| 489 |
+
'certified': True, 'return_to_initial': False, 'word': [action],
|
| 490 |
+
'reason': 'certified_safe_hold', 'candidate_count': len(survivors),
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
return {
|
| 494 |
+
'certified': False, 'return_to_initial': False, 'word': None,
|
| 495 |
+
'reason': 'no_safe_continuation', 'candidate_count': len(survivors),
|
| 496 |
+
}
|
crosswire/study.py
ADDED
|
@@ -0,0 +1,702 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Public-only acquisition followed by frozen decisions and private auditing."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
from collections import defaultdict
|
| 5 |
+
from contextlib import ExitStack
|
| 6 |
+
from dataclasses import asdict, dataclass, fields
|
| 7 |
+
from itertools import product
|
| 8 |
+
import math
|
| 9 |
+
import random
|
| 10 |
+
import threading
|
| 11 |
+
import time
|
| 12 |
+
import tracemalloc
|
| 13 |
+
from typing import Callable
|
| 14 |
+
|
| 15 |
+
from .core import CandidateSpace, Contract, Machine
|
| 16 |
+
from .fixtures import catalog, make_case, oracle_plan
|
| 17 |
+
from .interfaces import InterfaceError, TargetProcess
|
| 18 |
+
from .identity import canonical_hash, scientific_view, source_hash
|
| 19 |
+
from .conditions import condition_key, condition_fields, normalize_fault_schedule
|
| 20 |
+
from .backends import CANDIDATE_BACKENDS, make_space, backend_summary
|
| 21 |
+
|
| 22 |
+
POLICIES = ("fixed", "random", "disagreement", "task", "aalpy", "counterexample", "entropy", "ec2", "branching", "lookahead", "voi")
|
| 23 |
+
ADMISSIONS = ("robust", "adaptive", "one_best", "unique", "fit", "identity", "abstain", "oracle", "risk_controlled")
|
| 24 |
+
EXECUTION_MODES = ("open_loop", "shielded", "recovery", "replan")
|
| 25 |
+
RECOVERY_STRATEGIES = ("stabilize", "soft_return")
|
| 26 |
+
PROBE_SAFETY_MODES = ('unrestricted', 'model_safe')
|
| 27 |
+
_RUN_LOCK = threading.RLock()
|
| 28 |
+
_PROCESS_SOURCE_HASH = source_hash()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class Cancelled(RuntimeError):
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@dataclass(frozen=True)
|
| 36 |
+
class RunConfig:
|
| 37 |
+
family: str = "latch"
|
| 38 |
+
seed: int = 17
|
| 39 |
+
implementation: str = "procedural"
|
| 40 |
+
split: str = "development"
|
| 41 |
+
anchors: bool = False
|
| 42 |
+
budget: int = 16
|
| 43 |
+
horizon: int = 4
|
| 44 |
+
policy: str = "task"
|
| 45 |
+
admission: str = "robust"
|
| 46 |
+
execution_mode: str = "open_loop"
|
| 47 |
+
temporal: bool = False
|
| 48 |
+
escalate: bool = False
|
| 49 |
+
probe_safety: str = 'unrestricted'
|
| 50 |
+
risk_tolerance: float = 0.0
|
| 51 |
+
instance_seed: int | None = None
|
| 52 |
+
fault_schedule: dict | None = None
|
| 53 |
+
candidate_backend: str = 'flat'
|
| 54 |
+
recovery_strategy: str = 'stabilize'
|
| 55 |
+
|
| 56 |
+
def __post_init__(self):
|
| 57 |
+
if self.family == 'generated_protocol' and self.instance_seed is None:
|
| 58 |
+
object.__setattr__(self, 'instance_seed', 0)
|
| 59 |
+
|
| 60 |
+
@classmethod
|
| 61 |
+
def from_dict(cls, values: dict) -> "RunConfig":
|
| 62 |
+
if not isinstance(values, dict) or set(values) - {f.name for f in fields(cls)}:
|
| 63 |
+
raise ValueError("Unknown run fields or non-object configuration")
|
| 64 |
+
instance = cls(**values)
|
| 65 |
+
instance.check()
|
| 66 |
+
object.__setattr__(instance, 'fault_schedule', normalize_fault_schedule(instance.fault_schedule))
|
| 67 |
+
return instance
|
| 68 |
+
|
| 69 |
+
def check(self) -> None:
|
| 70 |
+
for key, low, high in (("budget", 0, 512), ("horizon", 1, 6), ("seed", 0, 2**32 - 1)):
|
| 71 |
+
value = getattr(self, key)
|
| 72 |
+
if type(value) is not int or not low <= value <= high:
|
| 73 |
+
raise ValueError(f"{key} must be an integer in [{low}, {high}]")
|
| 74 |
+
if self.instance_seed is not None and (
|
| 75 |
+
type(self.instance_seed) is not int or not 0 <= self.instance_seed <= 2**32 - 1):
|
| 76 |
+
raise ValueError('instance_seed must be an unsigned 32-bit integer or null')
|
| 77 |
+
if self.instance_seed is not None and self.family != 'generated_protocol':
|
| 78 |
+
raise ValueError('instance_seed is supported only by generated_protocol')
|
| 79 |
+
if type(self.anchors) is not bool:
|
| 80 |
+
raise ValueError("anchors must be a boolean")
|
| 81 |
+
if type(self.temporal) is not bool:
|
| 82 |
+
raise ValueError("temporal must be a boolean")
|
| 83 |
+
if type(self.escalate) is not bool:
|
| 84 |
+
raise ValueError("escalate must be a boolean")
|
| 85 |
+
if self.probe_safety not in PROBE_SAFETY_MODES:
|
| 86 |
+
raise ValueError('Unknown probe_safety mode')
|
| 87 |
+
if (type(self.risk_tolerance) not in (int, float) or not 0 <= self.risk_tolerance < 1
|
| 88 |
+
or not math.isfinite(self.risk_tolerance)):
|
| 89 |
+
raise ValueError('risk_tolerance must be a finite number in [0, 1)')
|
| 90 |
+
if self.risk_tolerance and self.admission != 'risk_controlled':
|
| 91 |
+
raise ValueError('Positive risk_tolerance requires risk_controlled admission')
|
| 92 |
+
if self.policy == 'aalpy' and self.probe_safety != 'unrestricted':
|
| 93 |
+
raise ValueError('The AALpy baseline does not support model_safe calibration')
|
| 94 |
+
if self.escalate and self.temporal:
|
| 95 |
+
raise ValueError("escalate cannot be combined with temporal=True (starts at static and escalates)")
|
| 96 |
+
if self.execution_mode not in EXECUTION_MODES:
|
| 97 |
+
raise ValueError("Unknown execution mode")
|
| 98 |
+
if self.family not in {f["id"] for f in catalog()}:
|
| 99 |
+
raise ValueError("Unknown fixture family")
|
| 100 |
+
if self.implementation not in ("table", "procedural") or self.split not in ("development", "evaluation"):
|
| 101 |
+
raise ValueError("Unknown implementation or split")
|
| 102 |
+
if self.policy not in POLICIES or self.admission not in ADMISSIONS:
|
| 103 |
+
raise ValueError("Unknown probe/admission policy")
|
| 104 |
+
if self.candidate_backend not in CANDIDATE_BACKENDS:
|
| 105 |
+
raise ValueError('Unknown candidate_backend')
|
| 106 |
+
if self.recovery_strategy not in RECOVERY_STRATEGIES:
|
| 107 |
+
raise ValueError('Unknown recovery strategy')
|
| 108 |
+
if self.admission == 'adaptive' and self.execution_mode not in ('shielded', 'recovery'):
|
| 109 |
+
raise ValueError('Adaptive admission requires shielded or recovery execution')
|
| 110 |
+
if self.policy == 'aalpy' and self.temporal:
|
| 111 |
+
raise ValueError('The AALpy baseline does not support temporal adapters')
|
| 112 |
+
if self.policy == 'aalpy' and self.admission == 'adaptive':
|
| 113 |
+
raise ValueError('The AALpy baseline does not support adaptive admission')
|
| 114 |
+
if self.policy == 'aalpy' and self.escalate:
|
| 115 |
+
raise ValueError('The AALpy baseline does not support model escalation')
|
| 116 |
+
normalize_fault_schedule(self.fault_schedule)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def _check_cancel(cancel: Callable[[], bool] | None) -> None:
|
| 120 |
+
if cancel and cancel():
|
| 121 |
+
raise Cancelled("Run cancelled before scientific completion")
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def fit_evidence(probes: list[dict]) -> list[dict]:
|
| 125 |
+
evidence = []
|
| 126 |
+
for item in probes:
|
| 127 |
+
if item['complete']:
|
| 128 |
+
evidence.append(item)
|
| 129 |
+
elif item.get('outputs'):
|
| 130 |
+
evidence.append({'word': item['word'][:len(item['outputs'])],
|
| 131 |
+
'outputs': item['outputs'], 'complete': True})
|
| 132 |
+
return evidence
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def fit_diagnostics(space: CandidateSpace, evidence: list[dict], anchors: dict) -> dict:
|
| 136 |
+
"""Residual of the forced best-fit map, split into raw-output and anchor terms.
|
| 137 |
+
|
| 138 |
+
Uses the same deterministic tie-break as CandidateSpace.plan('fit'), so a
|
| 139 |
+
nonzero residual distinguishes forced fitting from consistent survivors.
|
| 140 |
+
"""
|
| 141 |
+
losses = space.losses(evidence, anchors)
|
| 142 |
+
chosen = min(losses, key=lambda i: (losses[i], i))
|
| 143 |
+
raw = sum(p != o for item in evidence
|
| 144 |
+
for p, o in zip(space.predict(chosen, tuple(item["word"])), item["outputs"]))
|
| 145 |
+
tied = sum(1 for value in losses.values() if value == losses[chosen])
|
| 146 |
+
return {"chosen_adapter_id": chosen, "chosen_adapter": space.adapters[chosen].to_dict(),
|
| 147 |
+
"raw_mismatches": raw, "anchor_mismatches": losses[chosen] - raw,
|
| 148 |
+
"total_residual": losses[chosen], "zero_residual": losses[chosen] == 0,
|
| 149 |
+
"maps_tied_at_minimum": tied,
|
| 150 |
+
"raw_symbols_compared": sum(len(item["outputs"]) for item in evidence)}
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def _acquire(public: dict, policy: str, budget: int, query: Callable, counts: dict,
|
| 154 |
+
cancel: Callable[[], bool] | None, progress: Callable | None, temporal: bool = False,
|
| 155 |
+
admission: str = 'robust', escalate: bool = False, probe_safety: str = 'unrestricted',
|
| 156 |
+
step: Callable | None = None, reset: Callable | None = None, candidate_backend: str = 'flat'):
|
| 157 |
+
"""This function receives no Case, private map, target states or family label."""
|
| 158 |
+
space = make_space(Machine.from_dict(public["reference"]), Contract.from_dict(public["contract"]), candidate_backend, temporal)
|
| 159 |
+
anchors = {int(k): int(v) for k, v in public.get("anchors", {}).items()}
|
| 160 |
+
probes: list[dict] = []
|
| 161 |
+
survivors = space.survivors(probes, anchors)
|
| 162 |
+
seen: set[tuple] = set()
|
| 163 |
+
error = None
|
| 164 |
+
escalated = False
|
| 165 |
+
escalation_history = []
|
| 166 |
+
safe_mode = probe_safety == 'model_safe'
|
| 167 |
+
# An enabled larger class must be guarded before it becomes necessary for
|
| 168 |
+
# fitting. Static predictions alone cannot protect a delayed target.
|
| 169 |
+
safety_space = (make_space(space.machine, space.contract, candidate_backend, temporal=True)
|
| 170 |
+
if safe_mode and escalate else space)
|
| 171 |
+
stop_reason = 'CALL_CAP'
|
| 172 |
+
# `budget` is the shared system-call cap: every reset and every action
|
| 173 |
+
# attempt counts, so all methods compare at the same total target access.
|
| 174 |
+
while counts["target_calls"] < budget:
|
| 175 |
+
_check_cancel(cancel)
|
| 176 |
+
if admission == 'adaptive':
|
| 177 |
+
from .adaptive import synthesize_policy
|
| 178 |
+
ready = synthesize_policy(space, survivors, cancel=cancel)['admitted']
|
| 179 |
+
else:
|
| 180 |
+
ready = space.plan(survivors, "robust")["admitted"]
|
| 181 |
+
if ready:
|
| 182 |
+
stop_reason = 'CONTRACT_READY'
|
| 183 |
+
break
|
| 184 |
+
remaining_actions = budget - counts["target_calls"] - 1 # one reset per query
|
| 185 |
+
if remaining_actions < 1:
|
| 186 |
+
break
|
| 187 |
+
selection_space = safety_space if safe_mode else space
|
| 188 |
+
selection_ids = (selection_space.survivors(fit_evidence(probes), anchors)
|
| 189 |
+
if selection_space is not space else survivors)
|
| 190 |
+
before = len(survivors)
|
| 191 |
+
|
| 192 |
+
if policy == 'branching':
|
| 193 |
+
# Interactive within-query branching calibration:
|
| 194 |
+
# Observes intermediate outputs and selects each subsequent action conditionally.
|
| 195 |
+
word = []
|
| 196 |
+
outputs = []
|
| 197 |
+
complete = False
|
| 198 |
+
try:
|
| 199 |
+
if reset is not None:
|
| 200 |
+
reset()
|
| 201 |
+
elif hasattr(query, '__self__') and hasattr(query.__self__, 'reset'):
|
| 202 |
+
query.__self__.reset()
|
| 203 |
+
except InterfaceError as exc:
|
| 204 |
+
error = {"code": exc.code, "message": str(exc)}
|
| 205 |
+
stop_reason = 'INTERFACE_ERROR'
|
| 206 |
+
break
|
| 207 |
+
|
| 208 |
+
current_ids = list(selection_ids)
|
| 209 |
+
max_query_depth = min(4, remaining_actions)
|
| 210 |
+
|
| 211 |
+
while len(word) < max_query_depth and counts["target_calls"] < budget:
|
| 212 |
+
_check_cancel(cancel)
|
| 213 |
+
if len(current_ids) <= 1:
|
| 214 |
+
break
|
| 215 |
+
|
| 216 |
+
if budget - counts["target_calls"] < 1:
|
| 217 |
+
break
|
| 218 |
+
|
| 219 |
+
next_action = selection_space.choose_branching_step(
|
| 220 |
+
current_ids, tuple(word), safe_only=safe_mode, safe_ids=selection_ids,
|
| 221 |
+
seed=92821 + len(probes) + len(word)
|
| 222 |
+
)
|
| 223 |
+
if next_action is None:
|
| 224 |
+
break
|
| 225 |
+
|
| 226 |
+
try:
|
| 227 |
+
if step is not None:
|
| 228 |
+
out = step(next_action)
|
| 229 |
+
elif hasattr(query, '__self__') and hasattr(query.__self__, 'step'):
|
| 230 |
+
out = query.__self__.step(next_action)
|
| 231 |
+
else:
|
| 232 |
+
out = query([next_action])[-1]
|
| 233 |
+
word.append(next_action)
|
| 234 |
+
outputs.append(out)
|
| 235 |
+
except InterfaceError as exc:
|
| 236 |
+
error = {"code": exc.code, "message": str(exc)}
|
| 237 |
+
break
|
| 238 |
+
|
| 239 |
+
current_ids = [
|
| 240 |
+
i for i in current_ids
|
| 241 |
+
if selection_space.predict(i, tuple(word)) == tuple(outputs)
|
| 242 |
+
]
|
| 243 |
+
if len(current_ids) <= 1:
|
| 244 |
+
break
|
| 245 |
+
|
| 246 |
+
complete = error is None
|
| 247 |
+
if word:
|
| 248 |
+
seen.add(tuple(word))
|
| 249 |
+
entry = {"word": list(word), "outputs": list(outputs), "complete": complete,
|
| 250 |
+
"survivors_before": before}
|
| 251 |
+
if safe_mode:
|
| 252 |
+
entry['safety'] = {'model_class': 'temporal' if selection_space.temporal else 'static',
|
| 253 |
+
'candidate_count': len(selection_ids), 'model_safe': True}
|
| 254 |
+
probes.append(entry)
|
| 255 |
+
survivors = space.survivors(fit_evidence(probes), anchors)
|
| 256 |
+
entry["survivors_after"] = len(survivors)
|
| 257 |
+
else:
|
| 258 |
+
stop_reason = 'NO_ELIGIBLE_SAFE_PROBE' if safe_mode else 'NO_INFORMATIVE_PROBE'
|
| 259 |
+
break
|
| 260 |
+
else:
|
| 261 |
+
planning = None
|
| 262 |
+
if policy == 'lookahead':
|
| 263 |
+
from .experiments import synthesize_experiment
|
| 264 |
+
from .experiment_checks import verify_experiment_tree
|
| 265 |
+
planning = synthesize_experiment(selection_space, selection_ids,
|
| 266 |
+
budget - counts['target_calls'], seen=seen,
|
| 267 |
+
safe_only=safe_mode, cancel=cancel)
|
| 268 |
+
word = tuple(planning['word']) if planning['word'] is not None else None
|
| 269 |
+
if word is not None:
|
| 270 |
+
checked = verify_experiment_tree(public['reference'], public['contract'], planning['tree'],
|
| 271 |
+
temporal=selection_space.temporal,
|
| 272 |
+
evidence=fit_evidence(probes), anchors=anchors,
|
| 273 |
+
budget=planning['remaining_calls'], safe_only=safe_mode)
|
| 274 |
+
if not checked['valid']:
|
| 275 |
+
raise RuntimeError('Independent experiment check failed: ' + checked['reason'])
|
| 276 |
+
for key, value in planning['objective'].items():
|
| 277 |
+
if checked[key] != value:
|
| 278 |
+
raise RuntimeError('Independent experiment objective differs from synthesis')
|
| 279 |
+
planning.update(certificate=checked, model_class='temporal' if selection_space.temporal else 'static',
|
| 280 |
+
safe_only=safe_mode)
|
| 281 |
+
else:
|
| 282 |
+
word = selection_space.choose_probe(selection_ids, policy, remaining_actions, seen,
|
| 283 |
+
seed=92821 + len(probes), safe_only=safe_mode)
|
| 284 |
+
if word is None:
|
| 285 |
+
stop_reason = 'NO_ELIGIBLE_SAFE_PROBE' if safe_mode else 'NO_INFORMATIVE_PROBE'
|
| 286 |
+
break
|
| 287 |
+
seen.add(word)
|
| 288 |
+
outputs = []
|
| 289 |
+
complete = False
|
| 290 |
+
try:
|
| 291 |
+
outputs = query(word)
|
| 292 |
+
complete = True
|
| 293 |
+
except InterfaceError as exc:
|
| 294 |
+
error = {"code": exc.code, "message": str(exc)}
|
| 295 |
+
outputs = getattr(exc, 'trace', {}).get('outputs', [])
|
| 296 |
+
entry = {"word": list(word), "outputs": list(outputs), "complete": complete,
|
| 297 |
+
"survivors_before": before}
|
| 298 |
+
if planning is not None:
|
| 299 |
+
entry['planning'] = planning
|
| 300 |
+
if safe_mode:
|
| 301 |
+
entry['safety'] = {'model_class': 'temporal' if selection_space.temporal else 'static',
|
| 302 |
+
'candidate_count': len(selection_ids), 'model_safe': True}
|
| 303 |
+
probes.append(entry)
|
| 304 |
+
survivors = space.survivors(fit_evidence(probes), anchors)
|
| 305 |
+
entry["survivors_after"] = len(survivors)
|
| 306 |
+
if progress:
|
| 307 |
+
progress(counts["target_calls"], budget, f"{len(survivors)} mappings remain")
|
| 308 |
+
if error:
|
| 309 |
+
stop_reason = 'INTERFACE_ERROR'
|
| 310 |
+
break
|
| 311 |
+
if not survivors:
|
| 312 |
+
if escalate and not space.temporal:
|
| 313 |
+
# Autonomous Model-Class Escalation: static refuted -> escalate to temporal
|
| 314 |
+
previous_work = dict(space.work)
|
| 315 |
+
space = make_space(space.machine, space.contract, candidate_backend, temporal=True)
|
| 316 |
+
# Rebuilding a larger hypothesis class does not erase the work
|
| 317 |
+
# already spent refuting the smaller one.
|
| 318 |
+
space.work.update(previous_work)
|
| 319 |
+
survivors = space.survivors(fit_evidence(probes), anchors)
|
| 320 |
+
entry["survivors_after"] = len(survivors)
|
| 321 |
+
escalated = True
|
| 322 |
+
escalation_history.append({"step": len(probes), "from": "static", "to": "temporal",
|
| 323 |
+
"work_before_escalation": previous_work,
|
| 324 |
+
"survivors_after_escalation": len(survivors)})
|
| 325 |
+
if progress:
|
| 326 |
+
progress(counts["target_calls"], budget, f"Escalated to temporal model class: {len(survivors)} mappings")
|
| 327 |
+
if not survivors:
|
| 328 |
+
stop_reason = 'MODEL_CONTRADICTED'
|
| 329 |
+
break
|
| 330 |
+
else:
|
| 331 |
+
stop_reason = 'MODEL_CONTRADICTED'
|
| 332 |
+
break
|
| 333 |
+
if safety_space is not space and safe_mode:
|
| 334 |
+
# Count the independent envelope selection work as well as fitting and
|
| 335 |
+
# any discarded static search work. No target calls are hidden here.
|
| 336 |
+
for key, value in safety_space.work.items():
|
| 337 |
+
space.work[key] = space.work.get(key, 0) + value
|
| 338 |
+
escalation_info = {
|
| 339 |
+
"enabled": escalate,
|
| 340 |
+
"escalated": escalated,
|
| 341 |
+
"escalations": len(escalation_history),
|
| 342 |
+
"history": escalation_history,
|
| 343 |
+
"model_class": "temporal" if space.temporal else "static",
|
| 344 |
+
"structural_complexity": 1 if space.temporal else 0,
|
| 345 |
+
'calibration': {'mode': probe_safety, 'stop_reason': stop_reason,
|
| 346 |
+
'safety_model_class': 'temporal' if (temporal or (safe_mode and escalate)) else 'static',
|
| 347 |
+
'scope': 'Bounded probe search within the remaining call cap; model-safe means hazard-free '
|
| 348 |
+
'under every evidence-compatible enabled model, conditional on truthful semantics.'},
|
| 349 |
+
}
|
| 350 |
+
return space, survivors, probes, error, escalation_info
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
def run(config: RunConfig, cancel: Callable[[], bool] | None = None, progress: Callable | None = None) -> dict:
|
| 354 |
+
"""A single serialized experiment; targets are always closed on cancellation."""
|
| 355 |
+
config = RunConfig.from_dict(asdict(config))
|
| 356 |
+
with _RUN_LOCK:
|
| 357 |
+
return _run(config, cancel, progress)
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
def _run(config: RunConfig, cancel: Callable[[], bool] | None, progress: Callable | None) -> dict:
|
| 361 |
+
started, cpu = time.perf_counter(), time.process_time()
|
| 362 |
+
frozen_source = source_hash()
|
| 363 |
+
if frozen_source != _PROCESS_SOURCE_HASH:
|
| 364 |
+
raise RuntimeError('Source changed since runtime import; restart CROSSWIRE before creating evidence')
|
| 365 |
+
_check_cancel(cancel)
|
| 366 |
+
case = make_case(config.family, config.seed, config.implementation, config.split, config.anchors, config.horizon,
|
| 367 |
+
instance_seed=config.instance_seed, fault_schedule=config.fault_schedule)
|
| 368 |
+
public = case.public
|
| 369 |
+
counts = dict(reset_attempts=0, step_attempts=0, reset_successes=0, step_successes=0, target_calls=0)
|
| 370 |
+
baseline = {"name": config.policy, "privileged": config.admission == "oracle"}
|
| 371 |
+
resources = ExitStack()
|
| 372 |
+
target = None
|
| 373 |
+
if not tracemalloc.is_tracing():
|
| 374 |
+
tracemalloc.start()
|
| 375 |
+
tracemalloc.reset_peak()
|
| 376 |
+
try:
|
| 377 |
+
temporal = config.temporal
|
| 378 |
+
if config.policy == "aalpy" and config.admission != "oracle":
|
| 379 |
+
from .baselines import acquire_aalpy
|
| 380 |
+
target = resources.enter_context(TargetProcess(case.private))
|
| 381 |
+
space, survivors, probes, error, detail = acquire_aalpy(public, config.budget, target, cancel,
|
| 382 |
+
candidate_backend=config.candidate_backend)
|
| 383 |
+
counts = dict(target.counts)
|
| 384 |
+
baseline.update(detail)
|
| 385 |
+
escalation_info = {
|
| 386 |
+
"enabled": False, "escalated": False, "escalations": 0, "history": [],
|
| 387 |
+
"model_class": "static", "structural_complexity": 0,
|
| 388 |
+
}
|
| 389 |
+
elif config.budget and config.admission != "oracle":
|
| 390 |
+
target = resources.enter_context(TargetProcess(case.private))
|
| 391 |
+
space, survivors, probes, error, escalation_info = _acquire(
|
| 392 |
+
public, config.policy, config.budget, target.query, target.counts, cancel, progress,
|
| 393 |
+
temporal=temporal, admission=config.admission, escalate=config.escalate,
|
| 394 |
+
probe_safety=config.probe_safety,
|
| 395 |
+
step=target.step, reset=target.reset, candidate_backend=config.candidate_backend
|
| 396 |
+
)
|
| 397 |
+
counts = dict(target.counts)
|
| 398 |
+
else:
|
| 399 |
+
space = make_space(Machine.from_dict(public["reference"]), Contract.from_dict(public["contract"]),
|
| 400 |
+
config.candidate_backend, temporal=temporal)
|
| 401 |
+
survivors = space.survivors([], {int(k): int(v) for k, v in public.get("anchors", {}).items()})
|
| 402 |
+
probes, error = [], None
|
| 403 |
+
escalation_info = {
|
| 404 |
+
"enabled": config.escalate, "escalated": False, "escalations": 0, "history": [],
|
| 405 |
+
"model_class": "temporal" if space.temporal else "static",
|
| 406 |
+
"structural_complexity": 1 if space.temporal else 0,
|
| 407 |
+
}
|
| 408 |
+
_check_cancel(cancel)
|
| 409 |
+
if config.admission == "oracle":
|
| 410 |
+
word = oracle_plan(case.private, config.horizon)
|
| 411 |
+
decision = {"status": "ORACLE_DIAGNOSTIC", "admitted": word is not None,
|
| 412 |
+
"word": word, "candidate_count": len(survivors)}
|
| 413 |
+
elif config.admission == 'adaptive':
|
| 414 |
+
from .adaptive import synthesize_policy
|
| 415 |
+
decision = synthesize_policy(space, survivors, cancel=cancel)
|
| 416 |
+
else:
|
| 417 |
+
anchors = {int(k): int(v) for k, v in public.get("anchors", {}).items()}
|
| 418 |
+
decision = space.plan(survivors, config.admission, transcripts=fit_evidence(probes),
|
| 419 |
+
anchors=anchors, risk_tolerance=config.risk_tolerance)
|
| 420 |
+
if config.admission == "fit":
|
| 421 |
+
baseline["fit_diagnostics"] = fit_diagnostics(space, fit_evidence(probes), anchors)
|
| 422 |
+
if error:
|
| 423 |
+
decision = {"status": "INTERFACE_ERROR", "admitted": False, "word": None,
|
| 424 |
+
"candidate_count": len(survivors), "error": error}
|
| 425 |
+
elif config.policy == 'aalpy' and not baseline.get('model_complete') and config.admission != 'oracle':
|
| 426 |
+
decision = {'status': 'UNRESOLVED', 'admitted': False, 'word': None,
|
| 427 |
+
'candidate_count': 0, 'reason': 'Budget exhausted before a complete classical model'}
|
| 428 |
+
decision['calibration'] = escalation_info.pop('calibration', {
|
| 429 |
+
'mode': config.probe_safety,
|
| 430 |
+
'stop_reason': 'CLASSICAL_BASELINE' if config.policy == 'aalpy' else 'NOT_REQUESTED',
|
| 431 |
+
'safety_model_class': 'temporal' if (config.temporal or config.escalate) else 'static',
|
| 432 |
+
'scope': 'Calibration safety is conditional on the declared finite enabled model and truthful anchors.'})
|
| 433 |
+
decision["escalation"] = escalation_info
|
| 434 |
+
if decision["status"] == "FAMILY_CONTRADICTED" and config.escalate:
|
| 435 |
+
static_count = len(space.adapters) // (1 + space.machine.outputs) if space.temporal else len(space.adapters)
|
| 436 |
+
decision["structural_contradiction"] = {
|
| 437 |
+
"classes_refuted": [f"static ({static_count} maps)"] +
|
| 438 |
+
([f"temporal ({len(space.adapters)} latency maps)"] if space.temporal else []),
|
| 439 |
+
"evidence_length": len(probes),
|
| 440 |
+
"scope": "No adapter in the checked finite hypothesis class can explain the public evidence."
|
| 441 |
+
}
|
| 442 |
+
from .certificates import check_result
|
| 443 |
+
candidates = [space.adapters[identifier].to_dict() for identifier in survivors]
|
| 444 |
+
evidence_certificate = check_result({
|
| 445 |
+
'schema': 'crosswire-result-v1', 'configuration': asdict(config),
|
| 446 |
+
'reference': public['reference'], 'contract': public['contract'],
|
| 447 |
+
'anchors': public.get('anchors', {}), 'target_id': public['target_id'],
|
| 448 |
+
'decision': decision, 'candidates': candidates, 'probes': probes,
|
| 449 |
+
})
|
| 450 |
+
if not evidence_certificate['valid']:
|
| 451 |
+
raise RuntimeError('Independent public-evidence check failed: ' + evidence_certificate['reason'])
|
| 452 |
+
ambiguity = space.ambiguity(survivors)
|
| 453 |
+
backend = backend_summary(space, survivors)
|
| 454 |
+
backend['measurement_stage'] = 'after_public_check_before_deployment_and_private_audit'
|
| 455 |
+
# The digest is constructed before importing/calling the private auditor.
|
| 456 |
+
frozen = canonical_hash({"decision": decision, "probes": probes, "reference": public["reference"],
|
| 457 |
+
"contract": public["contract"], "target_id": public['target_id'],
|
| 458 |
+
'anchors': public.get('anchors', {}), "survivors": list(survivors),
|
| 459 |
+
'evidence_certificate': evidence_certificate})
|
| 460 |
+
deployment = {'word': decision['word'], 'outputs': [], 'complete': False,
|
| 461 |
+
'mode': config.execution_mode,
|
| 462 |
+
'error': None, 'target_steps': 0, 'resets': 0, 'target_calls': 0}
|
| 463 |
+
if decision['admitted']:
|
| 464 |
+
_check_cancel(cancel)
|
| 465 |
+
if target is None:
|
| 466 |
+
target = resources.enter_context(TargetProcess(case.private))
|
| 467 |
+
if config.admission == 'adaptive':
|
| 468 |
+
from .adaptive import execute_policy
|
| 469 |
+
deployment = execute_policy(target, decision['policy'], space, survivors, cancel,
|
| 470 |
+
fault_schedule=config.fault_schedule,
|
| 471 |
+
mode='recovery' if config.execution_mode == 'recovery' else 'adaptive', recovery_strategy=config.recovery_strategy)
|
| 472 |
+
else:
|
| 473 |
+
from .shield import execute_deployment
|
| 474 |
+
deployment = execute_deployment(target, decision['word'], space, survivors, space.contract,
|
| 475 |
+
mode=config.execution_mode, cancel=cancel,
|
| 476 |
+
fault_schedule=config.fault_schedule, recovery_strategy=config.recovery_strategy)
|
| 477 |
+
from .audit import evaluate
|
| 478 |
+
audit = evaluate(case, space, survivors, decision, probes, deployment)
|
| 479 |
+
peak = tracemalloc.get_traced_memory()[1]
|
| 480 |
+
work = dict(space.work)
|
| 481 |
+
result = {
|
| 482 |
+
"schema": "crosswire-result-v1", "configuration": asdict(config),
|
| 483 |
+
'backend': backend,
|
| 484 |
+
'source_hash': frozen_source,
|
| 485 |
+
"reference": public["reference"], "contract": public["contract"],
|
| 486 |
+
"target_id": public["target_id"], "parent_instance_id": public["parent_instance_id"],
|
| 487 |
+
"anchors": public.get("anchors", {}), "decision": decision, "ambiguity": ambiguity,
|
| 488 |
+
"candidates": candidates, "evidence_certificate": evidence_certificate,
|
| 489 |
+
"probes": probes, "decision_frozen_hash": frozen, 'deployment': deployment, "audit": audit,
|
| 490 |
+
"metrics": {"target_steps": counts["step_attempts"], "resets": counts["reset_attempts"],
|
| 491 |
+
"target_calls": counts["target_calls"], **work,
|
| 492 |
+
'anchor_facts_supplied': len(public.get('anchors', {})),
|
| 493 |
+
"escalations": escalation_info["escalations"],
|
| 494 |
+
"escalation_level": escalation_info["structural_complexity"],
|
| 495 |
+
"parent_cpu_seconds": time.process_time() - cpu,
|
| 496 |
+
"wall_seconds": time.perf_counter() - started, "parent_peak_traced_bytes": peak,
|
| 497 |
+
"audit_product_pairs": audit["audit_product_pairs"],
|
| 498 |
+
"audit_execution_steps": audit["audit_execution_steps"]},
|
| 499 |
+
"baseline": baseline,
|
| 500 |
+
}
|
| 501 |
+
if 'instance_design' in public:
|
| 502 |
+
result['instance_design'] = public['instance_design']
|
| 503 |
+
if source_hash() != frozen_source:
|
| 504 |
+
raise RuntimeError('Source changed during run; discard mixed-revision evidence')
|
| 505 |
+
result["scientific_hash"] = canonical_hash(scientific_view(result))
|
| 506 |
+
return result
|
| 507 |
+
finally:
|
| 508 |
+
resources.close()
|
| 509 |
+
tracemalloc.stop()
|
| 510 |
+
|
| 511 |
+
|
| 512 |
+
def _condition(configuration: dict) -> tuple:
|
| 513 |
+
"""Comparable scientific conditions, with defaults for legacy row inputs."""
|
| 514 |
+
return condition_key(configuration)
|
| 515 |
+
|
| 516 |
+
|
| 517 |
+
def summarize(rows: list[dict]) -> list[dict]:
|
| 518 |
+
groups = defaultdict(list)
|
| 519 |
+
scopes = {f["id"]: f["scope"] for f in catalog()}
|
| 520 |
+
for row in rows:
|
| 521 |
+
cfg = row["configuration"]
|
| 522 |
+
groups[(cfg["policy"] + "/" + cfg["admission"], cfg["family"], cfg["budget"],
|
| 523 |
+
_condition(cfg))].append(row)
|
| 524 |
+
result = []
|
| 525 |
+
for (method, family, budget, conditions), members in sorted(groups.items()):
|
| 526 |
+
n = len(members)
|
| 527 |
+
admitted = sum(r["decision"]["admitted"] for r in members)
|
| 528 |
+
success = sum(r["audit"]["contract_satisfied"] is True for r in members)
|
| 529 |
+
harm = sum(r["audit"]["contract_satisfied"] is False for r in members)
|
| 530 |
+
feasible = sum(r["audit"]["oracle_feasible"] for r in members)
|
| 531 |
+
abstained = n - admitted
|
| 532 |
+
result.append({
|
| 533 |
+
"method": method, "family": family, "scope": scopes[family], "budget": budget, "requests": n,
|
| 534 |
+
**condition_fields(conditions),
|
| 535 |
+
"admitted": admitted, "successful": success, "harmful": harm,
|
| 536 |
+
"hazardous": sum(r["audit"]["hazard"] is True for r in members),
|
| 537 |
+
"hazards_averted": sum(r["audit"].get("hazard_averted") is True for r in members),
|
| 538 |
+
"shield_interventions": sum(r["audit"].get("shield_intervened") is True for r in members),
|
| 539 |
+
"calibration_hazardous_probes": sum(r['audit']['calibration_hazardous_probes'] for r in members),
|
| 540 |
+
"calibration_hazardous_runs": sum(r['audit']['calibration_hazardous_probes'] > 0 for r in members),
|
| 541 |
+
"mean_target_calls": sum(r['metrics']['target_calls'] for r in members) / n,
|
| 542 |
+
"mean_deployment_calls": sum(r['deployment']['target_calls'] for r in members) / n,
|
| 543 |
+
"mean_total_calls": sum(r['metrics']['target_calls'] + r['deployment']['target_calls'] for r in members) / n,
|
| 544 |
+
"interface_errors": sum(r["decision"]["status"] == "INTERFACE_ERROR"
|
| 545 |
+
or bool((r.get("deployment") or {}).get("error")) for r in members),
|
| 546 |
+
"acquisition_interface_errors": sum(r["decision"]["status"] == "INTERFACE_ERROR" for r in members),
|
| 547 |
+
"deployment_interface_errors": sum(bool((r.get("deployment") or {}).get("error")) for r in members),
|
| 548 |
+
"escalated_runs": sum(r["metrics"].get("escalations", 0) > 0 for r in members),
|
| 549 |
+
"recovered_runs": sum((r.get("deployment") or {}).get("recovery", {}).get("succeeded") is True for r in members),
|
| 550 |
+
"abstained": abstained, "oracle_feasible": feasible,
|
| 551 |
+
"admission_coverage": admitted / n, "useful_coverage": success / n,
|
| 552 |
+
"feasible_useful_coverage": success / feasible if feasible else None,
|
| 553 |
+
"selective_harm": harm / admitted if admitted else None, "population_harm": harm / n,
|
| 554 |
+
"mean_target_steps": sum(r["metrics"]["target_steps"] for r in members) / n,
|
| 555 |
+
"mean_resets": sum(r["metrics"]["resets"] for r in members) / n,
|
| 556 |
+
"parent_clusters": len({r["parent_instance_id"] for r in members}),
|
| 557 |
+
"utility": {str(cost): (success - harm - cost * abstained) / n for cost in (0, 0.1, 0.5, 1)},
|
| 558 |
+
})
|
| 559 |
+
return result
|
| 560 |
+
|
| 561 |
+
|
| 562 |
+
def paired_comparisons(rows: list[dict]) -> list[dict]:
|
| 563 |
+
"""Exploratory cluster-paired coverage differences, no power claim.
|
| 564 |
+
|
| 565 |
+
Every comparison fixes the budget, execution settings, semantic grounding,
|
| 566 |
+
horizon and split. Label seeds and implementations are matched within each
|
| 567 |
+
semantic parent before whole-parent resampling. Duplicate cells are refused
|
| 568 |
+
instead of allowing their last value to silently determine the contrast.
|
| 569 |
+
Policy pairs also fix admission; admission contrasts use robust as reference.
|
| 570 |
+
"""
|
| 571 |
+
scopes = {f["id"]: f["scope"] for f in catalog()}
|
| 572 |
+
comparisons = {'policy': defaultdict(dict), 'admission': defaultdict(dict)}
|
| 573 |
+
seen = set()
|
| 574 |
+
for row in rows:
|
| 575 |
+
c = row["configuration"]
|
| 576 |
+
# Out-of-family/control cases are never pooled into primary in-family effect.
|
| 577 |
+
if scopes.get(c["family"]) != "in_family":
|
| 578 |
+
continue
|
| 579 |
+
conditions = _condition(c)
|
| 580 |
+
cell = (c['family'], c['seed'], c['implementation'], c.get('instance_seed'))
|
| 581 |
+
parent = row['parent_instance_id']
|
| 582 |
+
identity = (c['budget'], conditions, parent, cell, c['policy'], c['admission'])
|
| 583 |
+
if identity in seen:
|
| 584 |
+
raise ValueError('Duplicate matched analysis cell; each condition/method must occur once')
|
| 585 |
+
seen.add(identity)
|
| 586 |
+
outcome = int(row['audit']['contract_satisfied'] is True)
|
| 587 |
+
key = (c['budget'], conditions, c['admission'], parent, cell)
|
| 588 |
+
comparisons['policy'][key][c['policy']] = outcome
|
| 589 |
+
key = (c['budget'], conditions, c['policy'], parent, cell)
|
| 590 |
+
comparisons['admission'][key][c['admission']] = outcome
|
| 591 |
+
|
| 592 |
+
output = []
|
| 593 |
+
for axis, cells in comparisons.items():
|
| 594 |
+
strata = sorted({key[:3] for key in cells})
|
| 595 |
+
for budget, conditions, fixed in strata:
|
| 596 |
+
stratum = (budget, conditions, fixed)
|
| 597 |
+
methods = sorted({method for key, values in cells.items() if key[:3] == stratum for method in values})
|
| 598 |
+
if axis == 'policy':
|
| 599 |
+
# Preserve task-relative signs while making arbitrary selected
|
| 600 |
+
# policy pairs (e.g. entropy vs fixed) analyzable too.
|
| 601 |
+
ordered = (['task'] if 'task' in methods else []) + [m for m in methods if m != 'task']
|
| 602 |
+
contrasts = [(a, b) for i, a in enumerate(ordered) for b in ordered[i + 1:]]
|
| 603 |
+
else:
|
| 604 |
+
contrasts = [('robust', m) for m in methods if m != 'robust']
|
| 605 |
+
for reference, method in contrasts:
|
| 606 |
+
per_parent = defaultdict(list)
|
| 607 |
+
families = set()
|
| 608 |
+
for key, values in cells.items():
|
| 609 |
+
if key[:3] != stratum or reference not in values or method not in values:
|
| 610 |
+
continue
|
| 611 |
+
parent, cell = key[3:]
|
| 612 |
+
difference = values[reference] - values[method]
|
| 613 |
+
per_parent[parent].append(difference if axis == 'policy' else -difference)
|
| 614 |
+
families.add(cell[0])
|
| 615 |
+
if not per_parent:
|
| 616 |
+
continue
|
| 617 |
+
delta = [sum(v) / len(v) for _, v in sorted(per_parent.items())]
|
| 618 |
+
rng = random.Random(68219)
|
| 619 |
+
draws = sorted(sum(rng.choice(delta) for _ in delta) / len(delta) for _ in range(2000))
|
| 620 |
+
output.append({
|
| 621 |
+
'contrast': f'{reference} - {method}' if axis == 'policy' else f'{method} - robust',
|
| 622 |
+
'comparison_axis': axis,
|
| 623 |
+
'fixed_admission' if axis == 'policy' else 'fixed_policy': fixed,
|
| 624 |
+
'outcome': 'useful_coverage', 'budget': budget,
|
| 625 |
+
**condition_fields(conditions),
|
| 626 |
+
'budget_unit': 'target calls (resets + actions)',
|
| 627 |
+
'parent_clusters': len(delta),
|
| 628 |
+
'matched_cells': sum(len(v) for v in per_parent.values()),
|
| 629 |
+
'families_pooled': sorted(families),
|
| 630 |
+
'mean_delta': sum(delta) / len(delta),
|
| 631 |
+
'descriptive_95_interval': [draws[49], draws[1949]],
|
| 632 |
+
'scope': 'exploratory parent-cluster paired bootstrap within identical call cap and conditions; '
|
| 633 |
+
'in-family fixtures pooled; few clusters, no confirmatory claim',
|
| 634 |
+
})
|
| 635 |
+
return output
|
| 636 |
+
|
| 637 |
+
|
| 638 |
+
STUDY_DEFAULTS = {"families": ["latch", "symmetric", "neutral"], "seeds": [17], "budgets": [0, 8],
|
| 639 |
+
"policies": ["random", "task"], "admissions": ["robust"], "implementations": ["procedural"],
|
| 640 |
+
'instance_seeds': [None], 'fault_schedules': [None], 'candidate_backends': ['flat']}
|
| 641 |
+
|
| 642 |
+
|
| 643 |
+
def study_configs(spec: dict) -> list[RunConfig]:
|
| 644 |
+
if not isinstance(spec, dict):
|
| 645 |
+
raise ValueError("Study must be an object")
|
| 646 |
+
allowed = {"families", "seeds", "budgets", "policies", "admissions", "implementations",
|
| 647 |
+
"anchors", "split", "horizon", "execution_mode", "temporal", "escalate", "probe_safety", "risk_tolerance",
|
| 648 |
+
'instance_seeds', 'fault_schedules', 'candidate_backends', 'recovery_strategy'}
|
| 649 |
+
if set(spec) - allowed:
|
| 650 |
+
raise ValueError("Unknown study fields")
|
| 651 |
+
dimensions = []
|
| 652 |
+
for key, default in STUDY_DEFAULTS.items():
|
| 653 |
+
values = spec.get(key, default)
|
| 654 |
+
if not isinstance(values, list) or not 1 <= len(values) <= 64:
|
| 655 |
+
raise ValueError(f"{key} must contain 1-64 entries")
|
| 656 |
+
if key == 'fault_schedules':
|
| 657 |
+
values = [normalize_fault_schedule(value) for value in values]
|
| 658 |
+
if len({canonical_hash(v) for v in values}) != len(values):
|
| 659 |
+
raise ValueError(f"Duplicate {key} inflate repeated observations")
|
| 660 |
+
dimensions.append(values)
|
| 661 |
+
total = 1
|
| 662 |
+
for values in dimensions:
|
| 663 |
+
total *= len(values)
|
| 664 |
+
if total > 512:
|
| 665 |
+
raise ValueError("Study exceeds 512 runs; split the protocol explicitly")
|
| 666 |
+
configs = [RunConfig.from_dict(dict(zip(("family", "seed", "budget", "policy", "admission", "implementation", 'instance_seed', 'fault_schedule', 'candidate_backend'), values),
|
| 667 |
+
anchors=spec.get("anchors", False), split=spec.get("split", "development"),
|
| 668 |
+
horizon=spec.get("horizon", 4),
|
| 669 |
+
execution_mode=spec.get("execution_mode", "open_loop"),
|
| 670 |
+
temporal=spec.get("temporal", False),
|
| 671 |
+
escalate=spec.get("escalate", False),
|
| 672 |
+
probe_safety=spec.get('probe_safety', 'unrestricted'),
|
| 673 |
+
risk_tolerance=spec.get('risk_tolerance', 0.0), recovery_strategy=spec.get('recovery_strategy', 'stabilize'))) for values in product(*dimensions)]
|
| 674 |
+
if len({canonical_hash(asdict(cfg)) for cfg in configs}) != len(configs):
|
| 675 |
+
raise ValueError('Duplicate effective study configuration; null generated instance and zero are identical')
|
| 676 |
+
return configs
|
| 677 |
+
|
| 678 |
+
|
| 679 |
+
def run_study(spec: dict, cancel: Callable[[], bool] | None = None, progress: Callable | None = None) -> dict:
|
| 680 |
+
configs = study_configs(spec)
|
| 681 |
+
frozen_source = source_hash()
|
| 682 |
+
protocol = {**STUDY_DEFAULTS, **spec, "exploratory": True,
|
| 683 |
+
"analysis_unit": "underlying semantic parent instance; label permutations and implementations clustered",
|
| 684 |
+
"pre_execution_source_hash": frozen_source}
|
| 685 |
+
protocol_hash = canonical_hash(protocol)
|
| 686 |
+
rows = []
|
| 687 |
+
for i, cfg in enumerate(configs):
|
| 688 |
+
_check_cancel(cancel)
|
| 689 |
+
if progress:
|
| 690 |
+
progress(i, len(configs), f"{cfg.family}: {cfg.policy}/{cfg.admission}, budget {cfg.budget}")
|
| 691 |
+
rows.append(run(cfg, cancel))
|
| 692 |
+
if source_hash() != frozen_source:
|
| 693 |
+
raise RuntimeError("Source changed during study; results cannot be labeled frozen")
|
| 694 |
+
value = {"schema": "crosswire-study-v1", "protocol": protocol, "protocol_hash": protocol_hash,
|
| 695 |
+
"source_hash": frozen_source, "rows": rows, "summary": summarize(rows),
|
| 696 |
+
"paired": paired_comparisons(rows)}
|
| 697 |
+
from .reliability import reliability_summary
|
| 698 |
+
value['reliability'] = reliability_summary(rows)
|
| 699 |
+
value["scientific_hash"] = canonical_hash(scientific_view(value))
|
| 700 |
+
if progress:
|
| 701 |
+
progress(len(configs), len(configs), "Complete; decisions and private audits recorded")
|
| 702 |
+
return value
|
crosswire/target_worker.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Private target process. Standard output contains protocol JSON exclusively."""
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
from .fixtures import make_engine
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _send(message):
|
| 11 |
+
sys.stdout.write(json.dumps(message,separators=(',',':'),allow_nan=False)+'\n')
|
| 12 |
+
sys.stdout.flush()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def main():
|
| 16 |
+
engine, config, steps = None, None, 0
|
| 17 |
+
try:
|
| 18 |
+
for line in sys.stdin:
|
| 19 |
+
request_id = None
|
| 20 |
+
try:
|
| 21 |
+
if len(line)>16384:
|
| 22 |
+
raise ValueError('request too large')
|
| 23 |
+
request = json.loads(line)
|
| 24 |
+
if not isinstance(request,dict) or type(request.get('id')) is not int:
|
| 25 |
+
raise ValueError('invalid envelope')
|
| 26 |
+
request_id, operation = request['id'], request.get('op')
|
| 27 |
+
if operation == 'init' and engine is None and set(request)=={'id','op','config'}:
|
| 28 |
+
config = request['config']
|
| 29 |
+
engine = make_engine(config)
|
| 30 |
+
elif operation == 'set_phase' and engine is not None:
|
| 31 |
+
phase = request.get('phase', 'calibration')
|
| 32 |
+
if hasattr(engine, 'set_phase'):
|
| 33 |
+
engine.set_phase(phase)
|
| 34 |
+
_send({'id':request_id,'ok':True})
|
| 35 |
+
continue
|
| 36 |
+
elif operation == 'reset' and engine is not None and set(request)=={'id','op'}:
|
| 37 |
+
if config['family']=='malformed' and config.get('fault_operation')=='reset':
|
| 38 |
+
sys.stdout.write('{malformed\n')
|
| 39 |
+
sys.stdout.flush()
|
| 40 |
+
continue
|
| 41 |
+
engine.reset()
|
| 42 |
+
steps = 0
|
| 43 |
+
elif operation == 'step' and engine is not None and set(request)=={'id','op','input'}:
|
| 44 |
+
if config['family']=='malformed' and steps >= config.get('fault_after',0):
|
| 45 |
+
mode = config.get('fault_mode','malformed')
|
| 46 |
+
if mode == 'timeout':
|
| 47 |
+
time.sleep(60)
|
| 48 |
+
if mode == 'exit':
|
| 49 |
+
os._exit(3)
|
| 50 |
+
sys.stdout.write('{malformed\n')
|
| 51 |
+
sys.stdout.flush()
|
| 52 |
+
continue
|
| 53 |
+
output = engine.step(request['input'])
|
| 54 |
+
steps += 1
|
| 55 |
+
_send({'id':request_id,'ok':True,'output':output})
|
| 56 |
+
continue
|
| 57 |
+
elif operation == 'close' and set(request)=={'id','op'}:
|
| 58 |
+
_send({'id':request_id,'ok':True})
|
| 59 |
+
return
|
| 60 |
+
else:
|
| 61 |
+
raise ValueError('invalid operation')
|
| 62 |
+
_send({'id':request_id,'ok':True})
|
| 63 |
+
except (ValueError,KeyError,TypeError,IndexError):
|
| 64 |
+
_send({'id':request_id,'ok':False,'error':'invalid_request'})
|
| 65 |
+
finally:
|
| 66 |
+
if engine is not None:
|
| 67 |
+
engine.close()
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if __name__ == '__main__':
|
| 71 |
+
main()
|
crosswire/web/app.js
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict';
|
| 2 |
+
const $ = id => document.getElementById(id);
|
| 3 |
+
const NS = 'http://www.w3.org/2000/svg';
|
| 4 |
+
const state = {catalog:null, active:null, completed:null, busy:false};
|
| 5 |
+
const labels = {fixed:'Fixed sequence',random:'Seeded random',disagreement:'Mapping disagreement',task:'Task disagreement',aalpy:'AALpy learner',counterexample:'Counterexample-guided (CEGP)',entropy:'Shannon entropy (InfoGain)',ec2:'Equivalence class edge cutting (EC2)',branching:'Adaptive within-query branching',lookahead:'Budgeted experiment lookahead',flat:'Flat · exhaustive candidates',factored:'Factored · lazy candidates',robust:'Robust sequence across survivors',adaptive:'Adaptive policy across survivors',risk_controlled:'Risk-controlled version space',one_best:'One best survivor',unique:'Unique mapping only',fit:'Best fit (forced baseline)',identity:'Identity (forced baseline)',abstain:'Always abstain',oracle:'Oracle (privileged)',open_loop:'Open loop (unshielded)',shielded:'Dynamic runtime shield',recovery:'Simplex recovery shield',replan:'Receding-horizon replanning shield',voi:'Value of information (VoI)',stabilize:'Stabilize hold',soft_return:'Certified soft return'};
|
| 6 |
+
function element(tag, text, cls){const node=document.createElement(tag);if(text!==undefined&&text!==null)node.textContent=String(text);if(cls)node.className=cls;return node;}
|
| 7 |
+
function svgElement(tag, attrs={}, text){const node=document.createElementNS(NS,tag);Object.entries(attrs).forEach(([key,value])=>node.setAttribute(key,String(value)));if(text!==undefined)node.textContent=String(text);return node;}
|
| 8 |
+
function readable(value){if(value===null||value===undefined)return 'Not available';if(typeof value==='boolean')return value?'Yes':'No';if(typeof value==='object')return JSON.stringify(value);return String(value);}
|
| 9 |
+
function number(value){return typeof value==='number'&&Number.isFinite(value)?Number.isInteger(value)?String(value):value.toLocaleString(undefined,{maximumFractionDigits:4}):'—';}
|
| 10 |
+
function word(value){return Array.isArray(value)?value.length?value.join(' · '):'∅':'—';}
|
| 11 |
+
function notice(message,success=false){$('notice').textContent=message;$('notice').className=`notice${success?' success':''}`;$('notice').hidden=false;}
|
| 12 |
+
async function request(path,options={}){let response;try{response=await fetch(path,{...options,headers:{'Content-Type':'application/json',...options.headers}});}catch{throw new Error('The local HTTP worker is unavailable. Start the CROSSWIRE server and open its HTTP address.');}let data;try{data=await response.json();}catch{throw new Error(`The server returned a non-JSON response (${response.status}). Check that this is the CROSSWIRE HTTP server.`);}if(!response.ok){const error=new Error(typeof data.error==='string'?data.error:typeof data.message==='string'?data.message:`Request failed (${response.status}): ${readable(data.error||data)}`);error.status=response.status;throw error;}if(!data||typeof data!=='object')throw new Error('The server response is malformed.');return data;}
|
| 13 |
+
function lock(){const busy=state.busy||!!state.active;$('run-button').disabled=busy||!state.catalog;$('study-button').disabled=busy||!state.catalog;$('import-button').disabled=busy;$('replay-button').disabled=busy||!state.completed;$('export-button').disabled=!state.completed;}
|
| 14 |
+
function optionList(id,items,fallback){const select=$(id);select.replaceChildren();for(const entry of items){const value=typeof entry==='string'?entry:entry.id||entry.name||entry.value;if(!value)continue;const option=element('option',typeof entry==='object'?entry.label||labels[value]||value:labels[value]||value);option.value=value;select.append(option);}if(fallback&&Array.from(select.options).some(o=>o.value===fallback))select.value=fallback;}
|
| 15 |
+
function familyDescription(){const current=state.catalog?.families.find(entry=>entry.id===$('family').value);$('family-description').textContent=current?.description||'';$('family-scope').textContent=(current?.scope||'Scope unspecified').replaceAll('_',' ');}
|
| 16 |
+
function config(){
|
| 17 |
+
const data={};
|
| 18 |
+
for(const name of ['family','implementation','policy','admission','execution_mode','split','probe_safety','candidate_backend','recovery_strategy'])if($(name))data[name]=$(name).value;
|
| 19 |
+
for(const name of ['seed','budget','horizon','risk_tolerance'])if($(name))data[name]=Number($(name).value);
|
| 20 |
+
if(data.family==='generated_protocol')data.instance_seed=Number($('instance_seed').value);
|
| 21 |
+
data.anchors=$('anchors').checked;data.temporal=$('temporal').checked;data.escalate=$('escalate')?$('escalate').checked:false;
|
| 22 |
+
data.fault_schedule=$('fault_type').value==='off'?null:{type:$('fault_type').value,step:Number($('fault_step').value),phase:$('fault_phase').value};
|
| 23 |
+
if(data.fault_schedule?.type==='stuck_output')data.fault_schedule.value=Number($('fault_value').value);
|
| 24 |
+
return data;
|
| 25 |
+
}
|
| 26 |
+
function studyConfig(){
|
| 27 |
+
const selected=config(),adaptive=selected.admission==='adaptive';
|
| 28 |
+
const spec={families:[selected.family],seeds:[selected.seed,(selected.seed+1)%4294967296],
|
| 29 |
+
budgets:[...new Set([Math.floor(selected.budget/2),selected.budget])],
|
| 30 |
+
policies:adaptive?[selected.policy]:selected.policy==='fixed'?['task','fixed']:[selected.policy,'fixed'],admissions:adaptive?['robust','adaptive']:[selected.admission],
|
| 31 |
+
implementations:[selected.implementation],anchors:selected.anchors,split:selected.split,
|
| 32 |
+
horizon:selected.horizon,temporal:selected.temporal,escalate:selected.escalate,execution_mode:selected.execution_mode,
|
| 33 |
+
probe_safety:selected.probe_safety,risk_tolerance:selected.risk_tolerance,fault_schedules:[selected.fault_schedule],candidate_backends:[selected.candidate_backend]};
|
| 34 |
+
if(selected.family==='generated_protocol')spec.instance_seeds=[selected.instance_seed,(selected.instance_seed+1)%4294967296];
|
| 35 |
+
return spec;
|
| 36 |
+
}
|
| 37 |
+
function selectedPolicy(){return $('policy').value;}
|
| 38 |
+
function syncControls(){
|
| 39 |
+
const adaptive=$('admission').value==='adaptive',risk=$('admission').value==='risk_controlled';
|
| 40 |
+
$('instance_seed').disabled=$('family').value!=='generated_protocol';
|
| 41 |
+
const faultEnabled=$('fault_type').value!=='off';
|
| 42 |
+
$('fault_step').disabled=!faultEnabled;$('fault_phase').disabled=!faultEnabled;
|
| 43 |
+
$('fault_value').disabled=$('fault_type').value!=='stuck_output';
|
| 44 |
+
if(adaptive&&!['shielded','recovery'].includes($('execution_mode').value))$('execution_mode').value='shielded';
|
| 45 |
+
if($('escalate')&&$('escalate').checked){$('temporal').checked=false;$('temporal').disabled=true;}
|
| 46 |
+
else if($('temporal')){$('temporal').disabled=false;}
|
| 47 |
+
for(const option of $('execution_mode').options)option.disabled=adaptive&&!['shielded','recovery'].includes(option.value);
|
| 48 |
+
if($('recovery_strategy'))$('recovery_strategy').disabled=$('execution_mode').value!=='recovery';
|
| 49 |
+
$('risk_tolerance').disabled=!risk;if(!risk)$('risk_tolerance').value='0';
|
| 50 |
+
const incompatible=$('policy').value==='aalpy'&&(adaptive||$('temporal').checked||$('escalate').checked||$('probe_safety').value==='model_safe');
|
| 51 |
+
$('policy').setCustomValidity(incompatible?'AALpy requires unrestricted calibration, a static model without escalation, and a non-adaptive admission rule.':'');
|
| 52 |
+
$('policy-help').textContent=$('policy').value==='lookahead'?'Depth-two query planning uses a model-count objective, not a calibrated target success probability. Each query costs a reset plus its actions. Only the root query is executed before replanning.':($('policy').value==='voi'?'Value of information evaluates the exact probability that the next probe yields an admitted deployment plan.':'Probe selection uses public reference predictions and observed evidence.');
|
| 53 |
+
$('admission-help').textContent=adaptive?'Adaptive policies choose actions from the observed branch. Shielded or recovery execution is required.':risk?'Admission limits hazardous and goal-failing fractions of the surviving candidates under uniform weighting.':'Robust admission requires a sequence satisfying the contract for every surviving mapping. Other rules provide different guarantees; forced baselines are diagnostic.';
|
| 54 |
+
if(!state.catalog)return;
|
| 55 |
+
const spec=studyConfig(),runs=spec.seeds.length*spec.budgets.length*spec.policies.length*spec.admissions.length*(spec.instance_seeds?.length||1);
|
| 56 |
+
$('study-description').textContent=`${runs} runs · ${spec.families[0]}${spec.instance_seeds?` · structural seeds ${spec.instance_seeds.join(' / ')}`:''} · label seeds ${spec.seeds.join(' / ')} · call budgets ${spec.budgets.join(' / ')}. ${adaptive?'Robust vs adaptive admission, using the selected probe policy.':`${labels[selectedPolicy()]||selectedPolicy()} vs ${$('policy').value==='fixed'?'task':'fixed'} probes, using the selected admission rule.`} Horizon ${spec.horizon} · ${spec.split} · ${labels[spec.execution_mode]||spec.execution_mode} · backend ${spec.candidate_backends[0]} · ${spec.probe_safety==='model_safe'?'model-safe':'unrestricted'} calibration${risk?` · candidate tolerance ${spec.risk_tolerance}`:''} · ${faultLabel(spec.fault_schedules[0])}. Label seeds and target engines are nuisance repeats of structural parents.`;
|
| 57 |
+
if($('study-protocol'))$('study-protocol').textContent=JSON.stringify(spec,null,2);
|
| 58 |
+
}
|
| 59 |
+
async function startJob(path,body,kind='run'){
|
| 60 |
+
if(state.active||state.busy)return;
|
| 61 |
+
state.busy=true;lock();
|
| 62 |
+
try{
|
| 63 |
+
const job=await request(path,{method:'POST',body:JSON.stringify(body)});
|
| 64 |
+
if(typeof job.id!=='string')throw new Error('The worker did not return a valid job ID.');
|
| 65 |
+
state.active={id:job.id,kind};$('notice').hidden=true;$('job-panel').hidden=false;
|
| 66 |
+
$('job-message').textContent=`${kind==='replay'?'Scientific replay':kind==='import'?'Import and scientific replay':'Experiment'} queued`;
|
| 67 |
+
$('job-status').textContent=job.status||'QUEUED';$('job-progress').removeAttribute('value');$('cancel-button').disabled=false;
|
| 68 |
+
poll(job.id);
|
| 69 |
+
}catch(error){notice(error.message);}finally{state.busy=false;lock();}
|
| 70 |
+
}
|
| 71 |
+
async function poll(id){
|
| 72 |
+
if(state.active?.id!==id)return;
|
| 73 |
+
try{
|
| 74 |
+
const job=await request(`/api/runs/${encodeURIComponent(id)}`);
|
| 75 |
+
if(typeof job.status!=='string')throw new Error('The worker returned a malformed job status.');
|
| 76 |
+
$('job-status').textContent=job.status;
|
| 77 |
+
const p=job.progress||{};$('job-message').textContent=p.message||'Running experiment';
|
| 78 |
+
if(Number.isFinite(p.total)&&p.total>0&&Number.isFinite(p.current)){$('job-progress').max=p.total;$('job-progress').value=Math.max(0,Math.min(p.current,p.total));}
|
| 79 |
+
else $('job-progress').removeAttribute('value');
|
| 80 |
+
if(job.status==='COMPLETE'){
|
| 81 |
+
const kind=state.active.kind;
|
| 82 |
+
if(kind==='replay'){
|
| 83 |
+
const replay=job.result||{};
|
| 84 |
+
notice(`${replay.valid===true?'Replay verified':'Replay verification failed'}: ${readable(replay.message)}${replay.scientific_hash?` · ${replay.scientific_hash}`:''}`,replay.valid===true);
|
| 85 |
+
}else{
|
| 86 |
+
if(!job.result||!['crosswire-result-v1','crosswire-study-v1'].includes(job.result.schema))throw new Error('The completed job has no supported result.');
|
| 87 |
+
state.completed={id,result:job.result};render();
|
| 88 |
+
if(kind==='import')notice('Receipt loaded after its exact scientific replay matched.',true);
|
| 89 |
+
}
|
| 90 |
+
state.active=null;$('job-panel').hidden=true;lock();return;
|
| 91 |
+
}
|
| 92 |
+
if(['FAILED','CANCELLED'].includes(job.status)){
|
| 93 |
+
state.active=null;$('job-panel').hidden=true;
|
| 94 |
+
notice(job.status==='CANCELLED'?'Job cancelled. The last completed result is preserved.':`Job failed: ${readable(job.error||'No details supplied')}. The last completed result is preserved.`);
|
| 95 |
+
lock();return;
|
| 96 |
+
}
|
| 97 |
+
if(!['RUNNING','QUEUED'].includes(job.status))throw new Error(`Unsupported job status: ${job.status}`);
|
| 98 |
+
setTimeout(()=>poll(id),450);
|
| 99 |
+
}catch(error){
|
| 100 |
+
if(error.status===404){state.active=null;$('job-panel').hidden=true;notice('The worker no longer has this job. The last completed result is preserved.');lock();return;}
|
| 101 |
+
notice(`${error.message} Tracking job ${id}; retrying. The last completed result is preserved.`);
|
| 102 |
+
setTimeout(()=>poll(id),2000);
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
function panel(title,subtitle){const section=element('section',null,'panel section-panel');const header=element('div',null,'section-heading');header.append(element('h3',title));if(subtitle)header.append(element('span',subtitle));section.append(header);return section;}
|
| 106 |
+
function keyValues(values){const dl=element('dl',null,'kv');for(const [key,value] of Object.entries(values)){dl.append(element('dt',key.replaceAll('_',' ')),element('dd',readable(value)));}return dl;}
|
| 107 |
+
function rawDetails(title,value){const details=element('details',null,'fineprint');details.append(element('summary',title),element('pre',JSON.stringify(value,null,2),'raw'));return details;}
|
| 108 |
+
function table(headers,rows){const wrap=element('div',null,'table-scroll');const tab=element('table');const head=element('thead'),tr=element('tr');for(const header of headers){const cell=element('th',header);cell.scope='col';tr.append(cell);}head.append(tr);const body=element('tbody');for(const row of rows){const line=element('tr');for(const [index,value] of row.entries()){const cell=element('td');if(headers[index]==='Conditions')cell.className='conditions-cell wrap';cell.append(value instanceof Node?value:document.createTextNode(readable(value)));line.append(cell);}body.append(line);}tab.append(head,body);wrap.append(tab);return wrap;}
|
| 109 |
+
function drawGraph(reference){const transitions=reference?.transitions;if(!Array.isArray(transitions)||!transitions.length)return element('p','No supported reference transition table is available.','fineprint');const count=transitions.length;const svg=svgElement('svg',{viewBox:'0 0 560 400',class:'graph',role:'img','aria-label':'Reference state transition graph. Edge labels show input and output symbol IDs.'});svg.append(svgElement('title',{},'Reference transducer; edge label input / output'));const defs=svgElement('defs'),marker=svgElement('marker',{id:'arrow',viewBox:'0 0 10 10',refX:9,refY:5,markerWidth:6,markerHeight:6,orient:'auto-start-reverse'});marker.append(svgElement('path',{d:'M 0 0 L 10 5 L 0 10 z',fill:'#4d6e7d'}));defs.append(marker);svg.append(defs);const positions=transitions.map((_,i)=>({x:280+145*Math.cos(2*Math.PI*i/count-Math.PI/2),y:200+132*Math.sin(2*Math.PI*i/count-Math.PI/2)}));const grouped=new Map();transitions.forEach((row,from)=>{if(!Array.isArray(row))return;row.forEach((cell,input)=>{if(!Array.isArray(cell)||!positions[cell[0]])return;const key=`${from}:${cell[0]}`;if(!grouped.has(key))grouped.set(key,{from,to:cell[0],labels:[]});grouped.get(key).labels.push(`${input}/${cell[1]}`);});});for(const edge of grouped.values()){const a=positions[edge.from],b=positions[edge.to];let path,lx,ly;if(edge.from===edge.to){path=`M ${a.x-14} ${a.y-18} C ${a.x-68} ${a.y-88}, ${a.x+68} ${a.y-88}, ${a.x+14} ${a.y-18}`;lx=a.x;ly=a.y-66;}else{const dx=b.x-a.x,dy=b.y-a.y,d=Math.hypot(dx,dy),nx=-dy/d,ny=dx/d;const bend=grouped.has(`${edge.to}:${edge.from}`)?25:7;path=`M ${a.x+dx/d*26} ${a.y+dy/d*26} Q ${(a.x+b.x)/2+nx*bend} ${(a.y+b.y)/2+ny*bend} ${b.x-dx/d*28} ${b.y-dy/d*28}`;lx=(a.x+b.x)/2+nx*(bend+12);ly=(a.y+b.y)/2+ny*(bend+12);}svg.append(svgElement('path',{d:path,class:'edge','marker-end':'url(#arrow)'}),svgElement('text',{x:lx,y:ly,class:'label','text-anchor':'middle'},edge.labels.join(' ')));}positions.forEach((p,i)=>{svg.append(svgElement('circle',{cx:p.x,cy:p.y,r:24,class:`node${i===(reference.initial||0)?' initial':''}`}),svgElement('text',{x:p.x,y:p.y+5,class:'state','text-anchor':'middle'},`s${i}`));});return svg;}
|
| 110 |
+
function metricValue(value){return typeof value==='number'?number(value):value===null||value===undefined?'—':readable(value);}
|
| 111 |
+
function metricCards(values){
|
| 112 |
+
const grid=element('div',null,'metric-grid');
|
| 113 |
+
for(const [title,value] of values){const card=element('div',null,'metric');card.append(element('b',metricValue(value)),element('span',title));grid.append(card);}
|
| 114 |
+
return grid;
|
| 115 |
+
}
|
| 116 |
+
function renderPolicy(decision,deployment){
|
| 117 |
+
const section=panel('Contingent policy','OBSERVATION-DEPENDENT ACTIONS');
|
| 118 |
+
section.append(element('p','Each action is chosen before its raw output is observed. Follow the matching output branch to the next action or STOP. Highlighted nodes show the path actually executed.','fineprint'));
|
| 119 |
+
let actionNodes=0,stopLeaves=0,maxBranch=0,minD=Infinity,maxD=0;(function stats(n,d){if(!n||typeof n!=='object')return;if(n.stop===true){stopLeaves++;minD=Math.min(minD,d);maxD=Math.max(maxD,d);return;}actionNodes++;const br=Object.values(n.branches||{});maxBranch=Math.max(maxBranch,br.length);br.forEach(c=>stats(c,d+1));})(decision.policy,0);
|
| 120 |
+
if(actionNodes||stopLeaves)section.append(element('p',`Tree structure: ${actionNodes} decision nodes · ${stopLeaves} terminal leaves · max branching ${maxBranch} · depth ${Number.isFinite(minD)?minD:0}–${maxD} steps.`,'fineprint'));
|
| 121 |
+
const executed=Array.isArray(deployment.executed_word)?deployment.executed_word:[],outputs=Array.isArray(deployment.outputs)?deployment.outputs:[];
|
| 122 |
+
let shown=0,truncated=false;
|
| 123 |
+
function visit(node,depth,onPath,raw){
|
| 124 |
+
const item=element('li');
|
| 125 |
+
if(shown>=200){truncated=true;item.append(element('span','More nodes in the exported receipt','fineprint'));return item;}
|
| 126 |
+
shown++;
|
| 127 |
+
if(raw!==undefined)item.append(element('div',`Observe raw ${raw}`,'policy-observation'));
|
| 128 |
+
if(!node||typeof node!=='object'){item.append(element('span','Unsupported policy node','fineprint'));return item;}
|
| 129 |
+
const stop=node.stop===true,selected=onPath&&(stop?deployment.complete===true&&depth===executed.length:executed[depth]===node.action);
|
| 130 |
+
item.append(element('span',stop?'STOP':`Action ${readable(node.action)}`,`policy-node${stop?' policy-stop':''}${selected?' policy-executed':''}`));
|
| 131 |
+
if(!stop){
|
| 132 |
+
const children=element('ul');
|
| 133 |
+
for(const [symbol,child] of Object.entries(node.branches||{})){
|
| 134 |
+
if(shown>=200){truncated=true;break;}
|
| 135 |
+
children.append(visit(child,depth+1,onPath&&executed[depth]===node.action&&String(outputs[depth])===symbol,symbol));
|
| 136 |
+
}
|
| 137 |
+
if(children.childNodes.length)item.append(children);
|
| 138 |
+
}
|
| 139 |
+
return item;
|
| 140 |
+
}
|
| 141 |
+
const tree=element('ul',null,'policy-tree');tree.setAttribute('aria-label','Policy actions and raw observation branches');tree.append(visit(decision.policy,0,true));section.append(tree);
|
| 142 |
+
if(truncated)section.append(element('p','Showing up to 200 policy nodes. Export contains the full tree.','fineprint'));
|
| 143 |
+
return section;
|
| 144 |
+
}
|
| 145 |
+
function renderDeployment(deployment){
|
| 146 |
+
const section=panel('Executed deployment','TARGET ACTION / RAW OUTPUT');
|
| 147 |
+
const executed=Array.isArray(deployment.executed_word)?deployment.executed_word:[],outputs=Array.isArray(deployment.outputs)?deployment.outputs:[];
|
| 148 |
+
section.append(element('p',`Executed: ${word(executed)} · Observed: ${word(outputs)}`,'word'));
|
| 149 |
+
if(executed.length)section.append(table(['Step','Target action','Raw output','Survivors after observation'],executed.map((action,i)=>[i+1,action,outputs[i]??null,deployment.survivor_history?.[i+1]??null])));
|
| 150 |
+
else section.append(element('p','No target actions were executed.','fineprint'));
|
| 151 |
+
section.append(keyValues({complete:deployment.complete,mode:labels[deployment.mode]||deployment.mode,shield_status:deployment.shield_status,intervened:deployment.intervened,deployment_calls:deployment.target_calls}));
|
| 152 |
+
if(deployment.replanned)section.append(element('p',`Replanned online: safe detours were synthesized during deployment (${deployment.replan_count||1} replans).`,'fineprint'));
|
| 153 |
+
if(deployment.change_point?.scheduled){const cp=deployment.change_point;section.append(element('p',`Change-point fault (${readable(cp.type)} at step ${number(cp.step)}): ${cp.detected?`detected at step ${number(cp.detection_step)} (latency Δt = ${number(cp.detection_latency)})`:'not detected in raw observations'}.`,'fineprint'));}
|
| 154 |
+
for(const reason of deployment.reasons||[])section.append(element('p',reason,'fineprint'));
|
| 155 |
+
if(deployment.replan_events?.length)section.append(rawDetails('Replanning events',deployment.replan_events));
|
| 156 |
+
if(deployment.error)section.append(rawDetails('Deployment error',deployment.error));
|
| 157 |
+
return section;
|
| 158 |
+
}
|
| 159 |
+
function renderCertificate(cert,search){
|
| 160 |
+
const c=cert||{},section=panel('Public model evidence',c.valid===true?'CERTIFICATE CHECKED':'CERTIFICATE UNVERIFIED');
|
| 161 |
+
section.append(element('p',c.scope||'Public model evidence only; does not authenticate private audits or target execution.','fineprint'));
|
| 162 |
+
section.append(keyValues(Object.fromEntries(Object.entries(c).filter(([k,v])=>v===null||typeof v!=='object'))));
|
| 163 |
+
if(c.model_diagnostics)section.append(renderModelDiagnostics(c.model_diagnostics));
|
| 164 |
+
if(c.elimination?.length&&c.kind==='unresolved')section.append(rawDetails('Minimal contradiction core & elimination cascade',c.elimination));
|
| 165 |
+
section.append(rawDetails('Full certificate and search bounds',{certificate:c,search}));
|
| 166 |
+
return section;
|
| 167 |
+
}
|
| 168 |
+
function renderModelDiagnostics(diagnostic){
|
| 169 |
+
const section=element('div',null,'model-diagnostics'),refuted=diagnostic.inclusion_status==='REFUTED';
|
| 170 |
+
section.append(element('p',`Model inclusion: ${refuted?'REFUTED':'UNKNOWN'}`,`model-inclusion${refuted?' refuted':''}`));
|
| 171 |
+
section.append(element('p',refuted?'Public observations refute every enabled model class. The certificate and the private execution audit make separate claims.':'Actual model inclusion remains UNKNOWN. Compatible finite observations and an admitted plan leave unobserved behavior and semantic validity unresolved.','fineprint'));
|
| 172 |
+
if(diagnostic.scope)section.append(element('p',diagnostic.scope,'fineprint'));
|
| 173 |
+
if(Array.isArray(diagnostic.enabled_classes))section.append(element('p',`Enabled classes: ${diagnostic.enabled_classes.join(' / ')}`,'fineprint'));
|
| 174 |
+
for(const [name,model] of Object.entries(diagnostic.classes||{})){
|
| 175 |
+
const block=element('div',null,'diagnostic-class'),events=Array.isArray(model.events)?model.events:[],status=model.inclusion_status||(model.candidate_count===0?'REFUTED':'UNKNOWN');
|
| 176 |
+
block.append(element('h4',`${name.replaceAll('_',' ')} model class · ${status}`),keyValues({surviving_candidates:model.candidate_count,scored_symbols:model.scored_symbols,unscored_symbols:model.unscored_symbols,cumulative_surprisal_bits:model.cumulative_surprisal_bits,mean_surprisal_bits:model.mean_symbol_surprisal_bits,martingale_e_value:model.martingale_e_value,martingale_refuted_alpha_05:model.martingale_refuted_alpha_05}));
|
| 177 |
+
const first=model.first_refutation;
|
| 178 |
+
if(first){
|
| 179 |
+
block.append(element('p',first.kind==='anchor'?`First refutation: conflicting anchor for raw ${readable(first.raw)} and semantic ${readable(first.semantic)}.`:`First refutation: probe ${Number.isInteger(first.probe_index)?first.probe_index+1:'—'}, step ${number(first.step)}${first.word_prefix?` · prefix ${word(first.word_prefix)}`:''}. Observed raw output ${readable(first.observed_output)} had no support among ${number(first.survivors_before)} prior candidates.`,'fineprint'),rawDetails('First public refutation witness',first));
|
| 180 |
+
}else block.append(element('p','No refutation was observed in this finite transcript.','fineprint'));
|
| 181 |
+
block.append(element('h4','Prediction support timeline'),element('p','Predicted output counts use the candidates available before that output was observed. Counts describe finite candidate support. UNSCORED events follow class refutation.','fineprint'));
|
| 182 |
+
if(events.length)block.append(table(['Probe','Step','Action','Predicted raw output : count','Observed raw','Before','After','Support'],events.slice(0,200).map(event=>[Number.isInteger(event.probe_index)?event.probe_index+1:null,event.step,event.action,Object.entries(event.predicted_output_counts||{}).map(([symbol,count])=>`${symbol}: ${count}`).join(' · ')||'—',event.observed_output,event.survivors_before,event.survivors_after,event.prediction_status])));
|
| 183 |
+
else block.append(element('p','No observed symbols are available for prediction scoring.','fineprint'));
|
| 184 |
+
if(events.length>200)block.append(element('p',`Showing 200 of ${events.length} events. The exported receipt contains the complete timeline.`,'fineprint'));
|
| 185 |
+
section.append(block);
|
| 186 |
+
}
|
| 187 |
+
const reset=diagnostic.reset_consistency||{};
|
| 188 |
+
section.append(element('p',`Reset-prefix consistency: ${reset.status||'UNKNOWN'}. Repeated prefixes test only the observed reset behavior.`,'fineprint'));
|
| 189 |
+
if(reset.first_conflict){const conflict=reset.first_conflict;section.append(element('p',`First reset-prefix conflict: ${word(conflict.word_prefix)} across probes ${Number.isInteger(conflict.previous_probe_index)?conflict.previous_probe_index+1:'—'} and ${Number.isInteger(conflict.probe_index)?conflict.probe_index+1:'—'}.`,'fineprint'),rawDetails('Reset-prefix conflict witness',conflict));}
|
| 190 |
+
const blindSpots=Array.isArray(diagnostic.blind_spots)?diagnostic.blind_spots:[];
|
| 191 |
+
if(blindSpots.length){const list=element('ul',null,'fineprint diagnostic-notes');for(const warning of blindSpots)list.append(element('li',warning));section.append(element('h4','Public evidence blind spots'),list);}
|
| 192 |
+
return section;
|
| 193 |
+
}
|
| 194 |
+
function renderCalibration(result){
|
| 195 |
+
const cfg=result.configuration||{},check=result.evidence_certificate?.calibration_safety||{},acquisition=result.decision?.calibration||{};
|
| 196 |
+
const mode=check.mode||acquisition.mode||cfg.probe_safety||'unrestricted',section=panel('Calibration safety',mode==='model_safe'?'ALL SURVIVING MODELS':'UNRESTRICTED PROBES');
|
| 197 |
+
const reasons={NO_ELIGIBLE_SAFE_PROBE:'Calibration stopped because no eligible model-safe probe remained.',CONTRACT_READY:'Calibration stopped once the contract decision was ready.',CALL_CAP:'Calibration reached the configured target-call cap.',MODEL_CONTRADICTED:'The observations contradicted every model in the checked class.',NO_INFORMATIVE_PROBE:'No informative probe remained within the configured search.',INTERFACE_ERROR:'Calibration stopped after a target interface error.',NOT_REQUESTED:'Calibration was not requested.',CLASSICAL_BASELINE:'Calibration was performed by the classical learner.'};
|
| 198 |
+
section.append(element('p',mode==='model_safe'?'Each dispatched probe must avoid the contract hazard under every surviving model before execution. Safety is conditional on the target belonging to that model class and on truthful anchors.':'Probes were selected without a requirement to avoid the contract hazard across all surviving models. The private audit reports any hazardous calibration events separately.','fineprint'));
|
| 199 |
+
section.append(keyValues({mode,checked_probes:check.checked_probes??null,public_check_passed:check.safe??null,model_class:check.model_class||acquisition.safety_model_class||null,stop_reason:acquisition.stop_reason||null}));
|
| 200 |
+
if(acquisition.stop_reason)section.append(element('p',reasons[acquisition.stop_reason]||acquisition.stop_reason,'fineprint'));
|
| 201 |
+
if(check.scope||acquisition.scope)section.append(element('p',check.scope||acquisition.scope,'fineprint'));
|
| 202 |
+
return section;
|
| 203 |
+
}
|
| 204 |
+
function renderBackend(backend){
|
| 205 |
+
const section=panel('Candidate backend','RUNTIME TELEMETRY');
|
| 206 |
+
section.append(element('p','The worker reports the representation used for this run. Structural factor ratios describe the model class; they are not measured memory compression.','fineprint'));
|
| 207 |
+
const fields=Object.fromEntries(Object.entries(backend).filter(([,value])=>value===null||typeof value!=='object'));
|
| 208 |
+
section.append(keyValues(fields));
|
| 209 |
+
if(backend.work&&typeof backend.work==='object')section.append(element('h4','Prediction work'),keyValues(backend.work));
|
| 210 |
+
section.append(rawDetails('Inspect backend telemetry',backend));
|
| 211 |
+
return section;
|
| 212 |
+
}
|
| 213 |
+
function renderPlanning(probes){
|
| 214 |
+
const section=panel('Budgeted experiment planning','PUBLIC FINITE-MODEL CHECK');
|
| 215 |
+
section.append(element('p','Each proposal contains hypothetical future queries and responses. Only its root query is dispatched; the worker replans after observing the target. The objective counts uniformly weighted candidate models, not a calibrated target success probability.','fineprint planning-objective'));
|
| 216 |
+
for(const [index,probe] of probes.entries()){
|
| 217 |
+
const planning=probe.planning;if(!planning)continue;
|
| 218 |
+
const certificate=planning.certificate||{},block=element('article',null,'planning-proposal');
|
| 219 |
+
block.append(element('h4',`Probe ${index+1} proposal`),element('p',`Uniformly weighted models reaching ready leaves: ${number(certificate.ready_models)} / ${number(certificate.candidate_count)}. Proposed worst-case query cost: ${number(certificate.worst_case_calls)} reset/action calls.`,'fineprint'));
|
| 220 |
+
block.append(keyValues({expanded_nodes:planning.expanded_nodes,node_limit:planning.node_limit,depth_limit:planning.depth_limit,search_complete:planning.search_complete,selection:planning.selection,remaining_calls:planning.remaining_calls,model_class:planning.model_class,model_safe:planning.safe_only}));
|
| 221 |
+
block.append(element('p',`Independent finite check: ${certificate.valid===true?'passed':certificate.valid===false?'failed':'unavailable'}. This checks the proposal under its declared model class, not target truth or private semantics.`,'fineprint'));
|
| 222 |
+
if(planning.search_complete===false)block.append(element('p','Incomplete search: the selected completed proposal or deterministic fallback does not establish optimality or absence of a better experiment.','fineprint'));
|
| 223 |
+
if(certificate.scope)block.append(element('p',certificate.scope,'fineprint'));
|
| 224 |
+
block.append(rawDetails('Inspect independent experiment check',certificate));section.append(block);
|
| 225 |
+
}
|
| 226 |
+
return section;
|
| 227 |
+
}
|
| 228 |
+
function renderRun(result,container){
|
| 229 |
+
const d=result.decision||{},audit=result.audit||{},ambiguity=result.ambiguity||{},metrics=result.metrics||{},dep=result.deployment||{};
|
| 230 |
+
container.append(element('p',`Conditions: ${conditionLabel(result.configuration)}`,'fineprint'));
|
| 231 |
+
const statusGrid=element('div',null,'status-grid'),dec=element('section',null,`panel status-card${d.admitted?' good':''}`);
|
| 232 |
+
dec.append(element('p','02 / FROZEN DECISION','eyebrow'),element('h2',d.status||'Status unavailable'),element('div',d.admitted?(d.policy?'Branching policy admitted':`Plan: ${word(d.word)}`):'No plan admitted','word'),element('p',`Admission: ${labels[result.configuration?.admission]||result.configuration?.admission||'—'} · ${readable(d.candidate_count??ambiguity.map_count)} candidate mappings`));
|
| 233 |
+
if(d.reason)dec.append(element('p',d.reason,'fineprint'));
|
| 234 |
+
if(d.search)dec.append(element('p',`Policy search: ${number(d.search.expanded_nodes)} / ${number(d.search.node_limit)} nodes · ${d.search.complete===true?'complete within bounds':'incomplete within bounds'}`,'fineprint'));
|
| 235 |
+
if(d.risk_metrics)dec.append(element('p',d.admitted?`Uniformly weighted candidate fractions: hazardous ${number(d.risk_metrics.hazard_risk)} (allowed ${number(d.risk_metrics.tolerance)}) · goal-satisfying ${number(d.risk_metrics.goal_rate)}. These are finite model counts, not calibrated probabilities of harm.`:`Candidate-fraction admission: no plan selected; candidate rates are undefined. Configured tolerance ${number(result.configuration?.risk_tolerance??d.risk_metrics.tolerance)} under uniform candidate weighting.`,'fineprint'));
|
| 236 |
+
if(d.escalation?.escalated)dec.append(element('p','Escalated to temporal model class after static refutation','fineprint'));
|
| 237 |
+
if(d.structural_contradiction)dec.append(element('p','Structural contradiction: no adapter in static or temporal class explains trace','fineprint'));
|
| 238 |
+
const outcome=element('section',null,`panel status-card${audit.contract_satisfied===true?' good':audit.contract_satisfied===false?' bad':''}`);
|
| 239 |
+
outcome.append(element('p','03 / PRIVATE AUDIT','eyebrow'),element('h2',audit.contract_satisfied===true?'Contract satisfied':audit.contract_satisfied===false?'Contract violated':'No execution outcome'),element('p',`Goal reached: ${readable(audit.goal_success)} · Deployment hazard: ${readable(audit.hazard)}`),element('p',`Calibration: ${number(audit.calibration_hazardous_probes)} hazardous probes. Oracle feasible: ${readable(audit.oracle_feasible)}.`),element('p','Audit truth is withheld during acquisition.','fineprint'));
|
| 240 |
+
if(dep.intervened)outcome.append(element('p',`Shield intervened: ${readable(dep.reasons?.[0]||dep.shield_status)}`,'fineprint'));
|
| 241 |
+
if(audit.hazard_averted===true)outcome.append(element('p','Private counterfactual audit reports a deployment hazard averted.','fineprint'));
|
| 242 |
+
if(dep.recovery?.succeeded)outcome.append(element('p',dep.recovery.return_to_initial?'Certified soft return-to-home: target safely restored to initial baseline state (q0).':'Simplex fail-safe recovery successfully executed without hazard.','fineprint'));
|
| 243 |
+
if(dep.replanned)outcome.append(element('p',`Replanned online: ${dep.replan_count||1} safe detours executed.`,'fineprint'));
|
| 244 |
+
statusGrid.append(dec,outcome);container.append(statusGrid);
|
| 245 |
+
if(result.backend)container.append(renderBackend(result.backend));
|
| 246 |
+
container.append(metricCards([['Surviving mappings',ambiguity.map_count],['Observation classes',ambiguity.observational_classes],['Contract classes',ambiguity.contract_classes],['Acquisition calls',metrics.target_calls],['Deployment calls',metrics.deployment_calls??dep.target_calls],['Total target calls',metrics.total_calls??(Number.isFinite(metrics.target_calls)&&Number.isFinite(dep.target_calls)?metrics.target_calls+dep.target_calls:null)],['Hazardous calibration probes',audit.calibration_hazardous_probes],['Deployment hazard',audit.hazard],['Deployment mode',labels[result.configuration?.execution_mode]||result.configuration?.execution_mode]]));
|
| 247 |
+
if(d.policy)container.append(renderPolicy(d,dep));
|
| 248 |
+
if(d.admitted||dep.executed_word?.length)container.append(renderDeployment(dep));
|
| 249 |
+
container.append(renderCertificate(result.evidence_certificate||d.certificate,d.search));
|
| 250 |
+
const top=element('div',null,'two-panels'),reference=panel('Reference model','INPUT / OUTPUT');
|
| 251 |
+
reference.append(drawGraph(result.reference),element('p',`Goal symbol ${readable(result.contract?.goal)} · Hazard symbol ${readable(result.contract?.hazard)} · Horizon ${readable(result.contract?.horizon)}. Amber ring marks the initial state.`,'fineprint'));
|
| 252 |
+
const candidates=Array.isArray(result.candidates)?result.candidates:[],mapping=panel('Candidate mappings',`${candidates.length} RETAINED`),temporal=result.configuration?.temporal||candidates.some(c=>c.delay);
|
| 253 |
+
const activeInputFactors=new Set(candidates.map(c=>JSON.stringify(c.input_map))).size,activeOutputFactors=new Set(candidates.map(c=>JSON.stringify(c.output_map))).size;
|
| 254 |
+
mapping.append(element('p',`Input maps: target → reference. Output maps: reference → target. IDs are opaque symbols. Structural factors among surviving adapters: ${activeInputFactors} active input factors × ${activeOutputFactors} active output factors. These counts apply to either backend.`,'fineprint'));
|
| 255 |
+
if(temporal)mapping.append(element('p','Delay is measured in steps. Initial output is the reference symbol buffered by a delayed adapter; its output map gives the first raw symbol.','fineprint'));
|
| 256 |
+
if(candidates.length)mapping.append(table(temporal?['ID','Input map','Output map','Delay','Initial output']:['ID','Input map','Output map'],candidates.slice(0,200).map(c=>temporal?[c.id,word(c.input_map),word(c.output_map),c.delay||0,c.delay?c.init_output:'—']:[c.id,word(c.input_map),word(c.output_map)])));
|
| 257 |
+
else mapping.append(element('p','No candidate mappings retained.','fineprint'));
|
| 258 |
+
if(candidates.length>200)mapping.append(element('p','Showing the first 200 mappings; export contains the full result.','fineprint'));
|
| 259 |
+
top.append(reference,mapping);container.append(top);
|
| 260 |
+
const probes=Array.isArray(result.probes)?result.probes:[],timeline=panel('Acquisition evidence',`${probes.length} PROBES`),list=element('ol',null,'timeline');
|
| 261 |
+
timeline.append(element('p',`The private audit counted ${number(audit.calibration_hazardous_probes)} hazardous calibration probes. These costs are separate from deployment hazards; successful deployment does not erase acquisition harm.`,'fineprint'));
|
| 262 |
+
if(probes.length){const firstBefore=Array.isArray(probes[0].survivors_before)?probes[0].survivors_before.length:probes[0].survivors_before,lastAfter=Array.isArray(probes[probes.length-1].survivors_after)?probes[probes.length-1].survivors_after.length:probes[probes.length-1].survivors_after;if(Number.isFinite(firstBefore)&&Number.isFinite(lastAfter)&&firstBefore>0){const infoBits=lastAfter>0?Math.max(0,Math.log2(firstBefore/lastAfter)):Math.log2(firstBefore);timeline.append(element('p',`Version-space reduction: ${firstBefore} → ${lastAfter} mappings (${number(infoBits)} bits of uncertainty resolved across ${probes.length} queries).`,'fineprint'));}}
|
| 263 |
+
probes.forEach((probe,i)=>{const item=element('li'),info=element('div',null,'probe-code');info.append(element('div',`in ${word(probe.word)}`),element('div',`out ${word(probe.outputs)}`,'muted'));const count=element('div',null,'probe-count');count.append(element('div',`${Array.isArray(probe.survivors_before)?probe.survivors_before.length:readable(probe.survivors_before)} → ${Array.isArray(probe.survivors_after)?probe.survivors_after.length:readable(probe.survivors_after)}`),element('div',probe.complete===true?'Complete':'Incomplete'));if(probe.safety)count.append(element('div',probe.safety.model_safe===true?`Model-safe · ${number(probe.safety.candidate_count)} maps`:'Safety check unavailable'));item.append(element('span',String(i+1).padStart(2,'0'),'probe-index'),info,count);list.append(item);});
|
| 264 |
+
timeline.append(probes.length?list:element('p','No acquisition probes were executed.','fineprint'));container.append(timeline);
|
| 265 |
+
if(probes.some(probe=>probe.planning))container.append(renderPlanning(probes));
|
| 266 |
+
container.append(renderCalibration(result));
|
| 267 |
+
const bottom=element('div',null,'two-panels'),cost=panel('Measured work','SEPARATE COST DOMAINS');cost.append(keyValues(metrics));
|
| 268 |
+
const checks=panel('Audit & assumption checks','POST-DECISION');checks.append(keyValues(Object.fromEntries(Object.entries(audit).filter(([,v])=>v===null||typeof v!=='object'))));
|
| 269 |
+
if(audit.change_point?.scheduled){const cp=audit.change_point;checks.append(element('p',`Change-point audit (${readable(cp.type)} at step ${number(cp.step)}): detected ${readable(cp.detected)}${cp.detection_latency!==null&&cp.detection_latency!==undefined?` (Δt = ${number(cp.detection_latency)})`:''} · hazard averted ${readable(audit.hazard_averted)}.`,'fineprint'));}
|
| 270 |
+
if(audit.counterexample)checks.append(element('p',`Counterexample: ${word(audit.counterexample)}`,'fineprint'));
|
| 271 |
+
checks.append(rawDetails('Full audit, including finite assumptions',audit));bottom.append(cost,checks);container.append(bottom);
|
| 272 |
+
const receipt=panel('Receipt & scientific boundary');receipt.append(element('p','This result concerns the configured synthetic family and finite planning horizon. Mapping ambiguity, observable equivalence, and contract equivalence are different quantities. A successful private audit does not establish real-world safety or unrestricted semantic understanding.','fineprint'),keyValues({scientific_hash:result.scientific_hash,target_id:result.target_id,parent_instance_id:result.parent_instance_id}));
|
| 273 |
+
if(result.instance_design)receipt.append(element('h4','Structural instance design'),keyValues({structural_instance_seed:result.configuration?.instance_seed,generator:result.instance_design.generator,dynamics_hash:result.instance_design.dynamics_hash,sampling_unit:result.instance_design.sampling_unit}),rawDetails('Instance design metadata',result.instance_design));
|
| 274 |
+
receipt.append(rawDetails('Configuration and baseline',{configuration:result.configuration,baseline:result.baseline}));container.append(receipt);
|
| 275 |
+
}
|
| 276 |
+
function faultLabel(schedule){
|
| 277 |
+
if(!schedule)return 'No injected fault';
|
| 278 |
+
return `Fault: ${String(schedule.type||'unknown').replaceAll('_',' ')} · zero-based step ${number(schedule.step)} · ${schedule.phase==='always'?'both phases':schedule.phase||'deployment'}${schedule.type==='stuck_output'?` · output ${number(schedule.value??0)}`:''}`;
|
| 279 |
+
}
|
| 280 |
+
function conditionLabel(cfg={}){
|
| 281 |
+
const fault=Array.isArray(cfg.fault_schedules)?cfg.fault_schedules.map(faultLabel).join(' / '):faultLabel(cfg.fault_schedule);
|
| 282 |
+
const backend=Array.isArray(cfg.candidate_backends)?cfg.candidate_backends.join(' / '):cfg.candidate_backend||'flat';
|
| 283 |
+
return `${cfg.split||'development'} · H${cfg.horizon??4} · ${labels[cfg.execution_mode||'open_loop']||cfg.execution_mode} · backend ${backend} · anchors ${cfg.anchors?'on':'off'} · temporal ${cfg.temporal?'on':'off'} · escalation ${cfg.escalate?'on':'off'} · ${cfg.probe_safety==='model_safe'?'model-safe':'unrestricted'} probes · candidate tolerance ${number(cfg.risk_tolerance??0)} · ${fault}`;
|
| 284 |
+
}
|
| 285 |
+
function intervalText(interval){return Array.isArray(interval)&&interval.length===2&&interval.every(value=>typeof value==='number'&&Number.isFinite(value))?interval.map(number).join(' to '):null;}
|
| 286 |
+
function targetEventMetric(metric={}){
|
| 287 |
+
const interval=intervalText(metric.interval),unknown=Number.isInteger(metric.unknown_targets)?metric.unknown_targets:0;
|
| 288 |
+
return `${number(metric.events)} / ${number(metric.targets)} targets · observed rate ${number(metric.rate)}${interval?` · interval ${interval}`:''}${unknown?` · ${unknown} unknown target${unknown===1?'':'s'}${typeof metric.possible_rate==='number'?` · possible rate ${number(metric.possible_rate)}`:''}`:''}`;
|
| 289 |
+
}
|
| 290 |
+
function renderReliability(reliability){
|
| 291 |
+
const section=panel('Independent-target reliability','CONDITIONAL SYNTHETIC INFERENCE');
|
| 292 |
+
section.append(element('p','Structural parents are the sampling units. Label seeds and target engines are nuisance repeats; additional requests do not increase the number of independent targets. Intervals require the worker\'s independence and sampling assumptions within each condition.','fineprint'),element('p','Unknown target outcomes remain unresolved. A zero observed hazard count leaves uncertainty about unseen targets and unexecuted deployments. Conditional selective harm conditions on admission, using equal-target means.','fineprint reliability-note'));
|
| 293 |
+
for(const row of reliability){
|
| 294 |
+
const block=element('article',null,'reliability-stratum'),useful=row.target_average_useful_coverage||{},usefulInterval=intervalText(useful.interval),hasInterval=intervalText(row.target_any_deployment_hazard?.interval)||intervalText(row.target_any_calibration_hazard?.interval)||usefulInterval;
|
| 295 |
+
block.append(element('h4',`${row.family||'Unknown family'} / ${row.method||'Unknown method'} / B${row.budget??'?'}`),element('p',conditionLabel(row),'fineprint'),keyValues({Requests:row.requests,'Parent clusters':row.parent_clusters,'Independent parents':row.independent_parents,...(row.unique_nuisance_requests!==undefined?{'Unique nuisance cells':row.unique_nuisance_requests}:{}),...(row.structural_seed_collisions?{'Structural seed collisions':row.structural_seed_collisions}:{})}));
|
| 296 |
+
const metrics=element('div',null,'reliability-metrics');
|
| 297 |
+
for(const [title,value] of [['Any deployment hazard',targetEventMetric(row.target_any_deployment_hazard)],['Any calibration hazard',targetEventMetric(row.target_any_calibration_hazard)],['Target-average useful coverage',`${number(useful.estimate)}${usefulInterval?` · interval ${usefulInterval}`:''}`]]){
|
| 298 |
+
const metric=element('div',null,'reliability-metric');metric.append(element('h5',title),element('p',value));metrics.append(metric);
|
| 299 |
+
}
|
| 300 |
+
block.append(metrics,keyValues({'Conditional selective harm':row.conditional_selective_harm===null||row.conditional_selective_harm===undefined?'Undefined':number(row.conditional_selective_harm)}),element('p',`Inferential sampling eligible: ${readable(row.inference_eligible)}${row.inference_reason||row.reason?` · ${readable(row.inference_reason||row.reason).replaceAll('_',' ')}`:''}${hasInterval&&typeof row.confidence==='number'?` · ${number(row.confidence*100)}% pointwise intervals`:''}.`,'fineprint'));section.append(block);
|
| 301 |
+
}
|
| 302 |
+
for(const scope of new Set(reliability.map(row=>row.scope).filter(Boolean)))section.append(element('p',scope,'fineprint'));
|
| 303 |
+
section.append(rawDetails('Inspect target-level reliability',reliability));return section;
|
| 304 |
+
}
|
| 305 |
+
function renderStudy(result,container){
|
| 306 |
+
const rows=Array.isArray(result.rows)?result.rows:[],summary=Array.isArray(result.summary)?result.summary:[],intro=panel('Paired study','SERVER-COMPUTED SUMMARY');
|
| 307 |
+
intro.append(element('p',`${rows.length} run results. Rates and paired comparisons below come from the worker. Declined plans have no execution outcome; selective harm is undefined when no plan was admitted. Calibration hazards are counted independently of deployment outcomes.`,'fineprint'));
|
| 308 |
+
const protocol=result.protocol||{};
|
| 309 |
+
intro.append(keyValues({families:protocol.families,...(protocol.instance_seeds?{structural_instance_seeds:protocol.instance_seeds}:{}),label_seeds:protocol.seeds,budgets:protocol.budgets,probe_policies:protocol.policies,admissions:protocol.admissions,parent_clusters:new Set(rows.map(row=>row.parent_instance_id)).size}));
|
| 310 |
+
intro.append(element('p',`Conditions: ${conditionLabel({...rows[0]?.configuration,...protocol})}`,'fineprint'));container.append(intro);
|
| 311 |
+
if(Array.isArray(result.reliability)&&result.reliability.length)container.append(renderReliability(result.reliability));
|
| 312 |
+
if(summary.length){
|
| 313 |
+
const plot=panel('Useful coverage by method','SERVER SUMMARY'),height=Math.max(140,summary.length*36+30),svg=svgElement('svg',{viewBox:`0 0 720 ${height}`,class:'study-chart',role:'img','aria-label':'Worker-reported useful coverage by method, family and call budget'});
|
| 314 |
+
summary.forEach((row,i)=>{const y=22+i*36,label=`${row.family??''} / ${row.method??''} / B${row.budget??'?'}`,rate=typeof row.useful_coverage==='number'&&Number.isFinite(row.useful_coverage)?row.useful_coverage:null;const text=svgElement('text',{x:0,y:y+12},label.length>44?label.slice(0,43)+'…':label);text.append(svgElement('title',{},`${label} · ${conditionLabel(row)}`));svg.append(text);if(rate!==null)svg.append(svgElement('rect',{x:290,y,width:Math.max(0,Math.min(1,rate))*330,height:16,class:'bar'}));svg.append(svgElement('text',{x:635,y:y+12},rate===null?'N/A':`${number(rate*100)}%`));});plot.append(element('p','Each bar represents one family, method, budget, and condition stratum. Exact conditions are listed in the tables.','fineprint'),svg);container.append(plot);
|
| 315 |
+
const summaryTable=panel('Method outcomes','PAIRED PARENT CLUSTERS');
|
| 316 |
+
summaryTable.append(element('p','Contract failures include unsuccessful admitted plans. Deployment hazards count hazardous semantic events. Hazardous calibration runs and probes measure acquisition separately.','fineprint'));
|
| 317 |
+
summaryTable.append(table(['Family','Method','Budget','Conditions','Runs','Parent clusters','Admitted','Successful','Contract failures','Deployment hazards','Hazardous calibration runs','Hazardous calibration probes','Useful coverage','Selective harm'],summary.map(r=>[r.family,r.method,r.budget,conditionLabel(r),r.requests,r.parent_clusters,r.admitted,r.successful,r.harmful,r.hazardous,r.calibration_hazardous_runs,r.calibration_hazardous_probes,r.useful_coverage,r.selective_harm])));container.append(summaryTable);
|
| 318 |
+
const costs=panel('Acquisition & deployment costs','MEAN CALLS PER RUN');costs.append(table(['Family','Method','Budget','Conditions','Acquisition calls','Deployment calls','Total target calls','Acquisition action steps'],summary.map(r=>[r.family,r.method,r.budget,conditionLabel(r),r.mean_target_calls,r.mean_deployment_calls,r.mean_total_calls,r.mean_target_steps])));container.append(costs);
|
| 319 |
+
}
|
| 320 |
+
const paired=Array.isArray(result.paired)?result.paired:[],comparisons=panel('Paired comparisons & protocol','EXPLORATORY MATCHED CONTRASTS');
|
| 321 |
+
comparisons.append(element('p','Differences compare matched label seeds and implementations within each semantic parent and condition stratum. Positive values favor the first method in the contrast. The descriptive 95% interval resamples whole parents; a small number of parents cannot support a confirmatory claim. Out-of-family controls are excluded.','fineprint'));
|
| 322 |
+
if(paired.length)comparisons.append(table(['Contrast','Fixed method','Budget','Conditions','Parent clusters','Matched cells','Mean difference','Descriptive 95% interval'],paired.map(p=>[p.contrast,p.fixed_admission?`admission: ${p.fixed_admission}`:`policy: ${p.fixed_policy}`,p.budget,conditionLabel(p),p.parent_clusters,p.matched_cells,number(p.mean_delta),Array.isArray(p.descriptive_95_interval)?p.descriptive_95_interval.map(number).join(' to '):null])));
|
| 323 |
+
else comparisons.append(element('p','No matched in-family comparison is available for this protocol. Inspect individual outcomes and the recorded conditions.','fineprint'));
|
| 324 |
+
comparisons.append(rawDetails('Inspect full paired analysis',paired),rawDetails('Inspect study protocol',result.protocol));container.append(comparisons);
|
| 325 |
+
const detail=element('section',null,'study-run-detail');detail.id='study-run-detail';detail.hidden=true;detail.tabIndex=-1;detail.setAttribute('aria-label','Selected study run');
|
| 326 |
+
function inspectRun(row,index,trigger){
|
| 327 |
+
detail.replaceChildren();const heading=panel(`Study run ${index+1}`,`${row.configuration?.family||'Unknown family'} / LABEL ${row.configuration?.seed??'—'}${row.configuration?.instance_seed!==null&&row.configuration?.instance_seed!==undefined?` / STRUCTURE ${row.configuration.instance_seed}`:''}`),close=element('button','Close run details');close.type='button';
|
| 328 |
+
close.addEventListener('click',()=>{detail.hidden=true;trigger.focus();});heading.append(element('p','The toolbar continues to export and replay the complete parent study receipt.','fineprint'),close);detail.append(heading);renderRun(row,detail);detail.hidden=false;detail.focus();detail.scrollIntoView({block:'start'});
|
| 329 |
+
}
|
| 330 |
+
const all=panel('Individual run receipts',`${rows.length} RUNS`);all.append(table(['Inspect','Family','Structural seed','Label seed','Budget','Policy','Admission','Conditions','Decision','Audit satisfied','Calibration hazardous probes','Deployment hazard','Acquisition calls','Deployment calls'],rows.map((r,i)=>{const button=element('button',`Inspect run ${i+1}`);button.type='button';button.addEventListener('click',()=>inspectRun(r,i,button));return [button,r.configuration?.family,r.configuration?.instance_seed??'—',r.configuration?.seed,r.configuration?.budget,r.configuration?.policy,r.configuration?.admission,conditionLabel(r.configuration),r.decision?.status,r.audit?.contract_satisfied,r.audit?.calibration_hazardous_probes,r.audit?.hazard,r.metrics?.target_calls,r.metrics?.deployment_calls??r.deployment?.target_calls];})));container.append(all,detail);
|
| 331 |
+
}
|
| 332 |
+
function render(){const result=state.completed.result;$('empty-state').hidden=true;$('result-view').hidden=false;$('result-kind').textContent=result.schema==='crosswire-study-v1'?'PAIRED STUDY RECEIPT':'EXPERIMENT RECEIPT';$('result-meta').textContent=result.schema==='crosswire-study-v1'?`Source ${result.source_hash||'unavailable'}`:`${result.configuration?.family||'Unknown family'}${result.configuration?.instance_seed!==null&&result.configuration?.instance_seed!==undefined?` / structural seed ${result.configuration.instance_seed}`:''} / label seed ${result.configuration?.seed??'—'} / ${result.configuration?.implementation||'—'}`;const content=$('result-content');content.replaceChildren();if(result.schema==='crosswire-study-v1')renderStudy(result,content);else renderRun(result,content);}
|
| 333 |
+
$('family').addEventListener('change',familyDescription);
|
| 334 |
+
$('run-form').addEventListener('input',syncControls);
|
| 335 |
+
$('run-form').addEventListener('change',syncControls);
|
| 336 |
+
$('run-form').addEventListener('submit',event=>{event.preventDefault();if($('run-form').reportValidity())startJob('/api/runs',config());});
|
| 337 |
+
$('study-button').addEventListener('click',()=>{syncControls();if($('run-form').reportValidity())startJob('/api/studies',studyConfig(),'study');});
|
| 338 |
+
$('cancel-button').addEventListener('click',async()=>{if(!state.active)return;const id=state.active.id;$('cancel-button').disabled=true;try{await request(`/api/runs/${encodeURIComponent(id)}/cancel`,{method:'POST',body:'{}'});if(state.active?.id===id)$('job-message').textContent='Cancellation requested';}catch(error){if(state.active?.id===id){notice(error.message);$('cancel-button').disabled=false;}else console.warn(`Cancellation request for previous job ${id} failed: ${error.message}`);}});
|
| 339 |
+
$('export-button').addEventListener('click',async()=>{if(!state.completed)return;try{const response=await fetch(`/api/runs/${encodeURIComponent(state.completed.id)}/export`);if(!response.ok)throw new Error(`Export failed (${response.status})`);const text=await response.text(),blob=new Blob([text],{type:'application/json'}),url=URL.createObjectURL(blob),a=element('a');a.href=url;a.download=`crosswire-${state.completed.id}.json`;a.click();setTimeout(()=>URL.revokeObjectURL(url),1000);}catch(error){notice(error.message);}});
|
| 340 |
+
$('replay-button').addEventListener('click',()=>{if(state.completed)startJob(`/api/runs/${encodeURIComponent(state.completed.id)}/replay`,{},'replay');});
|
| 341 |
+
$('import-button').addEventListener('click',()=>$('receipt-file').click());
|
| 342 |
+
$('receipt-file').addEventListener('change',async event=>{
|
| 343 |
+
const file=event.target.files[0];event.target.value='';if(!file||state.active||state.busy)return;
|
| 344 |
+
if(file.size>20*1024*1024){notice('Receipt exceeds the 20 MiB browser import limit.');return;}
|
| 345 |
+
state.busy=true;lock();
|
| 346 |
+
try{
|
| 347 |
+
const text=await file.text();
|
| 348 |
+
try{JSON.parse(text);}catch{throw new Error('This file is not valid JSON.');}
|
| 349 |
+
state.busy=false;await startJob('/api/import',{receipt_text:text},'import');
|
| 350 |
+
}catch(error){notice(`Import failed: ${error.message} The last completed result is preserved.`);}
|
| 351 |
+
finally{state.busy=false;lock();}
|
| 352 |
+
});
|
| 353 |
+
async function initialize(){
|
| 354 |
+
if(location.protocol==='file:'){notice('HTTP worker unavailable: this page was opened directly from a file. Start CROSSWIRE and open the local HTTP address.');$('connection').textContent='HTTP server required';return;}
|
| 355 |
+
try{
|
| 356 |
+
const catalog=await request('/api/catalog');
|
| 357 |
+
if(!Array.isArray(catalog.families)||!Array.isArray(catalog.policies)||!Array.isArray(catalog.admissions))throw new Error('The server catalog is malformed or unsupported.');
|
| 358 |
+
state.catalog=catalog;const defaults=catalog.defaults||{};
|
| 359 |
+
optionList('family',catalog.families,defaults.family||'latch');optionList('policy',catalog.policies,defaults.policy||'task');optionList('admission',catalog.admissions,defaults.admission||'robust');
|
| 360 |
+
if(catalog.execution_modes)optionList('execution_mode',catalog.execution_modes,defaults.execution_mode||'open_loop');
|
| 361 |
+
if(catalog.candidate_backends)optionList('candidate_backend',catalog.candidate_backends,defaults.candidate_backend||'flat');
|
| 362 |
+
if(catalog.recovery_strategies)optionList('recovery_strategy',catalog.recovery_strategies,defaults.recovery_strategy||'stabilize');
|
| 363 |
+
for(const name of ['seed','budget','horizon','implementation','split','probe_safety','risk_tolerance'])if(defaults[name]!==undefined&&$(name))$(name).value=defaults[name];
|
| 364 |
+
if($('anchors'))$('anchors').checked=defaults.anchors===true;if($('temporal'))$('temporal').checked=defaults.temporal===true;if($('escalate'))$('escalate').checked=defaults.escalate===true;
|
| 365 |
+
familyDescription();syncControls();$('connection').textContent=`Local worker · ${catalog.version||'connected'}`;$('connection-dot').classList.add('online');lock();
|
| 366 |
+
}catch(error){notice(error.message);$('connection').textContent='Worker unavailable';}
|
| 367 |
+
}
|
| 368 |
+
initialize();
|