Skip to content

migration D (C2 wave 1): reversible VM opcodes (4 kernels, 4-gate green)#536

Merged
hyperpolymath merged 1 commit into
mainfrom
claude/cool-keller-gr5sl
Jun 5, 2026
Merged

migration D (C2 wave 1): reversible VM opcodes (4 kernels, 4-gate green)#536
hyperpolymath merged 1 commit into
mainfrom
claude/cool-keller-gr5sl

Conversation

@hyperpolymath
Copy link
Copy Markdown
Owner

Phase D / cluster C2 wave 1 of the ReScript → AffineScript → wasm migration of hyperpolymath/idaptik. Continues #535 (Phase B + C1, merged). Staged under proposals/idaptik/migrated/verified + applyable, not applied (idaptik write-access gate).

What landed — the reversible VM value-transform opcodes

The VM stores state in a dict<int> keyed by register name, each instruction a pair of execute/invert closures. Re-decomposition: names + dict stay host-side (senses); the brain is the reversible scalar-int value transform per opcode.

Kernel Brain Reversibility
VmArith ADD/SUB, NEGATE, NOOP, SWAP inverse-pair + self-inverse
VmBitwise FLIP, XOR, ROL, ROR (32-bit) self-inverse + rotation pair
VmAncilla AND, OR (Bennett ancilla) conditional (iff ancilla c==0)
VmInstruction 23-opcode encoding + tier classifier LOSSLESS taxonomy

The defining VM invariant — invert ∘ execute = id — is pinned as explicit *_roundtrip exports (oracle = identity) and swept over i32 extremes.

Four gates green (migrated/EVIDENCE-C2.adoc)

  • G1 compile — 4/4 → wasm
  • G2 parity2100/2100 oracle-vs-wasm inputs, including every reversibility round-trip over i32 extremes (incl. 0x55555555/0xAAAAAAAA and overflow)
  • G3 echo-boundaryVmInstruction 23-opcode encoding Agda-certified LOSSLESS (agda exit 0)
  • G4 assail — 4/4 clean

Compiler findings (worked around; recorded as findings)

Migrating the bitwise opcodes surfaced three AffineScript backend facts:

  1. Unary ~ mis-compiles — emits i32.xor with a missing operand (the wasm won't instantiate). Isolated to unary ~; &/|/^ are sound. → FLIP uses ~a == -a-1 (verified equal + self-inverse). Candidate Phase-F compiler fix.
  2. >> is arithmetic and >>> doesn't exist, but ROL/ROR need a logical shift-right → emulated lsr(a,n) = (a>>n) & ((1<<(32-n))-1); rotations then match JS ((a<<n)|(a>>>(32-n)))|0 and round-trip over the full i32 range.
  3. << and i32 wrap are correct (the VM's word model).

AND/OR are the honest outlier — reversible only via Bennett's ancilla trick under a declared precondition (ancilla starts at 0); every other opcode here is unconditionally reversible.

Ledger

Phase D (C2 wave 1) DONE; migration-map.json C2 → IN_PROGRESS with the wave-1 detail. NEXT: C2 wave 2 — Mul/Div, stack/memory (Push/Pop/Load/Store), control flow (Call/Loop/IfPos/IfZero), I/O (Send/Recv/CoprocessorCall) and the structural VM files; these need an array/linear-memory ABI.

On top of current main (d706d52, post-#535).

https://claude.ai/code/session_01WoKhFQePiRsAj7aqnxbG8s


Generated by Claude Code

Deep wave 2 of the ReScript -> AffineScript -> wasm migration. Cluster C2
(the vm/lib/ocaml reversible instruction set) wave 1: the value-transform
opcodes re-decomposed into four pure-integer kernels staged under
proposals/idaptik/migrated/, each verified by the 4-gate recipe.

The VM stores state in a dict<int> keyed by register NAME, with each
instruction a pair of execute/invert closures. Re-decomposition: the register
names + dict stay host-side (the senses); the brain is the reversible scalar-int
VALUE transform per opcode. "The integer IS the register."

The defining VM invariant is reversibility (invert undoes execute). It is pinned
directly as *_roundtrip exports whose oracle is the identity, and swept in Gate 2
over i32 extremes.

  VmArith       ADD/SUB (inverse pair), NEGATE/NOOP/SWAP (self-inverse)
  VmBitwise     FLIP/XOR (self-inverse), ROL/ROR (inverse pair, 32-bit)
  VmAncilla     AND/OR via Bennett's ancilla (reversible iff ancilla c == 0)
  VmInstruction 23-opcode instruction-set encoding + tier classifier

Four gates green (captured in migrated/EVIDENCE-C2.adoc):
  G1 compile        4/4 -> wasm
  G2 parity         2100/2100 oracle-vs-wasm inputs, incl. every reversibility
                    round-trip (inverse undoes execute) over i32 extremes
  G3 echo-boundary  VmInstruction 23-opcode encoding LOSSLESS (agda exit 0)
  G4 assail         4/4 clean

Three AffineScript backend facts surfaced and worked around (recorded in
EVIDENCE-C2.adoc as findings):
  * unary ~ mis-compiles (i32.xor missing an operand) -> FLIP uses the
    two's-complement identity ~a == -a - 1 (verified equal, self-inverse);
  * >> is arithmetic and >>> does not exist, but ROL/ROR need a logical shift
    right -> emulated as (a >> n) & ((1 << (32 - n)) - 1); rotations then match
    JS ((a<<n)|(a>>>(32-n)))|0 and round-trip over the full i32 range;
  * << and i32 wrap are correct (the VM's word model).
The unary-~ bug is a candidate Phase-F compiler fix.

AND/OR are the honest outlier: reversible only via Bennett's ancilla trick and
only under the declared precondition that the ancilla starts at 0; every other
opcode here is unconditionally reversible.

Wave 2 (deferred — needs an array/linear-memory ABI + control-flow model):
Mul/Div, stack/memory (Push/Pop/Load/Store), control (Call/Loop/IfPos/IfZero),
I/O (Send/Recv/CoprocessorCall), and the structural VM files (State, VM,
SubroutineRegistry, *Coprocessor, bindings).

Ledger: Phase D (C2 wave 1) DONE; migration-map.json C2 -> IN_PROGRESS with the
wave-1 detail. Staged, not applied (idaptik write-access gate, Phase Omega).

https://claude.ai/code/session_01WoKhFQePiRsAj7aqnxbG8s
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 5, 2026

🔍 Hypatia Security Scan

Findings: 87 issues detected

Severity Count
🔴 Critical 2
🟠 High 15
🟡 Medium 70

⚠️ Action Required: Critical security issues found!

View findings
[
  {
    "reason": "Action perpolymath/standards/.github/workflows/governance-reusable.yml@main\n needs attention",
    "type": "unpinned_action",
    "file": "governance.yml",
    "action": "pin_sha",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Action ons/checkout@v6\n    needs attention",
    "type": "unpinned_action",
    "file": "publish-jsr.yml",
    "action": "pin_sha",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Action land/setup-deno@v2\n    needs attention",
    "type": "unpinned_action",
    "file": "publish-jsr.yml",
    "action": "pin_sha",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in affine-vscode-publish.yml",
    "type": "missing_timeout_minutes",
    "file": "affine-vscode-publish.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in casket-pages.yml",
    "type": "missing_timeout_minutes",
    "file": "casket-pages.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in casket-pages.yml",
    "type": "missing_timeout_minutes",
    "file": "casket-pages.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in casket-pages.yml",
    "type": "missing_timeout_minutes",
    "file": "casket-pages.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in ci.yml",
    "type": "missing_timeout_minutes",
    "file": "ci.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in ci.yml",
    "type": "missing_timeout_minutes",
    "file": "ci.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  },
  {
    "reason": "Issue in ci.yml",
    "type": "missing_timeout_minutes",
    "file": "ci.yml",
    "action": "flag",
    "rule_module": "workflow_audit",
    "severity": "medium"
  }
]

Powered by Hypatia Neurosymbolic CI/CD Intelligence

@hyperpolymath hyperpolymath marked this pull request as ready for review June 5, 2026 12:18
@hyperpolymath hyperpolymath merged commit 2a01242 into main Jun 5, 2026
27 checks passed
@hyperpolymath hyperpolymath deleted the claude/cool-keller-gr5sl branch June 5, 2026 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants