Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions interscript-py/tests/test_golden_reverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""WO20 reverse-mode golden capture additivity — the Python mirror of
interscript-rs ``tests/golden_reverse_capture.rs``, the ruby gate
(``tools/isc_wf1_golden_gate.rb --reverse``) and interscript-ts
``test/golden-isc-reverse.test.ts``.

Replays every row of the RS-captured reverse golden file
(``interscript-rs/tests/golden/isc-reverse.jsonl``) through THIS
runtime — parse the sibling maps/libs corpus plus the rs reverse
fixtures, compile, REVERSE the map, execute the reversed main stage on
the row's src — and requires byte-identical output on every row. The
golden was captured once by ``iscrs capture --reverse``; a delta here
is a reverse-parity regression. No adopting-set exemption: the reverse
golden has no adoption flow. An execution error replays as ``null``,
the canonical marker the rs capture records.

The rs reverse fixtures (``tests/reverse_maps``, siblings of the
golden file) join the corpus AFTER it, first-wins, so a corpus map
shadows a same-named fixture exactly like the capture's load path.

Skips when the sibling interscript-rs / maps checkouts are missing
(``GOLDEN_ROOT`` overrides the sibling root, mirroring the forward
gate).
"""

from __future__ import annotations

import json
import os
from pathlib import Path

import pytest

from interscript.executor import execute_stage
from interscript.isc import isc_to_compiled_map, parse_isc
from interscript.reverse import reverse_map

REPO_ROOT = Path(__file__).resolve().parents[3]
SIBLING_ROOT = Path(os.environ.get("GOLDEN_ROOT", str(REPO_ROOT)))
GOLDEN = SIBLING_ROOT / "interscript-rs" / "tests" / "golden" / "isc-reverse.jsonl"
MAPS_DIR = SIBLING_ROOT / "maps" / "maps"
LIBS_DIR = SIBLING_ROOT / "maps" / "libs"
FIXTURES_DIR = SIBLING_ROOT / "interscript-rs" / "tests" / "reverse_maps"


def _sibling_checkouts_present() -> bool:
return GOLDEN.is_file() and MAPS_DIR.is_dir()


def _build_corpus() -> tuple[dict[str, object], object]:
"""Parse + compile every corpus .isc (libs first: dependency
targets validate eagerly) then the rs reverse fixtures FIRST-WINS,
so a corpus map shadows a same-named fixture exactly like the
capture's load path. Dependencies resolve lazily against the same
corpus."""
docs: dict[str, object] = {}

def resolve_dependency(target: str):
return docs.get(target)

for directory in (LIBS_DIR, MAPS_DIR, FIXTURES_DIR):
if not directory.is_dir():
continue
for path in sorted(directory.glob("*.isc")):
if path.stem in docs:
continue
docs[path.stem] = parse_isc(
path.read_text(encoding="utf-8"),
filename=str(path),
resolve_dependency=resolve_dependency,
)
compiled = {stem: isc_to_compiled_map(doc) for stem, doc in docs.items()}

class _Resolver:
"""MapResolver over the in-memory corpus (the MapLoader protocol:
.load(system_code))."""

def load(self, system_code: str):
return compiled[system_code]

return compiled, _Resolver()


@pytest.mark.skipif(
not _sibling_checkouts_present(),
reason="skip: reverse golden capture or sibling maps checkout missing",
)
def test_golden_reverse_capture_additivity() -> None:
rows = [
json.loads(line)
for line in GOLDEN.read_text(encoding="utf-8").splitlines()
if line.strip()
]
assert rows, "reverse golden file has no rows"
compiled, resolver = _build_corpus()

deltas: list[str] = []
failed_maps: set[str] = set()
checked = 0
for row in rows:
map_id = row["map"]
compiled_map = compiled.get(map_id)
if compiled_map is None:
got = "null"
else:
try:
reversed_map = reverse_map(compiled_map)
got = execute_stage(reversed_map, "main", row["src"], resolver)
except Exception: # noqa: BLE001 — mirror the rs/ts/ruby gates
got = "null"
checked += 1
if got == row["out"]:
continue
failed_maps.add(map_id)
if len(deltas) < 10:
deltas.append(
f"{map_id}: {row['src']!r} -> {got!r} (golden {row['out']!r})"
)

assert not failed_maps, (
f"reverse golden divergence on {len(failed_maps)} map(s); "
f"first deltas:\n" + "\n".join(deltas)
)
assert checked == len(rows)
62 changes: 53 additions & 9 deletions tools/isc_wf1_golden_gate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
# --maps-root /path/to/interscript/maps \
# --baseline /path/to/pre-change-ruby.jsonl --parity
#
# --reverse replays the WO20 reverse-mode golden (isc-reverse.jsonl)
# instead: every row runs through the REVERSED document (parse, `#reverse`,
# `Interpreter.call(rdoc)`), byte-identically to the rs capture. The
# reverse golden has no adopting-set exemption — it was captured once
# and must replay identically everywhere; an execution error replays
# as `null`, the canonical marker the rs capture records.
#
# Exit status is non-zero when a gated delta is found.

require "json" # standard:disable Lint/RedundantRequireStatement (not preloaded outside bundler)
Expand All @@ -21,13 +28,15 @@

options = {
golden: "tests/golden/isc-wf0.jsonl",
maps_root: ENV["ISC_MAPS_ROOT"] || "../maps"
maps_root: ENV["ISC_MAPS_ROOT"] || "../maps",
reverse: false
}
OptionParser.new do |o|
o.on("--golden PATH") { |v| options[:golden] = v }
o.on("--maps-root PATH") { |v| options[:maps_root] = v }
o.on("--baseline PATH", "compare against a saved pre-change capture") { |v| options[:baseline] = v }
o.on("--parity", "also compare against the golden 'out' fields") { |v| options[:parity] = true }
o.on("--reverse", "replay the reverse-mode golden (isc-reverse.jsonl)") { |v| options[:reverse] = true }
end.parse!(ARGV)

# The rubygems index is not assumed to work here; wire the registry in
Expand All @@ -41,8 +50,8 @@ def gate_map_gems(yaml_path)
@map_gems = {yaml_path => YAML.load_file(yaml_path)}
end

def gate_map_locations
@map_locations = map_gems.flat_map do |i, v|
def gate_map_locations(locations = nil)
@map_locations = locations || map_gems.flat_map do |i, v|
Array(v["paths"]).map { |j| File.expand_path(j, File.dirname(i)) }
end
end
Expand All @@ -56,26 +65,60 @@ def gate_map_aliases
Interscript.gate_map_locations
Interscript.gate_map_aliases

# The reverse golden covers the rs reverse fixtures too
# (tests/reverse_maps, siblings of the golden file): append them AFTER
# the corpus so a corpus map shadows a same-named fixture exactly like
# the rs capture's load path.
if options[:reverse]
fixtures = File.expand_path(File.join(File.dirname(options[:golden]), "..", "reverse_maps"))
if File.directory?(fixtures)
Interscript.gate_map_locations(
[File.join(maps_root, "maps"), File.join(maps_root, "libs"), fixtures]
)
end
end

# Adopting maps (spec §7): rows of maps whose source uses WF1 constructs
# are exempt from the zero-delta requirement. The corpus has none at
# adoption time; the list lives next to the golden capture
# (interscript-rs tests/golden/adopting-maps.txt) once the first pilot
# lands.
# lands. The reverse golden has no exemption: captured once, replayed
# identically, no adoption flow.
adopting_path = File.join(File.dirname(options[:golden]), "adopting-maps.txt")
adopting = if File.exist?(adopting_path)
adopting = if options[:reverse]
Set.new
elsif File.exist?(adopting_path)
File.readlines(adopting_path, encoding: "UTF-8")
.reject { |l| l.start_with?("#") || l.strip.empty? }
.map(&:strip).to_set
else
Set.new
end

# Missing golden = missing sibling checkout: skip like the rs/ts/py
# gates do instead of crashing (the reverse golden lands on rs main
# with the WO20 branch; until then this gate is a no-op).
unless File.exist?(options[:golden])
puts "skip: no golden capture at #{options[:golden]}"
exit 0
end

rows = File.readlines(options[:golden], encoding: "UTF-8").map { |l| JSON.parse(l) }
captured = rows.map do |row|
out = begin
Interscript.transliterate(row["map"], row["src"])
if options[:reverse]
# WO20: parse (the DSL cache makes re-parses free), reverse the
# document, run the row's src through the reversed main stage —
# the exact capture procedure, mirrored.
doc = Interscript::DSL.parse(row["map"])
Interscript::Interpreter.call(doc.reverse).call(row["src"])
else
Interscript.transliterate(row["map"], row["src"])
end
rescue StandardError, NotImplementedError => e
"ERROR:#{e.class}:#{e.message.lines.first.to_s.strip[0, 160]}"
# Forward keeps its self-comparison marker; the reverse gate maps
# errors to `null`, the canonical marker the rs capture records.
options[:reverse] ? "null" : "ERROR:#{e.class}:#{e.message.lines.first.to_s.strip[0, 160]}"
end
{map: row["map"], src: row["src"], out: out, golden: row["out"]}
end
Expand All @@ -94,12 +137,13 @@ def gate_map_aliases
puts " #{g[:map]} #{g[:src].inspect}: baseline=#{b["out"].inspect} now=#{g[:out].inspect}"
end
end
if options[:parity]
if options[:parity] || options[:reverse]
puts "delta vs golden (cross-runtime): #{delta_vs_golden.size}"
delta_vs_golden.first(10).each do |r|
puts " #{r[:map]} #{r[:src].inspect}: golden=#{r[:golden].inspect} ruby=#{r[:out].inspect}"
end
end

failed = (delta_vs_baseline || []).any? || (options[:parity] ? delta_vs_golden.any? : false)
failed = (delta_vs_baseline || []).any? ||
((options[:parity] || options[:reverse]) ? delta_vs_golden.any? : false)
exit(failed ? 1 : 0)
Loading